DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKL_LA_SALES_TAX_PVT

Source


1 PACKAGE BODY OKL_LA_SALES_TAX_PVT AS
2  /* $Header: OKLRSTXB.pls 120.69.12020000.4 2013/03/22 05:08:44 racheruv ship $ */
3 
4   G_MODULE VARCHAR2(255) := 'okl.stream.esg.okl_esg_transport_pvt';
5   G_DEBUG_ENABLED CONSTANT VARCHAR2(10) := OKL_DEBUG_PUB.CHECK_LOG_ENABLED;
6   G_IS_DEBUG_STATEMENT_ON BOOLEAN;
7 
8   -- Common Cursors
9 
10   -- Cursor to check if the sales tax parameter(switch) is set to yes.
11   CURSOR l_tax_system_info IS
12   SELECT nvl(tax_upfront_yn,'N')
13   FROM   okl_system_params;
14   l_upfront_tax_yn VARCHAR2(1);
15 
16 
17   -- R12 - START
18 
19   -- Global Cursor to get contract header linformation
20   CURSOR Product_csr (p_contract_id OKC_K_HEADERS_B.ID%TYPE)
21   IS
22   SELECT  khr.pdt_id                    product_id
23          ,NULL                          product_name
24          ,khr.sts_code                  contract_status
25          ,khr.start_date                start_date
26          ,khr.currency_code             currency_code
27          ,khr.authoring_org_id          authoring_org_id
28          ,khr.currency_conversion_rate  currency_conversion_rate
29          ,khr.currency_conversion_type  currency_conversion_type
30          ,khr.currency_conversion_date  currency_conversion_date
31          ,khr.scs_code                  scs_code
32   FROM    okl_k_headers_full_v  khr
33   WHERE   khr.id = p_contract_id;
34 
35   g_Product_rec      Product_csr%ROWTYPE;
36 
37   -- R12 - END
38 
39   --R12 Bug# 14033244 Start
40 
41    -- Global Cursor to get the Product related Parameters
42     CURSOR Product_Params_csr(p_product_id NUMBER)
43     IS
44     SELECT  name	                 product_name,
45             deal_type	                 book_classification_code,
46             tax_owner	                 tax_owner_code,
47             description	                 description,
48             revenue_recognition_method   revenue_recognition_method,
49             interest_calculation_basis   interest_calculation_basis
50     FROM    okl_product_parameters_v
51     WHERE   id	  =    p_product_id;
52 
53     g_Product_Params_rec	Product_Params_csr%ROWTYPE;
54 
55   --R12 Bug# 14033244 End
56 
57 
58   -- Cursor to get Transaction type and status descriptions
59   CURSOR fnd_lookups_csr( lkp_type VARCHAR2, mng VARCHAR2 ) IS
60   select description,  lookup_code
61   from   fnd_lookup_values
62   where  language     = 'US'
63   AND    lookup_type  = lkp_type
64   AND    meaning      = mng;
65 
66   -- The following cursor checks if BILLED is selected on T and C, user should not
67   -- select asset upfront tax to be FINANCED for some assets and CAPITALIZED
68   -- for some. Both Financed and Capitalized fee lines cannot be created
69   -- together for sales tax. We allow either Financed or Capitalize fee lines only
70 
71   -- Bug# 6512668: Exclude asset lines in Abandoned status
72   CURSOR check_feetype_csr (p_contract_id IN NUMBER)
73   IS
74   SELECT NVL(rule_information11,'BILLED') feetype
75   FROM   okc_rules_b rl,
76          okc_rule_groups_b rgp,
77          okc_k_lines_b cle
78   WHERE  rl.dnz_chr_id                    = p_contract_id
79   AND    rl.rule_information_category     = 'LAASTX'
80   AND    NVL(rule_information11,'BILLED') <> 'BILLED'
81   AND    rgp.id = rl.rgp_id
82   AND    cle.id = rgp.cle_id
83   AND    cle.sts_code <> 'ABANDONED';
84 
85   -- Cursor to get rule values
86   CURSOR rule_info_csr (p_contract_id IN NUMBER)
87   IS
88   SELECT rule_information1,
89          rule_information2,
90          rule_information3,
91          rule_information4,
92          rule_information5
93   FROM   okc_rules_b rl
94   WHERE  rl.dnz_chr_id = p_contract_id
95   AND    rl.rule_information_category = 'LASTPR';
96 
97   -- The following checks if all the lines have the asset upfront tax as
98   -- selected on T and C. for instance,user might select  FINANCED or CAPITALIZED
99   -- on T and C, but might do the opposite for all asset level taxes. If user
100   -- selects FINANCED on T and C but bills all line taxes, we do not need
101   -- to create fee lines at all
102 
103   -- Bug# 6512668: Exclude asset lines in Abandoned status
104   CURSOR check_lines_csr (p_contract_id IN NUMBER, l_fee_type IN VARCHAR2 )
105   IS
106   SELECT count(NVL(rule_information11, l_fee_type))
107   FROM   okc_rules_b rl,
108          okc_rule_groups_b rgp,
109          okc_k_lines_b cle
110   WHERE  rl.dnz_chr_id = p_contract_id
111   AND    rl.rule_information_category = 'LAASTX'
112   AND    NVL(rule_information11, l_fee_type) = l_fee_type
113   AND    rgp.id = rl.rgp_id
114   AND    cle.id = rgp.cle_id
115   AND    cle.sts_code <> 'ABANDONED';
116 
117   -- Cursor to get financed or capitalized tax amount if
118   -- default tax treatment is FINANCED or CAPITALIZED respectively
119 
120   -- Bug# 6512668: Exclude asset lines in Abandoned status
121   CURSOR get_fee_amt_csr1(p_contract_id IN NUMBER, p_feetype IN VARCHAR2)
122   IS
123   SELECT NVL(SUM(NVL(TOTAL_TAX,0)),0)
124   FROM   okl_tax_sources txs,
125            okc_rule_groups_b rg1,
126            okc_rules_b rl1,
127            okc_rule_groups_b rg2,
128            okc_rules_b rl2,
129            okc_k_lines_b cle
130   WHERE  txs.KHR_ID = p_contract_id
131   AND    txs.khr_id = rg1.dnz_chr_id
132   AND    txs.kle_id = rg1.cle_id
133   AND    txs.khr_id = rl1.dnz_chr_id
134   AND    rg1.rgd_code = 'LAASTX'
135   AND    rg1.id       = rl1.rgp_id
136   AND    rl1.rule_information_category = 'LAASTX'
137   AND    (rl1.rule_information11 = p_feetype
138          OR
139          rl1.rule_information11 IS NULL)
140   AND    txs.TAX_LINE_STATUS_CODE = 'ACTIVE'
141   AND    txs.TAX_CALL_TYPE_CODE = 'UPFRONT_TAX'
142   AND    txs.KHR_ID     = rg2.dnz_chr_id
143   AND    txs.KHR_ID     = rl2.dnz_chr_id
144   AND    rg2.id         = rl2.rgp_id
145   AND    rg2.rgd_code   = 'LAHDTX'
146   AND    rl2.rule_information_category = 'LASTPR'
147   AND    rl2.rule_information1 = p_feetype
148   AND    cle.id = rg1.cle_id
149   AND    cle.sts_code <> 'ABANDONED';
150 
151   -- Cursor to get financed or capitalized tax Amount if
152   -- default tax treatment is BILLED
153 
154   -- Bug# 6512668: Exclude asset lines in Abandoned status
155   CURSOR get_fee_amt_csr2 (p_contract_id IN NUMBER, p_feetype IN VARCHAR2)
156   IS
157   SELECT NVL(SUM(NVL(TOTAL_TAX,0)),0)
158   FROM    okl_tax_sources txs
159            ,okc_rule_groups_b rg1
160            ,okc_rules_b rl1
161            ,okc_k_lines_b cle
162   WHERE  txs.KHR_ID = p_Contract_id
163   AND    txs.khr_id = rg1.dnz_chr_id
164   AND    txs.kle_id = rg1.cle_id
165   AND    txs.khr_id = rl1.dnz_chr_id
166   AND    rg1.rgd_code = 'LAASTX'
167   AND    rg1.id       = rl1.rgp_id
168   AND    rl1.rule_information_category = 'LAASTX'
169   AND    rl1.rule_information11 = p_feetype
170   AND    txs.TAX_LINE_STATUS_CODE = 'ACTIVE'
171   AND    txs.TAX_CALL_TYPE_CODE = 'UPFRONT_TAX'
172   AND    cle.id = rg1.cle_id
173   AND    cle.sts_code <> 'ABANDONED'; -- Bug 5005269
174 
175 
176   -- Use this cursor for billing trx when T and C asset upfront tax is
177   -- Financed or Capitalized
178   -- R12B eBtax: Added check that upfront tax amount is not zero
179 
180   -- Bug# 6512668: Exclude asset lines in Abandoned status
181   CURSOR billing_required_csr1(p_contract_id IN NUMBER,
182                                p_transaction_id IN NUMBER) IS
183   SELECT 1
184   FROM  okl_tax_sources txs,
185         okc_rule_groups_b rg1,
186         okc_rules_b rl1,
187         okc_k_lines_b cle
188   WHERE txs.khr_id = p_contract_id
189   AND   txs.trx_id = p_transaction_id
190   -- R12B ebtax: Active and Inactive lines will be billed during rebook
191   -- AND   txs.tax_line_status_code = 'ACTIVE'
192   AND   txs.tax_call_type_code = 'UPFRONT_TAX'
193   AND   NVL(txs.total_tax,0) <> 0
194   AND   rg1.dnz_chr_id = txs.khr_id
195   AND   rg1.cle_id = txs.kle_id
196   AND   rg1.rgd_code = 'LAASTX'
197   AND   rl1.dnz_chr_id = rg1.dnz_chr_id
198   AND   rl1.rgp_id = rg1.id
199   AND   rl1.rule_information_category = 'LAASTX'
200   AND   rl1.rule_information11 = 'BILLED'
201   AND   cle.id = rg1.cle_id
202   AND   cle.sts_code <> 'ABANDONED';
203 
204   -- Use this cursor for billing trx when T and C asset upfront tax is
205   -- Billed
206   -- R12B eBtax: Added check that upfront tax amount is not zero
207 
208   -- Bug# 6512668: Exclude asset lines in Abandoned status
209   CURSOR billing_required_csr2(p_contract_id IN NUMBER,
210                                p_transaction_id IN NUMBER) IS
211   SELECT 1
212   FROM okl_tax_sources txs,
213        okc_rule_groups_b rg1,
214        okc_rules_b rl1,
215        okc_k_lines_b cle
216   WHERE txs.khr_id   = p_contract_id
217   AND   txs.trx_id = p_transaction_id
218   -- R12B ebtax: Active and Inactive lines will be billed during rebook
219   -- AND   txs.tax_line_status_code = 'ACTIVE'
220   AND   txs.tax_call_type_code = 'UPFRONT_TAX'
221   AND   NVL(txs.total_tax,0) <> 0
222   AND   rg1.dnz_chr_id = txs.khr_id
223   AND   rg1.cle_id = txs.kle_id
224   AND   rg1.rgd_code = 'LAASTX'
225   AND   rl1.dnz_chr_id = rg1.dnz_chr_id
226   AND   rl1.rgp_id = rg1.id
227   AND   rl1.rule_information_category = 'LAASTX'
228   AND   NVL(rl1.rule_information11,'BILLED') = 'BILLED'
229   AND   cle.id = rg1.cle_id
230   AND   cle.sts_code <> 'ABANDONED';
231 
232   --Bug# 6939336
233   -- This cursor gets biling amount at contract header level
234   CURSOR contract_billing_csr(p_contract_id IN NUMBER,
235                               p_transaction_id IN NUMBER) IS
236   SELECT 1
237   FROM   okl_tax_sources     txs
238   WHERE  txs.khr_id = p_contract_id
239   AND    txs.trx_id = p_transaction_id
240   AND    txs.kle_id IS NULL
241   AND    txs.tax_line_status_code = 'ACTIVE'
242   AND    txs.tax_call_type_code = 'UPFRONT_TAX'
243   AND    NVL(txs.total_tax,0) <> 0;
244 
245   -- ER# 9327076
246   -- Check if present contract is a rebook copy
247   CURSOR c_chk_rbk_csr (chrid IN okc_k_headers_b.ID%TYPE) IS
248       SELECT '!'
249         FROM okc_k_headers_b CHR
250       WHERE CHR.ID = chrid
251 	      AND CHR.orig_system_source_code = 'OKL_REBOOK';
252 
253    -- Cursor to check if mass rebook is in process
254    CURSOR c_chk_mass_rbk_csr (p_chr_id IN NUMBER) IS
255       SELECT '!'
256         FROM   okc_k_headers_b chr
257       WHERE  chr.ID = p_chr_id
258        AND EXISTS (SELECT '1'
259                    FROM   okl_trx_contracts ktrx
260                    WHERE  ktrx.khr_id = chr.ID
261                           AND ktrx.tsu_code = 'ENTERED'
262                           AND ktrx.rbr_code IS NOT NULL
263                           AND ktrx.tcn_type = 'TRBK'
264                           --rkuttiya added for 12.1.1 Multi GAAP Project
265                           AND ktrx.representation_type = 'PRIMARY')
266        AND EXISTS (SELECT '1'
267                    FROM   okl_rbk_selected_contract rbk_khr
268                    WHERE  rbk_khr.khr_id = chr.ID
269                           AND rbk_khr.status <> 'PROCESSED');
270 
271   -- Global Variables
272   g_source_table                   VARCHAR2(30) := 'OKL_TRX_CONTRACTS';
273 
274   -- ER# 9327076  - Added new function to check for prior upfront tax calculation
275   FUNCTION check_prior_upfront_tax(p_chr_id  IN OKC_K_HEADERS_B.ID%TYPE)
276   RETURN BOOLEAN IS
277     CURSOR c_chk_upfront(cp_chr_id OKC_K_HEADERS_B.ID%TYPE) IS
278        SELECT 'x'
279         FROM   okl_tax_sources
280        WHERE  khr_id = cp_chr_id
281             AND tax_call_type_code = 'UPFRONT_TAX' ;
282     l_chk VARCHAR2(1) := '?';
283 
284   BEGIN
285 
286     OPEN c_chk_upfront(p_chr_id);
287 	  FETCH c_chk_upfront INTO l_chk;
288 	CLOSE c_chk_upfront;
289 
290     IF l_chk = '?' THEN
291        RETURN FALSE;
292     ELSE RETURN TRUE;
293     END IF;
294 
295   END check_prior_upfront_tax;
296 
297     -- 16522428 14/3/13
298     -- Check if a release contract has now become a mixed contract during rebook
299 
300   FUNCTION check_rel_mxd_ctr(p_chr_id  IN OKC_K_HEADERS_B.ID%TYPE)
301   RETURN BOOLEAN IS
302 
303     l_chk VARCHAR2(1) := '?';
304 
305     CURSOR c_chk_mxd_ctr (cp_chr_id OKC_K_HEADERS_B.ID%TYPE) IS
306 	SELECT 'Y'
307 	FROM   okc_k_headers_b CHR
308 	WHERE   nvl(chr.orig_system_source_code,'XXXX') <> 'OKL_RELEASE'
309 	and     chr.ID = cp_chr_id
310 	AND     exists ( SELECT '1'
311 			FROM okl_k_lines kle
312 		       ,okc_k_lines_b cleb
313 			WHERE cleb.dnz_chr_id = chr.id
314 			AND cleb.chr_id = chr.id
315 			AND cleb.id = kle.id
316 			AND cleb.lse_id = 33
317 			AND cleb.sts_code <> 'ABANDONED'
318 			AND nvl(kle.re_lease_yn,'N') = 'Y')
319 	AND    exists  ( SELECT '1'
320 			FROM okl_k_lines kle
321 		       ,okc_k_lines_b cleb
322 			WHERE cleb.dnz_chr_id = chr.id
323 			AND cleb.chr_id = chr.id
324 			AND cleb.id = kle.id
325 			AND cleb.lse_id = 33
326 			AND cleb.sts_code <> 'ABANDONED'
327 			AND nvl(kle.re_lease_yn,'N') = 'N');
328 
329     CURSOR c_chk_orig_rls_ctr(cp_chr_id OKC_K_HEADERS_B.ID%TYPE) IS
330         SELECT 'Y'
331 	FROM   okc_k_headers_b CHR
332 	WHERE   nvl(chr.orig_system_source_code,'XXXX') <> 'OKL_RELEASE'
333 	and     chr.ID =  ( SELECT orig_system_id1
334                           FROM   okc_k_headers_b
335                           WHERE  id = cp_chr_id )
336 	AND     exists ( SELECT '1'
337 			FROM okl_k_lines kle
338 		       ,okc_k_lines_b cleb
339 			WHERE cleb.dnz_chr_id = chr.id
340 			AND cleb.chr_id = chr.id
341 			AND cleb.id = kle.id
342 			AND cleb.lse_id = 33
343 			AND cleb.sts_code <> 'ABANDONED'
344 			AND nvl(kle.re_lease_yn,'N') = 'Y')
345 	AND  not  exists  ( SELECT '1'
346 			FROM okl_k_lines kle
347 		       ,okc_k_lines_b cleb
348 			WHERE cleb.dnz_chr_id = chr.id
349 			AND cleb.chr_id = chr.id
350 			AND cleb.id = kle.id
351 			AND cleb.lse_id = 33
352 			AND cleb.sts_code <> 'ABANDONED'
353 			AND nvl(kle.re_lease_yn,'N') = 'N');
354 
355    l_mxd_ast     VARCHAR2(1) DEFAULT '?';
356    l_rls_ast     VARCHAR2(1) DEFAULT '?';
357 
358 
359   BEGIN
360 
361     OPEN c_chk_mxd_ctr (p_chr_id);
362     FETCH c_chk_mxd_ctr INTO l_mxd_ast;
363     IF c_chk_mxd_ctr%NOTFOUND THEN
364        NULL;
365     END IF;
366     CLOSE c_chk_mxd_ctr;
367 
368     OPEN c_chk_orig_rls_ctr (p_chr_id);
369     FETCH c_chk_orig_rls_ctr INTO l_rls_ast;
370     IF c_chk_orig_rls_ctr%NOTFOUND THEN
371        NULL;
372     END IF;
373     CLOSE c_chk_orig_rls_ctr;
374 
375     IF NVL(l_mxd_ast,'N') = 'Y' THEN
376       IF NVL(l_rls_ast , 'N') = 'Y' THEN
377          RETURN TRUE;
378       ELSE
379          RETURN FALSE;
380       END IF;
381     ELSE
382       RETURN FALSE;
383     END IF;
384         RETURN FALSE;
385 
386   END check_rel_mxd_ctr;
387     -- 16522428 14/3/13
388     -- Check if a release contract has now become a mixed contract during rebook
389 
390   -- Start of comments
391   --
392   -- Procedure Name  : create_sales_tax_rules
393   -- Description     : Creates sales tax rules after the asset is created. If
394   --                   header sales tax information does not exist, the
395   --                   default values will get set
396   -- Business Rules  :
397   -- Parameters      :
398   -- Version         : 1.0
399   -- End of comments
400 
401 
402   PROCEDURE create_sales_tax_rules(
403             p_api_version    	     IN  NUMBER,
404             p_init_msg_list          IN  VARCHAR2 DEFAULT OKC_API.G_FALSE,
405             x_return_status          OUT NOCOPY VARCHAR2,
406             x_msg_count              OUT NOCOPY NUMBER,
407             x_msg_data               OUT NOCOPY VARCHAR2,
408             p_chr_id                 IN  NUMBER,
409             p_cle_id                 IN  NUMBER)
410 
411   IS
412     l_rgpv_rec          rgpv_rec_type;
413     l_rulv_rec          rulv_rec_type;
414     lx_rgpv_rec         rgpv_rec_type;
415     lx_rulv_rec         rulv_rec_type;
416 
417     l_chr_id  okc_k_headers_b.id%TYPE;
418     l_cle_id  okc_k_lines_v.id%TYPE;
419     l_rgp_id  NUMBER;
420 
421     lv_k_rule_information1        okc_rules_v.rule_information1%TYPE;
422     lv_k_rule_information3        okc_rules_v.rule_information3%TYPE;
423     lv_k_rule_information4        okc_rules_v.rule_information4%TYPE;
424     lv_k_rule_information5        okc_rules_v.rule_information5%TYPE;
425     lv_k_rule_information6        okc_rules_v.rule_information6%TYPE;
426     lv_k_rule_information7        okc_rules_v.rule_information7%TYPE;
427 
428     CURSOR get_contract_deal_type(p_chr_id  okc_k_headers_b.id%TYPE) IS
429     select deal_type
430     from okl_k_headers
431     where deal_type = 'LOAN'
432     and id = p_chr_id;
433 
434     CURSOR get_contract_sales_tax_info(p_chr_id  okc_k_headers_b.id%TYPE) IS
435     SELECT rul.rule_information1, rul.rule_information3, rul.rule_information4,
436            rul.rule_information5, rul.rule_information6, rul.rule_information7
437     FROM  OKC_RULES_V rul,
438           OKC_RULE_GROUPS_V rgp
439     WHERE rul.rgp_id = rgp.id
440     AND   rgp.dnz_chr_id = p_chr_id
441     AND   rgp.cle_id is NULL
442     AND   rgp.rgd_code = 'LAHDTX'
443     AND   rul.rule_information_category = 'LASTCL';
444 
445     CURSOR l_hdr_upfront_tax(p_chr_id  okc_k_headers_b.id%TYPE) IS
446     SELECT rul.rule_information1
447     FROM  OKC_RULES_V rul,
448           OKC_RULE_GROUPS_V rgp
449     WHERE rul.rgp_id = rgp.id
450     AND   rgp.dnz_chr_id = p_chr_id
451     AND   rgp.cle_id is NULL
452     AND   rgp.rgd_code = 'LAHDTX'
453     AND   rul.rule_information_category = 'LASTPR';
454 
455     CURSOR l_asset_tax_rule_group(p_chr_id  okc_k_headers_b.id%TYPE,
456                                   p_cle_id  NUMBER) IS -- 5179119
457     SELECT id
458     FROM  OKC_RULE_GROUPS_B rgp
459     WHERE rgp.dnz_chr_id = p_chr_id
460     AND   rgp.cle_id is NOT NULL
461     AND   rgp.rgd_code = 'LAASTX'
462     AND   rgp.cle_id = p_cle_id; -- 5179119
463     l_asset_tax_rule_group_rec l_asset_tax_rule_group%ROWTYPE;
464     l_row_found BOOLEAN;
465 
466     l_api_name			CONSTANT VARCHAR2(30) := 'CREATE_SALES_TAX_RULES';
467 
468     l_flag			VARCHAR2(30) := 'XXXX';
469     l_deal_type         	okl_k_headers.deal_type%type;
470     l_orig_system_source_code   okc_k_headers_b.orig_system_source_code%type;
471     l_header_upfront_tax        okc_rules_v.rule_information1%TYPE;
472 
473   BEGIN
474 
475       x_return_status := OKL_API.G_RET_STS_SUCCESS;
476 
477       l_chr_id := p_chr_id;
478       l_cle_id := p_cle_id;
479 
480       IF okl_context.get_okc_org_id  IS NULL THEN
481         okl_context.set_okc_org_context(p_chr_id => l_chr_id);
482       END IF;
483 
484       -- call START_ACTIVITY to create savepoint, check compatibility
485       -- and initialize message list
486       x_return_status := OKL_API.START_ACTIVITY (
487                                l_api_name
488                                ,p_init_msg_list
489                                ,g_api_type
490                                ,x_return_status);
491 
492       -- check if activity started successfully
493       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
494         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
495       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
496         RAISE OKL_API.G_EXCEPTION_ERROR;
497       END IF;
498 
499       OPEN  l_hdr_upfront_tax(p_chr_id);
500       FETCH l_hdr_upfront_tax into l_header_upfront_tax;
501       CLOSE l_hdr_upfront_tax;
502 
503       -- get the contract header sales tax info to map to line sales tax
504       -- Also, get 'update line from contract header ' flag to check whether
505       -- to update with contract sales tax info
506 
507       OPEN  get_contract_sales_tax_info(l_chr_id);
508       FETCH get_contract_sales_tax_info into    lv_k_rule_information1,
509        						lv_k_rule_information3,
510                                                 lv_k_rule_information4,
511                                                 lv_k_rule_information5,
512                                                 lv_k_rule_information6,
513                                                 lv_k_rule_information7;
514       IF (get_contract_sales_tax_info%NOTFOUND) THEN
515         l_flag := 'DEFAULT';
516       END IF;
517       CLOSE get_contract_sales_tax_info;
518 
519       IF(NOT(NVL(lv_k_rule_information1,'N') = 'Y'))  THEN
520         l_flag := 'DEFAULT';
521       END IF;
522 
523       -- Create the rule group for sales Tax
524       l_rgpv_rec.rgd_code      :=  'LAASTX';
525       l_rgpv_rec.chr_id        :=  null;
526       l_rgpv_rec.dnz_chr_id    :=  l_chr_id;
527       l_rgpv_rec.cle_id        :=  l_cle_id;
528       l_rgpv_rec.rgp_type      :=  'KRG';
529 
530      --Bug#4990898 ramurt
531      OPEN l_asset_tax_rule_group(p_chr_id, p_cle_id); -- 5179119
532      FETCH l_asset_tax_rule_group INTO l_asset_tax_rule_group_rec;
533      l_row_found := l_asset_tax_rule_group%FOUND;
534      CLOSE l_asset_tax_rule_group;
535 
536       IF (l_row_found) THEN
537         l_rgp_id := l_asset_tax_rule_group_rec.id;
538       ELSE
539         OKL_RULE_PUB.create_rule_group(
540                 p_api_version       =>  p_api_version,
541                 p_init_msg_list     =>  p_init_msg_list,
542                 x_return_status     =>  x_return_status,
543                 x_msg_count         =>  x_msg_count,
544                 x_msg_data          =>  x_msg_data,
545                 p_rgpv_rec          =>  l_rgpv_rec,
546                 x_rgpv_rec          =>  lx_rgpv_rec);
547         l_rgp_id := lx_rgpv_rec.id;
548 
549         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
550             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
551         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
552             RAISE OKL_API.G_EXCEPTION_ERROR;
553         END IF;
554       END IF;
555 
556       -- Create the sales Tax rule
557         l_rulv_rec.dnz_chr_id        		:= l_chr_id;
558         l_rulv_rec.rgp_id            		:= l_rgp_id;
559         l_rulv_rec.rule_information_category    := 'LAASTX';
560         l_rulv_rec.sfwt_flag         		:= 'N';
561         l_rulv_rec.std_template_yn   		:= 'N';
562         l_rulv_rec.warn_yn           		:= 'N';
563         l_rulv_rec.template_yn       		:= 'N';
564         l_rulv_rec.rule_information5 		:= 'N';
565 
566         -- if the header sales info doesnt exist set default values
567 
568         IF(l_flag = 'DEFAULT') THEN
569 
570          l_deal_type := NULL;
571          OPEN get_contract_deal_type(l_chr_id);
572          FETCH get_contract_deal_type into l_deal_type;
573          CLOSE get_contract_deal_type;
574 
575          l_rulv_rec.rule_information6 := 'N';
576 
577          IF( l_deal_type IS NULL) THEN -- contract deal type is not loan
578            l_rulv_rec.rule_information7 := 'N'; -- Yes for Loan
579          ELSE
580            l_rulv_rec.rule_information7 := 'Y'; -- Yes for Loan
581          END IF;
582 
583          l_rulv_rec.rule_information8 := 'N';
584          --l_rulv_rec.rule_information9 := lv_k_rule_information6; -- user specified
585          --l_rulv_rec.rule_information10 := lv_k_rule_information7; -- user specified
586 
587         ELSE
588 
589          l_rulv_rec.rule_information6  := lv_k_rule_information3;
590          l_rulv_rec.rule_information7  := lv_k_rule_information4;
591          l_rulv_rec.rule_information8  := lv_k_rule_information5;
592          l_rulv_rec.rule_information9  := lv_k_rule_information6;
593          l_rulv_rec.rule_information10 := lv_k_rule_information7;
594          l_rulv_rec.rule_information11 := l_header_upfront_tax;
595 
596         END IF;
597 
598         OKL_RULE_PUB.create_rule(
599               p_api_version         => p_api_version,
600               p_init_msg_list       => p_init_msg_list,
601               x_return_status       => x_return_status,
602               x_msg_count           => x_msg_count,
603               x_msg_data            => x_msg_data,
604               p_rulv_rec            => l_rulv_rec,
605               x_rulv_rec            => lx_rulv_rec);
606 
607         IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
608           RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
609         ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
610           RAISE OKL_API.G_EXCEPTION_ERROR;
611         END IF;
612 
613       OKL_API.END_ACTIVITY (x_msg_count  => x_msg_count,
614                             x_msg_data   => x_msg_data);
615   EXCEPTION
616     WHEN OKL_API.G_EXCEPTION_ERROR THEN
617       IF get_contract_sales_tax_info%ISOPEN THEN
618         CLOSE get_contract_sales_tax_info;
619       END IF;
620       IF l_hdr_upfront_tax%ISOPEN THEN
621         CLOSE l_hdr_upfront_tax;
622       END IF;
623       IF l_asset_tax_rule_group%ISOPEN THEN
624         CLOSE l_asset_tax_rule_group;
625       END IF;
626 
627 
628       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
629 			p_api_name  => l_api_name,
630 			p_pkg_name  => g_pkg_name,
631 			p_exc_name  => 'OKC_API.G_RET_STS_ERROR',
632 			x_msg_count => x_msg_count,
633 			x_msg_data  => x_msg_data,
634 			p_api_type  => g_api_type);
635 
636     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
637       IF get_contract_sales_tax_info%ISOPEN THEN
638         CLOSE get_contract_sales_tax_info;
639       END IF;
640       IF l_hdr_upfront_tax%ISOPEN THEN
641         CLOSE l_hdr_upfront_tax;
642       END IF;
643       IF l_asset_tax_rule_group%ISOPEN THEN
644         CLOSE l_asset_tax_rule_group;
645       END IF;
646       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
647 			p_api_name  => l_api_name,
648 			p_pkg_name  => g_pkg_name,
649 			p_exc_name  => 'OKC_API.G_RET_STS_UNEXP_ERROR',
650 			x_msg_count => x_msg_count,
651 			x_msg_data  => x_msg_data,
652 			p_api_type  => g_api_type);
653 
654     WHEN OTHERS THEN
655       IF get_contract_sales_tax_info%ISOPEN THEN
656         CLOSE get_contract_sales_tax_info;
657       END IF;
658       IF l_asset_tax_rule_group%ISOPEN THEN
659         CLOSE l_asset_tax_rule_group;
660       END IF;
661       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
662 			p_api_name  => l_api_name,
663 			p_pkg_name  => g_pkg_name,
664 			p_exc_name  => 'OTHERS',
665 			x_msg_count => x_msg_count,
666 			x_msg_data  => x_msg_data,
667 			p_api_type  => g_api_type);
668   END create_sales_tax_rules;
669 
670   -- Procedure Name  : sync_contract_sales_tax
671   -- Description     : Sync the contract sales tax rule information with all the assets
672   --                   , if the flag 'Update lines from contract header' at the  header
673   --                   level is checked.
674   -- Business Rules  :
675   -- Parameters      :
676   -- Version         : 1.0
677   -- End of comments
678   PROCEDURE sync_contract_sales_tax(
679             p_api_version    	     IN  NUMBER,
680             p_init_msg_list          IN  VARCHAR2 DEFAULT OKC_API.G_FALSE,
681             x_return_status          OUT NOCOPY VARCHAR2,
682             x_msg_count              OUT NOCOPY NUMBER,
683             x_msg_data               OUT NOCOPY VARCHAR2,
684             p_chr_id                 IN  NUMBER)
685   IS
686     l_rulv_rec          rulv_rec_type;
687     lx_rulv_rec         rulv_rec_type;
688 
689     ln_cle_id                     okc_k_lines_v.id%TYPE;
690 
691     ln_rule_id                    okc_rules_v.id%TYPE;
692     lv_asset_k_rule_information5  okc_rules_v.rule_information5%TYPE;
693     l_ak_prompt                   varchar2(100);
694     l_rebook_yn                   varchar2(1) := 'N';
695     l_orig_system_id1             okc_k_lines_b.orig_system_id1%type;
696 
697     lv_k_rule_information1        okc_rules_v.rule_information1%TYPE := null;
698     lv_k_rule_information2        okc_rules_v.rule_information1%TYPE := null;
699     lv_k_rule_information3        okc_rules_v.rule_information3%TYPE := null;
700     lv_k_rule_information4        okc_rules_v.rule_information4%TYPE := null;
701     lv_k_rule_information5        okc_rules_v.rule_information5%TYPE := null;
702     lv_k_rule_information6        okc_rules_v.rule_information6%TYPE := null;
703     lv_k_rule_information7        okc_rules_v.rule_information7%TYPE := null;
704     l_header_upfront_tax          okc_rules_v.rule_information1%TYPE;
705 
706     l_api_name	CONSTANT VARCHAR2(30) := 'SYNC_CONTRACT_SALES_TAX';
707 
708     CURSOR get_contract_sales_tax_info(p_chr_id  okc_k_headers_b.id%TYPE) IS
709     SELECT rul.rule_information1, rul.rule_information3, rul.rule_information4,
710            rul.rule_information5, rul.rule_information6, rul.rule_information7
711     FROM  OKC_RULES_V rul,
712           OKC_RULE_GROUPS_V rgp
713     WHERE rul.rgp_id = rgp.id
714     AND   rgp.dnz_chr_id = p_chr_id
715     AND   rgp.cle_id is NULL
716     AND   rgp.rgd_code = 'LAHDTX'
717     AND   rul.rule_information_category = 'LASTCL';
718 
719     CURSOR get_contract_lines(p_chr_id  okc_k_headers_b.id%TYPE) IS
720     SELECT cle.id
721     FROM   OKC_K_LINES_V cle,
722            OKC_LINE_STYLES_V lse
723     WHERE  cle.lse_id = lse.id
724     AND    lse.lty_code = 'FREE_FORM1'
725     AND    lse.lse_type = 'TLS'
726     AND    cle.dnz_chr_id = p_chr_id;
727 --    AND    cle.orig_system_id1 IS NULL;      -- for rebook check
728 
729     CURSOR get_rebook_contract_csr(p_chr_id  okc_k_headers_b.id%TYPE) IS
730      select 'Y'
731      from okc_k_headers_b
732      where id = p_chr_id
733      and nvl(orig_system_source_code,'XXX')='OKL_REBOOK';
734 
735     CURSOR get_asset_sales_tax_info(p_chr_id  okc_k_headers_b.id%TYPE,
736                                        p_cle_id  okc_k_lines_v.id%TYPE) IS
737     SELECT rul.id, rul.rule_information5, cle.ORIG_SYSTEM_ID1
738     FROM  OKC_RULES_V rul,
739           OKC_RULE_GROUPS_V rgp,
740 		  okc_k_lines_b cle
741     WHERE rul.rgp_id = rgp.id
742     AND   rgp.dnz_chr_id = p_chr_id
743     AND   rgp.cle_id = p_cle_id
744     AND   rgp.rgd_code = 'LAASTX'
745     AND   rul.rule_information_category = 'LAASTX'
746 	AND   cle.id = rgp.cle_id
747 	AND   cle.id = p_cle_id;
748 
749     CURSOR get_sales_tax_prc_info(p_chr_id  okc_k_headers_b.id%TYPE) IS
750     SELECT rul.rule_information1, rul.rule_information2,
751            rul.rule_information3, rul.rule_information4, rul.rule_information4
752     FROM  OKC_RULES_V rul,
753           OKC_RULE_GROUPS_V rgp
754     WHERE rul.rgp_id = rgp.id
755     AND   rgp.dnz_chr_id = p_chr_id
756     AND   rgp.cle_id is NULL
757     AND   rgp.rgd_code = 'LAHDTX'
758     AND   rul.rule_information_category = 'LASTPR';
759 
760     CURSOR get_segment_prompt(p_ri okc_rules_b.rule_information1%TYPE) IS
761     select col.form_left_prompt
762     from fnd_descr_flex_col_usage_vl col,
763          okc_rule_defs_v  rdef
764     where col.application_id = 540
765     and col.application_id = rdef.application_id
766     and col.descriptive_flexfield_name=rdef.descriptive_flexfield_name
767     and col.descriptive_flex_context_code=rdef.rule_code
768     and rdef.rule_code = 'LASTPR'
769     and col.application_column_name = p_ri;
770 
771   BEGIN
772     IF (G_DEBUG_ENABLED = 'Y') THEN
773       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
774     END IF;
775 
776       x_return_status := OKL_API.G_RET_STS_SUCCESS;
777 
778       -- Bug 5129446
779       /*
780       l_upfront_tax_yn := 'N';
781       OPEN l_tax_system_info;
782       FETCH l_tax_system_info INTO l_upfront_tax_yn;
783       CLOSE l_tax_system_info; */
784 
785       IF okl_context.get_okc_org_id  IS NULL THEN
786         okl_context.set_okc_org_context(p_chr_id => p_chr_id);
787       END IF;
788       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
789               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'sync here1');
790       END IF;
791       -- call START_ACTIVITY to create savepoint, check compatibility
792       -- and initialize message list
793       x_return_status := OKL_API.START_ACTIVITY (
794                                l_api_name
795                                ,p_init_msg_list
796                                ,g_api_type
797                                ,x_return_status);
798 
799       -- check if activity started successfully
800       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
801         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
802       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
803         RAISE OKL_API.G_EXCEPTION_ERROR;
804       END IF;
805 
806       -- Bug 5129446
807       /*
808       OPEN  get_sales_tax_prc_info(p_chr_id);
809       FETCH get_sales_tax_prc_info into    lv_k_rule_information1,
810 					   lv_k_rule_information2,
811                                            lv_k_rule_information3,
812                                            lv_k_rule_information4;
813       CLOSE get_sales_tax_prc_info;
814       l_header_upfront_tax := lv_k_rule_information1;
815 
816       If((l_upfront_tax_yn = 'Y') AND l_header_upfront_tax is null) Then
817         x_return_status := OKL_API.G_RET_STS_ERROR;
818         OKL_API.set_message(
819               p_app_name     => G_APP_NAME,
820               p_msg_name     => 'OKL_QA_ST_HEADER_TAX');
821         raise OKL_API.G_EXCEPTION_ERROR;
822       End If;
823 
824       --If(lv_k_rule_information1 = G_BILLED and (lv_k_rule_information2 is null or lv_k_rule_information2 = OKL_API.G_MISS_CHAR)) Then
825       If((l_upfront_tax_yn = 'Y') AND (lv_k_rule_information2 is null) or (lv_k_rule_information2 = OKL_API.G_MISS_CHAR)) Then
826 
827          OPEN  get_segment_prompt('RULE_INFORMATION2');
828          FETCH get_segment_prompt into l_ak_prompt;
829          CLOSE get_segment_prompt;
830 
831          x_return_status := OKL_API.g_ret_sts_error;
832          OKL_API.SET_MESSAGE(      p_app_name => g_app_name
833    				, p_msg_name => 'OKL_REQUIRED_VALUE'
834   				, p_token1 => 'COL_NAME'
835    				, p_token1_value => l_ak_prompt
836    			   );
837          raise OKL_API.G_EXCEPTION_ERROR;
838 
839       --ElsIf(lv_k_rule_information1 = G_FINANCED and (lv_k_rule_information3 is null or lv_k_rule_information3 = OKL_API.G_MISS_CHAR)) Then
840       ElsIf((l_upfront_tax_yn) = 'Y' AND (lv_k_rule_information3 is null) or (lv_k_rule_information3 = OKL_API.G_MISS_CHAR)) Then
841 
842          OPEN  get_segment_prompt('RULE_INFORMATION3');
843          FETCH get_segment_prompt into l_ak_prompt;
844          CLOSE get_segment_prompt;
845 
846          x_return_status := OKL_API.g_ret_sts_error;
847          OKL_API.SET_MESSAGE(      p_app_name => g_app_name
848    				, p_msg_name => 'OKL_REQUIRED_VALUE'
849   				, p_token1 => 'COL_NAME'
850    				, p_token1_value => l_ak_prompt
851    			   );
852          raise OKL_API.G_EXCEPTION_ERROR;
853 
854       --ElsIf(lv_k_rule_information1 = G_CAPITALIZED and (lv_k_rule_information4 is null or lv_k_rule_information4 = OKL_API.G_MISS_CHAR)) Then
855       ElsIf((l_upfront_tax_yn = 'Y') AND (lv_k_rule_information4 is null) or (lv_k_rule_information4 = OKL_API.G_MISS_CHAR)) Then
856 
857          OPEN  get_segment_prompt('RULE_INFORMATION4');
858          FETCH get_segment_prompt into l_ak_prompt;
859          CLOSE get_segment_prompt;
860 
861          x_return_status := OKL_API.g_ret_sts_error;
862          OKL_API.SET_MESSAGE(      p_app_name => g_app_name
863          			, p_msg_name => 'OKL_REQUIRED_VALUE'
864         			, p_token1 => 'COL_NAME'
865          			, p_token1_value => l_ak_prompt
866          		   );
867          raise OKL_API.G_EXCEPTION_ERROR;
868 
869       End If;
870       */
871 
872       -- get the contract header sales tax info to map to line sales tax
873       -- Also, get 'update line from contract header ' flag to check whether to update
874       -- with contract sales tax info
875 
876       OPEN  get_contract_sales_tax_info(p_chr_id);
877       FETCH get_contract_sales_tax_info into    lv_k_rule_information1,
878       						lv_k_rule_information3,
879                                                 lv_k_rule_information4,
880                                                 lv_k_rule_information5,
881                                                 lv_k_rule_information6,
882                                                 lv_k_rule_information7;
883       IF (get_contract_sales_tax_info%NOTFOUND) THEN
884         RETURN;
885       END IF;
886       CLOSE get_contract_sales_tax_info;
887 
888       IF(NOT(nvl(lv_k_rule_information1,'N') = 'Y')) THEN
889         OKL_API.END_ACTIVITY (x_msg_count  => x_msg_count,
890                               x_msg_data   => x_msg_data);
891         RETURN;
892       END IF;
893 
894 	  l_rebook_yn := 'N';
895       OPEN get_rebook_contract_csr(p_chr_id  => p_chr_id);
896       FETCH get_rebook_contract_csr into l_rebook_yn;
897       CLOSE get_rebook_contract_csr;
898 
899 	  --- get all the contract lines and create/update
900       --  the lines with contract sales tax info
901       FOR r_get_contract_lines IN get_contract_lines(p_chr_id => p_chr_id) LOOP
902 
903        lv_asset_k_rule_information5 := null; -- Reset the value
904 
905         -- get 'update from contract' flag
906        OPEN get_asset_sales_tax_info(p_chr_id  => p_chr_id,
907                                          p_cle_id  => r_get_contract_lines.id);
908        FETCH get_asset_sales_tax_info into ln_rule_id, lv_asset_k_rule_information5, l_orig_system_id1;
909        CLOSE get_asset_sales_tax_info;
910 
911 	   IF( (l_rebook_yn = 'Y' AND l_orig_system_id1 IS NULL) OR (l_rebook_yn = 'N')) THEN
912 
913         IF (lv_asset_k_rule_information5 IS NOT NULL AND
914             lv_asset_k_rule_information5 = 'Y') THEN
915 
916           l_rulv_rec.id := ln_rule_id;
917           l_rulv_rec.rule_information6 := lv_k_rule_information3;
918           l_rulv_rec.rule_information7 := lv_k_rule_information4;
919           l_rulv_rec.rule_information8 := lv_k_rule_information5;
920           l_rulv_rec.rule_information9 := lv_k_rule_information6;
921           l_rulv_rec.rule_information10 := lv_k_rule_information7;
922           l_rulv_rec.rule_information11 := l_header_upfront_tax;
923 
924           OKL_RULE_PUB.update_rule(
925             p_api_version         => p_api_version,
926             p_init_msg_list       => p_init_msg_list,
927             x_return_status       => x_return_status,
928             x_msg_count           => x_msg_count,
929             x_msg_data            => x_msg_data,
930             p_rulv_rec            => l_rulv_rec,
931             x_rulv_rec            => lx_rulv_rec);
932 
933           IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
934             RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
935           ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
936             RAISE OKL_API.G_EXCEPTION_ERROR;
937           END IF;
938 
939         END IF;
940 
941 	  END IF; -- close of rebook check
942 
943      END LOOP;
944 
945      OKL_API.END_ACTIVITY (x_msg_count  => x_msg_count,
946                             x_msg_data   => x_msg_data);
947   EXCEPTION
948     WHEN OKL_API.G_EXCEPTION_ERROR THEN
949     IF get_contract_sales_tax_info%ISOPEN THEN
950       CLOSE get_contract_sales_tax_info;
951     END IF;
952     IF get_contract_lines%ISOPEN THEN
953       CLOSE get_contract_lines;
954     END IF;
955     IF get_asset_sales_tax_info%ISOPEN THEN
956       CLOSE get_asset_sales_tax_info;
957     END IF;
958     x_return_status := OKL_API.HANDLE_EXCEPTIONS(
959                                l_api_name,
960                                g_pkg_name,
961                                'OKL_API.G_RET_STS_ERROR',
962                                x_msg_count,
963                                x_msg_data,
964                                g_api_type);
965     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
966     IF get_contract_sales_tax_info%ISOPEN THEN
967       CLOSE get_contract_sales_tax_info;
968     END IF;
969     IF get_contract_lines%ISOPEN THEN
970       CLOSE get_contract_lines;
971     END IF;
972     IF get_asset_sales_tax_info%ISOPEN THEN
973       CLOSE get_asset_sales_tax_info;
974     END IF;
975     IF l_tax_system_info%ISOPEN THEN
976       CLOSE l_tax_system_info;
977     END IF;
978     x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
979                               l_api_name,
980                               g_pkg_name,
981                               'OKL_API.G_RET_STS_UNEXP_ERROR',
982                               x_msg_count,
983                               x_msg_data,
984                               g_api_type);
985     WHEN OTHERS THEN
986     IF get_contract_sales_tax_info%ISOPEN THEN
987       CLOSE get_contract_sales_tax_info;
988     END IF;
989     IF get_contract_lines%ISOPEN THEN
990       CLOSE get_contract_lines;
991     END IF;
992     IF get_asset_sales_tax_info%ISOPEN THEN
993       CLOSE get_asset_sales_tax_info;
994     END IF;
995     IF l_tax_system_info%ISOPEN THEN
996       CLOSE l_tax_system_info;
997     END IF;
998     x_return_status :=OKL_API.HANDLE_EXCEPTIONS(
999                               l_api_name,
1000                               g_pkg_name,
1001                               'OTHERS',
1002                               x_msg_count,
1003                               x_msg_data,
1004                               g_api_type);
1005   END sync_contract_sales_tax;
1006 
1007   -- Main Sales Tax Procedures
1008   -- Procedure create/update transaction records for pre-booking
1009   -- and pre-rebooking processes
1010   Procedure populate_transaction(
1011                       p_api_version      IN  NUMBER,
1012                       p_init_msg_list    IN  VARCHAR2 DEFAULT Okl_Api.G_FALSE,
1013                       p_contract_id      IN  okc_k_headers_all_b.id%TYPE,
1014                       p_transaction_type IN  okl_trx_types_tl.name%TYPE,
1015                       p_transaction_id   IN  okl_trx_contracts_all.id%TYPE DEFAULT NULL,
1016                       p_source_trx_id    IN  okl_trx_contracts_all.source_trx_id%TYPE DEFAULT NULL,
1017                       p_source_trx_type  IN  okl_trx_contracts_all.source_trx_type%TYPE DEFAULT NULL,
1018                       --Bug# 6619311
1019                       p_transaction_amount IN  okl_trx_contracts_all.amount%TYPE DEFAULT NULL,
1020                       x_transaction_id   OUT NOCOPY okl_trx_contracts_all.id%TYPE,
1021                       x_trxh_out_rec     OUT NOCOPY Okl_Trx_Contracts_Pvt.tcnv_rec_type, -- R12 Change
1022                       x_return_status    OUT NOCOPY VARCHAR2,
1023                       x_msg_count        OUT NOCOPY NUMBER,
1024                       x_msg_data         OUT NOCOPY VARCHAR2)  IS
1025 
1026      -- Define PL/SQL Records and Tables
1027     l_trxH_in_rec        Okl_Trx_Contracts_Pvt.tcnv_rec_type;
1028     l_trxH_out_rec       Okl_Trx_Contracts_Pvt.tcnv_rec_type;
1029 
1030     -- Define variables
1031     l_sysdate         DATE;
1032     l_sysdate_trunc   DATE;
1033     l_return_status   VARCHAR2(1) := Okl_Api.G_RET_STS_SUCCESS;
1034     l_post_to_gl_yn   VARCHAR2(1);
1035 
1036     CURSOR Transaction_Type_csr (p_transaction_type IN okl_trx_types_v.name%TYPE )
1037     IS
1038     SELECT id
1039     FROM  okl_trx_types_tl
1040     WHERE  name = p_transaction_type
1041     AND language = 'US';
1042 
1043     Cursor trx_csr( khrId NUMBER, tcntype VARCHAR2 ) is
1044     Select txh.ID HeaderTransID,
1045            txh.date_transaction_occurred date_transaction_occurred
1046     From   okl_trx_contracts txh
1047     Where  txh.khr_id = khrId
1048    --rkuttiya added for 12.1.1 Multi GAAP
1049     AND    txh.representation_type = 'PRIMARY'
1050    --
1051     AND    txh.tcn_type = tcntype;
1052 
1053     i                 NUMBER;
1054     l_amount          NUMBER;
1055     l_init_msg_list   VARCHAR2(1) := OKL_API.G_FALSE;
1056     l_msg_count       NUMBER;
1057     l_msg_data        VARCHAR2(2000);
1058     l_currency_code   okl_txl_cntrct_lns.currency_code%TYPE;
1059     l_fnd_profile     VARCHAR2(256);
1060     l_cust_trx_type_id NUMBER;
1061     l_has_trans          VARCHAR2(1);
1062 
1063     l_msg_index_out   NUMBER; --TBR
1064 
1065      -- Define constants
1066     l_api_name        CONSTANT VARCHAR(30) := 'POPULATE_TRANSACTION';
1067     l_api_version     CONSTANT NUMBER      := 1.0;
1068 
1069     -- Cursor Types
1070     l_Trx_Type_rec     Transaction_Type_csr%ROWTYPE;
1071     l_fnd_rec          fnd_lookups_csr%ROWTYPE;
1072     l_fnd_rec1         fnd_lookups_csr%ROWTYPE;
1073     l_trx_rec          trx_csr%ROWTYPE;
1074 
1075     p_chr_id           VARCHAR2(2000) := TO_CHAR(p_contract_id);
1076     l_transaction_type VARCHAR2(256) := p_transaction_type;
1077     l_transaction_id   NUMBER;
1078 
1079     --Bug# 3153003
1080     l_upd_trxH_rec  Okl_Trx_Contracts_Pvt.tcnv_rec_type;
1081     lx_upd_trxH_rec Okl_Trx_Contracts_Pvt.tcnv_rec_type;
1082 
1083     pop_trx_failed exception;
1084 
1085       --Added by dpsingh for LE uptake
1086   CURSOR contract_num_csr (p_ctr_id1 NUMBER) IS
1087   SELECT  contract_number
1088   FROM OKC_K_HEADERS_B
1089   WHERE id = p_ctr_id1;
1090 
1091   l_cntrct_number          OKC_K_HEADERS_B.CONTRACT_NUMBER%TYPE;
1092   l_legal_entity_id          NUMBER;
1093 
1094   -- R12 - START
1095 
1096   l_func_curr_code              OKL_K_HEADERS_FULL_V.CURRENCY_CODE%TYPE;
1097   l_chr_curr_code               OKL_K_HEADERS_FULL_V.CURRENCY_CODE%TYPE;
1098   x_currency_conversion_rate	okl_k_headers_full_v.currency_conversion_rate%TYPE;
1099   x_currency_conversion_type	okl_k_headers_full_v.currency_conversion_type%TYPE;
1100   x_currency_conversion_date	okl_k_headers_full_v.currency_conversion_date%TYPE;
1101 
1102   -- R12 - END
1103 
1104 
1105 
1106   BEGIN
1107 
1108     okl_debug_pub.logmessage('OKL: POP TRX : START');
1109     okl_debug_pub.logmessage('OKL: POP TRX : p_contract_id: ' || p_contract_id);
1110     okl_debug_pub.logmessage('OKL: POP TRX : p_transaction_type: ' || p_transaction_type);
1111     okl_debug_pub.logmessage('OKL: POP TRX : p_transaction_id: ' || p_transaction_id);
1112     okl_debug_pub.logmessage('OKL: POP TRX : p_source_trx_id: ' || p_source_trx_id);
1113     okl_debug_pub.logmessage('OKL: POP TRX : p_source_trx_type: ' || p_source_trx_type);
1114 
1115     IF (G_DEBUG_ENABLED = 'Y') THEN
1116       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
1117     END IF;
1118 
1119     x_return_status  := Okl_Api.G_RET_STS_SUCCESS;
1120     l_sysdate        := SYSDATE;
1121     l_sysdate_trunc  := trunc(SYSDATE);
1122     i                := 0;
1123 
1124     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1125           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : START ');
1126     END IF;
1127 
1128 
1129     -- R12 - START
1130 
1131     -- Get product_id
1132     OPEN  Product_csr (p_contract_id);
1133     FETCH Product_csr INTO g_Product_rec;
1134     IF Product_csr%NOTFOUND THEN
1135       Okl_Api.SET_MESSAGE(G_APP_NAME, G_REQUIRED_VALUE,G_COL_NAME_TOKEN, 'Product');
1136       CLOSE Product_csr;
1137       RAISE Okl_Api.G_EXCEPTION_ERROR;
1138     END IF;
1139     CLOSE Product_csr;
1140 
1141     l_chr_curr_code  := g_Product_rec.CURRENCY_CODE;
1142     l_func_curr_code := OKC_CURRENCY_API.GET_OU_CURRENCY(g_Product_rec.authoring_org_id);
1143 
1144     x_currency_conversion_rate := NULL;
1145     x_currency_conversion_type := NULL;
1146     x_currency_conversion_date := NULL;
1147 
1148     If ( ( l_func_curr_code IS NOT NULL) AND
1149          ( l_chr_curr_code <> l_func_curr_code ) ) Then
1150 
1151       x_currency_conversion_type := g_Product_rec.currency_conversion_type;
1152       x_currency_conversion_date := g_Product_rec.start_date;
1153 
1154       If ( g_Product_rec.currency_conversion_type = 'User') Then
1155         x_currency_conversion_rate := g_Product_rec.currency_conversion_rate;
1156         x_currency_conversion_date := g_Product_rec.currency_conversion_date;
1157       Else
1158         x_currency_conversion_rate := okl_accounting_util.get_curr_con_rate(
1159 	                                       p_from_curr_code => l_chr_curr_code,
1160 	                                       p_to_curr_code   => l_func_curr_code,
1161 					       p_con_date       => g_Product_rec.start_date,
1162 					       p_con_type       => g_Product_rec.currency_conversion_type);
1163 
1164       End If;
1165 
1166     End If;
1167 
1168     l_currency_code                          := g_Product_rec.currency_code;
1169     l_trxH_in_rec.pdt_id                     := g_Product_rec.product_id;
1170     l_trxH_in_rec.currency_code              := l_currency_code;
1171     l_trxH_in_rec.currency_conversion_rate   := x_currency_conversion_rate;
1172     l_trxH_in_rec.currency_conversion_type   := x_currency_conversion_type;
1173     l_trxH_in_rec.currency_conversion_date   := x_currency_conversion_date;
1174 
1175     -- R12 - END
1176 
1177 
1178     -- R12 Bug# 14033244 Start
1179 
1180     OPEN  Product_Params_csr (g_Product_rec.product_id);
1181     FETCH Product_Params_csr INTO g_Product_Params_rec;
1182 
1183         IF Product_Params_csr%NOTFOUND THEN
1184 
1185           Okl_Api.SET_MESSAGE(G_APP_NAME, G_REQUIRED_VALUE, G_COL_NAME_TOKEN, 'Product');
1186           CLOSE Product_Params_csr;
1187           RAISE Okl_Api.G_EXCEPTION_ERROR;
1188 
1189         END IF;
1190 
1191     CLOSE Product_Params_csr;
1192     -- R12 Bug# 14033244 End
1193 
1194 
1195 
1196     -- Validate passed parameters
1197     IF   ( p_contract_id = Okl_Api.G_MISS_NUM       )
1198       OR ( p_contract_id IS NULL                    ) THEN
1199         --Okl_Api.Set_Message(G_APP_NAME, G_REQUIRED_VALUE,G_COL_NAME_TOKEN, 'contract');
1200         Okl_Api.Set_Message(G_APP_NAME, 'OKL_LA_ST_K_ID_ERROR');
1201         RAISE pop_trx_failed;
1202     END IF;
1203 
1204     IF   ( p_transaction_type = Okl_Api.G_MISS_CHAR )
1205       OR ( p_transaction_type IS NULL               ) THEN
1206         --Okl_Api.Set_Message(G_APP_NAME, G_REQUIRED_VALUE,G_COL_NAME_TOKEN, l_transaction_type);
1207         Okl_Api.Set_Message(G_APP_NAME, 'OKL_LA_ST_TRX_TYPE_ERROR');
1208         RAISE pop_trx_failed;
1209     END IF;
1210 
1211     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1212           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : before Transaction_Type_csr ');
1213     END IF;
1214     -- Check Transaction_Type
1215     OPEN  Transaction_Type_csr(l_transaction_type);
1216     FETCH Transaction_Type_csr INTO l_Trx_Type_rec;
1217     IF Transaction_Type_csr%NOTFOUND THEN
1218           Okl_Api.SET_MESSAGE(G_APP_NAME, G_INVALID_VALUE,'TRANSACTION_TYPE', l_transaction_type);
1219             /*OKL_API.SET_MESSAGE(
1220                             p_app_name     =>  G_APP_NAME,
1221                             p_msg_name     =>  G_INVALID_VALUE,
1222                             p_token1       =>  'COL_NAME',
1223                             p_token1_value =>  'TRANSACTION_TYPE');*/
1224 
1225       CLOSE Transaction_Type_csr;
1226       RAISE pop_trx_failed;
1227     END IF;
1228     CLOSE Transaction_Type_csr;
1229 
1230     okl_debug_pub.logmessage('OKL: POP TRX : Transaction_Type_csr.id: ' || l_Trx_Type_rec.id);
1231 
1232     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1233           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : before fnd_lookups_csr ');
1234     END IF;
1235 
1236     OPEN  fnd_lookups_csr('OKL_TCN_TYPE', l_transaction_type);
1237     FETCH fnd_lookups_csr INTO l_fnd_rec;
1238 
1239     IF fnd_lookups_csr%NOTFOUND THEN
1240       Okl_Api.SET_MESSAGE(G_APP_NAME, G_INVALID_VALUE,'TRANSACTION_TYPE', l_transaction_type);
1241       CLOSE fnd_lookups_csr;
1242       RAISE pop_trx_failed;
1243     END IF;
1244     CLOSE fnd_lookups_csr;
1245 
1246     okl_debug_pub.logmessage('OKL: POP TRX : transaction type: ' || l_transaction_type);
1247     okl_debug_pub.logmessage('OKL: POP TRX : fnd_lookups_csr: Description ' || l_fnd_rec.description);
1248     okl_debug_pub.logmessage('OKL: POP TRX : fnd_lookups_csr: lookup_code ' || l_fnd_rec.lookup_code);
1249 
1250 
1251     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1252           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : before trx_csr ');
1253     END IF;
1254 
1255     -- gboomina Bug 6487183 - Start
1256     -- For Upfront Tax transaction always create transaction header record
1257     IF (l_transaction_type <> 'Upfront Tax') THEN
1258       OPEN  trx_csr(p_contract_id,l_fnd_rec.lookup_code);
1259       FETCH trx_csr INTO l_trx_rec;
1260       IF trx_csr%NOTFOUND THEN -- While activation, create a new trans always.
1261           l_has_trans := OKL_API.G_FALSE;
1262           okl_debug_pub.logmessage('OKL: POP TRX : No Existing TRX: '||l_fnd_rec.lookup_code);
1263       Else
1264           l_has_trans := OKL_API.G_TRUE;
1265           okl_debug_pub.logmessage('OKL: POP TRX : TRX Exists: '|| l_fnd_rec.lookup_code);
1266       END IF;
1267       CLOSE trx_csr;
1268     ELSE
1269       l_has_trans := OKL_API.G_FALSE;
1270     END IF;
1271     -- gboomina Bug 6487183 - End
1272 
1273     l_trxH_in_rec.khr_id                       := p_contract_id;
1274     l_trxH_in_rec.source_trx_id                := p_source_trx_id;
1275     l_trxH_in_rec.source_trx_type              := p_source_trx_type;
1276     --l_trxH_in_rec.date_transaction_occurred  := sysdate; -- OKL.H Code
1277     l_trxH_in_rec.date_transaction_occurred    := g_Product_rec.start_date; -- R12 Change
1278     l_trxH_in_rec.try_id                       := l_Trx_Type_rec.id;
1279     l_trxH_in_rec.tcn_type                     := l_fnd_rec.lookup_code; --'BKG'/'SYND'/'TRBK';
1280 
1281     --Bug# 6619311
1282     IF p_transaction_amount IS NOT NULL THEN
1283       l_trxH_in_rec.amount                      := p_transaction_amount;
1284     END IF;
1285 
1286     okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.khr_id : '|| l_trxH_in_rec.khr_id);
1287     okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.source_trx_id : '|| l_trxH_in_rec.source_trx_id);
1288     okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.source_trx_type : '|| l_trxH_in_rec.source_trx_type);
1289     okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.date_transaction_occurred : '|| l_trxH_in_rec.date_transaction_occurred);
1290     okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.try_id : '|| l_trxH_in_rec.try_id);
1291     okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.tcn_type : '|| l_trxH_in_rec.tcn_type);
1292     okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.amount : '|| l_trxH_in_rec.amount);
1293 
1294     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1295       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : transaction type '||l_transaction_type);
1296       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : try_id '||l_trxH_in_rec.try_id);
1297       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : tcn_type '||l_trxH_in_rec.tcn_type);
1298       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : l_has_trans '||l_has_trans);
1299     END IF;
1300 
1301     If ( l_has_trans = OKL_API.G_FALSE ) Then
1302 
1303       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1304               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : before fnd_lookups_csr ');
1305       END IF;
1306 
1307       -- The followinf if segment ensures that Upfront Tax transaction is created
1308       -- with 'Processed' status
1309       IF (l_transaction_type = 'Upfront Tax')
1310       THEN
1311         OPEN  fnd_lookups_csr('OKL_TRANSACTION_STATUS', 'Processed');
1312       ELSE
1313         OPEN  fnd_lookups_csr('OKL_TRANSACTION_STATUS', 'Entered');
1314       END IF;
1315       FETCH fnd_lookups_csr INTO l_fnd_rec;
1316       IF fnd_lookups_csr%NOTFOUND THEN
1317         Okl_Api.SET_MESSAGE(G_APP_NAME, G_INVALID_VALUE,G_COL_NAME_TOKEN,l_transaction_type);
1318         CLOSE fnd_lookups_csr;
1319         RAISE pop_trx_failed;
1320       END IF;
1321       CLOSE fnd_lookups_csr;
1322 
1323       l_trxH_in_rec.tsu_code       := l_fnd_rec.lookup_code;
1324       l_trxH_in_rec.description    := l_fnd_rec.description;
1325 
1326       okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.tsu_code : '|| l_trxH_in_rec.tsu_code);
1327       okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_in_rec.description : '|| l_trxH_in_rec.description);
1328 
1329 
1330       --Added by dpsingh for LE Uptake
1331       l_legal_entity_id  := OKL_LEGAL_ENTITY_UTIL.get_khr_le_id(p_contract_id) ;
1332       IF  l_legal_entity_id IS NOT NULL THEN
1333         l_trxH_in_rec.legal_entity_id :=  l_legal_entity_id;
1334       ELSE
1335         -- get the contract number
1336         OPEN contract_num_csr(p_contract_id);
1337         FETCH contract_num_csr INTO l_cntrct_number;
1338         CLOSE contract_num_csr;
1339 	Okl_Api.set_message(p_app_name     => g_app_name,
1340                              p_msg_name     => 'OKL_LE_NOT_EXIST_CNTRCT',
1341 			     p_token1           =>  'CONTRACT_NUMBER',
1342 			     p_token1_value  =>  l_cntrct_number);
1343          RAISE OKL_API.G_EXCEPTION_ERROR;
1344       END IF;
1345       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1346               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
1347                                       , G_MODULE
1348                                       ,'OKL: Populate Trx Procedure : before Okl_Trx_Contracts_Pub.create_trx_contracts ');
1349       END IF;
1350 
1351       -- Create Transaction Header, Lines
1352       Okl_Trx_Contracts_Pub.create_trx_contracts(
1353              p_api_version      => p_api_version
1354             ,p_init_msg_list    => p_init_msg_list
1355             ,x_return_status    => x_return_status
1356             ,x_msg_count        => x_msg_count
1357             ,x_msg_data         => x_msg_data
1358             ,p_tcnv_rec         => l_trxH_in_rec
1359             ,x_tcnv_rec         => l_trxH_out_rec);
1360 
1361       okl_debug_pub.logmessage('OKL: POP TRX : return status '||x_return_status);
1362 
1363       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
1364         RAISE pop_trx_failed;
1365       END IF;
1366 
1367       IF ((l_trxH_out_rec.id = OKL_API.G_MISS_NUM) OR
1368           (l_trxH_out_rec.id IS NULL) ) THEN
1369           OKL_API.set_message(G_APP_NAME, G_REQUIRED_VALUE,G_COL_NAME_TOKEN,'TRANSACTION_ID');
1370           --OKL_API.set_message(G_APP_NAME, 'Header Transaction Id is null or invalid');
1371           RAISE pop_trx_failed;
1372       END IF;
1373 
1374       l_fnd_rec := null;
1375 
1376       -- outbound transaction id
1377       x_transaction_id        := l_trxH_out_rec.id;
1378       x_trxh_out_rec          := l_trxH_out_rec; -- R12 Change
1379 
1380       okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_out_rec.id : '|| l_trxH_out_rec.id);
1381       okl_debug_pub.logmessage('OKL: POP TRX : l_trxH_out_rec.description : '|| l_trxH_out_rec.description);
1382 
1383       okl_debug_pub.logmessage('OKL: POP TRX : try id '||l_trxH_out_rec.TRY_ID);
1384       okl_debug_pub.logmessage('OKL: POP TRX : transaction type '||l_trxH_out_rec.TCN_TYPE);
1385       okl_debug_pub.logmessage('OKL: POP TRX : tsu code '||l_trxH_out_rec.TSU_CODE);
1386 
1387     ELSE
1388 
1389       --if transaction exists change the date transaction occured
1390       l_upd_trxH_rec.id                       := l_trx_rec.HeaderTransID;
1391       l_upd_trxH_rec.date_transaction_occurred := l_trx_rec.date_transaction_occurred;
1392       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1393               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : transaction ID '||l_trx_rec.HeaderTransID);
1394         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : before fnd_lookups_csr ');
1395       END IF;
1396 
1397       -- R12 Bug# 14033244 Start
1398         l_upd_trxH_rec.pdt_id                           := g_Product_rec.product_id;
1399         l_upd_trxH_rec.book_classification_code	        := g_Product_Params_rec.book_classification_code;
1400         l_upd_trxH_rec.tax_owner_code		        := g_Product_Params_rec.tax_owner_code;
1401         l_upd_trxH_rec.product_name		        := g_Product_Params_rec.product_name;
1402         l_upd_trxH_rec.currency_code                    := l_currency_code;
1403     	l_upd_trxH_rec.currency_conversion_rate         := x_currency_conversion_rate;
1404     	l_upd_trxH_rec.currency_conversion_type         := x_currency_conversion_type;
1405     	l_upd_trxH_rec.currency_conversion_date         := x_currency_conversion_date;
1406       -- R12 Bug# 14033244 End
1407 
1408       OPEN  fnd_lookups_csr('OKL_TRANSACTION_STATUS', 'Entered');
1409       FETCH fnd_lookups_csr INTO l_fnd_rec;
1410       IF fnd_lookups_csr%NOTFOUND THEN
1411         Okl_Api.SET_MESSAGE(G_APP_NAME, G_INVALID_VALUE,G_COL_NAME_TOKEN,l_transaction_type);
1412         --Okl_Api.SET_MESSAGE(G_APP_NAME, 'Cannot Find Transaction Status');
1413         CLOSE fnd_lookups_csr;
1414         RAISE pop_trx_failed;
1415       END IF;
1416       CLOSE fnd_lookups_csr;
1417 
1418       l_upd_trxH_rec.tsu_code       := l_fnd_rec.lookup_code;
1419 
1420       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1421               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : before Okl_Trx_Contracts_Pub.update_trx_contracts ');
1422       END IF;
1423 
1424       Okl_Trx_Contracts_Pub.update_trx_contracts(
1425              p_api_version      => p_api_version
1426             ,p_init_msg_list    => p_init_msg_list
1427             ,x_return_status    => x_return_status
1428             ,x_msg_count        => x_msg_count
1429             ,x_msg_data         => x_msg_data
1430             ,p_tcnv_rec         => l_upd_trxH_rec
1431             ,x_tcnv_rec         => lx_upd_trxH_rec);
1432 
1433       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
1434         RAISE pop_trx_failed;
1435       END IF;
1436 
1437       IF ((lx_upd_trxH_rec.id = OKL_API.G_MISS_NUM) OR
1438           (lx_upd_trxH_rec.id IS NULL) ) THEN
1439           OKL_API.set_message(G_APP_NAME, G_REQUIRED_VALUE,G_COL_NAME_TOKEN,'TRANSACTION_ID');
1440           --OKL_API.set_message(G_APP_NAME, 'TRANSACTION_ID Missing');
1441           RAISE pop_trx_failed;
1442       END IF;
1443 
1444       -- outbound transaction id
1445       x_transaction_id := l_trx_rec.HeaderTransID;
1446       x_trxh_out_rec   := lx_upd_trxH_rec; -- R12 Change
1447 
1448       okl_debug_pub.logmessage('OKL: POP TRX : return status '||x_return_status);
1449       okl_debug_pub.logmessage('OKL: POP TRX : lx_upd_trxH_rec.id : '|| lx_upd_trxH_rec.id);
1450       okl_debug_pub.logmessage('OKL: POP TRX : lx_upd_trxH_rec.description : '|| lx_upd_trxH_rec.description);
1451 
1452       okl_debug_pub.logmessage('OKL: POP TRX : try id '||lx_upd_trxH_rec.TRY_ID);
1453       okl_debug_pub.logmessage('OKL: POP TRX : transaction type '||lx_upd_trxH_rec.TCN_TYPE);
1454       okl_debug_pub.logmessage('OKL: POP TRX : tsu code '||lx_upd_trxH_rec.TSU_CODE);
1455 
1456 
1457     END IF;
1458 
1459     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
1460           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Populate Trx Procedure : END ');
1461     END IF;
1462 
1463     okl_debug_pub.logmessage('OKL: POP TRX : END');
1464 
1465     Exception
1466       when pop_trx_failed then
1467          x_return_status := OKL_API.G_RET_STS_ERROR;
1468 
1469   END populate_transaction;
1470 
1471 
1472   -- Function to get total estimated financed amount
1473   Function get_financed_tax ( p_Contract_id  IN NUMBER ,
1474                               p_default_tax_treatment IN VARCHAR2)
1475   RETURN NUMBER
1476   IS
1477 
1478     l_fee_amt NUMBER;
1479 
1480   BEGIN
1481 
1482     --OPEN  get_fee_amt_csr(p_contract_id, p_transaction_id);
1483     IF (p_default_tax_treatment = 'FINANCE') THEN
1484       OPEN  get_fee_amt_csr1(p_Contract_id, 'FINANCE');
1485       FETCH get_fee_amt_csr1 INTO l_fee_amt;
1486       CLOSE get_fee_amt_csr1;
1487     END IF;
1488     IF (p_default_tax_treatment = 'BILLED') THEN
1489       OPEN  get_fee_amt_csr2(p_Contract_id,'FINANCE');
1490       FETCH get_fee_amt_csr2 INTO l_fee_amt;
1491       CLOSE get_fee_amt_csr2;
1492     END IF;
1493 
1494     --Hard Code the value for the time being until tax
1495     -- APIs are ready
1496     --l_fee_amt := 50;
1497 
1498     RETURN l_fee_amt;
1499 
1500   END get_financed_tax;
1501 
1502   -- Function to get total estimated capitalized amount
1503   Function get_capitalized_tax ( p_Contract_id    IN NUMBER,
1504                                  p_default_tax_treatment IN VARCHAR2)
1505 
1506   RETURN NUMBER
1507   IS
1508 
1509   l_fee_amt NUMBER;
1510 
1511   BEGIN
1512 
1513     IF (p_default_tax_treatment = 'CAPITALIZE') THEN
1514       OPEN  get_fee_amt_csr1 (p_Contract_id, 'CAPITALIZE');
1515       FETCH get_fee_amt_csr1 INTO l_fee_amt;
1516       CLOSE get_fee_amt_csr1;
1517     END IF;
1518     IF (p_default_tax_treatment = 'BILLED') THEN
1519       OPEN  get_fee_amt_csr2(p_Contract_id, 'CAPITALIZE');
1520       FETCH get_fee_amt_csr2 INTO l_fee_amt;
1521       CLOSE get_fee_amt_csr2;
1522     END IF;
1523 
1524     --Hard Code the value for the time being until tax
1525     -- APIs are ready
1526     --l_fee_amt := 50;
1527 
1528     RETURN l_fee_amt;
1529 
1530   END get_capitalized_tax;
1531 
1532   -- R12B Authoring OA Migration
1533   -- Function to get total estimated billed amount
1534   Function get_billed_tax ( p_Contract_id    IN NUMBER,
1535                             p_default_tax_treatment IN VARCHAR2)
1536 
1537   RETURN NUMBER
1538   IS
1539 
1540   l_tax_amt NUMBER;
1541 
1542   --Bug# 6668721
1543   -- This cursor gets billed upfront tax amount at contract
1544   -- header level
1545   CURSOR contract_billed_tax_csr(p_contract_id IN NUMBER) IS
1546   SELECT NVL(SUM(NVL(total_tax,0)),0)
1547   FROM   okl_tax_sources txs
1548   WHERE  txs.khr_id = p_contract_id
1549   AND    txs.kle_id IS NULL
1550   AND    txs.tax_line_status_code = 'ACTIVE'
1551   AND    txs.tax_call_type_code = 'UPFRONT_TAX';
1552 
1553   l_contract_billed_tax NUMBER;
1554 
1555   BEGIN
1556 
1557     IF (p_default_tax_treatment = 'BILLED') THEN
1558       OPEN  get_fee_amt_csr1 (p_Contract_id, 'BILLED');
1559       FETCH get_fee_amt_csr1 INTO l_tax_amt;
1560       CLOSE get_fee_amt_csr1;
1561     END IF;
1562     IF (p_default_tax_treatment IN ('CAPITALIZE','FINANCE')) THEN
1563       OPEN  get_fee_amt_csr2(p_Contract_id, 'BILLED');
1564       FETCH get_fee_amt_csr2 INTO l_tax_amt;
1565       CLOSE get_fee_amt_csr2;
1566     END IF;
1567 
1568     --Bug# 6939336
1569     --Bug# 6668721
1570     OPEN contract_billed_tax_csr(p_contract_id => p_contract_id);
1571     FETCH contract_billed_tax_csr INTO l_contract_billed_tax;
1572     CLOSE contract_billed_tax_csr;
1573 
1574     l_tax_amt := l_tax_amt + l_contract_billed_tax;
1575     --Bug# 6668721
1576     --Bug# 6939336
1577 
1578     RETURN l_tax_amt;
1579 
1580   END get_billed_tax;
1581 
1582   FUNCTION get_upfront_tax(p_chr_id  IN NUMBER,
1583                            p_tax_treatment IN VARCHAR2)
1584   RETURN NUMBER
1585   IS
1586 
1587     l_tax_amt NUMBER;
1588     l_rule_info_rec  rule_info_csr%ROWTYPE;
1589 
1590   BEGIN
1591 
1592     OPEN  rule_info_csr(p_contract_id => p_chr_id);
1593     FETCH rule_info_csr INTO l_rule_info_rec;
1594     CLOSE rule_info_csr;
1595 
1596     IF l_rule_info_rec.rule_information1 IS NOT NULL THEN
1597       IF (p_tax_treatment = 'CAPITALIZED') THEN
1598         l_tax_amt := get_capitalized_tax (p_contract_id => p_chr_id,
1599                                           p_default_tax_treatment => l_rule_info_rec.rule_information1);
1600       ELSIF (p_tax_treatment = 'FINANCED') THEN
1601         l_tax_amt := get_financed_tax (p_contract_id => p_chr_id,
1602                                        p_default_tax_treatment => l_rule_info_rec.rule_information1);
1603       ELSIF (p_tax_treatment = 'BILLED') THEN
1604         l_tax_amt := get_billed_tax (p_contract_id => p_chr_id,
1605                                      p_default_tax_treatment => l_rule_info_rec.rule_information1);
1606       END IF;
1607     END IF;
1608 
1609     RETURN l_tax_amt;
1610 
1611   END get_upfront_tax;
1612 
1613   PROCEDURE update_fee(
1614             p_api_version      IN  NUMBER,
1615             p_init_msg_list    IN  VARCHAR2 DEFAULT Okl_Api.G_FALSE,
1616             p_contract_id      IN  NUMBER,
1617             p_transaction_id   IN  NUMBER,
1618             p_fee_line_id      IN  NUMBER,
1619             p_required_feetype IN  VARCHAR2,
1620             p_default_feetype  IN  VARCHAR2,
1621             x_return_status    OUT NOCOPY VARCHAR2,
1622             x_msg_count        OUT NOCOPY NUMBER,
1623             x_msg_data         OUT NOCOPY VARCHAR2
1624             )
1625   IS
1626     l_fee_types_rec   OKL_MAINTAIN_FEE_PVT.fee_types_rec_type;
1627     lx_fee_types_rec  OKL_MAINTAIN_FEE_PVT.fee_types_rec_type;
1628 
1629     l_u_line_item_tbl   OKL_CONTRACT_LINE_ITEM_PVT.line_item_tbl_type;
1630     lx_u_line_item_tbl  OKL_CONTRACT_LINE_ITEM_PVT.line_item_tbl_type;
1631 
1632     l_c_line_item_tbl   OKL_CONTRACT_LINE_ITEM_PVT.line_item_tbl_type;
1633     lx_c_line_item_tbl  OKL_CONTRACT_LINE_ITEM_PVT.line_item_tbl_type;
1634 
1635     l_rgpv_rec          rgpv_rec_type;
1636     l_rulv_rec          rulv_rec_type;
1637     l_rulv_tbl          rulv_tbl_type;
1638     lx_rgpv_rec         rgpv_rec_type;
1639     lx_rulv_rec         rulv_rec_type;
1640     lx_rulv_tbl         rulv_tbl_type;
1641 
1642     l_ins_count NUMBER := 0;
1643     l_upd_count NUMBER := 0;
1644     l_del_count NUMBER := 0;
1645     l_upfront_tax_fee_amount NUMBER;
1646 
1647     -- Cursor to get assets for financed fee association
1648     -- Bug# 6512668: Exclude asset lines in Abandoned status
1649     CURSOR get_asset_csr1 (p_fee_type IN VARCHAR2)
1650     IS
1651     SELECT cle.id, cle.name, cle.item_description
1652     FROM   okc_k_lines_v cle,
1653            okc_rule_groups_b rg1,
1654            okc_rules_b rl1,
1655            okc_line_styles_b lse,
1656            okc_rule_groups_b rg2,
1657            okc_rules_b rl2
1658     WHERE  cle.chr_ID = p_contract_id
1659     AND    cle.lse_id     = lse.id
1660     AND    lse.lty_code   = 'FREE_FORM1'
1661     AND    cle.dnz_chr_id = rg1.dnz_chr_id
1662     AND    cle.id         = rg1.cle_id
1663     AND    cle.dnz_chr_id = rl1.dnz_chr_id
1664     AND    rg1.id         = rl1.rgp_id
1665     AND    rg1.rgd_code   = 'LAASTX'
1666     AND    rl1.rule_information_category = 'LAASTX'
1667     AND    ( rl1.rule_information11 IS NULL
1668              OR
1669              rl1.rule_information11 = p_fee_type)
1670     AND    cle.dnz_chr_id = rg2.dnz_chr_id
1671     AND    cle.dnz_chr_id = rl2.dnz_chr_id
1672     AND    rg2.id         = rl2.rgp_id
1673     AND    rg2.rgd_code   = 'LAHDTX'
1674     AND    rl2.rule_information_category = 'LASTPR'
1675     AND    rl2.rule_information1 = p_fee_type
1676     AND    cle.sts_code <> 'ABANDONED';
1677 
1678     -- Cursor to get assets for fee association if T and C Tax treatment is
1679     -- 'BILLED'
1680     -- Bug# 6512668: Exclude asset lines in Abandoned status
1681     CURSOR get_asset_csr2 (p_fee_type IN VARCHAR2)
1682     IS
1683     SELECT cle.id, cle.name, cle.item_description
1684     FROM   okc_k_lines_v cle,
1685            okc_rule_groups_b rg1,
1686            okc_rules_b rl1,
1687            okc_line_styles_b lse
1688     WHERE  cle.dnz_chr_id = p_contract_id
1689     AND    cle.lse_id     = lse.id
1690     AND    lse.lty_code   = 'FREE_FORM1'
1691     AND    cle.dnz_chr_id = rg1.dnz_chr_id
1692     AND    cle.id         = rg1.cle_id
1693     AND    cle.dnz_chr_id = rl1.dnz_chr_id
1694     AND    rg1.id         = rl1.rgp_id
1695     AND    rg1.rgd_code   = 'LAASTX'
1696     AND    rl1.rule_information_category = 'LAASTX'
1697     AND    rl1.rule_information11 = p_fee_type
1698     AND    cle.sts_code <> 'ABANDONED';
1699 
1700     -- Cursor to get tax amounts for assets
1701     CURSOR get_asset_tax_amt_csr (asset_line_id IN NUMBER)
1702     IS
1703     SELECT NVL(SUM(NVL(total_tax,0)),0)
1704     FROM   okl_tax_sources txs
1705     WHERE  txs.khr_ID = p_contract_id
1706     AND    txs.trx_id = p_transaction_id
1707     AND    txs.kle_id = asset_line_id
1708     AND    txs.tax_line_status_code = 'ACTIVE'
1709     AND    txs.tax_call_type_code = 'UPFRONT_TAX';
1710 
1711     -- Cursor to get fee details
1712     CURSOR l_fee_csr(p_fee_cle_id IN NUMBER) IS
1713     SELECT NVL(kle_fee.capital_amount,kle_fee.amount) amount,
1714            kle_fee.fee_type,
1715            cleb_fee.start_date,
1716            cleb_fee.end_date,
1717            cim_fee.id cim_fee_id,
1718            cim_fee.object1_id1
1719     FROM okl_k_lines kle_fee,
1720          okc_k_lines_b cleb_fee,
1721          okc_k_items cim_fee
1722     WHERE cleb_fee.id = p_fee_cle_id
1723     AND kle_fee.id = cleb_fee.id
1724     AND cim_fee.cle_id = cleb_fee.id
1725     AND cim_fee.dnz_chr_id = cleb_fee.dnz_chr_id
1726     AND cim_fee.jtot_object1_code = 'OKL_STRMTYP';
1727 
1728     l_fee_rec l_fee_csr%ROWTYPE;
1729 
1730     --Bug# 12807203
1731     CURSOR stream_csr (stream_type_id IN NUMBER)  IS
1732     select name
1733     from   OKL_STRM_TYPE_TL
1734     where  id = stream_type_id
1735     AND    language = USERENV('LANG');
1736 
1737     l_strm_type_name OKL_STRM_TYPE_TL.name%TYPE;
1738 
1739     -- Cursor to get fee covered asset line
1740     -- Bug# 6512668: Exclude covered asset lines in Abandoned status
1741     CURSOR l_cov_asset_line_csr(p_chr_id IN NUMBER,
1742                                 p_fee_cle_id IN NUMBER,
1743                                 p_fin_asset_cle_id IN NUMBER) IS
1744     SELECT cleb_cov_asset.id cleb_cov_asset_id,
1745            NVL(kle_cov_asset.capital_amount,kle_cov_asset.amount) amount,
1746            cim_cov_asset.id cim_cov_asset_id
1747     FROM okc_k_lines_b cleb_cov_asset,
1748          okl_k_lines kle_cov_asset,
1749          okc_k_items cim_cov_asset
1750     WHERE cleb_cov_asset.cle_id = p_fee_cle_id
1751     AND   cleb_cov_asset.dnz_chr_id = p_chr_id
1752     AND   kle_cov_asset.id = cleb_cov_asset.id
1753     AND   cim_cov_asset.cle_id = cleb_cov_asset.id
1754     AND   cim_cov_asset.dnz_chr_id = cleb_cov_asset.dnz_chr_id
1755     AND   cim_cov_asset.object1_id1 = p_fin_asset_cle_id
1756     AND   cim_cov_asset.jtot_object1_code = 'OKX_COVASST'
1757     AND   cleb_cov_asset.sts_code <> 'ABANDONED';
1758 
1759     l_cov_asset_line_rec l_cov_asset_line_csr%ROWTYPE;
1760 
1761     -- Cursor to get fee expensee
1762     CURSOR l_rul_csr(p_chr_id IN NUMBER,
1763                      p_fee_cle_id IN NUMBER) IS
1764     SELECT rul_lafexp.id,
1765            rul_lafexp.rgp_id,
1766            rul_lafexp.dnz_chr_id,
1767            rul_lafexp.rule_information_category,
1768            TO_NUMBER(rul_lafexp.rule_information2) amount
1769     FROM okc_rule_groups_b rgp_lafexp,
1770          okc_rules_b rul_lafexp
1771     WHERE rgp_lafexp.dnz_chr_id = p_chr_id
1772     AND   rgp_lafexp.cle_id = p_fee_cle_id
1773     AND   rgp_lafexp.rgd_code = 'LAFEXP'
1774     AND   rul_lafexp.rgp_id = rgp_lafexp.id
1775     AND   rul_lafexp.dnz_chr_id = rgp_lafexp.dnz_chr_id
1776     AND   rul_lafexp.rule_information_category = 'LAFEXP';
1777 
1778     l_rul_rec l_rul_csr%ROWTYPE;
1779 
1780     -- Cursor to check if sales tax fee exists
1781     -- Bug# 6512668: Exclude fee lines in Abandoned status
1782     CURSOR check_fee_csr(p_chr_id IN NUMBER,
1783                          p_fee_type IN VARCHAR2 )  IS
1784     SELECT cle.id
1785     FROM   okc_k_lines_b cle,
1786            okl_k_lines kle
1787     WHERE  cle.id = kle.id
1788     AND    cle.dnz_chr_id = p_chr_id
1789     AND    kle.fee_purpose_code = 'SALESTAX'
1790     AND    kle.fee_type = p_fee_type
1791     AND    cle.sts_code <> 'ABANDONED';
1792 
1793     l_del_fee_line_id    OKC_K_LINES_B.id%TYPE;
1794     l_del_fee_types_rec  OKL_MAINTAIN_FEE_PVT.fee_types_rec_type;
1795 
1796     -- Bug# 6512668: Exclude asset lines in Abandoned status
1797     CURSOR l_del_cov_asset_csr1(p_chr_id IN NUMBER,
1798                                 p_fee_cle_id IN NUMBER) IS
1799     SELECT cleb_cov_asset.id cleb_cov_asset_id,
1800            cim_cov_asset.id cim_cov_asset_id
1801     FROM okc_k_lines_b cleb_cov_asset,
1802          okc_k_items cim_cov_asset,
1803          okc_k_lines_b cleb_fin,
1804          okc_rule_groups_b rgp,
1805          okc_rules_b rul
1806     WHERE cleb_cov_asset.cle_id = p_fee_cle_id
1807     AND   cleb_cov_asset.dnz_chr_id = p_chr_id
1808     AND   cim_cov_asset.cle_id = cleb_cov_asset.id
1809     AND   cim_cov_asset.dnz_chr_id = cleb_cov_asset.dnz_chr_id
1810     AND   cim_cov_asset.jtot_object1_code = 'OKX_COVASST'
1811     AND   cleb_fin.id = cim_cov_asset.object1_id1
1812     AND   cleb_fin.chr_id = cim_cov_asset.dnz_chr_id
1813     AND   cleb_fin.dnz_chr_id = cim_cov_asset.dnz_chr_id
1814     AND   rgp.dnz_chr_id = cleb_fin.dnz_chr_id
1815     AND   rgp.cle_id     = cleb_fin.id
1816     AND   rgp.rgd_code   = 'LAASTX'
1817     AND   rul.dnz_chr_id = rgp.dnz_chr_id
1818     AND   rul.rgp_id     = rgp.id
1819     AND   rul.rule_information_category = 'LAASTX'
1820     AND   rul.rule_information11 = 'BILLED'
1821     AND   cleb_fin.sts_code <> 'ABANDONED'
1822     AND   cleb_cov_asset.sts_code <> 'ABANDONED';
1823 
1824     -- Bug# 6512668: Exclude asset lines in Abandoned status
1825     CURSOR l_del_cov_asset_csr2(p_chr_id IN NUMBER,
1826                                 p_fee_cle_id IN NUMBER) IS
1827     SELECT cleb_cov_asset.id cleb_cov_asset_id,
1828            cim_cov_asset.id cim_cov_asset_id
1829     FROM okc_k_lines_b cleb_cov_asset,
1830          okc_k_items cim_cov_asset,
1831          okc_k_lines_b cleb_fin,
1832          okc_rule_groups_b rgp,
1833          okc_rules_b rul
1834     WHERE cleb_cov_asset.cle_id = p_fee_cle_id
1835     AND   cleb_cov_asset.dnz_chr_id = p_chr_id
1836     AND   cim_cov_asset.cle_id = cleb_cov_asset.id
1837     AND   cim_cov_asset.dnz_chr_id = cleb_cov_asset.dnz_chr_id
1838     AND   cim_cov_asset.jtot_object1_code = 'OKX_COVASST'
1839     AND   cleb_fin.id = cim_cov_asset.object1_id1
1840     AND   cleb_fin.chr_id = cim_cov_asset.dnz_chr_id
1841     AND   cleb_fin.dnz_chr_id = cim_cov_asset.dnz_chr_id
1842     AND   rgp.dnz_chr_id = cleb_fin.dnz_chr_id
1843     AND   rgp.cle_id     = cleb_fin.id
1844     AND   rgp.rgd_code   = 'LAASTX'
1845     AND   rul.dnz_chr_id = rgp.dnz_chr_id
1846     AND   rul.rgp_id     = rgp.id
1847     AND   rul.rule_information_category = 'LAASTX'
1848     AND   NVL(rul.rule_information11,'BILLED') = 'BILLED'
1849     AND   cleb_fin.sts_code <> 'ABANDONED'
1850     AND   cleb_cov_asset.sts_code <> 'ABANDONED';
1851 
1852     l_d_line_item_tbl   OKL_CONTRACT_LINE_ITEM_PVT.line_item_tbl_type;
1853 
1854     l_asset_tax_amt NUMBER;
1855     update_fee_exception exception;
1856 
1857     x_msg_index_out Number;
1858 
1859   BEGIN
1860     IF (G_DEBUG_ENABLED = 'Y') THEN
1861       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
1862     END IF;
1863 
1864     x_return_status  := Okl_Api.G_RET_STS_SUCCESS;
1865 
1866     -- If upfront tax is 'FINANCED'
1867     IF (p_required_feetype = 'FINANCE') THEN
1868       l_upfront_tax_fee_amount := get_financed_tax (p_contract_id, p_default_feetype);
1869 
1870       -- Delete any Sales Tax Fee having Fee Type 'Capitalized'
1871       OPEN check_fee_csr(p_chr_id => p_contract_id,
1872                          p_fee_type => 'CAPITALIZED');
1873       FETCH check_fee_csr INTO l_del_fee_line_id;
1874       CLOSE check_fee_csr;
1875 
1876     -- If upfront tax is 'CAPITALIZED'
1877     ELSIF (p_required_feetype = 'CAPITALIZE') THEN
1878       l_upfront_tax_fee_amount := get_capitalized_tax (p_contract_id, p_default_feetype);
1879 
1880       -- Delete any Sales Tax Fee having Fee Type 'Financed'
1881       OPEN check_fee_csr(p_chr_id => p_contract_id,
1882                          p_fee_type => 'FINANCED');
1883       FETCH check_fee_csr INTO l_del_fee_line_id;
1884       CLOSE check_fee_csr;
1885     END IF;
1886 
1887     IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1888       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: l_upfront_tax_fee_amount: '||l_upfront_tax_fee_amount);
1889     END IF;
1890 
1891     IF (l_del_fee_line_id IS NOT NULL) THEN
1892       l_del_fee_types_rec.line_id := l_del_fee_line_id;
1893       l_del_fee_types_rec.dnz_chr_id := p_contract_id;
1894 
1895       -- delete fee line
1896       OKL_MAINTAIN_FEE_PVT.delete_fee_type(
1897         p_api_version    => p_api_version,
1898         p_init_msg_list  => p_init_msg_list,
1899         x_return_status  => x_return_status,
1900         x_msg_count      => x_msg_count,
1901         x_msg_data       => x_msg_data,
1902         p_fee_types_rec  => l_del_fee_types_rec
1903        );
1904 
1905       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1906         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: after OKL_MAINTAIN_FEE_PVT.delete_fee_type: x_return_status '||x_return_status);
1907       END IF;
1908 
1909       -- Check if the call was successful
1910       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
1911         RAISE update_fee_exception;
1912       END IF;
1913     END IF;
1914 
1915     OPEN l_fee_csr(p_fee_cle_id => p_fee_line_id);
1916     FETCH l_fee_csr INTO l_fee_rec;
1917     CLOSE l_fee_csr;
1918 
1919     IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1920       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: l_fee_rec.amount: '||l_fee_rec.amount);
1921     END IF;
1922 
1923     IF (l_upfront_tax_fee_amount <> l_fee_rec.amount) THEN
1924 
1925       l_fee_types_rec.line_id          := p_fee_line_id;
1926       l_fee_types_rec.dnz_chr_id       := p_contract_id;
1927       l_fee_types_rec.amount           := l_upfront_tax_fee_amount;
1928       l_fee_types_rec.fee_type         := l_fee_rec.fee_type;
1929       l_fee_types_rec.effective_from   := l_fee_rec.start_date;
1930       l_fee_types_rec.effective_to     := l_fee_rec.end_date;
1931       l_fee_types_rec.item_id          := l_fee_rec.cim_fee_id;
1932       l_fee_types_rec.item_id1         := l_fee_rec.object1_id1;
1933 
1934       --Bug# 12807203
1935       l_strm_type_name := NULL;
1936       OPEN  stream_csr(l_fee_rec.object1_id1);
1937       FETCH stream_csr INTO l_strm_type_name;
1938       CLOSE stream_csr;
1939 
1940       l_fee_types_rec.item_name        := l_strm_type_name;
1941 
1942       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1943         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: before OKL_MAINTAIN_FEE_PVT.update_fee_type ');
1944       END IF;
1945 
1946       -- update fee top line
1947       OKL_MAINTAIN_FEE_PVT.update_fee_type(
1948         p_api_version    => p_api_version,
1949         p_init_msg_list  => p_init_msg_list,
1950         x_return_status  => x_return_status,
1951         x_msg_count      => x_msg_count,
1952         x_msg_data       => x_msg_data,
1953         p_fee_types_rec  => l_fee_types_rec,
1954         x_fee_types_rec  => lx_fee_types_rec
1955        );
1956 
1957       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1958         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: after OKL_MAINTAIN_FEE_PVT.update_fee_type: x_return_status '||x_return_status);
1959       END IF;
1960 
1961       -- Check if the call was successful
1962       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
1963         RAISE update_fee_exception;
1964       END IF;
1965     END IF;
1966 
1967     IF (p_default_feetype IN ('CAPITALIZE','FINANCE')) THEN
1968       l_ins_count :=1;
1969       l_upd_count :=1;
1970       FOR j in get_asset_csr1(p_required_feetype)
1971       LOOP
1972 
1973         IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1974           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: before get_asset_tax_amt_csr ');
1975         END IF;
1976 
1977         -- get asset tax amount
1978         OPEN  get_asset_tax_amt_csr(j.id);
1979         FETCH get_asset_tax_amt_csr INTO l_asset_tax_amt;
1980         IF get_asset_tax_amt_csr%NOTFOUND THEN
1981 
1982           OKL_API.set_message(G_APP_NAME, 'OKL_LA_ST_ASSET_TAX_AMT_ERROR');
1983           IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1984             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Cannot derive asset tax amount for ID'|| j.id);
1985           END IF;
1986           CLOSE get_asset_tax_amt_csr;
1987           RAISE update_fee_exception;
1988         END IF;
1989         CLOSE get_asset_tax_amt_csr;
1990 
1991         IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
1992           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: l_asset_tax_amt '||l_asset_tax_amt);
1993         END IF;
1994 
1995         l_cov_asset_line_rec.cleb_cov_asset_id := NULL;
1996         l_cov_asset_line_rec.amount := NULL;
1997         l_cov_asset_line_rec.cim_cov_asset_id := NULL;
1998         OPEN l_cov_asset_line_csr(p_chr_id     => p_contract_id,
1999                                   p_fee_cle_id => p_fee_line_id,
2000                                   p_fin_asset_cle_id => j.id);
2001         FETCH l_cov_asset_line_csr INTO l_cov_asset_line_rec;
2002         CLOSE l_cov_asset_line_csr;
2003 
2004         IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2005           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: l_cov_asset_line_rec.cleb_cov_asset_id '||l_cov_asset_line_rec.cleb_cov_asset_id);
2006           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: l_cov_asset_line_rec.amount '||l_cov_asset_line_rec.amount);
2007         END IF;
2008 
2009         -- Update Fee covered asset amount if covered asset line exists, else create covered asset line
2010         IF l_cov_asset_line_rec.cleb_cov_asset_id IS NOT NULL THEN
2011 
2012           IF  (l_asset_tax_amt <> l_cov_asset_line_rec.amount) THEN
2013 
2014                 l_u_line_item_tbl(l_upd_count).cle_id            := l_cov_asset_line_rec.cleb_cov_asset_id;
2015                 l_u_line_item_tbl(l_upd_count).item_id           := l_cov_asset_line_rec.cim_cov_asset_id;
2016 
2017                 l_u_line_item_tbl(l_upd_count).chr_id            := p_contract_id;
2018                 l_u_line_item_tbl(l_upd_count).parent_cle_id     := p_fee_line_id;
2019                 l_u_line_item_tbl(l_upd_count).item_id1          := j.id;
2020                 l_u_line_item_tbl(l_upd_count).item_id2          := '#';
2021                 l_u_line_item_tbl(l_upd_count).item_object1_code := 'OKX_COVASST';
2022                 l_u_line_item_tbl(l_upd_count).item_description  := j.item_description;
2023                 l_u_line_item_tbl(l_upd_count).name              := j.name;
2024                 l_u_line_item_tbl(l_upd_count).capital_amount    := l_asset_tax_amt;
2025 
2026                 l_upd_count := l_upd_count + 1;
2027           END IF;
2028         ELSE
2029 
2030               l_c_line_item_tbl(l_ins_count).chr_id            := p_contract_id;
2031               l_c_line_item_tbl(l_ins_count).parent_cle_id     := p_fee_line_id;
2032               l_c_line_item_tbl(l_ins_count).item_id1          := j.id;
2033               l_c_line_item_tbl(l_ins_count).item_id2          := '#';
2034               l_c_line_item_tbl(l_ins_count).item_object1_code := 'OKX_COVASST';
2035               l_c_line_item_tbl(l_ins_count).item_description  := j.item_description;
2036               l_c_line_item_tbl(l_ins_count).name              := j.name;
2037               l_c_line_item_tbl(l_ins_count).capital_amount    := l_asset_tax_amt;
2038 
2039               l_ins_count := l_ins_count + 1;
2040         END IF;
2041       END LOOP;
2042 
2043       l_del_count := 1;
2044       --Delete covered asset lines for assets that have tax treatment 'Billed'
2045       FOR l_del_cov_asset_rec IN l_del_cov_asset_csr1(p_chr_id     => p_contract_id,
2046                                                       p_fee_cle_id => p_fee_line_id) LOOP
2047 
2048         --Bug# 6512668: Corrected l_upd_count to l_del_count
2049         l_d_line_item_tbl(l_del_count).chr_id         := p_contract_id;
2050         l_d_line_item_tbl(l_del_count).parent_cle_id  := p_fee_line_id;
2051         l_d_line_item_tbl(l_del_count).cle_id         := l_del_cov_asset_rec.cleb_cov_asset_id;
2052         l_d_line_item_tbl(l_del_count).item_id        := l_del_cov_asset_rec.cim_cov_asset_id;
2053 
2054         l_del_count := l_del_count + 1;
2055 
2056       END LOOP;
2057 
2058     ELSIF (p_default_feetype = 'BILLED') THEN
2059       l_ins_count :=1;
2060       l_upd_count :=1;
2061       FOR j in get_asset_csr2 (p_required_feetype)
2062       LOOP
2063 
2064         IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2065           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: before get_asset_tax_amt_csr ');
2066         END IF;
2067 
2068         -- get asset tax amount
2069         OPEN  get_asset_tax_amt_csr(j.id);
2070         FETCH get_asset_tax_amt_csr INTO l_asset_tax_amt;
2071         IF get_asset_tax_amt_csr%NOTFOUND THEN
2072           OKL_API.set_message( p_app_name     => G_APP_NAME,
2073                                p_msg_name     => 'OKL_LA_ST_ASSET_TAX_AMT_ERROR');
2074           IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2075             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Cannot derive asset tax amount. for ID'|| j.id);
2076           END IF;
2077           CLOSE get_asset_tax_amt_csr;
2078           RAISE update_fee_exception;
2079         END IF;
2080         CLOSE get_asset_tax_amt_csr;
2081 
2082         l_cov_asset_line_rec.cleb_cov_asset_id := NULL;
2083         l_cov_asset_line_rec.amount := NULL;
2084         l_cov_asset_line_rec.cim_cov_asset_id := NULL;
2085         OPEN l_cov_asset_line_csr(p_chr_id     => p_contract_id,
2086                                   p_fee_cle_id => p_fee_line_id,
2087                                   p_fin_asset_cle_id => j.id);
2088         FETCH l_cov_asset_line_csr INTO l_cov_asset_line_rec;
2089         CLOSE l_cov_asset_line_csr;
2090 
2091         -- Update Fee covered asset amount if covered asset line exists, else create covered asset line
2092         IF l_cov_asset_line_rec.cleb_cov_asset_id IS NOT NULL THEN
2093 
2094           IF  (l_asset_tax_amt <> l_cov_asset_line_rec.amount) THEN
2095 
2096             l_u_line_item_tbl(l_upd_count).cle_id            := l_cov_asset_line_rec.cleb_cov_asset_id;
2097             l_u_line_item_tbl(l_upd_count).item_id           := l_cov_asset_line_rec.cim_cov_asset_id;
2098 
2099             l_u_line_item_tbl(l_upd_count).chr_id            := p_contract_id;
2100             l_u_line_item_tbl(l_upd_count).parent_cle_id     := p_fee_line_id;
2101             l_u_line_item_tbl(l_upd_count).item_id1          := j.id;
2102             l_u_line_item_tbl(l_upd_count).item_id2          := '#';
2103             l_u_line_item_tbl(l_upd_count).item_object1_code := 'OKX_COVASST';
2104             l_u_line_item_tbl(l_upd_count).item_description  := j.item_description;
2105             l_u_line_item_tbl(l_upd_count).name              := j.name;
2106             l_u_line_item_tbl(l_upd_count).capital_amount    := l_asset_tax_amt;
2107 
2108             l_upd_count := l_upd_count + 1;
2109           END IF;
2110         ELSE
2111 
2112             l_c_line_item_tbl(l_ins_count).chr_id            := p_contract_id;
2113             l_c_line_item_tbl(l_ins_count).parent_cle_id     := p_fee_line_id;
2114             l_c_line_item_tbl(l_ins_count).item_id1          := j.id;
2115             l_c_line_item_tbl(l_ins_count).item_id2          := '#';
2116             l_c_line_item_tbl(l_ins_count).item_object1_code := 'OKX_COVASST';
2117             l_c_line_item_tbl(l_ins_count).item_description  := j.item_description;
2118             l_c_line_item_tbl(l_ins_count).name              := j.name;
2119             l_c_line_item_tbl(l_ins_count).capital_amount    := l_asset_tax_amt;
2120 
2121             l_ins_count := l_ins_count + 1;
2122         END IF;
2123 
2124       END LOOP;
2125 
2126       l_del_count := 1;
2127       --Delete covered asset lines for assets that have tax treatment 'BILLED' or NULL
2128       FOR l_del_cov_asset_rec IN l_del_cov_asset_csr2(p_chr_id     => p_contract_id,
2129                                                       p_fee_cle_id => p_fee_line_id) LOOP
2130 
2131         --Bug# 6512668: Corrected l_upd_count to l_del_count
2132         l_d_line_item_tbl(l_del_count).chr_id         := p_contract_id;
2133         l_d_line_item_tbl(l_del_count).parent_cle_id  := p_fee_line_id;
2134         l_d_line_item_tbl(l_del_count).cle_id         := l_del_cov_asset_rec.cleb_cov_asset_id;
2135         l_d_line_item_tbl(l_del_count).item_id        := l_del_cov_asset_rec.cim_cov_asset_id;
2136 
2137         l_del_count := l_del_count + 1;
2138 
2139       END LOOP;
2140     END IF;
2141 
2142     -- Associate Assets to the fee
2143     IF (l_c_line_item_tbl.COUNT > 0) THEN
2144 
2145       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2146         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: before okl_contract_line_item_pvt.create_contract_line_item ');
2147       END IF;
2148 
2149       okl_contract_line_item_pvt.create_contract_line_item(
2150                              p_api_version      => p_api_version,
2151                              p_init_msg_list    => p_init_msg_list,
2152                              x_return_status    => x_return_status,
2153                              x_msg_count        => x_msg_count,
2154                              x_msg_data         => x_msg_data,
2155                              p_line_item_tbl    => l_c_line_item_tbl,
2156                              x_line_item_tbl    => lx_c_line_item_tbl);
2157 
2158 
2159       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2160         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: x_return_status '||x_return_status);
2161       END IF;
2162 
2163       -- Check if the call was successful
2164       If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2165         RAISE update_fee_exception;
2166       End If;
2167     END IF;
2168 
2169     IF (l_u_line_item_tbl.COUNT > 0) THEN
2170 
2171       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2172         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: before okl_contract_line_item_pvt.update_contract_line_item ');
2173       END IF;
2174 
2175       okl_contract_line_item_pvt.update_contract_line_item(
2176                              p_api_version      => p_api_version,
2177                              p_init_msg_list    => p_init_msg_list,
2178                              x_return_status    => x_return_status,
2179                              x_msg_count        => x_msg_count,
2180                              x_msg_data         => x_msg_data,
2181                              p_line_item_tbl    => l_u_line_item_tbl,
2182                              x_line_item_tbl    => lx_u_line_item_tbl);
2183 
2184       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2185         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: x_return_status '||x_return_status);
2186       END IF;
2187 
2188       -- Check if the call was successful
2189       If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2190         RAISE update_fee_exception;
2191       End If;
2192     END IF;
2193 
2194     IF (l_d_line_item_tbl.COUNT > 0) THEN
2195 
2196       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2197         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: before okl_contract_line_item_pvt.delete_contract_line_item ');
2198       END IF;
2199 
2200       okl_contract_line_item_pvt.delete_contract_line_item(
2201                              p_api_version      => p_api_version,
2202                              p_init_msg_list    => p_init_msg_list,
2203                              x_return_status    => x_return_status,
2204                              x_msg_count        => x_msg_count,
2205                              x_msg_data         => x_msg_data,
2206                              p_line_item_tbl    => l_d_line_item_tbl);
2207 
2208       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2209         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: x_return_status '||x_return_status);
2210       END IF;
2211 
2212       -- Check if the call was successful
2213       If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2214         RAISE update_fee_exception;
2215       End If;
2216     END IF;
2217 
2218     IF (p_required_feetype = 'FINANCE') THEN
2219       -- Following Segment is for updating Expense Item for Financed Fee
2220       OPEN l_rul_csr(p_chr_id     => p_contract_id,
2221                      p_fee_cle_id => p_fee_line_id);
2222       FETCH l_rul_csr INTO l_rul_rec;
2223       CLOSE l_rul_csr;
2224 
2225       IF (l_rul_rec.amount <> l_upfront_tax_fee_amount) THEN
2226 
2227           -- Populate Rule Values for Expenses
2228           l_rulv_tbl(1).id                    := l_rul_rec.id;
2229           l_rulv_tbl(1).rgp_id                := l_rul_rec.rgp_id;
2230           l_rulv_tbl(1).dnz_chr_id            := l_rul_rec.dnz_chr_id;
2231           l_rulv_tbl(1).rule_information_category := l_rul_rec.rule_information_category;
2232           l_rulv_tbl(1).rule_information2     :=  TO_CHAR(l_upfront_tax_fee_amount);
2233 
2234           IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2235                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: before OKL_RULE_PUB.update_rule ');
2236           END IF;
2237           OKL_RULE_PUB.update_rule(
2238              p_api_version    => p_api_version,
2239              p_init_msg_list  => p_init_msg_list,
2240              x_return_status  => x_return_status,
2241              x_msg_count      => x_msg_count,
2242              x_msg_data       => x_msg_data,
2243              p_rulv_tbl       => l_rulv_tbl,
2244              x_rulv_tbl       => lx_rulv_tbl);
2245 
2246           IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2247             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: x_return_status '||x_return_status);
2248           END IF;
2249 
2250           If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2251             RAISE update_fee_exception;
2252           End If;
2253       END IF;
2254     END IF;
2255 
2256   EXCEPTION
2257       when update_fee_exception then
2258          x_return_status := OKL_API.G_RET_STS_ERROR;
2259 
2260   END update_fee;
2261 
2262   PROCEDURE process_tax_override(
2263             p_api_version      IN  NUMBER,
2264             p_init_msg_list    IN  VARCHAR2 DEFAULT OKL_API.G_FALSE,
2265             p_chr_id           IN  NUMBER,
2266             p_transaction_id   IN  NUMBER,
2267             x_return_status    OUT NOCOPY VARCHAR2,
2268             x_msg_count        OUT NOCOPY NUMBER,
2269             x_msg_data         OUT NOCOPY VARCHAR2
2270             )
2271   IS
2272 
2273   l_api_name	CONSTANT VARCHAR2(30) := 'UPDATE_FEE';
2274   l_api_version	CONSTANT NUMBER	      := 1.0;
2275 
2276   l_rule_info_rec  rule_info_csr%ROWTYPE;
2277 
2278   -- Cursor to check if Sales Tax fee exists
2279   -- Bug# 6512668: Exclude fee line in Abandoned status
2280   CURSOR l_fee_csr (p_chr_id IN NUMBER)  IS
2281   SELECT cle.id,
2282          kle.fee_type
2283   FROM   okc_k_lines_b cle,
2284          okl_k_lines kle
2285   WHERE  cle.id = kle.id
2286   AND    cle.dnz_chr_id = p_chr_id
2287   AND    cle.chr_id = p_chr_id
2288   AND    kle.fee_purpose_code = 'SALESTAX'
2289   AND    cle.sts_code <> 'ABANDONED';
2290 
2291   l_fee_rec l_fee_csr%ROWTYPE;
2292 
2293   l_upfront_tax_prog_sts OKL_BOOK_CONTROLLER_TRX.progress_status%TYPE;
2294 
2295   BEGIN
2296 
2297     IF (G_DEBUG_ENABLED = 'Y') THEN
2298       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
2299     END IF;
2300 
2301     x_return_status := OKL_API.START_ACTIVITY(
2302                         p_api_name      => l_api_name,
2303                         p_pkg_name      => g_pkg_name,
2304                         p_init_msg_list => p_init_msg_list,
2305                         l_api_version   => l_api_version,
2306                         p_api_version   => p_api_version,
2307                         p_api_type      => G_API_TYPE,
2308                         x_return_status => x_return_status);
2309 
2310     -- check if activity started successfully
2311     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2312        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2313     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
2314        RAISE OKL_API.G_EXCEPTION_ERROR;
2315     END IF;
2316 
2317     OPEN  rule_info_csr(p_contract_id => p_chr_id);
2318     FETCH rule_info_csr INTO l_rule_info_rec;
2319     IF rule_info_csr%NOTFOUND THEN
2320       OKL_API.set_message( p_app_name     => G_APP_NAME,
2321                            p_msg_name     => 'OKL_QA_ST_MISSING');
2322       CLOSE rule_info_csr;
2323       RAISE OKL_API.G_EXCEPTION_ERROR;
2324     END IF;
2325     CLOSE rule_info_csr;
2326 
2327     IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2328       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: Default Fee Type '||l_rule_info_rec.rule_information1);
2329     END IF;
2330 
2331     OPEN l_fee_csr(p_chr_id => p_chr_id);
2332     FETCH l_fee_csr INTO l_fee_rec;
2333     CLOSE l_fee_csr;
2334 
2335     IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2336       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: Sales Tax Fee Line Id '||l_fee_rec.id);
2337       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: Sales Tax Fee Type '||l_fee_rec.fee_type);
2338     END IF;
2339 
2340     IF l_fee_rec.id IS NOT NULL THEN
2341 
2342       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2343         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: Before OKL_LA_SALES_TAX_PVT.update_fee ');
2344       END IF;
2345 
2346       OKL_LA_SALES_TAX_PVT.update_fee
2347        (p_api_version      => p_api_version,
2348         p_init_msg_list    => p_init_msg_list,
2349         p_contract_id      => p_chr_id,
2350         p_transaction_id   => p_transaction_id,
2351         p_fee_line_id      => l_fee_rec.id,
2352         p_required_feetype => l_fee_rec.fee_type,
2353         p_default_feetype  => l_rule_info_rec.rule_information1,
2354         x_return_status    => x_return_status,
2355         x_msg_count        => x_msg_count,
2356         x_msg_data         => x_msg_data);
2357 
2358       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2359         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: After OKL_LA_SALES_TAX_PVT.update_fee: x_return_status '||x_return_status);
2360       END IF;
2361 
2362       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2363         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2364       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
2365         RAISE OKL_API.G_EXCEPTION_ERROR;
2366       END IF;
2367 
2368       OKL_LA_SALES_TAX_PVT.validate_upfront_tax_fee(
2369        p_api_version       => p_api_version,
2370        p_init_msg_list     => p_init_msg_list,
2371        x_return_status     => x_return_status,
2372        x_msg_count         => x_msg_count,
2373        x_msg_data          => x_msg_data,
2374        p_chr_id            => p_chr_id);
2375 
2376       IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
2377         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Update Fee Procedure: After OKL_LA_SALES_TAX_PVT.validate_upfront_tax_fee: x_return_status '||x_return_status);
2378       END IF;
2379 
2380       IF (x_return_status <> OKL_API.G_RET_STS_SUCCESS) THEN
2381         l_upfront_tax_prog_sts := OKL_BOOK_CONTROLLER_PVT.G_PROG_STS_ERROR;
2382       ELSE
2383         l_upfront_tax_prog_sts := OKL_BOOK_CONTROLLER_PVT.G_PROG_STS_COMPLETE;
2384       END IF;
2385 
2386       OKL_BOOK_CONTROLLER_PVT.update_book_controller_trx(
2387         p_api_version        => p_api_version,
2388         p_init_msg_list      => p_init_msg_list,
2389         x_return_status      => x_return_status,
2390         x_msg_count          => x_msg_count,
2391         x_msg_data           => x_msg_data,
2392         p_khr_id             => p_chr_id ,
2393         p_prog_short_name    => OKL_BOOK_CONTROLLER_PVT.G_CALC_UPFRONT_TAX ,
2394         p_progress_status    => l_upfront_tax_prog_sts);
2395 
2396       IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2397         RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2398       ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
2399         RAISE OKL_API.G_EXCEPTION_ERROR;
2400       END IF;
2401     END IF;
2402 
2403     --Update Contract Status to Passed
2404     OKL_CONTRACT_STATUS_PUB.update_contract_status(
2405       p_api_version   => p_api_version,
2406       p_init_msg_list => p_init_msg_list,
2407       x_return_status => x_return_status,
2408       x_msg_count     => x_msg_count,
2409       x_msg_data      => x_msg_data,
2410       p_khr_status    => 'PASSED',
2411       p_chr_id        => p_chr_id);
2412 
2413     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2414       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2415     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
2416       RAISE OKL_API.G_EXCEPTION_ERROR;
2417     END IF;
2418 
2419     --call to cascade status on to lines
2420     OKL_CONTRACT_STATUS_PUB.cascade_lease_status
2421       (p_api_version     => p_api_version,
2422        p_init_msg_list   => p_init_msg_list,
2423        x_return_status   => x_return_status,
2424        x_msg_count       => x_msg_count,
2425        x_msg_data        => x_msg_data,
2426        p_chr_id          => p_chr_id);
2427 
2428     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2429       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2430     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
2431       RAISE OKL_API.G_EXCEPTION_ERROR;
2432     END IF;
2433 
2434     --Update status of Price Contract process to Pending
2435     OKL_BOOK_CONTROLLER_PVT.update_book_controller_trx(
2436         p_api_version        => p_api_version,
2437         p_init_msg_list      => p_init_msg_list,
2438         x_return_status      => x_return_status,
2439         x_msg_count          => x_msg_count,
2440         x_msg_data           => x_msg_data,
2441         p_khr_id             => p_chr_id ,
2442         p_prog_short_name    => OKL_BOOK_CONTROLLER_PVT.G_PRICE_CONTRACT,
2443         p_progress_status    => OKL_BOOK_CONTROLLER_PVT.G_PROG_STS_PENDING);
2444 
2445     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2446       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2447     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
2448       RAISE OKL_API.G_EXCEPTION_ERROR;
2449     END IF;
2450 
2451     --Update status of Submit Contract process to Pending
2452     OKL_BOOK_CONTROLLER_PVT.update_book_controller_trx(
2453         p_api_version        => p_api_version,
2454         p_init_msg_list      => p_init_msg_list,
2455         x_return_status      => x_return_status,
2456         x_msg_count          => x_msg_count,
2457         x_msg_data           => x_msg_data,
2458         p_khr_id             => p_chr_id ,
2459         p_prog_short_name    => OKL_BOOK_CONTROLLER_PVT.G_SUBMIT_CONTRACT,
2460         p_progress_status    => OKL_BOOK_CONTROLLER_PVT.G_PROG_STS_PENDING);
2461 
2462     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
2463       RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
2464     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
2465       RAISE OKL_API.G_EXCEPTION_ERROR;
2466     END IF;
2467 
2468   EXCEPTION
2469 
2470     WHEN OKL_API.G_EXCEPTION_ERROR THEN
2471       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
2472                         p_api_name  => l_api_name,
2473                         p_pkg_name  => g_pkg_name,
2474                         p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
2475                         x_msg_count => x_msg_count,
2476                         x_msg_data  => x_msg_data,
2477                         p_api_type  => g_api_type);
2478 
2479     WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2480         x_return_status := OKL_API.HANDLE_EXCEPTIONS(
2481                         p_api_name  => l_api_name,
2482                         p_pkg_name  => g_pkg_name,
2483                         p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
2484                         x_msg_count => x_msg_count,
2485                         x_msg_data  => x_msg_data,
2486                         p_api_type  => g_api_type);
2487 
2488      WHEN OTHERS THEN
2489         x_return_status := OKL_API.HANDLE_EXCEPTIONS(
2490                         p_api_name  => l_api_name,
2491                         p_pkg_name  => g_pkg_name,
2492                         p_exc_name  => 'OTHERS',
2493                         x_msg_count => x_msg_count,
2494                         x_msg_data  => x_msg_data,
2495                         p_api_type  => g_api_type);
2496 
2497   END process_tax_override;
2498   -- R12B Authoring OA Migration
2499 
2500   -- Procedure to create Financed fee
2501   Procedure create_fee(
2502                       p_api_version      IN  NUMBER,
2503                       p_init_msg_list    IN  VARCHAR2 DEFAULT Okl_Api.G_FALSE,
2504                       p_contract_id      IN  NUMBER,
2505                       p_transaction_id   IN  NUMBER,
2506                       p_default_feetype  IN  VARCHAR2,
2507                       p_required_feetype IN  VARCHAR2,
2508                       p_stream_id        IN  NUMBER,
2509                       x_return_status    OUT NOCOPY VARCHAR2,
2510                       x_msg_count        OUT NOCOPY NUMBER,
2511                       x_msg_data         OUT NOCOPY VARCHAR2
2512                       )
2513   IS
2514     l_fee_types_rec   OKL_MAINTAIN_FEE_PVT.fee_types_rec_type;
2515     lx_fee_types_rec  OKL_MAINTAIN_FEE_PVT.fee_types_rec_type;
2516     l_line_item_tbl   okl_contract_line_item_pvt.line_item_tbl_type;
2517     lx_line_item_tbl  okl_contract_line_item_pvt.line_item_tbl_type;
2518 
2519     l_rgpv_rec          rgpv_rec_type;
2520     l_rulv_rec          rulv_rec_type;
2521     l_rulv_tbl          rulv_tbl_type;
2522     lx_rgpv_rec         rgpv_rec_type;
2523     lx_rulv_rec         rulv_rec_type;
2524     lx_rulv_tbl         rulv_tbl_type;
2525 
2526     i NUMBER := 0;
2527     l_financed_amount NUMBER;
2528     l_capitalized_amount NUMBER;
2529 
2530     -- R12B Authoring OA Migration
2531     -- Cursor to check if financed fee exists
2532     -- Bug# 6512668: Exclude fee line in Abandoned status
2533     CURSOR check_fee_csr ( l_fee_type IN VARCHAR2 )  IS
2534     SELECT cle.id
2535     FROM   OKC_K_LINES_b cle,
2536            OKL_K_LINES KLE
2537     WHERE  cle.id = kle.id
2538     AND    cle.dnz_chr_id = p_contract_id
2539     AND    kle.fee_purpose_code = 'SALESTAX'
2540     AND    kle.fee_type = l_fee_type
2541     AND    cle.sts_code <> 'ABANDONED';
2542 
2543     l_fee_line_id OKC_K_LINES_B.id%TYPE;
2544     -- R12B Authoring OA Migration
2545 
2546     -- cursor to get item id
2547     CURSOR stream_csr (stream_type_id IN NUMBER)  IS
2548     select name
2549     from   OKL_STRM_TYPE_TL
2550     where  id = stream_type_id
2551     AND    language = USERENV('LANG');
2552 
2553     -- Get contract dates
2554     CURSOR contract_dates_csr IS
2555     SELECT start_date, end_date
2556     FROM   okc_k_headers_b
2557     WHERE  id = p_contract_id;
2558 
2559     -- Cursor to get assets for financed fee association
2560     -- Bug# 6512668: Exclude asset lines in Abandoned status
2561     CURSOR get_asset_csr1 (p_fee_type IN VARCHAR2)
2562     IS
2563     SELECT cle.id, cle.name, cle.item_description
2564     FROM   okc_k_lines_v cle,
2565            okc_rule_groups_b rg1,
2566            okc_rules_b rl1,
2567            okc_line_styles_b lse,
2568            okc_rule_groups_b rg2,
2569            okc_rules_b rl2
2570     WHERE  cle.chr_ID = p_contract_id
2571     AND    cle.lse_id     = lse.id
2572     AND    lse.lty_code   = 'FREE_FORM1'
2573     AND    cle.dnz_chr_id = rg1.dnz_chr_id
2574     AND    cle.id         = rg1.cle_id
2575     AND    cle.dnz_chr_id = rl1.dnz_chr_id
2576     AND    rg1.id         = rl1.rgp_id
2577     AND    rg1.rgd_code   = 'LAASTX'
2578     AND    rl1.rule_information_category = 'LAASTX'
2579     AND    ( rl1.rule_information11 IS NULL
2580              OR
2581              rl1.rule_information11 = p_fee_type)
2582     AND    cle.dnz_chr_id = rg2.dnz_chr_id
2583     AND    cle.dnz_chr_id = rl2.dnz_chr_id
2584     AND    rg2.id         = rl2.rgp_id
2585     AND    rg2.rgd_code   = 'LAHDTX'
2586     AND    rl2.rule_information_category = 'LASTPR'
2587     AND    rl2.rule_information1 = p_fee_type
2588     AND    cle.sts_code <> 'ABANDONED';
2589 
2590 
2591     -- Cursor to get assets for fee association if T and C Tax treatment is
2592     -- 'BILLED'
2593     -- Bug# 6512668: Exclude asset lines in Abandoned status
2594     CURSOR get_asset_csr2 (p_fee_type IN VARCHAR2)
2595     IS
2596     SELECT cle.id, cle.name, cle.item_description
2597     FROM   okc_k_lines_v cle,
2598            okc_rule_groups_b rg1,
2599            okc_rules_b rl1,
2600            okc_line_styles_b lse
2601     WHERE  cle.dnz_chr_ID = p_contract_id
2602     AND    cle.lse_id     = lse.id
2603     AND    lse.lty_code   = 'FREE_FORM1'
2604     AND    cle.dnz_chr_id = rg1.dnz_chr_id
2605     AND    cle.id         = rg1.cle_id
2606     AND    cle.dnz_chr_id = rl1.dnz_chr_id
2607     AND    rg1.id         = rl1.rgp_id
2608     AND    rg1.rgd_code   = 'LAASTX'
2609     AND    rl1.rule_information_category = 'LAASTX'
2610     AND    rl1.rule_information11 = p_fee_type
2611     AND    cle.sts_code <> 'ABANDONED';
2612 
2613 
2614     -- Cursor to get tax amounts for assets
2615     CURSOR get_asset_tax_amt_csr (asset_line_id IN NUMBER)
2616     IS
2617     SELECT NVL(SUM(NVL(TOTAL_TAX,0)),0)
2618     FROM   okl_tax_sources txs
2619     WHERE  txs.KHR_ID = p_contract_id
2620     AND    txs.trx_id = p_transaction_id
2621     AND    txs.kle_id = asset_line_id
2622     AND    txs.TAX_LINE_STATUS_CODE = 'ACTIVE'
2623     AND    txs.TAX_CALL_TYPE_CODE = 'UPFRONT_TAX';
2624 
2625     -- Cursor Types
2626     contract_dates_csr_rec contract_dates_csr%ROWTYPE;
2627 
2628     l_finance_fee_exists VARCHAR(1);
2629     l_capitalized_fee_exists VARCHAR(1);
2630     l_item_name VARCHAR2(150);
2631     l_asset_tax_amt NUMBER;
2632     l_asset_count NUMBER := 0;
2633 
2634      -- Define constants
2635     create_fee_exception exception;
2636 
2637     x_msg_index_out Number;
2638 
2639   BEGIN
2640     IF (G_DEBUG_ENABLED = 'Y') THEN
2641       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
2642     END IF;
2643 
2644     x_return_status  := Okl_Api.G_RET_STS_SUCCESS;
2645 
2646     -- If upfront tax is 'FINANCED'
2647     If (p_required_feetype = 'FINANCE') THEN
2648 
2649       --l_financed_amount := get_financed_tax ( p_Contract_id, p_transaction_id);
2650       l_financed_amount := get_financed_tax ( p_Contract_id, p_default_feetype);
2651       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2652               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: FINANCE segment: l_financed_amount: '
2653                                ||l_financed_amount);
2654       END IF;
2655 
2656       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2657               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before check_fee_csr ');
2658       END IF;
2659 
2660       -- Check If finance fee already exists
2661       -- if exists do nothing, because we can only create once
2662       OPEN check_fee_csr ('FINANCED');
2663       -- R12B Authoring OA Migration
2664       FETCH check_fee_csr INTO l_fee_line_id;
2665       -- R12B Authoring OA Migration
2666 
2667       IF check_fee_csr%NOTFOUND THEN
2668         l_finance_fee_exists := OKL_API.G_FALSE;
2669       Else
2670         l_finance_fee_exists := OKL_API.G_TRUE;
2671       END IF;
2672       CLOSE check_fee_csr;
2673 
2674       -- Create Fee only if the amount is > 0 and Sale Tax Fee does not
2675       -- exist already
2676       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2677               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: FINANCE segment: before IF condition');
2678       END IF;
2679       If ((l_financed_amount > 0) AND
2680           (l_finance_fee_exists = OKL_API.G_FALSE)) THEN
2681 
2682         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2683                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before stream_csr ');
2684         END IF;
2685         -- get item_id1 value
2686         OPEN  stream_csr(p_stream_id);
2687         FETCH stream_csr INTO l_item_name;
2688         IF stream_csr%NOTFOUND THEN
2689           OKL_API.set_message(
2690               p_app_name     => G_APP_NAME,
2691               p_msg_name     => 'OKL_LA_ST_STREAM_ERROR',
2692               p_token1       => 'FEE_TYPE',
2693               p_token1_value => p_required_feetype);
2694           CLOSE stream_csr;
2695           RAISE create_fee_exception;
2696         END IF;
2697         CLOSE stream_csr;
2698 
2699         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2700                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before contract_dates_csr ');
2701         END IF;
2702 
2703         -- get contract dates for fee effective date
2704         OPEN  contract_dates_csr;
2705         FETCH contract_dates_csr INTO contract_dates_csr_rec;
2706         IF contract_dates_csr%NOTFOUND THEN
2707           --Okl_Api.SET_MESSAGE(G_APP_NAME, G_INVALID_VALUE,G_COL_NAME_TOKEN,'Fee Effective dates cannot be derived');
2708           Okl_Api.SET_MESSAGE(G_APP_NAME, 'Cannot find contract effective dates for sales tax fee creation.');
2709           CLOSE contract_dates_csr;
2710           RAISE create_fee_exception;
2711         END IF;
2712         CLOSE contract_dates_csr;
2713 
2714         l_fee_types_rec.item_id1         := to_number(p_stream_id);
2715         l_fee_types_rec.dnz_chr_id       := p_contract_id;
2716         l_fee_types_rec.fee_purpose_code := 'SALESTAX';
2717         l_fee_types_rec.fee_type         := 'FINANCED';
2718         l_fee_types_rec.item_name        := l_item_name;
2719         l_fee_types_rec.amount           := l_financed_amount;
2720         l_fee_types_rec.effective_from   := contract_dates_csr_rec.start_date;
2721         l_fee_types_rec.effective_to     := contract_dates_csr_rec.end_date;
2722 
2723         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2724                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before OKL_MAINTAIN_FEE_PVT ');
2725         END IF;
2726         -- create fee top line
2727         OKL_MAINTAIN_FEE_PVT.create_fee_type(
2728                                               p_api_version    => p_api_version,
2729                                               p_init_msg_list  => p_init_msg_list,
2730                                               x_return_status  => x_return_status,
2731                                               x_msg_count      => x_msg_count,
2732                                               x_msg_data       => x_msg_data,
2733                                               p_fee_types_rec  => l_fee_types_rec,
2734                                               x_fee_types_rec  => lx_fee_types_rec
2735                                              );
2736         -- Check if the call was successful
2737         If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2738           RAISE create_fee_exception;
2739         End If;
2740 
2741 
2742         IF (p_default_feetype = 'FINANCE') THEN
2743           i:=1;
2744           FOR j in get_asset_csr1 ('FINANCE')
2745           LOOP
2746 
2747             IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2748                           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before get_asset_tax_amt_csr ');
2749             END IF;
2750             -- get asset tax amount
2751             OPEN  get_asset_tax_amt_csr(j.id);
2752             FETCH get_asset_tax_amt_csr INTO l_asset_tax_amt;
2753             IF get_asset_tax_amt_csr%NOTFOUND THEN
2754               --Okl_Api.SET_MESSAGE(G_APP_NAME, G_INVALID_VALUE,G_COL_NAME_TOKEN,'Asset Tax Amount cannot be derived');
2755               Okl_Api.SET_MESSAGE(G_APP_NAME, 'OKL_LA_ST_K_ID_ERROR');
2756               IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2757                               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Cannot derive asset tax amount for ID'|| j.id);
2758               END IF;
2759               CLOSE get_asset_tax_amt_csr;
2760               RAISE create_fee_exception;
2761             END IF;
2762             CLOSE get_asset_tax_amt_csr;
2763 
2764             -- Hard Code Asset Tax Amount for now
2765             --l_asset_tax_amt := 50;
2766 
2767             l_line_item_tbl(i).chr_id            := p_contract_id;
2768             l_line_item_tbl(i).parent_cle_id     := lx_fee_types_rec.line_id ;
2769             l_line_item_tbl(i).item_id1          := j.id;
2770             l_line_item_tbl(i).item_id2          := '#';
2771             l_line_item_tbl(i).item_object1_code := 'OKX_COVASST';
2772             l_line_item_tbl(i).item_description  := j.item_description;
2773             l_line_item_tbl(i).name              := j.name;
2774             l_line_item_tbl(i).capital_amount    := l_asset_tax_amt;
2775 
2776             i := i+1;
2777           END LOOP;
2778         END IF;
2779 
2780         IF (p_default_feetype = 'BILLED') THEN
2781           i:=1;
2782           FOR j in get_asset_csr2 ('FINANCE')
2783           LOOP
2784 
2785             IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2786                           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before get_asset_tax_amt_csr ');
2787             END IF;
2788             -- get asset tax amount
2789             OPEN  get_asset_tax_amt_csr(j.id);
2790             FETCH get_asset_tax_amt_csr INTO l_asset_tax_amt;
2791             IF get_asset_tax_amt_csr%NOTFOUND THEN
2792               OKL_API.set_message( p_app_name     => G_APP_NAME,
2793                                     p_msg_name     => 'OKL_LA_ST_ASSET_TAX_AMT_ERROR');
2794               IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2795                               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Cannot derive asset tax amount. for ID'|| j.id);
2796               END IF;
2797               CLOSE get_asset_tax_amt_csr;
2798               RAISE create_fee_exception;
2799             END IF;
2800             CLOSE get_asset_tax_amt_csr;
2801 
2802             -- Hard Code Asset Tax Amount for now
2803             --l_asset_tax_amt := 50;
2804 
2805             l_line_item_tbl(i).chr_id            := p_contract_id;
2806             l_line_item_tbl(i).parent_cle_id     := lx_fee_types_rec.line_id ;
2807             l_line_item_tbl(i).item_id1          := j.id;
2808             l_line_item_tbl(i).item_id2          := '#';
2809             l_line_item_tbl(i).item_object1_code := 'OKX_COVASST';
2810             l_line_item_tbl(i).item_description  := j.item_description;
2811             l_line_item_tbl(i).name              := j.name;
2812             l_line_item_tbl(i).capital_amount    := l_asset_tax_amt;
2813 
2814             i := i+1;
2815           END LOOP;
2816         END IF;
2817 
2818 
2819 
2820         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2821                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before okl_contract_line_item_pvt ');
2822         END IF;
2823         -- Associate Assets to the fee
2824         okl_contract_line_item_pvt.create_contract_line_item(
2825                              p_api_version      => p_api_version,
2826                              p_init_msg_list    => p_init_msg_list,
2827                              --p_init_msg_list    => 'T',
2828                              x_return_status    => x_return_status,
2829                              x_msg_count        => x_msg_count,
2830                              x_msg_data         => x_msg_data,
2831                              p_line_item_tbl    => l_line_item_tbl,
2832                              x_line_item_tbl    => lx_line_item_tbl);
2833 
2834         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2835                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: x_return_status '||x_return_status);
2836         END IF;
2837         FOR k in 1..x_msg_count LOOP
2838           fnd_msg_pub.get (p_encoded => 'F',
2839                                  p_data => x_msg_data,
2840                                  p_msg_index_out => x_msg_index_out
2841                                 );
2842           IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2843                       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment:'||to_char(k) || ':' || x_msg_data);
2844           END IF;
2845          END LOOP;
2846 
2847         -- Check if the call was successful
2848         If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2849           RAISE create_fee_exception;
2850         End If;
2851 
2852         -- Following Segment is for creating Expense Item for Financed Fee
2853         -- It is implemeted using Rule APIs, so will use the same
2854 
2855         -- Populate Rule Group Values for Expenses
2856         l_rgpv_rec.rgd_code   := 'LAFEXP';
2857         l_rgpv_rec.dnz_chr_id := p_contract_id;
2858         l_rgpv_rec.cle_id     := lx_fee_types_rec.line_id;
2859         l_rgpv_rec.rgp_type   := 'KRG';
2860 
2861         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2862                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before OKL_RULE_PUB.create_rule_group ');
2863         END IF;
2864 
2865         OKL_RULE_PUB.create_rule_group(
2866             p_api_version    => p_api_version,
2867             p_init_msg_list  => p_init_msg_list,
2868             x_return_status  => x_return_status,
2869             x_msg_count      => x_msg_count,
2870             x_msg_data       => x_msg_data,
2871             p_rgpv_rec       => l_rgpv_rec,
2872             x_rgpv_rec       => lx_rgpv_rec);
2873 
2874         If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2875           RAISE create_fee_exception;
2876         End If;
2877 
2878         -- Populate Rule Values for Expenses
2879         l_rulv_tbl(1).rgp_id                    := lx_rgpv_rec.id;
2880         l_rulv_tbl(1).dnz_chr_id                := p_contract_id;
2881         l_rulv_tbl(1).rule_information_category := 'LAFEXP';
2882         l_rulv_tbl(1).rule_information1         :=  1;
2883         l_rulv_tbl(1).rule_information2         :=  l_financed_amount;
2884         l_rulv_tbl(1).WARN_YN                   := 'N';
2885         l_rulv_tbl(1).STD_TEMPLATE_YN           := 'N';
2886         l_rulv_tbl(1).template_yn               := 'N';
2887 
2888         l_rulv_tbl(2).rgp_id                    := lx_rgpv_rec.id;
2889         l_rulv_tbl(2).dnz_chr_id                := p_contract_id;
2890         l_rulv_tbl(2).rule_information_category := 'LAFREQ';
2891         l_rulv_tbl(2).OBJECT1_ID1               := 'M';
2892         l_rulv_tbl(2).OBJECT1_ID2               := '#';
2893         l_rulv_tbl(2).JTOT_OBJECT1_CODE         := 'OKL_TUOM';
2894         l_rulv_tbl(2).WARN_YN                   := 'N';
2895         l_rulv_tbl(2).STD_TEMPLATE_YN           := 'N';
2896         l_rulv_tbl(2).template_yn               := 'N';
2897 
2898         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2899                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: FINANCE segment: before OKL_RULE_PUB.create_rule ');
2900         END IF;
2901         OKL_RULE_PUB.create_rule(
2902              p_api_version    => p_api_version,
2903              p_init_msg_list  => p_init_msg_list,
2904              x_return_status  => x_return_status,
2905              x_msg_count      => x_msg_count,
2906              x_msg_data       => x_msg_data,
2907              p_rulv_tbl       => l_rulv_tbl,
2908              x_rulv_tbl       => lx_rulv_tbl);
2909 
2910         If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2911           RAISE create_fee_exception;
2912         End If;
2913 
2914       -- R12B Authoring OA Migration
2915       Elsif (l_finance_fee_exists = OKL_API.G_TRUE) Then
2916 
2917         OKL_LA_SALES_TAX_PVT.update_fee
2918           (p_api_version      => p_api_version,
2919            p_init_msg_list    => p_init_msg_list,
2920            p_contract_id      => p_contract_id,
2921            p_transaction_id   => p_transaction_id,
2922            p_fee_line_id      => l_fee_line_id,
2923            p_required_feetype => p_required_feetype,
2924            p_default_feetype  => p_default_feetype,
2925            x_return_status    => x_return_status,
2926            x_msg_count        => x_msg_count,
2927            x_msg_data         => x_msg_data);
2928 
2929          If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
2930            RAISE create_fee_exception;
2931          End If;
2932       -- R12B Authoring OA Migration
2933 
2934       End If;
2935 
2936     End If;
2937 
2938     -- If upfront tax is 'CAPITALIZED'
2939     If (p_required_feetype = 'CAPITALIZE')
2940     THEN
2941 
2942       --l_capitalized_amount := get_capitalized_tax ( p_Contract_id
2943       --                                              ,p_transaction_id
2944       --                                              ,p_default_feetype);
2945       l_capitalized_amount := get_capitalized_tax ( p_Contract_id
2946                                                     ,p_default_feetype);
2947 
2948 
2949       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2950               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: l_capitalized_amount: '
2951                                ||l_capitalized_amount);
2952       END IF;
2953       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2954               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: before check_fee_csr ');
2955       END IF;
2956 
2957       OPEN check_fee_csr ('CAPITALIZED');
2958       -- R12B Authoring OA Migration
2959       FETCH check_fee_csr INTO l_fee_line_id;
2960       -- R12B Authoring OA Migration
2961       IF check_fee_csr%NOTFOUND THEN
2962         l_capitalized_fee_exists := OKL_API.G_FALSE;
2963       Else
2964         l_capitalized_fee_exists := OKL_API.G_TRUE;
2965       END IF;
2966       CLOSE check_fee_csr;
2967 
2968       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2969               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: before IF condition');
2970       END IF;
2971       -- Create Fee only if the amount is > 0 and fee does not exist
2972       If ((l_capitalized_amount > 0)  AND
2973           (l_capitalized_fee_exists = OKL_API.G_FALSE)) THEN
2974 
2975         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2976                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: before stream_csr ');
2977         END IF;
2978         -- get item_id1 value
2979         OPEN  stream_csr(p_stream_id);
2980         FETCH stream_csr INTO l_item_name;
2981 
2982         IF stream_csr%NOTFOUND THEN
2983           OKL_API.set_message(
2984               p_app_name     => G_APP_NAME,
2985               p_msg_name     => 'OKL_LA_ST_STREAM_ERROR',
2986               p_token1       => 'FEE_TYPE',
2987               p_token1_value => p_required_feetype);
2988           CLOSE stream_csr;
2989           RAISE create_fee_exception;
2990         END IF;
2991         CLOSE stream_csr;
2992 
2993         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
2994                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: before contract_dates_csr ');
2995         END IF;
2996         -- get contract dates for fee effective date
2997         OPEN  contract_dates_csr;
2998         FETCH contract_dates_csr INTO contract_dates_csr_rec;
2999         IF contract_dates_csr%NOTFOUND THEN
3000           --Okl_Api.SET_MESSAGE(G_APP_NAME, G_INVALID_VALUE,G_COL_NAME_TOKEN,'Fee Effective dates cannot be derived');
3001           Okl_Api.SET_MESSAGE(G_APP_NAME, 'Cannot find contract effective dates for sales tax fee creation.');
3002           CLOSE contract_dates_csr;
3003           RAISE create_fee_exception;
3004         END IF;
3005         CLOSE contract_dates_csr;
3006 
3007         l_fee_types_rec.dnz_chr_id       := p_contract_id;
3008         l_fee_types_rec.fee_type         := 'CAPITALIZED';
3009         l_fee_types_rec.fee_purpose_code := 'SALESTAX';
3010         l_fee_types_rec.item_name        := l_item_name;
3011         l_fee_types_rec.item_id1         := to_number(p_stream_id);
3012         l_fee_types_rec.amount           := l_capitalized_amount;
3013         l_fee_types_rec.effective_from   := contract_dates_csr_rec.start_date;
3014         l_fee_types_rec.effective_to     := contract_dates_csr_rec.end_date;
3015 
3016         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
3017                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: before OKL_MAINTAIN_FEE_PVT ');
3018         END IF;
3019         -- Create Fee Top Line
3020         OKL_MAINTAIN_FEE_PVT.create_fee_type(
3021                                               p_api_version    => p_api_version,
3022                                               p_init_msg_list  => p_init_msg_list,
3023                                               x_return_status  => x_return_status,
3024                                               x_msg_count      => x_msg_count,
3025                                               x_msg_data       => x_msg_data,
3026                                               p_fee_types_rec  => l_fee_types_rec,
3027                                               x_fee_types_rec  => lx_fee_types_rec
3028                                              );
3029         -- Check if the call was successful
3030         If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
3031           RAISE create_fee_exception;
3032         End If;
3033 
3034         IF (p_default_feetype = 'CAPITALIZE') THEN
3035           i:=1;
3036           FOR j in get_asset_csr1 ('CAPITALIZE')
3037           LOOP
3038             -- get asset tax amount
3039             IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
3040                           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: before get_asset_tax_amt_csr ');
3041             END IF;
3042             OPEN  get_asset_tax_amt_csr(j.id);
3043             FETCH get_asset_tax_amt_csr INTO l_asset_tax_amt;
3044             IF get_asset_tax_amt_csr%NOTFOUND THEN
3045               OKL_API.set_message( p_app_name     => G_APP_NAME,
3046                                     p_msg_name     => 'OKL_LA_ST_ASSET_TAX_AMT_ERROR');
3047               IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
3048                               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Cannot derive asset tax amount. for ID'|| j.id);
3049               END IF;
3050               CLOSE get_asset_tax_amt_csr;
3051               RAISE create_fee_exception;
3052             END IF;
3053             CLOSE get_asset_tax_amt_csr;
3054 
3055             -- Hard Code Asset Tax Amount for now
3056             --l_asset_tax_amt := 50;
3057 
3058             l_line_item_tbl(i).chr_id            := p_contract_id;
3059             l_line_item_tbl(i).parent_cle_id     := lx_fee_types_rec.line_id;
3060             l_line_item_tbl(i).item_id1          := j.id;
3061             l_line_item_tbl(i).item_id2          := '#';
3062             l_line_item_tbl(i).item_object1_code := 'OKX_COVASST';
3063             l_line_item_tbl(i).item_description  := j.item_description;
3064             l_line_item_tbl(i).name              := j.name;
3065             l_line_item_tbl(i).capital_amount    := l_asset_tax_amt;
3066 
3067             i := i+1;
3068 
3069           END LOOP;
3070         END IF;
3071 
3072         IF (p_default_feetype = 'BILLED') THEN
3073           i:=1;
3074           FOR j in get_asset_csr2 ('CAPITALIZE')
3075           LOOP
3076 
3077             IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
3078                           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Create Fee Procedure: CAPITALIZE segment: before get_asset_tax_amt_csr ');
3079             END IF;
3080             -- get asset tax amount
3081             OPEN  get_asset_tax_amt_csr(j.id);
3082             FETCH get_asset_tax_amt_csr INTO l_asset_tax_amt;
3083             IF get_asset_tax_amt_csr%NOTFOUND THEN
3084               OKL_API.set_message( p_app_name     => G_APP_NAME,
3085                                     p_msg_name     => 'OKL_LA_ST_ASSET_TAX_AMT_ERROR');
3086               IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
3087                               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Cannot derive asset tax amount. for ID'|| j.id);
3088               END IF;
3089               CLOSE get_asset_tax_amt_csr;
3090               RAISE create_fee_exception;
3091             END IF;
3092             CLOSE get_asset_tax_amt_csr;
3093 
3094             -- Hard Code Asset Tax Amount for now
3095             --l_asset_tax_amt := 50;
3096 
3097             l_line_item_tbl(i).chr_id            := p_contract_id;
3098             l_line_item_tbl(i).parent_cle_id     := lx_fee_types_rec.line_id ;
3099             l_line_item_tbl(i).item_id1          := j.id;
3100             l_line_item_tbl(i).item_id2          := '#';
3101             l_line_item_tbl(i).item_object1_code := 'OKX_COVASST';
3102             l_line_item_tbl(i).item_description  := j.item_description;
3103             l_line_item_tbl(i).name              := j.name;
3104             l_line_item_tbl(i).capital_amount    := l_asset_tax_amt;
3105 
3106             i := i+1;
3107           END LOOP;
3108         END IF;
3109 
3110 
3111 
3112         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
3113                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Pre Book Procedure: CAPITALIZE segment: before okl_contract_line_item_pvt ');
3114         END IF;
3115         -- Associate Assets to the fee
3116         okl_contract_line_item_pvt.create_contract_line_item(
3117                              p_api_version      => p_api_version,
3118                              p_init_msg_list    => p_init_msg_list,
3119                              x_return_status    => x_return_status,
3120                              x_msg_count        => x_msg_count,
3121                              x_msg_data         => x_msg_data,
3122                              p_line_item_tbl    => l_line_item_tbl,
3123                              x_line_item_tbl    => lx_line_item_tbl);
3124 
3125         -- Check if the call was successful
3126         If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
3127           RAISE create_fee_exception;
3128         End If;
3129 
3130       -- R12B Authoring OA Migration
3131       Elsif (l_capitalized_fee_exists = OKL_API.G_TRUE) Then
3132 
3133         OKL_LA_SALES_TAX_PVT.update_fee
3134           (p_api_version      => p_api_version,
3135            p_init_msg_list    => p_init_msg_list,
3136            p_contract_id      => p_contract_id,
3137            p_transaction_id   => p_transaction_id,
3138            p_fee_line_id      => l_fee_line_id,
3139            p_required_feetype => p_required_feetype,
3140            p_default_feetype  => p_default_feetype,
3141            x_return_status    => x_return_status,
3142            x_msg_count        => x_msg_count,
3143            x_msg_data         => x_msg_data);
3144 
3145          If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
3146            RAISE create_fee_exception;
3147          End If;
3148       -- R12B Authoring OA Migration
3149 
3150       End If;
3151 
3152     End If;
3153 
3154   Exception
3155       when create_fee_exception then
3156          x_return_status := OKL_API.G_RET_STS_ERROR;
3157 
3158   END create_fee;
3159 
3160   -- R12 - START
3161 
3162   -- Following procedures introduced to meet R12 upfront tax accounting requirements
3163 
3164   -- Start of comments
3165   --
3166   -- Procedure Name  : populate_account_api_data
3167   -- Description     :  This is a private procedure used by create_upfront_tax_accounting
3168   -- to populate accounting data tables prior to calling central OKL a/c API
3169   -- Business Rules  :
3170   -- Parameters      :
3171   -- Version         : 1.0
3172   -- End of comments
3173 
3174   PROCEDURE populate_account_data(
3175                     p_api_version        IN  NUMBER
3176                     ,p_init_msg_list     IN  VARCHAR2 DEFAULT OKC_API.G_FALSE
3177                     ,p_trxh_out_rec      IN  Okl_Trx_Contracts_Pvt.tcnv_rec_type
3178                     ,p_tclv_tbl          IN  okl_trx_contracts_pvt.tclv_tbl_type
3179                     ,p_acc_gen_tbl       IN  OUT NOCOPY okl_account_dist_pvt.ACC_GEN_TBL_TYPE
3180                     ,p_tmpl_identify_tbl IN  OUT NOCOPY okl_account_dist_pvt.tmpl_identify_tbl_type
3181                     ,p_dist_info_tbl     IN  OUT NOCOPY okl_account_dist_pvt.dist_info_tbl_type
3182                     ,x_return_status     OUT NOCOPY VARCHAR2
3183                     ,x_msg_count         OUT NOCOPY NUMBER
3184                     ,x_msg_data          OUT NOCOPY VARCHAR2)
3185   IS
3186     -- Cursors plucked from OKL_LA_JE_PVT for a/c - START
3187   CURSOR fnd_pro_csr
3188   IS
3189   SELECT mo_global.get_current_org_id() l_fnd_profile
3190   FROM   dual;
3191 
3192   fnd_pro_rec fnd_pro_csr%ROWTYPE;
3193 
3194   CURSOR ra_cust_csr
3195   IS
3196   SELECT cust_trx_type_id l_cust_trx_type_id
3197   FROM   ra_cust_trx_types
3198   WHERE  name = 'Invoice-OKL';
3199 
3200   ra_cust_rec ra_cust_csr%ROWTYPE;
3201 
3202   CURSOR salesP_csr
3203   IS
3204   SELECT  ct.object1_id1           id
3205          ,chr.scs_code             scs_code
3206   FROM   okc_contacts              ct,
3207          okc_contact_sources       csrc,
3208          okc_k_party_roles_b       pty,
3209          okc_k_headers_b           chr
3210   WHERE  ct.cpl_id               = pty.id
3211   AND    ct.cro_code             = csrc.cro_code
3212   AND    ct.jtot_object1_code    = csrc.jtot_object_code
3213   AND    ct.dnz_chr_id           = chr.id
3214   AND    pty.rle_code            = csrc.rle_code
3215   AND    csrc.cro_code           = 'SALESPERSON'
3216   AND    csrc.rle_code           = 'LESSOR'
3217   AND    csrc.buy_or_sell        = chr.buy_or_sell
3218   AND    pty.dnz_chr_id          = chr.id
3219   AND    pty.chr_id              = chr.id
3220   AND    chr.id                  = p_trxh_out_rec.khr_id;
3221 
3222   l_salesP_rec salesP_csr%ROWTYPE;
3223 
3224   CURSOR custBillTo_csr
3225   IS
3226   SELECT bill_to_site_use_id cust_acct_site_id
3227   FROM   okc_k_headers_b
3228   WHERE  id = p_trxh_out_rec.khr_id;
3229 
3230   l_custBillTo_rec custBillTo_csr%ROWTYPE;
3231 
3232   -- Cursors plucked from OKL_LA_JE_PVT for a/c - END
3233   l_acc_gen_primary_key_tbl   okl_account_dist_pvt.acc_gen_primary_key;
3234   l_fact_synd_code            FND_LOOKUPS.Lookup_code%TYPE;
3235   l_inv_acct_code             OKC_RULES_B.Rule_Information1%TYPE;
3236 
3237   account_data_exception  EXCEPTION;
3238 
3239   --Bug# 6619311
3240   CURSOR assetBillTo_csr(p_cle_id IN NUMBER)
3241   IS
3242   SELECT bill_to_site_use_id cust_acct_site_id
3243   FROM   okc_k_lines_b
3244   WHERE  id = p_cle_id;
3245 
3246   l_assetBillTo_rec assetBillTo_csr%ROWTYPE;
3247   l_acc_gen_primary_key_tbl1 okl_account_dist_pvt.acc_gen_primary_key;
3248 
3249   BEGIN
3250 
3251     okl_debug_pub.logmessage('OKL: populate_account_data : START');
3252 
3253     x_return_status := Okl_Api.G_RET_STS_SUCCESS;
3254 
3255     l_acc_gen_primary_key_tbl(1).source_table := 'FINANCIALS_SYSTEM_PARAMETERS';
3256     OPEN  fnd_pro_csr;
3257     FETCH fnd_pro_csr INTO fnd_pro_rec;
3258     IF ( fnd_pro_csr%NOTFOUND )
3259     THEN
3260       l_acc_gen_primary_key_tbl(1).primary_key_column := '';
3261     ELSE
3262       l_acc_gen_primary_key_tbl(1).primary_key_column := fnd_pro_rec.l_fnd_profile;
3263     End IF;
3264     CLOSE fnd_pro_csr;
3265 
3266     l_acc_gen_primary_key_tbl(2).source_table := 'AR_SITE_USES_V';
3267     OPEN  custBillTo_csr;
3268     FETCH custBillTo_csr INTO l_custBillTo_rec;
3269     CLOSE custBillTo_csr;
3270     l_acc_gen_primary_key_tbl(2).primary_key_column := l_custBillTo_rec.cust_acct_site_id;
3271 
3272     l_acc_gen_primary_key_tbl(3).source_table := 'RA_CUST_TRX_TYPES';
3273     OPEN  ra_cust_csr;
3274     FETCH ra_cust_csr INTO ra_cust_rec;
3275     IF ( ra_cust_csr%NOTFOUND ) THEN
3276       l_acc_gen_primary_key_tbl(3).primary_key_column := '';
3277     ELSE
3278       l_acc_gen_primary_key_tbl(3).primary_key_column := TO_CHAR(ra_cust_rec.l_cust_trx_type_id);
3279     END IF;
3280     CLOSE ra_cust_csr;
3281 
3282     l_acc_gen_primary_key_tbl(4).source_table := 'JTF_RS_SALESREPS_MO_V';
3283     OPEN  salesP_csr;
3284     FETCH salesP_csr INTO l_salesP_rec;
3285     CLOSE salesP_csr;
3286     l_acc_gen_primary_key_tbl(4).primary_key_column := l_salesP_rec.id;
3287 
3288     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3289     THEN
3290       OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
3291                               ,G_MODULE
3292                               , 'OKL: populate_account_data Procedure: Calling OKL_SECURITIZATION_PVT ');
3293     END IF;
3294 
3295     OKL_SECURITIZATION_PVT.Check_Khr_ia_associated(
3296                                   p_api_version             => p_api_version,
3297                                   p_init_msg_list           => p_init_msg_list,
3298                                   x_return_status           => x_return_status,
3299                                   x_msg_count               => x_msg_count,
3300                                   x_msg_data                => x_msg_data,
3301                                   p_khr_id                  => p_trxh_out_rec.khr_id,
3302                                   p_scs_code                => l_salesP_rec.scs_code,
3303                                   p_trx_date                => p_trxh_out_rec.date_transaction_occurred,
3304 
3305                                   x_fact_synd_code          => l_fact_synd_code,
3306                                   x_inv_acct_code           => l_inv_acct_code
3307                                   );
3308 
3309 
3310     okl_debug_pub.logmessage('OKL: populate_account_data : OKL_SECURITIZATION_PVT : '||x_return_status);
3311 
3312     IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
3313     THEN
3314       RAISE account_data_exception;
3315     END IF;
3316 
3317     FOR i in p_tclv_tbl.FIRST..p_tclv_tbl.LAST
3318     LOOP
3319 
3320       --Bug# 6619311: Populate asset level bill-to site if defined
3321       l_acc_gen_primary_key_tbl1 := l_acc_gen_primary_key_tbl;
3322       IF p_tclv_tbl(i).kle_id IS NOT NULL THEN
3323         l_assetBillTo_rec := NULL;
3324         OPEN  assetBillTo_csr(p_cle_id => p_tclv_tbl(i).kle_id);
3325         FETCH assetBillTo_csr INTO l_assetBillTo_rec;
3326         CLOSE assetBillTo_csr;
3327 
3328         IF l_assetBillTo_rec.cust_acct_site_id IS NOT NULL THEN
3329           l_acc_gen_primary_key_tbl1(2).primary_key_column := l_assetBillTo_rec.cust_acct_site_id;
3330         END IF;
3331       END IF;
3332 
3333       -- Populate account source
3334       p_acc_gen_tbl(i).acc_gen_key_tbl            := l_acc_gen_primary_key_tbl1;
3335       p_acc_gen_tbl(i).source_id                  := p_tclv_tbl(i).id;
3336 
3337       -- Populate template info
3338       p_tmpl_identify_tbl(i).product_id          := p_trxh_out_rec.pdt_id;
3339       p_tmpl_identify_tbl(i).transaction_type_id := p_trxh_out_rec.try_id;
3340       p_tmpl_identify_tbl(i).stream_type_id      := p_tclv_tbl(i).sty_id;
3341       p_tmpl_identify_tbl(i).advance_arrears     := NULL;
3342       p_tmpl_identify_tbl(i).prior_year_yn       := 'N';
3343       p_tmpl_identify_tbl(i).memo_yn             := 'N';
3344       p_tmpl_identify_tbl(i).factoring_synd_flag := l_fact_synd_code;
3345       p_tmpl_identify_tbl(i).investor_code       := l_inv_acct_code;
3346 
3347       -- Populate distribution info
3348       p_dist_info_tbl(i).SOURCE_ID                := p_tclv_tbl(i).id;
3349       p_dist_info_tbl(i).amount                   := p_tclv_tbl(i).amount;
3350       p_dist_info_tbl(i).ACCOUNTING_DATE          := p_trxh_out_rec.date_transaction_occurred;
3351       p_dist_info_tbl(i).SOURCE_TABLE             := 'OKL_TXL_CNTRCT_LNS';
3352       p_dist_info_tbl(i).GL_REVERSAL_FLAG         := 'N';
3353       p_dist_info_tbl(i).POST_TO_GL               := 'Y';
3354       p_dist_info_tbl(i).CONTRACT_ID              := p_trxh_out_rec.khr_id;
3355       p_dist_info_tbl(i).currency_conversion_rate := p_trxh_out_rec.currency_conversion_rate;
3356       p_dist_info_tbl(i).currency_conversion_type := p_trxh_out_rec.currency_conversion_type;
3357       p_dist_info_tbl(i).currency_conversion_date := p_trxh_out_rec.currency_conversion_date;
3358       p_dist_info_tbl(i).currency_code            := p_trxh_out_rec.currency_code;
3359       okl_debug_pub.logmessage('OKL: populate_account_data : p_tclv_tbl loop : l_dist_info_tbl(i).amount : '||p_dist_info_tbl(i).amount);
3360 
3361     END LOOP;
3362 
3363     okl_debug_pub.logmessage('OKL: populate_account_data : END');
3364 
3365   EXCEPTION
3366     WHEN account_data_exception
3367     THEN
3368       x_return_status := OKL_API.G_RET_STS_ERROR;
3369 
3370   END populate_account_data;
3371 
3372 
3373   -- Start of comments
3374   --
3375   -- Procedure Name  : create_upfront_tax_accounting
3376   -- Description     :  This procedure creates a/c journal entries for upfront tax lines.
3377   -- This procedure logic will be executed in its entirety, only if SLA accounting
3378   -- option AMB is enabled.
3379   -- When enabled, it creates:
3380   --      1. TRX header in OKL_TRX_CONTRACTS for type 'Upfront Tax'
3381   --      2. TRX lines in OKL_TXL_CNTRCT_LNS for each line in ZX_LINES,
3382   --         store values for cle-id, tax_line_id, tax_amount, etc.
3383   --      3. Identify tax treatment for each asset line, to derive stream type
3384   --      4. Call a/c API for upfront tax records in OKL_TXL_CNTRCT_LNS
3385   -- Business Rules  :
3386   -- Parameters      :
3387   -- Version         : 1.0
3388   -- End of comments
3389 
3390   PROCEDURE create_upfront_tax_accounting(
3391                     p_api_version       IN  NUMBER
3392                     ,p_init_msg_list    IN  VARCHAR2 DEFAULT OKC_API.G_FALSE
3393                     ,p_contract_id      IN  okc_k_headers_all_b.id%TYPE
3394                     ,p_transaction_id   IN  okl_trx_contracts_all.khr_id%TYPE
3395                     ,p_transaction_type IN  VARCHAR2
3396                     ,x_return_status    OUT NOCOPY VARCHAR2
3397                     ,x_msg_count        OUT NOCOPY NUMBER
3398                     ,x_msg_data         OUT NOCOPY VARCHAR2)
3399   IS
3400 
3401   -- Cursor to check system level accounting option
3402   -- Upfront tax a/c is done if AMB is enabled
3403   CURSOR acct_opt_csr
3404   IS
3405   SELECT account_derivation
3406   FROM   okl_sys_acct_opts;
3407   l_acct_opt okl_sys_acct_opts.account_derivation%TYPE;
3408 
3409   --Bug# 6619311
3410   -- Cursor to fetch tax lines from EBTax table
3411   -- for assets having tax treatment same as the
3412   -- Default tax treatment
3413   CURSOR tax_line_csr1 (p_default_tax_treatment VARCHAR2)
3414   IS
3415   SELECT   NVL(rul.rule_information11, p_default_tax_treatment)
3416                                      tax_treatment
3417          , rgp.cle_id                asset_id
3418          , txs.id                    tax_header_id
3419          , txl.tax_line_id           tax_line_id
3420          , txl.tax_amt               tax_amount
3421   FROM     OKC_RULES_V       rul
3422          , OKC_RULE_GROUPS_V rgp
3423          , okl_tax_sources   txs
3424          , zx_lines          txl
3425   WHERE  rul.rgp_id                       = rgp.id
3426   AND    rgp.dnz_chr_id                   = p_contract_id
3427   AND    rgp.rgd_code                     = 'LAASTX'
3428   AND    rul.rule_information_category    = 'LAASTX'
3429   AND    (rul.rule_information11          = p_default_tax_treatment
3430           OR
3431           rul.rule_information11           IS NULL)
3432   AND    txs.khr_id                       = rgp.dnz_chr_id
3433   AND    txs.kle_id                       = rgp.cle_id
3434   AND    txs.kle_id                       IS NOT NULL                           -- change
3435   AND    txs.TAX_LINE_STATUS_CODE         = 'ACTIVE'
3436   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3437   AND    txs.trx_id                       = txl.trx_id
3438   AND    txs.trx_line_id                  = txl.trx_line_id
3439   AND    txl.entity_code                  = 'CONTRACTS'                         -- change
3440   AND    txl.event_class_code             = 'BOOKING'                           -- change
3441   AND    txs.entity_code                  = txl.entity_code                     -- change
3442   AND    txs.event_class_code             = txl.event_class_code                -- change
3443   AND    txl.application_id               = 540
3444   AND    txl.trx_level_type               = 'LINE'
3445   AND    txs.application_id               = txl.application_id
3446   AND    txs.trx_level_type               = txl.trx_level_type;
3447 
3448   --Bug# 6619311
3449   -- Cursor to fetch tax lines from EBTax table
3450   -- for assets having tax treatment as Capitalized or Financed
3451   -- when the default tax treatment is Billed
3452   CURSOR tax_line_csr2
3453   IS
3454   SELECT   rul.rule_information11    tax_treatment
3455          , rgp.cle_id                asset_id
3456          , txs.id                    tax_header_id
3457          , txl.tax_line_id           tax_line_id
3458          , txl.tax_amt               tax_amount
3459   FROM     OKC_RULES_V       rul
3460          , OKC_RULE_GROUPS_V rgp
3461          , okl_tax_sources   txs
3462          , zx_lines          txl
3463   WHERE  rul.rgp_id                       = rgp.id
3464   AND    rgp.dnz_chr_id                   = p_contract_id
3465   AND    rgp.rgd_code                     = 'LAASTX'
3466   AND    rul.rule_information_category    = 'LAASTX'
3467   AND    rul.rule_information11           <> 'BILLED'
3468   AND    txs.khr_id                       = rgp.dnz_chr_id
3469   AND    txs.kle_id                       = rgp.cle_id
3470   AND    txs.kle_id                       IS NOT NULL                           -- change
3471   AND    txs.TAX_LINE_STATUS_CODE         = 'ACTIVE'
3472   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3473   AND    txs.trx_id                       = txl.trx_id
3474   AND    txs.trx_line_id                  = txl.trx_line_id
3475   AND    txl.entity_code                  = 'CONTRACTS'                         -- change
3476   AND    txl.event_class_code             = 'BOOKING'                           -- change
3477   AND    txs.entity_code                  = txl.entity_code                     -- change
3478   AND    txs.event_class_code             = txl.event_class_code                -- change
3479   AND    txl.application_id               = 540
3480   AND    txl.trx_level_type               = 'LINE'
3481   AND    txs.application_id               = txl.application_id
3482   AND    txs.trx_level_type               = txl.trx_level_type;
3483 
3484   --Bug# 6619311
3485   -- Cursor to fetch tax lines from EBTax table
3486   -- for assets having tax treatment as Billed
3487   -- when the default tax treatment is Capitalized or Financed
3488   CURSOR tax_line_csr3
3489   IS
3490   SELECT   rul.rule_information11    tax_treatment
3491          , rgp.cle_id                asset_id
3492          , txs.id                    tax_header_id
3493          , txl.tax_line_id           tax_line_id
3494          , txl.tax_amt               tax_amount
3495   FROM     OKC_RULES_V       rul
3496          , OKC_RULE_GROUPS_V rgp
3497          , okl_tax_sources   txs
3498          , zx_lines          txl
3499   WHERE  rul.rgp_id                       = rgp.id
3500   AND    rgp.dnz_chr_id                   = p_contract_id
3501   AND    rgp.rgd_code                     = 'LAASTX'
3502   AND    rul.rule_information_category    = 'LAASTX'
3503   AND    rul.rule_information11           = 'BILLED'
3504   AND    txs.khr_id                       = rgp.dnz_chr_id
3505   AND    txs.kle_id                       = rgp.cle_id
3506   AND    txs.kle_id                       IS NOT NULL                           -- change
3507   AND    txs.TAX_LINE_STATUS_CODE         = 'ACTIVE'
3508   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3509   AND    txs.trx_id                       = txl.trx_id
3510   AND    txs.trx_line_id                  = txl.trx_line_id
3511   AND    txl.entity_code                  = 'CONTRACTS'                         -- change
3512   AND    txl.event_class_code             = 'BOOKING'                           -- change
3513   AND    txs.entity_code                  = txl.entity_code                     -- change
3514   AND    txs.event_class_code             = txl.event_class_code                -- change
3515   AND    txl.application_id               = 540
3516   AND    txl.trx_level_type               = 'LINE'
3517   AND    txs.application_id               = txl.application_id
3518   AND    txs.trx_level_type               = txl.trx_level_type;
3519 
3520   --Bug# 6619311
3521   -- Cursor to fetch tax lines from EBTax table
3522   -- for contract level taxes
3523   CURSOR tax_line_csr4
3524   IS
3525   SELECT   'BILLED'                  tax_treatment
3526          , txs.kle_id                asset_id
3527          , txs.id                    tax_header_id
3528          , txl.tax_line_id           tax_line_id
3529          , txl.tax_amt               tax_amount
3530   FROM     okl_tax_sources   txs
3531          , zx_lines          txl
3532   WHERE  txs.khr_id                       = p_contract_id
3533   AND    txs.kle_id                       IS NULL
3534   AND    txs.TAX_LINE_STATUS_CODE         = 'ACTIVE'
3535   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3536   AND    txs.trx_id                       = txl.trx_id
3537   AND    txs.trx_line_id                  = txl.trx_line_id
3538   AND    txl.entity_code                  = 'CONTRACTS'
3539   AND    txl.event_class_code             = 'BOOKING'
3540   AND    txs.entity_code                  = txl.entity_code
3541   AND    txs.event_class_code             = txl.event_class_code
3542   AND    txl.application_id               = 540
3543   AND    txl.trx_level_type               = 'LINE'
3544   AND    txs.application_id               = txl.application_id
3545   AND    txs.trx_level_type               = txl.trx_level_type;
3546 
3547   -- Cursor to check if previous upfront tax TRX exists,
3548   -- This will be used by rebook logic to decide if there
3549   -- is a need for reversal accounting. If the cursor does not
3550   -- return any rows, skip the reversal accounting part
3551   CURSOR check_upfront_trx_csr
3552   IS
3553   SELECT 1
3554   FROM   dual
3555   WHERE  EXISTS (SELECT a.id
3556                  FROM   okl_trx_contracts_all a
3557                         ,okl_trx_types_v b
3558                  WHERE  a.khr_id = p_contract_id
3559                  AND    a.try_id = b.id
3560              --rkuttiya added for 12.1.1 Multi GAAP
3561                  AND    a.representation_type = 'PRIMARY'
3562              --
3563                  AND    b.name   = 'Upfront Tax');
3564 
3565   --Bug# 6619311
3566   -- Cursor to get reversed tax lines for rebook TRX
3567   -- for assets having tax treatment same as the
3568   -- Default tax treatment
3569   CURSOR rev_taxline_csr1(   p_rbk_trx_id            NUMBER
3570                             ,p_default_tax_treatment VARCHAR2)
3571   IS
3572   SELECT  NVL(rul.rule_information11,p_default_tax_treatment)
3573                                      tax_treatment
3574          , rgp.cle_id                asset_id
3575          , txs.id                    tax_header_id
3576          , txl.tax_line_id           tax_line_id
3577          , txl.tax_amt               tax_amount
3578   FROM     OKC_RULES_V rul
3579          , OKC_RULE_GROUPS_V rgp
3580          , okl_tax_sources txs
3581          , zx_lines txl
3582   WHERE  rul.rgp_id                       = rgp.id
3583   AND    rgp.dnz_chr_id                   = p_contract_id
3584   AND    rgp.rgd_code                     = 'LAASTX'
3585   AND    rul.rule_information_category    = 'LAASTX'
3586   AND    (rul.rule_information11          = p_default_tax_treatment
3587           OR
3588           rul.rule_information11           IS NULL)
3589   AND    txs.khr_id                       = rgp.dnz_chr_id
3590   AND    txs.kle_id                       = rgp.cle_id
3591   AND    txs.kle_id                       IS NOT NULL
3592   AND    txs.TAX_LINE_STATUS_CODE         = 'INACTIVE'
3593   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3594   AND    txs.trx_id                       = txl.trx_id
3595   AND    txs.trx_line_id                  = txl.trx_line_id
3596   AND    txl.entity_code                  IN ('CONTRACTS','ASSETS')
3597   AND    txl.event_class_code             IN ('BOOKING','ASSET_RELOCATION')
3598   AND    txs.entity_code                  = txl.entity_code
3599   AND    txs.event_class_code             = txl.event_class_code
3600   AND    txl.application_id               = 540
3601   AND    txl.trx_level_type               = 'LINE'
3602   AND    txs.application_id               = txl.application_id
3603   AND    txs.trx_level_type               = txl.trx_level_type
3604   AND    (txs.trx_id,txs.trx_line_id)
3605           IN
3606          (SELECT adjusted_doc_trx_id
3607                  ,adjusted_doc_trx_line_id
3608           FROM   okl_tax_sources
3609           WHERE  trx_id                   = p_rbk_trx_id
3610           AND    tax_line_status_code     = 'INACTIVE'
3611           AND    adjusted_doc_trx_id      IS NOT NULL
3612           AND    adjusted_doc_trx_line_id IS NOT NULL);
3613 
3614   --Bug# 6619311
3615   -- Cursor to get reversed tax lines for rebook TRX
3616   -- for assets having tax treatment as Capitalized or Financed
3617   -- when the default tax treatment is Billed
3618   CURSOR rev_taxline_csr2(   p_rbk_trx_id NUMBER)
3619   IS
3620   SELECT   rul.rule_information11    tax_treatment
3621          , rgp.cle_id                asset_id
3622          , txs.id                    tax_header_id
3623          , txl.tax_line_id           tax_line_id
3624          , txl.tax_amt               tax_amount
3625   FROM     OKC_RULES_V rul
3626          , OKC_RULE_GROUPS_V rgp
3627          , okl_tax_sources txs
3628          , zx_lines txl
3629   WHERE  rul.rgp_id                       = rgp.id
3630   AND    rgp.dnz_chr_id                   = p_contract_id
3631   AND    rgp.rgd_code                     = 'LAASTX'
3632   AND    rul.rule_information_category    = 'LAASTX'
3633   AND    rul.rule_information11           <> 'BILLED'
3634   AND    txs.khr_id                       = rgp.dnz_chr_id
3635   AND    txs.kle_id                       = rgp.cle_id
3636   AND    txs.kle_id                       IS NOT NULL
3637   AND    txs.TAX_LINE_STATUS_CODE         = 'INACTIVE'
3638   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3639   AND    txs.trx_id                       = txl.trx_id
3640   AND    txs.trx_line_id                  = txl.trx_line_id
3641   AND    txl.entity_code                  IN ('CONTRACTS','ASSETS')
3642   AND    txl.event_class_code             IN ('BOOKING','ASSET_RELOCATION')
3643   AND    txs.entity_code                  = txl.entity_code
3644   AND    txs.event_class_code             = txl.event_class_code
3645   AND    txl.application_id               = 540
3646   AND    txl.trx_level_type               = 'LINE'
3647   AND    txs.application_id               = txl.application_id
3648   AND    txs.trx_level_type               = txl.trx_level_type
3649   AND    (txs.trx_id,txs.trx_line_id)
3650           IN
3651          (SELECT adjusted_doc_trx_id
3652                  ,adjusted_doc_trx_line_id
3653           FROM   okl_tax_sources
3654           WHERE  trx_id                   = p_rbk_trx_id
3655           AND    tax_line_status_code     = 'INACTIVE'
3656           AND    adjusted_doc_trx_id      IS NOT NULL
3657           AND    adjusted_doc_trx_line_id IS NOT NULL);
3658 
3659   --Bug# 6619311
3660   -- Cursor to get reversed tax lines for rebook TRX
3661   -- for assets having tax treatment as Billed
3662   -- when the default tax treatment is Capitalized or Financed
3663   CURSOR rev_taxline_csr3(   p_rbk_trx_id NUMBER)
3664   IS
3665   SELECT   rul.rule_information11    tax_treatment
3666          , rgp.cle_id                asset_id
3667          , txs.id                    tax_header_id
3668          , txl.tax_line_id           tax_line_id
3669          , txl.tax_amt               tax_amount
3670   FROM     OKC_RULES_V rul
3671          , OKC_RULE_GROUPS_V rgp
3672          , okl_tax_sources txs
3673          , zx_lines txl
3674   WHERE  rul.rgp_id                       = rgp.id
3675   AND    rgp.dnz_chr_id                   = p_contract_id
3676   AND    rgp.rgd_code                     = 'LAASTX'
3677   AND    rul.rule_information_category    = 'LAASTX'
3678   AND    rul.rule_information11           = 'BILLED'
3679   AND    txs.khr_id                       = rgp.dnz_chr_id
3680   AND    txs.kle_id                       = rgp.cle_id
3681   AND    txs.kle_id                       IS NOT NULL
3682   AND    txs.TAX_LINE_STATUS_CODE         = 'INACTIVE'
3683   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3684   AND    txs.trx_id                       = txl.trx_id
3685   AND    txs.trx_line_id                  = txl.trx_line_id
3686   AND    txl.entity_code                  IN ('CONTRACTS','ASSETS')
3687   AND    txl.event_class_code             IN ('BOOKING','ASSET_RELOCATION')
3688   AND    txs.entity_code                  = txl.entity_code
3689   AND    txs.event_class_code             = txl.event_class_code
3690   AND    txl.application_id               = 540
3691   AND    txl.trx_level_type               = 'LINE'
3692   AND    txs.application_id               = txl.application_id
3693   AND    txs.trx_level_type               = txl.trx_level_type
3694   AND    (txs.trx_id,txs.trx_line_id)
3695           IN
3696          (SELECT adjusted_doc_trx_id
3697                  ,adjusted_doc_trx_line_id
3698           FROM   okl_tax_sources
3699           WHERE  trx_id                   = p_rbk_trx_id
3700           AND    tax_line_status_code     = 'INACTIVE'
3701           AND    adjusted_doc_trx_id      IS NOT NULL
3702           AND    adjusted_doc_trx_line_id IS NOT NULL);
3703 
3704   --Bug# 6619311
3705   -- Cursor to get reversed tax lines for rebook TRX
3706   -- for contract level taxes
3707   CURSOR rev_taxline_csr4(   p_rbk_trx_id NUMBER)
3708   IS
3709   SELECT   'BILLED'                  tax_treatment
3710          , txs.kle_id                asset_id
3711          , txs.id                    tax_header_id
3712          , txl.tax_line_id           tax_line_id
3713          , txl.tax_amt               tax_amount
3714   FROM     okl_tax_sources txs
3715          , zx_lines txl
3716   WHERE  txs.khr_id                       = p_contract_id
3717   AND    txs.kle_id                       IS NULL
3718   AND    txs.TAX_LINE_STATUS_CODE         = 'INACTIVE'
3719   AND    txs.TAX_CALL_TYPE_CODE           = 'UPFRONT_TAX'
3720   AND    txs.trx_id                       = txl.trx_id
3721   AND    txs.trx_line_id                  = txl.trx_line_id
3722   AND    txl.entity_code                  = 'CONTRACTS'
3723   AND    txl.event_class_code             = 'BOOKING'
3724   AND    txs.entity_code                  = txl.entity_code
3725   AND    txs.event_class_code             = txl.event_class_code
3726   AND    txl.application_id               = 540
3727   AND    txl.trx_level_type               = 'LINE'
3728   AND    txs.application_id               = txl.application_id
3729   AND    txs.trx_level_type               = txl.trx_level_type
3730   AND    (txs.trx_id,txs.trx_line_id)
3731           IN
3732          (SELECT adjusted_doc_trx_id
3733                  ,adjusted_doc_trx_line_id
3734           FROM   okl_tax_sources
3735           WHERE  trx_id                   = p_rbk_trx_id
3736           AND    tax_line_status_code     = 'INACTIVE'
3737           AND    adjusted_doc_trx_id      IS NOT NULL
3738           AND    adjusted_doc_trx_line_id IS NOT NULL);
3739 
3740   l_tclv_tbl                  okl_trx_contracts_pvt.tclv_tbl_type;
3741   x_tclv_tbl                  okl_trx_contracts_pvt.tclv_tbl_type;
3742 
3743 
3744   l_tmpl_identify_rec         okl_account_dist_pvt.tmpl_identify_rec_type;
3745   l_tmpl_identify_tbl         okl_account_dist_pvt.tmpl_identify_tbl_type;
3746   l_template_tbl              okl_account_dist_pvt.avlv_tbl_type;
3747   l_dist_info_tbl             okl_account_dist_pvt.dist_info_tbl_type;
3748   l_template_out_tbl          okl_account_dist_pvt.avlv_out_tbl_type;
3749   l_amount_tbl                okl_account_dist_pvt.amount_out_tbl_type;
3750   l_ctxt_val_tbl              okl_account_dist_pvt.CTXT_VAL_TBL_TYPE;
3751   l_acc_gen_tbl               okl_account_dist_pvt.ACC_GEN_TBL_TYPE;
3752   l_ctxt_tbl                  okl_account_dist_pvt.CTXT_TBL_TYPE;
3753 
3754   counter                     NUMBER := 0;
3755   j                           NUMBER := 0;
3756   l_trx_id                    NUMBER;
3757   l_fnd_rec                   fnd_lookups_csr%ROWTYPE;
3758   l_trxh_out_rec              Okl_Trx_Contracts_Pvt.tcnv_rec_type;
3759   l_financed_sty_id           NUMBER;
3760   l_capitalized_sty_id        NUMBER;
3761   l_upfront_trx_exists        NUMBER := 0;
3762   l_rule_info_csr             rule_info_csr%ROWTYPE;
3763   subtype ac_tax_line_rec is  tax_line_csr2%ROWTYPE;
3764   TYPE ac_tax_line_tbl        IS TABLE OF ac_tax_line_rec
3765                               INDEX BY BINARY_INTEGER;
3766   l_accoutable_tax_lines       ac_tax_line_tbl;
3767   l_fact_synd_code      FND_LOOKUPS.Lookup_code%TYPE;
3768   l_inv_acct_code       OKC_RULES_B.Rule_Information1%TYPE;
3769   upfront_tax_acct_exception  EXCEPTION;
3770 
3771   --Bug# 6619311
3772   l_billed_sty_id             NUMBER;
3773   l_transaction_amount        NUMBER;
3774 
3775   BEGIN
3776 
3777     okl_debug_pub.logmessage('OKL: UPF A/C : START');
3778 
3779     x_return_status := Okl_Api.G_RET_STS_SUCCESS;
3780 
3781     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3782     THEN
3783       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
3784                               , G_MODULE
3785                               , 'OKL: Booking Procedure: deriving Accounting option ');
3786     END IF;
3787 
3788     OPEN acct_opt_csr;
3789     FETCH acct_opt_csr INTO l_acct_opt;
3790 
3791     IF acct_opt_csr%NOTFOUND
3792     THEN
3793       OKL_API.set_message( p_app_name     => G_APP_NAME,
3794                            p_msg_name     => 'OKL_LA_ST_ACCT_ERROR');
3795       CLOSE acct_opt_csr;
3796       RAISE upfront_tax_acct_exception;
3797     END IF;
3798 
3799     CLOSE acct_opt_csr;
3800 
3801     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3802     THEN
3803       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
3804                               , G_MODULE
3805                               , 'OKL: Booking Procedure: Validating Accounting option ');
3806     END IF;
3807 
3808     IF (l_acct_opt IS NULL)
3809     THEN
3810       OKL_API.set_message( p_app_name     => G_APP_NAME,
3811                            p_msg_name     => 'OKL_LA_ST_ACCT_ERROR');
3812       RAISE upfront_tax_acct_exception;
3813     END IF;
3814 
3815     OPEN  rule_info_csr(p_contract_id);
3816     FETCH rule_info_csr INTO l_rule_info_csr;
3817     CLOSE rule_info_csr;
3818 
3819     -- execute the whole logic only if AMB is enabled, otherwise get out
3820     IF (l_acct_opt <> 'AMB' )
3821     THEN
3822       NULL;
3823     ELSE
3824 
3825       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3826       THEN
3827         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
3828                               , G_MODULE
3829                               ,'OKL: create_upfront_tax_accounting Procedure : before fnd_lookups_csr ');
3830       END IF;
3831 
3832       OPEN  fnd_lookups_csr('OKL_TCN_TYPE', 'Upfront Tax');
3833       FETCH fnd_lookups_csr INTO l_fnd_rec;
3834 
3835       IF fnd_lookups_csr%NOTFOUND
3836       THEN
3837           Okl_Api.SET_MESSAGE( G_APP_NAME
3838                               ,G_INVALID_VALUE
3839                               ,'TRANSACTION_TYPE'
3840                               ,'Upfront Tax');
3841           CLOSE fnd_lookups_csr;
3842           RAISE upfront_tax_acct_exception;
3843       END IF;
3844 
3845       CLOSE fnd_lookups_csr;
3846 
3847       -- Get Stream ID's for 'Financed' and 'Capitalized' Fee stream purposes,
3848       -- this ID will be populated in OKL_TXL_CNTRCT_LNS depending on
3849       -- each inserted record's tax treatment
3850 
3851       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3852       THEN
3853         OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
3854                                 ,G_MODULE
3855                                 ,'OKL: create_upfront_tax_accounting Procedure: deriving financed stream ID ');
3856       END IF;
3857 
3858       OKL_STREAMS_UTIL.get_primary_stream_type(
3859     			p_khr_id              => p_contract_id,
3860     			p_primary_sty_purpose => 'UPFRONT_TAX_FINANCED',
3861     			x_return_status       => x_return_status,
3862     			x_primary_sty_id      => l_financed_sty_id);
3863 
3864       okl_debug_pub.logmessage('OKL: UPF A/C : UPFRONT_TAX_FINANCED : '||l_financed_sty_id);
3865 
3866       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
3867       THEN
3868         RAISE upfront_tax_acct_exception;
3869       End If;
3870 
3871       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3872       THEN
3873         OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
3874                               ,G_MODULE
3875                               ,'OKL: create_upfront_tax_accounting Procedure: deriving capitalized stream ID ');
3876       END IF;
3877 
3878       OKL_STREAMS_UTIL.get_primary_stream_type(
3879     			p_khr_id              => p_contract_id,
3880     			p_primary_sty_purpose => 'UPFRONT_TAX_CAPITALIZED',
3881     			x_return_status       => x_return_status,
3882     			x_primary_sty_id      => l_capitalized_sty_id);
3883 
3884       okl_debug_pub.logmessage('OKL: UPF A/C : UPFRONT_TAX_CAPITALIZED : '||l_capitalized_sty_id);
3885 
3886       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
3887         RAISE upfront_tax_acct_exception;
3888       End If;
3889 
3890       --Bug# 6619311
3891       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3892       THEN
3893         OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
3894                               ,G_MODULE
3895                               ,'OKL: create_upfront_tax_accounting Procedure: deriving billed stream ID ');
3896       END IF;
3897 
3898       OKL_STREAMS_UTIL.get_primary_stream_type(
3899     			p_khr_id              => p_contract_id,
3900     			p_primary_sty_purpose => 'UPFRONT_TAX_BILLED',
3901     			x_return_status       => x_return_status,
3902     			x_primary_sty_id      => l_billed_sty_id);
3903 
3904       okl_debug_pub.logmessage('OKL: UPF A/C : UPFRONT_TAX_BILLED : '||l_billed_sty_id);
3905 
3906       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
3907         RAISE upfront_tax_acct_exception;
3908       End If;
3909       --End Bug# 6619311
3910 
3911       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3912       THEN
3913         OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
3914                               ,G_MODULE
3915                               ,'OKL: create_upfront_tax_accounting Procedure: deriving default tax treatment ');
3916       END IF;
3917 
3918       -- For Rebook transactions a reversal TRX needs to be created
3919       -- prior to an activation TRX. Hence this condition to perform
3920       -- reversal a/c
3921       IF (p_transaction_type = 'Rebook')
3922       THEN
3923 
3924         l_upfront_trx_exists := 0;
3925 
3926         OPEN  check_upfront_trx_csr;
3927         FETCH check_upfront_trx_csr INTO l_upfront_trx_exists;
3928         CLOSE check_upfront_trx_csr;
3929 
3930         -- Perform Rebook reversal a/c only if previous 'Upfront Tax' exists.
3931         IF (l_upfront_trx_exists = 1)
3932         THEN
3933 
3934           IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
3935           THEN
3936             OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
3937                                   ,G_MODULE
3938                                   ,'OKL: create_upfront_tax_accounting Procedure: deriving asset tax information ');
3939           END IF;
3940 
3941           --Bug# 6619311
3942           l_accoutable_tax_lines.DELETE;
3943           j := 0;
3944           l_trx_id := NULL;
3945           l_trxh_out_rec := NULL;
3946           l_tclv_tbl.DELETE;
3947           l_acc_gen_tbl.DELETE;
3948           l_tmpl_identify_tbl.DELETE;
3949           l_dist_info_tbl.DELETE;
3950           l_ctxt_tbl.DELETE;
3951 
3952           -- Derive accountable tax lines, based on default tax treatment,
3953           -- into temporary table prior to processing
3954           IF (l_rule_info_csr.rule_information1 <> 'BILLED')
3955           THEN
3956             --Bug# 6619311: Fetch tax lines for assets having tax treatment as
3957             --              Capitalized or Financed
3958             FOR i IN rev_taxline_csr1(  p_transaction_id
3959                                       , l_rule_info_csr.rule_information1)
3960             LOOP
3961               j                         := j+1;
3962               l_accoutable_tax_lines(j) := i;
3963             END LOOP;
3964 
3965             --Bug# 6619311: Fetch tax lines for assets having tax treatment as
3966             --              Billed
3967             FOR i IN rev_taxline_csr3(  p_transaction_id )
3968             LOOP
3969               j                         := j+1;
3970               l_accoutable_tax_lines(j) := i;
3971             END LOOP;
3972 
3973           ELSE
3974             --Bug# 6619311: Fetch tax lines for assets having tax treatment as
3975             --              Billed
3976             FOR i IN rev_taxline_csr1(  p_transaction_id
3977                                       , l_rule_info_csr.rule_information1)
3978             LOOP
3979               j                         := j+1;
3980               l_accoutable_tax_lines(j) := i;
3981             END LOOP;
3982 
3983             --Bug# 6619311: Fetch tax lines for assets having tax treatment as
3984             --              Capitalized or Financed
3985             FOR i IN rev_taxline_csr2(  p_transaction_id )
3986             LOOP
3987               j                         := j+1;
3988               l_accoutable_tax_lines(j) := i;
3989             END LOOP;
3990           END IF;
3991 
3992           --Bug# 6619311: Fetch tax lines for contract level taxes
3993           FOR i IN rev_taxline_csr4(  p_transaction_id )
3994           LOOP
3995             j                         := j+1;
3996             l_accoutable_tax_lines(j) := i;
3997           END LOOP;
3998 
3999 
4000           --Bug# 7506009
4001           -- Create Upfront Tax accounting only if Tax lines exist
4002           IF (l_accoutable_tax_lines.COUNT > 0) THEN
4003 
4004           --Bug# 6619311
4005           l_transaction_amount := 0;
4006           FOR i IN l_accoutable_tax_lines.FIRST..l_accoutable_tax_lines.LAST
4007           LOOP
4008             l_transaction_amount := l_transaction_amount + (-1 * l_accoutable_tax_lines(i).tax_amount);
4009           END LOOP;
4010 
4011           IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4012             OKL_DEBUG_PUB.LOG_DEBUG(  FND_LOG.LEVEL_STATEMENT
4013                                    , G_MODULE
4014                                   ,'OKL: create_upfront_tax_accounting Procedure: Calling populate_transaction procedure ');
4015           END IF;
4016 
4017           -- Create Upfront Tax Header TRX record in OKL_TRX_CONTRACTS table
4018           populate_transaction(
4019                       p_api_version      => p_api_version,
4020                       p_init_msg_list    => p_init_msg_list,
4021                       p_contract_id      => p_contract_id,
4022                       p_transaction_type => 'Upfront Tax',
4023                       p_transaction_id   => NULL,
4024                       p_source_trx_id    => p_transaction_id,
4025                       p_source_trx_type  => 'TCN',
4026                       --Bug# 6619311
4027                       p_transaction_amount => l_transaction_amount,
4028                       x_transaction_id   => l_trx_id,
4029                       x_trxh_out_rec     => l_trxh_out_rec,
4030                       x_return_status    => x_return_status,
4031                       x_msg_count        => x_msg_count,
4032                       x_msg_data         => x_msg_data);
4033 
4034           okl_debug_pub.logmessage('OKL: UPF A/C : populate_transaction : return status '||x_return_status);
4035           -- check transaction creation was successful
4036           IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4037           THEN
4038             RAISE upfront_tax_acct_exception;
4039           End If;
4040 
4041           -- Get Asset tax treatment (Financed or Capitalized),
4042           -- Tax line Amount for each asset, etc., for processing reversal
4043           FOR i IN l_accoutable_tax_lines.FIRST..l_accoutable_tax_lines.LAST
4044           LOOP
4045 
4046             -- Populate TRX line array
4047             l_tclv_tbl(i).line_number   := i;
4048             l_tclv_tbl(i).tcn_id        := l_trx_id;
4049             l_tclv_tbl(i).khr_id        := p_contract_id;
4050             l_tclv_tbl(i).kle_id        := l_accoutable_tax_lines(i).asset_id;
4051             l_tclv_tbl(i).tcl_type      := l_fnd_rec.lookup_code;
4052             l_tclv_tbl(i).tax_line_id   := l_accoutable_tax_lines(i).tax_line_id;
4053             l_tclv_tbl(i).amount        := -1 * l_accoutable_tax_lines(i).tax_amount; -- (-ve) amount
4054             l_tclv_tbl(i).currency_code :=l_trxh_out_rec.currency_code;
4055 
4056             IF (l_accoutable_tax_lines(i).tax_treatment = 'FINANCE')
4057             THEN
4058               l_tclv_tbl(i).sty_id    := l_financed_sty_id;
4059             END IF;
4060 
4061             IF (l_accoutable_tax_lines(i).tax_treatment = 'CAPITALIZE')
4062             THEN
4063               l_tclv_tbl(i).sty_id    := l_capitalized_sty_id;
4064             END IF;
4065 
4066             --Bug# 6619311
4067             IF (l_accoutable_tax_lines(i).tax_treatment = 'BILLED')
4068             THEN
4069               l_tclv_tbl(i).sty_id    := l_billed_sty_id;
4070             END IF;
4071             --End Bug# 6619311
4072 
4073           END LOOP;
4074 
4075           -- Create TRX lines with the data gathered
4076 
4077           IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4078           THEN
4079             OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
4080                                     ,G_MODULE
4081                                     , 'OKL: create_upfront_tax_accounting Procedure: Calling Okl_Trx_Contracts_Pub.create_trx_cntrct_lines ');
4082           END IF;
4083 
4084           Okl_Trx_Contracts_Pub.create_trx_cntrct_lines(
4085                                       p_api_version   => p_api_version,
4086                                       p_init_msg_list => p_init_msg_list,
4087                                       x_return_status => x_return_status,
4088                                       x_msg_count     => x_msg_count,
4089                                       x_msg_data      => x_msg_data,
4090                                       p_tclv_tbl      => l_tclv_tbl,
4091                                       x_tclv_tbl      => x_tclv_tbl);
4092 
4093           okl_debug_pub.logmessage('OKL: UPF A/C : create_trx_cntrct_lines : '||x_return_status);
4094 
4095           -- check transaction line creation was successful
4096           IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4097           THEN
4098             RAISE upfront_tax_acct_exception;
4099           END IF;
4100 
4101           -- Populate accounting API data structures
4102           populate_account_data(
4103                     p_api_version
4104                     ,p_init_msg_list
4105                     ,l_trxh_out_rec
4106                     ,x_tclv_tbl
4107                     ,l_acc_gen_tbl
4108                     ,l_tmpl_identify_tbl
4109                     ,l_dist_info_tbl
4110                     ,x_return_status
4111                     ,x_msg_count
4112                     ,x_msg_data);
4113 
4114           okl_debug_pub.logmessage('OKL: UPF A/C : populate_account_data : '||x_return_status);
4115 
4116           -- check transaction line creation was successful
4117           IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4118           THEN
4119             RAISE upfront_tax_acct_exception;
4120           END IF;
4121 
4122           IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4123           THEN
4124             OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
4125                                     ,G_MODULE
4126                                     ,'OKL: create_upfront_tax_accounting Procedure: Calling Okl_Account_Dist_Pub.CREATE_ACCOUNTING_DIST');
4127           END IF;
4128 
4129           okl_account_dist_pvt.create_accounting_dist(
4130                                   p_api_version             => 1.0,
4131                                   p_init_msg_list           => p_init_msg_list,
4132                                   x_return_status           => x_return_status,
4133                                   x_msg_count               => x_msg_count,
4134                                   x_msg_data                => x_msg_data,
4135                                   p_tmpl_identify_tbl       => l_tmpl_identify_tbl,
4136                                   p_dist_info_tbl           => l_dist_info_tbl,
4137                                   p_ctxt_val_tbl            => l_ctxt_tbl,
4138                                   p_acc_gen_primary_key_tbl => l_acc_gen_tbl,
4139                                   x_template_tbl            => l_template_out_tbl,
4140                                   x_amount_tbl              => l_amount_tbl,
4141                                   p_trx_header_id           => l_trxh_out_rec.id);
4142 
4143           IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4144           THEN
4145             RAISE upfront_tax_acct_exception;
4146           END IF;
4147 
4148           OKL_MULTIGAAP_ENGINE_PVT.CREATE_SEC_REP_TRX
4149                            (p_api_version => p_api_version
4150                            ,p_init_msg_list => p_init_msg_list
4151                            ,x_return_status => x_return_status
4152                            ,x_msg_count => x_msg_count
4153                            ,x_msg_data => x_msg_data
4154                            ,P_TCNV_REC => l_trxh_out_rec
4155                            ,P_TCLV_TBL => x_tclv_tbl
4156                            ,p_ctxt_val_tbl => l_ctxt_tbl
4157                            ,p_acc_gen_primary_key_tbl => l_acc_gen_tbl(1).acc_gen_key_tbl);
4158 
4159           IF (x_return_status = Okl_Api.G_RET_STS_UNEXP_ERROR) THEN
4160             RAISE Okl_Api.G_EXCEPTION_UNEXPECTED_ERROR;
4161           ELSIF (x_return_status = Okl_Api.G_RET_STS_ERROR) THEN
4162             RAISE Okl_Api.G_EXCEPTION_ERROR;
4163           END IF;
4164 
4165           END IF;
4166           --Bug# 7506009
4167 
4168         END IF; -- upfront tax exists
4169       END IF; -- Rebook condition
4170 
4171 
4172       --Bug# 6619311
4173       l_accoutable_tax_lines.DELETE;
4174       j := 0;
4175       l_trx_id := NULL;
4176       l_trxh_out_rec := NULL;
4177       l_tclv_tbl.DELETE;
4178       l_acc_gen_tbl.DELETE;
4179       l_tmpl_identify_tbl.DELETE;
4180       l_dist_info_tbl.DELETE;
4181       l_ctxt_tbl.DELETE;
4182 
4183       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4184       THEN
4185         OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
4186                                 ,G_MODULE
4187                                 ,'OKL: create_upfront_tax_accounting Procedure: deriving asset tax information ');
4188       END IF;
4189 
4190       -- Derive accountable tax lines, based on default tax treatment,
4191       -- into temporary table prior to processing
4192       IF (l_rule_info_csr.rule_information1 <> 'BILLED')
4193       THEN
4194 
4195         --Bug# 6619311: Fetch tax lines for assets having tax treatment as
4196         --              Capitalized or Financed
4197         FOR i IN tax_line_csr1(p_default_tax_treatment => l_rule_info_csr.rule_information1)
4198         LOOP
4199           j                         := j+1;
4200           l_accoutable_tax_lines(j) := i;
4201         END LOOP;
4202 
4203         --Bug# 6619311: Fetch tax lines for assets having tax treatment as
4204         --              Billed
4205         FOR i IN tax_line_csr3
4206         LOOP
4207           j                         := j+1;
4208           l_accoutable_tax_lines(j) := i;
4209         END LOOP;
4210 
4211       ELSE
4212 
4213         --Bug# 6619311: Fetch tax lines for assets having tax treatment as
4214         --              Billed
4215         FOR i IN tax_line_csr1(p_default_tax_treatment => l_rule_info_csr.rule_information1)
4216         LOOP
4217           j                         := j+1;
4218           l_accoutable_tax_lines(j) := i;
4219         END LOOP;
4220 
4221         --Bug# 6619311: Fetch tax lines for assets having tax treatment as
4222         --              Capitalized or Financed
4223         FOR i IN tax_line_csr2
4224         LOOP
4225           j                         := j+1;
4226           l_accoutable_tax_lines(j) := i;
4227         END LOOP;
4228 
4229       END IF;
4230 
4231       --Bug# 6619311: Fetch tax lines for contract level taxes
4232       FOR i IN tax_line_csr4
4233       LOOP
4234         j                         := j+1;
4235         l_accoutable_tax_lines(j) := i;
4236       END LOOP;
4237 
4238 
4239       --Bug# 7506009
4240       -- Create Upfront Tax accounting only if Tax lines exist
4241       IF (l_accoutable_tax_lines.COUNT > 0) THEN
4242 
4243       --Bug# 6619311
4244       l_transaction_amount := 0;
4245       FOR i IN l_accoutable_tax_lines.FIRST..l_accoutable_tax_lines.LAST
4246       LOOP
4247         l_transaction_amount := l_transaction_amount + l_accoutable_tax_lines(i).tax_amount;
4248       END LOOP;
4249 
4250       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4251       THEN
4252         OKL_DEBUG_PUB.LOG_DEBUG(  FND_LOG.LEVEL_STATEMENT
4253                               , G_MODULE
4254                               ,'OKL: create_upfront_tax_accounting Procedure: Calling populate_transaction procedure ');
4255       END IF;
4256 
4257       -- Create Upfront Tax Header TRX record in OKL_TRX_CONTRACTS table
4258       populate_transaction(
4259                       p_api_version      => p_api_version,
4260                       p_init_msg_list    => p_init_msg_list,
4261                       p_contract_id      => p_contract_id,
4262                       p_transaction_type => 'Upfront Tax',
4263                       p_transaction_id   => NULL,
4264                       p_source_trx_id    => p_transaction_id,
4265                       p_source_trx_type  => 'TCN',
4266                       --Bug# 6619311
4267                       p_transaction_amount => l_transaction_amount,
4268                       x_transaction_id   => l_trx_id,
4269                       x_trxh_out_rec     => l_trxh_out_rec,
4270                       x_return_status    => x_return_status,
4271                       x_msg_count        => x_msg_count,
4272                       x_msg_data         => x_msg_data);
4273 
4274       okl_debug_pub.logmessage('OKL: UPF A/C : populate_transaction : return status '||x_return_status);
4275 
4276       -- check transaction creation was successful
4277       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4278       THEN
4279         RAISE upfront_tax_acct_exception;
4280       END IF;
4281 
4282       -- Get Asset tax treatment (Financed or Capitalized), Tax line Amount for each asset, etc., for processing
4283       FOR i IN l_accoutable_tax_lines.FIRST..l_accoutable_tax_lines.LAST
4284       LOOP
4285 
4286         -- Populate TRX line array
4287         l_tclv_tbl(i).line_number   := i;
4288         l_tclv_tbl(i).tcn_id        := l_trx_id;
4289         l_tclv_tbl(i).khr_id        := p_contract_id;
4290         l_tclv_tbl(i).kle_id        := l_accoutable_tax_lines(i).asset_id;
4291         l_tclv_tbl(i).tcl_type      := l_fnd_rec.lookup_code;
4292         l_tclv_tbl(i).tax_line_id   := l_accoutable_tax_lines(i).tax_line_id;
4293         l_tclv_tbl(i).amount        := l_accoutable_tax_lines(i).tax_amount;
4294         l_tclv_tbl(i).currency_code := l_trxh_out_rec.currency_code;
4295 
4296         IF (l_accoutable_tax_lines(i).tax_treatment = 'FINANCE') THEN
4297           l_tclv_tbl(i).sty_id    := l_financed_sty_id;
4298         END IF;
4299 
4300         IF (l_accoutable_tax_lines(i).tax_treatment = 'CAPITALIZE') THEN
4301           l_tclv_tbl(i).sty_id    := l_capitalized_sty_id;
4302         END IF;
4303 
4304         --Bug# 6619311
4305         IF (l_accoutable_tax_lines(i).tax_treatment = 'BILLED')
4306         THEN
4307           l_tclv_tbl(i).sty_id    := l_billed_sty_id;
4308         END IF;
4309         --End Bug# 6619311
4310 
4311       END LOOP;
4312 
4313       -- Create TRX lines with the data gathered
4314 
4315       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4316       THEN
4317         OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
4318                               ,G_MODULE
4319                               , 'OKL: create_upfront_tax_accounting Procedure: Calling Okl_Trx_Contracts_Pub.create_trx_cntrct_lines ');
4320       END IF;
4321 
4322       Okl_Trx_Contracts_Pub.create_trx_cntrct_lines(
4323                                       p_api_version   => p_api_version,
4324                                       p_init_msg_list => p_init_msg_list,
4325                                       x_return_status => x_return_status,
4326                                       x_msg_count     => x_msg_count,
4327                                       x_msg_data      => x_msg_data,
4328                                       p_tclv_tbl      => l_tclv_tbl,
4329                                       x_tclv_tbl      => x_tclv_tbl);
4330 
4331       okl_debug_pub.logmessage('OKL: UPF A/C : create_trx_cntrct_lines : '||x_return_status);
4332 
4333       -- check transaction line creation was successful
4334       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4335       THEN
4336         RAISE upfront_tax_acct_exception;
4337       END IF;
4338 
4339       -- Populate accounting API data structures
4340       populate_account_data(
4341                     p_api_version
4342                     ,p_init_msg_list
4343                     ,l_trxh_out_rec
4344                     ,x_tclv_tbl
4345                     ,l_acc_gen_tbl
4346                     ,l_tmpl_identify_tbl
4347                     ,l_dist_info_tbl
4348                     ,x_return_status
4349                     ,x_msg_count
4350                     ,x_msg_data);
4351 
4352       okl_debug_pub.logmessage('OKL: UPF A/C : populate_account_data : '||x_return_status);
4353 
4354       -- check transaction line creation was successful
4355       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4356       THEN
4357         RAISE upfront_tax_acct_exception;
4358       END IF;
4359 
4360       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4361       THEN
4362         OKL_DEBUG_PUB.LOG_DEBUG( FND_LOG.LEVEL_STATEMENT
4363                               ,G_MODULE
4364                               ,'OKL: create_upfront_tax_accounting Procedure: Calling Okl_Account_Dist_Pub.CREATE_ACCOUNTING_DIST');
4365       END IF;
4366 
4367       okl_debug_pub.logmessage('OKL: UPF A/C : before calling okl_account_dist_pvt');
4368 
4369       -- Call Accounting API to create distributions
4370       okl_account_dist_pvt.create_accounting_dist(
4371                                   p_api_version             => 1.0,
4372                                   p_init_msg_list           => p_init_msg_list,
4373                                   x_return_status           => x_return_status,
4374                                   x_msg_count               => x_msg_count,
4375                                   x_msg_data                => x_msg_data,
4376                                   p_tmpl_identify_tbl       => l_tmpl_identify_tbl,
4377                                   p_dist_info_tbl           => l_dist_info_tbl,
4378                                   p_ctxt_val_tbl            => l_ctxt_tbl,
4379                                   p_acc_gen_primary_key_tbl => l_acc_gen_tbl,
4380                                   x_template_tbl            => l_template_out_tbl,
4381                                   x_amount_tbl              => l_amount_tbl,
4382                                   p_trx_header_id           => l_trxh_out_rec.id);
4383 
4384       okl_debug_pub.logmessage('OKL: UPF A/C : after calling okl_account_dist_pvt : '|| x_return_status);
4385 
4386       IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
4387       THEN
4388         RAISE upfront_tax_acct_exception;
4389       END IF;
4390 
4391       OKL_MULTIGAAP_ENGINE_PVT.CREATE_SEC_REP_TRX
4392                            (p_api_version => p_api_version
4393                            ,p_init_msg_list => p_init_msg_list
4394                            ,x_return_status => x_return_status
4395                            ,x_msg_count => x_msg_count
4396                            ,x_msg_data => x_msg_data
4397                            ,P_TCNV_REC => l_trxh_out_rec
4398                            ,P_TCLV_TBL => x_tclv_tbl
4399                            ,p_ctxt_val_tbl => l_ctxt_tbl
4400                            ,p_acc_gen_primary_key_tbl => l_acc_gen_tbl(1).acc_gen_key_tbl);
4401 
4402        IF (x_return_status = Okl_Api.G_RET_STS_UNEXP_ERROR) THEN
4403             RAISE Okl_Api.G_EXCEPTION_UNEXPECTED_ERROR;
4404        ELSIF (x_return_status = Okl_Api.G_RET_STS_ERROR) THEN
4405             RAISE Okl_Api.G_EXCEPTION_ERROR;
4406        END IF;
4407 
4408      END IF;
4409      --Bug# 7506009
4410 
4411     END IF; -- AMB Check
4412 
4413     okl_debug_pub.logmessage('OKL: UPF A/C : END');
4414 
4415   EXCEPTION
4416     WHEN upfront_tax_acct_exception
4417     THEN
4418       x_return_status := OKL_API.G_RET_STS_ERROR;
4419 
4420   END create_upfront_tax_accounting;
4421 
4422   -- R12 - END
4423 
4424 
4425   -- Process to process pre booking tax lines
4426   Procedure process_prebook_tax(
4427                       p_api_version      IN  NUMBER,
4428                       p_init_msg_list    IN  VARCHAR2 DEFAULT Okl_Api.G_FALSE,
4429                       p_contract_id      IN  NUMBER,
4430                       p_transaction_id   IN  NUMBER,
4431                       p_transaction_type IN  VARCHAR2,
4432                       x_return_status    OUT NOCOPY VARCHAR2,
4433                       x_msg_count        OUT NOCOPY NUMBER,
4434                       x_msg_data         OUT NOCOPY VARCHAR2
4435                           )
4436   IS
4437 
4438     rule_info_csr_rec           rule_info_csr%ROWTYPE;
4439     l_asset_count               NUMBER := 0;
4440     l_stream_id                 NUMBER := 0;
4441     l_fee_type                  OKC_RULES_B.rule_information1%TYPE;
4442     l_prev_fee_type             OKC_RULES_B.rule_information1%TYPE;
4443     l_multiple_fee_type         VARCHAR2(1) := Okl_Api.G_FALSE;
4444     pre_book_exception          exception;
4445 
4446     -- Cursor to check if sales tax fee exists
4447     -- Bug# 6512668: Exclude fee line in Abandoned status
4448     CURSOR check_st_fee_csr(p_chr_id IN NUMBER,
4449                             p_fee_type IN VARCHAR2)  IS
4450     SELECT cle.id
4451     FROM   okc_k_lines_b cle,
4452            okl_k_lines kle
4453     WHERE  cle.id = kle.id
4454     AND    cle.dnz_chr_id = p_chr_id
4455     AND    kle.fee_purpose_code = 'SALESTAX'
4456     AND    kle.fee_type = NVL(p_fee_type,kle.fee_type)
4457     AND    cle.sts_code <> 'ABANDONED';
4458 
4459     l_del_fee_line_id OKC_K_LINES_B.id%TYPE;
4460     l_del_fee_types_rec  OKL_MAINTAIN_FEE_PVT.fee_types_rec_type;
4461     l_fee_type_to_delete OKL_K_LINES.fee_type%TYPE;
4462 
4463   BEGIN
4464     IF (G_DEBUG_ENABLED = 'Y') THEN
4465       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
4466     END IF;
4467 
4468     x_return_status  := Okl_Api.G_RET_STS_SUCCESS;
4469     -- Get rule value to identify upfront tax type
4470 
4471     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4472           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: START ');
4473     END IF;
4474 
4475     OPEN  rule_info_csr(p_contract_id);
4476     FETCH rule_info_csr INTO rule_info_csr_rec;
4477     IF rule_info_csr%NOTFOUND THEN
4478       OKL_API.set_message( p_app_name     => G_APP_NAME,
4479                            p_msg_name     => 'OKL_QA_ST_MISSING');
4480       CLOSE rule_info_csr;
4481       RAISE pre_book_exception;
4482     END IF;
4483     CLOSE rule_info_csr;
4484 
4485     l_asset_count := 0;
4486     OPEN  check_lines_csr (p_contract_id, rule_info_csr_rec.rule_information1);
4487     FETCH check_lines_csr INTO l_asset_count;
4488     CLOSE check_lines_csr;
4489 
4490     -- If upfront tax is 'FINANCED'
4491     If (rule_info_csr_rec.rule_information1 = 'FINANCE'
4492         AND
4493         l_asset_count > 0)
4494     THEN
4495       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4496       THEN
4497         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL:  '||p_transaction_type||' Procedure: before calling create fee in FINANCE if');
4498       END IF;
4499       create_fee(
4500                  p_api_version      => p_api_version,
4501                  p_init_msg_list    => p_init_msg_list,
4502                  p_contract_id      => p_contract_id,
4503                  p_transaction_id   => p_transaction_id,
4504                  p_default_feetype  => rule_info_csr_rec.rule_information1,
4505                  p_required_feetype => rule_info_csr_rec.rule_information1,
4506                  p_stream_id        => to_number(rule_info_csr_rec.rule_information3),
4507                  x_return_status    => x_return_status,
4508                  x_msg_count        => x_msg_count,
4509                  x_msg_data         => x_msg_data);
4510       -- Check if the call was successful
4511       If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
4512         RAISE pre_book_exception;
4513       End If;
4514 
4515       -- R12B Authoring OA Migration
4516       -- Delete Sales Tax Fee of Fee Type Capitalized
4517       l_del_fee_line_id := NULL;
4518 
4519       -- Check if Sales Tax Fee exists
4520       OPEN check_st_fee_csr(p_chr_id => p_contract_id,
4521                             p_fee_type => 'CAPITALIZED');
4522       FETCH check_st_fee_csr INTO l_del_fee_line_id;
4523       CLOSE check_st_fee_csr;
4524 
4525       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4526         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||': CAPITALIZED Sales Tax Fee to Delete: '|| l_del_fee_line_id);
4527       END IF;
4528 
4529       IF l_del_fee_line_id IS NOT NULL THEN
4530 
4531         l_del_fee_types_rec.line_id := l_del_fee_line_id;
4532         l_del_fee_types_rec.dnz_chr_id := p_contract_id;
4533 
4534         -- delete fee line
4535         OKL_MAINTAIN_FEE_PVT.delete_fee_type(
4536           p_api_version    => p_api_version,
4537           p_init_msg_list  => p_init_msg_list,
4538           x_return_status  => x_return_status,
4539           x_msg_count      => x_msg_count,
4540           x_msg_data       => x_msg_data,
4541           p_fee_types_rec  => l_del_fee_types_rec
4542         );
4543 
4544         IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
4545           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: after calling OKL_MAINTAIN_FEE_PVT.delete_fee_type: x_return_status '|| x_return_status );
4546         END IF;
4547 
4548         -- Check if the call was successful
4549         IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
4550           RAISE pre_book_exception;
4551         END IF;
4552       END IF;
4553       -- R12B Authoring OA Migration
4554 
4555     End If;
4556 
4557     -- If upfront tax is 'CAPITALIZED'
4558     If (rule_info_csr_rec.rule_information1 = 'CAPITALIZE' AND l_asset_count > 0)
4559     THEN
4560       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4561               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: before calling create fee in CAPITALIZE if ');
4562       END IF;
4563       create_fee(
4564                  p_api_version      => p_api_version,
4565                  p_init_msg_list    => p_init_msg_list,
4566                  p_contract_id      => p_contract_id,
4567                  p_transaction_id   => p_transaction_id,
4568                  p_default_feetype  => rule_info_csr_rec.rule_information1,
4569                  p_required_feetype => rule_info_csr_rec.rule_information1,
4570                  p_stream_id        => to_number(rule_info_csr_rec.rule_information4),
4571                  x_return_status    => x_return_status,
4572                  x_msg_count        => x_msg_count,
4573                  x_msg_data         => x_msg_data);
4574       -- Check if the call was successful
4575       If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
4576         RAISE pre_book_exception;
4577       End If;
4578 
4579       -- R12B Authoring OA Migration
4580       -- Delete Sales Tax Fee of Fee Type Financed
4581       l_del_fee_line_id := NULL;
4582 
4583       -- Check if Sales Tax Fee exists
4584       OPEN check_st_fee_csr(p_chr_id => p_contract_id,
4585                             p_fee_type => 'FINANCED');
4586       FETCH check_st_fee_csr INTO l_del_fee_line_id;
4587       CLOSE check_st_fee_csr;
4588 
4589       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4590         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||': FINANCED Sales Tax Fee to Delete: '|| l_del_fee_line_id);
4591       END IF;
4592 
4593       IF l_del_fee_line_id IS NOT NULL THEN
4594 
4595         l_del_fee_types_rec.line_id := l_del_fee_line_id;
4596         l_del_fee_types_rec.dnz_chr_id := p_contract_id;
4597 
4598         -- delete fee line
4599         OKL_MAINTAIN_FEE_PVT.delete_fee_type(
4600           p_api_version    => p_api_version,
4601           p_init_msg_list  => p_init_msg_list,
4602           x_return_status  => x_return_status,
4603           x_msg_count      => x_msg_count,
4604           x_msg_data       => x_msg_data,
4605           p_fee_types_rec  => l_del_fee_types_rec
4606         );
4607 
4608         IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
4609           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: after calling OKL_MAINTAIN_FEE_PVT.delete_fee_type: x_return_status '|| x_return_status );
4610         END IF;
4611 
4612         -- Check if the call was successful
4613         IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
4614           RAISE pre_book_exception;
4615         END IF;
4616       END IF;
4617       -- R12B Authoring OA Migration
4618     End If;
4619 
4620     -- If upfront tax is 'BILLED'
4621     IF (rule_info_csr_rec.rule_information1 = 'BILLED') THEN
4622 
4623       l_multiple_fee_type := Okl_Api.G_FALSE;
4624       l_fee_type := NULL;
4625 
4626       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4627               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: verifying if multiple fee treatments are selected ');
4628       END IF;
4629       FOR i in check_feetype_csr (p_contract_id)
4630       LOOP
4631         l_fee_type := i.feetype;
4632         IF (l_fee_type <> l_prev_fee_type) THEN
4633           l_multiple_fee_type := Okl_Api.G_TRUE;
4634           EXIT;
4635         END IF;
4636         l_prev_fee_type := i.feetype;
4637       END LOOP;
4638 
4639       IF (l_multiple_fee_type = Okl_Api.G_FALSE AND l_fee_type IS NOT NULL)
4640       THEN
4641         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4642                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: if multiple fee treatments are NOT selected');
4643         END IF;
4644         IF (l_fee_type = 'FINANCE') THEN
4645           l_stream_id := to_number(rule_info_csr_rec.rule_information3);
4646         END IF;
4647 
4648         IF (l_fee_type = 'CAPITALIZE') THEN
4649           l_stream_id := to_number(rule_info_csr_rec.rule_information4);
4650         END IF;
4651 
4652         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4653                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: before calling create fee for '||l_fee_type|| ' in BILLED if ');
4654         END IF;
4655         create_fee(
4656                  p_api_version      => p_api_version,
4657                  p_init_msg_list    => p_init_msg_list,
4658                  p_contract_id      => p_contract_id,
4659                  p_transaction_id   => p_transaction_id,
4660                  p_default_feetype  => 'BILLED',
4661                  p_required_feetype => l_fee_type,
4662                  p_stream_id        => to_number(l_stream_id),
4663                  x_return_status    => x_return_status,
4664                  x_msg_count        => x_msg_count,
4665                  x_msg_data         => x_msg_data);
4666         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4667                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: after calling create fee for '||
4668                                  l_fee_type
4669                                  || ' in BILLED if return status '|| x_return_status );
4670         END IF;
4671         -- Check if the call was successful
4672         If (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) then
4673           RAISE pre_book_exception;
4674         End If;
4675 
4676         -- R12B Authoring OA Migration
4677         -- Delete Sales Tax Fee of Fee Type Financed
4678         l_del_fee_line_id := NULL;
4679         IF (l_fee_type = 'FINANCE') THEN
4680           l_fee_type_to_delete := 'CAPITALIZED';
4681         ELSIF (l_fee_type = 'CAPITALIZE') THEN
4682           l_fee_type_to_delete := 'FINANCED';
4683         END IF;
4684 
4685         -- Check if Sales Tax Fee exists
4686         OPEN check_st_fee_csr(p_chr_id => p_contract_id,
4687                               p_fee_type => l_fee_type_to_delete);
4688         FETCH check_st_fee_csr INTO l_del_fee_line_id;
4689         CLOSE check_st_fee_csr;
4690 
4691         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4692           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||': '||l_fee_type_to_delete||' Sales Tax Fee to Delete: '|| l_del_fee_line_id);
4693         END IF;
4694 
4695         IF l_del_fee_line_id IS NOT NULL THEN
4696 
4697           l_del_fee_types_rec.line_id := l_del_fee_line_id;
4698           l_del_fee_types_rec.dnz_chr_id := p_contract_id;
4699 
4700           -- delete fee line
4701           OKL_MAINTAIN_FEE_PVT.delete_fee_type(
4702             p_api_version    => p_api_version,
4703             p_init_msg_list  => p_init_msg_list,
4704             x_return_status  => x_return_status,
4705             x_msg_count      => x_msg_count,
4706             x_msg_data       => x_msg_data,
4707             p_fee_types_rec  => l_del_fee_types_rec
4708           );
4709 
4710           IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
4711             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: after calling OKL_MAINTAIN_FEE_PVT.delete_fee_type: x_return_status '|| x_return_status );
4712           END IF;
4713 
4714           -- Check if the call was successful
4715           IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
4716             RAISE pre_book_exception;
4717           END IF;
4718         END IF;
4719         -- R12B Authoring OA Migration
4720       End If;
4721     End If;
4722 
4723     -- R12B Authoring OA Migration
4724     -- Delete Sales Tax Fee line if no Assets have tax treatment as Financed or Capitalized
4725     IF (rule_info_csr_rec.rule_information1 IN ('CAPITALIZE','FINANCE') AND l_asset_count = 0) OR
4726        (rule_info_csr_rec.rule_information1 = 'BILLED' AND NVL(l_fee_type,'BILLED') = 'BILLED') THEN
4727 
4728       l_del_fee_line_id := NULL;
4729 
4730       -- Check if Sales Tax Fee exists
4731       OPEN check_st_fee_csr(p_chr_id => p_contract_id,
4732                             p_fee_type => NULL);
4733       FETCH check_st_fee_csr INTO l_del_fee_line_id;
4734       CLOSE check_st_fee_csr;
4735 
4736       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4737         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||': Sales Tax Fee to Delete: '|| l_del_fee_line_id);
4738       END IF;
4739 
4740       IF l_del_fee_line_id IS NOT NULL THEN
4741 
4742         l_del_fee_types_rec.line_id := l_del_fee_line_id;
4743         l_del_fee_types_rec.dnz_chr_id := p_contract_id;
4744 
4745         -- delete fee line
4746         OKL_MAINTAIN_FEE_PVT.delete_fee_type(
4747           p_api_version    => p_api_version,
4748           p_init_msg_list  => p_init_msg_list,
4749           x_return_status  => x_return_status,
4750           x_msg_count      => x_msg_count,
4751           x_msg_data       => x_msg_data,
4752           p_fee_types_rec  => l_del_fee_types_rec
4753         );
4754 
4755         IF (G_IS_DEBUG_STATEMENT_ON = true) THEN
4756           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: after calling OKL_MAINTAIN_FEE_PVT.delete_fee_type: x_return_status '|| x_return_status );
4757         END IF;
4758 
4759         -- Check if the call was successful
4760         IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
4761           RAISE pre_book_exception;
4762         END IF;
4763       END IF;
4764     END IF;
4765     -- R12B Authoring OA Migration
4766 
4767     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
4768           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: '||p_transaction_type||' Procedure: END ');
4769     END IF;
4770 
4771     Exception
4772       when pre_book_exception then
4773          x_return_status := OKL_API.G_RET_STS_ERROR;
4774 
4775   END process_prebook_tax;
4776 
4777   -- Procedure to process Booking
4778   Procedure process_booking_tax(
4779                       p_api_version      IN  NUMBER,
4780                       p_init_msg_list    IN  VARCHAR2 DEFAULT okl_Api.G_FALSE,
4781                       p_contract_id      IN  NUMBER,
4782                       p_transaction_id   IN  NUMBER,
4783                       p_transaction_type IN  VARCHAR2,
4784                       x_return_status    OUT NOCOPY VARCHAR2,
4785                       x_msg_count        OUT NOCOPY NUMBER,
4786                       x_msg_data         OUT NOCOPY VARCHAR2 )
4787   IS
4788 
4789     -- Cursor to get contract start date for billing
4790     CURSOR contract_csr IS
4791     SELECT start_date
4792     FROM   okc_k_headers_b
4793     WHERE  id = p_contract_id;
4794 
4795     rule_info_csr_rec                rule_info_csr%ROWTYPE;
4796     l_ST_params_exist                VARCHAR2(1);
4797     l_api_name                       CONSTANT VARCHAR(30) := 'process_booking_tax';
4798     l_booking_financed_tax           NUMBER;
4799     l_prebook_financed_tax           NUMBER;
4800     l_booking_capitalized_tax        NUMBER;
4801     L_prebook_capitalized_tax        NUMBER;
4802     l_booking_billable_tax           NUMBER;
4803     l_contract_start_date            OKC_K_HEADERS_B.START_DATE%TYPE;
4804     l_source_table                   VARCHAR2(30) := 'OKL_TRX_CONTRACTS';
4805     l_billed_assets                  NUMBER :=0;
4806     l_asset_count                    NUMBER :=0;
4807     l_multiple_fee_type              VARCHAR2(1) := Okl_Api.G_FALSE;
4808     l_fee_type                       OKC_RULES_B.rule_information1%TYPE;
4809     l_prev_fee_type                  OKC_RULES_B.rule_information1%TYPE;
4810     l_contract_bill_tax              NUMBER :=0;
4811     booking_exception                exception;
4812 
4813   BEGIN
4814 
4815     okl_debug_pub.logmessage('OKL: process_booking_tax : START' );
4816 
4817     IF (G_DEBUG_ENABLED = 'Y')
4818     THEN
4819       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE
4820                                                             , FND_LOG.LEVEL_STATEMENT);
4821     END IF;
4822 
4823     x_return_status  := okl_Api.G_RET_STS_SUCCESS;
4824 
4825     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4826     THEN
4827       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
4828                               ,G_MODULE
4829                               ,'OKL: Booking Procedure: START');
4830     END IF;
4831 
4832     -- 1. Check if user selected Sales Tax parameters. There is a posibility
4833     --    user does not create Sales Tax T and C
4834 
4835     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4836     THEN
4837       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
4838                               ,G_MODULE
4839                               ,'OKL: Booking Procedure: before rule_info_csr');
4840     END IF;
4841 
4842     OPEN rule_info_csr(p_contract_id);
4843     FETCH rule_info_csr INTO rule_info_csr_rec;
4844 
4845     IF rule_info_csr%NOTFOUND
4846     THEN
4847       OKL_API.set_message( p_app_name => G_APP_NAME,
4848                            p_msg_name => 'OKL_QA_ST_MISSING');
4849       CLOSE rule_info_csr;
4850       RAISE booking_exception;
4851     END IF;
4852 
4853     CLOSE rule_info_csr;
4854 
4855     -- Call Tax API
4856     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4857     THEN
4858       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
4859                               , G_MODULE
4860                               ,'OKL: Booking Procedure: before calling tax API');
4861     END IF;
4862 
4863     OKL_PROCESS_SALES_TAX_PUB.calculate_sales_tax(
4864                       p_api_version      => p_api_version,
4865                       p_init_msg_list    => p_init_msg_list,
4866                       x_return_status    => x_return_status,
4867                       x_msg_count        => x_msg_count,
4868                       x_msg_data         => x_msg_data,
4869                       p_source_trx_id    => p_transaction_id,
4870                       p_source_trx_name  => p_transaction_type,
4871                       p_source_table     => l_source_table,
4872                       p_tax_call_type    => 'ACTUAL' );
4873 
4874     okl_debug_pub.logmessage('OKL: process_booking_tax : OKL_PROCESS_SALES_TAX_PUB.calculate_sales_tax: ACTUAL : '|| x_return_status );
4875 
4876     -- Check if the tax call was successful
4877     IF (x_return_status <> okl_Api.G_RET_STS_SUCCESS)
4878     THEN
4879       RAISE booking_exception;
4880     END IF;
4881 
4882     --Bug# 6619311: Create upfront tax accounting for all tax treatments
4883     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4884     THEN
4885       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
4886                               ,G_MODULE
4887                               ,'OKL: Booking Procedure: Calling create_upfront_tax_accounting ');
4888     END IF;
4889 
4890     create_upfront_tax_accounting
4891             (
4892              p_api_version      => p_api_version,
4893              p_init_msg_list    => p_init_msg_list,
4894              p_contract_id      => p_contract_id,
4895              p_transaction_id   => p_transaction_id,
4896              p_transaction_type => p_transaction_type,
4897              x_return_status    => x_return_status,
4898              x_msg_count        => x_msg_count,
4899              x_msg_data         => x_msg_data
4900             );
4901 
4902     IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
4903       RAISE booking_exception;
4904     END IF;
4905 
4906     -- This segment will be executed when user selected Financed or Capitalized
4907     -- T and C but selected billed option for one or more asset at line level
4908     IF (rule_info_csr_rec.rule_information1 = 'CAPITALIZE'
4909         OR
4910         rule_info_csr_rec.rule_information1 = 'FINANCE')
4911     THEN
4912 
4913       -- Check if billing required and call billing API as required
4914 
4915       --R12B ebTax changes
4916       l_billed_assets := 0;
4917       OPEN billing_required_csr1(p_contract_id,p_transaction_id);
4918       FETCH billing_required_csr1 into l_billed_assets;
4919       CLOSE billing_required_csr1;
4920 
4921       --Bug# 6939336
4922       l_contract_bill_tax := 0;
4923       OPEN contract_billing_csr(p_contract_id, p_transaction_id);
4924       FETCH contract_billing_csr INTO l_contract_bill_tax;
4925       CLOSE contract_billing_csr;
4926 
4927       --Bug# 6939336
4928       IF (l_billed_assets <> 0 OR l_contract_bill_tax <> 0)
4929       THEN
4930 
4931         IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4932         THEN
4933           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
4934                                   , G_MODULE
4935                                   ,'OKL: Booking Procedure: before contract_csr');
4936         END IF;
4937 
4938         OPEN contract_csr;
4939         FETCH contract_csr INTO l_contract_start_date;
4940 
4941         IF contract_csr%NOTFOUND
4942         THEN
4943           OKL_API.set_message( p_app_name     => G_APP_NAME,
4944                                p_msg_name     => 'OKL_LA_ST_K_DATE_ERROR');
4945           CLOSE contract_csr;
4946           RAISE booking_exception;
4947         END IF;
4948 
4949         CLOSE contract_csr;
4950 
4951         OKL_BILL_UPFRONT_TAX_PVT.Bill_Upfront_Tax(
4952            p_api_version      => p_api_version,
4953            p_init_msg_list    => p_init_msg_list,
4954            p_khr_id           => p_contract_id,
4955            p_trx_id           => p_transaction_id,
4956            p_invoice_date     => l_contract_start_date,
4957            x_return_status    => x_return_status,
4958            x_msg_count        => x_msg_count,
4959            x_msg_data         => x_msg_data);
4960 
4961         IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
4962           RAISE booking_exception;
4963         END IF;
4964 
4965       END IF;
4966 
4967     END IF;
4968 
4969     -- But we will bill any amount that comes for Billing Tax
4970     IF (rule_info_csr_rec.rule_information1 = 'BILLED')
4971     THEN
4972 
4973       -- Check if any of the assets is financed or capitalized and
4974       -- check tax pre-book and booking tax amounts
4975       l_multiple_fee_type := Okl_Api.G_FALSE;
4976       l_fee_type := NULL;
4977 
4978       IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
4979       THEN
4980         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
4981                                 , G_MODULE
4982                                 ,'OKL: Booking Procedure: verifying if multiple fee treatments are selected ');
4983       END IF;
4984 
4985       FOR i in check_feetype_csr (p_contract_id)
4986       LOOP
4987         l_fee_type := i.feetype;
4988 
4989         IF (l_fee_type <> l_prev_fee_type)
4990         THEN
4991           l_multiple_fee_type := okl_Api.G_TRUE;
4992           EXIT;
4993         END IF;
4994 
4995         l_prev_fee_type := i.feetype;
4996       END LOOP;
4997 
4998       -- User cannot create both Financed and Capitalized sales tax fee lines
4999       IF (l_multiple_fee_type = okl_Api.G_TRUE)
5000       THEN
5001         OKL_API.set_message( p_app_name => G_APP_NAME,
5002                              p_msg_name => 'OKL_LA_ST_MIX_FEE_ERROR');
5003         RAISE booking_exception;
5004       END IF;
5005 
5006       -- Now Billing transaction creation logic starts
5007       -- Check if billing required and call billing API as required
5008       l_billed_assets := 0;
5009       OPEN billing_required_csr2(p_contract_id,p_transaction_id);
5010       FETCH billing_required_csr2 into l_billed_assets;
5011       CLOSE billing_required_csr2;
5012 
5013       -- R12B eBtax changes
5014       l_contract_bill_tax := 0;
5015       OPEN contract_billing_csr(p_contract_id, p_transaction_id);
5016       FETCH contract_billing_csr INTO l_contract_bill_tax;
5017       CLOSE contract_billing_csr;
5018 
5019       -- R12B eBtax changes
5020       IF (l_billed_assets <> 0 OR l_contract_bill_tax <> 0)
5021       THEN
5022         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5023           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
5024                                   , G_MODULE
5025                                   ,'OKL: Booking Procedure: before contract_csr');
5026         END IF;
5027         OPEN contract_csr;
5028         FETCH contract_csr INTO l_contract_start_date;
5029         IF contract_csr%NOTFOUND THEN
5030           OKL_API.set_message( p_app_name => G_APP_NAME,
5031                                p_msg_name => 'OKL_LA_ST_K_DATE_ERROR');
5032           CLOSE contract_csr;
5033           RAISE booking_exception;
5034         END IF;
5035         CLOSE contract_csr;
5036 
5037         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5038                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Booking Procedure: before OKL_BILL_UPFRONT_TAX_PVT ');
5039         END IF;
5040 
5041         OKL_BILL_UPFRONT_TAX_PVT.Bill_Upfront_Tax(
5042            p_api_version      => p_api_version,
5043            p_init_msg_list    => p_init_msg_list,
5044            p_khr_id           => p_contract_id,
5045            p_trx_id           => p_transaction_id,
5046            p_invoice_date     => l_contract_start_date,
5047            x_return_status    => x_return_status,
5048            x_msg_count        => x_msg_count,
5049            x_msg_data         => x_msg_data);
5050 
5051         IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
5052           RAISE booking_exception;
5053         END IF;
5054       END IF;
5055     END IF;
5056 
5057     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
5058     THEN
5059       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
5060                               , G_MODULE
5061                               ,'OKL: Booking Procedure: END');
5062     END IF;
5063 
5064     okl_debug_pub.logmessage('OKL: process_booking_tax : END' );
5065 
5066   Exception
5067       when booking_exception then
5068          x_return_status := OKL_API.G_RET_STS_ERROR;
5069 
5070 END process_booking_tax;
5071 
5072 
5073   -- Procedure to process Booking
5074   Procedure process_rebook_tax(
5075                       p_api_version      IN  NUMBER,
5076                       p_init_msg_list    IN  VARCHAR2 DEFAULT Okl_Api.G_FALSE,
5077                       p_contract_id      IN  NUMBER,
5078                       p_transaction_id   IN  NUMBER,
5079                       p_transaction_type IN  VARCHAR2,
5080                       p_transaction_date IN  DATE,
5081                       p_rbk_contract_id  IN  NUMBER,
5082                       x_return_status    OUT NOCOPY VARCHAR2,
5083                       x_msg_count        OUT NOCOPY NUMBER,
5084                       x_msg_data         OUT NOCOPY VARCHAR2 )
5085   IS
5086 
5087     -- R12 - START
5088     CURSOR acct_opt_csr
5089     IS
5090     SELECT account_derivation
5091     FROM   okl_sys_acct_opts;
5092 
5093     l_acct_opt okl_sys_acct_opts.account_derivation%TYPE;
5094     -- R12 - END
5095 
5096     l_contract_bill_tax         NUMBER :=0;
5097     rule_info_csr_rec           rule_info_csr%ROWTYPE;
5098     l_asset_count               NUMBER;
5099     l_billed_assets             NUMBER :=0;
5100     l_multiple_fee_type         VARCHAR2(1) := Okl_Api.G_FALSE;
5101     l_fee_type                  OKC_RULES_B.rule_information1%TYPE;
5102     l_prev_fee_type             OKC_RULES_B.rule_information1%TYPE;
5103     rebook_exception            EXCEPTION;
5104 
5105   BEGIN
5106 
5107     okl_debug_pub.logmessage('OKL: process_rebook_tax : START' );
5108 
5109     x_return_status  := Okl_Api.G_RET_STS_SUCCESS;
5110 
5111     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5112       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
5113                               ,G_MODULE
5114                               ,'OKL: Rebook Procedure: START');
5115     END IF;
5116 
5117     OPEN  rule_info_csr (p_contract_id);
5118     FETCH rule_info_csr INTO rule_info_csr_rec;
5119 
5120     IF rule_info_csr%NOTFOUND
5121     THEN
5122       OKL_API.set_message( p_app_name     => G_APP_NAME,
5123                            p_msg_name     => 'OKL_QA_ST_MISSING');
5124       CLOSE rule_info_csr;
5125       RAISE rebook_exception;
5126     END IF;
5127 
5128     CLOSE rule_info_csr;
5129 
5130     l_asset_count := 0;
5131     OPEN  check_lines_csr (p_contract_id, rule_info_csr_rec.rule_information1);
5132     FETCH check_lines_csr INTO l_asset_count;
5133     CLOSE check_lines_csr;
5134 
5135     OKL_PROCESS_SALES_TAX_PUB.calculate_sales_tax(
5136                       p_api_version      => p_api_version,
5137                       p_init_msg_list    => p_init_msg_list,
5138                       x_return_status    => x_return_status,
5139                       x_msg_count        => x_msg_count,
5140                       x_msg_data         => x_msg_data,
5141                       p_source_trx_id    => p_transaction_id,
5142                       p_source_trx_name  => p_transaction_type,
5143                       p_source_table     => g_source_table,
5144                       p_tax_call_type    => 'ACTUAL' );
5145 
5146     okl_debug_pub.logmessage('OKL: process_rebook_tax : OKL_PROCESS_SALES_TAX_PUB.calculate_sales_tax: ACTUAL : '|| x_return_status );
5147 
5148     -- Check if the tax call was successful
5149     IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS)
5150     THEN
5151       RAISE rebook_exception;
5152     END IF;
5153 
5154     --Bug# 6619311: Create upfront tax accounting for all tax treatments
5155     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
5156     THEN
5157       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
5158                               ,G_MODULE
5159                               ,'OKL: Booking Procedure: Calling create_upfront_tax_accounting ');
5160     END IF;
5161 
5162     create_upfront_tax_accounting
5163             (
5164              p_api_version      => p_api_version,
5165              p_init_msg_list    => p_init_msg_list,
5166              p_contract_id      => p_contract_id,
5167              p_transaction_id   => p_transaction_id,
5168              p_transaction_type => p_transaction_type,
5169              x_return_status    => x_return_status,
5170              x_msg_count        => x_msg_count,
5171              x_msg_data         => x_msg_data
5172             );
5173 
5174     IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
5175       RAISE rebook_exception;
5176     END IF;
5177 
5178     -- This segment will be executed when user selected Financed or Capitalized
5179     -- T and C but selected billed option for one or more asset at line level
5180     IF (rule_info_csr_rec.rule_information1 = 'CAPITALIZE'
5181         OR
5182         rule_info_csr_rec.rule_information1 = 'FINANCE')
5183     THEN
5184 
5185       -- Check if billing required and call billing API as required
5186       -- R12B eBTax changes
5187       l_billed_assets := 0;
5188       OPEN billing_required_csr1(p_contract_id,p_transaction_id);
5189       FETCH billing_required_csr1 into l_billed_assets;
5190       CLOSE billing_required_csr1;
5191 
5192       --Bug# 6939336
5193       l_contract_bill_tax := 0;
5194       OPEN contract_billing_csr(p_contract_id, p_transaction_id);
5195       FETCH contract_billing_csr INTO l_contract_bill_tax;
5196       CLOSE contract_billing_csr;
5197 
5198       --Bug# 6939336
5199       IF (l_billed_assets <> 0 OR l_contract_bill_tax <> 0)
5200       THEN
5201 
5202         OKL_BILL_UPFRONT_TAX_PVT.Bill_Upfront_Tax(
5203            p_api_version      => p_api_version,
5204            p_init_msg_list    => p_init_msg_list,
5205            p_khr_id           => p_contract_id,
5206            p_trx_id           => p_transaction_id,
5207            p_invoice_date     => p_transaction_date,
5208            x_return_status    => x_return_status,
5209            x_msg_count        => x_msg_count,
5210            x_msg_data         => x_msg_data);
5211 
5212         IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
5213           RAISE rebook_exception;
5214         END IF;
5215 
5216       END IF;
5217 
5218     END IF; -- rule_info_csr_rec.rule_information1 = 'CAPITALIZE' or 'FINANCED
5219 
5220 
5221     -- But we will bill any amount that comes for Billing Tax
5222     IF (rule_info_csr_rec.rule_information1 = 'BILLED')
5223     THEN
5224 
5225       -- Check if any of the assets is financed or capitalized and
5226       -- check tax pre-book and booking tax amounts
5227       l_multiple_fee_type := Okl_Api.G_FALSE;
5228       l_fee_type := NULL;
5229 
5230       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5231         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
5232                                 ,G_MODULE
5233                                 ,'OKL: Rebook Procedure: verifying if multiple fee treatments are selected ');
5234       END IF;
5235       FOR i in check_feetype_csr (p_contract_id)
5236       LOOP
5237         l_fee_type := i.feetype;
5238         IF (l_fee_type <> l_prev_fee_type) THEN
5239           l_multiple_fee_type := Okl_Api.G_TRUE;
5240           EXIT;
5241         END IF;
5242         l_prev_fee_type := i.feetype;
5243       END LOOP;
5244 
5245       -- User cannot create both Financed and Capitalized sales tax fee lines
5246       IF (l_multiple_fee_type = Okl_Api.G_TRUE) THEN
5247         OKL_API.set_message( p_app_name => G_APP_NAME,
5248                              p_msg_name => 'OKL_LA_ST_MIX_FEE_ERROR');
5249         RAISE rebook_exception;
5250       END IF;
5251 
5252       -- Now Billing transaction creation logic starts
5253       -- Check if billing required and call billing API as required
5254       l_billed_assets := 0;
5255       OPEN billing_required_csr2(p_contract_id,p_transaction_id);
5256       FETCH billing_required_csr2 into l_billed_assets;
5257       CLOSE billing_required_csr2;
5258 
5259       -- R12B eBTax changes
5260       l_contract_bill_tax := 0;
5261       OPEN contract_billing_csr(p_contract_id, p_transaction_id);
5262       FETCH contract_billing_csr INTO l_contract_bill_tax;
5263       CLOSE contract_billing_csr;
5264 
5265       -- R12B eBtax changes
5266       IF (l_billed_assets <> 0 OR l_contract_bill_tax <> 0) THEN
5267 
5268         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5269                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Rebook Procedure: before OKL_BILL_UPFRONT_TAX_PVT ');
5270         END IF;
5271 
5272         OKL_BILL_UPFRONT_TAX_PVT.Bill_Upfront_Tax(
5273            p_api_version      => p_api_version,
5274            p_init_msg_list    => p_init_msg_list,
5275            p_khr_id           => p_contract_id,
5276            p_trx_id           => p_transaction_id,
5277            p_invoice_date     => p_transaction_date,
5278            x_return_status    => x_return_status,
5279            x_msg_count        => x_msg_count,
5280            x_msg_data         => x_msg_data);
5281 
5282         IF (x_return_status <> Okl_Api.G_RET_STS_SUCCESS) THEN
5283           RAISE rebook_exception;
5284         END IF;
5285 
5286       END IF;
5287 
5288     END IF; -- rule_info_csr_rec.rule_information1 = 'BILLED'
5289 
5290     IF (G_IS_DEBUG_STATEMENT_ON = TRUE)
5291     THEN
5292       OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT
5293                               ,G_MODULE
5294                               ,'OKL: rebook Procedure: END');
5295     END IF;
5296 
5297     okl_debug_pub.logmessage('OKL: process_rebook_tax : END' );
5298 
5299   Exception
5300     when rebook_exception then
5301       x_return_status := OKL_API.G_RET_STS_ERROR;
5302 
5303   END process_rebook_tax;
5304 
5305 
5306 
5307 
5308   -- Main proccedure to call for all authoring
5309   -- processes to process upfront sales tax
5310 
5311   Procedure process_sales_tax(
5312                       p_api_version      IN  NUMBER,
5313                       p_init_msg_list    IN  VARCHAR2 DEFAULT Okl_Api.G_FALSE,
5314                       p_commit           IN  VARCHAR2 DEFAULT Okl_Api.G_FALSE,
5315                       p_contract_id      IN  NUMBER,
5316                       p_transaction_type IN  VARCHAR2,
5317                       p_transaction_id   IN  NUMBER DEFAULT NULL,
5318                       p_transaction_date IN  DATE DEFAULT NULL,
5319                       p_rbk_contract_id  IN  NUMBER DEFAULT NULL,
5320                       x_return_status    OUT NOCOPY VARCHAR2,
5321                       x_msg_count        OUT NOCOPY NUMBER,
5322                       x_msg_data         OUT NOCOPY VARCHAR2)  IS
5323 
5324      -- Define PL/SQL Records and Tables
5325     l_trxH_in_rec        Okl_Trx_Contracts_Pvt.tcnv_rec_type;
5326     l_trxL_in_rec        Okl_Trx_Contracts_Pvt.tclv_rec_type;
5327     l_trxH_out_rec       Okl_Trx_Contracts_Pvt.tcnv_rec_type;
5328     l_trxL_out_rec       Okl_Trx_Contracts_Pvt.tclv_rec_type;
5329 
5330     -- Define variables
5331     l_return_status   VARCHAR2(1) := Okl_Api.G_RET_STS_SUCCESS;
5332     l_transaction_id  NUMBER;
5333     l_has_trans       VARCHAR2(1);
5334     -- Bug 4769822 - START
5335     l_contract_id     NUMBER;
5336     -- Bug 4769822 - END
5337 
5338 
5339     -- R12 Change - START
5340     rule_info_csr_rec rule_info_csr%ROWTYPE;
5341     -- R12 Change - END
5342 
5343 
5344      -- Define constants
5345     l_api_name        CONSTANT VARCHAR(30) := 'PROCESS_SALES_TAX';
5346 
5347     CURSOR sys_param_csr IS
5348     SELECT nvl(tax_upfront_yn,'N')
5349                  --,           nvl(tax_schedule_yn,'N') -- added for Bug 4748910  -- Not required in R12
5350     FROM   okl_system_params;
5351 
5352     -- Cursor Types
5353     l_fnd_rec          fnd_lookups_csr%ROWTYPE;
5354 
5355     l_transaction_type     VARCHAR2(256) := p_transaction_type;
5356     l_source_table         VARCHAR2(30) := 'OKL_TRX_CONTRACTS';
5357     l_tax_upfront_yn       okl_system_params.tax_upfront_yn%TYPE;
5358     -- l_tax_schedule_yn      VARCHAR2(1) := NULL; -- this variable is not required in R12
5359 
5360     -- Bug 4748910 - START
5361 
5362     CURSOR c_currency_code(p_khr_id IN NUMBER) IS
5363     SELECT start_date, end_date, currency_code
5364     FROM   OKC_K_HEADERS_B
5365     WHERE  ID = p_contract_id;
5366 
5367     CURSOR c_try_type(p_name IN VARCHAR2) IS
5368     SELECT ID
5369     FROM   OKL_TRX_TYPES_TL
5370     WHERE  name = p_name
5371     AND    LANGUAGE = 'US';
5372 
5373     l_currency_code VARCHAR2(15);
5374     l_start_date    DATE;
5375     l_end_date      DATE;
5376     l_try_id        NUMBER;
5377     l_try_name      VARCHAR2(100) := 'Tax Schedule';
5378     l_trqv_tbl      okl_trx_requests_pub.trqv_tbl_type;
5379     x_trqv_tbl      okl_trx_requests_pub.trqv_tbl_type;
5380 
5381     -- BUG 4748910 - END
5382 
5383     -- Bug 6155565
5384 --Bug# 15992711 : The re-leased flag is no longer checked at contract header
5385 --                but needs to be validated at contract line
5386 --                Cursor modified to include validation at asset line level
5387 --                Cursor now counts the number of New assets in the contract
5388    /*     CURSOR l_chk_rel_ast_csr (p_chr_id IN NUMBER) IS
5389     SELECT NVL(rule_information1,'N')
5390     FROM   okc_rules_b rul
5391     WHERE  rul.dnz_chr_id = p_chr_id
5392     AND    rul.rule_information_category = 'LARLES'; */
5393 
5394     CURSOR l_chk_rel_ast_csr (p_chr_id IN NUMBER) IS
5395     SELECT COUNT(KLE.id) new_asset_cnt
5396     FROM   okc_k_lines_b CLEB
5397           ,okl_k_lines   KLE
5398     WHERE  CLEB.id         = KLE.id
5399     AND    CLEB.dnz_chr_id = p_chr_id
5400     AND    CLEB.chr_id     = p_chr_id
5401     AND    CLEB.LSE_ID     = 33 --for asset line
5402     AND    NVL(KLE.re_lease_yn,'N')= 'N';
5403 
5404     l_new_asset_cnt    NUMBER :=0;
5405 
5406 --Bug# 15992711 : End of modifications
5407 
5408 
5409 
5410     l_release_asset_yn VARCHAR2(1);
5411 
5412   BEGIN
5413 
5414     --okl_debug_pub.logmessage('OKL: process_sales_tax : START');
5415 
5416     IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5417         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'okl_la_sales_tax_pvt.process_sales_tax.', 'Begin(+)');
5418     END IF;
5419 
5420    /* IF (G_DEBUG_ENABLED = 'Y') THEN
5421       G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
5422     END IF;
5423 */
5424     x_return_status  := Okl_Api.G_RET_STS_SUCCESS;
5425 
5426     x_return_status := OKL_API.START_ACTIVITY(
5427 			p_api_name      => l_api_name,
5428 			p_pkg_name      => g_pkg_name,
5429 			p_init_msg_list => p_init_msg_list,
5430 			l_api_version   => p_api_version,
5431 			P_api_version   => p_api_version,
5432 			p_api_type      => G_API_TYPE,
5433 			x_return_status => x_return_status);
5434 
5435     IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5436         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '0..');
5437     END IF;
5438 
5439 
5440     -- check if activity started successfully
5441     If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
5442        raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5443     ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
5444        raise OKL_API.G_EXCEPTION_ERROR;
5445     End If;
5446 
5447     IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5448         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '1..');
5449     END IF;
5450 
5451 /*    IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5452           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5453                                   G_MODULE,
5454                                   'OKL: Process Sales Tax : START ');
5455     END IF;
5456 */
5457 
5458     IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5459         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '2..');
5460     END IF;
5461 
5462     OPEN  sys_param_csr;
5463     FETCH sys_param_csr INTO l_tax_upfront_yn;
5464     IF sys_param_csr%NOTFOUND THEN
5465       Okl_Api.SET_MESSAGE(G_APP_NAME, 'OKL_LA_ST_OU_UPFRONT_TAX_ERROR');
5466       CLOSE sys_param_csr;
5467       RAISE Okl_Api.G_EXCEPTION_ERROR;
5468     END IF;
5469     CLOSE sys_param_csr;
5470 
5471     IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5472         FND_LOG.STRING
5473         (FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '3..' || l_tax_upfront_yn);
5474     END IF;
5475 
5476 
5477     -- First off check upfront tax is enabled, if not
5478     -- Sales Tax processing is not required for the contract
5479     IF (l_tax_upfront_yn = 'Y' ) THEN
5480 
5481 
5482      -- Bug 6155565
5483      -- 16522428 14/03/13
5484 
5485      IF p_transaction_type IN( 'Pre-Rebook','Rebook') THEN
5486         OPEN  l_chk_rel_ast_csr(p_rbk_contract_id);
5487      ELSE
5488         OPEN  l_chk_rel_ast_csr(p_contract_id);
5489      END IF;
5490      -- 16522428 14/03/13
5491 
5492      -- Bug# 15992711 : The cursor fetches the number of new assets in a contract
5493      -- FETCH l_chk_rel_ast_csr INTO l_release_asset_yn;
5494          FETCH l_chk_rel_ast_csr INTO l_new_asset_cnt;
5495              IF l_chk_rel_ast_csr%NOTFOUND THEN
5496                l_new_asset_cnt := 0;
5497                CLOSE l_chk_rel_ast_csr;
5498              END IF;
5499      -- Bug# 15992711 : End of modifications
5500      CLOSE l_chk_rel_ast_csr;
5501 
5502 
5503      IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5504         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '4..' || l_release_asset_yn);
5505      END IF;
5506 
5507      -- Bug# 15992711 : If new asset count is zero, then do not calculate upfront tax for contract
5508      --IF (l_release_asset_yn = 'Y') THEN
5509      --     NULL; -- Do not calculate upfront tax for contracts having re-leased assets
5510      IF NVL(l_new_asset_cnt,0) = 0 THEN
5511        NULL;
5512      ELSE
5513      -- Bug# 15992711 : End of modifications
5514 
5515       l_transaction_id := p_transaction_id;
5516 
5517       -- Create or update tax transaction
5518       -- In R12, we will no longer create Pre-Rebook
5519       -- TRX as we used to in previous release, OKL.H
5520       -- For Pre-Rebook ebTax calls, we will reuse Rebook
5521       -- TRX. Remove Pre-Rebook reference in the following
5522       -- Code segment
5523 
5524       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5525         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '5..' || p_transaction_type);
5526       END IF;
5527 
5528       IF (p_transaction_type = 'Pre-Booking') THEN
5529 
5530 /*        IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5531           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5532                                   G_MODULE,
5533                                   'OKL: Process Sales Tax : before populate_transaction ');
5534         END IF;
5535 */
5536          -- R12 Change - START
5537 
5538         l_contract_id := p_contract_id;
5539 
5540         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5541           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '6..');
5542         END IF;
5543 
5544         -- create booking transaction at 'Validation' time
5545         -- instead of 'Pre-Booking' transaction
5546         populate_transaction(
5547                       p_api_version      => p_api_version,
5548                       p_init_msg_list    => p_init_msg_list,
5549                       p_contract_id      => l_contract_id,
5550                       -- p_transaction_type => p_transaction_type, -- R12 Change
5551                       p_transaction_type => 'Booking', -- R12 Change
5552                       p_transaction_id   => p_transaction_id,
5553                       p_source_trx_id    => NULL,
5554                       p_source_trx_type  => NULL,
5555                       x_transaction_id   => l_transaction_id,
5556                       x_trxh_out_rec     => l_trxh_out_rec,
5557                       x_return_status    => x_return_status,
5558                       x_msg_count        => x_msg_count,
5559                       x_msg_data         => x_msg_data);
5560 
5561         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5562           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '7..' || x_return_status);
5563         END IF;
5564 
5565 --        okl_debug_pub.logmessage('OKL: process_sales_tax : populate_transaction : '|| x_return_status );
5566 
5567         -- check transaction creation was successful
5568         If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
5569           raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5570         ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
5571           raise OKL_API.G_EXCEPTION_ERROR;
5572         End If;
5573 
5574         -- R12 Change - END
5575       End If;
5576 
5577       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5578           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '8..');
5579       END IF;
5580 
5581       -- Process Pre Booking Tax Call
5582       If (p_transaction_type = 'Pre-Booking'
5583           OR
5584           p_transaction_type = 'Pre-Rebook')    -- Bug 4769822
5585       THEN
5586 
5587 /*        IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5588           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5589                                   G_MODULE,
5590                                   'OKL: Process Sales Tax : '||p_transaction_type||' before calculate_sales_tax ');
5591         END IF;
5592 */
5593         -- Call Tax API
5594 
5595         -- R12 Change - START
5596 
5597         IF (p_transaction_type = 'Pre-Booking')
5598         THEN
5599           l_contract_id      := p_contract_id;
5600           l_transaction_type := 'Booking';
5601         ELSIF (p_transaction_type = 'Pre-Rebook') THEN
5602           l_contract_id := p_rbk_contract_id;
5603           l_transaction_type := 'Rebook';
5604         End If;
5605 
5606         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5607           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '9..');
5608         END IF;
5609 
5610         -- R12 Change - END
5611 
5612        -- ER# 9327076 - Added condition - For rebook estimated tax call, estimate tax only if prior upfront tax present
5613 	   IF ((p_transaction_type = 'Pre-Booking')
5614 	        OR (p_transaction_type = 'Pre-Rebook' AND check_prior_upfront_tax(p_contract_id))
5615 	        OR (p_transaction_type = 'Pre-Rebook' AND check_rel_mxd_ctr(p_rbk_contract_id))) THEN  -- 16522428 14/03/13 - additional condition
5616 
5617         OKL_PROCESS_SALES_TAX_PUB.calculate_sales_tax(
5618                       p_api_version      => p_api_version,
5619                       p_init_msg_list    => p_init_msg_list,
5620                       x_return_status    => x_return_status,
5621                       x_msg_count        => x_msg_count,
5622                       x_msg_data         => x_msg_data,
5623                       p_source_trx_id    => l_transaction_id,
5624                       -- p_source_trx_name  => p_transaction_type, -- R12 Change
5625                       p_source_trx_name  => l_transaction_type, -- R12 Change
5626                       p_source_table     => l_source_table,
5627                       p_tax_call_type    => 'ESTIMATED');
5628 
5629         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5630           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '10..' || x_return_status);
5631         END IF;
5632 
5633 --        okl_debug_pub.logmessage('OKL: process_sales_tax : OKL_PROCESS_SALES_TAX_PUB.calculate_sales_tax: ESTIMATED : '|| x_return_status );
5634 
5635 /*        IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5636           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5637                                   G_MODULE,
5638                                   'OKL: Process Sales Tax...3'|| x_return_status);
5639         END IF;
5640         */
5641         -- Check if the tax call was successful
5642         If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
5643           raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5644         ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
5645           raise OKL_API.G_EXCEPTION_ERROR;
5646         End If;
5647 
5648 /*        IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5649           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5650                                   G_MODULE,
5651                                   'OKL: Process Sales Tax : '||p_transaction_type||' before process_prebook_tax ');
5652         END IF;
5653 */
5654         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5655           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '11..');
5656         END IF;
5657 
5658         process_prebook_tax(
5659                       p_api_version      => p_api_version,
5660                       p_init_msg_list    => p_init_msg_list,
5661                       p_contract_id      => l_contract_id,   -- Bug 4769822
5662                       p_transaction_id   => l_transaction_id,
5663                       -- p_transaction_type => p_transaction_type, -- R12 change
5664                       p_transaction_type => l_transaction_type, -- R12 change
5665                       x_return_status    => x_return_status,
5666                       x_msg_count        => x_msg_count,
5667                       x_msg_data         => x_msg_data
5668                           );
5669 
5670         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5671           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '12..' || x_return_status);
5672         END IF;
5673 
5674         --okl_debug_pub.logmessage('OKL: process_sales_tax : process_prebook_tax : '|| x_return_status );
5675 
5676         /*IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5677           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5678                                   G_MODULE,
5679                                   'OKL: Process Sales Tax : '||p_transaction_type||
5680                                   ' after process_prebook_tax: x_return_status' ||x_return_status);
5681         END IF;
5682 */
5683         -- Check if the call was successful
5684         -- Following code enabled for bug 5005269
5685 
5686         If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
5687           raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5688         ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
5689           raise OKL_API.G_EXCEPTION_ERROR;
5690         End If;
5691 
5692        END IF; -- ER# 9327076
5693 
5694       End If; -- Prebook
5695 
5696       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5697          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '13..');
5698       END IF;
5699 
5700       -- Process Booking Activation Tax Call
5701       If (p_transaction_type = 'Booking') THEN
5702 
5703    /*     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5704                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Process Sales Tax : '||p_transaction_type||' before process_booking_tax ');
5705         END IF;
5706 */
5707         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5708          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '14..');
5709         END IF;
5710 
5711         process_booking_tax(
5712                       p_api_version      => p_api_version,
5713                       p_init_msg_list    => p_init_msg_list,
5714                       p_contract_id      => p_contract_id,
5715                       p_transaction_id   => l_transaction_id,
5716                       p_transaction_type => p_transaction_type,
5717                       x_return_status    => x_return_status,
5718                       x_msg_count        => x_msg_count,
5719                       x_msg_data         => x_msg_data);
5720 
5721         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5722          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '15..' || x_return_status);
5723         END IF;
5724 
5725 --        okl_debug_pub.logmessage('OKL: process_sales_tax : process_booking_tax : '|| x_return_status );
5726         -- Check if the call was successful
5727         -- Bug 5002042
5728         If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
5729           raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5730         ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
5731           raise OKL_API.G_EXCEPTION_ERROR;
5732         End If;
5733 
5734       End If; -- Booking
5735 
5736       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5737          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '16..');
5738       END IF;
5739 
5740 
5741       -- Process Rebook Activation Tax Call
5742       IF (p_transaction_type = 'Rebook') THEN
5743 
5744 
5745   /*      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5746                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Process Sales Tax : '||p_transaction_type||' before process_rebook_tax ');
5747         END IF;
5748 */
5749         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5750           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '17..');
5751         END IF;
5752 
5753 		--ER# 9327076
5754 		IF (check_prior_upfront_tax(p_contract_id)
5755 		    OR check_prior_upfront_tax(p_rbk_contract_id)) THEN  -- -- 16522428 18-mar-13 - additional condition
5756 
5757         process_rebook_tax(
5758                       p_api_version      => p_api_version,
5759                       p_init_msg_list    => p_init_msg_list,
5760                       p_contract_id      => p_contract_id,
5761                       p_transaction_id   => p_transaction_id,
5762                       p_transaction_type => p_transaction_type,
5763                       p_transaction_date => p_transaction_date,
5764                       p_rbk_contract_id  => p_rbk_contract_id,
5765                       x_return_status    => x_return_status,
5766                       x_msg_count        => x_msg_count,
5767                       x_msg_data         => x_msg_data);
5768 
5769          IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5770            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '18..' || x_return_status);
5771          END IF;
5772 
5773 --        okl_debug_pub.logmessage('OKL: process_sales_tax : process_rebook_tax : '|| x_return_status );
5774 		END IF;
5775 
5776       End If; -- Rebook
5777 
5778       -- Process Mass Rebook trx
5779       -- R12 Change - Code segment to process 'Mass-Rebook'
5780       -- is removed from this location. It is not required as per IA.
5781       -- If required, take it from OKL.H code line
5782 
5783       -- R12 Change - Code segment to update transaction records to 'Processed'
5784       -- is removed from this location. It is not required as per IA.
5785       -- If required, take it from OKL.H code line
5786 
5787     END IF; -- Bug 6155565
5788    END IF;
5789 
5790    IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5791      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '19..');
5792    END IF;
5793 
5794     -- R12 Change - Start
5795 
5796 
5797 /*    IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5798       OKL_DEBUG_PUB.LOG_DEBUG(  FND_LOG.LEVEL_STATEMENT
5799                               , G_MODULE
5800                               ,'OKL: Process Sales Tax : Tax schedule csr ');
5801     END IF;
5802 */
5803     OPEN   rule_info_csr(p_contract_id);
5804     FETCH  rule_info_csr INTO rule_info_csr_rec;
5805     IF rule_info_csr%NOTFOUND THEN
5806 
5807       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5808        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '20..');
5809       END IF;
5810 
5811       -- R12B Authoring OA Migration - Corrected error message
5812       OKL_API.set_message( p_app_name     => G_APP_NAME,
5813                            p_msg_name     => 'OKL_QA_ST_MISSING');
5814       CLOSE rule_info_csr;
5815       RAISE Okl_Api.G_EXCEPTION_ERROR;
5816     END IF;
5817     CLOSE rule_info_csr;
5818 
5819     IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5820       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '21..');
5821     END IF;
5822 
5823     -- R12 Change - End
5824 
5825     -- Bug 4748910 - START
5826 
5827     -- Process Tax schedule
5828     IF (nvl(rule_info_csr_rec.rule_information5,'N') = 'Y'
5829         AND
5830         (p_transaction_type = 'Booking'
5831          OR
5832          p_transaction_type = 'Rebook')
5833        )
5834     THEN
5835 
5836 /*      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5837         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5838                                 G_MODULE,
5839                                 'OKL: Process Sales Tax : START tax schedule ');
5840       END IF;
5841 */
5842       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5843         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '22..');
5844       END IF;
5845 
5846       OPEN c_currency_code(p_contract_id);
5847       FETCH c_currency_code INTO l_start_date, l_end_date, l_currency_code;
5848       CLOSE c_currency_code;
5849 /*      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5850         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5851                                 G_MODULE,
5852                                 'OKL: Process Sales Tax : after currency code csr ');
5853       END IF;
5854 */
5855       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5856         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '23..');
5857       END IF;
5858 
5859       OPEN c_try_type(l_try_name);
5860       FETCH c_try_type INTO l_try_id;
5861       CLOSE c_try_type;
5862 
5863       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5864         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '24..');
5865       END IF;
5866 
5867 /*      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5868         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5869                                 G_MODULE,
5870                                 'OKL: Process Sales Tax : after try type csr');
5871       END IF;
5872 */
5873       l_trqv_tbl(1).request_status_code   := 'PROCESSED';
5874       l_trqv_tbl(1).request_type_code     := 'TAX_SCHEDULES';
5875       l_trqv_tbl(1).dnz_khr_id            := p_contract_id;
5876       l_trqv_tbl(1).currency_code         := l_currency_code;
5877       l_trqv_tbl(1).start_date            := l_start_date;
5878       l_trqv_tbl(1).end_date              := l_end_date;
5879       l_trqv_tbl(1).try_id                := l_try_id;
5880 
5881       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5882         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '25..');
5883       END IF;
5884 
5885  /*     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5886         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5887                                 G_MODULE,
5888                                 'OKL: Process Sales Tax : before creating trx requests');
5889       END IF;
5890 */
5891       okl_trx_requests_pub.insert_trx_requests(
5892                                              p_api_version         => p_api_version,
5893                                              p_init_msg_list       => p_init_msg_list,
5894                                              x_return_status       => x_return_status,
5895                                              x_msg_count           => x_msg_count,
5896                                              x_msg_data            => x_msg_data,
5897                                              p_trqv_tbl            => l_trqv_tbl,
5898                                              x_trqv_tbl            => x_trqv_tbl);
5899 
5900       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5901         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '26..' || x_return_status);
5902       END IF;
5903 
5904 ---      okl_debug_pub.logmessage('OKL: process_sales_tax : okl_trx_requests_pub.insert_trx_requests : '|| x_return_status );
5905 
5906 /*      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5907         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5908                                 G_MODULE,
5909                                 'OKL: Process Sales Tax : after creating trx requests: status-> '|| x_return_status);
5910       END IF;
5911 */
5912       If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
5913         raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5914       ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
5915         raise OKL_API.G_EXCEPTION_ERROR;
5916       End If;
5917 
5918 /*      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5919         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5920                                 G_MODULE,
5921                                 'OKL: Process Sales Tax : before OKL_PROCESS_SALES_TAX_PUB');
5922       END IF;
5923 */
5924       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5925         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '27..');
5926       END IF;
5927 
5928       OKL_PROCESS_SALES_TAX_PUB.calculate_sales_tax(
5929                       p_api_version      => p_api_version,
5930                       p_init_msg_list    => p_init_msg_list,
5931                       x_return_status    => x_return_status,
5932                       x_msg_count        => x_msg_count,
5933                       x_msg_data         => x_msg_data,
5934                       p_source_trx_id    => x_trqv_tbl(1).id,
5935                       p_source_trx_name  => l_try_name,
5936                       p_source_table     => 'OKL_TRX_REQUESTS',
5937                       p_tax_call_type    => NULL);
5938 
5939       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5940        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '28..' || x_return_status);
5941       END IF;
5942 
5943 --      okl_debug_pub.logmessage('OKL: process_sales_tax : after creating tax schedule : '|| x_return_status );
5944 /*
5945       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5946         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5947                                 G_MODULE,'OKL: Process Sales Tax : after OKL_PROCESS_SALES_TAX_PUB: status-> '|| x_return_status);
5948       END IF;
5949 */
5950       -- Check if the tax call was successful
5951       If (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) then
5952         raise OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
5953       ElSIF (x_return_status = OKL_API.G_RET_STS_ERROR) then
5954         raise OKL_API.G_EXCEPTION_ERROR;
5955       End If;
5956 
5957       -- R12 Change - Code segment to update transaction records to 'Processed'
5958       -- is removed from this location. It is not required as per IA.
5959       -- If required, take it from OKL.H code line
5960 
5961 /*      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5962         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT,
5963                                 G_MODULE,
5964                                 'OKL: Process Sales Tax : END tax schedule ');
5965       END IF;
5966 */
5967 
5968     End If; -- Tax Schedule
5969 
5970     IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5971        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'okl_la_sales_tax_pvt.process_sales_tax.', '29..');
5972     END IF;
5973 
5974     -- Bug 4748910 - END
5975 
5976     OKC_API.END_ACTIVITY (x_msg_count	=> x_msg_count,
5977                           x_msg_data	=> x_msg_data);
5978 
5979 /*    IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
5980           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'OKL: Process Sales Tax : END ');
5981     END IF;
5982 */
5983     IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5984        FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'okl_la_sales_tax_pvt.process_sales_tax.', '30..');
5985     END IF;
5986 
5987   --  okl_debug_pub.logmessage('OKL: process_sales_tax : END');
5988 
5989     Exception
5990       when OKL_API.G_EXCEPTION_ERROR then
5991 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
5992 			p_api_name  => l_api_name,
5993 			p_pkg_name  => g_pkg_name,
5994 			p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
5995 			x_msg_count => x_msg_count,
5996 			x_msg_data  => x_msg_data,
5997 			p_api_type  => g_api_type);
5998 
5999       when OKL_API.G_EXCEPTION_UNEXPECTED_ERROR then
6000 		x_return_status := OKL_API.HANDLE_EXCEPTIONS(
6001 			p_api_name  => l_api_name,
6002 			p_pkg_name  => g_pkg_name,
6003 			p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
6004 			x_msg_count => x_msg_count,
6005 			x_msg_data  => x_msg_data,
6006 			p_api_type  => g_api_type);
6007 
6008       when OTHERS then
6009       	x_return_status := OKL_API.HANDLE_EXCEPTIONS(
6010 			p_api_name  => l_api_name,
6011 			p_pkg_name  => g_pkg_name,
6012 			p_exc_name  => 'OTHERS',
6013 			x_msg_count => x_msg_count,
6014 			x_msg_data  => x_msg_data,
6015 			p_api_type  => g_api_type);
6016 
6017   END process_sales_tax;
6018 
6019 
6020 -- Start of comments
6021 --
6022 -- Procedure Name  : check_sales_tax
6023 -- Description     : Qa Checker Validation for sales tax.
6024 --                   Called by Qa Checker to check for header,
6025 --                   asset level rules and validates Sales Tax
6026 --                   Fee lines
6027 -- Business Rules  :
6028 -- Parameters      :
6029 -- Version         : 1.0, ramurt Created. Bug#4373029
6030 --                   23-Jun-10 sechawla 9841386 : Modified to remove the validation for
6031 --                   sales tax terms and conditions for released assets on a contract or
6032 --                   for re-leased contracts
6033 -- End of comments
6034 
6035   PROCEDURE check_sales_tax(
6036     p_chr_id                   IN  NUMBER,
6037     x_return_status            OUT NOCOPY VARCHAR2
6038   ) IS
6039 
6040   cursor fnd_csr( fndType VARCHAR2, fndCode VARCHAR2 ) IS
6041   Select meaning,
6042   description
6043   From  fnd_lookups
6044   Where lookup_type = fndType
6045   and lookup_code = fndCode;
6046 
6047   CURSOR l_hdr_csr IS
6048   SELECT authoring_org_id
6049   FROM   okc_k_headers_b
6050   WHERE  id = p_chr_id;
6051   l_authoring_org_id OKC_K_HEADERS_B.AUTHORING_ORG_ID%TYPE;
6052 
6053   cursor l_hdrrl_csr( rgcode OKC_RULE_GROUPS_B.RGD_CODE%TYPE,
6054                       rlcat  OKC_RULES_B.RULE_INFORMATION_CATEGORY%TYPE,
6055                       chrId NUMBER) IS
6056   select crl.object1_id1,
6057          crl.RULE_INFORMATION1,
6058          crl.RULE_INFORMATION2,
6059          crl.RULE_INFORMATION3,
6060          crl.RULE_INFORMATION4,
6061          crl.RULE_INFORMATION5,
6062          crl.RULE_INFORMATION6,
6063          crl.RULE_INFORMATION7,
6064          crl.RULE_INFORMATION10,
6065          crl.RULE_INFORMATION11
6066   from   OKC_RULE_GROUPS_B crg,
6067          OKC_RULES_B crl
6068   where  crl.rgp_id = crg.id
6069   and crg.RGD_CODE = rgcode
6070   and crl.RULE_INFORMATION_CATEGORY = rlcat
6071   and crg.dnz_chr_id = chrId;
6072 
6073 
6074   cursor l_rl_csr1( rgcode OKC_RULE_GROUPS_B.RGD_CODE%TYPE,
6075                     rlcat  OKC_RULES_B.RULE_INFORMATION_CATEGORY%TYPE,
6076                     chrId NUMBER,
6077                     cleId NUMBER ) IS
6078   select crl.id slh_id,
6079          crl.object1_id1,
6080          crl.RULE_INFORMATION1,
6081          crl.RULE_INFORMATION2,
6082          crl.RULE_INFORMATION3,
6083          crl.RULE_INFORMATION5,
6084          crl.RULE_INFORMATION6,
6085          crl.RULE_INFORMATION7,
6086          crl.RULE_INFORMATION8,
6087          crl.RULE_INFORMATION10
6088   from   OKC_RULE_GROUPS_B crg,
6089          OKC_RULES_B crl
6090   where  crl.rgp_id = crg.id
6091          and crg.RGD_CODE = rgcode
6092          and crl.RULE_INFORMATION_CATEGORY = rlcat
6093          and crg.dnz_chr_id = chrId
6094          and nvl(crg.cle_id,-1) = cleId
6095   order by crl.RULE_INFORMATION1;
6096 
6097   l_hdrrl_rec l_hdrrl_csr%ROWTYPE;
6098   l_exists boolean;
6099   l_pmnt_exists boolean;
6100   l_linked_asset_exists boolean;
6101   --l_tax_treatment  VARCHAR2(100);
6102   l_tax_header VARCHAR2(100);
6103   l_fnd_rec fnd_csr%ROWTYPE;
6104   l_line_fnd_rec fnd_csr%ROWTYPE;
6105   l_hdr_fnd_rec fnd_csr%ROWTYPE;
6106   l_fnd_meaning fnd_lookups.MEANING%TYPE;
6107   l_line_fnd_meaning fnd_lookups.MEANING%TYPE;
6108   l_hdr_fnd_meaning fnd_lookups.MEANING%TYPE;
6109   l_fee_type_meaning fnd_lookups.MEANING%TYPE;
6110   --l_tax_treatment_fee fnd_lookups.MEANING%TYPE;
6111   l_tax_fee_meaning fnd_lookups.MEANING%TYPE;
6112   l_st_fee_code fnd_lookups.LOOKUP_CODE%TYPE;
6113   l_count NUMBER;
6114   l_amt_link_asset NUMBER;
6115   l_no_link_assets NUMBER;
6116   l_bill_strm_type NUMBER;
6117   l_line_asset_upfront_tax VARCHAR2(150);
6118   l_prev_line_asset_upfront_tax VARCHAR2(150);
6119   l_fee_line_found BOOLEAN;
6120   l_found_asset_not_billed BOOLEAN;
6121   l_rl_rec l_rl_csr1%ROWTYPE;
6122 
6123   CURSOR st_fee_csr (p_chrId OKC_K_HEADERS_V.ID%TYPE) IS
6124   SELECT cleb.id,
6125          kle.fee_type,
6126          kle.amount
6127          --decode(kle.fee_type,'CAPITALIZED',kle.capital_amount,kle.amount) amount
6128   FROM
6129          okc_k_lines_b cleb,
6130          okc_k_lines_tl clet,
6131          okl_k_lines kle,
6132          okc_line_styles_v sty,
6133          okc_statuses_v sts
6134   WHERE  cleb.lse_id = sty.id
6135   AND    cleb.sts_code = sts.code
6136   AND    sts.ste_code not in ( 'HOLD', 'TERMINATED', 'EXPIRED', 'CANCELLED')
6137   AND    sty.lty_code = 'FEE'
6138   AND    kle.FEE_PURPOSE_CODE='SALESTAX'
6139   AND    cleb.dnz_chr_id = p_chrId
6140   AND    cleb.id = clet.id
6141   AND    clet.LANGUAGE = USERENV('LANG')
6142   AND    kle.id = cleb.id;
6143   l_st_fee_rec st_fee_csr%ROWTYPE;
6144   l_fee_amount okl_k_lines.amount%TYPE;
6145 
6146   CURSOR st_fee_link_asset_csr(p_cleId OKC_K_LINES_B.ID%TYPE) IS
6147   SELECT cleb.id,
6148          nvl(kle.amount,0) amount,
6149          nvl(kle.capital_amount,0) capital_amount
6150   FROM  okc_k_lines_b cleb,
6151         okl_k_lines kle,
6152         okc_line_styles_b sty
6153   WHERE cle_id = p_cleId
6154   AND   cleb.id = kle.id
6155   AND   cleb.lse_id = sty.id
6156   AND   sty.lty_code = 'LINK_FEE_ASSET';
6157 
6158   l_st_fee_link_asset_rec st_fee_link_asset_csr%ROWTYPE;
6159 
6160   CURSOR l_asset_tax_rule_csr(p_cleId OKC_K_LINES_B.ID%TYPE) IS
6161   SELECT crl.id slh_id,
6162          crl.object1_id1,
6163          crl.RULE_INFORMATION11
6164   FROM   OKC_RULE_GROUPS_B crg,
6165          OKC_RULES_B crl
6166   WHERE  crl.rgp_id = crg.id
6167   AND    crg.RGD_CODE = 'LAASTX'
6168   AND    crl.RULE_INFORMATION_CATEGORY = 'LAASTX'
6169   AND    crg.dnz_chr_id = p_chr_id
6170   AND    nvl(crg.cle_id,-1) = p_cleId;
6171   l_asset_tax_rule_rec l_asset_tax_rule_csr%ROWTYPE;
6172 
6173   CURSOR l_asset_csr IS
6174   SELECT cle.id,
6175          name
6176   FROM   okc_k_lines_v cle,
6177          okc_line_styles_b sty,
6178          okc_statuses_b sts
6179   WHERE  cle.lse_id = sty.id
6180   AND    cle.dnz_chr_id = p_chr_id
6181   AND    lty_code = 'FREE_FORM1'
6182   AND    cle.sts_code = sts.code
6183   AND    sts.ste_code not in ( 'HOLD', 'TERMINATED', 'EXPIRED', 'CANCELLED');
6184 
6185   --CURSOR l_fee_asset_csr(p_assetId OKC_K_LINES_B.ID%TYPE, p_fee_type OKL_K_LINES.FEE_TYPE%TYPE) IS
6186   CURSOR l_fee_asset_csr(p_assetId OKC_K_LINES_B.ID%TYPE) IS
6187   SELECT item.object1_id1,cleb.id,cleb.cle_id,
6188          clet_asset.name,
6189          kle.amount,
6190          kle.fee_type
6191   FROM   okc_k_lines_b fee_line,
6192          okc_k_lines_b cleb,
6193          okl_k_lines kle,
6194          okc_k_items item,
6195          okc_k_lines_tl clet_asset,
6196          okc_line_styles_b sty
6197   WHERE  fee_line.id = kle.id
6198   AND    kle.FEE_PURPOSE_CODE='SALESTAX'
6199   AND    cleb.lse_id = sty.id
6200   AND    sty.lty_code = 'LINK_FEE_ASSET'
6201   AND    item.cle_id = cleb.id
6202   AND    item.dnz_chr_id = p_chr_id
6203   AND    cleb.dnz_chr_id = p_chr_id
6204   AND    cleb.cle_id = fee_line.id
6205   AND    clet_asset.id = item.object1_id1
6206   AND    clet_asset.id = p_assetId
6207   AND    clet_asset.language =  USERENV('LANG');
6208   --AND    kle.fee_type = p_fee_type;
6209   --l_fee_asset_rec l_fee_asset_csr%ROWTYPE;
6210 
6211   CURSOR l_tax_amt_csr IS
6212   SELECT SUM(NVL(TOTAL_TAX,0))
6213   FROM   okl_tax_sources txs
6214               --,okl_tax_trx_details txl
6215   WHERE  txs.khr_id = p_chr_id
6216   --AND    txs.id = txl.txs_id
6217   AND    txs.tax_line_status_code = 'ACTIVE'
6218   AND    txs.tax_call_type_code = 'UPFRONT_TAX';
6219   l_tax_amount NUMBER;
6220 
6221   CURSOR l_tax_line_amt_csr(p_asset_id OKC_K_LINES_B.ID%TYPE) IS
6222   SELECT SUM(NVL(TOTAL_TAX,0)) tax_amount  -- SUM added for 4740150 issue
6223   FROM   okl_tax_sources txs
6224          --,okl_tax_trx_details txl
6225   WHERE  txs.khr_id = p_chr_id
6226  -- AND    txs.id = txl.txs_id
6227   AND    txs.tax_line_status_code = 'ACTIVE'
6228   AND    txs.tax_call_type_code = 'UPFRONT_TAX'
6229   AND    txs.kle_id = p_asset_id;
6230   l_asset_tax_amount NUMBER;
6231 
6232   -- R12B Authoring OA Migration
6233   CURSOR st_fee_count_csr (p_chr_id OKC_K_HEADERS_B.ID%TYPE) IS
6234   SELECT COUNT(cleb.id)
6235   FROM   okc_k_lines_b cleb,
6236          okl_k_lines kle,
6237          okc_line_styles_b sty,
6238          okc_statuses_b sts
6239   WHERE  cleb.lse_id = sty.id
6240   AND    sty.lty_code = 'FEE'
6241   AND    cleb.sts_code = sts.code
6242   AND    sts.ste_code not in ( 'HOLD', 'TERMINATED', 'EXPIRED', 'CANCELLED')
6243   AND    kle.fee_purpose_code = 'SALESTAX'
6244   AND    cleb.dnz_chr_id = p_chr_id
6245   AND    cleb.chr_id = p_chr_id
6246   AND    kle.id = cleb.id;
6247 
6248   -- ER# 9327076 - Cursor to check line level setup for upfront tax
6249   CURSOR chk_line_upfront_tax (p_chr_id OKC_K_HEADERS_B.ID%TYPE) IS
6250      SELECT '!'
6251      FROM   okc_rule_groups_b crg,
6252                  okc_rules_b crl
6253      WHERE  crl.rgp_id = crg.ID
6254        AND crg.rgd_code = 'LAASTX'
6255        AND crl.rule_information_category = 'LAASTX'
6256        AND crg.dnz_chr_id = p_chr_id
6257        AND crg.cle_id IS NOT NULL
6258 	   AND crl.rule_information11 IN ('BILLED', 'CAPITALIZE', 'FINANCE');
6259 
6260   l_chk_rbk VARCHAR2(1) DEFAULT '?';
6261   l_chk_mass_rbk VARCHAR2(1) DEFAULT '?';
6262   l_chk_line_upfront_tax VARCHAR2(1) DEFAULT '?';
6263 
6264 --23-Jun-10 sechawla 9841386 : added this cursor : begin
6265 --Bug# 15992711 : The re-leased flag is no longer checked at contract header
6266 --                but needs to be validated at contract line
6267 --                Cursor modified to include validation at asset line level
6268 /*CURSOR l_chk_rel_ast_csr (p_chr_id IN NUMBER) IS
6269     SELECT NVL(rule_information1,'N')
6270     FROM   okc_rules_b rul
6271     WHERE  rul.dnz_chr_id = p_chr_id
6272     AND    rul.rule_information_category = 'LARLES';*/
6273 
6274    CURSOR l_chk_rel_ast_csr (p_chr_id IN NUMBER) IS
6275     SELECT COUNT(KLE.id) new_asset_cnt
6276     FROM   okc_k_lines_b CLEB
6277           ,okl_k_lines   KLE
6278     WHERE  CLEB.id         = KLE.id
6279     AND    CLEB.dnz_chr_id = p_chr_id
6280     AND    CLEB.chr_id     = p_chr_id
6281     AND    CLEB.LSE_ID     = 33 --for asset line
6282     AND    NVL(KLE.re_lease_yn,'N')= 'N';
6283 
6284     l_new_asset_cnt   NUMBER :=0;
6285 
6286 -- New cursor to check if the asset is a released asset
6287 -- at the asset line level.
6288      CURSOR l_chk_rel_line_csr (p_kle_id IN OKC_K_LINES_V.ID%TYPE) IS
6289      SELECT NVL(kle_top.re_lease_yn,'N') release_flag
6290      FROM   okl_k_lines       kle_top
6291      WHERE  kle_top.id      = p_kle_id;
6292 -- Bug# 15992711 : End of Modifications
6293 
6294     l_release_asset_yn varchar2(1);
6295 --23-Jun-10 sechawla 9841386 : end
6296 
6297 BEGIN
6298   IF (G_DEBUG_ENABLED = 'Y') THEN
6299     G_IS_DEBUG_STATEMENT_ON := OKL_DEBUG_PUB.CHECK_LOG_ON(G_MODULE, FND_LOG.LEVEL_STATEMENT);
6300   END IF;
6301    x_return_status := OKL_API.G_RET_STS_SUCCESS;
6302 
6303    --OPEN l_hdr_csr;
6304    --FETCH l_hdr_csr INTO l_authoring_org_id;
6305    --CLOSE l_hdr_csr;
6306 
6307    --DBMS_APPLICATION_INFO.SET_CLIENT_INFO(l_authoring_org_id);
6308 
6309    l_upfront_tax_yn := 'N';
6310    OPEN l_tax_system_info;
6311    FETCH l_tax_system_info INTO l_upfront_tax_yn;
6312    CLOSE l_tax_system_info;
6313 
6314    -- ER# 9327076
6315    OPEN c_chk_rbk_csr(p_chr_id);
6316 	    FETCH c_chk_rbk_csr INTO l_chk_rbk;
6317 	CLOSE c_chk_rbk_csr;
6318 
6319     OPEN c_chk_mass_rbk_csr(p_chr_id);
6320 	     FETCH c_chk_mass_rbk_csr INTO l_chk_mass_rbk;
6321 	CLOSE c_chk_mass_rbk_csr;
6322 
6323 	--23-Jun-10 sechawla 9841386 : check if contract has any released assets.
6324     --This applies to both released assets on a contract and re-leased contracts : begin
6325     -- Bug# 15992711 : Change in call to the release asset new cursor
6326     -- It will now be called at the asset line level
6327     /*
6328     OPEN  l_chk_rel_ast_csr(p_chr_id);
6329     FETCH l_chk_rel_ast_csr INTO l_release_asset_yn;
6330     CLOSE l_chk_rel_ast_csr;
6331 
6332     IF (l_release_asset_yn = 'Y') THEN
6333         NULL; -- Do not trigger upfront tax validations for contracts having re-leased assets or for re-leased contracts
6334          -- since we do not calculate estimated or actual sales tax for such contracts, sales tax validations should
6335          -- also not be done for such contracts
6336 
6337 
6338     ELSE*/
6339 
6340     OPEN  l_chk_rel_ast_csr(p_chr_id);
6341          FETCH l_chk_rel_ast_csr INTO l_new_asset_cnt;
6342              IF l_chk_rel_ast_csr%NOTFOUND THEN
6343                l_new_asset_cnt := 0;
6344                CLOSE l_chk_rel_ast_csr;
6345              END IF;
6346     CLOSE l_chk_rel_ast_csr;
6347 
6348     IF NVL(l_new_asset_cnt,0) = 0 THEN
6349       NULL; -- Do not trigger upfront tax validations for contracts having re-leased assets or for re-leased contracts
6350          -- since we do not calculate estimated or actual sales tax for such contracts, sales tax validations should
6351          -- also not be done for such contracts
6352     ELSE
6353     -- Bug# 15992711 : End of Modifications
6354    --23-Jun-10 sechawla 9841386 : end
6355 
6356     IF (nvl(l_upfront_tax_yn,'N') <> 'Y') THEN
6357       -- ER# 9327076
6358 	  -- This check is only at time of booking - not at time of rebook or mass rebook
6359 	  IF (l_chk_rbk <> '!' AND l_chk_mass_rbk <> '!') THEN
6360          --Get Header level Upfront Tax T and Cs
6361 		 OPEN l_hdrrl_csr('LAHDTX','LASTPR', p_chr_id);
6362             FETCH l_hdrrl_csr INTO l_hdrrl_rec;
6363             l_exists := l_hdrrl_csr%FOUND;
6364          CLOSE l_hdrrl_csr;
6365 
6366 		 -- Get Line Level Upfront Tax T and Cs
6367 	     OPEN chk_line_upfront_tax(p_chr_id);
6368 		   FETCH chk_line_upfront_tax INTO l_chk_line_upfront_tax;
6369 		 CLOSE chk_line_upfront_tax;
6370 
6371 		 -- Ensure that none of tax related T and Cs are populated at header and line level
6372 		 IF (l_chk_line_upfront_tax='!' OR (l_exists AND (l_hdrrl_rec.RULE_INFORMATION1 IS NOT NULL
6373 		                                                              OR l_hdrrl_rec.RULE_INFORMATION2 IS NOT NULL
6374 		                                                             OR l_hdrrl_rec.RULE_INFORMATION3 IS NOT NULL
6375 																	 OR l_hdrrl_rec.RULE_INFORMATION4 IS NOT NULL))) THEN
6376              OKL_API.set_message(
6377               p_app_name     => G_APP_NAME,
6378               p_msg_name     => 'OKL_UPFRONT_TAX_SYSOP_CHK'); -- Please remove the terms and conditions for upfront tax as upfront tax calculation has not been enabled.
6379              x_return_status := OKL_API.G_RET_STS_ERROR;
6380              RAISE G_EXCEPTION_HALT_VALIDATION;
6381 		 END IF;
6382 	  END IF;
6383 
6384       x_return_status := OKL_API.G_RET_STS_SUCCESS;
6385       return;
6386    END IF;
6387 
6388    OPEN l_hdrrl_csr('LAHDTX','LASTPR', p_chr_id);
6389    FETCH l_hdrrl_csr INTO l_hdrrl_rec;
6390    l_exists := l_hdrrl_csr%FOUND;
6391    CLOSE l_hdrrl_csr;
6392    l_tax_header := l_hdrrl_rec.RULE_INFORMATION1;
6393    l_bill_strm_type := l_hdrrl_rec.RULE_INFORMATION2;
6394 
6395 
6396 
6397 
6398 
6399 
6400 
6401    -- Upfront tax related T and Cs to be checked only
6402    --   i. if present call if for contract being booked
6403    --  ii. In case of rebook or massrebook, this check should happen only if upfront tax had earlier been calculated
6404    IF ((l_chk_rbk <> '!' AND l_chk_mass_rbk <> '!')
6405          OR check_prior_upfront_tax(p_chr_id)
6406          OR check_rel_mxd_ctr(p_chr_id))THEN -- ER# 9327076 -- 16522428 14/03/13 - additional condition
6407    IF (l_tax_header IS NULL ) THEN
6408      OKL_API.set_message(
6409               p_app_name     => G_APP_NAME,
6410               p_msg_name     => 'OKL_QA_ST_HEADER_TAX');
6411      x_return_status := OKL_API.G_RET_STS_ERROR;
6412      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6413             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_HEADER_TAX The Upfront Sales Tax at Contract header can not be null.');
6414      END IF;
6415    END IF;
6416 
6417 
6418    IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6419         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'rule_info1'||l_hdrrl_rec.RULE_INFORMATION1);
6420      OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'rule_info2'||l_hdrrl_rec.RULE_INFORMATION2);
6421      OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'rule_info3'||l_hdrrl_rec.RULE_INFORMATION3);
6422      OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'rule_info4'||l_hdrrl_rec.RULE_INFORMATION4);
6423    END IF;
6424 
6425    --IF (NOT l_exists OR l_tax_treatment IS NULL) THEN
6426    IF (NOT l_exists ) THEN
6427      OKL_API.set_message(
6428               p_app_name     => G_APP_NAME,
6429               p_msg_name     => 'OKL_QA_ST_MISSING');
6430      x_return_status := OKL_API.G_RET_STS_ERROR;
6431      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6432             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_MISSING');
6433      END IF;
6434    END IF;
6435 
6436 
6437    IF ( l_hdrrl_rec.RULE_INFORMATION2 IS NULL ) THEN
6438      OPEN fnd_csr('OKL_ASSET_UPFRONT_TAX','BILLED');
6439      FETCH fnd_csr INTO l_fnd_rec;
6440      l_fnd_meaning := l_fnd_rec.meaning;
6441      CLOSE fnd_csr;
6442      OKL_API.set_message(
6443               p_app_name     => G_APP_NAME,
6444               p_msg_name     => 'OKL_QA_ST_STREAM_MISSING',
6445               p_token1       => 'UPFRONT_TAX',
6446               p_token1_value => l_fnd_meaning);
6447      x_return_status := OKL_API.G_RET_STS_ERROR;
6448      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6449             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_STREAM_MISSING '||'Please enter '
6450                              ||l_fnd_meaning ||' stream type on Taxes (Sales Tax Processing)'
6451                              ||' terms and condition for the contract.');
6452      END IF;
6453    END IF;
6454    IF ( l_hdrrl_rec.RULE_INFORMATION3 IS NULL ) THEN
6455      OPEN fnd_csr('OKL_ASSET_UPFRONT_TAX','FINANCE');
6456      FETCH fnd_csr INTO l_fnd_rec;
6457      l_fnd_meaning := l_fnd_rec.meaning;
6458      CLOSE fnd_csr;
6459      OKL_API.set_message(
6460               p_app_name     => G_APP_NAME,
6461               p_msg_name     => 'OKL_QA_ST_STREAM_MISSING',
6462               p_token1       => 'UPFRONT_TAX',
6463               p_token1_value => l_fnd_meaning);
6464      x_return_status := OKL_API.G_RET_STS_ERROR;
6465      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6466             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_STREAM_MISSING '||'Please enter '
6467                              ||l_fnd_meaning ||' stream type on Taxes (Sales Tax Processing)'
6468                              ||' terms and condition for the contract.');
6469      END IF;
6470    END IF;
6471    IF ( l_hdrrl_rec.RULE_INFORMATION4 IS NULL ) THEN
6472      OPEN fnd_csr('OKL_ASSET_UPFRONT_TAX','CAPITALIZE');
6473      FETCH fnd_csr INTO l_fnd_rec;
6474      l_fnd_meaning := l_fnd_rec.meaning;
6475      CLOSE fnd_csr;
6476      OKL_API.set_message(
6477               p_app_name     => G_APP_NAME,
6478               p_msg_name     => 'OKL_QA_ST_STREAM_MISSING',
6479               p_token1       => 'UPFRONT_TAX',
6480               p_token1_value => l_fnd_meaning);
6481      x_return_status := OKL_API.G_RET_STS_ERROR;
6482      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6483             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_STREAM_MISSING '||'Please enter '
6484                              ||l_fnd_meaning ||' stream type on Taxes (Sales Tax Processing)'
6485                              ||' terms and condition for the contract.');
6486      END IF;
6487    END IF;
6488    END IF; -- ER# 9327076
6489 
6490   IF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6491     RAISE G_EXCEPTION_HALT_VALIDATION;
6492   END IF;
6493 
6494   -- R12B Authoring OA Migration
6495   /*
6496   l_count := 0;
6497   l_fee_line_found := false;
6498   FOR l_st_fee_rec IN st_fee_csr(p_chr_id)
6499   LOOP
6500    l_count := l_count + 1;
6501    OPEN fnd_csr('OKL_FEE_TYPES',l_st_fee_rec.fee_type);
6502    FETCH fnd_csr INTO l_fnd_rec;
6503    l_fee_type_meaning := l_fnd_rec.meaning;
6504    CLOSE fnd_csr;
6505 
6506    IF ( l_st_fee_rec.fee_type = 'FINANCED' )
6507        OR ( l_st_fee_rec.fee_type = 'CAPITALIZED' ) THEN
6508        l_fee_line_found := TRUE;
6509    END IF;
6510    l_fee_amount := l_st_fee_rec.amount;
6511 
6512    IF ( l_st_fee_rec.amount = 0 ) THEN
6513      OKL_API.set_message(
6514               p_app_name     => G_APP_NAME,
6515               p_msg_name     => 'OKL_QA_ST_FEE_ZERO',
6516               p_token1       => 'FEE_TYPE',
6517               p_token1_value => l_fee_type_meaning);
6518      x_return_status := OKL_API.G_RET_STS_ERROR;
6519      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6520             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_FEE_ZERO '||'The sales tax fee line (type='
6521                ||l_fee_type_meaning||') amount can not be zero or negative.');
6522      END IF;
6523    END IF;
6524 
6525    IF ( l_st_fee_rec.fee_type = 'FINANCED')
6526       OR ( l_st_fee_rec.fee_type = 'CAPITALIZED') THEN
6527      l_amt_link_asset := 0;
6528      l_no_link_assets := 0;
6529      FOR l_st_fee_link_asset_rec IN st_fee_link_asset_csr(l_st_fee_rec.id)
6530      LOOP
6531        IF (l_st_fee_rec.fee_type = 'FINANCED') THEN
6532          l_amt_link_asset := l_amt_link_asset + l_st_fee_link_asset_rec.amount;
6533        ELSIF (l_st_fee_rec.fee_type = 'CAPITALIZED') THEN
6534          l_amt_link_asset := l_amt_link_asset + l_st_fee_link_asset_rec.capital_amount;
6535        END IF;
6536        l_no_link_assets := l_no_link_assets + 1;
6537      END LOOP; --l_st_fee_link_asset_rec
6538      IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6539             OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,l_st_fee_rec.amount || ':' || l_amt_link_asset);
6540      END IF;
6541      IF (l_st_fee_rec.amount <> l_amt_link_asset) THEN
6542        OKL_API.set_message(
6543                 p_app_name     => G_APP_NAME,
6544                 p_msg_name     => 'OKL_QA_ST_FEE_ASSET_AMT',
6545                 p_token1       => 'LINE_TYPE',
6546                 p_token1_value => l_fee_type_meaning);
6547        x_return_status := OKL_API.G_RET_STS_ERROR;
6548        IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6549                 OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error: OKL_QA_ST_FEE_ASSET_AMT '||'The sales tax Fee line ('
6550           ||l_fee_type_meaning||') amount must be equal to the total amount on corresponding linked Fee asset(s).');
6551        END IF;
6552      END IF;
6553    END IF;
6554   END LOOP;
6555   */
6556   -- R12B Authoring OA Migration
6557 
6558   -- R12B Authoring OA Migration
6559   OPEN st_fee_count_csr(p_chr_id => p_chr_id);
6560   FETCH st_fee_count_csr INTO l_count;
6561   CLOSE st_fee_count_csr;
6562 
6563   --IF ( (l_tax_treatment IN ('FINANCE','CAPITALIZE') )  AND l_count > 1 ) THEN
6564   IF ( l_count > 1 ) THEN
6565     OKL_API.set_message(
6566               p_app_name     => G_APP_NAME,
6567               p_msg_name     => 'OKL_QA_ST_FEE_GT1');
6568     x_return_status := OKL_API.G_RET_STS_ERROR;
6569     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6570           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_FEE_GT1 ' || 'The contract can not have more than one Sales Tax Fee line defined.');
6571     END IF;
6572   END IF;
6573 
6574 
6575   --raise exception as we want to halt validation at this.
6576   IF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6577     RAISE G_EXCEPTION_HALT_VALIDATION;
6578   END IF;
6579 
6580   l_line_asset_upfront_tax := 'XXX';
6581   l_prev_line_asset_upfront_tax := 'XXX';
6582   l_found_asset_not_billed := false;
6583   FOR l_asset_rec IN l_asset_csr
6584   LOOP
6585     -- Bug# 15992711 : Start of Modifications
6586     -- Checking if the asset line is a release asset line
6587     -- if yes the validation should be skipped else should proceed
6588         OPEN l_chk_rel_line_csr(l_asset_rec.Id);
6589         FETCH l_chk_rel_line_csr INTO l_release_asset_yn;
6590         CLOSE l_chk_rel_line_csr;
6591 
6592     IF (l_release_asset_yn = 'Y') THEN
6593       NULL;
6594     ELSE
6595     -- Bug# 15992711 : End of Modifications
6596 
6597     OPEN l_asset_tax_rule_csr(l_asset_rec.Id);
6598     FETCH l_asset_tax_rule_csr INTO l_asset_tax_rule_rec;
6599     CLOSE l_asset_tax_rule_csr;
6600     IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6601           OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'l_asset_rec.id: '||l_asset_rec.id || ' ' || l_asset_rec.name);
6602     END IF;
6603     l_line_asset_upfront_tax := nvl(l_asset_tax_rule_rec.RULE_INFORMATION11,'XXX');
6604 
6605     IF (l_line_asset_upfront_tax <> 'BILLED' AND l_line_asset_upfront_tax <> 'XXX') THEN
6606       l_found_asset_not_billed := TRUE;
6607     END IF;
6608 
6609     --raise error capitalized and finance mix not allowed. For sales tax purposes all non billed asset
6610     --lines should be either capitalized or financed throughout.
6611       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6612               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'l_line_asset_upfront_tax:'||l_line_asset_upfront_tax);
6613         OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'l_prev_line_asset_upfront_tax:'||l_prev_line_asset_upfront_tax);
6614       END IF;
6615     IF (l_line_asset_upfront_tax IN ('FINANCE','CAPITALIZE')
6616          AND l_line_asset_upfront_tax <> l_prev_line_asset_upfront_tax) THEN
6617       IF (l_prev_line_asset_upfront_tax <> 'XXX') THEN
6618         OKL_API.set_message(
6619                 p_app_name     => G_APP_NAME,
6620                 p_msg_name     => 'OKL_QA_ST_ALL_CAP_OR_FIN');
6621         x_return_status := OKL_API.G_RET_STS_ERROR;
6622         IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6623                   OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_ALL_CAP_OR_FIN All asset lines must have sales upfront tax as either Financed or Capitalized.');
6624         END IF;
6625         EXIT;
6626         --RAISE G_EXCEPTION_HALT_VALIDATION;
6627       END IF;
6628       l_prev_line_asset_upfront_tax := l_line_asset_upfront_tax;
6629     END IF;
6630 
6631     IF ((l_line_asset_upfront_tax <> l_tax_header)
6632            AND l_line_asset_upfront_tax IN ('FINANCE','CAPITALIZE')
6633            AND l_tax_header IN ('FINANCE','CAPITALIZE')) THEN
6634       --raise error capitalized and financed mix not allowed.
6635       OPEN fnd_csr('OKL_ASSET_UPFRONT_TAX',l_tax_header);
6636       FETCH fnd_csr INTO l_hdr_fnd_rec;
6637       l_hdr_fnd_meaning := l_hdr_fnd_rec.meaning;
6638       CLOSE fnd_csr;
6639       OPEN fnd_csr('OKL_ASSET_UPFRONT_TAX',l_line_asset_upfront_tax);
6640       FETCH fnd_csr INTO l_line_fnd_rec;
6641       l_line_fnd_meaning := l_line_fnd_rec.meaning;
6642       CLOSE fnd_csr;
6643       OKL_API.set_message(
6644             p_app_name     => G_APP_NAME,
6645             p_msg_name     => 'OKL_QA_ST_CAP_FIN_MIX',
6646             p_token1       => 'UPFRONT_TAX',
6647             p_token1_value => l_hdr_fnd_meaning,
6648             p_token2       => 'UPFRONT_LN_TAX',
6649             p_token2_value => l_line_fnd_meaning,
6650             p_token3       => 'ASSET_NUMBER',
6651             p_token3_value => l_asset_rec.name);
6652       x_return_status := OKL_API.G_RET_STS_ERROR;
6653       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6654               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_CAP_FIN_MIX The upfront tax for the asset line ' ||
6655                          l_asset_rec.name||' is different from asset upfront tax at Contract header.' );
6656       END IF;
6657       null;
6658     END IF;
6659 
6660     -- R12B Authoring OA Migration
6661     /*
6662     IF (l_line_asset_upfront_tax = 'FINANCE') THEN
6663       l_st_fee_code := 'FINANCED';
6664     END IF;
6665     IF (l_line_asset_upfront_tax = 'CAPITALIZE') THEN
6666       l_st_fee_code := 'CAPITALIZED';
6667     END IF;
6668 
6669     --debug_message('l_bill_asset_overide: '||l_bill_asset_overide);
6670     l_linked_asset_exists := FALSE;
6671     FOR l_fee_asset_rec IN l_fee_asset_csr(l_asset_rec.id)
6672     LOOP
6673 
6674       OPEN fnd_csr('OKL_FEE_TYPES',l_fee_asset_rec.fee_type);
6675       FETCH fnd_csr INTO l_fnd_rec;
6676       l_fee_type_meaning := l_fnd_rec.meaning;
6677       CLOSE fnd_csr;
6678 
6679       IF (l_line_asset_upfront_tax IN ('FINANCE', 'CAPITALIZE')) THEN
6680         l_linked_asset_exists := TRUE;
6681       END IF;
6682 
6683       --IF (l_line_asset_upfront_tax = 'BILLED' AND l_linked_asset_exists) THEN
6684       IF (l_line_asset_upfront_tax = 'BILLED' ) THEN
6685          OPEN fnd_csr('OKL_FEE_TYPES','BILLED');
6686          FETCH fnd_csr INTO l_fnd_rec;
6687          CLOSE fnd_csr;
6688          l_tax_fee_meaning := l_fnd_rec.meaning;
6689         OKL_API.set_message(
6690               p_app_name     => G_APP_NAME,
6691               p_msg_name     => 'OKL_QA_ST_FEE_ASSET_ERROR',
6692               p_token1       => 'FEE_TYPE',
6693               p_token1_value => l_tax_fee_meaning,
6694               p_token2       => 'ASSET_NUMBER',
6695               p_token2_value => l_asset_rec.name);
6696          x_return_status := OKL_API.G_RET_STS_ERROR;
6697          IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6698                     OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error:  '||'OKL_QA_ST_FEE_ASSET_ERROR '||'The "Bill Upfront Tax" is selected for the asset = '
6699              ||l_asset_rec.name||'. Please remove this asset association from Sales Tax Fee line ('||l_tax_fee_meaning||').');
6700          END IF;
6701          null;
6702        END IF;
6703     END LOOP;
6704 
6705     --raise exception as we want to halt validation at this.
6706     IF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
6707       RAISE G_EXCEPTION_HALT_VALIDATION;
6708     END IF;
6709 
6710     IF ( (l_line_asset_upfront_tax IN ('FINANCE','CAPITALIZE') ) AND (NOT l_linked_asset_exists ) AND l_fee_line_found)THEN
6711       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6712               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Here00001' ||l_line_asset_upfront_tax );
6713       END IF;
6714       OPEN fnd_csr('OKL_FEE_TYPES',l_st_fee_code);
6715       FETCH fnd_csr INTO l_fnd_rec;
6716       CLOSE fnd_csr;
6717       l_tax_fee_meaning := l_fnd_rec.meaning;
6718       OKL_API.set_message(
6719                 p_app_name     => G_APP_NAME,
6720                 p_msg_name     => 'OKL_QA_ST_FEE_ASSET_MISSING',
6721                 p_token1       => 'ASSET_NUMBER',
6722                 p_token1_value => l_asset_rec.name,
6723                 p_token2       => 'LINE_TYPE',
6724                 p_token2_value => l_tax_fee_meaning );
6725       x_return_status := OKL_API.G_RET_STS_ERROR;
6726       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6727               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error: OKL_QA_ST_FEE_ASSET_MISSING '||'The sales tax Fee line ('
6728             ||l_tax_fee_meaning ||') does not have corresponding linked asset('||l_asset_rec.name||') defined.');
6729       END IF;
6730     END IF;
6731     */
6732     -- R12B Authoring OA Migration
6733     -- Bug# 15992711 : Start of Modifications
6734     END IF;
6735     -- Bug# 15992711 : End of Modifications
6736 
6737   END LOOP;
6738 
6739   -- R12B Authoring OA Migration
6740   /*
6741   --Bug#4693357 ramurt
6742   l_tax_amount := 0;
6743   OPEN l_tax_amt_csr;
6744   FETCH l_tax_amt_csr INTO l_tax_amount;
6745   CLOSE l_tax_amt_csr;
6746 
6747   IF ( (l_tax_amount <> 0) AND NOT l_fee_line_found AND l_found_asset_not_billed) THEN
6748       OKL_API.set_message(
6749                 p_app_name     => G_APP_NAME,
6750                 p_msg_name     => 'OKL_QA_ST_NO_FEE');
6751       x_return_status := OKL_API.G_RET_STS_ERROR;
6752       IF (G_IS_DEBUG_STATEMENT_ON = TRUE) THEN
6753               OKL_DEBUG_PUB.LOG_DEBUG(FND_LOG.LEVEL_STATEMENT, G_MODULE,'Error: OKL_QA_ST_NO_FEE '||'The sales tax Fee line must be present for the Contract');
6754       END IF;
6755   END IF;
6756 
6757   --Bug#4699379 ramurt
6758   -- getting the total tax engine calculated sales upfront tax amount by summing calculated amounts
6759   -- for each asset.
6760   l_tax_amount := 0;
6761   l_asset_tax_amount := 0;
6762   FOR l_asset_rec IN l_asset_csr
6763   LOOP
6764     OPEN l_asset_tax_rule_csr(l_asset_rec.Id);
6765     FETCH l_asset_tax_rule_csr INTO l_asset_tax_rule_rec;
6766     CLOSE l_asset_tax_rule_csr;
6767     l_line_asset_upfront_tax := nvl(l_asset_tax_rule_rec.RULE_INFORMATION11,'XXX');
6768 
6769     IF (l_line_asset_upfront_tax IN ('FINANCE','CAPITALIZE')) THEN
6770       OPEN  l_tax_line_amt_csr(l_asset_rec.id);
6771       FETCH l_tax_line_amt_csr INTO l_asset_tax_amount;
6772       l_tax_amount := l_tax_amount + l_asset_tax_amount;
6773       CLOSE l_tax_line_amt_csr;
6774     END IF;
6775   END LOOP;
6776 
6777   OPEN fnd_csr('OKL_FEE_TYPES',l_st_fee_code);
6778   FETCH fnd_csr INTO l_fnd_rec;
6779   CLOSE fnd_csr;
6780   l_tax_fee_meaning := l_fnd_rec.meaning;
6781 
6782   IF (l_tax_amount <> l_fee_amount) THEN
6783     OKL_API.set_message(
6784               p_app_name     => G_APP_NAME,
6785               p_msg_name     => 'OKL_QA_ST_FEE_AMT',
6786               p_token1       => 'FEE_TYPE',
6787               p_token1_value => l_tax_fee_meaning,
6788               p_token2       => 'UPFRONT_TAX',
6789               p_token2_value => l_tax_fee_meaning);
6790     x_return_status := OKL_API.G_RET_STS_ERROR;
6791   END IF;
6792   */
6793   -- R12B Authoring OA Migration
6794 
6795  END IF; --23-Jun-10 sechawla 9841386
6796 
6797 EXCEPTION
6798   WHEN G_EXCEPTION_HALT_VALIDATION THEN
6799     -- no processing necessary; validation can continue with next column
6800     IF st_fee_csr%ISOPEN THEN
6801       CLOSE st_fee_csr;
6802     END IF;
6803     IF l_hdrrl_csr%ISOPEN THEN
6804       CLOSE l_hdrrl_csr;
6805     END IF;
6806     IF l_rl_csr1%ISOPEN THEN
6807       CLOSE l_rl_csr1;
6808     END IF;
6809     IF l_asset_tax_rule_csr%ISOPEN THEN
6810       CLOSE l_asset_tax_rule_csr;
6811     END IF;
6812     IF fnd_csr%ISOPEN THEN
6813       CLOSE fnd_csr;
6814     END IF;
6815     IF l_hdr_csr%ISOPEN THEN
6816       CLOSE l_hdr_csr;
6817     END IF;
6818     IF l_fee_asset_csr%ISOPEN THEN
6819       CLOSE l_fee_asset_csr;
6820     END IF;
6821     IF l_asset_csr%ISOPEN THEN
6822       CLOSE l_asset_csr;
6823     END IF;
6824     IF st_fee_link_asset_csr%ISOPEN THEN
6825       CLOSE st_fee_link_asset_csr;
6826     END IF;
6827     IF l_tax_system_info%ISOPEN THEN
6828       CLOSE l_tax_system_info;
6829     END IF;
6830     IF l_tax_amt_csr%ISOPEN THEN
6831       CLOSE l_tax_amt_csr;
6832     END IF;
6833     IF l_tax_line_amt_csr%ISOPEN THEN
6834       CLOSE l_tax_line_amt_csr;
6835     END IF;
6836     NULL;
6837   WHEN OTHERS THEN
6838     -- store SQL error message on message stack
6839     OKL_API.SET_MESSAGE(
6840       p_app_name        => G_APP_NAME,
6841       p_msg_name        => G_UNEXPECTED_ERROR,
6842       p_token1	        => G_SQLCODE_TOKEN,
6843       p_token1_value    => SQLCODE,
6844       p_token2          => G_SQLERRM_TOKEN,
6845       p_token2_value    => SQLERRM);
6846     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6847     IF st_fee_csr%ISOPEN THEN
6848       CLOSE st_fee_csr;
6849     END IF;
6850     IF l_hdrrl_csr%ISOPEN THEN
6851       CLOSE l_hdrrl_csr;
6852     END IF;
6853     IF l_rl_csr1%ISOPEN THEN
6854       CLOSE l_rl_csr1;
6855     END IF;
6856     IF l_asset_tax_rule_csr%ISOPEN THEN
6857       CLOSE l_asset_tax_rule_csr;
6858     END IF;
6859     IF fnd_csr%ISOPEN THEN
6860       CLOSE fnd_csr;
6861     END IF;
6862     IF l_hdr_csr%ISOPEN THEN
6863       CLOSE l_hdr_csr;
6864     END IF;
6865     IF l_fee_asset_csr%ISOPEN THEN
6866       CLOSE l_fee_asset_csr;
6867     END IF;
6868     IF l_asset_csr%ISOPEN THEN
6869       CLOSE l_asset_csr;
6870     END IF;
6871     IF st_fee_link_asset_csr%ISOPEN THEN
6872       CLOSE st_fee_link_asset_csr;
6873     END IF;
6874     IF l_tax_system_info%ISOPEN THEN
6875       CLOSE l_tax_system_info;
6876     END IF;
6877     IF l_tax_amt_csr%ISOPEN THEN
6878       CLOSE l_tax_amt_csr;
6879     END IF;
6880     IF l_tax_line_amt_csr%ISOPEN THEN
6881       CLOSE l_tax_line_amt_csr;
6882     END IF;
6883 
6884 END check_sales_tax;
6885 
6886 -- Start of comments
6887 --
6888 -- Procedure Name  : check_sales_tax_asset_rules
6889 -- Description     : Page level Validation for sales tax
6890 --                   terms and conditions at asset level
6891 --                   This is called by process_line_rule_group_rules
6892 --                   in okl_rgrp_rules_process_pvt
6893 -- Business Rules  :
6894 -- Parameters      :
6895 -- Version         : 1.0, ramurt Created. Bug#4658944
6896 -- End of comments
6897 PROCEDURE check_sales_tax_asset_rules(
6898                      p_api_version       IN NUMBER,
6899                      p_init_msg_list     IN  VARCHAR2 DEFAULT OKC_API.G_FALSE,
6900                      x_return_status     OUT NOCOPY VARCHAR2,
6901                      x_msg_count         OUT NOCOPY NUMBER,
6902                      x_msg_data          OUT NOCOPY VARCHAR2,
6903                      p_chr_id            IN NUMBER,
6904                      p_line_id           IN NUMBER,
6905                      p_rule_group_id     IN NUMBER,
6906                      p_rgr_rec           IN rgr_rec_type) IS
6907 
6908   l_exists boolean;
6909   l_mix_fin_cap VARCHAR2(1);
6910 
6911   -- Bug# 6512668: Exclude asset lines in Abandoned status
6912   CURSOR l_upfront_tax_other_assets(p_line_upfront_tax OKC_RULES_B.RULE_INFORMATION11%TYPE) IS
6913   SELECT 'Y'
6914   FROM   okc_rule_groups_b rgp,
6915          okc_rules_b rule,
6916          okc_k_lines_b cle
6917   WHERE  rule.rgp_id = rgp.id
6918   AND    rgd_code = 'LAASTX'
6919   AND    rule_information_category = 'LAASTX'
6920   AND    rgp.dnz_chr_id = p_chr_id
6921   AND    nvl(rgp.cle_id,-1) <>  p_line_id
6922   AND    nvl(RULE_INFORMATION11,'XXX') NOT IN (p_line_upfront_tax,'BILLED','XXX')
6923   AND    cle.id = rgp.cle_id
6924   AND    cle.sts_code <> 'ABANDONED';
6925 
6926 BEGIN
6927    x_return_status := OKL_API.G_RET_STS_SUCCESS;
6928 
6929    l_upfront_tax_yn := 'N';
6930    OPEN l_tax_system_info;
6931    FETCH l_tax_system_info INTO l_upfront_tax_yn;
6932    CLOSE l_tax_system_info;
6933 
6934    IF (nvl(l_upfront_tax_yn,'N') <> 'Y') THEN
6935      x_return_status := OKL_API.G_RET_STS_SUCCESS;
6936      return;
6937    END IF;
6938 
6939    --Murthy
6940    l_exists := false;
6941    IF (p_rgr_rec.RULE_INFORMATION11 = 'CAPITALIZE') THEN
6942      OPEN l_upfront_tax_other_assets('CAPITALIZE');
6943      FETCH l_upfront_tax_other_assets INTO l_mix_fin_cap;
6944      l_exists := l_upfront_tax_other_assets%FOUND;
6945      CLOSE l_upfront_tax_other_assets;
6946    ELSIF (p_rgr_rec.RULE_INFORMATION11 = 'FINANCE') THEN
6947      OPEN l_upfront_tax_other_assets('FINANCE');
6948      FETCH l_upfront_tax_other_assets INTO l_mix_fin_cap;
6949      l_exists := l_upfront_tax_other_assets%FOUND;
6950      CLOSE l_upfront_tax_other_assets;
6951    END IF;
6952    IF (l_exists) THEN
6953        OKL_API.set_message(
6954                 p_app_name     => G_APP_NAME,
6955                 p_msg_name     => 'OKL_QA_ST_ALL_CAP_OR_FIN');
6956        x_return_status := OKL_API.G_RET_STS_ERROR;
6957    END IF;
6958 
6959   EXCEPTION
6960   WHEN G_EXCEPTION_HALT_VALIDATION THEN
6961     -- no processing necessary; validation can continue with next column
6962     IF l_upfront_tax_other_assets%ISOPEN THEN
6963       CLOSE l_upfront_tax_other_assets;
6964     END IF;
6965     IF l_tax_system_info%ISOPEN THEN
6966       CLOSE l_tax_system_info;
6967     END IF;
6968   WHEN OTHERS THEN
6969     IF l_upfront_tax_other_assets%ISOPEN THEN
6970       CLOSE l_upfront_tax_other_assets;
6971     END IF;
6972     IF l_tax_system_info%ISOPEN THEN
6973       CLOSE l_tax_system_info;
6974     END IF;
6975     -- store SQL error message on message stack
6976     OKL_API.SET_MESSAGE(
6977       p_app_name        => G_APP_NAME,
6978       p_msg_name        => G_UNEXPECTED_ERROR,
6979       p_token1	        => G_SQLCODE_TOKEN,
6980       p_token1_value    => SQLCODE,
6981       p_token2          => G_SQLERRM_TOKEN,
6982       p_token2_value    => SQLERRM);
6983     x_return_status := OKL_API.G_RET_STS_UNEXP_ERROR;
6984 
6985 END check_sales_tax_asset_rules;
6986 
6987 -----------------------------------------------------------------------------
6988  -- PROCEDURE validate_upfront_tax_fee
6989  -----------------------------------------------------------------------------
6990  -- Start of comments
6991  --
6992  -- Procedure Name  : validate_upfront_tax_fee
6993  -- Description     : Procedure will be called to validate upfront tax fee and
6994  --                   payments during online and batch contract activation.
6995  -- Business Rules  :
6996  -- Parameters      : p_chr_id
6997  -- Version         : 1.0
6998  -- History         : 24-Apr-2007 rpillay Created
6999  -- End of comments
7000 
7001   PROCEDURE validate_upfront_tax_fee(
7002             p_api_version     IN  NUMBER,
7003             p_init_msg_list   IN  VARCHAR2 DEFAULT OKL_API.G_FALSE,
7004             x_return_status   OUT NOCOPY VARCHAR2,
7005             x_msg_count       OUT NOCOPY NUMBER,
7006             x_msg_data        OUT NOCOPY VARCHAR2,
7007             p_chr_id          IN  VARCHAR2) IS
7008 
7009     l_api_name	CONSTANT VARCHAR2(30) := 'VALIDATE_UPFRONT_TAX_FEE';
7010     l_api_version	CONSTANT NUMBER	      := 1.0;
7011 
7012     /* Cursor to get the top fee line for Sales Tax financed fee of a given contract. */
7013     CURSOR l_fn_top_fee_ln_csr (p_chr_id IN OKC_K_HEADERS_B.ID%TYPE) IS
7014     SELECT kle.id,
7015            kle.amount,
7016            clev.name,
7017            lsev.name line_type,
7018            --Bug# 12807203
7019            kle.fee_type
7020     FROM okc_k_lines_v clev,
7021          okl_k_lines kle,
7022          okc_statuses_b okcsts,
7023          okc_line_styles_v lsev
7024     WHERE clev.dnz_chr_id = p_chr_id
7025     AND clev.chr_id = p_chr_id
7026     AND kle.id = clev.id
7027     --Bug# 12807203
7028     AND kle.fee_type IN ('FINANCED','CAPITALIZED')
7029     AND kle.fee_purpose_code = 'SALESTAX'
7030     AND lsev.id = clev.lse_id
7031     AND okcsts.code = clev.sts_code
7032     AND okcsts.ste_code NOT IN ('EXPIRED','HOLD','CANCELLED','TERMINATED', 'ABANDONED');
7033 
7034     /* Cursor to get the financed fee sub-lines (applied to assets) for a
7035        given contract. */
7036     CURSOR l_fn_sub_ln_fee_csr ( p_chr_id IN OKC_K_HEADERS_B.ID%TYPE,
7037                                  p_fee_line_id IN OKL_K_LINES.ID%TYPE) IS
7038     SELECT kle.id,
7039            kle.amount,
7040            cleb.end_date,
7041            cleb.start_date
7042     FROM okc_k_lines_b cleb,
7043          okl_k_lines kle,
7044          okc_statuses_b okcsts
7045     WHERE cleb.dnz_chr_id = p_chr_id
7046     AND kle.id = cleb.id
7047     AND cleb.cle_id = p_fee_line_id
7048     AND okcsts.code = cleb.sts_code
7049     AND okcsts.ste_code NOT IN ('EXPIRED','HOLD','CANCELLED','ABANDONED');
7050 
7051     /* Cursor to get the financed fee top/sub-line payments for a
7052        given contract. */
7053     CURSOR l_fn_fee_pmt_csr (p_chr_id IN OKC_K_HEADERS_B.ID%TYPE,
7054                              p_fee_line_id IN OKL_K_LINES.ID%TYPE) IS
7055     SELECT Fnd_Date.canonical_to_date(sll.rule_information2) start_date,
7056            SLL.rule_information3 periods,
7057            DECODE(sll.object1_id1, 'M', 1, 'Q', 3, 'S', 6, 'A', 12) mpp,
7058            TRUNC(ADD_MONTHS(Fnd_Date.canonical_to_date(sll.rule_information2),
7059            TO_NUMBER(sll.rule_information3)*DECODE(sll.object1_id1, 'M', 1, 'Q', 3, 'S', 6, 'A', 12)) - 1) end_date,
7060            styt.name stream_type
7061     FROM   okc_rules_b sll,
7062            okc_rules_b slh,
7063            okc_rule_groups_b rgp,
7064            okl_strm_type_b sty,
7065            okl_strm_type_tl styt
7066     WHERE  rgp.dnz_chr_id              = p_chr_id
7067     AND  rgp.cle_id                    = p_fee_line_id
7068     AND  rgp.rgd_code                  = 'LALEVL'
7069     AND  rgp.id                        = slh.rgp_id
7070     AND  slh.rule_information_category = 'LASLH'
7071     AND  slh.object1_id1               = TO_CHAR(sty.id)
7072     AND  styt.language                 = USERENV('LANG')
7073     AND  sty.id                        = styt.id
7074     AND  TO_CHAR(slh.id)               = sll.object2_id1
7075     AND  sll.rule_information_category = 'LASLL';
7076 
7077     /* Cursor to get the financed fee top/sub-line payments HEADER for a
7078        given contract. */
7079     CURSOR l_fn_fee_pmtH_csr (p_chr_id IN OKC_K_HEADERS_B.ID%TYPE,
7080                               p_fee_line_id IN OKL_K_LINES.ID%TYPE) IS
7081 
7082     SELECT slh.id
7083     FROM   okc_rules_b slh,
7084            okc_rule_groups_b rgp,
7085            okl_strm_type_b sty,
7086            okl_strm_type_tl styt
7087     WHERE  rgp.dnz_chr_id              = p_chr_id
7088     AND  rgp.cle_id                    = p_fee_line_id
7089     AND  rgp.rgd_code                  = 'LALEVL'
7090     AND  rgp.id                        = slh.rgp_id
7091     AND  slh.rule_information_category = 'LASLH'
7092     AND  slh.object1_id1               = TO_CHAR(sty.id)
7093     AND  styt.language                 = USERENV('LANG')
7094     AND  sty.id                        = styt.id;
7095 
7096     CURSOR l_strm_slh_csr (p_khr_id OKC_K_HEADERS_B.ID%TYPE,
7097                            p_kle_id OKC_K_LINES_B.ID%TYPE) IS
7098     SELECT styt.name stream_type,
7099            rule.id rule_id,
7100            rgp.id rgp_id
7101     FROM   okc_rules_b rule,
7102            okc_rule_groups_b rgp,
7103            okl_strm_type_b sty,
7104            okl_strm_type_tl styt
7105     WHERE  NVL(rgp.cle_id, -1)            = p_kle_id
7106     AND    rgp.dnz_chr_id                 = p_khr_id
7107     AND    rgp.rgd_code                   = 'LALEVL'
7108     AND    rgp.id                         = rule.rgp_id
7109     AND    rule.rule_information_category = 'LASLH'
7110     AND    TO_NUMBER(rule.object1_id1)    = sty.id
7111     AND    styt.LANGUAGE                  = USERENV('LANG')
7112     AND    sty.id                         = styt.id;
7113 
7114     CURSOR l_strm_sll_csr (p_rule_id OKC_RULES_B.ID%TYPE,
7115                            p_rgp_id  OKC_RULE_GROUPS_B.ID%TYPE) IS
7116     SELECT Fnd_Date.canonical_to_date(sll.rule_information2) start_date,
7117            sll.rule_information1 seq,
7118            sll.rule_information6 amt,
7119            sll.rule_information7 stub_day,
7120            sll.rule_information13 rate
7121     FROM   okc_rules_b sll
7122     WHERE  sll.rgp_id                    = p_rgp_id
7123     AND    sll.object2_id1               = TO_CHAR(p_rule_id)
7124     AND    sll.rule_information_category = 'LASLL'
7125     ORDER BY 1,2;
7126 
7127     l_prev_pmnt NUMBER;
7128 
7129 
7130     x_err_msg VARCHAR2(1000);
7131 
7132     l_fn_sub_ln_fee_cnt   NUMBER := 0;
7133     l_payment_top_ln_cnt  NUMBER := 0; -- Counter for financed payments top lines
7134     l_payment_sub_ln_cnt  NUMBER := 0; -- Counter for financed payments sub-lines
7135     l_fn_top_ln_fee_amt   NUMBER := 0; -- Financed top line total fee amount.
7136     l_fn_sub_ln_fee_amt   NUMBER := 0; -- Financed sum of sub-line fee amount.
7137     l_top_ln_pmt_exist    BOOLEAN := FALSE;
7138     l_fn_sln_fee_amt_chk  BOOLEAN := FALSE; -- If there are sub-lines payments exists.
7139 
7140     l_fn_top_fee_ln_rec   l_fn_top_fee_ln_csr%ROWTYPE;
7141     l_fn_fee_pmtH_rec     l_fn_fee_pmtH_csr%ROWTYPE;
7142 
7143     --Bug# 6609598
7144     l_tot_pmnt NUMBER;
7145 
7146     --Bug# 12807203
7147     l_return_status      VARCHAR2(1) := Okl_Api.G_RET_STS_SUCCESS;
7148 
7149   BEGIN
7150 
7151     x_return_status := OKL_API.START_ACTIVITY(
7152                         p_api_name      => l_api_name,
7153                         p_pkg_name      => g_pkg_name,
7154                         p_init_msg_list => p_init_msg_list,
7155                         l_api_version   => l_api_version,
7156                         p_api_version   => p_api_version,
7157                         p_api_type      => G_API_TYPE,
7158                         x_return_status => x_return_status);
7159 
7160     -- check if activity started successfully
7161     IF (x_return_status = OKL_API.G_RET_STS_UNEXP_ERROR) THEN
7162        RAISE OKL_API.G_EXCEPTION_UNEXPECTED_ERROR;
7163     ELSIF (x_return_status = OKL_API.G_RET_STS_ERROR) THEN
7164        RAISE OKL_API.G_EXCEPTION_ERROR;
7165     END IF;
7166 
7167     OPEN l_fn_top_fee_ln_csr(p_chr_id => p_chr_id);
7168     FETCH l_fn_top_fee_ln_csr INTO l_fn_top_fee_ln_rec;
7169     CLOSE l_fn_top_fee_ln_csr;
7170 
7171     --Bug# 12807203
7172     -- If Sales Tax FINANCED or CAPITALIZED fee is present, then validate that Asset Financed Cost
7173     -- has not been modified during an effective dated rebook when rebook effective date
7174     -- is not the contract line start date
7175     IF (l_fn_top_fee_ln_rec.id IS NOT NULL) THEN
7176       OKL_QA_DATA_INTEGRITY.check_rbk_financed_cost(
7177         x_return_status      => l_return_status,
7178         p_chr_id             => p_chr_id,
7179         p_upfront_tax_fee_yn => 'Y');
7180 
7181       IF l_return_status <> Okl_Api.G_RET_STS_SUCCESS THEN
7182         x_return_status := l_return_status;
7183       END IF;
7184     END IF;
7185 
7186     -- If Sales tax Financed Fee exists
7187     --Bug# 12807203
7188     IF (l_fn_top_fee_ln_rec.id IS NOT NULL) AND (l_fn_top_fee_ln_rec.fee_type = 'FINANCED') THEN
7189 
7190       /* Check Payments for the top fee line if they exist. */
7191       --Bug# 6609598
7192       /* FEE line should have only 1 payment defined. */
7193       l_tot_pmnt := 0;
7194       FOR l_fn_fee_pmtH_rec IN  l_fn_fee_pmtH_csr(p_chr_id      => p_chr_id,
7195                                                    p_fee_line_id => l_fn_top_fee_ln_rec.id)
7196       LOOP
7197         l_tot_pmnt := l_tot_pmnt + 1;
7198         l_top_ln_pmt_exist := TRUE;
7199       END LOOP;
7200 
7201       IF ( l_tot_pmnt > 1 ) THEN
7202         x_return_status := Okl_Api.G_RET_STS_ERROR;
7203 	  OKL_API.set_message(
7204                   p_app_name     => G_APP_NAME,
7205                   p_msg_name     => 'OKL_QA_ONLY_1PAY',
7206                   p_token1       => 'line',
7207                   p_token1_value => l_fn_top_fee_ln_rec.name
7208                  );
7209       END IF;
7210 
7211       /* Check payments for financed fee sub-line, if it exist both for top
7212          line and sub-line error out. If payment end date is past fee line end date error out. */
7213 
7214       FOR l_fn_sub_ln_fee_rec IN l_fn_sub_ln_fee_csr (p_chr_id => p_chr_id,
7215                                                       p_fee_line_id => l_fn_top_fee_ln_rec.id)
7216       LOOP
7217 
7218         l_fn_sub_ln_fee_cnt := l_fn_sub_ln_fee_cnt + 1;
7219 
7220         /* Store the sum of financed fee sub-line amount. */
7221         l_fn_sub_ln_fee_amt := l_fn_sub_ln_fee_amt + l_fn_sub_ln_fee_rec.amount;
7222 
7223         /* Sub-line fees exists. */
7224         l_fn_sln_fee_amt_chk := TRUE;
7225 
7226         /* Check Payments for the financed fee sub-line if they exist. */
7227 
7228 	  FOR l_fn_fee_pmtH IN l_fn_fee_pmtH_csr (p_chr_id => p_chr_id,
7229                                                 p_fee_line_id => l_fn_sub_ln_fee_rec.id )
7230 	  LOOP
7231 	    IF (l_top_ln_pmt_exist) THEN
7232             x_return_status := Okl_Api.G_RET_STS_ERROR;
7233 	      OKL_API.set_message(
7234 	        p_app_name     => G_APP_NAME,
7235 	  	  p_msg_name     => 'OKL_QA_FN_PMTS_TOP_SUB_EXIST',
7236 	        p_token1       => 'FEE_LINE',
7237 	        p_token1_value => l_fn_top_fee_ln_rec.name);
7238 	    END IF;
7239 
7240 	    l_payment_sub_ln_cnt := l_payment_sub_ln_cnt + 1;
7241 
7242 	  END LOOP;
7243 
7244 	  FOR l_fn_fee_pmt_rec IN l_fn_fee_pmt_csr (p_chr_id => p_chr_id,
7245                                                   p_fee_line_id => l_fn_sub_ln_fee_rec.id)
7246 	  LOOP
7247 	    /* Check if the payment end date is within in the financed
7248 	       fee sub-line's end date, if not error out. */
7249 
7250           IF ( TRUNC(l_fn_fee_pmt_rec.start_date) < TRUNC(l_fn_sub_ln_fee_rec.start_date) ) THEN
7251 
7252             x_return_status := Okl_Api.G_RET_STS_ERROR;
7253 	      OKL_API.set_message(
7254 	        p_app_name     => G_APP_NAME,
7255 	        p_msg_name     => 'OKL_QA_FN_SLN_PMT_SD',
7256 	        p_token1       => 'FEE_LINE',
7257 	        p_token1_value => l_fn_top_fee_ln_rec.name);
7258 	    END IF;
7259 
7260           IF ( TRUNC(l_fn_fee_pmt_rec.end_date) > TRUNC(l_fn_sub_ln_fee_rec.end_date) ) THEN
7261             x_return_status := Okl_Api.G_RET_STS_ERROR;
7262 	      OKL_API.set_message(
7263 	        p_app_name     => G_APP_NAME,
7264 	  	  p_msg_name     => 'OKL_QA_FN_SLN_PMT_ED',
7265 	        p_token1       => 'FEE_LINE',
7266 	        p_token1_value => l_fn_top_fee_ln_rec.name);
7267 	    END IF;
7268 
7269 	  END LOOP;
7270       END LOOP;
7271 
7272       /* Check if a financed top line fee amount is not equal to sub-line fee amount,
7273          if exists, if not error out. */
7274 
7275       IF ( (l_fn_sln_fee_amt_chk) AND (l_fn_top_fee_ln_rec.amount <> l_fn_sub_ln_fee_amt)) THEN
7276         x_return_status := Okl_Api.G_RET_STS_ERROR;
7277 	  OKL_API.set_message(
7278 	    p_app_name     => G_APP_NAME,
7279 	    p_msg_name     => 'OKL_QA_FN_FEE_AMT_NEQ',
7280 	    p_token1       => 'FEE_LINE',
7281 	    p_token1_value => l_fn_top_fee_ln_rec.name);
7282       END IF;
7283 
7284       /* If no payments are defiend for the fee line then error out. */
7285 
7286       IF ((NOT l_top_ln_pmt_exist) AND (NOT l_fn_sln_fee_amt_chk)) THEN
7287         x_return_status := Okl_Api.G_RET_STS_ERROR;
7288 	  OKL_API.set_message(
7289 	    p_app_name     => G_APP_NAME,
7290 	    p_msg_name     => 'OKL_QA_RQ_NO_PMTS',
7291 	    p_token1       => 'FEE_LINE',
7292 	    p_token1_value => l_fn_top_fee_ln_rec.name);
7293       END IF;
7294 
7295       /* Check if a payment is defined for EACH of the financed fee sub-lines HEADER,
7296          if not error out. If there are multiple payments defiend for a financed
7297          fee sub-line HEADER error out. */
7298 
7299       IF ((NOT l_top_ln_pmt_exist) AND (l_fn_sub_ln_fee_cnt > l_payment_sub_ln_cnt)) THEN
7300         x_return_status := Okl_Api.G_RET_STS_ERROR;
7301         OKL_API.set_message(
7302 	    p_app_name     => G_APP_NAME,
7303 	    p_msg_name     => 'OKL_QA_FN_PMTS_MISS_SLN',
7304 	    p_token1       => 'FEE_LINE',
7305 	    p_token1_value => l_fn_top_fee_ln_rec.name);
7306       ELSIF ((NOT l_top_ln_pmt_exist) AND (l_fn_sub_ln_fee_cnt <  l_payment_sub_ln_cnt)) THEN
7307         x_return_status := Okl_Api.G_RET_STS_ERROR;
7308 	  OKL_API.set_message(
7309 	    p_app_name     => G_APP_NAME,
7310 	    p_msg_name     => 'OKL_QA_FN_MUL_SLN_PMTS',
7311 	    p_token1       => 'FEE_LINE',
7312 	    p_token1_value => l_fn_top_fee_ln_rec.name);
7313       END IF;
7314 
7315 
7316       FOR l_strm_slh_rec IN l_strm_slh_csr (p_chr_id,
7317                                             l_fn_top_fee_ln_rec.id)
7318       LOOP
7319         l_prev_pmnt := NULL;
7320         FOR l_strm_sll_rec IN l_strm_sll_csr (l_strm_slh_rec.rule_id,
7321                                               l_strm_slh_rec.rgp_id)
7322         LOOP
7323           IF (l_strm_sll_rec.stub_day IS NOT NULL) THEN -- do not check
7324             l_prev_pmnt := NULL; -- reset
7325           ELSE
7326             -- Check payment amount here
7327             IF (l_prev_pmnt = TO_NUMBER(NVL(l_strm_sll_rec.amt,'0'))) THEN
7328               -- Error
7329               Okl_Api.set_message(
7330                 G_APP_NAME,
7331                 'OKL_QA_INVALID_PMNT',
7332                 'LINE_TYPE',
7333                 l_fn_top_fee_ln_rec.line_type,
7334                 'PMNT_TYPE',
7335                 l_strm_slh_rec.stream_type
7336               );
7337               x_return_status := Okl_Api.G_RET_STS_ERROR;
7338             ELSE
7339               l_prev_pmnt := TO_NUMBER(NVL(l_strm_sll_rec.amt,'0'));
7340             END IF; --check
7341 
7342           END IF; --stub
7343         END LOOP; --l_strm_sll_csr
7344       END LOOP; --l_strm_slh_csr
7345 
7346       --Bug# 12807203
7347       -- If Sales Tax FINANCED fee is present, then validate that Payment Frequency, Structure
7348       -- or Arrears option for the fee payment have not been modified during an effective
7349       -- dated rebook when rebook effective date is not the contract line start date
7350       OKL_QA_DATA_INTEGRITY.check_rbk_pymt_structure(
7351         x_return_status      => l_return_status,
7352         p_chr_id             => p_chr_id,
7353         p_upfront_tax_fee_yn => 'Y');
7354 
7355       IF l_return_status <> Okl_Api.G_RET_STS_SUCCESS THEN
7356         x_return_status := l_return_status;
7357       END IF;
7358 
7359     END IF;
7360 
7361     OKL_API.END_ACTIVITY(x_msg_count => x_msg_count, x_msg_data  => x_msg_data);
7362 
7363   EXCEPTION
7364     WHEN OKL_API.G_EXCEPTION_ERROR THEN
7365       x_return_status := OKL_API.HANDLE_EXCEPTIONS(
7366                         p_api_name  => l_api_name,
7367                         p_pkg_name  => g_pkg_name,
7368                         p_exc_name  => 'OKL_API.G_RET_STS_ERROR',
7369                         x_msg_count => x_msg_count,
7370                         x_msg_data  => x_msg_data,
7371                         p_api_type  => g_api_type);
7372 
7373      WHEN OKL_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
7374         x_return_status := OKL_API.HANDLE_EXCEPTIONS(
7375                         p_api_name  => l_api_name,
7376                         p_pkg_name  => g_pkg_name,
7377                         p_exc_name  => 'OKL_API.G_RET_STS_UNEXP_ERROR',
7378                         x_msg_count => x_msg_count,
7379                         x_msg_data  => x_msg_data,
7380                         p_api_type  => g_api_type);
7381 
7382      WHEN OTHERS THEN
7383         x_return_status := OKL_API.HANDLE_EXCEPTIONS(
7384                         p_api_name  => l_api_name,
7385                         p_pkg_name  => g_pkg_name,
7386                         p_exc_name  => 'OTHERS',
7387                         x_msg_count => x_msg_count,
7388                         x_msg_data  => x_msg_data,
7389                         p_api_type  => g_api_type);
7390 
7391  END validate_upfront_tax_fee;
7392 
7393 END OKL_LA_SALES_TAX_PVT;