DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKL_AM_LEASE_TRMNT_PVT

Source


1 PACKAGE BODY OKL_AM_LEASE_TRMNT_PVT AS
2 /* $Header: OKLRLTNB.pls 120.95.12020000.2 2012/08/28 14:16:51 racheruv ship $ */
3 
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) := 'okl.am.plsql.okl_am_lease_trmnt_pvt.';
9 
10 /* Bug 6674730 start */
11 subtype trxv_rec_type is OKL_TRX_ASSETS_PUB.thpv_rec_type;
12 subtype talv_rec_type is OKL_TXL_ASSETS_PUB.tlpv_rec_type;
13 subtype adpv_rec_type is okl_txd_assets_pvt.advv_rec_type;
14 
15 G_TRANS_TYPE VARCHAR2(3) := 'CFA';
16 /* Bug 6674730 end */
17 
18 /**----------------------------------------------------------------------------
19   -- LOGIC
20   ----------------------------------------------------------------------------
21 
22   Start API Transaction
23   Rollback if setting transaction fails
24   SET OVERALL STATUS
25 
26   If transaction not already set then
27     Initialize Transaction Record
28     **Insert Transaction  Record
29     ** get id and pass along
30   Else
31     ** get trn rec and pass along
32   End if
33 
34   Validate Lease + Contract
35   SET OVERALL STATUS
36   If validation failed then
37     If batch process then
38       **Update Transaction  Record
39     End if
40     abort
41   End if
42 
43   Get Lines
44 
45   If contract expired then
46     If evergreen_yn <> 'Y' Then
47       Get Evergreen Eligibility
48       If Batch process and Eligible Then
49         Update contract header and lines
50       End if
51     End if
52   End if
53 
54   SET OVERALL STATUS
55 
56   If return status = Success Then
57     If set to evergreen then
58       Set Transaction  Record
59       **Update Transaction  Record
60       SET OVERALL STATUS
61       Rollback if Insert fails
62       Abort
63     Else
64       Set Transaction  Record###
65     End if
66   Else
67     Set Transaction  Record***
68     **Update Transaction  Record
69     SET OVERALL STATUS
70     Rollback if Update fails
71     Abort
72   End if
73 
74   If Early Termination Then
75     If Cancel Policies YN <> 'Y' Then
76       Call Cancel Policies
77       SET OVERALL STATUS
78       Set Transaction  Record***
79     End if
80   Else
81     Set Transaction  Record###
82   End if
83 
84   If Total Balance < Tolerance Amount then
85     Get Tolerance Amount
86     Get Total Balance
87     If close balances YN <> 'Y' then
88       Call Adjust header to close balances
89       SET OVERALL STATUS
90       Call Accounting entries
91       SET OVERALL STATUS
92       Get code combination id
93       Call Adjust Lines to close balances
94       SET OVERALL STATUS
95       Set Transaction  Record***
96     End if
97   Else
98     Set Transaction  Record###
99   End if
100 
101   If Streams to be updated Then
102     If Update Streams YN <> 'Y' Then
103       Call Update Streams
104       SET OVERALL STATUS
105       Set Transaction  Record***
106     End if
107   Else
108     Set Transaction  Record###
109   End if
110 
111   If Account Entries YN <> 'Y' Then
112     Call Account Entries
113     SET OVERALL STATUS
114     Set Transaction  Record***
115   End if
116 
117   If Term With Purchase Then
118     If Asset Dispose <> 'Y' Then
119       Call Asset Dispose
120       SET OVERALL STATUS
121       Set Transaction  Record***
122     End if
123     Set Transaction  Record###
124   Else
125     If Amortization YN <> 'Y' Then
126       Call Amortization
127       SET OVERALL STATUS
128       Set Transaction  Record***
129     End if
130     If Asset Return YN <> 'Y' Then
131       Call Asset Return
132       SET OVERALL STATUS
133       Set Transaction  Record***
134     End if
135     Set Transaction  Record###
136   End If
137 
138   Set Transaction  Record***
139   If overall status = Success and Update contract YN <> 'Y' then
140     Update contract header and lines
141     SET OVERALL STATUS
142     Rollback if update fails
143   End if
144 
145   **Update Transaction  Record
146   SET OVERALL STATUS
147   Rollback if insert fails
148 
149   End API Transaction
150 
151 
152 OKL_AM_LEASE_TRMNT_PVT
153    -- Returns E or U only in case of Hard errors ie when errors are critical and
154    -- need to rollabck the whole transaction. This will happen when there is a
155    -- problem with creating or updating row in transaction table or validate
156    -- lease/contract fails when it is not from batch process.
157    -- All other errors such as not able to do accounting entries, or not able to
158    -- update k header or lines will result in soft error so will be propagated
159    -- out of this API as "Success".
160 
161 OKL_AM_LEASE_LOAN_TRMNT_PVT
162    -- The Rec Type procedure calls the rec type of Lease termination API
163    -- The Tbl Type procedure calls the rec type of this same API
164 
165 OKL_AM_LEASE_LOAN_TRMNT_PUB
166    -- The Rec Type procedure calls the rec type of PVT API
167    -- -- This version will be called from batch process, from termination quote
168    -- -- API/ Termination Quote Update Screen
169    -- The Tbl Type procedure calls the tbl type of PVT API
170    -- -- This version will be called from Request Termination Screen
171    -- -- This version will rollback if even one contract fails and will stop
172    -- -- further processing as soon as it hits a contract which fails to
173    -- -- terminate.
174 
175 ---------------------------------------------------------------------------**/
176 
177   -- Start of comments
178   --
179   -- Function Name : check_auto_invoice_yn
180   -- Desciption     : Checks to see if auto invoice applicable
181   -- Business Rules :
182   -- Parameters  :
183   -- Version  : 1.0
184   -- History        : RMUNJULU 18-aug-05 BUYOUT_PROCESS
185   --
186   -- End of comments
187   PROCEDURE check_auto_invoice_yn(
188            p_term_rec        IN  term_rec_type,
189            x_auto_invoce_yn  OUT NOCOPY VARCHAR2,
190            x_return_status   OUT NOCOPY VARCHAR2)IS
191 
192      l_auto_invoce_yn  VARCHAR2(3);
193 
194      l_rule_code VARCHAR2(30);
195      l_rgd_code  VARCHAR2(30);
196      l_khr_id    NUMBER;
197 
198      l_rulv_rec   OKL_RULE_PUB.rulv_rec_type;
199      l_params   OKL_EXECUTE_FORMULA_PUB.ctxt_val_tbl_type;
200 
201      l_return_status VARCHAR2(3) := OKL_API.G_RET_STS_SUCCESS;
202      l_dummy_status VARCHAR2(3) := OKL_API.G_RET_STS_SUCCESS;
203      -- asawanka added for debug feature start
204     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'check_auto_invoice_yn';
205     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
206     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
207     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
208     -- asawanka added for debug feature end
209 
210   BEGIN
211     IF (is_debug_procedure_on) THEN
212        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
213     END IF;
214 
215      l_auto_invoce_yn := 'N';
216 
217      IF (is_debug_statement_on) THEN
218          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_term_rec.p_quote_id = '||p_term_rec.p_quote_id);
219          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
220      END IF;
221      -- Check if no quote and if new rule says Y
222      IF p_term_rec.p_quote_id IS NULL
223      OR p_term_rec.p_quote_id = OKL_API.G_MISS_NUM THEN -- means end of term processing
224 
225        l_rgd_code  := 'AMTFOC'; -- End of Term Purchase Option
226        l_rule_code := 'AMTINV'; -- Automatically Invoice for Fixed Purchase Option
227 
228        l_khr_id := p_term_rec.p_contract_id;
229 
230        -- Check if Automatically Invoice YN is Y
231      IF (is_debug_statement_on) THEN
232          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_term_rec.p_quote_id = '||p_term_rec.p_quote_id);
233          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Calling OKL_AM_UTIL_PVT.get_rule_record');
234      END IF;
235        OKL_AM_UTIL_PVT.get_rule_record(
236      p_rgd_code       => l_rgd_code,
237      p_rdf_code     => l_rule_code,
238      p_chr_id         => l_khr_id,
239      p_cle_id         => NULL,
240      x_rulv_rec     => l_rulv_rec,
241       x_return_status => l_return_status,
242      p_message_yn     => TRUE);
243 
244      IF (is_debug_statement_on) THEN
245          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Called OKL_AM_UTIL_PVT.get_rule_record l_return_status = '||l_return_status);
246          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_rulv_rec.rule_information1 = '||l_rulv_rec.rule_information1);
247      END IF;
248        IF l_return_status = OKL_API.G_RET_STS_SUCCESS THEN
249           l_auto_invoce_yn  := nvl(l_rulv_rec.rule_information1,'N'); -- Automatically Invoice YN, NULL is considered N
250        END IF;
251      END IF;
252 
253      x_auto_invoce_yn := l_auto_invoce_yn;
254      IF (is_debug_statement_on) THEN
255          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'x_auto_invoce_yn = '||x_auto_invoce_yn);
256      END IF;
257      x_return_status := l_dummy_status; -- Don't use l_return_status, return success always
258 
259     IF (is_debug_procedure_on) THEN
260        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
261     END IF;
262   EXCEPTION
263      WHEN OTHERS THEN
264         IF (is_debug_exception_on) THEN
265            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
266         END IF;
267         x_return_status := OKL_API.G_RET_STS_ERROR;
268   END check_auto_invoice_yn;
269 
270   -- Start of comments
271   --
272   -- Function Name : get_purchase_amount
273   -- Desciption     : gets the purchase amount for asset id
274   -- Business Rules :
275   -- Parameters  :
276   -- Version  : 1.0
277   -- History        : RMUNJULU 18-aug-05 BUYOUT_PROCESS
278   --
279   -- End of comments
280   PROCEDURE get_purchase_amount(
281            p_term_rec        IN  term_rec_type,
282            p_kle_id          IN  NUMBER,
283            x_purchase_amount OUT NOCOPY NUMBER,
284            x_return_status   OUT NOCOPY VARCHAR2)IS
285 
286      l_purchase_amount NUMBER;
287 
288      l_rule_code VARCHAR2(30);
289      l_rgd_code  VARCHAR2(30);
290      l_khr_id    NUMBER;
291 
292      l_calc_option    VARCHAR2(150);
293      l_fixed_value    NUMBER;
294      l_formula_name    VARCHAR2(150);
295      l_formula_value   NUMBER;
296      l_prorate         VARCHAR2(150);
297 
298      l_rulv_rec   OKL_RULE_PUB.rulv_rec_type;
299      l_params   OKL_EXECUTE_FORMULA_PUB.ctxt_val_tbl_type;
300 
301      l_return_status VARCHAR2(3) := OKL_API.G_RET_STS_SUCCESS;
302      l_dummy_status VARCHAR2(3) := OKL_API.G_RET_STS_SUCCESS;
303      l_line_oec NUMBER;
304      l_contract_oec NUMBER;
305      G_EXCEPTION_ERROR EXCEPTION;
306 
307 -- sosharma bug 10100863
308      CURSOR get_contract_curr(p_chr_id NUMBER)
309     IS
310     select currency_code from okc_k_headers_b where
311     id  = p_chr_id;
312 
313     l_curr_code  VARCHAR2(10);
314 
315      -- asawanka added for debug feature start
316     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'get_purchase_amount';
317     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
318     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
319     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
320     -- asawanka added for debug feature end
321 
322   BEGIN
323      IF (is_debug_procedure_on) THEN
324        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
325      END IF;
326      l_purchase_amount := 0;
327 
328      l_rgd_code := 'AMTFOC'; -- End of Term Purchase Option
329      l_rule_code := 'AMBPOC'; -- Purchase Options
330      IF (is_debug_statement_on) THEN
331          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
332          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_kle_id = '||p_kle_id);
333          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Called OKL_AM_UTIL_PVT.get_rule_record l_return_status = '||l_return_status);
334          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_rulv_rec.rule_information1 = '||l_rulv_rec.rule_information1);
335      END IF;
336      l_khr_id := p_term_rec.p_contract_id;
337      IF (is_debug_statement_on) THEN
338          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Calling OKL_AM_UTIL_PVT.get_rule_record');
339      END IF;
340      -- Get End of Term Purchase Amount Details
341      OKL_AM_UTIL_PVT.get_rule_record(
342      p_rgd_code       => l_rgd_code,
343      p_rdf_code     => l_rule_code,
344      p_chr_id         => l_khr_id,
345      p_cle_id         => NULL,
346      x_rulv_rec     => l_rulv_rec,
347       x_return_status => l_return_status,
348      p_message_yn     => TRUE);
349 
350      IF (is_debug_statement_on) THEN
351          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Called OKL_AM_UTIL_PVT.get_rule_record l_return_status = '||l_return_status);
352          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_rulv_rec.rule_information1 = '||l_rulv_rec.rule_information1);
353          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_rulv_rec.rule_information2 = '||l_rulv_rec.rule_information2);
354          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_rulv_rec.rule_information3 = '||l_rulv_rec.rule_information3);
355          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_rulv_rec.rule_information4 = '||l_rulv_rec.rule_information4);
356      END IF;
357 
358      IF l_return_status = OKL_API.G_RET_STS_SUCCESS THEN
359          l_calc_option   := l_rulv_rec.rule_information1; -- Purchase Option
360          l_fixed_value   := NVL (TO_NUMBER (l_rulv_rec.rule_information2), 0); -- Purchase Option Amount
361          l_formula_name   := l_rulv_rec.rule_information3; -- Purchase Option Formula
362          l_prorate        := l_rulv_rec.rule_information4; -- Purchase Option Prorate
363      ELSE
364        RAISE G_EXCEPTION_ERROR;
365      END IF;
366 
367      IF l_calc_option = 'NOT_APPLICABLE' THEN -- Purchase Amount is NOT APPLICABLE
368 
369          l_purchase_amount := 0;
370 
371      ELSIF l_calc_option = 'USE_FIXED_AMOUNT' THEN -- Purchase Amount is FIXED AMOUNT
372 
373          l_purchase_amount := l_fixed_value;
374 
375      ELSIF l_calc_option = 'USE_FORMULA' THEN -- Purchase Amount is FORMULA
376 
377              IF (is_debug_statement_on) THEN
378                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Calling OKL_AM_UTIL_PVT.get_formula_value');
379              END IF;
380             -- Get the formula value
381             OKL_AM_UTIL_PVT.get_formula_value (
382                     p_formula_name            => l_formula_name,
383                     p_chr_id                  => l_khr_id,
384                     p_cle_id                  => p_kle_id,
385                     p_additional_parameters   => l_params,
386                     x_formula_value           => l_formula_value,
387                     x_return_status           => l_return_status);
388             IF (is_debug_statement_on) THEN
389                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Called OKL_AM_UTIL_PVT.get_formula_value l_return_status = '||l_return_status);
390                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_formula_value = '||l_formula_value);
391             END IF;
392 
393             IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
394               RAISE G_EXCEPTION_ERROR;
395             ELSE
396               l_purchase_amount := l_formula_value;
397             END IF;
398      END IF;
399 
400      IF l_prorate = 'PRORATE' AND l_purchase_amount <> 0 THEN
401 
402         -- Get line oec (evaluate formula contract_oec and pass line id)
403         IF (is_debug_statement_on) THEN
404                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Calling OKL_AM_UTIL_PVT.get_formula_value');
405         END IF;
406         OKL_AM_UTIL_PVT.get_formula_value (
407     p_formula_name           => 'CONTRACT_OEC',
408     p_chr_id               => l_khr_id,
409                 p_cle_id                  => p_kle_id,
410              p_additional_parameters   => l_params,
411     x_formula_value           => l_line_oec,
412     x_return_status           => l_return_status);
413     IF (is_debug_statement_on) THEN
414        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Called OKL_AM_UTIL_PVT.get_formula_value l_return_status = '||l_return_status);
415        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_formula_value = '||l_formula_value);
416     END IF;
417 
418          IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
419     RAISE G_EXCEPTION_ERROR;
420          END IF;
421 
422         -- Get contract oec
423         IF (is_debug_statement_on) THEN
424                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Calling OKL_AM_UTIL_PVT.get_formula_value');
425         END IF;
426         OKL_AM_UTIL_PVT.get_formula_value (
427     p_formula_name           => 'CONTRACT_OEC',
428     p_chr_id               => l_khr_id,
429                 p_cle_id                  => NULL,
430              p_additional_parameters   => l_params,
431     x_formula_value           => l_contract_oec,
432     x_return_status           => l_return_status);
433     IF (is_debug_statement_on) THEN
434          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Called OKL_AM_UTIL_PVT.get_formula_value l_return_status = '||l_return_status);
435          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_formula_value = '||l_formula_value);
436     END IF;
437 
438 
439          IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
440     RAISE G_EXCEPTION_ERROR;
441          END IF;
442 
443 -- sosharma bug 10100863
444 	OPEN get_contract_curr(l_khr_id);
445         FETCH get_contract_curr into l_curr_code;
446         CLOSE get_contract_curr;
447 
448   IF l_contract_oec <> 0 THEN
449       -- l_purchase_amount := l_purchase_amount * l_line_oec/l_contract_oec;
450       --sosharma bug 10100863
451 		     IF l_curr_code is not NULL then
452 	             l_purchase_amount:=Okl_Accounting_Util.ROUND_AMOUNT (l_purchase_amount * l_line_oec/l_contract_oec ,  l_curr_code );
453 
454 		     else
455                      l_purchase_amount := l_purchase_amount * l_line_oec/l_contract_oec;
456                      end if;
457     ELSE
458        l_purchase_amount := 0;
459   END IF;
460 
461      END IF;
462 
463      x_purchase_amount := l_purchase_amount;
464      x_return_status := l_return_status;
465 
466     IF (is_debug_procedure_on) THEN
467        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
468     END IF;
469   EXCEPTION
470      WHEN G_EXCEPTION_ERROR THEN
471         IF (is_debug_exception_on) THEN
472              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
473         END IF;
474         x_return_status := OKL_API.G_RET_STS_ERROR;
475      WHEN OTHERS THEN
476         IF (is_debug_exception_on) THEN
477            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
478         END IF;
479 
480         x_return_status := OKL_API.G_RET_STS_ERROR;
481   END get_purchase_amount;
482 
483     /* Changes*/
484 PROCEDURE get_purchase_amount_total(
485            p_term_rec        IN  term_rec_type,
486            x_prorate         OUT NOCOPY VARCHAR2,
487            x_purchase_amount OUT NOCOPY NUMBER,
488            x_return_status   OUT NOCOPY VARCHAR2)IS
489 
490      l_purchase_amount NUMBER;
491 
492      l_rule_code VARCHAR2(30);
493      l_rgd_code  VARCHAR2(30);
494      l_khr_id    NUMBER;
495 
496      l_calc_option	   VARCHAR2(150);
497      l_fixed_value	   NUMBER;
498      l_formula_name	   VARCHAR2(150);
499      l_formula_value   NUMBER;
500      l_prorate         VARCHAR2(150);
501 
502      l_rulv_rec	  OKL_RULE_PUB.rulv_rec_type;
503      l_params	  OKL_EXECUTE_FORMULA_PUB.ctxt_val_tbl_type;
504 
505      l_return_status VARCHAR2(3) := OKL_API.G_RET_STS_SUCCESS;
506      l_dummy_status VARCHAR2(3) := OKL_API.G_RET_STS_SUCCESS;
507 
508      G_EXCEPTION_ERROR EXCEPTION;
509 
510   BEGIN
511 
512      l_purchase_amount := 0;
513 
514      l_rgd_code := 'AMTFOC'; -- End of Term Purchase Option
515      l_rule_code := 'AMBPOC'; -- Purchase Options
516 
517      l_khr_id := p_term_rec.p_contract_id;
518 
519      -- Get End of Term Purchase Amount Details
520      OKL_AM_UTIL_PVT.get_rule_record(
521 		   p_rgd_code       => l_rgd_code,
522 		   p_rdf_code	    => l_rule_code,
523 		   p_chr_id	        => l_khr_id,
524 		   p_cle_id	        => NULL,
525 		   x_rulv_rec	    => l_rulv_rec,
526  		   x_return_status	=> l_return_status,
527 		   p_message_yn	    => TRUE);
528 
529      IF l_return_status = OKL_API.G_RET_STS_SUCCESS THEN
530          l_calc_option	  := l_rulv_rec.rule_information1; -- Purchase Option
531          l_fixed_value	  := NVL (TO_NUMBER (l_rulv_rec.rule_information2), 0); -- Purchase Option Amount
532          l_formula_name   := l_rulv_rec.rule_information3; -- Purchase Option Formula
533          l_prorate        := l_rulv_rec.rule_information4; -- Purchase Option Prorate
534 	 ELSE
535 		 RAISE G_EXCEPTION_ERROR;
536      END IF;
537 
538      IF l_calc_option = 'NOT_APPLICABLE' THEN -- Purchase Amount is NOT APPLICABLE
539 
540          l_purchase_amount := 0;
541 
542      ELSIF l_calc_option = 'USE_FIXED_AMOUNT' THEN -- Purchase Amount is FIXED AMOUNT
543 
544          l_purchase_amount := l_fixed_value;
545 
546      ELSIF l_calc_option = 'USE_FORMULA' THEN -- Purchase Amount is FORMULA
547 
548          -- Get the formula value
549          OKL_AM_UTIL_PVT.get_formula_value (
550 				p_formula_name	          => l_formula_name,
551 				p_chr_id	              => l_khr_id,
552                 p_cle_id                  => null,
553     	        p_additional_parameters   => l_params,
554 				x_formula_value           => l_formula_value,
555 				x_return_status	          => l_return_status);
556 
557          IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
558 			 RAISE G_EXCEPTION_ERROR;
559          ELSE
560              l_purchase_amount := l_formula_value;
561          END IF;
562 
563      END IF;
564 
565   --   IF l_prorate = 'PRORATE' AND l_purchase_amount <> 0 THEN
566 
567 
568     x_prorate := l_prorate;
569      x_purchase_amount := l_purchase_amount;
570      x_return_status := l_return_status;
571 
572   EXCEPTION
573      WHEN G_EXCEPTION_ERROR THEN
574         x_return_status := OKL_API.G_RET_STS_ERROR;
575      WHEN OTHERS THEN
576         x_return_status := OKL_API.G_RET_STS_ERROR;
577   END get_purchase_amount_total;
578 
579   -- Start of comments
580   --
581   -- Function Name : check_k_evergreen_ear
582   -- Desciption     : Checks to see if any old trn exists which had set contract to evergreen
583   -- Business Rules :
584   -- Parameters  :
585   -- Version  : 1.0
586   -- History        : RMUNJULU 04-MAR-04 3485854 Created
587   --
588   -- End of comments
589   FUNCTION check_k_evergreen_ear(
590                        p_khr_id          IN NUMBER,
591                        p_tcn_id          IN NUMBER,
592                        x_return_status   OUT NOCOPY VARCHAR2) RETURN VARCHAR2 IS
593 
594    -- Check if another transaction exists which is processed and for which tmt_evergreen_yn was Y
595    -- which means this contract was evergreen earlier
596    CURSOR chk_evergreen_ear_csr ( p_khr_id IN NUMBER, p_tcn_id IN NUMBER) IS
597     SELECT trx.tmt_evergreen_yn
598     FROM   OKL_TRX_CONTRACTS trx
599     WHERE  trx.khr_id =  p_khr_id
600     AND    trx.tmt_status_code = 'PROCESSED' --changed by akrangan sla tmt_status_code changes
601     AND    trx.tcn_type IN ('TMT','ALT','EVG')-- akrangan bug 5354501 fix added 'EVG'
602     AND    trx.tmt_evergreen_yn = 'Y'
603 --rkuttiya added for 12.1.1 Multi GAAP
604     AND    trx.representation_type = 'PRIMARY'
605     AND    trx.id <> p_tcn_id;
606 
607     l_evergreen_earlier VARCHAR2(3) := 'N';
608 
609     -- asawanka added for debug feature start
610     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'check_k_evergreen_ear';
611     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
612     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
613     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
614     -- asawanka added for debug feature end
615 
616   BEGIN
617      IF (is_debug_procedure_on) THEN
618        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
619      END IF;
620 
621       x_return_status := OKL_API.G_RET_STS_SUCCESS;
622 
623       IF (is_debug_statement_on) THEN
624           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_khr_id = '||p_khr_id);
625           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_tcn_id = '||p_tcn_id);
626       END IF;
627       FOR chk_evergreen_ear_rec IN chk_evergreen_ear_csr (p_khr_id, p_tcn_id) LOOP
628          l_evergreen_earlier := chk_evergreen_ear_rec.tmt_evergreen_yn;
629       END LOOP;
630 
631       IF (is_debug_statement_on) THEN
632           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_evergreen_earlier= '||l_evergreen_earlier);
633       END IF;
634 
635       IF (is_debug_procedure_on) THEN
636        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
637       END IF;
638 
639       RETURN l_evergreen_earlier;
640 
641   EXCEPTION
642 
643      WHEN OTHERS THEN
644 
645         IF (is_debug_exception_on) THEN
646            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
647         END IF;
648 
649          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
650 
651          -- Set the oracle error message
652          OKL_API.set_message(
653                          p_app_name      => OKC_API.G_APP_NAME,
654                          p_msg_name      => g_unexpected_error,
655                          p_token1        => g_sqlcode_token,
656                          p_token1_value  => SQLCODE,
657                          p_token2        => g_sqlerrm_token,
658                          p_token2_value  => SQLERRM);
659 
660          RETURN NULL;
661 
662   END  check_k_evergreen_ear;
663 
664   -- Start of comments
665   --
666   -- Procedure Name : delink_contract_from_asset
667   -- Desciption     :  contract ID from FA asset, upon full termination of a Booked or Evergreen contract
668   -- Business Rules :
669   -- Parameters     :
670   -- Version        : 1.0
671   -- History        : SECHAWLA 18-Dec-07 6690811 Created
672   --                  SECHAWLA 02-Jan-08 6720667 - Check if contract ID is already null, before updating to Null
673   --                  rmunjulu bug 6853566 modify delink to work from partial termination aswell
674   --
675   -- End of comments
676 
677   PROCEDURE delink_contract_from_asset(
678                        p_api_version      IN  NUMBER,
679                        x_msg_count        OUT  NOCOPY NUMBER,
680                        x_msg_data         OUT  NOCOPY VARCHAR2,
681                        p_full_term_yn     IN VARCHAR2 DEFAULT NULL, -- rmunjulu bug 6853566
682                        p_khr_id           IN NUMBER,
683                        p_klev_tbl         IN OKL_AM_CNTRCT_LN_TRMNT_PVT.klev_tbl_type DEFAULT l_emty_tbl, -- rmunjulu bug 6853566
684                        p_sts_code         IN VARCHAR2 DEFAULT NULL, -- rmunjulu bug 6853566 make it default null
685                        p_quote_accpt_date IN DATE,
686                        p_quote_eff_date   IN DATE,
687                        x_return_status    OUT NOCOPY VARCHAR2) IS
688 
689    -- get the active financial asset lines for the contract
690    CURSOR l_okcklines_csr(cp_khr_id IN NUMBER, cp_sts_code IN VARCHAR2) IS
691    SELECT a.id, a.name
692    FROM   okc_k_lines_v a , okc_line_styles_b b
693    WHERE  a.chr_id = cp_khr_id
694    AND    a.lse_id = b.id
695    AND    b.lty_code = 'FREE_FORM1'
696    AND    a.sts_code = cp_sts_code;
697 
698    -- Get all the FA books (corp and tax) that asset belongs to
699    CURSOR l_fabooks_csr(cp_asset_number IN VARCHAR2, cp_sysdate IN DATE) IS
700    SELECT fb.book_type_code, fb.asset_id, fb.contract_id --SECHAWLA 02-Jan-08 6720667 : Added contract_id
701    FROM   fa_books fb, fa_additions_b fab, fa_book_controls fbc
702    WHERE  fb.asset_id = fab.asset_id
703    AND    fb.book_type_code = fbc.book_type_code
704    AND    nvl(fbc.date_ineffective,cp_sysdate+1) > cp_sysdate
705    AND    fb.transaction_header_id_out IS NULL
706    AND    fab.asset_number = cp_asset_number;
707 
708 
709    l_asset_fin_rec_empty_adj    FA_API_TYPES.asset_fin_rec_type;
710    l_asset_hdr_empty_rec        FA_API_TYPES.asset_hdr_rec_type;
711    l_trans_empty_rec			FA_API_TYPES.trans_rec_type;
712    l_adj_trans_rec              FA_API_TYPES.trans_rec_type;
713    l_adj_asset_hdr_rec          FA_API_TYPES.asset_hdr_rec_type;
714    l_asset_fin_rec_adj          FA_API_TYPES.asset_fin_rec_type;
715    l_asset_fin_rec_new		    FA_API_TYPES.asset_fin_rec_type;
716    l_inv_trans_rec              FA_API_TYPES.inv_trans_rec_type;
717    l_adj_inv_tbl                FA_API_TYPES.inv_tbl_type;
718    l_asset_deprn_rec_adj        FA_API_TYPES.asset_deprn_rec_type;
719    l_asset_deprn_rec_new        FA_API_TYPES.asset_deprn_rec_type;
720    l_asset_deprn_mrc_tbl_new    FA_API_TYPES.asset_deprn_tbl_type;
721    l_group_reclass_options_rec  FA_API_TYPES.group_reclass_options_rec_type;
722    l_asset_fin_mrc_tbl_new	    FA_API_TYPES.asset_fin_tbl_type;
723 
724 
725    l_return_status           VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
726    delink_exception          EXCEPTION;
727 
728    l_module_name VARCHAR2(500) := G_MODULE_NAME || 'delink_contract_from_asset';
729    is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
730    is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
731    is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
732 
733    i NUMBER;
734 
735   BEGIN
736      IF (is_debug_procedure_on) THEN
737        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
738      END IF;
739 
740       x_return_status := OKL_API.G_RET_STS_SUCCESS;
741 
742       IF (is_debug_statement_on) THEN
743           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_khr_id = '||p_khr_id);
744           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_sts_code = '||p_sts_code);
745           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_quote_accpt_date = '||p_quote_accpt_date);
746           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_quote_eff_date = '||p_quote_eff_date);
747           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_full_term_yn = '||nvl(p_full_term_yn,'Y'));
748       END IF;
749 
750 
751       IF p_khr_id IS NULL OR p_khr_id = OKL_API.G_MISS_NUM THEN
752          x_return_status := OKL_API.G_RET_STS_ERROR;
753          -- chr id is required
754          OKC_API.set_message(        p_app_name      => 'OKC',
755                                      p_msg_name      => G_REQUIRED_VALUE,
756                                      p_token1        => G_COL_NAME_TOKEN,
757                                      p_token1_value  => 'Contract Id');
758          RAISE delink_exception;
759       END IF;
760 
761       -- rmunjulu modified to check for statusonly in case of full termination
762       IF nvl(p_full_term_yn,'Y') = 'Y' AND (p_sts_code IS NULL OR p_sts_code = OKL_API.G_MISS_CHAR) THEN
763          x_return_status := OKL_API.G_RET_STS_ERROR;
764          -- Status code is required
765          OKC_API.set_message(        p_app_name      => 'OKC',
766                                      p_msg_name      => G_REQUIRED_VALUE,
767                                      p_token1        => G_COL_NAME_TOKEN,
768                                      p_token1_value  => 'Contract Status Code');
769          RAISE delink_exception;
770       END IF;
771 
772       IF p_quote_accpt_date IS NULL OR p_quote_accpt_date = OKL_API.G_MISS_DATE THEN
773          x_return_status := OKL_API.G_RET_STS_ERROR;
774          -- Quote Acceptance Date is required
775          OKC_API.set_message(        p_app_name      => 'OKC',
776                                      p_msg_name      => G_REQUIRED_VALUE,
777                                      p_token1        => G_COL_NAME_TOKEN,
778                                      p_token1_value  => 'Quote Acceptance Date');
779          RAISE delink_exception;
780       END IF;
781 
782       IF p_quote_eff_date IS NULL OR p_quote_eff_date = OKL_API.G_MISS_DATE THEN
783          x_return_status := OKL_API.G_RET_STS_ERROR;
784          -- Quote Effective Date is required
785          OKC_API.set_message(        p_app_name      => 'OKC',
786                                      p_msg_name      => G_REQUIRED_VALUE,
787                                      p_token1        => G_COL_NAME_TOKEN,
788                                      p_token1_value  => 'Quote Effective Date');
789          RAISE delink_exception;
790       END IF;
791 
792       IF nvl(p_full_term_yn ,'Y') = 'Y' THEN -- rmunjulu bug 6853566 full termination-- do earlier processing
793       IF (is_debug_statement_on) THEN
794 
795         okl_debug_pub.log_debug (g_level_statement, l_module_name, 'p_full_term_yn :'||nvl(p_full_term_yn ,'Y'));
796 
797       END IF;
798       FOR l_okcklines_rec IN l_okcklines_csr(p_khr_id, p_sts_code) LOOP
799 
800           FOR l_fabooks_rec IN l_fabooks_csr(l_okcklines_rec.name, p_quote_accpt_date ) LOOP
801 
802            --SECHAWLA 02-Jan-08 6720667 - Added the following IF
803            IF l_fabooks_rec.contract_id IS NOT NULL THEN
804 
805                 l_asset_fin_rec_adj := l_asset_fin_rec_empty_adj ;
806                 l_adj_trans_rec := l_trans_empty_rec;
807                 l_adj_asset_hdr_rec := l_asset_hdr_empty_rec;
808 
809                 l_adj_trans_rec.transaction_subtype := 'AMORTIZED';
810                 l_adj_asset_hdr_rec.asset_id :=  l_fabooks_rec.asset_id;
811                 l_adj_asset_hdr_rec.book_type_code := l_fabooks_rec.book_type_code;
812                 l_asset_fin_rec_adj.contract_id := FND_API.G_MISS_NUM;
813                 l_adj_trans_rec.transaction_date_entered := p_quote_eff_date;
814 
815                 IF (is_debug_statement_on) THEN
816 
817                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
818                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_okcklines_rec.name: '|| l_okcklines_rec.name);
819                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
820                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, p_quote_accpt_date: '|| p_quote_accpt_date);
821                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
822                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_trans_rec.transaction_subtype: '|| l_adj_trans_rec.transaction_subtype);
823                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
824                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_asset_hdr_rec.asset_id: '|| l_adj_asset_hdr_rec.asset_id);
825                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
826                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_asset_hdr_rec.book_type_code: '|| l_adj_asset_hdr_rec.book_type_code);
827                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
828                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_trans_rec.transaction_date_entered: '|| l_adj_trans_rec.transaction_date_entered);
829 
830                 END IF;
831 
832                 fa_adjustment_pub.do_adjustment(
833                                       p_api_version              => p_api_version,
834                                       p_init_msg_list            => OKC_API.G_FALSE,
835                                       p_commit                   => FND_API.G_FALSE,
836                                       p_validation_level         => FND_API.G_VALID_LEVEL_FULL,
837                                       p_calling_fn               => NULL,
838                                       x_return_status            => x_return_status,
839                                       x_msg_count                => x_msg_count,
840                                       x_msg_data                 => x_msg_data,
841                                       px_trans_rec               => l_adj_trans_rec,
842                                       px_asset_hdr_rec           => l_adj_asset_hdr_rec,
843                                       p_asset_fin_rec_adj        => l_asset_fin_rec_adj,
844                                       x_asset_fin_rec_new        => l_asset_fin_rec_new,
845                                       x_asset_fin_mrc_tbl_new    => l_asset_fin_mrc_tbl_new,
846                                       px_inv_trans_rec           => l_inv_trans_rec,
847                                       px_inv_tbl                 => l_adj_inv_tbl,
848                                       p_asset_deprn_rec_adj      => l_asset_deprn_rec_adj,
849                                       x_asset_deprn_rec_new      => l_asset_deprn_rec_new,
850                                       x_asset_deprn_mrc_tbl_new  => l_asset_deprn_mrc_tbl_new,
851                                       p_group_reclass_options_rec => l_group_reclass_options_rec);
852 
853                 IF (is_debug_statement_on) THEN
854 
855                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
856                     'AFTER CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, x_return_status: '|| x_return_status);
857 
858                 END IF;
859 
860                IF x_return_status <> OKC_API.G_RET_STS_SUCCESS THEN
861 
862                   -- Error processing TRX_TYPE transaction in Fixed Assets for asset ASSET_NUMBER in book BOOK.
863                   OKC_API.set_message(  p_app_name      => 'OKL',
864                                         p_msg_name      => 'OKL_AM_AMT_TRANS_FAILED',
865                                         p_token1        =>  'TRX_TYPE',
866                                         p_token1_value  =>  'Contract Delink',
867                                         p_token2        =>  'ASSET_NUMBER',
868                                         p_token2_value  =>  l_okcklines_rec.name,
869                                         p_token3        =>  'BOOK',
870                                         p_token3_value  =>  l_fabooks_rec.book_type_code);
871                    RAISE delink_exception;
872                END IF;
873             END IF; --SECHAWLA 02-Jan-08 6720667
874 
875          END LOOP;
876 
877 
878       END LOOP;
879 
880       ELSE -- partial termination
881 
882       IF (is_debug_statement_on) THEN
883 
884         okl_debug_pub.log_debug (g_level_statement, l_module_name, 'p_full_term_yn :'||nvl(p_full_term_yn ,'Y'));
885 
886       END IF;
887       -- if assets present for contract
888       IF (p_klev_tbl.COUNT > 0) THEN
889          -- Loop thru assets table
890          i := p_klev_tbl.FIRST;
891          LOOP
892 
893 
894             FOR l_fabooks_rec IN l_fabooks_csr (p_klev_tbl (i).p_asset_name, p_quote_accpt_date) LOOP
895 
896               IF l_fabooks_rec.contract_id IS NOT NULL THEN
897 
898                 l_asset_fin_rec_adj := l_asset_fin_rec_empty_adj ;
899                 l_adj_trans_rec := l_trans_empty_rec;
900                 l_adj_asset_hdr_rec := l_asset_hdr_empty_rec;
901 
902                 l_adj_trans_rec.transaction_subtype := 'AMORTIZED';
903                 l_adj_asset_hdr_rec.asset_id :=  l_fabooks_rec.asset_id;
904                 l_adj_asset_hdr_rec.book_type_code := l_fabooks_rec.book_type_code;
905                 l_asset_fin_rec_adj.contract_id := FND_API.G_MISS_NUM;
906                 l_adj_trans_rec.transaction_date_entered := p_quote_eff_date;
907 
908                 IF (is_debug_statement_on) THEN
909 
910                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
911                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, p_klev_tbl('|| i || ').p_kle_id: '|| p_klev_tbl (i).p_kle_id);
912                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
913                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, p_klev_tbl('|| i || ').p_asset_name: '|| p_klev_tbl (i).p_asset_name);
914                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
915                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, p_quote_accpt_date: '|| p_quote_accpt_date);
916                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
917                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_trans_rec.transaction_subtype: '|| l_adj_trans_rec.transaction_subtype);
918                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
919                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_asset_hdr_rec.asset_id: '|| l_adj_asset_hdr_rec.asset_id);
920                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
921                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_asset_hdr_rec.book_type_code: '|| l_adj_asset_hdr_rec.book_type_code);
922                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
923                     'BEFORE CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, l_adj_trans_rec.transaction_date_entered: '|| l_adj_trans_rec.transaction_date_entered);
924 
925                 END IF;
926 
927                 fa_adjustment_pub.do_adjustment(
928                                       p_api_version              => p_api_version,
929                                       p_init_msg_list            => OKC_API.G_FALSE,
930                                       p_commit                   => FND_API.G_FALSE,
931                                       p_validation_level         => FND_API.G_VALID_LEVEL_FULL,
932                                       p_calling_fn               => NULL,
933                                       x_return_status            => x_return_status,
934                                       x_msg_count                => x_msg_count,
935                                       x_msg_data                 => x_msg_data,
936                                       px_trans_rec               => l_adj_trans_rec,
937                                       px_asset_hdr_rec           => l_adj_asset_hdr_rec,
938                                       p_asset_fin_rec_adj        => l_asset_fin_rec_adj,
939                                       x_asset_fin_rec_new        => l_asset_fin_rec_new,
940                                       x_asset_fin_mrc_tbl_new    => l_asset_fin_mrc_tbl_new,
941                                       px_inv_trans_rec           => l_inv_trans_rec,
942                                       px_inv_tbl                 => l_adj_inv_tbl,
943                                       p_asset_deprn_rec_adj      => l_asset_deprn_rec_adj,
944                                       x_asset_deprn_rec_new      => l_asset_deprn_rec_new,
945                                       x_asset_deprn_mrc_tbl_new  => l_asset_deprn_mrc_tbl_new,
946                                       p_group_reclass_options_rec => l_group_reclass_options_rec);
947 
948                 IF (is_debug_statement_on) THEN
949 
950                   okl_debug_pub.log_debug (g_level_statement, l_module_name,
951                     'AFTER CALL TO FA_ADJUSTMENT_PUB.DO_ADJUSTMENT, x_return_status: '|| x_return_status);
952 
953                 END IF;
954 
955                  IF x_return_status <> OKC_API.G_RET_STS_SUCCESS THEN
956 
957                     -- Error processing TRX_TYPE transaction in Fixed Assets for asset ASSET_NUMBER in book BOOK.
958                     OKC_API.set_message(  p_app_name      => 'OKL',
959                                         p_msg_name      => 'OKL_AM_AMT_TRANS_FAILED',
960                                         p_token1        =>  'TRX_TYPE',
961                                         p_token1_value  =>  'Contract Delink',
962                                         p_token2        =>  'ASSET_NUMBER',
963                                         p_token2_value  =>  p_klev_tbl (i).p_asset_name,
964                                         p_token3        =>  'BOOK',
965                                         p_token3_value  =>  l_fabooks_rec.book_type_code);
966                     RAISE delink_exception;
967                  END IF;
968                END IF;
969 
970             END LOOP; -- end of for loop
971 
972             EXIT WHEN (i = p_klev_tbl.LAST);
973             i := p_klev_tbl.NEXT (i);
974          END LOOP;      -- end of asset table loop
975 
976       END IF;
977       END IF;
978 
979       IF (is_debug_procedure_on) THEN
980        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
981       END IF;
982 
983 
984   EXCEPTION
985      WHEN delink_exception THEN
986         IF (is_debug_exception_on) THEN
987            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'delink_exception');
988         END IF;
989 
990         IF l_okcklines_csr%ISOPEN THEN
991            CLOSE l_okcklines_csr;
992         END IF;
993 
994         IF l_fabooks_csr%ISOPEN THEN
995            CLOSE l_fabooks_csr;
996         END IF;
997 
998         x_return_status := OKL_API.G_RET_STS_ERROR;
999      WHEN OTHERS THEN
1000 
1001         IF (is_debug_exception_on) THEN
1002            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
1003         END IF;
1004 
1005         IF l_okcklines_csr%ISOPEN THEN
1006            CLOSE l_okcklines_csr;
1007         END IF;
1008 
1009         IF l_fabooks_csr%ISOPEN THEN
1010            CLOSE l_fabooks_csr;
1011         END IF;
1012 
1013          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
1014 
1015          -- Set the oracle error message
1016          OKL_API.set_message(
1017                          p_app_name      => OKC_API.G_APP_NAME,
1018                          p_msg_name      => g_unexpected_error,
1019                          p_token1        => g_sqlcode_token,
1020                          p_token1_value  => SQLCODE,
1021                          p_token2        => g_sqlerrm_token,
1022                          p_token2_value  => SQLERRM);
1023 
1024   END  delink_contract_from_asset;
1025 
1026   -- Start of comments
1027   --
1028   -- Procedure Name : process_amortize
1029   -- Desciption     : Calls the Amortization API to do amortization
1030   -- Business Rules :
1031   -- Parameters  :
1032   -- Version  : 1.0
1033   -- History        : RMUNJULU 04-MAR-04 3485854 Moved Process_amortize out of
1034   --                  process_amortize_and_return so that it can be called from
1035   --                  evergreen too.
1036   --                : RMUNJULU 3485854 11-MAR-04 Added code to set return status properly
1037   --                : rmunjulu EDAT Added code to get the quote eff date and check for early term based on that
1038   --                  Also pass quote eff date and quote acceptance date to amortize api
1039   --                : rmunjulu 4150696 Changed to pass contract end date as effective dates when calling
1040   --                  AMORTIZE API
1041   --                : sechawla 18-dec-07 6690811 - delink contract id from fa asset for full termination of
1042   --                  Booked (thru term quote or EOT) or Evergreen contract.
1043   --                : sechawla 21-dec-07 6690811 - reverted back the changes done on 18-dec-07 for the same bug
1044   --                  and added IF block at the end.
1045   --
1046   -- End of comments
1047   PROCEDURE process_amortize(
1048            p_api_version                 IN  NUMBER,
1049            p_init_msg_list               IN  VARCHAR2,
1050            x_return_status               OUT NOCOPY VARCHAR2,
1051            x_msg_count                   OUT NOCOPY NUMBER,
1052            x_msg_data                    OUT NOCOPY VARCHAR2,
1053            p_term_rec                    IN term_rec_type,
1054            px_overall_status             IN OUT NOCOPY VARCHAR2,
1055            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
1056            p_sys_date                    IN DATE,
1057            p_trn_already_set             IN VARCHAR2 DEFAULT NULL,
1058            p_call_origin                 IN VARCHAR2 DEFAULT NULL)  IS
1059 
1060    -- Cursor to get the end date of contract
1061    -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
1062    CURSOR get_k_end_date_csr ( p_khr_id IN NUMBER) IS
1063     SELECT end_date, sts_code    --sechawla 18-dec-07 6690811 : added sts_code
1064     FROM   OKC_K_HEADERS_b       --sechawla 18-dec-07 6690811 : changed OKC_K_HEADERS_V to OKC_K_HEADERS_b
1065     WHERE  id =  p_khr_id;
1066 
1067    --sechawla 18-dec-07 6690811
1068    l_k_sts_code            VARCHAR2(30);
1069 
1070    l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
1071    l_overall_status        VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
1072 
1073    l_k_end_date            DATE := OKL_API.G_MISS_DATE;
1074 
1075    l_early_term_yn         VARCHAR2(1) := G_NO;
1076 
1077    l_trn_already_set   VARCHAR2(3);
1078    l_evergreen_earlier VARCHAR2(3) := 'N';
1079 
1080    -- rmunjulu EDAT
1081    l_quote_accpt_date DATE;
1082    l_quote_eff_date DATE;
1083 
1084    -- asawanka added for debug feature start
1085     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_amortize';
1086     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
1087     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
1088     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
1089     -- asawanka added for debug feature end
1090 
1091   BEGIN  -- begin for amortize
1092      IF (is_debug_procedure_on) THEN
1093        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
1094      END IF;
1095 
1096      -- sechawla -
1097      -- p_call_origin = EVERGREEN, when contract is getting converted to Evergreen
1098      -- p_call_origin = TERMINATION, during full termination of Evergreen contract
1099      -- p_call_origin = TERMINATION, during EOT processing of a Booked contract
1100      -- p_call_origin = TERMINATION, during full termination of a Booked contract
1101       -- Start a savepoint
1102       SAVEPOINT asset_amortize;
1103 
1104       IF (is_debug_statement_on) THEN
1105          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_trn_already_set = '||p_trn_already_set);
1106          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'px_tcnv_rec.tmt_amortization_yn = '||px_tcnv_rec.tmt_amortization_yn);
1107          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_term_rec.p_contract_id =  '||p_term_rec.p_contract_id);
1108          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'px_tcnv_rec.id = '||px_tcnv_rec.id);
1109       END IF;
1110       IF p_trn_already_set IS NULL THEN
1111         l_trn_already_set := G_NO;
1112       ELSE
1113         l_trn_already_set := p_trn_already_set;
1114       END IF;
1115 
1116       -- Check if amortization required
1117       IF (l_trn_already_set = G_YES
1118           AND NVL(px_tcnv_rec.tmt_amortization_yn, '?') <> G_YES)
1119       OR (l_trn_already_set = G_NO) THEN
1120 
1121           -- CHECK TO see IF old evergreen transaction exists
1122           -- Check if another transaction exists which is processed and for which tmt_evergreen_yn was Y
1123           -- which means this contract was evergreen earlier
1124           -- so no need to run amortization again
1125           IF (is_debug_statement_on) THEN
1126                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'calling check_k_evergreen_ear ');
1127           END IF;
1128           l_evergreen_earlier := check_k_evergreen_ear(
1129                                     p_khr_id          => p_term_rec.p_contract_id,
1130                                     p_tcn_id          => px_tcnv_rec.id,
1131                                     x_return_status   => l_return_status);
1132 
1133           IF (is_debug_statement_on) THEN
1134               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'called check_k_evergreen_ear l_return_status =  '||l_return_status);
1135               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_evergreen_earlier = '||l_evergreen_earlier);
1136           END IF;
1137 
1138           IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
1139 
1140             -- Error occurred during the creation of an amortization transaction
1141             -- for assets of contract CONTRACT_NUMBER.
1142             OKL_API.set_message( p_app_name      => G_APP_NAME,
1143                                  p_msg_name      => 'OKL_AM_ERR_AMORTIZE',
1144                                  p_token1        => 'CONTRACT_NUMBER',
1145                                  p_token1_value  => p_term_rec.p_contract_number);
1146 
1147           END IF;
1148 
1149           --sechawla 18-dec-07 6690811 : Moved the folllowing code outside here from the IF below
1150           OPEN  get_k_end_date_csr(p_term_rec.p_contract_id);
1151           FETCH get_k_end_date_csr INTO l_k_end_date, l_k_sts_code; --sechawla 18-dec-07 6690811 : added l_k_sts_code
1152           CLOSE get_k_end_date_csr;
1153           IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
1154 
1155              l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
1156              l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
1157 
1158           ELSE
1159 
1160              l_quote_accpt_date := l_k_end_date;  -- rmunjulu 4150696 Changed to pass contract end date
1161              l_quote_eff_date := l_k_end_date; -- rmunjulu 4150696 Changed to pass contract end date
1162 
1163           END IF;
1164           IF (l_k_end_date <> OKL_API.G_MISS_DATE)
1165           AND (TRUNC(l_k_end_date) > TRUNC(l_quote_eff_date)) THEN -- rmunjulu EDAT
1166             l_early_term_yn := G_YES;
1167           END IF;
1168           --sechawla 18-dec-07 6690811 : end move
1169 
1170 
1171           -- Check to make sure amortization was not done
1172           IF NVL(l_evergreen_earlier,'N') <> 'Y' THEN  --termination(full) of a Booked contract
1173              --sechawla - This condition is met for Full Termination of a Booked contract (thru term quote or EOT)
1174              --and also when contract is changing to Evergreen
1175 
1176           -- RMUNJULU 3018641 Step Message
1177           -- Step : Amortization
1178           OKL_API.set_message(
1179                         p_app_name      => G_APP_NAME,
1180                         p_msg_name      => 'OKL_AM_STEP_AMT');
1181 
1182           -- call amortization
1183 
1184           --sechawla 18-dec-07 6690811 : Moved outside the IF
1185           --OPEN  get_k_end_date_csr(p_term_rec.p_contract_id);
1186           --FETCH get_k_end_date_csr INTO l_k_end_date, l_k_sts_code; --sechawla 18-dec-07 6690811 : added l_k_sts_code
1187           --CLOSE get_k_end_date_csr;
1188 
1189     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
1190 
1191           -- rmunjulu EDAT
1192           -- If quote exists then cancelation date is quote eff from date else sysdate
1193 
1194           /* --sechawla 18-dec-07 6690811 : Moved outside the IF
1195           IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
1196 
1197              l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
1198              l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
1199 
1200           ELSE
1201 
1202              l_quote_accpt_date := l_k_end_date;  -- rmunjulu 4150696 Changed to pass contract end date
1203              l_quote_eff_date := l_k_end_date; -- rmunjulu 4150696 Changed to pass contract end date
1204 
1205           END IF;
1206           */
1207 
1208     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
1209 
1210           /* --sechawla 18-dec-07 6690811 : Moved outside the IF
1211           IF (l_k_end_date <> OKL_API.G_MISS_DATE)
1212           AND (TRUNC(l_k_end_date) > TRUNC(l_quote_eff_date)) THEN -- rmunjulu EDAT
1213             l_early_term_yn := G_YES;
1214           END IF;
1215           */
1216 
1217           IF (is_debug_statement_on) THEN
1218                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'calling OKL_AM_AMORTIZE_PUB.create_offlease_asset_trx ');
1219                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_early_term_yn = '||l_early_term_yn);
1220                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_quote_eff_date = '||l_quote_eff_date);
1221                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_quote_accpt_date =  '||l_quote_accpt_date);
1222           END IF;
1223           OKL_AM_AMORTIZE_PUB.create_offlease_asset_trx(
1224             p_api_version                => p_api_version,
1225             p_init_msg_list              => OKL_API.G_FALSE,
1226             x_return_status              => l_return_status,
1227             x_msg_count                  => x_msg_count,
1228             x_msg_data                   => x_msg_data,
1229             p_contract_id                 => p_term_rec.p_contract_id,
1230             p_early_termination_yn        => l_early_term_yn,
1231             p_quote_eff_date              => l_quote_eff_date,    -- rmunjulu EDAT
1232             p_quote_accpt_date            => l_quote_accpt_date); -- rmunjulu EDAT
1233 
1234           IF (is_debug_statement_on) THEN
1235                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'called OKL_AM_AMORTIZE_PUB.create_offlease_asset_trx l_return_status =  '||l_return_status);
1236           END IF;
1237           IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
1238 
1239             -- Error occurred during the creation of an amortization transaction
1240             -- for assets of contract CONTRACT_NUMBER.
1241             OKL_API.set_message( p_app_name      => G_APP_NAME,
1242                                  p_msg_name      => 'OKL_AM_ERR_AMORTIZE',
1243                                  p_token1        => 'CONTRACT_NUMBER',
1244                                  p_token1_value  => p_term_rec.p_contract_number);
1245 
1246           END IF;
1247 
1248           -- Raise exception to rollback to savepoint if error
1249           IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1250              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1251           ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
1252              RAISE OKL_API.G_EXCEPTION_ERROR;
1253           END IF;
1254 
1255 
1256           -- store the highest degree of error
1257           IF (is_debug_statement_on) THEN
1258                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'calling set_overall_status');
1259           END IF;
1260           set_overall_status(
1261             p_return_status               => l_return_status,
1262             px_overall_status             => px_overall_status);
1263           IF (is_debug_statement_on) THEN
1264                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'called set_overall_status');
1265           END IF;
1266 
1267           -- set the transaction record for amortization
1268           IF (is_debug_statement_on) THEN
1269                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'calling set_transaction_rec');
1270           END IF;
1271           set_transaction_rec(
1272             p_return_status               => l_return_status,
1273             p_overall_status              => px_overall_status,
1274             p_tmt_flag                    => 'TMT_AMORTIZATION_YN',
1275             p_tsu_code                    => 'WORKING',
1276             px_tcnv_rec                   => px_tcnv_rec);
1277 
1278           IF (is_debug_statement_on) THEN
1279                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'called set_transaction_rec');
1280           END IF;
1281 
1282           ELSE
1283             -- Message : Amortization done earlier
1284             NULL;
1285 
1286           END IF;
1287 
1288           -- rmunjulu bug 6853566 The call to delink should be in this IF so that it is run only
1289           -- when amortize is done, also set a message if error and set the amortize step flag
1290           --sechawla 21-dec-07 6690811 - Delink Contract ID upon full termination of a Booked contract - begin
1291           IF p_call_origin <> 'EVERGREEN' then
1292           -- Delink the contract ID in case of full termination of a Booked contract, full termination of an
1293           -- Evergreen contract and contract expiration. Do not delink contract ID when contract is converting to
1294           -- Evergreen (p_call_origin = 'EVERGREEN')
1295           delink_contract_from_asset(
1296                        p_api_version      => p_api_version,
1297                        x_msg_count        => x_msg_count,
1298                        x_msg_data         => x_msg_data,
1299                        p_khr_id           => p_term_rec.p_contract_id,
1300                        p_sts_code         => l_k_sts_code,
1301                        p_quote_accpt_date => l_quote_accpt_date,
1302                        p_quote_eff_date   => l_quote_eff_date,
1303                        x_return_status    => l_return_status);
1304 
1305           -- rmunjulu bug 6853566
1306           set_transaction_rec(
1307             p_return_status               => l_return_status,
1308             p_overall_status              => px_overall_status,
1309             p_tmt_flag                    => 'TMT_AMORTIZATION_YN',
1310             p_tsu_code                    => 'WORKING',
1311             px_tcnv_rec                   => px_tcnv_rec);
1312 
1313           -- rmunjulu bug 6853566
1314           IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
1315 
1316             -- Error occurred during the creation of an amortization transaction
1317             -- for assets of contract CONTRACT_NUMBER.
1318             OKL_API.set_message( p_app_name      => G_APP_NAME,
1319                                  p_msg_name      => 'OKL_AM_ERR_AMORTIZE',
1320                                  p_token1        => 'CONTRACT_NUMBER',
1321                                  p_token1_value  => p_term_rec.p_contract_number);
1322 
1323           END IF;
1324 
1325           IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1326              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1327           ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
1328              RAISE OKL_API.G_EXCEPTION_ERROR;
1329           END IF;
1330           END IF;
1331           --sechawla 21-dec-07 6690811 - Delink Contract ID upon full termination of a Booked contract - end
1332 
1333       END IF;
1334 
1335 
1336       -- RMUNJULU 3485854 11-MAR-04 Set the return_status
1337       x_return_status := l_return_status;
1338 
1339     IF (is_debug_procedure_on) THEN
1340        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
1341     END IF;
1342 
1343   EXCEPTION
1344       WHEN OKL_API.G_EXCEPTION_ERROR THEN
1345         IF (is_debug_exception_on) THEN
1346              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
1347         END IF;
1348 
1349          IF get_k_end_date_csr%ISOPEN THEN
1350             CLOSE get_k_end_date_csr;
1351          END IF;
1352 
1353          ROLLBACK TO asset_amortize;
1354 
1355          x_return_status := OKL_API.G_RET_STS_ERROR;
1356 
1357          -- store the highest degree of error
1358          set_overall_status(
1359                p_return_status                 => x_return_status,
1360                px_overall_status               => px_overall_status);
1361 
1362          -- set the transaction record
1363          set_transaction_rec(
1364                p_return_status                 => x_return_status,
1365                p_overall_status                => px_overall_status,
1366                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
1367                p_tsu_code                      => 'ERROR',
1368                px_tcnv_rec                     => px_tcnv_rec);
1369 
1370          -- set the transaction record
1371          set_transaction_rec(
1372                p_return_status                 => x_return_status,
1373                p_overall_status                => px_overall_status,
1374                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
1375                p_tsu_code                      => 'ERROR',
1376                p_ret_val                       => NULL,
1377                px_tcnv_rec                     => px_tcnv_rec);
1378 
1379       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1380         IF (is_debug_exception_on) THEN
1381              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
1382         END IF;
1383 
1384          IF get_k_end_date_csr%ISOPEN THEN
1385             CLOSE get_k_end_date_csr;
1386          END IF;
1387 
1388          ROLLBACK TO asset_amortize;
1389 
1390          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
1391 
1392          -- store the highest degree of error
1393          set_overall_status(
1394                p_return_status                 => x_return_status,
1395                px_overall_status               => px_overall_status);
1396 
1397          -- set the transaction record
1398          set_transaction_rec(
1399                p_return_status                 => x_return_status,
1400                p_overall_status                => px_overall_status,
1401                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
1402                p_tsu_code                      => 'ERROR',
1403                px_tcnv_rec                     => px_tcnv_rec);
1404 
1405          -- set the transaction record
1406          set_transaction_rec(
1407                p_return_status                 => x_return_status,
1408                p_overall_status                => px_overall_status,
1409                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
1410                p_tsu_code                      => 'ERROR',
1411                p_ret_val                       => NULL,
1412                px_tcnv_rec                     => px_tcnv_rec);
1413 
1414       WHEN OTHERS THEN
1415         IF (is_debug_exception_on) THEN
1416            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
1417         END IF;
1418 
1419          IF get_k_end_date_csr%ISOPEN THEN
1420             CLOSE get_k_end_date_csr;
1421          END IF;
1422 
1423          ROLLBACK TO asset_amortize;
1424 
1425          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
1426 
1427          -- store the highest degree of error
1428          set_overall_status(
1429                p_return_status                 => x_return_status,
1430                px_overall_status               => px_overall_status);
1431 
1432          -- set the transaction record
1433          set_transaction_rec(
1434                p_return_status                 => x_return_status,
1435                p_overall_status                => px_overall_status,
1436                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
1437                p_tsu_code                      => 'ERROR',
1438                px_tcnv_rec                     => px_tcnv_rec);
1439 
1440          -- set the transaction record
1441          set_transaction_rec(
1442                p_return_status                 => x_return_status,
1443                p_overall_status                => px_overall_status,
1444                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
1445                p_tsu_code                      => 'ERROR',
1446                p_ret_val                       => NULL,
1447                px_tcnv_rec                     => px_tcnv_rec);
1448 
1449          -- Set the oracle error message
1450          OKL_API.set_message(
1451                          p_app_name      => OKC_API.G_APP_NAME,
1452                          p_msg_name      => g_unexpected_error,
1453                          p_token1        => g_sqlcode_token,
1454                          p_token1_value  => SQLCODE,
1455                          p_token2        => g_sqlerrm_token,
1456                          p_token2_value  => SQLERRM);
1457 
1458   END process_amortize;
1459 
1460 
1461   -- Start of comments
1462   --
1463   -- Procedure Name : evergreen_eligibility
1464   -- Description : Checks if contract eligible for evergreen
1465   -- Business Rules :
1466   -- Parameters  :
1467   -- Version  : 1.0
1468   --
1469   -- End of comments
1470   PROCEDURE evergreen_eligibility(
1471            p_term_rec                    IN term_rec_type,
1472            x_return_status               OUT NOCOPY VARCHAR2,
1473            x_rule_found                  OUT NOCOPY VARCHAR2,
1474            x_msg_count                   OUT NOCOPY NUMBER,
1475            x_msg_data                    OUT NOCOPY VARCHAR2)  IS
1476 
1477      l_return_status           VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
1478      l_rule_found             VARCHAR2(1) := G_NO;
1479      l_rulv_rec              OKL_AM_CALCULATE_QUOTE_PVT.rulv_rec_type;
1480      l_rule_code            CONSTANT VARCHAR2(30) := 'LAEVEL';
1481 
1482      -- asawanka added for debug feature start
1483     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'evergreen_eligibility';
1484     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
1485     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
1486     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
1487     -- asawanka added for debug feature end
1488   BEGIN
1489      IF (is_debug_procedure_on) THEN
1490        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
1491      END IF;
1492     IF (is_debug_statement_on) THEN
1493        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'calling okl_am_util_pvt.get_rule_record p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
1494     END IF;
1495     OKL_AM_UTIL_PVT.get_rule_record(
1496     p_rgd_code             => 'LAEVEL',
1497         p_rdf_code             => l_rule_code,
1498     p_chr_id               => p_term_rec.p_contract_id,
1499     p_cle_id               => NULL,
1500         x_rulv_rec             => l_rulv_rec,
1501        x_return_status         => l_return_status,
1502       p_message_yn           => FALSE);
1503     IF (is_debug_statement_on) THEN
1504        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'called okl_am_util_pvt.get_rule_record l_return_status = '||l_return_status);
1505        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_rulv_rec.rule_information1 = '||l_rulv_rec.rule_information1);
1506     END IF;
1507     IF l_return_status = OKL_API.G_RET_STS_SUCCESS THEN
1508       IF (NVL (l_rulv_rec.rule_information1, '*') = G_YES) THEN
1509         l_rule_found := G_YES;
1510       END IF;
1511     ELSIF l_return_status = OKL_API.G_RET_STS_ERROR THEN
1512       l_return_status := OKL_API.G_RET_STS_SUCCESS;
1513       l_rule_found    := G_NO;
1514     END IF;
1515 
1516     x_return_status  := l_return_status;
1517     x_rule_found     := l_rule_found;
1518 
1519     IF (is_debug_procedure_on) THEN
1520        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
1521     END IF;
1522   EXCEPTION
1523 
1524     WHEN OKL_API.G_EXCEPTION_ERROR THEN
1525         IF (is_debug_exception_on) THEN
1526              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
1527         END IF;
1528 
1529      x_return_status := OKL_API.G_RET_STS_ERROR;
1530 
1531     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1532         IF (is_debug_exception_on) THEN
1533              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
1534         END IF;
1535 
1536      x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
1537 
1538     WHEN OTHERS THEN
1539         IF (is_debug_exception_on) THEN
1540            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
1541         END IF;
1542 
1543      -- Set the oracle error message
1544      OKL_API.set_message(p_app_name      => OKC_API.G_APP_NAME,
1545                          p_msg_name      => g_unexpected_error,
1546                          p_token1        => g_sqlcode_token,
1547                          p_token1_value  => SQLCODE,
1548                          p_token2        => g_sqlerrm_token,
1549                          p_token2_value  => SQLERRM);
1550      x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
1551   END evergreen_eligibility;
1552 
1553 
1554   -- Start of comments
1555   --
1556   -- Procedure Name : get_asset_name
1557   -- Description   : gets the name of asset for the line
1558   -- Business Rules :
1559   -- Parameters    :
1560   -- Version      : 1.0
1561   --
1562   -- End of comments
1563   FUNCTION get_asset_name ( p_kle_id IN NUMBER ) RETURN VARCHAR2 IS
1564 
1565     -- Cursor to get the asset name for the line number passed
1566     CURSOR  k_lines_csr(p_cle_id IN NUMBER) IS
1567     SELECT  OKLV.name           name
1568     FROM    OKC_K_LINES_V       OKLV
1569     WHERE   OKLV.id   = p_cle_id;
1570 
1571     l_name   OKC_K_LINES_V.name%TYPE;
1572 
1573     -- asawanka added for debug feature start
1574     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'get_asset_name';
1575     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
1576     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
1577     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
1578     -- asawanka added for debug feature end
1579   BEGIN
1580      IF (is_debug_procedure_on) THEN
1581        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
1582      END IF;
1583 
1584     IF (is_debug_statement_on) THEN
1585        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_kle_id = '||p_kle_id);
1586     END IF;
1587 
1588     OPEN k_lines_csr (p_kle_id);
1589     FETCH k_lines_csr INTO l_name;
1590     IF (is_debug_statement_on) THEN
1591        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_name = '||l_name);
1592     END IF;
1593     IF k_lines_csr%FOUND THEN
1594       IF (is_debug_procedure_on) THEN
1595         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
1596       END IF;
1597       RETURN l_name;
1598     ELSE
1599       IF (is_debug_procedure_on) THEN
1600         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
1601       END IF;
1602       RETURN NULL;
1603     END IF;
1604     CLOSE k_lines_csr;
1605 
1606 
1607   EXCEPTION
1608     WHEN OTHERS THEN
1609         IF (is_debug_exception_on) THEN
1610            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
1611         END IF;
1612 
1613      IF k_lines_csr%ISOPEN THEN
1614         CLOSE k_lines_csr;
1615      END IF;
1616 
1617      -- Set the oracle error message
1618      OKL_API.set_message(p_app_name      => OKC_API.G_APP_NAME,
1619                          p_msg_name      => g_unexpected_error,
1620                          p_token1        => g_sqlcode_token,
1621                          p_token1_value  => SQLCODE,
1622                          p_token2        => g_sqlerrm_token,
1623                          p_token2_value  => SQLERRM);
1624      RETURN NULL;
1625   END get_asset_name;
1626 
1627 
1628   -- Start of comments
1629   --
1630   -- Procedure Name : initialize_transaction
1631   -- Description : Initializes the transaction record for the contract
1632   -- Business Rules :
1633   -- Parameters  :
1634   -- Version  : 1.0
1635   -- History        : RMUNJULU 05-MAR-03 Added code to store tmt_recycle_yn flag
1636   --
1637   -- End of comments
1638   PROCEDURE initialize_transaction (
1639     px_tcnv_rec                    IN OUT NOCOPY tcnv_rec_type,
1640     p_term_rec                     IN term_rec_type,
1641     p_sys_date                     IN DATE,
1642     p_control_flag                 IN VARCHAR2,
1643     x_return_status                OUT NOCOPY VARCHAR2,
1644     --akrangan bug 5354501 fix start
1645     x_msg_count                    OUT NOCOPY NUMBER,
1646     x_msg_data                     OUT NOCOPY VARCHAR2) IS
1647 
1648 
1649  	    CURSOR l_k_end_csr ( p_khr_id IN NUMBER) IS
1650  	      SELECT khr.end_date,
1651  	             khr.sts_code
1652  	      FROM   OKC_K_HEADERS_V khr
1653  	      WHERE  khr.id = p_khr_id;
1654   --akrangan bug 5354501 fix end
1655 
1656    l_try_id                NUMBER;
1657    l_currency_code         VARCHAR2(2000);
1658    l_trans_meaning         VARCHAR2(200);
1659    l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
1660 --20-NOV-2006 ANSETHUR R12B - LEGAL ENTITY UPTAKE PROJECT
1661    l_legal_entity_id       NUMBER;
1662     --akrangan bug 5354501 fix start
1663     l_k_end_date            DATE;
1664     l_rule_found            VARCHAR2(1) := G_NO;
1665     l_try_name              VARCHAR2(30);
1666     l_trans_code            VARCHAR2(30);
1667     l_tcn_type              VARCHAR2(3);
1668     l_k_sts_code            VARCHAR2(200);
1669     l_evergreen_earlier     VARCHAR2(3) := 'N';
1670   --akrangan bug 5354501 fix end
1671 
1672   -- asawanka added for debug feature start
1673     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'initialize_transaction';
1674     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
1675     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
1676     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
1677     -- asawanka added for debug feature end
1678   BEGIN
1679      IF (is_debug_procedure_on) THEN
1680        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
1681      END IF;
1682 
1683     x_return_status   :=   OKL_API.G_RET_STS_SUCCESS;
1684 --20-NOV-2006 ANSETHUR R12B - LEGAL ENTITY UPTAKE PROJECT
1685       l_legal_entity_id     :=  OKL_LEGAL_ENTITY_UTIL.get_khr_le_id (p_term_rec.p_contract_id);
1686     IF (is_debug_statement_on) THEN
1687        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_legal_entity_id = '||l_legal_entity_id);
1688        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_control_flag = '||p_control_flag);
1689        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
1690     END IF;
1691     IF p_control_flag = 'CREATE' THEN
1692    --akrangan bug 5354501 fix start
1693  	       -- check if contract is eligible for Evergreen
1694 
1695  	         -- Get the contract end date
1696  	         OPEN l_k_end_csr ( p_term_rec.p_contract_id);
1697 
1698  	         FETCH l_k_end_csr INTO l_k_end_date, l_k_sts_code;
1699  	         CLOSE l_k_end_csr;
1700  	         IF (is_debug_statement_on) THEN
1701                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_k_end_date = '||l_k_end_date);
1702                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_k_sts_code = '||l_k_sts_code);
1703                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'px_tcnv_rec.id = '||px_tcnv_rec.id);
1704                  END IF;
1705  	         -- Check if this contract was evergreen earlier, so no need to set to evergreen again
1706  	         l_evergreen_earlier := check_k_evergreen_ear(
1707  	                                     p_khr_id          => p_term_rec.p_contract_id,
1708  	                                     p_tcn_id          => px_tcnv_rec.id,
1709  	                                     x_return_status   => l_return_status);
1710  	         IF (is_debug_statement_on) THEN
1711                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_evergreen_earlier = '||l_evergreen_earlier);
1712                  END IF;
1713  	             IF  (l_evergreen_earlier <> 'Y'
1714  	 --              AND p_term_rec.p_control_flag LIKE 'BATCH_PROCESS%'
1715  	                     AND TRUNC(l_k_end_date) <= TRUNC(p_sys_date)
1716  	                     AND (p_term_rec.p_quote_id IS NULL
1717  	                      OR p_term_rec.p_quote_id = OKL_API.G_MISS_NUM))THEN
1718 
1719  	            OKL_API.set_message(
1720  	                         p_app_name      => G_APP_NAME,
1721  	                         p_msg_name      => 'OKL_AM_STEP_EVR');
1722 
1723  	            -- get evergreen eligiblility
1724  	            evergreen_eligibility(
1725  	               p_term_rec                       => p_term_rec,
1726  	               x_return_status                  => l_return_status,
1727  	               x_rule_found                     => l_rule_found,
1728  	               x_msg_count                      => x_msg_count,
1729  	               x_msg_data                       => x_msg_data);
1730  	            IF (is_debug_statement_on) THEN
1731                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_return_status of evergreen_eligibility = '||l_return_status);
1732                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_rule_found = '||l_rule_found);
1733                    END IF;
1734  	            -- Raise exception to rollback to the savepoint
1735  	            IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
1736 
1737  	              -- Error getting evergreen rule for contract.
1738  	              OKL_API.set_message(
1739  	                                 p_app_name     => G_APP_NAME,
1740  	                                 p_msg_name     => 'OKL_AM_EVERGREEN_RULE_ERR');
1741  	              RAISE OKL_API.G_EXCEPTION_ERROR;
1742  	            END IF;
1743  	         END IF;
1744 
1745  	         -- if control_flag = "batch process" and evergreen_status = "ok" then
1746  	            -- update contract_status to evergreen
1747  	            -- exit (raise exception)
1748  	         -- end if
1749  	         IF  (l_rule_found = G_YES) THEN
1750  	               l_try_name := 'Evergreen';
1751  	               l_trans_code := 'EVERGREEN';
1752  	               l_tcn_type := 'EVG';
1753  	         ELSE
1754  	               l_try_name := 'Termination';
1755  	               l_trans_code := 'TERMINATION';
1756  	               l_tcn_type := 'TMT';
1757  	         END IF;
1758 --akrangan bug 5354501 fix end
1759       -- Get the Transaction Id
1760        IF (is_debug_statement_on) THEN
1761                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_AM_UTIL_PVT.get_transaction_id ');
1762                        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_try_name = '||l_try_name);
1763       END IF;
1764       OKL_AM_UTIL_PVT.get_transaction_id (
1765        p_try_name           => l_try_name , --akrangan bug 5354501 fix added l_try_name
1766        x_return_status       => l_return_status,
1767        x_try_id             => l_try_id);
1768       IF (is_debug_statement_on) THEN
1769        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_AM_UTIL_PVT.get_transaction_id l_return_status = '||l_return_status);
1770        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_trans_code = '||l_trans_code);
1771        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_try_id = '||l_try_id);
1772       END IF;
1773       -- Get the meaning of lookup
1774       l_trans_meaning := OKL_AM_UTIL_PVT.get_lookup_meaning(
1775                                p_lookup_type  => 'OKL_ACCOUNTING_EVENT_TYPE',
1776                                p_lookup_code => l_trans_code, --akrangan bug 5354501 fix added l_trans_code
1777                                p_validate_yn  => 'Y');
1778       IF (is_debug_statement_on) THEN
1779        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_trans_meaning = '||l_trans_meaning);
1780       END IF;
1781       IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
1782 
1783         -- Message: Unable to find a transaction type for the transaction TRY_NAME
1784         OKL_API.set_message(p_app_name            => G_APP_NAME,
1785                             p_msg_name            => 'OKL_AM_NO_TRX_TYPE_FOUND',
1786                             p_token1              => 'TRY_NAME',
1787                             p_token1_value        => l_trans_meaning);
1788 
1789         RAISE OKL_API.G_EXCEPTION_ERROR;
1790       END IF;
1791 
1792       -- Get the contract currency code
1793       l_currency_code := OKL_AM_UTIL_PVT.get_chr_currency(p_term_rec.p_contract_id);
1794       IF (is_debug_statement_on) THEN
1795        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_currency_code = '||l_currency_code);
1796       END IF;
1797       -- initialize the transaction rec
1798       px_tcnv_rec.khr_id                     := p_term_rec.p_contract_id;
1799       px_tcnv_rec.tcn_type                   := l_tcn_type; --akrangan bug 5354501 fix added l_tcn_type
1800       px_tcnv_rec.try_id                     := l_try_id;
1801       px_tcnv_rec.currency_code              := l_currency_code;
1802 --20-NOV-2006 ANSETHUR R12B - LEGAL ENTITY UPTAKE PROJECT
1803       px_tcnv_rec.legal_entity_id            := l_legal_entity_id;
1804     END IF;
1805 
1806 
1807     -- RMUNJULU 05-MAR-03 Get the tmt_recycle_yn flag if set and put in GLOBAL variable
1808     IF px_tcnv_rec.tmt_recycle_yn IS NULL
1809     OR px_tcnv_rec.tmt_recycle_yn = OKL_API.G_MISS_CHAR THEN
1810       G_TMT_RECYCLE_YN := NULL;
1811     ELSE
1812       G_TMT_RECYCLE_YN := px_tcnv_rec.tmt_recycle_yn;
1813     END IF;
1814     IF (is_debug_statement_on) THEN
1815        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_quote_id = '||p_term_rec.p_quote_id);
1816     END IF;
1817     -- Set the rest of the transaction rec
1818     px_tcnv_rec.qte_id                     := p_term_rec.p_quote_id;
1819     px_tcnv_rec.tsu_code                   := 'ENTERED';
1820     px_tcnv_rec.tmt_status_code            := 'ENTERED'; --akrangan changes for sla tmt_status_code cr
1821     px_tcnv_rec.date_transaction_occurred  := p_sys_date;
1822 --20-NOV-2006 ANSETHUR R12B - LEGAL ENTITY UPTAKE PROJECT
1823     px_tcnv_rec.legal_entity_id            := l_legal_entity_id;
1824 
1825     -- set the termination reason (TRN_CODE)
1826     IF (p_term_rec.p_termination_reason <> OKL_API.G_MISS_CHAR)
1827     AND (p_term_rec.p_termination_reason IS NOT NULL) THEN
1828       px_tcnv_rec.trn_code                := p_term_rec.p_termination_reason;
1829     ELSE
1830       px_tcnv_rec.trn_code                := 'EXP';
1831     END IF;
1832 
1833     IF (is_debug_procedure_on) THEN
1834        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
1835     END IF;
1836   EXCEPTION
1837      WHEN OKL_API.G_EXCEPTION_ERROR THEN
1838         IF (is_debug_exception_on) THEN
1839              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
1840         END IF;
1841 
1842        x_return_status   :=   OKL_API.G_RET_STS_ERROR;
1843      WHEN OTHERS THEN
1844         IF (is_debug_exception_on) THEN
1845            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
1846         END IF;
1847 
1848        OKL_API.set_message(
1849                          p_app_name      => OKC_API.G_APP_NAME,
1850                          p_msg_name      => g_unexpected_error,
1851                          p_token1        => g_sqlcode_token,
1852                          p_token1_value  => SQLCODE,
1853                          p_token2        => g_sqlerrm_token,
1854                          p_token2_value  => SQLERRM);
1855        x_return_status   :=   OKL_API.G_RET_STS_UNEXP_ERROR;
1856   END initialize_transaction;
1857 
1858 
1859   -- Start of comments
1860   --
1861   -- Procedure Name : get_contract_lines
1862   -- Description : Gets the financial lines for the contract
1863   -- Business Rules :
1864   -- Parameters  :
1865   -- Version  : 1.0
1866   -- History        : RMUNJULU 20-FEB-03 2757368 Changed k_lines_csr cursor to get only
1867   --                  active lines
1868   --
1869   -- End of comments
1870   PROCEDURE get_contract_lines(
1871            p_api_version                 IN  NUMBER,
1872            p_init_msg_list               IN  VARCHAR2,
1873            x_return_status               OUT NOCOPY VARCHAR2,
1874            x_msg_count                   OUT NOCOPY NUMBER,
1875            x_msg_data                    OUT NOCOPY VARCHAR2,
1876            p_term_rec                    IN term_rec_type,
1877            x_klev_tbl                    OUT NOCOPY klev_tbl_type)  IS
1878 
1879     -- Cursor to get the lines for the contracts -- Get only financial lines
1880     -- RMUNJULU 20-FEB-03 2757368 Changed cursor to get only active lines
1881     CURSOR  k_lines_csr(p_khr_id IN NUMBER) IS
1882     SELECT  OKLV.id             kle_id,
1883             OKLV.name           asset_name
1884     FROM    OKC_K_LINES_V       OKLV,
1885             OKC_LINE_STYLES_V   OLSV,
1886             OKC_K_HEADERS_V     KHR
1887     WHERE   OKLV.chr_id   = p_khr_id
1888     AND     OKLV.lse_id   = OLSV.id
1889     AND     OLSV.lty_code = 'FREE_FORM1'
1890     AND     OKLV.chr_id = KHR.id
1891     AND     OKLV.sts_code = KHR.sts_code;
1892 
1893 
1894     k_lines_rec          k_lines_csr%ROWTYPE;
1895     lx_klev_tbl          klev_tbl_type;
1896     i                    NUMBER := 1;
1897     l_return_status      VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
1898     l_api_name           VARCHAR2(30) := 'get_contract_lines';
1899 
1900     -- asawanka added for debug feature start
1901     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'get_contract_lines';
1902     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
1903     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
1904     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
1905     -- asawanka added for debug feature end
1906   BEGIN
1907      IF (is_debug_procedure_on) THEN
1908        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
1909      END IF;
1910 
1911     x_return_status := OKL_API.G_RET_STS_SUCCESS;
1912     l_return_status := OKL_API.START_ACTIVITY(l_api_name,
1913                                               p_init_msg_list,
1914                                               '_PVT',
1915                                               x_return_status);
1916 
1917     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1918       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1919     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
1920       RAISE OKL_API.G_EXCEPTION_ERROR;
1921     END IF;
1922     IF (is_debug_statement_on) THEN
1923        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
1924     END IF;
1925     i := 1;
1926     -- get the contract lines
1927     FOR k_lines_rec IN k_lines_csr ( p_term_rec.p_contract_id) LOOP
1928        lx_klev_tbl(i).p_kle_id     := k_lines_rec.kle_id;
1929        lx_klev_tbl(i).p_asset_name := k_lines_rec.asset_name;
1930        i := i + 1;
1931     END LOOP;
1932 
1933     x_return_status      := l_return_status;
1934     x_klev_tbl           := lx_klev_tbl;
1935     OKL_API.END_ACTIVITY(x_msg_count, x_msg_data);
1936     IF (is_debug_procedure_on) THEN
1937        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
1938     END IF;
1939   EXCEPTION
1940     WHEN OKL_API.G_EXCEPTION_ERROR THEN
1941         IF (is_debug_exception_on) THEN
1942              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
1943         END IF;
1944 
1945       x_return_status := OKL_API.HANDLE_EXCEPTIONS
1946       (
1947         l_api_name,
1948         G_PKG_NAME,
1949         'OKL_API.G_RET_STS_ERROR',
1950         x_msg_count,
1951         x_msg_data,
1952         '_PVT'
1953       );
1954     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1955         IF (is_debug_exception_on) THEN
1956              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
1957         END IF;
1958 
1959       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
1960       (
1961         l_api_name,
1962         G_PKG_NAME,
1963         'OKL_API.G_RET_STS_UNEXP_ERROR',
1964         x_msg_count,
1965         x_msg_data,
1966         '_PVT'
1967       );
1968     WHEN OTHERS THEN
1969         IF (is_debug_exception_on) THEN
1970            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
1971         END IF;
1972 
1973       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
1974       (
1975         l_api_name,
1976         G_PKG_NAME,
1977         'OTHERS',
1978         x_msg_count,
1979         x_msg_data,
1980         '_PVT'
1981       );
1982   END get_contract_lines;
1983 
1984 
1985   -- Start of comments
1986   --
1987   -- Procedure Name : set_overall_status
1988   -- Description : Sets the overall status for the api
1989   -- Business Rules :
1990   -- Parameters  :
1991   -- Version  : 1.0
1992   --
1993   -- End of comments
1994   PROCEDURE set_overall_status(
1995            p_return_status               IN VARCHAR2,
1996            px_overall_status             IN OUT NOCOPY VARCHAR2)  IS
1997   -- asawanka added for debug feature start
1998     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'set_overall_status';
1999     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
2000     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
2001     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
2002     -- asawanka added for debug feature end
2003   BEGIN
2004      IF (is_debug_procedure_on) THEN
2005        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
2006      END IF;
2007 
2008     -- Store the highest degree of error
2009     -- Set p_overall_status only if p_overall_status was successful and
2010     -- p_return_status is not null
2011     IF px_overall_status = OKL_API.G_RET_STS_SUCCESS
2012     AND (p_return_status IS NOT NULL
2013          OR p_return_status <> OKL_API.G_MISS_CHAR) THEN
2014         px_overall_status := p_return_status;
2015     END IF;
2016     IF (is_debug_procedure_on) THEN
2017        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
2018     END IF;
2019   EXCEPTION
2020 
2021      WHEN OTHERS THEN
2022         IF (is_debug_exception_on) THEN
2023            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
2024         END IF;
2025 
2026         OKL_API.set_message(
2027                          p_app_name      => OKC_API.G_APP_NAME,
2028                          p_msg_name      => g_unexpected_error,
2029                          p_token1        => g_sqlcode_token,
2030                          p_token1_value  => SQLCODE,
2031                          p_token2        => g_sqlerrm_token,
2032                          p_token2_value  => SQLERRM);
2033 
2034   END set_overall_status;
2035 
2036 
2037   -- Start of comments
2038   --
2039   -- Procedure Name : set_database_values
2040   -- Description : Sets the database values
2041   -- Business Rules :
2042   -- Parameters  :
2043   -- Version  : 1.0
2044   -- History        : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
2045   --
2046   -- End of comments
2047   PROCEDURE set_database_values(
2048            px_term_rec             IN OUT NOCOPY term_rec_type)  IS
2049 
2050     -- Cursor to get the quote details
2051     CURSOR get_quote_details_csr ( p_quote_id IN NUMBER ) IS
2052       SELECT qtp_code,
2053              qrs_code
2054       FROM   OKL_TRX_QUOTES_V
2055       WHERE  id = p_quote_id;
2056 
2057     -- Cursor to get the k details
2058     -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
2059     CURSOR get_k_details_csr (p_khr_id IN NUMBER) IS
2060       SELECT contract_number
2061       FROM   OKC_K_HEADERS_V
2062       WHERE  id = p_khr_id;
2063   -- asawanka added for debug feature start
2064     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'set_database_values';
2065     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
2066     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
2067     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
2068     -- asawanka added for debug feature end
2069   BEGIN
2070      IF (is_debug_procedure_on) THEN
2071        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
2072      END IF;
2073     IF (is_debug_statement_on) THEN
2074        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'px_term_rec.p_contract_id = '||px_term_rec.p_contract_id);
2075        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'px_term_rec.p_quote_id = '||px_term_rec.p_quote_id);
2076     END IF;
2077     FOR get_k_details_rec IN get_k_details_csr(px_term_rec.p_contract_id) LOOP
2078 
2079        px_term_rec.p_contract_number   :=  get_k_details_rec.contract_number;
2080 
2081     END LOOP;
2082 
2083     -- If the termination request is from quote, populate the rest of the quote attributes
2084     IF  px_term_rec.p_quote_id IS NOT NULL
2085     AND px_term_rec.p_quote_id <> OKL_API.G_MISS_NUM THEN
2086 
2087        FOR get_quote_details_rec IN get_quote_details_csr(px_term_rec.p_quote_id) LOOP
2088 
2089          px_term_rec.p_quote_type    :=   get_quote_details_rec.qtp_code;
2090          px_term_rec.p_quote_reason  :=   get_quote_details_rec.qrs_code;
2091 
2092        END LOOP;
2093 
2094     END IF;
2095     IF (is_debug_procedure_on) THEN
2096        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
2097     END IF;
2098   EXCEPTION
2099 
2100      WHEN OTHERS THEN
2101         IF (is_debug_exception_on) THEN
2102            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
2103         END IF;
2104 
2105         OKL_API.set_message(
2106                          p_app_name      => OKC_API.G_APP_NAME,
2107                          p_msg_name      => g_unexpected_error,
2108                          p_token1        => g_sqlcode_token,
2109                          p_token1_value  => SQLCODE,
2110                          p_token2        => g_sqlerrm_token,
2111                          p_token2_value  => SQLERRM);
2112 
2113   END set_database_values;
2114 
2115 
2116   -- Start of comments
2117   --
2118   -- Procedure Name : set_info_messages
2119   -- Description   : Sets the info messages when lease termination invoked
2120   -- Business Rules :
2121   -- Parameters    :
2122   -- Version      : 1.0
2123   --
2124   -- End of comments
2125   PROCEDURE set_info_messages(
2126            p_term_rec             IN term_rec_type)  IS
2127 
2128     l_quote_type VARCHAR2(2000);
2129   -- asawanka added for debug feature start
2130     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'set_info_messages';
2131     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
2132     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
2133     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
2134     -- asawanka added for debug feature end
2135   BEGIN
2136      IF (is_debug_procedure_on) THEN
2137        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
2138      END IF;
2139     IF (is_debug_statement_on) THEN
2140        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_control_flag = '||p_term_rec.p_control_flag);
2141     END IF;
2142     -- Check and Set the message saying where the termination request came from
2143     IF (p_term_rec.p_control_flag = 'CONTRACT_TERMINATE_SCRN') THEN
2144       -- Message: Termination request from Request Contract Termination screen
2145       -- for contract CONTRACT_NUMBER.
2146       OKL_API.set_message( p_app_name      => G_APP_NAME,
2147                            p_msg_name      => 'OKL_AM_TERM_REQ_FRM_SCRN',
2148                            p_token1        => 'CONTRACT_NUMBER',
2149                            p_token1_value  => p_term_rec.p_contract_number);
2150 
2151       -- Set the additional message to let the user know if there was a quote
2152       IF  p_term_rec.p_quote_id IS NOT NULL
2153       AND p_term_rec.p_quote_id <> OKL_API.G_MISS_NUM THEN
2154 
2155         -- Get the lookup meaning for quote type
2156         l_quote_type := OKL_AM_UTIL_PVT.get_lookup_meaning(
2157                                       p_lookup_type  => 'OKL_QUOTE_TYPE',
2158                                       p_lookup_code  => p_term_rec.p_quote_type,
2159                                       p_validate_yn  => G_YES);
2160 
2161         --Message:Termination request from accepted QUOTE_TYPE
2162         -- for contract CONTRACT_NUMBER.
2163         OKL_API.set_message( p_app_name      => G_APP_NAME,
2164                              p_msg_name      => 'OKL_AM_TERM_REQ_FRM_QTE',
2165                              p_token1        => 'QUOTE_TYPE',
2166                              p_token1_value  => l_quote_type,
2167                              p_token2        => 'CONTRACT_NUMBER',
2168                              p_token2_value  => p_term_rec.p_contract_number);
2169       END IF;
2170 
2171     ELSIF (p_term_rec.p_control_flag = 'TRMNT_QUOTE_UPDATE') THEN
2172 
2173       -- Get the lookup meaning for quote type
2174       l_quote_type := OKL_AM_UTIL_PVT.get_lookup_meaning(
2175                                       p_lookup_type  => 'OKL_QUOTE_TYPE',
2176                                       p_lookup_code  => p_term_rec.p_quote_type,
2177                                       p_validate_yn  => G_YES);
2178 
2179       --Message:Termination request from accepted QUOTE_TYPE
2180       -- for contract CONTRACT_NUMBER.
2181       OKL_API.set_message( p_app_name      => G_APP_NAME,
2182                            p_msg_name      => 'OKL_AM_TERM_REQ_FRM_QTE',
2183                            p_token1        => 'QUOTE_TYPE',
2184                            p_token1_value  => l_quote_type,
2185                            p_token2        => 'CONTRACT_NUMBER',
2186                            p_token2_value  => p_term_rec.p_contract_number);
2187 
2188     ELSIF (p_term_rec.p_control_flag = 'BATCH_PROCESS') THEN
2189       -- Message : Auto termination request for contract CONTRACT_NUMBER.
2190       OKL_API.set_message( p_app_name      => G_APP_NAME,
2191                            p_msg_name      => 'OKL_AM_AUTO_TERM_REQ',
2192                            p_token1        => 'CONTRACT_NUMBER',
2193                            p_token1_value  => p_term_rec.p_contract_number);
2194     END IF;
2195     IF (is_debug_procedure_on) THEN
2196        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
2197     END IF;
2198   EXCEPTION
2199 
2200      WHEN OTHERS THEN
2201         IF (is_debug_exception_on) THEN
2202            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
2203         END IF;
2204 
2205         OKL_API.set_message(
2206                          p_app_name      => OKC_API.G_APP_NAME,
2207                          p_msg_name      => g_unexpected_error,
2208                          p_token1        => g_sqlcode_token,
2209                          p_token1_value  => SQLCODE,
2210                          p_token2        => g_sqlerrm_token,
2211                          p_token2_value  => SQLERRM);
2212 
2213   END set_info_messages;
2214 
2215   -- Start of comments
2216   --
2217   -- Procedure Name : set_transaction_rec
2218   -- Description : Sets the transaction record for the contract
2219   -- Business Rules :
2220   -- Parameters  :
2221   -- Version  : 1.0
2222   -- History        : rmunjulu BUYOUT_PROCESS
2223   --
2224   -- End of comments
2225   PROCEDURE set_transaction_rec(
2226            p_return_status              IN VARCHAR2,
2227            p_overall_status             IN VARCHAR2,
2228            p_tmt_flag                   IN VARCHAR2,
2229            p_tsu_code                   IN VARCHAR2,
2230            p_ret_val                    IN VARCHAR2,
2231            px_tcnv_rec                  IN OUT NOCOPY tcnv_rec_type)  IS
2232     -- asawanka added for debug feature start
2233     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'set_transaction_rec';
2234     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
2235     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
2236     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
2237     -- asawanka added for debug feature end
2238   BEGIN
2239      IF (is_debug_procedure_on) THEN
2240        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
2241      END IF;
2242 
2243     -- set the transaction record
2244     IF (p_overall_status = OKL_API.G_RET_STS_SUCCESS) THEN
2245       px_tcnv_rec.tmt_status_code := p_tsu_code; --akrangan changes for sla tmt_status_code cr
2246     ELSE
2247       px_tcnv_rec.tmt_status_code := 'ERROR'; --akrangan changes for sla tmt_status_code cr
2248     END IF;
2249 
2250     IF (p_ret_val = OKL_API.G_MISS_CHAR) THEN -- No value for p_ret_val
2251       IF (p_return_status = OKL_API.G_RET_STS_SUCCESS) THEN -- ret stat success
2252         IF   (p_tmt_flag = 'TMT_EVERGREEN_YN') THEN
2253           px_tcnv_rec.TMT_EVERGREEN_YN := G_YES;
2254         ELSIF(p_tmt_flag = 'TMT_CLOSE_BALANCES_YN') THEN
2255           px_tcnv_rec.TMT_CLOSE_BALANCES_YN := G_YES;
2256         ELSIF(p_tmt_flag = 'TMT_ACCOUNTING_ENTRIES_YN') THEN
2257           px_tcnv_rec.TMT_ACCOUNTING_ENTRIES_YN := G_YES;
2258         ELSIF(p_tmt_flag = 'TMT_CANCEL_INSURANCE_YN') THEN
2259           px_tcnv_rec.TMT_CANCEL_INSURANCE_YN := G_YES;
2260         ELSIF(p_tmt_flag = 'TMT_ASSET_DISPOSITION_YN') THEN
2261           px_tcnv_rec.TMT_ASSET_DISPOSITION_YN := G_YES;
2262         ELSIF(p_tmt_flag = 'TMT_AMORTIZATION_YN') THEN
2263           px_tcnv_rec.TMT_AMORTIZATION_YN := G_YES;
2264         ELSIF(p_tmt_flag = 'TMT_ASSET_RETURN_YN') THEN
2265           px_tcnv_rec.TMT_ASSET_RETURN_YN := G_YES;
2266         ELSIF(p_tmt_flag = 'TMT_CONTRACT_UPDATED_YN') THEN
2267           px_tcnv_rec.TMT_CONTRACT_UPDATED_YN := G_YES;
2268         ELSIF(p_tmt_flag = 'TMT_STREAMS_UPDATED_YN') THEN
2269           px_tcnv_rec.TMT_STREAMS_UPDATED_YN := G_YES;
2270         ELSIF(p_tmt_flag = 'TMT_VALIDATED_YN') THEN
2271           px_tcnv_rec.TMT_VALIDATED_YN := G_YES;
2272         --END IF;
2273 
2274         -- RMUNJULU BUYOUT_PROCESS Added
2275         ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG1_YN') THEN
2276           px_tcnv_rec.TMT_GENERIC_FLAG1_YN := G_YES;
2277         ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG2_YN') THEN
2278           px_tcnv_rec.TMT_GENERIC_FLAG2_YN := G_YES;
2279         ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG3_YN') THEN
2280           px_tcnv_rec.TMT_GENERIC_FLAG3_YN := G_YES;
2281         END IF;
2282       ELSE -- return_status not success
2283         IF   (p_tmt_flag = 'TMT_EVERGREEN_YN') THEN
2284           px_tcnv_rec.TMT_EVERGREEN_YN := G_NO;
2285         ELSIF(p_tmt_flag = 'TMT_CLOSE_BALANCES_YN') THEN
2286           px_tcnv_rec.TMT_CLOSE_BALANCES_YN := G_NO;
2287         ELSIF(p_tmt_flag = 'TMT_ACCOUNTING_ENTRIES_YN') THEN
2288           px_tcnv_rec.TMT_ACCOUNTING_ENTRIES_YN := G_NO;
2289         ELSIF(p_tmt_flag = 'TMT_CANCEL_INSURANCE_YN') THEN
2290           px_tcnv_rec.TMT_CANCEL_INSURANCE_YN := G_NO;
2291         ELSIF(p_tmt_flag = 'TMT_ASSET_DISPOSITION_YN') THEN
2292           px_tcnv_rec.TMT_ASSET_DISPOSITION_YN := G_NO;
2293         ELSIF(p_tmt_flag = 'TMT_AMORTIZATION_YN') THEN
2294           px_tcnv_rec.TMT_AMORTIZATION_YN := G_NO;
2295         ELSIF(p_tmt_flag = 'TMT_ASSET_RETURN_YN') THEN
2296           px_tcnv_rec.TMT_ASSET_RETURN_YN := G_NO;
2297         ELSIF(p_tmt_flag = 'TMT_CONTRACT_UPDATED_YN') THEN
2298           px_tcnv_rec.TMT_CONTRACT_UPDATED_YN := G_NO;
2299         ELSIF(p_tmt_flag = 'TMT_STREAMS_UPDATED_YN') THEN
2300           px_tcnv_rec.TMT_STREAMS_UPDATED_YN := G_NO;
2301         ELSIF(p_tmt_flag = 'TMT_VALIDATED_YN') THEN
2302           px_tcnv_rec.TMT_VALIDATED_YN := G_NO;
2303         --END IF;
2304 
2305         -- RMUNJULU BUYOUT_PROCESS Added
2306         ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG1_YN') THEN
2307           px_tcnv_rec.TMT_GENERIC_FLAG1_YN := G_NO;
2308         ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG2_YN') THEN
2309           px_tcnv_rec.TMT_GENERIC_FLAG2_YN := G_NO;
2310         ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG3_YN') THEN
2311           px_tcnv_rec.TMT_GENERIC_FLAG3_YN := G_NO;
2312         END IF;
2313 
2314       END IF;
2315     ELSE -- value for p_ret_val passed ( will override return_status val)
2316       IF   (p_tmt_flag = 'TMT_EVERGREEN_YN') THEN
2317         px_tcnv_rec.TMT_EVERGREEN_YN := p_ret_val;
2318       ELSIF(p_tmt_flag = 'TMT_CLOSE_BALANCES_YN') THEN
2319         px_tcnv_rec.TMT_CLOSE_BALANCES_YN := p_ret_val;
2320       ELSIF(p_tmt_flag = 'TMT_ACCOUNTING_ENTRIES_YN') THEN
2321         px_tcnv_rec.TMT_ACCOUNTING_ENTRIES_YN := p_ret_val;
2322       ELSIF(p_tmt_flag = 'TMT_CANCEL_INSURANCE_YN') THEN
2323         px_tcnv_rec.TMT_CANCEL_INSURANCE_YN := p_ret_val;
2324       ELSIF(p_tmt_flag = 'TMT_ASSET_DISPOSITION_YN') THEN
2325         px_tcnv_rec.TMT_ASSET_DISPOSITION_YN := p_ret_val;
2326       ELSIF(p_tmt_flag = 'TMT_AMORTIZATION_YN') THEN
2327         px_tcnv_rec.TMT_AMORTIZATION_YN := p_ret_val;
2328       ELSIF(p_tmt_flag = 'TMT_ASSET_RETURN_YN') THEN
2329         px_tcnv_rec.TMT_ASSET_RETURN_YN := p_ret_val;
2330       ELSIF(p_tmt_flag = 'TMT_CONTRACT_UPDATED_YN') THEN
2331         px_tcnv_rec.TMT_CONTRACT_UPDATED_YN := p_ret_val;
2332       ELSIF(p_tmt_flag = 'TMT_STREAMS_UPDATED_YN') THEN
2333         px_tcnv_rec.TMT_STREAMS_UPDATED_YN := p_ret_val;
2334       ELSIF(p_tmt_flag = 'TMT_VALIDATED_YN') THEN
2335         px_tcnv_rec.TMT_VALIDATED_YN := p_ret_val;
2336       --END IF;
2337 
2338       -- RMUNJULU BUYOUT_PROCESS Added
2339       ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG1_YN') THEN
2340         px_tcnv_rec.TMT_GENERIC_FLAG1_YN := p_ret_val;
2341       ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG2_YN') THEN
2342         px_tcnv_rec.TMT_GENERIC_FLAG2_YN := p_ret_val;
2343       ELSIF(p_tmt_flag = 'TMT_GENERIC_FLAG3_YN') THEN
2344         px_tcnv_rec.TMT_GENERIC_FLAG3_YN := p_ret_val;
2345       END IF;
2346 
2347     END IF;
2348     IF (is_debug_procedure_on) THEN
2349        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
2350     END IF;
2351   EXCEPTION
2352     WHEN OTHERS THEN
2353         IF (is_debug_exception_on) THEN
2354            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
2355         END IF;
2356 
2357      OKL_API.set_message(p_app_name      => OKC_API.G_APP_NAME,
2358                          p_msg_name      => g_unexpected_error,
2359                          p_token1        => g_sqlcode_token,
2360                          p_token1_value  => SQLCODE,
2361                          p_token2        => g_sqlerrm_token,
2362                          p_token2_value  => SQLERRM);
2363   END set_transaction_rec;
2364 
2365   -- Start of comments
2366   --
2367   -- Procedure Name : validate_lease
2368   -- Description : Validates the lease (Contract) -- Add additional validations
2369   --                  if needed, most of the validations covered in validate_contract
2370   --                  of OKL_AM_LEASE_LOAN_TRMNT_PVT api
2371   --                : RMUNJULU 3061751 SERVICE K INTEGRATION CODE
2372   -- Business Rules :
2373   -- Parameters  :
2374   -- Version  : 1.0
2375   -- History        : RNUMJULU 3485854 Changed condition to NOT check for recycle but for Non quote
2376   --                : RMUNJULU LOANS_ENNHANCEMENTS Check for accruals using new api
2377   --                : SECHAWLA 23-JAN-06 4970009 : variable rate processing fixes
2378   --
2379   -- End of comments
2380   PROCEDURE validate_lease(
2381            p_api_version                 IN  NUMBER,
2382            p_init_msg_list               IN  VARCHAR2,
2383            x_return_status               OUT NOCOPY VARCHAR2,
2384            x_msg_count                   OUT NOCOPY NUMBER,
2385            x_msg_data                    OUT NOCOPY VARCHAR2,
2386            p_sys_date                    IN  DATE,
2387            p_term_rec                    IN  term_rec_type)  IS
2388 
2389    l_return_status          VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2390    l_api_name               VARCHAR2(30) := 'validate_lease';
2391 
2392     -- RMUNJULU 3061751 Added variables for SERVICE_K_INTEGRATION
2393     l_recycle_yn VARCHAR2(1) := 'N';
2394     l_billing_done VARCHAR2(1);
2395 
2396     -- Get the nonprocessed termination transaction for the contract
2397     CURSOR get_trn_csr (p_khr_id IN NUMBER) IS
2398     SELECT TRN.tmt_recycle_yn
2399     FROM   OKL_TRX_CONTRACTS TRN
2400     WHERE  TRN.khr_id = p_khr_id
2401     AND    TRN.tmt_status_code  NOT IN ('PROCESSED', 'CANCELED') --akrangan changes for sla tmt_status_code cr
2402     AND    TRN.tcn_type in ( 'TMT','EVG') --akrangan bug 5354501 fix added 'EVG'
2403     AND    TRN.representation_type = 'PRIMARY'; --rkuttiya added for 12.1.1
2404     -- Multi GAAP project
2405 
2406 
2407     -- rmunjulu LOANS_ENNHANCEMENTS
2408     l_accrual_done VARCHAR2(3);
2409     l_int_calc_done VARCHAR2(3);
2410 
2411     /* 20-JAN-06 SECHAWLA 4970009 : not required
2412     -- gboomina Bug 4755490 - Added - Start
2413     -- Get the last interim interest date to check whether
2414     -- Variable Rate processing concurrent program run previously or not
2415     CURSOR get_last_intrm_intrst_dt_csr (p_khr_id IN NUMBER) IS
2416     SELECT date_last_interim_interest_cal
2417     FROM okl_k_headers
2418     WHERE id = p_khr_id;
2419 
2420     -- Cursor to get Interest Calculation Method of a Contract
2421     CURSOR get_interest_calc_method_csr(p_khr_id IN NUMBER) IS
2422     SELECT  QVE.VALUE value
2423     FROM    OKL_PDT_QUALITYS PQY,
2424          OKL_PDT_PQY_VALS PQV,
2425          OKL_PQY_VALUES QVE
2426     WHERE   PQV.PDT_ID IN (SELECT pdt_id FROM OKL_K_HEADERS WHERE id = p_khr_id)
2427     AND     PQV.QVE_ID = QVE.ID
2428     AND     QVE.PQY_ID = PQY.ID
2429     AND     PQY.NAME ='INTEREST_CALCULATION_BASIS';
2430 
2431     l_interest_calc_method OKL_PQY_VALUES.VALUE%TYPE;
2432     l_last_intrm_intrst_dt DATE;
2433     -- gboomina Bug 4755490 - Added - End
2434 */-- 20-JAN-06 SECHAWLA 4970009 : not required
2435 
2436 -- 20-JAN-06 SECHAWLA 4970009 : added
2437     CURSOR l_okcheaders_b(cp_khr_id IN NUMBER) IS
2438  SELECT end_date
2439  FROM   okc_k_headers_b
2440  WHERE  id = cp_khr_id;
2441 
2442  l_end_date DATE;
2443  -- asawanka added for debug feature start
2444     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'validate_lease';
2445     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
2446     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
2447     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
2448     -- asawanka added for debug feature end
2449   BEGIN
2450      IF (is_debug_procedure_on) THEN
2451        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
2452      END IF;
2453 
2454     x_return_status := OKL_API.G_RET_STS_SUCCESS;
2455 
2456     l_return_status := OKL_API.START_ACTIVITY(l_api_name,
2457                                               p_init_msg_list,
2458                                               '_PVT',
2459                                               x_return_status);
2460 
2461     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2462       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2463     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
2464       RAISE OKL_API.G_EXCEPTION_ERROR;
2465     END IF;
2466 
2467     -- RMUNJULU 3018641 Step Message
2468     -- Step : Validate Contract
2469     OKL_API.set_message(
2470                         p_app_name      => G_APP_NAME,
2471                         p_msg_name      => 'OKL_AM_STEP_VAL');
2472 
2473     -- ADD ADDITIONAL VALIDATIONS HERE
2474 
2475     -- ++++++++++++++++++++  service contract integration begin ++++++++++++
2476 
2477     -- RMUNJULU 3061751 19-SEP-2003
2478     IF (is_debug_statement_on) THEN
2479        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
2480        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_control_flag = '||p_term_rec.p_control_flag);
2481        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_quote_id = '||p_term_rec.p_quote_id);
2482     END IF;
2483     -- Get the TRN details
2484     FOR get_trn_rec IN get_trn_csr(p_term_rec.p_contract_id) LOOP
2485 
2486         l_recycle_yn := NVL(get_trn_rec.tmt_recycle_yn,'N');
2487 
2488     END LOOP;
2489 
2490     -- If concurrent request and not from quote
2491 --  -- RMUNJULU 3485854 Changed condition to check for request not from quote
2492     IF p_term_rec.p_control_flag LIKE 'BATCH%'
2493 --    AND l_recycle_yn = 'N' THEN
2494     AND (p_term_rec.p_quote_id IS NULL OR p_term_rec.p_quote_id = OKL_API.G_MISS_NUM) THEN
2495 
2496 -- 20-JAN-06 SECHAWLA 4970009 : not required since OKL_AM_LEASE_LOAN_TRMNT_PVT.check_int_calc_done has
2497     -- been modified to check for lease contracts ('FLOAT_FACTORS','REAMORT')
2498 /* -- gboomina Bug 4755490 - Start
2499         FOR get_interest_calc_method_rec IN get_interest_calc_method_csr(p_term_rec.p_contract_id) LOOP
2500           l_interest_calc_method := get_interest_calc_method_rec.value;
2501         END LOOP;
2502         -- Only check for a Float Factor contract, Float Factor Adjustment streams exist
2503         IF l_interest_calc_method IN ('FLOAT_FACTORS','REAMORT') THEN -- SECHAWLA 09-JAN-05 4920618 : added REAMORT
2504           FOR get_last_intrm_intrst_dt_rec IN get_last_intrm_intrst_dt_csr(p_term_rec.p_contract_id) LOOP
2505             l_last_intrm_intrst_dt := get_last_intrm_intrst_dt_rec.date_last_interim_interest_cal;
2506           END LOOP;
2507           -- Check whether Float Factor Streams exist or not
2508           IF l_last_intrm_intrst_dt IS NULL THEN
2509             OKL_API.set_message( p_app_name      => G_APP_NAME,
2510                                  p_msg_name      => 'OKL_VAR_RATE_NOT_COMPLETED',
2511                                  p_token1        => 'CONTRACT_NUMBER',
2512                                  p_token1_value  => p_term_rec.p_contract_number);
2513 
2514             RAISE OKL_API.G_EXCEPTION_ERROR;
2515           END IF;
2516         END IF;
2517         -- gboomina Bug 4755490 - End
2518 */ -- 20-JAN-06 SECHAWLA 4970009 : not required  since OKL_AM_LEASE_LOAN_TRMNT_PVT.check_int_calc_done has
2519     -- been modified to check for lease contracts ('FLOAT_FACTORS','REAMORT')
2520 
2521         -- 23-JAN-06 SECHAWLA 4970009 : begin
2522         OPEN  l_okcheaders_b(p_term_rec.p_contract_id);
2523   FETCH l_okcheaders_b INTO l_end_date;
2524   CLOSE l_okcheaders_b;
2525         -- 23-JAN-06 SECHAWLA 4970009 : end
2526 
2527         -- 20-JAN-06 SECHAWLA 4970009 : added begin
2528         IF (is_debug_statement_on) THEN
2529                OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling  OKL_AM_LEASE_LOAN_TRMNT_PVT.check_int_calc_done');
2530         END IF;
2531         l_int_calc_done :=  OKL_AM_LEASE_LOAN_TRMNT_PVT.check_int_calc_done(
2532                                    p_contract_id      => p_term_rec.p_contract_id,
2533                                    p_contract_number  => p_term_rec.p_contract_number,
2534                                   -- p_quote_number     => db_quote_number,
2535                                    p_source           =>  'TERMINATE',
2536                                    --p_trn_date         => p_sys_date); -- 23-JAN-06 SECHAWLA 4970009
2537                                    p_trn_date         => l_end_date); -- 23-JAN-06 SECHAWLA 4970009
2538 
2539         IF (is_debug_statement_on) THEN
2540                OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_int_calc_done = '||l_int_calc_done);
2541         END IF;
2542 
2543         IF l_int_calc_done IS NULL OR l_int_calc_done = 'N' THEN
2544 
2545             -- Message will be set in called procedure
2546             l_return_status :=  OKL_API.G_RET_STS_ERROR;
2547         END IF;
2548         -- 20-JAN-06 SECHAWLA 4970009 : added end
2549         IF (is_debug_statement_on) THEN
2550                OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling  OKL_AM_LEASE_LOAN_TRMNT_PVT.check_billing_done');
2551         END IF;
2552         -- BPD Now provides a API which tells till when the billing was done, use that
2553         l_billing_done :=  OKL_AM_LEASE_LOAN_TRMNT_PVT.check_billing_done(
2554                                    p_contract_id      => p_term_rec.p_contract_id,
2555                                    p_contract_number  => p_term_rec.p_contract_number,
2556                                    p_trn_date         => p_sys_date );
2557         IF (is_debug_statement_on) THEN
2558                OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_billing_done = '||l_billing_done);
2559         END IF;
2560 
2561         IF l_billing_done IS NULL OR l_billing_done = 'N' THEN
2562 
2563             -- Message will be set in called procedure
2564             l_return_status :=  OKL_API.G_RET_STS_ERROR;
2565         END IF;
2566         IF (is_debug_statement_on) THEN
2567                OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling  OKL_GENERATE_ACCRUALS_PVT.check_date_accrued_till');
2568         END IF;
2569         -- rmunjulu LOANS_ENHANCEMENTS -- Check for accrual using new API
2570         l_accrual_done := OKL_GENERATE_ACCRUALS_PVT.check_date_accrued_till(
2571                                      p_khr_id => p_term_rec.p_contract_id,
2572                                      p_date   => p_sys_date);
2573         IF (is_debug_statement_on) THEN
2574                OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_accrual_done = '||l_accrual_done);
2575         END IF;
2576 
2577         -- if accrual not done then error
2578         IF (nvl(l_accrual_done,'N') = 'N' ) THEN
2579 
2580            -- Contract CONTRACT_NUMBER can not be terminated.
2581      -- Please run accruals for the contract till the termination date TERMINATION_DATE.
2582            OKL_API.set_message (
2583                    p_app_name     => OKL_API.G_APP_NAME,
2584                     p_msg_name     => 'OKL_AM_TERMNT_ACC_CHK',
2585                            p_token1       => 'CONTRACT_NUMBER',
2586                            p_token1_value => p_term_rec.p_contract_number,
2587                            p_token2       => 'TERMINATION_DATE',
2588                            p_token2_value => p_sys_date);
2589 
2590            RAISE OKL_API.G_EXCEPTION_ERROR;
2591 
2592         END IF;
2593     END IF;
2594 
2595     -- ++++++++++++++++++++  service contract integration end   ++++++++++++
2596 
2597     x_return_status   :=  l_return_status;
2598     OKL_API.END_ACTIVITY(x_msg_count, x_msg_data);
2599     IF (is_debug_procedure_on) THEN
2600        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
2601     END IF;
2602 
2603   EXCEPTION
2604     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2605         IF (is_debug_exception_on) THEN
2606              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
2607         END IF;
2608 
2609       -- 24-JAN-06 SECHAWLA 4970009 : close the open cursors
2610       IF l_okcheaders_b%ISOPEN THEN
2611          CLOSE l_okcheaders_b;
2612       END IF;
2613 
2614       IF get_trn_csr%ISOPEN THEN
2615          CLOSE get_trn_csr;
2616       END IF;
2617       x_return_status := OKL_API.HANDLE_EXCEPTIONS
2618       (
2619         l_api_name,
2620         G_PKG_NAME,
2621         'OKL_API.G_RET_STS_ERROR',
2622         x_msg_count,
2623         x_msg_data,
2624         '_PVT'
2625       );
2626     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2627         IF (is_debug_exception_on) THEN
2628              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
2629         END IF;
2630 
2631     -- 24-JAN-06 SECHAWLA 4970009 : close the open cursors
2632       IF l_okcheaders_b%ISOPEN THEN
2633          CLOSE l_okcheaders_b;
2634       END IF;
2635 
2636       IF get_trn_csr%ISOPEN THEN
2637          CLOSE get_trn_csr;
2638       END IF;
2639 
2640       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
2641       (
2642         l_api_name,
2643         G_PKG_NAME,
2644         'OKL_API.G_RET_STS_UNEXP_ERROR',
2645         x_msg_count,
2646         x_msg_data,
2647         '_PVT'
2648       );
2649     WHEN OTHERS THEN
2650         IF (is_debug_exception_on) THEN
2651            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
2652         END IF;
2653 
2654      -- 24-JAN-06 SECHAWLA 4970009 : close the open cursors
2655       IF l_okcheaders_b%ISOPEN THEN
2656          CLOSE l_okcheaders_b;
2657       END IF;
2658 
2659       IF get_trn_csr%ISOPEN THEN
2660          CLOSE get_trn_csr;
2661       END IF;
2662 
2663       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
2664       (
2665         l_api_name,
2666         G_PKG_NAME,
2667         'OTHERS',
2668         x_msg_count,
2669         x_msg_data,
2670         '_PVT'
2671       );
2672    END validate_lease;
2673 
2674    --Start of comments
2675    --
2676    -- Procedure Name  : update_quote_status
2677    -- Desciption     : UPDATE TERMINATION QUOTES FROM STATUS ACCEPTED TO
2678    -- Business Rules  :
2679    -- Parameters       :
2680    -- Version      : 1.0
2681    -- History        : RBRUNO BUG 6801022
2682    --
2683    -- End of comments
2684 
2685 PROCEDURE update_quote_status(p_term_rec IN term_rec_type) IS
2686 
2687     lp_qtev_rec                 OKL_TRX_QUOTES_PUB.qtev_rec_type;
2688     lx_qtev_rec                 OKL_TRX_QUOTES_PUB.qtev_rec_type;
2689 
2690     l_return_status             VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2691 
2692     l_quote_status              VARCHAR2(200) := 'COMPLETE';--'OKL_QUOTE_STATUS'
2693 
2694     lx_msg_count                NUMBER;
2695     lx_msg_data                 VARCHAR2(2000);
2696 
2697     l_qst_code                  varchar2(200);
2698 
2699     l_tmt_status_code           VARCHAR2(200);
2700 
2701     lx_return_status             VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2702     lx_quotes_found              VARCHAR2(1) := 'N';
2703     l_api_version               NUMBER := 1;
2704       l_module_name VARCHAR2(500) := G_MODULE_NAME || 'UPDATE_QUOTE_STATUS';
2705     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name,
2706 G_LEVEL_EXCEPTION);
2707     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name,
2708 G_LEVEL_PROCEDURE);
2709     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name,
2710 G_LEVEL_STATEMENT);
2711     --:= okl_debug_pub.check_log_on (l_module_name, g_level_exception);
2712 
2713 
2714     -- Fetch tmt_status_code
2715 
2716          CURSOR c_tmt_status_code_csr (p_qte_id IN NUMBER) IS
2717          SELECT tmt_status_code
2718           FROM okl_trx_contracts trx
2719           WHERE trx.qte_id = p_qte_id
2720          --rkuttiya added for 12.1.1 Multi GAAP
2721           AND trx.representation_type = 'PRIMARY';
2722 
2723     --- Fetch quote satus
2724 
2725          CURSOR k_quotes_csr (p_qte_id IN NUMBER) IS
2726          SELECT qst_code
2727           FROM okl_trx_quotes_v
2728           WHERE id = p_qte_id
2729           AND (qtp_code LIKE 'TER%' OR qtp_code LIKE 'RES%');
2730 
2731 BEGIN
2732 
2733   IF (is_debug_procedure_on) THEN
2734        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
2735   END IF;
2736 
2737 --Get termination quote status
2738 
2739 OPEN k_quotes_csr(p_term_rec.p_quote_id);
2740 FETCH k_quotes_csr into l_qst_code;
2741 CLOSE k_quotes_csr;
2742 
2743      IF (is_debug_statement_on) THEN
2744          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_qst_code =
2745 '||l_qst_code);
2746          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_term_rec.p_quote_id
2747 = '||p_term_rec.p_quote_id);
2748      END IF;
2749 
2750 IF p_term_rec.p_quote_id is not null and l_qst_code = 'ACCEPTED' THEN
2751 
2752  IF (is_debug_statement_on) THEN
2753          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'Quote in
2754 status accepted exists');
2755          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'p_term_rec.p_quote_id
2756 = '||p_term_rec.p_quote_id);
2757   END IF;
2758 
2759       OPEN  c_tmt_status_code_csr(p_term_rec.p_quote_id);
2760       FETCH c_tmt_status_code_csr INTO l_tmt_status_code;
2761       CLOSE c_tmt_status_code_csr;
2762 
2763       IF (is_debug_statement_on) THEN
2764          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'l_tmt_status_code
2765 = '||l_tmt_status_code);
2766       END IF;
2767 
2768       IF l_tmt_status_code = 'ERROR' THEN
2769           lp_qtev_rec.id        :=     p_term_rec.p_quote_id;
2770           lp_qtev_rec.qst_code   :=    l_quote_status;
2771 
2772    IF (is_debug_statement_on) THEN
2773          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'lp_qtev_rec.id
2774 = '||lp_qtev_rec.id);
2775             OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'lp_qtev_rec.qst_code
2776 = '||lp_qtev_rec.qst_code );
2777       END IF;
2778 
2779       -- Call the update of the quote header api
2780       OKL_TRX_QUOTES_PUB.update_trx_quotes (
2781            p_api_version                  => l_api_version,
2782            p_init_msg_list                => OKL_API.G_FALSE,
2783            x_return_status                => l_return_status,
2784            x_msg_count                    => lx_msg_count,
2785            x_msg_data                     => lx_msg_data,
2786            p_qtev_rec                     => lp_qtev_rec,
2787            x_qtev_rec                     => lx_qtev_rec);
2788 
2789       IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
2790 
2791 
2792        IF (is_debug_statement_on)
2793       THEN
2794        okl_debug_pub.log_debug (g_level_statement,
2795                                   l_module_name,
2796                                      'failure while updating the quote status
2797 ');
2798 
2799        END IF;
2800 
2801       END IF;
2802 
2803     END IF;
2804 
2805   END IF;
2806 
2807 END update_quote_status;
2808 
2809   -- Start of comments
2810   --
2811   -- Procedure Name : update_k_hdr_and_lines
2812   -- Description : set the termination record and call the tapis
2813   -- Business Rules :
2814   -- Parameters  :
2815   -- Version  : 1.0
2816   -- History        : RMUNJULU 18-FEB-03 2804703 changed to NOT set date_term
2817   --                  if setting to EVERGREEN
2818   --                : RMUNJULU 20-FEB-03 2757368 Changed k_lines_csr cursor to get
2819   --                  non TERMINATED/EXPIRED lines
2820   --                : RMUNJULU Bug # 3023206 27-JUN-03 Added call to
2821   --                  process_close_streams when TERMINATED/EXPIRED NOT when EVERGREEN
2822   --                : rmunjulu EDAT added code to set date_terminated as quote accpt date
2823   --
2824   -- End of comments
2825   PROCEDURE update_k_hdr_and_lines(
2826            p_api_version                 IN  NUMBER,
2827            p_init_msg_list               IN  VARCHAR2,
2828            x_return_status               OUT NOCOPY VARCHAR2,
2829            x_msg_count                   OUT NOCOPY NUMBER,
2830            x_msg_data                    OUT NOCOPY VARCHAR2,
2831            p_status                      IN  VARCHAR2,
2832            p_term_rec                    IN term_rec_type,
2833            p_klev_tbl                    IN klev_tbl_type,
2834            p_trn_reason_code             IN VARCHAR2,
2835            px_overall_status             IN OUT NOCOPY VARCHAR2,
2836            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
2837            x_chrv_rec                    OUT NOCOPY chrv_rec_type,
2838            x_clev_tbl                    OUT NOCOPY clev_tbl_type,
2839            p_sys_date                    IN DATE) IS
2840 
2841    -- Cursor to get the ste code
2842    CURSOR  get_old_ste_code_csr(p_sts_code VARCHAR2) IS
2843      SELECT STE_CODE
2844     FROM   OKC_STATUSES_V
2845     WHERE  CODE = p_sts_code;
2846 
2847    -- Cursor to get contract details
2848    CURSOR  k_header_csr IS
2849    SELECT  id,
2850            object_version_number,
2851          sts_code,
2852          authoring_org_id  --CDUBEY authoring_org_id added for MOAC
2853     FROM   OKC_K_HEADERS_B
2854     WHERE  id = p_term_rec.p_contract_id;
2855 
2856    -- Cursor to get all lines for the contract
2857    -- Cannot use the klev_tbl as these are financial lines only
2858    -- RMUNJULU 20-FEB-03 2757368 Changed code to get only active lines
2859    CURSOR  k_lines_csr (p_khr_id IN NUMBER) IS
2860    SELECT  KLE.id             kle_id,
2861            KLE.line_number    line_number
2862    FROM    OKC_K_LINES_B       KLE,
2863            OKC_K_HEADERS_B     KHR
2864    WHERE   KLE.dnz_chr_id   = p_khr_id
2865    AND     KLE.sts_code     = KHR.sts_code
2866    AND     KLE.dnz_chr_id   = KHR.id;
2867 
2868    -- Cursor to get the meaning of the sts_code passed
2869    CURSOR  k_sts_code_meaning_csr( p_sts_code IN VARCHAR2) IS
2870    SELECT  meaning
2871    FROM    OKC_STATUSES_V
2872    WHERE   code = p_sts_code;
2873 
2874 
2875    rec_k_header         k_header_csr%ROWTYPE;
2876    k_lines_rec          k_lines_csr%ROWTYPE;
2877    l_return_status      VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2878    --l_status_code        VARCHAR2(30);
2879    i                    NUMBER;
2880    l_scs_code           OKC_SUBCLASSES_V.code%TYPE;
2881    l_ste_code           OKC_STATUSES_V.CODE%TYPE;
2882    lp_chrv_rec          chrv_rec_type;
2883    lx_chrv_rec          chrv_rec_type;
2884    lp_clev_tbl          clev_tbl_type;
2885    lx_clev_tbl          clev_tbl_type;
2886    l_asset_name         VARCHAR2(200);
2887    l_sts_meaning        VARCHAR2(200);
2888    l_api_name           VARCHAR2(30) := 'update_k_hdr_and_lines';
2889 
2890    lx_stmv_tbl   stmv_tbl_type;
2891 
2892    -- rmunjulu EDAT
2893    l_quote_accpt_date DATE;
2894    l_quote_eff_date DATE;
2895    -- asawanka added for debug feature start
2896     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'update_k_hdr_and_lines';
2897     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
2898     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
2899     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
2900     -- asawanka added for debug feature end
2901   BEGIN
2902      IF (is_debug_procedure_on) THEN
2903        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
2904      END IF;
2905 
2906     -- Initialize savepoint to rollback to if error in this block
2907     SAVEPOINT update_k_hdr_lines;
2908 
2909     x_return_status := OKL_API.G_RET_STS_SUCCESS;
2910 
2911     IF (is_debug_statement_on) THEN
2912       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
2913     END IF;
2914     OPEN  k_header_csr;
2915     FETCH k_header_csr INTO rec_k_header;
2916     CLOSE k_header_csr;
2917     IF (is_debug_statement_on) THEN
2918       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'rec_k_header.sts_code = '||rec_k_header.sts_code);
2919     END IF;
2920     OPEN  get_old_ste_code_csr(rec_k_header.sts_code);
2921     FETCH get_old_ste_code_csr INTO l_ste_code;
2922     CLOSE get_old_ste_code_csr;
2923 
2924     OPEN  k_sts_code_meaning_csr(p_status);
2925     FETCH k_sts_code_meaning_csr INTO l_sts_meaning;
2926     CLOSE k_sts_code_meaning_csr;
2927 
2928     -- RMUNJULU 3018641 Step Message
2929     -- Step : Update Contract
2930     OKL_API.set_message(
2931                         p_app_name      => G_APP_NAME,
2932                         p_msg_name      => 'OKL_AM_STEP_UPD');
2933 
2934 
2935 
2936 
2937  -- RBRUNO BUG 6801022  START : UPDATE TERMINATION QUOTES FROM STATUS ACCEPTED
2938 
2939       IF (is_debug_statement_on)     THEN
2940          okl_debug_pub.log_debug (g_level_statement,
2941                                   l_module_name,
2942                                   'Invoking updating quote status');
2943                                   end if;
2944 
2945       --IF TERM QUOTE IN STATUS ACCEPTED EXISTS, UPDATE IT TO COMPLETE
2946       update_quote_status(p_term_rec);
2947 
2948       IF (is_debug_statement_on)   THEN
2949          okl_debug_pub.log_debug (g_level_statement,
2950                                   l_module_name,
2951                                   'post updating quote status');
2952                                   end if;
2953       -- RBRUNO BUG 6801022 END : UPDATE TERMINATION QUOTES FROM STATUS ACCEPTED
2954 
2955     -- RMUNJULU Bug # 3023206 Added call to Process_close_streams here
2956     -- Check if final termination ( DO NOT close streams if EVERGREEN)
2957     IF (is_debug_statement_on) THEN
2958       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_status = '||p_status);
2959     END IF;
2960 
2961     IF p_status IN ( 'TERMINATED','EXPIRED') THEN
2962 
2963      IF (is_debug_statement_on) THEN
2964       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  process_close_streams');
2965      END IF;
2966       process_close_streams(
2967             p_api_version       => p_api_version,
2968             p_init_msg_list     => OKL_API.G_FALSE,
2969             x_return_status      => l_return_status,
2970             x_msg_count         => x_msg_count,
2971             x_msg_data          => x_msg_data,
2972             p_term_rec           => p_term_rec,
2973             px_overall_status    => px_overall_status,
2974             px_tcnv_rec          => px_tcnv_rec,
2975             x_stmv_tbl           => lx_stmv_tbl,
2976             p_sys_date           => p_sys_date,
2977             p_trn_already_set    => G_NO); -- Always NO since this step was not done earlier
2978       IF (is_debug_statement_on) THEN
2979         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  process_close_streams l_return_status = '||l_return_status);
2980       END IF;
2981 
2982       -- Raise exception to rollback to savepoint
2983       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2984         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2985       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
2986         RAISE OKL_API.G_EXCEPTION_ERROR;
2987       END IF;
2988 
2989     END IF;
2990 
2991     -- RMUNJULU 20-FEB-03 2757368 flipped the order to update LINES and then CONTRACT
2992 
2993     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
2994 
2995     -- rmunjulu EDAT
2996     -- If quote exists then cancelation date is quote eff from date else sysdate
2997     IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
2998 
2999         l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
3000         l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
3001 
3002     ELSE
3003 
3004         l_quote_accpt_date := p_sys_date;
3005         l_quote_eff_date := p_sys_date;
3006 
3007     END IF;
3008 
3009     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
3010 
3011     -- Initialize i
3012     i := 1;
3013     -- set the line tbl
3014 
3015     -- RMUNJULU 20-FEB-03 2757368 Changed the cursor to send khr_id as IN param
3016     FOR k_lines_rec IN k_lines_csr (p_term_rec.p_contract_id) LOOP
3017 
3018        -- RMUNJULU 18-FEB-03 2804703 Added IF to check if being terminated
3019        IF p_status IN ('TERMINATED','EXPIRED') THEN
3020 
3021           -- Set the date terminated
3022           lp_clev_tbl(i).date_terminated  :=  l_quote_eff_date; -- rmunjulu EDAT
3023 
3024        END IF;
3025 
3026        -- set the k line tbl values
3027        lp_clev_tbl(i).id        :=  k_lines_rec.kle_id;
3028        lp_clev_tbl(i).sts_code  :=  p_status;
3029 
3030        i := i + 1;
3031 
3032     END LOOP;
3033 
3034 
3035      IF (is_debug_statement_on) THEN
3036       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  OKC_CONTRACT_PUB.update_contract_line');
3037      END IF;
3038 
3039     -- call the update line tbl
3040     OKC_CONTRACT_PUB.update_contract_line(
3041             p_api_version   => p_api_version,
3042           p_init_msg_list  => OKL_API.G_FALSE,
3043              x_return_status  => l_return_status,
3044              x_msg_count      => x_msg_count,
3045              x_msg_data       => x_msg_data,
3046              p_clev_tbl       => lp_clev_tbl,
3047              x_clev_tbl       => lx_clev_tbl);
3048     IF (is_debug_statement_on) THEN
3049       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  OKC_CONTRACT_PUB.update_contract_linel_return_status ='||l_return_status);
3050      END IF;
3051     IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
3052 
3053       -- Error updating assets of contract CONTRACT_NUMBER to status STATUS.
3054       OKL_API.set_message(
3055                          p_app_name      => G_APP_NAME,
3056                          p_msg_name      => 'OKL_AM_K_LINE_STATUS_UPD_ERR',
3057                          p_token1        => 'CONTRACT_NUMBER',
3058                          p_token1_value  => p_term_rec.p_contract_number,
3059                          p_token2        => 'STATUS',
3060                          p_token2_value  => l_sts_meaning);
3061 
3062     END IF;
3063 
3064     -- Raise exception to rollback to savepoint
3065     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3066       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3067     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
3068       RAISE OKL_API.G_EXCEPTION_ERROR;
3069     END IF;
3070 
3071     -- set the "in" record
3072     -- set the termination date
3073     -- RMUNJULU 18-FEB-03 2804703 Added IF to check if being terminated
3074     IF p_status IN ('TERMINATED','EXPIRED') THEN
3075        IF  (p_term_rec.p_termination_date IS NOT NULL)
3076        AND (p_term_rec.p_termination_date <> OKL_API.G_MISS_DATE) THEN
3077           lp_chrv_rec.date_terminated         := p_term_rec.p_termination_date;
3078        ELSE
3079           lp_chrv_rec.date_terminated         := l_quote_eff_date; -- rmunjulu EDAT
3080        END IF;
3081 
3082        lp_chrv_rec.trn_code                  := p_trn_reason_code;
3083 
3084     END IF;
3085     -- fix for bug 6945800 -start
3086     IF (p_status  = 'EXPIRED') THEN
3087              lp_chrv_rec.date_terminated         := null;
3088     END IF;
3089     -- fix for bug 6945800 -end
3090     lp_chrv_rec.id                        := rec_k_header.id;
3091     lp_chrv_rec.object_version_number     := rec_k_header.object_version_number;
3092     lp_chrv_rec.sts_code                  := p_status;
3093     lp_chrv_rec.old_sts_code              := rec_k_header.sts_code;
3094     lp_chrv_rec.new_sts_code              := p_status;
3095     lp_chrv_rec.old_ste_code              := l_ste_code;
3096     lp_chrv_rec.new_ste_code              := p_status;
3097     lp_chrv_rec.org_id                    := rec_k_header.authoring_org_id; --CDUBEY added for MOAC
3098 
3099     IF (is_debug_statement_on) THEN
3100       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  OKC_CONTRACT_PUB.update_contract_header');
3101     END IF;
3102     -- Call the tapi to update contract with termination info
3103     OKC_CONTRACT_PUB.update_contract_header(
3104                      p_api_version   => p_api_version,
3105                         p_init_msg_list   => OKL_API.G_FALSE,
3106                         x_return_status   => l_return_status,
3107                         x_msg_count       => x_msg_count,
3108                         x_msg_data        => x_msg_data,
3109                         p_restricted_update  => OKL_API.G_TRUE,
3110                         p_chrv_rec    => lp_chrv_rec,
3111                         x_chrv_rec    => lx_chrv_rec);
3112     IF (is_debug_statement_on) THEN
3113       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  OKC_CONTRACT_PUB.update_contract_header l_return_status = '||l_return_status);
3114     END IF;
3115     IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
3116 
3117       -- Error updating contract CONTRACT_NUMBER to status STATUS.
3118       OKL_API.set_message(
3119                          p_app_name      => G_APP_NAME,
3120                          p_msg_name      => 'OKL_AM_K_STATUS_UPD_ERR',
3121                          p_token1        => 'CONTRACT_NUMBER',
3122                          p_token1_value  => p_term_rec.p_contract_number,
3123                          p_token2        => 'STATUS',
3124                          p_token2_value  => l_sts_meaning);
3125 
3126     END IF;
3127 
3128     -- Raise exception to rollback to savepoint
3129     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3130       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3131     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
3132       RAISE OKL_API.G_EXCEPTION_ERROR;
3133     END IF;
3134 
3135 
3136     -- Set the success messages
3137 
3138     -- Contract line statuses updated to STATUS for assets of contract CONTRACT_NUMBER.
3139     OKL_API.set_message(
3140                            p_app_name      => G_APP_NAME,
3141                            p_msg_name      => 'OKL_AM_K_LINE_STATUS_UPD',
3142                            p_token1        => 'STATUS',
3143                            p_token1_value  => l_sts_meaning,
3144                            p_token2        => 'CONTRACT_NUMBER',
3145                            p_token2_value  => p_term_rec.p_contract_number);
3146 
3147 
3148     -- Contract CONTRACT_NUMBER status updated to STATUS.
3149     OKL_API.set_message( p_app_name      => G_APP_NAME,
3150                          p_msg_name      => 'OKL_AM_K_STATUS_UPD',
3151                          p_token1        => 'CONTRACT_NUMBER',
3152                          p_token1_value  => p_term_rec.p_contract_number,
3153                          p_token2        => 'STATUS',
3154                          p_token2_value  => l_sts_meaning);
3155 
3156     -- store the highest degree of error
3157     set_overall_status(
3158         p_return_status                 => l_return_status,
3159         px_overall_status               => px_overall_status);
3160 
3161     -- set the transaction record
3162     set_transaction_rec(
3163         p_return_status                 => l_return_status,
3164         p_overall_status                => px_overall_status,
3165         p_tmt_flag                      => 'TMT_CONTRACT_UPDATED_YN',
3166         p_tsu_code                      => 'PROCESSED',
3167         px_tcnv_rec                     => px_tcnv_rec);
3168 
3169     -- set the out params
3170     x_return_status   :=   l_return_status;
3171     x_chrv_rec        :=   lx_chrv_rec;
3172     x_clev_tbl        :=   lx_clev_tbl;
3173     IF (is_debug_procedure_on) THEN
3174        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
3175     END IF;
3176   EXCEPTION
3177     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3178         IF (is_debug_exception_on) THEN
3179              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
3180         END IF;
3181 
3182       IF get_old_ste_code_csr%ISOPEN THEN
3183          CLOSE get_old_ste_code_csr;
3184       END IF;
3185       IF k_header_csr%ISOPEN THEN
3186          CLOSE k_header_csr;
3187       END IF;
3188       IF k_sts_code_meaning_csr%ISOPEN THEN
3189          CLOSE k_sts_code_meaning_csr;
3190       END IF;
3191 
3192       ROLLBACK TO update_k_hdr_lines;
3193 
3194       x_return_status := OKL_API.G_RET_STS_ERROR;
3195 
3196       -- store the highest degree of error
3197       set_overall_status(
3198         p_return_status                 => x_return_status,
3199         px_overall_status               => px_overall_status);
3200 
3201       -- set the transaction record
3202       set_transaction_rec(
3203         p_return_status                 => x_return_status,
3204         p_overall_status                => px_overall_status,
3205         p_tmt_flag                      => 'TMT_CONTRACT_UPDATED_YN',
3206         p_tsu_code                      => 'ERROR',
3207         px_tcnv_rec                     => px_tcnv_rec);
3208 
3209     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3210         IF (is_debug_exception_on) THEN
3211              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
3212         END IF;
3213 
3214       IF get_old_ste_code_csr%ISOPEN THEN
3215          CLOSE get_old_ste_code_csr;
3216       END IF;
3217       IF k_header_csr%ISOPEN THEN
3218          CLOSE k_header_csr;
3219       END IF;
3220       IF k_sts_code_meaning_csr%ISOPEN THEN
3221          CLOSE k_sts_code_meaning_csr;
3222       END IF;
3223 
3224       ROLLBACK TO update_k_hdr_lines;
3225 
3226       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
3227 
3228       -- store the highest degree of error
3229       set_overall_status(
3230         p_return_status                 => x_return_status,
3231         px_overall_status               => px_overall_status);
3232 
3233       -- set the transaction record
3234       set_transaction_rec(
3235         p_return_status                 => x_return_status,
3236         p_overall_status                => px_overall_status,
3237         p_tmt_flag                      => 'TMT_CONTRACT_UPDATED_YN',
3238         p_tsu_code                      => 'ERROR',
3239         px_tcnv_rec                     => px_tcnv_rec);
3240 
3241     WHEN OTHERS THEN
3242         IF (is_debug_exception_on) THEN
3243            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
3244         END IF;
3245 
3246       IF get_old_ste_code_csr%ISOPEN THEN
3247          CLOSE get_old_ste_code_csr;
3248       END IF;
3249       IF k_header_csr%ISOPEN THEN
3250          CLOSE k_header_csr;
3251       END IF;
3252       IF k_sts_code_meaning_csr%ISOPEN THEN
3253          CLOSE k_sts_code_meaning_csr;
3254       END IF;
3255 
3256       ROLLBACK TO update_k_hdr_lines;
3257 
3258       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
3259 
3260       -- store the highest degree of error
3261       set_overall_status(
3262         p_return_status                 => x_return_status,
3263         px_overall_status               => px_overall_status);
3264 
3265       -- set the transaction record
3266       set_transaction_rec(
3267         p_return_status                 => x_return_status,
3268         p_overall_status                => px_overall_status,
3269         p_tmt_flag                      => 'TMT_CONTRACT_UPDATED_YN',
3270         p_tsu_code                      => 'ERROR',
3271         px_tcnv_rec                     => px_tcnv_rec);
3272 
3273        -- Set the oracle error message
3274        OKL_API.set_message(
3275                          p_app_name      => OKC_API.G_APP_NAME,
3276                          p_msg_name      => g_unexpected_error,
3277                          p_token1        => g_sqlcode_token,
3278                          p_token1_value  => SQLCODE,
3279                          p_token2        => g_sqlerrm_token,
3280                          p_token2_value  => SQLERRM);
3281 
3282   END update_k_hdr_and_lines;
3283 
3284   -- Start of comments
3285   --
3286   -- Procedure Name : process_transaction
3287   -- Description : Calls the Transaction TAPI based on whether in
3288   --                CREATE or UPDATE mode
3289   -- Business Rules :
3290   -- Parameters  :
3291   -- Version  : 1.0
3292   --
3293   -- End of comments
3294   PROCEDURE process_transaction(
3295            p_api_version                 IN  NUMBER,
3296            p_init_msg_list               IN  VARCHAR2,
3297            x_return_status               OUT NOCOPY VARCHAR2,
3298            x_msg_count                   OUT NOCOPY NUMBER,
3299            x_msg_data                    OUT NOCOPY VARCHAR2,
3300            p_id                          IN NUMBER,
3301            p_term_rec                    IN term_rec_type,
3302            p_tcnv_rec                    IN tcnv_rec_type,
3303            x_id                          OUT NOCOPY NUMBER,
3304            p_trn_mode                    IN VARCHAR2)  IS
3305 
3306    l_return_status      VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3307    lp_tcnv_rec          tcnv_rec_type := p_tcnv_rec;
3308    lx_tcnv_rec          tcnv_rec_type;
3309    l_api_name           VARCHAR2(30) := 'process_transaction';
3310 
3311    -- asawanka added for debug feature start
3312     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_transaction';
3313     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
3314     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
3315     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
3316     -- asawanka added for debug feature end
3317   BEGIN
3318      IF (is_debug_procedure_on) THEN
3319        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
3320      END IF;
3321 
3322      x_return_status := OKL_API.G_RET_STS_SUCCESS;
3323 
3324      l_return_status := OKL_API.START_ACTIVITY(l_api_name,
3325                                                p_init_msg_list,
3326                                                '_PVT',
3327                                                x_return_status);
3328 
3329      IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3330        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3331      ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
3332        RAISE OKL_API.G_EXCEPTION_ERROR;
3333      END IF;
3334 
3335      -- Clear the recycle flag after processing
3336      lp_tcnv_rec.tmt_recycle_yn := NULL;
3337      IF (is_debug_statement_on) THEN
3338       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_trn_mode = '||p_trn_mode);
3339      END IF;
3340      IF p_trn_mode = 'INSERT' THEN
3341        IF (is_debug_statement_on) THEN
3342          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  OKL_TRX_CONTRACTS_PUB.create_trx_contracts');
3343        END IF;
3344        -- insert transaction rec
3345        OKL_TRX_CONTRACTS_PUB.create_trx_contracts(
3346         p_api_version                => p_api_version,
3347         p_init_msg_list               => OKL_API.G_FALSE,
3348          x_return_status                => l_return_status,
3349          x_msg_count                    => x_msg_count,
3350          x_msg_data                     => x_msg_data,
3351          p_tcnv_rec                      => lp_tcnv_rec,
3352          x_tcnv_rec                      => lx_tcnv_rec);
3353        IF (is_debug_statement_on) THEN
3354          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  OKL_TRX_CONTRACTS_PUB.insert_trx_contracts l_return_status = '||l_return_status);
3355        END IF;
3356      ELSIF p_trn_mode = 'UPDATE' THEN
3357        IF (is_debug_statement_on) THEN
3358          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  OKL_TRX_CONTRACTS_PUB.update_trx_contracts');
3359        END IF;
3360 
3361        -- update transaction rec
3362        OKL_TRX_CONTRACTS_PUB.update_trx_contracts(
3363         p_api_version                => p_api_version,
3364         p_init_msg_list               => OKL_API.G_FALSE,
3365          x_return_status                => l_return_status,
3366          x_msg_count                    => x_msg_count,
3367          x_msg_data                     => x_msg_data,
3368          p_tcnv_rec                      => lp_tcnv_rec,
3369          x_tcnv_rec                      => lx_tcnv_rec);
3370        IF (is_debug_statement_on) THEN
3371          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  OKL_TRX_CONTRACTS_PUB.update_trx_contracts l_return_status = '||l_return_status);
3372        END IF;
3373      END IF;
3374 
3375      -- rollback if error
3376      IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3377        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3378      ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
3379        RAISE OKL_API.G_EXCEPTION_ERROR;
3380      END IF;
3381 
3382      -- set the return values
3383      x_return_status   :=  l_return_status;
3384      x_id              :=  lx_tcnv_rec.id;
3385 
3386     OKL_API.END_ACTIVITY(x_msg_count, x_msg_data);
3387     IF (is_debug_procedure_on) THEN
3388        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
3389     END IF;
3390   EXCEPTION
3391     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3392         IF (is_debug_exception_on) THEN
3393              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
3394         END IF;
3395 
3396       x_return_status := OKL_API.HANDLE_EXCEPTIONS
3397       (
3398         l_api_name,
3399         G_PKG_NAME,
3400         'OKL_API.G_RET_STS_ERROR',
3401         x_msg_count,
3402         x_msg_data,
3403         '_PVT'
3404       );
3405     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3406         IF (is_debug_exception_on) THEN
3407              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
3408         END IF;
3409 
3410       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
3411       (
3412         l_api_name,
3413         G_PKG_NAME,
3414         'OKL_API.G_RET_STS_UNEXP_ERROR',
3415         x_msg_count,
3416         x_msg_data,
3417         '_PVT'
3418       );
3419     WHEN OTHERS THEN
3420         IF (is_debug_exception_on) THEN
3421            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
3422         END IF;
3423 
3424       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
3425       (
3426         l_api_name,
3427         G_PKG_NAME,
3428         'OTHERS',
3429         x_msg_count,
3430         x_msg_data,
3431         '_PVT'
3432       );
3433   END process_transaction;
3434 
3435 
3436 /*  -- This procedure is commented temporarily
3437   -- Start of comments
3438   --
3439   -- Procedure Name : process_evergreen_deprn
3440   -- Desciption     : This procedure is used to start depreciation of the assets
3441   --            under a particular contract, after the contract status changes
3442   --            to "Evergreen". Evergreen Contracts are Direct Finace/Sales type
3443   --            of contracts with no hold periods.
3444   -- Business Rules :
3445   -- Parameters  :
3446   -- Version  : 1.0
3447   --
3448   -- End of comments
3449   PROCEDURE process_evergreen_deprn(
3450            p_api_version                 IN  NUMBER,
3451            p_init_msg_list               IN  VARCHAR2,
3452            x_return_status               OUT NOCOPY VARCHAR2,
3453            x_msg_count                   OUT NOCOPY NUMBER,
3454            x_msg_data                    OUT NOCOPY VARCHAR2,
3455            p_term_rec                    IN term_rec_type,
3456            p_sys_date                    IN DATE)  IS
3457    --This cursor will return all the Fixed Asset Lines for a particular contract
3458    CURSOR l_linesv_csr IS
3459    SELECT depreciation_category,
3460           corporate_book,
3461           salvage_value,
3462           deprn_method_code,
3463           life_in_months,
3464           parent_line_id,
3465           asset_number,
3466           item_description,
3467           asset_id,
3468           original_cost,
3469           current_units,
3470           in_service_date
3471    FROM   OKX_ASSET_LINES_V
3472    WHERE  dnz_chr_id = p_term_rec.p_contract_id;
3473 
3474    --This cursor returns the original cost from the original asset creation line
3475    CURSOR l_txlassetsv_csr(p_asset_number okl_txl_assets_v.asset_number%TYPE) IS
3476    SELECT original_cost
3477    FROM   okl_txl_assets_v
3478    WHERE  tal_type = 'CFA'
3479    AND    asset_number = p_asset_number
3480    AND    ROWNUM < 2;
3481 
3482    -- This cursor is used to get the cost,
3483    -- residual value of an asset from the Financial Asset (TOP LINE)
3484    CURSOR  l_linesfullv_csr(p_id  NUMBER) IS
3485    SELECT  oec, residual_value
3486    FROM    okl_k_lines_full_v
3487    WHERE   id = p_id;
3488 
3489    -- This cursor will return deal_type for a particular contract
3490    CURSOR l_lhrfv_csr IS
3491    SELECT deal_type
3492    FROM   OKL_K_HEADERS_FULL_V
3493    WHERE  id = p_term_rec.p_contract_id;
3494 
3495    l_return_status              VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3496    l_api_name                   VARCHAR2(30) := 'process_evergreen_deprn';
3497    lp_thpv_rec                  OKL_TRX_ASSETS_PUB.thpv_rec_type;
3498    lx_thpv_rec                  OKL_TRX_ASSETS_PUB.thpv_rec_type;
3499    lp_tlpv_rec               tlpv_rec_type;
3500    lx_tlpv_rec               tlpv_rec_type;
3501    l_try_id                     NUMBER;
3502    l_method_id                  FA_METHODS.method_id%TYPE;
3503    l_corporate_book             VARCHAR2(70);
3504    l_df_original_cost           NUMBER;
3505    l_oec                        NUMBER;
3506    l_residual_value             NUMBER;
3507    l_tax_owner                  VARCHAR2(10);
3508    l_rulv_rec                   okl_rule_pub.rulv_rec_type;
3509    l_line_status                VARCHAR2(15);
3510    l_deal_type                  VARCHAR2(15);
3511    l_api_version                CONSTANT NUMBER := 1;
3512   BEGIN
3513 
3514     l_return_status :=  OKL_API.START_ACTIVITY(l_api_name,
3515                                                  G_PKG_NAME,
3516                                                  p_init_msg_list,
3517                                                  l_api_version,
3518                                                  p_api_version,
3519                                                  '_PVT',
3520                                                  x_return_status);
3521     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3522       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3523     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
3524       RAISE OKL_API.G_EXCEPTION_ERROR;
3525     END IF;
3526 
3527     IF (p_term_rec.p_contract_id IS NULL)
3528     OR (p_term_rec.p_contract_id = OKL_API.G_MISS_NUM) THEN
3529          x_return_status := OKL_API.G_RET_STS_ERROR;
3530          ---- contract id parameter is null
3531          OKL_API.set_message(        p_app_name      => 'OKC',
3532                                      p_msg_name      => G_REQUIRED_VALUE,
3533                                      p_token1        => G_COL_NAME_TOKEN,
3534                                      p_token1_value  => 'CONTRACT_ID');
3535           RAISE OKL_API.G_EXCEPTION_ERROR;
3536     END IF;
3537 
3538     OPEN  l_lhrfv_csr;
3539     FETCH l_lhrfv_csr INTO l_deal_type;
3540     IF l_lhrfv_csr%NOTFOUND THEN
3541          x_return_status := OKL_API.G_RET_STS_ERROR;
3542          -- Contract ID is invalid
3543          OKL_API.set_message(        p_app_name      => 'OKC',
3544                                      p_msg_name      => G_INVALID_VALUE,
3545                                      p_token1        => G_COL_NAME_TOKEN,
3546                                      p_token1_value  => 'CONTRACT_ID');
3547          RAISE OKL_API.G_EXCEPTION_ERROR;
3548       END IF;
3549     CLOSE l_lhrfv_csr;
3550 
3551     IF l_deal_type IS NULL THEN
3552         x_return_status := OKL_API.G_RET_STS_ERROR;
3553        --deal type not defined for this contract
3554         OKL_API.set_message(       p_app_name      => 'OKL',
3555                                      p_msg_name      => 'OKL_AM_NO_DEAL_TYPE');
3556         RAISE OKL_API.G_EXCEPTION_ERROR;
3557     END IF;
3558 
3559     IF l_deal_type='LEASEDF' THEN
3560 
3561 
3562          -- get the tax owner (LESSOR/LESSEE) for the asset.
3563          -- This will help us in future if FA gives us the ability
3564          -- to adjust tax books directly. Tax books need to be adjusted in a
3565          -- different manner for different tax owners.
3566          -- Currently we do not have any control over tax books adjusments
3567 
3568          okl_am_util_pvt.get_rule_record(p_rgd_code         => 'LATOWN'
3569                                      ,p_rdf_code         =>'LATOWN'
3570                                      ,p_chr_id           => p_contract_id
3571                                      ,p_cle_id           => NULL
3572                                      ,x_rulv_rec         => l_rulv_rec
3573                                      ,x_return_status    => l_return_status
3574                                      ,x_msg_count        => x_msg_count
3575                                      ,x_msg_data         => x_msg_data);
3576 
3577          IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3578              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3579          ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
3580              RAISE OKL_API.G_EXCEPTION_ERROR;
3581          ELSIF (x_return_status = OKL_API.G_RET_STS_SUCCESS) THEN
3582           -- l_rulv_rec.RULE_INFORMATION1 will contain the value 'LESSEE' or
3583           -- 'LESSOR'
3584              l_tax_owner := l_rulv_rec.RULE_INFORMATION1;
3585          END IF;
3586 
3587       END IF;
3588 
3589 
3590 
3591     OKL_AM_UTIL_PVT.get_transaction_id('?',x_return_status,l_try_id);
3592        IF x_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
3593             OKL_API.set_message(p_app_name    => 'OKL',
3594                         p_msg_name            => 'OKL_AM_NO_TRX_TYPE_FOUND',
3595                         p_token1              => 'TRY_NAME',
3596                         p_token1_value        => '?');
3597             RAISE OKL_API.G_EXCEPTION_ERROR;
3598        END IF;
3599 
3600     -- scenario 8 - Direct Finance Lease expiration with NO hold period
3601     -- loop thru all the records from okx_asset_lines_v where
3602     -- contract id matches p_contract_id, validate the data
3603     -- and then create transaction header and line in okl_trx_assets_v and
3604     -- okl_txl_assets_v
3605     FOR l_lines_rec IN l_linesv_csr LOOP
3606        l_line_status := NULL;
3607        -- validate data before creating the transaction line.
3608        IF  l_lines_rec.parent_line_id IS NULL THEN
3609        --  Parent Line Id not defined for asset
3610             OKL_API.set_message(  p_app_name      => 'OKL',
3611                                   p_msg_name      => 'OKL_AM_NO_PARENT_LINE_ID',
3612                                   p_token1        =>  'ASSET_NUMBER',
3613                                   p_token1_value  =>  l_lines_rec.asset_number);
3614             l_line_status := 'ERROR';
3615         END IF;
3616         IF  l_lines_rec.life_in_months IS NULL THEN
3617             -- Life in Months not defined for asset
3618             OKL_API.set_message(  p_app_name      => 'OKL',
3619                                   p_msg_name      => 'OKL_AM_NO_LIFE_IN_MONTHS',
3620                                   p_token1        =>  'ASSET_NUMBER',
3621                                   p_token1_value  =>  l_lines_rec.asset_number);
3622              l_line_status := 'ERROR';
3623         END IF;
3624         OPEN  l_txlassetsv_csr(l_lines_rec.asset_number);
3625         FETCH l_txlassetsv_csr INTO l_df_original_cost;
3626         CLOSE l_txlassetsv_csr;
3627 
3628         OPEN  l_linesfullv_csr(l_lines_rec.parent_line_id);
3629         FETCH l_linesfullv_csr INTO l_oec, l_residual_value;
3630         CLOSE l_linesfullv_csr;
3631 
3632         IF l_residual_value IS NULL THEN
3633             -- Residual Value not defined for the asset
3634             OKL_API.set_message(p_app_name      => 'OKL',
3635                                 p_msg_name      => 'OKL_AM_NO_RESIDUAL_VALUE',
3636                                 p_token1        =>  'ASSET_NUMBER',
3637                                 p_token1_value  =>  l_lines_rec.asset_number);
3638             l_line_status := 'ERROR';
3639         END IF;
3640         IF l_df_original_cost IS NULL THEN
3641             IF l_oec IS NULL THEN
3642                -- OEC not defined for the asset
3643                OKL_API.set_message(p_app_name     => 'OKL',
3644                                    p_msg_name     => 'OKL_AM_NO_OEC',
3645                                    p_token1       =>  'ASSET_NUMBER',
3646                                    p_token1_value =>  l_lines_rec.asset_number);
3647                l_line_status := 'ERROR';
3648             END IF;
3649         END IF;
3650        -- end validation
3651 
3652        -- Create Transaction Header
3653        lp_thpv_rec.tas_type            := 'AED';
3654        IF l_line_status IS NULL THEN
3655           lp_thpv_rec.tsu_code            := 'ENTERED';
3656        ELSE
3657           lp_thpv_rec.tsu_code            := 'ERROR';
3658        END IF;
3659        lp_thpv_rec.try_id                 :=  l_try_id;
3660        lp_thpv_rec.date_trans_occurred    :=  p_sys_date;
3661 
3662        OKL_TRX_ASSETS_PUB.create_trx_ass_h_def(
3663                               p_api_version           => p_api_version,
3664                         p_init_msg_list         => p_init_msg_list,
3665                   x_return_status         => x_return_status,
3666                   x_msg_count             => x_msg_count,
3667                   x_msg_data              => x_msg_data,
3668             p_thpv_rec        => lp_thpv_rec,
3669             x_thpv_rec        => lx_thpv_rec);
3670        IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3671          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3672        ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
3673          RAISE OKL_API.G_EXCEPTION_ERROR;
3674        END IF;
3675 
3676        -- Create transaction Line
3677        lp_tlpv_rec.tas_id        := lx_thpv_rec.id;   -- FK
3678        lp_tlpv_rec.iay_id        := l_lines_rec.depreciation_category;
3679        lp_tlpv_rec.kle_id        := l_lines_rec.parent_line_id;
3680        lp_tlpv_rec.line_number       := 1;
3681        lp_tlpv_rec.tal_type       := 'AER';
3682     lp_tlpv_rec.asset_number   := l_lines_rec.asset_number;
3683        lp_tlpv_rec.description          := l_lines_rec.item_description;
3684        lp_tlpv_rec.life_in_months       := l_lines_rec.life_in_months;
3685        lp_tlpv_rec.deprn_method         := l_lines_rec.deprn_method_code;
3686        lp_tlpv_rec.corporate_book   := l_lines_rec.corporate_book;
3687        lp_tlpv_rec.depreciation_cost  := l_residual_value;
3688        lp_tlpv_rec.salvage_value   := l_lines_rec.salvage_value;
3689     IF l_df_original_cost IS NOT NULL THEN
3690              lp_tlpv_rec.original_cost  := l_df_original_cost;
3691        ELSE
3692              lp_tlpv_rec.original_cost  := l_oec;
3693        END IF;
3694     lp_tlpv_rec.current_units   := l_lines_rec.current_units;
3695     lp_tlpv_rec.depreciate_yn   := G_YES;
3696     lp_tlpv_rec.dnz_asset_id         := TO_NUMBER(l_lines_rec.asset_id);
3697        lp_tlpv_rec.dnz_khr_id       := p_term_rec.p_contract_id;
3698 
3699        -- In case of direct finance / Sales type of lease,
3700        -- in_service_date is defaulted to the transaction date.
3701        lp_tlpv_rec.in_service_date     := lp_thpv_rec.date_trans_occurred;
3702 
3703        OKL_TXL_ASSETS_PUB.create_txl_asset_def(
3704                                    p_api_version           => p_api_version,
3705                              p_init_msg_list         => p_init_msg_list,
3706                        x_return_status         => x_return_status,
3707                        x_msg_count             => x_msg_count,
3708                        x_msg_data              => x_msg_data,
3709                  p_tlpv_rec         => lp_tlpv_rec,
3710                  x_tlpv_rec         => lx_tlpv_rec);
3711        IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
3712          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
3713        ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
3714          RAISE OKL_API.G_EXCEPTION_ERROR;
3715        END IF;
3716     END LOOP;
3717     END IF; -- if deal_type='LEASEDF'
3718 
3719     OKL_API.END_ACTIVITY(x_msg_count, x_msg_data);
3720   EXCEPTION
3721     WHEN OKL_API.G_EXCEPTION_ERROR THEN
3722         IF l_linesv_csr%ISOPEN THEN
3723            CLOSE l_linesv_csr;
3724         END IF;
3725         IF l_lhrfv_csr%ISOPEN THEN
3726            CLOSE l_lhrfv_csr;
3727         END IF;
3728         IF l_txlassetsv_csr%ISOPEN THEN
3729            CLOSE l_txlassetsv_csr;
3730         END IF;
3731         IF l_linesfullv_csr%ISOPEN THEN
3732            CLOSE l_linesfullv_csr;
3733         END IF;
3734       x_return_status := OKL_API.HANDLE_EXCEPTIONS
3735       (
3736         l_api_name,
3737         G_PKG_NAME,
3738         'OKL_API.G_RET_STS_ERROR',
3739         x_msg_count,
3740         x_msg_data,
3741         '_PVT'
3742       );
3743     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
3744         IF l_linesv_csr%ISOPEN THEN
3745            CLOSE l_linesv_csr;
3746         END IF;
3747         IF l_lhrfv_csr%ISOPEN THEN
3748            CLOSE l_lhrfv_csr;
3749         END IF;
3750         IF l_txlassetsv_csr%ISOPEN THEN
3751            CLOSE l_txlassetsv_csr;
3752         END IF;
3753         IF l_linesfullv_csr%ISOPEN THEN
3754            CLOSE l_linesfullv_csr;
3755         END IF;
3756       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
3757       (
3758         l_api_name,
3759         G_PKG_NAME,
3760         'OKL_API.G_RET_STS_UNEXP_ERROR',
3761         x_msg_count,
3762         x_msg_data,
3763         '_PVT'
3764       );
3765     WHEN OTHERS THEN
3766         IF l_linesv_csr%ISOPEN THEN
3767            CLOSE l_linesv_csr;
3768         END IF;
3769         IF l_lhrfv_csr%ISOPEN THEN
3770            CLOSE l_lhrfv_csr;
3771         END IF;
3772         IF l_txlassetsv_csr%ISOPEN THEN
3773            CLOSE l_txlassetsv_csr;
3774         END IF;
3775         IF l_linesfullv_csr%ISOPEN THEN
3776            CLOSE l_linesfullv_csr;
3777         END IF;
3778       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
3779       (
3780         l_api_name,
3781         G_PKG_NAME,
3782         'OTHERS',
3783         x_msg_count,
3784         x_msg_data,
3785         '_PVT'
3786       );
3787   END process_evergreen_deprn;
3788 */
3789 
3790 
3791   -- Start of comments
3792   --
3793   -- Procedure Name : process_evergreen_contract
3794   -- Desciption     : Sets the evergreen flag and updates the K Header to
3795   --                  set it to evergreen, and calls evergreen depreciation
3796   -- Business Rules :
3797   -- Parameters  :
3798   -- Version  : 1.0
3799   -- History        : RMUNJULU 14-FEB-03 2804703 Added code to check if contract
3800   --                  not already evergreen
3801   --                : RMUNJULU 05-MAR-03 Added code to NOT check evergreen eligibility
3802   --                  if already checked and was not needed
3803   --                : RMUNJULU 04-MAR-04 3485854 Added code to get if K was evergreen earlier
3804   --                  Changed condition when evergreen processing needs to be done and
3805   --                  Added call to process_amortize after evergreen update
3806   --                : rmunjulu Bug 4141991 Modify Process Evergreen to
3807   --                   Call Process Accounting during conversion of contract  to evergreen
3808   --                : PAGARG 01-Mar-05 Bug 4190887 Pass klev_tbl to process_accounting_entries
3809   -- End of comments
3810   PROCEDURE process_evergreen_contract(
3811            p_api_version                 IN  NUMBER,
3812            p_init_msg_list               IN  VARCHAR2,
3813            x_return_status               OUT NOCOPY VARCHAR2,
3814            x_msg_count                   OUT NOCOPY NUMBER,
3815            x_msg_data                    OUT NOCOPY VARCHAR2,
3816            p_term_rec                    IN term_rec_type,
3817            p_sys_date                    IN DATE,
3818            p_trn_already_set             IN VARCHAR2,
3819            p_klev_tbl                    IN klev_tbl_type, -- pagarg 4190887 Added
3820            px_overall_status             IN OUT NOCOPY VARCHAR2,
3821            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
3822            x_evergreen_status            OUT NOCOPY VARCHAR2) IS
3823 
3824    -- Get the contract end date
3825    -- RMUNJULU 14-FEB-03 2804703 Added sts_code to cursor
3826    CURSOR l_k_end_csr ( p_khr_id IN NUMBER) IS
3827      SELECT khr.end_date,
3828             khr.sts_code
3829      FROM   OKC_K_HEADERS_V khr
3830      WHERE  khr.id = p_khr_id;
3831 
3832    l_return_status      VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3833    l_api_name           VARCHAR2(30) := 'process_evergreen_contract';
3834    l_evergreen_status   VARCHAR2(1) := OKL_API.G_FALSE;
3835    l_rule_found         VARCHAR2(1);
3836    lx_klev_tbl          klev_tbl_type;
3837    lx_chrv_rec          chrv_rec_type;
3838    lx_clev_tbl          clev_tbl_type;
3839    l_k_end_date         DATE;
3840 
3841    -- RMUNJULU 14-FEB-03 2804703 Added variable
3842    l_k_sts_code VARCHAR2(200);
3843 
3844    l_evergreen_earlier VARCHAR2(3) := 'N';
3845    -- asawanka added for debug feature start
3846     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_evergreen_contract';
3847     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
3848     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
3849     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
3850     -- asawanka added for debug feature end
3851   BEGIN
3852      IF (is_debug_procedure_on) THEN
3853        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
3854      END IF;
3855 
3856     -- Initialize savepoint to rollback to if error in this block
3857     SAVEPOINT evergreen;
3858 
3859     x_return_status := OKL_API.G_RET_STS_SUCCESS;
3860     IF (is_debug_statement_on) THEN
3861       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
3862       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'px_tcnv_rec.id = '||px_tcnv_rec.id);
3863     END IF;
3864     -- Get the contract end date
3865     OPEN l_k_end_csr ( p_term_rec.p_contract_id);
3866 
3867     -- RMUNJULU 14-FEB-03 2804703 Added code to get sts_code
3868     FETCH l_k_end_csr INTO l_k_end_date, l_k_sts_code;
3869     CLOSE l_k_end_csr;
3870 
3871     -- RMUNJULU 04-MAR-04 3485854
3872     -- CHECK TO see IF old evergreen transaction exists
3873     -- Check if another transaction exists which is processed and for which tmt_evergreen_yn was Y
3874     -- which means this contract was evergreen earlier
3875 
3876     -- so no need to set to evergreen again
3877     l_evergreen_earlier := check_k_evergreen_ear(
3878                                     p_khr_id          => p_term_rec.p_contract_id,
3879                                     p_tcn_id          => px_tcnv_rec.id,
3880                                     x_return_status   => l_return_status);
3881 
3882     -- Check for evergreen and process evergreen only when
3883     -- no trn exists and from batch process
3884     -- and contract has reached its end date
3885 
3886     -- RMUNJULU 14-FEB-03 2804703 Added condition that contract was not already evergreen
3887     -- Should not check for Evergreen if Contract already in EVERGREEN status
3888     -- RMUNJULU 05-MAR-03 Added condition to not check for evergreen only when
3889     -- not a recycled transaction and evergreen flag was not check to T
3890 /*
3891     IF  p_trn_already_set = G_NO
3892     AND p_term_rec.p_control_flag = 'BATCH_PROCESS'
3893     AND TRUNC(l_k_end_date) <= TRUNC(p_sys_date)
3894     AND l_k_sts_code <> 'EVERGREEN'
3895     AND NVL(G_TMT_RECYCLE_YN,'N') = 'N' THEN
3896 */
3897 
3898     -- RMUNJULU 04-MAR-04 3485854
3899     -- Revamped the condition when evergreen processing needs to be done
3900 
3901     -- Check contract was not earlier set for evergreen
3902     -- Check control from batch process
3903     -- Check k ended before termination date
3904     -- Check no quote triggered the termination process ( ie from batch at end of term)
3905     IF (is_debug_statement_on) THEN
3906       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_evergreen_earlier = '||l_evergreen_earlier);
3907       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_control_flag = '||p_term_rec.p_control_flag);
3908       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_quote_id = '||p_term_rec.p_quote_id);
3909     END IF;
3910     IF  (l_evergreen_earlier <> 'Y'
3911     AND p_term_rec.p_control_flag LIKE 'BATCH_PROCESS%'
3912     AND TRUNC(l_k_end_date) <= TRUNC(p_sys_date)
3913     AND (p_term_rec.p_quote_id IS NULL
3914      OR p_term_rec.p_quote_id = OKL_API.G_MISS_NUM))THEN
3915 
3916         -- RMUNJULU 3018641 Step Message
3917         -- Step : Evergreen
3918         OKL_API.set_message(
3919                         p_app_name      => G_APP_NAME,
3920                         p_msg_name      => 'OKL_AM_STEP_EVR');
3921 
3922         -- get evergreen eligiblility
3923         IF (is_debug_statement_on) THEN
3924               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling evergreen_eligibility');
3925         END IF;
3926         evergreen_eligibility(
3927            p_term_rec                       => p_term_rec,
3928            x_return_status                  => l_return_status,
3929            x_rule_found                     => l_rule_found,
3930            x_msg_count                      => x_msg_count,
3931            x_msg_data                       => x_msg_data);
3932 
3933         -- Raise exception to rollback to the savepoint
3934         IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
3935 
3936           -- Error getting evergreen rule for contract.
3937           OKL_API.set_message(
3938                              p_app_name     => G_APP_NAME,
3939                              p_msg_name     => 'OKL_AM_EVERGREEN_RULE_ERR');
3940           RAISE OKL_API.G_EXCEPTION_ERROR;
3941         END IF;
3942         IF (is_debug_statement_on) THEN
3943               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_rule_found = '||l_rule_found);
3944         END IF;
3945         -- if control_flag = "batch process" and evergreen_status = "ok" then
3946           -- update contract_status to evergreen
3947           -- exit (raise exception)
3948         -- end if
3949         IF  (l_rule_found = G_YES) THEN
3950          IF (is_debug_statement_on) THEN
3951               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling Okl_Am_Integration_Pvt.cancel_termination_quotes');
3952          END IF;
3953           -- rmunjulu EDAT cancel all quotes for the contract which are not accepted or completed
3954           Okl_Am_Integration_Pvt.cancel_termination_quotes  (
3955             p_api_version     => p_api_version,
3956                   p_init_msg_list   => OKL_API.G_FALSE,
3957                   p_khr_id          => p_term_rec.p_contract_id,
3958                   p_source_trx_id   => NULL ,
3959                   p_source          => 'EVERGREEN', -- rmunjulu bug 4556370 pass source to integration pvt
3960                   x_return_status   => l_return_status,
3961                   x_msg_count       => x_msg_count,
3962                   x_msg_data        => x_msg_data);
3963           IF (is_debug_statement_on) THEN
3964                 OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called Okl_Am_Integration_Pvt.cancel_termination_quotes l_return_status = '||l_return_status);
3965           END IF;
3966           -- Raise exception to rollback to the savepoint
3967           IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
3968             RAISE OKL_API.G_EXCEPTION_ERROR;
3969           END IF;
3970           IF (is_debug_statement_on) THEN
3971               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling update_k_hdr_and_lines');
3972          END IF;
3973           -- set_and_update_contract
3974           update_k_hdr_and_lines(
3975                   p_api_version       => p_api_version,
3976                   p_init_msg_list     => OKL_API.G_FALSE,
3977                   x_return_status     => l_return_status,
3978                   x_msg_count         => x_msg_count,
3979                   x_msg_data          => x_msg_data,
3980                   p_status           => 'EVERGREEN',
3981                   p_term_rec          => p_term_rec,
3982                   p_klev_tbl          => lx_klev_tbl,
3983                   p_trn_reason_code   => px_tcnv_rec.trn_code,
3984                   px_overall_status   => px_overall_status,
3985                   px_tcnv_rec         => px_tcnv_rec,
3986                   x_chrv_rec          => lx_chrv_rec,
3987                   x_clev_tbl          => lx_clev_tbl,
3988                   p_sys_date          => p_sys_date);
3989            IF (is_debug_statement_on) THEN
3990                 OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called update_k_hdr_and_lines l_return_status = '||l_return_status);
3991           END IF;
3992           -- Raise exception to rollback to the savepoint
3993           IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
3994             RAISE OKL_API.G_EXCEPTION_ERROR;
3995           END IF;
3996 
3997           -- This code is commented out till further notice
3998           -- start depreciation for evergreen contract
3999           /*
4000           process_evergreen_deprn(
4001              p_api_version                  => p_api_version,
4002              p_init_msg_list                => p_init_msg_list,
4003              x_return_status                => l_return_status,
4004              x_msg_count                    => x_msg_count,
4005              x_msg_data                     => x_msg_data,
4006              p_term_rec                     => p_term_rec,
4007              p_sys_date                     => p_sys_date);
4008 
4009           -- Raise exception to rollback to the savepoint
4010           IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4011             RAISE OKL_API.G_EXCEPTION_ERROR;
4012           END IF;
4013          */
4014           IF (is_debug_statement_on) THEN
4015               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling process_accounting_entries');
4016          END IF;
4017           -- rmunjulu 4141991 do accounting entries
4018           --pagarg 4190887 Pass klev_tbl to do accounting at line level
4019           process_accounting_entries(
4020                 p_api_version        => p_api_version,
4021                 p_init_msg_list      => OKL_API.G_FALSE,
4022                 x_return_status      => l_return_status,
4023                 x_msg_count          => x_msg_count,
4024                 x_msg_data           => x_msg_data,
4025                 p_term_rec            => p_term_rec,
4026                 px_overall_status     => px_overall_status,
4027                 px_tcnv_rec           => px_tcnv_rec,
4028                 p_sys_date            => p_sys_date,
4029                 p_klev_tbl            => p_klev_tbl, -- pagarg 4190887 Added
4030                 p_trn_already_set     => 'Y',
4031     p_source              => 'EVERGREEN'); -- rmunjulu Bug 4141991
4032           IF (is_debug_statement_on) THEN
4033                 OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called process_accounting_entries l_return_status = '||l_return_status);
4034           END IF;
4035           -- rmunjulu 4141991 Raise exception to rollback to the savepoint
4036           IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4037             RAISE OKL_API.G_EXCEPTION_ERROR;
4038           END IF;
4039 
4040          -- RMUNJULU 04-MAR-04 3485854 added call to process_amortize to do amortization
4041          -- Additional checks in process_amortize to cater to evergreen scenario
4042          IF (is_debug_statement_on) THEN
4043               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling process_amortize');
4044          END IF;
4045          process_amortize(
4046                 p_api_version       => p_api_version,
4047                 p_init_msg_list     => OKL_API.G_FALSE,
4048                 x_return_status     => l_return_status,
4049                 x_msg_count         => x_msg_count,
4050                 x_msg_data          => x_msg_data,
4051                 p_term_rec          => p_term_rec,
4052                 px_overall_status   => px_overall_status,
4053                 px_tcnv_rec         => px_tcnv_rec,
4054                 p_sys_date          => p_sys_date,
4055                 p_call_origin       => 'EVERGREEN');
4056          IF (is_debug_statement_on) THEN
4057                 OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called process_amortize l_return_status = '||l_return_status);
4058           END IF;
4059          -- Raise exception to rollback to the savepoint
4060          IF (px_overall_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4061            RAISE OKL_API.G_EXCEPTION_ERROR;
4062          END IF;
4063 
4064           -- set the transaction record
4065           set_transaction_rec(
4066               p_return_status               => l_return_status,
4067               p_overall_status              => px_overall_status,
4068               p_tmt_flag                    => 'TMT_EVERGREEN_YN',
4069               p_tsu_code                    => 'PROCESSED',
4070               px_tcnv_rec                   => px_tcnv_rec);
4071 
4072           -- Contract eligible for evergreen.
4073           OKL_API.set_message(
4074                              p_app_name     => G_APP_NAME,
4075                              p_msg_name     => 'OKL_AM_K_EVERGREEN');
4076 
4077           -- store the highest degree of error
4078           set_overall_status(
4079               p_return_status               => l_return_status,
4080               px_overall_status             => px_overall_status);
4081 
4082           l_evergreen_status := G_YES;
4083 
4084         ELSE -- contract not eligible for evergreen
4085 
4086           -- Contract not eligible for evergreen.
4087           OKL_API.set_message(
4088                              p_app_name     => G_APP_NAME,
4089                              p_msg_name     => 'OKL_AM_K_NOT_EVERGREEN');
4090 
4091           -- set the transaction record
4092           set_transaction_rec(
4093               p_return_status               => l_return_status,
4094               p_overall_status              => px_overall_status,
4095               p_tmt_flag                    => 'TMT_EVERGREEN_YN',
4096               p_tsu_code                    => 'WORKING',
4097               p_ret_val                     => NULL,
4098               px_tcnv_rec                   => px_tcnv_rec);
4099 
4100           -- store the highest degree of error
4101           set_overall_status(
4102               p_return_status               => l_return_status,
4103               px_overall_status             => px_overall_status);
4104 
4105 
4106           l_evergreen_status := G_NO;
4107 
4108         END IF;
4109         /* -- RMUNJULU 05-MAR-03 Removed ELSE
4110     ELSE
4111 
4112           -- Contract not eligible for evergreen.
4113           OKL_API.set_message(
4114                              p_app_name     => G_APP_NAME,
4115                              p_msg_name     => 'OKL_AM_K_NOT_EVERGREEN');
4116          */
4117     END IF;
4118 
4119     x_return_status      := l_return_status;
4120     x_evergreen_status   := l_evergreen_status;
4121     IF (is_debug_procedure_on) THEN
4122        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
4123     END IF;
4124   EXCEPTION
4125     WHEN OKL_API.G_EXCEPTION_ERROR THEN
4126         IF (is_debug_exception_on) THEN
4127              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
4128         END IF;
4129 
4130          IF l_k_end_csr%ISOPEN THEN
4131            CLOSE l_k_end_csr;
4132          END IF;
4133 
4134          ROLLBACK TO evergreen;
4135          x_return_status    := OKL_API.G_RET_STS_ERROR;
4136          x_evergreen_status := G_YES;
4137 
4138          -- store the highest degree of error
4139          set_overall_status(
4140                p_return_status                 => x_return_status,
4141                px_overall_status               => px_overall_status);
4142 
4143          -- set the transaction record
4144          set_transaction_rec(
4145                p_return_status                 => x_return_status,
4146                p_overall_status                => px_overall_status,
4147                p_tmt_flag                      => 'TMT_EVERGREEN_YN',
4148                p_tsu_code                      => 'ERROR',
4149                px_tcnv_rec                     => px_tcnv_rec);
4150 
4151     WHEN OTHERS THEN
4152         IF (is_debug_exception_on) THEN
4153            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
4154         END IF;
4155 
4156          IF l_k_end_csr%ISOPEN THEN
4157            CLOSE l_k_end_csr;
4158          END IF;
4159 
4160          ROLLBACK TO evergreen;
4161          x_return_status    := OKL_API.G_RET_STS_ERROR;
4162          x_evergreen_status := G_YES;
4163 
4164          -- store the highest degree of error
4165          set_overall_status(
4166                p_return_status                 => x_return_status,
4167                px_overall_status               => px_overall_status);
4168 
4169          -- set the transaction record
4170          set_transaction_rec(
4171                p_return_status                 => x_return_status,
4172                p_overall_status                => px_overall_status,
4173                p_tmt_flag                      => 'TMT_EVERGREEN_YN',
4174                p_tsu_code                      => 'ERROR',
4175                px_tcnv_rec                     => px_tcnv_rec);
4176 
4177          -- Set the oracle error message
4178          OKL_API.set_message(
4179                          p_app_name      => OKC_API.G_APP_NAME,
4180                          p_msg_name      => g_unexpected_error,
4181                          p_token1        => g_sqlcode_token,
4182                          p_token1_value  => SQLCODE,
4183                          p_token2        => g_sqlerrm_token,
4184                          p_token2_value  => SQLERRM);
4185 
4186   END process_evergreen_contract;
4187 
4188   -- Start of comments
4189   --
4190   -- Procedure Name : process_cancel_insurance
4191   -- Desciption     : Calls the cancel insurance api to cancel insurances
4192   -- Business Rules :
4193   -- Parameters  :
4194   -- History        : rmunjulu EDAT Added code to pass proper date for cancelation
4195   -- Version  : 1.0
4196   --
4197   -- End of comments
4198   PROCEDURE process_cancel_insurance(
4199            p_api_version                 IN  NUMBER,
4200            p_init_msg_list               IN  VARCHAR2,
4201            x_return_status               OUT NOCOPY VARCHAR2,
4202            x_msg_count                   OUT NOCOPY NUMBER,
4203            x_msg_data                    OUT NOCOPY VARCHAR2,
4204            p_term_rec                    IN term_rec_type,
4205            px_overall_status             IN OUT NOCOPY VARCHAR2,
4206            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
4207            p_sys_date                    IN DATE,
4208            p_trn_already_set             IN VARCHAR2)  IS
4209 
4210    -- Cursor to get the end date of contract -- Can get from main API
4211    CURSOR   k_end_date_csr ( p_chr_id IN NUMBER) IS
4212     SELECT  end_date
4213     FROM    OKC_K_HEADERS_B
4214     WHERE   id = p_chr_id;
4215 
4216    l_k_end_date            DATE;
4217    l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4218    l_early_termination_yn  VARCHAR2(1) := G_NO;
4219    i                       NUMBER;
4220 
4221    -- rmunjulu EDAT
4222    l_quote_accpt_date DATE;
4223    l_quote_eff_date DATE;
4224    -- asawanka added for debug feature start
4225     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_cancel_insurance';
4226     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
4227     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
4228     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
4229     -- asawanka added for debug feature end
4230   BEGIN
4231      IF (is_debug_procedure_on) THEN
4232        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
4233      END IF;
4234 
4235     -- Start savepoint to rollback to if error in this block
4236     SAVEPOINT cancel_insurance;
4237 
4238     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
4239 
4240     -- rmunjulu EDAT
4241     -- If quote exists then cancelation date is quote eff from date else sysdate
4242     IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
4243 
4244         l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
4245         l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
4246 
4247     ELSE
4248 
4249         l_quote_accpt_date := p_sys_date;
4250         l_quote_eff_date := p_sys_date;
4251 
4252     END IF;
4253 
4254     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
4255     IF (is_debug_statement_on) THEN
4256          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
4257          OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_early_termination_yn = '||p_term_rec.p_early_termination_yn);
4258     END IF;
4259     -- Get k end date
4260     OPEN k_end_date_csr(p_term_rec.p_contract_id);
4261     FETCH k_end_date_csr INTO l_k_end_date;
4262     CLOSE k_end_date_csr;
4263 
4264     -- check if early termination
4265     IF TRUNC (l_k_end_date) > TRUNC (l_quote_eff_date) THEN -- rmunjulu EDAT
4266        l_early_termination_yn := G_YES;
4267     END IF;
4268 
4269     -- if early termination then
4270     IF (NVL(p_term_rec.p_early_termination_yn, '?') = G_YES)
4271     OR (l_early_termination_yn = G_YES )THEN
4272        IF (p_trn_already_set = G_YES
4273            AND NVL(px_tcnv_rec.tmt_cancel_insurance_yn, '?') <> G_YES)
4274        OR (p_trn_already_set = G_NO) THEN
4275           -- cancel insurance
4276 
4277         -- RMUNJULU 3018641 Step Message
4278         -- Step : Cancel Insurance
4279         OKL_API.set_message(
4280                         p_app_name      => G_APP_NAME,
4281                         p_msg_name      => 'OKL_AM_STEP_INS');
4282 
4283           IF (is_debug_statement_on) THEN
4284                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling OKL_INSURANCE_POLICIES_PUB.cancel_policies');
4285                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_quote_eff_date = '||l_quote_eff_date);
4286           END IF;
4287 
4288           OKL_INSURANCE_POLICIES_PUB.cancel_policies(
4289              p_api_version                => p_api_version,
4290              p_init_msg_list              => OKL_API.G_FALSE,
4291              x_return_status              => l_return_status,
4292              x_msg_count                  => x_msg_count,
4293              x_msg_data                   => x_msg_data,
4294              p_contract_id                => p_term_rec.p_contract_id,
4295              p_cancellation_date          => l_quote_eff_date, -- rmunjulu EDAT -- pass quote effective date
4296              p_crx_code                   => 'CONTRACT_CANCELED'); -- rmunjulu EDAT -- need to pass new cancelation code
4297 
4298           IF (is_debug_statement_on) THEN
4299                  OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called OKL_INSURANCE_POLICIES_PUB.cancel_policies l_return_status = '||l_return_status);
4300           END IF;
4301 
4302           IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
4303             -- Error in cancelling Insurance.
4304             OKL_API.set_message( p_app_name      => G_APP_NAME,
4305                                  p_msg_name      => 'OKL_AM_ERR_CAN_INS');
4306           END IF;
4307 
4308           -- Raise exception to rollback to the savepoint
4309           IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4310             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4311           ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
4312             RAISE OKL_API.G_EXCEPTION_ERROR;
4313           END IF;
4314 
4315           -- store the highest degree of error
4316           set_overall_status(
4317             p_return_status                  => l_return_status,
4318             px_overall_status                => px_overall_status);
4319 
4320           -- set the transaction record
4321           set_transaction_rec(
4322             p_return_status                 => l_return_status,
4323             p_overall_status                => px_overall_status,
4324             p_tmt_flag                      => 'TMT_CANCEL_INSURANCE_YN',
4325             p_tsu_code                      => 'WORKING',
4326             px_tcnv_rec                     => px_tcnv_rec);
4327 
4328        END IF;
4329     ELSE --( no early termination then  set cancel insurance )
4330       -- Cancelling of insurance not needed because this
4331       -- is an end of term contract termination.
4332       OKL_API.set_message( p_app_name      => G_APP_NAME,
4333                            p_msg_name      => 'OKL_AM_CAN_INS_NOT_NEEDED');
4334 
4335       -- set the transaction record
4336       set_transaction_rec(
4337             p_return_status                 => l_return_status,
4338             p_overall_status                => px_overall_status,
4339             p_tmt_flag                      => 'TMT_CANCEL_INSURANCE_YN',
4340             p_tsu_code                      => 'WORKING',
4341             p_ret_val                       => NULL,
4342             px_tcnv_rec                     => px_tcnv_rec);
4343     END IF;
4344 
4345     x_return_status      := l_return_status;
4346     IF (is_debug_procedure_on) THEN
4347        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
4348     END IF;
4349   EXCEPTION
4350     WHEN OKL_API.G_EXCEPTION_ERROR THEN
4351         IF (is_debug_exception_on) THEN
4352              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
4353         END IF;
4354 
4355          IF k_end_date_csr%ISOPEN THEN
4356             CLOSE k_end_date_csr;
4357          END IF;
4358 
4359          ROLLBACK TO cancel_insurance;
4360 
4361          x_return_status := OKL_API.G_RET_STS_ERROR;
4362 
4363          -- store the highest degree of error
4364          set_overall_status(
4365                p_return_status                 => x_return_status,
4366                px_overall_status               => px_overall_status);
4367 
4368          -- set the transaction record
4369          set_transaction_rec(
4370                p_return_status                 => x_return_status,
4371                p_overall_status                => px_overall_status,
4372                p_tmt_flag                      => 'TMT_CANCEL_INSURANCE_YN',
4373                p_tsu_code                      => 'ERROR',
4374                px_tcnv_rec                     => px_tcnv_rec);
4375 
4376     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
4377         IF (is_debug_exception_on) THEN
4378              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
4379         END IF;
4380 
4381          IF k_end_date_csr%ISOPEN THEN
4382             CLOSE k_end_date_csr;
4383          END IF;
4384 
4385          ROLLBACK TO cancel_insurance;
4386 
4387          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
4388 
4389          -- store the highest degree of error
4390          set_overall_status(
4391                p_return_status                 => x_return_status,
4392                px_overall_status               => px_overall_status);
4393 
4394          -- set the transaction record
4395          set_transaction_rec(
4396                p_return_status                 => x_return_status,
4397                p_overall_status                => px_overall_status,
4398                p_tmt_flag                      => 'TMT_CANCEL_INSURANCE_YN',
4399                p_tsu_code                      => 'ERROR',
4400                px_tcnv_rec                     => px_tcnv_rec);
4401 
4402     WHEN OTHERS THEN
4403         IF (is_debug_exception_on) THEN
4404            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
4405         END IF;
4406 
4407          IF k_end_date_csr%ISOPEN THEN
4408             CLOSE k_end_date_csr;
4409          END IF;
4410 
4411          ROLLBACK TO cancel_insurance;
4412 
4413          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
4414 
4415          -- store the highest degree of error
4416          set_overall_status(
4417                p_return_status                 => x_return_status,
4418                px_overall_status               => px_overall_status);
4419 
4420          -- set the transaction record
4421          set_transaction_rec(
4422                p_return_status                 => x_return_status,
4423                p_overall_status                => px_overall_status,
4424                p_tmt_flag                      => 'TMT_CANCEL_INSURANCE_YN',
4425                p_tsu_code                      => 'ERROR',
4426                px_tcnv_rec                     => px_tcnv_rec);
4427 
4428          -- Set the oracle error message
4429          OKL_API.set_message(
4430                          p_app_name      => OKC_API.G_APP_NAME,
4431                          p_msg_name      => g_unexpected_error,
4432                          p_token1        => g_sqlcode_token,
4433                          p_token1_value  => SQLCODE,
4434                          p_token2        => g_sqlerrm_token,
4435                          p_token2_value  => SQLERRM);
4436 
4437   END process_cancel_insurance;
4438 
4439 
4440   -- Start of comments
4441   --
4442   -- Procedure Name : process_close_streams
4443   -- Desciption     : Calls the streams api to close all streams of contract
4444   -- Business Rules :
4445   -- Parameters  :
4446   -- Version  : 1.0
4447   -- History        : RMUNJULU 28-MAR-03 2877278 Changed the cursor and code
4448   --                  to get only CURRENT streams and HISTORIZE them
4449   --                : RMUNJULU Bug # 3023206 27-JUN-03 Change priority of checks
4450   --                : rmunjulu Bug 4058630 Do not check return status of PURPOSE check and do NVLs
4451   --
4452   -- End of comments
4453   PROCEDURE process_close_streams(
4454            p_api_version                 IN  NUMBER,
4455            p_init_msg_list               IN  VARCHAR2,
4456            x_return_status               OUT NOCOPY VARCHAR2,
4457            x_msg_count                   OUT NOCOPY NUMBER,
4458            x_msg_data                    OUT NOCOPY VARCHAR2,
4459            p_term_rec                    IN term_rec_type,
4460            px_overall_status             IN OUT NOCOPY VARCHAR2,
4461            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
4462            x_stmv_tbl                    OUT NOCOPY stmv_tbl_type,
4463            p_sys_date                    IN DATE,
4464            p_trn_already_set             IN VARCHAR2)  IS
4465 
4466    -- Cursor to get the streams of the contract
4467    -- RMUNJULU 28-MAR-03 2877278 Added conditions to pick only CURRENT
4468    -- streams.
4469    -- MDOKAL 19-SEP-03 Bug 3082639 Securitization
4470    -- Ensure we do not historize RESIDUAL VALUE and INVESTOR related streams
4471    -- SMODUGA 11-Oct-04 Bug 3925469
4472    -- Modified cursor by passing sty_id based on the purpose .
4473    -- Fix for Bug#13933234, sgn_code='INTC' needs to be histrized
4474    CURSOR   k_streams_csr ( p_chr_id IN NUMBER,p_sty_id IN NUMBER) IS
4475     SELECT  STM.id
4476     FROM    OKL_STREAMS_V STM,
4477             OKL_STRM_TYPE_B STY
4478     WHERE   STM.khr_id   = p_chr_id
4479     AND     STM.say_code = 'CURR'
4480     AND     STM.STY_ID   = STY.ID
4481     AND     STY.ID  NOT IN (nvl(p_sty_id, OKL_API.G_MISS_NUM)) -- rmunjulu 4058630 check for NVL
4482     AND     nvl(STY.STREAM_TYPE_SUBCLASS, 'X') NOT IN ('INVESTOR_DISBURSEMENT') -- new subclass
4483 --    AND     nvl(STM.sgn_code,'*') <> 'INTC'; -- rmunjulu 11-Apr-06 ER 5139307
4484     AND     nvl(STM.sgn_code,'*') NOT IN ('LATE_CALC'); -- Bug#i6472228 - Added to exclude streams generated
4485                                                                -- during late charge/interest calculation processs
4486                                                                -- These can be LATE FEE/LATE INTEREST/ INVESTOR LATE FEE/ INTEREST
4487 
4488     --Fix for Bug#14255615
4489     CURSOR rev_rec_method_csr(p_chr_id IN NUMBER) IS
4490     SELECT ppm.revenue_recognition_method
4491     FROM okc_k_headers_b chr,
4492          okl_k_headers khr,
4493          okl_product_parameters_v ppm
4494     WHERE chr.id = p_chr_id
4495     AND chr.id = khr.id
4496     AND khr.pdt_id = ppm.id;
4497 
4498     l_rev_rec_method       VARCHAR2(150);
4499 
4500 
4501    k_streams_rec           k_streams_csr%ROWTYPE;
4502    l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4503    lp_stmv_tbl             stmv_tbl_type;
4504    lx_stmv_tbl             stmv_tbl_type;
4505    l_streams_found         VARCHAR2(1) := G_NO;
4506    i                       NUMBER;
4507    l_id                    NUMBER;
4508 
4509     --smoduga added variables for userdefined streams 3925469
4510    lx_sty_id NUMBER;
4511 
4512    -- rmunjulu bug 4058630
4513    l_dummy_status VARCHAR2(3);
4514    -- asawanka added for debug feature start
4515     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_close_streams';
4516     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
4517     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
4518     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
4519     -- asawanka added for debug feature end
4520   BEGIN
4521      IF (is_debug_procedure_on) THEN
4522        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
4523      END IF;
4524 
4525     -- Start a savepoint to rollback to if error in this block
4526     SAVEPOINT close_streams;
4527 
4528     -- smoduga +++++++++ User Defined Streams -- start    ++++++++++++++++
4529    OKL_STREAMS_UTIL.get_primary_stream_type(p_term_rec.p_contract_id,
4530                                                    'RESIDUAL_VALUE',
4531                                                    l_dummy_status, -- rmunjulu 4058630 (get into dummy as return not needed)
4532                                                    lx_sty_id);
4533     -- smoduga +++++++++ User Defined Streams -- end    ++++++++++++++++
4534 
4535     IF (is_debug_statement_on) THEN
4536         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
4537         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_trn_already_set = '||p_trn_already_set);
4538         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'px_tcnv_rec.tmt_streams_updated_yn = '||px_tcnv_rec.tmt_streams_updated_yn);
4539     END IF;
4540     -- Check if streams exist
4541     OPEN k_streams_csr(p_term_rec.p_contract_id,nvl(lx_sty_id,OKL_API.G_MISS_NUM)); -- rmunjulu 4058630 check for NVL
4542     FETCH k_streams_csr INTO l_id;
4543     IF k_streams_csr%FOUND THEN
4544       l_streams_found := G_YES;
4545     END IF;
4546     CLOSE k_streams_csr;
4547 
4548    -- Fix for Bug#13933234
4549     OPEN  rev_rec_method_csr (p_chr_id => p_term_rec.p_contract_id);
4550     FETCH rev_rec_method_csr into l_rev_rec_method;
4551     CLOSE rev_rec_method_csr;
4552 
4553     IF (is_debug_statement_on) THEN
4554         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'rev_rec_method '|| l_rev_rec_method);
4555     END IF;
4556 
4557 
4558     -- RMUNJULU Bug # 3023206 Changed the priority, first check if Close Streams
4559     -- needed and then check if streams exists which needs to be closed
4560     -- if streams found then
4561 
4562 --    IF (l_streams_found = G_YES) THEN
4563     IF (p_trn_already_set = G_YES
4564         AND NVL(px_tcnv_rec.tmt_streams_updated_yn, '?') <> G_YES)
4565     OR (p_trn_already_set = G_NO) THEN
4566 
4567        -- if streams found then
4568        -- Fix for Bug#13933234, added condition rev_rec_method <> 'ACTUAL'
4569        IF (l_streams_found = G_YES AND (NVL(l_rev_rec_method,OKL_API.G_MISS_CHAR) <> 'ACTUAL')) THEN
4570 
4571              i := 1;
4572              -- set the tbl type for streams pub
4573              FOR k_streams_rec IN k_streams_csr(p_term_rec.p_contract_id,nvl(lx_sty_id,OKL_API.G_MISS_NUM)) LOOP -- rmunjulu 4058630 check for NVL
4574                lp_stmv_tbl(i).khr_id       :=   p_term_rec.p_contract_id;
4575                lp_stmv_tbl(i).active_yn    :=   G_NO;
4576                lp_stmv_tbl(i).id           :=   k_streams_rec.id;
4577 
4578                -- RMUNJULU 28-MAR-03 2877278 Added code to set say_code to HIST
4579                lp_stmv_tbl(i).say_code    :=   'HIST';
4580                lp_stmv_tbl(i).date_history :=   SYSDATE;
4581 
4582                i := i + 1;
4583              END LOOP;
4584              IF (is_debug_statement_on) THEN
4585                 OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling OKL_STREAMS_PUB.update_streams');
4586              END IF;
4587              -- close streams
4588              OKL_STREAMS_PUB.update_streams(
4589                p_api_version                => p_api_version,
4590                p_init_msg_list              => OKL_API.G_FALSE,
4591                x_return_status              => l_return_status,
4592                x_msg_count                  => x_msg_count,
4593                x_msg_data                   => x_msg_data,
4594                p_stmv_tbl                   => lp_stmv_tbl,
4595                x_stmv_tbl                   => lx_stmv_tbl);
4596              IF (is_debug_statement_on) THEN
4597                 OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called OKL_STREAMS_PUB.update_streams l_return_status ='||l_return_status);
4598              END IF;
4599 
4600              IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
4601                -- Streams table update failed.
4602                OKL_API.set_message( p_app_name    => G_APP_NAME,
4603                                     p_msg_name    => 'OKL_AM_ERR_UPD_STREAMS');
4604              END IF;
4605 
4606              -- Raise exception to rollback to if error
4607              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4608                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4609              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
4610                RAISE OKL_API.G_EXCEPTION_ERROR;
4611              END IF;
4612 
4613              -- store the highest degree of error
4614              set_overall_status(
4615                p_return_status                  => l_return_status,
4616                px_overall_status                => px_overall_status);
4617 
4618              -- set the transaction record
4619              set_transaction_rec(
4620                p_return_status                 => l_return_status,
4621                p_overall_status                => px_overall_status,
4622                p_tmt_flag                      => 'TMT_STREAMS_UPDATED_YN',
4623                p_tsu_code                      => 'WORKING',
4624                px_tcnv_rec                     => px_tcnv_rec);
4625 
4626 --       END IF;
4627        ELSE --( no streams found )
4628 
4629          -- No future billable streams found.
4630          OKL_API.set_message( p_app_name      => G_APP_NAME,
4631                               p_msg_name      => 'OKL_AM_NO_STREAMS');
4632 
4633          -- set the transaction record
4634          set_transaction_rec(
4635             p_return_status                 => l_return_status,
4636             p_overall_status                => px_overall_status,
4637             p_tmt_flag                      => 'TMT_STREAMS_UPDATED_YN',
4638             p_tsu_code                      => 'WORKING',
4639             p_ret_val                       => NULL,
4640             px_tcnv_rec                     => px_tcnv_rec);
4641 
4642       END IF;
4643 
4644     END IF;
4645 
4646     x_return_status      := l_return_status;
4647     x_stmv_tbl           := lx_stmv_tbl;
4648     IF (is_debug_procedure_on) THEN
4649        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
4650     END IF;
4651   EXCEPTION
4652     WHEN OKL_API.G_EXCEPTION_ERROR THEN
4653         IF (is_debug_exception_on) THEN
4654              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
4655         END IF;
4656 
4657          IF k_streams_csr%ISOPEN THEN
4658             CLOSE k_streams_csr;
4659          END IF;
4660 
4661          ROLLBACK TO close_streams;
4662 
4663          x_return_status := OKL_API.G_RET_STS_ERROR;
4664          -- store the highest degree of error
4665          set_overall_status(
4666                p_return_status                 => x_return_status,
4667                px_overall_status               => px_overall_status);
4668 
4669          -- set the transaction record
4670          set_transaction_rec(
4671                p_return_status                 => x_return_status,
4672                p_overall_status                => px_overall_status,
4673                p_tmt_flag                      => 'TMT_STREAMS_UPDATED_YN',
4674                p_tsu_code                      => 'ERROR',
4675                px_tcnv_rec                     => px_tcnv_rec);
4676 
4677     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
4678         IF (is_debug_exception_on) THEN
4679              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
4680         END IF;
4681 
4682          IF k_streams_csr%ISOPEN THEN
4683             CLOSE k_streams_csr;
4684          END IF;
4685 
4686          ROLLBACK TO close_streams;
4687 
4688          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
4689          -- store the highest degree of error
4690          set_overall_status(
4691                p_return_status                 => x_return_status,
4692                px_overall_status               => px_overall_status);
4693 
4694          -- set the transaction record
4695          set_transaction_rec(
4696                p_return_status                 => x_return_status,
4697                p_overall_status                => px_overall_status,
4698                p_tmt_flag                      => 'TMT_STREAMS_UPDATED_YN',
4699                p_tsu_code                      => 'ERROR',
4700                px_tcnv_rec                     => px_tcnv_rec);
4701 
4702     WHEN OTHERS THEN
4703         IF (is_debug_exception_on) THEN
4704            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
4705         END IF;
4706 
4707          IF k_streams_csr%ISOPEN THEN
4708             CLOSE k_streams_csr;
4709          END IF;
4710 
4711          ROLLBACK TO close_streams;
4712 
4713          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
4714          -- store the highest degree of error
4715          set_overall_status(
4716                p_return_status                 => x_return_status,
4717                px_overall_status               => px_overall_status);
4718 
4719          -- set the transaction record
4720          set_transaction_rec(
4721                p_return_status                 => x_return_status,
4722                p_overall_status                => px_overall_status,
4723                p_tmt_flag                      => 'TMT_STREAMS_UPDATED_YN',
4724                p_tsu_code                      => 'ERROR',
4725                px_tcnv_rec                     => px_tcnv_rec);
4726 
4727          -- Set the oracle error message
4728          OKL_API.set_message(
4729                          p_app_name      => OKC_API.G_APP_NAME,
4730                          p_msg_name      => g_unexpected_error,
4731                          p_token1        => g_sqlcode_token,
4732                          p_token1_value  => SQLCODE,
4733                          p_token2        => g_sqlerrm_token,
4734                          p_token2_value  => SQLERRM);
4735 
4736   END process_close_streams;
4737 
4738   -- rmunjulu BUYOUT PROCESS --+++++++ START   ++++++++++++++++++++++++++++++
4739   -- Start of comments
4740   --
4741   -- Procedure Name : process_auto_invoice
4742   -- Desciption     : This procedure checks if auto invoice is required and does invoicing for those amounts
4743   -- Business Rules :
4744   -- Parameters  :
4745   -- Version  : 1.0
4746   -- History        : RMUNJULU BUYOUT_PROCESS created
4747   --
4748   -- End of comments
4749   PROCEDURE process_auto_invoice(
4750            p_api_version                 IN  NUMBER,
4751            p_init_msg_list               IN  VARCHAR2,
4752            x_return_status               OUT NOCOPY VARCHAR2,
4753            x_msg_count                   OUT NOCOPY NUMBER,
4754            x_msg_data                    OUT NOCOPY VARCHAR2,
4755            p_term_rec                    IN term_rec_type,
4756            px_overall_status             IN OUT NOCOPY VARCHAR2,
4757            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
4758            --x_adjv_rec                    OUT NOCOPY adjv_rec_type,
4759            --x_ajlv_tbl                    OUT NOCOPY ajlv_tbl_type,
4760            p_sys_date                    IN DATE,
4761            p_trn_already_set             IN VARCHAR2,
4762      p_auto_invoice_yn             IN VARCHAR2 DEFAULT NULL, -- rmunjulu BUYOUT_PROCESS
4763            p_klev_tbl                    IN klev_tbl_type DEFAULT empty_klev_tbl)  IS -- rmunjulu BUYOUT_PROCESS
4764 
4765    l_invoice_amount NUMBER;
4766    j NUMBER;
4767    l_curr_code VARCHAR2(200);
4768    l_formatted_inv_amt VARCHAR2(200);
4769    l_try_id  NUMBER;
4770    l_purchase_option_sty_id  NUMBER;
4771    l_return_status             VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4772    l_taiv_rec         okl_trx_ar_invoices_pub.taiv_rec_type;
4773    lx_taiv_rec        okl_trx_ar_invoices_pub.taiv_rec_type;
4774    l_tilv_rec         okl_txl_ar_inv_lns_pub.tilv_rec_type;
4775    lx_tilv_rec        okl_txl_ar_inv_lns_pub.tilv_rec_type;
4776    l_empty_taiv_rec   okl_trx_ar_invoices_pub.taiv_rec_type;
4777    l_empty_tilv_rec   okl_txl_ar_inv_lns_pub.tilv_rec_type;
4778    l_api_name         CONSTANT VARCHAR2(30) := 'process_auto_inv';
4779    l_api_version      CONSTANT NUMBER := 1;
4780    l_msg_count        NUMBER;
4781    l_msg_data         VARCHAR2(2000);
4782    l_bpd_acc_rec       okl_acc_call_pub.bpd_acc_rec_type;
4783 
4784    l_quote_accpt_date DATE;
4785    l_quote_eff_date   DATE;
4786 
4787    l_functional_currency_code VARCHAR2(15);
4788    l_contract_currency_code VARCHAR2(15);
4789    l_currency_conversion_type VARCHAR2(30);
4790    l_currency_conversion_rate NUMBER;
4791    l_currency_conversion_date DATE;
4792    l_converted_amount NUMBER;
4793 
4794    -- Since we do not use the amount or converted amount
4795    -- set a hardcoded value for the amount (and pass to to
4796    -- OKL_ACCOUNTING_UTIL.convert_to_functional_currency and get back
4797    -- conversion values )
4798    l_hard_coded_amount NUMBER := 100;
4799 -- bug 12765251
4800        l_invoice_amount_total NUMBER;
4801        l_prorate VARCHAR2(150);
4802        l_invoice_amount_tot NUMBER;
4803        l_inv_diff NUMBER;
4804 
4805  -- ansethur  23-FEB-07  R12B Added for Billing Enhancement Project   Start Changes
4806    l_tldv_tbl          okl_tld_pvt.tldv_tbl_type;
4807    lx_tldv_tbl         okl_tld_pvt.tldv_tbl_type;
4808 
4809    l_tilv_tbl          okl_txl_ar_inv_lns_pub.tilv_tbl_type;
4810    lx_tilv_tbl         okl_txl_ar_inv_lns_pub.tilv_tbl_type;
4811  -- ansethur  23-FEB-07  R12B Added for Billing Enhancement Project  End Changes
4812    -- asawanka added for debug feature start
4813     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_auto_invoice';
4814     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
4815     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
4816     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
4817     -- asawanka added for debug feature end
4818   BEGIN
4819      IF (is_debug_procedure_on) THEN
4820        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
4821      END IF;
4822 
4823        SAVEPOINT auto_invoice;
4824 
4825        -- If quote exists then close date is quote accept date else sysdate
4826        IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
4827 
4828          l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
4829          l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
4830 
4831        ELSE
4832 
4833          l_quote_accpt_date := p_sys_date;
4834          l_quote_eff_date := p_sys_date;
4835 
4836        END IF;
4837        IF (is_debug_statement_on) THEN
4838           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_contract_id = '||p_term_rec.p_contract_id);
4839           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_trn_already_set = '||p_trn_already_set);
4840           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'px_tcnv_rec.tmt_streams_updated_yn = '||px_tcnv_rec.tmt_streams_updated_yn);
4841           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_auto_invoice_yn = '|| p_auto_invoice_yn);
4842        END IF;
4843        -- check if auto invoice needed and not done already
4844        IF  ((p_trn_already_set = G_YES
4845         AND  NVL(px_tcnv_rec.tmt_generic_flag3_yn, '?') <> G_YES)
4846         OR p_trn_already_set = G_NO)
4847     AND p_auto_invoice_yn = 'Y'
4848        AND p_klev_tbl.COUNT > 0 THEN
4849 
4850           IF (is_debug_statement_on) THEN
4851              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_STREAMS_UTIL.get_primary_stream_type.');
4852           END IF;
4853 
4854           -- get stream type id for Quote Purchase Amount Purpose
4855           OKL_STREAMS_UTIL.get_primary_stream_type(
4856                            p_khr_id              => p_term_rec.p_contract_id,
4857                            p_primary_sty_purpose => 'AMBPOC', -- Quote Purchase Amount Purpose
4858                            x_return_status       => l_return_status,
4859                            x_primary_sty_id      => l_purchase_option_sty_id);
4860 
4861           IF (is_debug_statement_on) THEN
4862              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_STREAMS_UTIL.get_primary_stream_type.l_return_status = '||l_return_status);
4863           END IF;
4864 
4865           IF l_return_status = OKL_API.G_RET_STS_SUCCESS
4866           AND (l_purchase_option_sty_id IS NULL OR l_purchase_option_sty_id = OKL_API.G_MISS_NUM) THEN
4867 
4868              l_return_status := OKL_API.G_RET_STS_ERROR;
4869           END IF;
4870 
4871           IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
4872 
4873              -- Unable to auto invoice the purchase amount for Contract CONTRACT_NUMBER.
4874              OKL_API.set_message(
4875                              p_app_name      => G_APP_NAME,
4876                              p_msg_name      => 'OKL_AM_INV_CNTRCT_ERR',
4877                              p_token1        => 'CONTRACT_NUMBER',
4878                              p_token1_value  => p_term_rec.p_contract_number);
4879           END IF;
4880 
4881           IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4882              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4883           ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
4884              RAISE OKL_API.G_EXCEPTION_ERROR;
4885           END IF;
4886 
4887           -- Get the currency code for contract
4888           l_curr_code := OKL_AM_UTIL_PVT.get_chr_currency(
4889                                  p_chr_id => p_term_rec.p_contract_id);
4890 
4891           -- Get the functional currency from AM_Util
4892           l_functional_currency_code := OKL_AM_UTIL_PVT.get_functional_currency();
4893           IF (is_debug_statement_on) THEN
4894              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_ACCOUNTING_UTIL.convert_to_functional_currency');
4895           END IF;
4896           -- Get the currency conversion details from ACCOUNTING_Util
4897           OKL_ACCOUNTING_UTIL.convert_to_functional_currency(
4898                      p_khr_id                   => p_term_rec.p_contract_id,
4899                      p_to_currency              => l_functional_currency_code,
4900                      p_transaction_date         => l_quote_accpt_date, -- rmunjulu EDAT
4901                      p_amount                   => l_hard_coded_amount,
4902                      x_return_status            => l_return_status,
4903                      x_contract_currency        => l_contract_currency_code,
4904                      x_currency_conversion_type => l_currency_conversion_type,
4905                      x_currency_conversion_rate => l_currency_conversion_rate,
4906                      x_currency_conversion_date => l_currency_conversion_date,
4907                      x_converted_amount         => l_converted_amount);
4908           IF (is_debug_statement_on) THEN
4909              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_ACCOUNTING_UTIL.convert_to_functional_currency l_return_status ='||l_return_status);
4910           END IF;
4911 
4912           -- Set some of the taiv rec attributes
4913           l_taiv_rec.currency_code            := l_contract_currency_code;
4914           l_taiv_rec.currency_conversion_type := l_currency_conversion_type;
4915           l_taiv_rec.currency_conversion_rate := l_currency_conversion_rate;
4916           l_taiv_rec.currency_conversion_date := l_currency_conversion_date;
4917           l_taiv_rec.try_id                   := l_try_id;
4918           l_taiv_rec.khr_id                   := p_term_rec.p_contract_id;
4919           l_taiv_rec.date_invoiced            := l_quote_accpt_date;
4920           l_taiv_rec.date_entered             := l_quote_accpt_date;
4921           l_taiv_rec.description              := 'Automatically Invoice at End Of Term';
4922           l_taiv_rec.trx_status_code          := 'SUBMITTED';--'ENTERED';
4923           --20-NOV-2006 ANSETHUR R12B - LEGAL ENTITY UPTAKE PROJECT
4924           l_taiv_rec.legal_entity_id          :=  OKL_LEGAL_ENTITY_UTIL.get_khr_le_id (p_term_rec.p_contract_id);
4925 
4926           -- set some of the tilv rec attributes
4927           l_tilv_rec.description              := 'Automatically Invoice at End Of Term';
4928           l_tilv_rec.sty_id                   := l_purchase_option_sty_id;
4929           l_tilv_rec.inv_receiv_line_code     := 'LINE';
4930 
4931 l_invoice_amount_total := 0;
4932 l_inv_diff :=0;
4933           -- loop thru assets and derive purchase amount and invoice the same
4934           FOR j IN p_klev_tbl.FIRST..p_klev_tbl.LAST LOOP
4935 
4936              -- get purchase amount for the asset
4937              get_purchase_amount(
4938                        p_term_rec        => p_term_rec,
4939                        p_kle_id          => p_klev_tbl(j).p_kle_id,
4940                        x_purchase_amount => l_invoice_amount,
4941                        x_return_status   => l_return_status);
4942 
4943              IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
4944 
4945                 -- Unable to auto invoice the purchase amount for Contract CONTRACT_NUMBER.
4946                 OKL_API.set_message(
4947                  p_app_name      => G_APP_NAME,
4948                              p_msg_name      => 'OKL_AM_INV_CNTRCT_ERR',
4949                              p_token1        => 'CONTRACT_NUMBER',
4950                              p_token1_value  => p_term_rec.p_contract_number);
4951              END IF;
4952 
4953              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4954                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
4955              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
4956                 RAISE OKL_API.G_EXCEPTION_ERROR;
4957              END IF;
4958 
4959 /*bug 12999514  start proration changes*/
4960 
4961    l_invoice_amount_total := l_invoice_amount_total+l_invoice_amount;
4962 IF j=p_klev_tbl.LAST THEN
4963   get_purchase_amount_total(
4964                        p_term_rec        => p_term_rec,
4965                        x_prorate          => l_prorate,
4966                        x_purchase_amount => l_invoice_amount_tot,
4967                        x_return_status   => l_return_status);
4968 
4969   IF l_prorate='PRORATE' THEN
4970 
4971 l_inv_diff := l_invoice_amount_tot - l_invoice_amount_total;
4972 l_invoice_amount:=l_invoice_amount+l_inv_diff;
4973   END IF;
4974 
4975 END IF;
4976 
4977 /*end proration changes*/
4978 
4979              -- Get transaction id based on amount type
4980              IF l_invoice_amount > 0 THEN
4981 
4982                  okl_am_util_pvt.get_transaction_id (
4983                           p_try_name        => 'BILLING',
4984                           x_return_status   => l_return_status,
4985                           x_try_id          => l_try_id);
4986 
4987              ELSIF l_invoice_amount < 0 THEN
4988 
4989                  okl_am_util_pvt.get_transaction_id (
4990                           p_try_name        => 'CREDIT MEMO',
4991                           x_return_status   => l_return_status,
4992                           x_try_id          => l_try_id);
4993              END IF;
4994 
4995              IF l_return_status <> OKL_API.G_RET_STS_SUCCESS
4996              OR NVL (l_try_id, OKL_API.G_MISS_NUM) = OKL_API.G_MISS_NUM THEN
4997 
4998                  OKL_API.SET_MESSAGE (
4999                      p_app_name      => G_APP_NAME,
5000                      p_msg_name      => G_INVALID_VALUE,
5001                      p_token1        => G_COL_NAME_TOKEN,
5002                      p_token1_value  => 'Transaction Type');
5003 
5004                 -- Unable to auto invoice the purchase amount for Contract CONTRACT_NUMBER.
5005                 OKL_API.set_message(
5006                  p_app_name      => G_APP_NAME,
5007                              p_msg_name      => 'OKL_AM_INV_CNTRCT_ERR',
5008                              p_token1        => 'CONTRACT_NUMBER',
5009                              p_token1_value  => p_term_rec.p_contract_number);
5010              END IF;
5011 
5012              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5013                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5014              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5015                 RAISE OKL_API.G_EXCEPTION_ERROR;
5016              END IF;
5017 
5018              -- start invoice part only if invoice amount <> 0
5019              IF  l_invoice_amount <> 0 THEN
5020 
5021                  l_taiv_rec.amount := l_invoice_amount;
5022                  l_taiv_rec.try_id := l_try_id;
5023 
5024  --ansethur  23-FEB-2007  Added for R12 B Billing Architecture Start Changes
5025  -- Included call to Enhanced Billing API in the place of the calls to Billing Header,Lines and Distributions
5026                  l_taiv_rec.okl_source_billing_trx := 'TERMINATION';
5027                  l_tilv_rec.line_number   := j;
5028                  l_tilv_rec.kle_id        := p_klev_tbl(j).p_kle_id;
5029                  l_tilv_rec.amount        := l_invoice_amount;
5030 
5031                  l_tilv_tbl(0)            := l_tilv_rec; -- Assign the line record in tilv_tbl structure
5032 
5033                  IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5034                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5035                          'Process_Auto_Invoice.okl_internal_billing_pvt.create_billing_trx.',
5036                          'Start(+)');
5037                  END IF;
5038                  IF (is_debug_statement_on) THEN
5039                    OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling okl_internal_billing_pvt.create_billing_trx');
5040                  END IF;
5041                  okl_internal_billing_pvt.create_billing_trx(p_api_version   => l_api_version,
5042                                                              p_init_msg_list => p_init_msg_list,
5043                                                              x_return_status => x_return_status,
5044                                                              x_msg_count     => x_msg_count,
5045                                                              x_msg_data      => x_msg_data,
5046                                                              p_taiv_rec      => l_taiv_rec,
5047                                                              p_tilv_tbl      => l_tilv_tbl,
5048                                                              p_tldv_tbl      => l_tldv_tbl,
5049                                                              x_taiv_rec      => lx_taiv_rec,
5050                                                              x_tilv_tbl      => lx_tilv_tbl,
5051                                                              x_tldv_tbl      => lx_tldv_tbl);
5052                  IF (is_debug_statement_on) THEN
5053                    OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called okl_internal_billing_pvt.create_billing_trx x_return_status = '||x_return_status);
5054                  END IF;
5055 
5056 
5057                IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5058                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5059                ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5060                 RAISE OKL_API.G_EXCEPTION_ERROR;
5061                END IF;
5062                IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5063                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5064                          'Process_Auto_Invoice.okl_internal_billing_pvt.create_billing_trx.',
5065                          'End(+)');
5066                  END IF;
5067 
5068 /* --ansethur  23-FEB-2007  commented for R12 B Billing Architecture
5069                  IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5070                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5071                          'Process_Auto_Invoice.okl_trx_ar_invoices_pub.insert_trx_ar_invoices.',
5072                          'Start(+)');
5073                  END IF;
5074 
5075                  OKL_TRX_AR_INVOICES_PUB.insert_trx_ar_invoices (
5076                        p_api_version   => P_api_version,
5077                        p_init_msg_list => OKL_API.G_FALSE,
5078                        x_return_status => l_return_status,
5079                        x_msg_count     => l_msg_count,
5080                        x_msg_data      => l_msg_data,
5081                        p_taiv_rec      => l_taiv_rec,
5082                        x_taiv_rec      => lx_taiv_rec);
5083 
5084                  IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5085 
5086                     -- Error invoicing Asset ASSET_NUMBER of Contract CONTRACT_NUMBER.
5087                     OKL_API.set_message(
5088                                          p_app_name      => G_APP_NAME,
5089                                          p_msg_name      => 'OKL_AM_INV_AMT_ERR',
5090                                          p_token1        => 'ASSET_NUMBER',
5091                                          p_token1_value  => p_klev_tbl(j).p_asset_name,
5092                                          p_token2        => 'CONTRACT_NUMBER',
5093                                          p_token2_value  => p_term_rec.p_contract_number);
5094                  END IF;
5095 
5096                  IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5097                    RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5098                  ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5099                     RAISE OKL_API.G_EXCEPTION_ERROR;
5100                  END IF;
5101 
5102                  IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5103                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5104                          'Process_Auto_Invoice.okl_trx_ar_invoices_pub.insert_trx_ar_invoices.',
5105                          'End(-)');
5106                  END IF;
5107 
5108                  l_tilv_rec.line_number   := j;
5109                  l_tilv_rec.kle_id        := p_klev_tbl(j).p_kle_id;
5110                  l_tilv_rec.amount        := l_invoice_amount;
5111                  l_tilv_rec.tai_id        := lx_taiv_rec.id;
5112 
5113                  IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5114                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5115                          'Process_Auto_Invoice.okl_trx_ar_invoices_pub.insert_txl_ar_inv_lns.',
5116                          'Start(+)');
5117                  END IF;
5118 
5119                  -- Create Invoice Line
5120                  OKL_TXL_AR_INV_LNS_PUB.insert_txl_ar_inv_lns (
5121                      p_api_version   => l_api_version,
5122                      p_init_msg_list => OKL_API.G_FALSE,
5123                      x_return_status => l_return_status,
5124                      x_msg_count     => l_msg_count,
5125                      x_msg_data      => l_msg_data,
5126                      p_tilv_rec      => l_tilv_rec,
5127                      x_tilv_rec      => lx_tilv_rec);
5128 
5129                  IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5130 
5131                     -- Error invoicing Asset ASSET_NUMBER of Contract CONTRACT_NUMBER.
5132                     OKL_API.set_message(
5133                                          p_app_name      => G_APP_NAME,
5134                                          p_msg_name      => 'OKL_AM_INV_AMT_ERR',
5135                                          p_token1        => 'ASSET_NUMBER',
5136                                          p_token1_value  => p_klev_tbl(j).p_asset_name,
5137                                          p_token2        => 'CONTRACT_NUMBER',
5138                                          p_token2_value  => p_term_rec.p_contract_number);
5139                  END IF;
5140                  IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5141                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5142                  ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5143                     RAISE OKL_API.G_EXCEPTION_ERROR;
5144                  END IF;
5145 
5146                  IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5147                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5148                          'Process_Auto_Invoice.okl_trx_ar_invoices_pub.insert_txl_ar_inv_lns.',
5149                          'End(-)');
5150                  END IF;
5151 
5152                  IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5153                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5154                          'Process_Auto_Invoice.OKL_ACC_CALL_PUB.create_acc_trans.',
5155                          'Start(+)');
5156                  END IF;
5157 
5158                  l_bpd_acc_rec.id            := lx_tilv_rec.id;
5159                  l_bpd_acc_rec.source_table  := 'OKL_TXL_AR_INV_LNS_B';
5160 
5161                  -- Create Accounting Distribution
5162                  OKL_ACC_CALL_PUB.create_acc_trans(
5163                         p_api_version   => l_api_version,
5164                         p_init_msg_list => OKL_API.G_FALSE,
5165                         x_return_status => l_return_status,
5166                         x_msg_count     => l_msg_count,
5167                         x_msg_data      => l_msg_data,
5168                         p_bpd_acc_rec   => l_bpd_acc_rec);
5169 
5170                  IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5171 
5172                     -- Error invoicing Asset ASSET_NUMBER of Contract CONTRACT_NUMBER.
5173                     OKL_API.set_message(
5174                              p_app_name      => G_APP_NAME,
5175                              p_msg_name      => 'OKL_AM_INV_AMT_ERR',
5176                              p_token1        => 'ASSET_NUMBER',
5177                              p_token1_value  => p_klev_tbl(j).p_asset_name,
5178                              p_token2        => 'CONTRACT_NUMBER',
5179                              p_token2_value  => p_term_rec.p_contract_number);
5180                  END IF;
5181 
5182                  IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5183                     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5184                  ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5185                     RAISE OKL_API.G_EXCEPTION_ERROR;
5186                  END IF;
5187 
5188                  IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5189                     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5190                          'Process_Auto_Invoice.OKL_ACC_CALL_PUB.create_acc_trans.',
5191                          'End(-)');
5192                  END IF;
5193 --ansethur  23-FEB-2007  commented for R12 B Billing Architecture  Ends
5194 */
5195  --ansethur  23-FEB-2007  Added for R12 B Billing Architecture End Changes
5196                  -- Format the invoice amt
5197                  l_formatted_inv_amt  := OKL_ACCOUNTING_UTIL.format_amount(
5198                                                            p_amount        => l_invoice_amount,
5199                                                            p_currency_code => l_curr_code);
5200 
5201                  -- Asset ASSET_NUMBER of Contract CONTRACT_NUMBER is invoiced with amount AMOUNT.
5202                  OKL_API.set_message(
5203                  p_app_name      => G_APP_NAME,
5204                              p_msg_name      => 'OKL_AM_INV_AMT_SUCCESS',
5205                              p_token1        => 'ASSET_NUMBER',
5206                              p_token1_value  => p_klev_tbl(j).p_asset_name,
5207                              p_token2        => 'CONTRACT_NUMBER',
5208                              p_token2_value  => p_term_rec.p_contract_number,
5209                              p_token3        => 'AMOUNT',
5210                              p_token3_value  => l_formatted_inv_amt);
5211 
5212               END IF; -- end of if l_invoice_amount <> 0
5213            END LOOP;
5214 
5215            -- Set the tmt_generic_flag3_yn
5216            set_transaction_rec(
5217                  p_return_status   => l_return_status,
5218                  p_overall_status  => px_overall_status,
5219                  p_tmt_flag        => 'TMT_GENERIC_FLAG3_YN',
5220                  p_tsu_code        => 'WORKING',
5221                  p_ret_val         => NULL,
5222                  px_tcnv_rec       => px_tcnv_rec);
5223 
5224        END IF;
5225 
5226        x_return_status := l_return_status;
5227 
5228     IF (is_debug_procedure_on) THEN
5229        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
5230     END IF;
5231     EXCEPTION
5232 
5233       WHEN OKL_API.G_EXCEPTION_ERROR THEN
5234         IF (is_debug_exception_on) THEN
5235              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
5236         END IF;
5237 
5238          ROLLBACK TO auto_invoice;
5239 
5240          x_return_status := OKL_API.G_RET_STS_ERROR;
5241 
5242          -- store the highest degree of error
5243          set_overall_status(
5244                  p_return_status    => x_return_status,
5245                  px_overall_status  => px_overall_status);
5246 
5247         -- Set the tmt_generic_flag3_yn
5248         set_transaction_rec(
5249                  p_return_status    => x_return_status,
5250                  p_overall_status   => px_overall_status,
5251                  p_tmt_flag        => 'TMT_GENERIC_FLAG3_YN',
5252                  p_tsu_code        => 'ERROR',
5253                  p_ret_val         => NULL,
5254                  px_tcnv_rec       => px_tcnv_rec);
5255 
5256       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5257         IF (is_debug_exception_on) THEN
5258              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
5259         END IF;
5260 
5261          ROLLBACK TO auto_invoice;
5262 
5263          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
5264 
5265          -- store the highest degree of error
5266          set_overall_status(
5267                  p_return_status    => x_return_status,
5268                  px_overall_status  => px_overall_status);
5269 
5270         -- Set the tmt_generic_flag3_yn
5271         set_transaction_rec(
5272                  p_return_status    => x_return_status,
5273                  p_overall_status   => px_overall_status,
5274                  p_tmt_flag        => 'TMT_GENERIC_FLAG3_YN',
5275                  p_tsu_code        => 'ERROR',
5276                  p_ret_val         => NULL,
5277                  px_tcnv_rec       => px_tcnv_rec);
5278 
5279       WHEN OTHERS THEN
5280         IF (is_debug_exception_on) THEN
5281            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
5282         END IF;
5283 
5284          ROLLBACK TO auto_invoice;
5285 
5286          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
5287 
5288          -- store the highest degree of error
5289          set_overall_status(
5290                  p_return_status    => x_return_status,
5291                  px_overall_status  => px_overall_status);
5292 
5293         -- Set the tmt_generic_flag3_yn
5294         set_transaction_rec(
5295                  p_return_status    => x_return_status,
5296                  p_overall_status   => px_overall_status,
5297                  p_tmt_flag        => 'TMT_GENERIC_FLAG3_YN',
5298                  p_tsu_code        => 'ERROR',
5299                  p_ret_val         => NULL,
5300                  px_tcnv_rec       => px_tcnv_rec);
5301 
5302     END;
5303     -- rmunjulu BUYOUT PROCESS --+++++++ END   ++++++++++++++++++++++++++++++
5304 
5305   -- Start of comments
5306   --
5307   -- Procedure Name : process_close_balances
5308   -- Desciption     : Calls the AR adjustments apis for K header and
5309   --                  Lines to close balances
5310   -- Business Rules :
5311   -- Parameters    :
5312   -- Version      : 1.0
5313   -- History        : 03-JAN-03 RMUNJULU 2683876 Changed
5314   --                  to set the currency conversion columns
5315   --                  Added code to exit when product id not found
5316   --                  Changed cursor to get balance rec with 100 percent
5317   --                  DEBIT distribution
5318   --                : RMUNJULU 07-APR-03 2883292 Changed IF to check for NULL
5319   --                  tolerance_amt instead of -1
5320   --                : RMUNJULU 28-APR-04 3596626 Added code to set lp_acc_gen_primary_key_tbl
5321   --                  Also changed the processing to do Create Header, Create Line , Create Accounting Distribution
5322   --                : rmunjulu EDAT Added code to get quote accept date and pass as close bal date
5323   --                  Also added code to get the unpaid invoices before the quote effective date
5324   --                  Also added code to send additional parameters of quote_id, contract_id and transaction_date
5325   --                  to accounting engine
5326   --                : rmunjulu EDAT 29-Dec-04 did to_char to convert to right format
5327   --                : rmunjulu BUYOUT_PROCESS
5328   --
5329   -- End of comments
5330   PROCEDURE process_close_balances(
5331            p_api_version                 IN  NUMBER,
5332            p_init_msg_list               IN  VARCHAR2,
5333            x_return_status               OUT NOCOPY VARCHAR2,
5334            x_msg_count                   OUT NOCOPY NUMBER,
5335            x_msg_data                    OUT NOCOPY VARCHAR2,
5336            p_term_rec                    IN term_rec_type,
5337            px_overall_status             IN OUT NOCOPY VARCHAR2,
5338            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
5339            x_adjv_rec                    OUT NOCOPY adjv_rec_type,
5340            x_ajlv_tbl                    OUT NOCOPY ajlv_tbl_type,
5341            p_sys_date                    IN DATE,
5342            p_trn_already_set             IN VARCHAR2,
5343      p_auto_invoice_yn             IN VARCHAR2 DEFAULT NULL, -- rmunjulu BUYOUT_PROCESS
5344            p_klev_tbl                    IN klev_tbl_type DEFAULT empty_klev_tbl)  IS -- rmunjulu BUYOUT_PROCESS
5345 
5346    -- Cursor to get the balances of contract
5347    CURSOR  k_balances_csr (p_khr_id IN NUMBER, p_trn_date DATE) IS -- rmunjulu EDAT
5348    SELECT  SUM(amount_due_remaining)
5349    FROM    OKL_BPD_LEASING_PAYMENT_TRX_V
5350    WHERE   contract_id = p_khr_id
5351    AND     invoice_date <= p_trn_date; -- rmunjulu EDAT -- Added condition to get only
5352                                 --those invoices which are before quote effective date
5353 
5354    -- Cursor to get the lines with amount due and payment schedule id for the balances
5355    CURSOR k_bal_lns_csr (p_khr_id IN NUMBER, p_trn_date DATE) IS -- rmunjulu EDAT
5356    SELECT OBLP.amount_due_remaining       AMOUNT,
5357           OBLP.stream_type_id             STREAM_TYPE_ID,
5358           OSTY.name                       STREAM_MEANING,
5359           OBLP.payment_schedule_id        SCHEDULE_ID,
5360           OBLP.receivables_invoice_number AR_INVOICE_NUMBER,
5361           OTIL.id                         TIL_ID,
5362           -999                            TLD_ID
5363    FROM   OKL_BPD_LEASING_PAYMENT_TRX_V  OBLP,
5364           OKL_TXL_AR_INV_LNS_V           OTIL,
5365           OKL_STRM_TYPE_V                OSTY
5366    WHERE  OBLP.contract_id             = p_khr_id
5367    AND    OBLP.receivables_invoice_id  = OTIL.receivables_invoice_id
5368    AND    OBLP.stream_type_id          = OSTY.id
5369    AND    OBLP.amount_due_remaining > 0
5370    AND    OBLP.invoice_date <= p_trn_date -- rmunjulu EDAT -- Added condition to get only
5371                                 --those invoices which are before quote effective date
5372    UNION
5373    SELECT OBLP.amount_due_remaining       AMOUNT,
5374           OBLP.stream_type_id             STREAM_TYPE_ID,
5375           OSTY.name                       STREAM_MEANING,
5376           OBLP.payment_schedule_id        SCHEDULE_ID,
5377           OBLP.receivables_invoice_number AR_INVOICE_NUMBER,
5378           OTAI.til_id_details             TIL_ID,
5379           OTAI.id                         TLD_ID
5380    FROM   OKL_BPD_LEASING_PAYMENT_TRX_V  OBLP,
5381           OKL_TXD_AR_LN_DTLS_V           OTAI,
5382           OKL_STRM_TYPE_V                OSTY
5383    WHERE  OBLP.contract_id             = p_khr_id
5384    AND    OBLP.receivables_invoice_id  = OTAI.receivables_invoice_id
5385    AND    OBLP.stream_type_id          = OSTY.id
5386    AND    OBLP.amount_due_remaining > 0
5387    AND    OBLP.invoice_date <= p_trn_date; -- rmunjulu EDAT -- Added condition to get only
5388                                 --those invoices which are before quote effective date
5389 
5390    -- Cursor to get the product of the contract
5391    CURSOR prod_id_csr (p_khr_id IN NUMBER) IS
5392      SELECT   pdt_id
5393      FROM     OKL_K_HEADERS_V
5394      WHERE    id = p_khr_id;
5395 
5396    -- Cursor to get the code_combination_id for the transaction id and
5397    -- transaction table
5398    -- RMUNJULU 03-JAN-03 2683876 Added code to
5399    -- make sure we get the debit distribution and also it is 100percent
5400    CURSOR code_combination_id_csr(p_source_id    IN NUMBER,
5401                                   p_source_table IN VARCHAR2) IS
5402     SELECT DST.code_combination_id
5403     FROM   OKL_TRNS_ACC_DSTRS DST
5404     WHERE  DST.source_id     = p_source_id
5405     AND    DST.source_table  = p_source_table
5406     AND    DST.cr_dr_flag    = 'D'
5407     AND    DST.percentage    = 100;
5408 
5409    k_bal_lns_rec               k_bal_lns_csr%ROWTYPE;
5410    l_return_status             VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5411    lp_adjv_rec                 adjv_rec_type;
5412    lx_adjv_rec                 adjv_rec_type;
5413 --   lp_ajlv_tbl                 ajlv_tbl_type;
5414 --   lx_ajlv_tbl                 ajlv_tbl_type;
5415    l_early_termination_yn      VARCHAR2(1) := OKL_API.G_FALSE;
5416    l_total_amount_due          NUMBER := -1;
5417    l_code_combination_id       NUMBER := -1;
5418    i                           NUMBER :=  1;
5419    l_tolerance_amt             NUMBER := -1;
5420    l_api_name                  VARCHAR2(30) := 'process_close_balances';
5421    l_pdt_id                    NUMBER := 0;
5422    lp_tmpl_identify_rec        OKL_ACCOUNT_DIST_PUB.tmpl_identify_rec_type;
5423    lp_dist_info_rec            OKL_ACCOUNT_DIST_PUB.dist_info_rec_type;
5424    lp_ctxt_val_tbl             OKL_ACCOUNT_DIST_PUB.ctxt_val_tbl_type;
5425    lp_acc_gen_primary_key_tbl  OKL_ACCOUNT_DIST_PUB.acc_gen_primary_key;
5426    lx_template_tbl             OKL_ACCOUNT_DIST_PUB.avlv_tbl_type;
5427    lx_amount_tbl               OKL_ACCOUNT_DIST_PUB.amount_tbl_type;
5428    l_overall_status            VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5429    l_try_id                    NUMBER;
5430    l_trans_meaning             VARCHAR2(200);
5431    l_currency_code             VARCHAR2(200);
5432    l_formatted_bal_amt         VARCHAR2(200);
5433    l_formatted_tol_amt         VARCHAR2(200);
5434    l_formatted_adj_amt         VARCHAR2(200);
5435 
5436      -- RMUNJULU 03-JAN-03 2683876 Added variables
5437      l_functional_currency_code VARCHAR2(15);
5438      l_contract_currency_code   VARCHAR2(15);
5439      l_currency_conversion_type VARCHAR2(30);
5440      l_currency_conversion_rate NUMBER;
5441      l_currency_conversion_date DATE;
5442      l_converted_amount NUMBER;
5443 
5444      -- Since we do not use the amount or converted amount
5445      -- set a hardcoded value for the amount (and pass to to
5446      -- OKL_ACCOUNTING_UTIL.convert_to_functional_currency and get back
5447      -- conversion values )
5448      l_hard_coded_amount NUMBER := 100;
5449 
5450      -- RMUNJULU 3596626
5451    lp_ajlv_rec                 OKL_TXL_ADJSTS_LNS_PUB.ajlv_rec_type;
5452    lx_ajlv_rec                 OKL_TXL_ADJSTS_LNS_PUB.ajlv_rec_type;
5453 
5454    l_ajlv_rec                  OKL_TXL_ADJSTS_LNS_PUB.ajlv_rec_type;
5455 
5456    -- rmunjulu EDAT
5457    l_quote_accpt_date DATE;
5458    l_quote_eff_date DATE;
5459 
5460    -- rmunjulu BUYOUT_PROCESS
5461    l_invoice_amount NUMBER;
5462    j NUMBER;
5463    l_curr_code VARCHAR2(200);
5464    l_formatted_inv_amt VARCHAR2(200);
5465    -- asawanka added for debug feature start
5466     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_close_balances';
5467     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
5468     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
5469     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
5470     -- asawanka added for debug feature end
5471   BEGIN
5472      IF (is_debug_procedure_on) THEN
5473        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
5474      END IF;
5475 
5476   ---
5477      --get the tolerance limit from profile
5478      -- get the total balances of ARs for the contract
5479      -- if total balance amount within the tolerance limit then
5480        -- close balances
5481      -- end if
5482 
5483     -- Establish savepoint so that when error rollback
5484     SAVEPOINT close_balances;
5485 
5486     -- RMUNJULU 3018641 Step Message
5487     -- Step : Close Balances
5488     OKL_API.set_message(
5489                         p_app_name      => G_APP_NAME,
5490                         p_msg_name      => 'OKL_AM_STEP_CLB');
5491 
5492     -- rmunjulu BUYOUT PROCESS --+++++++ START ++++++++++++++++++++++++++++++
5493     -- check for auto invoice yn and invoice if applicable
5494     process_auto_invoice(
5495         p_api_version                  => p_api_version,
5496         p_init_msg_list                => OKL_API.G_FALSE,
5497         x_return_status                => l_return_status,
5498         x_msg_count                    => x_msg_count,
5499         x_msg_data                     => x_msg_data,
5500         p_term_rec                      => p_term_rec,
5501         px_overall_status               => px_overall_status,
5502         px_tcnv_rec                     => px_tcnv_rec,
5503         --x_adjv_rec                      => lx_adjv_rec,
5504         --x_ajlv_tbl                      => lx_ajlv_tbl,
5505         p_sys_date                      => p_sys_date,
5506         p_trn_already_set               => p_trn_already_set,
5507         p_auto_invoice_yn               => p_auto_invoice_yn,
5508   p_klev_tbl                      => p_klev_tbl);
5509 
5510     -- If process auto invoice errors everything errors
5511     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5512       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5513     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5514       RAISE OKL_API.G_EXCEPTION_ERROR;
5515     END IF;
5516 
5517     -- store the highest degree of error
5518     set_overall_status(
5519                p_return_status                 => l_return_status,
5520                px_overall_status               => px_overall_status);
5521 
5522     -- set the transaction record
5523     set_transaction_rec(
5524                p_return_status                 => l_return_status,
5525                p_overall_status                => px_overall_status,
5526                p_tmt_flag                      => 'TMT_CLOSE_BALANCES_YN',
5527                p_tsu_code                      => 'WORKING',
5528                px_tcnv_rec                     => px_tcnv_rec);
5529 
5530     -- set the transaction record
5531     set_transaction_rec(
5532                p_return_status                 => l_return_status,
5533                p_overall_status                => px_overall_status,
5534                p_tmt_flag                      => 'TMT_GENERIC_FLAG3_YN',
5535                p_tsu_code                      => 'WORKING',
5536                px_tcnv_rec                     => px_tcnv_rec);
5537 
5538     -- rmunjulu BUYOUT PROCESS --+++++++ END   ++++++++++++++++++++++++++++++
5539     /*
5540     -- Establish savepoint so that when error rollback
5541     SAVEPOINT close_balances;
5542 
5543     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
5544 
5545     -- rmunjulu EDAT
5546     -- If quote exists then close date is quote accept date else sysdate
5547     IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
5548 
5549       l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
5550       l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
5551 
5552     ELSE
5553 
5554       l_quote_accpt_date := p_sys_date;
5555       l_quote_eff_date := p_sys_date;
5556 
5557     END IF;
5558 
5559     -- rmunjulu EDAT
5560     -- set the additional parameters with contract_id, quote_id and transaction_date
5561     -- to be passed to formula engine
5562 
5563     lp_ctxt_val_tbl(1).name := 'contract_id';
5564     lp_ctxt_val_tbl(1).value := p_term_rec.p_contract_id;
5565 
5566     lp_ctxt_val_tbl(2).name := 'quote_id';
5567     lp_ctxt_val_tbl(2).value := p_term_rec.p_quote_id;
5568 
5569     lp_ctxt_val_tbl(3).name := 'transaction_date';
5570     lp_ctxt_val_tbl(3).value := to_char(l_quote_accpt_date,'MM/DD/YYYY'); -- rmunjulu EDAT 29-Dec-04 did to_char to convert to right format
5571 
5572     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
5573 
5574     -- get the total balances of ARs for the contract
5575     OPEN  k_balances_csr(p_term_rec.p_contract_id,l_quote_eff_date); -- rmunjulu EDAT
5576     FETCH k_balances_csr INTO l_total_amount_due;
5577     CLOSE k_balances_csr;
5578 
5579     -- set the total amount if it is null
5580     IF l_total_amount_due IS NULL THEN
5581        l_total_amount_due := 0;
5582     END IF;
5583 
5584     -- Check if total amount due is +ve else set message and exit
5585     IF l_total_amount_due <= 0 THEN
5586 
5587       -- No outstanding balances found.
5588       OKL_API.set_message( p_app_name      => G_APP_NAME,
5589                            p_msg_name      => 'OKL_AM_NO_BAL');
5590 
5591       -- set the transaction record
5592       set_transaction_rec(
5593               p_return_status                 => l_return_status,
5594               p_overall_status                => px_overall_status,
5595               p_tmt_flag                      => 'TMT_CLOSE_BALANCES_YN',
5596               p_tsu_code                      => 'WORKING',
5597               p_ret_val                       => NULL,
5598               px_tcnv_rec                     => px_tcnv_rec);
5599 
5600     ELSE -- can try closing balances
5601 
5602       --get the tolerance limit from profile
5603       FND_PROFILE.get('OKL_SMALL_BALANCE_TOLERANCE',l_tolerance_amt);
5604 
5605       -- if no tolerance amt then assume tolerance amt = 0 ,
5606       -- raise warning msg and proceed
5607       -- RMUNJULU 07-APR-03 2883292 Changed IF to check for NULL instead of -1
5608       IF  l_tolerance_amt IS NULL THEN
5609 
5610         l_tolerance_amt := 0;
5611         -- No tolerance amount found for closing of balances.
5612         OKL_API.set_message( p_app_name    => G_APP_NAME,
5613                            p_msg_name      => 'OKL_AM_NO_TOL_AMT');
5614 
5615       END IF;
5616 
5617       -- rmunjulu BUYOUT_PROCESS -- do not close balances if auto invoice was done
5618       -- in this case user will run small balance write off concurrent program separately.
5619       IF  p_auto_invoice_yn = 'N' -- No auto invoice
5620    AND px_tcnv_rec.tmt_close_balances_yn = 'N' THEN -- had already errored
5621 
5622       -- IF total balance amount within the tolerance limit and amount due>0 then
5623       IF (l_total_amount_due <= l_tolerance_amt) THEN
5624 
5625          IF (p_trn_already_set = G_YES
5626              AND NVL(px_tcnv_rec.tmt_close_balances_yn, '?') <> G_YES)
5627          OR (p_trn_already_set = G_NO) THEN
5628 
5629            -- ******** CREATE HEADER TRN ************* --
5630 
5631            -- set the adjusts rec
5632            lp_adjv_rec.trx_status_code           :=   'WORKING'; -- tsu_code
5633            -- tcn_id is set to transaction id from transaction rec
5634            lp_adjv_rec.tcn_id                    :=   px_tcnv_rec.id;
5635            -- adjustment_reason_code comes from OKL_ADJUSTMENT_REASON
5636            lp_adjv_rec.adjustment_reason_code    :=   'SMALL AMT REMAINING';
5637            lp_adjv_rec.apply_date                :=   l_quote_eff_date; -- rmunjulu EDAT
5638            lp_adjv_rec.gl_date                   :=   l_quote_accpt_date; -- rmunjulu EDAT
5639 
5640            -- call the adjusts api
5641            OKL_TRX_AR_ADJSTS_PUB.insert_trx_ar_adjsts(
5642              p_api_version                   => p_api_version,
5643              p_init_msg_list                 => OKL_API.G_FALSE,
5644              x_return_status                 => l_return_status,
5645              x_msg_count                     => x_msg_count,
5646              x_msg_data                      => x_msg_data,
5647              p_adjv_rec                      => lp_adjv_rec,
5648              x_adjv_rec                      => lx_adjv_rec);
5649 
5650            IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5651              -- Error occurred when creating adjustment records to write off balances.
5652              OKL_API.set_message( p_app_name      => G_APP_NAME,
5653                                   p_msg_name      => 'OKL_AM_ERR_ADJST_BAL');
5654            END IF;
5655 
5656            -- Raise exception to rollback this whole block
5657            IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5658              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5659            ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5660              RAISE OKL_API.G_EXCEPTION_ERROR;
5661            END IF;
5662 
5663            -- ******** GET TRY ID FOR Balance Write off ************* --
5664 
5665            -- Get the transaction id for adjustments
5666            OKL_AM_UTIL_PVT.get_transaction_id(
5667                    p_try_name           => 'Balance Write off',
5668                    x_return_status       => l_return_status,
5669                    x_try_id             => l_try_id);
5670 
5671            IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5672 
5673              -- Message: Unable to find a transaction type for
5674              -- the transaction TRY_NAME
5675              OKL_API.set_message(
5676                               p_app_name       => G_APP_NAME,
5677                               p_msg_name       => 'OKL_AM_NO_TRX_TYPE_FOUND',
5678                               p_token1         => 'TRY_NAME',
5679                               p_token1_value   => l_trans_meaning);
5680            END IF;
5681 
5682            -- Raise exception to rollback this whole block
5683            IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5684              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5685            ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5686              RAISE OKL_API.G_EXCEPTION_ERROR;
5687            END IF;
5688 
5689            -- Get the meaning of lookup BALANCE_WRITE_OFF
5690            l_trans_meaning := OKL_AM_UTIL_PVT.get_lookup_meaning(
5691                                    p_lookup_type => 'OKL_ACCOUNTING_EVENT_TYPE',
5692                                    p_lookup_code => 'BALANCE_WRITE_OFF',
5693                                    p_validate_yn => 'Y');
5694 
5695            -- ******** GET PRODUCT ID ************* --
5696 
5697            -- get the product id
5698            OPEN  prod_id_csr(p_term_rec.p_contract_id);
5699            FETCH prod_id_csr INTO l_pdt_id;
5700            CLOSE prod_id_csr;
5701 
5702            -- raise error message if no pdt_id
5703            IF l_pdt_id IS NULL OR l_pdt_id = 0 THEN
5704              -- Error: Unable to create accounting entries because of a missing
5705              -- Product Type for the contract CONTRACT_NUMBER.
5706              OKL_API.set_message(
5707                                p_app_name    => G_APP_NAME,
5708                                p_msg_name    => 'OKL_AM_PRODUCT_ID_ERROR',
5709                                p_token1      => 'CONTRACT_NUMBER',
5710                                p_token1_value=> p_term_rec.p_contract_number);
5711            END IF;
5712 
5713            -- RMUNJULU 03-JAN-03 2683876 Added code to raise exception when
5714            -- no product id.
5715            -- Raise exception to rollback this whole block
5716            IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5717              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5718            ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5719              RAISE OKL_API.G_EXCEPTION_ERROR;
5720            END IF;
5721 
5722            -- RMUNJULU 03-JAN-03 2683876 Added code to Get
5723            -- the currency conversion parameters -- START
5724 
5725            -- Get the functional currency from AM_Util
5726            l_functional_currency_code := OKL_AM_UTIL_PVT.get_functional_currency;
5727 
5728            -- Get the currency conversion details from ACCOUNTING_Util
5729            OKL_ACCOUNTING_UTIL.convert_to_functional_currency(
5730                      p_khr_id                 => p_term_rec.p_contract_id,
5731                      p_to_currency           => l_functional_currency_code,
5732                      p_transaction_date     => l_quote_accpt_date, -- rmunjulu EDAT
5733                      p_amount              => l_hard_coded_amount,
5734                      x_return_status              => l_return_status,
5735                      x_contract_currency    => l_contract_currency_code,
5736                      x_currency_conversion_type   => l_currency_conversion_type,
5737                      x_currency_conversion_rate   => l_currency_conversion_rate,
5738                      x_currency_conversion_date   => l_currency_conversion_date,
5739                      x_converted_amount     => l_converted_amount);
5740 
5741 
5742             -- If error from OKL_ACCOUNTING_UTIL
5743            IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5744 
5745               -- Error occurred when creating accounting entries for
5746               -- transaction TRX_TYPE.
5747               OKL_API.set_message(
5748                            p_app_name      => G_APP_NAME,
5749                            p_msg_name      => 'OKL_AM_ERR_ACC_ENT',
5750                            p_token1        => 'TRX_TYPE',
5751                            p_token1_value  => l_trans_meaning);
5752 
5753            END IF;
5754 
5755 
5756            -- Raise exception to rollback this whole block
5757            IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5758              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5759            ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5760              RAISE OKL_API.G_EXCEPTION_ERROR;
5761            END IF;
5762 
5763            -- RMUNJULU 03-JAN-03 2683876 -- END
5764 
5765            -- ******** LOOP THRU AR INVOICES WHICH HAS BALANCES ************* --
5766 
5767            i := 1;
5768            FOR k_bal_lns_rec IN k_bal_lns_csr ( p_term_rec.p_contract_id,l_quote_eff_date) LOOP --rmunjulu EDAT
5769 
5770              -- RMUNJULU 28-APR-04 3596626
5771              -- Changed the processing, now do create Header, create line, do accounting, or else
5772              -- accounting engine will delete initial entries and will leave only
5773              -- one entry
5774 
5775              -- ******** CREATE A TRN LINE ************* --
5776 
5777              -- set the rec for adjsts lns
5778              lp_ajlv_rec.adj_id            :=   lx_adjv_rec.id;
5779              lp_ajlv_rec.til_id            :=   k_bal_lns_rec.til_id;
5780 
5781              IF  k_bal_lns_rec.tld_id <> -999
5782              AND k_bal_lns_rec.tld_id IS NOT NULL
5783              AND k_bal_lns_rec.tld_id <> OKL_API.G_MISS_NUM THEN
5784                  lp_ajlv_rec.tld_id          :=   k_bal_lns_rec.tld_id;
5785              END IF;
5786 
5787              lp_ajlv_rec.amount            :=   k_bal_lns_rec.amount;
5788              lp_ajlv_rec.psl_id            :=   k_bal_lns_rec.schedule_id;
5789 
5790              --call the txl_lns_adjsts
5791              OKL_TXL_ADJSTS_LNS_PUB.insert_txl_adjsts_lns(
5792                    p_api_version       => p_api_version,
5793                    p_init_msg_list     => OKL_API.G_FALSE,
5794                    x_return_status     => l_return_status,
5795                    x_msg_count         => x_msg_count,
5796                    x_msg_data          => x_msg_data,
5797                    p_ajlv_rec            => lp_ajlv_rec,
5798                    x_ajlv_rec            => lx_ajlv_rec);
5799 
5800              IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5801                   -- Error occurred when creating adjustment records to write
5802                   -- off balances.
5803                   OKL_API.set_message( p_app_name     => G_APP_NAME,
5804                                        p_msg_name     => 'OKL_AM_ERR_ADJST_BAL');
5805              END IF;
5806 
5807              -- Raise exception to rollback this whole block
5808              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5809                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5810              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5811                  RAISE OKL_API.G_EXCEPTION_ERROR;
5812              END IF;
5813 
5814              -- ******** CREATE ACCOUNTING DISTRIBUTIONS ************* --
5815 
5816              -- do accounting entries to get code_combination_id
5817              -- Set the tmpl_identify_rec in parameter
5818              lp_tmpl_identify_rec.product_id          :=  l_pdt_id;
5819              lp_tmpl_identify_rec.transaction_type_id :=  l_try_id;
5820              lp_tmpl_identify_rec.memo_yn             :=  G_NO;
5821              lp_tmpl_identify_rec.prior_year_yn       :=  G_NO;
5822              lp_tmpl_identify_rec.stream_type_id      :=  k_bal_lns_rec.stream_type_id;
5823 
5824              -- Set the dist_info_rec in parameter
5825              lp_dist_info_rec.source_id           :=  lx_ajlv_rec.id; -- RMUNJULU 3596626
5826              lp_dist_info_rec.source_table        :=  'OKL_TXL_ADJSTS_LNS_B'; -- RMUNJULU 3596626
5827              lp_dist_info_rec.accounting_date     :=  l_quote_accpt_date; -- rmunjulu EDAT
5828              lp_dist_info_rec.gl_reversal_flag    :=  G_NO;
5829              lp_dist_info_rec.post_to_gl          :=  G_NO;
5830              lp_dist_info_rec.contract_id         :=  p_term_rec.p_contract_id;
5831              lp_dist_info_rec.amount              :=  k_bal_lns_rec.amount;
5832 
5833              -- RMUNJULU 03-JAN-03 2683876 Added code to set
5834              -- the currency conversion parameters -- START
5835 
5836              -- Set the p_dist_info_rec for currency code
5837              lp_dist_info_rec.currency_code := l_contract_currency_code;
5838 
5839 
5840              -- If the functional currency code is different
5841              -- from contract currency code
5842              -- then set the rest of the currency conversion columns
5843              IF l_functional_currency_code <> l_contract_currency_code THEN
5844 
5845                 -- Set the p_dist_info_rec currency conversion columns
5846                 lp_dist_info_rec.currency_conversion_type := l_currency_conversion_type;
5847                 lp_dist_info_rec.currency_conversion_rate := l_currency_conversion_rate;
5848                 lp_dist_info_rec.currency_conversion_date := l_currency_conversion_date;
5849 
5850             END IF;
5851 
5852             -- RMUNJULU 03-JAN-03 2683876 -- END
5853 
5854             -- RMUNJULU 28-APR-04 3596626 Added code to set lp_acc_gen_primary_key_tbl
5855             -- for account generator
5856 
5857             OKL_ACC_CALL_PVT.okl_populate_acc_gen (
5858                            p_contract_id       => p_term_rec.p_contract_id,
5859                            p_contract_line_id  => NULL,
5860                            x_acc_gen_tbl       => lp_acc_gen_primary_key_tbl,
5861                            x_return_status     => l_return_status);
5862 
5863              -- Raise exception to rollback to savepoint for this block
5864              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5865                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5866              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5867                RAISE OKL_API.G_EXCEPTION_ERROR;
5868              END IF;
5869 
5870              -- call accounting engine
5871              -- This will calculate the adjstmnts and generate accounting entries
5872              OKL_ACCOUNT_DIST_PUB.create_accounting_dist(
5873                 p_api_version                  => p_api_version,
5874                 p_init_msg_list                => OKL_API.G_FALSE,
5875                 x_return_status                => l_return_status,
5876                 x_msg_count                    => x_msg_count,
5877                 x_msg_data                     => x_msg_data,
5878                 p_tmpl_identify_rec             => lp_tmpl_identify_rec,
5879                 p_dist_info_rec                 => lp_dist_info_rec,
5880                 p_ctxt_val_tbl                  => lp_ctxt_val_tbl,
5881                 p_acc_gen_primary_key_tbl       => lp_acc_gen_primary_key_tbl,
5882                 x_template_tbl                  => lx_template_tbl,
5883                 x_amount_tbl                    => lx_amount_tbl);
5884 
5885              IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5886 
5887                -- Error occurred when creating accounting entries
5888                -- for transaction type TRX_TYPE and stream type STREAM_TYPE.
5889                OKL_API.set_message( p_app_name      => G_APP_NAME,
5890                                     p_msg_name      => 'OKL_AM_ERR_ACC_ENT_MSG',
5891                                     p_token1        => 'TRX_TYPE',
5892                                     p_token1_value  => l_trans_meaning,
5893                                     p_token2        => 'STREAM_TYPE',
5894                                     p_token2_value  => k_bal_lns_rec.stream_meaning);
5895 
5896              END IF;
5897 
5898              -- Raise exception to rollback this whole block
5899              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5900                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5901              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5902                RAISE OKL_API.G_EXCEPTION_ERROR;
5903              END IF;
5904 
5905              -- ******** GET CCID FROM ACCOUNTING DISTRIBUTIONS ************* --
5906 
5907              -- Get the first code_combination_id for the transaction
5908              -- from OKL_TRNS_ACC_DSTRS_V
5909              OPEN  code_combination_id_csr(lx_ajlv_rec.id, 'OKL_TXL_ADJSTS_LNS_B'); -- RMUNJULU 3596626
5910              FETCH code_combination_id_csr INTO l_code_combination_id;
5911              CLOSE code_combination_id_csr;
5912 
5913              -- if code_combination_id not found then raise error
5914              IF l_code_combination_id = -1 OR l_code_combination_id IS NULL THEN
5915 
5916                -- Error: Unable to process small balance
5917                -- adjustments because of a missing Code Combination ID for the
5918                -- contract CONTRACT_NUMBER.
5919                OKL_API.set_message(
5920                                p_app_name    => G_APP_NAME,
5921                                p_msg_name    => 'OKL_AM_CODE_CMB_ERROR',
5922                                p_token1      => 'CONTRACT_NUMBER',
5923                                p_token1_value=> p_term_rec.p_contract_number);
5924 
5925                RAISE OKL_API.G_EXCEPTION_ERROR;
5926 
5927              END IF;
5928 
5929              -- ******** UPDATE TRN LINE WITH CCID ************* --
5930 
5931              lp_ajlv_rec := l_ajlv_rec; -- Empty the rec
5932 
5933              -- Set the rec with CCID got from accounting distibutions
5934              lp_ajlv_rec.id  := lx_ajlv_rec.id;
5935              lp_ajlv_rec.code_combination_id  :=   l_code_combination_id;
5936 
5937              lx_ajlv_rec := l_ajlv_rec; -- Empty the rec
5938 
5939              --call the txl_lns_adjsts
5940              OKL_TXL_ADJSTS_LNS_PUB.update_txl_adjsts_lns(
5941                    p_api_version       => p_api_version,
5942                    p_init_msg_list     => OKL_API.G_FALSE,
5943                    x_return_status     => l_return_status,
5944                    x_msg_count         => x_msg_count,
5945                    x_msg_data          => x_msg_data,
5946                    p_ajlv_rec            => lp_ajlv_rec,
5947                    x_ajlv_rec            => lx_ajlv_rec);
5948 
5949              IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
5950                   -- Error occurred when creating adjustment records to write
5951                   -- off balances.
5952                   OKL_API.set_message( p_app_name     => G_APP_NAME,
5953                                        p_msg_name     => 'OKL_AM_ERR_ADJST_BAL');
5954              END IF;
5955 
5956              -- Raise exception to rollback this whole block
5957              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5958                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5959              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5960                  RAISE OKL_API.G_EXCEPTION_ERROR;
5961              END IF;
5962 
5963 /*
5964              -- Get the first code_combination_id for the transaction
5965              -- from OKL_TRNS_ACC_DSTRS_V
5966              OPEN  code_combination_id_csr(lx_adjv_rec.id, 'OKL_TRX_AR_ADJSTS_B');
5967              FETCH code_combination_id_csr INTO l_code_combination_id;
5968              CLOSE code_combination_id_csr;
5969 
5970              -- if code_combination_id not found then raise error
5971              IF l_code_combination_id = -1 OR l_code_combination_id IS NULL THEN
5972 
5973                -- Error: Unable to process small balance
5974                -- adjustments because of a missing Code Combination ID for the
5975                -- contract CONTRACT_NUMBER.
5976                OKL_API.set_message(
5977                                p_app_name    => G_APP_NAME,
5978                                p_msg_name    => 'OKL_AM_CODE_CMB_ERROR',
5979                                p_token1      => 'CONTRACT_NUMBER',
5980                                p_token1_value=> p_term_rec.p_contract_number);
5981 
5982                RAISE OKL_API.G_EXCEPTION_ERROR;
5983 
5984              END IF;
5985 
5986              -- Loop thru the code combination ids to set the lns tbl
5987              FOR code_combination_id_rec
5988              IN  code_combination_id_csr (lx_adjv_rec.id, 'OKL_TRX_AR_ADJSTS_B') LOOP
5989 
5990                -- set the tbl for adjsts lns
5991                lp_ajlv_tbl(i).adj_id            :=   lx_adjv_rec.id;
5992                lp_ajlv_tbl(i).til_id            :=   k_bal_lns_rec.til_id;
5993 
5994                IF  k_bal_lns_rec.tld_id <> -999
5995                AND k_bal_lns_rec.tld_id IS NOT NULL
5996                AND k_bal_lns_rec.tld_id <> OKL_API.G_MISS_NUM THEN
5997                  lp_ajlv_tbl(i).tld_id          :=   k_bal_lns_rec.tld_id;
5998                END IF;
5999 
6000                lp_ajlv_tbl(i).amount            :=   k_bal_lns_rec.amount;
6001                lp_ajlv_tbl(i).psl_id            :=   k_bal_lns_rec.schedule_id;
6002 
6003                lp_ajlv_tbl(i).code_combination_id    :=   code_combination_id_rec.code_combination_id;
6004 
6005                i := i + 1;
6006              END LOOP; -- code combination recs
6007 */
6008 
6009           -- END LOOP; -- balances res
6010 
6011 /*
6012            --call the txl_lns_adjsts
6013            OKL_TXL_ADJSTS_LNS_PUB.insert_txl_adjsts_lns(
6014                p_api_version                     => p_api_version,
6015                p_init_msg_list                   => OKL_API.G_FALSE,
6016                x_return_status                   => l_return_status,
6017                x_msg_count                       => x_msg_count,
6018                x_msg_data                        => x_msg_data,
6019                p_ajlv_tbl                        => lp_ajlv_tbl,
6020                x_ajlv_tbl                        => lx_ajlv_tbl);
6021 
6022            IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
6023              -- Error occurred when creating adjustment records to write
6024              -- off balances.
6025              OKL_API.set_message( p_app_name     => G_APP_NAME,
6026                                   p_msg_name     => 'OKL_AM_ERR_ADJST_BAL');
6027            END IF;
6028 
6029            -- Raise exception to rollback this whole block
6030            IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6031              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6032            ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
6033              RAISE OKL_API.G_EXCEPTION_ERROR;
6034            END IF;
6035 */
6036 
6037 /*           -- Get the currency code for contract
6038            l_currency_code  := OKL_AM_UTIL_PVT.get_chr_currency(
6039                                                       p_term_rec.p_contract_id);
6040 
6041            -- Set all success messages for all balances
6042            FOR k_bal_lns_rec IN k_bal_lns_csr ( p_term_rec.p_contract_id, l_quote_eff_date) LOOP -- rmunjulu EDAT
6043 
6044              -- Format the adjustment amt
6045              l_formatted_adj_amt  := OKL_ACCOUNTING_UTIL.format_amount(
6046                                                             k_bal_lns_rec.amount,
6047                                                             l_currency_code);
6048 
6049              -- Append adjustment amt with currency code
6050              l_formatted_adj_amt  := l_formatted_adj_amt || ' ' ||l_currency_code;
6051 
6052              -- Adjustment transaction for AR invoice AR_INVOICE_NUM of amount AMOUNT
6053              -- has been created.
6054              OKL_API.set_message(
6055                                 p_app_name      => G_APP_NAME,
6056                                 p_msg_name      => 'OKL_AM_ACC_ENT_AR_INV_MSG',
6057                                 p_token1        => 'AR_INVOICE_NUM',
6058                                 p_token1_value  => k_bal_lns_rec.ar_invoice_number,
6059                                 p_token2        => 'AMOUNT',
6060                                 p_token2_value  => l_formatted_adj_amt);
6061 
6062              -- Accounting entries created for transaction type TRX_TYPE
6063              -- and stream type STREAM_TYPE.
6064              OKL_API.set_message(
6065                                 p_app_name      => G_APP_NAME,
6066                                 p_msg_name      => 'OKL_AM_ACC_ENT_CREATED_MSG',
6067                                 p_token1        => 'TRX_TYPE',
6068                                 p_token1_value  => l_trans_meaning,
6069                                 p_token2        => 'STREAM_TYPE',
6070                                 p_token2_value  => k_bal_lns_rec.stream_meaning);
6071            END LOOP;
6072 
6073            -- store the highest degree of error
6074            set_overall_status(
6075                p_return_status                 => l_return_status,
6076                px_overall_status               => px_overall_status);
6077 
6078            -- set the transaction record
6079            set_transaction_rec(
6080                p_return_status                 => l_return_status,
6081                p_overall_status                => px_overall_status,
6082                p_tmt_flag                      => 'TMT_CLOSE_BALANCES_YN',
6083                p_tsu_code                      => 'WORKING',
6084                px_tcnv_rec                     => px_tcnv_rec);
6085          END IF;
6086 
6087       ELSE  --(cannot close all balances since tolerance amt is less)
6088 
6089         -- Unable to close all outstanding balances due to tolerance amount.
6090         OKL_API.set_message( p_app_name      => G_APP_NAME,
6091                              p_msg_name      => 'OKL_AM_ERR_CLOSE_BAL');
6092 
6093         -- Get the currency code for contract
6094         l_currency_code      := OKL_AM_UTIL_PVT.get_chr_currency(p_term_rec.p_contract_id);
6095 
6096         -- Format the balance amt
6097         l_formatted_bal_amt  := OKL_ACCOUNTING_UTIL.format_amount(l_total_amount_due,l_currency_code);
6098 
6099         -- Append balance amt with currency code
6100         l_formatted_bal_amt  := l_formatted_bal_amt || ' ' ||l_currency_code;
6101 
6102         -- Format the tolerance amt
6103         l_formatted_tol_amt  := OKL_ACCOUNTING_UTIL.format_amount(l_tolerance_amt,l_currency_code);
6104 
6105         -- Append tolerance amt with currency code
6106         l_formatted_tol_amt  := l_formatted_tol_amt || ' ' ||l_currency_code;
6107 
6108         -- Outstanding balance BALANCE_AMT exceeds Tolerance Amount TOLERANCE_AMT.
6109         OKL_API.set_message( p_app_name      => G_APP_NAME,
6110                              p_msg_name      => 'OKL_AM_BAL_GTR_TOL',
6111                              p_token1        => 'BALANCE_AMT',
6112                              p_token1_value  => l_formatted_bal_amt,
6113                              p_token2        => 'TOLERANCE_AMT',
6114                              p_token2_value  => l_formatted_tol_amt);
6115 
6116         -- store the highest degree of error
6117         -- Outstanding balances being greater than tolerance amt is nomore a
6118         -- HARD error, it is now a SOFT error
6119         set_overall_status(
6120                p_return_status                 => OKL_API.G_RET_STS_SUCCESS,
6121                px_overall_status               => px_overall_status);
6122 
6123         -- set the transaction record
6124         set_transaction_rec(
6125               p_return_status                 => OKL_API.G_RET_STS_SUCCESS,
6126               p_overall_status                => px_overall_status,
6127               p_tmt_flag                      => 'TMT_CLOSE_BALANCES_YN',
6128               p_tsu_code                      => 'WORKING',
6129               p_ret_val                       => NULL,
6130               px_tcnv_rec                     => px_tcnv_rec);
6131       END IF;
6132       END IF; -- rmunjulu BUYOUT_PROCESS
6133     END IF;
6134 */
6135     x_return_status      := l_return_status;
6136     x_adjv_rec           := lx_adjv_rec;
6137     --x_ajlv_tbl           := lx_ajlv_tbl;
6138     IF (is_debug_procedure_on) THEN
6139        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
6140     END IF;
6141   EXCEPTION
6142     WHEN OKL_API.G_EXCEPTION_ERROR THEN
6143         IF (is_debug_exception_on) THEN
6144              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
6145         END IF;
6146 
6147          IF k_balances_csr%ISOPEN THEN
6148             CLOSE k_balances_csr;
6149          END IF;
6150          IF k_bal_lns_csr%ISOPEN THEN
6151             CLOSE k_bal_lns_csr;
6152          END IF;
6153          IF code_combination_id_csr%ISOPEN THEN
6154             CLOSE code_combination_id_csr;
6155          END IF;
6156 
6157          ROLLBACK TO close_balances;
6158 
6159          x_return_status := OKL_API.G_RET_STS_ERROR;
6160          -- store the highest degree of error
6161          set_overall_status(
6162                p_return_status                 => x_return_status,
6163                px_overall_status               => px_overall_status);
6164 
6165          -- set the transaction record
6166          set_transaction_rec(
6167                p_return_status                 => x_return_status,
6168                p_overall_status                => px_overall_status,
6169                p_tmt_flag                      => 'TMT_CLOSE_BALANCES_YN',
6170                p_tsu_code                      => 'ERROR',
6171                px_tcnv_rec                     => px_tcnv_rec);
6172 
6173         -- Set the tmt_generic_flag3_yn
6174         set_transaction_rec(
6175                  p_return_status    => x_return_status,
6176                  p_overall_status   => px_overall_status,
6177                  p_tmt_flag        => 'TMT_GENERIC_FLAG3_YN',
6178                  p_tsu_code        => 'ERROR',
6179                  px_tcnv_rec       => px_tcnv_rec);
6180 
6181     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6182         IF (is_debug_exception_on) THEN
6183              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
6184         END IF;
6185 
6186          IF k_balances_csr%ISOPEN THEN
6187             CLOSE k_balances_csr;
6188          END IF;
6189          IF k_bal_lns_csr%ISOPEN THEN
6190             CLOSE k_bal_lns_csr;
6191          END IF;
6192          IF code_combination_id_csr%ISOPEN THEN
6193             CLOSE code_combination_id_csr;
6194          END IF;
6195 
6196          ROLLBACK TO close_balances;
6197 
6198          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6199          -- store the highest degree of error
6200          set_overall_status(
6201                p_return_status                 => x_return_status,
6202                px_overall_status               => px_overall_status);
6203 
6204          -- set the transaction record
6205          set_transaction_rec(
6206                p_return_status                 => x_return_status,
6207                p_overall_status                => px_overall_status,
6208                p_tmt_flag                      => 'TMT_CLOSE_BALANCES_YN',
6209                p_tsu_code                      => 'ERROR',
6210                px_tcnv_rec                     => px_tcnv_rec);
6211 
6212         -- Set the tmt_generic_flag3_yn
6213         set_transaction_rec(
6214                  p_return_status    => x_return_status,
6215                  p_overall_status   => px_overall_status,
6216                  p_tmt_flag        => 'TMT_GENERIC_FLAG3_YN',
6217                  p_tsu_code        => 'ERROR',
6218                  px_tcnv_rec       => px_tcnv_rec);
6219 
6220     WHEN OTHERS THEN
6221         IF (is_debug_exception_on) THEN
6222            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
6223         END IF;
6224 
6225          IF k_balances_csr%ISOPEN THEN
6226             CLOSE k_balances_csr;
6227          END IF;
6228          IF k_bal_lns_csr%ISOPEN THEN
6229             CLOSE k_bal_lns_csr;
6230          END IF;
6231          IF code_combination_id_csr%ISOPEN THEN
6232             CLOSE code_combination_id_csr;
6233          END IF;
6234 
6235          ROLLBACK TO close_balances;
6236 
6237          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6238          -- store the highest degree of error
6239          set_overall_status(
6240                p_return_status                 => x_return_status,
6241                px_overall_status               => px_overall_status);
6242 
6243          -- set the transaction record
6244          set_transaction_rec(
6245                p_return_status                 => x_return_status,
6246                p_overall_status                => px_overall_status,
6247                p_tmt_flag                      => 'TMT_CLOSE_BALANCES_YN',
6248                p_tsu_code                      => 'ERROR',
6249                px_tcnv_rec                     => px_tcnv_rec);
6250 
6251         -- Set the tmt_generic_flag3_yn
6252         set_transaction_rec(
6253                  p_return_status    => x_return_status,
6254                  p_overall_status   => px_overall_status,
6255                  p_tmt_flag        => 'TMT_GENERIC_FLAG3_YN',
6256                  p_tsu_code        => 'ERROR',
6257                  px_tcnv_rec       => px_tcnv_rec);
6258 
6259          -- Set the oracle error message
6260          OKL_API.set_message(
6261                          p_app_name      => OKC_API.G_APP_NAME,
6262                          p_msg_name      => g_unexpected_error,
6263                          p_token1        => g_sqlcode_token,
6264                          p_token1_value  => SQLCODE,
6265                          p_token2        => g_sqlerrm_token,
6266                          p_token2_value  => SQLERRM);
6267 
6268   END process_close_balances;
6269 
6270 
6271 
6272   -- Start of comments
6273   --
6274   -- Procedure Name : process_accounting_entries
6275   -- Desciption     : Calls the Accounting engine to do the accounting entries
6276   -- Business Rules :
6277   -- Parameters     :
6278   -- Version     : 1.0
6279   -- History        : RMUNJULU 23-DEC-02 2726739 Added code for Multi-Currency settings
6280   --                : RMUNJULU Bug # 3023206 27-JUN-03 Increment the line number before the exit
6281   --                  to avoid duplicate line numbers when creating accnting transaction lines
6282   --                : RMUNJULU Bug # 2902876 23-JUL-03 Added code to pass valid GL date to GET_TEMPLATE_INFO
6283   --                : RMUNJULU Bug # 3097068 20-AUG-03 Added code to pass valid GL date to REVERSE LOSS PROVISIONS
6284   --                : RMUNJULU Bug # 3148215 19-SEP-03 Added code to pass valid GL date to CATCHUP OF ACCRUALS
6285   --
6286   --                : SMODUGA Bug # 3061772 21-OCT-03 Added call to process_discount_subsidy
6287   --                : MDOKAL  Bug # 3061765 21-OCT-03 Added IDC acceleration for Financed Fee's
6288   --                : SMODUGA Bug # 3061772 22-OCT-03 Moved call to process_discount_subsidy to end before success messages.
6289   --                : MDOKAL  Bug # 3061765 22-OCT-03 Added date validation for accruals acceleration
6290   --                : RMUNJULU 28-APR-04 3596626 Added code to set lp_acc_gen_primary_key_tbl
6291   --                : rmunjulu EDAT Added code to pass quote accpt date as accounting date
6292   --                  Also added code for reversal of accruals
6293   --                : rmunjulu TNA Modified call to process_adjustments
6294   --                : rmunjulu EDAT 29-Dec-04 did to_char to convert to right format
6295   --                : rmunjulu Bug 4162862 and Bug 4141991 Modify to do accounting on contract end date if no term quote. Also
6296   --                  provide a new parameter p_source which tells where the request is coming from. Act based on this new source
6297   --                : pagarg 01-Mar-05 Bug 4190887 Modified to do accounting for
6298   --                  each line, inorder to set KLE_ID in OKL_TXL_CNTRCT_LNS table
6299   --                : rmunjulu 4416094 pass interest income sty id for loans when doing acceleration of income
6300   --                : rmunjulu 4507662 pass proper date for reverse from when calling reversal of accruals
6301   --                : rmunjulu 4424713 check for product before getting stream type ids
6302   --                : rmunjulu LOANS_ENHANCEMENTS refund excess loan payments
6303   --                : akrangan 5514059 During termination, do accounting for asset, fees and service lines.
6304   --                : rbruno 7248153 FP:ACCELERATED IDC EXPENSE IS NOT CORRECT FOR TERMINATED CONTRACTS
6305   -- End of comments
6306 PROCEDURE process_accounting_entries(p_api_version     IN NUMBER,
6307                                      p_init_msg_list   IN VARCHAR2,
6308                                      x_return_status   OUT NOCOPY VARCHAR2,
6309                                      x_msg_count       OUT NOCOPY NUMBER,
6310                                      x_msg_data        OUT NOCOPY VARCHAR2,
6311                                      p_term_rec        IN term_rec_type,
6312                                      px_overall_status IN OUT NOCOPY VARCHAR2,
6313                                      px_tcnv_rec       IN OUT NOCOPY tcnv_rec_type,
6314                                      p_sys_date        IN DATE,
6315                                      p_klev_tbl        IN klev_tbl_type, -- pagarg 4190887 Added
6316                                      p_trn_already_set IN VARCHAR2,
6317                                      p_source          IN VARCHAR2 DEFAULT NULL) IS
6318   -- rmunjulu Bug 4141991
6319 
6320   -- Cursor to get the product of the contract
6321   -- rmunjulu Bug 4162862 Modified to get contract end date
6322   CURSOR prod_id_csr(p_khr_id IN NUMBER) IS
6323     SELECT khr.pdt_id,
6324            chr.end_date, -- rmunjulu Bug 4162862
6325            khr.deal_type, -- rmunjulu bug 4424713
6326            chr.scs_code -- rmunjulu 4622198
6327            ,chr.org_id  --akrangan added for sla ae uptake cr
6328            ,khr.multi_gaap_yn  --MGAAP 7263041
6329            ,pdt.reporting_pdt_id  --MGAAP 7263041
6330     FROM   okl_k_headers_v khr,
6331            okc_k_headers_b chr, -- rmunjulu Bug 4162862
6332            okl_products pdt
6333     WHERE  khr.id = p_khr_id AND khr.id = chr.id -- rmunjulu Bug 4162862
6334     AND    khr.pdt_id = pdt.ID;  -- MGAAP 7263041
6335 
6336   -- Get the product type
6337   CURSOR l_product_type_csr(p_pdt_id IN NUMBER) IS
6338     SELECT description FROM okl_products_v WHERE id = p_pdt_id;
6339 
6340   l_return_status            VARCHAR2(1) := okl_api.g_ret_sts_success;
6341   l_api_name                 VARCHAR2(30) := 'process_accounting_entries';
6342   l_pdt_id                   NUMBER := 0;
6343 
6344   -- MGAAP 7263041 start
6345   l_reporting_pdt_id      NUMBER                                     := 0;
6346   l_multi_gaap_yn      okl_k_headers.MULTI_GAAP_YN%TYPE              := null;
6347   l_valid_gl_date_rep     DATE;
6348   l_sob_id_rep            NUMBER;
6349   -- MGAAP 7263041 end
6350 
6351   l_try_id                   NUMBER;
6352   lp_tmpl_identify_rec       okl_account_dist_pub.tmpl_identify_rec_type;
6353   lp_dist_info_rec           okl_account_dist_pub.dist_info_rec_type;
6354   lp_ctxt_val_tbl            okl_account_dist_pub.ctxt_val_tbl_type;
6355   lp_acc_gen_primary_key_tbl okl_account_dist_pub.acc_gen_primary_key;
6356   lx_template_tbl            okl_account_dist_pub.avlv_tbl_type;
6357   lx_amount_tbl              okl_account_dist_pub.amount_tbl_type;
6358   lx_tcnv_tbl                okl_trx_contracts_pub.tcnv_tbl_type;
6359   l_catchup_rec              okl_generate_accruals_pub.accrual_rec_type;
6360   l_lprv_rec                 okl_rev_loss_prov_pub.lprv_rec_type;
6361   l_trans_meaning            VARCHAR2(200);
6362   lp_tclv_rec                okl_trx_contracts_pub.tclv_rec_type;
6363   lx_tclv_rec                okl_trx_contracts_pub.tclv_rec_type;
6364   li_tclv_rec                okl_trx_contracts_pub.tclv_rec_type;
6365   i                          NUMBER;
6366   l_total_amount             NUMBER := 0;
6367   lip_tmpl_identify_rec      okl_account_dist_pub.tmpl_identify_rec_type;
6368   lix_template_tbl           okl_account_dist_pub.avlv_tbl_type;
6369   lip_tcnv_rec               okl_trx_contracts_pub.tcnv_rec_type;
6370   lix_tcnv_rec               okl_trx_contracts_pub.tcnv_rec_type;
6371   l_product_type             VARCHAR2(2000);
6372   l_line_number              NUMBER := 1;
6373 
6374   -- RMUNJULU 23-DEC-02 2726739 Added variables
6375   l_functional_currency_code VARCHAR2(15);
6376   l_contract_currency_code   VARCHAR2(15);
6377   l_currency_conversion_type VARCHAR2(30);
6378   l_currency_conversion_rate NUMBER;
6379   l_currency_conversion_date DATE;
6380   l_converted_amount         NUMBER;
6381 
6382   -- Since we do not use the amount or converted amount
6383   -- set a hardcoded value for the amount (and pass to to
6384   -- OKL_ACCOUNTING_UTIL.convert_to_functional_currency and get back
6385   -- conversion values )
6386   l_hard_coded_amount NUMBER := 100;
6387 
6388   -- Bug 2902876
6389   l_valid_gl_date DATE;
6390 
6391   -- SMODUGA added variable for userdefined streams 3925469
6392   lx_sty_id             NUMBER;
6393   lx_sty_id_rep         NUMBER; -- MGAAP 7263041
6394   l_trx_number OKL_TRX_CONTRACTS.TRX_NUMBER%TYPE;  -- MGAAP 7263041
6395   lx_pretax_sty_id      NUMBER;
6396   lx_rentaccrual_sty_id NUMBER;
6397 
6398   -- MDOKAL Bug 3061765
6399   -- Cursor to get amoritized expense streams for IDC acceleration
6400   -- SMODUGA 11-Oct-04 Bug 3925469
6401   -- Modified cursor by passing sty_id based on the purspose and
6402   -- removed reference to stream type view.
6403   CURSOR idc_accel_csr(p_khr_id IN NUMBER, p_sty_id IN NUMBER,p_kle_id IN Number) IS
6404     SELECT okl_line.fee_type fee_type
6405     FROM   okl_k_lines       okl_line,
6406            okc_k_lines_b     okc_line,
6407            okc_line_styles_b lse,
6408            okl_streams       stm
6409     WHERE  okl_line.id     = okc_line.id AND
6410            okc_line.lse_id = lse.id AND lse.lty_code = 'FEE' AND
6411            okc_line.chr_id = p_khr_id AND
6412            stm.khr_id      = okc_line.chr_id AND
6413            stm.kle_id      = okc_line.id AND
6414            stm.sty_id      = p_sty_id AND
6415            stm.active_yn   = 'Y' AND
6416            stm.say_code    = 'CURR' AND
6417            okc_line.id     = P_kle_id;   -- Parameter added by ansethur for bug#6156337
6418 
6419 -- ansethur for bug#6156337 08-aug-07 start
6420         CURSOR primary_sty_id_csr (p_khr_id IN NUMBER) IS
6421         SELECT sty.id Primary_sty_id, sty.code,okc_line.id kle_id
6422           FROM okc_k_lines_b okc_line,
6423                okc_line_styles_b lse,
6424                okc_k_items item,
6425                okl_strm_type_b sty
6426          WHERE okc_line.dnz_chr_id = p_khr_id
6427            AND okc_line.lse_id = lse.id
6428            AND lse.lty_code = 'FEE'
6429            AND item.cle_id = okc_line.id
6430            AND item.JTOT_OBJECT1_CODE = 'OKL_STRMTYP'
6431            AND item.OBJECT1_ID2 = '#'
6432            AND item.OBJECT1_ID1 = sty.id
6433            AND sty.STREAM_TYPE_PURPOSE = 'EXPENSE';
6434 -- ansethur for bug#6156337 08-aug-07 end
6435 
6436   -- MDOKAL Bug 3061765
6437   -- Cursor to get accelerate till date from contract income stream
6438   -- SMODUGA 11-Oct-04 Bug 3925469
6439   -- Modified cursor by passing sty_id based on the purspose and
6440   -- removed reference to stream type view.
6441 
6442   -- Bug#7248153 - rbruno - commented - Start
6443    /*
6444   -- -- rmunjulu bug 4416094
6445   CURSOR get_accel_till_csr(p_khr_id IN NUMBER, p_pretax_sty_id IN NUMBER, p_rentaccrual_sty_id IN NUMBER, p_interestincome_sty_id IN NUMBER) IS
6446     SELECT MAX(stream_element_date)
6447     FROM   okl_strm_elements_v sel,
6448            okl_streams         stm
6449     WHERE  sel.stm_id = stm.id AND
6450            stm.sty_id IN (p_pretax_sty_id, p_rentaccrual_sty_id,
6451             p_interestincome_sty_id) AND stm.khr_id = p_khr_id;
6452             */
6453    -- Bug#7248153 - rbruno - commented - End
6454 
6455    -- Bug#7248153 - rbruno - changed - Start
6456 
6457    CURSOR get_accel_till_csr(p_khr_id IN NUMBER, p_sty_id IN NUMBER) IS
6458     SELECT MAX(stream_element_date)
6459     FROM   okl_strm_elements_v sel,
6460            okl_streams         stm
6461     WHERE  sel.stm_id = stm.id AND
6462            stm.sty_id = p_sty_id AND stm.khr_id = p_khr_id;
6463 
6464   -- Bug#7248153 - rbruno - changed - End
6465 
6466   -- MDOKAL Bug 3061765
6467   -- data structure used for calling accruals acceleration
6468   l_acceleration_rec okl_generate_accruals_pvt.acceleration_rec_type;
6469 
6470   -- MDOKAL Bug 3061765
6471   -- new parameter declarations
6472   l_accelerate_till_date DATE;
6473 
6474   -- rmunjulu EDAT
6475   l_quote_accpt_date      DATE;
6476   l_quote_eff_date        DATE;
6477   l_empty_tcn_type        VARCHAR2(30);
6478   l_empty_reverse_to_date DATE;
6479 
6480   -- rmunjulu 4141991 Cursor to get the product of the contract
6481   CURSOR get_k_sts_csr(p_khr_id IN NUMBER) IS
6482     SELECT chr.sts_code
6483     FROM   okc_k_headers_b chr
6484     WHERE  chr.id = p_khr_id;
6485 
6486   -- rmunjulu 4141991
6487   l_k_sts VARCHAR2(300);
6488 
6489   -- rmunjulu Bug 4162862
6490   l_k_end_date DATE;
6491 
6492   -- pagarg 01-Mar-05 Bug 4190887: counter to loop through asset table
6493   asset_counter NUMBER;
6494 
6495   -- rmunjulu bug 4416094
6496   lx_interestincome_sty_id NUMBER;
6497 
6498   -- rmunjulu bug 4507662
6499   l_reverse_from DATE;
6500 
6501   -- rmunjulu 4424713
6502   l_deal_type VARCHAR2(150);
6503 
6504   -- rmunjulu 4622198
6505   l_scs_code       okc_k_headers_b.scs_code%TYPE;
6506   l_fact_synd_code fnd_lookups.lookup_code%TYPE;
6507   l_inv_acct_code  okc_rules_b.rule_information1%TYPE;
6508 
6509   --rmunjulu 4769094
6510   CURSOR check_accrual_previous_csr IS
6511     SELECT nvl(chk_accrual_previous_mnth_yn, 'N')
6512     FROM   okl_system_params;
6513 
6514   --rmunjulu 4769094
6515   l_accrual_previous_mnth_yn VARCHAR2(3);
6516   --akrangan Bug 5514059 start
6517   -- Cursor to get asset, fee and service lines in a contract.
6518   CURSOR k_asst_fee_srvc_lns_csr(p_khr_id IN NUMBER) IS
6519     SELECT oklv.id   kle_id,
6520            oklv.NAME asset_name
6521     FROM   okc_k_lines_v     oklv,
6522            okc_line_styles_v olsv,
6523            okc_k_headers_v   khr
6524     WHERE  oklv.chr_id = p_khr_id AND oklv.lse_id = olsv.id AND
6525            olsv.lty_code IN
6526            ('FREE_FORM1', 'FEE', 'SOLD_SERVICE') AND
6527            oklv.chr_id = khr.id AND oklv.sts_code = khr.sts_code;
6528 
6529   k_asst_fee_srvc_lns_rec k_asst_fee_srvc_lns_csr%ROWTYPE;
6530   --akrangan Bug 5514059 end
6531   --akrangan sla single accounting call to ae uptake starts
6532   l_org_id                   NUMBER(15);
6533   --txl contracts specific tbl types
6534   l_tclv_tbl  okl_trx_contracts_pub.tclv_tbl_type;
6535   lx_tclv_tbl okl_trx_contracts_pub.tclv_tbl_type;
6536   --ae new table types declaration
6537   l_tmpl_identify_tbl okl_account_dist_pvt.tmpl_identify_tbl_type;
6538   l_dist_info_tbl     okl_account_dist_pvt.dist_info_tbl_type;
6539   l_ctxt_tbl          okl_account_dist_pvt.ctxt_tbl_type;
6540   l_template_out_tbl  okl_account_dist_pvt.avlv_out_tbl_type;
6541   l_amount_out_tbl    okl_account_dist_pvt.amount_out_tbl_type;
6542   l_acc_gen_tbl       okl_account_dist_pvt.acc_gen_tbl_type;
6543   l_tcn_id            NUMBER;
6544 
6545   --hdr dff fields cursor
6546   --this cursor is to populate the
6547   -- desc flex fields columns in okl_trx_contracts
6548   CURSOR trx_contracts_dff_csr(p_khr_id IN NUMBER) IS
6549     SELECT attribute_category,
6550            attribute1,
6551            attribute2,
6552            attribute3,
6553            attribute4,
6554            attribute5,
6555            attribute6,
6556            attribute7,
6557            attribute8,
6558            attribute9,
6559            attribute10,
6560            attribute11,
6561            attribute12,
6562            attribute13,
6563            attribute14,
6564            attribute15
6565     FROM   okl_k_headers okl
6566     WHERE  okl.id = p_khr_id;
6567   --line dff fields cursor
6568   --this cursor is to populate the
6569   -- desc flex fields columns in okl_txl_xontract_lines_b
6570   CURSOR txl_contracts_dff_csr(p_kle_id IN NUMBER) IS
6571     SELECT attribute_category,
6572            attribute1,
6573            attribute2,
6574            attribute3,
6575            attribute4,
6576            attribute5,
6577            attribute6,
6578            attribute7,
6579            attribute8,
6580            attribute9,
6581            attribute10,
6582            attribute11,
6583            attribute12,
6584            attribute13,
6585            attribute14,
6586            attribute15
6587     FROM   okl_k_lines okl
6588     WHERE  okl.id = p_kle_id;
6589   --record for storing okl_k_lines dffs and linked assets cle_id
6590   TYPE dff_rec_type IS RECORD(
6591     attribute_category okl_k_lines.attribute_category%TYPE,
6592     attribute1         okl_k_lines.attribute1%TYPE,
6593     attribute2         okl_k_lines.attribute2%TYPE,
6594     attribute3         okl_k_lines.attribute3%TYPE,
6595     attribute4         okl_k_lines.attribute4%TYPE,
6596     attribute5         okl_k_lines.attribute5%TYPE,
6597     attribute6         okl_k_lines.attribute6%TYPE,
6598     attribute7         okl_k_lines.attribute7%TYPE,
6599     attribute8         okl_k_lines.attribute8%TYPE,
6600     attribute9         okl_k_lines.attribute9%TYPE,
6601     attribute10        okl_k_lines.attribute10%TYPE,
6602     attribute11        okl_k_lines.attribute11%TYPE,
6603     attribute12        okl_k_lines.attribute12%TYPE,
6604     attribute13        okl_k_lines.attribute13%TYPE,
6605     attribute14        okl_k_lines.attribute14%TYPE,
6606     attribute15        okl_k_lines.attribute15%TYPE);
6607   txl_contracts_dff_rec dff_rec_type;
6608   --product name and tax owner
6609   CURSOR product_name_csr(p_pdt_id IN NUMBER) IS
6610     SELECT NAME,
6611            tax_owner
6612     FROM   okl_product_parameters_v
6613     WHERE  id = p_pdt_id;
6614   l_currency_code            okl_trx_contracts.currency_code%TYPE;
6615   --loop variables
6616   j NUMBER;
6617   k NUMBER;
6618   l NUMBER;
6619   m NUMBER;
6620   --akrangan sla single accounting call to ae uptake ends
6621   -- asawanka added for debug feature start
6622     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_accounting_entries';
6623     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
6624     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
6625     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
6626     -- asawanka added for debug feature end
6627 
6628     -- asahoo bug 13259692
6629     l_acc_period_name VARCHAR2(30);
6630     l_acc_period_start_date DATE;
6631     l_acc_period_end_date DATE;
6632 
6633 BEGIN
6634      IF (is_debug_procedure_on) THEN
6635        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
6636      END IF;
6637 
6638   -- Start savepoint for this block
6639   SAVEPOINT accounting_entries;
6640 
6641   -- get the product id
6642   OPEN prod_id_csr(p_term_rec.p_contract_id);
6643   FETCH prod_id_csr
6644     INTO l_pdt_id, l_k_end_date, l_deal_type, l_scs_code,l_org_id, -- rmunjulu Bug 4162862 Added -- rmunjulu 4424713 -- rmunjulu 4622198
6645          l_multi_gaap_yn, l_reporting_pdt_id;  -- MGAAP 7263041
6646   CLOSE prod_id_csr;
6647 
6648   -- ********************
6649   -- CHECK PRODUCT ID
6650   -- ********************
6651 
6652   -- raise error if no pdt_id
6653   IF l_pdt_id IS NULL OR l_pdt_id = 0
6654   THEN
6655 
6656     -- Error: Unable to create accounting entries because of a missing
6657     -- Product Type for the contract CONTRACT_NUMBER.
6658     okl_api.set_message(p_app_name     => g_app_name,
6659                         p_msg_name     => 'OKL_AM_PRODUCT_ID_ERROR',
6660                         p_token1       => 'CONTRACT_NUMBER',
6661                         p_token1_value => p_term_rec.p_contract_number);
6662 
6663     RAISE okl_api.g_exception_error;
6664 
6665   END IF;
6666   IF (is_debug_statement_on) THEN
6667       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,' l_pdt_id = '||l_pdt_id);
6668   END IF;
6669   -- get the product type
6670   OPEN l_product_type_csr(l_pdt_id);
6671   FETCH l_product_type_csr
6672     INTO l_product_type;
6673   CLOSE l_product_type_csr;
6674 
6675   -- If accounting entries needed
6676   IF (p_trn_already_set = g_yes AND
6677      nvl(px_tcnv_rec.tmt_accounting_entries_yn, '?') <> g_yes) OR
6678      (p_trn_already_set = g_no)
6679   THEN
6680 
6681     -- RMUNJULU 3018641 Step Message
6682     -- Step : Accounting Entries
6683     okl_api.set_message(p_app_name => g_app_name,
6684                         p_msg_name => 'OKL_AM_STEP_ACT');
6685 
6686     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
6687 
6688     -- rmunjulu EDAT
6689     -- If quote exists then accnting date is quote accept date else sysdate
6690     IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y'
6691     THEN
6692 
6693       l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
6694       l_quote_eff_date   := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
6695 
6696     ELSE
6697 
6698       l_quote_accpt_date := l_k_end_date; -- rmunjulu Bug 4162862 Changed to contract end date
6699       l_quote_eff_date   := l_k_end_date; -- rmunjulu Bug 4162862 Changed to contract end date
6700 
6701     END IF;
6702 
6703     -- rmunjulu EDAT
6704     -- set the additional parameters with contract_id, quote_id and transaction_date
6705     -- to be passed to formula engine
6706 
6707     lp_ctxt_val_tbl(1).NAME := 'contract_id';
6708     lp_ctxt_val_tbl(1).VALUE := p_term_rec.p_contract_id;
6709 
6710     lp_ctxt_val_tbl(2).NAME := 'quote_id';
6711     lp_ctxt_val_tbl(2).VALUE := p_term_rec.p_quote_id;
6712 
6713     lp_ctxt_val_tbl(3).NAME := 'transaction_date';
6714     lp_ctxt_val_tbl(3).VALUE := to_char(l_quote_accpt_date,
6715                                         'MM/DD/YYYY'); -- rmunjulu EDAT 29-Dec-04 did to_char to convert to right format
6716 
6717     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
6718 
6719     -- ********************
6720     -- GET TEMPLATES
6721     -- ********************
6722 
6723     -- Get the meaning of lookup
6724     l_trans_meaning := okl_am_util_pvt.get_lookup_meaning(p_lookup_type => 'OKL_ACCOUNTING_EVENT_TYPE',
6725                                                           p_lookup_code => 'TERMINATION',
6726                                                           p_validate_yn => 'Y');
6727 
6728     -- Set the tmpl_identify_rec in parameter to get accounting templates for the product
6729     lip_tmpl_identify_rec.product_id          := l_pdt_id;
6730     lip_tmpl_identify_rec.transaction_type_id := px_tcnv_rec.try_id;
6731     lip_tmpl_identify_rec.memo_yn             := g_no;
6732     lip_tmpl_identify_rec.prior_year_yn       := g_no;
6733 
6734     -- Bug 2902876 Added to get the valid GL date
6735     l_valid_gl_date := okl_accounting_util.get_valid_gl_date(p_gl_date => l_quote_accpt_date); -- rmunjulu EDAT
6736 
6737     IF (is_debug_statement_on) THEN
6738         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling okl_securitization_pvt.check_khr_ia_associated');
6739     END IF;
6740     -- rmunjulu 4622198 SPECIAL_ACCNT Get special accounting details
6741     okl_securitization_pvt.check_khr_ia_associated(p_api_version    => p_api_version,
6742                                                    p_init_msg_list  => okl_api.g_false,
6743                                                    x_return_status  => l_return_status,
6744                                                    x_msg_count      => x_msg_count,
6745                                                    x_msg_data       => x_msg_data,
6746                                                    p_khr_id         => p_term_rec.p_contract_id,
6747                                                    p_scs_code       => l_scs_code,
6748                                                    p_trx_date       => l_quote_accpt_date,
6749                                                    x_fact_synd_code => l_fact_synd_code,
6750                                                    x_inv_acct_code  => l_inv_acct_code);
6751     IF (is_debug_statement_on) THEN
6752         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called okl_securitization_pvt.check_khr_ia_associated l_return_status = '||l_return_status);
6753     END IF;
6754 
6755     IF (l_return_status = okl_api.g_ret_sts_unexp_error)
6756     THEN
6757       RAISE okl_api.g_exception_unexpected_error;
6758     ELSIF (l_return_status = okl_api.g_ret_sts_error)
6759     THEN
6760       RAISE okl_api.g_exception_error;
6761     END IF;
6762 
6763     -- rmunjulu 4622198 SPECIAL_ACCNT set the special accounting parameters
6764     lip_tmpl_identify_rec.factoring_synd_flag := l_fact_synd_code;
6765     lip_tmpl_identify_rec.investor_code       := l_inv_acct_code;
6766 
6767     IF (is_debug_statement_on) THEN
6768         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling okl_account_dist_pub.get_template_info');
6769     END IF;
6770     -- Get the accounting templates
6771     okl_account_dist_pub.get_template_info(p_api_version       => p_api_version,
6772                                            p_init_msg_list     => okl_api.g_false,
6773                                            x_return_status     => l_return_status,
6774                                            x_msg_count         => x_msg_count,
6775                                            x_msg_data          => x_msg_data,
6776                                            p_tmpl_identify_rec => lip_tmpl_identify_rec,
6777                                            x_template_tbl      => lix_template_tbl,
6778                                            p_validity_date     => l_valid_gl_date); -- Bug 2902876 Added to pass valid GL date
6779     IF (is_debug_statement_on) THEN
6780         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called okl_account_dist_pub.get_template_info l_return_status = '||l_return_status);
6781     END IF;
6782 
6783     IF l_return_status <> okl_api.g_ret_sts_success
6784     THEN
6785 
6786       -- No accounting templates found matching the transaction type TRX_TYPE
6787       -- and product  PRODUCT.
6788       okl_api.set_message(p_app_name     => g_app_name,
6789                           p_msg_name     => 'OKL_AM_NO_ACC_TEMPLATES',
6790                           p_token1       => 'TRX_TYPE',
6791                           p_token1_value => l_trans_meaning,
6792                           p_token2       => 'PRODUCT',
6793                           p_token2_value => l_product_type);
6794 
6795     END IF;
6796 
6797     IF (l_return_status = okl_api.g_ret_sts_unexp_error)
6798     THEN
6799       RAISE okl_api.g_exception_unexpected_error;
6800     ELSIF (l_return_status = okl_api.g_ret_sts_error)
6801     THEN
6802       RAISE okl_api.g_exception_error;
6803     END IF;
6804     l_functional_currency_code := okl_am_util_pvt.get_functional_currency();
6805     --akrangan Bug 6147049 code fix start
6806     --call functional currency conversion only
6807     --if functional currency and contract currency are not same
6808     l_currency_code := okl_am_util_pvt.get_chr_currency(p_term_rec.p_contract_id);
6809     IF l_functional_currency_code IS NOT NULL AND
6810        l_functional_currency_code <>  l_currency_code
6811     THEN
6812     --akrangan Bug 6147049 code fix end
6813     -- Get the currency conversion details from ACCOUNTING_Util
6814     IF (is_debug_statement_on) THEN
6815         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling okl_accounting_util.convert_to_functional_currency');
6816     END IF;
6817     okl_accounting_util.convert_to_functional_currency(
6818                            p_khr_id                   => p_term_rec.p_contract_id,
6819                            p_to_currency              => l_functional_currency_code,
6820                            p_transaction_date         => l_quote_accpt_date, -- rmunjulu EDAT
6821                            p_amount                   => l_hard_coded_amount,
6822                            x_return_status            => l_return_status,
6823                            x_contract_currency        => l_contract_currency_code,
6824                            x_currency_conversion_type => l_currency_conversion_type,
6825                            x_currency_conversion_rate => l_currency_conversion_rate,
6826                            x_currency_conversion_date => l_currency_conversion_date,
6827                            x_converted_amount         => l_converted_amount
6828                                                       );
6829     IF (is_debug_statement_on) THEN
6830         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called okl_accounting_util.convert_to_functional_currency l_return_status = '||l_return_status);
6831     END IF;
6832     -- If error from OKL_ACCOUNTING_UTIL
6833     IF l_return_status <> okl_api.g_ret_sts_success
6834     THEN
6835 
6836       -- Error occurred when creating accounting entries for
6837       -- transaction TRX_TYPE.
6838       okl_api.set_message(p_app_name     => g_app_name,
6839                           p_msg_name     => 'OKL_AM_ERR_ACC_ENT',
6840                           p_token1       => 'TRX_TYPE',
6841                           p_token1_value => l_trans_meaning);
6842 
6843     END IF;
6844     --akrangan Bug 6147049 code fix start
6845     END IF;
6846     --akrangan Bug 6147049 code fix end
6847     -- If no templates present
6848     IF lix_template_tbl.COUNT = 0
6849     THEN
6850 
6851       -- No accounting templates found matching the transaction type TRX_TYPE
6852       -- and product  PRODUCT.
6853       okl_api.set_message(p_app_name     => g_app_name,
6854                           p_msg_name     => 'OKL_AM_NO_ACC_TEMPLATES',
6855                           p_token1       => 'TRX_TYPE',
6856                           p_token1_value => l_trans_meaning,
6857                           p_token2       => 'PRODUCT',
6858                           p_token2_value => l_product_type);
6859 
6860       RAISE okl_api.g_exception_error;
6861 
6862     END IF;
6863 
6864     -- *****************************
6865     -- CREATE TXL_CNTRCT LINES
6866     -- ****************************
6867     -- currency operations related variables assigned
6868     l_currency_code            := NVL(l_contract_currency_code,l_currency_code);
6869     j := 1 ;
6870    --looping thru the templates to set line records and template identify tbl
6871     i := lix_template_tbl.FIRST; -- at this point we know that there are some templates
6872     LOOP
6873       -- Loop thru templates
6874       FOR k_asst_fee_srvc_lns_rec IN k_asst_fee_srvc_lns_csr(p_term_rec.p_contract_id)
6875       LOOP
6876         -- set the TXL_CNTRCT Line details for template
6877         l_tclv_tbl(j).line_number := l_line_number;
6878         l_tclv_tbl(j).khr_id := p_term_rec.p_contract_id;
6879         l_tclv_tbl(j).tcn_id := px_tcnv_rec.id;
6880         l_tclv_tbl(j).sty_id := lix_template_tbl(i).sty_id;
6881         l_tclv_tbl(j).tcl_type := 'TMT';
6882         l_tclv_tbl(j).currency_code := l_currency_code;
6883         l_tclv_tbl(j).kle_id := k_asst_fee_srvc_lns_rec.kle_id;
6884 	l_tclv_tbl(j).org_id := l_org_id;
6885         FOR txl_contracts_dff_rec IN txl_contracts_dff_csr( k_asst_fee_srvc_lns_rec.kle_id)
6886         LOOP
6887             --set dffs
6888             l_tclv_tbl(j).attribute_category := txl_contracts_dff_rec.attribute_category;
6889             l_tclv_tbl(j).attribute1 := txl_contracts_dff_rec.attribute1;
6890             l_tclv_tbl(j).attribute2 := txl_contracts_dff_rec.attribute2;
6891             l_tclv_tbl(j).attribute3 := txl_contracts_dff_rec.attribute3;
6892             l_tclv_tbl(j).attribute4 := txl_contracts_dff_rec.attribute4;
6893             l_tclv_tbl(j).attribute5 := txl_contracts_dff_rec.attribute5;
6894             l_tclv_tbl(j).attribute6 := txl_contracts_dff_rec.attribute6;
6895             l_tclv_tbl(j).attribute7 := txl_contracts_dff_rec.attribute7;
6896             l_tclv_tbl(j).attribute8 := txl_contracts_dff_rec.attribute8;
6897             l_tclv_tbl(j).attribute9 := txl_contracts_dff_rec.attribute9;
6898             l_tclv_tbl(j).attribute10 := txl_contracts_dff_rec.attribute10;
6899             l_tclv_tbl(j).attribute11 := txl_contracts_dff_rec.attribute11;
6900             l_tclv_tbl(j).attribute12 := txl_contracts_dff_rec.attribute12;
6901             l_tclv_tbl(j).attribute13 := txl_contracts_dff_rec.attribute13;
6902             l_tclv_tbl(j).attribute14 := txl_contracts_dff_rec.attribute14;
6903             l_tclv_tbl(j).attribute15 := txl_contracts_dff_rec.attribute15;
6904         END LOOP;
6905 
6906       -- This will calculate the amount and generate accounting entries
6907       -- Set the tmpl_identify_tbl in parameter
6908       l_tmpl_identify_tbl(j).product_id := l_pdt_id;
6909       l_tmpl_identify_tbl(j).transaction_type_id := px_tcnv_rec.try_id;
6910       l_tmpl_identify_tbl(j).memo_yn := g_no;
6911       l_tmpl_identify_tbl(j).prior_year_yn := g_no;
6912       l_tmpl_identify_tbl(j).stream_type_id := lix_template_tbl(i).sty_id;
6913       l_tmpl_identify_tbl(j).advance_arrears := lix_template_tbl(i).advance_arrears;
6914       l_tmpl_identify_tbl(j).factoring_synd_flag := lix_template_tbl(i).factoring_synd_flag;
6915       l_tmpl_identify_tbl(j).investor_code := lix_template_tbl(i).inv_code;
6916       l_tmpl_identify_tbl(j).syndication_code := lix_template_tbl(i).syt_code;
6917       l_tmpl_identify_tbl(j).factoring_code := lix_template_tbl(i).fac_code;
6918         --increment looping variable
6919 	j := j + 1;
6920 	--increment line number
6921         l_line_number := l_line_number + 1;
6922        END LOOP;
6923       EXIT WHEN(i = lix_template_tbl.LAST);
6924       i := lix_template_tbl.NEXT(i);
6925     END LOOP;
6926     IF (is_debug_statement_on) THEN
6927         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling okl_trx_contracts_pub.create_trx_cntrct_lines');
6928     END IF;
6929     --create trx contract lines table
6930     okl_trx_contracts_pub.create_trx_cntrct_lines(p_api_version   => p_api_version,
6931                                                   p_init_msg_list => okl_api.g_false,
6932                                                   x_return_status => l_return_status,
6933                                                   x_msg_count     => x_msg_count,
6934                                                   x_msg_data      => x_msg_data,
6935                                                   p_tclv_tbl      => l_tclv_tbl,
6936                                                   x_tclv_tbl      => lx_tclv_tbl);
6937     IF (is_debug_statement_on) THEN
6938         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called okl_trx_contracts_pub.create_trx_cntrct_lines l_return_status = '||l_return_status);
6939     END IF;
6940 
6941     -- If error inserting line then set message
6942     IF l_return_status <> okl_api.g_ret_sts_success
6943     THEN
6944 
6945       -- Error occurred when creating accounting entries for transaction TRX_TYPE.
6946       okl_api.set_message(p_app_name     => g_app_name,
6947                           p_msg_name     => 'OKL_AM_ERR_ACC_ENT',
6948                           p_token1       => 'TRX_TYPE',
6949                           p_token1_value => l_trans_meaning);
6950 
6951     END IF;
6952 
6953     -- Raise exception to rollback to savepoint for this block
6954     IF (l_return_status = okl_api.g_ret_sts_unexp_error)
6955     THEN
6956       RAISE okl_api.g_exception_unexpected_error;
6957     ELSIF (l_return_status = okl_api.g_ret_sts_error)
6958     THEN
6959       RAISE okl_api.g_exception_error;
6960     END IF;
6961     --setting the input table type to the obtained outout table type
6962     l_tclv_tbl := lx_tclv_tbl;
6963 
6964     -- ***************************
6965     -- POPULATE ACC GEN PRIMARY KEY TABLE
6966     -- ******************************
6967     -- added code to set lp_acc_gen_primary_key_tbl
6968     -- for account generator
6969     IF (is_debug_statement_on) THEN
6970         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  okl_acc_call_pvt.okl_populate_acc_gen');
6971     END IF;
6972     okl_acc_call_pvt.okl_populate_acc_gen(p_contract_id      => p_term_rec.p_contract_id,
6973                                           p_contract_line_id => NULL,
6974                                           x_acc_gen_tbl      => lp_acc_gen_primary_key_tbl,
6975                                           x_return_status    => l_return_status);
6976     IF (is_debug_statement_on) THEN
6977         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called okl_acc_call_pvt.okl_populate_acc_gen l_return_status = '||l_return_status);
6978     END IF;
6979     IF l_return_status <> okl_api.g_ret_sts_success
6980     THEN
6981       -- Error occurred when creating accounting entries for transaction TRX_TYPE.
6982       okl_api.set_message(p_app_name     => g_app_name,
6983                           p_msg_name     => 'OKL_AM_ERR_ACC_ENT',
6984                           p_token1       => 'TRX_TYPE',
6985                           p_token1_value => l_trans_meaning);
6986     END IF;
6987     -- Raise exception to rollback to savepoint for this block
6988     IF (l_return_status = okl_api.g_ret_sts_unexp_error)
6989     THEN
6990       RAISE okl_api.g_exception_unexpected_error;
6991     ELSIF (l_return_status = okl_api.g_ret_sts_error)
6992     THEN
6993       RAISE okl_api.g_exception_error;
6994     END IF;
6995 
6996     -- *********************************************
6997     -- Accounting Engine Call
6998     -- *********************************************
6999     --txl contracts loop
7000     IF l_tclv_tbl.COUNT <> 0
7001     THEN
7002       i := l_tclv_tbl.FIRST;
7003       LOOP
7004         --Assigning the account generator table
7005         l_acc_gen_tbl(i).acc_gen_key_tbl := lp_acc_gen_primary_key_tbl;
7006         l_acc_gen_tbl(i).source_id := l_tclv_tbl(i).id;
7007         --populating dist info tbl
7008         l_dist_info_tbl(i).source_id := l_tclv_tbl(i).id;
7009         l_dist_info_tbl(i).source_table := 'OKL_TXL_CNTRCT_LNS';
7010         l_dist_info_tbl(i).accounting_date := l_quote_accpt_date;
7011         l_dist_info_tbl(i).gl_reversal_flag := g_no;
7012         l_dist_info_tbl(i).post_to_gl := g_yes;
7013         l_dist_info_tbl(i).contract_id := l_tclv_tbl(i).khr_id;
7014         l_dist_info_tbl(i).contract_line_id := l_tclv_tbl(i).kle_id;
7015         l_dist_info_tbl(i).currency_code := l_currency_code;
7016         IF ((l_functional_currency_code IS NOT NULL) AND
7017            (l_currency_code <> l_functional_currency_code))
7018         THEN
7019           l_dist_info_tbl(i).currency_conversion_rate := l_currency_conversion_rate;
7020           l_dist_info_tbl(i).currency_conversion_type := l_currency_conversion_type;
7021           l_dist_info_tbl(i).currency_conversion_date := l_currency_conversion_date;
7022         END IF;
7023 	--form context val table
7024 	IF lp_ctxt_val_tbl.COUNT > 0 THEN
7025 	 l_ctxt_tbl(i).ctxt_val_tbl  := lp_ctxt_val_tbl;
7026          l_ctxt_tbl(i).source_id := l_tclv_tbl(i).id;
7027 	END IF;
7028         EXIT WHEN i = l_tclv_tbl.LAST;
7029         i := l_tclv_tbl.NEXT(i);
7030       END LOOP;
7031     END IF;
7032     l_tcn_id := px_tcnv_rec.id;
7033     -- call accounting engine
7034     -- This will calculate the amount and generate accounting entries
7035     IF (is_debug_statement_on) THEN
7036         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  okl_account_dist_pvt.create_accounting_dist');
7037     END IF;
7038     okl_account_dist_pvt.create_accounting_dist(p_api_version             => p_api_version,
7039                                                 p_init_msg_list           => okl_api.g_false,
7040                                                 x_return_status           => l_return_status,
7041                                                 x_msg_count               => x_msg_count,
7042                                                 x_msg_data                => x_msg_data,
7043                                                 p_tmpl_identify_tbl       => l_tmpl_identify_tbl,
7044                                                 p_dist_info_tbl           => l_dist_info_tbl,
7045                                                 p_ctxt_val_tbl            => l_ctxt_tbl,
7046                                                 p_acc_gen_primary_key_tbl => l_acc_gen_tbl,
7047                                                 x_template_tbl            => l_template_out_tbl,
7048                                                 x_amount_tbl              => l_amount_out_tbl,
7049                                                 p_trx_header_id           => l_tcn_id);
7050     IF (is_debug_statement_on) THEN
7051         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_account_dist_pvt.create_accounting_dist l_return_status = '||l_return_status);
7052     END IF;
7053 
7054     IF l_amount_out_tbl.COUNT = 0
7055     THEN
7056       l_return_status := okl_api.g_ret_sts_error;
7057     END IF;
7058 
7059     IF l_return_status <> okl_api.g_ret_sts_success
7060     THEN
7061       -- Error occurred when creating accounting entries for transaction TRX_TYPE.
7062       okl_api.set_message(p_app_name     => g_app_name,
7063                           p_msg_name     => 'OKL_AM_ERR_ACC_ENT',
7064                           p_token1       => 'TRX_TYPE',
7065                           p_token1_value => l_trans_meaning);
7066 
7067     END IF;
7068 
7069     -- Raise exception to rollback to savepoint for this block
7070     IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7071     THEN
7072       RAISE okl_api.g_exception_unexpected_error;
7073     ELSIF (l_return_status = okl_api.g_ret_sts_error)
7074     THEN
7075       RAISE okl_api.g_exception_error;
7076     END IF;
7077 
7078     -- ******************************************************
7079     --   Update Trx Contracts with Header and Line Amounts
7080     -- ******************************************************
7081 
7082     --call the update trx contract api to update amount per stream type
7083     lip_tcnv_rec := px_tcnv_rec;
7084     --set all the necessary attributes of the record type
7085     lip_tcnv_rec.amount          := 0;
7086     lip_tcnv_rec.set_of_books_id := okl_accounting_util.get_set_of_books_id();
7087     --akrangan bug 6147049 fix start
7088     lip_tcnv_rec.currency_conversion_rate := l_currency_conversion_rate;
7089     lip_tcnv_rec.currency_conversion_type := l_currency_conversion_type;
7090     lip_tcnv_rec.currency_conversion_date := l_currency_conversion_date;
7091     --akrangan bug 6147049 fix end
7092     --akrangan bug 6215707 fix start
7093     lip_tcnv_rec.tsu_code := 'PROCESSED';
7094     --akrangan bug 6215707 fix end
7095     --product name and tax owner code
7096     OPEN product_name_csr(l_pdt_id);
7097     FETCH product_name_csr
7098       INTO lip_tcnv_rec.product_name, lip_tcnv_rec.tax_owner_code;
7099     CLOSE product_name_csr;
7100 
7101    --trx contracts hdr dffs
7102     OPEN trx_contracts_dff_csr(p_term_rec.p_contract_id);
7103     FETCH trx_contracts_dff_csr
7104       INTO lip_tcnv_rec.attribute_category, lip_tcnv_rec.attribute1,
7105            lip_tcnv_rec.attribute2, lip_tcnv_rec.attribute3,
7106            lip_tcnv_rec.attribute4, lip_tcnv_rec.attribute5,
7107            lip_tcnv_rec.attribute6, lip_tcnv_rec.attribute7,
7108            lip_tcnv_rec.attribute8, lip_tcnv_rec.attribute9,
7109            lip_tcnv_rec.attribute10, lip_tcnv_rec.attribute11,
7110            lip_tcnv_rec.attribute12, lip_tcnv_rec.attribute13,
7111            lip_tcnv_rec.attribute14, lip_tcnv_rec.attribute15;
7112     CLOSE trx_contracts_dff_csr;
7113 
7114     IF (l_tclv_tbl.COUNT) > 0 AND (l_amount_out_tbl.COUNT > 0)
7115     THEN
7116       k := l_tclv_tbl.FIRST;
7117       m := l_amount_out_tbl.FIRST;
7118       LOOP
7119           l_tclv_tbl(k).amount := 0;
7120           IF l_tclv_tbl(k).id = l_amount_out_tbl(m).source_id
7121           THEN
7122             lx_amount_tbl   := l_amount_out_tbl(m).amount_tbl;
7123             lx_template_tbl := l_template_out_tbl(m).template_tbl;
7124             IF (lx_amount_tbl.COUNT <> 1 OR lx_template_tbl.COUNT <> 1)
7125             THEN
7126               --raise error
7127               l_return_status := okl_api.g_ret_sts_error;
7128               -- Error occurred when creating accounting entries for transaction TRX_TYPE.
7129               okl_api.set_message(p_app_name     => g_app_name,
7130                                   p_msg_name     => 'OKL_AM_ERR_ACC_ENT',
7131                                   p_token1       => 'TRX_TYPE',
7132                                   p_token1_value => l_trans_meaning);
7133 
7134               -- Raise exception to rollback to savepoint for this block
7135               RAISE okl_api.g_exception_error;
7136             ELSE
7137               l := lx_amount_tbl.FIRST;
7138               --update line amount
7139               l_tclv_tbl(k).amount := NVL(lx_amount_tbl(l),0);
7140             END IF;
7141           END IF;
7142 
7143         --update total header amount
7144         lip_tcnv_rec.amount := lip_tcnv_rec.amount + l_tclv_tbl(k)
7145                               .amount;
7146         EXIT WHEN k = l_tclv_tbl.LAST OR m = l_amount_out_tbl.LAST;
7147         k := l_tclv_tbl.NEXT(k);
7148 	m := l_amount_out_tbl.NEXT(m);
7149       END LOOP;
7150     END IF;
7151     --call the api to update trx contracts hdr and lines
7152     IF (is_debug_statement_on) THEN
7153         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  okl_trx_contracts_pub.update_trx_contracts');
7154     END IF;
7155     okl_trx_contracts_pub.update_trx_contracts(p_api_version   => p_api_version,
7156                                                p_init_msg_list => okl_api.g_false,
7157                                                x_return_status => l_return_status,
7158                                                x_msg_count     => x_msg_count,
7159                                                x_msg_data      => x_msg_data,
7160                                                p_tcnv_rec      => lip_tcnv_rec,
7161                                                p_tclv_tbl      => l_tclv_tbl,
7162                                                x_tcnv_rec      => lix_tcnv_rec,
7163                                                x_tclv_tbl      => lx_tclv_tbl);
7164     --handle exception
7165     IF (is_debug_statement_on) THEN
7166         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_trx_contracts_pub.update_trx_contracts l_return_status = '||l_return_status);
7167     END IF;
7168     IF l_return_status <> okl_api.g_ret_sts_success
7169     THEN
7170       -- Error occurred when creating accounting entries for transaction TRX_TYPE.
7171       okl_api.set_message(p_app_name     => g_app_name,
7172                           p_msg_name     => 'OKL_AM_ERR_ACC_ENT',
7173                           p_token1       => 'TRX_TYPE',
7174                           p_token1_value => l_trans_meaning);
7175 
7176     END IF;
7177 
7178     -- Raise exception to rollback to savepoint for this block
7179     IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7180     THEN
7181       RAISE okl_api.g_exception_unexpected_error;
7182     ELSIF (l_return_status = okl_api.g_ret_sts_error)
7183     THEN
7184       RAISE okl_api.g_exception_error;
7185     END IF;
7186 
7187     -- Set the return record
7188     px_tcnv_rec := lix_tcnv_rec;
7189 
7190     -- MGAAP 7628606.SGIYER. Changed input param for p_tcnv_rec
7191     -- from lip_tcnv_rec to lix_tcnv_rec.
7192     OKL_MULTIGAAP_ENGINE_PVT.CREATE_SEC_REP_TRX
7193                            (p_api_version => p_api_version
7194                            ,p_init_msg_list => p_init_msg_list
7195                            ,x_return_status => l_return_status
7196                            ,x_msg_count => x_msg_count
7197                            ,x_msg_data => x_msg_data
7198                            ,P_TCNV_REC => lix_tcnv_rec
7199                            ,P_TCLV_TBL => lx_tclv_tbl
7200                            ,p_ctxt_val_tbl => l_ctxt_tbl
7201                            ,p_acc_gen_primary_key_tbl => lp_acc_gen_primary_key_tbl);
7202 
7203     IF (l_return_status = Okl_Api.G_RET_STS_UNEXP_ERROR) THEN
7204         RAISE Okl_Api.G_EXCEPTION_UNEXPECTED_ERROR;
7205     ELSIF (l_return_status = Okl_Api.G_RET_STS_ERROR) THEN
7206         RAISE Okl_Api.G_EXCEPTION_ERROR;
7207     END IF;
7208 
7209     -- rmunjulu 4141991 Get the contract status
7210     OPEN get_k_sts_csr(p_term_rec.p_contract_id);
7211     FETCH get_k_sts_csr
7212       INTO l_k_sts;
7213     CLOSE get_k_sts_csr;
7214 
7215     -- rmunjulu 4141991 If contract is NOT TURNING TO EVERGREEN then do the rest of these steps
7216     IF nvl(p_source, '*') <> ('EVERGREEN')
7217     THEN
7218 
7219    -- rmunjulu bug 13074256, first do the reversal and then catchup or else reversal was reversing catchup
7220    IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y' THEN -- 13074256 Add condition to do Reversal Of Accruals only if Termination from Quote (Not if Expiration)
7221 
7222       -- rmunjulu 4769094 Based on CHK_ACCRUAL_PREVIOUS_MNTH_YN setup check accruals till quote eff date OR previous month last date
7223       OPEN check_accrual_previous_csr;
7224       FETCH check_accrual_previous_csr
7225         INTO l_accrual_previous_mnth_yn;
7226       CLOSE check_accrual_previous_csr;
7227       -- fix for bug -- 5623356 -- added below condition
7228       -- If quote exists then cancelation date is quote eff from date else sysdate
7229      IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
7230 
7231      -- asahoo bug 13259692 derive the period in which termination is falling first and then
7232      -- derive the current or next period based on Check_Accrual_Till_Previous_Month flag
7233 
7234      okl_accounting_util.get_period_info(l_quote_eff_date,l_acc_period_name, l_acc_period_start_date,l_acc_period_end_date);
7235 
7236      -- asahoo bug 13259692 now that we have derived the GL Period use the GL Period Start Date or End Date to derive the reversal date
7237 
7238       IF nvl(l_accrual_previous_mnth_yn, 'N') = 'N'
7239       THEN
7240         -- rmunjulu 4769094 continue with current reversal date as quote effective date
7241         -- rmunjulu bug 4507662
7242         -- To get first day of the month after the month of the quote effective date to pass it for reversal of accural
7243         --l_reverse_from := trunc(last_day(l_quote_eff_date) + 1);
7244         l_reverse_from := l_acc_period_end_date + 1; -- asahoo 13259692 reverse accruals from next period onwards
7245       ELSE
7246         -- rmunjulu 4769094 new reversal date is quote eff dates month first day
7247         --l_reverse_from := last_day(trunc(l_quote_eff_date,
7248                                          --'MONTH') - 1) + 1; -- NOTE THIS IS A BIT DIFFERENT FROM OTHER SUCH FIXES
7249         l_reverse_from := l_acc_period_start_date;   -- asahoo 13259692 reverse accruals from current period onwards
7250       END IF;
7251       ELSE
7252  	--l_reverse_from := TRUNC(LAST_DAY(l_quote_eff_date) + 1);
7253         l_reverse_from := l_acc_period_end_date + 1; -- asahoo 13259692
7254        END IF;
7255  	       -- fix for bug -- 5623356 --
7256       -- rmunjulu EDAT call reversal of accruals to reverse accruals from quote eff date
7257       IF (is_debug_statement_on) THEN
7258         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling   okl_generate_accruals_pub.reverse_accruals');
7259       END IF;
7260       okl_generate_accruals_pub.reverse_accruals(p_api_version     => p_api_version,
7261                                                  p_init_msg_list   => okl_api.g_false,
7262                                                  x_return_status   => l_return_status,
7263                                                  x_msg_count       => x_msg_count,
7264                                                  x_msg_data        => x_msg_data,
7265                                                  p_khr_id          => p_term_rec.p_contract_id,
7266                                                  p_reversal_date   => l_quote_eff_date, -- Transaction date - Mandatory
7267                                                  p_accounting_date => l_valid_gl_date, -- Valid GL Date - Mandatory
7268                                                  p_reverse_from    => l_reverse_from, -- Date from when accruals need to be reversed - Mandatory -- rmunjulu bug 4507662
7269                                                  p_reverse_to      => l_empty_reverse_to_date, -- No value needs to be passed to this
7270                                                  p_tcn_type        => l_empty_tcn_type); -- No value needs to be passed to this
7271       IF (is_debug_statement_on) THEN
7272         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_generate_accruals_pub.reverse_accruals l_return_status = '||l_return_status);
7273       END IF;
7274       IF l_return_status <> okl_api.g_ret_sts_success
7275       THEN
7276 
7277         -- Message: Unable to do reversal of accrual during
7278         -- termination of contract CONTRACT_NUMBER.
7279         okl_api.set_message(p_app_name     => g_app_name,
7280                             p_msg_name     => 'OKL_AM_EDT_REVERSAL_ERR',
7281                             p_token1       => 'CONTRACT_NUMBER',
7282                             p_token1_value => p_term_rec.p_contract_number);
7283       END IF;
7284 
7285       -- Raise exception to rollback this whole block
7286       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7287       THEN
7288         RAISE okl_api.g_exception_unexpected_error;
7289       ELSIF (l_return_status = okl_api.g_ret_sts_error)
7290       THEN
7291         RAISE okl_api.g_exception_error;
7292       END IF;
7293     END IF; -- 13074256 End to check if Termination from quote
7294 
7295 
7296       -- *************
7297       -- REVERSAL OF NON-INCOME
7298       -- *************
7299 
7300       -- Reversal of non-income during contract termination
7301       -- Get the transaction id for Accrual
7302       okl_am_util_pvt.get_transaction_id(p_try_name      => 'Accrual',
7303                                          x_return_status => l_return_status,
7304                                          x_try_id        => l_try_id);
7305 
7306       -- Get the meaning of lookup
7307       l_trans_meaning := okl_am_util_pvt.get_lookup_meaning(p_lookup_type => 'OKL_ACCOUNTING_EVENT_TYPE',
7308                                                             p_lookup_code => 'ACCRUAL',
7309                                                             p_validate_yn => 'Y');
7310 
7311       IF l_return_status <> okl_api.g_ret_sts_success
7312       THEN
7313 
7314         -- Message: Unable to find a transaction type for
7315         -- the transaction TRY_NAME
7316         okl_api.set_message(p_app_name     => g_app_name,
7317                             p_msg_name     => 'OKL_AM_NO_TRX_TYPE_FOUND',
7318                             p_token1       => 'TRY_NAME',
7319                             p_token1_value => l_trans_meaning);
7320 
7321         --Message: Unable to do reversal of non-income during termination
7322         -- of contract CONTRACT_NUMBER.
7323         okl_api.set_message(p_app_name     => g_app_name,
7324                             p_msg_name     => 'OKL_AM_REV_NONINC_ERR',
7325                             p_token1       => 'CONTRACT_NUMBER',
7326                             p_token1_value => p_term_rec.p_contract_number);
7327 
7328       END IF;
7329 
7330       -- Raise exception to rollback this whole block
7331       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7332       THEN
7333         RAISE okl_api.g_exception_unexpected_error;
7334       ELSIF (l_return_status = okl_api.g_ret_sts_error)
7335       THEN
7336         RAISE okl_api.g_exception_error;
7337       END IF;
7338 
7339       -- Set the rec type in parameter
7340       l_catchup_rec.contract_id         := p_term_rec.p_contract_id;
7341       l_catchup_rec.accrual_date        := l_valid_gl_date; -- RMUNJULU 3148215
7342       l_catchup_rec.contract_number     := p_term_rec.p_contract_number;
7343       l_catchup_rec.rule_result         := g_yes;
7344       l_catchup_rec.override_status     := g_no;
7345       l_catchup_rec.product_id          := l_pdt_id;
7346       l_catchup_rec.trx_type_id         := l_try_id;
7347       l_catchup_rec.advance_arrears     := NULL;
7348       l_catchup_rec.factoring_synd_flag := NULL;
7349       l_catchup_rec.post_to_gl          := g_yes;
7350       l_catchup_rec.gl_reversal_flag    := g_no;
7351       l_catchup_rec.memo_yn             := g_no;
7352       l_catchup_rec.description         := 'Catchup of income on termination of the contract';
7353       IF (is_debug_statement_on) THEN
7354         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  okl_generate_accruals_pub.catchup_accruals');
7355       END IF;
7356       okl_generate_accruals_pub.catchup_accruals(p_api_version   => p_api_version,
7357                                                  p_init_msg_list => okl_api.g_false,
7358                                                  x_return_status => l_return_status,
7359                                                  x_msg_count     => x_msg_count,
7360                                                  x_msg_data      => x_msg_data,
7361                                                  p_catchup_rec   => l_catchup_rec,
7362                                                  x_tcnv_tbl      => lx_tcnv_tbl,
7363                                                  x_tclv_tbl      => lx_tclv_tbl);
7364       IF (is_debug_statement_on) THEN
7365         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_generate_accruals_pub.catchup_accruals l_return_status = '||l_return_status);
7366       END IF;
7367       IF l_return_status <> okl_api.g_ret_sts_success
7368       THEN
7369 
7370         --Message: Unable to do reversal of non-income during termination
7371         -- of contract CONTRACT_NUMBER.
7372         okl_api.set_message(p_app_name     => g_app_name,
7373                             p_msg_name     => 'OKL_AM_REV_NONINC_ERR',
7374                             p_token1       => 'CONTRACT_NUMBER',
7375                             p_token1_value => p_term_rec.p_contract_number);
7376       END IF;
7377 
7378       -- Raise exception to rollback this whole block
7379       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7380       THEN
7381         RAISE okl_api.g_exception_unexpected_error;
7382       ELSIF (l_return_status = okl_api.g_ret_sts_error)
7383       THEN
7384         RAISE okl_api.g_exception_error;
7385       END IF;
7386 
7387       -- MGAAP 7263041 start
7388 
7389       IF (l_multi_gaap_yn = 'Y') THEN
7390 
7391         l_sob_id_rep := Okl_Accounting_Util.GET_SET_OF_BOOKS_ID(
7392                                      p_representation_type => 'SECONDARY');
7393 
7394         l_valid_gl_date_rep :=
7395                okl_accounting_util.get_valid_gl_date (
7396                                  p_gl_date      => l_quote_accpt_date,
7397                                  p_ledger_id    => l_sob_id_rep
7398                );
7399 
7400         l_catchup_rec.product_id := l_reporting_pdt_id;
7401         l_catchup_rec.accrual_date := l_valid_gl_date_rep;
7402 
7403         IF (is_debug_statement_on) THEN
7404           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  okl_generate_accruals_pub.catchup_accruals for SECONDARY');
7405         END IF;
7406         okl_generate_accruals_pub.catchup_accruals(p_api_version   => p_api_version,
7407                                                    p_init_msg_list => okl_api.g_false,
7408                                                    x_return_status => l_return_status,
7409                                                    x_msg_count     => x_msg_count,
7410                                                    x_msg_data      => x_msg_data,
7411                                                    p_catchup_rec   => l_catchup_rec,
7412                                                    x_tcnv_tbl      => lx_tcnv_tbl,
7413                                                    x_tclv_tbl      => lx_tclv_tbl,
7414                                                    p_representation_type   => 'SECONDARY');
7415         IF (is_debug_statement_on) THEN
7416           OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_generate_accruals_pub.catchup_accruals for SECONDARY l_return_status = '||l_return_status);
7417         END IF;
7418         IF l_return_status <> okl_api.g_ret_sts_success
7419         THEN
7420 
7421           --Message: Unable to do reversal of non-income during termination
7422           -- of contract CONTRACT_NUMBER.
7423           okl_api.set_message(p_app_name     => g_app_name,
7424                               p_msg_name     => 'OKL_AM_REV_NONINC_ERR',
7425                               p_token1       => 'CONTRACT_NUMBER',
7426                               p_token1_value => p_term_rec.p_contract_number);
7427         END IF;
7428 
7429         -- Raise exception to rollback this whole block
7430         IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7431         THEN
7432           RAISE okl_api.g_exception_unexpected_error;
7433         ELSIF (l_return_status = okl_api.g_ret_sts_error)
7434         THEN
7435           RAISE okl_api.g_exception_error;
7436         END IF;
7437 
7438       END IF;
7439 
7440       -- MGAAP 7263041 end
7441 
7442       -- *************
7443       -- REVERSAL OF LOSS-PROVISIONS
7444       -- *************
7445 
7446       -- Loss provisions reversal during contract termination
7447       -- Set the rec type in parameter
7448       l_lprv_rec.cntrct_num    := p_term_rec.p_contract_number;
7449       l_lprv_rec.reversal_type := NULL;
7450 
7451       -- RMUNJULU 3097068 Added code to set the reversal date with valid GL date
7452       l_lprv_rec.reversal_date := l_valid_gl_date;
7453       IF (is_debug_statement_on) THEN
7454         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_rev_loss_prov_pub.reverse_loss_provisions');
7455       END IF;
7456       okl_rev_loss_prov_pub.reverse_loss_provisions(p_api_version   => p_api_version,
7457                                                     p_init_msg_list => okl_api.g_false,
7458                                                     x_return_status => l_return_status,
7459                                                     x_msg_count     => x_msg_count,
7460                                                     x_msg_data      => x_msg_data,
7461                                                     p_lprv_rec      => l_lprv_rec);
7462       IF (is_debug_statement_on) THEN
7463         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_rev_loss_prov_pub.reverse_loss_provisions l_return_status = '||l_return_status);
7464       END IF;
7465       IF l_return_status <> okl_api.g_ret_sts_success
7466       THEN
7467 
7468         -- Message: Unable to do reversal of loss provisions during
7469         -- termination of contract CONTRACT_NUMBER.
7470         okl_api.set_message(p_app_name     => g_app_name,
7471                             p_msg_name     => 'OKL_AM_REV_LOSPROV_ERR',
7472                             p_token1       => 'CONTRACT_NUMBER',
7473                             p_token1_value => p_term_rec.p_contract_number);
7474       END IF;
7475 
7476       -- Raise exception to rollback this whole block
7477       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7478       THEN
7479         RAISE okl_api.g_exception_unexpected_error;
7480       ELSIF (l_return_status = okl_api.g_ret_sts_error)
7481       THEN
7482         RAISE okl_api.g_exception_error;
7483       END IF;
7484 
7485       -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
7486 
7487 
7488       -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
7489 
7490       --Bug# 3999921: pagarg +++ T and A +++++++ Start ++++++++++
7491       IF p_term_rec.p_quote_type <> 'TER_RELEASE_WO_PURCHASE'
7492       THEN
7493         --Bug# 3999921: pagarg +++ T and A +++++++ End ++++++++++
7494         -- MDOKAL Bug 3061765
7495         -- The following code added for processing IDC acceleration.
7496         -- Currently only applicable for termination of Fee lines.
7497 
7498         -- Bug#7248153 - rbruno - commented - Start
7499         /*
7500 
7501         -- rmunjulu 4424713 Derive stream type id based on product type
7502         IF l_deal_type IN ('LEASEDF', 'LEASEST')
7503         THEN
7504           -- DF/ST Lease
7505           -- smoduga +++++++++ User Defined Streams -- start    ++++++++++++++++
7506           okl_streams_util.get_dependent_stream_type(p_term_rec.p_contract_id,
7507                                                      'RENT',
7508                                                      'LEASE_INCOME',
7509                                                      l_return_status,
7510                                                      lx_pretax_sty_id);
7511         ELSIF l_deal_type = 'LEASEOP'
7512         THEN
7513           -- OP Lease
7514 
7515           okl_streams_util.get_dependent_stream_type(p_term_rec.p_contract_id,
7516                                                      'RENT',
7517                                                      'RENT_ACCRUAL',
7518                                                      l_return_status,
7519                                                      lx_rentaccrual_sty_id);
7520 
7521           -- smoduga +++++++++ User Defined Streams -- end    ++++++++++++++++
7522           -- 03-mar-06 sgorantl -- Bug 4931796
7523         ELSIF (l_deal_type = 'LOAN' OR
7524               l_deal_type = 'LOAN-REVOLVING')
7525         THEN
7526           -- Loan
7527           -- 03-mar-06 sgorantl -- Bug 4931796
7528           -- rmunjulu bug 4416094
7529           okl_streams_util.get_dependent_stream_type(p_term_rec.p_contract_id,
7530                                                      'RENT',
7531                                                      'INTEREST_INCOME',
7532                                                      l_return_status,
7533                                                      lx_interestincome_sty_id);
7534 
7535         END IF;
7536 
7537         OPEN get_accel_till_csr(p_term_rec.p_contract_id,
7538                                 lx_pretax_sty_id,
7539                                 lx_rentaccrual_sty_id,
7540                                 lx_interestincome_sty_id); -- rmunjulu bug 4416094
7541         FETCH get_accel_till_csr
7542           INTO l_accelerate_till_date;
7543         CLOSE get_accel_till_csr;
7544 
7545     */
7546     -- Bug#7248153 - rbruno - commented - End
7547 
7548   IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists, 'N') = 'Y' THEN -- 13074256 Add condition to do Accrual Acceleration only if Termination from Quote (Not if Expiration)
7549    -- Added by ansethur for bug#6156337 08-Aug-2007 start
7550         FOR primary_sty_id_rec in primary_sty_id_csr(p_term_rec.p_contract_id) LOOP
7551 
7552 
7553 
7554            OKL_STREAMS_UTIL.get_dependent_stream_type(p_term_rec.p_contract_id,
7555                                                       primary_sty_id_rec.Primary_sty_id,
7556                                                       'AMORTIZED_FEE_EXPENSE',
7557                                                       l_return_status,
7558                                                       lx_sty_id);
7559 
7560         -- Bug#7248153 - rbruno - Added - Start
7561 
7562         OPEN get_accel_till_csr(p_term_rec.p_contract_id,lx_sty_id);
7563         FETCH get_accel_till_csr  INTO l_accelerate_till_date;
7564         CLOSE get_accel_till_csr;
7565 
7566         -- Bug#7248153 - rbruno Added - End
7567 
7568 
7569    -- Added by ansethur for bug#6156337 08-Aug-2007 end
7570         /* commented by ansethur for bug#6156337 08-Aug-2007 start
7571         -- smoduga +++++++++ User Defined Streams -- start    ++++++++++++++++
7572         okl_streams_util.get_dependent_stream_type(p_term_rec.p_contract_id,
7573                                                    'EXPENSE',
7574                                                    'AMORTIZED_FEE_EXPENSE',
7575                                                    l_return_status,
7576                                                    lx_sty_id);
7577         -- smoduga +++++++++ User Defined Streams -- end    ++++++++++++++++
7578       */ -- commented by ansethur for bug#6156337 08-Aug-2007 End
7579         FOR idc_accel_rec IN idc_accel_csr(p_term_rec.p_contract_id,
7580                                            lx_sty_id ,primary_sty_id_rec.kle_id )
7581         LOOP
7582 
7583           IF p_term_rec.p_termination_date IS NULL OR
7584              p_term_rec.p_termination_date = okc_api.g_miss_date
7585           THEN
7586             l_acceleration_rec.acceleration_date := l_valid_gl_date; --sysdate; -- rmunjulu EDAT
7587           ELSE
7588             l_acceleration_rec.acceleration_date := p_term_rec.p_termination_date;
7589           END IF;
7590 
7591           IF l_accelerate_till_date IS NULL OR
7592              l_accelerate_till_date = okc_api.g_miss_date
7593           THEN
7594             l_acceleration_rec.accelerate_till_date := p_term_rec.p_orig_end_date;
7595           ELSE
7596             l_acceleration_rec.accelerate_till_date := l_accelerate_till_date;
7597           END IF;
7598 	  -- fix for bug -- 5623356 -- added below condition
7599  	  -- If quote exists then cancelation date is quote eff from date else sysdate
7600  	  IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
7601           --akrangan Bug 5526955 fix start
7602  	  --Based on CHK_ACCRUAL_PREVIOUS_MNTH_YN setup check accruals till quote eff date OR previous month last date
7603  	  IF nvl(l_accrual_previous_mnth_yn,'N') = 'N' THEN
7604  	    l_acceleration_rec.accelerate_from_date := TRUNC(LAST_DAY(l_quote_eff_date) + 1);
7605  	  ELSE
7606  	    l_acceleration_rec.accelerate_from_date := LAST_DAY(TRUNC(l_quote_eff_date, 'MONTH')-1)+1;
7607  	  END IF;
7608  	  --akrangan Bug 5526955 fix end
7609 	  ELSE
7610  	    l_acceleration_rec.accelerate_from_date := TRUNC(LAST_DAY(l_quote_eff_date) + 1);
7611  	  END IF;
7612  	  -- fix for bug -- 5623356 --
7613           l_acceleration_rec.khr_id          := p_term_rec.p_contract_id;
7614           l_acceleration_rec.sty_id          := lx_sty_id; -- User defined streams change 392546
7615           l_acceleration_rec.description     := 'Acceleration of IDC expense for Fee ' ||
7616                                                 idc_accel_rec.fee_type ||
7617                                                 ' - Accural';
7618           l_acceleration_rec.accrual_rule_yn := 'N';
7619           l_acceleration_rec.kle_id          := primary_sty_id_rec.kle_id;     -- Added by ansethur for bug#6156337 08-Aug-2007
7620 	  -- bug 9914727 - Initializing the trx_number to null for a fresh record
7621            l_acceleration_rec.trx_number := null;
7622 	   l_trx_number:=null;
7623 
7624           okl_generate_accruals_pvt.accelerate_accruals(p_api_version      => p_api_version,
7625                                                         p_init_msg_list    => p_init_msg_list,
7626                                                         x_return_status    => l_return_status,
7627                                                         x_msg_count        => x_msg_count,
7628                                                         x_msg_data         => x_msg_data,
7629                                                         p_acceleration_rec => l_acceleration_rec,
7630                              x_trx_number  => l_trx_number); -- MGAAP 7263041
7631 
7632           IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7633           THEN
7634             RAISE okl_api.g_exception_unexpected_error;
7635           ELSIF (l_return_status = okl_api.g_ret_sts_error)
7636           THEN
7637             RAISE okl_api.g_exception_error;
7638           END IF;
7639 
7640 -- sosharma added multigaap check for bug 13075531
7641   IF (l_multi_gaap_yn = 'Y') THEN
7642           -- Start MGAAP 7263041
7643           OKL_STREAMS_SEC_PVT.SET_REPO_STREAMS;
7644           okl_streams_util.get_dependent_stream_type_rep(
7645                                                    p_term_rec.p_contract_id,
7646                                                    'EXPENSE',
7647                                                    'AMORTIZED_FEE_EXPENSE',
7648                                                    l_return_status,
7649                                                    lx_sty_id_rep);
7650           IF (l_return_status = OKL_API.G_RET_STS_SUCCESS) THEN
7651             l_acceleration_rec.sty_id := lx_sty_id_rep;
7652             l_acceleration_rec.trx_number := l_trx_number;
7653 
7654             OKL_GENERATE_ACCRUALS_PVT.accelerate_accruals (
7655                              p_api_version       => p_api_version,
7656                              p_init_msg_list     => p_init_msg_list,
7657                              x_return_status     => l_return_status,
7658                              x_msg_count         => x_msg_count,
7659                              x_msg_data          => x_msg_data,
7660                              p_acceleration_rec  => l_acceleration_rec,
7661                              p_representation_type  => 'SECONDARY',
7662                              x_trx_number  => l_trx_number);
7663 
7664             OKL_STREAMS_SEC_PVT.RESET_REPO_STREAMS;
7665 
7666             IF (is_debug_statement_on) THEN
7667               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name,'called OKL_GENERATE_ACCRUALS_PVT.accelerate_accruals , return status : ' || l_return_status);
7668             END IF;
7669 
7670             IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7671             THEN
7672               RAISE okl_api.g_exception_unexpected_error;
7673             ELSIF (l_return_status = okl_api.g_ret_sts_error)
7674             THEN
7675               RAISE okl_api.g_exception_error;
7676             END IF;
7677 
7678           END IF;
7679           -- End MGAAP 7263041
7680 END IF;
7681 
7682         END LOOP;
7683       END LOOP;   -- Added by ansethur for bug#6156337 08-Aug-2007 start
7684 
7685   END IF; -- 13074256 End to check if Termination from quote
7686 
7687         --Bug# 3999921: pagarg +++ T and A +++++++ Start ++++++++++
7688       END IF; -- p_term_rec.p_quote_type <> 'TER_RELEASE_WO_PURCHASE'
7689       IF (is_debug_statement_on) THEN
7690         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling   okl_am_lease_loan_trmnt_pvt.process_adjustments');
7691       END IF;
7692       okl_am_lease_loan_trmnt_pvt.process_adjustments(p_api_version      => p_api_version,
7693                                                       p_init_msg_list    => okl_api.g_false,
7694                                                       x_return_status    => l_return_status,
7695                                                       x_msg_count        => x_msg_count,
7696                                                       x_msg_data         => x_msg_data,
7697                                                       p_term_rec         => p_term_rec,
7698                                                       p_tcnv_rec         => px_tcnv_rec, -- rmunjulu TNA Added since trn_id is needed
7699                                                       p_call_origin      => 'FULL',
7700                                                       p_termination_date => p_sys_date);
7701        IF (is_debug_statement_on) THEN
7702         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_am_lease_loan_trmnt_pvt.process_adjustments l_return_status = '||l_return_status);
7703       END IF;
7704 
7705       IF (is_debug_statement_on) THEN
7706         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  okl_am_lease_loan_trmnt_pvt.process_adjustments');
7707       END IF;
7708       --Bug# 3999921: pagarg +++ T and A +++++++ End ++++++++++
7709 
7710       -- End MDOKAL Bug 3061765
7711       -- Smoduga
7712       -- Call to Process discount and Subsidy during acceptance of a termination quote
7713       okl_am_lease_loan_trmnt_pvt.process_discount_subsidy(p_api_version      => p_api_version,
7714                                                            p_init_msg_list    => okl_api.g_false,
7715                                                            x_return_status    => l_return_status,
7716                                                            x_msg_count        => x_msg_count,
7717                                                            x_msg_data         => x_msg_data,
7718                                                            p_term_rec         => p_term_rec,
7719                                                            p_call_origin      => NULL,
7720                                                            p_termination_date => p_sys_date);
7721       IF (is_debug_statement_on) THEN
7722         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_am_lease_loan_trmnt_pvt.process_adjustments l_return_status = '||l_return_status);
7723       END IF;
7724       IF l_return_status <> okl_api.g_ret_sts_success
7725       THEN
7726 
7727         okl_api.set_message(p_app_name => g_app_name,
7728                             p_msg_name => 'OKL_AM_SUBSIDY_PROC_FAIL');
7729       END IF;
7730 
7731       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7732       THEN
7733         RAISE okl_api.g_exception_unexpected_error;
7734       ELSIF (l_return_status = okl_api.g_ret_sts_error)
7735       THEN
7736         RAISE okl_api.g_exception_error;
7737       END IF;
7738       -- Smoduga
7739       -- Call to Process discount and Subsidy during acceptance of a termination quote
7740       IF (is_debug_statement_on) THEN
7741         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'calling  okl_am_lease_loan_trmnt_pvt.process_loan_refunds');
7742       END IF;
7743       -- rmunjulu LOANS_ENHANCEMENTS Call to refund excess loan payments
7744       okl_am_lease_loan_trmnt_pvt.process_loan_refunds(p_api_version      => p_api_version,
7745                                                        p_init_msg_list    => okl_api.g_false,
7746                                                        x_return_status    => l_return_status,
7747                                                        x_msg_count        => x_msg_count,
7748                                                        x_msg_data         => x_msg_data,
7749                                                        p_term_rec         => p_term_rec,
7750                                                        p_tcnv_rec         => px_tcnv_rec,
7751                                                        p_call_origin      => 'FULL',
7752                                                        p_termination_date => p_sys_date);
7753       IF (is_debug_statement_on) THEN
7754         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  okl_am_lease_loan_trmnt_pvt.process_loan_refunds l_return_status = '||l_return_status);
7755       END IF;
7756       IF l_return_status <> okl_api.g_ret_sts_success
7757       THEN
7758 
7759         okl_api.set_message(p_app_name => g_app_name,
7760                             p_msg_name => 'OKL_AM_LOAN_REFUND_FAILED');
7761       END IF;
7762 
7763       IF (l_return_status = okl_api.g_ret_sts_unexp_error)
7764       THEN
7765         RAISE okl_api.g_exception_unexpected_error;
7766       ELSIF (l_return_status = okl_api.g_ret_sts_error)
7767       THEN
7768         RAISE okl_api.g_exception_error;
7769       END IF;
7770 
7771       -- *************
7772       -- SUCCESS MESSAGES
7773       -- *************
7774 
7775       -- Message: Reversal of non-income during termination
7776       -- of contract CONTRACT_NUMBER done successfully.
7777       okl_api.set_message(p_app_name     => g_app_name,
7778                           p_msg_name     => 'OKL_AM_REV_NONINC_SUC',
7779                           p_token1       => 'CONTRACT_NUMBER',
7780                           p_token1_value => p_term_rec.p_contract_number);
7781 
7782       -- Message: Reversal of loss provisions during
7783       -- termination of contract CONTRACT_NUMBER done successfully.
7784       okl_api.set_message(p_app_name     => g_app_name,
7785                           p_msg_name     => 'OKL_AM_REV_LOSPROV_SUC',
7786                           p_token1       => 'CONTRACT_NUMBER',
7787                           p_token1_value => p_term_rec.p_contract_number);
7788 
7789     END IF; -- rmunjulu 4141991 End of new If
7790 
7791     -- Set success messages here
7792     -- Get the meaning of lookup
7793     l_trans_meaning := okl_am_util_pvt.get_lookup_meaning(p_lookup_type => 'OKL_ACCOUNTING_EVENT_TYPE',
7794                                                           p_lookup_code => 'TERMINATION',
7795                                                           p_validate_yn => 'Y');
7796 
7797     -- Accounting entries created for transaction type TRX_TYPE.
7798     okl_api.set_message(p_app_name     => g_app_name,
7799                         p_msg_name     => 'OKL_AM_ACC_ENT_CREATED',
7800                         p_token1       => 'TRX_TYPE',
7801                         p_token1_value => l_trans_meaning);
7802 
7803     -- store the highest degree of error
7804     set_overall_status(p_return_status   => l_return_status,
7805                        px_overall_status => px_overall_status);
7806 
7807     -- set the transaction record
7808     set_transaction_rec(p_return_status  => l_return_status,
7809                         p_overall_status => px_overall_status,
7810                         p_tmt_flag       => 'TMT_ACCOUNTING_ENTRIES_YN',
7811                         p_tsu_code       => 'WORKING',
7812                         px_tcnv_rec      => px_tcnv_rec);
7813 
7814   END IF;
7815 
7816   x_return_status := l_return_status;
7817     IF (is_debug_procedure_on) THEN
7818        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
7819     END IF;
7820 EXCEPTION
7821 
7822   WHEN okl_api.g_exception_error THEN
7823           IF (is_debug_exception_on) THEN
7824              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
7825         END IF;
7826 
7827     -- mdokal bug 3061765
7828     IF get_accel_till_csr%ISOPEN
7829     THEN
7830       CLOSE get_accel_till_csr;
7831     END IF;
7832 
7833     -- mdokal bug 3061765
7834     IF idc_accel_csr%ISOPEN
7835     THEN
7836       CLOSE idc_accel_csr;
7837     END IF;
7838 
7839     IF l_product_type_csr%ISOPEN
7840     THEN
7841       CLOSE l_product_type_csr;
7842     END IF;
7843 
7844     IF prod_id_csr%ISOPEN
7845     THEN
7846       CLOSE prod_id_csr;
7847     END IF;
7848 
7849     -- rmunjulu 4141991
7850     IF get_k_sts_csr%ISOPEN
7851     THEN
7852       CLOSE get_k_sts_csr;
7853     END IF;
7854     IF trx_contracts_dff_csr%ISOPEN
7855     THEN
7856       CLOSE trx_contracts_dff_csr;
7857     END IF;
7858     IF txl_contracts_dff_csr%ISOPEN
7859     THEN
7860       CLOSE txl_contracts_dff_csr;
7861     END IF;
7862     IF product_name_csr%ISOPEN
7863     THEN
7864       CLOSE product_name_csr;
7865     END IF;
7866 
7867     ROLLBACK TO accounting_entries;
7868 
7869     x_return_status := okl_api.g_ret_sts_error;
7870     -- store the highest degree of error
7871     set_overall_status(p_return_status   => x_return_status,
7872                        px_overall_status => px_overall_status);
7873 
7874     -- set the transaction record
7875     set_transaction_rec(p_return_status  => x_return_status,
7876                         p_overall_status => px_overall_status,
7877                         p_tmt_flag       => 'TMT_ACCOUNTING_ENTRIES_YN',
7878                         p_tsu_code       => 'ERROR',
7879                         px_tcnv_rec      => px_tcnv_rec);
7880 
7881   WHEN okl_api.g_exception_unexpected_error THEN
7882         IF (is_debug_exception_on) THEN
7883              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
7884         END IF;
7885 
7886 
7887     -- mdokal bug 3061765
7888     IF get_accel_till_csr%ISOPEN
7889     THEN
7890       CLOSE get_accel_till_csr;
7891     END IF;
7892 
7893     -- mdokal bug 3061765
7894     IF idc_accel_csr%ISOPEN
7895     THEN
7896       CLOSE idc_accel_csr;
7897     END IF;
7898 
7899     IF l_product_type_csr%ISOPEN
7900     THEN
7901       CLOSE l_product_type_csr;
7902     END IF;
7903 
7904     IF prod_id_csr%ISOPEN
7905     THEN
7906       CLOSE prod_id_csr;
7907     END IF;
7908 
7909     -- rmunjulu 4141991
7910     IF get_k_sts_csr%ISOPEN
7911     THEN
7912       CLOSE get_k_sts_csr;
7913     END IF;
7914     IF trx_contracts_dff_csr%ISOPEN
7915     THEN
7916       CLOSE trx_contracts_dff_csr;
7917     END IF;
7918     IF txl_contracts_dff_csr%ISOPEN
7919     THEN
7920       CLOSE txl_contracts_dff_csr;
7921     END IF;
7922     IF product_name_csr%ISOPEN
7923     THEN
7924       CLOSE product_name_csr;
7925     END IF;
7926 
7927     ROLLBACK TO accounting_entries;
7928 
7929     x_return_status := okl_api.g_ret_sts_unexp_error;
7930     -- store the highest degree of error
7931     set_overall_status(p_return_status   => x_return_status,
7932                        px_overall_status => px_overall_status);
7933 
7934     -- set the transaction record
7935     set_transaction_rec(p_return_status  => x_return_status,
7936                         p_overall_status => px_overall_status,
7937                         p_tmt_flag       => 'TMT_ACCOUNTING_ENTRIES_YN',
7938                         p_tsu_code       => 'ERROR',
7939                         px_tcnv_rec      => px_tcnv_rec);
7940 
7941   WHEN OTHERS THEN
7942           IF (is_debug_exception_on) THEN
7943            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
7944         END IF;
7945 
7946     -- mdokal bug 3061765
7947     IF get_accel_till_csr%ISOPEN
7948     THEN
7949       CLOSE get_accel_till_csr;
7950     END IF;
7951 
7952     -- mdokal bug 3061765
7953     IF idc_accel_csr%ISOPEN
7954     THEN
7955       CLOSE idc_accel_csr;
7956     END IF;
7957 
7958     IF l_product_type_csr%ISOPEN
7959     THEN
7960       CLOSE l_product_type_csr;
7961     END IF;
7962 
7963     IF prod_id_csr%ISOPEN
7964     THEN
7965       CLOSE prod_id_csr;
7966     END IF;
7967 
7968     -- rmunjulu 4141991
7969     IF get_k_sts_csr%ISOPEN
7970     THEN
7971       CLOSE get_k_sts_csr;
7972     END IF;
7973     IF trx_contracts_dff_csr%ISOPEN
7974     THEN
7975       CLOSE trx_contracts_dff_csr;
7976     END IF;
7977     IF txl_contracts_dff_csr%ISOPEN
7978     THEN
7979       CLOSE txl_contracts_dff_csr;
7980     END IF;
7981     IF product_name_csr%ISOPEN
7982     THEN
7983       CLOSE product_name_csr;
7984     END IF;
7985 
7986     ROLLBACK TO accounting_entries;
7987 
7988     x_return_status := okl_api.g_ret_sts_unexp_error;
7989     -- store the highest degree of error
7990     set_overall_status(p_return_status   => x_return_status,
7991                        px_overall_status => px_overall_status);
7992 
7993     -- set the transaction record
7994     set_transaction_rec(p_return_status  => x_return_status,
7995                         p_overall_status => px_overall_status,
7996                         p_tmt_flag       => 'TMT_ACCOUNTING_ENTRIES_YN',
7997                         p_tsu_code       => 'ERROR',
7998                         px_tcnv_rec      => px_tcnv_rec);
7999 
8000     -- Set the oracle error message
8001     okl_api.set_message(p_app_name     => okc_api.g_app_name,
8002                         p_msg_name     => g_unexpected_error,
8003                         p_token1       => g_sqlcode_token,
8004                         p_token1_value => SQLCODE,
8005                         p_token2       => g_sqlerrm_token,
8006                         p_token2_value => SQLERRM);
8007 
8008 END process_accounting_entries;
8009 
8010 PROCEDURE Create_Repo_asset_2(
8011               p_init_msg_list               IN  VARCHAR2,
8012               p_khr_id          IN OKC_K_HEADERS_B.ID%TYPE,
8013               p_kle_id          IN OKL_K_LINES.ID%TYPE,
8014               p_corporate_book  IN OKL_SYSTEM_PARAMS_ALL.CORPORATE_BOOK%TYPE,
8015               p_tax_book_1      IN OKL_SYSTEM_PARAMS_ALL.TAX_BOOK_1%TYPE,
8016               p_tax_book_2      IN OKL_SYSTEM_PARAMS_ALL.TAX_BOOK_2%TYPE,
8017               p_rep_book        IN OKL_SYSTEM_PARAMS_ALL.RPT_PROD_BOOK_TYPE_CODE%TYPE,
8018               p_fa_location_id  IN OKL_SYSTEM_PARAMS_ALL.FA_LOCATION_ID%TYPE,
8019               p_asset_key_id    IN OKL_SYSTEM_PARAMS_ALL.ASSET_KEY_ID%TYPE,
8020               p_depreciate_yn   IN OKL_SYSTEM_PARAMS_ALL.DEPRECIATE_YN%TYPE,
8021               p_tas_id          IN OKL_TRX_ASSETS.ID%TYPE,
8022               p_line_number     IN NUMBER,
8023               p_quote_id        IN NUMBER,
8024               x_return_status   OUT NOCOPY VARCHAR2,
8025               x_msg_count       OUT NOCOPY NUMBER,
8026               x_msg_data        OUT NOCOPY VARCHAR2
8027            ) IS
8028 
8029        CURSOR l_sub_line_csr(p_chr_id   IN NUMBER
8030                             ,p_lty_code IN VARCHAR2
8031                             ,p_cle_id   IN NUMBER) IS
8032        SELECT cle.id,
8033               cim.object1_id1,
8034               cim.object1_id2,
8035               cim.number_of_items,
8036               fa_kle.year_of_manufacture
8037        FROM   okc_k_lines_b  cle,
8038               okc_k_items cim,
8039               okc_line_styles_b lse,
8040               okc_statuses_b sts,
8041               okl_k_lines fa_kle,
8042               okc_k_lines_b fa_cle,
8043               okc_line_styles_b fa_lse
8044        WHERE cle.dnz_chr_id = p_chr_id
8045        AND   cle.cle_id = p_cle_id
8046        AND   cle.lse_id = lse.id
8047        AND   lse.lty_code = p_lty_code
8048        AND   cim.cle_id = cle.id
8049        AND   cim.dnz_chr_id = cle.dnz_chr_id
8050        AND   sts.code = cle.sts_code
8051        AND   sts.ste_code NOT IN ('HOLD', 'TERMINATED', 'EXPIRED', 'CANCELLED')
8052        AND   fa_cle.id = p_cle_id
8053        AND   fa_kle.id = fa_cle.id
8054        AND   fa_cle.lse_id = fa_lse.id
8055        AND   fa_lse.lty_code = 'FREE_FORM1';
8056 
8057        l_model_line_rec  l_sub_line_csr%ROWTYPE;
8058 
8059        CURSOR l_inv_item_csr(p_inv_item_id IN NUMBER,
8060                              p_org_id IN NUMBER) IS
8061        SELECT asset_category_id
8062        FROM mtl_system_items
8063        WHERE inventory_item_id = p_inv_item_id
8064        AND organization_id = p_org_id;
8065 
8066        l_inv_item_rec l_inv_item_csr%ROWTYPE;
8067 
8068        CURSOR c_asset_description(p_cle_id IN NUMBER) IS
8069        SELECT NAME, ITEM_DESCRIPTION
8070        FROM OKC_K_LINES_TL
8071        WHERE ID = p_cle_id
8072        AND LANGUAGE = USERENV('LANG');
8073 
8074        l_asset_description c_asset_description%ROWTYPE;
8075 
8076        CURSOR c_asset_ret_dtls(p_cle_id IN NUMBER) IS
8077        SELECT DATE_RETURNED, ASSET_FMV_AMOUNT
8078        FROM OKL_ASSET_RETURNS_B
8079        WHERE KLE_ID = p_cle_id
8080        AND ARS_CODE = 'REPOSSESSED';
8081 
8082        l_asset_ret_dtls c_asset_ret_dtls%ROWTYPE;
8083 
8084        CURSOR c_fa_expense_ccid(
8085                 p_book_type_code FA_CATEGORY_BOOKS.BOOK_TYPE_CODE%TYPE,
8086                 p_category_id    FA_CATEGORY_BOOKS.CATEGORY_ID%TYPE) IS
8087        SELECT DEPRN_EXPENSE_ACCOUNT_CCID
8088        FROM   FA_CATEGORY_BOOKS
8089        WHERE  BOOK_TYPE_CODE = p_book_type_code
8090        AND    CATEGORY_ID    = p_category_id;
8091 
8092        l_fa_expense_ccid c_fa_expense_ccid%ROWTYPE;
8093 
8094        CURSOR c_cim_asset_id(p_cle_id IN NUMBER) IS
8095        SELECT ITM.ID
8096        FROM   OKC_K_ITEMS ITM,
8097               OKC_K_LINES_B CLE,
8098               OKC_LINE_STYLES_B LSE
8099        WHERE ITM.CLE_ID = cle.ID
8100        AND   CLE.CLE_ID = p_cle_id
8101        AND   CLE.LSE_ID = LSE.ID
8102        AND   LSE.LTY_CODE = 'FIXED_ASSET';
8103 
8104        l_cim_asset_id c_cim_asset_id%ROWTYPE;
8105 
8106        CURSOR c_get_fa_books( p_book_type_code FA_BOOKS.BOOK_TYPE_CODE%TYPE,
8107                               p_asset_id FA_BOOKS.ASSET_ID%TYPE) IS
8108        SELECT DEPRN_METHOD_CODE,
8109               LIFE_IN_MONTHS,
8110               COST,
8111               SALVAGE_VALUE
8112        FROM   FA_BOOKS
8113        WHERE  ASSET_ID = p_asset_id
8114        AND    BOOK_TYPE_CODE = p_book_type_code ;
8115 
8116        l_get_fa_books c_get_fa_books%ROWTYPE;
8117 
8118        l_set_of_books_id OKL_SYS_ACCT_OPTS.SET_OF_BOOKS_ID%TYPE;
8119        l_max_books NUMBER;
8120 
8121 l_trans_rec                FA_API_TYPES.trans_rec_type;
8122 l_dist_trans_rec           FA_API_TYPES.trans_rec_type;
8123 l_asset_hdr_rec            FA_API_TYPES.asset_hdr_rec_type;
8124 l_asset_desc_rec           FA_API_TYPES.asset_desc_rec_type;
8125 l_asset_cat_rec            FA_API_TYPES.asset_cat_rec_type;
8126 l_asset_type_rec           FA_API_TYPES.asset_type_rec_type;
8127 l_asset_hierarchy_rec      fa_api_types.asset_hierarchy_rec_type;
8128 l_asset_fin_rec            FA_API_TYPES.asset_fin_rec_type;
8129 l_asset_deprn_rec          FA_API_TYPES.asset_deprn_rec_type;
8130 l_asset_dist_rec           FA_API_TYPES.asset_dist_rec_type;
8131 l_asset_dist_tbl           FA_API_TYPES.asset_dist_tbl_type;
8132 l_inv_tbl                  FA_API_TYPES.inv_tbl_type;
8133 
8134 l_calling_interface Varchar2(30) := 'OKLRLTNB:Create_Repo_asset';
8135 l_api_name             CONSTANT VARCHAR2(30) := 'CREATE_REPO_ASSET';
8136 l_api_version          CONSTANT NUMBER := 1.0;
8137 l_pkg_name  VARCHAR2(30) := 'OKL_AM_LEASE_TRMNT_PVT';
8138 l_asset_id NUMBER;
8139 l_talv_rec               talv_rec_type;
8140 x_talv_rec               talv_rec_type;
8141 
8142 l_oec NUMBER;
8143 l_cim_rec                      okl_okc_migration_pvt.cimv_rec_type;
8144 x_cim_rec                      okl_okc_migration_pvt.cimv_rec_type;
8145 
8146 l_txdv_rec     adpv_rec_type;
8147 x_txdv_rec     adpv_rec_type;
8148 l_line_detail_number NUMBER := 1;
8149 -- asawanka added for debug feature start
8150     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'Create_Repo_asset';
8151     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
8152     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
8153     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
8154     -- asawanka added for debug feature end
8155 BEGIN
8156      IF (is_debug_procedure_on) THEN
8157        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
8158      END IF;
8159 
8160   x_return_status := OKL_API.START_ACTIVITY (
8161                                l_api_name
8162                                ,p_init_msg_list
8163                                ,'_PVT'
8164                                ,x_return_status);
8165   -- Check if activity started successfully
8166   IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8167        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8168   ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8169        RAISE OKL_API.G_EXCEPTION_ERROR;
8170   END IF;
8171   IF (is_debug_statement_on) THEN
8172         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_khr_id = '||p_khr_id);
8173         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_kle_id = '||p_kle_id);
8174         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_corporate_book = '||p_corporate_book);
8175         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_tax_book_1 = '||p_tax_book_1);
8176         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_tax_book_2 = '||p_tax_book_2);
8177         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_rep_book = '||p_rep_book);
8178         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_fa_location_id = '||p_fa_location_id);
8179         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_asset_key_id = '||p_asset_key_id);
8180         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_depreciate_yn = '||p_depreciate_yn);
8181         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_tas_id = '||p_tas_id);
8182         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_line_number = '||p_line_number);
8183         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_quote_id = '||p_quote_id);
8184   END IF;
8185   x_return_status := OKL_API.G_RET_STS_SUCCESS;
8186 
8187   OPEN l_sub_line_csr(p_chr_id => p_khr_id,
8188                       p_lty_code => 'ITEM',
8189                       p_cle_id => p_kle_id);
8190   FETCH l_sub_line_csr INTO l_model_line_rec;
8191   CLOSE l_sub_line_csr;
8192 
8193   OPEN l_inv_item_csr(p_inv_item_id => TO_NUMBER(l_model_line_rec.object1_id1),
8194                       p_org_id      => TO_NUMBER(l_model_line_rec.object1_id2));
8195   FETCH l_inv_item_csr INTO l_inv_item_rec;
8196   CLOSE l_inv_item_csr;
8197 
8198   OPEN c_asset_description(p_kle_id);
8199   FETCH c_asset_description INTO l_asset_description;
8200   CLOSE c_asset_description;
8201 
8202   OPEN c_asset_ret_dtls(p_kle_id);
8203   FETCH c_asset_ret_dtls INTO l_asset_ret_dtls;
8204   CLOSE c_asset_ret_dtls;
8205   l_oec := l_asset_ret_dtls.asset_fmv_amount;
8206 
8207   l_set_of_books_id := OKL_ACCOUNTING_UTIL.GET_SET_OF_BOOKS_ID;
8208 
8209   /*IF (p_tax_book_2 IS NOT NULL AND p_tax_book_2 <> OKL_API.G_MISS_CHAR)
8210   THEN
8211     l_max_books := 3;
8212   ELSE
8213     l_max_books := 2;
8214   END IF;*/
8215   l_max_books := 4;
8216 
8217   FOR l_counter IN 1..l_max_books
8218   LOOP
8219 
8220     IF (l_counter=1) OR
8221        (l_counter=2 AND p_tax_book_1 IS NOT NULL
8222             AND p_tax_book_1 <> OKL_API.G_MISS_CHAR) OR
8223        (l_counter=3 AND p_tax_book_2 IS NOT NULL
8224             AND p_tax_book_2 <> OKL_API.G_MISS_CHAR) OR
8225        (l_counter=4 AND p_rep_book IS NOT NULL
8226             AND p_rep_book <> OKL_API.G_MISS_CHAR) THEN
8227     l_trans_rec.transaction_type_code := 'ADDITION';
8228     l_asset_desc_rec.asset_key_ccid:= p_asset_key_id;
8229     l_asset_desc_rec.current_units:= l_model_line_rec.number_of_items;
8230     l_asset_hdr_rec.asset_id          := NULL;
8231 
8232     IF (l_counter = 1) THEN
8233       l_asset_hdr_rec.book_type_code :=p_corporate_book;
8234     ELSIF (l_counter=2) THEN
8235       l_asset_hdr_rec.asset_id := l_asset_id;
8236       l_asset_hdr_rec.book_type_code :=p_tax_book_1;
8237     ELSIF (l_counter=3) THEN
8238       l_asset_hdr_rec.asset_id := l_asset_id;
8239       l_asset_hdr_rec.book_type_code :=p_tax_book_2;
8240     ELSE
8241       l_asset_hdr_rec.asset_id := l_asset_id;
8242       l_asset_hdr_rec.book_type_code :=p_rep_book;
8243     END IF;
8244 
8245     okl_debug_pub.logmessage('AKP:l_counter=' || l_counter || ' book=' || l_asset_hdr_rec.book_type_code);
8246     l_asset_hdr_rec.set_of_books_id :=l_set_of_books_id;
8247 
8248     l_asset_desc_rec.asset_number       := l_asset_description.NAME;
8249     l_asset_desc_rec.description        := l_asset_description.ITEM_DESCRIPTION;
8250 
8251     l_asset_cat_rec.category_id :=l_inv_item_rec.asset_category_id;
8252 
8253     l_asset_fin_rec.set_of_books_id :=l_set_of_books_id;
8254     l_asset_fin_rec.date_placed_in_service := l_asset_ret_dtls.DATE_RETURNED;
8255     IF (NVL(p_depreciate_yn, 'N')  = 'N' ) THEN
8256       l_asset_fin_rec.depreciate_flag :='NO';
8257     ELSE
8258       l_asset_fin_rec.depreciate_flag :='NO';
8259     END IF;
8260 
8261     l_asset_fin_rec.contract_id            := p_khr_id;
8262 
8263     l_asset_dist_rec.units_assigned :=l_model_line_rec.number_of_items;
8264     OPEN c_fa_expense_ccid(
8265            l_asset_hdr_rec.book_type_code,
8266            l_inv_item_rec.asset_category_id);
8267     FETCH c_fa_expense_ccid INTO l_fa_expense_ccid;
8268     CLOSE c_fa_expense_ccid;
8269 
8270     --l_asset_dist_rec.expense_ccid :=140417;
8271     l_asset_dist_rec.expense_ccid :=l_fa_expense_ccid.DEPRN_EXPENSE_ACCOUNT_CCID;
8272     l_asset_dist_rec.location_ccid :=p_fa_location_id;
8273     l_asset_dist_tbl(1) := l_asset_dist_rec;
8274 
8275     /*IF (l_counter=1) THEN
8276       OPEN c_asset_fmv_amount(p_kle_id);
8277       FETCH c_asset_fmv_amount INTO l_oec;
8278       CLOSE c_asset_fmv_amount;
8279     END IF;*/
8280 
8281     l_asset_fin_rec.cost := l_oec;
8282     l_asset_fin_rec.original_cost := l_oec;
8283    IF (is_debug_statement_on) THEN
8284         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling fa_addition_pub.do_addition');
8285    END IF;
8286    fa_addition_pub.do_addition
8287       (p_api_version             => l_api_version,
8288        p_init_msg_list           => OKL_API.G_FALSE,
8289        p_commit                  => OKL_API.G_FALSE,
8290        p_validation_level        => FND_API.G_VALID_LEVEL_FULL,
8291        x_return_status           => x_return_status,
8292        x_msg_count               => x_msg_count,
8293        x_msg_data                => x_msg_data,
8294        p_calling_fn              => l_calling_interface,
8295        px_trans_rec              => l_trans_rec,
8296        px_dist_trans_rec         => l_dist_trans_rec,
8297        px_asset_hdr_rec          => l_asset_hdr_rec,
8298        px_asset_desc_rec         => l_asset_desc_rec,
8299        px_asset_type_rec         => l_asset_type_rec,
8300        px_asset_cat_rec          => l_asset_cat_rec,
8301        px_asset_hierarchy_rec    => l_asset_hierarchy_rec,
8302        px_asset_fin_rec          => l_asset_fin_rec,
8303        px_asset_deprn_rec        => l_asset_deprn_rec,
8304        px_asset_dist_tbl         => l_asset_dist_tbl,
8305        px_inv_tbl                => l_inv_tbl
8306       );
8307    IF (is_debug_statement_on) THEN
8308         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called fa_addition_pub.do_addition x_return_status = '||x_return_status);
8309         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_asset_hdr_rec.asset_id = '||l_asset_hdr_rec.asset_id);
8310    END IF;
8311 
8312     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8313       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8314     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8315       RAISE OKL_API.G_EXCEPTION_ERROR;
8316     END IF;
8317 
8318 
8319     l_asset_id := l_asset_hdr_rec.asset_id;
8320 
8321 
8322     IF (l_counter =1) THEN
8323       OPEN c_cim_asset_id(p_kle_id);
8324       FETCH c_cim_asset_id INTO l_cim_asset_id;
8325       CLOSE c_cim_asset_id;
8326 
8327       l_cim_rec.id          := l_cim_asset_id.ID;
8328       l_cim_rec.object1_id1 := to_char(l_asset_id);
8329       l_cim_rec.object1_id2 := '#';
8330       IF (is_debug_statement_on) THEN
8331         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling okl_okc_migration_pvt.update_contract_item');
8332       END IF;
8333 
8334       okl_okc_migration_pvt.update_contract_item(
8335             p_api_version                  => 1.0,
8336             p_init_msg_list                => okc_api.g_false,
8337             x_return_status                =>x_return_status,
8338             x_msg_count                    =>x_msg_count,
8339             x_msg_data                     =>x_msg_data,
8340             p_cimv_rec                     =>l_cim_rec,
8341             x_cimv_rec                     =>x_cim_rec);
8342       IF (is_debug_statement_on) THEN
8343         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called okl_okc_migration_pvt.update_contract_item x_return_status = '||x_return_status);
8344       END IF;
8345 
8346       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8347         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8348       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8349         RAISE OKL_API.G_EXCEPTION_ERROR;
8350       END IF;
8351 
8352       -- Now create 'Internal Asset Creation' transaction lines
8353 
8354       l_talv_rec.tal_type := G_TRANS_TYPE;
8355       l_talv_rec.asset_number := l_asset_description.NAME;
8356       l_talv_rec.dnz_khr_id            := p_khr_id;
8357       l_talv_rec.tas_id                := p_tas_id;
8358       l_talv_rec.line_number           := p_line_number;
8359       l_talv_rec.original_cost         := l_oec;
8360       l_talv_rec.current_units         := l_model_line_rec.number_of_items;
8361       l_talv_rec.year_manufactured     := l_model_line_rec.year_of_manufacture;
8362       l_talv_rec.Depreciation_Cost     := l_oec;
8363       l_talv_rec.kle_id                := p_kle_id;
8364       IF (is_debug_statement_on) THEN
8365         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_TXL_ASSETS_PUB.create_txl_asset_def');
8366       END IF;
8367       OKL_TXL_ASSETS_PUB.create_txl_asset_def(
8368                          p_api_version    => 1,
8369                          p_init_msg_list  => p_init_msg_list,
8370                          x_return_status  => x_return_status,
8371                          x_msg_count      => x_msg_count,
8372                          x_msg_data       => x_msg_data,
8373                          p_tlpv_rec       => l_talv_rec,
8374                          x_tlpv_rec       => x_talv_rec);
8375       IF (is_debug_statement_on) THEN
8376         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_TXL_ASSETS_PUB.create_txl_asset_def x_return_status = '||x_return_status);
8377         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'x_talv_rec.ID = '||x_talv_rec.ID);
8378       END IF;
8379 
8380         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8381           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8382         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8383           RAISE OKL_API.G_EXCEPTION_ERROR;
8384         END IF;
8385 
8386     END IF;
8387 
8388     IF (l_counter > 1) THEN
8389       IF (is_debug_statement_on) THEN
8390         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Before opening c_get_fa_books l_asset_hdr_rec.book_type_code = '||l_asset_hdr_rec.book_type_code);
8391       END IF;
8392 
8393     OPEN c_get_fa_books(l_asset_hdr_rec.book_type_code, l_asset_id);
8394     FETCH c_get_fa_books INTO l_get_fa_books;
8395     IF c_get_fa_books%NOTFOUND THEN
8396       IF (is_debug_statement_on) THEN
8397         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'No Data Found in Cursor c_get_fa_books');
8398       END IF;
8399       CLOSE c_get_fa_books;
8400     ELSE
8401       l_txdv_rec.OBJECT_VERSION_NUMBER := 1;
8402       l_txdv_rec.TAL_ID := x_talv_rec.ID;
8403       l_txdv_rec.LINE_DETAIL_NUMBER := l_line_detail_number;
8404       l_txdv_rec.ASSET_NUMBER := l_talv_rec.asset_number;
8405 
8406       l_txdv_rec.QUANTITY := l_model_line_rec.number_of_items;
8407 
8408       l_txdv_rec.COST := l_get_fa_books.COST;
8409 
8410       l_txdv_rec.TAX_BOOK := l_asset_hdr_rec.book_type_code;
8411 
8412       l_txdv_rec.LIFE_IN_MONTHS_TAX := l_get_fa_books.LIFE_IN_MONTHS;
8413 
8414       l_txdv_rec.DEPRN_METHOD_TAX := l_get_fa_books.DEPRN_METHOD_CODE;
8415 
8416       l_txdv_rec.SALVAGE_VALUE := l_get_fa_books.SALVAGE_VALUE;
8417 
8418       CLOSE c_get_fa_books;
8419 
8420       IF (is_debug_statement_on) THEN
8421         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_TXD_ASSETS_PUB.create_txd_asset_def');
8422       END IF;
8423 
8424       OKL_TXD_ASSETS_PUB.create_txd_asset_def(
8425                            p_api_version    => 1,
8426                            p_init_msg_list  => p_init_msg_list,
8427                            x_return_status  => x_return_status,
8428                            x_msg_count      => x_msg_count,
8429                            x_msg_data       => x_msg_data,
8430                            p_adpv_rec       => l_txdv_rec,
8431                            x_adpv_rec       => x_txdv_rec);
8432       IF (is_debug_statement_on) THEN
8433         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called  OKL_TXD_ASSETS_PUB.create_txd_asset_def x_return_status = '||x_return_status);
8434         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'x_txdv_rec.ID = '||x_txdv_rec.ID);
8435       END IF;
8436       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8437         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8438       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8439         RAISE OKL_API.G_EXCEPTION_ERROR;
8440       END IF;
8441 
8442       l_line_detail_number := l_line_detail_number + 1;
8443     END IF;
8444     END IF;
8445 
8446   END IF;
8447   END LOOP;
8448 
8449   OKL_API.END_ACTIVITY (x_msg_count, x_msg_data );
8450     IF (is_debug_procedure_on) THEN
8451        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
8452     END IF;
8453   EXCEPTION
8454     WHEN OKL_API.G_EXCEPTION_ERROR THEN
8455         IF (is_debug_exception_on) THEN
8456              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
8457         END IF;
8458 
8459       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
8460                                  l_api_name,
8461                                  G_PKG_NAME,
8462                                  'OKL_API.G_RET_STS_ERROR',
8463                                  x_msg_count,
8464                                  x_msg_data,
8465                                  '_PVT');
8466     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
8467         IF (is_debug_exception_on) THEN
8468              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
8469         END IF;
8470 
8471       x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
8472                                 l_api_name,
8473                                 G_PKG_NAME,
8474                                 'OKL_API.G_RET_STS_UNEXP_ERROR',
8475                                 x_msg_count,
8476                                 x_msg_data,
8477                                 '_PVT');
8478     WHEN OTHERS THEN
8479         IF (is_debug_exception_on) THEN
8480            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
8481         END IF;
8482 
8483       x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
8484                                 l_api_name,
8485                                 G_PKG_NAME,
8486                                 'OTHERS',
8487                                 x_msg_count,
8488                                 x_msg_data,
8489                                 '_PVT');
8490 
8491 
8492 END;
8493 
8494  --RKUTTIYA Added for bug 6674370
8495     --Call to  SLA Populate Sources
8496     PROCEDURE Populate_Sources(p_api_version     IN NUMBER,
8497                                p_init_msg_list   IN  VARCHAR2,
8498                                p_khr_id          IN OKC_K_HEADERS_B.ID%TYPE,
8499                                p_kle_id          IN OKL_K_LINES.ID%TYPE,
8500                                p_corporate_book  IN OKL_SYSTEM_PARAMS_ALL.CORPORATE_BOOK%TYPE,
8501                                p_tas_id          IN OKL_TRX_ASSETS.ID%TYPE,
8502                                p_tal_id          IN NUMBER,
8503                                p_line_type       IN VARCHAR2,
8504                                p_fa_trx_id       IN NUMBER,
8505                                p_asset_id        IN NUMBER,
8506                                p_quote_id        IN NUMBER,
8507                                x_return_status   OUT NOCOPY VARCHAR2,
8508                                x_msg_count       OUT NOCOPY NUMBER,
8509                                x_msg_data        OUT NOCOPY VARCHAR2) IS
8510 
8511      l_calling_interface    VARCHAR2(30) := 'OKLRLTNB:Populate_Sources';
8512      l_api_name             CONSTANT VARCHAR2(30) := 'POPULATE_SOURCES';
8513      l_api_version          CONSTANT NUMBER := 1.0;
8514      l_pkg_name  VARCHAR2(30) := 'OKL_AM_LEASE_TRMNT_PVT';
8515 
8516      CURSOR c_try_id(p_tas_id IN NUMBER) IS
8517      SELECT try_id
8518      FROM OKL_TRX_ASSETS
8519      WHERE id = p_tas_id;
8520 
8521      --Cursor for quote details
8522     CURSOR c_quote_details(p_quote_id IN NUMBER) IS
8523     SELECT QTP_CODE,
8524            QUOTE_NUMBER,
8525            DATE_ACCEPTED,
8526            REPO_QUOTE_INDICATOR_YN
8527     FROM OKL_TRX_QUOTES_B
8528     WHERE ID = p_quote_id;
8529 
8530      l_try_id    NUMBER;
8531      l_quote_id  NUMBER;
8532 
8533      l_fxhv_rec         okl_fxh_pvt.fxhv_rec_type;
8534      l_fxlv_rec         okl_fxl_pvt.fxlv_rec_type;
8535 
8536      l_quote_num         VARCHAR2(30);
8537      l_date_accepted     DATE;
8538      l_quote_type_code   VARCHAR2(30);
8539      l_repossess_flag    VARCHAR2(1);
8540    BEGIN
8541       x_return_status := OKL_API.START_ACTIVITY (
8542                                l_api_name
8543                                ,p_init_msg_list
8544                                ,'_PVT'
8545                                ,x_return_status);
8546 
8547       -- Check if activity started successfully
8548       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8549          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8550       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8551          RAISE OKL_API.G_EXCEPTION_ERROR;
8552       END IF;
8553 
8554     -- get the try id
8555       OPEN c_try_id(p_tas_id);
8556       FETCH c_try_id INTO l_try_id;
8557       CLOSE c_try_id;
8558 
8559        OPEN c_quote_details(p_quote_id);
8560        FETCH c_quote_details INTO l_quote_type_code,l_quote_num, l_date_accepted,l_repossess_flag;
8561        CLOSE c_quote_details;
8562 
8563          -- header record
8564            l_fxhv_rec.source_id    := p_tas_id;
8565            l_fxhv_rec.source_table := 'OKL_TRX_ASSETS';
8566            l_fxhv_rec.khr_id := p_khr_id;
8567            l_fxhv_rec.try_id := l_try_id;
8568            l_fxhv_rec.term_quote_type_code := l_quote_type_code;
8569            l_fxhv_rec.term_quote_num  := l_quote_num;
8570            l_fxhv_rec.term_quote_accept_date := l_date_accepted;
8571            l_fxhv_rec.repossess_flag         := l_repossess_flag;
8572 
8573 
8574            --line record
8575 
8576            l_fxlv_rec.source_id         := p_tal_id;
8577            IF p_line_type = 'L' THEN
8578              l_fxlv_rec.source_table      := 'OKL_TXL_ASSETS_B';
8579            ELSIF p_line_type = 'D' THEN
8580 		     l_fxlv_rec.source_table  := 'OKL_TXD_ASSETS_B';
8581 		   END IF;
8582            l_fxlv_rec.asset_id          :=  p_asset_id;
8583            l_fxlv_rec.kle_id            :=  p_kle_id;
8584            l_fxlv_rec.fa_transaction_id := p_fa_trx_id;
8585            l_fxlv_rec.asset_book_type_name := p_corporate_book;
8586 
8587            --call Populate sources api
8588            okl_sla_acc_sources_pvt.populate_sources(p_api_version   => p_api_version,
8589                                                     p_init_msg_list => okc_api.g_false,
8590                                                     p_fxhv_rec      => l_fxhv_rec,
8591                                                     p_fxlv_rec      => l_fxlv_rec,
8592                                                     x_return_status => x_return_status,
8593                                                     x_msg_count     => x_msg_count,
8594                                                     x_msg_data      => x_msg_data);
8595 
8596            IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8597              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8598            ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8599              RAISE OKL_API.G_EXCEPTION_ERROR;
8600            END IF;
8601 
8602    OKL_API.END_ACTIVITY (x_msg_count, x_msg_data );
8603  END;
8604 
8605 
8606 PROCEDURE Create_Repo_asset(
8607               p_init_msg_list               IN  VARCHAR2,
8608               p_khr_id          IN OKC_K_HEADERS_B.ID%TYPE,
8609               p_kle_id          IN OKL_K_LINES.ID%TYPE,
8610               p_corporate_book  IN OKL_SYSTEM_PARAMS_ALL.CORPORATE_BOOK%TYPE,
8611               p_tax_book_1      IN OKL_SYSTEM_PARAMS_ALL.TAX_BOOK_1%TYPE,
8612               p_tax_book_2      IN OKL_SYSTEM_PARAMS_ALL.TAX_BOOK_2%TYPE,
8613               p_rep_book        IN OKL_SYSTEM_PARAMS_ALL.RPT_PROD_BOOK_TYPE_CODE%TYPE,
8614               p_fa_location_id  IN OKL_SYSTEM_PARAMS_ALL.FA_LOCATION_ID%TYPE,
8615               p_asset_key_id    IN OKL_SYSTEM_PARAMS_ALL.ASSET_KEY_ID%TYPE,
8616               p_depreciate_yn   IN OKL_SYSTEM_PARAMS_ALL.DEPRECIATE_YN%TYPE,
8617               p_tas_id          IN OKL_TRX_ASSETS.ID%TYPE,
8618               p_line_number     IN NUMBER,
8619               p_quote_id        IN NUMBER,
8620               x_return_status   OUT NOCOPY VARCHAR2,
8621               x_msg_count       OUT NOCOPY NUMBER,
8622               x_msg_data        OUT NOCOPY VARCHAR2
8623            ) IS
8624 
8625        CURSOR l_sub_line_csr(p_chr_id   IN NUMBER
8626                             ,p_lty_code IN VARCHAR2
8627                             ,p_cle_id   IN NUMBER) IS
8628        SELECT cle.id,
8629               cim.object1_id1,
8630               cim.object1_id2,
8631               cim.number_of_items,
8632               fa_kle.year_of_manufacture
8633        FROM   okc_k_lines_b  cle,
8634               okc_k_items cim,
8635               okc_line_styles_b lse,
8636               okc_statuses_b sts,
8637               okl_k_lines fa_kle,
8638               okc_k_lines_b fa_cle,
8639               okc_line_styles_b fa_lse
8640        WHERE cle.dnz_chr_id = p_chr_id
8641        AND   cle.cle_id = p_cle_id
8642        AND   cle.lse_id = lse.id
8643        AND   lse.lty_code = p_lty_code
8644        AND   cim.cle_id = cle.id
8645        AND   cim.dnz_chr_id = cle.dnz_chr_id
8646        AND   sts.code = cle.sts_code
8647        AND   sts.ste_code NOT IN ('HOLD', 'TERMINATED', 'EXPIRED', 'CANCELLED')
8648        AND   fa_cle.id = p_cle_id
8649        AND   fa_kle.id = fa_cle.id
8650        AND   fa_cle.lse_id = fa_lse.id
8651        AND   fa_lse.lty_code = 'FREE_FORM1';
8652 
8653        l_model_line_rec  l_sub_line_csr%ROWTYPE;
8654 
8655        CURSOR l_inv_item_csr(p_inv_item_id IN NUMBER,
8656                              p_org_id IN NUMBER) IS
8657        SELECT asset_category_id
8658        FROM mtl_system_items
8659        WHERE inventory_item_id = p_inv_item_id
8660        AND organization_id = p_org_id;
8661 
8662        l_inv_item_rec l_inv_item_csr%ROWTYPE;
8663 
8664        CURSOR c_asset_description(p_cle_id IN NUMBER) IS
8665        SELECT NAME, ITEM_DESCRIPTION
8666        FROM OKC_K_LINES_TL
8667        WHERE ID = p_cle_id
8668        AND LANGUAGE = USERENV('LANG');
8669 
8670        l_asset_description c_asset_description%ROWTYPE;
8671 
8672        CURSOR c_asset_ret_dtls(p_cle_id IN NUMBER) IS
8673        SELECT DATE_RETURNED, ASSET_FMV_AMOUNT
8674        FROM OKL_ASSET_RETURNS_B
8675        WHERE KLE_ID = p_cle_id
8676        AND ARS_CODE = 'REPOSSESSED';
8677 
8678        l_asset_ret_dtls c_asset_ret_dtls%ROWTYPE;
8679 
8680        CURSOR c_fa_expense_ccid(
8681                 p_book_type_code FA_CATEGORY_BOOKS.BOOK_TYPE_CODE%TYPE,
8682                 p_category_id    FA_CATEGORY_BOOKS.CATEGORY_ID%TYPE) IS
8683        SELECT DEPRN_EXPENSE_ACCOUNT_CCID
8684        FROM   FA_CATEGORY_BOOKS
8685        WHERE  BOOK_TYPE_CODE = p_book_type_code
8686        AND    CATEGORY_ID    = p_category_id;
8687 
8688        l_fa_expense_ccid c_fa_expense_ccid%ROWTYPE;
8689 
8690        CURSOR c_cim_asset_id(p_cle_id IN NUMBER) IS
8691        SELECT ITM.ID
8692        FROM   OKC_K_ITEMS ITM,
8693               OKC_K_LINES_B CLE,
8694               OKC_LINE_STYLES_B LSE
8695        WHERE ITM.CLE_ID = cle.ID
8696        AND   CLE.CLE_ID = p_cle_id
8697        AND   CLE.LSE_ID = LSE.ID
8698        AND   LSE.LTY_CODE = 'FIXED_ASSET';
8699 
8700        l_cim_asset_id c_cim_asset_id%ROWTYPE;
8701 
8702        CURSOR c_get_fa_books( p_book_type_code FA_BOOKS.BOOK_TYPE_CODE%TYPE,
8703                               p_asset_id FA_BOOKS.ASSET_ID%TYPE) IS
8704        SELECT DEPRN_METHOD_CODE,
8705               LIFE_IN_MONTHS,
8706               COST,
8707               SALVAGE_VALUE
8708        FROM   FA_BOOKS
8709        WHERE  ASSET_ID = p_asset_id
8710        AND    BOOK_TYPE_CODE = p_book_type_code ;
8711 
8712        l_get_fa_books c_get_fa_books%ROWTYPE;
8713 
8714        l_set_of_books_id OKL_SYS_ACCT_OPTS.SET_OF_BOOKS_ID%TYPE;
8715        l_max_books NUMBER;
8716 
8717 l_trans_rec                FA_API_TYPES.trans_rec_type;
8718 l_dist_trans_rec           FA_API_TYPES.trans_rec_type;
8719 l_asset_hdr_rec            FA_API_TYPES.asset_hdr_rec_type;
8720 l_asset_desc_rec           FA_API_TYPES.asset_desc_rec_type;
8721 l_asset_cat_rec            FA_API_TYPES.asset_cat_rec_type;
8722 l_asset_type_rec           FA_API_TYPES.asset_type_rec_type;
8723 l_asset_hierarchy_rec      fa_api_types.asset_hierarchy_rec_type;
8724 l_asset_fin_rec            FA_API_TYPES.asset_fin_rec_type;
8725 l_asset_deprn_rec          FA_API_TYPES.asset_deprn_rec_type;
8726 l_asset_dist_rec           FA_API_TYPES.asset_dist_rec_type;
8727 l_asset_dist_tbl           FA_API_TYPES.asset_dist_tbl_type;
8728 l_inv_tbl                  FA_API_TYPES.inv_tbl_type;
8729 
8730 l_calling_interface Varchar2(30) := 'OKLRLTNB:Create_Repo_asset';
8731 l_api_name             CONSTANT VARCHAR2(30) := 'CREATE_REPO_ASSET';
8732 l_api_version          CONSTANT NUMBER := 1.0;
8733 l_pkg_name  VARCHAR2(30) := 'OKL_AM_LEASE_TRMNT_PVT';
8734 l_asset_id NUMBER;
8735 l_talv_rec               talv_rec_type;
8736 x_talv_rec               talv_rec_type;
8737 
8738 l_oec NUMBER;
8739 l_cim_rec                      okl_okc_migration_pvt.cimv_rec_type;
8740 x_cim_rec                      okl_okc_migration_pvt.cimv_rec_type;
8741 
8742 l_txdv_rec     adpv_rec_type;
8743 x_txdv_rec     adpv_rec_type;
8744 l_line_detail_number NUMBER := 1;
8745 
8746 l_asset_fin_rec_adj		    FA_API_TYPES.asset_fin_rec_type;
8747 l_asset_fin_rec_empty_adj    FA_API_TYPES.asset_fin_rec_type;
8748 l_trans_empty_rec			FA_API_TYPES.trans_rec_type;
8749 
8750    l_asset_fin_rec_new		    FA_API_TYPES.asset_fin_rec_type;
8751    l_asset_fin_mrc_tbl_new	    FA_API_TYPES.asset_fin_tbl_type;
8752 
8753    l_inv_trans_rec		        FA_API_TYPES.inv_trans_rec_type;
8754    l_asset_deprn_rec_adj	    FA_API_TYPES.asset_deprn_rec_type;
8755    l_asset_deprn_rec_new	    FA_API_TYPES.asset_deprn_rec_type;
8756    l_asset_deprn_mrc_tbl_new	FA_API_TYPES.asset_deprn_tbl_type;
8757    l_group_reclass_options_rec  FA_API_TYPES.group_reclass_options_rec_type;
8758    l_tal_tld_id                     NUMBER;
8759    l_line_type                      VARCHAR2(1);
8760 
8761    -- asawanka added for debug feature start
8762     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'Create_Repo_asset';
8763     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
8764     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
8765     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
8766     -- asawanka added for debug feature end
8767 
8768 BEGIN
8769 
8770      IF (is_debug_procedure_on) THEN
8771        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
8772      END IF;
8773   x_return_status := OKL_API.START_ACTIVITY (
8774                                l_api_name
8775                                ,p_init_msg_list
8776                                ,'_PVT'
8777                                ,x_return_status);
8778   -- Check if activity started successfully
8779   IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8780        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8781   ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8782        RAISE OKL_API.G_EXCEPTION_ERROR;
8783   END IF;
8784 
8785   IF (is_debug_statement_on) THEN
8786         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_khr_id = '||p_khr_id);
8787         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_kle_id = '||p_kle_id);
8788         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_corporate_book = '||p_corporate_book);
8789         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_tax_book_1 = '||p_tax_book_1);
8790         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_tax_book_2 = '||p_tax_book_2);
8791         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_rep_book = '||p_rep_book);
8792         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_fa_location_id = '||p_fa_location_id);
8793         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_asset_key_id = '||p_asset_key_id);
8794         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_depreciate_yn = '||p_depreciate_yn);
8795         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_tas_id = '||p_tas_id);
8796         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_line_number = '||p_line_number);
8797         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_quote_id = '||p_quote_id);
8798   END IF;
8799   x_return_status := OKL_API.G_RET_STS_SUCCESS;
8800 
8801   OPEN l_sub_line_csr(p_chr_id => p_khr_id,
8802                       p_lty_code => 'ITEM',
8803                       p_cle_id => p_kle_id);
8804   FETCH l_sub_line_csr INTO l_model_line_rec;
8805   CLOSE l_sub_line_csr;
8806 
8807   OPEN l_inv_item_csr(p_inv_item_id => TO_NUMBER(l_model_line_rec.object1_id1),
8808                       p_org_id      => TO_NUMBER(l_model_line_rec.object1_id2));
8809   FETCH l_inv_item_csr INTO l_inv_item_rec;
8810   CLOSE l_inv_item_csr;
8811 
8812   OPEN c_asset_description(p_kle_id);
8813   FETCH c_asset_description INTO l_asset_description;
8814   CLOSE c_asset_description;
8815 
8816   OPEN c_asset_ret_dtls(p_kle_id);
8817   FETCH c_asset_ret_dtls INTO l_asset_ret_dtls;
8818   CLOSE c_asset_ret_dtls;
8819   l_oec := l_asset_ret_dtls.asset_fmv_amount;
8820 
8821   l_set_of_books_id := OKL_ACCOUNTING_UTIL.GET_SET_OF_BOOKS_ID;
8822 
8823   /*IF (p_tax_book_2 IS NOT NULL AND p_tax_book_2 <> OKL_API.G_MISS_CHAR)
8824   THEN
8825     l_max_books := 3;
8826   ELSE
8827     l_max_books := 2;
8828   END IF;*/
8829   l_max_books := 4;
8830 
8831   FOR l_counter IN 1..l_max_books
8832   LOOP
8833 
8834     IF (l_counter=1) OR
8835        (l_counter=2 AND p_tax_book_1 IS NOT NULL
8836             AND p_tax_book_1 <> OKL_API.G_MISS_CHAR) OR
8837        (l_counter=3 AND p_tax_book_2 IS NOT NULL
8838             AND p_tax_book_2 <> OKL_API.G_MISS_CHAR) OR
8839        (l_counter=4 AND p_rep_book IS NOT NULL
8840             AND p_rep_book <> OKL_API.G_MISS_CHAR) THEN
8841     l_trans_rec.transaction_type_code := 'ADDITION';
8842     l_asset_desc_rec.asset_key_ccid:= p_asset_key_id;
8843     l_asset_desc_rec.current_units:= l_model_line_rec.number_of_items;
8844     l_asset_hdr_rec.asset_id          := NULL;
8845 
8846     IF (l_counter = 1) THEN
8847       l_asset_hdr_rec.book_type_code :=p_corporate_book;
8848     ELSIF (l_counter=2) THEN
8849       l_asset_hdr_rec.asset_id := l_asset_id;
8850       l_asset_hdr_rec.book_type_code :=p_tax_book_1;
8851     ELSIF (l_counter=3) THEN
8852       l_asset_hdr_rec.asset_id := l_asset_id;
8853       l_asset_hdr_rec.book_type_code :=p_tax_book_2;
8854     ELSE
8855       l_asset_hdr_rec.asset_id := l_asset_id;
8856       l_asset_hdr_rec.book_type_code :=p_rep_book;
8857     END IF;
8858 
8859     l_asset_hdr_rec.set_of_books_id :=l_set_of_books_id;
8860 
8861     l_asset_desc_rec.asset_number       := l_asset_description.NAME;
8862     l_asset_desc_rec.description        := l_asset_description.ITEM_DESCRIPTION;
8863 
8864     l_asset_cat_rec.category_id :=l_inv_item_rec.asset_category_id;
8865 
8866     l_asset_fin_rec.set_of_books_id :=l_set_of_books_id;
8867     l_asset_fin_rec.date_placed_in_service := l_asset_ret_dtls.DATE_RETURNED;
8868     IF (NVL(p_depreciate_yn, 'N')  = 'N' ) THEN
8869       l_asset_fin_rec.depreciate_flag :='NO';
8870     ELSE
8871       l_asset_fin_rec.depreciate_flag :='YES';
8872     END IF;
8873 
8874     l_asset_fin_rec.contract_id            := p_khr_id;
8875 
8876     l_asset_dist_rec.units_assigned :=l_model_line_rec.number_of_items;
8877     OPEN c_fa_expense_ccid(
8878            l_asset_hdr_rec.book_type_code,
8879            l_inv_item_rec.asset_category_id);
8880     FETCH c_fa_expense_ccid INTO l_fa_expense_ccid;
8881     CLOSE c_fa_expense_ccid;
8882 
8883     --l_asset_dist_rec.expense_ccid :=140417;
8884     l_asset_dist_rec.expense_ccid :=l_fa_expense_ccid.DEPRN_EXPENSE_ACCOUNT_CCID;
8885     l_asset_dist_rec.location_ccid :=p_fa_location_id;
8886     l_asset_dist_tbl(1) := l_asset_dist_rec;
8887 
8888     /*IF (l_counter=1) THEN
8889       OPEN c_asset_fmv_amount(p_kle_id);
8890       FETCH c_asset_fmv_amount INTO l_oec;
8891       CLOSE c_asset_fmv_amount;
8892     END IF;*/
8893 
8894     l_asset_fin_rec.cost := l_oec;
8895     l_asset_fin_rec.original_cost := l_oec;
8896 
8897      IF (is_debug_statement_on) THEN
8898         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling fa_addition_pub.do_addition');
8899    END IF;
8900 
8901    fa_addition_pub.do_addition
8902       (p_api_version             => l_api_version,
8903        p_init_msg_list           => OKL_API.G_FALSE,
8904        p_commit                  => OKL_API.G_FALSE,
8905        p_validation_level        => FND_API.G_VALID_LEVEL_FULL,
8906        x_return_status           => x_return_status,
8907        x_msg_count               => x_msg_count,
8908        x_msg_data                => x_msg_data,
8909        p_calling_fn              => l_calling_interface,
8910        px_trans_rec              => l_trans_rec,
8911        px_dist_trans_rec         => l_dist_trans_rec,
8912        px_asset_hdr_rec          => l_asset_hdr_rec,
8913        px_asset_desc_rec         => l_asset_desc_rec,
8914        px_asset_type_rec         => l_asset_type_rec,
8915        px_asset_cat_rec          => l_asset_cat_rec,
8916        px_asset_hierarchy_rec    => l_asset_hierarchy_rec,
8917        px_asset_fin_rec          => l_asset_fin_rec,
8918        px_asset_deprn_rec        => l_asset_deprn_rec,
8919        px_asset_dist_tbl         => l_asset_dist_tbl,
8920        px_inv_tbl                => l_inv_tbl
8921       );
8922 
8923      IF (is_debug_statement_on) THEN
8924         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called fa_addition_pub.do_addition x_return_status = '||x_return_status);
8925         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_asset_hdr_rec.asset_id = '||l_asset_hdr_rec.asset_id);
8926      END IF;
8927 
8928     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8929       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8930     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8931       RAISE OKL_API.G_EXCEPTION_ERROR;
8932     END IF;
8933 
8934 
8935     l_asset_id := l_asset_hdr_rec.asset_id;
8936 okl_debug_pub.logmessage('AKP:After do_addition: x_return_status=' || x_return_status || ' l_asset_id=' || l_asset_id);
8937 
8938     IF (l_counter =1) THEN
8939       OPEN c_cim_asset_id(p_kle_id);
8940       FETCH c_cim_asset_id INTO l_cim_asset_id;
8941       CLOSE c_cim_asset_id;
8942 
8943       l_cim_rec.id          := l_cim_asset_id.ID;
8944       l_cim_rec.object1_id1 := to_char(l_asset_id);
8945       l_cim_rec.object1_id2 := '#';
8946 
8947       IF (is_debug_statement_on) THEN
8948         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling okl_okc_migration_pvt.update_contract_item');
8949       END IF;
8950 
8951       okl_okc_migration_pvt.update_contract_item(
8952             p_api_version                  => 1.0,
8953             p_init_msg_list                => okc_api.g_false,
8954             x_return_status                =>x_return_status,
8955             x_msg_count                    =>x_msg_count,
8956             x_msg_data                     =>x_msg_data,
8957             p_cimv_rec                     =>l_cim_rec,
8958             x_cimv_rec                     =>x_cim_rec);
8959 
8960        IF (is_debug_statement_on) THEN
8961         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called okl_okc_migration_pvt.update_contract_item x_return_status = '||x_return_status);
8962       END IF;
8963 
8964       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8965         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8966       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8967         RAISE OKL_API.G_EXCEPTION_ERROR;
8968       END IF;
8969 
8970 okl_debug_pub.logmessage('AKP:After update_item: x_return_status=' || x_return_status || ' x_cim_rec.object1_id1=' || x_cim_rec.object1_id1);
8971 
8972       -- Now create 'Internal Asset Creation' transaction lines
8973 
8974       l_talv_rec.tal_type := G_TRANS_TYPE;
8975       l_talv_rec.asset_number := l_asset_description.NAME;
8976       l_talv_rec.dnz_khr_id            := p_khr_id;
8977       l_talv_rec.tas_id                := p_tas_id;
8978       l_talv_rec.line_number           := p_line_number;
8979       l_talv_rec.original_cost         := l_oec;
8980       l_talv_rec.current_units         := l_model_line_rec.number_of_items;
8981       l_talv_rec.year_manufactured     := l_model_line_rec.year_of_manufacture;
8982       l_talv_rec.Depreciation_Cost     := l_oec;
8983       l_talv_rec.kle_id                := p_kle_id;
8984       IF (NVL(p_depreciate_yn, 'N')  = 'N' ) THEN
8985         l_talv_rec.depreciate_yn                := 'N';
8986       ELSE
8987         l_talv_rec.depreciate_yn                := 'Y';
8988       END IF;
8989 
8990        IF (is_debug_statement_on) THEN
8991         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_TXL_ASSETS_PUB.create_txl_asset_def');
8992       END IF;
8993 
8994       OKL_TXL_ASSETS_PUB.create_txl_asset_def(
8995                          p_api_version    => 1,
8996                          p_init_msg_list  => p_init_msg_list,
8997                          x_return_status  => x_return_status,
8998                          x_msg_count      => x_msg_count,
8999                          x_msg_data       => x_msg_data,
9000                          p_tlpv_rec       => l_talv_rec,
9001                          x_tlpv_rec       => x_talv_rec);
9002 
9003        IF (is_debug_statement_on) THEN
9004         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_TXL_ASSETS_PUB.create_txl_asset_def x_return_status = '||x_return_status);
9005         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'x_talv_rec.ID = '||x_talv_rec.ID);
9006       END IF;
9007 
9008         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9009           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9010         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9011           RAISE OKL_API.G_EXCEPTION_ERROR;
9012         END IF;
9013 okl_debug_pub.logmessage('AKP:x_return_status=' || x_return_status || 'x_talv_rec.ID=' || x_talv_rec.ID);
9014 
9015      --rkuttiya added for passing the line id to populate sources
9016      l_tal_tld_id := x_talv_rec.id;
9017      l_line_type  := 'L';
9018      --
9019 
9020    END IF;
9021 
9022     IF (l_counter > 1) THEN
9023       IF (is_debug_statement_on) THEN
9024         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Before opening c_get_fa_books l_asset_hdr_rec.book_type_code = '||l_asset_hdr_rec.book_type_code);
9025       END IF;
9026 okl_debug_pub.logmessage('AKP:Before open c_get_fa_books');
9027     OPEN c_get_fa_books(l_asset_hdr_rec.book_type_code, l_asset_id);
9028     FETCH c_get_fa_books INTO l_get_fa_books;
9029 okl_debug_pub.logmessage('AKP:After fetch c_get_fa_books');
9030     IF c_get_fa_books%NOTFOUND THEN
9031       CLOSE c_get_fa_books;
9032     ELSE
9033       l_txdv_rec.OBJECT_VERSION_NUMBER := 1;
9034       l_txdv_rec.TAL_ID := x_talv_rec.ID;
9035       l_txdv_rec.LINE_DETAIL_NUMBER := l_line_detail_number;
9036       l_txdv_rec.ASSET_NUMBER := l_talv_rec.asset_number;
9037 
9038       l_txdv_rec.QUANTITY := l_model_line_rec.number_of_items;
9039 
9040       l_txdv_rec.COST := l_get_fa_books.COST;
9041 
9042       l_txdv_rec.TAX_BOOK := l_asset_hdr_rec.book_type_code;
9043 
9044       l_txdv_rec.LIFE_IN_MONTHS_TAX := l_get_fa_books.LIFE_IN_MONTHS;
9045 
9046       l_txdv_rec.DEPRN_METHOD_TAX := l_get_fa_books.DEPRN_METHOD_CODE;
9047 
9048       l_txdv_rec.SALVAGE_VALUE := l_get_fa_books.SALVAGE_VALUE;
9049 
9050       CLOSE c_get_fa_books;
9051 
9052       IF (is_debug_statement_on) THEN
9053         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_TXD_ASSETS_PUB.create_txd_asset_def');
9054       END IF;
9055 
9056       OKL_TXD_ASSETS_PUB.create_txd_asset_def(
9057                            p_api_version    => 1,
9058                            p_init_msg_list  => p_init_msg_list,
9059                            x_return_status  => x_return_status,
9060                            x_msg_count      => x_msg_count,
9061                            x_msg_data       => x_msg_data,
9062                            p_adpv_rec       => l_txdv_rec,
9063                            x_adpv_rec       => x_txdv_rec);
9064 
9065       IF (is_debug_statement_on) THEN
9066         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called  OKL_TXD_ASSETS_PUB.create_txd_asset_def x_return_status = '||x_return_status);
9067         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'x_txdv_rec.ID = '||x_txdv_rec.ID);
9068       END IF;
9069       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9070         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9071       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9072         RAISE OKL_API.G_EXCEPTION_ERROR;
9073       END IF;
9074 
9075 okl_debug_pub.logmessage('AKP:x_return_status=' || x_return_status || 'x_txdv_rec.ID=' || x_txdv_rec.ID);
9076 
9077       l_line_detail_number := l_line_detail_number + 1;
9078 
9079       --added by rkuttiya for populate sources
9080       l_tal_tld_id := x_txdv_rec.id;
9081       l_line_type  := 'D';
9082     --
9083     END IF;
9084     END IF;
9085 
9086     --call Populate Sources
9087 
9088       Populate_Sources(p_api_version     => l_api_version,
9089                        p_init_msg_list   => p_init_msg_list,
9090                        p_khr_id          => p_khr_id,
9091                        p_kle_id          => p_kle_id,
9092                        p_corporate_book  => l_asset_hdr_rec.book_type_code,
9093                        p_tas_id          => p_tas_id,
9094                        p_tal_id          => l_tal_tld_id,
9095                        p_line_type       => l_line_type,
9096                        p_fa_trx_id       => l_trans_rec.tranSaction_header_id,
9097                        p_asset_id        => l_asset_id,
9098                        p_quote_id        => p_quote_id,
9099                        x_return_status   => x_return_status,
9100                        x_msg_count       => x_msg_count,
9101                        x_msg_data        => x_msg_data);
9102 
9103         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9104           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9105         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9106           RAISE OKL_API.G_EXCEPTION_ERROR;
9107         END IF;
9108 
9109       okl_debug_pub.logmessage('RK:x_return_status after populate sources' || x_return_status );
9110 
9111 
9112 /* okl_debug_pub.logmessage('RK:Before calling FA Adjustment to delink the contract id..');
9113   --CALL TO FA_ADJUSTMENT PUB to delink the contract id
9114       l_trans_rec := l_trans_empty_rec;
9115 
9116       l_trans_rec.transaction_subtype := 'AMORTIZE';
9117 
9118       l_asset_fin_rec_adj := l_asset_fin_rec;
9119       l_asset_fin_rec_adj.contract_id := FND_API.G_MISS_NUM;
9120 
9121 
9122                             fa_adjustment_pub.do_adjustment(
9123                                    p_api_version              => L_api_version,
9124     		                       p_init_msg_list            => OKC_API.G_FALSE,
9125     		                       p_commit                   => FND_API.G_FALSE,
9126     		                       p_validation_level         => FND_API.G_VALID_LEVEL_FULL,
9127     		                       p_calling_fn               => NULL,
9128     		                       x_return_status            => x_return_status,
9129     		                       x_msg_count                => x_msg_count,
9130     		                       x_msg_data                 => x_msg_data,
9131     		                       px_trans_rec               => l_trans_rec,
9132     		                       px_asset_hdr_rec           => l_asset_hdr_rec,
9133     		                       p_asset_fin_rec_adj        => l_asset_fin_rec_adj,
9134     		                       x_asset_fin_rec_new        => l_asset_fin_rec_new,
9135     		                       x_asset_fin_mrc_tbl_new    => l_asset_fin_mrc_tbl_new,
9136     		                       px_inv_trans_rec           => l_inv_trans_rec,
9137     		                       px_inv_tbl                 => l_inv_tbl,
9138     		                       p_asset_deprn_rec_adj      => l_asset_deprn_rec_adj,
9139     		                       x_asset_deprn_rec_new      => l_asset_deprn_rec_new,
9140     		                       x_asset_deprn_mrc_tbl_new  => l_asset_deprn_mrc_tbl_new,
9141                                    p_group_reclass_options_rec => l_group_reclass_options_rec);
9142       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9143         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9144       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9145         RAISE OKL_API.G_EXCEPTION_ERROR;
9146       END IF;
9147       okl_debug_pub.logmessage('RK:Return Status after calling FA Adjustment API'|| x_return_status);  */
9148 
9149 
9150   END IF;
9151 
9152 
9153   END LOOP;
9154 
9155   OKL_API.set_message( p_app_name      => 'OKL',
9156                        p_msg_name      => 'OKL_AM_ASSET_CR_SUCC',
9157                        p_token1        => 'ASSET_NUMBER',
9158                        p_token1_value  => l_asset_description.NAME);
9159 
9160 
9161   OKL_API.END_ACTIVITY (x_msg_count, x_msg_data );
9162   IF (is_debug_procedure_on) THEN
9163        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
9164   END IF;
9165 
9166   EXCEPTION
9167     WHEN OKL_API.G_EXCEPTION_ERROR THEN
9168        IF (is_debug_exception_on) THEN
9169              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
9170         END IF;
9171 
9172       OKL_API.set_message( p_app_name      => 'OKL',
9173                            p_msg_name      => 'OKL_AM_ASSET_CR_FAIL',
9174                            p_token1        => 'ASSET_NUMBER',
9175                            p_token1_value  => l_asset_description.NAME);
9176 
9177       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
9178                                  l_api_name,
9179                                  G_PKG_NAME,
9180                                  'OKL_API.G_RET_STS_ERROR',
9181                                  x_msg_count,
9182                                  x_msg_data,
9183                                  '_PVT');
9184     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
9185        IF (is_debug_exception_on) THEN
9186              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
9187         END IF;
9188 
9189       OKL_API.set_message( p_app_name      => 'OKL',
9190                            p_msg_name      => 'OKL_AM_ASSET_CR_FAIL',
9191                            p_token1        => 'ASSET_NUMBER',
9192                            p_token1_value  => l_asset_description.NAME);
9193 
9194       x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
9195                                 l_api_name,
9196                                 G_PKG_NAME,
9197                                 'OKL_API.G_RET_STS_UNEXP_ERROR',
9198                                 x_msg_count,
9199                                 x_msg_data,
9200                                 '_PVT');
9201     WHEN OTHERS THEN
9202        IF (is_debug_exception_on) THEN
9203            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
9204         END IF;
9205 
9206       OKL_API.set_message( p_app_name      => 'OKL',
9207                            p_msg_name      => 'OKL_AM_ASSET_CR_FAIL',
9208                            p_token1        => 'ASSET_NUMBER',
9209                            p_token1_value  => l_asset_description.NAME);
9210 
9211       x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
9212                                 l_api_name,
9213                                 G_PKG_NAME,
9214                                 'OTHERS',
9215                                 x_msg_count,
9216                                 x_msg_data,
9217                                 '_PVT');
9218 
9219 
9220 END;
9221 
9222 
9223   -- Start of comments
9224   --
9225   -- Procedure Name : process_asset_dispose
9226   -- Desciption     : Calls the Asset disposition API to dispose off assets
9227   -- Business Rules :
9228   -- Parameters  :
9229   -- Version  : 1.0
9230   -- History        : SECHAWLA 31-DEC-02 Bug #2726739
9231   --                  Added logic to convert proceeds of sale amount to functional currency
9232   --                : RMUNJULU 04-FEB-03 2781557 Added code to get and set
9233   --                  proceeds of sale properly
9234   --                : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
9235   --                : rmunjulu EDAT Added code to set trn_date as currency conversion trn date
9236   --                  also send quote eff date and quote acceptance date to disposal api
9237   --                : rmunjulu BUYOUT_PROCESS
9238   -- End of comments
9239   PROCEDURE process_asset_dispose(
9240            p_api_version                 IN  NUMBER,
9241            p_init_msg_list               IN  VARCHAR2,
9242            x_return_status               OUT NOCOPY VARCHAR2,
9243            x_msg_count                   OUT NOCOPY NUMBER,
9244            x_msg_data                    OUT NOCOPY VARCHAR2,
9245            p_term_rec                    IN  term_rec_type,
9246            px_overall_status             IN  OUT NOCOPY VARCHAR2,
9247            p_sys_date                    IN DATE DEFAULT SYSDATE, -- rmunjulu EDAT
9248            px_tcnv_rec                   IN  OUT NOCOPY tcnv_rec_type,
9249            p_klev_tbl                    IN  klev_tbl_type,
9250            p_trn_already_set             IN  VARCHAR2,
9251      p_auto_invoice_yn             IN  VARCHAR2 DEFAULT NULL )  IS -- rmunjulu BUYOUT_PROCESS
9252 
9253    -- Cursor to get the asset id for the line
9254    CURSOR line_info_csr (p_l_id IN NUMBER) IS
9255      SELECT  asset_id,
9256              line_number
9257      FROM    OKX_ASSET_LINES_V    A
9258      WHERE   parent_line_id = p_l_id;
9259 
9260    -- Cursor to get the purchase amount for the line
9261    -- RMUNJULU 04-FEB-03 2781557 Changed cursor to get proper purchase amount lines
9262    CURSOR kle_pur_amt_csr  ( p_kle_id   IN NUMBER,
9263                              p_qte_id   IN NUMBER) IS
9264      SELECT amount
9265      FROM   OKL_TXL_QUOTE_LINES_V  TQL
9266      WHERE  kle_id = p_kle_id
9267      AND    qte_id = p_qte_id
9268      AND    qlt_code = 'AMBPOC'; -- Purchase Amount
9269 
9270    kle_pur_amt_rec           kle_pur_amt_csr%ROWTYPE;
9271    l_return_status           VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
9272    l_overall_dispose_status  VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
9273    l_asset_id                NUMBER;
9274    l_line_number             VARCHAR2(200);
9275    i                         NUMBER := 1;
9276    l_proceeds_of_sale        NUMBER;
9277 
9278    --SECHAWLA  Bug # 2726739 : new declarations
9279     l_func_curr_code             GL_LEDGERS_PUBLIC_V.CURRENCY_CODE%TYPE;
9280     l_contract_curr_code         okc_k_headers_b.currency_code%TYPE;
9281     -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
9282     lx_contract_currency         okc_k_headers_v.currency_code%TYPE;
9283     lx_currency_conversion_type  okl_k_headers_v.currency_conversion_type%TYPE;
9284     lx_currency_conversion_rate  okl_k_headers_v.currency_conversion_rate%TYPE;
9285     lx_currency_conversion_date  okl_k_headers_v.currency_conversion_date%TYPE;
9286     lx_converted_amount          NUMBER;
9287 
9288     -- rmunjulu EDAT
9289     l_quote_accpt_date DATE;
9290     l_quote_eff_date DATE;
9291 
9292     -- rmunjulu BUYOUT_PROCESS
9293     l_invoice_amount NUMBER;
9294 
9295     -- Bug 6674730 start
9296     CURSOR c_get_repo_indicator(p_quote_id NUMBER) IS
9297            SELECT NVL(OTQV.REPO_QUOTE_INDICATOR_YN, 'N'),
9298                   KHR.DEAL_TYPE,
9299                   CHR.AUTHORING_ORG_ID
9300            FROM   OKL_TRX_QUOTES_V OTQV,
9301                   OKL_K_HEADERS KHR,
9302                   OKC_K_HEADERS_B CHR
9303            WHERE  OTQV.ID = p_quote_id
9304            AND    OTQV.KHR_ID = KHR.ID
9305            AND    KHR.ID = CHR.ID;
9306 
9307     l_repo_yn OKL_TRX_QUOTES_V.REPO_QUOTE_INDICATOR_YN%TYPE := 'N';
9308     l_deal_type OKL_K_HEADERS.DEAL_TYPE%TYPE;
9309     l_org_id OKC_K_HEADERS_B.AUTHORING_ORG_ID%TYPE;
9310 
9311     CURSOR c_get_loan_repo_params(p_org_id NUMBER) IS
9312            --SELECT CORPORATE_BOOK,
9313            SELECT ASST_ADD_BOOK_TYPE_CODE CORPORATE_BOOK,
9314                   TAX_BOOK_1,
9315                   TAX_BOOK_2,
9316                   RPT_PROD_BOOK_TYPE_CODE,
9317                   FA_LOCATION_ID,
9318                   ASSET_KEY_ID,
9319                   DEPRECIATE_YN
9320            FROM   OKL_SYSTEM_PARAMS_ALL
9321            WHERE  ORG_ID = p_org_id;
9322 
9323     l_corporate_book OKL_SYSTEM_PARAMS_ALL.CORPORATE_BOOK%TYPE;
9324     l_tax_book_1 OKL_SYSTEM_PARAMS_ALL.TAX_BOOK_1%TYPE;
9325     l_tax_book_2 OKL_SYSTEM_PARAMS_ALL.TAX_BOOK_2%TYPE;
9326     l_rep_book   OKL_SYSTEM_PARAMS_ALL.RPT_PROD_BOOK_TYPE_CODE%TYPE;
9327     l_fa_location_id OKL_SYSTEM_PARAMS_ALL.FA_LOCATION_ID%TYPE;
9328     l_asset_key_id OKL_SYSTEM_PARAMS_ALL.ASSET_KEY_ID%TYPE;
9329     l_depreciate_yn OKL_SYSTEM_PARAMS_ALL.DEPRECIATE_YN%TYPE;
9330 
9331     CURSOR try_id_csr(p_try_name  OKL_TRX_TYPES_V.NAME%TYPE) IS
9332     SELECT id
9333     FROM   OKL_TRX_TYPES_tl
9334     WHERE  upper(name) = upper(p_try_name)
9335     AND    language = 'US';
9336 
9337     G_COL_NAME_TOKEN     CONSTANT  VARCHAR2(200) := OKL_API.G_COL_NAME_TOKEN;
9338     G_NO_MATCHING_RECORD CONSTANT  VARCHAR2(200) := 'OKL_LLA_NO_MATCHING_RECORD';
9339 
9340     l_try_id  NUMBER;
9341 
9342     l_legal_entity_id         NUMBER;
9343 
9344     l_trxv_rec               trxv_rec_type;
9345     x_trxv_rec               trxv_rec_type;
9346     l_talv_rec               talv_rec_type;
9347     --l_line_number            NUMBER := 0;
9348 
9349 l_pdt_parameter_rec  OKL_SETUPPRODUCTS_PUB.pdt_parameters_rec_type;
9350 l_pdtv_rec           OKL_SETUPPRODUCTS_PUB.pdtv_rec_type;
9351 l_pdt_parameter_rec2 OKL_SETUPPRODUCTS_PUB.pdt_parameters_rec_type;
9352 x_no_data_found      BOOLEAN;
9353 
9354 
9355     -- Bug 6674730 end
9356   -- asawanka added for debug feature start
9357     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_asset_dispose';
9358     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
9359     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
9360     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
9361     -- asawanka added for debug feature end
9362   BEGIN
9363      IF (is_debug_procedure_on) THEN
9364        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
9365      END IF;
9366 
9367   ---
9368       -- set the dispose rec
9369       -- call asset dispose
9370       -- store the highest degree of error
9371       -- set the transaction record
9372   ---
9373 
9374       -- Start savepoint to rollback to if the block fails
9375       SAVEPOINT asset_dispose;
9376       IF (is_debug_statement_on) THEN
9377         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_trn_already_set = '||p_trn_already_set);
9378         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'px_tcnv_rec.tmt_asset_disposition_yn = '||px_tcnv_rec.tmt_asset_disposition_yn);
9379         OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'p_term_rec.p_quote_id = '||p_term_rec.p_quote_id);
9380       END IF;
9381       IF (p_trn_already_set = G_YES
9382           AND NVL(px_tcnv_rec.tmt_asset_disposition_yn, '?') <> G_YES)
9383       OR (p_trn_already_set = G_NO) THEN
9384 
9385           -- RMUNJULU 3018641 Step Message
9386           -- Step : Asset Dispose
9387           OKL_API.set_message(
9388                         p_app_name      => G_APP_NAME,
9389                         p_msg_name      => 'OKL_AM_STEP_ADP');
9390 
9391     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
9392 
9393          -- rmunjulu EDAT
9394          -- If quote exists then accnting date is quote accept date else sysdate
9395          IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
9396 
9397              l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
9398              l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
9399 
9400          ELSE
9401 
9402              l_quote_accpt_date := p_sys_date;
9403              l_quote_eff_date :=  p_sys_date;
9404 
9405          END IF;
9406 
9407          -- 6674730 start
9408          IF (p_term_rec.p_quote_id IS NOT NULL AND
9409              p_term_rec.p_quote_id <> OKL_API.G_MISS_NUM) THEN
9410            OPEN c_get_repo_indicator(p_term_rec.p_quote_id);
9411            FETCH c_get_repo_indicator INTO l_repo_yn, l_deal_type, l_org_id;
9412            CLOSE c_get_repo_indicator;
9413          END IF;
9414          IF (is_debug_statement_on) THEN
9415             OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_repo_yn = '||l_repo_yn);
9416             OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_deal_type = '||l_deal_type);
9417             OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_org_id = '||l_org_id);
9418          END IF;
9419          IF (l_repo_yn = 'Y' AND l_deal_type='LOAN') THEN
9420            -- Get system params for asset repossession
9421            OPEN c_get_loan_repo_params(l_org_id);
9422            FETCH c_get_loan_repo_params INTO
9423                  l_corporate_book,
9424                  l_tax_book_1,
9425                  l_tax_book_2,
9426                  l_rep_book,
9427                  l_fa_location_id,
9428                  l_asset_key_id,
9429                  l_depreciate_yn;
9430 
9431             IF (is_debug_statement_on) THEN
9432               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_K_RATE_PARAMS_PVT.get_product');
9433             END IF;
9434            -- Get PDT parameters and check reporting product and set l_rep_book
9435             OKL_K_RATE_PARAMS_PVT.get_product(
9436                  p_api_version   => p_api_version,
9437                  p_init_msg_list => p_init_msg_list,
9438                  x_return_status => x_return_status,
9439                  x_msg_count     => x_msg_count,
9440                  x_msg_data      => x_msg_data,
9441                  p_khr_id        => p_term_rec.p_contract_id,
9442                  x_pdt_parameter_rec => l_pdt_parameter_rec);
9443             IF (is_debug_statement_on) THEN
9444               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_K_RATE_PARAMS_PVT.get_product x_return_status = '||x_return_status);
9445             END IF;
9446 
9447             IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9448               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9449             ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9450               RAISE OKL_API.G_EXCEPTION_ERROR;
9451             END IF;
9452             IF (is_debug_statement_on) THEN
9453               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_pdt_parameter_rec.reporting_pdt_id = '||l_pdt_parameter_rec.reporting_pdt_id);
9454             END IF;
9455 
9456             IF (l_pdt_parameter_rec.reporting_pdt_id IS NOT NULL AND
9457                 l_pdt_parameter_rec.reporting_pdt_id <> OKL_API.G_MISS_NUM)
9458             THEN
9459               l_pdtv_rec.ID := l_pdt_parameter_rec.reporting_pdt_id;
9460             IF (is_debug_statement_on) THEN
9461               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_SETUPPRODUCTS_PUB.Getpdt_parameters ');
9462             END IF;
9463 
9464               OKL_SETUPPRODUCTS_PUB.Getpdt_parameters(
9465                         p_api_version       => p_api_version,
9466                         p_init_msg_list     => p_init_msg_list,
9467                         x_return_status     => x_return_status,
9468                         x_msg_count         => x_msg_count,
9469                         x_msg_data          => x_msg_data,
9470                         p_pdtv_rec          => l_pdtv_rec,
9471 	                x_no_data_found     => x_no_data_found,
9472                         p_pdt_parameter_rec => l_pdt_parameter_rec2);
9473             IF (is_debug_statement_on) THEN
9474               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_SETUPPRODUCTS_PUB.Getpdt_parameters x_return_status = '||x_return_status);
9475             END IF;
9476 
9477               IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9478                  RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9479               ELSIF ( x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9480                  RAISE OKL_API.G_EXCEPTION_ERROR;
9481               ELSIF  NVL(l_pdt_parameter_rec2.Name,OKL_API.G_MISS_CHAR) = OKL_API.G_MISS_CHAR THEN
9482                  x_return_status := OKL_API.G_RET_STS_ERROR;
9483                  RAISE OKL_API.G_EXCEPTION_ERROR;
9484               END IF;
9485 
9486               IF (l_pdt_parameter_rec2.DEAL_TYPE <> 'LOAN') THEN
9487                 l_rep_book := NULL; -- No reporting book asset to be created
9488               END IF;
9489 
9490             ELSE
9491               l_rep_book := NULL; -- No reporting book asset to be created
9492             END IF;
9493 
9494          END IF;
9495          -- 6674730 end
9496 
9497     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
9498 
9499           IF (p_klev_tbl.COUNT > 0) THEN
9500 
9501           -- Create 'Internal Asset Creation' transaction header
9502           -- get try id
9503           Open  try_id_csr(p_try_name => 'Internal Asset Creation');
9504           Fetch try_id_csr into l_try_id;
9505           If try_id_csr%NOTFOUND then
9506             OKL_API.set_message(p_app_name     => G_APP_NAME,
9507                           p_msg_name     => G_NO_MATCHING_RECORD,
9508                           p_token1       => G_COL_NAME_TOKEN,
9509                           p_token1_value => 'OKL_TRX_TYPES_V.ID');
9510              RAISE OKL_API.G_EXCEPTION_ERROR;
9511           End If;
9512           Close try_id_csr;
9513 
9514           l_trxv_rec.try_id   := l_try_id;
9515           l_trxv_rec.tas_type := G_TRANS_TYPE;
9516           l_trxv_rec.tsu_code := 'PROCESSED';
9517           l_trxv_rec.date_trans_occurred := sysdate;
9518 
9519           l_legal_entity_id  := OKL_LEGAL_ENTITY_UTIL.get_khr_le_id
9520                                        (p_term_rec.p_contract_id) ;
9521           IF  l_legal_entity_id IS NOT NULL THEN
9522             l_trxv_rec.legal_entity_id :=  l_legal_entity_id;
9523           ELSE
9524 	     Okl_Api.set_message(p_app_name     => g_app_name,
9525                              p_msg_name     => 'OKL_LE_NOT_EXIST_CNTRCT',
9526 			     p_token1           =>  'CONTRACT_NUMBER',
9527 			     p_token1_value  =>  p_term_rec.p_contract_number);
9528                RAISE OKL_API.G_EXCEPTION_ERROR;
9529           END IF;
9530 
9531           /*-- Now creating the new header record
9532           OKL_TRX_ASSETS_PUB.create_trx_ass_h_def(
9533                        p_api_version    => p_api_version,
9534                        p_init_msg_list  => p_init_msg_list,
9535                        x_return_status  => x_return_status,
9536                        x_msg_count      => x_msg_count,
9537                        x_msg_data       => x_msg_data,
9538                        p_thpv_rec       => l_trxv_rec,
9539                        x_thpv_rec       => x_trxv_rec);
9540 
9541           IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9542             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9543           ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9544             RAISE OKL_API.G_EXCEPTION_ERROR;
9545           END IF;*/
9546 
9547 
9548             i := p_klev_tbl.FIRST;
9549             l_line_number := 1;
9550             LOOP
9551 
9552               -- Bug 6674730 start
9553               IF (l_repo_yn = 'Y' AND l_deal_type='LOAN') THEN
9554 
9555             IF (is_debug_statement_on) THEN
9556               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_TRX_ASSETS_PUB.create_trx_ass_h_def');
9557             END IF;
9558 
9559               -- Now creating the new header record
9560               OKL_TRX_ASSETS_PUB.create_trx_ass_h_def(
9561                        p_api_version    => p_api_version,
9562                        p_init_msg_list  => p_init_msg_list,
9563                        x_return_status  => x_return_status,
9564                        x_msg_count      => x_msg_count,
9565                        x_msg_data       => x_msg_data,
9566                        p_thpv_rec       => l_trxv_rec,
9567                        x_thpv_rec       => x_trxv_rec);
9568             IF (is_debug_statement_on) THEN
9569               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_TRX_ASSETS_PUB.create_trx_ass_h_def x_return_status = '||x_return_status);
9570               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'x_trxv_rec.ID =' ||x_trxv_rec.ID);
9571             END IF;
9572 
9573               IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9574                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9575               ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9576                 RAISE OKL_API.G_EXCEPTION_ERROR;
9577               END IF;
9578 
9579 
9580                 -- Create asset in FA for this KLE_ID
9581                 --l_line_number := l_line_number + 1;
9582             IF (is_debug_statement_on) THEN
9583               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling  Create_Repo_asset');
9584             END IF;
9585 
9586                 Create_Repo_asset(
9587                    p_init_msg_list   => p_init_msg_list,
9588                    p_khr_id          => p_term_rec.p_contract_id,
9589                    p_kle_id          => p_klev_tbl(i).p_kle_id,
9590                    p_corporate_book  => l_corporate_book,
9591                    p_tax_book_1      => l_tax_book_1,
9592                    p_tax_book_2      => l_tax_book_2,
9593                    p_rep_book        => l_rep_book,
9594                    p_fa_location_id  => l_fa_location_id,
9595                    p_asset_key_id    => l_asset_key_id,
9596                    p_depreciate_yn   => l_depreciate_yn,
9597                    p_tas_id          => x_trxv_rec.ID,
9598                    p_line_number     => l_line_number,
9599                    p_quote_id        => p_term_rec.p_quote_id,
9600                    x_return_status   => x_return_status,
9601                    x_msg_count       => x_msg_count,
9602                    x_msg_data        => x_msg_data
9603                 );
9604             IF (is_debug_statement_on) THEN
9605               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'called  Create_Repo_asset x_return_status = '||x_return_status);
9606             END IF;
9607 
9608               IF  l_overall_dispose_status = OKL_API.G_RET_STS_SUCCESS
9609                   and x_return_status in (OKL_API.G_RET_STS_ERROR,OKL_API.G_RET_STS_UNEXP_ERROR) then
9610               l_overall_dispose_status := x_return_status;
9611               END IF;
9612 
9613               ELSE
9614               -- Bug 6674730 end
9615 
9616               -- Initialize proceeds_of_sale
9617               l_proceeds_of_sale := 0;
9618 
9619               IF p_auto_invoice_yn IS NULL
9620      OR p_auto_invoice_yn = 'N' THEN -- rmunjulu BUYOUT_PROCESS check if auto invoice due to buyout process
9621 
9622                 -- Loop in the purchase amounts to set proceeds_of_sale
9623                 FOR kle_pur_amt_rec
9624                  IN kle_pur_amt_csr(p_klev_tbl(i).p_kle_id, p_term_rec.p_quote_id) LOOP
9625                  l_proceeds_of_sale := l_proceeds_of_sale +
9626                                        kle_pur_amt_rec.amount;
9627                 END LOOP;
9628 
9629               ELSE -- rmunjulu BUYOUT_PROCESS Proceeds calculated from terms and conditions
9630 
9631                  -- Derive value of sales proceeds
9632                 get_purchase_amount(
9633                   p_term_rec        => p_term_rec,
9634                   p_kle_id          => p_klev_tbl(i).p_kle_id,
9635                   x_purchase_amount => l_invoice_amount,
9636                   x_return_status   => l_return_status);
9637 
9638                 l_proceeds_of_sale := l_invoice_amount;
9639 
9640               END IF;
9641 
9642               -- RMUNJULU 04-FEB-03 2781557 Added if to set the proceeds of sales if no value
9643               IF l_proceeds_of_sale IS NULL  THEN
9644 
9645                 l_proceeds_of_sale := 0;
9646 
9647               END IF;
9648 
9649               -- SECHAWLA  Bug # 2726739 : added the folowing piece of code
9650 
9651               -- get the functional currency
9652               l_func_curr_code := okl_am_util_pvt.get_functional_currency;
9653               -- get the contract currency
9654               l_contract_curr_code := okl_am_util_pvt.get_chr_currency( p_chr_id => p_term_rec.p_contract_id);
9655 
9656               IF l_contract_curr_code <> l_func_curr_code  THEN
9657                     -- convert amount to functional currency
9658                     okl_accounting_util.convert_to_functional_currency(
9659                          p_khr_id              => p_term_rec.p_contract_id,
9660                          p_to_currency        => l_func_curr_code,
9661                          p_transaction_date     => l_quote_accpt_date , --px_tcnv_rec.date_transaction_occurred, -- rmunjulu EDAT
9662                          p_amount           => l_proceeds_of_sale,
9663                          x_return_status     =>  x_return_status,
9664                          x_contract_currency    => lx_contract_currency,
9665                       x_currency_conversion_type => lx_currency_conversion_type,
9666                       x_currency_conversion_rate => lx_currency_conversion_rate,
9667                       x_currency_conversion_date => lx_currency_conversion_date,
9668                       x_converted_amount      => lx_converted_amount );
9669 
9670                     IF x_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
9671 
9672                         -- Error occurred during disposal of asset NAME.
9673                         OKL_API.set_message( p_app_name      => G_APP_NAME,
9674                                      p_msg_name      => 'OKL_AM_ERR_DISPOSAL',
9675                                      p_token1        => 'NAME',
9676                                      p_token1_value  => p_klev_tbl(i).p_asset_name);
9677 
9678                     END IF;
9679 
9680                     -- Raise exception to rollback to savepoint if error
9681                     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9682                         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9683                     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
9684                         RAISE OKL_API.G_EXCEPTION_ERROR;
9685                     END IF;
9686 
9687                     l_proceeds_of_sale := lx_converted_amount ;
9688 
9689               END IF;
9690 
9691             -- RRAVIKIR Legal Entity Changes
9692               -- Populate the legal entity from the contract
9693               l_legal_entity_id :=
9694                 OKL_LEGAL_ENTITY_UTIL.get_khr_le_id(p_khr_id  => p_term_rec.p_contract_id);
9695               -- Legal Entity Changes end
9696 
9697                -- call asset dispose retirement
9698             IF (is_debug_statement_on) THEN
9699               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling  OKL_AM_ASSET_DISPOSE_PUB.dispose_asset');
9700             END IF;
9701 
9702               OKL_AM_ASSET_DISPOSE_PUB.dispose_asset(
9703                  p_api_version                     => p_api_version,
9704                  p_init_msg_list                   => OKL_API.G_FALSE,
9705                  x_return_status                   => l_return_status,
9706                  x_msg_count                       => x_msg_count,
9707                  x_msg_data                        => x_msg_data,
9708                  p_financial_asset_id              => p_klev_tbl(i).p_kle_id,
9709                  p_quantity                        => NULL,
9710                  p_proceeds_of_sale                => l_proceeds_of_sale,
9711                  p_quote_eff_date                  => l_quote_eff_date,    -- rmunjulu EDAT Pass additional parameters now required by disposal api
9712                  p_quote_accpt_date                => l_quote_accpt_date,  -- rmunjulu EDAT Pass additional parameters now required by disposal api
9713                  p_legal_entity_id                 => l_legal_entity_id);  -- RRAVIKIR Legal Entity Changes
9714             IF (is_debug_statement_on) THEN
9715               OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called  OKL_AM_ASSET_DISPOSE_PUB.dispose_asset l_return_status = '||l_return_status);
9716             END IF;
9717 
9718               -- -- SECHAWLA  Bug # 2726739 : end new code
9719 
9720 /*               -- call asset dispose retirement
9721               OKL_AM_ASSET_DISPOSE_PUB.dispose_asset(
9722                  p_api_version                     => p_api_version,
9723                  p_init_msg_list                   => OKL_API.G_FALSE,
9724                  x_return_status                   => l_return_status,
9725                  x_msg_count                       => x_msg_count,
9726                  x_msg_data                        => x_msg_data,
9727                  p_financial_asset_id              => p_klev_tbl(i).p_kle_id,
9728                  p_quantity                        => NULL,
9729                  p_proceeds_of_sale                => l_proceeds_of_sale,
9730                  p_quote_eff_date                  => l_quote_eff_date,    -- rmunjulu EDAT Pass additional parameters now required by disposal api
9731                  p_quote_accpt_date                => l_quote_accpt_date); -- rmunjulu EDAT Pass additional parameters now required by disposal api
9732 */
9733               IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
9734 
9735                 -- Error occurred during disposal of asset NAME.
9736                 OKL_API.set_message( p_app_name      => G_APP_NAME,
9737                                      p_msg_name      => 'OKL_AM_ERR_DISPOSAL',
9738                                      p_token1        => 'NAME',
9739                                      p_token1_value  => p_klev_tbl(i).p_asset_name);
9740 
9741               END IF;
9742      --08-mar-06 sgorantl -- Bug 3895098
9743      IF  l_overall_dispose_status = OKL_API.G_RET_STS_SUCCESS
9744          and l_return_status in (OKL_API.G_RET_STS_ERROR,OKL_API.G_RET_STS_UNEXP_ERROR) then
9745      l_overall_dispose_status := l_return_status;
9746      END IF;
9747 
9748               END IF;
9749               EXIT WHEN (i = p_klev_tbl.LAST);
9750               i := p_klev_tbl.NEXT(i);
9751             END LOOP;
9752 
9753        --08-mar-06 sgorantl -- Bug 3895098
9754               -- Raise exception to rollback to savepoint if error
9755               IF (l_overall_dispose_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
9756                 RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9757               ELSIF (l_overall_dispose_status = OKL_API.G_RET_STS_ERROR) THEN
9758                 RAISE OKL_API.G_EXCEPTION_ERROR;
9759               END IF;
9760        --08-mar-06 sgorantl -- Bug 3895098
9761 
9762             -- Set success message
9763             -- Asset dispostion for assets of contract CONTRACT_NUMBER done successfully.
9764             OKL_API.set_message( p_app_name     => G_APP_NAME,
9765                                  p_msg_name     => 'OKL_AM_ASS_DISPOSE_SUCCESS',
9766                                  p_token1       => 'CONTRACT_NUMBER',
9767                                  p_token1_value => p_term_rec.p_contract_number);
9768 
9769             -- store the highest degree of error
9770             set_overall_status(
9771               p_return_status                 => l_overall_dispose_status,
9772               px_overall_status               => px_overall_status);
9773 
9774             -- set the transaction record for asset disposition
9775             set_transaction_rec(
9776               p_return_status                 => l_overall_dispose_status,
9777               p_overall_status                => px_overall_status,
9778               p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
9779               p_tsu_code                      => 'WORKING',
9780               px_tcnv_rec                     => px_tcnv_rec);
9781 
9782           END IF;
9783       END IF;
9784 
9785       -- set the transaction record for amortization
9786       set_transaction_rec(
9787              p_return_status                 => l_return_status,
9788              p_overall_status                => px_overall_status,
9789              p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
9790              p_tsu_code                      => 'WORKING',
9791              p_ret_val                       => NULL,
9792              px_tcnv_rec                     => px_tcnv_rec);
9793 
9794       -- set the transaction record for asset return
9795       set_transaction_rec(
9796              p_return_status                 => l_return_status,
9797              p_overall_status                => px_overall_status,
9798              p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
9799              p_tsu_code                      => 'WORKING',
9800              p_ret_val                       => NULL,
9801              px_tcnv_rec                     => px_tcnv_rec);
9802 
9803     x_return_status      := l_return_status;
9804     IF (is_debug_procedure_on) THEN
9805        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
9806     END IF;
9807   EXCEPTION
9808     WHEN OKL_API.G_EXCEPTION_ERROR THEN
9809         IF (is_debug_exception_on) THEN
9810              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
9811         END IF;
9812 
9813          IF kle_pur_amt_csr%ISOPEN THEN
9814             CLOSE kle_pur_amt_csr;
9815          END IF;
9816 
9817          ROLLBACK TO asset_dispose;
9818 
9819          x_return_status := OKL_API.G_RET_STS_ERROR;
9820          -- store the highest degree of error
9821          set_overall_status(
9822                p_return_status                 => x_return_status,
9823                px_overall_status               => px_overall_status);
9824 
9825          -- set the transaction record
9826          set_transaction_rec(
9827                p_return_status                 => x_return_status,
9828                p_overall_status                => px_overall_status,
9829                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
9830                p_tsu_code                      => 'ERROR',
9831                px_tcnv_rec                     => px_tcnv_rec);
9832          -- set the transaction record for amortization
9833          set_transaction_rec(
9834                p_return_status                 => x_return_status,
9835                p_overall_status                => px_overall_status,
9836                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
9837                p_tsu_code                      => 'ERROR',
9838                p_ret_val                       => NULL,
9839                px_tcnv_rec                     => px_tcnv_rec);
9840          -- set the transaction record for asset return
9841          set_transaction_rec(
9842                p_return_status                 => x_return_status,
9843                p_overall_status                => px_overall_status,
9844                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
9845                p_tsu_code                      => 'ERROR',
9846                p_ret_val                       => NULL,
9847                px_tcnv_rec                     => px_tcnv_rec);
9848 
9849     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
9850         IF (is_debug_exception_on) THEN
9851              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
9852         END IF;
9853 
9854          IF kle_pur_amt_csr%ISOPEN THEN
9855             CLOSE kle_pur_amt_csr;
9856          END IF;
9857 
9858          ROLLBACK TO asset_dispose;
9859 
9860 
9861          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
9862          -- store the highest degree of error
9863          set_overall_status(
9864                p_return_status                 => x_return_status,
9865                px_overall_status               => px_overall_status);
9866 
9867          -- set the transaction record
9868          set_transaction_rec(
9869                p_return_status                 => x_return_status,
9870                p_overall_status                => px_overall_status,
9871                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
9872                p_tsu_code                      => 'ERROR',
9873                px_tcnv_rec                     => px_tcnv_rec);
9874          -- set the transaction record for amortization
9875          set_transaction_rec(
9876                p_return_status                 => x_return_status,
9877                p_overall_status                => px_overall_status,
9878                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
9879                p_tsu_code                      => 'ERROR',
9880                p_ret_val                       => NULL,
9881                px_tcnv_rec                     => px_tcnv_rec);
9882          -- set the transaction record for asset return
9883          set_transaction_rec(
9884                p_return_status                 => x_return_status,
9885                p_overall_status                => px_overall_status,
9886                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
9887                p_tsu_code                      => 'ERROR',
9888                p_ret_val                       => NULL,
9889                px_tcnv_rec                     => px_tcnv_rec);
9890 
9891     WHEN OTHERS THEN
9892         IF (is_debug_exception_on) THEN
9893            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
9894         END IF;
9895 
9896          IF kle_pur_amt_csr%ISOPEN THEN
9897             CLOSE kle_pur_amt_csr;
9898          END IF;
9899 
9900          ROLLBACK TO asset_dispose;
9901 
9902          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
9903          -- store the highest degree of error
9904          set_overall_status(
9905                p_return_status                 => x_return_status,
9906                px_overall_status               => px_overall_status);
9907 
9908          -- set the transaction record
9909          set_transaction_rec(
9910                p_return_status                 => x_return_status,
9911                p_overall_status                => px_overall_status,
9912                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
9913                p_tsu_code                      => 'ERROR',
9914                px_tcnv_rec                     => px_tcnv_rec);
9915          -- set the transaction record for amortization
9916          set_transaction_rec(
9917                p_return_status                 => x_return_status,
9918                p_overall_status                => px_overall_status,
9919                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
9920                p_tsu_code                      => 'ERROR',
9921                p_ret_val                       => NULL,
9922                px_tcnv_rec                     => px_tcnv_rec);
9923          -- set the transaction record for asset return
9924          set_transaction_rec(
9925                p_return_status                 => x_return_status,
9926                p_overall_status                => px_overall_status,
9927                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
9928                p_tsu_code                      => 'ERROR',
9929                p_ret_val                       => NULL,
9930                px_tcnv_rec                     => px_tcnv_rec);
9931 
9932          -- Set the oracle error message
9933          OKL_API.set_message(
9934                          p_app_name      => OKC_API.G_APP_NAME,
9935                          p_msg_name      => g_unexpected_error,
9936                          p_token1        => g_sqlcode_token,
9937                          p_token1_value  => SQLCODE,
9938                          p_token2        => g_sqlerrm_token,
9939                          p_token2_value  => SQLERRM);
9940 
9941   END process_asset_dispose;
9942 
9943   -- Start of comments
9944   --
9945   -- Procedure Name : process_amortize_and_return
9946   -- Desciption     : Calls the Amortization API and Asset Return API to do
9947   --                  amortization and then return of assets of contract
9948   -- Business Rules :
9949   -- Parameters  :
9950   -- Version  : 1.0
9951   -- History        : RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
9952   --                : RMUNJULU 04-MAR-04 3485854 Changed the code to make call to
9953   --                  process_amortize proc instead of having the code in this proc
9954   --                : rmunjulu EDAT modified code to check for quote exists to set reason code
9955   --                  Also pass date_returned as quote eff date
9956   --                : rmunjulu EDAT Removed code to set date_returned
9957   --
9958   -- End of comments
9959   PROCEDURE process_amortize_and_return(
9960            p_api_version                 IN  NUMBER,
9961            p_init_msg_list               IN  VARCHAR2,
9962            x_return_status               OUT NOCOPY VARCHAR2,
9963            x_msg_count                   OUT NOCOPY NUMBER,
9964            x_msg_data                    OUT NOCOPY VARCHAR2,
9965            p_term_rec                    IN term_rec_type,
9966            px_overall_status             IN OUT NOCOPY VARCHAR2,
9967            px_tcnv_rec                   IN OUT NOCOPY tcnv_rec_type,
9968            p_sys_date                    IN DATE,
9969            p_klev_tbl                    IN klev_tbl_type,
9970            p_trn_already_set             IN  VARCHAR2)  IS
9971 
9972    -- Cursor to get the end date of contract
9973    -- RMUNJULU 06-MAR-03 Performance Fix Replaced K_HDR_FULL
9974    CURSOR get_k_end_date_csr ( p_khr_id IN NUMBER) IS
9975     SELECT end_date
9976     FROM   OKC_K_HEADERS_V
9977     WHERE  id =  p_khr_id;
9978 
9979    -- Get the non-cancelled asset return for asset
9980    CURSOR get_asset_return_csr ( p_kle_id IN NUMBER) IS
9981     SELECT id id,
9982            OKL_AM_UTIL_PVT.get_lookup_meaning('OKL_ASSET_RETURN_STATUS',ars_code,'N') ret_status
9983     FROM   OKL_ASSET_RETURNS_V
9984     WHERE  kle_id = p_kle_id
9985     AND    ars_code <> 'CANCELLED';
9986 
9987    l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
9988    l_overall_status        VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
9989    lp_artv_rec             OKL_AM_ASSET_RETURN_PUB.artv_rec_type;
9990    lx_artv_rec             OKL_AM_ASSET_RETURN_PUB.artv_rec_type;
9991 
9992    i                       NUMBER := 1;
9993    j                       NUMBER := 1;
9994    l_kle_id                NUMBER;
9995    l_k_end_date            DATE := OKL_API.G_MISS_DATE;
9996    l_early_term_yn         VARCHAR2(1) := G_NO;
9997    l_return_needed         VARCHAR2(1) := G_NO;
9998    l_asset_return_status   VARCHAR2(2000);
9999 
10000    l_temp_klev_tbl         klev_tbl_type;
10001 
10002    -- rmunjulu EDAT
10003    l_quote_accpt_date DATE;
10004    l_quote_eff_date DATE;
10005    -- asawanka added for debug feature start
10006     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'process_amortize_and_return';
10007     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
10008     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
10009     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
10010     -- asawanka added for debug feature end
10011   BEGIN
10012      IF (is_debug_procedure_on) THEN
10013        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
10014      END IF;
10015 
10016   --
10017       -- set the amortization rec
10018       -- call amortization
10019       -- store the highest degree of error
10020       -- set the transaction record
10021 
10022       -- set the return rec
10023       -- call asset return
10024       -- store the highest degree of error
10025       -- set the transaction record
10026   ---
10027 
10028     SAVEPOINT amortize_and_return;
10029 
10030     ----------------------------
10031     --- START ASSET AMORTIZATION
10032     ----------------------------
10033 /* -- RMUNJULU 04-MAR-04 3485854 Removed this code for amortization and added call to
10034 -- process_amortize to do amortization
10035     BEGIN  -- begin for amortize
10036 
10037       -- Start a savepoint
10038       SAVEPOINT asset_amortize;
10039 
10040       -- Check if amortization required
10041       IF (p_trn_already_set = G_YES
10042           AND NVL(px_tcnv_rec.tmt_amortization_yn, '?') <> G_YES)
10043       OR (p_trn_already_set = G_NO) THEN
10044 
10045           -- RMUNJULU 3018641 Step Message
10046           -- Step : Amortization
10047           OKL_API.set_message(
10048                         p_app_name      => G_APP_NAME,
10049                         p_msg_name      => 'OKL_AM_STEP_AMT');
10050 
10051           -- call amortization
10052 
10053           OPEN  get_k_end_date_csr(p_term_rec.p_contract_id);
10054           FETCH get_k_end_date_csr INTO l_k_end_date;
10055           CLOSE get_k_end_date_csr;
10056 
10057           IF (l_k_end_date <> OKL_API.G_MISS_DATE)
10058           AND (TRUNC(l_k_end_date) > TRUNC(p_sys_date)) THEN
10059             l_early_term_yn := G_YES;
10060           END IF;
10061 
10062           OKL_AM_AMORTIZE_PUB.create_offlease_asset_trx(
10063             p_api_version                => p_api_version,
10064             p_init_msg_list              => OKL_API.G_FALSE,
10065             x_return_status              => l_return_status,
10066             x_msg_count                  => x_msg_count,
10067             x_msg_data                   => x_msg_data,
10068             p_contract_id                 => p_term_rec.p_contract_id,
10069             p_early_termination_yn        => l_early_term_yn);
10070 
10071 
10072           IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
10073 
10074             -- Error occurred during the creation of an amortization transaction
10075             -- for assets of contract CONTRACT_NUMBER.
10076             OKL_API.set_message( p_app_name      => G_APP_NAME,
10077                                  p_msg_name      => 'OKL_AM_ERR_AMORTIZE',
10078                                  p_token1        => 'CONTRACT_NUMBER',
10079                                  p_token1_value  => p_term_rec.p_contract_number);
10080 
10081           END IF;
10082 
10083           -- Raise exception to rollback to savepoint if error
10084           IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10085              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10086           ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10087              RAISE OKL_API.G_EXCEPTION_ERROR;
10088           END IF;
10089 
10090           -- store the highest degree of error
10091           set_overall_status(
10092             p_return_status               => l_return_status,
10093             px_overall_status             => px_overall_status);
10094 
10095           -- set the transaction record for amortization
10096           set_transaction_rec(
10097             p_return_status               => l_return_status,
10098             p_overall_status              => px_overall_status,
10099             p_tmt_flag                    => 'TMT_AMORTIZATION_YN',
10100             p_tsu_code                    => 'WORKING',
10101             px_tcnv_rec                   => px_tcnv_rec);
10102 
10103       END IF;
10104     EXCEPTION
10105       WHEN OKL_API.G_EXCEPTION_ERROR THEN
10106 
10107          IF get_k_end_date_csr%ISOPEN THEN
10108             CLOSE get_k_end_date_csr;
10109          END IF;
10110 
10111          ROLLBACK TO asset_amortize;
10112 
10113          x_return_status := OKL_API.G_RET_STS_ERROR;
10114 
10115          -- store the highest degree of error
10116          set_overall_status(
10117                p_return_status                 => x_return_status,
10118                px_overall_status               => px_overall_status);
10119 
10120          -- set the transaction record
10121          set_transaction_rec(
10122                p_return_status                 => x_return_status,
10123                p_overall_status                => px_overall_status,
10124                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
10125                p_tsu_code                      => 'ERROR',
10126                px_tcnv_rec                     => px_tcnv_rec);
10127 
10128          -- set the transaction record
10129          set_transaction_rec(
10130                p_return_status                 => x_return_status,
10131                p_overall_status                => px_overall_status,
10132                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10133                p_tsu_code                      => 'ERROR',
10134                p_ret_val                       => NULL,
10135                px_tcnv_rec                     => px_tcnv_rec);
10136 
10137       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
10138 
10139          IF get_k_end_date_csr%ISOPEN THEN
10140             CLOSE get_k_end_date_csr;
10141          END IF;
10142 
10143          ROLLBACK TO asset_amortize;
10144 
10145          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
10146 
10147          -- store the highest degree of error
10148          set_overall_status(
10149                p_return_status                 => x_return_status,
10150                px_overall_status               => px_overall_status);
10151 
10152          -- set the transaction record
10153          set_transaction_rec(
10154                p_return_status                 => x_return_status,
10155                p_overall_status                => px_overall_status,
10156                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
10157                p_tsu_code                      => 'ERROR',
10158                px_tcnv_rec                     => px_tcnv_rec);
10159 
10160          -- set the transaction record
10161          set_transaction_rec(
10162                p_return_status                 => x_return_status,
10163                p_overall_status                => px_overall_status,
10164                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10165                p_tsu_code                      => 'ERROR',
10166                p_ret_val                       => NULL,
10167                px_tcnv_rec                     => px_tcnv_rec);
10168 
10169       WHEN OTHERS THEN
10170 
10171          IF get_k_end_date_csr%ISOPEN THEN
10172             CLOSE get_k_end_date_csr;
10173          END IF;
10174 
10175          ROLLBACK TO asset_amortize;
10176 
10177          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
10178 
10179          -- store the highest degree of error
10180          set_overall_status(
10181                p_return_status                 => x_return_status,
10182                px_overall_status               => px_overall_status);
10183 
10184          -- set the transaction record
10185          set_transaction_rec(
10186                p_return_status                 => x_return_status,
10187                p_overall_status                => px_overall_status,
10188                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
10189                p_tsu_code                      => 'ERROR',
10190                px_tcnv_rec                     => px_tcnv_rec);
10191 
10192          -- set the transaction record
10193          set_transaction_rec(
10194                p_return_status                 => x_return_status,
10195                p_overall_status                => px_overall_status,
10196                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10197                p_tsu_code                      => 'ERROR',
10198                p_ret_val                       => NULL,
10199                px_tcnv_rec                     => px_tcnv_rec);
10200 
10201          -- Set the oracle error message
10202          OKL_API.set_message(
10203                          p_app_name      => OKC_API.G_APP_NAME,
10204                          p_msg_name      => g_unexpected_error,
10205                          p_token1        => g_sqlcode_token,
10206                          p_token1_value  => SQLCODE,
10207                          p_token2        => g_sqlerrm_token,
10208                          p_token2_value  => SQLERRM);
10209 
10210     END;
10211 */
10212 
10213 -- RMUNJULU 04-MAR-04 3485854 added call to process_amortize to do amortization
10214 -- Additional checks in process_amortize to cater to evergreen scenario
10215     process_amortize(
10216                 p_api_version       => p_api_version,
10217                 p_init_msg_list     => OKL_API.G_FALSE,
10218                 x_return_status     => l_return_status,
10219                 x_msg_count         => x_msg_count,
10220                 x_msg_data          => x_msg_data,
10221                 p_term_rec          => p_term_rec,
10222                 px_overall_status   => px_overall_status,
10223                 px_tcnv_rec         => px_tcnv_rec,
10224                 p_sys_date          => p_sys_date,
10225                 p_trn_already_set   => p_trn_already_set,
10226                 p_call_origin       => 'TERMINATION');
10227 
10228     ----------------------
10229     --- START ASSET RETURN
10230     ----------------------
10231 
10232     BEGIN -- begin asset return
10233 
10234       SAVEPOINT asset_return;
10235 
10236       -- Check if asset return required
10237       IF (p_trn_already_set = G_YES
10238           AND NVL(px_tcnv_rec.tmt_asset_return_yn, '?') <> G_YES)
10239       OR (p_trn_already_set = G_NO) THEN
10240 
10241           -- RMUNJULU 3018641 Step Message
10242           -- Step : Asset Return
10243           OKL_API.set_message(
10244                         p_app_name      => G_APP_NAME,
10245                         p_msg_name      => 'OKL_AM_STEP_ART');
10246 
10247           -- if assets present for contract
10248           IF (p_klev_tbl.COUNT > 0) THEN
10249 
10250 
10251     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
10252 
10253               -- rmunjulu EDAT
10254               -- If quote exists then accnting date is quote accept date else sysdate
10255               IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN
10256 
10257                  l_quote_accpt_date := okl_am_lease_loan_trmnt_pvt.g_quote_accept_date;
10258                  l_quote_eff_date := okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date;
10259 
10260               ELSE
10261 
10262                  l_quote_accpt_date := p_sys_date;
10263                  l_quote_eff_date :=  p_sys_date;
10264 
10265               END IF;
10266 
10267     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
10268 
10269               -- Loop thru assets table
10270               i := p_klev_tbl.FIRST;
10271               LOOP
10272 
10273                 l_return_needed := G_NO;
10274 
10275                 -- Check if return created
10276                 OPEN  get_asset_return_csr (p_klev_tbl(i).p_kle_id);
10277                 FETCH get_asset_return_csr INTO l_kle_id, l_asset_return_status;
10278                 IF get_asset_return_csr%NOTFOUND OR l_kle_id IS NULL THEN
10279                    l_return_needed := G_YES;
10280                 END IF;
10281                 CLOSE get_asset_return_csr;
10282 
10283                 -- if no return try creating else set message
10284                 IF l_return_needed = G_YES THEN
10285 
10286 
10287                   -- set the temp table to contain all assets returned NOW
10288                   l_temp_klev_tbl(j).p_kle_id := p_klev_tbl(i).p_kle_id;
10289                   l_temp_klev_tbl(j).p_asset_name := p_klev_tbl(i).p_asset_name;
10290                   j := j + 1;
10291 
10292                   -- set the asset return id
10293                   lp_artv_rec.kle_id    :=  p_klev_tbl(i).p_kle_id;
10294 
10295                   -- set the art1_code for asset return --'OKL_ASSET_RETURN_TYPE'
10296                   -- if early termination assume from quote else contract exp
10297 --                  IF (l_k_end_date <> OKL_API.G_MISS_DATE)
10298 --                  AND (TRUNC(l_k_end_date) < TRUNC(p_sys_date)) THEN
10299                   -- rmunjulu EDAT modified condition to say if quote exists then EXE_TERMINATION_QUOTE else EXPIRATION
10300                   IF nvl(okl_am_lease_loan_trmnt_pvt.g_quote_exists,'N') = 'Y' THEN -- rmunjulu EDAT
10301                      lp_artv_rec.art1_code := 'EXE_TERMINATION_QUOTE';
10302                   ELSE
10303                      lp_artv_rec.art1_code := 'CONTRACT_EXPIRATION';
10304                   END IF;
10305 
10306                   -- set the ars_code for asset return --'OKL_ASSET_RETURN_STATUS'
10307                   --Bug #3925453: pagarg +++ T and A +++++++ Start ++++++++++
10308                   if p_term_rec.p_quote_type = 'TER_RELEASE_WO_PURCHASE'
10309                   then
10310                       lp_artv_rec.ars_code := 'RELEASE_IN_PROCESS';
10311                   else
10312                       lp_artv_rec.ars_code := 'SCHEDULED';
10313                   end if;
10314                   --Bug #3925453: pagarg +++ T and A +++++++ End ++++++++++
10315 
10316                   --lp_artv_rec.date_returned := l_quote_eff_date; -- rmunjulu EDAT added to send return date
10317 
10318                   --Bug #3925453: pagarg +++ T and A ++++
10319                   -- Passing quote_id also to create_asset_return
10320                   -- call asset return
10321                   OKL_AM_ASSET_RETURN_PUB.create_asset_return(
10322                     p_api_version                => p_api_version,
10323                     p_init_msg_list              => OKL_API.G_FALSE,
10324                     x_return_status              => l_return_status,
10325                     x_msg_count                  => x_msg_count,
10326                     x_msg_data                   => x_msg_data,
10327                     p_artv_rec       => lp_artv_rec,
10328                     x_artv_rec       => lx_artv_rec,
10329                     p_quote_id                    => p_term_rec.p_quote_id);
10330 
10331                   IF l_return_status <> OKL_API.G_RET_STS_SUCCESS THEN
10332                     -- Error occurred during the creation of an asset return record
10333                     -- for asset  NAME.
10334                     OKL_API.set_message(
10335                             p_app_name      => G_APP_NAME,
10336                             p_msg_name      => 'OKL_AM_ERR_ASS_RET',
10337                             p_token1        => 'NAME',
10338                             p_token1_value  => p_klev_tbl(i).p_asset_name);
10339 
10340                     -- Raise exception to rollback to savepoint if error
10341                     RAISE OKL_API.G_EXCEPTION_ERROR;
10342 
10343                   END IF;
10344 
10345                 ELSE -- Asset return already exists -- This is not an error
10346 
10347                   -- Asset Return already exists for this asset NAME with the
10348                   -- status STATUS so cannot create a new asset return now.
10349                   OKL_API.set_message(
10350                             p_app_name      => G_APP_NAME,
10351                             p_msg_name      => 'OKL_AM_ASS_RET_ARS_ERR',
10352                             p_token1        => 'NAME',
10353                             p_token1_value  =>  p_klev_tbl(i).p_asset_name,
10354                             p_token2        => 'STATUS',
10355                             p_token2_value  =>  l_asset_return_status);
10356 
10357                 END IF;
10358                 EXIT WHEN (i = p_klev_tbl.LAST);
10359                 i := p_klev_tbl.NEXT(i);
10360               END LOOP;
10361 
10362               -- Set success messages once all returns done NOW
10363               IF l_temp_klev_tbl.COUNT > 0 THEN
10364                 i := l_temp_klev_tbl.FIRST;
10365                 LOOP
10366 
10367                     -- Asset return created for asset  NAME.
10368                     OKL_API.set_message(
10369                             p_app_name      => G_APP_NAME,
10370                             p_msg_name      => 'OKL_AM_ASS_RET_CREATED',
10371                             p_token1        => 'NAME',
10372                             p_token1_value  => l_temp_klev_tbl(i).p_asset_name);
10373 
10374                   EXIT WHEN (i = l_temp_klev_tbl.LAST);
10375                   i := l_temp_klev_tbl.NEXT(i);
10376                 END LOOP;
10377               END IF;
10378 
10379               -- set the transaction record for asset return
10380               set_transaction_rec(
10381                 p_return_status               => l_return_status,
10382                 p_overall_status              => px_overall_status,
10383                 p_tmt_flag                    => 'TMT_ASSET_RETURN_YN',
10384                 p_tsu_code                    => 'WORKING',
10385                 px_tcnv_rec                   => px_tcnv_rec);
10386 
10387               -- Set overall status
10388               set_overall_status(
10389                 p_return_status               => l_return_status,
10390                 px_overall_status             => px_overall_status);
10391 
10392           END IF;
10393       END IF;
10394     IF (is_debug_procedure_on) THEN
10395        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
10396     END IF;
10397     EXCEPTION
10398 
10399       WHEN OKL_API.G_EXCEPTION_ERROR THEN
10400         IF (is_debug_exception_on) THEN
10401              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
10402         END IF;
10403 
10404          IF get_asset_return_csr%ISOPEN THEN
10405             CLOSE get_asset_return_csr;
10406          END IF;
10407 
10408          ROLLBACK TO asset_return;
10409 
10410          x_return_status := OKL_API.G_RET_STS_ERROR;
10411 
10412          -- store the highest degree of error
10413          set_overall_status(
10414                p_return_status                 => x_return_status,
10415                px_overall_status               => px_overall_status);
10416 
10417          -- set the transaction record
10418          set_transaction_rec(
10419                p_return_status                 => x_return_status,
10420                p_overall_status                => px_overall_status,
10421                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
10422                p_tsu_code                      => 'ERROR',
10423                px_tcnv_rec                     => px_tcnv_rec);
10424          -- set the transaction record
10425          set_transaction_rec(
10426                p_return_status                 => x_return_status,
10427                p_overall_status                => px_overall_status,
10428                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10429                p_tsu_code                      => 'ERROR',
10430                p_ret_val                       => NULL,
10431                px_tcnv_rec                     => px_tcnv_rec);
10432 
10433       WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
10434         IF (is_debug_exception_on) THEN
10435              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
10436         END IF;
10437 
10438 
10439          IF get_asset_return_csr%ISOPEN THEN
10440             CLOSE get_asset_return_csr;
10441          END IF;
10442 
10443          ROLLBACK TO asset_return;
10444 
10445          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
10446 
10447          -- store the highest degree of error
10448          set_overall_status(
10449                p_return_status                 => x_return_status,
10450                px_overall_status               => px_overall_status);
10451 
10452          -- set the transaction record
10453          set_transaction_rec(
10454                p_return_status                 => x_return_status,
10455                p_overall_status                => px_overall_status,
10456                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
10457                p_tsu_code                      => 'ERROR',
10458                px_tcnv_rec                     => px_tcnv_rec);
10459 
10460          -- set the transaction record
10461          set_transaction_rec(
10462                p_return_status                 => x_return_status,
10463                p_overall_status                => px_overall_status,
10464                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10465                p_tsu_code                      => 'ERROR',
10466                p_ret_val                       => NULL,
10467                px_tcnv_rec                     => px_tcnv_rec);
10468 
10469       WHEN OTHERS THEN
10470         IF (is_debug_exception_on) THEN
10471            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
10472         END IF;
10473 
10474          IF get_asset_return_csr%ISOPEN THEN
10475             CLOSE get_asset_return_csr;
10476          END IF;
10477 
10478          ROLLBACK TO asset_return;
10479 
10480          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
10481 
10482          -- store the highest degree of error
10483          set_overall_status(
10484                p_return_status                 => x_return_status,
10485                px_overall_status               => px_overall_status);
10486 
10487          -- set the transaction record
10488          set_transaction_rec(
10489                p_return_status                 => x_return_status,
10490                p_overall_status                => px_overall_status,
10491                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
10492                p_tsu_code                      => 'ERROR',
10493                px_tcnv_rec                     => px_tcnv_rec);
10494 
10495          -- set the transaction record
10496          set_transaction_rec(
10497                p_return_status                 => x_return_status,
10498                p_overall_status                => px_overall_status,
10499                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10500                p_tsu_code                      => 'ERROR',
10501                p_ret_val                       => NULL,
10502                px_tcnv_rec                     => px_tcnv_rec);
10503 
10504          -- Set the oracle error message
10505          OKL_API.set_message(
10506                          p_app_name      => OKC_API.G_APP_NAME,
10507                          p_msg_name      => g_unexpected_error,
10508                          p_token1        => g_sqlcode_token,
10509                          p_token1_value  => SQLCODE,
10510                          p_token2        => g_sqlerrm_token,
10511                          p_token2_value  => SQLERRM);
10512 
10513     END;
10514 
10515     -- set the transaction record for asset disose
10516     set_transaction_rec(
10517             p_return_status               => l_return_status,
10518             p_overall_status              => px_overall_status,
10519             p_tmt_flag                    => 'TMT_ASSET_DISPOSITION_YN',
10520             p_tsu_code                    => 'WORKING',
10521             p_ret_val                     => NULL,
10522             px_tcnv_rec                   => px_tcnv_rec);
10523 
10524     x_return_status      := l_return_status;
10525     IF (is_debug_procedure_on) THEN
10526        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
10527     END IF;
10528   EXCEPTION
10529 
10530     WHEN OKL_API.G_EXCEPTION_ERROR THEN
10531         IF (is_debug_exception_on) THEN
10532              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
10533         END IF;
10534 
10535          ROLLBACK TO amortize_and_return;
10536 
10537          x_return_status := OKL_API.G_RET_STS_ERROR;
10538          -- store the highest degree of error
10539          set_overall_status(
10540                p_return_status                 => x_return_status,
10541                px_overall_status               => px_overall_status);
10542 
10543          -- set the transaction record
10544          set_transaction_rec(
10545                p_return_status                 => x_return_status,
10546                p_overall_status                => px_overall_status,
10547                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
10548                p_tsu_code                      => 'ERROR',
10549                px_tcnv_rec                     => px_tcnv_rec);
10550          -- set the transaction record
10551          set_transaction_rec(
10552                p_return_status                 => x_return_status,
10553                p_overall_status                => px_overall_status,
10554                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
10555                p_tsu_code                      => 'ERROR',
10556                px_tcnv_rec                     => px_tcnv_rec);
10557          -- set the transaction record
10558          set_transaction_rec(
10559                p_return_status                 => x_return_status,
10560                p_overall_status                => px_overall_status,
10561                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10562                p_tsu_code                      => 'ERROR',
10563                p_ret_val                       => NULL,
10564                px_tcnv_rec                     => px_tcnv_rec);
10565 
10566     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
10567         IF (is_debug_exception_on) THEN
10568              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
10569         END IF;
10570 
10571 
10572          ROLLBACK TO amortize_and_return;
10573 
10574          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
10575          -- store the highest degree of error
10576          set_overall_status(
10577                p_return_status                 => x_return_status,
10578                px_overall_status               => px_overall_status);
10579 
10580          -- set the transaction record
10581          set_transaction_rec(
10582                p_return_status                 => x_return_status,
10583                p_overall_status                => px_overall_status,
10584                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
10585                p_tsu_code                      => 'ERROR',
10586                px_tcnv_rec                     => px_tcnv_rec);
10587          -- set the transaction record
10588          set_transaction_rec(
10589                p_return_status                 => x_return_status,
10590                p_overall_status                => px_overall_status,
10591                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
10592                p_tsu_code                      => 'ERROR',
10593                px_tcnv_rec                     => px_tcnv_rec);
10594          -- set the transaction record
10595          set_transaction_rec(
10596                p_return_status                 => x_return_status,
10597                p_overall_status                => px_overall_status,
10598                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10599                p_tsu_code                      => 'ERROR',
10600                p_ret_val                       => NULL,
10601                px_tcnv_rec                     => px_tcnv_rec);
10602 
10603     WHEN OTHERS THEN
10604         IF (is_debug_exception_on) THEN
10605            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
10606         END IF;
10607 
10608          ROLLBACK TO amortize_and_return;
10609 
10610          x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
10611          -- store the highest degree of error
10612          set_overall_status(
10613                p_return_status                 => x_return_status,
10614                px_overall_status               => px_overall_status);
10615 
10616          -- set the transaction record
10617          set_transaction_rec(
10618                p_return_status                 => x_return_status,
10619                p_overall_status                => px_overall_status,
10620                p_tmt_flag                      => 'TMT_AMORTIZATION_YN',
10621                p_tsu_code                      => 'ERROR',
10622                px_tcnv_rec                     => px_tcnv_rec);
10623          -- set the transaction record
10624          set_transaction_rec(
10625                p_return_status                 => x_return_status,
10626                p_overall_status                => px_overall_status,
10627                p_tmt_flag                      => 'TMT_ASSET_RETURN_YN',
10628                p_tsu_code                      => 'ERROR',
10629                px_tcnv_rec                     => px_tcnv_rec);
10630          -- set the transaction record
10631          set_transaction_rec(
10632                p_return_status                 => x_return_status,
10633                p_overall_status                => px_overall_status,
10634                p_tmt_flag                      => 'TMT_ASSET_DISPOSITION_YN',
10635                p_tsu_code                      => 'ERROR',
10636                p_ret_val                       => NULL,
10637                px_tcnv_rec                     => px_tcnv_rec);
10638 
10639          -- Set the oracle error message
10640          OKL_API.set_message(
10641                          p_app_name      => OKC_API.G_APP_NAME,
10642                          p_msg_name      => g_unexpected_error,
10643                          p_token1        => g_sqlcode_token,
10644                          p_token1_value  => SQLCODE,
10645                          p_token2        => g_sqlerrm_token,
10646                          p_token2_value  => SQLERRM);
10647 
10648   END process_amortize_and_return;
10649 
10650   -- Start of comments
10651   --
10652   -- Procedure Name : lease_termination
10653   -- Desciption     : Main API which does the termination of Lease
10654   --                 Always rollback the whole process if processing transaction
10655   --                 fails this is done or else we lose information as to
10656   --                 the success/failure of different APIs
10657   --                 if the process is rolled back, then it will be picked
10658   --                 again by the batch_process
10659   -- Business Rules :
10660   -- Parameters     :
10661   -- History        : RMUNJULU 31-JAN-03 2780539 Added TER_MAN_PURCHASE check
10662   --                : RMUNJULU 27-JUN-03 3023206 Removed Process_Close_Streams
10663   --                  from this procedure as it is now called from
10664   --                  update_k_hdr_and_lines
10665   --                : RMUNJULU 3061751  Changed code to create a termination
10666   --                  trn even when request is NON BATCH and validation has failed
10667   --                : RMUNJULU 3018641 Added code to get and set TMG_RUN on OKL_TRX_MSGS
10668   --                : RMUNJULU 11-MAR-04 3485854 Changed the IF after evergreen to check for ERROR properly
10669   --                : rmunjulu EDAT Added code to get quote eff dates and set them as global
10670   --                : PAGARG 01-Mar-05 Bug 4190887 Pass klev_tbl to process_accounting_entries
10671   --                : rmunjulu BUYOUT_PROCESS
10672   -- Version     : 1.0
10673   --
10674   -- End of comments
10675   PROCEDURE lease_termination(
10676            p_api_version                 IN  NUMBER,
10677            p_init_msg_list               IN  VARCHAR2,
10678            x_return_status               OUT NOCOPY VARCHAR2,
10679            x_msg_count                   OUT NOCOPY NUMBER,
10680            x_msg_data                    OUT NOCOPY VARCHAR2,
10681            p_term_rec                    IN  term_rec_type,
10682            p_tcnv_rec                    IN  tcnv_rec_type) IS
10683    l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
10684    l_overall_status        VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
10685    lp_tcnv_rec             tcnv_rec_type;
10686    lx_stmv_tbl             stmv_tbl_type;
10687    lx_adjv_rec             adjv_rec_type;
10688    lx_ajlv_tbl             ajlv_tbl_type;
10689    lp_klev_tbl             klev_tbl_type;
10690    lx_klev_tbl             klev_tbl_type;
10691    lx_chrv_rec             chrv_rec_type;
10692    lx_clev_tbl             clev_tbl_type;
10693    lx_id                   NUMBER;
10694    i                       NUMBER := 1;
10695    l_tran_started          VARCHAR2(1)  := OKL_API.G_FALSE;
10696    l_evergreen_status      VARCHAR2(1)  := OKL_API.G_FALSE;
10697    l_api_name              VARCHAR2(30) := 'lease_termination';
10698    l_sys_date              DATE;
10699    l_trn_already_set       VARCHAR2(1)  := G_NO;
10700    lx_contract_status      VARCHAR2(200);
10701    l_validate              VARCHAR2(1) := OKL_API.G_RET_STS_ERROR;
10702    l_api_version           CONSTANT NUMBER := 1;
10703    l_status                VARCHAR2(200);
10704    l_term_rec              term_rec_type := p_term_rec;
10705 
10706    -- rmunjulu BUYOUT_PROCESS
10707    l_auto_invoice_yn  VARCHAR2(3);
10708    l_purchase_amount NUMBER;
10709    -- Added for bug# 6964174
10710 --start:|  05-29-08 cklee -- fixed bug: 7017824(R12)/OKL.H: bug#6964174              |
10711       l_is_securitized    VARCHAR2(1) := OKC_API.G_FALSE;
10712       l_inv_agmt_chr_id_tbl OKL_SECURITIZATION_PVT.inv_agmt_chr_id_tbl_type;
10713 --end:|  05-29-08 cklee -- fixed bug: 7017824(R12)/OKL.H: bug#6964174              |
10714 
10715    -- asawanka added for debug feature start
10716     l_module_name VARCHAR2(500) := G_MODULE_NAME || 'lease_termination';
10717     is_debug_exception_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_EXCEPTION);
10718     is_debug_procedure_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_PROCEDURE);
10719     is_debug_statement_on boolean := OKL_DEBUG_PUB.Check_Log_On (l_module_name, G_LEVEL_STATEMENT);
10720     -- asawanka added for debug feature end
10721 
10722   BEGIN
10723      IF (is_debug_procedure_on) THEN
10724        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'Begin(+)');
10725      END IF;
10726 
10727     -- Set the transaction
10728     l_return_status := OKL_API.START_ACTIVITY(l_api_name,
10729                                               G_PKG_NAME,
10730                                               p_init_msg_list,
10731                                               l_api_version,
10732                                               p_api_version,
10733                                               '_PVT',
10734                                               x_return_status);
10735 
10736     -- Rollback if error setting activity for api
10737     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10738       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10739     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10740       RAISE OKL_API.G_EXCEPTION_ERROR;
10741     END IF;
10742 
10743     -- Set the x return status
10744     x_return_status := OKL_API.G_RET_STS_SUCCESS;
10745 
10746     -- store the highest degree of error
10747     set_overall_status(
10748            p_return_status               => l_return_status,
10749            px_overall_status             => l_overall_status);
10750 
10751     -- If the termination request is from quote, populate the rest of the quote attributes
10752     set_database_values(
10753            px_term_rec                   => l_term_rec);
10754 
10755     -- Set the info messages intially
10756     set_info_messages(
10757            p_term_rec                    => l_term_rec);
10758 
10759     -- check if transaction already exists
10760     IF (p_tcnv_rec.id IS NOT NULL AND p_tcnv_rec.id <> OKL_API.G_MISS_NUM) THEN
10761       l_trn_already_set := G_YES;
10762     END IF;
10763 
10764     --get sysdate
10765     SELECT SYSDATE INTO l_sys_date FROM DUAL;
10766 
10767     IF l_trn_already_set = G_NO THEN
10768 
10769       -- initialize the transaction rec
10770       initialize_transaction (
10771           px_tcnv_rec                   => lp_tcnv_rec,
10772           p_term_rec                    => l_term_rec,
10773           p_sys_date                    => l_sys_date,
10774           p_control_flag                => 'CREATE',
10775 	  x_return_status               => l_return_status,
10776  	  --akrangan bug 5354501 fix start
10777  	  x_msg_count                   => x_msg_count,
10778  	  x_msg_data                    => x_msg_data);
10779 	  --akrangan bug 5354501 fix end
10780       -- rollback if intialize transaction failed
10781       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10782         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10783       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10784         RAISE OKL_API.G_EXCEPTION_ERROR;
10785       END IF;
10786 
10787       -- insert the transaction record
10788       process_transaction(
10789           p_api_version                => p_api_version,
10790           p_init_msg_list              => OKL_API.G_FALSE,
10791           x_return_status              => l_return_status,
10792           x_msg_count                  => x_msg_count,
10793           x_msg_data                   => x_msg_data,
10794           p_id                          => 0,
10795           p_term_rec                    => l_term_rec,
10796           p_tcnv_rec                    => lp_tcnv_rec,
10797           x_id                          => lx_id,
10798           p_trn_mode                    => 'INSERT');
10799 
10800       -- rollback if processing transaction failed
10801       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10802         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10803       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10804         RAISE OKL_API.G_EXCEPTION_ERROR;
10805       END IF;
10806 
10807       -- set the trn rec id
10808       lp_tcnv_rec.id := lx_id;
10809 
10810     ELSE -- transaction already set
10811 
10812       lp_tcnv_rec := p_tcnv_rec;
10813 
10814       -- initialize the transaction rec
10815       initialize_transaction (
10816           px_tcnv_rec                   => lp_tcnv_rec,
10817           p_term_rec                    => l_term_rec,
10818           p_sys_date                    => l_sys_date,
10819           p_control_flag                => 'UPDATE',
10820 	  x_return_status               => l_return_status,
10821  	  --akrangan bug 5354501 fix start
10822  	  x_msg_count                   => x_msg_count,
10823  	  x_msg_data                    => x_msg_data);
10824 	  --akrangan bug 5354501 fix end
10825 
10826 
10827       -- rollback if intialize transaction failed
10828       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10829         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10830       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10831         RAISE OKL_API.G_EXCEPTION_ERROR;
10832       END IF;
10833 
10834     END IF;
10835 
10836     -- rmunjulu +++++++++ Effective Dated Termination -- start  ++++++++++++++++
10837 
10838     -- rmunjulu EDAT Get the quote effectivity date and quote acceptance date
10839     -- and store as global variables, will be used later on in other procedures
10840     IF (is_debug_statement_on) THEN
10841       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling    OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_quote_dates');
10842       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_term_rec.p_quote_id = '||l_term_rec.p_quote_id);
10843     END IF;
10844 
10845     OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_quote_dates(
10846           p_qte_id              => l_term_rec.p_quote_id,
10847           x_return_status       => l_return_status);
10848     IF (is_debug_statement_on) THEN
10849       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called    OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_quote_dates');
10850       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_return_status = '||l_return_status);
10851     END IF;
10852 
10853     -- Rollback if error setting activity for api
10854     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10855       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10856     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10857       RAISE OKL_API.G_EXCEPTION_ERROR;
10858     END IF;
10859 
10860     -- rmunjulu +++++++++ Effective Dated Termination -- end    ++++++++++++++++
10861 
10862     -- check if lease valid
10863     validate_lease(
10864         p_api_version                   => p_api_version,
10865         p_init_msg_list                 => OKL_API.G_FALSE,
10866         x_return_status                 => l_return_status,
10867         x_msg_count                     => x_msg_count,
10868         x_msg_data                      => x_msg_data,
10869         p_sys_date                       => l_sys_date,
10870         p_term_rec                       => l_term_rec);
10871 
10872     -- Store the validation return status
10873     l_validate  := l_return_status;
10874 
10875     -- store the highest degree of error
10876     set_overall_status(
10877         p_return_status                  => l_return_status,
10878         px_overall_status                => l_overall_status);
10879 
10880     IF (is_debug_statement_on) THEN
10881       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_term_rec.p_control_flag = '||l_term_rec.p_control_flag);
10882     END IF;
10883 
10884     IF (l_term_rec.p_control_flag = 'BATCH_PROCESS') THEN
10885 
10886     IF (is_debug_statement_on) THEN
10887       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Calling OKL_AM_LEASE_LOAN_TRMNT_PUB.validate_contract');
10888       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_term_rec.p_contract_id = '||l_term_rec.p_contract_id);
10889     END IF;
10890 
10891       -- Since batch process is not checked initially in LLT check here
10892       OKL_AM_LEASE_LOAN_TRMNT_PUB.validate_contract(
10893            p_api_version                 => p_api_version,
10894            p_init_msg_list               => OKL_API.G_FALSE,
10895            x_return_status               => l_return_status,
10896            x_msg_count                   => x_msg_count,
10897            x_msg_data                    => x_msg_data,
10898            p_contract_id                 => l_term_rec.p_contract_id,
10899            p_control_flag                => l_term_rec.p_control_flag,
10900            x_contract_status             => lx_contract_status);
10901     IF (is_debug_statement_on) THEN
10902       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'Called OKL_AM_LEASE_LOAN_TRMNT_PUB.validate_contract l_return_status = '||l_return_status);
10903       OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_STATEMENT,l_module_name  ,'l_validate = '||l_validate);
10904     END IF;
10905 
10906       -- Store the highest validation return status
10907       -- To capture the return status of validate lease called above
10908       IF (l_validate = OKL_API.G_RET_STS_SUCCESS) THEN
10909         l_validate  := l_return_status;
10910       END IF;
10911 
10912       -- store the highest degree of error
10913       set_overall_status(
10914         p_return_status                 => l_validate, -- RMUNJULU 3018641 Changed from l_return_status
10915         px_overall_status               => l_overall_status);
10916 
10917       -- set the transaction record
10918       set_transaction_rec(
10919         p_return_status                 => l_validate, -- RMUNJULU 3018641 Changed from l_return_status
10920         p_overall_status                => l_overall_status,
10921         p_tmt_flag                      => 'TMT_VALIDATED_YN',
10922         p_tsu_code                      => 'ENTERED',
10923         px_tcnv_rec                     => lp_tcnv_rec);
10924 
10925       -- if validation failed then insert transaction
10926       -- AND abort else continue next process
10927       IF (l_validate <> OKL_API.G_RET_STS_SUCCESS) THEN
10928 
10929         -- Validation of contract failed.
10930         OKL_API.set_message( p_app_name      => G_APP_NAME,
10931                              p_msg_name      => 'OKL_AM_VAL_OF_K_FAILED');
10932 
10933         -- set the transaction record
10934         set_transaction_rec(
10935           p_return_status               => l_validate, -- RMUNJULU 3018641 Changed from l_return_status
10936           p_overall_status              => l_overall_status,
10937           p_tmt_flag                    => 'TMT_VALIDATED_YN',
10938           p_tsu_code                    => 'ERROR',
10939           px_tcnv_rec                   => lp_tcnv_rec);
10940 
10941         -- update the transaction record
10942         process_transaction(
10943           p_api_version                => p_api_version,
10944           p_init_msg_list              => OKL_API.G_FALSE,
10945           x_return_status              => l_return_status,
10946           x_msg_count                  => x_msg_count,
10947           x_msg_data                   => x_msg_data,
10948           p_id                          => 0,
10949           p_term_rec                    => l_term_rec,
10950           p_tcnv_rec                    => lp_tcnv_rec,
10951           x_id                          => lx_id,
10952           p_trn_mode                    => 'UPDATE');
10953 
10954         -- rollback if processing transaction failed
10955         IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10956           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10957         ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10958           RAISE OKL_API.G_EXCEPTION_ERROR;
10959         END IF;
10960 
10961         -- Save messages from stack into transaction message table
10962         OKL_AM_UTIL_PVT.process_messages(
10963          p_trx_source_table            => 'OKL_TRX_CONTRACTS',
10964          p_trx_id                     => lp_tcnv_rec.id,
10965          x_return_status                => l_return_status);
10966 
10967         -- RMUNJULU 3018641 Added code to get and set TMG_RUN
10968         OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run(
10969                p_trx_id         => lp_tcnv_rec.id,
10970                x_return_status  => l_return_status);
10971 
10972         -- abort since validation failed
10973         RAISE G_EXCEPTION_HALT_VALIDATION;
10974       END IF;
10975 
10976     ELSE --( not from batch process) then
10977 
10978 /* -- RMUNJULU 3061751 Changed this code to create a termination trn even when
10979 -- request is NON BATCH and validation has failed
10980 
10981       -- rollback if validation failed
10982       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
10983         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
10984       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
10985         RAISE OKL_API.G_EXCEPTION_ERROR;
10986       END IF;
10987 
10988       -- set the transaction record
10989       set_transaction_rec(
10990         p_return_status                 => l_return_status,
10991         p_overall_status                => l_overall_status,
10992         p_tmt_flag                      => 'TMT_VALIDATED_YN',
10993         p_tsu_code                      => 'ENTERED',
10994         px_tcnv_rec                     => lp_tcnv_rec);
10995 */
10996 
10997         -- RMUNJULU 3061751 Changed this code to create a termination trn even when
10998         -- request is NON BATCH and validation has failed
10999 
11000         IF (l_validate <> OKL_API.G_RET_STS_SUCCESS) THEN
11001 
11002         -- Validation of contract failed.
11003         OKL_API.set_message( p_app_name      => G_APP_NAME,
11004                              p_msg_name      => 'OKL_AM_VAL_OF_K_FAILED');
11005 
11006         -- set the transaction record
11007         set_transaction_rec(
11008           p_return_status               => l_validate, -- RMUNJULU 3018641 Changed from l_return_status
11009           p_overall_status              => l_overall_status,
11010           p_tmt_flag                    => 'TMT_VALIDATED_YN',
11011           p_tsu_code                    => 'ERROR',
11012           px_tcnv_rec                   => lp_tcnv_rec);
11013 
11014         -- update the transaction record
11015         process_transaction(
11016           p_api_version                => p_api_version,
11017           p_init_msg_list              => OKL_API.G_FALSE,
11018           x_return_status              => l_return_status,
11019           x_msg_count                  => x_msg_count,
11020           x_msg_data                   => x_msg_data,
11021           p_id                            => 0,
11022           p_term_rec                      => l_term_rec,
11023           p_tcnv_rec                      => lp_tcnv_rec,
11024           x_id                            => lx_id,
11025           p_trn_mode                      => 'UPDATE');
11026 
11027         -- rollback if processing transaction failed
11028         IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
11029           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11030         ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
11031           RAISE OKL_API.G_EXCEPTION_ERROR;
11032         END IF;
11033 
11034         -- Save messages from stack into transaction message table
11035         OKL_AM_UTIL_PVT.process_messages(
11036          p_trx_source_table    => 'OKL_TRX_CONTRACTS',
11037          p_trx_id         => lp_tcnv_rec.id,
11038          x_return_status        => l_return_status);
11039 
11040         -- RMUNJULU 3018641 Added code to get and set TMG_RUN
11041         OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run(
11042                p_trx_id         => lp_tcnv_rec.id,
11043                x_return_status  => l_return_status);
11044 
11045         -- abort since validation failed
11046         RAISE G_EXCEPTION_HALT_VALIDATION;
11047 
11048         END IF;
11049 
11050     END IF;
11051 
11052     -- get the lines
11053     get_contract_lines(
11054         p_api_version                  => p_api_version,
11055         p_init_msg_list                => OKL_API.G_FALSE,
11056         x_return_status                => l_return_status,
11057         x_msg_count                    => x_msg_count,
11058         x_msg_data                     => x_msg_data,
11059         p_term_rec                      => l_term_rec,
11060         x_klev_tbl                      => lx_klev_tbl);
11061 
11062     -- check and process an evergreen lease
11063     process_evergreen_contract(
11064           p_api_version                  => p_api_version,
11065           p_init_msg_list                => OKL_API.G_FALSE,
11066           x_return_status                => l_return_status,
11067           x_msg_count                    => x_msg_count,
11068           x_msg_data                     => x_msg_data,
11069           p_term_rec                      => l_term_rec,
11070           p_sys_date                      => l_sys_date,
11071           p_trn_already_set               => l_trn_already_set,
11072           p_klev_tbl                      => lx_klev_tbl, -- pagarg 4190887 Added
11073           px_overall_status               => l_overall_status,
11074           px_tcnv_rec                     => lp_tcnv_rec,
11075           x_evergreen_status              => l_evergreen_status);
11076 
11077     -- Update transaction only if evergreen was found and successful or if error
11078     IF  (l_evergreen_status = G_YES
11079          AND l_return_status = OKL_API.G_RET_STS_SUCCESS)
11080     -- RMUNJULU 11-MAR-04 3485854
11081     -- Changed the IF condition to check for ERROR properly
11082     OR  (l_return_status <> OKL_API.G_RET_STS_SUCCESS ) THEN
11083 
11084       -- update the transaction record
11085       process_transaction(
11086           p_api_version                  => p_api_version,
11087           p_init_msg_list                => OKL_API.G_FALSE,
11088           x_return_status                => l_return_status,
11089           x_msg_count                    => x_msg_count,
11090           x_msg_data                     => x_msg_data,
11091           p_id                            => 0,
11092           p_term_rec                      => l_term_rec,
11093           p_tcnv_rec                      => lp_tcnv_rec,
11094           x_id                            => lx_id,
11095           p_trn_mode                      => 'UPDATE');
11096 
11097       -- rollback if processing transaction failed
11098       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
11099         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11100       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
11101         RAISE OKL_API.G_EXCEPTION_ERROR;
11102       END IF;
11103 
11104       -- Save messages from stack into transaction message table
11105       OKL_AM_UTIL_PVT.process_messages(
11106        p_trx_source_table               => 'OKL_TRX_CONTRACTS',
11107        p_trx_id                        => lp_tcnv_rec.id,
11108        x_return_status                   => l_return_status);
11109 
11110         -- RMUNJULU 3018641 Added code to get and set TMG_RUN
11111         OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run(
11112                p_trx_id         => lp_tcnv_rec.id,
11113                x_return_status  => l_return_status);
11114 
11115       -- abort since evergreen was found
11116       RAISE G_EXCEPTION_HALT_VALIDATION;
11117     END IF;
11118 
11119     -- do cancellation of insurance
11120     process_cancel_insurance(
11121         p_api_version                  => p_api_version,
11122         p_init_msg_list                => OKL_API.G_FALSE,
11123         x_return_status                => l_return_status,
11124         x_msg_count                    => x_msg_count,
11125         x_msg_data                     => x_msg_data,
11126         p_term_rec                      => l_term_rec,
11127         px_overall_status               => l_overall_status,
11128         px_tcnv_rec                     => lp_tcnv_rec,
11129         p_sys_date                      => l_sys_date,
11130         p_trn_already_set               => l_trn_already_set);
11131 
11132     -- rmunjulu Check the buyout process BUYOUT_PROCESS
11133     check_auto_invoice_yn(
11134            p_term_rec        => l_term_rec,
11135            x_auto_invoce_yn  => l_auto_invoice_yn,
11136            x_return_status   => l_return_status);
11137 
11138     -- do closing of balances
11139     process_close_balances(
11140         p_api_version                  => p_api_version,
11141         p_init_msg_list                => OKL_API.G_FALSE,
11142         x_return_status                => l_return_status,
11143         x_msg_count                    => x_msg_count,
11144         x_msg_data                     => x_msg_data,
11145         p_term_rec                      => l_term_rec,
11146         px_overall_status               => l_overall_status,
11147         px_tcnv_rec                     => lp_tcnv_rec,
11148         x_adjv_rec                      => lx_adjv_rec,
11149         x_ajlv_tbl                      => lx_ajlv_tbl,
11150         p_sys_date                      => l_sys_date,
11151         p_trn_already_set               => l_trn_already_set,
11152         p_auto_invoice_yn               => l_auto_invoice_yn, -- rmunjulu BUYOUT_PROCESS
11153   p_klev_tbl                      => lx_klev_tbl); -- rmunjulu BUYOUT_PROCESS
11154 
11155     -- RMUNJULU BUG # 3023206 Moved Close Streams into update_k_hdr_and_lines
11156     -- as accounting uses some CURR streams and so they should not be closed
11157     -- before accounting is done
11158 
11159 /*
11160     -- process close streams
11161     process_close_streams(
11162         p_api_version                  => p_api_version,
11163         p_init_msg_list                => OKL_API.G_FALSE,
11164         x_return_status                => l_return_status,
11165         x_msg_count                    => x_msg_count,
11166         x_msg_data                     => x_msg_data,
11167         p_term_rec                      => l_term_rec,
11168         px_overall_status               => l_overall_status,
11169         px_tcnv_rec                     => lp_tcnv_rec,
11170         x_stmv_tbl                      => lx_stmv_tbl,
11171         p_sys_date                      => l_sys_date,
11172         p_trn_already_set               => l_trn_already_set);
11173 */
11174     -- do accounting entries
11175     process_accounting_entries(
11176          p_api_version                  => p_api_version,
11177          p_init_msg_list                => OKL_API.G_FALSE,
11178          x_return_status                => l_return_status,
11179          x_msg_count                    => x_msg_count,
11180          x_msg_data                     => x_msg_data,
11181          p_term_rec                     => l_term_rec,
11182          px_overall_status              => l_overall_status,
11183          px_tcnv_rec                    => lp_tcnv_rec,
11184          p_sys_date                     => l_sys_date,
11185          p_klev_tbl                     => lx_klev_tbl, -- PAGARG 4190887 Added
11186          p_trn_already_set              => l_trn_already_set);
11187 
11188     -- RMUNJULU 31-JAN-03 2780539 Added TER_MAN_PURCHASE which is also a
11189     -- termination with purchase
11190     IF (l_term_rec.p_quote_type IN('TER_PURCHASE',
11191                                    'TER_RECOURSE',
11192                                    'TER_ROLL_PURCHASE',
11193                                    'TER_MAN_PURCHASE')) THEN
11194 
11195       -- do asset dispose
11196       process_asset_dispose(
11197          p_api_version                  => p_api_version,
11198          p_init_msg_list                => OKL_API.G_FALSE,
11199          x_return_status                => l_return_status,
11200          x_msg_count                    => x_msg_count,
11201          x_msg_data                     => x_msg_data,
11202          p_term_rec                     => l_term_rec,
11203          px_overall_status              => l_overall_status,
11204          p_sys_date                     => l_sys_date, -- rmunjulu EDAT -- pass sysdate to asset dispose api
11205          px_tcnv_rec                    => lp_tcnv_rec,
11206          p_klev_tbl                     => lx_klev_tbl,
11207          p_trn_already_set              => l_trn_already_set);
11208 
11209       -- Amortization of assets not needed since termination with purchase
11210       OKL_API.set_message( p_app_name   => G_APP_NAME,
11211                            p_msg_name   => 'OKL_AM_AMORTIZE_NOT_NEED');
11212 
11213       -- Return of assets not needed since termination with purchase
11214       OKL_API.set_message( p_app_name   => G_APP_NAME,
11215                            p_msg_name   => 'OKL_AM_RETURN_NOT_NEED');
11216 
11217     ELSIF (l_auto_invoice_yn = 'Y')   THEN -- rmunjulu BUYOUT_PROCESS
11218 
11219       process_asset_dispose(
11220          p_api_version                  => p_api_version,
11221          p_init_msg_list                => OKL_API.G_FALSE,
11222          x_return_status                => l_return_status,
11223          x_msg_count                    => x_msg_count,
11224          x_msg_data                     => x_msg_data,
11225          p_term_rec                     => l_term_rec,
11226          px_overall_status              => l_overall_status,
11227          p_sys_date                     => l_sys_date,
11228          px_tcnv_rec                    => lp_tcnv_rec,
11229          p_klev_tbl                     => lx_klev_tbl,
11230          p_trn_already_set              => l_trn_already_set,
11231    p_auto_invoice_yn              => l_auto_invoice_yn); -- rmunjulu BUYOUT_PROCESS
11232 
11233       -- Amortization of assets not needed since termination with purchase
11234       OKL_API.set_message( p_app_name   => G_APP_NAME,
11235                            p_msg_name   => 'OKL_AM_AMORTIZE_NOT_NEED');
11236 
11237       -- Return of assets not needed since termination with purchase
11238       OKL_API.set_message( p_app_name   => G_APP_NAME,
11239                            p_msg_name   => 'OKL_AM_RETURN_NOT_NEED');
11240 
11241     ELSE
11242       -- do amortization and asset return
11243       process_amortize_and_return(
11244          p_api_version                  => p_api_version,
11245          p_init_msg_list                => OKL_API.G_FALSE,
11246          x_return_status                => l_return_status,
11247          x_msg_count                    => x_msg_count,
11248          x_msg_data                     => x_msg_data,
11249          p_term_rec                     => l_term_rec,
11250          px_overall_status              => l_overall_status,
11251          px_tcnv_rec                    => lp_tcnv_rec,
11252          p_sys_date                     => l_sys_date,
11253          p_klev_tbl                     => lx_klev_tbl,
11254          p_trn_already_set              => l_trn_already_set);
11255 
11256       -- Disposition of assets not needed since termination without purchase
11257       OKL_API.set_message( p_app_name   => G_APP_NAME,
11258                            p_msg_name   => 'OKL_AM_DISPOSE_NOT_NEED');
11259 
11260     END IF;
11261 
11262     -- update the contract only if the overall_status is success
11263     IF (l_overall_status = OKL_API.G_RET_STS_SUCCESS) THEN
11264 --start:|  05-29-08 cklee -- fixed bug: 7017824(R12)/OKL.H: bug#6964174              |
11265         -- Added Bug# 6964174 - Start
11266              -- Introduced the call to modify pool content api to inactivate pool contents
11267              -- and recreate them. This was initially present as part of the early termination
11268              -- workflow and the inactivation portion as part of the bug fix is being moved from
11269              -- the workflow to the last step in termination transaction so that the accounting
11270              -- transaction like termination billing, termination accounting can have special
11271              -- accounting before the pool contents are inactivated.
11272 
11273              -- Logic to run only for early termination -- check for quote_id
11274              IF l_term_rec.p_quote_id IS NOT NULL AND l_term_rec.p_quote_id <> OKL_API.G_MISS_NUM THEN
11275                -- Check if contract is securitized for RENT
11276                OKL_SECURITIZATION_PVT.check_khr_securitized(
11277                                  p_api_version          => p_api_version
11278                                 ,p_init_msg_list        => OKL_API.G_FALSE
11279                                 ,x_return_status        => l_return_status
11280                                 ,x_msg_count            => x_msg_count
11281                                 ,x_msg_data             => x_msg_data
11282                                 ,p_khr_id               => l_term_rec.p_contract_id
11283                                 ,p_effective_date       => okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date
11284                                 ,p_stream_type_subclass => 'RENT'
11285                                 ,x_value                => l_is_securitized
11286                                 ,x_inv_agmt_chr_id_tbl  => l_inv_agmt_chr_id_tbl );
11287            -- rollback if processing transaction failed
11288            IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
11289              RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11290            ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
11291              RAISE OKL_API.G_EXCEPTION_ERROR;
11292            END IF;
11293 
11294                -- if contract is securitized, then call the modify pool contents. Prior to fix for bug# 6964174
11295                -- this was present in OKL_AM_SECURITIZATION_PVT.create_pool_transaction
11296                IF l_is_securitized = OKC_API.G_TRUE THEN
11297                  OKL_SECURITIZATION_PVT.MODIFY_POOL_CONTENTS
11298                    ( p_api_version        => p_api_version
11299                     ,p_init_msg_list      => OKL_API.G_FALSE
11300                     ,p_transaction_reason => OKL_SECURITIZATION_PVT.G_TRX_REASON_EARLY_TERMINATION
11301                     ,p_khr_id             => l_term_rec.p_contract_id
11302                     ,p_stream_type_subclass => 'RENT'
11303                     ,p_transaction_date   => okl_am_lease_loan_trmnt_pvt.g_quote_accept_date
11304                     ,p_effective_date     => okl_am_lease_loan_trmnt_pvt.g_quote_eff_from_date
11305                     ,x_return_status      => l_return_status
11306                     ,x_msg_count          => x_msg_count
11307                     ,x_msg_data           => x_msg_data  );
11308              -- rollback if processing transaction failed
11309              IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
11310                RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11311              ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
11312                RAISE OKL_API.G_EXCEPTION_ERROR;
11313              END IF;
11314                END IF;
11315              END IF;
11316              -- Added Bug# 6964174 - End
11317 --end:|  05-29-08 cklee -- fixed bug: 7017824(R12)/OKL.H: bug#6964174              |
11318 
11319       -- Set the p_status (which sets the sts_code) for the contract
11320       IF  l_term_rec.p_control_flag = 'BATCH_PROCESS'
11321       AND (   l_term_rec.p_quote_id IS NULL
11322            OR l_term_rec.p_quote_id = OKL_API.G_MISS_NUM) THEN
11323          l_status := 'EXPIRED';
11324       ELSE
11325          l_status := 'TERMINATED';
11326       END IF;
11327 
11328       -- set_and_update_contract
11329       update_k_hdr_and_lines(
11330         p_api_version                  => p_api_version,
11331         p_init_msg_list                => OKL_API.G_FALSE,
11332         x_return_status                => l_return_status,
11333         x_msg_count                    => x_msg_count,
11334         x_msg_data                     => x_msg_data,
11335         p_status                       => l_status,
11336         p_term_rec                      => l_term_rec,
11337         p_klev_tbl                      => lx_klev_tbl,
11338         p_trn_reason_code               => lp_tcnv_rec.trn_code,
11339         px_overall_status               => l_overall_status,
11340         px_tcnv_rec                     => lp_tcnv_rec,
11341         x_chrv_rec                      => lx_chrv_rec,
11342         x_clev_tbl                      => lx_clev_tbl,
11343         p_sys_date                      => l_sys_date);
11344 
11345       IF l_return_status = OKL_API.G_RET_STS_SUCCESS THEN
11346 
11347         -- set the transaction record
11348         set_transaction_rec(
11349           p_return_status               => l_return_status,
11350           p_overall_status              => l_overall_status,
11351           p_tmt_flag                    => 'TMT_CONTRACT_UPDATED_YN',
11352           p_tsu_code                    => 'PROCESSED',
11353           px_tcnv_rec                   => lp_tcnv_rec);
11354 
11355       ELSE -- Update of K hdr and lines failed
11356 
11357         -- Contract table update failed.
11358         OKL_API.set_message( p_app_name => G_APP_NAME,
11359                              p_msg_name => 'OKL_AM_ERR_K_UPD');
11360 
11361         -- set the transaction record
11362         set_transaction_rec(
11363           p_return_status               => l_return_status,
11364           p_overall_status              => l_overall_status,
11365           p_tmt_flag                    => 'TMT_CONTRACT_UPDATED_YN',
11366           p_tsu_code                    => 'ERROR',
11367           px_tcnv_rec                   => lp_tcnv_rec);
11368       END IF;
11369     END IF;
11370 
11371     -- update the transaction record
11372     process_transaction(
11373           p_api_version                => p_api_version,
11374           p_init_msg_list              => OKL_API.G_FALSE,
11375           x_return_status              => l_return_status,
11376           x_msg_count                  => x_msg_count,
11377           x_msg_data                   => x_msg_data,
11378           p_id                          => 0,
11379           p_term_rec                    => l_term_rec,
11380           p_tcnv_rec                    => lp_tcnv_rec,
11381           x_id                          => lx_id,
11382           p_trn_mode                    => 'UPDATE');
11383 
11384     -- rollback if processing transaction failed
11385     IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
11386       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
11387     ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
11388       RAISE OKL_API.G_EXCEPTION_ERROR;
11389     END IF;
11390 
11391     -- Save messages from stack into transaction message table
11392     OKL_AM_UTIL_PVT.process_messages(
11393      p_trx_source_table               => 'OKL_TRX_CONTRACTS',
11394      p_trx_id                        => lp_tcnv_rec.id,
11395      x_return_status                   => l_return_status);
11396 
11397         -- RMUNJULU 3018641 Added code to get and set TMG_RUN
11398         OKL_AM_LEASE_LOAN_TRMNT_PVT.get_set_tmg_run(
11399                p_trx_id         => lp_tcnv_rec.id,
11400                x_return_status  => l_return_status);
11401 
11402     -- Set the return status
11403 
11404     x_return_status  :=  OKL_API.G_RET_STS_SUCCESS;
11405 
11406 
11407     OKL_API.END_ACTIVITY(x_msg_count, x_msg_data);
11408     IF (is_debug_procedure_on) THEN
11409        OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_PROCEDURE,l_module_name  ,'End(-)');
11410     END IF;
11411   EXCEPTION
11412     WHEN G_EXCEPTION_HALT_VALIDATION THEN
11413         IF (is_debug_exception_on) THEN
11414              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_HALT_VALIDATION');
11415         END IF;
11416 
11417       x_return_status := OKL_API.G_RET_STS_SUCCESS;
11418     WHEN OKL_API.G_EXCEPTION_ERROR THEN
11419         IF (is_debug_exception_on) THEN
11420              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_ERROR');
11421         END IF;
11422 
11423       x_return_status := OKL_API.HANDLE_EXCEPTIONS
11424       (
11425         l_api_name,
11426         G_PKG_NAME,
11427         'OKL_API.G_RET_STS_ERROR',
11428         x_msg_count,
11429         x_msg_data,
11430         '_PVT'
11431       );
11432     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
11433         IF (is_debug_exception_on) THEN
11434              OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'G_EXCEPTION_UNEXPECTED_ERROR');
11435         END IF;
11436 
11437       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
11438       (
11439         l_api_name,
11440         G_PKG_NAME,
11441         'OKL_API.G_RET_STS_UNEXP_ERROR',
11442         x_msg_count,
11443         x_msg_data,
11444         '_PVT'
11445       );
11446     WHEN OTHERS THEN
11447         IF (is_debug_exception_on) THEN
11448            OKL_DEBUG_PUB.LOG_DEBUG(G_LEVEL_EXCEPTION,l_module_name, 'EXCEPTION :'||'OTHERS, SQLCODE: ' || sqlcode || ' , SQLERRM : ' || sqlerrm);
11449         END IF;
11450 
11451       x_return_status :=OKL_API.HANDLE_EXCEPTIONS
11452       (
11453         l_api_name,
11454         G_PKG_NAME,
11455         'OTHERS',
11456         x_msg_count,
11457         x_msg_data,
11458         '_PVT'
11459       );
11460     END lease_termination;
11461 
11462 
11463 END OKL_AM_LEASE_TRMNT_PVT;