DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKL_FUNDING_PVT

Source


1 PACKAGE BODY OKL_FUNDING_PVT AS
2 /* $Header: OKLCFUNB.pls 120.72.12020000.6 2013/04/02 12:30:01 racheruv ship $ */
3 ----------------------------------------------------------------------------
4 -- Global Message Constants
5 ----------------------------------------------------------------------------
6 -- see FND_NEW_MESSAGES for full message text
7 G_NOT_FOUND                  CONSTANT VARCHAR2(30) := 'OKC_NOT_FOUND';  -- message_name
8 G_NOT_FOUND_V1               CONSTANT VARCHAR2(30) := 'VALUE1';         -- token 1
9 G_NOT_FOUND_V2               CONSTANT VARCHAR2(30) := 'VALUE2';         -- token 2
10 
11 G_NOT_UNIQUE                 CONSTANT VARCHAR2(30) := 'OKL_LLA_NOT_UNIQUE';
12 G_UNEXPECTED_ERROR           CONSTANT VARCHAR2(30) := 'OKL_UNEXPECTED_ERROR';
13 G_SQLERRM_TOKEN              CONSTANT VARCHAR2(30) := 'OKL_SQLERRM';
14 G_SQLCODE_TOKEN              CONSTANT VARCHAR2(30) := 'OKL_SQLCODE';
15 
16 G_NO_INIT_MSG                CONSTANT VARCHAR2(1)  := OKL_API.G_FALSE;
17 G_VIEW                       CONSTANT VARCHAR2(30) := 'OKL_TRX_AP_INVOICES_V';
18 
19 G_FND_APP                    CONSTANT VARCHAR2(30) := OKL_API.G_FND_APP;
20 G_FORM_UNABLE_TO_RESERVE_REC CONSTANT VARCHAR2(30) := OKL_API.G_FORM_UNABLE_TO_RESERVE_REC;
21 G_FORM_RECORD_DELETED        CONSTANT VARCHAR2(30) := OKL_API.G_FORM_RECORD_DELETED;
22 G_FORM_RECORD_CHANGED        CONSTANT VARCHAR2(30) := OKL_API.G_FORM_RECORD_CHANGED;
23 G_RECORD_LOGICALLY_DELETED	 CONSTANT VARCHAR2(30) := OKL_API.G_RECORD_LOGICALLY_DELETED;
24 G_REQUIRED_VALUE             CONSTANT VARCHAR2(30) := 'OKL_REQUIRED_VALUE';
25 G_INVALID_VALUE              CONSTANT VARCHAR2(30) := OKL_API.G_INVALID_VALUE;
26 G_COL_NAME_TOKEN             CONSTANT VARCHAR2(30) := OKL_API.G_COL_NAME_TOKEN;
27 G_PARENT_TABLE_TOKEN         CONSTANT VARCHAR2(30) := OKL_API.G_PARENT_TABLE_TOKEN;
28 G_CHILD_TABLE_TOKEN          CONSTANT VARCHAR2(30) := OKL_API.G_CHILD_TABLE_TOKEN;
29 G_NO_PARENT_RECORD           CONSTANT VARCHAR2(30) :='OKL_NO_PARENT_RECORD';
30 G_NOT_SAME                   CONSTANT VARCHAR2(30) :='OKL_CANNOT_BE_SAME';
31 
32 G_PREFUNDING_TYPE            CONSTANT VARCHAR2(30) :='PREFUNDING';
33 G_ASSET_TYPE                 CONSTANT VARCHAR2(30) :='ASSET';
34 G_INVOICE_TYPE               CONSTANT VARCHAR2(30) :='INVOICE';
35 G_FUNDING_TRX_TYPE           CONSTANT VARCHAR2(30) :='FUNDING';
36 
37  G_CREDIT_CHKLST_TPL CONSTANT VARCHAR2(30) := 'LACCLH';
38  G_CREDIT_CHKLST_TPL_RULE1 CONSTANT VARCHAR2(30) := 'LACCLT';
39  G_CREDIT_CHKLST_TPL_RULE2 CONSTANT VARCHAR2(30) := 'LACCLD';
40  G_CREDIT_CHKLST_TPL_RULE3 CONSTANT VARCHAR2(30) := 'LACLFD';
41 
42  G_FUNDING_CHKLST_TPL CONSTANT VARCHAR2(30) := 'LAFCLH';
43 -- G_FUNDING_CHKLST_TPL_RULE1 CONSTANT VARCHAR2(30) := 'LAFCLT';
44  G_FUNDING_CHKLST_TPL_RULE1 CONSTANT VARCHAR2(30) := 'LAFCLD';
45  G_RGP_TYPE CONSTANT VARCHAR2(30) := 'KRG';
46 
47 --cklee added user defined stream type modification
48  G_STY_PURPOSE_CODE_PREFUNDING        CONSTANT VARCHAR2(30) := 'PREFUNDING';
49  G_STY_PURPOSE_CODE_FUNDING           CONSTANT VARCHAR2(30) := 'FUNDING';
50  G_STY_PURPOSE_CODE_P_BALANCE         CONSTANT VARCHAR2(30) := 'PRINCIPAL_BALANCE';
51 --cklee added user defined stream type modification
52 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
53  G_OKL_LEASE_APP        CONSTANT VARCHAR2(30) := 'OKL_LEASE_APP';
54 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
55 
56 /*
57 -- vthiruva, 08/31/2004
58 -- Added Constants to enable Business Event
59 */
60 G_WF_EVT_FUN_REQ_CREATED CONSTANT VARCHAR2(50) := 'oracle.apps.okl.la.funding_request.created';
61 G_WF_EVT_FUN_REQ_UPDATED CONSTANT VARCHAR2(50) := 'oracle.apps.okl.la.funding_request.updated';
62 G_WF_EVT_FUN_REQ_CANCELLED CONSTANT VARCHAR2(50) := 'oracle.apps.okl.la.funding_request.cancelled';
63 G_WF_EVT_FUN_REQ_SUBMITTED CONSTANT VARCHAR2(50) := 'oracle.apps.okl.la.funding_request.submitted';
64 G_WF_ITM_CONTRACT_ID CONSTANT VARCHAR2(30) := 'CONTRACT_ID';
65 G_WF_ITM_FUN_REQ_ID CONSTANT VARCHAR2(30) := 'FUNDING_REQUEST_ID';
66 /*
67 -- cklee, 12/21/2005
68 -- Added Constants to enable Business Event bug#4901292
69 */
70 --START: 04-Jan-2006  cklee -- Fixed bug#4925269                                    |
71 G_WF_EVT_FUN_LIST_VALIDATED CONSTANT VARCHAR2(250) := 'oracle.apps.okl.sales.leaseapplication.validated_funding_request';
72 --END: 04-Jan-2006  cklee -- Fixed bug#4925269                                    |
73 
74 ----------------------------------------------------------------------------
75 -- Data Structures
76 ----------------------------------------------------------------------------
77 
78   subtype rgpv_rec_type is okl_okc_migration_pvt.rgpv_rec_type;
79   subtype rgpv_tbl_type is okl_okc_migration_pvt.rgpv_tbl_type;
80   subtype rulv_rec_type is okl_rule_pub.rulv_rec_type;
81   subtype rulv_tbl_type is okl_rule_pub.rulv_tbl_type;
82 
83 ----------------------------------------------------------------------------
84 -- Private Procedures and Functions
85 ----------------------------------------------------------------------------
86   -- Debug messages:
87   PROCEDURE msg (text VARCHAR2)
88   IS
89   BEGIN
90     OKL_API.Set_Message(G_APP_NAME,'FND_GENERIC_MESSAGE','MESSAGE', text);
91   END;
92 
93 ----------------------------------------------------------------------------
94 /*
95 -- vthiruva, 08/31/2004
96 -- START, Added PROCEDURE to enable Business Event
97 */
98 -- Start of comments
99 --
100 -- Procedure Name  : raise_business_event
101 -- Description     : local_procedure, raises business event by making a call to
102 --                   okl_wf_pvt.raise_event
103 -- Business Rules  :
104 -- Parameters      :
105 -- Version         : 1.0
106 -- End of comments
107 --
108 PROCEDURE raise_business_event(
109                 p_api_version       IN NUMBER,
110                 p_init_msg_list     IN VARCHAR2,
111                 x_return_status     OUT NOCOPY VARCHAR2,
112                 x_msg_count         OUT NOCOPY NUMBER,
113                 x_msg_data          OUT NOCOPY VARCHAR2,
114                 p_khr_id            IN okc_k_headers_b.id%TYPE,
115                 p_id                IN OKL_TRX_AP_INVOICES_B.id%TYPE,
116                 p_event_name        IN wf_events.name%TYPE) IS
117 
118 l_parameter_list        wf_parameter_list_t;
119 BEGIN
120     wf_event.AddParameterToList(G_WF_ITM_CONTRACT_ID,p_khr_id,l_parameter_list);
121     wf_event.AddParameterToList(G_WF_ITM_FUN_REQ_ID,p_id,l_parameter_list);
122 
123 
124     OKL_WF_PVT.raise_event (p_api_version    => p_api_version,
125 			    p_init_msg_list  => p_init_msg_list,
126 			    x_return_status  => x_return_status,
127 			    x_msg_count      => x_msg_count,
128 			    x_msg_data       => x_msg_data,
129 			    p_event_name     => p_event_name,
130 			    p_parameters     => l_parameter_list);
131 
132     EXCEPTION
133       WHEN OTHERS THEN
134         x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
135         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
136 END raise_business_event;
137 
138 /*
139 -- vthiruva, 08/31/2004
140 -- END, PROCEDURE to enable Business Event
141 */
142 
143 ----------------------------------------------------------------------------------
144 
145   PROCEDURE create_fund_asset_subsidies
146                              (p_api_version    IN  NUMBER,
147                               p_init_msg_list  IN  VARCHAR2 DEFAULT OKL_API.G_FALSE,
148                               x_return_status  OUT NOCOPY VARCHAR2,
149                               x_msg_count      OUT NOCOPY NUMBER,
150                               x_msg_data       OUT NOCOPY VARCHAR2,
151                               p_status         IN  OKL_TRX_AP_INVOICES_B.trx_status_code%TYPE,
152                               p_fund_id        IN  OKL_TRX_AP_INVOICES_B.ID%TYPE)
153   IS
154 
155   l_api_name         CONSTANT VARCHAR2(30) := 'create_fund_asset_subsidies';
156   l_api_version      CONSTANT NUMBER       := 1.0;
157   i                  NUMBER;
158   j                  NUMBER;
159   l_return_status    VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
160   l_row_found        boolean := false;
161 
162   l_tapv_rec        tapv_rec_type;
163   x_tapv_rec        tapv_rec_type;
164   l_tplv_rec        tplv_rec_type;
165   x_tplv_rec        tplv_rec_type;
166 
167   -- smadhava - Bug#5200033 - Added - Start
168   l_subsidy_line_count NUMBER DEFAULT 0;
169   l_tplv_tbl  tplv_tbl_type;
170   -- smadhava - Bug#5200033 - Added - End
171 
172 -- required to create funding request for subsidy
173   -- sjalasut, modified the below cursor to have khr_id referred to okl_txl_ap_inv_lns_all_b
174   -- instead of okl_trx_ap_invoices_b. changes made as part of OKLR12B disbursements
175   -- project
176   CURSOR c_khr (p_contract_id  NUMBER)
177   IS
178   select khr.ID KHR_ID,
179          khr.AUTHORING_ORG_ID ORG_ID,
180          khr.CURRENCY_CODE,
181          tap.ID TAP_ID,
182          tap.VENDOR_INVOICE_NUMBER,
183          tap.IPVS_ID,
184          tap.PAYMENT_METHOD_CODE,
185          -SUM(NVL(OKL_FUNDING_PVT.get_partial_subsidy_amount(tpl.KLE_ID, tpl.AMOUNT),0)) SUBSIDY_TOT_AMT
186   from   okc_k_headers_b khr,
187          okl_trx_ap_invoices_b tap,
188          okl_txl_ap_inv_lns_all_b  tpl
189   where  khr.id = tpl.khr_id
190   and    tap.id = tpl.tap_id
191   and    tap.id = p_fund_id
192   and    tap.funding_type_code = 'ASSET'
193   and    OKL_FUNDING_PVT.get_partial_subsidy_amount(tpl.kle_id, tpl.amount) > 0
194   group by khr.ID,
195            khr.AUTHORING_ORG_ID,
196            khr.CURRENCY_CODE,
197            tap.VENDOR_INVOICE_NUMBER,
198            tap.ID,
199            tap.IPVS_ID,
200            tap.PAYMENT_METHOD_CODE
201   ;
202 
203 -- assets request
204 cursor c_fund_asset(p_fund_id OKL_TRX_AP_INVOICES_B.ID%TYPE) is
205   select ast.KLE_ID,
206          ast.AMOUNT
207 from OKL_TXL_AP_INV_LNS_B ast
208 where ast.TAP_ID = p_fund_id;
209 
210 
211     r_khr c_khr%ROWTYPE;
212     x_asbv_tbl OKL_SUBSIDY_PROCESS_PVT.asbv_tbl_type;
213 
214 begin
215   -- Set API savepoint
216   SAVEPOINT create_fund_asset_subsidies;
217 
218   -- Check for call compatibility
219   IF (NOT FND_API.Compatible_API_Call (l_api_version,
220                                 	   p_api_version,
221                                 	   l_api_name,
222                                 	   G_PKG_NAME ))
223   THEN
224     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
225   END IF;
226 
227   -- Initialize message list if requested
228   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
229       FND_MSG_PUB.initialize;
230 	END IF;
231 
232   -- Initialize API status to success
233   x_return_status := OKL_API.G_RET_STS_SUCCESS;
234 
235 
236 --*** Begin API body ****************************************************
237 -------------------------------------------------------------------------
238 -- 1. create funding asset subsidy internal AP header
239 -------------------------------------------------------------------------
240   -- Get the internal invoice Details
241   OPEN  c_khr(p_fund_id);
242   FETCH c_khr INTO r_khr;
243   l_row_found := c_khr%FOUND;
244   CLOSE c_khr;
245 
246   -- if subsidy found
247   IF (l_row_found) THEN
248 
249     -- sjalasut, not commenting the khr_id reference in l_tapv_rec here as this
250     -- record variable is used as a parameter for validate_header_attributes,
251     -- validate_funding_request etc. since per the disbursements FDD, tapv_rec
252     -- .khr_id would continue to exist, not making this change would not cause
253     -- compilation issues.
254     -- the actual change of populating the khr_id at tplv_rec level is taken
255     -- care in the procedure create_funding_header and create_fund_asset_subsidies
256     l_tapv_rec.KHR_ID := r_khr.khr_id;
257     l_tapv_rec.AMOUNT := r_khr.subsidy_tot_amt;
258     l_tapv_rec.FUNDING_TYPE_CODE := G_ASSET_SUBSIDY;
259     l_tapv_rec.IPVS_ID := r_khr.ipvs_id;
260     l_tapv_rec.ORG_ID := r_khr.org_id;
261 
262     l_tapv_rec.TRX_STATUS_CODE := G_APPROVED;
263     l_tapv_rec.DATE_FUNDING_APPROVED := SYSDATE;
264 --  l_tapv_rec.VENDOR_INVOICE_NUMBER := TRIM(SUBSTR(r_khr.vendor_invoice_number,1,20)) || '''' || 'subsidies';
265 
266     l_tapv_rec.CURRENCY_CODE := r_khr.currency_code;
267 
268     l_tapv_rec.PAYMENT_METHOD_CODE := r_khr.PAYMENT_METHOD_CODE;
269 
270     l_tapv_rec.DATE_ENTERED := sysdate;
271     l_tapv_rec.DATE_INVOICED := sysdate;
272     l_tapv_rec.DATE_GL := sysdate;
273     l_tapv_rec.ASSET_TAP_ID := r_khr.tap_id;
274 --      l_tapv_rec.VENDOR_ID := r_khr.vendor_id;
275 
276     -- sjalasut, modified invoice_type from G_STANDARD to G_CREDIT
277     -- changes made as part of OKLR12B disbursements project
278     l_tapv_rec.INVOICE_TYPE := G_CREDIT;
279 
280     create_funding_header(
281       p_api_version   => p_api_version,
282       p_init_msg_list => p_init_msg_list,
283       x_return_status => x_return_status,
284       x_msg_count     => x_msg_count,
285       x_msg_data      => x_msg_data,
286       p_tapv_rec      => l_tapv_rec,
287       x_tapv_rec      => x_tapv_rec);
288 
289     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
290       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
291     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
292       RAISE OKL_API.G_EXCEPTION_ERROR;
293     END IF;
294 -----------------------------------------------------------------------
295 -- 2. get assets by funding request id
296 -----------------------------------------------------------------------
297     FOR r_ast IN c_fund_asset(p_fund_id) LOOP
298 
299       j := 1;
300       -------------------------------------------------------------------
301       -- get subsidies by asset and request amount
302       -------------------------------------------------------------------
303       OKL_SUBSIDY_PROCESS_PVT.get_partial_subsidy_amount(
304         p_api_version     => l_api_version,
305         p_init_msg_list   => p_init_msg_list,
306         x_return_status   => x_return_status,
307         x_msg_count       => x_msg_count,
308         x_msg_data        => x_msg_data,
309         p_asset_cle_id    => r_ast.kle_id,
310         p_req_fund_amount => r_ast.amount,
311         x_asbv_tbl        => x_asbv_tbl
312       );
313 
314       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
315         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
316       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
317         RAISE OKL_API.G_EXCEPTION_ERROR;
318       END IF;
319       -------------------------------------------------------------------
320       -- if subsidies found
321       -------------------------------------------------------------------
322       IF (x_asbv_tbl.COUNT > 0) THEN
323         i := x_asbv_tbl.FIRST;
324 
325         LOOP
326 
327           -------------------------------------------------------------------
328           -- create internal AP line for each subsidy
329           -------------------------------------------------------------------
330           l_tplv_rec.inv_distr_line_code := 'ITEM';
331           l_tplv_rec.tap_id := x_tapv_rec.id;
332 
333           -- sjalasut, added khr_id to the line. changes made as part of
334           -- OKLR12B disbursements project
335           l_tplv_rec.khr_id := r_khr.khr_id;
336 
337           l_tplv_rec.kle_id := r_ast.kle_id; -- asset_id, not subsidy_id
338 
339           -- smadhava - Bug#5200033 - Added - Start
340           -- Round the amount to the System Options setup
341           l_tplv_rec.amount := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => x_asbv_tbl(i).amount
342                                                               , p_currency_code => l_tapv_rec.CURRENCY_CODE);
343           l_tplv_rec.amount := -l_tplv_rec.amount;
344 --          l_tplv_rec.amount := -x_asbv_tbl(i).amount;
345           -- smadhava - Bug#5200033 - Added - End
346 
347           l_tplv_rec.org_id := r_khr.org_id;
348           l_tplv_rec.line_number := j;
349           l_tplv_rec.DISBURSEMENT_BASIS_CODE := 'BILL_DATE';
350 
351           l_tplv_rec.sty_id := x_asbv_tbl(i).stream_type_id;
352 
353           OKL_TXL_AP_INV_LNS_PUB.INSERT_TXL_AP_INV_LNS(
354             p_api_version   => p_api_version,
355             p_init_msg_list => p_init_msg_list,
356             x_return_status => x_return_status,
357             x_msg_count     => x_msg_count,
358             x_msg_data      => x_msg_data,
359             p_tplv_rec      => l_tplv_rec,
360             x_tplv_rec      => x_tplv_rec);
361 
362           IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
363             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
364           ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
365             RAISE OKL_API.G_EXCEPTION_ERROR;
366           END IF;
367 
368           -- smadhava - Bug#5200033 - Added - Start
369           -- Create a table of records of the AP subsidy lines to synchronize the header amount
370           l_tplv_tbl(l_subsidy_line_count) := x_tplv_rec;
371           l_subsidy_line_count := l_subsidy_line_count + 1;
372           -- smadhava - Bug#5200033 - Added - End
373 
374 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
375 /*
376           -------------------------------------------------------------------
377           -- create internal Accounting entry for each subsidy
378           -------------------------------------------------------------------
379           OKL_FUNDING_PVT.CREATE_ACCOUNTING_DIST(
380             p_api_version   => p_api_version,
381             p_init_msg_list => p_init_msg_list,
382             x_return_status => x_return_status,
383             x_msg_count     => x_msg_count,
384             x_msg_data      => x_msg_data,
385             p_status        => l_tapv_rec.trx_status_code,
386             p_fund_id       => x_tapv_rec.id,
387             p_fund_line_id  => x_tplv_rec.id,
388             p_subsidy_amt   => l_tplv_rec.amount,
389             p_sty_id        => l_tplv_rec.sty_id);
390 
391           IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
392             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
393           ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
394             RAISE OKL_API.G_EXCEPTION_ERROR;
395           END IF;
396 */
397 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
398 
399           EXIT WHEN (i = x_asbv_tbl.LAST);
400           i := x_asbv_tbl.NEXT(i);
401 
402         END LOOP; -- for each subsidy
403       END IF;
404       j := j+1;
405 
406     END LOOP; -- for each asset
407 
408 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
409     -------------------------------------------------------------------
410     -- create internal Accounting for a funding header
411     -------------------------------------------------------------------
412     OKL_FUNDING_PVT.CREATE_ACCOUNTING_DIST(
413             p_api_version   => p_api_version,
414             p_init_msg_list => p_init_msg_list,
415             x_return_status => x_return_status,
416             x_msg_count     => x_msg_count,
417             x_msg_data      => x_msg_data,
418             p_status        => l_tapv_rec.trx_status_code,
419             p_fund_id       => x_tapv_rec.id);
420 
421     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
422       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
423     ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
424       RAISE OKL_API.G_EXCEPTION_ERROR;
425     END IF;
426 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
427 
428     -- smadhava - Bug#5200033 - Added - Start
429     -- Synchronize the header record amount as the line record amounts have been
430     -- rounded to precision of the currency
431     SYNC_HEADER_AMOUNT(
432       p_api_version   => p_api_version,
433       p_init_msg_list => p_init_msg_list,
434       x_return_status => x_return_status,
435       x_msg_count     => x_msg_count,
436       x_msg_data      => x_msg_data,
437       p_tplv_tbl      => l_tplv_tbl);
438       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
439          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
440       ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
441          RAISE G_EXCEPTION_HALT_VALIDATION;
442       END IF;
443     -- smadhava - Bug#5200033 - Added - End
444 
445 
446   END IF; -- if subsidy found
447 
448 --*** End API body ******************************************************
449 
450   -- Get message count and if count is 1, get message info
451 
452 	FND_MSG_PUB.Count_And_Get
453     (p_count          =>      x_msg_count,
454      p_data           =>      x_msg_data);
455 
456   EXCEPTION
457     WHEN G_EXCEPTION_HALT_VALIDATION THEN
458       x_return_status := OKL_API.G_RET_STS_ERROR;
459     WHEN OTHERS THEN
460       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
461       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
462                           p_msg_name      => G_UNEXPECTED_ERROR,
463                           p_token1        => G_SQLCODE_TOKEN,
464                           p_token1_value  => SQLCODE,
465                           p_token2        => G_SQLERRM_TOKEN,
466                           p_token2_value  => SQLERRM);
467 
468   END create_fund_asset_subsidies;
469 
470 ----------------------------------------------------------------------------------
471 -- Start of comments
472 --
473 -- Procedure Name  : create_funding_chklst_tpl
474 -- Description     : wrapper api for create funding checklist FK associated
475 --                   with credit line contract ID
476 -- Business Rules  :
477 -- Parameters      :
478 -- Version         : 1.0
479 -- End of comments
480 ----------------------------------------------------------------------------------
481  PROCEDURE create_funding_chklst_tpl(
482     p_api_version                  IN NUMBER
483    ,p_init_msg_list                IN VARCHAR2
484    ,x_return_status                OUT NOCOPY VARCHAR2
485    ,x_msg_count                    OUT NOCOPY NUMBER
486    ,x_msg_data                     OUT NOCOPY VARCHAR2
487    ,p_chr_id                       IN  okc_k_headers_b.id%type
488    ,p_fund_req_id                  IN  okl_trx_ap_invoices_b.id%type
489    ,p_creditline_id                IN  okc_k_headers_b.id%type DEFAULT NULL
490  )
491 is
492   l_api_name         CONSTANT VARCHAR2(30) := 'create_funding_chklst_tpl';
493   l_api_version      CONSTANT NUMBER       := 1.0;
494   i                  NUMBER;
495   l_return_status    VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
496   lp_rgpv_rec        rgpv_rec_type;
497   lp_rulv_rec        rulv_rec_type;
498   lx_rgpv_rec        rgpv_rec_type;
499   lx_rulv_rec       rulv_rec_type;
500   l_crd_row_not_found   boolean;
501   l_grp_row_not_found   boolean;
502   l_expired_row_found   boolean;
503 
504   l_dummy number;
505   l_rgpv_id okc_rule_groups_b.id%type;
506   l_todo_item_code   okl_crd_fund_checklists_tpl_uv.TODO_ITEM_CODE%type;
507   l_mandatory_flag   okl_crd_fund_checklists_tpl_uv.mandatory_flag%type;
508   l_note             okl_crd_fund_checklists_tpl_uv.note%type;
509   l_status           okl_crd_fund_checklists_tpl_uv.status%type;
510   l_fund_cls_tpl_exp_found   boolean;
511   l_chklist_sts_row_found   boolean;
512 
513 
514   l_template_row_not_found  boolean;
515 
516   l_credit_id okc_k_headers_b.id%TYPE;
517 
518     l_funding_checklist_tpl okc_rules_b.rule_information2%TYPE;
519     l_checklists_row_found boolean;
520 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
521   l_function_id           okl_crd_fund_checklists_tpl_uv.function_id%type;
522   l_checklist_type        okl_crd_fund_checklists_tpl_uv.checklist_type%type;
523   l_lease_app_id number;
524   l_lease_app_found boolean;
525   l_lease_app_list_found boolean;
526 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
527 --------------------------------------------------------------------------------------------
528 -- Checklists link check
529 --------------------------------------------------------------------------------------------
530 CURSOR c_checklists (p_credit_id  NUMBER)
531   IS
532   select rule.rule_information2
533   from okc_rules_b rule
534   where rule.dnz_chr_id = p_credit_id
535   and   rule.rule_information_category = G_CREDIT_CHKLST_TPL_RULE1
536   ;
537 
538 ---------------------------------------------------------------------------------------------------
539 -- Funded contract group
540 ---------------------------------------------------------------------------------------------------
541 cursor c_grp (p_chr_id number) is
542   select rgp.id
543 from okc_rule_groups_b rgp
544 where rgp.dnz_chr_id = p_chr_id
545 and rgp.RGD_CODE = G_FUNDING_CHKLST_TPL
546 ;
547 
548 ---------------------------------------------------------------------------------------------------
549 -- funding request checklist template at credit line level
550 ---------------------------------------------------------------------------------------------------
551 cursor c_chk_tpl2 (p_credit_id number) is
552 select
553   tpl.TODO_ITEM_CODE,
554   nvl(tpl.MANDATORY_FLAG, 'N'),
555   tpl.NOTE,
556 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
557 -- commented out tpl.STATUS
558   tpl.FUNCTION_ID,
559   tpl.CHECKLIST_TYPE
560 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
561 from okl_crd_fund_checklists_tpl_uv tpl
562 where tpl.khr_id = p_credit_id
563 ;
564 
565 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
566 ---------------------------------------------------------------------------------------------------------
567 -- check if the contract was created from a lease application
568 ---------------------------------------------------------------------------------------------------------
569 CURSOR c_lease_app (p_chr_id okc_k_headers_b.id%type)
570 IS
571   select chr.ORIG_SYSTEM_ID1
572 from  okc_k_headers_b chr
573 where ORIG_SYSTEM_SOURCE_CODE = G_OKL_LEASE_APP
574 and   chr.id = p_chr_id
575 ;
576 
577 ---------------------------------------------------------------------------------------------------
578 -- funding request checklist refer from a Lease application
579 ---------------------------------------------------------------------------------------------------
580 cursor c_chk_lease_app (p_lease_app_id number) is
581 select
582   chk.TODO_ITEM_CODE,
583   NVL(chk.MANDATORY_FLAG, 'N') MANDATORY_FLAG,
584   chk.USER_NOTE,
585   chk.FUNCTION_ID,
586   chk.INST_CHECKLIST_TYPE
587 from OKL_CHECKLIST_DETAILS chk
588 --START:| 21-Dec-2005  cklee -- Fixed bug#4880288 -- 4908242
589      ,okl_checklists hdr
590 where chk.ckl_id = hdr.id
591 and hdr.CHECKLIST_OBJ_ID = p_lease_app_id
592 --where chk.DNZ_CHECKLIST_OBJ_ID = p_lease_app_id
593 --END:| 21-Dec-2005  cklee -- Fixed bug#4880288 -- 4908242
594 and chk.INST_CHECKLIST_TYPE = 'FUNDING'
595 ;
596 
597 cursor c_lease_app_list_exists (p_lease_app_id number) is
598 select 1
599 from OKL_CHECKLIST_DETAILS chk
600 --START:| 21-Dec-2005  cklee -- Fixed bug#4880288 -- 4908242
601      ,okl_checklists hdr
602 where chk.ckl_id = hdr.id
603 and hdr.CHECKLIST_OBJ_ID = p_lease_app_id
604 --where chk.DNZ_CHECKLIST_OBJ_ID = p_lease_app_id
605 --END:| 21-Dec-2005  cklee -- Fixed bug#4880288 -- 4908242
606 and chk.INST_CHECKLIST_TYPE = 'FUNDING'
607 ;
608 
609 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
610 
611 begin
612   -- Set API savepoint
613   SAVEPOINT create_funding_chklst_tpl;
614 
615   -- Check for call compatibility
616   IF (NOT FND_API.Compatible_API_Call (l_api_version,
617                                 	   p_api_version,
618 
619                                 	   l_api_name,
620                                 	   G_PKG_NAME ))
621   THEN
622     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
623   END IF;
624 
625   -- Initialize message list if requested
626   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
627       FND_MSG_PUB.initialize;
628 	END IF;
629 
630   -- Initialize API status to success
631   x_return_status := OKL_API.G_RET_STS_SUCCESS;
632 
633 
634 /*** Begin API body ****************************************************/
635 
636 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
637   OPEN c_lease_app(p_chr_id);
638   FETCH c_lease_app INTO l_lease_app_id;
639   l_lease_app_found := c_lease_app%FOUND;
640   CLOSE c_lease_app;
641 
642 
643   IF l_lease_app_id IS NOT NULL THEN
644 
645     OPEN c_lease_app_list_exists(l_lease_app_id);
646     FETCH c_lease_app_list_exists INTO l_dummy;
647     l_lease_app_list_found := c_lease_app_list_exists%FOUND;
648     CLOSE c_lease_app_list_exists;
649 
650   END IF;
651 
652   IF NOT l_lease_app_found THEN
653 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
654 
655 -- get credit line id
656 -- fixed bug if contract credit line has been changed, but funding requests
657 -- still have not been apporved. We need to re-generate list from the
658 -- new credit line
659     IF (p_creditline_id IS NOT NULL) THEN
660       l_credit_id := p_creditline_id;
661     ELSE
662       l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_chr_id);
663     END IF;
664 
665 -- get source checklist template ID
666 
667     OPEN c_checklists(l_credit_id);
668     FETCH c_checklists INTO l_funding_checklist_tpl;
669     l_checklists_row_found := c_checklists%FOUND;
670     CLOSE c_checklists;
671 
672 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
673   END IF;
674 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
675 
676 /*
677 ---------------------------------------------------------------------------------------------------
678 1. create rule group
679 2. create rule1 : get the source of the checklist template lists
680 3. create rules based on #2. cursor
681 ---------------------------------------------------------------------------------------------------
682 */
683 ---------------------------------------------------------------------------------------------------
684 --1. create rule group
685 ---------------------------------------------------------------------------------------------------
686 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
687 --  IF (l_funding_checklist_tpl IS NOT NULL) THEN
688   IF ( (NOT l_lease_app_found  AND l_funding_checklist_tpl IS NOT NULL) or
689        (l_lease_app_found  AND l_lease_app_list_found)
690      ) THEN
691 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
692 
693 
694     open c_grp(p_chr_id);
695     fetch c_grp into l_rgpv_id;
696 
697     l_grp_row_not_found := c_grp%NOTFOUND;
698     close c_grp;
699 
700     IF (l_grp_row_not_found) THEN
701 
702       lp_rgpv_rec.DNZ_CHR_ID := p_chr_id;
703       lp_rgpv_rec.CHR_ID := p_chr_id;
704   --    lp_rgpv_rec.CLE_ID := p_fund_req_id;
705       lp_rgpv_rec.RGD_CODE := G_FUNDING_CHKLST_TPL;
706       lp_rgpv_rec.RGP_TYPE := G_RGP_TYPE;
707 
708       okl_rule_pub.create_rule_group(
709         p_api_version    => p_api_version,
710         p_init_msg_list  => p_init_msg_list,
711         x_return_status  => x_return_status,
712         x_msg_count      => x_msg_count,
713         x_msg_data       => x_msg_data,
714         p_rgpv_rec       => lp_rgpv_rec,
715         x_rgpv_rec       => lx_rgpv_rec);
716 
717       If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
718          raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
719       Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
720          raise OKC_API.G_EXCEPTION_ERROR;
721       End If;
722 
723       l_rgpv_id := lx_rgpv_rec.id;
724     END IF;
725 ---------------------------------------------------------------------------------------------------
726 --2. create rule1 : get the source of the checklist template lists
727 --3. create rules based on #2. cursor
728 ---------------------------------------------------------------------------------------------------
729 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
730     IF NOT l_lease_app_found  AND l_funding_checklist_tpl IS NOT NULL THEN
731 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
732 
733 
734       OPEN c_chk_tpl2(l_credit_id);
735       LOOP
736 
737         FETCH c_chk_tpl2 INTO l_todo_item_code,
738                               l_mandatory_flag,
739                               l_note,
740 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
741 --                              l_status;
742                               l_function_id,
743                               l_checklist_type;
744 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
745 
746         EXIT WHEN c_chk_tpl2%NOTFOUND;
747 
748         lp_rulv_rec.OBJECT1_ID1 := p_fund_req_id;
749         lp_rulv_rec.OBJECT1_ID2 := '#';
750         lp_rulv_rec.RGP_ID := l_rgpv_id;
751         lp_rulv_rec.DNZ_CHR_ID := p_chr_id;
752         lp_rulv_rec.RULE_INFORMATION_CATEGORY := G_FUNDING_CHKLST_TPL_RULE1;
753         lp_rulv_rec.STD_TEMPLATE_YN := 'N';
754         lp_rulv_rec.WARN_YN := 'N';
755         lp_rulv_rec.RULE_INFORMATION1 := l_todo_item_code;
756         lp_rulv_rec.RULE_INFORMATION2 := l_mandatory_flag;
757         lp_rulv_rec.RULE_INFORMATION3 := 'N';
758         lp_rulv_rec.RULE_INFORMATION4 := l_note;
759 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
760         lp_rulv_rec.RULE_INFORMATION5 := 'NEW'; -- set default checklist status
761         lp_rulv_rec.RULE_INFORMATION9 := l_function_id;
762         lp_rulv_rec.RULE_INFORMATION10 := l_checklist_type;
763 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
764 
765         okl_rule_pub.create_rule(
766           p_api_version    => p_api_version,
767           p_init_msg_list  => p_init_msg_list,
768           x_return_status  => x_return_status,
769           x_msg_count      => x_msg_count,
770           x_msg_data       => x_msg_data,
771           p_rulv_rec       => lp_rulv_rec,
772           x_rulv_rec       => lx_rulv_rec);
773 
774         If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
775            raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
776         Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
777            raise OKC_API.G_EXCEPTION_ERROR;
778         End If;
779 
780       END LOOP;
781       CLOSE c_chk_tpl2;
782 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
783     ELSIF l_lease_app_found  AND l_lease_app_list_found THEN
784 
785       FOR r_this_row IN c_chk_lease_app(l_lease_app_id) LOOP
786 
787         lp_rulv_rec.OBJECT1_ID1 := p_fund_req_id;
788         lp_rulv_rec.OBJECT1_ID2 := '#';
789         lp_rulv_rec.RGP_ID := l_rgpv_id;
790         lp_rulv_rec.DNZ_CHR_ID := p_chr_id;
791         lp_rulv_rec.RULE_INFORMATION_CATEGORY := G_FUNDING_CHKLST_TPL_RULE1;
792         lp_rulv_rec.STD_TEMPLATE_YN := 'N';
793         lp_rulv_rec.WARN_YN := 'N';
794         lp_rulv_rec.RULE_INFORMATION1 := r_this_row.todo_item_code;
795         lp_rulv_rec.RULE_INFORMATION2 := r_this_row.mandatory_flag;
796         lp_rulv_rec.RULE_INFORMATION3 := 'N';
797         lp_rulv_rec.RULE_INFORMATION4 := r_this_row.user_note;
798 -- automatically activate this checklist if the checklist was copy from a lease application's checklist
799         lp_rulv_rec.RULE_INFORMATION5 := 'ACTIVE';
800 
801         lp_rulv_rec.RULE_INFORMATION9 := r_this_row.function_id;
802         lp_rulv_rec.RULE_INFORMATION10 := r_this_row.inst_checklist_type;
803 
804         okl_rule_pub.create_rule(
805           p_api_version    => p_api_version,
806           p_init_msg_list  => p_init_msg_list,
807           x_return_status  => x_return_status,
808           x_msg_count      => x_msg_count,
809           x_msg_data       => x_msg_data,
810           p_rulv_rec       => lp_rulv_rec,
811           x_rulv_rec       => lx_rulv_rec);
812 
813         If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
814            raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
815         Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
816            raise OKC_API.G_EXCEPTION_ERROR;
817         End If;
818 
819       END LOOP;
820     END IF;
821 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
822 
823   END IF;
824 
825 
826 /*** End API body ******************************************************/
827 
828   -- Get message count and if count is 1, get message info
829 	FND_MSG_PUB.Count_And_Get
830     (p_count          =>      x_msg_count,
831      p_data           =>      x_msg_data);
832 
833   EXCEPTION
834     WHEN G_EXCEPTION_HALT_VALIDATION THEN
835       x_return_status := OKL_API.G_RET_STS_ERROR;
836     WHEN OTHERS THEN
837       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
838       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
839                           p_msg_name      => G_UNEXPECTED_ERROR,
840                           p_token1        => G_SQLCODE_TOKEN,
841                           p_token1_value  => SQLCODE,
842                           p_token2        => G_SQLERRM_TOKEN,
843                           p_token2_value  => SQLERRM);
844 
845 end create_funding_chklst_tpl;
846 
847 ----------------------------------------------------------------------------------
848 -- Start of comments
849 --
850 -- Procedure Name  : refresh_fund_chklst
851 -- Description     : Refresh funding checklist if user have changed credit line
852 --                   for associated contract
853 -- Business Rules  :
854 -- Parameters      :
855 -- Version         : 1.0
856 -- End of comments
857 ----------------------------------------------------------------------------------
858  PROCEDURE refresh_fund_chklst(
859     p_api_version                  IN NUMBER
860    ,p_init_msg_list                IN VARCHAR2
861    ,x_return_status                OUT NOCOPY VARCHAR2
862    ,x_msg_count                    OUT NOCOPY NUMBER
863    ,x_msg_data                     OUT NOCOPY VARCHAR2
864    ,p_chr_id                       IN  okc_k_headers_b.id%type
865    ,p_MLA_id                       IN  okc_k_headers_b.id%type
866    ,p_creditline_id            IN  okc_k_headers_b.id%type
867  )
868 IS
869   l_api_name         CONSTANT VARCHAR2(30) := 'refresh_fund_chklst';
870   l_api_version      CONSTANT NUMBER       := 1.0;
871   i                  NUMBER;
872   l_return_status    VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
873   l_old_credit_id    okc_k_headers_b.id%TYPE;
874   l_funded_amout     NUMBER := 0;
875   l_fund_req_id      okl_trx_ap_invoices_b.id%type;
876 
877   l_refresh_flag     boolean := false;
878   l_rule_id          okc_rules_b.id%type;
879   ldel_rulv_rec      rulv_rec_type;
880   l_final_creditline_id okc_k_headers_b.id%type;
881   l_MLA_creditline_id okc_k_headers_b.id%type;
882 --START: CKLEE 01/04/06
883 --  l_MLA_creditline_not_found boolean := false;
884   l_MLA_creditline_found boolean := false;
885 --END: CKLEE 01/04/06
886 
887   l_dummy              number;
888   l_approved_req_not_found boolean := false;
889 
890 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
891   l_lease_app_found boolean;
892 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
893 
894 CURSOR c_MLA_credit (p_MLA_id NUMBER)
895   IS
896 select a.ID
897 from   OKC_K_HEADERS_B a,
898        okc_Governances_v g
899 where  a.id = g.chr_id_referred
900 and    a.sts_code = 'ACTIVE'
901 and    a.scs_code = 'CREDITLINE_CONTRACT'
902 and    g.dnz_chr_id = p_MLA_id
903 ;
904 
905 --4. Check if associated funding requests exist
906 -- sjalasut, modified the cursor to have khr_id referred from okl_txl_ap_inv_lns_all_b
907 -- instead of okl_trx_ap_invoices_b. changes made as part of OKLR12B disburesments
908 -- project.
909 cursor c_fund_req(p_chr_id NUMBER) is
910 select fr.id
911 from OKL_TRX_AP_INVOICES_B fr
912     ,okl_txl_ap_inv_lns_all_b b
913 where fr.id = b.tap_id
914 and   b.khr_id = p_chr_id
915 and   fr.trx_status_code = 'ENTERED'
916 ;
917 
918 --5. Delete all associated list by chr_id if any
919 cursor c_fund_req_list(p_chr_id NUMBER) is
920 --start modified abhsaxen for performance SQLID 20562295
921 SELECT rult.ID
922 FROM okc_rules_b rult
923 WHERE rult.rule_information_category = 'LAFCLD'
924 and rult.dnz_chr_id = p_chr_id
925 ;
926 --end modified abhsaxen for performance SQLID 20562295
927 
928 
929 --6. Check if associated approved funding requests exist
930 -- sjalasut, modified the cursor to have khr_id referred from okl_txl_ap_inv_lns_all_b
931 -- instead of okl_trx_ap_invoices_b. changes made as part of OKLR12B disburesments
932 -- project.
933 cursor c_approved_req(p_chr_id NUMBER) is
934 select 1
935 from OKL_TRX_AP_INVOICES_B fr
936     ,okl_txl_ap_inv_lns_all_b b
937 where fr.id = b.tap_id
938   and b.khr_id = p_chr_id
939   and fr.trx_status_code in ('APPROVED','PROCESSED')
940 ;
941 
942 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
943 ---------------------------------------------------------------------------------------------------------
944 -- 5. check if the contract was created from a lease application
945 ---------------------------------------------------------------------------------------------------------
946 CURSOR c_lease_app (p_chr_id okc_k_headers_b.id%type)
947 IS
948 --start modified abhsaxen for Performance SQLID 20562299
949 select 1
950 from  okc_k_headers_b chr
951 where ORIG_SYSTEM_SOURCE_CODE = G_OKL_LEASE_APP
952 and chr.id = p_chr_id
953 ;
954 --end modified abhsaxen for Performance SQLID 20562299
955 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
956 
957 BEGIN
958   -- Set API savepoint
959   SAVEPOINT refresh_fund_chklst;
960 
961   -- Check for call compatibility
962   IF (NOT FND_API.Compatible_API_Call (l_api_version,
963                                 	   p_api_version,
964 
965                                 	   l_api_name,
966                                 	   G_PKG_NAME ))
967   THEN
968     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
969   END IF;
970 
971   -- Initialize message list if requested
972   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
973       FND_MSG_PUB.initialize;
974 	END IF;
975 
976   -- Initialize API status to success
977   x_return_status := OKL_API.G_RET_STS_SUCCESS;
978 
979 
980 /*** Begin API body ****************************************************/
981 
982 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
983   OPEN c_lease_app(p_chr_id);
984   FETCH c_lease_app INTO l_dummy;
985   l_lease_app_found := c_lease_app%FOUND;
986   CLOSE c_lease_app;
987 
988   IF NOT l_lease_app_found THEN
989 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
990 
991 /*
992 ------------------------------------------------------------------------
993 0.1) find out if credit line has been changed
994 ..
995 1. Passed in p_chr_id and p_creditline_id can not be NULL
996 2. Check if creditline is changed
997 3. Check if associated funding requests have not been approved
998 
999 4. Check if associated funding requests exists
1000 5. Delete all associated list by chr_id
1001 6. Loop for funding requests
1002      call create_funding_chklst_tpl(p_chr_id, p_fund_req_id,p_creditline_id);
1003    end loop;
1004 ------------------------------------------------------------------------
1005 */
1006 
1007     IF (p_chr_id IS NOT NULL AND p_chr_id <> OKL_API.G_MISS_NUM) THEN
1008 
1009       -----------------------------------------------------------------
1010       -- get credit line id
1011       -----------------------------------------------------------------
1012       -- get old credit line id if exists
1013       l_old_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_chr_id);
1014 
1015       -- 1st to get new credit line id if there is any
1016       -- 1. old list <> new list
1017       -- 2. No old list, create a new list
1018       IF ((p_creditline_id IS NOT NULL AND
1019            p_creditline_id <> OKL_API.G_MISS_NUM AND
1020            l_old_credit_id IS NOT NULL AND
1021            l_old_credit_id <> p_creditline_id)
1022           OR
1023           (l_old_credit_id IS NULL AND
1024            p_creditline_id IS NOT NULL AND
1025            p_creditline_id <> OKL_API.G_MISS_NUM)
1026          ) THEN
1027 
1028         l_refresh_flag := true;
1029         l_final_creditline_id := p_creditline_id;
1030 
1031       END IF;
1032 
1033       IF ( p_creditline_id IS NULL OR
1034            p_creditline_id = OKL_API.G_MISS_NUM) THEN
1035 
1036         --------------------------------------------------------------------------------
1037         -- 1. p_MLA_id could be NULL
1038         -- 2. MLA doesn't have credit line attached
1039         -- 3. MLA has credit line attached, but no list associated with that credit line
1040         ---------------------------------------------------------------------------------
1041         OPEN c_MLA_credit(p_MLA_id);
1042         FETCH c_MLA_credit INTO l_MLA_creditline_id;
1043 --START: CKLEE 01/04/06
1044 --        l_MLA_creditline_not_found := c_MLA_credit%NOTFOUND;
1045         l_MLA_creditline_found := c_MLA_credit%FOUND;
1046 --END: CKLEE 01/04/06
1047         close c_MLA_credit;
1048 
1049 --START: CKLEE 01/04/06
1050 --        IF (l_MLA_creditline_not_found OR
1051         IF (l_MLA_creditline_found OR
1052 --END: CKLEE 01/04/06
1053            (l_old_credit_id IS NULL AND
1054             l_MLA_creditline_id IS NOT NULL) OR
1055            (l_old_credit_id IS NOT NULL AND
1056             l_MLA_creditline_id IS NOT NULL AND
1057             l_old_credit_id <> l_MLA_creditline_id)
1058            ) THEN
1059 
1060           l_refresh_flag := true;
1061           l_final_creditline_id := l_MLA_creditline_id;
1062 
1063         END IF;
1064 
1065       END IF;
1066 
1067       IF (l_refresh_flag = true) THEN
1068 
1069         -----------------------------------------------------------------
1070         -- Delete associated checklists by chr_id if ANY
1071         -----------------------------------------------------------------
1072         -- no approved requests exists
1073         --3. Check if associated funding requests have not been approved
1074         OPEN c_approved_req(p_chr_id);
1075         FETCH c_approved_req INTO l_dummy;
1076         l_approved_req_not_found := c_approved_req%NOTFOUND;
1077         close c_approved_req;
1078 
1079         IF ( l_approved_req_not_found ) THEN
1080 
1081           open c_fund_req_list(p_chr_id);
1082           LOOP
1083 
1084             fetch c_fund_req_list into l_rule_id;
1085             EXIT WHEN c_fund_req_list%NOTFOUND;
1086 
1087             ldel_rulv_rec.ID := l_rule_id;
1088 
1089             okl_rule_pub.delete_rule(
1090               p_api_version    => p_api_version,
1091               p_init_msg_list  => p_init_msg_list,
1092               x_return_status  => x_return_status,
1093               x_msg_count      => x_msg_count,
1094               x_msg_data       => x_msg_data,
1095               p_rulv_rec       => ldel_rulv_rec);
1096 
1097             If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
1098               raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1099             Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
1100               raise OKC_API.G_EXCEPTION_ERROR;
1101 
1102             End If;
1103 
1104           END LOOP;
1105           close c_fund_req_list;
1106 
1107         END IF;
1108 
1109         -----------------------------------------------------------------
1110         -- Create associated checklists by chr_id, fund_req_id if ANY
1111         -----------------------------------------------------------------
1112         IF (l_final_creditline_id IS NOT NULL) THEN
1113 
1114           OPEN c_fund_req(p_chr_id);
1115           -- Funding requests
1116           LOOP
1117 
1118             FETCH c_fund_req INTO l_fund_req_id;
1119             EXIT WHEN c_fund_req%NOTFOUND;
1120 
1121             create_funding_chklst_tpl(
1122               p_api_version       => p_api_version,
1123               p_init_msg_list     => p_init_msg_list,
1124               x_return_status     => x_return_status,
1125               x_msg_count         => x_msg_count,
1126               x_msg_data          => x_msg_data,
1127               p_chr_id            => p_chr_id,
1128               p_fund_req_id       => l_fund_req_id,
1129               p_creditline_id     => l_final_creditline_id);
1130 
1131 
1132             IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1133               RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1134             ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
1135               RAISE OKL_API.G_EXCEPTION_ERROR;
1136             END IF;
1137 
1138           END LOOP;-- Funding requests
1139         CLOSE c_fund_req;
1140 
1141         END IF; -- IF (l_final_creditline_id IS NOT NULL) THEN
1142 
1143       END IF; -- IF (l_refresh_flag = true) THEN
1144 
1145     END IF; -- p_chr_id check
1146 
1147 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
1148   END IF; -- IF NOT l_lease_app_found THEN
1149 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
1150 
1151 /*** End API body ******************************************************/
1152 
1153   -- Get message count and if count is 1, get message info
1154 	FND_MSG_PUB.Count_And_Get
1155     (p_count          =>      x_msg_count,
1156      p_data           =>      x_msg_data);
1157 
1158 EXCEPTION
1159   WHEN OKL_API.G_EXCEPTION_ERROR THEN
1160     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
1161     x_return_status := OKL_API.G_RET_STS_ERROR;
1162     FND_MSG_PUB.Count_And_Get
1163       (p_count         =>      x_msg_count,
1164        p_data          =>      x_msg_data);
1165 
1166   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1167     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
1168     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
1169     FND_MSG_PUB.Count_And_Get
1170       (p_count         =>      x_msg_count,
1171        p_data          =>      x_msg_data);
1172 
1173   WHEN OTHERS THEN
1174     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
1175     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1176     OKL_API.Set_Message(p_app_name      => G_APP_NAME,
1177                         p_msg_name      => G_UNEXPECTED_ERROR,
1178                         p_token1        => G_SQLCODE_TOKEN,
1179                         p_token1_value  => SQLCODE,
1180                         p_token2        => G_SQLERRM_TOKEN,
1181                         p_token2_value  => SQLERRM);
1182     FND_MSG_PUB.Count_And_Get
1183       (p_count         =>      x_msg_count,
1184        p_data          =>      x_msg_data);
1185 
1186 END refresh_fund_chklst;
1187 
1188 --------------------------------------------------------------------------------
1189 ----------------------------------CREATE_ACCOUNTING_DIST------------------------
1190 --------------------------------------------------------------------------------
1191   PROCEDURE CREATE_ACCOUNTING_DIST(p_api_version      IN  NUMBER,
1192                               p_init_msg_list  IN  VARCHAR2,
1193                               x_return_status  OUT NOCOPY VARCHAR2,
1194                               x_msg_count      OUT NOCOPY NUMBER,
1195                               x_msg_data       OUT NOCOPY VARCHAR2,
1196                               p_status         IN  OKL_TRX_AP_INVOICES_B.trx_status_code%TYPE,
1197 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1198                               p_fund_id        IN  OKL_TRX_AP_INVOICES_B.ID%TYPE ) IS --,
1199 /*
1200 -- cklee 11.5.10 subsidy
1201                               p_fund_line_id   IN  OKL_TXL_AP_INV_LNS_B.ID%TYPE,
1202                               p_subsidy_amt    IN  NUMBER,
1203                               p_sty_id         IN  NUMBER) IS
1204 */
1205 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1206 
1207     l_api_name   CONSTANT VARCHAR2(30) := 'CREATE_ACCOUNTING_DIST';
1208     l_tapv_rec            tapv_rec_type;
1209     x_tapv_rec            tapv_rec_type;
1210     l_chr_id              NUMBER;
1211     l_funding_type_code   VARCHAR2(30);
1212     l_product_id          NUMBER;
1213     l_trans_id            NUMBER;
1214     l_stream_id           NUMBER;
1215     l_name                VARCHAR2(30);
1216     l_trx_name            VARCHAR2(30);
1217     l_amount              NUMBER;
1218     l_cust_trx_type_id    NUMBER;
1219     l_sales_id            NUMBER;
1220     l_site_uses_id        NUMBER;
1221 
1222     --Bug# 4622198
1223     l_scs_code            OKC_K_HEADERS_B.SCS_CODE%TYPE;
1224     l_fact_synd_code      FND_LOOKUPS.Lookup_code%TYPE;
1225     l_inv_acct_code       OKC_RULES_B.Rule_Information1%TYPE;
1226     --Bug# 4622198
1227 
1228     l_cust_acct_site_id   NUMBER;
1229 
1230     l_ipvs_id             NUMBER;
1231 
1232 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1233 --    l_tmpl_identify_rec  OKL_ACCOUNT_DIST_PVT.TMPL_IDENTIFY_REC_TYPE;
1234 --    l_dist_info_rec      OKL_ACCOUNT_DIST_PVT.dist_info_REC_TYPE;
1235 --    l_template_tbl       OKL_ACCOUNT_DIST_PVT.AVLV_TBL_TYPE;
1236 --    l_amount_tbl         OKL_ACCOUNT_DIST_PVT.AMOUNT_TBL_TYPE;
1237 
1238 --    l_ctxt_val_tbl       OKL_ACCOUNT_DIST_PVT.CTXT_VAL_TBL_TYPE;
1239 --    l_acc_gen_primary_key_tbl OKL_ACCOUNT_DIST_PVT.acc_gen_primary_key;
1240     l_has_trans          VARCHAR2(1);
1241     l_org_id             NUMBER;
1242     l_fund_line_id       NUMBER;
1243 
1244     l_tmpl_identify_tbl     Okl_Account_Dist_Pvt.TMPL_IDENTIFY_TBL_TYPE;
1245     l_dist_info_tbl         Okl_Account_Dist_Pvt.dist_info_TBL_TYPE;
1246     l_pdt_id               Okl_k_headers.pdt_id%type;
1247     l_ctxt_val_tbl          Okl_Account_Dist_Pvt.CTXT_TBL_TYPE;
1248     l_acc_gen_primary_key_tbl  Okl_Account_Dist_Pvt.ACC_GEN_TBL_TYPE;
1249     l_template_tbl             Okl_Account_Dist_Pvt.AVLV_OUT_TBL_TYPE;
1250     l_amount_tbl               Okl_Account_Dist_Pvt.AMOUNT_OUT_TBL_TYPE;
1251 --    l_fact_synd_code           fnd_lookups.lookup_code%TYPE;
1252 --    l_inv_acct_code            okc_rules_b.RULE_INFORMATION1%TYPE;
1253 
1254 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1255 
1256     -- sjalasut, modified the below cursor to have khr_id being selected
1257     -- from okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b
1258     -- changes made as part of OKLR12B disbursements project
1259     CURSOR c (p_fund_id  NUMBER)
1260     IS
1261     select b.khr_id,
1262            a.funding_type_code,
1263            a.ipvs_id,
1264 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1265            a.amount,
1266            a.date_invoiced,
1267 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1268            a.org_id
1269     from okl_trx_ap_invoices_b a
1270         ,okl_txl_ap_inv_lns_all_b b
1271     where a.id = b.tap_id
1272         and a.id = p_fund_id;
1273 
1274     CURSOR pdt_c (p_chr_id  NUMBER)
1275     IS
1276     SELECT pdt_id,
1277            --Bug# 4622198
1278            scs_code
1279     FROM  okl_k_headers_full_v chr
1280     where chr.id = p_chr_id
1281     ;
1282 
1283     CURSOR trans_csr(p_trx_name VARCHAR2)
1284     IS
1285     SELECT id
1286     FROM  okl_trx_types_tl
1287     WHERE  name = p_trx_name
1288     AND language = 'US'
1289     ;
1290 
1291     CURSOR stream_c(p_name VARCHAR2)
1292     IS
1293     SELECT id
1294     FROM  OKL_STRM_TYPE_V
1295     where name = p_name
1296     ;
1297 
1298 --
1299 --FINANCIAL_SYSTEM_PARAMETERS
1300 
1301 
1302 
1303 --select mo_global.get_current_org_id() into l_fnd_profile
1304 --    from dual;
1305 
1306 
1307 --JTF_RS_SALESREPS_MO_V
1308 
1309     Cursor salesP_csr( p_chr_id NUMBER) IS
1310     select ct.object1_id1
1311     from   okc_contacts        ct,
1312            okc_contact_sources csrc,
1313            okc_k_party_roles_b pty,
1314            okc_k_headers_b     chr
1315     where  ct.cpl_id               = pty.id
1316           and    ct.cro_code             = csrc.cro_code
1317           and    ct.jtot_object1_code    = csrc.jtot_object_code
1318           and    ct.dnz_chr_id           =  chr.id
1319           and    pty.rle_code            = csrc.rle_code
1320           and    csrc.cro_code           = 'SALESPERSON'
1321           and    csrc.rle_code           = 'LESSOR'
1322           and    csrc.buy_or_sell        = chr.buy_or_sell
1323           and    pty.dnz_chr_id          = chr.id
1324           and    pty.chr_id              = chr.id
1325           and    chr.id                  = p_chr_id;
1326 
1327 
1328 --RA_CUST_TRX_TYPES
1329 
1330     Cursor ra_cust_trx_types_csr( p_chr_id NUMBER) IS
1331     select cust_trx_type_id
1332     from ra_cust_trx_types
1333     where name = 'Invoice-OKL';
1334 
1335 
1336 --AR_SITE_USES_V
1337 /*
1338     Cursor ar_site_uses_csr( p_chr_id NUMBER) IS
1339  select object1_id1 cust_acct_site_id
1340     from okc_rules_b rul
1341     where  rul.rule_information_category = 'BTO'
1342          and exists (select '1'
1343                      from okc_rule_groups_b rgp
1344                      where rgp.id = rul.rgp_id
1345                           and   rgp.rgd_code = 'LABILL'
1346                           and   rgp.chr_id   = rul.dnz_chr_id
1347                           and   rgp.chr_id = p_chr_id );
1348 */
1349 -- OKC/OKS rule migration project 11.5.10
1350     Cursor ar_site_uses_csr( p_chr_id NUMBER) IS
1351  select bill_to_site_use_id cust_acct_site_id
1352     from okc_k_headers_b chr
1353     where chr.id = p_chr_id;
1354 
1355     -- get sty_id for manual_disb
1356     -- cklee 05/04/2004
1357     Cursor manu_disb( p_fund_id NUMBER) IS
1358  select id, sty_id
1359     from okl_txl_ap_inv_lns_b txl
1360     where txl.tap_id = p_fund_id;
1361 
1362      -- Multi Currency Compliance
1363     CURSOR l_curr_conv_csr( p_khr_id  NUMBER ) IS
1364         SELECT  khr.currency_code
1365                ,chr.currency_conversion_type
1366                ,chr.currency_conversion_rate
1367                ,chr.currency_conversion_date
1368         FROM    okl_k_headers chr,
1369                 okc_k_headers_b khr
1370         WHERE   chr.id = khr.id
1371         AND     khr.id = p_khr_id;
1372 
1373     r_curr_conv l_curr_conv_csr%ROWTYPE;
1374 --
1375 
1376     --- vpanwar 21/02/2007 Added
1377     --- get the funding line amount based on funding_line_id
1378       CURSOR c_amount(p_fund_line_id  NUMBER)  IS
1379          SELECT nvl(tl.amount,0)
1380          FROM   okl_txl_ap_inv_lns_b tl
1381          WHERE tl.id = p_fund_line_id;
1382     ---- vpanwar 21/02/2007 End
1383 
1384 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1385       CURSOR c_fund_lines (p_fund_id NUMBER) IS
1386          SELECT tpl.id,
1387                 tpl.sty_id,
1388                 tpl.amount
1389          FROM okl_txl_ap_inv_lns_all_b tpl
1390          WHERE tpl.tap_id = p_fund_id;
1391 
1392       l_date_invoiced okl_trx_ap_invs_all_b.date_invoiced%type;
1393       l_tap_amount okl_trx_ap_invs_all_b.amount%type;
1394 --      l_fund_line_id okl_txl_ap_inv_lns_all_b.id%type;
1395       l_sty_id okl_txl_ap_inv_lns_all_b.sty_id%type;
1396 --      l_amount okl_txl_ap_inv_lns_all_b.amount%type;
1397       cnt NUMBER;
1398 
1399 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1400 
1401   BEGIN
1402     x_return_status      := OKL_API.G_RET_STS_SUCCESS;
1403     -- Call start_activity to create savepoint, check compatibility
1404     -- and initialize message list
1405     x_return_status := OKL_API.START_ACTIVITY (
1406                                l_api_name,
1407                                p_init_msg_list,
1408                                '_PVT',
1409                                x_return_status);
1410     -- Check if activity started successfully
1411     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1412        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1413     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
1414        RAISE OKL_API.G_EXCEPTION_ERROR;
1415     END IF;
1416 
1417 
1418     IF (p_status = 'APPROVED') THEN
1419       OPEN c(p_fund_id);
1420       FETCH c INTO l_chr_id,
1421                    l_funding_type_code,
1422                    l_ipvs_id,
1423 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1424                    l_tap_amount,
1425                    l_date_invoiced,
1426 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1427                    l_org_id;
1428       CLOSE c;
1429 
1430       OPEN pdt_c(l_chr_id);
1431       FETCH pdt_c INTO l_product_id,
1432                        --Bug# 4662198
1433                        l_scs_code;
1434       CLOSE pdt_c;
1435 
1436 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1437 -- cklee 11.5.10 subsidy
1438 /*
1439       IF (l_funding_type_code = G_ASSET_SUBSIDY) THEN
1440         l_amount := p_subsidy_amt;
1441       ELSE
1442 
1443       --- vpanwar 21/02/2007 changed
1444 --        /**
1445         l_amount := nvl(OKL_FUNDING_PVT.get_contract_line_funded_amt(p_fund_id, l_funding_type_code),0);
1446 --        **
1447         OPEN c_amount(p_fund_line_id);
1448         FETCH c_amount INTO l_amount;
1449         CLOSE c_amount;
1450        --- vpanwar 21/02/2007 end
1451       END IF;
1452 */
1453 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1454 
1455 
1456       IF ( (l_funding_type_code = G_SUPPLIER_RETENTION_TYPE_CODE)
1457            OR
1458 --           (l_funding_type_code = G_PREFUNDING_TYPE_CODE AND l_amount < 0)
1459            (l_funding_type_code = G_PREFUNDING_TYPE_CODE AND l_tap_amount < 0)
1460            OR
1461 -- cklee 11.5.10 subsidy
1462            (l_funding_type_code = G_ASSET_SUBSIDY)
1463          )
1464       THEN
1465         l_trx_name := G_TRANSACTION_DEBIT_MEMO;
1466 
1467       -- cklee 05/04/2004
1468       Elsif (l_funding_type_code = G_MANUAL_DISB) THEN
1469         l_trx_name := G_TRANSACTION_DISBURSEMENT;
1470       ELSE
1471         l_trx_name := G_TRANSACTION_FUNDING;
1472       END IF;
1473 
1474       OPEN trans_csr(l_trx_name);
1475       FETCH trans_csr INTO l_trans_id;
1476       CLOSE trans_csr;
1477 
1478       IF (l_funding_type_code = G_PREFUNDING_TYPE_CODE) THEN
1479         l_name := G_STY_PURPOSE_CODE_PREFUNDING;
1480       ELSIF (l_funding_type_code = G_BORROWER_PAYMENT_TYPE_CODE) THEN
1481         l_name := G_STY_PURPOSE_CODE_P_BALANCE;
1482 --10-22-2004
1483       ELSIF (l_funding_type_code = G_ASSET_TYPE) THEN
1484 -- start: comment out by cklee: 10/07/2004
1485 --      ELSE
1486         l_name := G_STY_PURPOSE_CODE_FUNDING;
1487 -- end: comment out by cklee: 10/07/2004
1488       END IF;
1489 
1490 /* cklee: comment out 09/15/2004
1491       OPEN  stream_c(l_name);
1492       FETCH  stream_c INTO l_stream_id;
1493       CLOSE  stream_c;
1494 */
1495 
1496 -- cklee: user defined stream changes
1497       IF (l_funding_type_code = G_BORROWER_PAYMENT_TYPE_CODE) THEN
1498 
1499         Okl_Streams_Util.GET_DEPENDENT_STREAM_TYPE(
1500                p_khr_id                => l_chr_id,
1501                p_primary_sty_purpose   => 'RENT',
1502                p_dependent_sty_purpose => l_name,
1503                x_return_status         => x_return_status,
1504                x_dependent_sty_id      => l_stream_id );
1505 
1506 --      ELSE
1507 --10-22-2004
1508       ELSIF (l_funding_type_code in (G_PREFUNDING_TYPE_CODE, G_ASSET_TYPE)) THEN
1509 
1510         Okl_Streams_Util.GET_PRIMARY_STREAM_TYPE(
1511                p_khr_id              => l_chr_id,
1512                p_primary_sty_purpose => l_name,
1513                x_return_status       => x_return_status,
1514                x_primary_sty_id      => l_stream_id );
1515 
1516       END IF;
1517 
1518       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1519         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1520       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
1521         RAISE OKL_API.G_EXCEPTION_ERROR;
1522       END IF;
1523 -- cklee: user defined stream changes
1524 
1525 
1526 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1527 /*
1528 -- cklee 11.5.10 subsidy
1529       IF (l_funding_type_code = G_ASSET_SUBSIDY) THEN
1530         l_stream_id := p_sty_id;
1531 
1532       -- cklee 05/04/2004
1533 -- cklee: 10/07/2004
1534 -- added Expense and Supplier rentation funding type
1535       ELSIF (l_funding_type_code in (G_MANUAL_DISB, G_SUPPLIER_RETENTION_TYPE_CODE, G_EXPENSE)) THEN
1536 
1537         OPEN manu_disb(p_fund_id);
1538         FETCH manu_disb INTO l_fund_line_id, l_stream_id;
1539         CLOSE manu_disb;
1540 
1541       END IF;
1542 */
1543 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1544 
1545 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1546 -- Major changes:
1547 -- 1. Change the logic to fit in the new spec of accounting API
1548 -- 2. Call OKL_SECURITIZATION_PVT.Check_Khr_ia_associated only once for a funding header
1549 -- 3. Call Okl_Account_Dist_Pub.CREATE_ACCOUNTING_DIST only once for a funding header
1550 --
1551       -- We need to call once per khr_id
1552       Okl_Securitization_Pvt.check_khr_ia_associated(p_api_version => p_api_version
1553                                                 ,p_init_msg_list => p_init_msg_list
1554                                                 ,x_return_status => x_return_status
1555                                                 ,x_msg_count => x_msg_count
1556                                                 ,x_msg_data => x_msg_data
1557                                                 ,p_khr_id =>  l_chr_id
1558                                                 ,p_scs_code => NULL
1559                                                 ,p_trx_date => l_date_invoiced
1560                                                 ,x_fact_synd_code => l_fact_synd_code
1561                                                 ,x_inv_acct_code => l_inv_acct_code);
1562 
1563       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1564          RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1565       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
1566          RAISE OKL_API.G_EXCEPTION_ERROR;
1567       END IF;
1568 
1569       cnt := 0;
1570       FOR r_this IN c_fund_lines (p_fund_id) LOOP
1571 
1572         IF (l_funding_type_code in (G_ASSET_SUBSIDY,
1573 	                            G_MANUAL_DISB,
1574 	                            G_SUPPLIER_RETENTION_TYPE_CODE,
1575 								G_EXPENSE)) THEN
1576           l_stream_id := r_this.sty_id;
1577         END IF;
1578 
1579         l_tmpl_identify_tbl(cnt).PRODUCT_ID := l_product_id;
1580         l_tmpl_identify_tbl(cnt).TRANSACTION_TYPE_ID := l_trans_id;
1581         l_tmpl_identify_tbl(cnt).STREAM_TYPE_ID := l_stream_id;
1582 
1583         l_tmpl_identify_tbl(cnt).MEMO_YN := 'N';
1584         l_tmpl_identify_tbl(cnt).PRIOR_YEAR_YN := 'N';
1585         l_tmpl_identify_tbl(cnt).factoring_synd_flag := l_fact_synd_code;
1586         l_tmpl_identify_tbl(cnt).investor_code       := l_inv_acct_code;
1587 
1588 --    l_dist_info_rec.SOURCE_ID := p_fund_id;
1589 -- cklee 11.5.10 subsidy
1590 
1591 --- vpanwar 21/02/2007 Changed
1592     /**
1593     IF (l_funding_type_code = G_ASSET_SUBSIDY) THEN
1594 
1595       l_dist_info_rec.SOURCE_ID := p_fund_line_id;
1596       l_dist_info_rec.SOURCE_TABLE := G_OKL_SUBSIDY_SOURCE_TABLE;
1597     ELSIF (l_funding_type_code = G_MANUAL_DISB) THEN
1598 
1599       l_dist_info_rec.SOURCE_ID := l_fund_line_id;
1600       l_dist_info_rec.SOURCE_TABLE := G_OKL_MANUAL_DISB_SOURCE_TABLE;
1601     ELSE
1602       l_dist_info_rec.SOURCE_ID := p_fund_id;
1603       l_dist_info_rec.SOURCE_TABLE := G_OKL_FUNDING_SOURCE_TABLE;
1604     END IF;
1605     **/
1606 
1607         l_dist_info_tbl(cnt).SOURCE_ID := r_this.id;
1608         l_dist_info_tbl(cnt).SOURCE_TABLE := G_OKL_FUNDING_SOURCE_TABLE;
1609 
1610 --- vpanwar 21/02/2007 End
1611 
1612         l_dist_info_tbl(cnt).ACCOUNTING_DATE := l_date_invoiced;--sysdate;
1613 
1614         l_dist_info_tbl(cnt).GL_REVERSAL_FLAG := 'N';
1615         l_dist_info_tbl(cnt).POST_TO_GL := 'N';
1616 
1617       -- changed to positive if it's negative
1618       -- sjalasut, commented the following code as part of OKLR12B Disbursements Project
1619       -- IF (l_amount < 0) THEN
1620         -- l_amount := -(l_amount);
1621       -- END IF;
1622       --
1623         l_dist_info_tbl(cnt).AMOUNT := r_this.amount;
1624 
1625 -- multi-currency info
1626 -- cklee 05/04/2004
1627         open l_curr_conv_csr(l_chr_id);
1628         fetch l_curr_conv_csr into r_curr_conv;
1629         close l_curr_conv_csr;
1630 
1631         l_dist_info_tbl(cnt).currency_code		:= r_curr_conv.currency_code;
1632         l_dist_info_tbl(cnt).contract_id		:= l_chr_id;
1633 
1634         -- Fill the Multi Currency parameters
1635         l_dist_info_tbl(cnt).currency_conversion_type
1636                             := r_curr_conv.CURRENCY_CONVERSION_TYPE;
1637         l_dist_info_tbl(cnt).currency_conversion_rate
1638                             := r_curr_conv.CURRENCY_CONVERSION_RATE;
1639         l_dist_info_tbl(cnt).currency_conversion_date
1640                             := r_curr_conv.CURRENCY_CONVERSION_DATE;
1641 
1642 --
1643 
1644         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(0).source_table := 'AP_VENDOR_SITES_V';
1645         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(0).primary_key_column := l_ipvs_id;
1646 
1647         OPEN ar_site_uses_csr (l_chr_id);
1648         FETCH ar_site_uses_csr INTO l_site_uses_id;
1649         CLOSE ar_site_uses_csr;
1650         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(1).source_table := 'AR_SITE_USES_V';
1651         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(1).primary_key_column := l_site_uses_id;
1652 
1653         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(2).source_table := 'FINANCIALS_SYSTEM_PARAMETERS';
1654         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(2).primary_key_column := l_org_id;--mo_global.get_current_org_id();
1655 
1656 
1657         OPEN salesP_csr (l_chr_id);
1658         FETCH salesP_csr INTO l_sales_id;
1659         CLOSE salesP_csr;
1660         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(3).source_table := 'JTF_RS_SALESREPS_MO_V';
1661         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(3).primary_key_column := l_sales_id;
1662 
1663         OPEN ra_cust_trx_types_csr (l_chr_id);
1664         FETCH ra_cust_trx_types_csr INTO l_cust_trx_type_id;
1665         CLOSE ra_cust_trx_types_csr;
1666         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(4).source_table := 'RA_CUST_TRX_TYPES';
1667         l_acc_gen_primary_key_tbl(cnt).acc_gen_key_tbl(4).primary_key_column := l_cust_trx_type_id;
1668 
1669 --start:| 05-Jul-2007 cklee Fixed Accounting call issue: assigned the following:     |
1670 --|      l_acc_gen_primary_key_tbl(cnt).source_id := r_this.id;      |
1671         l_acc_gen_primary_key_tbl(cnt).source_id := r_this.id;
1672 
1673 --end:| 05-Jul-2007 cklee Fixed Accounting call issue: assigned the following:     |
1674 --|      l_acc_gen_primary_key_tbl(cnt).source_id := lx_tplv_tbl(cnt).id;      |
1675       --Bug# 4622198
1676 
1677       cnt := cnt + 1;
1678       END LOOP;
1679 
1680       Okl_Account_Dist_Pvt.CREATE_ACCOUNTING_DIST(
1681 								   p_api_version             => p_api_version
1682                                   ,p_init_msg_list           => p_init_msg_list
1683                                   ,x_return_status  	     => x_return_status
1684                                   ,x_msg_count      	     => x_msg_count
1685                                   ,x_msg_data       	     => x_msg_data
1686                                   ,p_tmpl_identify_tbl 	     => l_tmpl_identify_tbl
1687                                   ,p_dist_info_tbl           => l_dist_info_tbl
1688                                   ,p_ctxt_val_tbl            => l_ctxt_val_tbl
1689                                   ,p_acc_gen_primary_key_tbl => l_acc_gen_primary_key_tbl
1690                                   ,x_template_tbl            => l_template_tbl
1691                                   ,x_amount_tbl              => l_amount_tbl
1692                                   ,p_trx_header_id           => p_fund_id
1693                                   ,p_trx_header_table        => 'OKL_TRX_AP_INVOICES_B');
1694 
1695       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
1696         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
1697       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
1698         RAISE OKL_API.G_EXCEPTION_ERROR;
1699       END IF;
1700 
1701     END IF;
1702 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
1703 
1704     OKL_API.END_ACTIVITY (x_msg_count,
1705                           x_msg_data );
1706   EXCEPTION
1707     WHEN OTHERS THEN
1708       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1709       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
1710                           p_msg_name      => G_UNEXPECTED_ERROR,
1711                           p_token1        => G_SQLCODE_TOKEN,
1712                           p_token1_value  => SQLCODE,
1713                           p_token2        => G_SQLERRM_TOKEN,
1714                           p_token2_value  => SQLERRM);
1715   END CREATE_ACCOUNTING_DIST;
1716 
1717 ----------------------------------------------------------------------------
1718  FUNCTION get_creditRem_by_chrid(
1719   p_contract_id                       IN NUMBER                 -- contract hdr
1720  ) RETURN NUMBER
1721 IS
1722 
1723     --l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
1724     l_api_version       NUMBER := 1.0;
1725     l_init_msg_list     VARCHAR2(1) := OKL_API.G_FALSE;
1726     x_return_status     VARCHAR2(1);
1727     x_msg_count         NUMBER;
1728     x_msg_data          VARCHAR2(2000);
1729     x_value             NUMBER := 0;
1730     l_credit_id NUMBER := 0;
1731     l_row_found boolean := false;
1732 begin
1733     l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_contract_id);
1734     IF (l_credit_id IS NOT NULL) THEN
1735       x_value := OKL_SEEDED_FUNCTIONS_PVT.creditline_total_remaining(l_credit_id);
1736     ELSE
1737       x_value := 0;
1738     END IF;
1739 
1740   RETURN x_value;
1741 
1742 
1743   EXCEPTION
1744     WHEN OTHERS THEN
1745       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1746       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
1747                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
1748                           p_token1        => 'OKL_SQLCODE',
1749                           p_token1_value  => SQLCODE,
1750                           p_token2        => 'OKL_SQLERRM',
1751                           p_token2_value  => SQLERRM);
1752       RETURN 0;
1753 END;
1754 
1755 
1756 -- Check to see if contract is legal to fund
1757 ----------------------------------------------------------------------------
1758  FUNCTION is_chr_fundable_status(
1759   p_contract_id                       IN NUMBER                 -- contract hdr
1760   --Bug# 10156343
1761  ,p_funding_type_code                 IN VARCHAR2 DEFAULT NULL
1762  ) RETURN NUMBER
1763 IS
1764   l_row_notfound boolean := false;
1765   l_yn number := 0;
1766   l_sts_code OKC_K_HEADERS_B.sts_code%TYPE;
1767   l_deal_type OKL_K_HEADERS.deal_type%TYPE;
1768 
1769   CURSOR c_deal_type(p_contract_id  NUMBER)
1770   IS
1771   select chr.sts_code,
1772          khr.deal_type
1773   from  OKL_K_HEADERS khr,
1774         OKC_K_HEADERS_B chr
1775   where khr.id = chr.id
1776   and   khr.id = p_contract_id
1777   ;
1778 
1779 
1780   CURSOR c (p_contract_id  NUMBER)
1781   IS
1782 select 1
1783 from   okc_statuses_b ste,
1784        okc_k_headers_b chr
1785 where  ste.code = chr.sts_code
1786 and    ste.ste_code in ('ENTERED', 'ACTIVE','SIGNED')
1787 and    chr.id = p_contract_id
1788   ;
1789 
1790 BEGIN
1791 
1792 --
1793 -- assume this is a valid contract id
1794 --
1795   OPEN c_deal_type (p_contract_id);
1796   FETCH c_deal_type INTO l_sts_code,
1797                          l_deal_type;
1798   CLOSE c_deal_type;
1799 
1800   IF (l_deal_type = 'LOAN-REVOLVING') THEN
1801 
1802     --Bug# 10156343
1803     -- Allow Manual Disbursement for Terminated and Expired contracts
1804     IF (NVL(p_funding_type_code,'XXX') = G_MANUAL_DISB) THEN
1805       IF (l_sts_code IN ('BOOKED','TERMINATED','EXPIRED')) THEN
1806         l_yn := 1;
1807       ELSE
1808         l_yn := 0;
1809       END IF;
1810     ELSE
1811       IF (l_sts_code = 'BOOKED') THEN
1812         l_yn := 1;
1813       ELSE
1814         l_yn := 0;
1815       END IF;
1816     END IF;
1817 
1818   ELSE -- for any other type of contract
1819 
1820     OPEN c (p_contract_id);
1821 
1822 
1823     FETCH c INTO l_yn;
1824     l_row_notfound := c%NOTFOUND;
1825     CLOSE c;
1826 
1827     IF (l_row_notfound) THEN
1828       --Bug# 10156343
1829       -- Allow Manual Disbursement for Terminated and Expired contracts
1830       IF (NVL(p_funding_type_code,'XXX') = G_MANUAL_DISB) THEN
1831         IF (l_sts_code IN ('TERMINATED','EXPIRED')) THEN
1832           l_yn := 1;
1833         ELSE
1834           l_yn := 0;
1835         END IF;
1836       ELSE
1837         l_yn := 0;
1838       END IF;
1839     ELSE
1840       l_yn := 1;
1841     END IF;
1842 
1843   END IF;
1844 
1845   RETURN l_yn;
1846 
1847   EXCEPTION
1848     WHEN OTHERS THEN
1849       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1850       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
1851                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
1852                           p_token1        => 'OKL_SQLCODE',
1853                           p_token1_value  => SQLCODE,
1854                           p_token2        => 'OKL_SQLERRM',
1855                           p_token2_value  => SQLERRM);
1856       RETURN 0;
1857 END;
1858 ----------------------------------------------------------------------------
1859 -- Total contract funded adjustments
1860  FUNCTION get_chr_funded_adjs(
1861   p_contract_id                  IN NUMBER                 -- contract hdr
1862  ,p_vendor_site_id               IN NUMBER
1863  ) RETURN NUMBER
1864 IS
1865   l_amount NUMBER := 0;
1866 
1867   -- sjalasut, modified the below cursor to make khr_id referred from
1868   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
1869   -- as part of OKLR12B disbursements project
1870   CURSOR c (p_contract_id  NUMBER)
1871   IS
1872   select nvl(sum(a.amount),0)
1873   from okl_trx_ap_invoices_b a
1874       ,okl_txl_ap_inv_lns_all_b b
1875   where a.id = b.tap_id
1876   and a.funding_type_code = 'PREFUNDING'
1877   and a.trx_status_code in ('APPROVED', 'PROCESSED')
1878   and a.amount < 0 -- adjustments
1879   and b.khr_id = p_contract_id;
1880 
1881   -- sjalasut, modified the below cursor to make khr_id referred from
1882   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
1883   -- as part of OKLR12B disbursements project
1884   CURSOR c2 (p_contract_id  NUMBER, p_vendor_site_id NUMBER)
1885   IS
1886   select nvl(sum(a.amount),0)
1887   from okl_trx_ap_invoices_b a
1888       ,okl_txl_ap_inv_lns_all_b b
1889   where a.id = b.tap_id
1890   and a.funding_type_code = 'PREFUNDING'
1891   and a.trx_status_code in ('APPROVED', 'PROCESSED')
1892   and a.amount < 0 -- adjustments
1893   and b.khr_id = p_contract_id
1894   and a.ipvs_id = p_vendor_site_id;
1895 
1896 BEGIN
1897 
1898   IF (p_vendor_site_id IS NULL OR p_vendor_site_id = OKL_API.G_MISS_NUM) THEN
1899 
1900     OPEN c (p_contract_id);
1901     FETCH c INTO l_amount;
1902     CLOSE c;
1903   ELSE
1904     OPEN c2 (p_contract_id, p_vendor_site_id);
1905     FETCH c2 INTO l_amount;
1906     CLOSE c2;
1907   END IF;
1908 
1909   RETURN l_amount;
1910 
1911   EXCEPTION
1912     WHEN OTHERS THEN
1913       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1914       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
1915                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
1916                           p_token1        => 'OKL_SQLCODE',
1917                           p_token1_value  => SQLCODE,
1918                           p_token2        => 'OKL_SQLERRM',
1919                           p_token2_value  => SQLERRM);
1920       RETURN 0;
1921 END;
1922 ----------------------------------------------------------------------------
1923 
1924 -- Total contract allowable funded remaining
1925  FUNCTION get_chr_canbe_funded_rem(
1926   p_contract_id                       IN NUMBER                 -- contract hdr
1927  ) RETURN NUMBER
1928 IS
1929   l_amount NUMBER := 0;
1930   l_amount_buffer NUMBER := 0;
1931   l_amount_hasbeen_funded NUMBER := 0;
1932   l_amount_canbe_funded NUMBER := 0;
1933 
1934   l_loan_rev NUMBER;
1935   l_loan_rev_notfound boolean := false;
1936 
1937 --START:bug#4882537
1938   l_amt_hasbeen_funded_sub number;
1939 --END:bug#4882537
1940 
1941   CURSOR c_loan_revolving (p_contract_id  NUMBER)
1942   IS
1943   select 1 from OKL_K_HEADERS khr
1944   where khr.id = p_contract_id
1945   and khr.deal_type = 'LOAN-REVOLVING';
1946 
1947   BEGIN
1948 
1949     OPEN c_loan_revolving(p_contract_id);
1950     FETCH c_loan_revolving INTO l_loan_rev;
1951     l_loan_rev_notfound := c_loan_revolving%NOTFOUND;
1952     CLOSE c_loan_revolving;
1953 
1954 
1955     -- is not loan revolving contract
1956     IF (l_loan_rev_notfound) THEN
1957 
1958       l_amount_hasbeen_funded := get_total_funded(p_contract_id);
1959       l_amount_canbe_funded := get_chr_canbe_funded(p_contract_id);
1960       l_amount := l_amount_canbe_funded - l_amount_hasbeen_funded;
1961 --START:bug#4882537
1962 -- Subsidy is a negative amount. So we just add back to the remaining balance
1963 -- of the contract for the funding
1964       l_amt_hasbeen_funded_sub := get_amount_subsidy(p_contract_id);
1965       l_amount := l_amount_canbe_funded - l_amount_hasbeen_funded + l_amt_hasbeen_funded_sub;
1966 --      l_amount := l_amount_canbe_funded - l_amount_hasbeen_funded;
1967 --END:bug#4882537
1968 
1969     ELSE
1970       -- get amount for the remaining of the attach credit line
1971       l_amount := get_creditRem_by_chrid(p_contract_id);
1972 
1973     END IF;
1974 
1975   RETURN l_amount;
1976   EXCEPTION
1977 
1978 
1979     WHEN OTHERS THEN
1980       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1981       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
1982                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
1983                           p_token1        => 'OKL_SQLCODE',
1984                           p_token1_value  => SQLCODE,
1985                           p_token2        => 'OKL_SQLERRM',
1986                           p_token2_value  => SQLERRM);
1987       RETURN 0;
1988 
1989 END;
1990 
1991 ----------------------------------------------------------------------------
1992 
1993 -- Total contract allowable funded
1994  FUNCTION get_chr_canbe_funded(
1995   p_contract_id                       IN NUMBER                 -- contract hdr
1996  ) RETURN NUMBER
1997 IS
1998   l_amount NUMBER := 0;
1999   l_amount_oec NUMBER := 0;
2000   l_amount_expense NUMBER := 0;
2001   l_loan_rev NUMBER := 0;
2002   l_loan_rev_notfound boolean := false;
2003   l_credit_id NUMBER;
2004 
2005   CURSOR c_loan_revolving (p_contract_id  NUMBER)
2006   IS
2007   select 1 from OKL_K_HEADERS khr
2008   where khr.id = p_contract_id
2009   and khr.deal_type = 'LOAN-REVOLVING';
2010 
2011   BEGIN
2012 
2013     OPEN c_loan_revolving(p_contract_id);
2014 
2015     FETCH c_loan_revolving INTO l_loan_rev;
2016     l_loan_rev_notfound := c_loan_revolving%NOTFOUND;
2017     CLOSE c_loan_revolving;
2018 
2019     -- is not loan revolving contract
2020     IF (l_loan_rev_notfound) THEN
2021 
2022       l_amount_oec := get_chr_oec_canbe_funded(p_contract_id);
2023 
2024       l_amount_expense := get_chr_exp_canbe_funded_amt(p_contract_id);
2025 
2026       l_amount := l_amount_oec + l_amount_expense;
2027 
2028     ELSE
2029       -- get amount for the remaining of the attach credit line
2030       l_amount := get_creditRem_by_chrid(p_contract_id);
2031 
2032     END IF;
2033 
2034   RETURN l_amount;
2035   EXCEPTION
2036     WHEN OTHERS THEN
2037       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2038       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2039                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2040                           p_token1        => 'OKL_SQLCODE',
2041                           p_token1_value  => SQLCODE,
2042                           p_token2        => 'OKL_SQLERRM',
2043                           p_token2_value  => SQLERRM);
2044       RETURN 0;
2045 
2046 END;
2047 
2048 ----------------------------------------------------------------------------
2049 
2050 -- Total contract allowable oec funded remaining
2051  FUNCTION get_chr_oec_canbe_funded_rem(
2052   p_contract_id                      IN NUMBER                 -- contract hdr
2053  ) RETURN NUMBER
2054 IS
2055   l_amount NUMBER := 0;
2056   l_total_canbe_OEC_amount NUMBER := 0;
2057   l_oec_hasbeen_funded_amount NUMBER := 0;
2058 
2059   BEGIN
2060 
2061     l_total_canbe_OEC_amount:= get_chr_oec_canbe_funded(p_contract_id);
2062     l_oec_hasbeen_funded_amount := get_chr_oec_hasbeen_funded_amt(p_contract_id);
2063     l_amount := l_total_canbe_OEC_amount - l_oec_hasbeen_funded_amount;
2064 
2065   RETURN l_amount;
2066   EXCEPTION
2067     WHEN OTHERS THEN
2068       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2069       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2070                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2071                           p_token1        => 'OKL_SQLCODE',
2072                           p_token1_value  => SQLCODE,
2073                           p_token2        => 'OKL_SQLERRM',
2074                           p_token2_value  => SQLERRM);
2075       RETURN 0;
2076 
2077 END;
2078 
2079 ----------------------------------------------------------------------------
2080 
2081 -- Total contract allowable oec funded
2082  FUNCTION get_chr_oec_canbe_funded(
2083   p_contract_id                      IN NUMBER                 -- contract hdr
2084  ) RETURN NUMBER
2085 IS
2086   l_amount NUMBER := 0;
2087 
2088 
2089   BEGIN
2090 
2091     IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 1) THEN
2092 
2093       l_amount := OKL_FUNDING_PVT.get_contract_line_amt(p_contract_id);
2094     END IF;
2095 
2096   RETURN l_amount;
2097   EXCEPTION
2098     WHEN OTHERS THEN
2099       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2100       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2101                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2102                           p_token1        => 'OKL_SQLCODE',
2103                           p_token1_value  => SQLCODE,
2104                           p_token2        => 'OKL_SQLERRM',
2105                           p_token2_value  => SQLERRM);
2106       RETURN 0;
2107 
2108 END;
2109 
2110 ----------------------------------------------------------------------------
2111 
2112 -- Total contract has been funded oec amount
2113  FUNCTION get_chr_oec_hasbeen_funded_amt(
2114   p_contract_id                       IN NUMBER                 -- contract hdr
2115  ,p_vendor_site_id               IN NUMBER
2116 
2117  ) RETURN NUMBER
2118 IS
2119   l_amount NUMBER := 0;
2120 
2121   -- get approved amount for Asset
2122   -- sjalasut, made changes to the below cursor to have khr_id be referred
2123   -- from okl_txl_inv_lns_all_b instead of okl_trx_ap_invoices_b.
2124   -- also changed the from clause to okl_txl_ap_inv_lns_all_b
2125   CURSOR c_tot_asset_fund (p_contract_id  NUMBER)
2126   IS
2127   select nvl(sum(b.amount),0)
2128   from okl_trx_ap_invoices_b a,
2129        okl_txl_ap_inv_lns_all_b b
2130   where a.id = b.tap_id
2131   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2132   and a.funding_type_code ='ASSET'
2133   and b.amount > 0 --?
2134   and b.khr_id = p_contract_id
2135   ;
2136 
2137   -- sjalasut, made changes to the below cursor to have khr_id be referred
2138   -- from okl_txl_inv_lns_all_b instead of okl_trx_ap_invoices_b.
2139   -- also changed the from clause to okl_txl_ap_inv_lns_all_b
2140   CURSOR c_tot_asset_fund_ven (p_contract_id  NUMBER, p_vendor_site_id NUMBER)
2141   IS
2142   select nvl(sum(b.amount),0)
2143   from okl_trx_ap_invoices_b a,
2144        okl_txl_ap_inv_lns_all_b b
2145   where a.id = b.tap_id
2146   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2147   and a.funding_type_code ='ASSET'
2148   and b.amount > 0 --?
2149   and b.khr_id = p_contract_id
2150   and a.ipvs_id = p_vendor_site_id
2151   ;
2152 
2153   BEGIN
2154     IF (p_vendor_site_id IS NULL OR p_vendor_site_id = OKL_API.G_MISS_NUM)
2155     THEN
2156 
2157       OPEN c_tot_asset_fund(p_contract_id);
2158       FETCH c_tot_asset_fund INTO l_amount;
2159       CLOSE c_tot_asset_fund;
2160     ELSE
2161       OPEN c_tot_asset_fund_ven(p_contract_id,p_vendor_site_id);
2162       FETCH c_tot_asset_fund_ven INTO l_amount;
2163       CLOSE c_tot_asset_fund_ven;
2164 
2165     END IF;
2166 
2167   RETURN l_amount;
2168   EXCEPTION
2169     WHEN OTHERS THEN
2170       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2171       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2172                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2173                           p_token1        => 'OKL_SQLCODE',
2174                           p_token1_value  => SQLCODE,
2175                           p_token2        => 'OKL_SQLERRM',
2176                           p_token2_value  => SQLERRM);
2177       RETURN 0;
2178 
2179 END;
2180 
2181 ----------------------------------------------------------------------------
2182 
2183 
2184 -- Total contract allowable expnese funded remaining
2185  FUNCTION get_chr_exp_canbe_funded_rem(
2186   p_contract_id                       IN NUMBER                 -- contract hdr
2187   ,p_vendor_site_id               IN NUMBER                 -- vendor_site_id
2188  ) RETURN NUMBER
2189 IS
2190   l_amount NUMBER := 0;
2191   l_total_canbe_expense_amount NUMBER := 0;
2192   l_expense_hasbeen_funded_amt NUMBER := 0;
2193 
2194   BEGIN
2195     l_expense_hasbeen_funded_amt:= get_chr_exp_hasbeen_funded_amt(p_contract_id,p_vendor_site_id);
2196 
2197     l_total_canbe_expense_amount := get_chr_exp_canbe_funded_amt(p_contract_id,p_vendor_site_id);
2198     l_amount := l_total_canbe_expense_amount - l_expense_hasbeen_funded_amt;
2199 
2200   RETURN l_amount;
2201   EXCEPTION
2202     WHEN OTHERS THEN
2203       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2204       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2205                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2206                           p_token1        => 'OKL_SQLCODE',
2207                           p_token1_value  => SQLCODE,
2208                           p_token2        => 'OKL_SQLERRM',
2209                           p_token2_value  => SQLERRM);
2210       RETURN 0;
2211 END;
2212 ----------------------------------------------------------------------------
2213 
2214 -- Total contract has been funded expense
2215  FUNCTION get_chr_exp_hasbeen_funded_amt(
2216   p_contract_id                       IN NUMBER                 -- contract hdr
2217   ,p_vendor_site_id               IN NUMBER                 -- vendor_site_id
2218  ) RETURN NUMBER
2219 
2220 IS
2221   l_amount NUMBER := 0;
2222 
2223   -- get approved amount for Expense by specific vendor
2224   -- sjalasut, modified the below cursor to have khr_id be referred from
2225   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices. changes made
2226   -- as part of OKLR12B disbursements project.
2227   CURSOR c_tot_expense_fund (p_contract_id  NUMBER, p_vendor_site_id  NUMBER)
2228   IS
2229   select nvl(sum(b.amount),0)
2230   from okl_trx_ap_invoices_b a,
2231        okl_txl_ap_inv_lns_b b
2232   where a.id = b.tap_id
2233   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2234   and a.funding_type_code ='EXPENSE'
2235   and b.amount > 0 --?
2236   and b.khr_id = p_contract_id
2237   and exists (select null
2238               from   okx_vendor_sites_v vs
2239               where  vs.id1 = a.ipvs_id
2240               and    vs.id1 = p_vendor_site_id)
2241   ;
2242 
2243   BEGIN
2244     OPEN c_tot_expense_fund(p_contract_id, p_vendor_site_id);
2245     FETCH c_tot_expense_fund INTO l_amount;
2246     CLOSE c_tot_expense_fund;
2247 
2248   RETURN l_amount;
2249   EXCEPTION
2250     WHEN OTHERS THEN
2251       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2252       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2253                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2254                           p_token1        => 'OKL_SQLCODE',
2255                           p_token1_value  => SQLCODE,
2256                           p_token2        => 'OKL_SQLERRM',
2257                           p_token2_value  => SQLERRM);
2258       RETURN 0;
2259 END;
2260 ----------------------------------------------------------------------------
2261 
2262 -- Total contract has been funded expense
2263  FUNCTION get_chr_exp_hasbeen_funded_amt(
2264   p_contract_id                       IN NUMBER                 -- contract hdr
2265  ) RETURN NUMBER
2266 IS
2267   l_amount NUMBER := 0;
2268 
2269 -- get approved amount for Expense
2270   -- sjalasut, made changes to the below cursor to have khr_id referred to
2271   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. change made
2272   -- as part of OKLR12B disbursements project.
2273   CURSOR c_tot_expense_fund (p_contract_id  NUMBER)
2274   IS
2275   select nvl(sum(b.amount),0)
2276   from okl_trx_ap_invoices_b a,
2277        okl_txl_ap_inv_lns_b b
2278   where a.id = b.tap_id
2279   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2280   and a.funding_type_code ='EXPENSE'
2281   and b.amount > 0 --?
2282   and b.khr_id = p_contract_id;
2283 
2284   BEGIN
2285 
2286     OPEN c_tot_expense_fund(p_contract_id);
2287     FETCH c_tot_expense_fund INTO l_amount;
2288     CLOSE c_tot_expense_fund;
2289 
2290   RETURN l_amount;
2291   EXCEPTION
2292     WHEN OTHERS THEN
2293       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2294 
2295       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2296                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2297                           p_token1        => 'OKL_SQLCODE',
2298                           p_token1_value  => SQLCODE,
2299                           p_token2        => 'OKL_SQLERRM',
2300                           p_token2_value  => SQLERRM);
2301 
2302       RETURN 0;
2303 END;
2304 ----------------------------------------------------------------------------
2305  FUNCTION get_chr_exp_canbe_funded_amt(
2306   p_contract_id                       IN NUMBER                 -- contract hdr
2307   ,p_vendor_site_id               IN NUMBER                 -- vendor_site_id
2308  ,p_due_date                     IN date  default sysdate   --cklee added) RETURN NUMBER
2309 ) RETURN NUMBER IS
2310   l_amount NUMBER := 0;
2311   l_cle_amount NUMBER := 0;
2312   l_amount_per NUMBER := 0;
2313 
2314   l_vendor_id NUMBER := 0;
2315   l_cle_id NUMBER := 0;
2316 
2317   l_cle_start_date DATE;
2318   l_period NUMBER := 0;
2319   l_period_org NUMBER := 0;
2320 
2321   l_row_notfound   BOOLEAN;
2322   l_new_lin_amt    NUMBER :=0;
2323 
2324 
2325   CURSOR cv1 (p_vendor_site_id NUMBER)
2326   IS
2327     select vendor_id from okx_vendor_sites_v
2328     where id1 = to_char(p_vendor_site_id)
2329   ;
2330 
2331   CURSOR c (p_contract_id  NUMBER, p_vendor_id NUMBER, p_rle_code VARCHAR2)
2332   IS
2333 select nvl(cle.AMOUNT,0),
2334        cle.id,
2335        nvl(cle.start_date,k.start_date)
2336 from   OKL_K_LINES_FULL_V cle,
2337        okc_k_party_roles_b cpl,
2338        okc_line_styles_b ls,
2339        okc_k_headers_b k
2340 where  k.id = cle.dnz_chr_id
2341 and    cle.dnz_chr_id = p_contract_id
2342 and    cle.lse_id = ls.id
2343 and    ls.lty_code = p_rle_code
2344 and    cle.id = cpl.cle_id
2345 and    cpl.dnz_chr_id  = p_contract_id
2346 and    cpl.chr_id     is null
2347 and    cpl.rle_code = 'OKL_VENDOR'
2348 and    cpl.object1_id1 = to_char(p_vendor_id)
2349 and    cpl.object1_id2 = '#'
2350 -- Pass through check
2351 /* and not exists (select null
2352                 from   okc_rule_groups_v crg,
2353                        okc_rules_v cr
2354                 where  crg.dnz_chr_id = p_contract_id
2355                 and    crg.cle_id = cle.id -- line id for rle_code
2356                 and    crg.id = cr.rgp_id
2357                 and    crg.rgd_code = 'LAPSTH') */
2358 and not exists (select null
2359                 from   okl_party_payment_hdr phr
2360                 where  phr.dnz_chr_id = p_contract_id
2361                 and    phr.cle_id = cle.id)
2362 ;
2363 
2364   -- Bug# 16493240 :Start of Modification
2365   -- Added a new cursor to fetch amounts at line level
2366     CURSOR c_new_lin_amt (p_contract_id  NUMBER,p_cle_id VARCHAR2)
2367     IS
2368 	SELECT SUM(NVL(kle_cov_asset.capital_amount,0)) amount
2369 	FROM   okc_k_lines_b cleb_cov_asset,
2370 	       okl_k_lines_full_v kle_cov_asset,
2371 	       okc_k_items cim_cov_asset,
2372 	       okc_k_lines_b cleb_fin,
2373 	       okc_statuses_b sts,
2374 	       okl_k_lines kle
2375 	WHERE  kle_cov_asset.id = cleb_cov_asset.id
2376 	AND    cim_cov_asset.cle_id = cleb_cov_asset.id
2377 	AND    cim_cov_asset.dnz_chr_id = cleb_cov_asset.dnz_chr_id
2378 	AND    cim_cov_asset.jtot_object1_code = 'OKX_COVASST'
2379 	AND    cleb_fin.id = cim_cov_asset.object1_id1
2380 	AND    CLEB_FIN.STS_CODE = STS.CODE
2381 	AND    STS.STE_CODE <> 'CANCELLED'
2382 	and cleb_cov_asset.dnz_chr_id = p_contract_id
2383 	and kle.id = cim_cov_asset.object1_id1
2384 	--and nvl(kle.re_lease_yn,'N') <> 'Y' -- ndani - 26-Mar-2013 : Bug# 16493240
2385 	and kle_cov_asset.cle_id = p_cle_id;
2386   -- Bug# 16493240 :End of Modification
2387 
2388 
2389 --
2390 -- get Number of Period
2391 --
2392 -- 1) take contract start date if line start date is null
2393 -- 2) truncate pay period if less than 0
2394 --
2395   CURSOR c_period (p_contract_id  NUMBER, p_cle_id NUMBER)
2396   IS
2397    --cklee start 10/3/2007 bug: 6128765
2398 /*select ceil(decode(cr.object1_id1, 'A', months_between(sysdate, nvl(cle.start_date, k.start_date))/12
2399                             , 'M', months_between(sysdate, nvl(cle.start_date, k.start_date))
2400                             , 'Q', months_between(sysdate, nvl(cle.start_date, k.start_date))/3
2401                             , 'S', months_between(sysdate, nvl(cle.start_date, k.start_date))/6
2402                             , months_between(sysdate, nvl(cle.start_date, k.start_date))))*/
2403  select ceil(decode(cr.object1_id1, 'A', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/12
2404                               , 'M', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))
2405                                , 'Q', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/3
2406                                , 'S', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/6
2407                                , months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))))
2408    --cklee end 10/3/2007 bug: 6128765
2409 
2410 from   okc_rule_groups_v crg,
2411        okc_rules_v cr,
2412        OKL_K_LINES_FULL_V cle,
2413        okc_k_headers_b k
2414 where  crg.dnz_chr_id = p_contract_id
2415 and    cle.dnz_chr_id = k.id
2416 and    crg.id = cr.rgp_id
2417 and    crg.rgd_code = 'LAFEXP'
2418 
2419 and    crg.cle_id = cle.id
2420 and    cr.RULE_INFORMATION_CATEGORY = 'LAFREQ'
2421 and    cle.id = p_cle_id
2422 ;
2423 
2424 --
2425 -- get amount per period
2426 --
2427 --
2428   CURSOR c_amount_per (p_contract_id  NUMBER, p_cle_id NUMBER)
2429   IS
2430 select to_number(nvl(cr.RULE_INFORMATION1,'0'))
2431        ,to_number(nvl(cr.RULE_INFORMATION2,'0'))
2432 from   okc_rule_groups_v crg,
2433        okc_rules_v cr
2434 where  crg.dnz_chr_id = p_contract_id
2435 and    crg.id = cr.rgp_id
2436 
2437 and    crg.rgd_code = 'LAFEXP'
2438 and    cr.RULE_INFORMATION_CATEGORY = 'LAFEXP'
2439 and    crg.cle_id = p_cle_id
2440 
2441 ;
2442 
2443 
2444 BEGIN
2445 
2446 
2447   IF (p_contract_id IS NULL OR p_contract_id = OKL_API.G_MISS_NUM)
2448   THEN
2449     RETURN 0;  -- error
2450   ELSE
2451     OPEN cv1 (p_vendor_site_id);
2452     FETCH cv1 INTO l_vendor_id;
2453     CLOSE cv1;
2454 
2455     ----------------------------------------------------
2456     -- FEE line
2457     ----------------------------------------------------
2458     OPEN c (p_contract_id, l_vendor_id, 'FEE');
2459     LOOP
2460 
2461 
2462       FETCH c into l_cle_amount,
2463                    l_cle_id,
2464                    l_cle_start_date;
2465 
2466       EXIT WHEN c%NOTFOUND;
2467 
2468       -- Bug# 16493240 : Start of Modifications
2469       OPEN c_new_lin_amt(p_contract_id, l_cle_id);
2470       FETCH c_new_lin_amt INTO l_new_lin_amt;
2471       IF l_new_lin_amt IS NULL THEN
2472         l_new_lin_amt := 0;
2473       END IF;
2474          l_cle_amount := l_new_lin_amt;
2475       CLOSE c_new_lin_amt;
2476       -- Bug# 16493240 : End of Modifications
2477 
2478       OPEN c_amount_per (p_contract_id, l_cle_id);
2479       FETCH c_amount_per INTO l_period_org,
2480                               l_amount_per;
2481 
2482       l_row_notfound := c_amount_per%NOTFOUND;
2483       CLOSE c_amount_per;
2484 
2485       -- if recurring records doesn't exists
2486       IF (l_row_notfound) THEN
2487 
2488         -- either fee effective date or contract effective date <= current date
2489         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN -- cklee start 10/3/2007 bug: 6128765
2490           l_amount := l_amount + l_cle_amount;
2491         END IF;
2492 
2493       ELSE
2494 
2495         OPEN c_period (p_contract_id, l_cle_id);
2496         FETCH c_period INTO l_period;
2497         CLOSE c_period;
2498 
2499         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN -- cklee start 10/3/2007 bug: 6128765
2500           l_period := 1;
2501         END IF;
2502 
2503         -- calculate only if period is positive
2504         IF (l_period > 0) THEN
2505 
2506           IF (l_period > l_period_org) THEN
2507             l_period := l_period_org;
2508           END IF;
2509           l_amount := l_amount + (l_amount_per * l_period);
2510         END IF;
2511 
2512       END IF;
2513 
2514     END LOOP;
2515     CLOSE c;
2516 
2517     -- SOLD_SERVICE line
2518     OPEN c (p_contract_id, l_vendor_id, 'SOLD_SERVICE');
2519     LOOP
2520 
2521 
2522       FETCH c into l_cle_amount,
2523                    l_cle_id,
2524                    l_cle_start_date;
2525 
2526       EXIT WHEN c%NOTFOUND;
2527 
2528       OPEN c_amount_per (p_contract_id, l_cle_id);
2529 
2530       FETCH c_amount_per INTO l_period_org,
2531                               l_amount_per;
2532       l_row_notfound := c_amount_per%NOTFOUND;
2533       CLOSE c_amount_per;
2534 
2535 
2536       -- if recurring records doesn't exists
2537       IF (l_row_notfound) THEN
2538 
2539         -- either fee effective date or contract effective date <= current date
2540         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN -- cklee start 10/3/2007 bug: 6128765
2541           l_amount := l_amount + l_cle_amount;
2542         END IF;
2543 
2544       ELSE
2545 
2546         OPEN c_period (p_contract_id, l_cle_id);
2547         FETCH c_period INTO l_period;
2548         CLOSE c_period;
2549 
2550         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN -- cklee start 10/3/2007 bug: 6128765
2551           l_period := 1;
2552         END IF;
2553 
2554         -- calculate only if period is positive
2555         IF (l_period > 0) THEN
2556 
2557           IF (l_period > l_period_org) THEN
2558             l_period := l_period_org;
2559           END IF;
2560           l_amount := l_amount + (l_amount_per * l_period);
2561         END IF;
2562 
2563       END IF;
2564 
2565     END LOOP;
2566     CLOSE c;
2567 
2568   END IF;
2569 --
2570   IF (l_amount IS NULL) THEN
2571     l_amount := 0;
2572   END IF;
2573 
2574   IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 0) THEN
2575     l_amount := 0;
2576   END IF;
2577 
2578   RETURN l_amount;
2579 
2580   EXCEPTION
2581     WHEN OTHERS THEN
2582       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2583       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2584                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2585                           p_token1        => 'OKL_SQLCODE',
2586                           p_token1_value  => SQLCODE,
2587                           p_token2        => 'OKL_SQLERRM',
2588                           p_token2_value  => SQLERRM);
2589       RETURN 0;
2590 
2591 END;
2592 ----------------------------------------------------------------------------
2593 -- used for pre-funding only
2594  FUNCTION get_chr_exp_canbe_funded_amt(
2595   p_contract_id                       IN NUMBER                 -- contract hdr
2596   ,p_due_date                         IN date  default sysdate  --shagarg added
2597   ) RETURN NUMBER
2598 IS
2599   l_amount NUMBER := 0;
2600   l_cle_amount NUMBER := 0;
2601   l_amount_per NUMBER := 0;
2602 
2603   l_cle_id NUMBER := 0;
2604   l_cle_start_date DATE;
2605   l_period NUMBER := 0;
2606   l_period_org NUMBER := 0;
2607   l_row_notfound   BOOLEAN;
2608   l_new_lin_amt    NUMBER;
2609 
2610   CURSOR c (p_contract_id  NUMBER, p_rle_code VARCHAR2)
2611   IS
2612 select nvl(cle.AMOUNT,0),
2613        cle.id,
2614        nvl(cle.start_date,k.start_date)
2615 from   OKL_K_LINES_FULL_V cle,
2616        okc_k_party_roles_b cpl,
2617        okc_line_styles_b ls,
2618        okc_k_headers_b k
2619 where  k.id = cle.dnz_chr_id
2620 and    cle.dnz_chr_id = p_contract_id
2621 and    cle.lse_id = ls.id
2622 and    ls.lty_code = p_rle_code
2623 and    cle.id = cpl.cle_id
2624 and    cpl.dnz_chr_id  = p_contract_id
2625 and    cpl.chr_id     is null
2626 and    cpl.rle_code = 'OKL_VENDOR'
2627 --and    cpl.object1_id1 = to_char(p_vendor_id)
2628 --and    cpl.object1_id2 = '#'
2629 -- Pass through check
2630 /*
2631 and not exists (select null
2632                 from   okc_rule_groups_v crg,
2633                        okc_rules_v cr
2634                 where  crg.dnz_chr_id = p_contract_id
2635                 and    crg.cle_id = cle.id -- line id for rle_code
2636                 and    crg.id = cr.rgp_id
2637                 and    crg.rgd_code = 'LAPSTH') */
2638 and not exists (select null
2639                 from   okl_party_payment_hdr phr
2640                 where  phr.dnz_chr_id = p_contract_id
2641                 and    phr.cle_id = cle.id);
2642 
2643 
2644   -- Bug 16493240 :Start of Modification
2645   -- Modifying the cursor to fetch fee related to new asset lines only
2646 CURSOR c_new_lin_amt (p_contract_id  NUMBER, p_cle_id VARCHAR2)
2647 IS
2648         SELECT SUM(NVL(kle_cov_asset.capital_amount,0)) amount
2649 	FROM   okc_k_lines_b cleb_cov_asset,
2650 	       okl_k_lines_full_v kle_cov_asset,
2651 	       okc_k_items cim_cov_asset,
2652 	       okc_k_lines_b cleb_fin,
2653 	       okc_statuses_b sts,
2654 	       okl_k_lines kle
2655 	WHERE  kle_cov_asset.id = cleb_cov_asset.id
2656 	AND    cim_cov_asset.cle_id = cleb_cov_asset.id
2657 	AND    cim_cov_asset.dnz_chr_id = cleb_cov_asset.dnz_chr_id
2658 	AND    cim_cov_asset.jtot_object1_code = 'OKX_COVASST'
2659 	AND    cleb_fin.id = cim_cov_asset.object1_id1
2660 	AND    CLEB_FIN.STS_CODE = STS.CODE
2661 	AND    STS.STE_CODE <> 'CANCELLED'
2662 	and cleb_cov_asset.dnz_chr_id = p_contract_id
2663 	and kle.id = cim_cov_asset.object1_id1
2664 	--and nvl(kle.re_lease_yn,'N') <> 'Y' -- ndani - 26-Mar-2013 : Bug# 16493240
2665         and kle_cov_asset.cle_id = p_cle_id;
2666 
2667   -- Bug 16493240 :End of Modification
2668 
2669 
2670 --
2671 -- get Number of Period
2672 --
2673 -- 1) take contract start date if cle start date is null
2674 -- 2) truncate pay period if less than 0
2675 --
2676   CURSOR c_period (p_contract_id  NUMBER, p_cle_id NUMBER)
2677   IS
2678    --cklee start 10/3/2007 bug: 6128765
2679 /*select ceil(decode(cr.object1_id1, 'A', months_between(sysdate, nvl(cle.start_date, k.start_date))/12
2680                             , 'M', months_between(sysdate, nvl(cle.start_date, k.start_date))
2681                             , 'Q', months_between(sysdate, nvl(cle.start_date, k.start_date))/3
2682                             , 'S', months_between(sysdate, nvl(cle.start_date, k.start_date))/6
2683                             , months_between(sysdate, nvl(cle.start_date, k.start_date))))*/
2684  select ceil(decode(cr.object1_id1, 'A', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/12
2685                               , 'M', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))
2686                                , 'Q', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/3
2687                                , 'S', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/6
2688                                , months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))))
2689    --cklee end 10/3/2007 bug: 6128765
2690 from   okc_rule_groups_v crg,
2691        okc_rules_v cr,
2692        OKL_K_LINES_FULL_V cle,
2693        okc_k_headers_b k
2694 where  crg.dnz_chr_id = p_contract_id
2695 and    cle.dnz_chr_id = k.id
2696 and    crg.id = cr.rgp_id
2697 and    crg.rgd_code = 'LAFEXP'
2698 
2699 and    crg.cle_id = cle.id
2700 and    cr.RULE_INFORMATION_CATEGORY = 'LAFREQ'
2701 and    cle.id = p_cle_id
2702 ;
2703 
2704 --
2705 -- get amount per period
2706 --
2707 --
2708   CURSOR c_amount_per (p_contract_id  NUMBER, p_cle_id NUMBER)
2709   IS
2710 select to_number(nvl(cr.RULE_INFORMATION1,'0'))
2711        ,to_number(nvl(cr.RULE_INFORMATION2,'0'))
2712 from   okc_rule_groups_v crg,
2713        okc_rules_v cr
2714 where  crg.dnz_chr_id = p_contract_id
2715 and    crg.id = cr.rgp_id
2716 and    crg.rgd_code = 'LAFEXP'
2717 and    cr.RULE_INFORMATION_CATEGORY = 'LAFEXP'
2718 and    crg.cle_id = p_cle_id
2719 ;
2720 
2721 
2722 BEGIN
2723 
2724   IF (p_contract_id IS NULL OR p_contract_id = OKL_API.G_MISS_NUM)
2725   THEN
2726     RETURN 0;  -- error
2727   ELSE
2728 
2729     ----------------------------------------------------
2730     -- FEE line
2731 
2732     ----------------------------------------------------
2733     OPEN c (p_contract_id, 'FEE');
2734     LOOP
2735 
2736       FETCH c into l_cle_amount,
2737                    l_cle_id,
2738                    l_cle_start_date;
2739 
2740       EXIT WHEN c%NOTFOUND;
2741 
2742 
2743       -- Bug# 16493240 : Start of Modifications
2744       OPEN c_new_lin_amt(p_contract_id, l_cle_id);
2745       FETCH c_new_lin_amt INTO l_new_lin_amt;
2746       IF l_new_lin_amt IS NULL THEN
2747         l_new_lin_amt := 0;
2748       END IF;
2749          l_cle_amount := l_new_lin_amt;
2750       CLOSE c_new_lin_amt;
2751       -- Bug# 16493240 : End of Modifications
2752 
2753 
2754 
2755       OPEN c_amount_per (p_contract_id, l_cle_id);
2756       FETCH c_amount_per INTO l_period_org,
2757                               l_amount_per;
2758 
2759       l_row_notfound := c_amount_per%NOTFOUND;
2760       CLOSE c_amount_per;
2761 
2762       -- if recurring records doesn't exists
2763       IF (l_row_notfound) THEN
2764 
2765         -- either fee effective date or contract effective date <= current date
2766         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN   --cklee end 10/3/2007 bug: 6128765
2767           l_amount := l_amount + l_cle_amount;
2768 
2769         END IF;
2770 
2771       ELSE
2772 
2773         OPEN c_period (p_contract_id, l_cle_id);
2774         FETCH c_period INTO l_period;
2775         CLOSE c_period;
2776 
2777         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN   --cklee end 10/3/2007 bug: 6128765
2778           l_period := 1;
2779         END IF;
2780 
2781         -- calculate only if period is positive
2782         IF (l_period > 0) THEN
2783 
2784           IF (l_period > l_period_org) THEN
2785             l_period := l_period_org;
2786           END IF;
2787           l_amount := l_amount + (l_amount_per * l_period);
2788         END IF;
2789 
2790 
2791       END IF;
2792 
2793     END LOOP;
2794 
2795     CLOSE c;
2796 
2797     -- SOLD_SERVICE line
2798 
2799     OPEN c (p_contract_id, 'SOLD_SERVICE');
2800     LOOP
2801 
2802       FETCH c into l_cle_amount,
2803                    l_cle_id,
2804                    l_cle_start_date;
2805 
2806       EXIT WHEN c%NOTFOUND;
2807 
2808       OPEN c_amount_per (p_contract_id, l_cle_id);
2809 
2810       FETCH c_amount_per INTO l_period_org,
2811                               l_amount_per;
2812 
2813       l_row_notfound := c_amount_per%NOTFOUND;
2814       CLOSE c_amount_per;
2815 
2816       -- if recurring records doesn't exists
2817       IF (l_row_notfound) THEN
2818 
2819 
2820         -- either fee effective date or contract effective date <= current date
2821         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN   --cklee end 10/3/2007 bug: 6128765
2822           l_amount := l_amount + l_cle_amount;
2823 
2824         END IF;
2825 
2826       ELSE
2827 
2828         OPEN c_period (p_contract_id, l_cle_id);
2829         FETCH c_period INTO l_period;
2830         CLOSE c_period;
2831 
2832         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN   --cklee end 10/3/2007 bug: 6128765
2833           l_period := 1;
2834         END IF;
2835 
2836         -- calculate only if period is positive
2837         IF (l_period > 0) THEN
2838 
2839           IF (l_period > l_period_org) THEN
2840             l_period := l_period_org;
2841 
2842           END IF;
2843           l_amount := l_amount + (l_amount_per * l_period);
2844         END IF;
2845       END IF;
2846 
2847     END LOOP;
2848     CLOSE c;
2849 
2850   END IF;
2851 
2852   IF (l_amount IS NULL) THEN
2853     l_amount := 0;
2854   END IF;
2855 
2856 
2857   IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 0) THEN
2858     l_amount := 0;
2859   END IF;
2860 
2861   RETURN l_amount;
2862 
2863   EXCEPTION
2864     WHEN OTHERS THEN
2865       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2866       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2867                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2868                           p_token1        => 'OKL_SQLCODE',
2869                           p_token1_value  => SQLCODE,
2870                           p_token2        => 'OKL_SQLERRM',
2871                           p_token2_value  => SQLERRM);
2872       RETURN 0;
2873 
2874 
2875 END;
2876 
2877   --------------------------------------------------------------------------
2878   --------------------------------------------------------------------------
2879      ----- Validate Re-lease contract for Manual Disbursement
2880      --------------------------------------------------------------------------
2881      FUNCTION validate_release_contract(
2882        p_tapv_rec                  IN tapv_rec_type
2883      ) RETURN VARCHAR2
2884      IS
2885 
2886 
2887      CURSOR c_release_k_flag(p_contract_id  NUMBER)
2888      IS
2889 
2890 
2891        SELECT nvl(rul.rule_information1, 'N') FROM
2892        okc_rules_b rul, okc_rule_groups_b rgp
2893        WHERE rul.rule_information_category='LARLES'
2894        AND rgp.id = rul.rgp_id
2895        AND rgp.rgd_code = 'LARLES'
2896        AND rgp.dnz_chr_id= p_contract_id;
2897 
2898 
2899        --Bug# 15992711 : The re-leased flag is no longer checked at contract header
2900        --                but needs to be validated at contract line
2901        --                Cursor modified to include validation at asset line level
2902        --Start Modification 120.67.12010000.8 Pratrao 5-Feb-2013
2903        /*SELECT nvl(kle.re_lease_yn,'N')
2904        FROM   OKC_K_HEADERS_B   chr
2905              ,okc_k_lines_b     cle
2906              ,okl_k_lines       kle
2907        WHERE  chr.id     = cle.chr_id
2908        and    chr.id     = cle.dnz_chr_id
2909        and    cle.id     = kle.id
2910        and    chr.id     = p_contract_id;
2911        */
2912 
2913        --Bug# 15992711 End Modification
2914 
2915        l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2916        l_release_flag   okc_rules_b.rule_information1%type;
2917 
2918 
2919      BEGIN
2920 
2921        OPEN c_release_k_flag(p_tapv_rec.khr_id);
2922        FETCH c_release_k_flag INTO l_release_flag;
2923        CLOSE c_release_k_flag;
2924 
2925        IF (l_release_flag = 'Y') THEN
2926          -- re-leased contract
2927             IF (p_tapv_rec.funding_type_code <> 'MANUAL_DISB' ) THEN
2928 
2929                OKL_API.Set_Message(p_app_name      => G_APP_NAME,
2930                              p_msg_name      => 'OKL_LLA_RELK_FUNDTYPE_CHK',
2931                              p_token1       => 'COL_NAME',
2932                              p_token1_value => p_tapv_rec.funding_type_code);
2933 
2934                RAISE G_EXCEPTION_HALT_VALIDATION;
2935            END IF;
2936        END IF;
2937        RETURN l_return_status;
2938      EXCEPTION
2939        WHEN G_EXCEPTION_HALT_VALIDATION THEN
2940          l_return_status := OKL_API.G_RET_STS_ERROR;
2941          RETURN l_return_status;
2942        WHEN OTHERS THEN
2943          l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2944          OKL_API.Set_Message(p_app_name      => G_APP_NAME,
2945                              p_msg_name      => G_UNEXPECTED_ERROR,
2946                              p_token1        => G_SQLCODE_TOKEN,
2947                              p_token1_value  => SQLCODE,
2948                              p_token2        => G_SQLERRM_TOKEN,
2949                              p_token2_value  => SQLERRM);
2950          RETURN l_return_status;
2951      END;
2952    --------------------------------------------------------------
2953 
2954   -- Validate Funding request Checklist
2955   --------------------------------------------------------------------------
2956   FUNCTION validate_funding_checklist(
2957     p_tapv_rec                  IN tapv_rec_type
2958   ) RETURN VARCHAR2
2959   IS
2960     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2961     l_req_row_found       boolean;
2962     l_list_row_not_found   boolean;
2963     l_template_row_not_found   boolean;
2964 
2965     l_dummy           number;
2966     l_chklist_sts_row_found   boolean;
2967     l_status okl_crd_fund_checklists_tpl_uv.STATUS%TYPE;
2968     l_fund_cls_tpl_exp_found boolean := false;
2969 
2970     l_credit_id okc_k_headers_b.id%TYPE;
2971 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2972     l_lease_app_found boolean := false;
2973 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2974 ---------------------------------------------------------------------------------------------------------
2975 -- 1. check credit line funding checklist template, used for existing requests w credit line that doesn't
2976 --    have checklist template setup
2977 ---------------------------------------------------------------------------------------------------------
2978 cursor c_chk_tpl (p_credit_id number) is
2979   select tpl.status
2980 from okl_crd_fund_checklists_tpl_uv tpl
2981 where tpl.khr_id = p_credit_id
2982   ;
2983 
2984 ---------------------------------------------------------------------------------------------------
2985 -- 2.
2986 -- validate if credit line contract's funding checklist template expired
2987 
2988 -- Note: assumption
2989 
2990 -- 1. Credit line exists : valiadte_creditline()
2991 -- 2. pass # 1 cursor check
2992 ---------------------------------------------------------------------------------------------------
2993 CURSOR c_fund_chklst_tpl (p_credit_id number)
2994 IS
2995   select 1
2996 from  okl_crd_fund_chklst_tpl_hdr_uv chk
2997 where TRUNC(chk.effective_to) < TRUNC(sysdate)
2998 and   chk.khr_id = p_credit_id
2999 ;
3000 
3001 
3002 ---------------------------------------------------------------------------------------------------------
3003 -- 3. check funding checklist if funding checklist has not been setup
3004 ---------------------------------------------------------------------------------------------------------
3005 CURSOR c_chklst_chk(p_req_id okl_trx_ap_invoices_b.id%type)
3006 IS
3007   select 1
3008 from okl_funding_checklists_uv chk
3009 where fund_req_id = TO_CHAR(p_req_id) -- cklee: 11/04/2004
3010 ;
3011 
3012 ---------------------------------------------------------------------------------------------------------
3013 -- 4. check checklist required items
3014 ---------------------------------------------------------------------------------------------------------
3015 
3016 CURSOR c_chklst (p_chr_id okc_k_headers_b.id%type, p_fund_req_id okl_trx_ap_invoices_b.id%type)
3017 IS
3018   select 1
3019 from  okc_rules_b rult
3020 where rult.rule_information_category = G_FUNDING_CHKLST_TPL_RULE1--'LAFCLD'
3021 and   rult.dnz_chr_id = p_chr_id
3022 and   rult.object1_id1 = p_fund_req_id
3023 and   rult.object1_id2 = '#'
3024 and   rult.RULE_INFORMATION2 = 'Y'
3025 and   (rult.RULE_INFORMATION3 <> 'Y' or rult.RULE_INFORMATION3 is null)
3026 ;
3027 
3028 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3029 ---------------------------------------------------------------------------------------------------------
3030 -- 5. check if the contract was created from a lease application
3031 ---------------------------------------------------------------------------------------------------------
3032 CURSOR c_lease_app (p_chr_id okc_k_headers_b.id%type)
3033 IS
3034 --start modified abhsaxen for performance SQLID 20562365
3035   select 1
3036 from  okc_k_headers_b chr
3037 where ORIG_SYSTEM_SOURCE_CODE = G_OKL_LEASE_APP
3038 and chr.id = p_chr_id
3039 ;
3040 --end modified abhsaxen for performance SQLID 20562365
3041 
3042   BEGIN
3043       -- sjalasut, tapv_rec.khr_id would work here as the calling procedure
3044       -- continue to populate this value.
3045       OPEN c_lease_app(p_tapv_rec.khr_id);
3046       FETCH c_lease_app INTO l_dummy;
3047       l_lease_app_found := c_lease_app%FOUND;
3048       CLOSE c_lease_app;
3049 
3050       IF NOT l_lease_app_found THEN
3051 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3052 
3053         l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
3054 
3055       ---------------------------------------------------------------------------------------------------------
3056       -- 1.1 check credit line funding checklist template, used for existing requests w credit line that doesn't
3057       --    have checklist template setup
3058       -- existing check
3059       ---------------------------------------------------------------------------------------------------------
3060 /* no need
3061       OPEN c_chk_tpl(l_credit_id);
3062       FETCH c_chk_tpl INTO l_status;
3063 
3064       l_template_row_not_found := c_chk_tpl%NOTFOUND;
3065 
3066       CLOSE c_chk_tpl;
3067 
3068 
3069       -- credit line checklist tempate doesn't exists
3070       IF (l_template_row_not_found) THEN
3071         -- Funding request checklist template not found. Please setup checklist template for associated credit line.
3072         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3073                             p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK');
3074 
3075         RAISE G_EXCEPTION_HALT_VALIDATION;
3076       END IF;
3077 */
3078 
3079       ---------------------------------------------------------------------------------------------------------
3080       -- 1.2 check credit line funding checklist template, used for existing requests w credit line that doesn't
3081       --    have checklist template setup
3082       -- status check
3083       ---------------------------------------------------------------------------------------------------------
3084 /*no need: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3085 
3086       IF (l_status IS NOT NULL and l_status <> 'ACTIVE') THEN
3087         -- Funding request checklist template status is new. Please activate Funding request checklist template
3088         -- for associated credit line.
3089         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3090                             p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK7');
3091 
3092         RAISE G_EXCEPTION_HALT_VALIDATION;
3093       END IF;
3094 */
3095         ---------------------------------------------------------------------------------------------------------
3096         -- 2. check credit line funding checklist template expiration
3097         -- 2nd place to check when user submit a request for approval
3098         ---------------------------------------------------------------------------------------------------------
3099 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3100         IF l_credit_id IS NOT NULL THEN
3101 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3102 
3103           OPEN c_fund_chklst_tpl(l_credit_id);
3104           FETCH c_fund_chklst_tpl INTO l_dummy;
3105           l_fund_cls_tpl_exp_found := c_fund_chklst_tpl%FOUND;
3106           CLOSE c_fund_chklst_tpl;
3107 
3108           -- funding checklist template expired.
3109           IF (l_fund_cls_tpl_exp_found) THEN
3110             -- Funding request checklist template expired. Please modify effective date of Funding request checklist template.
3111             OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3112                                 p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK6');
3113 
3114             RAISE G_EXCEPTION_HALT_VALIDATION;
3115           END IF;
3116 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3117         END IF;
3118       END IF;
3119 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3120 
3121 -- Fixed trx_status_code to include , 'SUBMITTED' for WF case 12-05-2003 cklee
3122 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3123 -- commented out for okl.h     IF (p_tapv_rec.trx_status_code in ('APPROVED', 'SUBMITTED')) THEN
3124 
3125         ---------------------------------------------------------------------------------------------------------
3126         -- 3. check funding checklist if funding checklist has not been setup
3127         -- Note: This is used for existing request which doesn't have checklist setup
3128         ---------------------------------------------------------------------------------------------------------
3129 /* no need
3130         OPEN c_chklst_chk(p_tapv_rec.id);
3131         FETCH c_chklst_chk INTO l_dummy;
3132         l_list_row_not_found := c_chklst_chk%NOTFOUND;
3133         CLOSE c_chklst_chk;
3134 
3135         -- checklist doesn't exists
3136         IF (l_list_row_not_found) THEN
3137           -- Funding checklist not found. Please update request and setup checklist before submit request.
3138           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3139                               p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK2');
3140 
3141           RAISE G_EXCEPTION_HALT_VALIDATION;
3142         END IF;
3143 */
3144 
3145 
3146         ---------------------------------------------------------------------------------------------------------
3147         -- 4. check checklist required items
3148         ---------------------------------------------------------------------------------------------------------
3149 /*no need for okl.h 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3150         OPEN c_chklst(p_tapv_rec.khr_id, p_tapv_rec.id);
3151         FETCH c_chklst INTO l_dummy;
3152         l_req_row_found := c_chklst%FOUND;
3153         CLOSE c_chklst;
3154 
3155         -- all required items have not met requirement
3156         IF (l_req_row_found) THEN
3157           -- Funding request has not met all checklist items. Please check off all mandatory checklist items.
3158           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3159                               p_msg_name     => 'OKL_LLA_FUND_CHKLST');
3160 
3161           RAISE G_EXCEPTION_HALT_VALIDATION;
3162         END IF;
3163       END IF;
3164 */
3165     RETURN l_return_status;
3166 
3167   EXCEPTION
3168     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3169       l_return_status := OKL_API.G_RET_STS_ERROR;
3170       RETURN l_return_status;
3171     WHEN OTHERS THEN
3172       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3173 
3174       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3175                           p_msg_name      => G_UNEXPECTED_ERROR,
3176                           p_token1        => G_SQLCODE_TOKEN,
3177                           p_token1_value  => SQLCODE,
3178                           p_token2        => G_SQLERRM_TOKEN,
3179                           p_token2_value  => SQLERRM);
3180       RETURN l_return_status;
3181   END;
3182 
3183 
3184   --------------------------------------------------------------------------
3185   ----- Validate amount if request status = 'SUBMITTED'
3186   --------------------------------------------------------------------------
3187   FUNCTION validate_trx_status_code(
3188     p_tapv_rec                  IN tapv_rec_type
3189   ) RETURN VARCHAR2
3190   IS
3191 
3192     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3193     l_amount          NUMBER := 0;
3194   BEGIN
3195 
3196     -- trx_status_code is required:
3197     IF (p_tapv_rec.trx_status_code IS NULL) OR
3198        (p_tapv_rec.trx_status_code = OKL_API.G_MISS_CHAR)
3199 
3200     THEN
3201       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3202                           p_msg_name     => G_REQUIRED_VALUE,
3203                           p_token1       => G_COL_NAME_TOKEN,
3204                           p_token1_value => 'Request Status');
3205       RAISE G_EXCEPTION_HALT_VALIDATION;
3206     END IF;
3207 
3208     RETURN l_return_status;
3209   EXCEPTION
3210     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3211       l_return_status := OKL_API.G_RET_STS_ERROR;
3212       RETURN l_return_status;
3213     WHEN OTHERS THEN
3214       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3215       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3216                           p_msg_name      => G_UNEXPECTED_ERROR,
3217                           p_token1        => G_SQLCODE_TOKEN,
3218                           p_token1_value  => SQLCODE,
3219                           p_token2        => G_SQLERRM_TOKEN,
3220                           p_token2_value  => SQLERRM);
3221       RETURN l_return_status;
3222   END;
3223 
3224   --------------------------------------------------------------------------
3225   ----- Validate payment due date
3226   --------------------------------------------------------------------------
3227   FUNCTION validate_payment_due_date(
3228 
3229     p_tapv_rec                  IN tapv_rec_type
3230   ) RETURN VARCHAR2
3231 
3232   IS
3233     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3234   BEGIN
3235 
3236 
3237     -- payment_due_date is required:
3238     IF (p_tapv_rec.date_invoiced IS NULL) OR
3239        (p_tapv_rec.date_invoiced = OKL_API.G_MISS_DATE)
3240     THEN
3241       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3242                           p_msg_name     => G_REQUIRED_VALUE,
3243                           p_token1       => G_COL_NAME_TOKEN,
3244                           p_token1_value => 'Payment due date');
3245       RAISE G_EXCEPTION_HALT_VALIDATION;
3246     END IF;
3247 
3248 /*** comment out this check
3249     -- date range check : date_entered will be default to sysdate @ UI
3250     IF (trunc(p_tapv_rec.date_invoiced) < trunc(p_tapv_rec.date_entered))
3251     THEN
3252       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3253                           p_msg_name     => 'OKL_LLA_RANGE_CHECK',
3254                           p_token1       => 'COL_NAME1',
3255                           p_token1_value => 'Payment due date',
3256                           p_token2       => 'COL_NAME2',
3257                           p_token2_value => 'Date entered');
3258 
3259       RAISE G_EXCEPTION_HALT_VALIDATION;
3260     END IF;
3261 ***/
3262     RETURN l_return_status;
3263   EXCEPTION
3264     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3265       l_return_status := OKL_API.G_RET_STS_ERROR;
3266       RETURN l_return_status;
3267     WHEN OTHERS THEN
3268       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3269       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3270                           p_msg_name      => G_UNEXPECTED_ERROR,
3271                           p_token1        => G_SQLCODE_TOKEN,
3272                           p_token1_value  => SQLCODE,
3273                           p_token2        => G_SQLERRM_TOKEN,
3274                           p_token2_value  => SQLERRM);
3275       RETURN l_return_status;
3276 
3277   END;
3278 
3279   --------------------------------------------------------------------------
3280   ----- Validate Funding Amount... when SUBMITTED, APPROVED
3281   --------------------------------------------------------------------------
3282   FUNCTION validate_header_amount(
3283     p_tapv_rec                  IN tapv_rec_type
3284   ) RETURN VARCHAR2
3285   IS
3286     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3287     l_api_version       NUMBER := 1.0;
3288     l_init_msg_list     VARCHAR2(1) := OKL_API.G_FALSE;
3289     x_return_status     VARCHAR2(1);
3290     x_msg_count         NUMBER;
3291     x_msg_data          VARCHAR2(2000);
3292     x_value             NUMBER := 0;
3293 --    l_chr_id            NUMBER;
3294 --    l_funding_type_code VARCHAR2(30);
3295     l_cur_total_amount  NUMBER := 0;
3296     l_cur_amount  NUMBER := 0;
3297     l_total_hasbeen_funded_amt_ven NUMBER := 0;
3298     l_total_hasbeen_funded_amount NUMBER := 0;
3299     l_total_canbe_funded_amount NUMBER := 0;
3300     l_total_canbe_OEC_amount NUMBER := 0;
3301     l_total_canbe_expense_amount NUMBER := 0;
3302     l_total_canbe_expense_amount_g NUMBER := 0;
3303     l_total_credit_amount NUMBER := 0;
3304     l_message_name      VARCHAR2(30);
3305     l_resuts_amount     NUMBER := 0;
3306     l_credit_id         NUMBER := 0;
3307     l_booked_count      NUMBER := 0;
3308     l_total_fund_amount NUMBER := 0;
3309     l_total_check_amount NUMBER := 0;
3310 
3311     l_invalid_fund         VARCHAR2(150) := 'X';
3312     l_prefund_amount       NUMBER := 0;
3313     l_pf_amount       NUMBER := 0;
3314     l_sr_amount       NUMBER := 0;
3315     l_amount_buffer       NUMBER := 0;
3316 
3317     l_loan_rev        NUMBER := 0;
3318     l_loan_row_found  boolean := false;
3319 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3320     l_chk_credit_id   number;
3321 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3322 
3323     l_line_amt        NUMBER := 0; --bug#5600694
3324 
3325 -- check FA line
3326 -- OKL_FUNDING_PVT.get_contract_line_funded_amt(a.CHR_ID, a.CLE_ID)
3327 -- will return 0 if user has not been funded FA line yet
3328 -- sjalasut, modified the below cursor to have khr_id be referred from
3329 -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
3330 -- as part of OKLR12B disbursements project
3331   CURSOR c_invalid_fund (p_fund_id  NUMBER)
3332   IS
3333 SELECT
3334 	   nvl(a.ASSET_NUMBER,'X') ASSET_NUMBER, a.CHR_ID, a.CLE_ID, b.ipvs_id
3335  	   FROM okl_assets_lov_uv a,
3336  	      okl_trx_ap_invoices_b b
3337  	   WHERE a.chr_id = b.khr_id
3338  	   and   b.id = p_fund_id;
3339  	 /*bug#5600694 veramach 29-Jun-2007
3340  	 commented and changed the cursor as above to improve the performance
3341  	 SELECT
3342   nvl(a.ASSET_NUMBER,'X')
3343 FROM okl_assets_lov_uv a,
3344 --START:| 13-Apr-2006  cklee -- Fixed bug#5160342                                    |
3345      okl_trx_ap_invoices_b b,
3346      OKL_TXL_AP_INV_LNS_V c
3347 WHERE a.chr_id = c.khr_id
3348 and   b.id = c.TAP_ID
3349 and   a.cle_id = c.kle_id
3350 --END:| 13-Apr-2006  cklee -- Fixed bug#5160342                                    |
3351 and   b.id = p_fund_id
3352 and   OKL_FUNDING_PVT.get_contract_line_amt(a.CHR_ID, a.CLE_ID, b.ipvs_id) > 0
3353 and   OKL_FUNDING_PVT.get_contract_line_funded_amt(a.CHR_ID, a.CLE_ID) >
3354   OKL_FUNDING_PVT.get_contract_line_amt(a.CHR_ID, a.CLE_ID, b.ipvs_id); */
3355 
3356 
3357 -- get current amount for Asset, Expense, or Supplier Retention
3358 
3359   CURSOR c_curr (p_fund_id  NUMBER)
3360   IS
3361   select nvl(sum(b.amount),0)
3362   from okl_trx_ap_invoices_b a,
3363        okl_txl_ap_inv_lns_b b
3364   where a.id = b.tap_id
3365   and b.tap_id = p_fund_id
3366   and a.trx_status_code IN ('ENTERED','SUBMITTED')
3367   ;
3368 
3369 -- get approved amount for Asset
3370   -- sjalasut, made changes to the below cursor to have khr_id be referred from
3371   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. change made
3372   -- as part of OKLR12B disbursements project.
3373   CURSOR c_tot_asset_fund (p_contract_id  NUMBER)
3374   IS
3375   select nvl(sum(b.amount),0)
3376   from okl_trx_ap_invoices_b a,
3377        okl_txl_ap_inv_lns_all_b b
3378   where a.id = b.tap_id
3379   and a.trx_status_code in ('APPROVED', 'PROCESSED')
3380   and a.funding_type_code ='ASSET'
3381   and b.amount > 0 --?
3382   and b.khr_id = p_contract_id;
3383 
3384 -- get approved amount for Expense by specific vendor
3385   -- sjalasut, made changes to the below cursor to have khr_id be referred from
3386   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. change made
3387   -- as part of OKLR12B disbursements project.
3388   CURSOR c_tot_expense_fund (p_contract_id  NUMBER, p_vendor_site_id  NUMBER)
3389   IS
3390   select nvl(sum(b.amount),0)
3391   from okl_trx_ap_invoices_b a,
3392        okl_txl_ap_inv_lns_all_b b
3393   where a.id = b.tap_id
3394   and a.trx_status_code in ('APPROVED', 'PROCESSED')
3395   and a.funding_type_code ='EXPENSE'
3396   and b.amount > 0 --?
3397   and b.khr_id = p_contract_id
3398   and exists (select null
3399               from   okx_vendor_sites_v vs
3400               where  vs.id1 = a.ipvs_id
3401               and    vs.id1 = p_vendor_site_id)
3402   ;
3403 
3404   CURSOR c_booked (p_contract_id  NUMBER)
3405   IS
3406   select count(1)
3407   from OKC_K_HEADERS_B a
3408   where id = p_contract_id
3409   and sts_code = 'BOOKED'
3410   ;
3411 
3412 -- bug 2604862
3413   CURSOR c_loan_revolving (p_contract_id  NUMBER)
3414   IS
3415   select 1 from OKL_K_HEADERS khr
3416   where khr.id = p_contract_id
3417   and khr.deal_type = 'LOAN-REVOLVING';
3418 
3419   BEGIN
3420 
3421     -- header Amount is required
3422     IF ((p_tapv_rec.funding_type_code in ('PREFUNDING','BORROWER_PAYMENT')) AND
3423         (p_tapv_rec.amount IS NULL OR
3424          p_tapv_rec.amount = OKL_API.G_MISS_NUM))
3425     THEN
3426       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3427                           p_msg_name     => G_REQUIRED_VALUE,
3428                           p_token1       => G_COL_NAME_TOKEN,
3429                           p_token1_value => 'Amount');
3430       RAISE G_EXCEPTION_HALT_VALIDATION;
3431     END IF;
3432 
3433 -- 1) get contract OEC amount w/o re-lease
3434 
3435     -- 1) get contract OEC
3436     l_total_canbe_OEC_amount := get_chr_oec_canbe_funded(p_tapv_rec.khr_id);
3437 
3438    --cklee start: bug 6128765
3439 /*    l_total_canbe_expense_amount := nvl(get_chr_exp_canbe_funded_amt(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id),0);
3440 
3441     l_total_canbe_expense_amount_g := nvl(get_chr_exp_canbe_funded_amt(p_tapv_rec.khr_id),0); -- for global check*/
3442      l_total_canbe_expense_amount := nvl(get_chr_exp_canbe_funded_amt(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id,p_tapv_rec.date_invoiced),0);
3443      l_total_canbe_expense_amount_g := nvl(get_chr_exp_canbe_funded_amt(p_tapv_rec.khr_id,p_tapv_rec.date_invoiced),0); -- for global check
3444    --cklee end: bug 6128765
3445 
3446     l_total_canbe_funded_amount := l_total_canbe_OEC_amount + l_total_canbe_expense_amount_g;
3447 
3448 -- bug 2604862
3449     OPEN c_loan_revolving(p_tapv_rec.khr_id);
3450     FETCH c_loan_revolving INTO l_loan_rev;
3451     l_loan_row_found := c_loan_revolving%FOUND;
3452     CLOSE c_loan_revolving;
3453 
3454     -- is loan revolving contract
3455     IF (l_loan_row_found) THEN
3456       -- get amount for the remaining of the attach credit line
3457       l_total_canbe_funded_amount := get_creditRem_by_chrid(p_tapv_rec.khr_id);
3458     END IF;
3459 -- bug 2604862
3460 
3461     -- get total has been funded
3462     l_total_hasbeen_funded_amount := get_total_funded(p_tapv_rec.khr_id);
3463 
3464 /*
3465 -- cklee 05/19/2004: exclude supplier retention and manual disbursement
3466     l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
3467 
3468     IF ( l_credit_id IS NULL AND
3469          p_tapv_rec.funding_type_code NOT IN ('SUPPLIER_RETENTION', 'MANUAL_DISB')) THEN
3470       -- Your request cannot be submitted. Credit line for this contract doesn't exists.
3471       l_message_name := 'OKL_LLA_FUND_CREDIT_AMT_CHK2';
3472       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3473                           p_msg_name      => l_message_name);
3474 
3475       RAISE G_EXCEPTION_HALT_VALIDATION;
3476     END IF;
3477 */
3478     l_total_credit_amount := get_creditRem_by_chrid(p_tapv_rec.khr_id);
3479 
3480     -- check booked
3481     OPEN c_booked(p_tapv_rec.khr_id);
3482     FETCH c_booked INTO l_booked_count;
3483     CLOSE c_booked;
3484 
3485     -- need to get amount from different AP table
3486     -- pre-funding current amount
3487     IF (p_tapv_rec.funding_type_code in ('PREFUNDING','BORROWER_PAYMENT')) THEN
3488 
3489       l_cur_amount := p_tapv_rec.amount; -- stores amount at header, user could changes the amount when submit
3490     ELSE
3491       -- get current amount: NOT used for pre-funding
3492       OPEN c_curr (p_tapv_rec.id);
3493       FETCH c_curr INTO l_cur_amount;
3494       CLOSE c_curr;
3495     END IF;
3496 
3497     -- check amount
3498     IF (l_cur_amount = 0 ) THEN
3499       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3500                           p_msg_name     => 'OKL_LLA_AMOUNT_CHECK');
3501       RAISE G_EXCEPTION_HALT_VALIDATION;
3502     END IF;
3503 
3504     l_resuts_amount := l_total_hasbeen_funded_amount + l_cur_amount;
3505 
3506     -- always check vs credit line total limit except adjustment request (pre-funding with negative amount)
3507     -- cklee 10/31/03 exclude supplier retention also
3508     -- cklee 05/14/04 exclude manual disb also
3509     IF ( p_tapv_rec.funding_type_code NOT IN ('PREFUNDING', 'SUPPLIER_RETENTION', 'MANUAL_DISB') OR
3510          (p_tapv_rec.funding_type_code = 'PREFUNDING' AND l_cur_amount > 0)
3511        ) THEN
3512       --Your request cannot be submitted. The total amount of this request exceeds the value of the contract credit limit.
3513       l_message_name := 'OKL_LLA_FUND_CREDIT_AMT_CHK';
3514 --      IF (l_resuts_amount > l_total_credit_amount) THEN
3515 -- fixed bug#3220634
3516       IF (l_total_credit_amount - l_cur_amount < 0) THEN
3517 
3518 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3519         l_chk_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
3520         IF l_loan_row_found OR (NOT l_loan_row_found and l_chk_credit_id is not null) THEN
3521           OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3522                           p_msg_name      => l_message_name);
3523           RAISE G_EXCEPTION_HALT_VALIDATION;
3524         END IF;
3525 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3526       END IF;
3527 
3528     -- adjustment check
3529     ELSIF (p_tapv_rec.funding_type_code = 'PREFUNDING' AND l_cur_amount < 0) THEN
3530 
3531       -- if it's revolving line of credit loan contract's adjustment
3532       IF (l_loan_row_found) THEN
3533         l_resuts_amount := l_total_hasbeen_funded_amount + l_cur_amount;
3534          --Your request cannot be submitted. The total funded amount cannot be less than 0.
3535         IF (l_resuts_amount < 0) THEN
3536           l_message_name := 'OKL_LLA_ADJUSTMENTS_AMT_CHK';
3537             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3538                           p_msg_name      => l_message_name);
3539             RAISE G_EXCEPTION_HALT_VALIDATION;
3540 
3541         END IF;
3542 
3543       ELSE
3544         --
3545         -- NOTE: adjustment is based on vendor specific. we need to calculate total has been funded amount
3546         --       by vendor
3547         --
3548         l_total_hasbeen_funded_amt_ven := get_chr_exp_hasbeen_funded_amt(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id)+
3549                                            get_chr_oec_hasbeen_funded_amt(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id)+
3550                                            get_amount_prefunded(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id)+
3551                                            -- 12-09-2003 cklee added adjustment
3552                                            get_chr_funded_adjs(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id);
3553 
3554         l_resuts_amount := l_total_hasbeen_funded_amt_ven + l_cur_amount;
3555          --Your request cannot be submitted. The total funded amount cannot be less than 0.
3556         IF (l_resuts_amount < 0) THEN
3557           l_message_name := 'OKL_LLA_ADJUSTMENTS_AMT_CHK';
3558 
3559             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3560                           p_msg_name      => l_message_name);
3561             RAISE G_EXCEPTION_HALT_VALIDATION;
3562 
3563         END IF;
3564       END IF;
3565 
3566     END IF;
3567 
3568     IF (p_tapv_rec.funding_type_code ='PREFUNDING') THEN
3569 
3570       -- check if it is NOT a loan revolving contract
3571       IF NOT l_loan_row_found THEN
3572         -- booked: check contract
3573         IF (l_booked_count > 0 AND l_cur_amount > 0 ) THEN
3574            --Your request cannot be submitted. Pre-funding requests are not allowed for contracts in Booked status.
3575             l_message_name := 'OKL_LLA_PREFUNDED_AMT_CHK2';
3576             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3577                           p_msg_name      => l_message_name);
3578             RAISE G_EXCEPTION_HALT_VALIDATION;
3579         END IF;
3580 
3581       END IF;
3582 
3583     ELSIF (p_tapv_rec.funding_type_code ='ASSET') THEN
3584 
3585       -- 1. check FA can be funded
3586       -- invalid fund amount for each FA line
3587       -- we need to show at most ONE asset number at a time
3588       /* Bug#5600694 to improve performance
3589       OPEN c_invalid_fund(p_tapv_rec.id);
3590 
3591       FETCH c_invalid_fund INTO l_invalid_fund;
3592       CLOSE c_invalid_fund;
3593       */
3594  	    FOR i IN  c_invalid_fund(p_tapv_rec.id)
3595  	    LOOP
3596 
3597  	       l_line_amt := OKL_FUNDING_PVT.get_contract_line_amt(i.CHR_ID, i.CLE_ID, i.ipvs_id);
3598 
3599  	        IF l_line_amt > 0
3600  	             AND OKL_FUNDING_PVT.get_contract_line_funded_amt(i.CHR_ID, i.CLE_ID) >
3601  	                  l_line_amt
3602  	        THEN
3603  	          l_invalid_fund := i.ASSET_NUMBER;
3604  	          EXIT;
3605  	        END IF;
3606 
3607  	    END LOOP;
3608 
3609  	       --End Bug#5600694
3610 
3611       IF (l_invalid_fund <> 'X') THEN
3612 
3613         l_message_name := 'OKL_LLA_FUND_ASSET_AMT_CHK';
3614         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3615                           p_msg_name      => l_message_name,
3616                           p_token1        => 'ASSET_NUMBER',
3617                           p_token1_value  => l_invalid_fund);
3618         RAISE G_EXCEPTION_HALT_VALIDATION;
3619       END IF;
3620 
3621       -- 2. check asset : will never happen if catch by previous check
3622       -- get current total HAS BEEN approved amount for ASSET
3623 
3624       OPEN c_tot_asset_fund (p_tapv_rec.khr_id);
3625       FETCH c_tot_asset_fund INTO l_cur_total_amount;
3626       CLOSE c_tot_asset_fund;
3627 
3628       l_resuts_amount := l_cur_total_amount + l_cur_amount;
3629       IF (l_resuts_amount > l_total_canbe_OEC_amount) THEN
3630         --Your request cannot be submitted. The total amount of this request exceeds
3631         -- the value of the contract total asset amount.
3632         l_message_name := 'OKL_LLA_FUND_TOT_ASSET_AMT_CHK';
3633         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3634                           p_msg_name      => l_message_name);
3635         RAISE G_EXCEPTION_HALT_VALIDATION;
3636       END IF;
3637 
3638     ELSIF (p_tapv_rec.funding_type_code ='EXPENSE') THEN
3639 
3640       -- 1. check expense
3641       -- get current total HAS BEEN approved amount for EXPENSE
3642       OPEN c_tot_expense_fund(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id);
3643       FETCH c_tot_expense_fund INTO l_cur_total_amount;
3644       CLOSE c_tot_expense_fund;
3645 
3646       l_resuts_amount := l_cur_total_amount + l_cur_amount;
3647       IF (l_resuts_amount > l_total_canbe_expense_amount) THEN
3648 
3649         l_message_name := 'OKL_LLA_EXPENSE_AMT_CHK';
3650         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3651                           p_msg_name      => l_message_name);
3652         RAISE G_EXCEPTION_HALT_VALIDATION;
3653       END IF;
3654 
3655     END IF;
3656 
3657     -- check for any kind of requests
3658     -- check total can be funded
3659     l_resuts_amount := l_total_hasbeen_funded_amount + l_cur_amount;
3660 
3661     -- exclude prefunding and supplier retention
3662     -- exclude manual disb
3663     IF (p_tapv_rec.funding_type_code NOT IN ('PREFUNDING', 'SUPPLIER_RETENTION', 'MANUAL_DISB')) THEN
3664 
3665       IF (l_resuts_amount > l_total_canbe_funded_amount) THEN
3666 
3667         l_message_name := 'OKL_LLA_FUNDED_AMT_CHK';
3668         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3669                           p_msg_name      => l_message_name);
3670         RAISE G_EXCEPTION_HALT_VALIDATION;
3671       END IF;
3672     END IF;
3673 
3674     RETURN l_return_status;
3675   EXCEPTION
3676     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3677       l_return_status := OKL_API.G_RET_STS_ERROR;
3678       RETURN l_return_status;
3679     WHEN OTHERS THEN
3680       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3681       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3682                           p_msg_name      => G_UNEXPECTED_ERROR,
3683                           p_token1        => G_SQLCODE_TOKEN,
3684                           p_token1_value  => SQLCODE,
3685                           p_token2        => G_SQLERRM_TOKEN,
3686                           p_token2_value  => SQLERRM);
3687       RETURN l_return_status;
3688   END;
3689 
3690   --------------------------------------------------------------------------
3691 -- Revolving line of credit contract allows adjustment funding request.
3692 -- Funding module implement adjustment request by Pre-funding type with
3693 -- negative request amount
3694 
3695   --------------------------------------------------------------------------
3696 
3697   FUNCTION validate_header_amount_for_RL(
3698     p_tapv_rec                  IN tapv_rec_type
3699   ) RETURN VARCHAR2
3700   IS
3701     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3702     l_loan_rev        NUMBER := 0;
3703     l_loan_row_found  boolean := false;
3704 
3705 -- bug 2604862
3706   CURSOR c_loan_revolving (p_contract_id  NUMBER)
3707 
3708   IS
3709   select 1 from OKL_K_HEADERS khr
3710   where khr.id = p_contract_id
3711   and khr.deal_type = 'LOAN-REVOLVING';
3712 
3713   BEGIN
3714 
3715 -- bug 2604862
3716     OPEN c_loan_revolving(p_tapv_rec.khr_id);
3717     FETCH c_loan_revolving INTO l_loan_rev;
3718     l_loan_row_found := c_loan_revolving%FOUND;
3719     CLOSE c_loan_revolving;
3720 
3721     -- is loan revolving contract
3722     IF (l_loan_row_found) THEN
3723       IF (p_tapv_rec.funding_type_code = 'PREFUNDING' AND nvl(p_tapv_rec.amount,0) >= 0 ) THEN
3724         --Please enter negative amount for your adjustment. Revolving line of credit loan contract are not allowed for pre-funding request with positive amount.
3725         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3726                           p_msg_name      => 'OKL_LLA_REVLOAN_ADJ_AMT_CHK');
3727         RAISE G_EXCEPTION_HALT_VALIDATION;
3728 
3729       END IF;
3730 
3731       IF (p_tapv_rec.funding_type_code = 'BORROWER_PAYMENT' AND nvl(p_tapv_rec.amount,0) < 0 ) THEN
3732         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3733                           p_msg_name     => 'OKL_LLA_POSITIVE_AMOUNT_ONLY',
3734                           p_token1       => 'COL_NAME',
3735                           p_token1_value => 'Amount');
3736 
3737         RAISE G_EXCEPTION_HALT_VALIDATION;
3738       END IF;
3739 
3740     END IF;
3741 -- bug 2604862
3742 
3743 
3744     RETURN l_return_status;
3745   EXCEPTION
3746     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3747       l_return_status := OKL_API.G_RET_STS_ERROR;
3748 
3749       RETURN l_return_status;
3750     WHEN OTHERS THEN
3751       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3752       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3753                           p_msg_name      => G_UNEXPECTED_ERROR,
3754                           p_token1        => G_SQLCODE_TOKEN,
3755                           p_token1_value  => SQLCODE,
3756                           p_token2        => G_SQLERRM_TOKEN,
3757                           p_token2_value  => SQLERRM);
3758       RETURN l_return_status;
3759   END;
3760 
3761   --------------------------------------------------------------------------
3762   ----- Populate additional attributes for BPD
3763   --------------------------------------------------------------------------
3764   FUNCTION populate_more_attrs(
3765     p_tapv_rec                  IN OUT NOCOPY tapv_rec_type
3766   ) RETURN VARCHAR2
3767   IS
3768     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3769 --    l_set_of_books_id NUMBER;
3770     l_terms_id NUMBER;
3771     l_application_id NUMBER;
3772     l_pay_group_lookup_code PO_VENDOR_SITES_ALL.PAY_GROUP_LOOKUP_CODE%TYPE;
3773     l_vendor_id NUMBER;
3774 
3775   CURSOR c_vendor(p_vendor_site_id NUMBER)
3776   IS
3777   --start modified abhsaxen for performance SQLID 20562381
3778   select vs.vendor_id
3779   from   ap_supplier_sites vs
3780   where vs.vendor_site_id = p_vendor_site_id
3781   ;
3782   --end modified abhsaxen for performance SQLID 20562381
3783 
3784   CURSOR c_app
3785   IS
3786   select a.application_id
3787   from FND_APPLICATION a
3788   where APPLICATION_SHORT_NAME = 'OKL'
3789   ;
3790 
3791 /*
3792   CURSOR c_set_of_books(p_org_id  NUMBER)
3793   IS
3794   select to_number(a.set_of_books_id)
3795   from HR_OPERATING_UNITS a
3796   where ORGANIZATION_ID = p_org_id
3797   ;
3798 */
3799 
3800   CURSOR c_vendor_sites(p_vendor_site_id  NUMBER)
3801   IS
3802   select a.TERMS_ID, a.PAY_GROUP_LOOKUP_CODE
3803   from PO_VENDOR_SITES_ALL a
3804   where vendor_site_id = p_vendor_site_id
3805   ;
3806 
3807     -- select apps.FND_DOC_SEQ_885_S.nextval from dual;
3808 
3809     l_document_category VARCHAR2(100):= 'OKL Lease Pay Invoices';--'OKL Lease Receipt Invoices';
3810     l_okl_application_id number(3) := 540;
3811 
3812     lX_dbseqnm           VARCHAR2(2000):= '';
3813     lX_dbseqid           NUMBER(38):= NULL;
3814 
3815   BEGIN
3816 
3817 /*
3818 -- 1. SET_OF_BOOKS_ID
3819     OPEN c_set_of_books(p_tapv_rec.org_id);
3820     FETCH c_set_of_books INTO l_set_of_books_id;
3821     CLOSE c_set_of_books;
3822 */
3823 
3824   p_tapv_rec.SET_OF_BOOKS_ID := OKL_ACCOUNTING_UTIL.get_set_of_books_id;--l_set_of_books_id;
3825 -- 2. IPPT_ID
3826   -- cklee 05/04/2004
3827     IF (p_tapv_rec.IPPT_ID IS NULL or
3828         p_tapv_rec.IPPT_ID = OKL_API.G_MISS_NUM) THEN
3829 
3830       OPEN c_vendor_sites(p_tapv_rec.ipvs_id);
3831       FETCH c_vendor_sites INTO l_terms_id, l_pay_group_lookup_code;
3832       CLOSE c_vendor_sites;
3833 
3834       p_tapv_rec.IPPT_ID := l_terms_id;
3835 
3836     END IF;
3837 
3838 -- 3. INVOICE_NUMBER
3839 
3840     OPEN c_app;
3841 
3842     FETCH c_app INTO l_application_id;
3843     CLOSE c_app;
3844 
3845     l_okl_application_id := nvl(l_application_id,540);
3846 --
3847 -- display specific application error if 'OKL Lease Pay Invoices' has not been setup or setup incorrectly
3848 --
3849     BEGIN
3850       p_tapv_rec.invoice_number := fnd_seqnum.get_next_sequence
3851                          (appid      =>  l_okl_application_id,
3852                          cat_code    =>  l_document_category,
3853                          sobid       =>  OKL_ACCOUNTING_UTIL.get_set_of_books_id,--l_set_of_books_id,
3854                          met_code    =>  'A',
3855                          trx_date    =>  SYSDATE,
3856                          dbseqnm     =>  lx_dbseqnm,
3857                          dbseqid     =>  lx_dbseqid);
3858     EXCEPTION
3859       WHEN OTHERS THEN
3860         IF SQLCODE = 100 THEN
3861           OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3862                               p_msg_name      => 'OKL_PAY_INV_SEQ_CHECK');
3863           RAISE G_EXCEPTION_HALT_VALIDATION;
3864         END IF;
3865     END;
3866 
3867 -- cklee set vendor_invoice_numner if it's NULL
3868     IF (p_tapv_rec.vendor_invoice_number IS NULL ) THEN
3869       p_tapv_rec.vendor_invoice_number := p_tapv_rec.invoice_number;
3870     END IF;
3871 
3872 -- 4. NETTABLE_YN
3873   p_tapv_rec.NETTABLE_YN := 'N';
3874 
3875 -- 5. PAY_GROUP_LOOKUP_CODE
3876   -- cklee 05/04/2004
3877     IF (p_tapv_rec.PAY_GROUP_LOOKUP_CODE IS NULL or
3878         p_tapv_rec.PAY_GROUP_LOOKUP_CODE = OKL_API.G_MISS_CHAR) THEN
3879 
3880 -- fixed PAY_GROUP_LOOKUP_CODE default data missing issues
3881       OPEN c_vendor_sites(p_tapv_rec.ipvs_id);
3882       FETCH c_vendor_sites INTO l_terms_id, l_pay_group_lookup_code;
3883       CLOSE c_vendor_sites;
3884 
3885       p_tapv_rec.PAY_GROUP_LOOKUP_CODE := l_pay_group_lookup_code;
3886 
3887     END IF;
3888 
3889 -- 6. vednor id
3890     OPEN c_vendor(p_tapv_rec.ipvs_id);
3891     FETCH c_vendor INTO l_vendor_id;
3892     CLOSE c_vendor;
3893 
3894   p_tapv_rec.VENDOR_ID := l_vendor_id;
3895 
3896 -- 7. invoice_type
3897 -- cklee 05/04/2004
3898 
3899    IF (p_tapv_rec.INVOICE_TYPE is null or
3900        p_tapv_rec.INVOICE_TYPE = OKL_API.G_MISS_CHAR) THEN
3901 
3902      p_tapv_rec.INVOICE_TYPE := G_STANDARD;
3903 
3904    END IF;
3905 --start:| 06-Aug-08  cklee Fixed bug: 6860777                                        |
3906 /* system shall not convert wrong data to correct data, instead, display error and request user
3907 to fix before proceed.
3908     -- 8. If invoice type is G_STANDARD then invoice amount is positive
3909     --    If invoice type is G_CREDIT then the invoice amount is negative.
3910     --    sjalasut, made changes to incorporate the business rule as part
3911     --    of OKLR12B Disbursements Project
3912     IF((p_tapv_rec.INVOICE_TYPE = G_STANDARD AND p_tapv_rec.AMOUNT < 0)
3913        OR(p_tapv_rec.INVOICE_TYPE = G_CREDIT AND p_tapv_rec.AMOUNT > 0))THEN
3914       p_tapv_rec.AMOUNT := ((p_tapv_rec.AMOUNT) * (-1));
3915     END IF;
3916 */
3917 --end:| 06-Aug-08  cklee Fixed bug: 6860777                                        |
3918 
3919     RETURN l_return_status;
3920   EXCEPTION
3921 
3922     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3923 
3924       l_return_status := OKL_API.G_RET_STS_ERROR;
3925 
3926 
3927       RETURN l_return_status;
3928     WHEN OTHERS THEN
3929       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3930 
3931       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3932                           p_msg_name      => G_UNEXPECTED_ERROR,
3933                           p_token1        => G_SQLCODE_TOKEN,
3934                           p_token1_value  => SQLCODE,
3935                           p_token2        => G_SQLERRM_TOKEN,
3936                           p_token2_value  => SQLERRM);
3937       RETURN l_return_status;
3938   END;
3939 
3940   --------------------------------------------------------------------------
3941   ----- Validate vendor site ID
3942   --------------------------------------------------------------------------
3943   FUNCTION validate_chr_status(
3944     p_chr_id                  IN NUMBER
3945     --Bug# 10156343
3946    ,p_funding_type_code       IN VARCHAR2
3947   ) RETURN VARCHAR2
3948 
3949   IS
3950     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3951     l_status          OKC_STATUSES_V.meaning%TYPE;
3952 
3953   CURSOR c_sts (p_contract_id  NUMBER)
3954   IS
3955 select ste.meaning
3956 from   OKC_STATUSES_V ste,
3957        okc_k_headers_b chr
3958 where  ste.code = chr.sts_code
3959 and    chr.id = p_chr_id
3960 ;
3961 
3962   BEGIN
3963 
3964     --Bug# 10156343
3965     IF (okl_funding_pvt.is_chr_fundable_status(p_chr_id, p_funding_type_code) = 0) THEN
3966 
3967       OPEN c_sts(p_chr_id);
3968       FETCH c_sts INTO l_status;
3969       CLOSE c_sts;
3970 
3971       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3972                           p_msg_name     => 'OKL_LLA_INVALID_FUNDED_REQUEST',
3973                           p_token1       => 'CHR_STATUS',
3974                           p_token1_value => l_status);
3975       RAISE G_EXCEPTION_HALT_VALIDATION;
3976     END IF;
3977 
3978     RETURN l_return_status;
3979   EXCEPTION
3980     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3981       l_return_status := OKL_API.G_RET_STS_ERROR;
3982       RETURN l_return_status;
3983     WHEN OTHERS THEN
3984 
3985       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3986       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3987                           p_msg_name      => G_UNEXPECTED_ERROR,
3988                           p_token1        => G_SQLCODE_TOKEN,
3989                           p_token1_value  => SQLCODE,
3990                           p_token2        => G_SQLERRM_TOKEN,
3991                           p_token2_value  => SQLERRM);
3992       RETURN l_return_status;
3993   END;
3994 
3995   --------------------------------------------------------------------------
3996   ----- Validate line of credit attach to funded contract
3997   --------------------------------------------------------------------------
3998   FUNCTION validate_creditline(
3999     p_tapv_rec                  IN tapv_rec_type
4000  ) RETURN VARCHAR2
4001 
4002 
4003 IS
4004 
4005     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4006 
4007     l_REVOLVING_CREDIT_YN  OKL_K_HEADERS.REVOLVING_CREDIT_YN%TYPE;
4008     l_END_DATE  OKC_K_HEADERS_B.END_DATE%TYPE;
4009 
4010     l_DEAL_TYPE  OKL_K_HEADERS.DEAL_TYPE%TYPE;
4011     l_creditline_row_found  boolean := false;
4012     l_credit_id okc_k_headers_b.id%TYPE;
4013 
4014   CURSOR c_contract (p_contract_id  NUMBER)
4015   IS
4016   select a.DEAL_TYPE
4017   from   OKL_K_HEADERS a
4018   where  a.id = p_contract_id
4019   ;
4020 
4021   CURSOR c_credit (p_credit_id  NUMBER)
4022   IS
4023   select khr.REVOLVING_CREDIT_YN,
4024          NVL(chr.END_DATE, SYSDATE)
4025   from   okl_k_headers khr,
4026          okc_k_headers_b chr
4027   where  khr.id = chr.id
4028   and    chr.id = p_credit_id
4029   ;
4030 
4031 
4032 begin
4033 
4034     -- 1) get deal type
4035     OPEN c_contract(p_tapv_rec.khr_id);
4036     FETCH c_contract INTO l_DEAL_TYPE;
4037     CLOSE c_contract;
4038 
4039     -- 2) get revolving flag
4040     l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
4041 
4042     OPEN c_credit(l_credit_id);
4043     FETCH c_credit INTO l_REVOLVING_CREDIT_YN,
4044                           l_END_DATE;
4045     l_creditline_row_found := c_credit%FOUND;
4046     CLOSE c_credit;
4047 
4048     IF (l_creditline_row_found) THEN
4049 
4050       IF ((l_DEAL_TYPE = 'LOAN-REVOLVING' AND l_REVOLVING_CREDIT_YN <> 'Y')
4051           OR
4052 
4053           (l_DEAL_TYPE <> 'LOAN-REVOLVING' AND l_REVOLVING_CREDIT_YN = 'Y')) THEN
4054 
4055            --Either Revolving line of credit attach to a normal contract (book classification is not LOAN-REVOLVING)
4056            -- or non-revolving line of credit attach to LOAN-REVOLVING contract.
4057            -- Invalid credit line attach to funding request contract.
4058             OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4059                             p_msg_name     => 'OKL_LLA_CREDITLINE_CHECK');
4060 
4061             RAISE G_EXCEPTION_HALT_VALIDATION;
4062 
4063       END IF;
4064 
4065       --Bug# 10144381: Do not check credit line expiry for Manual Disbursements
4066       IF (NVL(p_tapv_rec.funding_type_code,'XXX') <> G_MANUAL_DISB) THEN
4067         IF (trunc(l_END_DATE) < trunc(SYSDATE)) THEN
4068             OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4069                             p_msg_name     => 'OKL_LLA_CREDITLINE_EXPIRED');
4070 
4071             RAISE G_EXCEPTION_HALT_VALIDATION;
4072 
4073         END IF;
4074       END IF;
4075 
4076     ELSE -- creditline not found
4077 
4078 --      IF (p_tapv_rec.funding_type_code NOT IN (G_SUPPLIER_RETENTION_TYPE_CODE,G_MANUAL_DISB)) THEN
4079 --        -- There is no credit line for funding request contract.
4080 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring
4081 -- Credit Line is not required after okl.h except loan-revolving contract
4082 --
4083       IF l_DEAL_TYPE = 'LOAN-REVOLVING' THEN
4084         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4085                             p_msg_name     => 'OKL_LLA_CREDITLINE_NOTFOUND');
4086 
4087         RAISE G_EXCEPTION_HALT_VALIDATION;
4088      END IF;
4089 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring
4090 
4091     END IF;
4092 
4093 
4094 
4095     RETURN l_return_status;
4096   EXCEPTION
4097 
4098     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4099       l_return_status := OKL_API.G_RET_STS_ERROR;
4100       RETURN l_return_status;
4101     WHEN OTHERS THEN
4102       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4103       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4104                           p_msg_name      => G_UNEXPECTED_ERROR,
4105                           p_token1        => G_SQLCODE_TOKEN,
4106                           p_token1_value  => SQLCODE,
4107                           p_token2        => G_SQLERRM_TOKEN,
4108                           p_token2_value  => SQLERRM);
4109       RETURN l_return_status;
4110   END;
4111 
4112   --------------------------------------------------------------------------
4113   ----- Validate Funding (Invoice) Number...
4114   --------------------------------------------------------------------------
4115   FUNCTION validate_vendor_invoice_number(
4116     p_tapv_rec                  IN tapv_rec_type
4117   ) RETURN VARCHAR2
4118   IS
4119     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4120     l_dummy           VARCHAR2(1) := OKL_API.G_TRUE;
4121   BEGIN
4122     -- Invoice Number is required:
4123     --   TO DO: When in 'C'reate mode - allow user to omit invoice
4124     --          (funding request) number and generate one automatically,
4125     --          assuring that the invoice number and vendor id
4126     --          combination is unique in OKL_TRX_AP_INVOICES_V (OKL) and
4127     --          in the AP_INVOICES_ALL (AP).
4128 
4129     IF (p_tapv_rec.vendor_invoice_number IS NULL) OR
4130        (p_tapv_rec.vendor_invoice_number = OKL_API.G_MISS_CHAR)
4131     THEN
4132       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4133                           p_msg_name     => G_REQUIRED_VALUE,
4134                           p_token1       => G_COL_NAME_TOKEN,
4135                           p_token1_value => 'Request Number');
4136       RAISE G_EXCEPTION_HALT_VALIDATION;
4137     END IF;
4138 
4139 -- due to the external interface table limit to 30 chars
4140     IF (length(p_tapv_rec.vendor_invoice_number) > 30)
4141     THEN
4142       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4143                           p_msg_name     => 'OKL_LLA_EXCEED_MAXIMUM_LENGTH',
4144                           p_token1       => 'MAX_CHARS',
4145                           p_token1_value => 'thirty',
4146                           p_token2       => 'COL_NAME',
4147                           p_token2_value => 'Request Number');
4148 
4149       RAISE G_EXCEPTION_HALT_VALIDATION;
4150     END IF;
4151 
4152     RETURN l_return_status;
4153   EXCEPTION
4154     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4155       l_return_status := OKL_API.G_RET_STS_ERROR;
4156       RETURN l_return_status;
4157     WHEN OTHERS THEN
4158       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4159       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4160                           p_msg_name      => G_UNEXPECTED_ERROR,
4161                           p_token1        => G_SQLCODE_TOKEN,
4162                           p_token1_value  => SQLCODE,
4163                           p_token2        => G_SQLERRM_TOKEN,
4164                           p_token2_value  => SQLERRM);
4165       RETURN l_return_status;
4166   END;
4167 
4168   --------------------------------------------------------------------------
4169   ----- Validate vendor site ID
4170   --------------------------------------------------------------------------
4171   FUNCTION validate_ipvs_id(
4172     p_tapv_rec                  IN tapv_rec_type
4173   ) RETURN VARCHAR2
4174   IS
4175     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4176     l_dummy           VARCHAR2(1) := OKL_API.G_TRUE;
4177     l_dum number;
4178     l_rl_found boolean;
4179     l_lv_found boolean;
4180 
4181 cursor c_rl(p_khr_id number) is
4182   select 1
4183 from OKL_K_HEADERS khr
4184 where khr.id = p_khr_id
4185 and   khr.deal_type = 'LOAN-REVOLVING';
4186 
4187 cursor c_lv(p_khr_id number) is
4188   select 1
4189 from okl_fund_vendor_sites_uv vs
4190 where vs.dnz_chr_id = p_khr_id;
4191 
4192 
4193   BEGIN
4194     IF (p_tapv_rec.ipvs_id IS NULL) OR
4195        (p_tapv_rec.ipvs_id = OKL_API.G_MISS_NUM)
4196     THEN
4197 
4198 -- 10-10-2003 cklee fixed bug# 3159723
4199       open c_rl(p_tapv_rec.khr_id);
4200       fetch c_rl into l_dum;
4201       l_rl_found := c_rl%FOUND;
4202       close c_rl;
4203 
4204       IF (l_rl_found) THEN
4205 
4206         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4207                             p_msg_name     => 'OKL_LLA_LESSEE_AS_VENDOR_CHK');
4208       ELSE
4209 
4210         open c_lv(p_tapv_rec.khr_id);
4211         fetch c_lv into l_dum;
4212         l_lv_found := c_lv%FOUND;
4213         close c_lv;
4214 
4215         IF (l_lv_found) THEN
4216 
4217           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4218                             p_msg_name     => G_REQUIRED_VALUE,
4219                             p_token1       => G_COL_NAME_TOKEN,
4220                             p_token1_value => 'Vendor Site');
4221         ELSE
4222 
4223           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4224                               p_msg_name     => 'OKL_LLA_FUNDING_VENDOR_CHK');
4225 
4226         END IF;
4227       END IF;
4228 
4229 
4230       RAISE G_EXCEPTION_HALT_VALIDATION;
4231     END IF;
4232 
4233     RETURN l_return_status;
4234   EXCEPTION
4235     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4236       l_return_status := OKL_API.G_RET_STS_ERROR;
4237 
4238       RETURN l_return_status;
4239     WHEN OTHERS THEN
4240       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4241       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4242                           p_msg_name      => G_UNEXPECTED_ERROR,
4243                           p_token1        => G_SQLCODE_TOKEN,
4244                           p_token1_value  => SQLCODE,
4245                           p_token2        => G_SQLERRM_TOKEN,
4246                           p_token2_value  => SQLERRM);
4247       RETURN l_return_status;
4248   END;
4249 
4250   --------------------------------------------------------------------------
4251   ----- Validate Funding Type...
4252   --------------------------------------------------------------------------
4253   FUNCTION validate_funding_type(
4254     p_tapv_rec                  IN tapv_rec_type
4255    ,p_mode                            IN VARCHAR2 -- 'C'reate,'U'pdate,'D'elete
4256   ) RETURN VARCHAR2
4257   IS
4258     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4259     l_loan_rev NUMBER := 0;
4260     l_loan_row_found  boolean := false;
4261     l_prefunding_eligible_yn okl_k_headers.PREFUNDING_ELIGIBLE_YN%type;
4262     l_trx_status_code okl_trx_ap_invoices_b.trx_status_code%type;
4263     l_reverse_row_notfound  boolean := false;
4264     l_dummy number;
4265 
4266   CURSOR c_prefund (p_contract_id  NUMBER)
4267   IS
4268   select nvl(khr.PREFUNDING_ELIGIBLE_YN, 'N')
4269   from   OKL_K_HEADERS khr
4270   where  khr.id = p_contract_id
4271 ;
4272 
4273   CURSOR c_curr_trx_sts (p_req_id  NUMBER)
4274   IS
4275   select trx_status_code
4276   from   OKL_TRX_AP_INVOICES_B
4277   where  id = p_req_id
4278 ;
4279 
4280 
4281 -- bug 2604862
4282   CURSOR c_loan_revolving (p_contract_id  NUMBER)
4283   IS
4284   select 1 from OKL_K_HEADERS khr
4285   where khr.id = p_contract_id
4286   and khr.deal_type = 'LOAN-REVOLVING';
4287 
4288 -- cklee 09-24-03
4289   -- sjalasut, modified the below cursor to have khr_id referred from
4290   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b.
4291   -- also not using okl_cnsld_ap_invoices_all as this cursor only checks
4292   -- for a pre-funding request.
4293   Cursor c_reverse_chk(p_contract_id number)
4294   is
4295   select 1
4296   from okl_trx_ap_invoices_b a
4297       ,okl_txl_ap_inv_lns_all_b b
4298   where a.id = b.tap_id
4299   and a.vendor_invoice_number = a.invoice_number
4300   and b.khr_id = p_contract_id;
4301 
4302   BEGIN
4303 
4304 -- bug 2604862
4305     OPEN c_loan_revolving(p_tapv_rec.khr_id);
4306     FETCH c_loan_revolving INTO l_loan_rev;
4307     l_loan_row_found := c_loan_revolving%FOUND;
4308     CLOSE c_loan_revolving;
4309 
4310     -- is loan revolving contract
4311     IF (l_loan_row_found) THEN
4312       IF (p_tapv_rec.funding_type_code NOT IN ('PREFUNDING','BORROWER_PAYMENT', 'MANUAL_DISB') ) THEN
4313 
4314         --Revolving line of credit loan contract are not allowed for TOKEN funding type.
4315         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4316                           p_msg_name      => 'OKL_LLA_REVLOAN_FUNDTYPE_CHK',
4317                           p_token1       => 'COL_NAME',
4318                           p_token1_value => p_tapv_rec.funding_type_code);
4319 
4320         RAISE G_EXCEPTION_HALT_VALIDATION;
4321 
4322       END IF;
4323 
4324 
4325     ELSE -- is not revolvong line of credit loan contract
4326       IF (p_tapv_rec.funding_type_code IN ('BORROWER_PAYMENT') ) THEN
4327 
4328         --Borrower payment funding type is allow for revolving line of credit loan contract only.
4329         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4330                           p_msg_name      => 'OKL_LLA_FUNDING_TYPE_CHK');
4331         RAISE G_EXCEPTION_HALT_VALIDATION;
4332 
4333       END IF;
4334 
4335     END IF;
4336 -- bug 2604862
4337 
4338     -- funding_type_code is required
4339     IF (p_tapv_rec.funding_type_code IS NULL) OR
4340        (p_tapv_rec.funding_type_code = OKL_API.G_MISS_CHAR)
4341     THEN
4342       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4343                           p_msg_name     => G_REQUIRED_VALUE,
4344                           p_token1       => G_COL_NAME_TOKEN,
4345                           p_token1_value => 'Funding Type');
4346       RAISE G_EXCEPTION_HALT_VALIDATION;
4347     END IF;
4348 
4349 
4350     IF (p_mode = 'U') THEN
4351 
4352       -- get current req status
4353       OPEN c_curr_trx_sts(p_tapv_rec.id);
4354       FETCH c_curr_trx_sts INTO l_trx_status_code;
4355       CLOSE c_curr_trx_sts;
4356 
4357 
4358       -- check when submit for approval
4359       IF (l_trx_status_code = 'ENTERED' AND
4360           p_tapv_rec.trx_status_code in ('SUBMITTED','APPROVED')) THEN
4361 
4362         -- prefunding eligible flag check
4363         OPEN c_prefund(p_tapv_rec.khr_id);
4364         FETCH c_prefund INTO l_prefunding_eligible_yn;
4365         CLOSE c_prefund;
4366 
4367         -- cklee 09-25-2003 added p_tapv_rec.amount > 0
4368         IF ( p_tapv_rec.funding_type_code = 'PREFUNDING' AND p_tapv_rec.amount > 0 AND
4369              l_prefunding_eligible_yn <> 'Y') THEN
4370 
4371           OPEN c_reverse_chk(p_tapv_rec.khr_id);
4372           FETCH c_reverse_chk INTO l_dummy;
4373           l_reverse_row_notfound := c_reverse_chk%NOTFOUND;
4374           CLOSE c_reverse_chk;
4375 
4376           -- CKLEE 02-24-2003 :internal request will have the same value for these 2 columns
4377           IF (l_reverse_row_notfound) THEN
4378             -- You are not allowed to submit pre-funding request if Eligible For Pre-Funding
4379             -- has not been set for this contract.
4380             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4381                               p_msg_name      => 'OKL_LLA_PREFUND_ELIGIBLE_CHK');
4382             RAISE G_EXCEPTION_HALT_VALIDATION;
4383           END IF;
4384 
4385         END IF;
4386       END IF;
4387     END IF;
4388 
4389     RETURN l_return_status;
4390   EXCEPTION
4391     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4392       l_return_status := OKL_API.G_RET_STS_ERROR;
4393       RETURN l_return_status;
4394     WHEN OTHERS THEN
4395       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4396       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4397                           p_msg_name      => G_UNEXPECTED_ERROR,
4398                           p_token1        => G_SQLCODE_TOKEN,
4399                           p_token1_value  => SQLCODE,
4400                           p_token2        => G_SQLERRM_TOKEN,
4401                           p_token2_value  => SQLERRM);
4402 
4403       RETURN l_return_status;
4404 
4405 
4406   END;
4407 
4408   --------------------------------------------------------------------------
4409   ----- Validate Payment Method...
4410   --------------------------------------------------------------------------
4411   FUNCTION validate_payment_method(
4412     p_tapv_rec                  IN tapv_rec_type
4413   ) RETURN VARCHAR2
4414   IS
4415     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4416   BEGIN
4417     -- payment_method_code is required
4418     IF (p_tapv_rec.payment_method_code IS NULL) OR
4419        (p_tapv_rec.payment_method_code = OKL_API.G_MISS_CHAR)
4420     THEN
4421       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4422                           p_msg_name     => G_REQUIRED_VALUE,
4423                           p_token1       => G_COL_NAME_TOKEN,
4424                           p_token1_value => 'Payment Method');
4425       RAISE G_EXCEPTION_HALT_VALIDATION;
4426     END IF;
4427     RETURN l_return_status;
4428   EXCEPTION
4429     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4430       l_return_status := OKL_API.G_RET_STS_ERROR;
4431       RETURN l_return_status;
4432     WHEN OTHERS THEN
4433       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4434       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4435                           p_msg_name      => G_UNEXPECTED_ERROR,
4436                           p_token1        => G_SQLCODE_TOKEN,
4437                           p_token1_value  => SQLCODE,
4438                           p_token2        => G_SQLERRM_TOKEN,
4439                           p_token2_value  => SQLERRM);
4440       RETURN l_return_status;
4441   END;
4442 
4443   -- cklee 05/04/2004
4444   --------------------------------------------------------------------------
4445   ----- Validate invoice type..
4446   --------------------------------------------------------------------------
4447   FUNCTION validate_invoice_type(
4448     p_tapv_rec                  IN tapv_rec_type
4449   ) RETURN VARCHAR2
4450   IS
4451     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4452     l_funding_type varchar2(80);
4453 
4454   cursor c_funding_type (p_funding_type_code varchar2)
4455     is
4456   select lok.meaning
4457   from fnd_lookups lok
4458   where lok.lookup_type = 'OKL_FUNDING_TYPE'
4459   and lok.lookup_code = p_funding_type_code
4460   ;
4461 
4462   BEGIN
4463 
4464     -- check if value exists
4465     IF (p_tapv_rec.invoice_type IS NOT NULL) AND
4466        (p_tapv_rec.invoice_type <> OKL_API.G_MISS_CHAR)
4467     THEN
4468 
4469 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4470 /*
4471       IF (p_tapv_rec.funding_type_code = G_SUPPLIER_RETENTION_TYPE_CODE and
4472           p_tapv_rec.invoice_type <> G_CREDIT) OR
4473          (p_tapv_rec.funding_type_code NOT IN (G_SUPPLIER_RETENTION_TYPE_CODE, G_MANUAL_DISB) and
4474           p_tapv_rec.invoice_type = G_CREDIT) THEN
4475 */
4476       IF (p_tapv_rec.funding_type_code in (G_SUPPLIER_RETENTION_TYPE_CODE, G_ASSET_SUBSIDY) and
4477           p_tapv_rec.invoice_type <> G_CREDIT) OR
4478          (p_tapv_rec.funding_type_code in (G_EXPENSE, G_ASSET_TYPE_CODE, G_BORROWER_PAYMENT_TYPE_CODE) and
4479           p_tapv_rec.invoice_type <> G_STANDARD) THEN
4480 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4481 
4482         open c_funding_type(p_tapv_rec.funding_type_code);
4483         fetch c_funding_type into l_funding_type;
4484         close c_funding_type;
4485 
4486         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4487                             p_msg_name     => 'OKL_LLA_FUNDING_INVOICE_TYPE',
4488                             p_token1       => 'INVOICE_TYPE',
4489                             p_token1_value => p_tapv_rec.invoice_type,
4490                             p_token2       => 'FUNDING_TYPE',
4491                             p_token2_value => l_funding_type);
4492 
4493         RAISE G_EXCEPTION_HALT_VALIDATION;
4494 
4495       END IF;
4496 
4497     END IF;
4498 
4499     RETURN l_return_status;
4500   EXCEPTION
4501     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4502       l_return_status := OKL_API.G_RET_STS_ERROR;
4503       RETURN l_return_status;
4504     WHEN OTHERS THEN
4505       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4506       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4507                           p_msg_name      => G_UNEXPECTED_ERROR,
4508                           p_token1        => G_SQLCODE_TOKEN,
4509                           p_token1_value  => SQLCODE,
4510                           p_token2        => G_SQLERRM_TOKEN,
4511                           p_token2_value  => SQLERRM);
4512       RETURN l_return_status;
4513   END;
4514 
4515   -------------------------------------------------------------------------
4516   -- validate_invoice_type_and_sign
4517   -- sjalasut, added this function to implement new business validation
4518   -- that when the invoice_type is STANDARD, the invoice amount should be
4519   -- positive and when the invoice_type is CREDIT, invoice amount should be
4520   -- negative.
4521   -------------------------------------------------------------------------
4522   FUNCTION validate_invoice_type_and_sign(p_tapv_rec IN tapv_rec_type
4523                                          ) RETURN VARCHAR2 IS
4524     l_return_status VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4525   BEGIN
4526     -- If invoice type is G_STANDARD then invoice amount is positive
4527     -- If invoice type is G_CREDIT then the invoice amount is negative.
4528     -- sjalasut, made changes to incorporate the business rule as part
4529     -- of OKLR12B Disbursements Project
4530 
4531 --start: cklee 03/01/07 added the following condition, somehow UI has passed
4532 -- wrong data.
4533   IF p_tapv_rec.trx_status_code = 'ENTERED' THEN
4534 --end: cklee 03/01/07 added the following condition, somehow UI has passed
4535 -- wrong data.
4536     IF((p_tapv_rec.INVOICE_TYPE = G_STANDARD AND p_tapv_rec.AMOUNT < 0)
4537        OR(p_tapv_rec.INVOICE_TYPE = G_CREDIT AND p_tapv_rec.AMOUNT > 0))THEN
4538       OKL_API.set_message(
4539                           p_app_name => G_APP_NAME
4540                          ,p_msg_name => 'OKL_LLA_INV_TYPE_AND_SIGN'
4541                          );
4542       RAISE G_EXCEPTION_HALT_VALIDATION;
4543 --start: cklee 03/01/07 added the following condition, somehow UI has passed
4544 -- wrong data.
4545     END IF;
4546 --end: cklee 03/01/07 added the following condition, somehow UI has passed
4547 -- wrong data.
4548   END IF;
4549     RETURN l_return_status;
4550   EXCEPTION
4551     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4552       l_return_status := OKL_API.G_RET_STS_ERROR;
4553       RETURN l_return_status;
4554     WHEN OTHERS THEN
4555       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4556       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4557                           p_msg_name      => G_UNEXPECTED_ERROR,
4558                           p_token1        => G_SQLCODE_TOKEN,
4559                           p_token1_value  => SQLCODE,
4560                           p_token2        => G_SQLERRM_TOKEN,
4561                           p_token2_value  => SQLERRM);
4562       RETURN l_return_status;
4563   END validate_invoice_type_and_sign;
4564 
4565   --------------------------------------------------------------------------
4566   ----- Validate kle_id (contract line id)... Real version by chenkuang.lee
4567   ----- 1) if funding_type_code = 'ASSET'
4568   ----- 2) check required only, OKLSTPLB.pls will check FK for kle_id
4569   --------------------------------------------------------------------------
4570 
4571   FUNCTION validate_kle_id(
4572     p_tplv_rec                 IN tplv_rec_type
4573     ,p_mode                    IN VARCHAR2
4574   ) RETURN VARCHAR2
4575   IS
4576     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4577     l_dummy       VARCHAR2(1) := '?';
4578     l_result VARCHAR2(1) := OKL_API.G_TRUE;
4579 
4580     CURSOR c (p_tap_id NUMBER)
4581     IS
4582       SELECT 'X'
4583         FROM OKL_TRX_AP_INVOICES_B
4584        WHERE id = p_tap_id
4585        AND funding_type_code = 'ASSET'
4586     ;
4587   BEGIN
4588 
4589     OPEN c (p_tplv_rec.tap_id);
4590     FETCH c INTO l_dummy;
4591     CLOSE c;
4592 
4593 
4594     IF (l_dummy = 'X') THEN
4595 
4596       -- kle_id is required:
4597       IF (p_tplv_rec.kle_id IS NULL) OR
4598          (p_tplv_rec.kle_id = OKL_API.G_MISS_NUM)
4599       THEN
4600         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4601                           p_msg_name     => G_REQUIRED_VALUE,
4602                           p_token1       => G_COL_NAME_TOKEN,
4603 --START:| 30-May-2006  cklee -- Fixed bug#5241187                                    |
4604 --                          p_token1_value => 'Contract Top Line'); -- kle_id (contract_line_id) assoc asset number
4605                           p_token1_value => 'Asset Number'); -- kle_id (contract_line_id) assoc asset number
4606 --END:| 30-May-2006  cklee -- Fixed bug#5241187                                    |
4607         RAISE G_EXCEPTION_HALT_VALIDATION;
4608       END IF;
4609 
4610       -- check uniqueness
4611       l_result := is_contract_line_unique(
4612                           p_kle_id   => p_tplv_rec.kle_id
4613                           ,p_fund_id => p_tplv_rec.tap_id
4614 
4615                           ,p_fund_line_id => p_tplv_rec.id
4616                           ,p_mode    => p_mode);
4617       IF (l_result = OKL_API.G_FALSE) THEN
4618         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4619                             p_msg_name     => G_NOT_UNIQUE,
4620                             p_token1       => G_COL_NAME_TOKEN,
4621                             p_token1_value => 'Asset Number');
4622         RAISE G_EXCEPTION_HALT_VALIDATION;
4623       END IF;
4624 
4625     END IF;
4626 
4627     RETURN l_return_status;
4628   EXCEPTION
4629     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4630       l_return_status := OKL_API.G_RET_STS_ERROR;
4631       RETURN l_return_status;
4632     WHEN OTHERS THEN
4633       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4634       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4635                           p_msg_name      => G_UNEXPECTED_ERROR,
4636                           p_token1        => G_SQLCODE_TOKEN,
4637                           p_token1_value  => SQLCODE,
4638                           p_token2        => G_SQLERRM_TOKEN,
4639                           p_token2_value  => SQLERRM);
4640       RETURN l_return_status;
4641   END;
4642   --------------------------------------------------------------------------
4643   ----- Validate kle_id (contract line id)
4644   ----- 1) if funding_type_code = 'ASSET'
4645   ----- 2) check required only, OKLSTPLB.pls will check FK for kle_id
4646   ----- 3) check pl/sql table before check DB, something wrong with the DB
4647   -----    transaction control or some problem with the code logic
4648   --------------------------------------------------------------------------
4649 
4650  FUNCTION validate_table_kle_id(
4651     p_tplv_tbl                 IN tplv_tbl_type
4652 
4653  ) RETURN VARCHAR2
4654   IS
4655     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4656 
4657     l_dummy       VARCHAR2(1) := '?';
4658     l_result VARCHAR2(1) := OKL_API.G_TRUE;
4659 
4660     CURSOR c (p_tap_id NUMBER)
4661 
4662     IS
4663       SELECT 'X'
4664 
4665         FROM OKL_TRX_AP_INVOICES_B
4666        WHERE id = p_tap_id
4667        AND funding_type_code = 'ASSET'
4668     ;
4669   BEGIN
4670 
4671     OPEN c (p_tplv_tbl(p_tplv_tbl.FIRST).tap_id);
4672     FETCH c INTO l_dummy;
4673     CLOSE c;
4674 
4675     IF (l_dummy = 'X') THEN
4676 
4677       -- check uniqueness
4678       l_result := is_kle_id_unique(p_tplv_tbl=>p_tplv_tbl);
4679 
4680       IF (l_result = OKL_API.G_FALSE) THEN
4681 
4682         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4683                             p_msg_name     => G_NOT_UNIQUE,
4684                             p_token1       => G_COL_NAME_TOKEN,
4685                             p_token1_value => 'Asset Number');
4686         RAISE G_EXCEPTION_HALT_VALIDATION;
4687       END IF;
4688 
4689     END IF;
4690 
4691     RETURN l_return_status;
4692   EXCEPTION
4693 
4694     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4695       l_return_status := OKL_API.G_RET_STS_ERROR;
4696       RETURN l_return_status;
4697     WHEN OTHERS THEN
4698       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4699       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4700                           p_msg_name      => G_UNEXPECTED_ERROR,
4701                           p_token1        => G_SQLCODE_TOKEN,
4702                           p_token1_value  => SQLCODE,
4703                           p_token2        => G_SQLERRM_TOKEN,
4704                           p_token2_value  => SQLERRM);
4705 
4706       RETURN l_return_status;
4707   END;
4708   --------------------------------------------------------------------------
4709   ----- Validate Stream Type ID... Real version by chenkuang.lee
4710   ----- 1) if funding_type_code = 'SUPPLIER_RETENTION'
4711   ----- 2) check required only, OKLSTPLB.pls will check FK for sty_id
4712   --------------------------------------------------------------------------
4713 
4714   FUNCTION validate_stream_id(
4715     p_tplv_rec                 IN tplv_rec_type
4716   ) RETURN VARCHAR2
4717   IS
4718     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4719     l_dummy       VARCHAR2(1) := '?';
4720 
4721     CURSOR c (p_tap_id NUMBER)
4722     IS
4723       SELECT 'X'
4724         FROM OKL_TRX_AP_INVOICES_B
4725        WHERE id = p_tap_id
4726        AND funding_type_code in ('SUPPLIER_RETENTION', 'EXPENSE')
4727     ;
4728   BEGIN
4729 
4730     OPEN c (p_tplv_rec.tap_id);
4731     FETCH c INTO l_dummy;
4732     CLOSE c;
4733 
4734     IF (l_dummy = 'X') THEN
4735       -- Stream Type ID is required:
4736       IF (p_tplv_rec.sty_id IS NULL) OR
4737          (p_tplv_rec.sty_id = OKL_API.G_MISS_NUM)
4738       THEN
4739         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4740                           p_msg_name     => G_REQUIRED_VALUE,
4741                           p_token1       => G_COL_NAME_TOKEN,
4742                           p_token1_value => 'Stream Type'); -- sty_id
4743         RAISE G_EXCEPTION_HALT_VALIDATION;
4744       END IF;
4745     END IF;
4746 
4747     RETURN l_return_status;
4748   EXCEPTION
4749     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4750       l_return_status := OKL_API.G_RET_STS_ERROR;
4751       RETURN l_return_status;
4752     WHEN OTHERS THEN
4753       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4754       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4755                           p_msg_name      => G_UNEXPECTED_ERROR,
4756                           p_token1        => G_SQLCODE_TOKEN,
4757                           p_token1_value  => SQLCODE,
4758                           p_token2        => G_SQLERRM_TOKEN,
4759                           p_token2_value  => SQLERRM);
4760       RETURN l_return_status;
4761   END;
4762   --------------------------------------------------------------------------
4763   ----- Validate Funding Line Amount...
4764   --------------------------------------------------------------------------
4765 
4766   FUNCTION validate_line_amount(
4767     p_tplv_rec                  IN tplv_rec_type
4768     ,p_mode                     IN VARCHAR2
4769   ) RETURN VARCHAR2
4770   IS
4771     l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4772 
4773     l_api_version           NUMBER := 1.0;
4774     l_init_msg_list         VARCHAR2(1) := OKL_API.G_FALSE;
4775     x_return_status         VARCHAR2(1);
4776     x_msg_count             NUMBER;
4777     x_msg_data              VARCHAR2(2000);
4778     x_value                 NUMBER := 0;
4779     l_chr_id                NUMBER;
4780     l_funding_type_code     VARCHAR2(30);
4781     l_cur_total_amount      NUMBER := 0;
4782     l_cur_amount            NUMBER := 0;
4783     l_results_amount        NUMBER := 0;
4784     l_message_name          VARCHAR2(30);
4785 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4786 
4787   l_invoice_type okl_trx_ap_invoices_b.invoice_type%type;
4788   cursor c_invoice_type (p_tap_id number)is
4789     select invoice_type
4790     from okl_trx_ap_invoices_b
4791     where id = p_tap_id;
4792 
4793 --end: cklee 3/01/07 added invoice type and amount sign check at line level
4794 
4795   BEGIN
4796     -- line Amount is required: default to 0 @ UI
4797     IF (p_tplv_rec.amount IS NULL) OR
4798        (p_tplv_rec.amount = OKL_API.G_MISS_NUM)
4799     THEN
4800       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4801                           p_msg_name     => G_REQUIRED_VALUE,
4802                           p_token1       => G_COL_NAME_TOKEN,
4803                           p_token1_value => 'Amount');
4804       RAISE G_EXCEPTION_HALT_VALIDATION;
4805     END IF;
4806 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4807     -- If invoice type is G_STANDARD then invoice amount is positive
4808     -- If invoice type is G_CREDIT then the invoice amount is negative.
4809     -- cklee, made changes to incorporate the business rule as part
4810     -- of OKLR12B Disbursements Project
4811     open c_invoice_type(p_tplv_rec.tap_id);
4812     fetch c_invoice_type into l_invoice_type;
4813     close c_invoice_type;
4814 
4815     IF((l_invoice_type = G_STANDARD AND p_tplv_rec.AMOUNT < 0)
4816        OR(l_invoice_type = G_CREDIT AND p_tplv_rec.AMOUNT > 0))THEN
4817       OKL_API.set_message(
4818                           p_app_name => G_APP_NAME
4819                          ,p_msg_name => 'OKL_LLA_INV_TYPE_AND_SIGN'
4820                          );
4821       RAISE G_EXCEPTION_HALT_VALIDATION;
4822     END IF;
4823 --end: cklee 3/01/07 added invoice type and amount sign check at line level
4824 
4825     -- sjalasut, commented the following code as part of OKLR12B disbursements
4826     -- project.
4827     /*
4828     IF (p_tplv_rec.amount < 0 ) THEN
4829 
4830       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4831                           p_msg_name     => 'OKL_LLA_POSITIVE_AMOUNT_ONLY',
4832                           p_token1       => 'COL_NAME',
4833                           p_token1_value => 'Amount');
4834 
4835 
4836       RAISE G_EXCEPTION_HALT_VALIDATION;
4837     END IF;
4838     */
4839     RETURN l_return_status;
4840   EXCEPTION
4841     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4842       l_return_status := OKL_API.G_RET_STS_ERROR;
4843       RETURN l_return_status;
4844     WHEN OTHERS THEN
4845 
4846 
4847       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4848 
4849       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4850                           p_msg_name      => G_UNEXPECTED_ERROR,
4851                           p_token1        => G_SQLCODE_TOKEN,
4852                           p_token1_value  => SQLCODE,
4853                           p_token2        => G_SQLERRM_TOKEN,
4854                           p_token2_value  => SQLERRM);
4855       RETURN l_return_status;
4856   END;
4857 
4858   --------------------------------------------------------------------------
4859   FUNCTION validate_header_attributes(
4860     p_tapv_rec                        IN tapv_rec_type
4861    ,p_mode                            IN VARCHAR2 -- 'C'reate,'U'pdate,'D'elete
4862 
4863   ) RETURN VARCHAR2
4864   IS
4865     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4866     x_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4867 
4868     l_vendor_id PO_VENDOR_SITES_ALL.vendor_id%TYPE;
4869     l_org_id okc_k_headers_b.AUTHORING_ORG_ID%TYPE;
4870 
4871 
4872 cursor c_vs (p_vendor_site_id number)
4873 is
4874 select vs.vendor_id
4875 from PO_VENDOR_SITES_ALL VS
4876 where vs.vendor_site_id = p_vendor_site_id;
4877 
4878 cursor c_org (p_khr_id number)
4879 is
4880 select chr.AUTHORING_ORG_ID
4881 from okc_k_headers_b chr
4882 where chr.id = p_khr_id;
4883 
4884 
4885   BEGIN
4886 
4887     -- Do formal attribute validation:
4888     l_return_status := validate_trx_status_code(p_tapv_rec);
4889     --- Store the highest degree of error
4890     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4891       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4892         x_return_status := l_return_status;
4893       END IF;
4894       RAISE G_EXCEPTION_HALT_VALIDATION;
4895     END IF;
4896 
4897     -- Do formal attribute validation:
4898     l_return_status := validate_payment_due_date(p_tapv_rec);
4899 
4900     --- Store the highest degree of error
4901     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4902       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4903         x_return_status := l_return_status;
4904       END IF;
4905       RAISE G_EXCEPTION_HALT_VALIDATION;
4906     END IF;
4907 
4908 -- funding checklist enhancement for 11.5.9
4909 -- to be able to copy funding checklist from associated credit line contract, user has to
4910 -- select valid credit line before create a funding request
4911 
4912 --
4913     l_return_status := validate_creditline(p_tapv_rec);
4914     --- Store the highest degree of error
4915     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4916       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4917         x_return_status := l_return_status;
4918       END IF;
4919       RAISE G_EXCEPTION_HALT_VALIDATION;
4920     END IF;
4921 -- funding checklist enhancement for 11.5.9
4922 
4923 -- cklee 01/30/03 check at approved until integrated with WF
4924 -- cklee 06/24/03 WF enable, change check to 'SUBMITTED'
4925     IF (upper(p_mode) = 'U' AND p_tapv_rec.trx_status_code in ('SUBMITTED','APPROVED')) THEN
4926 
4927       --Bug# 10156343
4928       l_return_status := validate_chr_status(p_tapv_rec.khr_id, p_tapv_rec.funding_type_code);
4929       --- Store the highest degree of error
4930       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4931         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4932           x_return_status := l_return_status;
4933         END IF;
4934         RAISE G_EXCEPTION_HALT_VALIDATION;
4935       END IF;
4936 
4937       l_return_status := validate_header_amount(p_tapv_rec);
4938       --- Store the highest degree of error
4939       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4940         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4941           x_return_status := l_return_status;
4942         END IF;
4943         RAISE G_EXCEPTION_HALT_VALIDATION;
4944       END IF;
4945 
4946     END IF;
4947 
4948     l_return_status := validate_funding_checklist(p_tapv_rec);
4949     --- Store the highest degree of error
4950     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4951       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4952         x_return_status := l_return_status;
4953       END IF;
4954       RAISE G_EXCEPTION_HALT_VALIDATION;
4955     END IF;
4956 
4957 -- bug 2604862
4958     l_return_status := validate_header_amount_for_RL(p_tapv_rec);
4959 
4960     --- Store the highest degree of error
4961     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4962       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4963         x_return_status := l_return_status;
4964       END IF;
4965       RAISE G_EXCEPTION_HALT_VALIDATION;
4966 
4967     END IF;
4968 -- bug 2604862
4969 
4970     IF (p_tapv_rec.FUNDING_TYPE_CODE <> G_ASSET_SUBSIDY) THEN -- cklee 09/17/03
4971       l_return_status := validate_vendor_invoice_number(p_tapv_rec);
4972       --- Store the highest degree of error
4973       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4974         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4975           x_return_status := l_return_status;
4976         END IF;
4977         RAISE G_EXCEPTION_HALT_VALIDATION;
4978       END IF;
4979     END IF;
4980 
4981     l_return_status := validate_ipvs_id(p_tapv_rec);
4982     --- Store the highest degree of error
4983     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4984       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4985         x_return_status := l_return_status;
4986       END IF;
4987       RAISE G_EXCEPTION_HALT_VALIDATION;
4988     END IF;
4989 
4990     l_return_status := validate_funding_type(p_tapv_rec,p_mode);
4991     --- Store the highest degree of error
4992     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4993       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4994         x_return_status := l_return_status;
4995       END IF;
4996       RAISE G_EXCEPTION_HALT_VALIDATION;
4997     END IF;
4998     -- Added for bug 5704212 -- start
4999        l_return_status := validate_release_contract(p_tapv_rec);
5000        --- Store the highest degree of error
5001        IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5002          IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5003            x_return_status := l_return_status;
5004          END IF;
5005          RAISE G_EXCEPTION_HALT_VALIDATION;
5006        END IF;
5007        -- Added for bug 5704212 - End
5008 
5009     l_return_status := validate_payment_method(p_tapv_rec);
5010     --- Store the highest degree of error
5011     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5012       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5013         x_return_status := l_return_status;
5014       END IF;
5015       RAISE G_EXCEPTION_HALT_VALIDATION;
5016     END IF;
5017 
5018     -- 05/04/2004 cklee
5019     l_return_status := validate_invoice_type(p_tapv_rec);
5020     --- Store the highest degree of error
5021     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5022       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5023         x_return_status := l_return_status;
5024       END IF;
5025       RAISE G_EXCEPTION_HALT_VALIDATION;
5026     END IF;
5027 
5028     -- sjalasut, added the function validate_invoice_type_and_sign
5029     -- as part of OKLR12B disbursements project
5030     l_return_status := validate_invoice_type_and_sign(p_tapv_rec);
5031     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5032       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5033         x_return_status := l_return_status;
5034       END IF;
5035       RAISE G_EXCEPTION_HALT_VALIDATION;
5036     END IF;
5037 
5038     -- don't allow to change Request number (invoice number) at update mode
5039     IF (p_mode = 'C') THEN
5040 -- fixed uniqueness check for funding vendor_invoice_number. pass vendor_site_id, org_id
5041       open c_vs(p_tapv_rec.ipvs_id);
5042       fetch c_vs into l_vendor_id;
5043       close c_vs;
5044 
5045       open c_org(p_tapv_rec.khr_id);
5046       fetch c_org into l_org_id;
5047       close c_org;
5048 
5049       l_return_status := is_funding_unique(
5050                           p_vendor_id    => l_vendor_id
5051                           ,p_org_id      => l_org_id
5052                           ,p_fund_number => p_tapv_rec.vendor_invoice_number);
5053 
5054       IF (l_return_status = OKL_API.G_FALSE) THEN
5055         x_return_status := OKL_API.G_RET_STS_ERROR;
5056         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
5057                           p_msg_name     => G_NOT_UNIQUE,
5058                           p_token1       => G_COL_NAME_TOKEN,
5059                           p_token1_value => 'Request Number');
5060 
5061         RAISE G_EXCEPTION_HALT_VALIDATION;
5062       END IF;
5063     END IF;
5064 
5065     RETURN x_return_status;
5066   EXCEPTION
5067     WHEN G_EXCEPTION_HALT_VALIDATION THEN
5068 
5069 
5070       RETURN x_return_status;
5071     WHEN OTHERS THEN
5072       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
5073       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
5074                           p_msg_name      => G_UNEXPECTED_ERROR,
5075                           p_token1        => G_SQLCODE_TOKEN,
5076                           p_token1_value  => SQLCODE,
5077                           p_token2        => G_SQLERRM_TOKEN,
5078                           p_token2_value  => SQLERRM);
5079 
5080       RETURN l_return_status;
5081   END validate_header_attributes;
5082 
5083   --------------------------------------------------------------------------
5084   FUNCTION validate_line_attributes(
5085     p_tplv_rec                      IN tplv_rec_type
5086    ,p_mode                          IN VARCHAR2 -- 'C'reate,'U'pdate,'D'elete
5087   ) RETURN VARCHAR2
5088   IS
5089     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5090     x_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5091   BEGIN
5092 
5093     -- Do formal attribute validation:
5094     -- check sty_id
5095     l_return_status := validate_stream_id(p_tplv_rec);
5096 
5097     --- Store the highest degree of error
5098     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5099       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5100 
5101         x_return_status := l_return_status;
5102       END IF;
5103       RAISE G_EXCEPTION_HALT_VALIDATION; -- 09/2001: one error at a time until Okx/Java can accept more
5104     END IF;
5105 
5106     -- check kle_id
5107     l_return_status := validate_kle_id(p_tplv_rec, p_mode);
5108     --- Store the highest degree of error
5109     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5110       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5111         x_return_status := l_return_status;
5112       END IF;
5113       RAISE G_EXCEPTION_HALT_VALIDATION; -- 09/2001: one error at a time until Okx/Java can accept more
5114     END IF;
5115 
5116     l_return_status := validate_line_amount(p_tplv_rec,p_mode);
5117     --- Store the highest degree of error
5118     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5119       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5120         x_return_status := l_return_status;
5121       END IF;
5122       RAISE G_EXCEPTION_HALT_VALIDATION;
5123     END IF;
5124 
5125     RETURN x_return_status;
5126   EXCEPTION
5127     WHEN G_EXCEPTION_HALT_VALIDATION THEN
5128       RETURN x_return_status;
5129     WHEN OTHERS THEN
5130       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
5131       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
5132                           p_msg_name      => G_UNEXPECTED_ERROR,
5133                           p_token1        => G_SQLCODE_TOKEN,
5134                           p_token1_value  => SQLCODE,
5135                           p_token2        => G_SQLERRM_TOKEN,
5136                           p_token2_value  => SQLERRM);
5137       RETURN l_return_status;
5138   END validate_line_attributes;
5139 
5140   --------------------------------------------------------------------------
5141   ----- Populate additional attributes (sty_id) for line
5142   --------------------------------------------------------------------------
5143   FUNCTION populate_sty_id(
5144     p_tplv_rec                  IN OUT NOCOPY tplv_rec_type
5145   ) RETURN VARCHAR2
5146   IS
5147     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5148     l_stream_id           NUMBER;
5149     l_chr_id              NUMBER;
5150     l_name                VARCHAR2(30);
5151     l_funding_type_code okl_trx_ap_invoices_b.funding_type_code%TYPE;
5152 
5153     CURSOR stream_c(p_name VARCHAR2)
5154     IS
5155     SELECT id
5156     FROM  OKL_STRM_TYPE_V
5157     where name = p_name
5158     ;
5159 
5160     -- sjalasut, modified the cursor below to have khr_id referred from
5161     -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b.
5162     -- changes made as part of OKLR12B disbursements project.
5163     --vpanwar added 28/02/2007 for provide khr_id from p_tplv_rec.khr_id  ..start
5164     /*CURSOR c_funding_type (p_tap_id NUMBER)
5165     IS
5166     select tap.funding_type_code , tpl.khr_id
5167     from okl_trx_ap_invoices_b tap
5168         ,okl_txl_ap_inv_lns_all_b tpl
5169     where tap.id = p_tap_id
5170       and tap.id = tpl.tap_id;*/
5171 
5172     CURSOR c_funding_type (p_tap_id NUMBER)
5173     IS
5174     select tap.funding_type_code /*, tpl.khr_id*/
5175     from okl_trx_ap_invoices_b tap
5176         /*,okl_txl_ap_inv_lns_all_b tpl*/
5177     where tap.id = p_tap_id
5178       /*and tap.id = tpl.tap_id*/;
5179     --vpanwar added 28/02/2007 end
5180 
5181   BEGIN
5182 
5183 
5184       OPEN  c_funding_type(p_tplv_rec.tap_id);
5185       FETCH  c_funding_type INTO l_funding_type_code/*, l_chr_id*/;
5186       CLOSE  c_funding_type;
5187 
5188     --vpanwar added 28/02/2007 start
5189       l_chr_id := p_tplv_rec.khr_id;
5190     --vpanwar added 28/02/2007 end
5191 
5192       IF (l_funding_type_code = G_PREFUNDING_TYPE_CODE) THEN
5193         l_name := G_STY_PURPOSE_CODE_PREFUNDING;
5194       ELSIF (l_funding_type_code = G_BORROWER_PAYMENT_TYPE_CODE) THEN
5195         l_name := G_STY_PURPOSE_CODE_P_BALANCE;
5196       ELSE
5197         l_name := G_STY_PURPOSE_CODE_FUNDING;
5198       END IF;
5199 
5200 /*
5201       OPEN  stream_c(l_name);
5202       FETCH  stream_c INTO l_stream_id;
5203       CLOSE  stream_c;
5204 */
5205 
5206 -- cklee: user defined stream changes
5207       IF (l_funding_type_code = G_BORROWER_PAYMENT_TYPE_CODE) THEN
5208 
5209         Okl_Streams_Util.GET_DEPENDENT_STREAM_TYPE(
5210                p_khr_id                => l_chr_id,
5211                p_primary_sty_purpose   => 'RENT',
5212                p_dependent_sty_purpose => l_name,
5213                x_return_status         => l_return_status,
5214                x_dependent_sty_id      => l_stream_id );
5215 
5216       ELSE
5217 
5218         Okl_Streams_Util.GET_PRIMARY_STREAM_TYPE(
5219                p_khr_id              => l_chr_id,
5220                p_primary_sty_purpose => l_name,
5221                x_return_status       => l_return_status,
5222                x_primary_sty_id      => l_stream_id );
5223 
5224       END IF;
5225 --cklee user defined stream type modification
5226 
5227       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5228         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5229       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5230         RAISE OKL_API.G_EXCEPTION_ERROR;
5231       END IF;
5232 
5233       -- only fill if sty_id not exists
5234       IF (l_funding_type_code in (G_PREFUNDING_TYPE_CODE,
5235                                   G_ASSET_TYPE_CODE,
5236                                   G_BORROWER_PAYMENT_TYPE_CODE)) THEN
5237         p_tplv_rec.sty_id := l_stream_id;
5238       END IF;
5239 
5240     RETURN l_return_status;
5241   EXCEPTION
5242 
5243     WHEN G_EXCEPTION_HALT_VALIDATION THEN
5244 
5245       l_return_status := OKL_API.G_RET_STS_ERROR;
5246       RETURN l_return_status;
5247     WHEN OTHERS THEN
5248       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
5249 
5250       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
5251                           p_msg_name      => G_UNEXPECTED_ERROR,
5252                           p_token1        => G_SQLCODE_TOKEN,
5253                           p_token1_value  => SQLCODE,
5254                           p_token2        => G_SQLERRM_TOKEN,
5255                           p_token2_value  => SQLERRM);
5256       RETURN l_return_status;
5257   END populate_sty_id;
5258 ----------------------------------------------------------------------------
5259 
5260  PROCEDURE SYNC_HEADER_AMOUNT(
5261   p_api_version                  IN NUMBER
5262  ,p_init_msg_list                IN VARCHAR2
5263  ,x_return_status                OUT NOCOPY VARCHAR2
5264  ,x_msg_count                    OUT NOCOPY NUMBER
5265  ,x_msg_data                     OUT NOCOPY VARCHAR2
5266  ,p_tplv_tbl                     IN tplv_tbl_type
5267 )
5268 
5269 IS
5270   l_api_name        CONSTANT VARCHAR2(30) := 'SYNC_HEADER_AMOUNT';
5271   l_api_version     CONSTANT NUMBER       := 1.0;
5272   l_tapv_rec        tapv_rec_type;
5273   x_tapv_rec        tapv_rec_type;
5274   j                 BINARY_INTEGER;
5275   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5276   l_id                     OKL_TRX_AP_INVOICES_B.ID%TYPE;
5277   l_funding_type_code      OKL_TRX_AP_INVOICES_B.funding_type_code%TYPE;
5278   l_VENDOR_INVOICE_NUMBER  OKL_TRX_AP_INVOICES_B.VENDOR_INVOICE_NUMBER%TYPE;
5279   l_PAY_GROUP_LOOKUP_CODE  OKL_TRX_AP_INVOICES_B.PAY_GROUP_LOOKUP_CODE%TYPE;
5280   l_NETTABLE_YN            OKL_TRX_AP_INVOICES_B.NETTABLE_YN%TYPE;
5281   l_amount                 OKL_TRX_AP_INVOICES_B.AMOUNT%TYPE := 0;
5282   l_INVOICE_TYPE           OKL_TRX_AP_INVOICES_B.INVOICE_TYPE%TYPE;
5283 
5284     CURSOR c (p_id NUMBER)
5285     IS
5286       SELECT h.id,
5287              h.funding_type_code,
5288              h.VENDOR_INVOICE_NUMBER,
5289              h.PAY_GROUP_LOOKUP_CODE,
5290              h.NETTABLE_YN,
5291              h.INVOICE_TYPE
5292         FROM OKL_TRX_AP_INVOICES_B h
5293        WHERE h.id = p_id
5294     ;
5295 
5296 BEGIN
5297 
5298     x_return_status      := OKL_API.G_RET_STS_SUCCESS;
5299     -- Call start_activity to create savepoint, check compatibility
5300     -- and initialize message list
5301     x_return_status := OKL_API.START_ACTIVITY (
5302                                l_api_name,
5303                                p_init_msg_list,
5304                                '_PVT',
5305                                x_return_status);
5306     -- Check if activity started successfully
5307     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5308        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5309 
5310     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5311        RAISE OKL_API.G_EXCEPTION_ERROR;
5312     END IF;
5313 --
5314 
5315 --*** Begin API body ****************************************************
5316 
5317     OPEN c (p_tplv_tbl(p_tplv_tbl.FIRST).tap_id);
5318     FETCH c INTO l_id,
5319                  l_funding_type_code,
5320                  l_VENDOR_INVOICE_NUMBER,
5321                  l_PAY_GROUP_LOOKUP_CODE,
5322                  l_NETTABLE_YN,
5323                  l_INVOICE_TYPE;
5324     CLOSE c;
5325 
5326     -- set default
5327     l_amount := OKL_FUNDING_PVT.get_contract_line_funded_amt(l_id, l_funding_type_code);
5328     -- fill in all necessary attributes
5329     l_tapv_rec.id := l_id;
5330     l_tapv_rec.amount:= nvl(l_amount,0);
5331     l_tapv_rec.VENDOR_INVOICE_NUMBER := l_VENDOR_INVOICE_NUMBER;
5332     l_tapv_rec.PAY_GROUP_LOOKUP_CODE := l_PAY_GROUP_LOOKUP_CODE;
5333     l_tapv_rec.NETTABLE_YN := l_NETTABLE_YN;
5334     l_tapv_rec.INVOICE_TYPE := l_INVOICE_TYPE;
5335 
5336     OKL_TRX_AP_INVOICES_PUB.UPDATE_TRX_AP_INVOICES(
5337       p_api_version   => p_api_version,
5338       p_init_msg_list => p_init_msg_list,
5339       x_return_status => x_return_status,
5340       x_msg_count     => x_msg_count,
5341       x_msg_data      => x_msg_data,
5342       p_tapv_rec      => l_tapv_rec,
5343       x_tapv_rec      => x_tapv_rec);
5344 
5345     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5346       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5347     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5348       RAISE OKL_API.G_EXCEPTION_ERROR;
5349     END IF;
5350 
5351     OKL_API.END_ACTIVITY (x_msg_count,
5352                           x_msg_data );
5353   EXCEPTION
5354     WHEN OKL_API.G_EXCEPTION_ERROR THEN
5355     x_return_status := OKL_API.HANDLE_EXCEPTIONS(
5356                                l_api_name,
5357                                G_PKG_NAME,
5358                                'OKL_API.G_RET_STS_ERROR',
5359                                x_msg_count,
5360                                x_msg_data,
5361                                '_PVT');
5362     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5363     x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
5364                               l_api_name,
5365                               G_PKG_NAME,
5366                               'OKL_API.G_RET_STS_UNEXP_ERROR',
5367                               x_msg_count,
5368                               x_msg_data,
5369                               '_PVT');
5370     WHEN OTHERS THEN
5371     x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
5372                               l_api_name,
5373                               G_PKG_NAME,
5374                               'OTHERS',
5375                               x_msg_count,
5376                               x_msg_data,
5377                               '_PVT');
5378   END;
5379 
5380 ----------------------------------------------------------------------------
5381 -- Public Procedures and Functions
5382 ----------------------------------------------------------------------------
5383 
5384 PROCEDURE create_funding_header(
5385   p_api_version                  IN NUMBER
5386  ,p_init_msg_list                IN VARCHAR2
5387  ,x_return_status                OUT NOCOPY VARCHAR2
5388  ,x_msg_count                    OUT NOCOPY NUMBER
5389  ,x_msg_data                     OUT NOCOPY VARCHAR2
5390  ,p_tapv_rec                     IN tapv_rec_type
5391  ,x_tapv_rec                     OUT NOCOPY tapv_rec_type
5392 
5393 )
5394 IS
5395   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_HEADER';
5396   l_api_version     CONSTANT NUMBER       := 1.0;
5397   l_tapv_rec                 tapv_rec_type := p_tapv_rec;
5398   i                          NUMBER;
5399     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5400     l_dummy VARCHAR2(1) := OKL_API.G_TRUE;
5401     l_try_id NUMBER;
5402     l_org_id NUMBER;
5403    l_try_name OKL_TRX_TYPES_TL.NAME%TYPE;
5404 
5405 --
5406 --
5407   l_tplv_rec        tplv_rec_type;
5408   x_tplv_rec        tplv_rec_type;
5409 --
5410 --
5411 
5412     CURSOR l_tryv_csr(p_try_name varchar2) IS
5413 select TRYB.ID
5414 FROM
5415   OKL_TRX_TYPES_B TRYB,
5416   OKL_TRX_TYPES_TL TRYT
5417 WHERE
5418   TRYB.ID = TRYT.ID and
5419   TRYT.LANGUAGE = 'US' and
5420   TRYT.NAME = p_try_name; -- cklee 05/04/2004
5421 
5422 
5423     CURSOR l_org_id_csr(p_chr_id number) IS
5424       SELECT chr.authoring_org_id
5425       FROM okc_k_headers_b chr
5426       WHERE chr.id = p_chr_id;
5427 
5428 
5429 
5430 
5431   --Added by dpsingh for LE uptake
5432   CURSOR contract_num_csr (p_ctr_id1 NUMBER) IS
5433   SELECT  contract_number
5434   FROM okc_k_headers_b
5435   WHERE id = p_ctr_id1;
5436 
5437   l_cntrct_number          OKC_K_HEADERS_B.CONTRACT_NUMBER%TYPE;
5438   l_legal_entity_id          NUMBER;
5439 
5440 BEGIN
5441   -- Set API savepoint
5442 
5443   SAVEPOINT CREATE_FUNDING_HEADER_PVT;
5444 
5445   -- Check for call compatibility
5446   IF (NOT FND_API.Compatible_API_Call (l_api_version,
5447                                 	   p_api_version,
5448                                 	   l_api_name,
5449                                 	   G_PKG_NAME ))
5450   THEN
5451     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5452   END IF;
5453 
5454   -- Initialize message list if requested
5455   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
5456       FND_MSG_PUB.initialize;
5457 	END IF;
5458 
5459   -- Initialize API status to success
5460   x_return_status := OKL_API.G_RET_STS_SUCCESS;
5461 
5462 
5463 /*** Begin API body ****************************************************/
5464 
5465     IF (p_tapv_rec.funding_type_code = G_MANUAL_DISB) THEN
5466       l_try_name := G_TRANSACTION_DISBURSEMENT;
5467     ELSE
5468       l_try_name := G_TRANSACTION_FUNDING;
5469     END IF;
5470 
5471     open  l_tryv_csr(l_try_name);
5472     fetch l_tryv_csr into l_try_id;
5473     close l_tryv_csr;
5474 
5475 -- force to get try_id
5476     l_tapv_rec.try_id := l_try_id;
5477 
5478 -- 10-10-2003 cklee fixed bug# 3159723
5479     open  l_org_id_csr(l_tapv_rec.khr_id);
5480     fetch l_org_id_csr into l_org_id;
5481     close l_org_id_csr;
5482 
5483     IF (l_tapv_rec.org_id IS NULL) THEN
5484       l_tapv_rec.org_id := l_org_id;
5485     END IF;
5486 
5487     -- populates more attributes for BPD
5488     l_return_status := populate_more_attrs(l_tapv_rec);
5489     --- Store the highest degree of error
5490     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5491       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5492         x_return_status := l_return_status;
5493       END IF;
5494       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5495     END IF;
5496 
5497     l_return_status := validate_header_attributes(l_tapv_rec, 'C');
5498     --- Store the highest degree of error
5499     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5500       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5501         x_return_status := l_return_status;
5502       END IF;
5503       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5504     END IF;
5505     --Added by dpsingh for LE Uptake
5506     l_legal_entity_id  := OKL_LEGAL_ENTITY_UTIL.get_khr_le_id(p_tapv_rec.khr_id) ;
5507     IF  l_legal_entity_id IS NOT NULL THEN
5508        l_tapv_rec.legal_entity_id :=  l_legal_entity_id;
5509     ELSE
5510         -- get the contract number
5511        OPEN contract_num_csr(p_tapv_rec.khr_id);
5512        FETCH contract_num_csr INTO l_cntrct_number;
5513        CLOSE contract_num_csr;
5514 	Okl_Api.set_message(p_app_name     => g_app_name,
5515                              p_msg_name     => 'OKL_LE_NOT_EXIST_CNTRCT',
5516 			     p_token1           =>  'CONTRACT_NUMBER',
5517 			     p_token1_value  =>  l_cntrct_number);
5518          RAISE OKL_API.G_EXCEPTION_ERROR;
5519     END IF;
5520     -- smadhava - Bug#5200033 - Added - Start
5521     -- Round the amount to the System Options setup
5522     l_tapv_rec.AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => l_tapv_rec.AMOUNT
5523                                                         , p_currency_code => l_tapv_rec.CURRENCY_CODE);
5524     -- smadhava - Bug#5200033 - Added - End
5525 
5526 --    OKL_TAP_PVT.insert_row(
5527       OKL_TRX_AP_INVOICES_PUB.INSERT_TRX_AP_INVOICES(
5528       p_api_version   => p_api_version,
5529       p_init_msg_list => p_init_msg_list,
5530       x_return_status => x_return_status,
5531       x_msg_count     => x_msg_count,
5532       x_msg_data      => x_msg_data,
5533       p_tapv_rec      => l_tapv_rec,
5534       x_tapv_rec      => x_tapv_rec);
5535 
5536     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5537       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5538     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5539       RAISE OKL_API.G_EXCEPTION_ERROR;
5540     END IF;
5541 
5542 --
5543 -- create dummy funding line for pre-funding, AP required
5544 --
5545 
5546     IF (l_tapv_rec.funding_type_code in ('PREFUNDING','BORROWER_PAYMENT')) THEN
5547 
5548       l_tplv_rec.inv_distr_line_code := 'ITEM';
5549       l_tplv_rec.tap_id := x_tapv_rec.id;
5550       l_tplv_rec.amount := l_tapv_rec.amount;
5551       l_tplv_rec.org_id := l_tapv_rec.org_id;
5552       l_tplv_rec.line_number := 1;
5553       l_tplv_rec.DISBURSEMENT_BASIS_CODE := 'BILL_DATE';
5554       -- sjalasut, added code to populate khr_id at line level. the khr_id
5555       -- is assumed to be retained at p_tapv_rec level from which the value
5556       -- is derived in this procedure. changes made as part of OKLR12B
5557       -- disbursements project. START code changes
5558       l_tplv_rec.khr_id := l_tapv_rec.khr_id;
5559       -- sjalsut, END code changes
5560 
5561       -- fixed bug#3338910
5562       l_return_status := populate_sty_id(l_tplv_rec);
5563       --- Store the highest degree of error
5564       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5565         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5566           x_return_status := l_return_status;
5567         END IF;
5568         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5569       END IF;
5570 
5571 
5572       OKL_TXL_AP_INV_LNS_PUB.INSERT_TXL_AP_INV_LNS(
5573         p_api_version   => p_api_version,
5574         p_init_msg_list => p_init_msg_list,
5575         x_return_status => x_return_status,
5576         x_msg_count     => x_msg_count,
5577         x_msg_data      => x_msg_data,
5578         p_tplv_rec      => l_tplv_rec,
5579         x_tplv_rec      => x_tplv_rec);
5580 
5581       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5582         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5583       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5584         RAISE OKL_API.G_EXCEPTION_ERROR;
5585       END IF;
5586 
5587     END IF;
5588 
5589 --
5590 -- end of create dummy funding line for pre-funding
5591 
5592 --
5593 -- Removed code which creates checklists for funding request, as part of Funding OA Migration.
5594 -- by nikshah
5595 
5596    /*
5597    -- vthiruva, 08/31/2004
5598    -- START, Code change to enable Business Event
5599    */
5600 
5601     --raise the business event for create funding request if
5602     --transaction status code is ENTERED
5603     IF(p_tapv_rec.trx_status_code = 'ENTERED')THEN
5604     	raise_business_event(p_api_version    => p_api_version,
5605                              p_init_msg_list  => p_init_msg_list,
5606 			     x_return_status  => x_return_status,
5607 		  	     x_msg_count      => x_msg_count,
5608 			     x_msg_data       => x_msg_data,
5609 			     p_khr_id         => p_tapv_rec.khr_id,
5610 			     p_id             => x_tapv_rec.id,
5611 			     p_event_name     => G_WF_EVT_FUN_REQ_CREATED);
5612 
5613         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5614 	         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5615         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5616           RAISE OKL_API.G_EXCEPTION_ERROR;
5617         END IF;
5618 
5619     End If;
5620 
5621    /*
5622    -- vthiruva, 08/31/2004
5623    -- END, Code change to enable Business Event
5624    */
5625 
5626 /*** End API body ******************************************************/
5627 
5628   -- Get message count and if count is 1, get message info
5629 	FND_MSG_PUB.Count_And_Get
5630     (p_count          =>      x_msg_count,
5631      p_data           =>      x_msg_data);
5632 
5633 EXCEPTION
5634   WHEN OKL_API.G_EXCEPTION_ERROR THEN
5635     ROLLBACK TO CREATE_FUNDING_HEADER_PVT;
5636     x_return_status := OKL_API.G_RET_STS_ERROR;
5637 
5638     FND_MSG_PUB.Count_And_Get
5639       (p_count         =>      x_msg_count,
5640        p_data          =>      x_msg_data);
5641 
5642   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5643     ROLLBACK TO CREATE_FUNDING_HEADER_PVT;
5644     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
5645     FND_MSG_PUB.Count_And_Get
5646       (p_count         =>      x_msg_count,
5647        p_data          =>      x_msg_data);
5648 
5649   WHEN OTHERS THEN
5650 	ROLLBACK TO CREATE_FUNDING_HEADER_PVT;
5651       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
5652       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
5653                           p_msg_name      => G_UNEXPECTED_ERROR,
5654                           p_token1        => G_SQLCODE_TOKEN,
5655                           p_token1_value  => SQLCODE,
5656                           p_token2        => G_SQLERRM_TOKEN,
5657                           p_token2_value  => SQLERRM);
5658 
5659       FND_MSG_PUB.Count_And_Get
5660         (p_count         =>      x_msg_count,
5661          p_data          =>      x_msg_data);
5662 
5663   END create_funding_header;
5664 
5665 
5666 --------------------------------------------------------------------------
5667 
5668 PROCEDURE update_funding_header(
5669   p_api_version                  IN NUMBER
5670  ,p_init_msg_list                IN VARCHAR2
5671  ,x_return_status                OUT NOCOPY VARCHAR2
5672  ,x_msg_count                    OUT NOCOPY NUMBER
5673  ,x_msg_data                     OUT NOCOPY VARCHAR2
5674  ,p_tapv_rec                     IN tapv_rec_type
5675  ,x_tapv_rec                     OUT NOCOPY tapv_rec_type
5676 )
5677 IS
5678   l_api_name        CONSTANT VARCHAR2(30) := 'UPDATE_FUNDING_HEADER';
5679   l_api_version     CONSTANT NUMBER       := 1.0;
5680   l_tapv_rec                 tapv_rec_type := p_tapv_rec;
5681   i                          NUMBER;
5682 
5683   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5684 
5685   l_row_not_found   boolean := false;
5686   l_dummy           number;
5687   funding_line_id   number;
5688   l_approval_option varchar2(10);
5689 
5690   -- vthiruva, 08/31/2004
5691   -- variables to raise business events
5692   l_event_name            wf_events.name%TYPE := null;
5693   l_raise_business_event  VARCHAR2(1) := OKL_API.G_FALSE;
5694 
5695 cursor c_chklst_exists(p_fund_id varchar2) is
5696 --start modified abhsaxen for performance SQLID 20562448
5697 SELECT 1
5698 FROM OKC_RULES_B RULT
5699 WHERE rult.rule_information_category = 'LAFCLD'
5700 and rult.OBJECT1_ID1 = p_fund_id ;
5701 --end modified abhsaxen for performance SQLID 20562448
5702 
5703 --- vpanwar 21/02/2007 Added
5704   --- to get all the funding lines for the funding header
5705     CURSOR funding_line_csr(p_fund_id number) IS
5706         Select id funding_line_id
5707         from OKL_TXL_AP_INV_LNS_B
5708         Where tap_id = p_fund_id;
5709   --- vpanwar 21/02/2007 End
5710 
5711 BEGIN
5712   -- Set API savepoint
5713   SAVEPOINT UPDATE_FUNDING_HEADER_PVT;
5714 
5715   -- Check for call compatibility
5716   IF (NOT FND_API.Compatible_API_Call (l_api_version,
5717                                 	   p_api_version,
5718                                 	   l_api_name,
5719                                 	   G_PKG_NAME ))
5720   THEN
5721     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5722   END IF;
5723 
5724   -- Initialize message list if requested
5725 
5726   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
5727       FND_MSG_PUB.initialize;
5728 	END IF;
5729 
5730   -- Initialize API status to success
5731   x_return_status := OKL_API.G_RET_STS_SUCCESS;
5732 
5733 
5734 /*** Begin API body ****************************************************/
5735     l_return_status := validate_header_attributes(l_tapv_rec, 'U');
5736     --- Store the highest degree of error
5737     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5738       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5739         x_return_status := l_return_status;
5740       END IF;
5741       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5742     END IF;
5743 
5744     ------------------------------------------------------------------
5745     -- added for approval process
5746     ------------------------------------------------------------------
5747     l_approval_option := fnd_profile.value('OKL_LEASE_FUNDING_APPROVAL_PROCESS');
5748     IF (l_tapv_rec.trx_status_code = 'SUBMITTED' AND
5749         l_approval_option not in ('WF', 'AME')) THEN
5750 
5751       /*
5752       -- cklee, 12/21/2004
5753       -- START, Code change to enable Business Event bug#4901292
5754       */
5755 
5756       --raise the business event for Validated the Funding Request for checklist items
5757       raise_business_event(p_api_version    => p_api_version,
5758                          p_init_msg_list  => p_init_msg_list,
5759           			     x_return_status  => x_return_status,
5760            		  	     x_msg_count      => x_msg_count,
5761            			     x_msg_data       => x_msg_data,
5762            			     p_khr_id         => l_tapv_rec.khr_id,
5763 		                 p_id             => l_tapv_rec.id,
5764            			     p_event_name     => G_WF_EVT_FUN_LIST_VALIDATED);
5765 
5766       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5767         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5768       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5769         RAISE OKL_API.G_EXCEPTION_ERROR;
5770       END IF;
5771 
5772       /*
5773       -- cklee, 12/21/2004
5774       -- END, Code change to enable Business Event
5775       */
5776 
5777       -- update item function validation results
5778       update_checklist_function(
5779         p_api_version    => p_api_version,
5780         p_init_msg_list  => p_init_msg_list,
5781         x_return_status  => x_return_status,
5782         x_msg_count      => x_msg_count,
5783         x_msg_data       => x_msg_data,
5784         p_fund_req_id    => l_tapv_rec.id);
5785 
5786       If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
5787          raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
5788       Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
5789          raise OKC_API.G_EXCEPTION_ERROR;
5790       End If;
5791 
5792       l_tapv_rec.trx_status_code := 'APPROVED';
5793       l_tapv_rec.DATE_FUNDING_APPROVED := sysdate;
5794 
5795     END IF;
5796 
5797     --Bug# 5690875: Update contract status to 'Incomplete'
5798     --              when Pre-funding request is Approved
5799     IF (l_tapv_rec.funding_type_code = G_PREFUNDING_TYPE_CODE
5800         AND l_tapv_rec.trx_status_code = 'APPROVED') THEN
5801 
5802       OKL_CONTRACT_STATUS_PUB.cascade_lease_status_edit
5803         (p_api_version     => p_api_version,
5804          p_init_msg_list   => p_init_msg_list,
5805          x_return_status   => x_return_status,
5806          x_msg_count       => x_msg_count,
5807          x_msg_data        => x_msg_data,
5808          p_chr_id          => l_tapv_rec.khr_id);
5809       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5810         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5811       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5812         RAISE OKL_API.G_EXCEPTION_ERROR;
5813       END IF;
5814 
5815     END IF;
5816 
5817     -- smadhava - Bug#5200033 - Added - Start
5818     -- Round the amount to the System Options setup
5819     l_tapv_rec.AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => l_tapv_rec.AMOUNT
5820                                                         , p_currency_code => l_tapv_rec.CURRENCY_CODE);
5821     -- smadhava - Bug#5200033 - Added - End
5822 
5823 --    OKL_TAP_PVT.update_row(
5824       OKL_TRX_AP_INVOICES_PUB.UPDATE_TRX_AP_INVOICES(
5825       p_api_version   => p_api_version,
5826       p_init_msg_list => p_init_msg_list,
5827       x_return_status => x_return_status,
5828       x_msg_count     => x_msg_count,
5829       x_msg_data      => x_msg_data,
5830       p_tapv_rec      => l_tapv_rec,
5831       x_tapv_rec      => x_tapv_rec);
5832 
5833 
5834     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5835       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5836     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5837       RAISE OKL_API.G_EXCEPTION_ERROR;
5838     END IF;
5839 
5840     -----------------------------------------------------------
5841     -- trigger WF event if l_tapv_rec.trx_status_code = 'SUBMITTED' and
5842     -- profile option is WF or AME
5843     -----------------------------------------------------------
5844     IF (l_tapv_rec.trx_status_code = 'SUBMITTED' AND
5845         l_approval_option in ('WF', 'AME')) THEN
5846 
5847       /*
5848       -- cklee, 12/21/2004
5849       -- START, Code change to enable Business Event bug#4901292
5850       */
5851 
5852       --raise the business event for Validated the Funding Request for checklist items
5853       raise_business_event(p_api_version    => p_api_version,
5854                          p_init_msg_list  => p_init_msg_list,
5855           			     x_return_status  => x_return_status,
5856            		  	     x_msg_count      => x_msg_count,
5857            			     x_msg_data       => x_msg_data,
5858            			     p_khr_id         => l_tapv_rec.khr_id,
5859 		                 p_id             => l_tapv_rec.id,
5860            			     p_event_name     => G_WF_EVT_FUN_LIST_VALIDATED);
5861 
5862       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5863         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5864       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5865         RAISE OKL_API.G_EXCEPTION_ERROR;
5866       END IF;
5867 
5868       /*
5869       -- cklee, 12/21/2004
5870       -- END, Code change to enable Business Event
5871       */
5872 
5873        -- update item function validation results
5874       update_checklist_function(
5875         p_api_version    => p_api_version,
5876         p_init_msg_list  => p_init_msg_list,
5877         x_return_status  => x_return_status,
5878         x_msg_count      => x_msg_count,
5879         x_msg_data       => x_msg_data,
5880         p_fund_req_id    => l_tapv_rec.id);
5881 
5882       If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
5883          raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
5884       Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
5885          raise OKC_API.G_EXCEPTION_ERROR;
5886       End If;
5887 
5888      OKL_FUNDING_WF.raise_approval_event(
5889                            p_api_version   => p_api_version,
5890                            p_init_msg_list => p_init_msg_list,
5891                            x_return_status => x_return_status,
5892                            x_msg_count     => x_msg_count,
5893                            x_msg_data      => x_msg_data,
5894                            p_funding_id    => l_tapv_rec.id);
5895 
5896     -----------------------------------------------------------
5897     -- trigger post activities if l_tapv_rec.trx_status_code = 'APPROVED' and
5898     -- profile option is NOT WF or AME
5899     -----------------------------------------------------------
5900     ELSIF (l_tapv_rec.trx_status_code = 'APPROVED' AND
5901         l_approval_option not in ('WF', 'AME')) THEN
5902 
5903 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5904 /*    --- vpanwar 21/02/2007 Added
5905     OPEN funding_line_csr(l_tapv_rec.id);
5906     LOOP
5907     FETCH funding_line_csr into funding_line_id;
5908 
5909     EXIT WHEN funding_line_csr%NOTFOUND;
5910 */
5911 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5912 
5913       CREATE_ACCOUNTING_DIST(p_api_version   => p_api_version,
5914                            p_init_msg_list => p_init_msg_list,
5915                            x_return_status => x_return_status,
5916                            x_msg_count     => x_msg_count,
5917                            x_msg_data      => x_msg_data,
5918                            p_status        => l_tapv_rec.trx_status_code,
5919                            p_fund_id       => l_tapv_rec.id);--,
5920 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5921 --                           p_fund_line_id  => funding_line_id);
5922 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5923 
5924       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5925         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5926       ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
5927         RAISE OKL_API.G_EXCEPTION_ERROR;
5928       END IF;
5929 
5930 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5931 --    END LOOP;
5932 --    CLOSE funding_line_csr;
5933 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5934     --- vpanwar 21/02/2007 End
5935 
5936     -------------------------------------------------------------
5937       -- create subsidy entries for 11.5.10
5938       -------------------------------------------------------------
5939       IF (l_tapv_rec.FUNDING_TYPE_CODE = OKL_FUNDING_PVT.G_ASSET_TYPE_CODE) THEN
5940 
5941         create_fund_asset_subsidies(p_api_version   => p_api_version,
5942                            p_init_msg_list => p_init_msg_list,
5943                            x_return_status => x_return_status,
5944                            x_msg_count     => x_msg_count,
5945                            x_msg_data      => x_msg_data,
5946                            p_status        => l_tapv_rec.trx_status_code,
5947                            p_fund_id       => l_tapv_rec.id);
5948 
5949         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5950           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5951         ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
5952           RAISE OKL_API.G_EXCEPTION_ERROR;
5953         END IF;
5954       END IF;
5955     END IF;
5956 
5957 -- funding request checklist enhancement for 11.5.9
5958   IF (l_tapv_rec.funding_type_code <> G_ASSET_SUBSIDY AND
5959       l_tapv_rec.trx_status_code = 'ENTERED') THEN
5960 --start modified abhsaxen changing l_tapv_rec.id in VARCHAR2 for Performance
5961     open c_chklst_exists(TO_CHAR(l_tapv_rec.id));
5962     fetch c_chklst_exists into l_dummy;
5963     l_row_not_found := c_chklst_exists%NOTFOUND;
5964     close c_chklst_exists;
5965 
5966     IF (l_row_not_found) THEN
5967 
5968       create_funding_chklst_tpl(
5969         p_api_version   => p_api_version,
5970         p_init_msg_list => p_init_msg_list,
5971         x_return_status => x_return_status,
5972         x_msg_count     => x_msg_count,
5973         x_msg_data      => x_msg_data,
5974         p_chr_id        => l_tapv_rec.khr_id,
5975         p_fund_req_id   => l_tapv_rec.id);
5976 
5977       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5978         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5979       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5980         RAISE OKL_API.G_EXCEPTION_ERROR;
5981       END IF;
5982     END IF;
5983 
5984   END IF;
5985 
5986 -- funding request checklist enhancement for 11.5.9
5987 
5988    /*
5989    -- vthiruva, 08/31/2004
5990    -- START, Code change to enable Business Event
5991    */
5992     IF(p_tapv_rec.trx_status_code = 'ENTERED')THEN
5993     --raise the business event for update funding request
5994     --if transaction status code is ENTERED
5995         l_event_name  := G_WF_EVT_FUN_REQ_UPDATED;
5996         l_raise_business_event := OKL_API.G_TRUE;
5997 
5998     ELSIF(p_tapv_rec.trx_status_code = 'CANCELED')THEN
5999     --raise the business event for cancel funding request
6000     --if transaction status code is CANCELED
6001     --In DO the value of trx_status_code is being set to CANCELED
6002     --in place of CANCELLED. Hence changed the spelling in the above check.
6003         l_event_name  := G_WF_EVT_FUN_REQ_CANCELLED;
6004         l_raise_business_event := OKL_API.G_TRUE;
6005 
6006     ELSIF(p_tapv_rec.trx_status_code = 'SUBMITTED')THEN
6007     --raise the business event for submit funding request
6008     --if transaction status code is SUBMITTED
6009         l_event_name  := G_WF_EVT_FUN_REQ_SUBMITTED;
6010         l_raise_business_event := OKL_API.G_TRUE;
6011     END If;
6012 
6013     IF(l_raise_business_event = OKL_API.G_TRUE AND l_event_name IS NOT NULL) THEN
6014         --call to raise the appropriate business event
6015     	raise_business_event(p_api_version    => p_api_version,
6016                              p_init_msg_list  => p_init_msg_list,
6017 			     x_return_status  => x_return_status,
6018 			     x_msg_count      => x_msg_count,
6019 			     x_msg_data       => x_msg_data,
6020 			     p_khr_id         => p_tapv_rec.khr_id,
6021 			     p_id             => p_tapv_rec.id,
6022 			     p_event_name     => l_event_name);
6023 
6024         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6025            RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6026         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6027            RAISE OKL_API.G_EXCEPTION_ERROR;
6028         END IF;
6029     END IF;
6030 
6031    /*
6032    -- vthiruva, 08/31/2004
6033    -- END, Code change to enable Business Event
6034    */
6035 
6036 /*** End API body ******************************************************/
6037 
6038   -- Get message count and if count is 1, get message info
6039 	FND_MSG_PUB.Count_And_Get
6040     (p_count          =>      x_msg_count,
6041      p_data           =>      x_msg_data);
6042 
6043 EXCEPTION
6044   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6045     ROLLBACK TO UPDATE_FUNDING_HEADER_PVT;
6046     x_return_status := OKL_API.G_RET_STS_ERROR;
6047     FND_MSG_PUB.Count_And_Get
6048       (p_count         =>      x_msg_count,
6049        p_data          =>      x_msg_data);
6050 
6051   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6052     ROLLBACK TO UPDATE_FUNDING_HEADER_PVT;
6053     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6054     FND_MSG_PUB.Count_And_Get
6055       (p_count         =>      x_msg_count,
6056        p_data          =>      x_msg_data);
6057 
6058   WHEN OTHERS THEN
6059 	ROLLBACK TO UPDATE_FUNDING_HEADER_PVT;
6060       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6061       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6062                           p_msg_name      => G_UNEXPECTED_ERROR,
6063                           p_token1        => G_SQLCODE_TOKEN,
6064                           p_token1_value  => SQLCODE,
6065                           p_token2        => G_SQLERRM_TOKEN,
6066                           p_token2_value  => SQLERRM);
6067       FND_MSG_PUB.Count_And_Get
6068         (p_count         =>      x_msg_count,
6069          p_data          =>      x_msg_data);
6070 
6071   END update_funding_header;
6072 
6073 ----------------------------------------------------------------------------
6074 
6075 PROCEDURE create_funding_lines(
6076   p_api_version                  IN NUMBER
6077  ,p_init_msg_list                IN VARCHAR2
6078  ,x_return_status                OUT NOCOPY VARCHAR2
6079  ,x_msg_count                    OUT NOCOPY NUMBER
6080  ,x_msg_data                     OUT NOCOPY VARCHAR2
6081  ,p_tplv_tbl                     IN tplv_tbl_type
6082  ,x_tplv_tbl                     OUT NOCOPY tplv_tbl_type
6083 )
6084 IS
6085   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_LINES';
6086 
6087   l_api_version     CONSTANT NUMBER       := 1.0;
6088   l_tplv_tbl                 tplv_tbl_type := p_tplv_tbl;
6089   i                          NUMBER;
6090   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6091 
6092   -- smadhava - Bug#5200033 - Added - Start
6093   l_currency_code OKL_TRX_AP_INVOICES_B.CURRENCY_CODE%TYPE;
6094 
6095    --- vpanwar added 28/02/2007 start
6096   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
6097   --- vpanwar added 28/02/2007 end
6098 
6099   -- Cursor to get the currency code from the header record
6100   CURSOR c_get_currency_code(cp_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6101     SELECT CURRENCY_CODE
6102       FROM OKL_TRX_AP_INVOICES_B
6103      WHERE ID = cp_tap_id;
6104   -- smadhava - Bug#5200033 - Added - End
6105 
6106   -- vpanwar Added --28/02/2007 -start
6107     CURSOR c_get_khr_id(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6108     SELECT KHR_ID
6109       FROM OKL_TRX_AP_INVOICES_B
6110      WHERE ID = p_tap_id;
6111   -- vpanwar Added --28/02/2007 -end
6112 
6113   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
6114   CURSOR c_get_max_line_number(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6115     SELECT MAX(LINE_NUMBER)
6116 	    FROM OKL_TXL_AP_INV_LNS_B
6117 	    WHERE TAP_ID = P_TAP_ID;
6118 
6119   l_max_line_number NUMBER := 0;
6120   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
6121 
6122 BEGIN
6123   -- Set API savepoint
6124   SAVEPOINT CREATE_FUNDING_LINES_PVT;
6125 
6126   -- Check for call compatibility
6127   IF (NOT FND_API.Compatible_API_Call (l_api_version,
6128                                 	   p_api_version,
6129                                 	   l_api_name,
6130                                 	   G_PKG_NAME ))
6131   THEN
6132     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6133   END IF;
6134 
6135   -- Initialize message list if requested
6136   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
6137       FND_MSG_PUB.initialize;
6138 	END IF;
6139 
6140   -- Initialize API status to success
6141   x_return_status := OKL_API.G_RET_STS_SUCCESS;
6142 
6143 
6144 
6145 /*** Begin API body ****************************************************/
6146 
6147 
6148     -- OKL_TPL_PVT is not belongs to our dev team, so we have to write valid code here
6149     -- validate kle_id for passed in pl/sql table before insert into DB
6150     l_return_status := validate_table_kle_id(p_tplv_tbl);
6151     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
6152       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6153         x_return_status := l_return_status;
6154       END IF;
6155       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6156     END IF;
6157 
6158     -- check kle_id and sty_id if applicable
6159     IF (p_tplv_tbl.COUNT > 0) THEN
6160       i := p_tplv_tbl.FIRST;
6161 
6162     -- smadhava - Bug#5200033 - Added - Start
6163     -- get the currency code from the header record
6164     OPEN c_get_currency_code(p_tplv_tbl(i).TAP_ID);
6165       FETCH c_get_currency_code INTO l_currency_code;
6166     CLOSE c_get_currency_code;
6167     -- smadhava - Bug#5200033 - Added - End
6168 
6169   --- vpanwar added 28/02/2007 start
6170     OPEN c_get_khr_id(p_tplv_tbl(i).TAP_ID);
6171       FETCH  c_get_khr_id INTO l_khr_id;
6172     CLOSE c_get_khr_id;
6173   --- vpanwar added 28/02/2007 end
6174 
6175   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
6176     OPEN c_get_max_line_number(p_tplv_tbl(i).TAP_ID);
6177 	FETCH c_get_max_line_number INTO l_max_line_number;
6178     CLOSE c_get_max_line_number;
6179 
6180     IF (l_max_line_number IS NULL) THEN
6181 	l_max_line_number := 0;
6182     END IF;
6183 
6184   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - end
6185 
6186 
6187       LOOP
6188 
6189      --- vpanwar added 28/02/2007 start
6190         l_tplv_tbl(i).KHR_ID := l_khr_id;
6191      --- vpanwar added 28/02/2007 start
6192 
6193 
6194 --
6195 -- default DISBURSEMENT_BASIS_CODE = 'BILL_DATE';
6196 --
6197         l_tplv_tbl(i).DISBURSEMENT_BASIS_CODE := 'BILL_DATE';
6198 --
6199 --
6200 --
6201   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
6202 	IF (l_tplv_tbl(i).line_number = OKL_API.G_MISS_NUM OR l_tplv_tbl(i).line_number IS NULL) THEN
6203 		l_max_line_number	:=	l_max_line_number + 1;
6204 		l_tplv_tbl(i).line_number	:=	l_max_line_number;
6205 	END IF;
6206   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - end
6207 
6208         -- fixed bug#3338910
6209         l_return_status := populate_sty_id(l_tplv_tbl(i));
6210 
6211         IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
6212           IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6213             x_return_status := l_return_status;
6214           END IF;
6215           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6216         END IF;
6217 
6218 
6219         l_return_status := validate_line_attributes(p_tplv_tbl(i), 'C');
6220 
6221         IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6222           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6223         ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
6224           RAISE OKL_API.G_EXCEPTION_ERROR;
6225         END IF;
6226 
6227     -- smadhava - Bug#5200033 - Added - Start
6228     -- Round the amount to the System Options setup
6229     l_tplv_tbl(i).AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => p_tplv_tbl(i).AMOUNT
6230                                                            , p_currency_code => l_currency_code);
6231     -- smadhava - Bug#5200033 - Added - End
6232 
6233 
6234         EXIT WHEN (i = p_tplv_tbl.LAST);
6235         i := p_tplv_tbl.NEXT(i);
6236 
6237       END LOOP;
6238     END IF;
6239 
6240 --    OKL_TPL_PVT.insert_row(
6241       OKL_TXL_AP_INV_LNS_PUB.INSERT_TXL_AP_INV_LNS(
6242       p_api_version   => p_api_version,
6243       p_init_msg_list => p_init_msg_list,
6244       x_return_status => x_return_status,
6245       x_msg_count     => x_msg_count,
6246       x_msg_data      => x_msg_data,
6247       p_tplv_tbl      => l_tplv_tbl,
6248       x_tplv_tbl      => x_tplv_tbl);
6249 
6250     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6251       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6252     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6253       RAISE OKL_API.G_EXCEPTION_ERROR;
6254     END IF;
6255 
6256 
6257 -- sync funding header amount
6258     SYNC_HEADER_AMOUNT(
6259       p_api_version   => p_api_version,
6260       p_init_msg_list => p_init_msg_list,
6261       x_return_status => x_return_status,
6262       x_msg_count     => x_msg_count,
6263       x_msg_data      => x_msg_data,
6264       p_tplv_tbl      => x_tplv_tbl);
6265 
6266     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6267       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6268     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6269       RAISE OKL_API.G_EXCEPTION_ERROR;
6270     END IF;
6271 
6272 
6273 /*** End API body ******************************************************/
6274 
6275   -- Get message count and if count is 1, get message info
6276 	FND_MSG_PUB.Count_And_Get
6277     (p_count          =>      x_msg_count,
6278      p_data           =>      x_msg_data);
6279 
6280 EXCEPTION
6281   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6282 
6283     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6284     x_return_status := OKL_API.G_RET_STS_ERROR;
6285     FND_MSG_PUB.Count_And_Get
6286       (p_count         =>      x_msg_count,
6287        p_data          =>      x_msg_data);
6288 
6289 
6290   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6291     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6292     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6293     FND_MSG_PUB.Count_And_Get
6294       (p_count         =>      x_msg_count,
6295        p_data          =>      x_msg_data);
6296 
6297   WHEN OTHERS THEN
6298 	ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6299       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6300       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6301                           p_msg_name      => G_UNEXPECTED_ERROR,
6302                           p_token1        => G_SQLCODE_TOKEN,
6303                           p_token1_value  => SQLCODE,
6304                           p_token2        => G_SQLERRM_TOKEN,
6305                           p_token2_value  => SQLERRM);
6306       FND_MSG_PUB.Count_And_Get
6307         (p_count         =>      x_msg_count,
6308          p_data          =>      x_msg_data);
6309 
6310   END create_funding_lines;
6311 
6312 ----------------------------------------------------------------------------
6313 
6314 ----------------------------------------------------------------------------
6315 -- dcshanmu - Added - Qucik Fund performance fix - start
6316 ----------------------------------------------------------------------------
6317 PROCEDURE create_funding_lines(
6318   p_api_version                  IN NUMBER
6319  ,p_init_msg_list                IN VARCHAR2
6320  ,x_return_status                OUT NOCOPY VARCHAR2
6321  ,x_msg_count                    OUT NOCOPY NUMBER
6322  ,x_msg_data                     OUT NOCOPY VARCHAR2
6323  ,p_hdr_id				IN NUMBER
6324  ,p_khr_id				IN NUMBER
6325  ,p_vendor_site_id		IN NUMBER
6326  ,x_tplv_tbl                     OUT NOCOPY tplv_tbl_type
6327 )
6328 IS
6329   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_LINES';
6330 
6331   l_api_version     CONSTANT NUMBER       := 1.0;
6332   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6333   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
6334   l_vendor_site_id OKL_TRX_AP_INVOICES_B.IPVS_ID%TYPE;
6335   l_vendor_id OKL_TRX_AP_INVOICES_B.VENDOR_ID%TYPE;
6336 
6337   -- cursor to fetch assets, which has supplier invoice for a given khr_id and vendor_site_id
6338   CURSOR c_get_assets(p_vendor_site_id OKL_TRX_AP_INVOICES_B.IPVS_ID%TYPE,
6339 	p_khr_id OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE) IS
6340     SELECT a.cle_id cle_id,
6341            a.chr_id chr_id,
6342            a.asset_number kle_num,
6343            a.description kle_name,
6344            okl_funding_pvt.get_contract_line_amt(
6345              a.chr_id,
6346              a.cle_id,
6347              p_vendor_site_id
6348            ) kle_amt
6349       FROM okl_assets_lov_uv a,
6350            okc_k_party_roles_b cpl,
6351            okc_k_lines_b LN,
6352            okx_vendor_sites_v sites
6353      WHERE a.chr_id = p_khr_id
6354        AND cpl.rle_code = 'OKL_VENDOR'
6355        AND cpl.chr_id IS NULL
6356        AND cpl.dnz_chr_id = a.chr_id
6357        AND cpl.object1_id1 = TO_CHAR(sites.vendor_id)
6358        AND sites.id1 = p_vendor_site_id
6359        AND cpl.object1_id2 = '#'
6360        AND cpl.cle_id = LN.ID
6361        AND LN.cle_id = a.cle_id;
6362 
6363   -- cursor to fetch org_id from khr_id
6364   CURSOR c_get_org_id(p_khr_id OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE) IS
6365 	SELECT AUTHORING_ORG_ID
6366 	FROM OKC_K_HEADERS_ALL_B
6367 	WHERE ID = p_khr_id;
6368 
6369 assets_rec c_get_assets%ROWTYPE;
6370 TYPE assets_tbl IS TABLE OF assets_rec%TYPE INDEX BY BINARY_INTEGER;
6371 
6372   l_assets_tbl assets_tbl;
6373   l_sty_id	NUMBER := 0;
6374   l_tplv_tbl                 tplv_tbl_type;
6375   cnt	NUMBER := 0;
6376   l_org_id	NUMBER := 0;
6377 
6378  BEGIN
6379 	SAVEPOINT CREATE_FUNDING_LINES_PVT;
6380 
6381 	x_return_status := OKL_API.G_RET_STS_SUCCESS;
6382 
6383 	OPEN c_get_assets(p_vendor_site_id, p_khr_id);
6384 		FETCH c_get_assets BULK COLLECT INTO l_assets_tbl;
6385 	CLOSE c_get_assets;
6386 
6387 	OPEN c_get_org_id(p_khr_id);
6388 		FETCH c_get_org_id INTO l_org_id;
6389 	CLOSE c_get_org_id;
6390 
6391 	IF l_assets_tbl.COUNT > 0 THEN
6392 		-- populate l_tplv_tbl
6393 		FOR i in l_assets_tbl.FIRST..l_assets_tbl.LAST LOOP
6394 			-- increment count and assign to line numbers
6395 			l_tplv_tbl(i).tap_id			:=	p_hdr_id;
6396 			cnt := cnt + 1;
6397 			l_tplv_tbl(i).line_number		:=	cnt;
6398 			l_tplv_tbl(i).kle_id			:=	l_assets_tbl(i).cle_id;
6399 			l_tplv_tbl(i).inv_distr_line_code	:=	'ITEM';
6400 			l_tplv_tbl(i).amount			:=	l_assets_tbl(i).kle_amt;
6401 			l_tplv_tbl(i).org_id			:=	l_org_id;
6402 			l_tplv_tbl(i).description			:=	l_assets_tbl(i).kle_name;
6403 		END LOOP;
6404 
6405 		-- call create_funding_lines proc with table
6406 		create_funding_lines(
6407 			  p_api_version
6408 			 ,p_init_msg_list
6409 			 ,x_return_status
6410 			 ,x_msg_count
6411 			 ,x_msg_data
6412 			 ,l_tplv_tbl
6413 			 ,x_tplv_tbl);
6414 	END IF;
6415 
6416 
6417 	IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6418 		RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6419 	ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6420 		RAISE OKL_API.G_EXCEPTION_ERROR;
6421 	END IF;
6422 
6423   -- Get message count and if count is 1, get message info
6424   FND_MSG_PUB.Count_And_Get
6425     (p_count          =>      x_msg_count,
6426      p_data           =>      x_msg_data);
6427 
6428 EXCEPTION
6429   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6430 
6431     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6432     x_return_status := OKL_API.G_RET_STS_ERROR;
6433     FND_MSG_PUB.Count_And_Get
6434       (p_count         =>      x_msg_count,
6435        p_data          =>      x_msg_data);
6436 
6437 
6438   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6439     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6440     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6441     FND_MSG_PUB.Count_And_Get
6442       (p_count         =>      x_msg_count,
6443        p_data          =>      x_msg_data);
6444 
6445   WHEN OTHERS THEN
6446 	ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6447       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6448       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6449                           p_msg_name      => G_UNEXPECTED_ERROR,
6450                           p_token1        => G_SQLCODE_TOKEN,
6451                           p_token1_value  => SQLCODE,
6452                           p_token2        => G_SQLERRM_TOKEN,
6453                           p_token2_value  => SQLERRM);
6454       FND_MSG_PUB.Count_And_Get
6455         (p_count         =>      x_msg_count,
6456          p_data          =>      x_msg_data);
6457 
6458 
6459   END create_funding_lines;
6460 ----------------------------------------------------------------------------
6461 -- dcshanmu - Added - Qucik Fund performance fix - end
6462 ----------------------------------------------------------------------------
6463 
6464 PROCEDURE update_funding_lines(
6465   p_api_version                  IN NUMBER
6466  ,p_init_msg_list                IN VARCHAR2
6467  ,x_return_status                OUT NOCOPY VARCHAR2
6468  ,x_msg_count                    OUT NOCOPY NUMBER
6469  ,x_msg_data                     OUT NOCOPY VARCHAR2
6470  ,p_tplv_tbl                     IN tplv_tbl_type
6471  ,x_tplv_tbl                     OUT NOCOPY tplv_tbl_type
6472 )
6473 IS
6474   l_api_name        CONSTANT VARCHAR2(30) := 'UPDATE_FUNDING_LINES';
6475   l_api_version     CONSTANT NUMBER       := 1.0;
6476   l_tplv_tbl                 tplv_tbl_type := p_tplv_tbl;
6477   i                          NUMBER;
6478   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6479   l_sty_id number;
6480 
6481   --- vpanwar added 28/02/2007 start
6482   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
6483   --- vpanwar added 28/02/2007 end
6484 
6485 cursor c_sty_id(p_tpl_id number) is
6486 select sty_id
6487 from okl_txl_ap_inv_lns_b
6488 where id = p_tpl_id
6489 ;
6490 
6491   -- smadhava - Bug#5200033 - Added - Start
6492   l_currency_code OKL_TRX_AP_INVOICES_B.CURRENCY_CODE%TYPE;
6493 
6494   -- Cursor to get the currency code from the header record
6495   CURSOR c_get_currency_code(cp_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6496     SELECT CURRENCY_CODE
6497       FROM OKL_TRX_AP_INVOICES_B
6498      WHERE ID = cp_tap_id;
6499   -- smadhava - Bug#5200033 - Added - End
6500 
6501  -- vpanwar Added --28/02/2007 -start
6502     CURSOR c_get_khr_id(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6503     SELECT KHR_ID
6504       FROM OKL_TRX_AP_INVOICES_B
6505      WHERE ID = p_tap_id;
6506   -- vpanwar Added --28/02/2007 -end
6507 
6508 BEGIN
6509   -- Set API savepoint
6510   SAVEPOINT UPDATE_FUNDING_LINES_PVT;
6511 
6512   -- Check for call compatibility
6513   IF (NOT FND_API.Compatible_API_Call (l_api_version,
6514                                        p_api_version,
6515                                 	   l_api_name,
6516                                 	   G_PKG_NAME ))
6517   THEN
6518     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6519   END IF;
6520 
6521   -- Initialize message list if requested
6522   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
6523       FND_MSG_PUB.initialize;
6524 	END IF;
6525 
6526   -- Initialize API status to success
6527   x_return_status := OKL_API.G_RET_STS_SUCCESS;
6528 
6529 /*** Begin API body ****************************************************/
6530 
6531     -- check kle_id and sty_id if applicable
6532     -- OKL_TPL_PVT is not belongs to our dev team, so we have to write validate code here
6533     IF (l_tplv_tbl.COUNT > 0) THEN
6534       i := p_tplv_tbl.FIRST;
6535 
6536     -- smadhava - Bug#5200033 - Added - Start
6537     -- get the currency code from the header record
6538     OPEN c_get_currency_code(p_tplv_tbl(i).TAP_ID);
6539       FETCH c_get_currency_code INTO l_currency_code;
6540     CLOSE c_get_currency_code;
6541     -- smadhava - Bug#5200033 - Added - End
6542 
6543        --- vpanwar added 28/02/2007 start
6544     OPEN c_get_khr_id(p_tplv_tbl(i).TAP_ID);
6545       FETCH  c_get_khr_id INTO l_khr_id;
6546     CLOSE c_get_khr_id;
6547     --- vpanwar added 28/02/2007 end
6548 
6549 
6550       LOOP
6551 
6552    --- vpanwar added 28/02/2007 start
6553         l_tplv_tbl(i).KHR_ID := l_khr_id;
6554       --- vpanwar added 28/02/2007 start
6555 
6556 -- cklee: 09/16/2004
6557         IF (l_tplv_tbl(i).sty_id IS NULL or l_tplv_tbl(i).sty_id = OKL_API.G_MISS_NUM) THEN
6558 
6559           open c_sty_id(l_tplv_tbl(i).id);
6560           fetch c_sty_id into l_sty_id;
6561           IF c_sty_id%found THEN
6562             l_tplv_tbl(i).sty_id := l_sty_id;
6563           END IF;
6564           close c_sty_id;
6565         END IF;
6566 
6567         l_return_status := validate_line_attributes(l_tplv_tbl(i), 'U');
6568         IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6569           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6570         ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
6571           RAISE OKL_API.G_EXCEPTION_ERROR;
6572         END IF;
6573 
6574     -- smadhava - Bug#5200033 - Added - Start
6575     -- Round the amount to the System Options setup
6576     l_tplv_tbl(i).AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => p_tplv_tbl(i).AMOUNT
6577                                                            , p_currency_code => l_currency_code);
6578     -- smadhava - Bug#5200033 - Added - End
6579 
6580         EXIT WHEN (i = l_tplv_tbl.LAST);
6581         i := l_tplv_tbl.NEXT(i);
6582 
6583       END LOOP;
6584     END IF;
6585 
6586 --    OKL_TPL_PVT.update_row(
6587       OKL_TXL_AP_INV_LNS_PUB.UPDATE_TXL_AP_INV_LNS(
6588       p_api_version   => p_api_version,
6589       p_init_msg_list => p_init_msg_list,
6590       x_return_status => x_return_status,
6591       x_msg_count     => x_msg_count,
6592       x_msg_data      => x_msg_data,
6593       p_tplv_tbl      => l_tplv_tbl,
6594       x_tplv_tbl      => x_tplv_tbl);
6595 
6596     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6597       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6598     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6599       RAISE OKL_API.G_EXCEPTION_ERROR;
6600     END IF;
6601 
6602 -- sync funding header amount
6603     SYNC_HEADER_AMOUNT(
6604       p_api_version   => p_api_version,
6605       p_init_msg_list => p_init_msg_list,
6606       x_return_status => x_return_status,
6607       x_msg_count     => x_msg_count,
6608       x_msg_data      => x_msg_data,
6609       p_tplv_tbl      => l_tplv_tbl);
6610 
6611     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6612       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6613     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6614       RAISE OKL_API.G_EXCEPTION_ERROR;
6615     END IF;
6616 
6617 
6618 /*** End API body ******************************************************/
6619 
6620   -- Get message count and if count is 1, get message info
6621 	FND_MSG_PUB.Count_And_Get
6622     (p_count          =>      x_msg_count,
6623      p_data           =>      x_msg_data);
6624 
6625 EXCEPTION
6626   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6627     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
6628     x_return_status := OKL_API.G_RET_STS_ERROR;
6629     FND_MSG_PUB.Count_And_Get
6630       (p_count         =>      x_msg_count,
6631        p_data          =>      x_msg_data);
6632 
6633   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6634     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
6635     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6636     FND_MSG_PUB.Count_And_Get
6637       (p_count         =>      x_msg_count,
6638        p_data          =>      x_msg_data);
6639 
6640 
6641   WHEN OTHERS THEN
6642     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
6643     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6644     OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6645                         p_msg_name      => G_UNEXPECTED_ERROR,
6646                         p_token1        => G_SQLCODE_TOKEN,
6647                         p_token1_value  => SQLCODE,
6648                         p_token2        => G_SQLERRM_TOKEN,
6649                         p_token2_value  => SQLERRM);
6650     FND_MSG_PUB.Count_And_Get
6651       (p_count         =>      x_msg_count,
6652        p_data          =>      x_msg_data);
6653 
6654 END update_funding_lines;
6655 ----------------------------------------------------------------------------
6656 PROCEDURE create_funding_assets(
6657     p_api_version                  IN NUMBER
6658    ,p_init_msg_list                IN VARCHAR2
6659    ,x_return_status                OUT NOCOPY VARCHAR2
6660    ,x_msg_count                    OUT NOCOPY NUMBER
6661    ,x_msg_data                     OUT NOCOPY VARCHAR2
6662    ,p_fund_id                      IN NUMBER
6663  )
6664 IS
6665   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_ASSETS';
6666 
6667   l_api_version     CONSTANT NUMBER       := 1.0;
6668   l_tplv_tbl        tplv_tbl_type;
6669   x_tplv_tbl        tplv_tbl_type;
6670   i                 NUMBER;
6671   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6672   --l_khr_id          NUMBER;
6673   l_org_id          NUMBER;
6674   l_kle_id          NUMBER;
6675   l_asset_cost      NUMBER;
6676   l_asset_number    VARCHAR2(150);
6677 
6678 
6679   -- smadhava - Bug#5200033 - Modified - Start
6680   l_currency_code OKL_TRX_AP_INVOICES_B.CURRENCY_CODE%TYPE;
6681 
6682   --- vpanwar added 28/02/2007 start
6683   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
6684   --- vpanwar added 28/02/2007 end
6685 
6686 
6687   -- Modifed cursor to get the currency code
6688   CURSOR c (p_fund_id  NUMBER)
6689   IS
6690   select a.org_id
6691        , a.currency_code
6692   from   okl_trx_ap_invoices_b a
6693   where  a.id = p_fund_id
6694   and    a.funding_type_code = 'ASSET'
6695   ;
6696   -- smadhava - Bug#5200033 - Modified - End
6697 
6698 
6699   --veramach 5600694  start modified cursor c2
6700   /*CURSOR c2 (p_fund_id NUMBER)
6701   IS
6702   select a.cle_id
6703          ,a.KLE_AMT
6704   from   okl_fund_assets_lov_uv a
6705   where  a.FUND_ID = p_fund_id
6706   and    a.KLE_AMT > 0
6707   and    NOT EXISTS
6708           (select 1
6709            from okl_txl_ap_inv_lns_b b
6710            where a.FUND_ID = b.tap_id
6711            and   a.cle_id = b.kle_id)
6712   ;*/
6713  	CURSOR c2 (p_fund_id NUMBER)
6714  	IS
6715  	/*veramach 29-Jun-2007 bug#5600694 commented to improve the performance */
6716  	 --SELECT cle_id, KLE_AMT
6717  	 --FROM
6718  	 --(
6719  	   select a.cle_id,
6720  	       (select OKL_FUNDING_PVT.get_contract_line_amt(a.CHR_ID, a.CLE_ID, b.ipvs_id)  from dual) KLE_AMT
6721  	   from   OKL_ASSETS_LOV_UV A,
6722  	   OKL_TRX_AP_INVOICES_B b
6723  	   WHERE  a.chr_id = b.khr_id
6724  	   AND b.ID = p_fund_id
6725  	   and    NOT EXISTS
6726  	          (select 1
6727  	           from okl_txl_ap_inv_lns_b c
6728  	           where b.ID = c.tap_id
6729  	           and   a.cle_id = c.kle_id)
6730 	  -- )
6731  	   --WHERE    KLE_AMT > 0
6732  	   ;
6733  	   --veramach 5600694  end
6734 
6735    -- vpanwar Added --28/02/2007 -start
6736     CURSOR c_get_khr_id(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6737     SELECT KHR_ID
6738       FROM OKL_TRX_AP_INVOICES_B
6739      WHERE ID = p_tap_id;
6740   -- vpanwar Added --28/02/2007 -end
6741 
6742 
6743 BEGIN
6744   -- Set API savepoint
6745   SAVEPOINT CREATE_FUNDING_ASSETS_PVT;
6746 
6747   -- Check for call compatibility
6748   IF (NOT FND_API.Compatible_API_Call (l_api_version,
6749                                 	   p_api_version,
6750                                 	   l_api_name,
6751                                 	   G_PKG_NAME ))
6752   THEN
6753     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6754   END IF;
6755 
6756   -- Initialize message list if requested
6757   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
6758       FND_MSG_PUB.initialize;
6759 	END IF;
6760 
6761   -- Initialize API status to success
6762   x_return_status := OKL_API.G_RET_STS_SUCCESS;
6763 
6764 
6765 /*** Begin API body ****************************************************/
6766 
6767   -- smadhava - Bug#5200033 - Modified - Start
6768   -- get org_id and Currency code
6769 
6770   OPEN c(p_fund_id);
6771   FETCH c INTO l_org_id, l_currency_code;
6772   CLOSE c;
6773   -- smadhava - Bug#5200033 - Modified - End
6774 
6775 -- fill in l_tplv_tbl
6776   OPEN c2(p_fund_id);
6777   i := 0;
6778   LOOP
6779 
6780        FETCH c2 into
6781                 l_kle_id,
6782                 l_asset_cost;
6783        EXIT WHEN c2%NOTFOUND;
6784 	     IF l_asset_cost > 0 THEN  --for bug#5600694
6785 
6786        l_tplv_tbl(i).inv_distr_line_code := 'ITEM';
6787        l_tplv_tbl(i).tap_id := p_fund_id;
6788 
6789        --- vpanwar added 28/02/2007 start
6790        OPEN c_get_khr_id(l_tplv_tbl(i).TAP_ID);
6791 	FETCH  c_get_khr_id INTO l_khr_id;
6792        CLOSE c_get_khr_id;
6793 
6794         l_tplv_tbl(i).KHR_ID := l_khr_id;
6795 
6796        --- vpanwar added 28/02/2007 start
6797 
6798 
6799        l_tplv_tbl(i).kle_id := l_kle_id;
6800        -- smadhava - Bug#5200033 - Modified - Start
6801        -- Round the asset cost to the system options rounding setup
6802        l_tplv_tbl(i).amount := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => l_asset_cost
6803                                                               , p_currency_code => l_currency_code);
6804 --       l_tplv_tbl(i).amount := l_asset_cost;
6805        -- smadhava - Bug#5200033 - Modified - End
6806 
6807        l_tplv_tbl(i).org_id := l_org_id;
6808        l_tplv_tbl(i).line_number := i+1;
6809        l_tplv_tbl(i).DISBURSEMENT_BASIS_CODE := 'BILL_DATE';
6810 
6811         -- fixed bug#3338910
6812         l_return_status := populate_sty_id(l_tplv_tbl(i));
6813 
6814         IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
6815           IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6816             x_return_status := l_return_status;
6817           END IF;
6818           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6819         END IF;
6820 
6821 
6822        i := i + 1;
6823 
6824     END IF;
6825   END LOOP;
6826 
6827   CLOSE c2;
6828 
6829   IF (l_tplv_tbl.COUNT > 0) THEN
6830 
6831 --    OKL_TPL_PVT.insert_row(
6832     OKL_TXL_AP_INV_LNS_PUB.INSERT_TXL_AP_INV_LNS(
6833       p_api_version   => p_api_version,
6834       p_init_msg_list => p_init_msg_list,
6835       x_return_status => x_return_status,
6836       x_msg_count     => x_msg_count,
6837       x_msg_data      => x_msg_data,
6838       p_tplv_tbl      => l_tplv_tbl,
6839       x_tplv_tbl      => x_tplv_tbl);
6840 
6841 
6842     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6843       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6844     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6845       RAISE OKL_API.G_EXCEPTION_ERROR;
6846     END IF;
6847 
6848 -- sync funding header amount
6849     OKL_FUNDING_PVT.SYNC_HEADER_AMOUNT(
6850       p_api_version   => p_api_version,
6851       p_init_msg_list => p_init_msg_list,
6852       x_return_status => x_return_status,
6853       x_msg_count     => x_msg_count,
6854       x_msg_data      => x_msg_data,
6855       p_tplv_tbl      => x_tplv_tbl);
6856 
6857     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6858       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6859     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6860       RAISE OKL_API.G_EXCEPTION_ERROR;
6861     END IF;
6862 
6863   END IF;
6864 
6865 /*** End API body ******************************************************/
6866 
6867   -- Get message count and if count is 1, get message info
6868 	FND_MSG_PUB.Count_And_Get
6869     (p_count          =>      x_msg_count,
6870      p_data           =>      x_msg_data);
6871 
6872 EXCEPTION
6873   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6874     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
6875     x_return_status := OKL_API.G_RET_STS_ERROR;
6876     FND_MSG_PUB.Count_And_Get
6877       (p_count         =>      x_msg_count,
6878        p_data          =>      x_msg_data);
6879 
6880   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6881     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
6882     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6883 
6884     FND_MSG_PUB.Count_And_Get
6885       (p_count         =>      x_msg_count,
6886        p_data          =>      x_msg_data);
6887 
6888   WHEN OTHERS THEN
6889 	ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
6890       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6891       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6892                           p_msg_name      => G_UNEXPECTED_ERROR,
6893                           p_token1        => G_SQLCODE_TOKEN,
6894                           p_token1_value  => SQLCODE,
6895                           p_token2        => G_SQLERRM_TOKEN,
6896                           p_token2_value  => SQLERRM);
6897       FND_MSG_PUB.Count_And_Get
6898         (p_count         =>      x_msg_count,
6899 
6900          p_data          =>      x_msg_data);
6901 
6902   END create_funding_assets;
6903 ----------------------------------------------------------------------------
6904 
6905  PROCEDURE reverse_funding_requests(
6906     p_api_version                  IN NUMBER
6907    ,p_init_msg_list                IN VARCHAR2
6908    ,x_return_status                OUT NOCOPY VARCHAR2
6909    ,x_msg_count                    OUT NOCOPY NUMBER
6910    ,x_msg_data                     OUT NOCOPY VARCHAR2
6911    ,p_contract_id                  IN NUMBER
6912  )
6913 IS
6914   l_api_name        CONSTANT VARCHAR2(30) := 'reverse_funding_requests';
6915   l_api_version     CONSTANT NUMBER       := 1.0;
6916 
6917   l_tapv_rec        tapv_rec_type;
6918   x_tapv_rec        tapv_rec_type;
6919 
6920   funding_line_id   number;
6921 
6922 
6923   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6924   l_currency_code   okc_k_headers_b.CURRENCY_CODE%TYPE;
6925   l_org_id          okc_k_headers_b.AUTHORING_ORG_ID%TYPE;
6926   l_contract_number okc_k_headers_b.CONTRACT_NUMBER%TYPE;
6927 
6928   l_amount           okl_trx_ap_invoices_b.AMOUNT%TYPE;
6929 
6930   l_ipvs_id          okl_trx_ap_invoices_b.IPVS_ID%TYPE;
6931   l_vendor_site_code OKX_VENDOR_SITES_V.NAME%TYPE;
6932 
6933   l_PAY_GROUP_LOOKUP_CODE  OKL_TRX_AP_INVOICES_B.PAY_GROUP_LOOKUP_CODE%TYPE;
6934   l_NETTABLE_YN            OKL_TRX_AP_INVOICES_B.NETTABLE_YN%TYPE;
6935 
6936 
6937     CURSOR cu (p_id NUMBER)
6938     IS
6939       SELECT h.PAY_GROUP_LOOKUP_CODE,
6940              h.NETTABLE_YN
6941         FROM OKL_TRX_AP_INVOICES_B h
6942        WHERE h.id = p_id
6943     ;
6944 
6945   --
6946   CURSOR c (p_contract_id  NUMBER)
6947   IS
6948   select a.AUTHORING_ORG_ID,
6949          a.CURRENCY_CODE,
6950          a.CONTRACT_NUMBER
6951   from   okc_k_headers_b a
6952   where  a.id = p_contract_id
6953   ;
6954 
6955   -- sjalasut, modified the below cursor to have p_contract_id joined with
6956   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
6957   -- as part of OKLR12B disbursements project
6958   CURSOR c2 (p_contract_id  NUMBER)
6959   IS
6960   select a.ipvs_id,
6961          nvl(sum(OKL_FUNDING_PVT.get_contract_line_funded_amt(a.id,a.funding_type_code)),0)
6962   from  okl_trx_ap_invoices_b a
6963        ,okl_txl_ap_inv_lns_all_b b
6964   where a.id = b.tap_id
6965   and  b.khr_id = p_contract_id
6966   and a.trx_status_code in ('APPROVED', 'PROCESSED')
6967   and    a.funding_type_code IS NOT NULL -- cklee 09-24-03
6968   and    a.funding_type_code <> 'SUPPLIER_RETENTION'
6969   group by a.ipvs_id
6970   ;
6971 
6972   CURSOR c_vendor_site (p_ipvs_id  NUMBER)
6973   IS
6974   select a.name
6975   from OKX_VENDOR_SITES_V a
6976   where a.id1 = p_ipvs_id
6977   ;
6978 
6979   --- vpanwar 21/02/2007 Added
6980   --- to get all the funding lines for the funding header
6981     CURSOR fund_line_csr(p_fund_id number) IS
6982         Select id funding_line_id
6983         from OKL_TXL_AP_INV_LNS_B
6984         Where tap_id = p_fund_id;
6985   --- vpanwar 21/02/2007 End
6986 
6987 BEGIN
6988   -- Set API savepoint
6989   SAVEPOINT CREATE_FUNDING_ASSETS_PVT;
6990 
6991   -- Check for call compatibility
6992 
6993   IF (NOT FND_API.Compatible_API_Call (l_api_version,
6994                                 	   p_api_version,
6995 
6996                                 	   l_api_name,
6997                                 	   G_PKG_NAME ))
6998   THEN
6999     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7000 
7001   END IF;
7002 
7003   -- Initialize message list if requested
7004   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
7005       FND_MSG_PUB.initialize;
7006 	END IF;
7007 
7008   -- Initialize API status to success
7009   x_return_status := OKL_API.G_RET_STS_SUCCESS;
7010 
7011 
7012 /*** Begin API body ****************************************************/
7013   -- get org_id
7014   OPEN c(p_contract_id);
7015   FETCH c INTO l_org_id,
7016                l_currency_code,
7017                l_contract_number;
7018   CLOSE c;
7019 
7020 
7021   OPEN c2(p_contract_id);
7022   LOOP
7023 
7024     FETCH c2 into l_ipvs_id,
7025                   l_amount;
7026 
7027     OPEN c_vendor_site(l_ipvs_id);
7028     FETCH c_vendor_site into l_vendor_site_code;
7029     CLOSE c_vendor_site;
7030 
7031     EXIT WHEN c2%NOTFOUND;
7032 
7033     IF (l_amount <> 0 ) THEN
7034       l_amount := -l_amount;
7035 
7036       -- sjalasut, not commenting the khr_id reference in l_tapv_rec here as this
7037       -- record variable is used as a parameter for validate_header_attributes,
7038       -- validate_funding_request etc. since per the disbursements FDD, tapv_rec
7039       -- .khr_id would continue to exist, not making this change would not cause
7040       -- compilation issues.
7041       l_tapv_rec.KHR_ID := p_contract_id;
7042       l_tapv_rec.AMOUNT := l_amount;
7043       l_tapv_rec.FUNDING_TYPE_CODE := 'PREFUNDING';
7044       l_tapv_rec.IPVS_ID := l_ipvs_id;
7045       l_tapv_rec.ORG_ID := l_org_id;
7046 
7047 
7048       l_tapv_rec.TRX_STATUS_CODE := 'ENTERED'; -- create record 1st
7049       l_tapv_rec.DESCRIPTION
7050         := 'Account Payable debit for Reverse Contract, ' || l_contract_number || ', vendor site '|| l_vendor_site_code;
7051       l_tapv_rec.CURRENCY_CODE := l_currency_code;
7052       l_tapv_rec.PAYMENT_METHOD_CODE := 'CHECK';
7053       l_tapv_rec.DATE_ENTERED := sysdate;
7054 
7055       -- sjalasut, modified the invoice_type from G_STANDARD to G_CREDIT
7056       -- changes made as part of OKLR12B Disbursements Project
7057       l_tapv_rec.INVOICE_TYPE := G_CREDIT;
7058 
7059       -- sjalasut, added code to make sure that the invoice amount on the credit memo
7060       -- invoice is negative. changes made as part of OKLR12B Disbursements project
7061       IF(l_tapv_rec.AMOUNT > 0)THEN
7062         l_tapv_rec.AMOUNT := ((l_tapv_rec.AMOUNT)*(-1));
7063       END IF;
7064 
7065       l_tapv_rec.DATE_INVOICED := sysdate;
7066       l_tapv_rec.DATE_GL := sysdate;
7067 
7068       create_funding_header(
7069         p_api_version   => p_api_version,
7070         p_init_msg_list => p_init_msg_list,
7071         x_return_status => x_return_status,
7072         x_msg_count     => x_msg_count,
7073         x_msg_data      => x_msg_data,
7074         p_tapv_rec      => l_tapv_rec,
7075         x_tapv_rec      => x_tapv_rec);
7076 
7077 
7078       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
7079         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7080       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
7081         RAISE OKL_API.G_EXCEPTION_ERROR;
7082       END IF;
7083 
7084       OPEN cu (x_tapv_rec.id);
7085       FETCH cu INTO l_PAY_GROUP_LOOKUP_CODE,
7086                   l_NETTABLE_YN;
7087       CLOSE cu;
7088 
7089       l_tapv_rec.ID := x_tapv_rec.id;
7090       l_tapv_rec.PAY_GROUP_LOOKUP_CODE := l_PAY_GROUP_LOOKUP_CODE;
7091       l_tapv_rec.NETTABLE_YN := l_NETTABLE_YN;
7092       l_tapv_rec.TRX_STATUS_CODE := 'APPROVED';
7093 -- cklee 09-24-03
7094 -- due to the external interface table limit to 30 chars
7095       l_tapv_rec.VENDOR_INVOICE_NUMBER := x_tapv_rec.INVOICE_NUMBER;
7096 
7097       update_funding_header(
7098         p_api_version   => p_api_version,
7099         p_init_msg_list => p_init_msg_list,
7100         x_return_status => x_return_status,
7101         x_msg_count     => x_msg_count,
7102         x_msg_data      => x_msg_data,
7103         p_tapv_rec      => l_tapv_rec,
7104         x_tapv_rec      => x_tapv_rec);
7105 
7106       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
7107         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7108 
7109       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
7110         RAISE OKL_API.G_EXCEPTION_ERROR;
7111       END IF;
7112 
7113       -- WF enable, add accounting entry 06/24/03 cklee
7114 
7115 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
7116 /*
7117       --- vpanwar 21/02/2007 Added
7118     OPEN fund_line_csr(l_tapv_rec.id);
7119     LOOP
7120     FETCH fund_line_csr into funding_line_id;
7121 
7122     EXIT WHEN fund_line_csr%NOTFOUND;
7123 */
7124 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
7125 
7126       OKL_FUNDING_PVT.CREATE_ACCOUNTING_DIST
7127                           (p_api_version   => p_api_version,
7128                            p_init_msg_list => p_init_msg_list,
7129                            x_return_status => x_return_status,
7130                            x_msg_count     => x_msg_count,
7131                            x_msg_data      => x_msg_data,
7132                            p_status        => l_tapv_rec.trx_status_code,
7133                            p_fund_id       => l_tapv_rec.id);--,--:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
7134 --                           p_fund_line_id  => funding_line_id);--:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
7135 
7136 
7137       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
7138         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7139       ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
7140         RAISE OKL_API.G_EXCEPTION_ERROR;
7141       END IF;
7142 
7143 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
7144 --    END LOOP;
7145 --    CLOSE fund_line_csr;
7146     --- vpanwar 21/02/2007 End
7147 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
7148 
7149     END IF;
7150 
7151   END LOOP;
7152   CLOSE c2;
7153 
7154 /*** End API body ******************************************************/
7155 
7156   -- Get message count and if count is 1, get message info
7157 	FND_MSG_PUB.Count_And_Get
7158     (p_count          =>      x_msg_count,
7159      p_data           =>      x_msg_data);
7160 
7161 EXCEPTION
7162   WHEN OKL_API.G_EXCEPTION_ERROR THEN
7163     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
7164     x_return_status := OKL_API.G_RET_STS_ERROR;
7165     FND_MSG_PUB.Count_And_Get
7166       (p_count         =>      x_msg_count,
7167        p_data          =>      x_msg_data);
7168 
7169   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
7170     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
7171     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
7172     FND_MSG_PUB.Count_And_Get
7173       (p_count         =>      x_msg_count,
7174        p_data          =>      x_msg_data);
7175 
7176   WHEN OTHERS THEN
7177 	ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
7178       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
7179 
7180       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
7181                           p_msg_name      => G_UNEXPECTED_ERROR,
7182                           p_token1        => G_SQLCODE_TOKEN,
7183                           p_token1_value  => SQLCODE,
7184                           p_token2        => G_SQLERRM_TOKEN,
7185                           p_token2_value  => SQLERRM);
7186       FND_MSG_PUB.Count_And_Get
7187         (p_count         =>      x_msg_count,
7188          p_data          =>      x_msg_data);
7189 
7190 
7191   END reverse_funding_requests;
7192 
7193 ----------------------------------------------------------------------------
7194 /*
7195  	   veramach 29-Jun-2007 Bug#5600694 Modified the function get_contract_line_amt
7196  	   to improve the performance of funding screens. Merged the cursor cv_addon in
7197  	   cv_model.
7198  	   The cv_model cursor considers the item for which the vendor is passed and
7199  	   add on items for that item for which a vendor is attached.
7200  	   The add on items should have a vendor and not necessary that
7201  	   the vendor should match the vendor of the item.
7202  	   Example 1:
7203  	      Item     - >    Addon1     and Addon2
7204  	     (Vendor1)       (Vendor2)     (Vendor2)
7205 
7206  	         cv_model will consider Item, Addon1 and Addon2.
7207 
7208  	   Example 2:
7209  	      Item     - >    Addon1     and Addon2
7210  	     (Vendor1)       (Vendor1)     (No vendor)
7211 
7212  	     cv_model will consider Item and Addon1.
7213 
7214  	 */
7215 
7216 
7217 FUNCTION get_contract_line_amt(
7218   p_khr_id           IN   NUMBER,  -- contract hdr
7219   p_kle_id           IN   NUMBER,  -- contract line
7220   p_vendor_site_id   IN   NUMBER
7221 )
7222   RETURN NUMBER IS
7223   l_amount            NUMBER                                        := 0;
7224   l_amount_buffer     NUMBER                                        := 0;
7225   l_vendor_id         NUMBER                                        := 0;
7226 -- start: okl.h: cklee
7227   x_return_status     VARCHAR2(3)                                   := okl_api.g_ret_sts_success;
7228   l_api_version       NUMBER                                        := 1.0;
7229   x_msg_count         NUMBER;
7230   x_msg_data          VARCHAR2(4000);
7231   l_init_msg_list     VARCHAR2(10)                                  := okl_api.g_false;
7232   x_value             NUMBER;
7233 
7234 -- start: okl.h: cklee
7235   CURSOR cv1(
7236     p_vendor_site_id   NUMBER
7237   ) IS
7238     SELECT vendor_id
7239       FROM okx_vendor_sites_v
7240      WHERE id1 = TO_CHAR(p_vendor_site_id);
7241 
7242 --
7243 --
7244 -- FA line with vendor attach
7245 --
7246   CURSOR cv_model(
7247     p_khr_id      NUMBER,
7248     p_kle_id      NUMBER,
7249     p_vendor_id   NUMBER
7250   ) IS
7251     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7252       FROM okc_k_items cim,
7253            okc_k_party_roles_b cpl,
7254            okc_k_lines_b cle,
7255            --Bug# 15992711 : avsingh - New-Re-leased Asset Enhancement
7256            okl_k_lines   kle_top
7257            --End Bug# Bug# 15992711 : New Re-leased Asset Enhancement
7258      WHERE
7259 --for model lines of vendor
7260            (
7261                 cim.cle_id = cle.ID
7262             AND cim.dnz_chr_id = p_khr_id
7263             AND cpl.cle_id = cle.ID
7264             AND cpl.dnz_chr_id = p_khr_id
7265             AND cpl.chr_id IS NULL
7266             AND cpl.rle_code = 'OKL_VENDOR'
7267             AND cpl.object1_id1 = TO_CHAR(p_vendor_id)
7268             AND cpl.object1_id2 = '#'
7269             AND EXISTS(SELECT NULL
7270                          FROM okc_line_styles_b model_lse
7271                         WHERE model_lse.ID = cle.lse_id AND model_lse.lty_code = 'ITEM' AND cle.cle_id = p_kle_id)
7272             --Bug# 13809564
7273             AND cle.sts_code <> 'ABANDONED'
7274             --Bug# 15992711 : avsingh - New-Re-leased Asset Enhancement
7275             --Re-lease lines should not be included in total fundable calculation
7276             AND kle_top.id   = p_kle_id
7277             AND nvl(kle_top.re_lease_yn,'N') = 'N'
7278             --End Bug# Bug# 15992711 : New Re-leased Asset Enhancement
7279            );   -- end of model lines
7280 -- re lease flag check
7281        --AND EXISTS(SELECT NULL
7282                     --FROM okl_k_lines lkl
7283                    --WHERE lkl.ID = cle.ID AND lkl.re_lease_yn IS NULL);
7284 
7285 
7286 
7287 --
7288 --
7289 -- add on line with vendor attach, but don't care which vendor attach
7290 --
7291   CURSOR cv_addon(
7292     p_khr_id      NUMBER,
7293     p_kle_id      NUMBER,
7294 -- start: okl.h: cklee -- add vendor_id as a parameter
7295     p_vendor_id   NUMBER
7296   )
7297 -- end: okl.h: cklee -- add vendor_id as a parameter
7298   IS
7299     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7300       FROM okc_k_items cim,
7301            okc_k_party_roles_b cpl,
7302            okc_k_lines_b cle,
7303             --Bug# 15992711 : avsingh - New-Re-leased Asset Enhancement
7304             okl_k_lines kle_top
7305             --End Bug# Bug# 15992711 : New Re-leased Asset Enhancement
7306      WHERE
7307 --for add on lines: don't care the vendor association, but care if it has vendor association!
7308            (
7309                 cim.cle_id = cle.ID
7310             AND cim.dnz_chr_id = p_khr_id
7311             AND cpl.cle_id = cle.ID
7312             AND cpl.dnz_chr_id = p_khr_id
7313             AND cpl.chr_id IS NULL
7314             AND cpl.rle_code = 'OKL_VENDOR'
7315 -- start: okl.h: cklee
7316             AND cpl.object1_id1 = TO_CHAR(p_vendor_id)
7317             AND cpl.object1_id2 = '#'
7318 -- end: okl.h: cklee
7319             AND EXISTS(SELECT NULL
7320                          FROM okc_line_styles_b adon_lse
7321                         WHERE adon_lse.ID = cle.lse_id AND adon_lse.lty_code = 'ADD_ITEM'
7322                                                                                          -- ADD_ITEM has one parent ITEM only
7323                               AND EXISTS(SELECT NULL
7324                                            FROM okc_k_lines_b mdl_parent
7325                                           WHERE mdl_parent.ID = cle.cle_id AND mdl_parent.cle_id = p_kle_id))
7326             --Bug# 13809564
7327             AND cle.sts_code <> 'ABANDONED'
7328               --Bug# 15992711 : avsingh - New-Re-leased Asset Enhancement
7329             --Re-lease lines should not be included in total fundable calculation
7330             AND kle_top.id   = p_kle_id
7331             AND nvl(kle_top.re_lease_yn,'N') = 'N'
7332             --End Bug# Bug# 15992711 : New Re-leased Asset Enhancement
7333            );   -- end of add on lines
7334 -- re lease flag check
7335        --AND EXISTS(SELECT NULL
7336                     --FROM okl_k_lines lkl
7337                    --WHERE lkl.ID = cle.ID AND lkl.re_lease_yn IS NULL);
7338 
7339 --
7340 --
7341 -- if NO vendor_id
7342 --
7343   CURSOR cv_no_vendor(
7344     p_khr_id   NUMBER,
7345     p_kle_id   NUMBER
7346   ) IS
7347     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7348       FROM okc_k_items cim,
7349            okc_k_party_roles_b cpl,
7350            okc_k_lines_b cle,
7351             --Bug# 15992711 : avsingh - New-Re-leased Asset Enhancement
7352             okl_k_lines kle_top
7353             --End Bug# Bug# 15992711 : New Re-leased Asset Enhancement
7354      WHERE
7355 --for model lines of vendor
7356            (
7357                 cim.cle_id = cle.ID
7358             AND cim.dnz_chr_id = p_khr_id
7359             AND cpl.cle_id = cle.ID
7360             AND cpl.dnz_chr_id = p_khr_id
7361             AND cpl.chr_id IS NULL
7362             AND cpl.rle_code = 'OKL_VENDOR'
7363 --and    cpl.object1_id1 = to_char(p_vendor_id)
7364 --and    cpl.object1_id2 = '#'
7365             AND EXISTS(SELECT NULL
7366                          FROM okc_line_styles_b model_lse
7367                         WHERE model_lse.ID = cle.lse_id AND model_lse.lty_code = 'ITEM' AND cle.cle_id = p_kle_id)
7368             --Bug# 13809564
7369             AND cle.sts_code <> 'ABANDONED'
7370             --Bug# 15992711 : avsingh - New-Re-leased Asset Enhancement
7371             --Re-lease lines should not be included in total fundable calculation
7372             AND kle_top.id   = p_kle_id
7373             AND nvl(kle_top.re_lease_yn,'N') = 'N'
7374             --End Bug# Bug# 15992711 : New Re-leased Asset Enhancement
7375            )   -- end of model lines
7376         OR
7377 --for add on lines: don't care the vendor association, but care if it has vendor association!
7378                (
7379                     cim.cle_id = cle.ID
7380                 AND cim.dnz_chr_id = p_khr_id
7381                 AND cpl.cle_id = cle.ID
7382                 AND cpl.dnz_chr_id = p_khr_id
7383                 AND cpl.chr_id IS NULL
7384                 AND cpl.rle_code = 'OKL_VENDOR'
7385 --and    cpl.object1_id1 = to_char(p_vendor_id)
7386 --and    cpl.object1_id2 = '#'
7387                 AND EXISTS(SELECT NULL
7388                              FROM okc_line_styles_b adon_lse
7389                             WHERE adon_lse.ID = cle.lse_id AND adon_lse.lty_code = 'ADD_ITEM'
7390                                                                                              -- ADD_ITEM has one parent ITEM only
7391                                   AND EXISTS(SELECT NULL
7392                                                FROM okc_k_lines_b mdl_parent
7393                                               WHERE mdl_parent.ID = cle.cle_id AND mdl_parent.cle_id = p_kle_id))
7394                 --Bug# 13809564
7395                 AND cle.sts_code <> 'ABANDONED'
7396                 --Bug# 15992711 : avsingh - New-Re-leased Asset Enhancement
7397                 --Re-lease lines should not be included in total fundable calculation
7398                 AND kle_top.id   = p_kle_id
7399                 AND nvl(kle_top.re_lease_yn,'N') = 'N'
7400                 --End Bug# Bug# 15992711 : New Re-leased Asset Enhancement
7401                );   -- end of add on lines
7402 -- re lease flag check
7403 --           AND EXISTS(SELECT NULL
7404 --                        FROM okl_k_lines lkl
7405 --                       WHERE lkl.ID = cle.ID AND lkl.re_lease_yn IS NULL);
7406 
7407 --
7408 --
7409 -- if NO line and vendor_id
7410 --
7411   CURSOR c_no_line_and_vendor(
7412     p_khr_id   NUMBER
7413   ) IS
7414     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7415       FROM okc_k_items cim,
7416            okc_k_party_roles_b cpl,
7417            okc_k_lines_b cle,
7418            --Bug# 15992711 : avsingh - New-Released asset ER
7419            okl_k_lines   kle_top
7420            --End Bug# 15992711 : New-Released asset ER
7421      WHERE (
7422                 cim.cle_id = cle.ID
7423             AND cim.dnz_chr_id = p_khr_id
7424             AND cpl.cle_id = cle.ID
7425             AND cpl.dnz_chr_id = p_khr_id
7426             AND cpl.chr_id IS NULL
7427             AND cpl.rle_code = 'OKL_VENDOR'
7428             AND EXISTS(SELECT NULL
7429                          FROM okc_line_styles_b model_lse
7430                         WHERE model_lse.ID = cle.lse_id
7431                           AND model_lse.lty_code = 'ITEM')
7432             --Bug# 13809564
7433             AND cle.sts_code <> 'ABANDONED'
7434             --Bug# 15992711 : avsingh - New-Released asset ER
7435             AND kle_top.id   = cle.cle_id
7436             AND nvl(kle_top.re_lease_yn,'N')  = 'N'
7437             --End Bug# 15992711 : New-Released asset ER
7438            )
7439         OR     (
7440                     cim.cle_id = cle.ID
7441                 AND cim.dnz_chr_id = p_khr_id
7442                 AND cpl.cle_id = cle.ID
7443                 AND cpl.dnz_chr_id = p_khr_id
7444                 AND cpl.chr_id IS NULL
7445                 AND cpl.rle_code = 'OKL_VENDOR'
7446                 AND EXISTS(SELECT NULL
7447                              FROM okc_line_styles_b adon_lse
7448                             WHERE adon_lse.ID = cle.lse_id
7449                               AND adon_lse.lty_code = 'ADD_ITEM'
7450                               AND EXISTS(SELECT NULL
7451                                           FROM okc_k_lines_b mdl_parent
7452                                          WHERE mdl_parent.ID = cle.cle_id
7453                                          --Bug# 15992711 : avsingh - New-Released asset ER
7454                                          AND EXISTS(SELECT NULL
7455                                                     FROM okl_k_lines kle_top1
7456                                                     WHERE kle_top1.id = mdl_parent.cle_id
7457                                                     AND   nvl(kle_top1.re_lease_yn,'N') = 'N')))
7458                                          --End Bug# 15992711 : New-Released asset ER
7459                 --Bug# 13809564
7460                 AND cle.sts_code <> 'ABANDONED'
7461                );
7462            --AND EXISTS(SELECT NULL
7463                         --FROM okl_k_lines lkl
7464                        --WHERE lkl.ID = cle.ID
7465                          --AND lkl.re_lease_yn IS NULL);
7466 
7467 --
7468 -- bug 5384359 -- start
7469   CURSOR downpymnt_recvr_csr(
7470     p_kle_id   NUMBER
7471   ) IS
7472     SELECT down_payment_receiver_code
7473       FROM okl_k_lines
7474      WHERE ID = p_kle_id;
7475 
7476   l_downpymnt_recvr   okl_k_lines.down_payment_receiver_code%TYPE;
7477 -- bug 5384359 -- end
7478 --
7479 
7480 --start:| 08-Feb-08  cklee Fixed bug: 6783566                                        |
7481 /*
7482   CURSOR c_kle_id(
7483                   p_khr_id okc_k_headers_b.id%TYPE
7484                  ) IS
7485     SELECT kle.cle_id kle_id,
7486            kle_k.down_payment_receiver_code downpymnt_recvr
7487       FROM okl_assets_lov_uv kle,
7488            okl_k_lines kle_k
7489      WHERE kle.cle_id = kle_k.ID
7490        AND kle.chr_id = p_khr_id;
7491 */
7492   CURSOR c_kle_id(
7493                   p_khr_id okc_k_headers_b.id%TYPE
7494                  ) IS
7495     SELECT kle_k.id kle_id,
7496            kle_k.down_payment_receiver_code downpymnt_recvr
7497       FROM okl_k_lines kle_k,
7498            okc_k_lines_b kle
7499      where kle_k.id = kle.id
7500      and kle.dnz_chr_id = p_khr_id
7501      --Bug# 15992711 : avsingh - New-Released asset ER
7502      and nvl(kle_k.re_lease_yn,'N') = 'N'  -- re lease flag check
7503      --End Bug# 15992711 : New-Released asset ER
7504      --Bug# 13809564
7505      AND kle.sts_code <> 'ABANDONED'
7506      -- only asset lines associated with Lease Vendor (Supplier Invoice)
7507      and exists (
7508       SELECT 1
7509       FROM okc_k_party_roles_b cpl,
7510            okc_k_lines_b cle,
7511            okc_line_styles_b model_lse
7512      WHERE cpl.rle_code = 'OKL_VENDOR'
7513        AND cpl.chr_id IS NULL
7514        and model_lse.ID = cle.lse_id
7515        and model_lse.lty_code = 'ITEM'
7516        and cle.cle_id = kle_k.id -- link to FREE_FORM1 (top line)
7517        AND cpl.object1_id2 = '#'
7518        AND cpl.cle_id = cle.ID); -- link to ITEM
7519 
7520 --end:| 08-Feb-08  cklee Fixed bug: 6783566                                        |
7521 
7522 BEGIN
7523   IF (p_khr_id IS NULL) OR (p_khr_id = okl_api.g_miss_num) THEN
7524     RETURN 0;   -- error
7525   ELSIF ((p_kle_id IS NULL) OR (p_kle_id = okl_api.g_miss_num) AND (p_vendor_site_id IS NULL OR p_vendor_site_id = okl_api.g_miss_num)) THEN
7526 
7527     OPEN c_no_line_and_vendor(p_khr_id);
7528     FETCH c_no_line_and_vendor INTO l_amount;
7529     CLOSE c_no_line_and_vendor;
7530 
7531     --------------------------------------------------
7532     -- Contract Trade In AND Contract Capitalized Reduction -- Downpayment
7533     --------------------------------------------------
7534     FOR l_kle_id IN c_kle_id(p_khr_id) LOOP
7535       BEGIN
7536         l_amount_buffer := okl_seeded_functions_pvt.line_tradein(p_chr_id => p_khr_id, p_line_id => l_kle_id.kle_id);
7537 
7538         IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7539           l_amount_buffer := 0;
7540         ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7541           l_amount_buffer := 0;
7542         END IF;
7543       EXCEPTION
7544         WHEN OTHERS THEN
7545           l_amount_buffer := 0;
7546       END;
7547 
7548       l_amount := l_amount - l_amount_buffer;
7549 
7550       BEGIN
7551         IF ((l_kle_id.downpymnt_recvr IS NULL) OR (l_kle_id.downpymnt_recvr = 'VENDOR')) THEN
7552           l_amount_buffer := okl_seeded_functions_pvt.line_capital_reduction(p_chr_id => p_khr_id, p_line_id => l_kle_id.kle_id);
7553 
7554           IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7555             l_amount_buffer := 0;
7556           ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7557             l_amount_buffer := 0;
7558           END IF;
7559         ELSIF(l_kle_id.downpymnt_recvr = 'LESSOR') THEN
7560           l_amount_buffer := 0;
7561         END IF;
7562 
7563       EXCEPTION
7564         WHEN OTHERS THEN
7565           l_amount_buffer := 0;
7566       END;
7567 
7568       l_amount := l_amount - l_amount_buffer;
7569     END LOOP;
7570 
7571   ELSIF (p_vendor_site_id IS NULL OR p_vendor_site_id = okl_api.g_miss_num) THEN
7572 
7573     OPEN cv_no_vendor(p_khr_id, p_kle_id);
7574     FETCH cv_no_vendor INTO l_amount;
7575     CLOSE cv_no_vendor;
7576 
7577     -- start: skgautam Bug#5260198
7578     --------------------------------------------------
7579     -- Contract Trade In
7580     --------------------------------------------------
7581     BEGIN
7582       l_amount_buffer := okl_seeded_functions_pvt.line_tradein(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7583 
7584       IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7585         l_amount_buffer := 0;
7586       ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7587         l_amount_buffer := 0;
7588       END IF;
7589     EXCEPTION
7590       WHEN OTHERS THEN
7591         l_amount_buffer := 0;
7592     END;
7593 
7594     l_amount := l_amount - l_amount_buffer;
7595 
7596     --------------------------------------------------
7597     -- Contract Capitalized Reduction -- Downpayment
7598     --------------------------------------------------
7599     BEGIN
7600       -- bug 5384359 -- start
7601       OPEN downpymnt_recvr_csr(p_kle_id);
7602       FETCH downpymnt_recvr_csr INTO l_downpymnt_recvr;
7603       CLOSE downpymnt_recvr_csr;
7604 
7605       IF ((l_downpymnt_recvr IS NULL) OR (l_downpymnt_recvr = 'VENDOR')) THEN
7606         -- bug 5384359 -- end
7607         l_amount_buffer := okl_seeded_functions_pvt.line_capital_reduction(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7608 
7609         IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7610           l_amount_buffer := 0;
7611         ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7612           l_amount_buffer := 0;
7613         END IF;
7614         --bug 5473370 --start
7615       ELSIF(l_downpymnt_recvr = 'LESSOR') THEN
7616         l_amount_buffer := 0;
7617         --bug 5473370 -- end
7618         -- bug 5384359 -- start
7619       END IF;
7620         -- bug 5384359 -- END
7621     EXCEPTION
7622       WHEN OTHERS THEN
7623         l_amount_buffer := 0;
7624     END;
7625 
7626     l_amount := l_amount - l_amount_buffer;
7627     -- end: skgautam Bug#5260198
7628   ELSE
7629     OPEN cv1(p_vendor_site_id);
7630     FETCH cv1 INTO l_vendor_id;
7631     CLOSE cv1;
7632 
7633     -- get model line attach to vendor
7634     OPEN cv_model(p_khr_id,p_kle_id,l_vendor_id);
7635     FETCH cv_model INTO l_amount_buffer;
7636     CLOSE cv_model;
7637 
7638     -- start: okl.h: cklee
7639     /* commented out
7640         IF (l_amount_buffer <= 0) THEN
7641           return 0;
7642         ELSE
7643     */
7644     -- start: okl.h: cklee
7645     l_amount := l_amount_buffer;
7646 
7647     -- get add on attach to vendor, but don't care which vendor attach
7648     -- start: okl.h: cklee -- add vendor_id as a parameter
7649     OPEN cv_addon(p_khr_id,p_kle_id,l_vendor_id);
7650     -- end: okl.h: cklee
7651     FETCH cv_addon INTO l_amount_buffer;
7652     CLOSE cv_addon;
7653 
7654     l_amount := l_amount + l_amount_buffer;
7655 
7656     --    END IF;
7657     -- start: okl.h: cklee
7658     --------------------------------------------------
7659     -- Contract Capitalized Reduction -- Downpayment
7660     --------------------------------------------------
7661     BEGIN
7662       --START:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7663       /*
7664             OKL_EXECUTE_FORMULA_PUB.execute(
7665               p_api_version   => l_api_version,
7666               p_init_msg_list => l_init_msg_list,
7667               x_return_status => x_return_status,
7668               x_msg_count     => x_msg_count,
7669               x_msg_data      => x_msg_data,
7670               p_formula_name  => 'CONTRACT_TRADEIN',
7671               p_contract_id   => p_khr_id,
7672               p_line_id       => p_kle_id,
7673               x_value         => l_amount_buffer);
7674       */
7675       --skgautam Bug#5260198
7676       -- bug 5402377 -- start
7677       OPEN downpymnt_recvr_csr(p_kle_id);
7678       FETCH downpymnt_recvr_csr INTO l_downpymnt_recvr;
7679       CLOSE downpymnt_recvr_csr;
7680 
7681       IF ((l_downpymnt_recvr IS NULL) OR (l_downpymnt_recvr = 'VENDOR')) THEN
7682         -- bug 5402377 -- end
7683         l_amount_buffer := okl_seeded_functions_pvt.line_capital_reduction(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7684 
7685         --END:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7686         IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7687           l_amount_buffer := 0;
7688         ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7689           l_amount_buffer := 0;
7690         END IF;
7691         --bug 5500032 --start
7692       ELSIF(l_downpymnt_recvr = 'LESSOR') THEN
7693         l_amount_buffer := 0;
7694         --bug 5500032 -- end
7695         -- bug 5402377 -- start
7696       END IF;
7697       -- bug 5402377 -- END
7698     EXCEPTION
7699       WHEN OTHERS THEN
7700         l_amount_buffer := 0;
7701     END;
7702 
7703     l_amount := l_amount - l_amount_buffer;
7704 
7705     --------------------------------------------------
7706     -- Contract Trade In
7707     --------------------------------------------------
7708     BEGIN
7709       --START:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7710       /*
7711             OKL_EXECUTE_FORMULA_PUB.execute(
7712               p_api_version   => l_api_version,
7713               p_init_msg_list => l_init_msg_list,
7714               x_return_status => x_return_status,
7715               x_msg_count     => x_msg_count,
7716               x_msg_data      => x_msg_data,
7717               p_formula_name  => 'CONTRACT_CAPREDUCTION',
7718               p_contract_id   => p_khr_id,
7719               p_line_id       => p_kle_id,
7720               x_value         => l_amount_buffer);
7721       */
7722       --skgautam Bug#5260198
7723       l_amount_buffer := okl_seeded_functions_pvt.line_tradein(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7724 
7725       --END:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7726       IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7727         l_amount_buffer := 0;
7728       ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7729         l_amount_buffer := 0;
7730       END IF;
7731     EXCEPTION
7732       WHEN OTHERS THEN
7733         l_amount_buffer := 0;
7734     END;
7735 
7736     l_amount := l_amount - l_amount_buffer;
7737     -- end: okl.h: cklee
7738   END IF;
7739 
7740   IF (l_amount IS NULL) THEN
7741     l_amount := 0;
7742   END IF;
7743 
7744   RETURN l_amount;
7745 END get_contract_line_amt;
7746 
7747 
7748 
7749 ----------------------------------------------------------------------------
7750 FUNCTION get_contract_line_funded_amt(
7751   p_khr_id                       IN NUMBER                 -- contract hdr
7752  ,p_kle_id                       IN NUMBER                 -- contract line
7753  ,p_ref_type_code                IN VARCHAR2
7754 ) RETURN NUMBER
7755 IS
7756   l_amount NUMBER := 0;
7757 
7758   -- sjalasut, modified the cursor below to have khr_id referred from
7759   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7760   -- as part of OKLR12B disbursements project.
7761   CURSOR c (p_khr_id  NUMBER,
7762             p_kle_id  NUMBER)
7763   IS
7764 
7765     SELECT SUM(tl.amount)
7766       FROM okl_trx_ap_invoices_b th,
7767            okl_txl_ap_inv_lns_all_b tl
7768      WHERE th.id = tl.tap_id
7769        AND tl.khr_id = p_khr_id
7770        AND tl.kle_id = p_kle_id
7771 -- fixed bug 3007875
7772        AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED');
7773 
7774   -- sjalasut, modified the cursor below to have khr_id referred from
7775   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7776   -- as part of OKLR12B disbursements project.
7777   CURSOR ct (p_khr_id            NUMBER,
7778              p_kle_id            NUMBER,
7779              p_funding_type_code VARCHAR2)
7780   IS
7781     SELECT SUM(tl.amount)
7782       FROM okl_trx_ap_invoices_b th,
7783            okl_txl_ap_inv_lns_all_b tl
7784      WHERE th.id = tl.tap_id
7785        AND tl.khr_id = p_khr_id
7786        AND tl.kle_id = p_kle_id
7787        AND th.funding_type_code = p_funding_type_code
7788 -- fixed bug 3007875
7789        AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED');
7790 BEGIN
7791   IF (p_khr_id IS NULL OR p_khr_id = OKL_API.G_MISS_NUM) OR
7792      (p_kle_id IS NULL OR p_kle_id = OKL_API.G_MISS_NUM)
7793   THEN
7794     RETURN OKL_API.G_MISS_NUM;  -- error
7795   ELSIF (p_ref_type_code IS NULL OR p_ref_type_code = OKL_API.G_MISS_CHAR) THEN
7796     OPEN c (p_khr_id, p_kle_id);
7797     FETCH c INTO l_amount;
7798     CLOSE c;
7799   ELSE
7800     OPEN ct (p_khr_id, p_kle_id, p_ref_type_code);
7801     FETCH ct INTO l_amount;
7802     CLOSE ct;
7803   END IF;
7804   IF (l_amount IS NULL) THEN l_amount := 0; END IF;
7805   RETURN l_amount;
7806 END;
7807 ----------------------------------------------------------------------------
7808 
7809 -- get contract fund amount for asset lines
7810 FUNCTION get_contract_line_funded_amt(
7811   p_fund_id                       IN NUMBER                 -- fund hdr
7812   ,p_fund_type                    IN VARCHAR2               -- fund type code
7813 ) RETURN NUMBER
7814 IS
7815   l_amount NUMBER := 0;
7816   CURSOR c (p_fund_id  NUMBER)
7817   IS
7818     SELECT nvl(SUM(tl.amount),0)
7819       FROM okl_trx_ap_invoices_b th,
7820            okl_txl_ap_inv_lns_b tl
7821      WHERE tl.tap_id = th.id
7822        AND th.id = p_fund_id
7823 -- no need for this function. this is used for display at UI site only
7824 --       AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED')
7825   ;
7826   CURSOR c2 (p_fund_id  NUMBER)
7827   IS
7828     SELECT nvl(th.amount,0)
7829       FROM okl_trx_ap_invoices_b th
7830        WHERE th.id = p_fund_id
7831 -- no need for this function. this is used for display at UI site only
7832 --       AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED')
7833   ;
7834 
7835 BEGIN
7836     IF (p_fund_type in (G_ASSET_TYPE_CODE,
7837                         G_SUPPLIER_RETENTION_TYPE_CODE,
7838 --START:| 08-Jun-2006   cklee   Bug#5291817 get_contract_line_funded_amt() for
7839 --                              sync_header_amount()              |
7840                         G_ASSET_SUBSIDY,
7841 --END:| 08-Jun-2006   cklee   Bug#5291817 get_contract_line_funded_amt() for
7842 --                              sync_header_amount()              |
7843                         G_EXPENSE,
7844                         G_MANUAL_DISB)) THEN -- cklee 05/04/2004
7845       OPEN c (p_fund_id);
7846 
7847       FETCH c INTO l_amount;
7848       CLOSE c;
7849     ELSE
7850       OPEN c2(p_fund_id);
7851       FETCH c2 INTO l_amount;
7852       CLOSE c2;
7853     END IF;
7854 /*
7855     IF (p_fund_type = 'SUPPLIER_RETENTION') THEN
7856       l_amount := -(l_amount);
7857     END IF;
7858 */
7859 
7860   RETURN l_amount;
7861 
7862 END;
7863 ----------------------------------------------------------------------------
7864 
7865 FUNCTION is_funding_unique(
7866   p_vendor_id                    IN NUMBER
7867  ,p_fund_number                  IN VARCHAR2
7868  ,p_org_id                       IN NUMBER
7869 ) RETURN VARCHAR2
7870 IS
7871  l_result VARCHAR2(1) := OKL_API.G_TRUE;
7872  l_dummy  VARCHAR2(1) := '?';
7873 
7874   -- sjalasut, modified the cursor below to have khr_id referred from
7875   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7876   -- as part of OKLR12B disbursements project.
7877   CURSOR c (p_fund_number VARCHAR2,
7878             p_org_id number,
7879             p_vendor_id number)
7880   IS
7881     SELECT 'X'
7882       FROM okl_trx_ap_invoices_b th,
7883            okl_txl_ap_inv_lns_all_b tl,
7884            okc_k_headers_b chr,
7885            PO_VENDOR_SITES_ALL VS
7886      WHERE th.id = tl.tap_id
7887      AND tl.khr_id = chr.id
7888      AND th.ipvs_id = vs.vendor_site_id
7889      AND th.vendor_invoice_number = p_fund_number
7890      AND chr.AUTHORING_ORG_ID = p_org_id
7891      AND VS.vendor_id = p_vendor_id;
7892 
7893 BEGIN
7894   IF (p_vendor_id IS NULL OR p_vendor_id = OKL_API.G_MISS_NUM) OR
7895      (p_fund_number IS NULL OR p_fund_number = OKL_API.G_MISS_CHAR)
7896   THEN
7897     RETURN OKL_API.G_MISS_CHAR;
7898   END IF;
7899 
7900 --  OPEN c(p_vendor_id, p_fund_number);
7901   OPEN c(p_fund_number, p_org_id, p_vendor_id);
7902 
7903   FETCH c INTO l_dummy;
7904   CLOSE c;
7905   IF (l_dummy = 'X') THEN
7906     l_result := OKL_API.G_FALSE;
7907   END IF;
7908   RETURN l_result;
7909 END is_funding_unique;
7910 ----------------------------------------------------------------------------
7911 
7912 FUNCTION is_contract_line_unique(
7913   p_kle_id                       IN NUMBER -- contract_line_id
7914  ,p_fund_id                      IN NUMBER
7915  ,p_fund_line_id                 IN NUMBER
7916  ,p_mode                         IN VARCHAR2
7917  ,p_org_id                       IN NUMBER
7918 ) RETURN VARCHAR2
7919 IS
7920  l_result VARCHAR2(1) := OKL_API.G_TRUE;
7921  l_dummy  VARCHAR2(1) := '?';
7922  l_count  NUMBER(1) := 0;
7923 
7924   CURSOR c (p_fund_id NUMBER, p_kle_id NUMBER)
7925   IS
7926     SELECT 'X'
7927       FROM okl_txl_ap_inv_lns_b t
7928      WHERE t.tap_id = p_fund_id
7929      AND   t.kle_id = p_kle_id
7930   ;
7931 
7932   CURSOR c2 (p_fund_id NUMBER, p_kle_id NUMBER, p_fund_line_id NUMBER)
7933   IS
7934 
7935 
7936     SELECT 'X'
7937       FROM okl_txl_ap_inv_lns_b t
7938      WHERE t.tap_id = p_fund_id
7939      AND   t.kle_id = p_kle_id
7940 
7941      AND   t.id <> p_fund_line_id -- except itself
7942   ;
7943 
7944 BEGIN
7945 
7946   IF (p_kle_id IS NULL OR p_kle_id = OKL_API.G_MISS_NUM) OR
7947      (p_fund_id IS NULL OR p_fund_id = OKL_API.G_MISS_NUM)
7948   THEN
7949     RETURN OKL_API.G_MISS_NUM;
7950   END IF;
7951 
7952   IF (p_mode = 'C') THEN
7953 
7954     OPEN c(p_fund_id, p_kle_id);
7955     FETCH c INTO l_dummy;
7956     CLOSE c;
7957 
7958   ELSIF (p_mode = 'U') THEN
7959     OPEN c2(p_fund_id, p_kle_id, p_fund_line_id);
7960 
7961     FETCH c2 INTO l_dummy;
7962     CLOSE c2;
7963 
7964   END IF;
7965 
7966   IF (l_dummy = 'X') THEN
7967     l_result := OKL_API.G_FALSE;
7968   END IF;
7969 
7970 
7971   RETURN l_result;
7972 
7973 END is_contract_line_unique;
7974 
7975 --
7976 -- search duplicated kle_id in this table by specific okl_trx_ap_inv_lns_b.tap_id
7977 --
7978 FUNCTION is_kle_id_unique(
7979     p_tplv_tbl                 IN tplv_tbl_type
7980 ) RETURN VARCHAR2
7981 IS
7982   l_result   VARCHAR2(1) := OKL_API.G_TRUE;
7983   l_tplv_tbl tplv_tbl_type := p_tplv_tbl;
7984   i        NUMBER;
7985   j        NUMBER;
7986   l_count  NUMBER;
7987 
7988 
7989 BEGIN
7990 
7991   -- check duplicated kle_id in this table
7992   IF (p_tplv_tbl.COUNT > 0) THEN
7993     i := p_tplv_tbl.FIRST;
7994     LOOP
7995 
7996       -- inner being search loop
7997       l_count := 0;
7998       j := l_tplv_tbl.FIRST;
7999       LOOP
8000 
8001 
8002         IF (p_tplv_tbl(i).kle_id = l_tplv_tbl(j).kle_id) THEN
8003           l_count := l_count+1;
8004           IF (l_count > 1) THEN
8005             l_result := OKL_API.G_FALSE;
8006             EXIT;
8007 
8008           END IF;
8009         END IF;
8010 
8011         EXIT WHEN (j = l_tplv_tbl.LAST);
8012 --        j := l_tplv_tbl.NEXT(i);
8013         j := l_tplv_tbl.NEXT(j); --  cklee 10/3/2007 bug: 6318418
8014       END LOOP;
8015 
8016       -- exit if duplicated rows found
8017       IF (l_count > 0) THEN
8018         EXIT;
8019       END IF;
8020 
8021       EXIT WHEN (i = p_tplv_tbl.LAST);
8022       i := p_tplv_tbl.NEXT(i);
8023     END LOOP;
8024   END IF;
8025   RETURN l_result;
8026 
8027 END is_kle_id_unique;
8028 
8029 -------------------------------------------------------------------------------
8030 
8031 /*---------------------------------------------------------------------------+
8032 |                                                                            |
8033 |  FUNCTION: get_amount_prefunded                                            |
8034 |  DESC   : Sum of all approved requests for specfiic contract where type    |
8035 |          = prefunding                                                      |
8036 |  IN     : p_contract_id                                                    |
8037 |  OUT NOCOPY    : amount                                                    |
8038 |  HISTORY: 13-JAN-02 [email protected] -- Created                    |
8039 |                                                                            |
8040 *-------------------------------------------------------------------------- */
8041 FUNCTION get_amount_prefunded(
8042  p_contract_id                   IN NUMBER
8043  ,p_vendor_site_id               IN NUMBER
8044 
8045 ) RETURN NUMBER
8046 
8047 IS
8048   l_amount NUMBER := 0;
8049 
8050   -- sjalasut, modified the cursor below to have khr_id referred from
8051   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8052   -- as part of OKLR12B disbursements project.
8053   CURSOR c (p_contract_id  NUMBER)
8054   IS
8055   select nvl(sum(a.amount),0)
8056   from okl_trx_ap_invoices_b a
8057       ,okl_txl_ap_inv_lns_all_b b
8058   where a.id = b.tap_id
8059   and a.funding_type_code = 'PREFUNDING'
8060   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8061   and b.khr_id = p_contract_id
8062 -- positive only
8063   and a.amount > 0
8064   ;
8065 
8066   -- sjalasut, modified the cursor below to have khr_id referred from
8067   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8068   -- as part of OKLR12B disbursements project.
8069   CURSOR c2 (p_contract_id  NUMBER, p_vendor_site_id  NUMBER)
8070   IS
8071   select nvl(sum(a.amount),0)
8072 
8073   from okl_trx_ap_invoices_b a
8074       ,okl_txl_ap_inv_lns_all_b b
8075   where a.id = b.tap_id
8076    and a.funding_type_code = 'PREFUNDING'
8077   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8078   and b.khr_id = p_contract_id
8079   and a.ipvs_id = p_vendor_site_id
8080 -- positive only
8081   and a.amount > 0
8082   ;
8083 
8084 BEGIN
8085 
8086   IF (p_vendor_site_id IS NULL OR p_vendor_site_id = OKL_API.G_MISS_NUM) THEN
8087 
8088     OPEN c (p_contract_id);
8089     FETCH c INTO l_amount;
8090     CLOSE c;
8091   ELSE
8092     OPEN c2 (p_contract_id, p_vendor_site_id);
8093     FETCH c2 INTO l_amount;
8094     CLOSE c2;
8095   END IF;
8096 
8097   RETURN l_amount;
8098   EXCEPTION
8099     WHEN OTHERS THEN
8100       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8101 
8102       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8103                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8104                           p_token1        => 'OKL_SQLCODE',
8105                           p_token1_value  => SQLCODE,
8106                           p_token2        => 'OKL_SQLERRM',
8107                           p_token2_value  => SQLERRM);
8108       RETURN 0;
8109 
8110 END;
8111 /*---------------------------------------------------------------------------+
8112 |                                                                            |
8113 |  FUNCTION: get_total_funded                                                |
8114 |  DESC   : Sum of all approved requests for specific contract               |
8115 |  IN     : p_contract_id                                                    |
8116 |  OUT NOCOPY    : amount                                                    |
8117 |  HISTORY: 13-JAN-02 [email protected] -- Created                    |
8118 |                                                                            |
8119 *-------------------------------------------------------------------------- */
8120 FUNCTION get_total_funded(
8121  p_contract_id                   IN NUMBER
8122  ,p_contract_line_id             IN NUMBER
8123 ) RETURN NUMBER
8124 
8125 IS
8126   l_amount NUMBER := 0;
8127   x_amount NUMBER := 0;
8128 
8129   -- sjalasut, modified the cursor below to have khr_id referred from
8130   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8131   -- as part of OKLR12B disbursements project.
8132   CURSOR c (p_contract_id  NUMBER)
8133   IS
8134   select nvl(sum(b.amount),0)
8135   from okl_trx_ap_invoices_b a,
8136        okl_txl_ap_inv_lns_all_b b
8137   where a.id = b.tap_id
8138   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8139   and a.funding_type_code IN ('ASSET','EXPENSE', G_ASSET_SUBSIDY) -- cklee 11.5.10 subsidy
8140   and b.khr_id = p_contract_id
8141 UNION
8142   select nvl(sum(a.amount),0)
8143   from okl_trx_ap_invoices_b a
8144        ,okl_txl_ap_inv_lns_all_b b
8145   where a.id = b.tap_id
8146    and a.funding_type_code in ('PREFUNDING', 'BORROWER_PAYMENT') -- fixed bug# 2604862
8147   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8148   and b.khr_id = p_contract_id
8149   ;
8150 
8151 BEGIN
8152 
8153   OPEN c (p_contract_id);
8154   LOOP
8155     FETCH c INTO l_amount;
8156     EXIT WHEN c%NOTFOUND;
8157     x_amount := x_amount + l_amount;
8158   END LOOP;
8159   CLOSE c;
8160 
8161   RETURN x_amount;
8162   EXCEPTION
8163     WHEN OTHERS THEN
8164       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8165       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8166                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8167                           p_token1        => 'OKL_SQLCODE',
8168                           p_token1_value  => SQLCODE,
8169                           p_token2        => 'OKL_SQLERRM',
8170                           p_token2_value  => SQLERRM);
8171       RETURN 0;
8172 
8173 END;
8174 /*---------------------------------------------------------------------------+
8175 |                                                                            |
8176 |  FUNCTION: get_total_retention                                             |
8177 |  DESC   : Sum of all approved requests for specific contract               |
8178 |           where funding type = 'SUPPLIER_RETENTION'                        |
8179 |  IN     : p_contract_id                                                    |
8180 |  OUT NOCOPY    : amount                                                    |
8181 |  HISTORY: 13-JAN-02 [email protected] -- Created                    |
8182 |                                                                            |
8183 *-------------------------------------------------------------------------- */
8184 
8185 FUNCTION get_total_retention(
8186  p_contract_id                   IN NUMBER
8187  ,p_contract_line_id             IN NUMBER
8188 ) RETURN NUMBER
8189 IS
8190   l_amount NUMBER := 0;
8191 
8192   -- sjalasut, modified the cursor below to have khr_id referred from
8193   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8194   -- as part of OKLR12B disbursements project.
8195   CURSOR c (p_contract_id  NUMBER)
8196   IS
8197   select nvl(sum(b.amount),0)
8198   from okl_trx_ap_invoices_b a,
8199        okl_txl_ap_inv_lns_all_b b
8200   where a.id = b.tap_id
8201   and b.khr_id = p_contract_id
8202   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8203   and a.funding_type_code = 'SUPPLIER_RETENTION';
8204 
8205 BEGIN
8206 
8207   OPEN c (p_contract_id);
8208   FETCH c INTO l_amount;
8209   CLOSE c;
8210 
8211   RETURN l_amount;
8212   EXCEPTION
8213     WHEN OTHERS THEN
8214       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8215       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8216                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8217                           p_token1        => 'OKL_SQLCODE',
8218                           p_token1_value  => SQLCODE,
8219                           p_token2        => 'OKL_SQLERRM',
8220                           p_token2_value  => SQLERRM);
8221       RETURN 0;
8222 
8223 END;
8224 
8225 /*---------------------------------------------------------------------------+
8226 |                                                                            |
8227 |  FUNCTION: get_amount_borrowerPay                                          |
8228 |  DESC   : Sum of all approved requests for specific contract               |
8229 |           where funding type = 'BORROWER_PAYMENT'                          |
8230 |  IN     : p_contract_id                                                    |
8231 |  OUT NOCOPY    : amount                                                    |
8232 |  HISTORY: 02-OCT-02 [email protected] -- Created                    |
8233 |                                                                            |
8234 *-------------------------------------------------------------------------- */
8235 FUNCTION get_amount_borrowerPay(
8236  p_contract_id                   IN NUMBER
8237  ,p_contract_line_id             IN NUMBER
8238 ) RETURN NUMBER
8239 IS
8240   l_amount NUMBER := 0;
8241 
8242   -- sjalasut, modified the cursor below to have khr_id referred from
8243   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8244   -- as part of OKLR12B disbursements project.
8245   CURSOR c (p_contract_id  NUMBER)
8246   IS
8247   select nvl(sum(a.amount),0)
8248   from okl_trx_ap_invoices_b a
8249       ,okl_txl_ap_inv_lns_all_b b
8250   where a.id = b.tap_id
8251   and b.khr_id = p_contract_id
8252   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8253   and a.funding_type_code = 'BORROWER_PAYMENT';
8254 
8255 BEGIN
8256 
8257   OPEN c (p_contract_id);
8258   FETCH c INTO l_amount;
8259   CLOSE c;
8260 
8261   RETURN l_amount;
8262   EXCEPTION
8263     WHEN OTHERS THEN
8264       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8265 
8266       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8267                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8268                           p_token1        => 'OKL_SQLCODE',
8269                           p_token1_value  => SQLCODE,
8270                           p_token2        => 'OKL_SQLERRM',
8271                           p_token2_value  => SQLERRM);
8272       RETURN 0;
8273 
8274 
8275 END;
8276 
8277 /*---------------------------------------------------------------------------+
8278 |                                                                            |
8279 |  FUNCTION: get_amount_subsidy                                              |
8280 |  DESC   : Sum of all approved requests for specific contract               |
8281 |           where funding type = 'ASSET_SUBSIDY'                             |
8282 |  IN     : p_contract_id                                                    |
8283 |  OUT NOCOPY    : amount                                                    |
8284 |  HISTORY: 02-OCT-02 [email protected] -- Created                    |
8285 |                                                                            |
8286 *-------------------------------------------------------------------------- */
8287 FUNCTION get_amount_subsidy(
8288  p_contract_id                   IN NUMBER
8289  ,p_contract_line_id             IN NUMBER
8290 ) RETURN NUMBER
8291 IS
8292   l_amount NUMBER := 0;
8293 
8294   -- sjalasut, modified the cursor below to have khr_id referred from
8295   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8296   -- as part of OKLR12B disbursements project.
8297   CURSOR c (p_contract_id  NUMBER)
8298   IS
8299   select nvl(sum(a.amount),0)
8300   from okl_trx_ap_invoices_b a
8301       ,okl_txl_ap_inv_lns_all_b b
8302   where a.id = b.tap_id
8303   and a.khr_id = p_contract_id
8304   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8305   and a.funding_type_code = G_ASSET_SUBSIDY;
8306 
8307   -- sjalasut, modified the cursor below to have khr_id referred from
8308   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8309   -- as part of OKLR12B disbursements project.
8310   CURSOR c_sub (p_contract_id  NUMBER, p_contract_line_id NUMBER)
8311   IS
8312   select nvl(sum(subln.amount),0)
8313   from okl_trx_ap_invoices_b sub,
8314        okl_txl_ap_inv_lns_all_b subln
8315   where sub.id = subln.tap_id
8316   and subln.khr_id = p_contract_id
8317   and subln.kle_id = p_contract_line_id -- fixed asset ID
8318   and sub.trx_status_code in ('APPROVED', 'PROCESSED')
8319   and sub.funding_type_code = G_ASSET_SUBSIDY;
8320 
8321 
8322 BEGIN
8323 
8324   IF (p_contract_line_id IS NULL OR p_contract_line_id = OKL_API.G_MISS_NUM) THEN
8325 
8326     OPEN c (p_contract_id);
8327     FETCH c INTO l_amount;
8328     CLOSE c;
8329 
8330   ELSE
8331 
8332     OPEN c_sub (p_contract_id, p_contract_line_id);
8333     FETCH c_sub INTO l_amount;
8334     CLOSE c_sub;
8335 
8336   END IF;
8337 
8338   RETURN l_amount;
8339   EXCEPTION
8340     WHEN OTHERS THEN
8341       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8342       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8343                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8344                           p_token1        => 'OKL_SQLCODE',
8345                           p_token1_value  => SQLCODE,
8346                           p_token2        => 'OKL_SQLERRM',
8347                           p_token2_value  => SQLERRM);
8348       RETURN 0;
8349 
8350 
8351 END get_amount_subsidy;
8352 
8353 -------------------------------------------------------------------------
8354  FUNCTION get_funding_subsidy_amount(
8355     p_chr_id                       IN  NUMBER,
8356     p_asset_cle_id                 IN  NUMBER,
8357     p_vendor_site_id               IN  NUMBER
8358 ) RETURN NUMBER
8359 IS
8360 --    l_amount            NUMBER := 0;
8361     l_api_version       NUMBER := 1.0;
8362     l_init_msg_list     VARCHAR2(1) := OKL_API.G_FALSE;
8363     x_return_status     VARCHAR2(1);
8364     x_msg_count         NUMBER;
8365     x_msg_data          VARCHAR2(2000);
8366     x_subsidy_amount    NUMBER := 0;
8367     l_vendor_id         NUMBER := NULL;
8368 
8369 
8370   CURSOR cv1 (p_vendor_site_id NUMBER)
8371   IS
8372     select vendor_id from okx_vendor_sites_v
8373     where id1 = to_char(p_vendor_site_id)
8374   ;
8375 
8376 BEGIN
8377 
8378     IF (p_vendor_site_id IS NOT NULL) THEN
8379 
8380       OPEN cv1 (p_vendor_site_id);
8381       FETCH cv1 INTO l_vendor_id;
8382       CLOSE cv1;
8383     END IF;
8384 
8385     OKL_SUBSIDY_PROCESS_PVT.get_funding_subsidy_amount(
8386         p_api_version    => l_api_version,
8387         p_init_msg_list  => l_init_msg_list,
8388         x_return_status  => x_return_status,
8389         x_msg_count      => x_msg_count,
8390         x_msg_data       => x_msg_data,
8391         p_chr_id         => p_chr_id,
8392         p_asset_cle_id   => p_asset_cle_id,
8393         p_vendor_id      => l_vendor_id,
8394         x_subsidy_amount => x_subsidy_amount
8395 
8396     );
8397 
8398 
8399     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8400         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8401     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8402         RAISE OKL_API.G_EXCEPTION_ERROR;
8403     END IF;
8404 
8405     RETURN x_subsidy_amount;
8406 
8407   EXCEPTION
8408     WHEN OTHERS THEN
8409 
8410       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8411       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8412                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8413                           p_token1        => 'OKL_SQLCODE',
8414                           p_token1_value  => SQLCODE,
8415                           p_token2        => 'OKL_SQLERRM',
8416                           p_token2_value  => SQLERRM);
8417       RETURN NULL;
8418 
8419 END get_funding_subsidy_amount;
8420 
8421 
8422 -------------------------------------------------------------------------
8423  FUNCTION get_partial_subsidy_amount(
8424     p_asset_cle_id                 IN  NUMBER,
8425     p_req_fund_amount              IN  NUMBER
8426 ) RETURN NUMBER
8427 IS
8428     l_amount            NUMBER := 0;
8429     i                   NUMBER;
8430     l_api_version       NUMBER := 1.0;
8431     l_init_msg_list     VARCHAR2(1) := OKL_API.G_FALSE;
8432     x_return_status     VARCHAR2(1);
8433     x_msg_count         NUMBER;
8434     x_msg_data          VARCHAR2(2000);
8435     x_partial_subsidy_amount    NUMBER := 0;
8436     x_asbv_tbl OKL_SUBSIDY_PROCESS_PVT.asbv_tbl_type;
8437 
8438 BEGIN
8439 
8440 
8441     OKL_SUBSIDY_PROCESS_PVT.get_partial_subsidy_amount(
8442         p_api_version     => l_api_version,
8443         p_init_msg_list   => l_init_msg_list,
8444         x_return_status   => x_return_status,
8445         x_msg_count       => x_msg_count,
8446         x_msg_data        => x_msg_data,
8447         p_asset_cle_id    => p_asset_cle_id,
8448         p_req_fund_amount => p_req_fund_amount,
8449         x_asbv_tbl        => x_asbv_tbl
8450     );
8451 
8452     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8453         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8454     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8455         RAISE OKL_API.G_EXCEPTION_ERROR;
8456     END IF;
8457 
8458     IF (x_asbv_tbl.COUNT > 0) THEN
8459       i := x_asbv_tbl.FIRST;
8460 
8461       LOOP
8462 
8463         l_amount := l_amount + x_asbv_tbl(i).amount;
8464         EXIT WHEN (i = x_asbv_tbl.LAST);
8465         i := x_asbv_tbl.NEXT(i);
8466       END LOOP;
8467     END IF;
8468 
8469     RETURN l_amount;
8470 
8471   EXCEPTION
8472     WHEN OTHERS THEN
8473       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8474 
8475       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8476                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8477                           p_token1        => 'OKL_SQLCODE',
8478                           p_token1_value  => SQLCODE,
8479                           p_token2        => 'OKL_SQLERRM',
8480                           p_token2_value  => SQLERRM);
8481       RETURN NULL;
8482 
8483 END get_partial_subsidy_amount;
8484 
8485 
8486 -- cklee 04-May-2004
8487 /*---------------------------------------------------------------------------+
8488 |                                                                            |
8489 |  FUNCTION: get_amount_manu_disb                                            |
8490 |  DESC   : Sum of all approved requests for specific contract               |
8491 |           where funding type = 'MANUAL_DISB'                               |
8492 |  IN     : p_contract_id                                                    |
8493 |  OUT NOCOPY    : amount                                                    |
8494 |  HISTORY: 04-MAY-04 [email protected] -- Created                    |
8495 |                                                                            |
8496 *-------------------------------------------------------------------------- */
8497 FUNCTION get_amount_manu_disb(
8498  p_contract_id                   IN NUMBER
8499  ,p_contract_line_id             IN NUMBER
8500 ) RETURN NUMBER
8501 IS
8502   l_amount NUMBER := 0;
8503 
8504   CURSOR c_manu_disb (p_contract_id  NUMBER)
8505   IS
8506   -- select nvl(sum(decode(sub.invoice_type, 'CREDIT', -subln.amount, subln.amount)),0)
8507   -- sjalasut, commented the above select as part of OKLR12B disbursements project
8508   select nvl(sum(subln.amount),0)
8509   from okl_trx_ap_invoices_b sub,
8510        okl_txl_ap_inv_lns_b subln
8511   where sub.id = subln.tap_id
8512   and sub.trx_status_code in ('APPROVED', 'PROCESSED')
8513   and sub.funding_type_code = G_MANUAL_DISB
8514   and subln.khr_id = p_contract_id;
8515   -- sjalasut, commented the reference of khr_id. p_contract_id now joins with
8516   -- subln instead of sub. changes made as part of OKLR12B disbursements project
8517   -- and sub.khr_id = p_contract_id
8518 
8519 BEGIN
8520 
8521   OPEN c_manu_disb (p_contract_id);
8522   FETCH c_manu_disb INTO l_amount;
8523   CLOSE c_manu_disb;
8524 
8525   RETURN l_amount;
8526   EXCEPTION
8527     WHEN OTHERS THEN
8528       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8529       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8530                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8531                           p_token1        => 'OKL_SQLCODE',
8532                           p_token1_value  => SQLCODE,
8533                           p_token2        => 'OKL_SQLERRM',
8534                           p_token2_value  => SQLERRM);
8535       RETURN 0;
8536 
8537 
8538 END get_amount_manu_disb;
8539 
8540 -- strat: T and A 11/04/2004
8541 -- Total contract can be funded fee amount
8542 ----------------------------------------------------------------------------------
8543 -- Start of comments
8544 --
8545 -- Procedure Name  : Total contract can be funded fee amount
8546 -- Description     : Total contract can be funded fee amount for a fee line
8547 --                   by an given date
8548 --                   IN: p_contract_id is the lease contract ID
8549 --                   IN: p_fee_line_id is the lease contract fee line ID
8550 --                   IN: p_effective_date is the effective date of the total fee amount
8551 --                   OUT: x_value is the fee amount
8552 -- Business Rules  : x_value will be 0 if fee line has not meet the following requirements
8553 --                 : 1. Effective date greater than line start date
8554 --                      (or contract start date if line start date is null)
8555 --                   2. contract okc_k_headers_b.ste_code
8556 --                      in ('ENTERED', 'ACTIVE','SIGNED')
8557 --                   3. fee line is not passthrough
8558 --                   4. fee line is associated with vendor
8559 -- Business Rules  :
8560 -- Parameters      :
8561 -- Version         : 1.0
8562 -- End of comments
8563 ----------------------------------------------------------------------------------
8564  PROCEDURE contract_fee_canbe_funded(
8565     p_api_version                  IN NUMBER
8566    ,p_init_msg_list                IN VARCHAR2
8567    ,x_return_status                OUT NOCOPY VARCHAR2
8568    ,x_msg_count                    OUT NOCOPY NUMBER
8569    ,x_msg_data                     OUT NOCOPY VARCHAR2
8570    ,x_value                        OUT NOCOPY NUMBER
8571    ,p_contract_id                  IN NUMBER
8572    ,p_fee_line_id                  IN NUMBER
8573    ,p_effective_date               IN DATE
8574  )
8575 IS
8576   l_api_name        CONSTANT VARCHAR2(30) := 'contract_fee_canbe_funded';
8577   l_api_version     CONSTANT NUMBER       := 1.0;
8578   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
8579 
8580 BEGIN
8581   -- Set API savepoint
8582 
8583   SAVEPOINT contract_fee_canbe_funded_PVT;
8584 
8585   -- Check for call compatibility
8586   IF (NOT FND_API.Compatible_API_Call (l_api_version,
8587                                 	   p_api_version,
8588                                 	   l_api_name,
8589                                 	   G_PKG_NAME ))
8590   THEN
8591     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8592   END IF;
8593 
8594   -- Initialize message list if requested
8595   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
8596       FND_MSG_PUB.initialize;
8597 	END IF;
8598 
8599   -- Initialize API status to success
8600   x_return_status := OKL_API.G_RET_STS_SUCCESS;
8601 
8602 
8603 /*** Begin API body ****************************************************/
8604   x_value := get_chr_fee_canbe_funded_amt(
8605               p_contract_id    => p_contract_id
8606              ,p_fee_line_id    => p_fee_line_id
8607              ,p_effective_date => p_effective_date);
8608 
8609 /*** End API body ******************************************************/
8610 
8611   -- Get message count and if count is 1, get message info
8612 	FND_MSG_PUB.Count_And_Get
8613     (p_count          =>      x_msg_count,
8614      p_data           =>      x_msg_data);
8615 
8616 EXCEPTION
8617   WHEN OKL_API.G_EXCEPTION_ERROR THEN
8618     ROLLBACK TO contract_fee_canbe_funded;
8619     x_return_status := OKL_API.G_RET_STS_ERROR;
8620     FND_MSG_PUB.Count_And_Get
8621       (p_count         =>      x_msg_count,
8622        p_data          =>      x_msg_data);
8623 
8624   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
8625     ROLLBACK TO contract_fee_canbe_funded;
8626     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
8627     FND_MSG_PUB.Count_And_Get
8628       (p_count         =>      x_msg_count,
8629        p_data          =>      x_msg_data);
8630 
8631   WHEN OTHERS THEN
8632 	ROLLBACK TO contract_fee_canbe_funded;
8633       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8634 
8635       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
8636                           p_msg_name      => G_UNEXPECTED_ERROR,
8637                           p_token1        => G_SQLCODE_TOKEN,
8638                           p_token1_value  => SQLCODE,
8639                           p_token2        => G_SQLERRM_TOKEN,
8640                           p_token2_value  => SQLERRM);
8641       FND_MSG_PUB.Count_And_Get
8642         (p_count         =>      x_msg_count,
8643          p_data          =>      x_msg_data);
8644 
8645 
8646 END contract_fee_canbe_funded;
8647 
8648 ----------------------------------------------------------------------------------
8649 -- Start of comments
8650 --
8651 -- Procedure Name  : get_chr_fee_canbe_funded_amt
8652 -- Description     : Total contract can be funded fee amount for a fee line
8653 --                   by an given date
8654 --                   IN: p_contract_id is the lease contract ID
8655 --                   IN: p_fee_line_id is the lease contract fee line ID
8656 --                   IN: p_effective_date is the effective date of the total fee amount
8657 -- Business Rules  : x_value will be 0 if fee line has not meet the following requirements
8658 --                 : 1. Effective date greater than line start date
8659 --                      (or contract start date if line start date is null)
8660 --                   2. contract okc_k_headers_b.ste_code
8661 --                      in ('ENTERED', 'ACTIVE','SIGNED')
8662 --                   3. fee line is not passthrough
8663 --                   4. fee line is associated with vendor
8664 -- Parameters      :
8665 -- Version         : 1.0
8666 -- End of comments
8667 ----------------------------------------------------------------------------------
8668  FUNCTION get_chr_fee_canbe_funded_amt(
8669   p_contract_id                IN NUMBER                 -- contract hdr
8670   ,p_fee_line_id               IN NUMBER
8671   ,p_effective_date            IN DATE
8672 ) RETURN NUMBER
8673 IS
8674   l_amount NUMBER := 0;
8675   l_cle_amount NUMBER := 0;
8676   l_amount_per NUMBER := 0;
8677 
8678   l_cle_id NUMBER := 0;
8679   l_cle_start_date DATE;
8680   l_period NUMBER := 0;
8681   l_period_org NUMBER := 0;
8682   l_row_notfound   BOOLEAN;
8683 
8684   CURSOR c (p_contract_id  NUMBER,
8685             p_rle_code     VARCHAR2,
8686             p_fee_line_id  NUMBER)
8687   IS
8688 select nvl(cle.AMOUNT,0),
8689        cle.id,
8690        nvl(cle.start_date,k.start_date)
8691 from   OKL_K_LINES_FULL_V cle,
8692        okc_k_party_roles_b cpl,
8693        okc_line_styles_b ls,
8694        okc_k_headers_b k
8695 where  k.id           = cle.dnz_chr_id
8696 and    cle.dnz_chr_id = p_contract_id
8697 and    cle.lse_id     = ls.id
8698 and    ls.lty_code    = p_rle_code
8699 and    cle.id         = cpl.cle_id
8700 and    cpl.dnz_chr_id = p_contract_id
8701 and    cpl.chr_id     is null
8702 and    cpl.rle_code   = 'OKL_VENDOR'
8703 and    cle.id         = p_fee_line_id
8704 --and    cpl.object1_id1 = to_char(p_vendor_id)
8705 --and    cpl.object1_id2 = '#'
8706 -- Pass through check
8707 /*
8708 and not exists (select null
8709                 from   okc_rule_groups_v crg,
8710                        okc_rules_v cr
8711                 where  crg.dnz_chr_id = p_contract_id
8712                 and    crg.cle_id     = cle.id -- line id for rle_code
8713                 and    crg.id         = cr.rgp_id
8714                 and    crg.rgd_code   = 'LAPSTH') */
8715 and not exists (select null
8716                 from   okl_party_payment_hdr phr
8717                 where  phr.dnz_chr_id = p_contract_id
8718                 and    phr.cle_id = cle.id)
8719 ;
8720 
8721 
8722 --
8723 -- get Number of Period
8724 --
8725 -- 1) take contract start date if cle start date is null
8726 -- 2) truncate pay period if less than 0
8727 --
8728   CURSOR c_period (p_contract_id    NUMBER,
8729                    p_cle_id         NUMBER,
8730                    p_effective_date DATE)
8731   IS
8732 select ceil(decode(cr.object1_id1, 'A', months_between(p_effective_date, nvl(cle.start_date, k.start_date))/12
8733                             , 'M', months_between(p_effective_date, nvl(cle.start_date, k.start_date))
8734                             , 'Q', months_between(p_effective_date, nvl(cle.start_date, k.start_date))/3
8735                             , 'S', months_between(p_effective_date, nvl(cle.start_date, k.start_date))/6
8736                             , months_between(p_effective_date, nvl(cle.start_date, k.start_date))))
8737 from   okc_rule_groups_v crg,
8738        okc_rules_v cr,
8739        OKL_K_LINES_FULL_V cle,
8740        okc_k_headers_b k
8741 where  crg.dnz_chr_id = p_contract_id
8742 and    cle.dnz_chr_id = k.id
8743 and    crg.id         = cr.rgp_id
8744 and    crg.rgd_code   = 'LAFEXP'
8745 and    crg.cle_id     = cle.id
8746 and    cr.RULE_INFORMATION_CATEGORY = 'LAFREQ'
8747 and    cle.id         = p_cle_id
8748 ;
8749 
8750 --
8751 -- get amount per period
8752 --
8753 --
8754   CURSOR c_amount_per (p_contract_id NUMBER,
8755                        p_cle_id NUMBER)
8756   IS
8757 select to_number(nvl(cr.RULE_INFORMATION1,'0'))
8758        ,to_number(nvl(cr.RULE_INFORMATION2,'0'))
8759 from   okc_rule_groups_v crg,
8760        okc_rules_v cr
8761 where  crg.dnz_chr_id = p_contract_id
8762 and    crg.id         = cr.rgp_id
8763 and    crg.rgd_code   = 'LAFEXP'
8764 and    cr.RULE_INFORMATION_CATEGORY = 'LAFEXP'
8765 and    crg.cle_id     = p_cle_id
8766 ;
8767 
8768 
8769 BEGIN
8770   IF ((p_contract_id IS NULL OR p_contract_id = OKL_API.G_MISS_NUM) or
8771       (p_fee_line_id IS NULL OR p_fee_line_id = OKL_API.G_MISS_NUM) or
8772       (p_effective_date IS NULL OR p_effective_date = OKL_API.G_MISS_DATE))
8773   THEN
8774     RETURN 0;  -- error
8775   ELSE
8776 
8777     ----------------------------------------------------
8778     -- FEE line
8779 
8780     ----------------------------------------------------
8781     OPEN c (p_contract_id, 'FEE', p_fee_line_id);
8782     LOOP
8783 
8784       FETCH c into l_cle_amount,
8785                    l_cle_id,
8786                    l_cle_start_date;
8787 
8788       EXIT WHEN c%NOTFOUND;
8789 
8790 
8791       OPEN c_amount_per (p_contract_id, l_cle_id);
8792       FETCH c_amount_per INTO l_period_org,
8793                               l_amount_per;
8794 
8795       l_row_notfound := c_amount_per%NOTFOUND;
8796       CLOSE c_amount_per;
8797 
8798       -- if recurring records doesn't exists
8799       IF (l_row_notfound) THEN
8800 
8801         -- either fee effective date or contract effective date <= p_effective_date
8802         IF ( l_cle_start_date <= p_effective_date ) THEN
8803           l_amount := l_amount + l_cle_amount;
8804 
8805         END IF;
8806 
8807       ELSE
8808 
8809         OPEN c_period (p_contract_id, l_cle_id, p_effective_date);
8810         FETCH c_period INTO l_period;
8811         CLOSE c_period;
8812 
8813         -- calculate only if period is positive
8814         IF (l_period > 0) THEN
8815 
8816           IF (l_period > l_period_org) THEN
8817             l_period := l_period_org;
8818           END IF;
8819           l_amount := l_amount + (l_amount_per * l_period);
8820         END IF;
8821 
8822 
8823       END IF;
8824 
8825     END LOOP;
8826 
8827     CLOSE c;
8828 
8829   END IF; -- end if p_contract_id check
8830 
8831   IF (l_amount IS NULL) THEN
8832     l_amount := 0;
8833   END IF;
8834 
8835   IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 0) THEN
8836     l_amount := 0;
8837   END IF;
8838 
8839   RETURN l_amount;
8840 
8841   EXCEPTION
8842     WHEN OTHERS THEN
8843       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8844       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8845                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8846                           p_token1        => 'OKL_SQLCODE',
8847                           p_token1_value  => SQLCODE,
8848                           p_token2        => 'OKL_SQLERRM',
8849                           p_token2_value  => SQLERRM);
8850       RETURN 0;
8851 
8852 
8853 END;
8854 
8855 -- end: T and A 11/04/2004
8856 
8857 -- strat: T and A bug#4151222
8858  FUNCTION is_contract_fully_funded(
8859   p_contract_id                IN NUMBER
8860  ) RETURN boolean
8861 IS
8862   l_flag               boolean := false;
8863   l_chr_canbe_funded   number;
8864   l_amount_oec         number;
8865   l_amount_expense     number;
8866   l_amount_hasbeen_oec number;
8867   l_amount_hasbeen_exp number;
8868 
8869 
8870 BEGIN
8871 
8872   l_amount_oec := get_chr_oec_canbe_funded(p_contract_id);
8873   l_amount_expense := get_chr_exp_canbe_funded_amt(p_contract_id);
8874   l_amount_hasbeen_oec := get_chr_oec_hasbeen_funded_amt(p_contract_id);
8875   l_amount_hasbeen_exp := get_chr_exp_hasbeen_funded_amt(p_contract_id);
8876 
8877   --
8878   -- has been funded may over totoal fundable amount due to asset termination
8879   --
8880   if ( l_amount_hasbeen_oec + l_amount_hasbeen_exp >=
8881        l_amount_oec + l_amount_expense) then
8882     l_flag := true;
8883   else
8884     l_flag := false;
8885   end if;
8886 
8887   RETURN l_flag;
8888 
8889   EXCEPTION
8890     WHEN OTHERS THEN
8891       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8892       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8893                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8894                           p_token1        => 'OKL_SQLCODE',
8895                           p_token1_value  => SQLCODE,
8896                           p_token2        => 'OKL_SQLERRM',
8897                           p_token2_value  => SQLERRM);
8898       RETURN false;
8899 
8900 END is_contract_fully_funded;
8901 ----------------------------------------------------------------------------------
8902 -- Start of comments
8903 --
8904 -- Procedure Name  : Is contract fully funded
8905 -- Description     : Is contract fully funded
8906 --                   IN: p_contract_id is the lease contract ID
8907 --                   OUT: x_value is the flag to indicate if contract is fully funded
8908 -- Business Rules  : x_value will be false if error occurred
8909 -- Parameters      :
8910 -- Version         : 1.0
8911 -- End of comments
8912 ----------------------------------------------------------------------------------
8913  PROCEDURE is_contract_fully_funded(
8914     p_api_version                  IN NUMBER
8915    ,p_init_msg_list                IN VARCHAR2 DEFAULT OKL_API.G_FALSE
8916    ,x_return_status                OUT NOCOPY VARCHAR2
8917    ,x_msg_count                    OUT NOCOPY NUMBER
8918    ,x_msg_data                     OUT NOCOPY VARCHAR2
8919    ,x_value                        OUT NOCOPY BOOLEAN
8920    ,p_contract_id                  IN NUMBER
8921  )
8922 IS
8923   l_api_name        CONSTANT VARCHAR2(30) := 'is_contract_fully_funded';
8924   l_api_version     CONSTANT NUMBER       := 1.0;
8925   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
8926 
8927 BEGIN
8928   -- Set API savepoint
8929 
8930   SAVEPOINT is_contract_fully_funded_pvt;
8931 
8932   -- Check for call compatibility
8933   IF (NOT FND_API.Compatible_API_Call (l_api_version,
8934                                 	   p_api_version,
8935                                 	   l_api_name,
8936                                 	   G_PKG_NAME ))
8937   THEN
8938     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8939   END IF;
8940 
8941   -- Initialize message list if requested
8942   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
8943       FND_MSG_PUB.initialize;
8944 	END IF;
8945 
8946   -- Initialize API status to success
8947   x_return_status := OKL_API.G_RET_STS_SUCCESS;
8948 
8949 
8950 /*** Begin API body ****************************************************/
8951   x_value := is_contract_fully_funded(
8952               p_contract_id    => p_contract_id);
8953 
8954 /*** End API body ******************************************************/
8955 
8956   -- Get message count and if count is 1, get message info
8957 	FND_MSG_PUB.Count_And_Get
8958     (p_count          =>      x_msg_count,
8959      p_data           =>      x_msg_data);
8960 
8961 EXCEPTION
8962   WHEN OKL_API.G_EXCEPTION_ERROR THEN
8963     ROLLBACK TO is_contract_fully_funded;
8964     x_return_status := OKL_API.G_RET_STS_ERROR;
8965     FND_MSG_PUB.Count_And_Get
8966       (p_count         =>      x_msg_count,
8967        p_data          =>      x_msg_data);
8968 
8969   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
8970     ROLLBACK TO is_contract_fully_funded;
8971     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
8972     FND_MSG_PUB.Count_And_Get
8973       (p_count         =>      x_msg_count,
8974        p_data          =>      x_msg_data);
8975 
8976   WHEN OTHERS THEN
8977 	ROLLBACK TO is_contract_fully_funded;
8978       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8979 
8980       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
8981                           p_msg_name      => G_UNEXPECTED_ERROR,
8982                           p_token1        => G_SQLCODE_TOKEN,
8983                           p_token1_value  => SQLCODE,
8984                           p_token2        => G_SQLERRM_TOKEN,
8985                           p_token2_value  => SQLERRM);
8986       FND_MSG_PUB.Count_And_Get
8987         (p_count         =>      x_msg_count,
8988          p_data          =>      x_msg_data);
8989 
8990 
8991 END is_contract_fully_funded;
8992 
8993 
8994 -- end: T and A bug#4151222
8995 
8996 --Added procedure get_checklist_source as part of bug 5912358, Funding OA Migration Issues
8997  ----------------------------------------------------------------------------------
8998 -- Start of comments
8999 --
9000 -- Procedure Name  : get_checklist_source
9001 -- Description     : Returns checklist source details whether contract was originated from lease app, whether checklist exists or not and get source checklist template.
9002 --                   IN: p_chr_id is the contract ID
9003 --                   OUT: x_lease_app_found returns where contract was originated from leaseapp or not
9004 --                   OUT: x_lease_app_list_found returns whether lease checklist exists or not
9005 --                   OUT: x_funding_checklist_tpl returns source checklist template ID
9006 --                   OUT: x_lease_app_id returns lease application id
9007 --                   OUT: x_credit_id returns credit template id
9008 -- Business Rules  :
9009 -- Parameters      :
9010 -- Version         : 1.0
9011 -- End of comments
9012 ----------------------------------------------------------------------------------
9013  PROCEDURE get_checklist_source(
9014     p_api_version                  IN NUMBER
9015    ,p_init_msg_list                IN VARCHAR2 DEFAULT OKL_API.G_FALSE
9016    ,x_return_status                OUT NOCOPY VARCHAR2
9017    ,x_msg_count                    OUT NOCOPY NUMBER
9018    ,x_msg_data                     OUT NOCOPY VARCHAR2
9019    ,p_chr_id                        IN okc_k_headers_b.id%type
9020    ,x_lease_app_found       OUT NOCOPY VARCHAR2
9021    ,x_lease_app_list_found OUT NOCOPY VARCHAR2
9022    ,x_funding_checklist_tpl OUT NOCOPY okc_rules_b.rule_information2%TYPE
9023    ,x_lease_app_id          OUT NOCOPY NUMBER
9024    ,x_credit_id                OUT NOCOPY NUMBER
9025  ) IS
9026 
9027   l_lease_app_id number := OKC_API.G_MISS_NUM;
9028   l_lease_app_found boolean := FALSE;
9029   l_dummy number;
9030   l_lease_app_list_found boolean := FALSE;
9031   l_credit_id okc_k_headers_b.id%TYPE;
9032   l_funding_checklist_tpl okc_rules_b.rule_information2%TYPE := OKC_API.G_MISS_CHAR;
9033 
9034  --------------------------------------------------------------------------------------------
9035 -- Checklists link check
9036 --------------------------------------------------------------------------------------------
9037 CURSOR c_checklists (p_credit_id  NUMBER)
9038   IS
9039   select rule.rule_information2
9040   from okc_rules_b rule
9041   where rule.dnz_chr_id = p_credit_id
9042   and   rule.rule_information_category = G_CREDIT_CHKLST_TPL_RULE1
9043   ;
9044 
9045 ---------------------------------------------------------------------------------------------------------
9046 -- check if the contract was created from a lease application
9047 ---------------------------------------------------------------------------------------------------------
9048 CURSOR c_lease_app (p_chr_id okc_k_headers_b.id%type)
9049 IS
9050   select chr.ORIG_SYSTEM_ID1
9051 from  okc_k_headers_b chr
9052 where ORIG_SYSTEM_SOURCE_CODE = G_OKL_LEASE_APP
9053 and   chr.id = p_chr_id
9054 ;
9055 
9056 cursor c_lease_app_list_exists (p_lease_app_id number) is
9057 select 1
9058 from OKL_CHECKLIST_DETAILS chk
9059      ,okl_checklists hdr
9060 where chk.ckl_id = hdr.id
9061 and hdr.CHECKLIST_OBJ_ID = p_lease_app_id
9062 and chk.INST_CHECKLIST_TYPE = 'FUNDING'
9063 ;
9064 
9065  BEGIN
9066 
9067   OPEN c_lease_app(p_chr_id);
9068   FETCH c_lease_app INTO l_lease_app_id;
9069   l_lease_app_found := c_lease_app%FOUND;
9070   CLOSE c_lease_app;
9071 
9072 
9073   IF l_lease_app_id IS NOT NULL THEN
9074     OPEN c_lease_app_list_exists(l_lease_app_id);
9075     FETCH c_lease_app_list_exists INTO l_dummy;
9076     l_lease_app_list_found := c_lease_app_list_exists%FOUND;
9077     CLOSE c_lease_app_list_exists;
9078   END IF;
9079 
9080   IF NOT l_lease_app_found THEN
9081 -- get credit line id
9082 -- If contract credit line has been changed, but funding requests
9083 -- still have not been apporved. We need to re-generate list from the
9084 -- new credit line
9085     l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_chr_id);
9086 
9087 -- get source checklist template ID
9088     OPEN c_checklists(l_credit_id);
9089     FETCH c_checklists INTO l_funding_checklist_tpl;
9090     CLOSE c_checklists;
9091   END IF;
9092   IF (l_lease_app_found) THEN
9093 	x_lease_app_found := 'TRUE';
9094   ELSE
9095 	x_lease_app_found := 'FALSE';
9096   END IF;
9097   IF (l_lease_app_list_found) THEN
9098 	x_lease_app_list_found := 'TRUE';
9099   ELSE
9100 	x_lease_app_list_found := 'FALSE';
9101   END IF;
9102   x_funding_checklist_tpl := l_funding_checklist_tpl;
9103   x_lease_app_id := l_lease_app_id;
9104   x_credit_id := l_credit_id;
9105   x_return_status := 'S';
9106  EXCEPTION
9107      WHEN G_EXCEPTION_HALT_VALIDATION THEN
9108       x_return_status := OKL_API.G_RET_STS_ERROR;
9109     WHEN OTHERS THEN
9110       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
9111       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
9112                           p_msg_name      => G_UNEXPECTED_ERROR,
9113                           p_token1        => G_SQLCODE_TOKEN,
9114                           p_token1_value  => SQLCODE,
9115                           p_token2        => G_SQLERRM_TOKEN,
9116                           p_token2_value  => SQLERRM);
9117 
9118  END get_checklist_source;
9119 
9120 -- start: 24-May-2005  cklee okl.h Lease App IA Authoring
9121 ----------------------------------------------------------------------------------
9122 -- Start of comments
9123 --
9124 -- Procedure Name  : update_checklist_function
9125 -- Description     : This API will execute function for each item and
9126 --                   update the execution results for the function.
9127 -- Business Rules  :
9128 -- Parameters      :
9129 -- Version         : 1.0
9130 -- End of comments
9131 ----------------------------------------------------------------------------------
9132  PROCEDURE update_checklist_function(
9133     p_api_version                  IN NUMBER
9134    ,p_init_msg_list                IN VARCHAR2
9135    ,x_return_status                OUT NOCOPY VARCHAR2
9136    ,x_msg_count                    OUT NOCOPY NUMBER
9137    ,x_msg_data                     OUT NOCOPY VARCHAR2
9138    ,p_fund_req_id                  IN  NUMBER
9139  ) is
9140   l_api_name         CONSTANT VARCHAR2(30) := 'update_checklist_function';
9141   l_api_version      CONSTANT NUMBER       := 1.0;
9142   i                  NUMBER;
9143   l_return_status    VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
9144   l_dummy  number;
9145 
9146   l_row_not_found boolean := false;
9147 
9148   lp_rulv_tbl        okl_funding_checklist_pvt.rulv_tbl_type;
9149   lx_rulv_tbl        okl_funding_checklist_pvt.rulv_tbl_type;
9150   plsql_block        VARCHAR2(500);
9151 
9152   lp_return_status   okl_funding_checklists_uv.FUNCTION_VALIDATE_RSTS%type;
9153   lp_fund_rst        okl_funding_checklists_uv.FUNCTION_VALIDATE_RSTS%type;
9154   lp_msg_data        okl_funding_checklists_uv.FUNCTION_VALIDATE_MSG%type;
9155   l_contract_id      okl_funding_checklists_uv.KHR_ID%type;
9156 
9157 -- get checklist template attributes
9158 cursor c_clist_funs (p_fund_req_id varchar2) is
9159 --start modified abhsaxen for performance SQLID 20562504
9160 SELECT rult.ID,  rult.DNZ_CHR_ID khr_id,
9161 fun.source function_source
9162 FROM OKC_RULES_B RULT,
9163 OKL_DATA_SRC_FNCTNS_B FUN
9164 WHERE rult.rule_information_category = 'LAFCLD'
9165 and rult.object1_id1 = p_fund_req_id
9166 and rult.RULE_INFORMATION9 = fun.Id   ;
9167 --end modified abhsaxen for performance SQLID 20562504
9168 
9169 begin
9170   -- Set API savepoint
9171   SAVEPOINT update_checklist_function;
9172 
9173   -- Check for call compatibility
9174   IF (NOT FND_API.Compatible_API_Call (l_api_version,
9175                                 	   p_api_version,
9176                                 	   l_api_name,
9177                                 	   G_PKG_NAME ))
9178   THEN
9179     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9180   END IF;
9181 
9182   -- Initialize message list if requested
9183   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
9184       FND_MSG_PUB.initialize;
9185 	END IF;
9186 
9187   -- Initialize API status to success
9188   x_return_status := OKL_API.G_RET_STS_SUCCESS;
9189 
9190 
9191 /*** Begin API body ****************************************************/
9192     ------------------------------------------------------------------------
9193     -- execute function for each to do item and save the return to each row
9194     ------------------------------------------------------------------------
9195     i := 0;
9196     --modified abhsaxen pass p_fund_req_id in varchar2 format for performannce SQLID 20562504
9197     FOR r_this_row IN c_clist_funs (TO_CHAR(p_fund_req_id)) LOOP
9198 
9199       BEGIN
9200 
9201         l_contract_id := r_this_row.khr_id;
9202 --START:| 02-Mar-2006  cklee -- Fixed bug#5068910                                    |
9203 --        plsql_block := 'BEGIN :l_rtn := '|| r_this_row.FUNCTION_SOURCE ||'(:l_contract_id); END;';
9204 --        EXECUTE IMMEDIATE plsql_block USING OUT lp_return_status, l_contract_id;
9205         plsql_block := 'BEGIN :l_rtn := '|| r_this_row.FUNCTION_SOURCE ||'(:l_contract_id, :l_fund_req_id); END;';
9206         EXECUTE IMMEDIATE plsql_block USING OUT lp_return_status, l_contract_id, p_fund_req_id;
9207 --END:| 02-Mar-2006  cklee -- Fixed bug#5068910                                    |
9208 
9209         IF lp_return_status = 'P' THEN
9210           lp_fund_rst := 'PASSED';
9211           lp_msg_data := 'Passed';
9212         ELSIF lp_return_status = 'F' THEN
9213           lp_fund_rst := 'FAILED';
9214           lp_msg_data := 'Failed';
9215         ELSE
9216           lp_fund_rst := 'ERROR';
9217           lp_msg_data := r_this_row.FUNCTION_SOURCE || ' returns: ' || lp_return_status;
9218         END IF;
9219 
9220       EXCEPTION
9221         WHEN OKL_API.G_EXCEPTION_ERROR THEN
9222           lp_fund_rst := 'ERROR';
9223           FND_MSG_PUB.Count_And_Get
9224             (p_count         =>      x_msg_count,
9225              p_data          =>      x_msg_data);
9226           lp_msg_data := substr('Application error: ' || x_msg_data, 240);
9227 
9228         WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
9229           lp_fund_rst := 'ERROR';
9230           FND_MSG_PUB.Count_And_Get
9231             (p_count         =>      x_msg_count,
9232              p_data          =>      x_msg_data);
9233           lp_msg_data := substr('Unexpected application error: ' || x_msg_data, 240);
9234 
9235         WHEN OTHERS THEN
9236           lp_fund_rst := 'ERROR';
9237           lp_msg_data := substr('Unexpected system error: ' || SQLERRM, 240);
9238 
9239       END;
9240 
9241       lp_rulv_tbl(i).ID := r_this_row.ID;
9242       lp_rulv_tbl(i).RULE_INFORMATION7 := lp_fund_rst;
9243       lp_rulv_tbl(i).RULE_INFORMATION8 := lp_msg_data;
9244       i := i + 1;
9245 
9246     END LOOP;
9247 
9248     IF lp_rulv_tbl.count > 0 THEN
9249 
9250       okl_funding_checklist_pvt.update_funding_chklst(
9251           p_api_version    => p_api_version,
9252           p_init_msg_list  => p_init_msg_list,
9253           x_return_status  => x_return_status,
9254           x_msg_count      => x_msg_count,
9255           x_msg_data       => x_msg_data,
9256           p_rulv_tbl       => lp_rulv_tbl,
9257           x_rulv_tbl       => lx_rulv_tbl);
9258 
9259       If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
9260         raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
9261       Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
9262         raise OKC_API.G_EXCEPTION_ERROR;
9263       End If;
9264     END IF;
9265 
9266 /*** End API body ******************************************************/
9267 
9268   -- Get message count and if count is 1, get message info
9269 	FND_MSG_PUB.Count_And_Get
9270     (p_count          =>      x_msg_count,
9271      p_data           =>      x_msg_data);
9272 
9273 EXCEPTION
9274   WHEN OKL_API.G_EXCEPTION_ERROR THEN
9275     ROLLBACK TO update_checklist_function;
9276     x_return_status := OKL_API.G_RET_STS_ERROR;
9277     FND_MSG_PUB.Count_And_Get
9278       (p_count         =>      x_msg_count,
9279        p_data          =>      x_msg_data);
9280 
9281   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
9282     ROLLBACK TO update_checklist_function;
9283     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
9284     FND_MSG_PUB.Count_And_Get
9285       (p_count         =>      x_msg_count,
9286        p_data          =>      x_msg_data);
9287 
9288   WHEN OTHERS THEN
9289 
9290 	ROLLBACK TO update_checklist_function;
9291       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
9292       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
9293                           p_msg_name      => G_UNEXPECTED_ERROR,
9294                           p_token1        => G_SQLCODE_TOKEN,
9295                           p_token1_value  => SQLCODE,
9296                           p_token2        => G_SQLERRM_TOKEN,
9297                           p_token2_value  => SQLERRM);
9298       FND_MSG_PUB.Count_And_Get
9299         (p_count         =>      x_msg_count,
9300          p_data          =>      x_msg_data);
9301 
9302 end update_checklist_function;
9303 -- end: 24-May-2005  cklee okl.h Lease App IA Authoring
9304 
9305 ----------------------------------------------------------------------------
9306 /*
9307 -- zrehman, 10/13/2006
9308 -- START, Added PROCEDURE to get Funding Summary
9309 */
9310 -- Start of comments
9311 --
9312 -- Procedure Name  : get_fund_summary
9313 -- Description     : public_procedure, gets fund summary by making calls to
9314 --                   functions in OKL_FUND_SMRY_PVT
9315 -- Business Rules  :
9316 -- Parameters      : contract ID
9317 -- Version         : 1.0
9318 -- End of comments
9319 --
9320 
9321 PROCEDURE get_fund_summary(
9322                 p_api_version       IN NUMBER,
9323                 p_init_msg_list     IN VARCHAR2 DEFAULT OKL_API.G_FALSE,
9324                 x_return_status     OUT NOCOPY VARCHAR2,
9325                 x_msg_count         OUT NOCOPY NUMBER,
9326                 x_msg_data          OUT NOCOPY VARCHAR2,
9327                 p_contract_id       IN NUMBER,
9328 		x_fnd_rec           OUT NOCOPY fnd_rec_type
9329                 ) IS
9330 
9331 BEGIN
9332 
9333    x_fnd_rec.TOTAL_FUNDABLE_AMOUNT := OKL_FUNDING_PVT.get_chr_canbe_funded(p_contract_id);
9334    x_fnd_rec.TOTAL_PRE_FUNDED := OKL_FUNDING_PVT.get_amount_prefunded(p_contract_id);
9335    x_fnd_rec.TOTAL_ASSETS_FUNDED := OKL_FUNDING_PVT.get_chr_oec_hasbeen_funded_amt(p_contract_id);
9336    x_fnd_rec.TOTAL_EXPENSES_FUNDED := OKL_FUNDING_PVT.get_chr_exp_hasbeen_funded_amt(p_contract_id);
9337    x_fnd_rec.TOTAL_ADJUSTMENTS := OKL_FUNDING_PVT.get_chr_funded_adjs(p_contract_id);
9338    x_fnd_rec.TOTAL_REMAINING_TO_FUND := OKL_FUNDING_PVT.get_chr_canbe_funded_rem(p_contract_id);
9339    x_fnd_rec.TOTAL_SUPPLIER_RETENTION := OKL_FUNDING_PVT.get_total_retention(p_contract_id);
9340    x_fnd_rec.TOTAL_BORROWER_PAYMENTS := OKL_FUNDING_PVT.get_amount_borrowerPay(p_contract_id);
9341    x_fnd_rec.TOTAL_SUBSIDIES_FUNDED := OKL_FUNDING_PVT.get_amount_subsidy(p_contract_id);
9342    x_fnd_rec.TOTAL_MANUAL_DISBURSEMENT := OKL_FUNDING_PVT.get_amount_manu_disb(p_contract_id);
9343 
9344    --Bug# 15992711: This Variable will be used to display in UI Funding Summary section
9345    --Start Modification 120.67.12010000.8 Pratrao 4-Mar-2013
9346    x_fnd_rec.TOTAL_RELEASE_ASSET_AMOUNT := OKL_FUNDING_PVT.GET_RELEASED_ASST_AMT(p_contract_id);
9347    --Bug# 15992711 End Modification
9348 
9349 
9350     EXCEPTION
9351       WHEN OTHERS THEN
9352         x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
9353         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9354 END get_fund_summary;
9355 
9356 /*
9357 -- zrehman, 10/13/2006
9358 -- END, PROCEDURE to get Funding Summary
9359 */
9360 
9361  --Bug# 15992711 :Added function get_released_asst_amt to add
9362  --               field "Total Re-Lease Asset Amount" in funding summary screen
9363  --Start Modification 120.67.12010000.8 Pratrao 1-Mar-2013
9364 FUNCTION GET_RELEASED_ASST_AMT(
9365   p_khr_id           IN   NUMBER  -- contract hdr
9366 )
9367   RETURN NUMBER IS
9368   l_amount            NUMBER                                        := 0;
9369   l_amount_buffer     NUMBER                                        := 0;
9370   l_vendor_id         NUMBER                                        := 0;
9371 -- start: okl.h: cklee
9372   x_return_status     VARCHAR2(3)                                   := okl_api.g_ret_sts_success;
9373   l_api_version       NUMBER                                        := 1.0;
9374   x_msg_count         NUMBER;
9375   x_msg_data          VARCHAR2(4000);
9376   l_init_msg_list     VARCHAR2(10)                                  := okl_api.g_false;
9377   x_value             NUMBER;
9378 
9379   --Bug# 16567807 : snarasip - Total release asset amount should be displayed
9380   -- even if no vendor information is provided on the asset
9381   CURSOR c_rel_asset_amt (
9382     p_khr_id NUMBER
9383   )  IS
9384      SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
9385      FROM okc_k_items cim,
9386           okc_k_lines_b cle,
9387           okl_k_lines   kle_top
9388      WHERE (
9389                 cim.cle_id = cle.ID
9390             AND cim.dnz_chr_id = p_khr_id
9391             AND cim.jtot_object1_code = 'OKX_ASSET'
9392             AND cle.sts_code <> 'ABANDONED'
9393             AND kle_top.id   = cle.cle_id
9394             AND nvl(kle_top.re_lease_yn,'N')  = 'Y'
9395            );
9396    --End Bug# 16567807 : snarasip
9397 
9398 --
9399 -- if NO line and vendor_id
9400 --
9401   CURSOR c_no_line_and_vendor(
9402     p_khr_id   NUMBER
9403   ) IS
9404     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
9405       FROM okc_k_items cim,
9406            okc_k_party_roles_b cpl,
9407            okc_k_lines_b cle,
9408            --Bug# 15992711 : avsingh - New-Released asset ER
9409            okl_k_lines   kle_top
9410            --End Bug# 15992711 : New-Released asset ER
9411      WHERE (
9412                 cim.cle_id = cle.ID
9413             AND cim.dnz_chr_id = p_khr_id
9414             AND cpl.cle_id = cle.ID
9415             AND cpl.dnz_chr_id = p_khr_id
9416             AND cpl.chr_id IS NULL
9417             AND cpl.rle_code = 'OKL_VENDOR'
9418             AND EXISTS(SELECT NULL
9419                          FROM okc_line_styles_b model_lse
9420                         WHERE model_lse.ID = cle.lse_id
9421                           AND model_lse.lty_code = 'ITEM')
9422             --Bug# 13809564
9423             AND cle.sts_code <> 'ABANDONED'
9424             --Bug# 15992711 : avsingh - New-Released asset ER
9425             AND kle_top.id   = cle.cle_id
9426             AND nvl(kle_top.re_lease_yn,'N')  = 'Y'
9427             --End Bug# 15992711 : New-Released asset ER
9428            )
9429         OR     (
9430                     cim.cle_id = cle.ID
9431                 AND cim.dnz_chr_id = p_khr_id
9432                 AND cpl.cle_id = cle.ID
9433                 AND cpl.dnz_chr_id = p_khr_id
9434                 AND cpl.chr_id IS NULL
9435                 AND cpl.rle_code = 'OKL_VENDOR'
9436                 AND EXISTS(SELECT NULL
9437                              FROM okc_line_styles_b adon_lse
9438                             WHERE adon_lse.ID = cle.lse_id
9439                               AND adon_lse.lty_code = 'ADD_ITEM'
9440                               AND EXISTS(SELECT NULL
9441                                           FROM okc_k_lines_b mdl_parent
9442                                          WHERE mdl_parent.ID = cle.cle_id
9443                                          --Bug# 15992711 : avsingh - New-Released asset ER
9444                                          AND EXISTS(SELECT NULL
9445                                                     FROM okl_k_lines kle_top1
9446                                                     WHERE kle_top1.id = mdl_parent.cle_id
9447                                                     AND   nvl(kle_top1.re_lease_yn,'N') = 'Y')))
9448                                          --End Bug# 15992711 : New-Released asset ER
9449                 --Bug# 13809564
9450                 AND cle.sts_code <> 'ABANDONED'
9451                );
9452 
9453 --
9454 -- bug 5384359 -- start
9455   CURSOR downpymnt_recvr_csr(
9456     p_kle_id   NUMBER
9457   ) IS
9458     SELECT down_payment_receiver_code
9459       FROM okl_k_lines
9460      WHERE ID = p_kle_id;
9461 
9462   l_downpymnt_recvr   okl_k_lines.down_payment_receiver_code%TYPE;
9463 
9464   CURSOR c_kle_id(
9465                   p_khr_id okc_k_headers_b.id%TYPE
9466                  ) IS
9467     SELECT kle_k.id kle_id,
9468            kle_k.down_payment_receiver_code downpymnt_recvr
9469       FROM okl_k_lines kle_k,
9470            okc_k_lines_b kle
9471      where kle_k.id = kle.id
9472      and kle.dnz_chr_id = p_khr_id
9473      --Bug# 15992711 : avsingh - New-Released asset ER
9474      and nvl(kle_k.re_lease_yn,'N') = 'Y'  -- re lease flag check
9475      --End Bug# 15992711 : New-Released asset ER
9476      --Bug# 13809564
9477      AND kle.sts_code <> 'ABANDONED';
9478      -- only asset lines associated with Lease Vendor (Supplier Invoice)
9479      --and exists (
9480      --SELECT 1
9481       --FROM okc_k_party_roles_b cpl,
9482            --okc_k_lines_b cle,
9483            --okc_line_styles_b model_lse
9484      --WHERE cpl.rle_code = 'OKL_VENDOR'
9485        --AND cpl.chr_id IS NULL
9486        --and model_lse.ID = cle.lse_id
9487        --and model_lse.lty_code = 'ITEM'
9488        --and cle.cle_id = kle_k.id -- link to FREE_FORM1 (top line)
9489        --AND cpl.object1_id2 = '#'
9490        --AND cpl.cle_id = cle.ID); -- link to ITEM
9491 
9492 --end:| 08-Feb-08  cklee Fixed bug: 6783566                                        |
9493 
9494 BEGIN
9495   IF (p_khr_id IS NULL) OR (p_khr_id = okl_api.g_miss_num) THEN
9496     RETURN 0;   -- error
9497   ELSE
9498 
9499     --Bug# 16567807 : snarasip - Total release asset amount should be displayed
9500     /*OPEN c_no_line_and_vendor(p_khr_id);
9501       FETCH c_no_line_and_vendor INTO l_amount;
9502       CLOSE c_no_line_and_vendor;*/
9503 
9504       OPEN  c_rel_asset_amt(p_khr_id);
9505       FETCH c_rel_asset_amt INTO l_amount;
9506       CLOSE c_rel_asset_amt;
9507 
9508     -- Trade ins and Down payments are not relevant for released assets , hence commenting the code below
9509     --------------------------------------------------
9510     -- Contract Trade In AND Contract Capitalized Reduction -- Downpayment
9511     --------------------------------------------------
9512     /*FOR l_kle_id IN c_kle_id(p_khr_id) LOOP
9513       BEGIN
9514         l_amount_buffer := okl_seeded_functions_pvt.line_tradein(p_chr_id => p_khr_id, p_line_id => l_kle_id.kle_id);
9515 
9516         IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
9517           l_amount_buffer := 0;
9518         ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
9519           l_amount_buffer := 0;
9520         END IF;
9521       EXCEPTION
9522         WHEN OTHERS THEN
9523           l_amount_buffer := 0;
9524       END;
9525 
9526       l_amount := l_amount - l_amount_buffer;
9527 
9528       BEGIN
9529         IF ((l_kle_id.downpymnt_recvr IS NULL) OR (l_kle_id.downpymnt_recvr = 'VENDOR')) THEN
9530           l_amount_buffer := okl_seeded_functions_pvt.line_capital_reduction(p_chr_id => p_khr_id, p_line_id => l_kle_id.kle_id);
9531 
9532           IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
9533             l_amount_buffer := 0;
9534           ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
9535             l_amount_buffer := 0;
9536           END IF;
9537         ELSIF(l_kle_id.downpymnt_recvr = 'LESSOR') THEN
9538           l_amount_buffer := 0;
9539         END IF;
9540 
9541       EXCEPTION
9542         WHEN OTHERS THEN
9543           l_amount_buffer := 0;
9544       END;
9545 
9546       l_amount := l_amount - l_amount_buffer;
9547     END LOOP;
9548    */
9549       --Bug# 16567807 : snarasip - Total release asset amount should be displayed
9550   END IF;
9551 
9552   IF (l_amount IS NULL) THEN
9553     l_amount := 0;
9554   END IF;
9555 
9556   RETURN l_amount;
9557 END get_released_asst_amt;
9558 --Bug# 15992711 End Modification
9559 
9560 END OKL_FUNDING_PVT;