DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKL_FUNDING_PVT

Source


1 PACKAGE BODY OKL_FUNDING_PVT AS
2 /* $Header: OKLCFUNB.pls 120.67.12010000.2 2008/08/06 18:12:35 cklee 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  ) RETURN NUMBER
1761 IS
1762   l_row_notfound boolean := false;
1763   l_yn number := 0;
1764   l_sts_code OKC_K_HEADERS_B.sts_code%TYPE;
1765   l_deal_type OKL_K_HEADERS.deal_type%TYPE;
1766 
1767   CURSOR c_deal_type(p_contract_id  NUMBER)
1768   IS
1769   select chr.sts_code,
1770          khr.deal_type
1771   from  OKL_K_HEADERS khr,
1772         OKC_K_HEADERS_B chr
1773   where khr.id = chr.id
1774   and   khr.id = p_contract_id
1775   ;
1776 
1777 
1778   CURSOR c (p_contract_id  NUMBER)
1779   IS
1780 select 1
1781 from   okc_statuses_b ste,
1782        okc_k_headers_b chr
1783 where  ste.code = chr.sts_code
1784 and    ste.ste_code in ('ENTERED', 'ACTIVE','SIGNED')
1785 and    chr.id = p_contract_id
1786   ;
1787 
1788 BEGIN
1789 
1790 --
1791 -- assume this is a valid contract id
1792 --
1793   OPEN c_deal_type (p_contract_id);
1794   FETCH c_deal_type INTO l_sts_code,
1795                          l_deal_type;
1796   CLOSE c_deal_type;
1797 
1798   IF (l_deal_type = 'LOAN-REVOLVING') THEN
1799 
1800     IF (l_sts_code = 'BOOKED') THEN
1801       l_yn := 1;
1802     ELSE
1803       l_yn := 0;
1804     END IF;
1805 
1806   ELSE -- for any other type of contract
1807 
1808     OPEN c (p_contract_id);
1809 
1810 
1811     FETCH c INTO l_yn;
1812     l_row_notfound := c%NOTFOUND;
1813     CLOSE c;
1814 
1815     IF (l_row_notfound) THEN
1816       l_yn := 0;
1817     ELSE
1818       l_yn := 1;
1819     END IF;
1820 
1821   END IF;
1822 
1823   RETURN l_yn;
1824 
1825   EXCEPTION
1826     WHEN OTHERS THEN
1827       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1828       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
1829                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
1830                           p_token1        => 'OKL_SQLCODE',
1831                           p_token1_value  => SQLCODE,
1832                           p_token2        => 'OKL_SQLERRM',
1833                           p_token2_value  => SQLERRM);
1834       RETURN 0;
1835 END;
1836 ----------------------------------------------------------------------------
1837 -- Total contract funded adjustments
1838  FUNCTION get_chr_funded_adjs(
1839   p_contract_id                  IN NUMBER                 -- contract hdr
1840  ,p_vendor_site_id               IN NUMBER
1841  ) RETURN NUMBER
1842 IS
1843   l_amount NUMBER := 0;
1844 
1845   -- sjalasut, modified the below cursor to make khr_id referred from
1846   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
1847   -- as part of OKLR12B disbursements project
1848   CURSOR c (p_contract_id  NUMBER)
1849   IS
1850   select nvl(sum(a.amount),0)
1851   from okl_trx_ap_invoices_b a
1852       ,okl_txl_ap_inv_lns_all_b b
1853   where a.id = b.tap_id
1854   and a.funding_type_code = 'PREFUNDING'
1855   and a.trx_status_code in ('APPROVED', 'PROCESSED')
1856   and a.amount < 0 -- adjustments
1857   and b.khr_id = p_contract_id;
1858 
1859   -- sjalasut, modified the below cursor to make khr_id referred from
1860   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
1861   -- as part of OKLR12B disbursements project
1862   CURSOR c2 (p_contract_id  NUMBER, p_vendor_site_id NUMBER)
1863   IS
1864   select nvl(sum(a.amount),0)
1865   from okl_trx_ap_invoices_b a
1866       ,okl_txl_ap_inv_lns_all_b b
1867   where a.id = b.tap_id
1868   and a.funding_type_code = 'PREFUNDING'
1869   and a.trx_status_code in ('APPROVED', 'PROCESSED')
1870   and a.amount < 0 -- adjustments
1871   and b.khr_id = p_contract_id
1872   and a.ipvs_id = p_vendor_site_id;
1873 
1874 BEGIN
1875 
1876   IF (p_vendor_site_id IS NULL OR p_vendor_site_id = OKL_API.G_MISS_NUM) THEN
1877 
1878     OPEN c (p_contract_id);
1879     FETCH c INTO l_amount;
1880     CLOSE c;
1881   ELSE
1882     OPEN c2 (p_contract_id, p_vendor_site_id);
1883     FETCH c2 INTO l_amount;
1884     CLOSE c2;
1885   END IF;
1886 
1887   RETURN l_amount;
1888 
1889   EXCEPTION
1890     WHEN OTHERS THEN
1891       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1892       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
1893                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
1894                           p_token1        => 'OKL_SQLCODE',
1895                           p_token1_value  => SQLCODE,
1896                           p_token2        => 'OKL_SQLERRM',
1897                           p_token2_value  => SQLERRM);
1898       RETURN 0;
1899 END;
1900 ----------------------------------------------------------------------------
1901 
1902 -- Total contract allowable funded remaining
1903  FUNCTION get_chr_canbe_funded_rem(
1904   p_contract_id                       IN NUMBER                 -- contract hdr
1905  ) RETURN NUMBER
1906 IS
1907   l_amount NUMBER := 0;
1908   l_amount_buffer NUMBER := 0;
1909   l_amount_hasbeen_funded NUMBER := 0;
1910   l_amount_canbe_funded NUMBER := 0;
1911 
1912   l_loan_rev NUMBER;
1913   l_loan_rev_notfound boolean := false;
1914 
1915 --START:bug#4882537
1916   l_amt_hasbeen_funded_sub number;
1917 --END:bug#4882537
1918 
1919   CURSOR c_loan_revolving (p_contract_id  NUMBER)
1920   IS
1921   select 1 from OKL_K_HEADERS khr
1922   where khr.id = p_contract_id
1923   and khr.deal_type = 'LOAN-REVOLVING';
1924 
1925   BEGIN
1926 
1927     OPEN c_loan_revolving(p_contract_id);
1928     FETCH c_loan_revolving INTO l_loan_rev;
1929     l_loan_rev_notfound := c_loan_revolving%NOTFOUND;
1930     CLOSE c_loan_revolving;
1931 
1932 
1933     -- is not loan revolving contract
1934     IF (l_loan_rev_notfound) THEN
1935 
1936       l_amount_hasbeen_funded := get_total_funded(p_contract_id);
1937       l_amount_canbe_funded := get_chr_canbe_funded(p_contract_id);
1938       l_amount := l_amount_canbe_funded - l_amount_hasbeen_funded;
1939 --START:bug#4882537
1940 -- Subsidy is a negative amount. So we just add back to the remaining balance
1941 -- of the contract for the funding
1942       l_amt_hasbeen_funded_sub := get_amount_subsidy(p_contract_id);
1943       l_amount := l_amount_canbe_funded - l_amount_hasbeen_funded + l_amt_hasbeen_funded_sub;
1944 --      l_amount := l_amount_canbe_funded - l_amount_hasbeen_funded;
1945 --END:bug#4882537
1946 
1947     ELSE
1948       -- get amount for the remaining of the attach credit line
1949       l_amount := get_creditRem_by_chrid(p_contract_id);
1950 
1951     END IF;
1952 
1953   RETURN l_amount;
1954   EXCEPTION
1955 
1956 
1957     WHEN OTHERS THEN
1958       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
1959       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
1960                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
1961                           p_token1        => 'OKL_SQLCODE',
1962                           p_token1_value  => SQLCODE,
1963                           p_token2        => 'OKL_SQLERRM',
1964                           p_token2_value  => SQLERRM);
1965       RETURN 0;
1966 
1967 END;
1968 
1969 ----------------------------------------------------------------------------
1970 
1971 -- Total contract allowable funded
1972  FUNCTION get_chr_canbe_funded(
1973   p_contract_id                       IN NUMBER                 -- contract hdr
1974  ) RETURN NUMBER
1975 IS
1976   l_amount NUMBER := 0;
1977   l_amount_oec NUMBER := 0;
1978   l_amount_expense NUMBER := 0;
1979   l_loan_rev NUMBER := 0;
1980   l_loan_rev_notfound boolean := false;
1981   l_credit_id NUMBER;
1982 
1983   CURSOR c_loan_revolving (p_contract_id  NUMBER)
1984   IS
1985   select 1 from OKL_K_HEADERS khr
1986   where khr.id = p_contract_id
1987   and khr.deal_type = 'LOAN-REVOLVING';
1988 
1989   BEGIN
1990 
1991     OPEN c_loan_revolving(p_contract_id);
1992 
1993     FETCH c_loan_revolving INTO l_loan_rev;
1994     l_loan_rev_notfound := c_loan_revolving%NOTFOUND;
1995     CLOSE c_loan_revolving;
1996 
1997     -- is not loan revolving contract
1998     IF (l_loan_rev_notfound) THEN
1999       l_amount_oec := get_chr_oec_canbe_funded(p_contract_id);
2000       l_amount_expense := get_chr_exp_canbe_funded_amt(p_contract_id);
2001       l_amount := l_amount_oec + l_amount_expense;
2002     ELSE
2003       -- get amount for the remaining of the attach credit line
2004       l_amount := get_creditRem_by_chrid(p_contract_id);
2005 
2006     END IF;
2007 
2008   RETURN l_amount;
2009   EXCEPTION
2010     WHEN OTHERS THEN
2011       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2012       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2013                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2014                           p_token1        => 'OKL_SQLCODE',
2015                           p_token1_value  => SQLCODE,
2016                           p_token2        => 'OKL_SQLERRM',
2017                           p_token2_value  => SQLERRM);
2018       RETURN 0;
2019 
2020 END;
2021 
2022 ----------------------------------------------------------------------------
2023 
2024 -- Total contract allowable oec funded remaining
2025  FUNCTION get_chr_oec_canbe_funded_rem(
2026   p_contract_id                      IN NUMBER                 -- contract hdr
2027  ) RETURN NUMBER
2028 IS
2029   l_amount NUMBER := 0;
2030   l_total_canbe_OEC_amount NUMBER := 0;
2031   l_oec_hasbeen_funded_amount NUMBER := 0;
2032 
2033   BEGIN
2034 
2035     l_total_canbe_OEC_amount:= get_chr_oec_canbe_funded(p_contract_id);
2036     l_oec_hasbeen_funded_amount := get_chr_oec_hasbeen_funded_amt(p_contract_id);
2037     l_amount := l_total_canbe_OEC_amount - l_oec_hasbeen_funded_amount;
2038 
2039   RETURN l_amount;
2040   EXCEPTION
2041     WHEN OTHERS THEN
2042       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2043       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2044                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2045                           p_token1        => 'OKL_SQLCODE',
2046                           p_token1_value  => SQLCODE,
2047                           p_token2        => 'OKL_SQLERRM',
2048                           p_token2_value  => SQLERRM);
2049       RETURN 0;
2050 
2051 END;
2052 
2053 ----------------------------------------------------------------------------
2054 
2055 -- Total contract allowable oec funded
2056  FUNCTION get_chr_oec_canbe_funded(
2057   p_contract_id                      IN NUMBER                 -- contract hdr
2058  ) RETURN NUMBER
2059 IS
2060   l_amount NUMBER := 0;
2061 
2062 
2063   BEGIN
2064 
2065     IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 1) THEN
2066 
2067       l_amount := OKL_FUNDING_PVT.get_contract_line_amt(p_contract_id);
2068     END IF;
2069 
2070   RETURN l_amount;
2071   EXCEPTION
2072     WHEN OTHERS THEN
2073       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2074       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2075                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2076                           p_token1        => 'OKL_SQLCODE',
2077                           p_token1_value  => SQLCODE,
2078                           p_token2        => 'OKL_SQLERRM',
2079                           p_token2_value  => SQLERRM);
2080       RETURN 0;
2081 
2082 END;
2083 
2084 ----------------------------------------------------------------------------
2085 
2086 -- Total contract has been funded oec amount
2087  FUNCTION get_chr_oec_hasbeen_funded_amt(
2088   p_contract_id                       IN NUMBER                 -- contract hdr
2089  ,p_vendor_site_id               IN NUMBER
2090 
2091  ) RETURN NUMBER
2092 IS
2093   l_amount NUMBER := 0;
2094 
2095   -- get approved amount for Asset
2096   -- sjalasut, made changes to the below cursor to have khr_id be referred
2097   -- from okl_txl_inv_lns_all_b instead of okl_trx_ap_invoices_b.
2098   -- also changed the from clause to okl_txl_ap_inv_lns_all_b
2099   CURSOR c_tot_asset_fund (p_contract_id  NUMBER)
2100   IS
2101   select nvl(sum(b.amount),0)
2102   from okl_trx_ap_invoices_b a,
2103        okl_txl_ap_inv_lns_all_b b
2104   where a.id = b.tap_id
2105   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2106   and a.funding_type_code ='ASSET'
2107   and b.amount > 0 --?
2108   and b.khr_id = p_contract_id
2109   ;
2110 
2111   -- sjalasut, made changes to the below cursor to have khr_id be referred
2112   -- from okl_txl_inv_lns_all_b instead of okl_trx_ap_invoices_b.
2113   -- also changed the from clause to okl_txl_ap_inv_lns_all_b
2114   CURSOR c_tot_asset_fund_ven (p_contract_id  NUMBER, p_vendor_site_id NUMBER)
2115   IS
2116   select nvl(sum(b.amount),0)
2117   from okl_trx_ap_invoices_b a,
2118        okl_txl_ap_inv_lns_all_b b
2119   where a.id = b.tap_id
2120   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2121   and a.funding_type_code ='ASSET'
2122   and b.amount > 0 --?
2123   and b.khr_id = p_contract_id
2124   and a.ipvs_id = p_vendor_site_id
2125   ;
2126 
2127   BEGIN
2128     IF (p_vendor_site_id IS NULL OR p_vendor_site_id = OKL_API.G_MISS_NUM)
2129     THEN
2130 
2131       OPEN c_tot_asset_fund(p_contract_id);
2132       FETCH c_tot_asset_fund INTO l_amount;
2133       CLOSE c_tot_asset_fund;
2134     ELSE
2135       OPEN c_tot_asset_fund_ven(p_contract_id,p_vendor_site_id);
2136       FETCH c_tot_asset_fund_ven INTO l_amount;
2137       CLOSE c_tot_asset_fund_ven;
2138 
2139     END IF;
2140 
2141   RETURN l_amount;
2142   EXCEPTION
2143     WHEN OTHERS THEN
2144       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2145       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2146                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2147                           p_token1        => 'OKL_SQLCODE',
2148                           p_token1_value  => SQLCODE,
2149                           p_token2        => 'OKL_SQLERRM',
2150                           p_token2_value  => SQLERRM);
2151       RETURN 0;
2152 
2153 END;
2154 
2155 ----------------------------------------------------------------------------
2156 
2157 
2158 -- Total contract allowable expnese funded remaining
2159  FUNCTION get_chr_exp_canbe_funded_rem(
2160   p_contract_id                       IN NUMBER                 -- contract hdr
2161   ,p_vendor_site_id               IN NUMBER                 -- vendor_site_id
2162  ) RETURN NUMBER
2163 IS
2164   l_amount NUMBER := 0;
2165   l_total_canbe_expense_amount NUMBER := 0;
2166   l_expense_hasbeen_funded_amt NUMBER := 0;
2167 
2168   BEGIN
2169     l_expense_hasbeen_funded_amt:= get_chr_exp_hasbeen_funded_amt(p_contract_id,p_vendor_site_id);
2170 
2171     l_total_canbe_expense_amount := get_chr_exp_canbe_funded_amt(p_contract_id,p_vendor_site_id);
2172     l_amount := l_total_canbe_expense_amount - l_expense_hasbeen_funded_amt;
2173 
2174   RETURN l_amount;
2175   EXCEPTION
2176     WHEN OTHERS THEN
2177       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2178       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2179                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2180                           p_token1        => 'OKL_SQLCODE',
2181                           p_token1_value  => SQLCODE,
2182                           p_token2        => 'OKL_SQLERRM',
2183                           p_token2_value  => SQLERRM);
2184       RETURN 0;
2185 END;
2186 ----------------------------------------------------------------------------
2187 
2188 -- Total contract has been funded expense
2189  FUNCTION get_chr_exp_hasbeen_funded_amt(
2190   p_contract_id                       IN NUMBER                 -- contract hdr
2191   ,p_vendor_site_id               IN NUMBER                 -- vendor_site_id
2192  ) RETURN NUMBER
2193 
2194 IS
2195   l_amount NUMBER := 0;
2196 
2197   -- get approved amount for Expense by specific vendor
2198   -- sjalasut, modified the below cursor to have khr_id be referred from
2199   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices. changes made
2200   -- as part of OKLR12B disbursements project.
2201   CURSOR c_tot_expense_fund (p_contract_id  NUMBER, p_vendor_site_id  NUMBER)
2202   IS
2203   select nvl(sum(b.amount),0)
2204   from okl_trx_ap_invoices_b a,
2205        okl_txl_ap_inv_lns_b b
2206   where a.id = b.tap_id
2207   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2208   and a.funding_type_code ='EXPENSE'
2209   and b.amount > 0 --?
2210   and b.khr_id = p_contract_id
2211   and exists (select null
2212               from   okx_vendor_sites_v vs
2213               where  vs.id1 = a.ipvs_id
2214               and    vs.id1 = p_vendor_site_id)
2215   ;
2216 
2217   BEGIN
2218     OPEN c_tot_expense_fund(p_contract_id, p_vendor_site_id);
2219     FETCH c_tot_expense_fund INTO l_amount;
2220     CLOSE c_tot_expense_fund;
2221 
2222   RETURN l_amount;
2223   EXCEPTION
2224     WHEN OTHERS THEN
2225       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2226       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2227                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2228                           p_token1        => 'OKL_SQLCODE',
2229                           p_token1_value  => SQLCODE,
2230                           p_token2        => 'OKL_SQLERRM',
2231                           p_token2_value  => SQLERRM);
2232       RETURN 0;
2233 END;
2234 ----------------------------------------------------------------------------
2235 
2236 -- Total contract has been funded expense
2237  FUNCTION get_chr_exp_hasbeen_funded_amt(
2238   p_contract_id                       IN NUMBER                 -- contract hdr
2239  ) RETURN NUMBER
2240 IS
2241   l_amount NUMBER := 0;
2242 
2243 -- get approved amount for Expense
2244   -- sjalasut, made changes to the below cursor to have khr_id referred to
2245   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. change made
2246   -- as part of OKLR12B disbursements project.
2247   CURSOR c_tot_expense_fund (p_contract_id  NUMBER)
2248   IS
2249   select nvl(sum(b.amount),0)
2250   from okl_trx_ap_invoices_b a,
2251        okl_txl_ap_inv_lns_b b
2252   where a.id = b.tap_id
2253   and a.trx_status_code in ('APPROVED', 'PROCESSED')
2254   and a.funding_type_code ='EXPENSE'
2255   and b.amount > 0 --?
2256   and b.khr_id = p_contract_id;
2257 
2258   BEGIN
2259 
2260     OPEN c_tot_expense_fund(p_contract_id);
2261     FETCH c_tot_expense_fund INTO l_amount;
2262     CLOSE c_tot_expense_fund;
2263 
2264   RETURN l_amount;
2265   EXCEPTION
2266     WHEN OTHERS THEN
2267       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2268 
2269       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2270                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2271                           p_token1        => 'OKL_SQLCODE',
2272                           p_token1_value  => SQLCODE,
2273                           p_token2        => 'OKL_SQLERRM',
2274                           p_token2_value  => SQLERRM);
2275 
2276       RETURN 0;
2277 END;
2278 ----------------------------------------------------------------------------
2279  FUNCTION get_chr_exp_canbe_funded_amt(
2280   p_contract_id                       IN NUMBER                 -- contract hdr
2281   ,p_vendor_site_id               IN NUMBER                 -- vendor_site_id
2282  ,p_due_date                     IN date  default sysdate   --cklee added) RETURN NUMBER
2283 ) RETURN NUMBER IS
2284   l_amount NUMBER := 0;
2285   l_cle_amount NUMBER := 0;
2286   l_amount_per NUMBER := 0;
2287 
2288   l_vendor_id NUMBER := 0;
2289   l_cle_id NUMBER := 0;
2290 
2291   l_cle_start_date DATE;
2292   l_period NUMBER := 0;
2293   l_period_org NUMBER := 0;
2294 
2295   l_row_notfound   BOOLEAN;
2296 
2297 
2298   CURSOR cv1 (p_vendor_site_id NUMBER)
2299   IS
2300     select vendor_id from okx_vendor_sites_v
2301     where id1 = to_char(p_vendor_site_id)
2302   ;
2303 
2304   CURSOR c (p_contract_id  NUMBER, p_vendor_id NUMBER, p_rle_code VARCHAR2)
2305   IS
2306 select nvl(cle.AMOUNT,0),
2307        cle.id,
2308        nvl(cle.start_date,k.start_date)
2309 from   OKL_K_LINES_FULL_V cle,
2310        okc_k_party_roles_b cpl,
2311        okc_line_styles_b ls,
2312        okc_k_headers_b k
2313 where  k.id = cle.dnz_chr_id
2314 and    cle.dnz_chr_id = p_contract_id
2315 and    cle.lse_id = ls.id
2316 and    ls.lty_code = p_rle_code
2317 and    cle.id = cpl.cle_id
2318 and    cpl.dnz_chr_id  = p_contract_id
2319 and    cpl.chr_id     is null
2320 and    cpl.rle_code = 'OKL_VENDOR'
2321 and    cpl.object1_id1 = to_char(p_vendor_id)
2322 and    cpl.object1_id2 = '#'
2323 -- Pass through check
2324 and not exists (select null
2325                 from   okc_rule_groups_v crg,
2326                        okc_rules_v cr
2327                 where  crg.dnz_chr_id = p_contract_id
2328                 and    crg.cle_id = cle.id -- line id for rle_code
2329                 and    crg.id = cr.rgp_id
2330                 and    crg.rgd_code = 'LAPSTH')
2331 ;
2332 
2333 
2334 --
2335 -- get Number of Period
2336 --
2337 -- 1) take contract start date if line start date is null
2338 -- 2) truncate pay period if less than 0
2339 --
2340   CURSOR c_period (p_contract_id  NUMBER, p_cle_id NUMBER)
2341   IS
2342    --cklee start 10/3/2007 bug: 6128765
2343 /*select ceil(decode(cr.object1_id1, 'A', months_between(sysdate, nvl(cle.start_date, k.start_date))/12
2344                             , 'M', months_between(sysdate, nvl(cle.start_date, k.start_date))
2345                             , 'Q', months_between(sysdate, nvl(cle.start_date, k.start_date))/3
2346                             , 'S', months_between(sysdate, nvl(cle.start_date, k.start_date))/6
2347                             , months_between(sysdate, nvl(cle.start_date, k.start_date))))*/
2348  select ceil(decode(cr.object1_id1, 'A', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/12
2349                               , 'M', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))
2350                                , 'Q', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/3
2351                                , 'S', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/6
2352                                , months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))))
2353    --cklee end 10/3/2007 bug: 6128765
2354 
2355 from   okc_rule_groups_v crg,
2356        okc_rules_v cr,
2357        OKL_K_LINES_FULL_V cle,
2358        okc_k_headers_b k
2359 where  crg.dnz_chr_id = p_contract_id
2360 and    cle.dnz_chr_id = k.id
2361 and    crg.id = cr.rgp_id
2362 and    crg.rgd_code = 'LAFEXP'
2363 
2364 and    crg.cle_id = cle.id
2365 and    cr.RULE_INFORMATION_CATEGORY = 'LAFREQ'
2366 and    cle.id = p_cle_id
2367 ;
2368 
2369 --
2370 -- get amount per period
2371 --
2372 --
2373   CURSOR c_amount_per (p_contract_id  NUMBER, p_cle_id NUMBER)
2374   IS
2375 select to_number(nvl(cr.RULE_INFORMATION1,'0'))
2376        ,to_number(nvl(cr.RULE_INFORMATION2,'0'))
2377 from   okc_rule_groups_v crg,
2378        okc_rules_v cr
2379 where  crg.dnz_chr_id = p_contract_id
2380 and    crg.id = cr.rgp_id
2381 
2382 and    crg.rgd_code = 'LAFEXP'
2383 and    cr.RULE_INFORMATION_CATEGORY = 'LAFEXP'
2384 and    crg.cle_id = p_cle_id
2385 
2386 ;
2387 
2388 
2389 BEGIN
2390   IF (p_contract_id IS NULL OR p_contract_id = OKL_API.G_MISS_NUM)
2391   THEN
2392     RETURN 0;  -- error
2393   ELSE
2394     OPEN cv1 (p_vendor_site_id);
2395     FETCH cv1 INTO l_vendor_id;
2396     CLOSE cv1;
2397 
2398     ----------------------------------------------------
2399     -- FEE line
2400     ----------------------------------------------------
2401     OPEN c (p_contract_id, l_vendor_id, 'FEE');
2402     LOOP
2403 
2404 
2405       FETCH c into l_cle_amount,
2406                    l_cle_id,
2407                    l_cle_start_date;
2408 
2409       EXIT WHEN c%NOTFOUND;
2410 
2411       OPEN c_amount_per (p_contract_id, l_cle_id);
2412       FETCH c_amount_per INTO l_period_org,
2413                               l_amount_per;
2414 
2415       l_row_notfound := c_amount_per%NOTFOUND;
2416       CLOSE c_amount_per;
2417 
2418       -- if recurring records doesn't exists
2419       IF (l_row_notfound) THEN
2420 
2421         -- either fee effective date or contract effective date <= current date
2422         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN -- cklee start 10/3/2007 bug: 6128765
2423           l_amount := l_amount + l_cle_amount;
2424         END IF;
2425 
2426       ELSE
2427 
2428         OPEN c_period (p_contract_id, l_cle_id);
2429         FETCH c_period INTO l_period;
2430         CLOSE c_period;
2431 
2432         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN -- cklee start 10/3/2007 bug: 6128765
2433           l_period := 1;
2434         END IF;
2435 
2436         -- calculate only if period is positive
2437         IF (l_period > 0) THEN
2438 
2439           IF (l_period > l_period_org) THEN
2440             l_period := l_period_org;
2441           END IF;
2442           l_amount := l_amount + (l_amount_per * l_period);
2443         END IF;
2444 
2445       END IF;
2446 
2447     END LOOP;
2448     CLOSE c;
2449 
2450     -- SOLD_SERVICE line
2451     OPEN c (p_contract_id, l_vendor_id, 'SOLD_SERVICE');
2452     LOOP
2453 
2454 
2455       FETCH c into l_cle_amount,
2456                    l_cle_id,
2457                    l_cle_start_date;
2458 
2459       EXIT WHEN c%NOTFOUND;
2460 
2461       OPEN c_amount_per (p_contract_id, l_cle_id);
2462 
2463       FETCH c_amount_per INTO l_period_org,
2464                               l_amount_per;
2465       l_row_notfound := c_amount_per%NOTFOUND;
2466       CLOSE c_amount_per;
2467 
2468 
2469       -- if recurring records doesn't exists
2470       IF (l_row_notfound) THEN
2471 
2472         -- either fee effective date or contract effective date <= current date
2473         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN -- cklee start 10/3/2007 bug: 6128765
2474           l_amount := l_amount + l_cle_amount;
2475         END IF;
2476 
2477       ELSE
2478 
2479         OPEN c_period (p_contract_id, l_cle_id);
2480         FETCH c_period INTO l_period;
2481         CLOSE c_period;
2482 
2483         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN -- cklee start 10/3/2007 bug: 6128765
2484           l_period := 1;
2485         END IF;
2486 
2487         -- calculate only if period is positive
2488         IF (l_period > 0) THEN
2489 
2490           IF (l_period > l_period_org) THEN
2491             l_period := l_period_org;
2492           END IF;
2493           l_amount := l_amount + (l_amount_per * l_period);
2494         END IF;
2495 
2496       END IF;
2497 
2498     END LOOP;
2499     CLOSE c;
2500 
2501   END IF;
2502 --
2503   IF (l_amount IS NULL) THEN
2504     l_amount := 0;
2505   END IF;
2506 
2507   IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 0) THEN
2508     l_amount := 0;
2509   END IF;
2510 
2511   RETURN l_amount;
2512 
2513   EXCEPTION
2514     WHEN OTHERS THEN
2515       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2516       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2517                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2518                           p_token1        => 'OKL_SQLCODE',
2519                           p_token1_value  => SQLCODE,
2520                           p_token2        => 'OKL_SQLERRM',
2521                           p_token2_value  => SQLERRM);
2522       RETURN 0;
2523 
2524 END;
2525 ----------------------------------------------------------------------------
2526 -- used for pre-funding only
2527  FUNCTION get_chr_exp_canbe_funded_amt(
2528   p_contract_id                       IN NUMBER                 -- contract hdr
2529   ,p_due_date                         IN date  default sysdate  --shagarg added
2530   ) RETURN NUMBER
2531 IS
2532   l_amount NUMBER := 0;
2533   l_cle_amount NUMBER := 0;
2534   l_amount_per NUMBER := 0;
2535 
2536   l_cle_id NUMBER := 0;
2537   l_cle_start_date DATE;
2538   l_period NUMBER := 0;
2539   l_period_org NUMBER := 0;
2540   l_row_notfound   BOOLEAN;
2541 
2542   CURSOR c (p_contract_id  NUMBER, p_rle_code VARCHAR2)
2543   IS
2544 select nvl(cle.AMOUNT,0),
2545        cle.id,
2546        nvl(cle.start_date,k.start_date)
2547 from   OKL_K_LINES_FULL_V cle,
2548        okc_k_party_roles_b cpl,
2549        okc_line_styles_b ls,
2550        okc_k_headers_b k
2551 where  k.id = cle.dnz_chr_id
2552 and    cle.dnz_chr_id = p_contract_id
2553 and    cle.lse_id = ls.id
2554 and    ls.lty_code = p_rle_code
2555 and    cle.id = cpl.cle_id
2556 and    cpl.dnz_chr_id  = p_contract_id
2557 and    cpl.chr_id     is null
2558 and    cpl.rle_code = 'OKL_VENDOR'
2559 --and    cpl.object1_id1 = to_char(p_vendor_id)
2560 --and    cpl.object1_id2 = '#'
2561 -- Pass through check
2562 and not exists (select null
2563                 from   okc_rule_groups_v crg,
2564                        okc_rules_v cr
2565                 where  crg.dnz_chr_id = p_contract_id
2566                 and    crg.cle_id = cle.id -- line id for rle_code
2567                 and    crg.id = cr.rgp_id
2568                 and    crg.rgd_code = 'LAPSTH')
2569 ;
2570 
2571 
2572 --
2573 -- get Number of Period
2574 --
2575 -- 1) take contract start date if cle start date is null
2576 -- 2) truncate pay period if less than 0
2577 --
2578   CURSOR c_period (p_contract_id  NUMBER, p_cle_id NUMBER)
2579   IS
2580    --cklee start 10/3/2007 bug: 6128765
2581 /*select ceil(decode(cr.object1_id1, 'A', months_between(sysdate, nvl(cle.start_date, k.start_date))/12
2582                             , 'M', months_between(sysdate, nvl(cle.start_date, k.start_date))
2583                             , 'Q', months_between(sysdate, nvl(cle.start_date, k.start_date))/3
2584                             , 'S', months_between(sysdate, nvl(cle.start_date, k.start_date))/6
2585                             , months_between(sysdate, nvl(cle.start_date, k.start_date))))*/
2586  select ceil(decode(cr.object1_id1, 'A', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/12
2587                               , 'M', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))
2588                                , 'Q', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/3
2589                                , 'S', months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))/6
2590                                , months_between(trunc(p_due_date), nvl(cle.start_date, k.start_date))))
2591    --cklee end 10/3/2007 bug: 6128765
2592 from   okc_rule_groups_v crg,
2593        okc_rules_v cr,
2594        OKL_K_LINES_FULL_V cle,
2595        okc_k_headers_b k
2596 where  crg.dnz_chr_id = p_contract_id
2597 and    cle.dnz_chr_id = k.id
2598 and    crg.id = cr.rgp_id
2599 and    crg.rgd_code = 'LAFEXP'
2600 
2601 and    crg.cle_id = cle.id
2602 and    cr.RULE_INFORMATION_CATEGORY = 'LAFREQ'
2603 and    cle.id = p_cle_id
2604 ;
2605 
2606 --
2607 -- get amount per period
2608 --
2609 --
2610   CURSOR c_amount_per (p_contract_id  NUMBER, p_cle_id NUMBER)
2611   IS
2612 select to_number(nvl(cr.RULE_INFORMATION1,'0'))
2613        ,to_number(nvl(cr.RULE_INFORMATION2,'0'))
2614 from   okc_rule_groups_v crg,
2615        okc_rules_v cr
2616 where  crg.dnz_chr_id = p_contract_id
2617 and    crg.id = cr.rgp_id
2618 and    crg.rgd_code = 'LAFEXP'
2619 and    cr.RULE_INFORMATION_CATEGORY = 'LAFEXP'
2620 and    crg.cle_id = p_cle_id
2621 ;
2622 
2623 
2624 BEGIN
2625   IF (p_contract_id IS NULL OR p_contract_id = OKL_API.G_MISS_NUM)
2626   THEN
2627     RETURN 0;  -- error
2628   ELSE
2629 
2630     ----------------------------------------------------
2631     -- FEE line
2632 
2633     ----------------------------------------------------
2634     OPEN c (p_contract_id, 'FEE');
2635     LOOP
2636 
2637       FETCH c into l_cle_amount,
2638                    l_cle_id,
2639                    l_cle_start_date;
2640 
2641       EXIT WHEN c%NOTFOUND;
2642 
2643 
2644       OPEN c_amount_per (p_contract_id, l_cle_id);
2645       FETCH c_amount_per INTO l_period_org,
2646                               l_amount_per;
2647 
2648       l_row_notfound := c_amount_per%NOTFOUND;
2649       CLOSE c_amount_per;
2650 
2651       -- if recurring records doesn't exists
2652       IF (l_row_notfound) THEN
2653 
2654         -- either fee effective date or contract effective date <= current date
2655         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN   --cklee end 10/3/2007 bug: 6128765
2656           l_amount := l_amount + l_cle_amount;
2657 
2658         END IF;
2659 
2660       ELSE
2661 
2662         OPEN c_period (p_contract_id, l_cle_id);
2663         FETCH c_period INTO l_period;
2664         CLOSE c_period;
2665 
2666         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN   --cklee end 10/3/2007 bug: 6128765
2667           l_period := 1;
2668         END IF;
2669 
2670         -- calculate only if period is positive
2671         IF (l_period > 0) THEN
2672 
2673           IF (l_period > l_period_org) THEN
2674             l_period := l_period_org;
2675           END IF;
2676           l_amount := l_amount + (l_amount_per * l_period);
2677         END IF;
2678 
2679 
2680       END IF;
2681 
2682     END LOOP;
2683 
2684     CLOSE c;
2685 
2686     -- SOLD_SERVICE line
2687 
2688     OPEN c (p_contract_id, 'SOLD_SERVICE');
2689     LOOP
2690 
2691       FETCH c into l_cle_amount,
2692                    l_cle_id,
2693                    l_cle_start_date;
2694 
2695       EXIT WHEN c%NOTFOUND;
2696 
2697       OPEN c_amount_per (p_contract_id, l_cle_id);
2698 
2699       FETCH c_amount_per INTO l_period_org,
2700                               l_amount_per;
2701 
2702       l_row_notfound := c_amount_per%NOTFOUND;
2703       CLOSE c_amount_per;
2704 
2705       -- if recurring records doesn't exists
2706       IF (l_row_notfound) THEN
2707 
2708 
2709         -- either fee effective date or contract effective date <= current date
2710         IF ( l_cle_start_date <= trunc(p_due_date) ) THEN   --cklee end 10/3/2007 bug: 6128765
2711           l_amount := l_amount + l_cle_amount;
2712 
2713         END IF;
2714 
2715       ELSE
2716 
2717         OPEN c_period (p_contract_id, l_cle_id);
2718         FETCH c_period INTO l_period;
2719         CLOSE c_period;
2720 
2721         IF l_period = 0 AND trunc(p_due_date) = TRUNC(l_cle_start_date) THEN   --cklee end 10/3/2007 bug: 6128765
2722           l_period := 1;
2723         END IF;
2724 
2725         -- calculate only if period is positive
2726         IF (l_period > 0) THEN
2727 
2728           IF (l_period > l_period_org) THEN
2729             l_period := l_period_org;
2730 
2731           END IF;
2732           l_amount := l_amount + (l_amount_per * l_period);
2733         END IF;
2734       END IF;
2735 
2736     END LOOP;
2737     CLOSE c;
2738 
2739   END IF;
2740 
2741   IF (l_amount IS NULL) THEN
2742     l_amount := 0;
2743   END IF;
2744 
2745 
2746   IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 0) THEN
2747     l_amount := 0;
2748   END IF;
2749 
2750   RETURN l_amount;
2751 
2752   EXCEPTION
2753     WHEN OTHERS THEN
2754       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2755       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
2756                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
2757                           p_token1        => 'OKL_SQLCODE',
2758                           p_token1_value  => SQLCODE,
2759                           p_token2        => 'OKL_SQLERRM',
2760                           p_token2_value  => SQLERRM);
2761       RETURN 0;
2762 
2763 
2764 END;
2765 
2766   --------------------------------------------------------------------------
2767   --------------------------------------------------------------------------
2768      ----- Validate Re-lease contract for Manual Disbursement
2769      --------------------------------------------------------------------------
2770      FUNCTION validate_release_contract(
2771        p_tapv_rec                  IN tapv_rec_type
2772      ) RETURN VARCHAR2
2773      IS
2774 
2775 
2776      CURSOR c_release_k_flag(p_contract_id  NUMBER)
2777      IS
2778       SELECT nvl(rul.rule_information1, 'N') FROM
2779        okc_rules_b rul, okc_rule_groups_b rgp
2780        WHERE rul.rule_information_category='LARLES'
2781        AND rgp.id = rul.rgp_id
2782        AND rgp.rgd_code = 'LARLES'
2783        AND rgp.dnz_chr_id= p_contract_id;
2784 
2785        l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2786        l_release_flag   okc_rules_b.rule_information1%type;
2787 
2788 
2789      BEGIN
2790 
2791        OPEN c_release_k_flag(p_tapv_rec.khr_id);
2792        FETCH c_release_k_flag INTO l_release_flag;
2793        CLOSE c_release_k_flag;
2794 
2795        IF (l_release_flag = 'Y') THEN
2796          -- re-leased contract
2797             IF (p_tapv_rec.funding_type_code <> 'MANUAL_DISB' ) THEN
2798 
2799                OKL_API.Set_Message(p_app_name      => G_APP_NAME,
2800                              p_msg_name      => 'OKL_LLA_RELK_FUNDTYPE_CHK',
2801                              p_token1       => 'COL_NAME',
2802                              p_token1_value => p_tapv_rec.funding_type_code);
2803 
2804                RAISE G_EXCEPTION_HALT_VALIDATION;
2805            END IF;
2806        END IF;
2807        RETURN l_return_status;
2808      EXCEPTION
2809        WHEN G_EXCEPTION_HALT_VALIDATION THEN
2810          l_return_status := OKL_API.G_RET_STS_ERROR;
2811          RETURN l_return_status;
2812        WHEN OTHERS THEN
2813          l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
2814          OKL_API.Set_Message(p_app_name      => G_APP_NAME,
2815                              p_msg_name      => G_UNEXPECTED_ERROR,
2816                              p_token1        => G_SQLCODE_TOKEN,
2817                              p_token1_value  => SQLCODE,
2818                              p_token2        => G_SQLERRM_TOKEN,
2819                              p_token2_value  => SQLERRM);
2820          RETURN l_return_status;
2821      END;
2822    --------------------------------------------------------------
2823 
2824   -- Validate Funding request Checklist
2825   --------------------------------------------------------------------------
2826   FUNCTION validate_funding_checklist(
2827     p_tapv_rec                  IN tapv_rec_type
2828   ) RETURN VARCHAR2
2829   IS
2830     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
2831     l_req_row_found       boolean;
2832     l_list_row_not_found   boolean;
2833     l_template_row_not_found   boolean;
2834 
2835     l_dummy           number;
2836     l_chklist_sts_row_found   boolean;
2837     l_status okl_crd_fund_checklists_tpl_uv.STATUS%TYPE;
2838     l_fund_cls_tpl_exp_found boolean := false;
2839 
2840     l_credit_id okc_k_headers_b.id%TYPE;
2841 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2842     l_lease_app_found boolean := false;
2843 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2844 ---------------------------------------------------------------------------------------------------------
2845 -- 1. check credit line funding checklist template, used for existing requests w credit line that doesn't
2846 --    have checklist template setup
2847 ---------------------------------------------------------------------------------------------------------
2848 cursor c_chk_tpl (p_credit_id number) is
2849   select tpl.status
2850 from okl_crd_fund_checklists_tpl_uv tpl
2851 where tpl.khr_id = p_credit_id
2852   ;
2853 
2854 ---------------------------------------------------------------------------------------------------
2855 -- 2.
2856 -- validate if credit line contract's funding checklist template expired
2857 
2858 -- Note: assumption
2859 
2860 -- 1. Credit line exists : valiadte_creditline()
2861 -- 2. pass # 1 cursor check
2862 ---------------------------------------------------------------------------------------------------
2863 CURSOR c_fund_chklst_tpl (p_credit_id number)
2864 IS
2865   select 1
2866 from  okl_crd_fund_chklst_tpl_hdr_uv chk
2867 where TRUNC(chk.effective_to) < TRUNC(sysdate)
2868 and   chk.khr_id = p_credit_id
2869 ;
2870 
2871 
2872 ---------------------------------------------------------------------------------------------------------
2873 -- 3. check funding checklist if funding checklist has not been setup
2874 ---------------------------------------------------------------------------------------------------------
2875 CURSOR c_chklst_chk(p_req_id okl_trx_ap_invoices_b.id%type)
2876 IS
2877   select 1
2878 from okl_funding_checklists_uv chk
2879 where fund_req_id = TO_CHAR(p_req_id) -- cklee: 11/04/2004
2880 ;
2881 
2882 ---------------------------------------------------------------------------------------------------------
2883 -- 4. check checklist required items
2884 ---------------------------------------------------------------------------------------------------------
2885 
2886 CURSOR c_chklst (p_chr_id okc_k_headers_b.id%type, p_fund_req_id okl_trx_ap_invoices_b.id%type)
2887 IS
2888   select 1
2889 from  okc_rules_b rult
2890 where rult.rule_information_category = G_FUNDING_CHKLST_TPL_RULE1--'LAFCLD'
2891 and   rult.dnz_chr_id = p_chr_id
2892 and   rult.object1_id1 = p_fund_req_id
2893 and   rult.object1_id2 = '#'
2894 and   rult.RULE_INFORMATION2 = 'Y'
2895 and   (rult.RULE_INFORMATION3 <> 'Y' or rult.RULE_INFORMATION3 is null)
2896 ;
2897 
2898 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2899 ---------------------------------------------------------------------------------------------------------
2900 -- 5. check if the contract was created from a lease application
2901 ---------------------------------------------------------------------------------------------------------
2902 CURSOR c_lease_app (p_chr_id okc_k_headers_b.id%type)
2903 IS
2904 --start modified abhsaxen for performance SQLID 20562365
2905   select 1
2906 from  okc_k_headers_b chr
2907 where ORIG_SYSTEM_SOURCE_CODE = G_OKL_LEASE_APP
2908 and chr.id = p_chr_id
2909 ;
2910 --end modified abhsaxen for performance SQLID 20562365
2911 
2912   BEGIN
2913       -- sjalasut, tapv_rec.khr_id would work here as the calling procedure
2914       -- continue to populate this value.
2915       OPEN c_lease_app(p_tapv_rec.khr_id);
2916       FETCH c_lease_app INTO l_dummy;
2917       l_lease_app_found := c_lease_app%FOUND;
2918       CLOSE c_lease_app;
2919 
2920       IF NOT l_lease_app_found THEN
2921 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2922 
2923         l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
2924 
2925       ---------------------------------------------------------------------------------------------------------
2926       -- 1.1 check credit line funding checklist template, used for existing requests w credit line that doesn't
2927       --    have checklist template setup
2928       -- existing check
2929       ---------------------------------------------------------------------------------------------------------
2930 /* no need
2931       OPEN c_chk_tpl(l_credit_id);
2932       FETCH c_chk_tpl INTO l_status;
2933 
2934       l_template_row_not_found := c_chk_tpl%NOTFOUND;
2935 
2936       CLOSE c_chk_tpl;
2937 
2938 
2939       -- credit line checklist tempate doesn't exists
2940       IF (l_template_row_not_found) THEN
2941         -- Funding request checklist template not found. Please setup checklist template for associated credit line.
2942         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
2943                             p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK');
2944 
2945         RAISE G_EXCEPTION_HALT_VALIDATION;
2946       END IF;
2947 */
2948 
2949       ---------------------------------------------------------------------------------------------------------
2950       -- 1.2 check credit line funding checklist template, used for existing requests w credit line that doesn't
2951       --    have checklist template setup
2952       -- status check
2953       ---------------------------------------------------------------------------------------------------------
2954 /*no need: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2955 
2956       IF (l_status IS NOT NULL and l_status <> 'ACTIVE') THEN
2957         -- Funding request checklist template status is new. Please activate Funding request checklist template
2958         -- for associated credit line.
2959         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
2960                             p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK7');
2961 
2962         RAISE G_EXCEPTION_HALT_VALIDATION;
2963       END IF;
2964 */
2965         ---------------------------------------------------------------------------------------------------------
2966         -- 2. check credit line funding checklist template expiration
2967         -- 2nd place to check when user submit a request for approval
2968         ---------------------------------------------------------------------------------------------------------
2969 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2970         IF l_credit_id IS NOT NULL THEN
2971 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2972 
2973           OPEN c_fund_chklst_tpl(l_credit_id);
2974           FETCH c_fund_chklst_tpl INTO l_dummy;
2975           l_fund_cls_tpl_exp_found := c_fund_chklst_tpl%FOUND;
2976           CLOSE c_fund_chklst_tpl;
2977 
2978           -- funding checklist template expired.
2979           IF (l_fund_cls_tpl_exp_found) THEN
2980             -- Funding request checklist template expired. Please modify effective date of Funding request checklist template.
2981             OKL_API.Set_Message(p_app_name     => G_APP_NAME,
2982                                 p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK6');
2983 
2984             RAISE G_EXCEPTION_HALT_VALIDATION;
2985           END IF;
2986 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2987         END IF;
2988       END IF;
2989 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2990 
2991 -- Fixed trx_status_code to include , 'SUBMITTED' for WF case 12-05-2003 cklee
2992 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
2993 -- commented out for okl.h     IF (p_tapv_rec.trx_status_code in ('APPROVED', 'SUBMITTED')) THEN
2994 
2995         ---------------------------------------------------------------------------------------------------------
2996         -- 3. check funding checklist if funding checklist has not been setup
2997         -- Note: This is used for existing request which doesn't have checklist setup
2998         ---------------------------------------------------------------------------------------------------------
2999 /* no need
3000         OPEN c_chklst_chk(p_tapv_rec.id);
3001         FETCH c_chklst_chk INTO l_dummy;
3002         l_list_row_not_found := c_chklst_chk%NOTFOUND;
3003         CLOSE c_chklst_chk;
3004 
3005         -- checklist doesn't exists
3006         IF (l_list_row_not_found) THEN
3007           -- Funding checklist not found. Please update request and setup checklist before submit request.
3008           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3009                               p_msg_name     => 'OKL_LLA_FUND_CHKLST_CHECK2');
3010 
3011           RAISE G_EXCEPTION_HALT_VALIDATION;
3012         END IF;
3013 */
3014 
3015 
3016         ---------------------------------------------------------------------------------------------------------
3017         -- 4. check checklist required items
3018         ---------------------------------------------------------------------------------------------------------
3019 /*no need for okl.h 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3020         OPEN c_chklst(p_tapv_rec.khr_id, p_tapv_rec.id);
3021         FETCH c_chklst INTO l_dummy;
3022         l_req_row_found := c_chklst%FOUND;
3023         CLOSE c_chklst;
3024 
3025         -- all required items have not met requirement
3026         IF (l_req_row_found) THEN
3027           -- Funding request has not met all checklist items. Please check off all mandatory checklist items.
3028           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3029                               p_msg_name     => 'OKL_LLA_FUND_CHKLST');
3030 
3031           RAISE G_EXCEPTION_HALT_VALIDATION;
3032         END IF;
3033       END IF;
3034 */
3035     RETURN l_return_status;
3036 
3037   EXCEPTION
3038     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3039       l_return_status := OKL_API.G_RET_STS_ERROR;
3040       RETURN l_return_status;
3041     WHEN OTHERS THEN
3042       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3043 
3044       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3045                           p_msg_name      => G_UNEXPECTED_ERROR,
3046                           p_token1        => G_SQLCODE_TOKEN,
3047                           p_token1_value  => SQLCODE,
3048                           p_token2        => G_SQLERRM_TOKEN,
3049                           p_token2_value  => SQLERRM);
3050       RETURN l_return_status;
3051   END;
3052 
3053 
3054   --------------------------------------------------------------------------
3055   ----- Validate amount if request status = 'SUBMITTED'
3056   --------------------------------------------------------------------------
3057   FUNCTION validate_trx_status_code(
3058     p_tapv_rec                  IN tapv_rec_type
3059   ) RETURN VARCHAR2
3060   IS
3061 
3062     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3063     l_amount          NUMBER := 0;
3064   BEGIN
3065 
3066     -- trx_status_code is required:
3067     IF (p_tapv_rec.trx_status_code IS NULL) OR
3068        (p_tapv_rec.trx_status_code = OKL_API.G_MISS_CHAR)
3069 
3070     THEN
3071       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3072                           p_msg_name     => G_REQUIRED_VALUE,
3073                           p_token1       => G_COL_NAME_TOKEN,
3074                           p_token1_value => 'Request Status');
3075       RAISE G_EXCEPTION_HALT_VALIDATION;
3076     END IF;
3077 
3078     RETURN l_return_status;
3079   EXCEPTION
3080     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3081       l_return_status := OKL_API.G_RET_STS_ERROR;
3082       RETURN l_return_status;
3083     WHEN OTHERS THEN
3084       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3085       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3086                           p_msg_name      => G_UNEXPECTED_ERROR,
3087                           p_token1        => G_SQLCODE_TOKEN,
3088                           p_token1_value  => SQLCODE,
3089                           p_token2        => G_SQLERRM_TOKEN,
3090                           p_token2_value  => SQLERRM);
3091       RETURN l_return_status;
3092   END;
3093 
3094   --------------------------------------------------------------------------
3095   ----- Validate payment due date
3096   --------------------------------------------------------------------------
3097   FUNCTION validate_payment_due_date(
3098 
3099     p_tapv_rec                  IN tapv_rec_type
3100   ) RETURN VARCHAR2
3101 
3102   IS
3103     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3104   BEGIN
3105 
3106 
3107     -- payment_due_date is required:
3108     IF (p_tapv_rec.date_invoiced IS NULL) OR
3109        (p_tapv_rec.date_invoiced = OKL_API.G_MISS_DATE)
3110     THEN
3111       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3112                           p_msg_name     => G_REQUIRED_VALUE,
3113                           p_token1       => G_COL_NAME_TOKEN,
3114                           p_token1_value => 'Payment due date');
3115       RAISE G_EXCEPTION_HALT_VALIDATION;
3116     END IF;
3117 
3118 /*** comment out this check
3119     -- date range check : date_entered will be default to sysdate @ UI
3120     IF (trunc(p_tapv_rec.date_invoiced) < trunc(p_tapv_rec.date_entered))
3121     THEN
3122       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3123                           p_msg_name     => 'OKL_LLA_RANGE_CHECK',
3124                           p_token1       => 'COL_NAME1',
3125                           p_token1_value => 'Payment due date',
3126                           p_token2       => 'COL_NAME2',
3127                           p_token2_value => 'Date entered');
3128 
3129       RAISE G_EXCEPTION_HALT_VALIDATION;
3130     END IF;
3131 ***/
3132     RETURN l_return_status;
3133   EXCEPTION
3134     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3135       l_return_status := OKL_API.G_RET_STS_ERROR;
3136       RETURN l_return_status;
3137     WHEN OTHERS THEN
3138       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3139       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3140                           p_msg_name      => G_UNEXPECTED_ERROR,
3141                           p_token1        => G_SQLCODE_TOKEN,
3142                           p_token1_value  => SQLCODE,
3143                           p_token2        => G_SQLERRM_TOKEN,
3144                           p_token2_value  => SQLERRM);
3145       RETURN l_return_status;
3146 
3147   END;
3148 
3149   --------------------------------------------------------------------------
3150   ----- Validate Funding Amount... when SUBMITTED, APPROVED
3151   --------------------------------------------------------------------------
3152   FUNCTION validate_header_amount(
3153     p_tapv_rec                  IN tapv_rec_type
3154   ) RETURN VARCHAR2
3155   IS
3156     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3157     l_api_version       NUMBER := 1.0;
3158     l_init_msg_list     VARCHAR2(1) := OKL_API.G_FALSE;
3159     x_return_status     VARCHAR2(1);
3160     x_msg_count         NUMBER;
3161     x_msg_data          VARCHAR2(2000);
3162     x_value             NUMBER := 0;
3163 --    l_chr_id            NUMBER;
3164 --    l_funding_type_code VARCHAR2(30);
3165     l_cur_total_amount  NUMBER := 0;
3166     l_cur_amount  NUMBER := 0;
3167     l_total_hasbeen_funded_amt_ven NUMBER := 0;
3168     l_total_hasbeen_funded_amount NUMBER := 0;
3169     l_total_canbe_funded_amount NUMBER := 0;
3170     l_total_canbe_OEC_amount NUMBER := 0;
3171     l_total_canbe_expense_amount NUMBER := 0;
3172     l_total_canbe_expense_amount_g NUMBER := 0;
3173     l_total_credit_amount NUMBER := 0;
3174     l_message_name      VARCHAR2(30);
3175     l_resuts_amount     NUMBER := 0;
3176     l_credit_id         NUMBER := 0;
3177     l_booked_count      NUMBER := 0;
3178     l_total_fund_amount NUMBER := 0;
3179     l_total_check_amount NUMBER := 0;
3180 
3181     l_invalid_fund         VARCHAR2(150) := 'X';
3182     l_prefund_amount       NUMBER := 0;
3183     l_pf_amount       NUMBER := 0;
3184     l_sr_amount       NUMBER := 0;
3185     l_amount_buffer       NUMBER := 0;
3186 
3187     l_loan_rev        NUMBER := 0;
3188     l_loan_row_found  boolean := false;
3189 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3190     l_chk_credit_id   number;
3191 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3192 
3193     l_line_amt        NUMBER := 0; --bug#5600694
3194 
3195 -- check FA line
3196 -- OKL_FUNDING_PVT.get_contract_line_funded_amt(a.CHR_ID, a.CLE_ID)
3197 -- will return 0 if user has not been funded FA line yet
3198 -- sjalasut, modified the below cursor to have khr_id be referred from
3199 -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
3200 -- as part of OKLR12B disbursements project
3201   CURSOR c_invalid_fund (p_fund_id  NUMBER)
3202   IS
3203 SELECT
3204 	   nvl(a.ASSET_NUMBER,'X') ASSET_NUMBER, a.CHR_ID, a.CLE_ID, b.ipvs_id
3205  	   FROM okl_assets_lov_uv a,
3206  	      okl_trx_ap_invoices_b b
3207  	   WHERE a.chr_id = b.khr_id
3208  	   and   b.id = p_fund_id;
3209  	 /*bug#5600694 veramach 29-Jun-2007
3210  	 commented and changed the cursor as above to improve the performance
3211  	 SELECT
3212   nvl(a.ASSET_NUMBER,'X')
3213 FROM okl_assets_lov_uv a,
3214 --START:| 13-Apr-2006  cklee -- Fixed bug#5160342                                    |
3215      okl_trx_ap_invoices_b b,
3216      OKL_TXL_AP_INV_LNS_V c
3217 WHERE a.chr_id = c.khr_id
3218 and   b.id = c.TAP_ID
3219 and   a.cle_id = c.kle_id
3220 --END:| 13-Apr-2006  cklee -- Fixed bug#5160342                                    |
3221 and   b.id = p_fund_id
3222 and   OKL_FUNDING_PVT.get_contract_line_amt(a.CHR_ID, a.CLE_ID, b.ipvs_id) > 0
3223 and   OKL_FUNDING_PVT.get_contract_line_funded_amt(a.CHR_ID, a.CLE_ID) >
3224   OKL_FUNDING_PVT.get_contract_line_amt(a.CHR_ID, a.CLE_ID, b.ipvs_id); */
3225 
3226 
3227 -- get current amount for Asset, Expense, or Supplier Retention
3228 
3229   CURSOR c_curr (p_fund_id  NUMBER)
3230   IS
3231   select nvl(sum(b.amount),0)
3232   from okl_trx_ap_invoices_b a,
3233        okl_txl_ap_inv_lns_b b
3234   where a.id = b.tap_id
3235   and b.tap_id = p_fund_id
3236   and a.trx_status_code IN ('ENTERED','SUBMITTED')
3237   ;
3238 
3239 -- get approved amount for Asset
3240   -- sjalasut, made changes to the below cursor to have khr_id be referred from
3241   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. change made
3242   -- as part of OKLR12B disbursements project.
3243   CURSOR c_tot_asset_fund (p_contract_id  NUMBER)
3244   IS
3245   select nvl(sum(b.amount),0)
3246   from okl_trx_ap_invoices_b a,
3247        okl_txl_ap_inv_lns_all_b b
3248   where a.id = b.tap_id
3249   and a.trx_status_code in ('APPROVED', 'PROCESSED')
3250   and a.funding_type_code ='ASSET'
3251   and b.amount > 0 --?
3252   and b.khr_id = p_contract_id;
3253 
3254 -- get approved amount for Expense by specific vendor
3255   -- sjalasut, made changes to the below cursor to have khr_id be referred from
3256   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. change made
3257   -- as part of OKLR12B disbursements project.
3258   CURSOR c_tot_expense_fund (p_contract_id  NUMBER, p_vendor_site_id  NUMBER)
3259   IS
3260   select nvl(sum(b.amount),0)
3261   from okl_trx_ap_invoices_b a,
3262        okl_txl_ap_inv_lns_all_b b
3263   where a.id = b.tap_id
3264   and a.trx_status_code in ('APPROVED', 'PROCESSED')
3265   and a.funding_type_code ='EXPENSE'
3266   and b.amount > 0 --?
3267   and b.khr_id = p_contract_id
3268   and exists (select null
3269               from   okx_vendor_sites_v vs
3270               where  vs.id1 = a.ipvs_id
3271               and    vs.id1 = p_vendor_site_id)
3272   ;
3273 
3274   CURSOR c_booked (p_contract_id  NUMBER)
3275   IS
3276   select count(1)
3277   from OKC_K_HEADERS_B a
3278   where id = p_contract_id
3279   and sts_code = 'BOOKED'
3280   ;
3281 
3282 -- bug 2604862
3283   CURSOR c_loan_revolving (p_contract_id  NUMBER)
3284   IS
3285   select 1 from OKL_K_HEADERS khr
3286   where khr.id = p_contract_id
3287   and khr.deal_type = 'LOAN-REVOLVING';
3288 
3289   BEGIN
3290 
3291     -- header Amount is required
3292     IF ((p_tapv_rec.funding_type_code in ('PREFUNDING','BORROWER_PAYMENT')) AND
3293         (p_tapv_rec.amount IS NULL OR
3294          p_tapv_rec.amount = OKL_API.G_MISS_NUM))
3295     THEN
3296       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3297                           p_msg_name     => G_REQUIRED_VALUE,
3298                           p_token1       => G_COL_NAME_TOKEN,
3299                           p_token1_value => 'Amount');
3300       RAISE G_EXCEPTION_HALT_VALIDATION;
3301     END IF;
3302 
3303 -- 1) get contract OEC amount w/o re-lease
3304 
3305     -- 1) get contract OEC
3306     l_total_canbe_OEC_amount := get_chr_oec_canbe_funded(p_tapv_rec.khr_id);
3307 
3308    --cklee start: bug 6128765
3309 /*    l_total_canbe_expense_amount := nvl(get_chr_exp_canbe_funded_amt(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id),0);
3310 
3311     l_total_canbe_expense_amount_g := nvl(get_chr_exp_canbe_funded_amt(p_tapv_rec.khr_id),0); -- for global check*/
3312      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);
3313      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
3314    --cklee end: bug 6128765
3315 
3316     l_total_canbe_funded_amount := l_total_canbe_OEC_amount + l_total_canbe_expense_amount_g;
3317 
3318 -- bug 2604862
3319     OPEN c_loan_revolving(p_tapv_rec.khr_id);
3320     FETCH c_loan_revolving INTO l_loan_rev;
3321     l_loan_row_found := c_loan_revolving%FOUND;
3322     CLOSE c_loan_revolving;
3323 
3324     -- is loan revolving contract
3325     IF (l_loan_row_found) THEN
3326       -- get amount for the remaining of the attach credit line
3327       l_total_canbe_funded_amount := get_creditRem_by_chrid(p_tapv_rec.khr_id);
3328     END IF;
3329 -- bug 2604862
3330 
3331     -- get total has been funded
3332     l_total_hasbeen_funded_amount := get_total_funded(p_tapv_rec.khr_id);
3333 
3334 /*
3335 -- cklee 05/19/2004: exclude supplier retention and manual disbursement
3336     l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
3337 
3338     IF ( l_credit_id IS NULL AND
3339          p_tapv_rec.funding_type_code NOT IN ('SUPPLIER_RETENTION', 'MANUAL_DISB')) THEN
3340       -- Your request cannot be submitted. Credit line for this contract doesn't exists.
3341       l_message_name := 'OKL_LLA_FUND_CREDIT_AMT_CHK2';
3342       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3343                           p_msg_name      => l_message_name);
3344 
3345       RAISE G_EXCEPTION_HALT_VALIDATION;
3346     END IF;
3347 */
3348     l_total_credit_amount := get_creditRem_by_chrid(p_tapv_rec.khr_id);
3349 
3350     -- check booked
3351     OPEN c_booked(p_tapv_rec.khr_id);
3352     FETCH c_booked INTO l_booked_count;
3353     CLOSE c_booked;
3354 
3355     -- need to get amount from different AP table
3356     -- pre-funding current amount
3357     IF (p_tapv_rec.funding_type_code in ('PREFUNDING','BORROWER_PAYMENT')) THEN
3358 
3359       l_cur_amount := p_tapv_rec.amount; -- stores amount at header, user could changes the amount when submit
3360     ELSE
3361       -- get current amount: NOT used for pre-funding
3362       OPEN c_curr (p_tapv_rec.id);
3363       FETCH c_curr INTO l_cur_amount;
3364       CLOSE c_curr;
3365     END IF;
3366 
3367     -- check amount
3368     IF (l_cur_amount = 0 ) THEN
3369       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3370                           p_msg_name     => 'OKL_LLA_AMOUNT_CHECK');
3371       RAISE G_EXCEPTION_HALT_VALIDATION;
3372     END IF;
3373 
3374     l_resuts_amount := l_total_hasbeen_funded_amount + l_cur_amount;
3375 
3376     -- always check vs credit line total limit except adjustment request (pre-funding with negative amount)
3377     -- cklee 10/31/03 exclude supplier retention also
3378     -- cklee 05/14/04 exclude manual disb also
3379     IF ( p_tapv_rec.funding_type_code NOT IN ('PREFUNDING', 'SUPPLIER_RETENTION', 'MANUAL_DISB') OR
3380          (p_tapv_rec.funding_type_code = 'PREFUNDING' AND l_cur_amount > 0)
3381        ) THEN
3382       --Your request cannot be submitted. The total amount of this request exceeds the value of the contract credit limit.
3383       l_message_name := 'OKL_LLA_FUND_CREDIT_AMT_CHK';
3384 --      IF (l_resuts_amount > l_total_credit_amount) THEN
3385 -- fixed bug#3220634
3386       IF (l_total_credit_amount - l_cur_amount < 0) THEN
3387 
3388 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3389         l_chk_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
3390         IF l_loan_row_found OR (NOT l_loan_row_found and l_chk_credit_id is not null) THEN
3391           OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3392                           p_msg_name      => l_message_name);
3393           RAISE G_EXCEPTION_HALT_VALIDATION;
3394         END IF;
3395 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring                            |
3396       END IF;
3397 
3398     -- adjustment check
3399     ELSIF (p_tapv_rec.funding_type_code = 'PREFUNDING' AND l_cur_amount < 0) THEN
3400 
3401       -- if it's revolving line of credit loan contract's adjustment
3402       IF (l_loan_row_found) THEN
3403         l_resuts_amount := l_total_hasbeen_funded_amount + l_cur_amount;
3404          --Your request cannot be submitted. The total funded amount cannot be less than 0.
3405         IF (l_resuts_amount < 0) THEN
3406           l_message_name := 'OKL_LLA_ADJUSTMENTS_AMT_CHK';
3407             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3408                           p_msg_name      => l_message_name);
3409             RAISE G_EXCEPTION_HALT_VALIDATION;
3410 
3411         END IF;
3412 
3413       ELSE
3414         --
3415         -- NOTE: adjustment is based on vendor specific. we need to calculate total has been funded amount
3416         --       by vendor
3417         --
3418         l_total_hasbeen_funded_amt_ven := get_chr_exp_hasbeen_funded_amt(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id)+
3419                                            get_chr_oec_hasbeen_funded_amt(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id)+
3420                                            get_amount_prefunded(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id)+
3421                                            -- 12-09-2003 cklee added adjustment
3422                                            get_chr_funded_adjs(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id);
3423 
3424         l_resuts_amount := l_total_hasbeen_funded_amt_ven + l_cur_amount;
3425          --Your request cannot be submitted. The total funded amount cannot be less than 0.
3426         IF (l_resuts_amount < 0) THEN
3427           l_message_name := 'OKL_LLA_ADJUSTMENTS_AMT_CHK';
3428 
3429             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3430                           p_msg_name      => l_message_name);
3431             RAISE G_EXCEPTION_HALT_VALIDATION;
3432 
3433         END IF;
3434       END IF;
3435 
3436     END IF;
3437 
3438     IF (p_tapv_rec.funding_type_code ='PREFUNDING') THEN
3439 
3440       -- check if it is NOT a loan revolving contract
3441       IF NOT l_loan_row_found THEN
3442         -- booked: check contract
3443         IF (l_booked_count > 0 AND l_cur_amount > 0 ) THEN
3444            --Your request cannot be submitted. Pre-funding requests are not allowed for contracts in Booked status.
3445             l_message_name := 'OKL_LLA_PREFUNDED_AMT_CHK2';
3446             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3447                           p_msg_name      => l_message_name);
3448             RAISE G_EXCEPTION_HALT_VALIDATION;
3449         END IF;
3450 
3451       END IF;
3452 
3453     ELSIF (p_tapv_rec.funding_type_code ='ASSET') THEN
3454 
3455       -- 1. check FA can be funded
3456       -- invalid fund amount for each FA line
3457       -- we need to show at most ONE asset number at a time
3458       /* Bug#5600694 to improve performance
3459       OPEN c_invalid_fund(p_tapv_rec.id);
3460 
3461       FETCH c_invalid_fund INTO l_invalid_fund;
3462       CLOSE c_invalid_fund;
3463       */
3464  	    FOR i IN  c_invalid_fund(p_tapv_rec.id)
3465  	    LOOP
3466 
3467  	       l_line_amt := OKL_FUNDING_PVT.get_contract_line_amt(i.CHR_ID, i.CLE_ID, i.ipvs_id);
3468 
3469  	        IF l_line_amt > 0
3470  	             AND OKL_FUNDING_PVT.get_contract_line_funded_amt(i.CHR_ID, i.CLE_ID) >
3471  	                  l_line_amt
3472  	        THEN
3473  	          l_invalid_fund := i.ASSET_NUMBER;
3474  	          EXIT;
3475  	        END IF;
3476 
3477  	    END LOOP;
3478 
3479  	       --End Bug#5600694
3480 
3481       IF (l_invalid_fund <> 'X') THEN
3482 
3483         l_message_name := 'OKL_LLA_FUND_ASSET_AMT_CHK';
3484         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3485                           p_msg_name      => l_message_name,
3486                           p_token1        => 'ASSET_NUMBER',
3487                           p_token1_value  => l_invalid_fund);
3488         RAISE G_EXCEPTION_HALT_VALIDATION;
3489       END IF;
3490 
3491       -- 2. check asset : will never happen if catch by previous check
3492       -- get current total HAS BEEN approved amount for ASSET
3493 
3494       OPEN c_tot_asset_fund (p_tapv_rec.khr_id);
3495       FETCH c_tot_asset_fund INTO l_cur_total_amount;
3496       CLOSE c_tot_asset_fund;
3497 
3498       l_resuts_amount := l_cur_total_amount + l_cur_amount;
3499       IF (l_resuts_amount > l_total_canbe_OEC_amount) THEN
3500         --Your request cannot be submitted. The total amount of this request exceeds
3501         -- the value of the contract total asset amount.
3502         l_message_name := 'OKL_LLA_FUND_TOT_ASSET_AMT_CHK';
3503         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3504                           p_msg_name      => l_message_name);
3505         RAISE G_EXCEPTION_HALT_VALIDATION;
3506       END IF;
3507 
3508     ELSIF (p_tapv_rec.funding_type_code ='EXPENSE') THEN
3509 
3510       -- 1. check expense
3511       -- get current total HAS BEEN approved amount for EXPENSE
3512       OPEN c_tot_expense_fund(p_tapv_rec.khr_id, p_tapv_rec.ipvs_id);
3513       FETCH c_tot_expense_fund INTO l_cur_total_amount;
3514       CLOSE c_tot_expense_fund;
3515 
3516       l_resuts_amount := l_cur_total_amount + l_cur_amount;
3517       IF (l_resuts_amount > l_total_canbe_expense_amount) THEN
3518 
3519         l_message_name := 'OKL_LLA_EXPENSE_AMT_CHK';
3520         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3521                           p_msg_name      => l_message_name);
3522         RAISE G_EXCEPTION_HALT_VALIDATION;
3523       END IF;
3524 
3525     END IF;
3526 
3527     -- check for any kind of requests
3528     -- check total can be funded
3529     l_resuts_amount := l_total_hasbeen_funded_amount + l_cur_amount;
3530 
3531     -- exclude prefunding and supplier retention
3532     -- exclude manual disb
3533     IF (p_tapv_rec.funding_type_code NOT IN ('PREFUNDING', 'SUPPLIER_RETENTION', 'MANUAL_DISB')) THEN
3534 
3535       IF (l_resuts_amount > l_total_canbe_funded_amount) THEN
3536 
3537         l_message_name := 'OKL_LLA_FUNDED_AMT_CHK';
3538         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3539                           p_msg_name      => l_message_name);
3540         RAISE G_EXCEPTION_HALT_VALIDATION;
3541       END IF;
3542     END IF;
3543 
3544     RETURN l_return_status;
3545   EXCEPTION
3546     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3547       l_return_status := OKL_API.G_RET_STS_ERROR;
3548       RETURN l_return_status;
3549     WHEN OTHERS THEN
3550       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3551       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3552                           p_msg_name      => G_UNEXPECTED_ERROR,
3553                           p_token1        => G_SQLCODE_TOKEN,
3554                           p_token1_value  => SQLCODE,
3555                           p_token2        => G_SQLERRM_TOKEN,
3556                           p_token2_value  => SQLERRM);
3557       RETURN l_return_status;
3558   END;
3559 
3560   --------------------------------------------------------------------------
3561 -- Revolving line of credit contract allows adjustment funding request.
3562 -- Funding module implement adjustment request by Pre-funding type with
3563 -- negative request amount
3564 
3565   --------------------------------------------------------------------------
3566 
3567   FUNCTION validate_header_amount_for_RL(
3568     p_tapv_rec                  IN tapv_rec_type
3569   ) RETURN VARCHAR2
3570   IS
3571     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3572     l_loan_rev        NUMBER := 0;
3573     l_loan_row_found  boolean := false;
3574 
3575 -- bug 2604862
3576   CURSOR c_loan_revolving (p_contract_id  NUMBER)
3577 
3578   IS
3579   select 1 from OKL_K_HEADERS khr
3580   where khr.id = p_contract_id
3581   and khr.deal_type = 'LOAN-REVOLVING';
3582 
3583   BEGIN
3584 
3585 -- bug 2604862
3586     OPEN c_loan_revolving(p_tapv_rec.khr_id);
3587     FETCH c_loan_revolving INTO l_loan_rev;
3588     l_loan_row_found := c_loan_revolving%FOUND;
3589     CLOSE c_loan_revolving;
3590 
3591     -- is loan revolving contract
3592     IF (l_loan_row_found) THEN
3593       IF (p_tapv_rec.funding_type_code = 'PREFUNDING' AND nvl(p_tapv_rec.amount,0) >= 0 ) THEN
3594         --Please enter negative amount for your adjustment. Revolving line of credit loan contract are not allowed for pre-funding request with positive amount.
3595         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3596                           p_msg_name      => 'OKL_LLA_REVLOAN_ADJ_AMT_CHK');
3597         RAISE G_EXCEPTION_HALT_VALIDATION;
3598 
3599       END IF;
3600 
3601       IF (p_tapv_rec.funding_type_code = 'BORROWER_PAYMENT' AND nvl(p_tapv_rec.amount,0) < 0 ) THEN
3602         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3603                           p_msg_name     => 'OKL_LLA_POSITIVE_AMOUNT_ONLY',
3604                           p_token1       => 'COL_NAME',
3605                           p_token1_value => 'Amount');
3606 
3607         RAISE G_EXCEPTION_HALT_VALIDATION;
3608       END IF;
3609 
3610     END IF;
3611 -- bug 2604862
3612 
3613 
3614     RETURN l_return_status;
3615   EXCEPTION
3616     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3617       l_return_status := OKL_API.G_RET_STS_ERROR;
3618 
3619       RETURN l_return_status;
3620     WHEN OTHERS THEN
3621       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3622       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3623                           p_msg_name      => G_UNEXPECTED_ERROR,
3624                           p_token1        => G_SQLCODE_TOKEN,
3625                           p_token1_value  => SQLCODE,
3626                           p_token2        => G_SQLERRM_TOKEN,
3627                           p_token2_value  => SQLERRM);
3628       RETURN l_return_status;
3629   END;
3630 
3631   --------------------------------------------------------------------------
3632   ----- Populate additional attributes for BPD
3633   --------------------------------------------------------------------------
3634   FUNCTION populate_more_attrs(
3635     p_tapv_rec                  IN OUT NOCOPY tapv_rec_type
3636   ) RETURN VARCHAR2
3637   IS
3638     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3639 --    l_set_of_books_id NUMBER;
3640     l_terms_id NUMBER;
3641     l_application_id NUMBER;
3642     l_pay_group_lookup_code PO_VENDOR_SITES_ALL.PAY_GROUP_LOOKUP_CODE%TYPE;
3643     l_vendor_id NUMBER;
3644 
3645   CURSOR c_vendor(p_vendor_site_id NUMBER)
3646   IS
3647   --start modified abhsaxen for performance SQLID 20562381
3648   select vs.vendor_id
3649   from   ap_supplier_sites vs
3650   where vs.vendor_site_id = p_vendor_site_id
3651   ;
3652   --end modified abhsaxen for performance SQLID 20562381
3653 
3654   CURSOR c_app
3655   IS
3656   select a.application_id
3657   from FND_APPLICATION a
3658   where APPLICATION_SHORT_NAME = 'OKL'
3659   ;
3660 
3661 /*
3662   CURSOR c_set_of_books(p_org_id  NUMBER)
3663   IS
3664   select to_number(a.set_of_books_id)
3665   from HR_OPERATING_UNITS a
3666   where ORGANIZATION_ID = p_org_id
3667   ;
3668 */
3669 
3670   CURSOR c_vendor_sites(p_vendor_site_id  NUMBER)
3671   IS
3672   select a.TERMS_ID, a.PAY_GROUP_LOOKUP_CODE
3673   from PO_VENDOR_SITES_ALL a
3674   where vendor_site_id = p_vendor_site_id
3675   ;
3676 
3677     -- select apps.FND_DOC_SEQ_885_S.nextval from dual;
3678 
3679     l_document_category VARCHAR2(100):= 'OKL Lease Pay Invoices';--'OKL Lease Receipt Invoices';
3680     l_okl_application_id number(3) := 540;
3681 
3682     lX_dbseqnm           VARCHAR2(2000):= '';
3683     lX_dbseqid           NUMBER(38):= NULL;
3684 
3685   BEGIN
3686 
3687 /*
3688 -- 1. SET_OF_BOOKS_ID
3689     OPEN c_set_of_books(p_tapv_rec.org_id);
3690     FETCH c_set_of_books INTO l_set_of_books_id;
3691     CLOSE c_set_of_books;
3692 */
3693 
3694   p_tapv_rec.SET_OF_BOOKS_ID := OKL_ACCOUNTING_UTIL.get_set_of_books_id;--l_set_of_books_id;
3695 -- 2. IPPT_ID
3696   -- cklee 05/04/2004
3697     IF (p_tapv_rec.IPPT_ID IS NULL or
3698         p_tapv_rec.IPPT_ID = OKL_API.G_MISS_NUM) THEN
3699 
3700       OPEN c_vendor_sites(p_tapv_rec.ipvs_id);
3701       FETCH c_vendor_sites INTO l_terms_id, l_pay_group_lookup_code;
3702       CLOSE c_vendor_sites;
3703 
3704       p_tapv_rec.IPPT_ID := l_terms_id;
3705 
3706     END IF;
3707 
3708 -- 3. INVOICE_NUMBER
3709 
3710     OPEN c_app;
3711 
3712     FETCH c_app INTO l_application_id;
3713     CLOSE c_app;
3714 
3715     l_okl_application_id := nvl(l_application_id,540);
3716 --
3717 -- display specific application error if 'OKL Lease Pay Invoices' has not been setup or setup incorrectly
3718 --
3719     BEGIN
3720       p_tapv_rec.invoice_number := fnd_seqnum.get_next_sequence
3721                          (appid      =>  l_okl_application_id,
3722                          cat_code    =>  l_document_category,
3723                          sobid       =>  OKL_ACCOUNTING_UTIL.get_set_of_books_id,--l_set_of_books_id,
3724                          met_code    =>  'A',
3725                          trx_date    =>  SYSDATE,
3726                          dbseqnm     =>  lx_dbseqnm,
3727                          dbseqid     =>  lx_dbseqid);
3728     EXCEPTION
3729       WHEN OTHERS THEN
3730         IF SQLCODE = 100 THEN
3731           OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3732                               p_msg_name      => 'OKL_PAY_INV_SEQ_CHECK');
3733           RAISE G_EXCEPTION_HALT_VALIDATION;
3734         END IF;
3735     END;
3736 
3737 -- cklee set vendor_invoice_numner if it's NULL
3738     IF (p_tapv_rec.vendor_invoice_number IS NULL ) THEN
3739       p_tapv_rec.vendor_invoice_number := p_tapv_rec.invoice_number;
3740     END IF;
3741 
3742 -- 4. NETTABLE_YN
3743   p_tapv_rec.NETTABLE_YN := 'N';
3744 
3745 -- 5. PAY_GROUP_LOOKUP_CODE
3746   -- cklee 05/04/2004
3747     IF (p_tapv_rec.PAY_GROUP_LOOKUP_CODE IS NULL or
3748         p_tapv_rec.PAY_GROUP_LOOKUP_CODE = OKL_API.G_MISS_CHAR) THEN
3749 
3750 -- fixed PAY_GROUP_LOOKUP_CODE default data missing issues
3751       OPEN c_vendor_sites(p_tapv_rec.ipvs_id);
3752       FETCH c_vendor_sites INTO l_terms_id, l_pay_group_lookup_code;
3753       CLOSE c_vendor_sites;
3754 
3755       p_tapv_rec.PAY_GROUP_LOOKUP_CODE := l_pay_group_lookup_code;
3756 
3757     END IF;
3758 
3759 -- 6. vednor id
3760     OPEN c_vendor(p_tapv_rec.ipvs_id);
3761     FETCH c_vendor INTO l_vendor_id;
3762     CLOSE c_vendor;
3763 
3764   p_tapv_rec.VENDOR_ID := l_vendor_id;
3765 
3766 -- 7. invoice_type
3767 -- cklee 05/04/2004
3768 
3769    IF (p_tapv_rec.INVOICE_TYPE is null or
3770        p_tapv_rec.INVOICE_TYPE = OKL_API.G_MISS_CHAR) THEN
3771 
3772      p_tapv_rec.INVOICE_TYPE := G_STANDARD;
3773 
3774    END IF;
3775 --start:| 06-Aug-08  cklee Fixed bug: 6860777                                        |
3776 /* system shall not convert wrong data to correct data, instead, display error and request user
3777 to fix before proceed.
3778     -- 8. If invoice type is G_STANDARD then invoice amount is positive
3779     --    If invoice type is G_CREDIT then the invoice amount is negative.
3780     --    sjalasut, made changes to incorporate the business rule as part
3781     --    of OKLR12B Disbursements Project
3782     IF((p_tapv_rec.INVOICE_TYPE = G_STANDARD AND p_tapv_rec.AMOUNT < 0)
3783        OR(p_tapv_rec.INVOICE_TYPE = G_CREDIT AND p_tapv_rec.AMOUNT > 0))THEN
3784       p_tapv_rec.AMOUNT := ((p_tapv_rec.AMOUNT) * (-1));
3785     END IF;
3786 */
3787 --end:| 06-Aug-08  cklee Fixed bug: 6860777                                        |
3788 
3789     RETURN l_return_status;
3790   EXCEPTION
3791 
3792     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3793 
3794       l_return_status := OKL_API.G_RET_STS_ERROR;
3795 
3796 
3797       RETURN l_return_status;
3798     WHEN OTHERS THEN
3799       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3800 
3801       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3802                           p_msg_name      => G_UNEXPECTED_ERROR,
3803                           p_token1        => G_SQLCODE_TOKEN,
3804                           p_token1_value  => SQLCODE,
3805                           p_token2        => G_SQLERRM_TOKEN,
3806                           p_token2_value  => SQLERRM);
3807       RETURN l_return_status;
3808   END;
3809 
3810   --------------------------------------------------------------------------
3811   ----- Validate vendor site ID
3812   --------------------------------------------------------------------------
3813   FUNCTION validate_chr_status(
3814     p_chr_id                  IN NUMBER
3815   ) RETURN VARCHAR2
3816 
3817   IS
3818     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3819     l_status          OKC_STATUSES_V.meaning%TYPE;
3820 
3821   CURSOR c_sts (p_contract_id  NUMBER)
3822   IS
3823 select ste.meaning
3824 from   OKC_STATUSES_V ste,
3825        okc_k_headers_b chr
3826 where  ste.code = chr.sts_code
3827 and    chr.id = p_chr_id
3828 ;
3829 
3830   BEGIN
3831 
3832     IF (okl_funding_pvt.is_chr_fundable_status(p_chr_id) = 0) THEN
3833 
3834       OPEN c_sts(p_chr_id);
3835       FETCH c_sts INTO l_status;
3836       CLOSE c_sts;
3837 
3838       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3839                           p_msg_name     => 'OKL_LLA_INVALID_FUNDED_REQUEST',
3840                           p_token1       => 'CHR_STATUS',
3841                           p_token1_value => l_status);
3842       RAISE G_EXCEPTION_HALT_VALIDATION;
3843     END IF;
3844 
3845     RETURN l_return_status;
3846   EXCEPTION
3847     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3848       l_return_status := OKL_API.G_RET_STS_ERROR;
3849       RETURN l_return_status;
3850     WHEN OTHERS THEN
3851 
3852       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3853       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3854                           p_msg_name      => G_UNEXPECTED_ERROR,
3855                           p_token1        => G_SQLCODE_TOKEN,
3856                           p_token1_value  => SQLCODE,
3857                           p_token2        => G_SQLERRM_TOKEN,
3858                           p_token2_value  => SQLERRM);
3859       RETURN l_return_status;
3860   END;
3861 
3862   --------------------------------------------------------------------------
3863   ----- Validate line of credit attach to funded contract
3864   --------------------------------------------------------------------------
3865   FUNCTION validate_creditline(
3866     p_tapv_rec                  IN tapv_rec_type
3867  ) RETURN VARCHAR2
3868 
3869 
3870 IS
3871 
3872     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3873 
3874     l_REVOLVING_CREDIT_YN  OKL_K_HEADERS.REVOLVING_CREDIT_YN%TYPE;
3875     l_END_DATE  OKC_K_HEADERS_B.END_DATE%TYPE;
3876 
3877     l_DEAL_TYPE  OKL_K_HEADERS.DEAL_TYPE%TYPE;
3878     l_creditline_row_found  boolean := false;
3879     l_credit_id okc_k_headers_b.id%TYPE;
3880 
3881   CURSOR c_contract (p_contract_id  NUMBER)
3882   IS
3883   select a.DEAL_TYPE
3884   from   OKL_K_HEADERS a
3885   where  a.id = p_contract_id
3886   ;
3887 
3888   CURSOR c_credit (p_credit_id  NUMBER)
3889   IS
3890   select khr.REVOLVING_CREDIT_YN,
3891          NVL(chr.END_DATE, SYSDATE)
3892   from   okl_k_headers khr,
3893          okc_k_headers_b chr
3894   where  khr.id = chr.id
3895   and    chr.id = p_credit_id
3896   ;
3897 
3898 
3899 begin
3900 
3901     -- 1) get deal type
3902     OPEN c_contract(p_tapv_rec.khr_id);
3903     FETCH c_contract INTO l_DEAL_TYPE;
3904     CLOSE c_contract;
3905 
3906     -- 2) get revolving flag
3907     l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_tapv_rec.khr_id);
3908 
3909     OPEN c_credit(l_credit_id);
3910     FETCH c_credit INTO l_REVOLVING_CREDIT_YN,
3911                           l_END_DATE;
3912     l_creditline_row_found := c_credit%FOUND;
3913     CLOSE c_credit;
3914 
3915     IF (l_creditline_row_found) THEN
3916 
3917       IF ((l_DEAL_TYPE = 'LOAN-REVOLVING' AND l_REVOLVING_CREDIT_YN <> 'Y')
3918           OR
3919 
3920           (l_DEAL_TYPE <> 'LOAN-REVOLVING' AND l_REVOLVING_CREDIT_YN = 'Y')) THEN
3921 
3922            --Either Revolving line of credit attach to a normal contract (book classification is not LOAN-REVOLVING)
3923            -- or non-revolving line of credit attach to LOAN-REVOLVING contract.
3924            -- Invalid credit line attach to funding request contract.
3925             OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3926                             p_msg_name     => 'OKL_LLA_CREDITLINE_CHECK');
3927 
3928             RAISE G_EXCEPTION_HALT_VALIDATION;
3929 
3930       END IF;
3931 
3932       IF (trunc(l_END_DATE) < trunc(SYSDATE)) THEN
3933             OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3934                             p_msg_name     => 'OKL_LLA_CREDITLINE_EXPIRED');
3935 
3936             RAISE G_EXCEPTION_HALT_VALIDATION;
3937 
3938       END IF;
3939 
3940     ELSE -- creditline not found
3941 
3942 --      IF (p_tapv_rec.funding_type_code NOT IN (G_SUPPLIER_RETENTION_TYPE_CODE,G_MANUAL_DISB)) THEN
3943 --        -- There is no credit line for funding request contract.
3944 -- start: 23-May-2005  cklee okl.h Lease App IA Authoring
3945 -- Credit Line is not required after okl.h except loan-revolving contract
3946 --
3947       IF l_DEAL_TYPE = 'LOAN-REVOLVING' THEN
3948         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3949                             p_msg_name     => 'OKL_LLA_CREDITLINE_NOTFOUND');
3950 
3951         RAISE G_EXCEPTION_HALT_VALIDATION;
3952      END IF;
3953 -- end: 23-May-2005  cklee okl.h Lease App IA Authoring
3954 
3955     END IF;
3956 
3957 
3958 
3959     RETURN l_return_status;
3960   EXCEPTION
3961 
3962     WHEN G_EXCEPTION_HALT_VALIDATION THEN
3963       l_return_status := OKL_API.G_RET_STS_ERROR;
3964       RETURN l_return_status;
3965     WHEN OTHERS THEN
3966       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
3967       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
3968                           p_msg_name      => G_UNEXPECTED_ERROR,
3969                           p_token1        => G_SQLCODE_TOKEN,
3970                           p_token1_value  => SQLCODE,
3971                           p_token2        => G_SQLERRM_TOKEN,
3972                           p_token2_value  => SQLERRM);
3973       RETURN l_return_status;
3974   END;
3975 
3976   --------------------------------------------------------------------------
3977   ----- Validate Funding (Invoice) Number...
3978   --------------------------------------------------------------------------
3979   FUNCTION validate_vendor_invoice_number(
3980     p_tapv_rec                  IN tapv_rec_type
3981   ) RETURN VARCHAR2
3982   IS
3983     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
3984     l_dummy           VARCHAR2(1) := OKL_API.G_TRUE;
3985   BEGIN
3986     -- Invoice Number is required:
3987     --   TO DO: When in 'C'reate mode - allow user to omit invoice
3988     --          (funding request) number and generate one automatically,
3989     --          assuring that the invoice number and vendor id
3990     --          combination is unique in OKL_TRX_AP_INVOICES_V (OKL) and
3991     --          in the AP_INVOICES_ALL (AP).
3992 
3993     IF (p_tapv_rec.vendor_invoice_number IS NULL) OR
3994        (p_tapv_rec.vendor_invoice_number = OKL_API.G_MISS_CHAR)
3995     THEN
3996       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
3997                           p_msg_name     => G_REQUIRED_VALUE,
3998                           p_token1       => G_COL_NAME_TOKEN,
3999                           p_token1_value => 'Request Number');
4000       RAISE G_EXCEPTION_HALT_VALIDATION;
4001     END IF;
4002 
4003 -- due to the external interface table limit to 30 chars
4004     IF (length(p_tapv_rec.vendor_invoice_number) > 30)
4005     THEN
4006       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4007                           p_msg_name     => 'OKL_LLA_EXCEED_MAXIMUM_LENGTH',
4008                           p_token1       => 'MAX_CHARS',
4009                           p_token1_value => 'thirty',
4010                           p_token2       => 'COL_NAME',
4011                           p_token2_value => 'Request Number');
4012 
4013       RAISE G_EXCEPTION_HALT_VALIDATION;
4014     END IF;
4015 
4016     RETURN l_return_status;
4017   EXCEPTION
4018     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4019       l_return_status := OKL_API.G_RET_STS_ERROR;
4020       RETURN l_return_status;
4021     WHEN OTHERS THEN
4022       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4023       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4024                           p_msg_name      => G_UNEXPECTED_ERROR,
4025                           p_token1        => G_SQLCODE_TOKEN,
4026                           p_token1_value  => SQLCODE,
4027                           p_token2        => G_SQLERRM_TOKEN,
4028                           p_token2_value  => SQLERRM);
4029       RETURN l_return_status;
4030   END;
4031 
4032   --------------------------------------------------------------------------
4033   ----- Validate vendor site ID
4034   --------------------------------------------------------------------------
4035   FUNCTION validate_ipvs_id(
4036     p_tapv_rec                  IN tapv_rec_type
4037   ) RETURN VARCHAR2
4038   IS
4039     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4040     l_dummy           VARCHAR2(1) := OKL_API.G_TRUE;
4041     l_dum number;
4042     l_rl_found boolean;
4043     l_lv_found boolean;
4044 
4045 cursor c_rl(p_khr_id number) is
4046   select 1
4047 from OKL_K_HEADERS khr
4048 where khr.id = p_khr_id
4049 and   khr.deal_type = 'LOAN-REVOLVING';
4050 
4051 cursor c_lv(p_khr_id number) is
4052   select 1
4053 from okl_fund_vendor_sites_uv vs
4054 where vs.dnz_chr_id = p_khr_id;
4055 
4056 
4057   BEGIN
4058     IF (p_tapv_rec.ipvs_id IS NULL) OR
4059        (p_tapv_rec.ipvs_id = OKL_API.G_MISS_NUM)
4060     THEN
4061 
4062 -- 10-10-2003 cklee fixed bug# 3159723
4063       open c_rl(p_tapv_rec.khr_id);
4064       fetch c_rl into l_dum;
4065       l_rl_found := c_rl%FOUND;
4066       close c_rl;
4067 
4068       IF (l_rl_found) THEN
4069 
4070         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4071                             p_msg_name     => 'OKL_LLA_LESSEE_AS_VENDOR_CHK');
4072       ELSE
4073 
4074         open c_lv(p_tapv_rec.khr_id);
4075         fetch c_lv into l_dum;
4076         l_lv_found := c_lv%FOUND;
4077         close c_lv;
4078 
4079         IF (l_lv_found) THEN
4080 
4081           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4082                             p_msg_name     => G_REQUIRED_VALUE,
4083                             p_token1       => G_COL_NAME_TOKEN,
4084                             p_token1_value => 'Vendor Site');
4085         ELSE
4086 
4087           OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4088                               p_msg_name     => 'OKL_LLA_FUNDING_VENDOR_CHK');
4089 
4090         END IF;
4091       END IF;
4092 
4093 
4094       RAISE G_EXCEPTION_HALT_VALIDATION;
4095     END IF;
4096 
4097     RETURN l_return_status;
4098   EXCEPTION
4099     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4100       l_return_status := OKL_API.G_RET_STS_ERROR;
4101 
4102       RETURN l_return_status;
4103     WHEN OTHERS THEN
4104       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4105       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4106                           p_msg_name      => G_UNEXPECTED_ERROR,
4107                           p_token1        => G_SQLCODE_TOKEN,
4108                           p_token1_value  => SQLCODE,
4109                           p_token2        => G_SQLERRM_TOKEN,
4110                           p_token2_value  => SQLERRM);
4111       RETURN l_return_status;
4112   END;
4113 
4114   --------------------------------------------------------------------------
4115   ----- Validate Funding Type...
4116   --------------------------------------------------------------------------
4117   FUNCTION validate_funding_type(
4118     p_tapv_rec                  IN tapv_rec_type
4119    ,p_mode                            IN VARCHAR2 -- 'C'reate,'U'pdate,'D'elete
4120   ) RETURN VARCHAR2
4121   IS
4122     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4123     l_loan_rev NUMBER := 0;
4124     l_loan_row_found  boolean := false;
4125     l_prefunding_eligible_yn okl_k_headers.PREFUNDING_ELIGIBLE_YN%type;
4126     l_trx_status_code okl_trx_ap_invoices_b.trx_status_code%type;
4127     l_reverse_row_notfound  boolean := false;
4128     l_dummy number;
4129 
4130   CURSOR c_prefund (p_contract_id  NUMBER)
4131   IS
4132   select nvl(khr.PREFUNDING_ELIGIBLE_YN, 'N')
4133   from   OKL_K_HEADERS khr
4134   where  khr.id = p_contract_id
4135 ;
4136 
4137   CURSOR c_curr_trx_sts (p_req_id  NUMBER)
4138   IS
4139   select trx_status_code
4140   from   OKL_TRX_AP_INVOICES_B
4141   where  id = p_req_id
4142 ;
4143 
4144 
4145 -- bug 2604862
4146   CURSOR c_loan_revolving (p_contract_id  NUMBER)
4147   IS
4148   select 1 from OKL_K_HEADERS khr
4149   where khr.id = p_contract_id
4150   and khr.deal_type = 'LOAN-REVOLVING';
4151 
4152 -- cklee 09-24-03
4153   -- sjalasut, modified the below cursor to have khr_id referred from
4154   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b.
4155   -- also not using okl_cnsld_ap_invoices_all as this cursor only checks
4156   -- for a pre-funding request.
4157   Cursor c_reverse_chk(p_contract_id number)
4158   is
4159   select 1
4160   from okl_trx_ap_invoices_b a
4161       ,okl_txl_ap_inv_lns_all_b b
4162   where a.id = b.tap_id
4163   and a.vendor_invoice_number = a.invoice_number
4164   and b.khr_id = p_contract_id;
4165 
4166   BEGIN
4167 
4168 -- bug 2604862
4169     OPEN c_loan_revolving(p_tapv_rec.khr_id);
4170     FETCH c_loan_revolving INTO l_loan_rev;
4171     l_loan_row_found := c_loan_revolving%FOUND;
4172     CLOSE c_loan_revolving;
4173 
4174     -- is loan revolving contract
4175     IF (l_loan_row_found) THEN
4176       IF (p_tapv_rec.funding_type_code NOT IN ('PREFUNDING','BORROWER_PAYMENT', 'MANUAL_DISB') ) THEN
4177 
4178         --Revolving line of credit loan contract are not allowed for TOKEN funding type.
4179         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4180                           p_msg_name      => 'OKL_LLA_REVLOAN_FUNDTYPE_CHK',
4181                           p_token1       => 'COL_NAME',
4182                           p_token1_value => p_tapv_rec.funding_type_code);
4183 
4184         RAISE G_EXCEPTION_HALT_VALIDATION;
4185 
4186       END IF;
4187 
4188 
4189     ELSE -- is not revolvong line of credit loan contract
4190       IF (p_tapv_rec.funding_type_code IN ('BORROWER_PAYMENT') ) THEN
4191 
4192         --Borrower payment funding type is allow for revolving line of credit loan contract only.
4193         OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4194                           p_msg_name      => 'OKL_LLA_FUNDING_TYPE_CHK');
4195         RAISE G_EXCEPTION_HALT_VALIDATION;
4196 
4197       END IF;
4198 
4199     END IF;
4200 -- bug 2604862
4201 
4202     -- funding_type_code is required
4203     IF (p_tapv_rec.funding_type_code IS NULL) OR
4204        (p_tapv_rec.funding_type_code = OKL_API.G_MISS_CHAR)
4205     THEN
4206       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4207                           p_msg_name     => G_REQUIRED_VALUE,
4208                           p_token1       => G_COL_NAME_TOKEN,
4209                           p_token1_value => 'Funding Type');
4210       RAISE G_EXCEPTION_HALT_VALIDATION;
4211     END IF;
4212 
4213 
4214     IF (p_mode = 'U') THEN
4215 
4216       -- get current req status
4217       OPEN c_curr_trx_sts(p_tapv_rec.id);
4218       FETCH c_curr_trx_sts INTO l_trx_status_code;
4219       CLOSE c_curr_trx_sts;
4220 
4221 
4222       -- check when submit for approval
4223       IF (l_trx_status_code = 'ENTERED' AND
4224           p_tapv_rec.trx_status_code in ('SUBMITTED','APPROVED')) THEN
4225 
4226         -- prefunding eligible flag check
4227         OPEN c_prefund(p_tapv_rec.khr_id);
4228         FETCH c_prefund INTO l_prefunding_eligible_yn;
4229         CLOSE c_prefund;
4230 
4231         -- cklee 09-25-2003 added p_tapv_rec.amount > 0
4232         IF ( p_tapv_rec.funding_type_code = 'PREFUNDING' AND p_tapv_rec.amount > 0 AND
4233              l_prefunding_eligible_yn <> 'Y') THEN
4234 
4235           OPEN c_reverse_chk(p_tapv_rec.khr_id);
4236           FETCH c_reverse_chk INTO l_dummy;
4237           l_reverse_row_notfound := c_reverse_chk%NOTFOUND;
4238           CLOSE c_reverse_chk;
4239 
4240           -- CKLEE 02-24-2003 :internal request will have the same value for these 2 columns
4241           IF (l_reverse_row_notfound) THEN
4242             -- You are not allowed to submit pre-funding request if Eligible For Pre-Funding
4243             -- has not been set for this contract.
4244             OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4245                               p_msg_name      => 'OKL_LLA_PREFUND_ELIGIBLE_CHK');
4246             RAISE G_EXCEPTION_HALT_VALIDATION;
4247           END IF;
4248 
4249         END IF;
4250       END IF;
4251     END IF;
4252 
4253     RETURN l_return_status;
4254   EXCEPTION
4255     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4256       l_return_status := OKL_API.G_RET_STS_ERROR;
4257       RETURN l_return_status;
4258     WHEN OTHERS THEN
4259       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4260       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4261                           p_msg_name      => G_UNEXPECTED_ERROR,
4262                           p_token1        => G_SQLCODE_TOKEN,
4263                           p_token1_value  => SQLCODE,
4264                           p_token2        => G_SQLERRM_TOKEN,
4265                           p_token2_value  => SQLERRM);
4266 
4267       RETURN l_return_status;
4268 
4269 
4270   END;
4271 
4272   --------------------------------------------------------------------------
4273   ----- Validate Payment Method...
4274   --------------------------------------------------------------------------
4275   FUNCTION validate_payment_method(
4276     p_tapv_rec                  IN tapv_rec_type
4277   ) RETURN VARCHAR2
4278   IS
4279     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4280   BEGIN
4281     -- payment_method_code is required
4282     IF (p_tapv_rec.payment_method_code IS NULL) OR
4283        (p_tapv_rec.payment_method_code = OKL_API.G_MISS_CHAR)
4284     THEN
4285       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4286                           p_msg_name     => G_REQUIRED_VALUE,
4287                           p_token1       => G_COL_NAME_TOKEN,
4288                           p_token1_value => 'Payment Method');
4289       RAISE G_EXCEPTION_HALT_VALIDATION;
4290     END IF;
4291     RETURN l_return_status;
4292   EXCEPTION
4293     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4294       l_return_status := OKL_API.G_RET_STS_ERROR;
4295       RETURN l_return_status;
4296     WHEN OTHERS THEN
4297       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4298       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4299                           p_msg_name      => G_UNEXPECTED_ERROR,
4300                           p_token1        => G_SQLCODE_TOKEN,
4301                           p_token1_value  => SQLCODE,
4302                           p_token2        => G_SQLERRM_TOKEN,
4303                           p_token2_value  => SQLERRM);
4304       RETURN l_return_status;
4305   END;
4306 
4307   -- cklee 05/04/2004
4308   --------------------------------------------------------------------------
4309   ----- Validate invoice type..
4310   --------------------------------------------------------------------------
4311   FUNCTION validate_invoice_type(
4312     p_tapv_rec                  IN tapv_rec_type
4313   ) RETURN VARCHAR2
4314   IS
4315     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4316     l_funding_type varchar2(80);
4317 
4318   cursor c_funding_type (p_funding_type_code varchar2)
4319     is
4320   select lok.meaning
4321   from fnd_lookups lok
4322   where lok.lookup_type = 'OKL_FUNDING_TYPE'
4323   and lok.lookup_code = p_funding_type_code
4324   ;
4325 
4326   BEGIN
4327 
4328     -- check if value exists
4329     IF (p_tapv_rec.invoice_type IS NOT NULL) AND
4330        (p_tapv_rec.invoice_type <> OKL_API.G_MISS_CHAR)
4331     THEN
4332 
4333 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4334 /*
4335       IF (p_tapv_rec.funding_type_code = G_SUPPLIER_RETENTION_TYPE_CODE and
4336           p_tapv_rec.invoice_type <> G_CREDIT) OR
4337          (p_tapv_rec.funding_type_code NOT IN (G_SUPPLIER_RETENTION_TYPE_CODE, G_MANUAL_DISB) and
4338           p_tapv_rec.invoice_type = G_CREDIT) THEN
4339 */
4340       IF (p_tapv_rec.funding_type_code in (G_SUPPLIER_RETENTION_TYPE_CODE, G_ASSET_SUBSIDY) and
4341           p_tapv_rec.invoice_type <> G_CREDIT) OR
4342          (p_tapv_rec.funding_type_code in (G_EXPENSE, G_ASSET_TYPE_CODE, G_BORROWER_PAYMENT_TYPE_CODE) and
4343           p_tapv_rec.invoice_type <> G_STANDARD) THEN
4344 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4345 
4346         open c_funding_type(p_tapv_rec.funding_type_code);
4347         fetch c_funding_type into l_funding_type;
4348         close c_funding_type;
4349 
4350         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4351                             p_msg_name     => 'OKL_LLA_FUNDING_INVOICE_TYPE',
4352                             p_token1       => 'INVOICE_TYPE',
4353                             p_token1_value => p_tapv_rec.invoice_type,
4354                             p_token2       => 'FUNDING_TYPE',
4355                             p_token2_value => l_funding_type);
4356 
4357         RAISE G_EXCEPTION_HALT_VALIDATION;
4358 
4359       END IF;
4360 
4361     END IF;
4362 
4363     RETURN l_return_status;
4364   EXCEPTION
4365     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4366       l_return_status := OKL_API.G_RET_STS_ERROR;
4367       RETURN l_return_status;
4368     WHEN OTHERS THEN
4369       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4370       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4371                           p_msg_name      => G_UNEXPECTED_ERROR,
4372                           p_token1        => G_SQLCODE_TOKEN,
4373                           p_token1_value  => SQLCODE,
4374                           p_token2        => G_SQLERRM_TOKEN,
4375                           p_token2_value  => SQLERRM);
4376       RETURN l_return_status;
4377   END;
4378 
4379   -------------------------------------------------------------------------
4380   -- validate_invoice_type_and_sign
4381   -- sjalasut, added this function to implement new business validation
4382   -- that when the invoice_type is STANDARD, the invoice amount should be
4383   -- positive and when the invoice_type is CREDIT, invoice amount should be
4384   -- negative.
4385   -------------------------------------------------------------------------
4386   FUNCTION validate_invoice_type_and_sign(p_tapv_rec IN tapv_rec_type
4387                                          ) RETURN VARCHAR2 IS
4388     l_return_status VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4389   BEGIN
4390     -- If invoice type is G_STANDARD then invoice amount is positive
4391     -- If invoice type is G_CREDIT then the invoice amount is negative.
4392     -- sjalasut, made changes to incorporate the business rule as part
4393     -- of OKLR12B Disbursements Project
4394 
4395 --start: cklee 03/01/07 added the following condition, somehow UI has passed
4396 -- wrong data.
4397   IF p_tapv_rec.trx_status_code = 'ENTERED' THEN
4398 --end: cklee 03/01/07 added the following condition, somehow UI has passed
4399 -- wrong data.
4400     IF((p_tapv_rec.INVOICE_TYPE = G_STANDARD AND p_tapv_rec.AMOUNT < 0)
4401        OR(p_tapv_rec.INVOICE_TYPE = G_CREDIT AND p_tapv_rec.AMOUNT > 0))THEN
4402       OKL_API.set_message(
4403                           p_app_name => G_APP_NAME
4404                          ,p_msg_name => 'OKL_LLA_INV_TYPE_AND_SIGN'
4405                          );
4406       RAISE G_EXCEPTION_HALT_VALIDATION;
4407 --start: cklee 03/01/07 added the following condition, somehow UI has passed
4408 -- wrong data.
4409     END IF;
4410 --end: cklee 03/01/07 added the following condition, somehow UI has passed
4411 -- wrong data.
4412   END IF;
4413     RETURN l_return_status;
4414   EXCEPTION
4415     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4416       l_return_status := OKL_API.G_RET_STS_ERROR;
4417       RETURN l_return_status;
4418     WHEN OTHERS THEN
4419       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4420       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4421                           p_msg_name      => G_UNEXPECTED_ERROR,
4422                           p_token1        => G_SQLCODE_TOKEN,
4423                           p_token1_value  => SQLCODE,
4424                           p_token2        => G_SQLERRM_TOKEN,
4425                           p_token2_value  => SQLERRM);
4426       RETURN l_return_status;
4427   END validate_invoice_type_and_sign;
4428 
4429   --------------------------------------------------------------------------
4430   ----- Validate kle_id (contract line id)... Real version by chenkuang.lee
4431   ----- 1) if funding_type_code = 'ASSET'
4432   ----- 2) check required only, OKLSTPLB.pls will check FK for kle_id
4433   --------------------------------------------------------------------------
4434 
4435   FUNCTION validate_kle_id(
4436     p_tplv_rec                 IN tplv_rec_type
4437     ,p_mode                    IN VARCHAR2
4438   ) RETURN VARCHAR2
4439   IS
4440     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4441     l_dummy       VARCHAR2(1) := '?';
4442     l_result VARCHAR2(1) := OKL_API.G_TRUE;
4443 
4444     CURSOR c (p_tap_id NUMBER)
4445     IS
4446       SELECT 'X'
4447         FROM OKL_TRX_AP_INVOICES_B
4448        WHERE id = p_tap_id
4449        AND funding_type_code = 'ASSET'
4450     ;
4451   BEGIN
4452 
4453     OPEN c (p_tplv_rec.tap_id);
4454     FETCH c INTO l_dummy;
4455     CLOSE c;
4456 
4457 
4458     IF (l_dummy = 'X') THEN
4459 
4460       -- kle_id is required:
4461       IF (p_tplv_rec.kle_id IS NULL) OR
4462          (p_tplv_rec.kle_id = OKL_API.G_MISS_NUM)
4463       THEN
4464         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4465                           p_msg_name     => G_REQUIRED_VALUE,
4466                           p_token1       => G_COL_NAME_TOKEN,
4467 --START:| 30-May-2006  cklee -- Fixed bug#5241187                                    |
4468 --                          p_token1_value => 'Contract Top Line'); -- kle_id (contract_line_id) assoc asset number
4469                           p_token1_value => 'Asset Number'); -- kle_id (contract_line_id) assoc asset number
4470 --END:| 30-May-2006  cklee -- Fixed bug#5241187                                    |
4471         RAISE G_EXCEPTION_HALT_VALIDATION;
4472       END IF;
4473 
4474       -- check uniqueness
4475       l_result := is_contract_line_unique(
4476                           p_kle_id   => p_tplv_rec.kle_id
4477                           ,p_fund_id => p_tplv_rec.tap_id
4478 
4479                           ,p_fund_line_id => p_tplv_rec.id
4480                           ,p_mode    => p_mode);
4481       IF (l_result = OKL_API.G_FALSE) THEN
4482         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4483                             p_msg_name     => G_NOT_UNIQUE,
4484                             p_token1       => G_COL_NAME_TOKEN,
4485                             p_token1_value => 'Asset Number');
4486         RAISE G_EXCEPTION_HALT_VALIDATION;
4487       END IF;
4488 
4489     END IF;
4490 
4491     RETURN l_return_status;
4492   EXCEPTION
4493     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4494       l_return_status := OKL_API.G_RET_STS_ERROR;
4495       RETURN l_return_status;
4496     WHEN OTHERS THEN
4497       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4498       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4499                           p_msg_name      => G_UNEXPECTED_ERROR,
4500                           p_token1        => G_SQLCODE_TOKEN,
4501                           p_token1_value  => SQLCODE,
4502                           p_token2        => G_SQLERRM_TOKEN,
4503                           p_token2_value  => SQLERRM);
4504       RETURN l_return_status;
4505   END;
4506   --------------------------------------------------------------------------
4507   ----- Validate kle_id (contract line id)
4508   ----- 1) if funding_type_code = 'ASSET'
4509   ----- 2) check required only, OKLSTPLB.pls will check FK for kle_id
4510   ----- 3) check pl/sql table before check DB, something wrong with the DB
4511   -----    transaction control or some problem with the code logic
4512   --------------------------------------------------------------------------
4513 
4514  FUNCTION validate_table_kle_id(
4515     p_tplv_tbl                 IN tplv_tbl_type
4516 
4517  ) RETURN VARCHAR2
4518   IS
4519     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4520 
4521     l_dummy       VARCHAR2(1) := '?';
4522     l_result VARCHAR2(1) := OKL_API.G_TRUE;
4523 
4524     CURSOR c (p_tap_id NUMBER)
4525 
4526     IS
4527       SELECT 'X'
4528 
4529         FROM OKL_TRX_AP_INVOICES_B
4530        WHERE id = p_tap_id
4531        AND funding_type_code = 'ASSET'
4532     ;
4533   BEGIN
4534 
4535     OPEN c (p_tplv_tbl(p_tplv_tbl.FIRST).tap_id);
4536     FETCH c INTO l_dummy;
4537     CLOSE c;
4538 
4539     IF (l_dummy = 'X') THEN
4540 
4541       -- check uniqueness
4542       l_result := is_kle_id_unique(p_tplv_tbl=>p_tplv_tbl);
4543 
4544       IF (l_result = OKL_API.G_FALSE) THEN
4545 
4546         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4547                             p_msg_name     => G_NOT_UNIQUE,
4548                             p_token1       => G_COL_NAME_TOKEN,
4549                             p_token1_value => 'Asset Number');
4550         RAISE G_EXCEPTION_HALT_VALIDATION;
4551       END IF;
4552 
4553     END IF;
4554 
4555     RETURN l_return_status;
4556   EXCEPTION
4557 
4558     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4559       l_return_status := OKL_API.G_RET_STS_ERROR;
4560       RETURN l_return_status;
4561     WHEN OTHERS THEN
4562       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4563       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4564                           p_msg_name      => G_UNEXPECTED_ERROR,
4565                           p_token1        => G_SQLCODE_TOKEN,
4566                           p_token1_value  => SQLCODE,
4567                           p_token2        => G_SQLERRM_TOKEN,
4568                           p_token2_value  => SQLERRM);
4569 
4570       RETURN l_return_status;
4571   END;
4572   --------------------------------------------------------------------------
4573   ----- Validate Stream Type ID... Real version by chenkuang.lee
4574   ----- 1) if funding_type_code = 'SUPPLIER_RETENTION'
4575   ----- 2) check required only, OKLSTPLB.pls will check FK for sty_id
4576   --------------------------------------------------------------------------
4577 
4578   FUNCTION validate_stream_id(
4579     p_tplv_rec                 IN tplv_rec_type
4580   ) RETURN VARCHAR2
4581   IS
4582     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4583     l_dummy       VARCHAR2(1) := '?';
4584 
4585     CURSOR c (p_tap_id NUMBER)
4586     IS
4587       SELECT 'X'
4588         FROM OKL_TRX_AP_INVOICES_B
4589        WHERE id = p_tap_id
4590        AND funding_type_code in ('SUPPLIER_RETENTION', 'EXPENSE')
4591     ;
4592   BEGIN
4593 
4594     OPEN c (p_tplv_rec.tap_id);
4595     FETCH c INTO l_dummy;
4596     CLOSE c;
4597 
4598     IF (l_dummy = 'X') THEN
4599       -- Stream Type ID is required:
4600       IF (p_tplv_rec.sty_id IS NULL) OR
4601          (p_tplv_rec.sty_id = OKL_API.G_MISS_NUM)
4602       THEN
4603         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4604                           p_msg_name     => G_REQUIRED_VALUE,
4605                           p_token1       => G_COL_NAME_TOKEN,
4606                           p_token1_value => 'Stream Type'); -- sty_id
4607         RAISE G_EXCEPTION_HALT_VALIDATION;
4608       END IF;
4609     END IF;
4610 
4611     RETURN l_return_status;
4612   EXCEPTION
4613     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4614       l_return_status := OKL_API.G_RET_STS_ERROR;
4615       RETURN l_return_status;
4616     WHEN OTHERS THEN
4617       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4618       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4619                           p_msg_name      => G_UNEXPECTED_ERROR,
4620                           p_token1        => G_SQLCODE_TOKEN,
4621                           p_token1_value  => SQLCODE,
4622                           p_token2        => G_SQLERRM_TOKEN,
4623                           p_token2_value  => SQLERRM);
4624       RETURN l_return_status;
4625   END;
4626   --------------------------------------------------------------------------
4627   ----- Validate Funding Line Amount...
4628   --------------------------------------------------------------------------
4629 
4630   FUNCTION validate_line_amount(
4631     p_tplv_rec                  IN tplv_rec_type
4632     ,p_mode                     IN VARCHAR2
4633   ) RETURN VARCHAR2
4634   IS
4635     l_return_status         VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4636 
4637     l_api_version           NUMBER := 1.0;
4638     l_init_msg_list         VARCHAR2(1) := OKL_API.G_FALSE;
4639     x_return_status         VARCHAR2(1);
4640     x_msg_count             NUMBER;
4641     x_msg_data              VARCHAR2(2000);
4642     x_value                 NUMBER := 0;
4643     l_chr_id                NUMBER;
4644     l_funding_type_code     VARCHAR2(30);
4645     l_cur_total_amount      NUMBER := 0;
4646     l_cur_amount            NUMBER := 0;
4647     l_results_amount        NUMBER := 0;
4648     l_message_name          VARCHAR2(30);
4649 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4650 
4651   l_invoice_type okl_trx_ap_invoices_b.invoice_type%type;
4652   cursor c_invoice_type (p_tap_id number)is
4653     select invoice_type
4654     from okl_trx_ap_invoices_b
4655     where id = p_tap_id;
4656 
4657 --end: cklee 3/01/07 added invoice type and amount sign check at line level
4658 
4659   BEGIN
4660     -- line Amount is required: default to 0 @ UI
4661     IF (p_tplv_rec.amount IS NULL) OR
4662        (p_tplv_rec.amount = OKL_API.G_MISS_NUM)
4663     THEN
4664       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4665                           p_msg_name     => G_REQUIRED_VALUE,
4666                           p_token1       => G_COL_NAME_TOKEN,
4667                           p_token1_value => 'Amount');
4668       RAISE G_EXCEPTION_HALT_VALIDATION;
4669     END IF;
4670 --start: cklee 3/01/07 added invoice type and amount sign check at line level
4671     -- If invoice type is G_STANDARD then invoice amount is positive
4672     -- If invoice type is G_CREDIT then the invoice amount is negative.
4673     -- cklee, made changes to incorporate the business rule as part
4674     -- of OKLR12B Disbursements Project
4675     open c_invoice_type(p_tplv_rec.tap_id);
4676     fetch c_invoice_type into l_invoice_type;
4677     close c_invoice_type;
4678 
4679     IF((l_invoice_type = G_STANDARD AND p_tplv_rec.AMOUNT < 0)
4680        OR(l_invoice_type = G_CREDIT AND p_tplv_rec.AMOUNT > 0))THEN
4681       OKL_API.set_message(
4682                           p_app_name => G_APP_NAME
4683                          ,p_msg_name => 'OKL_LLA_INV_TYPE_AND_SIGN'
4684                          );
4685       RAISE G_EXCEPTION_HALT_VALIDATION;
4686     END IF;
4687 --end: cklee 3/01/07 added invoice type and amount sign check at line level
4688 
4689     -- sjalasut, commented the following code as part of OKLR12B disbursements
4690     -- project.
4691     /*
4692     IF (p_tplv_rec.amount < 0 ) THEN
4693 
4694       OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4695                           p_msg_name     => 'OKL_LLA_POSITIVE_AMOUNT_ONLY',
4696                           p_token1       => 'COL_NAME',
4697                           p_token1_value => 'Amount');
4698 
4699 
4700       RAISE G_EXCEPTION_HALT_VALIDATION;
4701     END IF;
4702     */
4703     RETURN l_return_status;
4704   EXCEPTION
4705     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4706       l_return_status := OKL_API.G_RET_STS_ERROR;
4707       RETURN l_return_status;
4708     WHEN OTHERS THEN
4709 
4710 
4711       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4712 
4713       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4714                           p_msg_name      => G_UNEXPECTED_ERROR,
4715                           p_token1        => G_SQLCODE_TOKEN,
4716                           p_token1_value  => SQLCODE,
4717                           p_token2        => G_SQLERRM_TOKEN,
4718                           p_token2_value  => SQLERRM);
4719       RETURN l_return_status;
4720   END;
4721 
4722   --------------------------------------------------------------------------
4723   FUNCTION validate_header_attributes(
4724     p_tapv_rec                        IN tapv_rec_type
4725    ,p_mode                            IN VARCHAR2 -- 'C'reate,'U'pdate,'D'elete
4726 
4727   ) RETURN VARCHAR2
4728   IS
4729     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4730     x_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4731 
4732     l_vendor_id PO_VENDOR_SITES_ALL.vendor_id%TYPE;
4733     l_org_id okc_k_headers_b.AUTHORING_ORG_ID%TYPE;
4734 
4735 
4736 cursor c_vs (p_vendor_site_id number)
4737 is
4738 select vs.vendor_id
4739 from PO_VENDOR_SITES_ALL VS
4740 where vs.vendor_site_id = p_vendor_site_id;
4741 
4742 cursor c_org (p_khr_id number)
4743 is
4744 select chr.AUTHORING_ORG_ID
4745 from okc_k_headers_b chr
4746 where chr.id = p_khr_id;
4747 
4748 
4749   BEGIN
4750 
4751     -- Do formal attribute validation:
4752     l_return_status := validate_trx_status_code(p_tapv_rec);
4753     --- Store the highest degree of error
4754     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4755       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4756         x_return_status := l_return_status;
4757       END IF;
4758       RAISE G_EXCEPTION_HALT_VALIDATION;
4759     END IF;
4760 
4761     -- Do formal attribute validation:
4762     l_return_status := validate_payment_due_date(p_tapv_rec);
4763 
4764     --- Store the highest degree of error
4765     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4766       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4767         x_return_status := l_return_status;
4768       END IF;
4769       RAISE G_EXCEPTION_HALT_VALIDATION;
4770     END IF;
4771 
4772 -- funding checklist enhancement for 11.5.9
4773 -- to be able to copy funding checklist from associated credit line contract, user has to
4774 -- select valid credit line before create a funding request
4775 
4776 --
4777     l_return_status := validate_creditline(p_tapv_rec);
4778     --- Store the highest degree of error
4779     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4780       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4781         x_return_status := l_return_status;
4782       END IF;
4783       RAISE G_EXCEPTION_HALT_VALIDATION;
4784     END IF;
4785 -- funding checklist enhancement for 11.5.9
4786 
4787 -- cklee 01/30/03 check at approved until integrated with WF
4788 -- cklee 06/24/03 WF enable, change check to 'SUBMITTED'
4789     IF (upper(p_mode) = 'U' AND p_tapv_rec.trx_status_code in ('SUBMITTED','APPROVED')) THEN
4790 
4791       l_return_status := validate_chr_status(p_tapv_rec.khr_id);
4792       --- Store the highest degree of error
4793       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4794         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4795           x_return_status := l_return_status;
4796         END IF;
4797         RAISE G_EXCEPTION_HALT_VALIDATION;
4798       END IF;
4799 
4800       l_return_status := validate_header_amount(p_tapv_rec);
4801       --- Store the highest degree of error
4802       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4803         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4804           x_return_status := l_return_status;
4805         END IF;
4806         RAISE G_EXCEPTION_HALT_VALIDATION;
4807       END IF;
4808 
4809     END IF;
4810 
4811     l_return_status := validate_funding_checklist(p_tapv_rec);
4812     --- Store the highest degree of error
4813     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4814       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4815         x_return_status := l_return_status;
4816       END IF;
4817       RAISE G_EXCEPTION_HALT_VALIDATION;
4818     END IF;
4819 
4820 -- bug 2604862
4821     l_return_status := validate_header_amount_for_RL(p_tapv_rec);
4822 
4823     --- Store the highest degree of error
4824     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4825       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4826         x_return_status := l_return_status;
4827       END IF;
4828       RAISE G_EXCEPTION_HALT_VALIDATION;
4829 
4830     END IF;
4831 -- bug 2604862
4832 
4833     IF (p_tapv_rec.FUNDING_TYPE_CODE <> G_ASSET_SUBSIDY) THEN -- cklee 09/17/03
4834       l_return_status := validate_vendor_invoice_number(p_tapv_rec);
4835       --- Store the highest degree of error
4836       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4837         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4838           x_return_status := l_return_status;
4839         END IF;
4840         RAISE G_EXCEPTION_HALT_VALIDATION;
4841       END IF;
4842     END IF;
4843 
4844     l_return_status := validate_ipvs_id(p_tapv_rec);
4845     --- Store the highest degree of error
4846     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4847       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4848         x_return_status := l_return_status;
4849       END IF;
4850       RAISE G_EXCEPTION_HALT_VALIDATION;
4851     END IF;
4852 
4853     l_return_status := validate_funding_type(p_tapv_rec,p_mode);
4854     --- Store the highest degree of error
4855     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4856       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4857         x_return_status := l_return_status;
4858       END IF;
4859       RAISE G_EXCEPTION_HALT_VALIDATION;
4860     END IF;
4861     -- Added for bug 5704212 -- start
4862        l_return_status := validate_release_contract(p_tapv_rec);
4863        --- Store the highest degree of error
4864        IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4865          IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4866            x_return_status := l_return_status;
4867          END IF;
4868          RAISE G_EXCEPTION_HALT_VALIDATION;
4869        END IF;
4870        -- Added for bug 5704212 - End
4871 
4872     l_return_status := validate_payment_method(p_tapv_rec);
4873     --- Store the highest degree of error
4874     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4875       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4876         x_return_status := l_return_status;
4877       END IF;
4878       RAISE G_EXCEPTION_HALT_VALIDATION;
4879     END IF;
4880 
4881     -- 05/04/2004 cklee
4882     l_return_status := validate_invoice_type(p_tapv_rec);
4883     --- Store the highest degree of error
4884     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4885       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4886         x_return_status := l_return_status;
4887       END IF;
4888       RAISE G_EXCEPTION_HALT_VALIDATION;
4889     END IF;
4890 
4891     -- sjalasut, added the function validate_invoice_type_and_sign
4892     -- as part of OKLR12B disbursements project
4893     l_return_status := validate_invoice_type_and_sign(p_tapv_rec);
4894     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4895       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4896         x_return_status := l_return_status;
4897       END IF;
4898       RAISE G_EXCEPTION_HALT_VALIDATION;
4899     END IF;
4900 
4901     -- don't allow to change Request number (invoice number) at update mode
4902     IF (p_mode = 'C') THEN
4903 -- fixed uniqueness check for funding vendor_invoice_number. pass vendor_site_id, org_id
4904       open c_vs(p_tapv_rec.ipvs_id);
4905       fetch c_vs into l_vendor_id;
4906       close c_vs;
4907 
4908       open c_org(p_tapv_rec.khr_id);
4909       fetch c_org into l_org_id;
4910       close c_org;
4911 
4912       l_return_status := is_funding_unique(
4913                           p_vendor_id    => l_vendor_id
4914                           ,p_org_id      => l_org_id
4915                           ,p_fund_number => p_tapv_rec.vendor_invoice_number);
4916 
4917       IF (l_return_status = OKL_API.G_FALSE) THEN
4918         x_return_status := OKL_API.G_RET_STS_ERROR;
4919         OKL_API.Set_Message(p_app_name     => G_APP_NAME,
4920                           p_msg_name     => G_NOT_UNIQUE,
4921                           p_token1       => G_COL_NAME_TOKEN,
4922                           p_token1_value => 'Request Number');
4923 
4924         RAISE G_EXCEPTION_HALT_VALIDATION;
4925       END IF;
4926     END IF;
4927 
4928     RETURN x_return_status;
4929   EXCEPTION
4930     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4931 
4932 
4933       RETURN x_return_status;
4934     WHEN OTHERS THEN
4935       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4936       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4937                           p_msg_name      => G_UNEXPECTED_ERROR,
4938                           p_token1        => G_SQLCODE_TOKEN,
4939                           p_token1_value  => SQLCODE,
4940                           p_token2        => G_SQLERRM_TOKEN,
4941                           p_token2_value  => SQLERRM);
4942 
4943       RETURN l_return_status;
4944   END validate_header_attributes;
4945 
4946   --------------------------------------------------------------------------
4947   FUNCTION validate_line_attributes(
4948     p_tplv_rec                      IN tplv_rec_type
4949    ,p_mode                          IN VARCHAR2 -- 'C'reate,'U'pdate,'D'elete
4950   ) RETURN VARCHAR2
4951   IS
4952     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4953     x_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
4954   BEGIN
4955 
4956     -- Do formal attribute validation:
4957     -- check sty_id
4958     l_return_status := validate_stream_id(p_tplv_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 
4964         x_return_status := l_return_status;
4965       END IF;
4966       RAISE G_EXCEPTION_HALT_VALIDATION; -- 09/2001: one error at a time until Okx/Java can accept more
4967     END IF;
4968 
4969     -- check kle_id
4970     l_return_status := validate_kle_id(p_tplv_rec, p_mode);
4971     --- Store the highest degree of error
4972     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4973       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4974         x_return_status := l_return_status;
4975       END IF;
4976       RAISE G_EXCEPTION_HALT_VALIDATION; -- 09/2001: one error at a time until Okx/Java can accept more
4977     END IF;
4978 
4979     l_return_status := validate_line_amount(p_tplv_rec,p_mode);
4980     --- Store the highest degree of error
4981     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
4982       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
4983         x_return_status := l_return_status;
4984       END IF;
4985       RAISE G_EXCEPTION_HALT_VALIDATION;
4986     END IF;
4987 
4988     RETURN x_return_status;
4989   EXCEPTION
4990     WHEN G_EXCEPTION_HALT_VALIDATION THEN
4991       RETURN x_return_status;
4992     WHEN OTHERS THEN
4993       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
4994       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
4995                           p_msg_name      => G_UNEXPECTED_ERROR,
4996                           p_token1        => G_SQLCODE_TOKEN,
4997                           p_token1_value  => SQLCODE,
4998                           p_token2        => G_SQLERRM_TOKEN,
4999                           p_token2_value  => SQLERRM);
5000       RETURN l_return_status;
5001   END validate_line_attributes;
5002 
5003   --------------------------------------------------------------------------
5004   ----- Populate additional attributes (sty_id) for line
5005   --------------------------------------------------------------------------
5006   FUNCTION populate_sty_id(
5007     p_tplv_rec                  IN OUT NOCOPY tplv_rec_type
5008   ) RETURN VARCHAR2
5009   IS
5010     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5011     l_stream_id           NUMBER;
5012     l_chr_id              NUMBER;
5013     l_name                VARCHAR2(30);
5014     l_funding_type_code okl_trx_ap_invoices_b.funding_type_code%TYPE;
5015 
5016     CURSOR stream_c(p_name VARCHAR2)
5017     IS
5018     SELECT id
5019     FROM  OKL_STRM_TYPE_V
5020     where name = p_name
5021     ;
5022 
5023     -- sjalasut, modified the cursor below to have khr_id referred from
5024     -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b.
5025     -- changes made as part of OKLR12B disbursements project.
5026     --vpanwar added 28/02/2007 for provide khr_id from p_tplv_rec.khr_id  ..start
5027     /*CURSOR c_funding_type (p_tap_id NUMBER)
5028     IS
5029     select tap.funding_type_code , tpl.khr_id
5030     from okl_trx_ap_invoices_b tap
5031         ,okl_txl_ap_inv_lns_all_b tpl
5032     where tap.id = p_tap_id
5033       and tap.id = tpl.tap_id;*/
5034 
5035     CURSOR c_funding_type (p_tap_id NUMBER)
5036     IS
5037     select tap.funding_type_code /*, tpl.khr_id*/
5038     from okl_trx_ap_invoices_b tap
5039         /*,okl_txl_ap_inv_lns_all_b tpl*/
5040     where tap.id = p_tap_id
5041       /*and tap.id = tpl.tap_id*/;
5042     --vpanwar added 28/02/2007 end
5043 
5044   BEGIN
5045 
5046 
5047       OPEN  c_funding_type(p_tplv_rec.tap_id);
5048       FETCH  c_funding_type INTO l_funding_type_code/*, l_chr_id*/;
5049       CLOSE  c_funding_type;
5050 
5051     --vpanwar added 28/02/2007 start
5052       l_chr_id := p_tplv_rec.khr_id;
5053     --vpanwar added 28/02/2007 end
5054 
5055       IF (l_funding_type_code = G_PREFUNDING_TYPE_CODE) THEN
5056         l_name := G_STY_PURPOSE_CODE_PREFUNDING;
5057       ELSIF (l_funding_type_code = G_BORROWER_PAYMENT_TYPE_CODE) THEN
5058         l_name := G_STY_PURPOSE_CODE_P_BALANCE;
5059       ELSE
5060         l_name := G_STY_PURPOSE_CODE_FUNDING;
5061       END IF;
5062 
5063 /*
5064       OPEN  stream_c(l_name);
5065       FETCH  stream_c INTO l_stream_id;
5066       CLOSE  stream_c;
5067 */
5068 
5069 -- cklee: user defined stream changes
5070       IF (l_funding_type_code = G_BORROWER_PAYMENT_TYPE_CODE) THEN
5071 
5072         Okl_Streams_Util.GET_DEPENDENT_STREAM_TYPE(
5073                p_khr_id                => l_chr_id,
5074                p_primary_sty_purpose   => 'RENT',
5075                p_dependent_sty_purpose => l_name,
5076                x_return_status         => l_return_status,
5077                x_dependent_sty_id      => l_stream_id );
5078 
5079       ELSE
5080 
5081         Okl_Streams_Util.GET_PRIMARY_STREAM_TYPE(
5082                p_khr_id              => l_chr_id,
5083                p_primary_sty_purpose => l_name,
5084                x_return_status       => l_return_status,
5085                x_primary_sty_id      => l_stream_id );
5086 
5087       END IF;
5088 --cklee user defined stream type modification
5089 
5090       IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5091         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5092       ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
5093         RAISE OKL_API.G_EXCEPTION_ERROR;
5094       END IF;
5095 
5096       -- only fill if sty_id not exists
5097       IF (l_funding_type_code in (G_PREFUNDING_TYPE_CODE,
5098                                   G_ASSET_TYPE_CODE,
5099                                   G_BORROWER_PAYMENT_TYPE_CODE)) THEN
5100         p_tplv_rec.sty_id := l_stream_id;
5101       END IF;
5102 
5103     RETURN l_return_status;
5104   EXCEPTION
5105 
5106     WHEN G_EXCEPTION_HALT_VALIDATION THEN
5107 
5108       l_return_status := OKL_API.G_RET_STS_ERROR;
5109       RETURN l_return_status;
5110     WHEN OTHERS THEN
5111       l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
5112 
5113       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
5114                           p_msg_name      => G_UNEXPECTED_ERROR,
5115                           p_token1        => G_SQLCODE_TOKEN,
5116                           p_token1_value  => SQLCODE,
5117                           p_token2        => G_SQLERRM_TOKEN,
5118                           p_token2_value  => SQLERRM);
5119       RETURN l_return_status;
5120   END populate_sty_id;
5121 ----------------------------------------------------------------------------
5122 
5123  PROCEDURE SYNC_HEADER_AMOUNT(
5124   p_api_version                  IN NUMBER
5125  ,p_init_msg_list                IN VARCHAR2
5126  ,x_return_status                OUT NOCOPY VARCHAR2
5127  ,x_msg_count                    OUT NOCOPY NUMBER
5128  ,x_msg_data                     OUT NOCOPY VARCHAR2
5129  ,p_tplv_tbl                     IN tplv_tbl_type
5130 )
5131 
5132 IS
5133   l_api_name        CONSTANT VARCHAR2(30) := 'SYNC_HEADER_AMOUNT';
5134   l_api_version     CONSTANT NUMBER       := 1.0;
5135   l_tapv_rec        tapv_rec_type;
5136   x_tapv_rec        tapv_rec_type;
5137   j                 BINARY_INTEGER;
5138   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5139   l_id                     OKL_TRX_AP_INVOICES_B.ID%TYPE;
5140   l_funding_type_code      OKL_TRX_AP_INVOICES_B.funding_type_code%TYPE;
5141   l_VENDOR_INVOICE_NUMBER  OKL_TRX_AP_INVOICES_B.VENDOR_INVOICE_NUMBER%TYPE;
5142   l_PAY_GROUP_LOOKUP_CODE  OKL_TRX_AP_INVOICES_B.PAY_GROUP_LOOKUP_CODE%TYPE;
5143   l_NETTABLE_YN            OKL_TRX_AP_INVOICES_B.NETTABLE_YN%TYPE;
5144   l_amount                 OKL_TRX_AP_INVOICES_B.AMOUNT%TYPE := 0;
5145   l_INVOICE_TYPE           OKL_TRX_AP_INVOICES_B.INVOICE_TYPE%TYPE;
5146 
5147     CURSOR c (p_id NUMBER)
5148     IS
5149       SELECT h.id,
5150              h.funding_type_code,
5151              h.VENDOR_INVOICE_NUMBER,
5152              h.PAY_GROUP_LOOKUP_CODE,
5153              h.NETTABLE_YN,
5154              h.INVOICE_TYPE
5155         FROM OKL_TRX_AP_INVOICES_B h
5156        WHERE h.id = p_id
5157     ;
5158 
5159 BEGIN
5160 
5161     x_return_status      := OKL_API.G_RET_STS_SUCCESS;
5162     -- Call start_activity to create savepoint, check compatibility
5163     -- and initialize message list
5164     x_return_status := OKL_API.START_ACTIVITY (
5165                                l_api_name,
5166                                p_init_msg_list,
5167                                '_PVT',
5168                                x_return_status);
5169     -- Check if activity started successfully
5170     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5171        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5172 
5173     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5174        RAISE OKL_API.G_EXCEPTION_ERROR;
5175     END IF;
5176 --
5177 
5178 --*** Begin API body ****************************************************
5179 
5180     OPEN c (p_tplv_tbl(p_tplv_tbl.FIRST).tap_id);
5181     FETCH c INTO l_id,
5182                  l_funding_type_code,
5183                  l_VENDOR_INVOICE_NUMBER,
5184                  l_PAY_GROUP_LOOKUP_CODE,
5185                  l_NETTABLE_YN,
5186                  l_INVOICE_TYPE;
5187     CLOSE c;
5188 
5189     -- set default
5190     l_amount := OKL_FUNDING_PVT.get_contract_line_funded_amt(l_id, l_funding_type_code);
5191     -- fill in all necessary attributes
5192     l_tapv_rec.id := l_id;
5193     l_tapv_rec.amount:= nvl(l_amount,0);
5194     l_tapv_rec.VENDOR_INVOICE_NUMBER := l_VENDOR_INVOICE_NUMBER;
5195     l_tapv_rec.PAY_GROUP_LOOKUP_CODE := l_PAY_GROUP_LOOKUP_CODE;
5196     l_tapv_rec.NETTABLE_YN := l_NETTABLE_YN;
5197     l_tapv_rec.INVOICE_TYPE := l_INVOICE_TYPE;
5198 
5199     OKL_TRX_AP_INVOICES_PUB.UPDATE_TRX_AP_INVOICES(
5200       p_api_version   => p_api_version,
5201       p_init_msg_list => p_init_msg_list,
5202       x_return_status => x_return_status,
5203       x_msg_count     => x_msg_count,
5204       x_msg_data      => x_msg_data,
5205       p_tapv_rec      => l_tapv_rec,
5206       x_tapv_rec      => x_tapv_rec);
5207 
5208     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5209       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5210     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5211       RAISE OKL_API.G_EXCEPTION_ERROR;
5212     END IF;
5213 
5214     OKL_API.END_ACTIVITY (x_msg_count,
5215                           x_msg_data );
5216   EXCEPTION
5217     WHEN OKL_API.G_EXCEPTION_ERROR THEN
5218     x_return_status := OKL_API.HANDLE_EXCEPTIONS(
5219                                l_api_name,
5220                                G_PKG_NAME,
5221                                'OKL_API.G_RET_STS_ERROR',
5222                                x_msg_count,
5223                                x_msg_data,
5224                                '_PVT');
5225     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5226     x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
5227                               l_api_name,
5228                               G_PKG_NAME,
5229                               'OKL_API.G_RET_STS_UNEXP_ERROR',
5230                               x_msg_count,
5231                               x_msg_data,
5232                               '_PVT');
5233     WHEN OTHERS THEN
5234     x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
5235                               l_api_name,
5236                               G_PKG_NAME,
5237                               'OTHERS',
5238                               x_msg_count,
5239                               x_msg_data,
5240                               '_PVT');
5241   END;
5242 
5243 ----------------------------------------------------------------------------
5244 -- Public Procedures and Functions
5245 ----------------------------------------------------------------------------
5246 
5247 PROCEDURE create_funding_header(
5248   p_api_version                  IN NUMBER
5249  ,p_init_msg_list                IN VARCHAR2
5250  ,x_return_status                OUT NOCOPY VARCHAR2
5251  ,x_msg_count                    OUT NOCOPY NUMBER
5252  ,x_msg_data                     OUT NOCOPY VARCHAR2
5253  ,p_tapv_rec                     IN tapv_rec_type
5254  ,x_tapv_rec                     OUT NOCOPY tapv_rec_type
5255 
5256 )
5257 IS
5258   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_HEADER';
5259   l_api_version     CONSTANT NUMBER       := 1.0;
5260   l_tapv_rec                 tapv_rec_type := p_tapv_rec;
5261   i                          NUMBER;
5262     l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5263     l_dummy VARCHAR2(1) := OKL_API.G_TRUE;
5264     l_try_id NUMBER;
5265     l_org_id NUMBER;
5266    l_try_name OKL_TRX_TYPES_TL.NAME%TYPE;
5267 
5268 --
5269 --
5270   l_tplv_rec        tplv_rec_type;
5271   x_tplv_rec        tplv_rec_type;
5272 --
5273 --
5274 
5275     CURSOR l_tryv_csr(p_try_name varchar2) IS
5276 select TRYB.ID
5277 FROM
5278   OKL_TRX_TYPES_B TRYB,
5279   OKL_TRX_TYPES_TL TRYT
5280 WHERE
5281   TRYB.ID = TRYT.ID and
5282   TRYT.LANGUAGE = 'US' and
5283   TRYT.NAME = p_try_name; -- cklee 05/04/2004
5284 
5285 
5286     CURSOR l_org_id_csr(p_chr_id number) IS
5287       SELECT chr.authoring_org_id
5288       FROM okc_k_headers_b chr
5289       WHERE chr.id = p_chr_id;
5290 
5291 
5292 
5293 
5294   --Added by dpsingh for LE uptake
5295   CURSOR contract_num_csr (p_ctr_id1 NUMBER) IS
5296   SELECT  contract_number
5297   FROM okc_k_headers_b
5298   WHERE id = p_ctr_id1;
5299 
5300   l_cntrct_number          OKC_K_HEADERS_B.CONTRACT_NUMBER%TYPE;
5301   l_legal_entity_id          NUMBER;
5302 
5303 BEGIN
5304   -- Set API savepoint
5305 
5306   SAVEPOINT CREATE_FUNDING_HEADER_PVT;
5307 
5308   -- Check for call compatibility
5309   IF (NOT FND_API.Compatible_API_Call (l_api_version,
5310                                 	   p_api_version,
5311                                 	   l_api_name,
5312                                 	   G_PKG_NAME ))
5313   THEN
5314     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5315   END IF;
5316 
5317   -- Initialize message list if requested
5318   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
5319       FND_MSG_PUB.initialize;
5320 	END IF;
5321 
5322   -- Initialize API status to success
5323   x_return_status := OKL_API.G_RET_STS_SUCCESS;
5324 
5325 
5326 /*** Begin API body ****************************************************/
5327 
5328     IF (p_tapv_rec.funding_type_code = G_MANUAL_DISB) THEN
5329       l_try_name := G_TRANSACTION_DISBURSEMENT;
5330     ELSE
5331       l_try_name := G_TRANSACTION_FUNDING;
5332     END IF;
5333 
5334     open  l_tryv_csr(l_try_name);
5335     fetch l_tryv_csr into l_try_id;
5336     close l_tryv_csr;
5337 
5338 -- force to get try_id
5339     l_tapv_rec.try_id := l_try_id;
5340 
5341 -- 10-10-2003 cklee fixed bug# 3159723
5342     open  l_org_id_csr(l_tapv_rec.khr_id);
5343     fetch l_org_id_csr into l_org_id;
5344     close l_org_id_csr;
5345 
5346     IF (l_tapv_rec.org_id IS NULL) THEN
5347       l_tapv_rec.org_id := l_org_id;
5348     END IF;
5349 
5350     -- populates more attributes for BPD
5351     l_return_status := populate_more_attrs(l_tapv_rec);
5352     --- Store the highest degree of error
5353     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5354       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5355         x_return_status := l_return_status;
5356       END IF;
5357       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5358     END IF;
5359 
5360     l_return_status := validate_header_attributes(l_tapv_rec, 'C');
5361     --- Store the highest degree of error
5362     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5363       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5364         x_return_status := l_return_status;
5365       END IF;
5366       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5367     END IF;
5368     --Added by dpsingh for LE Uptake
5369     l_legal_entity_id  := OKL_LEGAL_ENTITY_UTIL.get_khr_le_id(p_tapv_rec.khr_id) ;
5370     IF  l_legal_entity_id IS NOT NULL THEN
5371        l_tapv_rec.legal_entity_id :=  l_legal_entity_id;
5372     ELSE
5373         -- get the contract number
5374        OPEN contract_num_csr(p_tapv_rec.khr_id);
5375        FETCH contract_num_csr INTO l_cntrct_number;
5376        CLOSE contract_num_csr;
5377 	Okl_Api.set_message(p_app_name     => g_app_name,
5378                              p_msg_name     => 'OKL_LE_NOT_EXIST_CNTRCT',
5379 			     p_token1           =>  'CONTRACT_NUMBER',
5380 			     p_token1_value  =>  l_cntrct_number);
5381          RAISE OKL_API.G_EXCEPTION_ERROR;
5382     END IF;
5383     -- smadhava - Bug#5200033 - Added - Start
5384     -- Round the amount to the System Options setup
5385     l_tapv_rec.AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => l_tapv_rec.AMOUNT
5386                                                         , p_currency_code => l_tapv_rec.CURRENCY_CODE);
5387     -- smadhava - Bug#5200033 - Added - End
5388 
5389 --    OKL_TAP_PVT.insert_row(
5390       OKL_TRX_AP_INVOICES_PUB.INSERT_TRX_AP_INVOICES(
5391       p_api_version   => p_api_version,
5392       p_init_msg_list => p_init_msg_list,
5393       x_return_status => x_return_status,
5394       x_msg_count     => x_msg_count,
5395       x_msg_data      => x_msg_data,
5396       p_tapv_rec      => l_tapv_rec,
5397       x_tapv_rec      => x_tapv_rec);
5398 
5399     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5400       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5401     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5402       RAISE OKL_API.G_EXCEPTION_ERROR;
5403     END IF;
5404 
5405 --
5406 -- create dummy funding line for pre-funding, AP required
5407 --
5408 
5409     IF (l_tapv_rec.funding_type_code in ('PREFUNDING','BORROWER_PAYMENT')) THEN
5410 
5411       l_tplv_rec.inv_distr_line_code := 'ITEM';
5412       l_tplv_rec.tap_id := x_tapv_rec.id;
5413       l_tplv_rec.amount := l_tapv_rec.amount;
5414       l_tplv_rec.org_id := l_tapv_rec.org_id;
5415       l_tplv_rec.line_number := 1;
5416       l_tplv_rec.DISBURSEMENT_BASIS_CODE := 'BILL_DATE';
5417       -- sjalasut, added code to populate khr_id at line level. the khr_id
5418       -- is assumed to be retained at p_tapv_rec level from which the value
5419       -- is derived in this procedure. changes made as part of OKLR12B
5420       -- disbursements project. START code changes
5421       l_tplv_rec.khr_id := l_tapv_rec.khr_id;
5422       -- sjalsut, END code changes
5423 
5424       -- fixed bug#3338910
5425       l_return_status := populate_sty_id(l_tplv_rec);
5426       --- Store the highest degree of error
5427       IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5428         IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5429           x_return_status := l_return_status;
5430         END IF;
5431         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5432       END IF;
5433 
5434 
5435       OKL_TXL_AP_INV_LNS_PUB.INSERT_TXL_AP_INV_LNS(
5436         p_api_version   => p_api_version,
5437         p_init_msg_list => p_init_msg_list,
5438         x_return_status => x_return_status,
5439         x_msg_count     => x_msg_count,
5440         x_msg_data      => x_msg_data,
5441         p_tplv_rec      => l_tplv_rec,
5442         x_tplv_rec      => x_tplv_rec);
5443 
5444       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5445         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5446       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5447         RAISE OKL_API.G_EXCEPTION_ERROR;
5448       END IF;
5449 
5450     END IF;
5451 
5452 --
5453 -- end of create dummy funding line for pre-funding
5454 
5455 --
5456 -- Removed code which creates checklists for funding request, as part of Funding OA Migration.
5457 -- by nikshah
5458 
5459    /*
5460    -- vthiruva, 08/31/2004
5461    -- START, Code change to enable Business Event
5462    */
5463 
5464     --raise the business event for create funding request if
5465     --transaction status code is ENTERED
5466     IF(p_tapv_rec.trx_status_code = 'ENTERED')THEN
5467     	raise_business_event(p_api_version    => p_api_version,
5468                              p_init_msg_list  => p_init_msg_list,
5469 			     x_return_status  => x_return_status,
5470 		  	     x_msg_count      => x_msg_count,
5471 			     x_msg_data       => x_msg_data,
5472 			     p_khr_id         => p_tapv_rec.khr_id,
5473 			     p_id             => x_tapv_rec.id,
5474 			     p_event_name     => G_WF_EVT_FUN_REQ_CREATED);
5475 
5476         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5477 	         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5478         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5479           RAISE OKL_API.G_EXCEPTION_ERROR;
5480         END IF;
5481 
5482     End If;
5483 
5484    /*
5485    -- vthiruva, 08/31/2004
5486    -- END, Code change to enable Business Event
5487    */
5488 
5489 /*** End API body ******************************************************/
5490 
5491   -- Get message count and if count is 1, get message info
5492 	FND_MSG_PUB.Count_And_Get
5493     (p_count          =>      x_msg_count,
5494      p_data           =>      x_msg_data);
5495 
5496 EXCEPTION
5497   WHEN OKL_API.G_EXCEPTION_ERROR THEN
5498     ROLLBACK TO CREATE_FUNDING_HEADER_PVT;
5499     x_return_status := OKL_API.G_RET_STS_ERROR;
5500 
5501     FND_MSG_PUB.Count_And_Get
5502       (p_count         =>      x_msg_count,
5503        p_data          =>      x_msg_data);
5504 
5505   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5506     ROLLBACK TO CREATE_FUNDING_HEADER_PVT;
5507     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
5508     FND_MSG_PUB.Count_And_Get
5509       (p_count         =>      x_msg_count,
5510        p_data          =>      x_msg_data);
5511 
5512   WHEN OTHERS THEN
5513 	ROLLBACK TO CREATE_FUNDING_HEADER_PVT;
5514       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
5515       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
5516                           p_msg_name      => G_UNEXPECTED_ERROR,
5517                           p_token1        => G_SQLCODE_TOKEN,
5518                           p_token1_value  => SQLCODE,
5519                           p_token2        => G_SQLERRM_TOKEN,
5520                           p_token2_value  => SQLERRM);
5521 
5522       FND_MSG_PUB.Count_And_Get
5523         (p_count         =>      x_msg_count,
5524          p_data          =>      x_msg_data);
5525 
5526   END create_funding_header;
5527 
5528 
5529 --------------------------------------------------------------------------
5530 
5531 PROCEDURE update_funding_header(
5532   p_api_version                  IN NUMBER
5533  ,p_init_msg_list                IN VARCHAR2
5534  ,x_return_status                OUT NOCOPY VARCHAR2
5535  ,x_msg_count                    OUT NOCOPY NUMBER
5536  ,x_msg_data                     OUT NOCOPY VARCHAR2
5537  ,p_tapv_rec                     IN tapv_rec_type
5538  ,x_tapv_rec                     OUT NOCOPY tapv_rec_type
5539 )
5540 IS
5541   l_api_name        CONSTANT VARCHAR2(30) := 'UPDATE_FUNDING_HEADER';
5542   l_api_version     CONSTANT NUMBER       := 1.0;
5543   l_tapv_rec                 tapv_rec_type := p_tapv_rec;
5544   i                          NUMBER;
5545 
5546   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5547 
5548   l_row_not_found   boolean := false;
5549   l_dummy           number;
5550   funding_line_id   number;
5551   l_approval_option varchar2(10);
5552 
5553   -- vthiruva, 08/31/2004
5554   -- variables to raise business events
5555   l_event_name            wf_events.name%TYPE := null;
5556   l_raise_business_event  VARCHAR2(1) := OKL_API.G_FALSE;
5557 
5558 cursor c_chklst_exists(p_fund_id varchar2) is
5559 --start modified abhsaxen for performance SQLID 20562448
5560 SELECT 1
5561 FROM OKC_RULES_B RULT
5562 WHERE rult.rule_information_category = 'LAFCLD'
5563 and rult.OBJECT1_ID1 = p_fund_id ;
5564 --end modified abhsaxen for performance SQLID 20562448
5565 
5566 --- vpanwar 21/02/2007 Added
5567   --- to get all the funding lines for the funding header
5568     CURSOR funding_line_csr(p_fund_id number) IS
5569         Select id funding_line_id
5570         from OKL_TXL_AP_INV_LNS_B
5571         Where tap_id = p_fund_id;
5572   --- vpanwar 21/02/2007 End
5573 
5574 BEGIN
5575   -- Set API savepoint
5576   SAVEPOINT UPDATE_FUNDING_HEADER_PVT;
5577 
5578   -- Check for call compatibility
5579   IF (NOT FND_API.Compatible_API_Call (l_api_version,
5580                                 	   p_api_version,
5581                                 	   l_api_name,
5582                                 	   G_PKG_NAME ))
5583   THEN
5584     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5585   END IF;
5586 
5587   -- Initialize message list if requested
5588 
5589   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
5590       FND_MSG_PUB.initialize;
5591 	END IF;
5592 
5593   -- Initialize API status to success
5594   x_return_status := OKL_API.G_RET_STS_SUCCESS;
5595 
5596 
5597 /*** Begin API body ****************************************************/
5598     l_return_status := validate_header_attributes(l_tapv_rec, 'U');
5599     --- Store the highest degree of error
5600     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5601       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5602         x_return_status := l_return_status;
5603       END IF;
5604       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5605     END IF;
5606 
5607     ------------------------------------------------------------------
5608     -- added for approval process
5609     ------------------------------------------------------------------
5610     l_approval_option := fnd_profile.value('OKL_LEASE_FUNDING_APPROVAL_PROCESS');
5611     IF (l_tapv_rec.trx_status_code = 'SUBMITTED' AND
5612         l_approval_option not in ('WF', 'AME')) THEN
5613 
5614       /*
5615       -- cklee, 12/21/2004
5616       -- START, Code change to enable Business Event bug#4901292
5617       */
5618 
5619       --raise the business event for Validated the Funding Request for checklist items
5620       raise_business_event(p_api_version    => p_api_version,
5621                          p_init_msg_list  => p_init_msg_list,
5622           			     x_return_status  => x_return_status,
5623            		  	     x_msg_count      => x_msg_count,
5624            			     x_msg_data       => x_msg_data,
5625            			     p_khr_id         => l_tapv_rec.khr_id,
5626 		                 p_id             => l_tapv_rec.id,
5627            			     p_event_name     => G_WF_EVT_FUN_LIST_VALIDATED);
5628 
5629       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5630         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5631       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5632         RAISE OKL_API.G_EXCEPTION_ERROR;
5633       END IF;
5634 
5635       /*
5636       -- cklee, 12/21/2004
5637       -- END, Code change to enable Business Event
5638       */
5639 
5640       -- update item function validation results
5641       update_checklist_function(
5642         p_api_version    => p_api_version,
5643         p_init_msg_list  => p_init_msg_list,
5644         x_return_status  => x_return_status,
5645         x_msg_count      => x_msg_count,
5646         x_msg_data       => x_msg_data,
5647         p_fund_req_id    => l_tapv_rec.id);
5648 
5649       If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
5650          raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
5651       Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
5652          raise OKC_API.G_EXCEPTION_ERROR;
5653       End If;
5654 
5655       l_tapv_rec.trx_status_code := 'APPROVED';
5656       l_tapv_rec.DATE_FUNDING_APPROVED := sysdate;
5657 
5658     END IF;
5659 
5660     -- smadhava - Bug#5200033 - Added - Start
5661     -- Round the amount to the System Options setup
5662     l_tapv_rec.AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => l_tapv_rec.AMOUNT
5663                                                         , p_currency_code => l_tapv_rec.CURRENCY_CODE);
5664     -- smadhava - Bug#5200033 - Added - End
5665 
5666 --    OKL_TAP_PVT.update_row(
5667       OKL_TRX_AP_INVOICES_PUB.UPDATE_TRX_AP_INVOICES(
5668       p_api_version   => p_api_version,
5669       p_init_msg_list => p_init_msg_list,
5670       x_return_status => x_return_status,
5671       x_msg_count     => x_msg_count,
5672       x_msg_data      => x_msg_data,
5673       p_tapv_rec      => l_tapv_rec,
5674       x_tapv_rec      => x_tapv_rec);
5675 
5676 
5677     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5678       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5679     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5680       RAISE OKL_API.G_EXCEPTION_ERROR;
5681     END IF;
5682 
5683     -----------------------------------------------------------
5684     -- trigger WF event if l_tapv_rec.trx_status_code = 'SUBMITTED' and
5685     -- profile option is WF or AME
5686     -----------------------------------------------------------
5687     IF (l_tapv_rec.trx_status_code = 'SUBMITTED' AND
5688         l_approval_option in ('WF', 'AME')) THEN
5689 
5690       /*
5691       -- cklee, 12/21/2004
5692       -- START, Code change to enable Business Event bug#4901292
5693       */
5694 
5695       --raise the business event for Validated the Funding Request for checklist items
5696       raise_business_event(p_api_version    => p_api_version,
5697                          p_init_msg_list  => p_init_msg_list,
5698           			     x_return_status  => x_return_status,
5699            		  	     x_msg_count      => x_msg_count,
5700            			     x_msg_data       => x_msg_data,
5701            			     p_khr_id         => l_tapv_rec.khr_id,
5702 		                 p_id             => l_tapv_rec.id,
5703            			     p_event_name     => G_WF_EVT_FUN_LIST_VALIDATED);
5704 
5705       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5706         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5707       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5708         RAISE OKL_API.G_EXCEPTION_ERROR;
5709       END IF;
5710 
5711       /*
5712       -- cklee, 12/21/2004
5713       -- END, Code change to enable Business Event
5714       */
5715 
5716        -- update item function validation results
5717       update_checklist_function(
5718         p_api_version    => p_api_version,
5719         p_init_msg_list  => p_init_msg_list,
5720         x_return_status  => x_return_status,
5721         x_msg_count      => x_msg_count,
5722         x_msg_data       => x_msg_data,
5723         p_fund_req_id    => l_tapv_rec.id);
5724 
5725       If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
5726          raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
5727       Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
5728          raise OKC_API.G_EXCEPTION_ERROR;
5729       End If;
5730 
5731      OKL_FUNDING_WF.raise_approval_event(
5732                            p_api_version   => p_api_version,
5733                            p_init_msg_list => p_init_msg_list,
5734                            x_return_status => x_return_status,
5735                            x_msg_count     => x_msg_count,
5736                            x_msg_data      => x_msg_data,
5737                            p_funding_id    => l_tapv_rec.id);
5738 
5739     -----------------------------------------------------------
5740     -- trigger post activities if l_tapv_rec.trx_status_code = 'APPROVED' and
5741     -- profile option is NOT WF or AME
5742     -----------------------------------------------------------
5743     ELSIF (l_tapv_rec.trx_status_code = 'APPROVED' AND
5744         l_approval_option not in ('WF', 'AME')) THEN
5745 
5746 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5747 /*    --- vpanwar 21/02/2007 Added
5748     OPEN funding_line_csr(l_tapv_rec.id);
5749     LOOP
5750     FETCH funding_line_csr into funding_line_id;
5751 
5752     EXIT WHEN funding_line_csr%NOTFOUND;
5753 */
5754 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5755 
5756       CREATE_ACCOUNTING_DIST(p_api_version   => p_api_version,
5757                            p_init_msg_list => p_init_msg_list,
5758                            x_return_status => x_return_status,
5759                            x_msg_count     => x_msg_count,
5760                            x_msg_data      => x_msg_data,
5761                            p_status        => l_tapv_rec.trx_status_code,
5762                            p_fund_id       => l_tapv_rec.id);--,
5763 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5764 --                           p_fund_line_id  => funding_line_id);
5765 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5766 
5767       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5768         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5769       ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
5770         RAISE OKL_API.G_EXCEPTION_ERROR;
5771       END IF;
5772 
5773 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5774 --    END LOOP;
5775 --    CLOSE funding_line_csr;
5776 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
5777     --- vpanwar 21/02/2007 End
5778 
5779     -------------------------------------------------------------
5780       -- create subsidy entries for 11.5.10
5781       -------------------------------------------------------------
5782       IF (l_tapv_rec.FUNDING_TYPE_CODE = OKL_FUNDING_PVT.G_ASSET_TYPE_CODE) THEN
5783 
5784         create_fund_asset_subsidies(p_api_version   => p_api_version,
5785                            p_init_msg_list => p_init_msg_list,
5786                            x_return_status => x_return_status,
5787                            x_msg_count     => x_msg_count,
5788                            x_msg_data      => x_msg_data,
5789                            p_status        => l_tapv_rec.trx_status_code,
5790                            p_fund_id       => l_tapv_rec.id);
5791 
5792         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5793           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5794         ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
5795           RAISE OKL_API.G_EXCEPTION_ERROR;
5796         END IF;
5797       END IF;
5798     END IF;
5799 
5800 -- funding request checklist enhancement for 11.5.9
5801   IF (l_tapv_rec.funding_type_code <> G_ASSET_SUBSIDY AND
5802       l_tapv_rec.trx_status_code = 'ENTERED') THEN
5803 --start modified abhsaxen changing l_tapv_rec.id in VARCHAR2 for Performance
5804     open c_chklst_exists(TO_CHAR(l_tapv_rec.id));
5805     fetch c_chklst_exists into l_dummy;
5806     l_row_not_found := c_chklst_exists%NOTFOUND;
5807     close c_chklst_exists;
5808 
5809     IF (l_row_not_found) THEN
5810 
5811       create_funding_chklst_tpl(
5812         p_api_version   => p_api_version,
5813         p_init_msg_list => p_init_msg_list,
5814         x_return_status => x_return_status,
5815         x_msg_count     => x_msg_count,
5816         x_msg_data      => x_msg_data,
5817         p_chr_id        => l_tapv_rec.khr_id,
5818         p_fund_req_id   => l_tapv_rec.id);
5819 
5820       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5821         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5822       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5823         RAISE OKL_API.G_EXCEPTION_ERROR;
5824       END IF;
5825     END IF;
5826 
5827   END IF;
5828 
5829 -- funding request checklist enhancement for 11.5.9
5830 
5831    /*
5832    -- vthiruva, 08/31/2004
5833    -- START, Code change to enable Business Event
5834    */
5835     IF(p_tapv_rec.trx_status_code = 'ENTERED')THEN
5836     --raise the business event for update funding request
5837     --if transaction status code is ENTERED
5838         l_event_name  := G_WF_EVT_FUN_REQ_UPDATED;
5839         l_raise_business_event := OKL_API.G_TRUE;
5840 
5841     ELSIF(p_tapv_rec.trx_status_code = 'CANCELED')THEN
5842     --raise the business event for cancel funding request
5843     --if transaction status code is CANCELED
5844     --In DO the value of trx_status_code is being set to CANCELED
5845     --in place of CANCELLED. Hence changed the spelling in the above check.
5846         l_event_name  := G_WF_EVT_FUN_REQ_CANCELLED;
5847         l_raise_business_event := OKL_API.G_TRUE;
5848 
5849     ELSIF(p_tapv_rec.trx_status_code = 'SUBMITTED')THEN
5850     --raise the business event for submit funding request
5851     --if transaction status code is SUBMITTED
5852         l_event_name  := G_WF_EVT_FUN_REQ_SUBMITTED;
5853         l_raise_business_event := OKL_API.G_TRUE;
5854     END If;
5855 
5856     IF(l_raise_business_event = OKL_API.G_TRUE AND l_event_name IS NOT NULL) THEN
5857         --call to raise the appropriate business event
5858     	raise_business_event(p_api_version    => p_api_version,
5859                              p_init_msg_list  => p_init_msg_list,
5860 			     x_return_status  => x_return_status,
5861 			     x_msg_count      => x_msg_count,
5862 			     x_msg_data       => x_msg_data,
5863 			     p_khr_id         => p_tapv_rec.khr_id,
5864 			     p_id             => p_tapv_rec.id,
5865 			     p_event_name     => l_event_name);
5866 
5867         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5868            RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5869         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
5870            RAISE OKL_API.G_EXCEPTION_ERROR;
5871         END IF;
5872     END IF;
5873 
5874    /*
5875    -- vthiruva, 08/31/2004
5876    -- END, Code change to enable Business Event
5877    */
5878 
5879 /*** End API body ******************************************************/
5880 
5881   -- Get message count and if count is 1, get message info
5882 	FND_MSG_PUB.Count_And_Get
5883     (p_count          =>      x_msg_count,
5884      p_data           =>      x_msg_data);
5885 
5886 EXCEPTION
5887   WHEN OKL_API.G_EXCEPTION_ERROR THEN
5888     ROLLBACK TO UPDATE_FUNDING_HEADER_PVT;
5889     x_return_status := OKL_API.G_RET_STS_ERROR;
5890     FND_MSG_PUB.Count_And_Get
5891       (p_count         =>      x_msg_count,
5892        p_data          =>      x_msg_data);
5893 
5894   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
5895     ROLLBACK TO UPDATE_FUNDING_HEADER_PVT;
5896     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
5897     FND_MSG_PUB.Count_And_Get
5898       (p_count         =>      x_msg_count,
5899        p_data          =>      x_msg_data);
5900 
5901   WHEN OTHERS THEN
5902 	ROLLBACK TO UPDATE_FUNDING_HEADER_PVT;
5903       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
5904       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
5905                           p_msg_name      => G_UNEXPECTED_ERROR,
5906                           p_token1        => G_SQLCODE_TOKEN,
5907                           p_token1_value  => SQLCODE,
5908                           p_token2        => G_SQLERRM_TOKEN,
5909                           p_token2_value  => SQLERRM);
5910       FND_MSG_PUB.Count_And_Get
5911         (p_count         =>      x_msg_count,
5912          p_data          =>      x_msg_data);
5913 
5914   END update_funding_header;
5915 
5916 ----------------------------------------------------------------------------
5917 
5918 PROCEDURE create_funding_lines(
5919   p_api_version                  IN NUMBER
5920  ,p_init_msg_list                IN VARCHAR2
5921  ,x_return_status                OUT NOCOPY VARCHAR2
5922  ,x_msg_count                    OUT NOCOPY NUMBER
5923  ,x_msg_data                     OUT NOCOPY VARCHAR2
5924  ,p_tplv_tbl                     IN tplv_tbl_type
5925  ,x_tplv_tbl                     OUT NOCOPY tplv_tbl_type
5926 )
5927 IS
5928   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_LINES';
5929 
5930   l_api_version     CONSTANT NUMBER       := 1.0;
5931   l_tplv_tbl                 tplv_tbl_type := p_tplv_tbl;
5932   i                          NUMBER;
5933   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
5934 
5935   -- smadhava - Bug#5200033 - Added - Start
5936   l_currency_code OKL_TRX_AP_INVOICES_B.CURRENCY_CODE%TYPE;
5937 
5938    --- vpanwar added 28/02/2007 start
5939   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
5940   --- vpanwar added 28/02/2007 end
5941 
5942   -- Cursor to get the currency code from the header record
5943   CURSOR c_get_currency_code(cp_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
5944     SELECT CURRENCY_CODE
5945       FROM OKL_TRX_AP_INVOICES_B
5946      WHERE ID = cp_tap_id;
5947   -- smadhava - Bug#5200033 - Added - End
5948 
5949   -- vpanwar Added --28/02/2007 -start
5950     CURSOR c_get_khr_id(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
5951     SELECT KHR_ID
5952       FROM OKL_TRX_AP_INVOICES_B
5953      WHERE ID = p_tap_id;
5954   -- vpanwar Added --28/02/2007 -end
5955 
5956   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
5957   CURSOR c_get_max_line_number(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
5958     SELECT MAX(LINE_NUMBER)
5959 	    FROM OKL_TXL_AP_INV_LNS_B
5960 	    WHERE TAP_ID = P_TAP_ID;
5961 
5962   l_max_line_number NUMBER := 0;
5963   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
5964 
5965 BEGIN
5966   -- Set API savepoint
5967   SAVEPOINT CREATE_FUNDING_LINES_PVT;
5968 
5969   -- Check for call compatibility
5970   IF (NOT FND_API.Compatible_API_Call (l_api_version,
5971                                 	   p_api_version,
5972                                 	   l_api_name,
5973                                 	   G_PKG_NAME ))
5974   THEN
5975     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5976   END IF;
5977 
5978   -- Initialize message list if requested
5979   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
5980       FND_MSG_PUB.initialize;
5981 	END IF;
5982 
5983   -- Initialize API status to success
5984   x_return_status := OKL_API.G_RET_STS_SUCCESS;
5985 
5986 
5987 
5988 /*** Begin API body ****************************************************/
5989 
5990 
5991     -- OKL_TPL_PVT is not belongs to our dev team, so we have to write valid code here
5992     -- validate kle_id for passed in pl/sql table before insert into DB
5993     l_return_status := validate_table_kle_id(p_tplv_tbl);
5994     IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
5995       IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
5996         x_return_status := l_return_status;
5997       END IF;
5998       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5999     END IF;
6000 
6001     -- check kle_id and sty_id if applicable
6002     IF (p_tplv_tbl.COUNT > 0) THEN
6003       i := p_tplv_tbl.FIRST;
6004 
6005     -- smadhava - Bug#5200033 - Added - Start
6006     -- get the currency code from the header record
6007     OPEN c_get_currency_code(p_tplv_tbl(i).TAP_ID);
6008       FETCH c_get_currency_code INTO l_currency_code;
6009     CLOSE c_get_currency_code;
6010     -- smadhava - Bug#5200033 - Added - End
6011 
6012   --- vpanwar added 28/02/2007 start
6013     OPEN c_get_khr_id(p_tplv_tbl(i).TAP_ID);
6014       FETCH  c_get_khr_id INTO l_khr_id;
6015     CLOSE c_get_khr_id;
6016   --- vpanwar added 28/02/2007 end
6017 
6018   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
6019     OPEN c_get_max_line_number(p_tplv_tbl(i).TAP_ID);
6020 	FETCH c_get_max_line_number INTO l_max_line_number;
6021     CLOSE c_get_max_line_number;
6022 
6023     IF (l_max_line_number IS NULL) THEN
6024 	l_max_line_number := 0;
6025     END IF;
6026 
6027   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - end
6028 
6029 
6030       LOOP
6031 
6032      --- vpanwar added 28/02/2007 start
6033         l_tplv_tbl(i).KHR_ID := l_khr_id;
6034      --- vpanwar added 28/02/2007 start
6035 
6036 
6037 --
6038 -- default DISBURSEMENT_BASIS_CODE = 'BILL_DATE';
6039 --
6040         l_tplv_tbl(i).DISBURSEMENT_BASIS_CODE := 'BILL_DATE';
6041 --
6042 --
6043 --
6044   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - start
6045 	IF (l_tplv_tbl(i).line_number = OKL_API.G_MISS_NUM OR l_tplv_tbl(i).line_number IS NULL) THEN
6046 		l_max_line_number	:=	l_max_line_number + 1;
6047 		l_tplv_tbl(i).line_number	:=	l_max_line_number;
6048 	END IF;
6049   -- dcshanmu added - 23-Nov-2007 - bug # 6639928 - end
6050 
6051         -- fixed bug#3338910
6052         l_return_status := populate_sty_id(l_tplv_tbl(i));
6053 
6054         IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
6055           IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6056             x_return_status := l_return_status;
6057           END IF;
6058           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6059         END IF;
6060 
6061 
6062         l_return_status := validate_line_attributes(p_tplv_tbl(i), 'C');
6063 
6064         IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6065           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6066         ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
6067           RAISE OKL_API.G_EXCEPTION_ERROR;
6068         END IF;
6069 
6070     -- smadhava - Bug#5200033 - Added - Start
6071     -- Round the amount to the System Options setup
6072     l_tplv_tbl(i).AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => p_tplv_tbl(i).AMOUNT
6073                                                            , p_currency_code => l_currency_code);
6074     -- smadhava - Bug#5200033 - Added - End
6075 
6076 
6077         EXIT WHEN (i = p_tplv_tbl.LAST);
6078         i := p_tplv_tbl.NEXT(i);
6079 
6080       END LOOP;
6081     END IF;
6082 
6083 --    OKL_TPL_PVT.insert_row(
6084       OKL_TXL_AP_INV_LNS_PUB.INSERT_TXL_AP_INV_LNS(
6085       p_api_version   => p_api_version,
6086       p_init_msg_list => p_init_msg_list,
6087       x_return_status => x_return_status,
6088       x_msg_count     => x_msg_count,
6089       x_msg_data      => x_msg_data,
6090       p_tplv_tbl      => l_tplv_tbl,
6091       x_tplv_tbl      => x_tplv_tbl);
6092 
6093     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6094       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6095     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6096       RAISE OKL_API.G_EXCEPTION_ERROR;
6097     END IF;
6098 
6099 
6100 -- sync funding header amount
6101     SYNC_HEADER_AMOUNT(
6102       p_api_version   => p_api_version,
6103       p_init_msg_list => p_init_msg_list,
6104       x_return_status => x_return_status,
6105       x_msg_count     => x_msg_count,
6106       x_msg_data      => x_msg_data,
6107       p_tplv_tbl      => x_tplv_tbl);
6108 
6109     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6110       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6111     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6112       RAISE OKL_API.G_EXCEPTION_ERROR;
6113     END IF;
6114 
6115 
6116 /*** End API body ******************************************************/
6117 
6118   -- Get message count and if count is 1, get message info
6119 	FND_MSG_PUB.Count_And_Get
6120     (p_count          =>      x_msg_count,
6121      p_data           =>      x_msg_data);
6122 
6123 EXCEPTION
6124   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6125 
6126     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6127     x_return_status := OKL_API.G_RET_STS_ERROR;
6128     FND_MSG_PUB.Count_And_Get
6129       (p_count         =>      x_msg_count,
6130        p_data          =>      x_msg_data);
6131 
6132 
6133   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6134     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6135     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6136     FND_MSG_PUB.Count_And_Get
6137       (p_count         =>      x_msg_count,
6138        p_data          =>      x_msg_data);
6139 
6140   WHEN OTHERS THEN
6141 	ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6142       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6143       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6144                           p_msg_name      => G_UNEXPECTED_ERROR,
6145                           p_token1        => G_SQLCODE_TOKEN,
6146                           p_token1_value  => SQLCODE,
6147                           p_token2        => G_SQLERRM_TOKEN,
6148                           p_token2_value  => SQLERRM);
6149       FND_MSG_PUB.Count_And_Get
6150         (p_count         =>      x_msg_count,
6151          p_data          =>      x_msg_data);
6152 
6153   END create_funding_lines;
6154 
6155 ----------------------------------------------------------------------------
6156 
6157 ----------------------------------------------------------------------------
6158 -- dcshanmu - Added - Qucik Fund performance fix - start
6159 ----------------------------------------------------------------------------
6160 PROCEDURE create_funding_lines(
6161   p_api_version                  IN NUMBER
6162  ,p_init_msg_list                IN VARCHAR2
6163  ,x_return_status                OUT NOCOPY VARCHAR2
6164  ,x_msg_count                    OUT NOCOPY NUMBER
6165  ,x_msg_data                     OUT NOCOPY VARCHAR2
6166  ,p_hdr_id				IN NUMBER
6167  ,p_khr_id				IN NUMBER
6168  ,p_vendor_site_id		IN NUMBER
6169  ,x_tplv_tbl                     OUT NOCOPY tplv_tbl_type
6170 )
6171 IS
6172   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_LINES';
6173 
6174   l_api_version     CONSTANT NUMBER       := 1.0;
6175   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6176   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
6177   l_vendor_site_id OKL_TRX_AP_INVOICES_B.IPVS_ID%TYPE;
6178   l_vendor_id OKL_TRX_AP_INVOICES_B.VENDOR_ID%TYPE;
6179 
6180   -- cursor to fetch assets, which has supplier invoice for a given khr_id and vendor_site_id
6181   CURSOR c_get_assets(p_vendor_site_id OKL_TRX_AP_INVOICES_B.IPVS_ID%TYPE,
6182 	p_khr_id OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE) IS
6183     SELECT a.cle_id cle_id,
6184            a.chr_id chr_id,
6185            a.asset_number kle_num,
6186            a.description kle_name,
6187            okl_funding_pvt.get_contract_line_amt(
6188              a.chr_id,
6189              a.cle_id,
6190              p_vendor_site_id
6191            ) kle_amt
6192       FROM okl_assets_lov_uv a,
6193            okc_k_party_roles_b cpl,
6194            okc_k_lines_b LN,
6195            okx_vendor_sites_v sites
6196      WHERE a.chr_id = p_khr_id
6197        AND cpl.rle_code = 'OKL_VENDOR'
6198        AND cpl.chr_id IS NULL
6199        AND cpl.dnz_chr_id = a.chr_id
6200        AND cpl.object1_id1 = TO_CHAR(sites.vendor_id)
6201        AND sites.id1 = p_vendor_site_id
6202        AND cpl.object1_id2 = '#'
6203        AND cpl.cle_id = LN.ID
6204        AND LN.cle_id = a.cle_id;
6205 
6206   -- cursor to fetch org_id from khr_id
6207   CURSOR c_get_org_id(p_khr_id OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE) IS
6208 	SELECT AUTHORING_ORG_ID
6209 	FROM OKC_K_HEADERS_ALL_B
6210 	WHERE ID = p_khr_id;
6211 
6212 assets_rec c_get_assets%ROWTYPE;
6213 TYPE assets_tbl IS TABLE OF assets_rec%TYPE INDEX BY BINARY_INTEGER;
6214 
6215   l_assets_tbl assets_tbl;
6216   l_sty_id	NUMBER := 0;
6217   l_tplv_tbl                 tplv_tbl_type;
6218   cnt	NUMBER := 0;
6219   l_org_id	NUMBER := 0;
6220 
6221  BEGIN
6222 	SAVEPOINT CREATE_FUNDING_LINES_PVT;
6223 
6224 	x_return_status := OKL_API.G_RET_STS_SUCCESS;
6225 
6226 	OPEN c_get_assets(p_vendor_site_id, p_khr_id);
6227 		FETCH c_get_assets BULK COLLECT INTO l_assets_tbl;
6228 	CLOSE c_get_assets;
6229 
6230 	OPEN c_get_org_id(p_khr_id);
6231 		FETCH c_get_org_id INTO l_org_id;
6232 	CLOSE c_get_org_id;
6233 
6234 	IF l_assets_tbl.COUNT > 0 THEN
6235 		-- populate l_tplv_tbl
6236 		FOR i in l_assets_tbl.FIRST..l_assets_tbl.LAST LOOP
6237 			-- increment count and assign to line numbers
6238 			l_tplv_tbl(i).tap_id			:=	p_hdr_id;
6239 			cnt := cnt + 1;
6240 			l_tplv_tbl(i).line_number		:=	cnt;
6241 			l_tplv_tbl(i).kle_id			:=	l_assets_tbl(i).cle_id;
6242 			l_tplv_tbl(i).inv_distr_line_code	:=	'ITEM';
6243 			l_tplv_tbl(i).amount			:=	l_assets_tbl(i).kle_amt;
6244 			l_tplv_tbl(i).org_id			:=	l_org_id;
6245 			l_tplv_tbl(i).description			:=	l_assets_tbl(i).kle_name;
6246 		END LOOP;
6247 
6248 		-- call create_funding_lines proc with table
6249 		create_funding_lines(
6250 			  p_api_version
6251 			 ,p_init_msg_list
6252 			 ,x_return_status
6253 			 ,x_msg_count
6254 			 ,x_msg_data
6255 			 ,l_tplv_tbl
6256 			 ,x_tplv_tbl);
6257 	END IF;
6258 
6259 
6260 	IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6261 		RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6262 	ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6263 		RAISE OKL_API.G_EXCEPTION_ERROR;
6264 	END IF;
6265 
6266   -- Get message count and if count is 1, get message info
6267   FND_MSG_PUB.Count_And_Get
6268     (p_count          =>      x_msg_count,
6269      p_data           =>      x_msg_data);
6270 
6271 EXCEPTION
6272   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6273 
6274     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6275     x_return_status := OKL_API.G_RET_STS_ERROR;
6276     FND_MSG_PUB.Count_And_Get
6277       (p_count         =>      x_msg_count,
6278        p_data          =>      x_msg_data);
6279 
6280 
6281   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6282     ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6283     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6284     FND_MSG_PUB.Count_And_Get
6285       (p_count         =>      x_msg_count,
6286        p_data          =>      x_msg_data);
6287 
6288   WHEN OTHERS THEN
6289 	ROLLBACK TO CREATE_FUNDING_LINES_PVT;
6290       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6291       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6292                           p_msg_name      => G_UNEXPECTED_ERROR,
6293                           p_token1        => G_SQLCODE_TOKEN,
6294                           p_token1_value  => SQLCODE,
6295                           p_token2        => G_SQLERRM_TOKEN,
6296                           p_token2_value  => SQLERRM);
6297       FND_MSG_PUB.Count_And_Get
6298         (p_count         =>      x_msg_count,
6299          p_data          =>      x_msg_data);
6300 
6301 
6302   END create_funding_lines;
6303 ----------------------------------------------------------------------------
6304 -- dcshanmu - Added - Qucik Fund performance fix - end
6305 ----------------------------------------------------------------------------
6306 
6307 PROCEDURE update_funding_lines(
6308   p_api_version                  IN NUMBER
6309  ,p_init_msg_list                IN VARCHAR2
6310  ,x_return_status                OUT NOCOPY VARCHAR2
6311  ,x_msg_count                    OUT NOCOPY NUMBER
6312  ,x_msg_data                     OUT NOCOPY VARCHAR2
6313  ,p_tplv_tbl                     IN tplv_tbl_type
6314  ,x_tplv_tbl                     OUT NOCOPY tplv_tbl_type
6315 )
6316 IS
6317   l_api_name        CONSTANT VARCHAR2(30) := 'UPDATE_FUNDING_LINES';
6318   l_api_version     CONSTANT NUMBER       := 1.0;
6319   l_tplv_tbl                 tplv_tbl_type := p_tplv_tbl;
6320   i                          NUMBER;
6321   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6322   l_sty_id number;
6323 
6324   --- vpanwar added 28/02/2007 start
6325   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
6326   --- vpanwar added 28/02/2007 end
6327 
6328 cursor c_sty_id(p_tpl_id number) is
6329 select sty_id
6330 from okl_txl_ap_inv_lns_b
6331 where id = p_tpl_id
6332 ;
6333 
6334   -- smadhava - Bug#5200033 - Added - Start
6335   l_currency_code OKL_TRX_AP_INVOICES_B.CURRENCY_CODE%TYPE;
6336 
6337   -- Cursor to get the currency code from the header record
6338   CURSOR c_get_currency_code(cp_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6339     SELECT CURRENCY_CODE
6340       FROM OKL_TRX_AP_INVOICES_B
6341      WHERE ID = cp_tap_id;
6342   -- smadhava - Bug#5200033 - Added - End
6343 
6344  -- vpanwar Added --28/02/2007 -start
6345     CURSOR c_get_khr_id(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6346     SELECT KHR_ID
6347       FROM OKL_TRX_AP_INVOICES_B
6348      WHERE ID = p_tap_id;
6349   -- vpanwar Added --28/02/2007 -end
6350 
6351 BEGIN
6352   -- Set API savepoint
6353   SAVEPOINT UPDATE_FUNDING_LINES_PVT;
6354 
6355   -- Check for call compatibility
6356   IF (NOT FND_API.Compatible_API_Call (l_api_version,
6357                                        p_api_version,
6358                                 	   l_api_name,
6359                                 	   G_PKG_NAME ))
6360   THEN
6361     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6362   END IF;
6363 
6364   -- Initialize message list if requested
6365   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
6366       FND_MSG_PUB.initialize;
6367 	END IF;
6368 
6369   -- Initialize API status to success
6370   x_return_status := OKL_API.G_RET_STS_SUCCESS;
6371 
6372 /*** Begin API body ****************************************************/
6373 
6374     -- check kle_id and sty_id if applicable
6375     -- OKL_TPL_PVT is not belongs to our dev team, so we have to write validate code here
6376     IF (l_tplv_tbl.COUNT > 0) THEN
6377       i := p_tplv_tbl.FIRST;
6378 
6379     -- smadhava - Bug#5200033 - Added - Start
6380     -- get the currency code from the header record
6381     OPEN c_get_currency_code(p_tplv_tbl(i).TAP_ID);
6382       FETCH c_get_currency_code INTO l_currency_code;
6383     CLOSE c_get_currency_code;
6384     -- smadhava - Bug#5200033 - Added - End
6385 
6386        --- vpanwar added 28/02/2007 start
6387     OPEN c_get_khr_id(p_tplv_tbl(i).TAP_ID);
6388       FETCH  c_get_khr_id INTO l_khr_id;
6389     CLOSE c_get_khr_id;
6390     --- vpanwar added 28/02/2007 end
6391 
6392 
6393       LOOP
6394 
6395    --- vpanwar added 28/02/2007 start
6396         l_tplv_tbl(i).KHR_ID := l_khr_id;
6397       --- vpanwar added 28/02/2007 start
6398 
6399 -- cklee: 09/16/2004
6400         IF (l_tplv_tbl(i).sty_id IS NULL or l_tplv_tbl(i).sty_id = OKL_API.G_MISS_NUM) THEN
6401 
6402           open c_sty_id(l_tplv_tbl(i).id);
6403           fetch c_sty_id into l_sty_id;
6404           IF c_sty_id%found THEN
6405             l_tplv_tbl(i).sty_id := l_sty_id;
6406           END IF;
6407           close c_sty_id;
6408         END IF;
6409 
6410         l_return_status := validate_line_attributes(l_tplv_tbl(i), 'U');
6411         IF (l_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6412           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6413         ELSIF (l_return_status = OKL_API.G_RET_STS_ERROR) THEN
6414           RAISE OKL_API.G_EXCEPTION_ERROR;
6415         END IF;
6416 
6417     -- smadhava - Bug#5200033 - Added - Start
6418     -- Round the amount to the System Options setup
6419     l_tplv_tbl(i).AMOUNT := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => p_tplv_tbl(i).AMOUNT
6420                                                            , p_currency_code => l_currency_code);
6421     -- smadhava - Bug#5200033 - Added - End
6422 
6423         EXIT WHEN (i = l_tplv_tbl.LAST);
6424         i := l_tplv_tbl.NEXT(i);
6425 
6426       END LOOP;
6427     END IF;
6428 
6429 --    OKL_TPL_PVT.update_row(
6430       OKL_TXL_AP_INV_LNS_PUB.UPDATE_TXL_AP_INV_LNS(
6431       p_api_version   => p_api_version,
6432       p_init_msg_list => p_init_msg_list,
6433       x_return_status => x_return_status,
6434       x_msg_count     => x_msg_count,
6435       x_msg_data      => x_msg_data,
6436       p_tplv_tbl      => l_tplv_tbl,
6437       x_tplv_tbl      => x_tplv_tbl);
6438 
6439     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6440       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6441     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6442       RAISE OKL_API.G_EXCEPTION_ERROR;
6443     END IF;
6444 
6445 -- sync funding header amount
6446     SYNC_HEADER_AMOUNT(
6447       p_api_version   => p_api_version,
6448       p_init_msg_list => p_init_msg_list,
6449       x_return_status => x_return_status,
6450       x_msg_count     => x_msg_count,
6451       x_msg_data      => x_msg_data,
6452       p_tplv_tbl      => l_tplv_tbl);
6453 
6454     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6455       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6456     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6457       RAISE OKL_API.G_EXCEPTION_ERROR;
6458     END IF;
6459 
6460 
6461 /*** End API body ******************************************************/
6462 
6463   -- Get message count and if count is 1, get message info
6464 	FND_MSG_PUB.Count_And_Get
6465     (p_count          =>      x_msg_count,
6466      p_data           =>      x_msg_data);
6467 
6468 EXCEPTION
6469   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6470     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
6471     x_return_status := OKL_API.G_RET_STS_ERROR;
6472     FND_MSG_PUB.Count_And_Get
6473       (p_count         =>      x_msg_count,
6474        p_data          =>      x_msg_data);
6475 
6476   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6477     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
6478     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6479     FND_MSG_PUB.Count_And_Get
6480       (p_count         =>      x_msg_count,
6481        p_data          =>      x_msg_data);
6482 
6483 
6484   WHEN OTHERS THEN
6485     ROLLBACK TO UPDATE_FUNDING_LINES_PVT;
6486     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6487     OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6488                         p_msg_name      => G_UNEXPECTED_ERROR,
6489                         p_token1        => G_SQLCODE_TOKEN,
6490                         p_token1_value  => SQLCODE,
6491                         p_token2        => G_SQLERRM_TOKEN,
6492                         p_token2_value  => SQLERRM);
6493     FND_MSG_PUB.Count_And_Get
6494       (p_count         =>      x_msg_count,
6495        p_data          =>      x_msg_data);
6496 
6497 END update_funding_lines;
6498 ----------------------------------------------------------------------------
6499 PROCEDURE create_funding_assets(
6500     p_api_version                  IN NUMBER
6501    ,p_init_msg_list                IN VARCHAR2
6502    ,x_return_status                OUT NOCOPY VARCHAR2
6503    ,x_msg_count                    OUT NOCOPY NUMBER
6504    ,x_msg_data                     OUT NOCOPY VARCHAR2
6505    ,p_fund_id                      IN NUMBER
6506  )
6507 IS
6508   l_api_name        CONSTANT VARCHAR2(30) := 'CREATE_FUNDING_ASSETS';
6509 
6510   l_api_version     CONSTANT NUMBER       := 1.0;
6511   l_tplv_tbl        tplv_tbl_type;
6512   x_tplv_tbl        tplv_tbl_type;
6513   i                 NUMBER;
6514   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6515   --l_khr_id          NUMBER;
6516   l_org_id          NUMBER;
6517   l_kle_id          NUMBER;
6518   l_asset_cost      NUMBER;
6519   l_asset_number    VARCHAR2(150);
6520 
6521 
6522   -- smadhava - Bug#5200033 - Modified - Start
6523   l_currency_code OKL_TRX_AP_INVOICES_B.CURRENCY_CODE%TYPE;
6524 
6525   --- vpanwar added 28/02/2007 start
6526   l_khr_id  OKL_TRX_AP_INVOICES_B.KHR_ID%TYPE;
6527   --- vpanwar added 28/02/2007 end
6528 
6529 
6530   -- Modifed cursor to get the currency code
6531   CURSOR c (p_fund_id  NUMBER)
6532   IS
6533   select a.org_id
6534        , a.currency_code
6535   from   okl_trx_ap_invoices_b a
6536   where  a.id = p_fund_id
6537   and    a.funding_type_code = 'ASSET'
6538   ;
6539   -- smadhava - Bug#5200033 - Modified - End
6540 
6541 
6542   --veramach 5600694  start modified cursor c2
6543   /*CURSOR c2 (p_fund_id NUMBER)
6544   IS
6545   select a.cle_id
6546          ,a.KLE_AMT
6547   from   okl_fund_assets_lov_uv a
6548   where  a.FUND_ID = p_fund_id
6549   and    a.KLE_AMT > 0
6550   and    NOT EXISTS
6551           (select 1
6552            from okl_txl_ap_inv_lns_b b
6553            where a.FUND_ID = b.tap_id
6554            and   a.cle_id = b.kle_id)
6555   ;*/
6556  	CURSOR c2 (p_fund_id NUMBER)
6557  	IS
6558  	/*veramach 29-Jun-2007 bug#5600694 commented to improve the performance */
6559  	 --SELECT cle_id, KLE_AMT
6560  	 --FROM
6561  	 --(
6562  	   select a.cle_id,
6563  	       (select OKL_FUNDING_PVT.get_contract_line_amt(a.CHR_ID, a.CLE_ID, b.ipvs_id)  from dual) KLE_AMT
6564  	   from   OKL_ASSETS_LOV_UV A,
6565  	   OKL_TRX_AP_INVOICES_B b
6566  	   WHERE  a.chr_id = b.khr_id
6567  	   AND b.ID = p_fund_id
6568  	   and    NOT EXISTS
6569  	          (select 1
6570  	           from okl_txl_ap_inv_lns_b c
6571  	           where b.ID = c.tap_id
6572  	           and   a.cle_id = c.kle_id)
6573 	  -- )
6574  	   --WHERE    KLE_AMT > 0
6575  	   ;
6576  	   --veramach 5600694  end
6577 
6578    -- vpanwar Added --28/02/2007 -start
6579     CURSOR c_get_khr_id(p_tap_id OKL_TRX_AP_INVOICES_B.ID%TYPE) IS
6580     SELECT KHR_ID
6581       FROM OKL_TRX_AP_INVOICES_B
6582      WHERE ID = p_tap_id;
6583   -- vpanwar Added --28/02/2007 -end
6584 
6585 
6586 BEGIN
6587   -- Set API savepoint
6588   SAVEPOINT CREATE_FUNDING_ASSETS_PVT;
6589 
6590   -- Check for call compatibility
6591   IF (NOT FND_API.Compatible_API_Call (l_api_version,
6592                                 	   p_api_version,
6593                                 	   l_api_name,
6594                                 	   G_PKG_NAME ))
6595   THEN
6596     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6597   END IF;
6598 
6599   -- Initialize message list if requested
6600   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
6601       FND_MSG_PUB.initialize;
6602 	END IF;
6603 
6604   -- Initialize API status to success
6605   x_return_status := OKL_API.G_RET_STS_SUCCESS;
6606 
6607 
6608 /*** Begin API body ****************************************************/
6609 
6610   -- smadhava - Bug#5200033 - Modified - Start
6611   -- get org_id and Currency code
6612 
6613   OPEN c(p_fund_id);
6614   FETCH c INTO l_org_id, l_currency_code;
6615   CLOSE c;
6616   -- smadhava - Bug#5200033 - Modified - End
6617 
6618 -- fill in l_tplv_tbl
6619   OPEN c2(p_fund_id);
6620   i := 0;
6621   LOOP
6622 
6623        FETCH c2 into
6624                 l_kle_id,
6625                 l_asset_cost;
6626        EXIT WHEN c2%NOTFOUND;
6627 	     IF l_asset_cost > 0 THEN  --for bug#5600694
6628 
6629        l_tplv_tbl(i).inv_distr_line_code := 'ITEM';
6630        l_tplv_tbl(i).tap_id := p_fund_id;
6631 
6632        --- vpanwar added 28/02/2007 start
6633        OPEN c_get_khr_id(l_tplv_tbl(i).TAP_ID);
6634 	FETCH  c_get_khr_id INTO l_khr_id;
6635        CLOSE c_get_khr_id;
6636 
6637         l_tplv_tbl(i).KHR_ID := l_khr_id;
6638 
6639        --- vpanwar added 28/02/2007 start
6640 
6641 
6642        l_tplv_tbl(i).kle_id := l_kle_id;
6643        -- smadhava - Bug#5200033 - Modified - Start
6644        -- Round the asset cost to the system options rounding setup
6645        l_tplv_tbl(i).amount := OKL_ACCOUNTING_UTIL.round_amount(p_amount        => l_asset_cost
6646                                                               , p_currency_code => l_currency_code);
6647 --       l_tplv_tbl(i).amount := l_asset_cost;
6648        -- smadhava - Bug#5200033 - Modified - End
6649 
6650        l_tplv_tbl(i).org_id := l_org_id;
6651        l_tplv_tbl(i).line_number := i+1;
6652        l_tplv_tbl(i).DISBURSEMENT_BASIS_CODE := 'BILL_DATE';
6653 
6654         -- fixed bug#3338910
6655         l_return_status := populate_sty_id(l_tplv_tbl(i));
6656 
6657         IF (l_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
6658           IF (x_return_status <> OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6659             x_return_status := l_return_status;
6660           END IF;
6661           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6662         END IF;
6663 
6664 
6665        i := i + 1;
6666 
6667     END IF;
6668   END LOOP;
6669 
6670   CLOSE c2;
6671 
6672   IF (l_tplv_tbl.COUNT > 0) THEN
6673 
6674 --    OKL_TPL_PVT.insert_row(
6675     OKL_TXL_AP_INV_LNS_PUB.INSERT_TXL_AP_INV_LNS(
6676       p_api_version   => p_api_version,
6677       p_init_msg_list => p_init_msg_list,
6678       x_return_status => x_return_status,
6679       x_msg_count     => x_msg_count,
6680       x_msg_data      => x_msg_data,
6681       p_tplv_tbl      => l_tplv_tbl,
6682       x_tplv_tbl      => x_tplv_tbl);
6683 
6684 
6685     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6686       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6687     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6688       RAISE OKL_API.G_EXCEPTION_ERROR;
6689     END IF;
6690 
6691 -- sync funding header amount
6692     OKL_FUNDING_PVT.SYNC_HEADER_AMOUNT(
6693       p_api_version   => p_api_version,
6694       p_init_msg_list => p_init_msg_list,
6695       x_return_status => x_return_status,
6696       x_msg_count     => x_msg_count,
6697       x_msg_data      => x_msg_data,
6698       p_tplv_tbl      => x_tplv_tbl);
6699 
6700     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6701       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6702     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6703       RAISE OKL_API.G_EXCEPTION_ERROR;
6704     END IF;
6705 
6706   END IF;
6707 
6708 /*** End API body ******************************************************/
6709 
6710   -- Get message count and if count is 1, get message info
6711 	FND_MSG_PUB.Count_And_Get
6712     (p_count          =>      x_msg_count,
6713      p_data           =>      x_msg_data);
6714 
6715 EXCEPTION
6716   WHEN OKL_API.G_EXCEPTION_ERROR THEN
6717     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
6718     x_return_status := OKL_API.G_RET_STS_ERROR;
6719     FND_MSG_PUB.Count_And_Get
6720       (p_count         =>      x_msg_count,
6721        p_data          =>      x_msg_data);
6722 
6723   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
6724     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
6725     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6726 
6727     FND_MSG_PUB.Count_And_Get
6728       (p_count         =>      x_msg_count,
6729        p_data          =>      x_msg_data);
6730 
6731   WHEN OTHERS THEN
6732 	ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
6733       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
6734       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
6735                           p_msg_name      => G_UNEXPECTED_ERROR,
6736                           p_token1        => G_SQLCODE_TOKEN,
6737                           p_token1_value  => SQLCODE,
6738                           p_token2        => G_SQLERRM_TOKEN,
6739                           p_token2_value  => SQLERRM);
6740       FND_MSG_PUB.Count_And_Get
6741         (p_count         =>      x_msg_count,
6742 
6743          p_data          =>      x_msg_data);
6744 
6745   END create_funding_assets;
6746 ----------------------------------------------------------------------------
6747 
6748  PROCEDURE reverse_funding_requests(
6749     p_api_version                  IN NUMBER
6750    ,p_init_msg_list                IN VARCHAR2
6751    ,x_return_status                OUT NOCOPY VARCHAR2
6752    ,x_msg_count                    OUT NOCOPY NUMBER
6753    ,x_msg_data                     OUT NOCOPY VARCHAR2
6754    ,p_contract_id                  IN NUMBER
6755  )
6756 IS
6757   l_api_name        CONSTANT VARCHAR2(30) := 'reverse_funding_requests';
6758   l_api_version     CONSTANT NUMBER       := 1.0;
6759 
6760   l_tapv_rec        tapv_rec_type;
6761   x_tapv_rec        tapv_rec_type;
6762 
6763   funding_line_id   number;
6764 
6765 
6766   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
6767   l_currency_code   okc_k_headers_b.CURRENCY_CODE%TYPE;
6768   l_org_id          okc_k_headers_b.AUTHORING_ORG_ID%TYPE;
6769   l_contract_number okc_k_headers_b.CONTRACT_NUMBER%TYPE;
6770 
6771   l_amount           okl_trx_ap_invoices_b.AMOUNT%TYPE;
6772 
6773   l_ipvs_id          okl_trx_ap_invoices_b.IPVS_ID%TYPE;
6774   l_vendor_site_code OKX_VENDOR_SITES_V.NAME%TYPE;
6775 
6776   l_PAY_GROUP_LOOKUP_CODE  OKL_TRX_AP_INVOICES_B.PAY_GROUP_LOOKUP_CODE%TYPE;
6777   l_NETTABLE_YN            OKL_TRX_AP_INVOICES_B.NETTABLE_YN%TYPE;
6778 
6779 
6780     CURSOR cu (p_id NUMBER)
6781     IS
6782       SELECT h.PAY_GROUP_LOOKUP_CODE,
6783              h.NETTABLE_YN
6784         FROM OKL_TRX_AP_INVOICES_B h
6785        WHERE h.id = p_id
6786     ;
6787 
6788   --
6789   CURSOR c (p_contract_id  NUMBER)
6790   IS
6791   select a.AUTHORING_ORG_ID,
6792          a.CURRENCY_CODE,
6793          a.CONTRACT_NUMBER
6794   from   okc_k_headers_b a
6795   where  a.id = p_contract_id
6796   ;
6797 
6798   -- sjalasut, modified the below cursor to have p_contract_id joined with
6799   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
6800   -- as part of OKLR12B disbursements project
6801   CURSOR c2 (p_contract_id  NUMBER)
6802   IS
6803   select a.ipvs_id,
6804          nvl(sum(OKL_FUNDING_PVT.get_contract_line_funded_amt(a.id,a.funding_type_code)),0)
6805   from  okl_trx_ap_invoices_b a
6806        ,okl_txl_ap_inv_lns_all_b b
6807   where a.id = b.tap_id
6808   and  b.khr_id = p_contract_id
6809   and a.trx_status_code in ('APPROVED', 'PROCESSED')
6810   and    a.funding_type_code IS NOT NULL -- cklee 09-24-03
6811   and    a.funding_type_code <> 'SUPPLIER_RETENTION'
6812   group by a.ipvs_id
6813   ;
6814 
6815   CURSOR c_vendor_site (p_ipvs_id  NUMBER)
6816   IS
6817   select a.name
6818   from OKX_VENDOR_SITES_V a
6819   where a.id1 = p_ipvs_id
6820   ;
6821 
6822   --- vpanwar 21/02/2007 Added
6823   --- to get all the funding lines for the funding header
6824     CURSOR fund_line_csr(p_fund_id number) IS
6825         Select id funding_line_id
6826         from OKL_TXL_AP_INV_LNS_B
6827         Where tap_id = p_fund_id;
6828   --- vpanwar 21/02/2007 End
6829 
6830 BEGIN
6831   -- Set API savepoint
6832   SAVEPOINT CREATE_FUNDING_ASSETS_PVT;
6833 
6834   -- Check for call compatibility
6835 
6836   IF (NOT FND_API.Compatible_API_Call (l_api_version,
6837                                 	   p_api_version,
6838 
6839                                 	   l_api_name,
6840                                 	   G_PKG_NAME ))
6841   THEN
6842     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6843 
6844   END IF;
6845 
6846   -- Initialize message list if requested
6847   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
6848       FND_MSG_PUB.initialize;
6849 	END IF;
6850 
6851   -- Initialize API status to success
6852   x_return_status := OKL_API.G_RET_STS_SUCCESS;
6853 
6854 
6855 /*** Begin API body ****************************************************/
6856   -- get org_id
6857   OPEN c(p_contract_id);
6858   FETCH c INTO l_org_id,
6859                l_currency_code,
6860                l_contract_number;
6861   CLOSE c;
6862 
6863 
6864   OPEN c2(p_contract_id);
6865   LOOP
6866 
6867     FETCH c2 into l_ipvs_id,
6868                   l_amount;
6869 
6870     OPEN c_vendor_site(l_ipvs_id);
6871     FETCH c_vendor_site into l_vendor_site_code;
6872     CLOSE c_vendor_site;
6873 
6874     EXIT WHEN c2%NOTFOUND;
6875 
6876     IF (l_amount <> 0 ) THEN
6877       l_amount := -l_amount;
6878 
6879       -- sjalasut, not commenting the khr_id reference in l_tapv_rec here as this
6880       -- record variable is used as a parameter for validate_header_attributes,
6881       -- validate_funding_request etc. since per the disbursements FDD, tapv_rec
6882       -- .khr_id would continue to exist, not making this change would not cause
6883       -- compilation issues.
6884       l_tapv_rec.KHR_ID := p_contract_id;
6885       l_tapv_rec.AMOUNT := l_amount;
6886       l_tapv_rec.FUNDING_TYPE_CODE := 'PREFUNDING';
6887       l_tapv_rec.IPVS_ID := l_ipvs_id;
6888       l_tapv_rec.ORG_ID := l_org_id;
6889 
6890 
6891       l_tapv_rec.TRX_STATUS_CODE := 'ENTERED'; -- create record 1st
6892       l_tapv_rec.DESCRIPTION
6893         := 'Account Payable debit for Reverse Contract, ' || l_contract_number || ', vendor site '|| l_vendor_site_code;
6894       l_tapv_rec.CURRENCY_CODE := l_currency_code;
6895       l_tapv_rec.PAYMENT_METHOD_CODE := 'CHECK';
6896       l_tapv_rec.DATE_ENTERED := sysdate;
6897 
6898       -- sjalasut, modified the invoice_type from G_STANDARD to G_CREDIT
6899       -- changes made as part of OKLR12B Disbursements Project
6900       l_tapv_rec.INVOICE_TYPE := G_CREDIT;
6901 
6902       -- sjalasut, added code to make sure that the invoice amount on the credit memo
6903       -- invoice is negative. changes made as part of OKLR12B Disbursements project
6904       IF(l_tapv_rec.AMOUNT > 0)THEN
6905         l_tapv_rec.AMOUNT := ((l_tapv_rec.AMOUNT)*(-1));
6906       END IF;
6907 
6908       l_tapv_rec.DATE_INVOICED := sysdate;
6909       l_tapv_rec.DATE_GL := sysdate;
6910 
6911       create_funding_header(
6912         p_api_version   => p_api_version,
6913         p_init_msg_list => p_init_msg_list,
6914         x_return_status => x_return_status,
6915         x_msg_count     => x_msg_count,
6916         x_msg_data      => x_msg_data,
6917         p_tapv_rec      => l_tapv_rec,
6918         x_tapv_rec      => x_tapv_rec);
6919 
6920 
6921       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6922         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6923       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6924         RAISE OKL_API.G_EXCEPTION_ERROR;
6925       END IF;
6926 
6927       OPEN cu (x_tapv_rec.id);
6928       FETCH cu INTO l_PAY_GROUP_LOOKUP_CODE,
6929                   l_NETTABLE_YN;
6930       CLOSE cu;
6931 
6932       l_tapv_rec.ID := x_tapv_rec.id;
6933       l_tapv_rec.PAY_GROUP_LOOKUP_CODE := l_PAY_GROUP_LOOKUP_CODE;
6934       l_tapv_rec.NETTABLE_YN := l_NETTABLE_YN;
6935       l_tapv_rec.TRX_STATUS_CODE := 'APPROVED';
6936 -- cklee 09-24-03
6937 -- due to the external interface table limit to 30 chars
6938       l_tapv_rec.VENDOR_INVOICE_NUMBER := x_tapv_rec.INVOICE_NUMBER;
6939 
6940       update_funding_header(
6941         p_api_version   => p_api_version,
6942         p_init_msg_list => p_init_msg_list,
6943         x_return_status => x_return_status,
6944         x_msg_count     => x_msg_count,
6945         x_msg_data      => x_msg_data,
6946         p_tapv_rec      => l_tapv_rec,
6947         x_tapv_rec      => x_tapv_rec);
6948 
6949       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6950         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6951 
6952       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6953         RAISE OKL_API.G_EXCEPTION_ERROR;
6954       END IF;
6955 
6956       -- WF enable, add accounting entry 06/24/03 cklee
6957 
6958 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
6959 /*
6960       --- vpanwar 21/02/2007 Added
6961     OPEN fund_line_csr(l_tapv_rec.id);
6962     LOOP
6963     FETCH fund_line_csr into funding_line_id;
6964 
6965     EXIT WHEN fund_line_csr%NOTFOUND;
6966 */
6967 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
6968 
6969       OKL_FUNDING_PVT.CREATE_ACCOUNTING_DIST
6970                           (p_api_version   => p_api_version,
6971                            p_init_msg_list => p_init_msg_list,
6972                            x_return_status => x_return_status,
6973                            x_msg_count     => x_msg_count,
6974                            x_msg_data      => x_msg_data,
6975                            p_status        => l_tapv_rec.trx_status_code,
6976                            p_fund_id       => l_tapv_rec.id);--,--:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
6977 --                           p_fund_line_id  => funding_line_id);--:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
6978 
6979 
6980       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
6981         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
6982       ELSIF (x_return_Status = OKL_API.G_RET_STS_ERROR)  THEN
6983         RAISE OKL_API.G_EXCEPTION_ERROR;
6984       END IF;
6985 
6986 --start:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
6987 --    END LOOP;
6988 --    CLOSE fund_line_csr;
6989     --- vpanwar 21/02/2007 End
6990 --end:| 21-May-2007 cklee    OKLR12B Accounting CR                                 |
6991 
6992     END IF;
6993 
6994   END LOOP;
6995   CLOSE c2;
6996 
6997 /*** End API body ******************************************************/
6998 
6999   -- Get message count and if count is 1, get message info
7000 	FND_MSG_PUB.Count_And_Get
7001     (p_count          =>      x_msg_count,
7002      p_data           =>      x_msg_data);
7003 
7004 EXCEPTION
7005   WHEN OKL_API.G_EXCEPTION_ERROR THEN
7006     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
7007     x_return_status := OKL_API.G_RET_STS_ERROR;
7008     FND_MSG_PUB.Count_And_Get
7009       (p_count         =>      x_msg_count,
7010        p_data          =>      x_msg_data);
7011 
7012   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
7013     ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
7014     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
7015     FND_MSG_PUB.Count_And_Get
7016       (p_count         =>      x_msg_count,
7017        p_data          =>      x_msg_data);
7018 
7019   WHEN OTHERS THEN
7020 	ROLLBACK TO CREATE_FUNDING_ASSETS_PVT;
7021       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
7022 
7023       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
7024                           p_msg_name      => G_UNEXPECTED_ERROR,
7025                           p_token1        => G_SQLCODE_TOKEN,
7026                           p_token1_value  => SQLCODE,
7027                           p_token2        => G_SQLERRM_TOKEN,
7028                           p_token2_value  => SQLERRM);
7029       FND_MSG_PUB.Count_And_Get
7030         (p_count         =>      x_msg_count,
7031          p_data          =>      x_msg_data);
7032 
7033 
7034   END reverse_funding_requests;
7035 
7036 ----------------------------------------------------------------------------
7037 /*
7038  	   veramach 29-Jun-2007 Bug#5600694 Modified the function get_contract_line_amt
7039  	   to improve the performance of funding screens. Merged the cursor cv_addon in
7040  	   cv_model.
7041  	   The cv_model cursor considers the item for which the vendor is passed and
7042  	   add on items for that item for which a vendor is attached.
7043  	   The add on items should have a vendor and not necessary that
7044  	   the vendor should match the vendor of the item.
7045  	   Example 1:
7046  	      Item     - >    Addon1     and Addon2
7047  	     (Vendor1)       (Vendor2)     (Vendor2)
7048 
7049  	         cv_model will consider Item, Addon1 and Addon2.
7050 
7051  	   Example 2:
7052  	      Item     - >    Addon1     and Addon2
7053  	     (Vendor1)       (Vendor1)     (No vendor)
7054 
7055  	     cv_model will consider Item and Addon1.
7056 
7057  	 */
7058 
7059 
7060 FUNCTION get_contract_line_amt(
7061   p_khr_id           IN   NUMBER,  -- contract hdr
7062   p_kle_id           IN   NUMBER,  -- contract line
7063   p_vendor_site_id   IN   NUMBER
7064 )
7065   RETURN NUMBER IS
7066   l_amount            NUMBER                                        := 0;
7067   l_amount_buffer     NUMBER                                        := 0;
7068   l_vendor_id         NUMBER                                        := 0;
7069 -- start: okl.h: cklee
7070   x_return_status     VARCHAR2(3)                                   := okl_api.g_ret_sts_success;
7071   l_api_version       NUMBER                                        := 1.0;
7072   x_msg_count         NUMBER;
7073   x_msg_data          VARCHAR2(4000);
7074   l_init_msg_list     VARCHAR2(10)                                  := okl_api.g_false;
7075   x_value             NUMBER;
7076 
7077 -- start: okl.h: cklee
7078   CURSOR cv1(
7079     p_vendor_site_id   NUMBER
7080   ) IS
7081     SELECT vendor_id
7082       FROM okx_vendor_sites_v
7083      WHERE id1 = TO_CHAR(p_vendor_site_id);
7084 
7085 --
7086 --
7087 -- FA line with vendor attach
7088 --
7089   CURSOR cv_model(
7090     p_khr_id      NUMBER,
7091     p_kle_id      NUMBER,
7092     p_vendor_id   NUMBER
7093   ) IS
7094     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7095       FROM okc_k_items cim,
7096            okc_k_party_roles_b cpl,
7097            okc_k_lines_b cle
7098      WHERE
7099 --for model lines of vendor
7100            (
7101                 cim.cle_id = cle.ID
7102             AND cim.dnz_chr_id = p_khr_id
7103             AND cpl.cle_id = cle.ID
7104             AND cpl.dnz_chr_id = p_khr_id
7105             AND cpl.chr_id IS NULL
7106             AND cpl.rle_code = 'OKL_VENDOR'
7107             AND cpl.object1_id1 = TO_CHAR(p_vendor_id)
7108             AND cpl.object1_id2 = '#'
7109             AND EXISTS(SELECT NULL
7110                          FROM okc_line_styles_b model_lse
7111                         WHERE model_lse.ID = cle.lse_id AND model_lse.lty_code = 'ITEM' AND cle.cle_id = p_kle_id)
7112            )   -- end of model lines
7113 -- re lease flag check
7114        AND EXISTS(SELECT NULL
7115                     FROM okl_k_lines lkl
7116                    WHERE lkl.ID = cle.ID AND lkl.re_lease_yn IS NULL);
7117 
7118 --
7119 --
7120 -- add on line with vendor attach, but don't care which vendor attach
7121 --
7122   CURSOR cv_addon(
7123     p_khr_id      NUMBER,
7124     p_kle_id      NUMBER,
7125 -- start: okl.h: cklee -- add vendor_id as a parameter
7126     p_vendor_id   NUMBER
7127   )
7128 -- end: okl.h: cklee -- add vendor_id as a parameter
7129   IS
7130     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7131       FROM okc_k_items cim,
7132            okc_k_party_roles_b cpl,
7133            okc_k_lines_b cle
7134      WHERE
7135 --for add on lines: don't care the vendor association, but care if it has vendor association!
7136            (
7137                 cim.cle_id = cle.ID
7138             AND cim.dnz_chr_id = p_khr_id
7139             AND cpl.cle_id = cle.ID
7140             AND cpl.dnz_chr_id = p_khr_id
7141             AND cpl.chr_id IS NULL
7142             AND cpl.rle_code = 'OKL_VENDOR'
7143 -- start: okl.h: cklee
7144             AND cpl.object1_id1 = TO_CHAR(p_vendor_id)
7145             AND cpl.object1_id2 = '#'
7146 -- end: okl.h: cklee
7147             AND EXISTS(SELECT NULL
7148                          FROM okc_line_styles_b adon_lse
7149                         WHERE adon_lse.ID = cle.lse_id AND adon_lse.lty_code = 'ADD_ITEM'
7150                                                                                          -- ADD_ITEM has one parent ITEM only
7151                               AND EXISTS(SELECT NULL
7152                                            FROM okc_k_lines_b mdl_parent
7153                                           WHERE mdl_parent.ID = cle.cle_id AND mdl_parent.cle_id = p_kle_id))
7154            )   -- end of add on lines
7155 -- re lease flag check
7156        AND EXISTS(SELECT NULL
7157                     FROM okl_k_lines lkl
7158                    WHERE lkl.ID = cle.ID AND lkl.re_lease_yn IS NULL);
7159 
7160 --
7161 --
7162 -- if NO vendor_id
7163 --
7164   CURSOR cv_no_vendor(
7165     p_khr_id   NUMBER,
7166     p_kle_id   NUMBER
7167   ) IS
7168     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7169       FROM okc_k_items cim,
7170            okc_k_party_roles_b cpl,
7171            okc_k_lines_b cle
7172      WHERE
7173 --for model lines of vendor
7174            (
7175                 cim.cle_id = cle.ID
7176             AND cim.dnz_chr_id = p_khr_id
7177             AND cpl.cle_id = cle.ID
7178             AND cpl.dnz_chr_id = p_khr_id
7179             AND cpl.chr_id IS NULL
7180             AND cpl.rle_code = 'OKL_VENDOR'
7181 --and    cpl.object1_id1 = to_char(p_vendor_id)
7182 --and    cpl.object1_id2 = '#'
7183             AND EXISTS(SELECT NULL
7184                          FROM okc_line_styles_b model_lse
7185                         WHERE model_lse.ID = cle.lse_id AND model_lse.lty_code = 'ITEM' AND cle.cle_id = p_kle_id)
7186            )   -- end of model lines
7187         OR
7188 --for add on lines: don't care the vendor association, but care if it has vendor association!
7189                (
7190                     cim.cle_id = cle.ID
7191                 AND cim.dnz_chr_id = p_khr_id
7192                 AND cpl.cle_id = cle.ID
7193                 AND cpl.dnz_chr_id = p_khr_id
7194                 AND cpl.chr_id IS NULL
7195                 AND cpl.rle_code = 'OKL_VENDOR'
7196 --and    cpl.object1_id1 = to_char(p_vendor_id)
7197 --and    cpl.object1_id2 = '#'
7198                 AND EXISTS(SELECT NULL
7199                              FROM okc_line_styles_b adon_lse
7200                             WHERE adon_lse.ID = cle.lse_id AND adon_lse.lty_code = 'ADD_ITEM'
7201                                                                                              -- ADD_ITEM has one parent ITEM only
7202                                   AND EXISTS(SELECT NULL
7203                                                FROM okc_k_lines_b mdl_parent
7204                                               WHERE mdl_parent.ID = cle.cle_id AND mdl_parent.cle_id = p_kle_id))
7205                )   -- end of add on lines
7206 -- re lease flag check
7207            AND EXISTS(SELECT NULL
7208                         FROM okl_k_lines lkl
7209                        WHERE lkl.ID = cle.ID AND lkl.re_lease_yn IS NULL);
7210 
7211 --
7212 --
7213 -- if NO line and vendor_id
7214 --
7215   CURSOR c_no_line_and_vendor(
7216     p_khr_id   NUMBER
7217   ) IS
7218     SELECT NVL(SUM(NVL(cle.price_unit, 0) * NVL(cim.number_of_items, 0)), 0)
7219       FROM okc_k_items cim,
7220            okc_k_party_roles_b cpl,
7221            okc_k_lines_b cle
7222      WHERE (
7223                 cim.cle_id = cle.ID
7224             AND cim.dnz_chr_id = p_khr_id
7225             AND cpl.cle_id = cle.ID
7226             AND cpl.dnz_chr_id = p_khr_id
7227             AND cpl.chr_id IS NULL
7228             AND cpl.rle_code = 'OKL_VENDOR'
7229             AND EXISTS(SELECT NULL
7230                          FROM okc_line_styles_b model_lse
7231                         WHERE model_lse.ID = cle.lse_id
7232                           AND model_lse.lty_code = 'ITEM')
7233            )
7234         OR     (
7235                     cim.cle_id = cle.ID
7236                 AND cim.dnz_chr_id = p_khr_id
7237                 AND cpl.cle_id = cle.ID
7238                 AND cpl.dnz_chr_id = p_khr_id
7239                 AND cpl.chr_id IS NULL
7240                 AND cpl.rle_code = 'OKL_VENDOR'
7241                 AND EXISTS(SELECT NULL
7242                              FROM okc_line_styles_b adon_lse
7243                             WHERE adon_lse.ID = cle.lse_id
7244                               AND adon_lse.lty_code = 'ADD_ITEM'
7245                               AND EXISTS(SELECT NULL
7246                                           FROM okc_k_lines_b mdl_parent
7247                                          WHERE mdl_parent.ID = cle.cle_id))
7248                )
7249            AND EXISTS(SELECT NULL
7250                         FROM okl_k_lines lkl
7251                        WHERE lkl.ID = cle.ID
7252                          AND lkl.re_lease_yn IS NULL);
7253 
7254 --
7255 -- bug 5384359 -- start
7256   CURSOR downpymnt_recvr_csr(
7257     p_kle_id   NUMBER
7258   ) IS
7259     SELECT down_payment_receiver_code
7260       FROM okl_k_lines
7261      WHERE ID = p_kle_id;
7262 
7263   l_downpymnt_recvr   okl_k_lines.down_payment_receiver_code%TYPE;
7264 -- bug 5384359 -- end
7265 --
7266 
7267 --start:| 08-Feb-08  cklee Fixed bug: 6783566                                        |
7268 /*
7269   CURSOR c_kle_id(
7270                   p_khr_id okc_k_headers_b.id%TYPE
7271                  ) IS
7272     SELECT kle.cle_id kle_id,
7273            kle_k.down_payment_receiver_code downpymnt_recvr
7274       FROM okl_assets_lov_uv kle,
7275            okl_k_lines kle_k
7276      WHERE kle.cle_id = kle_k.ID
7277        AND kle.chr_id = p_khr_id;
7278 */
7279   CURSOR c_kle_id(
7280                   p_khr_id okc_k_headers_b.id%TYPE
7281                  ) IS
7282     SELECT kle_k.id kle_id,
7283            kle_k.down_payment_receiver_code downpymnt_recvr
7284       FROM okl_k_lines kle_k,
7285            okc_k_lines_b kle
7286      where kle_k.id = kle.id
7287      and kle.dnz_chr_id = p_khr_id
7288      and kle_k.re_lease_yn IS NULL  -- re lease flag check
7289      -- only asset lines associated with Lease Vendor (Supplier Invoice)
7290      and exists (
7291       SELECT 1
7292       FROM okc_k_party_roles_b cpl,
7293            okc_k_lines_b cle,
7294            okc_line_styles_b model_lse
7295      WHERE cpl.rle_code = 'OKL_VENDOR'
7296        AND cpl.chr_id IS NULL
7297        and model_lse.ID = cle.lse_id
7298        and model_lse.lty_code = 'ITEM'
7299        and cle.cle_id = kle_k.id -- link to FREE_FORM1 (top line)
7300        AND cpl.object1_id2 = '#'
7301        AND cpl.cle_id = cle.ID); -- link to ITEM
7302 
7303 --end:| 08-Feb-08  cklee Fixed bug: 6783566                                        |
7304 
7305 BEGIN
7306   IF (p_khr_id IS NULL) OR (p_khr_id = okl_api.g_miss_num) THEN
7307     RETURN 0;   -- error
7308   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
7309 
7310     OPEN c_no_line_and_vendor(p_khr_id);
7311     FETCH c_no_line_and_vendor INTO l_amount;
7312     CLOSE c_no_line_and_vendor;
7313 
7314     --------------------------------------------------
7315     -- Contract Trade In AND Contract Capitalized Reduction -- Downpayment
7316     --------------------------------------------------
7317     FOR l_kle_id IN c_kle_id(p_khr_id) LOOP
7318       BEGIN
7319         l_amount_buffer := okl_seeded_functions_pvt.line_tradein(p_chr_id => p_khr_id, p_line_id => l_kle_id.kle_id);
7320 
7321         IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7322           l_amount_buffer := 0;
7323         ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7324           l_amount_buffer := 0;
7325         END IF;
7326       EXCEPTION
7327         WHEN OTHERS THEN
7328           l_amount_buffer := 0;
7329       END;
7330 
7331       l_amount := l_amount - l_amount_buffer;
7332 
7333       BEGIN
7334         IF ((l_kle_id.downpymnt_recvr IS NULL) OR (l_kle_id.downpymnt_recvr = 'VENDOR')) THEN
7335           l_amount_buffer := okl_seeded_functions_pvt.line_capital_reduction(p_chr_id => p_khr_id, p_line_id => l_kle_id.kle_id);
7336 
7337           IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7338             l_amount_buffer := 0;
7339           ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7340             l_amount_buffer := 0;
7341           END IF;
7342         ELSIF(l_kle_id.downpymnt_recvr = 'LESSOR') THEN
7343           l_amount_buffer := 0;
7344         END IF;
7345 
7346       EXCEPTION
7347         WHEN OTHERS THEN
7348           l_amount_buffer := 0;
7349       END;
7350 
7351       l_amount := l_amount - l_amount_buffer;
7352     END LOOP;
7353 
7354   ELSIF (p_vendor_site_id IS NULL OR p_vendor_site_id = okl_api.g_miss_num) THEN
7355 
7356     OPEN cv_no_vendor(p_khr_id, p_kle_id);
7357     FETCH cv_no_vendor INTO l_amount;
7358     CLOSE cv_no_vendor;
7359 
7360     -- start: skgautam Bug#5260198
7361     --------------------------------------------------
7362     -- Contract Trade In
7363     --------------------------------------------------
7364     BEGIN
7365       l_amount_buffer := okl_seeded_functions_pvt.line_tradein(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7366 
7367       IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7368         l_amount_buffer := 0;
7369       ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7370         l_amount_buffer := 0;
7371       END IF;
7372     EXCEPTION
7373       WHEN OTHERS THEN
7374         l_amount_buffer := 0;
7375     END;
7376 
7377     l_amount := l_amount - l_amount_buffer;
7378 
7379     --------------------------------------------------
7380     -- Contract Capitalized Reduction -- Downpayment
7381     --------------------------------------------------
7382     BEGIN
7383       -- bug 5384359 -- start
7384       OPEN downpymnt_recvr_csr(p_kle_id);
7385       FETCH downpymnt_recvr_csr INTO l_downpymnt_recvr;
7386       CLOSE downpymnt_recvr_csr;
7387 
7388       IF ((l_downpymnt_recvr IS NULL) OR (l_downpymnt_recvr = 'VENDOR')) THEN
7389         -- bug 5384359 -- end
7390         l_amount_buffer := okl_seeded_functions_pvt.line_capital_reduction(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7391 
7392         IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7393           l_amount_buffer := 0;
7394         ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7395           l_amount_buffer := 0;
7396         END IF;
7397         --bug 5473370 --start
7398       ELSIF(l_downpymnt_recvr = 'LESSOR') THEN
7399         l_amount_buffer := 0;
7400         --bug 5473370 -- end
7401         -- bug 5384359 -- start
7402       END IF;
7403         -- bug 5384359 -- END
7404     EXCEPTION
7405       WHEN OTHERS THEN
7406         l_amount_buffer := 0;
7407     END;
7408 
7409     l_amount := l_amount - l_amount_buffer;
7410     -- end: skgautam Bug#5260198
7411   ELSE
7412     OPEN cv1(p_vendor_site_id);
7413     FETCH cv1 INTO l_vendor_id;
7414     CLOSE cv1;
7415 
7416     -- get model line attach to vendor
7417     OPEN cv_model(p_khr_id,p_kle_id,l_vendor_id);
7418     FETCH cv_model INTO l_amount_buffer;
7419     CLOSE cv_model;
7420 
7421     -- start: okl.h: cklee
7422     /* commented out
7423         IF (l_amount_buffer <= 0) THEN
7424           return 0;
7425         ELSE
7426     */
7427     -- start: okl.h: cklee
7428     l_amount := l_amount_buffer;
7429 
7430     -- get add on attach to vendor, but don't care which vendor attach
7431     -- start: okl.h: cklee -- add vendor_id as a parameter
7432     OPEN cv_addon(p_khr_id,p_kle_id,l_vendor_id);
7433     -- end: okl.h: cklee
7434     FETCH cv_addon INTO l_amount_buffer;
7435     CLOSE cv_addon;
7436 
7437     l_amount := l_amount + l_amount_buffer;
7438 
7439     --    END IF;
7440     -- start: okl.h: cklee
7441     --------------------------------------------------
7442     -- Contract Capitalized Reduction -- Downpayment
7443     --------------------------------------------------
7444     BEGIN
7445       --START:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7446       /*
7447             OKL_EXECUTE_FORMULA_PUB.execute(
7448               p_api_version   => l_api_version,
7449               p_init_msg_list => l_init_msg_list,
7450               x_return_status => x_return_status,
7451               x_msg_count     => x_msg_count,
7452               x_msg_data      => x_msg_data,
7453               p_formula_name  => 'CONTRACT_TRADEIN',
7454               p_contract_id   => p_khr_id,
7455               p_line_id       => p_kle_id,
7456               x_value         => l_amount_buffer);
7457       */
7458       --skgautam Bug#5260198
7459       -- bug 5402377 -- start
7460       OPEN downpymnt_recvr_csr(p_kle_id);
7461       FETCH downpymnt_recvr_csr INTO l_downpymnt_recvr;
7462       CLOSE downpymnt_recvr_csr;
7463 
7464       IF ((l_downpymnt_recvr IS NULL) OR (l_downpymnt_recvr = 'VENDOR')) THEN
7465         -- bug 5402377 -- end
7466         l_amount_buffer := okl_seeded_functions_pvt.line_capital_reduction(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7467 
7468         --END:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7469         IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7470           l_amount_buffer := 0;
7471         ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7472           l_amount_buffer := 0;
7473         END IF;
7474         --bug 5500032 --start
7475       ELSIF(l_downpymnt_recvr = 'LESSOR') THEN
7476         l_amount_buffer := 0;
7477         --bug 5500032 -- end
7478         -- bug 5402377 -- start
7479       END IF;
7480       -- bug 5402377 -- END
7481     EXCEPTION
7482       WHEN OTHERS THEN
7483         l_amount_buffer := 0;
7484     END;
7485 
7486     l_amount := l_amount - l_amount_buffer;
7487 
7488     --------------------------------------------------
7489     -- Contract Trade In
7490     --------------------------------------------------
7491     BEGIN
7492       --START:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7493       /*
7494             OKL_EXECUTE_FORMULA_PUB.execute(
7495               p_api_version   => l_api_version,
7496               p_init_msg_list => l_init_msg_list,
7497               x_return_status => x_return_status,
7498               x_msg_count     => x_msg_count,
7499               x_msg_data      => x_msg_data,
7500               p_formula_name  => 'CONTRACT_CAPREDUCTION',
7501               p_contract_id   => p_khr_id,
7502               p_line_id       => p_kle_id,
7503               x_value         => l_amount_buffer);
7504       */
7505       --skgautam Bug#5260198
7506       l_amount_buffer := okl_seeded_functions_pvt.line_tradein(p_chr_id => p_khr_id, p_line_id => p_kle_id);
7507 
7508       --END:| 27-Feb-2006  cklee -- Fixed bug#5003962                                    |
7509       IF (x_return_status = okl_api.g_ret_sts_unexp_error) THEN
7510         l_amount_buffer := 0;
7511       ELSIF(x_return_status = okl_api.g_ret_sts_error) THEN
7512         l_amount_buffer := 0;
7513       END IF;
7514     EXCEPTION
7515       WHEN OTHERS THEN
7516         l_amount_buffer := 0;
7517     END;
7518 
7519     l_amount := l_amount - l_amount_buffer;
7520     -- end: okl.h: cklee
7521   END IF;
7522 
7523   IF (l_amount IS NULL) THEN
7524     l_amount := 0;
7525   END IF;
7526 
7527   RETURN l_amount;
7528 END get_contract_line_amt;
7529 
7530 
7531 
7532 ----------------------------------------------------------------------------
7533 FUNCTION get_contract_line_funded_amt(
7534   p_khr_id                       IN NUMBER                 -- contract hdr
7535  ,p_kle_id                       IN NUMBER                 -- contract line
7536  ,p_ref_type_code                IN VARCHAR2
7537 ) RETURN NUMBER
7538 IS
7539   l_amount NUMBER := 0;
7540 
7541   -- sjalasut, modified the cursor below to have khr_id referred from
7542   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7543   -- as part of OKLR12B disbursements project.
7544   CURSOR c (p_khr_id  NUMBER,
7545             p_kle_id  NUMBER)
7546   IS
7547 
7548     SELECT SUM(tl.amount)
7549       FROM okl_trx_ap_invoices_b th,
7550            okl_txl_ap_inv_lns_all_b tl
7551      WHERE th.id = tl.tap_id
7552        AND tl.khr_id = p_khr_id
7553        AND tl.kle_id = p_kle_id
7554 -- fixed bug 3007875
7555        AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED');
7556 
7557   -- sjalasut, modified the cursor below to have khr_id referred from
7558   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7559   -- as part of OKLR12B disbursements project.
7560   CURSOR ct (p_khr_id            NUMBER,
7561              p_kle_id            NUMBER,
7562              p_funding_type_code VARCHAR2)
7563   IS
7564     SELECT SUM(tl.amount)
7565       FROM okl_trx_ap_invoices_b th,
7566            okl_txl_ap_inv_lns_all_b tl
7567      WHERE th.id = tl.tap_id
7568        AND tl.khr_id = p_khr_id
7569        AND tl.kle_id = p_kle_id
7570        AND th.funding_type_code = p_funding_type_code
7571 -- fixed bug 3007875
7572        AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED');
7573 BEGIN
7574   IF (p_khr_id IS NULL OR p_khr_id = OKL_API.G_MISS_NUM) OR
7575      (p_kle_id IS NULL OR p_kle_id = OKL_API.G_MISS_NUM)
7576   THEN
7577     RETURN OKL_API.G_MISS_NUM;  -- error
7578   ELSIF (p_ref_type_code IS NULL OR p_ref_type_code = OKL_API.G_MISS_CHAR) THEN
7579     OPEN c (p_khr_id, p_kle_id);
7580     FETCH c INTO l_amount;
7581     CLOSE c;
7582   ELSE
7583     OPEN ct (p_khr_id, p_kle_id, p_ref_type_code);
7584     FETCH ct INTO l_amount;
7585     CLOSE ct;
7586   END IF;
7587   IF (l_amount IS NULL) THEN l_amount := 0; END IF;
7588   RETURN l_amount;
7589 END;
7590 ----------------------------------------------------------------------------
7591 
7592 -- get contract fund amount for asset lines
7593 FUNCTION get_contract_line_funded_amt(
7594   p_fund_id                       IN NUMBER                 -- fund hdr
7595   ,p_fund_type                    IN VARCHAR2               -- fund type code
7596 ) RETURN NUMBER
7597 IS
7598   l_amount NUMBER := 0;
7599   CURSOR c (p_fund_id  NUMBER)
7600   IS
7601     SELECT nvl(SUM(tl.amount),0)
7602       FROM okl_trx_ap_invoices_b th,
7603            okl_txl_ap_inv_lns_b tl
7604      WHERE tl.tap_id = th.id
7605        AND th.id = p_fund_id
7606 -- no need for this function. this is used for display at UI site only
7607 --       AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED')
7608   ;
7609   CURSOR c2 (p_fund_id  NUMBER)
7610   IS
7611     SELECT nvl(th.amount,0)
7612       FROM okl_trx_ap_invoices_b th
7613        WHERE th.id = p_fund_id
7614 -- no need for this function. this is used for display at UI site only
7615 --       AND th.TRX_STATUS_CODE NOT IN ('CANCELED', 'ERROR', 'REJECTED')
7616   ;
7617 
7618 BEGIN
7619     IF (p_fund_type in (G_ASSET_TYPE_CODE,
7620                         G_SUPPLIER_RETENTION_TYPE_CODE,
7621 --START:| 08-Jun-2006   cklee   Bug#5291817 get_contract_line_funded_amt() for
7622 --                              sync_header_amount()              |
7623                         G_ASSET_SUBSIDY,
7624 --END:| 08-Jun-2006   cklee   Bug#5291817 get_contract_line_funded_amt() for
7625 --                              sync_header_amount()              |
7626                         G_EXPENSE,
7627                         G_MANUAL_DISB)) THEN -- cklee 05/04/2004
7628       OPEN c (p_fund_id);
7629 
7630       FETCH c INTO l_amount;
7631       CLOSE c;
7632     ELSE
7633       OPEN c2(p_fund_id);
7634       FETCH c2 INTO l_amount;
7635       CLOSE c2;
7636     END IF;
7637 /*
7638     IF (p_fund_type = 'SUPPLIER_RETENTION') THEN
7639       l_amount := -(l_amount);
7640     END IF;
7641 */
7642 
7643   RETURN l_amount;
7644 
7645 END;
7646 ----------------------------------------------------------------------------
7647 
7648 FUNCTION is_funding_unique(
7649   p_vendor_id                    IN NUMBER
7650  ,p_fund_number                  IN VARCHAR2
7651  ,p_org_id                       IN NUMBER
7652 ) RETURN VARCHAR2
7653 IS
7654  l_result VARCHAR2(1) := OKL_API.G_TRUE;
7655  l_dummy  VARCHAR2(1) := '?';
7656 
7657   -- sjalasut, modified the cursor below to have khr_id referred from
7658   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7659   -- as part of OKLR12B disbursements project.
7660   CURSOR c (p_fund_number VARCHAR2,
7661             p_org_id number,
7662             p_vendor_id number)
7663   IS
7664     SELECT 'X'
7665       FROM okl_trx_ap_invoices_b th,
7666            okl_txl_ap_inv_lns_all_b tl,
7667            okc_k_headers_b chr,
7668            PO_VENDOR_SITES_ALL VS
7669      WHERE th.id = tl.tap_id
7670      AND tl.khr_id = chr.id
7671      AND th.ipvs_id = vs.vendor_site_id
7672      AND th.vendor_invoice_number = p_fund_number
7673      AND chr.AUTHORING_ORG_ID = p_org_id
7674      AND VS.vendor_id = p_vendor_id;
7675 
7676 BEGIN
7677   IF (p_vendor_id IS NULL OR p_vendor_id = OKL_API.G_MISS_NUM) OR
7678      (p_fund_number IS NULL OR p_fund_number = OKL_API.G_MISS_CHAR)
7679   THEN
7680     RETURN OKL_API.G_MISS_CHAR;
7681   END IF;
7682 
7683 --  OPEN c(p_vendor_id, p_fund_number);
7684   OPEN c(p_fund_number, p_org_id, p_vendor_id);
7685 
7686   FETCH c INTO l_dummy;
7687   CLOSE c;
7688   IF (l_dummy = 'X') THEN
7689     l_result := OKL_API.G_FALSE;
7690   END IF;
7691   RETURN l_result;
7692 END is_funding_unique;
7693 ----------------------------------------------------------------------------
7694 
7695 FUNCTION is_contract_line_unique(
7696   p_kle_id                       IN NUMBER -- contract_line_id
7697  ,p_fund_id                      IN NUMBER
7698  ,p_fund_line_id                 IN NUMBER
7699  ,p_mode                         IN VARCHAR2
7700  ,p_org_id                       IN NUMBER
7701 ) RETURN VARCHAR2
7702 IS
7703  l_result VARCHAR2(1) := OKL_API.G_TRUE;
7704  l_dummy  VARCHAR2(1) := '?';
7705  l_count  NUMBER(1) := 0;
7706 
7707   CURSOR c (p_fund_id NUMBER, p_kle_id NUMBER)
7708   IS
7709     SELECT 'X'
7710       FROM okl_txl_ap_inv_lns_b t
7711      WHERE t.tap_id = p_fund_id
7712      AND   t.kle_id = p_kle_id
7713   ;
7714 
7715   CURSOR c2 (p_fund_id NUMBER, p_kle_id NUMBER, p_fund_line_id NUMBER)
7716   IS
7717 
7718 
7719     SELECT 'X'
7720       FROM okl_txl_ap_inv_lns_b t
7721      WHERE t.tap_id = p_fund_id
7722      AND   t.kle_id = p_kle_id
7723 
7724      AND   t.id <> p_fund_line_id -- except itself
7725   ;
7726 
7727 BEGIN
7728 
7729   IF (p_kle_id IS NULL OR p_kle_id = OKL_API.G_MISS_NUM) OR
7730      (p_fund_id IS NULL OR p_fund_id = OKL_API.G_MISS_NUM)
7731   THEN
7732     RETURN OKL_API.G_MISS_NUM;
7733   END IF;
7734 
7735   IF (p_mode = 'C') THEN
7736 
7737     OPEN c(p_fund_id, p_kle_id);
7738     FETCH c INTO l_dummy;
7739     CLOSE c;
7740 
7741   ELSIF (p_mode = 'U') THEN
7742     OPEN c2(p_fund_id, p_kle_id, p_fund_line_id);
7743 
7744     FETCH c2 INTO l_dummy;
7745     CLOSE c2;
7746 
7747   END IF;
7748 
7749   IF (l_dummy = 'X') THEN
7750     l_result := OKL_API.G_FALSE;
7751   END IF;
7752 
7753 
7754   RETURN l_result;
7755 
7756 END is_contract_line_unique;
7757 
7758 --
7759 -- search duplicated kle_id in this table by specific okl_trx_ap_inv_lns_b.tap_id
7760 --
7761 FUNCTION is_kle_id_unique(
7762     p_tplv_tbl                 IN tplv_tbl_type
7763 ) RETURN VARCHAR2
7764 IS
7765   l_result   VARCHAR2(1) := OKL_API.G_TRUE;
7766   l_tplv_tbl tplv_tbl_type := p_tplv_tbl;
7767   i        NUMBER;
7768   j        NUMBER;
7769   l_count  NUMBER;
7770 
7771 
7772 BEGIN
7773 
7774   -- check duplicated kle_id in this table
7775   IF (p_tplv_tbl.COUNT > 0) THEN
7776     i := p_tplv_tbl.FIRST;
7777     LOOP
7778 
7779       -- inner being search loop
7780       l_count := 0;
7781       j := l_tplv_tbl.FIRST;
7782       LOOP
7783 
7784 
7785         IF (p_tplv_tbl(i).kle_id = l_tplv_tbl(j).kle_id) THEN
7786           l_count := l_count+1;
7787           IF (l_count > 1) THEN
7788             l_result := OKL_API.G_FALSE;
7789             EXIT;
7790 
7791           END IF;
7792         END IF;
7793 
7794         EXIT WHEN (j = l_tplv_tbl.LAST);
7795 --        j := l_tplv_tbl.NEXT(i);
7796         j := l_tplv_tbl.NEXT(j); --  cklee 10/3/2007 bug: 6318418
7797       END LOOP;
7798 
7799       -- exit if duplicated rows found
7800       IF (l_count > 0) THEN
7801         EXIT;
7802       END IF;
7803 
7804       EXIT WHEN (i = p_tplv_tbl.LAST);
7805       i := p_tplv_tbl.NEXT(i);
7806     END LOOP;
7807   END IF;
7808   RETURN l_result;
7809 
7810 END is_kle_id_unique;
7811 
7812 -------------------------------------------------------------------------------
7813 
7814 /*---------------------------------------------------------------------------+
7815 |                                                                            |
7816 |  FUNCTION: get_amount_prefunded                                            |
7817 |  DESC   : Sum of all approved requests for specfiic contract where type    |
7818 |          = prefunding                                                      |
7819 |  IN     : p_contract_id                                                    |
7820 |  OUT NOCOPY    : amount                                                    |
7821 |  HISTORY: 13-JAN-02 [email protected] -- Created                    |
7822 |                                                                            |
7823 *-------------------------------------------------------------------------- */
7824 FUNCTION get_amount_prefunded(
7825  p_contract_id                   IN NUMBER
7826  ,p_vendor_site_id               IN NUMBER
7827 
7828 ) RETURN NUMBER
7829 
7830 IS
7831   l_amount NUMBER := 0;
7832 
7833   -- sjalasut, modified the cursor below to have khr_id referred from
7834   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7835   -- as part of OKLR12B disbursements project.
7836   CURSOR c (p_contract_id  NUMBER)
7837   IS
7838   select nvl(sum(a.amount),0)
7839   from okl_trx_ap_invoices_b a
7840       ,okl_txl_ap_inv_lns_all_b b
7841   where a.id = b.tap_id
7842   and a.funding_type_code = 'PREFUNDING'
7843   and a.trx_status_code in ('APPROVED', 'PROCESSED')
7844   and b.khr_id = p_contract_id
7845 -- positive only
7846   and a.amount > 0
7847   ;
7848 
7849   -- sjalasut, modified the cursor below to have khr_id referred from
7850   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7851   -- as part of OKLR12B disbursements project.
7852   CURSOR c2 (p_contract_id  NUMBER, p_vendor_site_id  NUMBER)
7853   IS
7854   select nvl(sum(a.amount),0)
7855 
7856   from okl_trx_ap_invoices_b a
7857       ,okl_txl_ap_inv_lns_all_b b
7858   where a.id = b.tap_id
7859    and a.funding_type_code = 'PREFUNDING'
7860   and a.trx_status_code in ('APPROVED', 'PROCESSED')
7861   and b.khr_id = p_contract_id
7862   and a.ipvs_id = p_vendor_site_id
7863 -- positive only
7864   and a.amount > 0
7865   ;
7866 
7867 BEGIN
7868 
7869   IF (p_vendor_site_id IS NULL OR p_vendor_site_id = OKL_API.G_MISS_NUM) THEN
7870 
7871     OPEN c (p_contract_id);
7872     FETCH c INTO l_amount;
7873     CLOSE c;
7874   ELSE
7875     OPEN c2 (p_contract_id, p_vendor_site_id);
7876     FETCH c2 INTO l_amount;
7877     CLOSE c2;
7878   END IF;
7879 
7880   RETURN l_amount;
7881   EXCEPTION
7882     WHEN OTHERS THEN
7883       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
7884 
7885       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
7886                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
7887                           p_token1        => 'OKL_SQLCODE',
7888                           p_token1_value  => SQLCODE,
7889                           p_token2        => 'OKL_SQLERRM',
7890                           p_token2_value  => SQLERRM);
7891       RETURN 0;
7892 
7893 END;
7894 /*---------------------------------------------------------------------------+
7895 |                                                                            |
7896 |  FUNCTION: get_total_funded                                                |
7897 |  DESC   : Sum of all approved requests for specific contract               |
7898 |  IN     : p_contract_id                                                    |
7899 |  OUT NOCOPY    : amount                                                    |
7900 |  HISTORY: 13-JAN-02 [email protected] -- Created                    |
7901 |                                                                            |
7902 *-------------------------------------------------------------------------- */
7903 FUNCTION get_total_funded(
7904  p_contract_id                   IN NUMBER
7905  ,p_contract_line_id             IN NUMBER
7906 ) RETURN NUMBER
7907 
7908 IS
7909   l_amount NUMBER := 0;
7910   x_amount NUMBER := 0;
7911 
7912   -- sjalasut, modified the cursor below to have khr_id referred from
7913   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7914   -- as part of OKLR12B disbursements project.
7915   CURSOR c (p_contract_id  NUMBER)
7916   IS
7917   select nvl(sum(b.amount),0)
7918   from okl_trx_ap_invoices_b a,
7919        okl_txl_ap_inv_lns_all_b b
7920   where a.id = b.tap_id
7921   and a.trx_status_code in ('APPROVED', 'PROCESSED')
7922   and a.funding_type_code IN ('ASSET','EXPENSE', G_ASSET_SUBSIDY) -- cklee 11.5.10 subsidy
7923   and b.khr_id = p_contract_id
7924 UNION
7925   select nvl(sum(a.amount),0)
7926   from okl_trx_ap_invoices_b a
7927        ,okl_txl_ap_inv_lns_all_b b
7928   where a.id = b.tap_id
7929    and a.funding_type_code in ('PREFUNDING', 'BORROWER_PAYMENT') -- fixed bug# 2604862
7930   and a.trx_status_code in ('APPROVED', 'PROCESSED')
7931   and b.khr_id = p_contract_id
7932   ;
7933 
7934 BEGIN
7935 
7936   OPEN c (p_contract_id);
7937   LOOP
7938     FETCH c INTO l_amount;
7939     EXIT WHEN c%NOTFOUND;
7940     x_amount := x_amount + l_amount;
7941   END LOOP;
7942   CLOSE c;
7943 
7944   RETURN x_amount;
7945   EXCEPTION
7946     WHEN OTHERS THEN
7947       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
7948       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
7949                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
7950                           p_token1        => 'OKL_SQLCODE',
7951                           p_token1_value  => SQLCODE,
7952                           p_token2        => 'OKL_SQLERRM',
7953                           p_token2_value  => SQLERRM);
7954       RETURN 0;
7955 
7956 END;
7957 /*---------------------------------------------------------------------------+
7958 |                                                                            |
7959 |  FUNCTION: get_total_retention                                             |
7960 |  DESC   : Sum of all approved requests for specific contract               |
7961 |           where funding type = 'SUPPLIER_RETENTION'                        |
7962 |  IN     : p_contract_id                                                    |
7963 |  OUT NOCOPY    : amount                                                    |
7964 |  HISTORY: 13-JAN-02 [email protected] -- Created                    |
7965 |                                                                            |
7966 *-------------------------------------------------------------------------- */
7967 
7968 FUNCTION get_total_retention(
7969  p_contract_id                   IN NUMBER
7970  ,p_contract_line_id             IN NUMBER
7971 ) RETURN NUMBER
7972 IS
7973   l_amount NUMBER := 0;
7974 
7975   -- sjalasut, modified the cursor below to have khr_id referred from
7976   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
7977   -- as part of OKLR12B disbursements project.
7978   CURSOR c (p_contract_id  NUMBER)
7979   IS
7980   select nvl(sum(b.amount),0)
7981   from okl_trx_ap_invoices_b a,
7982        okl_txl_ap_inv_lns_all_b b
7983   where a.id = b.tap_id
7984   and b.khr_id = p_contract_id
7985   and a.trx_status_code in ('APPROVED', 'PROCESSED')
7986   and a.funding_type_code = 'SUPPLIER_RETENTION';
7987 
7988 BEGIN
7989 
7990   OPEN c (p_contract_id);
7991   FETCH c INTO l_amount;
7992   CLOSE c;
7993 
7994   RETURN l_amount;
7995   EXCEPTION
7996     WHEN OTHERS THEN
7997       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
7998       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
7999                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8000                           p_token1        => 'OKL_SQLCODE',
8001                           p_token1_value  => SQLCODE,
8002                           p_token2        => 'OKL_SQLERRM',
8003                           p_token2_value  => SQLERRM);
8004       RETURN 0;
8005 
8006 END;
8007 
8008 /*---------------------------------------------------------------------------+
8009 |                                                                            |
8010 |  FUNCTION: get_amount_borrowerPay                                          |
8011 |  DESC   : Sum of all approved requests for specific contract               |
8012 |           where funding type = 'BORROWER_PAYMENT'                          |
8013 |  IN     : p_contract_id                                                    |
8014 |  OUT NOCOPY    : amount                                                    |
8015 |  HISTORY: 02-OCT-02 [email protected] -- Created                    |
8016 |                                                                            |
8017 *-------------------------------------------------------------------------- */
8018 FUNCTION get_amount_borrowerPay(
8019  p_contract_id                   IN NUMBER
8020  ,p_contract_line_id             IN NUMBER
8021 ) RETURN NUMBER
8022 IS
8023   l_amount NUMBER := 0;
8024 
8025   -- sjalasut, modified the cursor below to have khr_id referred from
8026   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8027   -- as part of OKLR12B disbursements project.
8028   CURSOR c (p_contract_id  NUMBER)
8029   IS
8030   select nvl(sum(a.amount),0)
8031   from okl_trx_ap_invoices_b a
8032       ,okl_txl_ap_inv_lns_all_b b
8033   where a.id = b.tap_id
8034   and b.khr_id = p_contract_id
8035   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8036   and a.funding_type_code = 'BORROWER_PAYMENT';
8037 
8038 BEGIN
8039 
8040   OPEN c (p_contract_id);
8041   FETCH c INTO l_amount;
8042   CLOSE c;
8043 
8044   RETURN l_amount;
8045   EXCEPTION
8046     WHEN OTHERS THEN
8047       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8048 
8049       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8050                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8051                           p_token1        => 'OKL_SQLCODE',
8052                           p_token1_value  => SQLCODE,
8053                           p_token2        => 'OKL_SQLERRM',
8054                           p_token2_value  => SQLERRM);
8055       RETURN 0;
8056 
8057 
8058 END;
8059 
8060 /*---------------------------------------------------------------------------+
8061 |                                                                            |
8062 |  FUNCTION: get_amount_subsidy                                              |
8063 |  DESC   : Sum of all approved requests for specific contract               |
8064 |           where funding type = 'ASSET_SUBSIDY'                             |
8065 |  IN     : p_contract_id                                                    |
8066 |  OUT NOCOPY    : amount                                                    |
8067 |  HISTORY: 02-OCT-02 [email protected] -- Created                    |
8068 |                                                                            |
8069 *-------------------------------------------------------------------------- */
8070 FUNCTION get_amount_subsidy(
8071  p_contract_id                   IN NUMBER
8072  ,p_contract_line_id             IN NUMBER
8073 ) RETURN NUMBER
8074 IS
8075   l_amount NUMBER := 0;
8076 
8077   -- sjalasut, modified the cursor below to have khr_id referred from
8078   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8079   -- as part of OKLR12B disbursements project.
8080   CURSOR c (p_contract_id  NUMBER)
8081   IS
8082   select nvl(sum(a.amount),0)
8083   from okl_trx_ap_invoices_b a
8084       ,okl_txl_ap_inv_lns_all_b b
8085   where a.id = b.tap_id
8086   and a.khr_id = p_contract_id
8087   and a.trx_status_code in ('APPROVED', 'PROCESSED')
8088   and a.funding_type_code = G_ASSET_SUBSIDY;
8089 
8090   -- sjalasut, modified the cursor below to have khr_id referred from
8091   -- okl_txl_ap_inv_lns_all_b instead of okl_trx_ap_invoices_b. changes made
8092   -- as part of OKLR12B disbursements project.
8093   CURSOR c_sub (p_contract_id  NUMBER, p_contract_line_id NUMBER)
8094   IS
8095   select nvl(sum(subln.amount),0)
8096   from okl_trx_ap_invoices_b sub,
8097        okl_txl_ap_inv_lns_all_b subln
8098   where sub.id = subln.tap_id
8099   and subln.khr_id = p_contract_id
8100   and subln.kle_id = p_contract_line_id -- fixed asset ID
8101   and sub.trx_status_code in ('APPROVED', 'PROCESSED')
8102   and sub.funding_type_code = G_ASSET_SUBSIDY;
8103 
8104 
8105 BEGIN
8106 
8107   IF (p_contract_line_id IS NULL OR p_contract_line_id = OKL_API.G_MISS_NUM) THEN
8108 
8109     OPEN c (p_contract_id);
8110     FETCH c INTO l_amount;
8111     CLOSE c;
8112 
8113   ELSE
8114 
8115     OPEN c_sub (p_contract_id, p_contract_line_id);
8116     FETCH c_sub INTO l_amount;
8117     CLOSE c_sub;
8118 
8119   END IF;
8120 
8121   RETURN l_amount;
8122   EXCEPTION
8123     WHEN OTHERS THEN
8124       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8125       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8126                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8127                           p_token1        => 'OKL_SQLCODE',
8128                           p_token1_value  => SQLCODE,
8129                           p_token2        => 'OKL_SQLERRM',
8130                           p_token2_value  => SQLERRM);
8131       RETURN 0;
8132 
8133 
8134 END get_amount_subsidy;
8135 
8136 -------------------------------------------------------------------------
8137  FUNCTION get_funding_subsidy_amount(
8138     p_chr_id                       IN  NUMBER,
8139     p_asset_cle_id                 IN  NUMBER,
8140     p_vendor_site_id               IN  NUMBER
8141 ) RETURN NUMBER
8142 IS
8143 --    l_amount            NUMBER := 0;
8144     l_api_version       NUMBER := 1.0;
8145     l_init_msg_list     VARCHAR2(1) := OKL_API.G_FALSE;
8146     x_return_status     VARCHAR2(1);
8147     x_msg_count         NUMBER;
8148     x_msg_data          VARCHAR2(2000);
8149     x_subsidy_amount    NUMBER := 0;
8150     l_vendor_id         NUMBER := NULL;
8151 
8152 
8153   CURSOR cv1 (p_vendor_site_id NUMBER)
8154   IS
8155     select vendor_id from okx_vendor_sites_v
8156     where id1 = to_char(p_vendor_site_id)
8157   ;
8158 
8159 BEGIN
8160 
8161     IF (p_vendor_site_id IS NOT NULL) THEN
8162 
8163       OPEN cv1 (p_vendor_site_id);
8164       FETCH cv1 INTO l_vendor_id;
8165       CLOSE cv1;
8166     END IF;
8167 
8168     OKL_SUBSIDY_PROCESS_PVT.get_funding_subsidy_amount(
8169         p_api_version    => l_api_version,
8170         p_init_msg_list  => l_init_msg_list,
8171         x_return_status  => x_return_status,
8172         x_msg_count      => x_msg_count,
8173         x_msg_data       => x_msg_data,
8174         p_chr_id         => p_chr_id,
8175         p_asset_cle_id   => p_asset_cle_id,
8176         p_vendor_id      => l_vendor_id,
8177         x_subsidy_amount => x_subsidy_amount
8178 
8179     );
8180 
8181 
8182     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8183         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8184     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8185         RAISE OKL_API.G_EXCEPTION_ERROR;
8186     END IF;
8187 
8188     RETURN x_subsidy_amount;
8189 
8190   EXCEPTION
8191     WHEN OTHERS THEN
8192 
8193       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8194       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8195                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8196                           p_token1        => 'OKL_SQLCODE',
8197                           p_token1_value  => SQLCODE,
8198                           p_token2        => 'OKL_SQLERRM',
8199                           p_token2_value  => SQLERRM);
8200       RETURN NULL;
8201 
8202 END get_funding_subsidy_amount;
8203 
8204 
8205 -------------------------------------------------------------------------
8206  FUNCTION get_partial_subsidy_amount(
8207     p_asset_cle_id                 IN  NUMBER,
8208     p_req_fund_amount              IN  NUMBER
8209 ) RETURN NUMBER
8210 IS
8211     l_amount            NUMBER := 0;
8212     i                   NUMBER;
8213     l_api_version       NUMBER := 1.0;
8214     l_init_msg_list     VARCHAR2(1) := OKL_API.G_FALSE;
8215     x_return_status     VARCHAR2(1);
8216     x_msg_count         NUMBER;
8217     x_msg_data          VARCHAR2(2000);
8218     x_partial_subsidy_amount    NUMBER := 0;
8219     x_asbv_tbl OKL_SUBSIDY_PROCESS_PVT.asbv_tbl_type;
8220 
8221 BEGIN
8222 
8223 
8224     OKL_SUBSIDY_PROCESS_PVT.get_partial_subsidy_amount(
8225         p_api_version     => l_api_version,
8226         p_init_msg_list   => l_init_msg_list,
8227         x_return_status   => x_return_status,
8228         x_msg_count       => x_msg_count,
8229         x_msg_data        => x_msg_data,
8230         p_asset_cle_id    => p_asset_cle_id,
8231         p_req_fund_amount => p_req_fund_amount,
8232         x_asbv_tbl        => x_asbv_tbl
8233     );
8234 
8235     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
8236         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8237     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
8238         RAISE OKL_API.G_EXCEPTION_ERROR;
8239     END IF;
8240 
8241     IF (x_asbv_tbl.COUNT > 0) THEN
8242       i := x_asbv_tbl.FIRST;
8243 
8244       LOOP
8245 
8246         l_amount := l_amount + x_asbv_tbl(i).amount;
8247         EXIT WHEN (i = x_asbv_tbl.LAST);
8248         i := x_asbv_tbl.NEXT(i);
8249       END LOOP;
8250     END IF;
8251 
8252     RETURN l_amount;
8253 
8254   EXCEPTION
8255     WHEN OTHERS THEN
8256       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8257 
8258       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8259                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8260                           p_token1        => 'OKL_SQLCODE',
8261                           p_token1_value  => SQLCODE,
8262                           p_token2        => 'OKL_SQLERRM',
8263                           p_token2_value  => SQLERRM);
8264       RETURN NULL;
8265 
8266 END get_partial_subsidy_amount;
8267 
8268 
8269 -- cklee 04-May-2004
8270 /*---------------------------------------------------------------------------+
8271 |                                                                            |
8272 |  FUNCTION: get_amount_manu_disb                                            |
8273 |  DESC   : Sum of all approved requests for specific contract               |
8274 |           where funding type = 'MANUAL_DISB'                               |
8275 |  IN     : p_contract_id                                                    |
8276 |  OUT NOCOPY    : amount                                                    |
8277 |  HISTORY: 04-MAY-04 [email protected] -- Created                    |
8278 |                                                                            |
8279 *-------------------------------------------------------------------------- */
8280 FUNCTION get_amount_manu_disb(
8281  p_contract_id                   IN NUMBER
8282  ,p_contract_line_id             IN NUMBER
8283 ) RETURN NUMBER
8284 IS
8285   l_amount NUMBER := 0;
8286 
8287   CURSOR c_manu_disb (p_contract_id  NUMBER)
8288   IS
8289   -- select nvl(sum(decode(sub.invoice_type, 'CREDIT', -subln.amount, subln.amount)),0)
8290   -- sjalasut, commented the above select as part of OKLR12B disbursements project
8291   select nvl(sum(subln.amount),0)
8292   from okl_trx_ap_invoices_b sub,
8293        okl_txl_ap_inv_lns_b subln
8294   where sub.id = subln.tap_id
8295   and sub.trx_status_code in ('APPROVED', 'PROCESSED')
8296   and sub.funding_type_code = G_MANUAL_DISB
8297   and subln.khr_id = p_contract_id;
8298   -- sjalasut, commented the reference of khr_id. p_contract_id now joins with
8299   -- subln instead of sub. changes made as part of OKLR12B disbursements project
8300   -- and sub.khr_id = p_contract_id
8301 
8302 BEGIN
8303 
8304   OPEN c_manu_disb (p_contract_id);
8305   FETCH c_manu_disb INTO l_amount;
8306   CLOSE c_manu_disb;
8307 
8308   RETURN l_amount;
8309   EXCEPTION
8310     WHEN OTHERS THEN
8311       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8312       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8313                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8314                           p_token1        => 'OKL_SQLCODE',
8315                           p_token1_value  => SQLCODE,
8316                           p_token2        => 'OKL_SQLERRM',
8317                           p_token2_value  => SQLERRM);
8318       RETURN 0;
8319 
8320 
8321 END get_amount_manu_disb;
8322 
8323 -- strat: T and A 11/04/2004
8324 -- Total contract can be funded fee amount
8325 ----------------------------------------------------------------------------------
8326 -- Start of comments
8327 --
8328 -- Procedure Name  : Total contract can be funded fee amount
8329 -- Description     : Total contract can be funded fee amount for a fee line
8330 --                   by an given date
8331 --                   IN: p_contract_id is the lease contract ID
8332 --                   IN: p_fee_line_id is the lease contract fee line ID
8333 --                   IN: p_effective_date is the effective date of the total fee amount
8334 --                   OUT: x_value is the fee amount
8335 -- Business Rules  : x_value will be 0 if fee line has not meet the following requirements
8336 --                 : 1. Effective date greater than line start date
8337 --                      (or contract start date if line start date is null)
8338 --                   2. contract okc_k_headers_b.ste_code
8339 --                      in ('ENTERED', 'ACTIVE','SIGNED')
8340 --                   3. fee line is not passthrough
8341 --                   4. fee line is associated with vendor
8342 -- Business Rules  :
8343 -- Parameters      :
8344 -- Version         : 1.0
8345 -- End of comments
8346 ----------------------------------------------------------------------------------
8347  PROCEDURE contract_fee_canbe_funded(
8348     p_api_version                  IN NUMBER
8349    ,p_init_msg_list                IN VARCHAR2
8350    ,x_return_status                OUT NOCOPY VARCHAR2
8351    ,x_msg_count                    OUT NOCOPY NUMBER
8352    ,x_msg_data                     OUT NOCOPY VARCHAR2
8353    ,x_value                        OUT NOCOPY NUMBER
8354    ,p_contract_id                  IN NUMBER
8355    ,p_fee_line_id                  IN NUMBER
8356    ,p_effective_date               IN DATE
8357  )
8358 IS
8359   l_api_name        CONSTANT VARCHAR2(30) := 'contract_fee_canbe_funded';
8360   l_api_version     CONSTANT NUMBER       := 1.0;
8361   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
8362 
8363 BEGIN
8364   -- Set API savepoint
8365 
8366   SAVEPOINT contract_fee_canbe_funded_PVT;
8367 
8368   -- Check for call compatibility
8369   IF (NOT FND_API.Compatible_API_Call (l_api_version,
8370                                 	   p_api_version,
8371                                 	   l_api_name,
8372                                 	   G_PKG_NAME ))
8373   THEN
8374     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8375   END IF;
8376 
8377   -- Initialize message list if requested
8378   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
8379       FND_MSG_PUB.initialize;
8380 	END IF;
8381 
8382   -- Initialize API status to success
8383   x_return_status := OKL_API.G_RET_STS_SUCCESS;
8384 
8385 
8386 /*** Begin API body ****************************************************/
8387   x_value := get_chr_fee_canbe_funded_amt(
8388               p_contract_id    => p_contract_id
8389              ,p_fee_line_id    => p_fee_line_id
8390              ,p_effective_date => p_effective_date);
8391 
8392 /*** End API body ******************************************************/
8393 
8394   -- Get message count and if count is 1, get message info
8395 	FND_MSG_PUB.Count_And_Get
8396     (p_count          =>      x_msg_count,
8397      p_data           =>      x_msg_data);
8398 
8399 EXCEPTION
8400   WHEN OKL_API.G_EXCEPTION_ERROR THEN
8401     ROLLBACK TO contract_fee_canbe_funded;
8402     x_return_status := OKL_API.G_RET_STS_ERROR;
8403     FND_MSG_PUB.Count_And_Get
8404       (p_count         =>      x_msg_count,
8405        p_data          =>      x_msg_data);
8406 
8407   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
8408     ROLLBACK TO contract_fee_canbe_funded;
8409     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
8410     FND_MSG_PUB.Count_And_Get
8411       (p_count         =>      x_msg_count,
8412        p_data          =>      x_msg_data);
8413 
8414   WHEN OTHERS THEN
8415 	ROLLBACK TO contract_fee_canbe_funded;
8416       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8417 
8418       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
8419                           p_msg_name      => G_UNEXPECTED_ERROR,
8420                           p_token1        => G_SQLCODE_TOKEN,
8421                           p_token1_value  => SQLCODE,
8422                           p_token2        => G_SQLERRM_TOKEN,
8423                           p_token2_value  => SQLERRM);
8424       FND_MSG_PUB.Count_And_Get
8425         (p_count         =>      x_msg_count,
8426          p_data          =>      x_msg_data);
8427 
8428 
8429 END contract_fee_canbe_funded;
8430 
8431 ----------------------------------------------------------------------------------
8432 -- Start of comments
8433 --
8434 -- Procedure Name  : get_chr_fee_canbe_funded_amt
8435 -- Description     : Total contract can be funded fee amount for a fee line
8436 --                   by an given date
8437 --                   IN: p_contract_id is the lease contract ID
8438 --                   IN: p_fee_line_id is the lease contract fee line ID
8439 --                   IN: p_effective_date is the effective date of the total fee amount
8440 -- Business Rules  : x_value will be 0 if fee line has not meet the following requirements
8441 --                 : 1. Effective date greater than line start date
8442 --                      (or contract start date if line start date is null)
8443 --                   2. contract okc_k_headers_b.ste_code
8444 --                      in ('ENTERED', 'ACTIVE','SIGNED')
8445 --                   3. fee line is not passthrough
8446 --                   4. fee line is associated with vendor
8447 -- Parameters      :
8448 -- Version         : 1.0
8449 -- End of comments
8450 ----------------------------------------------------------------------------------
8451  FUNCTION get_chr_fee_canbe_funded_amt(
8452   p_contract_id                IN NUMBER                 -- contract hdr
8453   ,p_fee_line_id               IN NUMBER
8454   ,p_effective_date            IN DATE
8455 ) RETURN NUMBER
8456 IS
8457   l_amount NUMBER := 0;
8458   l_cle_amount NUMBER := 0;
8459   l_amount_per NUMBER := 0;
8460 
8461   l_cle_id NUMBER := 0;
8462   l_cle_start_date DATE;
8463   l_period NUMBER := 0;
8464   l_period_org NUMBER := 0;
8465   l_row_notfound   BOOLEAN;
8466 
8467   CURSOR c (p_contract_id  NUMBER,
8468             p_rle_code     VARCHAR2,
8469             p_fee_line_id  NUMBER)
8470   IS
8471 select nvl(cle.AMOUNT,0),
8472        cle.id,
8473        nvl(cle.start_date,k.start_date)
8474 from   OKL_K_LINES_FULL_V cle,
8475        okc_k_party_roles_b cpl,
8476        okc_line_styles_b ls,
8477        okc_k_headers_b k
8478 where  k.id           = cle.dnz_chr_id
8479 and    cle.dnz_chr_id = p_contract_id
8480 and    cle.lse_id     = ls.id
8481 and    ls.lty_code    = p_rle_code
8482 and    cle.id         = cpl.cle_id
8483 and    cpl.dnz_chr_id = p_contract_id
8484 and    cpl.chr_id     is null
8485 and    cpl.rle_code   = 'OKL_VENDOR'
8486 and    cle.id         = p_fee_line_id
8487 --and    cpl.object1_id1 = to_char(p_vendor_id)
8488 --and    cpl.object1_id2 = '#'
8489 -- Pass through check
8490 and not exists (select null
8491                 from   okc_rule_groups_v crg,
8492                        okc_rules_v cr
8493                 where  crg.dnz_chr_id = p_contract_id
8494                 and    crg.cle_id     = cle.id -- line id for rle_code
8495                 and    crg.id         = cr.rgp_id
8496                 and    crg.rgd_code   = 'LAPSTH')
8497 ;
8498 
8499 
8500 --
8501 -- get Number of Period
8502 --
8503 -- 1) take contract start date if cle start date is null
8504 -- 2) truncate pay period if less than 0
8505 --
8506   CURSOR c_period (p_contract_id    NUMBER,
8507                    p_cle_id         NUMBER,
8508                    p_effective_date DATE)
8509   IS
8510 select ceil(decode(cr.object1_id1, 'A', months_between(p_effective_date, nvl(cle.start_date, k.start_date))/12
8511                             , 'M', months_between(p_effective_date, nvl(cle.start_date, k.start_date))
8512                             , 'Q', months_between(p_effective_date, nvl(cle.start_date, k.start_date))/3
8513                             , 'S', months_between(p_effective_date, nvl(cle.start_date, k.start_date))/6
8514                             , months_between(p_effective_date, nvl(cle.start_date, k.start_date))))
8515 from   okc_rule_groups_v crg,
8516        okc_rules_v cr,
8517        OKL_K_LINES_FULL_V cle,
8518        okc_k_headers_b k
8519 where  crg.dnz_chr_id = p_contract_id
8520 and    cle.dnz_chr_id = k.id
8521 and    crg.id         = cr.rgp_id
8522 and    crg.rgd_code   = 'LAFEXP'
8523 and    crg.cle_id     = cle.id
8524 and    cr.RULE_INFORMATION_CATEGORY = 'LAFREQ'
8525 and    cle.id         = p_cle_id
8526 ;
8527 
8528 --
8529 -- get amount per period
8530 --
8531 --
8532   CURSOR c_amount_per (p_contract_id NUMBER,
8533                        p_cle_id NUMBER)
8534   IS
8535 select to_number(nvl(cr.RULE_INFORMATION1,'0'))
8536        ,to_number(nvl(cr.RULE_INFORMATION2,'0'))
8537 from   okc_rule_groups_v crg,
8538        okc_rules_v cr
8539 where  crg.dnz_chr_id = p_contract_id
8540 and    crg.id         = cr.rgp_id
8541 and    crg.rgd_code   = 'LAFEXP'
8542 and    cr.RULE_INFORMATION_CATEGORY = 'LAFEXP'
8543 and    crg.cle_id     = p_cle_id
8544 ;
8545 
8546 
8547 BEGIN
8548   IF ((p_contract_id IS NULL OR p_contract_id = OKL_API.G_MISS_NUM) or
8549       (p_fee_line_id IS NULL OR p_fee_line_id = OKL_API.G_MISS_NUM) or
8550       (p_effective_date IS NULL OR p_effective_date = OKL_API.G_MISS_DATE))
8551   THEN
8552     RETURN 0;  -- error
8553   ELSE
8554 
8555     ----------------------------------------------------
8556     -- FEE line
8557 
8558     ----------------------------------------------------
8559     OPEN c (p_contract_id, 'FEE', p_fee_line_id);
8560     LOOP
8561 
8562       FETCH c into l_cle_amount,
8563                    l_cle_id,
8564                    l_cle_start_date;
8565 
8566       EXIT WHEN c%NOTFOUND;
8567 
8568 
8569       OPEN c_amount_per (p_contract_id, l_cle_id);
8570       FETCH c_amount_per INTO l_period_org,
8571                               l_amount_per;
8572 
8573       l_row_notfound := c_amount_per%NOTFOUND;
8574       CLOSE c_amount_per;
8575 
8576       -- if recurring records doesn't exists
8577       IF (l_row_notfound) THEN
8578 
8579         -- either fee effective date or contract effective date <= p_effective_date
8580         IF ( l_cle_start_date <= p_effective_date ) THEN
8581           l_amount := l_amount + l_cle_amount;
8582 
8583         END IF;
8584 
8585       ELSE
8586 
8587         OPEN c_period (p_contract_id, l_cle_id, p_effective_date);
8588         FETCH c_period INTO l_period;
8589         CLOSE c_period;
8590 
8591         -- calculate only if period is positive
8592         IF (l_period > 0) THEN
8593 
8594           IF (l_period > l_period_org) THEN
8595             l_period := l_period_org;
8596           END IF;
8597           l_amount := l_amount + (l_amount_per * l_period);
8598         END IF;
8599 
8600 
8601       END IF;
8602 
8603     END LOOP;
8604 
8605     CLOSE c;
8606 
8607   END IF; -- end if p_contract_id check
8608 
8609   IF (l_amount IS NULL) THEN
8610     l_amount := 0;
8611   END IF;
8612 
8613   IF (okl_funding_pvt.is_chr_fundable_status(p_contract_id) = 0) THEN
8614     l_amount := 0;
8615   END IF;
8616 
8617   RETURN l_amount;
8618 
8619   EXCEPTION
8620     WHEN OTHERS THEN
8621       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8622       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8623                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8624                           p_token1        => 'OKL_SQLCODE',
8625                           p_token1_value  => SQLCODE,
8626                           p_token2        => 'OKL_SQLERRM',
8627                           p_token2_value  => SQLERRM);
8628       RETURN 0;
8629 
8630 
8631 END;
8632 
8633 -- end: T and A 11/04/2004
8634 
8635 -- strat: T and A bug#4151222
8636  FUNCTION is_contract_fully_funded(
8637   p_contract_id                IN NUMBER
8638  ) RETURN boolean
8639 IS
8640   l_flag               boolean := false;
8641   l_chr_canbe_funded   number;
8642   l_amount_oec         number;
8643   l_amount_expense     number;
8644   l_amount_hasbeen_oec number;
8645   l_amount_hasbeen_exp number;
8646 
8647 
8648 BEGIN
8649 
8650   l_amount_oec := get_chr_oec_canbe_funded(p_contract_id);
8651   l_amount_expense := get_chr_exp_canbe_funded_amt(p_contract_id);
8652   l_amount_hasbeen_oec := get_chr_oec_hasbeen_funded_amt(p_contract_id);
8653   l_amount_hasbeen_exp := get_chr_exp_hasbeen_funded_amt(p_contract_id);
8654 
8655   --
8656   -- has been funded may over totoal fundable amount due to asset termination
8657   --
8658   if ( l_amount_hasbeen_oec + l_amount_hasbeen_exp >=
8659        l_amount_oec + l_amount_expense) then
8660     l_flag := true;
8661   else
8662     l_flag := false;
8663   end if;
8664 
8665   RETURN l_flag;
8666 
8667   EXCEPTION
8668     WHEN OTHERS THEN
8669       --l_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8670       OKL_API.Set_Message(p_app_name      => OKL_API.G_APP_NAME,
8671                           p_msg_name      => 'OKL_UNEXPECTED_ERROR',
8672                           p_token1        => 'OKL_SQLCODE',
8673                           p_token1_value  => SQLCODE,
8674                           p_token2        => 'OKL_SQLERRM',
8675                           p_token2_value  => SQLERRM);
8676       RETURN false;
8677 
8678 END is_contract_fully_funded;
8679 ----------------------------------------------------------------------------------
8680 -- Start of comments
8681 --
8682 -- Procedure Name  : Is contract fully funded
8683 -- Description     : Is contract fully funded
8684 --                   IN: p_contract_id is the lease contract ID
8685 --                   OUT: x_value is the flag to indicate if contract is fully funded
8686 -- Business Rules  : x_value will be false if error occurred
8687 -- Parameters      :
8688 -- Version         : 1.0
8689 -- End of comments
8690 ----------------------------------------------------------------------------------
8691  PROCEDURE is_contract_fully_funded(
8692     p_api_version                  IN NUMBER
8693    ,p_init_msg_list                IN VARCHAR2 DEFAULT OKL_API.G_FALSE
8694    ,x_return_status                OUT NOCOPY VARCHAR2
8695    ,x_msg_count                    OUT NOCOPY NUMBER
8696    ,x_msg_data                     OUT NOCOPY VARCHAR2
8697    ,x_value                        OUT NOCOPY BOOLEAN
8698    ,p_contract_id                  IN NUMBER
8699  )
8700 IS
8701   l_api_name        CONSTANT VARCHAR2(30) := 'is_contract_fully_funded';
8702   l_api_version     CONSTANT NUMBER       := 1.0;
8703   l_return_status   VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
8704 
8705 BEGIN
8706   -- Set API savepoint
8707 
8708   SAVEPOINT is_contract_fully_funded_pvt;
8709 
8710   -- Check for call compatibility
8711   IF (NOT FND_API.Compatible_API_Call (l_api_version,
8712                                 	   p_api_version,
8713                                 	   l_api_name,
8714                                 	   G_PKG_NAME ))
8715   THEN
8716     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8717   END IF;
8718 
8719   -- Initialize message list if requested
8720   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
8721       FND_MSG_PUB.initialize;
8722 	END IF;
8723 
8724   -- Initialize API status to success
8725   x_return_status := OKL_API.G_RET_STS_SUCCESS;
8726 
8727 
8728 /*** Begin API body ****************************************************/
8729   x_value := is_contract_fully_funded(
8730               p_contract_id    => p_contract_id);
8731 
8732 /*** End API body ******************************************************/
8733 
8734   -- Get message count and if count is 1, get message info
8735 	FND_MSG_PUB.Count_And_Get
8736     (p_count          =>      x_msg_count,
8737      p_data           =>      x_msg_data);
8738 
8739 EXCEPTION
8740   WHEN OKL_API.G_EXCEPTION_ERROR THEN
8741     ROLLBACK TO is_contract_fully_funded;
8742     x_return_status := OKL_API.G_RET_STS_ERROR;
8743     FND_MSG_PUB.Count_And_Get
8744       (p_count         =>      x_msg_count,
8745        p_data          =>      x_msg_data);
8746 
8747   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
8748     ROLLBACK TO is_contract_fully_funded;
8749     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
8750     FND_MSG_PUB.Count_And_Get
8751       (p_count         =>      x_msg_count,
8752        p_data          =>      x_msg_data);
8753 
8754   WHEN OTHERS THEN
8755 	ROLLBACK TO is_contract_fully_funded;
8756       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8757 
8758       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
8759                           p_msg_name      => G_UNEXPECTED_ERROR,
8760                           p_token1        => G_SQLCODE_TOKEN,
8761                           p_token1_value  => SQLCODE,
8762                           p_token2        => G_SQLERRM_TOKEN,
8763                           p_token2_value  => SQLERRM);
8764       FND_MSG_PUB.Count_And_Get
8765         (p_count         =>      x_msg_count,
8766          p_data          =>      x_msg_data);
8767 
8768 
8769 END is_contract_fully_funded;
8770 
8771 
8772 -- end: T and A bug#4151222
8773 
8774 --Added procedure get_checklist_source as part of bug 5912358, Funding OA Migration Issues
8775  ----------------------------------------------------------------------------------
8776 -- Start of comments
8777 --
8778 -- Procedure Name  : get_checklist_source
8779 -- Description     : Returns checklist source details whether contract was originated from lease app, whether checklist exists or not and get source checklist template.
8780 --                   IN: p_chr_id is the contract ID
8781 --                   OUT: x_lease_app_found returns where contract was originated from leaseapp or not
8782 --                   OUT: x_lease_app_list_found returns whether lease checklist exists or not
8783 --                   OUT: x_funding_checklist_tpl returns source checklist template ID
8784 --                   OUT: x_lease_app_id returns lease application id
8785 --                   OUT: x_credit_id returns credit template id
8786 -- Business Rules  :
8787 -- Parameters      :
8788 -- Version         : 1.0
8789 -- End of comments
8790 ----------------------------------------------------------------------------------
8791  PROCEDURE get_checklist_source(
8792     p_api_version                  IN NUMBER
8793    ,p_init_msg_list                IN VARCHAR2 DEFAULT OKL_API.G_FALSE
8794    ,x_return_status                OUT NOCOPY VARCHAR2
8795    ,x_msg_count                    OUT NOCOPY NUMBER
8796    ,x_msg_data                     OUT NOCOPY VARCHAR2
8797    ,p_chr_id                        IN okc_k_headers_b.id%type
8798    ,x_lease_app_found       OUT NOCOPY VARCHAR2
8799    ,x_lease_app_list_found OUT NOCOPY VARCHAR2
8800    ,x_funding_checklist_tpl OUT NOCOPY okc_rules_b.rule_information2%TYPE
8801    ,x_lease_app_id          OUT NOCOPY NUMBER
8802    ,x_credit_id                OUT NOCOPY NUMBER
8803  ) IS
8804 
8805   l_lease_app_id number := OKC_API.G_MISS_NUM;
8806   l_lease_app_found boolean := FALSE;
8807   l_dummy number;
8808   l_lease_app_list_found boolean := FALSE;
8809   l_credit_id okc_k_headers_b.id%TYPE;
8810   l_funding_checklist_tpl okc_rules_b.rule_information2%TYPE := OKC_API.G_MISS_CHAR;
8811 
8812  --------------------------------------------------------------------------------------------
8813 -- Checklists link check
8814 --------------------------------------------------------------------------------------------
8815 CURSOR c_checklists (p_credit_id  NUMBER)
8816   IS
8817   select rule.rule_information2
8818   from okc_rules_b rule
8819   where rule.dnz_chr_id = p_credit_id
8820   and   rule.rule_information_category = G_CREDIT_CHKLST_TPL_RULE1
8821   ;
8822 
8823 ---------------------------------------------------------------------------------------------------------
8824 -- check if the contract was created from a lease application
8825 ---------------------------------------------------------------------------------------------------------
8826 CURSOR c_lease_app (p_chr_id okc_k_headers_b.id%type)
8827 IS
8828   select chr.ORIG_SYSTEM_ID1
8829 from  okc_k_headers_b chr
8830 where ORIG_SYSTEM_SOURCE_CODE = G_OKL_LEASE_APP
8831 and   chr.id = p_chr_id
8832 ;
8833 
8834 cursor c_lease_app_list_exists (p_lease_app_id number) is
8835 select 1
8836 from OKL_CHECKLIST_DETAILS chk
8837      ,okl_checklists hdr
8838 where chk.ckl_id = hdr.id
8839 and hdr.CHECKLIST_OBJ_ID = p_lease_app_id
8840 and chk.INST_CHECKLIST_TYPE = 'FUNDING'
8841 ;
8842 
8843  BEGIN
8844 
8845   OPEN c_lease_app(p_chr_id);
8846   FETCH c_lease_app INTO l_lease_app_id;
8847   l_lease_app_found := c_lease_app%FOUND;
8848   CLOSE c_lease_app;
8849 
8850 
8851   IF l_lease_app_id IS NOT NULL THEN
8852     OPEN c_lease_app_list_exists(l_lease_app_id);
8853     FETCH c_lease_app_list_exists INTO l_dummy;
8854     l_lease_app_list_found := c_lease_app_list_exists%FOUND;
8855     CLOSE c_lease_app_list_exists;
8856   END IF;
8857 
8858   IF NOT l_lease_app_found THEN
8859 -- get credit line id
8860 -- If contract credit line has been changed, but funding requests
8861 -- still have not been apporved. We need to re-generate list from the
8862 -- new credit line
8863     l_credit_id := OKL_CREDIT_PUB.get_creditline_by_chrid(p_chr_id);
8864 
8865 -- get source checklist template ID
8866     OPEN c_checklists(l_credit_id);
8867     FETCH c_checklists INTO l_funding_checklist_tpl;
8868     CLOSE c_checklists;
8869   END IF;
8870   IF (l_lease_app_found) THEN
8871 	x_lease_app_found := 'TRUE';
8872   ELSE
8873 	x_lease_app_found := 'FALSE';
8874   END IF;
8875   IF (l_lease_app_list_found) THEN
8876 	x_lease_app_list_found := 'TRUE';
8877   ELSE
8878 	x_lease_app_list_found := 'FALSE';
8879   END IF;
8880   x_funding_checklist_tpl := l_funding_checklist_tpl;
8881   x_lease_app_id := l_lease_app_id;
8882   x_credit_id := l_credit_id;
8883   x_return_status := 'S';
8884  EXCEPTION
8885      WHEN G_EXCEPTION_HALT_VALIDATION THEN
8886       x_return_status := OKL_API.G_RET_STS_ERROR;
8887     WHEN OTHERS THEN
8888       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
8889       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
8890                           p_msg_name      => G_UNEXPECTED_ERROR,
8891                           p_token1        => G_SQLCODE_TOKEN,
8892                           p_token1_value  => SQLCODE,
8893                           p_token2        => G_SQLERRM_TOKEN,
8894                           p_token2_value  => SQLERRM);
8895 
8896  END get_checklist_source;
8897 
8898 -- start: 24-May-2005  cklee okl.h Lease App IA Authoring
8899 ----------------------------------------------------------------------------------
8900 -- Start of comments
8901 --
8902 -- Procedure Name  : update_checklist_function
8903 -- Description     : This API will execute function for each item and
8904 --                   update the execution results for the function.
8905 -- Business Rules  :
8906 -- Parameters      :
8907 -- Version         : 1.0
8908 -- End of comments
8909 ----------------------------------------------------------------------------------
8910  PROCEDURE update_checklist_function(
8911     p_api_version                  IN NUMBER
8912    ,p_init_msg_list                IN VARCHAR2
8913    ,x_return_status                OUT NOCOPY VARCHAR2
8914    ,x_msg_count                    OUT NOCOPY NUMBER
8915    ,x_msg_data                     OUT NOCOPY VARCHAR2
8916    ,p_fund_req_id                  IN  NUMBER
8917  ) is
8918   l_api_name         CONSTANT VARCHAR2(30) := 'update_checklist_function';
8919   l_api_version      CONSTANT NUMBER       := 1.0;
8920   i                  NUMBER;
8921   l_return_status    VARCHAR2(1) := OKL_API.G_RET_STS_SUCCESS;
8922   l_dummy  number;
8923 
8924   l_row_not_found boolean := false;
8925 
8926   lp_rulv_tbl        okl_funding_checklist_pvt.rulv_tbl_type;
8927   lx_rulv_tbl        okl_funding_checklist_pvt.rulv_tbl_type;
8928   plsql_block        VARCHAR2(500);
8929 
8930   lp_return_status   okl_funding_checklists_uv.FUNCTION_VALIDATE_RSTS%type;
8931   lp_fund_rst        okl_funding_checklists_uv.FUNCTION_VALIDATE_RSTS%type;
8932   lp_msg_data        okl_funding_checklists_uv.FUNCTION_VALIDATE_MSG%type;
8933   l_contract_id      okl_funding_checklists_uv.KHR_ID%type;
8934 
8935 -- get checklist template attributes
8936 cursor c_clist_funs (p_fund_req_id varchar2) is
8937 --start modified abhsaxen for performance SQLID 20562504
8938 SELECT rult.ID,  rult.DNZ_CHR_ID khr_id,
8939 fun.source function_source
8940 FROM OKC_RULES_B RULT,
8941 OKL_DATA_SRC_FNCTNS_B FUN
8942 WHERE rult.rule_information_category = 'LAFCLD'
8943 and rult.object1_id1 = p_fund_req_id
8944 and rult.RULE_INFORMATION9 = fun.Id   ;
8945 --end modified abhsaxen for performance SQLID 20562504
8946 
8947 begin
8948   -- Set API savepoint
8949   SAVEPOINT update_checklist_function;
8950 
8951   -- Check for call compatibility
8952   IF (NOT FND_API.Compatible_API_Call (l_api_version,
8953                                 	   p_api_version,
8954                                 	   l_api_name,
8955                                 	   G_PKG_NAME ))
8956   THEN
8957     RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
8958   END IF;
8959 
8960   -- Initialize message list if requested
8961   IF (FND_API.to_Boolean(p_init_msg_list)) THEN
8962       FND_MSG_PUB.initialize;
8963 	END IF;
8964 
8965   -- Initialize API status to success
8966   x_return_status := OKL_API.G_RET_STS_SUCCESS;
8967 
8968 
8969 /*** Begin API body ****************************************************/
8970     ------------------------------------------------------------------------
8971     -- execute function for each to do item and save the return to each row
8972     ------------------------------------------------------------------------
8973     i := 0;
8974     --modified abhsaxen pass p_fund_req_id in varchar2 format for performannce SQLID 20562504
8975     FOR r_this_row IN c_clist_funs (TO_CHAR(p_fund_req_id)) LOOP
8976 
8977       BEGIN
8978 
8979         l_contract_id := r_this_row.khr_id;
8980 --START:| 02-Mar-2006  cklee -- Fixed bug#5068910                                    |
8981 --        plsql_block := 'BEGIN :l_rtn := '|| r_this_row.FUNCTION_SOURCE ||'(:l_contract_id); END;';
8982 --        EXECUTE IMMEDIATE plsql_block USING OUT lp_return_status, l_contract_id;
8983         plsql_block := 'BEGIN :l_rtn := '|| r_this_row.FUNCTION_SOURCE ||'(:l_contract_id, :l_fund_req_id); END;';
8984         EXECUTE IMMEDIATE plsql_block USING OUT lp_return_status, l_contract_id, p_fund_req_id;
8985 --END:| 02-Mar-2006  cklee -- Fixed bug#5068910                                    |
8986 
8987         IF lp_return_status = 'P' THEN
8988           lp_fund_rst := 'PASSED';
8989           lp_msg_data := 'Passed';
8990         ELSIF lp_return_status = 'F' THEN
8991           lp_fund_rst := 'FAILED';
8992           lp_msg_data := 'Failed';
8993         ELSE
8994           lp_fund_rst := 'ERROR';
8995           lp_msg_data := r_this_row.FUNCTION_SOURCE || ' returns: ' || lp_return_status;
8996         END IF;
8997 
8998       EXCEPTION
8999         WHEN OKL_API.G_EXCEPTION_ERROR THEN
9000           lp_fund_rst := 'ERROR';
9001           FND_MSG_PUB.Count_And_Get
9002             (p_count         =>      x_msg_count,
9003              p_data          =>      x_msg_data);
9004           lp_msg_data := substr('Application error: ' || x_msg_data, 240);
9005 
9006         WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
9007           lp_fund_rst := 'ERROR';
9008           FND_MSG_PUB.Count_And_Get
9009             (p_count         =>      x_msg_count,
9010              p_data          =>      x_msg_data);
9011           lp_msg_data := substr('Unexpected application error: ' || x_msg_data, 240);
9012 
9013         WHEN OTHERS THEN
9014           lp_fund_rst := 'ERROR';
9015           lp_msg_data := substr('Unexpected system error: ' || SQLERRM, 240);
9016 
9017       END;
9018 
9019       lp_rulv_tbl(i).ID := r_this_row.ID;
9020       lp_rulv_tbl(i).RULE_INFORMATION7 := lp_fund_rst;
9021       lp_rulv_tbl(i).RULE_INFORMATION8 := lp_msg_data;
9022       i := i + 1;
9023 
9024     END LOOP;
9025 
9026     IF lp_rulv_tbl.count > 0 THEN
9027 
9028       okl_funding_checklist_pvt.update_funding_chklst(
9029           p_api_version    => p_api_version,
9030           p_init_msg_list  => p_init_msg_list,
9031           x_return_status  => x_return_status,
9032           x_msg_count      => x_msg_count,
9033           x_msg_data       => x_msg_data,
9034           p_rulv_tbl       => lp_rulv_tbl,
9035           x_rulv_tbl       => lx_rulv_tbl);
9036 
9037       If (x_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) then
9038         raise OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
9039       Elsif (x_return_status = OKC_API.G_RET_STS_ERROR) then
9040         raise OKC_API.G_EXCEPTION_ERROR;
9041       End If;
9042     END IF;
9043 
9044 /*** End API body ******************************************************/
9045 
9046   -- Get message count and if count is 1, get message info
9047 	FND_MSG_PUB.Count_And_Get
9048     (p_count          =>      x_msg_count,
9049      p_data           =>      x_msg_data);
9050 
9051 EXCEPTION
9052   WHEN OKL_API.G_EXCEPTION_ERROR THEN
9053     ROLLBACK TO update_checklist_function;
9054     x_return_status := OKL_API.G_RET_STS_ERROR;
9055     FND_MSG_PUB.Count_And_Get
9056       (p_count         =>      x_msg_count,
9057        p_data          =>      x_msg_data);
9058 
9059   WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
9060     ROLLBACK TO update_checklist_function;
9061     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
9062     FND_MSG_PUB.Count_And_Get
9063       (p_count         =>      x_msg_count,
9064        p_data          =>      x_msg_data);
9065 
9066   WHEN OTHERS THEN
9067 
9068 	ROLLBACK TO update_checklist_function;
9069       x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR ;
9070       OKL_API.Set_Message(p_app_name      => G_APP_NAME,
9071                           p_msg_name      => G_UNEXPECTED_ERROR,
9072                           p_token1        => G_SQLCODE_TOKEN,
9073                           p_token1_value  => SQLCODE,
9074                           p_token2        => G_SQLERRM_TOKEN,
9075                           p_token2_value  => SQLERRM);
9076       FND_MSG_PUB.Count_And_Get
9077         (p_count         =>      x_msg_count,
9078          p_data          =>      x_msg_data);
9079 
9080 end update_checklist_function;
9081 -- end: 24-May-2005  cklee okl.h Lease App IA Authoring
9082 
9083 ----------------------------------------------------------------------------
9084 /*
9085 -- zrehman, 10/13/2006
9086 -- START, Added PROCEDURE to get Funding Summary
9087 */
9088 -- Start of comments
9089 --
9090 -- Procedure Name  : get_fund_summary
9091 -- Description     : public_procedure, gets fund summary by making calls to
9092 --                   functions in OKL_FUND_SMRY_PVT
9093 -- Business Rules  :
9094 -- Parameters      : contract ID
9095 -- Version         : 1.0
9096 -- End of comments
9097 --
9098 
9099 PROCEDURE get_fund_summary(
9100                 p_api_version       IN NUMBER,
9101                 p_init_msg_list     IN VARCHAR2 DEFAULT OKL_API.G_FALSE,
9102                 x_return_status     OUT NOCOPY VARCHAR2,
9103                 x_msg_count         OUT NOCOPY NUMBER,
9104                 x_msg_data          OUT NOCOPY VARCHAR2,
9105                 p_contract_id       IN NUMBER,
9106 		x_fnd_rec           OUT NOCOPY fnd_rec_type
9107                 ) IS
9108 
9109 BEGIN
9110    x_fnd_rec.TOTAL_FUNDABLE_AMOUNT := OKL_FUNDING_PVT.get_chr_canbe_funded(p_contract_id);
9111    x_fnd_rec.TOTAL_PRE_FUNDED := OKL_FUNDING_PVT.get_amount_prefunded(p_contract_id);
9112    x_fnd_rec.TOTAL_ASSETS_FUNDED := OKL_FUNDING_PVT.get_chr_oec_hasbeen_funded_amt(p_contract_id);
9113    x_fnd_rec.TOTAL_EXPENSES_FUNDED := OKL_FUNDING_PVT.get_chr_exp_hasbeen_funded_amt(p_contract_id);
9114    x_fnd_rec.TOTAL_ADJUSTMENTS := OKL_FUNDING_PVT.get_chr_funded_adjs(p_contract_id);
9115    x_fnd_rec.TOTAL_REMAINING_TO_FUND := OKL_FUNDING_PVT.get_chr_canbe_funded_rem(p_contract_id);
9116    x_fnd_rec.TOTAL_SUPPLIER_RETENTION := OKL_FUNDING_PVT.get_total_retention(p_contract_id);
9117    x_fnd_rec.TOTAL_BORROWER_PAYMENTS := OKL_FUNDING_PVT.get_amount_borrowerPay(p_contract_id);
9118    x_fnd_rec.TOTAL_SUBSIDIES_FUNDED := OKL_FUNDING_PVT.get_amount_subsidy(p_contract_id);
9119    x_fnd_rec.TOTAL_MANUAL_DISBURSEMENT := OKL_FUNDING_PVT.get_amount_manu_disb(p_contract_id);
9120 
9121 
9122 
9123     EXCEPTION
9124       WHEN OTHERS THEN
9125         x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
9126         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
9127 END get_fund_summary;
9128 
9129 /*
9130 -- zrehman, 10/13/2006
9131 -- END, PROCEDURE to get Funding Summary
9132 */
9133 
9134 
9135 END OKL_FUNDING_PVT;