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