DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKL_AM_CNTRCT_LN_TRMNT_PVT

Source


1 PACKAGE BODY okl_am_cntrct_ln_trmnt_pvt AS
2    /* $Header: OKLRCLTB.pls 120.45 2009/05/07 06:17:08 rpillay noship $ */
3    -- GLOBAL VARIABLES
4 
5    g_level_procedure   CONSTANT NUMBER         := fnd_log.level_procedure;
6    g_level_exception   CONSTANT NUMBER         := fnd_log.level_exception;
7    g_level_statement   CONSTANT NUMBER         := fnd_log.level_statement;
8    g_module_name       CONSTANT VARCHAR2 (500)
9                                 := 'okl.am.plsql.okl_am_cntrct_ln_trmnt_pvt.';
10    g_amort_complete_flag        VARCHAR2 (1)   := 'N';
11 
12 --------------- TO DO -----------------------------------------------------
13 
14    --invalidating trns once the lines are updated. -- differed for now
15 
16    --There may be a change becos of the requirement that mass rebook wont work
17    --synchronously with booking the contract and so there may be a delay in the
18    --booking of the contract. Soln may be to spawn a WF which checks when
19    --contract status changes and then calls the activate insur api.
20 
21    --------------------------------------------------------------------------
22 
23    -- Start of comments
24    --
25    -- Procedure Name : check_lease_or_loan
26    -- Desciption     : Checks if lease or loan
27    -- Business Rules :
28    -- Parameters     :
29    -- Version        : 1.0
30    -- History        : RMUNJULU 02-JAN-03 2724951 created
31    --                : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
32    --
33    -- End of comments
34    PROCEDURE check_lease_or_loan (
35       p_khr_id            IN              NUMBER,
36       x_lease_loan_type   OUT NOCOPY      VARCHAR2
37    )
38    IS
39       -- Get the K scs_code and deal_type
40       -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
41       CURSOR k_deal_type_csr (
42          p_khr_id   IN   NUMBER
43       )
44       IS
45          SELECT CHR.scs_code,
46                 khr.deal_type
47            FROM okc_k_headers_v CHR,
48                 okl_k_headers_v khr
49           WHERE CHR.ID = p_khr_id AND khr.ID = CHR.ID;
50 
51       l_lease_loan_type       VARCHAR2 (30)             := '$';
52       k_deal_type_rec         k_deal_type_csr%ROWTYPE;
53       l_module_name           VARCHAR2 (500)
54                                      := g_module_name || 'check_lease_or_loan';
55       is_debug_exception_on   BOOLEAN
56               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
57       is_debug_procedure_on   BOOLEAN
58               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
59       is_debug_statement_on   BOOLEAN
60               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
61    BEGIN
62       IF (is_debug_procedure_on)
63       THEN
64          okl_debug_pub.log_debug (g_level_procedure,
65                                   l_module_name,
66                                   'Begin(+)'
67                                  );
68       END IF;
69 
70       IF (is_debug_statement_on)
71       THEN
72          okl_debug_pub.log_debug (g_level_statement,
73                                   l_module_name,
74                                   'In param, p_khr_id: ' || p_khr_id
75                                  );
76       END IF;
77 
78       OPEN k_deal_type_csr (p_khr_id);
79 
80       FETCH k_deal_type_csr
81        INTO k_deal_type_rec;
82 
83       IF k_deal_type_csr%FOUND
84       THEN
85          -- Set the lease or loan type
86          -- (If scs_code = Lease and deal_type = Loan then Loan)
87          IF     k_deal_type_rec.scs_code = 'LEASE'
88             AND NVL (k_deal_type_rec.deal_type, '?') LIKE 'LOAN%'
89          THEN
90             l_lease_loan_type := 'LOAN';
91          ELSIF     k_deal_type_rec.scs_code = 'LEASE'
92                AND NVL (k_deal_type_rec.deal_type, '?') LIKE 'LEASE%'
93          THEN
94             l_lease_loan_type := 'LEASE';
95          ELSIF k_deal_type_rec.scs_code = 'LOAN'
96          THEN
97             l_lease_loan_type := 'LOAN';
98          END IF;
99       END IF;
100 
101       CLOSE k_deal_type_csr;
102 
103       x_lease_loan_type := l_lease_loan_type;
104 
105       IF (is_debug_procedure_on)
106       THEN
107          okl_debug_pub.log_debug (g_level_procedure,
108                                   l_module_name,
109                                   'End(-)'
110                                  );
111       END IF;
112    EXCEPTION
113       WHEN OTHERS
114       THEN
115          IF k_deal_type_csr%ISOPEN
116          THEN
117             CLOSE k_deal_type_csr;
118          END IF;
119 
120          -- Set the oracle error message
121          okl_api.set_message (p_app_name          => g_app_name_1,
122                               p_msg_name          => g_unexpected_error,
123                               p_token1            => g_sqlcode_token,
124                               p_token1_value      => SQLCODE,
125                               p_token2            => g_sqlerrm_token,
126                               p_token2_value      => SQLERRM
127                              );
128 
129          IF (is_debug_exception_on)
130          THEN
131             okl_debug_pub.log_debug (g_level_exception,
132                                      l_module_name,
133                                         'EXCEPTION :'
134                                      || 'OTHERS, SQLCODE: '
135                                      || SQLCODE
136                                      || ' , SQLERRM : '
137                                      || SQLERRM
138                                     );
139          END IF;
140    END check_lease_or_loan;
141 
142 
143  -- Start of comments
144    --
145    -- Procedure Name  : update_quote_status
146    -- Desciption     : UPDATE TERMINATION QUOTES FROM STATUS ACCEPTED TO
147    -- Business Rules  :
148    -- Parameters       :
149    -- Version      : 1.0
150    -- History        : RBRUNO BUG 6801022
151    -- 02-sep-2008 rbruno bug 7129269 fix  -
152    -- End of comments
153 
154 PROCEDURE update_quote_status(p_term_rec IN term_rec_type) IS
155 
156     lp_qtev_rec                 OKL_TRX_QUOTES_PUB.qtev_rec_type;
157     lx_qtev_rec                 OKL_TRX_QUOTES_PUB.qtev_rec_type;
158 
159     l_return_status             VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
160 
161     l_quote_status              VARCHAR2(200) := 'COMPLETE';--'OKL_QUOTE_STATUS'
162 
163     lx_msg_count                NUMBER;
164     lx_msg_data                 VARCHAR2(2000);
165 
166     l_qst_code                  varchar2(200);
167 
168     l_tmt_status_code           VARCHAR2(200);
169 
170     lx_return_status             VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
171     lx_quotes_found              VARCHAR2(1) := 'N';
172     l_api_version               NUMBER := 1;
173     l_module_name           VARCHAR2 (500)
174                                      := g_module_name || 'update_quote_status';
175       is_debug_exception_on   BOOLEAN
176               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
177       is_debug_procedure_on   BOOLEAN
178               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
179       is_debug_statement_on   BOOLEAN
180               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
181     --:= okl_debug_pub.check_log_on (l_module_name, g_level_exception);
182 
183 
184     -- Fetch tmt_status_code
185 
186          CURSOR c_tmt_status_code_csr (p_qte_id IN NUMBER) IS
187          SELECT tmt_status_code
188           FROM okl_trx_contracts trx
189           WHERE trx.qte_id = p_qte_id
190    --rkuttiya added for 12.1.1 Multi GAAP Project
191           AND  trx.representation_type = 'PRIMARY';
192    --
193 
194     --- Fetch quote satus
195 
196          CURSOR k_quotes_csr (p_qte_id IN NUMBER) IS
197          SELECT qst_code
198           FROM okl_trx_quotes_v
199           WHERE id = p_qte_id
200           AND (qtp_code LIKE 'TER%' OR qtp_code LIKE 'RES%');
201 
202 BEGIN
203      IF (is_debug_procedure_on)
204       THEN
205          okl_debug_pub.log_debug (g_level_procedure,
206                                   l_module_name,
207                                   'Begin(+)'
208                                  );
209       END IF;
210 
211 --Get termination quote status
212 
213 OPEN k_quotes_csr(p_term_rec.p_quote_id);
214 FETCH k_quotes_csr into l_qst_code;
215 CLOSE k_quotes_csr;
216 
217 IF (is_debug_statement_on)
218       THEN
219        okl_debug_pub.log_debug (g_level_statement,
220                                   l_module_name,
221                                      'quote id value :p_term_rec.p_quote_id: '
222                                   || p_term_rec.p_quote_id
223                                  );
224       END IF;
225 --check whether cutrrent  quote is in status accepted
226 IF p_term_rec.p_quote_id is not null and l_qst_code = 'ACCEPTED' THEN
227 
228 
229 
230       OPEN  c_tmt_status_code_csr(p_term_rec.p_quote_id);
231       FETCH c_tmt_status_code_csr INTO l_tmt_status_code;
232       CLOSE c_tmt_status_code_csr;
233 
234 IF (is_debug_statement_on)
235       THEN
236        okl_debug_pub.log_debug (g_level_statement,
237                                   l_module_name,
238                                      'quote exists and is in status accepted ');
239       okl_debug_pub.log_debug (g_level_statement,
240                                   l_module_name,
241                                      'Show value of  :l_tmt_status_code '
242                                   || l_tmt_status_code
243                                  );
244               end if;
245     --  02-sep-2008 rbruno bug 7129269 fix   commenting line as check is not needed
246     --  IF l_tmt_status_code = 'PROCESSED' THEN
247           lp_qtev_rec.id        :=     p_term_rec.p_quote_id;
248           lp_qtev_rec.qst_code   :=    l_quote_status;
249 
250 
251       -- Call the update of the quote header api
252       OKL_TRX_QUOTES_PUB.update_trx_quotes (
253            p_api_version                  => l_api_version,
254            p_init_msg_list                => OKL_API.G_FALSE,
255            x_return_status                => l_return_status,
256            x_msg_count                    => lx_msg_count,
257            x_msg_data                     => lx_msg_data,
258            p_qtev_rec                     => lp_qtev_rec,
259            x_qtev_rec                     => lx_qtev_rec);
260 
261       IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
262 
263        IF (is_debug_statement_on)
264       THEN
265        okl_debug_pub.log_debug (g_level_statement,
266                                   l_module_name,
267                                      'failure while updating the quote status ');
268 
269        END IF;
270       END IF;
271 
272     --END IF;
273 
274   END IF;
275 
276 END update_quote_status;
277 
278 
279    -- Start of comments
280    --
281    -- Procedure Name : log_messages
282    -- Desciption     : Logs the messages in the output log
283    -- Business Rules :
284    -- Parameters     :
285    -- Version        : 1.0
286    -- History        : RMUNJULU 04-DEC-02 Bug # 2484327. Added exception block
287    --
288    -- End of comments
289    PROCEDURE log_messages
290    IS
291       lx_error_rec   okl_api.error_rec_type;
292       l_msg_idx      INTEGER                := fnd_msg_pub.g_first;
293    BEGIN
294       -- Get the messages in the log
295       LOOP
296          fnd_msg_pub.get (p_msg_index          => l_msg_idx,
297                           p_encoded            => fnd_api.g_false,
298                           p_data               => lx_error_rec.msg_data,
299                           p_msg_index_out      => lx_error_rec.msg_count
300                          );
301 
302          IF (lx_error_rec.msg_count IS NOT NULL)
303          THEN
304             fnd_file.put_line (fnd_file.LOG, lx_error_rec.msg_data);
305             fnd_file.put_line (fnd_file.output, lx_error_rec.msg_data);
306          END IF;
307 
308          EXIT WHEN (   (lx_error_rec.msg_count = fnd_msg_pub.count_msg)
309                     OR (lx_error_rec.msg_count IS NULL)
310                    );
311          l_msg_idx := fnd_msg_pub.g_next;
312       END LOOP;
313    EXCEPTION
314       WHEN OTHERS
315       THEN
316          -- Set the oracle error message
317          okl_api.set_message (p_app_name          => g_app_name_1,
318                               p_msg_name          => g_unexpected_error,
319                               p_token1            => g_sqlcode_token,
320                               p_token1_value      => SQLCODE,
321                               p_token2            => g_sqlerrm_token,
322                               p_token2_value      => SQLERRM
323                              );
324    END log_messages;
325 
326    -- Start of comments
327    --
328    -- Procedure Name : set_database_values
329    -- Desciption     : Set the parameters values from database
330    -- Business Rules :
331    -- Parameters     :
332    -- Version        : 1.0
333    -- History        : RMUNJULU 04-DEC-02 Bug#2484327 Added comments to FOR LOOPs
334    --                : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
335    --
336    -- End of comments
337    PROCEDURE set_database_values (
338       px_term_rec   IN OUT NOCOPY   term_rec_type
339    )
340    IS
341       -- Cursor to get the quote details
342       CURSOR get_quote_details_csr (
343          p_quote_id   IN   NUMBER
344       )
345       IS
346          SELECT qte.qtp_code qtp_code,
347                 qte.qrs_code qrs_code
348            FROM okl_trx_quotes_v qte
349           WHERE qte.ID = p_quote_id;
350 
351       -- Cursor to get the k details
352       -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
353       CURSOR get_k_details_csr (
354          p_khr_id   IN   NUMBER
355       )
356       IS
357          SELECT khr.contract_number contract_number
358            FROM okc_k_headers_v khr
359           WHERE khr.ID = p_khr_id;
360 
361       l_module_name           VARCHAR2 (500)
362                                      := g_module_name || 'set_database_values';
363       is_debug_exception_on   BOOLEAN
364               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
365       is_debug_procedure_on   BOOLEAN
366               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
367       is_debug_statement_on   BOOLEAN
368               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
369    BEGIN
370       IF (is_debug_procedure_on)
371       THEN
372          okl_debug_pub.log_debug (g_level_procedure,
373                                   l_module_name,
374                                   'Begin(+)'
375                                  );
376       END IF;
377 
378       IF (is_debug_statement_on)
379       THEN
380          okl_debug_pub.log_debug (g_level_statement,
381                                   l_module_name,
382                                      'In param, px_term_rec.p_contract_id: '
383                                   || px_term_rec.p_contract_id
384                                  );
385          okl_debug_pub.log_debug (g_level_statement,
386                                   l_module_name,
387                                      'In param, px_term_rec.p_quote_id: '
388                                   || px_term_rec.p_quote_id
389                                  );
390       END IF;
391 
392       -- RMUNJULU -- Bug # 2484327 Added comments to for loop
393       -- Get the contract number for the contract id passed
394       FOR get_k_details_rec IN get_k_details_csr (px_term_rec.p_contract_id)
395       LOOP
396          px_term_rec.p_contract_number := get_k_details_rec.contract_number;
397       END LOOP;
398 
399       -- If the termination request is from quote,
400       -- populate the rest of the quote attributes
401       IF     px_term_rec.p_quote_id IS NOT NULL
402          AND px_term_rec.p_quote_id <> g_miss_num
403       THEN
404          -- RMUNJULU -- Bug # 2484327 Added comments to for loop
405          -- Get the quote_type and quote_reason for the quote id passed
406          FOR get_quote_details_rec IN
407             get_quote_details_csr (px_term_rec.p_quote_id)
408          LOOP
409             px_term_rec.p_quote_type := get_quote_details_rec.qtp_code;
410             px_term_rec.p_quote_reason := get_quote_details_rec.qrs_code;
411          END LOOP;
412       END IF;
413 
414       IF (is_debug_procedure_on)
415       THEN
416          okl_debug_pub.log_debug (g_level_procedure,
417                                   l_module_name,
418                                   'End(-)'
419                                  );
420       END IF;
421    EXCEPTION
422       WHEN OTHERS
423       THEN
424          -- RMUNJULU -- Bug # 2484327 Added code to close cursors if open
425          IF get_k_details_csr%ISOPEN
426          THEN
427             CLOSE get_k_details_csr;
428          END IF;
429 
430          IF get_quote_details_csr%ISOPEN
431          THEN
432             CLOSE get_quote_details_csr;
433          END IF;
434 
435          -- Set the oracle error message
436          okl_api.set_message (p_app_name          => g_app_name_1,
437                               p_msg_name          => g_unexpected_error,
438                               p_token1            => g_sqlcode_token,
439                               p_token1_value      => SQLCODE,
440                               p_token2            => g_sqlerrm_token,
441                               p_token2_value      => SQLERRM
442                              );
443 
444          IF (is_debug_exception_on)
445          THEN
446             okl_debug_pub.log_debug (g_level_exception,
447                                      l_module_name,
448                                         'EXCEPTION :'
449                                      || 'OTHERS, SQLCODE: '
450                                      || SQLCODE
451                                      || ' , SQLERRM : '
452                                      || SQLERRM
453                                     );
454          END IF;
455    END set_database_values;
456 
457    -- Start of comments
458    --
459    -- Procedure Name : set_info_messages
460    -- Desciption     : Set the messages before starting the termination process
461    -- Business Rules :
462    -- Parameters     :
463    -- Version        : 1.0
464    -- History        :
465    --
466    -- End of comments
467    PROCEDURE set_info_messages (
468       p_term_rec   IN   term_rec_type
469    )
470    IS
471       l_quote_type            VARCHAR2 (2000);
472       l_module_name           VARCHAR2 (500)
473                                       := g_module_name || 'set_info_messages';
474       is_debug_exception_on   BOOLEAN
475              := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
476       is_debug_procedure_on   BOOLEAN
477              := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
478       is_debug_statement_on   BOOLEAN
479              := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
480    BEGIN
481       IF (is_debug_procedure_on)
482       THEN
483          okl_debug_pub.log_debug (g_level_procedure,
484                                   l_module_name,
485                                   'Begin(+)'
486                                  );
487       END IF;
488 
489       IF (is_debug_statement_on)
490       THEN
491          okl_debug_pub.log_debug (g_level_statement,
492                                   l_module_name,
493                                      'In param, p_term_rec.p_control_flag: '
494                                   || p_term_rec.p_control_flag
495                                  );
496          okl_debug_pub.log_debug (g_level_statement,
497                                   l_module_name,
498                                      'In param, p_term_rec.p_quote_id: '
499                                   || p_term_rec.p_quote_id
500                                  );
501       END IF;
502 
503       -- Check and Set the message saying where the termination request came from
504       IF (p_term_rec.p_control_flag = 'CONTRACT_TERMINATE_SCRN')
505       THEN
506          -- Termination request from Request Contract Termination screen
507          -- for contract CONTRACT_NUMBER.
508          okl_api.set_message (p_app_name          => g_app_name,
509                               p_msg_name          => 'OKL_AM_TERM_REQ_FRM_SCRN',
510                               p_token1            => 'CONTRACT_NUMBER',
511                               p_token1_value      => p_term_rec.p_contract_number
512                              );
513 
514          -- Set the additional message to let the user know if there was a quote
515          IF     p_term_rec.p_quote_id IS NOT NULL
516             AND p_term_rec.p_quote_id <> g_miss_num
517          THEN
518             -- Get the lookup meaning for quote type
519             l_quote_type :=
520                okl_am_util_pvt.get_lookup_meaning
521                                    (p_lookup_type      => 'OKL_QUOTE_TYPE',
522                                     p_lookup_code      => p_term_rec.p_quote_type,
523                                     p_validate_yn      => g_yes
524                                    );
525             -- Termination request from accepted QUOTE_TYPE
526             -- for contract CONTRACT_NUMBER.
527             okl_api.set_message
528                                (p_app_name          => g_app_name,
529                                 p_msg_name          => 'OKL_AM_TERM_REQ_FRM_QTE',
530                                 p_token1            => 'QUOTE_TYPE',
531                                 p_token1_value      => l_quote_type,
532                                 p_token2            => 'CONTRACT_NUMBER',
533                                 p_token2_value      => p_term_rec.p_contract_number
534                                );
535          END IF;
536       ELSIF (p_term_rec.p_control_flag = 'TRMNT_QUOTE_UPDATE')
537       THEN
538          -- Get the lookup meaning for quote type
539          l_quote_type :=
540             okl_am_util_pvt.get_lookup_meaning
541                                    (p_lookup_type      => 'OKL_QUOTE_TYPE',
542                                     p_lookup_code      => p_term_rec.p_quote_type,
543                                     p_validate_yn      => g_yes
544                                    );
545          -- Termination request from accepted QUOTE_TYPE
546          -- for contract CONTRACT_NUMBER.
547          okl_api.set_message (p_app_name          => g_app_name,
548                               p_msg_name          => 'OKL_AM_TERM_REQ_FRM_QTE',
549                               p_token1            => 'QUOTE_TYPE',
550                               p_token1_value      => l_quote_type,
551                               p_token2            => 'CONTRACT_NUMBER',
552                               p_token2_value      => p_term_rec.p_contract_number
553                              );
554       ELSIF (p_term_rec.p_control_flag = 'BATCH_PROCESS')
555       THEN
556          -- Auto termination request for contract CONTRACT_NUMBER.
557          okl_api.set_message (p_app_name          => g_app_name,
558                               p_msg_name          => 'OKL_AM_AUTO_TERM_REQ',
559                               p_token1            => 'CONTRACT_NUMBER',
560                               p_token1_value      => p_term_rec.p_contract_number
561                              );
562       END IF;
563 
564       IF (is_debug_procedure_on)
565       THEN
566          okl_debug_pub.log_debug (g_level_procedure,
567                                   l_module_name,
568                                   'End(-)'
569                                  );
570       END IF;
571    EXCEPTION
572       WHEN OTHERS
573       THEN
574          -- Set the oracle error message
575          okl_api.set_message (p_app_name          => g_app_name_1,
576                               p_msg_name          => g_unexpected_error,
577                               p_token1            => g_sqlcode_token,
578                               p_token1_value      => SQLCODE,
579                               p_token2            => g_sqlerrm_token,
580                               p_token2_value      => SQLERRM
581                              );
582 
583          IF (is_debug_exception_on)
584          THEN
585             okl_debug_pub.log_debug (g_level_exception,
586                                      l_module_name,
587                                         'EXCEPTION :'
588                                      || 'OTHERS, SQLCODE: '
589                                      || SQLCODE
590                                      || ' , SQLERRM : '
591                                      || SQLERRM
592                                     );
593          END IF;
594    END set_info_messages;
595 
596    -- Start of comments
597    --
598    -- Procedure Name : set_overall_status
599    -- Desciption     : Set the overall status for the Termination API
600    -- Business Rules :
601    -- Parameters     :
602    -- Version        : 1.0
603    -- History        :
604    --
605    -- End of comments
606    PROCEDURE set_overall_status (
607       p_return_status     IN              VARCHAR2,
608       px_overall_status   IN OUT NOCOPY   VARCHAR2
609    )
610    IS
611    BEGIN
612       -- Store the highest degree of error
613       -- Set p_overall_status only if p_overall_status was successful and
614       -- p_return_status is not null
615       IF     px_overall_status = g_ret_sts_success
616          AND (   p_return_status IS NOT NULL
617               OR p_return_status <> okl_api.g_miss_char
618              )
619       THEN
620          px_overall_status := p_return_status;
621       END IF;
622    EXCEPTION
623       WHEN OTHERS
624       THEN
625          -- Set the oracle error message
626          okl_api.set_message (p_app_name          => g_app_name_1,
627                               p_msg_name          => g_unexpected_error,
628                               p_token1            => g_sqlcode_token,
629                               p_token1_value      => SQLCODE,
630                               p_token2            => g_sqlerrm_token,
631                               p_token2_value      => SQLERRM
632                              );
633    END set_overall_status;
634 
635    -- Start of comments
636    --
637    -- Procedure Name : initialize_transaction
638    -- Desciption     : Initialize the Transaction Record (okl_trx_contracts rec)
639    -- Business Rules :
640    -- Parameters     :
641    -- Version        : 1.0
642    -- History        :
643    --
644    -- End of comments
645    PROCEDURE initialize_transaction (
646       p_term_rec        IN              term_rec_type,
647       p_sys_date        IN              DATE,
648       p_control_flag    IN              VARCHAR2,
649       px_tcnv_rec       IN OUT NOCOPY   tcnv_rec_type,
650       x_return_status   OUT NOCOPY      VARCHAR2
651    )
652    IS
653       l_try_id                NUMBER;
654       l_currency_code         VARCHAR2 (2000);
655       l_trans_meaning         VARCHAR2 (200);
656       l_return_status         VARCHAR2 (1)    := g_ret_sts_success;
657       l_module_name           VARCHAR2 (500)
658                                  := g_module_name || 'initialize_transaction';
659       is_debug_exception_on   BOOLEAN
660              := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
661       is_debug_procedure_on   BOOLEAN
662              := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
663       is_debug_statement_on   BOOLEAN
664              := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
665    BEGIN
666       IF (is_debug_procedure_on)
667       THEN
668          okl_debug_pub.log_debug (g_level_procedure,
669                                   l_module_name,
670                                   'Begin(+)'
671                                  );
672       END IF;
673 
674       IF (is_debug_statement_on)
675       THEN
676          okl_debug_pub.log_debug (g_level_statement,
677                                   l_module_name,
678                                   'In param, p_sys_date: ' || p_sys_date
679                                  );
680          okl_debug_pub.log_debug (g_level_statement,
681                                   l_module_name,
682                                      'In param, p_control_flag: '
683                                   || p_control_flag
684                                  );
685          okl_debug_pub.log_debug (g_level_statement,
686                                   l_module_name,
687                                      'In param, p_term_rec.p_contract_id: '
688                                   || p_term_rec.p_contract_id
689                                  );
690          okl_debug_pub.log_debug (g_level_statement,
691                                   l_module_name,
692                                      'In param, p_term_rec.p_quote_id: '
693                                   || p_term_rec.p_quote_id
694                                  );
695          okl_debug_pub.log_debug
696                              (g_level_statement,
697                               l_module_name,
698                                  'In param, p_term_rec.p_termination_reason: '
699                               || p_term_rec.p_termination_reason
700                              );
701       END IF;
702 
703       IF p_control_flag = 'CREATE'
704       THEN
705          IF (is_debug_statement_on)
706          THEN
707             okl_debug_pub.log_debug
708                                 (g_level_statement,
709                                  l_module_name,
710                                  'calling OKL_AM_UTIL_PVT.get_transaction_id'
711                                 );
712          END IF;
713 
714          -- Get the Accounting Transaction Id
715          okl_am_util_pvt.get_transaction_id
716                                           (p_try_name           => 'Termination',
717                                            x_return_status      => l_return_status,
718                                            x_try_id             => l_try_id
719                                           );
720 
721          IF (is_debug_statement_on)
722          THEN
723             okl_debug_pub.log_debug
724                (g_level_statement,
725                 l_module_name,
726                    'called OKL_AM_UTIL_PVT.get_transaction_id , l_return_status: '
727                 || l_return_status
728                );
729          END IF;
730 
731          -- Get the meaning of Accounting lookup
732          l_trans_meaning :=
733             okl_am_util_pvt.get_lookup_meaning
734                                 (p_lookup_type      => 'OKL_ACCOUNTING_EVENT_TYPE',
735                                  p_lookup_code      => 'TERMINATION',
736                                  p_validate_yn      => 'Y'
737                                 );
738 
739          IF l_return_status <> g_ret_sts_success
740          THEN
741             -- Unable to find a transaction type for the transaction TRY_NAME
742             okl_api.set_message (p_app_name          => g_app_name,
743                                  p_msg_name          => 'OKL_AM_NO_TRX_TYPE_FOUND',
744                                  p_token1            => 'TRY_NAME',
745                                  p_token1_value      => l_trans_meaning
746                                 );
747             RAISE okl_api.g_exception_error;
748          END IF;
749 
750          -- Get the contract currency code
751          l_currency_code :=
752                    okl_am_util_pvt.get_chr_currency (p_term_rec.p_contract_id);
753          -- initialize the transaction rec
754          px_tcnv_rec.khr_id := p_term_rec.p_contract_id;
755          px_tcnv_rec.tcn_type := 'ALT';                                 -- TMT
756          px_tcnv_rec.try_id := l_try_id;
757          px_tcnv_rec.currency_code := l_currency_code;
758       END IF;
759 
760       -- Set the rest of the transaction rec
761       px_tcnv_rec.qte_id := p_term_rec.p_quote_id;
762       px_tcnv_rec.tsu_code := 'ENTERED';
763       px_tcnv_rec.tmt_status_code := 'ENTERED';
764                                  --akrangan changes for sla tmt_status_code cr
765       px_tcnv_rec.date_transaction_occurred := p_sys_date;
766       --20-NOV-2006 ANSETHUR R12B - LEGAL ENTITY UPTAKE PROJECT
767       px_tcnv_rec.legal_entity_id :=
768                 okl_legal_entity_util.get_khr_le_id (p_term_rec.p_contract_id);
769 
770       -- set the termination reason (TRN_CODE)
771       IF     (p_term_rec.p_termination_reason <> okl_api.g_miss_char)
772          AND (p_term_rec.p_termination_reason IS NOT NULL)
773       THEN
774          px_tcnv_rec.trn_code := p_term_rec.p_termination_reason;
775       ELSE
776          px_tcnv_rec.trn_code := 'EXP';
777       END IF;
778 
779       -- Set the return status
780       x_return_status := l_return_status;
781 
782       IF (is_debug_procedure_on)
783       THEN
784          okl_debug_pub.log_debug (g_level_procedure,
785                                   l_module_name,
786                                   'End(-)'
787                                  );
788       END IF;
789    EXCEPTION
790       WHEN okl_api.g_exception_error
791       THEN
792          -- Set the return status
793          x_return_status := g_ret_sts_unexp_error;
794 
795          IF (is_debug_exception_on)
796          THEN
797             okl_debug_pub.log_debug (g_level_exception,
798                                      l_module_name,
799                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
800                                     );
801          END IF;
802       WHEN OTHERS
803       THEN
804          -- Set the oracle error message
805          okl_api.set_message (p_app_name          => g_app_name_1,
806                               p_msg_name          => g_unexpected_error,
807                               p_token1            => g_sqlcode_token,
808                               p_token1_value      => SQLCODE,
809                               p_token2            => g_sqlerrm_token,
810                               p_token2_value      => SQLERRM
811                              );
812          -- Set the return status
813          x_return_status := g_ret_sts_unexp_error;
814 
815          IF (is_debug_exception_on)
816          THEN
817             okl_debug_pub.log_debug (g_level_exception,
818                                      l_module_name,
819                                         'EXCEPTION :'
820                                      || 'OTHERS, SQLCODE: '
821                                      || SQLCODE
822                                      || ' , SQLERRM : '
823                                      || SQLERRM
824                                     );
825          END IF;
826    END initialize_transaction;
827 
828    -- Start of comments
829    --
830    -- Procedure Name : set_transaction_rec
831    -- Desciption     : Set the Transaction Record (okl_trx_contracts rec)
832    -- Business Rules :
833    -- Parameters     :
834    -- Version      : 1.0
835    -- History        : RMUNJULU 2757312 Added code to set tmt_generic flags
836    --
837    -- End of comments
838    PROCEDURE set_transaction_rec (
839       p_return_status    IN              VARCHAR2,
840       p_overall_status   IN              VARCHAR2,
841       p_tmt_flag         IN              VARCHAR2,
842       p_tsu_code         IN              VARCHAR2,
843       p_ret_val          IN              VARCHAR2,
844       px_tcnv_rec        IN OUT NOCOPY   tcnv_rec_type
845    )
846    IS
847       l_module_name           VARCHAR2 (500)
848                                     := g_module_name || 'set_transaction_rec';
849       is_debug_exception_on   BOOLEAN
850              := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
851       is_debug_procedure_on   BOOLEAN
852              := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
853       is_debug_statement_on   BOOLEAN
854              := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
855    BEGIN
856       IF (is_debug_procedure_on)
857       THEN
858          okl_debug_pub.log_debug (g_level_procedure,
859                                   l_module_name,
860                                   'Begin(+)'
861                                  );
862       END IF;
863 
864       IF (is_debug_statement_on)
865       THEN
866          okl_debug_pub.log_debug (g_level_statement,
867                                   l_module_name,
868                                      'In param, p_return_status: '
869                                   || p_return_status
870                                  );
871          okl_debug_pub.log_debug (g_level_statement,
872                                   l_module_name,
873                                      'In param, p_overall_status: '
874                                   || p_overall_status
875                                  );
876          okl_debug_pub.log_debug (g_level_statement,
877                                   l_module_name,
878                                   'In param, p_tmt_flag: ' || p_tmt_flag
879                                  );
880          okl_debug_pub.log_debug (g_level_statement,
881                                   l_module_name,
882                                   'In param, p_tsu_code: ' || p_tsu_code
883                                  );
884          okl_debug_pub.log_debug (g_level_statement,
885                                   l_module_name,
886                                   'In param, p_ret_val: ' || p_ret_val
887                                  );
888       END IF;
889 
890       -- set the transaction record
891       IF (p_overall_status = g_ret_sts_success)
892       THEN
893          --px_tcnv_rec.tsu_code := p_tsu_code; --akrangan chaged
894          px_tcnv_rec.tmt_status_code := p_tsu_code;
895                                 --akrangan changed for sla tmt_status_code cr
896       ELSE
897          px_tcnv_rec.tmt_status_code := 'ERROR';
898                                 --akrangan changes for sla tmt_status_code cr
899       END IF;
900 
901       IF (p_ret_val = okl_api.g_miss_char)
902       THEN
903          -- No value for p_ret_val
904          IF (p_return_status = g_ret_sts_success)
905          THEN
906             -- ret stat success
907             IF (p_tmt_flag = 'TMT_EVERGREEN_YN')
908             THEN
909                px_tcnv_rec.tmt_evergreen_yn := g_yes;
910             ELSIF (p_tmt_flag = 'TMT_CLOSE_BALANCES_YN')
911             THEN
912                px_tcnv_rec.tmt_close_balances_yn := g_yes;
913             ELSIF (p_tmt_flag = 'TMT_ACCOUNTING_ENTRIES_YN')
914             THEN
915                px_tcnv_rec.tmt_accounting_entries_yn := g_yes;
916             ELSIF (p_tmt_flag = 'TMT_CANCEL_INSURANCE_YN')
917             THEN
918                px_tcnv_rec.tmt_cancel_insurance_yn := g_yes;
919             ELSIF (p_tmt_flag = 'TMT_ASSET_DISPOSITION_YN')
920             THEN
921                px_tcnv_rec.tmt_asset_disposition_yn := g_yes;
922             ELSIF (p_tmt_flag = 'TMT_AMORTIZATION_YN')
923             THEN
924                px_tcnv_rec.tmt_amortization_yn := g_yes;
925             ELSIF (p_tmt_flag = 'TMT_ASSET_RETURN_YN')
926             THEN
927                px_tcnv_rec.tmt_asset_return_yn := g_yes;
928             ELSIF (p_tmt_flag = 'TMT_CONTRACT_UPDATED_YN')
929             THEN
930                px_tcnv_rec.tmt_contract_updated_yn := g_yes;
931             ELSIF (p_tmt_flag = 'TMT_STREAMS_UPDATED_YN')
932             THEN
933                px_tcnv_rec.tmt_streams_updated_yn := g_yes;
934             ELSIF (p_tmt_flag = 'TMT_VALIDATED_YN')
935             THEN
936                px_tcnv_rec.tmt_validated_yn := g_yes;
937             ELSIF (p_tmt_flag = 'TMT_SPLIT_ASSET_YN')
938             THEN
939                px_tcnv_rec.tmt_split_asset_yn := g_yes;
940             -- RMUNJULU 2757312 Added
941             ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG1_YN')
942             THEN
943                px_tcnv_rec.tmt_generic_flag1_yn := g_yes;
944             ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG2_YN')
945             THEN
946                px_tcnv_rec.tmt_generic_flag2_yn := g_yes;
947             ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG3_YN')
948             THEN
949                px_tcnv_rec.tmt_generic_flag3_yn := g_yes;
950             END IF;
951          ELSE
952             -- return_status not success
953             IF (p_tmt_flag = 'TMT_EVERGREEN_YN')
954             THEN
955                px_tcnv_rec.tmt_evergreen_yn := g_no;
956             ELSIF (p_tmt_flag = 'TMT_CLOSE_BALANCES_YN')
957             THEN
958                px_tcnv_rec.tmt_close_balances_yn := g_no;
959             ELSIF (p_tmt_flag = 'TMT_ACCOUNTING_ENTRIES_YN')
960             THEN
961                px_tcnv_rec.tmt_accounting_entries_yn := g_no;
962             ELSIF (p_tmt_flag = 'TMT_CANCEL_INSURANCE_YN')
963             THEN
964                px_tcnv_rec.tmt_cancel_insurance_yn := g_no;
965             ELSIF (p_tmt_flag = 'TMT_ASSET_DISPOSITION_YN')
966             THEN
967                px_tcnv_rec.tmt_asset_disposition_yn := g_no;
968             ELSIF (p_tmt_flag = 'TMT_AMORTIZATION_YN')
969             THEN
970                px_tcnv_rec.tmt_amortization_yn := g_no;
971             ELSIF (p_tmt_flag = 'TMT_ASSET_RETURN_YN')
972             THEN
973                px_tcnv_rec.tmt_asset_return_yn := g_no;
974             ELSIF (p_tmt_flag = 'TMT_CONTRACT_UPDATED_YN')
975             THEN
976                px_tcnv_rec.tmt_contract_updated_yn := g_no;
977             ELSIF (p_tmt_flag = 'TMT_STREAMS_UPDATED_YN')
978             THEN
979                px_tcnv_rec.tmt_streams_updated_yn := g_no;
980             ELSIF (p_tmt_flag = 'TMT_VALIDATED_YN')
981             THEN
982                px_tcnv_rec.tmt_validated_yn := g_no;
983             ELSIF (p_tmt_flag = 'TMT_SPLIT_ASSET_YN')
984             THEN
985                px_tcnv_rec.tmt_split_asset_yn := g_no;
986             -- RMUNJULU 2757312 Added
987             ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG1_YN')
988             THEN
989                px_tcnv_rec.tmt_generic_flag1_yn := g_no;
990             ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG2_YN')
991             THEN
992                px_tcnv_rec.tmt_generic_flag2_yn := g_no;
993             ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG3_YN')
994             THEN
995                px_tcnv_rec.tmt_generic_flag3_yn := g_no;
996             END IF;
997          END IF;
998       ELSE
999          -- value for p_ret_val passed ( will override return_status val)
1000          IF (p_tmt_flag = 'TMT_EVERGREEN_YN')
1001          THEN
1002             px_tcnv_rec.tmt_evergreen_yn := p_ret_val;
1003          ELSIF (p_tmt_flag = 'TMT_CLOSE_BALANCES_YN')
1004          THEN
1005             px_tcnv_rec.tmt_close_balances_yn := p_ret_val;
1006          ELSIF (p_tmt_flag = 'TMT_ACCOUNTING_ENTRIES_YN')
1007          THEN
1008             px_tcnv_rec.tmt_accounting_entries_yn := p_ret_val;
1009          ELSIF (p_tmt_flag = 'TMT_CANCEL_INSURANCE_YN')
1010          THEN
1011             px_tcnv_rec.tmt_cancel_insurance_yn := p_ret_val;
1012          ELSIF (p_tmt_flag = 'TMT_ASSET_DISPOSITION_YN')
1013          THEN
1014             px_tcnv_rec.tmt_asset_disposition_yn := p_ret_val;
1015          ELSIF (p_tmt_flag = 'TMT_AMORTIZATION_YN')
1016          THEN
1017             px_tcnv_rec.tmt_amortization_yn := p_ret_val;
1018          ELSIF (p_tmt_flag = 'TMT_ASSET_RETURN_YN')
1019          THEN
1020             px_tcnv_rec.tmt_asset_return_yn := p_ret_val;
1021          ELSIF (p_tmt_flag = 'TMT_CONTRACT_UPDATED_YN')
1022          THEN
1023             px_tcnv_rec.tmt_contract_updated_yn := p_ret_val;
1024          ELSIF (p_tmt_flag = 'TMT_STREAMS_UPDATED_YN')
1025          THEN
1026             px_tcnv_rec.tmt_streams_updated_yn := p_ret_val;
1027          ELSIF (p_tmt_flag = 'TMT_VALIDATED_YN')
1028          THEN
1029             px_tcnv_rec.tmt_validated_yn := p_ret_val;
1030          ELSIF (p_tmt_flag = 'TMT_SPLIT_ASSET_YN')
1031          THEN
1032             px_tcnv_rec.tmt_split_asset_yn := p_ret_val;
1033          -- RMUNJULU 2757312 Added
1034          ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG1_YN')
1035          THEN
1036             px_tcnv_rec.tmt_generic_flag1_yn := p_ret_val;
1037          ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG2_YN')
1038          THEN
1039             px_tcnv_rec.tmt_generic_flag2_yn := p_ret_val;
1040          ELSIF (p_tmt_flag = 'TMT_GENERIC_FLAG3_YN')
1041          THEN
1042             px_tcnv_rec.tmt_generic_flag3_yn := p_ret_val;
1043          END IF;
1044       END IF;
1045 
1046       IF (is_debug_procedure_on)
1047       THEN
1048          okl_debug_pub.log_debug (g_level_procedure,
1049                                   l_module_name,
1050                                   'End(-)'
1051                                  );
1052       END IF;
1053    EXCEPTION
1054       WHEN OTHERS
1055       THEN
1056          -- Set the oracle error message
1057          okl_api.set_message (p_app_name          => g_app_name_1,
1058                               p_msg_name          => g_unexpected_error,
1059                               p_token1            => g_sqlcode_token,
1060                               p_token1_value      => SQLCODE,
1061                               p_token2            => g_sqlerrm_token,
1062                               p_token2_value      => SQLERRM
1063                              );
1064 
1065          IF (is_debug_exception_on)
1066          THEN
1067             okl_debug_pub.log_debug (g_level_exception,
1068                                      l_module_name,
1069                                         'EXCEPTION :'
1070                                      || 'OTHERS, SQLCODE: '
1071                                      || SQLCODE
1072                                      || ' , SQLERRM : '
1073                                      || SQLERRM
1074                                     );
1075          END IF;
1076    END set_transaction_rec;
1077 
1078    -- Start of comments
1079    --
1080    -- Procedure Name : process_transaction
1081    -- Desciption     : Insert/Update the Transaction Record (okl_trx_contracts )
1082    -- Business Rules :
1083    -- Parameters     :
1084    -- Version        : 1.0
1085    -- History
1086    --
1087    -- End of comments
1088    PROCEDURE process_transaction (
1089       p_id              IN              NUMBER,
1090       p_term_rec        IN              term_rec_type,
1091       p_tcnv_rec        IN              tcnv_rec_type,
1092       p_trn_mode        IN              VARCHAR2,
1093       x_id              OUT NOCOPY      NUMBER,
1094       x_return_status   OUT NOCOPY      VARCHAR2
1095    )
1096    IS
1097       l_return_status          VARCHAR2 (1)    := g_ret_sts_success;
1098       lp_tcnv_rec              tcnv_rec_type   := p_tcnv_rec;
1099       lx_tcnv_rec              tcnv_rec_type;
1100       l_api_version   CONSTANT NUMBER          := g_api_version;
1101       l_msg_count              NUMBER          := g_miss_num;
1102       l_msg_data               VARCHAR2 (2000);
1103       l_module_name            VARCHAR2 (500)
1104                                     := g_module_name || 'process_transaction';
1105       is_debug_exception_on    BOOLEAN
1106              := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
1107       is_debug_procedure_on    BOOLEAN
1108              := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
1109       is_debug_statement_on    BOOLEAN
1110              := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
1111    BEGIN
1112       IF (is_debug_procedure_on)
1113       THEN
1114          okl_debug_pub.log_debug (g_level_procedure,
1115                                   l_module_name,
1116                                   'Begin(+)'
1117                                  );
1118       END IF;
1119 
1120       IF (is_debug_statement_on)
1121       THEN
1122          okl_debug_pub.log_debug (g_level_statement,
1123                                   l_module_name,
1124                                   'In param, p_id: ' || p_id
1125                                  );
1126          okl_debug_pub.log_debug (g_level_statement,
1127                                   l_module_name,
1128                                   'In param, p_trn_mode: ' || p_trn_mode
1129                                  );
1130       END IF;
1131 
1132       -- Set the savepoint for this api
1133       SAVEPOINT process_transaction;
1134       -- Clear the recycle flag after processing
1135       lp_tcnv_rec.tmt_recycle_yn := NULL;
1136 
1137       -- Based on mode Insert/Update the transaction rec
1138       IF p_trn_mode = 'INSERT'
1139       THEN
1140          IF (is_debug_statement_on)
1141          THEN
1142             okl_debug_pub.log_debug
1143                         (g_level_statement,
1144                          l_module_name,
1145                          'calling OKL_TRX_CONTRACTS_PUB.create_trx_contracts'
1146                         );
1147          END IF;
1148 
1149          -- insert transaction rec
1150          okl_trx_contracts_pub.create_trx_contracts
1151                                           (p_api_version        => l_api_version,
1152                                            p_init_msg_list      => g_false,
1153                                            x_return_status      => l_return_status,
1154                                            x_msg_count          => l_msg_count,
1155                                            x_msg_data           => l_msg_data,
1156                                            p_tcnv_rec           => lp_tcnv_rec,
1157                                            x_tcnv_rec           => lx_tcnv_rec
1158                                           );
1159 
1160          IF (is_debug_statement_on)
1161          THEN
1162             okl_debug_pub.log_debug
1163                (g_level_statement,
1164                 l_module_name,
1165                    'called OKL_TRX_CONTRACTS_PUB.create_trx_contracts , return status: '
1166                 || l_return_status
1167                );
1168          END IF;
1169       ELSIF p_trn_mode = 'UPDATE'
1170       THEN
1171          IF (is_debug_statement_on)
1172          THEN
1173             okl_debug_pub.log_debug
1174                         (g_level_statement,
1175                          l_module_name,
1176                          'calling OKL_TRX_CONTRACTS_PUB.update_trx_contracts'
1177                         );
1178          END IF;
1179 
1180          -- update transaction rec
1181          okl_trx_contracts_pub.update_trx_contracts
1182                                           (p_api_version        => l_api_version,
1183                                            p_init_msg_list      => g_false,
1184                                            x_return_status      => l_return_status,
1185                                            x_msg_count          => l_msg_count,
1186                                            x_msg_data           => l_msg_data,
1187                                            p_tcnv_rec           => lp_tcnv_rec,
1188                                            x_tcnv_rec           => lx_tcnv_rec
1189                                           );
1190 
1191          IF (is_debug_statement_on)
1192          THEN
1193             okl_debug_pub.log_debug
1194                (g_level_statement,
1195                 l_module_name,
1196                    'called OKL_TRX_CONTRACTS_PUB.update_trx_contracts , return status: '
1197                 || l_return_status
1198                );
1199          END IF;
1200       END IF;
1201 
1202       -- rollback if error
1203       IF (l_return_status = g_ret_sts_unexp_error)
1204       THEN
1205          RAISE okl_api.g_exception_unexpected_error;
1206       ELSIF (l_return_status = g_ret_sts_error)
1207       THEN
1208          RAISE okl_api.g_exception_error;
1209       END IF;
1210 
1211       -- set the return values
1212       x_return_status := l_return_status;
1213       x_id := lx_tcnv_rec.ID;
1214 
1215       IF (is_debug_procedure_on)
1216       THEN
1217          okl_debug_pub.log_debug (g_level_procedure,
1218                                   l_module_name,
1219                                   'End(-)'
1220                                  );
1221       END IF;
1222    EXCEPTION
1223       WHEN okl_api.g_exception_error
1224       THEN
1225          ROLLBACK TO process_transaction;
1226          x_return_status := g_ret_sts_error;
1227 
1228          IF (is_debug_exception_on)
1229          THEN
1230             okl_debug_pub.log_debug (g_level_exception,
1231                                      l_module_name,
1232                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
1233                                     );
1234          END IF;
1235       WHEN okl_api.g_exception_unexpected_error
1236       THEN
1237          ROLLBACK TO process_transaction;
1238          x_return_status := g_ret_sts_unexp_error;
1239 
1240          IF (is_debug_exception_on)
1241          THEN
1242             okl_debug_pub.log_debug (g_level_exception,
1243                                      l_module_name,
1244                                         'EXCEPTION :'
1245                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
1246                                     );
1247          END IF;
1248       WHEN OTHERS
1249       THEN
1250          ROLLBACK TO process_transaction;
1251          x_return_status := g_ret_sts_unexp_error;
1252          -- Set the oracle error message
1253          okl_api.set_message (p_app_name          => g_app_name_1,
1254                               p_msg_name          => g_unexpected_error,
1255                               p_token1            => g_sqlcode_token,
1256                               p_token1_value      => SQLCODE,
1257                               p_token2            => g_sqlerrm_token,
1258                               p_token2_value      => SQLERRM
1259                              );
1260 
1261          IF (is_debug_exception_on)
1262          THEN
1263             okl_debug_pub.log_debug (g_level_exception,
1264                                      l_module_name,
1265                                         'EXCEPTION :'
1266                                      || 'OTHERS, SQLCODE: '
1267                                      || SQLCODE
1268                                      || ' , SQLERRM : '
1269                                      || SQLERRM
1270                                     );
1271          END IF;
1272    END process_transaction;
1273 
1274    -- Start of comments
1275    --
1276    -- Procedure Name : get_lines
1277    -- Desciption     : For the Termination quote get the contract lines
1278    -- Business Rules :
1279    -- Parameters     :
1280    -- Version        : 1.0
1281    -- History        : RMUNJULU -- Bug # 2484327 16-DEC-02 set additional values
1282    --                  for lines rec type
1283    --
1284    -- End of comments
1285    PROCEDURE get_lines (
1286       p_term_rec        IN              term_rec_type,
1287       x_klev_tbl        OUT NOCOPY      klev_tbl_type,
1288       x_return_status   OUT NOCOPY      VARCHAR2
1289    )
1290    IS
1291       -- Get the lines for the quote
1292       -- RMUNJULU -- Bug # 2484327 16-DEC-02 Added columns to query
1293       CURSOR get_qte_lines_csr (
1294          p_qte_id   IN   NUMBER
1295       )
1296       IS
1297          SELECT kle.ID kle_id,
1298                 kle.NAME asset_name,
1299                 tql.asset_quantity asset_quantity,
1300                 tql.ID tql_id,
1301                 tql.quote_quantity quote_quantity,
1302                 tql.split_kle_name split_kle_name          -- RMUNJULU 2757312
1303            FROM okl_txl_quote_lines_v tql,
1304                 okc_k_lines_v kle
1305           WHERE tql.qte_id = p_qte_id
1306             AND tql.qlt_code = 'AMCFIA'
1307             AND tql.kle_id = kle.ID;
1308 
1309       lx_klev_tbl             klev_tbl_type;
1310       i                       NUMBER         := 1;
1311       l_return_status         VARCHAR2 (1)   := g_ret_sts_success;
1312       l_module_name           VARCHAR2 (500) := g_module_name || 'get_lines';
1313       is_debug_exception_on   BOOLEAN
1314               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
1315       is_debug_procedure_on   BOOLEAN
1316               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
1317       is_debug_statement_on   BOOLEAN
1318               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
1319    BEGIN
1320       IF (is_debug_procedure_on)
1321       THEN
1322          okl_debug_pub.log_debug (g_level_procedure,
1323                                   l_module_name,
1324                                   'Begin(+)'
1325                                  );
1326       END IF;
1327 
1328       IF (is_debug_statement_on)
1329       THEN
1330          okl_debug_pub.log_debug (g_level_statement,
1331                                   l_module_name,
1332                                      'In param, p_term_rec.p_quote_id: '
1333                                   || p_term_rec.p_quote_id
1334                                  );
1335       END IF;
1336 
1337       i := 1;
1338 
1339       -- Set the klev_tbl
1340       FOR get_qte_lines_rec IN get_qte_lines_csr (p_term_rec.p_quote_id)
1341       LOOP
1342          lx_klev_tbl (i).p_kle_id := get_qte_lines_rec.kle_id;
1343          lx_klev_tbl (i).p_asset_name := get_qte_lines_rec.asset_name;
1344          -- RMUNJULU -- Bug # 2484327 16-DEC-02 -- START --
1345          -- set additional values for lines
1346          lx_klev_tbl (i).p_asset_quantity := get_qte_lines_rec.asset_quantity;
1347          lx_klev_tbl (i).p_tql_id := get_qte_lines_rec.tql_id;
1348          lx_klev_tbl (i).p_quote_quantity := get_qte_lines_rec.quote_quantity;
1349          -- RMUNJULU -- Bug # 2484327 16-DEC-02 -- END --
1350 
1351          -- RMUNJULU 2757312
1352          lx_klev_tbl (i).p_split_kle_name := get_qte_lines_rec.split_kle_name;
1353          i := i + 1;
1354       END LOOP;
1355 
1356       -- Set the return status
1357       x_return_status := l_return_status;
1358       x_klev_tbl := lx_klev_tbl;
1359 
1360       IF (is_debug_procedure_on)
1361       THEN
1362          okl_debug_pub.log_debug (g_level_procedure,
1363                                   l_module_name,
1364                                   'End(-)'
1365                                  );
1366       END IF;
1367    EXCEPTION
1368       WHEN OTHERS
1369       THEN
1370          -- RMUNJULU -- Bug # 2484327 Added code to close cursor if open
1371          IF get_qte_lines_csr%ISOPEN
1372          THEN
1373             CLOSE get_qte_lines_csr;
1374          END IF;
1375 
1376          -- Set the oracle error message
1377          okl_api.set_message (p_app_name          => g_app_name_1,
1378                               p_msg_name          => g_unexpected_error,
1379                               p_token1            => g_sqlcode_token,
1380                               p_token1_value      => SQLCODE,
1381                               p_token2            => g_sqlerrm_token,
1382                               p_token2_value      => SQLERRM
1383                              );
1384          -- Set the return status
1385          x_return_status := g_ret_sts_unexp_error;
1386 
1387          IF (is_debug_exception_on)
1388          THEN
1389             okl_debug_pub.log_debug (g_level_exception,
1390                                      l_module_name,
1391                                         'EXCEPTION :'
1392                                      || 'OTHERS, SQLCODE: '
1393                                      || SQLCODE
1394                                      || ' , SQLERRM : '
1395                                      || SQLERRM
1396                                     );
1397          END IF;
1398    END get_lines;
1399 
1400    -- Start of comments
1401    --
1402    -- Procedure Name : validate_contract_and_lines
1403    -- Desciption     : Validates the contract and lines to check they have right
1404    --                  statuses (ste_code and sts_code).
1405    -- Business Rules :
1406    -- Parameters   :
1407    -- Version    : 1.0
1408    -- History        : RMUNJULU 20-DEC-02 2484327 Added message when error
1409    --                : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
1410    --
1411    -- End of comments
1412    PROCEDURE validate_contract_and_lines (
1413       p_term_rec        IN              term_rec_type,
1414       p_sys_date        IN              DATE,
1415       p_klev_tbl        IN              klev_tbl_type,
1416       x_return_status   OUT NOCOPY      VARCHAR2
1417    )
1418    IS
1419       -- Get the status of the contract
1420       -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
1421       CURSOR chr_sts_csr (
1422          p_khr_id   IN   NUMBER
1423       )
1424       IS
1425          SELECT khr.sts_code sts_code
1426            FROM okc_k_headers_v khr
1427           WHERE khr.ID = p_khr_id;
1428 
1429       -- Get the status of the line
1430       -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_LNS_FULL
1431       CURSOR cle_sts_csr (
1432          p_kle_id   IN   NUMBER
1433       )
1434       IS
1435          SELECT kle.sts_code sts_code
1436            FROM okc_k_lines_v kle
1437           WHERE kle.ID = p_kle_id;
1438 
1439       i                       NUMBER         := 1;
1440       l_k_sts                 VARCHAR2 (30);
1441       l_l_sts                 VARCHAR2 (30);
1442       l_sts_match             VARCHAR2 (1)   := g_yes;
1443       l_return_status         VARCHAR2 (1)   := g_ret_sts_success;
1444       l_module_name           VARCHAR2 (500)
1445                              := g_module_name || 'validate_contract_and_lines';
1446       is_debug_exception_on   BOOLEAN
1447               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
1448       is_debug_procedure_on   BOOLEAN
1449               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
1450       is_debug_statement_on   BOOLEAN
1451               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
1452    BEGIN
1453       IF (is_debug_procedure_on)
1454       THEN
1455          okl_debug_pub.log_debug (g_level_procedure,
1456                                   l_module_name,
1457                                   'Begin(+)'
1458                                  );
1459       END IF;
1460 
1461       IF (is_debug_statement_on)
1462       THEN
1463          okl_debug_pub.log_debug (g_level_statement,
1464                                   l_module_name,
1465                                   'In param, p_sys_date: ' || p_sys_date
1466                                  );
1467          okl_debug_pub.log_debug (g_level_statement,
1468                                   l_module_name,
1469                                      'In param, p_term_rec.p_contract_id: '
1470                                   || p_term_rec.p_contract_id
1471                                  );
1472          okl_debug_pub.log_debug
1473                                 (g_level_statement,
1474                                  l_module_name,
1475                                     'In param, p_term_rec.p_contract_number: '
1476                                  || p_term_rec.p_contract_number
1477                                 );
1478 
1479          IF (p_klev_tbl.COUNT > 0)
1480          THEN
1481             i := p_klev_tbl.FIRST;
1482 
1483             LOOP
1484                okl_debug_pub.log_debug (g_level_statement,
1485                                         l_module_name,
1486                                            'In param, p_klev_tbl('
1487                                         || i
1488                                         || ').p_kle_id: '
1489                                         || p_klev_tbl (i).p_kle_id
1490                                        );
1491                EXIT WHEN (i = p_klev_tbl.LAST);
1492                i := p_klev_tbl.NEXT (i);
1493             END LOOP;
1494          END IF;
1495       END IF;
1496 
1497       -- Get the contract status
1498       OPEN chr_sts_csr (p_term_rec.p_contract_id);
1499 
1500       FETCH chr_sts_csr
1501        INTO l_k_sts;
1502 
1503       CLOSE chr_sts_csr;
1504 
1505       -- RMUNJULU 3018641 Step Message
1506       -- Step : Validate Contract
1507       okl_api.set_message (p_app_name      => g_app_name,
1508                            p_msg_name      => 'OKL_AM_STEP_VAL');
1509 
1510       -- While looping thru the lines get the line status and compare with
1511       -- contract status. If both the statuses doesnot match then error.
1512       IF (p_klev_tbl.COUNT > 0)
1513       THEN
1514          i := p_klev_tbl.FIRST;
1515 
1516          LOOP
1517             -- Get the line status
1518             OPEN cle_sts_csr (p_klev_tbl (i).p_kle_id);
1519 
1520             FETCH cle_sts_csr
1521              INTO l_l_sts;
1522 
1523             CLOSE cle_sts_csr;
1524 
1525             -- Check if both statuses match
1526             IF l_k_sts <> l_l_sts
1527             THEN
1528                l_sts_match := g_no;
1529                EXIT;
1530             END IF;
1531 
1532             EXIT WHEN (i = p_klev_tbl.LAST);
1533             i := p_klev_tbl.NEXT (i);
1534          END LOOP;
1535       END IF;
1536 
1537       -- If statuses do not match set return status
1538       IF l_sts_match <> g_yes
1539       THEN
1540          -- Validation of contract and/or lines failed for contract CONTRACT_NUMBER.
1541          okl_api.set_message (p_app_name          => g_app_name,
1542                               p_msg_name          => 'OKL_AM_VAL_K_LNS_FAILED',
1543                               p_token1            => 'CONTRACT_NUMBER',
1544                               p_token1_value      => p_term_rec.p_contract_number
1545                              );
1546          l_return_status := g_ret_sts_error;
1547       END IF;
1548 
1549       -- Set the return status
1550       x_return_status := l_return_status;
1551 
1552       IF (is_debug_procedure_on)
1553       THEN
1554          okl_debug_pub.log_debug (g_level_procedure,
1555                                   l_module_name,
1556                                   'End(-)'
1557                                  );
1558       END IF;
1559    EXCEPTION
1560       WHEN OTHERS
1561       THEN
1562          -- Close any cursors which are open
1563          IF chr_sts_csr%ISOPEN
1564          THEN
1565             CLOSE chr_sts_csr;
1566          END IF;
1567 
1568          IF cle_sts_csr%ISOPEN
1569          THEN
1570             CLOSE cle_sts_csr;
1571          END IF;
1572 
1573          -- Set the oracle error message
1574          okl_api.set_message (p_app_name          => g_app_name_1,
1575                               p_msg_name          => g_unexpected_error,
1576                               p_token1            => g_sqlcode_token,
1577                               p_token1_value      => SQLCODE,
1578                               p_token2            => g_sqlerrm_token,
1579                               p_token2_value      => SQLERRM
1580                              );
1581          -- Set the return status
1582          x_return_status := g_ret_sts_unexp_error;
1583 
1584          IF (is_debug_exception_on)
1585          THEN
1586             okl_debug_pub.log_debug (g_level_exception,
1587                                      l_module_name,
1588                                         'EXCEPTION :'
1589                                      || 'OTHERS, SQLCODE: '
1590                                      || SQLCODE
1591                                      || ' , SQLERRM : '
1592                                      || SQLERRM
1593                                     );
1594          END IF;
1595    END validate_contract_and_lines;
1596 
1597    -- Start of comments
1598    --
1599    -- Procedure Name  : do_split_asset_trn
1600    -- Description     : This procedure Creates the Split Trn and Updates the Split TRN if needed
1601    -- Business Rules  :
1602    -- Parameters      :
1603    -- Version         : 1.0
1604    -- History         : RMUNJULU 2757312 Added this proc
1605    --                   This proc will use tmt_generic_flag1_yn to maintain status of
1606    --                   whether the split trn was created and updated successfully or not
1607    --                 : rmunjulu EDAT Added code to get quote eff dates and call new overloaded split asset API
1608    -- End of comments
1609    PROCEDURE do_split_asset_trn (
1610       p_term_rec          IN              term_rec_type,
1611       p_sys_date          IN              DATE,
1612       p_trn_already_set   IN              VARCHAR2,
1613       px_overall_status   IN OUT NOCOPY   VARCHAR2,
1614       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
1615       px_klev_tbl         IN OUT NOCOPY   klev_tbl_type,
1616       x_return_status     OUT NOCOPY      VARCHAR2
1617    )
1618    IS
1619       -- SECHAWLA 23-DEC-02 2484327 Added cursors
1620       -- Get the count of IB lines for the quote_line_id (TQL_ID )
1621       CURSOR get_ib_lines_cnt_csr (
1622          p_tql_id   IN   NUMBER
1623       )
1624       IS
1625          SELECT COUNT (txd.ID) ib_lines_count
1626            FROM okl_txd_quote_line_dtls txd
1627           WHERE txd.tql_id = p_tql_id;
1628 
1629       -- Get the IB Lines for the quote_line_id (tql_id)
1630       CURSOR get_ib_lines_csr (
1631          p_tql_id   IN   NUMBER
1632       )
1633       IS
1634          SELECT txd.kle_id
1635            FROM okl_txd_quote_line_dtls txd
1636           WHERE txd.tql_id = p_tql_id;
1637 
1638       lx_txdv_tbl              okl_txd_assets_pub.adpv_tbl_type;
1639       lx_txlv_rec              okl_txl_assets_pub.tlpv_rec_type;
1640       lx_trxv_rec              okl_trx_assets_pub.thpv_rec_type;
1641       lx_cle_tbl               okl_split_asset_pvt.cle_tbl_type;
1642       lx_sno_yn                VARCHAR2 (3)                        := g_false;
1643       lx_clev_tbl              okl_okc_migration_pvt.clev_tbl_type;
1644       lp_ib_tbl                okl_split_asset_pvt.ib_tbl_type;
1645       lp_empty_ib_tbl          okl_split_asset_pvt.ib_tbl_type;
1646       l_api_name      CONSTANT VARCHAR2 (30)           := 'do_split_asset_trn';
1647       l_return_status          VARCHAR2 (1)               := g_ret_sts_success;
1648       cle_index                NUMBER                              := 0;
1649       i                        NUMBER                              := 0;
1650       ib_id                    NUMBER                              := 0;
1651       l_module_name            VARCHAR2 (500)
1652                                       := g_module_name || 'do_split_asset_trn';
1653       is_debug_exception_on    BOOLEAN
1654               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
1655       is_debug_procedure_on    BOOLEAN
1656               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
1657       is_debug_statement_on    BOOLEAN
1658               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
1659       --SECHAWLA 14-JAN-03 2748110 : New Declarations
1660       ib_line_id               NUMBER                              := 0;
1661       id_exists                VARCHAR2 (1);
1662       -- SECHAWLA 23-DEC-02 2484327 Added variable
1663       l_ib_lines_count         NUMBER;
1664       l_api_version   CONSTANT NUMBER                         := g_api_version;
1665       l_msg_count              NUMBER                            := g_miss_num;
1666       l_msg_data               VARCHAR2 (2000);
1667       lxx_txdv_tbl             okl_txd_assets_pub.adpv_tbl_type;
1668       lx_txd_assets_rec        okl_txd_assets_pub.adpv_rec_type;
1669       lx_txdv_rec              okl_txd_assets_pub.adpv_rec_type;
1670       j                        NUMBER;
1671       -- rmunjulu EDAT
1672       l_quote_accpt_date       DATE;
1673       l_quote_eff_date         DATE;
1674    BEGIN
1675       SAVEPOINT do_split_asset_trn;
1676 
1677       IF (is_debug_procedure_on)
1678       THEN
1679          okl_debug_pub.log_debug (g_level_procedure,
1680                                   l_module_name,
1681                                   'Begin(+)'
1682                                  );
1683       END IF;
1684 
1685       IF (is_debug_statement_on)
1686       THEN
1687          okl_debug_pub.log_debug (g_level_statement,
1688                                   l_module_name,
1689                                      'In param, p_trn_already_set: '
1690                                   || p_trn_already_set
1691                                  );
1692          okl_debug_pub.log_debug (g_level_statement,
1693                                   l_module_name,
1694                                      'In param, px_overall_status: '
1695                                   || px_overall_status
1696                                  );
1697          okl_debug_pub.log_debug (g_level_statement,
1698                                   l_module_name,
1699                                   'In param, p_sys_date: ' || p_sys_date
1700                                  );
1701          okl_debug_pub.log_debug
1702                             (g_level_statement,
1703                              l_module_name,
1704                                 'In param, px_tcnv_rec.tmt_generic_flag1_yn: '
1705                              || px_tcnv_rec.tmt_generic_flag1_yn
1706                             );
1707 
1708          IF px_klev_tbl.COUNT > 0
1709          THEN
1710             FOR i IN px_klev_tbl.FIRST .. px_klev_tbl.LAST
1711             LOOP
1712                IF (px_klev_tbl.EXISTS (i))
1713                THEN
1714                   okl_debug_pub.log_debug (g_level_statement,
1715                                            l_module_name,
1716                                               'In param, px_klev_tbl('
1717                                            || i
1718                                            || ').p_kle_id: '
1719                                            || px_klev_tbl (i).p_kle_id
1720                                           );
1721                   okl_debug_pub.log_debug (g_level_statement,
1722                                            l_module_name,
1723                                               'In param, px_klev_tbl('
1724                                            || i
1725                                            || ').p_asset_quantity: '
1726                                            || px_klev_tbl (i).p_asset_quantity
1727                                           );
1728                   okl_debug_pub.log_debug (g_level_statement,
1729                                            l_module_name,
1730                                               'In param, px_klev_tbl('
1731                                            || i
1732                                            || ').p_asset_name: '
1733                                            || px_klev_tbl (i).p_asset_name
1734                                           );
1735                   okl_debug_pub.log_debug (g_level_statement,
1736                                            l_module_name,
1737                                               'In param, px_klev_tbl('
1738                                            || i
1739                                            || ').p_quote_quantity: '
1740                                            || px_klev_tbl (i).p_quote_quantity
1741                                           );
1742                   okl_debug_pub.log_debug (g_level_statement,
1743                                            l_module_name,
1744                                               'In param, px_klev_tbl('
1745                                            || i
1746                                            || ').p_tql_id: '
1747                                            || px_klev_tbl (i).p_tql_id
1748                                           );
1749                   okl_debug_pub.log_debug (g_level_statement,
1750                                            l_module_name,
1751                                               'In param, px_klev_tbl('
1752                                            || i
1753                                            || ').p_split_kle_name: '
1754                                            || px_klev_tbl (i).p_split_kle_name
1755                                           );
1756                END IF;
1757             END LOOP;
1758          END IF;
1759       END IF;
1760 
1761       x_return_status := okl_api.g_ret_sts_success;
1762 
1763       IF    (    p_trn_already_set = g_yes
1764              AND NVL (px_tcnv_rec.tmt_generic_flag1_yn, '?') <> g_yes
1765             )
1766          OR (p_trn_already_set = g_no)
1767       THEN
1768          -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
1769 
1770          -- rmunjulu EDAT
1771          -- If quote exists then accnting date is quote accept date else sysdate
1772          IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
1773          THEN
1774             l_quote_accpt_date :=
1775                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
1776             l_quote_eff_date :=
1777                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
1778          ELSE
1779             l_quote_accpt_date := p_sys_date;
1780             l_quote_eff_date := p_sys_date;
1781          END IF;
1782 
1783          -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
1784          IF px_klev_tbl.COUNT > 0
1785          THEN
1786             FOR i IN px_klev_tbl.FIRST .. px_klev_tbl.LAST
1787             LOOP
1788                IF    px_klev_tbl (i).p_kle_id IS NULL
1789                   OR px_klev_tbl (i).p_kle_id = g_miss_num
1790                THEN
1791                   -- kle id parameter is null
1792                   okl_api.set_message (p_app_name          => g_app_name_1,
1793                                        p_msg_name          => g_required_value,
1794                                        p_token1            => g_col_name_token,
1795                                        p_token1_value      => 'Kle Id'
1796                                       );
1797                   RAISE okl_api.g_exception_error;
1798                END IF;
1799 
1800                IF    px_klev_tbl (i).p_asset_quantity IS NULL
1801                   OR px_klev_tbl (i).p_asset_quantity = okl_api.g_miss_num
1802                THEN
1803                   -- Asset Quantity parameter is null
1804                   okl_api.set_message (p_app_name          => g_app_name_1,
1805                                        p_msg_name          => g_required_value,
1806                                        p_token1            => g_col_name_token,
1807                                        p_token1_value      => 'Asset Quantity'
1808                                       );
1809                   RAISE okl_api.g_exception_error;
1810                END IF;
1811 
1812                IF    px_klev_tbl (i).p_asset_name IS NULL
1813                   OR px_klev_tbl (i).p_asset_name = okl_api.g_miss_char
1814                THEN
1815                   -- Asset Name parameter is null
1816                   okl_api.set_message (p_app_name          => g_app_name_1,
1817                                        p_msg_name          => g_required_value,
1818                                        p_token1            => g_col_name_token,
1819                                        p_token1_value      => 'Asset Name'
1820                                       );
1821                   RAISE okl_api.g_exception_error;
1822                END IF;
1823 
1824                IF    px_klev_tbl (i).p_quote_quantity IS NULL
1825                   OR px_klev_tbl (i).p_quote_quantity = okl_api.g_miss_num
1826                THEN
1827                   -- Quote Quantity parameter is null
1828                   okl_api.set_message (p_app_name          => g_app_name_1,
1829                                        p_msg_name          => g_required_value,
1830                                        p_token1            => g_col_name_token,
1831                                        p_token1_value      => 'Quote Quantity'
1832                                       );
1833                   RAISE okl_api.g_exception_error;
1834                END IF;
1835 
1836                IF    px_klev_tbl (i).p_tql_id IS NULL
1837                   OR px_klev_tbl (i).p_tql_id = okl_api.g_miss_num
1838                THEN
1839                   -- quote line id parameter is null
1840                   okl_api.set_message (p_app_name          => g_app_name_1,
1841                                        p_msg_name          => g_required_value,
1842                                        p_token1            => g_col_name_token,
1843                                        p_token1_value      => 'Quote Line ID'
1844                                       );
1845                   RAISE okl_api.g_exception_error;
1846                END IF;
1847 
1848                -- Check if the IB instances for this asset are serialized
1849                -- also get the IB instances which are serialized
1850                IF (is_debug_statement_on)
1851                THEN
1852                   okl_debug_pub.log_debug
1853                            (g_level_statement,
1854                             l_module_name,
1855                             'calling OKL_AM_TERMNT_QUOTE_PVT.check_asset_sno'
1856                            );
1857                END IF;
1858 
1859                x_return_status :=
1860                   okl_am_termnt_quote_pvt.check_asset_sno
1861                                      (p_asset_line      => px_klev_tbl (i).p_kle_id,
1862                                       x_sno_yn          => lx_sno_yn,
1863                                       x_clev_tbl        => lx_clev_tbl
1864                                      );
1865 
1866                IF (is_debug_statement_on)
1867                THEN
1868                   okl_debug_pub.log_debug
1869                      (g_level_statement,
1870                       l_module_name,
1871                          'called OKL_AM_TERMNT_QUOTE_PVT.check_asset_sno , return status: '
1872                       || x_return_status
1873                      );
1874                END IF;
1875 
1876                IF (x_return_status = g_ret_sts_unexp_error)
1877                THEN
1878                   RAISE okl_api.g_exception_unexpected_error;
1879                ELSIF (x_return_status = g_ret_sts_error)
1880                THEN
1881                   RAISE okl_api.g_exception_error;
1882                END IF;
1883 
1884                -- If Asset serialized
1885                IF lx_sno_yn = g_true
1886                THEN
1887                   -- SECHAWLA 23-DEC-02 2484327 Changed processing to get the correct
1888                   -- IB lines for the quote
1889 
1890                   -- Get the IB line count
1891                   OPEN get_ib_lines_cnt_csr (px_klev_tbl (i).p_tql_id);
1892 
1893                   FETCH get_ib_lines_cnt_csr
1894                    INTO l_ib_lines_count;
1895 
1896                   CLOSE get_ib_lines_cnt_csr;
1897 
1898                   -- If IB line count does not match Quote Qty raise msg and exp
1899                   IF l_ib_lines_count <> px_klev_tbl (i).p_quote_quantity
1900                   THEN
1901                      -- Asset ASSET_NUMBER is serialized. Quote quantity
1902                      -- QUOTE_QUANTITY does not match the number of selected asset
1903                      -- units ASSET_UNITS.
1904                      okl_api.set_message
1905                           (p_app_name          => 'OKL',
1906                            p_msg_name          => 'OKL_AM_QTE_QTY_SRL_CNT_ERR',
1907                            p_token1            => 'ASSET_NUMBER',
1908                            p_token1_value      => px_klev_tbl (i).p_asset_name,
1909                            p_token2            => 'QUOTE_QUANTITY',
1910                            p_token2_value      => px_klev_tbl (i).p_quote_quantity,
1911                            p_token3            => 'ASSET_UNITS',
1912                            p_token3_value      => l_ib_lines_count
1913                           );
1914                      RAISE okl_api.g_exception_error;
1915                   END IF;
1916 
1917                   --SECHAWLA 14-JAN-03 2748110 : Modified the logic to send those ib
1918                   -- line ids that do not exist in
1919                   --OKL_TXD_QUOTE_LINE_DTLS_V, to create_split_transaction procedure.
1920                   -- This change follows the change
1921                   -- in p_split_into_units quantity, on 08-JAN-03, to send
1922                   -- asset quantity minus quote quantity as split
1923                   -- into units.
1924                   IF lx_clev_tbl.COUNT > 0
1925                   THEN
1926                      ib_line_id := lx_clev_tbl.FIRST;
1927                      ib_id := 1;
1928 
1929                      LOOP
1930                         id_exists := 'F';
1931 
1932                         -- Populate the input table of IB line IDs to create split transaction
1933                         -- procedure with rows from okl_txd_quote_line_dtls table
1934                         FOR get_ib_lines_rec IN
1935                            get_ib_lines_csr (px_klev_tbl (i).p_tql_id)
1936                         LOOP
1937                            IF lx_clev_tbl (ib_line_id).ID =
1938                                                       get_ib_lines_rec.kle_id
1939                            THEN
1940                               id_exists := 'T';
1941                               EXIT;
1942                            END IF;
1943                         END LOOP;
1944 
1945                         IF id_exists = 'F'
1946                         THEN
1947                            lp_ib_tbl (ib_id).ID :=
1948                                                   lx_clev_tbl (ib_line_id).ID;
1949                            ib_id := ib_id + 1;
1950                         END IF;
1951 
1952                         EXIT WHEN (ib_line_id = lx_clev_tbl.LAST);
1953                         ib_line_id := lx_clev_tbl.NEXT (ib_line_id);
1954                      END LOOP;
1955                   ELSE
1956                      -- IB line ids not found
1957                      okl_api.set_message (p_app_name          => g_app_name_1,
1958                                           p_msg_name          => g_required_value,
1959                                           p_token1            => g_col_name_token,
1960                                           p_token1_value      => 'IB Line IDs'
1961                                          );
1962                      RAISE okl_api.g_exception_error;
1963                   END IF;
1964                END IF;
1965 
1966                -- Create the split asset transactions
1967                -- rmunjulu EDAT Call the new signature of split asset which takes trx date
1968                IF (is_debug_statement_on)
1969                THEN
1970                   okl_debug_pub.log_debug
1971                       (g_level_statement,
1972                        l_module_name,
1973                        'calling OKL_SPLIT_ASSET_PUB.create_split_transaction'
1974                       );
1975                END IF;
1976 
1977                okl_split_asset_pub.create_split_transaction
1978                   (p_api_version                    => l_api_version,
1979                    p_init_msg_list                  => g_false,
1980                    x_return_status                  => x_return_status,
1981                    x_msg_count                      => l_msg_count,
1982                    x_msg_data                       => l_msg_data,
1983                    p_cle_id                         => px_klev_tbl (i).p_kle_id,
1984                    p_split_into_individuals_yn      => 'N',
1985                                           -- RMUNJULU Changed was NULL earlier
1986                    p_split_into_units               =>   px_klev_tbl (i).p_asset_quantity
1987                                                        - px_klev_tbl (i).p_quote_quantity,
1988                    p_ib_tbl                         => lp_ib_tbl,
1989                    p_trx_date                       => l_quote_eff_date,
1990                                                               -- rmunjulu EDAT
1991                    x_txdv_tbl                       => lx_txdv_tbl,
1992                                                                  --okl_asd_pvt
1993                    x_txlv_rec                       => lx_txlv_rec,
1994                    x_trxv_rec                       => lx_trxv_rec
1995                   );
1996 
1997                IF (is_debug_statement_on)
1998                THEN
1999                   okl_debug_pub.log_debug
2000                      (g_level_statement,
2001                       l_module_name,
2002                          'called OKL_SPLIT_ASSET_PUB.create_split_transaction , return status: '
2003                       || x_return_status
2004                      );
2005                END IF;
2006 
2007                IF (x_return_status = g_ret_sts_unexp_error)
2008                THEN
2009                   RAISE okl_api.g_exception_unexpected_error;
2010                ELSIF (x_return_status = g_ret_sts_error)
2011                THEN
2012                   RAISE okl_api.g_exception_error;
2013                END IF;
2014 
2015                -- get the transaction which needs to be updated
2016                IF     px_klev_tbl (i).p_split_kle_name IS NOT NULL
2017                   AND px_klev_tbl (i).p_split_kle_name <> g_miss_char
2018                THEN
2019                   IF lx_txdv_tbl.COUNT > 0
2020                   THEN
2021                      -- Update the TXDV rec which has no target_kle_id ie which is the
2022                      -- new asset created
2023                      FOR j IN lx_txdv_tbl.FIRST .. lx_txdv_tbl.LAST
2024                      LOOP
2025                         IF lx_txdv_tbl (j).target_kle_id IS NULL
2026                         THEN
2027                            lx_txd_assets_rec.asset_number :=
2028                                              px_klev_tbl (i).p_split_kle_name;
2029                            lx_txd_assets_rec.ID := lx_txdv_tbl (j).ID;
2030 
2031                            IF (is_debug_statement_on)
2032                            THEN
2033                               okl_debug_pub.log_debug
2034                                  (g_level_statement,
2035                                   l_module_name,
2036                                   'calling OKL_TXD_ASSETS_PUB.update_txd_asset_def'
2037                                  );
2038                            END IF;
2039 
2040                            okl_txd_assets_pub.update_txd_asset_def
2041                                           (p_api_version        => l_api_version,
2042                                            p_init_msg_list      => g_false,
2043                                            x_return_status      => x_return_status,
2044                                            x_msg_count          => l_msg_count,
2045                                            x_msg_data           => l_msg_data,
2046                                            p_adpv_rec           => lx_txd_assets_rec,
2047                                            x_adpv_rec           => lx_txdv_rec
2048                                           );
2049 
2050                            IF (is_debug_statement_on)
2051                            THEN
2052                               okl_debug_pub.log_debug
2053                                  (g_level_statement,
2054                                   l_module_name,
2055                                      'called OKL_TXD_ASSETS_PUB.update_txd_asset_def , return status: '
2056                                   || x_return_status
2057                                  );
2058                            END IF;
2059 
2060                            IF (x_return_status = g_ret_sts_unexp_error)
2061                            THEN
2062                               RAISE okl_api.g_exception_unexpected_error;
2063                            ELSIF (x_return_status = g_ret_sts_error)
2064                            THEN
2065                               RAISE okl_api.g_exception_error;
2066                            END IF;
2067                         END IF;
2068                      END LOOP;
2069                   ELSE
2070                      -- Create Split Trn did not work properly
2071                      okl_api.set_message
2072                                        (p_app_name          => g_app_name_1,
2073                                         p_msg_name          => g_invalid_value,
2074                                         p_token1            => g_col_name_token,
2075                                         p_token1_value      => 'lx_txdv_tbl.COUNT'
2076                                        );
2077                      RAISE okl_api.g_exception_error;
2078                   END IF;
2079                END IF;
2080             END LOOP;
2081          END IF;
2082 
2083          -- store the highest degree of error
2084          set_overall_status (p_return_status        => x_return_status,
2085                              px_overall_status      => px_overall_status);
2086 
2087          IF (is_debug_statement_on)
2088          THEN
2089             okl_debug_pub.log_debug
2090                             (g_level_statement,
2091                              l_module_name,
2092                                 'called set_overall_status , return status: '
2093                              || x_return_status
2094                             );
2095             okl_debug_pub.log_debug (g_level_statement,
2096                                      l_module_name,
2097                                      'px_overall_status: '
2098                                      || px_overall_status
2099                                     );
2100          END IF;
2101 
2102          -- set the transaction record
2103          set_transaction_rec (p_return_status       => x_return_status,
2104                               p_overall_status      => px_overall_status,
2105                               p_tmt_flag            => 'TMT_GENERIC_FLAG1_YN',
2106                               p_tsu_code            => 'WORKING',
2107                               px_tcnv_rec           => px_tcnv_rec
2108                              );
2109 
2110          IF (is_debug_statement_on)
2111          THEN
2112             okl_debug_pub.log_debug
2113                            (g_level_statement,
2114                             l_module_name,
2115                                'called set_transaction_rec , return status: '
2116                             || x_return_status
2117                            );
2118             okl_debug_pub.log_debug (g_level_statement,
2119                                      l_module_name,
2120                                      'px_overall_status: '
2121                                      || px_overall_status
2122                                     );
2123          END IF;
2124       END IF;
2125 
2126       IF (is_debug_procedure_on)
2127       THEN
2128          okl_debug_pub.log_debug (g_level_procedure,
2129                                   l_module_name,
2130                                   'End(-)'
2131                                  );
2132       END IF;
2133    EXCEPTION
2134       WHEN okl_api.g_exception_error
2135       THEN
2136          -- SECHAWLA 23-DEC-02 2484327 Closed cursors if open
2137          IF get_ib_lines_cnt_csr%ISOPEN
2138          THEN
2139             CLOSE get_ib_lines_cnt_csr;
2140          END IF;
2141 
2142          IF get_ib_lines_csr%ISOPEN
2143          THEN
2144             CLOSE get_ib_lines_csr;
2145          END IF;
2146 
2147          ROLLBACK TO do_split_asset_trn;
2148          x_return_status := g_ret_sts_error;
2149          -- store the highest degree of error
2150          set_overall_status (p_return_status        => x_return_status,
2151                              px_overall_status      => px_overall_status);
2152          -- set the transaction record
2153          set_transaction_rec (p_return_status       => x_return_status,
2154                               p_overall_status      => px_overall_status,
2155                               p_tmt_flag            => 'TMT_GENERIC_FLAG1_YN',
2156                               p_tsu_code            => 'ERROR',
2157                               px_tcnv_rec           => px_tcnv_rec
2158                              );
2159 
2160          IF (is_debug_exception_on)
2161          THEN
2162             okl_debug_pub.log_debug (g_level_exception,
2163                                      l_module_name,
2164                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
2165                                     );
2166          END IF;
2167       WHEN okl_api.g_exception_unexpected_error
2168       THEN
2169          -- SECHAWLA 23-DEC-02 2484327 Closed cursors if open
2170          IF get_ib_lines_cnt_csr%ISOPEN
2171          THEN
2172             CLOSE get_ib_lines_cnt_csr;
2173          END IF;
2174 
2175          IF get_ib_lines_csr%ISOPEN
2176          THEN
2177             CLOSE get_ib_lines_csr;
2178          END IF;
2179 
2180          ROLLBACK TO do_split_asset_trn;
2181          x_return_status := g_ret_sts_unexp_error;
2182          -- store the highest degree of error
2183          set_overall_status (p_return_status        => x_return_status,
2184                              px_overall_status      => px_overall_status);
2185          -- set the transaction record
2186          set_transaction_rec (p_return_status       => x_return_status,
2187                               p_overall_status      => px_overall_status,
2188                               p_tmt_flag            => 'TMT_GENERIC_FLAG1_YN',
2189                               p_tsu_code            => 'ERROR',
2190                               px_tcnv_rec           => px_tcnv_rec
2191                              );
2192 
2193          IF (is_debug_exception_on)
2194          THEN
2195             okl_debug_pub.log_debug (g_level_exception,
2196                                      l_module_name,
2197                                         'EXCEPTION :'
2198                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
2199                                     );
2200          END IF;
2201       WHEN OTHERS
2202       THEN
2203          -- SECHAWLA 23-DEC-02 2484327 Closed cursors if open
2204          IF get_ib_lines_cnt_csr%ISOPEN
2205          THEN
2206             CLOSE get_ib_lines_cnt_csr;
2207          END IF;
2208 
2209          IF get_ib_lines_csr%ISOPEN
2210          THEN
2211             CLOSE get_ib_lines_csr;
2212          END IF;
2213 
2214          ROLLBACK TO do_split_asset_trn;
2215          x_return_status := g_ret_sts_unexp_error;
2216          -- Set the oracle error message
2217          okl_api.set_message (p_app_name          => g_app_name_1,
2218                               p_msg_name          => g_unexpected_error,
2219                               p_token1            => g_sqlcode_token,
2220                               p_token1_value      => SQLCODE,
2221                               p_token2            => g_sqlerrm_token,
2222                               p_token2_value      => SQLERRM
2223                              );
2224          -- store the highest degree of error
2225          set_overall_status (p_return_status        => x_return_status,
2226                              px_overall_status      => px_overall_status);
2227          -- set the transaction record
2228          set_transaction_rec (p_return_status       => x_return_status,
2229                               p_overall_status      => px_overall_status,
2230                               p_tmt_flag            => 'TMT_GENERIC_FLAG1_YN',
2231                               p_tsu_code            => 'ERROR',
2232                               px_tcnv_rec           => px_tcnv_rec
2233                              );
2234 
2235          IF (is_debug_exception_on)
2236          THEN
2237             okl_debug_pub.log_debug (g_level_exception,
2238                                      l_module_name,
2239                                         'EXCEPTION :'
2240                                      || 'OTHERS, SQLCODE: '
2241                                      || SQLCODE
2242                                      || ' , SQLERRM : '
2243                                      || SQLERRM
2244                                     );
2245          END IF;
2246    END do_split_asset_trn;
2247 
2248    -- Start of comments
2249    --
2250    -- Procedure Name  : do_split_asset
2251    -- Description     : This procedure splits a financial asset into two
2252    -- Business Rules  :
2253    -- Parameters      :  Input/Output parameters : px_klev_tbl
2254    --                    px_klev_tbl is a table of records of the following structure :
2255 
2256    --                    p_kle_id            : Original kle ID
2257    --                    p_asset_quantity    : Asset Quantity
2258    --                    p_asset_name        : Asset Number
2259    --                    p_quote_quantity    : Quantity to Split
2260    --                    p_tql_id            : Quote Line ID
2261    --                    p_split_kle_id      : Split (new) Kle Id
2262    --
2263    -- Version         : 1.0
2264    -- History         : SECHAWLA 16-DEC-02 Bug# 2484327 Created
2265    --                   SECHAWLA 23-DEC-02 2484327 Changed the way IB line ids are identified
2266    --                   SECHAWLA 08-JAN-03 2736865 Changed the logic to send (asset qty - quote qty)
2267    --                       in p_split_into_units parameter, instead of sending the quote qty
2268    --                   SECHAWLA 14-JAN-03 2748110 : Modified the logic to send
2269    --                       those ib line ids that do not exist in
2270    --                       OKL_TXD_QUOTE_LINE_DTLS_V, to create_split_transaction procedure
2271    --                   RMUNJULU 2757312 MAJOR CHANGES TO this proc
2272    --                   Added parameters, removed create split trn to do_split_trn
2273    --                   coded for two flags to maintain split asset status
2274    --                   This proc will use tmt_split_asset_yn
2275    --                   RMUNJULU 2757312 Added code to check that split trn is not processed
2276    --                   RMUNJULU ASSETNUM INPUT 2757312 Added code to make sure error from split
2277    --                   asset is becos of uniqueness failure
2278    --                   RMUNJULU 3241502 Call asset_number_exists from OKL_AM_CREATE_QUOTE_PVT
2279    --                   SECHAWLA Split Asset Enhancements (FPs) Addes a parameter p_source_call in call to
2280    --                            OKL_SPLIT_ASSET_PUB.split_fixed_asset
2281    -- End of comments
2282    PROCEDURE do_split_asset (
2283       p_term_rec          IN              term_rec_type,
2284                                                       --RMUNJULU 2757312 Added
2285       p_sys_date          IN              DATE,       --RMUNJULU 2757312 Added
2286       p_trn_already_set   IN              VARCHAR2,   --RMUNJULU 2757312 Added
2287       px_overall_status   IN OUT NOCOPY   VARCHAR2,   --RMUNJULU 2757312 Added
2288       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
2289                                                       --RMUNJULU 2757312 Added
2290       px_klev_tbl         IN OUT NOCOPY   klev_tbl_type,
2291       x_return_status     OUT NOCOPY      VARCHAR2
2292    )
2293    IS
2294       -- SECHAWLA 23-DEC-02 2484327 Added cursors
2295       -- Get the count of IB lines for the quote_line_id (TQL_ID )
2296       CURSOR get_ib_lines_cnt_csr (
2297          p_tql_id   IN   NUMBER
2298       )
2299       IS
2300          SELECT COUNT (txd.ID) ib_lines_count
2301            FROM okl_txd_quote_line_dtls txd
2302           WHERE txd.tql_id = p_tql_id;
2303 
2304       -- Get the IB Lines for the quote_line_id (tql_id)
2305       CURSOR get_ib_lines_csr (
2306          p_tql_id   IN   NUMBER
2307       )
2308       IS
2309          SELECT txd.kle_id
2310            FROM okl_txd_quote_line_dtls txd
2311           WHERE txd.tql_id = p_tql_id;
2312 
2313       lx_txdv_tbl                okl_txd_assets_pub.adpv_tbl_type;
2314       lx_txlv_rec                okl_txl_assets_pub.tlpv_rec_type;
2315       lx_trxv_rec                okl_trx_assets_pub.thpv_rec_type;
2316       lx_cle_tbl                 okl_split_asset_pvt.cle_tbl_type;
2317       lx_sno_yn                  VARCHAR2 (3)                       := g_false;
2318       lx_clev_tbl                okl_okc_migration_pvt.clev_tbl_type;
2319       lp_ib_tbl                  okl_split_asset_pvt.ib_tbl_type;
2320       lp_empty_ib_tbl            okl_split_asset_pvt.ib_tbl_type;
2321       l_api_name        CONSTANT VARCHAR2 (30)             := 'do_split_asset';
2322       l_return_status            VARCHAR2 (1)             := g_ret_sts_success;
2323       cle_index                  NUMBER                                := 0;
2324       i                          NUMBER                                := 0;
2325       ib_id                      NUMBER                                := 0;
2326       l_module_name              VARCHAR2 (500)
2327                                           := g_module_name || 'do_split_asset';
2328       is_debug_exception_on      BOOLEAN
2329               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
2330       is_debug_procedure_on      BOOLEAN
2331               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
2332       is_debug_statement_on      BOOLEAN
2333               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
2334       --SECHAWLA 14-JAN-03 2748110 : New Declarations
2335       ib_line_id                 NUMBER                                := 0;
2336       id_exists                  VARCHAR2 (1);
2337       -- SECHAWLA 23-DEC-02 2484327 Added variable
2338       l_ib_lines_count           NUMBER;
2339       l_api_version     CONSTANT NUMBER                       := g_api_version;
2340       l_msg_count                NUMBER                          := g_miss_num;
2341       l_msg_data                 VARCHAR2 (2000);
2342       -- RMUNJULU 2757312 added
2343       lp_tqlv_tbl                okl_txl_quote_lines_pub.tqlv_tbl_type;
2344       lx_tqlv_tbl                okl_txl_quote_lines_pub.tqlv_tbl_type;
2345       l_new_asset_name           VARCHAR2 (2000);
2346       l_new_asset_quantity       NUMBER;
2347 
2348       -- RMUNJULU 2757312 added
2349       -- Get the asset name
2350       CURSOR get_asset_name_csr (
2351          p_kle_id   IN   NUMBER
2352       )
2353       IS
2354          SELECT kle.NAME NAME
2355            FROM okc_k_lines_v kle
2356           WHERE kle.ID = p_kle_id;
2357 
2358       -- RMUNJULU 2757312 added
2359       -- Get the ENTERED split trn
2360       -- RMUNJULU ASSETNUM INPUT 2757312
2361       -- Added txl.id to select
2362       CURSOR get_split_trn_csr (
2363          p_kle_id   IN   NUMBER
2364       )
2365       IS
2366          SELECT 'N',
2367                 txl.ID
2368            FROM okl_txl_assets_b txl,
2369                 okl_trx_assets trx,
2370                 okc_k_lines_v kle_fin,
2371                 okc_k_lines_v kle_fix,
2372                 okc_line_styles_b lty_fin,
2373                 okc_line_styles_b lty_fix
2374           WHERE txl.tal_type = 'ALI'
2375             AND trx.tsu_code = 'ENTERED'
2376             AND txl.tas_id = trx.ID
2377             AND kle_fin.lse_id = lty_fin.ID
2378             AND lty_fin.lty_code = 'FREE_FORM1'
2379             AND kle_fin.ID = kle_fix.cle_id
2380             AND kle_fix.lse_id = lty_fix.ID
2381             AND lty_fix.lty_code = 'FIXED_ASSET'
2382             AND txl.kle_id = kle_fix.ID
2383             AND kle_fin.ID = p_kle_id;
2384 
2385       -- RMUNJULU ASSETNUM INPUT 2757312
2386       -- Get the split asset
2387       CURSOR get_split_asset_csr (
2388          p_tal_id   IN   NUMBER
2389       )
2390       IS
2391          SELECT asset_number
2392            FROM okl_txd_assets_b
2393           WHERE tal_id = p_tal_id AND target_kle_id IS NULL;
2394 
2395       l_split_trn_processed_yn   VARCHAR2 (1)                          := 'Y';
2396       -- RMUNJULU ASSETNUM INPUT 2757312
2397       l_split_trn_id             NUMBER;
2398       l_split_asset              VARCHAR2 (400);
2399       l_asset_exists             VARCHAR2 (3);
2400 
2401       -- RMUNJULU ASSETNUM INPUT 2757312 Added function
2402       -- Function to check asset number is not duplicated
2403       FUNCTION asset_number_exists (
2404          p_asset_number   IN   VARCHAR2
2405       )
2406          RETURN VARCHAR2
2407       IS
2408          l_asset_exists   VARCHAR2 (1)   DEFAULT 'N';
2409          l_module_name    VARCHAR2 (500)
2410                                     := g_module_name || 'asset_number_exists';
2411 
2412          --chk for asset in FA
2413          CURSOR asset_chk_curs1 (
2414             p_asset_number   IN   VARCHAR2
2415          )
2416          IS
2417             SELECT 'Y' a
2418               FROM okx_assets_v okx
2419              WHERE okx.asset_number = p_asset_number;
2420 
2421          --chk for asset on asset line
2422          CURSOR asset_chk_curs2 (
2423             p_asset_number   IN   VARCHAR2
2424          )
2425          IS
2426             SELECT 'Y' a
2427               FROM okl_k_lines_full_v kle,
2428                    okc_line_styles_b lse
2429              WHERE kle.NAME = p_asset_number
2430                AND kle.lse_id = lse.ID
2431                AND lse.lty_code = 'FIXED_ASSET';
2432       BEGIN
2433          IF (is_debug_procedure_on)
2434          THEN
2435             okl_debug_pub.log_debug (g_level_procedure,
2436                                      l_module_name,
2437                                      'Begin(+)'
2438                                     );
2439          END IF;
2440 
2441          IF (is_debug_statement_on)
2442          THEN
2443             okl_debug_pub.log_debug (g_level_statement,
2444                                      l_module_name,
2445                                         'In param, p_asset_number: '
2446                                      || p_asset_number
2447                                     );
2448          END IF;
2449 
2450          FOR asset_chk_rec1 IN asset_chk_curs1 (p_asset_number)
2451          LOOP
2452             l_asset_exists := asset_chk_rec1.a;
2453 
2454             IF l_asset_exists <> 'Y'
2455             THEN
2456                FOR asset_chk_rec2 IN asset_chk_curs2 (p_asset_number)
2457                LOOP
2458                   l_asset_exists := asset_chk_rec2.a;
2459                END LOOP;
2460             END IF;
2461          END LOOP;
2462 
2463          IF (is_debug_statement_on)
2464          THEN
2465             okl_debug_pub.log_debug (g_level_statement,
2466                                      l_module_name,
2467                                         'Returning l_asset_exists: '
2468                                      || l_asset_exists
2469                                     );
2470          END IF;
2471 
2472          IF (is_debug_procedure_on)
2473          THEN
2474             okl_debug_pub.log_debug (g_level_procedure,
2475                                      l_module_name,
2476                                      'End(-)'
2477                                     );
2478          END IF;
2479 
2480          RETURN (l_asset_exists);
2481       EXCEPTION
2482          WHEN OTHERS
2483          THEN
2484             -- store SQL error message on message stack for caller
2485             okl_api.set_message (g_app_name,
2486                                  g_unexpected_error,
2487                                  g_sqlcode_token,
2488                                  SQLCODE,
2489                                  g_sqlerrm_token,
2490                                  SQLERRM
2491                                 );
2492 
2493             IF (is_debug_exception_on)
2494             THEN
2495                okl_debug_pub.log_debug (g_level_exception,
2496                                         l_module_name,
2497                                            'EXCEPTION :'
2498                                         || 'OTHERS, SQLCODE: '
2499                                         || SQLCODE
2500                                         || ' , SQLERRM : '
2501                                         || SQLERRM
2502                                        );
2503             END IF;
2504 
2505             RETURN ('N');
2506       END asset_number_exists;
2507    BEGIN
2508       SAVEPOINT do_split_asset;
2509 
2510       IF (is_debug_procedure_on)
2511       THEN
2512          okl_debug_pub.log_debug (g_level_procedure,
2513                                   l_module_name,
2514                                   'Begin(+)'
2515                                  );
2516       END IF;
2517 
2518       IF (is_debug_statement_on)
2519       THEN
2520          okl_debug_pub.log_debug
2521                                (g_level_statement,
2522                                 l_module_name,
2523                                    'In param, p_term_rec.p_contract_number: '
2524                                 || p_term_rec.p_contract_number
2525                                );
2526          okl_debug_pub.log_debug (g_level_statement,
2527                                   l_module_name,
2528                                   'In param, p_sys_date: ' || p_sys_date
2529                                  );
2530          okl_debug_pub.log_debug (g_level_statement,
2531                                   l_module_name,
2532                                      'In param, p_trn_already_set: '
2533                                   || p_trn_already_set
2534                                  );
2535          okl_debug_pub.log_debug (g_level_statement,
2536                                   l_module_name,
2537                                      'In param, px_overall_status: '
2538                                   || px_overall_status
2539                                  );
2540          okl_debug_pub.log_debug
2541                               (g_level_statement,
2542                                l_module_name,
2543                                   'In param, px_tcnv_rec.tmt_split_asset_yn: '
2544                                || px_tcnv_rec.tmt_split_asset_yn
2545                               );
2546 
2547          IF px_klev_tbl.COUNT > 0
2548          THEN
2549             FOR i IN px_klev_tbl.FIRST .. px_klev_tbl.LAST
2550             LOOP
2551                IF (px_klev_tbl.EXISTS (i))
2552                THEN
2553                   okl_debug_pub.log_debug (g_level_statement,
2554                                            l_module_name,
2555                                               'In param, px_klev_tbl('
2556                                            || i
2557                                            || ').p_kle_id: '
2558                                            || px_klev_tbl (i).p_kle_id
2559                                           );
2560                   okl_debug_pub.log_debug (g_level_statement,
2561                                            l_module_name,
2562                                               'In param, px_klev_tbl('
2563                                            || i
2564                                            || ').p_asset_quantity: '
2565                                            || px_klev_tbl (i).p_asset_quantity
2566                                           );
2567                   okl_debug_pub.log_debug (g_level_statement,
2568                                            l_module_name,
2569                                               'In param, px_klev_tbl('
2570                                            || i
2571                                            || ').p_asset_name: '
2572                                            || px_klev_tbl (i).p_asset_name
2573                                           );
2574                   okl_debug_pub.log_debug (g_level_statement,
2575                                            l_module_name,
2576                                               'In param, px_klev_tbl('
2577                                            || i
2578                                            || ').p_quote_quantity: '
2579                                            || px_klev_tbl (i).p_quote_quantity
2580                                           );
2581                   okl_debug_pub.log_debug (g_level_statement,
2582                                            l_module_name,
2583                                               'In param, px_klev_tbl('
2584                                            || i
2585                                            || ').p_tql_id: '
2586                                            || px_klev_tbl (i).p_tql_id
2587                                           );
2588                   okl_debug_pub.log_debug (g_level_statement,
2589                                            l_module_name,
2590                                               'In param, px_klev_tbl('
2591                                            || i
2592                                            || ').p_split_kle_name: '
2593                                            || px_klev_tbl (i).p_split_kle_name
2594                                           );
2595                END IF;
2596             END LOOP;
2597          END IF;
2598       END IF;
2599 
2600       x_return_status := okl_api.g_ret_sts_success;
2601 
2602       IF    (    p_trn_already_set = g_yes
2603              AND NVL (px_tcnv_rec.tmt_split_asset_yn, '?') <> g_yes
2604             )
2605          OR (p_trn_already_set = g_no)
2606       THEN
2607          IF px_klev_tbl.COUNT > 0
2608          THEN
2609             -- Loop thru financial assets
2610             FOR i IN px_klev_tbl.FIRST .. px_klev_tbl.LAST
2611             LOOP
2612                -- Check the trn if processed if processed then set tmt_split_asset_yn = y and do
2613                -- not do split, but how to get split_kle_id and update the quote with split_kle_id
2614                -- AND split_kle_name
2615                -- For financial asset line - get fixed asset line
2616                -- query for all trns in txl_assets for fixed asset line of tal_type 'ALI'
2617                -- see if the trx_assets trn is not in status other than processed
2618                OPEN get_split_trn_csr (px_klev_tbl (i).p_kle_id);
2619 
2620                FETCH get_split_trn_csr
2621                 INTO l_split_trn_processed_yn,
2622                      l_split_trn_id;
2623                        -- RMUNJULU ASSETNUM INPUT 2757312 Added l_split_trn_id
2624 
2625                CLOSE get_split_trn_csr;
2626 
2627                -- If Not Processed Split Trn exists then our split trn is pending
2628                IF l_split_trn_processed_yn = 'N'
2629                THEN
2630                   IF (is_debug_statement_on)
2631                   THEN
2632                      okl_debug_pub.log_debug
2633                              (g_level_statement,
2634                               l_module_name,
2635                               'calling OKL_SPLIT_ASSET_PUB.split_fixed_asset'
2636                              );
2637                   END IF;
2638 
2639                   -- Process split asset transactions
2640                   okl_split_asset_pub.split_fixed_asset
2641                                          (p_api_version        => l_api_version,
2642                                           p_init_msg_list      => g_false,
2643                                           x_return_status      => x_return_status,
2644                                           x_msg_count          => l_msg_count,
2645                                           x_msg_data           => l_msg_data,
2646                                           p_cle_id             => px_klev_tbl
2647                                                                            (i).p_kle_id,
2648                                           x_cle_tbl            => lx_cle_tbl,
2649                                           p_source_call        => 'PARTIAL_TERM'
2650                                          );
2651                               -- sechawla 18-dec-07 - Split Asset Enhancements
2652 
2653                   IF (is_debug_statement_on)
2654                   THEN
2655                      okl_debug_pub.log_debug
2656                         (g_level_statement,
2657                          l_module_name,
2658                             'called OKL_SPLIT_ASSET_PUB.split_fixed_asset , return status: '
2659                          || x_return_status
2660                         );
2661                   END IF;
2662 
2663                   -- Try doing split for all split assets and get all errors
2664                   IF x_return_status <> okl_api.g_ret_sts_success
2665                   THEN
2666                      -- RMUNJULU ASSETNUM INPUT 2757312
2667                      -- Get the split asset from the split transaction
2668                      FOR get_split_asset_rec IN
2669                         get_split_asset_csr (l_split_trn_id)
2670                      LOOP
2671                         l_split_asset := get_split_asset_rec.asset_number;
2672 
2673                         IF (is_debug_statement_on)
2674                         THEN
2675                            okl_debug_pub.log_debug
2676                               (g_level_statement,
2677                                l_module_name,
2678                                'calling OKL_AM_CREATE_QUOTE_PVT.asset_number_exists'
2679                               );
2680                         END IF;
2681 
2682                         -- RMUNJULU 3241502 Call asset_number_exists from OKL_AM_CREATE_QUOTE_PVT
2683                         l_return_status :=
2684                            okl_am_create_quote_pvt.asset_number_exists
2685                                              (p_asset_number      => l_split_asset,
2686                                               x_asset_exists      => l_asset_exists);
2687 
2688                         IF (is_debug_statement_on)
2689                         THEN
2690                            okl_debug_pub.log_debug
2691                               (g_level_statement,
2692                                l_module_name,
2693                                   'called OKL_AM_CREATE_QUOTE_PVT.asset_number_exists , return status: '
2694                                || l_return_status
2695                               );
2696                         END IF;
2697 
2698                         -- Make sure new asset is not unique before throwing message saying so
2699                         IF l_asset_exists = 'Y'
2700                         THEN
2701                            -- New Asset Number NEW_ASSET_NUMBER is not unique. Please update the
2702                            -- New Asset Number for the Contract CONTRACT_NUMBER and Original
2703                            -- Asset Number ORIG_ASSET_NUMBER from the Split Asset screen.
2704                            okl_api.set_message
2705                               (p_app_name          => g_app_name,
2706                                p_msg_name          => 'OKL_AM_SPLIT_ASSET_ERROR',
2707                                p_token1            => 'NEW_ASSET_NUMBER',
2708                                p_token1_value      => l_split_asset,
2709                                             -- RMUNJULU ASSETNUM INPUT 2757312
2710                                p_token2            => 'CONTRACT_NUMBER',
2711                                p_token2_value      => p_term_rec.p_contract_number,
2712                                p_token3            => 'ORIG_ASSET_NUMBER',
2713                                p_token3_value      => px_klev_tbl (i).p_asset_name
2714                               );
2715                         END IF;
2716                      END LOOP;
2717 
2718                      -- Set Return status
2719                      l_return_status := x_return_status;
2720                   END IF;
2721 
2722                   IF x_return_status = okl_api.g_ret_sts_success
2723                   THEN
2724                      -- RMUNJULU 2757312 Added this IF
2725                      IF lx_cle_tbl.COUNT = 2
2726                      THEN
2727                         -- lx_cle_tbl returns 2 rows. One for the original kle id and
2728                         -- the other one for the split (new) kle_id
2729                         cle_index := lx_cle_tbl.FIRST;
2730 
2731                         IF lx_cle_tbl (cle_index).cle_id <>
2732                                                      px_klev_tbl (i).p_kle_id
2733                         THEN
2734                            -- split kle id is the first one in the table
2735                            px_klev_tbl (i).p_split_kle_id :=
2736                                                 lx_cle_tbl (cle_index).cle_id;
2737                         ELSE
2738                            -- split kle id is the second one in the table
2739                            px_klev_tbl (i).p_split_kle_id :=
2740                                             lx_cle_tbl (cle_index + 1).cle_id;
2741                         END IF;
2742                      ELSE
2743                         -- Invalid value for x_cle_tbl.
2744                         okl_api.set_message (p_app_name          => g_app_name_1,
2745                                              p_msg_name          => g_invalid_value,
2746                                              p_token1            => g_col_name_token,
2747                                              p_token1_value      => 'x_cle_tbl'
2748                                             );
2749                         RAISE okl_api.g_exception_error;
2750                      END IF;
2751                   END IF;
2752                END IF;
2753             END LOOP;
2754          END IF;
2755 
2756          -- Raise Exception here after trying to do all splits
2757          IF (l_return_status = g_ret_sts_unexp_error)
2758          THEN
2759             RAISE okl_api.g_exception_unexpected_error;
2760          ELSIF (l_return_status = g_ret_sts_error)
2761          THEN
2762             RAISE okl_api.g_exception_error;
2763          END IF;
2764 
2765 --++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++START
2766 -- RMUNJULU 2757312 ADD code
2767 
2768          -- RMUNJULU Bug # 2484327 16-DEC-02
2769          -- Set the lp_tqlv_tbl and call OKL_TXL_QUOTE_LINES_PUB, set
2770          -- success messages
2771          IF px_klev_tbl.COUNT > 0
2772          THEN
2773             FOR i IN px_klev_tbl.FIRST .. px_klev_tbl.LAST
2774             LOOP
2775                -- Set the TXL_quote_lines rec to set the split_kle_id
2776                lp_tqlv_tbl (i).ID := px_klev_tbl (i).p_tql_id;
2777                lp_tqlv_tbl (i).split_kle_id := px_klev_tbl (i).p_split_kle_id;
2778             END LOOP;
2779          END IF;
2780 
2781          IF (is_debug_statement_on)
2782          THEN
2783             okl_debug_pub.log_debug
2784                     (g_level_statement,
2785                      l_module_name,
2786                      'calling OKL_TXL_QUOTE_LINES_PUB.update_txl_quote_lines'
2787                     );
2788          END IF;
2789 
2790          -- Call the TAPI to update TXL_quote_lines
2791          okl_txl_quote_lines_pub.update_txl_quote_lines
2792                                           (p_api_version        => l_api_version,
2793                                            p_init_msg_list      => g_false,
2794                                            x_return_status      => x_return_status,
2795                                            x_msg_count          => l_msg_count,
2796                                            x_msg_data           => l_msg_data,
2797                                            p_tqlv_tbl           => lp_tqlv_tbl,
2798                                            x_tqlv_tbl           => lx_tqlv_tbl
2799                                           );
2800 
2801          IF (is_debug_statement_on)
2802          THEN
2803             okl_debug_pub.log_debug
2804                (g_level_statement,
2805                 l_module_name,
2806                    'called OKL_TXL_QUOTE_LINES_PUB.update_txl_quote_lines , return status: '
2807                 || x_return_status
2808                );
2809          END IF;
2810 
2811          -- Raise exception to rollback to if error
2812          IF (x_return_status = g_ret_sts_unexp_error)
2813          THEN
2814             RAISE okl_api.g_exception_unexpected_error;
2815          ELSIF (x_return_status = g_ret_sts_error)
2816          THEN
2817             RAISE okl_api.g_exception_error;
2818          END IF;
2819 
2820          IF px_klev_tbl.COUNT > 0
2821          THEN
2822             -- Set the success messages if successful split and update
2823             FOR i IN px_klev_tbl.FIRST .. px_klev_tbl.LAST
2824             LOOP
2825                OPEN get_asset_name_csr (px_klev_tbl (i).p_split_kle_id);
2826 
2827                FETCH get_asset_name_csr
2828                 INTO l_new_asset_name;
2829 
2830                CLOSE get_asset_name_csr;
2831 
2832                l_new_asset_quantity :=
2833                     px_klev_tbl (i).p_asset_quantity
2834                   - px_klev_tbl (i).p_quote_quantity;
2835                -- Asset ASSET_NUMBER_OLD has been split. Asset ASSET_NUMBER_OLD
2836                -- retains quantity QUANTITY_OLD. New asset ASSET_NUMBER_NEW has
2837                -- been created with quantity QUANTITY_NEW.
2838                -- RMUNJULU 03-JAN-03 2683876 Changed msg token
2839                -- RMUNJULU 21-JAN-03 2760324 Removed additonal msg token
2840                okl_api.set_message
2841                            (p_app_name          => g_app_name,
2842                             p_msg_name          => 'OKL_AM_SPLIT_ASSET_MSG',
2843                             p_token1            => 'ASSET_NUMBER_OLD',
2844                             p_token1_value      => px_klev_tbl (i).p_asset_name,
2845                             p_token2            => 'QUANTITY_OLD',
2846                             p_token2_value      => px_klev_tbl (i).p_quote_quantity,
2847                             p_token3            => 'ASSET_NUMBER_NEW',
2848                             p_token3_value      => l_new_asset_name,
2849                             p_token4            => 'QUANTITY_NEW',
2850                             p_token4_value      => l_new_asset_quantity
2851                            );
2852             END LOOP;
2853          END IF;
2854 
2855          -- store the highest degree of error
2856          set_overall_status (p_return_status        => x_return_status,
2857                              px_overall_status      => px_overall_status);
2858          -- set the transaction record
2859          set_transaction_rec (p_return_status       => x_return_status,
2860                               p_overall_status      => px_overall_status,
2861                               p_tmt_flag            => 'TMT_SPLIT_ASSET_YN',
2862                               p_tsu_code            => 'WORKING',
2863                               px_tcnv_rec           => px_tcnv_rec
2864                              );
2865 --++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END
2866       END IF;
2867 
2868       IF (is_debug_procedure_on)
2869       THEN
2870          okl_debug_pub.log_debug (g_level_procedure,
2871                                   l_module_name,
2872                                   'End(-)'
2873                                  );
2874       END IF;
2875    EXCEPTION
2876       WHEN okl_api.g_exception_error
2877       THEN
2878          -- SECHAWLA 23-DEC-02 2484327 Closed cursors if open
2879          IF get_ib_lines_cnt_csr%ISOPEN
2880          THEN
2881             CLOSE get_ib_lines_cnt_csr;
2882          END IF;
2883 
2884          IF get_ib_lines_csr%ISOPEN
2885          THEN
2886             CLOSE get_ib_lines_csr;
2887          END IF;
2888 
2889          -- close open cursors
2890          IF get_asset_name_csr%ISOPEN
2891          THEN
2892             CLOSE get_asset_name_csr;
2893          END IF;
2894 
2895          IF get_split_trn_csr%ISOPEN
2896          THEN
2897             CLOSE get_split_trn_csr;
2898          END IF;
2899 
2900          ROLLBACK TO do_split_asset;
2901          x_return_status := g_ret_sts_error;
2902          -- store the highest degree of error
2903          set_overall_status (p_return_status        => x_return_status,
2904                              px_overall_status      => px_overall_status);
2905          -- set the transaction record
2906          set_transaction_rec (p_return_status       => x_return_status,
2907                               p_overall_status      => px_overall_status,
2908                               p_tmt_flag            => 'TMT_SPLIT_ASSET_YN',
2909                               p_tsu_code            => 'ERROR',
2910                               px_tcnv_rec           => px_tcnv_rec
2911                              );
2912 
2913          IF (is_debug_exception_on)
2914          THEN
2915             okl_debug_pub.log_debug (g_level_exception,
2916                                      l_module_name,
2917                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
2918                                     );
2919          END IF;
2920       WHEN okl_api.g_exception_unexpected_error
2921       THEN
2922          -- SECHAWLA 23-DEC-02 2484327 Closed cursors if open
2923          IF get_ib_lines_cnt_csr%ISOPEN
2924          THEN
2925             CLOSE get_ib_lines_cnt_csr;
2926          END IF;
2927 
2928          IF get_ib_lines_csr%ISOPEN
2929          THEN
2930             CLOSE get_ib_lines_csr;
2931          END IF;
2932 
2933          -- close open cursors
2934          IF get_asset_name_csr%ISOPEN
2935          THEN
2936             CLOSE get_asset_name_csr;
2937          END IF;
2938 
2939          IF get_split_trn_csr%ISOPEN
2940          THEN
2941             CLOSE get_split_trn_csr;
2942          END IF;
2943 
2944          ROLLBACK TO do_split_asset;
2945          x_return_status := g_ret_sts_unexp_error;
2946          -- store the highest degree of error
2947          set_overall_status (p_return_status        => x_return_status,
2948                              px_overall_status      => px_overall_status);
2949          -- set the transaction record
2950          set_transaction_rec (p_return_status       => x_return_status,
2951                               p_overall_status      => px_overall_status,
2952                               p_tmt_flag            => 'TMT_SPLIT_ASSET_YN',
2953                               p_tsu_code            => 'ERROR',
2954                               px_tcnv_rec           => px_tcnv_rec
2955                              );
2956 
2957          IF (is_debug_exception_on)
2958          THEN
2959             okl_debug_pub.log_debug (g_level_exception,
2960                                      l_module_name,
2961                                         'EXCEPTION :'
2962                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
2963                                     );
2964          END IF;
2965       WHEN OTHERS
2966       THEN
2967          -- SECHAWLA 23-DEC-02 2484327 Closed cursors if open
2968          IF get_ib_lines_cnt_csr%ISOPEN
2969          THEN
2970             CLOSE get_ib_lines_cnt_csr;
2971          END IF;
2972 
2973          IF get_ib_lines_csr%ISOPEN
2974          THEN
2975             CLOSE get_ib_lines_csr;
2976          END IF;
2977 
2978          -- close open cursors
2979          IF get_asset_name_csr%ISOPEN
2980          THEN
2981             CLOSE get_asset_name_csr;
2982          END IF;
2983 
2984          IF get_split_trn_csr%ISOPEN
2985          THEN
2986             CLOSE get_split_trn_csr;
2987          END IF;
2988 
2989          ROLLBACK TO do_split_asset;
2990          x_return_status := g_ret_sts_unexp_error;
2991          -- Set the oracle error message
2992          okl_api.set_message (p_app_name          => g_app_name_1,
2993                               p_msg_name          => g_unexpected_error,
2994                               p_token1            => g_sqlcode_token,
2995                               p_token1_value      => SQLCODE,
2996                               p_token2            => g_sqlerrm_token,
2997                               p_token2_value      => SQLERRM
2998                              );
2999          -- store the highest degree of error
3000          set_overall_status (p_return_status        => x_return_status,
3001                              px_overall_status      => px_overall_status);
3002          -- set the transaction record
3003          set_transaction_rec (p_return_status       => x_return_status,
3004                               p_overall_status      => px_overall_status,
3005                               p_tmt_flag            => 'TMT_SPLIT_ASSET_YN',
3006                               p_tsu_code            => 'ERROR',
3007                               px_tcnv_rec           => px_tcnv_rec
3008                              );
3009 
3010          IF (is_debug_exception_on)
3011          THEN
3012             okl_debug_pub.log_debug (g_level_exception,
3013                                      l_module_name,
3014                                         'EXCEPTION :'
3015                                      || 'OTHERS, SQLCODE: '
3016                                      || SQLCODE
3017                                      || ' , SQLERRM : '
3018                                      || SQLERRM
3019                                     );
3020          END IF;
3021    END do_split_asset;
3022 
3023    -- Start of comments
3024    --
3025    -- Procedure Name : split_asset
3026    -- Desciption     : Checks if split asset needed ( if partial line qte)
3027    --                  Calls the do_split_asset if split asset needed and
3028    --                  sets the transaction properly
3029    -- Business Rules :
3030    -- Parameters     :
3031    -- Version      : 1.0
3032    -- History        : RMUNJULU Bug # 2484327 16-DEC-02, changed logic to set
3033    --                  split_tbl and call do_split_asset and updating TXL_quote_lines
3034    --                  and setting success messages
3035    --                  RMUNJULU 03-JAN-03 2683876 Changed msg token
3036    --                  RMUNJULU 21-JAN-03 2760324 Removed additonal msg token
3037    --                  RMUNJULU 2757312 MAJOR CHANGES to the whole procedure
3038    --                  will now call do_split_trn which will create + update split
3039    --                  trn and then call do_split_asset which will do split_asset
3040    --
3041    -- End of comments
3042    PROCEDURE split_asset (
3043       p_term_rec          IN              term_rec_type,
3044       p_sys_date          IN              DATE,
3045       p_klev_tbl          IN              klev_tbl_type,
3046       p_trn_already_set   IN              VARCHAR2,
3047       px_overall_status   IN OUT NOCOPY   VARCHAR2,
3048       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
3049       x_klev_tbl          OUT NOCOPY      klev_tbl_type,
3050       x_return_status     OUT NOCOPY      VARCHAR2
3051    )
3052    IS
3053       l_split_tbl              klev_tbl_type;
3054       i                        NUMBER;
3055       j                        NUMBER;
3056       l_return_status          VARCHAR2 (1)              := g_ret_sts_success;
3057       lp_tqlv_tbl              okl_txl_quote_lines_pub.tqlv_tbl_type;
3058       lx_tqlv_tbl              okl_txl_quote_lines_pub.tqlv_tbl_type;
3059       l_new_asset_name         VARCHAR2 (2000);
3060       l_new_asset_quantity     NUMBER;
3061       l_api_version   CONSTANT NUMBER                        := g_api_version;
3062       l_msg_count              NUMBER                           := g_miss_num;
3063       l_msg_data               VARCHAR2 (2000);
3064       l_module_name            VARCHAR2 (500)
3065                                             := g_module_name || 'split_asset';
3066       is_debug_exception_on    BOOLEAN
3067              := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
3068       is_debug_procedure_on    BOOLEAN
3069              := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
3070       is_debug_statement_on    BOOLEAN
3071              := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
3072    BEGIN
3073       -- Start a savepoint to rollback to if error in this block
3074       SAVEPOINT split_asset;
3075 
3076       IF (is_debug_procedure_on)
3077       THEN
3078          okl_debug_pub.log_debug (g_level_procedure,
3079                                   l_module_name,
3080                                   'Begin(+)'
3081                                  );
3082       END IF;
3083 
3084       IF (is_debug_statement_on)
3085       THEN
3086          okl_debug_pub.log_debug (g_level_statement,
3087                                   l_module_name,
3088                                   'In param, p_sys_date: ' || p_sys_date
3089                                  );
3090          okl_debug_pub.log_debug (g_level_statement,
3091                                   l_module_name,
3092                                      'In param, p_trn_already_set: '
3093                                   || p_trn_already_set
3094                                  );
3095          okl_debug_pub.log_debug (g_level_statement,
3096                                   l_module_name,
3097                                      'In param, px_overall_status: '
3098                                   || px_overall_status
3099                                  );
3100          okl_debug_pub.log_debug
3101                               (g_level_statement,
3102                                l_module_name,
3103                                   'In param, px_tcnv_rec.tmt_split_asset_yn: '
3104                                || px_tcnv_rec.tmt_split_asset_yn
3105                               );
3106          okl_debug_pub.log_debug
3107                             (g_level_statement,
3108                              l_module_name,
3109                                 'In param, px_tcnv_rec.tmt_generic_flag1_yn: '
3110                              || px_tcnv_rec.tmt_generic_flag1_yn
3111                             );
3112 
3113          IF p_klev_tbl.COUNT > 0
3114          THEN
3115             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
3116             LOOP
3117                IF (p_klev_tbl.EXISTS (i))
3118                THEN
3119                   okl_debug_pub.log_debug (g_level_statement,
3120                                            l_module_name,
3121                                               'In param, p_klev_tbl('
3122                                            || i
3123                                            || ').p_kle_id: '
3124                                            || p_klev_tbl (i).p_kle_id
3125                                           );
3126                   okl_debug_pub.log_debug (g_level_statement,
3127                                            l_module_name,
3128                                               'In param, p_klev_tbl('
3129                                            || i
3130                                            || ').p_asset_quantity: '
3131                                            || p_klev_tbl (i).p_asset_quantity
3132                                           );
3133                   okl_debug_pub.log_debug (g_level_statement,
3134                                            l_module_name,
3135                                               'In param, p_klev_tbl('
3136                                            || i
3137                                            || ').p_asset_name: '
3138                                            || p_klev_tbl (i).p_asset_name
3139                                           );
3140                   okl_debug_pub.log_debug (g_level_statement,
3141                                            l_module_name,
3142                                               'In param, p_klev_tbl('
3143                                            || i
3144                                            || ').p_quote_quantity: '
3145                                            || p_klev_tbl (i).p_quote_quantity
3146                                           );
3147                   okl_debug_pub.log_debug (g_level_statement,
3148                                            l_module_name,
3149                                               'In param, p_klev_tbl('
3150                                            || i
3151                                            || ').p_tql_id: '
3152                                            || p_klev_tbl (i).p_tql_id
3153                                           );
3154                   okl_debug_pub.log_debug (g_level_statement,
3155                                            l_module_name,
3156                                               'In param, p_klev_tbl('
3157                                            || i
3158                                            || ').p_split_kle_name: '
3159                                            || p_klev_tbl (i).p_split_kle_name
3160                                           );
3161                   okl_debug_pub.log_debug (g_level_statement,
3162                                            l_module_name,
3163                                               'In param, p_klev_tbl('
3164                                            || i
3165                                            || ').p_split_kle_id: '
3166                                            || p_klev_tbl (i).p_split_kle_id
3167                                           );
3168                END IF;
3169             END LOOP;
3170          END IF;
3171       END IF;
3172 
3173       -- if split asset step not done
3174       IF    (    p_trn_already_set = g_yes
3175              AND (   NVL (px_tcnv_rec.tmt_split_asset_yn, '?') <> g_yes
3176                   OR NVL (px_tcnv_rec.tmt_generic_flag1_yn, '?') <> g_yes
3177                  )
3178             )
3179          OR (p_trn_already_set = g_no)
3180       THEN
3181          -- For each asset check if split asset needs to be done
3182          -- only if quote qty < asset qty set the klev_tbl to be passed to
3183          -- split asset
3184          IF (p_klev_tbl.COUNT > 0)
3185          THEN
3186             i := p_klev_tbl.FIRST;
3187             j := 1;
3188 
3189             LOOP
3190                -- get the assets which needs to be splited into l_split_tbl
3191                -- RMUNJULU Bug # 2484327 16-DEC-02
3192                -- Removed the cursor access and setting of non_splited_tbl
3193                IF p_klev_tbl (i).p_quote_quantity <
3194                                               p_klev_tbl (i).p_asset_quantity
3195                THEN
3196                   l_split_tbl (j).p_kle_id := p_klev_tbl (i).p_kle_id;
3197                   l_split_tbl (j).p_asset_quantity :=
3198                                               p_klev_tbl (i).p_asset_quantity;
3199                   l_split_tbl (j).p_asset_name := p_klev_tbl (i).p_asset_name;
3200                   l_split_tbl (j).p_quote_quantity :=
3201                                               p_klev_tbl (i).p_quote_quantity;
3202                   l_split_tbl (j).p_tql_id := p_klev_tbl (i).p_tql_id;
3203                   l_split_tbl (j).p_split_kle_id :=
3204                                                 p_klev_tbl (i).p_split_kle_id;
3205                   -- RMUNJULU 2757312
3206                   l_split_tbl (j).p_split_kle_name :=
3207                                               p_klev_tbl (i).p_split_kle_name;
3208                   j := j + 1;
3209                END IF;
3210 
3211                EXIT WHEN (i = p_klev_tbl.LAST);
3212                i := p_klev_tbl.NEXT (i);
3213             END LOOP;
3214          END IF;
3215 
3216          -- If l_split_tbl is not empty then we do need to do split asset -- IE - Partial Line
3217          IF (l_split_tbl.COUNT > 0)
3218          THEN
3219             -- RMUNJULU 3018641 Step Message
3220             -- Step : Split Asset
3221             okl_api.set_message (p_app_name      => g_app_name,
3222                                  p_msg_name      => 'OKL_AM_STEP_SPL');
3223             -- Call the do_split_asset_trn with the l_split_tbl
3224             do_split_asset_trn (p_term_rec             => p_term_rec,
3225                                 p_sys_date             => p_sys_date,
3226                                 p_trn_already_set      => p_trn_already_set,
3227                                 px_overall_status      => px_overall_status,
3228                                 px_tcnv_rec            => px_tcnv_rec,
3229                                 px_klev_tbl            => l_split_tbl,
3230                                 x_return_status        => l_return_status
3231                                );
3232 
3233             IF (is_debug_statement_on)
3234             THEN
3235                okl_debug_pub.log_debug
3236                             (g_level_statement,
3237                              l_module_name,
3238                                 'called do_split_asset_trn , return status: '
3239                              || l_return_status
3240                             );
3241             END IF;
3242 
3243             IF l_return_status <> g_ret_sts_success
3244             THEN
3245                -- Split asset failed.
3246                okl_api.set_message (p_app_name      => g_app_name,
3247                                     p_msg_name      => 'OKL_AM_ERR_SPLIT_ASST');
3248                x_return_status := l_return_status;
3249             END IF;
3250 
3251             IF l_return_status = g_ret_sts_success
3252             THEN
3253                -- Call the do_split_asset with the l_split_tbl
3254                do_split_asset (p_term_rec             => p_term_rec,
3255                                p_sys_date             => p_sys_date,
3256                                p_trn_already_set      => p_trn_already_set,
3257                                px_overall_status      => px_overall_status,
3258                                px_tcnv_rec            => px_tcnv_rec,
3259                                px_klev_tbl            => l_split_tbl,
3260                                x_return_status        => l_return_status
3261                               );
3262 
3263                IF (is_debug_statement_on)
3264                THEN
3265                   okl_debug_pub.log_debug
3266                                 (g_level_statement,
3267                                  l_module_name,
3268                                     'called do_split_asset , return status: '
3269                                  || l_return_status
3270                                 );
3271                END IF;
3272 
3273                IF l_return_status <> g_ret_sts_success
3274                THEN
3275                   -- Split asset failed.
3276                   okl_api.set_message (p_app_name      => g_app_name,
3277                                        p_msg_name      => 'OKL_AM_ERR_SPLIT_ASST');
3278                   x_return_status := l_return_status;
3279                END IF;
3280             END IF;
3281          ELSE
3282             --( no need for split asset since no partial line )
3283 
3284             -- set the transaction record
3285             set_transaction_rec (p_return_status       => l_return_status,
3286                                  p_overall_status      => px_overall_status,
3287                                  p_tmt_flag            => 'TMT_GENERIC_FLAG1_YN',
3288                                  p_tsu_code            => 'WORKING',
3289                                  p_ret_val             => NULL,
3290                                  px_tcnv_rec           => px_tcnv_rec
3291                                 );
3292             -- set the transaction record
3293             set_transaction_rec (p_return_status       => l_return_status,
3294                                  p_overall_status      => px_overall_status,
3295                                  p_tmt_flag            => 'TMT_SPLIT_ASSET_YN',
3296                                  p_tsu_code            => 'WORKING',
3297                                  p_ret_val             => NULL,
3298                                  px_tcnv_rec           => px_tcnv_rec
3299                                 );
3300          END IF;
3301       END IF;
3302 
3303       -- Set the return status
3304       x_return_status := l_return_status;
3305       -- Set the return klev_tbl
3306       x_klev_tbl := p_klev_tbl;
3307 
3308       IF (is_debug_procedure_on)
3309       THEN
3310          okl_debug_pub.log_debug (g_level_procedure,
3311                                   l_module_name,
3312                                   'End(-)'
3313                                  );
3314       END IF;
3315    EXCEPTION
3316       WHEN okl_api.g_exception_error
3317       THEN
3318          ROLLBACK TO split_asset;
3319          x_return_status := g_ret_sts_error;
3320 
3321          IF (is_debug_exception_on)
3322          THEN
3323             okl_debug_pub.log_debug (g_level_exception,
3324                                      l_module_name,
3325                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
3326                                     );
3327          END IF;
3328       WHEN okl_api.g_exception_unexpected_error
3329       THEN
3330          ROLLBACK TO split_asset;
3331          x_return_status := g_ret_sts_unexp_error;
3332 
3333          IF (is_debug_exception_on)
3334          THEN
3335             okl_debug_pub.log_debug (g_level_exception,
3336                                      l_module_name,
3337                                         'EXCEPTION :'
3338                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
3339                                     );
3340          END IF;
3341       WHEN OTHERS
3342       THEN
3343          ROLLBACK TO split_asset;
3344          x_return_status := g_ret_sts_unexp_error;
3345          -- Set the oracle error message
3346          okl_api.set_message (p_app_name          => g_app_name_1,
3347                               p_msg_name          => g_unexpected_error,
3348                               p_token1            => g_sqlcode_token,
3349                               p_token1_value      => SQLCODE,
3350                               p_token2            => g_sqlerrm_token,
3351                               p_token2_value      => SQLERRM
3352                              );
3353 
3354          IF (is_debug_exception_on)
3355          THEN
3356             okl_debug_pub.log_debug (g_level_exception,
3357                                      l_module_name,
3358                                         'EXCEPTION :'
3359                                      || 'OTHERS, SQLCODE: '
3360                                      || SQLCODE
3361                                      || ' , SQLERRM : '
3362                                      || SQLERRM
3363                                     );
3364          END IF;
3365    END split_asset;
3366 
3367    -- Start of comments
3368    --
3369    -- Procedure Name : close_streams
3370    -- Desciption     : Checks if any active streams for the assets and closes
3371    -- Business Rules :
3372    -- Parameters     :
3373    -- Version    : 1.0
3374    -- History        : RMUNJULU 28-MAR-03 2877278 Changed the cursor and code
3375    --                  to get only CURRENT streams and HISTORIZE them
3376    --
3377    -- End of comments
3378    PROCEDURE close_streams (
3379       p_term_rec          IN              term_rec_type,
3380       p_sys_date          IN              DATE,
3381       p_klev_tbl          IN              klev_tbl_type,
3382       p_trn_already_set   IN              VARCHAR2,
3383       px_overall_status   IN OUT NOCOPY   VARCHAR2,
3384       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
3385       x_return_status     OUT NOCOPY      VARCHAR2
3386    )
3387    IS
3388       -- Cursor to get the active streams of the asset
3389       -- RMUNJULU 28-MAR-03 2877278 Added conditions to pick only CURRENT
3390       -- streams.
3391       CURSOR k_streams_csr (
3392          p_kle_id   IN   NUMBER
3393       )
3394       IS
3395          SELECT stm.ID ID
3396            FROM okl_streams_v stm
3397           WHERE stm.kle_id = p_kle_id AND stm.say_code = 'CURR';
3398 
3399       k_streams_rec            k_streams_csr%ROWTYPE;
3400       l_return_status          VARCHAR2 (1)               := g_ret_sts_success;
3401       lp_stmv_tbl              okl_streams_pub.stmv_tbl_type;
3402       lx_stmv_tbl              okl_streams_pub.stmv_tbl_type;
3403       l_streams_found          VARCHAR2 (1)                  := g_no;
3404       i                        NUMBER;
3405       j                        NUMBER;
3406       l_id                     NUMBER;
3407       l_api_version   CONSTANT NUMBER                        := g_api_version;
3408       l_msg_count              NUMBER                        := g_miss_num;
3409       l_msg_data               VARCHAR2 (2000);
3410       l_module_name            VARCHAR2 (500)
3411                                            := g_module_name || 'close_streams';
3412       is_debug_exception_on    BOOLEAN
3413               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
3414       is_debug_procedure_on    BOOLEAN
3415               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
3416       is_debug_statement_on    BOOLEAN
3417               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
3418    BEGIN
3419       -- Start a savepoint to rollback to if error in this block
3420       SAVEPOINT close_streams;
3421 
3422       IF (is_debug_procedure_on)
3423       THEN
3424          okl_debug_pub.log_debug (g_level_procedure,
3425                                   l_module_name,
3426                                   'Begin(+)'
3427                                  );
3428       END IF;
3429 
3430       IF (is_debug_statement_on)
3431       THEN
3432          okl_debug_pub.log_debug (g_level_statement,
3433                                   l_module_name,
3434                                   'In param, p_sys_date: ' || p_sys_date
3435                                  );
3436          okl_debug_pub.log_debug (g_level_statement,
3437                                   l_module_name,
3438                                      'In param, p_trn_already_set: '
3439                                   || p_trn_already_set
3440                                  );
3441          okl_debug_pub.log_debug (g_level_statement,
3442                                   l_module_name,
3443                                      'In param, px_overall_status: '
3444                                   || px_overall_status
3445                                  );
3446          okl_debug_pub.log_debug (g_level_statement,
3447                                   l_module_name,
3448                                      'In param, p_term_rec.p_contract_id: '
3449                                   || p_term_rec.p_contract_id
3450                                  );
3451          okl_debug_pub.log_debug
3452                           (g_level_statement,
3453                            l_module_name,
3454                               'In param, px_tcnv_rec.tmt_streams_updated_yn: '
3455                            || px_tcnv_rec.tmt_streams_updated_yn
3456                           );
3457 
3458          IF p_klev_tbl.COUNT > 0
3459          THEN
3460             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
3461             LOOP
3462                IF (p_klev_tbl.EXISTS (i))
3463                THEN
3464                   okl_debug_pub.log_debug (g_level_statement,
3465                                            l_module_name,
3466                                               'In param, p_klev_tbl('
3467                                            || i
3468                                            || ').p_kle_id: '
3469                                            || p_klev_tbl (i).p_kle_id
3470                                           );
3471                   okl_debug_pub.log_debug (g_level_statement,
3472                                            l_module_name,
3473                                               'In param, p_klev_tbl('
3474                                            || i
3475                                            || ').p_asset_quantity: '
3476                                            || p_klev_tbl (i).p_asset_quantity
3477                                           );
3478                   okl_debug_pub.log_debug (g_level_statement,
3479                                            l_module_name,
3480                                               'In param, p_klev_tbl('
3481                                            || i
3482                                            || ').p_asset_name: '
3483                                            || p_klev_tbl (i).p_asset_name
3484                                           );
3485                   okl_debug_pub.log_debug (g_level_statement,
3486                                            l_module_name,
3487                                               'In param, p_klev_tbl('
3488                                            || i
3489                                            || ').p_quote_quantity: '
3490                                            || p_klev_tbl (i).p_quote_quantity
3491                                           );
3492                   okl_debug_pub.log_debug (g_level_statement,
3493                                            l_module_name,
3494                                               'In param, p_klev_tbl('
3495                                            || i
3496                                            || ').p_tql_id: '
3497                                            || p_klev_tbl (i).p_tql_id
3498                                           );
3499                   okl_debug_pub.log_debug (g_level_statement,
3500                                            l_module_name,
3501                                               'In param, p_klev_tbl('
3502                                            || i
3503                                            || ').p_split_kle_name: '
3504                                            || p_klev_tbl (i).p_split_kle_name
3505                                           );
3506                   okl_debug_pub.log_debug (g_level_statement,
3507                                            l_module_name,
3508                                               'In param, p_klev_tbl('
3509                                            || i
3510                                            || ').p_split_kle_id: '
3511                                            || p_klev_tbl (i).p_split_kle_id
3512                                           );
3513                END IF;
3514             END LOOP;
3515          END IF;
3516       END IF;
3517 
3518       -- for each line check if streams exists
3519       IF (p_klev_tbl.COUNT > 0)
3520       THEN
3521          i := p_klev_tbl.FIRST;
3522 
3523          LOOP
3524             -- check if streams exists
3525             OPEN k_streams_csr (p_klev_tbl (i).p_kle_id);
3526 
3527             FETCH k_streams_csr
3528              INTO l_id;
3529 
3530             IF k_streams_csr%FOUND
3531             THEN
3532                l_streams_found := g_yes;
3533             END IF;
3534 
3535             CLOSE k_streams_csr;
3536 
3537             EXIT WHEN (i = p_klev_tbl.LAST);
3538             i := p_klev_tbl.NEXT (i);
3539          END LOOP;
3540       END IF;
3541 
3542       -- if close streams need to be done
3543       IF    (    p_trn_already_set = g_yes
3544              AND NVL (px_tcnv_rec.tmt_streams_updated_yn, '?') <> g_yes
3545             )
3546          OR (p_trn_already_set = g_no)
3547       THEN
3548          -- if streams found then
3549          IF (l_streams_found = g_yes)
3550          THEN
3551             j := 1;
3552 
3553             -- Loop thru the lines table
3554             IF (p_klev_tbl.COUNT > 0)
3555             THEN
3556                i := p_klev_tbl.FIRST;
3557 
3558                LOOP
3559                   -- for each line's streams set the tbl type for streams pub
3560                   FOR k_streams_rec IN k_streams_csr (p_klev_tbl (i).p_kle_id)
3561                   LOOP
3562                      lp_stmv_tbl (j).khr_id := p_term_rec.p_contract_id;
3563                      lp_stmv_tbl (j).active_yn := g_no;
3564                      lp_stmv_tbl (j).ID := k_streams_rec.ID;
3565                      lp_stmv_tbl (j).kle_id := p_klev_tbl (i).p_kle_id;
3566                      -- RMUNJULU 28-MAR-03 2877278 Added code to set say_code to HIST
3567                      lp_stmv_tbl (j).say_code := 'HIST';
3568                      lp_stmv_tbl (j).date_history := SYSDATE;
3569                      j := j + 1;
3570                   END LOOP;
3571 
3572                   EXIT WHEN (i = p_klev_tbl.LAST);
3573                   i := p_klev_tbl.NEXT (i);
3574                END LOOP;
3575             END IF;
3576 
3577             IF (is_debug_statement_on)
3578             THEN
3579                okl_debug_pub.log_debug
3580                                     (g_level_statement,
3581                                      l_module_name,
3582                                      'calling OKL_STREAMS_PUB.update_streams'
3583                                     );
3584             END IF;
3585 
3586             -- close streams
3587             okl_streams_pub.update_streams
3588                                           (p_api_version        => l_api_version,
3589                                            p_init_msg_list      => g_false,
3590                                            x_return_status      => l_return_status,
3591                                            x_msg_count          => l_msg_count,
3592                                            x_msg_data           => l_msg_data,
3593                                            p_stmv_tbl           => lp_stmv_tbl,
3594                                            x_stmv_tbl           => lx_stmv_tbl
3595                                           );
3596 
3597             IF (is_debug_statement_on)
3598             THEN
3599                okl_debug_pub.log_debug
3600                   (g_level_statement,
3601                    l_module_name,
3602                       'called OKL_STREAMS_PUB.update_streams , return status: '
3603                    || l_return_status
3604                   );
3605             END IF;
3606 
3607             IF l_return_status <> g_ret_sts_success
3608             THEN
3609                -- Streams table update failed.
3610                okl_api.set_message (p_app_name      => g_app_name,
3611                                     p_msg_name      => 'OKL_AM_ERR_UPD_STREAMS');
3612             END IF;
3613 
3614             -- Raise exception to rollback to if error
3615             IF (l_return_status = g_ret_sts_unexp_error)
3616             THEN
3617                RAISE okl_api.g_exception_unexpected_error;
3618             ELSIF (l_return_status = g_ret_sts_error)
3619             THEN
3620                RAISE okl_api.g_exception_error;
3621             END IF;
3622 
3623             -- store the highest degree of error
3624             set_overall_status (p_return_status        => l_return_status,
3625                                 px_overall_status      => px_overall_status);
3626             -- set the transaction record
3627             set_transaction_rec (p_return_status       => l_return_status,
3628                                  p_overall_status      => px_overall_status,
3629                                  p_tmt_flag            => 'TMT_STREAMS_UPDATED_YN',
3630                                  p_tsu_code            => 'WORKING',
3631                                  px_tcnv_rec           => px_tcnv_rec
3632                                 );
3633          ELSE
3634             --( no streams found )
3635 
3636             -- No future billable streams found.
3637             okl_api.set_message (p_app_name      => g_app_name,
3638                                  p_msg_name      => 'OKL_AM_NO_STREAMS');
3639             -- set the transaction record
3640             set_transaction_rec (p_return_status       => l_return_status,
3641                                  p_overall_status      => px_overall_status,
3642                                  p_tmt_flag            => 'TMT_STREAMS_UPDATED_YN',
3643                                  p_tsu_code            => 'WORKING',
3644                                  p_ret_val             => NULL,
3645                                  px_tcnv_rec           => px_tcnv_rec
3646                                 );
3647          END IF;
3648       END IF;
3649 
3650       -- Set the return status
3651       x_return_status := l_return_status;
3652 
3653       IF (is_debug_procedure_on)
3654       THEN
3655          okl_debug_pub.log_debug (g_level_procedure,
3656                                   l_module_name,
3657                                   'End(-)'
3658                                  );
3659       END IF;
3660    EXCEPTION
3661       WHEN okl_api.g_exception_error
3662       THEN
3663          -- Close any open cursors
3664          IF k_streams_csr%ISOPEN
3665          THEN
3666             CLOSE k_streams_csr;
3667          END IF;
3668 
3669          ROLLBACK TO close_streams;
3670          x_return_status := g_ret_sts_error;
3671          -- store the highest degree of error
3672          set_overall_status (p_return_status        => x_return_status,
3673                              px_overall_status      => px_overall_status);
3674          -- set the transaction record
3675          set_transaction_rec (p_return_status       => x_return_status,
3676                               p_overall_status      => px_overall_status,
3677                               p_tmt_flag            => 'TMT_STREAMS_UPDATED_YN',
3678                               p_tsu_code            => 'ERROR',
3679                               px_tcnv_rec           => px_tcnv_rec
3680                              );
3681 
3682          IF (is_debug_exception_on)
3683          THEN
3684             okl_debug_pub.log_debug (g_level_exception,
3685                                      l_module_name,
3686                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
3687                                     );
3688          END IF;
3689       WHEN okl_api.g_exception_unexpected_error
3690       THEN
3691          -- Close any open cursors
3692          IF k_streams_csr%ISOPEN
3693          THEN
3694             CLOSE k_streams_csr;
3695          END IF;
3696 
3697          ROLLBACK TO close_streams;
3698          x_return_status := g_ret_sts_unexp_error;
3699          -- store the highest degree of error
3700          set_overall_status (p_return_status        => x_return_status,
3701                              px_overall_status      => px_overall_status);
3702          -- set the transaction record
3703          set_transaction_rec (p_return_status       => x_return_status,
3704                               p_overall_status      => px_overall_status,
3705                               p_tmt_flag            => 'TMT_STREAMS_UPDATED_YN',
3706                               p_tsu_code            => 'ERROR',
3707                               px_tcnv_rec           => px_tcnv_rec
3708                              );
3709 
3710          IF (is_debug_exception_on)
3711          THEN
3712             okl_debug_pub.log_debug (g_level_exception,
3713                                      l_module_name,
3714                                         'EXCEPTION :'
3715                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
3716                                     );
3717          END IF;
3718       WHEN OTHERS
3719       THEN
3720          -- Close any open cursors
3721          IF k_streams_csr%ISOPEN
3722          THEN
3723             CLOSE k_streams_csr;
3724          END IF;
3725 
3726          ROLLBACK TO close_streams;
3727          x_return_status := g_ret_sts_unexp_error;
3728          -- store the highest degree of error
3729          set_overall_status (p_return_status        => x_return_status,
3730                              px_overall_status      => px_overall_status);
3731          -- set the transaction record
3732          set_transaction_rec (p_return_status       => x_return_status,
3733                               p_overall_status      => px_overall_status,
3734                               p_tmt_flag            => 'TMT_STREAMS_UPDATED_YN',
3735                               p_tsu_code            => 'ERROR',
3736                               px_tcnv_rec           => px_tcnv_rec
3737                              );
3738          -- Set the oracle error message
3739          okl_api.set_message (p_app_name          => g_app_name_1,
3740                               p_msg_name          => g_unexpected_error,
3741                               p_token1            => g_sqlcode_token,
3742                               p_token1_value      => SQLCODE,
3743                               p_token2            => g_sqlerrm_token,
3744                               p_token2_value      => SQLERRM
3745                              );
3746 
3747          IF (is_debug_exception_on)
3748          THEN
3749             okl_debug_pub.log_debug (g_level_exception,
3750                                      l_module_name,
3751                                         'EXCEPTION :'
3752                                      || 'OTHERS, SQLCODE: '
3753                                      || SQLCODE
3754                                      || ' , SQLERRM : '
3755                                      || SQLERRM
3756                                     );
3757          END IF;
3758    END close_streams;
3759 
3760    -- Start of comments
3761    --
3762    -- Procedure Name : accounting_entries
3763    -- Desciption     : Does the accounting entries for the assets
3764    -- Business Rules :
3765    -- Parameters     :
3766    -- Version      : 1.0
3767    -- History        : RMUNJULU 23-DEC-02 2726739 Added code for Multi-Currency settings
3768    --                : RMUNJULU Bug # 3023206 27-JUN-03 Increment the line number before the exit
3769    --                  to avoid duplicate line numbers when creating accnting transaction lines
3770    --                : RMUNJULU Bug # 2902876 23-JUL-03 Added code to pass valid GL date to GET_TEMPLATE_INFO
3771    --                : RMUNJULU 3138794 Added code to pass kle_id to Accounting Engine
3772    --                : RMUNJULU 28-APR-04 3596626 Added code to set lp_acc_gen_primary_key_tbl
3773    --
3774    --                : SMODUGA  3061772 Added call to process_discount_subsidy
3775    --                : rmunjulu EDAT Added code to pass quote accpt date as accounting date
3776    --                  also passing the contract_id, quote_id, quote_accept_date and line_id to accounting engine
3777    --                : rmunjulu EDAT 29-Dec-04 did to_char to convert to right format
3778    --                : akrangan Bug 5514059 - During termination, do accounting for asset, fees and service
3779    --                : akrangan SLA Single Accounting Engine Call Uptake
3780    -- End of comments
3781    PROCEDURE accounting_entries (
3782       p_term_rec          IN              term_rec_type,
3783       p_sys_date          IN              DATE,
3784       p_klev_tbl          IN              klev_tbl_type,
3785       p_trn_already_set   IN              VARCHAR2,
3786       px_overall_status   IN OUT NOCOPY   VARCHAR2,
3787       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
3788       x_return_status     OUT NOCOPY      VARCHAR2
3789    )
3790    IS
3791       -- Cursor to get the product of the contract
3792       CURSOR prod_id_csr (
3793          p_khr_id   IN   NUMBER
3794       )
3795       IS
3796          SELECT khr.pdt_id pdt_id,
3797                 CHR.scs_code                               -- rmunjulu 4622198
3798                             ,
3799                 CHR.org_id                  --akrangan added for sla ae uptake
3800            FROM okl_k_headers_v khr,
3801                 okc_k_headers_b CHR                        -- rmunjulu 4622198
3802           WHERE khr.ID = p_khr_id AND khr.ID = CHR.ID;     -- rmunjulu 4622198
3803 
3804       -- Get the product type
3805       CURSOR l_product_type_csr (
3806          p_pdt_id   IN   NUMBER
3807       )
3808       IS
3809          SELECT prd.description description
3810            FROM okl_products_v prd
3811           WHERE prd.ID = p_pdt_id;
3812 
3813       l_return_status              VARCHAR2 (1)           := g_ret_sts_success;
3814       l_pdt_id                     NUMBER                                 := 0;
3815       l_try_id                     NUMBER;
3816       lp_tmpl_identify_rec         okl_account_dist_pub.tmpl_identify_rec_type;
3817       lp_dist_info_rec             okl_account_dist_pub.dist_info_rec_type;
3818       lp_ctxt_val_tbl              okl_account_dist_pub.ctxt_val_tbl_type;
3819       lp_acc_gen_primary_key_tbl   okl_account_dist_pub.acc_gen_primary_key;
3820       lx_template_tbl              okl_account_dist_pub.avlv_tbl_type;
3821       lx_amount_tbl                okl_account_dist_pub.amount_tbl_type;
3822       lx_tcnv_tbl                  okl_trx_contracts_pub.tcnv_tbl_type;
3823       lx_tclv_tbl                  okl_trx_contracts_pub.tclv_tbl_type;
3824       l_catchup_rec                okl_generate_accruals_pub.accrual_rec_type;
3825       l_lprv_rec                   okl_rev_loss_prov_pub.lprv_rec_type;
3826       l_trans_meaning              VARCHAR2 (200);
3827       l_module_name                VARCHAR2 (500)
3828                                       := g_module_name || 'accounting_entries';
3829       is_debug_exception_on        BOOLEAN
3830               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
3831       is_debug_procedure_on        BOOLEAN
3832               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
3833       is_debug_statement_on        BOOLEAN
3834               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
3835       lp_tclv_rec                  okl_trx_contracts_pub.tclv_rec_type;
3836       lx_tclv_rec                  okl_trx_contracts_pub.tclv_rec_type;
3837       li_tclv_rec                  okl_trx_contracts_pub.tclv_rec_type;
3838       i                            NUMBER;
3839       l_total_amount               NUMBER                                 := 0;
3840       lip_tmpl_identify_rec        okl_account_dist_pub.tmpl_identify_rec_type;
3841       lix_template_tbl             okl_account_dist_pub.avlv_tbl_type;
3842       lip_tcnv_rec                 tcnv_rec_type;
3843       lix_tcnv_rec                 tcnv_rec_type;
3844       l_product_type               VARCHAR2 (2000);
3845       l_line_number                NUMBER                                 := 1;
3846       j                            NUMBER;
3847       l_api_version       CONSTANT NUMBER                     := g_api_version;
3848       l_msg_count                  NUMBER                        := g_miss_num;
3849       l_msg_data                   VARCHAR2 (2000);
3850       -- RMUNJULU 23-DEC-02 2726739 Added variables
3851       l_functional_currency_code   VARCHAR2 (15);
3852       l_contract_currency_code     VARCHAR2 (15);
3853       l_currency_conversion_type   VARCHAR2 (30);
3854       l_currency_conversion_rate   NUMBER;
3855       l_currency_conversion_date   DATE;
3856       l_converted_amount           NUMBER;
3857       -- Since we do not use the amount or converted amount
3858       -- set a hardcoded value for the amount (and pass to to
3859       -- OKL_ACCOUNTING_UTIL.convert_to_functional_currency and get back
3860       -- conversion values )
3861       l_hard_coded_amount          NUMBER                               := 100;
3862       -- Bug 2902876
3863       l_valid_gl_date              DATE;
3864       -- rmunjulu EDAT
3865       l_quote_accpt_date           DATE;
3866       l_quote_eff_date             DATE;
3867       -- rmunjulu 4622198
3868       l_scs_code                   okc_k_headers_b.scs_code%TYPE;
3869       l_fact_synd_code             fnd_lookups.lookup_code%TYPE;
3870       l_inv_acct_code              okc_rules_b.rule_information1%TYPE;
3871 
3872       --akrangan Bug 5514059 start
3873       -- Used to get Assets selected for termination and
3874       -- Fee and Sold Service lines associated to the assets
3875       CURSOR k_asst_fee_srvc_lns_csr (
3876          p_chr_id   IN   okc_k_headers_b.ID%TYPE,
3877          p_qte_id   IN   okc_k_lines_b.ID%TYPE
3878       )
3879       IS
3880          SELECT cle.ID cle_id
3881            FROM okc_k_lines_b cle,
3882                 okl_txl_quote_lines_b tql
3883           WHERE tql.qte_id = p_qte_id
3884             AND tql.qlt_code = 'AMCFIA'
3885             AND tql.kle_id = cle.ID
3886          UNION
3887          SELECT DISTINCT hdrcle.ID cle_id
3888                     FROM okc_k_lines_b cle,
3889                          okc_k_lines_b hdrcle,
3890                          okc_k_items cim,
3891                          okl_txl_quote_lines_b tql,
3892                          okc_k_headers_b CHR,
3893                          okc_line_styles_b lse
3894                    WHERE hdrcle.chr_id = p_chr_id
3895                      AND cle.cle_id = hdrcle.ID
3896                      AND cim.dnz_chr_id = cle.dnz_chr_id
3897                      AND cim.jtot_object1_code = 'OKX_COVASST'
3898                      AND cim.cle_id = cle.ID
3899                      AND cim.object1_id1 = tql.kle_id
3900                      AND tql.qte_id = p_qte_id
3901                      AND tql.qlt_code = 'AMCFIA'
3902                      AND hdrcle.lse_id = lse.ID
3903                      AND lse.lty_code IN ('FEE', 'SOLD_SERVICE')
3904                      AND hdrcle.chr_id = CHR.ID
3905                      AND hdrcle.sts_code = CHR.sts_code;
3906 
3907       --akrangan Bug 5514059 end
3908       --akrangan sla single accounting call to ae uptake starts
3909       l_org_id                     NUMBER (15);
3910       --txl contracts specific tbl types
3911       l_tclv_tbl                   okl_trx_contracts_pub.tclv_tbl_type;
3912       --ae new table types declaration
3913       l_tmpl_identify_tbl          okl_account_dist_pvt.tmpl_identify_tbl_type;
3914       l_dist_info_tbl              okl_account_dist_pvt.dist_info_tbl_type;
3915       l_ctxt_tbl                   okl_account_dist_pvt.ctxt_tbl_type;
3916       l_template_out_tbl           okl_account_dist_pvt.avlv_out_tbl_type;
3917       l_amount_out_tbl             okl_account_dist_pvt.amount_out_tbl_type;
3918       l_acc_gen_tbl                okl_account_dist_pvt.acc_gen_tbl_type;
3919       l_tcn_id                     NUMBER;
3920 
3921       --hdr dff fields cursor
3922       --this cursor is to populate the
3923       -- desc flex fields columns in okl_trx_contracts
3924       CURSOR trx_contracts_dff_csr (
3925          p_khr_id   IN   NUMBER
3926       )
3927       IS
3928          SELECT attribute_category,
3929                 attribute1,
3930                 attribute2,
3931                 attribute3,
3932                 attribute4,
3933                 attribute5,
3934                 attribute6,
3935                 attribute7,
3936                 attribute8,
3937                 attribute9,
3938                 attribute10,
3939                 attribute11,
3940                 attribute12,
3941                 attribute13,
3942                 attribute14,
3943                 attribute15
3944            FROM okl_k_headers okl
3945           WHERE okl.ID = p_khr_id;
3946 
3947       --line dff fields cursor
3948       --this cursor is to populate the
3949       -- desc flex fields columns in okl_txl_xontract_lines_b
3950       CURSOR txl_contracts_dff_csr (
3951          p_kle_id   IN   NUMBER
3952       )
3953       IS
3954          SELECT attribute_category,
3955                 attribute1,
3956                 attribute2,
3957                 attribute3,
3958                 attribute4,
3959                 attribute5,
3960                 attribute6,
3961                 attribute7,
3962                 attribute8,
3963                 attribute9,
3964                 attribute10,
3965                 attribute11,
3966                 attribute12,
3967                 attribute13,
3968                 attribute14,
3969                 attribute15
3970            FROM okl_k_lines okl
3971           WHERE okl.ID = p_kle_id;
3972 
3973       --record for storing okl_k_lines dffs and linked assets cle_id
3974       TYPE dff_rec_type IS RECORD (
3975          attribute_category   okl_k_lines.attribute_category%TYPE,
3976          attribute1           okl_k_lines.attribute1%TYPE,
3977          attribute2           okl_k_lines.attribute2%TYPE,
3978          attribute3           okl_k_lines.attribute3%TYPE,
3979          attribute4           okl_k_lines.attribute4%TYPE,
3980          attribute5           okl_k_lines.attribute5%TYPE,
3981          attribute6           okl_k_lines.attribute6%TYPE,
3982          attribute7           okl_k_lines.attribute7%TYPE,
3983          attribute8           okl_k_lines.attribute8%TYPE,
3984          attribute9           okl_k_lines.attribute9%TYPE,
3985          attribute10          okl_k_lines.attribute10%TYPE,
3986          attribute11          okl_k_lines.attribute11%TYPE,
3987          attribute12          okl_k_lines.attribute12%TYPE,
3988          attribute13          okl_k_lines.attribute13%TYPE,
3989          attribute14          okl_k_lines.attribute14%TYPE,
3990          attribute15          okl_k_lines.attribute15%TYPE
3991       );
3992 
3993       txl_contracts_dff_rec        dff_rec_type;
3994 
3995       --product name and tax owner
3996       CURSOR product_name_csr (
3997          p_pdt_id   IN   NUMBER
3998       )
3999       IS
4000          SELECT NAME,
4001                 tax_owner
4002            FROM okl_product_parameters_v
4003           WHERE ID = p_pdt_id;
4004 
4005       l_currency_code              okl_trx_contracts.currency_code%TYPE;
4006       --loop variables
4007       k                            NUMBER;
4008       l                            NUMBER;
4009       m                            NUMBER;
4010    --akrangan sla single accounting call to ae uptake ends
4011    BEGIN
4012 
4013 
4014       IF (is_debug_procedure_on)
4015       THEN
4016          okl_debug_pub.log_debug (g_level_procedure,
4017                                   l_module_name,
4018                                   'Begin(+)'
4019                                  );
4020       END IF;
4021 
4022       IF (is_debug_statement_on)
4023       THEN
4024          okl_debug_pub.log_debug (g_level_statement,
4025                                   l_module_name,
4026                                   'In param, p_sys_date: ' || p_sys_date
4027                                  );
4028          okl_debug_pub.log_debug (g_level_statement,
4029                                   l_module_name,
4030                                      'In param, p_trn_already_set: '
4031                                   || p_trn_already_set
4032                                  );
4033          okl_debug_pub.log_debug (g_level_statement,
4034                                   l_module_name,
4035                                      'In param, px_overall_status: '
4036                                   || px_overall_status
4037                                  );
4038          okl_debug_pub.log_debug (g_level_statement,
4039                                   l_module_name,
4040                                      'In param, p_term_rec.p_quote_id: '
4041                                   || p_term_rec.p_quote_id
4042                                  );
4043          okl_debug_pub.log_debug (g_level_statement,
4044                                   l_module_name,
4045                                      'In param, p_term_rec.p_contract_id: '
4046                                   || p_term_rec.p_contract_id
4047                                  );
4048          okl_debug_pub.log_debug
4049                                 (g_level_statement,
4050                                  l_module_name,
4051                                     'In param, p_term_rec.p_contract_number: '
4052                                  || p_term_rec.p_contract_number
4053                                 );
4054          okl_debug_pub.log_debug (g_level_statement,
4055                                   l_module_name,
4056                                      'In param, px_tcnv_rec.try_id: '
4057                                   || px_tcnv_rec.try_id
4058                                  );
4059          okl_debug_pub.log_debug
4060                        (g_level_statement,
4061                         l_module_name,
4062                            'In param, px_tcnv_rec.tmt_accounting_entries_yn: '
4063                         || px_tcnv_rec.tmt_accounting_entries_yn
4064                        );
4065          okl_debug_pub.log_debug (g_level_statement,
4066                                   l_module_name,
4067                                      'In param, px_tcnv_rec.id: '
4068                                   || px_tcnv_rec.ID
4069                                  );
4070       END IF;
4071 
4072       -- Logic
4073 
4074       --Check the product id
4075       --If product_id NULL Then
4076       --   Error and Exit
4077       --End if
4078 
4079       --Get Templates for product ( get sty_id)
4080       --If no templates found Then
4081       --   Error and Exit
4082       --End If
4083 
4084       --Loop thru templates
4085       --   Loop thru lines
4086       --     Create TXL Line ( with kle_id)
4087       --     Do Accnt entries (get back amt)
4088       --     Update TXL Line (for amt)
4089       --     Increment total amt
4090       --     Increment line number
4091       --   End Loop
4092       --End Loop
4093       --Update TRX header with total amt
4094 
4095       -- Start savepoint for this block
4096       SAVEPOINT accounting_entries;
4097 
4098       -- get the product id
4099       OPEN prod_id_csr (p_term_rec.p_contract_id);
4100 
4101       FETCH prod_id_csr
4102        INTO l_pdt_id,
4103             l_scs_code,
4104             l_org_id;                                      -- rmunjulu 4622198
4105 
4106       CLOSE prod_id_csr;
4107 
4108       -- CHECK PRODUCT ID
4109 
4110       -- raise error if no pdt_id
4111       IF l_pdt_id IS NULL OR l_pdt_id = 0
4112       THEN
4113          -- Error: Unable to create accounting entries because of a missing
4114          -- Product Type for the contract CONTRACT_NUMBER.
4115          okl_api.set_message (p_app_name          => g_app_name,
4116                               p_msg_name          => 'OKL_AM_PRODUCT_ID_ERROR',
4117                               p_token1            => 'CONTRACT_NUMBER',
4118                               p_token1_value      => p_term_rec.p_contract_number
4119                              );
4120          RAISE okl_api.g_exception_error;
4121       END IF;
4122 
4123       -- get the product type
4124       OPEN l_product_type_csr (l_pdt_id);
4125 
4126       FETCH l_product_type_csr
4127        INTO l_product_type;
4128 
4129       CLOSE l_product_type_csr;
4130 
4131       -- If accounting entries needed
4132       IF    (    p_trn_already_set = g_yes
4133              AND NVL (px_tcnv_rec.tmt_accounting_entries_yn, '?') <> g_yes
4134             )
4135          OR (p_trn_already_set = g_no)
4136       THEN
4137          -- RMUNJULU 3018641 Step Message
4138          -- Step : Accounting Entries
4139          okl_api.set_message (p_app_name      => g_app_name,
4140                               p_msg_name      => 'OKL_AM_STEP_ACT');
4141 
4142          -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
4143 
4144          -- rmunjulu EDAT
4145          -- If quote exists then accnting date is quote accept date else sysdate
4146          IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
4147          THEN
4148             l_quote_accpt_date :=
4149                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
4150             l_quote_eff_date :=
4151                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
4152          ELSE
4153             l_quote_accpt_date := p_sys_date;
4154             l_quote_eff_date := p_sys_date;
4155          END IF;
4156 
4157          -- rmunjulu EDAT
4158          -- set the additional parameters with contract_id, quote_id and transaction_date
4159          -- to be passed to formula engine
4160          lp_ctxt_val_tbl (1).NAME := 'contract_id';
4161          lp_ctxt_val_tbl (1).VALUE := p_term_rec.p_contract_id;
4162          lp_ctxt_val_tbl (2).NAME := 'quote_id';
4163          lp_ctxt_val_tbl (2).VALUE := p_term_rec.p_quote_id;
4164          lp_ctxt_val_tbl (3).NAME := 'transaction_date';
4165          lp_ctxt_val_tbl (3).VALUE :=
4166                                     TO_CHAR (l_quote_accpt_date, 'MM/DD/YYYY');
4167              -- rmunjulu EDAT 29-Dec-04 did to_char to convert to right format
4168          -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
4169 
4170          -- GET TEMPLATES
4171 
4172          -- Get the meaning of lookup
4173          l_trans_meaning :=
4174             okl_am_util_pvt.get_lookup_meaning
4175                                 (p_lookup_type      => 'OKL_ACCOUNTING_EVENT_TYPE',
4176                                  p_lookup_code      => 'TERMINATION',
4177                                  p_validate_yn      => 'Y'
4178                                 );
4179          -- Set the tmpl_identify_rec in parameter to get
4180          -- accounting templates for the product
4181          lip_tmpl_identify_rec.product_id := l_pdt_id;
4182          lip_tmpl_identify_rec.transaction_type_id := px_tcnv_rec.try_id;
4183          lip_tmpl_identify_rec.memo_yn := g_no;
4184          lip_tmpl_identify_rec.prior_year_yn := g_no;
4185          -- Bug 2902876 Added to get the valid GL date
4186          l_valid_gl_date :=
4187             okl_accounting_util.get_valid_gl_date
4188                                               (p_gl_date      => l_quote_accpt_date);
4189                                                               -- rmunjulu EDAT
4190 
4191          IF (is_debug_statement_on)
4192          THEN
4193             okl_debug_pub.log_debug
4194                     (g_level_statement,
4195                      l_module_name,
4196                      'calling okl_securitization_pvt.check_khr_ia_associated'
4197                     );
4198          END IF;
4199 
4200          -- rmunjulu 4622198 SPECIAL_ACCNT Get special accounting details
4201          okl_securitization_pvt.check_khr_ia_associated
4202                                         (p_api_version         => l_api_version,
4203                                          p_init_msg_list       => okl_api.g_false,
4204                                          x_return_status       => l_return_status,
4205                                          x_msg_count           => l_msg_count,
4206                                          x_msg_data            => l_msg_data,
4207                                          p_khr_id              => p_term_rec.p_contract_id,
4208                                          p_scs_code            => l_scs_code,
4209                                          p_trx_date            => l_quote_accpt_date,
4210                                          x_fact_synd_code      => l_fact_synd_code,
4211                                          x_inv_acct_code       => l_inv_acct_code
4212                                         );
4213 
4214          IF (is_debug_statement_on)
4215          THEN
4216             okl_debug_pub.log_debug
4217                (g_level_statement,
4218                 l_module_name,
4219                    'called okl_securitization_pvt.check_khr_ia_associated , return status: '
4220                 || l_return_status
4221                );
4222          END IF;
4223 
4224          IF (l_return_status = g_ret_sts_unexp_error)
4225          THEN
4226             RAISE okl_api.g_exception_unexpected_error;
4227          ELSIF (l_return_status = g_ret_sts_error)
4228          THEN
4229             RAISE okl_api.g_exception_error;
4230          END IF;
4231 
4232          -- rmunjulu 4622198 SPECIAL_ACCNT set the special accounting parameters
4233          lip_tmpl_identify_rec.factoring_synd_flag := l_fact_synd_code;
4234          lip_tmpl_identify_rec.investor_code := l_inv_acct_code;
4235 
4236          IF (is_debug_statement_on)
4237          THEN
4238             okl_debug_pub.log_debug
4239                             (g_level_statement,
4240                              l_module_name,
4241                              'calling okl_account_dist_pub.get_template_info'
4242                             );
4243          END IF;
4244 
4245          -- Get the accounting templates
4246          okl_account_dist_pub.get_template_info
4247                                 (p_api_version            => l_api_version,
4248                                  p_init_msg_list          => g_false,
4249                                  x_return_status          => l_return_status,
4250                                  x_msg_count              => l_msg_count,
4251                                  x_msg_data               => l_msg_data,
4252                                  p_tmpl_identify_rec      => lip_tmpl_identify_rec,
4253                                  x_template_tbl           => lix_template_tbl,
4254                                  p_validity_date          => l_valid_gl_date
4255                                 );  -- Bug 2902876 Added to pass valid GL date
4256 
4257          IF (is_debug_statement_on)
4258          THEN
4259             okl_debug_pub.log_debug
4260                (g_level_statement,
4261                 l_module_name,
4262                    'called okl_account_dist_pub.get_template_info , return status: '
4263                 || l_return_status
4264                );
4265          END IF;
4266 
4267          IF l_return_status <> g_ret_sts_success
4268          THEN
4269             -- No accounting templates found matching the transaction type
4270             -- TRX_TYPE and product  PRODUCT.
4271             okl_api.set_message (p_app_name          => g_app_name,
4272                                  p_msg_name          => 'OKL_AM_NO_ACC_TEMPLATES',
4273                                  p_token1            => 'TRX_TYPE',
4274                                  p_token1_value      => l_trans_meaning,
4275                                  p_token2            => 'PRODUCT',
4276                                  p_token2_value      => l_product_type
4277                                 );
4278          END IF;
4279 
4280          IF (l_return_status = g_ret_sts_unexp_error)
4281          THEN
4282             RAISE okl_api.g_exception_unexpected_error;
4283          ELSIF (l_return_status = g_ret_sts_error)
4284          THEN
4285             RAISE okl_api.g_exception_error;
4286          END IF;
4287 
4288          -- If no templates present
4289          IF lix_template_tbl.COUNT = 0
4290          THEN
4291             -- No accounting templates found matching the transaction type
4292             -- TRX_TYPE and product  PRODUCT.
4293             okl_api.set_message (p_app_name          => g_app_name,
4294                                  p_msg_name          => 'OKL_AM_NO_ACC_TEMPLATES',
4295                                  p_token1            => 'TRX_TYPE',
4296                                  p_token1_value      => l_trans_meaning,
4297                                  p_token2            => 'PRODUCT',
4298                                  p_token2_value      => l_product_type
4299                                 );
4300             RAISE okl_api.g_exception_error;
4301          END IF;
4302 
4303 -- ******************
4304 -- CURRENCY CONVERSION OPERATIONS
4305 -- ******************
4306          l_currency_code :=
4307                    okl_am_util_pvt.get_chr_currency (p_term_rec.p_contract_id);
4308          l_functional_currency_code :=
4309                                     okl_am_util_pvt.get_functional_currency
4310                                                                            ();
4311 
4312          --akrangan Bug 6147049 code fix start
4313          --call functional currency conversion only
4314          --if functional currency and contract currency are not same
4315          IF     l_functional_currency_code IS NOT NULL
4316             AND l_functional_currency_code <> l_currency_code
4317          THEN
4318             --akrangan Bug 6147049 code fix end
4319             -- Get the currency conversion details from ACCOUNTING_Util
4320             IF (is_debug_statement_on)
4321             THEN
4322                okl_debug_pub.log_debug
4323                   (g_level_statement,
4324                    l_module_name,
4325                    'calling okl_accounting_util.convert_to_functional_currency'
4326                   );
4327             END IF;
4328 
4329             okl_accounting_util.convert_to_functional_currency
4330                     (p_khr_id                        => p_term_rec.p_contract_id,
4331                      p_to_currency                   => l_functional_currency_code,
4332                      p_transaction_date              => l_quote_accpt_date,
4333                                                               -- rmunjulu EDAT
4334                      p_amount                        => l_hard_coded_amount,
4335                      x_return_status                 => l_return_status,
4336                      x_contract_currency             => l_contract_currency_code,
4337                      x_currency_conversion_type      => l_currency_conversion_type,
4338                      x_currency_conversion_rate      => l_currency_conversion_rate,
4339                      x_currency_conversion_date      => l_currency_conversion_date,
4340                      x_converted_amount              => l_converted_amount
4341                     );
4342 
4343             IF (is_debug_statement_on)
4344             THEN
4345                okl_debug_pub.log_debug
4346                   (g_level_statement,
4347                    l_module_name,
4348                       'called okl_accounting_util.convert_to_functional_currency , return status: '
4349                    || l_return_status
4350                   );
4351                okl_debug_pub.log_debug (g_level_statement,
4352                                         l_module_name,
4353                                            'l_contract_currency_code: '
4354                                         || l_contract_currency_code
4355                                        );
4356                okl_debug_pub.log_debug (g_level_statement,
4357                                         l_module_name,
4358                                            'l_currency_conversion_type: '
4359                                         || l_currency_conversion_type
4360                                        );
4361                okl_debug_pub.log_debug (g_level_statement,
4362                                         l_module_name,
4363                                            'l_currency_conversion_rate: '
4364                                         || l_currency_conversion_rate
4365                                        );
4366                okl_debug_pub.log_debug (g_level_statement,
4367                                         l_module_name,
4368                                            'l_currency_conversion_date: '
4369                                         || l_currency_conversion_date
4370                                        );
4371                okl_debug_pub.log_debug (g_level_statement,
4372                                         l_module_name,
4373                                            'l_converted_amount: '
4374                                         || l_converted_amount
4375                                        );
4376             END IF;
4377 
4378             -- If error from OKL_ACCOUNTING_UTIL
4379             IF l_return_status <> okl_api.g_ret_sts_success
4380             THEN
4381                -- Error occurred when creating accounting entries for
4382                -- transaction TRX_TYPE.
4383                okl_api.set_message (p_app_name          => g_app_name,
4384                                     p_msg_name          => 'OKL_AM_ERR_ACC_ENT',
4385                                     p_token1            => 'TRX_TYPE',
4386                                     p_token1_value      => l_trans_meaning
4387                                    );
4388             END IF;
4389 
4390             -- Raise exception to rollback to savepoint for this block
4391             IF (l_return_status = okl_api.g_ret_sts_unexp_error)
4392             THEN
4393                RAISE okl_api.g_exception_unexpected_error;
4394             ELSIF (l_return_status = okl_api.g_ret_sts_error)
4395             THEN
4396                RAISE okl_api.g_exception_error;
4397             END IF;
4398          --akrangan Bug 6147049 code fix start
4399          END IF;
4400 
4401 --akrangan Bug 6147049 code fix end
4402 -- *****************************
4403 -- CREATE TXL_CNTRCT LINES
4404 -- ****************************
4405 -- currency operations related variables assigned
4406          --Start fix for bug 7204083
4407          l_currency_code := NVL(l_contract_currency_code, l_currency_code);
4408          --End fix for bug 7204083
4409          j := 1;
4410          --looping thru the templates to set line records and template identify tbl
4411          i := lix_template_tbl.FIRST;
4412                         -- at this point we know that there are some templates
4413 
4414          LOOP
4415             -- Loop thru templates
4416             FOR k_asst_fee_srvc_lns_rec IN
4417                k_asst_fee_srvc_lns_csr (p_term_rec.p_contract_id,
4418                                         p_term_rec.p_quote_id)
4419             LOOP
4420                -- set the TXL_CNTRCT Line details for template
4421                l_tclv_tbl (j).line_number := l_line_number;
4422                l_tclv_tbl (j).khr_id := p_term_rec.p_contract_id;
4423                l_tclv_tbl (j).tcn_id := px_tcnv_rec.ID;
4424                l_tclv_tbl (j).sty_id := lix_template_tbl (i).sty_id;
4425                l_tclv_tbl (j).tcl_type := 'ALT';
4426                l_tclv_tbl (j).currency_code := l_currency_code;
4427                l_tclv_tbl (j).kle_id := k_asst_fee_srvc_lns_rec.cle_id;
4428                l_tclv_tbl (j).org_id := l_org_id;
4429 
4430                FOR txl_contracts_dff_rec IN
4431                   txl_contracts_dff_csr (k_asst_fee_srvc_lns_rec.cle_id)
4432                LOOP
4433                   --set dffs
4434                   l_tclv_tbl (j).attribute_category :=
4435                                      txl_contracts_dff_rec.attribute_category;
4436                   l_tclv_tbl (j).attribute1 :=
4437                                              txl_contracts_dff_rec.attribute1;
4438                   l_tclv_tbl (j).attribute2 :=
4439                                              txl_contracts_dff_rec.attribute2;
4440                   l_tclv_tbl (j).attribute3 :=
4441                                              txl_contracts_dff_rec.attribute3;
4442                   l_tclv_tbl (j).attribute4 :=
4443                                              txl_contracts_dff_rec.attribute4;
4444                   l_tclv_tbl (j).attribute5 :=
4445                                              txl_contracts_dff_rec.attribute5;
4446                   l_tclv_tbl (j).attribute6 :=
4447                                              txl_contracts_dff_rec.attribute6;
4448                   l_tclv_tbl (j).attribute7 :=
4449                                              txl_contracts_dff_rec.attribute7;
4450                   l_tclv_tbl (j).attribute8 :=
4451                                              txl_contracts_dff_rec.attribute8;
4452                   l_tclv_tbl (j).attribute9 :=
4453                                              txl_contracts_dff_rec.attribute9;
4454                   l_tclv_tbl (j).attribute10 :=
4455                                             txl_contracts_dff_rec.attribute10;
4456                   l_tclv_tbl (j).attribute11 :=
4457                                             txl_contracts_dff_rec.attribute11;
4458                   l_tclv_tbl (j).attribute12 :=
4459                                             txl_contracts_dff_rec.attribute12;
4460                   l_tclv_tbl (j).attribute13 :=
4461                                             txl_contracts_dff_rec.attribute13;
4462                   l_tclv_tbl (j).attribute14 :=
4463                                             txl_contracts_dff_rec.attribute14;
4464                   l_tclv_tbl (j).attribute15 :=
4465                                             txl_contracts_dff_rec.attribute15;
4466                END LOOP;
4467 
4468                -- This will calculate the amount and generate accounting entries
4469                -- Set the tmpl_identify_tbl in parameter
4470                l_tmpl_identify_tbl (j).product_id := l_pdt_id;
4471                l_tmpl_identify_tbl (j).transaction_type_id :=
4472                                                             px_tcnv_rec.try_id;
4473                l_tmpl_identify_tbl (j).memo_yn := g_no;
4474                l_tmpl_identify_tbl (j).prior_year_yn := g_no;
4475                l_tmpl_identify_tbl (j).stream_type_id :=
4476                                                    lix_template_tbl (i).sty_id;
4477                l_tmpl_identify_tbl (j).advance_arrears :=
4478                                           lix_template_tbl (i).advance_arrears;
4479                l_tmpl_identify_tbl (j).factoring_synd_flag :=
4480                                       lix_template_tbl (i).factoring_synd_flag;
4481                l_tmpl_identify_tbl (j).investor_code :=
4482                                                  lix_template_tbl (i).inv_code;
4483                l_tmpl_identify_tbl (j).syndication_code :=
4484                                                  lix_template_tbl (i).syt_code;
4485                l_tmpl_identify_tbl (j).factoring_code :=
4486                                                  lix_template_tbl (i).fac_code;
4487                --increment looping variable
4488                j := j + 1;
4489                --increment line number
4490                l_line_number := l_line_number + 1;
4491             END LOOP;
4492 
4493             EXIT WHEN (i = lix_template_tbl.LAST);
4494             i := lix_template_tbl.NEXT (i);
4495          END LOOP;
4496 
4497          IF (is_debug_statement_on)
4498          THEN
4499             okl_debug_pub.log_debug
4500                      (g_level_statement,
4501                       l_module_name,
4502                       'calling okl_trx_contracts_pub.create_trx_cntrct_lines'
4503                      );
4504          END IF;
4505 
4506          --create trx contract lines table
4507          okl_trx_contracts_pub.create_trx_cntrct_lines
4508                                           (p_api_version        => l_api_version,
4509                                            p_init_msg_list      => g_false,
4510                                            x_return_status      => l_return_status,
4511                                            x_msg_count          => l_msg_count,
4512                                            x_msg_data           => l_msg_data,
4513                                            p_tclv_tbl           => l_tclv_tbl,
4514                                            x_tclv_tbl           => lx_tclv_tbl
4515                                           );
4516 
4517          IF (is_debug_statement_on)
4518          THEN
4519             okl_debug_pub.log_debug
4520                (g_level_statement,
4521                 l_module_name,
4522                    'called okl_trx_contracts_pub.create_trx_cntrct_lines , return status: '
4523                 || l_return_status
4524                );
4525          END IF;
4526 
4527          -- If error inserting line then set message
4528          IF l_return_status <> okl_api.g_ret_sts_success
4529          THEN
4530             -- Error occurred when creating accounting entries for transaction TRX_TYPE.
4531             okl_api.set_message (p_app_name          => g_app_name,
4532                                  p_msg_name          => 'OKL_AM_ERR_ACC_ENT',
4533                                  p_token1            => 'TRX_TYPE',
4534                                  p_token1_value      => l_trans_meaning
4535                                 );
4536          END IF;
4537 
4538          -- Raise exception to rollback to savepoint for this block
4539          IF (l_return_status = okl_api.g_ret_sts_unexp_error)
4540          THEN
4541             RAISE okl_api.g_exception_unexpected_error;
4542          ELSIF (l_return_status = okl_api.g_ret_sts_error)
4543          THEN
4544             RAISE okl_api.g_exception_error;
4545          END IF;
4546 
4547          --setting the input table type to the obtained outout table type
4548          l_tclv_tbl := lx_tclv_tbl;
4549 
4550 -- ***************************
4551 -- POPULATE ACC GEN PRIMARY KEY TABLE
4552 -- ******************************
4553 -- added code to set lp_acc_gen_primary_key_tbl
4554 -- for account generator
4555 -- *********************************************
4556 -- Accounting Engine Call
4557 -- *********************************************
4558 --txl contracts loop
4559 -- udhenuko Bug 6685693 Start. Moving the loop so that the accounting transactions
4560 -- includes fees service lines for the partially terminated asset.
4561          IF l_tclv_tbl.COUNT <> 0
4562          THEN
4563             i := l_tclv_tbl.FIRST;
4564 
4565             LOOP
4566                IF (is_debug_statement_on)
4567                THEN
4568                   okl_debug_pub.log_debug
4569                              (g_level_statement,
4570                               l_module_name,
4571                               'calling okl_acc_call_pvt.okl_populate_acc_gen'
4572                              );
4573                END IF;
4574 
4575                okl_acc_call_pvt.okl_populate_acc_gen
4576                                  (p_contract_id           => p_term_rec.p_contract_id,
4577                                   p_contract_line_id      => l_tclv_tbl (i).kle_id,
4578                                   x_acc_gen_tbl           => lp_acc_gen_primary_key_tbl,
4579                                   x_return_status         => l_return_status
4580                                  );
4581 
4582                -- udhenuko Bug 6685693 Bug end.
4583                IF (is_debug_statement_on)
4584                THEN
4585                   okl_debug_pub.log_debug
4586                      (g_level_statement,
4587                       l_module_name,
4588                          'called okl_acc_call_pvt.okl_populate_acc_gen , return status: '
4589                       || l_return_status
4590                      );
4591                END IF;
4592 
4593                IF l_return_status <> okl_api.g_ret_sts_success
4594                THEN
4595                   -- Error occurred when creating accounting entries for transaction TRX_TYPE.
4596                   okl_api.set_message (p_app_name          => g_app_name,
4597                                        p_msg_name          => 'OKL_AM_ERR_ACC_ENT',
4598                                        p_token1            => 'TRX_TYPE',
4599                                        p_token1_value      => l_trans_meaning
4600                                       );
4601                END IF;
4602 
4603                -- Raise exception to rollback to savepoint for this block
4604                IF (l_return_status = okl_api.g_ret_sts_unexp_error)
4605                THEN
4606                   RAISE okl_api.g_exception_unexpected_error;
4607                ELSIF (l_return_status = okl_api.g_ret_sts_error)
4608                THEN
4609                   RAISE okl_api.g_exception_error;
4610                END IF;
4611 
4612                --Assigning the account generator table
4613                l_acc_gen_tbl (i).acc_gen_key_tbl := lp_acc_gen_primary_key_tbl;
4614                l_acc_gen_tbl (i).source_id := l_tclv_tbl (i).ID;
4615                --populating dist info tbl
4616                l_dist_info_tbl (i).source_id := l_tclv_tbl (i).ID;
4617                l_dist_info_tbl (i).source_table := 'OKL_TXL_CNTRCT_LNS';
4618                l_dist_info_tbl (i).accounting_date := l_quote_accpt_date;
4619                l_dist_info_tbl (i).gl_reversal_flag := g_no;
4620                l_dist_info_tbl (i).post_to_gl := g_yes;
4621                l_dist_info_tbl (i).contract_id := l_tclv_tbl (i).khr_id;
4622                l_dist_info_tbl (i).contract_line_id := l_tclv_tbl (i).kle_id;
4623                l_dist_info_tbl (i).currency_code := l_currency_code;
4624 
4625                IF (    (l_functional_currency_code IS NOT NULL)
4626                    AND (l_currency_code <> l_functional_currency_code)
4627                   )
4628                THEN
4629                   l_dist_info_tbl (i).currency_conversion_rate :=
4630                                                    l_currency_conversion_rate;
4631                   l_dist_info_tbl (i).currency_conversion_type :=
4632                                                    l_currency_conversion_type;
4633                   l_dist_info_tbl (i).currency_conversion_date :=
4634                                                    l_currency_conversion_date;
4635                END IF;
4636 
4637                --form context val table
4638                IF lp_ctxt_val_tbl.COUNT > 0
4639                THEN
4640                   l_ctxt_tbl (i).ctxt_val_tbl := lp_ctxt_val_tbl;
4641                   l_ctxt_tbl (i).source_id := l_tclv_tbl (i).ID;
4642                END IF;
4643 
4644                EXIT WHEN i = l_tclv_tbl.LAST;
4645                i := l_tclv_tbl.NEXT (i);
4646             END LOOP;
4647          END IF;
4648 
4649          l_tcn_id := px_tcnv_rec.ID;
4650 
4651          -- call accounting engine
4652          -- This will calculate the amount and generate accounting entries
4653          IF (is_debug_statement_on)
4654          THEN
4655             okl_debug_pub.log_debug
4656                        (g_level_statement,
4657                         l_module_name,
4658                         'calling okl_account_dist_pvt.create_accounting_dist'
4659                        );
4660          END IF;
4661 
4662          okl_account_dist_pvt.create_accounting_dist
4663                                   (p_api_version                  => l_api_version,
4664                                    p_init_msg_list                => g_false,
4665                                    x_return_status                => l_return_status,
4666                                    x_msg_count                    => l_msg_count,
4667                                    x_msg_data                     => l_msg_data,
4668                                    p_tmpl_identify_tbl            => l_tmpl_identify_tbl,
4669                                    p_dist_info_tbl                => l_dist_info_tbl,
4670                                    p_ctxt_val_tbl                 => l_ctxt_tbl,
4671                                    p_acc_gen_primary_key_tbl      => l_acc_gen_tbl,
4672                                    x_template_tbl                 => l_template_out_tbl,
4673                                    x_amount_tbl                   => l_amount_out_tbl,
4674                                    p_trx_header_id                => l_tcn_id
4675                                   );
4676 
4677          IF (is_debug_statement_on)
4678          THEN
4679             okl_debug_pub.log_debug
4680                (g_level_statement,
4681                 l_module_name,
4682                    'called okl_account_dist_pvt.create_accounting_dist , return status: '
4683                 || l_return_status
4684                );
4685          END IF;
4686 
4687          IF l_amount_out_tbl.COUNT = 0
4688          THEN
4689             l_return_status := okl_api.g_ret_sts_error;
4690          END IF;
4691 
4692          IF l_return_status <> okl_api.g_ret_sts_success
4693          THEN
4694             -- Error occurred when creating accounting entries for transaction TRX_TYPE.
4695             okl_api.set_message (p_app_name          => g_app_name,
4696                                  p_msg_name          => 'OKL_AM_ERR_ACC_ENT',
4697                                  p_token1            => 'TRX_TYPE',
4698                                  p_token1_value      => l_trans_meaning
4699                                 );
4700          END IF;
4701 
4702          -- Raise exception to rollback to savepoint for this block
4703          IF (l_return_status = okl_api.g_ret_sts_unexp_error)
4704          THEN
4705             RAISE okl_api.g_exception_unexpected_error;
4706          ELSIF (l_return_status = okl_api.g_ret_sts_error)
4707          THEN
4708             RAISE okl_api.g_exception_error;
4709          END IF;
4710 
4711 -- ******************************************************
4712 --   Update Trx Contracts with Header and Line Amounts
4713 -- ******************************************************
4714 
4715          --call the update trx contract api to update amount per stream type
4716          lip_tcnv_rec := px_tcnv_rec;
4717          --set all the necessary attributes of the record type
4718          lip_tcnv_rec.amount := 0;
4719          lip_tcnv_rec.set_of_books_id :=
4720                                     okl_accounting_util.get_set_of_books_id
4721                                                                            ();
4722          lip_tcnv_rec.org_id := l_org_id;
4723          --akrangan bug 6147049 fix start
4724          lip_tcnv_rec.currency_conversion_rate := l_currency_conversion_rate;
4725          lip_tcnv_rec.currency_conversion_type := l_currency_conversion_type;
4726          lip_tcnv_rec.currency_conversion_date := l_currency_conversion_date;
4727          --akrangan bug 6147049 fix end
4728          --akrangan bug 6215707 fix start
4729          lip_tcnv_rec.tsu_code := 'PROCESSED';
4730 
4731          --akrangan bug 6215707 fix end
4732          --product name and tax owner code
4733          OPEN product_name_csr (l_pdt_id);
4734 
4735          FETCH product_name_csr
4736           INTO lip_tcnv_rec.product_name,
4737                lip_tcnv_rec.tax_owner_code;
4738 
4739          CLOSE product_name_csr;
4740 
4741          --trx contracts hdr dffs
4742          OPEN trx_contracts_dff_csr (p_term_rec.p_contract_id);
4743 
4744          FETCH trx_contracts_dff_csr
4745           INTO lip_tcnv_rec.attribute_category,
4746                lip_tcnv_rec.attribute1,
4747                lip_tcnv_rec.attribute2,
4748                lip_tcnv_rec.attribute3,
4749                lip_tcnv_rec.attribute4,
4750                lip_tcnv_rec.attribute5,
4751                lip_tcnv_rec.attribute6,
4752                lip_tcnv_rec.attribute7,
4753                lip_tcnv_rec.attribute8,
4754                lip_tcnv_rec.attribute9,
4755                lip_tcnv_rec.attribute10,
4756                lip_tcnv_rec.attribute11,
4757                lip_tcnv_rec.attribute12,
4758                lip_tcnv_rec.attribute13,
4759                lip_tcnv_rec.attribute14,
4760                lip_tcnv_rec.attribute15;
4761 
4762          CLOSE trx_contracts_dff_csr;
4763 
4764          IF (l_tclv_tbl.COUNT) > 0 AND (l_amount_out_tbl.COUNT > 0)
4765          THEN
4766             k := l_tclv_tbl.FIRST;
4767             m := l_amount_out_tbl.FIRST;
4768 
4769             LOOP
4770                l_tclv_tbl (k).amount := 0;
4771 
4772                IF l_tclv_tbl (k).ID = l_amount_out_tbl (m).source_id
4773                THEN
4774                   lx_amount_tbl := l_amount_out_tbl (m).amount_tbl;
4775                   lx_template_tbl := l_template_out_tbl (m).template_tbl;
4776 
4777                   IF (lx_amount_tbl.COUNT <> 1 OR lx_template_tbl.COUNT <> 1
4778                      )
4779                   THEN
4780                      --raise error
4781                      l_return_status := okl_api.g_ret_sts_error;
4782                      -- Error occurred when creating accounting entries for transaction TRX_TYPE.
4783                      okl_api.set_message (p_app_name          => g_app_name,
4784                                           p_msg_name          => 'OKL_AM_ERR_ACC_ENT',
4785                                           p_token1            => 'TRX_TYPE',
4786                                           p_token1_value      => l_trans_meaning
4787                                          );
4788                      -- Raise exception to rollback to savepoint for this block
4789                      RAISE okl_api.g_exception_error;
4790                   ELSE
4791                      l := lx_amount_tbl.FIRST;
4792                      --update line amount
4793                      l_tclv_tbl (k).amount := NVL (lx_amount_tbl (l), 0);
4794                   END IF;
4795                END IF;
4796 
4797                --update total header amount
4798                lip_tcnv_rec.amount :=
4799                                    lip_tcnv_rec.amount + l_tclv_tbl (k).amount;
4800                EXIT WHEN k = l_tclv_tbl.LAST OR m = l_amount_out_tbl.LAST;
4801                k := l_tclv_tbl.NEXT (k);
4802                m := l_amount_out_tbl.NEXT (m);
4803             END LOOP;
4804          END IF;
4805 
4806          IF (is_debug_statement_on)
4807          THEN
4808             okl_debug_pub.log_debug
4809                         (g_level_statement,
4810                          l_module_name,
4811                          'calling okl_trx_contracts_pub.update_trx_contracts'
4812                         );
4813          END IF;
4814 
4815          --call the api to update trx contracts hdr and lines
4816          okl_trx_contracts_pub.update_trx_contracts
4817                                           (p_api_version        => l_api_version,
4818                                            p_init_msg_list      => g_false,
4819                                            x_return_status      => l_return_status,
4820                                            x_msg_count          => l_msg_count,
4821                                            x_msg_data           => l_msg_data,
4822                                            p_tcnv_rec           => lip_tcnv_rec,
4823                                            p_tclv_tbl           => l_tclv_tbl,
4824                                            x_tcnv_rec           => lix_tcnv_rec,
4825                                            x_tclv_tbl           => lx_tclv_tbl
4826                                           );
4827 
4828          IF (is_debug_statement_on)
4829          THEN
4830             okl_debug_pub.log_debug
4831                (g_level_statement,
4832                 l_module_name,
4833                    'called okl_trx_contracts_pub.update_trx_contracts , return status: '
4834                 || l_return_status
4835                );
4836          END IF;
4837 
4838          --handle exception
4839          IF l_return_status <> okl_api.g_ret_sts_success
4840          THEN
4841             -- Error occurred when creating accounting entries for transaction TRX_TYPE.
4842             okl_api.set_message (p_app_name          => g_app_name,
4843                                  p_msg_name          => 'OKL_AM_ERR_ACC_ENT',
4844                                  p_token1            => 'TRX_TYPE',
4845                                  p_token1_value      => l_trans_meaning
4846                                 );
4847          END IF;
4848 
4849          -- Raise exception to rollback to savepoint for this block
4850          IF (l_return_status = okl_api.g_ret_sts_unexp_error)
4851          THEN
4852             RAISE okl_api.g_exception_unexpected_error;
4853          ELSIF (l_return_status = okl_api.g_ret_sts_error)
4854          THEN
4855             RAISE okl_api.g_exception_error;
4856          END IF;
4857 
4858          -- Set the return record
4859          px_tcnv_rec := lix_tcnv_rec;
4860 
4861         OKL_MULTIGAAP_ENGINE_PVT.CREATE_SEC_REP_TRX
4862                            (p_api_version => l_api_version
4863                            ,p_init_msg_list => g_false
4864                            ,x_return_status => l_return_status
4865                            ,x_msg_count => l_msg_count
4866                            ,x_msg_data => l_msg_data
4867                            ,P_TCNV_REC => lip_tcnv_rec
4868                            ,P_TCLV_TBL => l_tclv_tbl
4869                            ,p_ctxt_val_tbl => l_ctxt_tbl
4870                            ,p_acc_gen_primary_key_tbl => lp_acc_gen_primary_key_tbl);
4871 
4872         IF (l_return_status = Okl_Api.G_RET_STS_UNEXP_ERROR) THEN
4873            RAISE Okl_Api.G_EXCEPTION_UNEXPECTED_ERROR;
4874         ELSIF (l_return_status = Okl_Api.G_RET_STS_ERROR) THEN
4875            RAISE Okl_Api.G_EXCEPTION_ERROR;
4876         END IF;
4877 
4878          -- Bug 3061772
4879          -- Call to Process dicount and Subsidy during acceptance of a termination quote
4880          IF (is_debug_statement_on)
4881          THEN
4882             okl_debug_pub.log_debug
4883                (g_level_statement,
4884                 l_module_name,
4885                 'calling okl_am_lease_loan_trmnt_pvt.process_discount_subsidy'
4886                );
4887          END IF;
4888 
4889          okl_am_lease_loan_trmnt_pvt.process_discount_subsidy
4890                                           (p_api_version           => l_api_version,
4891                                            p_init_msg_list         => okl_api.g_false,
4892                                            x_return_status         => l_return_status,
4893                                            x_msg_count             => l_msg_count,
4894                                            x_msg_data              => l_msg_data,
4895                                            p_term_rec              => p_term_rec,
4896                                            p_call_origin           => 'PARTIAL',
4897                                            p_termination_date      => p_sys_date
4898                                           );
4899 
4900          IF (is_debug_statement_on)
4901          THEN
4902             okl_debug_pub.log_debug
4903                (g_level_statement,
4904                 l_module_name,
4905                    'called okl_am_lease_loan_trmnt_pvt.process_discount_subsidy , return status: '
4906                 || l_return_status
4907                );
4908          END IF;
4909 
4910          IF l_return_status <> okl_api.g_ret_sts_success
4911          THEN
4912             okl_api.set_message (p_app_name      => g_app_name,
4913                                  p_msg_name      => 'OKL_AM_SUBSIDY_PROC_FAIL');
4914          END IF;
4915 
4916          IF (l_return_status = g_ret_sts_unexp_error)
4917          THEN
4918             RAISE okl_api.g_exception_unexpected_error;
4919          ELSIF (l_return_status = g_ret_sts_error)
4920          THEN
4921             RAISE okl_api.g_exception_error;
4922          END IF;
4923 
4924          -- SUCCESS MESSAGES
4925 
4926          -- Set success messages here
4927          -- Get the meaning of lookup
4928          l_trans_meaning :=
4929             okl_am_util_pvt.get_lookup_meaning
4930                                 (p_lookup_type      => 'OKL_ACCOUNTING_EVENT_TYPE',
4931                                  p_lookup_code      => 'TERMINATION',
4932                                  p_validate_yn      => 'Y'
4933                                 );
4934          -- Accounting entries created for transaction type TRX_TYPE.
4935          okl_api.set_message (p_app_name          => g_app_name,
4936                               p_msg_name          => 'OKL_AM_ACC_ENT_CREATED',
4937                               p_token1            => 'TRX_TYPE',
4938                               p_token1_value      => l_trans_meaning
4939                              );
4940          -- store the highest degree of error
4941          set_overall_status (p_return_status        => l_return_status,
4942                              px_overall_status      => px_overall_status);
4943          -- set the transaction record
4944          set_transaction_rec (p_return_status       => l_return_status,
4945                               p_overall_status      => px_overall_status,
4946                               p_tmt_flag            => 'TMT_ACCOUNTING_ENTRIES_YN',
4947                               p_tsu_code            => 'WORKING',
4948                               px_tcnv_rec           => px_tcnv_rec
4949                              );
4950       END IF;
4951 
4952       x_return_status := l_return_status;
4953 
4954       IF (is_debug_procedure_on)
4955       THEN
4956          okl_debug_pub.log_debug (g_level_procedure,
4957                                   l_module_name,
4958                                   'End(-)'
4959                                  );
4960       END IF;
4961    EXCEPTION
4962       WHEN okl_api.g_exception_error
4963       THEN
4964          -- Close any open cursors
4965          IF l_product_type_csr%ISOPEN
4966          THEN
4967             CLOSE l_product_type_csr;
4968          END IF;
4969 
4970          IF prod_id_csr%ISOPEN
4971          THEN
4972             CLOSE prod_id_csr;
4973          END IF;
4974 
4975          -- Rollback to savepoint
4976          ROLLBACK TO accounting_entries;
4977          -- Set Return status
4978          x_return_status := g_ret_sts_error;
4979          -- store the highest degree of error
4980          set_overall_status (p_return_status        => x_return_status,
4981                              px_overall_status      => px_overall_status);
4982          -- set the transaction record
4983          set_transaction_rec (p_return_status       => x_return_status,
4984                               p_overall_status      => px_overall_status,
4985                               p_tmt_flag            => 'TMT_ACCOUNTING_ENTRIES_YN',
4986                               p_tsu_code            => 'ERROR',
4987                               px_tcnv_rec           => px_tcnv_rec
4988                              );
4989 
4990          IF (is_debug_exception_on)
4991          THEN
4992             okl_debug_pub.log_debug (g_level_exception,
4993                                      l_module_name,
4994                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
4995                                     );
4996          END IF;
4997       WHEN okl_api.g_exception_unexpected_error
4998       THEN
4999          -- Close any open cursors
5000          IF l_product_type_csr%ISOPEN
5001          THEN
5002             CLOSE l_product_type_csr;
5003          END IF;
5004 
5005          IF prod_id_csr%ISOPEN
5006          THEN
5007             CLOSE prod_id_csr;
5008          END IF;
5009 
5010          -- Rollback to savepoint
5011          ROLLBACK TO accounting_entries;
5012          -- Set Return status
5013          x_return_status := g_ret_sts_unexp_error;
5014          -- store the highest degree of error
5015          set_overall_status (p_return_status        => x_return_status,
5016                              px_overall_status      => px_overall_status);
5017          -- set the transaction record
5018          set_transaction_rec (p_return_status       => x_return_status,
5019                               p_overall_status      => px_overall_status,
5020                               p_tmt_flag            => 'TMT_ACCOUNTING_ENTRIES_YN',
5021                               p_tsu_code            => 'ERROR',
5022                               px_tcnv_rec           => px_tcnv_rec
5023                              );
5024 
5025          IF (is_debug_exception_on)
5026          THEN
5027             okl_debug_pub.log_debug (g_level_exception,
5028                                      l_module_name,
5029                                         'EXCEPTION :'
5030                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
5031                                     );
5032          END IF;
5033       WHEN OTHERS
5034       THEN
5035          -- Close any open cursors
5036          IF l_product_type_csr%ISOPEN
5037          THEN
5038             CLOSE l_product_type_csr;
5039          END IF;
5040 
5041          IF prod_id_csr%ISOPEN
5042          THEN
5043             CLOSE prod_id_csr;
5044          END IF;
5045 
5046          -- Rollback to savepoint
5047          ROLLBACK TO accounting_entries;
5048          -- Set Return status
5049          x_return_status := g_ret_sts_unexp_error;
5050          -- store the highest degree of error
5051          set_overall_status (p_return_status        => x_return_status,
5052                              px_overall_status      => px_overall_status);
5053          -- set the transaction record
5054          set_transaction_rec (p_return_status       => x_return_status,
5055                               p_overall_status      => px_overall_status,
5056                               p_tmt_flag            => 'TMT_ACCOUNTING_ENTRIES_YN',
5057                               p_tsu_code            => 'ERROR',
5058                               px_tcnv_rec           => px_tcnv_rec
5059                              );
5060          -- Set the oracle error message
5061          okl_api.set_message (p_app_name          => g_app_name_1,
5062                               p_msg_name          => g_unexpected_error,
5063                               p_token1            => g_sqlcode_token,
5064                               p_token1_value      => SQLCODE,
5065                               p_token2            => g_sqlerrm_token,
5066                               p_token2_value      => SQLERRM
5067                              );
5068 
5069          IF (is_debug_exception_on)
5070          THEN
5071             okl_debug_pub.log_debug (g_level_exception,
5072                                      l_module_name,
5073                                         'EXCEPTION :'
5074                                      || 'OTHERS, SQLCODE: '
5075                                      || SQLCODE
5076                                      || ' , SQLERRM : '
5077                                      || SQLERRM
5078                                     );
5079          END IF;
5080    END accounting_entries;
5081 
5082    -- Start of comments
5083    --
5084    -- Procedure Name : dispose_assets
5085    -- Desciption     : Call the Asset Dispose API to dispose off assets
5086    -- Business Rules :
5087    -- Parameters     :
5088    -- Version        : 1.0
5089    -- History        : RMUNJULU Bug # 2484327 16-DEC-02, changed parameter value
5090    --                  to p_quantity to set the quote_quantity
5091    --                : SECHAWLA 31-DEC-02 Bug #2726739
5092    --                  Added logic to convert proceeds of sale amount to functional currency
5093    --                : RMUNJULU 23-JAN-03 2762065 Return E if split asset already
5094    --                  done
5095    --                : RMUNJULU 04-FEB-03 2781557 Added code to get and set
5096    --                  proceeds of sale properly. Also removed the FA related fix
5097    --                  introduced by bug 2762065 since split asset wont retire if
5098    --                  dispose also being done
5099    --                : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
5100    --                : SECHAWLA 11-MAR-03 Modified kle_pur_amt_csr cursor to nvl the purchase amount
5101    --                : rmunjulu EDAT Added code to set trn_date as currency conversion trn date
5102    --                  also send quote eff date and quote acceptance date to disposal api.
5103    --
5104    -- End of comments
5105    PROCEDURE dispose_assets (
5106       p_term_rec          IN              term_rec_type,
5107       p_sys_date          IN              DATE,
5108       p_klev_tbl          IN              klev_tbl_type,
5109       p_trn_already_set   IN              VARCHAR2,
5110       px_overall_status   IN OUT NOCOPY   VARCHAR2,
5111       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
5112       x_return_status     OUT NOCOPY      VARCHAR2
5113    )
5114    IS
5115       -- Cursor to get the purchase amount for the line
5116       -- RMUNJULU 04-FEB-03 2781557 Changed cursor to get proper purchase amount lines
5117       CURSOR kle_pur_amt_csr (
5118          p_kle_id   IN   NUMBER,
5119          p_qte_id   IN   NUMBER
5120       )
5121       IS
5122          SELECT NVL (tql.amount, 0) amount    --SECHAWLA 11-MAR-03 : Added nvl
5123            FROM okl_txl_quote_lines_v tql
5124           WHERE tql.kle_id = p_kle_id
5125             AND tql.qte_id = p_qte_id
5126             AND tql.qlt_code = 'AMBPOC';                    -- Purchase Amount
5127 
5128       kle_pur_amt_rec               kle_pur_amt_csr%ROWTYPE;
5129       l_return_status               VARCHAR2 (1)          := g_ret_sts_success;
5130       l_overall_dispose_status      VARCHAR2 (1)          := g_ret_sts_success;
5131       l_asset_id                    NUMBER;
5132       l_line_number                 VARCHAR2 (200);
5133       i                             NUMBER                                := 1;
5134       l_proceeds_of_sale            NUMBER;
5135       l_api_version        CONSTANT NUMBER                    := g_api_version;
5136       l_msg_count                   NUMBER                       := g_miss_num;
5137       l_msg_data                    VARCHAR2 (2000);
5138       l_module_name                 VARCHAR2 (500)
5139                                           := g_module_name || 'dispose_assets';
5140       is_debug_exception_on         BOOLEAN
5141               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
5142       is_debug_procedure_on         BOOLEAN
5143               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
5144       is_debug_statement_on         BOOLEAN
5145               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
5146       --SECHAWLA  Bug # 2726739 : new declarations
5147       l_func_curr_code              gl_ledgers_public_v.currency_code%TYPE;
5148       l_contract_curr_code          okc_k_headers_b.currency_code%TYPE;
5149       -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
5150       lx_contract_currency          okc_k_headers_v.currency_code%TYPE;
5151       lx_currency_conversion_type   okl_k_headers_v.currency_conversion_type%TYPE;
5152       lx_currency_conversion_rate   okl_k_headers_v.currency_conversion_rate%TYPE;
5153       lx_currency_conversion_date   okl_k_headers_v.currency_conversion_date%TYPE;
5154       lx_converted_amount           NUMBER;
5155       -- rmunjulu EDAT
5156       l_quote_accpt_date            DATE;
5157       l_quote_eff_date              DATE;
5158       -- RRAVIKIR Legal Entity changes
5159       l_legal_entity_id             NUMBER;
5160    -- Legal Entity changes end
5161    BEGIN
5162       -- Start savepoint to rollback to if the block fails
5163       SAVEPOINT asset_dispose;
5164 
5165       IF (is_debug_procedure_on)
5166       THEN
5167          okl_debug_pub.log_debug (g_level_procedure,
5168                                   l_module_name,
5169                                   'Begin(+)'
5170                                  );
5171       END IF;
5172 
5173       IF (is_debug_statement_on)
5174       THEN
5175          okl_debug_pub.log_debug (g_level_statement,
5176                                   l_module_name,
5177                                   'In param, p_sys_date: ' || p_sys_date
5178                                  );
5179          okl_debug_pub.log_debug (g_level_statement,
5180                                   l_module_name,
5181                                      'In param, p_trn_already_set: '
5182                                   || p_trn_already_set
5183                                  );
5184          okl_debug_pub.log_debug (g_level_statement,
5185                                   l_module_name,
5186                                      'In param, px_overall_status: '
5187                                   || px_overall_status
5188                                  );
5189          okl_debug_pub.log_debug (g_level_statement,
5190                                   l_module_name,
5191                                      'In param, p_term_rec.p_quote_id: '
5192                                   || p_term_rec.p_quote_id
5193                                  );
5194          okl_debug_pub.log_debug (g_level_statement,
5195                                   l_module_name,
5196                                      'In param, p_term_rec.p_contract_id: '
5197                                   || p_term_rec.p_contract_id
5198                                  );
5199          okl_debug_pub.log_debug
5200                                 (g_level_statement,
5201                                  l_module_name,
5202                                     'In param, p_term_rec.p_contract_number: '
5203                                  || p_term_rec.p_contract_number
5204                                 );
5205          okl_debug_pub.log_debug
5206                         (g_level_statement,
5207                          l_module_name,
5208                             'In param, px_tcnv_rec.tmt_asset_disposition_yn: '
5209                          || px_tcnv_rec.tmt_asset_disposition_yn
5210                         );
5211 
5212          IF p_klev_tbl.COUNT > 0
5213          THEN
5214             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
5215             LOOP
5216                IF (p_klev_tbl.EXISTS (i))
5217                THEN
5218                   okl_debug_pub.log_debug (g_level_statement,
5219                                            l_module_name,
5220                                               'In param, p_klev_tbl('
5221                                            || i
5222                                            || ').p_kle_id: '
5223                                            || p_klev_tbl (i).p_kle_id
5224                                           );
5225                   okl_debug_pub.log_debug (g_level_statement,
5226                                            l_module_name,
5227                                               'In param, p_klev_tbl('
5228                                            || i
5229                                            || ').p_asset_quantity: '
5230                                            || p_klev_tbl (i).p_asset_quantity
5231                                           );
5232                   okl_debug_pub.log_debug (g_level_statement,
5233                                            l_module_name,
5234                                               'In param, p_klev_tbl('
5235                                            || i
5236                                            || ').p_asset_name: '
5237                                            || p_klev_tbl (i).p_asset_name
5238                                           );
5239                   okl_debug_pub.log_debug (g_level_statement,
5240                                            l_module_name,
5241                                               'In param, p_klev_tbl('
5242                                            || i
5243                                            || ').p_quote_quantity: '
5244                                            || p_klev_tbl (i).p_quote_quantity
5245                                           );
5246                   okl_debug_pub.log_debug (g_level_statement,
5247                                            l_module_name,
5248                                               'In param, p_klev_tbl('
5249                                            || i
5250                                            || ').p_tql_id: '
5251                                            || p_klev_tbl (i).p_tql_id
5252                                           );
5253                   okl_debug_pub.log_debug (g_level_statement,
5254                                            l_module_name,
5255                                               'In param, p_klev_tbl('
5256                                            || i
5257                                            || ').p_split_kle_name: '
5258                                            || p_klev_tbl (i).p_split_kle_name
5259                                           );
5260                   okl_debug_pub.log_debug (g_level_statement,
5261                                            l_module_name,
5262                                               'In param, p_klev_tbl('
5263                                            || i
5264                                            || ').p_split_kle_id: '
5265                                            || p_klev_tbl (i).p_split_kle_id
5266                                           );
5267                END IF;
5268             END LOOP;
5269          END IF;
5270       END IF;
5271 
5272       -- Check if disposition done earlier
5273       IF    (    p_trn_already_set = g_yes
5274              AND NVL (px_tcnv_rec.tmt_asset_disposition_yn, '?') <> g_yes
5275             )
5276          OR (p_trn_already_set = g_no)
5277       THEN
5278          -- RMUNJULU 3018641 Step Message
5279          -- Step : Asset Dispose
5280          okl_api.set_message (p_app_name      => g_app_name,
5281                               p_msg_name      => 'OKL_AM_STEP_ADP');
5282 
5283          -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
5284 
5285          -- rmunjulu EDAT
5286          -- If quote exists then accnting date is quote accept date else sysdate
5287          IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
5288          THEN
5289             l_quote_accpt_date :=
5290                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
5291             l_quote_eff_date :=
5292                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
5293          ELSE
5294             l_quote_accpt_date := p_sys_date;
5295             l_quote_eff_date := p_sys_date;
5296          END IF;
5297 
5298          -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
5299          IF (p_klev_tbl.COUNT > 0)
5300          THEN
5301             i := p_klev_tbl.FIRST;
5302 
5303             LOOP
5304                -- Initialize proceeds_of_sale
5305                l_proceeds_of_sale := 0;
5306 
5307                -- Loop in the purchase amounts to set proceeds_of_sale
5308                FOR kle_pur_amt_rec IN
5309                   kle_pur_amt_csr (p_klev_tbl (i).p_kle_id,
5310                                    p_term_rec.p_quote_id)
5311                LOOP
5312                   l_proceeds_of_sale :=
5313                                   l_proceeds_of_sale + kle_pur_amt_rec.amount;
5314                END LOOP;
5315 
5316                -- SECHAWLA 11-MAR-03 : Commented the following IF, as it is not necessary
5317 
5318                /*
5319                -- RMUNJULU 04-FEB-03 2781557 Added if to set the proceeds of sales if no value
5320                IF l_proceeds_of_sale IS NULL THEN
5321                   l_proceeds_of_sale := 0;
5322                END IF;
5323                */
5324 
5325                -- SECHAWLA  Bug # 2726739 : added the folowing piece of code
5326 
5327                -- get the functional currency
5328                l_func_curr_code := okl_am_util_pvt.get_functional_currency;
5329                -- get the contract currency
5330                l_contract_curr_code :=
5331                   okl_am_util_pvt.get_chr_currency
5332                                          (p_chr_id      => p_term_rec.p_contract_id);
5333 
5334                IF l_contract_curr_code <> l_func_curr_code
5335                THEN
5336                   -- convert amount to functional currency
5337                   IF (is_debug_statement_on)
5338                   THEN
5339                      okl_debug_pub.log_debug
5340                         (g_level_statement,
5341                          l_module_name,
5342                          'calling okl_accounting_util.convert_to_functional_currency'
5343                         );
5344                   END IF;
5345 
5346                   okl_accounting_util.convert_to_functional_currency
5347                      (p_khr_id                        => p_term_rec.p_contract_id,
5348                       p_to_currency                   => l_func_curr_code,
5349                       p_transaction_date              => p_sys_date,
5350                       p_amount                        => l_proceeds_of_sale,
5351                       x_return_status                 => x_return_status,
5352                       x_contract_currency             => lx_contract_currency,
5353                       x_currency_conversion_type      => lx_currency_conversion_type,
5354                       x_currency_conversion_rate      => lx_currency_conversion_rate,
5355                       x_currency_conversion_date      => lx_currency_conversion_date,
5356                       x_converted_amount              => lx_converted_amount
5357                      );
5358 
5359                   IF (is_debug_statement_on)
5360                   THEN
5361                      okl_debug_pub.log_debug
5362                         (g_level_statement,
5363                          l_module_name,
5364                             'called okl_accounting_util.convert_to_functional_currency , return status: '
5365                          || x_return_status
5366                         );
5367                      okl_debug_pub.log_debug (g_level_statement,
5368                                               l_module_name,
5369                                                  'lx_contract_currency_code: '
5370                                               || lx_contract_currency
5371                                              );
5372                      okl_debug_pub.log_debug
5373                                            (g_level_statement,
5374                                             l_module_name,
5375                                                'lx_currency_conversion_type: '
5376                                             || lx_currency_conversion_type
5377                                            );
5378                      okl_debug_pub.log_debug
5379                                            (g_level_statement,
5380                                             l_module_name,
5381                                                'lx_currency_conversion_rate: '
5382                                             || lx_currency_conversion_rate
5383                                            );
5384                      okl_debug_pub.log_debug
5385                                            (g_level_statement,
5386                                             l_module_name,
5387                                                'lx_currency_conversion_date: '
5388                                             || lx_currency_conversion_date
5389                                            );
5390                      okl_debug_pub.log_debug (g_level_statement,
5391                                               l_module_name,
5392                                                  'lx_converted_amount: '
5393                                               || lx_converted_amount
5394                                              );
5395                   END IF;
5396 
5397                   IF x_return_status <> g_ret_sts_success
5398                   THEN
5399                      -- Error occurred during disposal of asset NAME.
5400                      okl_api.set_message
5401                                 (p_app_name          => g_app_name,
5402                                  p_msg_name          => 'OKL_AM_ERR_DISPOSAL',
5403                                  p_token1            => 'NAME',
5404                                  p_token1_value      => p_klev_tbl (i).p_asset_name
5405                                 );
5406                   END IF;
5407 
5408                   -- Raise exception to rollback to savepoint if error
5409                   IF (x_return_status = g_ret_sts_unexp_error)
5410                   THEN
5411                      RAISE okl_api.g_exception_unexpected_error;
5412                   ELSIF (x_return_status = g_ret_sts_error)
5413                   THEN
5414                      RAISE okl_api.g_exception_error;
5415                   END IF;
5416 
5417                   l_proceeds_of_sale := lx_converted_amount;
5418                END IF;
5419 
5420                -- -- SECHAWLA  Bug # 2726739 : end new code
5421 
5422                -- RMUNJULU 23-JAN-03 2762065 -- START --
5423                -- Code fix for FA related error in Asset Retirement
5424                -- We always return error when Asset Dispose called after a split asset
5425 
5426                -- RMUNJULU 04-FEB-03 2781557 Removed the temporary FA related fix
5427                --              IF NVL(px_tcnv_rec.tmt_split_asset_yn, 'N') = 'Y' THEN
5428 
5429                --                 l_return_status := G_RET_STS_ERROR;
5430 
5431                --              ELSE
5432 
5433                -- RRAVIKIR Legal Entity Changes
5434                -- Populate the legal entity from the contract
5435                l_legal_entity_id :=
5436                   okl_legal_entity_util.get_khr_le_id
5437                                          (p_khr_id      => p_term_rec.p_contract_id);
5438 
5439                -- Legal Entity Changes end
5440 
5441                -- call asset dispose retirement
5442                IF (is_debug_statement_on)
5443                THEN
5444                   okl_debug_pub.log_debug
5445                             (g_level_statement,
5446                              l_module_name,
5447                              'calling OKL_AM_ASSET_DISPOSE_PUB.dispose_asset'
5448                             );
5449                END IF;
5450 
5451                okl_am_asset_dispose_pub.dispose_asset
5452                   (p_api_version             => l_api_version,
5453                    p_init_msg_list           => g_false,
5454                    x_return_status           => l_return_status,
5455                    x_msg_count               => l_msg_count,
5456                    x_msg_data                => l_msg_data,
5457                    p_financial_asset_id      => p_klev_tbl (i).p_kle_id,
5458                    p_quantity                => NULL,
5459                    p_proceeds_of_sale        => l_proceeds_of_sale,
5460                    p_quote_eff_date          => l_quote_eff_date,
5461       -- rmunjulu EDAT Pass additional parameters now required by disposal api
5462                    p_quote_accpt_date        => l_quote_accpt_date,
5463       -- rmunjulu EDAT Pass additional parameters now required by disposal api
5464                    p_legal_entity_id         => l_legal_entity_id
5465                   );                          -- RRAVIKIR Legal Entity Changes
5466 
5467                IF (is_debug_statement_on)
5468                THEN
5469                   okl_debug_pub.log_debug
5470                      (g_level_statement,
5471                       l_module_name,
5472                          'called OKL_AM_ASSET_DISPOSE_PUB.dispose_asset , return status: '
5473                       || l_return_status
5474                      );
5475                END IF;
5476 
5477                -- call asset dispose API
5478                -- RMUNJULU Bug # 2484327 16-DEC-02, changed parameter value to
5479                -- p_quantity to set the quote_quantity
5480                -- RMUNJULU 04-FEB-03 2781557 Changed value passed to p_quantity
5481                -- from quote_quantity to NULL
5482                /*    OKL_AM_ASSET_DISPOSE_PUB.dispose_asset(
5483                           p_api_version        => l_api_version,
5484                           p_init_msg_list      => G_FALSE,
5485                           x_return_status      => l_return_status,
5486                           x_msg_count          => l_msg_count,
5487                           x_msg_data           => l_msg_data,
5488                           p_financial_asset_id => p_klev_tbl(i).p_kle_id,
5489                           p_quantity           => NULL,
5490                           p_proceeds_of_sale   => l_proceeds_of_sale,
5491                           p_quote_eff_date     => l_quote_eff_date,    -- rmunjulu EDAT Pass additional parameters now required by disposal api
5492                           p_quote_accpt_date   => l_quote_accpt_date); -- rmunjulu EDAT Pass additional parameters now required by disposal api
5493                */
5494 
5495                --              END IF;
5496 
5497                -- RMUNJULU 23-JAN-03 2762065 -- END --
5498 
5499                -- Check the return status
5500                IF l_return_status <> g_ret_sts_success
5501                THEN
5502                   -- Error occurred during disposal of asset NAME.
5503                   okl_api.set_message
5504                                 (p_app_name          => g_app_name,
5505                                  p_msg_name          => 'OKL_AM_ERR_DISPOSAL',
5506                                  p_token1            => 'NAME',
5507                                  p_token1_value      => p_klev_tbl (i).p_asset_name
5508                                 );
5509                END IF;
5510 
5511                --08-mar-06 sgorantl -- Bug 3895098
5512                IF     l_overall_dispose_status = okl_api.g_ret_sts_success
5513                   AND l_return_status IN
5514                          (okl_api.g_ret_sts_error,
5515                           okl_api.g_ret_sts_unexp_error
5516                          )
5517                THEN
5518                   l_overall_dispose_status := l_return_status;
5519                END IF;
5520 
5521                EXIT WHEN (i = p_klev_tbl.LAST);
5522                i := p_klev_tbl.NEXT (i);
5523             END LOOP;
5524 
5525             --08-mar-06 sgorantl -- Bug 3895098
5526             -- Raise exception to rollback to savepoint if error
5527             IF (l_overall_dispose_status = okl_api.g_ret_sts_unexp_error)
5528             THEN
5529                RAISE okl_api.g_exception_unexpected_error;
5530             ELSIF (l_overall_dispose_status = okl_api.g_ret_sts_error)
5531             THEN
5532                RAISE okl_api.g_exception_error;
5533             END IF;
5534 
5535             --08-mar-06 sgorantl -- Bug 3895098
5536 
5537             -- Asset dispostion for assets of contract CONTRACT_NUMBER
5538             -- done successfully.
5539             okl_api.set_message
5540                                (p_app_name          => g_app_name,
5541                                 p_msg_name          => 'OKL_AM_ASS_DISPOSE_SUCCESS',
5542                                 p_token1            => 'CONTRACT_NUMBER',
5543                                 p_token1_value      => p_term_rec.p_contract_number
5544                                );
5545             -- store the highest degree of error
5546             set_overall_status (p_return_status        => l_overall_dispose_status,
5547                                 px_overall_status      => px_overall_status);
5548             -- set the transaction record for asset disposition
5549             set_transaction_rec (p_return_status       => l_overall_dispose_status,
5550                                  p_overall_status      => px_overall_status,
5551                                  p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
5552                                  p_tsu_code            => 'WORKING',
5553                                  px_tcnv_rec           => px_tcnv_rec
5554                                 );
5555          END IF;
5556       END IF;
5557 
5558       -- set the transaction record for amortization
5559       set_transaction_rec (p_return_status       => l_return_status,
5560                            p_overall_status      => px_overall_status,
5561                            p_tmt_flag            => 'TMT_AMORTIZATION_YN',
5562                            p_tsu_code            => 'WORKING',
5563                            p_ret_val             => NULL,
5564                            px_tcnv_rec           => px_tcnv_rec
5565                           );
5566       -- set the transaction record for asset return
5567       set_transaction_rec (p_return_status       => l_return_status,
5568                            p_overall_status      => px_overall_status,
5569                            p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
5570                            p_tsu_code            => 'WORKING',
5571                            p_ret_val             => NULL,
5572                            px_tcnv_rec           => px_tcnv_rec
5573                           );
5574       x_return_status := l_return_status;
5575 
5576       IF (is_debug_procedure_on)
5577       THEN
5578          okl_debug_pub.log_debug (g_level_procedure,
5579                                   l_module_name,
5580                                   'End(-)'
5581                                  );
5582       END IF;
5583    EXCEPTION
5584       WHEN okl_api.g_exception_error
5585       THEN
5586          -- Close any open cursors
5587          IF kle_pur_amt_csr%ISOPEN
5588          THEN
5589             CLOSE kle_pur_amt_csr;
5590          END IF;
5591 
5592          ROLLBACK TO asset_dispose;
5593          x_return_status := g_ret_sts_error;
5594          -- store the highest degree of error
5595          set_overall_status (p_return_status        => x_return_status,
5596                              px_overall_status      => px_overall_status);
5597          -- set the transaction record
5598          set_transaction_rec (p_return_status       => x_return_status,
5599                               p_overall_status      => px_overall_status,
5600                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
5601                               p_tsu_code            => 'ERROR',
5602                               px_tcnv_rec           => px_tcnv_rec
5603                              );
5604          -- set the transaction record for amortization
5605          set_transaction_rec (p_return_status       => x_return_status,
5606                               p_overall_status      => px_overall_status,
5607                               p_tmt_flag            => 'TMT_AMORTIZATION_YN',
5608                               p_tsu_code            => 'ERROR',
5609                               p_ret_val             => NULL,
5610                               px_tcnv_rec           => px_tcnv_rec
5611                              );
5612          -- set the transaction record for asset return
5613          set_transaction_rec (p_return_status       => x_return_status,
5614                               p_overall_status      => px_overall_status,
5615                               p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
5616                               p_tsu_code            => 'ERROR',
5617                               p_ret_val             => NULL,
5618                               px_tcnv_rec           => px_tcnv_rec
5619                              );
5620 
5621          IF (is_debug_exception_on)
5622          THEN
5623             okl_debug_pub.log_debug (g_level_exception,
5624                                      l_module_name,
5625                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
5626                                     );
5627          END IF;
5628       WHEN okl_api.g_exception_unexpected_error
5629       THEN
5630          -- Close any open cursors
5631          IF kle_pur_amt_csr%ISOPEN
5632          THEN
5633             CLOSE kle_pur_amt_csr;
5634          END IF;
5635 
5636          ROLLBACK TO asset_dispose;
5637          x_return_status := g_ret_sts_unexp_error;
5638          -- store the highest degree of error
5639          set_overall_status (p_return_status        => x_return_status,
5640                              px_overall_status      => px_overall_status);
5641          -- set the transaction record
5642          set_transaction_rec (p_return_status       => x_return_status,
5643                               p_overall_status      => px_overall_status,
5644                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
5645                               p_tsu_code            => 'ERROR',
5646                               px_tcnv_rec           => px_tcnv_rec
5647                              );
5648          -- set the transaction record for amortization
5649          set_transaction_rec (p_return_status       => x_return_status,
5650                               p_overall_status      => px_overall_status,
5651                               p_tmt_flag            => 'TMT_AMORTIZATION_YN',
5652                               p_tsu_code            => 'ERROR',
5653                               p_ret_val             => NULL,
5654                               px_tcnv_rec           => px_tcnv_rec
5655                              );
5656          -- set the transaction record for asset return
5657          set_transaction_rec (p_return_status       => x_return_status,
5658                               p_overall_status      => px_overall_status,
5659                               p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
5660                               p_tsu_code            => 'ERROR',
5661                               p_ret_val             => NULL,
5662                               px_tcnv_rec           => px_tcnv_rec
5663                              );
5664 
5665          IF (is_debug_exception_on)
5666          THEN
5667             okl_debug_pub.log_debug (g_level_exception,
5668                                      l_module_name,
5669                                         'EXCEPTION :'
5670                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
5671                                     );
5672          END IF;
5673       WHEN OTHERS
5674       THEN
5675          -- Close any open cursors
5676          IF kle_pur_amt_csr%ISOPEN
5677          THEN
5678             CLOSE kle_pur_amt_csr;
5679          END IF;
5680 
5681          ROLLBACK TO asset_dispose;
5682          x_return_status := g_ret_sts_unexp_error;
5683          -- store the highest degree of error
5684          set_overall_status (p_return_status        => x_return_status,
5685                              px_overall_status      => px_overall_status);
5686          -- set the transaction record
5687          set_transaction_rec (p_return_status       => x_return_status,
5688                               p_overall_status      => px_overall_status,
5689                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
5690                               p_tsu_code            => 'ERROR',
5691                               px_tcnv_rec           => px_tcnv_rec
5692                              );
5693          -- set the transaction record for amortization
5694          set_transaction_rec (p_return_status       => x_return_status,
5695                               p_overall_status      => px_overall_status,
5696                               p_tmt_flag            => 'TMT_AMORTIZATION_YN',
5697                               p_tsu_code            => 'ERROR',
5698                               p_ret_val             => NULL,
5699                               px_tcnv_rec           => px_tcnv_rec
5700                              );
5701          -- set the transaction record for asset return
5702          set_transaction_rec (p_return_status       => x_return_status,
5703                               p_overall_status      => px_overall_status,
5704                               p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
5705                               p_tsu_code            => 'ERROR',
5706                               p_ret_val             => NULL,
5707                               px_tcnv_rec           => px_tcnv_rec
5708                              );
5709          -- Set the oracle error message
5710          okl_api.set_message (p_app_name          => g_app_name_1,
5711                               p_msg_name          => g_unexpected_error,
5712                               p_token1            => g_sqlcode_token,
5713                               p_token1_value      => SQLCODE,
5714                               p_token2            => g_sqlerrm_token,
5715                               p_token2_value      => SQLERRM
5716                              );
5717 
5718          IF (is_debug_exception_on)
5719          THEN
5720             okl_debug_pub.log_debug (g_level_exception,
5721                                      l_module_name,
5722                                         'EXCEPTION :'
5723                                      || 'OTHERS, SQLCODE: '
5724                                      || SQLCODE
5725                                      || ' , SQLERRM : '
5726                                      || SQLERRM
5727                                     );
5728          END IF;
5729    END dispose_assets;
5730 
5731    -- Start of comments
5732    --
5733    -- Procedure Name : delink_assets
5734    -- Desciption     : Calls the FA adjustment API to delink the asset from contract
5735    -- Business Rules :
5736    -- Parameters     :
5737    -- Version      : 1.0
5738    -- History        : AKRANGAN created
5739    --
5740    -- End of comments
5741    PROCEDURE delink_assets (
5742       p_term_rec          IN              term_rec_type,
5743       p_sys_date          IN              DATE,
5744       p_klev_tbl          IN              klev_tbl_type,
5745       p_trn_already_set   IN              VARCHAR2,
5746       px_overall_status   IN OUT NOCOPY   VARCHAR2,
5747       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
5748       x_return_status     OUT NOCOPY      VARCHAR2
5749    )
5750    IS
5751       -- Get all the FA books (corp and tax) that asset belongs to
5752       CURSOR l_fabooks_csr (
5753          cp_asset_number   IN   VARCHAR2,
5754          cp_sysdate        IN   DATE
5755       )
5756       IS
5757          SELECT fb.book_type_code,
5758                 fb.asset_id,
5759                 fb.contract_id
5760            FROM fa_books fb,
5761                 fa_additions_b fab,
5762                 fa_book_controls fbc
5763           WHERE fb.asset_id = fab.asset_id
5764             AND fb.book_type_code = fbc.book_type_code
5765             AND NVL (fbc.date_ineffective, cp_sysdate + 1) > cp_sysdate
5766             AND fb.transaction_header_id_out IS NULL
5767             AND fab.asset_number = cp_asset_number;
5768 
5769       l_return_status               VARCHAR2 (1)          := g_ret_sts_success;
5770       l_overall_status              VARCHAR2 (1)          := g_ret_sts_success;
5771       i                             NUMBER                                := 1;
5772       l_early_term_yn               VARCHAR2 (1)                       := g_no;
5773       l_k_end_date                  DATE                        := g_miss_date;
5774       l_api_version        CONSTANT NUMBER                    := g_api_version;
5775       l_msg_count                   NUMBER                       := g_miss_num;
5776       l_msg_data                    VARCHAR2 (2000);
5777       l_module_name                 VARCHAR2 (500)
5778                                            := g_module_name || 'delink_assets';
5779       is_debug_exception_on         BOOLEAN
5780               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
5781       is_debug_procedure_on         BOOLEAN
5782               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
5783       is_debug_statement_on         BOOLEAN
5784               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
5785       l_evergreen_earlier           VARCHAR2 (3)                        := 'N';
5786       l_quote_accpt_date            DATE;
5787       l_quote_eff_date              DATE;
5788       l_asset_fin_rec_empty_adj     fa_api_types.asset_fin_rec_type;
5789       l_asset_hdr_empty_rec         fa_api_types.asset_hdr_rec_type;
5790       l_trans_empty_rec             fa_api_types.trans_rec_type;
5791       l_adj_trans_rec               fa_api_types.trans_rec_type;
5792       l_adj_asset_hdr_rec           fa_api_types.asset_hdr_rec_type;
5793       l_asset_fin_rec_adj           fa_api_types.asset_fin_rec_type;
5794       l_asset_fin_rec_new           fa_api_types.asset_fin_rec_type;
5795       l_inv_trans_rec               fa_api_types.inv_trans_rec_type;
5796       l_adj_inv_tbl                 fa_api_types.inv_tbl_type;
5797       l_asset_deprn_rec_adj         fa_api_types.asset_deprn_rec_type;
5798       l_asset_deprn_rec_new         fa_api_types.asset_deprn_rec_type;
5799       l_asset_deprn_mrc_tbl_new     fa_api_types.asset_deprn_tbl_type;
5800       l_group_reclass_options_rec   fa_api_types.group_reclass_options_rec_type;
5801       l_asset_fin_mrc_tbl_new       fa_api_types.asset_fin_tbl_type;
5802       l_transaction_subtype         VARCHAR2 (20);
5803       l_tmt_flag                    VARCHAR2 (50);
5804    BEGIN
5805       -- Start a savepoint
5806       SAVEPOINT asset_delink;
5807 
5808       IF (is_debug_procedure_on)
5809       THEN
5810          okl_debug_pub.log_debug (g_level_procedure,
5811                                   l_module_name,
5812                                   'Begin(+)'
5813                                  );
5814       END IF;
5815 
5816       IF (is_debug_statement_on)
5817       THEN
5818          okl_debug_pub.log_debug (g_level_statement,
5819                                   l_module_name,
5820                                   'In param, p_sys_date: ' || p_sys_date
5821                                  );
5822          okl_debug_pub.log_debug (g_level_statement,
5823                                   l_module_name,
5824                                      'In param, p_trn_already_set: '
5825                                   || p_trn_already_set
5826                                  );
5827          okl_debug_pub.log_debug (g_level_statement,
5828                                   l_module_name,
5829                                      'In param, px_overall_status: '
5830                                   || px_overall_status
5831                                  );
5832          okl_debug_pub.log_debug (g_level_statement,
5833                                   l_module_name,
5834                                      'In param, p_term_rec.p_quote_id: '
5835                                   || p_term_rec.p_quote_id
5836                                  );
5837          okl_debug_pub.log_debug (g_level_statement,
5838                                   l_module_name,
5839                                      'In param, p_term_rec.p_contract_id: '
5840                                   || p_term_rec.p_contract_id
5841                                  );
5842          okl_debug_pub.log_debug
5843                                 (g_level_statement,
5844                                  l_module_name,
5845                                     'In param, p_term_rec.p_contract_number: '
5846                                  || p_term_rec.p_contract_number
5847                                 );
5848          okl_debug_pub.log_debug
5849                              (g_level_statement,
5850                               l_module_name,
5851                                  'In param, px_tcnv_rec.tmt_amortization_yn: '
5852                               || px_tcnv_rec.tmt_amortization_yn
5853                              );
5854          okl_debug_pub.log_debug (g_level_statement,
5855                                   l_module_name,
5856                                      'In param, px_tcnv_rec.id: '
5857                                   || px_tcnv_rec.ID
5858                                  );
5859 
5860          IF p_klev_tbl.COUNT > 0
5861          THEN
5862             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
5863             LOOP
5864                IF (p_klev_tbl.EXISTS (i))
5865                THEN
5866                   okl_debug_pub.log_debug (g_level_statement,
5867                                            l_module_name,
5868                                               'In param, p_klev_tbl('
5869                                            || i
5870                                            || ').p_kle_id: '
5871                                            || p_klev_tbl (i).p_kle_id
5872                                           );
5873                   okl_debug_pub.log_debug (g_level_statement,
5874                                            l_module_name,
5875                                               'In param, p_klev_tbl('
5876                                            || i
5877                                            || ').p_asset_name: '
5878                                            || p_klev_tbl (i).p_asset_name
5879                                           );
5880                END IF;
5881             END LOOP;
5882          END IF;
5883       END IF;
5884 
5885       IF (is_debug_statement_on)
5886       THEN
5887          okl_debug_pub.log_debug
5888                       (g_level_statement,
5889                        l_module_name,
5890                        'calling OKL_AM_LEASE_TRMNT_PVT.check_k_evergreen_ear'
5891                       );
5892       END IF;
5893 
5894       l_evergreen_earlier :=
5895          okl_am_lease_trmnt_pvt.check_k_evergreen_ear
5896                                         (p_khr_id             => p_term_rec.p_contract_id,
5897                                          p_tcn_id             => px_tcnv_rec.ID,
5898                                          x_return_status      => l_return_status
5899                                         );
5900 
5901       IF (is_debug_statement_on)
5902       THEN
5903          okl_debug_pub.log_debug
5904             (g_level_statement,
5905              l_module_name,
5906                 'called OKL_AM_LEASE_TRMNT_PVT.check_k_evergreen_ear , return status: '
5907              || l_return_status
5908             );
5909          okl_debug_pub.log_debug (g_level_statement,
5910                                   l_module_name,
5911                                      'l_evergreen_earlier: '
5912                                   || l_evergreen_earlier
5913                                  );
5914       END IF;
5915 
5916       IF l_return_status <> okl_api.g_ret_sts_success
5917       THEN
5918          -- Error occurred during the creation of an amortization transaction
5919          -- for assets of contract CONTRACT_NUMBER.
5920          okl_api.set_message (p_app_name          => g_app_name,
5921                               p_msg_name          => 'OKL_AM_ERR_AMORTIZE',
5922                               p_token1            => 'CONTRACT_NUMBER',
5923                               p_token1_value      => p_term_rec.p_contract_number
5924                              );
5925       END IF;
5926 
5927       -- If quote exists then cancelation date is quote eff from date else sysdate
5928       IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
5929       THEN
5930          l_quote_accpt_date :=
5931                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
5932          l_quote_eff_date :=
5933                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
5934       ELSE
5935          l_quote_accpt_date := p_sys_date;
5936          l_quote_eff_date := p_sys_date;
5937       END IF;
5938 
5939       IF l_evergreen_earlier = 'Y'
5940       THEN
5941          l_transaction_subtype := '';
5942          l_tmt_flag := '';
5943       ELSE
5944          l_transaction_subtype := 'AMORTIZED';
5945          l_tmt_flag := 'TMT_AMORTIZATION_YN';
5946       END IF;
5947 
5948       -- if assets present for contract
5949       IF (p_klev_tbl.COUNT > 0)
5950       THEN
5951          -- Loop thru assets table
5952          i := p_klev_tbl.FIRST;
5953 
5954          LOOP
5955             FOR l_fabooks_rec IN l_fabooks_csr (p_klev_tbl (i).p_asset_name,
5956                                                 l_quote_accpt_date)
5957             LOOP
5958                IF l_fabooks_rec.contract_id IS NOT NULL
5959                THEN
5960                   l_asset_fin_rec_adj := l_asset_fin_rec_empty_adj;
5961                   l_adj_trans_rec := l_trans_empty_rec;
5962                   l_adj_asset_hdr_rec := l_asset_hdr_empty_rec;
5963                   l_adj_trans_rec.transaction_subtype :=
5964                                                         l_transaction_subtype;
5965                   l_adj_asset_hdr_rec.asset_id := l_fabooks_rec.asset_id;
5966                   l_adj_asset_hdr_rec.book_type_code :=
5967                                                  l_fabooks_rec.book_type_code;
5968                   l_asset_fin_rec_adj.contract_id := fnd_api.g_miss_num;
5969                   l_adj_trans_rec.transaction_date_entered :=
5970                                                              l_quote_eff_date;
5971                   fa_adjustment_pub.do_adjustment
5972                      (p_api_version                    => l_api_version,
5973                       p_init_msg_list                  => okc_api.g_false,
5974                       p_commit                         => fnd_api.g_false,
5975                       p_validation_level               => fnd_api.g_valid_level_full,
5976                       p_calling_fn                     => NULL,
5977                       x_return_status                  => l_return_status,
5978                       x_msg_count                      => l_msg_count,
5979                       x_msg_data                       => l_msg_data,
5980                       px_trans_rec                     => l_adj_trans_rec,
5981                       px_asset_hdr_rec                 => l_adj_asset_hdr_rec,
5982                       p_asset_fin_rec_adj              => l_asset_fin_rec_adj,
5983                       x_asset_fin_rec_new              => l_asset_fin_rec_new,
5984                       x_asset_fin_mrc_tbl_new          => l_asset_fin_mrc_tbl_new,
5985                       px_inv_trans_rec                 => l_inv_trans_rec,
5986                       px_inv_tbl                       => l_adj_inv_tbl,
5987                       p_asset_deprn_rec_adj            => l_asset_deprn_rec_adj,
5988                       x_asset_deprn_rec_new            => l_asset_deprn_rec_new,
5989                       x_asset_deprn_mrc_tbl_new        => l_asset_deprn_mrc_tbl_new,
5990                       p_group_reclass_options_rec      => l_group_reclass_options_rec
5991                      );
5992 
5993                   IF l_return_status <> okc_api.g_ret_sts_success
5994                   THEN
5995                      -- Error processing TRX_TYPE transaction in Fixed Assets for asset ASSET_NUMBER in book BOOK.
5996                      okc_api.set_message
5997                               (p_app_name          => 'OKL',
5998                                p_msg_name          => 'OKL_AM_AMT_TRANS_FAILED',
5999                                p_token1            => 'TRX_TYPE',
6000                                p_token1_value      => 'Contract Delink',
6001                                p_token2            => 'ASSET_NUMBER',
6002                                p_token2_value      => p_klev_tbl (i).p_asset_name,
6003                                p_token3            => 'BOOK',
6004                                p_token3_value      => l_fabooks_rec.book_type_code
6005                               );
6006                      RAISE okl_api.g_exception_error;
6007                   END IF;
6008                END IF;
6009             END LOOP;
6010 
6011             EXIT WHEN (i = p_klev_tbl.LAST);
6012             i := p_klev_tbl.NEXT (i);
6013          END LOOP;
6014 
6015          -- set the transaction record for asset return
6016          set_transaction_rec (p_return_status       => l_return_status,
6017                               p_overall_status      => px_overall_status,
6018                               p_tmt_flag            => l_tmt_flag,
6019                               p_tsu_code            => 'WORKING',
6020                               px_tcnv_rec           => px_tcnv_rec
6021                              );
6022          -- Set overall status
6023          set_overall_status (p_return_status        => l_return_status,
6024                              px_overall_status      => px_overall_status);
6025       END IF;
6026 
6027       IF (is_debug_procedure_on)
6028       THEN
6029          okl_debug_pub.log_debug (g_level_procedure,
6030                                   l_module_name,
6031                                   'End(-)'
6032                                  );
6033       END IF;
6034    EXCEPTION
6035       WHEN okl_api.g_exception_error
6036       THEN
6037          IF l_fabooks_csr%ISOPEN
6038          THEN
6039             CLOSE l_fabooks_csr;
6040          END IF;
6041 
6042          ROLLBACK TO asset_delink;
6043          x_return_status := g_ret_sts_error;
6044          -- store the highest degree of error
6045          set_overall_status (p_return_status        => x_return_status,
6046                              px_overall_status      => px_overall_status);
6047          -- set the transaction record
6048          set_transaction_rec (p_return_status       => x_return_status,
6049                               p_overall_status      => px_overall_status,
6050                               p_tmt_flag            => l_tmt_flag,
6051                               p_tsu_code            => 'ERROR',
6052                               px_tcnv_rec           => px_tcnv_rec
6053                              );
6054 
6055          IF (is_debug_exception_on)
6056          THEN
6057             okl_debug_pub.log_debug (g_level_exception,
6058                                      l_module_name,
6059                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
6060                                     );
6061          END IF;
6062       WHEN okl_api.g_exception_unexpected_error
6063       THEN
6064          IF l_fabooks_csr%ISOPEN
6065          THEN
6066             CLOSE l_fabooks_csr;
6067          END IF;
6068 
6069          ROLLBACK TO asset_delink;
6070          x_return_status := g_ret_sts_unexp_error;
6071          -- store the highest degree of error
6072          set_overall_status (p_return_status        => x_return_status,
6073                              px_overall_status      => px_overall_status);
6074          -- set the transaction record
6075          set_transaction_rec (p_return_status       => x_return_status,
6076                               p_overall_status      => px_overall_status,
6077                               p_tmt_flag            => l_tmt_flag,
6078                               p_tsu_code            => 'ERROR',
6079                               px_tcnv_rec           => px_tcnv_rec
6080                              );
6081 
6082          IF (is_debug_exception_on)
6083          THEN
6084             okl_debug_pub.log_debug (g_level_exception,
6085                                      l_module_name,
6086                                         'EXCEPTION :'
6087                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
6088                                     );
6089          END IF;
6090       WHEN OTHERS
6091       THEN
6092          IF l_fabooks_csr%ISOPEN
6093          THEN
6094             CLOSE l_fabooks_csr;
6095          END IF;
6096 
6097          ROLLBACK TO asset_delink;
6098          x_return_status := g_ret_sts_unexp_error;
6099          -- store the highest degree of error
6100          set_overall_status (p_return_status        => x_return_status,
6101                              px_overall_status      => px_overall_status);
6102          -- set the transaction record
6103          set_transaction_rec (p_return_status       => x_return_status,
6104                               p_overall_status      => px_overall_status,
6105                               p_tmt_flag            => l_tmt_flag,
6106                               p_tsu_code            => 'ERROR',
6107                               px_tcnv_rec           => px_tcnv_rec
6108                              );
6109          -- Set the oracle error message
6110          okl_api.set_message (p_app_name          => g_app_name_1,
6111                               p_msg_name          => g_unexpected_error,
6112                               p_token1            => g_sqlcode_token,
6113                               p_token1_value      => SQLCODE,
6114                               p_token2            => g_sqlerrm_token,
6115                               p_token2_value      => SQLERRM
6116                              );
6117 
6118          IF (is_debug_exception_on)
6119          THEN
6120             okl_debug_pub.log_debug (g_level_exception,
6121                                      l_module_name,
6122                                         'EXCEPTION :'
6123                                      || 'OTHERS, SQLCODE: '
6124                                      || SQLCODE
6125                                      || ' , SQLERRM : '
6126                                      || SQLERRM
6127                                     );
6128          END IF;
6129    END delink_assets;
6130 
6131    -- Start of comments
6132    --
6133    -- Procedure Name : amortize_assets
6134    -- Desciption     : Calls the Amortization API to amortize assets of contract
6135    -- Business Rules :
6136    -- Parameters     :
6137    -- Version      : 1.0
6138    -- History        : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
6139    --                : RMUNJULU 3485854 12-MAR-04 Added code to check contract was
6140    --                  EVERGREEN earlier, if so then amortization was done, DO NOT do again
6141    --                : rmunjulu EDAT Added code to get the quote eff date and check for early term based on that
6142    --                  Also pass quote eff date and quote acceptance date to amortize api
6143    --                : SECHAWLA 14-dec-07 6690811 - Delink contract ID from FA asset, upon partial termination of
6144    --                  Booked or Evergreen contract.
6145    --                : SECHAWLA 02-Jan-08 6720667 - Check if contract ID is already null, before updating it to Null
6146    --
6147    -- End of comments
6148    PROCEDURE amortize_assets (
6149       p_term_rec          IN              term_rec_type,
6150       p_sys_date          IN              DATE,
6151       p_klev_tbl          IN              klev_tbl_type,
6152       p_trn_already_set   IN              VARCHAR2,
6153       px_overall_status   IN OUT NOCOPY   VARCHAR2,
6154       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
6155       x_return_status     OUT NOCOPY      VARCHAR2
6156    )
6157    IS
6158       -- Cursor to get the end date of contract
6159       -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
6160       CURSOR get_k_end_date_csr (
6161          p_khr_id   IN   NUMBER
6162       )
6163       IS
6164          SELECT khr.end_date end_date
6165            FROM okc_k_headers_v khr
6166           WHERE khr.ID = p_khr_id;
6167 
6168       l_return_status          VARCHAR2 (1)    := g_ret_sts_success;
6169       l_overall_status         VARCHAR2 (1)    := g_ret_sts_success;
6170       i                        NUMBER          := 1;
6171       l_early_term_yn          VARCHAR2 (1)    := g_no;
6172       l_k_end_date             DATE            := g_miss_date;
6173       l_api_version   CONSTANT NUMBER          := g_api_version;
6174       l_msg_count              NUMBER          := g_miss_num;
6175       l_msg_data               VARCHAR2 (2000);
6176       l_module_name            VARCHAR2 (500)
6177                                          := g_module_name || 'amortize_assets';
6178       is_debug_exception_on    BOOLEAN
6179               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
6180       is_debug_procedure_on    BOOLEAN
6181               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
6182       is_debug_statement_on    BOOLEAN
6183               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
6184       -- RMUNJULU 3485854
6185       l_evergreen_earlier      VARCHAR2 (3)    := 'N';
6186       -- rmunjulu EDAT
6187       l_quote_accpt_date       DATE;
6188       l_quote_eff_date         DATE;
6189    BEGIN
6190       -- Start a savepoint
6191       SAVEPOINT asset_amortize;
6192 
6193       IF (is_debug_procedure_on)
6194       THEN
6195          okl_debug_pub.log_debug (g_level_procedure,
6196                                   l_module_name,
6197                                   'Begin(+)'
6198                                  );
6199       END IF;
6200 
6201       IF (is_debug_statement_on)
6202       THEN
6203          okl_debug_pub.log_debug (g_level_statement,
6204                                   l_module_name,
6205                                   'In param, p_sys_date: ' || p_sys_date
6206                                  );
6207          okl_debug_pub.log_debug (g_level_statement,
6208                                   l_module_name,
6209                                      'In param, p_trn_already_set: '
6210                                   || p_trn_already_set
6211                                  );
6212          okl_debug_pub.log_debug (g_level_statement,
6213                                   l_module_name,
6214                                      'In param, px_overall_status: '
6215                                   || px_overall_status
6216                                  );
6217          okl_debug_pub.log_debug (g_level_statement,
6218                                   l_module_name,
6219                                      'In param, p_term_rec.p_quote_id: '
6220                                   || p_term_rec.p_quote_id
6221                                  );
6222          okl_debug_pub.log_debug (g_level_statement,
6223                                   l_module_name,
6224                                      'In param, p_term_rec.p_contract_id: '
6225                                   || p_term_rec.p_contract_id
6226                                  );
6227          okl_debug_pub.log_debug
6228                                 (g_level_statement,
6229                                  l_module_name,
6230                                     'In param, p_term_rec.p_contract_number: '
6231                                  || p_term_rec.p_contract_number
6232                                 );
6233          okl_debug_pub.log_debug
6234                              (g_level_statement,
6235                               l_module_name,
6236                                  'In param, px_tcnv_rec.tmt_amortization_yn: '
6237                               || px_tcnv_rec.tmt_amortization_yn
6238                              );
6239          okl_debug_pub.log_debug (g_level_statement,
6240                                   l_module_name,
6241                                      'In param, px_tcnv_rec.id: '
6242                                   || px_tcnv_rec.ID
6243                                  );
6244 
6245          IF p_klev_tbl.COUNT > 0
6246          THEN
6247             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
6248             LOOP
6249                IF (p_klev_tbl.EXISTS (i))
6250                THEN
6251                   okl_debug_pub.log_debug (g_level_statement,
6252                                            l_module_name,
6253                                               'In param, p_klev_tbl('
6254                                            || i
6255                                            || ').p_kle_id: '
6256                                            || p_klev_tbl (i).p_kle_id
6257                                           );
6258                   okl_debug_pub.log_debug (g_level_statement,
6259                                            l_module_name,
6260                                               'In param, p_klev_tbl('
6261                                            || i
6262                                            || ').p_asset_name: '
6263                                            || p_klev_tbl (i).p_asset_name
6264                                           );
6265                END IF;
6266             END LOOP;
6267          END IF;
6268       END IF;
6269 
6270       -- Check if amortization required
6271       IF    (    p_trn_already_set = g_yes
6272              AND NVL (px_tcnv_rec.tmt_amortization_yn, '?') <> g_yes
6273             )
6274          OR (p_trn_already_set = g_no)
6275       THEN
6276 
6277             -- rmunjulu bug 7009808 Set the variables outside so that delink can use the values
6278             -- If quote exists then cancelation date is quote eff from date else sysdate
6279             IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
6280             THEN
6281                l_quote_accpt_date :=
6282                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
6283                l_quote_eff_date :=
6284                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
6285             ELSE
6286                l_quote_accpt_date := p_sys_date;
6287                l_quote_eff_date := p_sys_date;
6288             END IF;
6289 
6290 
6291 
6292          -- RMUNJULU 3485854
6293          -- CHECK TO see IF old evergreen transaction exists
6294          -- Check if another transaction exists which is processed and for which tmt_evergreen_yn was Y
6295          -- which means this contract was evergreen earlier
6296          -- so no need to run amortization again
6297          IF (is_debug_statement_on)
6298          THEN
6299             okl_debug_pub.log_debug
6300                       (g_level_statement,
6301                        l_module_name,
6302                        'calling OKL_AM_LEASE_TRMNT_PVT.check_k_evergreen_ear'
6303                       );
6304          END IF;
6305 
6306          l_evergreen_earlier :=
6307             okl_am_lease_trmnt_pvt.check_k_evergreen_ear
6308                                         (p_khr_id             => p_term_rec.p_contract_id,
6309                                          p_tcn_id             => px_tcnv_rec.ID,
6310                                          x_return_status      => l_return_status
6311                                         );
6312 
6313          IF (is_debug_statement_on)
6314          THEN
6315             okl_debug_pub.log_debug
6316                (g_level_statement,
6317                 l_module_name,
6318                    'called OKL_AM_LEASE_TRMNT_PVT.check_k_evergreen_ear , return status: '
6319                 || l_return_status
6320                );
6321             okl_debug_pub.log_debug (g_level_statement,
6322                                      l_module_name,
6323                                         'l_evergreen_earlier: '
6324                                      || l_evergreen_earlier
6325                                     );
6326          END IF;
6327 
6328          IF l_return_status <> okl_api.g_ret_sts_success
6329          THEN
6330             -- Error occurred during the creation of an amortization transaction
6331             -- for assets of contract CONTRACT_NUMBER.
6332             okl_api.set_message
6333                               (p_app_name          => g_app_name,
6334                                p_msg_name          => 'OKL_AM_ERR_AMORTIZE',
6335                                p_token1            => 'CONTRACT_NUMBER',
6336                                p_token1_value      => p_term_rec.p_contract_number
6337                               );
6338          END IF;
6339 
6340          -- RMUNJULU 3485854
6341          -- Check to make sure amortization was not done
6342          IF NVL (l_evergreen_earlier, 'N') <> 'Y'
6343          THEN
6344             -- RMUNJULU 3018641 Step Message
6345             -- Step : Amortization
6346             okl_api.set_message (p_app_name      => g_app_name,
6347                                  p_msg_name      => 'OKL_AM_STEP_AMT');
6348 
6349             -- get k end date
6350             OPEN get_k_end_date_csr (p_term_rec.p_contract_id);
6351 
6352             FETCH get_k_end_date_csr
6353              INTO l_k_end_date;
6354 
6355             CLOSE get_k_end_date_csr;
6356 
6357             -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
6358 
6359             -- rmunjulu EDAT
6360             -- If quote exists then cancelation date is quote eff from date else sysdate
6361             IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
6362             THEN
6363                l_quote_accpt_date :=
6364                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
6365                l_quote_eff_date :=
6366                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
6367             ELSE
6368                l_quote_accpt_date := p_sys_date;
6369                l_quote_eff_date := p_sys_date;
6370             END IF;
6371 
6372             -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
6373 
6374             -- Set early termination yn flag
6375             IF     (l_k_end_date <> g_miss_date)
6376                AND (TRUNC (l_k_end_date) > TRUNC (l_quote_eff_date))
6377             THEN
6378                -- rmunjulu EDAT Check based on quote eff date
6379                l_early_term_yn := g_yes;
6380             END IF;
6381 
6382             -- if assets present for contract
6383             IF (p_klev_tbl.COUNT > 0)
6384             THEN
6385                -- Loop thru assets table
6386                i := p_klev_tbl.FIRST;
6387 
6388                LOOP
6389                   IF (is_debug_statement_on)
6390                   THEN
6391                      okl_debug_pub.log_debug
6392                         (g_level_statement,
6393                          l_module_name,
6394                          'calling OKL_AM_AMORTIZE_PUB.create_offlease_asset_trx'
6395                         );
6396                   END IF;
6397 
6398                   -- call amortization
6399                   okl_am_amortize_pub.create_offlease_asset_trx
6400                         (p_api_version               => l_api_version,
6401                          p_init_msg_list             => g_false,
6402                          x_return_status             => l_return_status,
6403                          x_msg_count                 => l_msg_count,
6404                          x_msg_data                  => l_msg_data,
6405                          p_kle_id                    => p_klev_tbl (i).p_kle_id,
6406                          p_early_termination_yn      => l_early_term_yn,
6407                          p_quote_eff_date            => l_quote_eff_date,
6408                                                               -- rmunjulu EDAT
6409                          p_quote_accpt_date          => l_quote_accpt_date
6410                         );                                    -- rmunjulu EDAT
6411 
6412                   IF (is_debug_statement_on)
6413                   THEN
6414                      okl_debug_pub.log_debug
6415                         (g_level_statement,
6416                          l_module_name,
6417                             'called OKL_AM_AMORTIZE_PUB.create_offlease_asset_trx , return status: '
6418                          || l_return_status
6419                         );
6420                   END IF;
6421 
6422                   IF l_return_status <> g_ret_sts_success
6423                   THEN
6424                      -- Error occurred during the amortization process for asset NAME.
6425                      okl_api.set_message
6426                                 (p_app_name          => g_app_name,
6427                                  p_msg_name          => 'OKL_AM_ERR_LNS_AMORTIZE',
6428                                  p_token1            => 'NAME',
6429                                  p_token1_value      => p_klev_tbl (i).p_asset_name
6430                                 );
6431                      -- Raise exception to rollback to savepoint if error
6432                      RAISE okl_api.g_exception_error;
6433                   END IF;
6434 
6435                   EXIT WHEN (i = p_klev_tbl.LAST);
6436                   i := p_klev_tbl.NEXT (i);
6437                END LOOP;
6438 
6439                -- set the transaction record for asset return
6440                set_transaction_rec (p_return_status       => l_return_status,
6441                                     p_overall_status      => px_overall_status,
6442                                     p_tmt_flag            => 'TMT_AMORTIZATION_YN',
6443                                     p_tsu_code            => 'WORKING',
6444                                     px_tcnv_rec           => px_tcnv_rec
6445                                    );
6446                -- Set overall status
6447                set_overall_status (p_return_status        => l_return_status,
6448                                    px_overall_status      => px_overall_status);
6449             END IF;
6450          END IF;
6451 
6452          -- rmunjulu bug 6853566 make call to delink contract from here
6453          -- This call will always be made if amortize is supposed to be run
6454          --
6455          OKL_AM_LEASE_TRMNT_PVT.delink_contract_from_asset(
6456                        p_api_version      => l_api_version,
6457                        x_msg_count        => l_msg_count,
6458                        x_msg_data         => l_msg_data,
6459                        p_full_term_yn     => 'N', -- not full termination but partial
6460                        p_khr_id           => p_term_rec.p_contract_id,
6461                        p_klev_tbl         => p_klev_tbl,
6462                        --p_sts_code         => l_dummy_sts_code,
6463                        p_quote_accpt_date => l_quote_accpt_date,
6464                        p_quote_eff_date   => l_quote_eff_date,
6465                        x_return_status    => l_return_status);
6466 
6467          IF l_return_status <> g_ret_sts_success  THEN
6468             -- Error occurred during the amortization process for asset NAME.
6469             okl_api.set_message
6470                     (p_app_name          => g_app_name,
6471                      p_msg_name          => 'OKL_AM_ERR_LNS_AMORTIZE',
6472                      p_token1            => 'NAME',
6473                      p_token1_value      => p_klev_tbl (i).p_asset_name
6474                      );
6475              -- Raise exception to rollback to savepoint if error
6476              RAISE okl_api.g_exception_error;
6477          END IF;
6478 
6479          -- set the transaction record for asset return
6480          set_transaction_rec (p_return_status       => l_return_status,
6481                               p_overall_status      => px_overall_status,
6482                               p_tmt_flag            => 'TMT_AMORTIZATION_YN',
6483                               p_tsu_code            => 'WORKING',
6484                               px_tcnv_rec           => px_tcnv_rec
6485                               );
6486          -- Set overall status
6487          set_overall_status (p_return_status        => l_return_status,
6488                              px_overall_status      => px_overall_status);
6489       END IF;
6490 
6491       IF (is_debug_procedure_on)
6492       THEN
6493          okl_debug_pub.log_debug (g_level_procedure,
6494                                   l_module_name,
6495                                   'End(-)'
6496                                  );
6497       END IF;
6498 
6499       --SET THE AMORTIZATION FLAG
6500       g_amort_complete_flag := 'Y';
6501    EXCEPTION
6502       WHEN okl_api.g_exception_error
6503       THEN
6504          IF get_k_end_date_csr%ISOPEN
6505          THEN
6506             CLOSE get_k_end_date_csr;
6507          END IF;
6508 
6509          ROLLBACK TO asset_amortize;
6510          x_return_status := g_ret_sts_error;
6511          -- store the highest degree of error
6512          set_overall_status (p_return_status        => x_return_status,
6513                              px_overall_status      => px_overall_status);
6514          -- set the transaction record
6515          set_transaction_rec (p_return_status       => x_return_status,
6516                               p_overall_status      => px_overall_status,
6517                               p_tmt_flag            => 'TMT_AMORTIZATION_YN',
6518                               p_tsu_code            => 'ERROR',
6519                               px_tcnv_rec           => px_tcnv_rec
6520                              );
6521          -- set the transaction record
6522          set_transaction_rec (p_return_status       => x_return_status,
6523                               p_overall_status      => px_overall_status,
6524                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
6525                               p_tsu_code            => 'ERROR',
6526                               p_ret_val             => NULL,
6527                               px_tcnv_rec           => px_tcnv_rec
6528                              );
6529          g_amort_complete_flag := 'N';
6530 
6531          IF (is_debug_exception_on)
6532          THEN
6533             okl_debug_pub.log_debug (g_level_exception,
6534                                      l_module_name,
6535                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
6536                                     );
6537          END IF;
6538       WHEN okl_api.g_exception_unexpected_error
6539       THEN
6540          IF get_k_end_date_csr%ISOPEN
6541          THEN
6542             CLOSE get_k_end_date_csr;
6543          END IF;
6544 
6545          ROLLBACK TO asset_amortize;
6546          x_return_status := g_ret_sts_unexp_error;
6547          -- store the highest degree of error
6548          set_overall_status (p_return_status        => x_return_status,
6549                              px_overall_status      => px_overall_status);
6550          -- set the transaction record
6551          set_transaction_rec (p_return_status       => x_return_status,
6552                               p_overall_status      => px_overall_status,
6553                               p_tmt_flag            => 'TMT_AMORTIZATION_YN',
6554                               p_tsu_code            => 'ERROR',
6555                               px_tcnv_rec           => px_tcnv_rec
6556                              );
6557          -- set the transaction record
6558          set_transaction_rec (p_return_status       => x_return_status,
6559                               p_overall_status      => px_overall_status,
6560                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
6561                               p_tsu_code            => 'ERROR',
6562                               p_ret_val             => NULL,
6563                               px_tcnv_rec           => px_tcnv_rec
6564                              );
6565          g_amort_complete_flag := 'N';
6566 
6567          IF (is_debug_exception_on)
6568          THEN
6569             okl_debug_pub.log_debug (g_level_exception,
6570                                      l_module_name,
6571                                         'EXCEPTION :'
6572                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
6573                                     );
6574          END IF;
6575       WHEN OTHERS
6576       THEN
6577          IF get_k_end_date_csr%ISOPEN
6578          THEN
6579             CLOSE get_k_end_date_csr;
6580          END IF;
6581 
6582          ROLLBACK TO asset_amortize;
6583          x_return_status := g_ret_sts_unexp_error;
6584          -- store the highest degree of error
6585          set_overall_status (p_return_status        => x_return_status,
6586                              px_overall_status      => px_overall_status);
6587          -- set the transaction record
6588          set_transaction_rec (p_return_status       => x_return_status,
6589                               p_overall_status      => px_overall_status,
6590                               p_tmt_flag            => 'TMT_AMORTIZATION_YN',
6591                               p_tsu_code            => 'ERROR',
6592                               px_tcnv_rec           => px_tcnv_rec
6593                              );
6594          -- set the transaction record
6595          set_transaction_rec (p_return_status       => x_return_status,
6596                               p_overall_status      => px_overall_status,
6597                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
6598                               p_tsu_code            => 'ERROR',
6599                               p_ret_val             => NULL,
6600                               px_tcnv_rec           => px_tcnv_rec
6601                              );
6602          -- Set the oracle error message
6603          okl_api.set_message (p_app_name          => g_app_name_1,
6604                               p_msg_name          => g_unexpected_error,
6605                               p_token1            => g_sqlcode_token,
6606                               p_token1_value      => SQLCODE,
6607                               p_token2            => g_sqlerrm_token,
6608                               p_token2_value      => SQLERRM
6609                              );
6610          g_amort_complete_flag := 'N';
6611 
6612          IF (is_debug_exception_on)
6613          THEN
6614             okl_debug_pub.log_debug (g_level_exception,
6615                                      l_module_name,
6616                                         'EXCEPTION :'
6617                                      || 'OTHERS, SQLCODE: '
6618                                      || SQLCODE
6619                                      || ' , SQLERRM : '
6620                                      || SQLERRM
6621                                     );
6622          END IF;
6623    END amortize_assets;
6624 
6625    -- Start of comments
6626    --
6627    -- Procedure Name : return_assets
6628    -- Desciption     : Calls the Asset Return API to return assets of contract
6629    -- Business Rules :
6630    -- Parameters     :
6631    -- Version        : 1.0
6632    -- History        :  rmunjulu EDAT modified code to check for quote exists to set reason code
6633    --                : PAGARG Bug# 3925453: Set the asset return status as
6634    --                  'RELEASE_IN_PROCESS' for quote type TER_RELEASE_WO_PURCHASE.
6635    -- End of comments
6636    PROCEDURE return_assets (
6637       p_term_rec          IN              term_rec_type,
6638       p_sys_date          IN              DATE,
6639       p_klev_tbl          IN              klev_tbl_type,
6640       p_trn_already_set   IN              VARCHAR2,
6641       px_overall_status   IN OUT NOCOPY   VARCHAR2,
6642       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type,
6643       x_return_status     OUT NOCOPY      VARCHAR2
6644    )
6645    IS
6646       -- Get the non-cancelled asset return for asset
6647       CURSOR get_asset_return_csr (
6648          p_kle_id   IN   NUMBER
6649       )
6650       IS
6651          SELECT arr.ID ID,
6652                 okl_am_util_pvt.get_lookup_meaning ('OKL_ASSET_RETURN_STATUS',
6653                                                     arr.ars_code,
6654                                                     'N'
6655                                                    ) ret_status
6656            FROM okl_asset_returns_v arr
6657           WHERE arr.kle_id = p_kle_id AND arr.ars_code <> 'CANCELLED';
6658 
6659       l_return_status          VARCHAR2 (1)               := g_ret_sts_success;
6660       l_overall_status         VARCHAR2 (1)               := g_ret_sts_success;
6661       lp_artv_rec              okl_am_asset_return_pub.artv_rec_type;
6662       lx_artv_rec              okl_am_asset_return_pub.artv_rec_type;
6663       i                        NUMBER                                := 1;
6664       j                        NUMBER                                := 1;
6665       l_kle_id                 NUMBER;
6666       l_k_end_date             DATE                             := g_miss_date;
6667       l_return_needed          VARCHAR2 (1)                          := g_no;
6668       l_asset_return_status    VARCHAR2 (2000);
6669       l_temp_klev_tbl          klev_tbl_type;
6670       l_api_version   CONSTANT NUMBER                         := g_api_version;
6671       l_msg_count              NUMBER                            := g_miss_num;
6672       l_msg_data               VARCHAR2 (2000);
6673       l_module_name            VARCHAR2 (500)
6674                                            := g_module_name || 'return_assets';
6675       is_debug_exception_on    BOOLEAN
6676               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
6677       is_debug_procedure_on    BOOLEAN
6678               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
6679       is_debug_statement_on    BOOLEAN
6680               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
6681       -- rmunjulu EDAT
6682       l_quote_accpt_date       DATE;
6683       l_quote_eff_date         DATE;
6684    BEGIN
6685       SAVEPOINT asset_return;
6686 
6687       IF (is_debug_procedure_on)
6688       THEN
6689          okl_debug_pub.log_debug (g_level_procedure,
6690                                   l_module_name,
6691                                   'Begin(+)'
6692                                  );
6693       END IF;
6694 
6695       IF (is_debug_statement_on)
6696       THEN
6697          okl_debug_pub.log_debug (g_level_statement,
6698                                   l_module_name,
6699                                   'In param, p_sys_date: ' || p_sys_date
6700                                  );
6701          okl_debug_pub.log_debug (g_level_statement,
6702                                   l_module_name,
6703                                      'In param, p_trn_already_set: '
6704                                   || p_trn_already_set
6705                                  );
6706          okl_debug_pub.log_debug (g_level_statement,
6707                                   l_module_name,
6708                                      'In param, px_overall_status: '
6709                                   || px_overall_status
6710                                  );
6711          okl_debug_pub.log_debug (g_level_statement,
6712                                   l_module_name,
6713                                      'In param, p_term_rec.p_quote_type: '
6714                                   || p_term_rec.p_quote_type
6715                                  );
6716          okl_debug_pub.log_debug (g_level_statement,
6717                                   l_module_name,
6718                                      'In param, p_term_rec.p_quote_id: '
6719                                   || p_term_rec.p_quote_id
6720                                  );
6721          okl_debug_pub.log_debug
6722                              (g_level_statement,
6723                               l_module_name,
6724                                  'In param, px_tcnv_rec.tmt_asset_return_yn: '
6725                               || px_tcnv_rec.tmt_asset_return_yn
6726                              );
6727 
6728          IF p_klev_tbl.COUNT > 0
6729          THEN
6730             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
6731             LOOP
6732                IF (p_klev_tbl.EXISTS (i))
6733                THEN
6734                   okl_debug_pub.log_debug (g_level_statement,
6735                                            l_module_name,
6736                                               'In param, p_klev_tbl('
6737                                            || i
6738                                            || ').p_kle_id: '
6739                                            || p_klev_tbl (i).p_kle_id
6740                                           );
6741                   okl_debug_pub.log_debug (g_level_statement,
6742                                            l_module_name,
6743                                               'In param, p_klev_tbl('
6744                                            || i
6745                                            || ').p_asset_name: '
6746                                            || p_klev_tbl (i).p_asset_name
6747                                           );
6748                END IF;
6749             END LOOP;
6750          END IF;
6751       END IF;
6752 
6753       -- Check if asset return required
6754       IF    (    p_trn_already_set = g_yes
6755              AND NVL (px_tcnv_rec.tmt_asset_return_yn, '?') <> g_yes
6756             )
6757          OR (p_trn_already_set = g_no)
6758       THEN
6759          -- RMUNJULU 3018641 Step Message
6760          -- Step : Asset Return
6761          okl_api.set_message (p_app_name      => g_app_name,
6762                               p_msg_name      => 'OKL_AM_STEP_ART');
6763 
6764          -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
6765 
6766          -- rmunjulu EDAT
6767          -- If quote exists then accnting date is quote accept date else sysdate
6768          IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
6769          THEN
6770             l_quote_accpt_date :=
6771                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
6772             l_quote_eff_date :=
6773                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
6774          ELSE
6775             l_quote_accpt_date := p_sys_date;
6776             l_quote_eff_date := p_sys_date;
6777          END IF;
6778 
6779          -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
6780 
6781          -- if assets present for contract
6782          IF (p_klev_tbl.COUNT > 0)
6783          THEN
6784             -- Loop thru assets table
6785             i := p_klev_tbl.FIRST;
6786 
6787             LOOP
6788                l_return_needed := g_no;
6789 
6790                -- Check if return created
6791                OPEN get_asset_return_csr (p_klev_tbl (i).p_kle_id);
6792 
6793                FETCH get_asset_return_csr
6794                 INTO l_kle_id,
6795                      l_asset_return_status;
6796 
6797                IF get_asset_return_csr%NOTFOUND OR l_kle_id IS NULL
6798                THEN
6799                   l_return_needed := g_yes;
6800                END IF;
6801 
6802                CLOSE get_asset_return_csr;
6803 
6804                -- if no return try creating else set message
6805                IF l_return_needed = g_yes
6806                THEN
6807                   -- set the temp table to contain all assets returned NOW
6808                   l_temp_klev_tbl (j).p_kle_id := p_klev_tbl (i).p_kle_id;
6809                   l_temp_klev_tbl (j).p_asset_name :=
6810                                                   p_klev_tbl (i).p_asset_name;
6811                   j := j + 1;
6812                   -- set the asset return id
6813                   lp_artv_rec.kle_id := p_klev_tbl (i).p_kle_id;
6814 
6815                   -- set the art1_code for asset return 'OKL_ASSET_RETURN_TYPE'
6816                   -- if early termination assume from quote else contract exp
6817                   --                  IF (l_k_end_date <> OKL_API.G_MISS_DATE)
6818                   --                  AND (TRUNC(l_k_end_date) < TRUNC(p_sys_date)) THEN
6819                   -- rmunjulu EDAT modified condition to say if quote exists then EXE_TERMINATION_QUOTE else EXPIRATION
6820                   IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') =
6821                                                                           'Y'
6822                   THEN
6823                      -- rmunjulu EDAT
6824                      lp_artv_rec.art1_code := 'EXE_TERMINATION_QUOTE';
6825                   ELSE
6826                      lp_artv_rec.art1_code := 'CONTRACT_EXPIRATION';
6827                   END IF;
6828 
6829                   --Bug# 3925453: pagarg +++ T and A +++++++ Start ++++++++++
6830                   IF p_term_rec.p_quote_type = 'TER_RELEASE_WO_PURCHASE'
6831                   THEN
6832                      lp_artv_rec.ars_code := 'RELEASE_IN_PROCESS';
6833                   ELSE
6834                      -- set the ars_code for asset return 'OKL_ASSET_RETURN_STATUS'
6835                      lp_artv_rec.ars_code := 'SCHEDULED';
6836                   END IF;
6837 
6838                   --Bug# 3925453: pagarg +++ T and A +++++++ End ++++++++++
6839 
6840                   --Bug #3925453: pagarg +++ T and A ++++
6841                   -- Passing quote_id also to create_asset_return
6842                   -- call asset return
6843                   IF (is_debug_statement_on)
6844                   THEN
6845                      okl_debug_pub.log_debug
6846                         (g_level_statement,
6847                          l_module_name,
6848                          'calling OKL_AM_ASSET_RETURN_PUB.create_asset_return'
6849                         );
6850                   END IF;
6851 
6852                   okl_am_asset_return_pub.create_asset_return
6853                                           (p_api_version        => l_api_version,
6854                                            p_init_msg_list      => g_false,
6855                                            x_return_status      => l_return_status,
6856                                            x_msg_count          => l_msg_count,
6857                                            x_msg_data           => l_msg_data,
6858                                            p_artv_rec           => lp_artv_rec,
6859                                            x_artv_rec           => lx_artv_rec,
6860                                            p_quote_id           => p_term_rec.p_quote_id
6861                                           );
6862 
6863                   IF (is_debug_statement_on)
6864                   THEN
6865                      okl_debug_pub.log_debug
6866                         (g_level_statement,
6867                          l_module_name,
6868                             'called OKL_AM_ASSET_RETURN_PUB.create_asset_return , return status: '
6869                          || l_return_status
6870                         );
6871                   END IF;
6872 
6873                   IF l_return_status <> g_ret_sts_success
6874                   THEN
6875                      -- Error occurred during the creation of an asset
6876                      -- return record for asset  NAME.
6877                      okl_api.set_message
6878                                 (p_app_name          => g_app_name,
6879                                  p_msg_name          => 'OKL_AM_ERR_ASS_RET',
6880                                  p_token1            => 'NAME',
6881                                  p_token1_value      => p_klev_tbl (i).p_asset_name
6882                                 );
6883                      -- Raise exception to rollback to savepoint if error
6884                      RAISE okl_api.g_exception_error;
6885                   END IF;
6886                ELSE
6887                   -- Asset return already exists -- This is not an error
6888 
6889                   -- Asset Return already exists for this asset NAME with the
6890                   -- status STATUS so cannot create a new asset return now.
6891                   okl_api.set_message
6892                                (p_app_name          => g_app_name,
6893                                 p_msg_name          => 'OKL_AM_ASS_RET_ARS_ERR',
6894                                 p_token1            => 'NAME',
6895                                 p_token1_value      => p_klev_tbl (i).p_asset_name,
6896                                 p_token2            => 'STATUS',
6897                                 p_token2_value      => l_asset_return_status
6898                                );
6899                END IF;
6900 
6901                EXIT WHEN (i = p_klev_tbl.LAST);
6902                i := p_klev_tbl.NEXT (i);
6903             END LOOP;
6904 
6905             -- Set success messages once all returns done NOW
6906             IF l_temp_klev_tbl.COUNT > 0
6907             THEN
6908                i := l_temp_klev_tbl.FIRST;
6909 
6910                LOOP
6911                   -- Asset return created for asset  NAME.
6912                   okl_api.set_message
6913                            (p_app_name          => g_app_name,
6914                             p_msg_name          => 'OKL_AM_ASS_RET_CREATED',
6915                             p_token1            => 'NAME',
6916                             p_token1_value      => l_temp_klev_tbl (i).p_asset_name
6917                            );
6918                   EXIT WHEN (i = l_temp_klev_tbl.LAST);
6919                   i := l_temp_klev_tbl.NEXT (i);
6920                END LOOP;
6921             END IF;
6922 
6923             -- set the transaction record for asset return
6924             set_transaction_rec (p_return_status       => l_return_status,
6925                                  p_overall_status      => px_overall_status,
6926                                  p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
6927                                  p_tsu_code            => 'WORKING',
6928                                  px_tcnv_rec           => px_tcnv_rec
6929                                 );
6930             -- Set overall status
6931             set_overall_status (p_return_status        => l_return_status,
6932                                 px_overall_status      => px_overall_status);
6933          END IF;
6934       END IF;
6935 
6936       IF (is_debug_procedure_on)
6937       THEN
6938          okl_debug_pub.log_debug (g_level_procedure,
6939                                   l_module_name,
6940                                   'End(-)'
6941                                  );
6942       END IF;
6943    EXCEPTION
6944       WHEN okl_api.g_exception_error
6945       THEN
6946          IF get_asset_return_csr%ISOPEN
6947          THEN
6948             CLOSE get_asset_return_csr;
6949          END IF;
6950 
6951          ROLLBACK TO asset_return;
6952          x_return_status := g_ret_sts_error;
6953          -- store the highest degree of error
6954          set_overall_status (p_return_status        => x_return_status,
6955                              px_overall_status      => px_overall_status);
6956          -- set the transaction record
6957          set_transaction_rec (p_return_status       => x_return_status,
6958                               p_overall_status      => px_overall_status,
6959                               p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
6960                               p_tsu_code            => 'ERROR',
6961                               px_tcnv_rec           => px_tcnv_rec
6962                              );
6963          -- set the transaction record
6964          set_transaction_rec (p_return_status       => x_return_status,
6965                               p_overall_status      => px_overall_status,
6966                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
6967                               p_tsu_code            => 'ERROR',
6968                               p_ret_val             => NULL,
6969                               px_tcnv_rec           => px_tcnv_rec
6970                              );
6971 
6972          IF (is_debug_exception_on)
6973          THEN
6974             okl_debug_pub.log_debug (g_level_exception,
6975                                      l_module_name,
6976                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
6977                                     );
6978          END IF;
6979       WHEN okl_api.g_exception_unexpected_error
6980       THEN
6981          IF get_asset_return_csr%ISOPEN
6982          THEN
6983             CLOSE get_asset_return_csr;
6984          END IF;
6985 
6986          ROLLBACK TO asset_return;
6987          x_return_status := g_ret_sts_unexp_error;
6988          -- store the highest degree of error
6989          set_overall_status (p_return_status        => x_return_status,
6990                              px_overall_status      => px_overall_status);
6991          -- set the transaction record
6992          set_transaction_rec (p_return_status       => x_return_status,
6993                               p_overall_status      => px_overall_status,
6994                               p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
6995                               p_tsu_code            => 'ERROR',
6996                               px_tcnv_rec           => px_tcnv_rec
6997                              );
6998          -- set the transaction record
6999          set_transaction_rec (p_return_status       => x_return_status,
7000                               p_overall_status      => px_overall_status,
7001                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
7002                               p_tsu_code            => 'ERROR',
7003                               p_ret_val             => NULL,
7004                               px_tcnv_rec           => px_tcnv_rec
7005                              );
7006 
7007          IF (is_debug_exception_on)
7008          THEN
7009             okl_debug_pub.log_debug (g_level_exception,
7010                                      l_module_name,
7011                                         'EXCEPTION :'
7012                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
7013                                     );
7014          END IF;
7015       WHEN OTHERS
7016       THEN
7017          IF get_asset_return_csr%ISOPEN
7018          THEN
7019             CLOSE get_asset_return_csr;
7020          END IF;
7021 
7022          ROLLBACK TO asset_return;
7023          x_return_status := g_ret_sts_unexp_error;
7024          -- store the highest degree of error
7025          set_overall_status (p_return_status        => x_return_status,
7026                              px_overall_status      => px_overall_status);
7027          -- set the transaction record
7028          set_transaction_rec (p_return_status       => x_return_status,
7029                               p_overall_status      => px_overall_status,
7030                               p_tmt_flag            => 'TMT_ASSET_RETURN_YN',
7031                               p_tsu_code            => 'ERROR',
7032                               px_tcnv_rec           => px_tcnv_rec
7033                              );
7034          -- set the transaction record
7035          set_transaction_rec (p_return_status       => x_return_status,
7036                               p_overall_status      => px_overall_status,
7037                               p_tmt_flag            => 'TMT_ASSET_DISPOSITION_YN',
7038                               p_tsu_code            => 'ERROR',
7039                               p_ret_val             => NULL,
7040                               px_tcnv_rec           => px_tcnv_rec
7041                              );
7042          -- Set the oracle error message
7043          okl_api.set_message (p_app_name          => g_app_name_1,
7044                               p_msg_name          => g_unexpected_error,
7045                               p_token1            => g_sqlcode_token,
7046                               p_token1_value      => SQLCODE,
7047                               p_token2            => g_sqlerrm_token,
7048                               p_token2_value      => SQLERRM
7049                              );
7050 
7051          IF (is_debug_exception_on)
7052          THEN
7053             okl_debug_pub.log_debug (g_level_exception,
7054                                      l_module_name,
7055                                         'EXCEPTION :'
7056                                      || 'OTHERS, SQLCODE: '
7057                                      || SQLCODE
7058                                      || ' , SQLERRM : '
7059                                      || SQLERRM
7060                                     );
7061          END IF;
7062    END return_assets;
7063 
7064    -- Start of comments
7065    --
7066    -- Procedure Name : close_balances
7067    -- Desciption     : Calls the AR adjustments apis for K header and
7068    --                  Lines to close balances
7069    -- Business Rules :
7070    -- Parameters     :
7071    -- Version      : 1.0
7072    -- History        : RMUNJULU 03-JAN-03 2683876 Created
7073    --                : RMUNJULU 07-APR-03 2883292 Changed IF to check for NULL
7074    --                  tolerance_amt instead of -1
7075    --                : RMUNJULU 28-APR-04 3596626 Added code to set lp_acc_gen_primary_key_tbl
7076    --
7077    -- End of comments
7078    PROCEDURE close_balances (
7079       p_api_version     IN              NUMBER,
7080       p_init_msg_list   IN              VARCHAR2,
7081       x_msg_count       OUT NOCOPY      NUMBER,
7082       x_msg_data        OUT NOCOPY      VARCHAR2,
7083       x_return_status   OUT NOCOPY      VARCHAR2,
7084       p_term_rec        IN              term_rec_type,
7085       p_sys_date        IN              DATE,
7086       p_tcnv_rec        IN              tcnv_rec_type,
7087       px_msg_tbl        IN OUT NOCOPY   g_msg_tbl
7088    )
7089    IS
7090       -- Cursor to get the balances of contract
7091       CURSOR k_balances_csr (
7092          p_khr_id   IN   NUMBER
7093       )
7094       IS
7095          SELECT SUM (blp.amount_due_remaining)
7096            FROM okl_bpd_leasing_payment_trx_v blp
7097           WHERE blp.contract_id = p_khr_id;
7098 
7099       -- Cursor to get the lines with amount due and payment schedule id for the balances
7100       CURSOR k_bal_lns_csr (
7101          p_khr_id   IN   NUMBER
7102       )
7103       IS
7104          SELECT oblp.amount_due_remaining amount,
7105                 oblp.stream_type_id stream_type_id,
7106                 osty.NAME stream_meaning,
7107                 oblp.payment_schedule_id schedule_id,
7108                 oblp.receivables_invoice_number ar_invoice_number,
7109                 otil.ID til_id,
7110                 -999 tld_id
7111            FROM okl_bpd_leasing_payment_trx_v oblp,
7112                 okl_txl_ar_inv_lns_v otil,
7113                 okl_strm_type_v osty
7114           WHERE oblp.contract_id = p_khr_id
7115             AND oblp.receivables_invoice_id = otil.receivables_invoice_id
7116             AND oblp.stream_type_id = osty.ID
7117             AND oblp.amount_due_remaining > 0
7118          UNION
7119          SELECT oblp.amount_due_remaining amount,
7120                 oblp.stream_type_id stream_type_id,
7121                 osty.NAME stream_meaning,
7122                 oblp.payment_schedule_id schedule_id,
7123                 oblp.receivables_invoice_number ar_invoice_number,
7124                 otai.til_id_details til_id,
7125                 otai.ID tld_id
7126            FROM okl_bpd_leasing_payment_trx_v oblp,
7127                 okl_txd_ar_ln_dtls_v otai,
7128                 okl_strm_type_v osty
7129           WHERE oblp.contract_id = p_khr_id
7130             AND oblp.receivables_invoice_id = otai.receivables_invoice_id
7131             AND oblp.stream_type_id = osty.ID
7132             AND oblp.amount_due_remaining > 0;
7133 
7134       -- Cursor to get the product of the contract
7135       CURSOR prod_id_csr (
7136          p_khr_id   IN   NUMBER
7137       )
7138       IS
7139          SELECT khr.pdt_id
7140            FROM okl_k_headers_v khr
7141           WHERE khr.ID = p_khr_id;
7142 
7143       -- Cursor to get the distribution for the transaction id and
7144       -- transaction table
7145       -- Make sure we get the debit distribution and also it is 100percent
7146       CURSOR code_combination_id_csr (
7147          p_source_id      IN   NUMBER,
7148          p_source_table   IN   VARCHAR2
7149       )
7150       IS
7151          SELECT dst.code_combination_id
7152            FROM okl_trns_acc_dstrs dst
7153           WHERE dst.source_id = p_source_id
7154             AND dst.source_table = p_source_table
7155             AND dst.cr_dr_flag = 'C'
7156             AND dst.percentage = 100;
7157 
7158       k_bal_lns_rec                k_bal_lns_csr%ROWTYPE;
7159       l_return_status              VARCHAR2 (1)   := okl_api.g_ret_sts_success;
7160       lp_adjv_rec                  okl_trx_ar_adjsts_pub.adjv_rec_type;
7161       lx_adjv_rec                  okl_trx_ar_adjsts_pub.adjv_rec_type;
7162       lp_ajlv_tbl                  okl_txl_adjsts_lns_pub.ajlv_tbl_type;
7163       lx_ajlv_tbl                  okl_txl_adjsts_lns_pub.ajlv_tbl_type;
7164       l_early_termination_yn       VARCHAR2 (1)             := okl_api.g_false;
7165       l_total_amount_due           NUMBER                                := -1;
7166       l_code_combination_id        NUMBER                                := -1;
7167       i                            NUMBER                                 := 1;
7168       l_tolerance_amt              NUMBER                                := -1;
7169       l_api_name                   VARCHAR2 (30)           := 'close_balances';
7170       l_module_name                VARCHAR2 (500)
7171                                           := g_module_name || 'close_balances';
7172       is_debug_exception_on        BOOLEAN
7173               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
7174       is_debug_procedure_on        BOOLEAN
7175               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
7176       is_debug_statement_on        BOOLEAN
7177               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
7178       l_pdt_id                     NUMBER                                 := 0;
7179       lp_tmpl_identify_rec         okl_account_dist_pub.tmpl_identify_rec_type;
7180       lp_dist_info_rec             okl_account_dist_pub.dist_info_rec_type;
7181       lp_ctxt_val_tbl              okl_account_dist_pub.ctxt_val_tbl_type;
7182       lp_acc_gen_primary_key_tbl   okl_account_dist_pub.acc_gen_primary_key;
7183       lx_template_tbl              okl_account_dist_pub.avlv_tbl_type;
7184       lx_amount_tbl                okl_account_dist_pub.amount_tbl_type;
7185       l_try_id                     NUMBER;
7186       l_trans_meaning              VARCHAR2 (200);
7187       l_currency_code              VARCHAR2 (200);
7188       l_formatted_bal_amt          VARCHAR2 (200);
7189       l_formatted_tol_amt          VARCHAR2 (200);
7190       l_formatted_adj_amt          VARCHAR2 (200);
7191       l_functional_currency_code   VARCHAR2 (15);
7192       l_contract_currency_code     VARCHAR2 (15);
7193       l_currency_conversion_type   VARCHAR2 (30);
7194       l_currency_conversion_rate   NUMBER;
7195       l_currency_conversion_date   DATE;
7196       l_converted_amount           NUMBER;
7197       -- Since we do not use the amount or converted amount
7198       -- set a hardcoded value for the amount (and pass to to
7199       -- OKL_ACCOUNTING_UTIL.convert_to_functional_currency and get back
7200       -- conversion values )
7201       l_hard_coded_amount          NUMBER                               := 100;
7202       l_count                      NUMBER;
7203    BEGIN
7204       ---
7205       --get the tolerance limit from profile
7206       -- get the total balances of ARs for the contract
7207       -- if total balance amount within the tolerance limit then
7208       -- close balances
7209       -- end if
7210 
7211       -- Establish savepoint so that when error rollback
7212       SAVEPOINT close_balances;
7213 
7214       IF (is_debug_procedure_on)
7215       THEN
7216          okl_debug_pub.log_debug (g_level_procedure,
7217                                   l_module_name,
7218                                   'Begin(+)'
7219                                  );
7220       END IF;
7221 
7222       IF (is_debug_statement_on)
7223       THEN
7224          okl_debug_pub.log_debug (g_level_statement,
7225                                   l_module_name,
7226                                   'In param, p_sys_date: ' || p_sys_date
7227                                  );
7228          okl_debug_pub.log_debug (g_level_statement,
7229                                   l_module_name,
7230                                      'In param, p_term_rec.p_contract_id: '
7231                                   || p_term_rec.p_contract_id
7232                                  );
7233          okl_debug_pub.log_debug
7234                                 (g_level_statement,
7235                                  l_module_name,
7236                                     'In param, p_term_rec.p_contract_number: '
7237                                  || p_term_rec.p_contract_number
7238                                 );
7239          okl_debug_pub.log_debug (g_level_statement,
7240                                   l_module_name,
7241                                   'In param, p_tcnv_rec.id: ' || p_tcnv_rec.ID
7242                                  );
7243       END IF;
7244 
7245       l_count := px_msg_tbl.COUNT;
7246 
7247       -- get the total balances of ARs for the contract
7248       OPEN k_balances_csr (p_term_rec.p_contract_id);
7249 
7250       FETCH k_balances_csr
7251        INTO l_total_amount_due;
7252 
7253       CLOSE k_balances_csr;
7254 
7255       -- set the total amount if it is null
7256       IF l_total_amount_due IS NULL
7257       THEN
7258          l_total_amount_due := 0;
7259       END IF;
7260 
7261       -- RMUNJULU 3018641 Step Message
7262       -- Step : Close Balances
7263       okl_api.set_message (p_app_name      => g_app_name,
7264                            p_msg_name      => 'OKL_AM_STEP_CLB');
7265 
7266       -- Check if total amount due is +ve else set message and exit
7267       IF l_total_amount_due <= 0
7268       THEN
7269          -- No outstanding balances found.
7270          okl_api.set_message (p_app_name      => g_app_name,
7271                               p_msg_name      => 'OKL_AM_NO_BAL');
7272       ELSE
7273          -- can try closing balances
7274 
7275          --get the tolerance limit from profile
7276          fnd_profile.get ('OKL_SMALL_BALANCE_TOLERANCE', l_tolerance_amt);
7277 
7278          -- if no tolerance amt then assume tolerance amt = 0 ,
7279          -- raise warning msg and proceed
7280          -- RMUNJULU 07-APR-03 2883292 Changed IF to check for NULL instead of -1
7281          IF l_tolerance_amt IS NULL
7282          THEN
7283             l_tolerance_amt := 0;
7284             -- No tolerance amount found for closing of balances.
7285             okl_api.set_message (p_app_name      => g_app_name,
7286                                  p_msg_name      => 'OKL_AM_NO_TOL_AMT');
7287          END IF;
7288 
7289          -- IF total balance amount within the tolerance limit and amount due>0 then
7290          IF (l_total_amount_due <= l_tolerance_amt)
7291          THEN
7292             -- set the adjusts rec
7293             lp_adjv_rec.trx_status_code := 'WORKING';             -- tsu_code
7294             -- tcn_id is set to transaction id from transaction rec
7295             lp_adjv_rec.tcn_id := p_tcnv_rec.ID;
7296             -- adjustment_reason_code comes from OKL_ADJUSTMENT_REASON
7297             lp_adjv_rec.adjustment_reason_code := 'SMALL AMT REMAINING';
7298             lp_adjv_rec.apply_date := p_sys_date;
7299             lp_adjv_rec.gl_date := p_sys_date;
7300 
7301             IF (is_debug_statement_on)
7302             THEN
7303                okl_debug_pub.log_debug
7304                         (g_level_statement,
7305                          l_module_name,
7306                          'calling OKL_TRX_AR_ADJSTS_PUB.insert_trx_ar_adjsts'
7307                         );
7308             END IF;
7309 
7310             -- call the adjusts api
7311             okl_trx_ar_adjsts_pub.insert_trx_ar_adjsts
7312                                           (p_api_version        => p_api_version,
7313                                            p_init_msg_list      => okl_api.g_false,
7314                                            x_return_status      => l_return_status,
7315                                            x_msg_count          => x_msg_count,
7316                                            x_msg_data           => x_msg_data,
7317                                            p_adjv_rec           => lp_adjv_rec,
7318                                            x_adjv_rec           => lx_adjv_rec
7319                                           );
7320 
7321             IF (is_debug_statement_on)
7322             THEN
7323                okl_debug_pub.log_debug
7324                   (g_level_statement,
7325                    l_module_name,
7326                       'called OKL_TRX_AR_ADJSTS_PUB.insert_trx_ar_adjsts , return status: '
7327                    || l_return_status
7328                   );
7329             END IF;
7330 
7331             IF l_return_status <> g_ret_sts_success
7332             THEN
7333                -- Error occurred when creating adjustment
7334                -- records to write off balances.
7335                okl_api.set_message (p_app_name      => g_app_name,
7336                                     p_msg_name      => 'OKL_AM_ERR_ADJST_BAL');
7337             END IF;
7338 
7339             -- Raise exception to rollback this whole block
7340             IF (l_return_status = g_ret_sts_unexp_error)
7341             THEN
7342                RAISE okl_api.g_exception_unexpected_error;
7343             ELSIF (l_return_status = g_ret_sts_error)
7344             THEN
7345                RAISE okl_api.g_exception_error;
7346             END IF;
7347 
7348             IF (is_debug_statement_on)
7349             THEN
7350                okl_debug_pub.log_debug
7351                                 (g_level_statement,
7352                                  l_module_name,
7353                                  'calling OKL_AM_UTIL_PVT.get_transaction_id'
7354                                 );
7355             END IF;
7356 
7357             -- Get the transaction id for adjustments
7358             okl_am_util_pvt.get_transaction_id
7359                                           (p_try_name           => 'Balance Write off',
7360                                            x_return_status      => l_return_status,
7361                                            x_try_id             => l_try_id
7362                                           );
7363 
7364             IF (is_debug_statement_on)
7365             THEN
7366                okl_debug_pub.log_debug
7367                   (g_level_statement,
7368                    l_module_name,
7369                       'called OKL_AM_UTIL_PVT.get_transaction_id , return status: '
7370                    || l_return_status
7371                   );
7372                okl_debug_pub.log_debug (g_level_statement,
7373                                         l_module_name,
7374                                         'l_try_id: ' || l_try_id
7375                                        );
7376             END IF;
7377 
7378             IF l_return_status <> g_ret_sts_success
7379             THEN
7380                -- Message: Unable to find a transaction type for
7381                -- the transaction TRY_NAME
7382                okl_api.set_message (p_app_name          => g_app_name,
7383                                     p_msg_name          => 'OKL_AM_NO_TRX_TYPE_FOUND',
7384                                     p_token1            => 'TRY_NAME',
7385                                     p_token1_value      => l_trans_meaning
7386                                    );
7387             END IF;
7388 
7389             -- Raise exception to rollback this whole block
7390             IF (l_return_status = g_ret_sts_unexp_error)
7391             THEN
7392                RAISE okl_api.g_exception_unexpected_error;
7393             ELSIF (l_return_status = g_ret_sts_error)
7394             THEN
7395                RAISE okl_api.g_exception_error;
7396             END IF;
7397 
7398             -- Get the meaning of lookup BALANCE_WRITE_OFF
7399             l_trans_meaning :=
7400                okl_am_util_pvt.get_lookup_meaning
7401                                 (p_lookup_type      => 'OKL_ACCOUNTING_EVENT_TYPE',
7402                                  p_lookup_code      => 'BALANCE_WRITE_OFF',
7403                                  p_validate_yn      => 'Y'
7404                                 );
7405 
7406             -- get the product id
7407             OPEN prod_id_csr (p_term_rec.p_contract_id);
7408 
7409             FETCH prod_id_csr
7410              INTO l_pdt_id;
7411 
7412             CLOSE prod_id_csr;
7413 
7414             -- raise error message if no pdt_id
7415             IF l_pdt_id IS NULL OR l_pdt_id = 0
7416             THEN
7417                -- Error: Unable to create accounting entries because of a missing
7418                -- Product Type for the contract CONTRACT_NUMBER.
7419                okl_api.set_message
7420                               (p_app_name          => g_app_name,
7421                                p_msg_name          => 'OKL_AM_PRODUCT_ID_ERROR',
7422                                p_token1            => 'CONTRACT_NUMBER',
7423                                p_token1_value      => p_term_rec.p_contract_number
7424                               );
7425             END IF;
7426 
7427             -- Raise exception to rollback to savepoint for this block
7428             IF (l_return_status = g_ret_sts_unexp_error)
7429             THEN
7430                RAISE okl_api.g_exception_unexpected_error;
7431             ELSIF (l_return_status = g_ret_sts_error)
7432             THEN
7433                RAISE okl_api.g_exception_error;
7434             END IF;
7435 
7436             -- Get, set and pass the currency conversion parameters
7437             -- Get the functional currency from AM_Util
7438             l_functional_currency_code :=
7439                                        okl_am_util_pvt.get_functional_currency;
7440 
7441             IF (is_debug_statement_on)
7442             THEN
7443                okl_debug_pub.log_debug
7444                   (g_level_statement,
7445                    l_module_name,
7446                    'calling OKL_ACCOUNTING_UTIL.convert_to_functional_currency'
7447                   );
7448             END IF;
7449 
7450             -- Get the currency conversion details from ACCOUNTING_Util
7451             okl_accounting_util.convert_to_functional_currency
7452                     (p_khr_id                        => p_term_rec.p_contract_id,
7453                      p_to_currency                   => l_functional_currency_code,
7454                      p_transaction_date              => p_sys_date,
7455                      p_amount                        => l_hard_coded_amount,
7456                      x_return_status                 => l_return_status,
7457                      x_contract_currency             => l_contract_currency_code,
7458                      x_currency_conversion_type      => l_currency_conversion_type,
7459                      x_currency_conversion_rate      => l_currency_conversion_rate,
7460                      x_currency_conversion_date      => l_currency_conversion_date,
7461                      x_converted_amount              => l_converted_amount
7462                     );
7463 
7464             IF (is_debug_statement_on)
7465             THEN
7466                okl_debug_pub.log_debug
7467                   (g_level_statement,
7468                    l_module_name,
7469                       'called okl_accounting_util.convert_to_functional_currency , return status: '
7470                    || l_return_status
7471                   );
7472                okl_debug_pub.log_debug (g_level_statement,
7473                                         l_module_name,
7474                                            'l_contract_currency_code: '
7475                                         || l_contract_currency_code
7476                                        );
7477                okl_debug_pub.log_debug (g_level_statement,
7478                                         l_module_name,
7479                                            'l_currency_conversion_type: '
7480                                         || l_currency_conversion_type
7481                                        );
7482                okl_debug_pub.log_debug (g_level_statement,
7483                                         l_module_name,
7484                                            'l_currency_conversion_rate: '
7485                                         || l_currency_conversion_rate
7486                                        );
7487                okl_debug_pub.log_debug (g_level_statement,
7488                                         l_module_name,
7489                                            'l_currency_conversion_date: '
7490                                         || l_currency_conversion_date
7491                                        );
7492                okl_debug_pub.log_debug (g_level_statement,
7493                                         l_module_name,
7494                                            'l_converted_amount: '
7495                                         || l_converted_amount
7496                                        );
7497             END IF;
7498 
7499             -- If error from OKL_ACCOUNTING_UTIL
7500             IF l_return_status <> okl_api.g_ret_sts_success
7501             THEN
7502                -- Error occurred when creating accounting entries for
7503                -- transaction TRX_TYPE.
7504                okl_api.set_message (p_app_name          => g_app_name,
7505                                     p_msg_name          => 'OKL_AM_ERR_ACC_ENT',
7506                                     p_token1            => 'TRX_TYPE',
7507                                     p_token1_value      => l_trans_meaning
7508                                    );
7509             END IF;
7510 
7511             -- Raise exception to rollback to savepoint for this block
7512             IF (l_return_status = g_ret_sts_unexp_error)
7513             THEN
7514                RAISE okl_api.g_exception_unexpected_error;
7515             ELSIF (l_return_status = g_ret_sts_error)
7516             THEN
7517                RAISE okl_api.g_exception_error;
7518             END IF;
7519 
7520             i := 1;
7521 
7522             FOR k_bal_lns_rec IN k_bal_lns_csr (p_term_rec.p_contract_id)
7523             LOOP
7524                -- do accounting entries to get code_combination_id
7525                -- Set the tmpl_identify_rec in parameter
7526                lp_tmpl_identify_rec.product_id := l_pdt_id;
7527                lp_tmpl_identify_rec.transaction_type_id := l_try_id;
7528                lp_tmpl_identify_rec.memo_yn := g_no;
7529                lp_tmpl_identify_rec.prior_year_yn := g_no;
7530                lp_tmpl_identify_rec.stream_type_id :=
7531                                                  k_bal_lns_rec.stream_type_id;
7532                -- Set the dist_info_rec in parameter
7533                lp_dist_info_rec.source_id := lx_adjv_rec.ID;
7534                lp_dist_info_rec.source_table := 'OKL_TRX_AR_ADJSTS_B';
7535                lp_dist_info_rec.accounting_date := p_sys_date;
7536                lp_dist_info_rec.gl_reversal_flag := g_no;
7537                lp_dist_info_rec.post_to_gl := g_no;
7538                lp_dist_info_rec.contract_id := p_term_rec.p_contract_id;
7539                lp_dist_info_rec.amount := k_bal_lns_rec.amount;
7540                -- Set the p_dist_info_rec for currency code
7541                lp_dist_info_rec.currency_code := l_contract_currency_code;
7542 
7543                -- If the functional currency code is different
7544                -- from contract currency code
7545                -- then set the rest of the currency conversion columns
7546                IF l_functional_currency_code <> l_contract_currency_code
7547                THEN
7548                   -- Set the p_dist_info_rec currency conversion columns
7549                   lp_dist_info_rec.currency_conversion_type :=
7550                                                    l_currency_conversion_type;
7551                   lp_dist_info_rec.currency_conversion_rate :=
7552                                                    l_currency_conversion_rate;
7553                   lp_dist_info_rec.currency_conversion_date :=
7554                                                    l_currency_conversion_date;
7555                END IF;
7556 
7557                -- RMUNJULU 28-APR-04 3596626 Added code to set lp_acc_gen_primary_key_tbl
7558                -- for account generator
7559                IF (is_debug_statement_on)
7560                THEN
7561                   okl_debug_pub.log_debug
7562                              (g_level_statement,
7563                               l_module_name,
7564                               'calling OKL_ACC_CALL_PVT.okl_populate_acc_gen'
7565                              );
7566                END IF;
7567 
7568                okl_acc_call_pvt.okl_populate_acc_gen
7569                                  (p_contract_id           => p_term_rec.p_contract_id,
7570                                   p_contract_line_id      => NULL,
7571                                   x_acc_gen_tbl           => lp_acc_gen_primary_key_tbl,
7572                                   x_return_status         => l_return_status
7573                                  );
7574 
7575                IF (is_debug_statement_on)
7576                THEN
7577                   okl_debug_pub.log_debug
7578                      (g_level_statement,
7579                       l_module_name,
7580                          'called OKL_ACC_CALL_PVT.okl_populate_acc_gen , return status: '
7581                       || l_return_status
7582                      );
7583                END IF;
7584 
7585                -- Raise exception to rollback to savepoint for this block
7586                IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7587                THEN
7588                   RAISE okl_api.g_exception_unexpected_error;
7589                ELSIF (l_return_status = okl_api.g_ret_sts_error)
7590                THEN
7591                   RAISE okl_api.g_exception_error;
7592                END IF;
7593 
7594                IF (is_debug_statement_on)
7595                THEN
7596                   okl_debug_pub.log_debug
7597                        (g_level_statement,
7598                         l_module_name,
7599                         'calling OKL_ACCOUNT_DIST_PUB.create_accounting_dist'
7600                        );
7601                END IF;
7602 
7603                -- call accounting engine
7604                -- This will calculate the adjstmnts and generate accounting entries
7605                okl_account_dist_pub.create_accounting_dist
7606                      (p_api_version                  => p_api_version,
7607                       p_init_msg_list                => okl_api.g_false,
7608                       x_return_status                => l_return_status,
7609                       x_msg_count                    => x_msg_count,
7610                       x_msg_data                     => x_msg_data,
7611                       p_tmpl_identify_rec            => lp_tmpl_identify_rec,
7612                       p_dist_info_rec                => lp_dist_info_rec,
7613                       p_ctxt_val_tbl                 => lp_ctxt_val_tbl,
7614                       p_acc_gen_primary_key_tbl      => lp_acc_gen_primary_key_tbl,
7615                       x_template_tbl                 => lx_template_tbl,
7616                       x_amount_tbl                   => lx_amount_tbl
7617                      );
7618 
7619                IF (is_debug_statement_on)
7620                THEN
7621                   okl_debug_pub.log_debug
7622                      (g_level_statement,
7623                       l_module_name,
7624                          'called OKL_ACCOUNT_DIST_PUB.create_accounting_dist , return status: '
7625                       || l_return_status
7626                      );
7627                END IF;
7628 
7629                IF l_return_status <> g_ret_sts_success
7630                THEN
7631                   -- Error occurred when creating accounting entries
7632                   -- for transaction type TRX_TYPE and stream type STREAM_TYPE.
7633                   okl_api.set_message
7634                               (p_app_name          => g_app_name,
7635                                p_msg_name          => 'OKL_AM_ERR_ACC_ENT_MSG',
7636                                p_token1            => 'TRX_TYPE',
7637                                p_token1_value      => l_trans_meaning,
7638                                p_token2            => 'STREAM_TYPE',
7639                                p_token2_value      => k_bal_lns_rec.stream_meaning
7640                               );
7641                END IF;
7642 
7643                -- Raise exception to rollback this whole block
7644                IF (l_return_status = g_ret_sts_unexp_error)
7645                THEN
7646                   RAISE okl_api.g_exception_unexpected_error;
7647                ELSIF (l_return_status = g_ret_sts_error)
7648                THEN
7649                   RAISE okl_api.g_exception_error;
7650                END IF;
7651 
7652                -- Get the first code_combination_id for the transaction
7653                -- from OKL_TRNS_ACC_DSTRS_V
7654                OPEN code_combination_id_csr (lx_adjv_rec.ID,
7655                                              'OKL_TRX_AR_ADJSTS_B');
7656 
7657                FETCH code_combination_id_csr
7658                 INTO l_code_combination_id;
7659 
7660                CLOSE code_combination_id_csr;
7661 
7662                -- if code_combination_id not found then raise error
7663                IF l_code_combination_id = -1 OR l_code_combination_id IS NULL
7664                THEN
7665                   -- Error: Unable to process small balance
7666                   -- adjustments because of a missing Code Combination ID for the
7667                   -- contract CONTRACT_NUMBER.
7668                   okl_api.set_message
7669                               (p_app_name          => g_app_name,
7670                                p_msg_name          => 'OKL_AM_CODE_CMB_ERROR',
7671                                p_token1            => 'CONTRACT_NUMBER',
7672                                p_token1_value      => p_term_rec.p_contract_number
7673                               );
7674                   RAISE okl_api.g_exception_error;
7675                END IF;
7676 
7677                -- Loop thru the code combination ids to set the lns tbl
7678                FOR code_combination_id_rec IN
7679                   code_combination_id_csr (lx_adjv_rec.ID,
7680                                            'OKL_TRX_AR_ADJSTS_B')
7681                LOOP
7682                   -- set the tbl for adjsts lns
7683                   lp_ajlv_tbl (i).adj_id := lx_adjv_rec.ID;
7684                   lp_ajlv_tbl (i).til_id := k_bal_lns_rec.til_id;
7685 
7686                   IF     k_bal_lns_rec.tld_id <> -999
7687                      AND k_bal_lns_rec.tld_id IS NOT NULL
7688                      AND k_bal_lns_rec.tld_id <> okl_api.g_miss_num
7689                   THEN
7690                      lp_ajlv_tbl (i).tld_id := k_bal_lns_rec.tld_id;
7691                   END IF;
7692 
7693                   lp_ajlv_tbl (i).amount := k_bal_lns_rec.amount;
7694                   lp_ajlv_tbl (i).psl_id := k_bal_lns_rec.schedule_id;
7695                   lp_ajlv_tbl (i).code_combination_id :=
7696                                    code_combination_id_rec.code_combination_id;
7697                   i := i + 1;
7698                END LOOP;                              -- code combination recs
7699             END LOOP;                                          -- balances res
7700 
7701             IF (is_debug_statement_on)
7702             THEN
7703                okl_debug_pub.log_debug
7704                       (g_level_statement,
7705                        l_module_name,
7706                        'calling OKL_TXL_ADJSTS_LNS_PUB.insert_txl_adjsts_lns'
7707                       );
7708             END IF;
7709 
7710             --call the txl_lns_adjsts
7711             okl_txl_adjsts_lns_pub.insert_txl_adjsts_lns
7712                                           (p_api_version        => p_api_version,
7713                                            p_init_msg_list      => okl_api.g_false,
7714                                            x_return_status      => l_return_status,
7715                                            x_msg_count          => x_msg_count,
7716                                            x_msg_data           => x_msg_data,
7717                                            p_ajlv_tbl           => lp_ajlv_tbl,
7718                                            x_ajlv_tbl           => lx_ajlv_tbl
7719                                           );
7720 
7721             IF (is_debug_statement_on)
7722             THEN
7723                okl_debug_pub.log_debug
7724                   (g_level_statement,
7725                    l_module_name,
7726                       'called OKL_TXL_ADJSTS_LNS_PUB.insert_txl_adjsts_lns , return status: '
7727                    || l_return_status
7728                   );
7729             END IF;
7730 
7731             IF l_return_status <> g_ret_sts_success
7732             THEN
7733                -- Error occurred when creating adjustment records to write
7734                -- off balances.
7735                okl_api.set_message (p_app_name      => g_app_name,
7736                                     p_msg_name      => 'OKL_AM_ERR_ADJST_BAL');
7737             END IF;
7738 
7739             -- Raise exception to rollback this whole block
7740             IF (l_return_status = g_ret_sts_unexp_error)
7741             THEN
7742                RAISE okl_api.g_exception_unexpected_error;
7743             ELSIF (l_return_status = g_ret_sts_error)
7744             THEN
7745                RAISE okl_api.g_exception_error;
7746             END IF;
7747 
7748             -- Get the currency code for contract
7749             l_currency_code :=
7750                    okl_am_util_pvt.get_chr_currency (p_term_rec.p_contract_id);
7751 
7752             -- Set all success messages for all balances
7753             FOR k_bal_lns_rec IN k_bal_lns_csr (p_term_rec.p_contract_id)
7754             LOOP
7755                -- Format the adjustment amt
7756                l_formatted_adj_amt :=
7757                   okl_accounting_util.format_amount (k_bal_lns_rec.amount,
7758                                                      l_currency_code);
7759                -- Append adjustment amt with currency code
7760                l_formatted_adj_amt :=
7761                                 l_formatted_adj_amt || ' ' || l_currency_code;
7762                -- Set the success message in the message table
7763 
7764                -- Adjustment transaction for AR invoice AR_INVOICE_NUM of amount AMOUNT
7765                -- has been created.
7766                px_msg_tbl (l_count).msg_desc := 'OKL_AM_ACC_ENT_AR_INV_MSG';
7767                px_msg_tbl (l_count).msg_token1 := 'AR_INVOICE_NUM';
7768                px_msg_tbl (l_count).msg_token1_value :=
7769                                               k_bal_lns_rec.ar_invoice_number;
7770                px_msg_tbl (l_count).msg_token2 := 'AMOUNT';
7771                px_msg_tbl (l_count).msg_token2_value := l_formatted_adj_amt;
7772                l_count := l_count + 1;
7773                -- Accounting entries created for transaction type TRX_TYPE
7774                -- and stream type STREAM_TYPE.
7775                px_msg_tbl (l_count).msg_desc := 'OKL_AM_ACC_ENT_CREATED_MSG';
7776                px_msg_tbl (l_count).msg_token1 := 'TRX_TYPE';
7777                px_msg_tbl (l_count).msg_token1_value := l_trans_meaning;
7778                px_msg_tbl (l_count).msg_token2 := 'STREAM_TYPE';
7779                px_msg_tbl (l_count).msg_token2_value :=
7780                                                  k_bal_lns_rec.stream_meaning;
7781                l_count := l_count + 1;
7782             END LOOP;
7783          ELSE
7784             --(cannot close all balances since tolerance amt is less)
7785 
7786             -- Unable to close all outstanding balances due to tolerance amount.
7787             okl_api.set_message (p_app_name      => g_app_name,
7788                                  p_msg_name      => 'OKL_AM_ERR_CLOSE_BAL');
7789             -- Get the currency code for contract
7790             l_currency_code :=
7791                   okl_am_util_pvt.get_chr_currency (p_term_rec.p_contract_id);
7792             -- Format the balance amt
7793             l_formatted_bal_amt :=
7794                okl_accounting_util.format_amount (l_total_amount_due,
7795                                                   l_currency_code);
7796             -- Append balance amt with currency code
7797             l_formatted_bal_amt :=
7798                                 l_formatted_bal_amt || ' ' || l_currency_code;
7799             -- Format the tolerance amt
7800             l_formatted_tol_amt :=
7801                okl_accounting_util.format_amount (l_tolerance_amt,
7802                                                   l_currency_code);
7803             -- Append tolerance amt with currency code
7804             l_formatted_tol_amt :=
7805                                 l_formatted_tol_amt || ' ' || l_currency_code;
7806             -- Outstanding balance BALANCE_AMT exceeds Tolerance Amount TOLERANCE_AMT.
7807             okl_api.set_message (p_app_name          => g_app_name,
7808                                  p_msg_name          => 'OKL_AM_BAL_GTR_TOL',
7809                                  p_token1            => 'BALANCE_AMT',
7810                                  p_token1_value      => l_formatted_bal_amt,
7811                                  p_token2            => 'TOLERANCE_AMT',
7812                                  p_token2_value      => l_formatted_tol_amt
7813                                 );
7814          END IF;
7815       END IF;
7816 
7817       x_return_status := l_return_status;
7818 
7819       IF (is_debug_procedure_on)
7820       THEN
7821          okl_debug_pub.log_debug (g_level_procedure,
7822                                   l_module_name,
7823                                   'End(-)'
7824                                  );
7825       END IF;
7826    EXCEPTION
7827       WHEN okl_api.g_exception_error
7828       THEN
7829          IF k_balances_csr%ISOPEN
7830          THEN
7831             CLOSE k_balances_csr;
7832          END IF;
7833 
7834          IF k_bal_lns_csr%ISOPEN
7835          THEN
7836             CLOSE k_bal_lns_csr;
7837          END IF;
7838 
7839          IF code_combination_id_csr%ISOPEN
7840          THEN
7841             CLOSE code_combination_id_csr;
7842          END IF;
7843 
7844          ROLLBACK TO close_balances;
7845          x_return_status := okl_api.g_ret_sts_error;
7846 
7847          IF (is_debug_exception_on)
7848          THEN
7849             okl_debug_pub.log_debug (g_level_exception,
7850                                      l_module_name,
7851                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
7852                                     );
7853          END IF;
7854       WHEN okl_api.g_exception_unexpected_error
7855       THEN
7856          IF k_balances_csr%ISOPEN
7857          THEN
7858             CLOSE k_balances_csr;
7859          END IF;
7860 
7861          IF k_bal_lns_csr%ISOPEN
7862          THEN
7863             CLOSE k_bal_lns_csr;
7864          END IF;
7865 
7866          IF code_combination_id_csr%ISOPEN
7867          THEN
7868             CLOSE code_combination_id_csr;
7869          END IF;
7870 
7871          ROLLBACK TO close_balances;
7872          x_return_status := okl_api.g_ret_sts_unexp_error;
7873 
7874          IF (is_debug_exception_on)
7875          THEN
7876             okl_debug_pub.log_debug (g_level_exception,
7877                                      l_module_name,
7878                                         'EXCEPTION :'
7879                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
7880                                     );
7881          END IF;
7882       WHEN OTHERS
7883       THEN
7884          IF k_balances_csr%ISOPEN
7885          THEN
7886             CLOSE k_balances_csr;
7887          END IF;
7888 
7889          IF k_bal_lns_csr%ISOPEN
7890          THEN
7891             CLOSE k_bal_lns_csr;
7892          END IF;
7893 
7894          IF code_combination_id_csr%ISOPEN
7895          THEN
7896             CLOSE code_combination_id_csr;
7897          END IF;
7898 
7899          ROLLBACK TO close_balances;
7900          x_return_status := okl_api.g_ret_sts_unexp_error;
7901          -- Set the oracle error message
7902          okl_api.set_message (p_app_name          => g_app_name_1,
7903                               p_msg_name          => g_unexpected_error,
7904                               p_token1            => g_sqlcode_token,
7905                               p_token1_value      => SQLCODE,
7906                               p_token2            => g_sqlerrm_token,
7907                               p_token2_value      => SQLERRM
7908                              );
7909 
7910          IF (is_debug_exception_on)
7911          THEN
7912             okl_debug_pub.log_debug (g_level_exception,
7913                                      l_module_name,
7914                                         'EXCEPTION :'
7915                                      || 'OTHERS, SQLCODE: '
7916                                      || SQLCODE
7917                                      || ' , SQLERRM : '
7918                                      || SQLERRM
7919                                     );
7920          END IF;
7921    END close_balances;
7922 
7923    -- Start of comments
7924    --
7925    -- Procedure Name : mass_rebook
7926    -- Desciption     : Will do a mass rebook of the contract may have some
7927    --                  changed lines
7928    -- Business Rules :
7929    -- Parameters     :
7930    -- Version        : 1.0
7931    -- History        : RMUNJULU -- 04-DEC-02 Bug # 2484327, removed unnecessary
7932    --                  cursors and changed others, moved the if condition
7933    --                  Changed code to set savepoint instead of start/end activity
7934    --                : RMUNJULU 16-DEC-02 Bug # 2484327, changed cursor to get lines
7935    --                  which are of the same status as header
7936    --                : RMUNJULU 20-DEC-02 2484327, corrected the cursor for aliases
7937    --                : RMUNJULU 07-JAN-03 2736865 Removed processing messages
7938    --                : RMUNJULU 23-JAN-03 2762065 removed log msgs here, see
7939    --                  msgs explanation comments in asset_level_termination
7940    --                : SECHAWLA 10-FEB-03 2793689 Removed the NOT NULL residual
7941    --                  value condition from l_okclines_csr
7942    --                  cursor, as the LOAN contracts do not have a residual value.
7943    --                  Mass Rebook works fine when we pass a null residual value
7944    --                : RMUNJULU 14-FEB-03 2804703 Added new cursor and check to
7945    --                  do mass rebook when contract not evergreen
7946    --                : RMUNJULU 06-MAR-03 Performance Fix Replaced K_LNS_FULL
7947    --                : RMUNJULU CONTRACT BLOCKING Changed MASS_REBOOK to check if
7948    --                  the Rebook TRN processed. If NOT then x_mrbk_success = 'E' else 'S'
7949    --                : RMUNJULU CONTRACT BLOCKING Changed MASS_REBOOK to Call New
7950    --                  Mass Rebook API done only thru terminations
7951    --                : rmunjulu EDAT Added code to get quote eff date and call new overloaded mass_rebook api
7952    --                  which takes trx date as parameter
7953    -- End of comments
7954    PROCEDURE mass_rebook (
7955       p_api_version     IN              NUMBER,
7956       p_init_msg_list   IN              VARCHAR2,
7957       x_msg_count       OUT NOCOPY      NUMBER,
7958       x_msg_data        OUT NOCOPY      VARCHAR2,
7959       x_return_status   OUT NOCOPY      VARCHAR2,
7960       p_term_rec        IN              term_rec_type,
7961       p_tcnv_rec        IN              tcnv_rec_type,
7962       p_sys_date        IN              DATE,                 -- rmunjulu EDAT
7963       x_mrbk_success    OUT NOCOPY      VARCHAR2
7964    )
7965    IS
7966       -- RMUNJULU CONTRACT BLOCKING ADDED
7967 
7968       -- RMUNJULU -- Bug # 2484327 changed to cater to plsql standards
7969       -- This cursor is used to get a BOOKED contract line.
7970       -- RMUNJULU 16-DEC-02 Bug # 2484327, get lines which are of the same status as header
7971       -- RMUNJULU 20-DEC-02 2484327, corrected the cursor for aliases
7972       -- SECHAWLA 10-FEB-03 2793689, Removed the NOT NULL residual value condition
7973       -- from the following cursor, as the LOAN contracts do not have a residual value.
7974       -- Mass Rebook works fine when we pass a null residual value
7975       -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_LNS_FULL
7976       CURSOR l_okclines_csr (
7977          p_khr_id   IN   NUMBER
7978       )
7979       IS
7980          SELECT cle.ID ID,
7981                 cle.NAME NAME,
7982                 kle.residual_value residual_value
7983            FROM okl_k_lines_v kle,
7984                 okc_k_lines_v cle,
7985                 okc_k_headers_v khr
7986           WHERE cle.chr_id = p_khr_id
7987             AND cle.chr_id = khr.ID
7988             AND cle.sts_code = khr.sts_code
7989             AND kle.ID = cle.ID
7990             AND ROWNUM < 2;
7991 
7992       --Bug# 3999921: pagarg +++ T and A +++++++ Start ++++++++++
7993       --Cursor to obtain all the quote asset lines
7994       CURSOR l_quote_assets_csr (
7995          p_qte_id   IN   NUMBER
7996       )
7997       IS
7998          SELECT qlt.kle_id
7999            FROM okl_txl_quote_lines_b qlt
8000           WHERE qlt.qlt_code = 'AMCFIA' AND qlt.qte_id = p_qte_id;
8001 
8002       l_counter                 NUMBER;
8003 
8004       --Bug# 3999921: pagarg +++ T and A +++++++ End ++++++++++
8005 
8006       -- RMUNJULU 14-FEB-03 2804703 Added cursor
8007       -- Get the k sts_code
8008       CURSOR l_k_details_csr (
8009          p_khr_id   IN   NUMBER
8010       )
8011       IS
8012          SELECT khr.sts_code
8013            FROM okc_k_headers_v khr
8014           WHERE khr.ID = p_khr_id;
8015 
8016       -- RMUNJULU CONTRACT BLOCKING
8017       -- RMUNJULU CONTRACT BLOCKING (2)
8018       -- Get the REBOOK TRN details
8019       CURSOR get_trn_details_csr (
8020          p_trx_id   IN   NUMBER
8021       )
8022       IS
8023          SELECT trn.tsu_code
8024            FROM okl_trx_contracts trn
8025           WHERE trn.ID = p_trx_id;
8026 
8027       l_strm_lalevl_empty_tbl   okl_mass_rebook_pub.strm_lalevl_tbl_type;
8028       l_rbk_tbl                 okl_mass_rebook_pub.rbk_tbl_type;
8029       l_return_status           VARCHAR2 (1)              := g_ret_sts_success;
8030       l_api_name       CONSTANT VARCHAR2 (30)                 := 'mass_rebook';
8031       l_module_name             VARCHAR2 (500)
8032                                              := g_module_name || 'mass_rebook';
8033       is_debug_exception_on     BOOLEAN
8034               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
8035       is_debug_procedure_on     BOOLEAN
8036               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
8037       is_debug_statement_on     BOOLEAN
8038               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
8039       l_api_version    CONSTANT NUMBER                                   := 1;
8040       l_contract_number         VARCHAR2 (120);
8041       l_id                      NUMBER;
8042       l_name                    VARCHAR2 (150);
8043       l_residual_value          NUMBER;
8044       l_dummy                   VARCHAR2 (1);
8045       l_tcnv_rec                tcnv_rec_type                    := p_tcnv_rec;
8046       -- RMUNJULU 14-FEB-03 2804703 Added variable
8047       l_sts_code                VARCHAR2 (200);
8048       -- RMUNJULU CONTRACT BLOCKING (2)
8049       l_mass_rebook_trx_id      NUMBER;
8050       -- rmunjulu EDAT
8051       l_quote_accpt_date        DATE;
8052       l_quote_eff_date          DATE;
8053    BEGIN
8054       -- RMUNJULU Bug # 2484327, added code to set savepoint
8055       -- Start a savepoint to rollback to if error in this block
8056       SAVEPOINT mass_rebook;
8057 
8058       IF (is_debug_procedure_on)
8059       THEN
8060          okl_debug_pub.log_debug (g_level_procedure,
8061                                   l_module_name,
8062                                   'Begin(+)'
8063                                  );
8064       END IF;
8065 
8066       IF (is_debug_statement_on)
8067       THEN
8068          okl_debug_pub.log_debug (g_level_statement,
8069                                   l_module_name,
8070                                   'In param, p_sys_date: ' || p_sys_date
8071                                  );
8072          okl_debug_pub.log_debug (g_level_statement,
8073                                   l_module_name,
8074                                      'In param, p_term_rec.p_quote_id: '
8075                                   || p_term_rec.p_quote_id
8076                                  );
8077          okl_debug_pub.log_debug (g_level_statement,
8078                                   l_module_name,
8079                                      'In param, p_term_rec.p_contract_id: '
8080                                   || p_term_rec.p_contract_id
8081                                  );
8082          okl_debug_pub.log_debug
8083                                 (g_level_statement,
8084                                  l_module_name,
8085                                     'In param, p_term_rec.p_contract_number: '
8086                                  || p_term_rec.p_contract_number
8087                                 );
8088          okl_debug_pub.log_debug (g_level_statement,
8089                                   l_module_name,
8090                                   'In param, p_tcnv_rec.id: ' || p_tcnv_rec.ID
8091                                  );
8092       END IF;
8093 
8094       -- RMUNJULU CONTRACT BLOCKING
8095       x_mrbk_success := g_ret_sts_success;
8096 
8097       -- Get the contract line details
8098       OPEN l_okclines_csr (p_term_rec.p_contract_id);
8099 
8100       FETCH l_okclines_csr
8101        INTO l_id,
8102             l_name,
8103             l_residual_value;
8104 
8105       CLOSE l_okclines_csr;
8106 
8107       -- RMUNJULU Bug # 2484327, moved this if down from above cursor loop
8108       IF l_id IS NULL
8109       THEN
8110          -- SECHAWLA 10-FEB-03 2793689: Modified the following message
8111          -- There are no booked lines for the contract CONTRACT_NUMBER
8112          okl_api.set_message (p_app_name          => okl_api.g_app_name,
8113                               p_msg_name          => 'OKL_AM_NO_BOOKED_LINES',
8114                               p_token1            => 'CONTRACT_NUMBER',
8115                               p_token1_value      => p_term_rec.p_contract_number
8116                              );
8117          RAISE okl_api.g_exception_error;
8118       END IF;
8119 
8120       -- RMUNJULU 14-FEB-03 2804703 get sts_code for contract
8121       OPEN l_k_details_csr (p_term_rec.p_contract_id);
8122 
8123       FETCH l_k_details_csr
8124        INTO l_sts_code;
8125 
8126       CLOSE l_k_details_csr;
8127 
8128       -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
8129 
8130       -- rmunjulu EDAT
8131       -- If quote exists then accnting date is quote accept date else sysdate
8132       IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
8133       THEN
8134          l_quote_accpt_date :=
8135                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
8136          l_quote_eff_date :=
8137                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
8138       ELSE
8139          l_quote_accpt_date := p_sys_date;
8140          l_quote_eff_date := p_sys_date;
8141       END IF;
8142 
8143       -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
8144 
8145       -- RMUNJULU 14-FEB-03 2804703 Added IF to check contract not already
8146       -- EVERGREEN. Do mass rebook when contract not evergreen
8147       IF l_sts_code <> 'EVERGREEN'
8148       THEN
8149          --Bug# 3999921: pagarg +++ T and A +++++++ Start ++++++++++
8150          --Pass all the quote assets to mass rebook api
8151          l_counter := 0;
8152 
8153          FOR l_quote_assets_rec IN l_quote_assets_csr (p_term_rec.p_quote_id)
8154          LOOP
8155             l_counter := l_counter + 1;
8156             l_rbk_tbl (l_counter).khr_id := p_term_rec.p_contract_id;
8157             l_rbk_tbl (l_counter).kle_id := l_quote_assets_rec.kle_id;
8158          END LOOP;
8159 
8160          --Bug# 3999921: pagarg +++ T and A +++++++ End ++++++++++
8161          IF (is_debug_statement_on)
8162          THEN
8163             okl_debug_pub.log_debug
8164                              (g_level_statement,
8165                               l_module_name,
8166                               'calling OKL_MASS_REBOOK_PVT.apply_mass_rebook'
8167                              );
8168          END IF;
8169 
8170          -- rmunjulu EDAT Changed the signature to call new API which takes transaction date
8171          okl_mass_rebook_pvt.apply_mass_rebook
8172             (p_api_version             => p_api_version,
8173              p_init_msg_list           => g_false,                 --  Changed
8174              x_return_status           => l_return_status,
8175              x_msg_count               => x_msg_count,
8176              x_msg_data                => x_msg_data,
8177              p_rbk_tbl                 => l_rbk_tbl,
8178              p_deprn_method_code       => NULL,
8179              p_in_service_date         => NULL,
8180              p_life_in_months          => NULL,
8181              p_basic_rate              => NULL,
8182              p_adjusted_rate           => NULL,
8183              --Bug# : pagarg +++ T and A ++++
8184              --Pass residual value as null
8185              p_residual_value          => NULL,
8186              p_strm_lalevl_tbl         => l_strm_lalevl_empty_tbl,
8187              p_source_trx_id           => p_tcnv_rec.ID,
8188                                              -- RMUNJULU CONTRACT BLOCKING (2)
8189              p_source_trx_type         => 'TCN',
8190                                              -- RMUNJULU CONTRACT BLOCKING (2)
8191              p_transaction_date        => l_quote_eff_date,   -- rmunjulu EDAT
8192              x_mass_rebook_trx_id      => l_mass_rebook_trx_id
8193             );                               -- RMUNJULU CONTRACT BLOCKING (2)
8194 
8195          IF (is_debug_statement_on)
8196          THEN
8197             okl_debug_pub.log_debug
8198                (g_level_statement,
8199                 l_module_name,
8200                    'called OKL_MASS_REBOOK_PVT.apply_mass_rebook , return status: '
8201                 || l_return_status
8202                );
8203          END IF;
8204 
8205          IF l_return_status <> g_ret_sts_success
8206          THEN
8207             -- Mass Rebook failed for the contract CONTRACT_NUMBER
8208             okl_api.set_message
8209                               (p_app_name          => g_app_name,
8210                                p_msg_name          => 'OKL_AM_MASS_REBOOK_FAILED',
8211                                p_token1            => 'CONTRACT_NUMBER',
8212                                p_token1_value      => p_term_rec.p_contract_number
8213                               );
8214             RAISE okl_api.g_exception_error;
8215          -- RMUNJULU CONTRACT BLOCKING (2)
8216          ELSE
8217             -- Mass Rebook successful
8218             FOR get_trn_details_rec IN
8219                get_trn_details_csr (l_mass_rebook_trx_id)
8220             LOOP
8221                -- Mass Rebook is NOT Processed
8222                IF get_trn_details_rec.tsu_code <> 'PROCESSED'
8223                THEN
8224                   x_mrbk_success := g_ret_sts_error;
8225                END IF;
8226             END LOOP;
8227          END IF;
8228       END IF;
8229 
8230       -- Set the x_return_status
8231       x_return_status := l_return_status;
8232 
8233 
8234 
8235  -- RBRUNO BUG 6801022  START : UPDATE TERMINATION QUOTES FROM STATUS ACCEPTED
8236 
8237 
8238       IF (is_debug_statement_on)     THEN
8239          okl_debug_pub.log_debug (g_level_statement,
8240                                   l_module_name,
8241                                   'Invoking updating quote status');
8242                                   end if;
8243 
8244       --IF TERM QUOTE IN STATUS ACCEPTED EXISTS, UPDATE IT TO COMPLETE
8245       update_quote_status(p_term_rec);
8246 
8247       IF (is_debug_statement_on)   THEN
8248          okl_debug_pub.log_debug (g_level_statement,
8249                                   l_module_name,
8250                                   'post updating quote status');
8251                                   end if;
8252       -- RBRUNO BUG 6801022 END : UPDATE TERMINATION QUOTES FROM STATUS ACCEPTED
8253 
8254 
8255 
8256 
8257 
8258 
8259 
8260      IF (is_debug_procedure_on)
8261       THEN
8262          okl_debug_pub.log_debug (g_level_procedure,
8263                                   l_module_name,
8264                                   'End(-)'
8265                                  );
8266       END IF;
8267    EXCEPTION
8268       WHEN okl_api.g_exception_error
8269       THEN
8270          IF l_okclines_csr%ISOPEN
8271          THEN
8272             CLOSE l_okclines_csr;
8273          END IF;
8274 
8275          -- RMUNJULU 14-FEB-03 2804703 Added IF to check if cursor still open
8276          IF l_k_details_csr%ISOPEN
8277          THEN
8278             CLOSE l_k_details_csr;
8279          END IF;
8280 
8281          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
8282          -- return status
8283          ROLLBACK TO mass_rebook;
8284          x_return_status := g_ret_sts_error;
8285          -- RMUNJULU CONTRACT BLOCKING
8286          x_mrbk_success := g_ret_sts_error;
8287 
8288          IF (is_debug_exception_on)
8289          THEN
8290             okl_debug_pub.log_debug (g_level_exception,
8291                                      l_module_name,
8292                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
8293                                     );
8294          END IF;
8295       WHEN okl_api.g_exception_unexpected_error
8296       THEN
8297          IF l_okclines_csr%ISOPEN
8298          THEN
8299             CLOSE l_okclines_csr;
8300          END IF;
8301 
8302          -- RMUNJULU 14-FEB-03 2804703 Added IF to check if cursor still open
8303          IF l_k_details_csr%ISOPEN
8304          THEN
8305             CLOSE l_k_details_csr;
8306          END IF;
8307 
8308          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
8309          -- return status
8310          ROLLBACK TO mass_rebook;
8311          x_return_status := g_ret_sts_unexp_error;
8312          -- RMUNJULU CONTRACT BLOCKING
8313          x_mrbk_success := g_ret_sts_error;
8314 
8315          IF (is_debug_exception_on)
8316          THEN
8317             okl_debug_pub.log_debug (g_level_exception,
8318                                      l_module_name,
8319                                         'EXCEPTION :'
8320                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
8321                                     );
8322          END IF;
8323       WHEN OTHERS
8324       THEN
8325          IF l_okclines_csr%ISOPEN
8326          THEN
8327             CLOSE l_okclines_csr;
8328          END IF;
8329 
8330          -- RMUNJULU 14-FEB-03 2804703 Added IF to check if cursor still open
8331          IF l_k_details_csr%ISOPEN
8332          THEN
8333             CLOSE l_k_details_csr;
8334          END IF;
8335 
8336          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
8337          -- return status
8338          ROLLBACK TO mass_rebook;
8339          x_return_status := g_ret_sts_unexp_error;
8340          -- RMUNJULU CONTRACT BLOCKING
8341          x_mrbk_success := g_ret_sts_error;
8342          -- Set the oracle error message
8343          okl_api.set_message (p_app_name          => g_app_name_1,
8344                               p_msg_name          => g_unexpected_error,
8345                               p_token1            => g_sqlcode_token,
8346                               p_token1_value      => SQLCODE,
8347                               p_token2            => g_sqlerrm_token,
8348                               p_token2_value      => SQLERRM
8349                              );
8350 
8351          IF (is_debug_exception_on)
8352          THEN
8353             okl_debug_pub.log_debug (g_level_exception,
8354                                      l_module_name,
8355                                         'EXCEPTION :'
8356                                      || 'OTHERS, SQLCODE: '
8357                                      || SQLCODE
8358                                      || ' , SQLERRM : '
8359                                      || SQLERRM
8360                                     );
8361          END IF;
8362    END mass_rebook;
8363 
8364    -- Start of comments
8365    --
8366    -- Procedure Name : cancel_activate_insurance
8367    -- Desciption     : Will cancel and reactivate the insurances for the contract
8368    -- Business Rules :
8369    -- Parameters     :
8370    -- Version      : 1.0
8371    -- History        : RMUNJULU -- 04-DEC-02 Bug # 2484327
8372    --                  Changed code to set savepoint instead of start/end activity
8373    --                : RMUNJULU 14-FEB-03 2804703 Added code to check if contract
8374    --                  is EVERGREEN
8375    --                : RMUNJULU 03-MAR-03 2830997 Fixed the exception block
8376    --
8377    -- End of comments
8378    PROCEDURE cancel_activate_insurance (
8379       p_api_version     IN              NUMBER,
8380       p_init_msg_list   IN              VARCHAR2,
8381       x_msg_count       OUT NOCOPY      NUMBER,
8382       x_msg_data        OUT NOCOPY      VARCHAR2,
8383       x_return_status   OUT NOCOPY      VARCHAR2,
8384       p_term_rec        IN              term_rec_type,
8385       p_sys_date        IN              DATE,
8386       p_klev_tbl        IN              klev_tbl_type
8387    )
8388    IS
8389       -- Cursor to get the end date of contract
8390       -- RMUNJULU 14-FEB-03 2804703 Added code to get sts_code
8391       CURSOR k_end_date_csr (
8392          p_chr_id   IN   NUMBER
8393       )
8394       IS
8395          SELECT khr.end_date end_date,
8396                 khr.sts_code sts_code
8397            FROM okc_k_headers_v khr
8398           WHERE khr.ID = p_chr_id;
8399 
8400       l_k_end_date             DATE;
8401       l_return_status          VARCHAR2 (1)   := g_ret_sts_success;
8402       l_early_termination_yn   VARCHAR2 (1)   := g_no;
8403       i                        NUMBER;
8404       l_api_name               VARCHAR2 (30)  := 'cancel_act_insurance';
8405       l_module_name            VARCHAR2 (500)
8406                                := g_module_name || 'cancel_activate_insurance';
8407       is_debug_exception_on    BOOLEAN
8408               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
8409       is_debug_procedure_on    BOOLEAN
8410               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
8411       is_debug_statement_on    BOOLEAN
8412               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
8413       l_api_version   CONSTANT NUMBER         := 1;
8414       -- RMUNJULU 14-FEB-03 2804703 Added variable
8415       l_sts_code               VARCHAR2 (200);
8416    BEGIN
8417       -- RMUNJULU Bug # 2484327, added code to set savepoint
8418       -- Start a savepoint to rollback to if error in this block
8419       SAVEPOINT cancel_activate_insurance;
8420 
8421       IF (is_debug_procedure_on)
8422       THEN
8423          okl_debug_pub.log_debug (g_level_procedure,
8424                                   l_module_name,
8425                                   'Begin(+)'
8426                                  );
8427       END IF;
8428 
8429       IF (is_debug_statement_on)
8430       THEN
8431          okl_debug_pub.log_debug (g_level_statement,
8432                                   l_module_name,
8433                                   'In param, p_sys_date: ' || p_sys_date
8434                                  );
8435          okl_debug_pub.log_debug
8436                            (g_level_statement,
8437                             l_module_name,
8438                                'In param, p_term_rec.p_early_termination_yn: '
8439                             || p_term_rec.p_early_termination_yn
8440                            );
8441          okl_debug_pub.log_debug (g_level_statement,
8442                                   l_module_name,
8443                                      'In param, p_term_rec.p_contract_id: '
8444                                   || p_term_rec.p_contract_id
8445                                  );
8446       END IF;
8447 
8448       -- Get k end date
8449       -- RMUNJULU 14-FEB-03 2804703 Added sts_code
8450       OPEN k_end_date_csr (p_term_rec.p_contract_id);
8451 
8452       FETCH k_end_date_csr
8453        INTO l_k_end_date,
8454             l_sts_code;
8455 
8456       CLOSE k_end_date_csr;
8457 
8458       -- check if early termination
8459       IF TRUNC (l_k_end_date) > TRUNC (p_sys_date)
8460       THEN
8461          l_early_termination_yn := g_yes;
8462       END IF;
8463 
8464       -- if early termination then
8465       -- RMUNJULU 14-FEB-03 2804703 Added condition to check if contract not
8466       -- already evergreen
8467       IF     (   NVL (p_term_rec.p_early_termination_yn, '?') = g_yes
8468               OR l_early_termination_yn = g_yes
8469              )
8470          AND l_sts_code <> 'EVERGREEN'
8471       THEN
8472          IF (is_debug_statement_on)
8473          THEN
8474             okl_debug_pub.log_debug
8475                  (g_level_statement,
8476                   l_module_name,
8477                   'calling OKL_INSURANCE_POLICIES_PUB.cancel_create_policies'
8478                  );
8479          END IF;
8480 
8481          -- cancel and reactivate insurances
8482          okl_insurance_policies_pub.cancel_create_policies
8483                                         (p_api_version            => p_api_version,
8484                                          p_init_msg_list          => g_false,
8485                                          x_return_status          => l_return_status,
8486                                          x_msg_count              => x_msg_count,
8487                                          x_msg_data               => x_msg_data,
8488                                          p_khr_id                 => p_term_rec.p_contract_id,
8489                                          p_cancellation_date      => p_sys_date,
8490                                          p_transaction_id         => NULL
8491                                         );
8492 
8493          IF (is_debug_statement_on)
8494          THEN
8495             okl_debug_pub.log_debug
8496                (g_level_statement,
8497                 l_module_name,
8498                    'called OKL_INSURANCE_POLICIES_PUB.cancel_create_policies , return status: '
8499                 || l_return_status
8500                );
8501          END IF;
8502 
8503          IF l_return_status <> g_ret_sts_success
8504          THEN
8505             -- Error in cancelling Insurance.
8506             okl_api.set_message (p_app_name      => g_app_name,
8507                                  p_msg_name      => 'OKL_AM_ERR_CAN_INS');
8508          END IF;
8509 
8510          -- Raise exception to rollback to the savepoint
8511          IF (l_return_status = g_ret_sts_unexp_error)
8512          THEN
8513             RAISE okl_api.g_exception_unexpected_error;
8514          ELSIF (l_return_status = g_ret_sts_error)
8515          THEN
8516             RAISE okl_api.g_exception_error;
8517          END IF;
8518       END IF;
8519 
8520       -- Set the return status
8521       x_return_status := l_return_status;
8522 
8523       IF (is_debug_procedure_on)
8524       THEN
8525          okl_debug_pub.log_debug (g_level_procedure,
8526                                   l_module_name,
8527                                   'End(-)'
8528                                  );
8529       END IF;
8530    EXCEPTION
8531       WHEN okl_api.g_exception_error
8532       THEN
8533          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
8534          IF k_end_date_csr%ISOPEN
8535          THEN
8536             CLOSE k_end_date_csr;
8537          END IF;
8538 
8539          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
8540          -- return status
8541          ROLLBACK TO cancel_activate_insurance;
8542          x_return_status := g_ret_sts_error;
8543 
8544          IF (is_debug_exception_on)
8545          THEN
8546             okl_debug_pub.log_debug (g_level_exception,
8547                                      l_module_name,
8548                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
8549                                     );
8550          END IF;
8551       WHEN okl_api.g_exception_unexpected_error
8552       THEN
8553          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
8554          IF k_end_date_csr%ISOPEN
8555          THEN
8556             CLOSE k_end_date_csr;
8557          END IF;
8558 
8559          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
8560          -- return status
8561          ROLLBACK TO cancel_activate_insurance;
8562          x_return_status := g_ret_sts_unexp_error;
8563 
8564          IF (is_debug_exception_on)
8565          THEN
8566             okl_debug_pub.log_debug (g_level_exception,
8567                                      l_module_name,
8568                                         'EXCEPTION :'
8569                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
8570                                     );
8571          END IF;
8572       WHEN OTHERS
8573       THEN
8574          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
8575          IF k_end_date_csr%ISOPEN
8576          THEN
8577             CLOSE k_end_date_csr;
8578          END IF;
8579 
8580          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
8581          -- return status
8582          ROLLBACK TO cancel_activate_insurance;
8583          x_return_status := g_ret_sts_unexp_error;
8584          -- Set the oracle error message
8585          okl_api.set_message (p_app_name          => g_app_name_1,
8586                               p_msg_name          => g_unexpected_error,
8587                               p_token1            => g_sqlcode_token,
8588                               p_token1_value      => SQLCODE,
8589                               p_token2            => g_sqlerrm_token,
8590                               p_token2_value      => SQLERRM
8591                              );
8592 
8593          IF (is_debug_exception_on)
8594          THEN
8595             okl_debug_pub.log_debug (g_level_exception,
8596                                      l_module_name,
8597                                         'EXCEPTION :'
8598                                      || 'OTHERS, SQLCODE: '
8599                                      || SQLCODE
8600                                      || ' , SQLERRM : '
8601                                      || SQLERRM
8602                                     );
8603          END IF;
8604    END cancel_activate_insurance;
8605 
8606    -- Start of comments
8607    --
8608    -- Procedure Name : reverse_loss_provisions
8609    -- Desciption     : Will reverse the non-incomes and loss provisions for contract
8610    -- Business Rules :
8611    -- Parameters   :
8612    -- Version    : 1.0
8613    -- History        : RMUNJULU 09-JAN-03  2743604 Created
8614    --                : RMUNJULU 03-MAR-03 2830997 Fixed the exception block
8615    --                : RMUNJULU Bug # 3097068 20-AUG-03 Added code to pass
8616    --                  valid GL date to REVERSE LOSS PROVISIONS
8617    --                : RMUNJULU Bug # 3148215 19-SEP-03 Added code to pass
8618    --                  valid GL date to CATCHUP OF ACCRUALS
8619    --
8620    -- End of comments
8621    PROCEDURE reverse_loss_provisions (
8622       p_api_version     IN              NUMBER,
8623       p_init_msg_list   IN              VARCHAR2,
8624       x_msg_count       OUT NOCOPY      NUMBER,
8625       x_msg_data        OUT NOCOPY      VARCHAR2,
8626       x_return_status   OUT NOCOPY      VARCHAR2,
8627       p_term_rec        IN              term_rec_type,
8628       p_sys_date        IN              DATE,
8629       px_msg_tbl        IN OUT NOCOPY   g_msg_tbl
8630    )
8631    IS
8632       -- Cursor to get the product of the contract
8633       CURSOR prod_id_csr (
8634          p_khr_id   IN   NUMBER
8635       )
8636       IS
8637          SELECT A.pdt_id, A.MULTI_GAAP_YN, B.REPORTING_PDT_ID
8638            FROM okl_k_headers_v A,
8639                 okl_products B
8640           WHERE A.ID = p_khr_id
8641           AND   B.ID = A.pdt_id; -- MGAAP 7263041
8642 
8643       l_return_status         VARCHAR2 (1)                := g_ret_sts_success;
8644       l_try_id                NUMBER;
8645       l_trans_meaning         VARCHAR2 (200);
8646       l_pdt_id                NUMBER                                     := 0;
8647 
8648       -- MGAAP 7263041 start
8649       l_reporting_pdt_id      NUMBER                                     := 0;
8650       l_multi_gaap_yn      okl_k_headers.MULTI_GAAP_YN%TYPE              := null;
8651       l_valid_gl_date_rep     DATE;
8652       l_sob_id_rep            NUMBER;
8653       -- MGAAP 7263041 end
8654 
8655       l_catchup_rec           okl_generate_accruals_pub.accrual_rec_type;
8656       lx_tcnv_tbl             okl_trx_contracts_pub.tcnv_tbl_type;
8657       lx_tclv_tbl             okl_trx_contracts_pub.tclv_tbl_type;
8658       l_lprv_rec              okl_rev_loss_prov_pub.lprv_rec_type;
8659       l_count                 NUMBER;
8660       l_module_name           VARCHAR2 (500)
8661                                  := g_module_name || 'reverse_loss_provisions';
8662       is_debug_exception_on   BOOLEAN
8663               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
8664       is_debug_procedure_on   BOOLEAN
8665               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
8666       is_debug_statement_on   BOOLEAN
8667               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
8668       -- Bug 3097068
8669       l_valid_gl_date         DATE;
8670    BEGIN
8671       -- Start a savepoint to rollback to if error in this block
8672       SAVEPOINT reverse_loss_provisions;
8673 
8674       IF (is_debug_procedure_on)
8675       THEN
8676          okl_debug_pub.log_debug (g_level_procedure,
8677                                   l_module_name,
8678                                   'Begin(+)'
8679                                  );
8680       END IF;
8681 
8682       IF (is_debug_statement_on)
8683       THEN
8684          okl_debug_pub.log_debug (g_level_statement,
8685                                   l_module_name,
8686                                   'In param, p_sys_date: ' || p_sys_date
8687                                  );
8688          okl_debug_pub.log_debug (g_level_statement,
8689                                   l_module_name,
8690                                      'In param, p_term_rec.p_contract_id: '
8691                                   || p_term_rec.p_contract_id
8692                                  );
8693          okl_debug_pub.log_debug
8694                                 (g_level_statement,
8695                                  l_module_name,
8696                                     'In param, p_term_rec.p_contract_number: '
8697                                  || p_term_rec.p_contract_number
8698                                 );
8699       END IF;
8700 
8701 -- *************
8702 -- REVERSAL OF NON-INCOME
8703 -- *************
8704       IF (is_debug_statement_on)
8705       THEN
8706          okl_debug_pub.log_debug
8707                                 (g_level_statement,
8708                                  l_module_name,
8709                                  'calling OKL_AM_UTIL_PVT.get_transaction_id'
8710                                 );
8711       END IF;
8712 
8713       -- Get the transaction id for Accrual
8714       okl_am_util_pvt.get_transaction_id (p_try_name           => 'Accrual',
8715                                           x_return_status      => l_return_status,
8716                                           x_try_id             => l_try_id
8717                                          );
8718 
8719       IF (is_debug_statement_on)
8720       THEN
8721          okl_debug_pub.log_debug
8722             (g_level_statement,
8723              l_module_name,
8724                 'called OKL_AM_UTIL_PVT.get_transaction_id , return status: '
8725              || l_return_status
8726             );
8727          okl_debug_pub.log_debug (g_level_statement,
8728                                   l_module_name,
8729                                   'l_try_id: ' || l_try_id
8730                                  );
8731       END IF;
8732 
8733       -- Get the meaning of lookup
8734       l_trans_meaning :=
8735          okl_am_util_pvt.get_lookup_meaning
8736                                 (p_lookup_type      => 'OKL_ACCOUNTING_EVENT_TYPE',
8737                                  p_lookup_code      => 'ACCRUAL',
8738                                  p_validate_yn      => 'Y'
8739                                 );
8740 
8741       IF l_return_status <> g_ret_sts_success
8742       THEN
8743          -- Unable to find a transaction type for
8744          -- the transaction TRY_NAME
8745          okl_api.set_message (p_app_name          => g_app_name,
8746                               p_msg_name          => 'OKL_AM_NO_TRX_TYPE_FOUND',
8747                               p_token1            => 'TRY_NAME',
8748                               p_token1_value      => l_trans_meaning
8749                              );
8750          -- Unable to do reversal of non-income during termination
8751          -- of contract CONTRACT_NUMBER.
8752          okl_api.set_message (p_app_name          => g_app_name,
8753                               p_msg_name          => 'OKL_AM_REV_NONINC_ERR',
8754                               p_token1            => 'CONTRACT_NUMBER',
8755                               p_token1_value      => p_term_rec.p_contract_number
8756                              );
8757       END IF;
8758 
8759       -- Raise exception to rollback this whole block
8760       IF (l_return_status = g_ret_sts_unexp_error)
8761       THEN
8762          RAISE okl_api.g_exception_unexpected_error;
8763       ELSIF (l_return_status = g_ret_sts_error)
8764       THEN
8765          RAISE okl_api.g_exception_error;
8766       END IF;
8767 
8768       -- get the product id
8769       OPEN prod_id_csr (p_term_rec.p_contract_id);
8770 
8771       FETCH prod_id_csr
8772        INTO l_pdt_id,
8773             l_multi_gaap_yn,
8774             l_reporting_pdt_id;  -- MGAAP 7263041
8775 
8776       CLOSE prod_id_csr;
8777 
8778       -- raise error if no pdt_id
8779       IF l_pdt_id IS NULL OR l_pdt_id = 0
8780       THEN
8781          -- Unable to do reversal of non-income during termination
8782          -- of contract CONTRACT_NUMBER.
8783          okl_api.set_message (p_app_name          => g_app_name,
8784                               p_msg_name          => 'OKL_AM_REV_NONINC_ERR',
8785                               p_token1            => 'CONTRACT_NUMBER',
8786                               p_token1_value      => p_term_rec.p_contract_number
8787                              );
8788          RAISE okl_api.g_exception_error;
8789       END IF;
8790 
8791       -- Bug 3097068 Added to get the valid GL date
8792       l_valid_gl_date :=
8793                okl_accounting_util.get_valid_gl_date (p_gl_date      => p_sys_date);
8794       -- Set the l_catchup_rec rec type
8795       l_catchup_rec.contract_id := p_term_rec.p_contract_id;
8796       l_catchup_rec.accrual_date := l_valid_gl_date;       -- RMUNJULU 3148215
8797       l_catchup_rec.contract_number := p_term_rec.p_contract_number;
8798       l_catchup_rec.rule_result := g_yes;
8799       l_catchup_rec.override_status := g_no;
8800       l_catchup_rec.product_id := l_pdt_id;
8801       l_catchup_rec.trx_type_id := l_try_id;
8802       l_catchup_rec.advance_arrears := NULL;
8803       l_catchup_rec.factoring_synd_flag := NULL;
8804       l_catchup_rec.post_to_gl := g_yes;
8805       l_catchup_rec.gl_reversal_flag := g_no;
8806       l_catchup_rec.memo_yn := g_no;
8807       l_catchup_rec.description :=
8808                             'Catchup of income on termination of the contract';
8809 
8810       IF (is_debug_statement_on)
8811       THEN
8812          okl_debug_pub.log_debug
8813                         (g_level_statement,
8814                          l_module_name,
8815                          'calling OKL_GENERATE_ACCRUALS_PUB.catchup_accruals'
8816                         );
8817       END IF;
8818 
8819       -- Do reversal of non-income
8820       okl_generate_accruals_pub.catchup_accruals
8821                                           (p_api_version        => p_api_version,
8822                                            p_init_msg_list      => g_false,
8823                                            x_return_status      => l_return_status,
8824                                            x_msg_count          => x_msg_count,
8825                                            x_msg_data           => x_msg_data,
8826                                            p_catchup_rec        => l_catchup_rec,
8827                                            x_tcnv_tbl           => lx_tcnv_tbl,
8828                                            x_tclv_tbl           => lx_tclv_tbl
8829                                           );
8830 
8831       IF (is_debug_statement_on)
8832       THEN
8833          okl_debug_pub.log_debug
8834             (g_level_statement,
8835              l_module_name,
8836                 'called OKL_GENERATE_ACCRUALS_PUB.catchup_accruals , return status: '
8837              || l_return_status
8838             );
8839       END IF;
8840 
8841       IF l_return_status <> g_ret_sts_success
8842       THEN
8843          -- Unable to do reversal of non-income during termination
8844          -- of contract CONTRACT_NUMBER.
8845          okl_api.set_message (p_app_name          => g_app_name,
8846                               p_msg_name          => 'OKL_AM_REV_NONINC_ERR',
8847                               p_token1            => 'CONTRACT_NUMBER',
8848                               p_token1_value      => p_term_rec.p_contract_number
8849                              );
8850       END IF;
8851 
8852       -- Raise exception to rollback this whole block
8853       IF (l_return_status = g_ret_sts_unexp_error)
8854       THEN
8855          RAISE okl_api.g_exception_unexpected_error;
8856       ELSIF (l_return_status = g_ret_sts_error)
8857       THEN
8858          RAISE okl_api.g_exception_error;
8859       END IF;
8860 
8861    -- Bug 7263041 start
8862 
8863    IF (l_multi_gaap_yn = 'Y') THEN
8864 
8865       l_sob_id_rep := Okl_Accounting_Util.GET_SET_OF_BOOKS_ID(
8866                                      p_representation_type => 'SECONDARY');
8867 
8868       l_valid_gl_date_rep :=
8869                okl_accounting_util.get_valid_gl_date (
8870                                  p_gl_date      => p_sys_date,
8871                                  p_ledger_id    => l_sob_id_rep
8872                );
8873       l_catchup_rec.product_id := l_reporting_pdt_id;
8874       l_catchup_rec.accrual_date := l_valid_gl_date_rep;
8875 
8876       IF (is_debug_statement_on)
8877       THEN
8878          okl_debug_pub.log_debug
8879                         (g_level_statement,
8880                          l_module_name,
8881                          'calling OKL_GENERATE_ACCRUALS_PUB.catchup_accruals for SECONDARY'
8882                         );
8883       END IF;
8884 
8885       -- Do reversal of non-income
8886       okl_generate_accruals_pub.catchup_accruals
8887                                           (p_api_version        => p_api_version,
8888                                            p_init_msg_list      => g_false,
8889                                            x_return_status      => l_return_status,
8890                                            x_msg_count          => x_msg_count,
8891                                            x_msg_data           => x_msg_data,
8892                                            p_catchup_rec        => l_catchup_rec,
8893                                            x_tcnv_tbl           => lx_tcnv_tbl,
8894                                            x_tclv_tbl           => lx_tclv_tbl,
8895                                            p_representation_type => 'SECONDARY'
8896                                           );
8897 
8898       IF (is_debug_statement_on)
8899       THEN
8900          okl_debug_pub.log_debug
8901             (g_level_statement,
8902              l_module_name,
8903                 'called OKL_GENERATE_ACCRUALS_PUB.catchup_accruals for SECONDARY, return status: '
8904              || l_return_status
8905             );
8906       END IF;
8907 
8908       IF l_return_status <> g_ret_sts_success
8909       THEN
8910          -- Unable to do reversal of non-income during termination
8911          -- of contract CONTRACT_NUMBER.
8912          okl_api.set_message (p_app_name          => g_app_name,
8913                               p_msg_name          => 'OKL_AM_REV_NONINC_ERR',
8914                               p_token1            => 'CONTRACT_NUMBER',
8915                               p_token1_value      => p_term_rec.p_contract_number
8916                              );
8917       END IF;
8918 
8919       -- Raise exception to rollback this whole block
8920       IF (l_return_status = g_ret_sts_unexp_error)
8921       THEN
8922          RAISE okl_api.g_exception_unexpected_error;
8923       ELSIF (l_return_status = g_ret_sts_error)
8924       THEN
8925          RAISE okl_api.g_exception_error;
8926       END IF;
8927 
8928 
8929    END IF;
8930    -- Bug 7263041 end
8931 
8932 -- *************
8933 -- REVERSAL OF LOSS-PROVISIONS
8934 -- *************
8935 
8936       -- Set the l_lprv_rec rec type
8937       l_lprv_rec.cntrct_num := p_term_rec.p_contract_number;
8938       l_lprv_rec.reversal_type := NULL;
8939       -- RMUNJULU 3097068 Added code to set the reversal date with valid GL date
8940       l_lprv_rec.reversal_date := l_valid_gl_date;
8941 
8942       IF (is_debug_statement_on)
8943       THEN
8944          okl_debug_pub.log_debug
8945                      (g_level_statement,
8946                       l_module_name,
8947                       'calling OKL_REV_LOSS_PROV_PUB.reverse_loss_provisions'
8948                      );
8949       END IF;
8950 
8951       -- Do reversal of loss provisions
8952       okl_rev_loss_prov_pub.reverse_loss_provisions
8953                                           (p_api_version        => p_api_version,
8954                                            p_init_msg_list      => g_false,
8955                                            x_return_status      => l_return_status,
8956                                            x_msg_count          => x_msg_count,
8957                                            x_msg_data           => x_msg_data,
8958                                            p_lprv_rec           => l_lprv_rec
8959                                           );
8960 
8961       IF (is_debug_statement_on)
8962       THEN
8963          okl_debug_pub.log_debug
8964             (g_level_statement,
8965              l_module_name,
8966                 'called OKL_REV_LOSS_PROV_PUB.reverse_loss_provisions , return status: '
8967              || l_return_status
8968             );
8969       END IF;
8970 
8971       IF l_return_status <> g_ret_sts_success
8972       THEN
8973          -- Unable to do reversal of loss provisions during
8974          -- termination of contract CONTRACT_NUMBER.
8975          okl_api.set_message (p_app_name          => g_app_name,
8976                               p_msg_name          => 'OKL_AM_REV_LOSPROV_ERR',
8977                               p_token1            => 'CONTRACT_NUMBER',
8978                               p_token1_value      => p_term_rec.p_contract_number
8979                              );
8980       END IF;
8981 
8982       -- Raise exception to rollback this whole block
8983       IF (l_return_status = g_ret_sts_unexp_error)
8984       THEN
8985          RAISE okl_api.g_exception_unexpected_error;
8986       ELSIF (l_return_status = g_ret_sts_error)
8987       THEN
8988          RAISE okl_api.g_exception_error;
8989       END IF;
8990 
8991       -- Set the success messages to message table
8992       l_count := px_msg_tbl.COUNT;
8993       -- Reversal of non-income during termination
8994       -- of contract CONTRACT_NUMBER done successfully.
8995       px_msg_tbl (l_count).msg_desc := 'OKL_AM_REV_NONINC_SUC';
8996       px_msg_tbl (l_count).msg_token1 := 'CONTRACT_NUMBER';
8997       px_msg_tbl (l_count).msg_token1_value := p_term_rec.p_contract_number;
8998       l_count := l_count + 1;
8999       -- Reversal of loss provisions during
9000       -- termination of contract CONTRACT_NUMBER done successfully.
9001       px_msg_tbl (l_count).msg_desc := 'OKL_AM_REV_LOSPROV_SUC';
9002       px_msg_tbl (l_count).msg_token1 := 'CONTRACT_NUMBER';
9003       px_msg_tbl (l_count).msg_token1_value := p_term_rec.p_contract_number;
9004       l_count := l_count + 1;
9005       -- Set the return status
9006       x_return_status := l_return_status;
9007 
9008       IF (is_debug_procedure_on)
9009       THEN
9010          okl_debug_pub.log_debug (g_level_procedure,
9011                                   l_module_name,
9012                                   'End(-)'
9013                                  );
9014       END IF;
9015    EXCEPTION
9016       WHEN okl_api.g_exception_error
9017       THEN
9018          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
9019          IF prod_id_csr%ISOPEN
9020          THEN
9021             CLOSE prod_id_csr;
9022          END IF;
9023 
9024          ROLLBACK TO reverse_loss_provisions;
9025          x_return_status := g_ret_sts_error;
9026 
9027          IF (is_debug_exception_on)
9028          THEN
9029             okl_debug_pub.log_debug (g_level_exception,
9030                                      l_module_name,
9031                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
9032                                     );
9033          END IF;
9034       WHEN okl_api.g_exception_unexpected_error
9035       THEN
9036          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
9037          IF prod_id_csr%ISOPEN
9038          THEN
9039             CLOSE prod_id_csr;
9040          END IF;
9041 
9042          ROLLBACK TO reverse_loss_provisions;
9043          x_return_status := g_ret_sts_unexp_error;
9044 
9045          IF (is_debug_exception_on)
9046          THEN
9047             okl_debug_pub.log_debug (g_level_exception,
9048                                      l_module_name,
9049                                         'EXCEPTION :'
9050                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
9051                                     );
9052          END IF;
9053       WHEN OTHERS
9054       THEN
9055          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
9056          IF prod_id_csr%ISOPEN
9057          THEN
9058             CLOSE prod_id_csr;
9059          END IF;
9060 
9061          ROLLBACK TO reverse_loss_provisions;
9062          x_return_status := g_ret_sts_unexp_error;
9063          -- Set the oracle error message
9064          okl_api.set_message (p_app_name          => g_app_name_1,
9065                               p_msg_name          => g_unexpected_error,
9066                               p_token1            => g_sqlcode_token,
9067                               p_token1_value      => SQLCODE,
9068                               p_token2            => g_sqlerrm_token,
9069                               p_token2_value      => SQLERRM
9070                              );
9071 
9072          IF (is_debug_exception_on)
9073          THEN
9074             okl_debug_pub.log_debug (g_level_exception,
9075                                      l_module_name,
9076                                         'EXCEPTION :'
9077                                      || 'OTHERS, SQLCODE: '
9078                                      || SQLCODE
9079                                      || ' , SQLERRM : '
9080                                      || SQLERRM
9081                                     );
9082          END IF;
9083    END reverse_loss_provisions;
9084 
9085    -- Start of comments
9086    --
9087    -- Procedure Name : cancel_insurance
9088    -- Desciption     : Will cancel the insurances for the contract
9089    -- Business Rules :
9090    -- Parameters     :
9091    -- Version      : 1.0
9092    -- History        : RMUNJULU -- 20-DEC-02 2484327 Created
9093    --                : RMUNJULU 14-FEB-03 2804703 Added code to check if contract
9094    --                  is EVERGREEN
9095    --                : RMUNJULU 03-MAR-03 2830997 Fixed the exception block
9096    --
9097    -- End of comments
9098    PROCEDURE cancel_insurance (
9099       p_api_version     IN              NUMBER,
9100       p_init_msg_list   IN              VARCHAR2,
9101       x_msg_count       OUT NOCOPY      NUMBER,
9102       x_msg_data        OUT NOCOPY      VARCHAR2,
9103       x_return_status   OUT NOCOPY      VARCHAR2,
9104       p_term_rec        IN              term_rec_type,
9105       p_sys_date        IN              DATE,
9106       p_klev_tbl        IN              klev_tbl_type
9107    )
9108    IS
9109       -- Cursor to get the end date of contract
9110       -- RMUNJULU 14-FEB-03 2804703 Added code to get sts_code
9111       CURSOR k_end_date_csr (
9112          p_chr_id   IN   NUMBER
9113       )
9114       IS
9115          SELECT khr.end_date end_date,
9116                 khr.sts_code sts_code
9117            FROM okc_k_headers_v khr
9118           WHERE khr.ID = p_chr_id;
9119 
9120       l_k_end_date             DATE;
9121       l_return_status          VARCHAR2 (1)   := g_ret_sts_success;
9122       l_early_termination_yn   VARCHAR2 (1)   := g_no;
9123       i                        NUMBER;
9124       l_api_name               VARCHAR2 (30)  := 'cancel_insurance';
9125       l_api_version   CONSTANT NUMBER         := 1;
9126       l_module_name            VARCHAR2 (500)
9127                                         := g_module_name || 'cancel_insurance';
9128       is_debug_exception_on    BOOLEAN
9129               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
9130       is_debug_procedure_on    BOOLEAN
9131               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
9132       is_debug_statement_on    BOOLEAN
9133               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
9134       -- RMUNJULU 14-FEB-03 2804703 Added variable
9135       l_sts_code               VARCHAR2 (200);
9136    BEGIN
9137       -- Start a savepoint to rollback to if error in this block
9138       SAVEPOINT cancel_insurance;
9139 
9140       IF (is_debug_procedure_on)
9141       THEN
9142          okl_debug_pub.log_debug (g_level_procedure,
9143                                   l_module_name,
9144                                   'Begin(+)'
9145                                  );
9146       END IF;
9147 
9148       IF (is_debug_statement_on)
9149       THEN
9150          okl_debug_pub.log_debug (g_level_statement,
9151                                   l_module_name,
9152                                   'In param, p_sys_date: ' || p_sys_date
9153                                  );
9154          okl_debug_pub.log_debug (g_level_statement,
9155                                   l_module_name,
9156                                      'In param, p_term_rec.p_contract_id: '
9157                                   || p_term_rec.p_contract_id
9158                                  );
9159          okl_debug_pub.log_debug
9160                            (g_level_statement,
9161                             l_module_name,
9162                                'In param, p_term_rec.p_early_termination_yn: '
9163                             || p_term_rec.p_early_termination_yn
9164                            );
9165       END IF;
9166 
9167       -- Get k end date
9168       -- RMUNJULU 14-FEB-03 2804703 Added sts_code
9169       OPEN k_end_date_csr (p_term_rec.p_contract_id);
9170 
9171       FETCH k_end_date_csr
9172        INTO l_k_end_date,
9173             l_sts_code;
9174 
9175       CLOSE k_end_date_csr;
9176 
9177       -- check if early termination
9178       IF TRUNC (l_k_end_date) > TRUNC (p_sys_date)
9179       THEN
9180          l_early_termination_yn := g_yes;
9181       END IF;
9182 
9183       -- if early termination then
9184       -- RMUNJULU 14-FEB-03 2804703 Added condition to check if contract not
9185       -- already evergreen
9186       IF     (   NVL (p_term_rec.p_early_termination_yn, '?') = g_yes
9187               OR l_early_termination_yn = g_yes
9188              )
9189          AND l_sts_code <> 'EVERGREEN'
9190       THEN
9191          IF (is_debug_statement_on)
9192          THEN
9193             okl_debug_pub.log_debug
9194                         (g_level_statement,
9195                          l_module_name,
9196                          'calling OKL_INSURANCE_POLICIES_PUB.cancel_policies'
9197                         );
9198          END IF;
9199 
9200          -- cancel insurances
9201          okl_insurance_policies_pub.cancel_policies
9202                                    (p_api_version            => p_api_version,
9203                                     p_init_msg_list          => g_false,
9204                                     x_return_status          => l_return_status,
9205                                     x_msg_count              => x_msg_count,
9206                                     x_msg_data               => x_msg_data,
9207                                     p_contract_id            => p_term_rec.p_contract_id,
9208                                     p_cancellation_date      => p_sys_date
9209                                    );
9210 
9211          IF (is_debug_statement_on)
9212          THEN
9213             okl_debug_pub.log_debug
9214                (g_level_statement,
9215                 l_module_name,
9216                    'called OKL_INSURANCE_POLICIES_PUB.cancel_policies , return status: '
9217                 || l_return_status
9218                );
9219          END IF;
9220 
9221          IF l_return_status <> g_ret_sts_success
9222          THEN
9223             -- Error in cancelling Insurance.
9224             okl_api.set_message (p_app_name      => g_app_name,
9225                                  p_msg_name      => 'OKL_AM_ERR_CAN_INS');
9226          END IF;
9227 
9228          -- Raise exception to rollback to the savepoint
9229          IF (l_return_status = g_ret_sts_unexp_error)
9230          THEN
9231             RAISE okl_api.g_exception_unexpected_error;
9232          ELSIF (l_return_status = g_ret_sts_error)
9233          THEN
9234             RAISE okl_api.g_exception_error;
9235          END IF;
9236       END IF;
9237 
9238       -- Set the return status
9239       x_return_status := l_return_status;
9240 
9241       IF (is_debug_procedure_on)
9242       THEN
9243          okl_debug_pub.log_debug (g_level_procedure,
9244                                   l_module_name,
9245                                   'End(-)'
9246                                  );
9247       END IF;
9248    EXCEPTION
9249       WHEN okl_api.g_exception_error
9250       THEN
9251          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
9252          IF k_end_date_csr%ISOPEN
9253          THEN
9254             CLOSE k_end_date_csr;
9255          END IF;
9256 
9257          ROLLBACK TO cancel_insurance;
9258          x_return_status := g_ret_sts_error;
9259 
9260          IF (is_debug_exception_on)
9261          THEN
9262             okl_debug_pub.log_debug (g_level_exception,
9263                                      l_module_name,
9264                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
9265                                     );
9266          END IF;
9267       WHEN okl_api.g_exception_unexpected_error
9268       THEN
9269          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
9270          IF k_end_date_csr%ISOPEN
9271          THEN
9272             CLOSE k_end_date_csr;
9273          END IF;
9274 
9275          ROLLBACK TO cancel_insurance;
9276          x_return_status := g_ret_sts_unexp_error;
9277 
9278          IF (is_debug_exception_on)
9279          THEN
9280             okl_debug_pub.log_debug (g_level_exception,
9281                                      l_module_name,
9282                                         'EXCEPTION :'
9283                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
9284                                     );
9285          END IF;
9286       WHEN OTHERS
9287       THEN
9288          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
9289          IF k_end_date_csr%ISOPEN
9290          THEN
9291             CLOSE k_end_date_csr;
9292          END IF;
9293 
9294          ROLLBACK TO cancel_insurance;
9295          x_return_status := g_ret_sts_unexp_error;
9296          -- Set the oracle error message
9297          okl_api.set_message (p_app_name          => g_app_name_1,
9298                               p_msg_name          => g_unexpected_error,
9299                               p_token1            => g_sqlcode_token,
9300                               p_token1_value      => SQLCODE,
9301                               p_token2            => g_sqlerrm_token,
9302                               p_token2_value      => SQLERRM
9303                              );
9304 
9305          IF (is_debug_exception_on)
9306          THEN
9307             okl_debug_pub.log_debug (g_level_exception,
9308                                      l_module_name,
9309                                         'EXCEPTION :'
9310                                      || 'OTHERS, SQLCODE: '
9311                                      || SQLCODE
9312                                      || ' , SQLERRM : '
9313                                      || SQLERRM
9314                                     );
9315          END IF;
9316    END cancel_insurance;
9317 
9318    -- Start of Commnets
9319    --
9320    -- Procedure Name       : validate_chr_cle_id
9321    -- Description          : This Local Procedure is used for validation of
9322    --                        Chr_id and Asset line.
9323    -- Business Rules       :
9324    -- Parameters           :
9325    -- Version              : 1.0
9326    -- History              : RMUNJULU -- 04-DEC-02 Bug # 2484327 Added comments to
9327    --                        cursor
9328    --                        RMUNJULU 16-DEC-02 Bug # 2484327, changed cursor to get lines
9329    --                        which are of the same status as header
9330    --
9331    -- End of Commnets
9332    PROCEDURE validate_chr_cle_id (
9333       p_dnz_chr_id      IN              okc_k_lines_v.dnz_chr_id%TYPE,
9334       p_top_line_id     IN              okc_k_lines_v.ID%TYPE,
9335       x_return_status   OUT NOCOPY      VARCHAR2
9336    )
9337    IS
9338       ln_dummy                NUMBER         := 0;
9339       l_module_name           VARCHAR2 (500)
9340                                     := g_module_name || 'validate_chr_cle_id';
9341       is_debug_exception_on   BOOLEAN
9342              := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
9343       is_debug_procedure_on   BOOLEAN
9344              := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
9345       is_debug_statement_on   BOOLEAN
9346              := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
9347 
9348       -- RMUNJULU Bug #  2484327 Added comments, changed parameters to IN
9349       -- RMUNJULU 16-DEC-02 Bug # 2484327, get lines which are of the same status as header
9350       -- Cursor to validate the contract and lines to see if they are booked or not
9351       CURSOR l_k_lines_validate_csr (
9352          p_dnz_chr_id    IN   okc_k_lines_v.dnz_chr_id%TYPE,
9353          p_top_line_id   IN   okc_k_lines_v.ID%TYPE
9354       )
9355       IS
9356          SELECT 1
9357            FROM DUAL
9358           WHERE EXISTS (
9359                    SELECT 1
9360                      FROM okc_subclass_top_line stl,
9361                           okc_line_styles_v lse,
9362                           okc_k_lines_v cle,
9363                           okc_k_headers_v khr
9364                     WHERE cle.ID = p_top_line_id
9365                       AND cle.dnz_chr_id = p_dnz_chr_id
9366                       AND cle.cle_id IS NULL
9367                       AND cle.chr_id = cle.dnz_chr_id
9368                       AND cle.lse_id = lse.ID
9369                       AND lse.lty_code = g_fin_line_lty_code
9370                       AND lse.lse_type = g_tls_type
9371                       AND lse.lse_parent_id IS NULL
9372                       AND lse.ID = stl.lse_id
9373                       AND cle.sts_code = khr.sts_code
9374                       AND cle.dnz_chr_id = khr.ID
9375                       AND stl.scs_code IN (g_lease_scs_code, g_loan_scs_code));
9376    BEGIN
9377       IF (is_debug_procedure_on)
9378       THEN
9379          okl_debug_pub.log_debug (g_level_procedure,
9380                                   l_module_name,
9381                                   'Begin(+)'
9382                                  );
9383       END IF;
9384 
9385       IF (is_debug_statement_on)
9386       THEN
9387          okl_debug_pub.log_debug (g_level_statement,
9388                                   l_module_name,
9389                                   'In param, p_dnz_chr_id: ' || p_dnz_chr_id
9390                                  );
9391          okl_debug_pub.log_debug (g_level_statement,
9392                                   l_module_name,
9393                                   'In param, p_top_line_id: ' || p_top_line_id
9394                                  );
9395       END IF;
9396 
9397       -- initialize return status
9398       x_return_status := g_ret_sts_success;
9399 
9400       -- data is required
9401       IF     (p_dnz_chr_id = g_miss_num OR p_dnz_chr_id IS NULL)
9402          AND (p_top_line_id = g_miss_num OR p_top_line_id IS NULL)
9403       THEN
9404          -- store SQL error message on message stack
9405          okl_api.set_message
9406                           (p_app_name          => g_app_name,
9407                            p_msg_name          => g_required_value,
9408                            p_token1            => g_col_name_token,
9409                            p_token1_value      => 'p_dnz_chr_id and p_top_line_id'
9410                           );
9411          -- halt validation as it is a required field
9412          RAISE g_exception_stop_validation;
9413       ELSIF    (p_dnz_chr_id = g_miss_num OR p_dnz_chr_id IS NULL)
9414             OR (p_top_line_id = g_miss_num OR p_top_line_id IS NULL)
9415       THEN
9416          -- store SQL error message on message stack
9417          okl_api.set_message
9418                           (p_app_name          => g_app_name,
9419                            p_msg_name          => g_required_value,
9420                            p_token1            => g_col_name_token,
9421                            p_token1_value      => 'p_dnz_chr_id and p_top_line_id'
9422                           );
9423          -- halt validation as it is a required field
9424          RAISE g_exception_stop_validation;
9425       END IF;
9426 
9427       -- Combination of dnz_chr_id and Top line id should be valid one
9428       OPEN l_k_lines_validate_csr (p_dnz_chr_id       => p_dnz_chr_id,
9429                                    p_top_line_id      => p_top_line_id);
9430 
9431       IF l_k_lines_validate_csr%NOTFOUND
9432       THEN
9433          okl_api.set_message
9434                           (p_app_name          => g_app_name,
9435                            p_msg_name          => g_no_matching_record,
9436                            p_token1            => g_col_name_token,
9437                            p_token1_value      => 'p_dnz_chr_id and p_top_line_id'
9438                           );
9439          -- halt validation as it has no parent record
9440          RAISE g_exception_halt_validation;
9441       END IF;
9442 
9443       FETCH l_k_lines_validate_csr
9444        INTO ln_dummy;
9445 
9446       CLOSE l_k_lines_validate_csr;
9447 
9448       IF (ln_dummy = 0)
9449       THEN
9450          okl_api.set_message
9451                           (p_app_name          => g_app_name,
9452                            p_msg_name          => g_no_matching_record,
9453                            p_token1            => g_col_name_token,
9454                            p_token1_value      => 'p_dnz_chr_id and p_top_line_id'
9455                           );
9456          -- halt validation as it has no parent record
9457          RAISE g_exception_halt_validation;
9458       END IF;
9459 
9460       IF (is_debug_procedure_on)
9461       THEN
9462          okl_debug_pub.log_debug (g_level_procedure,
9463                                   l_module_name,
9464                                   'End(-)'
9465                                  );
9466       END IF;
9467    EXCEPTION
9468       WHEN g_exception_stop_validation
9469       THEN
9470          -- We are here since the field is required
9471          -- Notify Error
9472          -- RMUNJULU -- Bug # 2484327 -- Added code to close cursor if open
9473          IF l_k_lines_validate_csr%ISOPEN
9474          THEN
9475             CLOSE l_k_lines_validate_csr;
9476          END IF;
9477 
9478          x_return_status := g_ret_sts_error;
9479 
9480          IF (is_debug_exception_on)
9481          THEN
9482             okl_debug_pub.log_debug (g_level_exception,
9483                                      l_module_name,
9484                                         'EXCEPTION :'
9485                                      || 'G_EXCEPTION_STOP_VALIDATION'
9486                                     );
9487          END IF;
9488       WHEN g_exception_halt_validation
9489       THEN
9490          -- If the cursor is open then it has to be closed
9491          IF l_k_lines_validate_csr%ISOPEN
9492          THEN
9493             CLOSE l_k_lines_validate_csr;
9494          END IF;
9495 
9496          x_return_status := g_ret_sts_error;
9497 
9498          IF (is_debug_exception_on)
9499          THEN
9500             okl_debug_pub.log_debug (g_level_exception,
9501                                      l_module_name,
9502                                         'EXCEPTION :'
9503                                      || 'G_EXCEPTION_HALT_VALIDATION'
9504                                     );
9505          END IF;
9506       WHEN OTHERS
9507       THEN
9508          -- store SQL error message on message stack
9509          okl_api.set_message (p_app_name          => g_app_name,
9510                               p_msg_name          => g_unexpected_error,
9511                               p_token1            => g_sqlcode_token,
9512                               p_token1_value      => SQLCODE,
9513                               p_token2            => g_sqlerrm_token,
9514                               p_token2_value      => SQLERRM
9515                              );
9516 
9517          -- If the cursor is open then it has to be closed
9518          IF l_k_lines_validate_csr%ISOPEN
9519          THEN
9520             CLOSE l_k_lines_validate_csr;
9521          END IF;
9522 
9523          -- notify caller of an error as UNEXPETED error
9524          x_return_status := g_ret_sts_unexp_error;
9525 
9526          IF (is_debug_exception_on)
9527          THEN
9528             okl_debug_pub.log_debug (g_level_exception,
9529                                      l_module_name,
9530                                         'EXCEPTION :'
9531                                      || 'OTHERS, SQLCODE: '
9532                                      || SQLCODE
9533                                      || ' , SQLERRM : '
9534                                      || SQLERRM
9535                                     );
9536          END IF;
9537    END validate_chr_cle_id;
9538 
9539    /*========================================================================
9540    | PRIVATE PROCEDURE update_payments
9541    |
9542    | DESCRIPTION
9543    |    This procedure updates the financial asset, service line and service
9544    |    subline level payments with the proposed payments calculated during
9545    |    quote creation
9546    |
9547    | CALLED FROM PROCEDURES/FUNCTIONS
9548    |     update_lines
9549    |
9550    | CALLS PROCEDURES/FUNCTIONS
9551    |
9552    |
9553    | PARAMETERS
9554    |      p_quote_id                 IN        Quote ID
9555    |
9556    | KNOWN ISSUES
9557    |
9558    | NOTES
9559    |
9560    |
9561    | MODIFICATION HISTORY
9562    | Date                  Author            Description of Changes
9563    | 28-OCT-2003           SECHAWLA          22846988  Created
9564    | 20-SEP-04             SECHAWLA          3816891 : Modified payment processing
9565    |                                           for Arrears
9566    | 29-SEP-04             PAGARG            Bug #3921591: Added payment
9567    |                                         processing for Rollover Fee line
9568    | 15-JUN-06             SMADHAVA          Bug#5043646: Modified payment processing
9569    |                                         for fee header level payments
9570    *=======================================================================*/
9571    PROCEDURE update_payments (
9572       p_api_version     IN              NUMBER,
9573       p_init_msg_list   IN              VARCHAR2 DEFAULT okc_api.g_false,
9574       x_msg_count       OUT NOCOPY      NUMBER,
9575       x_msg_data        OUT NOCOPY      VARCHAR2,
9576       x_return_status   OUT NOCOPY      VARCHAR2,
9577       p_quote_id        IN              NUMBER
9578    )
9579    IS
9580       -- This cursor returns all the proposed objects of a particular type, created during quote creation
9581       -- some of the proposed objects may not have any associated cash flows
9582       CURSOR l_quoteobjects_csr (
9583          cp_qte_id   IN   NUMBER
9584       )
9585       IS
9586          SELECT DISTINCT qco.cfo_id cfo_id,
9587                          qco.base_source_id line_id,
9588                          caf.sts_code,
9589                          cfo.oty_code
9590                     FROM okl_trx_qte_cf_objects qco,
9591                          okl_cash_flows caf,
9592                          okl_cash_flow_objects cfo
9593                    WHERE cfo.ID = qco.cfo_id
9594                      AND cfo.ID =
9595                             caf.cfo_id(+)
9596 -- cash flow object is created even if there are no proposed payments (quote eff dt < first level start date)
9597                      --Bug #3921591: pagarg +++ Rollover +++
9598                      -- Included fee line and fee asset line type cash flow objects also
9599                      AND cfo.oty_code IN
9600                             ('FINANCIAL_ASSET_LINE',
9601                              'SERVICE_LINE',
9602                              'SERVICED_ASSET_LINE',
9603                              'FEE_LINE',
9604                              'FEE_ASSET_LINE'
9605                             )
9606                      AND qco.qte_id = cp_qte_id
9607                      AND NVL (caf.sts_code, 'PROPOSED') = 'PROPOSED';
9608 
9609       -- get the quote effective date
9610       CURSOR l_trxquotes_csr (
9611          cp_qte_id   IN   NUMBER
9612       )
9613       IS
9614          SELECT TRUNC (date_effective_from)
9615            FROM okl_trx_quotes_b
9616           WHERE ID = cp_qte_id;
9617 
9618       -- get the quote line info
9619       CURSOR l_quotelines_csr (
9620          cp_qte_id   IN   NUMBER,
9621          cp_kle_id   IN   NUMBER
9622       )
9623       IS
9624          SELECT ID tql_id,
9625                 asset_quantity asset_quantity,
9626                 quote_quantity quote_quantity
9627            FROM okl_txl_quote_lines_b
9628           WHERE qte_id = cp_qte_id
9629             AND qlt_code = 'AMCFIA'
9630             AND kle_id = cp_kle_id;
9631 
9632       -- get all the stream types from cashflows, for which proposed payment exists
9633       CURSOR l_cashflowstreams_csr (
9634          cp_cfo_id   IN   NUMBER
9635       )
9636       IS
9637          SELECT ID caf_id,
9638                 sty_id,
9639                 dnz_khr_id,
9640                 number_of_advance_periods,
9641                 due_arrears_yn
9642            FROM okl_cash_flows
9643           WHERE cfo_id = cp_cfo_id
9644             AND sts_code = 'PROPOSED'
9645             AND cft_code = 'PAYMENT_SCHEDULE';
9646 
9647       -- get all the cash flow levels for a cashflow header
9648       CURSOR l_cashflowlevels_csr (
9649          cp_caf_id   IN   NUMBER
9650       )
9651       IS
9652          SELECT ID cfl_id,
9653                 amount,
9654                 number_of_periods,
9655                 fqy_code,
9656                 stub_days,
9657                 stub_amount,
9658                 start_date
9659            FROM okl_cash_flow_levels
9660           WHERE caf_id = cp_caf_id;
9661 
9662       -- This cursor returns the rule group ID and slh ID of all the payments for a given line
9663       CURSOR l_rgpslh_csr (
9664          cp_cle_id   IN   NUMBER
9665       )
9666       IS
9667          SELECT DISTINCT rgp.ID rgp_id,
9668                          slh_rul.ID slh_id,
9669                          rgp.dnz_chr_id,
9670                          sttyp.id1 sty_id,
9671                          sll_rul.rule_information5 advance_periods,
9672                          sll_rul.rule_information10 due_arrears_yn,
9673                          sll_rul.object1_id1 frequency
9674                     FROM okc_rules_b sll_rul,
9675                          okc_rules_b slh_rul,
9676                          okc_rule_groups_b rgp,
9677                          okl_strmtyp_source_v sttyp
9678                    WHERE sll_rul.object2_id1 = TO_CHAR (slh_rul.ID)
9679                      AND sll_rul.rgp_id = rgp.ID
9680                      AND sll_rul.rule_information_category = 'LASLL'
9681                      AND sttyp.id1 = slh_rul.object1_id1
9682                      AND slh_rul.rgp_id = rgp.ID
9683                      AND slh_rul.rule_information_category = 'LASLH'
9684                      AND rgp.rgd_code = 'LALEVL'
9685                      AND rgp.cle_id = cp_cle_id;
9686 
9687       -- Get the proposed cashflows for an object, for a given stream type
9688       CURSOR l_cashflows_csr (
9689          cp_cfo_id   IN   NUMBER,
9690          cp_sty_id   IN   NUMBER
9691       )
9692       IS
9693          SELECT   caf.ID,
9694                   caf.sty_id,
9695                   cfl.start_date,
9696                   cfl.amount,
9697                   cfl.number_of_periods,
9698                   cfl.stub_days,
9699                   cfl.stub_amount,
9700                   NVL (caf.due_arrears_yn, 'N') due_arrears_yn,
9701                                          -- SECHAWLA 20-SEP-04 3816891 : Added
9702                   cfl.fqy_code           -- SECHAWLA 20-SEP-04 3816891 : Added
9703              FROM okl_cash_flows caf,
9704                   okl_cash_flow_levels cfl
9705             WHERE cfo_id = cp_cfo_id
9706               AND caf.sty_id = cp_sty_id
9707               AND caf.ID = cfl.caf_id
9708               AND caf.sts_code = 'PROPOSED'
9709               AND caf.cft_code = 'PAYMENT_SCHEDULE'
9710          ORDER BY cfl.start_date;
9711 
9712       -- get the current payments for a line, as they exist after split asset has happened
9713       -- Get the Line Level payments
9714       CURSOR l_lpayments_csr (
9715          cp_cle_id   IN   NUMBER,
9716          cp_sty_id   IN   NUMBER
9717       )
9718       IS
9719          SELECT   rgp.cle_id cle_id,
9720                   sttyp.id1 sty_id,
9721                   sttyp.code stream_type,
9722                   sll_rul.rule_information2 start_date,
9723                   sll_rul.rule_information3 periods,
9724                   sll_rul.rule_information6 amount,
9725                   sll_rul.rule_information7 stub_days,
9726                   sll_rul.rule_information8 stub_amount,
9727                   rgp.dnz_chr_id
9728              FROM okc_rules_b sll_rul,
9729                   okl_strmtyp_source_v sttyp,
9730                   okc_rules_b slh_rul,
9731                   okc_rule_groups_b rgp
9732             WHERE sll_rul.object2_id1 = TO_CHAR (slh_rul.ID)
9733               AND sll_rul.rgp_id = rgp.ID
9734               AND sll_rul.rule_information_category = 'LASLL'
9735               AND sttyp.id1 = slh_rul.object1_id1
9736               AND sttyp.id1 = cp_sty_id
9737               AND slh_rul.rgp_id = rgp.ID
9738               AND slh_rul.rule_information_category = 'LASLH'
9739               AND rgp.rgd_code = 'LALEVL'
9740               AND rgp.cle_id = cp_cle_id
9741          ORDER BY start_date;
9742 
9743       --Bug #3921591: pagarg +++ Rollover +++
9744       -- Modified the cursor to get financial assets for a given line type
9745 
9746       -- Get the financial asset associated with a given line type (subline)
9747       CURSOR l_finasset_csr (
9748          cp_fee_serviced_asset_line_id   IN   NUMBER,
9749          cp_line_type                    IN   VARCHAR2
9750       )
9751       IS
9752          SELECT cim.object1_id1,
9753                 cle.cle_id
9754            FROM okc_k_lines_b cle,
9755                 okc_line_styles_b lse,
9756                 okc_k_items cim
9757           WHERE cle.lse_id = lse.ID
9758             AND lse.lty_code = cp_line_type
9759             AND cim.cle_id = cle.ID
9760             AND cle.ID = cp_fee_serviced_asset_line_id;
9761 
9762       -- smadhava - Bug#5043646 - Added - Start
9763       l_active_assets_flag      VARCHAR2 (1);
9764 
9765       -- Find if there are ACTIVE assets associated to fees/services (subline)
9766       CURSOR c_find_active_link_assets (
9767          cp_line_id     IN   NUMBER,
9768          cp_line_type   IN   VARCHAR2
9769       )
9770       IS
9771          SELECT 'X'
9772            FROM okc_k_lines_b linked_asset,
9773                 okc_line_styles_b line_styl,
9774                 okc_k_items item,
9775                 okc_statuses_b sts
9776           WHERE linked_asset.lse_id = line_styl.ID
9777             AND line_styl.lty_code = cp_line_type
9778             AND item.cle_id = linked_asset.ID
9779             AND linked_asset.cle_id = cp_line_id
9780             AND sts.code = linked_asset.sts_code
9781             AND sts.ste_code = 'ACTIVE';
9782 
9783       -- smadhava - Bug#5043646 - Added - End
9784 
9785       --Bug #3921591: pagarg +++ Rollover +++
9786       -- Modified the cursor to get the assets for a given line type
9787 
9788       -- Get all the assets associated with the service or fee line
9789       -- This cursor might also return assets with status = 'TERMINATED'
9790       -- By the time this procedure is called, financial assets that are included in the quote, will
9791       -- already be terminated (in update_lines procedure)
9792       CURSOR l_lineassets_csr (
9793          cp_line_id     IN   NUMBER,
9794          cp_line_type   IN   VARCHAR2
9795       )
9796       IS
9797          SELECT cim.object1_id1,
9798                 cle.ID
9799            FROM okc_k_lines_b cle,
9800                 okc_line_styles_b lse,
9801                 okc_k_items cim
9802           WHERE cle.lse_id = lse.ID
9803             AND lse.lty_code = cp_line_type
9804             AND cim.cle_id = cle.ID
9805             AND cle.cle_id = cp_line_id;
9806 
9807       -- SECHAWLA 20-SEP-04 3816891 : new declarations begin
9808 
9809       -- Get the next level start date
9810       CURSOR l_nextlevelstartdt_csr (
9811          cp_currlevelstartdt   IN   DATE,
9812          cp_number_of_months   IN   NUMBER
9813       )
9814       IS
9815          SELECT ADD_MONTHS (cp_currlevelstartdt, cp_number_of_months)
9816            FROM DUAL;
9817 
9818       l_number_of_months        NUMBER;
9819       l_next_level_start_date   DATE;
9820 
9821       -- SECHAWLA 20-SEP-04 3816891 : new declarations end
9822       SUBTYPE pym_hdr_rec_type IS okl_la_payments_pvt.pym_hdr_rec_type;
9823 
9824       SUBTYPE pym_tbl_type IS okl_la_payments_pvt.pym_tbl_type;
9825 
9826       SUBTYPE rulv_tbl_type IS okl_rule_pub.rulv_tbl_type;
9827 
9828       SUBTYPE pym_del_tbl_type IS okl_la_payments_pvt.pym_del_tbl_type;
9829 
9830       TYPE splitpymt_rec_type IS RECORD (
9831          p_start_date          DATE,
9832          p_number_of_periods   NUMBER,
9833          p_amount              NUMBER,
9834          p_stub_days           NUMBER,
9835          p_stub_amount         NUMBER
9836       );
9837 
9838       TYPE splitpymt_tbl_type IS TABLE OF splitpymt_rec_type
9839          INDEX BY BINARY_INTEGER;
9840 
9841       l_tql_id                  NUMBER;
9842       l_asset_qty               NUMBER;
9843       l_quote_qty               NUMBER;
9844       pym_tbl_ind               NUMBER;
9845       l_pym_freq                VARCHAR2 (30);
9846       lp_pym_tbl                pym_tbl_type;
9847       lp_pym_tbl_empty          pym_tbl_type;
9848       lp_pym_hdr_rec            pym_hdr_rec_type;
9849       l_return_status           VARCHAR2 (1)      := okl_api.g_ret_sts_success;
9850       lx_rulv_tbl               rulv_tbl_type;
9851       l_proppymt_count          NUMBER;
9852       l_splitpymt_count         NUMBER;
9853       l_date_eff_from           DATE;
9854       i                         NUMBER;
9855       lpym_del_tbl              pym_del_tbl_type;
9856       lpym_del_tbl_empty        pym_del_tbl_type;
9857       lpym_del_tbl_count        NUMBER;
9858       l_freq_found              VARCHAR2 (1)       := 'N';
9859       l_name                    VARCHAR2 (150);
9860       l_strm_type_code          VARCHAR2 (150);
9861       l_splitpymt_tbl           splitpymt_tbl_type;
9862       l_splitpymt_tbl_empty     splitpymt_tbl_type;
9863       l_fin_asset_id            NUMBER;
9864       l_upd_required            VARCHAR2 (1);
9865       l_count                   NUMBER;
9866       l_service_line_id         NUMBER;
9867       --Bug #3921591: pagarg +++ Rollover +++
9868       -- Variable to store fine line id
9869       l_fee_line_id             NUMBER;
9870       l_module_name             VARCHAR2 (500)
9871                                          := g_module_name || 'update_payments';
9872       is_debug_exception_on     BOOLEAN
9873               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
9874       is_debug_procedure_on     BOOLEAN
9875               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
9876       is_debug_statement_on     BOOLEAN
9877               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
9878    BEGIN
9879       SAVEPOINT update_payments;
9880 
9881       IF (is_debug_procedure_on)
9882       THEN
9883          okl_debug_pub.log_debug (g_level_procedure,
9884                                   l_module_name,
9885                                   'Begin(+)'
9886                                  );
9887       END IF;
9888 
9889       IF (is_debug_statement_on)
9890       THEN
9891          okl_debug_pub.log_debug (g_level_statement,
9892                                   l_module_name,
9893                                   'In param, p_quote_id: ' || p_quote_id
9894                                  );
9895       END IF;
9896 
9897       IF p_quote_id IS NULL OR p_quote_id = okl_api.g_miss_num
9898       THEN
9899          -- quote id is required
9900          okc_api.set_message (p_app_name          => 'OKC',
9901                               p_msg_name          => g_required_value,
9902                               p_token1            => g_col_name_token,
9903                               p_token1_value      => 'QUOTE_ID'
9904                              );
9905          RAISE okl_api.g_exception_error;
9906       END IF;
9907 
9908       OPEN l_trxquotes_csr (p_quote_id);
9909 
9910       FETCH l_trxquotes_csr
9911        INTO l_date_eff_from;
9912 
9913       IF l_trxquotes_csr%NOTFOUND
9914       THEN
9915          -- quote id is invalid
9916          okc_api.set_message (p_app_name          => 'OKC',
9917                               p_msg_name          => g_invalid_value,
9918                               p_token1            => g_col_name_token,
9919                               p_token1_value      => 'QUOTE_ID'
9920                              );
9921          RAISE okl_api.g_exception_error;
9922       END IF;
9923 
9924       CLOSE l_trxquotes_csr;
9925 
9926       --Bug #3921591: pagarg +++ Rollover +++
9927       -- update payments for all proposed objects of type : financial asset,
9928       -- service line, serviced assets line, fee line, fee asset line
9929       FOR l_quoteobjects_rec IN l_quoteobjects_csr (p_quote_id)
9930       LOOP
9931          l_upd_required := 'N';
9932 
9933          -- current payment exists, but no future payments exist.
9934          IF l_quoteobjects_rec.sts_code IS NULL
9935          THEN
9936             -- This will
9937             -- be the case only if quote is created before the first payment start
9938             -- date. This will not happen now since now first payment start dt
9939             -- is the K start date and quote will be created only after the
9940             -- K is created. So quote eff dt will always be >= first pymt start dt
9941 
9942             -- delete all the payments, for all stream types
9943             lpym_del_tbl_count := 0;
9944             lpym_del_tbl := lpym_del_tbl_empty;
9945 
9946             FOR l_rgpslh_rec IN l_rgpslh_csr (l_quoteobjects_rec.line_id)
9947             LOOP
9948                lpym_del_tbl_count := lpym_del_tbl_count + 1;
9949                lpym_del_tbl (lpym_del_tbl_count).chr_id :=
9950                                                       l_rgpslh_rec.dnz_chr_id;
9951                lpym_del_tbl (lpym_del_tbl_count).rgp_id :=
9952                                                           l_rgpslh_rec.rgp_id;
9953                lpym_del_tbl (lpym_del_tbl_count).slh_id :=
9954                                                           l_rgpslh_rec.slh_id;
9955             END LOOP;
9956 
9957             IF (is_debug_statement_on)
9958             THEN
9959                okl_debug_pub.log_debug
9960                                 (g_level_statement,
9961                                  l_module_name,
9962                                  'calling OKL_LA_PAYMENTS_PVT.delete_payment'
9963                                 );
9964             END IF;
9965 
9966             okl_la_payments_pvt.delete_payment
9967                                           (p_api_version        => p_api_version,
9968                                            p_init_msg_list      => okc_api.g_false,
9969                                            x_return_status      => l_return_status,
9970                                            x_msg_count          => x_msg_count,
9971                                            x_msg_data           => x_msg_data,
9972                                            p_del_pym_tbl        => lpym_del_tbl,
9973                                           --bug #7498330
9974                                            p_source_trx         => 'TQ'
9975                                           );
9976 
9977             IF (is_debug_statement_on)
9978             THEN
9979                okl_debug_pub.log_debug
9980                   (g_level_statement,
9981                    l_module_name,
9982                       'called OKL_LA_PAYMENTS_PVT.delete_payment , return status: '
9983                    || l_return_status
9984                   );
9985             END IF;
9986 
9987             IF (l_return_status = okl_api.g_ret_sts_unexp_error)
9988             THEN
9989                RAISE okl_api.g_exception_unexpected_error;
9990             ELSIF (l_return_status = okl_api.g_ret_sts_error)
9991             THEN
9992                RAISE okl_api.g_exception_error;
9993             END IF;
9994          ELSIF l_quoteobjects_rec.sts_code = 'PROPOSED'
9995          THEN
9996             -- future payment exists for one or more stream types
9997             IF l_quoteobjects_rec.oty_code = 'FINANCIAL_ASSET_LINE'
9998             THEN
9999                l_fin_asset_id := l_quoteobjects_rec.line_id;
10000 
10001                -- check if asset belongs to quote
10002                OPEN l_quotelines_csr (p_quote_id, l_fin_asset_id);
10003 
10004                FETCH l_quotelines_csr
10005                 INTO l_tql_id,
10006                      l_asset_qty,
10007                      l_quote_qty;
10008 
10009                IF l_quotelines_csr%FOUND
10010                THEN
10011                   l_upd_required := 'Y';
10012                END IF;
10013 
10014                CLOSE l_quotelines_csr;
10015             ELSIF l_quoteobjects_rec.oty_code = 'SERVICED_ASSET_LINE'
10016             THEN
10017                --Bug #3921591: pagarg +++ Rollover +++
10018                -- Modified the call to pass line type code also.
10019                -- get the financial asset associated with the subline
10020                OPEN l_finasset_csr (l_quoteobjects_rec.line_id,
10021                                     g_srl_line_lty_code);
10022 
10023                FETCH l_finasset_csr
10024                 INTO l_fin_asset_id,
10025                      l_service_line_id;
10026 
10027                CLOSE l_finasset_csr;
10028 
10029                -- check if asset belongs to quote
10030                OPEN l_quotelines_csr (p_quote_id, l_fin_asset_id);
10031 
10032                FETCH l_quotelines_csr
10033                 INTO l_tql_id,
10034                      l_asset_qty,
10035                      l_quote_qty;
10036 
10037                IF l_quotelines_csr%FOUND
10038                THEN
10039                   l_upd_required := 'Y';
10040                END IF;
10041 
10042                CLOSE l_quotelines_csr;
10043             ELSIF l_quoteobjects_rec.oty_code = 'SERVICE_LINE'
10044             THEN
10045                --Bug #3921591: pagarg +++ Rollover +++
10046                -- Modified the call to pass line type code also.
10047                -- check if service line has any quoted assets.
10048                -- Get all the assets associated with the service line
10049                FOR l_servicelineassets_rec IN
10050                   l_lineassets_csr (l_quoteobjects_rec.line_id,
10051                                     g_srl_line_lty_code)
10052                LOOP
10053                   -- check if asset belongs to quote
10054                   OPEN l_quotelines_csr (p_quote_id,
10055                                          l_servicelineassets_rec.object1_id1);
10056 
10057                   FETCH l_quotelines_csr
10058                    INTO l_tql_id,
10059                         l_asset_qty,
10060                         l_quote_qty;
10061 
10062                   IF l_quotelines_csr%FOUND
10063                   THEN
10064                      l_upd_required := 'Y';
10065 
10066                      CLOSE l_quotelines_csr;
10067 
10068                      EXIT;
10069                   END IF;
10070 
10071                   CLOSE l_quotelines_csr;
10072                END LOOP;
10073             --Bug #3921591: pagarg +++ Rollover +++++++ Start ++++++++++
10074             -- Check whether payment is required to be updated for fee line and fee asset line
10075             ELSIF l_quoteobjects_rec.oty_code = 'FEE_ASSET_LINE'
10076             THEN
10077                -- as of now fee_asset_line type cash flow object is created for rollover fee only.
10078                -- get the financial asset associated with the subline
10079                OPEN l_finasset_csr (l_quoteobjects_rec.line_id,
10080                                     g_fel_line_lty_code);
10081 
10082                FETCH l_finasset_csr
10083                 INTO l_fin_asset_id,
10084                      l_fee_line_id;
10085 
10086                CLOSE l_finasset_csr;
10087 
10088                -- check if asset belongs to quote
10089                OPEN l_quotelines_csr (p_quote_id, l_fin_asset_id);
10090 
10091                FETCH l_quotelines_csr
10092                 INTO l_tql_id,
10093                      l_asset_qty,
10094                      l_quote_qty;
10095 
10096                IF l_quotelines_csr%FOUND
10097                THEN
10098                   l_upd_required := 'Y';
10099                END IF;
10100 
10101                CLOSE l_quotelines_csr;
10102             ELSIF l_quoteobjects_rec.oty_code = 'FEE_LINE'
10103             THEN
10104                -- check if fee line has any quoted assets.
10105                -- Get all the assets associated with the fee line
10106                FOR l_feelineassets_rec IN
10107                   l_lineassets_csr (l_quoteobjects_rec.line_id,
10108                                     g_fel_line_lty_code)
10109                LOOP
10110                   -- check if asset belongs to quote
10111                   OPEN l_quotelines_csr (p_quote_id,
10112                                          l_feelineassets_rec.object1_id1);
10113 
10114                   FETCH l_quotelines_csr
10115                    INTO l_tql_id,
10116                         l_asset_qty,
10117                         l_quote_qty;
10118 
10119                   IF l_quotelines_csr%FOUND
10120                   THEN
10121                      -- smadhava - Bug#5043646 - Added - Start
10122                      -- Now CHECK  if the all the assets attached to this fee are terminated. Going by this logic
10123                      -- as payments are updated in termination flow only after the asset statuses are
10124                      -- updated to TERMINATED. Hence checking for statuses of all assets linked to this fee.
10125                      -- Never update the header payments for partial termination. This is because for
10126                      -- partial temination, by this time, the assets have been split and the orignial
10127                      -- individual units prior to split are separate assets linked to this fee line.
10128                      l_active_assets_flag := 'N';
10129 
10130                      IF (l_asset_qty = l_quote_qty)
10131                      THEN
10132                         -- cursor to check if there are assets associated to this fee which are still ACTIVE
10133                         -- if there are ACTIVE assets, then donot update the payment at the header
10134                         OPEN c_find_active_link_assets
10135                                                  (l_quoteobjects_rec.line_id,
10136                                                   g_fel_line_lty_code);
10137 
10138                         FETCH c_find_active_link_assets
10139                          INTO l_active_assets_flag;
10140 
10141                         -- update the payments on header lines only if all the associated assets
10142                         -- have been terminated
10143                         IF c_find_active_link_assets%NOTFOUND
10144                         THEN
10145                            l_upd_required := 'Y';
10146                         END IF;
10147 
10148                         CLOSE c_find_active_link_assets;
10149                      END IF;
10150 
10151                      -- Assigning fee line id to the variable
10152                      l_fee_line_id := l_quoteobjects_rec.line_id;
10153 
10154                      -- smadhava - Bug#5043646 - Added - End
10155                      CLOSE l_quotelines_csr;
10156 
10157                      EXIT;
10158                   END IF;
10159 
10160                   CLOSE l_quotelines_csr;
10161                END LOOP;
10162             END IF;
10163 
10164             --Bug #3921591: pagarg +++ Rollover +++++++ End ++++++++++
10165             IF l_upd_required = 'Y'
10166             THEN
10167                --Bug #3921591: pagarg +++ Rollover +++
10168                -- Modified the condition to include fee line and fee asset line also
10169                IF (   (    l_quoteobjects_rec.oty_code IN
10170                               ('FINANCIAL_ASSET_LINE',
10171                                'SERVICED_ASSET_LINE',
10172                                'FEE_ASSET_LINE'
10173                               )
10174                        AND l_asset_qty =
10175                               l_quote_qty
10176                                  -- full line termination, asset was not split
10177                       )
10178                    OR l_quoteobjects_rec.oty_code IN
10179                                                  ('SERVICE_LINE', 'FEE_LINE')
10180                   -- service line level pymts are not split, if
10181                   -- any of the attached assets are partially terminated.
10182                   -- so we can always overwrite the service line level pymts
10183                   )
10184                THEN
10185                   -- overwrite the payments
10186                   -- delete all the payments, for all stream types
10187                   lpym_del_tbl_count := 0;
10188                   lpym_del_tbl := lpym_del_tbl_empty;
10189 
10190                   FOR l_rgpslh_rec IN
10191                      l_rgpslh_csr (l_quoteobjects_rec.line_id)
10192                   LOOP
10193                      lpym_del_tbl_count := lpym_del_tbl_count + 1;
10194                      lpym_del_tbl (lpym_del_tbl_count).chr_id :=
10195                                                       l_rgpslh_rec.dnz_chr_id;
10196                      lpym_del_tbl (lpym_del_tbl_count).rgp_id :=
10197                                                           l_rgpslh_rec.rgp_id;
10198                      lpym_del_tbl (lpym_del_tbl_count).slh_id :=
10199                                                           l_rgpslh_rec.slh_id;
10200                   END LOOP;
10201 
10202                   IF (is_debug_statement_on)
10203                   THEN
10204                      okl_debug_pub.log_debug
10205                                 (g_level_statement,
10206                                  l_module_name,
10207                                  'calling OKL_LA_PAYMENTS_PVT.delete_payment'
10208                                 );
10209                   END IF;
10210 
10211                   okl_la_payments_pvt.delete_payment
10212                                           (p_api_version        => p_api_version,
10213                                            p_init_msg_list      => okc_api.g_false,
10214                                            x_return_status      => l_return_status,
10215                                            x_msg_count          => x_msg_count,
10216                                            x_msg_data           => x_msg_data,
10217                                            p_del_pym_tbl        => lpym_del_tbl,
10218                                           --bug # 7498330
10219                                            p_source_trx         => 'TQ'
10220                                           );
10221 
10222                   IF (is_debug_statement_on)
10223                   THEN
10224                      okl_debug_pub.log_debug
10225                             (g_level_statement,
10226                              l_module_name,
10227                                 'calling OKL_LA_PAYMENTS_PVT.delete_payment '
10228                              || l_return_status
10229                             );
10230                   END IF;
10231 
10232                   IF (l_return_status = okl_api.g_ret_sts_unexp_error)
10233                   THEN
10234                      RAISE okl_api.g_exception_unexpected_error;
10235                   ELSIF (l_return_status = okl_api.g_ret_sts_error)
10236                   THEN
10237                      RAISE okl_api.g_exception_error;
10238                   END IF;
10239 -- Bug 6908509: add start
10240                  l_freq_found := 'N';
10241 -- Bug 6908509: add end
10242                   -- create new payments from cash flow tables
10243                   -- get all the proposed stream types
10244                   FOR l_cashflowstreams_rec IN
10245                      l_cashflowstreams_csr (l_quoteobjects_rec.cfo_id)
10246                   LOOP
10247                      -- create payment for each proposed stream type
10248                      pym_tbl_ind := 0;
10249                      lp_pym_tbl := lp_pym_tbl_empty;
10250 
10251                      -- get all the proposed cash flow levels and populate lp_pym_tbl
10252                      FOR l_cashflowlevels_rec IN
10253                         l_cashflowlevels_csr (l_cashflowstreams_rec.caf_id)
10254                      LOOP
10255                         IF (    l_freq_found = 'N'
10256                             AND l_cashflowlevels_rec.fqy_code IS NOT NULL
10257                            )
10258                         THEN
10259                            l_pym_freq := l_cashflowlevels_rec.fqy_code;
10260                            l_freq_found := 'Y';
10261                         END IF;
10262 
10263                         pym_tbl_ind := pym_tbl_ind + 1;
10264                         lp_pym_tbl (pym_tbl_ind).stub_days :=
10265                                                 l_cashflowlevels_rec.stub_days;
10266                         lp_pym_tbl (pym_tbl_ind).stub_amount :=
10267                                               l_cashflowlevels_rec.stub_amount;
10268                         lp_pym_tbl (pym_tbl_ind).period :=
10269                                         l_cashflowlevels_rec.number_of_periods;
10270                         lp_pym_tbl (pym_tbl_ind).amount :=
10271                                                    l_cashflowlevels_rec.amount;
10272                         lp_pym_tbl (pym_tbl_ind).update_type := 'CREATE';
10273                      END LOOP;
10274 
10275                      IF l_freq_found = 'N'
10276                      THEN
10277                         -- all levels are stub levels
10278                         l_pym_freq := 'M';
10279       -- default freq to Monthly for Stub payments, as it is a required field
10280                      END IF;
10281 
10282                      lp_pym_hdr_rec.STRUCTURE :=
10283                                l_cashflowstreams_rec.number_of_advance_periods;
10284                      lp_pym_hdr_rec.frequency := l_pym_freq;
10285                      lp_pym_hdr_rec.arrears :=
10286                                           l_cashflowstreams_rec.due_arrears_yn;
10287 
10288                      IF lp_pym_tbl.COUNT > 0
10289                      THEN
10290                         IF l_quoteobjects_rec.oty_code =
10291                                                        'FINANCIAL_ASSET_LINE'
10292                         THEN
10293                            -- create asset level payments
10294                            IF (is_debug_statement_on)
10295                            THEN
10296                               okl_debug_pub.log_debug
10297                                  (g_level_statement,
10298                                   l_module_name,
10299                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10300                                  );
10301                            END IF;
10302 
10303                            okl_la_payments_pvt.process_payment
10304                                 (p_api_version        => p_api_version,
10305                                  p_init_msg_list      => okc_api.g_false,
10306                                  x_return_status      => l_return_status,
10307                                  x_msg_count          => x_msg_count,
10308                                  x_msg_data           => x_msg_data,
10309                                  p_chr_id             => l_cashflowstreams_rec.dnz_khr_id,
10310                                  --  p_service_fee_id         => OKC_API.G_MISS_NUM,
10311                                  p_asset_id           => l_quoteobjects_rec.line_id,
10312                                  p_payment_id         => l_cashflowstreams_rec.sty_id,
10313                                  p_pym_hdr_rec        => lp_pym_hdr_rec,
10314                                  p_pym_tbl            => lp_pym_tbl,
10315                                  p_update_type        => 'CREATE',
10316                                  x_rulv_tbl           => lx_rulv_tbl
10317                                 );
10318 
10319                            IF (is_debug_statement_on)
10320                            THEN
10321                               okl_debug_pub.log_debug
10322                                  (g_level_statement,
10323                                   l_module_name,
10324                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10325                                   || l_return_status
10326                                  );
10327                            END IF;
10328                         ELSIF l_quoteobjects_rec.oty_code =
10329                                                          'SERVICED_ASSET_LINE'
10330                         THEN
10331                            IF (is_debug_statement_on)
10332                            THEN
10333                               okl_debug_pub.log_debug
10334                                  (g_level_statement,
10335                                   l_module_name,
10336                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10337                                  );
10338                            END IF;
10339 
10340                            -- create subline level payments
10341                            okl_la_payments_pvt.process_payment
10342                                 (p_api_version         => p_api_version,
10343                                  p_init_msg_list       => okc_api.g_false,
10344                                  x_return_status       => l_return_status,
10345                                  x_msg_count           => x_msg_count,
10346                                  x_msg_data            => x_msg_data,
10347                                  p_chr_id              => l_cashflowstreams_rec.dnz_khr_id,
10348                                  p_service_fee_id      => l_service_line_id,
10349                                  p_asset_id            => l_fin_asset_id,
10350                                  p_payment_id          => l_cashflowstreams_rec.sty_id,
10351                                  p_pym_hdr_rec         => lp_pym_hdr_rec,
10352                                  p_pym_tbl             => lp_pym_tbl,
10353                                  p_update_type         => 'CREATE',
10354                                  x_rulv_tbl            => lx_rulv_tbl
10355                                 );
10356 
10357                            IF (is_debug_statement_on)
10358                            THEN
10359                               okl_debug_pub.log_debug
10360                                  (g_level_statement,
10361                                   l_module_name,
10362                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10363                                   || l_return_status
10364                                  );
10365                            END IF;
10366                         ELSIF l_quoteobjects_rec.oty_code = 'SERVICE_LINE'
10367                         THEN
10368                            -- create service line level payments
10369                            IF (is_debug_statement_on)
10370                            THEN
10371                               okl_debug_pub.log_debug
10372                                  (g_level_statement,
10373                                   l_module_name,
10374                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10375                                  );
10376                            END IF;
10377 
10378                            okl_la_payments_pvt.process_payment
10379                               (p_api_version         => p_api_version,
10380                                p_init_msg_list       => okc_api.g_false,
10381                                x_return_status       => l_return_status,
10382                                x_msg_count           => x_msg_count,
10383                                x_msg_data            => x_msg_data,
10384                                p_chr_id              => l_cashflowstreams_rec.dnz_khr_id,
10385                                p_service_fee_id      => l_quoteobjects_rec.line_id,
10386                                --p_asset_id               => l_quoteobjects_rec.line_id,
10387                                p_payment_id          => l_cashflowstreams_rec.sty_id,
10388                                p_pym_hdr_rec         => lp_pym_hdr_rec,
10389                                p_pym_tbl             => lp_pym_tbl,
10390                                p_update_type         => 'CREATE',
10391                                x_rulv_tbl            => lx_rulv_tbl
10392                               );
10393 
10394                            IF (is_debug_statement_on)
10395                            THEN
10396                               okl_debug_pub.log_debug
10397                                  (g_level_statement,
10398                                   l_module_name,
10399                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10400                                   || l_return_status
10401                                  );
10402                            END IF;
10403                         --Bug #3921591: pagarg +++ Rollover +++++++ Start ++++++++++
10404                         -- creating payments for fee line and fee asset line.
10405                         ELSIF l_quoteobjects_rec.oty_code = 'FEE_ASSET_LINE'
10406                         THEN
10407                            -- create fee asset subline level payments
10408                            IF (is_debug_statement_on)
10409                            THEN
10410                               okl_debug_pub.log_debug
10411                                  (g_level_statement,
10412                                   l_module_name,
10413                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10414                                  );
10415                            END IF;
10416 
10417                            okl_la_payments_pvt.process_payment
10418                                 (p_api_version         => p_api_version,
10419                                  p_init_msg_list       => okc_api.g_false,
10420                                  x_return_status       => l_return_status,
10421                                  x_msg_count           => x_msg_count,
10422                                  x_msg_data            => x_msg_data,
10423                                  p_chr_id              => l_cashflowstreams_rec.dnz_khr_id,
10424                                  p_service_fee_id      => l_fee_line_id,
10425                                  p_asset_id            => l_fin_asset_id,
10426                                  p_payment_id          => l_cashflowstreams_rec.sty_id,
10427                                  p_pym_hdr_rec         => lp_pym_hdr_rec,
10428                                  p_pym_tbl             => lp_pym_tbl,
10429                                  p_update_type         => 'CREATE',
10430                                  x_rulv_tbl            => lx_rulv_tbl
10431                                 );
10432 
10433                            IF (is_debug_statement_on)
10434                            THEN
10435                               okl_debug_pub.log_debug
10436                                  (g_level_statement,
10437                                   l_module_name,
10438                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10439                                   || l_return_status
10440                                  );
10441                            END IF;
10442                         ELSIF l_quoteobjects_rec.oty_code = 'FEE_LINE'
10443                         THEN
10444                            IF (is_debug_statement_on)
10445                            THEN
10446                               okl_debug_pub.log_debug
10447                                  (g_level_statement,
10448                                   l_module_name,
10449                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10450                                  );
10451                            END IF;
10452 
10453                            -- create fee line level payments
10454                            okl_la_payments_pvt.process_payment
10455                                 (p_api_version         => p_api_version,
10456                                  p_init_msg_list       => okc_api.g_false,
10457                                  x_return_status       => l_return_status,
10458                                  x_msg_count           => x_msg_count,
10459                                  x_msg_data            => x_msg_data,
10460                                  p_chr_id              => l_cashflowstreams_rec.dnz_khr_id,
10461                                  p_service_fee_id      => l_fee_line_id,
10462                                  p_payment_id          => l_cashflowstreams_rec.sty_id,
10463                                  p_pym_hdr_rec         => lp_pym_hdr_rec,
10464                                  p_pym_tbl             => lp_pym_tbl,
10465                                  p_update_type         => 'CREATE',
10466                                  x_rulv_tbl            => lx_rulv_tbl
10467                                 );
10468 
10469                            --Bug #3921591: pagarg +++ Rollover +++++++ End ++++++++++
10470                            IF (is_debug_statement_on)
10471                            THEN
10472                               okl_debug_pub.log_debug
10473                                  (g_level_statement,
10474                                   l_module_name,
10475                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10476                                   || l_return_status
10477                                  );
10478                            END IF;
10479                         END IF;
10480 
10481                         IF (l_return_status = okl_api.g_ret_sts_unexp_error)
10482                         THEN
10483                            RAISE okl_api.g_exception_unexpected_error;
10484                         ELSIF (l_return_status = okl_api.g_ret_sts_error)
10485                         THEN
10486                            RAISE okl_api.g_exception_error;
10487                         END IF;
10488                      END IF;
10489                   END LOOP;
10490                ELSE
10491                   -- partial line termination (for fin assets and serviced assets only)
10492                   -- asset was split. cann't overwrite the split payments with proposed pymts
10493                   -- on quote. Adjust the split asset payment, save in a table and then overwrite the
10494                   -- existing payments with the adjusted payments
10495 
10496                   -- get all stream types (from rules), for which payments exist, after split asset
10497                   FOR l_rgpslh_rec IN
10498                      l_rgpslh_csr (l_quoteobjects_rec.line_id)
10499                   LOOP
10500                      -- get the payments for this stream type (from rules) as they exist after the split asset, and populate l_splitpymt_tbl
10501                      l_splitpymt_count := 0;
10502                      l_splitpymt_tbl := l_splitpymt_tbl_empty;
10503 
10504                      -- populate l_splitpymt_tbl with payments as they exist after split
10505                      FOR l_lpayments_rec IN
10506                         l_lpayments_csr (l_quoteobjects_rec.line_id,
10507                                          l_rgpslh_rec.sty_id)
10508                      LOOP
10509                         l_splitpymt_count := l_splitpymt_count + 1;
10510                         l_splitpymt_tbl (l_splitpymt_count).p_start_date :=
10511                            TO_DATE (l_lpayments_rec.start_date,
10512                                     'yyyy/mm/dd hh24:mi:ss');
10513                         l_splitpymt_tbl (l_splitpymt_count).p_number_of_periods :=
10514                                                       l_lpayments_rec.periods;
10515                         l_splitpymt_tbl (l_splitpymt_count).p_amount :=
10516                                                        l_lpayments_rec.amount;
10517                         l_splitpymt_tbl (l_splitpymt_count).p_stub_days :=
10518                                                     l_lpayments_rec.stub_days;
10519                         l_splitpymt_tbl (l_splitpymt_count).p_stub_amount :=
10520                                                   l_lpayments_rec.stub_amount;
10521                      END LOOP;
10522 
10523                      -- loop thru the proposed payments, calculated by quote creation process, or this stream type
10524                      i := 0;
10525 
10526                      FOR l_cashflows_rec IN
10527                         l_cashflows_csr (l_quoteobjects_rec.cfo_id,
10528                                          l_rgpslh_rec.sty_id)
10529                      LOOP
10530                         i := i + 1;
10531 
10532                         -- if proposed payment exists, current (split) payment will definitely exist
10533                         IF l_splitpymt_tbl.EXISTS (i)
10534                         THEN
10535                            IF     l_cashflows_rec.start_date =
10536                                              l_splitpymt_tbl (i).p_start_date
10537                               AND l_cashflows_rec.start_date <=
10538                                                                l_date_eff_from
10539                            THEN
10540                               IF l_cashflows_rec.due_arrears_yn = 'N'
10541                               THEN
10542                                  -- SECHAWLA 20-SEP-04 3816891 : ADV, Added this condition
10543                                  -- SECHAWLA 20-SEP-04 3816891 Keep the same logic for Advance : begin
10544                                  -- update period or stub days
10545                                  IF l_cashflows_rec.stub_days IS NULL
10546                                  THEN
10547                                     IF l_splitpymt_tbl (i).p_number_of_periods <>
10548                                             l_cashflows_rec.number_of_periods
10549                                     THEN
10550                                        l_splitpymt_tbl (i).p_number_of_periods :=
10551                                             l_cashflows_rec.number_of_periods;
10552                                     END IF;
10553                                  ELSE
10554                                     IF l_splitpymt_tbl (i).p_stub_days <>
10555                                                     l_cashflows_rec.stub_days
10556                                     THEN
10557                                        l_splitpymt_tbl (i).p_stub_days :=
10558                                                     l_cashflows_rec.stub_days;
10559                                     END IF;
10560                                  END IF;
10561                               -- SECHAWLA 20-SEP-04 3816891 Keep the same logic for Advance : end
10562                               ELSIF l_cashflows_rec.due_arrears_yn = 'Y'
10563                               THEN
10564                                  -- SECHAWLA 20-SEP-04 3816891 : Arrears, added this section
10565 
10566                                  -- SECHAWLA 20-SEP-04 3816891 : Added following piece of code for Arrears : begin
10567 
10568                                  -----------------------------------
10569                                  IF l_cashflows_rec.stub_days IS NULL
10570                                  THEN
10571                                     -- get the number of months that a payment covers
10572                                     IF l_cashflows_rec.fqy_code = 'M'
10573                                     THEN
10574                                        l_number_of_months :=
10575                                           (l_cashflows_rec.number_of_periods
10576                                           );
10577                                     ELSIF l_cashflows_rec.fqy_code = 'Q'
10578                                     THEN
10579                                        l_number_of_months :=
10580                                             (l_cashflows_rec.number_of_periods
10581                                             )
10582                                           * 3;
10583                                     ELSIF l_cashflows_rec.fqy_code = 'S'
10584                                     THEN
10585                                        l_number_of_months :=
10586                                             (l_cashflows_rec.number_of_periods
10587                                             )
10588                                           * 6;
10589                                     ELSIF l_cashflows_rec.fqy_code = 'A'
10590                                     THEN
10591                                        l_number_of_months :=
10592                                             (l_cashflows_rec.number_of_periods
10593                                             )
10594                                           * 12;
10595                                     END IF;
10596 
10597                                     -- add months
10598                                     -- Get the first date after the last level period ends
10599                                     OPEN l_nextlevelstartdt_csr
10600                                                   (l_cashflows_rec.start_date,
10601                                                    l_number_of_months);
10602 
10603                                     FETCH l_nextlevelstartdt_csr
10604                                      INTO l_next_level_start_date;
10605 
10606                                     CLOSE l_nextlevelstartdt_csr;
10607                                  ELSE
10608                                     -- sechawla 20-SEP-04  3816891 : still ok
10609                                     l_next_level_start_date :=
10610                                          l_cashflows_rec.start_date
10611                                        + l_cashflows_rec.stub_days;
10612                                  END IF;
10613 
10614 -----------------------
10615                                  IF l_date_eff_from >=
10616                                                 (l_next_level_start_date - 1
10617                                                 )
10618                                  THEN
10619                                     ---last day of the current level
10620                                     -- keep the level, update p_number_of_periods/p_stub_days, as above
10621 
10622                                     -- update period or stub days
10623                                     IF l_cashflows_rec.stub_days IS NULL
10624                                     THEN
10625                                        IF l_splitpymt_tbl (i).p_number_of_periods <>
10626                                              l_cashflows_rec.number_of_periods
10627                                        THEN
10628                                           l_splitpymt_tbl (i).p_number_of_periods :=
10629                                              l_cashflows_rec.number_of_periods;
10630                                        END IF;
10631                                     ELSE
10632                                        IF l_splitpymt_tbl (i).p_stub_days <>
10633                                                     l_cashflows_rec.stub_days
10634                                        THEN
10635                                           l_splitpymt_tbl (i).p_stub_days :=
10636                                                     l_cashflows_rec.stub_days;
10637                                        END IF;
10638                                     END IF;
10639                                  ELSE
10640                                     l_splitpymt_tbl.DELETE
10641                                                        (i,
10642                                                         l_splitpymt_tbl.COUNT);
10643                                     EXIT;
10644                                  END IF;
10645                               -- SECHAWLA 20-SEP-04 3816891 : Added following piece of code for Arrears : end
10646                               END IF;            -- SECHAWLA 20-SEP-04 3816891
10647                            ELSIF    l_cashflows_rec.start_date <
10648                                        l_splitpymt_tbl (i).p_start_date
10649                                         -- split has happened in proposed pymt
10650                                  OR l_cashflows_rec.start_date >
10651                                                                l_date_eff_from
10652                            THEN
10653                               -- SECHAWLA 20-SEP-04 3816891 : still ok, no changes
10654                               l_splitpymt_tbl.DELETE (i,
10655                                                       l_splitpymt_tbl.COUNT);
10656                               EXIT;
10657                            END IF;
10658                         END IF;
10659                      END LOOP;
10660 
10661                      IF i = 0
10662                      THEN
10663                         -- proposed payment does not exist for this stream type
10664                         -- delete the payment from rules, for this stream type
10665                         lpym_del_tbl := lpym_del_tbl_empty;
10666                         lpym_del_tbl (1).chr_id := l_rgpslh_rec.dnz_chr_id;
10667                         lpym_del_tbl (1).rgp_id := l_rgpslh_rec.rgp_id;
10668                         lpym_del_tbl (1).slh_id := l_rgpslh_rec.slh_id;
10669 
10670                         IF (is_debug_statement_on)
10671                         THEN
10672                            okl_debug_pub.log_debug
10673                                 (g_level_statement,
10674                                  l_module_name,
10675                                  'calling OKL_LA_PAYMENTS_PVT.delete_payment'
10676                                 );
10677                         END IF;
10678 
10679                         okl_la_payments_pvt.delete_payment
10680                                           (p_api_version        => p_api_version,
10681                                            p_init_msg_list      => okc_api.g_false,
10682                                            x_return_status      => l_return_status,
10683                                            x_msg_count          => x_msg_count,
10684                                            x_msg_data           => x_msg_data,
10685                                            p_del_pym_tbl        => lpym_del_tbl,
10686                                           --bug # 7498330
10687                                            p_source_trx         => 'TQ'
10688                                           );
10689 
10690                         IF (is_debug_statement_on)
10691                         THEN
10692                            okl_debug_pub.log_debug
10693                               (g_level_statement,
10694                                l_module_name,
10695                                   'calling OKL_LA_PAYMENTS_PVT.delete_payment , return status: '
10696                                || l_return_status
10697                               );
10698                         END IF;
10699 
10700                         IF (l_return_status = okl_api.g_ret_sts_unexp_error)
10701                         THEN
10702                            RAISE okl_api.g_exception_unexpected_error;
10703                         ELSIF (l_return_status = okl_api.g_ret_sts_error)
10704                         THEN
10705                            RAISE okl_api.g_exception_error;
10706                         END IF;
10707                      END IF;
10708 
10709                      IF l_splitpymt_tbl.COUNT > 0
10710                      THEN
10711                         -- delete the payment for this stream type
10712                         lpym_del_tbl := lpym_del_tbl_empty;
10713                         lpym_del_tbl (1).chr_id := l_rgpslh_rec.dnz_chr_id;
10714                         lpym_del_tbl (1).rgp_id := l_rgpslh_rec.rgp_id;
10715                         lpym_del_tbl (1).slh_id := l_rgpslh_rec.slh_id;
10716 
10717                         IF (is_debug_statement_on)
10718                         THEN
10719                            okl_debug_pub.log_debug
10720                                 (g_level_statement,
10721                                  l_module_name,
10722                                  'calling OKL_LA_PAYMENTS_PVT.delete_payment'
10723                                 );
10724                         END IF;
10725 
10726                         okl_la_payments_pvt.delete_payment
10727                                           (p_api_version        => p_api_version,
10728                                            p_init_msg_list      => okc_api.g_false,
10729                                            x_return_status      => l_return_status,
10730                                            x_msg_count          => x_msg_count,
10731                                            x_msg_data           => x_msg_data,
10732                                            p_del_pym_tbl        => lpym_del_tbl,
10733                                          --bug # 7498330
10734                                            p_source_trx         => 'TQ'
10735                                           );
10736 
10737                         IF (is_debug_statement_on)
10738                         THEN
10739                            okl_debug_pub.log_debug
10740                               (g_level_statement,
10741                                l_module_name,
10742                                   'calling OKL_LA_PAYMENTS_PVT.delete_payment , return status: '
10743                                || l_return_status
10744                               );
10745                         END IF;
10746 
10747                         IF (l_return_status = okl_api.g_ret_sts_unexp_error)
10748                         THEN
10749                            RAISE okl_api.g_exception_unexpected_error;
10750                         ELSIF (l_return_status = okl_api.g_ret_sts_error)
10751                         THEN
10752                            RAISE okl_api.g_exception_error;
10753                         END IF;
10754 
10755                         -- create payment for this stream type from the l_splitpymt_tbl table
10756                         lp_pym_hdr_rec.STRUCTURE :=
10757                                                   l_rgpslh_rec.advance_periods;
10758                         lp_pym_hdr_rec.frequency := l_rgpslh_rec.frequency;
10759                         lp_pym_hdr_rec.arrears := l_rgpslh_rec.due_arrears_yn;
10760                         lp_pym_tbl := lp_pym_tbl_empty;
10761                         i := l_splitpymt_tbl.FIRST;
10762 
10763                         LOOP
10764                            lp_pym_tbl (i).stub_days :=
10765                                               l_splitpymt_tbl (i).p_stub_days;
10766                            lp_pym_tbl (i).stub_amount :=
10767                                             l_splitpymt_tbl (i).p_stub_amount;
10768                            lp_pym_tbl (i).period :=
10769                                       l_splitpymt_tbl (i).p_number_of_periods;
10770                            lp_pym_tbl (i).amount :=
10771                                                  l_splitpymt_tbl (i).p_amount;
10772                            lp_pym_tbl (i).update_type := 'CREATE';
10773                            EXIT WHEN (i = l_splitpymt_tbl.LAST);
10774                            i := l_splitpymt_tbl.NEXT (i);
10775                         END LOOP;
10776 
10777                         IF l_quoteobjects_rec.oty_code =
10778                                                         'FINANCIAL_ASSET_LINE'
10779                         THEN
10780                            -- create asset level payments
10781                            IF (is_debug_statement_on)
10782                            THEN
10783                               okl_debug_pub.log_debug
10784                                  (g_level_statement,
10785                                   l_module_name,
10786                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10787                                  );
10788                            END IF;
10789 
10790                            okl_la_payments_pvt.process_payment
10791                                     (p_api_version        => p_api_version,
10792                                      p_init_msg_list      => okc_api.g_false,
10793                                      x_return_status      => l_return_status,
10794                                      x_msg_count          => x_msg_count,
10795                                      x_msg_data           => x_msg_data,
10796                                      p_chr_id             => l_rgpslh_rec.dnz_chr_id,
10797                                      --  p_service_fee_id         =>         OKC_API.G_MISS_NUM,
10798                                      p_asset_id           => l_quoteobjects_rec.line_id,
10799                                      p_payment_id         => l_rgpslh_rec.sty_id,
10800                                      p_pym_hdr_rec        => lp_pym_hdr_rec,
10801                                      p_pym_tbl            => lp_pym_tbl,
10802                                      p_update_type        => 'CREATE',
10803                                      x_rulv_tbl           => lx_rulv_tbl
10804                                     );
10805 
10806                            IF (is_debug_statement_on)
10807                            THEN
10808                               okl_debug_pub.log_debug
10809                                  (g_level_statement,
10810                                   l_module_name,
10811                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10812                                   || l_return_status
10813                                  );
10814                            END IF;
10815                         ELSIF l_quoteobjects_rec.oty_code =
10816                                                          'SERVICED_ASSET_LINE'
10817                         THEN
10818                            IF (is_debug_statement_on)
10819                            THEN
10820                               okl_debug_pub.log_debug
10821                                  (g_level_statement,
10822                                   l_module_name,
10823                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10824                                  );
10825                            END IF;
10826 
10827                            -- create subline level payments
10828                            okl_la_payments_pvt.process_payment
10829                                        (p_api_version         => p_api_version,
10830                                         p_init_msg_list       => okc_api.g_false,
10831                                         x_return_status       => l_return_status,
10832                                         x_msg_count           => x_msg_count,
10833                                         x_msg_data            => x_msg_data,
10834                                         p_chr_id              => l_rgpslh_rec.dnz_chr_id,
10835                                         p_service_fee_id      => l_service_line_id,
10836                                         p_asset_id            => l_fin_asset_id,
10837                                         p_payment_id          => l_rgpslh_rec.sty_id,
10838                                         p_pym_hdr_rec         => lp_pym_hdr_rec,
10839                                         p_pym_tbl             => lp_pym_tbl,
10840                                         p_update_type         => 'CREATE',
10841                                         x_rulv_tbl            => lx_rulv_tbl
10842                                        );
10843 
10844                            IF (is_debug_statement_on)
10845                            THEN
10846                               okl_debug_pub.log_debug
10847                                  (g_level_statement,
10848                                   l_module_name,
10849                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10850                                   || l_return_status
10851                                  );
10852                            END IF;
10853                         --Bug #3921591: pagarg +++ Rollover +++++++ Start ++++++++++
10854                         ELSIF l_quoteobjects_rec.oty_code = 'FEE_ASSET_LINE'
10855                         THEN
10856                            IF (is_debug_statement_on)
10857                            THEN
10858                               okl_debug_pub.log_debug
10859                                  (g_level_statement,
10860                                   l_module_name,
10861                                   'calling OKL_LA_PAYMENTS_PVT.process_payment'
10862                                  );
10863                            END IF;
10864 
10865                            -- create fee asset subline level payments
10866                            okl_la_payments_pvt.process_payment
10867                                          (p_api_version         => p_api_version,
10868                                           p_init_msg_list       => okc_api.g_false,
10869                                           x_return_status       => l_return_status,
10870                                           x_msg_count           => x_msg_count,
10871                                           x_msg_data            => x_msg_data,
10872                                           p_chr_id              => l_rgpslh_rec.dnz_chr_id,
10873                                           p_service_fee_id      => l_fee_line_id,
10874                                           p_asset_id            => l_fin_asset_id,
10875                                           p_payment_id          => l_rgpslh_rec.sty_id,
10876                                           p_pym_hdr_rec         => lp_pym_hdr_rec,
10877                                           p_pym_tbl             => lp_pym_tbl,
10878                                           p_update_type         => 'CREATE',
10879                                           x_rulv_tbl            => lx_rulv_tbl
10880                                          );
10881 
10882                            --Bug #3921591: pagarg +++ Rollover +++++++ End ++++++++++
10883                            IF (is_debug_statement_on)
10884                            THEN
10885                               okl_debug_pub.log_debug
10886                                  (g_level_statement,
10887                                   l_module_name,
10888                                      'called OKL_LA_PAYMENTS_PVT.process_payment , return status: '
10889                                   || l_return_status
10890                                  );
10891                            END IF;
10892                         END IF;
10893 
10894                         IF (l_return_status = okl_api.g_ret_sts_unexp_error)
10895                         THEN
10896                            RAISE okl_api.g_exception_unexpected_error;
10897                         ELSIF (l_return_status = okl_api.g_ret_sts_error)
10898                         THEN
10899                            RAISE okl_api.g_exception_error;
10900                         END IF;
10901                      END IF;
10902                   END LOOP;
10903                END IF;
10904             END IF;                                       -- if asset in quote
10905          END IF;
10906       END LOOP;
10907 
10908       x_return_status := l_return_status;
10909 
10910       IF (is_debug_procedure_on)
10911       THEN
10912          okl_debug_pub.log_debug (g_level_procedure,
10913                                   l_module_name,
10914                                   'End(-)'
10915                                  );
10916       END IF;
10917    EXCEPTION
10918       WHEN okl_api.g_exception_error
10919       THEN
10920          IF l_quoteobjects_csr%ISOPEN
10921          THEN
10922             CLOSE l_quoteobjects_csr;
10923          END IF;
10924 
10925          IF l_trxquotes_csr%ISOPEN
10926          THEN
10927             CLOSE l_trxquotes_csr;
10928          END IF;
10929 
10930          IF l_quotelines_csr%ISOPEN
10931          THEN
10932             CLOSE l_quotelines_csr;
10933          END IF;
10934 
10935          IF l_cashflowstreams_csr%ISOPEN
10936          THEN
10937             CLOSE l_cashflowstreams_csr;
10938          END IF;
10939 
10940          IF l_cashflowlevels_csr%ISOPEN
10941          THEN
10942             CLOSE l_cashflowlevels_csr;
10943          END IF;
10944 
10945          IF l_rgpslh_csr%ISOPEN
10946          THEN
10947             CLOSE l_rgpslh_csr;
10948          END IF;
10949 
10950          IF l_cashflows_csr%ISOPEN
10951          THEN
10952             CLOSE l_cashflows_csr;
10953          END IF;
10954 
10955          IF l_lpayments_csr%ISOPEN
10956          THEN
10957             CLOSE l_lpayments_csr;
10958          END IF;
10959 
10960          IF l_finasset_csr%ISOPEN
10961          THEN
10962             CLOSE l_finasset_csr;
10963          END IF;
10964 
10965          --Bug #3921591: pagarg +++ Rollover +++
10966          -- Changed the cursor name as made it generalised
10967          IF l_lineassets_csr%ISOPEN
10968          THEN
10969             CLOSE l_lineassets_csr;
10970          END IF;
10971 
10972          ROLLBACK TO update_payments;
10973          x_return_status := g_ret_sts_error;
10974 
10975          IF (is_debug_exception_on)
10976          THEN
10977             okl_debug_pub.log_debug (g_level_exception,
10978                                      l_module_name,
10979                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
10980                                     );
10981          END IF;
10982       WHEN okl_api.g_exception_unexpected_error
10983       THEN
10984          IF l_quoteobjects_csr%ISOPEN
10985          THEN
10986             CLOSE l_quoteobjects_csr;
10987          END IF;
10988 
10989          IF l_trxquotes_csr%ISOPEN
10990          THEN
10991             CLOSE l_trxquotes_csr;
10992          END IF;
10993 
10994          IF l_quotelines_csr%ISOPEN
10995          THEN
10996             CLOSE l_quotelines_csr;
10997          END IF;
10998 
10999          IF l_cashflowstreams_csr%ISOPEN
11000          THEN
11001             CLOSE l_cashflowstreams_csr;
11002          END IF;
11003 
11004          IF l_cashflowlevels_csr%ISOPEN
11005          THEN
11006             CLOSE l_cashflowlevels_csr;
11007          END IF;
11008 
11009          IF l_rgpslh_csr%ISOPEN
11010          THEN
11011             CLOSE l_rgpslh_csr;
11012          END IF;
11013 
11014          IF l_cashflows_csr%ISOPEN
11015          THEN
11016             CLOSE l_cashflows_csr;
11017          END IF;
11018 
11019          IF l_lpayments_csr%ISOPEN
11020          THEN
11021             CLOSE l_lpayments_csr;
11022          END IF;
11023 
11024          IF l_finasset_csr%ISOPEN
11025          THEN
11026             CLOSE l_finasset_csr;
11027          END IF;
11028 
11029          --Bug #3921591: pagarg +++ Rollover +++
11030          -- Changed the cursor name as made it generalised
11031          IF l_lineassets_csr%ISOPEN
11032          THEN
11033             CLOSE l_lineassets_csr;
11034          END IF;
11035 
11036          ROLLBACK TO update_payments;
11037          x_return_status := g_ret_sts_unexp_error;
11038 
11039          IF (is_debug_exception_on)
11040          THEN
11041             okl_debug_pub.log_debug (g_level_exception,
11042                                      l_module_name,
11043                                         'EXCEPTION :'
11044                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
11045                                     );
11046          END IF;
11047       WHEN OTHERS
11048       THEN
11049          IF l_quoteobjects_csr%ISOPEN
11050          THEN
11051             CLOSE l_quoteobjects_csr;
11052          END IF;
11053 
11054          IF l_trxquotes_csr%ISOPEN
11055          THEN
11056             CLOSE l_trxquotes_csr;
11057          END IF;
11058 
11059          IF l_quotelines_csr%ISOPEN
11060          THEN
11061             CLOSE l_quotelines_csr;
11062          END IF;
11063 
11064          IF l_cashflowstreams_csr%ISOPEN
11065          THEN
11066             CLOSE l_cashflowstreams_csr;
11067          END IF;
11068 
11069          IF l_cashflowlevels_csr%ISOPEN
11070          THEN
11071             CLOSE l_cashflowlevels_csr;
11072          END IF;
11073 
11074          IF l_rgpslh_csr%ISOPEN
11075          THEN
11076             CLOSE l_rgpslh_csr;
11077          END IF;
11078 
11079          IF l_cashflows_csr%ISOPEN
11080          THEN
11081             CLOSE l_cashflows_csr;
11082          END IF;
11083 
11084          IF l_lpayments_csr%ISOPEN
11085          THEN
11086             CLOSE l_lpayments_csr;
11087          END IF;
11088 
11089          IF l_finasset_csr%ISOPEN
11090          THEN
11091             CLOSE l_finasset_csr;
11092          END IF;
11093 
11094          --Bug #3921591: pagarg +++ Rollover +++
11095          -- Changed the cursor name as made it generalised
11096          IF l_lineassets_csr%ISOPEN
11097          THEN
11098             CLOSE l_lineassets_csr;
11099          END IF;
11100 
11101          ROLLBACK TO update_payments;
11102          x_return_status := g_ret_sts_unexp_error;
11103          -- Set the oracle error message
11104          okl_api.set_message (p_app_name          => g_app_name_1,
11105                               p_msg_name          => g_unexpected_error,
11106                               p_token1            => g_sqlcode_token,
11107                               p_token1_value      => SQLCODE,
11108                               p_token2            => g_sqlerrm_token,
11109                               p_token2_value      => SQLERRM
11110                              );
11111 
11112          IF (is_debug_exception_on)
11113          THEN
11114             okl_debug_pub.log_debug (g_level_exception,
11115                                      l_module_name,
11116                                         'EXCEPTION :'
11117                                      || 'OTHERS, SQLCODE: '
11118                                      || SQLCODE
11119                                      || ' , SQLERRM : '
11120                                      || SQLERRM
11121                                     );
11122          END IF;
11123    END update_payments;
11124 
11125    -- Start of Commnets
11126    --
11127    -- Procedure Name       : update_lines
11128    -- Description          : This Procedure is Used to Update the Asset lines by
11129    --                        changing the status to TERMINATED
11130    --                        also update other lines like FEE, Sold Sevice and
11131    --                        Usage Based Billing Lines by
11132    --                        changing the status to TERMINATED, where the
11133    --                        Terminated Asset lines are referenced.
11134    --                        Also Sum the capital amount for fee sub lines and
11135    --                        Sold service Sub lines excluding the
11136    --                        terminated Asset lines to update the Top lines of FEE,
11137    --                        SOLD SERVICE lines. And further
11138    --                        pro-rate the payment amount respectively associated
11139    --                        to the top lines of FEE, SOLD SERVICE Lines.
11140 
11141    -- Business Rules       :
11142    -- Parameters           :
11143    -- Version              :1.0
11144    -- History              : 02-DEC-2002 BAKUHCIB 115.2 Bug# 2484327
11145    --                         Added code to populate Message Stack in case Failure
11146    --                         and Success of Call to an API
11147    --                      : RMUNJULU -- 04-DEC-02 Bug #  2484327
11148    --                         Moved record and tbl types and constants to spec
11149    --                         Moved the setting of message stack to terminate_lines
11150    --                         Added parameter p_trn_reason_code and setting that
11151    --                         when lines terminated, Added x_msg_tbl parameter
11152    --                         Added comments to cursors, Added code to set trn_code
11153    --                         Changed code to set savepoint instead of start/end activity
11154    --                      :  BAKUCHIB 28-MAR-03 2877278 Added code to get the klev_tbl
11155    --                         and append to it all the top service and fee lines which
11156    --                         are being terminated, this will be passed to close streams
11157    --                         which will close the streams for those lines
11158    --                      :  RMUNJULU 29-AUG-03 OKC RULES MIGRATION changes
11159    --                         Changed SLL rule to LASLL
11160    --                      :  SECHAWLA 28-OCT-03 2846988 Update payments on quote acceptance
11161    --                         Added a call to update_payments procedure
11162    --                      :  rmunjulu EDAT Added code for effective dated terminations
11163    --                      : rmunjulu LOANS_ENHANCEMENTS get and set termination value in okl_K_lines and okl_contract_balances
11164    -- End of Comments
11165    PROCEDURE update_lines (
11166       p_api_version       IN              NUMBER,
11167       p_init_msg_list     IN              VARCHAR2,
11168       x_msg_count         OUT NOCOPY      NUMBER,
11169       x_msg_data          OUT NOCOPY      VARCHAR2,
11170       x_return_status     OUT NOCOPY      VARCHAR2,
11171       p_term_rec          IN              term_rec_type,
11172       p_sys_date          IN              DATE,
11173       p_klev_tbl          IN              klev_tbl_type,
11174       p_status            IN              VARCHAR2,
11175       p_trn_reason_code   IN              VARCHAR2,
11176       x_klev_tbl          OUT NOCOPY      klev_tbl_type,
11177                                  -- BAKUCHIB 28-MAR-03 2877278 Added parameter
11178       x_msg_tbl           OUT NOCOPY      g_msg_tbl
11179    )
11180    IS
11181       -- RMUNJULU Bug #  2484327 changed parameters to IN
11182       -- We need to change the status of lines
11183       CURSOR l_trmnt_line_csr (
11184          p_cle_id   IN   okc_k_lines_b.ID%TYPE
11185       )
11186       IS
11187          SELECT     cle.ID ID
11188                FROM okc_k_lines_b cle
11189          CONNECT BY PRIOR cle.ID = cle.cle_id
11190          START WITH cle.ID = p_cle_id;
11191 
11192       -- RMUNJULU Bug #  2484327 changed parameters to IN
11193       -- We need to change the status of Fee, Sold Service and Usage
11194       -- Based Billing lines
11195       CURSOR l_get_sls_csr (
11196          p_chr_id     IN   okc_k_headers_b.ID%TYPE,
11197          p_sts_code   IN   okc_k_headers_b.sts_code%TYPE,
11198          p_cle_id     IN   okc_k_lines_b.ID%TYPE
11199       )
11200       IS
11201          SELECT cim.cle_id cle_id
11202            FROM okc_k_items cim
11203           WHERE cim.dnz_chr_id = p_chr_id
11204             AND cim.jtot_object1_code = 'OKX_COVASST'
11205             AND cim.object1_id1 IN (
11206                    SELECT cle.ID
11207                      FROM okc_k_lines_b cle,
11208                           okc_line_styles_b lse
11209                     WHERE cle.dnz_chr_id = p_chr_id
11210                       AND cle.lse_id = lse.ID
11211                       AND lse.lty_code = g_fin_line_lty_code
11212                       AND lse.lse_type = g_tls_type
11213                       AND cle.sts_code = p_sts_code
11214                       AND cle.ID = p_cle_id);
11215 
11216       -- RMUNJULU Bug #  2484327 changed parameters to IN
11217       -- We need to make sure that there are not Orphaned TOP lines of Fee,
11218       -- Sold Service and Usage Based Billing Lines
11219       CURSOR l_scan_sls_csr (
11220          p_cle_id   IN   okc_k_lines_v.ID%TYPE
11221       )
11222       IS
11223          SELECT kle.sts_code sts_code
11224            FROM okc_k_lines_b kle
11225           WHERE kle.cle_id = p_cle_id;
11226 
11227       -- RMUNJULU Bug #  2484327 changed parameters to IN
11228       -- We need to find out weahter we have fee, service, and Usage lines first
11229       -- so that we can Pro- rate and Sum up the Capital amount
11230       --and amount to the Top Lines after excluding Terminated Lines
11231       CURSOR l_chk_other_line_csr (
11232          p_chr_id   IN   okc_k_headers_b.ID%TYPE
11233       )
11234       IS
11235          SELECT '1'
11236            FROM DUAL
11237           WHERE EXISTS (
11238                    SELECT '1'
11239                      FROM okc_k_lines_b cle,
11240                           okc_line_styles_b lse
11241                     WHERE cle.dnz_chr_id = p_chr_id
11242                       AND lse.ID = cle.lse_id
11243                       AND lse.lty_code IN
11244                              (g_ser_line_lty_code,
11245                               g_srl_line_lty_code,
11246                               g_fee_line_lty_code,
11247                               g_fel_line_lty_code,
11248                               g_usg_line_lty_code,
11249                               g_usl_line_lty_code
11250                              ));
11251 
11252       -- RMUNJULU Bug #  2484327 changed parameters to IN
11253       -- We need to get the summed up amount for Sub lines of Fee and
11254       -- Sold Service Lines Excluding the terminated Lines
11255       CURSOR l_new_sls_amt_csr (
11256          p_chr_id     IN   okc_k_lines_v.dnz_chr_id%TYPE,
11257          p_sts_code   IN   okc_k_headers_b.sts_code%TYPE
11258       )
11259       IS
11260          SELECT   SUM (kle.capital_amount) amount,
11261                   cle.cle_id cle_id
11262              FROM okc_k_lines_b cle,
11263                   okl_k_lines kle,
11264                   okc_line_styles_b lse
11265             WHERE kle.ID = cle.ID
11266               AND cle.dnz_chr_id = p_chr_id
11267               AND cle.lse_id = lse.ID
11268               AND lse.lty_code IN (g_srl_line_lty_code, g_fel_line_lty_code)
11269               AND cle.sts_code <> p_sts_code
11270               AND cle.date_terminated IS NULL
11271          GROUP BY cle.cle_id;
11272 
11273       -- RMUNJULU Bug #  2484327 changed parameters to IN
11274       -- We need to get the summed up amount for Sub lines of Fee and
11275       -- Sold Service Lines, Including the terminated Lines
11276       CURSOR l_old_sls_amt_csr (
11277          p_chr_id   IN   okc_k_lines_v.dnz_chr_id%TYPE
11278       )
11279       IS
11280          SELECT   SUM (kle.capital_amount) amount,
11281                   cle.cle_id cle_id
11282              FROM okc_k_lines_b cle,
11283                   okl_k_lines kle,
11284                   okc_line_styles_b lse
11285             WHERE kle.ID = cle.ID
11286               AND cle.dnz_chr_id = p_chr_id
11287               AND cle.lse_id = lse.ID
11288               AND lse.lty_code IN (g_srl_line_lty_code, g_fel_line_lty_code)
11289               AND cle.date_terminated IS NULL
11290          GROUP BY cle.cle_id;
11291 
11292       -- RMUNJULU Bug #  2484327 changed parameters to IN
11293       -- We need to get the Payment Info for Fee and Sold Service lines
11294       CURSOR l_sls_rule_pymnt_csr (
11295          p_chr_id     IN   okc_k_lines_v.dnz_chr_id%TYPE,
11296          p_cle_id     IN   okc_k_lines_v.ID%TYPE,
11297          p_sts_code   IN   okc_k_headers_b.sts_code%TYPE
11298       )
11299       IS
11300          SELECT rl.ID ID,
11301                 rl.rule_information6 payment_amount
11302            FROM okc_rule_groups_b rg,
11303                 okc_rules_b rl,
11304                 okc_k_lines_v cle,
11305                 okc_line_styles_b lse
11306           WHERE cle.dnz_chr_id = p_chr_id
11307             AND cle.ID = p_cle_id
11308             AND cle.lse_id = lse.ID
11309             AND lse.lty_code IN (g_ser_line_lty_code, g_fee_line_lty_code)
11310             AND lse.lse_type = 'TLS'
11311             AND cle.cle_id IS NULL
11312             AND rg.dnz_chr_id = cle.dnz_chr_id
11313             AND rg.cle_id = cle.ID
11314             AND rg.chr_id IS NULL
11315             AND rg.ID = rl.rgp_id
11316             AND rg.rgd_code = 'LALEVL'
11317             AND rl.rule_information_category =
11318                               'LASLL'
11319                                      -- RMUNJULU 29-AUG-03 OKC RULES MIGRATION
11320             AND EXISTS (
11321                    SELECT '1'
11322                      FROM okc_k_lines_v cle_sl,
11323                           okc_line_styles_b lse_sl
11324                     WHERE cle_sl.dnz_chr_id = p_chr_id
11325                       AND cle_sl.cle_id = cle.ID
11326                       AND cle_sl.lse_id = lse_sl.ID
11327                       AND cle_sl.sts_code <> p_sts_code
11328                       AND lse_sl.lty_code IN
11329                                    (g_srl_line_lty_code, g_fel_line_lty_code));
11330 
11331       l_api_name     CONSTANT VARCHAR2 (30)                  := 'update_lines';
11332       i                       NUMBER                               := 0;
11333       j                       NUMBER                               := 0;
11334       ln_dummy                NUMBER                               := 0;
11335       l_module_name           VARCHAR2 (500)
11336                                             := g_module_name || 'update_lines';
11337       is_debug_exception_on   BOOLEAN
11338               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
11339       is_debug_procedure_on   BOOLEAN
11340               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
11341       is_debug_statement_on   BOOLEAN
11342               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
11343       lv_terminate_tls        VARCHAR2 (3)                         := NULL;
11344       ln_sls_payment_amount   NUMBER                               := 0;
11345       ln_new_amount           NUMBER                               := 0;
11346       ln_old_amount           NUMBER                               := 0;
11347       ln_cle_id               NUMBER                               := 0;
11348       l_chr_id                okc_k_headers_b.ID%TYPE
11349                                                    := p_term_rec.p_contract_id;
11350       l_akle_tbl              klev_tbl_type                      := p_klev_tbl;
11351       l_clev_rec              okl_okc_migration_pvt.clev_rec_type;
11352       l_klev_rec              okl_contract_pub.klev_rec_type;
11353       lx_clev_rec             okl_okc_migration_pvt.clev_rec_type;
11354       lx_klev_rec             okl_contract_pub.klev_rec_type;
11355       r_clev_rec              okl_okc_migration_pvt.clev_rec_type;
11356       r_klev_rec              okl_contract_pub.klev_rec_type;
11357       rx_clev_rec             okl_okc_migration_pvt.clev_rec_type;
11358       rx_klev_rec             okl_contract_pub.klev_rec_type;
11359       m_clev_rec              okl_okc_migration_pvt.clev_rec_type;
11360       m_klev_rec              okl_contract_pub.klev_rec_type;
11361       mx_clev_rec             okl_okc_migration_pvt.clev_rec_type;
11362       mx_klev_rec             okl_contract_pub.klev_rec_type;
11363       n_clev_rec              okl_okc_migration_pvt.clev_rec_type;
11364       n_klev_rec              okl_contract_pub.klev_rec_type;
11365       nx_clev_rec             okl_okc_migration_pvt.clev_rec_type;
11366       nx_klev_rec             okl_contract_pub.klev_rec_type;
11367       l_rulv_rec              okl_rule_pub.rulv_rec_type;
11368       lx_rulv_rec             okl_rule_pub.rulv_rec_type;
11369       r_rulv_rec              okl_rule_pub.rulv_rec_type;
11370       rx_rulv_rec             okl_rule_pub.rulv_rec_type;
11371       lt_old_sls_amt          g_cle_amt_tbl;
11372       lt_new_sls_amt          g_cle_amt_tbl;
11373       k                       NUMBER                               := 0;
11374       lt_msg_tbl              g_msg_tbl;
11375       lv_id1                  okc_k_items.object1_id1%TYPE;
11376       lv_id2                  okc_k_items.object1_id2%TYPE;
11377       lv_code                 okc_k_items.jtot_object1_code%TYPE;
11378       -- SECHAWLA 28-OCT-03 2846988 : New declarations
11379       l_return_status         VARCHAR2 (1)        := okl_api.g_ret_sts_success;
11380 
11381       -- RMUNJULU Bug #  2484327 Added comments, changed parameters to IN
11382       -- We need to find out the object1_id1 and object1_id2 and jtot_object1_code
11383       -- to get the name of the fee or service or ubb line based on jtot_object1_code
11384       CURSOR l_item_line_csr (
11385          p_cle_id       IN   okc_k_lines_b.ID%TYPE,
11386          p_dnz_chr_id   IN   okc_k_lines_b.dnz_chr_id%TYPE
11387       )
11388       IS
11389          SELECT cim.object1_id1 object1_id1,
11390                 cim.object1_id2 object1_id2,
11391                 cim.jtot_object1_code jtot_object1_code
11392            FROM okc_k_items cim
11393           WHERE cim.dnz_chr_id = p_dnz_chr_id AND cim.cle_id = p_cle_id;
11394 
11395       -- RMUNJULU Bug #  2484327 Added comments, changed parameters to IN
11396       -- We need to get the name of the service line
11397       CURSOR l_service_line_csr (
11398          p_id1   IN   okc_k_items.object1_id1%TYPE,
11399          p_id2   IN   okc_k_items.object1_id2%TYPE
11400       )
11401       IS
11402          SELECT siv.NAME NAME
11403            FROM okx_system_items_v siv
11404           WHERE siv.id1 = p_id1 AND siv.id2 = p_id2;
11405 
11406       -- RMUNJULU Bug #  2484327 Added comments, changed parameters to IN
11407       -- We need to get the name of the fee line
11408       CURSOR l_fee_line_csr (
11409          p_id1   IN   okc_k_items.object1_id1%TYPE,
11410          p_id2   IN   okc_k_items.object1_id2%TYPE
11411       )
11412       IS
11413          SELECT ssv.NAME NAME
11414            FROM okl_strmtyp_source_v ssv
11415           WHERE ssv.id1 = p_id1 AND ssv.id2 = p_id2;
11416 
11417       -- RMUNJULU Bug #  2484327 Added comments, changed parameters to IN
11418       -- We need to get the name of the UBB line
11419       CURSOR l_usage_line_csr (
11420          p_id1   IN   okc_k_items.object1_id1%TYPE,
11421          p_id2   IN   okc_k_items.object1_id2%TYPE
11422       )
11423       IS
11424          SELECT siv.NAME NAME
11425            FROM okx_system_items_v siv
11426           WHERE siv.id1 = p_id1 AND siv.id2 = p_id2;
11427 
11428       -- BAKUCHIB 28-MAR-03 2877278 Added variable
11429       lx_klev_tbl             klev_tbl_type                      := l_akle_tbl;
11430       -- rmunjulu EDAT
11431       l_quote_accpt_date      DATE;
11432       l_quote_eff_date        DATE;
11433 
11434       -- rmunjulu LOANS_ENHACEMENTS
11435       CURSOR check_balances_rec_exists_csr (
11436          p_kle_id   IN   NUMBER
11437       )
11438       IS
11439          SELECT 'Y'
11440            FROM okl_contract_balances
11441           WHERE kle_id = p_kle_id;
11442 
11443       -- rmunjulu LOANS_ENHACEMENTS
11444       l_termination_value     NUMBER;
11445       lap_clev_rec            okl_okc_migration_pvt.clev_rec_type;
11446       lap_klev_rec            okl_contract_pub.klev_rec_type;
11447       lax_clev_rec            okl_okc_migration_pvt.clev_rec_type;
11448       lax_klev_rec            okl_contract_pub.klev_rec_type;
11449       p_cblv_rec              okl_cbl_pvt.cblv_rec_type;
11450       x_cblv_rec              okl_cbl_pvt.cblv_rec_type;
11451       l_empty_clev_rec        okl_okc_migration_pvt.clev_rec_type;
11452       l_empty_klev_rec        okl_contract_pub.klev_rec_type;
11453       l_balances_rec_exists   VARCHAR2 (3);
11454    BEGIN
11455       -- RMUNJULU Bug # 2484327, added code to set savepoint
11456       -- Start a savepoint to rollback to if error in this block
11457       SAVEPOINT update_lines;
11458 
11459       IF (is_debug_procedure_on)
11460       THEN
11461          okl_debug_pub.log_debug (g_level_procedure,
11462                                   l_module_name,
11463                                   'Begin(+)'
11464                                  );
11465       END IF;
11466 
11467       IF (is_debug_statement_on)
11468       THEN
11469          okl_debug_pub.log_debug (g_level_statement,
11470                                   l_module_name,
11471                                   'In param, p_sys_date: ' || p_sys_date
11472                                  );
11473          okl_debug_pub.log_debug (g_level_statement,
11474                                   l_module_name,
11475                                   'In param, p_status: ' || p_status
11476                                  );
11477          okl_debug_pub.log_debug (g_level_statement,
11478                                   l_module_name,
11479                                      'In param, p_trn_reason_code: '
11480                                   || p_trn_reason_code
11481                                  );
11482          okl_debug_pub.log_debug (g_level_statement,
11483                                   l_module_name,
11484                                      'In param, p_term_rec.p_quote_id: '
11485                                   || p_term_rec.p_quote_id
11486                                  );
11487          okl_debug_pub.log_debug (g_level_statement,
11488                                   l_module_name,
11489                                      'In param, p_term_rec.p_contract_id: '
11490                                   || p_term_rec.p_contract_id
11491                                  );
11492 
11493          IF p_klev_tbl.COUNT > 0
11494          THEN
11495             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
11496             LOOP
11497                IF (p_klev_tbl.EXISTS (i))
11498                THEN
11499                   okl_debug_pub.log_debug (g_level_statement,
11500                                            l_module_name,
11501                                               'In param, p_klev_tbl('
11502                                            || i
11503                                            || ').p_kle_id: '
11504                                            || p_klev_tbl (i).p_kle_id
11505                                           );
11506                   okl_debug_pub.log_debug (g_level_statement,
11507                                            l_module_name,
11508                                               'In param, p_klev_tbl('
11509                                            || i
11510                                            || ').p_asset_name: '
11511                                            || p_klev_tbl (i).p_asset_name
11512                                           );
11513                END IF;
11514             END LOOP;
11515          END IF;
11516       END IF;
11517 
11518       x_return_status := g_ret_sts_success;
11519 
11520       -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
11521 
11522       -- rmunjulu EDAT
11523       -- If quote exists then accnting date is quote accept date else sysdate
11524       IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
11525       THEN
11526          l_quote_accpt_date :=
11527                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
11528          l_quote_eff_date :=
11529                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
11530       ELSE
11531          l_quote_accpt_date := p_sys_date;
11532          l_quote_eff_date := p_sys_date;
11533       END IF;
11534 
11535       -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
11536 
11537       -- Summing up amount of all the sub lines populating Amount and Capital amount
11538       FOR r_old_sls_amt_csr IN l_old_sls_amt_csr (p_chr_id      => l_chr_id)
11539       LOOP
11540          lt_old_sls_amt (j).amount := r_old_sls_amt_csr.amount;
11541          lt_old_sls_amt (j).cle_id := r_old_sls_amt_csr.cle_id;
11542          j := j + 1;
11543       END LOOP;
11544 
11545       -- Verify the Lines and Contract id
11546       IF l_akle_tbl.COUNT > 0
11547       THEN
11548          i := l_akle_tbl.FIRST;
11549 
11550          LOOP
11551             validate_chr_cle_id (p_dnz_chr_id         => l_chr_id,
11552                                  p_top_line_id        => l_akle_tbl (i).p_kle_id,
11553                                  x_return_status      => x_return_status
11554                                 );
11555 
11556             IF (is_debug_statement_on)
11557             THEN
11558                okl_debug_pub.log_debug
11559                            (g_level_statement,
11560                             l_module_name,
11561                                'called validate_chr_cle_id , return status: '
11562                             || x_return_status
11563                            );
11564             END IF;
11565 
11566             IF (x_return_status = g_ret_sts_unexp_error)
11567             THEN
11568                EXIT WHEN (x_return_status = g_ret_sts_unexp_error);
11569             ELSIF (x_return_status = g_ret_sts_error)
11570             THEN
11571                EXIT WHEN (x_return_status = g_ret_sts_error);
11572             END IF;
11573 
11574             -- Now we Change the status of the Lines
11575             FOR r_trmnt_line_csr IN
11576                l_trmnt_line_csr (p_cle_id      => l_akle_tbl (i).p_kle_id)
11577             LOOP
11578                l_clev_rec.ID := r_trmnt_line_csr.ID;
11579                l_klev_rec.ID := r_trmnt_line_csr.ID;
11580                l_clev_rec.date_terminated := l_quote_eff_date;
11581                                                              -- rmunjulu EDAT
11582                l_clev_rec.sts_code := p_status;
11583                -- RMUNJULU -- Bug # 2484327 -- Added code to set trn_code
11584                l_clev_rec.trn_code := p_trn_reason_code;
11585 
11586                IF (is_debug_statement_on)
11587                THEN
11588                   okl_debug_pub.log_debug
11589                              (g_level_statement,
11590                               l_module_name,
11591                               'calling OKL_CONTRACT_PUB.update_contract_line'
11592                              );
11593                END IF;
11594 
11595                okl_contract_pub.update_contract_line
11596                                           (p_api_version        => p_api_version,
11597                                            p_init_msg_list      => g_false,
11598                                            x_return_status      => x_return_status,
11599                                            x_msg_count          => x_msg_count,
11600                                            x_msg_data           => x_msg_data,
11601                                            p_clev_rec           => l_clev_rec,
11602                                            p_klev_rec           => l_klev_rec,
11603                                            x_clev_rec           => lx_clev_rec,
11604                                            x_klev_rec           => lx_klev_rec
11605                                           );
11606 
11607                IF (is_debug_statement_on)
11608                THEN
11609                   okl_debug_pub.log_debug
11610                      (g_level_statement,
11611                       l_module_name,
11612                          'calling OKL_CONTRACT_PUB.update_contract_line , return status: '
11613                       || x_return_status
11614                      );
11615                END IF;
11616 
11617                IF x_return_status <> g_ret_sts_success
11618                THEN
11619                   -- Error in terminating asset ASSET_NAME.
11620                   okl_api.set_message
11621                                 (p_app_name          => g_app_name,
11622                                  p_msg_name          => g_am_err_trmt_asset,
11623                                  p_token1            => 'ASSET_NAME',
11624                                  p_token1_value      => l_akle_tbl (i).p_asset_name
11625                                 );
11626                ELSIF x_return_status = g_ret_sts_success
11627                THEN
11628                   lt_msg_tbl (k).msg_desc := g_am_asset_trmt;
11629                   lt_msg_tbl (k).msg_token1 := 'ASSET_NUMBER';
11630                   lt_msg_tbl (k).msg_token1_value :=
11631                                                   l_akle_tbl (i).p_asset_name;
11632                END IF;
11633 
11634                IF (x_return_status = g_ret_sts_unexp_error)
11635                THEN
11636                   EXIT WHEN (x_return_status = g_ret_sts_unexp_error);
11637                ELSIF (x_return_status = g_ret_sts_error)
11638                THEN
11639                   EXIT WHEN (x_return_status = g_ret_sts_error);
11640                END IF;
11641             END LOOP;
11642 
11643             IF (x_return_status = g_ret_sts_unexp_error)
11644             THEN
11645                EXIT WHEN (x_return_status = g_ret_sts_unexp_error);
11646             ELSIF (x_return_status = g_ret_sts_error)
11647             THEN
11648                EXIT WHEN (x_return_status = g_ret_sts_error);
11649             END IF;
11650 
11651             --Now we are terminating the Service Line, Fee line, Ubb Lines,
11652             -- if exists, Where the Asset lines are terminated
11653             FOR r_get_sls_csr IN
11654                l_get_sls_csr (p_chr_id        => l_chr_id,
11655                               p_sts_code      => p_status,
11656                               p_cle_id        => l_akle_tbl (i).p_kle_id
11657                              )
11658             LOOP
11659                r_clev_rec.ID := r_get_sls_csr.cle_id;
11660                r_klev_rec.ID := r_get_sls_csr.cle_id;
11661                r_clev_rec.date_terminated := l_quote_eff_date;
11662                                                              -- rmunjulu EDAT
11663                r_clev_rec.sts_code := p_status;
11664                -- RMUNJULU -- Bug # 2484327 -- Added code to set trn_code
11665                r_clev_rec.trn_code := p_trn_reason_code;
11666 
11667                IF (is_debug_statement_on)
11668                THEN
11669                   okl_debug_pub.log_debug
11670                              (g_level_statement,
11671                               l_module_name,
11672                               'calling OKL_CONTRACT_PUB.update_contract_line'
11673                              );
11674                END IF;
11675 
11676                okl_contract_pub.update_contract_line
11677                                           (p_api_version        => p_api_version,
11678                                            p_init_msg_list      => g_false,
11679                                            x_return_status      => x_return_status,
11680                                            x_msg_count          => x_msg_count,
11681                                            x_msg_data           => x_msg_data,
11682                                            p_clev_rec           => r_clev_rec,
11683                                            p_klev_rec           => r_klev_rec,
11684                                            x_clev_rec           => rx_clev_rec,
11685                                            x_klev_rec           => rx_klev_rec
11686                                           );
11687 
11688                IF (is_debug_statement_on)
11689                THEN
11690                   okl_debug_pub.log_debug
11691                      (g_level_statement,
11692                       l_module_name,
11693                          'called OKL_CONTRACT_PUB.update_contract_line , return status: '
11694                       || x_return_status
11695                      );
11696                END IF;
11697 
11698                IF x_return_status <> g_ret_sts_success
11699                THEN
11700                   -- Error in terminating sublines of service lines, fee lines and
11701                   -- UBB lines of asset ASSET_NAME.
11702                   okl_api.set_message
11703                                 (p_app_name          => g_app_name,
11704                                  p_msg_name          => g_am_err_trmt_asset_ln,
11705                                  p_token1            => 'ASSET_NAME',
11706                                  p_token1_value      => l_akle_tbl (i).p_asset_name
11707                                 );
11708                END IF;
11709 
11710                IF (x_return_status = g_ret_sts_unexp_error)
11711                THEN
11712                   EXIT WHEN (x_return_status = g_ret_sts_unexp_error);
11713                ELSIF (x_return_status = g_ret_sts_error)
11714                THEN
11715                   EXIT WHEN (x_return_status = g_ret_sts_error);
11716                END IF;
11717 
11718                -- Now need to make sure we do not have orphaned Top Lines
11719                -- Where the sub lines are terminated.
11720                -- If we have we need to terminate the Top Line also.
11721                FOR r_l_scan_sls_csr IN
11722                   l_scan_sls_csr (p_cle_id      => rx_clev_rec.cle_id)
11723                LOOP
11724                   IF r_l_scan_sls_csr.sts_code <> p_status
11725                   THEN
11726                      lv_terminate_tls := 'N';
11727                      EXIT WHEN (lv_terminate_tls = 'N');
11728                   ELSIF r_l_scan_sls_csr.sts_code = p_status
11729                   THEN
11730                      lv_terminate_tls := 'Y';
11731                   END IF;
11732                END LOOP;
11733 
11734                IF lv_terminate_tls = 'Y'
11735                THEN
11736                   IF    rx_clev_rec.cle_id <> g_miss_num
11737                      OR rx_clev_rec.cle_id IS NOT NULL
11738                   THEN
11739                      m_clev_rec.ID := rx_clev_rec.cle_id;
11740                      m_klev_rec.ID := rx_clev_rec.cle_id;
11741                      m_clev_rec.date_terminated := l_quote_eff_date;
11742                                                              -- rmunjulu EDAT
11743                      m_clev_rec.sts_code := p_status;
11744                      -- RMUNJULU -- Bug # 2484327 -- Added code to set trn_code
11745                      m_clev_rec.trn_code := p_trn_reason_code;
11746 
11747                      IF (is_debug_statement_on)
11748                      THEN
11749                         okl_debug_pub.log_debug
11750                              (g_level_statement,
11751                               l_module_name,
11752                               'calling OKL_CONTRACT_PUB.update_contract_line'
11753                              );
11754                      END IF;
11755 
11756                      okl_contract_pub.update_contract_line
11757                                           (p_api_version        => p_api_version,
11758                                            p_init_msg_list      => g_false,
11759                                            x_return_status      => x_return_status,
11760                                            x_msg_count          => x_msg_count,
11761                                            x_msg_data           => x_msg_data,
11762                                            p_clev_rec           => m_clev_rec,
11763                                            p_klev_rec           => m_klev_rec,
11764                                            x_clev_rec           => mx_clev_rec,
11765                                            x_klev_rec           => mx_klev_rec
11766                                           );
11767 
11768                      IF (is_debug_statement_on)
11769                      THEN
11770                         okl_debug_pub.log_debug
11771                            (g_level_statement,
11772                             l_module_name,
11773                                'called OKL_CONTRACT_PUB.update_contract_line , return status: '
11774                             || x_return_status
11775                            );
11776                      END IF;
11777 
11778                      IF x_return_status <> g_ret_sts_success
11779                      THEN
11780                         -- Error in terminating service lines, fee lines and UBB lines.
11781                         okl_api.set_message
11782                                           (p_app_name      => g_app_name,
11783                                            p_msg_name      => g_am_err_trmt_top_ln);
11784                      ELSIF x_return_status = g_ret_sts_success
11785                      THEN
11786                         OPEN l_item_line_csr
11787                                       (p_cle_id          => mx_clev_rec.ID,
11788                                        p_dnz_chr_id      => mx_clev_rec.dnz_chr_id);
11789 
11790                         FETCH l_item_line_csr
11791                          INTO lv_id1,
11792                               lv_id2,
11793                               lv_code;
11794 
11795                         CLOSE l_item_line_csr;
11796 
11797                         IF lv_code = 'OKL_STRMTYP'
11798                         THEN
11799                            k := k + 1;
11800 
11801                            OPEN l_fee_line_csr (p_id1      => lv_id1,
11802                                                 p_id2      => lv_id2);
11803 
11804                            FETCH l_fee_line_csr
11805                             INTO lt_msg_tbl (k).msg_token1_value;
11806 
11807                            CLOSE l_fee_line_csr;
11808 
11809                            lt_msg_tbl (k).msg_desc := g_am_fee_trmt;
11810                            lt_msg_tbl (k).msg_token1 := 'FEE_NAME';
11811                         ELSIF lv_code = 'OKX_SERVICE'
11812                         THEN
11813                            k := k + 1;
11814 
11815                            OPEN l_service_line_csr (p_id1      => lv_id1,
11816                                                     p_id2      => lv_id2);
11817 
11818                            FETCH l_service_line_csr
11819                             INTO lt_msg_tbl (k).msg_token1_value;
11820 
11821                            CLOSE l_service_line_csr;
11822 
11823                            lt_msg_tbl (k).msg_desc := g_am_service_trmt;
11824                            lt_msg_tbl (k).msg_token1 := 'SERVICE_NAME';
11825                         ELSIF lv_code = 'OKX_USAGE'
11826                         THEN
11827                            k := k + 1;
11828 
11829                            OPEN l_usage_line_csr (p_id1      => lv_id1,
11830                                                   p_id2      => lv_id2);
11831 
11832                            FETCH l_usage_line_csr
11833                             INTO lt_msg_tbl (k).msg_token1_value;
11834 
11835                            CLOSE l_usage_line_csr;
11836 
11837                            lt_msg_tbl (k).msg_desc := g_am_usage_trmt;
11838                            lt_msg_tbl (k).msg_token1 := 'USAGE_NAME';
11839                         END IF;
11840                      END IF;
11841 
11842                      -- BAKUCHIB 28-MAR-03 2877278 Append to the klev_tbl this top service/fee
11843                      -- line which too is being terminated
11844                      lx_klev_tbl (lx_klev_tbl.LAST + 1).p_kle_id :=
11845                                                                 mx_clev_rec.ID;
11846 
11847                      IF (x_return_status = g_ret_sts_unexp_error)
11848                      THEN
11849                         EXIT WHEN (x_return_status = g_ret_sts_unexp_error);
11850                      ELSIF (x_return_status = g_ret_sts_error)
11851                      THEN
11852                         EXIT WHEN (x_return_status = g_ret_sts_error);
11853                      END IF;
11854                   ELSE
11855                      x_return_status := g_ret_sts_error;
11856                      EXIT WHEN (x_return_status = g_ret_sts_error);
11857                   END IF;
11858                END IF;
11859             END LOOP;
11860 
11861             IF (x_return_status = g_ret_sts_unexp_error)
11862             THEN
11863                EXIT WHEN (x_return_status = g_ret_sts_unexp_error);
11864             ELSIF (x_return_status = g_ret_sts_error)
11865             THEN
11866                EXIT WHEN (x_return_status = g_ret_sts_error);
11867             END IF;
11868 
11869             -- rmunjulu LOANS_ENHACEMENTS -- start
11870             -- get termination value for the terminated asset
11871             l_termination_value :=
11872                okl_am_util_pvt.get_actual_asset_residual
11873                                            (p_khr_id      => l_chr_id,
11874                                             p_kle_id      => l_akle_tbl (i).p_kle_id);
11875             -- update okc_k_lines termination value
11876             lap_clev_rec := l_empty_clev_rec;
11877             lap_klev_rec := l_empty_klev_rec;
11878             lap_clev_rec.ID := l_akle_tbl (i).p_kle_id;
11879             lap_klev_rec.ID := l_akle_tbl (i).p_kle_id;
11880             lap_klev_rec.termination_value := l_termination_value;
11881 
11882             IF (is_debug_statement_on)
11883             THEN
11884                okl_debug_pub.log_debug
11885                              (g_level_statement,
11886                               l_module_name,
11887                               'calling OKL_CONTRACT_PUB.update_contract_line'
11888                              );
11889             END IF;
11890 
11891             okl_contract_pub.update_contract_line
11892                                           (p_api_version        => p_api_version,
11893                                            p_init_msg_list      => g_false,
11894                                            x_return_status      => l_return_status,
11895                                            x_msg_count          => x_msg_count,
11896                                            x_msg_data           => x_msg_data,
11897                                            p_clev_rec           => lap_clev_rec,
11898                                            p_klev_rec           => lap_klev_rec,
11899                                            x_clev_rec           => lax_clev_rec,
11900                                            x_klev_rec           => lax_klev_rec
11901                                           );
11902 
11903             IF (is_debug_statement_on)
11904             THEN
11905                okl_debug_pub.log_debug
11906                   (g_level_statement,
11907                    l_module_name,
11908                       'called OKL_CONTRACT_PUB.update_contract_line , return status: '
11909                    || l_return_status
11910                   );
11911             END IF;
11912 
11913             -- Raise exception to rollback to savepoint
11914             IF (l_return_status = g_ret_sts_unexp_error)
11915             THEN
11916                RAISE okl_api.g_exception_unexpected_error;
11917             ELSIF (l_return_status = g_ret_sts_error)
11918             THEN
11919                RAISE okl_api.g_exception_error;
11920             END IF;
11921 
11922             -- update balances table
11923             -- check if rec exists if exists then update else create
11924             OPEN check_balances_rec_exists_csr (l_akle_tbl (i).p_kle_id);
11925 
11926             FETCH check_balances_rec_exists_csr
11927              INTO l_balances_rec_exists;
11928 
11929             CLOSE check_balances_rec_exists_csr;
11930 
11931             p_cblv_rec.khr_id := l_chr_id;
11932             p_cblv_rec.kle_id := l_akle_tbl (i).p_kle_id;
11933             p_cblv_rec.termination_value_amt := l_termination_value;
11934             p_cblv_rec.termination_date := l_quote_eff_date;
11935 
11936             IF NVL (l_balances_rec_exists, 'N') = 'Y'
11937             THEN
11938                IF (is_debug_statement_on)
11939                THEN
11940                   okl_debug_pub.log_debug (g_level_statement,
11941                                            l_module_name,
11942                                            'calling OKL_CBL_PVT.update_row'
11943                                           );
11944                END IF;
11945 
11946                okl_cbl_pvt.update_row (p_api_version        => p_api_version,
11947                                        p_init_msg_list      => okl_api.g_false,
11948                                        x_return_status      => x_return_status,
11949                                        x_msg_count          => x_msg_count,
11950                                        x_msg_data           => x_msg_data,
11951                                        p_cblv_rec           => p_cblv_rec,
11952                                        x_cblv_rec           => x_cblv_rec
11953                                       );
11954 
11955                IF (is_debug_statement_on)
11956                THEN
11957                   okl_debug_pub.log_debug
11958                         (g_level_statement,
11959                          l_module_name,
11960                             'called OKL_CBL_PVT.update_row , return status: '
11961                          || x_return_status
11962                         );
11963                END IF;
11964 
11965                -- Raise exception to rollback to savepoint
11966                IF (l_return_status = g_ret_sts_unexp_error)
11967                THEN
11968                   RAISE okl_api.g_exception_unexpected_error;
11969                ELSIF (l_return_status = g_ret_sts_error)
11970                THEN
11971                   RAISE okl_api.g_exception_error;
11972                END IF;
11973             ELSE
11974                -- balances rec does not exist so insert
11975                IF (is_debug_statement_on)
11976                THEN
11977                   okl_debug_pub.log_debug (g_level_statement,
11978                                            l_module_name,
11979                                            'calling OKL_CBL_PVT.insert_row'
11980                                           );
11981                END IF;
11982 
11983                okl_cbl_pvt.insert_row (p_api_version        => p_api_version,
11984                                        p_init_msg_list      => okl_api.g_false,
11985                                        x_return_status      => x_return_status,
11986                                        x_msg_count          => x_msg_count,
11987                                        x_msg_data           => x_msg_data,
11988                                        p_cblv_rec           => p_cblv_rec,
11989                                        x_cblv_rec           => x_cblv_rec
11990                                       );
11991 
11992                IF (is_debug_statement_on)
11993                THEN
11994                   okl_debug_pub.log_debug
11995                         (g_level_statement,
11996                          l_module_name,
11997                             'called OKL_CBL_PVT.insert_row , return status: '
11998                          || x_return_status
11999                         );
12000                END IF;
12001 
12002                -- Raise exception to rollback to savepoint
12003                IF (l_return_status = g_ret_sts_unexp_error)
12004                THEN
12005                   RAISE okl_api.g_exception_unexpected_error;
12006                ELSIF (l_return_status = g_ret_sts_error)
12007                THEN
12008                   RAISE okl_api.g_exception_error;
12009                END IF;
12010             END IF;
12011 
12012             -- rmunjulu LOANS_ENHACEMENTS -- end
12013             EXIT WHEN (i = l_akle_tbl.LAST);
12014             i := l_akle_tbl.NEXT (i);
12015             k := k + 1;
12016          END LOOP;
12017 
12018          IF (x_return_status = g_ret_sts_unexp_error)
12019          THEN
12020             RAISE okl_api.g_exception_unexpected_error;
12021          ELSIF (x_return_status = g_ret_sts_error)
12022          THEN
12023             RAISE okl_api.g_exception_error;
12024          END IF;
12025       ELSE
12026          -- RMUNJULU -- Bug # 2484327 -- Changed to call set_message with =>
12027          -- No assets found for termination.
12028          okl_api.set_message (p_app_name          => g_app_name,
12029                               p_msg_name          => g_required_value,
12030                               p_token1            => g_col_name_token,
12031                               p_token1_value      => 'p_klev_tbl.id'
12032                              );
12033          RAISE okl_api.g_exception_error;
12034       END IF;
12035 
12036       --29-JUL-04 SECHAWLA 3798158 : Commented out the check for existence of service/fee/usage lines
12037       -- Payments should be updated even if K has no service/fee/usage lines attached
12038 
12039       /*
12040       -- We need to find out whether we have fee, service, and Usage lines first
12041       -- so that we can process further
12042       OPEN  l_chk_other_line_csr(p_chr_id => l_chr_id);
12043       FETCH l_chk_other_line_csr INTO ln_dummy;
12044       CLOSE l_chk_other_line_csr;
12045 
12046       IF ln_dummy = 1 THEN
12047       */
12048       -- SECHAWLA 28-OCT-03 2846988  Added the following procedure call to update payments on the
12049       -- contract, when a partial termination quote is accepted
12050       update_payments (p_api_version        => p_api_version,
12051                        p_init_msg_list      => okc_api.g_false,
12052                        x_msg_count          => x_msg_count,
12053                        x_msg_data           => x_msg_data,
12054                        x_return_status      => l_return_status,
12055                        p_quote_id           => p_term_rec.p_quote_id
12056                       );
12057 
12058       IF (is_debug_statement_on)
12059       THEN
12060          okl_debug_pub.log_debug
12061                                (g_level_statement,
12062                                 l_module_name,
12063                                    'called update_payments , return status: '
12064                                 || l_return_status
12065                                );
12066       END IF;
12067 
12068       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
12069       THEN
12070          RAISE okl_api.g_exception_unexpected_error;
12071       ELSIF (l_return_status = okl_api.g_ret_sts_error)
12072       THEN
12073          RAISE okl_api.g_exception_error;
12074       END IF;
12075 
12076       -- SECHAWLA 28-OCT-03 2846988 : Commented out the following code. The above call now updates the payments
12077 
12078       /*  -- Summing up amount of all the sub lines which are not
12079       -- Terminated populating Amount and Capital amount
12080       FOR r_new_sls_amt_csr IN l_new_sls_amt_csr(p_chr_id   => l_chr_id,
12081                                                  p_sts_code => p_status) LOOP
12082 
12083         ln_new_amount             := ROUND(r_new_sls_amt_csr.amount,2);
12084         n_klev_rec.amount         := ROUND(r_new_sls_amt_csr.amount,2);
12085         n_klev_rec.capital_amount := ROUND(r_new_sls_amt_csr.amount,2);
12086         n_klev_rec.id             := r_new_sls_amt_csr.cle_id;
12087         n_clev_rec.id             := r_new_sls_amt_csr.cle_id;
12088 
12089         OKL_CONTRACT_PUB.update_contract_line(
12090                          p_api_version   => p_api_version,
12091                          p_init_msg_list => G_FALSE,
12092                          x_return_status => x_return_status,
12093                          x_msg_count     => x_msg_count,
12094                          x_msg_data      => x_msg_data,
12095                          p_clev_rec      => n_clev_rec,
12096                          p_klev_rec      => n_klev_rec,
12097                          x_clev_rec      => nx_clev_rec,
12098                          x_klev_rec      => nx_klev_rec);
12099 
12100         IF x_return_status <> G_RET_STS_SUCCESS THEN
12101 
12102             -- Error in updating amounts of service and fee lines.
12103             OKL_API.set_message(
12104                           p_app_name      => G_APP_NAME,
12105                           p_msg_name      => G_AM_ERR_UPD_AMT);
12106 
12107 
12108         END IF;
12109 
12110         IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
12111           EXIT WHEN (x_return_status = G_RET_STS_ERROR);
12112         ELSIF (x_return_status = G_RET_STS_ERROR) THEN
12113           EXIT WHEN (x_return_status = G_RET_STS_ERROR);
12114         END IF;
12115 
12116         -- We need to pro-rate the payment amount of top service
12117         -- or fee line having Sub lines
12118         FOR r_sls_rule_pymnt_csr IN l_sls_rule_pymnt_csr(
12119                                                   p_chr_id => l_chr_id,
12120                                                   p_cle_id => nx_clev_rec.id,
12121                                                   p_sts_code => p_status) LOOP
12122 
12123           IF lt_old_sls_amt.COUNT > 0 THEN
12124             j := lt_old_sls_amt.FIRST;
12125             LOOP
12126               IF lt_old_sls_amt(j).cle_id = nx_clev_rec.id THEN
12127                 ln_old_amount := lt_old_sls_amt(j).amount;
12128                 EXIT WHEN (lt_old_sls_amt(j).cle_id = nx_clev_rec.id);
12129               END IF;
12130               EXIT WHEN (j = lt_old_sls_amt.LAST);
12131               j := lt_old_sls_amt.NEXT(j);
12132             END LOOP;
12133           END IF;
12134 
12135           IF (ln_old_amount IS NOT NULL OR
12136              ln_old_amount <> G_MISS_NUM) AND
12137              (ln_new_amount IS NOT NULL OR
12138              ln_new_amount <> G_MISS_NUM) THEN
12139              IF ln_old_amount <> 0 THEN
12140                ln_sls_payment_amount := r_sls_rule_pymnt_csr.payment_amount *
12141                                         ln_new_amount/ln_old_amount;
12142                r_rulv_rec.rule_information6 := round(ln_sls_payment_amount,2);
12143              END IF;
12144           END IF;
12145 
12146           r_rulv_rec.id := r_sls_rule_pymnt_csr.id;
12147 
12148           OKL_RULE_PUB.update_rule(
12149                        p_api_version   => p_api_version,
12150                        p_init_msg_list => G_FALSE,
12151                        x_return_status => x_return_status,
12152                        x_msg_count     => x_msg_count,
12153                        x_msg_data      => x_msg_data,
12154                        p_rulv_rec      => r_rulv_rec,
12155                        x_rulv_rec      => rx_rulv_rec);
12156 
12157 
12158           IF x_return_status <> G_RET_STS_SUCCESS THEN
12159 
12160               -- Error in updating payment amounts of service and fee lines.
12161               OKL_API.set_message(
12162                           p_app_name      => G_APP_NAME,
12163                           p_msg_name      => G_AM_ERR_UPD_PAY_AMT);
12164 
12165           END IF;
12166 
12167 
12168 
12169           IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
12170             EXIT WHEN (x_return_status = G_RET_STS_ERROR);
12171           ELSIF (x_return_status = G_RET_STS_ERROR) THEN
12172             EXIT WHEN (x_return_status = G_RET_STS_ERROR);
12173           END IF;
12174 
12175         END LOOP;
12176 
12177         IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
12178           EXIT WHEN (x_return_status = G_RET_STS_ERROR);
12179         ELSIF (x_return_status = G_RET_STS_ERROR) THEN
12180           EXIT WHEN (x_return_status = G_RET_STS_ERROR);
12181         END IF;
12182 
12183       END LOOP;
12184 
12185       IF (x_return_status = G_RET_STS_UNEXP_ERROR) THEN
12186         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
12187       ELSIF (x_return_status = G_RET_STS_ERROR) THEN
12188         RAISE OKL_API.G_EXCEPTION_ERROR;
12189       END IF;
12190       */
12191 
12192       --  END IF; --29-JUL-04 SECHAWLA 3798158
12193 
12194       -- RMUNJULU -- 04-DEC-02 Bug # 2484327-- Set the x_msg_tbl
12195       x_msg_tbl := lt_msg_tbl;
12196       -- BAKUCHIB 28-MAR-03 2877278 Set new klev_tbl which has the top service and fee
12197       -- lines which too needs to be terminated to x_klev_tbl
12198       x_klev_tbl := lx_klev_tbl;
12199 
12200       IF (is_debug_procedure_on)
12201       THEN
12202          okl_debug_pub.log_debug (g_level_procedure,
12203                                   l_module_name,
12204                                   'End(-)'
12205                                  );
12206       END IF;
12207    EXCEPTION
12208       WHEN okl_api.g_exception_error
12209       THEN
12210          IF l_trmnt_line_csr%ISOPEN
12211          THEN
12212             CLOSE l_trmnt_line_csr;
12213          END IF;
12214 
12215          IF l_chk_other_line_csr%ISOPEN
12216          THEN
12217             CLOSE l_chk_other_line_csr;
12218          END IF;
12219 
12220          IF l_get_sls_csr%ISOPEN
12221          THEN
12222             CLOSE l_get_sls_csr;
12223          END IF;
12224 
12225          IF l_scan_sls_csr%ISOPEN
12226          THEN
12227             CLOSE l_scan_sls_csr;
12228          END IF;
12229 
12230          IF l_new_sls_amt_csr%ISOPEN
12231          THEN
12232             CLOSE l_new_sls_amt_csr;
12233          END IF;
12234 
12235          IF l_old_sls_amt_csr%ISOPEN
12236          THEN
12237             CLOSE l_old_sls_amt_csr;
12238          END IF;
12239 
12240          IF l_sls_rule_pymnt_csr%ISOPEN
12241          THEN
12242             CLOSE l_sls_rule_pymnt_csr;
12243          END IF;
12244 
12245          IF l_item_line_csr%ISOPEN
12246          THEN
12247             CLOSE l_item_line_csr;
12248          END IF;
12249 
12250          IF l_service_line_csr%ISOPEN
12251          THEN
12252             CLOSE l_service_line_csr;
12253          END IF;
12254 
12255          IF l_fee_line_csr%ISOPEN
12256          THEN
12257             CLOSE l_fee_line_csr;
12258          END IF;
12259 
12260          IF l_usage_line_csr%ISOPEN
12261          THEN
12262             CLOSE l_usage_line_csr;
12263          END IF;
12264 
12265          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
12266          -- return status
12267          ROLLBACK TO update_lines;
12268          x_return_status := g_ret_sts_error;
12269 
12270          IF (is_debug_exception_on)
12271          THEN
12272             okl_debug_pub.log_debug (g_level_exception,
12273                                      l_module_name,
12274                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
12275                                     );
12276          END IF;
12277       WHEN okl_api.g_exception_unexpected_error
12278       THEN
12279          IF l_trmnt_line_csr%ISOPEN
12280          THEN
12281             CLOSE l_trmnt_line_csr;
12282          END IF;
12283 
12284          IF l_chk_other_line_csr%ISOPEN
12285          THEN
12286             CLOSE l_chk_other_line_csr;
12287          END IF;
12288 
12289          IF l_get_sls_csr%ISOPEN
12290          THEN
12291             CLOSE l_get_sls_csr;
12292          END IF;
12293 
12294          IF l_scan_sls_csr%ISOPEN
12295          THEN
12296             CLOSE l_scan_sls_csr;
12297          END IF;
12298 
12299          IF l_new_sls_amt_csr%ISOPEN
12300          THEN
12301             CLOSE l_new_sls_amt_csr;
12302          END IF;
12303 
12304          IF l_old_sls_amt_csr%ISOPEN
12305          THEN
12306             CLOSE l_old_sls_amt_csr;
12307          END IF;
12308 
12309          IF l_sls_rule_pymnt_csr%ISOPEN
12310          THEN
12311             CLOSE l_sls_rule_pymnt_csr;
12312          END IF;
12313 
12314          IF l_item_line_csr%ISOPEN
12315          THEN
12316             CLOSE l_item_line_csr;
12317          END IF;
12318 
12319          IF l_service_line_csr%ISOPEN
12320          THEN
12321             CLOSE l_service_line_csr;
12322          END IF;
12323 
12324          IF l_fee_line_csr%ISOPEN
12325          THEN
12326             CLOSE l_fee_line_csr;
12327          END IF;
12328 
12329          IF l_usage_line_csr%ISOPEN
12330          THEN
12331             CLOSE l_usage_line_csr;
12332          END IF;
12333 
12334          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
12335          -- return status
12336          ROLLBACK TO update_lines;
12337          x_return_status := g_ret_sts_unexp_error;
12338 
12339          IF (is_debug_exception_on)
12340          THEN
12341             okl_debug_pub.log_debug (g_level_exception,
12342                                      l_module_name,
12343                                         'EXCEPTION :'
12344                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
12345                                     );
12346          END IF;
12347       WHEN OTHERS
12348       THEN
12349          IF l_trmnt_line_csr%ISOPEN
12350          THEN
12351             CLOSE l_trmnt_line_csr;
12352          END IF;
12353 
12354          IF l_chk_other_line_csr%ISOPEN
12355          THEN
12356             CLOSE l_chk_other_line_csr;
12357          END IF;
12358 
12359          IF l_get_sls_csr%ISOPEN
12360          THEN
12361             CLOSE l_get_sls_csr;
12362          END IF;
12363 
12364          IF l_scan_sls_csr%ISOPEN
12365          THEN
12366             CLOSE l_scan_sls_csr;
12367          END IF;
12368 
12369          IF l_new_sls_amt_csr%ISOPEN
12370          THEN
12371             CLOSE l_new_sls_amt_csr;
12372          END IF;
12373 
12374          IF l_old_sls_amt_csr%ISOPEN
12375          THEN
12376             CLOSE l_old_sls_amt_csr;
12377          END IF;
12378 
12379          IF l_sls_rule_pymnt_csr%ISOPEN
12380          THEN
12381             CLOSE l_sls_rule_pymnt_csr;
12382          END IF;
12383 
12384          IF l_item_line_csr%ISOPEN
12385          THEN
12386             CLOSE l_item_line_csr;
12387          END IF;
12388 
12389          IF l_service_line_csr%ISOPEN
12390          THEN
12391             CLOSE l_service_line_csr;
12392          END IF;
12393 
12394          IF l_fee_line_csr%ISOPEN
12395          THEN
12396             CLOSE l_fee_line_csr;
12397          END IF;
12398 
12399          IF l_usage_line_csr%ISOPEN
12400          THEN
12401             CLOSE l_usage_line_csr;
12402          END IF;
12403 
12404          -- RMUNJULU Bug # 2484327 added code to rollback to savepoint and set
12405          -- return status
12406          ROLLBACK TO update_lines;
12407          x_return_status := g_ret_sts_unexp_error;
12408          -- Set the oracle error message
12409          okl_api.set_message (p_app_name          => g_app_name_1,
12410                               p_msg_name          => g_unexpected_error,
12411                               p_token1            => g_sqlcode_token,
12412                               p_token1_value      => SQLCODE,
12413                               p_token2            => g_sqlerrm_token,
12414                               p_token2_value      => SQLERRM
12415                              );
12416 
12417          IF (is_debug_exception_on)
12418          THEN
12419             okl_debug_pub.log_debug (g_level_exception,
12420                                      l_module_name,
12421                                         'EXCEPTION :'
12422                                      || 'OTHERS, SQLCODE: '
12423                                      || SQLCODE
12424                                      || ' , SQLERRM : '
12425                                      || SQLERRM
12426                                     );
12427          END IF;
12428    END update_lines;
12429 
12430    -- Start of comments
12431    --
12432    -- Procedure Name : update_contract
12433    -- Desciption     : Will terminate the contract if all lines terminated
12434    -- Business Rules :
12435    -- Parameters   :
12436    -- Version    : 1.0
12437    -- History        : RMUNJULU 04-DEC-02 Bug # 2484327 Procedure Created
12438    --                : RMUNJULU 18-FEB-03 2805703 Changed logic to terminate
12439    --                  any hanging lines for the contract before terminating the
12440    --                  contract
12441    --                : RMUNJULU 03-MAR-03 2830997 Fixed the exception block
12442    --                : rmunjulu EDAT Added code to get effective dates and set
12443    --                  termination date accordingly.
12444    --
12445    -- End of comments
12446    PROCEDURE update_contract (
12447       p_api_version       IN              NUMBER,
12448       p_init_msg_list     IN              VARCHAR2,
12449       x_msg_count         OUT NOCOPY      NUMBER,
12450       x_msg_data          OUT NOCOPY      VARCHAR2,
12451       x_return_status     OUT NOCOPY      VARCHAR2,
12452       p_term_rec          IN              term_rec_type,
12453       p_sys_date          IN              DATE,
12454       p_status            IN              VARCHAR2,
12455       p_trn_reason_code   IN              VARCHAR2,
12456       px_msg_tbl          IN OUT NOCOPY   g_msg_tbl
12457    )
12458    IS
12459       -- Get the contract details
12460       CURSOR get_k_dtls_csr (
12461          p_khr_id   IN   NUMBER
12462       )
12463       IS
12464          SELECT khr.object_version_number object_version_number,
12465                 khr.sts_code sts_code,
12466                 khr.authoring_org_id authoring_org_id
12467                                       --CDUBEY authoring_org_id added for MOAC
12468            FROM okc_k_headers_v khr
12469           WHERE khr.ID = p_khr_id;
12470 
12471       -- Cursor to get the ste code
12472       CURSOR get_old_ste_code_csr (
12473          p_sts_code   VARCHAR2
12474       )
12475       IS
12476          SELECT stv.ste_code ste_code
12477            FROM okc_statuses_v stv
12478           WHERE stv.code = p_sts_code;
12479 
12480       -- Cursor to get the meaning of the sts_code passed
12481       CURSOR get_sts_meaning_csr (
12482          p_sts_code   IN   VARCHAR2
12483       )
12484       IS
12485          SELECT stv.meaning meaning
12486            FROM okc_statuses_v stv
12487           WHERE stv.code = p_sts_code;
12488 
12489       -- RMUNJULU 18-FEB-03 2805703 cursor to get active
12490       -- service or fee lines attached to contract directly
12491       CURSOR get_k_serv_fee_lines_csr (
12492          p_khr_id   IN   NUMBER
12493       )
12494       IS
12495          SELECT kle.ID ID
12496            FROM okc_k_lines_v kle,
12497                 okc_k_headers_v khr
12498           WHERE kle.dnz_chr_id = p_khr_id
12499             AND kle.sts_code = khr.sts_code
12500             AND kle.chr_id = khr.ID;
12501 
12502       l_return_status          VARCHAR2 (1)               := g_ret_sts_success;
12503       l_api_name      CONSTANT VARCHAR2 (30)              := 'update_contract';
12504       l_api_version   CONSTANT NUMBER                              := 1;
12505       l_id                     NUMBER                              := -9999;
12506       get_k_dtls_rec           get_k_dtls_csr%ROWTYPE;
12507       l_ste_code               okc_statuses_v.code%TYPE;
12508       lp_chrv_rec              okc_contract_pub.chrv_rec_type;
12509       lx_chrv_rec              okc_contract_pub.chrv_rec_type;
12510       l_sts_meaning            VARCHAR2 (300);
12511       l_msg_tbl                g_msg_tbl                         := px_msg_tbl;
12512       l_count                  NUMBER;
12513       l_module_name            VARCHAR2 (500)
12514                                          := g_module_name || 'update_contract';
12515       is_debug_exception_on    BOOLEAN
12516               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
12517       is_debug_procedure_on    BOOLEAN
12518               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
12519       is_debug_statement_on    BOOLEAN
12520               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
12521       -- RMUNJULU 18-FEB-03 2805703  Added variables
12522       l_clev_rec               okl_okc_migration_pvt.clev_rec_type;
12523       l_klev_rec               okl_contract_pub.klev_rec_type;
12524       lx_clev_rec              okl_okc_migration_pvt.clev_rec_type;
12525       lx_klev_rec              okl_contract_pub.klev_rec_type;
12526       -- rmunjulu EDAT
12527       l_quote_accpt_date       DATE;
12528       l_quote_eff_date         DATE;
12529       l_authoring_org_id       NUMBER;
12530                                     --CDUBEY l_authoring_org_id added for MOAC
12531    BEGIN
12532       -- Start a savepoint to rollback to if error
12533       SAVEPOINT update_contract;
12534 
12535       IF (is_debug_procedure_on)
12536       THEN
12537          okl_debug_pub.log_debug (g_level_procedure,
12538                                   l_module_name,
12539                                   'Begin(+)'
12540                                  );
12541       END IF;
12542 
12543       IF (is_debug_statement_on)
12544       THEN
12545          okl_debug_pub.log_debug (g_level_statement,
12546                                   l_module_name,
12547                                   'In param, p_sys_date: ' || p_sys_date
12548                                  );
12549          okl_debug_pub.log_debug (g_level_statement,
12550                                   l_module_name,
12551                                   'In param, p_status: ' || p_status
12552                                  );
12553          okl_debug_pub.log_debug (g_level_statement,
12554                                   l_module_name,
12555                                      'In param, p_trn_reason_code: '
12556                                   || p_trn_reason_code
12557                                  );
12558          okl_debug_pub.log_debug (g_level_statement,
12559                                   l_module_name,
12560                                      'In param, p_term_rec.p_contract_id: '
12561                                   || p_term_rec.p_contract_id
12562                                  );
12563          okl_debug_pub.log_debug
12564                                 (g_level_statement,
12565                                  l_module_name,
12566                                     'In param, p_term_rec.p_contract_number: '
12567                                  || p_term_rec.p_contract_number
12568                                 );
12569          okl_debug_pub.log_debug
12570                                (g_level_statement,
12571                                 l_module_name,
12572                                    'In param, p_term_rec.p_termination_date: '
12573                                 || p_term_rec.p_termination_date
12574                                );
12575       END IF;
12576 
12577       -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
12578 
12579       -- rmunjulu EDAT
12580       -- If quote exists then accnting date is quote accept date else sysdate
12581       IF NVL (okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
12582       THEN
12583          l_quote_accpt_date :=
12584                               okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
12585          l_quote_eff_date :=
12586                             okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
12587       ELSE
12588          l_quote_accpt_date := p_sys_date;
12589          l_quote_eff_date := p_sys_date;
12590       END IF;
12591 
12592       -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
12593 
12594       -- RMUNJULU 18-FEB-03 2805703 terminate the active service and fee lines
12595       FOR get_k_serv_fee_lines_rec IN
12596          get_k_serv_fee_lines_csr (p_term_rec.p_contract_id)
12597       LOOP
12598          l_klev_rec.ID := get_k_serv_fee_lines_rec.ID;
12599          l_clev_rec.ID := get_k_serv_fee_lines_rec.ID;
12600          l_clev_rec.date_terminated := l_quote_eff_date;     -- rmunjulu EDAT
12601          l_clev_rec.sts_code := p_status;
12602          l_clev_rec.trn_code := p_trn_reason_code;
12603 
12604          IF (is_debug_statement_on)
12605          THEN
12606             okl_debug_pub.log_debug
12607                              (g_level_statement,
12608                               l_module_name,
12609                               'calling OKL_CONTRACT_PUB.update_contract_line'
12610                              );
12611          END IF;
12612 
12613          okl_contract_pub.update_contract_line
12614                                           (p_api_version        => p_api_version,
12615                                            p_init_msg_list      => g_false,
12616                                            x_return_status      => x_return_status,
12617                                            x_msg_count          => x_msg_count,
12618                                            x_msg_data           => x_msg_data,
12619                                            p_clev_rec           => l_clev_rec,
12620                                            p_klev_rec           => l_klev_rec,
12621                                            x_clev_rec           => lx_clev_rec,
12622                                            x_klev_rec           => lx_klev_rec
12623                                           );
12624 
12625          IF (is_debug_statement_on)
12626          THEN
12627             okl_debug_pub.log_debug
12628                (g_level_statement,
12629                 l_module_name,
12630                    'called OKL_CONTRACT_PUB.update_contract_line , return status: '
12631                 || x_return_status
12632                );
12633          END IF;
12634 
12635          IF l_return_status <> g_ret_sts_success
12636          THEN
12637             -- Contract line table update failed.
12638             okl_api.set_message (p_app_name      => g_app_name,
12639                                  p_msg_name      => 'OKL_AM_ERR_K_LINE_UPD');
12640          END IF;
12641 
12642          -- Raise exception to rollback to savepoint
12643          IF (l_return_status = g_ret_sts_unexp_error)
12644          THEN
12645             RAISE okl_api.g_exception_unexpected_error;
12646          ELSIF (l_return_status = g_ret_sts_error)
12647          THEN
12648             RAISE okl_api.g_exception_error;
12649          END IF;
12650       END LOOP;
12651 
12652       -- Get the contract details
12653       OPEN get_k_dtls_csr (p_term_rec.p_contract_id);
12654 
12655       FETCH get_k_dtls_csr
12656        INTO get_k_dtls_rec;
12657 
12658       CLOSE get_k_dtls_csr;
12659 
12660       -- Get the ste_code
12661       OPEN get_old_ste_code_csr (get_k_dtls_rec.sts_code);
12662 
12663       FETCH get_old_ste_code_csr
12664        INTO l_ste_code;
12665 
12666       CLOSE get_old_ste_code_csr;
12667 
12668       -- Get the sts meaning
12669       OPEN get_sts_meaning_csr (p_status);
12670 
12671       FETCH get_sts_meaning_csr
12672        INTO l_sts_meaning;
12673 
12674       CLOSE get_sts_meaning_csr;
12675 
12676       -- set the lp_chrv_rec record
12677       IF     (p_term_rec.p_termination_date IS NOT NULL)
12678          AND (p_term_rec.p_termination_date <> okl_api.g_miss_date)
12679       THEN
12680          lp_chrv_rec.date_terminated := p_term_rec.p_termination_date;
12681       ELSE
12682          lp_chrv_rec.date_terminated := l_quote_eff_date;    -- rmunjulu EDAT
12683       END IF;
12684 
12685       lp_chrv_rec.ID := p_term_rec.p_contract_id;
12686       lp_chrv_rec.object_version_number :=
12687                                           get_k_dtls_rec.object_version_number;
12688       lp_chrv_rec.sts_code := p_status;
12689       lp_chrv_rec.old_sts_code := get_k_dtls_rec.sts_code;
12690       lp_chrv_rec.new_sts_code := p_status;
12691       lp_chrv_rec.old_ste_code := l_ste_code;
12692       lp_chrv_rec.new_ste_code := p_status;
12693       lp_chrv_rec.trn_code := p_trn_reason_code;
12694       lp_chrv_rec.org_id := l_authoring_org_id;        --CDUBEY added for MOAC
12695 
12696       -- Call the okl api to update contract with termination info
12697       IF (is_debug_statement_on)
12698       THEN
12699          okl_debug_pub.log_debug
12700                            (g_level_statement,
12701                             l_module_name,
12702                             'calling OKC_CONTRACT_PUB.update_contract_header'
12703                            );
12704       END IF;
12705 
12706       okc_contract_pub.update_contract_header
12707                                           (p_api_version            => p_api_version,
12708                                            p_init_msg_list          => g_false,
12709                                            x_return_status          => l_return_status,
12710                                            x_msg_count              => x_msg_count,
12711                                            x_msg_data               => x_msg_data,
12712                                            p_restricted_update      => g_true,
12713                                            p_chrv_rec               => lp_chrv_rec,
12714                                            x_chrv_rec               => lx_chrv_rec
12715                                           );
12716 
12717       IF (is_debug_statement_on)
12718       THEN
12719          okl_debug_pub.log_debug
12720             (g_level_statement,
12721              l_module_name,
12722                 'called OKC_CONTRACT_PUB.update_contract_header , return status: '
12723              || l_return_status
12724             );
12725       END IF;
12726 
12727       IF l_return_status <> g_ret_sts_success
12728       THEN
12729          -- Error updating contract CONTRACT_NUMBER to status STATUS.
12730          okl_api.set_message (p_app_name          => g_app_name,
12731                               p_msg_name          => 'OKL_AM_K_STATUS_UPD_ERR',
12732                               p_token1            => 'CONTRACT_NUMBER',
12733                               p_token1_value      => p_term_rec.p_contract_number,
12734                               p_token2            => 'STATUS',
12735                               p_token2_value      => l_sts_meaning
12736                              );
12737       ELSE
12738          l_count := l_msg_tbl.COUNT;
12739          -- Set the success message in the message table
12740          l_msg_tbl (l_count).msg_desc := g_am_k_status_upd;
12741          l_msg_tbl (l_count).msg_token1 := 'CONTRACT_NUMBER';
12742          l_msg_tbl (l_count).msg_token1_value := p_term_rec.p_contract_number;
12743          l_msg_tbl (l_count).msg_token2 := 'STATUS';
12744          l_msg_tbl (l_count).msg_token2_value := l_sts_meaning;
12745       END IF;
12746 
12747       -- Raise exception to rollback to savepoint
12748       IF (l_return_status = g_ret_sts_unexp_error)
12749       THEN
12750          RAISE okl_api.g_exception_unexpected_error;
12751       ELSIF (l_return_status = g_ret_sts_error)
12752       THEN
12753          RAISE okl_api.g_exception_error;
12754       END IF;
12755 
12756       -- Set the return status and message table
12757       x_return_status := l_return_status;
12758       px_msg_tbl := l_msg_tbl;
12759 
12760       IF (is_debug_procedure_on)
12761       THEN
12762          okl_debug_pub.log_debug (g_level_procedure,
12763                                   l_module_name,
12764                                   'End(-)'
12765                                  );
12766       END IF;
12767    EXCEPTION
12768       WHEN okl_api.g_exception_error
12769       THEN
12770          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12771          IF get_k_dtls_csr%ISOPEN
12772          THEN
12773             CLOSE get_k_dtls_csr;
12774          END IF;
12775 
12776          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12777          IF get_old_ste_code_csr%ISOPEN
12778          THEN
12779             CLOSE get_old_ste_code_csr;
12780          END IF;
12781 
12782          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12783          IF get_sts_meaning_csr%ISOPEN
12784          THEN
12785             CLOSE get_sts_meaning_csr;
12786          END IF;
12787 
12788          ROLLBACK TO update_contract;
12789          x_return_status := g_ret_sts_error;
12790 
12791          IF (is_debug_exception_on)
12792          THEN
12793             okl_debug_pub.log_debug (g_level_exception,
12794                                      l_module_name,
12795                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
12796                                     );
12797          END IF;
12798       WHEN okl_api.g_exception_unexpected_error
12799       THEN
12800          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12801          IF get_k_dtls_csr%ISOPEN
12802          THEN
12803             CLOSE get_k_dtls_csr;
12804          END IF;
12805 
12806          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12807          IF get_old_ste_code_csr%ISOPEN
12808          THEN
12809             CLOSE get_old_ste_code_csr;
12810          END IF;
12811 
12812          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12813          IF get_sts_meaning_csr%ISOPEN
12814          THEN
12815             CLOSE get_sts_meaning_csr;
12816          END IF;
12817 
12818          ROLLBACK TO update_contract;
12819          x_return_status := g_ret_sts_unexp_error;
12820 
12821          IF (is_debug_exception_on)
12822          THEN
12823             okl_debug_pub.log_debug (g_level_exception,
12824                                      l_module_name,
12825                                         'EXCEPTION :'
12826                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
12827                                     );
12828          END IF;
12829       WHEN OTHERS
12830       THEN
12831          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12832          IF get_k_dtls_csr%ISOPEN
12833          THEN
12834             CLOSE get_k_dtls_csr;
12835          END IF;
12836 
12837          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12838          IF get_old_ste_code_csr%ISOPEN
12839          THEN
12840             CLOSE get_old_ste_code_csr;
12841          END IF;
12842 
12843          -- RMUNJULU 03-MAR-03 2830997 Changed NOTFOUND to ISOPEN
12844          IF get_sts_meaning_csr%ISOPEN
12845          THEN
12846             CLOSE get_sts_meaning_csr;
12847          END IF;
12848 
12849          ROLLBACK TO update_contract;
12850          x_return_status := g_ret_sts_unexp_error;
12851          -- Set the oracle error message
12852          okl_api.set_message (p_app_name          => g_app_name_1,
12853                               p_msg_name          => g_unexpected_error,
12854                               p_token1            => g_sqlcode_token,
12855                               p_token1_value      => SQLCODE,
12856                               p_token2            => g_sqlerrm_token,
12857                               p_token2_value      => SQLERRM
12858                              );
12859 
12860          IF (is_debug_exception_on)
12861          THEN
12862             okl_debug_pub.log_debug (g_level_exception,
12863                                      l_module_name,
12864                                         'EXCEPTION :'
12865                                      || 'OTHERS, SQLCODE: '
12866                                      || SQLCODE
12867                                      || ' , SQLERRM : '
12868                                      || SQLERRM
12869                                     );
12870          END IF;
12871    END update_contract;
12872 
12873    -- Start of comments
12874    --
12875    -- Procedure Name : terminate_lines
12876    -- Desciption     : Terminate the lines
12877    -- Business Rules :
12878    -- Parameters   :
12879    -- Version      : 1.0
12880    -- History        : RMUNJULU 04-DEC-02 Bug # 2484327
12881    --                  Added call to update_contract and code to set messages on
12882    --                  message stack from message table
12883    --                  Changed logic to do mass_rebook only when BOOKED or
12884    --                  EVERGREEN lines found
12885    --                : RMUNJULU 16-DEC-02 Bug # 2484327, changed cursor to get lines
12886    --                  which are of the same status as header
12887    --                : RMUNJULU 20-DEC-02 2484327 Added call to cancel_insu procedure
12888    --                  when no more lines and moved cancel_activate_insu when
12889    --                  some lines exist
12890    --                : RMUNJULU 03-JAN-03 2683876 Added call to close_balances
12891    --                  when no more lines
12892    --                : RMUNJULU 20-DEC-02 2683876 Set the trn if cancel insurance
12893    --                  and close balances successful
12894    --                : RMUNJULU 09-JAN-03 2743604 Added call to reverse loss provisions
12895    --                : RMUNJULU 18-FEB-03 2805703 Changed cursor to get asset lines
12896    --                : RMUNJULU 14-MAR-03 2852933 Removed call to cancel activate since is
12897    --                  now done in Mass Rebook
12898    --                : RMUNJULU 28-MAR-03 2877278 Added code to call close_streams here
12899    --                  and changed call to update lines
12900    --                : RMUNJULU CONTRACT BLOCKING : CHANGED CODE FOR DOING MASS_REBOOK
12901    --                  MULTIPLE TIMES WHEN IT FAILS
12902    --                : RMUNJULU CONTRACT BLOCKING (2) Changed to update termination trn
12903    --                  before mass rebook and update trn after mass rebook properly
12904    --                : RMUNJULU 3485854 12-MAR-04 Added code to update trn to PROCESSED if Mass rebook
12905    --                  was not needed, which will be in case of EVERGREEN partial termination
12906    --                : RMUNJULU 3816891 FORWARDPORT Removed close_streams from central processing
12907    --                  as rebook does historization
12908    --                : rmunjulu EDAT called mass_rebook with p_sys_date
12909    -- End of comments
12910    PROCEDURE terminate_lines (
12911       p_api_version       IN              NUMBER,
12912       p_init_msg_list     IN              VARCHAR2,
12913       x_msg_count         OUT NOCOPY      NUMBER,
12914       x_msg_data          OUT NOCOPY      VARCHAR2,
12915       x_return_status     OUT NOCOPY      VARCHAR2,
12916       px_overall_status   IN OUT NOCOPY   VARCHAR2,
12917       p_trn_already_set   IN              VARCHAR2,
12918                                                  -- RMUNJULU CONTRACT BLOCKING
12919       p_term_rec          IN              term_rec_type,
12920       p_sys_date          IN              DATE,
12921       p_klev_tbl          IN              klev_tbl_type,
12922       p_status            IN              VARCHAR2,
12923       px_tcnv_rec         IN OUT NOCOPY   tcnv_rec_type
12924    )
12925    IS
12926       -- RMUNJULU Bug # 2484327, added this cursor
12927       -- RMUNJULU 16-DEC-02 Bug # 2484327, get lines which are of the same status as header
12928       -- RMUNJULU 18-FEB-03 2805703 Added code to get only financial asset lines
12929       CURSOR get_k_lines_csr (
12930          p_khr_id   IN   NUMBER
12931       )
12932       IS
12933          SELECT kle.ID ID
12934            FROM okc_k_lines_v kle,
12935                 okc_k_headers_v khr,
12936                 okc_line_styles_v lse
12937           WHERE kle.dnz_chr_id = p_khr_id
12938             AND kle.dnz_chr_id = khr.ID
12939             AND kle.sts_code = khr.sts_code
12940             AND kle.lse_id = lse.ID
12941             AND lse.lty_code = g_fin_line_lty_code;
12942 
12943       -- RMUNJULU 3485854 12-MAR-04
12944       -- get the trn status
12945       CURSOR get_trn_status_csr (
12946          p_tcn_id   IN   NUMBER
12947       )
12948       IS
12949          SELECT tcn.tmt_status_code    -- akrangan sla tmt_status_code changes
12950            FROM okl_trx_contracts tcn
12951           WHERE tcn.ID = p_tcn_id;
12952 
12953       l_return_status          VARCHAR2 (1)   := g_ret_sts_success;
12954       l_module_name            VARCHAR2 (500)
12955                                          := g_module_name || 'terminate_lines';
12956       is_debug_exception_on    BOOLEAN
12957               := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
12958       is_debug_procedure_on    BOOLEAN
12959               := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
12960       is_debug_statement_on    BOOLEAN
12961               := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
12962       -- RMUNJULU Bug # 2484327 Added these variables
12963       l_msg_tbl                g_msg_tbl;
12964       k                        NUMBER;
12965       l_id                     NUMBER         := -9999;
12966       lx_klev_tbl              klev_tbl_type;
12967       -- RMUNJULU CONTRACT BLOCKING ADDED VARIABLE
12968       lx_mass_rebook_success   VARCHAR2 (3)   := g_ret_sts_success;
12969       -- CONTRACT BLOCKING (2)
12970       lx_id                    NUMBER;
12971    BEGIN
12972       -- LOGIC START
12973       -- Update Lines
12974       -- Close Streams
12975       -- If any more active lines then
12976       -- Mass Rebook
12977       -- Cancel Activate Insurance
12978       -- Else -- no more active lines
12979       -- Close Balances
12980       -- Update Contract
12981       -- reverse loss provisions
12982       -- Cancel Insurances
12983       -- End if
12984       -- Set Success msgs to msg stack
12985       -- LOGIC END
12986 
12987       -- Create a savepoint
12988       SAVEPOINT terminate_lines;
12989 
12990       IF (is_debug_procedure_on)
12991       THEN
12992          okl_debug_pub.log_debug (g_level_procedure,
12993                                   l_module_name,
12994                                   'Begin(+)'
12995                                  );
12996       END IF;
12997 
12998       IF (is_debug_statement_on)
12999       THEN
13000          okl_debug_pub.log_debug (g_level_statement,
13001                                   l_module_name,
13002                                   'In param, p_sys_date: ' || p_sys_date
13003                                  );
13004          okl_debug_pub.log_debug (g_level_statement,
13005                                   l_module_name,
13006                                   'In param, p_status: ' || p_status
13007                                  );
13008          okl_debug_pub.log_debug (g_level_statement,
13009                                   l_module_name,
13010                                      'In param, p_trn_already_set: '
13011                                   || p_trn_already_set
13012                                  );
13013          okl_debug_pub.log_debug (g_level_statement,
13014                                   l_module_name,
13015                                      'In param, px_overall_status: '
13016                                   || px_overall_status
13017                                  );
13018          okl_debug_pub.log_debug (g_level_statement,
13019                                   l_module_name,
13020                                      'In param, p_term_rec.p_contract_id: '
13021                                   || p_term_rec.p_contract_id
13022                                  );
13023          okl_debug_pub.log_debug
13024                             (g_level_statement,
13025                              l_module_name,
13026                                 'In param, px_tcnv_rec.tmt_generic_flag2_yn: '
13027                              || px_tcnv_rec.tmt_generic_flag2_yn
13028                             );
13029          okl_debug_pub.log_debug (g_level_statement,
13030                                   l_module_name,
13031                                      'In param, px_tcnv_rec.trn_code: '
13032                                   || px_tcnv_rec.trn_code
13033                                  );
13034          okl_debug_pub.log_debug (g_level_statement,
13035                                   l_module_name,
13036                                      'In param, px_tcnv_rec.id: '
13037                                   || px_tcnv_rec.ID
13038                                  );
13039 
13040          IF p_klev_tbl.COUNT > 0
13041          THEN
13042             FOR i IN p_klev_tbl.FIRST .. p_klev_tbl.LAST
13043             LOOP
13044                IF (p_klev_tbl.EXISTS (i))
13045                THEN
13046                   okl_debug_pub.log_debug (g_level_statement,
13047                                            l_module_name,
13048                                               'In param, p_klev_tbl('
13049                                            || i
13050                                            || ').p_kle_id: '
13051                                            || p_klev_tbl (i).p_kle_id
13052                                           );
13053                   okl_debug_pub.log_debug (g_level_statement,
13054                                            l_module_name,
13055                                               'In param, p_klev_tbl('
13056                                            || i
13057                                            || ').p_asset_quantity: '
13058                                            || p_klev_tbl (i).p_asset_quantity
13059                                           );
13060                   okl_debug_pub.log_debug (g_level_statement,
13061                                            l_module_name,
13062                                               'In param, p_klev_tbl('
13063                                            || i
13064                                            || ').p_asset_name: '
13065                                            || p_klev_tbl (i).p_asset_name
13066                                           );
13067                   okl_debug_pub.log_debug (g_level_statement,
13068                                            l_module_name,
13069                                               'In param, p_klev_tbl('
13070                                            || i
13071                                            || ').p_quote_quantity: '
13072                                            || p_klev_tbl (i).p_quote_quantity
13073                                           );
13074                   okl_debug_pub.log_debug (g_level_statement,
13075                                            l_module_name,
13076                                               'In param, p_klev_tbl('
13077                                            || i
13078                                            || ').p_tql_id: '
13079                                            || p_klev_tbl (i).p_tql_id
13080                                           );
13081                   okl_debug_pub.log_debug (g_level_statement,
13082                                            l_module_name,
13083                                               'In param, p_klev_tbl('
13084                                            || i
13085                                            || ').p_split_kle_name: '
13086                                            || p_klev_tbl (i).p_split_kle_name
13087                                           );
13088                   okl_debug_pub.log_debug (g_level_statement,
13089                                            l_module_name,
13090                                               'In param, p_klev_tbl('
13091                                            || i
13092                                            || ').p_split_kle_id: '
13093                                            || p_klev_tbl (i).p_split_kle_id
13094                                           );
13095                END IF;
13096             END LOOP;
13097          END IF;
13098       END IF;
13099 
13100       -- RMUNJULU CONTRACT BLOCKING
13101       -- Added code to check that generic_flag2 which is now used to check
13102       -- if update_lines and close_streams was done earlier or not is checked
13103       -- since we now can recycle if only mass_rebook fails and in that condition
13104       -- we do mass_rebook only
13105 
13106       -- Check if Update_Lines and Close Streams required
13107       IF    (    p_trn_already_set = g_yes
13108              AND NVL (px_tcnv_rec.tmt_generic_flag2_yn, '?') <> g_yes
13109             )
13110          OR (p_trn_already_set = g_no)
13111       THEN
13112          -- Call the Update Lines to update the lines to terminated
13113          -- BAKUCHIB 28-MAR-03 2877278 Added parameter to call
13114          update_lines
13115                (p_api_version          => p_api_version,
13116                 p_init_msg_list        => g_false,
13117                 x_msg_count            => x_msg_count,
13118                 x_msg_data             => x_msg_data,
13119                 x_return_status        => l_return_status,
13120                 p_term_rec             => p_term_rec,
13121                 p_sys_date             => p_sys_date,
13122                 p_klev_tbl             => p_klev_tbl,
13123                 p_status               => p_status,
13124                 p_trn_reason_code      => px_tcnv_rec.trn_code,
13125                 x_klev_tbl             => lx_klev_tbl,
13126                                            -- BAKUCHIB 28-MAR-03 2877278 Added
13127                 x_msg_tbl              => l_msg_tbl
13128                );
13129 
13130          IF (is_debug_statement_on)
13131          THEN
13132             okl_debug_pub.log_debug
13133                                   (g_level_statement,
13134                                    l_module_name,
13135                                       'called update_lines , return status: '
13136                                    || l_return_status
13137                                   );
13138          END IF;
13139 
13140          -- RMUNJULU CONTRACT BLOCKING -- Moved this if from exception
13141          IF l_return_status <> g_ret_sts_success
13142          THEN
13143             -- Contract line table update failed.
13144             okl_api.set_message (p_app_name      => g_app_name,
13145                                  p_msg_name      => 'OKL_AM_ERR_K_LINE_UPD');
13146          END IF;
13147 
13148          -- rollback if update lines failed
13149          IF (l_return_status = g_ret_sts_unexp_error)
13150          THEN
13151             RAISE okl_api.g_exception_unexpected_error;
13152          ELSIF (l_return_status = g_ret_sts_error)
13153          THEN
13154             RAISE okl_api.g_exception_error;
13155          END IF;
13156 
13157          -- BAKUCHIB 28-MAR-03 2877278 Added call to close streams here
13158          -- close streams
13159 
13160          /* -- RMUNJULU 3816891 FORWARDPORT Do not historize streams as rebook will do it
13161 
13162            close_streams(
13163              p_term_rec           => p_term_rec,
13164              p_sys_date           => p_sys_date,
13165              p_klev_tbl           => lx_klev_tbl,
13166              p_trn_already_set    => G_NO,
13167              px_overall_status    => px_overall_status,
13168              px_tcnv_rec          => px_tcnv_rec,
13169              x_return_status      => l_return_status);
13170 
13171            -- RMUNJULU CONTRACT BLOCKING -- Moved this if from exception
13172            IF l_return_status <> G_RET_STS_SUCCESS THEN
13173 
13174               -- Contract line table update failed.
13175               OKL_API.set_message(
13176                           p_app_name => G_APP_NAME,
13177                           p_msg_name => 'OKL_AM_ERR_K_LINE_UPD');
13178 
13179            END IF;
13180 
13181            -- rollback if close streams failed
13182            IF (l_return_status = G_RET_STS_UNEXP_ERROR) THEN
13183              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
13184            ELSIF (l_return_status = G_RET_STS_ERROR) THEN
13185              RAISE OKL_API.G_EXCEPTION_ERROR;
13186            END IF;
13187          */
13188 
13189          -- RMUNJULU CONTRACT BLOCKING
13190          -- set the transaction record for new flag being maintained
13191          set_transaction_rec (p_return_status       => l_return_status,
13192                               p_overall_status      => px_overall_status,
13193                               p_tmt_flag            => 'TMT_GENERIC_FLAG2_YN',
13194                               p_tsu_code            => 'WORKING',
13195                               px_tcnv_rec           => px_tcnv_rec
13196                              );
13197       END IF;                        -- RMUNJULU CONTRACT BLOCKING - END OF IF
13198 
13199       -- RMUNJULU Bug # 2484327
13200       -- Get any assets which are in BOOKED or EVERGREEN status
13201       OPEN get_k_lines_csr (p_term_rec.p_contract_id);
13202 
13203       FETCH get_k_lines_csr
13204        INTO l_id;
13205 
13206       -- RMUNJULU 18-FEB-03 2805703 Added If to set l_id
13207       IF get_k_lines_csr%NOTFOUND
13208       THEN
13209          l_id := -9999;
13210       END IF;
13211 
13212       CLOSE get_k_lines_csr;
13213 
13214       -- RMUNJULU Bug # 2484327 -- Added this IF
13215       -- If any BOOKED or EVERGREEN lines then do mass_rebook
13216       -- else do update_contract
13217       -- RMUNJULU 18-FEB-03 2805703 Changed IF condition for better check
13218       IF l_id <> -9999
13219       THEN
13220          --+++++++++++++++start -- CONTRACT BLOCKING (2)
13221          -- Update the transaction before mass rebook, since will be updated again by mass rebook
13222          -- update the transaction record
13223          process_transaction (p_id                 => 0,
13224                               p_term_rec           => p_term_rec,
13225                               p_tcnv_rec           => px_tcnv_rec,
13226                               p_trn_mode           => 'UPDATE',
13227                               x_id                 => lx_id,
13228                               x_return_status      => l_return_status
13229                              );
13230 
13231          IF (is_debug_statement_on)
13232          THEN
13233             okl_debug_pub.log_debug
13234                            (g_level_statement,
13235                             l_module_name,
13236                                'called process_transaction , return status: '
13237                             || l_return_status
13238                            );
13239          END IF;
13240 
13241          -- rollback if processing transaction failed
13242          IF (l_return_status = g_ret_sts_unexp_error)
13243          THEN
13244             RAISE okl_api.g_exception_unexpected_error;
13245          ELSIF (l_return_status = g_ret_sts_error)
13246          THEN
13247             RAISE okl_api.g_exception_error;
13248          END IF;
13249 
13250          --+++++++++++++++end -- CONTRACT BLOCKING (2)
13251 
13252          -- Call the Mass Rebook to rebook the contract lines
13253          mass_rebook (p_api_version        => p_api_version,
13254                       p_init_msg_list      => g_false,
13255                       x_msg_count          => x_msg_count,
13256                       x_msg_data           => x_msg_data,
13257                       x_return_status      => l_return_status,
13258                       p_term_rec           => p_term_rec,
13259                       p_tcnv_rec           => px_tcnv_rec,
13260                       p_sys_date           => p_sys_date,     -- rmunjulu EDAT
13261                       x_mrbk_success       => lx_mass_rebook_success
13262                      );                    -- RMUNJULU CONTRACT BLOCKING ADDED
13263 
13264          IF (is_debug_statement_on)
13265          THEN
13266             okl_debug_pub.log_debug
13267                                    (g_level_statement,
13268                                     l_module_name,
13269                                        'called mass_rebook , return status: '
13270                                     || l_return_status
13271                                    );
13272          END IF;
13273       ELSE
13274          -- No more active lines
13275 
13276          -- RMUNJULU 03-JAN-03 2683876 Added call to this procedure
13277          -- Call the Close Balances to close the remaining balances
13278          close_balances (p_api_version        => p_api_version,
13279                          p_init_msg_list      => g_false,
13280                          x_msg_count          => x_msg_count,
13281                          x_msg_data           => x_msg_data,
13282                          x_return_status      => l_return_status,
13283                          p_term_rec           => p_term_rec,
13284                          p_sys_date           => p_sys_date,
13285                          p_tcnv_rec           => px_tcnv_rec,
13286                          px_msg_tbl           => l_msg_tbl
13287                         );
13288 
13289          IF (is_debug_statement_on)
13290          THEN
13291             okl_debug_pub.log_debug
13292                                 (g_level_statement,
13293                                  l_module_name,
13294                                     'called close_balances , return status: '
13295                                  || l_return_status
13296                                 );
13297          END IF;
13298 
13299          -- RMUNJULU CONTRACT BLOCKING -- Moved this if from exception
13300          IF l_return_status <> g_ret_sts_success
13301          THEN
13302             -- Contract line table update failed.
13303             okl_api.set_message (p_app_name      => g_app_name,
13304                                  p_msg_name      => 'OKL_AM_ERR_K_LINE_UPD');
13305          END IF;
13306 
13307          -- rollback if close balances failed
13308          IF (l_return_status = g_ret_sts_unexp_error)
13309          THEN
13310             RAISE okl_api.g_exception_unexpected_error;
13311          ELSIF (l_return_status = g_ret_sts_error)
13312          THEN
13313             RAISE okl_api.g_exception_error;
13314          END IF;
13315 
13316          -- RMUNJULU 09-JAN-03 2743604 Added call to this procedure
13317          -- Call to Reverse the non-incomes and loss-provisions
13318          reverse_loss_provisions (p_api_version        => p_api_version,
13319                                   p_init_msg_list      => g_false,
13320                                   x_msg_count          => x_msg_count,
13321                                   x_msg_data           => x_msg_data,
13322                                   x_return_status      => l_return_status,
13323                                   p_term_rec           => p_term_rec,
13324                                   p_sys_date           => p_sys_date,
13325                                   px_msg_tbl           => l_msg_tbl
13326                                  );
13327 
13328          IF (is_debug_statement_on)
13329          THEN
13330             okl_debug_pub.log_debug
13331                        (g_level_statement,
13332                         l_module_name,
13333                            'called reverse_loss_provisions , return status: '
13334                         || l_return_status
13335                        );
13336          END IF;
13337 
13338          -- RMUNJULU CONTRACT BLOCKING -- Moved this if from exception
13339          IF l_return_status <> g_ret_sts_success
13340          THEN
13341             -- Contract line table update failed.
13342             okl_api.set_message (p_app_name      => g_app_name,
13343                                  p_msg_name      => 'OKL_AM_ERR_K_LINE_UPD');
13344          END IF;
13345 
13346          -- rollback if reverse loss provisions failed
13347          IF (l_return_status = g_ret_sts_unexp_error)
13348          THEN
13349             RAISE okl_api.g_exception_unexpected_error;
13350          ELSIF (l_return_status = g_ret_sts_error)
13351          THEN
13352             RAISE okl_api.g_exception_error;
13353          END IF;
13354 
13355          -- RMUNJULU Bug # 2484327 Added call to this procedure
13356          -- Call the Update Lines to update the contract to terminated if
13357          -- all lines of the contract are terminated
13358          update_contract (p_api_version          => p_api_version,
13359                           p_init_msg_list        => g_false,
13360                           x_msg_count            => x_msg_count,
13361                           x_msg_data             => x_msg_data,
13362                           x_return_status        => l_return_status,
13363                           p_term_rec             => p_term_rec,
13364                           p_sys_date             => p_sys_date,
13365                           p_status               => p_status,
13366                           p_trn_reason_code      => px_tcnv_rec.trn_code,
13367                           px_msg_tbl             => l_msg_tbl
13368                          );
13369 
13370          IF (is_debug_statement_on)
13371          THEN
13372             okl_debug_pub.log_debug
13373                                (g_level_statement,
13374                                 l_module_name,
13375                                    'called update_contract , return status: '
13376                                 || l_return_status
13377                                );
13378          END IF;
13379 
13380          -- RMUNJULU CONTRACT BLOCKING -- Moved this if from exception
13381          IF l_return_status <> g_ret_sts_success
13382          THEN
13383             -- Contract line table update failed.
13384             okl_api.set_message (p_app_name      => g_app_name,
13385                                  p_msg_name      => 'OKL_AM_ERR_K_LINE_UPD');
13386          END IF;
13387 
13388          -- rollback if update contract failed
13389          IF (l_return_status = g_ret_sts_unexp_error)
13390          THEN
13391             RAISE okl_api.g_exception_unexpected_error;
13392          ELSIF (l_return_status = g_ret_sts_error)
13393          THEN
13394             RAISE okl_api.g_exception_error;
13395          END IF;
13396 
13397          -- RMUNJULU 20-DEC-02 2484327 Added call to this procedure
13398          -- Call to Cancel Insurance to cancel the insurances
13399          cancel_insurance (p_api_version        => p_api_version,
13400                            p_init_msg_list      => g_false,
13401                            x_msg_count          => x_msg_count,
13402                            x_msg_data           => x_msg_data,
13403                            x_return_status      => l_return_status,
13404                            p_term_rec           => p_term_rec,
13405                            p_sys_date           => p_sys_date,
13406                            p_klev_tbl           => p_klev_tbl
13407                           );
13408 
13409          IF (is_debug_statement_on)
13410          THEN
13411             okl_debug_pub.log_debug
13412                               (g_level_statement,
13413                                l_module_name,
13414                                   'called cancel_insurance , return status: '
13415                                || l_return_status
13416                               );
13417          END IF;
13418 
13419          -- RMUNJULU CONTRACT BLOCKING -- Moved this if from exception
13420          IF l_return_status <> g_ret_sts_success
13421          THEN
13422             -- Contract line table update failed.
13423             okl_api.set_message (p_app_name      => g_app_name,
13424                                  p_msg_name      => 'OKL_AM_ERR_K_LINE_UPD');
13425          END IF;
13426 
13427          -- rollback if cancel insurance failed
13428          IF (l_return_status = g_ret_sts_unexp_error)
13429          THEN
13430             RAISE okl_api.g_exception_unexpected_error;
13431          ELSIF (l_return_status = g_ret_sts_error)
13432          THEN
13433             RAISE okl_api.g_exception_error;
13434          END IF;
13435 
13436          -- RMUNJULU 20-DEC-02 2683876 Set the trn if close balances successful
13437          -- set the transaction record
13438          set_transaction_rec (p_return_status       => l_return_status,
13439                               p_overall_status      => px_overall_status,
13440                               p_tmt_flag            => 'TMT_CLOSE_BALANCES_YN',
13441                               p_tsu_code            => 'WORKING',
13442                               px_tcnv_rec           => px_tcnv_rec
13443                              );
13444          -- RMUNJULU 20-DEC-02 2683876 Set the trn if cancel insurance successful
13445          -- set the transaction record
13446          set_transaction_rec (p_return_status       => l_return_status,
13447                               p_overall_status      => px_overall_status,
13448                               p_tmt_flag            => 'TMT_CANCEL_INSURANCE_YN',
13449                               p_tsu_code            => 'WORKING',
13450                               px_tcnv_rec           => px_tcnv_rec
13451                              );
13452       END IF;
13453 
13454       -- RMUNJULU Bug # 2484327. Added code to set the message stack
13455       -- Set all the success messages to message stack
13456       IF l_return_status = g_ret_sts_success AND l_msg_tbl.COUNT > 0
13457       THEN
13458          k := l_msg_tbl.FIRST;
13459 
13460          LOOP
13461             okl_api.set_message
13462                             (p_app_name          => g_app_name,
13463                              p_msg_name          => l_msg_tbl (k).msg_desc,
13464                              p_token1            => l_msg_tbl (k).msg_token1,
13465                              p_token1_value      => l_msg_tbl (k).msg_token1_value,
13466                              p_token2            => l_msg_tbl (k).msg_token2,
13467                              p_token2_value      => l_msg_tbl (k).msg_token2_value
13468                             );
13469             EXIT WHEN (k = l_msg_tbl.LAST);
13470             k := l_msg_tbl.NEXT (k);
13471          END LOOP;
13472       END IF;
13473 
13474       -- RMUNJULU CONTRACT BLOCKING : Now since update_lines can succeed and mass_rebook can hang
13475       -- Need to set TMT_CONTRACT_UPDATED_YN to 'E' if mass_rebook hangs so that it can be
13476       -- recycled again
13477       -- Remember : lx_mass_rebook_success will be 'S' if Mass_Rebook was not called
13478       -- Also : lx_mass_rebook_success will be 'E' if Mass_Rebook RBK TRN NOT PROCESSED
13479       --        or IF Mass_Rebook failed and it in that case it would have done rollback
13480       IF l_id <> -9999
13481       THEN
13482          -- When Mass Rebook was called
13483          --++++++++start -- CONTRACT BLOCKING (2)
13484          -- If Mass Rebook failed, It did not touch termination trn, so has to update term trn
13485          IF l_return_status <> g_ret_sts_success
13486          THEN
13487             set_transaction_rec (p_return_status       => l_return_status,
13488                                  p_overall_status      => px_overall_status,
13489                                  p_tmt_flag            => 'TMT_CONTRACT_UPDATED_YN',
13490                                  p_tsu_code            => 'ERROR',
13491                                  px_tcnv_rec           => px_tcnv_rec
13492                                 );
13493             -- update the transaction record
13494             process_transaction (p_id                 => 0,
13495                                  p_term_rec           => p_term_rec,
13496                                  p_tcnv_rec           => px_tcnv_rec,
13497                                  p_trn_mode           => 'UPDATE',
13498                                  x_id                 => lx_id,
13499                                  x_return_status      => l_return_status
13500                                 );
13501 
13502             IF (is_debug_statement_on)
13503             THEN
13504                okl_debug_pub.log_debug
13505                            (g_level_statement,
13506                             l_module_name,
13507                                'called process_transaction , return status: '
13508                             || l_return_status
13509                            );
13510             END IF;
13511 
13512             -- rollback if processing transaction failed
13513             IF (l_return_status = g_ret_sts_unexp_error)
13514             THEN
13515                RAISE okl_api.g_exception_unexpected_error;
13516             ELSIF (l_return_status = g_ret_sts_error)
13517             THEN
13518                RAISE okl_api.g_exception_error;
13519             END IF;
13520          ELSE
13521             -- Mass Rebook Till Streams was successful
13522 
13523             -- Whole mass rebook was successful, Mass Rebook will have updated the termination trn
13524             IF lx_mass_rebook_success = g_ret_sts_success
13525             THEN
13526                NULL;      --Mass Rebook will have updated the termination trn
13527 
13528                -- RMUNJULU 3485854 12-MAR-04
13529                -- get trn tsu_code, if not updated then we update here
13530                -- used when EVERGREEN and partial termination
13531                FOR get_trn_status_rec IN get_trn_status_csr (px_tcnv_rec.ID)
13532                LOOP
13533                   IF get_trn_status_rec.tmt_status_code <> 'PROCESSED'
13534                   THEN
13535                      set_transaction_rec
13536                                     (p_return_status       => l_return_status,
13537                                      p_overall_status      => px_overall_status,
13538                                      p_tmt_flag            => 'TMT_CONTRACT_UPDATED_YN',
13539                                      p_tsu_code            => 'PROCESSED',
13540                                      px_tcnv_rec           => px_tcnv_rec
13541                                     );
13542                      -- update the transaction record
13543                      process_transaction (p_id                 => 0,
13544                                           p_term_rec           => p_term_rec,
13545                                           p_tcnv_rec           => px_tcnv_rec,
13546                                           p_trn_mode           => 'UPDATE',
13547                                           x_id                 => lx_id,
13548                                           x_return_status      => l_return_status
13549                                          );
13550 
13551                      IF (is_debug_statement_on)
13552                      THEN
13553                         okl_debug_pub.log_debug
13554                            (g_level_statement,
13555                             l_module_name,
13556                                'called process_transaction , return status: '
13557                             || l_return_status
13558                            );
13559                      END IF;
13560 
13561                      -- rollback if processing transaction failed
13562                      IF (l_return_status = g_ret_sts_unexp_error)
13563                      THEN
13564                         RAISE okl_api.g_exception_unexpected_error;
13565                      ELSIF (l_return_status = g_ret_sts_error)
13566                      THEN
13567                         RAISE okl_api.g_exception_error;
13568                      END IF;
13569                   END IF;
13570                END LOOP;
13571             ELSE
13572                -- Mass Rebook hanged becos of stream, has not yet updated term trn, so update term trn
13573 
13574                -- Might need to set only tsu_code in this case ***
13575                -- then l_tcnv_rec.id and l_tcnv_rec.tsu_code should be set
13576                px_tcnv_rec.tmt_status_code := 'WORKING';
13577                             -- Since mass rebook hanged set status to WORKING
13578                -- update the transaction record
13579                process_transaction (p_id                 => 0,
13580                                     p_term_rec           => p_term_rec,
13581                                     p_tcnv_rec           => px_tcnv_rec,
13582                                     p_trn_mode           => 'UPDATE',
13583                                     x_id                 => lx_id,
13584                                     x_return_status      => l_return_status
13585                                    );
13586 
13587                IF (is_debug_statement_on)
13588                THEN
13589                   okl_debug_pub.log_debug
13590                            (g_level_statement,
13591                             l_module_name,
13592                                'called process_transaction , return status: '
13593                             || l_return_status
13594                            );
13595                END IF;
13596 
13597                -- rollback if processing transaction failed
13598                IF (l_return_status = g_ret_sts_unexp_error)
13599                THEN
13600                   RAISE okl_api.g_exception_unexpected_error;
13601                ELSIF (l_return_status = g_ret_sts_error)
13602                THEN
13603                   RAISE okl_api.g_exception_error;
13604                END IF;
13605             END IF;
13606          END IF;
13607       ELSE
13608          -- When Mass Rebook was not called ie NO MORE ASSETS ie NOT A TRUE PARTIAL TERMINATION
13609 
13610          -- At the point it would have already rolled back if there were an error
13611          -- so can assume that contract updation went thru successfully
13612          -- set the transaction record
13613          set_transaction_rec (p_return_status       => l_return_status,
13614                               p_overall_status      => px_overall_status,
13615                               p_tmt_flag            => 'TMT_CONTRACT_UPDATED_YN',
13616                               p_tsu_code            => 'PROCESSED',
13617                               px_tcnv_rec           => px_tcnv_rec
13618                              );
13619          -- update the transaction record
13620          process_transaction (p_id                 => 0,
13621                               p_term_rec           => p_term_rec,
13622                               p_tcnv_rec           => px_tcnv_rec,
13623                               p_trn_mode           => 'UPDATE',
13624                               x_id                 => lx_id,
13625                               x_return_status      => l_return_status
13626                              );
13627 
13628          IF (is_debug_statement_on)
13629          THEN
13630             okl_debug_pub.log_debug
13631                            (g_level_statement,
13632                             l_module_name,
13633                                'called process_transaction , return status: '
13634                             || l_return_status
13635                            );
13636          END IF;
13637 
13638          -- rollback if processing transaction failed
13639          IF (l_return_status = g_ret_sts_unexp_error)
13640          THEN
13641             RAISE okl_api.g_exception_unexpected_error;
13642          ELSIF (l_return_status = g_ret_sts_error)
13643          THEN
13644             RAISE okl_api.g_exception_error;
13645          END IF;
13646       END IF;
13647 
13648       --+++++++++++++end  -- CONTRACT BLOCKING (2)
13649 
13650       -- Set the return status
13651       x_return_status := l_return_status;
13652  -- will be 'S' always SHOULD BE OK SINCE THIS IS NOT CONSIDERED GOING FURTHER
13653 
13654       IF (is_debug_procedure_on)
13655       THEN
13656          okl_debug_pub.log_debug (g_level_procedure,
13657                                   l_module_name,
13658                                   'End(-)'
13659                                  );
13660       END IF;
13661    EXCEPTION
13662       WHEN okl_api.g_exception_error
13663       THEN
13664          -- RMUNJULU Bug # 2484327 Added IF
13665          IF get_k_lines_csr%ISOPEN
13666          THEN
13667             CLOSE get_k_lines_csr;
13668          END IF;
13669 
13670          ROLLBACK TO terminate_lines;
13671          x_return_status := g_ret_sts_error;
13672          -- RMUNJULU CONTRACT BLOCKING -- Moved msg to body above
13673 
13674          -- set the transaction record
13675          set_transaction_rec (p_return_status       => x_return_status,
13676                               p_overall_status      => px_overall_status,
13677                               p_tmt_flag            => 'TMT_CONTRACT_UPDATED_YN',
13678                               p_tsu_code            => 'ERROR',
13679                               px_tcnv_rec           => px_tcnv_rec
13680                              );
13681 
13682          IF (is_debug_exception_on)
13683          THEN
13684             okl_debug_pub.log_debug (g_level_exception,
13685                                      l_module_name,
13686                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
13687                                     );
13688          END IF;
13689       WHEN okl_api.g_exception_unexpected_error
13690       THEN
13691          -- RMUNJULU Bug # 2484327 Added IF
13692          IF get_k_lines_csr%ISOPEN
13693          THEN
13694             CLOSE get_k_lines_csr;
13695          END IF;
13696 
13697          ROLLBACK TO terminate_lines;
13698          x_return_status := g_ret_sts_unexp_error;
13699          -- RMUNJULU CONTRACT BLOCKING -- Moved msg to body above
13700 
13701          -- set the transaction record
13702          set_transaction_rec (p_return_status       => x_return_status,
13703                               p_overall_status      => px_overall_status,
13704                               p_tmt_flag            => 'TMT_CONTRACT_UPDATED_YN',
13705                               p_tsu_code            => 'ERROR',
13706                               px_tcnv_rec           => px_tcnv_rec
13707                              );
13708 
13709          IF (is_debug_exception_on)
13710          THEN
13711             okl_debug_pub.log_debug (g_level_exception,
13712                                      l_module_name,
13713                                         'EXCEPTION :'
13714                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
13715                                     );
13716          END IF;
13717       WHEN OTHERS
13718       THEN
13719          -- RMUNJULU Bug # 2484327 Added IF
13720          IF get_k_lines_csr%ISOPEN
13721          THEN
13722             CLOSE get_k_lines_csr;
13723          END IF;
13724 
13725          ROLLBACK TO terminate_lines;
13726          x_return_status := g_ret_sts_unexp_error;
13727          -- RMUNJULU CONTRACT BLOCKING -- Moved msg to body above
13728 
13729          -- set the transaction record
13730          set_transaction_rec (p_return_status       => x_return_status,
13731                               p_overall_status      => px_overall_status,
13732                               p_tmt_flag            => 'TMT_CONTRACT_UPDATED_YN',
13733                               p_tsu_code            => 'ERROR',
13734                               px_tcnv_rec           => px_tcnv_rec
13735                              );
13736          -- Set the oracle error message
13737          okl_api.set_message (p_app_name          => g_app_name_1,
13738                               p_msg_name          => g_unexpected_error,
13739                               p_token1            => g_sqlcode_token,
13740                               p_token1_value      => SQLCODE,
13741                               p_token2            => g_sqlerrm_token,
13742                               p_token2_value      => SQLERRM
13743                              );
13744 
13745          IF (is_debug_exception_on)
13746          THEN
13747             okl_debug_pub.log_debug (g_level_exception,
13748                                      l_module_name,
13749                                         'EXCEPTION :'
13750                                      || 'OTHERS, SQLCODE: '
13751                                      || SQLCODE
13752                                      || ' , SQLERRM : '
13753                                      || SQLERRM
13754                                     );
13755          END IF;
13756    END terminate_lines;
13757 
13758    -- Start of comments
13759    --
13760    -- Procedure Name : asset_level_termination
13761    -- Desciption     : Use this API to terminate lines for
13762    --                  asset level termination
13763    -- Business Rules :
13764    -- Parameters     :
13765    -- Version        : 1.0
13766    -- History        : RMUNJULU 20-DEC-02 2484327 Removed validation message
13767    --                : RMUNJULU 02-JAN-03 2724951 Added code to check lease or loan
13768    --                  and processing accordingly
13769    --                : RMUNJULU 07-JAN-03 2736865 Added code to store messages
13770    --                  and set stack to true
13771    --                : RMUNJULU 23-JAN-03 2762065 Added code to set msg stack
13772    --                  before split asset call
13773    --                : RMUNJULU 31-JAN-03 2780539 Added TER_MAN_PURCHASE check
13774    --                : RMUNJULU 04-FEB-03 2781557 Added code to set trn to
13775    --                  WORKING after validation
13776    --                : RMUNJULU 28-MAR-03 2877278 Removed call to close_streams
13777    --                  as it moves to Terminate_Lines
13778    --                : RMUNJULU 3061751  Changed code to create a termination
13779    --                  trn even when request is NON BATCH and validation has failed
13780    --                : RMUNJULU 3061751 Changed l_return_status to l_validate in set_trn_rec
13781    --                  setting the value for tmt_validated_yn properly
13782    --                : RMUNJULU 2730783 Use OKL_AM_BTCH_EXP_LEASE_LOAN_PVT.POP_ASSET_MSG_TBL
13783    --                  instead of log_messages to set messages into the POP_ASSET_MSG_TBL
13784    --                  of OKL_AM_BTCH_EXP_LEASE_LOAN_PVT which will be used in the output
13785    --                  displayed from concurrent program
13786    --                : RMUNJULU 3018641 Added code to get and set TMG_RUN on OKL_TRX_MSGS
13787    --                : RMUNJULU 3018641 Added code to get and set TMG_RUN on OKL_TRX_MSGS  in one more place
13788    --                : RMUNJULU CONTRACT BLOCKING : CHANGED CALL TO TERMINATE_LINES
13789    --                  and added condition for validate_k_and_lines
13790    --                : RMUNJULU CONTRACT BLOCKING (2) Changed to update termination trn
13791    --                  If Overall status not successful or if mass_rebook was not called
13792    --                : RMUNJULU CONTRACT BLOCKING (3) -- Added NVLs to Validate condition
13793    --                : rmunjulu EDAT Added code to get quote eff dates and set them as global
13794    -- End of comments
13795    PROCEDURE asset_level_termination (
13796       p_api_version     IN              NUMBER,
13797       p_init_msg_list   IN              VARCHAR2,
13798       p_term_rec        IN              term_rec_type,
13799       p_tcnv_rec        IN              tcnv_rec_type,
13800       x_msg_count       OUT NOCOPY      NUMBER,
13801       x_msg_data        OUT NOCOPY      VARCHAR2,
13802       x_return_status   OUT NOCOPY      VARCHAR2
13803    )
13804    IS
13805       l_return_status          VARCHAR2 (1)   := g_ret_sts_success;
13806       l_overall_status         VARCHAR2 (1)   := g_ret_sts_success;
13807       lp_tcnv_rec              tcnv_rec_type;
13808       lp_klev_tbl              klev_tbl_type;
13809       lx_klev_tbl              klev_tbl_type;
13810       lx_id                    NUMBER;
13811       i                        NUMBER         := 1;
13812       l_tran_started           VARCHAR2 (1)   := g_false;
13813       l_evergreen_status       VARCHAR2 (1)   := g_false;
13814       l_api_name               VARCHAR2 (30)  := 'asset_level_termination';
13815       l_module_name            VARCHAR2 (500)
13816                                 := g_module_name || 'asset_level_termination';
13817       is_debug_exception_on    BOOLEAN
13818              := okl_debug_pub.check_log_on (l_module_name, g_level_exception);
13819       is_debug_procedure_on    BOOLEAN
13820              := okl_debug_pub.check_log_on (l_module_name, g_level_procedure);
13821       is_debug_statement_on    BOOLEAN
13822              := okl_debug_pub.check_log_on (l_module_name, g_level_statement);
13823       l_sys_date               DATE;
13824       l_trn_already_set        VARCHAR2 (1)   := g_no;
13825       lx_contract_status       VARCHAR2 (200);
13826       l_validate               VARCHAR2 (1)   := g_ret_sts_error;
13827       l_api_version   CONSTANT NUMBER         := 1;
13828       l_status                 VARCHAR2 (200);
13829       l_term_rec               term_rec_type  := p_term_rec;
13830       l_lease_or_loan          VARCHAR2 (30);
13831    BEGIN
13832       IF (is_debug_procedure_on)
13833       THEN
13834          okl_debug_pub.log_debug (g_level_procedure,
13835                                   l_module_name,
13836                                   'Begin(+)'
13837                                  );
13838       END IF;
13839 
13840       IF (is_debug_statement_on)
13841       THEN
13842          okl_debug_pub.log_debug (g_level_statement,
13843                                   l_module_name,
13844                                      'In param, p_term_rec.p_quote_id: '
13845                                   || p_term_rec.p_quote_id
13846                                  );
13847          okl_debug_pub.log_debug (g_level_statement,
13848                                   l_module_name,
13849                                      'In param, p_term_rec.p_contract_id: '
13850                                   || p_term_rec.p_contract_id
13851                                  );
13852          okl_debug_pub.log_debug
13853                                 (g_level_statement,
13854                                  l_module_name,
13855                                     'In param, p_term_rec.p_contract_number: '
13856                                  || p_term_rec.p_contract_number
13857                                 );
13858          okl_debug_pub.log_debug (g_level_statement,
13859                                   l_module_name,
13860                                      'In param, p_term_rec.p_quote_type: '
13861                                   || p_term_rec.p_quote_type
13862                                  );
13863          okl_debug_pub.log_debug
13864                            (g_level_statement,
13865                             l_module_name,
13866                                'In param, p_term_rec.p_early_termination_yn: '
13867                             || p_term_rec.p_early_termination_yn
13868                            );
13869          okl_debug_pub.log_debug
13870                                (g_level_statement,
13871                                 l_module_name,
13872                                    'In param, p_term_rec.p_termination_date: '
13873                                 || p_term_rec.p_termination_date
13874                                );
13875          okl_debug_pub.log_debug
13876                              (g_level_statement,
13877                               l_module_name,
13878                                  'In param, p_tcnv_rec.tmt_generic_flag2_yn: '
13879                               || p_tcnv_rec.tmt_generic_flag2_yn
13880                              );
13881          okl_debug_pub.log_debug (g_level_statement,
13882                                   l_module_name,
13883                                   'In param, p_tcnv_rec.id: ' || p_tcnv_rec.ID
13884                                  );
13885          okl_debug_pub.log_debug
13886                           (g_level_statement,
13887                            l_module_name,
13888                               'In param, p_tcnv_rec.tmt_contract_updated_yn: '
13889                            || p_tcnv_rec.tmt_contract_updated_yn
13890                           );
13891          okl_debug_pub.log_debug (g_level_statement,
13892                                   l_module_name,
13893                                      'In param, p_tcnv_rec.tmt_status_code: '
13894                                   || p_tcnv_rec.tmt_status_code
13895                                  );
13896       END IF;
13897 
13898       -- RMUNJULU 23-JAN-03 2762065 -- Added comments to explain msgs manipulation
13899       -- Issue with msgs and msg stack is resolved the following way
13900       -- Split asset initializes msg stack so log + process + initialize msgs before
13901       -- split asset
13902       -- Mass rebook initializes msg stack so log + process + initialize msgs before
13903       -- terminate lines
13904       -- There was a concern of msgs being lost which are set in update lines if mass
13905       -- rebook is done after it and is sucessful, but that is not a concern since
13906       -- we are not storing the update lines msgs on stack but in msg_tbl and
13907       -- putting them on stack only if every step in terminate line is sucessful
13908 
13909       -- Set the transaction
13910       l_return_status :=
13911          okl_api.start_activity (l_api_name,
13912                                  g_pkg_name,
13913                                  p_init_msg_list,
13914                                  l_api_version,
13915                                  p_api_version,
13916                                  '_PVT',
13917                                  x_return_status
13918                                 );
13919 
13920       -- Rollback if error setting activity for api
13921       IF (l_return_status = g_ret_sts_unexp_error)
13922       THEN
13923          RAISE okl_api.g_exception_unexpected_error;
13924       ELSIF (l_return_status = g_ret_sts_error)
13925       THEN
13926          RAISE okl_api.g_exception_error;
13927       END IF;
13928 
13929       -- store the highest degree of error
13930       set_overall_status (p_return_status        => l_return_status,
13931                           px_overall_status      => l_overall_status);
13932       -- If the termination request is from quote
13933       -- populate the rest of the quote attributes
13934       set_database_values (px_term_rec      => l_term_rec);
13935       -- Set the info messages intially
13936       set_info_messages (p_term_rec      => l_term_rec);
13937 
13938       -- check if transaction already exists
13939       IF (p_tcnv_rec.ID IS NOT NULL AND p_tcnv_rec.ID <> g_miss_num)
13940       THEN
13941          l_trn_already_set := g_yes;
13942       END IF;
13943 
13944       --get sysdate
13945       SELECT SYSDATE
13946         INTO l_sys_date
13947         FROM DUAL;
13948 
13949       -- If the transaction is not already set then initialize and insert
13950       IF l_trn_already_set = g_no
13951       THEN
13952          -- initialize the transaction rec
13953          initialize_transaction (p_term_rec           => l_term_rec,
13954                                  p_sys_date           => l_sys_date,
13955                                  p_control_flag       => 'CREATE',
13956                                  px_tcnv_rec          => lp_tcnv_rec,
13957                                  x_return_status      => l_return_status
13958                                 );
13959 
13960          IF (is_debug_statement_on)
13961          THEN
13962             okl_debug_pub.log_debug
13963                         (g_level_statement,
13964                          l_module_name,
13965                             'called initialize_transaction , return status: '
13966                          || l_return_status
13967                         );
13968          END IF;
13969 
13970          -- rollback if intialize transaction failed
13971          IF (l_return_status = g_ret_sts_unexp_error)
13972          THEN
13973             RAISE okl_api.g_exception_unexpected_error;
13974          ELSIF (l_return_status = g_ret_sts_error)
13975          THEN
13976             RAISE okl_api.g_exception_error;
13977          END IF;
13978 
13979          -- insert the transaction record
13980          process_transaction (p_id                 => 0,
13981                               p_term_rec           => l_term_rec,
13982                               p_tcnv_rec           => lp_tcnv_rec,
13983                               p_trn_mode           => 'INSERT',
13984                               x_id                 => lx_id,
13985                               x_return_status      => l_return_status
13986                              );
13987 
13988          IF (is_debug_statement_on)
13989          THEN
13990             okl_debug_pub.log_debug
13991                            (g_level_statement,
13992                             l_module_name,
13993                                'called process_transaction , return status: '
13994                             || l_return_status
13995                            );
13996          END IF;
13997 
13998          -- rollback if processing transaction failed
13999          IF (l_return_status = g_ret_sts_unexp_error)
14000          THEN
14001             RAISE okl_api.g_exception_unexpected_error;
14002          ELSIF (l_return_status = g_ret_sts_error)
14003          THEN
14004             RAISE okl_api.g_exception_error;
14005          END IF;
14006 
14007          -- set the trn rec id
14008          lp_tcnv_rec.ID := lx_id;
14009       ELSE
14010          -- transaction already set
14011          lp_tcnv_rec := p_tcnv_rec;
14012          -- initialize the transaction rec
14013          initialize_transaction (p_term_rec           => l_term_rec,
14014                                  p_sys_date           => l_sys_date,
14015                                  p_control_flag       => 'UPDATE',
14016                                  px_tcnv_rec          => lp_tcnv_rec,
14017                                  x_return_status      => l_return_status
14018                                 );
14019 
14020          IF (is_debug_statement_on)
14021          THEN
14022             okl_debug_pub.log_debug
14023                         (g_level_statement,
14024                          l_module_name,
14025                             'called initialize_transaction , return status: '
14026                          || l_return_status
14027                         );
14028          END IF;
14029 
14030          -- rollback if intialize transaction failed
14031          IF (l_return_status = g_ret_sts_unexp_error)
14032          THEN
14033             RAISE okl_api.g_exception_unexpected_error;
14034          ELSIF (l_return_status = g_ret_sts_error)
14035          THEN
14036             RAISE okl_api.g_exception_error;
14037          END IF;
14038       END IF;
14039 
14040       -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
14041 
14042       -- rmunjulu EDAT Get the quote effectivity date and quote acceptance date
14043       -- and store as global variables, will be used later on in other procedures
14044       IF (is_debug_statement_on)
14045       THEN
14046          okl_debug_pub.log_debug
14047                    (g_level_statement,
14048                     l_module_name,
14049                     'calling OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_quote_dates'
14050                    );
14051       END IF;
14052 
14053       okl_am_lease_loan_trmnt_pvt.get_set_quote_dates
14054                                            (p_qte_id             => l_term_rec.p_quote_id,
14055                                             x_return_status      => l_return_status);
14056 
14057       IF (is_debug_statement_on)
14058       THEN
14059          okl_debug_pub.log_debug
14060             (g_level_statement,
14061              l_module_name,
14062                 'called OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_quote_dates , return status: '
14063              || l_return_status
14064             );
14065       END IF;
14066 
14067       -- Rollback if error setting activity for api
14068       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
14069       THEN
14070          RAISE okl_api.g_exception_unexpected_error;
14071       ELSIF (l_return_status = okl_api.g_ret_sts_error)
14072       THEN
14073          RAISE okl_api.g_exception_error;
14074       END IF;
14075 
14076       -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
14077 
14078       -- get the lines
14079       get_lines (p_term_rec           => l_term_rec,
14080                  x_klev_tbl           => lp_klev_tbl,
14081                  x_return_status      => l_return_status
14082                 );
14083 
14084       IF (is_debug_statement_on)
14085       THEN
14086          okl_debug_pub.log_debug (g_level_statement,
14087                                   l_module_name,
14088                                      'called get_lines , return status: '
14089                                   || l_return_status
14090                                  );
14091       END IF;
14092 
14093       -- RMUNJULU CONTRACT BLOCKING Do not validate if update_lines done and only mass_rebook pending
14094       -- RMUNJULU CONTRACT BLOCKING (3) -- Added NVLs
14095       IF     NVL (lp_tcnv_rec.tmt_contract_updated_yn, 'N') = 'N'
14096          AND NVL (lp_tcnv_rec.tmt_generic_flag2_yn, 'N') = 'Y'
14097       THEN
14098          l_return_status := g_ret_sts_success;
14099       ELSE
14100          -- Not a Mass rebook recycle, so do validate
14101 
14102          -- check if lease and lines valid
14103          validate_contract_and_lines (p_term_rec           => l_term_rec,
14104                                       p_sys_date           => l_sys_date,
14105                                       p_klev_tbl           => lp_klev_tbl,
14106                                       x_return_status      => l_return_status
14107                                      );
14108 
14109          IF (is_debug_statement_on)
14110          THEN
14111             okl_debug_pub.log_debug
14112                    (g_level_statement,
14113                     l_module_name,
14114                        'called validate_contract_and_lines , return status: '
14115                     || l_return_status
14116                    );
14117          END IF;
14118       END IF;
14119 
14120       -- Store the validation return status
14121       l_validate := l_return_status;
14122       -- store the highest degree of error
14123       set_overall_status (p_return_status        => l_return_status,
14124                           px_overall_status      => l_overall_status);
14125 
14126       IF (l_term_rec.p_control_flag = 'BATCH_PROCESS')
14127       THEN
14128          -- Since batch process is not checked initially in LLT check here
14129          IF (is_debug_statement_on)
14130          THEN
14131             okl_debug_pub.log_debug
14132                      (g_level_statement,
14133                       l_module_name,
14134                       'calling OKL_AM_LEASE_LOAN_TRMNT_PUB.validate_contract'
14135                      );
14136          END IF;
14137 
14138          okl_am_lease_loan_trmnt_pub.validate_contract
14139                                  (p_api_version          => p_api_version,
14140                                   p_init_msg_list        => g_false,
14141                                   x_return_status        => l_return_status,
14142                                   x_msg_count            => x_msg_count,
14143                                   x_msg_data             => x_msg_data,
14144                                   p_contract_id          => l_term_rec.p_contract_id,
14145                                   p_control_flag         => l_term_rec.p_control_flag,
14146                                   x_contract_status      => lx_contract_status
14147                                  );
14148 
14149          IF (is_debug_statement_on)
14150          THEN
14151             okl_debug_pub.log_debug
14152                (g_level_statement,
14153                 l_module_name,
14154                    'called OKL_AM_LEASE_LOAN_TRMNT_PUB.validate_contract , return status: '
14155                 || l_return_status
14156                );
14157          END IF;
14158 
14159          -- Store the highest validation return status
14160          -- To capture the return status of validate lease called above
14161          IF (l_validate = g_ret_sts_success)
14162          THEN
14163             l_validate := l_return_status;
14164          END IF;
14165 
14166          -- store the highest degree of error
14167          set_overall_status
14168             (p_return_status        => l_validate,
14169                               -- RMUNJULU 3061751 Changed from l_return_status
14170              px_overall_status      => l_overall_status);
14171          -- set the transaction record
14172          set_transaction_rec
14173             (p_return_status       => l_validate,
14174                               -- RMUNJULU 3061751 Changed from l_return_status
14175              p_overall_status      => l_overall_status,
14176              p_tmt_flag            => 'TMT_VALIDATED_YN',
14177              p_tsu_code            => 'ENTERED',
14178              px_tcnv_rec           => lp_tcnv_rec
14179             );
14180 
14181          -- if validation failed then insert transaction
14182          -- AND abort else continue next process
14183          IF (l_validate <> g_ret_sts_success)
14184          THEN
14185             -- set the transaction record
14186             set_transaction_rec
14187                (p_return_status       => l_validate,
14188                               -- RMUNJULU 3061751 Changed from l_return_status
14189                 p_overall_status      => l_overall_status,
14190                 p_tmt_flag            => 'TMT_VALIDATED_YN',
14191                 p_tsu_code            => 'ERROR',
14192                 px_tcnv_rec           => lp_tcnv_rec
14193                );
14194             -- update the transaction record
14195             process_transaction (p_id                 => 0,
14196                                  p_term_rec           => l_term_rec,
14197                                  p_tcnv_rec           => lp_tcnv_rec,
14198                                  p_trn_mode           => 'UPDATE',
14199                                  x_id                 => lx_id,
14200                                  x_return_status      => l_return_status
14201                                 );
14202 
14203             IF (is_debug_statement_on)
14204             THEN
14205                okl_debug_pub.log_debug
14206                            (g_level_statement,
14207                             l_module_name,
14208                                'called process_transaction , return status: '
14209                             || l_return_status
14210                            );
14211             END IF;
14212 
14213             -- rollback if processing transaction failed
14214             IF (l_return_status = g_ret_sts_unexp_error)
14215             THEN
14216                RAISE okl_api.g_exception_unexpected_error;
14217             ELSIF (l_return_status = g_ret_sts_error)
14218             THEN
14219                RAISE okl_api.g_exception_error;
14220             END IF;
14221 
14222             -- Save messages from stack into transaction message table
14223             IF (is_debug_statement_on)
14224             THEN
14225                okl_debug_pub.log_debug
14226                                   (g_level_statement,
14227                                    l_module_name,
14228                                    'calling OKL_AM_UTIL_PVT.process_messages'
14229                                   );
14230             END IF;
14231 
14232             okl_am_util_pvt.process_messages
14233                                    (p_trx_source_table      => 'OKL_TRX_CONTRACTS',
14234                                     p_trx_id                => lp_tcnv_rec.ID,
14235                                     x_return_status         => l_return_status
14236                                    );
14237 
14238             IF (is_debug_statement_on)
14239             THEN
14240                okl_debug_pub.log_debug
14241                   (g_level_statement,
14242                    l_module_name,
14243                       'called OKL_AM_UTIL_PVT.process_messages , return status: '
14244                    || l_return_status
14245                   );
14246             END IF;
14247 
14248             -- RMUNJULU 3018641 Added code to get and set TMG_RUN
14249             IF (is_debug_statement_on)
14250             THEN
14251                okl_debug_pub.log_debug
14252                        (g_level_statement,
14253                         l_module_name,
14254                         'calling OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run'
14255                        );
14256             END IF;
14257 
14258             okl_am_lease_loan_trmnt_pvt.get_set_tmg_run
14259                                            (p_trx_id             => lp_tcnv_rec.ID,
14260                                             x_return_status      => l_return_status);
14261 
14262             IF (is_debug_statement_on)
14263             THEN
14264                okl_debug_pub.log_debug
14265                   (g_level_statement,
14266                    l_module_name,
14267                       'called OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run , return status: '
14268                    || l_return_status
14269                   );
14270             END IF;
14271 
14272             -- abort since validation failed
14273             RAISE g_exception_halt_validation;
14274          END IF;
14275       ELSE
14276          --( not from batch process) then
14277 
14278          -- RMUNJULU 3061751 Changed this code to create a termination trn even when
14279          -- request is NON BATCH and validation has failed
14280          IF l_validate <> g_ret_sts_success
14281          THEN
14282             -- set the transaction record
14283             set_transaction_rec
14284                (p_return_status       => l_validate,
14285                              -- RMUNJULU 3061751 Changed from l_return_status,
14286                 p_overall_status      => l_overall_status,
14287                 p_tmt_flag            => 'TMT_VALIDATED_YN',
14288                 p_tsu_code            => 'ERROR',
14289                 px_tcnv_rec           => lp_tcnv_rec
14290                );
14291             -- update the transaction record
14292             process_transaction (p_id                 => 0,
14293                                  p_term_rec           => l_term_rec,
14294                                  p_tcnv_rec           => lp_tcnv_rec,
14295                                  p_trn_mode           => 'UPDATE',
14296                                  x_id                 => lx_id,
14297                                  x_return_status      => l_return_status
14298                                 );
14299 
14300             IF (is_debug_statement_on)
14301             THEN
14302                okl_debug_pub.log_debug
14303                            (g_level_statement,
14304                             l_module_name,
14305                                'called process_transaction , return status: '
14306                             || l_return_status
14307                            );
14308             END IF;
14309 
14310             -- rollback if processing transaction failed
14311             IF (l_return_status = g_ret_sts_unexp_error)
14312             THEN
14313                RAISE okl_api.g_exception_unexpected_error;
14314             ELSIF (l_return_status = g_ret_sts_error)
14315             THEN
14316                RAISE okl_api.g_exception_error;
14317             END IF;
14318 
14319             -- Save messages from stack into transaction message table
14320             IF (is_debug_statement_on)
14321             THEN
14322                okl_debug_pub.log_debug
14323                                   (g_level_statement,
14324                                    l_module_name,
14325                                    'calling OKL_AM_UTIL_PVT.process_messages'
14326                                   );
14327             END IF;
14328 
14329             okl_am_util_pvt.process_messages
14330                                    (p_trx_source_table      => 'OKL_TRX_CONTRACTS',
14331                                     p_trx_id                => lp_tcnv_rec.ID,
14332                                     x_return_status         => l_return_status
14333                                    );
14334 
14335             IF (is_debug_statement_on)
14336             THEN
14337                okl_debug_pub.log_debug
14338                   (g_level_statement,
14339                    l_module_name,
14340                       'called OKL_AM_UTIL_PVT.process_messages , return status: '
14341                    || l_return_status
14342                   );
14343             END IF;
14344 
14345             -- RMUNJULU 3018641 Added code to get and set TMG_RUN
14346             IF (is_debug_statement_on)
14347             THEN
14348                okl_debug_pub.log_debug
14349                        (g_level_statement,
14350                         l_module_name,
14351                         'calling OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run'
14352                        );
14353             END IF;
14354 
14355             okl_am_lease_loan_trmnt_pvt.get_set_tmg_run
14356                                            (p_trx_id             => lp_tcnv_rec.ID,
14357                                             x_return_status      => l_return_status);
14358 
14359             IF (is_debug_statement_on)
14360             THEN
14361                okl_debug_pub.log_debug
14362                   (g_level_statement,
14363                    l_module_name,
14364                       'called OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run , return status: '
14365                    || l_return_status
14366                   );
14367             END IF;
14368 
14369             -- abort since validation failed
14370             RAISE g_exception_halt_validation;
14371          ELSE
14372             -- Validate was successful -- RMUNJULU CONTRACT BLOCKING -- Validate flag was not set properly earlier
14373 
14374             -- set the transaction record
14375             set_transaction_rec (p_return_status       => l_validate,
14376                                  p_overall_status      => l_overall_status,
14377                                  p_tmt_flag            => 'TMT_VALIDATED_YN',
14378                                  p_tsu_code            => 'ENTERED',
14379                                  px_tcnv_rec           => lp_tcnv_rec
14380                                 );
14381          END IF;
14382       END IF;
14383 
14384       -- RMUNJULU 23-JAN-03 2762065  -- START
14385       -- Added this code to store msgs in log and tbl since split asset setting the
14386       -- msg stack again
14387 
14388       -- If batch process then log the messages from stack
14389       IF p_term_rec.p_control_flag LIKE 'BATCH%'
14390       THEN
14391          -- RMUNJULU 2730738 for proper output file
14392          okl_am_btch_exp_lease_loan_pvt.pop_asset_msg_tbl;    --log_messages;
14393       END IF;
14394 
14395       -- Store messages in TRX_MSGS
14396       IF (is_debug_statement_on)
14397       THEN
14398          okl_debug_pub.log_debug (g_level_statement,
14399                                   l_module_name,
14400                                   'calling OKL_AM_UTIL_PVT.process_messages'
14401                                  );
14402       END IF;
14403 
14404       okl_am_util_pvt.process_messages
14405                                    (p_trx_source_table      => 'OKL_TRX_CONTRACTS',
14406                                     p_trx_id                => lp_tcnv_rec.ID,
14407                                     x_return_status         => l_return_status
14408                                    );
14409 
14410       IF (is_debug_statement_on)
14411       THEN
14412          okl_debug_pub.log_debug
14413               (g_level_statement,
14414                l_module_name,
14415                   'called OKL_AM_UTIL_PVT.process_messages , return status: '
14416                || l_return_status
14417               );
14418       END IF;
14419 
14420       -- Set message stack to true
14421       okl_api.init_msg_list (g_true);
14422       -- RMUNJULU 23-JAN-03 2762065  -- END
14423 
14424       -- RMUNJULU 04-FEB-03 2781557 -- START
14425 
14426       -- Update the transaction to WORKING before starting other steps
14427       lp_tcnv_rec.tmt_status_code := 'WORKING';
14428                                  --akrangan changes for sla tmt_status_code cr
14429       -- update the transaction record with tsu_code = WORKING
14430       process_transaction (p_id                 => 0,
14431                            p_term_rec           => l_term_rec,
14432                            p_tcnv_rec           => lp_tcnv_rec,
14433                            p_trn_mode           => 'UPDATE',
14434                            x_id                 => lx_id,
14435                            x_return_status      => l_return_status
14436                           );
14437 
14438       IF (is_debug_statement_on)
14439       THEN
14440          okl_debug_pub.log_debug
14441                            (g_level_statement,
14442                             l_module_name,
14443                                'called process_transaction , return status: '
14444                             || l_return_status
14445                            );
14446       END IF;
14447 
14448       -- rollback if process transaction failed
14449       IF (l_return_status = g_ret_sts_unexp_error)
14450       THEN
14451          RAISE okl_api.g_exception_unexpected_error;
14452       ELSIF (l_return_status = g_ret_sts_error)
14453       THEN
14454          RAISE okl_api.g_exception_error;
14455       END IF;
14456 
14457       -- RMUNJULU 04-FEB-03 2781557 -- END
14458 
14459       -- do asset split
14460       split_asset (p_term_rec             => l_term_rec,
14461                    p_sys_date             => l_sys_date,
14462                    p_klev_tbl             => lp_klev_tbl,
14463                    p_trn_already_set      => l_trn_already_set,
14464                    px_overall_status      => l_overall_status,
14465                    px_tcnv_rec            => lp_tcnv_rec,
14466                    x_klev_tbl             => lx_klev_tbl,
14467                    x_return_status        => l_return_status
14468                   );
14469 
14470       IF (is_debug_statement_on)
14471       THEN
14472          okl_debug_pub.log_debug (g_level_statement,
14473                                   l_module_name,
14474                                      'called split_asset , return status: '
14475                                   || l_return_status
14476                                  );
14477       END IF;
14478 
14479       -- Log error and exit if split asset fails
14480       IF l_return_status <> g_ret_sts_success
14481       THEN
14482          -- update the transaction record
14483          process_transaction (p_id                 => 0,
14484                               p_term_rec           => l_term_rec,
14485                               p_tcnv_rec           => lp_tcnv_rec,
14486                               p_trn_mode           => 'UPDATE',
14487                               x_id                 => lx_id,
14488                               x_return_status      => l_return_status
14489                              );
14490 
14491          IF (is_debug_statement_on)
14492          THEN
14493             okl_debug_pub.log_debug
14494                            (g_level_statement,
14495                             l_module_name,
14496                                'called process_transaction , return status: '
14497                             || l_return_status
14498                            );
14499          END IF;
14500 
14501          -- rollback if processing transaction failed
14502          IF (l_return_status = g_ret_sts_unexp_error)
14503          THEN
14504             RAISE okl_api.g_exception_unexpected_error;
14505          ELSIF (l_return_status = g_ret_sts_error)
14506          THEN
14507             RAISE okl_api.g_exception_error;
14508          END IF;
14509 
14510          -- RMUNJULU 2730738 for proper output file
14511          IF p_term_rec.p_control_flag LIKE 'BATCH%'
14512          THEN
14513             okl_am_btch_exp_lease_loan_pvt.pop_asset_msg_tbl;
14514          END IF;
14515 
14516          -- Save messages from stack into transaction message table
14517          IF (is_debug_statement_on)
14518          THEN
14519             okl_debug_pub.log_debug
14520                                   (g_level_statement,
14521                                    l_module_name,
14522                                    'calling OKL_AM_UTIL_PVT.process_messages'
14523                                   );
14524          END IF;
14525 
14526          okl_am_util_pvt.process_messages
14527                                    (p_trx_source_table      => 'OKL_TRX_CONTRACTS',
14528                                     p_trx_id                => lp_tcnv_rec.ID,
14529                                     x_return_status         => l_return_status
14530                                    );
14531 
14532          IF (is_debug_statement_on)
14533          THEN
14534             okl_debug_pub.log_debug
14535                (g_level_statement,
14536                 l_module_name,
14537                    'called OKL_AM_UTIL_PVT.process_messages , return status: '
14538                 || l_return_status
14539                );
14540          END IF;
14541 
14542          -- RMUNJULU 3018641 Added to get and set latest run - 02-OCT-03
14543          IF (is_debug_statement_on)
14544          THEN
14545             okl_debug_pub.log_debug
14546                        (g_level_statement,
14547                         l_module_name,
14548                         'calling OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run'
14549                        );
14550          END IF;
14551 
14552          okl_am_lease_loan_trmnt_pvt.get_set_tmg_run
14553                                            (p_trx_id             => lp_tcnv_rec.ID,
14554                                             x_return_status      => l_return_status);
14555 
14556          IF (is_debug_statement_on)
14557          THEN
14558             okl_debug_pub.log_debug
14559                (g_level_statement,
14560                 l_module_name,
14561                    'called OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run , return status: '
14562                 || l_return_status
14563                );
14564          END IF;
14565 
14566          -- exit since split asset failed
14567          RAISE g_exception_halt_validation;
14568       END IF;
14569 
14570       -- do accounting entries
14571       accounting_entries (p_term_rec             => l_term_rec,
14572                           p_sys_date             => l_sys_date,
14573                           p_klev_tbl             => lx_klev_tbl,
14574                           p_trn_already_set      => l_trn_already_set,
14575                           px_overall_status      => l_overall_status,
14576                           px_tcnv_rec            => lp_tcnv_rec,
14577                           x_return_status        => l_return_status
14578                          );
14579 
14580       IF (is_debug_statement_on)
14581       THEN
14582          okl_debug_pub.log_debug
14583                             (g_level_statement,
14584                              l_module_name,
14585                                 'called accounting_entries , return status: '
14586                              || l_return_status
14587                             );
14588       END IF;
14589 
14590       -- RMUNJULU 02-JAN-03 2724951 Added code to get lease or loan
14591       -- Check if lease or loan
14592       check_lease_or_loan (p_khr_id               => l_term_rec.p_contract_id,
14593                            x_lease_loan_type      => l_lease_or_loan);
14594 
14595       IF (is_debug_statement_on)
14596       THEN
14597          okl_debug_pub.log_debug
14598                          (g_level_statement,
14599                           l_module_name,
14600                              'called check_lease_or_loan , l_lease_or_loan: '
14601                           || l_lease_or_loan
14602                          );
14603       END IF;
14604 
14605       -- RMUNJULU 02-JAN-03 2724951 Added code to check if loan and do dispose else
14606       -- if lease then check if with purchase or without and do accordingly
14607       -- If loan then do only dispose
14608       IF l_lease_or_loan = 'LOAN'
14609       THEN
14610          -- do asset dispose
14611          dispose_assets (p_term_rec             => l_term_rec,
14612                          p_sys_date             => l_sys_date,
14613                          p_klev_tbl             => lx_klev_tbl,
14614                          p_trn_already_set      => l_trn_already_set,
14615                          px_overall_status      => l_overall_status,
14616                          px_tcnv_rec            => lp_tcnv_rec,
14617                          x_return_status        => l_return_status
14618                         );
14619 
14620          IF (is_debug_statement_on)
14621          THEN
14622             okl_debug_pub.log_debug
14623                                 (g_level_statement,
14624                                  l_module_name,
14625                                     'called dispose_assets , return status: '
14626                                  || l_return_status
14627                                 );
14628          END IF;
14629       ELSE
14630          --  l_lease_or_loan = 'LEASE'
14631 
14632          -- If termination with purchase then do dispose else amortize and return
14633          -- RMUNJULU 31-JAN-03 2780539 Added TER_MAN_PURCHASE which is also a
14634          -- termination with purchase
14635          IF (l_term_rec.p_quote_type IN
14636                 ('TER_PURCHASE',
14637                  'TER_RECOURSE',
14638                  'TER_ROLL_PURCHASE',
14639                  'TER_MAN_PURCHASE'
14640                 )
14641             )
14642          THEN
14643             -- do asset dispose
14644             dispose_assets (p_term_rec             => l_term_rec,
14645                             p_sys_date             => l_sys_date,
14646                             p_klev_tbl             => lx_klev_tbl,
14647                             p_trn_already_set      => l_trn_already_set,
14648                             px_overall_status      => l_overall_status,
14649                             px_tcnv_rec            => lp_tcnv_rec,
14650                             x_return_status        => l_return_status
14651                            );
14652 
14653             IF (is_debug_statement_on)
14654             THEN
14655                okl_debug_pub.log_debug
14656                                 (g_level_statement,
14657                                  l_module_name,
14658                                     'called dispose_assets , return status: '
14659                                  || l_return_status
14660                                 );
14661             END IF;
14662 
14663             -- Amortization of assets not needed since termination
14664             -- with purchase.
14665             okl_api.set_message (p_app_name      => g_app_name,
14666                                  p_msg_name      => 'OKL_AM_AMORTIZE_NOT_NEED');
14667             -- Return of assets not needed since termination with purchase
14668             okl_api.set_message (p_app_name      => g_app_name,
14669                                  p_msg_name      => 'OKL_AM_RETURN_NOT_NEED');
14670          ELSE
14671             -- do amortization
14672             amortize_assets (p_term_rec             => l_term_rec,
14673                              p_sys_date             => l_sys_date,
14674                              p_klev_tbl             => lx_klev_tbl,
14675                              p_trn_already_set      => l_trn_already_set,
14676                              px_overall_status      => l_overall_status,
14677                              px_tcnv_rec            => lp_tcnv_rec,
14678                              x_return_status        => l_return_status
14679                             );
14680 
14681             IF (is_debug_statement_on)
14682             THEN
14683                okl_debug_pub.log_debug
14684                                (g_level_statement,
14685                                 l_module_name,
14686                                    'called amortize_assets , return status: '
14687                                 || l_return_status
14688                                );
14689             END IF;
14690 
14691 /* rmunjulu  bug 6853566 do not call delink here, call from amortize.
14692             IF g_amort_complete_flag = 'Y'
14693             THEN
14694                -- do denlink of assets from k if it is going offlease
14695                delink_assets (p_term_rec             => l_term_rec,
14696                               p_sys_date             => l_sys_date,
14697                               p_klev_tbl             => lx_klev_tbl,
14698                               p_trn_already_set      => l_trn_already_set,
14699                               px_overall_status      => l_overall_status,
14700                               px_tcnv_rec            => lp_tcnv_rec,
14701                               x_return_status        => l_return_status
14702                              );
14703             END IF;
14704 */
14705             -- do asset return
14706             return_assets (p_term_rec             => l_term_rec,
14707                            p_sys_date             => l_sys_date,
14708                            p_klev_tbl             => lx_klev_tbl,
14709                            p_trn_already_set      => l_trn_already_set,
14710                            px_overall_status      => l_overall_status,
14711                            px_tcnv_rec            => lp_tcnv_rec,
14712                            x_return_status        => l_return_status
14713                           );
14714 
14715             IF (is_debug_statement_on)
14716             THEN
14717                okl_debug_pub.log_debug
14718                                  (g_level_statement,
14719                                   l_module_name,
14720                                      'called return_assets , return status: '
14721                                   || l_return_status
14722                                  );
14723             END IF;
14724 
14725             -- Disposition of assets not needed since termination without purchase
14726             okl_api.set_message (p_app_name      => g_app_name,
14727                                  p_msg_name      => 'OKL_AM_DISPOSE_NOT_NEED');
14728          END IF;
14729       END IF;
14730 
14731       -- update the lines only if the overall_status is success
14732       IF (l_overall_status = g_ret_sts_success)
14733       THEN
14734          -- Set the p_status (which sets the sts_code) for the contract
14735          IF     l_term_rec.p_control_flag = 'BATCH_PROCESS'
14736             AND (   l_term_rec.p_quote_id IS NULL
14737                  OR l_term_rec.p_quote_id = g_miss_num
14738                 )
14739          THEN
14740             l_status := 'EXPIRED';
14741          ELSE
14742             l_status := 'TERMINATED';
14743          END IF;
14744 
14745          -- RMUNJULU 3018641 Step Message
14746          -- Step : Update Contract and Lines
14747          okl_api.set_message (p_app_name      => g_app_name,
14748                               p_msg_name      => 'OKL_AM_STEP_UPD');
14749 
14750          -- RMUNJULU 23-JAN-03 2762065
14751          -- If batch process then log the messages from stack
14752          IF p_term_rec.p_control_flag LIKE 'BATCH%'
14753          THEN
14754             -- RMUNJULU 2730738 for proper output file
14755             okl_am_btch_exp_lease_loan_pvt.pop_asset_msg_tbl; --log_messages;
14756          END IF;
14757 
14758          -- RMUNJULU 07-JAN-03 2736865 Added code to store messages and set stack to true
14759          -- Store messages in TRX_MSGS
14760          IF (is_debug_statement_on)
14761          THEN
14762             okl_debug_pub.log_debug
14763                                   (g_level_statement,
14764                                    l_module_name,
14765                                    'calling OKL_AM_UTIL_PVT.process_messages'
14766                                   );
14767          END IF;
14768 
14769          okl_am_util_pvt.process_messages
14770                                    (p_trx_source_table      => 'OKL_TRX_CONTRACTS',
14771                                     p_trx_id                => lp_tcnv_rec.ID,
14772                                     x_return_status         => l_return_status
14773                                    );
14774 
14775          IF (is_debug_statement_on)
14776          THEN
14777             okl_debug_pub.log_debug
14778                (g_level_statement,
14779                 l_module_name,
14780                    'called OKL_AM_UTIL_PVT.process_messages , return status: '
14781                 || l_return_status
14782                );
14783          END IF;
14784 
14785          -- Set message stack to true
14786          okl_api.init_msg_list (g_true);
14787          -- update lines, do mass rebook and activate insurances
14788          terminate_lines
14789             (p_api_version          => p_api_version,
14790              p_init_msg_list        => g_false,
14791              x_msg_count            => x_msg_count,
14792              x_msg_data             => x_msg_data,
14793              x_return_status        => l_return_status,
14794              px_overall_status      => l_overall_status,
14795              p_trn_already_set      => l_trn_already_set,
14796                                         -- RMUNJULU CONTRACT BLOCKING -- ADDED
14797              p_term_rec             => l_term_rec,
14798              p_sys_date             => l_sys_date,
14799              p_klev_tbl             => lx_klev_tbl,
14800              p_status               => l_status,
14801              px_tcnv_rec            => lp_tcnv_rec
14802             );
14803 
14804          IF (is_debug_statement_on)
14805          THEN
14806             okl_debug_pub.log_debug
14807                                (g_level_statement,
14808                                 l_module_name,
14809                                    'called terminate_lines , return status: '
14810                                 || l_return_status
14811                                );
14812          END IF;
14813 
14814          --++++++++start   -- CONTRACT BLOCKING (2)
14815          -- Transaction is now updated in terminate lines (before massrebook and after massrebook if needed)
14816          -- But not if anyproc before mass rebook fails
14817          IF l_return_status <> g_ret_sts_success
14818          THEN
14819             -- update the transaction record
14820             process_transaction (p_id                 => 0,
14821                                  p_term_rec           => l_term_rec,
14822                                  p_tcnv_rec           => lp_tcnv_rec,
14823                                  p_trn_mode           => 'UPDATE',
14824                                  x_id                 => lx_id,
14825                                  x_return_status      => l_return_status
14826                                 );
14827 
14828             IF (is_debug_statement_on)
14829             THEN
14830                okl_debug_pub.log_debug
14831                            (g_level_statement,
14832                             l_module_name,
14833                                'called process_transaction , return status: '
14834                             || l_return_status
14835                            );
14836             END IF;
14837 
14838             -- rollback if processing transaction failed
14839             IF (l_return_status = g_ret_sts_unexp_error)
14840             THEN
14841                RAISE okl_api.g_exception_unexpected_error;
14842             ELSIF (l_return_status = g_ret_sts_error)
14843             THEN
14844                RAISE okl_api.g_exception_error;
14845             END IF;
14846          END IF;
14847       --    END IF;
14848       ELSE
14849          -- Overall Not successfull so update transaction
14850 
14851          -- update the transaction record
14852          process_transaction (p_id                 => 0,
14853                               p_term_rec           => l_term_rec,
14854                               p_tcnv_rec           => lp_tcnv_rec,
14855                               p_trn_mode           => 'UPDATE',
14856                               x_id                 => lx_id,
14857                               x_return_status      => l_return_status
14858                              );
14859 
14860          IF (is_debug_statement_on)
14861          THEN
14862             okl_debug_pub.log_debug
14863                            (g_level_statement,
14864                             l_module_name,
14865                                'called process_transaction , return status: '
14866                             || l_return_status
14867                            );
14868          END IF;
14869 
14870          -- rollback if processing transaction failed
14871          IF (l_return_status = g_ret_sts_unexp_error)
14872          THEN
14873             RAISE okl_api.g_exception_unexpected_error;
14874          ELSIF (l_return_status = g_ret_sts_error)
14875          THEN
14876             RAISE okl_api.g_exception_error;
14877          END IF;
14878       END IF;
14879 
14880       --+++++++++end -- CONTRACT BLOCKING (2)
14881 
14882       -- RMUNJULU 2730738 for proper output file
14883       IF p_term_rec.p_control_flag LIKE 'BATCH%'
14884       THEN
14885          okl_am_btch_exp_lease_loan_pvt.pop_asset_msg_tbl;
14886       END IF;
14887 
14888       -- Save messages from stack into transaction message table
14889       IF (is_debug_statement_on)
14890       THEN
14891          okl_debug_pub.log_debug (g_level_statement,
14892                                   l_module_name,
14893                                   'calling OKL_AM_UTIL_PVT.process_messages'
14894                                  );
14895       END IF;
14896 
14897       okl_am_util_pvt.process_messages
14898                                    (p_trx_source_table      => 'OKL_TRX_CONTRACTS',
14899                                     p_trx_id                => lp_tcnv_rec.ID,
14900                                     x_return_status         => l_return_status
14901                                    );
14902 
14903       IF (is_debug_statement_on)
14904       THEN
14905          okl_debug_pub.log_debug
14906               (g_level_statement,
14907                l_module_name,
14908                   'called OKL_AM_UTIL_PVT.process_messages , return status: '
14909                || l_return_status
14910               );
14911       END IF;
14912 
14913       -- RMUNJULU 3018641 Added code to get and set TMG_RUN
14914       IF (is_debug_statement_on)
14915       THEN
14916          okl_debug_pub.log_debug
14917                        (g_level_statement,
14918                         l_module_name,
14919                         'calling OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run'
14920                        );
14921       END IF;
14922 
14923       okl_am_lease_loan_trmnt_pvt.get_set_tmg_run
14924                                            (p_trx_id             => lp_tcnv_rec.ID,
14925                                             x_return_status      => l_return_status);
14926 
14927       IF (is_debug_statement_on)
14928       THEN
14929          okl_debug_pub.log_debug
14930             (g_level_statement,
14931              l_module_name,
14932                 'called OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run , return status: '
14933              || l_return_status
14934             );
14935       END IF;
14936 
14937       -- Set the return status
14938       x_return_status := g_ret_sts_success;
14939       -- End the activity
14940       okl_api.end_activity (x_msg_count, x_msg_data);
14941 
14942       IF (is_debug_procedure_on)
14943       THEN
14944          okl_debug_pub.log_debug (g_level_procedure,
14945                                   l_module_name,
14946                                   'End(-)'
14947                                  );
14948       END IF;
14949    EXCEPTION
14950       WHEN g_exception_halt_validation
14951       THEN
14952          x_return_status := g_ret_sts_success;
14953 
14954          IF (is_debug_exception_on)
14955          THEN
14956             okl_debug_pub.log_debug (g_level_exception,
14957                                      l_module_name,
14958                                         'EXCEPTION :'
14959                                      || 'G_EXCEPTION_HALT_VALIDATION'
14960                                     );
14961          END IF;
14962       WHEN okl_api.g_exception_error
14963       THEN
14964          x_return_status :=
14965             okl_api.handle_exceptions (l_api_name,
14966                                        g_pkg_name,
14967                                        'G_RET_STS_ERROR',
14968                                        x_msg_count,
14969                                        x_msg_data,
14970                                        '_PVT'
14971                                       );
14972 
14973          IF (is_debug_exception_on)
14974          THEN
14975             okl_debug_pub.log_debug (g_level_exception,
14976                                      l_module_name,
14977                                      'EXCEPTION :' || 'G_EXCEPTION_ERROR'
14978                                     );
14979          END IF;
14980       WHEN okl_api.g_exception_unexpected_error
14981       THEN
14982          x_return_status :=
14983             okl_api.handle_exceptions (l_api_name,
14984                                        g_pkg_name,
14985                                        'G_RET_STS_UNEXP_ERROR',
14986                                        x_msg_count,
14987                                        x_msg_data,
14988                                        '_PVT'
14989                                       );
14990 
14991          IF (is_debug_exception_on)
14992          THEN
14993             okl_debug_pub.log_debug (g_level_exception,
14994                                      l_module_name,
14995                                         'EXCEPTION :'
14996                                      || 'G_EXCEPTION_UNEXPECTED_ERROR'
14997                                     );
14998          END IF;
14999       WHEN OTHERS
15000       THEN
15001          x_return_status :=
15002             okl_api.handle_exceptions (l_api_name,
15003                                        g_pkg_name,
15004                                        'OTHERS',
15005                                        x_msg_count,
15006                                        x_msg_data,
15007                                        '_PVT'
15008                                       );
15009 
15010          IF (is_debug_exception_on)
15011          THEN
15012             okl_debug_pub.log_debug (g_level_exception,
15013                                      l_module_name,
15014                                         'EXCEPTION :'
15015                                      || 'OTHERS, SQLCODE: '
15016                                      || SQLCODE
15017                                      || ' , SQLERRM : '
15018                                      || SQLERRM
15019                                     );
15020          END IF;
15021    END asset_level_termination;
15022 END okl_am_cntrct_ln_trmnt_pvt;