DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKL_STREAM_GENERATOR_PVT

Source


1 PACKAGE BODY OKL_STREAM_GENERATOR_PVT AS
2 /* $Header: OKLRSGPB.pls 120.123.12020000.4 2012/10/17 11:43:28 vloomba ship $ */
3 
4   --Added for debug_logging
5   L_DEBUG_ENABLED VARCHAR2(10) := OKL_DEBUG_PUB.CHECK_LOG_ENABLED;
6   G_STREAM_ACTIVITY_WORK       CONSTANT VARCHAR2(30) := 'WORK';
7 
8   TYPE interim_interest_rec_type IS RECORD (cf_days NUMBER, cf_amount NUMBER, cf_dpp NUMBER);
9   TYPE interim_interest_tbl_type IS TABLE OF interim_interest_rec_type INDEX BY BINARY_INTEGER;
10   -- Added by RGOOTY : Start
11 
12   CURSOR G_SRV_ASSETS_EXISTS_CSR( chrId NUMBER ) IS
13   select 'T' flag
14   from dual where exists
15    (
16     select 1
17     from  okc_k_lines_b kle,
18           okc_line_styles_b lse,
19           okc_statuses_b sts
20     where KLE.LSE_ID = LSE.ID
21       and lse.lty_code = 'SOLD_SERVICE'
22       and kle.chr_id = chrId
23       and sts.code = kle.sts_code
24       and sts.ste_code not in ('HOLD', 'TERMINATED', 'EXPIRED', 'CANCELLED')
25    );
26 
27   G_ORG_ID              OKC_K_HEADERS_B.AUTHORING_ORG_ID%TYPE;
28   G_CURRENCY_CODE       OKC_K_HEADERS_B.CURRENCY_CODE%TYPE;
29   G_DIFF_LOOKUP_CODE    FND_LOOKUPS.LOOKUP_CODE%TYPE;
30   G_PRECISION           NUMBER;
31   G_ROUNDING_RULE       OKL_SYS_ACCT_OPTS.AEL_ROUNDING_RULE%TYPE;
32 
33   CURSOR G_HDR( p_khr_id OKC_K_HEADERS_V.ID%TYPE) IS
34     select chr.orig_system_source_code,
35         chr.start_date,
36         chr.end_date,
37         chr.template_yn,
38 	    chr.authoring_org_id,
39         chr.inv_organization_id,
40         khr.deal_type,
41         to_char(pdt.id)  pid,
42         nvl(pdt.reporting_pdt_id, -1) report_pdt_id,
43         chr.currency_code currency_code,
44         khr.term_duration term
45     from  okc_k_headers_v chr,
46           okl_k_headers khr,
47           okl_products_v pdt
48     where khr.id = chr.id
49       and chr.id = p_khr_id
50       and khr.pdt_id = pdt.id(+);
51     r_hdr G_HDR%ROWTYPE;
52 
53     Cursor G_ROLLOVER_PMNTS(p_khr_id  OKC_RULE_GROUPS_B.dnz_chr_id%type ) IS
54     Select distinct nvl(slh.object1_id1, -1) styId
55     From   OKC_RULE_GROUPS_B rgp,
56            OKC_RULES_B sll,
57            okc_rules_b slh,
58 	   okl_strm_type_b sty
59     Where  slh.rgp_id = rgp.id
60        and rgp.RGD_CODE = 'LALEVL'
61        and sll.RULE_INFORMATION_CATEGORY = 'LASLL'
62        and slh.RULE_INFORMATION_CATEGORY = 'LASLH'
63        AND TO_CHAR(slh.id) = sll.object2_id1
64        and slh.object1_id1 = sty.id
65        and sty.stream_type_purpose = 'RENT'
66        and rgp.dnz_chr_id = p_khr_id;
67 
68     r_rollover_pmnts G_ROLLOVER_PMNTS%ROWTYPE;
69   -- Added by RGOOTY : End
70 
71   G_API_TYPE                CONSTANT  VARCHAR2(4) := '_PVT';
72   G_IA_STS_NEW              CONSTANT VARCHAR2(6)  := okl_pool_pvt.G_POL_STS_NEW;
73   G_IA_STS_ACTIVE           CONSTANT VARCHAR2(6)  := okl_pool_pvt.G_POL_STS_ACTIVE;
74   G_IA_STS_INACTIVE         CONSTANT VARCHAR2(8)  := okl_pool_pvt.G_POL_STS_INACTIVE;
75   G_IA_STS_EXPIRED          CONSTANT VARCHAR2(10) := okl_pool_pvt.G_POL_STS_EXPIRED;
76   --sosharma 14-12-2007 ,Added pending status
77     G_PC_STS_PENDING         CONSTANT VARCHAR2(8)  := 'PENDING';
78 
79   -- Added by RGOOTY: Start  4371472
80   -- This add_months_new is modified to handle even the
81   --   negative p_months_after ...
82   -- Hence, we can use add_months_new to calculate the months before
83   --  p_start_date
84   PROCEDURE add_months_new(
85     p_start_date     IN  DATE,
86     p_months_after   IN  NUMBER,
87     x_date           OUT NOCOPY DATE,
88     x_return_status  OUT NOCOPY VARCHAR2)
89   IS
90     l_day              NUMBER;
91     l_month            NUMBER;
92     l_year             NUMBER;
93     l_year_inc         NUMBER;
94 
95     l_api_name         VARCHAR2(30) := 'add_months_new';
96     l_return_status    VARCHAR2(1);
97   BEGIN
98     -- Initialize the status
99     l_return_status := OKL_API.G_RET_STS_SUCCESS;
100 
101     l_month := to_char( p_start_date, 'MM'   );
102     l_year  := to_char( p_start_date, 'YYYY' );
103     l_day   := to_char( p_start_date, 'DD'   );
104     --print( l_api_name || ': ' || 'l_month | l_year | l_day | p_months_after' );
105     --print( l_api_name || ': ' || '1 |' || l_month || '|' ||  l_year || '|' ||  l_day || '|' || p_months_after );
106 
107     l_month := l_month + nvl(p_months_after,0);
108     --print( l_api_name || ': ' ||  '2 |' || l_month || '|' ||  l_year || '|' ||  l_day || '|' || p_months_after );
109 
110     IF l_month > 0
111     THEN
112       l_year := l_year + floor( (l_month - 1) / 12);
113       --print( l_api_name || ': ' || '3 |' || l_month || '|' ||  l_year || '|' ||  l_day || '|' || p_months_after );
114       l_month := mod(l_month ,12 );
115       --print( l_api_name || ': ' || '4 |' || l_month || '|' ||  l_year || '|' ||  l_day || '|' || p_months_after );
116       IF l_month = 0
117       THEN
118         l_month := 12;
119       END IF;
120     ELSE
121       l_year_inc := ceil( (ABS(l_month) + 1 ) / 12 );
122       l_year := l_year - l_year_inc;
123       --print( l_api_name || ': ' || '3 |' || l_month || '|' ||  l_year || '|' ||  l_day || '|' || p_months_after );
124       l_month := 12 * l_year_inc  - ABS(l_month);
125       IF l_month = 13
126       THEN
127         l_month := 1;
128       END IF;
129       --print( l_api_name || ': ' || '4 |' || l_month || '|' ||  l_year || '|' ||  l_day || '|' || p_months_after );
130     END IF;
131     CASE
132       -- Considering the cases where 30 is the last day in the month
133       WHEN  ( l_month = 4  OR l_month = 6 OR
134               l_month = 9  OR l_month = 11 ) AND
135             l_day > 30
136       THEN
137         l_day := 30;
138       WHEN l_month = 2
139        AND l_day > 28
140       THEN
141         -- Considering the cases where day > 28 and month is February in a leap/Non Leap Year
142         IF  mod(l_year,400 ) = 0 OR
143              ( mod(l_year, 100) <> 0 AND mod(l_year,4) = 0 )
144         THEN
145           -- Leap Year is divisible by 4, but not with 100 except for the years which are divisible by 400
146           -- Like 1900 is not leap year, but 2000 is a leap year
147           l_day := 29;
148         ELSE
149           -- Its a non Leap Year
150           l_day := 28;
151         END IF;
152       ELSE
153         -- Do Nothing
154         NULL;
155     END CASE;
156     --print( l_api_name || ': ' || '5 |' || l_month || '|' ||  l_year || '|' ||  l_day || '|' || p_months_after );
157     -- Return things
158     x_date := to_date( l_day || '-' || l_month || '-' || l_year, 'DD-MM-YYYY' );
159     x_return_status := l_return_status;
160   EXCEPTION
161     WHEN OKL_API.G_EXCEPTION_ERROR THEN
162       x_return_status := G_RET_STS_ERROR;
163     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
164       x_return_status := G_RET_STS_UNEXP_ERROR;
165     WHEN OTHERS THEN
166       OKL_API.SET_MESSAGE (
167         p_app_name     => G_APP_NAME,
168         p_msg_name     => G_DB_ERROR,
169         p_token1       => G_PROG_NAME_TOKEN,
170         p_token1_value => l_api_name,
171         p_token2       => G_SQLCODE_TOKEN,
172         p_token2_value => sqlcode,
173         p_token3       => G_SQLERRM_TOKEN,
174         p_token3_value => sqlerrm);
175       x_return_status := G_RET_STS_UNEXP_ERROR;
176   END add_months_new;
177 
178   -- API calculating the Stream element date after
179   --  (p_periods_after * p_months_per_period ) months
180   --  from p_start_date, also considering the payments are
181   --  Advance/Arrears !!
182 
183 -- Added parameter p_recurrence_date by djanaswa for bug 6007644
184 -- Added parameter p_arrears_pay_dates_option DJANASWA ER6274342
185   PROCEDURE get_sel_date(
186     p_start_date         IN  DATE,
187     p_advance_or_arrears IN  VARCHAR2,
188     p_periods_after      IN  NUMBER,
189     p_months_per_period  IN  NUMBER,
190     x_date               OUT NOCOPY DATE,
191     x_return_status      OUT NOCOPY VARCHAR2,
192     p_recurrence_date    IN  DATE,
193     p_arrears_pay_dates_option IN VARCHAR2)
194   AS
195     l_start_date         DATE;
196     l_start_day          NUMBER;
197     l_temp_day           NUMBER;
198     l_temp_date          DATE;
199     l_month              NUMBER;
200     l_api_name           VARCHAR2(30) := 'get_sel_date';
201     l_return_status      VARCHAR2(1);
202 
203     -- Added by djanaswa for bug 6007644
204     l_recurrence_day     NUMBER;
205     l_temp_month         NUMBER;
206     l_temp_year          NUMBER;
207     --end djanaswa
208 
209 
210 
211 
212   BEGIN
213     -- Initialize Things ...
214     l_start_date := p_start_date;
215     l_return_status := OKL_API.G_RET_STS_SUCCESS;
216     l_start_date := trunc(p_start_date);
217     l_start_day  := to_char( l_start_date, 'DD');
218     l_month := to_char(p_start_date, 'MM');
219 
220     -- Added by djanaswa for bug 6007644
221     l_month := to_char(p_recurrence_date, 'MM');
222     l_recurrence_day := to_char(p_recurrence_date, 'DD');
223     --end djanaswa
224 
225     IF p_advance_or_arrears = 'ARREARS' OR
226        p_advance_or_arrears = 'Y'
227     THEN
228        add_months_new(
229           p_start_date     => l_start_date,
230           p_months_after   => p_periods_after * p_months_per_period,
231           x_date           => l_temp_date,
232           x_return_status  => l_return_status);
233        IF(l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
234          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
235        ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
236          RAISE OKL_API.G_EXCEPTION_ERROR;
237        END IF;
238     --   IF to_char( l_temp_date, 'DD' ) >= l_start_day
239     --   THEN
240     --     l_temp_date := l_temp_date - 1;
241     --   END IF;
242     ELSE
243       add_months_new(
244         p_start_date     => l_start_date,
245         p_months_after   => (p_periods_after-1)* p_months_per_period,
246         x_date           => l_temp_date,
247         x_return_status  => l_return_status);
248       IF(l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
249         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
250       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
251         RAISE OKL_API.G_EXCEPTION_ERROR;
252       END IF;
253     END IF;   -- changed by djanaswa for bug 6007644
254 
255       -- Note:
256       --   The logic below is to address the rule, that if the start date is last day of a month
257       --    then the succeeding advance payments should be on the last day of the month..
258       --   Please remove the below IF clause, if the above mentioned rule is not applicable.
259 
260       -- Added p_recurrence_date in IF clause by djanaswa for bug 6007644
261 
262       IF l_month in (1,  3 , 5 , 7, 8, 10, 12) THEN
263         IF p_recurrence_date = LAST_DAY( p_recurrence_date )
264         THEN
265           l_temp_date := LAST_DAY( l_temp_date );
266         END IF;
267       END IF;
268 
269       -- Added by djanaswa for bug 6007644
270     IF(l_recurrence_day in(29, 30)) THEN
271         l_temp_month := to_char(l_temp_date, 'MM');
272         l_temp_year  := to_char(l_temp_date, 'YYYY');
273         IF(l_temp_month = 2) THEN
274           IF  mod(l_temp_year,400 ) = 0 OR (mod(l_temp_year, 100) <> 0 AND mod(l_temp_year,4) = 0)
275           THEN
276             -- Leap Year is divisible by 4, but not with 100 except for the years which are divisible by 400
277             -- Like 1900 is not leap year, but 2000 is a leap year
278             l_temp_day := 29;
279           ELSE
280             -- Its a non Leap Year
281             l_temp_day := 28;
282           END IF;
283         ELSE
284           l_temp_day := l_recurrence_day;
285         END IF;
286         l_temp_date := to_date(l_temp_day || '-' || l_temp_month || '-' || l_temp_year, 'DD-MM-YYYY');
287       END IF;
288 
289  -- djanaswa 6274342 start
290      IF p_advance_or_arrears = 'ARREARS' OR
291          p_advance_or_arrears = 'Y'
292       THEN
293           IF p_arrears_pay_dates_option = 'FIRST_DAY_OF_NEXT_PERIOD' THEN
294                 l_temp_date := l_temp_date;
295           ELSE
296                 l_temp_date := l_temp_date - 1;
297           END IF;
298       END IF;
299  -- djanaswa 6274342 end
300       --end djanaswa
301 
302 
303     -- Return the things
304     x_date := l_temp_date;
305     x_return_status := l_return_status;
306   EXCEPTION
307     WHEN OKL_API.G_EXCEPTION_ERROR THEN
308       x_return_status := G_RET_STS_ERROR;
309     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
310       x_return_status := G_RET_STS_UNEXP_ERROR;
311     WHEN OTHERS THEN
312       OKL_API.SET_MESSAGE (
313         p_app_name     => G_APP_NAME,
314         p_msg_name     => G_DB_ERROR,
315         p_token1       => G_PROG_NAME_TOKEN,
316         p_token1_value => l_api_name,
317         p_token2       => G_SQLCODE_TOKEN,
318         p_token2_value => sqlcode,
319         p_token3       => G_SQLERRM_TOKEN,
320         p_token3_value => sqlerrm);
321       x_return_status := G_RET_STS_UNEXP_ERROR;
322   END get_sel_date;
323  -- Added by RGOOTY: End  4371472
324 
325  ---------------------------------------------------------------------------
326   -- PROCEDURE get_mapped_stream
327   --
328   -- DESC
329   -- Repository of hard-coded stream type mapping
330   --
331   -- USAGE
332   -- Driven by mapping type.  Must provide mapping type
333   ---------------------------------------------------------------------------
334   PROCEDURE get_mapped_stream(p_sty_purpose       IN         VARCHAR2  DEFAULT NULL,
335                                p_line_style        IN         VARCHAR2,
336                                p_mapping_type      IN         VARCHAR2,
337                                p_deal_type         IN         VARCHAR2,
338                                p_primary_sty_id    IN         NUMBER DEFAULT NULL,
339                                p_fee_type          IN         VARCHAR2 DEFAULT NULL,
340                                p_recurr_yn         IN         VARCHAR2 DEFAULT NULL,
341                                p_pt_yn             IN         VARCHAR2  DEFAULT 'N',
342                                p_khr_id            IN         NUMBER,
343                                x_mapped_stream     OUT NOCOPY VARCHAR2,
344                                x_return_status     OUT NOCOPY VARCHAR2,
345                                p_hdr IN G_HDR%ROWTYPE,
346                                p_rollover_pmnts IN G_ROLLOVER_PMNTS%ROWTYPE) IS
347 
348     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_mapped_stream';
349     l_sty_id NUMBER;
350     l_primary_sty_id    NUMBER;
351     p_get_k_info_csr    OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR%ROWTYPE;
352   BEGIN
353     -- Enhancement pending to remove hard coding dependency (TBD by MCHAKRAB, SRAWLING)
354     -- Accrual Stream Type will be specified during setup of Payment Stream Type
355     IF p_rollover_pmnts.styId IS NULL
356     THEN
357         OPEN G_ROLLOVER_PMNTS(p_khr_id);
358         FETCH G_ROLLOVER_PMNTS INTO r_rollover_pmnts;
359         CLOSE G_ROLLOVER_PMNTS;
360     ELSE
361         r_rollover_pmnts := p_rollover_pmnts;
362     END IF;
363 
364     OPEN OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR (p_khr_id);
365     FETCH OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR INTO p_get_k_info_csr;
366     CLOSE OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR;
367 
368     l_primary_sty_id := nvl(p_primary_sty_id, r_rollover_pmnts.styId);
369 
370    -- print( l_prog_name, 'begin' );
371    -- print( l_prog_name, ' mapping type ' || p_mapping_type || ' sty ame ' || p_sty_purpose || ' deal type ' || p_deal_type );
372     IF p_mapping_type = 'ACCRUAL' THEN
373           IF p_sty_purpose = 'RENT' AND p_deal_type = 'LEASEOP' AND
374              (p_line_style = 'FREE_FORM1' OR p_line_style IS NULL) THEN
375                 --x_mapped_stream  :=  'RENTAL ACCRUAL';
376                 --  print( l_prog_name, '##1##' );
377                 OKL_ISG_UTILS_PVT.get_dep_stream_type(
378                          p_khr_id                => p_khr_id,
379                    					 p_deal_type             => p_deal_type,
380                    					 p_primary_sty_id        => l_primary_sty_id,
381                          p_dependent_sty_purpose => 'RENT_ACCRUAL',
382                          x_return_status         => x_return_status,
383                          x_dependent_sty_id      => l_sty_id,
384                          x_dependent_sty_name    => x_mapped_stream,
385                          p_get_k_info_rec        => p_get_k_info_csr);
386 
387           ELSIF p_sty_purpose = 'RENT' AND p_deal_type <> 'LEASEOP' AND
388                 (p_line_style = 'FREE_FORM1' OR p_line_style IS NULL) THEN
389                 -- print( l_prog_name, '##2##' );
390                 x_mapped_stream  :=  NULL;
391           ELSIF p_pt_yn = 'Y' THEN
392                 --  print( l_prog_name, '##3##' );
393                 --x_mapped_stream := 'PASS THROUGH REVENUE ACCRUAL';
394                 --Bug 4434343 - Start of Changes
395                 IF (p_line_style = 'FEE') THEN
396                     OKL_ISG_UTILS_PVT.get_dependent_stream_type(
397                                          p_khr_id                => p_khr_id,
398                                    					 p_deal_type             => p_deal_type,
399                                    					 p_primary_sty_id        => l_primary_sty_id,
400                                          p_dependent_sty_purpose => 'PASS_THRU_REV_ACCRUAL',
401                                          x_return_status         => x_return_status,
402                                          x_dependent_sty_id      => l_sty_id,
403                                          x_dependent_sty_name    => x_mapped_stream);
404                 ELSIF (p_line_style = 'SOLD_SERVICE') OR (p_line_style = 'LINK_SERV_ASSET') THEN
405                      OKL_ISG_UTILS_PVT.get_dependent_stream_type(
406                                          p_khr_id                => p_khr_id,
407                                    					 p_deal_type             => p_deal_type,
408                                    					 p_primary_sty_id        => l_primary_sty_id,
409                                          p_dependent_sty_purpose => 'PASS_THRU_SVC_REV_ACCRUAL',
410                                          x_return_status         => x_return_status,
411                                          x_dependent_sty_id      => l_sty_id,
412                                          x_dependent_sty_name    => x_mapped_stream);
413                END IF;
414                --Bug 4434343 - End of Changes
415           ELSIF ((p_line_style = 'SOLD_SERVICE') OR (p_line_style = 'LINK_SERV_ASSET')) THEN
416                 --  print( l_prog_name, '##4##' );
417                 --x_mapped_stream  :=  'SERVICE INCOME';
418                 OKL_ISG_UTILS_PVT.get_dep_stream_type(
419                         p_khr_id                => p_khr_id,
420                    					p_deal_type             => p_deal_type,
421                    					p_primary_sty_id        => l_primary_sty_id,
422                         p_dependent_sty_purpose => 'SERVICE_INCOME',
423                         x_return_status         => x_return_status,
424                         x_dependent_sty_id      => l_sty_id,
425                         x_dependent_sty_name    => x_mapped_stream,
426                         p_get_k_info_rec        => p_get_k_info_csr);
427 
428           ELSIF (p_line_style IN ('FEE', 'FREE_FORM1') OR p_line_style IS NULL) AND
429                 (p_sty_purpose <> 'SECURITY_DEPOSIT') THEN
430 
431             If ( p_fee_type = 'INCOME' AND p_recurr_yn = OKL_API.G_FALSE ) Then
432                 --x_mapped_stream := 'AMORTIZED FEE INCOME';
433                 OKL_ISG_UTILS_PVT.get_dep_stream_type(
434                         p_khr_id                => p_khr_id,
435                    					p_deal_type             => p_deal_type,
436                    					p_primary_sty_id        => l_primary_sty_id,
437                         p_dependent_sty_purpose => 'AMORTIZE_FEE_INCOME',
438                         x_return_status         => x_return_status,
439                         x_dependent_sty_id      => l_sty_id,
440                         x_dependent_sty_name    => x_mapped_stream,
441                         p_get_k_info_rec        => p_get_k_info_csr);
442 
443     	   elsif ( p_fee_type = 'FINANCED' OR p_fee_type = 'ROLLOVER') Then
444     	       x_mapped_stream := NULL;
445     	   else
446                 --x_mapped_stream  :=  'FEE INCOME';
447                  OKL_ISG_UTILS_PVT.get_dep_stream_type(
448                          p_khr_id                => p_khr_id,
449                    					 p_deal_type             => p_deal_type,
450                    					 p_primary_sty_id        => l_primary_sty_id,
451                          p_dependent_sty_purpose => 'ACCRUED_FEE_INCOME',
452                          x_return_status         => x_return_status,
453                          x_dependent_sty_id      => l_sty_id,
454                          x_dependent_sty_name    => x_mapped_stream,
455                          p_get_k_info_rec        => p_get_k_info_csr);
456 
457     	   end if;
458         ELSE
459             x_mapped_stream  :=  NULL;
460         END IF;
461 
462     ELSIF p_mapping_type = 'PRE-TAX INCOME' THEN
463 
464       IF (p_deal_type IN ('LOAN', 'LOAN-REVOLVING')) OR
465          ( p_fee_type in ('FINANCED', 'ROLLOVER','LINK_FEE_ASSET') ) THEN
466             --x_mapped_stream  :=  'PRE-TAX INCOME';
467             OKL_ISG_UTILS_PVT.get_dep_stream_type(
468                     p_khr_id                => p_khr_id,
469                					p_deal_type             => p_deal_type,
470                					p_primary_sty_id        => l_primary_sty_id,
471                     p_dependent_sty_purpose => 'INTEREST_INCOME',
472                     x_return_status         => x_return_status,
473                     x_dependent_sty_id      => l_sty_id,
474                     x_dependent_sty_name    => x_mapped_stream,
475                     p_get_k_info_rec        => p_get_k_info_csr);
476 
477       ELSIF (p_deal_type IN ('LEASEDF', 'LEASEST')) THEN
478             --x_mapped_stream  :=  'PRE-TAX INCOME';
479             OKL_ISG_UTILS_PVT.get_dep_stream_type(
480                     p_khr_id                => p_khr_id,
481                					p_deal_type             => p_deal_type,
482                 				p_primary_sty_id        => l_primary_sty_id,
483                     p_dependent_sty_purpose => 'LEASE_INCOME',
484                     x_return_status         => x_return_status,
485                     x_dependent_sty_id      => l_sty_id,
486                     x_dependent_sty_name    => x_mapped_stream,
487                     p_get_k_info_rec        => p_get_k_info_csr);
488 
489       ELSE
490         x_mapped_stream  :=  NULL;
491       END IF;
492 
493     ELSIF p_mapping_type = 'PAYMENT' THEN
494       IF p_sty_purpose = 'RENT' AND p_deal_type IN ('LOAN', 'LOAN-REVOLVING') THEN
495         x_mapped_stream  := 'LOAN PAYMENT';
496       ELSE
497         x_mapped_stream  :=  p_sty_purpose;
498       END IF;
499 
500     ELSIF p_mapping_type = 'CAPITAL RECOVERY' THEN
501       IF p_deal_type IN ('LEASEDF', 'LEASEST', 'LOAN', 'LOAN-REVOLVING') THEN
502         x_mapped_stream  :=  'PRINCIPAL PAYMENT';
503       ELSE
504         x_mapped_stream  :=  NULL;
505       END IF;
506 
507     ELSIF p_mapping_type = 'INTEREST PAYMENT' THEN
508       IF p_deal_type IN ('LEASEDF', 'LEASEST', 'LOAN', 'LOAN-REVOLVING') THEN
509         x_mapped_stream  :=  'INTEREST PAYMENT';
510       ELSE
511         x_mapped_stream  :=  NULL;
512       END IF;
513 
514     ELSIF p_mapping_type = 'CLOSE BOOK' THEN
515       IF p_deal_type IN ('LEASEDF', 'LEASEST', 'LOAN', 'LOAN-REVOLVING') THEN
516         x_mapped_stream  :=  'PRINCIPAL BALANCE';
517       ELSE
518         x_mapped_stream  :=  NULL;
519       END IF;
520 
521     ELSIF p_mapping_type = 'DISBURSEMENT' THEN
522         IF p_hdr.pid IS NULL
523         THEN
524           OPEN G_HDR(p_khr_id);
525           FETCH G_HDR INTO r_hdr;
526           CLOSE G_HDR;
527         ELSE
528           r_hdr := p_hdr;
529         END IF;
530 
531       If ( p_sty_purpose = 'RENT' ) THen
532             --x_mapped_stream  :=  'INVESTOR RENT DISBURSEMENT BASIS';
533             OKL_ISG_UTILS_PVT.get_primary_stream_type(
534                     p_khr_id              => p_khr_id,
535                					p_pdt_id              => r_hdr.pid,
536                     p_primary_sty_purpose => 'INVESTOR_RENT_DISB_BASIS',
537                     x_return_status       => x_return_status,
538                     x_primary_sty_id      => l_sty_id,
539                     x_primary_sty_name    => x_mapped_stream);
540       ElsIf ( p_sty_purpose = 'RESIDUAL' ) Then
541             --x_mapped_stream  :=  'INVESTOR RESIDUAL DISBURSEMENT BASIS';
542             OKL_ISG_UTILS_PVT.get_primary_stream_type(
543                     p_khr_id              => p_khr_id,
544                					p_pdt_id              => r_hdr.pid,
545                     p_primary_sty_purpose => 'INVESTOR_RESIDUAL_DISB_BASIS',
546                     x_return_status       => x_return_status,
547                     x_primary_sty_id      => l_sty_id,
548                     x_primary_sty_name    => x_mapped_stream);
549       End If;
550 
551     ELSIF p_mapping_type = 'PV_DISBURSEMENT' THEN
552         IF p_hdr.pid IS NULL
553         THEN
554           OPEN G_HDR(p_khr_id);
555           FETCH G_HDR INTO r_hdr;
556           CLOSE G_HDR;
557         ELSE
558           r_hdr := p_hdr;
559         END IF;
560 
561       If ( p_sty_purpose = 'RENT' ) THen
562             --x_mapped_stream  :=  'PRESENT VALUE SECURITIZED RENT';
563             OKL_ISG_UTILS_PVT.get_primary_stream_type(
564                     p_khr_id              => p_khr_id,
565                					p_pdt_id              => r_hdr.pid,
566                     p_primary_sty_purpose => 'PV_RENT_SECURITIZED',
567                     x_return_status       => x_return_status,
568                     x_primary_sty_id      => l_sty_id,
569                     x_primary_sty_name    => x_mapped_stream);
570       ElsIf ( p_sty_purpose = 'RESIDUAL' ) Then
571             --x_mapped_stream  :=  'PRESENT VALUE SECURITIZED RESIDUAL';
572             OKL_ISG_UTILS_PVT.get_primary_stream_type(
573                     p_khr_id              => p_khr_id,
574                					p_pdt_id              => r_hdr.pid,
575                     p_primary_sty_purpose => 'PV_RV_SECURITIZED',
576                     x_return_status       => x_return_status,
577                     x_primary_sty_id      => l_sty_id,
578                     x_primary_sty_name    => x_mapped_stream);
579       End If;
580 
581     ELSE
582         x_mapped_stream  :=  NULL;
583     END IF;
584     x_return_status  :=  G_RET_STS_SUCCESS;
585    -- print( l_prog_name, 'end' );
586   EXCEPTION
587 
588     WHEN OKL_API.G_EXCEPTION_ERROR THEN
589 
590       x_return_status := G_RET_STS_ERROR;
591 
592     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
593 
594       x_return_status := G_RET_STS_UNEXP_ERROR;
595 
596     WHEN OTHERS THEN
597 
598       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
599                            p_msg_name     => G_DB_ERROR,
600                            p_token1       => G_PROG_NAME_TOKEN,
601                            p_token1_value => l_prog_name,
602                            p_token2       => G_SQLCODE_TOKEN,
603                            p_token2_value => sqlcode,
604                            p_token3       => G_SQLERRM_TOKEN,
605                            p_token3_value => sqlerrm);
606 
607       x_return_status := G_RET_STS_UNEXP_ERROR;
608 
609   END get_mapped_stream;
610 
611   Procedure print( p_proc_name     IN VARCHAR2,
612                    p_message       IN VARCHAR2,
613                    x_return_status IN VARCHAR2) IS
614   Begin
615      IF L_DEBUG_ENABLED = 'Y' then
616        fnd_file.put_line (fnd_file.log, p_proc_name || ':' || p_message);
617      END IF;
618      --dbms_output.put_line( p_proc_name||':'||p_message||':'||x_return_status );
619   End;
620 
621   Procedure print( p_proc_name     IN VARCHAR2,
622                    p_message       IN VARCHAR2) IS
623   Begin
624      -- print(p_proc_name, p_message, 'S' );
625      IF L_DEBUG_ENABLED = 'Y' then
626        fnd_file.put_line (fnd_file.log, p_proc_name || ':' || p_message);
627      END IF;
628   End;
629 
630   Procedure generate_stub_element(p_khr_id   IN NUMBER,
631                                   p_deal_type     IN VARCHAR2,
632                                   p_api_version   IN  NUMBER,
633                                   p_init_msg_list IN  VARCHAR2,
634                                   x_msg_count     OUT NOCOPY NUMBER,
635                                   x_msg_data      OUT NOCOPY VARCHAR2,
636                                   x_return_status OUT NOCOPY VARCHAR2,
637 				  x_se_id         OUT NOCOPY NUMBER) Is
638 
639     l_prog_name     CONSTANT VARCHAR2(100) := G_PKG_NAME||'.'||'generate_stub_element';
640     p_selv_tbl      OKL_STREAMS_PUB.selv_tbl_type;
641     x_selv_tbl      OKL_STREAMS_PUB.selv_tbl_type;
642     p_stmv_rec      OKL_STREAMS_PUB.stmv_rec_type;
643     x_stmv_rec      OKL_STREAMS_PUB.stmv_rec_type;
644     l_sty_id        NUMBER;
645 
646 --prasjain:start for bug 5474827
647 /*
648     Cursor c_sty IS
649         SELECT sel.id
650         FROM  okl_streams stm,
651               okl_strm_elements sel
652         where stm.khr_id = p_khr_id
653           and stm.say_code     =  'HIST'
654           and stm.SGN_CODE     =  'MANL'
655           and stm.active_yn    =  'N'
656           and stm.purpose_code =  'STUBS'
657           and stm.comments     =  'STUB STREAMS'
658           and sel.stm_id = stm.id;
659 */
660 --prasjain:End
661 
662 
663     error           VARCHAR2(256);
664     l_stream_name   VARCHAR2(256);
665 
666   Begin
667     --print( l_prog_name, ' deal type ' || p_deal_type );
668     -- Utility API to get the Stream Type Id of the RENT Stream Type.
669     OKL_ISG_UTILS_PVT.get_primary_stream_type(
670         p_khr_id              => p_khr_id,
671         p_deal_type           => p_deal_type,
672         p_primary_sty_purpose => 'RENT',
673         x_return_status       => x_return_status,
674         x_primary_sty_id      => l_sty_id,
675         x_primary_sty_name    => l_stream_name);
676 --     print( l_prog_name, ' stream name ' || l_stream_name );
677     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
678         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
679     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
680         RAISE OKL_API.G_EXCEPTION_ERROR;
681     END IF;
682 
683     -- Creates a new Stream Header record populating the input values passed
684     -- and generates a transaction number for the header.
685     get_stream_header(
686         p_khr_id         =>   p_khr_id,
687         p_kle_id         =>   NULL,
688         p_sty_id         =>   l_sty_id,
689         p_purpose_code   =>   'STUBS',
690         x_stmv_rec       =>   p_stmv_rec,
691         x_return_status  =>   x_return_status);
692     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
693         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
694     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
695         RAISE OKL_API.G_EXCEPTION_ERROR;
696     END IF;
697     -- Updating the Stream Header Details as per the need
698     p_stmv_rec.date_history := sysdate  ;
699     p_stmv_rec.say_code     :=  'HIST' ;
700     p_stmv_rec.SGN_CODE     :=  'MANL';
701     p_stmv_rec.active_yn    :=  'N';
702     p_stmv_rec.purpose_code :=  'STUBS';
703     p_stmv_rec.comments     :=  'STUB STREAMS';
704     p_stmv_rec.date_history := SYSDATE;
705     -- Updating the Stream Element Details as per the need
706     p_selv_tbl(0).stream_element_date := sysdate;
707     p_selv_tbl(0).amount := 0.0;
708     p_selv_tbl(0).se_line_number := 1 ;
709     p_selv_tbl(0).comments := 'STUB STREAM ELEMENT' ;
710     p_selv_tbl(0).parent_index := 1 ;
711 
712     -- Create the Stream Header along with one stream element.
713     --print( l_prog_name, ' b4 create_stub ' || to_char(l_sty_id) );
714     okl_streams_pub.create_streams(
715             p_api_version,
716             p_init_msg_list,
717             x_return_status,
718             x_msg_count,
719             x_msg_data,
720             p_stmv_rec,
721             p_selv_tbl,
722             x_stmv_rec,
723             x_selv_tbl);
724     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
725         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
726     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
727         RAISE OKL_API.G_EXCEPTION_ERROR;
728     END IF;
729     --print( l_prog_name, ' after create_stub ', x_return_status );
730     --print( l_prog_name, ' # of stubf created ' || to_chaR( x_selv_tbl.COUNT ) );
731     --x_se_id := x_selv_tbl(x_selv_tbl.FIRST).id;
732 
733 --prasjain:start for bug 5474827
734 --Populating the stream element id from parameter x_selv_tbl instead of getting from cursor c_sty
735     x_se_id := x_selv_tbl(0).id;
736 
737     --Returns the id of the new Stream Created ?????
738 /*
739     OPEN    c_sty;
740     FETCH   c_sty INTO x_se_id;
741     CLOSE   c_sty;
742 */
743 --prasjain:End
744     --print( l_prog_name, ' stub Id  ' || to_chaR( x_se_id ) );
745   end generate_stub_element;
746   ---------------------------------------------------------------------------
747   -- PROCEDURE generate_quote_streams
748   ---------------------------------------------------------------------------
749 
750   ---------------------------------------------------------------------------
751   PROCEDURE generate_quote_streams(
752                              p_api_version   IN  NUMBER,
753                              p_init_msg_list IN  VARCHAR2,
754                              p_khr_id        IN  NUMBER,
755                              p_kle_id        IN  NUMBER,
756                              x_return_status OUT NOCOPY VARCHAR2,
757                              x_msg_count     OUT NOCOPY NUMBER,
758                              x_msg_data      OUT NOCOPY VARCHAR2) IS
759 
760     lx_return_status              VARCHAR2(1);
761 
762 
763     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'generate_quote_streams';
764 
765   --Added sll.rule_information2 in order by clause by djanaswa for bug 6007644
766     Cursor c_rollover_fee_pmnts ( feeId NUMBER ) IS
767     SELECT DISTINCT
768            cle.id kleId,
769 	   kle.amount fee_amount,
770            cle.start_date fee_start_date,
771 	   stm.id styId,
772            sll.object1_id1 frequency,
773            TO_NUMBER(sll.rule_information3) periods,
774            FND_DATE.canonical_to_date(sll.rule_information2) start_date,
775            sll.rule_information5 structure,
776            sll.rule_information10 advance_arrears,
777            FND_NUMBER.canonical_to_number(sll.rule_information6) amount,
778            TO_NUMBER(sll.rule_information7) stub_days,
779            TO_NUMBER(sll.rule_information8) stub_amount
780     FROM okc_k_headers_b chr_so,
781          okc_k_lines_b cle,
782          okl_k_lines kle,
783          okc_line_styles_b lse,
784          okc_rules_b sll,
785          okc_rules_b slh,
786          okc_rule_groups_b rgp,
787 	 okl_strm_type_b stm
788     WHERE chr_so.id = p_khr_id
789     and cle.sts_code in( 'COMPLETE', 'INCOMPLETE')--'ENTERED'
790     AND cle.dnz_chr_id = chr_so.id
791     AND cle.id = kle.id
792     AND kle.id = feeId
793     AND trunc(cle.START_DATE) = trunc(chr_so.START_DATE)
794     AND cle.lse_id = lse.id
795     AND ((lse.lty_code = 'FEE' AND kle.fee_type = 'ROLLOVER') OR lse.lty_code = 'LINK_FEE_ASSET')
796     AND kle.fee_type <> 'CAPITALIZED'
797     AND cle.id = rgp.cle_id
798     AND rgp.rgd_code = 'LALEVL'
799     AND rgp.id = slh.rgp_id
800     AND slh.rule_information_category = 'LASLH'
801     AND slh.object1_id1 = TO_CHAR(stm.id)
802     AND TO_CHAR(slh.id) = sll.object2_id1
803     AND sll.rule_information_category = 'LASLL'
804     order by cle.id,stm.id, FND_DATE.canonical_to_date(sll.rule_information2);
805 
806     r_rollover_fee_pmnts c_rollover_fee_pmnts%ROWTYPE;
807 
808     Cursor c_rollover_fee IS
809     SELECT DISTINCT
810            to_char(cle.id) kleId,
811 	   kle.amount fee_amount,
812            cle.start_date fee_start_date
813     FROM okc_k_headers_b chr_so,
814          okc_k_lines_b cle,
815          okl_k_lines kle,
816          okc_line_styles_b lse
817     WHERE chr_so.id = p_khr_id
818     and cle.sts_code in( 'COMPLETE', 'INCOMPLETE')--'ENTERED'
819     AND cle.dnz_chr_id = chr_so.id
820     AND cle.id = kle.id
821     AND trunc(cle.START_DATE) = trunc(chr_so.START_DATE)
822     AND cle.lse_id = lse.id
823     AND ((lse.lty_code = 'FEE' AND kle.fee_type = 'ROLLOVER') OR lse.lty_code = 'LINK_FEE_ASSET')
824     AND kle.fee_type <> 'CAPITALIZED';
825 
826     r_rollover_fee c_rollover_fee%ROWTYPE;
827 
828     l_selv_tbl               okl_streams_pub.selv_tbl_type;
829     l_tmp_selv_tbl               okl_streams_pub.selv_tbl_type;
830     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
831     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
832 
833     l_stmv_rec               okl_streams_pub.stmv_rec_type;
834     l_pt_rec                 okl_streams_pub.stmv_rec_type;
835     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
836 
837     l_capital_cost           NUMBER;
838 
839     i                        BINARY_INTEGER := 0;
840     j                        BINARY_INTEGER := 0;
841 
842     l_cle_id                 NUMBER;
843     l_sty_id                 NUMBER;
844 
845     l_deal_type varchar2(30);
846 
847     CURSOR c_hdr IS
848       SELECT chr.template_yn,
849              chr.currency_code,
850              chr.start_date,
851              khr.deal_type,
852              khr.term_duration,
853              NVL(khr.generate_accrual_yn, 'Y')
854       FROM   okc_k_headers_b chr,
855              okl_k_headers khr
856       WHERE  khr.id = p_khr_id
857         AND  chr.id = khr.id;
858 
859     l_hdr c_hdr%ROWTYPE;
860 
861     l_se_id NUMBER;
862     l_fee_start_date DATE;
863 
864     l_principal_tbl          okl_streams_pub.selv_tbl_type;
865     l_interest_tbl           okl_streams_pub.selv_tbl_type;
866     l_prin_bal_tbl           okl_streams_pub.selv_tbl_type;
867     l_termination_tbl        okl_streams_pub.selv_tbl_type;
868     l_pre_tax_inc_tbl        okl_streams_pub.selv_tbl_type;
869 
870     l_interim_interest       NUMBER;
871     l_interim_days           NUMBER;
872     l_interim_dpp            NUMBER;
873     l_asset_iir              NUMBER;
874     l_asset_guess_iir        NUMBER;
875     l_bkg_yield_guess        NUMBER;
876     l_asset_booking_yield    NUMBER;
877 
878     l_interim_tbl            OKL_PRICING_PVT.interim_interest_tbl_type;
879     l_sub_interim_tbl            OKL_PRICING_PVT.interim_interest_tbl_type;
880 
881     l_principal_id           NUMBER;
882     l_interest_id            NUMBER;
883     l_prin_bal_id            NUMBER;
884     l_termination_id         NUMBER;
885     l_pre_tax_inc_id         NUMBER;
886 
887     l_sty_name VARCHAR2(256);
888     l_has_pmnts VARCHAR2(1);
889 
890   --Added by djanaswa for bug 6007644
891     l_recurrence_date    DATE := NULL;
892     l_old_sty_id         NUMBER;
893     --end djanaswa
894 
895 
896 
897   BEGIN
898 
899      OPEN c_hdr;
900      FETCH c_hdr into l_hdr;
901      CLOSE c_hdr;
902 
903      l_deal_type := l_hdr.deal_type;
904 
905     generate_stub_element( p_api_version   => p_api_version,
906                            p_init_msg_list => p_init_msg_list,
907 			   p_deal_type     => l_deal_type,
908                            p_khr_id        => p_khr_id,
909                            x_return_status => x_return_status,
910                            x_msg_count     => x_msg_count,
911                            x_msg_data      => x_msg_data,
912                            x_se_id         => l_se_id );
913 
914     print( l_prog_name, 'stub', x_return_status );
915     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
916       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
917     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
918       RAISE OKL_API.G_EXCEPTION_ERROR;
919     END IF;
920 
921      For r_rollover_fee in c_rollover_fee
922      LOOP
923 
924     print( l_prog_name, ' found a roll fee ', to_char(r_rollover_fee.kleId) );
925 
926         update okl_streams
927         set say_code = 'HIST',
928         date_history = SYSDATE,
929         last_updated_by = FND_GLOBAL.USER_ID,      -- BUG:14749215 changes start here
930         last_update_date = sysdate,
931         last_update_login = FND_GLOBAL.LOGIN_ID    -- BUG:14749215 changes end here
932         where khr_id = p_khr_id
933            and kle_id = r_rollover_fee.kleId;
934 
935         update okl_streams
936         set active_yn = 'N',
937         last_updated_by = FND_GLOBAL.USER_ID,      -- BUG:14749215 changes start here
938         last_update_date = sysdate,
939         last_update_login = FND_GLOBAL.LOGIN_ID    -- BUG:14749215 changes end here
940         where khr_id = p_khr_id
941            and kle_id = r_rollover_fee.kleId;
942 
943 
944      r_rollover_fee_pmnts := NULL;
945      l_has_pmnts := 'N';
946 
947      For r_rollover_fee_pmnts in c_rollover_fee_pmnts ( r_rollover_fee.kleId )
948      LOOP
949     print( l_prog_name, ' foudn a roll fee pmnts ', to_char(r_rollover_fee_pmnts.kleId) );
950 
951         l_has_pmnts := 'Y';
952 
953         l_cle_id := r_rollover_fee_pmnts.kleId;
954        	l_sty_id := r_rollover_fee_pmnts.styId;
955         l_capital_cost := r_rollover_fee_pmnts.fee_amount;
956 
957         l_fee_start_date := r_rollover_fee_pmnts.fee_start_date;
958 
959         --Added by djanaswa for bug 6007644
960         IF((r_rollover_fee_pmnts.periods IS NULL) AND (r_rollover_fee_pmnts.stub_days IS NOT NULL)) THEN
961           --Set the recurrence date to null for stub payment
962           l_recurrence_date := NULL;
963         ELSIF(l_recurrence_date IS NULL OR l_old_sty_id <> r_rollover_fee_pmnts.styId) THEN
964           --Set the recurrence date as periodic payment level start date
965           l_recurrence_date := r_rollover_fee_pmnts.start_date;
966         END IF;
967         l_old_sty_id := r_rollover_fee_pmnts.styId;
968         --end djanaswa
969 
970         --Added parameter p_recurrence_date by djanaswa for bug 6007644
971         get_stream_elements( p_start_date          =>   r_rollover_fee_pmnts.start_date,
972                              p_periods             =>   r_rollover_fee_pmnts.periods,
973                              p_frequency           =>   r_rollover_fee_pmnts.frequency,
974                              p_structure           =>   r_rollover_fee_pmnts.structure,
975                              p_advance_or_arrears  =>   r_rollover_fee_pmnts.advance_arrears,
976                              p_amount              =>   r_rollover_fee_pmnts.amount,
977                      			     p_stub_days           =>   r_rollover_fee_pmnts.stub_days,
978                      			     p_stub_amount         =>   r_rollover_fee_pmnts.stub_amount,
979                              p_currency_code       =>   l_hdr.currency_code,
980                              p_khr_id              =>   p_khr_id,
981                              p_kle_id              =>   r_rollover_fee_pmnts.kleId,
982                              p_purpose_code        =>   NULL,
983                              x_selv_tbl            =>   l_tmp_selv_tbl,
984                              x_pt_tbl              =>   l_pt_tbl,
985                              x_return_status       =>   lx_return_status,
986                              x_msg_count           =>   x_msg_count,
987                              x_msg_data            =>   x_msg_data,
988                              p_recurrence_date     =>   l_recurrence_date);
989         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
990           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
991         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
992           RAISE OKL_API.G_EXCEPTION_ERROR;
993         END IF;
994 
995         j := l_selv_tbl.COUNT;
996         For i in l_tmp_selv_tbl.FIRST..l_tmp_selv_tbl.LAST
997 	LOOP
998 
999 	    l_selv_tbl(i+j) := l_tmp_selv_tbl(i);
1000             If (  r_rollover_fee_pmnts.stub_days IS NOT NULL ) AND (  r_rollover_fee_pmnts.periods IS NULL ) Then
1001 	        l_selv_tbl(i+j).sel_id := l_se_id;
1002 	    End If;
1003 
1004 	END LOOP;
1005 	l_tmp_selv_tbl.DELETE;
1006 
1007      END LOOP;
1008 
1009      If ( l_has_pmnts = 'Y') Then
1010 
1011         get_stream_header(p_khr_id         =>   p_khr_id,
1012                           p_kle_id         =>   l_cle_id,
1013                           p_sty_id         =>   l_sty_id,
1014                           p_purpose_code   =>   NULL,
1015                           x_stmv_rec       =>   l_stmv_rec,
1016                           x_return_status  =>   lx_return_status);
1017 
1018         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1019           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1020         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1021           RAISE OKL_API.G_EXCEPTION_ERROR;
1022         END IF;
1023 
1024 
1025         lx_return_status := Okl_Streams_Util.round_streams_amount(
1026                             p_api_version   => g_api_version,
1027                             p_init_msg_list => p_init_msg_list,
1028                             x_msg_count     => x_msg_count,
1029                             x_msg_data      => x_msg_data,
1030                             p_chr_id        => p_khr_id,
1031                             p_selv_tbl      => l_selv_tbl,
1032                             x_selv_tbl      => lx_selv_tbl);
1033 
1034         IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
1035             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1036         ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
1037             RAISE OKL_API.G_EXCEPTION_ERROR;
1038         END IF;
1039 
1040         l_selv_tbl.DELETE;
1041         l_selv_tbl := lx_selv_tbl;
1042 
1043         l_stmv_rec.say_code := 'CURR';
1044         l_stmv_rec.active_yn := 'Y';
1045         l_stmv_rec.date_current := SYSDATE;
1046 
1047         okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
1048                                        p_init_msg_list   =>   G_FALSE,
1049                                        x_return_status   =>   lx_return_status,
1050                                        x_msg_count       =>   x_msg_count,
1051                                        x_msg_data        =>   x_msg_data,
1052                                        p_stmv_rec        =>   l_stmv_rec,
1053                                        p_selv_tbl        =>   l_selv_tbl,
1054                                        x_stmv_rec        =>   lx_stmv_rec,
1055                                        x_selv_tbl        =>   lx_selv_tbl);
1056 
1057         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1058           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1059         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1060           RAISE OKL_API.G_EXCEPTION_ERROR;
1061         END IF;
1062 
1063 print( l_prog_name, 'amortize with capital cost ' || to_char(l_capital_cost) );
1064 
1065              OKL_PRICING_PVT.get_quote_amortization(
1066 	                        p_khr_id           => p_khr_id,
1067                                 p_kle_id           => l_cle_id,
1068                                 p_investment       => l_capital_cost,
1069                                 p_residual_value   => 0,
1070                                 p_start_date       => l_hdr.start_date,
1071                                 p_asset_start_date => l_fee_start_date,
1072                                 p_term_duration    => l_hdr.term_duration,
1073                                 x_principal_tbl    => l_principal_tbl,
1074                                 x_interest_tbl     => l_interest_tbl,
1075                                 x_prin_bal_tbl     => l_prin_bal_tbl,
1076                                 x_termination_tbl  => l_termination_tbl,
1077                                 x_pre_tax_inc_tbl  => l_pre_tax_inc_tbl,
1078                                 x_interim_interest => l_interim_interest,
1079                                 x_interim_days     => l_interim_days,
1080                                 x_interim_dpp      => l_interim_dpp,
1081                                 x_iir              => l_asset_iir,
1082                                 x_booking_yield    => l_asset_booking_yield,
1083                                 x_return_status    => lx_return_status);
1084 
1085 print( l_prog_name, 'amortize ',  lx_return_status );
1086 
1087              IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1088                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1089              ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1090                  RAISE OKL_API.G_EXCEPTION_ERROR;
1091              END IF;
1092 
1093 
1094           OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1095                                          p_khr_id                => p_khr_id,
1096                                    					 p_deal_type             => l_deal_type,
1097                                          p_dependent_sty_purpose => 'INTEREST_INCOME',
1098                                          x_return_status         => x_return_status,
1099                                          x_dependent_sty_id      => l_pre_tax_inc_id,
1100                                          x_dependent_sty_name    => l_sty_name);
1101 
1102           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1103               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1104           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
1105               RAISE OKL_API.G_EXCEPTION_ERROR;
1106           END IF;
1107 
1108         OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1109                     p_khr_id  		    => p_khr_id,
1110 	            p_deal_type             => l_deal_type,
1111                     p_dependent_sty_purpose => 'INTEREST_PAYMENT',
1112                     x_return_status         => lx_return_status,
1113                     x_dependent_sty_id      => l_interest_id,
1114                     x_dependent_sty_name    => l_sty_name);
1115 
1116         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1117           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1118         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1119           RAISE OKL_API.G_EXCEPTION_ERROR;
1120         END IF;
1121 
1122         OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1123                     p_khr_id  		    => p_khr_id,
1124 	            p_deal_type             => l_deal_type,
1125                     p_dependent_sty_purpose => 'PRINCIPAL_PAYMENT',
1126                     x_return_status         => lx_return_status,
1127                     x_dependent_sty_id      => l_principal_id,
1128                     x_dependent_sty_name    => l_sty_name);
1129 
1130         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1131           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1132         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1133           RAISE OKL_API.G_EXCEPTION_ERROR;
1134         END IF;
1135 
1136         OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1137                     p_khr_id  		    => p_khr_id,
1138                     p_deal_type             => l_deal_type,
1139                     p_dependent_sty_purpose => 'PRINCIPAL_BALANCE',
1140                     x_return_status         => lx_return_status,
1141                     x_dependent_sty_id      => l_prin_bal_id,
1142                     x_dependent_sty_name    => l_sty_name);
1143 
1144         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1145           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1146         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1147           RAISE OKL_API.G_EXCEPTION_ERROR;
1148         END IF;
1149 
1150         OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1151                     p_khr_id  		    => p_khr_id,
1152 	            p_deal_type             => l_deal_type,
1153                     p_dependent_sty_purpose => 'TERMINATION_VALUE',
1154                     x_return_status         => lx_return_status,
1155                     x_dependent_sty_id      => l_termination_id,
1156                     x_dependent_sty_name    => l_sty_name);
1157 
1158         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1159           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1160         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1161           RAISE OKL_API.G_EXCEPTION_ERROR;
1162         END IF;
1163 
1164           IF l_principal_tbl.COUNT > 0 AND (l_principal_id IS NOT NULL) Then
1165 
1166             get_stream_header(p_khr_id         =>   p_khr_id,
1167                               p_kle_id         =>   l_cle_id,
1168                               p_sty_id         =>   l_principal_id,
1169                               p_purpose_code   =>   NULL,
1170                               x_stmv_rec       =>   l_stmv_rec,
1171                               x_return_status  =>   lx_return_status);
1172 
1173             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1174               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1175             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1176               RAISE OKL_API.G_EXCEPTION_ERROR;
1177             END IF;
1178 
1179 
1180             l_stmv_rec.say_code := 'CURR';
1181             l_stmv_rec.active_yn := 'Y';
1182             l_stmv_rec.date_current := SYSDATE;
1183 
1184             lx_return_status := Okl_Streams_Util.round_streams_amount(
1185                                 p_api_version   => g_api_version,
1186                                 p_init_msg_list => p_init_msg_list,
1187                                 x_msg_count     => x_msg_count,
1188                                 x_msg_data      => x_msg_data,
1189                                 p_chr_id        => p_khr_id,
1190                                 p_selv_tbl      => l_principal_tbl,
1191                                 x_selv_tbl      => lx_selv_tbl);
1192 
1193             IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
1194                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1195             ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
1196                 RAISE OKL_API.G_EXCEPTION_ERROR;
1197             END IF;
1198 
1199             l_principal_tbl.DELETE;
1200             l_principal_tbl := lx_selv_tbl;
1201 
1202             okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
1203                                            p_init_msg_list   =>   G_FALSE,
1204                                            x_return_status   =>   lx_return_status,
1205                                            x_msg_count       =>   x_msg_count,
1206                                            x_msg_data        =>   x_msg_data,
1207                                            p_stmv_rec        =>   l_stmv_rec,
1208                                            p_selv_tbl        =>   l_principal_tbl,
1209                                            x_stmv_rec        =>   lx_stmv_rec,
1210                                            x_selv_tbl        =>   lx_selv_tbl);
1211 
1212             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1213               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1214             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1215               RAISE OKL_API.G_EXCEPTION_ERROR;
1216             END IF;
1217 
1218           END IF;
1219 
1220           IF l_interest_tbl.COUNT > 0 AND ( l_interest_id IS NOT NULL) Then
1221 
1222             get_stream_header(p_khr_id         =>   p_khr_id,
1223                               p_kle_id         =>   l_cle_id,
1224                               p_sty_id         =>   l_interest_id,
1225                               p_purpose_code   =>   NULL,
1226                               x_stmv_rec       =>   l_stmv_rec,
1227                               x_return_status  =>   lx_return_status);
1228 
1229             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1230               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1231             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1232               RAISE OKL_API.G_EXCEPTION_ERROR;
1233             END IF;
1234 
1235 
1236             l_stmv_rec.say_code := 'CURR';
1237             l_stmv_rec.active_yn := 'Y';
1238             l_stmv_rec.date_current := SYSDATE;
1239 
1240             lx_return_status := Okl_Streams_Util.round_streams_amount(
1241                                 p_api_version   => g_api_version,
1242                                 p_init_msg_list => p_init_msg_list,
1243                                 x_msg_count     => x_msg_count,
1244                                 x_msg_data      => x_msg_data,
1245                                 p_chr_id        => p_khr_id,
1246                                 p_selv_tbl      => l_interest_tbl,
1247                                 x_selv_tbl      => lx_selv_tbl);
1248 
1249             IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
1250                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1251             ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
1252                 RAISE OKL_API.G_EXCEPTION_ERROR;
1253             END IF;
1254 
1255             l_interest_tbl.DELETE;
1256             l_interest_tbl := lx_selv_tbl;
1257 
1258             okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
1259                                            p_init_msg_list   =>   G_FALSE,
1260                                            x_return_status   =>   lx_return_status,
1261                                            x_msg_count       =>   x_msg_count,
1262                                            x_msg_data        =>   x_msg_data,
1263                                            p_stmv_rec        =>   l_stmv_rec,
1264                                            p_selv_tbl        =>   l_interest_tbl,
1265                                            x_stmv_rec        =>   lx_stmv_rec,
1266                                            x_selv_tbl        =>   lx_selv_tbl);
1267 
1268             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1269               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1270             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1271               RAISE OKL_API.G_EXCEPTION_ERROR;
1272             END IF;
1273 
1274           END IF;
1275 
1276           IF l_prin_bal_tbl.COUNT > 0 AND (l_prin_bal_id IS NOT NULL) Then
1277 
1278             get_stream_header(p_khr_id         =>   p_khr_id,
1279                               p_kle_id         =>   l_cle_id,
1280                               p_sty_id         =>   l_prin_bal_id,
1281                               p_purpose_code   =>   NULL,
1282                               x_stmv_rec       =>   l_stmv_rec,
1283                               x_return_status  =>   lx_return_status);
1284 
1285             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1286               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1287             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1288               RAISE OKL_API.G_EXCEPTION_ERROR;
1289             END IF;
1290 
1291 
1292             l_stmv_rec.say_code := 'CURR';
1293             l_stmv_rec.active_yn := 'Y';
1294             l_stmv_rec.date_current := SYSDATE;
1295 
1296             lx_return_status := Okl_Streams_Util.round_streams_amount(
1297                                 p_api_version   => g_api_version,
1298                                 p_init_msg_list => p_init_msg_list,
1299                                 x_msg_count     => x_msg_count,
1300                                 x_msg_data      => x_msg_data,
1301                                 p_chr_id        => p_khr_id,
1302                                 p_selv_tbl      => l_prin_bal_tbl,
1303                                 x_selv_tbl      => lx_selv_tbl);
1304 
1305             IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
1306                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1307             ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
1308                 RAISE OKL_API.G_EXCEPTION_ERROR;
1309             END IF;
1310 
1311             l_prin_bal_tbl.DELETE;
1312             l_prin_bal_tbl := lx_selv_tbl;
1313 
1314             okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
1315                                            p_init_msg_list   =>   G_FALSE,
1316                                            x_return_status   =>   lx_return_status,
1317                                            x_msg_count       =>   x_msg_count,
1318                                            x_msg_data        =>   x_msg_data,
1319                                            p_stmv_rec        =>   l_stmv_rec,
1320                                            p_selv_tbl        =>   l_prin_bal_tbl,
1321                                            x_stmv_rec        =>   lx_stmv_rec,
1322                                            x_selv_tbl        =>   lx_selv_tbl);
1323 
1324             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1325               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1326             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1327               RAISE OKL_API.G_EXCEPTION_ERROR;
1328             END IF;
1329 
1330           END IF;
1331 
1332           IF l_termination_tbl.COUNT > 0 AND (l_termination_id IS NOT NULL) THEN
1333 
1334             get_stream_header(p_khr_id         =>   p_khr_id,
1335                               p_kle_id         =>   l_cle_id,
1336                               p_sty_id         =>   l_termination_id,
1337                               p_purpose_code   =>   NULL,
1338                               x_stmv_rec       =>   l_stmv_rec,
1339                               x_return_status  =>   lx_return_status);
1340 
1341             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1342               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1343             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1344               RAISE OKL_API.G_EXCEPTION_ERROR;
1345             END IF;
1346 
1347 
1348             l_stmv_rec.say_code := 'CURR';
1349             l_stmv_rec.active_yn := 'Y';
1350             l_stmv_rec.date_current := SYSDATE;
1351 
1352             lx_return_status := Okl_Streams_Util.round_streams_amount(
1353                                 p_api_version   => g_api_version,
1354                                 p_init_msg_list => p_init_msg_list,
1355                                 x_msg_count     => x_msg_count,
1356                                 x_msg_data      => x_msg_data,
1357                                 p_chr_id        => p_khr_id,
1358                                 p_selv_tbl      => l_termination_tbl,
1359                                 x_selv_tbl      => lx_selv_tbl);
1360 
1361             IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
1362                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1363             ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
1364                 RAISE OKL_API.G_EXCEPTION_ERROR;
1365             END IF;
1366 
1367             l_termination_tbl.DELETE;
1368             l_termination_tbl := lx_selv_tbl;
1369 
1370             okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
1371                                            p_init_msg_list   =>   G_FALSE,
1372                                            x_return_status   =>   lx_return_status,
1373                                            x_msg_count       =>   x_msg_count,
1374                                            x_msg_data        =>   x_msg_data,
1375                                            p_stmv_rec        =>   l_stmv_rec,
1376                                            p_selv_tbl        =>   l_termination_tbl,
1377                                            x_stmv_rec        =>   lx_stmv_rec,
1378                                            x_selv_tbl        =>   lx_selv_tbl);
1379 
1380             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1381               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1382             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1383               RAISE OKL_API.G_EXCEPTION_ERROR;
1384             END IF;
1385 
1386           END IF;
1387 
1388           IF l_pre_tax_inc_tbl.COUNT > 0 AND l_pre_tax_inc_id IS NOT NULL THEN
1389 
1390             get_stream_header(p_khr_id         =>   p_khr_id,
1391                               p_kle_id         =>   l_cle_id,
1392                               p_sty_id         =>   l_pre_tax_inc_id,
1393                               p_purpose_code   =>   NULL,
1394                               x_stmv_rec       =>   l_stmv_rec,
1395                               x_return_status  =>   lx_return_status);
1396 
1397             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1398               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1399             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1400               RAISE OKL_API.G_EXCEPTION_ERROR;
1401             END IF;
1402 
1403             l_stmv_rec.say_code := 'CURR';
1404             l_stmv_rec.active_yn := 'Y';
1405             l_stmv_rec.date_current := SYSDATE;
1406 
1407 
1408             lx_return_status := Okl_Streams_Util.round_streams_amount(
1409                                 p_api_version   => g_api_version,
1410                                 p_init_msg_list => p_init_msg_list,
1411                                 x_msg_count     => x_msg_count,
1412                                 x_msg_data      => x_msg_data,
1413                                 p_chr_id        => p_khr_id,
1414                                 p_selv_tbl      => l_pre_tax_inc_tbl,
1415                                 x_selv_tbl      => lx_selv_tbl);
1416 
1417             IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
1418                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1419             ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
1420                 RAISE OKL_API.G_EXCEPTION_ERROR;
1421             END IF;
1422 
1423             l_pre_tax_inc_tbl.DELETE;
1424             l_pre_tax_inc_tbl := lx_selv_tbl;
1425 
1426             okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
1427                                            p_init_msg_list   =>   G_FALSE,
1428                                            x_return_status   =>   lx_return_status,
1429                                            x_msg_count       =>   x_msg_count,
1430                                            x_msg_data        =>   x_msg_data,
1431                                            p_stmv_rec        =>   l_stmv_rec,
1432                                            p_selv_tbl        =>   l_pre_tax_inc_tbl,
1433                                            x_stmv_rec        =>   lx_stmv_rec,
1434                                            x_selv_tbl        =>   lx_selv_tbl);
1435 
1436             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1437               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1438             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
1439               RAISE OKL_API.G_EXCEPTION_ERROR;
1440             END IF;
1441 
1442           END IF;
1443 
1444           -- Clear out data structures
1445 
1446           l_principal_tbl.delete;
1447           l_interest_tbl.delete;
1448           l_prin_bal_tbl.delete;
1449           l_termination_tbl.delete;
1450           l_pre_tax_inc_tbl.delete;
1451 
1452        End If;
1453 
1454      END LOOP;
1455 
1456   EXCEPTION
1457 
1458     WHEN OKL_API.G_EXCEPTION_ERROR THEN
1459 
1460       x_return_status := G_RET_STS_ERROR;
1461 
1462     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1463 
1464       x_return_status := G_RET_STS_UNEXP_ERROR;
1465 
1466     WHEN OTHERS THEN
1467 
1468       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
1469                            p_msg_name     => G_DB_ERROR,
1470                            p_token1       => G_PROG_NAME_TOKEN,
1471                            p_token1_value => l_prog_name,
1472                            p_token2       => G_SQLCODE_TOKEN,
1473                            p_token2_value => sqlcode,
1474                            p_token3       => G_SQLERRM_TOKEN,
1475                            p_token3_value => sqlerrm);
1476 
1477       x_return_status := G_RET_STS_UNEXP_ERROR;
1478 
1479   END generate_quote_streams;
1480 
1481   PROCEDURE get_present_value(     p_api_version            IN NUMBER,
1482                                    p_init_msg_list          IN VARCHAR2,
1483 				   p_amount_date            IN DATE,
1484 				   p_amount                 IN NUMBER,
1485 				   p_frequency              IN VARCHAR2 DEFAULT 'M',
1486 				   p_rate                   IN NUMBER,
1487                                    p_pv_date                IN DATE,
1488 				   p_day_convention_month    IN VARCHAR2 DEFAULT '30',
1489 				   p_day_convention_year    IN VARCHAR2 DEFAULT '360',
1490 				   x_pv_amount              OUT NOCOPY NUMBER,
1491                                    x_return_status          OUT NOCOPY VARCHAR2,
1492                                    x_msg_count              OUT NOCOPY NUMBER,
1493                                    x_msg_data               OUT NOCOPY VARCHAR2) IS
1494 
1495     l_days NUMBER;
1496     l_periods NUMBER;
1497     l_cf_dpp NUMBER;
1498     l_cf_ppy NUMBER;
1499 
1500     l_prog_name CONSTANT VARCHAR2(61) := 'get_present_value';
1501 
1502   Begin
1503      --print(l_prog_name, 'begin' );
1504     l_days := OKL_PRICING_UTILS_PVT.get_day_count (p_start_date => p_pv_date,
1505                                  p_days_in_month => p_day_convention_month,
1506                                  p_days_in_year => p_day_convention_year,
1507                                  p_end_date   => p_amount_date,
1508                                  p_arrears    => 'N',
1509                                  x_return_status => x_return_status);
1510 
1511         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1512           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1513         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
1514           RAISE OKL_API.G_EXCEPTION_ERROR;
1515         END IF;
1516 
1517 
1518         IF p_frequency = 'M' THEN
1519           l_cf_dpp  :=  30;
1520           l_cf_ppy  :=  12;
1521         ELSIF p_frequency = 'Q' THEN
1522        	  l_cf_dpp := 90;
1523           l_cf_ppy  :=  4;
1524         ELSIF p_frequency = 'S' THEN
1525        	  l_cf_dpp := 180;
1526           l_cf_ppy  :=  2;
1527         ELSIF p_frequency = 'A' THEN
1528        	  l_cf_dpp := 360;
1529           l_cf_ppy  :=  1;
1530         ELSE
1531           l_cf_dpp  :=  30;
1532           l_cf_ppy  :=  12;
1533         END IF;
1534 
1535         l_periods         := l_days/l_cf_dpp;
1536         x_pv_amount :=  p_amount / POWER( 1 + (p_rate/(l_cf_ppy*100)), l_periods );
1537 
1538         --print(l_prog_name, 'end' );
1539   EXCEPTION
1540     WHEN OKL_API.G_EXCEPTION_ERROR
1541     THEN
1542       x_return_status := G_RET_STS_ERROR;
1543 
1544     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1545 
1546       x_return_status := G_RET_STS_UNEXP_ERROR;
1547 
1548     WHEN OTHERS THEN
1549 
1550       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
1551                            p_msg_name     => G_DB_ERROR,
1552                            p_token1       => G_PROG_NAME_TOKEN,
1553                            p_token1_value => l_prog_name,
1554                            p_token2       => G_SQLCODE_TOKEN,
1555                            p_token2_value => sqlcode,
1556                            p_token3       => G_SQLERRM_TOKEN,
1557                            p_token3_value => sqlerrm);
1558 
1559       x_return_status := G_RET_STS_UNEXP_ERROR;
1560 
1561   End get_present_value;
1562 
1563   PROCEDURE get_present_value(
1564                p_api_version            IN NUMBER,
1565                p_init_msg_list          IN VARCHAR2,
1566                p_cash_flow_tbl          IN cash_flow_tbl,
1567                p_rate                   IN NUMBER,
1568                p_pv_date                IN DATE,
1569                p_day_convention_month    IN VARCHAR2 DEFAULT '30',
1570                p_day_convention_year    IN VARCHAR2 DEFAULT '360',
1571          			   x_pv_amount              OUT NOCOPY NUMBER,
1572                x_return_status          OUT NOCOPY VARCHAR2,
1573                x_msg_count              OUT NOCOPY NUMBER,
1574                x_msg_data               OUT NOCOPY VARCHAR2) IS
1575 
1576     l_prog_name CONSTANT VARCHAR2(61) := 'get_present_value';
1577     i BINARY_INTEGER;
1578 
1579     l_pv_amount NUMBER := 0;
1580 
1581   BEGIN
1582    --print(l_prog_name, 'begin' );
1583    x_pv_amount := 0;
1584    FOR i in p_cash_flow_tbl.FIRST..p_cash_flow_tbl.LAST
1585    LOOP
1586       get_present_value(
1587          p_api_version   => p_api_version,
1588          p_init_msg_list => p_init_msg_list,
1589          p_amount_date   => p_cash_flow_tbl(i).cf_date,
1590          p_amount        => p_cash_flow_tbl(i).cf_amount,
1591          p_frequency     => nvl(p_cash_flow_tbl(i).cf_frequency, 'M'),
1592          p_rate          => p_rate,
1593          p_pv_date       => p_pv_date,
1594   				   p_day_convention_month => p_day_convention_month,
1595          p_day_convention_year => p_day_convention_year,
1596    			   x_pv_amount     => l_pv_amount,
1597          x_return_status => x_return_status,
1598          x_msg_count     => x_msg_count,
1599          x_msg_data      => x_msg_data);
1600         x_pv_amount := x_pv_amount + l_pv_amount;
1601     	END LOOP;
1602      --print(l_prog_name, 'end' );
1603 
1604   EXCEPTION
1605     WHEN OKL_API.G_EXCEPTION_ERROR THEN
1606       x_return_status := G_RET_STS_ERROR;
1607     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1608       x_return_status := G_RET_STS_UNEXP_ERROR;
1609     WHEN OTHERS THEN
1610       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
1611                            p_msg_name     => G_DB_ERROR,
1612                            p_token1       => G_PROG_NAME_TOKEN,
1613                            p_token1_value => l_prog_name,
1614                            p_token2       => G_SQLCODE_TOKEN,
1615                            p_token2_value => sqlcode,
1616                            p_token3       => G_SQLERRM_TOKEN,
1617                            p_token3_value => sqlerrm);
1618 
1619       x_return_status := G_RET_STS_UNEXP_ERROR;
1620   END get_present_value;
1621 
1622   PROCEDURE get_amortized_accruals ( p_khr_id         IN NUMBER,
1623                                      p_currency_code  IN  VARCHAR2,
1624                                      p_start_date     IN  DATE,
1625                             				     p_end_date       IN  DATE,
1626                                      p_deal_type      IN  VARCHAR2,
1627                             				     p_amount         IN  NUMBER,
1628                                      x_selv_tbl       OUT NOCOPY okl_sel_pvt.selv_tbl_type,
1629                                      x_return_status  OUT NOCOPY VARCHAR2) IS
1630 
1631     l_prog_name CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_amortized_accruals';
1632 
1633     CURSOR c_k_income (p_sty_name VARCHAR2) IS
1634       SELECT sel.amount income_amount,
1635              sel.stream_element_date income_date
1636       FROM   okl_strm_elements sel,
1637              okl_streams stm,
1638              okl_strm_type_b sty,
1639              okl_strm_type_tl styt
1640       WHERE  stm.khr_id = p_khr_id
1641         AND  stm.say_code = 'WORK'
1642         AND  stm.id = sel.stm_id
1643         AND  stm.sty_id = sty.id
1644         AND  sty.version = '1.0'
1645         AND  sty.id = styt.id
1646         AND  styt.language = 'US'
1647         AND  styt.name = p_sty_name
1648 	AND  sel.stream_element_date >= p_start_date
1649 	AND  sel.stream_element_date <= LAST_DAY(p_end_date) --bug# 3379436
1650       ORDER BY sel.stream_element_date;
1651 
1652     l_selv_tbl               okl_streams_pub.selv_tbl_type;
1653     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
1654 
1655     l_stmv_rec               okl_streams_pub.stmv_rec_type;
1656     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
1657 
1658     l_amortized_exp_id       NUMBER;
1659     l_sty_name               VARCHAR2(150);
1660     l_sty_id                 NUMBER;
1661     l_name                   VARCHAR2(150);
1662     l_total_rent_income      NUMBER            := 0;
1663     i                        BINARY_INTEGER    := 0;
1664 
1665     lx_return_status         VARCHAR2(1);
1666     lx_msg_data              VARCHAR2(4000);
1667     lx_msg_count             NUMBER;
1668 
1669     TYPE inc_strms_rec_type is RECORD
1670     (  amount NUMBER,
1671        ele_date DATE
1672     );
1673 
1674     TYPE inc_strms_tbl_type is TABLE OF
1675              inc_strms_rec_type INDEX BY BINARY_INTEGER;
1676 
1677     inc_strms_tbl inc_strms_tbl_type;
1678     lastDate DATE := NULL;
1679     Cursor c_rollover_pmnts IS
1680     Select distinct nvl(slh.object1_id1, -1) styId
1681     From   OKC_RULE_GROUPS_B rgp,
1682            OKC_RULES_B sll,
1683            okc_rules_b slh,
1684 	   okl_strm_type_b sty
1685     Where  slh.rgp_id = rgp.id
1686        and rgp.RGD_CODE = 'LALEVL'
1687        and sll.RULE_INFORMATION_CATEGORY = 'LASLL'
1688        and slh.RULE_INFORMATION_CATEGORY = 'LASLH'
1689        AND TO_CHAR(slh.id) = sll.object2_id1
1690        and slh.object1_id1 = sty.id
1691        and sty.stream_type_purpose = 'RENT'
1692        and rgp.dnz_chr_id = p_khr_id;
1693     r_rollover_pmnts c_rollover_pmnts%ROWTYPE;
1694     l_primary_sty_id NUMBER;
1695   BEGIN
1696     print( l_prog_name, 'begin' );
1697 
1698     OPEN c_rollover_pmnts;
1699     FETCH c_rollover_pmnts INTO r_rollover_pmnts;
1700     CLOSE c_rollover_pmnts;
1701 
1702     l_primary_sty_id := r_rollover_pmnts.styId;
1703 
1704     IF p_deal_type = 'LEASEOP' THEN
1705       --l_sty_name := 'RENTAL ACCRUAL';
1706       OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1707                           p_khr_id                => p_khr_id,
1708 					                     p_deal_type             => p_deal_type,
1709 					                     p_primary_sty_id        => l_primary_sty_id,
1710                           p_dependent_sty_purpose => 'RENT_ACCRUAL',
1711                           x_return_status         => x_return_status,
1712                           x_dependent_sty_id      => l_sty_id,
1713                           x_dependent_sty_name    => l_sty_name);
1714 
1715           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1716               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1717           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
1718               RAISE OKL_API.G_EXCEPTION_ERROR;
1719           END IF;
1720     ELSIF p_deal_type IN ('LEASEDF', 'LEASEST') THEN
1721       --l_sty_name := 'PRE-TAX INCOME';
1722           OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1723                                          p_khr_id                => p_khr_id,
1724                                    					 p_deal_type             => p_deal_type,
1725                                     				 p_primary_sty_id        => l_primary_sty_id,
1726                                          p_dependent_sty_purpose => 'LEASE_INCOME',
1727                                          x_return_status         => x_return_status,
1728                                          x_dependent_sty_id      => l_sty_id,
1729                                          x_dependent_sty_name    => l_sty_name);
1730 
1731           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1732               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1733           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
1734               RAISE OKL_API.G_EXCEPTION_ERROR;
1735           END IF;
1736 
1737     ELSIF p_deal_type IN ('LOAN', 'LOAN-REVOLVING') THEN
1738       --l_sty_name := 'PRE-TAX INCOME';
1739           OKL_ISG_UTILS_PVT.get_dependent_stream_type(
1740                                p_khr_id                => p_khr_id,
1741                                p_deal_type             => p_deal_type,
1742                                p_primary_sty_id        => l_primary_sty_id,
1743                                p_dependent_sty_purpose => 'INTEREST_INCOME',
1744                                x_return_status         => x_return_status,
1745                                x_dependent_sty_id      => l_sty_id,
1746                                x_dependent_sty_name    => l_sty_name);
1747 
1748           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
1749               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1750           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
1751               RAISE OKL_API.G_EXCEPTION_ERROR;
1752           END IF;
1753 
1754     END IF;
1755 
1756     print( l_prog_name, ' income stream ' || l_sty_name );
1757 
1758     i := 0;
1759     FOR l_k_income IN c_k_income(p_sty_name => l_sty_name) LOOP
1760       l_total_rent_income := l_total_rent_income + l_k_income.income_amount;
1761       If ( trunc(l_k_income.income_date) =
1762              trunc(nvl(lastDate, l_k_income.income_date+1) )) Then
1763           inc_strms_tbl(i).amount := inc_strms_tbl(i).amount + l_k_income.income_amount;
1764       Else
1765           i := i + 1;
1766           inc_strms_tbl(i).amount := l_k_income.income_amount;
1767           inc_strms_tbl(i).ele_date := l_K_income.income_date;
1768           lastDate := l_K_income.income_date;
1769       End If;
1770     END LOOP;
1771 
1772 
1773     print( l_prog_name, ' l_total_rent_income ' || to_char( l_total_rent_income) );
1774     print( l_prog_name, ' amount 2 b amort ' || to_char( p_amount) );
1775       IF NVL(p_amount, 0) > 0 THEN
1776 
1777 
1778       FOR i IN 1..inc_strms_tbl.count
1779       LOOP
1780 
1781 
1782         l_selv_tbl(i).stream_element_date := inc_strms_tbl(i).ele_date;
1783         l_selv_tbl(i).se_line_number      := i;
1784 
1785         --bug 6751635 veramach start
1786         IF(inc_strms_tbl(i).amount = 0 OR l_total_rent_income = 0) THEN
1787           l_selv_tbl(i).amount := 0;
1788         ELSE
1789           l_selv_tbl(i).amount :=
1790                (inc_strms_tbl(i).amount/l_total_rent_income)*p_amount;
1791         END IF;
1792         --bug 6751635 veramach end
1793 
1794       END LOOP;
1795 
1796       x_selv_tbl := l_selv_tbl;
1797 
1798       i := 0;
1799 
1800       END IF;
1801 
1802     x_return_status := G_RET_STS_SUCCESS;
1803     print( l_prog_name, 'end' );
1804 
1805   EXCEPTION
1806 
1807     WHEN OKL_API.G_EXCEPTION_ERROR THEN
1808 
1809       x_return_status := G_RET_STS_ERROR;
1810 
1811     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1812 
1813       x_return_status := G_RET_STS_UNEXP_ERROR;
1814 
1815     WHEN OTHERS THEN
1816 
1817       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
1818                            p_msg_name     => G_DB_ERROR,
1819                            p_token1       => G_PROG_NAME_TOKEN,
1820                            p_token1_value => l_prog_name,
1821                            p_token2       => G_SQLCODE_TOKEN,
1822                            p_token2_value => sqlcode,
1823                            p_token3       => G_SQLERRM_TOKEN,
1824                            p_token3_value => sqlerrm);
1825 
1826       x_return_status := G_RET_STS_UNEXP_ERROR;
1827 
1828   END get_amortized_accruals;
1829 
1830   Function Is_Var_Rate_Contract (p_chr_id in number) Return Varchar2 is
1831   Cursor get_rul_csr(p_chr_id in Number, p_rule_code in varchar2, p_rgd_code in varchar2) is
1832   Select rul.*
1833   from   okc_rules_b rul,
1834          okc_rule_groups_b rgp
1835   where  rul.rgp_id  = rgp.id
1836   and    rul.rule_information_category = p_rule_code
1837   and    rul.dnz_chr_id  = rgp.dnz_chr_id
1838   and    rgp.chr_id      = p_chr_id
1839   and    rgp.rgd_code    = p_rgd_code
1840   and    rgp.dnz_chr_id  = p_chr_id;
1841 
1842   l_rul_rec             get_rul_csr%ROWTYPE;
1843   l_var_int             varchar2(1) default 'N';
1844   l_is_k_var_rate       varchar2(1) default 'N';
1845 
1846   begin
1847 
1848     print( 'is var rate', 'begin');
1849     l_is_k_var_rate := 'N';
1850     l_var_int := 'N';
1851     Open get_rul_csr(p_chr_id,'LAINTP','LAIIND');
1852     Fetch get_rul_csr into l_rul_rec;
1853     If get_rul_csr%NOTFOUND Then
1854         Null;
1855     Elsif nvl(l_rul_rec.rule_information1,'N') = 'Y' Then
1856         l_var_int := 'Y';
1857     End If;
1858     Close get_rul_csr;
1859 
1860     If l_var_int = 'Y' Then
1861         Open get_rul_csr(p_chr_id,'LAICLC','LAIIND');
1862         Fetch get_rul_csr into l_rul_rec;
1863         If get_rul_csr%NOTFOUND Then
1864             Null;
1865         Elsif nvl(l_rul_rec.rule_information5,'NONE') = 'FORMULA' Then
1866             l_is_k_var_rate := 'Y';
1867         End If;
1868         Close get_rul_csr;
1869     End If;
1870     Return(l_is_k_var_rate);
1871 
1872   End Is_Var_Rate_Contract;
1873 
1874   ---------------------------------------------------------------------------
1875   -- PROCEDURE get_sty_details
1876   --
1877   -- Check status of stream
1878   -- Return Stream Type ID or Name as requested
1879   ---------------------------------------------------------------------------
1880   PROCEDURE get_sty_details (p_sty_id        IN  NUMBER   DEFAULT NULL,
1881                              p_sty_name      IN  VARCHAR2 DEFAULT NULL,
1882                              x_sty_id        OUT NOCOPY NUMBER,
1883                              x_sty_name      OUT NOCOPY VARCHAR2,
1884                              x_return_status OUT NOCOPY VARCHAR2) IS
1885 
1886     CURSOR c_sty_name IS
1887       SELECT styt.name
1888       FROM   okl_strm_type_b sty,
1889              okl_strm_type_tl styt
1890       WHERE  sty.id = p_sty_id
1891         AND  sty.version = '1.0'  -- not really needed in 1159
1892         AND  sty.id = styt.id
1893         AND  styt.language = 'US'
1894         AND  sty.start_date <= TRUNC(SYSDATE)
1895         AND  NVL(sty.end_date, SYSDATE) >= TRUNC(SYSDATE);
1896 
1897     CURSOR c_sty_id IS
1898       SELECT sty.id
1899       FROM   okl_strm_type_b sty,
1900              okl_strm_type_tl styt
1901       WHERE  styt.name = p_sty_name
1902         AND  styt.language = 'US'
1903         AND  styt.id = sty.id
1904         AND  sty.version = '1.0'  -- not really needed in 1159
1905         AND  sty.start_date <= TRUNC(SYSDATE)
1906         AND  NVL(sty.end_date, SYSDATE) >= TRUNC(SYSDATE);
1907 
1908     l_sty_id          NUMBER;
1909     l_sty_name        VARCHAR2(150);
1910 
1911     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_sty_details';
1912 
1913   BEGIN
1914 
1915     print( l_prog_name, 'begin' );
1916 
1917     IF p_sty_id IS NOT NULL THEN
1918 
1919       OPEN c_sty_name;
1920       FETCH c_sty_name INTO l_sty_name;
1921       CLOSE c_sty_name;
1922 
1923       IF l_sty_name IS NOT NULL THEN
1924 
1925         x_sty_name := l_sty_name;
1926         x_sty_id   := p_sty_id;
1927 
1928       ELSE
1929 
1930         OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
1931                             p_msg_name     => 'OKL_STREAM_ID_NOT_FOUND',
1932                             p_token1       => 'STY_ID',
1933                             p_token1_value => p_sty_id);
1934 
1935         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1936 
1937       END IF;
1938 
1939     ELSIF p_sty_name IS NOT NULL THEN
1940 
1941       OPEN c_sty_id;
1942       FETCH c_sty_id INTO l_sty_id;
1943       CLOSE c_sty_id;
1944 
1945       IF l_sty_id IS NOT NULL THEN
1946 
1947         x_sty_id   := l_sty_id;
1948         x_sty_name := p_sty_name;
1949 
1950       ELSE
1951 
1952         OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
1953                             p_msg_name     => 'OKL_STREAM_TYPE_NOT_FOUND',
1954                             p_token1       => 'STY_NAME',
1955                             p_token1_value => p_sty_name);
1956 
1957         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1958 
1959       END IF;
1960 
1961     END IF;
1962 
1963     x_return_status  :=  G_RET_STS_SUCCESS;
1964     print( l_prog_name, 'end' );
1965 
1966   EXCEPTION
1967 
1968     WHEN OKL_API.G_EXCEPTION_ERROR THEN
1969 
1970       x_return_status := G_RET_STS_ERROR;
1971 
1972     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1973 
1974       x_return_status := G_RET_STS_UNEXP_ERROR;
1975 
1976     WHEN OTHERS THEN
1977 
1978       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
1979                            p_msg_name     => G_DB_ERROR,
1980                            p_token1       => G_PROG_NAME_TOKEN,
1981                            p_token1_value => l_prog_name,
1982                            p_token2       => G_SQLCODE_TOKEN,
1983                            p_token2_value => sqlcode,
1984                            p_token3       => G_SQLERRM_TOKEN,
1985                            p_token3_value => sqlerrm);
1986 
1987       x_return_status := G_RET_STS_UNEXP_ERROR;
1988 
1989   END get_sty_details;
1990 
1991 
1992   ---------------------------------------------------------------------------
1993   -- PROCEDURE get_mapped_stream
1994   --
1995   -- DESC
1996   -- Repository of hard-coded stream type mapping
1997   --
1998   -- USAGE
1999   -- Driven by mapping type.  Must provide mapping type
2000   ---------------------------------------------------------------------------
2001   PROCEDURE get_mapped_stream (p_sty_purpose       IN         VARCHAR2  DEFAULT NULL,
2002                                p_line_style        IN         VARCHAR2,
2003                                p_mapping_type      IN         VARCHAR2,
2004                                p_deal_type         IN         VARCHAR2,
2005                                p_primary_sty_id    IN         NUMBER DEFAULT NULL,
2006                                p_fee_type          IN         VARCHAR2 DEFAULT NULL,
2007                                p_recurr_yn         IN         VARCHAR2 DEFAULT NULL,
2008                                p_pt_yn             IN         VARCHAR2  DEFAULT 'N',
2009                                p_khr_id            IN         NUMBER,
2010                                p_stream_type_purpose        IN         VARCHAR2  DEFAULT NULL,
2011                                x_mapped_stream     OUT NOCOPY VARCHAR2,
2012                                x_return_status     OUT NOCOPY VARCHAR2) IS
2013 
2014     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_mapped_stream';
2015     l_sty_id NUMBER;
2016 
2017     CURSOR c_hdr IS
2018 		    select chr.orig_system_source_code,
2019            chr.start_date,
2020            chr.end_date,
2021            chr.template_yn,
2022 	   chr.authoring_org_id,
2023 	   chr.inv_organization_id,
2024            khr.deal_type,
2025            to_char(pdt.id)  pid, --358660899972842057983133434721270318297
2026 	   nvl(pdt.reporting_pdt_id, -1) report_pdt_id,
2027            chr.currency_code currency_code,
2028            khr.term_duration term
2029     from   okc_k_headers_v chr,
2030            okl_k_headers khr,
2031            okl_products_v pdt
2032     where khr.id = chr.id
2033         and chr.id = p_khr_id
2034         and khr.pdt_id = pdt.id(+);
2035 
2036     r_hdr c_hdr%ROWTYPE;
2037 
2038     Cursor c_rollover_pmnts IS
2039     Select distinct nvl(slh.object1_id1, -1) styId
2040     From   OKC_RULE_GROUPS_B rgp,
2041            OKC_RULES_B sll,
2042            okc_rules_b slh,
2043 	   okl_strm_type_b sty
2044     Where  slh.rgp_id = rgp.id
2045        and rgp.RGD_CODE = 'LALEVL'
2046        and sll.RULE_INFORMATION_CATEGORY = 'LASLL'
2047        and slh.RULE_INFORMATION_CATEGORY = 'LASLH'
2048        AND TO_CHAR(slh.id) = sll.object2_id1
2049        and slh.object1_id1 = sty.id
2050        and sty.stream_type_purpose = 'RENT'
2051        and rgp.dnz_chr_id = p_khr_id;
2052 
2053     r_rollover_pmnts c_rollover_pmnts%ROWTYPE;
2054 
2055 
2056 			       l_primary_sty_id    NUMBER;
2057   BEGIN
2058 
2059     -- Enhancement pending to remove hard coding dependency (TBD by MCHAKRAB, SRAWLING)
2060     -- Accrual Stream Type will be specified during setup of Payment Stream Type
2061 
2062             OPEN c_rollover_pmnts;
2063             FETCH c_rollover_pmnts INTO r_rollover_pmnts;
2064             CLOSE c_rollover_pmnts;
2065 
2066 	    l_primary_sty_id := nvl(p_primary_sty_id, r_rollover_pmnts.styId);
2067 
2068     print( l_prog_name, 'begin' );
2069     print( l_prog_name, ' mapping type ' || p_mapping_type || ' sty ame ' || p_sty_purpose || ' deal type ' || p_deal_type );
2070     IF p_mapping_type = 'ACCRUAL' THEN
2071 
2072       IF p_sty_purpose = 'RENT' AND p_deal_type = 'LEASEOP' AND
2073          (p_line_style = 'FREE_FORM1' OR p_line_style IS NULL) THEN
2074          --x_mapped_stream  :=  'RENTAL ACCRUAL';
2075     print( l_prog_name, '##1##' );
2076              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2077                                          p_khr_id                => p_khr_id,
2078 					 p_deal_type             => p_deal_type,
2079 					 p_primary_sty_id        => l_primary_sty_id,
2080                                          p_dependent_sty_purpose => 'RENT_ACCRUAL',
2081                                          x_return_status         => x_return_status,
2082                                          x_dependent_sty_id      => l_sty_id,
2083                                          x_dependent_sty_name    => x_mapped_stream);
2084 
2085       ELSIF p_sty_purpose = 'RENT' AND p_deal_type <> 'LEASEOP' AND
2086          (p_line_style = 'FREE_FORM1' OR p_line_style IS NULL) THEN
2087     print( l_prog_name, '##2##' );
2088          x_mapped_stream  :=  NULL;
2089       ELSIF p_pt_yn = 'Y' THEN
2090     print( l_prog_name, '##3##' );
2091          --x_mapped_stream := 'PASS THROUGH REVENUE ACCRUAL';
2092          --If condition added by mansrini on 10-Jun-2005 for generating accrual streams for Service Lines
2093          --Bug 4434343 -Start of Changes
2094 
2095 	 IF (p_line_style = 'FEE') THEN
2096              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2097                                          p_khr_id                => p_khr_id,
2098 					 p_deal_type             => p_deal_type,
2099 					 p_primary_sty_id        => l_primary_sty_id,
2100                                          p_dependent_sty_purpose => 'PASS_THRU_REV_ACCRUAL',
2101                                          x_return_status         => x_return_status,
2102                                          x_dependent_sty_id      => l_sty_id,
2103                                          x_dependent_sty_name    => x_mapped_stream);
2104 
2105         ELSIF ((p_line_style = 'SOLD_SERVICE') OR (p_line_style = 'LINK_SERV_ASSET')) THEN
2106              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2107                                          p_khr_id                => p_khr_id,
2108 					 p_deal_type             => p_deal_type,
2109 					 p_primary_sty_id        => l_primary_sty_id,
2110                                          p_dependent_sty_purpose => 'PASS_THRU_SVC_REV_ACCRUAL',
2111                                          x_return_status         => x_return_status,
2112                                          x_dependent_sty_id      => l_sty_id,
2113                                          x_dependent_sty_name    => x_mapped_stream);
2114          END IF;
2115          --Bug 4434343 -End of Changes
2116 
2117       ELSIF ((p_line_style = 'SOLD_SERVICE') OR (p_line_style = 'LINK_SERV_ASSET')) THEN
2118     print( l_prog_name, '##4##' );
2119         --x_mapped_stream  :=  'SERVICE INCOME';
2120              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2121                                          p_khr_id                => p_khr_id,
2122 					 p_deal_type             => p_deal_type,
2123 					 p_primary_sty_id        => l_primary_sty_id,
2124                                          p_dependent_sty_purpose => 'SERVICE_INCOME',
2125                                          x_return_status         => x_return_status,
2126                                          x_dependent_sty_id      => l_sty_id,
2127                                          x_dependent_sty_name    => x_mapped_stream);
2128 
2129       ELSIF (p_line_style IN ('FEE', 'FREE_FORM1') OR p_line_style IS NULL) AND
2130             (p_sty_purpose <> 'SECURITY_DEPOSIT') THEN
2131 
2132         If ( p_fee_type = 'INCOME' AND p_recurr_yn = OKL_API.G_FALSE ) Then
2133 	    --x_mapped_stream := 'AMORTIZED FEE INCOME';
2134              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2135                                          p_khr_id                => p_khr_id,
2136 					 p_deal_type             => p_deal_type,
2137 					 p_primary_sty_id        => l_primary_sty_id,
2138                                          p_dependent_sty_purpose => 'AMORTIZE_FEE_INCOME',
2139                                          x_return_status         => x_return_status,
2140                                          x_dependent_sty_id      => l_sty_id,
2141                                          x_dependent_sty_name    => x_mapped_stream);
2142 
2143 	elsif ( p_fee_type = 'FINANCED' OR p_fee_type = 'ROLLOVER') Then
2144 	    x_mapped_stream := NULL;
2145 	else
2146             --x_mapped_stream  :=  'FEE INCOME';
2147              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2148                                          p_khr_id                => p_khr_id,
2149 					 p_deal_type             => p_deal_type,
2150 					 p_primary_sty_id        => l_primary_sty_id,
2151                                          p_dependent_sty_purpose => 'ACCRUED_FEE_INCOME',
2152                                          x_return_status         => x_return_status,
2153                                          x_dependent_sty_id      => l_sty_id,
2154                                          x_dependent_sty_name    => x_mapped_stream);
2155 
2156 	end if;
2157 
2158       ELSE
2159         x_mapped_stream  :=  NULL;
2160       END IF;
2161 
2162     ELSIF p_mapping_type = 'PRE-TAX INCOME' THEN
2163 
2164       IF (p_deal_type IN ('LEASEDF', 'LEASEST')) THEN
2165         --x_mapped_stream  :=  'PRE-TAX INCOME';
2166              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2167                                          p_khr_id                => p_khr_id,
2168 					 p_deal_type             => p_deal_type,
2169 					 p_primary_sty_id        => l_primary_sty_id,
2170                                          p_dependent_sty_purpose => 'LEASE_INCOME',
2171                                          x_return_status         => x_return_status,
2172                                          x_dependent_sty_id      => l_sty_id,
2173                                          x_dependent_sty_name    => x_mapped_stream);
2174 
2175       ElsIF (p_deal_type IN ('LOAN', 'LOAN-REVOLVING')) OR ( p_fee_type in ('FINANCED', 'ROLLOVER','LINK_FEE_ASSET') ) THEN
2176         --x_mapped_stream  :=  'PRE-TAX INCOME';
2177              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
2178                                          p_khr_id                => p_khr_id,
2179 					 p_deal_type             => p_deal_type,
2180 					 p_primary_sty_id        => l_primary_sty_id,
2181                                          p_dependent_sty_purpose => 'INTEREST_INCOME',
2182                                          x_return_status         => x_return_status,
2183                                          x_dependent_sty_id      => l_sty_id,
2184                                          x_dependent_sty_name    => x_mapped_stream);
2185       ELSE
2186         x_mapped_stream  :=  NULL;
2187       END IF;
2188 
2189     ELSIF p_mapping_type = 'PAYMENT' THEN
2190 
2191       IF p_sty_purpose = 'RENT' AND p_deal_type IN ('LOAN', 'LOAN-REVOLVING') THEN
2192         x_mapped_stream  := 'LOAN PAYMENT';
2193       ELSE
2194         x_mapped_stream  :=  p_sty_purpose;
2195       END IF;
2196 
2197     ELSIF p_mapping_type = 'CAPITAL RECOVERY' THEN
2198 
2199       IF p_deal_type IN ('LEASEDF', 'LEASEST', 'LOAN', 'LOAN-REVOLVING') THEN
2200         x_mapped_stream  :=  'PRINCIPAL PAYMENT';
2201       ELSE
2202         x_mapped_stream  :=  NULL;
2203       END IF;
2204 
2205     ELSIF p_mapping_type = 'INTEREST PAYMENT' THEN
2206 
2207       IF p_deal_type IN ('LEASEDF', 'LEASEST', 'LOAN', 'LOAN-REVOLVING') THEN
2208         x_mapped_stream  :=  'INTEREST PAYMENT';
2209       ELSE
2210         x_mapped_stream  :=  NULL;
2211       END IF;
2212 
2213     ELSIF p_mapping_type = 'CLOSE BOOK' THEN
2214 
2215       IF p_deal_type IN ('LEASEDF', 'LEASEST', 'LOAN', 'LOAN-REVOLVING') THEN
2216         x_mapped_stream  :=  'PRINCIPAL BALANCE';
2217       ELSE
2218         x_mapped_stream  :=  NULL;
2219       END IF;
2220 
2221    ELSIF p_mapping_type = 'DISBURSEMENT' THEN
2222 
2223       OPEN c_hdr;
2224       FETCH c_hdr INTO r_hdr;
2225       CLOSE c_hdr;
2226       IF p_deal_type = 'LEASE' THEN
2227       If ( p_sty_purpose = 'RENT' ) THen
2228         --x_mapped_stream  :=  'INVESTOR RENT DISBURSEMENT BASIS';
2229                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2230                                                 p_khr_id              => p_khr_id,
2231 					        p_pdt_id              => r_hdr.pid,
2232                                                 p_primary_sty_purpose => 'INVESTOR_RENT_DISB_BASIS',
2233                                                 x_return_status       => x_return_status,
2234                                                 x_primary_sty_id      => l_sty_id,
2235                                                 x_primary_sty_name    => x_mapped_stream);
2236 
2237       ElsIf ( p_sty_purpose = 'RESIDUAL' ) Then
2238         --x_mapped_stream  :=  'INVESTOR RESIDUAL DISBURSEMENT BASIS';
2239                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2240                                                 p_khr_id              => p_khr_id,
2241 					        p_pdt_id              => r_hdr.pid,
2242                                                 p_primary_sty_purpose => 'INVESTOR_RESIDUAL_DISB_BASIS',
2243                                                 x_return_status       => x_return_status,
2244                                                 x_primary_sty_id      => l_sty_id,
2245                                                 x_primary_sty_name    => x_mapped_stream);
2246        End If;
2247        ELSIF p_deal_type = 'LOAN' THEN
2248          If ( p_sty_purpose = 'LOAN_PAYMENT' ) Then
2249             IF p_stream_type_purpose = 'PRINCIPAL_PAYMENT' THEN
2250               OKL_ISG_UTILS_PVT.get_primary_stream_type(
2251                                                 p_khr_id              => p_khr_id,
2252                                                 p_pdt_id              => r_hdr.pid,
2253                                                 p_primary_sty_purpose => 'INVESTOR_PRINCIPAL_DISB_BASIS',
2254                                                 x_return_status       => x_return_status,
2255                                                 x_primary_sty_id      => l_sty_id,
2256                                                 x_primary_sty_name    => x_mapped_stream);
2257            ELSIF p_stream_type_purpose = 'INTEREST_PAYMENT' THEN
2258                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2259                                                 p_khr_id              => p_khr_id,
2260                                                 p_pdt_id              => r_hdr.pid,
2261                                                 p_primary_sty_purpose => 'INVESTOR_INTEREST_DISB_BASIS',
2262                                                 x_return_status       => x_return_status,
2263                                                 x_primary_sty_id      => l_sty_id,
2264                                                 x_primary_sty_name    => x_mapped_stream);
2265              ELSIF p_stream_type_purpose = 'UNSCHEDULED_PRINCIPAL_PAYMENT' THEN
2266                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2267                                                 p_khr_id              => p_khr_id,
2268                                                 p_pdt_id              => r_hdr.pid,
2269                                                 p_primary_sty_purpose => 'INVESTOR_PPD_DISB_BASIS',
2270                                                 x_return_status       => x_return_status,
2271                                                 x_primary_sty_id      => l_sty_id,
2272                                                 x_primary_sty_name    => x_mapped_stream);
2273 
2274             End If;
2275           End If;
2276         End If;
2277     ELSIF p_mapping_type = 'PV_DISBURSEMENT' THEN
2278 
2279       OPEN c_hdr;
2280       FETCH c_hdr INTO r_hdr;
2281       CLOSE c_hdr;
2282       IF p_deal_type = 'LEASE' THEN
2283       If ( p_sty_purpose = 'RENT' ) THen
2284                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2285                                                 p_khr_id              => p_khr_id,
2286                                                 p_pdt_id              => r_hdr.pid,
2287                                                 p_primary_sty_purpose => 'PV_RENT_SECURITIZED',
2288                                                 x_return_status       => x_return_status,
2289                                                 x_primary_sty_id      => l_sty_id,
2290                                                 x_primary_sty_name    => x_mapped_stream);
2291 
2292       ElsIf ( p_sty_purpose = 'RESIDUAL' ) Then
2293                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2294                                                 p_khr_id              => p_khr_id,
2295                                                 p_pdt_id              => r_hdr.pid,
2296                                                 p_primary_sty_purpose => 'PV_RV_SECURITIZED',
2297                                                 x_return_status       => x_return_status,
2298                                                 x_primary_sty_id      => l_sty_id,
2299                                                 x_primary_sty_name    => x_mapped_stream);
2300        End If;
2301        ELSIF p_deal_type = 'LOAN' THEN
2302          If ( p_sty_purpose = 'LOAN_PAYMENT' ) Then
2303             IF p_stream_type_purpose = 'PRINCIPAL_PAYMENT' THEN
2304               OKL_ISG_UTILS_PVT.get_primary_stream_type(
2305                                                 p_khr_id              => p_khr_id,
2306                                                 p_pdt_id              => r_hdr.pid,
2307                                                 p_primary_sty_purpose => 'PV_PRINCIPAL_SECURITIZED',
2308                                                 x_return_status       => x_return_status,
2309                                                 x_primary_sty_id      => l_sty_id,
2310                                                 x_primary_sty_name    => x_mapped_stream);
2311            ELSIF p_stream_type_purpose = 'INTEREST_PAYMENT' THEN
2312                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2313                                                 p_khr_id              => p_khr_id,
2314                                                 p_pdt_id              => r_hdr.pid,
2315                                                 p_primary_sty_purpose => 'PV_INTEREST_SECURITIZED',
2316                                                 x_return_status       => x_return_status,
2317                                                 x_primary_sty_id      => l_sty_id,
2318                                                 x_primary_sty_name    => x_mapped_stream);
2319             ELSIF p_stream_type_purpose = 'UNSCHEDULED_PRINCIPAL_PAYMENT' THEN
2320                OKL_ISG_UTILS_PVT.get_primary_stream_type(
2321                                                 p_khr_id              => p_khr_id,
2322                                                 p_pdt_id              => r_hdr.pid,
2323                                                 p_primary_sty_purpose => 'PV_UNSCHEDULED_PMT_SECURITIZED',
2324                                                 x_return_status       => x_return_status,
2325                                                 x_primary_sty_id      => l_sty_id,
2326                                                 x_primary_sty_name    => x_mapped_stream);
2327 
2328             End If;
2329           End If;
2330         End If;
2331 
2332     ELSE
2333 
2334       x_mapped_stream  :=  NULL;
2335 
2336     END IF;
2337 
2338     x_return_status  :=  G_RET_STS_SUCCESS;
2339     print( l_prog_name, 'end' );
2340 
2341   EXCEPTION
2342 
2343     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2344 
2345       x_return_status := G_RET_STS_ERROR;
2346 
2347     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2348 
2349       x_return_status := G_RET_STS_UNEXP_ERROR;
2350 
2351     WHEN OTHERS THEN
2352 
2353       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
2354                            p_msg_name     => G_DB_ERROR,
2355                            p_token1       => G_PROG_NAME_TOKEN,
2356                            p_token1_value => l_prog_name,
2357                            p_token2       => G_SQLCODE_TOKEN,
2358                            p_token2_value => sqlcode,
2359                            p_token3       => G_SQLERRM_TOKEN,
2360                            p_token3_value => sqlerrm);
2361 
2362       x_return_status := G_RET_STS_UNEXP_ERROR;
2363 
2364   END get_mapped_stream;
2365 
2366 
2367 
2368   ---------------------------------------------------------------------------
2369   -- FUNCTION get_months_factor
2370   ---------------------------------------------------------------------------
2371   FUNCTION get_months_factor( p_frequency     IN VARCHAR2,
2372                               x_return_status OUT NOCOPY VARCHAR2) RETURN NUMBER IS
2373     l_months  NUMBER;
2374     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_months_factor';
2375   BEGIN
2376     IF p_frequency = 'M' THEN
2377       l_months := 1;
2378     ELSIF p_frequency = 'Q' THEN
2379       l_months := 3;
2380     ELSIF p_frequency = 'S' THEN
2381       l_months := 6;
2382     ELSIF p_frequency = 'A' THEN
2383       l_months := 12;
2384     END IF;
2385     --print( 'get_months_factor: p_frequency, l_months = ' || p_frequency || ',' || l_months );
2386     IF l_months IS NOT NULL THEN
2387       x_return_status := G_RET_STS_SUCCESS;
2388       RETURN l_months;
2389     ELSE
2390       OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
2391                           p_msg_name     => 'OKL_INVALID_FREQUENCY_CODE',
2392                           p_token1       => 'FRQ_CODE',
2393                           p_token1_value => p_frequency);
2394       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2395     END IF;
2396   EXCEPTION
2397     WHEN OKL_API.G_EXCEPTION_ERROR
2398     THEN
2399       x_return_status := G_RET_STS_ERROR;
2400     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR
2401     THEN
2402       x_return_status := G_RET_STS_UNEXP_ERROR;
2403     WHEN OTHERS
2404     THEN
2405       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
2406                            p_msg_name     => G_DB_ERROR,
2407                            p_token1       => G_PROG_NAME_TOKEN,
2408                            p_token1_value => l_prog_name,
2409                            p_token2       => G_SQLCODE_TOKEN,
2410                            p_token2_value => sqlcode,
2411                            p_token3       => G_SQLERRM_TOKEN,
2412                            p_token3_value => sqlerrm);
2413       x_return_status := G_RET_STS_UNEXP_ERROR;
2414   END get_months_factor;
2415 
2416 
2417   ---------------------------------------------------------------------------
2418   -- FUNCTION get_first_sel_date
2419   ---------------------------------------------------------------------------
2420 -- djanaswa ER6274342 added parameter p_arrears_pay_dates_option
2421 
2422   FUNCTION get_first_sel_date( p_start_date          IN    DATE,
2423                                p_advance_or_arrears  IN    VARCHAR2,
2424                                p_months_increment    IN    NUMBER,
2425                                p_arrears_pay_dates_option IN VARCHAR2,
2426                                x_return_status       OUT NOCOPY VARCHAR2) RETURN DATE IS
2427     l_date  DATE;
2428 
2429     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_first_sel_date';
2430 
2431   BEGIN
2432 
2433 
2434     print( l_prog_name, 'begin' );
2435 
2436     --Added by kthiruva on 04-01-2005
2437     --Bug 4100610 - Start of Changes
2438     IF ((p_advance_or_arrears = 'ADVANCE') OR (p_advance_or_arrears = 'N')) THEN
2439       l_date  :=  TRUNC(p_start_date);
2440     ELSIF ((p_advance_or_arrears = 'ARREARS') OR (p_advance_or_arrears = 'Y')) THEN
2441     -- djanaswa ER 6274342 Added IF condition
2442          IF p_arrears_pay_dates_option = 'FIRST_DAY_OF_NEXT_PERIOD' THEN
2443                 l_date  :=  ADD_MONTHS(TRUNC(p_start_date), p_months_increment);
2444             ELSE
2445                l_date  :=  ADD_MONTHS(TRUNC(p_start_date), p_months_increment) - 1;
2446          END IF;
2447     END IF;
2448     --Bug 4100610 - End Of Changes
2449     IF l_date IS NOT NULL THEN
2450       x_return_status := G_RET_STS_SUCCESS;
2451       RETURN l_date;
2452     ELSE
2453       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2454     END IF;
2455 
2456     print( l_prog_name, 'end' );
2457   EXCEPTION
2458 
2459     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2460 
2461       x_return_status := G_RET_STS_ERROR;
2462 
2463     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2464 
2465       x_return_status := G_RET_STS_UNEXP_ERROR;
2466 
2467     WHEN OTHERS THEN
2468 
2469       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
2470                            p_msg_name     => G_DB_ERROR,
2471                            p_token1       => G_PROG_NAME_TOKEN,
2472                            p_token1_value => l_prog_name,
2473                            p_token2       => G_SQLCODE_TOKEN,
2474                            p_token2_value => sqlcode,
2475                            p_token3       => G_SQLERRM_TOKEN,
2476                            p_token3_value => sqlerrm);
2477 
2478       x_return_status := G_RET_STS_UNEXP_ERROR;
2479 
2480   END get_first_sel_date;
2481 
2482 
2483   ---------------------------------------------------------------------------
2484   -- FUNCTION get_advance_count
2485   ---------------------------------------------------------------------------
2486   FUNCTION get_advance_count( p_structure       IN   VARCHAR2,
2487                               x_return_status   OUT NOCOPY VARCHAR2) RETURN NUMBER IS
2488 
2489     l_count  NUMBER;
2490     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_advance_count';
2491 
2492 
2493   BEGIN
2494 
2495     print( l_prog_name, 'begin' );
2496     IF NVL(p_structure, 'LEVEL') IN ('LEVEL', '0') THEN
2497       l_count := 0;
2498     ELSIF p_structure IN ('1STLAST', '1') THEN
2499       l_count := 1;
2500     ELSIF p_structure IN ('1STLAST2', '2')  THEN
2501       l_count := 2;
2502     ELSIF p_structure IN ('1STLAST3', '3')  THEN
2503       l_count := 3;
2504     ELSE
2505       OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
2506                           p_msg_name     => 'OKL_INVALID_PAYMENT_STRUCTURE',
2507                           p_token1       => 'STRUCTURE_CODE',
2508                           p_token1_value => p_structure);
2509       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2510     END IF;
2511 
2512     x_return_status := G_RET_STS_SUCCESS;
2513     print( l_prog_name, 'end' );
2514     RETURN l_count;
2515 
2516   EXCEPTION
2517 
2518     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2519 
2520       x_return_status := G_RET_STS_ERROR;
2521 
2522     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2523 
2524       x_return_status := G_RET_STS_UNEXP_ERROR;
2525 
2526     WHEN OTHERS THEN
2527 
2528       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
2529                            p_msg_name     => G_DB_ERROR,
2530                            p_token1       => G_PROG_NAME_TOKEN,
2531                            p_token1_value => l_prog_name,
2532                            p_token2       => G_SQLCODE_TOKEN,
2533                            p_token2_value => sqlcode,
2534                            p_token3       => G_SQLERRM_TOKEN,
2535                            p_token3_value => sqlerrm);
2536 
2537       x_return_status := G_RET_STS_UNEXP_ERROR;
2538 
2539   END get_advance_count;
2540 
2541 
2542   ---------------------------------------------------------------------------
2543   -- FUNCTION get_fractional_month
2544   ---------------------------------------------------------------------------
2545   FUNCTION get_fractional_month( p_start_date    IN  DATE,
2546                                  x_return_status OUT NOCOPY VARCHAR2) RETURN NUMBER IS
2547 
2548     l_fraction  NUMBER;
2549 
2550     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_fractional_month';
2551 
2552   BEGIN
2553 
2554     print( l_prog_name, 'begin' );
2555     IF TO_CHAR(p_start_date, 'DD') IN ('30', '31') OR
2556        (TO_CHAR(p_start_date, 'MON') = 'FEB' AND TO_CHAR(p_start_date, 'DD') IN ('28', '29')) THEN
2557       l_fraction := 0;
2558     ELSIF p_start_date = (ADD_MONTHS(LAST_DAY(p_start_date), -1) + 1) THEN
2559       l_fraction :=1;
2560     ELSE
2561       l_fraction := (30 - TO_CHAR(p_start_date-1, 'DD')) / 30;
2562     END IF;
2563 
2564     x_return_status := G_RET_STS_SUCCESS;
2565     print( l_prog_name, 'end' );
2566     RETURN l_fraction;
2567 
2568   EXCEPTION
2569 
2570     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2571 
2572       x_return_status := G_RET_STS_ERROR;
2573 
2574     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2575 
2576       x_return_status := G_RET_STS_UNEXP_ERROR;
2577 
2578     WHEN OTHERS THEN
2579 
2580       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
2581                            p_msg_name     => G_DB_ERROR,
2582                            p_token1       => G_PROG_NAME_TOKEN,
2583                            p_token1_value => l_prog_name,
2584                            p_token2       => G_SQLCODE_TOKEN,
2585                            p_token2_value => sqlcode,
2586                            p_token3       => G_SQLERRM_TOKEN,
2587                            p_token3_value => sqlerrm);
2588 
2589       x_return_status := G_RET_STS_UNEXP_ERROR;
2590 
2591   END get_fractional_month;
2592 
2593 
2594   ---------------------------------------------------------------------------
2595   -- PROCEDURE get_stream_header
2596   ---------------------------------------------------------------------------
2597   PROCEDURE get_stream_header(p_purpose_code   IN  VARCHAR2,
2598                               p_khr_id         IN  NUMBER,
2599                               p_kle_id         IN  NUMBER,
2600                               p_sty_id         IN  NUMBER,
2601                               x_stmv_rec       OUT NOCOPY okl_stm_pvt.stmv_rec_type,
2602                               x_return_status  OUT NOCOPY VARCHAR2) IS
2603 
2604     l_stmv_rec                okl_stm_pvt.stmv_rec_type;
2605     l_transaction_number      NUMBER;
2606 
2607     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_stream_header';
2608 
2609 
2610   BEGIN
2611 
2612     --print( l_prog_name, 'begin' );
2613     SELECT okl_sif_seq.nextval INTO l_transaction_number FROM DUAL;
2614 
2615     -- NOTE: UV for Streams inquiry (OKL_ASSET_STREAMS_UV) assumes a denormalized use of KHR_ID
2616 
2617     l_stmv_rec.khr_id  :=  p_khr_id;
2618     l_stmv_rec.kle_id              :=  p_kle_id;
2619     l_stmv_rec.sty_id              :=  p_sty_id;
2620     l_stmv_rec.sgn_code            :=  'MANL';
2621     l_stmv_rec.say_code            :=  'WORK';                    --  calling API will update to CURR as required
2622     l_stmv_rec.active_yn           :=  'N';                       --  calling API will update to Y as required
2623     l_stmv_rec.transaction_number  :=  l_transaction_number;      --  approved by AKJAIN
2624     -- l_stmv_rec.date_current        :=  NULL;                                    --  TBD
2625     l_stmv_rec.date_working        :=  SYSDATE;                                    --  TBD
2626     -- l_stmv_rec.date_history        :=  NULL;                                    --  TBD
2627     -- l_stmv_rec.comments            :=  NULL;                                    --  TBD
2628 
2629     IF p_purpose_code = 'REPORT' THEN
2630 
2631       l_stmv_rec.purpose_code := 'REPORT';
2632 
2633     END IF;
2634 
2635     x_stmv_rec                     := l_stmv_rec;
2636     x_return_status                := G_RET_STS_SUCCESS;
2637 
2638     --print( l_prog_name, 'end' );
2639   EXCEPTION
2640 
2641     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2642 
2643       x_return_status := G_RET_STS_ERROR;
2644 
2645     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2646 
2647       x_return_status := G_RET_STS_UNEXP_ERROR;
2648 
2649     WHEN OTHERS THEN
2650 
2651       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
2652                            p_msg_name     => G_DB_ERROR,
2653                            p_token1       => G_PROG_NAME_TOKEN,
2654                            p_token1_value => l_prog_name,
2655                            p_token2       => G_SQLCODE_TOKEN,
2656                            p_token2_value => sqlcode,
2657                            p_token3       => G_SQLERRM_TOKEN,
2658                            p_token3_value => sqlerrm);
2659 
2660       x_return_status := G_RET_STS_UNEXP_ERROR;
2661 
2662   END get_stream_header;
2663 
2664   ---------------------------------------------------------------------------
2665   -- PROCEDURE get_stream_elements
2666   --
2667   -- Description
2668   -- Populates Stream Elements array for contiguous periodic charges/expenses
2669   --
2670   -- Added parameter p_recurrence_date by djanaswa for bug 6007644
2671   ---------------------------------------------------------------------------
2672   PROCEDURE get_stream_elements( p_start_date          IN      DATE,
2673                                  p_periods             IN      NUMBER,
2674                                  p_frequency           IN      VARCHAR2,
2675                                  p_structure           IN      VARCHAR2,
2676                                  p_advance_or_arrears  IN      VARCHAR2,
2677                                  p_amount              IN      NUMBER,
2678                                  p_stub_days           IN      NUMBER,
2679                                  p_stub_amount         IN      NUMBER,
2680                                  p_currency_code       IN      VARCHAR2,
2681                                  p_khr_id              IN      NUMBER,
2682                                  p_kle_id              IN      NUMBER,
2683                                  p_purpose_code        IN      VARCHAR2,
2684                                  x_selv_tbl            OUT NOCOPY okl_sel_pvt.selv_tbl_type,
2685                                  x_pt_tbl              OUT NOCOPY okl_sel_pvt.selv_tbl_type,
2686                                  x_pt_pro_fee_tbl      OUT NOCOPY okl_sel_pvt.selv_tbl_type,
2687                                  x_return_status       OUT NOCOPY VARCHAR2,
2688                                  x_msg_count           OUT NOCOPY NUMBER,
2689                                  x_msg_data            OUT NOCOPY VARCHAR2,
2690                                  p_recurrence_date     IN      DATE) IS
2691 
2692 
2693     top_svc_rec top_svc_csr%ROWTYPE;
2694 
2695     lx_return_status             VARCHAR2(1);
2696 
2697     l_months_factor              NUMBER;
2698     l_first_sel_date             DATE;
2699     l_element_count              NUMBER;
2700     l_base_amount                NUMBER;
2701     l_amount                     NUMBER;
2702 
2703     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_stream_elements';
2704 
2705 -- djanaswa ER6274342 start
2706     l_arrears_pay_dates_option  okl_st_gen_tmpt_sets_all.isg_arrears_pay_dates_option%type;
2707 -- djanaswa ER6274342 end
2708 
2709 
2710   BEGIN
2711 
2712     print( l_prog_name, 'begin' );
2713     l_months_factor := get_months_factor( p_frequency       =>   p_frequency,
2714                                           x_return_status   =>   lx_return_status);
2715 
2716     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
2717       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2718     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
2719       RAISE OKL_API.G_EXCEPTION_ERROR;
2720     END IF;
2721 
2722 
2723 -- djanaswa ER6274342 start
2724      IF p_advance_or_arrears = 'ARREARS' THEN
2725        OKL_ISG_UTILS_PVT.get_arrears_pay_dates_option(
2726         p_khr_id                   => p_khr_id,
2727         x_arrears_pay_dates_option => l_arrears_pay_dates_option,
2728         x_return_status            => lx_return_status);
2729 
2730        IF(lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2731          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2732        ELSIF (lx_return_status = OKL_API.G_RET_STS_ERROR) THEN
2733          RAISE OKL_API.G_EXCEPTION_ERROR;
2734        END IF;
2735     END IF;
2736 
2737 -- djanaswa ER6274342 end
2738 
2739 
2740     IF ( p_amount IS NULL )
2741     THEN
2742         l_amount := NULL;
2743     ELSE
2744         l_amount := p_amount;
2745     END IF;
2746 
2747     IF ( p_periods IS NULL ) AND ( p_stub_days IS NOT NULL )
2748     THEN
2749         x_selv_tbl(1).amount                     := p_stub_amount;
2750         x_selv_tbl(1).se_line_number             := 1;                            -- TBD
2751         x_selv_tbl(1).accrued_yn                 := NULL;                         -- TBD
2752 
2753         IF p_advance_or_arrears = 'ARREARS' THEN
2754 -- djanaswa  ER6274342 start
2755            --  x_selv_tbl(1).stream_element_date        := p_start_date + p_stub_days - 1;
2756             x_selv_tbl(1).comments                   := 'Y';
2757 
2758             IF l_arrears_pay_dates_option = 'FIRST_DAY_OF_NEXT_PERIOD' THEN
2759                     x_selv_tbl(1).stream_element_date        := p_start_date + p_stub_days;
2760             ELSE
2761                     x_selv_tbl(1).stream_element_date        := p_start_date + p_stub_days - 1;
2762             END IF;
2763 -- djanaswa  ER6274342 end
2764 
2765         ELSE
2766             x_selv_tbl(1).stream_element_date        := p_start_date;
2767             x_selv_tbl(1).comments                   := 'N';
2768         END IF;
2769 
2770     ELSE
2771 
2772         l_element_count := p_periods;
2773         --print( l_prog_name, 'creating elements: ' || to_char(l_element_count) );
2774 
2775 
2776         FOR i IN 1 .. l_element_count
2777         LOOP
2778             x_selv_tbl(i).amount                     := l_amount;
2779 
2780             IF p_advance_or_arrears = 'ARREARS'
2781             THEN
2782               x_selv_tbl(i).comments := 'Y';
2783             ELSE
2784               x_selv_tbl(i).comments := 'N';
2785             END IF;
2786 
2787             -- Modified by RGOOTY: Start 4371472
2788          --Added parameter p_recurrence_date by djanaswa for bug 6007644
2789          -- added p_arrears_pay_dates_option by DJANASWA ER 6274342
2790             get_sel_date(
2791               p_start_date         => p_start_date,
2792               p_advance_or_arrears => p_advance_or_arrears,
2793               p_periods_after      => i,
2794               p_months_per_period  => l_months_factor,
2795               x_date               => x_selv_tbl(i).stream_element_date,
2796               x_return_status      => lx_return_status,
2797               p_recurrence_date    => p_recurrence_date,
2798               p_arrears_pay_dates_option => l_arrears_pay_dates_option);
2799             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
2800               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2801             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
2802               RAISE OKL_API.G_EXCEPTION_ERROR;
2803             END IF;
2804             -- Modified by RGOOTY: End
2805 
2806             x_selv_tbl(i).se_line_number             := i;                            -- TBD
2807             x_selv_tbl(i).accrued_yn                 := NULL;                         -- TBD
2808         END LOOP;
2809      END IF;
2810 
2811     x_return_status := G_RET_STS_SUCCESS ;
2812     print( l_prog_name, 'end' );
2813 
2814   EXCEPTION
2815 
2816     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2817 
2818       x_return_status := G_RET_STS_ERROR;
2819 
2820     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2821 
2822       x_return_status := G_RET_STS_UNEXP_ERROR;
2823 
2824     WHEN OTHERS THEN
2825 
2826       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
2827                            p_msg_name     => G_DB_ERROR,
2828                            p_token1       => G_PROG_NAME_TOKEN,
2829                            p_token1_value => l_prog_name,
2830                            p_token2       => G_SQLCODE_TOKEN,
2831                            p_token2_value => sqlcode,
2832                            p_token3       => G_SQLERRM_TOKEN,
2833                            p_token3_value => sqlerrm);
2834 
2835       x_return_status := G_RET_STS_UNEXP_ERROR;
2836 
2837   END get_stream_elements;
2838 
2839 
2840   ---------------------------------------------------------------------------
2841   -- PROCEDURE get_stream_elements
2842   --
2843   -- Description
2844   -- Populates Stream Elements array for contiguous periodic charges/expenses
2845   --
2846   -- Added parameter p_recurrence_date by djanaswa for bug 6007644
2847   ---------------------------------------------------------------------------
2848   PROCEDURE get_stream_elements( p_start_date          IN      DATE,
2849                                  p_periods             IN      NUMBER,
2850                                  p_frequency           IN      VARCHAR2,
2851                                  p_structure           IN      VARCHAR2,
2852                                  p_advance_or_arrears  IN      VARCHAR2,
2853                                  p_amount              IN      NUMBER,
2854                                  p_stub_days           IN      NUMBER,
2855                                  p_stub_amount         IN      NUMBER,
2856                                  p_currency_code       IN      VARCHAR2,
2857                                  p_khr_id              IN      NUMBER,
2858                                  p_kle_id              IN      NUMBER,
2859                                  p_purpose_code        IN      VARCHAR2,
2860                                  x_selv_tbl            OUT NOCOPY okl_sel_pvt.selv_tbl_type,
2861                                  x_pt_tbl              OUT NOCOPY okl_sel_pvt.selv_tbl_type,
2862                                  x_return_status       OUT NOCOPY VARCHAR2,
2863                                  x_msg_count           OUT NOCOPY NUMBER,
2864                                  x_msg_data            OUT NOCOPY VARCHAR2,
2865                                  p_recurrence_date     IN      DATE) IS
2866 
2867      CURSOR c_pt_perc( kleid NUMBER) IS
2868       SELECT NVL(TO_NUMBER(rul.rule_information1), 100) pass_through_percentage
2869       FROM   okc_rule_groups_b rgp,
2870              okc_rules_b rul
2871       WHERE  rgp.cle_id = kleid
2872         AND  rgp.rgd_code = 'LAPSTH'
2873         AND  rgp.id = rul.rgp_id
2874         AND  rul.rule_information_category = 'LAPTPR';
2875 
2876     rec_pt_perc c_pt_perc%ROWTYPE;
2877 
2878     top_svc_rec top_svc_csr%ROWTYPE;
2879 
2880     l_pt_perc                    NUMBER;
2881 
2882     lx_return_status             VARCHAR2(1);
2883 
2884     l_months_factor              NUMBER;
2885     l_first_sel_date             DATE;
2886     l_element_count              NUMBER;
2887     l_base_amount                NUMBER;
2888     l_amount                     NUMBER;
2889 
2890 -- djanaswa ER6274342 start
2891     l_arrears_pay_dates_option  okl_st_gen_tmpt_sets_all.isg_arrears_pay_dates_option%type;
2892 -- djanaswa ER6274342 end
2893 
2894     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_stream_elements';
2895 
2896   BEGIN
2897 
2898     print( l_prog_name, 'begin' );
2899     l_months_factor := get_months_factor( p_frequency       =>   p_frequency,
2900                                           x_return_status   =>   lx_return_status);
2901 
2902     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
2903       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2904     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
2905       RAISE OKL_API.G_EXCEPTION_ERROR;
2906     END IF;
2907 
2908 -- djanaswa ER6274342 start
2909      IF p_advance_or_arrears = 'ARREARS' THEN
2910        OKL_ISG_UTILS_PVT.get_arrears_pay_dates_option(
2911         p_khr_id                   => p_khr_id,
2912         x_arrears_pay_dates_option => l_arrears_pay_dates_option,
2913         x_return_status            => lx_return_status);
2914 
2915        IF(lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2916          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2917        ELSIF (lx_return_status = OKL_API.G_RET_STS_ERROR) THEN
2918          RAISE OKL_API.G_EXCEPTION_ERROR;
2919        END IF;
2920     END IF;
2921 
2922 -- djanaswa ER6274342 end
2923 
2924     IF ( p_amount IS NULL )
2925     THEN
2926         l_amount := NULL;
2927     ELSE
2928         l_amount := p_amount;
2929     END IF;
2930 
2931     IF ( p_periods IS NULL ) AND ( p_stub_days IS NOT NULL )
2932     THEN
2933         x_selv_tbl(1).amount                     := p_stub_amount;
2934         x_selv_tbl(1).se_line_number             := 1;                            -- TBD
2935         x_selv_tbl(1).accrued_yn                 := NULL;                         -- TBD
2936 
2937         IF p_advance_or_arrears = 'ARREARS' THEN
2938         -- djanaswa  ER6274342 start
2939             -- x_selv_tbl(1).stream_element_date        := p_start_date + p_stub_days - 1;
2940             x_selv_tbl(1).comments                   := 'Y';
2941 
2942             IF l_arrears_pay_dates_option = 'FIRST_DAY_OF_NEXT_PERIOD' THEN
2943                     x_selv_tbl(1).stream_element_date        := p_start_date + p_stub_days;
2944             ELSE
2945                     x_selv_tbl(1).stream_element_date        := p_start_date + p_stub_days - 1;
2946             END IF;
2947         -- djanaswa  ER6274342 end
2948 
2949         ELSE
2950             x_selv_tbl(1).stream_element_date        := p_start_date;
2951             x_selv_tbl(1).comments                   := 'N';
2952         END IF;
2953 
2954     ELSE
2955 
2956         l_element_count := p_periods;
2957         --print( l_prog_name, 'creating elements: ' || to_char(l_element_count) );
2958 
2959 
2960         FOR i IN 1 .. l_element_count
2961         LOOP
2962             x_selv_tbl(i).amount                     := l_amount;
2963 
2964             IF p_advance_or_arrears = 'ARREARS'
2965             THEN
2966               x_selv_tbl(i).comments := 'Y';
2967             ELSE
2968               x_selv_tbl(i).comments := 'N';
2969             END IF;
2970 
2971             -- Modified by RGOOTY: Start 4371472
2972             --Added parameter p_recurrence_date by djanaswa for bug 6007644
2973            -- added p_arrears_pay_dates_option by DJANASWA ER 6274342
2974             get_sel_date(
2975               p_start_date         => p_start_date,
2976               p_advance_or_arrears => p_advance_or_arrears,
2977               p_periods_after      => i,
2978               p_months_per_period  => l_months_factor,
2979               x_date               => x_selv_tbl(i).stream_element_date,
2980               x_return_status      => lx_return_status,
2981               p_recurrence_date    => p_recurrence_date,
2982               p_arrears_pay_dates_option => l_arrears_pay_dates_option);
2983             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
2984               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2985             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
2986               RAISE OKL_API.G_EXCEPTION_ERROR;
2987             END IF;
2988             -- Modified by RGOOTY: End
2989 
2990             x_selv_tbl(i).se_line_number             := i;                            -- TBD
2991             x_selv_tbl(i).accrued_yn                 := NULL;                         -- TBD
2992         END LOOP;
2993      END IF;
2994 
2995     OPEN top_svc_csr( p_khr_id, p_kle_id );
2996     FETCH top_svc_csr INTO top_svc_rec;
2997     If ( top_svc_csr%FOUND ) Then
2998         print( l_prog_name, ' found top svc line ' || to_char( p_kle_id ));
2999         OPEN c_pt_perc( top_svc_rec.top_svc_id);
3000     Else
3001         print( l_prog_name, ' not found top svc line ' || to_char( p_kle_id ));
3002         OPEN c_pt_perc( p_kle_id);
3003     End If;
3004     CLOSE top_svc_csr;
3005 
3006     FETCH c_pt_perc INTO rec_pt_perc;
3007     CLOSE c_pt_perc;
3008 
3009     l_pt_perc := rec_pt_perc.pass_through_percentage;
3010 
3011     print( l_prog_name, ' pass thru percent ' || to_char( l_pt_perc ) );
3012     print( l_prog_name, ' payment amount ' || to_char( p_amount ) );
3013 
3014     IF (l_pt_perc IS NOT NULL) THEN
3015 
3016         get_accrual_elements (p_start_date          => p_start_date,
3017                               p_periods             => p_periods,
3018                               p_frequency           => p_frequency,
3019                               p_structure           => p_structure,
3020                               p_advance_or_arrears  => p_advance_or_arrears,
3021                               p_amount              => nvl(p_amount,0)*(l_pt_perc/100),
3022 			      p_stub_days           => p_stub_days,
3023                               p_stub_amount         => nvl(p_stub_amount,0)*(l_pt_perc/100),
3024                               p_currency_code       => p_currency_code,
3025                               x_selv_tbl            => x_pt_tbl,
3026                               x_return_status       => lx_return_status);
3027 
3028         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
3029           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3030         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
3031           RAISE OKL_API.G_EXCEPTION_ERROR;
3032         END IF;
3033 
3034 
3035     END IF;
3036 
3037     x_return_status := G_RET_STS_SUCCESS ;
3038     print( l_prog_name, 'end' );
3039 
3040   EXCEPTION
3041 
3042     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3043 
3044       x_return_status := G_RET_STS_ERROR;
3045 
3046     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3047 
3048       x_return_status := G_RET_STS_UNEXP_ERROR;
3049 
3050     WHEN OTHERS THEN
3051 
3052       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
3053                            p_msg_name     => G_DB_ERROR,
3054                            p_token1       => G_PROG_NAME_TOKEN,
3055                            p_token1_value => l_prog_name,
3056                            p_token2       => G_SQLCODE_TOKEN,
3057                            p_token2_value => sqlcode,
3058                            p_token3       => G_SQLERRM_TOKEN,
3059                            p_token3_value => sqlerrm);
3060 
3061       x_return_status := G_RET_STS_UNEXP_ERROR;
3062 
3063   END get_stream_elements;
3064 
3065 
3066   ---------------------------------------------------------------------------
3067   -- PROCEDURE get_accrual_elements
3068   --
3069   -- Description
3070   -- Populates Stream Elements array for Accurals
3071   -- such as RENT ACCRUAL
3072   ---------------------------------------------------------------------------
3073   PROCEDURE get_accrual_elements (p_start_date          IN         DATE,
3074                                   p_periods             IN         NUMBER,
3075                                   p_frequency           IN         VARCHAR2,
3076                                   p_structure           IN         NUMBER,
3077                                   p_advance_or_arrears  IN         VARCHAR2,
3078                                   p_amount              IN         NUMBER,
3079                                   p_stub_days           IN         NUMBER,
3080                                   p_stub_amount         IN         NUMBER,
3081                                   p_currency_code       IN         VARCHAR2,
3082                                   p_day_convention_month    IN VARCHAR2 DEFAULT '30',
3083 				  p_day_convention_year    IN VARCHAR2 DEFAULT '360',
3084                                   x_selv_tbl            OUT NOCOPY okl_streams_pub.selv_tbl_type,
3085                                   x_return_status       OUT NOCOPY VARCHAR2) IS
3086 
3087     lx_return_status             VARCHAR2(1);
3088 
3089     l_fractional_month           NUMBER;
3090     l_months_factor              NUMBER;
3091     l_element_count              NUMBER;
3092     l_day_count                  NUMBER;
3093     l_se_date                    DATE;
3094     l_amount                     NUMBER;
3095 
3096     i  BINARY_INTEGER := 0;
3097     l_temp_start_date DATE;
3098     l_amount_per_day NUMBER;
3099     l_stream_amount NUMBER;
3100     l_remaining_amount NUMBER;
3101     l_remaining_days NUMBER;
3102     l_temp_end_date DATE;
3103     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_accrual_elements';
3104 
3105   BEGIN
3106 
3107 --    print( l_prog_name, 'begin' );
3108     l_fractional_month := get_fractional_month(p_start_date    => p_start_date,
3109                                                x_return_status => lx_return_status);
3110 
3111     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
3112       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3113     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
3114       RAISE OKL_API.G_EXCEPTION_ERROR;
3115     END IF;
3116 
3117     l_months_factor := get_months_factor(p_frequency     =>   p_frequency,
3118                                          x_return_status =>   lx_return_status);
3119 
3120     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
3121       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3122     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
3123       RAISE OKL_API.G_EXCEPTION_ERROR;
3124     END IF;
3125 
3126   If ( p_periods IS NULL ) AND ( p_stub_days IS NOT NULL ) THen
3127 
3128      l_se_date    :=  LAST_DAY(p_start_date);
3129 
3130      l_remaining_days := p_stub_days;
3131      l_temp_start_date := p_start_date;
3132      l_temp_end_date := l_temp_start_date + l_remaining_days - 1;
3133      l_amount_per_day := p_stub_amount/p_stub_days;
3134      IF l_temp_end_date > l_se_date THEN
3135        l_temp_end_date := l_se_date;
3136      END IF;
3137 
3138      l_remaining_amount := p_stub_amount;
3139      while l_remaining_days > 0  loop
3140        i := i + 1;
3141        IF l_temp_end_date < l_se_date THEN
3142          l_day_count := l_remaining_days;
3143        ELSE
3144          l_day_count := OKL_PRICING_UTILS_PVT.get_day_count (p_start_date => l_temp_start_date,
3145                                    p_days_in_month => 'ACTUAL', --p_day_convention_month,
3146                                    p_days_in_year => 'ACTUAL', --p_day_convention_year,
3147                                    p_end_date   => l_temp_end_date,
3148                                    p_arrears    => 'Y',
3149                                    x_return_status => lx_return_status);
3150        END IF;
3151 
3152      IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
3153        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3154      ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
3155        RAISE OKL_API.G_EXCEPTION_ERROR;
3156      END IF;
3157 
3158        l_stream_amount := l_amount_per_day * l_day_count;
3159        x_selv_tbl(i).amount                     := l_stream_amount;
3160        x_selv_tbl(i).stream_element_date        := l_se_date;
3161        x_selv_tbl(i).se_line_number             := i;
3162        l_remaining_amount := l_remaining_amount - l_stream_amount;
3163        l_temp_start_date := l_se_date + 1;
3164        l_se_date :=  add_months(l_se_date, 1);
3165        l_remaining_days := l_remaining_days - l_day_count;
3166        l_temp_end_date := l_temp_start_date + l_remaining_days - 1;
3167        IF l_temp_end_date > l_se_date THEN
3168          l_temp_end_date := l_se_date;
3169        END IF;
3170     end loop;
3171     /* If ( l_day_count < p_stub_days ) Then
3172 
3173          x_selv_tbl(1).amount                     := (p_stub_amount*l_day_count)/p_stub_days;
3174          x_selv_tbl(1).stream_element_date        := l_se_date;
3175          x_selv_tbl(1).se_line_number             := 1;
3176 
3177          l_se_date    :=  ADD_MONTHS(LAST_DAY(p_start_date) , 1);
3178          IF TO_CHAR(l_se_date, 'MON') IN ('JAN', 'MAR', 'MAY', 'JUL', 'AUG', 'OCT', 'DEC') THEN
3179              l_se_date  :=  l_se_date - 1;
3180          END IF;
3181 
3182          x_selv_tbl(2).amount                     := p_stub_amount - x_selv_tbl(1).amount;
3183          x_selv_tbl(2).stream_element_date        := l_se_date;
3184          x_selv_tbl(2).se_line_number             := 2;
3185 
3186      Else
3187 
3188          x_selv_tbl(1).amount                     := p_stub_amount;
3189          x_selv_tbl(1).stream_element_date        := l_se_date;
3190          x_selv_tbl(1).se_line_number             := 1;
3191 
3192      End If; */
3193 
3194   Else
3195 
3196     l_element_count := (p_periods * l_months_factor) + 1;
3197     FOR i IN 1 .. l_element_count LOOP
3198 
3199       l_se_date    :=  ADD_MONTHS(LAST_DAY(p_start_date) , (i - 1));
3200       IF TO_CHAR(l_se_date, 'MON') IN ('JAN', 'MAR', 'MAY', 'JUL', 'AUG', 'OCT', 'DEC') THEN
3201 
3202         l_se_date  :=  l_se_date - 1;
3203 
3204       END IF;
3205 
3206       IF i = 1 THEN
3207 
3208         l_amount := (p_amount/l_months_factor) * l_fractional_month;
3209 
3210 /*
3211         l_amount := okl_accounting_util.validate_amount(
3212                       p_amount         =>  ((p_amount/l_months_factor) * l_fractional_month),
3213                       p_currency_code  =>  p_currency_code);
3214 
3215 */
3216       ELSIF i = l_element_count THEN
3217 
3218         l_amount :=  (p_amount/l_months_factor) * (1 - l_fractional_month);
3219 /*
3220         l_amount :=  okl_accounting_util.validate_amount(
3221                       p_amount         =>  ((p_amount/l_months_factor) * (1 - l_fractional_month)),
3222                       p_currency_code  =>  p_currency_code);
3223 		      */
3224 
3225       ELSE
3226 
3227         l_amount := p_amount/l_months_factor;
3228 /*
3229         l_amount := okl_accounting_util.validate_amount(p_amount         =>  (p_amount/l_months_factor),
3230                                                         p_currency_code  =>  p_currency_code);
3231 							*/
3232 
3233       END IF;
3234 
3235       IF (i < l_element_count) OR (i = l_element_count AND l_amount <> 0) THEN
3236 
3237         x_selv_tbl(i).amount                     := l_amount;
3238         x_selv_tbl(i).stream_element_date        := l_se_date;
3239         x_selv_tbl(i).se_line_number             := i;
3240 
3241       END IF;
3242 
3243     END LOOP;
3244 
3245   End If;
3246 
3247     print( l_prog_name, 'end' );
3248     x_return_status := OKL_API.G_RET_STS_SUCCESS;
3249   EXCEPTION
3250 
3251     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3252 
3253       x_return_status := G_RET_STS_ERROR;
3254 
3255     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3256 
3257       x_return_status := G_RET_STS_UNEXP_ERROR;
3258 
3259     WHEN OTHERS THEN
3260 
3261       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
3262                            p_msg_name     => G_DB_ERROR,
3263                            p_token1       => G_PROG_NAME_TOKEN,
3264                            p_token1_value => l_prog_name,
3265                            p_token2       => G_SQLCODE_TOKEN,
3266                            p_token2_value => sqlcode,
3267                            p_token3       => G_SQLERRM_TOKEN,
3268                            p_token3_value => sqlerrm);
3269 
3270       x_return_status := G_RET_STS_UNEXP_ERROR;
3271 
3272   END get_accrual_elements;
3273 
3274 
3275   ---------------------------------------------------------------------------
3276   -- FUNCTION get_day_count
3277   ---------------------------------------------------------------------------
3278   FUNCTION get_day_count (p_start_date     IN   DATE,
3279                           p_end_date       IN   DATE,
3280                           p_arrears        IN   VARCHAR2,
3281                           x_return_status  OUT NOCOPY VARCHAR2) RETURN NUMBER IS
3282 
3283     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'get_day_count';
3284 
3285     l_days       NUMBER  :=  0;
3286 
3287     l_sd_ld      VARCHAR2(1) := 'N';
3288     l_cf_ld      VARCHAR2(1) := 'N';
3289 
3290     l_fom        DATE;
3291     i            NUMBER := 0;
3292     l_mon_Date   DATE;
3293     l_char_days varchar2(4);
3294 
3295     l_start_date DATE;
3296     l_end_date DATE;
3297     l_months NUMBER;
3298     -- Bug 4626837 : Start
3299     l_start_mon VARCHAR2(15);
3300     l_end_mon VARCHAR2(15);
3301     -- Bug 4626837 : End
3302   BEGIN
3303 
3304    l_start_date := trunc( p_start_date );
3305    l_end_date := trunc( p_end_date );
3306 
3307     --print( l_prog_name, 'begin' );
3308     x_return_status  :=  G_RET_STS_SUCCESS;
3309     l_months := 0;
3310    IF l_start_date > l_end_date THEN
3311       RETURN 0;
3312     END IF;
3313 
3314     l_fom := LAST_DAY(l_start_date) + 1;
3315     l_mon_date := ADD_MONTHS(LAST_DAY(p_end_date) + 1, -1);
3316 
3317     IF l_fom < l_mon_date THEN
3318 
3319  /*     LOOP
3320         IF l_fom = l_mon_date THEN
3321           EXIT;
3322         ELSE
3323           l_days := l_days + 30;
3324           l_fom  := ADD_MONTHS(l_fom, 1);
3325           i      := i + 1;
3326         END IF;
3327 
3328       END LOOP;*/
3329       i := 1;
3330       l_months := trunc(months_between( l_mon_date, l_fom ));
3331       l_days := l_months * 30;
3332       l_fom := add_months( l_fom, l_months );
3333 
3334 
3335     END IF;
3336 
3337    l_start_mon := TO_CHAR(l_start_date, 'MON');
3338    l_end_mon := TO_CHAR(l_end_date, 'MON');
3339 
3340    l_char_days := TO_CHAR(l_end_date, 'DD');
3341     IF l_char_days IN ('30', '31') OR
3342        (l_end_mon = 'FEB' AND l_char_days IN ('28', '29')) THEN
3343       l_cf_ld := 'Y';
3344     END IF;
3345 
3346    l_char_days := TO_CHAR(l_start_date, 'DD');
3347     IF  l_char_days IN ('30', '31') OR
3348        ( l_start_mon = 'FEB' AND l_char_days IN ('28', '29')) THEN
3349       l_sd_ld := 'Y';
3350     END IF;
3351 
3352     -- Starting date is always counted
3353     -- CF date is only counted if payment is in arrears (SRAWLING)
3354 
3355     IF i > 0 THEN
3356 
3357       IF l_sd_ld = 'Y' THEN
3358         l_days := l_days + 1;
3359       ELSE
3360         l_days := l_days + 30 - l_char_days + 1;
3361       END IF;
3362 
3363       IF l_cf_ld = 'Y' THEN
3364         l_days := l_days + 30;
3365       ELSE
3366         l_days := l_days + (l_end_date - l_fom) + 1;
3367       END IF;
3368 
3369     ELSE
3370 
3371       IF l_end_mon <> l_start_mon THEN  -- i=0 so YYYY will be the same
3372 
3373         IF l_sd_ld = 'Y' THEN
3374           l_days := l_days + 1;
3375         ELSE
3376           l_days := l_days + 30 - l_char_days + 1;
3377         END IF;
3378 
3379         IF l_cf_ld = 'Y' THEN
3380           l_days := l_days + 30;
3381         ELSE
3382           l_days := l_days + (l_end_date - l_fom) + 1;
3383         END IF;
3384 
3385       ELSE
3386 
3387         IF l_sd_ld = 'Y' AND l_cf_ld = 'Y' THEN
3388           l_days := 1;
3389         ELSIF l_cf_ld = 'Y' THEN
3390           l_days := 30 - l_char_days + 1;
3391         ELSE
3392           l_days := (l_end_date - l_start_date) + 1;
3393         END IF;
3394 
3395       END IF;
3396 
3397     END IF;
3398 
3399     IF p_arrears <> 'Y' THEN
3400       l_days := l_days - 1;
3401     END IF;
3402 
3403   --print( l_prog_name, 'end' );
3404     RETURN l_days;
3405 
3406   EXCEPTION
3407 
3408     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3409 
3410       x_return_status := G_RET_STS_ERROR;
3411 
3412     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3413 
3414       x_return_status := G_RET_STS_UNEXP_ERROR;
3415 
3416     WHEN OTHERS THEN
3417 
3418       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
3419                            p_msg_name     => G_DB_ERROR,
3420                            p_token1       => G_PROG_NAME_TOKEN,
3421                            p_token1_value => l_prog_name,
3422                            p_token2       => G_SQLCODE_TOKEN,
3423                            p_token2_value => sqlcode,
3424                            p_token3       => G_SQLERRM_TOKEN,
3425                            p_token3_value => sqlerrm);
3426 
3427       x_return_status := G_RET_STS_UNEXP_ERROR;
3428 
3429   END get_day_count;
3430 
3431 
3432 
3433   ---------------------------------------------------------------------------
3434   -- PROCEDURE consolidate_header_streams
3435   ---------------------------------------------------------------------------
3436   PROCEDURE consolidate_header_streams(p_khr_id         IN NUMBER,
3437                                      p_purpose_code   IN VARCHAR2,
3438                                      x_return_status  OUT NOCOPY VARCHAR2) IS
3439 
3440     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'consolidate_header_streams';
3441 
3442     CURSOR c_stm_count IS
3443       SELECT COUNT(stm.id),
3444              stm.sty_id
3445       FROM   okl_streams stm
3446       WHERE  stm.khr_id = p_khr_id
3447         AND  stm.say_code = 'WORK'
3448         AND  NVL(stm.purpose_code, '-99') = p_purpose_code
3449         AND  stm.kle_id IS NULL
3450       HAVING COUNT(stm.id) > 1
3451       GROUP BY stm.kle_id, stm.sty_id;
3452 
3453     CURSOR c_stm_first (p_sty_id NUMBER) IS
3454       SELECT id
3455       FROM   okl_streams
3456       WHERE  khr_id = p_khr_id
3457         AND  kle_id IS NULL
3458         AND  say_code = 'WORK'
3459         AND  sty_id = p_sty_id
3460         AND  NVL(purpose_code, '-99') = p_purpose_code;
3461 
3462     CURSOR c_stm (p_sty_id NUMBER, p_stm_id NUMBER) IS
3463       SELECT id
3464       FROM   okl_streams
3465       WHERE  khr_id = p_khr_id
3466         AND  kle_id IS NULL
3467         AND  sty_id = p_sty_id
3468         AND  say_code = 'WORK'
3469         AND  NVL(purpose_code, '-99') = p_purpose_code
3470         AND  id <> p_stm_id;
3471 
3472     CURSOR c_last_sel (p_stm_id NUMBER) IS
3473       SELECT se_line_number
3474       FROM   okl_strm_elements
3475       WHERE  stm_id = p_stm_id
3476       ORDER BY se_line_number DESC;
3477 
3478     CURSOR c_sel (p_stm_id NUMBER) IS
3479       SELECT id
3480       FROM   okl_strm_elements
3481       WHERE  stm_id = p_stm_id
3482       ORDER BY stream_element_date;
3483 
3484     l_stm_first         NUMBER;
3485     l_line_num          NUMBER;
3486     n                   NUMBER := 0;
3487 
3488   BEGIN
3489 
3490     print( l_prog_name, 'begin' );
3491     FOR l_stm_count IN c_stm_count LOOP
3492 
3493       OPEN c_stm_first (p_sty_id => l_stm_count.sty_id);
3494       FETCH c_stm_first INTO l_stm_first;
3495       CLOSE c_stm_first;
3496 
3497       OPEN c_last_sel (p_stm_id => l_stm_first);
3498       FETCH c_last_sel INTO l_line_num;
3499       CLOSE c_last_sel;
3500 
3501       FOR l_stm IN c_stm (l_stm_count.sty_id, l_stm_first) LOOP
3502 
3503         DELETE FROM okl_streams WHERE id = l_stm.id
3504           and khr_id = p_khr_id;
3505 
3506         FOR l_sel IN c_sel (l_stm.id) LOOP
3507 
3508           n := n + 1;
3509 
3510           UPDATE okl_strm_elements
3511           SET  stm_id = l_stm_first,
3512 	       se_line_number = (l_line_num + n)
3513 
3514 	WHERE id = l_sel.id;
3515 
3516         END LOOP;
3517 
3518 
3519       END LOOP;
3520 
3521     END LOOP;
3522 
3523 
3524     DELETE FROM OKL_STREAMS WHERE purpose_code = 'STUBS'
3525      and khr_id = p_khr_id;
3526     x_return_status := G_RET_STS_SUCCESS;
3527     print( l_prog_name, 'end' );
3528 
3529 
3530   EXCEPTION
3531 
3532     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3533 
3534       x_return_status := G_RET_STS_ERROR;
3535 
3536     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3537 
3538       x_return_status := G_RET_STS_UNEXP_ERROR;
3539 
3540     WHEN OTHERS THEN
3541 
3542       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
3543                            p_msg_name     => G_DB_ERROR,
3544                            p_token1       => G_PROG_NAME_TOKEN,
3545                            p_token1_value => l_prog_name,
3546                            p_token2       => G_SQLCODE_TOKEN,
3547                            p_token2_value => sqlcode,
3548                            p_token3       => G_SQLERRM_TOKEN,
3549                            p_token3_value => sqlerrm);
3550 
3551       x_return_status := G_RET_STS_UNEXP_ERROR;
3552 
3553   END consolidate_header_streams;
3554 
3555   ---------------------------------------------------------------------------
3556   -- PROCEDURE consolidate_line_streams
3557   ---------------------------------------------------------------------------
3558   PROCEDURE consolidate_line_streams(p_khr_id         IN NUMBER,
3559                                      p_purpose_code   IN VARCHAR2,
3560                                      x_return_status  OUT NOCOPY VARCHAR2) IS
3561 
3562     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'consolidate_line_streams';
3563 
3564     CURSOR c_stm_count IS
3565       SELECT COUNT(stm.id),
3566              stm.kle_id,
3567              stm.sty_id
3568       FROM   okl_streams stm,
3569              okc_k_lines_b cle,
3570              okc_line_styles_b lse
3571       WHERE  stm.khr_id = p_khr_id
3572         AND  stm.say_code = 'WORK'
3573         AND  NVL(stm.purpose_code, '-99') = p_purpose_code
3574         AND  stm.kle_id IS NOT NULL
3575         AND  stm.kle_id = cle.id
3576         AND  cle.lse_id = lse.id
3577         AND  lse.lty_code IN ('FEE', 'SOLD_SERVICE', 'LINK_SERV_ASSET', 'FREE_FORM1', 'LINK_FEE_ASSET')
3578       HAVING COUNT(stm.id) > 1
3579       GROUP BY stm.kle_id, stm.sty_id;
3580 
3581     CURSOR c_stm_first (p_kle_id NUMBER, p_sty_id NUMBER) IS
3582       SELECT id
3583       FROM   okl_streams
3584       WHERE  khr_id = p_khr_id
3585         AND  kle_id = p_kle_id
3586         AND  sty_id = p_sty_id
3587         AND  say_code = 'WORK'
3588         AND  NVL(purpose_code, '-99') = p_purpose_code;
3589 
3590     CURSOR c_stm (p_kle_id NUMBER, p_sty_id NUMBER, p_stm_id NUMBER) IS
3591       SELECT id
3592       FROM   okl_streams
3593       WHERE  khr_id = p_khr_id
3594         AND  kle_id = p_kle_id
3595         AND  sty_id = p_sty_id
3596         AND  say_code = 'WORK'
3597         AND  NVL(purpose_code, '-99') = p_purpose_code
3598         AND  id <> p_stm_id;
3599 
3600     CURSOR c_last_sel (p_stm_id NUMBER) IS
3601       SELECT se_line_number
3602       FROM   okl_strm_elements
3603       WHERE  stm_id = p_stm_id
3604       ORDER BY se_line_number DESC;
3605 
3606     CURSOR c_sel (p_stm_id NUMBER) IS
3607       SELECT id
3608       FROM   okl_strm_elements
3609       WHERE  stm_id = p_stm_id
3610       ORDER BY stream_element_date;
3611 
3612     l_stm_first         NUMBER;
3613     l_line_num          NUMBER;
3614     n                   NUMBER := 0;
3615 
3616   BEGIN
3617 
3618     print( l_prog_name, 'begin' );
3619 
3620     FOR l_stm_count IN c_stm_count LOOP
3621 
3622       OPEN c_stm_first (p_kle_id => l_stm_count.kle_id, p_sty_id => l_stm_count.sty_id);
3623       FETCH c_stm_first INTO l_stm_first;
3624       CLOSE c_stm_first;
3625 
3626       OPEN c_last_sel (p_stm_id => l_stm_first);
3627       FETCH c_last_sel INTO l_line_num;
3628       CLOSE c_last_sel;
3629 
3630       FOR l_stm IN c_stm (l_stm_count.kle_id, l_stm_count.sty_id, l_stm_first) LOOP
3631 
3632         DELETE FROM okl_streams WHERE id = l_stm.id
3633          and khr_id = p_khr_id;
3634 
3635         FOR l_sel IN c_sel (l_stm.id) LOOP
3636 
3637           n := n + 1;
3638 
3639           UPDATE okl_strm_elements
3640              SET stm_id = l_stm_first,
3641                  se_line_number = (l_line_num + n)
3642            WHERE id = l_sel.id;
3643 
3644         END LOOP;
3645 
3646       END LOOP;
3647 
3648     END LOOP;
3649 
3650 
3651     DELETE FROM OKL_STREAMS WHERE purpose_code = 'STUBS'
3652      and khr_id = p_khr_id;
3653 
3654     x_return_status := G_RET_STS_SUCCESS;
3655     print( l_prog_name, 'end' );
3656 
3657 
3658   EXCEPTION
3659 
3660     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3661 
3662       x_return_status := G_RET_STS_ERROR;
3663 
3664     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3665 
3666       x_return_status := G_RET_STS_UNEXP_ERROR;
3667 
3668     WHEN OTHERS THEN
3669 
3670       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
3671                            p_msg_name     => G_DB_ERROR,
3672                            p_token1       => G_PROG_NAME_TOKEN,
3673                            p_token1_value => l_prog_name,
3674                            p_token2       => G_SQLCODE_TOKEN,
3675                            p_token2_value => sqlcode,
3676                            p_token3       => G_SQLERRM_TOKEN,
3677                            p_token3_value => sqlerrm);
3678 
3679       x_return_status := G_RET_STS_UNEXP_ERROR;
3680 
3681   END consolidate_line_streams;
3682 
3683   ---------------------------------------------------------------------------
3684   -- PROCEDURE consolidate_acc_streams
3685   ---------------------------------------------------------------------------
3686   PROCEDURE consolidate_acc_streams(p_khr_id         IN NUMBER,
3687                                      p_purpose_code   IN VARCHAR2,
3688                                      x_return_status  OUT NOCOPY VARCHAR2) IS
3689 
3690     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'consolidate_acc_streams';
3691 
3692    --Modified by kthiruva on 30-May-2005
3693    --Bug 4374085 - Start of Changes
3694    -- The purpose 'SUBSIDY_INCOME' was added to the cursor
3695    --For Bug 4434343 - Purposes 'PASS_THRU_SVC_REV_ACCRUAL' and 'PASS_THRU_SVC_EXP_ACCRUAL'
3696    --added by mansrini on 10-Jun-2005 to the cursor
3697 
3698    --Bug 5503678 - order by clause modified by dpsingh on 18-Sep-2006
3699    -- Order on sty_id and not stream_Type_purpose
3700 
3701     CURSOR c_acc_stm IS
3702       SELECT ele.stream_element_date,
3703              ele.id element_id,
3704              stm.id stream_id,
3705              stm.kle_id kle_id,
3706              stm.sty_id,
3707              sty.stream_type_purpose stm_purpose,
3708              ele.amount amount
3709       FROM   okl_streams stm,
3710              okl_strm_elements ele,
3711 	     okl_strm_type_b sty
3712       WHERE  stm.khr_id = p_khr_id
3713         AND  ele.stm_id = stm.id
3714         AND  stm.say_code = 'WORK'
3715         AND  NVL(stm.purpose_code, '-99') = p_purpose_code
3716 	AND  sty.id = stm.sty_id
3717 	AND  sty.stream_type_purpose in ( 'RENT_ACCRUAL','PASS_THRU_PRO_FEE_ACCRUAL',
3718 	                   'PASS_THRU_REV_ACCRUAL', 'SERVICE_INCOME',
3719 	                   'AMORTIZE_FEE_INCOME', 'ACCRUED_FEE_INCOME', 'LEASE_INCOME','INTEREST_INCOME',
3720 			   'PASS_THRU_EXP_ACCRUAL', 'ACCOUNTING', 'AMORTIZED_FEE_EXPENSE','SUBSIDY_INCOME',
3721                            'PASS_THRU_SVC_EXP_ACCRUAL','PASS_THRU_SVC_REV_ACCRUAL')
3722 --Added by dpsingh for consolidation to happen only for ISG streams 5949810
3723         AND (stm.sgn_code <> 'STMP' and stm.sgn_code <> 'STMP-REBK')
3724  --dpsingh 5949810 ends
3725 
3726     ORDER BY stm.sty_id,stm.kle_id,ele.stream_element_date;
3727     -- Bug 4374085 - End of Changes
3728 
3729     r_acc_stm c_acc_stm%ROWTYPE;
3730 
3731 
3732     l_stm_first         NUMBER;
3733     l_line_num          NUMBER;
3734     n                   NUMBER := 0;
3735 
3736     -- Added by RGOOTY
3737     eleid_tbl Okl_Streams_Util.NumberTabTyp;
3738     streamid_tbl Okl_Streams_Util.NumberTabTyp;
3739     sel_date Okl_Streams_Util.DateTabTyp;
3740 
3741     lom_date DATE;
3742     i   NUMBER;
3743     -- Added by RGOOTY: 4403311
3744     m   NUMBER;
3745     d   NUMBER;
3746     j   NUMBER;
3747     r   NUMBER;
3748 
3749 
3750     l_ele_id_all_tbl        Okl_Streams_Util.NumberTabTyp;
3751     l_streamid_all_tbl      Okl_Streams_Util.NumberTabTyp;
3752     l_sel_date_all_tbl      Okl_Streams_Util.DateTabTyp;
3753     l_kle_id_all_tbl        Okl_Streams_Util.NumberTabTyp;
3754     l_amt_all_tbl           Okl_Streams_Util.NumberTabTyp;
3755     l_stm_purpose_all_tbl   Okl_Streams_Util.Var150TabTyp;
3756 
3757     --Added by dpsingh for bug 5503678
3758     l_sty_id_all_tbl        Okl_Streams_Util.NumberTabTyp;
3759 
3760     l_ele_id_mod_tbl        Okl_Streams_Util.NumberTabTyp;
3761     l_streamid_mod_tbl      Okl_Streams_Util.NumberTabTyp;
3762     l_sel_date_mod_tbl      Okl_Streams_Util.DateTabTyp;
3763     l_kle_id_mod_tbl        Okl_Streams_Util.NumberTabTyp;
3764     l_amt_mod_tbl           Okl_Streams_Util.NumberTabTyp;
3765     l_stm_purpose_mod_tbl   Okl_Streams_Util.Var150TabTyp;
3766 
3767     l_ele_id_del_tbl        Okl_Streams_Util.NumberTabTyp;
3768     l_streamid_del_tbl      Okl_Streams_Util.NumberTabTyp;
3769     l_sel_date_del_tbl      Okl_Streams_Util.DateTabTyp;
3770     l_kle_id_del_tbl        Okl_Streams_Util.NumberTabTyp;
3771     l_amt_del_tbl           Okl_Streams_Util.NumberTabTyp;
3772     l_stm_purpose_del_tbl   Okl_Streams_Util.Var150TabTyp;
3773 
3774     l_sum_amount   NUMBER;
3775   BEGIN
3776 
3777 --    print( l_prog_name, 'begin' );
3778 
3779     i := 1;
3780     r := 1;
3781     FOR l_acc_stm IN c_acc_stm
3782     LOOP
3783         lom_date := LAST_DAY(l_acc_stm.stream_element_date);
3784         l_sel_date_all_tbl(r)    := trunc(lom_date);
3785         l_streamid_all_tbl(r)    := l_acc_stm.stream_id;
3786         l_ele_id_all_tbl(r)      := l_acc_stm.element_id;
3787         l_kle_id_all_tbl(r)      := l_acc_stm.kle_id;
3788         l_amt_all_tbl(r)         := l_acc_stm.amount;
3789         l_stm_purpose_all_tbl(r) := l_acc_stm.stm_purpose;
3790 	--Added by dpsingh for bug 5503678
3791            l_sty_id_all_tbl(r)      := l_acc_stm.sty_id;
3792 
3793         r := r + 1;
3794     END LOOP;
3795 
3796 --print( l_prog_name, 'Number of accrual Stream Elements ' || l_amt_all_tbl.COUNT );
3797     -- Consolidation of Rental Accruals ..
3798     IF l_ele_id_all_tbl.count > 0
3799     THEN
3800       i := 1; -- Index to loop through all the Accrual Streams
3801       m := 1; -- Index for PL/SQL table for storing the modifed and updatable records only.
3802       d := 1; -- Index for PL/SQL table for storing the streams which have to be deleted.
3803       WHILE i <= l_ele_id_all_tbl.LAST
3804       LOOP
3805         l_sum_amount := l_amt_all_tbl(i);
3806 
3807         j := i + 1;
3808         WHILE j >= 2 AND
3809               j <= l_ele_id_all_tbl.COUNT AND
3810               l_kle_id_all_tbl(j)    = l_kle_id_all_tbl(i) AND
3811               l_sel_date_all_tbl(j)  = l_sel_date_all_tbl(i) AND
3812               --Modified by dpsingh for bug 5503678.
3813                  --Check if sty_id remains the same not purpose
3814                  l_sty_id_all_tbl(j) = l_sty_id_all_tbl(i)
3815         LOOP
3816           l_sum_amount := l_sum_amount + l_amt_all_tbl(j);
3817 
3818           -- Put the record in the to be deleted table
3819           l_ele_id_del_tbl(d)      := l_ele_id_all_tbl(j);
3820           l_streamid_del_tbl(d)    := l_streamid_all_tbl(j);
3821           l_sel_date_del_tbl(d)    := l_sel_date_all_tbl(j);
3822           l_kle_id_del_tbl(d)      := l_kle_id_all_tbl(j);
3823           l_amt_del_tbl(d)         := l_amt_all_tbl(j);
3824           l_stm_purpose_del_tbl(d) := l_stm_purpose_all_tbl(d);
3825           d := d + 1;
3826 
3827           -- Increment j, k
3828           j := j + 1;
3829         END LOOP; -- Loop on j
3830         -- Put the record in to be Updated Table
3831         l_ele_id_mod_tbl(m)      := l_ele_id_all_tbl(i);
3832         l_streamid_mod_tbl(m)    := l_streamid_all_tbl(i);
3833         l_sel_date_mod_tbl(m)    := l_sel_date_all_tbl(i);
3834         l_kle_id_mod_tbl(m)      := l_kle_id_all_tbl(i);
3835         l_amt_mod_tbl(m)         := l_sum_amount;
3836         l_stm_purpose_mod_tbl(d) := l_stm_purpose_all_tbl(d);
3837         m := m + 1;
3838         -- proceed to unprocessed record !!
3839         i := j;
3840       END LOOP; -- Loop on i
3841     END IF; -- IF l_ele_id_all_tbl.count > 0
3842 
3843 --print( l_prog_name, 'Updateable Stream Elements ' || l_ele_id_mod_tbl.COUNT );
3844 --print( l_prog_name, 'To be Deleted Stream Elements ' || l_ele_id_del_tbl.COUNT );
3845 
3846     -- Delete the Redundant Rental Accrual Stream Elements
3847     IF l_ele_id_del_tbl.COUNT > 0
3848     THEN
3849 --print( l_prog_name, 'Deleting  ' || l_ele_id_del_tbl.COUNT   || ' Stream Elements ' );
3850       BEGIN
3851         FORALL indx in l_ele_id_del_tbl.FIRST .. l_ele_id_del_tbl.LAST
3852           DELETE okl_strm_elements
3853              WHERE id = l_ele_id_del_tbl(indx) AND
3854                    stm_id = l_streamid_del_tbl(indx);
3855       EXCEPTION
3856       	WHEN OTHERS THEN
3857 	      okl_api.set_message (
3858 	        p_app_name     => G_APP_NAME,
3859 	        p_msg_name     => G_DB_ERROR,
3860 	        p_token1       => G_PROG_NAME_TOKEN,
3861 	        p_token1_value => 'consolidate_acc_streams',
3862 	        p_token2       => G_SQLCODE_TOKEN,
3863 	        p_token2_value => sqlcode,
3864 	        p_token3       => G_SQLERRM_TOKEN,
3865 	        p_token3_value => sqlerrm);
3866 	        x_return_status := Okl_Api.G_RET_STS_UNEXP_ERROR;
3867 	      RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3868 	    END;
3869     END IF; -- IF l_ele_id_del_tbl.COUNT > 0
3870 
3871 --print( l_prog_name, 'Deleted Redundant Stream Elements ' );
3872 
3873     -- Update the Rental Accrual Stream Elements
3874     IF l_ele_id_mod_tbl.COUNT > 0
3875     THEN
3876 --print( l_prog_name, 'Modifiying  ' || l_ele_id_mod_tbl.COUNT   || ' Stream Elements ' );
3877       BEGIN
3878         FORALL indx in l_ele_id_mod_tbl.FIRST .. l_ele_id_mod_tbl.LAST
3879           UPDATE okl_strm_elements
3880              SET stream_element_date = l_sel_date_mod_tbl(indx),
3881                  amount = l_amt_mod_tbl(indx)
3882              WHERE id = l_ele_id_mod_tbl(indx) AND
3883                    stm_id = l_streamid_mod_tbl(indx);
3884       EXCEPTION
3885       	WHEN OTHERS THEN
3886 	      okl_api.set_message (
3887 	        p_app_name     => G_APP_NAME,
3888 	        p_msg_name     => G_DB_ERROR,
3889 	        p_token1       => G_PROG_NAME_TOKEN,
3890 	        p_token1_value => 'consolidate_acc_streams',
3891 	        p_token2       => G_SQLCODE_TOKEN,
3892 	        p_token2_value => sqlcode,
3893 	        p_token3       => G_SQLERRM_TOKEN,
3894 	        p_token3_value => sqlerrm);
3895 	        x_return_status := Okl_Api.G_RET_STS_UNEXP_ERROR;
3896 	      RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3897 	    END;
3898     END IF; -- IF l_ele_id_mod_tbl.COUNT > 0
3899 
3900     x_return_status := G_RET_STS_SUCCESS;
3901 --    print( l_prog_name, 'end' );
3902 
3903   EXCEPTION
3904 
3905     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3906 
3907       x_return_status := G_RET_STS_ERROR;
3908 
3909     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3910 
3911       x_return_status := G_RET_STS_UNEXP_ERROR;
3912 
3913     WHEN OTHERS THEN
3914 
3915       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
3916                            p_msg_name     => G_DB_ERROR,
3917                            p_token1       => G_PROG_NAME_TOKEN,
3918                            p_token1_value => l_prog_name,
3919                            p_token2       => G_SQLCODE_TOKEN,
3920                            p_token2_value => sqlcode,
3921                            p_token3       => G_SQLERRM_TOKEN,
3922                            p_token3_value => sqlerrm);
3923 
3924       x_return_status := G_RET_STS_UNEXP_ERROR;
3925 
3926   END consolidate_acc_streams;
3927 
3928   ---------------------------------------------------------------------------
3929   -- PROCEDURE gen_non_cash_flows
3930   ---------------------------------------------------------------------------
3931   ---------------------------------------------------------------------------
3932   PROCEDURE gen_non_cash_flows
3933                             ( p_api_version                IN         NUMBER,
3934                               p_init_msg_list              IN         VARCHAR2,
3935                               p_khr_id                     IN         NUMBER,
3936 			      p_pre_tax_irr                IN         NUMBER,
3937                               p_generation_type            IN         VARCHAR2,
3938                               p_reporting_book_class       IN         VARCHAR2,
3939 			      p_subsidies_yn               IN         VARCHAR2,
3940                               x_return_status              OUT NOCOPY VARCHAR2,
3941                               x_msg_count                  OUT NOCOPY NUMBER,
3942                               x_msg_data                   OUT NOCOPY VARCHAR2) IS
3943 
3944     lx_return_status              VARCHAR2(1);
3945 
3946     lx_isAllowed                  BOOLEAN;
3947     lx_passStatus                 VARCHAR2(30);
3948     lx_failStatus                 VARCHAR2(30);
3949 
3950 
3951     CURSOR c_hdr IS
3952       SELECT chr.template_yn,
3953              chr.currency_code,
3954              chr.start_date,
3955              chr.end_date,
3956              khr.deal_type,
3957              khr.term_duration,
3958              khr.pre_tax_irr,
3959              NVL(khr.generate_accrual_yn, 'Y')
3960       FROM   okc_k_headers_b chr,
3961              okl_k_headers khr
3962       WHERE  khr.id = p_khr_id
3963         AND  chr.id = khr.id;
3964 
3965     cursor l_line_csr( chrid NUMBER, lnetype VARCHAR2 ) is
3966     select kle.id,
3967            kle.oec,
3968            kle.residual_code,
3969            kle.capital_amount,
3970            kle.delivered_date,
3971            kle.date_funding_required,
3972            kle.residual_grnty_amount,
3973            kle.date_funding,
3974            kle.residual_value,
3975            kle.amount,
3976            kle.price_negotiated,
3977            kle.start_date,
3978            kle.end_date,
3979            kle.orig_system_id1,
3980            kle.fee_type,
3981            kle.initial_direct_cost,
3982            kle.capital_reduction,
3983            kle.capital_reduction_percent,
3984            NVL(kle.capitalize_down_payment_yn, 'N') capitalize_down_payment_yn
3985         /*   ,tl.item_description,
3986            tl.name  */
3987            --Bug# 9725362
3988            ,sts.ste_code
3989      from  okl_k_lines_full_v kle,
3990            okc_line_styles_b lse,
3991 --           okc_k_lines_tl tl,
3992 	   okc_statuses_b sts
3993      where KLE.LSE_ID = LSE.ID
3994           and lse.lty_code = lnetype
3995 --	  and tl.id = kle.id
3996 --          and tl.language = userenv('LANG')
3997           and kle.dnz_chr_id = chrid
3998 	  and sts.code = kle.sts_code
3999 	  and sts.ste_code not in ('HOLD', 'EXPIRED', 'CANCELLED');
4000 
4001      l_line_rec l_line_csr%ROWTYPE;
4002 
4003     l_hdr                    c_hdr%ROWTYPE;
4004     l_deal_type              VARCHAR2(30);
4005     l_purpose_code           VARCHAR2(30);
4006 
4007     l_sty_id                 NUMBER;
4008     l_sty_name               VARCHAR2(150);
4009     l_mapped_sty_name        VARCHAR2(150);
4010 
4011     l_selv_tbl               okl_streams_pub.selv_tbl_type;
4012     l_tmp_selv_tbl               okl_streams_pub.selv_tbl_type;
4013     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
4014     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
4015 
4016     l_stmv_rec               okl_streams_pub.stmv_rec_type;
4017     l_pt_rec                 okl_streams_pub.stmv_rec_type;
4018     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
4019 
4020     l_capital_cost           NUMBER;
4021 
4022     i                        BINARY_INTEGER := 0;
4023     j                        BINARY_INTEGER := 0;
4024 
4025 
4026     Cursor c_rollover_pmnts( chrId NUMBER, kleId NUMBER ) IS
4027     Select nvl(slh.object1_id1, -1) styId
4028     From   OKC_RULE_GROUPS_B rgp,
4029            OKC_RULES_B sll,
4030            okc_rules_b slh,
4031 	   okl_strm_type_b sty
4032     Where  slh.rgp_id = rgp.id
4033        and rgp.RGD_CODE = 'LALEVL'
4034        and sll.RULE_INFORMATION_CATEGORY = 'LASLL'
4035        and slh.RULE_INFORMATION_CATEGORY = 'LASLH'
4036        AND TO_CHAR(slh.id) = sll.object2_id1
4037        and rgp.dnz_chr_id = chrId
4038        and rgp.cle_id = kleId
4039        and sty.id = to_number(slh.object1_id1)
4040        and sty.stream_type_purpose NOT IN ('ESTIMATED_PROPERTY_TAX', 'UNSCHEDULED_PRINCIPAL_PAYMENT', 'DOWN_PAYMENT');
4041        --Added DOWN_PAYMENT by rgooty for bug 7536131
4042        --bug# 4092324 bug# 4122385
4043 
4044     r_rollover_pmnts c_rollover_pmnts%ROWTYPE;
4045 
4046     CURSOR c_inflows( khrId NUMBER, kleId NUMBER) IS
4047     SELECT rul2.object1_id1 frequency,
4048            DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears
4049     FROM   okc_rule_groups_b rgp,
4050            okc_rules_b rul1,
4051            okc_rules_b rul2,
4052            okc_k_lines_b cle,
4053            okl_k_lines kle,
4054            okc_line_styles_b lse,
4055 	   okl_strm_type_b sty
4056     WHERE  rul1.rgp_id= rgp.id
4057       ANd  rgp.rgd_code = 'LALEVL'
4058       AND  rul2.rule_information_category = 'LASLL'
4059       and  rul1.RULE_INFORMATION_CATEGORY = 'LASLH'
4060       AND  TO_NUMBER(rul2.object2_id1) = rul1.id
4061       AND  rul2.rgp_id = rgp.id
4062       AND  rgp.dnz_chr_id = khrid
4063       AND  rgp.cle_id = kleid
4064       AND  cle.id = rgp.cle_id
4065       AND  cle.sts_code IN ('PASSED', 'COMPLETE')
4066       AND  cle.id = kle.id
4067       AND  cle.lse_id = lse.id
4068       and  sty.id = to_number(rul1.object1_id1)
4069       and sty.stream_type_purpose NOT IN ('ESTIMATED_PROPERTY_TAX', 'UNSCHEDULED_PRINCIPAL_PAYMENT' );
4070        --bug# 4092324 bug# 4122385
4071 
4072     r_inflows c_inflows%ROWTYPE;
4073 
4074     l_asbv_tbl  OKL_SUBSIDY_PROCESS_PVT.asbv_tbl_type;
4075     l_first_sel_date DATE;
4076     l_months_factor  NUMBER;
4077     l_end_date       DATE;
4078 
4079     l_primary_sty_id NUMBER;
4080 
4081     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'gen_non_cash_flows';
4082 
4083     Cursor c_subsidy_strm ( styId NUMBER ) IS
4084     Select to_number(Value)
4085     from okl_sgn_translations
4086     where jtot_object1_code = 'OKL_STRMTYP'
4087       and object1_id1 = to_char( styId );
4088 
4089     Cursor c_strm_name ( styId NUMBER ) IS
4090     Select name
4091     from okl_strm_type_tl
4092     where language = 'US'
4093      and id = styId;
4094 
4095     l_subsidy_stream VARCHAR2(256);
4096     l_accrual_sty_id NUMBER;
4097 
4098     CURSOR c_inflows_line ( khrId NUMBER, kleId NUMBER, pCode VARCHAR2 ) IS
4099       SELECT DISTINCT
4100              sel.id id,
4101              sel.amount cf_amount,
4102              sel.stream_element_date cf_date,
4103              sel.comments cf_arrear,
4104              sty.stream_type_purpose cf_purpose,
4105              sll.object1_id1 cf_frequency,
4106              DECODE(sll.object1_id1, 'M', 30, 'Q', 90, 'S', 180, 'A', 360) days_per_period,
4107              DECODE(sll.object1_id1, 'M', 12, 'Q', 4, 'S', 2, 'A', 1) periods_per_year
4108       FROM   okl_streams stm,
4109              okl_strm_type_b sty,
4110              okl_strm_elements sel,
4111              okc_k_lines_b cle,
4112              okc_line_styles_b lse,
4113              okc_rules_b sll,
4114              okc_rules_b slh,
4115              okc_rule_groups_b rgp
4116       WHERE  stm.khr_id = khrId
4117         AND  stm.kle_id = kleId
4118         AND  stm.say_code = 'WORK'
4119         AND  nvl(stm.purpose_code, '-99') = pCode
4120         AND  stm.sty_id = sty.id
4121         AND  stm.id = sel.stm_id
4122 	AND  sty.stream_type_purpose IN ( 'RENT', 'LOAN_PAYMENT')
4123         AND  sel.comments IS NOT NULL
4124         AND  stm.kle_id = cle.id
4125         AND  NOT EXISTS (SELECT 1
4126                          FROM   okc_rule_groups_b rgp2
4127                          WHERE  rgp2.dnz_chr_id = khrId
4128                            AND  rgp2.cle_id = cle.id
4129                            AND  rgp2.rgd_code = 'LAPSTH')
4130         AND  cle.sts_code IN ('PASSED', 'COMPLETE', 'TERMINATED')
4131         AND  cle.lse_id = lse.id
4132         AND  lse.lty_code = 'FREE_FORM1'
4133         AND  cle.id = rgp.cle_id
4134         AND  rgp.rgd_code = 'LALEVL'
4135         AND  rgp.id = slh.rgp_id
4136         AND  slh.rule_information_category = 'LASLH'
4137         AND  slh.object1_id1 = TO_CHAR(stm.sty_id)
4138         AND  TO_CHAR(slh.id) = sll.object2_id1
4139         AND  sll.rule_information_category = 'LASLL'
4140         AND  cle.id = kleid;
4141 
4142     r_inflows_line c_inflows_line%ROWTYPE;
4143     TYPE r_inflow_line_tbl_type is table of c_inflows_line%ROWTYPE
4144       INDEX BY BINARY_INTEGER;
4145     r_inflow_line_tbl r_inflow_line_tbl_type;
4146     l_pv_date DATE;
4147     l_pv_amount NUMBER;
4148     x_pv_amount NUMBER;
4149 
4150     P_FIRST VARCHAR2(1);
4151     L NUMBER := 0;
4152 
4153     -- Added by RGOOTY : Start
4154     l_rv_sty_id              NUMBER;
4155     l_rv_sty_name            VARCHAR2(150);
4156 
4157     l_capred_sty_id              NUMBER;
4158     l_capred_sty_name            VARCHAR2(150);
4159 
4160     -- Added by RGOOTY : End
4161     -- Added by RGOOTY for perf.: Bug Number 4346646 Start
4162     l_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
4163     l_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
4164 
4165     lx_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
4166     lx_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
4167     -- Added by RGOOTY for perf.: End
4168 
4169 -- djanaswa ER6274342 start
4170     l_arrears_pay_dates_option  okl_st_gen_tmpt_sets_all.isg_arrears_pay_dates_option%type;
4171 -- djanaswa ER6274342 end
4172 
4173   BEGIN
4174 
4175     -- print( l_prog_name, 'begin' );
4176     OPEN  c_hdr;
4177     FETCH c_hdr INTO l_hdr;
4178     CLOSE c_hdr;
4179 
4180     IF p_reporting_book_class IS NOT NULL THEN
4181       l_deal_type    :=  p_reporting_book_class;
4182       l_purpose_code := 'REPORT';
4183     ELSE
4184       l_deal_type    :=  l_hdr.deal_type;
4185       l_purpose_code := '-99';
4186     END IF;
4187 
4188     OKL_ISG_UTILS_PVT.get_primary_stream_type(
4189             p_khr_id              => p_khr_id,
4190 	    p_deal_type           => l_deal_type,
4191             p_primary_sty_purpose => 'RESIDUAL_VALUE',
4192             x_return_status       => lx_return_status,
4193             x_primary_sty_id      => l_rv_sty_id,
4194             x_primary_sty_name    => l_rv_sty_name);
4195 
4196 -- djanaswa ER6274342 start
4197 
4198    IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4199       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4200     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4201       RAISE OKL_API.G_EXCEPTION_ERROR;
4202     END IF;
4203 
4204    OKL_ISG_UTILS_PVT.get_arrears_pay_dates_option(
4205         p_khr_id                   => p_khr_id,
4206         x_arrears_pay_dates_option => l_arrears_pay_dates_option,
4207         x_return_status            => lx_return_status);
4208 
4209        IF(lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4210          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4211        ELSIF (lx_return_status = OKL_API.G_RET_STS_ERROR) THEN
4212          RAISE OKL_API.G_EXCEPTION_ERROR;
4213        END IF;
4214 
4215 -- djanaswa ER6274342 end
4216 
4217     --   print( l_prog_name, ' generating streams - begin');
4218     -- Added by RGOOTY: Start
4219     IF ( p_generation_type <> 'SERVICE_LINES' )
4220     THEN
4221       FOR l_line_rec in l_line_csr( p_khr_id, 'FREE_FORM1' )
4222       LOOP
4223         -- Residual Value Streams are getting generated here ....
4224         IF ( (p_generation_type = 'FULL' OR p_generation_type = 'RESIDUAL VALUE') ) AND
4225              ( nvl(l_line_rec.residual_value, 0) > 0 )
4226         THEN
4227 
4228             l_selv_tbl.delete;
4229             -- Moving the execution of the RV Stream out of loop
4230             IF ( l_rv_sty_id IS NOT NULL ) THEN
4231                get_stream_header(
4232                           p_khr_id         =>   p_khr_id,
4233                           p_kle_id         =>   l_line_rec.id,
4234                           p_sty_id         =>   l_rv_sty_id,
4235                           p_purpose_code   =>   l_purpose_code,
4236                           x_stmv_rec       =>   l_stmv_rec,
4237                           x_return_status  =>   lx_return_status);
4238 
4239                IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4240                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4241                ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4242                  RAISE OKL_API.G_EXCEPTION_ERROR;
4243                END IF;
4244 
4245                l_selv_tbl(1).amount                     := l_line_rec.residual_value;
4246                l_selv_tbl(1).stream_element_date        := l_hdr.end_date;
4247 
4248                --Bug# 9725362
4249                if l_line_rec.ste_code ='TERMINATED' then
4250                  l_selv_tbl(1).stream_element_date      := l_line_rec.end_date;
4251                end if;
4252 
4253                l_selv_tbl(1).se_line_number             := 1;
4254                l_selv_tbl(1).accrued_yn                 := NULL;
4255 
4256                lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
4257 	                                p_api_version   => g_api_version,
4258                                         p_init_msg_list => p_init_msg_list,
4259                                         x_msg_count     => x_msg_count,
4260                                         x_msg_data      => x_msg_data,
4261                                         p_chr_id        => p_khr_id,
4262                                         p_selv_tbl      => l_selv_tbl,
4263                                         x_selv_tbl      => lx_selv_tbl,
4264                                         p_org_id        => G_ORG_ID,
4265                                         p_precision     => G_PRECISION,
4266                                         p_currency_code => G_CURRENCY_CODE,
4267                                         p_rounding_rule => G_ROUNDING_RULE,
4268                                         p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
4269 
4270                IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4271                    RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4272                ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4273                    RAISE OKL_API.G_EXCEPTION_ERROR;
4274                END IF;
4275                l_selv_tbl.DELETE;
4276                l_selv_tbl := lx_selv_tbl;
4277                --Accumulate Stream Header: 4346646
4278                OKL_STREAMS_UTIL.accumulate_strm_headers(
4279                  p_stmv_rec       => l_stmv_rec,
4280                  x_full_stmv_tbl  => l_stmv_tbl,
4281                  x_return_status  => lx_return_status );
4282                IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4283                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4284                ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4285                  RAISE OKL_API.G_EXCEPTION_ERROR;
4286                END IF;
4287                --Accumulate Stream Elements
4288                OKL_STREAMS_UTIL.accumulate_strm_elements(
4289                  p_stm_index_no  =>  l_stmv_tbl.LAST,
4290                  p_selv_tbl       => l_selv_tbl,
4291                  x_full_selv_tbl  => l_full_selv_tbl,
4292                  x_return_status  => lx_return_status );
4293                IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4294                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4295                ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4296                  RAISE OKL_API.G_EXCEPTION_ERROR;
4297                END IF;
4298                l_stmv_rec := NULL;
4299                l_selv_tbl.delete;
4300                lx_stmv_rec := NULL;
4301                lx_selv_tbl.delete;
4302           END IF;
4303         END IF;
4304 
4305         -- Capital Reduction Streams are getting generated here ....
4306         IF ( l_line_rec.capitalize_down_payment_yn = 'Y' AND
4307              ( nvl(l_line_rec.capital_reduction, l_line_rec.capital_reduction_percent) > 0 ) ) AND
4308            (p_generation_type = 'FULL' OR p_generation_type = 'CAPITAL REDUCTION')
4309         THEN
4310             OKL_ISG_UTILS_PVT.get_primary_stream_type(
4311               p_khr_id              => p_khr_id,
4312               p_deal_type           => l_deal_type,
4313               p_primary_sty_purpose => 'CAPITAL_REDUCTION',
4314               x_return_status       => lx_return_status,
4315               x_primary_sty_id      => l_capred_sty_id,
4316               x_primary_sty_name    => l_capred_sty_name);
4317             IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4318               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4319             ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4320               RAISE OKL_API.G_EXCEPTION_ERROR;
4321             END IF;
4322 
4323             l_selv_tbl.delete;
4324             IF ( l_capred_sty_id IS NOT NULL ) THEN
4325                get_stream_header(
4326                           p_khr_id         =>   p_khr_id,
4327                           p_kle_id         =>   l_line_rec.id,
4328                           p_sty_id         =>   l_capred_sty_id,
4329                           p_purpose_code   =>   l_purpose_code,
4330                           x_stmv_rec       =>   l_stmv_rec,
4331                           x_return_status  =>   lx_return_status);
4332 
4333                IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4334                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4335                ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4336                  RAISE OKL_API.G_EXCEPTION_ERROR;
4337                END IF;
4338 
4339                IF (l_line_rec.capital_reduction IS NULL ) Then
4340                  l_selv_tbl(1).amount                     := l_line_rec.oec * l_line_rec.capital_reduction_percent * 0.01;
4341                Else
4342                  l_selv_tbl(1).amount                     := l_line_rec.capital_reduction;
4343                END IF;
4344 
4345                l_selv_tbl(1).stream_element_date        := l_hdr.start_date;
4346                l_selv_tbl(1).se_line_number             := 1;
4347                l_selv_tbl(1).accrued_yn                 := NULL;
4348 
4349                lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
4350 	                                p_api_version   => g_api_version,
4351                                         p_init_msg_list => p_init_msg_list,
4352                                         x_msg_count     => x_msg_count,
4353                                         x_msg_data      => x_msg_data,
4354                                         p_chr_id        => p_khr_id,
4355                                         p_selv_tbl      => l_selv_tbl,
4356                                         x_selv_tbl      => lx_selv_tbl,
4357                                         p_org_id        => G_ORG_ID,
4358                                         p_precision     => G_PRECISION,
4359                                         p_currency_code => G_CURRENCY_CODE,
4360                                         p_rounding_rule => G_ROUNDING_RULE,
4361                                         p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
4362 
4363                IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4364                    RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4365                ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4366                    RAISE OKL_API.G_EXCEPTION_ERROR;
4367                END IF;
4368 
4369                l_selv_tbl.DELETE;
4370                l_selv_tbl := lx_selv_tbl;
4371 
4372                --Accumulate Stream Header: 4346646
4373                OKL_STREAMS_UTIL.accumulate_strm_headers(
4374                  p_stmv_rec       => l_stmv_rec,
4375                  x_full_stmv_tbl  => l_stmv_tbl,
4376                  x_return_status  => lx_return_status );
4377                IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4378                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4379                ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4380                  RAISE OKL_API.G_EXCEPTION_ERROR;
4381                END IF;
4382 
4383                --Accumulate Stream Elements
4384                OKL_STREAMS_UTIL.accumulate_strm_elements(
4385                  p_stm_index_no  =>  l_stmv_tbl.LAST,
4386                  p_selv_tbl       => l_selv_tbl,
4387                  x_full_selv_tbl  => l_full_selv_tbl,
4388                  x_return_status  => lx_return_status );
4389                IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4390                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4391                ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4392                  RAISE OKL_API.G_EXCEPTION_ERROR;
4393                END IF;
4394 
4395                l_stmv_rec := NULL;
4396                l_selv_tbl.delete;
4397                lx_stmv_rec := NULL;
4398                lx_selv_tbl.delete;
4399           END IF;
4400 
4401         END IF;
4402 
4403         IF (p_generation_type = 'FULL' OR p_generation_type = 'SUBSIDY') AND ( p_subsidies_yn = 'Y' ) THEN
4404 
4405             OKL_SUBSIDY_PROCESS_PVT.get_asset_subsidy_amount(
4406                                        p_api_version     =>   G_API_VERSION,
4407                                        p_init_msg_list   =>   G_FALSE,
4408                                        p_asset_cle_id    =>   l_line_rec.id,
4409                                        x_return_status   =>   lx_return_status,
4410                                        x_msg_count       =>   x_msg_count,
4411                                        x_msg_data        =>   x_msg_data,
4412                                        x_asbv_tbl        =>   l_asbv_tbl);
4413 
4414             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4415                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4416             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4417                 RAISE OKL_API.G_EXCEPTION_ERROR;
4418             END IF;
4419 
4420             print( l_prog_name, ' count ' || to_char(l_asbv_tbl.count));
4421 
4422 
4423 
4424         FOR i in 1..l_asbv_tbl.COUNT
4425 	    LOOP
4426                 --print( l_prog_name, ' method ' || l_asbv_tbl(i).accounting_method_code);
4427 
4428                 If ( l_asbv_tbl(i).accounting_method_code = 'AMORTIZE' )
4429                 THEN
4430 	               l_end_date := l_hdr.end_date;
4431 	               IF ( l_asbv_tbl(i).maximum_term < l_hdr.term_duration )
4432                    THEN
4433                         OPEN c_inflows( p_khr_id, l_line_rec.id);
4434                         FETCH c_inflows INTO r_inflows;
4435                         CLOSE c_inflows;
4436                         l_months_factor := get_months_factor(
4437                                                 p_frequency     => r_inflows.frequency,
4438                                                 x_return_status => lx_return_status );
4439 
4440                         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4441                             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4442                         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4443                             RAISE OKL_API.G_EXCEPTION_ERROR;
4444                         END IF;
4445 
4446 -- djanaswa ER6274342 added parameter p_arrears_pay_dates_option
4447 
4448                         l_first_sel_date := get_first_sel_date( p_start_date          => l_hdr.start_date,
4449                                                                 p_advance_or_arrears  => r_inflows.advance_arrears,
4450                                                                 p_months_increment    => l_months_factor,
4451                                                                 p_arrears_pay_dates_option => l_arrears_pay_dates_option,
4452                                                                 x_return_status       => lx_return_status );
4453 
4454                         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4455                             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4456                         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4457                             RAISE OKL_API.G_EXCEPTION_ERROR;
4458                         END IF;
4459                         l_end_date := ADD_MONTHS(l_first_sel_date,
4460                                             l_months_factor*(l_asbv_tbl(i).maximum_term-1));
4461 
4462 		    End If;
4463 
4464 	            get_amortized_accruals (
4465 		                     p_khr_id         => p_khr_id,
4466 	                             p_currency_code  => l_hdr.currency_code,
4467                                      p_start_date     => l_line_rec.start_date,
4468 				     p_end_date       => l_end_date,
4469                                      p_deal_type      => l_hdr.deal_type,
4470 				     p_amount         => l_asbv_tbl(i).amount,
4471                                      x_selv_tbl       => l_selv_tbl,
4472                                      x_return_status  => lx_return_status);
4473 
4474                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4475                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4476                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4477                         RAISE OKL_API.G_EXCEPTION_ERROR;
4478 	            END IF;
4479 
4480 		ElsIf (l_asbv_tbl(i).accounting_method_code = 'NET' ) Then
4481 
4482                     get_accrual_elements (
4483 		                p_start_date          =>   l_hdr.start_date,
4484                                 p_periods             =>   1,
4485                                 p_frequency           =>   'M',
4486                                 p_structure           =>   0,
4487                                 p_advance_or_arrears  =>   'ARREARS',
4488                                 p_amount              =>   l_asbv_tbl(i).amount,
4489 				p_stub_days           =>   NULL,
4490 				p_stub_amount         =>   NULL,
4491                                 p_currency_code       =>   l_hdr.currency_code,
4492                                 x_selv_tbl            =>   l_selv_tbl,
4493                                 x_return_status       =>   lx_return_status);
4494 
4495                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4496                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4497                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4498                         RAISE OKL_API.G_EXCEPTION_ERROR;
4499                     END IF;
4500 
4501                     -- bug# 3330636
4502 		    l_tmp_selv_tbl := l_selv_tbl;
4503 		    l_selv_tbl.DELETE;
4504 		    l_selv_tbl(1) := l_tmp_selv_tbl(1);
4505 		    l_selv_tbl(1).amount := l_asbv_tbl(i).amount;
4506 		    l_tmp_selv_tbl.DELETE;
4507 
4508 		End If;
4509 
4510                 -- get the accrual stream type;
4511 		/*
4512 
4513 		OPEN c_subsidy_strm ( l_asbv_tbl(i).stream_type_id );
4514 		FETCH c_subsidy_strm INTO l_accrual_sty_id;
4515 
4516 
4517 		IF ( c_subsidy_strm%NOTFOUND ) THen
4518 
4519 		    OPEN c_strm_name ( l_asbv_tbl(i).stream_type_id );
4520 		    FETCH c_strm_name INTO l_subsidy_stream;
4521 		    CLOSE c_strm_name;
4522 
4523                     OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
4524                                          p_msg_name     => 'OKL_NO_SUBSIDY_MAP',
4525                                          p_token1       => 'SUBS',
4526                                          p_token1_value => l_subsidy_stream);
4527 
4528 		    CLOSE c_subsidy_strm;
4529                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4530 
4531 	        END IF;
4532 		CLOSE c_subsidy_strm;
4533 
4534 		*/
4535 
4536                 -- bug# 4041666
4537                 OKL_ISG_UTILS_PVT.get_dependent_stream_type(
4538                         p_khr_id  		=> p_khr_id,
4539 			p_deal_type             => l_deal_type,
4540 			p_primary_sty_id        => l_asbv_tbl(i).stream_type_id,
4541                         p_dependent_sty_purpose => 'SUBSIDY_INCOME',
4542                         x_return_status         => lx_return_status,
4543                         x_dependent_sty_id      => l_accrual_sty_id,
4544                         x_dependent_sty_name    => l_sty_name);
4545 
4546              If ( l_accrual_sty_id is not null ) Then
4547 
4548                 get_stream_header(p_khr_id         =>   p_khr_id,
4549                                   p_kle_id         =>   l_line_rec.id,
4550                                   p_sty_id         =>   l_accrual_sty_id,
4551                                   p_purpose_code   =>   l_purpose_code,
4552                                   x_stmv_rec       =>   l_stmv_rec,
4553                                   x_return_status  =>   lx_return_status);
4554 
4555                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4556                   RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4557                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4558                   RAISE OKL_API.G_EXCEPTION_ERROR;
4559                 END IF;
4560 
4561 
4562                lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
4563 	                                p_api_version   => g_api_version,
4564                                         p_init_msg_list => p_init_msg_list,
4565                                         x_msg_count     => x_msg_count,
4566                                         x_msg_data      => x_msg_data,
4567                                         p_chr_id        => p_khr_id,
4568                                         p_selv_tbl      => l_selv_tbl,
4569                                         x_selv_tbl      => lx_selv_tbl,
4570                                         p_org_id        => G_ORG_ID,
4571                                         p_precision     => G_PRECISION,
4572                                         p_currency_code => G_CURRENCY_CODE,
4573                                         p_rounding_rule => G_ROUNDING_RULE,
4574                                         p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
4575 
4576                IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
4577                    RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4578                ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
4579                    RAISE OKL_API.G_EXCEPTION_ERROR;
4580                END IF;
4581 
4582 	       l_selv_tbl.DELETE;
4583 	       l_selv_tbl := lx_selv_tbl;
4584 
4585                 okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
4586                                                p_init_msg_list   =>   G_FALSE,
4587                                                x_return_status   =>   lx_return_status,
4588                                                x_msg_count       =>   x_msg_count,
4589                                                x_msg_data        =>   x_msg_data,
4590                                                p_stmv_rec        =>   l_stmv_rec,
4591                                                p_selv_tbl        =>   l_selv_tbl,
4592                                                x_stmv_rec        =>   lx_stmv_rec,
4593                                                x_selv_tbl        =>   lx_selv_tbl);
4594 
4595                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4596                   RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4597                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4598                   RAISE OKL_API.G_EXCEPTION_ERROR;
4599                 END IF;
4600 
4601                 l_selv_tbl.delete;
4602 
4603 	     End If;
4604 
4605 	    END LOOP;
4606 
4607 	    l_asbv_tbl.delete;
4608 
4609 	End If;
4610 
4611         IF (p_generation_type = 'FULL' ) AND (l_purpose_code = '-99' ) THEN
4612 
4613 
4614             OPEN c_rollover_pmnts( p_khr_id, l_line_rec.id);
4615             FETCH c_rollover_pmnts INTO r_rollover_pmnts;
4616             CLOSE c_rollover_pmnts;
4617 
4618 
4619 	    l_primary_sty_id := r_rollover_pmnts.styId;
4620 
4621             OKL_ISG_UTILS_PVT.get_dependent_stream_type(
4622                         p_khr_id  		=> p_khr_id,
4623 			p_deal_type           => l_deal_type,
4624 			p_primary_sty_id        => l_primary_sty_id,
4625                         p_dependent_sty_purpose => 'PV_RENT',
4626                         x_return_status         => lx_return_status,
4627                         x_dependent_sty_id      => l_sty_id,
4628                         x_dependent_sty_name    => l_sty_name);
4629 
4630 
4631         IF ( l_sty_id is not null ) THEN
4632 
4633             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4634               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4635             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4636               RAISE OKL_API.G_EXCEPTION_ERROR;
4637             END IF;
4638 
4639             get_stream_header(p_khr_id   =>   p_khr_id,
4640                         p_kle_id         =>   l_line_rec.id,
4641                         p_sty_id         =>   l_sty_id,
4642                         p_purpose_code   =>   l_purpose_code,
4643                         x_stmv_rec       =>   l_stmv_rec,
4644                         x_return_status  =>   lx_return_status);
4645 
4646             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4647               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4648             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4649               RAISE OKL_API.G_EXCEPTION_ERROR;
4650             END IF;
4651 
4652             l_selv_tbl.delete;
4653 
4654 
4655              P_FIRST := 'T';
4656              L := 0;
4657 	         i := 0;
4658 
4659             r_inflow_line_tbl.delete;
4660             For r_inflows_line_rec in c_inflows_line( p_khr_id, l_line_rec.id, l_purpose_code )
4661             LOOP
4662                i := i + 1;
4663                r_inflow_line_tbl(i) := r_inflows_line_rec;
4664             END LOOP;
4665             i := 1;
4666 
4667           FOR i IN r_inflow_line_tbl.first .. r_inflow_line_tbl.last
4668           LOOP
4669     	        l_pv_date := r_inflow_line_tbl(i).cf_date;
4670     	        l_pv_amount := 0;
4671 
4672               FOR L IN r_inflow_line_tbl.first .. r_inflow_line_tbl.last
4673               LOOP
4674                  IF trunc(r_inflow_line_tbl(l).cf_date) >= trunc(l_pv_date)
4675                  THEN
4676 
4677                     get_present_value( p_api_version   =>   G_API_VERSION,
4678     				        p_init_msg_list =>   G_FALSE,
4679     				        p_amount_date   =>   r_inflow_line_tbl(l).cf_date,
4680     				        p_amount        =>   r_inflow_line_tbl(l).cf_amount,
4681     				        p_frequency     =>   r_inflow_line_tbl(l).cf_frequency,
4682     				        p_rate          =>   p_pre_tax_irr,
4683                                         p_pv_date       =>   l_pv_date,
4684     				        x_pv_amount     =>   x_pv_amount,
4685                                         x_return_status =>   lx_return_status,
4686                             x_msg_count     =>   x_msg_count,
4687                             x_msg_data      =>   x_msg_data);
4688 
4689                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4690                           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4691                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4692                           RAISE OKL_API.G_EXCEPTION_ERROR;
4693                     END IF;
4694                     l_pv_amount := l_pv_amount + x_pv_amount;
4695                 END IF;
4696             END LOOP;
4697 
4698     	    l_selv_tbl(i).stream_element_date := l_pv_date;
4699             l_selv_tbl(i).amount := l_pv_amount;
4700 
4701             l_selv_tbl(i).se_line_number := i;
4702     		l_selv_tbl(i).accrued_yn := NULL;
4703             l_selv_tbl(i).comments   := r_inflow_line_tbl(i).cf_arrear;
4704 	    END LOOP;
4705 
4706 
4707             lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
4708                                 p_api_version   => p_api_version,
4709                                 p_init_msg_list => p_init_msg_list,
4710                                 x_msg_count     => x_msg_count,
4711                                 x_msg_data      => x_msg_data,
4712                                 p_chr_id        => p_khr_id,
4713                                 p_selv_tbl      => l_selv_tbl,
4714                                 x_selv_tbl      => lx_selv_tbl,
4715                                 p_org_id        => G_ORG_ID,
4716                                 p_precision     => G_PRECISION,
4717                                 p_currency_code => G_CURRENCY_CODE,
4718                                 p_rounding_rule => G_ROUNDING_RULE,
4719                                 p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
4720 
4721             IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
4722                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4723             ELSIF (x_return_status = G_RET_STS_ERROR) THEN
4724                 RAISE OKL_API.G_EXCEPTION_ERROR;
4725             END IF;
4726 
4727             l_selv_tbl.DELETE;
4728             l_selv_tbl := lx_selv_tbl;
4729 
4730             okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
4731                                            p_init_msg_list   =>   G_FALSE,
4732                                            x_return_status   =>   lx_return_status,
4733                                            x_msg_count       =>   x_msg_count,
4734                                            x_msg_data        =>   x_msg_data,
4735                                            p_stmv_rec        =>   l_stmv_rec,
4736                                            p_selv_tbl        =>   l_selv_tbl,
4737                                            x_stmv_rec        =>   lx_stmv_rec,
4738                                            x_selv_tbl        =>   lx_selv_tbl);
4739 
4740             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4741               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4742             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4743               RAISE OKL_API.G_EXCEPTION_ERROR;
4744             END IF;
4745 	  END IF;
4746 
4747         END IF;
4748      END LOOP;
4749          --Create all the accumulated Streams at one shot ..
4750          IF l_stmv_tbl.COUNT > 0 AND
4751             l_full_selv_tbl.COUNT > 0
4752          THEN
4753 Okl_Streams_Pub.create_streams_perf(
4754                                p_api_version,
4755                                p_init_msg_list,
4756                                lx_return_status,
4757                                x_msg_count,
4758                                x_msg_data,
4759                                l_stmv_tbl,
4760                                l_full_selv_tbl,
4761                                lx_stmv_tbl,
4762                                lx_full_selv_tbl);
4763 
4764            IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4765              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4766            ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4767              RAISE OKL_API.G_EXCEPTION_ERROR;
4768            END IF;
4769          END IF;
4770 
4771        END IF;
4772        x_return_status := OKL_API.G_RET_STS_SUCCESS;
4773 
4774        -- Added by RGOOTY: End
4775        --    print( l_prog_name, 'end');
4776 
4777   EXCEPTION
4778 
4779     WHEN OKL_API.G_EXCEPTION_ERROR THEN
4780 
4781       x_return_status := G_RET_STS_ERROR;
4782 
4783     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
4784 
4785       x_return_status := G_RET_STS_UNEXP_ERROR;
4786 
4787     WHEN OTHERS THEN
4788 
4789       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
4790                            p_msg_name     => G_DB_ERROR,
4791                            p_token1       => G_PROG_NAME_TOKEN,
4792                            p_token1_value => l_prog_name,
4793                            p_token2       => G_SQLCODE_TOKEN,
4794                            p_token2_value => sqlcode,
4795                            p_token3       => G_SQLERRM_TOKEN,
4796                            p_token3_value => sqlerrm);
4797 
4798       x_return_status := G_RET_STS_UNEXP_ERROR;
4799 
4800   END  gen_non_cash_flows;
4801 
4802 
4803   ---------------------------------------------------------------------------
4804   -- PROCEDURE generate_cash_flows
4805   ---------------------------------------------------------------------------
4806 
4807   ---------------------------------------------------------------------------
4808   PROCEDURE generate_cash_flows(
4809                              p_api_version   IN  NUMBER,
4810                              p_init_msg_list IN  VARCHAR2,
4811                              p_khr_id        IN  NUMBER,
4812 			     p_kle_id        IN  NUMBER,
4813 			     p_sty_id        IN  NUMBER,
4814 			     p_payment_tbl   IN  payment_tbl_type,
4815                              x_return_status OUT NOCOPY VARCHAR2,
4816                              x_msg_count     OUT NOCOPY NUMBER,
4817                              x_msg_data      OUT NOCOPY VARCHAR2,
4818                              x_payment_count OUT NOCOPY BINARY_INTEGER) IS
4819 
4820     lx_return_status              VARCHAR2(1);
4821 
4822     lx_isAllowed                  BOOLEAN;
4823     lx_passStatus                 VARCHAR2(30);
4824     lx_failStatus                 VARCHAR2(30);
4825 
4826     CURSOR c_hdr IS
4827       SELECT chr.template_yn,
4828              chr.currency_code,
4829              chr.start_date,
4830              khr.deal_type,
4831              khr.term_duration,
4832              NVL(khr.generate_accrual_yn, 'Y')
4833       FROM   okc_k_headers_b chr,
4834              okl_k_headers khr
4835       WHERE  khr.id = p_khr_id
4836         AND  chr.id = khr.id;
4837 
4838     l_hdr                    c_hdr%ROWTYPE;
4839     l_deal_type              VARCHAR2(30);
4840     l_purpose_code           VARCHAR2(30) := 'FLOW';
4841 
4842     l_pt_yn                  VARCHAR2(1);
4843     l_passthrough_id         NUMBER;
4844 
4845     l_sty_id                 NUMBER;
4846     l_sty_name               VARCHAR2(150);
4847     l_mapped_sty_name        VARCHAR2(150);
4848 
4849     l_pre_tax_inc_id         NUMBER;
4850     l_principal_id           NUMBER;
4851     l_interest_id            NUMBER;
4852     l_prin_bal_id            NUMBER;
4853     l_termination_id         NUMBER;
4854 
4855     l_selv_tbl               okl_streams_pub.selv_tbl_type;
4856     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
4857     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
4858 
4859     l_stmv_rec               okl_streams_pub.stmv_rec_type;
4860     l_pt_rec                 okl_streams_pub.stmv_rec_type;
4861     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
4862 
4863     i                        BINARY_INTEGER := 0;
4864     j                        BINARY_INTEGER := 0;
4865     l_ele_count              BINARY_INTEGER := 0;
4866 
4867     l_adv_arr                VARCHAR2(30);
4868 
4869 
4870 
4871     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'generate_cash_flows';
4872     l_se_id NUMBER;
4873 
4874     --Added by djanaswa for bug 6007644
4875     l_recurrence_date    DATE := NULL;
4876     --end djanaswa
4877 
4878   BEGIN
4879 
4880     -- print( l_prog_name, 'begin' );
4881     -- c_hdr cursor fetches the contract/quote details
4882     OPEN  c_hdr;
4883     FETCH c_hdr INTO l_hdr;
4884     CLOSE c_hdr;
4885 
4886     l_deal_type := l_hdr.deal_type;
4887     -- Generates a Stub Element and returns the id of the
4888     -- new Stream Created into x_se_id
4889     generate_stub_element( p_api_version   => p_api_version,
4890                            p_init_msg_list => p_init_msg_list,
4891                            p_khr_id        => p_khr_id,
4892 			               p_deal_type     => l_deal_type,
4893                            x_return_status => x_return_status,
4894                            x_msg_count     => x_msg_count,
4895                            x_msg_data      => x_msg_data,
4896                            x_se_id         => l_se_id );
4897     --print( l_prog_name, 'stub', x_return_status );
4898     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4899       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4900     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
4901       RAISE OKL_API.G_EXCEPTION_ERROR;
4902     END IF;
4903 
4904     ---------------------------------------------
4905     -- STEP 1: Spread cash INFLOW
4906     ---------------------------------------------
4907     --print( l_prog_name, ' generating streams - begin');
4908     get_stream_header(
4909         p_khr_id         =>   p_khr_id,
4910         p_kle_id         =>   p_kle_id,  -- p_kle_id is passed to generate_cash_flows
4911         p_sty_id         =>   p_sty_id,  -- p_sty_id is passed
4912         p_purpose_code   =>   l_purpose_code, -- 'FLOW'
4913         x_stmv_rec       =>   l_stmv_rec,
4914         x_return_status  =>   lx_return_status);
4915     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4916         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4917     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4918         RAISE OKL_API.G_EXCEPTION_ERROR;
4919     END IF;
4920 
4921     l_stmv_rec.purpose_code := 'FLOW';
4922     -- Proceed with Creating the RENT Stream Header
4923 okl_streams_pub.create_streams(
4924         p_api_version     =>   G_API_VERSION,
4925         p_init_msg_list   =>   G_FALSE,
4926         x_return_status   =>   lx_return_status,
4927         x_msg_count       =>   x_msg_count,
4928         x_msg_data        =>   x_msg_data,
4929         p_stmv_rec        =>   l_stmv_rec,
4930         x_stmv_rec        =>   lx_stmv_rec);
4931     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4932         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4933     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
4934         RAISE OKL_API.G_EXCEPTION_ERROR;
4935     END IF;
4936 
4937     --print( l_prog_name, 'created header' );
4938     x_payment_count  :=  0;
4939     l_ele_count := 0;
4940     -- Creating Stream lines based on the Payments Received ...
4941     -- p_payment_tbl will be having the information similiar to rules..
4942     FOR i IN p_payment_tbl.FIRST..p_payment_tbl.LAST
4943     LOOP
4944         IF p_payment_tbl(i).start_date IS NULL
4945         THEN
4946           OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
4947                                p_msg_name     => 'OKL_NO_SLL_SDATE');
4948           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4949         END IF;
4950         /*
4951          * calculate stream elements for each payment level
4952          * also means that if there are multiple payment levels for an asset, streams are
4953          * calculated at different points.
4954          * the streams amounts are they are entered in payments. In case of passthru, a
4955          * new set of streams are created with amounts that are passthru'ed - l_pt_tbl.
4956          */
4957         IF ( p_payment_tbl(i).arrears_yn = 'Y' )
4958         THEN
4959             l_adv_arr := 'ARREARS';
4960 	    ELSE
4961             l_adv_arr := 'ADVANCE';
4962 	    End If;
4963 
4964        --Added by djanaswa for bug 6007644
4965         IF((p_payment_tbl(i).periods IS NULL) AND (p_payment_tbl(i).stub_days IS NOT NULL)) THEN
4966           --Set the recurrence date to null for stub payment
4967           l_recurrence_date := NULL;
4968         ELSIF(l_recurrence_date IS NULL) THEN
4969           --Set the recurrence date as Periodic payment level start date
4970           l_recurrence_date := p_payment_tbl(i).start_date;
4971         END IF;
4972         --end djanaswa
4973 
4974         --print( l_prog_name, 'start date ' || p_payment_tbl(i).start_date );
4975         -- get_stream_elements create the Stream elements records based on the
4976         -- payments scheduled.
4977         --Added parameter p_recurrence_date by djanaswa for bug 6007644
4978         get_stream_elements(
4979             p_start_date          =>   p_payment_tbl(i).start_date,
4980             p_periods             =>   p_payment_tbl(i).periods,
4981             p_frequency           =>   p_payment_tbl(i).frequency,
4982             p_structure           =>   p_payment_tbl(i).structure,
4983             p_advance_or_arrears  =>   l_adv_arr,
4984             p_amount              =>   p_payment_tbl(i).amount,
4985             p_stub_days           =>   p_payment_tbl(i).stub_days,
4986             p_stub_amount         =>   p_payment_tbl(i).stub_amount,
4987             p_currency_code       =>   l_hdr.currency_code,
4988             p_khr_id              =>   p_khr_id,
4989             p_kle_id              =>   p_kle_id,
4990             p_purpose_code        =>   l_purpose_code,
4991             x_selv_tbl            =>   l_selv_tbl,
4992             x_pt_tbl              =>   l_pt_tbl,
4993             x_return_status       =>   lx_return_status,
4994             x_msg_count           =>   x_msg_count,
4995             x_msg_data            =>   x_msg_data,
4996             p_recurrence_date     =>   l_recurrence_date);
4997         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
4998           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4999         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5000           RAISE OKL_API.G_EXCEPTION_ERROR;
5001         END IF;
5002 
5003         --print( l_prog_name, 'created elements ' || to_char(l_selv_tbl.COUNT) );
5004         FOR j in 1..l_selv_tbl.COUNT
5005         LOOP
5006     	    l_ele_count := l_ele_count + 1;
5007     	    l_selv_tbl(j).stm_id := lx_stmv_rec.id;
5008     	    l_selv_tbl(j).se_line_number := l_ele_count;
5009     	    l_selv_tbl(j).id := NULL;
5010         END LOOP;
5011 
5012         -- Case of Stub days is given
5013         IF ( p_payment_tbl(i).stub_days IS NOT NULL ) AND ( p_payment_tbl(i).periods IS NULL )
5014         THEN
5015     	    FOR i in 1..l_selv_tbl.COUNT
5016     	    LOOP
5017     	        l_selv_tbl(i).sel_id := l_se_id;
5018     	    END LOOP;
5019 
5020     	    FOR i in 1..l_pt_tbl.COUNT
5021     	    LOOP
5022     	        l_pt_tbl(i).sel_id := l_se_id;
5023     	    END LOOP;
5024     	END IF;
5025         --print( l_prog_name, 'start date ' || l_selv_tbl(1).stream_element_date );
5026         -- Create the Stream Elements in the OKL_STRM_ELEMENTS table
5027         -- feels like the join between stream header and stream elements here is
5028         -- sel_id, we are populating it in the above for loop.
5029 okl_streams_pub.create_stream_elements(
5030 	                               p_api_version     =>   G_API_VERSION,
5031                                    p_init_msg_list   =>   G_FALSE,
5032                                    x_return_status   =>   lx_return_status,
5033                                    x_msg_count       =>   x_msg_count,
5034                                    x_msg_data        =>   x_msg_data,
5035                                    p_selv_tbl        =>   l_selv_tbl,
5036                                    x_selv_tbl        =>   lx_selv_tbl);
5037 
5038         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5039           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5040         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5041           RAISE OKL_API.G_EXCEPTION_ERROR;
5042         END IF;
5043 
5044         --print( l_prog_name, 'created elements ' || to_char(l_ele_count) );
5045         -- Deleting the Stream Elements for next iteration.
5046         l_selv_tbl.DELETE;
5047 
5048         FOR j in 1..lx_selv_tbl.COUNT
5049         LOOP
5050     	    l_ele_count := l_ele_count + 1;
5051     	    l_selv_tbl(j) := lx_selv_tbl(j);
5052     	    -- If Payment rate is not present, then initialize the amount with -999999
5053     	    l_selv_tbl(j).amount := nvl(p_payment_tbl(i).rate, -9999999);
5054     	    l_selv_tbl(j).sel_id := lx_selv_tbl(j).id;
5055     	    l_selv_tbl(j).se_line_number := l_ele_count;
5056     	    l_selv_tbl(j).id := NULL;
5057     	    -- Store in stream elements comments whether that represents the
5058     	    -- missing payments or not.
5059     	    IF ( lx_selv_tbl(j).amount = -9999999 )
5060             THEN
5061 	           l_selv_tbl(j).comments := 'Y';
5062             ELSE
5063 	           l_selv_tbl(j).comments := 'N';
5064 	       END IF;
5065         END LOOP;
5066         lx_selv_tbl.DELETE;
5067         -- Create/Update Stream Elements with the above modification.
5068 okl_streams_pub.create_stream_elements(
5069 	                               p_api_version     =>   G_API_VERSION,
5070                                    p_init_msg_list   =>   G_FALSE,
5071                                    x_return_status   =>   lx_return_status,
5072                                    x_msg_count       =>   x_msg_count,
5073                                    x_msg_data        =>   x_msg_data,
5074                                    p_selv_tbl        =>   l_selv_tbl,
5075                                    x_selv_tbl        =>   lx_selv_tbl);
5076         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5077             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5078         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5079             RAISE OKL_API.G_EXCEPTION_ERROR;
5080         END IF;
5081         --print( l_prog_name, 'created elements rates ' || to_char(l_ele_count) );
5082         --print( l_prog_name, ' pass thru count ' || to_char(l_pt_tbl.COUNT) );
5083 
5084         -- Code for handling Passthroughs Follows
5085         IF l_pt_tbl.COUNT > 0
5086         THEN
5087           IF l_passthrough_id IS NULL
5088           THEN
5089                 OKL_ISG_UTILS_PVT.get_dependent_stream_type(
5090                                     p_khr_id                => p_khr_id,
5091                                     p_deal_type             => l_deal_type,
5092                                     p_dependent_sty_purpose => 'PASS_THRU_EXP_ACCRUAL',
5093                                     x_return_status         => lx_return_status,
5094                                     x_dependent_sty_id      => l_passthrough_id,
5095                                     x_dependent_sty_name    => l_sty_name);
5096                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5097                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5098                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5099                     RAISE OKL_API.G_EXCEPTION_ERROR;
5100                 END IF;
5101             END IF;
5102             IF l_passthrough_id IS NOT NULL
5103             THEN
5104                 get_stream_header(p_khr_id         =>   p_khr_id,
5105                             p_kle_id         =>   p_kle_id,
5106                             p_sty_id         =>   l_passthrough_id,
5107                             p_purpose_code   =>   l_purpose_code,
5108                             x_stmv_rec       =>   l_pt_rec,
5109                             x_return_status  =>   lx_return_status);
5110 
5111                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5112                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5113                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5114                     RAISE OKL_API.G_EXCEPTION_ERROR;
5115                 END IF;
5116 
5117                 l_pt_rec.purpose_code := 'PLOW';
5118                 lx_return_status := Okl_Streams_Util.round_streams_amount(
5119                               p_api_version   => g_api_version,
5120                               p_init_msg_list => p_init_msg_list,
5121                               x_msg_count     => x_msg_count,
5122                               x_msg_data      => x_msg_data,
5123                               p_chr_id        => p_khr_id,
5124                               p_selv_tbl      => l_pt_tbl,
5125                               x_selv_tbl      => lx_selv_tbl);
5126 
5127                 IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5128                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5129                 ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5130                     RAISE OKL_API.G_EXCEPTION_ERROR;
5131                 END IF;
5132                 l_pt_tbl.DELETE;
5133                 l_pt_tbl := lx_selv_tbl;
5134                 -- Create additional Pass Through Streams
5135 okl_streams_pub.create_streams(
5136                             p_api_version     =>   G_API_VERSION,
5137                             p_init_msg_list   =>   G_FALSE,
5138                             x_return_status   =>   lx_return_status,
5139                             x_msg_count       =>   x_msg_count,
5140                             x_msg_data        =>   x_msg_data,
5141                             p_stmv_rec        =>   l_pt_rec,
5142                             p_selv_tbl        =>   l_pt_tbl,
5143                             x_stmv_rec        =>   lx_stmv_rec,
5144                             x_selv_tbl        =>   lx_selv_tbl);
5145 
5146                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5147                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5148                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5149                     RAISE OKL_API.G_EXCEPTION_ERROR;
5150                 END IF;
5151             END IF;
5152         END IF;
5153         -- Clear out reusable data structures
5154         l_pt_rec := NULL;
5155         l_selv_tbl.delete;
5156         l_pt_tbl.delete;
5157         lx_selv_tbl.delete;
5158         x_payment_count  :=  x_payment_count + 1;
5159         --print( l_prog_name, ' payment count ' || to_char(x_payment_count) );
5160     END LOOP;
5161     --print( l_prog_name, ' done ' );
5162     l_sty_name  :=  NULL;
5163     l_sty_id    :=  NULL;
5164     l_stmv_rec := NULL;
5165     lx_stmv_rec := NULL;
5166     x_return_status := lx_return_status;
5167 
5168   EXCEPTION
5169 
5170     WHEN OKL_API.G_EXCEPTION_ERROR THEN
5171 
5172       x_return_status := G_RET_STS_ERROR;
5173 
5174     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5175 
5176       x_return_status := G_RET_STS_UNEXP_ERROR;
5177 
5178     WHEN OTHERS THEN
5179 
5180       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
5181                            p_msg_name     => G_DB_ERROR,
5182                            p_token1       => G_PROG_NAME_TOKEN,
5183                            p_token1_value => l_prog_name,
5184                            p_token2       => G_SQLCODE_TOKEN,
5185                            p_token2_value => sqlcode,
5186                            p_token3       => G_SQLERRM_TOKEN,
5187                            p_token3_value => sqlerrm);
5188 
5189       x_return_status := G_RET_STS_UNEXP_ERROR;
5190 
5191   END generate_cash_flows;
5192 
5193   ---------------------------------------------------------------------------
5194   -- PROCEDURE generate_cash_flows
5195   ---------------------------------------------------------------------------
5196   -- Added output parameter x_se_id by prasjain for bug 5474827
5197   ---------------------------------------------------------------------------
5198   PROCEDURE generate_cash_flows
5199                             ( p_api_version                IN         NUMBER,
5200                               p_init_msg_list              IN         VARCHAR2,
5201                               p_khr_id                     IN         NUMBER,
5202                               p_generation_type            IN         VARCHAR2,
5203                               p_reporting_book_class       IN         VARCHAR2,
5204                               x_payment_count              OUT NOCOPY BINARY_INTEGER,
5205                               x_return_status              OUT NOCOPY VARCHAR2,
5206                               x_msg_count                  OUT NOCOPY NUMBER,
5207                               x_msg_data                   OUT NOCOPY VARCHAR2,
5208 			      x_se_id                      OUT NOCOPY NUMBER) IS
5209 
5210     lx_return_status              VARCHAR2(1);
5211 
5212     lx_isAllowed                  BOOLEAN;
5213     lx_passStatus                 VARCHAR2(30);
5214     lx_failStatus                 VARCHAR2(30);
5215 
5216 
5217     CURSOR c_hdr IS
5218       SELECT chr.template_yn,
5219              chr.currency_code,
5220              chr.start_date,
5221              khr.deal_type,
5222              khr.term_duration,
5223              NVL(khr.generate_accrual_yn, 'Y')
5224       FROM   okc_k_headers_b chr,
5225              okl_k_headers khr
5226       WHERE  khr.id = p_khr_id
5227         AND  chr.id = khr.id;
5228 
5229     l_hdr                    c_hdr%ROWTYPE;
5230     l_deal_type              VARCHAR2(30);
5231     l_purpose_code           VARCHAR2(30);
5232 
5233  --Added order by cle_id, sty_id, start_date by djanaswa for bug 6007644
5234     CURSOR c_inflows IS
5235       (SELECT rgp.cle_id cle_id,
5236              TO_NUMBER(rul1.object1_id1) sty_id,
5237              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
5238              TO_NUMBER(rul2.rule_information3) periods,
5239              rul2.object1_id1 frequency,
5240              rul2.rule_information5 structure,
5241              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
5242              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
5243              TO_NUMBER(rul2.rule_information7) stub_days,
5244              TO_NUMBER(rul2.rule_information8) stub_amount,
5245              lse.lty_code lty_code,
5246              kle.capital_amount capital_amount,
5247              kle.residual_value residual_value,
5248              kle.fee_type fee_type
5249       FROM   okc_rule_groups_b rgp,
5250              okc_rules_b rul1,
5251              okc_rules_b rul2,
5252              okc_k_lines_b cle,
5253              okl_k_lines kle,
5254              okc_line_styles_b lse
5255       WHERE
5256              rul2.dnz_chr_id = p_khr_id
5257         AND  rul2.rule_information_category = 'LASLL'
5258         AND  rul1.rule_information_category = 'LASLH'
5259         AND  rul1.jtot_object1_code = 'OKL_STRMTYP'
5260         AND  rul2.rgp_id = rgp.id
5261         AND  TO_NUMBER(rul2.object2_id1) = rul1.id
5262         AND  rgp.cle_id = cle.id
5263         AND  cle.sts_code IN ('PASSED', 'COMPLETE', 'TERMINATED')
5264         AND  cle.id = kle.id
5265         AND  cle.lse_id = lse.id)
5266       UNION
5267       (SELECT TO_NUMBER(NULL) cle_id,
5268              TO_NUMBER(rul1.object1_id1) sty_id,
5269              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
5270              TO_NUMBER(rul2.rule_information3) periods,
5271              rul2.object1_id1 frequency,
5272              rul2.rule_information5 structure,
5273              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
5274              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
5275              TO_NUMBER(rul2.rule_information7) stub_days,
5276              TO_NUMBER(rul2.rule_information8) stub_amount,
5277              NULL lty_code,
5278              TO_NUMBER(NULL) capital_amount,
5279              TO_NUMBER(NULL) residual_value,
5280              NULL fee_type
5281       FROM   okc_rule_groups_b rgp,
5282              okc_rules_b rul1,
5283              okc_rules_b rul2
5284       WHERE
5285              rul2.dnz_chr_id = p_khr_id
5286         AND  rul2.rule_information_category = 'LASLL'
5287         AND  rul1.rule_information_category = 'LASLH'
5288         AND  rul1.jtot_object1_code = 'OKL_STRMTYP'
5289         AND  rul2.rgp_id = rgp.id
5290         AND  rgp.cle_id IS NULL
5291         AND  TO_NUMBER(rul2.object2_id1) = rul1.id)
5292         ORDER BY cle_id, sty_id, start_date;
5293 
5294   l_inflow c_inflows%rowtype;
5295 
5296     CURSOR c_fin_assets IS
5297       SELECT kle.id,
5298              NVL(kle.residual_value, 0) residual_value,
5299              cle.start_date
5300       FROM   okc_k_lines_b cle,
5301              okl_k_lines kle,
5302              okc_line_styles_b lse
5303       WHERE  cle.chr_id = p_khr_id
5304         AND  cle.sts_code IN ('PASSED', 'COMPLETE', 'TERMINATED')
5305         AND  cle.lse_id = lse.id
5306         AND  lse.lty_code = 'FREE_FORM1'
5307         AND  cle.id = kle.id;
5308 
5309 
5310     CURSOR c_pt_yn (p_cle_id NUMBER) IS
5311       SELECT 'Y'
5312       FROM   okc_rule_groups_b
5313       WHERE  cle_id = p_cle_id
5314         AND  rgd_code = 'LAPSTH';
5315 
5316     l_pt_yn                  VARCHAR2(1);
5317     l_passthrough_id         NUMBER;
5318 
5319     l_sty_id                 NUMBER;
5320     l_sty_name               VARCHAR2(150);
5321     l_mapped_sty_name        VARCHAR2(150);
5322 
5323     l_pre_tax_inc_id         NUMBER;
5324     l_principal_id           NUMBER;
5325     l_interest_id            NUMBER;
5326     l_prin_bal_id            NUMBER;
5327     l_termination_id         NUMBER;
5328 
5329     l_selv_tbl               okl_streams_pub.selv_tbl_type;
5330     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
5331     l_pt_pro_fee_tbl         okl_streams_pub.selv_tbl_type;
5332     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
5333 
5334     l_stmv_rec               okl_streams_pub.stmv_rec_type;
5335     l_pt_rec                 okl_streams_pub.stmv_rec_type;
5336     l_pt_pro_fee_rec         okl_streams_pub.stmv_rec_type;
5337     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
5338 
5339     -- Loan Amortization
5340     l_principal_tbl          okl_streams_pub.selv_tbl_type;
5341     l_interest_tbl           okl_streams_pub.selv_tbl_type;
5342     l_prin_bal_tbl           okl_streams_pub.selv_tbl_type;
5343     l_termination_tbl        okl_streams_pub.selv_tbl_type;
5344     l_pre_tax_inc_tbl        okl_streams_pub.selv_tbl_type;
5345 
5346     l_capital_cost           NUMBER;
5347     l_interim_interest       NUMBER;
5348     l_interim_days           NUMBER;
5349     l_interim_dpp            NUMBER;
5350     l_asset_iir              NUMBER;
5351     l_asset_booking_yield    NUMBER;
5352 
5353     i                        BINARY_INTEGER := 0;
5354 
5355 
5356     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'generate_cash_flows';
5357     l_pay_tbl OKL_STREAM_GENERATOR_PVT.payment_tbl_type;
5358 
5359     l_se_id NUMBER;
5360 
5361    l_passthrough_pro_fee_id NUMBER;
5362 
5363     -- Added by RGOOTY for perf.: Bug Number 4346646 Start
5364     l_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
5365     l_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
5366 
5367     lx_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
5368     lx_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
5369     -- Added by RGOOTY for perf.: End
5370 
5371     cursor fee_strm_type_csr ( kleid NUMBER,
5372                                linestyle VARCHAR2 ) is
5373     select tl.name strm_name,
5374            sty.capitalize_yn capitalize_yn,
5375            kle.id   line_id,
5376            sty.id   styp_id,
5377            sty.stream_type_class stream_type_class
5378     from okl_strm_type_tl tl,
5379          okl_strm_type_v sty,
5380          okc_k_items cim,
5381          okl_k_lines_full_v kle,
5382          okc_line_styles_b ls
5383     where tl.id = sty.id
5384          and tl.language = 'US'
5385          and cim.cle_id = kle.id
5386          and ls.id = kle.lse_id
5387          and ls.lty_code = 'FEE'
5388          and cim.object1_id1 = sty.id
5389          and cim.object1_id2 = '#'
5390          and kle.id = kleid;
5391 
5392     fee_strm_type_rec fee_strm_type_csr%ROWTYPE;
5393 
5394     top_svc_rec top_svc_csr%ROWTYPE;
5395 
5396   --Added by djanaswa for bug 6007644
5397     l_recurrence_date    DATE := NULL;
5398     l_old_cle_id         NUMBER;
5399     l_old_sty_id         NUMBER;
5400     --end djanaswa
5401 
5402 
5403   BEGIN
5404 
5405 --    print( l_prog_name, 'begin' );
5406     OPEN  c_hdr;
5407     FETCH c_hdr INTO l_hdr;
5408     CLOSE c_hdr;
5409 
5410     IF p_reporting_book_class IS NOT NULL THEN
5411       l_deal_type    :=  p_reporting_book_class;
5412       l_purpose_code := 'REPORT';
5413     ELSE
5414       l_deal_type    :=  l_hdr.deal_type;
5415       l_purpose_code := '-99';
5416     END IF;
5417 
5418    generate_stub_element( p_api_version   => p_api_version,
5419                           p_init_msg_list => p_init_msg_list,
5420                           p_khr_id        => p_khr_id,
5421 			  p_deal_type     => l_deal_type,
5422                           x_return_status => x_return_status,
5423                           x_msg_count     => x_msg_count,
5424                           x_msg_data      => x_msg_data,
5425 			              x_se_id         => l_se_id );
5426  --       print( l_prog_name, ' generate_stub_elements ', x_return_status );
5427 
5428    IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5429      RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5430    ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
5431      RAISE OKL_API.G_EXCEPTION_ERROR;
5432    END IF;
5433 
5434       --Added by prasjain for bug 5474827
5435       x_se_id := l_se_id;
5436       --end prasjain
5437 
5438       ---------------------------------------------
5439       -- STEP 1: Spread cash INFLOW
5440       ---------------------------------------------
5441 
5442         print( l_prog_name, ' generating streams - begin');
5443       x_payment_count  :=  0;
5444       FOR l_inflow IN c_inflows LOOP
5445         IF ( (l_inflow.lty_code = 'SOLD_SERVICE' OR l_inflow.lty_code = 'LINK_SERV_ASSET') AND
5446            p_generation_type = 'SERVICE_LINES') OR (p_generation_type = 'FULL') THEN
5447             IF l_inflow.start_date IS NULL
5448             THEN
5449               OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
5450                                    p_msg_name     => 'OKL_NO_SLL_SDATE');
5451               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5452             END IF;
5453 
5454         --Added by djanaswa for bug 6007644
5455            IF((l_inflow.periods IS NULL) AND (l_inflow.stub_days IS NOT NULL)) THEN
5456              --Set the recurrence date to null for stub payment
5457              l_recurrence_date := NULL;
5458            ELSIF(l_recurrence_date IS NULL
5459               OR l_old_cle_id <> l_inflow.cle_id
5460               OR l_old_sty_id <> l_inflow.sty_id) THEN
5461              --Set the recurrence date as periodic payment level start date
5462              l_recurrence_date := l_inflow.start_date;
5463            END IF;
5464            l_old_cle_id := l_inflow.cle_id;
5465            l_old_sty_id := l_inflow.sty_id;
5466            --end djanaswa
5467 
5468         /*
5469          * calculate stream elements for each payment level
5470          * also means that if there are multiple payment levels for an asset, streams are
5471          * calculated at different points.
5472          * the streams amounts are they are entered in payments. In case of passthru, a
5473          * new set of streams are created with amounts that are passthru'ed - l_pt_tbl.
5474          */
5475         --Added parameter p_recurrence_date by djanaswa for bug 6007644
5476 
5477          get_stream_elements( p_start_date          =>   l_inflow.start_date,
5478                             p_periods             =>   l_inflow.periods,
5479                             p_frequency           =>   l_inflow.frequency,
5480                             p_structure           =>   l_inflow.structure,
5481                             p_advance_or_arrears  =>   l_inflow.advance_arrears,
5482                             p_amount              =>   l_inflow.amount,
5483                             p_stub_days           =>   l_inflow.stub_days,
5484                             p_stub_amount         =>   l_inflow.stub_amount,
5485                             p_currency_code       =>   l_hdr.currency_code,
5486                             p_khr_id              =>   p_khr_id,
5487                             p_kle_id              =>   l_inflow.cle_id,
5488                             p_purpose_code        =>   l_purpose_code,
5489                             x_selv_tbl            =>   l_selv_tbl,
5490                             x_pt_tbl              =>   l_pt_tbl,
5491                             x_pt_pro_fee_tbl      =>   l_pt_pro_fee_tbl,
5492                             x_return_status       =>   lx_return_status,
5493                             x_msg_count           =>   x_msg_count,
5494                             x_msg_data            =>   x_msg_data,
5495                             p_recurrence_date     =>   l_recurrence_date);
5496 
5497         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5498           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5499         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5500           RAISE OKL_API.G_EXCEPTION_ERROR;
5501         END IF;
5502 
5503         IF ( l_inflow.stub_days IS NOT NULL ) AND ( l_inflow.periods IS NULL )
5504         THEN
5505     	    FOR i in 1..l_selv_tbl.COUNT
5506     	    LOOP
5507     	        l_selv_tbl(i).sel_id := l_se_id;
5508     	    END LOOP;
5509     	    FOR i in 1..l_pt_tbl.COUNT
5510     	    LOOP
5511     	        l_pt_tbl(i).sel_id := l_se_id;
5512 	        l_pt_pro_fee_tbl(i).sel_id := l_se_id;
5513     	    END LOOP;
5514         End If;
5515 
5516         /*
5517         * will get multiple headers for same ( khr_id, kle_id, sty_id ) :-()
5518         */
5519 
5520         get_stream_header(p_khr_id         =>   p_khr_id,
5521                           p_kle_id         =>   l_inflow.cle_id,
5522                           p_sty_id         =>   l_inflow.sty_id,
5523                           p_purpose_code   =>   l_purpose_code,
5524                           x_stmv_rec       =>   l_stmv_rec,
5525                           x_return_status  =>   lx_return_status);
5526 
5527         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5528           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5529         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5530           RAISE OKL_API.G_EXCEPTION_ERROR;
5531         END IF;
5532 
5533         lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
5534                             p_api_version   => g_api_version,
5535                             p_init_msg_list => p_init_msg_list,
5536                             x_msg_count     => x_msg_count,
5537                             x_msg_data      => x_msg_data,
5538                             p_chr_id        => p_khr_id,
5539                             p_selv_tbl      => l_selv_tbl,
5540                             x_selv_tbl      => lx_selv_tbl,
5541                             p_org_id        => G_ORG_ID,
5542                             p_precision     => G_PRECISION,
5543                             p_currency_code => G_CURRENCY_CODE,
5544                             p_rounding_rule => G_ROUNDING_RULE,
5545                             p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
5546 
5547         IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5548             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5549         ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5550             RAISE OKL_API.G_EXCEPTION_ERROR;
5551         END IF;
5552 
5553         l_selv_tbl.DELETE;
5554         l_selv_tbl := lx_selv_tbl;
5555 
5556         --Accumulate Stream Header
5557         OKL_STREAMS_UTIL.accumulate_strm_headers(
5558           p_stmv_rec       => l_stmv_rec,
5559           x_full_stmv_tbl  => l_stmv_tbl,
5560           x_return_status  => lx_return_status );
5561         IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5562             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5563         ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5564             RAISE OKL_API.G_EXCEPTION_ERROR;
5565         END IF;
5566 
5567         --Accumulate Stream Elements
5568         OKL_STREAMS_UTIL.accumulate_strm_elements(
5569           p_stm_index_no  =>  l_stmv_tbl.LAST,
5570           p_selv_tbl       => l_selv_tbl,
5571           x_full_selv_tbl  => l_full_selv_tbl,
5572           x_return_status  => lx_return_status );
5573         IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5574             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5575         ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5576             RAISE OKL_API.G_EXCEPTION_ERROR;
5577         END IF;
5578 /*
5579         okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
5580                                        p_init_msg_list   =>   G_FALSE,
5581                                        x_return_status   =>   lx_return_status,
5582                                        x_msg_count       =>   x_msg_count,
5583                                        x_msg_data        =>   x_msg_data,
5584                                        p_stmv_rec        =>   l_stmv_rec,
5585                                        p_selv_tbl        =>   l_selv_tbl,
5586                                        x_stmv_rec        =>   lx_stmv_rec,
5587                                        x_selv_tbl        =>   lx_selv_tbl);
5588 
5589         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5590           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5591         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5592           RAISE OKL_API.G_EXCEPTION_ERROR;
5593         END IF;
5594 */
5595         IF l_pt_tbl.COUNT > 0 THEN
5596           --Added by mansrini on 30-Jun-2005 for generating pass through accrual streams for service lines
5597           --Bug 4434343 - Start of Changes
5598           IF (l_inflow.lty_code = 'FEE') OR (l_inflow.lty_code = 'LINK_FEE_ASSET')  THEN
5599             OKL_ISG_UTILS_PVT.get_dependent_stream_type(
5600                                    p_khr_id                => p_khr_id,
5601                                          p_deal_type             => l_deal_type,
5602                                    p_primary_sty_id        => l_inflow.sty_id,
5603                                    p_dependent_sty_purpose => 'PASS_THRU_EXP_ACCRUAL',
5604                                    x_return_status         => lx_return_status,
5605                                    x_dependent_sty_id      => l_passthrough_id,
5606                                    x_dependent_sty_name    => l_sty_name);
5607             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5608               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5609             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5610               RAISE OKL_API.G_EXCEPTION_ERROR;
5611             END IF;
5612          ELSIF (l_inflow.lty_code = 'SOLD_SERVICE') OR (l_inflow.lty_code = 'LINK_SERV_ASSET') THEN
5613             OKL_ISG_UTILS_PVT.get_dependent_stream_type(
5614                                    p_khr_id                => p_khr_id,
5615                                          p_deal_type             => l_deal_type,
5616                                    p_primary_sty_id        => l_inflow.sty_id,
5617                                    p_dependent_sty_purpose => 'PASS_THRU_SVC_EXP_ACCRUAL',
5618                                    x_return_status         => lx_return_status,
5619                                    x_dependent_sty_id      => l_passthrough_id,
5620                                    x_dependent_sty_name    => l_sty_name);
5621             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5622               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5623             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5624               RAISE OKL_API.G_EXCEPTION_ERROR;
5625             END IF;
5626          END IF;
5627          --Bug 4434343 - End of Changes
5628 
5629 
5630             If l_passthrough_id IS NOT NULL then
5631 
5632                 get_stream_header(p_khr_id         =>   p_khr_id,
5633                                 p_kle_id         =>   l_inflow.cle_id,
5634                                 p_sty_id         =>   l_passthrough_id,
5635                                 p_purpose_code   =>   l_purpose_code,
5636                                 x_stmv_rec       =>   l_pt_rec,
5637                                 x_return_status  =>   lx_return_status);
5638 
5639                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5640                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5641                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5642                     RAISE OKL_API.G_EXCEPTION_ERROR;
5643                 END IF;
5644 
5645 
5646                 lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
5647                                   p_api_version   => g_api_version,
5648                                   p_init_msg_list => p_init_msg_list,
5649                                   x_msg_count     => x_msg_count,
5650                                   x_msg_data      => x_msg_data,
5651                                   p_chr_id        => p_khr_id,
5652                                   p_selv_tbl      => l_pt_tbl,
5653                                   x_selv_tbl      => lx_selv_tbl,
5654                                   p_org_id        => G_ORG_ID,
5655                                   p_precision     => G_PRECISION,
5656                                   p_currency_code => G_CURRENCY_CODE,
5657                                   p_rounding_rule => G_ROUNDING_RULE,
5658                                   p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
5659 
5660                 IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5661                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5662                 ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5663                     RAISE OKL_API.G_EXCEPTION_ERROR;
5664                 END IF;
5665 
5666                 l_pt_tbl.DELETE;
5667                 l_pt_tbl := lx_selv_tbl;
5668                --Accumulate Stream Header: 4346646
5669                 OKL_STREAMS_UTIL.accumulate_strm_headers(
5670                   p_stmv_rec       => l_pt_rec,
5671                   x_full_stmv_tbl  => l_stmv_tbl,
5672                   x_return_status  => lx_return_status );
5673                 IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5674                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5675                 ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5676                     RAISE OKL_API.G_EXCEPTION_ERROR;
5677                 END IF;
5678 
5679                 --Accumulate Stream Elements
5680                 OKL_STREAMS_UTIL.accumulate_strm_elements(
5681                   p_stm_index_no  =>  l_stmv_tbl.LAST,
5682                   p_selv_tbl       => l_pt_tbl,
5683                   x_full_selv_tbl  => l_full_selv_tbl,
5684                   x_return_status  => lx_return_status );
5685                 IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5686                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5687                 ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5688                     RAISE OKL_API.G_EXCEPTION_ERROR;
5689                 END IF;
5690 /*
5691                okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
5692                                              p_init_msg_list   =>   G_FALSE,
5693                                              x_return_status   =>   lx_return_status,
5694                                              x_msg_count       =>   x_msg_count,
5695                                              x_msg_data        =>   x_msg_data,
5696                                              p_stmv_rec        =>   l_pt_rec,
5697                                              p_selv_tbl        =>   l_pt_tbl,
5698                                              x_stmv_rec        =>   lx_stmv_rec,
5699                                              x_selv_tbl        =>   lx_selv_tbl);
5700 
5701                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5702                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5703                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5704                     RAISE OKL_API.G_EXCEPTION_ERROR;
5705                 END IF;
5706 */
5707             End If;
5708         END IF;
5709 
5710         IF l_pt_pro_fee_tbl.COUNT > 0 THEN
5711 
5712           IF l_passthrough_pro_fee_id IS NULL THEN
5713 
5714              If ( l_inflow.lty_code = 'LINK_FEE_ASSET') Then
5715 
5716 	       OPEN top_svc_csr( p_khr_id, l_inflow.cle_id );
5717                FETCH top_svc_csr INTO top_svc_rec;
5718                CLOSE top_svc_csr;
5719 
5720                OPEN  fee_strm_type_csr  ( top_svc_rec.top_svc_id, 'FEE' );
5721                FETCH fee_strm_type_csr into fee_strm_type_rec;
5722                CLOSE fee_strm_type_csr;
5723 
5724 	     Else
5725 
5726                OPEN  fee_strm_type_csr  ( l_inflow.cle_id, 'FEE' );
5727                FETCH fee_strm_type_csr into fee_strm_type_rec;
5728                CLOSE fee_strm_type_csr;
5729 
5730              End If;
5731 
5732              OKL_ISG_UTILS_PVT.get_dependent_stream_type(
5733                                          p_khr_id                => p_khr_id,
5734 					 p_deal_type             => l_deal_type,
5735                                          p_primary_sty_id        => fee_strm_type_rec.styp_id,
5736                                          p_dependent_sty_purpose => 'PASS_THRU_PRO_FEE_ACCRUAL',
5737                                          x_return_status         => lx_return_status,
5738                                          x_dependent_sty_id      => l_passthrough_pro_fee_id,
5739                                          x_dependent_sty_name    => l_sty_name);
5740 
5741             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5742               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5743             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5744               RAISE OKL_API.G_EXCEPTION_ERROR;
5745             END IF;
5746 
5747           END IF;
5748 
5749         If l_passthrough_pro_fee_id IS NOT NULL then
5750 
5751           get_stream_header(p_khr_id         =>   p_khr_id,
5752                             p_kle_id         =>   l_inflow.cle_id,
5753                             p_sty_id         =>   l_passthrough_pro_fee_id,
5754                             p_purpose_code   =>   l_purpose_code,
5755                             x_stmv_rec       =>   l_pt_pro_fee_rec,
5756                             x_return_status  =>   lx_return_status);
5757 
5758           IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5759             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5760           ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5761             RAISE OKL_API.G_EXCEPTION_ERROR;
5762           END IF;
5763 
5764 
5765                lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
5766                                   p_api_version   => g_api_version,
5767                                   p_init_msg_list => p_init_msg_list,
5768                                   x_msg_count     => x_msg_count,
5769                                   x_msg_data      => x_msg_data,
5770                                   p_chr_id        => p_khr_id,
5771                                   p_selv_tbl      => l_pt_pro_fee_tbl,
5772                                   x_selv_tbl      => lx_selv_tbl,
5773                                   p_org_id        => G_ORG_ID,
5774                                   p_precision     => G_PRECISION,
5775                                   p_currency_code => G_CURRENCY_CODE,
5776                                   p_rounding_rule => G_ROUNDING_RULE,
5777                                   p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
5778 
5779           IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5780               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5781           ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5782               RAISE OKL_API.G_EXCEPTION_ERROR;
5783           END IF;
5784 
5785           l_pt_pro_fee_tbl.DELETE;
5786           l_pt_pro_fee_tbl := lx_selv_tbl;
5787 
5788                 OKL_STREAMS_UTIL.accumulate_strm_headers(
5789                   p_stmv_rec       => l_pt_pro_fee_rec,
5790                   x_full_stmv_tbl  => l_stmv_tbl,
5791                   x_return_status  => lx_return_status );
5792 
5793                 IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
5794                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5795                 ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
5796                     RAISE OKL_API.G_EXCEPTION_ERROR;
5797                 END IF;
5798 
5799                 --Accumulate Stream Elements
5800                 OKL_STREAMS_UTIL.accumulate_strm_elements(
5801                   p_stm_index_no  =>  l_stmv_tbl.LAST,
5802                   p_selv_tbl       => l_pt_pro_fee_tbl,
5803                   x_full_selv_tbl  => l_full_selv_tbl,
5804                   x_return_status  => lx_return_status );
5805 
5806           IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5807             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5808           ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5809             RAISE OKL_API.G_EXCEPTION_ERROR;
5810           END IF;
5811 
5812 	 End If;
5813 
5814         END IF;
5815 
5816 
5817         -- Clear out reusable data structures
5818 
5819         l_sty_name  :=  NULL;
5820         l_sty_id    :=  NULL;
5821 
5822         l_stmv_rec := NULL;
5823         l_pt_rec := NULL;
5824         l_selv_tbl.delete;
5825         l_pt_tbl.delete;
5826 
5827         l_pt_pro_fee_rec := NULL;
5828         l_pt_pro_fee_tbl.delete;
5829 
5830 
5831         lx_stmv_rec := NULL;
5832         lx_selv_tbl.delete;
5833 
5834         x_payment_count  :=  x_payment_count + 1;
5835     END IF;
5836     END LOOP;
5837     --Create all the accumulated Streams at one shot ..
5838 Okl_Streams_Pub.create_streams_perf(
5839                     p_api_version,
5840                     p_init_msg_list,
5841                     lx_return_status,
5842                     x_msg_count,
5843                     x_msg_data,
5844                     l_stmv_tbl,
5845                     l_full_selv_tbl,
5846                     lx_stmv_tbl,
5847                     lx_full_selv_tbl);
5848     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5849       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5850     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
5851       RAISE OKL_API.G_EXCEPTION_ERROR;
5852     END IF;
5853 
5854   EXCEPTION
5855 
5856     WHEN OKL_API.G_EXCEPTION_ERROR THEN
5857 
5858       x_return_status := G_RET_STS_ERROR;
5859 
5860     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5861 
5862       x_return_status := G_RET_STS_UNEXP_ERROR;
5863 
5864     WHEN OTHERS THEN
5865 
5866       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
5867                            p_msg_name     => G_DB_ERROR,
5868                            p_token1       => G_PROG_NAME_TOKEN,
5869                            p_token1_value => l_prog_name,
5870                            p_token2       => G_SQLCODE_TOKEN,
5871                            p_token2_value => sqlcode,
5872                            p_token3       => G_SQLERRM_TOKEN,
5873                            p_token3_value => sqlerrm);
5874 
5875       x_return_status := G_RET_STS_UNEXP_ERROR;
5876 
5877   END generate_cash_flows;
5878 
5879   -- Added by rgooty for bug 9305846
5880   ---------------------------------------------------------------------------
5881   -- PROCEDURE gen_rental_accr_streams
5882   ----This procedure generates Rental Accrual Streams------------------------
5883   ---------------------------------------------------------------------------
5884   PROCEDURE gen_rental_accr_streams (p_api_version        IN    NUMBER,
5885                                      p_init_msg_list      IN    VARCHAR2,
5886                                      p_khr_id             IN    NUMBER,
5887                                      p_purpose_code       IN    VARCHAR2,
5888                                      x_return_status      OUT   NOCOPY VARCHAR2,
5889                                      x_msg_count          OUT   NOCOPY NUMBER,
5890                                      x_msg_data           OUT   NOCOPY VARCHAR2) IS
5891 
5892     --To get all the assets for the contract
5893     CURSOR c_assets IS
5894       SELECT cle.id cle_id,
5895              cle.start_date start_date,
5896              NVL(cle.date_terminated, cle.end_date) end_date
5897       FROM   okc_k_lines_b cle,
5898              okc_line_styles_b lse
5899       WHERE  cle.dnz_chr_id = p_khr_id
5900         AND  cle.sts_code IN ('PASSED', 'COMPLETE')
5901              -- Removing 'TERMINATED' Status Lines as Partial Terminated Asset Accruals
5902              -- will be copied over any way.
5903         AND  cle.lse_id = lse.id
5904         AND  lse.lty_code = 'FREE_FORM1';
5905 
5906     --To get payment information for antr asset line
5907     CURSOR c_inflows(p_cle_id  NUMBER)
5908         IS
5909       SELECT TO_NUMBER(rul1.object1_id1) sty_id,
5910              TO_NUMBER(rul2.rule_information3) periods,
5911              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
5912              TO_NUMBER(rul2.rule_information7) stub_days,
5913              TO_NUMBER(rul2.rule_information8) stub_amount
5914         FROM okc_rule_groups_b rgp,
5915              okc_rules_b rul1,
5916              okc_rules_b rul2,
5917              okc_k_lines_b cle,
5918              okl_strm_type_b sty
5919        WHERE rul2.dnz_chr_id = p_khr_id
5920          AND rul2.rule_information_category = 'LASLL'
5921          AND rul2.rgp_id = rgp.id
5922          AND TO_NUMBER(rul2.object2_id1) = rul1.id
5923          AND rgp.cle_id = cle.id
5924          AND cle.id = p_cle_id
5925          -- 12944979: For Terminated lines, copy_accrual_streams_isg will syncup
5926          AND cle.sts_code IN ('PASSED', 'COMPLETE')
5927          AND sty.id = rul1.object1_id1
5928          AND sty.stream_type_purpose = 'RENT';
5929 
5930     l_prog_name   CONSTANT  VARCHAR2(100) := G_PKG_NAME||'.'||'gen_rental_accr_streams';
5931     l_sum_of_rents          NUMBER := 0;
5932     l_currency_code         OKC_K_HEADERS_B.CURRENCY_CODE%TYPE;
5933     l_day_convention_month  VARCHAR2(30);
5934     l_day_convention_year   VARCHAR2(30);
5935     l_end_date              DATE;
5936     l_total_days            NUMBER;
5937     l_daily_amt             NUMBER;
5938     l_start_date            DATE;
5939     l_month_end             DATE;
5940     l_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
5941     l_stmv_rec              Okl_Streams_Pub.stmv_rec_type;
5942     l_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
5943     l_selv_tbl              Okl_Streams_Pub.selv_tbl_type;
5944     lx_selv_tbl             Okl_Streams_Pub.selv_tbl_type;
5945     lx_stmv_tbl             Okl_Streams_Pub.stmv_tbl_type;
5946     lx_full_selv_tbl        Okl_Streams_Pub.selv_tbl_type;
5947     l_dep_sty_id            NUMBER;
5948     p_get_k_info_csr        OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR%ROWTYPE;
5949     l_mapped_sty_name       VARCHAR2(150);
5950     l_days                  NUMBER;
5951     i                       BINARY_INTEGER := 0;
5952     l_start_day             NUMBER;
5953     l_end_day               NUMBER;
5954 
5955   BEGIN
5956 
5957     --Get the day convention
5958     OKL_PRICING_UTILS_PVT.get_day_convention(
5959          p_id              => p_khr_id,
5960          p_source          => 'ISG',
5961          x_days_in_month   => l_day_convention_month,
5962          x_days_in_year    => l_day_convention_year,
5963          x_return_status   => x_return_status);
5964     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5965        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5966     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
5967        RAISE OKL_API.G_EXCEPTION_ERROR;
5968     END IF;
5969 
5970     OPEN OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR (p_khr_id);
5971     FETCH OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR INTO p_get_k_info_csr;
5972     CLOSE OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR;
5973 
5974     FOR l_asset_rec IN c_assets LOOP
5975        l_sum_of_rents := 0;
5976        FOR l_inflow IN c_inflows(l_asset_rec.cle_id) LOOP
5977 
5978          IF(l_mapped_sty_name IS NULL) THEN
5979             OKL_ISG_UTILS_PVT.get_dep_stream_type(
5980                    p_khr_id                => p_khr_id,
5981                    p_deal_type             => 'LEASEOP',
5982                    p_primary_sty_id        => l_inflow.sty_id,
5983                    p_dependent_sty_purpose => 'RENT_ACCRUAL',
5984                    x_return_status         => x_return_status,
5985                    x_dependent_sty_id      => l_dep_sty_id,
5986                    x_dependent_sty_name    => l_mapped_sty_name,
5987                    p_get_k_info_rec        => p_get_k_info_csr);
5988             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
5989               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5990             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
5991               RAISE OKL_API.G_EXCEPTION_ERROR;
5992             END IF;
5993 
5994             --If Rental Accrual stream is not present in the SGT, then exit the procedure
5995             IF(l_mapped_sty_name IS NULL) THEN
5996               EXIT;
5997             END IF;
5998          END IF;
5999 
6000          IF (l_inflow.periods IS NOT NULL) AND (l_inflow.amount IS NOT NULL) THEN
6001             l_sum_of_rents := l_sum_of_rents + (l_inflow.periods * l_inflow.amount);
6002          ELSE
6003             l_sum_of_rents := l_sum_of_rents + l_inflow.stub_amount;
6004          END IF;
6005        END LOOP;
6006 
6007        l_start_date  :=  l_asset_rec.start_date;
6008        l_end_date    :=  l_asset_rec.end_date;
6009        l_month_end   :=  LAST_DAY(l_start_date);
6010 
6011        l_total_days  := OKL_PRICING_UTILS_PVT.get_day_count(
6012                             p_start_date    => l_start_date,
6013                             p_days_in_month => l_day_convention_month,
6014                             p_days_in_year  => l_day_convention_year,
6015                             p_end_date      => l_end_date,
6016                             p_arrears       => 'Y',
6017                             x_return_status => x_return_status);
6018        IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
6019           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6020        ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
6021           RAISE OKL_API.G_EXCEPTION_ERROR;
6022        END IF;
6023 
6024        IF(l_day_convention_month = '30' AND l_day_convention_year = '360') THEN
6025          l_start_day := to_char(l_start_date, 'DD' );
6026          l_end_day   := to_char(l_end_date, 'DD' );
6027          IF(l_start_day = l_end_day) THEN
6028             l_total_days := l_total_days + 1;
6029          END IF;
6030        END IF;
6031 
6032        l_daily_amt  :=  l_sum_of_rents / l_total_days;
6033 
6034        l_stmv_rec := NULL;
6035        l_selv_tbl.delete;
6036        i := 0;
6037 
6038        LOOP
6039          i := i + 1;
6040          IF TO_CHAR(l_month_end, 'MON') IN ('JAN', 'MAR', 'MAY', 'JUL', 'AUG', 'OCT', 'DEC') THEN
6041            l_selv_tbl(i).stream_element_date := l_month_end - 1;
6042          ELSE
6043            l_selv_tbl(i).stream_element_date := l_month_end;
6044          END IF;
6045 
6046          l_selv_tbl(i).se_line_number :=  i;
6047 
6048          IF l_month_end > l_end_date THEN
6049            l_month_end := l_end_date;
6050          END IF;
6051 
6052          l_days := OKL_PRICING_UTILS_PVT.get_day_count(
6053                         p_start_date    => l_start_date,
6054                         p_days_in_month => l_day_convention_month,
6055                         p_days_in_year  => l_day_convention_year,
6056                         p_end_date      => l_month_end,
6057                         p_arrears       => 'Y',
6058                         x_return_status => x_return_status);
6059          IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
6060            RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6061          ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
6062            RAISE OKL_API.G_EXCEPTION_ERROR;
6063          END IF;
6064 
6065          l_selv_tbl(i).amount := l_days * l_daily_amt;
6066 
6067          IF l_month_end >= l_end_date THEN
6068            EXIT;
6069          END IF;
6070 
6071          l_start_date   := LAST_DAY(l_start_date) + 1;
6072          l_month_end    := ADD_MONTHS(l_month_end, 1);
6073        END LOOP;
6074 
6075        IF (l_selv_tbl.COUNT > 0 ) THEN
6076           x_return_status := Okl_Streams_Util.round_streams_amount_esg(
6077                                  p_api_version   => g_api_version,
6078                                  p_init_msg_list => p_init_msg_list,
6079                                  x_msg_count     => x_msg_count,
6080                                  x_msg_data      => x_msg_data,
6081                                  p_chr_id        => p_khr_id,
6082                                  p_selv_tbl      => l_selv_tbl,
6083                                  x_selv_tbl      => lx_selv_tbl,
6084                                  p_org_id        => G_ORG_ID,
6085                                  p_precision     => G_PRECISION,
6086                                  p_currency_code => G_CURRENCY_CODE,
6087                                  p_rounding_rule => G_ROUNDING_RULE,
6088                                  p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
6089 
6090           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
6091              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6092           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
6093              RAISE OKL_API.G_EXCEPTION_ERROR;
6094           END IF;
6095 
6096           l_selv_tbl.DELETE;
6097           l_selv_tbl := lx_selv_tbl;
6098 
6099           get_stream_header(p_khr_id         =>   p_khr_id,
6100                             p_kle_id         =>   l_asset_rec.cle_id,
6101                             p_sty_id         =>   l_dep_sty_id,
6102                             p_purpose_code   =>   p_purpose_code,
6103                             x_stmv_rec       =>   l_stmv_rec,
6104                             x_return_status  =>   x_return_status);
6105           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
6106              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6107           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
6108              RAISE OKL_API.G_EXCEPTION_ERROR;
6109           END IF;
6110 
6111           --Accumulate Stream Header
6112           OKL_STREAMS_UTIL.accumulate_strm_headers(
6113                 p_stmv_rec       => l_stmv_rec,
6114                 x_full_stmv_tbl  => l_stmv_tbl,
6115                 x_return_status  => x_return_status );
6116           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
6117              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6118           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
6119              RAISE OKL_API.G_EXCEPTION_ERROR;
6120           END IF;
6121 
6122           --Accumulate Stream Elements
6123           OKL_STREAMS_UTIL.accumulate_strm_elements(
6124                 p_stm_index_no  =>  l_stmv_tbl.LAST,
6125                 p_selv_tbl       => l_selv_tbl,
6126                 x_full_selv_tbl  => l_full_selv_tbl,
6127                 x_return_status  => x_return_status );
6128           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
6129              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6130           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
6131              RAISE OKL_API.G_EXCEPTION_ERROR;
6132           END IF;
6133        END IF;
6134 
6135     END LOOP; --Assets loop
6136 
6137     --Create all the accumulated Streams
6138     IF l_stmv_tbl.COUNT > 0 AND
6139        l_full_selv_tbl.COUNT > 0
6140     THEN
6141       Okl_Streams_Pub.create_streams_perf(
6142                                p_api_version,
6143                                p_init_msg_list,
6144                                x_return_status,
6145                                x_msg_count,
6146                                x_msg_data,
6147                                l_stmv_tbl,
6148                                l_full_selv_tbl,
6149                                lx_stmv_tbl,
6150                                lx_full_selv_tbl);
6151       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
6152         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6153       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
6154         RAISE OKL_API.G_EXCEPTION_ERROR;
6155       END IF;
6156     END IF;
6157 
6158     x_return_status := G_RET_STS_SUCCESS;
6159 
6160   EXCEPTION
6161     WHEN OKL_API.G_EXCEPTION_ERROR THEN
6162       x_return_status := G_RET_STS_ERROR;
6163     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6164       x_return_status := G_RET_STS_UNEXP_ERROR;
6165     WHEN OTHERS THEN
6166       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
6167                            p_msg_name     => G_DB_ERROR,
6168                            p_token1       => G_PROG_NAME_TOKEN,
6169                            p_token1_value => l_prog_name,
6170                            p_token2       => G_SQLCODE_TOKEN,
6171                            p_token2_value => sqlcode,
6172                            p_token3       => G_SQLERRM_TOKEN,
6173                            p_token3_value => sqlerrm);
6174       x_return_status := G_RET_STS_UNEXP_ERROR;
6175   END gen_rental_accr_streams;
6176   -- end rgooty for bug 9305846
6177 
6178   -- rgooty - Bug 12944979 - Added - Start
6179   -- Start of comments
6180   -- Procedure Name     : COPY_ACCRUAL_STREAMS_ISG
6181   -- Description        : Creates Working copy of the CURRENT streams of Accrual Streams, only for
6182   --                      TERMINATED LINES.
6183   -- Business Rules     : During partial termination, mass rebook re-generates streams of terminated lines.
6184   --                      This process has been modified not to generate streams for
6185   --                      Accrual Streams of Terminated Lines. Instead the current streams will be copied over.
6186   -- Parameters         : p_chr_id      - Contract Identifier
6187   --                      p_source_call - defaulted to ISG
6188   -- Version            : 1.0 - Introduced as part of Bug# 12944979
6189   --
6190   -- End of comments
6191   PROCEDURE COPY_ACCRUAL_STREAMS_ISG(
6192             p_api_version     IN  NUMBER,
6193             p_init_msg_list   IN  VARCHAR2 DEFAULT OKL_API.G_FALSE,
6194             x_return_status   OUT NOCOPY VARCHAR2,
6195             x_msg_count       OUT NOCOPY NUMBER,
6196             x_msg_data        OUT NOCOPY VARCHAR2,
6197             p_chr_id          IN  VARCHAR2,
6198             p_purpose_code    IN  VARCHAR2,
6199             p_source_call     IN  VARCHAR2 default 'ISG') AS
6200 
6201     -- Declare Variables
6202     l_api_name                CONSTANT VARCHAR2(30) := 'COPY_ACCRUAL_STREAMS_ISG';
6203     l_strm_index              NUMBER;
6204     l_sel_index               NUMBER;
6205     l_prev_stm_id             OKL_STREAMS.ID%TYPE;
6206     l_orig_chr_id             OKC_K_HEADERS_B.ID%TYPE;
6207     l_new_stm_cle_id          OKC_K_LINES_B.ID%TYPE;
6208 
6209     -- Declare records/table types
6210     l_stmv_tbl          okl_streams_pub.stmv_tbl_type;
6211     lx_stmv_tbl         okl_streams_pub.stmv_tbl_type;
6212     l_selv_tbl          okl_streams_pub.selv_tbl_type;
6213     lx_selv_tbl         okl_streams_pub.selv_tbl_type;
6214 
6215      -- Cursor to fetch the terminated lines
6216      CURSOR l_get_curr_streams_lines(cp_chr_id IN NUMBER )
6217      IS
6218        -- Query the assets that got terminated
6219        SELECT 1                        order_sequence
6220             , lse.lty_code             line_type -- 'FREE_FORM1'
6221             , cle.id                   kle_id
6222             , NULL                     fee_id
6223             , cle.date_terminated      date_terminated
6224          FROM okc_k_lines_b     cle
6225             , okc_line_styles_b lse
6226             , okc_statuses_b    sts
6227         WHERE cle.dnz_chr_id = cp_chr_id
6228           AND lse.id         = cle.lse_id
6229           AND lse.lty_code   = 'FREE_FORM1'
6230           AND sts.code       = cle.sts_code
6231           AND sts.ste_code   = 'TERMINATED'
6232       UNION
6233       -- Query the Link Fee Asset Lines that got terminated
6234       SELECT 2                         order_sequence
6235            , lnk_lse.lty_code          line_type  -- 'LINK_FEE_ASSET'
6236            , lnk_cle.id                kle_id
6237            , fee_cle.id                fee_id
6238            , lnk_cle.date_terminated   date_terminated
6239         FROM okc_k_lines_b     lnk_cle
6240            , okl_k_lines       fee_kle
6241            , okc_k_lines_b     fee_cle
6242            , okc_line_styles_b lnk_lse
6243            , okc_statuses_b    sts
6244        WHERE lnk_cle.dnz_chr_id = cp_chr_id
6245          AND fee_cle.dnz_chr_id = cp_chr_id
6246          AND lnk_cle.cle_id     = fee_cle.id
6247          AND lnk_lse.id         = lnk_cle.lse_id
6248          AND lnk_lse.lty_code   = 'LINK_FEE_ASSET'
6249          AND sts.code           = lnk_cle.sts_code
6250          AND sts.ste_code       = 'TERMINATED'
6251          AND fee_kle.id         = FEE_CLE.ID
6252       UNION
6253       SELECT DISTINCT
6254               3                        order_sequence
6255             , lse1.lty_code            line_type
6256             , cle1.id                  kle_id
6257             , NULL                     fee_id
6258             , cle1.date_terminated     date_terminated
6259         FROM  okl_streams              stm
6260             , okc_k_lines_b            cle1
6261             , okc_line_styles_b        lse1
6262             , okc_statuses_b           sts1
6263        WHERE stm.khr_id         = cp_chr_id
6264          AND stm.say_code       = 'CURR'
6265          AND stm.kle_id         = cle1.id
6266          AND lse1.id            = cle1.lse_id
6267          AND lse1.lty_code      NOT IN ( 'FREE_FORM1' , 'LINK_FEE_ASSET' )
6268          AND sts1.code          = cle1.sts_code
6269          AND sts1.ste_code      = 'TERMINATED'
6270       ORDER BY order_sequence, line_type, kle_id;
6271 
6272    -- Modified by bkatraga for bug 5704245. Added bind condition for purpose_code
6273    -- Cursor to get Current Streams and elements for a particular line of a contract
6274    -- Added ISG generated stream type purposes so that these streams will not be
6275    -- copied over during mass/online rebook. Streams will be regenerated by ISG
6276    CURSOR c_streams (cp_chr_id OKC_K_HEADERS_B.ID%TYPE
6277                    , cp_cle_id OKC_K_LINES_B.ID%TYPE
6278                    , cp_date_terminated DATE
6279                    , cp_purpose_code  okl_streams.purpose_code%TYPE) IS
6280      SELECT STM.ID STREAM_ID
6281           , STM.STY_ID
6282           , STM.SGN_CODE
6283           , STM.PURPOSE_CODE
6284           , STM.ACTIVE_YN
6285           , SEL.STREAM_ELEMENT_DATE
6286           , SEL.AMOUNT
6287           , SEL.SE_LINE_NUMBER
6288           , SEL.COMMENTS
6289           , SEL.ACCRUED_YN
6290           , STM.TRANSACTION_NUMBER
6291           , STY.STREAM_TYPE_PURPOSE
6292        FROM OKL_STREAMS STM
6293           , OKL_STRM_ELEMENTS SEL
6294           , OKL_STRM_TYPE_B STY
6295       WHERE SEL.STM_ID = STM.ID
6296         AND STM.KHR_ID = cp_chr_id
6297         AND STM.KLE_ID = cp_cle_id
6298         AND DECODE(STM.PURPOSE_CODE, NULL, '-99', 'REPORT' ) = cp_purpose_code
6299         AND STM.SAY_CODE = 'CURR'
6300         AND SEL.STREAM_ELEMENT_DATE <= nvl(cp_date_terminated, SEL.STREAM_ELEMENT_DATE)
6301         AND STM.SGN_CODE <> 'INTC'
6302         AND STM.STY_ID = STY.ID
6303         AND STY.stream_type_purpose IN
6304               -- List of the Accrual Streams identified
6305               (
6306                  'ACCRUED_FEE_EXPENSE'
6307                 ,'ACCRUED_FEE_INCOME'
6308                 ,'ACTUAL_INCOME_ACCRUAL'
6309                 ,'EXPENSE'
6310                 ,'INTEREST_INCOME'
6311                 ,'LEASE_INCOME'
6312                 ,'PASS_THRU_EXP_ACCRUAL'
6313                 ,'PASS_THRU_REV_ACCRUAL'
6314                 ,'PASS_THRU_SVC_EXP_ACCRUAL'
6315                 ,'PASS_THRU_SVC_REV_ACCRUAL'
6316                 ,'PROCESSING_FEE_ACCRUAL'
6317                 ,'RENT_ACCRUAL'
6318                 ,'SERVICE_EXPENSE'
6319                 ,'SERVICE_INCOME'
6320                 -- Apart from copying accruals, we are copying the following stream with the purpose
6321                 -- as we have modified the generate_streams api code, not to call get_loan_amortization
6322                 -- Hence, we will be loosing out all these streams
6323                 ,'INTEREST_PAYMENT'
6324                 ,'PRINCIPAL_PAYMENT'
6325                 ,'PRINCIPAL_BALANCE'
6326                 ,'TERMINATION_VALUE'
6327               )
6328         AND STM.SGN_CODE  NOT IN ('INTC','LATE_CALC')
6329        ORDER BY STM.ID, SEL.SE_LINE_NUMBER  -- to get the stream elements of a stream together
6330        ;
6331 
6332      streams_rec              c_streams%ROWTYPE;
6333 
6334      --Cursor to check whether it is a partial termination mass rebook transaction
6335      CURSOR  l_chk_mass_rbk_csr IS
6336        SELECT '!'
6337          FROM  okc_k_headers_b CHR,
6338                okl_trx_contracts ktrx,
6339                okl_trx_contracts trx
6340         WHERE  CHR.ID         = p_chr_id
6341           AND  ktrx.KHR_ID    =  chr.id
6342           AND  ktrx.tsu_code  = 'ENTERED'
6343           AND  ktrx.rbr_code  IS NOT NULL
6344           AND  ktrx.tcn_type  = 'TRBK'
6345           AND  ktrx.representation_type = 'PRIMARY'
6346           AND  ktrx.source_trx_type  = 'TCN'
6347           AND  trx.tcn_type   = 'ALT'
6348           AND  trx.id         = ktrx.source_trx_id
6349           AND  EXISTS (SELECT '1'
6350                          FROM  okl_rbk_selected_contract rbk_khr
6351                         WHERE  rbk_khr.KHR_ID = chr.id
6352                           AND  rbk_khr.STATUS <> 'PROCESSED');
6353 
6354      l_mass_rbk_khr   VARCHAR2(1) DEFAULT '?';
6355      l_terminated_date DATE;
6356 
6357      -- Get Line ID in rebook copy that was copied from LINE the original contract
6358      -- cp_rbk_chr_id is the rebook copy contract
6359      -- cp_ole_id is original contract line ID
6360      CURSOR c_get_orig_line_id (cp_rbk_chr_id IN okc_k_headers_b.id%type
6361                               , cp_ole_id IN OKC_K_LINES_B.ID%TYPE) is
6362        SELECT cle.id new_cle_id
6363         FROM okc_k_lines_b cle
6364             ,okc_k_lines_b ole
6365         WHERE cle.dnz_chr_id=cp_rbk_chr_id
6366           and ole.id = cp_ole_id
6367           and ole.lse_id = cle.lse_id
6368           and ole.id=cle.orig_system_id1;
6369 
6370      CURSOR c_chk_accrual_sty_on_pdt(cp_pdt_id OKL_PRODUCTS.ID%TYPE,
6371                                      cp_sty_id OKL_STRM_TYPE_B.ID%TYPE) IS
6372      SELECT 'Y'
6373        FROM OKL_STRM_TYPE_B sty,
6374             OKL_PROD_STRM_TYPES psty
6375       WHERE sty.id = psty.sty_id
6376         AND psty.pdt_id = cp_pdt_id
6377         AND psty.accrual_yn = 'Y'
6378         AND psty.sty_id=cp_sty_id;
6379 
6380      --To get the regular product id
6381      CURSOR c_get_regular_product_id IS
6382      SELECT pdt_id
6383        FROM okl_k_headers
6384       WHERE id = p_chr_id;
6385 
6386      --To get the reporting product id
6387      CURSOR c_get_report_product_id IS
6388      SELECT pdt.reporting_pdt_id
6389        FROM okl_k_headers khr,
6390             okl_products pdt
6391       WHERE khr.id = p_chr_id
6392         AND khr.pdt_id = pdt.id;
6393 
6394      -- Cursor to identify the Stream ID corresponding to the one that
6395      -- of the Stream from the Original copy of the contract.
6396      CURSOR  c_identify_rbk_stm_csr(
6397                  cp_chr_id       IN NUMBER
6398                 ,cp_kle_id       IN NUMBER
6399                 ,cp_sty_id       IN NUMBER
6400                 ,cp_sgn_code     IN VARCHAR2
6401                 ,cp_purpose_code IN VARCHAR2
6402                 ,cp_say_code     IN VARCHAR2
6403 --                ,cp_active_yn    IN VARCHAR2
6404              )
6405      IS
6406        SELECT  stm.id                      stm_id
6407               ,stm.transaction_number      transaction_number
6408          FROM  okl_streams   stm
6409         WHERE  stm.khr_id                 = cp_chr_id
6410           AND  stm.kle_id                 = cp_kle_id
6411           AND  stm.sty_id                 = cp_sty_id
6412           AND  stm.sgn_code               = cp_sgn_code
6413           AND  NVL(stm.purpose_code,-99)  = NVL(cp_purpose_code,-99)
6414           AND  stm.say_code               = cp_say_code;
6415 
6416      --To check whether the accrual flag is checked or not
6417      CURSOR check_accrual_previous_csr IS
6418      SELECT NVL(CHK_ACCRUAL_PREVIOUS_MNTH_YN,'N')
6419        FROM OKL_SYSTEM_PARAMS;
6420 
6421      CURSOR chk_curr_terminated_line(p_kle_id IN OKC_K_LINES_B.ID%TYPE)
6422      IS
6423        SELECT 'Y'
6424          FROM okc_k_headers_b CHR,
6425               okl_trx_contracts ktrx,
6426               okl_trx_contracts trx,
6427               okl_txl_quote_lines_b txl
6428         WHERE CHR.ID         = p_chr_id
6429           AND ktrx.KHR_ID    =  chr.id
6430           AND ktrx.tsu_code  = 'ENTERED'
6431           AND ktrx.rbr_code  IS NOT NULL
6432           AND ktrx.tcn_type  = 'TRBK'
6433           AND ktrx.representation_type = 'PRIMARY'
6434           AND ktrx.source_trx_type  = 'TCN'
6435           AND trx.tcn_type   = 'ALT'
6436           AND trx.id         = ktrx.source_trx_id
6437           AND TXL.QTE_ID = TRX.QTE_ID
6438           AND TXL.qlt_code = 'AMCFIA'
6439           AND TXL.kle_id = p_kle_id
6440           AND EXISTS (SELECT '1'
6441                         FROM  okl_rbk_selected_contract rbk_khr
6442                        WHERE  rbk_khr.KHR_ID = chr.id
6443                          AND rbk_khr.STATUS <> 'PROCESSED')
6444           AND TXL.QTE_ID = TRX.QTE_ID
6445 
6446        UNION
6447 
6448        SELECT 'Y'
6449          FROM okc_k_headers_b CHR,
6450               okl_trx_contracts ktrx,
6451               okl_trx_contracts trx,
6452               okl_txl_quote_lines_b txl
6453             , OKC_K_ITEMS LNK_ITM
6454         WHERE CHR.ID         = p_chr_id
6455           AND ktrx.KHR_ID    =  chr.id
6456           AND ktrx.tsu_code  = 'ENTERED'
6457           AND ktrx.rbr_code  IS NOT NULL
6458           AND ktrx.tcn_type  = 'TRBK'
6459           AND ktrx.source_trx_type  = 'TCN'
6460           AND trx.tcn_type   = 'ALT'
6461           AND trx.id         = ktrx.source_trx_id
6462           AND TXL.QTE_ID = TRX.QTE_ID
6463           AND TXL.qlt_code = 'AMCFIA'
6464           AND TXL.kle_id = LNK_ITM.OBJECT1_ID1
6465           AND LNK_ITM.CLE_ID = p_kle_id
6466           AND LNK_ITM.DNZ_CHR_ID = p_chr_id
6467           AND LNK_ITM.JTOT_OBJECT1_CODE = 'OKX_COVASST'
6468           AND LNK_ITM.OBJECT1_ID2       = '#'
6469           AND EXISTS (SELECT '1'
6470                         FROM  okl_rbk_selected_contract rbk_khr
6471                        WHERE  rbk_khr.KHR_ID = chr.id
6472                          AND rbk_khr.STATUS <> 'PROCESSED')
6473           AND TXL.QTE_ID = TRX.QTE_ID;
6474 
6475      TYPE l_del_stm_id_tbl_type IS TABLE OF NUMBER
6476           INDEX BY BINARY_INTEGER;
6477      l_del_stm_id_tbl           l_del_stm_id_tbl_type;
6478      l_del_stm_id_idx           NUMBER := 0;
6479      l_wrk_strm_trx_number      NUMBER;
6480      l_accrual_previous_mnth_yn VARCHAR2(3);
6481      l_product_id               okl_products.id%TYPE;
6482      l_temp_term_date           DATE;
6483      l_param_termination_date   DATE;
6484      l_chk_accrual_stream       VARCHAR2(1);
6485      l_curr_term_flag           VARCHAR2(1);
6486      l_prog_name       CONSTANT VARCHAR2(100) := G_PKG_NAME || '.' || l_api_name;
6487 
6488   BEGIN
6489     x_return_status := OKL_API.G_RET_STS_SUCCESS;
6490     x_return_status := OKL_API.START_ACTIVITY (
6491                                l_api_name
6492                                ,p_init_msg_list
6493                                ,'_PVT'
6494                                ,x_return_status);
6495     -- Check if activity started successfully
6496     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6497        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6498     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6499        RAISE OKL_API.G_EXCEPTION_ERROR;
6500     END IF;
6501 
6502     print(l_prog_name, '---------------------------------------------------------------------------' );
6503     print(l_prog_name, l_api_name || ': Begin' );
6504     print(l_prog_name, '---------------------------------------------------------------------------' );
6505     -- Initialize variables/records
6506     l_strm_index  := 0;
6507     l_sel_index   := 0;
6508     l_prev_stm_id := NULL;
6509     print(l_prog_name, 'p_chr_id       = ' || p_chr_id );
6510     print(l_prog_name, 'p_purpose_code = ' || p_purpose_code );
6511     print(l_prog_name, 'p_source_call  = ' || p_source_call );
6512 
6513     IF( NVL(p_purpose_code, '-99') = '-99')
6514     THEN
6515       print(l_prog_name, 'Fetching Regular Product ID' );
6516       OPEN c_get_regular_product_id;
6517       FETCH c_get_regular_product_id INTO l_product_id;
6518       CLOSE c_get_regular_product_id;
6519     ELSE
6520       print(l_prog_name, 'Fetching Reporting Product ID' );
6521       OPEN c_get_report_product_id;
6522       FETCH c_get_report_product_id INTO l_product_id;
6523       CLOSE c_get_report_product_id;
6524     END IF;
6525     print(l_prog_name, 'l_product_id  = ' || l_product_id );
6526 
6527     --Check whether the CHECK_ACCRUALS_TILL_PREVIOUS_MONTH flag is checked or not
6528     OPEN check_accrual_previous_csr;
6529     FETCH check_accrual_previous_csr INTO l_accrual_previous_mnth_yn;
6530     CLOSE check_accrual_previous_csr;
6531     print(l_prog_name, 'l_accrual_previous_mnth_yn  = ' || l_accrual_previous_mnth_yn );
6532 
6533     -- Check if this API is being called in the context of the Mass Rebook Contract
6534     --If termination mass rebook transaction exists, then copy the old streams as
6535     --new streams instead of re-generating them
6536     l_mass_rbk_khr := '?';
6537     OPEN l_chk_mass_rbk_csr;
6538     FETCH l_chk_mass_rbk_csr INTO l_mass_rbk_khr;
6539     CLOSE l_chk_mass_rbk_csr;
6540     print(l_prog_name, 'l_mass_rbk_khr  = ' || l_mass_rbk_khr );
6541 
6542     IF l_mass_rbk_khr = '!'
6543     THEN
6544       print(l_prog_name, 'This API is called in context of the Mass Rebook Transaction only. Hence storing p_chr_id in l_orig_chr_id' );
6545       l_orig_chr_id := p_chr_id;
6546     ELSE
6547       -- This call is not during the Mass Rebook Partial Termination call
6548       print(l_prog_name, 'This API is called not during the Mass Rebook Partial Termination call. Hence fetch l_orig_chr_id from p_chr_id' );
6549       -- In case of a Revision copy contract context, use the below API to identify the proper line_id
6550       okl_la_stream_pvt.retrive_orig_hdr_id(p_chr_id, l_orig_chr_id);
6551     END IF;
6552 
6553     print(l_prog_name, 'Before executing the cursor l_get_curr_streams_lines. l_orig_chr_id= ' || l_orig_chr_id );
6554     -- Execute a cursor that helps in identifying the list of lines that got terminated.
6555     print(l_prog_name, '----------------------------------------------------------------------');
6556     print(l_prog_name, '--------Start: Copying and Deletion of the identified Streams --------');
6557     print(l_prog_name, '----------------------------------------------------------------------');
6558     FOR curr_rec IN l_get_curr_streams_lines(l_orig_chr_id )
6559     LOOP
6560       -- Store the Termination Date in l_terminated_date
6561       l_terminated_date  := curr_rec.date_terminated;
6562       print(l_prog_name, 'Line ID           = ' || curr_rec.kle_id );
6563       print(l_prog_name, 'l_terminated_date = ' || l_terminated_date );
6564 
6565       l_curr_term_flag := NULL;
6566       l_new_stm_cle_id := NULL;
6567       IF l_mass_rbk_khr = '!'
6568       THEN
6569         print(l_prog_name, 'Context is that of the Mass Rebook Partial Termination transaction ');
6570         print(l_prog_name, 'Before executing the cursor chk_curr_terminated_line p_kle_id = ' || curr_rec.kle_id );
6571         -- Execute a cursor to see, if the current line is the one that got terminated in the current termination cycle
6572         OPEN chk_curr_terminated_line (p_kle_id => curr_rec.kle_id);
6573         FETCH chk_curr_terminated_line INTO l_curr_term_flag;
6574         CLOSE chk_curr_terminated_line;
6575         -- Store the current line as the original line
6576         l_new_stm_cle_id := curr_rec.kle_id;
6577       ELSE
6578         print(l_prog_name, 'Context is that of the Rebook copy. Hence, fetch and store the Orignal Line ID ' );
6579         print(l_prog_name, 'Before executing the cursor c_get_orig_line_id cp_rbk_chr_id = ' || p_chr_id || ' cp_ole_id= ' || curr_rec.kle_id );
6580         -- Fetch the original line as the current line is the rebook version of the line
6581         OPEN c_get_orig_line_id(cp_rbk_chr_id => p_chr_id,
6582                                 cp_ole_id     => curr_rec.kle_id );
6583         FETCH c_get_orig_line_id INTO l_new_stm_cle_id;
6584         CLOSE c_get_orig_line_id;
6585       END IF;
6586       print(l_prog_name, 'l_curr_term_flag = ' || l_curr_term_flag );
6587       print(l_prog_name, 'l_new_stm_cle_id = ' || l_new_stm_cle_id );
6588 
6589       IF curr_rec.line_type = 'LINK_FEE_ASSET'
6590       THEN
6591         l_param_termination_date := NULL;
6592       ELSE
6593         l_param_termination_date := TRUNC(LAST_DAY(l_terminated_date));
6594       END IF;
6595 
6596       -- Execute the cursor to fetch the streams of the Terminated line in context
6597       print(l_prog_name, 'line_type | l_orig_chr_id | kle_id | l_param_termination_date | Purpose Code ' );
6598       print(l_prog_name, curr_rec.line_type || ' | ' || l_orig_chr_id || ' | ' || curr_rec.kle_id || ' | ' || l_param_termination_date || ' | ' || p_purpose_code );
6599       FOR streams_rec IN c_streams (l_orig_chr_id
6600                                    ,curr_rec.kle_id
6601                                    ,l_param_termination_date
6602                                    ,p_purpose_code)
6603       LOOP
6604         -- Set the Stream record if this is a new Stream
6605         IF l_prev_stm_id IS NULL OR (l_prev_stm_id <> streams_rec.stream_id)
6606         THEN
6607           -- Place where Stream Header gets built-up
6608           print(l_prog_name, 'l_prev_stm_id | Current: streams_rec.stream_id ' );
6609           print(l_prog_name, l_prev_stm_id || ' | ' ||  streams_rec.stream_id );
6610 
6611           -- Description of the Logic:
6612           -- If the context is Mass Rebook - Partial Termination then
6613           --   it means that the current line WORK streams need to be deleted
6614           --      Implementing this delete using Bulk Delete. Hence, just store the IDs as of now, and perfrom deletion later, all in one shot
6615           -- else if the context is the Rebook
6616           --   then find the Stream ID representing the current line stream in the Rebook version of the line
6617           --      and store for deleting this stream later.
6618           -- Identify the proper stream to delete
6619           print(l_prog_name, 'Find Stream Header ID with the following Predicates ' );
6620           print(l_prog_name, 'chr_id | cp_kle_id | cp_sty_id | cp_sgn_code | cp_purpose_code | cp_say_code | cp_active_yn' );
6621           print(l_prog_name, p_chr_id                   || ' | ' ||
6622                              l_new_stm_cle_id           || ' | ' ||
6623                              streams_rec.sty_id         || ' | ' ||
6624                              streams_rec.sgn_code       || ' | ' ||
6625                              streams_rec.purpose_code   || ' | ' ||
6626                              G_STREAM_ACTIVITY_WORK     || ' | ' ||
6627                              streams_rec.active_yn
6628           );
6629 
6630           l_wrk_strm_trx_number := NULL;
6631           print(l_prog_name,  'l_del_stm_id_idx | l_del_stm_id_tbl(l_del_stm_id_idx)');
6632           FOR t_rec IN c_identify_rbk_stm_csr(
6633                           cp_chr_id       => p_chr_id
6634                          ,cp_kle_id       => l_new_stm_cle_id
6635                          ,cp_sty_id       => streams_rec.sty_id
6636                          ,cp_sgn_code     => streams_rec.sgn_code
6637                          ,cp_purpose_code => streams_rec.purpose_code
6638                          ,cp_say_code     => G_STREAM_ACTIVITY_WORK
6639           )
6640           LOOP
6641             l_del_stm_id_idx                   := l_del_stm_id_idx + 1;
6642             l_del_stm_id_tbl(l_del_stm_id_idx) := t_rec.stm_id;
6643             l_wrk_strm_trx_number              := t_rec.transaction_number;
6644             print(l_prog_name,  l_del_stm_id_idx || ' | ' || l_del_stm_id_tbl(l_del_stm_id_idx) );
6645           END LOOP;
6646           IF l_wrk_strm_trx_number IS NULL
6647           THEN
6648             SELECT okl_sif_seq.nextval
6649               INTO l_wrk_strm_trx_number
6650               FROM DUAL;
6651           END IF;
6652           print(l_prog_name,  'l_wrk_strm_trx_number = ' || l_wrk_strm_trx_number );
6653 
6654           -- Store this Index
6655           l_prev_stm_id                         := streams_rec.stream_id;
6656           l_strm_index                          := l_strm_index + 1;
6657           l_stmv_tbl(l_strm_index).khr_id       := p_chr_id;
6658           -- l_new_stm_cle_id reprsents curr_rec.kle_id in case of the Mass Rebook partial Termination context,
6659           -- l_new_stm_cle_id represents the orignal_cle_id in case of the Rebook revision context
6660           l_stmv_tbl(l_strm_index).kle_id       := l_new_stm_cle_id; -- curr_rec.kle_id;
6661           l_stmv_tbl(l_strm_index).sty_id       := streams_rec.sty_id;
6662           l_stmv_tbl(l_strm_index).sgn_code     := streams_rec.sgn_code;
6663           l_stmv_tbl(l_strm_index).purpose_code := streams_rec.purpose_code;
6664           l_stmv_tbl(l_strm_index).say_code     := G_STREAM_ACTIVITY_WORK;
6665           l_stmv_tbl(l_strm_index).active_yn    := streams_rec.active_yn;
6666           -- Transaction Number re-used from the WORK Status Streams
6667           l_stmv_tbl(l_strm_index).transaction_number    := l_wrk_strm_trx_number;
6668           l_stmv_tbl(l_strm_index).date_working := SYSDATE;
6669 
6670           print(l_prog_name, 'l_strm_index | stream_id | chr_id | cle_id | sty_id | sgn_code | purpose_code | say_code | active_yn | date_working ' );
6671           print(l_prog_name, l_strm_index
6672                             || ' | ' || streams_rec.stream_id
6673                             || ' | ' || p_chr_id
6674                             || ' | ' || l_new_stm_cle_id
6675                             || ' | ' || streams_rec.sty_id
6676                             || ' | ' || streams_rec.sgn_code
6677                             || ' | ' || streams_rec.purpose_code
6678                             || ' | ' || G_STREAM_ACTIVITY_WORK
6679                             || ' | ' || streams_rec.active_yn
6680                             || ' | ' || l_stmv_tbl(l_strm_index).date_working
6681           );
6682           l_temp_term_date                      := NULL;
6683           l_chk_accrual_stream                  := NULL;
6684 
6685 
6686           -- Current and existing behavior for ESG, just copying over to ISG
6687           -- but applicable only in context of the Mass Rebook-Partial Termination
6688           IF ( l_terminated_date IS NOT NULL AND
6689                l_curr_term_flag = 'Y'        AND
6690                l_mass_rbk_khr = '!'
6691              )
6692           THEN
6693             OPEN c_chk_accrual_sty_on_pdt(cp_pdt_id => l_product_id,
6694                                           cp_sty_id => streams_rec.sty_id);
6695             FETCH c_chk_accrual_sty_on_pdt INTO l_chk_accrual_stream;
6696             CLOSE c_chk_accrual_sty_on_pdt;
6697 
6698             -- Check whether the stream in context is setup as Accruable in Product or not
6699             IF(l_chk_accrual_stream = 'Y')
6700             THEN
6701               IF nvl(l_accrual_previous_mnth_yn,'N') = 'N'
6702               THEN
6703                 l_temp_term_date := TRUNC(LAST_DAY(l_terminated_date));
6704               ELSE
6705                 l_temp_term_date := LAST_DAY(TRUNC(l_terminated_date, 'MONTH')-1);
6706               END IF;
6707             ELSE
6708               l_temp_term_date := TRUNC(l_terminated_date);
6709             END IF;
6710           END IF; -- End:  IF ( l_terminated_date IS NOT NULL AND
6711           print(l_prog_name, 'l_terminated_date | l_curr_term_flag | l_mass_rbk_khr | l_product_id | streams_rec.sty_id | l_chk_accrual_stream | l_accrual_previous_mnth_yn | l_temp_term_date' );
6712           print(l_prog_name,
6713                   l_terminated_date    || ' | ' ||
6714                   l_curr_term_flag     || ' | ' ||
6715                   l_mass_rbk_khr       || ' | ' ||
6716                   l_product_id         || ' | ' ||
6717                   streams_rec.sty_id   || ' | ' ||
6718                   l_chk_accrual_stream || ' | ' ||
6719                   l_accrual_previous_mnth_yn || ' | ' ||
6720                   l_temp_term_date
6721           );
6722           print(l_prog_name, 'Copying streams only till l_temp_term_date= ' || l_temp_term_date );
6723           print(l_prog_name, 'Status | Index | Date | Amount | Line # | Comments ' );
6724         END IF; -- end of check for new stream record
6725         ----------------------------------------------------
6726         -- Get the Stream Elements
6727         ----------------------------------------------------
6728         IF( ( l_temp_term_date IS NULL OR
6729               streams_rec.stream_element_date <= l_temp_term_date)
6730 --            OR (streams_rec.accrued_yn = 'Y' )
6731 /*
6732             OR
6733             ( l_curr_term_flag <> 'Y' AND
6734               streams_rec.stream_type_purpose IN
6735               (
6736                  'INTEREST_PAYMENT'
6737                 ,'PRINCIPAL_PAYMENT'
6738                 ,'PRINCIPAL_BALANCE'
6739                 ,'TERMINATION_VALUE'
6740               )
6741            )
6742 */       )
6743         THEN
6744           l_sel_index                                 := l_sel_index + 1;
6745           l_selv_tbl(l_sel_index).parent_index        := l_strm_index;
6746           l_selv_tbl(l_sel_index).stream_element_date := streams_rec.stream_element_date;
6747           l_selv_tbl(l_sel_index).amount              := streams_rec.amount;
6748           l_selv_tbl(l_sel_index).se_line_number      := streams_rec.se_line_number;
6749           l_selv_tbl(l_sel_index).comments            := streams_rec.comments;
6750           l_selv_tbl(l_sel_index).accrued_yn          := streams_rec.accrued_yn;
6751           print(l_prog_name, 'Copied '
6752                  || ' | ' || l_sel_index
6753                  || ' | ' || streams_rec.stream_element_date
6754                  || ' | ' || streams_rec.amount
6755                  || ' | ' || streams_rec.se_line_number
6756                  || ' | ' || streams_rec.comments
6757           );
6758         ELSE
6759           -- Just print for the logging to verify
6760           print(l_prog_name, 'Removed '
6761                  || ' | ' || l_sel_index
6762                  || ' | ' || streams_rec.stream_element_date
6763                  || ' | ' || streams_rec.amount
6764                  || ' | ' || streams_rec.se_line_number
6765                  || ' | ' || streams_rec.comments
6766           );
6767         END IF;
6768       END LOOP; -- END of FOR for Streams
6769     END LOOP; -- End: FOR curr_rec IN l_get_curr_streams_lines(l_orig_chr_id )
6770 
6771     -- Before creating the new set of streams, make sure that we delete the identified streams in WORK status
6772     print(l_prog_name, 'Before calling APIs to Delete the Identified Streams l_del_stm_id_tbl.COUNT=' || l_del_stm_id_tbl.COUNT );
6773     IF l_del_stm_id_tbl.COUNT > 0
6774     THEN
6775       -- Delete the records from the OKL_STREAM_ELEMENTS first
6776       print(l_prog_name, 'Before calling Delete okl_strm_elements statement : ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
6777       FORALL i IN l_del_stm_id_tbl.FIRST .. l_del_stm_id_tbl.LAST
6778         DELETE  okl_strm_elements sel
6779          WHERE  sel.stm_id = l_del_stm_id_tbl(i);
6780       print(l_prog_name, 'After calling Delete okl_strm_elements statement  : ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
6781 
6782       -- Delete teh records from the OKL_STREAMS table now
6783       print(l_prog_name, 'Before calling Delete okl_streams statement       : ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
6784       FORALL i IN l_del_stm_id_tbl.FIRST .. l_del_stm_id_tbl.LAST
6785         DELETE  okl_streams       stm
6786          WHERE  stm.id = l_del_stm_id_tbl(i);
6787       print(l_prog_name, 'After calling Delete okl_streams statement        : ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
6788     END IF;
6789 
6790     -- Finally at the very end of this API, create all the streams back in the DB using the Performant utiltiy API
6791     print(l_prog_name, 'Before calling APIs to insert the copied over streams.  l_stmv_tbl.COUNT =' || l_stmv_tbl.COUNT );
6792     IF l_stmv_tbl.COUNT > 0
6793     THEN
6794       -- Call the API to bulk insert data into streams and stream elements
6795       print(l_prog_name, 'Before calling OKL_STREAMS_PUB.CREATE_STREAMS_PERF : ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
6796       OKL_STREAMS_PUB.CREATE_STREAMS_PERF(p_api_version
6797                                          , p_init_msg_list
6798                                          , x_return_status
6799                                          , x_msg_count
6800                                          , x_msg_data
6801                                          , l_stmv_tbl
6802                                          , l_selv_tbl
6803                                          , lx_stmv_tbl
6804                                          , lx_selv_tbl);
6805       print(l_prog_name, 'After calling OKL_STREAMS_PUB.CREATE_STREAMS_PERF : ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') || 'x_return_status= ' || x_return_status);
6806       IF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6807         RAISE OKL_API.G_EXCEPTION_ERROR;
6808       ELSIF (x_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
6809         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6810       END IF;
6811     END IF; -- l_stmv_tbl count check
6812 
6813     -- End of API
6814     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data  => x_msg_data);
6815   EXCEPTION
6816     WHEN OKL_API.G_EXCEPTION_ERROR
6817     THEN
6818       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
6819               p_api_name  => l_api_name,
6820               p_pkg_name  => g_pkg_name,
6821               p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
6822               x_msg_count => x_msg_count,
6823               x_msg_data  => x_msg_data,
6824               p_api_type  => g_api_type);
6825     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR
6826     THEN
6827       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
6828                     p_api_name  => l_api_name,
6829                     p_pkg_name  => g_pkg_name,
6830                     p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
6831                     x_msg_count => x_msg_count,
6832                     x_msg_data  => x_msg_data,
6833                     p_api_type  => g_api_type);
6834      WHEN OTHERS
6835      THEN
6836        x_return_status := OKL_API.HANDLE_EXCEPTIONS(
6837                      p_api_name  => l_api_name,
6838                      p_pkg_name  => g_pkg_name,
6839                      p_exc_name  => 'OTHERS',
6840                      x_msg_count => x_msg_count,
6841                      x_msg_data  => x_msg_data,
6842                      p_api_type  => g_api_type);
6843  	END COPY_ACCRUAL_STREAMS_ISG;
6844 
6845 /*****************************************************************************************
6846   * API introduced as part of the Prospective Rebooking Enhancement
6847   * Author: Ravindranath Gooty
6848   * Description: This API fetches all the elgibile Accrual Streams generated
6849   *              and adjusts the change of the Accrual amount till Last Accrued
6850   *              date to the remaining Accrual elements.
6851   *              [For both Algorithms: Straight Line Algorithm and Spread By Income]
6852   *              sechawla 8/7/09 : support both 'FULL' and 'SERVICE_LINES' as the generation
6853   *                                context for the Service Income/Expense Accrual Streams
6854   *****************************************************************************************/
6855   PROCEDURE prosp_adj_acc_strms(
6856               p_api_version         IN         NUMBER
6857              ,p_init_msg_list       IN         VARCHAR2
6858              ,p_rebook_type         IN         VARCHAR2
6859              ,p_rebook_date         IN         DATE
6860              ,p_khr_id              IN         NUMBER
6861              ,p_deal_type           IN         VARCHAR2
6862              ,p_currency_code       IN         VARCHAR2
6863              ,p_start_date          IN         DATE
6864              ,p_end_date            IN         DATE
6865              ,p_context             IN         VARCHAR2
6866              ,p_purpose_code        IN         VARCHAR2
6867              ,x_return_status       OUT NOCOPY VARCHAR2
6868              ,x_msg_count           OUT NOCOPY NUMBER
6869              ,x_msg_data            OUT NOCOPY VARCHAR2)
6870   IS
6871 
6872     -- Cursor to fetch all the related Accrual Streams
6873     -- Introducing new column called Priority, because, in case of a OP Lease
6874     --  we need to make sure that the Rental Accrual Streams are first adjusted prospectively
6875     --  before we adjust any other Accrual Stream using the Spread By Income Logic
6876 
6877     --  15-Sep-10 SECHAWLA  10045043 : Added purpose PASS_THRU_SVC_EXP_ACCRUAL, for
6878     --  Passthru Service Expense Accrual Streams
6879     CURSOR lcu_get_accrual_streams(
6880               p_khr_id        NUMBER
6881              ,p_say_code      VARCHAR2
6882              ,p_purpose       VARCHAR2 )
6883     IS
6884       SELECT  stm.id                  stm_id
6885              ,stm.kle_id              kle_id
6886              ,stm.sty_id              sty_id
6887              ,sty.stream_type_purpose stm_purpose
6888              ,stm.say_code            say_code
6889              ,stm.purpose_code        purpose_code
6890              ,sty.stream_type_purpose sty_purpose
6891              ,sty.code                sty_code
6892              ,DECODE(
6893                sty.stream_type_purpose,
6894                'RENT_ACCRUAL', 1,
6895                99 )                   sty_priority
6896       FROM    okl_streams        stm,
6897               okl_strm_type_b    sty
6898       WHERE   stm.khr_id = p_khr_id
6899         AND   stm.say_code = p_say_code
6900         AND   NVL(stm.purpose_code, '-99') = p_purpose
6901         AND   sty.id = stm.sty_id
6902         AND   sty.stream_type_purpose IN (
6903               -- Accrual Streams generated using Straight Line Algorithm
6904                ( SELECT 'RENT_ACCRUAL' FROM DUAL WHERE p_context IN ( 'FULL' ) )
6905               ,( SELECT 'ACCRUED_FEE_INCOME' FROM DUAL WHERE p_context IN ( 'FULL' ) )
6906               ,( SELECT 'ACCRUED_FEE_EXPENSE' FROM DUAL WHERE p_context IN ( 'FULL' ) )
6907 
6908               --sechawla 10-aug-09 Added 'SERVICE_LINES' context for Service Income and Service Expense
6909               --streams to support Prospective Rebooking for these streams
6910               ,( SELECT 'SERVICE_INCOME'  FROM DUAL WHERE p_context IN ( 'FULL','SERVICE_LINES' ) )
6911               ,( SELECT 'SERVICE_EXPENSE'  FROM DUAL WHERE p_context IN ( 'FULL', 'SERVICE_LINES') )
6912 
6913               ,( SELECT 'PASS_THRU_REV_ACCRUAL'  FROM DUAL WHERE p_context IN ('FULL', 'PASSTHRU_ONLY' ) )
6914               ,( SELECT 'PASS_THRU_EXP_ACCRUAL'  FROM DUAL WHERE p_context IN ('FULL', 'PASSTHRU_ONLY' ) )
6915               --  15-Sep-10 SECHAWLA  10045043 : begin
6916               ,( SELECT 'PASS_THRU_SVC_EXP_ACCRUAL'  FROM DUAL WHERE p_context IN ('FULL', 'PASSTHRU_ONLY' ) )
6917               --  15-Sep-10 SECHAWLA  10045043 : end
6918 
6919               -- Accrual Streams generated using Spread By Income Logic
6920               ,( SELECT 'AMORTIZE_FEE_INCOME'  FROM DUAL WHERE p_context IN ('FULL' ) )
6921               ,( SELECT 'AMORTIZED_FEE_EXPENSE'  FROM DUAL WHERE p_context IN ('FULL') )
6922               ,( SELECT 'SUBSIDY_INCOME' FROM DUAL WHERE p_context IN ('FULL') )
6923           )
6924         AND stm.sgn_code NOT IN ( 'STMP' , 'STMP-REBK')
6925       ORDER BY  DECODE( sty.stream_type_purpose, 'RENT_ACCRUAL', 1, 99 ) ASC;
6926     TYPE l_accrual_streams_tbl_type  IS TABLE OF lcu_get_accrual_streams%ROWTYPE
6927       INDEX BY PLS_INTEGER;
6928 
6929     -- Cursor to fetch the Stream Elements and its details
6930     CURSOR get_strms_csr (
6931        p_khr_id       IN NUMBER
6932       ,p_kle_id       IN NUMBER
6933       ,p_sty_id       IN NUMBER
6934       ,p_purpose_code IN VARCHAR2
6935       ,p_say_code     IN VARCHAR2
6936     )
6937     IS
6938        SELECT sel.id                  sel_id,
6939               sel.amount              se_amount,
6940               sel.stream_element_date se_date,
6941               sel.comments            se_arrears,
6942               sel.sel_id              se_sel_id,
6943               stm.id                  stm_id,
6944               stm.khr_id              khr_id,
6945               stm.kle_id              kle_id,
6946               stm.sty_id              sty_id,
6947               stm.say_code            say_code,
6948               stm.purpose_code        purpose_code
6949        FROM   okl_strm_elements sel,
6950               okl_streams       stm
6951        WHERE  stm.kle_id = p_kle_id
6952          AND  stm.khr_id = p_khr_id
6953          AND  stm.sty_id = p_sty_id
6954          AND  DECODE(stm.purpose_code,
6955                      NULL, '-99',
6956                      'REPORT'
6957                     ) = p_purpose_code
6958          AND  stm.say_code = p_say_code
6959          AND  stm.id = sel.stm_id
6960        ORDER BY sel.stream_element_date;
6961     TYPE l_acc_strm_ele_tbl_type IS TABLE OF get_strms_csr%ROWTYPE
6962       INDEX BY BINARY_INTEGER;
6963 
6964     -- Cursor to get the Original Contract Id and Line Id Details
6965     CURSOR get_orig_khr_dtls_csr (
6966       p_kle_id       IN NUMBER   )
6967     IS
6968     SELECT  cle.orig_system_id1   orig_kle_id
6969            ,chr.orig_system_id1   orig_khr_id
6970       FROM  okc_k_lines_b         cle
6971            ,okc_k_headers_b       chr
6972      WHERE  cle.id          = p_kle_id
6973        AND  cle.dnz_chr_id  = chr.id
6974        AND  cle.orig_system_id1 IS NOT NULL;
6975 
6976     -- Cursor to fetch the Payment Level Information for the Lines
6977     CURSOR get_payment_sll_dtls_csr(
6978               p_khr_id   IN   NUMBER
6979              ,p_kle_id   IN   NUMBER
6980            )
6981     IS
6982       SELECT TO_NUMBER(slh.object1_id1)                            sty_id,
6983              sty.stream_type_purpose                               sty_purpose,
6984              FND_DATE.canonical_to_date(sll.rule_information2)     start_date,
6985              TO_NUMBER(sll.rule_information3)                      periods,
6986              sll.object1_id1                                       frequency,
6987              sll.rule_information5                                 structure,
6988              DECODE(sll.rule_information10,
6989              'Y', 'ARREARS',
6990                   'ADVANCE')                                       advance_arrears,
6991              FND_NUMBER.canonical_to_number(sll.rule_information6) amount,
6992              TO_NUMBER(sll.rule_information7)                      stub_days,
6993              TO_NUMBER(sll.rule_information8)                      stub_amount,
6994              DECODE( sll.rule_information7
6995                -- Case: When Payment Level is Regular Periods
6996                ,NULL
6997                 ,( ADD_MONTHS
6998                    ( FND_DATE.canonical_to_date(sll.rule_information2),
6999                       NVL(TO_NUMBER(sll.rule_information3),1)
7000                       * DECODE(sll.object1_id1, 'M',1,'Q',3,'S',6,'A',12)
7001                    ) - 1
7002                  )
7003                -- Case: When Payment Level is Regular Periods
7004                ,FND_DATE.canonical_to_date(sll.rule_information2)
7005                  + TO_NUMBER(sll.rule_information7) - 1
7006              )                                                    end_date
7007       FROM   okc_rule_groups_b  rgp,
7008              okc_rules_b        slh,
7009              okc_rules_b        sll,
7010              okl_strm_type_b    sty
7011       WHERE  sll.dnz_chr_id                = p_khr_id
7012         AND  sll.rule_information_category = 'LASLL'
7013         AND  sll.rgp_id                    = rgp.id
7014         AND  TO_NUMBER(sll.object2_id1)    = slh.id
7015         AND  rgp.cle_id                    = p_kle_id
7016         AND  sty.id                        = slh.object1_id1
7017         AND  sty.stream_type_purpose NOT IN
7018              ( 'DOWN_PAYMENT'
7019                ,'ESTIMATED_PROPERTY_TAX'
7020                ,'UNSCHEDULED_PRINCIPAL_PAYMENT'
7021              )
7022      ORDER BY sty.id, FND_DATE.canonical_to_date(sll.rule_information2);
7023 
7024     -- Cursor to find the End Date of the Expense Fee
7025     CURSOR get_exp_end_date_csr(
7026               p_khr_id   IN   NUMBER
7027              ,p_kle_id   IN   NUMBER
7028            )
7029     IS
7030      SELECT TO_NUMBER(rul.rule_information1) periods,
7031             TO_NUMBER(rul.rule_information2) amount,
7032             DECODE(rul2.object1_id1, 'M', 1, 'Q', 3, 'S', 6, 'A', 12, NULL) mpp,
7033             cle.start_date,
7034             cle.sts_code,
7035             kle.fee_type,
7036             ( ADD_MONTHS(cle.start_date,
7037                TO_NUMBER(rul.rule_information1)  -- Periods
7038                * DECODE(rul2.object1_id1, 'M', 1, 'Q', 3, 'S', 6, 'A', 12, NULL)
7039             ) - 1 ) end_date
7040      FROM   okc_rules_b       rul,
7041             okc_rules_b       rul2,
7042             okc_rule_groups_b rgp,
7043             okc_k_lines_b     cle,
7044             okl_k_lines       kle
7045      WHERE  cle.id         = p_kle_id
7046        AND  cle.sts_code IN ('NEW', 'INCOMPLETE', 'PASSED', 'COMPLETE')
7047        AND  kle.id         = cle.id
7048        AND  kle.fee_type NOT IN
7049             (
7050                'FINANCED'
7051               ,'ABSORBED'
7052               ,'ROLLOVER'
7053             )
7054        AND  rgp.cle_id     = cle.id
7055        AND  rgp.dnz_chr_id = p_khr_id
7056        AND  rgp.rgd_code = 'LAFEXP'
7057        AND  rgp.id = rul.rgp_id
7058        AND  rgp.id = rul2.rgp_id
7059        AND  rul.rule_information_category = 'LAFEXP'
7060        AND  rul2.rule_information_category = 'LAFREQ';
7061 
7062     -- Cursor: Spread By Income Logic: To fetch all the eligible Income Streams
7063     CURSOR c_k_income (
7064               p_sty_name     IN VARCHAR2
7065              ,p_start_date   IN DATE
7066              ,p_end_date     IN DATE
7067              ,p_purpose_code IN VARCHAR2
7068     )
7069     IS
7070       SELECT  TRUNC(sel.stream_element_date) income_date
7071              ,SUM(sel.amount)                income_amount
7072         FROM  okl_strm_elements       sel
7073              ,okl_streams             stm
7074              ,okl_strm_type_b         sty
7075              ,okl_strm_type_tl        styt
7076       WHERE  stm.khr_id      = p_khr_id
7077         AND  stm.say_code    = 'WORK'
7078         AND  stm.id          = sel.stm_id
7079         AND  stm.sty_id      = sty.id
7080         AND  sty.version     = '1.0'
7081         AND  sty.id          = styt.id
7082         AND  styt.language   = 'US'
7083         AND  styt.name       = p_sty_name
7084         AND  sel.stream_element_date >= p_start_date
7085         AND  sel.stream_element_date <= LAST_DAY(p_end_date)
7086         AND  DECODE(stm.purpose_code, NULL, '-99', 'REPORT') = p_purpose_code
7087     GROUP BY TRUNC(sel.stream_element_date)
7088     ORDER BY TRUNC(sel.stream_element_date) ASC;
7089 
7090     TYPE income_streams_rec_type is RECORD
7091     (
7092         income_amount      NUMBER
7093        ,income_date        DATE
7094        ,income_proportion  NUMBER
7095 
7096     );
7097 
7098     TYPE income_streams_tbl_type is TABLE OF income_streams_rec_type
7099     INDEX BY BINARY_INTEGER;
7100     income_streams_tbl         income_streams_tbl_type;
7101     inx_tot_inc                NUMBER;
7102     ln_tot_inc_amount          NUMBER;
7103 
7104     -- Cursor: Spread By Income Logic: To identify the Stream Type used for Authoring the Asset Payments
7105     CURSOR l_rent_pri_sty_name_csr
7106     IS
7107       SELECT DISTINCT NVL(slh.object1_id1, -1) styid
7108         FROM OKC_RULE_GROUPS_B rgp,
7109              OKC_RULES_B       sll,
7110              okc_rules_b       slh,
7111              okl_strm_type_b   sty
7112        WHERE slh.rgp_id   = rgp.id
7113          AND rgp.RGD_CODE = 'LALEVL'
7114          AND sll.RULE_INFORMATION_CATEGORY = 'LASLL'
7115          AND slh.RULE_INFORMATION_CATEGORY = 'LASLH'
7116          AND TO_CHAR(slh.id) = sll.object2_id1
7117          AND slh.object1_id1 = sty.id
7118          AND sty.stream_type_purpose = 'RENT'
7119          AND rgp.dnz_chr_id  = p_khr_id;
7120     l_rent_pri_sty_name_rec    l_rent_pri_sty_name_csr%ROWTYPE;
7121 
7122     l_accrual_streams_tbl      l_accrual_streams_tbl_type;
7123     l_work_acc_strms_tbl       l_acc_strm_ele_tbl_type;
7124     l_curr_acc_strms_tbl       l_acc_strm_ele_tbl_type;
7125     l_api_name                 VARCHAR2(30)  := 'prosp_adj_acc_strms';
7126     l_prog_name       CONSTANT VARCHAR2(100) := G_PKG_NAME || '.' || l_api_name;
7127     l_return_status            VARCHAR2(1);
7128     l_rebook_type              VARCHAR2(30);
7129     l_rebook_date              DATE;
7130     l_last_accrued_date        DATE;
7131     l_rebook_eff_date          DATE;
7132     l_term_end_date            DATE;
7133     inx_work                   NUMBER;
7134     inx_curr                   NUMBER;
7135     l_kle_id                   NUMBER;
7136     l_sty_id                   NUMBER;
7137     l_stm_id                   NUMBER;
7138     l_stm_purpose              VARCHAR2(150);
7139     l_orig_khr_id              NUMBER;
7140     l_orig_kle_id              NUMBER;
7141     l_flip_prb_rbk_reason      VARCHAR2(150);
7142     ln_work_acc_amount         NUMBER;
7143     ln_curr_acc_amount         NUMBER;
7144     l_acc_adjustment           NUMBER;
7145     l_rem_days                 NUMBER;
7146     l_day_convention_month     VARCHAR2(30);
7147     l_day_convention_year      VARCHAR2(30);
7148     l_per_start_date           DATE;   -- Period Start Date
7149     l_per_end_date             DATE;   -- Period End Date
7150     l_per_days                 NUMBER; -- Days in the Period
7151     l_per_adjustment           NUMBER; -- Adjustment in the period
7152     l_per_rounded_adjustment   NUMBER;
7153     l_tot_per_adjustment       NUMBER; -- Total of the Period Adjustments
7154     l_un_modified_amount       NUMBER;
7155 
7156     -- Spread By Income Logic Modifications
7157     ln_tot_work_acc_amt        NUMBER;
7158     l_pri_rent_sty_id          NUMBER;
7159     l_inc_stream_id            NUMBER;
7160     l_inc_stream_name          VARCHAR2(300);
7161     ln_amt_before_adjustment   NUMBER;
7162     l_need_to_fetch_inc_strms  VARCHAR2(3);
7163 
7164     TYPE l_num_tbl_type IS TABLE OF NUMBER
7165       INDEX BY BINARY_INTEGER;
7166     l_sel_id_tbl               l_num_tbl_type;
7167     l_stm_id_tbl               l_num_tbl_type;
7168     l_sel_amt_tbl              l_num_tbl_type;
7169     i                          NUMBER;
7170 
7171     --Bug# 12596925
7172     l_selv_tbl                 okl_streams_pub.selv_tbl_type;
7173     lx_selv_tbl                okl_streams_pub.selv_tbl_type;
7174     j                          NUMBER;
7175 
7176   BEGIN
7177     -- Added by RGOOTY: For Debugging purposes
7178     L_DEBUG_ENABLED := OKL_DEBUG_PUB.CHECK_LOG_ENABLED;
7179     x_return_status := OKL_API.G_RET_STS_SUCCESS;
7180     x_return_status := OKL_API.START_ACTIVITY(
7181                          p_api_name      => l_api_name,
7182                          p_pkg_name      => g_pkg_name,
7183                          p_init_msg_list => p_init_msg_list,
7184                          l_api_version   => p_api_version,
7185                          p_api_version   => p_api_version,
7186                          p_api_type      => G_API_TYPE,
7187                          x_return_status => x_return_status);
7188     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR)
7189     THEN
7190        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7191     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR)
7192     THEN
7193        RAISE OKL_API.G_EXCEPTION_ERROR;
7194     END IF;
7195     -- Initialize the things
7196     l_rebook_date := TRUNC(p_rebook_date);
7197     l_rebook_type := p_rebook_type;
7198 
7199     print(l_prog_name, 'p_rebook_type  = ' || l_rebook_type );
7200     print(l_prog_name, 'p_rebook_date  = ' || l_rebook_date );
7201     print(l_prog_name, 'p_khr_id       = ' || p_khr_id );
7202     print(l_prog_name, 'p_deal_type    = ' || p_deal_type );
7203     print(l_prog_name, 'p_currency_code= ' || p_currency_code );
7204     print(l_prog_name, 'p_start_date   = ' || p_start_date );
7205     print(l_prog_name, 'p_end_date     = ' || p_end_date );
7206     print(l_prog_name, 'p_context      = ' || p_context );
7207     print(l_prog_name, 'p_purpose_code = ' || p_purpose_code );
7208     print(l_prog_name, 'Rebook Date    = ' || p_rebook_date );
7209 
7210     -- Derieve the Rebook Effective Date
7211     l_flip_prb_rbk_reason := 'NO_REASON';
7212     IF l_rebook_date IS NULL
7213     THEN
7214       l_flip_prb_rbk_reason := 'NO_REBOOK_DATE';
7215     END IF;
7216     IF l_rebook_type IS NULL
7217     THEN
7218       l_flip_prb_rbk_reason := 'NO_REBOOK_TYPE_MENTIONED';
7219     END IF;
7220     IF p_currency_code IS NULL
7221     THEN
7222       l_flip_prb_rbk_reason := 'NO_CURRENCY_CODE_MENTIONED';
7223     END IF;
7224     IF p_deal_type IS NULL
7225     THEN
7226       l_flip_prb_rbk_reason := 'NO_DEAL_TYPE_MENTIONED';
7227     END IF;
7228 
7229     -- Calculate the Last Accrued Date
7230     l_last_accrued_date := TRUNC( LAST_DAY(ADD_MONTHS(l_rebook_date, -1) ) );
7231     print( l_prog_name,'Contract Start Date | Rebook Date | Last Accrued Date | End Date');
7232     print( l_prog_name, p_start_date || ' | ' || l_rebook_date
7233                       || ' | ' || l_last_accrued_date || ' | ' || p_end_date );
7234     IF TRUNC(p_start_date) = TRUNC(p_rebook_date)
7235     THEN
7236       l_flip_prb_rbk_reason := 'REBOOK_DATE_EQUALS_CONTRACT_START_DATE';
7237     ELSIF l_last_accrued_date < TRUNC(p_start_date)
7238     THEN
7239       -- In cases where the immediately preceding Lease/Interest Income
7240       --  stream element does not exist, the Rebook is retrospective.
7241       -- This applies to contract lines where the rebook effective date
7242       -- is before or on the first calendar month end.
7243       l_flip_prb_rbk_reason := 'LAST_ACCRUED_DATE_LESS_THAN_CONTRACT_START_DATE';
7244     END IF;
7245 
7246     -- Fetch the day convention ..
7247     IF l_flip_prb_rbk_reason = 'NO_REASON'
7248     THEN
7249       l_flip_prb_rbk_reason := 'INVALID_DAY_CONVENTION';
7250       OKL_PRICING_UTILS_PVT.get_day_convention(
7251          p_id              => p_khr_id
7252         ,p_source          => 'ISG'
7253         ,x_days_in_month   => l_day_convention_month
7254         ,x_days_in_year    => l_day_convention_year
7255         ,x_return_status   => x_return_status);
7256       print(l_prog_name, 'Day Convention Month/Year = ' || l_day_convention_month || '/' || l_day_convention_year );
7257       IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
7258         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7259       ELSIF (x_return_status = G_RET_STS_ERROR) THEN
7260         RAISE OKL_API.G_EXCEPTION_ERROR;
7261       END IF;
7262       l_flip_prb_rbk_reason := 'NO_REASON';
7263     END IF;
7264 
7265     -- Modifications: Spread By Income Logic
7266     --  Step: First identify the Rent Primary Stream Type name associated to Asset Payments
7267     -- Find the Primary Rent Stream Id
7268     OPEN  l_rent_pri_sty_name_csr;
7269     FETCH l_rent_pri_sty_name_csr INTO l_rent_pri_sty_name_rec;
7270     CLOSE l_rent_pri_sty_name_csr;
7271     -- Assign the Rent Primary Sty Id to l_pri_rent_sty_id
7272     l_pri_rent_sty_id := l_rent_pri_sty_name_rec.styid;
7273     print(l_prog_name, 'Primary Rent Stream ID = ' || l_pri_rent_sty_id );
7274 
7275     -- Using the l_pri_rent_sty_id, identify the Dependent Income Stream Name,
7276     --  based on the Lease Contract Book Classification
7277     print(l_prog_name, 'Identifying the Dependent Income Stream Type Name' );
7278     IF p_deal_type = 'LEASEOP'
7279     THEN
7280       OKL_ISG_UTILS_PVT.get_dependent_stream_type(
7281                           p_khr_id                => p_khr_id,
7282                           p_deal_type             => p_deal_type,
7283                           p_primary_sty_id        => l_pri_rent_sty_id,
7284                           p_dependent_sty_purpose => 'RENT_ACCRUAL',
7285                           x_return_status         => x_return_status,
7286                           x_dependent_sty_id      => l_inc_stream_id,
7287                           x_dependent_sty_name    => l_inc_stream_name);
7288       print(l_prog_name, '1. ' || p_deal_type || ': Income Stream Name = ' || l_inc_stream_name );
7289       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
7290           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7291       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
7292           RAISE OKL_API.G_EXCEPTION_ERROR;
7293       END IF;
7294     ELSIF p_deal_type IN ('LEASEDF', 'LEASEST')
7295     THEN
7296       OKL_ISG_UTILS_PVT.get_dependent_stream_type(
7297                           p_khr_id                => p_khr_id,
7298                           p_deal_type             => p_deal_type,
7299                           p_primary_sty_id        => l_pri_rent_sty_id,
7300                           p_dependent_sty_purpose => 'LEASE_INCOME',
7301                           x_return_status         => x_return_status,
7302                           x_dependent_sty_id      => l_inc_stream_id,
7303                           x_dependent_sty_name    => l_inc_stream_name);
7304       print(l_prog_name, '2. ' || p_deal_type || ': Income Stream Name = ' || l_inc_stream_name );
7305       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
7306           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7307       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
7308           RAISE OKL_API.G_EXCEPTION_ERROR;
7309       END IF;
7310     ELSIF p_deal_type IN ('LOAN', 'LOAN-REVOLVING')
7311     THEN
7312       OKL_ISG_UTILS_PVT.get_dependent_stream_type(
7313                          p_khr_id                => p_khr_id,
7314                          p_deal_type             => p_deal_type,
7315                          p_primary_sty_id        => l_pri_rent_sty_id,
7316                          p_dependent_sty_purpose => 'INTEREST_INCOME',
7317                          x_return_status         => x_return_status,
7318                          x_dependent_sty_id      => l_inc_stream_id,
7319                          x_dependent_sty_name    => l_inc_stream_name);
7320       print(l_prog_name, '3. ' || p_deal_type || ': Income Stream Name = ' || l_inc_stream_name );
7321       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
7322           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7323       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
7324           RAISE OKL_API.G_EXCEPTION_ERROR;
7325       END IF;
7326     END IF;
7327     -- Initialize the variable l_need_to_fetch_inc_strms
7328     l_need_to_fetch_inc_strms := 'Y';
7329 
7330     IF p_deal_type NOT IN ('LEASEOP' )
7331     THEN
7332       -- Fetch all the Income Streams of the Contract
7333       -- Consolidate and calculate the Income Proportions for all
7334       --  the Income Stream Elements after the Last Accrued Date
7335       print(l_prog_name, 'Executing the Cursor c_k_income: (p_sty_name | p_start_date | p_end_date | p_purpose_code )'  );
7336       print(l_prog_name, '(' || l_inc_stream_name || '|' || p_start_date || '|' || p_end_date || '|' ||
7337                               p_purpose_code || ')' );
7338       print(l_prog_name, 'WORK: Contract Level Income Streams After' ||
7339                           l_last_accrued_date || '[Last Accrued Date] '  );
7340       print(l_prog_name, 'Date | Amount ' );
7341       inx_tot_inc       := 1;
7342       ln_tot_inc_amount := 0;
7343       FOR inc_rec IN c_k_income(
7344               p_sty_name     => l_inc_stream_name
7345              ,p_start_date   => p_start_date
7346              ,p_end_date     => p_end_date
7347              ,p_purpose_code => p_purpose_code
7348           )
7349       LOOP
7350         IF inc_rec.income_date > l_last_accrued_date
7351         THEN
7352           print(l_prog_name, inc_rec.income_date || '|' || inc_rec.income_amount );
7353           -- Store it in the Income Streams Tbl
7354           income_streams_tbl(inx_tot_inc).income_date   := inc_rec.income_date;
7355           income_streams_tbl(inx_tot_inc).income_amount := inc_rec.income_amount;
7356           -- Calculate the Total Income Amount
7357           ln_tot_inc_amount := ln_tot_inc_amount + income_streams_tbl(inx_tot_inc).income_amount;
7358           -- Increment the inx_tot_inc index
7359           inx_tot_inc := inx_tot_inc + 1;
7360         END IF;
7361       END LOOP;
7362       -- Calculate the Income Proportions for the Income Streams fetched above
7363       print(l_prog_name, 'Calculating the Income Proporitions for the Income Streams ' );
7364       IF income_streams_tbl.COUNT > 0
7365       THEN
7366         print(l_prog_name, 'Date | Amount | Total Income Amount | Income Proportion ' );
7367         FOR inx_tot_inc IN income_streams_tbl.FIRST .. income_streams_tbl.LAST
7368         LOOP
7369           --Added by bkatraga for bug 12413695
7370           IF(ln_tot_inc_amount = 0) THEN
7371             income_streams_tbl(inx_tot_inc).income_proportion := 0;
7372           ELSE
7373           --end bkatraga
7374             income_streams_tbl(inx_tot_inc).income_proportion :=
7375               income_streams_tbl(inx_tot_inc).income_amount / ln_tot_inc_amount;
7376           END IF; --Added by bkatraga for bug 12413695
7377 
7378           print(l_prog_name, income_streams_tbl(inx_tot_inc).income_date || '|' ||
7379                              income_streams_tbl(inx_tot_inc).income_amount || '|' ||
7380                              ln_tot_inc_amount || '|' ||
7381                              income_streams_tbl(inx_tot_inc).income_proportion );
7382         END LOOP;
7383       END IF; -- IF income_streams_tbl.COUNT > 0
7384       -- Flaging off the variable l_need_to_fetch_inc_strms
7385       l_need_to_fetch_inc_strms := 'N';
7386     END IF;
7387 
7388     -- Fetch all the streams for this Accrual Streams already generated
7389     --   and eligible for Prospective Rebooking
7390     IF l_flip_prb_rbk_reason = 'NO_REASON'
7391     THEN
7392       -- Initialize the Index i for the Bulk Update of all the Eligible Streams
7393       i := 1;
7394       print(l_prog_name, 'Fetching all the Eligible Accrual Stream Elements '
7395             || 'p_khr_id=' || p_khr_id || 'p_say_code=WORK p_purpose='|| p_purpose_code );
7396       FOR t_rec IN lcu_get_accrual_streams(
7397                       p_khr_id        => p_khr_id
7398                      ,p_say_code      => 'WORK'
7399                      ,p_purpose       => p_purpose_code )
7400       LOOP
7401         -- For each Accrual Stream Header, proceed to adjust the streams
7402         --  prospectively
7403         l_kle_id  := t_rec.kle_id;
7404         l_sty_id  := t_rec.sty_id;
7405         l_stm_id  := t_rec.stm_id;
7406         l_stm_purpose := t_rec.stm_purpose;
7407         -- Delete the Old Streams Collection for each iteration
7408         l_accrual_streams_tbl.DELETE;
7409         l_work_acc_strms_tbl.DELETE;
7410         l_curr_acc_strms_tbl.DELETE;
7411 
7412          --Bug# 12596925
7413         l_selv_tbl.DELETE;
7414         lx_selv_tbl.DELETE;
7415         j := 0;
7416 
7417         print(l_prog_name, 'Handling the Stream (Stream Name, Purpose, khr_id, kle_id, sty_id, stm_id) = ' ||
7418            t_rec.sty_code || ' | ' || t_rec.sty_purpose || ' | ' ||
7419            p_khr_id || ',' || l_kle_id || ',' || l_sty_id || ',' || l_stm_id );
7420         l_flip_prb_rbk_reason   := 'WORK_STRMS_NOT_FOUND';
7421         -- Step 1:
7422         --   Fetch the Stream Elements for the current version. [Status = WORK]
7423         print(l_prog_name, 'WORK Streams ' );
7424         print(l_prog_name, 'Date|Amount|Arrears ');
7425         -- Re-Initialize the variables related to the WORK Accrual Streams
7426         inx_work := 1;
7427         ln_work_acc_amount  := 0;
7428         ln_tot_work_acc_amt := 0;
7429 
7430         FOR t_rec IN get_strms_csr(
7431                p_khr_id       => p_khr_id
7432               ,p_kle_id       => l_kle_id
7433               ,p_sty_id       => l_sty_id
7434               ,p_purpose_code => p_purpose_code
7435               ,p_say_code     => 'WORK')
7436         LOOP
7437           l_work_acc_strms_tbl(inx_work) := t_rec;
7438           print(l_prog_name, l_work_acc_strms_tbl(inx_work).se_date
7439                 || '|' || l_work_acc_strms_tbl(inx_work).se_amount
7440                 || '|' || l_work_acc_strms_tbl(inx_work).se_arrears );
7441           IF TRUNC(l_work_acc_strms_tbl(inx_work).se_date) <= l_last_accrued_date
7442           THEN
7443             ln_work_acc_amount := ln_work_acc_amount +
7444                                   l_work_acc_strms_tbl(inx_work).se_amount;
7445           END IF;
7446           ln_tot_work_acc_amt := ln_tot_work_acc_amt + l_work_acc_strms_tbl(inx_work).se_amount;
7447           -- Increment the inx_work
7448           inx_work := inx_work + 1;
7449         END LOOP;
7450         IF l_work_acc_strms_tbl.COUNT > 0
7451         THEN
7452           l_flip_prb_rbk_reason   := 'NO_REASON';
7453         END IF;
7454 
7455         -- Step 2:
7456         --   Fetch the Original Contract Id and Kle_id and fetch CURR status streams
7457         -- First, fetch the Original Contract and Configuration Line ID
7458         IF l_flip_prb_rbk_reason = 'NO_REASON'
7459         THEN
7460           -- Only in case of Online Rebook, we need to fetch the
7461           --  Streams using the Original contract and Line Id
7462           IF l_rebook_type = 'ONLINE_REBOOK'
7463           THEN
7464             print( l_prog_name, 'Fetching Original Line and Contract Id. Rebook Type' || l_rebook_type );
7465             l_flip_prb_rbk_reason := 'ORIG_KHR_KLE_ID_NOT_FOUND';
7466             FOR t1_rec IN get_orig_khr_dtls_csr  (
7467                            p_kle_id => l_kle_id )
7468             LOOP
7469               l_orig_khr_id := t1_rec.orig_khr_id;
7470               l_orig_kle_id := t1_rec.orig_kle_id;
7471               l_flip_prb_rbk_reason := 'NO_REASON';
7472             END LOOP;
7473           ELSE
7474             -- Mass Rebook flows
7475             print( l_prog_name, 'Using current Line and Contract Id. Rebook Type=' || l_rebook_type );
7476             l_orig_khr_id := p_khr_id;
7477             l_orig_kle_id := l_kle_id;
7478           END IF;
7479           print( l_prog_name, 'Original Contract Id | Original Line Id ' );
7480           print( l_prog_name, l_orig_khr_id || ' | ' || l_orig_kle_id );
7481         END IF; -- IF l_flip_prb_rbk_reason = 'NO_REASON'
7482 
7483         IF l_flip_prb_rbk_reason = 'NO_REASON'
7484         THEN
7485           l_flip_prb_rbk_reason   := 'CURR_STRMS_NOT_FOUND';
7486           print( l_prog_name, 'Fetching the Original CURR Streams ');
7487           -- Fetch the Original Streams, status = CURR
7488           print(l_prog_name, 'CURR Streams ' );
7489           print(l_prog_name, 'Date|Amount|Arrears ');
7490           inx_curr := 1;
7491           ln_curr_acc_amount := 0;
7492           FOR t_rec IN get_strms_csr(
7493                  p_khr_id       => l_orig_khr_id
7494                 ,p_kle_id       => l_orig_kle_id
7495                 ,p_sty_id       => l_sty_id
7496                 ,p_purpose_code => p_purpose_code
7497                 ,p_say_code     => 'CURR')
7498           LOOP
7499             l_curr_acc_strms_tbl(inx_curr) := t_rec;
7500             print(l_prog_name, l_curr_acc_strms_tbl(inx_curr).se_date
7501                   || '|' || l_curr_acc_strms_tbl(inx_curr).se_amount
7502                   || '|' || l_curr_acc_strms_tbl(inx_curr).se_arrears );
7503             IF TRUNC(l_curr_acc_strms_tbl(inx_curr).se_date) <= l_last_accrued_date
7504             THEN
7505               ln_curr_acc_amount := ln_curr_acc_amount +
7506                                     l_curr_acc_strms_tbl(inx_curr).se_amount;
7507             END IF;
7508             -- Increment the inx_curr
7509             inx_curr := inx_curr + 1;
7510           END LOOP;
7511           IF l_curr_acc_strms_tbl.COUNT > 0
7512           THEN
7513             l_flip_prb_rbk_reason   := 'NO_REASON';
7514           END IF;
7515         END IF;
7516 
7517         IF l_stm_purpose IN (
7518               'AMORTIZE_FEE_INCOME'
7519              ,'AMORTIZED_FEE_EXPENSE'
7520              ,'SUBSIDY_INCOME'
7521            )
7522         THEN
7523           -- Handle the Case when the Contract is a Operating Lease
7524           --   and the Current Accrual is a Non-Rental Accrual Stream
7525           --   and till now we have not fetched and calculated the Income Proportions
7526           IF p_deal_type IN ('LEASEOP' )  -- Operating Lease Contract
7527              AND t_rec.sty_priority = 99  -- Non Rental Accrual Streams
7528              AND l_need_to_fetch_inc_strms = 'Y' -- Till Now the Income Streams are not fetched
7529           THEN
7530             print(l_prog_name, '!!!!! First of all, fetching the Income Proportions of the Rental Accruals !!!!! ' );
7531             -- First Step: Bulk Update all the Prospectively adjusted Rental Accrual Streams till now
7532             print(l_prog_name, 'Total Number of Rental Accrual Stream Elements to be updated ' || l_sel_id_tbl.COUNT );
7533             IF l_sel_id_tbl.COUNT > 0
7534             THEN
7535               print(l_prog_name, 'Start: Performaing the Bulk Update of ' || l_sel_id_tbl.COUNT || ' Streams. '
7536                                  || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
7537               -- Bulk update
7538               FORALL i in l_sel_id_tbl.FIRST .. l_sel_id_tbl.LAST
7539                  UPDATE okl_strm_elements
7540                     SET amount = l_sel_amt_tbl(i)
7541                   WHERE id     = l_sel_id_tbl(i)
7542                     AND stm_id = l_stm_id_tbl(i);
7543               print(l_prog_name, 'Done: Performaing the Bulk Update of ' || l_sel_id_tbl.COUNT || ' Streams '
7544                                  || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
7545             END IF;
7546             -- Re-Initialize the collections used for the Bulk Update
7547             i := 1;
7548             l_sel_id_tbl.DELETE;
7549             l_sel_amt_tbl.DELETE;
7550             -- Next time onwards, we dont need to fetch the Rental Income Streams again
7551             l_need_to_fetch_inc_strms := 'N';
7552 
7553             -- Fetch all the Income Streams of the Contract
7554             -- Consolidate and calculate the Income Proportions for all
7555             --  the Income Stream Elements after the Last Accrued Date
7556             print(l_prog_name, 'Executing the Cursor c_k_income: (p_sty_name | p_start_date | p_end_date | p_purpose_code )'  );
7557             print(l_prog_name, '(' || l_inc_stream_name || '|' || p_start_date || '|' || p_end_date || '|' ||
7558                                     p_purpose_code || ')' );
7559             print(l_prog_name, 'WORK: Contract Level Income Streams After' ||
7560                                 l_last_accrued_date || '[Last Accrued Date] '  );
7561             print(l_prog_name, 'Date | Amount ' );
7562             inx_tot_inc       := 1;
7563             ln_tot_inc_amount := 0;
7564             FOR inc_rec IN c_k_income(
7565                     p_sty_name     => l_inc_stream_name
7566                    ,p_start_date   => p_start_date
7567                    ,p_end_date     => p_end_date
7568                    ,p_purpose_code => p_purpose_code
7569                 )
7570             LOOP
7571               IF inc_rec.income_date > l_last_accrued_date
7572               THEN
7573                 print(l_prog_name, inc_rec.income_date || '|' || inc_rec.income_amount );
7574                 -- Store it in the Income Streams Tbl
7575                 income_streams_tbl(inx_tot_inc).income_date   := inc_rec.income_date;
7576                 income_streams_tbl(inx_tot_inc).income_amount := inc_rec.income_amount;
7577                 -- Calculate the Total Income Amount
7578                 ln_tot_inc_amount := ln_tot_inc_amount + income_streams_tbl(inx_tot_inc).income_amount;
7579                 -- Increment the inx_tot_inc index
7580                 inx_tot_inc := inx_tot_inc + 1;
7581               END IF;
7582             END LOOP;
7583             -- Calculate the Income Proportions for the Income Streams fetched above
7584             print(l_prog_name, 'Calculating the Income Proporitions for the Income Streams ' );
7585             IF income_streams_tbl.COUNT > 0
7586             THEN
7587               print(l_prog_name, 'Date | Amount | Total Income Amount | Income Proportion ' );
7588               FOR inx_tot_inc IN income_streams_tbl.FIRST .. income_streams_tbl.LAST
7589               LOOP
7590                 --Added by bkatraga for bug 12413695
7591                 IF(ln_tot_inc_amount = 0) THEN
7592                   income_streams_tbl(inx_tot_inc).income_proportion := 0;
7593                 ELSE
7594                 --end bkatraga
7595                   income_streams_tbl(inx_tot_inc).income_proportion :=
7596                     income_streams_tbl(inx_tot_inc).income_amount / ln_tot_inc_amount;
7597                 END IF;  --Added by bkatraga for bug 12413695
7598 
7599                 print(l_prog_name, income_streams_tbl(inx_tot_inc).income_date || '|' ||
7600                                    income_streams_tbl(inx_tot_inc).income_amount || '|' ||
7601                                    ln_tot_inc_amount || '|' ||
7602                                    income_streams_tbl(inx_tot_inc).income_proportion );
7603               END LOOP;
7604             END IF; -- IF income_streams_tbl.COUNT > 0
7605           END IF;
7606 
7607           print(l_prog_name, '**************************************************' );
7608           print(l_prog_name, '******** Using Spread By Income Algorithm ********' );
7609           print(l_prog_name, '**************************************************' );
7610 
7611           -- Prospectively Adjusting those Accrual Streams
7612           --   which are generated using the Spread By Income Logic
7613           IF l_flip_prb_rbk_reason = 'NO_REASON'
7614           THEN
7615             print( l_prog_name, 'WORK: Total Accurual Income Amount = ' || ln_tot_work_acc_amt );
7616             print( l_prog_name, 'CURR: Total Accrual Amount before Last Accrued Date = ' || ln_curr_acc_amount );
7617             -- Calculating the Remaining Accrual Amount to be Spread after the Rebook Effective Date
7618             l_acc_adjustment := ln_tot_work_acc_amt - ln_curr_acc_amount;
7619             print(l_prog_name, 'Date | WORK: SEL Amount | Income Amount | Income Total | Income Proportion | ' ||
7620 						                   'CURR: SEL Amount | Prosp. Adjusted Amount');
7621             FOR inx_work IN l_work_acc_strms_tbl.FIRST .. l_work_acc_strms_tbl.LAST
7622             LOOP
7623               -- For reporting purposes store the current stream element amount
7624               ln_amt_before_adjustment :=  l_work_acc_strms_tbl(inx_work).se_amount;
7625               -- Step : {Prospective Adjustment}
7626               --  For Stream Element whose Date <= Last Accrued Date, update the amount
7627               --   with that of the CURR Accrual Amount.
7628               -- If the Stream Element is Accrued on or Before the Last Accrued Date
7629               IF l_work_acc_strms_tbl(inx_work).se_date <= l_last_accrued_date
7630               THEN
7631                 FOR inx_curr IN l_curr_acc_strms_tbl.FIRST .. l_curr_acc_strms_tbl.LAST
7632                 LOOP
7633                   IF TRUNC( l_curr_acc_strms_tbl(inx_curr).se_date ) =
7634                      TRUNC( l_work_acc_strms_tbl(inx_work).se_date )
7635                   THEN
7636                     l_work_acc_strms_tbl(inx_work).se_amount := l_curr_acc_strms_tbl(inx_curr).se_amount ;
7637                     print(l_prog_name,
7638                        l_work_acc_strms_tbl(inx_work).se_date || '|' ||  -- SEL Date
7639                        ln_amt_before_adjustment || ' | ' || -- WORK SEL Amount
7640                        ' - ' || ' | ' ||  -- Income Amount
7641                        ' - ' || ' | ' ||  -- Income Total
7642                        ' - ' || ' | ' ||  -- Income Proportion
7643                        l_curr_acc_strms_tbl(inx_curr).se_amount || ' | ' ||  -- CURR SEL Amount
7644                        l_work_acc_strms_tbl(inx_work).se_amount -- Prospectively Adjusted SEL Amount
7645                     );
7646                     EXIT; -- Break it now, no further iterations required
7647                   END IF;
7648                 END LOOP;
7649 
7650               ELSE
7651                 -- Stream Element has been accrued after the Last Accrued Date
7652                 FOR inx_tot_inc IN income_streams_tbl.FIRST .. income_streams_tbl.LAST
7653                 LOOP
7654                   IF income_streams_tbl(inx_tot_inc).income_date =
7655                      l_work_acc_strms_tbl(inx_work).se_date
7656                   THEN
7657                     -- Modify the Accrual Amount as Income Proportion *
7658                     --  Remaining Accrual Amount to Be Spread
7659                     l_work_acc_strms_tbl(inx_work).se_amount :=
7660                       income_streams_tbl(inx_tot_inc).income_proportion * l_acc_adjustment;
7661                     print(l_prog_name,
7662                            l_work_acc_strms_tbl(inx_work).se_date || '|' ||  -- SEL Date
7663                            ln_amt_before_adjustment || ' | ' || -- WORK SEL Amount
7664                            income_streams_tbl(inx_tot_inc).income_amount || ' | ' ||  -- Income Amount
7665                            ln_tot_inc_amount || ' | ' ||  -- Income Total
7666                            income_streams_tbl(inx_tot_inc).income_proportion || ' | ' ||  -- Income Proportion
7667                            '_' || ' | ' ||  -- CURR SEL Amount
7668                            l_work_acc_strms_tbl(inx_work).se_amount -- Prospectively Adjusted SEL Amount
7669                     );
7670                     EXIT; -- Break it now, no further iterations required
7671                   END IF;
7672                 END LOOP; -- End Loop on the income_streams_tbl.
7673               END IF; -- If stream element date <= Last Accured Date
7674 
7675               --Bug# 12596925
7676               j := j + 1;
7677               l_selv_tbl(j).id := l_work_acc_strms_tbl(inx_work).sel_id;
7678               l_selv_tbl(j).stream_element_date := l_work_acc_strms_tbl(inx_work).se_date;
7679               l_selv_tbl(j).amount := l_work_acc_strms_tbl(inx_work).se_amount;
7680               l_selv_tbl(j).stm_id := l_work_acc_strms_tbl(inx_work).stm_id;
7681 
7682               /*-- Store this updated amount and ids for bulk updation later
7683               l_sel_id_tbl(i)     := l_work_acc_strms_tbl(inx_work).sel_id;
7684               l_stm_id_tbl(i)     := l_work_acc_strms_tbl(inx_work).stm_id;
7685               l_sel_amt_tbl(i)    := l_work_acc_strms_tbl(inx_work).se_amount;
7686               -- Increment the index
7687               i := i + 1;*/
7688             END LOOP; -- FOR inx_work IN l_work_acc_strms_tbl.FIRST ..
7689           END IF;
7690           print(l_prog_name, '****** Done: Using Spread By Income Algorithm ****' );
7691         ELSE
7692           -- Prospectively Adjusting those Accrual Streams
7693           --   which are generated using the Straight Line Algorithm
7694           -- Calculate the Total Accrual Adjustment that needs to be proportionately distributed
7695           print(l_prog_name, '**************************************************' );
7696           print(l_prog_name, '********** Using Straight Line Algorithm *********' );
7697           print(l_prog_name, '**************************************************' );
7698           l_acc_adjustment := NVL( ( ln_work_acc_amount - ln_curr_acc_amount ), 0 );
7699           print(l_prog_name, 'SUM(Acc Before Revision) | SUM(Acc After Revision) | Adjustment Amount' );
7700           print(l_prog_name, ln_curr_acc_amount || ' | ' || ln_work_acc_amount || ' | ' || l_acc_adjustment );
7701 
7702           IF NVL(l_acc_adjustment,0) = 0
7703           THEN
7704              l_flip_prb_rbk_reason  := 'NOTHING_TO_ADJUST';
7705           END IF;
7706 
7707           IF l_flip_prb_rbk_reason = 'NO_REASON'
7708           THEN
7709             -- Rebook Effective Day is the next of the Last Accrued Day
7710             l_rebook_eff_date := l_last_accrued_date + 1;
7711             -- End Date: Considered it as Minimum (Last Accrual Element Date, Contract End Date)
7712             -- l_term_end_date := TRUNC(p_end_date);
7713             -- IF  TRUNC(l_work_acc_strms_tbl(l_work_acc_strms_tbl.LAST).se_date) < l_term_end_date
7714             -- THEN
7715             --  l_term_end_date := TRUNC(l_work_acc_strms_tbl(l_work_acc_strms_tbl.LAST).se_date);
7716             -- END IF;
7717             l_term_end_date := TRUNC(p_end_date);
7718             IF l_stm_purpose IN (
7719                   'RENT_ACCRUAL' )
7720             THEN
7721               -- As part of the bug 9305846, Rental accruals are always generated
7722               --  till the end of the contract term
7723               l_term_end_date := TRUNC(p_end_date);
7724             ELSIF l_stm_purpose IN (
7725                   'ACCRUED_FEE_INCOME'
7726                  ,'SERVICE_INCOME'
7727                  ,'PASS_THRU_REV_ACCRUAL' )
7728             THEN
7729               print(l_prog_name, 'get_payment_sll_dtls_csr: Fetching the Payment Level Details for (khr_id, kle_id)= ('
7730                     || p_khr_id || ' , ' || l_kle_id || ')' );
7731               --  Fetch the End Date for the Last Payment Level of the RENT/Fee Payment/Service Payment
7732               --  and consider that as the l_term_end_date
7733               FOR t_sll_rec IN get_payment_sll_dtls_csr(
7734                               p_khr_id => p_khr_id
7735                              ,p_kle_id => l_kle_id )
7736               LOOP
7737                 l_term_end_date := t_sll_rec.end_date;
7738               END LOOP;
7739             ELSIF l_stm_purpose IN ( 'ACCRUED_FEE_EXPENSE' )
7740             THEN
7741               print(l_prog_name, 'get_exp_end_date_csr: Fetching the Payment Level Details for (khr_id, kle_id)= ('
7742                     || p_khr_id || ' , ' || l_kle_id || ')' );
7743               --  Fetch the End Date for the Last Payment Level of the RENT/Fee Payment/Service Payment
7744               --  and consider that as the l_term_end_date
7745               FOR t_exp_rec IN get_exp_end_date_csr(
7746                               p_khr_id => p_khr_id
7747                              ,p_kle_id => l_kle_id )
7748               LOOP
7749                 l_term_end_date := t_exp_rec.end_date;
7750                 print(l_prog_name,'Line Type = Fee. Fee Type =  ' || t_exp_rec.fee_type
7751                       || ' Start Date = ' || t_exp_rec.start_date
7752                       || 'Month
7753                       nn:/s/Period = ' || t_exp_rec.mpp
7754                       || 'Periods = '  || t_exp_rec.periods
7755                       || 'Amouont = '  || t_exp_rec.amount
7756                       || 'End Date = ' || t_exp_rec.end_date);
7757               END LOOP;
7758             END IF;
7759             -- Step: Calculate the Number of the Remaining Days
7760             --       For time being assuming that the end is till Contract End Date
7761             l_rem_days := OKL_PRICING_UTILS_PVT.get_day_count(
7762                              p_start_date    => l_rebook_eff_date
7763                             ,p_days_in_month => l_day_convention_month
7764                             ,p_days_in_year  => l_day_convention_year
7765                             ,p_end_date      => l_term_end_date
7766                             ,p_arrears       => 'Y'
7767                             ,x_return_status => x_return_status);
7768             IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
7769               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7770             ELSIF (x_return_status = G_RET_STS_ERROR) THEN
7771               RAISE OKL_API.G_EXCEPTION_ERROR;
7772             END IF;
7773             print(l_prog_name, 'Rebook Effective Date | End Date | Days Remaining' );
7774             print(l_prog_name,l_rebook_eff_date ||' | ' || l_term_end_date || ' | ' || l_rem_days);
7775 
7776             IF l_rem_days IS NULL OR l_rem_days <= 0
7777             THEN
7778               l_flip_prb_rbk_reason := 'INVALID_REM_DAYS';
7779               print(l_prog_name,'Invalid # of Remaining Days ' || l_rem_days );
7780             END IF;
7781           END IF;
7782           -- Check and Proceed
7783           IF l_flip_prb_rbk_reason = 'NO_REASON'
7784           THEN
7785             -- Step Modify the WORK Accrual Stream to the CURR Accrual Stream Amount
7786             -- If Accrual is before the Last Accrual Date:
7787             --     then copy the Old Accrual Amount
7788             -- If Accrual is after the Last Accrual Date:
7789             --     then Add the accrual adjustment amount also
7790             print(l_prog_name, '# | Start Date | End Date | Days/Period | Total Days | Amount | Adj Amount | Modified Amount | Cummulative Adj Total');
7791             inx_work := 1;
7792             l_tot_per_adjustment := 0;
7793             FOR inx_work IN l_work_acc_strms_tbl.FIRST .. l_work_acc_strms_tbl.LAST
7794             LOOP
7795               -- Derieve the Period Start Date. First of the Current Accrual Month.
7796               l_per_start_date :=
7797                 LAST_DAY( ADD_MONTHS(l_work_acc_strms_tbl(inx_work).se_date, -1) ) + 1;
7798               -- Derieve the Period End Date
7799               l_per_end_date := l_work_acc_strms_tbl(inx_work).se_date;
7800               IF inx_work = l_work_acc_strms_tbl.LAST
7801               THEN
7802                 -- Last Accrual Stream Element
7803                 l_per_end_date := l_term_end_date;
7804               END IF;
7805               IF l_work_acc_strms_tbl(inx_work).se_date <= l_last_accrued_date
7806               THEN
7807                 l_un_modified_amount := l_work_acc_strms_tbl(inx_work).se_amount;
7808                 -- Accrual Element is On or Before the Last Accrual Date
7809                 -- Find the Accrual Amount before revision and update the amount on it
7810                 FOR t_in IN l_curr_acc_strms_tbl.FIRST .. l_curr_acc_strms_tbl.LAST
7811                 LOOP
7812                   IF l_work_acc_strms_tbl(inx_work).se_date = l_curr_acc_strms_tbl(t_in).se_date
7813                   THEN
7814                     -- Modify the Work Accrual Stream Amount to as that of the Current one
7815                     l_work_acc_strms_tbl(inx_work).se_amount := l_curr_acc_strms_tbl(t_in).se_amount;
7816                     EXIT;
7817                   END IF;
7818                 END LOOP; -- Loop on the l_curr_acc_strms_tbl
7819                 print(l_prog_name, inx_work || ' | ' || l_per_start_date || ' | ' || l_per_end_date || ' | ' ||
7820                       '-  | -  | ' || l_un_modified_amount
7821                       || ' | - | ' || l_work_acc_strms_tbl(inx_work).se_amount
7822                       || ' | - | ' );
7823               ELSE
7824                 -- Accrual Amount after the Last Accrued Date
7825                 l_per_days := OKL_PRICING_UTILS_PVT.get_day_count(
7826                                p_start_date    => l_per_start_date
7827                               ,p_days_in_month => l_day_convention_month
7828                               ,p_days_in_year  => l_day_convention_year
7829                               ,p_end_date      => l_per_end_date
7830                               ,p_arrears       => 'Y'
7831                               ,x_return_status => x_return_status);
7832                 IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
7833                   RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7834                 ELSIF (x_return_status = G_RET_STS_ERROR) THEN
7835                   RAISE OKL_API.G_EXCEPTION_ERROR;
7836                 END IF;
7837                 l_per_adjustment := l_acc_adjustment * l_per_days / l_rem_days;
7838 
7839                 -- Store the Accrual Adjustment prior modifications for reporting purposes
7840                 l_un_modified_amount := l_work_acc_strms_tbl(inx_work).se_amount;
7841                 IF inx_work = l_work_acc_strms_tbl.LAST
7842                 THEN
7843                   -- Calculate the Remaining Accrual Adjustment Amount
7844                   l_per_rounded_adjustment := l_acc_adjustment - l_tot_per_adjustment;
7845                   -- Add the remaining Accrual Adjustment to the Last Accrual Stream Element
7846                   l_work_acc_strms_tbl(inx_work).se_amount := l_work_acc_strms_tbl(inx_work).se_amount
7847                     + l_per_rounded_adjustment;
7848                 ELSE
7849                   -- Round the Accrual Adjustment Amount
7850                   okl_accounting_util.round_amount(
7851                      p_api_version   => p_api_version
7852                     ,p_init_msg_list => OKL_API.G_FALSE
7853                     ,x_return_status => x_return_status
7854                     ,x_msg_count     => x_msg_count
7855                     ,x_msg_data      => x_msg_data
7856                     ,p_amount        => l_per_adjustment
7857                     ,p_currency_code => p_currency_code
7858                     ,p_round_option  => 'STM'
7859                     ,x_rounded_amount => l_per_rounded_adjustment);
7860                   IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
7861                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7862                   ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
7863                     RAISE OKL_API.G_EXCEPTION_ERROR;
7864                   END IF;
7865                   -- Add the Accrual Adjustment amount
7866                   l_work_acc_strms_tbl(inx_work).se_amount := l_work_acc_strms_tbl(inx_work).se_amount
7867                     + l_per_rounded_adjustment;
7868                   -- Accumulate the Accrual Adjustment Amount
7869                   l_tot_per_adjustment := l_tot_per_adjustment + l_per_rounded_adjustment;
7870                 END IF;
7871                 print(l_prog_name, inx_work || ' | ' || l_per_start_date || ' | ' || l_per_end_date || ' | ' ||
7872                       l_per_days || ' | ' || l_rem_days || ' | ' || l_un_modified_amount
7873                       || ' | ' || l_per_rounded_adjustment || ' | ' || l_work_acc_strms_tbl(inx_work).se_amount
7874                       || ' | ' || l_tot_per_adjustment );
7875               END IF;
7876               -- Store this updated amount and ids for bulk updation later
7877               l_sel_id_tbl(i)     := l_work_acc_strms_tbl(inx_work).sel_id;
7878               l_stm_id_tbl(i)     := l_work_acc_strms_tbl(inx_work).stm_id;
7879               l_sel_amt_tbl(i)    := l_work_acc_strms_tbl(inx_work).se_amount;
7880               -- Increment the index
7881               i := i + 1;
7882             END LOOP; -- Loop on the l_work_acc_strms_tbl
7883           END IF;
7884           print(l_prog_name, '******* Done: Using Straight Line Algorithm ******' );
7885         END IF; -- IF: Split on the Spread By Income/Straight Line Logic
7886 
7887         --Bug# 12596925
7888         -- Step:
7889         --   Perform rounding for stream elements adjusted using Spread By Income logic
7890         print(l_prog_name, 'Total Number of Stream Elements to be rounded ' || l_selv_tbl.COUNT );
7891         IF l_selv_tbl.COUNT > 0 THEN
7892 
7893           for k in l_selv_tbl.first..l_selv_tbl.last loop
7894             print(l_prog_name, l_selv_tbl(k).stream_element_date
7895                   || '|' || l_selv_tbl(k).amount
7896                   || '|' || l_selv_tbl(k).stm_id);
7897           end loop;
7898           x_return_status := Okl_Streams_Util.round_streams_amount(
7899                                 p_api_version   => p_api_version,
7900                                 p_init_msg_list => OKL_API.G_FALSE,
7901                                 x_msg_count     => x_msg_count,
7902                                 x_msg_data      => x_msg_data,
7903                                 p_chr_id        => p_khr_id,
7904                                 p_selv_tbl      => l_selv_tbl,
7905                                 x_selv_tbl      => lx_selv_tbl);
7906 
7907           print(l_prog_name, 'After calling Okl_Streams_Util.round_streams_amount' || x_return_status);
7908 
7909           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
7910               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7911           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
7912               RAISE OKL_API.G_EXCEPTION_ERROR;
7913           END IF;
7914 
7915           print(l_prog_name, 'Total Number of Stream Elements rounded ' || lx_selv_tbl.COUNT );
7916 
7917           FOR k IN lx_selv_tbl.FIRST..lx_selv_tbl.LAST LOOP
7918             -- Store this updated amount and ids for bulk updation later
7919             l_sel_id_tbl(i)     := lx_selv_tbl(k).id;
7920             l_stm_id_tbl(i)     := lx_selv_tbl(k).stm_id;
7921             l_sel_amt_tbl(i)    := lx_selv_tbl(k).amount;
7922             -- Increment the index
7923             i := i + 1;
7924           END LOOP;
7925 
7926         END IF;
7927 
7928       END LOOP; -- Loop on the eligible Accrual Streams
7929 
7930       -- Step:
7931       --   For all the updated streams, perform the bulk update in one go
7932       print(l_prog_name, 'Total Number of Stream Elements to be updated ' || l_sel_id_tbl.COUNT );
7933       IF l_sel_id_tbl.COUNT > 0
7934       THEN
7935         print(l_prog_name, 'Start: Performaing the Bulk Update of ' || l_sel_id_tbl.COUNT || ' Streams. '
7936                            || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
7937         -- Bulk update
7938         FORALL i in l_sel_id_tbl.FIRST .. l_sel_id_tbl.LAST
7939            UPDATE okl_strm_elements
7940               SET amount = l_sel_amt_tbl(i)
7941             WHERE id     = l_sel_id_tbl(i)
7942               AND stm_id = l_stm_id_tbl(i);
7943         print(l_prog_name, 'Done: Performaing the Bulk Update of ' || l_sel_id_tbl.COUNT || ' Streams '
7944                            || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') );
7945       END IF;
7946 
7947     ELSE
7948       print(l_prog_name, 'Switched Back to Retrospective Method. Reason= ' || l_flip_prb_rbk_reason);
7949     END IF;  -- If l_flip_prb_rbk_reason = 'NO_REASON'
7950     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data  => x_msg_data);
7951     print( l_api_name, 'end' );
7952   EXCEPTION
7953     WHEN OKL_API.G_EXCEPTION_ERROR
7954     THEN
7955       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
7956         p_api_name  => l_api_name,
7957         p_pkg_name  => g_pkg_name,
7958         p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
7959         x_msg_count => x_msg_count,
7960         x_msg_data  => x_msg_data,
7961         p_api_type  => g_api_type);
7962     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR
7963     THEN
7964       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
7965         p_api_name  => l_api_name,
7966         p_pkg_name  => g_pkg_name,
7967         p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
7968         x_msg_count => x_msg_count,
7969         x_msg_data  => x_msg_data,
7970         p_api_type  => g_api_type);
7971 
7972     WHEN OTHERS
7973     THEN
7974       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
7975         p_api_name  => l_api_name,
7976         p_pkg_name  => g_pkg_name,
7977         p_exc_name  => 'OTHERS',
7978         x_msg_count => x_msg_count,
7979         x_msg_data  => x_msg_data,
7980         p_api_type  => g_api_type);
7981   END prosp_adj_acc_strms;
7982 
7983   ---------------------------------------------------------------------------
7984   -- PROCEDURE generate_streams
7985   ---------------------------------------------------------------------------
7986   ---------------------------------------------------------------------------
7987   /*
7988    *  single point entry for streams generation.
7989    *  p_compute_irr : boolean to calculate irr.
7990    *  p_gen.._type  : FULL, SERVICE_ACCRUAL, etc
7991    *  p_repor..class: streams for reporting product. yields are not calculated for report.
7992    */
7993   PROCEDURE generate_streams( p_api_version                IN         NUMBER,
7994                               p_init_msg_list              IN         VARCHAR2,
7995                               p_khr_id                     IN         NUMBER,
7996                               p_compute_rates              IN         VARCHAR2,
7997                               p_generation_type            IN         VARCHAR2,
7998                               p_reporting_book_class       IN         VARCHAR2,
7999                               x_contract_rates             OUT NOCOPY rate_rec_type,
8000                               x_return_status              OUT NOCOPY VARCHAR2,
8001                               x_msg_count                  OUT NOCOPY NUMBER,
8002                               x_msg_data                   OUT NOCOPY VARCHAR2) IS
8003 
8004     lx_return_status              VARCHAR2(1);
8005 
8006     lx_isAllowed                  BOOLEAN;
8007     lx_passStatus                 VARCHAR2(30);
8008     lx_failStatus                 VARCHAR2(30);
8009 
8010 
8011     CURSOR c_hdr IS
8012       SELECT chr.template_yn,
8013              chr.currency_code,
8014              chr.start_date,
8015              chr.end_date,
8016              khr.deal_type,
8017              khr.term_duration,
8018              NVL(khr.generate_accrual_yn, 'Y'),
8019 	     nvl(rpar.base_rate, 10) base_rate
8020       FROM   okc_k_headers_b chr,
8021              okl_k_headers khr,
8022              OKL_K_RATE_PARAMS rpar
8023       WHERE  khr.id = p_khr_id
8024         AND  chr.id = khr.id
8025 	AND  rpar.khr_id(+) = khr.id;
8026 
8027     l_hdr                    c_hdr%ROWTYPE;
8028     l_deal_type              VARCHAR2(30);
8029     l_purpose_code           VARCHAR2(30);
8030 
8031     p_compute_irr VARCHAR2(1) :=  p_compute_rates;
8032 
8033     CURSOR c_niv (kleId NUMBER) IS
8034       SELECT FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
8035              TO_NUMBER(rul2.rule_information8) stub_amount
8036       FROM   okc_rule_groups_b rgp,
8037              okc_rules_b rul1,
8038              okc_rules_b rul2,
8039              okc_k_lines_b cle,
8040              okl_k_lines kle,
8041              okc_line_styles_b lse,
8042 	     okl_strm_type_b sty
8043       WHERE  rul2.dnz_chr_id = p_khr_id
8044         AND  rul2.rule_information_category = 'LASLL'
8045         AND  rul2.rgp_id = rgp.id
8046         AND  TO_NUMBER(rul2.object2_id1) = rul1.id
8047         AND  rgp.cle_id = cle.id
8048         AND  cle.sts_code = 'TERMINATED'
8049         AND  cle.id = kle.id
8050         AND  cle.lse_id = lse.id
8051 	AND  cle.id = kleId
8052 	AND  sty.id = rul1.object1_id1
8053 	AND  sty.stream_type_purpose = 'TERMINATION_VALUE';
8054 
8055     r_niv c_niv%ROWTYPE;
8056 
8057     -- Bug 12938353: RGOOTY: Modified
8058     CURSOR c_inflows
8059     IS
8060       -- This part of the cursor fetches only the Income Fee and its Origination Income Details
8061       --  so that ISG uses this information and generate the Amortized Fee Income Streams
8062       --  for the Income Fees
8063       SELECT rgp.cle_id cle_id,
8064              TO_NUMBER(rul1.object1_id1) sty_id,
8065              sty.stream_type_purpose,
8066              FND_DATE.canonical_to_date(cle.start_date) start_date,
8067              NULL periods,
8068              NULL frequency,
8069              NULL structure,
8070              NULL advance_arrears,
8071              NULL amount,
8072              NULL stub_days,
8073              NULL stub_amount,
8074              lse.lty_code lty_code,
8075              NULL capital_amount,
8076              NULL residual_value,
8077              kle.fee_type fee_type,
8078              kle.origination_income origination_income,
8079              kle.amount   fee_amount,
8080              'INCOME_AMORT'  fee_line_level_type
8081       FROM   okc_k_lines_b cle,
8082              okl_k_lines kle,
8083              okc_line_styles_b lse,
8084              okl_strm_type_b sty,
8085              okc_rule_groups_b rgp,
8086              okc_rules_b rul1
8087       WHERE
8088              rul1.dnz_chr_id = p_khr_id
8089         AND  rul1.rule_information_category = 'LASLH'
8090         AND  rul1.rgp_id = rgp.id
8091         AND  rgp.cle_id = cle.id
8092         AND  cle.sts_code IN ('PASSED', 'COMPLETE', 'TERMINATED')
8093         AND  cle.id = kle.id
8094         AND  cle.lse_id = lse.id
8095 	      AND  sty.id = rul1.object1_id1
8096         AND  kle.fee_type = 'INCOME'
8097         AND  kle.origination_income IS NOT NULL
8098      UNION
8099       -- This part of the cursor will continue to fetch the Payments information for all fees
8100       --  so that ISG uses this information and generate the Accrued Fee Income Streams
8101       SELECT rgp.cle_id cle_id,
8102              TO_NUMBER(rul1.object1_id1) sty_id,
8103              sty.stream_type_purpose,
8104              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
8105              TO_NUMBER(rul2.rule_information3) periods,
8106              rul2.object1_id1 frequency,
8107              rul2.rule_information5 structure,
8108              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
8109              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
8110              TO_NUMBER(rul2.rule_information7) stub_days,
8111              TO_NUMBER(rul2.rule_information8) stub_amount,
8112              lse.lty_code lty_code,
8113              kle.capital_amount capital_amount,
8114              kle.residual_value residual_value,
8115              kle.fee_type fee_type,
8116              kle.origination_income origination_income,
8117              kle.amount   fee_amount,
8118              DECODE(kle.fee_type, 'INCOME', 'INCOME_ACCRUED', 'FEE_PAYMENT')  fee_line_level_type
8119       FROM   okc_rule_groups_b rgp,
8120              okc_rules_b rul1,
8121              okc_rules_b rul2,
8122              okc_k_lines_b cle,
8123              okl_k_lines kle,
8124              okc_line_styles_b lse,
8125              okl_strm_type_b sty
8126       WHERE
8127              rul2.dnz_chr_id = p_khr_id
8128         AND  rul2.rule_information_category = 'LASLL'
8129         AND  rul2.rgp_id = rgp.id
8130         AND  TO_NUMBER(rul2.object2_id1) = rul1.id
8131         AND  rgp.cle_id = cle.id
8132         AND  cle.sts_code IN ('PASSED', 'COMPLETE', 'TERMINATED')
8133         AND  cle.id = kle.id
8134         AND  cle.lse_id = lse.id
8135 	      AND  sty.id = rul1.object1_id1
8136       UNION
8137       SELECT TO_NUMBER(NULL) cle_id,
8138              TO_NUMBER(rul1.object1_id1) sty_id,
8139              sty.stream_type_purpose,
8140              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
8141              TO_NUMBER(rul2.rule_information3) periods,
8142              rul2.object1_id1 frequency,
8143              rul2.rule_information5 structure,
8144              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
8145              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
8146              TO_NUMBER(rul2.rule_information7) stub_days,
8147              TO_NUMBER(rul2.rule_information8) stub_amount,
8148              NULL lty_code,
8149              TO_NUMBER(NULL) capital_amount,
8150              TO_NUMBER(NULL) residual_value,
8151              NULL fee_type,
8152              NULL origination_income,
8153              NULL   fee_amount,
8154              'ASSET_ACCRUALS'  fee_line_level_type
8155       FROM   okc_rule_groups_b rgp,
8156              okc_rules_b rul1,
8157              okc_rules_b rul2,
8158              okl_strm_type_b sty
8159       WHERE
8160              rul2.dnz_chr_id = p_khr_id
8161         AND  rul2.rule_information_category = 'LASLL'
8162         AND  rul2.rgp_id = rgp.id
8163         AND  rgp.cle_id IS NULL
8164         AND  sty.id = rul1.object1_id1
8165         AND  TO_NUMBER(rul2.object2_id1) = rul1.id;
8166 
8167     -- Added by RGOOTY. Bug 4403311
8168     TYPE l_inflows_tbl_type IS TABLE OF c_inflows%ROWTYPE
8169       INDEX BY BINARY_INTEGER;
8170     l_inflows_tbl                   l_inflows_tbl_type;
8171     inf_index                       NUMBER;
8172     inf_start_date                  DATE;
8173     l_use_first_pmnt_date           VARCHAR2(1);
8174 
8175     -- Added by RGOOTY. Bug 4403311: End
8176 
8177     CURSOR c_fin_assets IS
8178       SELECT kle.id,
8179              NVL(kle.residual_value, 0) residual_value,
8180              cle.start_date,
8181 	     cle.sts_code,
8182 	     kle.fee_type,
8183 	     lse.lty_code,
8184 	     kle.amount,
8185 	     kle.capital_amount,
8186 	     kle.capitalized_interest
8187       FROM   okc_k_lines_b cle,
8188              okl_k_lines kle,
8189              okc_line_styles_b lse
8190       WHERE  cle.dnz_chr_id = p_khr_id
8191         -- 12944979: For Terminated lines, copy_accrual_streams_isg will syncup
8192         AND  cle.sts_code IN ('PASSED', 'COMPLETE') --, 'TERMINATED')
8193         AND  cle.lse_id = lse.id
8194         AND  (lse.lty_code = 'FREE_FORM1' OR
8195 	      kle.fee_type = 'FINANCED' OR
8196 	      kle.fee_type = 'ROLLOVER' OR
8197 	      lse.lty_code = 'LINK_FEE_ASSET')
8198         AND  cle.id = kle.id;
8199 
8200     top_svc_rec top_svc_csr%ROWTYPE;
8201 
8202     Cursor c_rollover_pmnts( chrId NUMBER, kleId NUMBER ) IS
8203     Select nvl(slh.object1_id1, -1) styId
8204     From   OKC_RULE_GROUPS_B rgp,
8205            OKC_RULES_B sll,
8206            okc_rules_b slh,
8207 	   okl_strm_type_b sty
8208     Where  slh.rgp_id = rgp.id
8209        and rgp.RGD_CODE = 'LALEVL'
8210        and sll.RULE_INFORMATION_CATEGORY = 'LASLL'
8211        and slh.RULE_INFORMATION_CATEGORY = 'LASLH'
8212        AND TO_CHAR(slh.id) = sll.object2_id1
8213        and rgp.dnz_chr_id = chrId
8214        and rgp.cle_id = kleId
8215        and sty.id = to_number(slh.object1_id1)
8216        and sty.stream_type_purpose NOT IN ('ESTIMATED_PROPERTY_TAX', 'UNSCHEDULED_PRINCIPAL_PAYMENT', 'DOWN_PAYMENT');
8217        --Added DOWN_PAYMENT by rgooty for bug 7536131
8218        --bug# 4092324 bug# 4122385
8219 
8220     r_rollover_pmnts c_rollover_pmnts%ROWTYPE;
8221 
8222     Cursor c_link_pmnts( chrId NUMBER, kleId NUMBER ) IS
8223     Select nvl(slh.object1_id1, -1) styId
8224     From   OKC_RULE_GROUPS_B crg,
8225            OKC_RULES_B crl,
8226            okc_rules_b slh,
8227            okc_K_lines_b cle_lnk,
8228 	   okl_K_lines kle_roll
8229     Where  slh.rgp_id = crg.id
8230        and crg.RGD_CODE = 'LALEVL'
8231        and crl.RULE_INFORMATION_CATEGORY = 'LASLL'
8232        and slh.RULE_INFORMATION_CATEGORY = 'LASLH'
8233        AND TO_CHAR(slh.id) = crl.object2_id1
8234        and crg.dnz_chr_id = chrId
8235        and crg.cle_id = kleId
8236        and crg.cle_id = cle_lnk.id
8237        and cle_lnk.cle_id = kle_roll.id
8238        and kle_roll.fee_type in ('ROLLOVER', 'FINANCED');
8239 
8240     r_link_pmnts c_link_pmnts%ROWTYPE;
8241 
8242     CURSOR c_pt_yn (p_cle_id NUMBER) IS
8243     Select 'Y'
8244     from dual
8245     where Exists (
8246     select vDtls.DISBURSEMENT_BASIS,
8247            vDtls.DISBURSEMENT_FIXED_AMOUNT,
8248 	   vDtls.DISBURSEMENT_PERCENT,
8249 	   vDtls.PROCESSING_FEE_BASIS,
8250 	   vDtls.PROCESSING_FEE_FIXED_AMOUNT,
8251 	   vDtls.PROCESSING_FEE_PERCENT
8252     from okl_party_payment_hdr vHdr,
8253          okl_party_payment_dtls vDtls
8254     where vDtls.payment_hdr_id = vHdr.id
8255       and vHdr.CLE_ID = p_cle_id);
8256 
8257     CURSOR c_financed_fees IS
8258     SELECT kle.id,
8259            nvl(kle.fee_type, 'LINK_FEE_ASSET') fee_type,
8260 	   lse.lty_code
8261     FROM   okc_k_lines_b cle,
8262            okl_k_lines kle,
8263            okc_line_styles_b lse
8264     WHERE  cle.dnz_chr_id = p_khr_id
8265       AND  cle.sts_code IN ('PASSED', 'COMPLETE', 'TERMINATED')
8266       AND  cle.id = kle.id
8267       AND  cle.lse_id = lse.id
8268       AND  (kle.fee_type='FINANCED' OR kle.fee_type='ROLLOVER' OR lse.lty_code='LINK_FEE_ASSET');
8269 
8270     r_financed_fees c_financed_fees%ROWTYPE;
8271 
8272     l_primary_sty_id         NUMBER;
8273 
8274     l_pt_yn                  VARCHAR2(1);
8275     l_passthrough_id         NUMBER;
8276 
8277     l_sty_id                 NUMBER;
8278     l_sty_name               VARCHAR2(150);
8279     l_mapped_sty_name        VARCHAR2(150);
8280 
8281     l_pre_tax_inc_id         NUMBER;
8282     l_principal_id           NUMBER;
8283     l_interest_id            NUMBER;
8284     l_prin_bal_id            NUMBER;
8285     l_termination_id         NUMBER;
8286 
8287     l_selv_tbl               okl_streams_pub.selv_tbl_type;
8288     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
8289     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
8290 
8291     l_stmv_rec               okl_streams_pub.stmv_rec_type;
8292     l_pt_rec                 okl_streams_pub.stmv_rec_type;
8293     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
8294 
8295     -- Loan Amortization
8296     l_principal_tbl          okl_streams_pub.selv_tbl_type;
8297     l_interest_tbl           okl_streams_pub.selv_tbl_type;
8298     l_prin_bal_tbl           okl_streams_pub.selv_tbl_type;
8299     l_termination_tbl        okl_streams_pub.selv_tbl_type;
8300     l_pre_tax_inc_tbl        okl_streams_pub.selv_tbl_type;
8301 
8302     l_capital_cost           NUMBER;
8303     l_interim_interest       NUMBER;
8304     l_interim_days           NUMBER;
8305     l_interim_dpp            NUMBER;
8306     l_asset_iir              NUMBER;
8307     l_asset_guess_iir        NUMBER;
8308     l_bkg_yield_guess        NUMBER;
8309     l_asset_booking_yield    NUMBER;
8310 
8311     l_interim_tbl            OKL_PRICING_PVT.interim_interest_tbl_type;
8312     l_sub_interim_tbl            OKL_PRICING_PVT.interim_interest_tbl_type;
8313     i                        BINARY_INTEGER := 0;
8314 
8315     l_payment_count          BINARY_INTEGER := 0;
8316 
8317     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'generate_streams';
8318 
8319     prof_rate                   VARCHAR2(256);
8320     contract_comments VARCHAR2(256);
8321 
8322     l_recurr_yn  VARCHAR2(1) := NULL;
8323     l_blnHasFinFees VARCHAR2(1) := OKL_API.G_FALSE;
8324 
8325     l_clev_tbl okl_okc_migration_pvt.clev_tbl_type;
8326     l_klev_tbl okl_contract_pub.klev_tbl_type;
8327     x_clev_tbl okl_okc_migration_pvt.clev_tbl_type;
8328     x_klev_tbl okl_contract_pub.klev_tbl_type;
8329 
8330     l_PRE_TAX_IRR            NUMBER;
8331     l_IMPLICIT_INTEREST_RATE NUMBER;
8332     l_SUB_IMPL_INTEREST_RATE NUMBER;
8333     l_SUB_PRE_TAX_IRR        NUMBER;
8334 
8335     Cursor c_subs Is
8336     Select 'Y'
8337     From dual
8338     where Exists(
8339     select kle.id
8340      from  okl_k_lines_full_v kle,
8341            okc_line_styles_b lse,
8342 	   okc_statuses_b sts
8343      where KLE.LSE_ID = LSE.ID
8344           and lse.lty_code = 'SUBSIDY'
8345           and kle.dnz_chr_id = p_khr_id
8346 	  and sts.code = kle.sts_code
8347 	  and sts.ste_code not in ('HOLD', 'EXPIRED', 'CANCELLED'));
8348 
8349     r_subs c_subs%ROWTYPE;
8350     l_subsidies_yn VARCHAR2(1);
8351     l_subsidy_amount  NUMBER;
8352     l_residual_value  NUMBER;
8353 
8354     l_fee_type VARCHAR2(256);
8355 
8356     l_additional_parameters  OKL_EXECUTE_FORMULA_PUB.ctxt_val_tbl_type;
8357 
8358     l_sty_purpose VARCHAR2(256);
8359 
8360 	     l_rent_sty_id NUMBER;
8361 
8362 
8363      CURSOR c_rent_sty_id (finfeesId NUMBER) IS
8364                        SELECT slh.object1_id1
8365                        FROM   okc_rules_b slh,
8366                               okc_rule_groups_b rgp
8367                        WHERE  rgp.dnz_chr_id = p_khr_id
8368                           AND  rgp.cle_id = finfeesId
8369                           AND  rgp.rgd_code= 'LALEVL'
8370                           AND  rgp.id = slh.rgp_id
8371                           AND  slh.rule_information_category = 'LASLH';
8372    -- Added by RGOOTY
8373 
8374 	 -- bug 13013062: cursor added.  get the rent stream type id
8375      CURSOR get_rent_sty_id (p_khr_id NUMBER) IS
8376      SELECT slh.object1_id1
8377        FROM okc_rules_b slh,
8378             okc_rule_groups_b rgp,
8379 			okl_strm_type_b stm
8380       WHERE rgp.dnz_chr_id = p_khr_id
8381         AND rgp.rgd_code= 'LALEVL'
8382         AND rgp.id = slh.rgp_id
8383         AND slh.rule_information_category = 'LASLH'
8384 		AND slh.object1_id1 = stm.id
8385 		AND stm.STREAM_TYPE_PURPOSE = 'RENT';
8386 
8387 
8388    -- Assuming that there are no Services initially.
8389    exec_svc_csr VARCHAR2(1) := 'F';
8390    l_tmp_cle_id NUMBER;
8391    p_get_k_info_csr    OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR%ROWTYPE;
8392 
8393    l_initial_irr NUMBER := 0;
8394    l_asset_count NUMBER := 0;
8395 
8396     -- Added by RGOOTY for perf.: Bug Number 4346646 Start
8397     l_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
8398     l_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
8399 
8400     lx_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
8401     lx_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
8402     -- Added by RGOOTY for perf.: End
8403 
8404 --Added by prasjain for bug 5474827
8405     l_se_id  NUMBER;
8406 --end prasjain
8407     -- Prospective Rebooking Enhancement
8408     -- Applicabke for both ESG and ISG
8409     CURSOR isg_during_rbk_csr( p_khr_id    IN NUMBER)
8410     IS
8411     SELECT rbk_chr.contract_number       rbk_contract_number,
8412            rbk_chr.orig_system_id1       original_chr_id,
8413            trx.rbr_code                  rbk_reason_code,
8414            trx.date_transaction_occurred revision_date,
8415            'ONLINE_REBOOK'               rebook_type
8416            ,rbk_chr.start_date           rbk_chr_start_date
8417            ,orig_chr.start_date          orig_chr_start_date
8418       FROM okc_k_headers_all_b   rbk_chr,
8419            okc_k_headers_all_b   orig_chr,
8420            okl_trx_contracts_all trx
8421      WHERE rbk_chr.id = p_khr_id
8422        AND rbk_chr.orig_system_source_code = 'OKL_REBOOK'
8423        AND trx.khr_id_new = rbk_chr.id
8424        AND trx.tsu_code = 'ENTERED'
8425        AND trx.tcn_type = 'TRBK'
8426        AND rbk_chr.orig_system_id1 = orig_chr.id
8427     UNION
8428     SELECT orig_chr.contract_number       rbk_contract_number,
8429            orig_chr.id                    original_chr_id,
8430            trx.rbr_code                   rbk_reason_code,
8431            trx.date_transaction_occurred  revision_date,
8432            'MASS_REBOOK'                  rebook_type
8433            ,orig_chr.start_date           rbk_chr_start_date
8434            ,orig_chr.start_date           orig_chr_start_date
8435       FROM okc_k_headers_all_b orig_chr,
8436            okl_trx_contracts_all trx
8437      WHERE  orig_chr.id    =  p_khr_id
8438       AND  trx.khr_id     =  orig_chr.id
8439       AND  trx.tsu_code   = 'ENTERED'
8440       AND  trx.tcn_type   = 'TRBK'
8441       AND  EXISTS
8442            (
8443             SELECT '1'
8444               FROM okl_rbk_selected_contract rbk_chr
8445              WHERE rbk_chr.khr_id = orig_chr.id
8446                AND rbk_chr.status <> 'PROCESSED'
8447             );
8448 
8449    -- Cursor to fetch the Prospective Rebooking Option from System Options
8450    CURSOR is_prospective_rbk_csr(p_khr_id  NUMBER)
8451    IS
8452    SELECT amort_inc_adj_rev_dt_yn   rbk_prospectively
8453      FROM okl_sys_acct_opts_all     sysop
8454          ,okc_k_headers_b           chr
8455     WHERE sysop.org_id = chr.authoring_org_id;
8456 
8457    l_rebook_date              DATE;
8458    l_prb_orig_khr_id          NUMBER;
8459    l_prosp_rebook_flag        VARCHAR2(3);
8460    l_is_during_rebook_yn      VARCHAR2(3);
8461    l_rebook_type              VARCHAR2(100);
8462    l_orig_khr_start_date      DATE;
8463    l_rbk_khr_start_date       DATE;
8464    l_rental_accr_stream       VARCHAR2(1):= 'N';  --Added by RGOOTY for bug 9305846
8465    -- Bug 12944979: RGOOTY
8466     CURSOR c_chk_tmnt_asset_exists_csr(cp_khr_id IN NUMBER)
8467     IS
8468       SELECT 'EXISTS'
8469         FROM DUAL
8470        WHERE EXISTS
8471              (
8472                SELECT   1
8473                  FROM   okc_k_lines_b cle
8474                 WHERE   cle.dnz_chr_id = cp_khr_id
8475                  -- 12944979: For Terminated lines, copy_accrual_streams_isg will syncup
8476                    AND  cle.sts_code = 'TERMINATED'
8477               );
8478    l_terminated_line_exists   VARCHAR2(1) := 'N';
8479 BEGIN
8480     -- Added by RGOOTY: For Debugging purposes
8481     L_DEBUG_ENABLED := OKL_DEBUG_PUB.CHECK_LOG_ENABLED;
8482 --    print( l_prog_name, 'begin' );
8483     -- Added by RGOOTY: Start
8484     OKL_STREAMS_UTIL.get_acc_options(
8485                             p_khr_id         => p_khr_id,
8486                             x_org_id         => G_ORG_ID,
8487                             x_precision      => G_PRECISION,
8488                             x_currency_code  => G_CURRENCY_CODE,
8489                             x_rounding_rule  => G_ROUNDING_RULE,
8490                             x_apply_rnd_diff => G_DIFF_LOOKUP_CODE,
8491                             x_return_status  => x_return_status);
8492      IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8493         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8494      ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
8495         RAISE OKL_API.G_EXCEPTION_ERROR;
8496      END IF;
8497 
8498     OPEN G_HDR(p_khr_id);
8499     FETCH G_HDR INTO r_hdr;
8500     CLOSE G_HDR;
8501 
8502     OPEN G_ROLLOVER_PMNTS(p_khr_id);
8503     FETCH G_ROLLOVER_PMNTS INTO r_rollover_pmnts;
8504     CLOSE G_ROLLOVER_PMNTS;
8505 
8506     OPEN OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR (p_khr_id);
8507     FETCH OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR INTO p_get_k_info_csr;
8508     CLOSE OKL_ISG_UTILS_PVT.G_GET_K_INFO_CSR;
8509 
8510     -- Added by RGOOTY : End
8511     OPEN  c_hdr;
8512     FETCH c_hdr INTO l_hdr;
8513     CLOSE c_hdr;
8514 
8515     IF p_reporting_book_class IS NOT NULL THEN
8516       l_deal_type    :=  p_reporting_book_class;
8517       l_purpose_code := 'REPORT';
8518     ELSE
8519       l_deal_type    :=  l_hdr.deal_type;
8520       l_purpose_code := '-99';
8521     END IF;
8522 
8523     l_asset_guess_iir := l_hdr.base_rate / 100.0;
8524 
8525     OKL_ISG_UTILS_PVT.validate_strm_gen_template(
8526                                   p_api_version          => p_api_version,
8527                                   p_init_msg_list        => p_init_msg_list,
8528                                   x_return_status        => x_return_status,
8529                                   x_msg_count            => x_msg_count,
8530                                   x_msg_data             => x_msg_data,
8531                                   p_khr_id               => p_khr_id);
8532 
8533      IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8534         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8535      ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
8536         RAISE OKL_API.G_EXCEPTION_ERROR;
8537      END IF;
8538 
8539      -- Get the Prospective Rebooking Option from System Options
8540      l_prosp_rebook_flag := 'N';
8541      FOR t_rec IN is_prospective_rbk_csr( p_khr_id => p_khr_id )
8542      LOOP
8543        l_prosp_rebook_flag := NVL( t_rec.rbk_prospectively, 'N' );
8544      END LOOP;
8545 
8546      IF l_prosp_rebook_flag = 'Y'
8547      THEN
8548        -- Inquire whether the Contract being processed is during Rebook or not
8549        l_is_during_rebook_yn := 'N';
8550        FOR t_rec IN isg_during_rbk_csr( p_khr_id => p_khr_id )
8551        LOOP
8552          l_is_during_rebook_yn := 'Y';
8553          l_rebook_date         := t_rec.revision_date;
8554          l_prb_orig_khr_id     := t_rec.original_chr_id;
8555          l_rebook_type         := t_rec.rebook_type;
8556          l_orig_khr_start_date := TRUNC(t_rec.orig_chr_start_date);
8557          l_rbk_khr_start_date  := TRUNC(t_rec.rbk_chr_start_date);
8558        END LOOP;
8559      END IF;
8560      print( l_prog_name, ' l_prosp_rebook_flag   = ' || l_prosp_rebook_flag );
8561      print( l_prog_name, ' l_is_during_rebook_yn = ' || l_is_during_rebook_yn );
8562      print( l_prog_name, ' l_rebook_date         = ' || l_rebook_date );
8563      print( l_prog_name, ' l_prb_orig_khr_id     = ' || l_prb_orig_khr_id );
8564      print( l_prog_name, ' l_orig_khr_start_date = ' || l_orig_khr_start_date );
8565      print( l_prog_name, ' l_rbk_khr_start_date  = ' || l_rbk_khr_start_date );
8566      IF l_rebook_type = 'ONLINE_REBOOK' AND
8567         l_orig_khr_start_date <> l_rbk_khr_start_date
8568      THEN
8569        -- Case: During Online Revision, Contract Start Date has been Changed
8570        --       Hence, consider this as a Retrospective Case only
8571        l_prosp_rebook_flag := 'N';
8572      END IF;
8573 -- Added by RGOOTY: Start. Not accepting this as its giving problem for ISG Streams
8574 IF ( p_generation_type = 'SERVICE_LINES' OR p_generation_type = 'FULL' )
8575 THEN
8576 
8577 --        print( l_prog_name, ' validating stream templates - done');
8578     generate_cash_flows (     p_api_version          => p_api_version,
8579                               p_init_msg_list        => p_init_msg_list,
8580                               p_khr_id               => p_khr_id,
8581                               p_generation_type      => p_generation_type,
8582                               p_reporting_book_class => p_reporting_book_class,
8583                               x_payment_count        => l_payment_count,
8584                               x_return_status        => x_return_status,
8585                               x_msg_count            => x_msg_count,
8586                               x_msg_data             => x_msg_data,
8587 			      x_se_id                => l_se_id); --Added by prasjain for bug 5474827
8588       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8589         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8590       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
8591         RAISE OKL_API.G_EXCEPTION_ERROR;
8592       END IF;
8593 
8594 END IF;
8595 -- Added by RGOOTY: End
8596 
8597 /*      print( l_prog_name, ' generating streams - done');
8598       print( l_prog_name, '  payment count ' || l_payment_count);
8599 */
8600 
8601     -- cursor to check for existense of financed fee
8602     l_blnHasFinFees := OKL_API.G_FALSE;
8603     l_fee_type := NULL;
8604     OPEN c_financed_fees;
8605     FETCH c_financed_fees INTO r_financed_fees;
8606     IF ( c_financed_fees%FOUND ) Then
8607         l_blnHasFinFees := OKL_API.G_TRUE;
8608 	l_fee_type := r_financed_fees.fee_type;
8609 	--'FINANCED';
8610     End If;
8611     CLOSE c_financed_fees;
8612 
8613     OPEN c_subs;
8614     FETCH c_subs INTO l_subsidies_yn;
8615     CLOSE c_subs;
8616 
8617     IF p_generation_type = 'FULL' THEN
8618       ---------------------------------------------
8619       -- STEP 2: Generate Pre-Tax Income
8620       ---------------------------------------------
8621       get_mapped_stream (p_mapping_type   =>  'PRE-TAX INCOME',
8622             p_line_style     =>  NULL,
8623             p_deal_type      =>  l_deal_type,
8624 			p_fee_type       =>  l_fee_type,
8625 			p_khr_id         =>  p_khr_id,
8626             x_mapped_stream  =>  l_mapped_sty_name,
8627             x_return_status  =>  lx_return_status,
8628             p_hdr            =>  r_hdr,
8629             p_rollover_pmnts =>  r_rollover_pmnts);
8630     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8631         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8632     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8633         RAISE OKL_API.G_EXCEPTION_ERROR;
8634     END IF;
8635 
8636       print( l_prog_name, ' mapped sty name' || l_mapped_sty_name || ' has fees ' || l_blnHasFinFees );
8637 
8638 
8639 
8640 
8641     IF (l_mapped_sty_name IS NOT NULL) OR ( l_blnHasFinFees = OKL_API.G_TRUE ) THEN
8642 --        print( l_prog_name, ' loop calc amort o');
8643 
8644         FOR l_fin_asset IN c_fin_assets LOOP
8645 
8646             r_rollover_pmnts := NULL;
8647             r_link_pmnts := NULL;
8648 
8649 
8650             If ( l_fin_asset.fee_type = 'ROLLOVER' ) Then
8651                 OPEN c_rollover_pmnts( p_khr_id, l_fin_asset.id);
8652                 FETCH c_rollover_pmnts INTO r_rollover_pmnts;
8653                 CLOSE c_rollover_pmnts;
8654                 l_primary_sty_id := nvl(r_rollover_pmnts.styId, -1);
8655             Elsif ( l_fin_asset.lty_code = 'LINK_FEE_ASSET' ) Then
8656                 OPEN c_link_pmnts( p_khr_id, l_fin_asset.id);
8657                 FETCH c_link_pmnts INTO r_link_pmnts;
8658                 CLOSE c_link_pmnts;
8659                 l_primary_sty_id := nvl(r_link_pmnts.styId, -1);
8660             Else
8661                 OPEN c_rollover_pmnts( p_khr_id, l_fin_asset.id);
8662                 FETCH c_rollover_pmnts INTO r_rollover_pmnts;
8663                 CLOSE c_rollover_pmnts;
8664                 l_primary_sty_id := nvl(r_rollover_pmnts.styId, -1);
8665                 l_rent_sty_id := l_primary_sty_id;
8666             End If;
8667 
8668                if (l_fin_asset.lty_code = 'FREE_FORM1') Then
8669                 get_mapped_stream (p_mapping_type   =>  'PRE-TAX INCOME',
8670                         p_line_style     =>  NULL,
8671             			p_primary_sty_id =>  l_primary_sty_id,
8672                         p_deal_type      =>  l_deal_type,
8673             			p_fee_type       =>  NULL,
8674             			p_khr_id         =>  p_khr_id,
8675                         x_mapped_stream  =>  l_mapped_sty_name,
8676                         x_return_status  =>  lx_return_status,
8677                         p_hdr            =>  r_hdr,
8678                         p_rollover_pmnts =>  r_rollover_pmnts);
8679 	       Else
8680                 get_mapped_stream (p_mapping_type   =>  'PRE-TAX INCOME',
8681                         p_line_style     =>  NULL,
8682             			p_primary_sty_id =>  l_primary_sty_id,
8683                         p_deal_type      =>  l_deal_type,
8684             			p_fee_type       =>  l_fee_type,
8685             			p_khr_id         =>  p_khr_id,
8686                         x_mapped_stream  =>  l_mapped_sty_name,
8687                         x_return_status  =>  lx_return_status,
8688                         p_hdr            =>  r_hdr,
8689                         p_rollover_pmnts =>  r_rollover_pmnts);
8690 	       End if;
8691 
8692             IF ( l_fin_asset.lty_code = 'FREE_FORM1' OR
8693                  (l_fin_asset.fee_type = 'FINANCED' AND nvl(r_rollover_pmnts.styId, -1) <> -1) OR
8694                  (l_fin_asset.fee_type = 'ROLLOVER' AND nvl(r_rollover_pmnts.styId, -1) <> -1) OR
8695                  (l_fin_asset.lty_code = 'LINK_FEE_ASSET' and nvl(r_link_pmnts.styId, -1) <> -1)) Then
8696 
8697         print( l_prog_name, ' amortize fee type ' ||   nvl(l_fin_asset.fee_type, 'XXX') );
8698         print( l_prog_name, ' amortize lty code ' ||   nvl(l_fin_asset.lty_code, 'XXX') );
8699         print( l_prog_name, ' rollover  pmnts ' ||   nvl(r_rollover_pmnts.styId, -1) );
8700         print( l_prog_name, ' link  pmnts ' ||   nvl(r_link_pmnts.styId, -1) );
8701         print( l_prog_name, ' l_mapped_sty_name ' ||   l_mapped_sty_name );
8702 
8703                 l_capital_cost := 0;
8704                 IF ( nvl(l_fin_asset.fee_type, 'XXX') = 'FINANCED' ) OR
8705                      ( nvl(l_fin_asset.lty_code, 'XXX') = 'LINK_FEE_ASSET' ) OR
8706                      ( nvl(l_fin_asset.fee_type, 'XXX') = 'ROLLOVER' ) THEN
8707                      l_capital_cost := l_fin_asset.amount;
8708                 ELSIF (l_mapped_sty_name IS NOT NULL) THEN
8709                     l_capital_cost := nvl(l_fin_asset.capital_amount, 0) +
8710                                       nvl(l_fin_asset.capitalized_interest,0);
8711                 END IF;
8712 
8713                 IF ( l_capital_cost > 0 ) THEN
8714                     IF l_fin_asset.sts_code = 'TERMINATED' THEN
8715                         l_residual_value := OKL_AM_UTIL_PVT.get_actual_asset_residual(
8716                                                 p_khr_id => p_khr_id,
8717                                                 p_kle_id => l_fin_asset.id);
8718                     ELSE
8719                         l_residual_value := l_fin_asset.residual_value;
8720                     END IF;
8721                     --print( l_prog_name, ' subsidies_yn ' || l_subsidies_yn );
8722 
8723 
8724                     IF ( l_subsidies_yn = 'Y' ) THEN
8725 
8726                         OKL_SUBSIDY_PROCESS_PVT.get_asset_subsidy_amount(
8727                                 p_api_version   => G_API_VERSION,
8728                                 p_init_msg_list => G_FALSE,
8729                                 x_return_status => lx_return_status,
8730                                 x_msg_data      => x_msg_data,
8731                                 x_msg_count     => x_msg_count,
8732                                 p_asset_cle_id  => l_fin_asset.id,
8733                                 x_subsidy_amount=> l_subsidy_amount);
8734 
8735                                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8736                                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8737                                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8738                                     RAISE OKL_API.G_EXCEPTION_ERROR;
8739                                 END IF;
8740                                 l_capital_cost := l_capital_cost + l_subsidy_amount;
8741                     END IF;
8742 
8743 		    print( l_prog_name, ' lty code ' || l_fin_asset.lty_code );
8744                     print( l_prog_name, ' mapped stream PRE-TAX INCOME ' || l_mapped_sty_name );
8745                     print( l_prog_name, ' prim ' || to_char(l_primary_sty_id) );
8746                     print( l_prog_name, ' deal type  ' || l_deal_type );
8747                     print( l_prog_name, ' fee type  ' || l_fee_type );
8748                     print( l_prog_name, ' count  ' || l_pre_tax_inc_tbl.COUNT);
8749 
8750                     -- Fetch the Relevant Stream Ids first
8751                     -- Modification required for Prospective Rebooking
8752                     --get_sty_details (p_sty_name      => 'PRE-TAX INCOME',
8753                     get_sty_details (p_sty_name       => l_mapped_sty_name,
8754                                       x_sty_id        => l_pre_tax_inc_id,
8755                                       x_sty_name      => l_sty_name,
8756                                       x_return_status => lx_return_status);
8757 
8758                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8759                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8760                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8761                         RAISE OKL_API.G_EXCEPTION_ERROR;
8762                     END IF;
8763 
8764                     OKL_ISG_UTILS_PVT.get_dep_stream_type(
8765                         p_khr_id                => p_khr_id,
8766                         p_deal_type           => l_deal_type,
8767                         p_primary_sty_id =>  l_primary_sty_id,
8768                         p_dependent_sty_purpose => 'INTEREST_PAYMENT',
8769                         x_return_status         => lx_return_status,
8770                         x_dependent_sty_id      => l_interest_id,
8771                         x_dependent_sty_name    => l_sty_name,
8772                         p_get_k_info_rec        => p_get_k_info_csr);
8773 
8774                     print( l_prog_name, ' INTEREST_PAYMENT ' || l_sty_name );
8775 
8776                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8777                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8778                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8779                         RAISE OKL_API.G_EXCEPTION_ERROR;
8780                     END IF;
8781 
8782                     OKL_ISG_UTILS_PVT.get_dep_stream_type(
8783                         p_khr_id                => p_khr_id,
8784                         p_deal_type           => l_deal_type,
8785                         p_primary_sty_id =>  l_primary_sty_id,
8786                         p_dependent_sty_purpose => 'PRINCIPAL_PAYMENT',
8787                         x_return_status         => lx_return_status,
8788                         x_dependent_sty_id      => l_principal_id,
8789                         x_dependent_sty_name    => l_sty_name,
8790                         p_get_k_info_rec        => p_get_k_info_csr);
8791 
8792 
8793                     print( l_prog_name, ' PRINCIPAL_PAYMENT ' || l_sty_name );
8794 
8795                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8796                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8797                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8798                         RAISE OKL_API.G_EXCEPTION_ERROR;
8799                     END IF;
8800 
8801                     OKL_ISG_UTILS_PVT.get_dep_stream_type(
8802                         p_khr_id                => p_khr_id,
8803                         p_deal_type           => l_deal_type,
8804                         p_primary_sty_id =>  l_primary_sty_id,
8805                         p_dependent_sty_purpose => 'PRINCIPAL_BALANCE',
8806                         x_return_status         => lx_return_status,
8807                         x_dependent_sty_id      => l_prin_bal_id,
8808                         x_dependent_sty_name    => l_sty_name,
8809                         p_get_k_info_rec        => p_get_k_info_csr);
8810 
8811                     print( l_prog_name, ' PRINCIPAL_BALANCE ' || l_sty_name );
8812 
8813                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8814                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8815                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8816                         RAISE OKL_API.G_EXCEPTION_ERROR;
8817                     END IF;
8818 
8819                     OKL_ISG_UTILS_PVT.get_dep_stream_type(
8820                         p_khr_id                => p_khr_id,
8821                         p_deal_type           => l_deal_type,
8822                         p_primary_sty_id =>  l_primary_sty_id,
8823                         p_dependent_sty_purpose => 'TERMINATION_VALUE',
8824                         x_return_status         => lx_return_status,
8825                         x_dependent_sty_id      => l_termination_id,
8826                         x_dependent_sty_name    => l_sty_name,
8827                         p_get_k_info_rec        => p_get_k_info_csr);
8828 
8829                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8830                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8831                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8832                         RAISE OKL_API.G_EXCEPTION_ERROR;
8833                     END IF;
8834 
8835 
8836              print( l_prog_name, ' calling loan amort ' );
8837 
8838                     OKL_PRICING_PVT.get_loan_amortization(p_khr_id           => p_khr_id,
8839                                 p_kle_id           => l_fin_asset.id,
8840                                 p_purpose_code     => l_purpose_code,
8841                                 p_investment       => l_capital_cost,
8842                                 p_residual_value   => l_residual_value,
8843                                 p_start_date       => l_hdr.start_date,
8844                                 p_asset_start_date => l_fin_asset.start_date,
8845                                 p_term_duration    => l_hdr.term_duration,
8846                                 p_currency_code    => l_hdr.currency_code,
8847                                 p_deal_type        => l_deal_type,
8848                                 p_asset_iir_guess  => 0, -- bug 12997124
8849                                -- p_asset_iir_guess  => l_asset_guess_iir,
8850                                 p_bkg_yield_guess  => l_bkg_yield_guess,
8851                                 x_principal_tbl    => l_principal_tbl,
8852                                 x_interest_tbl     => l_interest_tbl,
8853                                 x_prin_bal_tbl     => l_prin_bal_tbl,
8854                                 x_termination_tbl  => l_termination_tbl,
8855                                 x_pre_tax_inc_tbl  => l_pre_tax_inc_tbl,
8856                                 x_interim_interest => l_interim_interest,
8857                                 x_interim_days     => l_interim_days,
8858                                 x_interim_dpp      => l_interim_dpp,
8859                                 x_iir              => l_asset_iir,
8860                                 x_booking_yield    => l_asset_booking_yield,
8861                                 x_return_status    => lx_return_status,
8862                                 p_se_id            => l_se_id
8863                                 -- parameters added for Prospective Rebooking Enhancement
8864                                 ,p_during_rebook_yn   => l_is_during_rebook_yn
8865                                 ,p_rebook_type        => l_rebook_type
8866                                 ,p_prosp_rebook_flag  => l_prosp_rebook_flag
8867                                 ,p_rebook_date        => l_rebook_date
8868                                 ,p_income_strm_sty_id => l_pre_tax_inc_id
8869                                ); --Added by bkatraga for bug 5370233
8870 
8871 
8872                     prof_rate := fnd_profile.value('OKL_BOOK_CONTRACT_WITHOUT_IRR');
8873                     IF ((lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) OR
8874                         (lx_return_status = OKL_API.G_RET_STS_ERROR)) AND
8875                         (nvl(prof_rate, 'N') = 'Y')
8876                     THEN
8877                         lx_return_status := OKL_API.G_RET_STS_SUCCESS;
8878                         OKL_API.init_msg_list( 'T' );
8879                         OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
8880                                            p_msg_name     => 'OKL_CANNOT_CALC_IIR');
8881                         contract_comments := fnd_message.get_string(OKL_API.G_APP_NAME,
8882                                                  'OKL_CANNOT_CALC_IIR');
8883 
8884                         UPDATE OKC_K_HEADERS_TL
8885                             SET COMMENTS = CONTRACT_COMMENTS
8886                         WHERE ID = P_KHR_ID
8887                         AND LANGUAGE = USERENV('LANG');
8888                     END IF;
8889 
8890                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8891                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8892                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8893                         RAISE OKL_API.G_EXCEPTION_ERROR;
8894                     END IF;
8895 
8896 
8897                     IF l_interim_interest <> 0 THEN
8898                         i := i + 1;
8899                         l_interim_tbl(i).cf_days   := l_interim_days;
8900                         l_interim_tbl(i).cf_amount := l_interim_interest;
8901                         l_interim_tbl(i).cf_dpp    := l_interim_dpp;
8902                     END IF;
8903 
8904                     IF ( l_subsidies_yn = 'Y' ) THEN
8905                         l_capital_cost := l_capital_cost - l_subsidy_amount;
8906                         l_principal_tbl.DELETE;
8907                         l_interest_tbl.DELETE;
8908                         l_prin_bal_tbl.DELETE;
8909                         l_termination_tbl.DELETE;
8910                         l_pre_tax_inc_tbl.DELETE;
8911 
8912                         OKL_PRICING_PVT.get_loan_amortization(
8913                             p_khr_id           => p_khr_id,
8914                             p_kle_id           => l_fin_asset.id,
8915                             p_purpose_code     => l_purpose_code,
8916                             p_investment       => l_capital_cost,
8917                             p_residual_value   => l_residual_value,
8918                             p_start_date       => l_hdr.start_date,
8919                             p_asset_start_date => l_fin_asset.start_date,
8920                             p_term_duration    => l_hdr.term_duration,
8921                             p_currency_code    => l_hdr.currency_code,
8922                             p_deal_type        => l_deal_type,
8923                             p_asset_iir_guess  => l_asset_guess_iir,
8924                             p_bkg_yield_guess  => l_bkg_yield_guess,
8925                             x_principal_tbl    => l_principal_tbl,
8926                             x_interest_tbl     => l_interest_tbl,
8927                             x_prin_bal_tbl     => l_prin_bal_tbl,
8928                             x_termination_tbl  => l_termination_tbl,
8929                             x_pre_tax_inc_tbl  => l_pre_tax_inc_tbl,
8930                             x_interim_interest => l_interim_interest,
8931                             x_interim_days     => l_interim_days,
8932                             x_interim_dpp      => l_interim_dpp,
8933                             x_iir              => l_asset_iir,
8934                             x_booking_yield    => l_asset_booking_yield,
8935                             x_return_status    => lx_return_status,
8936                             p_se_id            => l_se_id
8937                             -- parameters added for Prospective Rebooking Enhancement
8938                             ,p_during_rebook_yn   => l_is_during_rebook_yn
8939                             ,p_rebook_type        => l_rebook_type
8940                             ,p_prosp_rebook_flag  => l_prosp_rebook_flag
8941                             ,p_rebook_date        => l_rebook_date
8942                             ,p_income_strm_sty_id => l_pre_tax_inc_id
8943                            ); --Added by bkatraga for bug 5370233
8944 
8945                         prof_rate := fnd_profile.value('OKL_BOOK_CONTRACT_WITHOUT_IRR');
8946                         IF ((lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) OR
8947                             (lx_return_status = OKL_API.G_RET_STS_ERROR)) AND
8948                             (nvl(prof_rate, 'N') = 'Y') THEN
8949 
8950                                 lx_return_status := OKL_API.G_RET_STS_SUCCESS;
8951                                 OKL_API.init_msg_list( 'T' );
8952                                 OKL_API.SET_MESSAGE (p_app_name => G_APP_NAME,
8953                                             p_msg_name     => 'OKL_CANNOT_CALC_IIR');
8954                                 contract_comments := fnd_message.get_string(OKL_API.G_APP_NAME,
8955                                             'OKL_CANNOT_CALC_IIR');
8956 
8957                                 UPDATE OKC_K_HEADERS_TL
8958                                 SET COMMENTS = CONTRACT_COMMENTS
8959                                 WHERE ID = P_KHR_ID
8960                                 AND LANGUAGE = USERENV('LANG');
8961                         END IF;
8962                         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8963                             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8964                         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8965                             RAISE OKL_API.G_EXCEPTION_ERROR;
8966                         END IF;
8967 
8968                         IF l_interim_interest <> 0 THEN
8969                             i := i + 1;
8970                             l_sub_interim_tbl(i).cf_days   := l_interim_days;
8971                             l_sub_interim_tbl(i).cf_amount := l_interim_interest;
8972                             l_sub_interim_tbl(i).cf_dpp    := l_interim_dpp;
8973                         END IF;
8974                     End If;
8975                     i := i + 1;
8976                     --l_clev_tbl(i).dnz_chr_id := p_khr_id;
8977                     --l_clev_tbl(i).chr_id := p_khr_id;
8978                     l_clev_tbl(i).id := l_fin_asset.id;
8979                     l_klev_tbl(i).id := l_fin_asset.id;
8980                     l_klev_tbl(i).implicit_interest_rate := l_asset_iir * 100.0;
8981                     l_asset_guess_iir := l_asset_iir;
8982                     l_bkg_yield_guess := l_asset_booking_yield;
8983                 End If;
8984 
8985 
8986 
8987              print( l_prog_name, ' lty code ' || l_fin_asset.lty_code );
8988              print( l_prog_name, ' mapped stream PRE-TAX INCOME ' || l_mapped_sty_name );
8989              print( l_prog_name, ' prim ' || to_char(l_primary_sty_id) );
8990              print( l_prog_name, ' deal type  ' || l_deal_type );
8991              print( l_prog_name, ' fee type  ' || l_fee_type );
8992              print( l_prog_name, ' count  ' || l_pre_tax_inc_tbl.COUNT);
8993 
8994                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
8995                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8996                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
8997                     RAISE OKL_API.G_EXCEPTION_ERROR;
8998                 END IF;
8999 
9000 
9001             --print( l_prog_name, ' inserting principal ' );
9002                 IF l_principal_tbl.COUNT > 0 AND (l_principal_id IS NOT NULL) AND
9003                     ( (l_deal_type IN ('LOAN', 'LOAN-REVOLVING')) OR
9004                     ( l_fin_asset.fee_type = 'FINANCED' OR
9005 		      l_fin_asset.fee_type = 'ROLLOVER' OR
9006 		      l_fin_asset.lty_code = 'LINK_FEE_ASSET') )
9007                 THEN
9008                     get_stream_header(
9009                         p_khr_id         =>   p_khr_id,
9010                         p_kle_id         =>   l_fin_asset.id,
9011                         p_sty_id         =>   l_principal_id,
9012                         p_purpose_code   =>   l_purpose_code,
9013                         x_stmv_rec       =>   l_stmv_rec,
9014                         x_return_status  =>   lx_return_status);
9015 
9016                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9017                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9018                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9019                         RAISE OKL_API.G_EXCEPTION_ERROR;
9020                     END IF;
9021 
9022                     lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
9023                                             p_api_version   => g_api_version,
9024                                             p_init_msg_list => p_init_msg_list,
9025                                             x_msg_count     => x_msg_count,
9026                                             x_msg_data      => x_msg_data,
9027                                             p_chr_id        => p_khr_id,
9028                                             p_selv_tbl      => l_principal_tbl,
9029                                             x_selv_tbl      => lx_selv_tbl,
9030                                             p_org_id        => G_ORG_ID,
9031                                             p_precision     => G_PRECISION,
9032                                             p_currency_code => G_CURRENCY_CODE,
9033                                             p_rounding_rule => G_ROUNDING_RULE,
9034                                             p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
9035 
9036                     IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9037                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9038                     ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
9039                         RAISE OKL_API.G_EXCEPTION_ERROR;
9040                     END IF;
9041 
9042                     l_principal_tbl.DELETE;
9043                     l_principal_tbl := lx_selv_tbl;
9044 
9045                     okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
9046                                                     p_init_msg_list   =>   G_FALSE,
9047                                                     x_return_status   =>   lx_return_status,
9048                                                     x_msg_count       =>   x_msg_count,
9049                                                     x_msg_data        =>   x_msg_data,
9050                                                     p_stmv_rec        =>   l_stmv_rec,
9051                                                     p_selv_tbl        =>   l_principal_tbl,
9052                                                     x_stmv_rec        =>   lx_stmv_rec,
9053                                                     x_selv_tbl        =>   lx_selv_tbl);
9054                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9055                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9056                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9057                         RAISE OKL_API.G_EXCEPTION_ERROR;
9058                     END IF;
9059               END IF;
9060 --print( l_prog_name, ' inserting interest ' );
9061             IF l_interest_tbl.COUNT > 0 AND ( l_interest_id IS NOT NULL) AND
9062                 ( (l_deal_type IN ('LOAN', 'LOAN-REVOLVING')) OR
9063                 ( l_fin_asset.fee_type = 'FINANCED' OR l_fin_asset.fee_type = 'ROLLOVER' OR
9064 		      l_fin_asset.lty_code = 'LINK_FEE_ASSET') )
9065                 THEN
9066 
9067 
9068 
9069                 get_stream_header(
9070                     p_khr_id         =>   p_khr_id,
9071                     p_kle_id         =>   l_fin_asset.id,
9072                     p_sty_id         =>   l_interest_id,
9073                     p_purpose_code   =>   l_purpose_code,
9074                     x_stmv_rec       =>   l_stmv_rec,
9075                     x_return_status  =>   lx_return_status);
9076 
9077                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9078                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9079                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9080                     RAISE OKL_API.G_EXCEPTION_ERROR;
9081                 END IF;
9082 
9083                 lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
9084                                 p_api_version   => g_api_version,
9085                                 p_init_msg_list => p_init_msg_list,
9086                                 x_msg_count     => x_msg_count,
9087                                 x_msg_data      => x_msg_data,
9088                                 p_chr_id        => p_khr_id,
9089                                 p_selv_tbl      => l_interest_tbl,
9090                                 x_selv_tbl      => lx_selv_tbl,
9091                                 p_org_id        => G_ORG_ID,
9092                                 p_precision     => G_PRECISION,
9093                                 p_currency_code => G_CURRENCY_CODE,
9094                                 p_rounding_rule => G_ROUNDING_RULE,
9095                                 p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
9096 
9097                 IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9098                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9099                 ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
9100                     RAISE OKL_API.G_EXCEPTION_ERROR;
9101                 END IF;
9102 
9103                 l_interest_tbl.DELETE;
9104                 l_interest_tbl := lx_selv_tbl;
9105 
9106                 okl_streams_pub.create_streams(
9107                     p_api_version     =>   G_API_VERSION,
9108                     p_init_msg_list   =>   G_FALSE,
9109                     x_return_status   =>   lx_return_status,
9110                     x_msg_count       =>   x_msg_count,
9111                     x_msg_data        =>   x_msg_data,
9112                     p_stmv_rec        =>   l_stmv_rec,
9113                     p_selv_tbl        =>   l_interest_tbl,
9114                     x_stmv_rec        =>   lx_stmv_rec,
9115                     x_selv_tbl        =>   lx_selv_tbl);
9116 
9117                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9118                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9119                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9120                     RAISE OKL_API.G_EXCEPTION_ERROR;
9121                 END IF;
9122             END IF;
9123 --             print( l_prog_name, ' inserting prin bal ' );
9124             IF l_prin_bal_tbl.COUNT > 0 AND (l_prin_bal_id IS NOT NULL) AND
9125                 ( (l_deal_type IN ('LOAN', 'LOAN-REVOLVING')) OR
9126                 ( l_fin_asset.fee_type = 'FINANCED'
9127                     OR l_fin_asset.fee_type = 'ROLLOVER' OR
9128 		      l_fin_asset.lty_code = 'LINK_FEE_ASSET') )
9129                 THEN
9130                 get_stream_header(
9131                     p_khr_id         =>   p_khr_id,
9132                     p_kle_id         =>   l_fin_asset.id,
9133                     p_sty_id         =>   l_prin_bal_id,
9134                     p_purpose_code   =>   l_purpose_code,
9135                     x_stmv_rec       =>   l_stmv_rec,
9136                     x_return_status  =>   lx_return_status);
9137 
9138                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9139                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9140                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9141                     RAISE OKL_API.G_EXCEPTION_ERROR;
9142                 END IF;
9143 
9144                 lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
9145                                 p_api_version   => g_api_version,
9146                                 p_init_msg_list => p_init_msg_list,
9147                                 x_msg_count     => x_msg_count,
9148                                 x_msg_data      => x_msg_data,
9149                                 p_chr_id        => p_khr_id,
9150                                 p_selv_tbl      => l_prin_bal_tbl,
9151                                 x_selv_tbl      => lx_selv_tbl,
9152                                 p_org_id        => G_ORG_ID,
9153                                 p_precision     => G_PRECISION,
9154                                 p_currency_code => G_CURRENCY_CODE,
9155                                 p_rounding_rule => G_ROUNDING_RULE,
9156                                 p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
9157 
9158                 IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9159                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9160                 ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
9161                     RAISE OKL_API.G_EXCEPTION_ERROR;
9162                 END IF;
9163 
9164                 l_prin_bal_tbl.DELETE;
9165                 l_prin_bal_tbl := lx_selv_tbl;
9166 
9167                 okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
9168                                                p_init_msg_list   =>   G_FALSE,
9169                                                x_return_status   =>   lx_return_status,
9170                                                x_msg_count       =>   x_msg_count,
9171                                                x_msg_data        =>   x_msg_data,
9172                                                p_stmv_rec        =>   l_stmv_rec,
9173                                                p_selv_tbl        =>   l_prin_bal_tbl,
9174                                                x_stmv_rec        =>   lx_stmv_rec,
9175                                                x_selv_tbl        =>   lx_selv_tbl);
9176 
9177                 IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9178                   RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9179                 ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9180                   RAISE OKL_API.G_EXCEPTION_ERROR;
9181                 END IF;
9182               END IF;
9183 
9184     --             print( l_prog_name, ' inserting termin  ' );
9185               IF l_termination_tbl.COUNT > 0 AND (l_termination_id IS NOT NULL) THEN
9186                     get_stream_header(p_khr_id         =>   p_khr_id,
9187                                   p_kle_id         =>   l_fin_asset.id,
9188                                   p_sty_id         =>   l_termination_id,
9189                                   p_purpose_code   =>   l_purpose_code,
9190                                   x_stmv_rec       =>   l_stmv_rec,
9191                                   x_return_status  =>   lx_return_status);
9192 
9193                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9194                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9195                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9196                         RAISE OKL_API.G_EXCEPTION_ERROR;
9197                     END IF;
9198 
9199                     lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
9200                                     p_api_version   => g_api_version,
9201                                     p_init_msg_list => p_init_msg_list,
9202                                     x_msg_count     => x_msg_count,
9203                                     x_msg_data      => x_msg_data,
9204                                     p_chr_id        => p_khr_id,
9205                                     p_selv_tbl      => l_termination_tbl,
9206                                     x_selv_tbl      => lx_selv_tbl,
9207                                     p_org_id        => G_ORG_ID,
9208                                     p_precision     => G_PRECISION,
9209                                     p_currency_code => G_CURRENCY_CODE,
9210                                     p_rounding_rule => G_ROUNDING_RULE,
9211                                     p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
9212 
9213                     IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9214                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9215                     ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
9216                         RAISE OKL_API.G_EXCEPTION_ERROR;
9217                     END IF;
9218 
9219                     l_termination_tbl.DELETE;
9220                     l_termination_tbl := lx_selv_tbl;
9221 
9222                     okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
9223                                                    p_init_msg_list   =>   G_FALSE,
9224                                                    x_return_status   =>   lx_return_status,
9225                                                    x_msg_count       =>   x_msg_count,
9226                                                    x_msg_data        =>   x_msg_data,
9227                                                    p_stmv_rec        =>   l_stmv_rec,
9228                                                    p_selv_tbl        =>   l_termination_tbl,
9229                                                    x_stmv_rec        =>   lx_stmv_rec,
9230                                                    x_selv_tbl        =>   lx_selv_tbl);
9231                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9232                       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9233                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9234                       RAISE OKL_API.G_EXCEPTION_ERROR;
9235                     END IF;
9236                 END IF;
9237  --            print( l_prog_name, ' inserting pre-tax  ' );
9238                 IF l_pre_tax_inc_tbl.COUNT > 0 AND (l_pre_tax_inc_id is NOT NULL) THEN
9239                     get_stream_header(p_khr_id         =>   p_khr_id,
9240                                     p_kle_id         =>   l_fin_asset.id,
9241                                     p_sty_id         =>   l_pre_tax_inc_id,
9242                                     p_purpose_code   =>   l_purpose_code,
9243                                     x_stmv_rec       =>   l_stmv_rec,
9244                                     x_return_status  =>   lx_return_status);
9245 
9246                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9247                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9248                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9249                         RAISE OKL_API.G_EXCEPTION_ERROR;
9250                     END IF;
9251 
9252                     lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
9253                                     p_api_version   => g_api_version,
9254                                     p_init_msg_list => p_init_msg_list,
9255                                     x_msg_count     => x_msg_count,
9256                                     x_msg_data      => x_msg_data,
9257                                     p_chr_id        => p_khr_id,
9258                                     p_selv_tbl      => l_pre_tax_inc_tbl,
9259                                     x_selv_tbl      => lx_selv_tbl,
9260                                     p_org_id        => G_ORG_ID,
9261                                     p_precision     => G_PRECISION,
9262                                     p_currency_code => G_CURRENCY_CODE,
9263                                     p_rounding_rule => G_ROUNDING_RULE,
9264                                     p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
9265 
9266                     IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9267                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9268                     ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
9269                         RAISE OKL_API.G_EXCEPTION_ERROR;
9270                     END IF;
9271 
9272                     l_pre_tax_inc_tbl.DELETE;
9273                     l_pre_tax_inc_tbl := lx_selv_tbl;
9274 
9275                     okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
9276                                                    p_init_msg_list   =>   G_FALSE,
9277                                                    x_return_status   =>   lx_return_status,
9278                                                    x_msg_count       =>   x_msg_count,
9279                                                    x_msg_data        =>   x_msg_data,
9280                                                    p_stmv_rec        =>   l_stmv_rec,
9281                                                    p_selv_tbl        =>   l_pre_tax_inc_tbl,
9282                                                    x_stmv_rec        =>   lx_stmv_rec,
9283                                                    x_selv_tbl        =>   lx_selv_tbl);
9284 
9285                     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9286                       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9287                     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9288                       RAISE OKL_API.G_EXCEPTION_ERROR;
9289                     END IF;
9290                 END IF;
9291                 -- Clear out data structures
9292                 l_principal_tbl.delete;
9293                 l_interest_tbl.delete;
9294                 l_prin_bal_tbl.delete;
9295                 l_termination_tbl.delete;
9296                 l_pre_tax_inc_tbl.delete;
9297                 l_stmv_rec              :=  NULL;
9298                 l_interim_interest      :=  NULL;
9299                 l_interim_days          :=  NULL;
9300                 l_asset_iir             :=  NULL;
9301                 l_asset_booking_yield   :=  NULL;
9302                 l_capital_cost          :=  NULL;
9303 
9304             END IF;
9305         END LOOP;
9306 
9307 
9308         IF ( l_clev_tbl.COUNT > 0 OR l_klev_tbl.COUNT > 0 ) THEN
9309 
9310             okl_contract_pub.update_contract_line(
9311                 p_api_version   => p_api_version,
9312                 p_init_msg_list => p_init_msg_list,
9313                 x_return_status => lx_return_status,
9314                 x_msg_count     => x_msg_count,
9315                 x_msg_data      => x_msg_data,
9316                 p_clev_tbl      => l_clev_tbl,
9317                 p_klev_tbl      => l_klev_tbl,
9318                 p_edit_mode     => 'N',
9319                 x_clev_tbl      => x_clev_tbl,
9320                 x_klev_tbl      => x_klev_tbl);
9321             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9322                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9323             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9324                 RAISE OKL_API.G_EXCEPTION_ERROR;
9325             END IF;
9326         End If;
9327     END IF;
9328   END IF;
9329 
9330     --print( l_prog_name, ' compute irr ' || p_compute_irr );
9331 
9332     IF (p_compute_irr = G_TRUE) AND (l_purpose_code <> 'REPORT') THEN
9333 
9334       ---------------------------------------------
9335       -- STEP 3: Compute IRR
9336       ---------------------------------------------
9337 
9338       IF l_payment_count = 0 THEN
9339 
9340         OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
9341                              p_msg_name     => 'OKL_NO_SLL_DEFINED');
9342 
9343         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9344 
9345       END IF;
9346 
9347       -- Calculate IRR after all inflow stream elements have been generated but before accrual
9348       -- and collateral streams have been generated because IRR cursors will not be able to
9349       -- distinguish between Payment and Accrual stream elements.
9350 
9351       --print( l_prog_name, ' subsidies_yn ' || l_subsidies_yn );
9352       If ( nvl(l_subsidies_yn, 'N') = 'Y' ) Then
9353 
9354           OKL_PRICING_PVT.compute_irr (p_khr_id          =>  p_khr_id,
9355                        p_start_date      =>  l_hdr.start_date,
9356                        p_term_duration   =>  l_hdr.term_duration,
9357                        p_interim_tbl     =>  l_sub_interim_tbl,
9358 		               p_subsidies_yn    =>  'Y',
9359                        p_initial_irr     =>  0,
9360                        x_irr             =>  l_sub_pre_tax_irr,
9361                        x_return_status   =>  lx_return_status);
9362 
9363       prof_rate := fnd_profile.value('OKL_BOOK_CONTRACT_WITHOUT_IRR');
9364 
9365       IF ((lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) OR
9366           (lx_return_status = OKL_API.G_RET_STS_ERROR)) AND
9367 	 (nvl(prof_rate, 'N') = 'Y') THEN
9368 
9369          lx_return_status := OKL_API.G_RET_STS_SUCCESS;
9370           OKL_API.init_msg_list( 'T' );
9371           OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
9372                                p_msg_name     => 'OKL_CANNOT_CALC_IIR');
9373 
9374 --	 select nvl(comments, '') into contract_comments
9375 --	 from   okc_K_headers_tl
9376 --	 where id = p_khr_id
9377 --	   and language = userenv('LANG');
9378 
9379       --   contract_comments := concat(contract_comments, ' : ');
9380          contract_comments := fnd_message.get_string(OKL_API.G_APP_NAME,'OKL_CANNOT_CALC_IIR');
9381 
9382 	 update okc_K_headers_tl
9383 	 set comments = contract_comments
9384 	 where id = p_khr_id
9385 	   and language = userenv('LANG');
9386 
9387       End If;
9388 
9389           IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9390             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9391           ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9392             RAISE OKL_API.G_EXCEPTION_ERROR;
9393           END IF;
9394 
9395           x_contract_rates.sub_pre_tax_irr := l_sub_pre_tax_irr;
9396 
9397           OKL_PRICING_PVT.compute_iir (p_khr_id          =>  p_khr_id,
9398                        p_start_date      =>  l_hdr.start_date,
9399                        p_term_duration   =>  l_hdr.term_duration,
9400                        p_interim_tbl     =>  l_sub_interim_tbl,
9401 		               p_subsidies_yn    =>  'Y',
9402                        -- Bug 77641094 - Pass 0 for p_initial_iir
9403                        p_initial_iir     =>  0, --l_sub_pre_tax_irr,
9404                        x_iir             =>  l_sub_pre_tax_irr,
9405                        x_return_status   =>  lx_return_status);
9406 
9407       prof_rate := fnd_profile.value('OKL_BOOK_CONTRACT_WITHOUT_IRR');
9408 
9409       IF ((lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) OR
9410           (lx_return_status = OKL_API.G_RET_STS_ERROR)) AND
9411 	 (nvl(prof_rate, 'N') = 'Y') THEN
9412 
9413          lx_return_status := OKL_API.G_RET_STS_SUCCESS;
9414           OKL_API.init_msg_list( 'T' );
9415           OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
9416                                p_msg_name     => 'OKL_CANNOT_CALC_IIR');
9417 
9418 --	 select nvl(comments, '') into contract_comments
9419 --	 from   okc_K_headers_tl
9420 --	 where id = p_khr_id
9421 --	   and language = userenv('LANG');
9422 
9423       --   contract_comments := concat(contract_comments, ' : ');
9424          contract_comments := fnd_message.get_string(OKL_API.G_APP_NAME,'OKL_CANNOT_CALC_IIR');
9425 
9426 	 update okc_K_headers_tl
9427 	 set comments = contract_comments
9428 	 where id = p_khr_id
9429 	   and language = userenv('LANG');
9430 
9431       End If;
9432 
9433           IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9434             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9435           ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9436             RAISE OKL_API.G_EXCEPTION_ERROR;
9437           END IF;
9438 
9439           x_contract_rates.sub_impl_interest_rate := l_sub_pre_tax_irr;
9440 
9441       End If;
9442 
9443 
9444         OKL_PRICING_PVT.compute_irr (p_khr_id          =>  p_khr_id,
9445                        p_start_date      =>  l_hdr.start_date,
9446                        p_term_duration   =>  l_hdr.term_duration,
9447                        p_interim_tbl     =>  l_interim_tbl,
9448         		       p_subsidies_yn    =>  'N',
9449         		       p_initial_irr     =>  l_initial_irr,  -- Added by RGOOTY
9450                        x_irr             =>  l_pre_tax_irr,
9451                        x_return_status   =>  lx_return_status);
9452 
9453       prof_rate := fnd_profile.value('OKL_BOOK_CONTRACT_WITHOUT_IRR');
9454 
9455       IF ((lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) OR
9456           (lx_return_status = OKL_API.G_RET_STS_ERROR)) AND
9457 	 (nvl(prof_rate, 'N') = 'Y') THEN
9458 
9459          lx_return_status := OKL_API.G_RET_STS_SUCCESS;
9460           OKL_API.init_msg_list( 'T' );
9461           OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
9462                                p_msg_name     => 'OKL_CANNOT_CALC_IIR');
9463 
9464 --	 select nvl(comments, '') into contract_comments
9465 --	 from   okc_K_headers_tl
9466 --	 where id = p_khr_id
9467 --	   and language = userenv('LANG');
9468 
9469       --   contract_comments := concat(contract_comments, ' : ');
9470          contract_comments := fnd_message.get_string(OKL_API.G_APP_NAME,'OKL_CANNOT_CALC_IIR');
9471 
9472 	 update okc_K_headers_tl
9473 	 set comments = contract_comments
9474 	 where id = p_khr_id
9475 	   and language = userenv('LANG');
9476 
9477       End If;
9478 
9479       print( l_prog_name, ' compute irr ', lx_return_status );
9480       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9481         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9482       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9483         RAISE OKL_API.G_EXCEPTION_ERROR;
9484       END IF;
9485 
9486       print( l_prog_name, ' pre tax irr ' || to_char( nvl(l_pre_tax_irr, -999) ) , lx_return_status );
9487       x_contract_rates.pre_tax_irr := l_pre_tax_irr;
9488 
9489       OKL_PRICING_PVT.compute_iir (p_khr_id          =>  p_khr_id,
9490                        p_start_date      =>  l_hdr.start_date,
9491                        p_term_duration   =>  l_hdr.term_duration,
9492                        p_interim_tbl     =>  l_interim_tbl,
9493         	       p_subsidies_yn    =>  'N',
9494                        -- Bug 7641094 - Pass 0 for p_initial_iir
9495                        p_initial_iir     =>  0, --x_contract_rates.pre_tax_irr,
9496                        x_iir             =>  l_pre_tax_irr,
9497                        x_return_status   =>  lx_return_status);
9498 
9499       prof_rate := fnd_profile.value('OKL_BOOK_CONTRACT_WITHOUT_IRR');
9500 
9501       IF ((lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) OR
9502           (lx_return_status = OKL_API.G_RET_STS_ERROR)) AND
9503 	 (nvl(prof_rate, 'N') = 'Y') THEN
9504 
9505          lx_return_status := OKL_API.G_RET_STS_SUCCESS;
9506           OKL_API.init_msg_list( 'T' );
9507           OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
9508                                p_msg_name     => 'OKL_CANNOT_CALC_IIR');
9509 
9510 --	 select nvl(comments, '') into contract_comments
9511 --	 from   okc_K_headers_tl
9512 --	 where id = p_khr_id
9513 --	   and language = userenv('LANG');
9514 
9515       --   contract_comments := concat(contract_comments, ' : ');
9516          contract_comments := fnd_message.get_string(OKL_API.G_APP_NAME,'OKL_CANNOT_CALC_IIR');
9517 
9518 	 update okc_K_headers_tl
9519 	 set comments = contract_comments
9520 	 where id = p_khr_id
9521 	   and language = userenv('LANG');
9522 
9523       End If;
9524 
9525 --      print( l_prog_name, ' compute iir ', lx_return_status );
9526       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9527         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9528       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9529         RAISE OKL_API.G_EXCEPTION_ERROR;
9530       END IF;
9531 
9532       print( l_prog_name, ' pre tax iir ' || to_char( nvl(l_pre_tax_irr, -999) ) , lx_return_status );
9533       x_contract_rates.implicit_interest_rate := l_pre_tax_irr;
9534 
9535     END IF;
9536 
9537     ---------------------------------------------
9538     -- STEP 4: Generate Income Accrual
9539     ---------------------------------------------
9540 --    print( l_prog_name, ' accruals '  );
9541 
9542  -- Added by RGOOTY: Start
9543  IF (p_generation_type = 'SERVICE_LINES') OR (p_generation_type = 'FULL') THEN
9544     -- Added by RGOOTY for bug 9305846
9545     IF(l_deal_type =  'LEASEOP' AND p_generation_type = 'FULL') THEN
9546       -- Bug 10266623: Call the gen_rental_accr_streams only in context of ISG
9547       --Call the new api to generate Rental Accrual Streams
9548       gen_rental_accr_streams(p_api_version   => g_api_version,
9549                               p_init_msg_list => p_init_msg_list,
9550                               p_khr_id        => p_khr_id,
9551                               p_purpose_code  => l_purpose_code,
9552                               x_return_status => lx_return_status,
9553                               x_msg_count     => x_msg_count,
9554                               x_msg_data      => x_msg_data);
9555       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9556         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9557       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9558         RAISE OKL_API.G_EXCEPTION_ERROR;
9559       END IF;
9560     END IF;
9561     --end bkatraga
9562 
9563     FOR l_inflow IN c_inflows LOOP
9564 
9565       l_sty_purpose := l_inflow.stream_type_purpose;
9566 
9567       get_sty_details (p_sty_id        => l_inflow.sty_id,
9568                        x_sty_id        => l_sty_id,
9569                        x_sty_name      => l_sty_name,
9570                        x_return_status => lx_return_status);
9571 
9572       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9573         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9574       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9575         RAISE OKL_API.G_EXCEPTION_ERROR;
9576       END IF;
9577 
9578       l_primary_sty_id := l_sty_id;
9579       -- Changed by RGOOTY: Start
9580       -- Condition added by mansrini on 13-Jun-2005 for Bug 4434343
9581       -- If the line is a serviced asset line, the service line id needs to be determined. Otherwise, the line_id is passed as it is
9582       IF ( l_inflow.lty_code = 'LINK_SERV_ASSET' )
9583       THEN
9584           OPEN top_svc_csr( p_khr_id, l_inflow.cle_id );
9585           FETCH top_svc_csr INTO top_svc_rec;
9586           l_tmp_cle_id := top_svc_rec.top_svc_id;
9587           CLOSE top_svc_csr;
9588       ELSE
9589           l_tmp_cle_id := l_inflow.cle_id;
9590       END IF;
9591       OPEN c_pt_yn (p_cle_id => l_tmp_cle_id);
9592 
9593       FETCH c_pt_yn INTO l_pt_yn;
9594       CLOSE c_pt_yn;
9595       -- Changed by RGOOTY: End
9596 
9597       -- Ignoring billable_yn AND stream_type_class attributes of the SLH stream type.
9598       -- Per MKMITTAL, payments defined from LLA UI are implicitly billable. And
9599       -- every billable payment must generate accrual (MKMITTAL, SRAWLING)
9600 
9601       If( TO_NUMBER(nvl(l_inflow.periods, 0)) > 1 ) Then
9602           l_recurr_yn := OKL_API.G_TRUE;
9603       ElsIf( TO_NUMBER(nvl(l_inflow.periods, 0)) = 1 ) Then
9604           l_recurr_yn := OKL_API.G_FALSE;
9605       ENd If;
9606 
9607       -- Asking ISG to get the AMORTIZED_FEE_INCOME dependent streams
9608       IF l_inflow.fee_line_level_type = 'INCOME_AMORT'
9609       THEN
9610         l_recurr_yn := OKL_API.G_FALSE;
9611       ELSIF l_inflow.fee_line_level_type = 'INCOME_ACCRUED'
9612       THEN
9613         l_recurr_yn := OKL_API.G_TRUE;
9614       END IF;
9615       print( l_prog_name, ' l_recurr_yn      : ' || l_recurr_yn  );
9616       print( l_prog_name, ' lty_code         : ' || l_inflow.lty_code  );
9617       print( l_prog_name, ' l_primary_sty_id : ' || to_char(l_primary_sty_id)  );
9618       print( l_prog_name, ' l_deal_type      : ' || to_char(l_deal_type)  );
9619 
9620       get_mapped_stream ( p_sty_purpose      =>  l_sty_purpose,
9621                             p_line_style     =>  l_inflow.lty_code,
9622                             p_primary_sty_id =>  l_primary_sty_id,
9623                             p_mapping_type   =>  'ACCRUAL',
9624                             p_deal_type      =>  l_deal_type,
9625                             p_fee_type       =>  l_inflow.fee_type,
9626 			                      p_recurr_yn      =>  l_recurr_yn,
9627                             p_pt_yn          =>  l_pt_yn,
9628                             p_khr_id         =>  p_khr_id,
9629                             x_mapped_stream  =>  l_mapped_sty_name,
9630                             x_return_status  =>  lx_return_status,
9631                             p_hdr            =>  r_hdr,
9632                             p_rollover_pmnts =>  r_rollover_pmnts);
9633       print( l_prog_name, ' l_mapped_sty_name ' || to_char(l_mapped_sty_name)  );
9634 
9635       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9636         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9637       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9638         RAISE OKL_API.G_EXCEPTION_ERROR;
9639       END IF;
9640 
9641       inf_start_date := l_inflow.start_date;
9642       l_rental_accr_stream := 'N'; --Added by RGOOTY for bug 9305846
9643       -- Fix to write the Rental Accruals seperately
9644       IF p_generation_type = 'FULL' AND
9645          l_deal_type =  'LEASEOP' AND
9646          l_inflow.lty_code = 'FREE_FORM1' AND
9647          l_inflow.stream_type_purpose = 'RENT' AND
9648          l_mapped_sty_name IS NOT NULL
9649       THEN
9650         print( l_prog_name, ' Handling Rental Accrual Streams ');
9651         l_rental_accr_stream := 'Y'; --Added by RGOOTY for bug 9305846
9652         -- You are about to generate the RENTAL ACCRUAL Streams
9653         -- Accumulate into the l_inflow_tbl for later generation of Rental Accruals
9654         -- If the table doesn't exists until, then create one
9655         IF l_inflows_tbl.COUNT = 0
9656         THEN
9657           inf_index := 1;
9658           l_inflows_tbl( inf_index ) := l_inflow;
9659           inf_index := inf_index + 1;
9660         ELSE
9661           -- Inflows table is already present,
9662           --  if the payment levels belong to the same cle_id, sty_id then accumulate
9663           IF l_inflows_tbl(1).cle_id = l_inflow.cle_id AND
9664              l_inflows_tbl(1).sty_id = l_inflow.sty_id
9665           THEN
9666              -- Inflows belong to the same Asset line and same stream
9667              -- so accumulate
9668              l_inflows_tbl( inf_index ) := l_inflow;
9669              inf_index := inf_index + 1;
9670           ELSE
9671              -- New Payment Levels for another asset / another Payment Stream
9672              -- Hence, destroy the payment levels
9673              l_inflows_tbl.DELETE;
9674              -- Create new table and then store into that
9675              inf_index := 1;
9676              l_inflows_tbl( inf_index ) := l_inflow;
9677              inf_index := inf_index + 1;
9678           END IF;
9679         END IF;
9680         -- Now check whether to pick the contract date
9681         --  instead of the start_date stored @ payment level.
9682         -- Fix Explanation:
9683         --   We will use the contract start_date only for a Payment level
9684         --    which is after one or more stubs with zero amount.
9685         l_use_first_pmnt_date := 'N';
9686         IF l_inflows_tbl.COUNT >= 2 AND
9687            l_inflow.periods IS NOT NULL OR
9688            l_inflow.amount IS NOT NULL
9689         THEN
9690           -- If the current Payment level is not the first one
9691           --  and also the current payment level is a regular payment
9692           --  and now, loop through the all the payment levels before
9693           --  this, if all of them are stubs with zero amount, then
9694           -- change the date, else keep the start date at the payment itself.
9695           l_use_first_pmnt_date := 'Y';
9696           FOR t_index in l_inflows_tbl.FIRST .. (l_inflows_tbl.LAST - 1 )
9697           LOOP
9698             IF l_inflows_tbl(t_index).STUB_DAYS IS NOT NULL AND
9699                l_inflows_tbl(t_index).STUB_AMOUNT = 0 AND
9700                l_use_first_pmnt_date = 'Y'
9701             THEN
9702               -- No problem, continue checking
9703               NULL;
9704             ELSE
9705               l_use_first_pmnt_date := 'N';
9706             END IF;
9707           END LOOP;
9708         END IF; -- IF l_inflows_tbl.COUNT >= 2
9709         IF l_use_first_pmnt_date = 'Y'
9710         THEN
9711           -- Pick the first Payment Level Start Date
9712           inf_start_date := l_inflows_tbl(1).start_date;
9713         ELSE
9714            -- Use the payment date stored in this level itself !
9715           inf_start_date := l_inflow.start_date;
9716         END IF;  -- IF l_use_first_pmnt_date = 'Y'
9717       END IF; -- Handling of the Rental Accrual Streams
9718 
9719 
9720       IF l_mapped_sty_name IS NOT NULL
9721       THEN
9722         IF ((l_inflow.lty_code = 'SOLD_SERVICE' OR l_inflow.lty_code = 'LINK_SERV_ASSET') AND
9723 	             p_generation_type = 'SERVICE_LINES') OR
9724             (p_generation_type = 'FULL')
9725         THEN
9726           print( l_prog_name, 'Handling Fee Type            : ' || l_inflow.fee_type  );
9727           print( l_prog_name, 'Origination Income           : ' || l_inflow.origination_income  );
9728           print( l_prog_name, 'l_inflow.fee_line_level_type : ' || l_inflow.fee_line_level_type  );
9729 
9730           -- Bug 12938353: RGOOTY: Handling Origination_Income for Income Fees
9731           --  In effect, handling the Amortized Fee Income details
9732           IF ( l_inflow.fee_line_level_type = 'INCOME_AMORT' AND
9733                l_inflow.fee_type = 'INCOME' AND
9734                l_inflow.origination_income IS NOT NULL AND
9735                l_inflow.origination_income > 0 )
9736           THEN
9737             print( l_prog_name, 'Handling the Amortized Stream Amounts for Income fee ' );
9738             print( l_prog_name, 'p_khr_id                    = ' || p_khr_id );
9739             print( l_prog_name, 'l_hdr.currency_code         = ' || l_hdr.currency_code );
9740             print( l_prog_name, 'l_inflow.start_date         = ' || l_inflow.start_date );
9741             print( l_prog_name, 'l_hdr.end_date              = ' || l_hdr.end_date );
9742             print( l_prog_name, 'l_inflow.origination_income = ' || l_inflow.origination_income );
9743             print( l_prog_name, 'Before calling the get_amortized_accruals API ' );
9744             -- Call get_amortized_accruals API to generate the Amortized Fee Income
9745             -- accruals based on the Origination Income defined on the Income Fees
9746             get_amortized_accruals (
9747               p_khr_id         => p_khr_id,
9748               p_currency_code  => l_hdr.currency_code,
9749               p_start_date     => l_inflow.start_date,
9750               p_end_date       => l_hdr.end_date,
9751               p_deal_type      => l_hdr.deal_type,
9752               p_amount         => l_inflow.origination_income,
9753               x_selv_tbl       => l_selv_tbl,
9754               x_return_status  => lx_return_status);
9755             print( l_prog_name, 'After calling the get_amortized_accruals API lx_return_status=' || lx_return_status );
9756             IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9757               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9758             ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9759               RAISE OKL_API.G_EXCEPTION_ERROR;
9760             END IF;
9761           ELSE
9762             IF l_inflow.fee_line_level_type = 'INCOME_ACCRUED' AND
9763                l_inflow.origination_income IS NOT NULL AND
9764                l_inflow.origination_income > 0
9765             THEN
9766               -- Handling the Accrued Fee Income streams generation for Income Fee
9767               print( l_prog_name, 'Handling the Accrued Stream Amounts for Income Fees' );
9768               print( l_prog_name, 'l_inflow.amount             = ' || l_inflow.amount );
9769               print( l_prog_name, 'l_inflow.stub_amount        = ' || l_inflow.stub_amount );
9770               print( l_prog_name, 'l_inflow.fee_amount         = ' || l_inflow.fee_amount );
9771               print( l_prog_name, 'l_inflow.origination_income = ' || l_inflow.origination_income );
9772 
9773               IF l_inflow.amount IS NOT NULL
9774               THEN
9775                 -- Regular Payment
9776                 l_inflow.amount := l_inflow.amount *
9777                                     ( ( l_inflow.fee_amount - NVL(l_inflow.origination_income,0) ) / l_inflow.fee_amount );
9778               ELSE
9779                 -- Stub Payment
9780                 l_inflow.stub_amount := l_inflow.stub_amount *
9781                                     ( ( l_inflow.fee_amount - NVL(l_inflow.origination_income,0) ) / l_inflow.fee_amount );
9782               END IF;
9783               print( l_prog_name, 'Modified: l_inflow.amount             = ' || l_inflow.amount );
9784               print( l_prog_name, 'Modified: l_inflow.stub_amount        = ' || l_inflow.stub_amount );
9785             ELSE
9786               print( l_prog_name, 'Handling the Accrued Stream Amounts for Non-income Fees' );
9787             END IF; -- IF l_inflow.fee_line_level_type = 'INCOME_ACCRUED'
9788 
9789             IF ( l_rental_accr_stream = 'N' AND
9790                  ( l_inflow.fee_line_level_type = 'FEE_PAYMENT' OR
9791                    -- If it is Income Fee, make sure that there exists some amount that needs to be Accrued
9792                    (
9793                      l_inflow.fee_line_level_type = 'INCOME_ACCRUED' AND
9794                     ( ( l_inflow.fee_amount - NVL(l_inflow.origination_income,0) ) > 0 )
9795                    )
9796                  )
9797                )
9798             THEN  --Added by RGOOTY for bug 9305846
9799               print( l_prog_name, 'Before calling the get_accrual_elements ' );
9800               get_accrual_elements (
9801                 p_start_date          =>   l_inflow.start_date,
9802                 p_periods             =>   l_inflow.periods,
9803                 p_frequency           =>   l_inflow.frequency,
9804                 p_structure           =>   l_inflow.structure,
9805                 p_advance_or_arrears  =>   l_inflow.advance_arrears,
9806                 p_amount              =>   l_inflow.amount,
9807                 p_stub_days           =>   l_inflow.stub_days,
9808                 p_stub_amount         =>   l_inflow.stub_amount,
9809                 p_currency_code       =>   l_hdr.currency_code,
9810                 x_selv_tbl            =>   l_selv_tbl,
9811                 x_return_status       =>   lx_return_status);
9812               print( l_prog_name, 'After calling the get_accrual_elements lx_return_status=' || lx_return_status );
9813               IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9814                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9815               ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9816                 RAISE OKL_API.G_EXCEPTION_ERROR;
9817               END IF;
9818             END IF; --Added by RGOOTY for bug 9305846
9819           END IF;
9820 
9821           get_sty_details (p_sty_name      => l_mapped_sty_name,
9822                            x_sty_id        => l_sty_id,
9823                            x_sty_name      => l_sty_name,
9824                            x_return_status => lx_return_status);
9825 
9826           IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9827             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9828           ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9829             RAISE OKL_API.G_EXCEPTION_ERROR;
9830           END IF;
9831 
9832           --Modified by kthiruva on 27-May-2005
9833           --Bug 4371472 - Start of Changes
9834           --Added the condition to check of the l_selv_tbl.count >0
9835           IF (l_selv_tbl.COUNT > 0 ) THEN
9836             get_stream_header(p_khr_id         =>   p_khr_id,
9837                               p_kle_id         =>   l_inflow.cle_id,
9838                               p_sty_id         =>   l_sty_id,
9839                               p_purpose_code   =>   l_purpose_code,
9840                               x_stmv_rec       =>   l_stmv_rec,
9841                               x_return_status  =>   lx_return_status);
9842 
9843              IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9844                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9845              ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9846                RAISE OKL_API.G_EXCEPTION_ERROR;
9847              END IF;
9848 
9849           --
9850           -- call process API to create parent Stream Header and its child Stream Elements
9851           --
9852 
9853               lx_return_status := Okl_Streams_Util.round_streams_amount_esg(
9854                                   p_api_version   => g_api_version,
9855                                   p_init_msg_list => p_init_msg_list,
9856                                   x_msg_count     => x_msg_count,
9857                                   x_msg_data      => x_msg_data,
9858                                   p_chr_id        => p_khr_id,
9859                                   p_selv_tbl      => l_selv_tbl,
9860                                   x_selv_tbl      => lx_selv_tbl,
9861                                   p_org_id        => G_ORG_ID,
9862                                   p_precision     => G_PRECISION,
9863                                   p_currency_code => G_CURRENCY_CODE,
9864                                   p_rounding_rule => G_ROUNDING_RULE,
9865                                   p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
9866 
9867               IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9868                   RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9869               ELSIF (x_return_status = G_RET_STS_ERROR) THEN
9870                   RAISE OKL_API.G_EXCEPTION_ERROR;
9871               END IF;
9872 
9873               l_selv_tbl.DELETE;
9874               l_selv_tbl := lx_selv_tbl;
9875               --Accumulate Stream Header
9876               OKL_STREAMS_UTIL.accumulate_strm_headers(
9877                 p_stmv_rec       => l_stmv_rec,
9878                 x_full_stmv_tbl  => l_stmv_tbl,
9879                 x_return_status  => lx_return_status );
9880               IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9881                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9882               ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
9883                 RAISE OKL_API.G_EXCEPTION_ERROR;
9884               END IF;
9885               --Accumulate Stream Elements
9886               OKL_STREAMS_UTIL.accumulate_strm_elements(
9887                 p_stm_index_no  =>  l_stmv_tbl.LAST,
9888                 p_selv_tbl       => l_selv_tbl,
9889                 x_full_selv_tbl  => l_full_selv_tbl,
9890                 x_return_status  => lx_return_status );
9891               IF (lx_return_status = G_RET_STS_UNEXP_ERROR) THEN
9892                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9893               ELSIF (lx_return_status = G_RET_STS_ERROR) THEN
9894                 RAISE OKL_API.G_EXCEPTION_ERROR;
9895               END IF;
9896 
9897            END IF;
9898            --Bug 4371472 - End of Changes
9899         END IF;
9900 
9901       END IF;
9902 
9903       -- Clear out reusable data structures
9904 
9905       l_mapped_sty_name := NULL;
9906       l_sty_name        :=  NULL;
9907       l_sty_id          :=  NULL;
9908 
9909       l_stmv_rec := NULL;
9910       l_selv_tbl.delete;
9911 
9912       lx_stmv_rec := NULL;
9913       lx_selv_tbl.delete;
9914 
9915       l_pt_yn := NULL;
9916 
9917     END LOOP;
9918     --Create all the accumulated Streams at one shot ..
9919     IF l_stmv_tbl.COUNT > 0 AND
9920        l_full_selv_tbl.COUNT > 0
9921     THEN
9922 Okl_Streams_Pub.create_streams_perf(
9923                                p_api_version,
9924                                p_init_msg_list,
9925                                lx_return_status,
9926                                x_msg_count,
9927                                x_msg_data,
9928                                l_stmv_tbl,
9929                                l_full_selv_tbl,
9930                                lx_stmv_tbl,
9931                                lx_full_selv_tbl);
9932       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9933         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9934       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9935         RAISE OKL_API.G_EXCEPTION_ERROR;
9936       END IF;
9937     END IF;
9938 END IF;
9939 -- Added by RGOOTY: End
9940 
9941     -- streams like RESIDUAL VALUE, etc.
9942     gen_non_cash_flows (    p_api_version          => p_api_version,
9943                             p_init_msg_list        => p_init_msg_list,
9944                             p_khr_id               => p_khr_id,
9945 			    p_pre_tax_irr          => (l_pre_tax_irr*100.00),
9946                             p_generation_type      => p_generation_type,
9947                             p_subsidies_yn         => nvl(l_subsidies_yn, 'N'),
9948                             p_reporting_book_class => p_reporting_book_class,
9949                             x_return_status        => x_return_status,
9950                             x_msg_count            => x_msg_count,
9951                             x_msg_data             => x_msg_data);
9952 
9953      IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9954         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9955      ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
9956         RAISE OKL_API.G_EXCEPTION_ERROR;
9957      END IF;
9958 
9959     print( l_prog_name, ' non gen cash flows ', x_return_status );
9960     print( l_prog_name, ' gen type ' || p_generation_type );
9961 
9962     IF p_generation_type = 'FULL' THEN
9963 
9964       ---------------------------------------------
9965       -- STEP 5: Generate Fee Expense Accrual
9966       ---------------------------------------------
9967 
9968       okl_expense_streams_pvt.generate_expense_streams(p_api_version    => G_API_VERSION,
9969                                                        p_init_msg_list  => G_FALSE,
9970                                                        p_khr_id         => p_khr_id,
9971                                                        p_purpose_code   => l_purpose_code,
9972                                                        p_deal_type      => l_deal_type,
9973                                                        x_return_status  => lx_return_status,
9974                                                        x_msg_count      => x_msg_count,
9975                                                        x_msg_data       => x_msg_data);
9976 
9977       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
9978         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9979       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
9980         RAISE OKL_API.G_EXCEPTION_ERROR;
9981       END IF;
9982 
9983 
9984 
9985     print( l_prog_name, ' updating loan payment ' );
9986 
9987 
9988     IF l_deal_type IN ('LOAN', 'LOAN-REVOLVING') THEN
9989 
9990 	   -- bug 13013062: fetch the rent stream type id .. start
9991 	   open get_rent_sty_id(p_khr_id);
9992 	   fetch get_rent_sty_id into l_rent_sty_id;
9993 	   close get_rent_sty_id;
9994 	   -- bug 13013062: fetch the rent stream type id .. end
9995 
9996           OKL_ISG_UTILS_PVT.get_dependent_stream_type(
9997                                          p_khr_id                => p_khr_id,
9998                     					 p_deal_type             => l_deal_type,
9999                     					 p_primary_sty_id        => l_rent_sty_id,
10000                                          p_dependent_sty_purpose => 'LOAN_PAYMENT',
10001                                          x_return_status         => x_return_status,
10002                                          x_dependent_sty_id      => l_sty_id,
10003                                          x_dependent_sty_name    => l_sty_name);
10004 
10005           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10006               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10007           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10008               RAISE OKL_API.G_EXCEPTION_ERROR;
10009           END IF;
10010 
10011    If ( l_sty_id IS NOT NULL ) Then
10012 
10013       UPDATE okl_streams
10014       SET sty_id = l_sty_id,
10015           last_updated_by = FND_GLOBAL.USER_ID,      -- BUG:14749215 changes start here
10016           last_update_date = sysdate,
10017           last_update_login = FND_GLOBAL.LOGIN_ID    -- BUG:14749215 changes end here
10018       WHERE khr_id = p_khr_id
10019         AND say_code = 'WORK'
10020         AND NVL(purpose_code, '-99') = l_purpose_code
10021         AND sty_id = l_rent_sty_id;
10022 
10023     End if;
10024   END IF;
10025 
10026 
10027     If ( nvl(l_blnHasFinFees, OKL_API.G_FALSE) = OKL_APi.G_TRUE ) Then
10028 --    print( l_prog_name, ' updating financed fees ' );
10029 
10030      FOR r_financed_fees in c_financed_fees
10031      LOOP
10032 --print( l_prog_name, ' getting financed fees ' || to_char( r_financed_fees.id ));
10033 
10034        l_rent_sty_id := NULL; --bug# 4096605.
10035        OPEN c_rent_sty_id(r_financed_fees.id);
10036        FETCH  c_rent_sty_id INTO l_rent_sty_id;
10037        CLOSE  c_rent_sty_id;
10038 
10039 
10040        If ( l_rent_sty_id is NOT NULL ) Then
10041 
10042           OKL_ISG_UTILS_PVT.get_dependent_stream_type(
10043                                          p_khr_id                => p_khr_id,
10044 					 p_deal_type             => l_deal_type,
10045 					 p_primary_sty_id        => l_rent_sty_id,
10046                                          p_dependent_sty_purpose => 'LOAN_PAYMENT',
10047                                          x_return_status         => x_return_status,
10048                                          x_dependent_sty_id      => l_sty_id,
10049                                          x_dependent_sty_name    => l_sty_name);
10050 
10051           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10052               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10053           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10054               RAISE OKL_API.G_EXCEPTION_ERROR;
10055           END IF;
10056 
10057    If ( l_sty_id IS NOT NULL ) Then
10058 
10059 --    print( l_prog_name, ' updating financed fees ' || to_char( r_financed_fees.id ));
10060 
10061       UPDATE okl_streams
10062       SET sty_id = l_sty_id,
10063           last_updated_by = FND_GLOBAL.USER_ID,      -- BUG:14749215 changes start here
10064           last_update_date = sysdate,
10065           last_update_login = FND_GLOBAL.LOGIN_ID    -- BUG:14749215 changes end here
10066       WHERE khr_id = p_khr_id
10067         AND kle_id = r_financed_fees.id
10068         AND say_code = 'WORK'
10069         AND NVL(purpose_code, '-99') = l_purpose_code
10070         AND sty_id =  l_rent_sty_id;
10071 
10072    End If;
10073 
10074       END IF ;
10075 
10076      END LOOP;
10077 
10078     End If;
10079 
10080 
10081       END IF;
10082 
10083 --    print( l_prog_name, ' updating ' );
10084 
10085     UPDATE okl_strm_elements
10086     SET comments=NULL
10087     WHERE stm_id IN
10088     (SELECT id from okl_streams WHERE khr_id = p_khr_id and say_code = 'WORK');
10089 --    print( l_prog_name, ' done ' );
10090 
10091  /*
10092  * delete the duplicates from the okl_streams header table.
10093  */
10094 -- Modified by RGOOTY: Start
10095 IF (p_generation_type <> 'RESIDUAL VALUE' AND p_generation_type <> 'CAPITAL REDUCTION')
10096 THEN
10097 
10098     consolidate_line_streams(p_khr_id         => p_khr_id,
10099                              p_purpose_code   => l_purpose_code,
10100                              x_return_status  => lx_return_status);
10101 --    print( l_prog_name, ' consolidated ', lx_return_status );
10102 
10103     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10104       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10105     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
10106       RAISE OKL_API.G_EXCEPTION_ERROR;
10107     END IF;
10108 END IF;
10109 
10110 IF (p_generation_type <> 'SERVICE_LINES' AND
10111     p_generation_type <> 'RESIDUAL VALUE' AND
10112     p_generation_type <> 'CAPITAL REDUCTION')
10113 THEN
10114 
10115     consolidate_header_streams(p_khr_id         => p_khr_id,
10116                              p_purpose_code   => l_purpose_code,
10117                              x_return_status  => lx_return_status);
10118 
10119 --   print( l_prog_name, ' consolidated ', lx_return_status );
10120 
10121     IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10122       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10123     ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
10124       RAISE OKL_API.G_EXCEPTION_ERROR;
10125     END IF;
10126 --bakuchib changes
10127 END IF;
10128 
10129 IF (p_generation_type <> 'RESIDUAL VALUE' AND p_generation_type <> 'CAPITAL REDUCTION')
10130 THEN
10131         consolidate_acc_streams(p_khr_id         => p_khr_id,
10132                                  p_purpose_code   => l_purpose_code,
10133                                  x_return_status  => lx_return_status);
10134 --        print( l_prog_name, ' consolidated ', lx_return_status );
10135 
10136         IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10137           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10138         ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
10139           RAISE OKL_API.G_EXCEPTION_ERROR;
10140         END IF;
10141 
10142 END IF;
10143 
10144 --Call API to generate passthrough expense accrual streams
10145 --Call this API only for ISG, because there is already a call
10146 --for ESG in OKL_LA_STREAM_PVT
10147 --Bug 8624532  by NIKSHAH
10148  IF p_generation_type = 'FULL' THEN
10149    OKL_LA_STREAM_PVT.GENERATE_PASSTHRU_EXP_STREAMS
10150    (
10151      p_api_version      => p_api_version
10152     ,p_init_msg_list    => p_init_msg_list
10153     ,P_CHR_ID           => p_khr_id
10154     ,P_PURPOSE_CODE     => l_purpose_code
10155     ,x_return_status    => lx_return_status
10156     ,x_msg_count        => x_msg_count
10157     ,x_msg_data         => x_msg_data
10158    );
10159    IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10160      RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10161    ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
10162      RAISE OKL_API.G_EXCEPTION_ERROR;
10163    END IF;
10164 
10165    -- 15-Sep-10 SECHAWLA  10045043 : begin
10166    -- Added following code to generate passthru service expense accrual streams
10167    -- for ISG. For ESG, code to generate these streams is in okl_la_stream_pvt.GEN_PASSTHRU_SERV_EXP_STREAMS, which
10168    -- gets called from okl_la_stream_pvt.adjust_passthrough_streams
10169 
10170       OKL_LA_STREAM_PVT.GEN_PASSTHRU_SERV_EXP_STREAMS
10171  (
10172    p_api_version     => p_api_version
10173   ,p_init_msg_list   => p_init_msg_list
10174   ,P_CHR_ID          => p_khr_id
10175  , P_PURPOSE_CODE    => l_purpose_code
10176   ,x_return_status   => lx_return_status
10177   ,x_msg_count       => x_msg_count
10178   ,x_msg_data        => x_msg_data
10179 ) ;
10180 
10181    IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10182      RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10183    ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
10184      RAISE OKL_API.G_EXCEPTION_ERROR;
10185    END IF;
10186    -- 15-Sep-10 SECHAWLA  10045043 : end
10187 
10188 
10189  END IF;
10190 
10191     -- Bug 12944979: RGOOTY: Start
10192     -- Before concluding on generating the Streams
10193     --  If there exists a Line, that is Terminated, call the below API
10194     --  only if ISG generation context is FULL.
10195     -- Below API makes sure that the Accrual Streams for Terminated Lines doesnot
10196     --  result into any Adjustments.
10197     print(l_prog_name, '--------------------------------------------------------' );
10198     print(l_prog_name, '------ ISG to behave like ESG for Terminated Lines -----' );
10199     print(l_prog_name, '--------------------------------------------------------' );
10200     print(l_prog_name, 'Only when there exists a Terminated Line, COPY_ACCRUAL_STREAMS_ISG API needs to be executed.' );
10201     print(l_prog_name, 'Executing the Cursor c_chk_tmnt_asset_exists_csr cp_khr_id=' || p_khr_id );
10202    l_terminated_line_exists := 'N';
10203     FOR t_rec IN c_chk_tmnt_asset_exists_csr(cp_khr_id => p_khr_id)
10204     LOOP
10205      l_terminated_line_exists := 'Y';
10206     END LOOP;
10207     print(l_prog_name, 'p_generation_type        = ' || p_generation_type );
10208     print(l_prog_name, 'l_terminated_line_exists = ' || l_terminated_line_exists );
10209     IF   p_generation_type        = 'FULL'  AND
10210          l_terminated_line_exists = 'Y'
10211     THEN
10212       print(l_prog_name, 'Before the call to the COPY_ACCRUAL_STREAMS_ISG' );
10213       print(l_prog_name, 'p_khr_id       = ' || p_khr_id );
10214       print(l_prog_name, 'l_purpose_code = ' || l_purpose_code );
10215 
10216       COPY_ACCRUAL_STREAMS_ISG(
10217              p_api_version     => p_api_version
10218             ,p_init_msg_list   => p_init_msg_list
10219             ,x_return_status   => lx_return_status
10220             ,x_msg_count       => x_msg_count
10221             ,x_msg_data        => x_msg_data
10222             ,p_chr_id          => p_khr_id
10223             ,p_purpose_code    => l_purpose_code
10224             ,p_source_call     => 'ISG');
10225       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10226         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10227       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
10228         RAISE OKL_API.G_EXCEPTION_ERROR;
10229       END IF;
10230     END IF;
10231     -- Bug 12944979: RGOOTY: End
10232 
10233     -- Just Before completing the Stream Generation Process, adjust the
10234     --  Accrual Streams based on the Straight Line Logic for Prospective Rebooking
10235     -- IF Prospective Rebooking Options is switched On
10236     --   And Stream Generation is during Rebook process,
10237     --   And is by ISG process
10238     --   And the Rebook Date <> Contract Start Date
10239     print(l_prog_name, '--------------------------------------------------------' );
10240     print(l_prog_name, '--------------- Prospective Rebooking Changes ----------' );
10241     print(l_prog_name, '--------------------------------------------------------' );
10242     print(l_prog_name, 'l_prosp_rebook_flag   : ' || l_prosp_rebook_flag );
10243     print(l_prog_name, 'l_is_during_rebook_yn : ' || l_is_during_rebook_yn);
10244     print(l_prog_name, 'l_rebook_date         : ' || l_rebook_date );
10245     print(l_prog_name, 'l_hdr.start_date      : ' || l_hdr.start_date );
10246     print(l_prog_name, 'p_generation_type     : ' || p_generation_type );
10247     IF l_prosp_rebook_flag = 'Y'
10248        AND l_is_during_rebook_yn = 'Y'
10249        AND l_rebook_date <> TRUNC(l_hdr.start_date)
10250        AND p_generation_type IN ('SERVICE_LINES' , 'FULL')
10251     THEN
10252       prosp_adj_acc_strms(
10253               p_api_version         => p_api_version
10254              ,p_init_msg_list       => p_init_msg_list
10255              ,p_rebook_type         => l_rebook_type
10256              ,p_rebook_date         => l_rebook_date
10257              ,p_khr_id              => p_khr_id
10258              ,p_deal_type           => l_deal_type -- l_hdr.deal_type
10259              ,p_currency_code       => l_hdr.currency_code
10260              ,p_start_date          => l_hdr.start_date
10261              ,p_end_date            => l_hdr.end_date
10262              ,p_context             => p_generation_type
10263              ,p_purpose_code        => l_purpose_code
10264              ,x_return_status       => lx_return_status
10265              ,x_msg_count           => x_msg_count
10266              ,x_msg_data            => x_msg_data);
10267       IF lx_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10268         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10269       ELSIF lx_return_status = OKL_API.G_RET_STS_ERROR THEN
10270         RAISE OKL_API.G_EXCEPTION_ERROR;
10271       END IF;
10272     END IF;
10273 
10274     OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
10275                          p_msg_name     => 'OKL_STREAM_GENERATION_SUCCESS');
10276 
10277     x_return_status := G_RET_STS_SUCCESS;
10278     print( l_prog_name, 'end' );
10279 
10280   EXCEPTION
10281 
10282     WHEN OKL_API.G_EXCEPTION_ERROR THEN
10283 
10284       x_return_status := G_RET_STS_ERROR;
10285 
10286     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
10287 
10288       x_return_status := G_RET_STS_UNEXP_ERROR;
10289 
10290     WHEN OTHERS THEN
10291 
10292       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
10293                            p_msg_name     => G_DB_ERROR,
10294                            p_token1       => G_PROG_NAME_TOKEN,
10295                            p_token1_value => l_prog_name,
10296                            p_token2       => G_SQLCODE_TOKEN,
10297                            p_token2_value => sqlcode,
10298                            p_token3       => G_SQLERRM_TOKEN,
10299                            p_token3_value => sqlerrm);
10300 
10301       x_return_status := G_RET_STS_UNEXP_ERROR;
10302 
10303   END generate_streams;
10304 
10305   PROCEDURE generate_streams( p_api_version                IN         NUMBER,
10306                               p_init_msg_list              IN         VARCHAR2,
10307                               p_khr_id                     IN         NUMBER,
10308                               p_compute_irr                IN         VARCHAR2,
10309                               p_generation_type            IN         VARCHAR2,
10310                               p_reporting_book_class       IN         VARCHAR2,
10311                               x_pre_tax_irr                OUT NOCOPY NUMBER,
10312                               x_return_status              OUT NOCOPY VARCHAR2,
10313                               x_msg_count                  OUT NOCOPY NUMBER,
10314                               x_msg_data                   OUT NOCOPY VARCHAR2) IS
10315 
10316    l_contract_rates rate_rec_type;
10317 
10318   Begin
10319 
10320 
10321             generate_streams( p_api_version          => p_api_version,
10322                               p_init_msg_list        => p_init_msg_list,
10323                               p_khr_id               => p_khr_id,
10324                               p_compute_rates        => p_compute_irr,
10325                               p_generation_type      => p_generation_type,
10326                               p_reporting_book_class => p_reporting_book_class,
10327                               x_contract_rates       => l_contract_rates,
10328                               x_return_status        => x_return_status,
10329                               x_msg_count            => x_msg_count,
10330                      			      x_msg_data             => x_msg_data);
10331 
10332             x_pre_tax_irr := l_contract_rates.pre_tax_irr;
10333 
10334 
10335   End generate_streams;
10336 
10337  PROCEDURE  GEN_VAR_INT_SCHEDULE(  p_api_version         IN      NUMBER,
10338                                    p_init_msg_list       IN      VARCHAR2,
10339                                    p_khr_id              IN      NUMBER,
10340 				   p_purpose_code        IN      VARCHAR2,
10341                                    x_return_status       OUT NOCOPY VARCHAR2,
10342                                    x_msg_count           OUT NOCOPY NUMBER,
10343                                    x_msg_data            OUT NOCOPY VARCHAR2) IS
10344 
10345       CURSOR l_varint_sll_csr( khrid NUMBER ) IS
10346       SELECT TO_NUMBER(NULL) cle_id,
10347              TO_NUMBER(rul1.object1_id1) sty_id,
10348              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
10349              TO_NUMBER(rul2.rule_information3) periods,
10350              rul2.object1_id1 frequency,
10351              rul2.rule_information5 structure,
10352              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
10353              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
10354              TO_NUMBER(rul2.rule_information7) stub_days,
10355              TO_NUMBER(rul2.rule_information8) stub_amount,
10356              NULL lty_code,
10357              TO_NUMBER(NULL) capital_amount,
10358              TO_NUMBER(NULL) residual_value
10359       FROM   okc_rule_groups_b rgp,
10360              okc_rules_b rul1,
10361              okc_rules_b rul2,
10362 	     okl_strm_type_tl sty
10363       WHERE  rul2.dnz_chr_id = khrid
10364         AND  rul2.rule_information_category = 'LASLL'
10365         AND  rul2.rgp_id = rgp.id
10366         AND  rul1.rgp_id = rgp.id
10367         AND  rgp.cle_id IS NULL
10368         AND  rul2.object2_id1 = rul1.id
10369 	AND  rul1.object1_id1 = sty.id
10370 	AND  sty.language = USERENV('LANG')
10371 	AND  sty.name = 'VARIABLE INTEREST SCHEDULE';
10372 
10373       l_varint_sll_rec l_varint_sll_csr%ROWTYPE;
10374 
10375         l_advance_or_arrears VARCHAR2(10) := 'ARREARS';
10376 
10377    Cursor l_strms_csr ( chrId NUMBER, styId NUMBER ) IS
10378    Select str.id  strm_id
10379    From okl_streams str
10380    Where str.sty_id = styId
10381        and str.khr_id = chrId
10382        and str.say_code = 'WORK';
10383 
10384     l_strms_rec l_strms_csr%ROWTYPE;
10385 
10386     CURSOR c_hdr IS
10387       SELECT chr.template_yn,
10388              chr.currency_code,
10389              chr.start_date,
10390              khr.deal_type,
10391              khr.term_duration,
10392              NVL(khr.generate_accrual_yn, 'Y')
10393       FROM   okc_k_headers_b chr,
10394              okl_k_headers khr
10395       WHERE  khr.id = p_khr_id
10396         AND  chr.id = khr.id;
10397 
10398     l_hdr                    c_hdr%ROWTYPE;
10399     l_deal_type              VARCHAR2(30);
10400     l_purpose_code           VARCHAR2(30);
10401 
10402     l_sty_id                 NUMBER;
10403     l_sty_name               VARCHAR2(150);
10404     l_mapped_sty_name        VARCHAR2(150);
10405 
10406     l_selv_tbl               okl_streams_pub.selv_tbl_type;
10407     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
10408     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
10409 
10410     l_stmv_rec               okl_streams_pub.stmv_rec_type;
10411     l_stmv_tbl               okl_streams_pub.stmv_tbl_type;
10412     lx_stmv_tbl               okl_streams_pub.stmv_tbl_type;
10413     x_stmv_tbl               okl_streams_pub.stmv_tbl_type;
10414     l_pt_rec                 okl_streams_pub.stmv_rec_type;
10415     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
10416 
10417     i                        BINARY_INTEGER := 0;
10418 
10419     l_api_name         CONSTANT VARCHAR2(61) := 'GEN_VAR_INT_SCHEDULE';
10420 
10421    BEGIN
10422     print( l_api_name, 'begin' );
10423 
10424     x_return_status := OKL_API.G_RET_STS_SUCCESS;
10425 
10426     x_return_status := OKL_API.START_ACTIVITY(
10427 			p_api_name      => l_api_name,
10428 			p_pkg_name      => g_pkg_name,
10429 			p_init_msg_list => p_init_msg_list,
10430 			l_api_version   => p_api_version,
10431 			p_api_version   => p_api_version,
10432 			p_api_type      => G_API_TYPE,
10433 			x_return_status => x_return_status);
10434 
10435     -- check if activity started successfully
10436     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
10437        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10438     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
10439        raise OKL_API.G_EXCEPTION_ERROR;
10440     End If;
10441 
10442     print( l_api_name, 'checking for var rate' );
10443     If (Is_Var_Rate_Contract( p_khr_id ) = 'N' ) THEN
10444          print( l_api_name, 'checking for var rate - nah' );
10445 	 return;
10446     END IF;
10447     print( l_api_name, 'checking for var rate - yah' );
10448 
10449     get_sty_details (p_sty_name      => 'VARIABLE INTEREST SCHEDULE',
10450                      x_sty_id        => l_sty_id,
10451                      x_sty_name      => l_sty_name,
10452 		     x_return_status => x_return_status);
10453 
10454     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10455         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10456     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10457         RAISE OKL_API.G_EXCEPTION_ERROR;
10458     END IF;
10459 
10460     print( l_api_name, 'got stream type' );
10461     i := 0;
10462     FOR  l_strms_rec in l_strms_csr ( p_khr_id, l_sty_id)
10463     LOOP
10464 
10465         i := i + 1;
10466         l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
10467         l_stmv_tbl(i).say_code := 'HIST';
10468         l_stmv_tbl(i).active_yn := 'N';
10469         l_stmv_tbl(i).date_history := sysdate;
10470 
10471     END LOOP;
10472 
10473     If ( l_stmv_tbl.COUNT > 0 ) Then
10474 
10475         Okl_Streams_pub.update_streams(
10476                          p_api_version => p_api_version,
10477                          p_init_msg_list => p_init_msg_list,
10478                          x_return_status => x_return_status,
10479                          x_msg_count => x_msg_count,
10480                          x_msg_data => x_msg_data,
10481                          p_stmv_tbl => l_stmv_tbl,
10482                          x_stmv_tbl => x_stmv_tbl);
10483 
10484         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10485             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10486         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10487             RAISE OKL_API.G_EXCEPTION_ERROR;
10488         END IF;
10489 
10490      End If;
10491 
10492 /*
10493      OPEN  c_hdr;
10494      FETCH c_hdr INTO l_hdr;
10495      CLOSE c_hdr;
10496 
10497      FOR l_varint_sll_rec in l_varint_sll_csr( p_khr_id )
10498      LOOP
10499 
10500          IF ( lx_selv_tbl.COUNT > 0 ) Then
10501              lx_selv_tbl.delete;
10502 	 END IF;
10503 
10504          get_stream_elements(
10505 	                   p_start_date          =>   l_varint_sll_rec.start_date,
10506                            p_periods             =>   l_varint_sll_rec.periods,
10507 			   p_frequency           =>   l_varint_sll_rec.frequency,
10508 			   p_structure           =>   l_varint_sll_rec.structure,
10509 			   p_advance_or_arrears  =>   l_varint_sll_rec.advance_arrears,
10510 			   p_amount              =>   l_varint_sll_rec.amount,
10511 			   p_stub_days           =>   l_varint_sll_rec.stub_days,
10512 			   p_stub_amount         =>   l_varint_sll_rec.stub_amount,
10513 			   p_currency_code       =>   l_hdr.currency_code,
10514 			   p_khr_id              =>   p_khr_id,
10515 			   p_kle_id              =>   NULL,
10516 			   p_purpose_code        =>   p_purpose_code,
10517 			   x_selv_tbl            =>   lx_selv_tbl,
10518 			   x_pt_tbl              =>   l_pt_tbl,
10519 			   x_return_status       =>   x_return_status,
10520 			   x_msg_count           =>   x_msg_count,
10521 			   x_msg_data            =>   x_msg_data);
10522 
10523          IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10524              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10525          ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10526              RAISE OKL_API.G_EXCEPTION_ERROR;
10527          END IF;
10528 
10529          FOR i in 1..lx_selv_tbl.COUNT
10530 	 LOOP
10531 	     l_selv_tbl( l_selv_tbl.COUNT + 1) := lx_selv_tbl(i);
10532 	 END LOOP;
10533 
10534        END LOOP;
10535 
10536             get_stream_header(p_khr_id      =>   p_khr_id,
10537 	                      p_kle_id         =>   NULL,
10538 			      p_sty_id         =>   l_sty_id,
10539 			      p_purpose_code   =>   p_purpose_code,
10540 			      x_stmv_rec       =>   l_stmv_rec,
10541 			      x_return_status  =>   x_return_status);
10542 
10543             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10544               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10545             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10546               RAISE OKL_API.G_EXCEPTION_ERROR;
10547             END IF;
10548 
10549 
10550             x_return_status := Okl_Streams_Util.round_streams_amount(
10551                                 p_api_version   => p_api_version,
10552                                 p_init_msg_list => p_init_msg_list,
10553                                 x_msg_count     => x_msg_count,
10554                                 x_msg_data      => x_msg_data,
10555                                 p_chr_id        => p_khr_id,
10556                                 p_selv_tbl      => l_selv_tbl,
10557                                 x_selv_tbl      => lx_selv_tbl);
10558 
10559             IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
10560                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10561             ELSIF (x_return_status = G_RET_STS_ERROR) THEN
10562                 RAISE OKL_API.G_EXCEPTION_ERROR;
10563             END IF;
10564 
10565             l_selv_tbl.DELETE;
10566             l_selv_tbl := lx_selv_tbl;
10567 
10568             okl_streams_pub.create_streams(p_api_version     =>   p_api_version,
10569                                        p_init_msg_list   =>   p_init_msg_list,
10570                                        x_return_status   =>   x_return_status,
10571                                        x_msg_count       =>   x_msg_count,
10572                                        x_msg_data        =>   x_msg_data,
10573                                        p_stmv_rec        =>   l_stmv_rec,
10574                                        p_selv_tbl        =>   l_selv_tbl,
10575                                        x_stmv_rec        =>   lx_stmv_rec,
10576                                        x_selv_tbl        =>   lx_selv_tbl);
10577 
10578             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10579               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10580             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10581               RAISE OKL_API.G_EXCEPTION_ERROR;
10582             END IF;
10583 
10584 */
10585     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
10586     print( l_api_name, 'end' );
10587     Exception
10588 
10589 	when OKL_API.G_EXCEPTION_ERROR then
10590 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
10591 			p_api_name  => l_api_name,
10592 			p_pkg_name  => g_pkg_name,
10593 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
10594 			x_msg_count => x_msg_count,
10595 			x_msg_data  => x_msg_data,
10596 			p_api_type  => g_api_type);
10597 
10598 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
10599 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
10600 			p_api_name  => l_api_name,
10601 			p_pkg_name  => g_pkg_name,
10602 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
10603 			x_msg_count => x_msg_count,
10604 			x_msg_data  => x_msg_data,
10605 			p_api_type  => g_api_type);
10606 
10607 	when OTHERS then
10608       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
10609 			p_api_name  => l_api_name,
10610 			p_pkg_name  => g_pkg_name,
10611 			p_exc_name  => 'OTHERS',
10612 			x_msg_count => x_msg_count,
10613 			x_msg_data  => x_msg_data,
10614 			p_api_type  => g_api_type);
10615 
10616    END GEN_VAR_INT_SCHEDULE;
10617 
10618    PROCEDURE  gen_pv_streams(p_api_version         IN      NUMBER,
10619                                p_init_msg_list       IN      VARCHAR2,
10620                                p_pool_status         IN      VARCHAR2,
10621                                p_agreement_id        IN      NUMBER,
10622                                p_contract_id         IN      NUMBER,
10623                                p_kle_id              IN      NUMBER,
10624                                p_sty_id              IN      NUMBER,
10625                                p_mode                IN      VARCHAR2,
10626                                x_stmv_rec            OUT NOCOPY okl_streams_pub.stmv_rec_type,
10627                                x_selv_tbl            OUT NOCOPY okl_streams_pub.selv_tbl_type,
10628                                x_return_status       OUT NOCOPY VARCHAR2,
10629                                x_msg_count           OUT NOCOPY NUMBER,
10630                                x_msg_data            OUT NOCOPY VARCHAR2) IS
10631 
10632 
10633     l_api_name         CONSTANT VARCHAR2(61) := 'gen_pv_streams';
10634 
10635     Cursor sec_strms_csr IS
10636     select pol.khr_id agreement_id,
10637            pcn.khr_id contract_id,
10638 	   pcn.kle_id asset_id,
10639            pcn.sty_id stream_type_id,
10640            pcn.streams_from_date,
10641 	   pcn.streams_to_date,
10642 	   ele.STREAM_ELEMENT_DATE,
10643 	   ele.se_line_number,
10644 	   ele.id stream_ele_id,
10645 	   ele.amount stream_ele_amount,
10646 	   sty.name stream_name,
10647 	   stm.id stm_id
10648     from   okl_pools pol,
10649            okl_pool_contents pcn,
10650            okl_strm_type_v sty,
10651 	   okl_streams stm,
10652 	   okl_strm_elements ele
10653      where pcn.sty_id = sty.id
10654        and pcn.pol_id = pol.id
10655        and stm.kle_id = pcn.kle_id
10656        and stm.khr_id = pcn.khr_id
10657        and stm.sty_id = pcn.sty_id
10658        and stm.say_code = 'CURR'
10659        and stm.active_yn = 'Y'
10660        and ele.stm_id = stm.id
10661        and pol.khr_id = p_agreement_id
10662        and stm.khr_id = p_contract_id
10663        and stm.kle_id = p_kle_id
10664        and stm.sty_id = p_sty_id
10665        and pol.status_code = p_pool_status
10666        and ele.stream_element_date >= nvl(pcn.streams_from_date, ele.stream_element_date-1)
10667        and ele.stream_element_date <= nvl(pcn.STREAMS_TO_DATE, OKL_POOL_PVT.G_FINAL_DATE)
10668        AND pcn.status_code IN (G_IA_STS_ACTIVE,G_IA_STS_NEW) -- Added by VARANGAN -Pool Contents Impact - 26/11/07
10669        order by pcn.khr_id, pcn.kle_id, pcn.sty_id;
10670 
10671       sec_strms_rec sec_strms_csr%ROWTYPE;
10672 
10673     Cursor ylds_csr( agmntID NUMBER, khrId NUMBER) IS
10674     select pol.khr_id agreement_id,
10675 	   pcn.khr_id contract_id,
10676 	   pkhr.start_date contract_start_date,
10677 	   pkhr.contract_number agreement_number,
10678 	   nvl(pkhr.after_tax_yield, -1) agmnt_yield,
10679 	   nvl(khr.PRE_TAX_IRR, -1) contract_yield
10680     from  okl_pools pol,
10681 	  okl_pool_contents pcn,
10682 	  okl_K_headers_full_v pkhr,
10683 	  okl_k_headers khr
10684     where pcn.pol_id = pol.id
10685       and pcn.khr_id = khr.id
10686       and pol.khr_id = pkhr.id
10687       and pol.khr_id = agmntID
10688       and pcn.khr_id = khrId
10689       AND pcn.status_CODE IN (G_IA_STS_ACTIVE,G_IA_STS_NEW) ; -- Added by VARANGAN -Pool Contents Impact - 26/11/07
10690 /* sosharma ,14-12-2007
10691 Bug 6691554
10692 Changed cursors for pending pool
10693 Start Changes*/
10694     Cursor sec_strms_pend_csr IS
10695     select pol.khr_id agreement_id,
10696            pcn.khr_id contract_id,
10697 	   pcn.kle_id asset_id,
10698            pcn.sty_id stream_type_id,
10699            pcn.streams_from_date,
10700 	   pcn.streams_to_date,
10701 	   ele.STREAM_ELEMENT_DATE,
10702 	   ele.se_line_number,
10703 	   ele.id stream_ele_id,
10704 	   ele.amount stream_ele_amount,
10705 	   sty.name stream_name,
10706 	   stm.id stm_id
10707     from   okl_pools pol,
10708            okl_pool_contents pcn,
10709            okl_strm_type_v sty,
10710 	   okl_streams stm,
10711 	   okl_strm_elements ele
10712      where pcn.sty_id = sty.id
10713        and pcn.pol_id = pol.id
10714        and stm.kle_id = pcn.kle_id
10715        and stm.khr_id = pcn.khr_id
10716        and stm.sty_id = pcn.sty_id
10717        and stm.say_code = 'CURR'
10718        and stm.active_yn = 'Y'
10719        and ele.stm_id = stm.id
10720        and pol.khr_id = p_agreement_id
10721        and stm.khr_id = p_contract_id
10722        and stm.kle_id = p_kle_id
10723        and stm.sty_id = p_sty_id
10724        and pol.status_code = p_pool_status
10725        and ele.stream_element_date >= nvl(pcn.streams_from_date, ele.stream_element_date-1)
10726        and ele.stream_element_date <= nvl(pcn.STREAMS_TO_DATE, OKL_POOL_PVT.G_FINAL_DATE)
10727        AND pcn.status_code = G_PC_STS_PENDING
10728        order by pcn.khr_id, pcn.kle_id, pcn.sty_id;
10729 
10730 
10731 
10732     Cursor ylds_pend_csr( agmntID NUMBER, khrId NUMBER) IS
10733     select pol.khr_id agreement_id,
10734 	   pcn.khr_id contract_id,
10735 	   pkhr.start_date contract_start_date,
10736 	   pkhr.contract_number agreement_number,
10737 	   nvl(pkhr.after_tax_yield, -1) agmnt_yield,
10738 	   nvl(khr.PRE_TAX_IRR, -1) contract_yield
10739     from  okl_pools pol,
10740 	  okl_pool_contents pcn,
10741 	  okl_K_headers_full_v pkhr,
10742 	  okl_k_headers khr
10743     where pcn.pol_id = pol.id
10744       and pcn.khr_id = khr.id
10745       and pol.khr_id = pkhr.id
10746       and pol.khr_id = agmntID
10747       and pcn.khr_id = khrId
10748       AND pcn.status_CODE = G_PC_STS_PENDING ;
10749 /* sosharma end changes*/
10750 
10751     ylds_rec ylds_csr%ROWTYPE;
10752 
10753     l_day_convention_month VARCHAR2(30);
10754     l_day_convention_year VARCHAR2(30);
10755 
10756     l_agt_yld NUMBER;
10757     l_khr_yld NUMBER;
10758 
10759     l_sty_id NUMBER;
10760     l_sty_name               VARCHAR2(150);
10761     l_mapped_sty_name        VARCHAR2(150);
10762 
10763     l_selv_tbl               okl_streams_pub.selv_tbl_type;
10764     l_stmv_tbl               okl_streams_pub.stmv_tbl_type;
10765     x_stmv_tbl               okl_streams_pub.stmv_tbl_type;
10766     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
10767 
10768     l_stmv_rec               okl_streams_pub.stmv_rec_type;
10769     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
10770 
10771    cursor l_hdrrl_csr IS
10772    select crl.RULE_INFORMATION1 method
10773    from   OKC_RULE_GROUPS_B crg,
10774           OKC_RULES_B crl
10775    where  crl.rgp_id = crg.id
10776           and crg.RGD_CODE = 'LASEIR'
10777           and crl.RULE_INFORMATION_CATEGORY = 'LASEIR'
10778           and crg.dnz_chr_id = p_agreement_id;
10779 
10780    l_hdrrl_rec l_hdrrl_csr%ROWTYPE;
10781 
10782    Cursor c_stm_name IS
10783    Select sty.stream_type_subclass name,
10784               sty.stream_type_purpose stream_type_purpose
10785    from okl_strm_type_tl tl,
10786         okl_strm_type_v sty
10787    where tl.language = 'US'
10788      and sty.id = tl.id
10789      and tl.id = p_sty_id;
10790 
10791    r_stm_name c_stm_name%ROWTYPE;
10792 
10793    i NUMBER;
10794    n NUMBER;
10795 
10796    l_rentsty_id NUMBER;
10797    l_rvsty_id   NUMBER;
10798    l_rentsty_name VARCHAR2(256);
10799    l_rvsty_name   VARCHAR2(256);
10800 
10801    l_days NUMBER;
10802 
10803    l_start_date DATE;
10804    l_end_date   DATE;
10805 
10806    BEGIN
10807 
10808     print( l_api_name, 'begin' );
10809 
10810     x_return_status := OKL_API.G_RET_STS_SUCCESS;
10811 
10812     x_return_status := OKL_API.START_ACTIVITY(
10813 			p_api_name      => l_api_name,
10814 			p_pkg_name      => g_pkg_name,
10815 			p_init_msg_list => p_init_msg_list,
10816 			l_api_version   => p_api_version,
10817 			p_api_version   => p_api_version,
10818 			p_api_type      => G_API_TYPE,
10819 			x_return_status => x_return_status);
10820 
10821     -- check if activity started successfully
10822     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
10823        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10824     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
10825        raise OKL_API.G_EXCEPTION_ERROR;
10826     End If;
10827 
10828     OPEN l_hdrrl_csr;
10829     FETCH l_hdrrl_csr INTO l_hdrrl_rec;
10830     CLOSE l_hdrrl_csr;
10831     print( l_api_name, ' method ' || l_hdrrl_rec.method );
10832 
10833 IF p_mode IS NULL THEN
10834 
10835     OPEN sec_strms_csr;
10836     FETCH sec_strms_csr INTO sec_strms_rec;
10837     CLOSE sec_strms_csr;
10838 
10839     OPEN ylds_csr( p_agreement_id, sec_strms_rec.contract_id );
10840     FETCH ylds_csr INTO ylds_rec;
10841     CLOSE ylds_csr;
10842 
10843     If (nvl(ylds_rec.contract_yield, -360)/360 = -1 ) Then
10844 
10845         OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
10846                             p_msg_name     => 'OKL_AGMNT_NOYLD',
10847                             p_token1       => 'AGMNT',
10848                             p_token1_value => ylds_rec.agreement_number);
10849 
10850         RAISE OKL_API.G_EXCEPTION_ERROR;
10851 
10852     End If;
10853 
10854     l_khr_yld := ylds_rec.contract_yield / 100.00;
10855 
10856     OPEN c_stm_name;
10857     FETCH c_stm_name INTO r_stm_name;
10858     CLOSE c_stm_name;
10859 
10860     IF r_stm_name.name = 'LOAN_PAYMENT' THEN
10861     get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
10862                        p_mapping_type   =>  'PV_DISBURSEMENT',
10863                        p_line_style     =>  NULL,
10864                        p_deal_type      =>  'LOAN',
10865                        p_khr_id         =>  p_agreement_id,
10866                        p_stream_type_purpose => r_stm_name.stream_type_purpose,
10867                        x_mapped_stream  =>  l_mapped_sty_name,
10868                        x_return_status  =>  x_return_status);
10869 
10870     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10871         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10872     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10873         RAISE OKL_API.G_EXCEPTION_ERROR;
10874     END IF;
10875       If l_mapped_sty_name is NOT NULL THen
10876 
10877     get_sty_details (p_sty_name      => l_mapped_sty_name,
10878                      x_sty_id        => l_sty_id,
10879                      x_sty_name      => l_sty_name,
10880                      x_return_status => x_return_status);
10881 
10882     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10883         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10884     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10885         RAISE OKL_API.G_EXCEPTION_ERROR;
10886     END IF;
10887 
10888     l_stmv_rec := NULL;
10889     get_stream_header(p_purpose_code   => NULL,
10890                       p_khr_id         => p_contract_id,
10891                       p_kle_id         => p_kle_id,
10892                       p_sty_id         => l_sty_id,
10893                       x_stmv_rec       => l_stmv_rec,
10894                       x_return_status  => x_return_status);
10895 
10896     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10897         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10898     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10899         RAISE OKL_API.G_EXCEPTION_ERROR;
10900     END IF;
10901 
10902     l_selv_tbl(1).stream_element_date := ylds_rec.contract_start_date;
10903     l_selv_tbl(1).se_line_number := 1;
10904     l_start_date := ylds_rec.contract_start_date;
10905 
10906     l_selv_tbl(1).amount := 0.0;
10907     print( l_api_name, ' khr_yld ' || to_char( l_khr_yld ) );
10908 
10909     -- Fetch the day convention ..
10910     OKL_PRICING_UTILS_PVT.get_day_convention(
10911       p_id              => p_contract_id,
10912       p_source          => 'ISG',
10913       x_days_in_month   => l_day_convention_month,
10914       x_days_in_year    => l_day_convention_year,
10915       x_return_status   => x_return_status);
10916     print( 'gen_pv_streams', 'Month / Year = ' || l_day_convention_month || '/' || l_day_convention_year );
10917     IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
10918       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10919     ELSIF (x_return_status = G_RET_STS_ERROR) THEN
10920       RAISE OKL_API.G_EXCEPTION_ERROR;
10921     END IF;
10922 
10923     FOR sec_strms_rec IN sec_strms_csr
10924     LOOP
10925 
10926         l_end_date := sec_strms_rec.stream_element_date;
10927 
10928        l_days := OKL_PRICING_UTILS_PVT.get_day_count (p_start_date => l_start_date,
10929                                  p_days_in_month => l_day_convention_month,
10930                                  p_days_in_year => l_day_convention_year,
10931                                  p_end_date   => l_end_date,
10932                                  p_arrears    => 'N',
10933                                  x_return_status => x_return_status);
10934 
10935         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10936           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10937         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10938           RAISE OKL_API.G_EXCEPTION_ERROR;
10939         END IF;
10940 
10941         l_selv_tbl(1).amount := l_selv_tbl(1).amount +
10942 	              sec_strms_rec.stream_ele_amount / POWER( 1 + (l_khr_yld/360), l_days );
10943 
10944     print( l_api_name, ' amount ' || to_char( l_selv_tbl(1).amount ) );
10945     END LOOP;
10946 
10947 
10948     x_stmv_rec := l_stmv_rec;
10949     x_selv_tbl := l_selv_tbl;
10950 
10951   End If;
10952 
10953     ELSE
10954       get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
10955                        p_mapping_type   =>  'PV_DISBURSEMENT',
10956                        p_line_style     =>  NULL,
10957                        p_deal_type      =>  'LEASE',
10958 			 p_khr_id         =>  p_agreement_id, --p_khr_id         =>  p_contract_id,
10959                        x_mapped_stream  =>  l_mapped_sty_name,
10960                        x_return_status  =>  x_return_status);
10961 
10962     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10963         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10964     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10965         RAISE OKL_API.G_EXCEPTION_ERROR;
10966     END IF;
10967     If l_mapped_sty_name is NOT NULL THen
10968 
10969     get_sty_details (p_sty_name      => l_mapped_sty_name,
10970                      x_sty_id        => l_sty_id,
10971                      x_sty_name      => l_sty_name,
10972                      x_return_status => x_return_status);
10973 
10974     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10975         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10976     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10977         RAISE OKL_API.G_EXCEPTION_ERROR;
10978     END IF;
10979 
10980     l_stmv_rec := NULL;
10981     get_stream_header(p_purpose_code   => NULL,
10982                       p_khr_id         => p_contract_id,
10983                       p_kle_id         => p_kle_id,
10984                       p_sty_id         => l_sty_id,
10985                       x_stmv_rec       => l_stmv_rec,
10986                       x_return_status  => x_return_status);
10987 
10988     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
10989         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10990     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
10991         RAISE OKL_API.G_EXCEPTION_ERROR;
10992     END IF;
10993 
10994     l_selv_tbl(1).stream_element_date := ylds_rec.contract_start_date;
10995     l_selv_tbl(1).se_line_number := 1;
10996     l_start_date := ylds_rec.contract_start_date;
10997 
10998     l_selv_tbl(1).amount := 0.0;
10999     print( l_api_name, ' khr_yld ' || to_char( l_khr_yld ) );
11000 
11001     -- Fetch the day convention ..
11002     OKL_PRICING_UTILS_PVT.get_day_convention(
11003       p_id              => p_contract_id,
11004       p_source          => 'ISG',
11005       x_days_in_month   => l_day_convention_month,
11006       x_days_in_year    => l_day_convention_year,
11007       x_return_status   => x_return_status);
11008     print( 'gen_pv_streams', 'Month / Year = ' || l_day_convention_month || '/' || l_day_convention_year );
11009     IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
11010       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11011     ELSIF (x_return_status = G_RET_STS_ERROR) THEN
11012       RAISE OKL_API.G_EXCEPTION_ERROR;
11013     END IF;
11014 
11015     FOR sec_strms_rec IN sec_strms_csr
11016     LOOP
11017 
11018         l_end_date := sec_strms_rec.stream_element_date;
11019 
11020        l_days := OKL_PRICING_UTILS_PVT.get_day_count (p_start_date => l_start_date,
11021                                  p_days_in_month => l_day_convention_month,
11022                                  p_days_in_year => l_day_convention_year,
11023                                  p_end_date   => l_end_date,
11024                                  p_arrears    => 'N',
11025                                  x_return_status => x_return_status);
11026 
11027         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11028           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11029         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11030           RAISE OKL_API.G_EXCEPTION_ERROR;
11031         END IF;
11032 
11033         l_selv_tbl(1).amount := l_selv_tbl(1).amount +
11034 	              sec_strms_rec.stream_ele_amount / POWER( 1 + (l_khr_yld/360), l_days );
11035 
11036     print( l_api_name, ' amount ' || to_char( l_selv_tbl(1).amount ) );
11037     END LOOP;
11038 
11039 
11040     x_stmv_rec := l_stmv_rec;
11041     x_selv_tbl := l_selv_tbl;
11042 
11043   End If;
11044 
11045     END IF;
11046 
11047 
11048   ELSE
11049 
11050    OPEN sec_strms_pend_csr;
11051     FETCH sec_strms_pend_csr INTO sec_strms_rec;
11052     CLOSE sec_strms_pend_csr;
11053 
11054     OPEN ylds_pend_csr( p_agreement_id, sec_strms_rec.contract_id );
11055     FETCH ylds_pend_csr INTO ylds_rec;
11056     CLOSE ylds_pend_csr;
11057 
11058     If (nvl(ylds_rec.contract_yield, -360)/360 = -1 ) Then
11059 
11060         OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
11061                             p_msg_name     => 'OKL_AGMNT_NOYLD',
11062                             p_token1       => 'AGMNT',
11063                             p_token1_value => ylds_rec.agreement_number);
11064 
11065         RAISE OKL_API.G_EXCEPTION_ERROR;
11066 
11067     End If;
11068 
11069     l_khr_yld := ylds_rec.contract_yield / 100.00;
11070 
11071     OPEN c_stm_name;
11072     FETCH c_stm_name INTO r_stm_name;
11073     CLOSE c_stm_name;
11074      IF r_stm_name.name = 'LOAN_PAYMENT' THEN
11075        get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
11076                        p_mapping_type   =>  'PV_DISBURSEMENT',
11077                        p_line_style     =>  NULL,
11078                        p_deal_type      =>  'LOAN',
11079                        p_khr_id         =>  p_agreement_id,
11080                        p_stream_type_purpose => r_stm_name.stream_type_purpose,
11081                        x_mapped_stream  =>  l_mapped_sty_name,
11082                        x_return_status  =>  x_return_status);
11083 
11084     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11085         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11086     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11087         RAISE OKL_API.G_EXCEPTION_ERROR;
11088     END IF;
11089       If l_mapped_sty_name is NOT NULL THen
11090 
11091     get_sty_details (p_sty_name      => l_mapped_sty_name,
11092                      x_sty_id        => l_sty_id,
11093                      x_sty_name      => l_sty_name,
11094                      x_return_status => x_return_status);
11095 
11096     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11097         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11098     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11099         RAISE OKL_API.G_EXCEPTION_ERROR;
11100     END IF;
11101 
11102     l_stmv_rec := NULL;
11103     get_stream_header(p_purpose_code   => NULL,
11104                       p_khr_id         => p_contract_id,
11105                       p_kle_id         => p_kle_id,
11106                       p_sty_id         => l_sty_id,
11107                       x_stmv_rec       => l_stmv_rec,
11108                       x_return_status  => x_return_status);
11109 
11110     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11111         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11112     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11113         RAISE OKL_API.G_EXCEPTION_ERROR;
11114     END IF;
11115 
11116     l_selv_tbl(1).stream_element_date := ylds_rec.contract_start_date;
11117     l_selv_tbl(1).se_line_number := 1;
11118     l_start_date := ylds_rec.contract_start_date;
11119 
11120     l_selv_tbl(1).amount := 0.0;
11121     print( l_api_name, ' khr_yld ' || to_char( l_khr_yld ) );
11122 
11123     -- Fetch the day convention ..
11124     OKL_PRICING_UTILS_PVT.get_day_convention(
11125       p_id              => p_contract_id,
11126       p_source          => 'ISG',
11127       x_days_in_month   => l_day_convention_month,
11128       x_days_in_year    => l_day_convention_year,
11129       x_return_status   => x_return_status);
11130     print( 'gen_pv_streams', 'Month / Year = ' || l_day_convention_month || '/' || l_day_convention_year );
11131     IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
11132       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11133     ELSIF (x_return_status = G_RET_STS_ERROR) THEN
11134       RAISE OKL_API.G_EXCEPTION_ERROR;
11135     END IF;
11136 
11137     FOR sec_strms_rec IN sec_strms_pend_csr
11138     LOOP
11139 
11140         l_end_date := sec_strms_rec.stream_element_date;
11141 
11142        l_days := OKL_PRICING_UTILS_PVT.get_day_count (p_start_date => l_start_date,
11143                                  p_days_in_month => l_day_convention_month,
11144                                  p_days_in_year => l_day_convention_year,
11145                                  p_end_date   => l_end_date,
11146                                  p_arrears    => 'N',
11147                                  x_return_status => x_return_status);
11148 
11149         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11150           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11151         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11152           RAISE OKL_API.G_EXCEPTION_ERROR;
11153         END IF;
11154 
11155         l_selv_tbl(1).amount := l_selv_tbl(1).amount +
11156 	              sec_strms_rec.stream_ele_amount / POWER( 1 + (l_khr_yld/360), l_days );
11157 
11158     print( l_api_name, ' amount ' || to_char( l_selv_tbl(1).amount ) );
11159     END LOOP;
11160   END IF;
11161     ELSE
11162       get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
11163                        p_mapping_type   =>  'PV_DISBURSEMENT',
11164                        p_line_style     =>  NULL,
11165                        p_deal_type      =>  'LEASE',
11166 			 p_khr_id         =>  p_agreement_id, --p_khr_id         =>  p_contract_id,
11167                        x_mapped_stream  =>  l_mapped_sty_name,
11168                        x_return_status  =>  x_return_status);
11169 
11170     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11171         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11172     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11173         RAISE OKL_API.G_EXCEPTION_ERROR;
11174     END IF;
11175     If l_mapped_sty_name is NOT NULL THen
11176 
11177     get_sty_details (p_sty_name      => l_mapped_sty_name,
11178                      x_sty_id        => l_sty_id,
11179                      x_sty_name      => l_sty_name,
11180                      x_return_status => x_return_status);
11181 
11182     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11183         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11184     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11185         RAISE OKL_API.G_EXCEPTION_ERROR;
11186     END IF;
11187 
11188     l_stmv_rec := NULL;
11189     get_stream_header(p_purpose_code   => NULL,
11190                       p_khr_id         => p_contract_id,
11191                       p_kle_id         => p_kle_id,
11192                       p_sty_id         => l_sty_id,
11193                       x_stmv_rec       => l_stmv_rec,
11194                       x_return_status  => x_return_status);
11195 
11196     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11197         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11198     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11199         RAISE OKL_API.G_EXCEPTION_ERROR;
11200     END IF;
11201 
11202     l_selv_tbl(1).stream_element_date := ylds_rec.contract_start_date;
11203     l_selv_tbl(1).se_line_number := 1;
11204     l_start_date := ylds_rec.contract_start_date;
11205 
11206     l_selv_tbl(1).amount := 0.0;
11207     print( l_api_name, ' khr_yld ' || to_char( l_khr_yld ) );
11208 
11209     -- Fetch the day convention ..
11210     OKL_PRICING_UTILS_PVT.get_day_convention(
11211       p_id              => p_contract_id,
11212       p_source          => 'ISG',
11213       x_days_in_month   => l_day_convention_month,
11214       x_days_in_year    => l_day_convention_year,
11215       x_return_status   => x_return_status);
11216     print( 'gen_pv_streams', 'Month / Year = ' || l_day_convention_month || '/' || l_day_convention_year );
11217     IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
11218       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11219     ELSIF (x_return_status = G_RET_STS_ERROR) THEN
11220       RAISE OKL_API.G_EXCEPTION_ERROR;
11221     END IF;
11222 
11223     FOR sec_strms_rec IN sec_strms_pend_csr
11224     LOOP
11225 
11226         l_end_date := sec_strms_rec.stream_element_date;
11227 
11228        l_days := OKL_PRICING_UTILS_PVT.get_day_count (p_start_date => l_start_date,
11229                                  p_days_in_month => l_day_convention_month,
11230                                  p_days_in_year => l_day_convention_year,
11231                                  p_end_date   => l_end_date,
11232                                  p_arrears    => 'N',
11233                                  x_return_status => x_return_status);
11234 
11235         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11236           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11237         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11238           RAISE OKL_API.G_EXCEPTION_ERROR;
11239         END IF;
11240 
11241         l_selv_tbl(1).amount := l_selv_tbl(1).amount +
11242 	              sec_strms_rec.stream_ele_amount / POWER( 1 + (l_khr_yld/360), l_days );
11243 
11244     print( l_api_name, ' amount ' || to_char( l_selv_tbl(1).amount ) );
11245     END LOOP;
11246   END IF;
11247  END IF;
11248     x_stmv_rec := l_stmv_rec;
11249     x_selv_tbl := l_selv_tbl;
11250 
11251   END IF;
11252 
11253     print( l_api_name, 'end' );
11254     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
11255 
11256     Exception
11257 
11258 	when OKL_API.G_EXCEPTION_ERROR then
11259 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
11260 			p_api_name  => l_api_name,
11261 			p_pkg_name  => g_pkg_name,
11262 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
11263 			x_msg_count => x_msg_count,
11264 			x_msg_data  => x_msg_data,
11265 			p_api_type  => g_api_type);
11266 
11267 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
11268 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
11269 			p_api_name  => l_api_name,
11270 			p_pkg_name  => g_pkg_name,
11271 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
11272 			x_msg_count => x_msg_count,
11273 			x_msg_data  => x_msg_data,
11274 			p_api_type  => g_api_type);
11275 
11276 	when OTHERS then
11277       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
11278 			p_api_name  => l_api_name,
11279 			p_pkg_name  => g_pkg_name,
11280 			p_exc_name  => 'OTHERS',
11281 			x_msg_count => x_msg_count,
11282 			x_msg_data  => x_msg_data,
11283 			p_api_type  => g_api_type);
11284 
11285    END gen_pv_streams;
11286 
11287    PROCEDURE  gen_disb_streams(p_api_version         IN      NUMBER,
11288                                p_init_msg_list       IN      VARCHAR2,
11289                                p_pool_status         IN      VARCHAR2,
11290                                p_agreement_id        IN      NUMBER,
11291                                p_contract_id         IN      NUMBER,
11292                                p_kle_id              IN      NUMBER,
11293                                p_sty_id              IN      NUMBER,
11294 --sosharma added, bug 6691554
11295                                p_mode                IN      VARCHAR2,
11296                                x_stmv_rec            OUT NOCOPY okl_streams_pub.stmv_rec_type,
11297                                x_selv_tbl            OUT NOCOPY okl_streams_pub.selv_tbl_type,
11298                                x_return_status       OUT NOCOPY VARCHAR2,
11299                                x_msg_count           OUT NOCOPY NUMBER,
11300                                x_msg_data            OUT NOCOPY VARCHAR2) IS
11301 
11302 
11303     l_api_name         CONSTANT VARCHAR2(61) := 'gen_disb_streams';
11304 
11305     Cursor sec_strms_csr IS
11306     select pol.khr_id agreement_id,
11307            pcn.khr_id contract_id,
11308 	   pcn.kle_id asset_id,
11309            pcn.sty_id stream_type_id,
11310            pcn.streams_from_date,
11311 	   pcn.streams_to_date,
11312 	   ele.STREAM_ELEMENT_DATE,
11313 	   ele.se_line_number,
11314 	   ele.id stream_ele_id,
11315 	   ele.amount stream_ele_amount,
11316 	   sty.name stream_name,
11317 	   stm.id stm_id
11318     from   okl_pools pol,
11319            okl_pool_contents pcn,
11320            okl_strm_type_v sty,
11321 	   okl_streams stm,
11322 	   okl_strm_elements ele
11323      where pcn.sty_id = sty.id
11324        and pcn.pol_id = pol.id
11325        and stm.kle_id = pcn.kle_id
11326        and stm.khr_id = pcn.khr_id
11327        and stm.sty_id = pcn.sty_id
11328        and pcn.status_code IN (G_IA_STS_ACTIVE,G_IA_STS_NEW)
11329        and stm.say_code = 'CURR'
11330        and stm.active_yn = 'Y'
11331        and ele.stm_id = stm.id
11332        and pol.khr_id = p_agreement_id
11333        and stm.khr_id = p_contract_id
11334        and stm.kle_id = p_kle_id
11335        and stm.sty_id = p_sty_id
11336        and pol.status_code = p_pool_status
11337        and ele.stream_element_date >= nvl(pcn.streams_from_date, ele.stream_element_date-1)
11338        and ele.stream_element_date <= nvl(pcn.STREAMS_TO_DATE, OKL_POOL_PVT.G_FINAL_DATE)
11339        order by ele.stream_element_date,pcn.khr_id, pcn.kle_id, pcn.sty_id;
11340 
11341       sec_strms_rec sec_strms_csr%ROWTYPE;
11342 
11343     Cursor ylds_csr( agmntID NUMBER, khrId NUMBER) IS
11344     select pol.khr_id agreement_id,
11345 	   pcn.khr_id contract_id,
11346 	   nvl(pkhr.after_tax_yield, -1) agmnt_yield,
11347 	   pkhr.contract_number agreement_number,
11348 	   nvl(khr.PRE_TAX_IRR, -1) contract_yield
11349     from  okl_pools pol,
11350 	  okl_pool_contents pcn,
11351 	  okl_K_headers_full_v pkhr,
11352 	  okl_k_headers khr
11353     where pcn.pol_id = pol.id
11354       and pcn.khr_id = khr.id
11355       and pol.khr_id = pkhr.id
11356       and pol.khr_id = agmntID
11357       and pcn.khr_id = khrId
11358       AND pcn.status_code IN (G_IA_STS_ACTIVE,G_IA_STS_NEW) ; -- Added by VARANGAN -Pool Contents Impact - 26/11/07
11359 
11360     ylds_rec ylds_csr%ROWTYPE;
11361 
11362     /* sosharma, 14-12-2007
11363     Added Cursors for handling pending status for pool contents
11364     Start changes
11365     */
11366 
11367     Cursor sec_strms_pend_csr IS
11368     select pol.khr_id agreement_id,
11369            pcn.khr_id contract_id,
11370 	   pcn.kle_id asset_id,
11371            pcn.sty_id stream_type_id,
11372            pcn.streams_from_date,
11373 	   pcn.streams_to_date,
11374 	   ele.STREAM_ELEMENT_DATE,
11375 	   ele.se_line_number,
11376 	   ele.id stream_ele_id,
11377 	   ele.amount stream_ele_amount,
11378 	   sty.name stream_name,
11379 	   stm.id stm_id
11380     from   okl_pools pol,
11381            okl_pool_contents pcn,
11382            okl_strm_type_v sty,
11383 	   okl_streams stm,
11384 	   okl_strm_elements ele
11385      where pcn.sty_id = sty.id
11386        and pcn.pol_id = pol.id
11387        and stm.kle_id = pcn.kle_id
11388        and stm.khr_id = pcn.khr_id
11389        and stm.sty_id = pcn.sty_id
11390        and pcn.status_code IN (G_PC_STS_PENDING)
11391        and stm.say_code = 'CURR'
11392        and stm.active_yn = 'Y'
11393        and ele.stm_id = stm.id
11394        and pol.khr_id = p_agreement_id
11395        and stm.khr_id = p_contract_id
11396        and stm.kle_id = p_kle_id
11397        and stm.sty_id = p_sty_id
11398        and pol.status_code = p_pool_status
11399        and ele.stream_element_date >= nvl(pcn.streams_from_date, ele.stream_element_date-1)
11400        and ele.stream_element_date <= nvl(pcn.STREAMS_TO_DATE, OKL_POOL_PVT.G_FINAL_DATE)
11401        order by ele.stream_element_date,pcn.khr_id, pcn.kle_id, pcn.sty_id;
11402 
11403 
11404        Cursor ylds_pend_csr( agmntID NUMBER, khrId NUMBER) IS
11405        select pol.khr_id agreement_id,
11406        pcn.khr_id contract_id,
11407        nvl(pkhr.after_tax_yield, -1) agmnt_yield,
11408        pkhr.contract_number agreement_number,
11409        nvl(khr.PRE_TAX_IRR, -1) contract_yield
11410        from  okl_pools pol,
11411       okl_pool_contents pcn,
11412       okl_K_headers_full_v pkhr,
11413       okl_k_headers khr
11414        where pcn.pol_id = pol.id
11415         and pcn.khr_id = khr.id
11416         and pol.khr_id = pkhr.id
11417         and pol.khr_id = agmntID
11418         and pcn.khr_id = khrId
11419         and pcn.status_code IN (G_PC_STS_PENDING) ;
11420 
11421     /* Sosharma End changes*/
11422 
11423     l_agt_yld NUMBER;
11424     l_khr_yld NUMBER;
11425 
11426     l_sty_id NUMBER;
11427     l_sty_name               VARCHAR2(150);
11428     l_mapped_sty_name        VARCHAR2(150);
11429 
11430     l_selv_tbl               okl_streams_pub.selv_tbl_type;
11431     l_stmv_tbl               okl_streams_pub.stmv_tbl_type;
11432     x_stmv_tbl               okl_streams_pub.stmv_tbl_type;
11433     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
11434 
11435     l_stmv_rec               okl_streams_pub.stmv_rec_type;
11436     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
11437 
11438    cursor l_hdrrl_csr IS
11439    select crl.RULE_INFORMATION1 method
11440    from   OKC_RULE_GROUPS_B crg,
11441           OKC_RULES_B crl
11442    where  crl.rgp_id = crg.id
11443           and crg.RGD_CODE = 'LASEIR'
11444           and crl.RULE_INFORMATION_CATEGORY = 'LASEIR'
11445           and crg.dnz_chr_id = p_agreement_id;
11446 
11447    l_hdrrl_rec l_hdrrl_csr%ROWTYPE;
11448 
11449    Cursor c_stm_name IS
11450    Select sty.stream_type_subclass name,
11451              sty.stream_type_purpose stream_type_purpose
11452    from okl_strm_type_tl tl,
11453         okl_strm_type_v sty
11454    where tl.language = 'US'
11455      and sty.id = tl.id
11456      and tl.id = p_sty_id;
11457 
11458    r_stm_name c_stm_name%ROWTYPE;
11459 
11460    i NUMBER;
11461    n NUMBER;
11462 
11463    l_rentsty_id NUMBER;
11464    l_rvsty_id   NUMBER;
11465    l_rentsty_name VARCHAR2(256);
11466    l_rvsty_name   VARCHAR2(256);
11467 
11468    BEGIN
11469 
11470     print( l_api_name, 'begin' );
11471 
11472     x_return_status := OKL_API.G_RET_STS_SUCCESS;
11473 
11474     x_return_status := OKL_API.START_ACTIVITY(
11475 			p_api_name      => l_api_name,
11476 			p_pkg_name      => g_pkg_name,
11477 			p_init_msg_list => p_init_msg_list,
11478 			l_api_version   => p_api_version,
11479 			p_api_version   => p_api_version,
11480 			p_api_type      => G_API_TYPE,
11481 			x_return_status => x_return_status);
11482 
11483     -- check if activity started successfully
11484     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
11485        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11486     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
11487        raise OKL_API.G_EXCEPTION_ERROR;
11488     End If;
11489 
11490     OPEN l_hdrrl_csr;
11491     FETCH l_hdrrl_csr INTO l_hdrrl_rec;
11492     CLOSE l_hdrrl_csr;
11493     print( l_api_name, ' method ' || l_hdrrl_rec.method );
11494 
11495 /* sosharma ,14-12-2007
11496 Bug 6691554
11497 Start Changes*/
11498 
11499 --Based on the p_mode passed to the procedure two flows have been defined
11500 IF p_mode IS NULL THEN
11501 --(1) When Streams are getting generated on activation of Investor Agreement/Changes in contract
11502     OPEN sec_strms_csr;
11503     FETCH sec_strms_csr INTO sec_strms_rec;
11504     CLOSE sec_strms_csr;
11505 
11506 
11507     If  ( l_hdrrl_rec.method = 'YIELD' )  Then
11508 
11509         OPEN ylds_csr( p_agreement_id, sec_strms_rec.contract_id );
11510         FETCH ylds_csr INTO ylds_rec;
11511         CLOSE ylds_csr;
11512 
11513         If (( ylds_rec.agmnt_yield = -1 ) OR (ylds_rec.contract_yield = -1 )) Then
11514 
11515             OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
11516                                 p_msg_name     => 'OKL_AGMNT_NOYLD',
11517                                 p_token1       => 'AGMNT',
11518                                 p_token1_value => ylds_rec.agreement_number);
11519 
11520             RAISE OKL_API.G_EXCEPTION_ERROR;
11521 
11522         End If;
11523 
11524 	l_agt_yld := ylds_rec.agmnt_yield/100.0;
11525 	l_khr_yld := ylds_rec.contract_yield/100.0;
11526 
11527     ElsIf  ( nvl(l_hdrrl_rec.method, 'XYZ') = 'XYZ' )  Then
11528 
11529         OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
11530                             p_msg_name     => 'OKL_INVALID_METHOD',
11531                             p_token1       => 'AGMNT',
11532                             p_token1_value => ylds_rec.agreement_number);
11533 
11534         RAISE OKL_API.G_EXCEPTION_ERROR;
11535 
11536     End If;
11537 
11538     OPEN c_stm_name;
11539     FETCH c_stm_name INTO r_stm_name;
11540     CLOSE c_stm_name;
11541     IF r_stm_name.name = 'LOAN_PAYMENT' THEN
11542       get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
11543                        p_mapping_type   =>  'DISBURSEMENT',
11544                        p_line_style     =>  NULL,
11545                        p_deal_type      =>  'LOAN',
11546                        p_khr_id         =>  p_agreement_id,
11547                        p_stream_type_purpose => r_stm_name.stream_type_purpose,
11548                        x_mapped_stream  =>  l_mapped_sty_name,
11549                        x_return_status  =>  x_return_status);
11550 
11551     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11552         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11553     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11554         RAISE OKL_API.G_EXCEPTION_ERROR;
11555     END IF;
11556       If l_mapped_sty_name IS NOT NULL Then
11557 
11558     get_sty_details (p_sty_name      => l_mapped_sty_name,
11559                      x_sty_id        => l_sty_id,
11560                      x_sty_name      => l_sty_name,
11561                      x_return_status => x_return_status);
11562 
11563     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11564         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11565     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11566         RAISE OKL_API.G_EXCEPTION_ERROR;
11567     END IF;
11568 
11569     l_stmv_rec := NULL;
11570     get_stream_header(p_purpose_code   => NULL,
11571                       p_khr_id         => p_contract_id,
11572                       p_kle_id         => p_kle_id,
11573                       p_sty_id         => l_sty_id,
11574                       x_stmv_rec       => l_stmv_rec,
11575                       x_return_status  => x_return_status);
11576 
11577     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11578         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11579     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11580         RAISE OKL_API.G_EXCEPTION_ERROR;
11581     END IF;
11582 
11583     FOR sec_strms_rec IN sec_strms_csr
11584     LOOP
11585 
11586 	i := l_selv_tbl.COUNT + 1;
11587 	l_selv_tbl(i).se_line_number := i;
11588 	l_selv_tbl(i).sel_id := sec_strms_rec.stream_ele_id;
11589 
11590 	l_selv_tbl(i).stream_element_date := sec_strms_rec.stream_element_date;
11591 	If ( l_hdrrl_rec.method = 'CASH_FLOW' ) Then
11592 	    l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount;
11593 	Elsif ( l_hdrrl_rec.method = 'YIELD' ) Then
11594 	    l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount *
11595 	                 POWER(( 1+l_agt_yld )/(1+l_khr_yld), i);
11596 	ENd If;
11597 	l_stmv_rec.stm_id := sec_strms_rec.stm_id;
11598 
11599     END LOOP;
11600 
11601     x_stmv_rec := l_stmv_rec;
11602     x_selv_tbl := l_selv_tbl;
11603 
11604   End If;
11605 
11606     ELSE
11607         get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
11608                        p_mapping_type   =>  'DISBURSEMENT',
11609                        p_line_style     =>  NULL,
11610                        p_deal_type      =>  'LEASE',
11611 		       p_khr_id         =>  p_agreement_id,
11612                        x_mapped_stream  =>  l_mapped_sty_name,
11613                        x_return_status  =>  x_return_status);
11614 
11615     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11616         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11617     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11618         RAISE OKL_API.G_EXCEPTION_ERROR;
11619     END IF;
11620       If l_mapped_sty_name IS NOT NULL Then
11621 
11622     get_sty_details (p_sty_name      => l_mapped_sty_name,
11623                      x_sty_id        => l_sty_id,
11624                      x_sty_name      => l_sty_name,
11625                      x_return_status => x_return_status);
11626 
11627     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11628         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11629     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11630         RAISE OKL_API.G_EXCEPTION_ERROR;
11631     END IF;
11632 
11633     l_stmv_rec := NULL;
11634     get_stream_header(p_purpose_code   => NULL,
11635                       p_khr_id         => p_contract_id,
11636                       p_kle_id         => p_kle_id,
11637                       p_sty_id         => l_sty_id,
11638                       x_stmv_rec       => l_stmv_rec,
11639                       x_return_status  => x_return_status);
11640 
11641     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11642         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11643     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11644         RAISE OKL_API.G_EXCEPTION_ERROR;
11645     END IF;
11646 
11647     FOR sec_strms_rec IN sec_strms_csr
11648     LOOP
11649 
11650 	i := l_selv_tbl.COUNT + 1;
11651 	l_selv_tbl(i).se_line_number := i;
11652 	l_selv_tbl(i).sel_id := sec_strms_rec.stream_ele_id;
11653 
11654 	l_selv_tbl(i).stream_element_date := sec_strms_rec.stream_element_date;
11655 	If ( l_hdrrl_rec.method = 'CASH_FLOW' ) Then
11656 	    l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount;
11657 	Elsif ( l_hdrrl_rec.method = 'YIELD' ) Then
11658 	    l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount *
11659 	                 POWER(( 1+l_agt_yld )/(1+l_khr_yld), i);
11660 	ENd If;
11661 	l_stmv_rec.stm_id := sec_strms_rec.stm_id;
11662 
11663     END LOOP;
11664 
11665     x_stmv_rec := l_stmv_rec;
11666     x_selv_tbl := l_selv_tbl;
11667 
11668   End If;
11669 
11670    END IF;
11671 
11672 ELSE
11673 
11674     OPEN sec_strms_pend_csr;
11675     FETCH sec_strms_pend_csr INTO sec_strms_rec;
11676     CLOSE sec_strms_pend_csr;
11677 
11678 
11679     If  ( l_hdrrl_rec.method = 'YIELD' )  Then
11680 
11681         OPEN ylds_pend_csr( p_agreement_id, sec_strms_rec.contract_id );
11682         FETCH ylds_pend_csr INTO ylds_rec;
11683         CLOSE ylds_pend_csr;
11684 
11685         If (( ylds_rec.agmnt_yield = -1 ) OR (ylds_rec.contract_yield = -1 )) Then
11686 
11687             OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
11688                                 p_msg_name     => 'OKL_AGMNT_NOYLD',
11689                                 p_token1       => 'AGMNT',
11690                                 p_token1_value => ylds_rec.agreement_number);
11691 
11692             RAISE OKL_API.G_EXCEPTION_ERROR;
11693 
11694         End If;
11695 
11696 	l_agt_yld := ylds_rec.agmnt_yield/100.0;
11697 	l_khr_yld := ylds_rec.contract_yield/100.0;
11698 
11699     ElsIf  ( nvl(l_hdrrl_rec.method, 'XYZ') = 'XYZ' )  Then
11700 
11701         OKL_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
11702                             p_msg_name     => 'OKL_INVALID_METHOD',
11703                             p_token1       => 'AGMNT',
11704                             p_token1_value => ylds_rec.agreement_number);
11705 
11706         RAISE OKL_API.G_EXCEPTION_ERROR;
11707 
11708     End If;
11709 
11710     OPEN c_stm_name;
11711     FETCH c_stm_name INTO r_stm_name;
11712     CLOSE c_stm_name;
11713      IF r_stm_name.name = 'LOAN_PAYMENT' THEN
11714       get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
11715                        p_mapping_type   =>  'DISBURSEMENT',
11716                        p_line_style     =>  NULL,
11717                        p_deal_type      =>  'LOAN',
11718                        p_khr_id         =>  p_agreement_id,
11719                        p_stream_type_purpose => r_stm_name.stream_type_purpose,
11720                        x_mapped_stream  =>  l_mapped_sty_name,
11721                        x_return_status  =>  x_return_status);
11722 
11723       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11724         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11725       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11726         RAISE OKL_API.G_EXCEPTION_ERROR;
11727       END IF;
11728       If l_mapped_sty_name IS NOT NULL Then
11729 
11730     get_sty_details (p_sty_name      => l_mapped_sty_name,
11731                      x_sty_id        => l_sty_id,
11732                      x_sty_name      => l_sty_name,
11733                      x_return_status => x_return_status);
11734 
11735     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11736         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11737     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11738         RAISE OKL_API.G_EXCEPTION_ERROR;
11739     END IF;
11740 
11741     l_stmv_rec := NULL;
11742     get_stream_header(p_purpose_code   => NULL,
11743                       p_khr_id         => p_contract_id,
11744                       p_kle_id         => p_kle_id,
11745                       p_sty_id         => l_sty_id,
11746                       x_stmv_rec       => l_stmv_rec,
11747                       x_return_status  => x_return_status);
11748 
11749     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11750         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11751     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11752         RAISE OKL_API.G_EXCEPTION_ERROR;
11753     END IF;
11754 
11755     FOR sec_strms_rec IN sec_strms_pend_csr
11756     LOOP
11757 
11758        i := l_selv_tbl.COUNT + 1;
11759        l_selv_tbl(i).se_line_number := i;
11760        l_selv_tbl(i).sel_id := sec_strms_rec.stream_ele_id;
11761 
11762        l_selv_tbl(i).stream_element_date := sec_strms_rec.stream_element_date;
11763        If ( l_hdrrl_rec.method = 'CASH_FLOW' ) Then
11764            l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount;
11765        Elsif ( l_hdrrl_rec.method = 'YIELD' ) Then
11766            l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount *
11767                         POWER(( 1+l_agt_yld )/(1+l_khr_yld), i);
11768        End If;
11769        l_stmv_rec.stm_id := sec_strms_rec.stm_id;
11770 
11771     END LOOP;
11772 
11773     x_stmv_rec := l_stmv_rec;
11774     x_selv_tbl := l_selv_tbl;
11775 
11776   End If;
11777   ELSE
11778          get_mapped_stream (p_sty_purpose       =>  r_stm_name.name,
11779                        p_mapping_type   =>  'DISBURSEMENT',
11780                        p_line_style     =>  NULL,
11781                        p_deal_type      =>  'LEASE',
11782 		                     p_khr_id         =>  p_agreement_id,
11783                        x_mapped_stream  =>  l_mapped_sty_name,
11784                        x_return_status  =>  x_return_status);
11785 
11786       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11787         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11788       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11789         RAISE OKL_API.G_EXCEPTION_ERROR;
11790       END IF;
11791       If l_mapped_sty_name IS NOT NULL Then
11792 
11793     get_sty_details (p_sty_name      => l_mapped_sty_name,
11794                      x_sty_id        => l_sty_id,
11795                      x_sty_name      => l_sty_name,
11796                      x_return_status => x_return_status);
11797 
11798     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11799         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11800     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11801         RAISE OKL_API.G_EXCEPTION_ERROR;
11802     END IF;
11803 
11804     l_stmv_rec := NULL;
11805     get_stream_header(p_purpose_code   => NULL,
11806                       p_khr_id         => p_contract_id,
11807                       p_kle_id         => p_kle_id,
11808                       p_sty_id         => l_sty_id,
11809                       x_stmv_rec       => l_stmv_rec,
11810                       x_return_status  => x_return_status);
11811 
11812     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
11813         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11814     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
11815         RAISE OKL_API.G_EXCEPTION_ERROR;
11816     END IF;
11817 
11818     FOR sec_strms_rec IN sec_strms_pend_csr
11819     LOOP
11820 
11821        i := l_selv_tbl.COUNT + 1;
11822        l_selv_tbl(i).se_line_number := i;
11823        l_selv_tbl(i).sel_id := sec_strms_rec.stream_ele_id;
11824 
11825        l_selv_tbl(i).stream_element_date := sec_strms_rec.stream_element_date;
11826        If ( l_hdrrl_rec.method = 'CASH_FLOW' ) Then
11827            l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount;
11828        Elsif ( l_hdrrl_rec.method = 'YIELD' ) Then
11829            l_selv_tbl(i).amount := sec_strms_rec.stream_ele_amount *
11830                         POWER(( 1+l_agt_yld )/(1+l_khr_yld), i);
11831        End If;
11832        l_stmv_rec.stm_id := sec_strms_rec.stm_id;
11833 
11834     END LOOP;
11835 
11836     x_stmv_rec := l_stmv_rec;
11837     x_selv_tbl := l_selv_tbl;
11838 
11839   End If;
11840  END IF;
11841 END IF;
11842 /* sosharma End Changes*/
11843 
11844   print( l_api_name, 'end' );
11845     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
11846 
11847     Exception
11848 
11849 	when OKL_API.G_EXCEPTION_ERROR then
11850 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
11851 			p_api_name  => l_api_name,
11852 			p_pkg_name  => g_pkg_name,
11853 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
11854 			x_msg_count => x_msg_count,
11855 			x_msg_data  => x_msg_data,
11856 			p_api_type  => g_api_type);
11857 
11858 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
11859 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
11860 			p_api_name  => l_api_name,
11861 			p_pkg_name  => g_pkg_name,
11862 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
11863 			x_msg_count => x_msg_count,
11864 			x_msg_data  => x_msg_data,
11865 			p_api_type  => g_api_type);
11866 
11867 	when OTHERS then
11868       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
11869 			p_api_name  => l_api_name,
11870 			p_pkg_name  => g_pkg_name,
11871 			p_exc_name  => 'OTHERS',
11872 			x_msg_count => x_msg_count,
11873 			x_msg_data  => x_msg_data,
11874 			p_api_type  => g_api_type);
11875 
11876    END gen_disb_streams;
11877 
11878 
11879   PROCEDURE  gen_pv_disb_streams(p_api_version         IN      NUMBER,
11880                                        p_init_msg_list       IN      VARCHAR2,
11881                                        p_contract_id         IN      NUMBER,
11882                                        p_agreement_id        IN      NUMBER,
11883                                	       p_pool_status         IN      VARCHAR2,
11884                                        p_mode                IN      VARCHAR2 DEFAULT NULL,
11885                                        x_return_status       OUT NOCOPY VARCHAR2,
11886                                        x_msg_count           OUT NOCOPY NUMBER,
11887                                        x_msg_data            OUT NOCOPY VARCHAR2) IS
11888 
11889 
11890     l_api_name         CONSTANT VARCHAR2(61) := 'gen_pv_disb_streams';
11891 
11892     Cursor l_strms_csr ( khrId NUMBER, kleId NUMBER, strmname VARCHAR2) IS
11893     select stm.id strm_id
11894     from   okl_strm_type_v sty,
11895 	   okl_strm_type_tl stl,
11896 	   okl_streams stm
11897      where stl.language = 'US'
11898        and stl.name = strmname
11899        and sty.id = stl.id
11900        and stm.sty_id = sty.id
11901        and stm.kle_id = kleId
11902        and stm.khr_id = khrId
11903        and stm.say_code = 'CURR';
11904 
11905     l_strms_rec l_strms_csr%ROWTYPE;
11906 
11907     Cursor sec_strms_csr ( poolstat VARCHAR2 ) IS
11908     select pol.khr_id agreement_id,
11909            pcn.khr_id contract_id,
11910            pcn.kle_id asset_id,
11911            pcn.sty_id stream_type_id,
11912            sty.stream_type_subclass stream_type_subclass,
11913            sty.stream_type_purpose stream_type_purpose
11914     from   okl_pools pol,
11915            okl_pool_contents pcn,
11916 	   okl_strm_type_v sty,
11917 	   okl_streams stm
11918      where pcn.sty_id = sty.id
11919        and pcn.pol_id = pol.id
11920        and stm.kle_id = pcn.kle_id
11921        and stm.khr_id = pcn.khr_id
11922        and stm.sty_id = pcn.sty_id
11923        and stm.say_code = 'CURR'
11924        and stm.active_yn = 'Y'
11925        and pol.status_code = poolstat
11926        and nvl(stm.khr_id, 0) = nvl(p_contract_id, -1)
11927        and pcn.status_code IN (G_IA_STS_NEW,G_IA_STS_ACTIVE)
11928     Union
11929     select pol.khr_id agreement_id,
11930            pcn.khr_id contract_id,
11931            pcn.kle_id asset_id,
11932            pcn.sty_id stream_type_id,
11933            sty.stream_type_subclass stream_type_subclass,
11934            sty.stream_type_purpose stream_type_purpose
11935     from   okl_pools pol,
11936            okl_pool_contents pcn,
11937 	   okl_strm_type_v sty,
11938 	   okl_streams stm
11939      where pcn.sty_id = sty.id
11940        and pcn.pol_id = pol.id
11941        and stm.kle_id = pcn.kle_id
11942        and stm.khr_id = pcn.khr_id
11943        and stm.sty_id = pcn.sty_id
11944        and stm.say_code = 'CURR'
11945        and stm.active_yn = 'Y'
11946        and pol.status_code = poolstat
11947        and nvl(pol.khr_id, 0) = nvl(p_agreement_id, -1)
11948        and pcn.status_code IN (G_IA_STS_NEW,G_IA_STS_ACTIVE)
11949        order by agreement_id, contract_id, asset_id, stream_type_id;
11950 
11951 /* sosharma ,14-12-2007
11952 Bug 6691554
11953 Start Changes*/
11954 
11955     Cursor sec_strms_pend_csr ( poolstat VARCHAR2 ) IS
11956     select pol.khr_id agreement_id,
11957            pcn.khr_id contract_id,
11958            pcn.kle_id asset_id,
11959            pcn.sty_id stream_type_id,
11960           sty.stream_type_subclass stream_type_subclass,
11961            sty.stream_type_purpose stream_type_purpose
11962     from   okl_pools pol,
11963            okl_pool_contents pcn,
11964            okl_strm_type_v sty,
11965            okl_streams stm
11966      where pcn.sty_id = sty.id
11967        and pcn.pol_id = pol.id
11968        and stm.kle_id = pcn.kle_id
11969        and stm.khr_id = pcn.khr_id
11970        and stm.sty_id = pcn.sty_id
11971        and stm.say_code = 'CURR'
11972        and stm.active_yn = 'Y'
11973        and pol.status_code = poolstat
11974        and nvl(stm.khr_id, 0) = nvl(p_contract_id, -1)
11975        and pcn.status_code=G_PC_STS_PENDING
11976     Union
11977     select pol.khr_id agreement_id,
11978            pcn.khr_id contract_id,
11979            pcn.kle_id asset_id,
11980            pcn.sty_id stream_type_id,
11981            sty.stream_type_subclass stream_type_subclass,
11982            sty.stream_type_purpose stream_type_purpose
11983     from   okl_pools pol,
11984            okl_pool_contents pcn,
11985            okl_strm_type_v sty,
11986            okl_streams stm
11987      where pcn.sty_id = sty.id
11988        and pcn.pol_id = pol.id
11989        and stm.kle_id = pcn.kle_id
11990        and stm.khr_id = pcn.khr_id
11991        and stm.sty_id = pcn.sty_id
11992        and stm.say_code = 'CURR'
11993        and stm.active_yn = 'Y'
11994        and pol.status_code = poolstat
11995        and nvl(pol.khr_id, 0) = nvl(p_agreement_id, -1)
11996        and pcn.status_code=G_PC_STS_PENDING
11997        order by agreement_id, contract_id, asset_id, stream_type_id;
11998 
11999 /* sosharma End Changes*/
12000       sec_strms_rec sec_strms_csr%ROWTYPE;
12001 
12002     l_sty_id NUMBER;
12003 
12004     l_selv_tbl               okl_streams_pub.selv_tbl_type;
12005     l_stmv_tbl               okl_streams_pub.stmv_tbl_type;
12006     x_stmv_tbl               okl_streams_pub.stmv_tbl_type;
12007     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
12008 
12009     l_stmv_rec               okl_streams_pub.stmv_rec_type;
12010     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
12011 
12012    i NUMBER;
12013    n NUMBER;
12014 
12015    l_sty_id NUMBER;
12016    l_sty_name VARCHAR2(256);
12017 
12018    l_pool_status VARCHAR2(256);
12019    l_mapped_sty_name        VARCHAR2(150);
12020 
12021    BEGIN
12022 
12023     print( l_api_name, 'begin' );
12024 
12025     x_return_status := OKL_API.G_RET_STS_SUCCESS;
12026 
12027     x_return_status := OKL_API.START_ACTIVITY(
12028 			p_api_name      => l_api_name,
12029 			p_pkg_name      => g_pkg_name,
12030 			p_init_msg_list => p_init_msg_list,
12031 			l_api_version   => p_api_version,
12032 			p_api_version   => p_api_version,
12033 			p_api_type      => G_API_TYPE,
12034 			x_return_status => x_return_status);
12035 
12036     -- check if activity started successfully
12037     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
12038        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12039     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
12040        raise OKL_API.G_EXCEPTION_ERROR;
12041     End If;
12042 
12043     print( l_api_name, 'save point' );
12044 
12045     If ( nvl(p_pool_status, 'XXX') = 'XXX' ) AND ( nvl(p_agreement_id, -1) <> -1  ) Then
12046         l_pool_status := 'NEW';
12047     ELsif( nvl(p_pool_status, 'XXX') = 'XXX' ) AND ( nvl(p_contract_id, -1) <> -1) Then
12048         l_pool_status := 'ACTIVE';
12049     Else
12050         l_pool_status := p_pool_status;
12051     End If;
12052 
12053     print( l_api_name, 'pool stat' || l_pool_status );
12054 
12055   IF p_mode IS NULL THEN
12056     FOR sec_strms_rec IN sec_strms_csr( l_pool_status )
12057     LOOP
12058          IF sec_strms_rec.stream_type_subclass = 'LOAN_PAYMENT' THEN
12059             get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12060                            p_mapping_type   =>  'PV_DISBURSEMENT',
12061                            p_line_style     =>  NULL,
12062                            p_deal_type      =>  'LOAN',
12063                            p_khr_id         =>  sec_strms_rec.agreement_id,
12064                            p_stream_type_purpose => sec_strms_rec.stream_type_purpose,
12065                            x_mapped_stream  =>  l_mapped_sty_name,
12066                            x_return_status  =>  x_return_status);
12067 
12068         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12069             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12070         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12071             RAISE OKL_API.G_EXCEPTION_ERROR;
12072         END IF;
12073 
12074       If ( l_mapped_sty_name IS NOT NULL ) Then
12075 
12076         print( l_api_name, ' mapped stream ' || l_mapped_sty_name, x_return_status );
12077         i := 0;
12078         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12079 	                                 sec_strms_rec.asset_id,
12080 					 l_mapped_sty_name )
12081         LOOP
12082 
12083             i := i + 1;
12084             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12085             l_stmv_tbl(i).say_code := 'HIST';
12086             l_stmv_tbl(i).active_yn := 'N';
12087             l_stmv_tbl(i).date_history := sysdate;
12088 
12089         END LOOP;
12090 
12091         If ( l_stmv_tbl.COUNT > 0 ) Then
12092 
12093             Okl_Streams_pub.update_streams(
12094                              p_api_version => p_api_version,
12095                              p_init_msg_list => p_init_msg_list,
12096                              x_return_status => x_return_status,
12097                              x_msg_count => x_msg_count,
12098                              x_msg_data => x_msg_data,
12099                              p_stmv_tbl => l_stmv_tbl,
12100                              x_stmv_tbl => x_stmv_tbl);
12101 
12102             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12103                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12104             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12105                 RAISE OKL_API.G_EXCEPTION_ERROR;
12106             END IF;
12107 
12108         End If;
12109         l_stmv_tbl.delete;
12110 
12111       End If;
12112 
12113       ELSE
12114         get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12115                            p_mapping_type   =>  'PV_DISBURSEMENT',
12116                            p_line_style     =>  NULL,
12117                            p_deal_type      =>  'LEASE',
12118                        	   p_khr_id         =>  sec_strms_rec.agreement_id,
12119                            x_mapped_stream  =>  l_mapped_sty_name,
12120                            x_return_status  =>  x_return_status);
12121 
12122         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12123             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12124         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12125             RAISE OKL_API.G_EXCEPTION_ERROR;
12126         END IF;
12127 
12128       If ( l_mapped_sty_name IS NOT NULL ) Then
12129 
12130         print( l_api_name, ' mapped stream ' || l_mapped_sty_name, x_return_status );
12131         i := 0;
12132         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12133 	                                 sec_strms_rec.asset_id,
12134 					 l_mapped_sty_name )
12135         LOOP
12136 
12137             i := i + 1;
12138             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12139             l_stmv_tbl(i).say_code := 'HIST';
12140             l_stmv_tbl(i).active_yn := 'N';
12141             l_stmv_tbl(i).date_history := sysdate;
12142 
12143         END LOOP;
12144 
12145         If ( l_stmv_tbl.COUNT > 0 ) Then
12146 
12147             Okl_Streams_pub.update_streams(
12148                              p_api_version => p_api_version,
12149                              p_init_msg_list => p_init_msg_list,
12150                              x_return_status => x_return_status,
12151                              x_msg_count => x_msg_count,
12152                              x_msg_data => x_msg_data,
12153                              p_stmv_tbl => l_stmv_tbl,
12154                              x_stmv_tbl => x_stmv_tbl);
12155 
12156             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12157                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12158             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12159                 RAISE OKL_API.G_EXCEPTION_ERROR;
12160             END IF;
12161 
12162         End If;
12163         l_stmv_tbl.delete;
12164 
12165       End If;
12166       END IF;
12167 
12168     END LOOP;
12169 
12170     FOR sec_strms_rec IN sec_strms_csr( l_pool_status )
12171     LOOP
12172 
12173         l_stmv_rec := NULL;
12174         gen_pv_streams(p_api_version   => p_api_version,
12175                          p_init_msg_list => p_init_msg_list,
12176                          p_pool_status   => l_pool_status,
12177                          p_agreement_id  => sec_strms_rec.agreement_id,
12178                          p_contract_id   => sec_strms_rec.contract_id,
12179                          p_kle_id        => sec_strms_rec.asset_id,
12180                          p_sty_id        => sec_strms_rec.stream_type_id,
12181                          p_mode          => p_mode,
12182                          x_stmv_rec      => l_stmv_rec,
12183                          x_selv_tbl      => l_selv_tbl,
12184                          x_return_status => x_return_status,
12185                          x_msg_count     => x_msg_count,
12186                          x_msg_data      => x_msg_data);
12187 
12188          IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12189              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12190          ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12191              RAISE OKL_API.G_EXCEPTION_ERROR;
12192          END IF;
12193 
12194          l_stmv_rec.say_code := 'CURR';
12195 	 l_stmv_rec.date_current := sysdate;
12196 	 l_stmv_rec.active_yn := 'Y';
12197          l_stmv_rec.source_table := 'OKL_K_HEADERS';
12198          l_stmv_rec.source_id := sec_strms_rec.agreement_id;
12199 
12200          print( l_api_name, ' # of streams ' || to_char( l_selv_tbl.COUNT ) );
12201          If( l_selv_tbl.COUNT > 0 AND (l_stmv_rec.sty_id IS NOT NULL) ) Then
12202 
12203              print( l_api_name, ' creating pv disb streams ' );
12204 
12205             x_return_status := Okl_Streams_Util.round_streams_amount(
12206                                 p_api_version   => g_api_version,
12207                                 p_init_msg_list => p_init_msg_list,
12208                                 x_msg_count     => x_msg_count,
12209                                 x_msg_data      => x_msg_data,
12210                                 p_chr_id        => l_stmv_Rec.khr_id,
12211                                 p_selv_tbl      => l_selv_tbl,
12212                                 x_selv_tbl      => lx_selv_tbl);
12213 
12214             IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
12215                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12216             ELSIF (x_return_status = G_RET_STS_ERROR) THEN
12217                 RAISE OKL_API.G_EXCEPTION_ERROR;
12218             END IF;
12219 
12220             l_selv_tbl.DELETE;
12221             l_selv_tbl := lx_selv_tbl;
12222 
12223              okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
12224                                             p_init_msg_list   =>   G_FALSE,
12225                                             x_return_status   =>   x_return_status,
12226                                             x_msg_count       =>   x_msg_count,
12227                                             x_msg_data        =>   x_msg_data,
12228                                             p_stmv_rec        =>   l_stmv_rec,
12229                                             p_selv_tbl        =>   l_selv_tbl,
12230                                             x_stmv_rec        =>   lx_stmv_rec,
12231                                             x_selv_tbl        =>   lx_selv_tbl);
12232 
12233              print( l_api_name, ' creating pv disb streams  : done ', x_return_status );
12234                IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12235                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12236                ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12237                  RAISE OKL_API.G_EXCEPTION_ERROR;
12238                END IF;
12239 
12240                 l_selv_tbl.delete;
12241 
12242 	  End if;
12243 
12244     END LOOP;
12245  ELSE
12246   FOR sec_strms_rec IN sec_strms_pend_csr( l_pool_status )
12247     LOOP
12248         IF sec_strms_rec.stream_type_subclass = 'LOAN_PAYMENT' THEN
12249           get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12250                            p_mapping_type   =>  'PV_DISBURSEMENT',
12251                            p_line_style     =>  NULL,
12252                            p_deal_type      =>  'LOAN',
12253                            p_stream_type_purpose => sec_strms_rec.stream_type_purpose,
12254                            p_khr_id         =>  sec_strms_rec.agreement_id,
12255                            x_mapped_stream  =>  l_mapped_sty_name,
12256                            x_return_status  =>  x_return_status);
12257 
12258         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12259             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12260         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12261             RAISE OKL_API.G_EXCEPTION_ERROR;
12262         END IF;
12263 
12264       If ( l_mapped_sty_name IS NOT NULL ) Then
12265 
12266         print( l_api_name, ' mapped stream ' || l_mapped_sty_name, x_return_status );
12267         i := 0;
12268         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12269 	                                 sec_strms_rec.asset_id,
12270 					 l_mapped_sty_name )
12271         LOOP
12272 
12273             i := i + 1;
12274             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12275             l_stmv_tbl(i).say_code := 'HIST';
12276             l_stmv_tbl(i).active_yn := 'N';
12277             l_stmv_tbl(i).date_history := sysdate;
12278 
12279         END LOOP;
12280 
12281         If ( l_stmv_tbl.COUNT > 0 ) Then
12282 
12283             Okl_Streams_pub.update_streams(
12284                              p_api_version => p_api_version,
12285                              p_init_msg_list => p_init_msg_list,
12286                              x_return_status => x_return_status,
12287                              x_msg_count => x_msg_count,
12288                              x_msg_data => x_msg_data,
12289                              p_stmv_tbl => l_stmv_tbl,
12290                              x_stmv_tbl => x_stmv_tbl);
12291 
12292             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12293                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12294             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12295                 RAISE OKL_API.G_EXCEPTION_ERROR;
12296             END IF;
12297 
12298         End If;
12299         l_stmv_tbl.delete;
12300 
12301       End If;
12302       ELSE
12303         get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12304                            p_mapping_type   =>  'PV_DISBURSEMENT',
12305                            p_line_style     =>  NULL,
12306                            p_deal_type      =>  'LEASE',
12307                        	   p_khr_id         =>  sec_strms_rec.agreement_id,
12308                            x_mapped_stream  =>  l_mapped_sty_name,
12309                            x_return_status  =>  x_return_status);
12310 
12311         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12312             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12313         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12314             RAISE OKL_API.G_EXCEPTION_ERROR;
12315         END IF;
12316 
12317       If ( l_mapped_sty_name IS NOT NULL ) Then
12318 
12319         print( l_api_name, ' mapped stream ' || l_mapped_sty_name, x_return_status );
12320         i := 0;
12321         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12322 	                                 sec_strms_rec.asset_id,
12323 					 l_mapped_sty_name )
12324         LOOP
12325 
12326             i := i + 1;
12327             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12328             l_stmv_tbl(i).say_code := 'HIST';
12329             l_stmv_tbl(i).active_yn := 'N';
12330             l_stmv_tbl(i).date_history := sysdate;
12331 
12332         END LOOP;
12333 
12334         If ( l_stmv_tbl.COUNT > 0 ) Then
12335 
12336             Okl_Streams_pub.update_streams(
12337                              p_api_version => p_api_version,
12338                              p_init_msg_list => p_init_msg_list,
12339                              x_return_status => x_return_status,
12340                              x_msg_count => x_msg_count,
12341                              x_msg_data => x_msg_data,
12342                              p_stmv_tbl => l_stmv_tbl,
12343                              x_stmv_tbl => x_stmv_tbl);
12344 
12345             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12346                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12347             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12348                 RAISE OKL_API.G_EXCEPTION_ERROR;
12349             END IF;
12350 
12351         End If;
12352         l_stmv_tbl.delete;
12353 
12354       End If;
12355 END IF;
12356 
12357     END LOOP;
12358 
12359     FOR sec_strms_rec IN sec_strms_pend_csr( l_pool_status )
12360     LOOP
12361 
12362         l_stmv_rec := NULL;
12363         gen_pv_streams(p_api_version   => p_api_version,
12364                          p_init_msg_list => p_init_msg_list,
12365                          p_pool_status   => l_pool_status,
12366                          p_agreement_id  => sec_strms_rec.agreement_id,
12367                          p_contract_id   => sec_strms_rec.contract_id,
12368                          p_kle_id        => sec_strms_rec.asset_id,
12369                          p_sty_id        => sec_strms_rec.stream_type_id,
12370                          p_mode          => p_mode,
12371                          x_stmv_rec      => l_stmv_rec,
12372                          x_selv_tbl      => l_selv_tbl,
12373                          x_return_status => x_return_status,
12374                          x_msg_count     => x_msg_count,
12375                          x_msg_data      => x_msg_data);
12376 
12377          IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12378              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12379          ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12380              RAISE OKL_API.G_EXCEPTION_ERROR;
12381          END IF;
12382 
12383          l_stmv_rec.say_code := 'CURR';
12384 	 l_stmv_rec.date_current := sysdate;
12385 	 l_stmv_rec.active_yn := 'Y';
12386          l_stmv_rec.source_table := 'OKL_K_HEADERS';
12387          l_stmv_rec.source_id := sec_strms_rec.agreement_id;
12388 
12389          print( l_api_name, ' # of streams ' || to_char( l_selv_tbl.COUNT ) );
12390          If( l_selv_tbl.COUNT > 0 AND (l_stmv_rec.sty_id IS NOT NULL) ) Then
12391 
12392              print( l_api_name, ' creating pv disb streams ' );
12393 
12394             x_return_status := Okl_Streams_Util.round_streams_amount(
12395                                 p_api_version   => g_api_version,
12396                                 p_init_msg_list => p_init_msg_list,
12397                                 x_msg_count     => x_msg_count,
12398                                 x_msg_data      => x_msg_data,
12399                                 p_chr_id        => l_stmv_Rec.khr_id,
12400                                 p_selv_tbl      => l_selv_tbl,
12401                                 x_selv_tbl      => lx_selv_tbl);
12402 
12403             IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
12404                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12405             ELSIF (x_return_status = G_RET_STS_ERROR) THEN
12406                 RAISE OKL_API.G_EXCEPTION_ERROR;
12407             END IF;
12408 
12409             l_selv_tbl.DELETE;
12410             l_selv_tbl := lx_selv_tbl;
12411 
12412              okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
12413                                             p_init_msg_list   =>   G_FALSE,
12414                                             x_return_status   =>   x_return_status,
12415                                             x_msg_count       =>   x_msg_count,
12416                                             x_msg_data        =>   x_msg_data,
12417                                             p_stmv_rec        =>   l_stmv_rec,
12418                                             p_selv_tbl        =>   l_selv_tbl,
12419                                             x_stmv_rec        =>   lx_stmv_rec,
12420                                             x_selv_tbl        =>   lx_selv_tbl);
12421 
12422              print( l_api_name, ' creating pv disb streams  : done ', x_return_status );
12423                IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12424                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12425                ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12426                  RAISE OKL_API.G_EXCEPTION_ERROR;
12427                END IF;
12428 
12429                 l_selv_tbl.delete;
12430 
12431 	  End if;
12432 
12433     END LOOP;
12434 
12435  END IF;
12436 
12437     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
12438     print( l_api_name, 'end' );
12439 
12440     Exception
12441 
12442 	when OKL_API.G_EXCEPTION_ERROR then
12443 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
12444 			p_api_name  => l_api_name,
12445 			p_pkg_name  => g_pkg_name,
12446 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
12447 			x_msg_count => x_msg_count,
12448 			x_msg_data  => x_msg_data,
12449 			p_api_type  => g_api_type);
12450 
12451 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
12452 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
12453 			p_api_name  => l_api_name,
12454 			p_pkg_name  => g_pkg_name,
12455 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
12456 			x_msg_count => x_msg_count,
12457 			x_msg_data  => x_msg_data,
12458 			p_api_type  => g_api_type);
12459 
12460 	when OTHERS then
12461       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
12462 			p_api_name  => l_api_name,
12463 			p_pkg_name  => g_pkg_name,
12464 			p_exc_name  => 'OTHERS',
12465 			x_msg_count => x_msg_count,
12466 			x_msg_data  => x_msg_data,
12467 			p_api_type  => g_api_type);
12468 
12469    END gen_pv_disb_streams;
12470 
12471    PROCEDURE  gen_disbursement_streams(p_api_version         IN      NUMBER,
12472                                        p_init_msg_list       IN      VARCHAR2,
12473                                        p_contract_id         IN      NUMBER,
12474                                        p_agreement_id        IN      NUMBER,
12475                                        p_pool_status         IN      VARCHAR2,
12476 /* sosharma 14-12-2007 Added p_mode to the signature to pass mode
12477 from calling procedure*/
12478                                        p_mode                IN      VARCHAR2 DEFAULT NULL,
12479                                        x_return_status       OUT NOCOPY VARCHAR2,
12480                                        x_msg_count           OUT NOCOPY NUMBER,
12481                                        x_msg_data            OUT NOCOPY VARCHAR2) IS
12482 
12483 
12484     l_api_name         CONSTANT VARCHAR2(61) := 'gen_disbursement_streams';
12485 
12486     Cursor l_strms_csr ( khrId NUMBER, kleId NUMBER, strmname VARCHAR2) IS
12487     select stm.id strm_id
12488     from   okl_strm_type_v sty,
12489 	   okl_strm_type_tl stl,
12490 	   okl_streams stm
12491      where stl.language = 'US'
12492        and stl.name = strmname
12493        and sty.id = stl.id
12494        and stm.sty_id = sty.id
12495        and stm.kle_id = kleId
12496        and stm.khr_id = khrId
12497        and stm.say_code = 'CURR';
12498 
12499     l_strms_rec l_strms_csr%ROWTYPE;
12500 
12501     Cursor sec_strms_csr ( poolstat VARCHAR2 ) IS
12502     select pol.khr_id agreement_id,
12503            pcn.khr_id contract_id,
12504            pcn.kle_id asset_id,
12505            pcn.sty_id stream_type_id,
12506            sty.stream_type_subclass stream_type_subclass,
12507            sty.stream_type_purpose stream_type_purpose
12508     from   okl_pools pol,
12509            okl_pool_contents pcn,
12510            okl_strm_type_v sty,
12511            okl_streams stm
12512      where pcn.sty_id = sty.id
12513        and pcn.pol_id = pol.id
12514        and stm.kle_id = pcn.kle_id
12515        and stm.khr_id = pcn.khr_id
12516        and stm.sty_id = pcn.sty_id
12517        and stm.say_code = 'CURR'
12518        and stm.active_yn = 'Y'
12519        and pol.status_code = poolstat
12520        and nvl(stm.khr_id, 0) = nvl(p_contract_id, -1)
12521        and pcn.status_code IN(G_IA_STS_NEW,G_IA_STS_ACTIVE)
12522     Union
12523     select pol.khr_id agreement_id,
12524            pcn.khr_id contract_id,
12525            pcn.kle_id asset_id,
12526            pcn.sty_id stream_type_id,
12527           sty.stream_type_subclass stream_type_subclass,
12528           sty.stream_type_purpose stream_type_purpose
12529     from   okl_pools pol,
12530            okl_pool_contents pcn,
12531            okl_strm_type_v sty,
12532            okl_streams stm
12533      where pcn.sty_id = sty.id
12534        and pcn.pol_id = pol.id
12535        and stm.kle_id = pcn.kle_id
12536        and stm.khr_id = pcn.khr_id
12537        and stm.sty_id = pcn.sty_id
12538        and stm.say_code = 'CURR'
12539        and stm.active_yn = 'Y'
12540        and pol.status_code = poolstat
12541        and nvl(pol.khr_id, 0) = nvl(p_agreement_id, -1)
12542        and pcn.status_code IN(G_IA_STS_NEW,G_IA_STS_ACTIVE)
12543        order by agreement_id, contract_id, asset_id, stream_type_id;
12544 
12545 /* sosharma ,14-12-2007
12546 Bug 6691554
12547 Start Changes*/
12548 --Changed Cursor to pich up pending requests
12549 
12550     Cursor sec_strms_pend_csr ( poolstat VARCHAR2 ) IS
12551     select pol.khr_id agreement_id,
12552            pcn.khr_id contract_id,
12553            pcn.kle_id asset_id,
12554            pcn.sty_id stream_type_id,
12555            sty.stream_type_subclass stream_type_subclass,
12556            sty.stream_type_purpose stream_type_purpose
12557     from   okl_pools pol,
12558            okl_pool_contents pcn,
12559            okl_strm_type_v sty,
12560            okl_streams stm
12561      where pcn.sty_id = sty.id
12562        and pcn.pol_id = pol.id
12563        and stm.kle_id = pcn.kle_id
12564        and stm.khr_id = pcn.khr_id
12565        and stm.sty_id = pcn.sty_id
12566        and stm.say_code = 'CURR'
12567        and stm.active_yn = 'Y'
12568        and pol.status_code = poolstat
12569        and nvl(stm.khr_id, 0) = nvl(p_contract_id, -1)
12570        and pcn.status_code=G_PC_STS_PENDING
12571     Union
12572     select pol.khr_id agreement_id,
12573            pcn.khr_id contract_id,
12574            pcn.kle_id asset_id,
12575            pcn.sty_id stream_type_id,
12576            sty.stream_type_subclass stream_type_subclass,
12577            sty.stream_type_purpose stream_type_purpose
12578     from   okl_pools pol,
12579            okl_pool_contents pcn,
12580            okl_strm_type_v sty,
12581            okl_streams stm
12582      where pcn.sty_id = sty.id
12583        and pcn.pol_id = pol.id
12584        and stm.kle_id = pcn.kle_id
12585        and stm.khr_id = pcn.khr_id
12586        and stm.sty_id = pcn.sty_id
12587        and stm.say_code = 'CURR'
12588        and stm.active_yn = 'Y'
12589        and pol.status_code = poolstat
12590        and nvl(pol.khr_id, 0) = nvl(p_agreement_id, -1)
12591        and pcn.status_code=G_PC_STS_PENDING
12592        order by agreement_id, contract_id, asset_id, stream_type_id;
12593 
12594 
12595 /*soharma End Changes*/
12596 
12597       sec_strms_rec sec_strms_csr%ROWTYPE;
12598 
12599     l_sty_id NUMBER;
12600 
12601     l_selv_tbl               okl_streams_pub.selv_tbl_type;
12602     l_stmv_tbl               okl_streams_pub.stmv_tbl_type;
12603     x_stmv_tbl               okl_streams_pub.stmv_tbl_type;
12604     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
12605 
12606     l_stmv_rec               okl_streams_pub.stmv_rec_type;
12607     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
12608 
12609    i NUMBER;
12610    n NUMBER;
12611 
12612    l_sty_id NUMBER;
12613    l_sty_name VARCHAR2(256);
12614 
12615    l_pool_status VARCHAR2(256);
12616    l_mapped_sty_name        VARCHAR2(150);
12617 
12618    BEGIN
12619 
12620     print( l_api_name, 'begin' );
12621 
12622     x_return_status := OKL_API.G_RET_STS_SUCCESS;
12623 
12624     x_return_status := OKL_API.START_ACTIVITY(
12625 			p_api_name      => l_api_name,
12626 			p_pkg_name      => g_pkg_name,
12627 			p_init_msg_list => p_init_msg_list,
12628 			l_api_version   => p_api_version,
12629 			p_api_version   => p_api_version,
12630 			p_api_type      => G_API_TYPE,
12631 			x_return_status => x_return_status);
12632 
12633     -- check if activity started successfully
12634     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
12635        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12636     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
12637        raise OKL_API.G_EXCEPTION_ERROR;
12638     End If;
12639 
12640     print( l_api_name, 'save point' );
12641 
12642     If ( nvl(p_pool_status, 'XXX') = 'XXX' ) AND ( nvl(p_agreement_id, -1) <> -1  ) Then
12643         l_pool_status := 'NEW';
12644     ELsif( nvl(p_pool_status, 'XXX') = 'XXX' ) AND ( nvl(p_contract_id, -1) <> -1) Then
12645         l_pool_status := 'ACTIVE';
12646     Else
12647         l_pool_status := p_pool_status;
12648     End If;
12649 
12650     print( l_api_name, 'pool stat' || l_pool_status );
12651 /* sosharma ,14-12-2007
12652 Bug 6691554
12653 Start Changes*/
12654 
12655 --Based on the p_mode passed to the procedure two flows have been defined
12656  IF p_mode IS NULL THEN
12657     i := 0;
12658     FOR sec_strms_rec IN sec_strms_csr( l_pool_status )
12659     LOOP
12660         IF sec_strms_rec.stream_type_subclass = 'LOAN_PAYMENT' THEN
12661           get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12662                            p_mapping_type   =>  'DISBURSEMENT',
12663                            p_line_style     =>  NULL,
12664                            p_deal_type      =>  'LOAN',
12665                            p_stream_type_purpose => sec_strms_rec.stream_type_purpose,
12666                            p_khr_id         =>  sec_strms_rec.agreement_id,
12667                            x_mapped_stream  =>  l_mapped_sty_name,
12668                            x_return_status  =>  x_return_status);
12669 
12670     print( l_api_name, ' mapped sty ' || l_mapped_sty_name );
12671 
12672         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12673             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12674         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12675             RAISE OKL_API.G_EXCEPTION_ERROR;
12676         END IF;
12677 
12678       If l_mapped_sty_name IS NOT NULL Then
12679 
12680         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12681                                          sec_strms_rec.asset_id,
12682                                          l_mapped_sty_name )
12683         LOOP
12684 
12685             i := i + 1;
12686             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12687             l_stmv_tbl(i).say_code := 'HIST';
12688             l_stmv_tbl(i).active_yn := 'N';
12689             l_stmv_tbl(i).date_history := sysdate;
12690 
12691         END LOOP;
12692 
12693         If ( l_stmv_tbl.COUNT > 0 ) Then
12694 
12695             Okl_Streams_pub.update_streams(
12696                              p_api_version => p_api_version,
12697                              p_init_msg_list => p_init_msg_list,
12698                              x_return_status => x_return_status,
12699                              x_msg_count => x_msg_count,
12700                              x_msg_data => x_msg_data,
12701                              p_stmv_tbl => l_stmv_tbl,
12702                              x_stmv_tbl => x_stmv_tbl);
12703 
12704             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12705                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12706             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12707                 RAISE OKL_API.G_EXCEPTION_ERROR;
12708             END IF;
12709 
12710         End If;
12711         l_stmv_tbl.delete;
12712 
12713       End If;
12714 
12715         ELSE
12716            get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12717                            p_mapping_type   =>  'DISBURSEMENT',
12718                            p_line_style     =>  NULL,
12719                            p_deal_type      =>  'LEASE',
12720 	                          p_khr_id         =>  sec_strms_rec.agreement_id,
12721                            x_mapped_stream  =>  l_mapped_sty_name,
12722                            x_return_status  =>  x_return_status);
12723 
12724     print( l_api_name, ' mapped sty ' || l_mapped_sty_name );
12725 
12726         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12727             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12728         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12729             RAISE OKL_API.G_EXCEPTION_ERROR;
12730         END IF;
12731 
12732       If l_mapped_sty_name IS NOT NULL Then
12733 
12734         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12735                                          sec_strms_rec.asset_id,
12736                                          l_mapped_sty_name )
12737         LOOP
12738 
12739             i := i + 1;
12740             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12741             l_stmv_tbl(i).say_code := 'HIST';
12742             l_stmv_tbl(i).active_yn := 'N';
12743             l_stmv_tbl(i).date_history := sysdate;
12744 
12745         END LOOP;
12746 
12747         If ( l_stmv_tbl.COUNT > 0 ) Then
12748 
12749             Okl_Streams_pub.update_streams(
12750                              p_api_version => p_api_version,
12751                              p_init_msg_list => p_init_msg_list,
12752                              x_return_status => x_return_status,
12753                              x_msg_count => x_msg_count,
12754                              x_msg_data => x_msg_data,
12755                              p_stmv_tbl => l_stmv_tbl,
12756                              x_stmv_tbl => x_stmv_tbl);
12757 
12758             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12759                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12760             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12761                 RAISE OKL_API.G_EXCEPTION_ERROR;
12762             END IF;
12763 
12764         End If;
12765         l_stmv_tbl.delete;
12766 
12767       End If;
12768 
12769         END IF;
12770     END LOOP;
12771 
12772     FOR sec_strms_rec IN sec_strms_csr( l_pool_status )
12773     LOOP
12774 
12775         print( l_api_name, ' mapped stream ' || l_mapped_sty_name, x_return_status );
12776 
12777         l_stmv_rec := NULL;
12778         gen_disb_streams(p_api_version   => p_api_version,
12779                          p_init_msg_list => p_init_msg_list,
12780                          p_pool_status   => l_pool_status,
12781                          p_agreement_id  => sec_strms_rec.agreement_id,
12782                          p_contract_id   => sec_strms_rec.contract_id,
12783                          p_kle_id        => sec_strms_rec.asset_id,
12784                          p_sty_id        => sec_strms_rec.stream_type_id,
12785                          p_mode          => p_mode,
12786                          x_stmv_rec      => l_stmv_rec,
12787                          x_selv_tbl      => l_selv_tbl,
12788                          x_return_status => x_return_status,
12789                          x_msg_count     => x_msg_count,
12790                          x_msg_data      => x_msg_data);
12791 
12792          IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12793              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12794          ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12795              RAISE OKL_API.G_EXCEPTION_ERROR;
12796          END IF;
12797 
12798          l_stmv_rec.say_code := 'CURR';
12799 	 l_stmv_rec.date_current := sysdate;
12800 	 l_stmv_rec.active_yn := 'Y';
12801          l_stmv_rec.source_table := 'OKL_K_HEADERS';
12802          l_stmv_rec.source_id := sec_strms_rec.agreement_id;
12803 
12804          If( l_selv_tbl.COUNT > 0  AND l_stmv_rec.sty_id IS NOT NULL) Then
12805 
12806              print( l_api_name, ' creating disb streams ' );
12807 
12808             x_return_status := Okl_Streams_Util.round_streams_amount(
12809                                 p_api_version   => g_api_version,
12810                                 p_init_msg_list => p_init_msg_list,
12811                                 x_msg_count     => x_msg_count,
12812                                 x_msg_data      => x_msg_data,
12813                                 p_chr_id        => l_stmv_Rec.khr_id,
12814                                 p_selv_tbl      => l_selv_tbl,
12815                                 x_selv_tbl      => lx_selv_tbl);
12816 
12817             IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
12818                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12819             ELSIF (x_return_status = G_RET_STS_ERROR) THEN
12820                 RAISE OKL_API.G_EXCEPTION_ERROR;
12821             END IF;
12822 
12823             l_selv_tbl.DELETE;
12824             l_selv_tbl := lx_selv_tbl;
12825 
12826              okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
12827                                             p_init_msg_list   =>   G_FALSE,
12828                                             x_return_status   =>   x_return_status,
12829                                             x_msg_count       =>   x_msg_count,
12830                                             x_msg_data        =>   x_msg_data,
12831                                             p_stmv_rec        =>   l_stmv_rec,
12832                                             p_selv_tbl        =>   l_selv_tbl,
12833                                             x_stmv_rec        =>   lx_stmv_rec,
12834                                             x_selv_tbl        =>   lx_selv_tbl);
12835 
12836                IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12837                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12838                ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12839                  RAISE OKL_API.G_EXCEPTION_ERROR;
12840                END IF;
12841 
12842                 l_selv_tbl.delete;
12843 
12844 	  End if;
12845 
12846     END LOOP;
12847  ELSE
12848     i := 0;
12849     FOR sec_strms_rec IN sec_strms_pend_csr( l_pool_status )
12850     LOOP
12851         IF sec_strms_rec.stream_type_subclass = 'LOAN_PAYMENT' THEN
12852           get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12853                            p_mapping_type   =>  'DISBURSEMENT',
12854                            p_line_style     =>  NULL,
12855                            p_deal_type      =>  'LOAN',
12856                            p_stream_type_purpose => sec_strms_rec.stream_type_purpose,
12857                            p_khr_id         =>  sec_strms_rec.agreement_id,
12858                            x_mapped_stream  =>  l_mapped_sty_name,
12859                            x_return_status  =>  x_return_status);
12860 
12861     print( l_api_name, ' mapped sty ' || l_mapped_sty_name );
12862 
12863         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12864             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12865         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12866             RAISE OKL_API.G_EXCEPTION_ERROR;
12867         END IF;
12868 
12869       If l_mapped_sty_name IS NOT NULL Then
12870 
12871         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12872                                          sec_strms_rec.asset_id,
12873                                          l_mapped_sty_name )
12874         LOOP
12875 
12876             i := i + 1;
12877             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12878             l_stmv_tbl(i).say_code := 'HIST';
12879             l_stmv_tbl(i).active_yn := 'N';
12880             l_stmv_tbl(i).date_history := sysdate;
12881 
12882         END LOOP;
12883 
12884         If ( l_stmv_tbl.COUNT > 0 ) Then
12885 
12886             Okl_Streams_pub.update_streams(
12887                              p_api_version => p_api_version,
12888                              p_init_msg_list => p_init_msg_list,
12889                              x_return_status => x_return_status,
12890                              x_msg_count => x_msg_count,
12891                              x_msg_data => x_msg_data,
12892                              p_stmv_tbl => l_stmv_tbl,
12893                              x_stmv_tbl => x_stmv_tbl);
12894 
12895             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12896                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12897             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12898                 RAISE OKL_API.G_EXCEPTION_ERROR;
12899             END IF;
12900 
12901         End If;
12902         l_stmv_tbl.delete;
12903 
12904       End If;
12905 
12906       ELSE
12907          get_mapped_stream (p_sty_purpose       =>  sec_strms_rec.stream_type_subclass,
12908                            p_mapping_type   =>  'DISBURSEMENT',
12909                            p_line_style     =>  NULL,
12910                            p_deal_type      =>  'LEASE',
12911 			   p_khr_id         =>  sec_strms_rec.agreement_id,
12912                            x_mapped_stream  =>  l_mapped_sty_name,
12913                            x_return_status  =>  x_return_status);
12914 
12915     print( l_api_name, ' mapped sty ' || l_mapped_sty_name );
12916 
12917         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12918             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12919         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12920             RAISE OKL_API.G_EXCEPTION_ERROR;
12921         END IF;
12922 
12923       If l_mapped_sty_name IS NOT NULL Then
12924 
12925         FOR  l_strms_rec in l_strms_csr( sec_strms_rec.contract_id,
12926                                          sec_strms_rec.asset_id,
12927                                          l_mapped_sty_name )
12928         LOOP
12929 
12930             i := i + 1;
12931             l_stmv_tbl(i).id := l_strms_rec.STRM_ID;
12932             l_stmv_tbl(i).say_code := 'HIST';
12933             l_stmv_tbl(i).active_yn := 'N';
12934             l_stmv_tbl(i).date_history := sysdate;
12935 
12936         END LOOP;
12937 
12938         If ( l_stmv_tbl.COUNT > 0 ) Then
12939 
12940             Okl_Streams_pub.update_streams(
12941                              p_api_version => p_api_version,
12942                              p_init_msg_list => p_init_msg_list,
12943                              x_return_status => x_return_status,
12944                              x_msg_count => x_msg_count,
12945                              x_msg_data => x_msg_data,
12946                              p_stmv_tbl => l_stmv_tbl,
12947                              x_stmv_tbl => x_stmv_tbl);
12948 
12949             IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12950                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12951             ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12952                 RAISE OKL_API.G_EXCEPTION_ERROR;
12953             END IF;
12954 
12955         End If;
12956         l_stmv_tbl.delete;
12957 
12958       End If;
12959       END IF;
12960 
12961     END LOOP;
12962 
12963 
12964 
12965     FOR sec_strms_rec IN sec_strms_pend_csr( l_pool_status )
12966     LOOP
12967 
12968         print( l_api_name, ' mapped stream ' || l_mapped_sty_name, x_return_status );
12969 
12970         l_stmv_rec := NULL;
12971         gen_disb_streams(p_api_version   => p_api_version,
12972                          p_init_msg_list => p_init_msg_list,
12973                          p_pool_status   => l_pool_status,
12974                          p_agreement_id  => sec_strms_rec.agreement_id,
12975                          p_contract_id   => sec_strms_rec.contract_id,
12976                          p_kle_id        => sec_strms_rec.asset_id,
12977                          p_sty_id        => sec_strms_rec.stream_type_id,
12978                          p_mode          => p_mode,
12979                          x_stmv_rec      => l_stmv_rec,
12980                          x_selv_tbl      => l_selv_tbl,
12981                          x_return_status => x_return_status,
12982                          x_msg_count     => x_msg_count,
12983                          x_msg_data      => x_msg_data);
12984 
12985          IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
12986              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12987          ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
12988              RAISE OKL_API.G_EXCEPTION_ERROR;
12989          END IF;
12990 
12991          l_stmv_rec.say_code := 'CURR';
12992 	 l_stmv_rec.date_current := sysdate;
12993 	 l_stmv_rec.active_yn := 'Y';
12994          l_stmv_rec.source_table := 'OKL_K_HEADERS';
12995          l_stmv_rec.source_id := sec_strms_rec.agreement_id;
12996 
12997          If( l_selv_tbl.COUNT > 0  AND l_stmv_rec.sty_id IS NOT NULL) Then
12998 
12999              print( l_api_name, ' creating disb streams ' );
13000 
13001             x_return_status := Okl_Streams_Util.round_streams_amount(
13002                                 p_api_version   => g_api_version,
13003                                 p_init_msg_list => p_init_msg_list,
13004                                 x_msg_count     => x_msg_count,
13005                                 x_msg_data      => x_msg_data,
13006                                 p_chr_id        => l_stmv_Rec.khr_id,
13007                                 p_selv_tbl      => l_selv_tbl,
13008                                 x_selv_tbl      => lx_selv_tbl);
13009 
13010             IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
13011                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13012             ELSIF (x_return_status = G_RET_STS_ERROR) THEN
13013                 RAISE OKL_API.G_EXCEPTION_ERROR;
13014             END IF;
13015 
13016             l_selv_tbl.DELETE;
13017             l_selv_tbl := lx_selv_tbl;
13018 
13019              okl_streams_pub.create_streams(p_api_version     =>   G_API_VERSION,
13020                                             p_init_msg_list   =>   G_FALSE,
13021                                             x_return_status   =>   x_return_status,
13022                                             x_msg_count       =>   x_msg_count,
13023                                             x_msg_data        =>   x_msg_data,
13024                                             p_stmv_rec        =>   l_stmv_rec,
13025                                             p_selv_tbl        =>   l_selv_tbl,
13026                                             x_stmv_rec        =>   lx_stmv_rec,
13027                                             x_selv_tbl        =>   lx_selv_tbl);
13028 
13029                IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
13030                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13031                ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
13032                  RAISE OKL_API.G_EXCEPTION_ERROR;
13033                END IF;
13034 
13035                 l_selv_tbl.delete;
13036 
13037 	  End if;
13038 
13039     END LOOP;
13040 
13041  END IF;
13042 
13043     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
13044     print( l_api_name, 'end' );
13045 
13046     Exception
13047 
13048 	when OKL_API.G_EXCEPTION_ERROR then
13049 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13050 			p_api_name  => l_api_name,
13051 			p_pkg_name  => g_pkg_name,
13052 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
13053 			x_msg_count => x_msg_count,
13054 			x_msg_data  => x_msg_data,
13055 			p_api_type  => g_api_type);
13056 
13057 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
13058 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13059 			p_api_name  => l_api_name,
13060 			p_pkg_name  => g_pkg_name,
13061 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
13062 			x_msg_count => x_msg_count,
13063 			x_msg_data  => x_msg_data,
13064 			p_api_type  => g_api_type);
13065 
13066 	when OTHERS then
13067       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13068 			p_api_name  => l_api_name,
13069 			p_pkg_name  => g_pkg_name,
13070 			p_exc_name  => 'OTHERS',
13071 			x_msg_count => x_msg_count,
13072 			x_msg_data  => x_msg_data,
13073 			p_api_type  => g_api_type);
13074 
13075    END gen_disbursement_streams;
13076 
13077 
13078    PROCEDURE  create_disb_streams(p_api_version      IN      NUMBER,
13079                                        p_init_msg_list       IN      VARCHAR2,
13080                                        p_agreement_id        IN      NUMBER,
13081                                        p_pool_status         IN      VARCHAR2 DEFAULT 'NEW',
13082 -- sosharma bug 6691554 ,added p_mode call for content status pending
13083                                        p_mode                IN       VARCHAR2 DEFAULT NULL,
13084                                        x_return_status       OUT NOCOPY VARCHAR2,
13085                                        x_msg_count           OUT NOCOPY NUMBER,
13086                                        x_msg_data            OUT NOCOPY VARCHAR2) IS
13087 
13088 
13089     l_api_name         CONSTANT VARCHAR2(61) := 'create_disb_streams';
13090 
13091    BEGIN
13092 
13093     print( l_api_name, 'begin' );
13094 
13095     x_return_status := OKL_API.G_RET_STS_SUCCESS;
13096 
13097     x_return_status := OKL_API.START_ACTIVITY(
13098 			p_api_name      => l_api_name,
13099 			p_pkg_name      => g_pkg_name,
13100 			p_init_msg_list => p_init_msg_list,
13101 			l_api_version   => p_api_version,
13102 			p_api_version   => p_api_version,
13103 			p_api_type      => G_API_TYPE,
13104 			x_return_status => x_return_status);
13105 
13106     -- check if activity started successfully
13107     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13108        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13109     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13110        raise OKL_API.G_EXCEPTION_ERROR;
13111     End If;
13112 
13113     gen_disbursement_streams(p_api_version   => p_api_version,
13114                              p_init_msg_list => p_init_msg_list,
13115                              p_contract_id   => NULL,
13116                              p_agreement_id  => p_agreement_id,
13117                              p_pool_status   => p_pool_status,
13118 -- sosharma 14-12-2007 bug 6691554 ,added p_mode to pass mode pending for downstream processing
13119                              p_mode          => p_mode,
13120                              x_return_status => x_return_status,
13121                              x_msg_count     => x_msg_count,
13122                              x_msg_data      => x_msg_data);
13123 
13124     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13125        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13126     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13127        raise OKL_API.G_EXCEPTION_ERROR;
13128     End If;
13129 
13130     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
13131     print( l_api_name, 'end' );
13132     Exception
13133 
13134 	when OKL_API.G_EXCEPTION_ERROR then
13135 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13136 			p_api_name  => l_api_name,
13137 			p_pkg_name  => g_pkg_name,
13138 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
13139 			x_msg_count => x_msg_count,
13140 			x_msg_data  => x_msg_data,
13141 			p_api_type  => g_api_type);
13142 
13143 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
13144 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13145 			p_api_name  => l_api_name,
13146 			p_pkg_name  => g_pkg_name,
13147 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
13148 			x_msg_count => x_msg_count,
13149 			x_msg_data  => x_msg_data,
13150 			p_api_type  => g_api_type);
13151 
13152 	when OTHERS then
13153       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13154 			p_api_name  => l_api_name,
13155 			p_pkg_name  => g_pkg_name,
13156 			p_exc_name  => 'OTHERS',
13157 			x_msg_count => x_msg_count,
13158 			x_msg_data  => x_msg_data,
13159 			p_api_type  => g_api_type);
13160 
13161    END create_disb_streams;
13162 
13163 
13164    PROCEDURE  create_disb_streams(p_api_version     IN      NUMBER,
13165                                        p_init_msg_list      IN      VARCHAR2,
13166                                        p_contract_id        IN      NUMBER,
13167                                        p_pool_status        IN      VARCHAR2 DEFAULT 'ACTIVE',
13168                                        x_return_status       OUT NOCOPY VARCHAR2,
13169                                        x_msg_count           OUT NOCOPY NUMBER,
13170                                        x_msg_data            OUT NOCOPY VARCHAR2) IS
13171 
13172 
13173     l_api_name         CONSTANT VARCHAR2(61) := 'create_disb_streams';
13174 
13175    BEGIN
13176 
13177     print( l_api_name, 'begin' );
13178 
13179     x_return_status := OKL_API.G_RET_STS_SUCCESS;
13180 
13181     x_return_status := OKL_API.START_ACTIVITY(
13182 			p_api_name      => l_api_name,
13183 			p_pkg_name      => g_pkg_name,
13184 			p_init_msg_list => p_init_msg_list,
13185 			l_api_version   => p_api_version,
13186 			p_api_version   => p_api_version,
13187 			p_api_type      => G_API_TYPE,
13188 			x_return_status => x_return_status);
13189 
13190     -- check if activity started successfully
13191     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13192        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13193     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13194        raise OKL_API.G_EXCEPTION_ERROR;
13195     End If;
13196 
13197     gen_disbursement_streams(p_api_version   => p_api_version,
13198                              p_init_msg_list => p_init_msg_list,
13199                              p_contract_id   => p_contract_id,
13200                              p_agreement_id  => NULL,
13201                              p_pool_status   => p_pool_status,
13202                              x_return_status => x_return_status,
13203                              x_msg_count     => x_msg_count,
13204                              x_msg_data      => x_msg_data);
13205 
13206     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13207        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13208     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13209        raise OKL_API.G_EXCEPTION_ERROR;
13210     End If;
13211 
13212     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
13213     print( l_api_name, 'end' );
13214     Exception
13215 
13216 	when OKL_API.G_EXCEPTION_ERROR then
13217 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13218 			p_api_name  => l_api_name,
13219 			p_pkg_name  => g_pkg_name,
13220 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
13221 			x_msg_count => x_msg_count,
13222 			x_msg_data  => x_msg_data,
13223 			p_api_type  => g_api_type);
13224 
13225 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
13226 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13227 			p_api_name  => l_api_name,
13228 			p_pkg_name  => g_pkg_name,
13229 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
13230 			x_msg_count => x_msg_count,
13231 			x_msg_data  => x_msg_data,
13232 			p_api_type  => g_api_type);
13233 
13234 	when OTHERS then
13235       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13236 			p_api_name  => l_api_name,
13237 			p_pkg_name  => g_pkg_name,
13238 			p_exc_name  => 'OTHERS',
13239 			x_msg_count => x_msg_count,
13240 			x_msg_data  => x_msg_data,
13241 			p_api_type  => g_api_type);
13242 
13243    END create_disb_streams;
13244 
13245    PROCEDURE  create_pv_streams(p_api_version      IN      NUMBER,
13246                                        p_init_msg_list       IN      VARCHAR2,
13247                                        p_agreement_id        IN      NUMBER,
13248                                        p_pool_status         IN      VARCHAR2 DEFAULT 'NEW',
13249                                        p_mode                IN      VARCHAR2 DEFAULT NULL,
13250                                        x_return_status       OUT NOCOPY VARCHAR2,
13251                                        x_msg_count           OUT NOCOPY NUMBER,
13252                                        x_msg_data            OUT NOCOPY VARCHAR2) IS
13253 
13254 
13255     l_api_name         CONSTANT VARCHAR2(61) := 'create_pv_streams';
13256 
13257    BEGIN
13258 
13259     print( l_api_name, 'begin' );
13260 
13261     x_return_status := OKL_API.G_RET_STS_SUCCESS;
13262 
13263     x_return_status := OKL_API.START_ACTIVITY(
13264 			p_api_name      => l_api_name,
13265 			p_pkg_name      => g_pkg_name,
13266 			p_init_msg_list => p_init_msg_list,
13267 			l_api_version   => p_api_version,
13268 			p_api_version   => p_api_version,
13269 			p_api_type      => G_API_TYPE,
13270 			x_return_status => x_return_status);
13271 
13272     -- check if activity started successfully
13273     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13274        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13275     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13276        raise OKL_API.G_EXCEPTION_ERROR;
13277     End If;
13278 
13279     gen_pv_disb_streams(p_api_version   => p_api_version,
13280                              p_init_msg_list => p_init_msg_list,
13281                              p_contract_id   => NULL,
13282                              p_agreement_id  => p_agreement_id,
13283                              p_pool_status   => p_pool_status,
13284                              p_mode          => p_mode,
13285                              x_return_status => x_return_status,
13286                              x_msg_count     => x_msg_count,
13287                              x_msg_data      => x_msg_data);
13288 
13289     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13290        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13291     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13292        raise OKL_API.G_EXCEPTION_ERROR;
13293     End If;
13294 
13295     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
13296     print( l_api_name, 'end' );
13297     Exception
13298 
13299       when OKL_API.G_EXCEPTION_ERROR then
13300        x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13301         p_api_name  => l_api_name,
13302         p_pkg_name  => g_pkg_name,
13303         p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
13304         x_msg_count => x_msg_count,
13305         x_msg_data  => x_msg_data,
13306         p_api_type  => g_api_type);
13307 
13308        when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
13309         x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13310          p_api_name  => l_api_name,
13311          p_pkg_name  => g_pkg_name,
13312          p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
13313          x_msg_count => x_msg_count,
13314          x_msg_data  => x_msg_data,
13315          p_api_type  => g_api_type);
13316 
13317       when OTHERS then
13318             x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13319         p_api_name  => l_api_name,
13320         p_pkg_name  => g_pkg_name,
13321         p_exc_name  => 'OTHERS',
13322         x_msg_count => x_msg_count,
13323         x_msg_data  => x_msg_data,
13324         p_api_type  => g_api_type);
13325 
13326    END create_pv_streams;
13327 
13328 
13329    PROCEDURE  create_pv_streams(p_api_version     IN      NUMBER,
13330                                        p_init_msg_list      IN      VARCHAR2,
13331                                        p_contract_id        IN      NUMBER,
13332                                        p_pool_status        IN      VARCHAR2 DEFAULT 'ACTIVE',
13333                                        x_return_status       OUT NOCOPY VARCHAR2,
13334                                        x_msg_count           OUT NOCOPY NUMBER,
13335                                        x_msg_data            OUT NOCOPY VARCHAR2) IS
13336 
13337 
13338     l_api_name         CONSTANT VARCHAR2(61) := 'create_pv_streams';
13339 
13340    BEGIN
13341 
13342     print( l_api_name, 'begin' );
13343 
13344     x_return_status := OKL_API.G_RET_STS_SUCCESS;
13345 
13346     x_return_status := OKL_API.START_ACTIVITY(
13347 			p_api_name      => l_api_name,
13348 			p_pkg_name      => g_pkg_name,
13349 			p_init_msg_list => p_init_msg_list,
13350 			l_api_version   => p_api_version,
13351 			p_api_version   => p_api_version,
13352 			p_api_type      => G_API_TYPE,
13353 			x_return_status => x_return_status);
13354 
13355     -- check if activity started successfully
13356     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13357        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13358     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13359        raise OKL_API.G_EXCEPTION_ERROR;
13360     End If;
13361 
13362     gen_pv_disb_streams(p_api_version   => p_api_version,
13363                              p_init_msg_list => p_init_msg_list,
13364                              p_contract_id   => p_contract_id,
13365                              p_agreement_id  => NULL,
13366                              p_pool_status   => p_pool_status,
13367                              x_return_status => x_return_status,
13368                              x_msg_count     => x_msg_count,
13369                              x_msg_data      => x_msg_data);
13370 
13371     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13372        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13373     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13374        raise OKL_API.G_EXCEPTION_ERROR;
13375     End If;
13376 
13377     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
13378     print( l_api_name, 'end' );
13379     Exception
13380 
13381 	when OKL_API.G_EXCEPTION_ERROR then
13382 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13383 			p_api_name  => l_api_name,
13384 			p_pkg_name  => g_pkg_name,
13385 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
13386 			x_msg_count => x_msg_count,
13387 			x_msg_data  => x_msg_data,
13388 			p_api_type  => g_api_type);
13389 
13390 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
13391 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13392 			p_api_name  => l_api_name,
13393 			p_pkg_name  => g_pkg_name,
13394 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
13395 			x_msg_count => x_msg_count,
13396 			x_msg_data  => x_msg_data,
13397 			p_api_type  => g_api_type);
13398 
13399 	when OTHERS then
13400       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
13401 			p_api_name  => l_api_name,
13402 			p_pkg_name  => g_pkg_name,
13403 			p_exc_name  => 'OTHERS',
13404 			x_msg_count => x_msg_count,
13405 			x_msg_data  => x_msg_data,
13406 			p_api_type  => g_api_type);
13407 
13408    END create_pv_streams;
13409 
13410   PROCEDURE  get_line_principal_bal( p_api_version         IN      NUMBER,
13411                                     p_init_msg_list       IN      VARCHAR2,
13412                                     p_khr_id              IN      NUMBER,
13413                                     p_kle_id              IN      NUMBER,
13414                                     p_date                IN      DATE,
13415 				    x_principal_balance   OUT NOCOPY NUMBER,
13416 				    x_accumulated_int     OUT NOCOPY NUMBER,
13417                                     x_return_status       OUT NOCOPY VARCHAR2,
13418                                     x_msg_count           OUT NOCOPY NUMBER,
13419                                     x_msg_data            OUT NOCOPY VARCHAR2) IS
13420 
13421 
13422     CURSOR c_hdr IS
13423     SELECT  nvl(khr.implicit_interest_rate, 0),
13424             khr.deal_type
13425     FROM   okc_k_headers_b chr,
13426            okl_k_headers khr
13427     WHERE  khr.id = p_khr_id
13428       AND  chr.id = khr.id;
13429 
13430     r_hdr c_hdr%ROWTYPE;
13431 
13432 
13433     CURSOR c_pbal IS
13434     SELECT ele.amount,
13435            ele.stream_element_date
13436     FROM okl_streams stm,
13437          okl_strm_elements ele,
13438      	 okl_strm_type_b sty
13439     WHERE sty.stream_type_purpose = 'PRINCIPAL_BALANCE'
13440       AND stm.sty_id = sty.id
13441       AND stm.khr_id = p_khr_id
13442       AND stm.kle_id = p_kle_id
13443       AND stm.say_code = 'CURR'
13444       AND stm.active_yn = 'Y'
13445       AND ele.stm_id = stm.id
13446       AND ele.stream_element_date =
13447                     ( SELECT max( ele.stream_element_date)
13448                       FROM okl_streams stm,
13449                            okl_strm_elements ele,
13450 	                   okl_strm_type_b sty
13451                       WHERE sty.stream_type_purpose = 'PRINCIPAL_BALANCE'
13452                         AND stm.sty_id = sty.id
13453                         AND stm.khr_id = p_khr_id
13454                         AND stm.kle_id = p_kle_id
13455                         AND stm.say_code = 'CURR'
13456                     	AND stm.active_yn = 'Y'
13457                         AND ele.stm_id = stm.id
13458                         AND ele.stream_element_date <= p_date );
13459 
13460     r_pbal c_pbal%ROWTYPE;
13461 
13462     CURSOR c_inflows ( styId NUMBER) IS
13463     SELECT DISTINCT
13464            sll.object1_id1 frequency,
13465 	   nvl(sll.rule_information10, 'N') advance_arrears
13466     FROM   okc_rules_b sll,
13467            okc_rules_b slh,
13468            okc_rule_groups_b rgp
13469     WHERE  rgp.rgd_code = 'LALEVL'
13470       AND  rgp.id = slh.rgp_id
13471       AND  slh.rule_information_category = 'LASLH'
13472       AND  slh.object1_id1 = styId
13473       AND  slh.id = sll.object2_id1
13474       AND  sll.rule_information_category = 'LASLL'
13475       AND  sll.dnz_chr_id = p_khr_id
13476       AND  rgp.cle_id = p_kle_id;
13477 
13478     r_inflows c_inflows%ROWTYPE;
13479 
13480     l_iir NUMBER;
13481     l_days NUMBER;
13482     l_principal_balance NUMBER;
13483     l_principal_bal_date DATE;
13484     l_accumulated_int NUMBER;
13485 
13486 
13487     Cursor fee_type_csr IS
13488     Select 'Y' What
13489     from dual where Exists(
13490     SELECT nvl(kle.fee_type, 'XYZ'),
13491            nvl(lse.lty_code, 'XYZ')
13492     FROM   okc_k_lines_b cle,
13493            okl_k_lines kle,
13494            okc_line_styles_b lse
13495     WHERE  cle.dnz_chr_id = p_khr_id
13496         AND  cle.sts_code IN ('PASSED', 'COMPLETE', 'TERMINATED')
13497         AND  cle.lse_id = lse.id
13498         AND  (kle.fee_type = 'FINANCED' OR kle.fee_type = 'ROLLOVER' OR lse.lty_code = 'LINK_FEE_ASSET')
13499         AND  cle.id = kle.id
13500 	AND  cle.id = p_kle_id);
13501 
13502 
13503     fee_type_rec fee_type_csr%ROWTYPe;
13504 
13505 
13506     l_stream_name VARCHAR2(256);
13507     l_sty_id NUMBER;
13508 
13509     cursor fee_strm_type_csr is
13510     SELECT sty.id
13511       FROM   okc_rules_b sll,
13512              okc_rules_b slh,
13513              okc_rule_groups_b rgp,
13514              okl_strm_type_b sty
13515       WHERE  rgp.dnz_chr_id = p_khr_id
13516         AND  rgp.cle_id = p_kle_id
13517         AND  rgp.rgd_code= 'LALEVL'
13518         AND  rgp.id = slh.rgp_id
13519         AND  slh.rule_information_category = 'LASLH'
13520         AND  slh.object1_id1 = TO_CHAR(sty.id)
13521         AND  TO_CHAR(slh.id) = sll.object2_id1
13522         AND  sll.rule_information_category = 'LASLL'
13523       ORDER BY fnd_date.canonical_to_date(sll.rule_information2);
13524 
13525     l_deal_type VARCHAR2(30);
13526 
13527     l_api_name         CONSTANT VARCHAR2(61) := 'GET_SCHED_PRINCIPAL_BAL';
13528 
13529     l_day_convention_month VARCHAR2(30);
13530     l_day_convention_year VARCHAR2(30);
13531 
13532    Begin
13533 
13534        print( l_api_name, 'end' );
13535 
13536        OPEN c_pbal;
13537        FETCH c_pbal INTO r_pbal;
13538        CLOSE c_pbal;
13539 
13540        l_principal_balance := nvl( r_pbal.amount, 0 );
13541        l_principal_bal_date := nvl( r_pbal.stream_element_date, p_date );
13542 
13543        print( l_api_name, ' prince date ' || to_char( l_principal_bal_date) );
13544        OPEN c_hdr;
13545        FETCH c_hdr INTO l_iir, l_deal_type;
13546        CLOSE c_hdr;
13547        l_iir := l_iir/100.0;
13548 
13549        OPEN fee_type_csr;
13550        FETCH fee_type_csr INTO fee_type_rec;
13551        CLOSE fee_type_csr;
13552 
13553        If nvl(fee_type_rec.What, 'N') = 'Y' Then
13554 
13555            OPEN fee_strm_type_csr;
13556            FETCH fee_strm_type_csr INTO l_sty_id;
13557    	   CLOSE fee_strm_type_csr;
13558 
13559        Else
13560            OKL_ISG_UTILS_PVT.get_primary_stream_type(
13561                                                 p_khr_id              => p_khr_id,
13562 						p_deal_type           => l_deal_type,
13563                                                 p_primary_sty_purpose => 'RENT',
13564                                                 x_return_status       => x_return_status,
13565                                                 x_primary_sty_id      => l_sty_id,
13566                                                 x_primary_sty_name    => l_stream_name);
13567 
13568            IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
13569                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13570            ELSIF (x_return_status = G_RET_STS_ERROR) THEN
13571                RAISE OKL_API.G_EXCEPTION_ERROR;
13572            END IF;
13573         --l_stream_name := 'RENT';
13574        End If;
13575 
13576        OPEN c_inflows ( l_sty_id );
13577        FETCH c_inflows INTO r_inflows;
13578        CLOSE c_inflows;
13579 
13580        print( l_api_name, ' bal datae ' || to_char( l_principal_bal_date ), x_return_status );
13581        print( l_api_name, ' p_days ' || to_char( p_date ), x_return_status );
13582 
13583        -- Fetch the day convention ..
13584        OKL_PRICING_UTILS_PVT.get_day_convention(
13585          p_id              => p_khr_id,
13586          p_source          => 'ISG',
13587          x_days_in_month   => l_day_convention_month,
13588          x_days_in_year    => l_day_convention_year,
13589          x_return_status   => x_return_status);
13590        print( 'get_line_principal_bal', 'Month / Year = ' || l_day_convention_month || '/' || l_day_convention_year );
13591        IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
13592          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13593        ELSIF (x_return_status = G_RET_STS_ERROR) THEN
13594          RAISE OKL_API.G_EXCEPTION_ERROR;
13595        END IF;
13596        l_days := OKL_PRICING_UTILS_PVT.get_day_count (p_start_date => trunc(l_principal_bal_date),
13597                                 p_days_in_month => l_day_convention_month,
13598                                 p_days_in_year => l_day_convention_year,
13599                                 p_end_date   => trunc(p_date),
13600                                 p_arrears    => r_inflows.advance_arrears,
13601                                 x_return_status => x_return_status);
13602 
13603        print( l_api_name, ' n days ' || to_char( l_days), x_return_status );
13604 
13605        IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
13606            RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13607        ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
13608            RAISE OKL_API.G_EXCEPTION_ERROR;
13609        END IF;
13610 
13611        l_accumulated_int := l_principal_balance * l_days * l_iir/360;
13612 
13613        x_principal_balance := l_principal_balance;
13614        x_accumulated_int   := l_accumulated_int;
13615 
13616        print( l_api_name, ' line ' || to_char(p_kle_id) );
13617        print( l_api_name, '     iir ' || to_char(l_iir) );
13618        print( l_api_name, '     prince ' || to_char(l_principal_balance) );
13619        print( l_api_name, '     days ' || to_char(l_days) );
13620        print( l_api_name, '     accu ' || to_char(l_accumulated_int) );
13621 
13622        print( l_api_name, 'end');
13623   EXCEPTION
13624 
13625     WHEN OKL_API.G_EXCEPTION_ERROR THEN
13626 
13627       x_return_status := G_RET_STS_ERROR;
13628 
13629     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
13630 
13631       x_return_status := G_RET_STS_UNEXP_ERROR;
13632 
13633     WHEN OTHERS THEN
13634 
13635       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
13636                            p_msg_name     => G_DB_ERROR,
13637                            p_token1       => G_PROG_NAME_TOKEN,
13638                            p_token1_value => l_api_name,
13639                            p_token2       => G_SQLCODE_TOKEN,
13640                            p_token2_value => sqlcode,
13641                            p_token3       => G_SQLERRM_TOKEN,
13642                            p_token3_value => sqlerrm);
13643 
13644       x_return_status := G_RET_STS_UNEXP_ERROR;
13645 
13646    End get_line_principal_bal;
13647 
13648   PROCEDURE  get_sched_principal_bal( p_api_version         IN      NUMBER,
13649                                     p_init_msg_list       IN      VARCHAR2,
13650                                     p_khr_id              IN      NUMBER,
13651                                     p_kle_id              IN      NUMBER DEFAULT NULL,
13652                                     p_date                IN      DATE,
13653 				    x_principal_balance   OUT NOCOPY NUMBER,
13654 				    x_accumulated_int     OUT NOCOPY NUMBER,
13655                                     x_return_status       OUT NOCOPY VARCHAR2,
13656                                     x_msg_count           OUT NOCOPY NUMBER,
13657                                     x_msg_data            OUT NOCOPY VARCHAR2) IS
13658 
13659     cursor l_line_csr IS
13660     select kle.id
13661      from  okl_k_lines_full_v kle,
13662            okc_line_styles_b lse,
13663 	   okc_statuses_b sts
13664      where KLE.LSE_ID = LSE.ID
13665           and lse.lty_code in  ('FREE_FORM1', 'FEE')
13666 	  AND nvl(kle.fee_type,'-99') in ( '-99', 'FINANCED', 'ROLLOVER')
13667           and kle.dnz_chr_id = p_khr_id
13668 	  and sts.code = kle.sts_code
13669 	  and sts.ste_code not in ('HOLD', 'TERMINATED', 'EXPIRED', 'CANCELLED');
13670 
13671      l_line_rec l_line_csr%ROWTYPE;
13672 
13673      l_principal_balance NUMBER;
13674      l_principal_bal_date DATE;
13675      l_accumulated_int NUMBER;
13676 
13677     l_api_name         CONSTANT VARCHAR2(61) := 'GET_SCHED_PRINCIPAL_BAL';
13678 
13679    Begin
13680 
13681        print( l_api_name, 'begin');
13682        If (p_kle_id IS NOT NULL ) Then
13683 
13684            get_line_principal_bal( p_api_version         => p_api_version,
13685                                     p_init_msg_list       => p_init_msg_list,
13686                                     p_khr_id              => p_khr_id,
13687                                     p_kle_id              => p_kle_id,
13688                                     p_date                => p_date,
13689 				    x_principal_balance   => x_principal_balance,
13690 				    x_accumulated_int     => x_accumulated_int,
13691                                     x_return_status       => x_return_status,
13692                                     x_msg_count           => x_msg_count,
13693                                     x_msg_data            => x_msg_data);
13694 
13695            IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
13696                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13697            ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
13698                RAISE OKL_API.G_EXCEPTION_ERROR;
13699            END IF;
13700 
13701            return;
13702 
13703        End If;
13704 
13705        l_principal_balance := 0;
13706        l_accumulated_int := 0;
13707 
13708        FOR l_line_rec IN l_line_csr
13709        LOOP
13710 
13711            get_line_principal_bal( p_api_version         => p_api_version,
13712                                     p_init_msg_list       => p_init_msg_list,
13713                                     p_khr_id              => p_khr_id,
13714                                     p_kle_id              => l_line_rec.id,
13715                                     p_date                => p_date,
13716 				    x_principal_balance   => x_principal_balance,
13717 				    x_accumulated_int     => x_accumulated_int,
13718                                     x_return_status       => x_return_status,
13719                                     x_msg_count           => x_msg_count,
13720                                     x_msg_data            => x_msg_data);
13721 
13722            IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
13723                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13724            ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
13725                RAISE OKL_API.G_EXCEPTION_ERROR;
13726            END IF;
13727 
13728            l_principal_balance := l_principal_balance + x_principal_balance;
13729            l_accumulated_int := l_accumulated_int + x_accumulated_int;
13730 
13731        END LOOP;
13732 
13733        x_principal_balance := l_principal_balance;
13734        x_accumulated_int := l_accumulated_int;
13735 
13736        print( l_api_name, 'end');
13737 
13738  EXCEPTION
13739 
13740     WHEN OKL_API.G_EXCEPTION_ERROR THEN
13741 
13742       x_return_status := G_RET_STS_ERROR;
13743 
13744     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
13745 
13746       x_return_status := G_RET_STS_UNEXP_ERROR;
13747 
13748     WHEN OTHERS THEN
13749 
13750       OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
13751                            p_msg_name     => G_DB_ERROR,
13752                            p_token1       => G_PROG_NAME_TOKEN,
13753                            p_token1_value => l_api_name,
13754                            p_token2       => G_SQLCODE_TOKEN,
13755                            p_token2_value => sqlcode,
13756                            p_token3       => G_SQLERRM_TOKEN,
13757                            p_token3_value => sqlerrm);
13758 
13759       x_return_status := G_RET_STS_UNEXP_ERROR;
13760 
13761    End get_sched_principal_bal;
13762 
13763   PROCEDURE get_next_billing_date( p_api_version            IN NUMBER,
13764                                    p_init_msg_list          IN VARCHAR2,
13765                                    p_khr_id                 IN NUMBER,
13766                                    p_billing_date           IN DATE DEFAULT NULL,
13767                                    x_next_due_date          OUT NOCOPY DATE,
13768                                    x_next_period_start_date OUT NOCOPY DATE,
13769                                    x_next_period_end_date   OUT NOCOPY  DATE,
13770                                    x_return_status          OUT NOCOPY VARCHAR2,
13771                                    x_msg_count              OUT NOCOPY NUMBER,
13772                                    x_msg_data               OUT NOCOPY VARCHAR2) IS
13773 
13774 
13775   --Added order by rul2.rule_information2 by djanaswa for bug 6007644
13776       CURSOR l_varint_sll_csr( khrid NUMBER ) IS
13777       SELECT TO_NUMBER(NULL) cle_id,
13778              TO_NUMBER(rul1.object1_id1) sty_id,
13779              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
13780              TO_NUMBER(rul2.rule_information3) periods,
13781              rul2.object1_id1 frequency,
13782              rul2.rule_information5 structure,
13783              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
13784              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
13785              TO_NUMBER(rul2.rule_information7) stub_days,
13786              TO_NUMBER(rul2.rule_information8) stub_amount,
13787              NULL lty_code,
13788              TO_NUMBER(NULL) capital_amount,
13789              TO_NUMBER(NULL) residual_value
13790       FROM   okc_rule_groups_b rgp,
13791              okc_rules_b rul1,
13792              okc_rules_b rul2,
13793 	     okl_strm_type_b sty
13794       WHERE  rul2.dnz_chr_id = khrid
13795         AND  rul2.rule_information_category = 'LASLL'
13796         AND  rul2.rgp_id = rgp.id
13797         AND  rgp.cle_id IS NULL
13798         AND  TO_NUMBER(rul2.object2_id1) = rul1.id
13799 	AND  TO_NUMBER(rul1.object1_id1) = sty.id
13800 	AND  sty.stream_type_purpose = 'VARIABLE_INTEREST_SCHEDULE'
13801        ORDER BY FND_DATE.canonical_to_date(rul2.rule_information2);
13802 
13803       l_varint_sll_rec l_varint_sll_csr%ROWTYPE;
13804 
13805         l_advance_or_arrears VARCHAR2(10) := 'ARREARS';
13806 
13807    Cursor l_strms_csr ( chrId NUMBER, styId NUMBER ) IS
13808    Select str.id  strm_id
13809    From okl_streams str
13810    Where str.sty_id = styId
13811        and str.khr_id = chrId
13812        and str.say_code = 'WORK';
13813 
13814     l_strms_rec l_strms_csr%ROWTYPE;
13815 
13816     CURSOR c_hdr IS
13817       SELECT chr.template_yn,
13818              chr.currency_code,
13819              chr.start_date,
13820              chr.end_date,
13821              khr.deal_type,
13822              khr.term_duration,
13823              NVL(khr.generate_accrual_yn, 'Y')
13824       FROM   okc_k_headers_b chr,
13825              okl_k_headers khr
13826       WHERE  khr.id = p_khr_id
13827         AND  chr.id = khr.id;
13828 
13829     l_hdr                    c_hdr%ROWTYPE;
13830     l_deal_type              VARCHAR2(30);
13831     l_purpose_code           VARCHAR2(30);
13832 
13833     l_sty_id                 NUMBER;
13834     l_sty_name               VARCHAR2(150);
13835     l_mapped_sty_name        VARCHAR2(150);
13836 
13837     l_selv_tbl               okl_streams_pub.selv_tbl_type;
13838     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
13839     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
13840 
13841     l_stmv_rec               okl_streams_pub.stmv_rec_type;
13842     l_stmv_tbl               okl_streams_pub.stmv_tbl_type;
13843     lx_stmv_tbl               okl_streams_pub.stmv_tbl_type;
13844     x_stmv_tbl               okl_streams_pub.stmv_tbl_type;
13845     l_pt_rec                 okl_streams_pub.stmv_rec_type;
13846     lx_stmv_rec              okl_streams_pub.stmv_rec_type;
13847 
13848     i                        BINARY_INTEGER := 0;
13849 
13850     l_api_name         CONSTANT VARCHAR2(61) := 'GET_NEXT_BILL_DATE';
13851 
13852    --Added by djanaswa for bug 6007644
13853     l_recurrence_date    DATE := NULL;
13854     --end djanaswa
13855 
13856    BEGIN
13857 
13858     print( l_api_name, 'begin' );
13859 
13860     x_return_status := OKL_API.G_RET_STS_SUCCESS;
13861 
13862     x_return_status := OKL_API.START_ACTIVITY(
13863 			p_api_name      => l_api_name,
13864 			p_pkg_name      => g_pkg_name,
13865 			p_init_msg_list => p_init_msg_list,
13866 			l_api_version   => p_api_version,
13867 			p_api_version   => p_api_version,
13868 			p_api_type      => G_API_TYPE,
13869 			x_return_status => x_return_status);
13870 
13871     -- check if activity started successfully
13872     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
13873        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13874     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
13875        raise OKL_API.G_EXCEPTION_ERROR;
13876     End If;
13877 
13878 /*
13879     get_sty_details (p_sty_name      => 'VARIABLE INTEREST SCHEDULE',
13880                      x_sty_id        => l_sty_id,
13881                      x_sty_name      => l_sty_name,
13882 		     x_return_status => x_return_status);
13883 
13884     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
13885         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13886     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
13887         RAISE OKL_API.G_EXCEPTION_ERROR;
13888     END IF;
13889 
13890 */
13891      OPEN  c_hdr;
13892      FETCH c_hdr INTO l_hdr;
13893      CLOSE c_hdr;
13894 
13895      x_next_due_date := NULL;
13896      x_next_period_end_date := NULL;
13897 
13898      FOR l_varint_sll_rec in l_varint_sll_csr( p_khr_id )
13899      LOOP
13900 
13901          IF ( lx_selv_tbl.COUNT > 0 ) Then
13902              lx_selv_tbl.delete;
13903 	 END IF;
13904 
13905        --Added by djanaswa for bug 6007644
13906          IF((l_varint_sll_rec.periods IS NULL) AND (l_varint_sll_rec.stub_days IS NOT NULL)) THEN
13907            --Set the recurrence date to null for stub payment
13908            l_recurrence_date := NULL;
13909          ELSIF(l_recurrence_date IS NULL) THEN
13910            --Set the recurrence date as periodic payment level start date
13911            l_recurrence_date := l_varint_sll_rec.start_date;
13912          END IF;
13913          --end djanaswa
13914 
13915          --Added parameter p_recurrence_date by djanaswa for bug 6007644
13916          get_stream_elements(
13917 	                   p_start_date          =>   l_varint_sll_rec.start_date,
13918                            p_periods             =>   l_varint_sll_rec.periods,
13919 			   p_frequency           =>   l_varint_sll_rec.frequency,
13920 			   p_structure           =>   l_varint_sll_rec.structure,
13921 			   p_advance_or_arrears  =>   l_varint_sll_rec.advance_arrears,
13922 			   p_amount              =>   l_varint_sll_rec.amount,
13923 			   p_stub_days           =>   l_varint_sll_rec.stub_days,
13924 			   p_stub_amount         =>   l_varint_sll_rec.stub_amount,
13925 			   p_currency_code       =>   l_hdr.currency_code,
13926 			   p_khr_id              =>   p_khr_id,
13927 			   p_kle_id              =>   NULL,
13928 			   p_purpose_code        =>   NULL,
13929 			   x_selv_tbl            =>   lx_selv_tbl,
13930 			   x_pt_tbl              =>   l_pt_tbl,
13931 			   x_return_status       =>   x_return_status,
13932 			   x_msg_count           =>   x_msg_count,
13933 			   x_msg_data            =>   x_msg_data,
13934                            p_recurrence_date     =>   l_recurrence_date);
13935 
13936          IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
13937              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13938          ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
13939              RAISE OKL_API.G_EXCEPTION_ERROR;
13940          END IF;
13941 
13942          FOR i in 1..lx_selv_tbl.COUNT
13943 	 LOOP
13944 
13945 	     If  trunc(lx_selv_tbl(i).stream_element_date) > trunc(nvl(p_billing_date, (l_hdr.start_date-1)))Then
13946 
13947 	       If ( x_next_due_date IS NULL ) Then
13948 
13949 		 x_next_due_date := lx_selv_tbl(i).stream_element_date;
13950 		 if ( l_varint_sll_Rec.advance_arrears = 'ADVANCE' ) Then
13951 		     x_next_period_start_date := lx_selv_tbl(i).stream_element_date;
13952 		     If ( i < lx_selv_tbl.COUNT ) Then
13953                          x_next_period_end_date := lx_selv_tbl(i+1).stream_element_date - 1;
13954                          --Bug# 13815781
13955                          /*
13956                          IF TO_CHAR(x_next_period_end_date, 'DD') = '31' OR
13957                             (TO_CHAR(x_next_period_end_date, 'MON') = 'FEB' AND
13958 			     TO_CHAR(x_next_period_end_date, 'DD') = '29') THEN
13959                              x_next_period_end_date := x_next_period_end_date  - 1;
13960                          END IF;
13961                          */
13962                          return;
13963 		     End If;
13964 		 Else
13965                      x_next_period_end_date := lx_selv_tbl(i).stream_element_date;
13966 		     If ( i > 1 ) Then
13967                          x_next_period_start_date := lx_selv_tbl(i-1).stream_element_date + 1;
13968                      --Added ELSIF and ELSE by bkatraga for bug 13478101
13969                      ELSIF(p_billing_date IS NULL OR p_billing_date = l_hdr.start_date) THEN
13970                          x_next_period_start_date := l_hdr.start_date;
13971                      ELSE
13972                          x_next_period_start_date := p_billing_date + 1;
13973 		     End If;
13974                      --Bug# 13815781
13975                      /*
13976                      IF TO_CHAR(x_next_period_start_date, 'DD') = '31' OR
13977                         (TO_CHAR(x_next_period_start_date, 'MON') = 'FEB' AND
13978 		         TO_CHAR(x_next_period_start_date, 'DD') = '29') THEN
13979                          x_next_period_start_date := x_next_period_start_date  + 1;
13980                      END IF;
13981                      */
13982                      return;
13983 		 End If;
13984 
13985 	      Else
13986                  x_next_period_end_date := lx_selv_tbl(i).stream_element_date - 1;
13987                  --Bug# 13815781
13988                  /*
13989                  IF TO_CHAR(x_next_period_end_date, 'DD') = '31' OR
13990                     (TO_CHAR(x_next_period_end_date, 'MON') = 'FEB' AND
13991 		     TO_CHAR(x_next_period_end_date, 'DD') = '29') THEN
13992                      x_next_period_end_date := x_next_period_end_date  - 1;
13993                  END IF;
13994                  */
13995                  return;
13996 	      ENd If;
13997 
13998 	     End If;
13999 
14000 	 END LOOP;
14001 
14002        END LOOP;
14003 
14004        if(x_next_period_end_date IS NULL) Then
14005            x_next_period_end_date := l_hdr.end_date;
14006        End If;
14007 
14008     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
14009     print( l_api_name, 'end' );
14010 
14011     Exception
14012 
14013 	when OKL_API.G_EXCEPTION_ERROR then
14014 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
14015 			p_api_name  => l_api_name,
14016 			p_pkg_name  => g_pkg_name,
14017 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
14018 			x_msg_count => x_msg_count,
14019 			x_msg_data  => x_msg_data,
14020 			p_api_type  => g_api_type);
14021 
14022 	when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
14023 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
14024 			p_api_name  => l_api_name,
14025 			p_pkg_name  => g_pkg_name,
14026 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
14027 			x_msg_count => x_msg_count,
14028 			x_msg_data  => x_msg_data,
14029 			p_api_type  => g_api_type);
14030 
14031 	when OTHERS then
14032       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
14033 			p_api_name  => l_api_name,
14034 			p_pkg_name  => g_pkg_name,
14035 			p_exc_name  => 'OTHERS',
14036 			x_msg_count => x_msg_count,
14037 			x_msg_data  => x_msg_data,
14038 			p_api_type  => g_api_type);
14039 
14040       x_return_status := G_RET_STS_UNEXP_ERROR;
14041 
14042   end get_next_billing_date;
14043 
14044 
14045 /** gboomina created GEN_CASH_FLOWS_FOR_IA API for Bug 6763287
14046 ------------------------------------------------------------------
14047       PROCEDURE GEN_CASH_FLOWS_FOR_IA
14048 ------------------------------------------------------------------
14049 Description: This procedure is used to generate streams for Fees defined in
14050              Investor Agreement
14051 **/
14052 
14053 PROCEDURE gen_cash_flows_for_IA( p_api_version       IN NUMBER
14054                                , p_init_msg_list     IN VARCHAR2
14055                                , p_khr_id            IN NUMBER
14056                                , x_return_status     OUT NOCOPY VARCHAR2
14057                                , x_msg_count         OUT NOCOPY NUMBER
14058                                , x_msg_data          OUT NOCOPY VARCHAR2
14059                                )
14060   IS
14061     CURSOR c_inflows IS
14062       SELECT rgp.cle_id cle_id,
14063              TO_NUMBER(rul1.object1_id1) sty_id,
14064              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
14065              TO_NUMBER(rul2.rule_information3) periods,
14066              rul2.object1_id1 frequency,
14067              rul2.rule_information5 structure,
14068              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
14069              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
14070              TO_NUMBER(rul2.rule_information7) stub_days,
14071              TO_NUMBER(rul2.rule_information8) stub_amount,
14072              lse.lty_code lty_code,
14073              kle.capital_amount capital_amount,
14074              kle.residual_value residual_value,
14075              kle.fee_type fee_type
14076       FROM   okc_rule_groups_b rgp,
14077              okc_rules_b rul1,
14078              okc_rules_b rul2,
14079              okc_k_lines_b cle,
14080              okl_k_lines kle,
14081              okc_line_styles_b lse
14082       WHERE
14083              rul2.dnz_chr_id = p_khr_id
14084         AND  rul2.rule_information_category = 'LASLL'
14085         AND  rul1.rule_information_category = 'LASLH'
14086         AND  rul1.jtot_object1_code = 'OKL_STRMTYP'
14087         AND  rul2.rgp_id = rgp.id
14088         AND  TO_NUMBER(rul2.object2_id1) = rul1.id
14089         AND  rgp.cle_id = cle.id
14090         AND  cle.id = kle.id
14091         AND  cle.lse_id = lse.id
14092         ORDER BY cle_id, sty_id, start_date;
14093 
14094     l_inflow c_inflows%rowtype;
14095 
14096     CURSOR c_hdr IS
14097       SELECT chr.currency_code
14098       FROM   okc_k_headers_b chr
14099       WHERE  chr.id = p_khr_id;
14100 
14101     l_curreny_code okc_k_headers_all_b.currency_code%type;
14102 
14103     l_recurrence_date    DATE := NULL;
14104     l_old_cle_id         NUMBER;
14105     l_old_sty_id         NUMBER;
14106     l_purpose_code           VARCHAR2(30);
14107 
14108     l_pt_tbl                 okl_streams_pub.selv_tbl_type;
14109     l_pt_pro_fee_tbl         okl_streams_pub.selv_tbl_type;
14110 
14111     l_selv_tbl               okl_streams_pub.selv_tbl_type;
14112     lx_selv_tbl              okl_streams_pub.selv_tbl_type;
14113 
14114     l_stmv_rec               okl_streams_pub.stmv_rec_type;
14115     l_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
14116     lx_stmv_tbl              Okl_Streams_Pub.stmv_tbl_type;
14117 
14118     l_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
14119     lx_full_selv_tbl         Okl_Streams_Pub.selv_tbl_type;
14120 
14121     l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'gen_cash_flows_for_IA';
14122 
14123   BEGIN
14124 
14125     -- Generate streams for the payments
14126     FOR l_inflow IN c_inflows LOOP
14127       IF l_inflow.start_date IS NULL THEN
14128         OKL_API.SET_MESSAGE ( p_app_name     => G_APP_NAME
14129                             , p_msg_name     => 'OKL_NO_SLL_SDATE');
14130         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14131       END IF;
14132 
14133       IF((l_inflow.periods IS NULL) AND (l_inflow.stub_days IS NOT NULL)) THEN
14134         --Set the recurrence date to null for stub payment
14135         l_recurrence_date := NULL;
14136       ELSIF(l_recurrence_date IS NULL
14137          OR l_old_cle_id <> l_inflow.cle_id
14138          OR l_old_sty_id <> l_inflow.sty_id) THEN
14139         --Set the recurrence date as periodic payment level start date
14140         l_recurrence_date := l_inflow.start_date;
14141       END IF;
14142       l_old_cle_id := l_inflow.cle_id;
14143       l_old_sty_id := l_inflow.sty_id;
14144 
14145       OPEN c_hdr;
14146       FETCH c_hdr INTO l_curreny_code;
14147       CLOSE c_hdr;
14148 
14149       get_stream_elements( p_start_date          =>   l_inflow.start_date,
14150                            p_periods             =>   l_inflow.periods,
14151                            p_frequency           =>   l_inflow.frequency,
14152                            p_structure           =>   l_inflow.structure,
14153                            p_advance_or_arrears  =>   l_inflow.advance_arrears,
14154                            p_amount              =>   l_inflow.amount,
14155                            p_stub_days           =>   l_inflow.stub_days,
14156                            p_stub_amount         =>   l_inflow.stub_amount,
14157                            p_currency_code       =>   l_curreny_code,
14158                            p_khr_id              =>   p_khr_id,
14159                            p_kle_id              =>   l_inflow.cle_id,
14160                            p_purpose_code        =>   l_purpose_code,
14161                            x_selv_tbl            =>   l_selv_tbl,
14162                            x_pt_tbl              =>   l_pt_tbl,
14163                            x_pt_pro_fee_tbl      =>   l_pt_pro_fee_tbl,
14164                            x_return_status       =>   x_return_status,
14165                            x_msg_count           =>   x_msg_count,
14166                            x_msg_data            =>   x_msg_data,
14167                            p_recurrence_date     =>   l_recurrence_date);
14168 
14169       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14170         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14171       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14172         RAISE OKL_API.G_EXCEPTION_ERROR;
14173       END IF;
14174 
14175       get_stream_header(p_khr_id         =>   p_khr_id,
14176                         p_kle_id         =>   l_inflow.cle_id,
14177                         p_sty_id         =>   l_inflow.sty_id,
14178                         p_purpose_code   =>   l_purpose_code,
14179                         x_stmv_rec       =>   l_stmv_rec,
14180                         x_return_status  =>   x_return_status);
14181 
14182       IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14183         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14184       ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14185         RAISE OKL_API.G_EXCEPTION_ERROR;
14186       END IF;
14187 
14188       x_return_status := Okl_Streams_Util.round_streams_amount_esg(
14189                           p_api_version   => g_api_version,
14190                           p_init_msg_list => p_init_msg_list,
14191                           x_msg_count     => x_msg_count,
14192                           x_msg_data      => x_msg_data,
14193                           p_chr_id        => p_khr_id,
14194                           p_selv_tbl      => l_selv_tbl,
14195                           x_selv_tbl      => lx_selv_tbl,
14196                           p_org_id        => G_ORG_ID,
14197                           p_precision     => G_PRECISION,
14198                           p_currency_code => G_CURRENCY_CODE,
14199                           p_rounding_rule => G_ROUNDING_RULE,
14200                           p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
14201 
14202       IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
14203           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14204       ELSIF (x_return_status = G_RET_STS_ERROR) THEN
14205           RAISE OKL_API.G_EXCEPTION_ERROR;
14206       END IF;
14207 
14208       l_selv_tbl.DELETE;
14209       l_selv_tbl := lx_selv_tbl;
14210 
14211       --Accumulate Stream Header
14212       OKL_STREAMS_UTIL.accumulate_strm_headers( p_stmv_rec       => l_stmv_rec,
14213                                                 x_full_stmv_tbl  => l_stmv_tbl,
14214                                                 x_return_status  => x_return_status );
14215       IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
14216           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14217       ELSIF (x_return_status = G_RET_STS_ERROR) THEN
14218           RAISE OKL_API.G_EXCEPTION_ERROR;
14219       END IF;
14220 
14221       --Accumulate Stream Elements
14222       OKL_STREAMS_UTIL.accumulate_strm_elements( p_stm_index_no  =>  l_stmv_tbl.LAST,
14223                                                  p_selv_tbl       => l_selv_tbl,
14224                                                  x_full_selv_tbl  => l_full_selv_tbl,
14225                                                  x_return_status  => x_return_status );
14226       IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
14227           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14228       ELSIF (x_return_status = G_RET_STS_ERROR) THEN
14229           RAISE OKL_API.G_EXCEPTION_ERROR;
14230       END IF;
14231 
14232       l_stmv_rec := NULL;
14233       l_selv_tbl.delete;
14234       lx_selv_tbl.delete;
14235 
14236     END LOOP;
14237 
14238     --Create all the accumulated Streams at one shot ..
14239     Okl_Streams_Pub.create_streams_perf( p_api_version,
14240                                          p_init_msg_list,
14241                                          x_return_status,
14242                                          x_msg_count,
14243                                          x_msg_data,
14244                                          l_stmv_tbl,
14245                                          l_full_selv_tbl,
14246                                          lx_stmv_tbl,
14247                                          lx_full_selv_tbl);
14248     IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14249       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14250     ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14251       RAISE OKL_API.G_EXCEPTION_ERROR;
14252     END IF;
14253 
14254     EXCEPTION
14255       WHEN OKL_API.G_EXCEPTION_ERROR THEN
14256         x_return_status := G_RET_STS_ERROR;
14257       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
14258         x_return_status := G_RET_STS_UNEXP_ERROR;
14259       WHEN OTHERS THEN
14260         OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
14261                              p_msg_name     => G_DB_ERROR,
14262                              p_token1       => G_PROG_NAME_TOKEN,
14263                              p_token1_value => l_prog_name,
14264                              p_token2       => G_SQLCODE_TOKEN,
14265                              p_token2_value => sqlcode,
14266                              p_token3       => G_SQLERRM_TOKEN,
14267                              p_token3_value => sqlerrm);
14268 
14269         x_return_status := G_RET_STS_UNEXP_ERROR;
14270   END gen_cash_flows_for_IA;
14271 
14272 
14273   /** gboomina created GEN_INCOME_ACCRUAL_FOR_IA API for Bug 6763287
14274   ------------------------------------------------------------------
14275         PROCEDURE GEN_INCOME_ACCRUAL_FOR_IA
14276   ------------------------------------------------------------------
14277   Description: This procedure is used to generate income accrual streams
14278                 for Fees defined in Investor Agreement
14279   **/
14280 
14281   PROCEDURE gen_income_accruals_for_IA( p_api_version  IN NUMBER
14282                                , p_init_msg_list  IN VARCHAR2
14283                                , p_khr_id         IN NUMBER
14284                                , x_return_status  OUT NOCOPY VARCHAR2
14285                                , x_msg_count      OUT NOCOPY NUMBER
14286                                , x_msg_data       OUT NOCOPY VARCHAR2
14287                                )
14288     IS
14289 
14290     CURSOR c_inflows IS
14291       SELECT rgp.cle_id cle_id,
14292              TO_NUMBER(rul1.object1_id1) sty_id,
14293 	            sty.stream_type_purpose,
14294              FND_DATE.canonical_to_date(rul2.rule_information2) start_date,
14295              TO_NUMBER(rul2.rule_information3) periods,
14296              rul2.object1_id1 frequency,
14297              rul2.rule_information5 structure,
14298              DECODE(rul2.rule_information10, 'Y', 'ARREARS', 'ADVANCE') advance_arrears,
14299              FND_NUMBER.canonical_to_number(rul2.rule_information6) amount,
14300              TO_NUMBER(rul2.rule_information7) stub_days,
14301              TO_NUMBER(rul2.rule_information8) stub_amount,
14302              lse.lty_code lty_code,
14303              kle.capital_amount capital_amount,
14304              kle.residual_value residual_value,
14305        	     kle.fee_type fee_type
14306       FROM   okc_rule_groups_b rgp,
14307              okc_rules_b rul1,
14308              okc_rules_b rul2,
14309              okc_k_lines_b cle,
14310              okl_k_lines kle,
14311              okc_line_styles_b lse,
14312 	            okl_strm_type_b sty
14313       WHERE
14314              rul2.dnz_chr_id = p_khr_id
14315         AND  rul2.rule_information_category = 'LASLL'
14316         AND  rul2.rgp_id = rgp.id
14317         AND  TO_NUMBER(rul2.object2_id1) = rul1.id
14318         AND  rgp.cle_id = cle.id
14319         AND  cle.id = kle.id
14320         AND  cle.lse_id = lse.id
14321        	AND  sty.id = rul1.object1_id1;
14322 
14323       l_inflow c_inflows%rowtype;
14324 
14325       CURSOR c_hdr IS
14326         SELECT to_char(pdt.id)  pid,
14327                chr.currency_code currency_code,
14328                khr.multi_gaap_yn multi_gaap_yn -- R12.1.2
14329         FROM okc_k_headers_v chr,
14330              okl_k_headers khr,
14331              okl_products_v pdt
14332         WHERE khr.id = chr.id
14333           AND chr.id = p_khr_id
14334           AND khr.pdt_id = pdt.id(+);
14335 
14336       -- racheruv.. R12.1.2: start
14337       l_multi_gaap_yn      okl_k_headers.multi_gaap_yn%TYPE;
14338 
14339       cursor rep_pdt_csr(p_pdt_id number) is
14340       select reporting_pdt_id
14341         from okl_products
14342        where id = p_pdt_id;
14343 
14344       l_rep_pdt_id        okl_products.id%TYPE;
14345       TYPE t_pdt_tbl is table of number index by binary_integer;
14346       l_pdt_tbl           t_pdt_tbl;
14347       l_rep_flag          t_pdt_tbl;
14348       -- R12.1.2 end.
14349 
14350       l_sty_purpose       okl_strm_type_b.STREAM_TYPE_PURPOSE%TYPE;
14351       l_sty_id            okl_strm_type_b.ID%TYPE;
14352       l_sty_name          OKL_STRM_TYPE_v.name%TYPE;
14353       l_mapped_sty_name   OKL_STRM_TYPE_v.name%TYPE;
14354       l_pdt_id            okl_products.id%type;
14355       l_currency_code     okc_k_headers_all_b.currency_code%type;
14356       l_purpose_code      VARCHAR2(30) := '-99';
14357 
14358       l_selv_tbl          okl_streams_pub.selv_tbl_type;
14359       lx_selv_tbl         okl_streams_pub.selv_tbl_type;
14360       l_full_selv_tbl     okl_streams_pub.selv_tbl_type;
14361       lx_full_selv_tbl    okl_streams_pub.selv_tbl_type;
14362 
14363       l_stmv_rec          okl_streams_pub.stmv_rec_type;
14364       l_stmv_tbl          okl_Streams_Pub.stmv_tbl_type;
14365       lx_stmv_tbl         okl_Streams_Pub.stmv_tbl_type;
14366 
14367       l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'gen_income_accruals_for_IA';
14368 
14369     BEGIN
14370 
14371         -- R12.1.2 .. racheruv.. get the reporting product
14372         OPEN c_hdr;
14373         FETCH c_hdr INTO l_pdt_id, l_currency_code, l_multi_gaap_yn;
14374         CLOSE c_hdr;
14375         l_pdt_tbl(1) := l_pdt_id;
14376         l_rep_flag(1) := 0;
14377 
14378         if nvl(l_multi_gaap_yn, 'N') = 'Y' then
14379           open rep_pdt_csr(l_pdt_id);
14380           fetch rep_pdt_csr into l_rep_pdt_id;
14381           close rep_pdt_csr;
14382           if l_rep_pdt_id is not null then
14383             l_pdt_tbl(2):= l_rep_pdt_id;
14384             l_rep_flag(2) := 1;
14385           end if;
14386         end if;
14387 
14388       for i in l_pdt_tbl.first..l_pdt_tbl.last loop
14389       -- R12.1.2 .. racheruv .. end
14390 
14391       FOR l_inflow IN c_inflows LOOP
14392 
14393         l_sty_purpose := l_inflow.stream_type_purpose;
14394 
14395         get_sty_details (p_sty_id        => l_inflow.sty_id,
14396                          x_sty_id        => l_sty_id,
14397                          x_sty_name      => l_sty_name,
14398                          x_return_status => x_return_status);
14399 
14400         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14401           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14402         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14403           RAISE OKL_API.G_EXCEPTION_ERROR;
14404         END IF;
14405 
14406         /* R12.1.2 .. racheruv ..commented out the below.
14407         OPEN c_hdr;
14408         FETCH c_hdr INTO l_pdt_id, l_currency_code;
14409         CLOSE c_hdr;
14410         */
14411 
14412         OKL_ISG_UTILS_PVT.get_dependent_stream_type(
14413                 p_khr_id                => p_khr_id,
14414                 p_pdt_id                => l_pdt_tbl(i), -- R12.1.2
14415                 p_dependent_sty_purpose => 'ACCRUED_FEE_INCOME',
14416                 x_return_status         => x_return_status,
14417                 x_dependent_sty_id      => l_sty_id,
14418                 x_dependent_sty_name    => l_mapped_sty_name);
14419 
14420         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14421           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14422         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14423           RAISE OKL_API.G_EXCEPTION_ERROR;
14424         END IF;
14425 
14426         IF l_mapped_sty_name IS NOT NULL THEN
14427           get_accrual_elements (
14428             p_start_date          =>   l_inflow.start_date,
14429             p_periods             =>   l_inflow.periods,
14430             p_frequency           =>   l_inflow.frequency,
14431             p_structure           =>   l_inflow.structure,
14432             p_advance_or_arrears  =>   l_inflow.advance_arrears,
14433             p_amount              =>   l_inflow.amount,
14434             p_stub_days           =>   l_inflow.stub_days,
14435             p_stub_amount         =>   l_inflow.stub_amount,
14436             p_currency_code       =>   l_currency_code,
14437             x_selv_tbl            =>   l_selv_tbl,
14438             x_return_status       =>   x_return_status);
14439 
14440           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14441             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14442           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14443             RAISE OKL_API.G_EXCEPTION_ERROR;
14444           END IF;
14445         END IF;
14446 
14447         get_sty_details (p_sty_name      => l_mapped_sty_name,
14448                          x_sty_id        => l_sty_id,
14449                          x_sty_name      => l_sty_name,
14450                          x_return_status => x_return_status);
14451 
14452         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14453           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14454         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14455           RAISE OKL_API.G_EXCEPTION_ERROR;
14456         END IF;
14457 
14458         IF (l_selv_tbl.COUNT > 0 ) THEN
14459 
14460           -- R12.1.2 ..start
14461           if l_multi_gaap_yn = 'Y' and l_rep_flag(i) = 1 then
14462             l_purpose_code := 'REPORT';
14463           end if;
14464           -- R12.1.2 .. end
14465 
14466           get_stream_header(p_khr_id         =>   p_khr_id,
14467                             p_kle_id         =>   l_inflow.cle_id,
14468                             p_sty_id         =>   l_sty_id,
14469                             p_purpose_code   =>   l_purpose_code,
14470                             x_stmv_rec       =>   l_stmv_rec,
14471                             x_return_status  =>   x_return_status);
14472 
14473           IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14474             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14475           ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14476             RAISE OKL_API.G_EXCEPTION_ERROR;
14477           END IF;
14478 
14479           x_return_status := Okl_Streams_Util.round_streams_amount_esg(
14480                               p_api_version   => g_api_version,
14481                               p_init_msg_list => p_init_msg_list,
14482                               x_msg_count     => x_msg_count,
14483                               x_msg_data      => x_msg_data,
14484                               p_chr_id        => p_khr_id,
14485                               p_selv_tbl      => l_selv_tbl,
14486                               x_selv_tbl      => lx_selv_tbl,
14487                               p_org_id        => G_ORG_ID,
14488                               p_precision     => G_PRECISION,
14489                               p_currency_code => G_CURRENCY_CODE,
14490                               p_rounding_rule => G_ROUNDING_RULE,
14491                               p_apply_rnd_diff=> G_DIFF_LOOKUP_CODE);
14492 
14493           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
14494               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14495           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
14496               RAISE OKL_API.G_EXCEPTION_ERROR;
14497           END IF;
14498 
14499           l_selv_tbl.DELETE;
14500           l_selv_tbl := lx_selv_tbl;
14501 
14502           --Accumulate Stream Header
14503           OKL_STREAMS_UTIL.accumulate_strm_headers(
14504             p_stmv_rec       => l_stmv_rec,
14505             x_full_stmv_tbl  => l_stmv_tbl,
14506             x_return_status  => x_return_status );
14507           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
14508             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14509           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
14510             RAISE OKL_API.G_EXCEPTION_ERROR;
14511           END IF;
14512 
14513           --Accumulate Stream Elements
14514           OKL_STREAMS_UTIL.accumulate_strm_elements(
14515             p_stm_index_no  =>  l_stmv_tbl.LAST,
14516             p_selv_tbl       => l_selv_tbl,
14517             x_full_selv_tbl  => l_full_selv_tbl,
14518             x_return_status  => x_return_status );
14519           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
14520             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14521           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
14522             RAISE OKL_API.G_EXCEPTION_ERROR;
14523           END IF;
14524 
14525         END IF;
14526 
14527         l_mapped_sty_name := NULL;
14528         l_sty_name        :=  NULL;
14529         l_sty_id          :=  NULL;
14530 
14531         l_stmv_rec := NULL;
14532         l_selv_tbl.delete;
14533 
14534         lx_selv_tbl.delete;
14535 
14536       END LOOP;
14537 
14538       --Create all the accumulated Streams at one shot ..
14539       IF l_stmv_tbl.COUNT > 0 AND
14540          l_full_selv_tbl.COUNT > 0
14541       THEN
14542         Okl_Streams_Pub.create_streams_perf(
14543                                  p_api_version,
14544                                  p_init_msg_list,
14545                                  x_return_status,
14546                                  x_msg_count,
14547                                  x_msg_data,
14548                                  l_stmv_tbl,
14549                                  l_full_selv_tbl,
14550                                  lx_stmv_tbl,
14551                                  lx_full_selv_tbl);
14552         IF x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR THEN
14553           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14554         ELSIF x_return_status = OKL_API.G_RET_STS_ERROR THEN
14555           RAISE OKL_API.G_EXCEPTION_ERROR;
14556         END IF;
14557       END IF;
14558 
14559       l_stmv_tbl.delete; -- R12.1.2
14560       l_full_selv_tbl.delete; -- R12.1.2
14561 
14562     end loop; -- l_pdt_tbl(i) R12.1.2
14563 
14564     EXCEPTION
14565       WHEN OKL_API.G_EXCEPTION_ERROR THEN
14566         x_return_status := G_RET_STS_ERROR;
14567       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
14568         x_return_status := G_RET_STS_UNEXP_ERROR;
14569       WHEN OTHERS THEN
14570         OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
14571                              p_msg_name     => G_DB_ERROR,
14572                              p_token1       => G_PROG_NAME_TOKEN,
14573                              p_token1_value => l_prog_name,
14574                              p_token2       => G_SQLCODE_TOKEN,
14575                              p_token2_value => sqlcode,
14576                              p_token3       => G_SQLERRM_TOKEN,
14577                              p_token3_value => sqlerrm);
14578 
14579         x_return_status := G_RET_STS_UNEXP_ERROR;
14580 
14581   END gen_income_accruals_for_IA;
14582 
14583 
14584   /** gboomina created GEN_INCOME_ACCRUAL_FOR_IA API for Bug 6763287
14585   ------------------------------------------------------------------
14586         PROCEDURE GEN_INCOME_ACCRUAL_FOR_IA
14587   ------------------------------------------------------------------
14588   Description: This procedure is used to generate income accrual streams
14589                 for Fees defined in Investor Agreement
14590   **/
14591 
14592   PROCEDURE adjust_IA_streams( p_api_version  IN NUMBER
14593                                , p_init_msg_list  IN VARCHAR2
14594                                , p_khr_id         IN NUMBER
14595                                , x_return_status  OUT NOCOPY VARCHAR2
14596                                , x_msg_count      OUT NOCOPY NUMBER
14597                                , x_msg_data       OUT NOCOPY VARCHAR2
14598                                )
14599     IS
14600 
14601       l_strm_id_tbl okl_streams_util.NumberTabTyp;
14602      	l_say_code_tbl okl_streams_util.Var10TabTyp;
14603     	 l_active_yn_tbl okl_streams_util.Var10TabTyp;
14604      	l_date_history_tbl okl_streams_util.DateTabTyp;
14605      	l_date_curr_tbl okl_streams_util.DateTabTyp;
14606 
14607       CURSOR strms_csr  IS
14608         SELECT strm.id
14609              , strm.say_code
14610              , purpose_code -- R12.1.2
14611         FROM okl_streams strm
14612         WHERE khr_id = p_khr_id;
14613       l_strms_rec strms_csr%rowtype;
14614 
14615       i NUMBER;
14616       l_prog_name         CONSTANT VARCHAR2(61) := G_PKG_NAME||'.'||'adjust_IA_streams';
14617     BEGIN
14618 
14619       i := 0;
14620       FOR l_strms_rec IN strms_csr
14621       LOOP
14622         -- Make WORK streams CURR and CURR streams WORK
14623         IF l_strms_rec.say_code = 'WORK' THEN
14624           i := i+1;
14625           l_strm_id_tbl(i):= l_strms_rec.id;
14626           l_say_code_tbl(i) := 'CURR';
14627           l_date_curr_tbl(i) := sysdate;
14628           l_date_history_tbl(i) := NULL;
14629           l_active_yn_tbl(i) := 'Y';
14630           -- R12.1.2 .. racheruv .. change the active_yn for rpt pdt
14631           if l_strms_rec.purpose_code = 'REPORT' then
14632             l_active_yn_tbl(i) := 'N';
14633           end if;
14634           -- R12.1.2 end .. racheruv.
14635         ELSIF l_strms_rec.say_code = 'CURR' THEN
14636           i := i+1;
14637           l_strm_id_tbl(i):= l_strms_rec.id;
14638           l_say_code_tbl(i) := 'HIST';
14639           l_date_curr_tbl(i) := sysdate;
14640           l_date_history_tbl(i) := NULL;
14641           l_active_yn_tbl(i) := 'N';
14642         END IF;
14643       END LOOP;
14644 
14645      -- Update all streams at once...
14646      FORALL i IN l_strm_id_tbl.FIRST..l_strm_id_tbl.LAST
14647        UPDATE OKL_STREAMS
14648        SET 	say_code = l_say_code_tbl(i),
14649        active_yn = l_active_yn_tbl(i),
14650        date_history = l_date_history_tbl(i),
14651        date_current = l_date_curr_tbl(i),
14652        last_updated_by = FND_GLOBAL.USER_ID,      -- BUG:14749215 changes start here
14653        last_update_date = sysdate,
14654        last_update_login = FND_GLOBAL.LOGIN_ID    -- BUG:14749215 changes end here
14655        WHERE 	ID = l_strm_id_tbl(i);
14656 
14657     EXCEPTION
14658       WHEN OKL_API.G_EXCEPTION_ERROR THEN
14659         x_return_status := G_RET_STS_ERROR;
14660       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
14661         x_return_status := G_RET_STS_UNEXP_ERROR;
14662       WHEN OTHERS THEN
14663         OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
14664                              p_msg_name     => G_DB_ERROR,
14665                              p_token1       => G_PROG_NAME_TOKEN,
14666                              p_token1_value => l_prog_name,
14667                              p_token2       => G_SQLCODE_TOKEN,
14668                              p_token2_value => sqlcode,
14669                              p_token3       => G_SQLERRM_TOKEN,
14670                              p_token3_value => sqlerrm);
14671         x_return_status := G_RET_STS_UNEXP_ERROR;
14672 
14673   END adjust_IA_streams;
14674 
14675 
14676   /** gboomina created GENERATE_STREAMS_FOR_IA API for Bug 6763287
14677   ------------------------------------------------------------------
14678         PROCEDURE GENERATE_STREAMS_FOR_IA
14679   ------------------------------------------------------------------
14680   Description: This procedure is used to generate streams
14681                 for Fees defined in Investor Agreement
14682   **/
14683 
14684   PROCEDURE generate_streams_for_IA( p_api_version    IN NUMBER
14685                                    , p_init_msg_list  IN VARCHAR2
14686                                    , p_khr_id         IN NUMBER
14687                                    , x_return_status  OUT NOCOPY VARCHAR2
14688                                    , x_msg_count      OUT NOCOPY NUMBER
14689                                    , x_msg_data       OUT NOCOPY VARCHAR2
14690                                    )
14691     IS
14692       l_api_name         CONSTANT VARCHAR2(61) := 'generate_streams_for_IA';
14693 
14694       CURSOR c_hdr IS
14695       SELECT chr.currency_code
14696       FROM okc_k_headers_b chr
14697       WHERE chr.id = p_khr_id;
14698 
14699       l_currency_code okc_k_headers_all_b.currency_code%type;
14700       l_purpose_code VARCHAR2(10) := '-99';
14701       l_return_status VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
14702 
14703       -- R12.1.2 .. start
14704       CURSOR get_multi_gaap_yn(p_khr_id number) IS
14705       select multi_gaap_yn
14706         from okl_k_headers
14707        where id = p_khr_id;
14708 
14709       l_multi_gaap_yn   okl_k_headers.multi_gaap_yn%TYPE;
14710       -- R12.1.2 .. end
14711 
14712     BEGIN
14713       x_return_status := OKL_API.G_RET_STS_SUCCESS;
14714 
14715       l_return_status := OKL_API.START_ACTIVITY(
14716                                   p_api_name      => l_api_name,
14717                                   p_pkg_name      => g_pkg_name,
14718                                   p_init_msg_list => p_init_msg_list,
14719                                   l_api_version   => p_api_version,
14720                                   p_api_version   => p_api_version,
14721                                   p_api_type      => G_API_TYPE,
14722                                   x_return_status => l_return_status);
14723 
14724       -- check if activity started successfully
14725       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14726          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14727       ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14728          RAISE OKL_API.G_EXCEPTION_ERROR;
14729       END IF;
14730 
14731       ---------------------------------------------
14732       -- STEP 1: Generate Income Streams
14733       ---------------------------------------------
14734       gen_cash_flows_for_IA( p_api_version   => p_api_version
14735                            , p_init_msg_list => p_init_msg_list
14736                            , p_khr_id        => p_khr_id
14737                            , x_return_status => l_return_status
14738                            , x_msg_count     => x_msg_count
14739                            , x_msg_data      => x_msg_data);
14740 
14741       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14742          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14743       ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14744          RAISE OKL_API.G_EXCEPTION_ERROR;
14745       END IF;
14746 
14747       ---------------------------------------------
14748       -- STEP 2: Generate Income Accrual Streams
14749       ---------------------------------------------
14750       gen_income_accruals_for_IA( p_api_version   => p_api_version
14751                                 , p_init_msg_list => p_init_msg_list
14752                                 , p_khr_id        => p_khr_id
14753                                 , x_return_status => l_return_status
14754                                 , x_msg_count     => x_msg_count
14755                                 , x_msg_data      => x_msg_data);
14756 
14757       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14758          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14759       ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14760          RAISE OKL_API.G_EXCEPTION_ERROR;
14761       END IF;
14762 
14763       ---------------------------------------------
14764       -- STEP 3: Generate Expense Accrual Streams
14765       ---------------------------------------------
14766       OPEN c_hdr;
14767       FETCH c_hdr INTO l_currency_code;
14768       CLOSE c_hdr;
14769 
14770       okl_expense_streams_pvt.generate_rec_exp(
14771                                  p_khr_id        => p_khr_id
14772                                , p_deal_type     => NULL -- NULL for Investor
14773                                , p_purpose_code  => l_purpose_code
14774                                , p_currency_code => l_currency_code
14775                                , x_return_status => l_return_status);
14776 
14777       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14778          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14779       ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14780          RAISE OKL_API.G_EXCEPTION_ERROR;
14781       END IF;
14782 
14783       -- R12.1.2 .. racheruv .. generate expense for rpt.
14784       open get_multi_gaap_yn(p_khr_id);
14785       fetch get_multi_gaap_yn into l_multi_gaap_yn;
14786       close get_multi_gaap_yn;
14787 
14788       if l_multi_gaap_yn = 'Y' then
14789          l_purpose_code := 'REPORT';
14790 
14791          okl_expense_streams_pvt.generate_rec_exp(
14792                                  p_khr_id        => p_khr_id
14793                                , p_deal_type     => NULL -- NULL for Investor
14794                                , p_purpose_code  => l_purpose_code
14795                                , p_currency_code => l_currency_code
14796                                , x_return_status => l_return_status);
14797 
14798          IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14799             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14800          ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14801             RAISE OKL_API.G_EXCEPTION_ERROR;
14802          END IF;
14803 
14804       end if;
14805       -- R12.1.2 .. racheruv .. generate expense for rpt.. end
14806 
14807       -- one streams header per the payment line (LASLL) will be
14808       -- created by the previous API's. So we need to consolidate
14809       -- those stream headers into one stream header per fee line
14810       ---------------------------------------------------------------
14811       -- STEP 4: Consolidate Income Fee streams into one stream header
14812       --         per fee line
14813       ---------------------------------------------------------------
14814       l_purpose_code := '-99';
14815       consolidate_line_streams( p_khr_id         => p_khr_id
14816                               , p_purpose_code   => l_purpose_code
14817                               , x_return_status  => l_return_status);
14818 
14819       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14820          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14821       ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14822          RAISE OKL_API.G_EXCEPTION_ERROR;
14823       END IF;
14824 
14825       -- R12.1.2 .. racheruv ..
14826       if l_multi_gaap_yn = 'Y' then
14827          l_purpose_code := 'REPORT';
14828 
14829          consolidate_line_streams( p_khr_id         => p_khr_id
14830                                  , p_purpose_code   => l_purpose_code
14831                                  , x_return_status  => l_return_status);
14832 
14833          IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14834             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14835          ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14836             RAISE OKL_API.G_EXCEPTION_ERROR;
14837          END IF;
14838      end if;
14839       -- R12.1.2 .. racheruv ..
14840 
14841       ------------------------------------------------------------------
14842       -- STEP 5: Consolidate Income Accrual Fee streams into one stream
14843       --         header per fee line
14844       ------------------------------------------------------------------
14845       l_purpose_code := '-99';
14846       consolidate_acc_streams( p_khr_id         => p_khr_id
14847                              , p_purpose_code   => l_purpose_code
14848                              , x_return_status  => l_return_status);
14849 
14850       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14851          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14852       ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14853          RAISE OKL_API.G_EXCEPTION_ERROR;
14854       END IF;
14855 
14856       -- R12.1.2 .. racheruv ..
14857       if l_multi_gaap_yn = 'Y' then
14858          l_purpose_code := 'REPORT';
14859 
14860          consolidate_acc_streams( p_khr_id         => p_khr_id
14861                                 , p_purpose_code   => l_purpose_code
14862                                 , x_return_status  => l_return_status);
14863 
14864          IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14865             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14866          ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14867             RAISE OKL_API.G_EXCEPTION_ERROR;
14868          END IF;
14869      end if;
14870       -- R12.1.2 .. racheruv ..
14871 
14872       ---------------------------------------------
14873       -- STEP 6: Adjust Streams
14874       ---------------------------------------------
14875       -- Move the Working status streams to Current
14876       -- and the existing Current status streams to History
14877       adjust_IA_streams( p_api_version   => p_api_version
14878                        , p_init_msg_list => p_init_msg_list
14879                        , p_khr_id        => p_khr_id
14880                        , x_return_status => l_return_status
14881                        , x_msg_count     => x_msg_count
14882                        , x_msg_data      => x_msg_data);
14883 
14884       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
14885          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
14886       ElSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
14887          RAISE OKL_API.G_EXCEPTION_ERROR;
14888       END IF;
14889 
14890       OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data	 => x_msg_data);
14891 
14892     EXCEPTION
14893       WHEN OKL_API.G_EXCEPTION_ERROR THEN
14894         l_return_status := G_RET_STS_ERROR;
14895       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
14896         l_return_status := G_RET_STS_UNEXP_ERROR;
14897       WHEN OTHERS THEN
14898         OKL_API.SET_MESSAGE (p_app_name     => G_APP_NAME,
14899                              p_msg_name     => G_DB_ERROR,
14900                              p_token1       => G_PROG_NAME_TOKEN,
14901                              p_token1_value => l_api_name,
14902                              p_token2       => G_SQLCODE_TOKEN,
14903                              p_token2_value => sqlcode,
14904                              p_token3       => G_SQLERRM_TOKEN,
14905                              p_token3_value => sqlerrm);
14906         l_return_status := G_RET_STS_UNEXP_ERROR;
14907 
14908   END generate_streams_for_IA;
14909 
14910 END OKL_STREAM_GENERATOR_PVT;