DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKS_BILL_REC_PUB

Source


1 PACKAGE BODY OKS_BILL_REC_PUB AS
2  /* $Header: OKSPBRCB.pls 120.79.12020000.6 2013/04/16 15:53:11 mchandak ship $ */
3 
4 
5 --------------------------------------------
6 -- Global Variable to store terminated amount
7 --------------------------------------------
8 g_bcl_id        NUMBER;
9 g_bsl_id        NUMBER;
10 g_credit_amount NUMBER;
11 G_MODULE_CURRENT   CONSTANT VARCHAR2(255) := 'oks.plsql.oks_bill_rec_pub';
12 ------------------------------------------------------------------------
13 G_LOG_YES_NO Varchar2(10); --added by ansraj for 14016663
14 ------------------------------------------------------------------------
15 -- Begin partial period computation logic
16 -- Developer Mani Choudhary
17 -- Date 27-MAY-2005
18 -- DESCRIPTION:
19 -- This procedure will calculate the credit amount used for previewing
20 -- if the period start and period type are not null.
21 -------------------------------------------------------------------------
22 Procedure  Terminate_PPC
23 (
24                            P_called_from          IN NUMBER DEFAULT NULL,
25                            P_end_date             IN DATE,
26                            P_termination_date     IN DATE,
27                            P_top_line_id          IN NUMBER,
28                            P_cp_line_id           IN NUMBER,
29                            P_suppress_credit      IN VARCHAR2,
30                            P_period_type          IN VARCHAR2,
31                            P_period_start         IN VARCHAR2,
32                            P_override_amount      IN NUMBER,
33                            P_con_terminate_amount IN NUMBER,
34                            X_return_status        OUT NOCOPY VARCHAR2
35 );
36 ------------------------------------------------------------------------
37 -- End partial period computation logic
38 -------------------------------------------------------------------------
39 
40 ------------------------------------------------------------------------
41 -- Begin partial period computation logic
42 -- Developer Mani Choudhary
43 -- Date 27-MAY-2005
44 -- DESCRIPTION:
45 -- This procedure will calculate the credit amount used for previewing
46 -- if the period start and period type are not null.
47 -------------------------------------------------------------------------
48 Procedure get_term_amt_ppc
49 (
50 P_called_from IN NUMBER DEFAULT NULL,
51 P_line_id IN NUMBER ,
52 P_cov_line IN VARCHAR2,
53 P_termination_date IN DATE,
54 P_period_type   VARCHAR2,
55 P_period_start  VARCHAR2,
56 X_amount OUT NOCOPY NUMBER,
57 X_return_status OUT NOCOPY VARCHAR2
58 );
59 ------------------------------------------------------------------------
60 -- End partial period computation logic
61 -------------------------------------------------------------------------
62 
63 ------------------------------------------------------------------------
64 -- Begin partial period computation logic
65 -- Developer Mani Choudhary
66 -- Date 27-MAY-2005
67 -- DESCRIPTION:
68 -- This new procedure will calculate the partial termination amount between
69 -- termination date and the billing period end using Time measure APIs for USAGE only.
70 ---------------------------------------------------------------------------
71 Function Get_partial_term_amount
72                        (
73                        p_start_date        IN DATE,
74                        p_end_date          IN DATE,
75                        P_termination_date  IN DATE,
76                        P_amount            IN NUMBER,
77                        P_uom               IN VARCHAR2,
78                        P_period_start      IN VARCHAR2,
79                        P_period_type       IN VARCHAR2
80                        )
81 RETURN NUMBER;
82 
83 
84 ------------------------------------------------------------------------
85   -- FUNCTION to get message
86 ------------------------------------------------------------------------
87 
88 PROCEDURE get_message(l_msg_cnt  IN            NUMBER,
89                       l_msg_data IN OUT NOCOPY VARCHAR) IS
90 l_msg_index_out    NUMBER;
91 BEGIN
92   IF (l_msg_cnt > 0)      THEN
93     FOR i in 1..l_msg_cnt LOOP
94       FND_MSG_PUB.GET
95         (p_msg_index     => -1,
96          p_encoded       => 'F',
97          p_data          => l_msg_data,
98          p_msg_index_out => l_msg_index_out);
99 
100        FND_FILE.PUT_LINE(FND_FILE.LOG,'Error Message = '||l_msg_data);
101      END LOOP;
102    END IF;
103 END get_message;
104 
105 ------------------------------------------------------------------------
106   -- FUNCTION to generate id
107 ------------------------------------------------------------------------
108 
109 FUNCTION get_seq_id RETURN NUMBER IS
110 BEGIN
111   RETURN(okc_p_util.raw_to_number(sys_guid()));
112 END get_seq_id;
113 
114 
115 ------------------------------------------------------------------------
116   -- FUNCTION to set top line for billing report
117 ------------------------------------------------------------------------
118  PROCEDURE Set_Top_line(
119  P_PROCESSED_LINES_TBL          IN OUT    NOCOPY LINE_REPORT_TBL_TYPE,
120  P_PROCESSED_SUB_LINES_TBL      IN OUT    NOCOPY LINE_REPORT_TBL_TYPE,
121  P_ERROR_MESSAGE                IN               VARCHAR2,
122  P_TOP_LINE                     IN               NUMBER) IS
123  BEGIN
124   FOR i in P_PROCESSED_LINES_TBL.FIRST..P_PROCESSED_LINES_TBL.LAST
125   LOOP
126     IF (P_PROCESSED_LINES_TBL(i).Line_id = p_top_line) THEN
127       P_PROCESSED_LINES_TBL(i).Billed_YN := 'N';
128       P_PROCESSED_LINES_TBL(i).Error_Message := P_ERROR_MESSAGE;
129     END IF;
130   END LOOP;
131  END Set_top_line;
132 
133 ------------------------------------------------------------------------
134   -- FUNCTION to set sub line for billing report
135 ------------------------------------------------------------------------
136 PROCEDURE Set_sub_line(
137  P_PROCESSED_LINES_TBL          IN OUT NOCOPY    LINE_REPORT_TBL_TYPE,
138  P_PROCESSED_SUB_LINES_TBL      IN OUT NOCOPY    LINE_REPORT_TBL_TYPE,
139  P_ERROR_MESSAGE                IN               VARCHAR2,
140  P_TOP_LINE                     IN               NUMBER) IS
141  BEGIN
142    FOR i in P_PROCESSED_SUB_LINES_TBL.FIRST..P_PROCESSED_SUB_LINES_TBL.LAST
143    LOOP
144      IF (P_PROCESSED_SUB_LINES_TBL(i).Line_id = p_top_line) THEN
145        P_PROCESSED_SUB_LINES_TBL(i).Billed_YN := 'N';
146        P_PROCESSED_SUB_LINES_TBL(i).Error_Message := P_ERROR_MESSAGE;
147      END IF;
148    END LOOP;
149  END Set_sub_line;
150 
151 
152 
153  ------------------------------------------------------------------------
154   -- FUNCTION get_term_end_date
155 ------------------------------------------------------------------------
156 
157 FUNCTION get_term_end_date (
158         p_cle_id           IN NUMBER,
159         p_termination_date IN DATE
160   ) RETURN Date IS
161    Cursor check_hold(p_cle_id IN NUMBER) Is
162      SELECT hold_billing
163           FROM oks_k_headers_b   hdr,
164                okc_k_lines_b     line
165           WHERE line.id = p_cle_id
166           AND   line.dnz_chr_id = hdr.chr_id;
167 
168 
169   Cursor Termination_csr(p_cle_id NUMBER,p_termination_date DATE) IS
170     SELECT lvl.date_end FROM oks_level_elements lvl
171       WHERE lvl.cle_id = p_cle_id
172       AND   trunc(p_termination_date ) BETWEEN trunc(lvl.date_start) AND
173                                                     trunc(lvl.date_end);
174 
175 l_ret_date   DATE;
176 l_hold       VARCHAR2(10);
177 BEGIN
178   OPEN  check_hold(p_cle_id);
179   FETCH check_hold into l_hold;
180   CLOSE check_hold;
181   IF (nvl(l_hold,'N') = 'Y') THEN
182     OPEN  Termination_csr( p_cle_id,p_termination_date);
183     FETCH Termination_csr into l_ret_date;
184     -- BUG FIX 3450592 .  L_rel_date is populated with null
185     IF (Termination_csr%NOTFOUND) THEN
186       l_ret_date := NULL ;     --sysdate;
187     END IF;
188     CLOSE Termination_csr;
189   ELSE
190     l_ret_date := NULL ;       --sysdate;
191   END IF;
192   Return (l_ret_date);
193 END get_term_end_date;
194 
195 -------------------------------------------------------------------------
196 -- Begin partial period computation logic
197 -- Developer Mani Choudhary
198 -- Date 11-JUN-2005
199 -- Added period start and period type parameters
200 -------------------------------------------------------------------------
201 PROCEDURE Create_trx_records(
202                              p_called_from          IN   NUMBER   DEFAULT Null ,
203                              p_top_line_id          IN   NUMBER,
204                              p_cov_line_id          IN   NUMBER,
205                              p_date_from            IN   DATE,
206                              p_date_to              IN   DATE,
207                              p_amount               IN   NUMBER,
208                              p_override_amount      IN   NUMBER,
209                              p_suppress_credit      IN   VARCHAR2,
210                              p_con_terminate_amount IN   NUMBER,
211                              --p_existing_credit      IN   NUMBER,
212                              p_bill_action          IN   VARCHAR2,
213                              p_period_start         IN   VARCHAR2 DEFAULT NULL,
214                              p_period_type          IN   VARCHAR2 DEFAULT NULL,
215                              x_return_status        OUT  NOCOPY VARCHAR2,
216                              p_termination_date     IN   DATE  DEFAULT NULL  ---skuchima bug 11691332
217                              )
218 IS
219  CURSOR bcl_cur (p_date_from IN DATE,
220                  p_date_to   IN DATE ) IS
221   SELECT  bcl.id                    bcl_bcl_id
222          ,bcl.cle_id                bcl_cle_id
223          ,bcl.btn_id                bcl_btn_id
224          ,bcl.Date_Billed_from      bcl_date_billed_from
225          ,bcl.Date_Billed_to        bcl_date_billed_to
226          ,bcl.date_next_invoice     bcl_date_next_invoice
227          ,bcl.amount                bcl_amount
228          ,bcl.bill_action           bcl_bill_action
229          ,bcl.Attribute_category    bcl_attribute_category
230          ,bcl.Attribute1            bcl_attribute1
231          ,bcl.Attribute2            bcl_attribute2
232          ,bcl.Attribute3            bcl_attribute3
233          ,bcl.Attribute4            bcl_attribute4
234          ,bcl.Attribute5            bcl_attribute5
235          ,bcl.Attribute6            bcl_attribute6
236          ,bcl.Attribute7            bcl_attribute7
237          ,bcl.Attribute8            bcl_attribute8
238          ,bcl.Attribute9            bcl_attribute9
239          ,bcl.Attribute10           bcl_attribute10
240          ,bcl.Attribute11           bcl_attribute11
241          ,bcl.Attribute12           bcl_attribute12
242          ,bcl.Attribute13           bcl_attribute13
243          ,bcl.Attribute14           bcl_attribute14
244          ,bcl.Attribute15           bcl_attribute15
245          ,bsl.id                    bsl_bsl_id
246          ,bsl.cle_id                bsl_cle_id
247          ,bsl.average               bsl_average
248          ,bsl.amount                bsl_amount
249          ,bsl.Date_Billed_from      bsl_date_billed_From
250          ,bsl.Date_Billed_to        bsl_date_billed_to
251          ,bsl.date_to_interface     bsl_date_to_interface
252          ,bsl.Attribute_category    bsl_attribute_category
253          ,bsl.Attribute1            bsl_attribute1
254          ,bsl.Attribute2            bsl_attribute2
255          ,bsl.Attribute3            bsl_attribute3
256          ,bsl.Attribute4            bsl_attribute4
257          ,bsl.Attribute5            bsl_attribute5
258          ,bsl.Attribute6            bsl_attribute6
259          ,bsl.Attribute7            bsl_attribute7
260          ,bsl.Attribute8            bsl_attribute8
261          ,bsl.Attribute9            bsl_attribute9
262          ,bsl.Attribute10           bsl_attribute10
263          ,bsl.Attribute11           bsl_attribute11
264          ,bsl.Attribute12           bsl_attribute12
265          ,bsl.Attribute13           bsl_attribute13
266          ,bsl.Attribute14           bsl_attribute14
267          ,bsl.Attribute15           bsl_attribute15
268          --,bsl.manual_credit         line_existing_credit
269          ,line.start_date           cov_start_date
270          ,line.end_date             cov_end_date
271          ,line.price_negotiated     cov_price_negotiated
272    FROM
273          okc_k_lines_b       line,
274          oks_bill_cont_lines bcl,
275          oks_bill_sub_lines  bsl
276    WHERE bcl.Cle_id = p_top_line_id
277    AND   bsl.cle_id = nvl(p_cov_line_id,bsl.cle_id)
278    AND   bcl.id     = bsl.bcl_id
279    AND   bcl.bill_action = 'RI'
280    ---AND   bsl.amount > 0                      -- Added to avoid -ve inv rec generated during settlement cycle  /*Commented the condition for bug 11773025*/
281    --09-FEB mchoudha changed from bcl to bsl
282    --For OM contracts if the subline end date is less than the top line end date then the termination records
283    --were no getting created because of the bcl condition.
284    AND   trunc(bsl.date_billed_to) <= trunc(p_date_to)
285    AND   trunc(bsl.date_billed_to) >= trunc(p_date_from)
286    AND   line.id    =   bsl.cle_id
287    AND   line.date_Terminated is NULL
288 /*
289    and   not exists
290          (select 1 from oks_bill_cont_lines bclsub
291           where bclsub.cle_id = bcl.cle_id
292           and  trunc(bclsub.date_billed_from) = trunc(bcl.date_billed_from)
293           and  bclsub.bill_action = 'AV')
294 */
295    ORDER  by bcl.Date_billed_from, bcl.id desc;
296 
297 CURSOR bsd_cur(p_bsl_id IN NUMBER) IS
298    SELECT  bsl_id_averaged
299           ,bsd_id
300           ,bsd_id_applied
301           ,ccr_id
302           ,cgr_id
303           ,start_reading
304           ,end_reading
305           ,unit_of_measure
306           ,fixed
307           ,actual
308           ,default_default
309           ,amcv_yn
310           ,adjustment_level
311           ,adjustment_minimum
312           ,result
313           ,amount
314           ,Attribute_category
315           ,Attribute1
316           ,Attribute2
317           ,Attribute3
318           ,Attribute4
319           ,Attribute5
320           ,Attribute6
321           ,Attribute7
322           ,Attribute8
323           ,Attribute9
324           ,Attribute10
325           ,Attribute11
326           ,Attribute12
327           ,Attribute13
328           ,Attribute14
329           ,Attribute15
330     FROM  Oks_bill_sub_line_dtls
331     WHERE bsl_id = p_bsl_id;
332 
333 Cursor check_bcl(p_date_billed_from   IN   DATE
334                 ,p_date_billed_to     IN   DATE
335                 ,p_bill_action        IN   VARCHAR2)
336      IS
337     SELECT id
338           ,amount
339      FROM oks_bill_cont_lines
340      WHERE cle_id = p_top_line_id
341      AND   bill_Action = p_bill_Action
342      AND   trunc(date_billed_from) = trunc(p_date_billed_from)
343      AND   trunc(date_billed_to )  = trunc(p_date_billed_to)
344      AND   btn_id is null;
345 
346 Cursor hdr_cur (p_cle_id  IN  NUMBER) IS
347     SELECT  line.Start_date
348            ,line.End_date
349            ,line.Date_Terminated
350            ,line.dnz_chr_id
351            ,line.sts_code
352            ,line.lse_id
353            ,line.line_number
354            ,hdr.currency_code
355            ,hdr.id
356          FROM    okc_k_headers_b hdr ,
357                  okc_k_lines_B   line
358          WHERE   line.id = p_cle_id
359          AND     line.dnz_chr_id = hdr.id;
360 
361 Cursor qty_uom_csr(p_cle_id  IN NUMBER) Is
362      SELECT  okc.Number_of_items
363             ,tl.Unit_of_measure uom_code
364      FROM   okc_k_items OKC
365            ,mtl_units_of_measure_tl tl
366      WHERE  okc.cle_id = P_cle_id
367      AND    tl.uom_code = OKC.uom_code
368      AND    tl.language = USERENV('LANG');
369 
370 CURSOR inv_item_csr(p_cle_id IN NUMBER) Is
371   SELECT  primary_uom_code
372   FROM   Okc_K_items Item
373         ,mtl_system_items mtl
374         ,okc_k_lines_b      line
375         ,okc_k_headers_b    hdr
376   WHERE  item.cle_id = p_cle_id
377   AND    line.id     = item.cle_id
378   AND    hdr.id      = line.dnz_chr_id
379   AND    mtl.inventory_item_id = item.object1_id1
380   ---AND    mtl.organization_id = hdr.authoring_org_id;
381   AND    mtl.organization_id = hdr.inv_organization_id;
382 
383 Cursor bill_instance_csr (p_bcl_id in NUMBER,
384                           p_bsl_id in NUMBER) is
385        SELECT txn.bill_instance_number from oks_bill_txn_lines txn
386        WHERE bcl_id = p_bcl_id
387        AND   nvl(bsl_id,-1) = decode(bsl_id,NULL,-1,p_bsl_id);
388       /*nvl condition added in above to take care of summary billing records.
389         in case of summary billing bsl_id is null in above table*/
390 
391 Cursor rules_csr(l_termination_date IN DATE,p_cov_line_id IN NUMBER) IS
392     SELECT  str.uom_code      l_freq
393       FROM oks_stream_levels_b str,
394            oks_level_elements  lvl
395       WHERE lvl.cle_id = p_cov_line_id
396       AND   lvl.rul_id = str.id
397       AND   l_termination_date between lvl.date_start and lvl.date_end;
398 
399 Cursor avg_csr_bsl_amt(p_cle_id IN NUMBER, p_date_from IN DATE) IS
400     SELECT sum(amount)
401       FROM oks_bill_sub_lines
402       WHERE cle_id =  p_cle_id
403       AND   date_billed_from = p_date_from;
404 
405 SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
406   l_bclv_tbl_in   l_bclv_tbl_type_in;
407   l_bclv_tbl_out  l_bclv_tbl_type_in;
408 
409 SUBTYPE l_bslv_tbl_type_in  is OKS_bsl_PVT.bslv_tbl_type;
410   l_bslv_tbl_in   l_bslv_tbl_type_in;
411   l_bslv_tbl_out   l_bslv_tbl_type_in;
412 
413 SUBTYPE l_bsdv_tbl_type_in  is OKS_bsd_PVT.bsdv_tbl_type;
414   l_bsdv_tbl_in   l_bsdv_tbl_type_in;
415   l_bsdv_tbl_out   l_bsdv_tbl_type_in;
416 
417 bcl_rec                               BCL_CUR%ROWTYPE;
418 hdr_rec                               HDR_CUR%ROWTYPE;
419 inv_rec                               INV_ITEM_CSR%ROWTYPE;
420 rules_rec                             RULES_CSR%ROWTYPE;
421 
422 
423 l_cov_tbl                             OKS_BILL_REC_PUB.COVERED_TBL;
424 l_return_status                       VARCHAR2(20);
425 l_msg_data                            VARCHAR2(2000);
426 l_freq                                VARCHAR2(20);
427 l_temp_bcl_id_ri                      NUMBER := -11;  -- stores the bcl_id for invoice line
428 l_temp_bcl_id_tr                      NUMBER := -11;  -- stores the bcl_id for Credit line
429 l_msg_cnt                             NUMBER;
430 l_amount                              NUMBER;
431 l_bcl_id                              NUMBER;
432 l_bill_instance_number                NUMBER;
433 l_bill_amount                         NUMBER;
434 term_bsl_amount                       NUMBER;
435 l_diff                                NUMBER;
436 l_api_version         CONSTANT        NUMBER     := 1.0;
437 l_init_msg_list       CONSTANT        VARCHAR2(1):= 'F';
438 --l_bill_instance_number NUMBER;
439 l_termination_date                    DATE;
440 
441 
442 
443 BEGIN
444 
445   l_return_status := OKC_API.G_RET_STS_SUCCESS;
446 
447 
448   DBMS_TRANSACTION.SAVEPOINT('BEFORE_TRANSACTION');
449 
450   IF (p_bill_action in ('RI','SRI')) THEN
451 
452 
453     OKS_BILL_REC_PUB.INSERT_BCL
454      (
455        P_CALLEDFROM        => 1,
456        X_RETURN_STAT       => l_return_status,
457        P_CLE_ID            => p_top_line_id,
458        P_DATE_BILLED_FROM  => p_date_from,
459        P_DATE_BILLED_TO    => p_date_to,
460        P_DATE_NEXT_INVOICE => trunc(sysdate),
461        P_BILL_ACTION       => p_bill_action ,
462        P_OKL_FLAG          => 2,
463        P_PRV               => 1,
464        P_MSG_COUNT         => l_msg_cnt,
465        P_MSG_DATA          => l_msg_data,
466        X_BCL_ID            => l_bcl_id
467        );
468 
469     OPEN  INV_ITEM_CSR(p_top_line_id);
470     FETCH INV_ITEM_CSR into inv_rec;
471     CLOSE INV_ITEM_CSR;
472 
473 
474     OPEN  hdr_cur(p_top_line_id);
475     FETCH hdr_cur into hdr_rec;
476     CLOSE hdr_cur;
477 
478 
479     l_cov_tbl(1).flag              := Null;
480     l_cov_tbl(1).id                := p_cov_line_id ;
481     l_cov_tbl(1).bcl_id            := l_bcl_id;
482     l_cov_tbl(1).date_billed_from  := p_date_from;
483     l_cov_tbl(1).date_billed_to    := p_date_to;
484     l_cov_tbl(1).amount            := p_con_terminate_amount; --p_amount;
485     l_cov_tbl(1).sign              := 1;
486     l_cov_tbl(1).average           := 0;
487     l_cov_tbl(1).unit_of_measure   := inv_rec.primary_uom_code;
488     l_cov_tbl(1).fixed             := 0;
489     l_cov_tbl(1).actual            := p_amount;
490     l_cov_tbl(1).default_default   := 0;
491     l_cov_tbl(1).amcv_yn           := 'N';
492     l_cov_tbl(1).adjustment_level  := 0 ;
493     l_cov_tbl(1).adjustment_minimum:= 0 ;
494     l_cov_tbl(1).result            := p_amount ;
495     l_cov_tbl(1).estimated_quantity:= 0;
496     l_cov_tbl(1).x_stat            := Null ;
497     l_cov_tbl(1).amount            := p_con_terminate_amount;  --p_amount;
498     l_cov_tbl(1).bcl_amount        := p_con_terminate_amount;  --p_amount;
499     l_cov_tbl(1).date_to_interface := sysdate;
500 
501     OKS_BILL_REC_PUB.Insert_all_subline
502            (
503             P_CALLEDFROM     => 1,
504             X_RETURN_STAT    => l_return_status,
505             P_COVERED_TBL    => l_cov_tbl,
506             P_CURRENCY_CODE  => hdr_rec.currency_code,
507             P_DNZ_CHR_ID     => hdr_rec.id,
508             P_PRV            => 1,
509             P_MSG_COUNT      => l_msg_cnt,
510             P_MSG_DATA       => l_msg_data
511             );
512 
513   --for ppc bug#4638641
514   --The following logic is called only during
515   --termination after repricing when the unbilled amount is
516   --greater than the termination amount and termination date
517   --is less than or equal to the max billed to date
518   IF  p_period_start is NOT NULL and p_period_type IS NOT NULL
519   THEN
520         UPDATE oks_level_elements
521         SET date_completed = p_date_to,
522             amount = p_con_terminate_amount
523         WHERE cle_id = p_cov_line_id
524         AND   date_completed is null
525         AND   date_start = p_date_from
526         AND   date_end = p_date_to;
527 
528         UPDATE oks_level_elements
529         SET date_completed = p_date_to,
530         amount = amount-p_amount+p_con_terminate_amount
531         WHERE cle_id = p_top_line_id
532         AND   date_completed is null
533         AND   date_start = p_date_from
534         AND   date_end = p_date_to;
535 
536   END IF;
537 
538   ELSIF ( p_bill_action in ('AV','TR','STR')) THEN
539 
540 
541     FOR bcl_rec in bcl_cur(p_date_from,p_date_to)
542     LOOP
543       OPEN  hdr_cur(bcl_rec.bsl_cle_id);
544       FETCH hdr_cur into hdr_rec;
545       CLOSE hdr_cur;
546 
547       OPEN  rules_csr(p_date_from,bcl_rec.bsl_cle_id);
548       FETCH rules_csr into rules_rec;
549       CLOSE rules_csr;
550 
551       l_bclv_tbl_in.delete;
552       l_bclv_tbl_out.delete;
553       l_bslv_tbl_in.delete;
554       l_bslv_tbl_out.delete;
555 
556 
557       IF ( bcl_rec.bcl_date_billed_from < p_date_from ) THEN
558         l_bclv_tbl_in(1).DATE_BILLED_FROM   := p_date_from;
559       ELSE
560         l_bclv_tbl_in(1).DATE_BILLED_FROM   := bcl_rec.bcl_date_billed_from;
561       END IF;
562 
563       l_bclv_tbl_in(1).DATE_BILLED_TO     := bcl_rec.bcl_date_billed_to;
564 
565 /*****
566             OPEN check_bcl(l_bclv_tbl_in(1).date_billed_from,
567                      l_bclv_tbl_in(1).date_billed_to,
568                      p_bill_action);
569             FETCH check_bcl into l_bclv_tbl_in(1).id,
570                            l_bclv_tbl_in(1).AMOUNT ;
571 
572       IF (check_bcl%NOTFOUND) THEN
573 **/
574 
575 /***
576     The above cursor is commented to fix bug# 4243931. the below if clause check is
577     used to create credit bcl's for its corresponding invoice bcl's
578 ***/
579       if ( l_temp_bcl_id_ri <> bcl_rec.bcl_bcl_id) Then
580 
581         l_bclv_tbl_in(1).CLE_ID             := bcl_rec.bcl_cle_id;
582         l_bclv_tbl_in(1).BTN_ID             := Null;
583         l_bclv_tbl_in(1).SENT_YN            :=  'N';
584 
585         l_bclv_tbl_in(1).DATE_NEXT_INVOICE  := bcl_rec.bcl_date_next_invoice;
586         l_bclv_tbl_in(1).ATTRIBUTE_CATEGORY := bcl_rec.bcl_attribute_category;
587         l_bclv_tbl_in(1).ATTRIBUTE1         := bcl_rec.bcl_attribute1;
588         l_bclv_tbl_in(1).ATTRIBUTE2         := bcl_rec.bcl_attribute2;
589         l_bclv_tbl_in(1).ATTRIBUTE3         := bcl_rec.bcl_attribute3;
590         l_bclv_tbl_in(1).ATTRIBUTE4         := bcl_rec.bcl_attribute4;
591         l_bclv_tbl_in(1).ATTRIBUTE5         := bcl_rec.bcl_attribute5;
592         l_bclv_tbl_in(1).ATTRIBUTE6         := bcl_rec.bcl_attribute6;
593         l_bclv_tbl_in(1).ATTRIBUTE7         := bcl_rec.bcl_attribute7;
594         l_bclv_tbl_in(1).ATTRIBUTE8         := bcl_rec.bcl_attribute8;
595         l_bclv_tbl_in(1).ATTRIBUTE9         := bcl_rec.bcl_attribute9;
596         l_bclv_tbl_in(1).ATTRIBUTE10        := bcl_rec.bcl_attribute10;
597         l_bclv_tbl_in(1).ATTRIBUTE11        := bcl_rec.bcl_attribute11;
598         l_bclv_tbl_in(1).ATTRIBUTE12        := bcl_rec.bcl_attribute12;
599         l_bclv_tbl_in(1).ATTRIBUTE13        := bcl_rec.bcl_attribute13;
600         l_bclv_tbl_in(1).ATTRIBUTE14        := bcl_rec.bcl_attribute14;
601         l_bclv_tbl_in(1).ATTRIBUTE15        := bcl_rec.bcl_attribute15;
602         l_bclv_tbl_in(1).BILL_ACTION        := p_bill_action;
603         l_bclv_tbl_in(1).CURRENCY_CODE      := hdr_rec.currency_code;
604         l_bclv_tbl_in(1).AMOUNT             := 0;
605 
606        --skuchima 11691332
607           IF (p_termination_date IS NOT NULL AND (l_bclv_tbl_in(1).DATE_BILLED_FROM < p_termination_date)) THEN
608                 l_bclv_tbl_in(1).DATE_BILLED_FROM := p_termination_date;
609           END IF;
610         --skuchima
611 
612 
613         IF (nvl(p_suppress_credit,'N') = 'Y') THEN
614           l_bclv_tbl_in(1).BTN_ID             := -44;
615         END IF;
616 
617         OKS_BILLCONTLINE_PUB.insert_Bill_Cont_Line
618               (
619                p_api_version                  =>  1.0,
620                p_init_msg_list                =>  'T',
621                x_return_status                =>   l_return_status,
622                x_msg_count                    =>   l_msg_cnt,
623                x_msg_data                     =>   l_msg_data,
624                p_bclv_tbl                     =>   l_bclv_tbl_in,
625                x_bclv_tbl                     =>   l_bclv_tbl_out
626                );
627 
628         /* These statements are added to get the id of new record
629              which is inserted in bcl*/
630         l_bclv_tbl_in(1).ID         := l_bclv_tbl_out(1).ID;
631 
632         --- the temp variable assignment holds the current invoice bcl
633         l_temp_bcl_id_ri := bcl_rec.bcl_bcl_id;
634         --- the temp variable assignment holds the current credit bcl
635         l_temp_bcl_id_tr := l_bclv_tbl_out(1).ID;
636 
637         IF not l_return_status = OKC_API.G_RET_STS_SUCCESS THEN
638           x_return_status := l_return_status;
639           Raise G_EXCEPTION_HALT_VALIDATION;
640         END IF;
641 
642       END IF;
643 /****
644       commented to fix bug# 4243931
645       CLOSE Check_bcl;
646 ***/
647 
648       ---l_bslv_tbl_in(1).BCL_ID  := l_bclv_tbl_in(1).id;
649       l_bslv_tbl_in(1).CLE_ID := bcl_rec.bsl_cle_id;
650       l_bslv_tbl_in(1).BCL_ID  := l_temp_bcl_id_tr;
651 
652       /*Average field is used to store bill_instance_number
653         of the parent. It is used to populate referenc_line_id in
654         AR Feeder
655       */
656       OPEN  bill_instance_csr(bcl_rec.bcl_bcl_id, bcl_rec.bsl_bsl_id);
657       FETCH bill_instance_csr into l_bill_instance_number;
658       CLOSE bill_instance_csr;
659 
660 
661       l_bslv_tbl_in(1).AVERAGE := l_bill_instance_number;
662       --l_bslv_tbl_in(1).AVERAGE := l_bill_instance_number;
663 
664 
665       IF ( bcl_rec.bsl_date_billed_from < p_date_From ) THEN
666         l_bslv_tbl_in(1).DATE_BILLED_FROM := p_date_From;
667       ELSE
668         l_bslv_tbl_in(1).DATE_BILLED_FROM := bcl_rec.bsl_date_billed_from;
669       END IF;
670 
671 
672 
673       l_bslv_tbl_in(1).DATE_BILLED_TO := bcl_rec.bsl_date_billed_to;
674       l_bslv_tbl_in(1).ATTRIBUTE_CATEGORY := bcl_rec.bsl_attribute_category;
675       l_bslv_tbl_in(1).ATTRIBUTE1 := bcl_rec.bsl_attribute1;
676       l_bslv_tbl_in(1).ATTRIBUTE2 := bcl_rec.bsl_attribute2;
677       l_bslv_tbl_in(1).ATTRIBUTE3 := bcl_rec.bsl_attribute3;
678       l_bslv_tbl_in(1).ATTRIBUTE4 := bcl_rec.bsl_attribute4;
679       l_bslv_tbl_in(1).ATTRIBUTE5 := bcl_rec.bsl_attribute5;
680       l_bslv_tbl_in(1).ATTRIBUTE6 := bcl_rec.bsl_attribute6;
681       l_bslv_tbl_in(1).ATTRIBUTE7 := bcl_rec.bsl_attribute7;
682       l_bslv_tbl_in(1).ATTRIBUTE8 := bcl_rec.bsl_attribute8;
683       l_bslv_tbl_in(1).ATTRIBUTE9 := bcl_rec.bsl_attribute9;
684       l_bslv_tbl_in(1).ATTRIBUTE10 := bcl_rec.bsl_attribute10;
685       l_bslv_tbl_in(1).ATTRIBUTE11 := bcl_rec.bsl_attribute11;
686       l_bslv_tbl_in(1).ATTRIBUTE12 := bcl_rec.bsl_attribute12;
687       l_bslv_tbl_in(1).ATTRIBUTE13 := bcl_rec.bsl_attribute13;
688       l_bslv_tbl_in(1).ATTRIBUTE14 := bcl_rec.bsl_attribute14;
689       l_bslv_tbl_in(1).ATTRIBUTE15 := bcl_rec.bsl_attribute15;
690 
691       --skuchima 11691332
692         IF (p_termination_date IS NOT NULL AND (l_bslv_tbl_in(1).DATE_BILLED_FROM < p_termination_date)) THEN
693                  l_bslv_tbl_in(1).DATE_BILLED_FROM := p_termination_date;
694         END IF;
695        --skuchima 11691332
696 
697       l_bslv_tbl_in(1).date_to_interface := get_term_end_date (bcl_rec.bsl_cle_id , p_date_from );
698 
699      if ( hdr_rec.lse_id in (12,13)) Then
700            OPEN  avg_csr_bsl_amt(bcl_rec.bsl_cle_id, bcl_rec.bsl_date_billed_from);
701            FETCH avg_csr_bsl_amt into term_bsl_amount;
702            CLOSE avg_csr_bsl_amt;
703      else
704            term_bsl_amount := bcl_rec.bsl_amount;
705      End if; --- ( hdr_rec.lse_id in (12,13)) Then
706 
707 
708       IF (trunc(p_date_from) > trunc(bcl_rec.bsl_date_billed_from)) THEN
709         /*For termination of contract create from OKS */
710 
711         IF (p_called_from = 1) THEN
712          -------------------------------------------------------------------------
713          -- Begin partial period computation logic
714          -- Developer Mani Choudhary
715          -- Date 15-JUN-2005
716          -------------------------------------------------------------------------
717          IF p_period_type IS NOT NULL AND
718             p_period_start IS NOT NULL AND
719             hdr_rec.lse_id in (12,13)
720          THEN
721            l_amount:= Get_partial_term_amount(
722                                     p_start_date        => bcl_rec.bsl_date_billed_from,
723                                     p_end_date          => bcl_rec.bsl_date_billed_to,
724                                     P_termination_date  => p_date_from,
725                                     ---P_amount            => bcl_rec.bsl_amount,
726                                     P_amount            => term_bsl_amount,
727                                     P_uom               => rules_rec.l_freq,
728                                     P_period_start      => P_period_start,
729                                     P_period_type       => P_period_type
730                                     );
731 
732             IF l_amount is NULL THEN
733                RAISE G_EXCEPTION_HALT_VALIDATION;
734             END IF;
735          ELSE
736 
737             get_bill_amount_period(
738                   -99,
739                   hdr_rec.start_date , --p_con_start_date,
740                   bcl_rec.bsl_date_billed_from, --p_con_end_date,
741                   rules_rec.l_freq,
742                   ---bcl_rec.bsl_amount,--p_con_amount,
743                   term_bsl_amount,
744                   bcl_rec.bsl_date_billed_to,
745                   p_date_from,         -- termination_date
746                   G_NONREGULAR,
747                   l_amount);
748          END IF;
749         /*For termination of contract create from OM */
750         ELSIF (p_called_from = 2) THEN
751           l_diff :=abs(TRUNC(bcl_rec.cov_end_date - bcl_rec.cov_start_date)+1);
752           l_bill_amount := bcl_rec.cov_price_negotiated/l_diff;
753           l_diff := abs(trunc((bcl_rec.cov_end_date + 1 ) - p_date_from));
754           l_amount :=  l_bill_amount * l_diff;
755         ELSIF (p_called_from = 3) THEN
756           l_amount :=  p_con_terminate_amount;
757         END IF;
758 
759         --dbms_output.put_line('Amount credited '|| l_amount);
760         IF (nvl(l_amount,0) > 0) THEN
761           l_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(abs(l_amount), hdr_rec.currency_code );
762         END IF;
763 
764         l_bslv_tbl_in(1).AMOUNT := -1 * (l_amount );
765       ELSE
766         IF (p_called_from = 3) THEN
767           l_amount :=  p_con_terminate_amount;
768         ELSE
769           ---l_bslv_tbl_in(1).AMOUNT := -1 * (bcl_rec.bsl_amount );
770           ---l_amount    := bcl_rec.bsl_amount;
771           l_bslv_tbl_in(1).AMOUNT := -1 * (term_bsl_amount );
772           l_amount    := term_bsl_amount;
773         END IF;
774       END IF;
775 
776       IF (p_override_amount is not null) THEN
777         l_amount :=  (p_override_amount * (l_amount ))/ (p_con_terminate_amount);    ---Removed abs() for bug 11773025
778       END IF;
779 
780       --IF (bcl_rec.bsl_amount - abs(nvl(bcl_rec.line_existing_credit,0)) < l_amount) THEN
781       --  l_amount :=  abs(bcl_rec.bsl_amount - abs(nvl(bcl_rec.line_existing_credit,0)));
782       --END IF;
783 
784      --rounded for bug # 2791940
785      l_amount                := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_amount, hdr_rec.currency_code );
786      l_bslv_tbl_in(1).AMOUNT :=  -1 * l_amount;
787       --dbms_output.put_line('Amount credited after if'|| l_amount);
788 
789 
790      OKS_BILLSUBLINE_PUB.insert_Bill_subLine_Pub
791               (
792               p_api_version                  =>  1.0,
793               p_init_msg_list                =>  'T',
794               x_return_status                =>   l_return_status,
795               x_msg_count                    =>   l_msg_cnt,
796               x_msg_data                     =>   l_msg_data,
797               p_bslv_tbl                     =>   l_bslv_tbl_in,
798               x_bslv_tbl                     =>   l_bslv_tbl_out
799               );
800 
801 
802      IF (l_return_status <> OKC_API.G_RET_STS_SUCCESS)  THEN
803        x_return_status := l_return_status;
804        Raise G_EXCEPTION_HALT_VALIDATION;
805      END IF;
806 
807      g_credit_amount := nvl(g_credit_amount,0) + l_amount;
808 
809      g_bcl_id := l_bslv_tbl_in(1).BCL_ID;
810      g_bsl_id := l_bslv_tbl_out(1).id;
811       FOR bsd_rec IN bsd_cur(bcl_rec.bsl_bsl_id)
812       LOOP
813         l_bsdv_tbl_in.delete;
814         l_bsdv_tbl_out.delete;
815 
816         l_bsdv_tbl_in(1).BSL_ID              := l_bslv_tbl_out(1).id;
817         l_bsdv_tbl_in(1).BSL_ID_AVERAGED     := bsd_rec.bsl_id_averaged;
818         l_bsdv_tbl_in(1).BSD_ID              := bsd_rec.bsd_id;
819         l_bsdv_tbl_in(1).BSD_ID_APPLIED      := bsd_rec.bsd_id_applied;
820         l_bsdv_tbl_in(1).UNIT_OF_MEASURE     := bsd_rec.unit_of_measure;
821         l_bsdv_tbl_in(1).FIXED               := bsd_rec.fixed;
822         l_bsdv_tbl_in(1).ACTUAL              := bsd_rec.actual;
823         l_bsdv_tbl_in(1).DEFAULT_DEFAULT     := bsd_rec.default_default;
824         l_bsdv_tbl_in(1).ESTIMATED_QUANTITY  := 0;
825         l_bsdv_tbl_in(1).AMCV_YN             := bsd_rec.amcv_yn;
826         l_bsdv_tbl_in(1).ADJUSTMENT_LEVEL    := bsd_rec.adjustment_level;
827         l_bsdv_tbl_in(1).ADJUSTMENT_MINIMUM  := bsd_rec.adjustment_minimum;
828         IF (P_amount IS NOT NULL) THEN
829           l_bsdv_tbl_in(1).RESULT              := p_amount;
830         ELSE
831           l_bsdv_tbl_in(1).RESULT              := bsd_rec.result;
832         END IF;
833         l_bsdv_tbl_in(1).ATTRIBUTE_CATEGORY  := bsd_rec.attribute_category;
834         l_bsdv_tbl_in(1).ATTRIBUTE1          := bsd_rec.attribute1;
835         l_bsdv_tbl_in(1).ATTRIBUTE2          := bsd_rec.attribute2;
836         l_bsdv_tbl_in(1).ATTRIBUTE3          := bsd_rec.attribute3;
837         l_bsdv_tbl_in(1).ATTRIBUTE4          := bsd_rec.attribute4;
838         l_bsdv_tbl_in(1).ATTRIBUTE5          := bsd_rec.attribute5;
839         l_bsdv_tbl_in(1).ATTRIBUTE6          := bsd_rec.attribute6;
840         l_bsdv_tbl_in(1).ATTRIBUTE7          := bsd_rec.attribute7;
841         l_bsdv_tbl_in(1).ATTRIBUTE8          := bsd_rec.attribute8;
842         l_bsdv_tbl_in(1).ATTRIBUTE9          := bsd_rec.attribute9;
843         l_bsdv_tbl_in(1).ATTRIBUTE10         := bsd_rec.attribute10;
844 
845         l_bsdv_tbl_in(1).ATTRIBUTE11         := bsd_rec.attribute11;
846         l_bsdv_tbl_in(1).ATTRIBUTE12         := bsd_rec.attribute12;
847         l_bsdv_tbl_in(1).ATTRIBUTE13         := bsd_rec.attribute13;
848         l_bsdv_tbl_in(1).ATTRIBUTE14         := bsd_rec.attribute14;
849         l_bsdv_tbl_in(1).ATTRIBUTE15         := bsd_rec.attribute15;
850         l_bsdv_tbl_in(1).AMOUNT              := -1 * l_amount;
851 
852 
853         OKS_BSL_det_PUB.insert_bsl_det_Pub
854                  (
855                     p_api_version                  =>  1.0,
856                     p_init_msg_list                =>  'T',
857                     x_return_status                =>   l_return_status,
858                     x_msg_count                    =>   l_msg_cnt,
859                     x_msg_data                     =>   l_msg_data,
860                     p_bsdv_tbl                     =>   l_bsdv_tbl_in,
861                     x_bsdv_tbl                     =>   l_bsdv_tbl_out
862                  );
863 
864 
865 
866         IF (l_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
867           x_return_status := l_return_status;
868           Raise G_EXCEPTION_HALT_VALIDATION;
869         END IF;
870       END LOOP;    --bsd cursor loop
871 
872       --l_bclv_tbl_in(1).AMOUNT := (l_bclv_tbl_in(1).amount - nvl(l_amount,0));
873       --rounded for bug # 2791940
874 /*
875       l_bclv_tbl_in(1).AMOUNT := OKS_EXTWAR_UTIL_PVT.round_currency_amt(
876          (nvl(l_bclv_tbl_in(1).amount,0) - nvl(abs(l_amount),0)),
877                                                     hdr_rec.currency_code);
878 */
879 
880 
881      ---commented the above since the rounded value is stored in bsl amount
882       UPDATE oks_bill_cont_lines
883       SET    amount = nvl(amount,0) + l_bslv_tbl_in(1).AMOUNT
884       WHERE  id     = l_temp_bcl_id_tr;
885 
886     l_temp_bcl_id_ri := bcl_rec.bcl_bcl_id;
887 
888     END LOOP;  --bcl cursor loop
889 
890   END IF;  -- p_bill_action = 'RI'
891 
892     x_return_status := l_return_status;
893 
894 EXCEPTION
895   WHEN G_EXCEPTION_HALT_VALIDATION THEN
896         x_return_status := OKC_API.G_RET_STS_ERROR ;
897   WHEN  OTHERS THEN
898           OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN, SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
899 
900 END Create_trx_records;
901 
902 
903 
904 Procedure terminate_amount(
905     P_CALLEDFROM          IN         NUMBER   DEFAULT Null ,
906     P_TOP_LINE_ID         IN         NUMBER,
907     P_COVLVL_ID           IN         NUMBER,
908     P_TERMINATION_DATE    IN         DATE,
909     P_USAGE_TYPE          IN         VARCHAR2,
910     P_USAGE_PERIOD        IN         VARCHAR2,
911     X_AMOUNT              OUT NOCOPY NUMBER,
912     X_QUANTITY            OUT NOCOPY NUMBER
913   ) IS
914 CURSOR l_uom_csr Is
915   SELECT uom_code
916   FROM   Okc_time_code_units_v
917   WHERE  tce_code = 'DAY'
918   AND    quantity = 1;
919 
920 
921 CURSOR l_inv_item_csr(p_cle_id Number) Is
922   SELECT mtl.primary_uom_code,
923          line.dnz_chr_id
924   FROM   Okc_K_items Item
925         ,mtl_system_items_b   mtl  --Okx_system_items_v mtl
926         ,okc_k_headers_b      hdr
927         ,okc_k_lines_b   line
928   WHERE  item.cle_id = line.id     --p_cle_id
929   AND    line.id     = p_cle_id
930   AND    line.dnz_chr_id = hdr.id
931   --AND    mtl.id1 = item.object1_id1
932   AND    mtl.inventory_item_id = item.object1_id1
933   AND    mtl.organization_id = hdr.inv_organization_id;   --p_org_id;
934 
935 
936 
937 CURSOR covlvl_line(p_top_id IN   NUMBER,
938                    p_cov_id IN   NUMBER) is
939   SELECT line.id,
940          line.start_date,
941          line.end_date,
942          rline.usage_est_yn,
943          rline.usage_est_method,
944          rline.default_quantity,
945          rline.usage_est_start_date    usage_est_start_date
946    FROM  okc_k_lines_b   line,
947          oks_k_lines_b   rline
948    WHERE line.cle_id = p_top_id
949    AND   line.id     = nvl(p_cov_id,line.id)
950    AND   line.lse_id = 13
951    AND   line.date_cancelled is null --LLC BUG FIX 4742661
952    AND   rline.cle_id = line.id;
953 
954 --Cursor declaration for partial periods
955 ------------------------------------------------
956 CURSOR l_period_type_csr(p_hdr_id IN NUMBER) IS
957 SELECT period_type,period_start
958 FROM   oks_k_headers_b
959 WHERE  chr_id = p_hdr_id;
960 -------------------------------------------------
961 
962 l_return_status          VARCHAR2(10);
963 l_counter_uom_code       VARCHAR2(10);
964 l_flag                   VARCHAR2(10);
965 l_uom_code               VARCHAR2(25);
966 l_primary_uom_code       VARCHAR2(25);
967 l_qty                    NUMBER;
968 l_est_qty                NUMBER;
969 l_actual_qty             NUMBER;
970 l_amt                    NUMBER;
971 l_tot_billed_amount      NUMBER;
972 l_amt_before_term        NUMBER;
973 l_start_reading          NUMBER;
974 l_end_reading            NUMBER;
975 l_base_reading           NUMBER;
976 l_counter_value_id       NUMBER;
977 l_counter_grp_id         NUMBER;
978 
979 l_msg_cnt                NUMBER;
980 l_msg_data               VARCHAR2(2000);
981 l_api_version   CONSTANT NUMBER     := 1.0;
982 l_init_msg_list CONSTANT VARCHAR2(1):= 'F';
983 
984 --local variables for partial periods
985 ----------------------------------------------------------
986 l_period_type            OKS_K_HEADERS_B.period_type%TYPE;
987 l_period_start           OKS_K_HEADERS_B.period_start%TYPE;
988 l_hdr_id                 NUMBER;
989 ----------------------------------------------------------
990 l_line_rec               OKS_QP_PKG.Input_details ;
991 l_price_rec              OKS_QP_PKG.Price_Details ;
992 l_modifier_details       QP_PREQ_GRP.LINE_DETAIL_TBL_TYPE;
993 l_price_break_details    OKS_QP_PKG.G_PRICE_BREAK_TBL_TYPE;
994 BEGIN
995   OPEN  l_uom_csr;
996   FETCH l_uom_csr into l_uom_code;
997   CLOSE l_uom_csr;
998 
999 
1000   OPEN  l_inv_item_csr(p_top_line_id);
1001   FETCH l_inv_item_csr into l_primary_uom_code,l_hdr_id;
1002   CLOSE l_inv_item_csr;
1003 
1004   --Mani 12-JUN-2005
1005   --fetch the period type for partial period calculation
1006   ---------------------------------------------
1007   OPEN l_period_type_csr(l_hdr_id);
1008   FETCH l_period_type_csr INTO l_period_type,l_period_start;
1009   CLOSE l_period_type_csr;
1010   ---------------------------------------------
1011 
1012   FOR cur in covlvl_line(p_top_line_id,p_covlvl_id)
1013   LOOP
1014     -------------------------------------------------------------------------
1015     -- Begin partial period computation logic
1016     -- Developer Mani Choudhary
1017     -- Date 11-JUN-2005
1018     -- Added additional period_type parameter
1019     -------------------------------------------------------------------------
1020     OKS_BILL_REC_PUB.Usage_qty_to_bill
1021                 (
1022                 P_calledfrom            => 3,  --1 for normal , 2 for preview,
1023                 P_cle_id                => cur.id,
1024                 P_Usage_type            => p_usage_type,
1025                 P_estimation_flag       => cur.usage_est_yn,
1026                 P_estimation_method     => cur.usage_est_method,
1027                 P_default_qty           => cur.default_quantity,
1028                 P_cov_start_date        => cur.start_date,
1029                 P_cov_end_date          => cur.end_date,
1030                 P_cov_prd_start_date    => cur.start_date,
1031                 P_cov_prd_end_date      => p_termination_date,
1032                 p_usage_period          => p_usage_period,
1033                 p_time_uom_code         => l_uom_code,
1034                 p_settle_interval       => NULL,
1035                 p_minimum_quantity      => 0,
1036                 p_usg_est_start_date    => cur.usage_est_start_date,
1037                 p_period_type           => l_period_type, -- period type
1038                 p_period_start          => l_period_start, -- period start
1039                 X_qty                   => l_qty,
1040                 X_Uom_Code              => l_counter_uom_code,
1041                 X_flag                  => l_flag,
1042                 X_end_reading           => l_end_reading,
1043                 X_start_reading         => l_start_reading,
1044                 X_base_reading          => l_base_reading,
1045                 X_estimated_qty         => l_est_qty,
1046                 X_actual_qty            => l_actual_qty,
1047                 X_counter_value_id      => l_counter_value_id,
1048                 X_counter_group_id      => l_counter_grp_id,
1049                 X_return_status         => l_return_status
1050                   );
1051 
1052 
1053 
1054     l_line_rec.line_id          := p_top_line_id;
1055     l_line_rec.intent           := 'USG';
1056     l_line_rec.usage_qty        := l_qty;
1057     l_line_rec.usage_uom_code   := l_primary_uom_code; --qty_uom_rec.uom_code;
1058 
1059     X_QUANTITY := l_qty;
1060 
1061     OKS_QP_PKG.CALC_PRICE
1062                 (
1063                  P_DETAIL_REC          => l_line_rec,
1064                  X_PRICE_DETAILS       => l_price_rec,
1065                  X_MODIFIER_DETAILS    => l_modifier_details,
1066                  X_PRICE_BREAK_DETAILS => l_price_break_details,
1067                  X_RETURN_STATUS       => l_return_status,
1068                  X_MSG_COUNT           => l_msg_cnt,
1069                  X_MSG_DATA            => l_msg_data
1070                 );
1071 
1072     X_amount :=  l_price_rec.PROD_EXT_AMOUNT;
1073     --X_amount :=  l_amt_before_term + l_price_rec.PROD_EXT_AMOUNT;
1074 
1075 
1076   END LOOP;
1077 
1078 END terminate_amount;
1079 
1080 ------------------------------------------------------------------------
1081   -- FUNCTION pre_vol_based_terminate
1082 ------------------------------------------------------------------------
1083  PROCEDURE  pre_vol_based_terminate(
1084    P_CALLEDFROM                IN         NUMBER   DEFAULT Null ,
1085    P_API_VERSION               IN         NUMBER   ,
1086    P_INIT_MSG_LIST             IN         VARCHAR2 DEFAULT OKC_API.G_FALSE   ,
1087    X_RETURN_STATUS             OUT NOCOPY VARCHAR2   ,
1088    X_MSG_COUNT                 OUT NOCOPY NUMBER   ,
1089    X_MSG_DATA                  OUT NOCOPY VARCHAR2   ,
1090    P_K_LINE_ID                 IN         NUMBER   ,
1091    P_CP_LINE_ID                IN         NUMBER   ,
1092    P_TERMINATION_DATE          IN         DATE   ,
1093    P_TERMINATION_AMOUNT        IN         NUMBER   ,
1094    P_CON_TERMINATION_AMOUNT    IN         NUMBER   ,
1095    --P_EXISTING_CREDIT           IN         NUMBER   ,
1096    P_SUPPRESS_CREDIT           IN         VARCHAR2  ,
1097    P_USAGE_TYPE                IN         VARCHAR2,
1098    P_USAGE_PERIOD              IN         VARCHAR2,
1099    X_AMOUNT                    IN         NUMBER )
1100 IS
1101  CURSOR covlvl_line(p_cle_id IN   NUMBER,p_cp_line_id IN NUMBER) is
1102   SELECT line.id,
1103          line.start_date,
1104          rline.usage_est_yn,
1105          rline.usage_est_method
1106    FROM  okc_k_lines_b   line,
1107          oks_k_lines_b   rline
1108    WHERE line.cle_id = p_cle_id
1109    AND   line.lse_id = 13
1110    AND   line.id     = nvl(p_cp_line_id,line.id)
1111    AND   line.date_cancelled is NULL --LLC BUG FIX 4742661
1112    AND   rline.cle_id = line.id;
1113 
1114 
1115 
1116 Cursor total_billed_qty (p_cle_id   IN NUMBER)
1117 IS
1118 SELECT sum(bsd.result) ,
1119        sum(bsl.amount) ,
1120        max(bsl.date_billed_From),
1121        max(bsl.date_billed_to)
1122    FROM oks_bill_sub_line_dtls      bsd,
1123         oks_bill_sub_lines          bsl
1124    WHERE bsl.cle_id = p_cle_id
1125    AND   bsd.bsl_id = bsl.id;
1126 
1127 
1128 Cursor bsl_cur (p_cle_id IN NUMBER)
1129 IS
1130 SELECT bsl.id,
1131        bsl.date_billed_from,
1132        bsl.date_billed_to,
1133        bsl.amount,
1134        bsd.result
1135   FROM oks_bill_cont_lines    bcl,
1136        oks_bill_sub_lines     bsl,
1137        oks_bill_sub_line_dtls bsd
1138   WHERE bcl.id  =  bsl.bcl_id
1139   AND   bsl.cle_id = p_cle_id
1140   AND   bsd.bsl_id = bsl.id
1141  ORDER by bsl.date_billed_to desc;
1142 
1143 
1144 l_qty                   NUMBER;
1145 l_amt                   NUMBER;
1146 l_credit_amount         NUMBER;
1147 l_credit_qty            NUMBER;
1148 l_total_term_qty        NUMBER;
1149 l_term_amount           NUMBER;
1150 l_term_qty              NUMBER;
1151 l_term_bcl_id           NUMBER;
1152 l_term_bsl_id           NUMBER;
1153 l_billed_qty            NUMBER;
1154 l_billed_amt            NUMBER;
1155 l_max_date_billed_from  DATE;
1156 l_max_date_billed_to    DATE;
1157 l_term_period_bill_qty  NUMBER;
1158 l_term_period_cre_qty   NUMBER;
1159 l_return_status         VARCHAR2(10);
1160 l_uom_code              VARCHAR2(25);
1161 l_term_prd_date_from    DATE;
1162 p_date                  DATE;
1163 
1164 l_msg_cnt                NUMBER;
1165 l_msg_data               VARCHAR2(2000);
1166 l_api_version   CONSTANT NUMBER     := 1.0;
1167 l_init_msg_list CONSTANT VARCHAR2(1):= 'F';
1168 
1169 
1170 
1171 BEGIN
1172 
1173   FOR cur in covlvl_line(p_k_line_id,p_cp_line_id)
1174   LOOP
1175     Terminate_amount(
1176        P_CALLEDFROM       => 1,
1177        P_TOP_LINE_ID      => p_k_line_id   ,
1178        P_COVLVL_ID        => cur.id   ,
1179        P_TERMINATION_DATE => p_termination_date   ,
1180        P_USAGE_TYPE       => p_usage_type   ,
1181        P_USAGE_PERIOD     => p_usage_period   ,
1182        X_AMOUNT           => l_amt   ,
1183        X_QUANTITY         => l_qty
1184         );
1185 
1186     OPEN   total_billed_qty(cur.id);
1187     FETCH  total_billed_qty into l_billed_qty,l_billed_amt,
1188                                  l_max_date_billed_from,l_max_date_billed_to;
1189     CLOSE  total_billed_qty;
1190 
1191     IF (l_billed_amt <  l_amt) THEN
1192        --create invoice as already billed amount is lesser than total amount to be charged till termination date
1193       l_term_amount := l_amt - l_billed_amt;
1194       Create_trx_records(
1195                p_called_from           => 1,
1196                p_top_line_id           => p_k_line_id,
1197                p_cov_line_id           => cur.id,
1198                p_date_from             => l_max_date_billed_from,
1199                p_date_to               => l_max_date_billed_to,
1200                p_amount                => l_qty - l_billed_qty,
1201                p_override_amount       => 0,
1202              --p_suppress_credit       => 'N',
1203                p_suppress_credit       => P_SUPPRESS_CREDIT,  --  Bug Fix 5062595 maanand --
1204                p_con_terminate_amount  => l_term_amount ,
1205                --p_existing_credit       => p_existing_credit,
1206                p_bill_action           => 'RI',
1207                x_return_status         => l_return_status,
1208                P_TERMINATION_DATE => p_termination_date    --skuchima 11691332
1209                );
1210 
1211     ELSE
1212       -- Create credits  as already billed amount is greater than total amount to be charged till termination date
1213       /* BUG 3343780 fix. L_credit_qty is prorated so that total terminated qty can be viewed from billing history */
1214       l_credit_amount := l_billed_amt - l_amt;
1215       l_credit_qty    := l_billed_qty - l_qty;
1216 
1217       FOR bsl_rec in bsl_cur(cur.id)
1218       LOOP
1219         IF (l_credit_amount <= 0) THEN
1220           EXIT;
1221         END IF;
1222 
1223         IF (l_credit_amount >= bsl_rec.amount) THEN
1224           l_term_amount :=  bsl_rec.amount;
1225           --l_term_qty    :=  bsl_rec.result;
1226           IF (l_credit_qty <= bsl_rec.result) THEN
1227             l_term_qty := l_credit_qty;
1228           ELSE
1229             l_term_qty := bsl_rec.result;
1230           END IF;
1231         ELSE
1232           l_term_amount :=  l_credit_amount;
1233           l_term_qty    :=  l_credit_qty   ;
1234         END IF;
1235 
1236         Create_trx_records(
1237                p_called_from           => 1,
1238                p_top_line_id           => p_k_line_id,
1239                p_cov_line_id           => cur.id,
1240                p_date_from             => bsl_rec.date_billed_from,
1241                p_date_to               => bsl_rec.date_billed_to,
1242                p_amount                => l_term_qty,
1243                p_override_amount       => l_term_amount,
1244              --p_suppress_credit       => 'N',
1245                p_suppress_credit       => P_SUPPRESS_CREDIT, ----  Bug Fix 5062595 maanand --
1246                p_con_terminate_amount  => bsl_rec.amount,
1247                --p_existing_credit       => p_existing_credit,
1248                p_bill_action           => 'TR',
1249                x_return_status         => l_return_status,
1250                P_TERMINATION_DATE => p_termination_date      --skuchima 11691332
1251                );
1252 
1253         l_credit_amount := l_credit_amount - bsl_rec.amount;
1254         --l_credit_qty    := l_credit_qty    - bsl_rec.result;
1255         IF (l_credit_qty > bsl_rec.result ) THEN
1256           l_credit_qty    := l_credit_qty    - bsl_rec.result;
1257         ELSE
1258           l_credit_qty := 0;
1259         END IF;
1260       END LOOP;
1261 
1262 
1263     END IF;
1264 
1265 
1266   END LOOP;
1267 
1268 END;
1269 
1270 ------------------------------------------------------------------------
1271   -- FUNCTION pre_terminate_amount
1272 ------------------------------------------------------------------------
1273   PROCEDURE pre_terminate_amount
1274   (
1275     P_CALLEDFROM                   IN         NUMBER DEFAULT Null,
1276     p_id                           IN         NUMBER,
1277     p_terminate_date               IN         DATE,
1278     p_flag                         IN         NUMBER,
1279     X_Amount                      OUT NOCOPY  NUMBER,
1280     --X_manual_credit               OUT NOCOPY  NUMBER,
1281     X_return_status               OUT NOCOPY  VARCHAR2)IS
1282 Cursor line_cur(p_hdr_id IN NUMBER) is
1283 SELECT okl.id ,
1284        okl.lse_id ,
1285        okl.price_negotiated,
1286        okh.currency_code,
1287        okh.contract_number,
1288        okh.contract_number_modifier
1289   FROM okc_k_lines_b okl,
1290        okc_k_headers_b  okh
1291   WHERE okh.id = p_hdr_id
1292   AND   okl.dnz_chr_id = okh.id
1293   AND   okl.cle_id is null
1294   AND   okl.date_cancelled is null --LLC BUG FIX 4742661
1295   AND   okl.date_terminated is null;
1296  -- AND   okl.sts_code in ('ACTIVE','SIGNED') ;
1297 
1298   -------------------------------------------------------------------------
1299 -- Begin partial period computation logic
1300 -- Developer Mani Choudhary
1301 -- Date 30-MAY-2005
1302 -- Added header id in the select clause
1303 -------------------------------------------------------------------------
1304 Cursor hdr_currency_code_cur(p_id IN NUMBER) is
1305 SELECT okh.currency_code,
1306        okh.id header_id,
1307        okl.lse_id  ,
1308        okl.cle_id  ,
1309        okl.price_negotiated,
1310        okh.contract_number,
1311        okh.contract_number_modifier
1312    FROM okc_k_headers_b okh,
1313         okc_k_lines_b    okl
1314    WHERE  okl.id = p_id
1315    AND    okh.id = okl.dnz_chr_id;
1316 
1317 
1318 Cursor line_billed_cur (p_id IN NUMBER) is
1319 SELECT bcl.id ,rline.termn_method,
1320        rline.usage_type, rline.usage_period
1321    FROM oks_bill_cont_lines  bcl,
1322         oks_k_lines_b        rline
1323    WHERE bcl.cle_id = p_id
1324    AND   rline.cle_id = bcl.cle_id
1325    AND   bcl.bill_action = 'RI';
1326 
1327 Cursor subscr_line_bill_cur (p_cle_id IN NUMBER) is
1328 SELECT nvl(sum(nvl(amount,0)),0),max(date_billed_To)
1329     FROM oks_bill_cont_lines
1330     WHERE cle_id = p_cle_id
1331     AND   bill_action = 'RI';
1332 
1333 Cursor subscr_line_ship_cur (p_id IN NUMBER) is
1334 SELECT nvl(sum(nvl(amount,0)),0),max(end_date)
1335     FROM oks_subscr_elements
1336     WHERE dnz_cle_id = p_id
1337     AND   order_header_id is not null;
1338 
1339 Cursor sub_line_billed_cur (p_id IN NUMBER) is
1340 SELECT bsl.id ,rline.TERMN_METHOD ,bcl.cle_id,
1341        rline.usage_type, rline.usage_period
1342    FROM oks_bill_sub_lines bsl,
1343         oks_bill_cont_lines bcl,
1344         oks_k_lines_b       rline
1345    WHERE bsl.cle_id = p_id
1346    AND   bsl.bcl_id = bcl.id
1347    AND   rline.cle_id = bcl.cle_id
1348    AND   bcl.bill_action = 'RI';
1349 
1350 
1351 Cursor tot_sub_line_billed( p_cle_id IN NUMBER) is
1352 SELECT sum(amount)
1353    FROM oks_bill_sub_lines
1354    WHERE cle_id = p_cle_id;
1355 
1356 
1357 Cursor tot_line_billed( p_cle_id IN NUMBER) is
1358 SELECT sum(amount)
1359    FROM oks_bill_cont_lines
1360    WHERE cle_id = p_cle_id;
1361 
1362 
1363 cursor l_calc_bill_amount_csr (p_line_id number ) is
1364 SELECT sum(amount) FROM
1365 oks_bill_cont_lines
1366 WHERE cle_id = p_line_id ;
1367 
1368 l_billed_amount        NUMBER ;
1369 final_amount           NUMBER;
1370 l_lse_id               NUMBER;
1371 l_cov_id               NUMBER;
1372 l_top_id               NUMBER;
1373 l_number               NUMBER;
1374 l_bill_amount          NUMBER;
1375 l_ship_amount          NUMBER;
1376 l_bill_qty             NUMBER;
1377 l_quantity             NUMBER;
1378 l_cov_line             VARCHAR2(2);
1379 l_return_status        VARCHAR2(5);
1380 l_fulfillment_channel  VARCHAR2(10);
1381 l_term_method          VARCHAR2(10);
1382 l_usage_type           VARCHAR2(10);
1383 l_usage_period         VARCHAR2(10);
1384 ----Hari fix for bug 5667743
1385 l_contract_number      OKC_K_HEADERS_B.CONTRACT_NUMBER%TYPE;
1386 l_contract_modifier    OKC_K_HEADERS_B.CONTRACT_NUMBER_MODIFIER%TYPE;
1387 l_term_date            DATE;
1388 l_max_bill_date        DATE;
1389 l_max_ship_date        DATE;
1390 l_billed               BOOLEAN ;
1391 l_tang                 BOOLEAN ;
1392 l_currency_code        okc_k_headers_b.currency_code%TYPE;
1393 l_orig_price           NUMBER;
1394 -------------------------------------------------------------------------
1395 -- Begin partial period computation logic
1396 -- Developer Mani Choudhary
1397 -- Date 30-MAY-2005
1398 -- local variables for partal periods
1399 -------------------------------------------------------------------------
1400 l_hdr_id              NUMBER;
1401 l_price_uom           OKS_K_HEADERS_B.PRICE_UOM%TYPE;
1402 l_period_start        OKS_K_HEADERS_B.PERIOD_START%TYPE;
1403 l_period_type         OKS_K_HEADERS_B.PERIOD_TYPE%TYPE;
1404 l_pricing_method      VARCHAR2(30);
1405 -------------------------------------------------------------------------
1406 
1407 
1408 BEGIN
1409 
1410   IF fnd_log.level_procedure >= fnd_log.g_current_runtime_level THEN
1411      fnd_log.string(fnd_log.level_procedure,G_MODULE_CURRENT||'.pre_terminate_amount.p_flag',
1412     'Entering pre_terminate_amount with input parameters  p_flag ' ||p_flag||' ,p_id '||p_id||' ,p_terminate_date '||to_char(p_terminate_date));
1413   END IF;
1414 
1415   x_return_status := OKC_API.G_RET_STS_SUCCESS ;
1416   l_billed        := FALSE;
1417   l_term_method   := 'AMOUNT';
1418   IF (p_flag in  (1,3)) THEN -- p_id is line_id
1419 
1420     l_cov_line  := 'N';
1421 
1422 -------------------------------------------------------------------------
1423 -- Begin partial period computation logic
1424 -- Developer Mani Choudhary
1425 -- Date 30-MAY-2005
1426 -- Added l_hdr_id
1427 -------------------------------------------------------------------------
1428 
1429     OPEN  hdr_currency_code_cur(p_id);
1430     FETCH hdr_currency_code_cur into l_currency_code,l_hdr_id,l_lse_id,l_top_id,l_orig_price,
1431                                      l_contract_number,
1432                                      l_contract_modifier;
1433     CLOSE hdr_currency_code_cur;
1434 
1435 -------------------------------------------------------------------------
1436 -- Begin partial period computation logic
1437 -- Developer Mani Choudhary
1438 -- Date 30-MAY-2005
1439 -- Call oks_renew_util_pub.get_period_defaults to fetch period start and period type
1440 -------------------------------------------------------------------------
1441     IF l_hdr_id IS NOT NULL THEN
1442 
1443       OKS_RENEW_UTIL_PUB.Get_Period_Defaults
1444                 (
1445                  p_hdr_id        => l_hdr_id,
1446                  p_org_id        => NULL,
1447                  x_period_start  => l_period_start,
1448                  x_period_type   => l_period_type,
1449                  x_price_uom     => l_price_uom,
1450                  x_return_status => l_return_status);
1451       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1452           fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.line_termination.ppc_defaults',
1453           'After calling OKS_RENEW_UTIL_PUB.Get_Period_Defaults l_period_start ' ||l_period_start||' ,l_period_type '||l_period_type);
1454       END IF;
1455       IF l_return_status <> 'S' THEN
1456         RAISE G_EXCEPTION_HALT_VALIDATION;
1457       END IF;
1458     END IF;
1459 
1460     --mchoudha bug fix 4732550
1461     --Added condition lse_id 13 for subline case
1462     IF l_lse_id in (12,46,13) AND l_period_start IS NOT NULL THEN
1463       l_period_start:= 'SERVICE';
1464     END IF;
1465 -------------------------------------------------------------------------
1466 -- End partial period computation logic
1467 --------------------------------------------------------------------------
1468     IF (p_flag = 3) THEN
1469       l_cov_line := 'Y';
1470       l_cov_id   := p_id;
1471 
1472       IF (l_lse_id = 13) THEN
1473         OPEN  Sub_line_billed_cur(p_id);
1474         FETCH sub_line_billed_cur into l_number,l_term_method,l_top_id,
1475                                         l_usage_type, l_usage_period;
1476         IF (sub_line_billed_cur%FOUND) THEN
1477           l_billed := TRUE;
1478         ELSE
1479           l_billed := FALSE;
1480         END IF;
1481         CLOSE sub_line_billed_cur;
1482       END IF;
1483 
1484     ELSIF (p_flag = 1) THEN
1485       l_top_id := p_id;
1486       IF (l_lse_id = 46 ) THEN
1487         l_tang := OKS_SUBSCRIPTION_PUB.IS_SUBS_TANGIBLE(P_ID) ;
1488       END IF;
1489 
1490       IF (l_lse_id = 12) THEN
1491         OPEN line_billed_cur(p_id);
1492         FETCH line_billed_cur into l_number,l_term_method,
1493                                    l_usage_type, l_usage_period;
1494         IF (line_billed_cur%FOUND) THEN
1495           l_billed := TRUE;
1496         ELSE
1497           l_billed := FALSE;
1498         END IF;
1499         CLOSE line_billed_cur;
1500       ELSIF ((l_lse_id = 46) AND (l_tang)) THEN
1501         OPEN subscr_line_bill_cur(p_id);
1502         FETCH subscr_line_bill_cur into l_bill_amount,l_max_bill_date;
1503         CLOSE subscr_line_bill_cur;
1504 
1505         OPEN subscr_line_ship_cur(p_id);
1506         FETCH subscr_line_ship_cur into l_ship_amount,l_max_ship_date;
1507         CLOSE subscr_line_ship_cur;
1508 
1509       END IF;
1510     END IF;  -- p_flag
1511 
1512 
1513     l_term_date := p_terminate_date;
1514 
1515 
1516     IF (l_lse_id = 46)  THEN
1517 
1518       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1519           fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.line_termination','Calling Subscription_termination');
1520       END IF;
1521 
1522       IF l_tang  THEN
1523 
1524         get_subscr_terminate_amount
1525            ( P_CALLEDFROM         => p_calledfrom,
1526              P_LINE_ID            => p_id,
1527              P_TERMINATE_DATE     => l_term_date,
1528              P_BILLED_AMOUNT      => l_bill_amount,
1529              P_SHIPPED_AMOUNT     => l_ship_amount,
1530              P_MAX_BILL_DATE      => l_max_bill_date,
1531              P_MAX_SHIP_DATE      => l_max_ship_date,
1532              X_AMOUNT             => X_amount,
1533              X_RETURN_STATUS      => X_return_status ) ;
1534         If x_amount < 0 then
1535            x_amount := 0;
1536         End If;
1537       ELSE
1538         OPEN  l_calc_bill_amount_csr ( p_id );
1539         FETCH l_calc_bill_amount_csr into l_billed_amount ;
1540         CLOSE l_calc_bill_amount_csr;
1541         --mchoudha fix for bug#4729993
1542       /*  l_pricing_method :=FND_PROFILE.value('OKS_SUBS_PRICING_METHOD');
1543         IF l_period_start IS NOT NULL AND
1544            l_period_type IS NOT NULL AND
1545            l_pricing_method = 'EFFECTIVITY' THEN
1546           x_amount :=  l_billed_amount - l_orig_price +
1547                              OKS_SUBSCRIPTION_PUB.subs_termn_amount(
1548                                            p_cle_id     =>p_id ,
1549                                            p_termn_date =>l_term_date );
1550         ELSE*/
1551           x_amount :=  l_billed_amount -
1552                              OKS_SUBSCRIPTION_PUB.subs_termn_amount(
1553                                            p_cle_id     =>p_id ,
1554                                            p_termn_date =>l_term_date );
1555         --END IF;
1556         If x_amount < 0 then
1557            x_amount := 0 ;
1558         End If;
1559       END IF;
1560     ELSE
1561       IF (l_term_method = 'VOLUME') THEN
1562 
1563         IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1564           fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.line_termination.Volume_termination',
1565           'usage period '||l_usage_period);
1566         END IF;
1567 
1568         terminate_amount(
1569              P_CALLEDFROM          => 2,
1570              P_TOP_LINE_ID         => l_top_id,
1571              P_COVLVL_ID           => l_cov_id,
1572              P_TERMINATION_DATE    => l_term_date,
1573              P_USAGE_TYPE          => l_usage_type,
1574              P_USAGE_PERIOD        => l_usage_period,
1575              X_AMOUNT              => X_amount,
1576              X_QUANTITY            => l_quantity
1577            );
1578 
1579         IF (l_lse_id = 12) THEN
1580           OPEN  tot_line_billed(p_id);
1581           FETCH tot_line_billed into l_bill_Qty;
1582           CLOSE tot_line_billed;
1583         ELSIF (l_lse_id = 13) THEN
1584           OPEN  tot_sub_line_billed(p_id);
1585           FETCH tot_sub_line_billed into l_bill_Qty;
1586           CLOSE tot_sub_line_billed;
1587         END IF;
1588 
1589         X_AMOUNT := l_bill_qty - X_amount;
1590 
1591       ELSE
1592          -------------------------------------------------------------------------
1593          -- Begin partial period computation logic
1594          -- Developer Mani Choudhary
1595          -- Date 30-MAY-2005
1596          -- Call the procedure Get_Term_Amt_Ppc for partial periods
1597          -------------------------------------------------------------------------
1598         IF l_period_start IS NOT NULL AND
1599            l_period_type IS NOT NULL
1600         THEN
1601           IF l_lse_id in (12,13) THEN
1602             IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1603                fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.line_termination.PPC',
1604                'Calling Get_Terminate_Amount for usage with l_cov_line '||l_cov_line);
1605             END IF;
1606 
1607             Get_Terminate_Amount
1608             (
1609              P_CALLEDFROM       => p_calledfrom     ,
1610              P_LINE_ID          => p_id             ,
1611              P_COV_LINE         => l_cov_line       ,
1612              P_TERMINATE_DATE   => l_term_date      ,
1613              P_PERIOD_START     => l_period_start   ,
1614              P_PERIOD_TYPE      => l_period_type    ,
1615              X_AMOUNT           => X_amount         ,
1616              X_RETURN_STATUS    => X_return_status
1617              );
1618              IF X_return_status <> 'S' THEN
1619                RAISE G_EXCEPTION_HALT_VALIDATION;
1620              END IF;
1621           ELSE
1622 
1623             IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1624                fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.line_termination.PPC',
1625                'Calling Get_Term_Amt_Ppc for service  with l_cov_line '||l_cov_line);
1626             END IF;
1627 
1628             Get_Term_Amt_Ppc
1629                (P_termination_date => l_term_date,
1630                 P_line_id          => p_id,
1631                 P_cov_line         => l_cov_line,
1632                 p_period_start     => l_period_start,
1633                 p_period_type      => l_period_type,
1634                 X_amount           => x_amount,
1635                 x_return_status    => l_return_status);
1636 
1637                 IF l_return_status <> 'S' THEN
1638                   RAISE G_EXCEPTION_HALT_VALIDATION;
1639                 END IF;
1640           END IF;
1641         ELSE
1642 
1643           Get_Terminate_Amount
1644            (
1645             P_CALLEDFROM       => p_calledfrom     ,
1646             P_LINE_ID          => p_id             ,
1647             P_COV_LINE         => l_cov_line       ,
1648             P_TERMINATE_DATE   => l_term_date      ,
1649             X_AMOUNT           => X_amount         ,
1650             X_RETURN_STATUS    => X_return_status
1651             );
1652           IF X_return_status <> 'S' THEN
1653             RAISE G_EXCEPTION_HALT_VALIDATION;
1654           END IF;
1655         END IF;
1656       END IF;
1657     END IF;
1658 
1659     final_amount := X_Amount;
1660 
1661 
1662    ELSIF (p_flag = 2) Then -- p_id is hdr_id
1663     --Mani R12 PPC
1664     OKS_RENEW_UTIL_PUB.Get_Period_Defaults
1665                 (
1666                  p_hdr_id        => p_id,
1667                  p_org_id        => NULL,
1668                  x_period_start  => l_period_start,
1669                  x_period_type   => l_period_type,
1670                  x_price_uom     => l_price_uom,
1671                  x_return_status => l_return_status);
1672       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1673          fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.hdr_termination.ppc_defaults',
1674          ' After calling OKS_RENEW_UTIL_PUB.Get_Period_Defaults l_period_start ' ||l_period_start||' ,l_period_type '||l_period_type);
1675       END IF;
1676 
1677       IF l_return_status <> 'S' THEN
1678         RAISE G_EXCEPTION_HALT_VALIDATION;
1679       END IF;
1680 
1681      --Mani R12 PPC
1682      final_amount := 0;
1683      X_amount     := 0;
1684      l_cov_line   := 'N';
1685      FOR line_rec in line_cur(p_id)
1686      LOOP
1687        --Mani R12 PPC
1688        IF line_rec.lse_id = 12 AND l_period_start IS NOT NULL THEN
1689          l_period_start:= 'SERVICE';
1690        END IF;
1691        --Mani R12 PPC
1692        l_currency_code := line_rec.currency_code;
1693        l_billed        := FALSE;
1694        X_amount     := 0;
1695 
1696        IF (line_rec.lse_id = 46 ) THEN
1697          l_tang := OKS_SUBSCRIPTION_PUB.IS_SUBS_TANGIBLE(line_rec.id) ;
1698        END IF;
1699 
1700        IF (line_rec.lse_id = 12)  THEN
1701          OPEN  line_billed_cur(line_rec.id);
1702          FETCH line_billed_cur into l_number,l_term_method,
1703                                     l_usage_type, l_usage_period;
1704          IF (line_billed_cur%FOUND) THEN
1705            l_billed := TRUE;
1706          ELSE
1707            l_billed := FALSE;
1708          END IF;
1709          CLOSE line_billed_cur;
1710        ELSIF ((line_rec.lse_id = 46) AND (l_tang)) THEN
1711          OPEN subscr_line_bill_cur(line_rec.id);
1712          FETCH subscr_line_bill_cur into l_bill_amount,l_max_bill_date;
1713          CLOSE subscr_line_bill_cur;
1714 
1715          OPEN subscr_line_ship_cur(line_rec.id);
1716          FETCH subscr_line_ship_cur into l_ship_amount,l_max_ship_date;
1717          CLOSE subscr_line_ship_cur;
1718        END IF;
1719 
1720        l_term_date := p_terminate_date;
1721 
1722        IF (line_rec.lse_id = 46)  THEN
1723          IF l_tang THEN
1724            get_subscr_terminate_amount
1725             (
1726               P_CALLEDFROM         => p_calledfrom,
1727               P_LINE_ID            => line_rec.id,
1728               P_TERMINATE_DATE     => l_term_date,
1729               P_BILLED_AMOUNT      => l_bill_amount,
1730               P_SHIPPED_AMOUNT     => l_ship_amount,
1731               P_MAX_BILL_DATE      => l_max_bill_date,
1732               P_MAX_SHIP_DATE      => l_max_ship_date,
1733               X_AMOUNT             => X_amount,
1734               X_RETURN_STATUS      => X_return_status
1735              ) ;
1736             If x_amount < 0 then
1737                x_amount := 0;
1738             End If;
1739          ELSE
1740            OPEN  l_calc_bill_amount_csr ( line_rec.id );
1741            FETCH l_calc_bill_amount_csr into l_billed_amount ;
1742            CLOSE l_calc_bill_amount_csr;
1743           /* l_pricing_method :=FND_PROFILE.value('OKS_SUBS_PRICING_METHOD');
1744            IF l_period_start IS NOT NULL AND
1745               l_period_type IS NOT NULL AND
1746               l_pricing_method = 'EFFECTIVITY' THEN
1747               x_amount := x_amount + l_billed_amount - line_rec.price_negotiated +
1748                                  OKS_SUBSCRIPTION_PUB.subs_termn_amount(
1749                                              p_cle_id     =>line_rec.id ,
1750                                              p_termn_date =>l_term_date );
1751            ELSE*/
1752              x_amount := x_amount + l_billed_amount -
1753                                  OKS_SUBSCRIPTION_PUB.subs_termn_amount(
1754                                              p_cle_id     =>line_rec.id ,
1755                                              p_termn_date =>l_term_date );
1756            --END IF;
1757 
1758            If x_amount < 0 then
1759               x_amount := 0;
1760            End If;
1761          END IF;
1762 
1763        ELSE
1764 
1765          l_top_id   := line_rec.id;
1766          l_cov_id   := NULL;
1767 
1768          IF (l_term_method = 'VOLUME') THEN
1769            terminate_amount(
1770              P_CALLEDFROM          => 2,
1771              P_TOP_LINE_ID         => l_top_id,
1772              P_COVLVL_ID           => l_cov_id,
1773              P_TERMINATION_DATE    => l_term_date,
1774              P_USAGE_TYPE          => l_usage_type,
1775              P_USAGE_PERIOD        => l_usage_period,
1776              X_AMOUNT              => X_amount,
1777              X_QUANTITY            => l_quantity
1778              );
1779 
1780            IF (line_rec.lse_id = 12) THEN
1781              OPEN  tot_line_billed(line_rec.id);
1782              FETCH tot_line_billed into l_bill_Qty;
1783              CLOSE tot_line_billed;
1784            END IF;
1785 
1786            X_AMOUNT := l_bill_qty - X_amount;
1787 
1788          ELSE
1789            -------------------------------------------------------------------------
1790            -- Begin partial period computation logic
1791            -- Developer Mani Choudhary
1792            -- Date 30-MAY-2005
1793            -- Call the procedure Get_Term_Amt_Ppc for partial periods
1794            -------------------------------------------------------------------------
1795            IF l_period_start IS NOT NULL AND
1796               l_period_type IS NOT NULL
1797            THEN
1798              IF line_rec.lse_id in (12,13) THEN
1799                IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1800                   fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.hdr_termination.PPC',
1801                   'Calling Get_Terminate_Amount for usage with l_cov_line '||l_cov_line);
1802                END IF;
1803                Get_Terminate_Amount
1804                (
1805                 P_CALLEDFROM       => p_calledfrom     ,
1806                 P_LINE_ID          => line_rec.id             ,
1807                 P_COV_LINE         => l_cov_line       ,
1808                 P_TERMINATE_DATE   => l_term_date      ,
1809                 P_PERIOD_START     => l_period_start   ,
1810                 P_PERIOD_TYPE      => l_period_type    ,
1811                 X_AMOUNT           => X_amount         ,
1812                 X_RETURN_STATUS    => X_return_status
1813                );
1814                IF X_return_status <> 'S' THEN
1815                  RAISE G_EXCEPTION_HALT_VALIDATION;
1816                END IF;
1817              ELSE
1818                IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1819                   fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount.hdr_termination.PPC',
1820                   'Calling Get_Term_Amt_Ppc for service  with l_cov_line '||l_cov_line);
1821                END IF;
1822                Get_Term_Amt_Ppc
1823                  (P_termination_date => l_term_date,
1824                   P_line_id          => line_rec.id,
1825                   P_cov_line         => l_cov_line,
1826                   p_period_start     => l_period_start,
1827                   p_period_type      => l_period_type,
1828                   X_amount           => x_amount,
1829                   x_return_status    => l_return_status);
1830                 IF l_return_status <> 'S' THEN
1831                   RAISE G_EXCEPTION_HALT_VALIDATION;
1832                 END IF;
1833 
1834              END IF;
1835            ELSE
1836              Get_Terminate_Amount
1837               (
1838               P_CALLEDFROM       => p_calledfrom     ,
1839               P_LINE_ID          => line_rec.id      ,
1840               P_COV_LINE         => l_cov_line       ,
1841               P_TERMINATE_DATE   => l_term_date      ,
1842               X_AMOUNT           => X_amount         ,
1843               X_RETURN_STATUS    => X_return_status
1844               );
1845              IF X_return_status <> 'S' THEN
1846                RAISE G_EXCEPTION_HALT_VALIDATION;
1847              END IF;
1848            END IF;
1849          END IF;
1850        END IF;
1851        final_amount := nvl(final_amount,0) + nvl(X_amount,0);
1852 
1853 
1854      END LOOP;
1855    END IF;
1856    X_Amount :=  OKS_EXTWAR_UTIL_PVT.round_currency_amt(final_amount ,
1857                                                          l_currency_code);
1858    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
1859       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_amount',
1860      'Returning Credit amount X_Amount '||X_Amount);
1861    END IF;
1862 EXCEPTION
1863    WHEN G_EXCEPTION_HALT_VALIDATION THEN
1864      x_return_status := l_return_status ;
1865    WHEN OTHERS THEN
1866      x_return_status := OKC_API.G_RET_STS_ERROR ;
1867 
1868 END pre_terminate_amount;
1869 
1870 
1871 ------------------------------------------------------------------------
1872   -- FUNCTION get_subscr_terminate_amount
1873 ------------------------------------------------------------------------
1874  PROCEDURE get_subscr_terminate_amount
1875    (
1876     P_CALLEDFROM                   IN        NUMBER DEFAULT Null,
1877     p_line_id                      IN        NUMBER,
1878     p_terminate_date               IN        DATE,
1879     p_billed_amount                IN        NUMBER,
1880     p_shipped_amount               IN        NUMBER,
1881     p_max_bill_date                IN        DATE,
1882     p_max_ship_date                IN        DATE,
1883     X_amount                      OUT NOCOPY NUMBER,
1884     X_return_status               OUT NOCOPY VARCHAR2
1885     )
1886     IS
1887  Cursor bill_amount_cur(p_line_id in number,p_term_date in DATE) is
1888  SELECT nvl(sum(amount),0) FROM oks_bill_cont_lines
1889    WHERE cle_id = p_line_id
1890    AND   bill_action = 'RI';
1891 
1892  Cursor ship_amount_cur(p_line_id in number,p_term_date in DATE) is
1893  SELECT nvl(sum(amount),0) FROM oks_subscr_elements
1894    WHERE dnz_cle_id = p_line_id
1895    AND   trunc(start_date) < trunc(p_term_date);
1896 
1897  l_ship_amount        NUMBER;
1898  l_bill_amount        NUMBER;
1899  l_amount             NUMBER;
1900 
1901 
1902  BEGIN
1903    x_return_status := OKC_API.G_RET_STS_SUCCESS ;
1904 
1905    IF (p_billed_amount <= p_shipped_amount ) THEN
1906       X_amount := 0;
1907    ELSE
1908      IF ((TRUNC(p_terminate_date) <  trunc(p_max_bill_date))
1909                AND (trunc(p_terminate_date) > trunc(p_max_ship_date) or (p_max_ship_date is null) )) THEN
1910        /* Two cursor are added because
1911           billing schedule and shipping schedule can be diiferent from
1912           each other. Contracts can have one period of 1 year billing schedule
1913           but monthly shipping schedule  */
1914 
1915        OPEN bill_amount_cur( p_line_id,p_terminate_date);
1916        FETCH bill_amount_cur into l_bill_amount;
1917        CLOSE bill_amount_cur;
1918 
1919        OPEN ship_amount_cur( p_line_id,p_terminate_date);
1920        FETCH ship_amount_cur into l_ship_amount;
1921        CLOSE ship_amount_cur;
1922 
1923         X_amount := l_bill_amount - l_ship_amount;
1924      ELSIF (trunc(p_terminate_date) <= trunc(p_max_ship_date)) THEN
1925         X_amount := p_billed_amount - p_shipped_amount;
1926      ELSIF (trunc(p_terminate_date) >=  trunc(p_max_bill_date))  THEN
1927         --OR (trunc(p_terminate_date) <=  trunc(p_max_ship_date))) THEN
1928         X_amount := 0;
1929      END IF;
1930    END IF;
1931  END;
1932 
1933 ------------------------------------------------------------------------
1934   -- FUNCTION get_terminate_amount
1935 ------------------------------------------------------------------------
1936   PROCEDURE get_terminate_amount
1937   (
1938     P_CALLEDFROM                   IN        NUMBER DEFAULT Null,
1939     p_line_id                      IN        NUMBER,
1940     p_cov_line                     IN        VARCHAR2,
1941     p_terminate_date               IN        DATE,
1942     p_period_start                 IN        VARCHAR2 DEFAULT NULL,
1943     p_period_type                  IN        VARCHAR2 DEFAULT NULL,
1944     X_amount                      OUT NOCOPY NUMBER,
1945     X_return_status               OUT NOCOPY VARCHAR2
1946   )IS
1947 
1948  Cursor get_future_period_amount is
1949   SELECT nvl(sum(bsl.amount),0) amount
1950         FROM
1951             okc_k_lines_b       okl2,
1952             oks_bill_sub_lines  bsl,
1953             oks_bill_cont_lines bcl,
1954             okc_k_lines_b       okl1
1955         WHERE   okl1.id = p_line_id
1956         AND     bcl.cle_id = okl1.id
1957         AND     bsl.bcl_id = bcl.id
1958         AND     bsl.cle_id = okl2.id
1959         AND     okl2.date_terminated is NULL
1960         AND     trunc(bsl.date_billed_from) > trunc(p_terminate_date);
1961 
1962   Cursor get_future_prd_amt_covlvl is
1963   SELECT nvl(sum(bsl.amount),0) amount
1964          FROM
1965                  okc_k_lines_b       okl,
1966                  oks_bill_sub_lines  bsl
1967           WHERE   okl.id = p_line_id
1968           AND     bsl.cle_id = okl.id
1969           AND     okl.date_terminated is NULL
1970           AND     trunc(bsl.date_billed_from) > trunc(p_terminate_date);
1971 
1972  Cursor get_cur_prd_amt_covlvl is
1973      SELECT nvl(bsl.amount,0)  amount,
1974             bsl.date_billed_from date_billed_from,
1975             bsl.date_billed_to   date_billed_to,
1976             okl.start_date       start_date,
1977             okl.end_date         end_date,
1978             okl.id               id
1979             FROM
1980                  oks_bill_sub_lines  bsl,
1981                  okc_k_lines_b       okl
1982            WHERE   okl.id     = p_line_id
1983            AND     okl.id = bsl.cle_id
1984            AND     okl.date_terminated is null
1985            AND     trunc(bsl.date_billed_to)   >= trunc(p_terminate_date)
1986            AND     trunc(bsl.date_billed_from) <= trunc(p_terminate_date);
1987 
1988  Cursor get_current_period_amount is
1989      SELECT nvl(bsl.amount,0)  amount,
1990             bcl.date_billed_from date_billed_from,
1991             bcl.date_billed_to   date_billed_to,
1992             okl1.start_date       start_date,
1993             okl1.end_date         end_date,
1994             okl1.id               id
1995             FROM
1996                  okc_k_lines_b       okl2,
1997                  oks_bill_sub_lines  bsl,
1998                  oks_bill_cont_lines bcl,
1999                  okc_k_lines_b       okl1
2000            WHERE   okl1.id     = p_line_id
2001            AND     bcl.cle_id = okl1.id
2002            AND     bsl.bcl_id = bcl.id
2003            AND     okl2.id = bsl.cle_id
2004            AND     okl2.date_terminated is null
2005            AND     trunc(bcl.date_billed_to)   >= trunc(p_terminate_date)
2006            AND     trunc(bcl.date_billed_from) <= trunc(p_terminate_date);
2007 
2008   Cursor rules_csr(p_line_id in number,p_termination_date in  Date) Is
2009        SELECT  str.uom_code l_freq
2010            FROM oks_stream_levels_b   str,
2011                 oks_level_elements    lvl
2012            WHERE lvl.cle_id =  p_line_id
2013            AND   p_termination_date BETWEEN lvl.date_start AND lvl.date_end
2014            AND   lvl.rul_id = str.id;
2015 
2016 
2017 
2018 Cursor l_rel_csr_cov (p_line_id in NUMBER) Is
2019    SELECT id FROM   OKC_K_REL_OBJS_V
2020          WHERE  cle_id = p_line_id;
2021 
2022 Cursor l_rel_csr (p_line_id in NUMBER) Is
2023     SELECT obj.id FROM   OKC_K_REL_OBJS_V obj,
2024                          OKC_K_LINES_B  ln
2025         WHERE  obj.cle_id = ln.id
2026         AND    ln.cle_id =  p_line_id;
2027 
2028 Cursor line_extwar_cur( p_line_id in NUMBER) is
2029   SELECT price_negotiated
2030         ,start_date
2031         ,end_date
2032         ,dnz_chr_id
2033     FROM   Okc_k_lines_b
2034     WHERE  cle_id = p_line_id
2035     AND    lse_id = 25
2036     AND    date_cancelled is NULL --LLC BUG FIX 4742661
2037     AND    date_terminated is NULL;
2038 
2039 
2040 Cursor subline_extwar_cur( p_line_id in NUMBER) is
2041   SELECT     price_negotiated
2042             ,start_date
2043             ,end_date
2044             ,dnz_chr_id
2045     FROM   Okc_k_lines_b
2046     WHERE  id = p_line_id
2047     AND    lse_id = 25
2048     AND    date_cancelled is NULL --LLC BUG FIX 4742661
2049     AND    date_terminated is NULL;
2050 
2051 Cursor line_lse_id_cur (p_id IN NUMBER) is
2052 SELECT lse_id
2053       FROM okc_k_lines_b
2054       WHERE id = p_id;
2055 
2056 
2057  cur_rec       get_current_period_amount%ROWTYPE;
2058  p_freq        okc_rules_b.object1_id1%TYPE;
2059 
2060  l_amount      NUMBER;
2061  l_diff        NUMBER;
2062  l_id          NUMBER;
2063  l_lse_id      NUMBER;
2064  final_amount  NUMBER;
2065  l_ctr         BOOLEAN;
2066 
2067  BEGIN
2068    final_amount := 0;
2069 
2070    x_return_status := OKC_API.G_RET_STS_SUCCESS ;
2071 
2072 
2073    OPEN  line_lse_id_cur(p_line_id);
2074    FETCH line_lse_id_cur into l_lse_id;
2075    CLOSE line_lse_id_cur;
2076 
2077    l_ctr  := TRUE ; -- NORMAL PROCESSING
2078 
2079    IF (p_cov_line = 'N') THEN
2080      IF (l_lse_id = 19) THEN
2081        OPEN l_rel_csr(p_line_id) ;
2082        FETCH l_rel_csr into l_id;
2083        IF (l_rel_csr%FOUND ) THEN
2084          l_ctr := FALSE; --FOR EXT WAR BILLED FROM OM
2085        ELSE
2086          l_ctr := TRUE;  --FOR EXT WAR BILLED IN SC
2087        END IF;
2088        CLOSE l_rel_csr;
2089      END IF;
2090 
2091      IF (l_ctr = TRUE) THEN
2092        OPEN get_current_period_amount;
2093        FETCH get_current_period_amount into cur_rec;
2094 
2095        OPEN  rules_csr(cur_rec.id ,p_terminate_date);
2096        FETCH rules_csr into p_freq;
2097        CLOSE rules_csr;
2098 
2099        LOOP
2100          EXIT WHEN get_current_period_amount%NOTFOUND;
2101          -------------------------------------------------------------------------
2102          -- Begin partial period computation logic
2103          -- Developer Mani Choudhary
2104          -- Date 15-JUN-2005
2105          -------------------------------------------------------------------------
2106          IF p_period_type IS NOT NULL AND
2107             p_period_start IS NOT NULL AND
2108             l_lse_id = 12
2109          THEN
2110            l_amount:= Get_partial_term_amount(
2111                                     p_start_date        => cur_rec.date_billed_from,
2112                                     p_end_date          => cur_rec.date_billed_to,
2113                                     P_termination_date  => p_terminate_date,
2114                                     P_amount            => cur_rec.amount,
2115                                     P_uom               => p_freq,
2116                                     P_period_start      => P_period_start,
2117                                     P_period_type       => P_period_type
2118                                     );
2119             IF l_amount is NULL THEN
2120                RAISE G_EXCEPTION_HALT_VALIDATION;
2121             END IF;
2122          ELSE
2123            get_bill_amount_period(
2124                                 -99,
2125                                 cur_rec.start_date,
2126                                 cur_rec.date_billed_from,
2127                                 --p_con_end_date,
2128                                 p_freq,
2129                                 cur_rec.amount,--p_con_amount,
2130                                 cur_rec.date_billed_to,
2131                                 p_terminate_date,
2132                                 G_NONREGULAR,
2133                                 l_amount);
2134          END IF;
2135          Final_amount := nvl(Final_Amount,0) + nvl(l_amount,0);
2136 
2137          FETCH get_current_period_amount into cur_rec;
2138        END LOOP;
2139 
2140        CLOSE get_current_period_amount;
2141 
2142        OPEN get_future_period_amount;
2143        FETCH get_future_period_amount into l_amount;
2144        CLOSE get_future_period_amount;
2145 
2146        Final_amount := Final_Amount + l_amount;
2147 
2148      ELSE
2149        Final_amount := 0;
2150        l_amount := 0;
2151 
2152        FOR line_extwar_Rec in line_extwar_cur(p_line_id)
2153        LOOP
2154          l_diff := abs(TRUNC(line_extwar_Rec.end_date - line_extwar_Rec.start_date) + 1);
2155          l_amount := line_extwar_Rec.PRICE_NEGOTIATED/l_diff;
2156 
2157          IF (trunc(p_terminate_date) < trunc(line_extwar_Rec.start_date)) THEN
2158            l_diff :=  abs(trunc((line_extwar_Rec.end_date + 1 ) -
2159                                         trunc(line_extwar_Rec.start_date)));
2160          ELSE
2161            l_diff :=  abs(trunc((line_extwar_Rec.end_date + 1 ) -
2162                                         p_terminate_date));
2163          END IF;
2164 
2165          l_amount := (l_amount * l_diff);
2166          Final_amount := nvl(Final_Amount,0) + nvl(l_amount,0);
2167 
2168        END LOOP;
2169      END IF;
2170 
2171      x_amount     := Final_Amount;
2172    ELSE
2173      l_ctr := TRUE;
2174 
2175      IF (l_lse_id = 25) THEN
2176        OPEN l_rel_csr_cov(p_line_id) ;
2177        FETCH l_rel_csr_cov into l_id;
2178        IF (l_rel_csr_cov%FOUND ) THEN
2179          l_ctr := FALSE; --FOR EXT WAR BILLED FROM OM
2180        ELSE
2181          l_ctr := TRUE;  --FOR EXT WAR BILLED IN SC
2182        END IF;
2183        CLOSE l_rel_csr_cov;
2184      END IF;
2185 
2186      IF (l_ctr = TRUE) THEN
2187        OPEN get_cur_prd_amt_covlvl;
2188        FETCH get_cur_prd_amt_covlvl into cur_rec;
2189 
2190        OPEN  rules_csr(cur_rec.id ,p_terminate_date);
2191        FETCH rules_csr into p_freq;
2192        CLOSE rules_csr;
2193 
2194        LOOP
2195          EXIT WHEN get_cur_prd_amt_covlvl%NOTFOUND;
2196          -------------------------------------------------------------------------
2197          -- Begin partial period computation logic
2198          -- Developer Mani Choudhary
2199          -- Date 15-JUN-2005
2200          -------------------------------------------------------------------------
2201          IF p_period_type IS NOT NULL AND
2202             p_period_start IS NOT NULL AND
2203             l_lse_id = 13
2204          THEN
2205            l_amount:= Get_partial_term_amount(
2206                                     p_start_date        => cur_rec.date_billed_from,
2207                                     p_end_date          => cur_rec.date_billed_to,
2208                                     P_termination_date  => p_terminate_date,
2209                                     P_amount            => cur_rec.amount,
2210                                     P_uom               => p_freq,
2211                                     P_period_start      => P_period_start,
2212                                     P_period_type       => P_period_type
2213                                     );
2214             IF l_amount is NULL THEN
2215                RAISE G_EXCEPTION_HALT_VALIDATION;
2216             END IF;
2217          ELSE
2218 
2219            get_bill_amount_period(
2220                                  -99,
2221                                 cur_rec.start_date,
2222                                 cur_rec.date_billed_from,
2223                                 --p_con_end_date,
2224                                 p_freq,
2225                                 cur_rec.amount,--p_con_amount,
2226                                 cur_rec.date_billed_to,
2227                                 p_terminate_date,
2228                                 G_NONREGULAR,
2229                                 l_amount);
2230          END IF;
2231          Final_amount := Final_Amount + nvl(l_amount,0);
2232 
2233          FETCH get_cur_prd_amt_covlvl into cur_rec;
2234        END LOOP;
2235        CLOSE get_cur_prd_amt_covlvl;
2236 
2237        OPEN get_future_prd_amt_covlvl;
2238        FETCH get_future_prd_amt_covlvl into l_amount;
2239        CLOSE get_future_prd_amt_covlvl;
2240 
2241        Final_amount := Final_Amount + l_amount;
2242      ELSE
2243        Final_amount := 0;
2244        l_amount := 0;
2245        l_diff := 0;
2246 
2247        FOR subline_extwar_Rec in subline_extwar_cur(p_line_id)
2248        LOOP
2249          l_diff := abs(TRUNC(subline_extwar_Rec.end_date - subline_extwar_Rec.start_date) + 1);
2250          l_amount := subline_extwar_Rec.PRICE_NEGOTIATED/l_diff;
2251 
2252          IF (trunc(p_terminate_date)< trunc(subline_extwar_Rec.start_date)) THEN
2253 
2254            l_diff :=  abs(trunc((subline_extwar_Rec.end_date + 1 ) -
2255                                         trunc(subline_extwar_Rec.start_date)));
2256          ELSE
2257            l_diff :=  abs(trunc((subline_extwar_Rec.end_date + 1 ) -
2258                                         p_terminate_date));
2259          END IF;
2260 
2261          l_amount := (l_amount * l_diff);
2262          Final_amount := nvl(Final_Amount,0) + nvl(l_amount,0);
2263        END LOOP;
2264      END IF;
2265 
2266      x_amount     := Final_Amount;
2267    END IF;
2268 
2269 
2270 
2271  EXCEPTION
2272    WHEN G_EXCEPTION_HALT_VALIDATION THEN
2273         x_return_status := OKC_API.G_RET_STS_ERROR ;
2274    WHEN OTHERS THEN
2275      x_return_status := OKC_API.G_RET_STS_ERROR ;
2276  END;
2277 
2278 
2279 
2280 
2281 
2282 ------------------------------------------------------------------------
2283   -- FUNCTION create_term_recs
2284 ------------------------------------------------------------------------
2285   FUNCTION create_term_recs (
2286     P_CALLEDFROM                   IN         NUMBER,
2287     p_bcl_id_new                   IN         NUMBER,
2288     p_bcl_id                       IN         NUMBER,
2289     p_con_start_date               IN         DATE,
2290     p_con_end_date                 IN         DATE,
2291     p_freq                         IN         VARCHAR2,
2292     p_con_amount                   IN         NUMBER,
2293     p_term_date                    IN         DATE,
2294     p_term_amt                     IN         NUMBER,
2295     p_con_termination_amount       IN         NUMBER,
2296     p_stat                         IN         NUMBER,
2297     p_currency_code                IN         VARCHAR2,
2298     x_return_status                OUT NOCOPY VARCHAR2
2299 
2300   ) RETURN NUMBER IS
2301 
2302    SUBTYPE l_bslv_tbl_type_in  is OKS_bsl_PVT.bslv_tbl_type;
2303    l_bslv_tbl_in   l_bslv_tbl_type_in;
2304    l_bslv_tbl_out   l_bslv_tbl_type_in;
2305    SUBTYPE l_bsdv_tbl_type_in  is OKS_bsd_PVT.bsdv_tbl_type;
2306    l_bsdv_tbl_in   l_bsdv_tbl_type_in;
2307    l_bsdv_tbl_out   l_bsdv_tbl_type_in;
2308 
2309   CURSOR bsl_cur(id_in IN NUMBER) IS
2310          SELECT  bsl.id
2311                 ,bsl.cle_id
2312                 ,bsl.average
2313                 ,bsl.amount
2314                 ,bsl.Date_Billed_from
2315                 ,bsl.Date_Billed_to
2316                 ,bsl.Attribute_category
2317                 ,bsl.Attribute1
2318                 ,bsl.Attribute2
2319                 ,bsl.Attribute3
2320                 ,bsl.Attribute4
2321                 ,bsl.Attribute5
2322                 ,bsl.Attribute6
2323                 ,bsl.Attribute7
2324                 ,bsl.Attribute8
2325                 ,bsl.Attribute9
2326                 ,bsl.Attribute10
2327                 ,bsl.Attribute11
2328                 ,bsl.Attribute12
2329                 ,bsl.Attribute13
2330                 ,bsl.Attribute14
2331                 ,bsl.Attribute15
2332          FROM   oks_bill_sub_lines bsl,
2333                 okc_k_lines_b      okl
2334          WHERE  bcl_id = id_in
2335          AND    bsl.cle_id = okl.id
2336          AND    okl.date_cancelled is null --LLC BUG FIX 4742661
2337          AND    okl.date_terminated is null;
2338 
2339   CURSOR bsd_cur(id_in IN NUMBER) IS
2340          SELECT  bsl_id_averaged
2341                 ,bsd_id
2342                 ,bsd_id_applied
2343                 ,unit_of_measure
2344                 ,fixed
2345                 ,actual
2346                 ,default_default
2347                 ,amcv_yn
2348                 ,adjustment_level
2349                 ,adjustment_minimum
2350                 ,result
2351                 ,amount
2352                 ,start_reading
2353                 ,end_reading
2354                 ,ccr_id
2355                 ,cgr_id
2356                 ,Attribute_category
2357                 ,Attribute1
2358                 ,Attribute2
2359                 ,Attribute3
2360                 ,Attribute4
2361                 ,Attribute5
2362                 ,Attribute6
2363                 ,Attribute7
2364                 ,Attribute8
2365                 ,Attribute9
2366                 ,Attribute10
2367                 ,Attribute11
2368                 ,Attribute12
2369                 ,Attribute13
2370                 ,Attribute14
2371                 ,Attribute15
2372          FROM   oks_bill_sub_line_dtls
2373          WHERE  bsl_id = id_in;
2374 
2375    Cursor Bcl_Csr Is
2376          SELECT  Id
2377                  ,OBJECT_VERSION_NUMBER
2378                  ,CLE_ID
2379                  ,BTN_ID
2380                  ,DATE_BILLED_FROM
2381                  ,DATE_BILLED_TO
2382                  ,DATE_NEXT_INVOICE
2383                  ,AMOUNT
2384                  ,BILL_ACTION
2385                  ,ATTRIBUTE_CATEGORY
2386                  ,ATTRIBUTE1
2387                  ,ATTRIBUTE2
2388                  ,ATTRIBUTE3
2389                  ,ATTRIBUTE4
2390                  ,ATTRIBUTE5
2391                  ,ATTRIBUTE6
2392                  ,ATTRIBUTE7
2393                  ,ATTRIBUTE8
2394                  ,ATTRIBUTE9
2395                  ,ATTRIBUTE10
2396                  ,ATTRIBUTE11
2397                  ,ATTRIBUTE12
2398                  ,ATTRIBUTE13
2399                  ,ATTRIBUTE14
2400                  ,ATTRIBUTE15
2401          FROM  OKS_BILL_CONT_LINES_V
2402          WHERE  ID = p_bcl_id_new;
2403 
2404  Cursor cur_billinstance_sum (p_bcl_id IN NUMBER) is
2405   SELECT bill_instance_number ,bsl_id
2406   FROM  OKS_BILL_TXN_LINES txn
2407   WHERE bcl_id = p_bcl_id
2408   AND   bsl_id is null;
2409 
2410  Cursor cur_billinstance_dtl (p_bcl_id IN NUMBER,
2411                               p_cle_id IN NUMBER) is
2412   SELECT bill_instance_number ,bsl_id
2413   FROM  OKS_BILL_TXN_LINES txn
2414        ,OKS_BILL_SUB_LINES bsl
2415   WHERE txn.bcl_id     = p_bcl_id
2416   AND   bsl.cle_id = p_cle_id
2417   AND   bsl.id     = txn.bsl_id;
2418 
2419 
2420 
2421    bsl_rec bsl_cur%ROWTYPE;
2422    bsd_rec bsd_cur%ROWTYPE;
2423    bcl_rec bcl_csr%rowtype;
2424 
2425    bill_inst_rec_sum  cur_billinstance_sum%ROWTYPE;
2426    bill_inst_rec_dtl  cur_billinstance_dtl%ROWTYPE;
2427 
2428    l_ret_stat         VARCHAR2(20);
2429    l_msg_data         VARCHAR2(2000);
2430    l_msg_cnt          NUMBER;
2431    l_stat             NUMBER;
2432    l_amount           NUMBER ;
2433    l_round_amt        NUMBER ;
2434    l_return_status    VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
2435 
2436   BEGIN
2437 
2438 
2439     X_return_status := l_return_status;
2440 
2441     FOR bsl_rec IN bsl_cur(p_bcl_id)
2442     LOOP
2443 
2444       IF (trunc(p_term_date) <= trunc(bsl_rec.date_billed_to)) THEN
2445 
2446         IF (trunc(p_term_date) <= trunc(bsl_rec.date_billed_to)
2447            AND   trunc(p_term_date) <= trunc(bsl_rec.date_billed_from) ) THEN
2448           --errorout('SERVICE :- CREATE TERM G-REGULAR');
2449 
2450           l_stat := G_REGULAR;
2451         ELSE
2452           --errorout('SERVICE :- CREATE TERM G-NONREGULAR');
2453 
2454           l_stat := G_NONREGULAR;
2455         END IF;
2456 
2457         OPEN  bcl_csr;
2458         FETCH bcl_csr into bcl_rec;
2459         CLOSE bcl_csr;
2460 
2461         --l_bslv_tbl_in(1).OBJECT_VERSION_NUMBER:=bsl_rec.object_version_number;
2462         l_bslv_tbl_in(1).BCL_ID  := p_bcl_id_new;
2463         l_bslv_tbl_in(1).CLE_ID := bsl_rec.cle_id;
2464 
2465         /*Average field is used to store bill_instance_number
2466           of the parent. It is used to populate referenc_line_id in
2467           AR Feeder
2468         */
2469         OPEN  cur_billinstance_sum(p_bcl_id);
2470         FETCH cur_billinstance_sum into  bill_inst_rec_sum;
2471 
2472         IF (cur_billinstance_sum%FOUND) THEN --Summary billing for parent
2473           CLOSE cur_billinstance_sum;
2474           l_bslv_tbl_in(1).AVERAGE:= bill_inst_rec_sum.bill_instance_number;
2475         ELSE   --Detailed billing for parent
2476           CLOSE cur_billinstance_sum;
2477           OPEN  cur_billinstance_dtl(p_bcl_id,bsl_rec.cle_id);
2478           FETCH cur_billinstance_dtl into  bill_inst_rec_dtl;
2479 
2480           l_bslv_tbl_in(1).AVERAGE:= bill_inst_rec_dtl.bill_instance_number;
2481           CLOSE cur_billinstance_dtl;
2482         END IF;
2483 
2484 
2485         --l_bslv_tbl_in(1).AVERAGE := bsl_rec.average;
2486 
2487         IF ( bsl_rec.date_billed_from < p_term_date ) THEN
2488           l_bslv_tbl_in(1).DATE_BILLED_FROM := p_term_date;
2489         ELSE
2490           l_bslv_tbl_in(1).DATE_BILLED_FROM := bsl_rec.date_billed_from;
2491         END IF;
2492 
2493         l_bslv_tbl_in(1).DATE_BILLED_TO := bsl_rec.date_billed_to;
2494         l_bslv_tbl_in(1).ATTRIBUTE_CATEGORY := bsl_rec.attribute_category;
2495         l_bslv_tbl_in(1).ATTRIBUTE1 := bsl_rec.attribute1;
2496         l_bslv_tbl_in(1).ATTRIBUTE2 := bsl_rec.attribute2;
2497         l_bslv_tbl_in(1).ATTRIBUTE3 := bsl_rec.attribute3;
2498         l_bslv_tbl_in(1).ATTRIBUTE4 := bsl_rec.attribute4;
2499         l_bslv_tbl_in(1).ATTRIBUTE5 := bsl_rec.attribute5;
2500         l_bslv_tbl_in(1).ATTRIBUTE6 := bsl_rec.attribute6;
2501         l_bslv_tbl_in(1).ATTRIBUTE7 := bsl_rec.attribute7;
2502         l_bslv_tbl_in(1).ATTRIBUTE8 := bsl_rec.attribute8;
2503         l_bslv_tbl_in(1).ATTRIBUTE9 := bsl_rec.attribute9;
2504         l_bslv_tbl_in(1).ATTRIBUTE10 := bsl_rec.attribute10;
2505         l_bslv_tbl_in(1).ATTRIBUTE11 := bsl_rec.attribute11;
2506         l_bslv_tbl_in(1).ATTRIBUTE12 := bsl_rec.attribute12;
2507         l_bslv_tbl_in(1).ATTRIBUTE13 := bsl_rec.attribute13;
2508         l_bslv_tbl_in(1).ATTRIBUTE14 := bsl_rec.attribute14;
2509         l_bslv_tbl_in(1).ATTRIBUTE15 := bsl_rec.attribute15;
2510         l_bslv_tbl_in(1).date_to_interface :=  get_term_end_date (bcl_rec.cle_id , p_term_date );
2511 
2512         IF (l_stat = G_REGULAR) THEN
2513           IF (p_term_amt is not null) THEN
2514             l_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(
2515                                 ((p_term_amt * bsl_rec.amount)/
2516                                   p_con_termination_amount),p_currency_code);
2517             l_round_amt := l_amount;
2518             l_bslv_tbl_in(1).AMOUNT := -1 * l_amount;
2519           ELSE
2520             l_bslv_tbl_in(1).AMOUNT := -1 * (bsl_rec.amount );
2521             l_amount    := bsl_rec.amount;
2522             l_round_amt := bsl_rec.amount;
2523           END IF;
2524         ELSE
2525           get_bill_amount_period(
2526                                      -99,
2527                                      p_con_start_date,
2528                                      bsl_rec.date_billed_from,
2529                                      --p_con_end_date,
2530                                      p_freq,
2531                                      bsl_rec.amount,--p_con_amount,
2532                                      bsl_rec.date_billed_to,
2533                                      p_term_date,
2534                                      G_NONREGULAR,
2535                                      l_amount);
2536 
2537           IF (p_term_amt is not null) Then
2538             l_amount :=  OKS_EXTWAR_UTIL_PVT.round_currency_amt(
2539                                 ((p_term_amt * l_amount)/
2540                                   p_con_termination_amount),p_currency_code);
2541             l_round_amt := l_amount;
2542             l_bslv_tbl_in(1).AMOUNT := -1 * l_amount;
2543 
2544           ELSE
2545             l_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(
2546                                          l_amount, p_currency_code );
2547             l_round_amt := l_amount;
2548             l_bslv_tbl_in(1).AMOUNT := -1 * (l_amount );
2549           END IF;
2550 
2551         END IF;
2552 
2553         OKS_BILLSUBLINE_PUB.insert_Bill_subLine_Pub
2554             (
2555             p_api_version                  =>  1.0,
2556             p_init_msg_list                =>  'T',
2557             x_return_status                =>   l_return_status,
2558             x_msg_count                    =>   l_msg_cnt,
2559             x_msg_data                     =>   l_msg_data,
2560             p_bslv_tbl                     =>   l_bslv_tbl_in,
2561             x_bslv_tbl                     =>   l_bslv_tbl_out
2562             );
2563 
2564         --errorout('SERVICE :- CREATE TERM RECS INSERT BSL' || l_return_status);
2565 
2566 
2567         IF not l_return_status = OKC_API.G_RET_STS_SUCCESS THEN
2568           x_return_status := l_return_status;
2569           Raise G_EXCEPTION_HALT_VALIDATION;
2570         END IF;
2571 
2572         g_credit_amount := nvl(g_credit_amount,0) + l_amount;
2573         g_bsl_id        := l_bslv_tbl_out(1).id;
2574         g_bcl_id        := p_bcl_id_new;
2575 
2576         FOR bsd_rec IN bsd_cur(bsl_rec.id)
2577         LOOP
2578 
2579           l_bsdv_tbl_in(1).BSL_ID  := l_bslv_tbl_out(1).id;
2580           l_bsdv_tbl_in(1).BSL_ID_AVERAGED  := bsd_rec.bsl_id_averaged;
2581           l_bsdv_tbl_in(1).BSD_ID  := bsd_rec.bsd_id;
2582           l_bsdv_tbl_in(1).BSD_ID_APPLIED  := bsd_rec.bsd_id_applied;
2583           l_bsdv_tbl_in(1).UNIT_OF_MEASURE := bsd_rec.unit_of_measure;
2584           l_bsdv_tbl_in(1).FIXED := bsd_rec.fixed;
2585           l_bsdv_tbl_in(1).ACTUAL := bsd_rec.actual;
2586           l_bsdv_tbl_in(1).DEFAULT_DEFAULT := bsd_rec.default_default;
2587           l_bsdv_tbl_in(1).AMCV_YN := bsd_rec.amcv_yn;
2588           l_bsdv_tbl_in(1).ADJUSTMENT_LEVEL := bsd_rec.adjustment_level;
2589           l_bsdv_tbl_in(1).ADJUSTMENT_MINIMUM := bsd_rec.adjustment_minimum;
2590           l_bsdv_tbl_in(1).RESULT := bsd_rec.result;
2591           l_bsdv_tbl_in(1).ATTRIBUTE_CATEGORY := bsd_rec.attribute_category;
2592           l_bsdv_tbl_in(1).ATTRIBUTE1 := bsd_rec.attribute1;
2593           l_bsdv_tbl_in(1).ATTRIBUTE2 := bsd_rec.attribute2;
2594           l_bsdv_tbl_in(1).ATTRIBUTE3 := bsd_rec.attribute3;
2595           l_bsdv_tbl_in(1).ATTRIBUTE4 := bsd_rec.attribute4;
2596           l_bsdv_tbl_in(1).ATTRIBUTE5 := bsd_rec.attribute5;
2597           l_bsdv_tbl_in(1).ATTRIBUTE6 := bsd_rec.attribute6;
2598           l_bsdv_tbl_in(1).ATTRIBUTE7 := bsd_rec.attribute7;
2599           l_bsdv_tbl_in(1).ATTRIBUTE8 := bsd_rec.attribute8;
2600           l_bsdv_tbl_in(1).ATTRIBUTE9 := bsd_rec.attribute9;
2601           l_bsdv_tbl_in(1).ATTRIBUTE10 := bsd_rec.attribute10;
2602           l_bsdv_tbl_in(1).ATTRIBUTE11 := bsd_rec.attribute11;
2603           l_bsdv_tbl_in(1).ATTRIBUTE12 := bsd_rec.attribute12;
2604           l_bsdv_tbl_in(1).ATTRIBUTE13 := bsd_rec.attribute13;
2605           l_bsdv_tbl_in(1).ATTRIBUTE14 := bsd_rec.attribute14;
2606           l_bsdv_tbl_in(1).ATTRIBUTE15 := bsd_rec.attribute15;
2607           l_bsdv_tbl_in(1).start_reading := bsd_rec.start_reading;
2608           l_bsdv_tbl_in(1).end_reading := bsd_rec.end_reading;
2609           l_bsdv_tbl_in(1).ccr_id := bsd_rec.ccr_id;
2610           l_bsdv_tbl_in(1).cgr_id := bsd_rec.cgr_id;
2611 
2612           l_bsdv_tbl_in(1).AMOUNT := -1 * (l_round_amt );
2613 
2614           OKS_BSL_det_PUB.insert_bsl_det_Pub
2615                (
2616                   p_api_version                  =>  1.0,
2617                   p_init_msg_list                =>  'T',
2618                   x_return_status                =>   l_ret_stat,
2619                   x_msg_count                    =>   l_msg_cnt,
2620                   x_msg_data                     =>   l_msg_data,
2621                   p_bsdv_tbl                     =>   l_bsdv_tbl_in,
2622                   x_bsdv_tbl                     =>   l_bsdv_tbl_out
2623                );
2624 
2625           --errorout('SERVICE :- CREATE TERM RECS INSERT BSL DET ' || l_return_status);
2626 
2627           IF not l_return_status = OKC_API.G_RET_STS_SUCCESS THEN
2628             x_return_status := l_return_status;
2629             Raise G_EXCEPTION_HALT_VALIDATION;
2630           END IF;
2631 
2632           UPDATE oks_bill_cont_lines
2633             SET amount = bcl_rec.amount - nvl(l_round_amt,0)
2634             WHERE id   = bcl_rec.id;
2635 
2636 
2637         END LOOP; -- LOOP for BSD
2638       END If; -- p_termination_date <= bsl_rec.date_billed_to
2639          --exit;
2640     END LOOP;
2641     return 0;
2642 
2643   EXCEPTION
2644     WHEN  G_EXCEPTION_HALT_VALIDATION THEN
2645       x_return_status   :=   l_return_status;
2646     WHEN  OTHERS THEN
2647       x_return_status   :=   OKC_API.G_RET_STS_UNEXP_ERROR;
2648       OKC_API.set_message(G_APP_NAME,G_UNEXPECTED_ERROR, G_SQLCODE_TOKEN, SQLCODE,G_SQLERRM_TOKEN, SQLERRM);
2649 
2650   END create_term_recs;
2651 
2652 
2653 
2654 /*****
2655 Procedure create_bank_Account(
2656      p_dnz_chr_id      IN               NUMBER,
2657      p_bill_start_date IN               DATE,
2658      p_currency_code   IN               VARCHAR2,
2659      x_status         OUT NOCOPY        VARCHAR2,
2660      l_msg_count   IN OUT NOCOPY        NUMBER,
2661      l_msg_data    IN OUT NOCOPY        VARCHAR2)
2662   IS
2663 Cursor ccr_rule_csr(p_hdr_id IN NUMBER) Is
2664    SELECT rhdr.cc_no         ,
2665           rhdr.cc_expiry_date,
2666           rhdr.cc_bank_acct_id,
2667           hdr.bill_to_site_use_id
2668    FROM   oks_k_headers_b  rhdr,
2669           okc_k_headers_b  hdr
2670    WHERE  rhdr.chr_id   = hdr.id
2671    AND    hdr.id        = p_hdr_id;
2672 
2673 Cursor line_grp_csr(p_hdr_id IN NUMBER) Is
2674    SELECT  line.id                    line_id,
2675            rline.cc_no                line_cc_number,
2676            rline.cc_expiry_date       line_cc_exp_date,
2677            rline.cc_bank_acct_id      line_bank_number,
2678            line.bill_to_site_use_id   site_use_id
2679    FROM
2680              oks_k_lines_b      rline,
2681              okc_k_lines_b      line
2682    WHERE line.dnz_chr_id = p_hdr_id
2683    AND   line.lse_id  in (1,12,19)
2684    AND   line.date_cancelled is NULL --LLC BUG FIX 4742661
2685    AND   line.id = rline.cle_id;
2686 
2687 
2688 
2689 
2690 Cursor cust_acct_csr(p_object1_id1 IN  NUMBER) Is
2691   SELECT ca.cust_account_id
2692   FROM  hz_cust_acct_sites_all ca,
2693         hz_cust_site_uses_all cs
2694   WHERE ca.cust_acct_site_id = cs.cust_acct_site_id
2695   AND   cs.site_use_id = p_object1_id1;
2696 
2697 
2698 rule_bank_id              VARCHAR2(30);
2699 cc_number                 VARCHAR2(30);
2700 line_cc_number            VARCHAR2(30);
2701 cc_exp_date               DATE;
2702 cc_rule_id                NUMBER;
2703 cust_account_id           NUMBER;
2704 site_use_id               NUMBER;
2705 x_bank_account_id         NUMBER;
2706 x_bank_account_uses_id    NUMBER;
2707 l_api_version   CONSTANT  NUMBER      := 1.0;
2708 l_init_msg_list CONSTANT  VARCHAR2(1) := 'F';
2709 l_return_status           VARCHAR2(1);
2710 
2711 BEGIN
2712 
2713 
2714   cc_rule_id := NULL;
2715   rule_bank_id := NULL;
2716   x_status     := 'S';
2717 
2718   OPEN  ccr_rule_csr(p_dnz_chr_id);
2719   FETCH ccr_rule_csr into
2720                 cc_number, cc_exp_date,
2721                 rule_bank_id,
2722                 site_use_id;
2723   CLOSE ccr_rule_csr;
2724 
2725 
2726   IF (rule_bank_id is NULL) AND (cc_number is NOT NULL)  THEN
2727 
2728 
2729     OPEN  cust_acct_csr(site_use_id);
2730     FETCH cust_acct_csr into cust_account_id;
2731     CLOSE cust_acct_csr;
2732 
2733     x_bank_account_id := NULL;
2734 
2735     arp_bank_pkg.process_cust_bank_account
2736           (
2737            p_trx_date             => p_bill_start_date,
2738            p_currency_code        => p_currency_code,
2739            p_cust_id              => cust_account_id,
2740            p_site_use_id          => site_use_id,
2741            p_credit_card_num      => cc_number,
2742            p_exp_date             => cc_exp_date,
2743            p_bank_account_id      => x_bank_account_id,
2744            p_bank_account_uses_id => x_bank_account_uses_id
2745            );
2746 
2747 
2748 
2749     IF (x_bank_account_id IS NULL) THEN
2750       FND_FILE.PUT_LINE( FND_FILE.LOG, 'AR API returns with failure');
2751       x_status := 'E';
2752       Raise G_EXCEPTION_HALT_VALIDATION;
2753     ELSE
2754 
2755       UPDATE oks_k_headers_b
2756       SET cc_bank_acct_id  = x_bank_account_id
2757       WHERE chr_id =  p_dnz_chr_id;
2758 
2759     END IF; -- check null for bank account id
2760 
2761   END IF; -- for bank account id
2762 
2763   FOR line_cur in line_grp_csr(p_dnz_chr_id)
2764   LOOP
2765     IF ((line_cur.line_bank_number is NULL) AND
2766            (line_cur.line_cc_number is NOT NULL)) THEN
2767       site_use_id := NULL;
2768       cust_account_id := NULL;
2769       x_bank_account_id := NULL;
2770 
2771 
2772       OPEN  cust_acct_csr(line_cur.site_use_id);
2773       FETCH cust_acct_csr into cust_account_id;
2774       CLOSE cust_acct_csr;
2775 
2776       arp_bank_pkg.process_cust_bank_account
2777             (
2778              p_trx_date             => p_bill_start_date,
2779              p_currency_code        => p_currency_code,
2780              p_cust_id              => cust_account_id,
2781              p_site_use_id          => line_cur.site_use_id,
2782              p_credit_card_num      => line_cur.line_cc_number,
2783              p_exp_date             => line_cur.line_cc_exp_date,
2784              p_bank_account_id      => x_bank_account_id,
2785              p_bank_account_uses_id => x_bank_account_uses_id
2786             );
2787 
2788       IF (x_bank_account_id IS NULL) THEN
2789         FND_FILE.PUT_LINE( FND_FILE.LOG, 'Error in getting bank account  for line '||line_cur.line_id);
2790         x_status := 'E';
2791         Raise G_EXCEPTION_HALT_VALIDATION;
2792       ELSE
2793         UPDATE oks_k_lines_b
2794         SET cc_bank_acct_id = x_bank_account_id,
2795             cc_no           = line_cur.line_cc_number,
2796             cc_expiry_date  = line_Cur.line_cc_exp_date
2797         WHERE cle_id = line_cur.line_id;
2798       END IF;
2799 
2800 
2801     END IF; -- Check for cc number null or changed
2802   END LOOP;
2803 
2804 
2805 EXCEPTION
2806  WHEN G_EXCEPTION_HALT_VALIDATION THEN
2807     l_return_status   :=   OKC_API.G_RET_STS_UNEXP_ERROR;
2808     OKC_API.set_message(G_APP_NAME,G_UNEXPECTED_ERROR, G_SQLCODE_TOKEN, SQLCODE,G_SQLERRM_TOKEN, SQLERRM);
2809  WHEN OTHERS THEN
2810     l_return_status   :=   OKC_API.G_RET_STS_UNEXP_ERROR;
2811     OKC_API.set_message(G_APP_NAME,G_UNEXPECTED_ERROR, G_SQLCODE_TOKEN, SQLCODE,G_SQLERRM_TOKEN, SQLERRM);
2812 
2813 END create_bank_Account;
2814 ***/
2815 
2816 
2817 
2818 --------------------------------------------------------------------------------------------------------------------
2819                      -- Pre_terminate_extwar
2820 -------------------------------------------------------------------------------------------------------------------
2821 Procedure Pre_terminate_extwar
2822 (
2823     P_calleDfrom                 IN           NUMBER,
2824     p_line_id                    IN           NUMBER,
2825     p_termination_date           IN           DATE,
2826     p_suppress_credit            IN           VARCHAR2,
2827     p_termination_amount         IN           NUMBER,
2828     p_con_termination_amount     IN           NUMBER,
2829     p_cov_line                   IN           VARCHAR2,
2830     p_full_credit                IN           VARCHAR2,
2831     --p_existing_credit            IN           NUMBER,
2832     x_amount                     OUT  NOCOPY  NUMBER,
2833     X_return_status              OUT  NOCOPY  VARCHAR2
2834   )
2835 IS
2836 
2837 
2838 Cursor l_covlvl_csr (p_line_id in number,p_cov_line in Varchar)Is
2839       SELECT cle.Id
2840               ,cle.start_date
2841               ,cle.end_date
2842               ,cle.price_negotiated
2843               ,cle.cle_id
2844               ,TO_NUMBER(cim.object1_id1) instance_id
2845         FROM  Okc_k_lines_b cle,
2846                  okc_k_items cim
2847         WHERE cle.id = p_line_id
2848         AND  cle.lse_id = 25
2849         AND  cle.id = cim.cle_id;
2850 
2851 Cursor l_cov_csr (p_line_id in number,p_cov_line in Varchar)Is
2852        SELECT  Id
2853               ,start_date
2854               ,end_date
2855               ,price_negotiated
2856               ,cle_id
2857         FROM  Okc_k_lines_b
2858         WHERE (cle_id = p_line_id
2859                And  lse_id = 25
2860                AND  date_cancelled is null --LLC BUG FIX 4742661
2861                And  date_terminated is null) OR
2862               (id  = p_line_id
2863                And  lse_id = 25
2864                AND  date_cancelled is null --LLC BUG FIX 4742661
2865                And  date_terminated is null);
2866 
2867 
2868 Cursor l_extwar_csr(p_line_id in number) Is
2869        SELECT okl1.price_negotiated
2870               ,okl1.start_date
2871               ,okl1.end_date
2872               ,okl1.dnz_chr_id
2873               ,okl1.id
2874        FROM   Okc_k_lines_b     okl1
2875        WHERE  okl1.id = p_line_id
2876        AND    okl1.lse_id = 19
2877        AND    exists (Select 1 from okc_k_lines_b okl2
2878                       Where  okl2.cle_id = okl1.id
2879                       And    okl2.lse_id = 25
2880                       AND    okl2.date_cancelled is null --LLC BUG FIX 4742661
2881                       And    okl2.date_terminated is null);
2882 
2883 
2884 
2885 
2886 
2887 Cursor l_curr_csr(p_chr_id number) is
2888        SELECT  currency_code
2889        FROM okc_k_headers_b
2890        WHERE id = p_chr_id;
2891 
2892 Cursor qty_uom_csr(p_cle_id  Number) Is
2893      SELECt  okc.Number_of_items
2894             ,tl.Unit_of_measure uom_code
2895      FROM   OKC_K_ITEMS OKC
2896            ,mtl_units_of_measure_tl tl
2897      WHERE  okc.cle_id = P_cle_id
2898      AND    tl.uom_code = OKC.uom_code
2899      AND    tl.language = USERENV('LANG');
2900 
2901 -- BUG#3312595 mchoudha: Cursor to check for service request
2902 -- against the subline
2903 
2904 Cursor cur_subline_sr(p_id IN NUMBER,p_cp_id IN NUMBER) IS
2905 SELECT 'x'
2906 FROM CS_INCIDENTS_ALL_B  sr
2907 where sr.contract_service_id = p_id
2908 AND   sr.customer_product_id = p_cp_id
2909 and   sr.status_flag = 'O';
2910 
2911 Cursor cur_lineno(p_id IN NUMBER) IS
2912 SELECT p.line_number||'.'||s.line_number,
2913        hdr.contract_number
2914 FROM   okc_k_lines_b p,
2915        okc_k_lines_b s,
2916        okc_k_headers_b hdr
2917 WHERE  s.id=p_id
2918 AND    p.id=s.cle_id
2919 AND    hdr.id=p.dnz_chr_id;
2920 
2921 
2922 
2923 -- End BUG#3312595 mchoudha
2924 
2925 
2926    CURSOR  l_hdr_csr(p_id in NUMBER ) is
2927   SELECT dnz_chr_id
2928     FROM OKC_K_LINES_B
2929    WHERE id = p_id;
2930 
2931 
2932 l_extwar_rec   l_extwar_csr%rowtype;
2933 l_cov_rec      l_covlvl_csr%rowtype;
2934 
2935 SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
2936 l_bclv_tbl_in   l_bclv_tbl_type_in;
2937 l_bclv_tbl_out   l_bclv_tbl_type_in;
2938 SUBTYPE l_bslv_tbl_type_in  is OKS_bsl_PVT.bslv_tbl_type;
2939 l_bslv_tbl_in   l_bslv_tbl_type_in;
2940 l_bslv_tbl_out   l_bslv_tbl_type_in;
2941 
2942 SUBTYPE l_bsdv_tbl_type_in  is OKS_bsd_PVT.bsdv_tbl_type;
2943 
2944  L_BSDV_TBL_IN           L_BSDV_TBL_TYPE_IN;
2945  L_BSDV_TBL_OUT          L_BSDV_TBL_TYPE_IN;
2946  L_RET_STAT              VARCHAR2(1);
2947  L_MSG_CNT               NUMBER;
2948  L_MSG_DATA              VARCHAR2(2000);
2949  L_BILL_AMOUNT           NUMBER ;
2950  L_DIFF                  NUMBER;
2951  L_INDEX                 NUMBER;
2952  L_CURRENCY_CODE         VARCHAR2(15);
2953  L_ROUND_AMT             NUMBER;
2954  L_TOP_LINE_ID           NUMBER;
2955  L_COV_LINE_ID           NUMBER;
2956  --L_MANUAL_CREDIT         NUMBER;
2957  L_PROCESS_FLAG          BOOLEAN;
2958  L_RETURN_STATUS         VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
2959  QTY_UOM_REC             QTY_UOM_CSR%ROWTYPE;
2960  L_TERMINATION_DATE      DATE;
2961  L_MAX_DATE_BILLED_FROM  DATE;
2962  L_MAX_DATE_BILLED_TO    DATE;
2963  l_status_flag           VARCHAR2(1);
2964  l_line_number           VARCHAR2(500);
2965  l_contract_number       VARCHAR2(120);
2966 
2967 -------------------------------------------------------------------------
2968 -- Begin partial period computation logic
2969 -- Developer Mani Choudhary
2970 -- Date 30-MAY-2005
2971 -- local variables for partal periods
2972 -------------------------------------------------------------------------
2973 
2974 l_hdr_id              NUMBER;
2975 l_price_uom VARCHAR2(10);
2976 l_period_start VARCHAR2(30);
2977 l_period_type VARCHAR2(10);
2978 
2979 
2980 BEGIN
2981 
2982   X_return_status := l_return_status;
2983 
2984   l_process_flag  := TRUE;
2985 
2986    OPEN l_hdr_csr(p_line_id);
2987    FETCH l_hdr_csr into l_hdr_id;
2988    Close l_hdr_csr;
2989 
2990 -------------------------------------------------------------------------
2991 -- Begin partial period computation logic
2992 -- Developer Mani Choudhary
2993 -- Date 30-MAY-2005
2994 -- Call oks_renew_util_pub.get_period_defaults to fetch period start and period type
2995 -- 1)For extended warranty , period start will be 'SERVICE'
2996 -------------------------------------------------------------------------
2997    IF l_hdr_id IS NOT NULL THEN
2998 
2999     OKS_RENEW_UTIL_PUB.Get_Period_Defaults
3000                 (
3001                  p_hdr_id =>l_hdr_id,
3002                  p_org_id => NULL,    --p_org_id
3003                  x_period_start => l_period_start,
3004                  x_period_type => l_period_type,
3005                  x_price_uom => l_price_uom,
3006                  x_return_status => l_return_status);
3007 
3008     IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
3009      fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Pre_terminate_extwar.ppc_defaults',
3010     'After calling OKS_RENEW_UTIL_PUB.Get_Period_Defaults l_period_start ' ||l_period_start||' ,l_period_type '||l_period_type);
3011     END IF;
3012 
3013     IF l_return_status <> 'S' THEN
3014         RAISE G_EXCEPTION_HALT_VALIDATION;
3015     END IF;
3016 
3017    END IF;
3018    --mchoudha commented as per CR-001
3019    --IF l_period_start IS NOT NULL THEN
3020   --   l_period_start := 'SERVICE';
3021   -- END IF;
3022 -------------------------------------------------------------------------
3023 -- End partial period computation logic
3024 --------------------------------------------------------------------------
3025 
3026 
3027   -- BUG FIX 3438281. Added nvl to take care of contract created from OM.
3028   -- P_con_termination_amount is null when product is returned in OM
3029   IF (nvl(p_con_termination_amount,1)  > 0) THEN
3030     IF (p_cov_line = 'Y') THEN
3031 
3032       OPEN  l_covlvl_csr(p_line_id,p_cov_line);
3033       FETCH l_covlvl_csr into l_cov_rec;
3034       CLOSE l_covlvl_csr;
3035 
3036       -- BUG#3312595 mchoudha: Checking for service request
3037       -- against the subline
3038 
3039      -- Open Service Request Check should not be done for
3040      -- P_CALLEDFROM = -1. -1 is used for IB Integration
3041      -- No Other Callers should pass -1 as P_CALLEDFROM
3042 
3043      IF NVL(P_CALLEDFROM, 0) <> -1
3044      THEN
3045 
3046      OPEN  cur_subline_sr(l_cov_rec.cle_id,l_cov_rec.instance_id);
3047      FETCH cur_subline_sr into l_status_flag;
3048      CLOSE cur_subline_sr;
3049 
3050      IF(l_status_flag = 'x') THEN
3051 
3052        OPEN  cur_lineno(p_line_id);
3053        FETCH cur_lineno into l_line_number,l_contract_number;
3054        CLOSE cur_lineno;
3055 
3056 
3057        OKC_API.set_message(p_app_name      => g_app_name,
3058                         p_msg_name      => 'OKC_SR_PENDING',
3059                         p_token1        => 'NUMBER',
3060                         p_token1_value  => l_contract_number,
3061                         p_token2        => 'LINENO',
3062                         p_token2_value  =>  l_line_number);
3063 
3064        l_return_status := okc_api.g_ret_sts_error;
3065        raise  G_EXCEPTION_HALT_VALIDATION;
3066      END IF;
3067 
3068      END IF; --FOR P_CALLEDFROM
3069 
3070      -- END BUG#3312595 mchoudha
3071 
3072 
3073       l_cov_line_id   := p_line_id;
3074       l_top_line_id   := l_cov_rec.cle_id;
3075       --l_max_date_billed_from := l_cov_rec.start_date;
3076       --l_max_date_billed_to   := l_cov_rec.end_date;
3077 
3078 
3079       OPEN  l_extwar_csr(l_cov_rec.cle_id);
3080       FETCH l_extwar_csr into l_extwar_rec;
3081       IF (l_extwar_csr%NOTFOUND) THEN
3082         l_process_flag := FALSE;
3083       ELSE
3084         l_max_date_billed_from := l_extwar_rec.start_date;
3085         l_max_date_billed_to   := l_extwar_rec.end_date;
3086       END IF;
3087       CLOSE l_extwar_csr;
3088 
3089       IF p_full_credit = 'Y' then
3090         l_termination_date := l_cov_rec.start_date ;
3091       ELSIF p_full_credit = 'N' then
3092         l_termination_date := p_termination_date ;
3093       END IF;
3094     ELSE
3095 
3096       l_top_line_id   :=  p_line_id;
3097       l_cov_line_id   :=  NULL;
3098 
3099       OPEN  l_extwar_csr(p_line_id);
3100       FETCH l_extwar_csr into l_extwar_rec;
3101       IF (l_extwar_csr%NOTFOUND) THEN
3102         l_process_flag := FALSE;
3103       ELSE
3104         l_max_date_billed_from := l_extwar_rec.start_date;
3105         l_max_date_billed_to   := l_extwar_rec.end_date;
3106       END IF;
3107       CLOSE l_extwar_csr;
3108 
3109       IF p_full_credit = 'Y' then
3110         l_termination_date := l_extwar_rec.start_date ;
3111       ELSIF p_full_credit = 'N' then
3112         l_termination_date := p_termination_date ;
3113       END IF;
3114 
3115     END IF;
3116 
3117     IF (l_process_flag = TRUE) THEN
3118       IF (trunc(l_termination_date) >  trunc(l_max_date_billed_from)) THEN
3119           --(trunc(l_termination_date) >  trunc(l_cov_rec.start_date))) THEN
3120         l_max_date_billed_from :=  l_termination_date;
3121       END IF;
3122      -------------------------------------------------------------------------
3123       -- Begin partial period computation logic
3124       -- Developer Mani Choudhary
3125       -- Date 30-MAY-2005
3126      -------------------------------------------------------------------------
3127       IF l_period_start is not null AND
3128          l_period_type is not null
3129       THEN
3130         IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
3131            fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Pre_Terminate_Extwar',
3132             'calling OKS_BILL_REC_PUB.Terminate_PPC with parameters  P_period_start '||l_period_start||', P_period_type '||l_period_type
3133             ||' P_override_amount ' ||p_termination_amount||'p_con_terminate_amount '||p_con_termination_amount||' ,P_suppress_credit'||P_suppress_credit);
3134         END IF;
3135         OKS_BILL_REC_PUB.Terminate_PPC
3136                (P_termination_date => l_max_date_billed_from,
3137                 p_end_date         => l_max_date_billed_to,
3138                 P_top_line_id      => l_top_line_id,
3139                 P_cp_line_id       => l_cov_line_id,  --01-APR-2006 mchoudha passing l_cov_line_id
3140                 P_period_start     => l_period_start,
3141                 P_period_type      => l_period_type,
3142                 P_suppress_credit  => p_suppress_credit,
3143                 P_override_amount  => p_termination_amount,
3144                 p_con_terminate_amount => p_con_termination_amount,
3145                 x_return_status    => l_return_status);
3146         IF  l_return_status <> 'S' THEN
3147             RAISE G_EXCEPTION_HALT_VALIDATION;
3148         END IF;
3149       ELSE
3150 
3151           Create_trx_records(
3152                  P_CALLED_FROM           => 2,
3153                  P_TOP_LINE_ID           => l_top_line_id ,
3154                  P_COV_LINE_ID           => l_cov_line_id,
3155                  P_DATE_FROM             => l_max_date_billed_from,
3156                  P_DATE_TO               => l_max_date_billed_to,
3157                  P_AMOUNT                => 0,
3158                  P_OVERRIDE_AMOUNT       => p_termination_amount,
3159                  P_SUPPRESS_CREDIT       => p_suppress_credit,
3160                  P_CON_TERMINATE_AMOUNT  => p_con_termination_amount ,
3161                    --P_EXISTING_CREDIT       => p_existing_credit,
3162                  P_BILL_ACTION           => 'TR',
3163                  X_RETURN_STATUS         => l_return_status,
3164                  P_TERMINATION_DATE => l_termination_date    --skuchima 11691332
3165                  );
3166           IF  l_return_status <> 'S' THEN
3167              RAISE G_EXCEPTION_HALT_VALIDATION;
3168           END IF;
3169       END IF;
3170     END IF;  -- if  l_process_flag = true
3171   END IF;  --
3172 EXCEPTION
3173    WHEN  G_EXCEPTION_HALT_VALIDATION THEN
3174       x_return_status   :=   l_return_status;
3175    WHEN  OTHERS THEN
3176       x_return_status   :=   OKC_API.G_RET_STS_UNEXP_ERROR;
3177       OKC_API.set_message(G_APP_NAME,G_UNEXPECTED_ERROR, G_SQLCODE_TOKEN, SQLCODE,G_SQLERRM_TOKEN, SQLERRM);
3178 END;
3179 
3180 
3181 
3182 
3183 
3184 --================================================================================
3185                            --pre_terminate_srvc
3186 --=====================================================================================
3187  PROCEDURE pre_terminate_srvc
3188   (
3189     P_CALLEDFROM                   IN           NUMBER DEFAULT Null,
3190     p_k_line_id                    IN           NUMBER,
3191     p_termination_date             IN           DATE,
3192     p_flag                         IN           NUMBER, -- 1 - regular, 2- simu
3193     p_termination_amount           IN           NUMBER,
3194     p_con_termination_amount       IN           NUMBER,
3195     --p_existing_credit              IN                 NUMBER,
3196     p_suppress_credit              IN           VARCHAR2,
3197     p_full_credit                  IN           VARCHAR2,
3198     x_amount                       OUT NOCOPY   NUMBER,
3199     X_return_status                OUT NOCOPY   VARCHAR2
3200   )
3201   Is
3202 
3203    CURSOR kline_cur IS
3204            Select  Start_date
3205                 ,End_date
3206                 ,Price_negotiated
3207                 ,Date_terminated
3208          From   Okc_k_lines_b
3209          Where  cle_id = p_k_line_id
3210          And    date_cancelled is null --LLC BUG FIX 4742661
3211          And    date_terminated IS NULL
3212          And    lse_id in (7,8,9,10,11,13,35,25);
3213 
3214 
3215    /* Commented Cursor for Bug#14647106 */
3216    /*Cursor l_date_csr Is
3217          SELECT  line.Start_date
3218                 ,line.End_date
3219                 ,line.Date_Terminated
3220                 ,line.dnz_chr_id
3221                 ,line.lse_id
3222                 ,hdr.currency_code
3223          FROM    Okc_k_lines_B  line,
3224                  okc_k_headers_b hdr
3225          WHERE   line.Cle_id = p_k_line_id
3226          AND     line.dnz_chr_id = hdr.id;*/
3227 
3228    /* Added Cursor for Bug#14647106 */
3229    CURSOR l_date_csr Is
3230    SELECT max(line.End_date) end_date
3231      FROM okc_k_lines_B  line,
3232           okc_k_headers_b hdr
3233     WHERE line.Cle_id = p_k_line_id
3234       AND line.dnz_chr_id = hdr.id;
3235 
3236   CURSOR l_start_date_csr(p_id in NUMBER ) is
3237   SELECT start_date,lse_id
3238     FROM OKC_K_LINES_B
3239    WHERE id = p_k_line_id;
3240 
3241 
3242    CURSOR  l_hdr_csr(p_id in NUMBER ) is
3243   SELECT dnz_chr_id
3244     FROM OKC_K_LINES_B
3245    WHERE id = p_id;
3246 
3247   --mchoudha 10-FEB
3248   /* Modified cursor for Bug#14647106 */
3249   Cursor l_usage_csr(p_id in NUMBER) is
3250   SELECT s.usage_type,c.lse_id
3251     FROM oks_k_lines_b s, okc_k_lines_b c
3252    WHERE c.id = p_id
3253      AND s.cle_id = c.id;
3254 
3255   l_start_date_rec  l_start_date_csr%ROWTYPE;
3256 
3257   l_date_rec l_date_csr%rowtype;
3258 
3259   kline_rec  kline_cur%ROWTYPE;
3260 
3261   SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
3262   l_bclv_tbl_in   l_bclv_tbl_type_in;
3263   l_bclv_tbl_out  l_bclv_tbl_type_in;
3264 
3265   l_return_status    Varchar2(20);
3266   l_msg_cnt     Number;
3267   l_msg_data    Varchar2(2000);
3268    l_billed_amount          NUMBER ;
3269    l_term_amount            NUMBER ;
3270    l_term_amount_temp       NUMBER ;
3271    l_termination_date       DATE;
3272 
3273    -----------------------------------------------------------------------
3274 -- Begin partial period computation logic
3275 -- Developer Mani Choudhary
3276 -- Date 30-MAY-2005
3277 -- local variables for partal periods
3278 -------------------------------------------------------------------------
3279 l_hdr_id              NUMBER;
3280 l_price_uom           OKS_K_HEADERS_B.PRICE_UOM%TYPE;
3281 l_period_start        OKS_K_HEADERS_B.PERIOD_START%TYPE;
3282 l_period_type         OKS_K_HEADERS_B.PERIOD_TYPE%TYPE;
3283 l_usage_type          VARCHAR2(10);
3284 l_lse_id   NUMBER;    --Added for Bug#14647106
3285 --------------------------------------------------------------------------
3286 
3287  BEGIN
3288 
3289 
3290    x_return_status := OKC_API.G_RET_STS_SUCCESS;
3291    x_amount        := 0;
3292 
3293    OPEN l_hdr_csr(p_k_line_id);
3294    FETCH l_hdr_csr into l_hdr_id;
3295    Close l_hdr_csr;
3296 
3297 -------------------------------------------------------------------------
3298 -- Begin partial period computation logic
3299 -- Developer Mani Choudhary
3300 -- Date 30-MAY-2005
3301 -- Call oks_renew_util_pub.get_period_defaults to fetch period start and period type
3302 -------------------------------------------------------------------------
3303    IF l_hdr_id IS NOT NULL THEN
3304 
3305       OKS_RENEW_UTIL_PUB.Get_Period_Defaults
3306                 (
3307                  p_hdr_id        => l_hdr_id,
3308                  p_org_id        => NULL,
3309                  x_period_start  => l_period_start,
3310                  x_period_type   => l_period_type,
3311                  x_price_uom     => l_price_uom,
3312                  x_return_status => l_return_status);
3313 
3314       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
3315          fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.pre_terminate_srvc.ppc_defaults',
3316          'After calling OKS_RENEW_UTIL_PUB.Get_Period_Defaults l_period_start ' ||l_period_start||' ,l_period_type '||l_period_type);
3317       END IF;
3318 
3319       IF l_return_status <> 'S' THEN
3320         RAISE G_EXCEPTION_HALT_VALIDATION;
3321       END IF;
3322 
3323    END IF;
3324 -------------------------------------------------------------------------
3325 -- End partial period computation logic
3326 -------------------------------------------------------------------------
3327    IF (p_flag = 2) THEN
3328 
3329      --errorout('SERVICE :- SIMULATION  ');
3330 
3331 
3332      SELECT nvl(sum(nvl(amount,0)),0)
3333       INTO l_billed_amount
3334       FROM oks_bill_cont_lines
3335       WHERE cle_id = p_k_line_id
3336       GROUP by cle_id;
3337 
3338      FOR kline_rec IN kline_cur
3339      LOOP
3340         get_bill_amount_period(
3341                              -99,
3342                              kline_rec.start_date,
3343                              kline_rec.end_date,
3344                              Null,
3345                              kline_rec.PRICE_NEGOTIATED,
3346                              kline_rec.start_date,
3347                              kline_rec.DATE_TERMINATED,
3348                              G_NONREGULAR,
3349                              l_term_amount_temp);
3350         l_term_amount := nvl(l_term_amount,0) + nvl(l_term_amount_temp,0);
3351      END LOOP;
3352      x_amount := nvl(l_term_amount,0) - nvl(l_billed_amount,0);
3353    ELSE
3354      IF p_full_credit = 'Y' then
3355         OPEN  l_start_date_csr(p_k_line_id);
3356         FETCH l_start_date_csr into l_start_date_rec;
3357         CLOSE l_start_date_csr;
3358         l_termination_date := l_start_date_rec.start_date;
3359      ELSIF p_full_credit = 'N' then
3360         l_termination_date := p_termination_date;
3361      END IF;
3362 
3363      OPEN  l_date_csr;
3364      FETCH l_date_csr into l_date_rec;
3365      CLOSE l_date_csr;
3366      -------------------------------------------------------------------------
3367       -- Begin partial period computation logic
3368       -- Developer Mani Choudhary
3369       -- Date 30-MAY-2005
3370      -------------------------------------------------------------------------
3371 
3372      OPEN  l_usage_csr(p_k_line_id);
3373      FETCH l_usage_csr into l_usage_type,l_lse_id;   /* Modified for Bug#14647106*/
3374      CLOSE l_usage_csr;
3375 
3376      IF l_period_start IS NOT NULL AND
3377         l_period_type IS NOT NULL  AND
3378         p_termination_amount  is NULL --- Bug# 5005401 Overiding the credit amount must overide PPC
3379      THEN
3380         IF l_lse_id <> 13 OR nvl(l_usage_type,'XYZ') = 'NPR' THEN  /* Modified for Bug#14647106*/
3381           IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
3382              fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Pre_Terminate_srvc.Service',
3383              'calling OKS_BILL_REC_PUB.Terminate_PPC with parameters  l_period_start '||l_period_start||', l_period_type '||l_period_type
3384              ||' P_override_amount ' ||p_termination_amount||'p_con_terminate_amount '||p_con_termination_amount||' ,P_suppress_credit'||P_suppress_credit);
3385           END IF;
3386 
3387           OKS_BILL_REC_PUB.Terminate_PPC
3388                (P_termination_date => l_termination_date,
3389                 p_end_date         => l_date_rec.end_date,
3390                 P_top_line_id      => p_k_line_id,
3391                 P_cp_line_id       => NULL,
3392                 P_period_start     => l_period_start,
3393                 P_period_type      => l_period_type,
3394                 P_suppress_credit  => p_suppress_credit,
3395                 P_override_amount  => p_termination_amount,
3396                 p_con_terminate_amount => p_con_termination_amount,
3397                 x_return_status    => l_return_status);
3398 
3399           IF l_return_status <> 'S' THEN
3400              RAISE G_EXCEPTION_HALT_VALIDATION;
3401           END IF;
3402         ELSE
3403           --For usage , period start should be SERVICE
3404           -- and amount based termination will be based on billing not on price uom
3405           IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
3406              fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Pre_Terminate_srvc.Usage',
3407              'calling OKS_BILL_REC_PUB.Create_trx_records with parameters  l_period_start '||l_period_start||', l_period_type '||l_period_type
3408              ||' P_override_amount ' ||p_termination_amount||'p_con_terminate_amount '||p_con_termination_amount||' ,P_suppress_credit'||P_suppress_credit);
3409           END IF;
3410 
3411           l_period_start := 'SERVICE';
3412           Create_trx_records(
3413                 P_CALLED_FROM          => 1 ,
3414                 P_TOP_LINE_ID          => p_k_line_id,
3415                 P_COV_LINE_ID          => null,
3416                 P_DATE_FROM            => l_termination_date,
3417                 P_DATE_TO              => l_date_rec.end_date,
3418                 P_AMOUNT               => 0,
3419                 P_OVERRIDE_AMOUNT      => p_termination_amount,
3420                 P_SUPPRESS_CREDIT      => p_suppress_credit,
3421                 P_CON_TERMINATE_AMOUNT => p_con_termination_amount,
3422                 --P_EXISTING_CREDIT      => p_existing_credit,
3423                 P_BILL_ACTION          => 'TR',
3424                 P_PERIOD_START         => l_period_start,
3425                 P_PERIOD_TYPE          => l_period_type,
3426                 X_RETURN_STATUS        => l_return_status,
3427                 P_termination_date => l_termination_date --skuchima 11691332
3428                 );
3429           IF l_return_status <> 'S' THEN
3430              RAISE G_EXCEPTION_HALT_VALIDATION;
3431           END IF;
3432         END IF;
3433      ELSE
3434        Create_trx_records(
3435           P_CALLED_FROM          => 1 ,
3436           P_TOP_LINE_ID          => p_k_line_id,
3437           P_COV_LINE_ID          => null,
3438           P_DATE_FROM            => l_termination_date,
3439           P_DATE_TO              => l_date_rec.end_date,
3440           P_AMOUNT               => 0,
3441           P_OVERRIDE_AMOUNT      => p_termination_amount,
3442           P_SUPPRESS_CREDIT      => p_suppress_credit,
3443           P_CON_TERMINATE_AMOUNT => p_con_termination_amount,
3444           --P_EXISTING_CREDIT      => p_existing_credit,
3445           P_BILL_ACTION          => 'TR',
3446           X_RETURN_STATUS        => l_return_status,
3447           P_termination_date => l_termination_date --skuchima 11691332
3448           );
3449           IF l_return_status <> 'S' THEN
3450              RAISE G_EXCEPTION_HALT_VALIDATION;
3451           END IF;
3452      END IF;
3453    END IF;
3454 EXCEPTION
3455   WHEN  G_EXCEPTION_HALT_VALIDATION THEN
3456     x_return_status   :=   l_return_status;
3457   WHEN  OTHERS THEN
3458     x_return_status   :=   OKC_API.G_RET_STS_UNEXP_ERROR;
3459     OKC_API.set_message(G_APP_NAME,G_UNEXPECTED_ERROR, G_SQLCODE_TOKEN, SQLCODE,G_SQLERRM_TOKEN, SQLERRM);
3460 
3461 END pre_terminate_srvc;
3462 
3463 PROCEDURE terminate_subscribtion_line
3464   (
3465     P_CALLEDFROM                   IN         NUMBER DEFAULT NULL,
3466     p_api_version                  IN         NUMBER,
3467     p_init_msg_list                IN         VARCHAR2 DEFAULT OKC_API.G_FALSE,
3468     x_return_status                OUT NOCOPY VARCHAR2,
3469     x_msg_count                    OUT NOCOPY NUMBER,
3470     x_msg_data                     OUT NOCOPY VARCHAR2,
3471     p_line_id                      IN         NUMBER,
3472     p_termination_date             IN         DATE,
3473     p_termination_amount           IN         NUMBER DEFAULT NULL,
3474     p_con_termination_amount       IN         NUMBER DEFAULT NULL,
3475     p_billed_amount                IN         NUMBER,
3476     p_shipped_amount               IN         NUMBER,
3477     p_next_ship_date               IN         DATE,
3478     --p_existing_credit              IN         NUMBER,
3479     p_suppress_credit              IN         VARCHAR2 DEFAULT 'N',
3480     p_tang                         IN         BOOLEAN ,
3481     p_full_credit                  IN         VARCHAR2
3482   )  IS
3483 
3484  l_elems_tbl_del_in OKS_SUBSCR_ELEMS_PVT.scev_tbl_type;
3485 
3486  CURSOR del_fulfillment_schedule(p_id in number) is
3487   SELECT id from oks_subscr_elements
3488    where dnz_cle_id = p_id
3489    and   order_header_id is null
3490    and   start_date >= p_termination_date;
3491 
3492  CURSOR billed_cur (p_id in number,l_termination_date in DATE,
3493                                    l_next_ship_date in DATE) is
3494  SELECT * from oks_bill_cont_lines
3495   WHERE cle_id = p_id
3496     AND bill_action = 'RI'
3497     --AND date_billed_to >= l_termination_date
3498   ORDER BY DATE_BILLED_FROM DESC;
3499 
3500 /*************************************************************
3501  CURSOR billed_cur (p_id in number,l_termination_date in DATE,
3502                                    l_next_ship_date in DATE) is
3503  SELECT * from oks_bill_cont_lines
3504   WHERE cle_id = p_id
3505     AND bill_action = 'RI'
3506     AND (( date_billed_from >= l_termination_date
3507     AND    date_billed_to >= nvl(l_next_ship_date,date_billed_to))
3508         OR
3509         (l_termination_date between date_billed_from and date_billed_to
3510     AND nvl(l_next_ship_date,date_billed_from) between date_billed_from and date_billed_to))
3511   ORDER BY DATE_BILLED_FROM DESC;
3512 **************************************************************/
3513 
3514  Cursor bsl_cur (p_id in number) is
3515  SELECT bsl.*
3516  FROM oks_bill_sub_lines bsl
3517  WHERE bsl.bcl_id = p_id;
3518 
3519  Cursor bsd_cur (p_id in number) is
3520  Select * from oks_bill_sub_line_dtls
3521  where   bsl_id = p_id;
3522 
3523 
3524  Cursor check_bcl(p_id in Number,
3525                   p_date_billed_from IN Date,
3526                   p_date_billed_to In Date) is
3527       Select id
3528       from oks_bill_cont_lines
3529       where cle_id = p_id
3530       and   bill_Action = 'TR'
3531       and   trunc(date_billed_from) = trunc(p_date_billed_from)
3532       and   trunc(date_billed_to )  = trunc(p_date_billed_to);
3533 
3534  Cursor fullfilled_subscr_amount(p_id IN NUMBER) is
3535  Select nvl(sum(nvl(amount,0)),0)
3536  From oks_subscr_elements
3537  Where dnz_cle_id = p_id
3538  and start_date <= p_termination_date;
3539 
3540  Cursor bill_amount_cur(p_line_id in number,p_term_date in DATE) is
3541  Select nvl(sum(amount),0) from oks_bill_cont_lines
3542    Where cle_id = p_line_id
3543    and   bill_action = 'RI'
3544    and   trunc(date_billed_From) <= trunc(p_term_date);
3545 
3546  Cursor ship_amount_cur(p_line_id in number,p_term_date in DATE) is
3547  Select nvl(sum(amount),0) from oks_subscr_elements
3548    Where dnz_cle_id = p_line_id;
3549    --and   trunc(start_date) < trunc(p_term_date);
3550 
3551  Cursor l_get_bcl_count(l_termination_date in date, l_next_ship_date in date, l_line_id in NUMBER ) is
3552  select count(*)
3553   from oks_bill_cont_lines
3554  where cle_id = l_line_id
3555    and trunc(date_billed_to) >=  trunc(l_next_ship_date)
3556    and trunc(date_billed_to) >=  trunc(l_termination_date);
3557 
3558  CURSOR l_lines_csr( p_id NUMBER ) is
3559  SELECT start_date
3560    FROM okc_k_lines_b
3561   WHERE id = p_id ;
3562 
3563  SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
3564   l_bclv_tbl_in   l_bclv_tbl_type_in;
3565   l_bclv_tbl_out  l_bclv_tbl_type_in;
3566 
3567  SUBTYPE l_bslv_tbl_type_in  is OKS_bsl_PVT.bslv_tbl_type;
3568   l_bslv_tbl_in   l_bslv_tbl_type_in;
3569   l_bslv_tbl_out  l_bslv_tbl_type_in;
3570 
3571  SUBTYPE l_bsdv_tbl_type_in  is OKS_bsd_PVT.bsdv_tbl_type;
3572   l_bsdv_tbl_in   l_bsdv_tbl_type_in;
3573   l_bsdv_tbl_out  l_bsdv_tbl_type_in;
3574 
3575 --  Bug Fix 5062595 maanand --
3576 
3577   Cursor l_get_bcl_count_intang(l_termination_date in date, l_line_id in NUMBER ) is
3578   select count(*)
3579   from oks_bill_cont_lines
3580   where cle_id = l_line_id
3581   and trunc(date_billed_to) >=  trunc(l_termination_date);
3582 
3583 --  End Bug Fix 5062595 --
3584 
3585 --  Bug Fix 5236358  Hari --
3586 
3587 Cursor bill_instance_csr (p_bcl_id in NUMBER) is
3588        SELECT txn.bill_instance_number from oks_bill_txn_lines txn
3589        WHERE bcl_id = p_bcl_id;
3590 
3591 
3592  i                     NUMBER;
3593  l_id                  NUMBER;
3594  l_sum_billed_amount   NUMBER;
3595  l_sum_ship_amount     NUMBER;
3596  l_quantity            NUMBER;
3597  l_round_amount        NUMBER;
3598  l_amount              NUMBER;
3599  l_return_status       VARCHAR2(1);
3600  l_msg_count           NUMBER;
3601  l_msg_data            VARCHAR2(2000):=null;
3602  l_ship_amount         NUMBER;
3603  l_bill_amount         NUMBER;
3604  l_termination_date    DATE;
3605  l_next_ship_date      DATE;
3606  l_lines_rec           l_lines_csr%ROWTYPE;
3607  l_termination_amount  number;
3608  l_con_termination_amount number ;
3609  l_bcl_count           number;
3610  l_amount_per_period   number;
3611 
3612  NEGATIVE_TERM_AMOUNT  EXCEPTION;
3613 
3614  BEGIN
3615    IF p_full_credit = 'Y' then
3616      OPEN l_lines_csr(p_line_id);
3617      FETCH l_lines_csr into l_lines_rec;
3618      CLOSE l_lines_csr;
3619      l_termination_date := l_lines_rec.start_date;
3620      l_next_ship_date   := l_lines_rec.start_date;
3621    ELSE
3622      l_termination_date := p_termination_date;
3623      l_next_ship_date   := p_next_ship_date;
3624    END IF;
3625 
3626    IF (p_shipped_amount >= p_billed_amount  and p_full_credit <> 'Y' ) THEN
3627 
3628     ---Changed for bug#3514292
3629     --if p_tang is false then ship amount is always 0 and if it is not at billed then billed amt is
3630     ---also 0. so p_tang condition moved inside if.
3631 
3632        IF p_tang THEN
3633          OKS_SUBSCRIPTION_PUB.recreate_schedule
3634          ( p_api_version     => 1.0,
3635            p_init_msg_list   => 'T',
3636            x_return_status   => l_return_status,
3637            x_msg_count       => l_msg_count,
3638            x_msg_data        => l_msg_data,
3639            p_cle_id          => p_line_id,
3640            p_intent          => NULL,
3641            x_quantity        => l_quantity );
3642        END IF;
3643 
3644        OKS_BILL_SCH.Create_Subcription_bs
3645          ( p_top_line_id     => p_line_id,
3646            p_full_credit     => p_full_credit,
3647            x_return_status   => l_return_status,
3648            x_msg_count       => l_msg_count,
3649            x_msg_data        => l_msg_data);
3650    ELSE
3651       IF p_tang  then
3652          OKS_SUBSCRIPTION_PUB.recreate_schedule
3653             (p_api_version     => 1.0,
3654              p_init_msg_list   => 'T',
3655              x_return_status   => l_return_status,
3656              x_msg_count       => l_msg_count,
3657              x_msg_data        => l_msg_data,
3658              p_cle_id          => p_line_id,
3659              p_intent          => NULL,
3660              x_quantity        => l_quantity );
3661       END IF;
3662       OKS_BILL_SCH.Create_Subcription_bs
3663           ( p_top_line_id     => p_line_id,
3664             p_full_credit     => p_full_credit,
3665             x_return_status   => l_return_status,
3666             x_msg_count       => l_msg_count,
3667             x_msg_data        => l_msg_data);
3668 
3669       l_termination_amount     := p_termination_amount;
3670 
3671       OKS_BILL_REC_PUB.Pre_Terminate_Amount
3672        ( p_id             => p_line_id,
3673          p_terminate_date => l_termination_date,
3674          p_flag           => 1,
3675          X_Amount         => l_con_termination_amount,
3676          X_return_status  => l_return_status);
3677 
3678       If p_termination_amount is not null then
3679          l_con_termination_amount := l_con_termination_amount*(nvl(p_termination_amount,p_con_termination_amount)/p_con_termination_amount);
3680       End If;
3681 
3682       If l_termination_amount is not null then
3683 
3684          --  Bug Fix 5062595 maanand --
3685          IF p_tang  then
3686                  Open  l_get_bcl_count(l_termination_date , l_next_ship_date , p_line_id );
3687                  Fetch l_get_bcl_count into l_bcl_count;
3688                  close l_get_bcl_count ;
3689         ELSE
3690                  Open  l_get_bcl_count_intang(l_termination_date , p_line_id );
3691                  Fetch l_get_bcl_count_intang into l_bcl_count;
3692                  close l_get_bcl_count_intang ;
3693         END IF;
3694         --  End Bug Fix 5062595 --
3695 
3696          If (l_bcl_count = 0 or l_bcl_count is null ) then
3697             l_bcl_count := 1 ;
3698          end If;
3699          l_amount_per_period := l_con_termination_amount /l_bcl_count;
3700 
3701       End If;
3702 
3703       <<A>>
3704       FOR bcl_rec in billed_cur(p_line_id,l_termination_date,l_next_ship_date )
3705       LOOP
3706       BEGIN
3707         EXIT A WHEN (l_termination_amount <= 0 OR l_con_termination_amount <= 0  and p_full_credit <> 'Y' );
3708         DBMS_TRANSACTION.SAVEPOINT('BEFORE_TRANSACTION');
3709         l_bclv_tbl_in.delete;
3710         l_bclv_tbl_out.delete;
3711         l_bslv_tbl_in.delete;
3712         l_bslv_tbl_out.delete;
3713         l_bsdv_tbl_in.delete;
3714         l_bsdv_tbl_out.delete;
3715         OPEN check_bcl(bcl_rec.cle_id,
3716                        bcl_rec.date_billed_From,
3717                        bcl_rec.date_billed_to);
3718         FETCH check_bcl into l_id;
3719         IF (check_bcl%NOTFOUND) THEN
3720             --OPEN fullfilled_subscr_amount(bcl_rec.cle_id);
3721             --FETCH fullfilled_subscr_amount into l_sum_ship_amount;
3722             --CLOSE fullfilled_subscr_amount;
3723 
3724             l_bclv_tbl_in(1).CLE_ID             := bcl_rec.cle_id;
3725             l_bclv_tbl_in(1).BTN_ID             := Null;
3726             l_bclv_tbl_in(1).SENT_YN            :=  'N';
3727           --l_bclv_tbl_in(1).DATE_BILLED_FROM   := bcl_rec.date_billed_from;
3728 
3729             --bug#5245918 is Forward port fix for bug#5245719 (bug#5245719 is fix for bug#5239335)
3730             IF p_termination_date >= bcl_rec.date_billed_from and  p_termination_date <= bcl_rec.date_billed_to and not(p_tang) THEN
3731                 l_bclv_tbl_in(1).DATE_BILLED_FROM   := p_termination_date;
3732             ELSE
3733                 l_bclv_tbl_in(1).DATE_BILLED_FROM   := bcl_rec.date_billed_from;
3734            END IF;
3735 
3736             l_bclv_tbl_in(1).DATE_BILLED_TO     := bcl_rec.date_billed_to;
3737             l_bclv_tbl_in(1).DATE_NEXT_INVOICE  := bcl_rec.date_next_invoice;
3738             l_bclv_tbl_in(1).ATTRIBUTE_CATEGORY := bcl_rec.attribute_category;
3739             l_bclv_tbl_in(1).ATTRIBUTE1         := bcl_rec.attribute1;
3740             l_bclv_tbl_in(1).ATTRIBUTE2         := bcl_rec.attribute2;
3741             l_bclv_tbl_in(1).ATTRIBUTE3         := bcl_rec.attribute3;
3742             l_bclv_tbl_in(1).ATTRIBUTE4         := bcl_rec.attribute4;
3743             l_bclv_tbl_in(1).ATTRIBUTE5         := bcl_rec.attribute5;
3744             l_bclv_tbl_in(1).ATTRIBUTE6         := bcl_rec.attribute6;
3745             l_bclv_tbl_in(1).ATTRIBUTE7         := bcl_rec.attribute7;
3746             l_bclv_tbl_in(1).ATTRIBUTE8         := bcl_rec.attribute8;
3747             l_bclv_tbl_in(1).ATTRIBUTE9         := bcl_rec.attribute9;
3748             l_bclv_tbl_in(1).ATTRIBUTE10        := bcl_rec.attribute10;
3749             l_bclv_tbl_in(1).ATTRIBUTE11        := bcl_rec.attribute11;
3750             l_bclv_tbl_in(1).ATTRIBUTE12        := bcl_rec.attribute12;
3751             l_bclv_tbl_in(1).ATTRIBUTE13        := bcl_rec.attribute13;
3752             l_bclv_tbl_in(1).ATTRIBUTE14        := bcl_rec.attribute14;
3753             l_bclv_tbl_in(1).ATTRIBUTE15        := bcl_rec.attribute15;
3754             l_bclv_tbl_in(1).BILL_ACTION        := G_BILLACTION_TR;
3755             l_bclv_tbl_in(1).CURRENCY_CODE      := bcl_rec.currency_code;
3756             l_bclv_tbl_in(1).AMOUNT             :=  0;
3757             IF (nvl(p_suppress_credit,'N') = 'Y') THEN
3758                l_bclv_tbl_in(1).BTN_ID          := -44;
3759             END IF;
3760             OKS_BILLCONTLINE_PUB.insert_Bill_Cont_Line
3761                  ( p_api_version                => 1.0,
3762                    p_init_msg_list              => 'T',
3763                    x_return_status              => l_return_status,
3764                    x_msg_count                  => l_msg_count,
3765                    x_msg_data                   => l_msg_data,
3766                    p_bclv_tbl                   => l_bclv_tbl_in,
3767                    x_bclv_tbl                   => l_bclv_tbl_out);
3768 
3769            l_bclv_tbl_in(1).id      :=  l_bclv_tbl_out(1).id;
3770            l_bclv_tbl_in(1).amount  :=  l_bclv_tbl_out(1).amount;
3771            l_round_amount := 0;
3772 
3773            FOR bsl_rec in bsl_cur(bcl_rec.id)
3774            LOOP
3775               l_bslv_tbl_in(1).BCL_ID  := l_bclv_tbl_out(1).id;
3776               l_bslv_tbl_in(1).CLE_ID := bsl_rec.cle_id;
3777               l_bslv_tbl_in(1).AVERAGE := bsl_rec.average;
3778             --l_bslv_tbl_in(1).DATE_BILLED_FROM := bsl_rec.date_billed_from;
3779 
3780             --bug#5245918 is Forward port fix for bug#5245719 (bug#5245719 is fix for bug#5239335)
3781             IF p_termination_date >= bsl_rec.date_billed_from and  p_termination_date <= bsl_rec.date_billed_to and not(p_tang) THEN
3782                 l_bslv_tbl_in(1).DATE_BILLED_FROM   := p_termination_date;
3783              ELSE
3784                 l_bslv_tbl_in(1).DATE_BILLED_FROM   := bsl_rec.date_billed_from;
3785             END IF;
3786 
3787               l_bslv_tbl_in(1).DATE_BILLED_TO := bsl_rec.date_billed_to;
3788               l_bslv_tbl_in(1).ATTRIBUTE_CATEGORY :=bsl_rec.attribute_category;
3789               l_bslv_tbl_in(1).ATTRIBUTE1 := bsl_rec.attribute1;
3790               l_bslv_tbl_in(1).ATTRIBUTE2 := bsl_rec.attribute2;
3791               l_bslv_tbl_in(1).ATTRIBUTE3 := bsl_rec.attribute3;
3792               l_bslv_tbl_in(1).ATTRIBUTE4 := bsl_rec.attribute4;
3793               l_bslv_tbl_in(1).ATTRIBUTE5 := bsl_rec.attribute5;
3794               l_bslv_tbl_in(1).ATTRIBUTE6 := bsl_rec.attribute6;
3795               l_bslv_tbl_in(1).ATTRIBUTE7 := bsl_rec.attribute7;
3796               l_bslv_tbl_in(1).ATTRIBUTE8 := bsl_rec.attribute8;
3797               l_bslv_tbl_in(1).ATTRIBUTE9 := bsl_rec.attribute9;
3798               l_bslv_tbl_in(1).ATTRIBUTE10 := bsl_rec.attribute10;
3799               l_bslv_tbl_in(1).ATTRIBUTE11 := bsl_rec.attribute11;
3800               l_bslv_tbl_in(1).ATTRIBUTE12 := bsl_rec.attribute12;
3801               l_bslv_tbl_in(1).ATTRIBUTE13 := bsl_rec.attribute13;
3802               l_bslv_tbl_in(1).ATTRIBUTE14 := bsl_rec.attribute14;
3803               l_bslv_tbl_in(1).ATTRIBUTE15 := bsl_rec.attribute15;
3804               l_bslv_tbl_in(1).date_to_interface :=
3805               get_term_end_date (bsl_rec.cle_id , p_termination_date );
3806 
3807    If p_full_credit = 'Y' then
3808       If p_termination_amount is null then
3809          l_bslv_tbl_in(1).AMOUNT   :=  -1* bsl_rec.amount;
3810          l_amount                  := l_bslv_tbl_in(1).AMOUNT;
3811       Else
3812          --l_amount := p_termination_amount *(bsl_rec.amount/p_con_termination_amount);
3813          l_amount :=  l_amount_per_period;
3814          l_amount :=  OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_amount, bcl_rec.currency_code);
3815          l_round_amount := l_round_amount + l_amount ;
3816          l_bslv_tbl_in(1).AMOUNT   :=  -1* l_amount;
3817          l_amount                  := l_bslv_tbl_in(1).AMOUNT;
3818       End If;
3819    Else
3820       If p_termination_amount is null then
3821          If (l_con_termination_amount - bsl_rec.amount ) < 0 then
3822             If l_con_termination_amount < 0 then
3823                l_bslv_tbl_in(1).AMOUNT   := 0;
3824             Else
3825                l_bslv_tbl_in(1).AMOUNT   := -1*l_con_termination_amount ;
3826             End If;
3827             l_con_termination_amount  := l_con_termination_amount - l_con_termination_amount;
3828             l_amount := l_bslv_tbl_in(1).AMOUNT;
3829          Else
3830             l_bslv_tbl_in(1).AMOUNT   := -1 * bsl_rec.amount ;
3831             l_con_termination_amount  := l_con_termination_amount - bsl_rec.amount;
3832             l_amount := l_bslv_tbl_in(1).AMOUNT;
3833          End If;
3834       Else
3835          --l_amount :=  p_termination_amount *(bsl_rec.amount/p_con_termination_amount);
3836          l_amount :=  l_amount_per_period;
3837          l_amount :=  OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_amount, bcl_rec.currency_code);
3838          --l_termination_amount := l_termination_amount - l_amount ;
3839          l_con_termination_amount := l_con_termination_amount - l_amount ;
3840          l_round_amount := l_round_amount + l_amount ;
3841          l_bslv_tbl_in(1).AMOUNT   :=  -1* l_amount;
3842          l_amount                  := l_bslv_tbl_in(1).AMOUNT;
3843       End If;
3844    End If;
3845 
3846 /****************************************************************************************************************
3847               IF ((l_termination_date between bcl_rec.date_billed_From and bcl_rec.date_billed_to)
3848                  AND (l_next_ship_date  between bcl_rec.date_billed_From and bcl_rec.date_billed_to)
3849                       AND p_full_credit <> 'Y')  THEN
3850                       IF (p_termination_amount is NULL) THEN
3851                           OPEN  bill_amount_cur( p_line_id,l_termination_date);
3852                           FETCH bill_amount_cur into l_bill_amount;
3853                           CLOSE bill_amount_cur;
3854 
3855                           OPEN  ship_amount_cur( p_line_id,l_termination_date);
3856                           FETCH ship_amount_cur into l_ship_amount;
3857                           CLOSE ship_amount_cur;
3858 
3859                          l_amount  := OKS_EXTWAR_UTIL_PVT.round_currency_amt( l_bill_amount - l_ship_amount,
3860                                                                               bcl_rec.currency_code) ;
3861                          l_round_amount :=  l_round_amount +  l_amount;
3862                          l_bslv_tbl_in(1).AMOUNT   :=  -1* l_amount ;
3863                       ELSE
3864                          l_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(( p_termination_amount *
3865                                                                               bsl_rec.amount  ) / p_con_termination_amount  ,
3866                                                                                bcl_rec.currency_code);
3867                          l_round_amount :=  l_round_amount +  l_amount;
3868                          l_bslv_tbl_in(1).AMOUNT   :=  -1* l_amount ;
3869                      END IF;
3870              ELSE
3871                      IF (p_termination_amount is NULL) THEN
3872                         l_amount  := OKS_EXTWAR_UTIL_PVT.round_currency_amt(( bsl_rec.amount -
3873                                                                     OKS_SUBSCRIPTION_PUB.subs_termn_amount
3874                                                                     ( p_cle_id     =>p_line_id ,
3875                                                                       p_termn_date =>p_termination_date )),
3876                                                                       bcl_rec.currency_code) ;
3877                         l_round_amount :=  l_round_amount +  l_amount;
3878                         l_bslv_tbl_in(1).AMOUNT   :=  -1* l_amount ;
3879                      ELSE
3880                         l_amount:= OKS_EXTWAR_UTIL_PVT.round_currency_amt
3881                                    ((p_termination_amount *(( bsl_rec.amount - OKS_SUBSCRIPTION_PUB.subs_termn_amount
3882                                                                                            ( p_cle_id     =>p_line_id ,
3883                                                                                              p_termn_date =>p_termination_date ))
3884                                                                                              ))/
3885                                                                                              p_con_termination_amount  ,
3886                                                                                              bcl_rec.currency_code) ;
3887                         l_round_amount :=  l_round_amount +  l_amount;
3888                         l_bslv_tbl_in(1).AMOUNT   :=  -1* l_amount ;
3889                      END IF;
3890 
3891                      IF ( l_AMOUNT  <= 0) THEN
3892                           DBMS_TRANSACTION.ROLLBACK_SAVEPOINT('BEFORE_TRANSACTION');
3893                           RAISE NEGATIVE_TERM_AMOUNT;
3894                      END IF;
3895              END IF;
3896 ***********************************************************************************************************************/
3897 
3898 
3899       /*Average field is used to store bill_instance_number
3900         of the parent. It is used to populate referenc_line_id in
3901         AR Feeder
3902       */
3903       OPEN  bill_instance_csr(bcl_rec.id);
3904       FETCH bill_instance_csr into l_bslv_tbl_in(1).AVERAGE;
3905       CLOSE bill_instance_csr;
3906 
3907          OKS_BILLSUBLINE_PUB.insert_Bill_subLine_Pub
3908                (
3909                p_api_version                  =>  1.0,
3910                p_init_msg_list                =>  'T',
3911                x_return_status                =>   l_return_status,
3912                x_msg_count                    =>   l_msg_count,
3913                x_msg_data                     =>   l_msg_data,
3914                p_bslv_tbl                     =>   l_bslv_tbl_in,
3915                x_bslv_tbl                     =>   l_bslv_tbl_out
3916                );
3917 
3918          IF (l_return_status <> OKC_API.G_RET_STS_SUCCESS)  THEN
3919            x_return_status := l_return_status;
3920            Raise G_EXCEPTION_HALT_VALIDATION;
3921          END IF;
3922          g_credit_amount := nvl(g_credit_amount,0) + (-1*l_amount);
3923          g_bsl_id := l_bslv_tbl_out(1).id;
3924          g_bcl_id := l_bslv_tbl_in(1).BCL_ID;
3925 
3926 
3927          FOR bsd_rec in bsd_cur(bsl_rec.id)
3928          LOOP
3929            l_bsdv_tbl_in(1).BSL_ID              := l_bslv_tbl_out(1).id;
3930            l_bsdv_tbl_in(1).BSL_ID_AVERAGED     := bsd_rec.bsl_id_averaged;
3931            l_bsdv_tbl_in(1).BSD_ID              := bsd_rec.bsd_id;
3932            l_bsdv_tbl_in(1).BSD_ID_APPLIED      := bsd_rec.bsd_id_applied;
3933            l_bsdv_tbl_in(1).UNIT_OF_MEASURE     := bsd_rec.unit_of_measure;
3934            l_bsdv_tbl_in(1).FIXED               := bsd_rec.fixed;
3935            l_bsdv_tbl_in(1).ACTUAL              := bsd_rec.actual;
3936            l_bsdv_tbl_in(1).DEFAULT_DEFAULT     := bsd_rec.default_default;
3937            l_bsdv_tbl_in(1).AMCV_YN             := bsd_rec.amcv_yn;
3938            l_bsdv_tbl_in(1).ADJUSTMENT_LEVEL    := bsd_rec.adjustment_level;
3939            l_bsdv_tbl_in(1).ADJUSTMENT_MINIMUM  := bsd_rec.adjustment_minimum;
3940            l_bsdv_tbl_in(1).RESULT              := bsd_rec.result;
3941            l_bsdv_tbl_in(1).ATTRIBUTE_CATEGORY  := bsd_rec.attribute_category;
3942            l_bsdv_tbl_in(1).ATTRIBUTE1          := bsd_rec.attribute1;
3943            l_bsdv_tbl_in(1).ATTRIBUTE2          := bsd_rec.attribute2;
3944            l_bsdv_tbl_in(1).ATTRIBUTE3          := bsd_rec.attribute3;
3945            l_bsdv_tbl_in(1).ATTRIBUTE4          := bsd_rec.attribute4;
3946            l_bsdv_tbl_in(1).ATTRIBUTE5          := bsd_rec.attribute5;
3947            l_bsdv_tbl_in(1).ATTRIBUTE6          := bsd_rec.attribute6;
3948            l_bsdv_tbl_in(1).ATTRIBUTE7          := bsd_rec.attribute7;
3949            l_bsdv_tbl_in(1).ATTRIBUTE8          := bsd_rec.attribute8;
3950            l_bsdv_tbl_in(1).ATTRIBUTE9          := bsd_rec.attribute9;
3951            l_bsdv_tbl_in(1).ATTRIBUTE10         := bsd_rec.attribute10;
3952            l_bsdv_tbl_in(1).ATTRIBUTE11         := bsd_rec.attribute11;
3953            l_bsdv_tbl_in(1).ATTRIBUTE12         := bsd_rec.attribute12;
3954            l_bsdv_tbl_in(1).ATTRIBUTE13         := bsd_rec.attribute13;
3955            l_bsdv_tbl_in(1).ATTRIBUTE14         := bsd_rec.attribute14;
3956            l_bsdv_tbl_in(1).ATTRIBUTE15         := bsd_rec.attribute15;
3957            l_bsdv_tbl_in(1).AMOUNT              := l_amount;
3958 
3959            OKS_BSL_det_PUB.insert_bsl_det_Pub
3960                   (
3961                      p_api_version                  =>  1.0,
3962                      p_init_msg_list                =>  'T',
3963                      x_return_status                =>   l_return_status,
3964                      x_msg_count                    =>   l_msg_count,
3965                      x_msg_data                     =>   l_msg_data,
3966                      p_bsdv_tbl                     =>   l_bsdv_tbl_in,
3967                      x_bsdv_tbl                     =>   l_bsdv_tbl_out
3968                   );
3969 
3970 
3971            IF (l_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
3972              x_return_status := l_return_status;
3973              Raise G_EXCEPTION_HALT_VALIDATION;
3974            END IF;
3975 
3976 
3977          END LOOP;
3978 
3979        END LOOP;
3980 
3981        --l_bclv_tbl_in(1).AMOUNT :=  -1 * nvl(l_round_amount,0);
3982        l_bclv_tbl_in(1).AMOUNT :=  l_amount ;
3983 
3984        UPDATE oks_bill_cont_lines
3985              SET amount = l_bclv_tbl_in(1).AMOUNT
3986              WHERE  id  = l_bclv_tbl_in(1).id;
3987 
3988 
3989      END IF;  --check_bcl
3990 
3991      CLOSE check_bcl;
3992 
3993    EXCEPTION
3994      WHEN NEGATIVE_TERM_AMOUNT THEN
3995        NULL;
3996    END;
3997    END LOOP;
3998  END IF;
3999 END terminate_subscribtion_line;
4000 
4001 
4002 
4003 PROCEDURE pre_terminate
4004   (P_CALLEDFROM                   IN         NUMBER DEFAULT Null,
4005    x_return_status                OUT NOCOPY VARCHAR2,
4006    p_terminate_tbl                IN         TERMINATE_TBL
4007    ) IS
4008  BEGIN
4009    FOR i in 1..p_terminate_tbl.count
4010     LOOP
4011        pre_terminate
4012         (P_CALLEDFROM               => P_CALLEDFROM    ,
4013         x_return_status             => x_return_status,
4014         p_id                        => p_terminate_tbl(i).p_id,
4015         p_termination_date          => p_terminate_tbl(i).p_termination_date,
4016         p_termination_amount        => p_terminate_tbl(i).p_termination_amount,
4017         p_con_termination_amount    => p_terminate_tbl(i).p_con_termination_amount,
4018         p_reason_code               => p_terminate_tbl(i).p_reason_code ,
4019         p_flag                      => p_terminate_tbl(i).p_flag,
4020         p_termination_flag          => p_terminate_tbl(i).p_termination_flag,
4021         p_suppress_credit           => p_terminate_tbl(i).p_suppress_credit,
4022         p_full_credit               => p_terminate_tbl(i).p_full_credit,
4023         P_Term_Date_flag            => p_terminate_tbl(i).P_Term_Date_flag,
4024         P_Term_Cancel_source        => p_terminate_tbl(i).P_Term_Cancel_source
4025         );
4026 
4027     END LOOP;
4028  END;
4029 
4030 PROCEDURE pre_terminate
4031 (P_CALLEDFROM                   IN            NUMBER   DEFAULT Null,
4032  x_return_status                OUT NOCOPY    VARCHAR2,
4033  p_id                           IN            NUMBER,
4034  p_termination_date             IN            DATE,
4035  p_termination_amount           IN            NUMBER   ,-- user input for termination
4036  p_con_termination_amount       IN            NUMBER   DEFAULT NULL,-- actual value to be terminated
4037  p_reason_code                  IN            VARCHAR2 DEFAULT NULL,
4038  p_flag                         IN            NUMBER   DEFAULT NULL,
4039  p_termination_flag             IN            NUMBER   DEFAULT 1,
4040  p_suppress_credit              IN            VARCHAR2 DEFAULT 'N',
4041  p_full_credit                  IN            VARCHAR2,
4042 P_Term_Date_flag   IN varchar2 default 'N',
4043 P_Term_Cancel_source in Varchar2 default NULL
4044 )
4045 IS
4046 /*******************Modified this cursor as below for BUG # 3029249 ***
4047 Cursor line_cur(p_hdr_id IN NUMBER) is
4048   SELECT id,
4049          lse_id
4050   FROM okc_k_lines_b
4051   WHERE dnz_chr_id = p_hdr_id
4052   AND   cle_id is null
4053   AND   date_terminated is null --Sts_cd can be active for future terminate
4054   AND   sts_code in ('ACTIVE','SIGNED');
4055 *******************Modified this cursor as below for BUG # 3029249 ***/
4056 
4057 Cursor line_cur(p_hdr_id IN NUMBER) is
4058 SELECT lines.id,
4059        lines.lse_id,
4060        /* Start Addition for bug fix 6012384 (FP for 5469820) */
4061        lines.end_date
4062        /* End Addition for bug fix 6012384 (FP for 5469820) */
4063   FROM okc_k_lines_b lines,
4064        okc_k_headers_b  hdr
4065  WHERE hdr.id  =  p_hdr_id
4066    AND lines.dnz_chr_id = hdr.id
4067    AND lines.date_cancelled is null --LLC BUG FIX 4742661
4068    And lines.date_terminated is null
4069    And lines.cle_id is null
4070    And hdr.sts_code <> 'QA_HOLD'
4071    AND (exists ( SELECT 1 from okc_assents a
4072                  where Hdr.scs_code = a.scs_code
4073                    and lines.sts_code = a.sts_code
4074                    and lines.sts_code <> 'HOLD'
4075                    and a.opn_code = 'INVOICE'
4076                    and a.allowed_yn = 'Y')
4077         OR
4078         (lines.sts_code = 'HOLD'));
4079                    --And exists (Select 1 from okc_statuses_b osb,
4080                    --                          okc_k_headers_b hdr
4081                    --             Where osb.ste_code <> 'HOLD'
4082                    --               and osb.code =   Hdr.sts_code ));
4083 
4084 Cursor line_Det (p_line_id IN NUMBER) is
4085   SELECT lines.dnz_chr_id,
4086          lines.sts_code,
4087          lines.end_date,
4088          lines.lse_id,
4089          lines.line_number,
4090          lines.date_terminated
4091    FROM  okc_k_headers_b  hdr,
4092          okc_k_lines_b  lines
4093    WHERE lines.id = p_line_id
4094    AND   hdr.id  = lines.dnz_chr_id
4095    AND   hdr.sts_code <> 'QA_HOLD'
4096    AND  (exists ( SELECT 1 from okc_assents a
4097                  where Hdr.scs_code = a.scs_code
4098                    and lines.sts_code = a.sts_code
4099                    and lines.sts_code <> 'HOLD'
4100                    and a.opn_code = 'INVOICE'
4101                    and a.allowed_yn = 'Y')
4102          OR
4103          (lines.sts_code = 'HOLD'));
4104 
4105 
4106 Cursor hdr_Det (p_hdr_id IN NUMBER) is
4107   SELECT end_date,
4108          contract_number,
4109          contract_number_modifier
4110    FROM okc_k_headers_b
4111    WHERE id = p_hdr_id;
4112 
4113 Cursor fullfilled_subscr_amount(p_id IN NUMBER) is
4114 Select nvl(sum(nvl(amount,0)),0),max(end_date)
4115  From oks_subscr_elements
4116  Where dnz_cle_id = p_id
4117  And   order_header_id is not null;
4118 
4119 Cursor fullfilled_subscr_date(p_id IN NUMBER,p_termination_date IN date) is
4120 Select max(end_date)
4121  From oks_subscr_elements
4122  Where dnz_cle_id = p_id
4123    and start_date <= p_termination_date - 1 ;
4124 
4125 
4126 Cursor billed_subscr_amount(p_id IN NUMBER) is
4127 Select nvl(sum(nvl(bcl.amount,0)),0)
4128          from oks_bill_cont_lines bcl
4129          where bcl.cle_id = p_id
4130          and   bcl.bill_action = 'RI';
4131 
4132 Cursor unbilled_subscr_date (p_id in NUMBER) is
4133  Select min(om_interface_date)
4134     from oks_subscr_elements
4135     Where dnz_Cle_id = p_id
4136     and   order_header_id is null;
4137 
4138 
4139 Cursor line_billed_cur (p_id IN NUMBER) is
4140 Select id from oks_bill_cont_lines
4141          where cle_id = p_id
4142          and   bill_action = 'RI';
4143 
4144 Cursor sub_line_billed_cur (p_id IN NUMBER) is
4145 Select bsl.id from oks_bill_sub_lines bsl,
4146                         oks_bill_cont_lines bcl
4147          where bsl.cle_id = p_id
4148          and   bsl.bcl_id = bcl.id
4149          and   bcl.bill_action = 'RI';
4150 
4151 Cursor sub_fullfilment_channel(p_id IN NUMBER) is
4152 Select fulfillment_channel from oks_subscr_header_b
4153     where cle_id = p_id;
4154 
4155 Cursor bcl_amount(p_bcl_id in number) is
4156  Select nvl(amount,0),cle_id ,btn_id,bill_action
4157  from oks_bill_cont_lines
4158    where id = p_bcl_id;
4159 
4160 Cursor bsl_amount (p_bsl_id in number) is
4161  Select nvl(amount,0),cle_id
4162  from oks_bill_sub_lines
4163    where id = p_bsl_id;
4164 
4165 Cursor sub_line_tax(p_sub_line_id in number ) is
4166 Select tax_amount
4167   from oks_k_lines_b
4168  where cle_id = p_sub_line_id;
4169 
4170  CURSOR l_line_max_bill_date( p_line_id in NUMBER ) is
4171  SELECT max(bsl.date_billed_to) max_bill_date
4172    FROM oks_bill_cont_lines bcl
4173       , oks_bill_sub_lines bsl
4174   WHERE bcl.cle_id = p_line_id
4175     AND bcl.bill_action = 'RI'
4176     AND bcl.id = bsl.bcl_id ;
4177 
4178  CURSOR l_get_line_start_date(p_id in number ) is
4179  SELECT start_date
4180    FROM okc_k_lines_b
4181   WHERE id = p_id ;
4182 
4183 
4184 /* Added for BUG 3364773 */
4185 Cursor check_all_sublines_terminated(p_cle_id IN NUMBER) is
4186  SELECT id
4187  FROM okc_k_lines_b
4188  WHERE cle_id  = p_cle_id
4189  AND   lse_id in  (7,8,9,10,11,13,35,25)
4190   /* Start changes For bug fix 6012384 (FP for 5990067) */
4191  AND   sts_code IN(SELECT code FROM okc_statuses_b where ste_code NOT IN('TERMINATED','EXPIRED','CANCELLED'));   ---bug 12310525
4192  --AND   sts_code <> 'TERMINATED';
4193  /* End changes for bug fix 6012384 (FP for 5990067) */
4194 
4195 /* Added for BUG 3364773 */
4196 Cursor check_all_lines_terminated(p_hdr_id IN NUMBER) is
4197  SELECT id
4198  FROM okc_k_lines_b
4199  WHERE dnz_chr_id = p_hdr_id
4200   /* Start changes For bug fix 6012384 (FP for 5990067) */
4201   AND   lse_id in (1,12,14,19,46)
4202   --AND   lse_id in (1,12,14,19)
4203   /* End changes for bug fix 6012384 (FP for 5990067) */
4204   AND   cle_id is null
4205   /* Start changes For bug fix 6012384 (FP for 5990067) */
4206   AND   sts_code IN(SELECT code FROM okc_statuses_b where ste_code NOT IN('TERMINATED','EXPIRED','CANCELLED'));    ---bug 12310525
4207   --AND   sts_code <> 'TERMINATED';
4208   /* End changes for bug fix 6012384 (FP for 5990067) */
4209 
4210 -- BUG#3312595 mchoudha: Cursor to check for service request
4211 -- against the line
4212 
4213 Cursor cur_line_sr(p_id IN NUMBER) IS
4214 SELECT 'x'
4215 FROM CS_INCIDENTS_ALL_B  sr
4216 WHERE sr.contract_service_id = p_id
4217 AND   sr.status_flag = 'O';
4218 
4219 Cursor cur_lineno(p_id IN NUMBER) IS
4220 SELECT lin.line_number,hdr.contract_number
4221 FROM   okc_k_lines_b lin,
4222        okc_k_headers_b hdr
4223 WHERE  lin.id=p_id
4224 AND    hdr.id=lin.dnz_chr_id;
4225 
4226 -- End BUG#3312595 mchoudha
4227 
4228 -- Bug 4354983 TAKINTOY
4229 Cursor cur_hdr_sr IS
4230 SELECT 'x'
4231 FROM CS_INCIDENTS_ALL_B  sr
4232 WHERE sr.contract_id = p_id
4233 AND   sr.status_flag = 'O';
4234 
4235 Cursor cur_contract_num IS
4236 select contract_number
4237 from okc_k_headers_b
4238 where id=p_id;
4239 --END Bug 4354983
4240 
4241 Cursor avg_bcl_amount_partial (p_cle_id in number) is
4242  Select sum(bsl.amount)
4243    from oks_bill_cont_lines bcl,
4244         oks_bill_sub_lines  bsl
4245    where bcl.cle_id = p_cle_id
4246    and   bsl.bcl_id = bcl.id
4247    and   bcl.bill_action <> 'TR'
4248    and   trunc(bcl.date_billed_from) >= trunc(p_termination_date);
4249 
4250 Cursor avg_bcl_amount_full (p_cle_id in number) is
4251  Select sum(bsl.amount)
4252    from oks_bill_cont_lines bcl,
4253         oks_bill_sub_lines  bsl
4254    where bcl.cle_id = p_cle_id
4255    and   bsl.bcl_id = bcl.id
4256    and   bcl.bill_action <> 'TR';
4257 
4258 
4259 Cursor neg_bcl_amount_line (p_cle_id in number) is
4260  Select nvl(sum(decode(sign(trunc(bsl.date_billed_from) -   trunc(p_termination_date))  ,-1,
4261            ((trunc(bsl.date_billed_to) -  trunc(p_termination_date) + 1) * bsl.amount) /
4262             (trunc(bsl.date_billed_to) - trunc(bsl.date_billed_from) + 1) ,bsl.amount      )),0)
4263    -- nvl(sum(bsl.amount),0)
4264    from oks_bill_cont_lines bcl,
4265         oks_bill_sub_lines  bsl
4266    where bcl.cle_id = p_cle_id
4267    and   bsl.bcl_id = bcl.id
4268    and   bcl.bill_action <> 'TR'
4269    and   trunc(bsl.date_billed_from) >= trunc(p_termination_date)
4270    and   bsl.amount < 0;
4271 
4272 Cursor pos_bcl_amount_line (p_cle_id in number) is
4273  Select nvl(sum(decode(sign(trunc(bsl.date_billed_from) -   trunc(p_termination_date))  ,-1,
4274            ((trunc(bsl.date_billed_to) -  trunc(p_termination_date) + 1) * bsl.amount) /
4275             (trunc(bsl.date_billed_to) - trunc(bsl.date_billed_from) + 1) ,bsl.amount      )),0)
4276    from oks_bill_cont_lines bcl,
4277         oks_bill_sub_lines  bsl
4278    where bcl.cle_id = p_cle_id
4279    and   bsl.bcl_id = bcl.id
4280    and   bcl.bill_action <> 'TR'
4281    and   trunc(bsl.date_billed_from) >= trunc(p_termination_date)
4282    and   bsl.amount > 0;
4283 
4284 Cursor check_avg_csr(p_cle_id in number) is
4285  Select 1 from oks_bill_cont_lines
4286   where cle_id = p_cle_id
4287   and   bill_action = 'AV';
4288 
4289 Cursor neg_bcl_amount_hdr (p_hdr_id in number) is
4290  Select nvl(sum(decode(sign(trunc(bsl.date_billed_from) -   trunc(p_termination_date))  ,-1,
4291            ((trunc(bsl.date_billed_to) -  trunc(p_termination_date) + 1) * bsl.amount) /
4292             (trunc(bsl.date_billed_to) - trunc(bsl.date_billed_from) + 1) ,bsl.amount      )),0)
4293    from oks_bill_cont_lines bcl,
4294         oks_bill_sub_lines  bsl,
4295         okc_k_lines_b       line
4296    where bsl.bcl_id = bcl.id
4297    and   line.id = bcl.cle_id
4298    and   bcl.bill_action <> 'TR'
4299    and   line.dnz_chr_id = p_hdr_id
4300    and   line.lse_id = 12
4301    and   trunc(bsl.date_billed_from) >= trunc(p_termination_date)
4302    and   bsl.amount < 0;
4303 
4304 
4305 Cursor pos_bcl_amount_hdr (p_hdr_id in number) is
4306  Select nvl(sum(decode(sign(trunc(bsl.date_billed_from) -   trunc(p_termination_date))  ,-1,
4307            ((trunc(bsl.date_billed_to) -  trunc(p_termination_date) + 1) * bsl.amount) /
4308             (trunc(bsl.date_billed_to) - trunc(bsl.date_billed_from) + 1) ,bsl.amount      )),0)
4309    from oks_bill_cont_lines bcl,
4310         oks_bill_sub_lines  bsl,
4311         okc_k_lines_b       line
4312    where bsl.bcl_id = bcl.id
4313    and   line.id = bcl.cle_id
4314    and   bcl.bill_action <> 'TR'
4315    and   line.dnz_chr_id = p_hdr_id
4316    and   line.lse_id = 12
4317    and   trunc(bsl.date_billed_from) >= trunc(p_termination_date)
4318    and   bsl.amount > 0;
4319 
4320 l_msg_count                NUMBER;
4321 l_msg_data                 VARCHAR2(2000);
4322 l_fulfillment_channel      VARCHAR2(10);
4323 l_termn_method             VARCHAR2(10);
4324 l_usage_type               VARCHAR2(10);
4325 l_usage_period             VARCHAR2(10);
4326 l_amount                   NUMBER;
4327 l_chr_id                   NUMBER;
4328 l_number                   NUMBER;
4329 l_billed_amount            NUMBER;
4330 l_shipped_amount           NUMBER;
4331 l_termination_amount       NUMBER;
4332 l_con_termination_amount   NUMBER;
4333 l_dummy                    NUMBER;
4334 l_billed                   BOOLEAN;
4335 l_term_date                DATE;
4336 l_next_ship_date           DATE;
4337 l_max_term_date            DATE;
4338 l_credit_amount            NUMBER;
4339 --l_manual_credit          NUMBER;
4340 l_line_parameter_rec       OKC_TERMINATE_PVT.terminate_in_cle_rec;
4341 l_hdr_parameter_rec        OKC_TERMINATE_PVT.terminate_in_parameters_rec;
4342 l_tang                     BOOLEAN ;
4343 l_terminate                BOOLEAN ;
4344 l_line_failed              BOOLEAN ;
4345 l_sub_termn_amount         NUMBER;
4346 l_neg_amount               NUMBER;
4347 l_return_status            VARCHAR2(1);
4348 l_allowed_date             DATE;
4349 l_subscr_term_date         DATE;
4350 l_okc_status               VARCHAR2(10);
4351 l_status_flag              VARCHAR2(1);
4352 l_lineno                   VARCHAR2(150);
4353 l_contract_number          VARCHAR2(120);
4354 l_bcl_credit_amount        NUMBER;
4355 l_bsl_credit_amount        NUMBER;
4356 l_bcl_update_id            NUMBER;
4357 l_bsl_update_id            NUMBER;
4358 l_bcl_cle_id               NUMBER;
4359 l_bsl_cle_id               NUMBER;
4360 l_bcl_btn_id               NUMBER;
4361 l_bcl_bill_action          VARCHAR2(9);
4362 l_sub_line_tax_amount      NUMBER;
4363 G_RAIL_REC                 OKS_TAX_UTIL_PVT.ra_rec_type;
4364 
4365 
4366 SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
4367    l_bclv_tbl_in   l_bclv_tbl_type_in;
4368    l_bclv_tbl_out   l_bclv_tbl_type_in;
4369 SUBTYPE l_bslv_tbl_type_in  is OKS_bsl_PVT.bslv_tbl_type;
4370    l_bslv_tbl_in   l_bslv_tbl_type_in;
4371    l_bslv_tbl_out   l_bslv_tbl_type_in;
4372    l_true_value_tbl L_TRUE_VAL_TBL  ;
4373 
4374  LINE_FAILED  EXCEPTION;
4375  /* Start Addition for bug fix 6012384 (FP for 5990067) */
4376   SKIP_THIS_LINE   EXCEPTION;
4377  /* End Addition for bug fix 6012384 (FP for 5990067) */
4378 BEGIN
4379  DBMS_TRANSACTION.SAVEPOINT('BEFORE_PRE_TERMINATE');
4380  x_return_status := OKC_API.G_RET_STS_SUCCESS ;
4381  l_billed        := FALSE;
4382  l_terminate     := FALSE;
4383  l_termination_amount := p_termination_amount;
4384  l_con_termination_amount := p_con_termination_amount;
4385  g_bsl_id := NULL;
4386  g_bcl_id := NULL;
4387  g_credit_amount := 0;
4388 
4389 
4390   --dbms_output.put_line ('p_termination_amount ='||p_termination_amount);
4391   --dbms_output.put_line ('p_con_termination_amount ='||p_con_termination_amount);
4392  IF (p_flag = 1) Then -- p_id is line_id
4393 
4394     -- BUG#3312595 mchoudha:  checking for service request
4395     -- against the line
4396 
4397     OPEN cur_line_sr(p_id);
4398     FETCH cur_line_sr into l_status_flag;
4399     CLOSE cur_line_sr;
4400 
4401 
4402     IF (l_status_flag = 'x') THEN
4403 
4404       OPEN cur_lineno(p_id);
4405       FETCH cur_lineno into l_lineno,l_contract_number;
4406       CLOSE cur_lineno;
4407 
4408       OKC_API.set_message(p_app_name      => g_app_name,
4409                           p_msg_name      => 'OKC_SR_PENDING',
4410                           p_token1        => 'NUMBER',
4411                           p_token1_value  => l_contract_number,
4412                           p_token2        => 'LINENO',
4413                           p_token2_value  =>  l_lineno);
4414 
4415       x_return_status := okc_api.g_ret_sts_error;
4416       raise  G_EXCEPTION_HALT_VALIDATION;
4417     end if;
4418 
4419    -- END BUG#3312595 mchoudha
4420 
4421 
4422    OPEN  line_det(p_id);
4423    FETCH line_det into  l_line_parameter_rec.p_dnz_chr_id,
4424                         l_line_parameter_rec.p_sts_code,
4425                         l_line_parameter_rec.p_orig_end_date ,
4426                         l_line_parameter_rec.p_lse_id ,
4427                         l_line_parameter_rec.p_line_number ,
4428                         l_line_parameter_rec.p_date_terminated;
4429 
4430    IF  (line_det%FOUND) THEN
4431      l_line_parameter_rec.p_cle_id   := p_id;
4432      l_line_parameter_rec.p_termination_reason := p_reason_code;
4433      l_terminate := TRUE;
4434 
4435 
4436      IF (l_line_parameter_rec.p_lse_id = 12) THEN
4437        IF ( p_termination_amount IS NULL) THEN
4438 
4439         l_neg_amount := 0;
4440         OPEN  check_avg_csr(p_id);
4441         FETCH check_avg_csr into l_neg_amount;
4442         close check_avg_csr;
4443 
4444         if l_neg_amount <> 1  Then
4445 
4446           OPEN  neg_bcl_amount_line(p_id);
4447           FETCH neg_bcl_amount_line into l_neg_amount;
4448           IF (nvl(l_neg_amount,0) < 0) THEN
4449               OPEN  pos_bcl_amount_line(p_id);
4450               FETCH pos_bcl_amount_line into l_termination_amount;
4451               CLOSE pos_bcl_amount_line;
4452               l_termination_amount := nvl(l_con_termination_amount,0) +  nvl(l_neg_amount,0) ;
4453           END IF;
4454           CLOSE neg_bcl_amount_line;
4455 
4456         Else
4457             l_termination_amount := l_con_termination_amount;
4458         End if;
4459        END IF;
4460 
4461        OPEN line_billed_cur(p_id);
4462        FETCH line_billed_cur into l_number;
4463        IF (line_billed_cur%FOUND) THEN
4464          l_billed := TRUE;
4465        ELSE
4466          l_billed := FALSE;
4467        END IF;
4468        CLOSE line_billed_cur;
4469      ELSIF (l_line_parameter_rec.p_lse_id = 13) THEN
4470        OPEN sub_line_billed_cur(p_id);
4471        FETCH sub_line_billed_cur into l_number;
4472        IF (sub_line_billed_cur%FOUND) THEN
4473          l_billed := TRUE;
4474        ELSE
4475          l_billed := FALSE;
4476        END IF;
4477        CLOSE sub_line_billed_cur;
4478      ELSIF (l_line_parameter_rec.p_lse_id = 46) THEN
4479        OPEN billed_subscr_amount(p_id);
4480        FETCH billed_subscr_amount into l_billed_amount;
4481        IF (l_billed_amount  >  0) THEN
4482          l_billed := TRUE;
4483        ELSE
4484          l_billed := FALSE;
4485        END IF;
4486        CLOSE billed_subscr_amount;
4487 
4488        l_tang := OKS_SUBSCRIPTION_PUB.IS_SUBS_TANGIBLE(P_ID);
4489        IF l_tang THEN
4490          OPEN fullfilled_subscr_amount(p_id);
4491          FETCH fullfilled_subscr_amount into l_shipped_amount,l_next_ship_date;
4492          CLOSE fullfilled_subscr_amount;
4493          IF (l_next_ship_date is null) THEN
4494            OPEN  fullfilled_subscr_date (p_id , p_termination_date );
4495            FETCH fullfilled_subscr_date into l_next_ship_date;
4496            CLOSE fullfilled_subscr_date ;
4497          END IF;
4498        ELSE
4499          l_shipped_amount := 0;
4500          l_next_ship_date := NULL;
4501        END IF;
4502 
4503      END IF;
4504 
4505 
4506      IF l_line_parameter_rec.p_lse_id  = 46 THEN  -- Subscribtion line
4507        IF ((l_shipped_amount > l_billed_amount) AND
4508              (p_termination_date < l_next_ship_date))   THEN
4509          l_term_date := l_next_ship_date;
4510          l_line_parameter_rec.p_termination_date := l_next_ship_date;
4511        ELSE
4512          l_line_parameter_rec.p_termination_date := p_termination_date;
4513          l_term_date := p_termination_date;
4514        END IF;
4515      ELSE
4516        l_term_date := p_termination_date;
4517        l_line_parameter_rec.p_termination_date := p_termination_date;
4518      END IF;
4519 
4520      /*
4521      IF (l_line_parameter_rec.p_orig_end_date < l_term_date ) THEN
4522        l_line_parameter_rec.p_termination_date  := p_termination_date;
4523      ELSE
4524        l_line_parameter_rec.p_termination_date  := l_term_date;
4525      END IF;
4526      */
4527 
4528      IF (l_line_parameter_rec.p_lse_id = 46 )  THEN
4529        IF (p_full_credit = 'Y')  THEN
4530           OPEN  l_get_line_start_date(p_id);
4531           FETCH l_get_line_start_date into l_subscr_term_date;
4532           CLOSE l_get_line_start_date;
4533        ELSE
4534           l_subscr_term_date := l_term_date ;
4535        END IF;
4536 
4537        PRE_TERMINATE_AMOUNT
4538            ( P_CALLEDFROM     => 1.0 ,
4539              P_ID             => p_id,
4540              P_TERMINATE_DATE => l_subscr_term_date,
4541              P_FLAG           => 1 ,
4542              X_AMOUNT         => l_sub_termn_amount ,
4543              --X_MANUAL_CREDIT  => l_manual_credit,
4544              X_RETURN_STATUS  => l_return_status );
4545 
4546        IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
4547          raise G_EXCEPTION_HALT_VALIDATION;
4548        END IF;
4549 
4550        OKC_TERMINATE_PVT.terminate_cle
4551            ( P_API_VERSION                 => 1.0,
4552              P_INIT_MSG_LIST               => OKC_API.G_FALSE,
4553              X_RETURN_STATUS               => x_return_status,
4554              X_MSG_COUNT                   => l_msg_count,
4555              X_MSG_DATA                    => l_msg_data,
4556              P_TERMINATE_IN_PARAMETERS_REC => l_line_parameter_rec);
4557 
4558        ---For BUG#3372535 check for status S and W
4559         l_okc_status  := x_return_status;
4560 
4561         IF x_return_status NOT IN ( OKC_API.G_RET_STS_SUCCESS, 'W') Then
4562           raise G_EXCEPTION_HALT_VALIDATION;
4563         END IF;
4564 
4565        terminate_subscribtion_line
4566             (P_CALLEDFROM                   => p_calledfrom,
4567              P_API_VERSION                  => 1,
4568              P_INIT_MSG_LIST                => OKC_API.G_FALSE,
4569              X_RETURN_STATUS                => x_return_status,
4570              X_MSG_COUNT                    => l_msg_count,
4571              X_MSG_DATA                     => l_msg_data,
4572              P_LINE_ID                      => p_id,
4573              P_TERMINATION_DATE             => l_term_date,
4574              P_TERMINATION_AMOUNT           => l_termination_amount,
4575              P_CON_TERMINATION_AMOUNT       => l_con_termination_amount,
4576              P_BILLED_AMOUNT                => l_billed_amount,
4577              P_SHIPPED_AMOUNT               => l_shipped_amount,
4578              P_NEXT_SHIP_DATE               => l_next_ship_date,
4579              --P_EXISTING_CREDIT              => p_existing_credit,
4580              P_SUPPRESS_CREDIT              => p_suppress_credit ,
4581              P_TANG                         => l_tang,
4582              P_FULL_CREDIT                  => P_FULL_CREDIT );
4583 
4584        IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
4585          raise G_EXCEPTION_HALT_VALIDATION;
4586        END IF;
4587      IF ((l_termination_amount is NOT NULL) and (g_credit_amount <> l_termination_amount)) THEN
4588          OPEN  bsl_amount(g_bsl_id);
4589          FETCH bsl_amount into l_bsl_credit_amount,l_bsl_cle_id;
4590          CLOSE bsl_amount ;
4591 
4592          OPEN  bcl_amount(g_bcl_id);
4593          FETCH bcl_amount into l_bcl_credit_amount,l_bcl_cle_id,l_bcl_btn_id,l_bcl_bill_action;
4594          CLOSE bcl_amount ;
4595 
4596          If g_credit_amount < l_termination_amount then
4597             l_bsl_credit_amount :=  l_bsl_credit_amount +
4598                               ((-1)*(l_termination_amount - g_credit_amount)) ;
4599             l_bsl_update_id := g_bsl_id;
4600 
4601             UPDATE oks_bill_sub_lines
4602                SET amount = l_bsl_credit_amount
4603              WHERE id = l_bsl_update_id ;
4604 
4605             l_bcl_credit_amount :=  l_bcl_credit_amount +
4606                                ((-1)*(l_termination_amount - g_credit_amount)) ;
4607             l_bcl_update_id := g_bcl_id;
4608 
4609             UPDATE oks_bill_cont_lines
4610                SET amount = l_bcl_credit_amount
4611              WHERE id = l_bcl_update_id ;
4612 
4613          Elsif g_credit_amount > l_termination_amount then
4614 
4615             l_bsl_credit_amount :=  l_bsl_credit_amount + (g_credit_amount - l_termination_amount ) ;
4616             l_bsl_update_id := g_bsl_id;
4617 
4618             UPDATE oks_bill_sub_lines
4619                SET amount = l_bsl_credit_amount
4620              WHERE id  = l_bsl_update_id;
4621 
4622             l_bcl_credit_amount :=  l_bcl_credit_amount + (g_credit_amount - l_termination_amount) ;
4623             l_bcl_update_id := g_bcl_id;
4624 
4625             UPDATE oks_bill_cont_lines
4626                SET amount = l_bcl_credit_amount
4627              WHERE id    = l_bcl_update_id;
4628 
4629          End If;
4630      END IF;
4631      g_credit_amount := 0;
4632      g_bcl_id := null;
4633      g_bsl_id := null;
4634 
4635        l_true_value_tbl(1).p_cp_line_id           := 0;
4636        l_true_value_tbl(1).p_top_line_id          := p_id;
4637        l_true_value_tbl(1).p_hdr_id               := 0 ;
4638        l_true_value_tbl(1).p_termination_date     := p_termination_date;
4639        l_true_value_tbl(1).p_terminate_reason     := p_reason_code;
4640        l_true_value_tbl(1).p_override_amount      := l_termination_amount;
4641        l_true_value_tbl(1).p_con_terminate_amount := l_sub_termn_amount;
4642        l_true_value_tbl(1).p_termination_amount   := l_termination_amount;
4643        l_true_value_tbl(1).p_suppress_credit      := p_suppress_credit;
4644        l_true_valUe_tbl(1).p_full_credit          := p_full_credit ;
4645        True_value(l_true_value_tbl , x_return_status );
4646 
4647        IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
4648          raise G_EXCEPTION_HALT_VALIDATION;
4649        END IF;
4650 
4651      ELSE
4652        pre_terminate_service
4653           ( P_CALLEDFROM                   => p_calledfrom,
4654             P_API_VERSION                  => 1,
4655             X_RETURN_STATUS                => x_return_status,
4656             X_MSG_COUNT                    => l_msg_count,
4657             X_MSG_DATA                     => l_msg_data,
4658             P_K_LINE_ID                    => p_id,
4659             P_TERMINATION_DATE             => l_term_date,
4660             P_TERMINATION_AMOUNT           => l_termination_amount,  -- user input for termination
4661             P_CON_TERMINATION_AMOUNT       => l_con_termination_amount,  -- actual value to be terminated
4662             --P_EXISTING_CREDIT              => p_existing_credit,
4663             P_TERMINATION_FLAG             => p_termination_flag ,-- 1 - regular, 2- simulation
4664             P_SUPPRESS_CREDIT              => p_suppress_credit,
4665             P_FULL_CREDIT                  => P_FULL_CREDIT,
4666             X_AMOUNT                       => l_amount);
4667 
4668        IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
4669          raise G_EXCEPTION_HALT_VALIDATION;
4670        END IF;
4671 
4672        IF ((l_termination_amount is NOT NULL) AND
4673            (g_credit_amount <> l_termination_amount)) THEN
4674          OPEN  bsl_amount(g_bsl_id);
4675          FETCH bsl_amount into l_bsl_credit_amount,l_bsl_cle_id;
4676          CLOSE bsl_amount ;
4677 
4678          OPEN  bcl_amount(g_bcl_id);
4679          FETCH bcl_amount into l_bcl_credit_amount,l_bcl_cle_id,
4680                                l_bcl_btn_id,l_bcl_bill_action;
4681          CLOSE bcl_amount ;
4682 
4683          IF (g_credit_amount < l_termination_amount) THEN
4684            l_bsl_credit_amount :=  l_bsl_credit_amount +
4685                                ((-1)*(l_termination_amount - g_credit_amount)) ;
4686            l_bsl_update_id := g_bsl_id;
4687 
4688            UPDATE oks_bill_sub_lines
4689              SET amount = l_bsl_credit_amount
4690              WHERE id = l_bsl_update_id ;
4691 
4692            l_bcl_credit_amount :=  l_bcl_credit_amount +
4693                                ((-1)*(l_termination_amount - g_credit_amount));
4694            l_bcl_update_id := g_bcl_id;
4695 
4696            UPDATE oks_bill_cont_lines
4697              SET amount = l_bcl_credit_amount
4698              WHERE id = l_bcl_update_id ;
4699 
4700          ELSIF (g_credit_amount > l_termination_amount) THEN
4701 
4702            l_bsl_credit_amount :=  l_bsl_credit_amount +
4703                                  (g_credit_amount - l_termination_amount ) ;
4704            l_bsl_update_id := g_bsl_id;
4705 
4706            UPDATE oks_bill_sub_lines
4707              SET amount = l_bsl_credit_amount
4708              WHERE id  = l_bsl_update_id;
4709 
4710            l_bcl_credit_amount :=  l_bcl_credit_amount +
4711                                  (g_credit_amount - l_termination_amount) ;
4712            l_bcl_update_id := g_bcl_id;
4713 
4714            UPDATE oks_bill_cont_lines
4715              SET amount = l_bcl_credit_amount
4716              WHERE id    = l_bcl_update_id;
4717 
4718          END IF;
4719        END IF;
4720        g_credit_amount := 0;
4721        g_bcl_id := null;
4722        g_bsl_id := null;
4723      END IF;
4724 
4725      /* The following If Statement was added to take care of rounding problem in case of termination.
4726         If user inputs the termination amount ,program should give credits for the exact amount. There
4727          should not be any rounding errors */
4728 -- Code below is moved to call it before call to procedure true_value
4729 /************************************************
4730      IF ((l_termination_amount is NOT NULL) and (g_credit_amount <> l_termination_amount)) THEN
4731          OPEN  bsl_amount(g_bsl_id);
4732          FETCH bsl_amount into l_bsl_credit_amount,l_bsl_cle_id;
4733          CLOSE bsl_amount ;
4734 
4735          OPEN  bcl_amount(g_bcl_id);
4736          FETCH bcl_amount into l_bcl_credit_amount,l_bcl_cle_id,l_bcl_btn_id,l_bcl_bill_action;
4737          CLOSE bcl_amount ;
4738 
4739          If g_credit_amount < l_termination_amount then
4740             l_bsl_credit_amount :=  l_bsl_credit_amount +
4741                                ((-1)*(l_termination_amount - g_credit_amount)) ;
4742             l_bsl_update_id := g_bsl_id;
4743 
4744             UPDATE oks_bill_sub_lines
4745                SET amount = l_bsl_credit_amount
4746              WHERE id = l_bsl_update_id ;
4747 
4748             l_bcl_credit_amount :=  l_bcl_credit_amount +
4749                                ((-1)*(l_termination_amount - g_credit_amount)) ;
4750             l_bcl_update_id := g_bcl_id;
4751 
4752             UPDATE oks_bill_cont_lines
4753                SET amount = l_bcl_credit_amount
4754              WHERE id = l_bcl_update_id ;
4755 
4756          Elsif g_credit_amount > l_termination_amount then
4757 
4758             l_bsl_credit_amount :=  l_bsl_credit_amount + (g_credit_amount - l_termination_amount ) ;
4759             l_bsl_update_id := g_bsl_id;
4760 
4761             UPDATE oks_bill_sub_lines
4762                SET amount = l_bsl_credit_amount
4763              WHERE id  = l_bsl_update_id;
4764 
4765             l_bcl_credit_amount :=  l_bcl_credit_amount + (g_credit_amount - l_termination_amount) ;
4766             l_bcl_update_id := g_bcl_id;
4767 
4768             UPDATE oks_bill_cont_lines
4769                SET amount = l_bcl_credit_amount
4770              WHERE id    = l_bcl_update_id;
4771 
4772          End If;
4773      END IF;
4774 */
4775      g_credit_amount := 0;
4776      g_bcl_id := null;
4777      g_bsl_id := null;
4778 
4779 
4780 
4781    END IF ;  -- line_det%FOUND
4782    CLOSE line_det;
4783 
4784    IF (l_line_parameter_rec.p_lse_id <> 46 ) THEN
4785 
4786       /* Added for BUG 3364773.If all sublines  are already in TERMINATED
4787          status , then change the line status to terminate too
4788       */
4789      OPEN  check_all_sublines_terminated(p_id);
4790      FETCH check_all_sublines_terminated into l_dummy;
4791      IF (( l_terminate = TRUE) OR
4792         ((l_terminate = FALSE) AND (check_all_sublines_terminated%NOTFOUND))) THEN
4793 
4794        l_true_value_tbl(1).p_cp_line_id           := 0;
4795        l_true_value_tbl(1).p_top_line_id          := p_id;
4796        l_true_value_tbl(1).p_hdr_id               := 0 ;
4797        l_true_value_tbl(1).p_termination_date     := p_termination_date;
4798        l_true_value_tbl(1).p_terminate_reason     := p_reason_code;
4799        l_true_value_tbl(1).p_override_amount      := l_termination_amount;
4800        l_true_value_tbl(1).p_con_terminate_amount := l_con_termination_amount;
4801        l_true_value_tbl(1).p_termination_amount   := l_termination_amount;
4802        l_true_value_tbl(1).p_suppress_credit      := p_suppress_credit;
4803        l_true_valUe_tbl(1).p_full_credit          := p_full_credit ;
4804        True_value(l_true_value_tbl , x_return_status );
4805 
4806        IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
4807          raise G_EXCEPTION_HALT_VALIDATION;
4808        END IF;
4809 
4810 
4811        OKC_TERMINATE_PVT.terminate_cle
4812            ( P_API_VERSION                 => 1.0,
4813              P_INIT_MSG_LIST               => OKC_API.G_FALSE,
4814              X_RETURN_STATUS               => x_return_status,
4815              X_MSG_COUNT                   => l_msg_count,
4816              X_MSG_DATA                    => l_msg_data,
4817              P_TERMINATE_IN_PARAMETERS_REC => l_line_parameter_rec
4818             );
4819        -----for bug#3377509 check for S and W
4820         l_okc_status := x_return_status;
4821 
4822         IF x_return_status NOT IN (OKC_API.G_RET_STS_SUCCESS,'W') Then
4823           RAISE G_EXCEPTION_HALT_VALIDATION;
4824         END IF;
4825      END IF;
4826      CLOSE  check_all_sublines_terminated;
4827    END IF;
4828 
4829    update oks_k_lines_b topline set topline.tax_amount =  ( select sum(tax_amount) from
4830                   oks_k_lines_b oksline, okc_k_lines_b okcline
4831                   where okcline.id = oksline.cle_id
4832                     and okcline.cle_id = p_id
4833                     and okcline.date_cancelled is null )
4834    where  topline.cle_id = p_id;
4835 
4836    update oks_k_headers_b hdr set hdr.tax_amount =  ( select sum(tax_amount) from
4837                   oks_k_lines_b oksline, okc_k_lines_b okcline
4838                   where okcline.id = oksline.cle_id
4839                   and okcline.dnz_chr_id = l_line_parameter_rec.p_dnz_chr_id
4840                   and okcline.date_cancelled is null
4841                   and lse_id in (1,12,19,46) )
4842    where  hdr.chr_id = l_line_parameter_rec.p_dnz_chr_id;
4843 
4844  ELSIF  (p_flag = 2) Then -- p_id is hdr_id
4845 
4846  --Bug 4354983 check if there is any open Service request for the contract
4847 
4848     OPEN cur_hdr_sr;
4849     FETCH cur_hdr_sr into l_status_flag;
4850     CLOSE cur_hdr_sr;
4851 
4852 
4853     IF (l_status_flag = 'x') THEN
4854 
4855       OPEN cur_contract_num;
4856       FETCH cur_contract_num into l_contract_number;
4857       CLOSE cur_contract_num;
4858 
4859       OKC_API.set_message(p_app_name      => g_app_name,
4860                           p_msg_name      => 'OKC_SR_PENDING',
4861                           p_token1        => 'NUMBER',
4862                           p_token1_value  => l_contract_number);
4863       x_return_status := okc_api.g_ret_sts_error;
4864       raise  G_EXCEPTION_HALT_VALIDATION;
4865     end if;
4866 -- Bug 4354983 End
4867 
4868 /****
4869    IF ( p_termination_amount IS NULL) THEN
4870 
4871      OPEN  neg_bcl_amount_hdr(p_id);
4872      FETCH neg_bcl_amount_hdr into l_neg_amount;
4873      IF (nvl(l_neg_amount,0) <0 ) THEN
4874        OPEN  pos_bcl_amount_hdr(p_id);
4875        FETCH pos_bcl_amount_hdr into l_con_termination_amount;
4876        CLOSE pos_bcl_amount_hdr;
4877        l_termination_amount := nvl(l_con_termination_amount,0) +  nvl(l_neg_amount,0) ;
4878      END IF;
4879      CLOSE neg_bcl_amount_hdr;
4880 
4881    END IF;
4882 ****/
4883 
4884    l_line_failed := FALSE;
4885    l_max_term_date := p_termination_date;
4886 
4887    FOR line_rec in line_cur(p_id)
4888    LOOP
4889    BEGIN
4890    /* Start Addition for bug fix 6012384 (FP for 5990067) */
4891    	IF line_rec.end_date < p_termination_date AND p_full_credit = 'N' /* Modified for Bug# 7013317 */
4892    	THEN
4893    		RAISE SKIP_THIS_LINE;
4894    	END IF;
4895    /* End Addition for bug fix 6012384 (FP for 5990067) */
4896      l_billed     := FALSE;
4897      l_terminate  := TRUE;
4898 
4899 
4900 
4901      l_termination_amount := p_termination_amount;
4902      l_con_termination_amount := p_con_termination_amount;
4903 
4904      IF (line_rec.lse_id = 12) THEN
4905        OPEN line_billed_cur(p_id);
4906        FETCH line_billed_cur into l_number;
4907        IF (line_billed_cur%FOUND) THEN
4908          l_billed := TRUE;
4909        ELSE
4910          l_billed := FALSE;
4911        END IF;
4912        CLOSE line_billed_cur;
4913      ELSIF (line_rec.lse_id = 46) THEN
4914        OPEN billed_subscr_amount(line_rec.id);
4915        FETCH billed_subscr_amount into l_billed_amount;
4916        IF (l_billed_amount  >  0) THEN
4917          l_billed := TRUE;
4918        ELSE
4919          l_billed := FALSE;
4920        END IF;
4921        CLOSE billed_subscr_amount;
4922 
4923        l_tang := OKS_SUBSCRIPTION_PUB.IS_SUBS_TANGIBLE(line_rec.id);
4924 
4925        IF l_tang THEN
4926          OPEN fullfilled_subscr_amount(line_rec.id);
4927          FETCH fullfilled_subscr_amount into l_shipped_amount,l_next_ship_date;
4928          CLOSE fullfilled_subscr_amount;
4929 
4930          IF (l_next_ship_date is null) THEN
4931            OPEN  fullfilled_subscr_date (line_rec.id , p_termination_date );
4932            FETCH fullfilled_subscr_date into l_next_ship_date;
4933            CLOSE fullfilled_subscr_date ;
4934          END IF;
4935        ELSE
4936          l_shipped_amount := 0;
4937          l_next_ship_date := NULL;
4938        END IF;
4939 
4940      END IF;
4941 
4942      ----l_line_parameter_rec changed to line_rec for bug#3514292
4943 
4944      IF line_rec.lse_id  = 46 THEN  -- Subscribtion line
4945        IF ((l_shipped_amount > l_billed_amount) AND
4946                (p_termination_date < l_next_ship_date))   THEN
4947          l_term_date := l_next_ship_date;
4948        ELSE
4949          l_term_date := p_termination_date;
4950        END IF;
4951      ELSE
4952        l_term_date := p_termination_date;
4953      END IF;
4954 
4955      l_fulfillment_channel := NULL;
4956 
4957      IF (line_rec.lse_id = 46 ) THEN
4958        If p_full_credit = 'Y' then
4959           open  l_get_line_start_date(line_rec.id);
4960           fetch l_get_line_start_date into l_subscr_term_date;
4961           close l_get_line_start_date;
4962        Else
4963           l_subscr_term_date := l_term_date ;
4964        End If;
4965 
4966        PRE_TERMINATE_AMOUNT
4967            ( p_calledfrom     => 1.0 ,
4968              p_id             => line_rec.id,
4969              p_terminate_date => l_subscr_term_date,
4970              p_flag           => 1 ,
4971              x_amount         => l_sub_termn_amount ,
4972              --x_manual_credit  => l_manual_credit,
4973              x_return_status  => l_return_status );
4974 
4975        IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
4976          raise G_EXCEPTION_HALT_VALIDATION;
4977        END IF;
4978 
4979        OPEN  line_det(line_rec.id);
4980        FETCH line_det into     l_line_parameter_rec.p_dnz_chr_id,
4981                                l_line_parameter_rec.p_sts_code,
4982                                l_line_parameter_rec.p_orig_end_date ,
4983                                l_line_parameter_rec.p_lse_id ,
4984                                l_line_parameter_rec.p_line_number ,
4985                                l_line_parameter_rec.p_date_terminated;
4986        CLOSE line_det;
4987        l_line_parameter_rec.p_cle_id   := line_rec.id;
4988        l_line_parameter_rec.p_termination_reason := p_reason_code;
4989        l_line_parameter_rec.p_termination_date  := l_term_date;
4990 
4991        /*
4992        IF (l_line_parameter_rec.p_orig_end_date < l_term_date ) THEN
4993          l_line_parameter_rec.p_termination_date  := p_termination_date;
4994        ELSE
4995          l_line_parameter_rec.p_termination_date  := l_term_date;
4996        END IF;
4997        */
4998 
4999        IF (l_max_term_date < l_term_date) THEN
5000          l_max_term_date := l_term_date;
5001        END IF;
5002 
5003        OKC_TERMINATE_PVT.terminate_cle
5004               ( P_API_VERSION               => 1.0,
5005                 P_INIT_MSG_LIST               => OKC_API.G_FALSE,
5006                 X_RETURN_STATUS               => x_return_status,
5007                 X_MSG_COUNT                   => l_msg_count,
5008                 X_MSG_DATA                    => l_msg_data,
5009                 P_TERMINATE_IN_PARAMETERS_REC => l_line_parameter_rec
5010                 );
5011 
5012        ---For BUG#3372535 check for status S and W
5013         l_okc_status  := x_return_status;
5014 
5015         IF x_return_status NOT IN ( OKC_API.G_RET_STS_SUCCESS, 'W') Then
5016           raise G_EXCEPTION_HALT_VALIDATION;
5017         END IF;
5018 
5019        terminate_subscribtion_line
5020            ( P_CALLEDFROM                   => p_calledfrom,
5021              P_API_VERSION                  => 1,
5022              P_INIT_MSG_LIST                => OKC_API.G_FALSE,
5023              X_RETURN_STATUS                => x_return_status,
5024              X_MSG_COUNT                    => l_msg_count,
5025              X_MSG_DATA                     => l_msg_data,
5026              P_LINE_ID                      => line_rec.id,
5027              P_TERMINATION_DATE             => l_term_date,
5028              P_TERMINATION_AMOUNT           => l_termination_amount,
5029              P_CON_TERMINATION_AMOUNT       => l_con_termination_amount,
5030              P_BILLED_AMOUNT                => l_billed_amount,
5031              P_SHIPPED_AMOUNT               => l_shipped_amount,
5032              P_NEXT_SHIP_DATE               => l_next_ship_date,
5033              --P_EXISTING_CREDIT              => p_existing_credit,
5034              P_SUPPRESS_CREDIT              => p_suppress_credit ,
5035              P_TANG                         => l_tang ,
5036              P_FULL_CREDIT                  => P_FULL_CREDIT);
5037 
5038        l_true_value_tbl(1).p_cp_line_id           := 0;
5039        l_true_value_tbl(1).p_top_line_id          := line_rec.id;
5040        l_true_value_tbl(1).p_hdr_id               := p_id ;
5041        l_true_value_tbl(1).p_termination_date     := p_termination_date;
5042        l_true_value_tbl(1).p_terminate_reason     := p_reason_code;
5043        l_true_value_tbl(1).p_override_amount      := l_termination_amount;
5044        l_true_value_tbl(1).p_con_terminate_amount := l_sub_termn_amount;
5045        l_true_value_tbl(1).p_termination_amount   := l_termination_amount;
5046        l_true_value_tbl(1).p_suppress_credit      := p_suppress_credit;
5047        l_true_valUe_tbl(1).p_full_credit          := p_full_credit ;
5048        True_value(l_true_value_tbl , x_return_status );
5049 
5050        IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
5051          raise G_EXCEPTION_HALT_VALIDATION;
5052        END IF;
5053 
5054    --ELSE
5055 
5056      ELSE
5057        pre_terminate_service
5058             (
5059             P_CALLEDFROM                   => p_calledfrom,
5060             P_API_VERSION                  => 1,
5061             P_INIT_MSG_LIST                => OKC_API.G_FALSE,
5062             X_RETURN_STATUS                => x_return_status,
5063             X_MSG_COUNT                    => l_msg_count,
5064             X_MSG_DATA                     => l_msg_data,
5065             P_K_LINE_ID                    => line_rec.id,
5066             P_TERMINATION_DATE             => l_term_date,
5067             P_TERMINATION_AMOUNT           => l_termination_amount,  -- user input for termination
5068             P_CON_TERMINATION_AMOUNT       => l_con_termination_amount,  -- actual value to be terminated
5069             P_TERMINATION_FLAG             => p_termination_flag ,-- 1 - regular, 2- simulation
5070             P_SUPPRESS_CREDIT              => p_suppress_credit,
5071             P_FULL_CREDIT                  => P_FULL_CREDIT,
5072             X_AMOUNT                       => l_amount
5073             );
5074            IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
5075               raise G_EXCEPTION_HALT_VALIDATION;
5076            END IF;
5077 
5078            l_true_value_tbl(1).p_cp_line_id           := 0;
5079            l_true_value_tbl(1).p_top_line_id          := line_rec.id;
5080            l_true_value_tbl(1).p_hdr_id               := p_id ;
5081            l_true_value_tbl(1).p_termination_date     := p_termination_date;
5082            l_true_value_tbl(1).p_terminate_reason     := p_reason_code;
5083            l_true_value_tbl(1).p_override_amount      := l_termination_amount;
5084            l_true_value_tbl(1).p_con_terminate_amount := l_con_termination_amount;
5085            l_true_value_tbl(1).p_termination_amount   := l_termination_amount;
5086            l_true_value_tbl(1).p_suppress_credit      := p_suppress_credit;
5087            l_true_valUe_tbl(1).p_full_credit          := p_full_credit ;
5088            True_value(l_true_value_tbl , x_return_status );
5089            IF (x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
5090               raise G_EXCEPTION_HALT_VALIDATION;
5091            END IF;
5092 
5093      END IF;
5094 
5095    update oks_k_lines_b topline set topline.tax_amount =  ( select sum(tax_amount) from
5096                   oks_k_lines_b oksline, okc_k_lines_b okcline
5097                   where okcline.id = oksline.cle_id
5098                     and okcline.cle_id = line_rec.id
5099                     and okcline.date_cancelled is null )
5100    where  topline.cle_id = line_rec.id;
5101 
5102    EXCEPTION
5103      WHEN LINE_FAILED THEN
5104         l_line_failed := TRUE;
5105      /* Start Addition for bug fix 6012384 (FP for 5990067) */
5106      WHEN SKIP_THIS_LINE THEN
5107      	NULL;
5108      /* End Addition for bug fix 6012384 (FP for 5990067) */
5109    END;
5110    END LOOP;
5111 
5112    IF (l_terminate = TRUE ) THEN
5113 
5114        /* The following If Statement was added to take care of rounding problem in case of termination.
5115           If user inputs the termination amount ,program should give credits for the exact amount. There
5116           should not be any rounding errors */
5117 
5118 
5119        IF ((l_termination_amount is NOT NULL) AND (g_credit_amount <> l_termination_amount)) THEN
5120 
5121           OPEN  bsl_amount(g_bsl_id);
5122           FETCH bsl_amount into l_bsl_credit_amount,l_bsl_cle_id;
5123           CLOSE bsl_amount ;
5124 
5125           OPEN  bcl_amount(g_bcl_id);
5126           FETCH bcl_amount into l_bcl_credit_amount,l_bcl_cle_id,l_bcl_btn_id,l_bcl_bill_action;
5127           CLOSE bcl_amount ;
5128 
5129           If g_credit_amount < l_termination_amount then
5130               l_bsl_credit_amount :=  l_bsl_credit_amount +
5131                                ((-1)*(l_termination_amount - g_credit_amount)) ;
5132               l_bsl_update_id := g_bsl_id;
5133 
5134               UPDATE oks_bill_sub_lines
5135                  SET amount = l_bsl_credit_amount
5136                WHERE id = l_bsl_update_id ;
5137 
5138               l_bcl_credit_amount :=  l_bcl_credit_amount +
5139                               ((-1)*(l_termination_amount - g_credit_amount)) ;
5140               l_bcl_update_id := g_bcl_id;
5141 
5142               UPDATE oks_bill_cont_lines
5143                  SET amount = l_bcl_credit_amount
5144                WHERE id = l_bcl_update_id ;
5145               If l_bcl_btn_id = -44 and l_bcl_bill_action = 'TR' then
5146                  update oks_k_lines_b
5147                     set suppressed_credit = suppressed_credit + (l_termination_amount - g_credit_amount)
5148                       , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5149                  where cle_id = l_bsl_cle_id;
5150                  If l_bcl_cle_id <> l_bsl_cle_id then
5151                     update oks_k_lines_b
5152                        set suppressed_credit = suppressed_credit + (l_termination_amount - g_credit_amount)
5153                          , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5154                      where cle_id = l_bcl_cle_id;
5155                  End If;
5156               Elsif l_bcl_btn_id is null and l_bcl_bill_action = 'TR' then
5157                  update oks_k_lines_b
5158                     set credit_amount = credit_amount + (l_termination_amount - g_credit_amount)
5159                       , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5160                  where cle_id = l_bsl_cle_id;
5161                  If l_bcl_cle_id <> l_bsl_cle_id then
5162                     update oks_k_lines_b
5163                        set credit_amount = credit_amount + (l_termination_amount - g_credit_amount)
5164                          , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5165                      where cle_id = l_bcl_cle_id;
5166                  End If;
5167               End If;
5168               update okc_k_lines_b
5169                  set price_negotiated = price_negotiated - ( l_termination_amount - g_credit_amount )
5170               where id = l_bcl_cle_id
5171                 and lse_id <> 12;
5172               If l_bsl_cle_id <> l_bcl_cle_id then
5173                  update okc_k_lines_b
5174                     set price_negotiated = price_negotiated - ( l_termination_amount - g_credit_amount )
5175                   where id = l_bsl_cle_id
5176                     and lse_id <>13;
5177               End If;
5178               If sql%rowcount > 0 then
5179               update okc_k_headers_b
5180                  set estimated_amount = estimated_amount - ( l_termination_amount - g_credit_amount )
5181                where id =  p_id;
5182               End if;
5183               open sub_line_tax(l_bsl_cle_id);
5184               fetch sub_line_tax into l_sub_line_tax_amount;
5185               close sub_line_tax;
5186               If l_sub_line_tax_amount > 0 then
5187                     OKS_TAX_UTIL_PVT.Get_Tax
5188                      ( p_api_version      => 1.0,
5189                        p_init_msg_list    => OKC_API.G_TRUE,
5190                        p_chr_id           => p_id,
5191                        p_cle_id           => l_bsl_cle_id,
5192                        px_rail_rec        => G_RAIL_REC,
5193                        x_msg_count        => l_msg_count,
5194                        x_msg_data         => l_msg_data,
5195                        x_return_status    => l_return_status);
5196                     IF ( x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
5197                        RAISE G_EXCEPTION_HALT_VALIDATION;
5198                     END IF;
5199                     If G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG = 'N' then
5200                        update oks_k_lines_b
5201                           set tax_amount = G_RAIL_REC.tax_value
5202                         where cle_id = l_bsl_cle_id;
5203 
5204                     End If;
5205               End If;
5206           Elsif g_credit_amount > l_termination_amount then
5207               l_bsl_credit_amount :=  l_bsl_credit_amount + (g_credit_amount - l_termination_amount ) ;
5208               l_bsl_update_id := g_bsl_id;
5209 
5210               UPDATE oks_bill_sub_lines
5211                  SET amount = l_bsl_credit_amount
5212                WHERE id  = l_bsl_update_id ;
5213 
5214               l_bcl_credit_amount :=  l_bcl_credit_amount + (g_credit_amount - l_termination_amount) ;
5215               l_bcl_update_id := g_bcl_id;
5216 
5217               UPDATE oks_bill_cont_lines
5218                  SET amount = l_bcl_credit_amount
5219                WHERE id    = l_bcl_update_id ;
5220               If l_bcl_btn_id = -44 and l_bcl_bill_action = 'TR' then
5221                  update oks_k_lines_b
5222                     set suppressed_credit = suppressed_credit + (l_termination_amount - g_credit_amount)
5223                       , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5224                  where cle_id = l_bsl_cle_id;
5225                  If l_bcl_cle_id <> l_bsl_cle_id then
5226                     update oks_k_lines_b
5227                        set suppressed_credit = suppressed_credit + (l_termination_amount - g_credit_amount)
5228                       , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5229                      where cle_id = l_bcl_cle_id;
5230                  End If;
5231               Elsif l_bcl_btn_id is null and l_bcl_bill_action = 'TR' then
5232                  update oks_k_lines_b
5233                     set credit_amount = credit_amount + (l_termination_amount - g_credit_amount)
5234                       , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5235                  where cle_id = l_bsl_cle_id;
5236                  If l_bcl_cle_id <> l_bsl_cle_id then
5237                     update oks_k_lines_b
5238                        set credit_amount = credit_amount + (l_termination_amount - g_credit_amount)
5239                          , override_amount = override_amount - (l_termination_amount - g_credit_amount)
5240                      where cle_id = l_bcl_cle_id;
5241                  End If;
5242               End If;
5243               update okc_k_lines_b
5244                  set price_negotiated = price_negotiated + (  g_credit_amount - l_termination_amount )
5245                where id = l_bcl_cle_id
5246                  and lse_id <> 12;
5247               If l_bsl_cle_id <> l_bcl_cle_id then
5248                  update okc_k_lines_b
5249                     set price_negotiated = price_negotiated + (  g_credit_amount - l_termination_amount )
5250                   where id = l_bsl_cle_id
5251                     and lse_id <>13;
5252               End If;
5253               If sql%rowcount > 0 then
5254               update okc_k_headers_b
5255                  set estimated_amount = estimated_amount + (  g_credit_amount -  l_termination_amount )
5256                where id =  p_id;
5257               End if;
5258               open sub_line_tax(l_bsl_cle_id);
5259               fetch sub_line_tax into l_sub_line_tax_amount;
5260               close sub_line_tax;
5261               If l_sub_line_tax_amount > 0 then
5262                     OKS_TAX_UTIL_PVT.Get_Tax
5263                      ( p_api_version      => 1.0,
5264                        p_init_msg_list    => OKC_API.G_TRUE,
5265                        p_chr_id           => p_id,
5266                        p_cle_id           => l_bsl_cle_id,
5267                        px_rail_rec        => G_RAIL_REC,
5268                        x_msg_count        => l_msg_count,
5269                        x_msg_data         => l_msg_data,
5270                        x_return_status    => l_return_status);
5271                     IF ( x_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
5272                        RAISE G_EXCEPTION_HALT_VALIDATION;
5273                     END IF;
5274                     If G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG = 'N' then
5275                        update oks_k_lines_b
5276                           set tax_amount = G_RAIL_REC.tax_value
5277                         where cle_id = l_bsl_cle_id;
5278 
5279                     End If;
5280               End If;
5281           End If;
5282        END IF;
5283 
5284    update oks_k_lines_b topline set topline.tax_amount =  ( select sum(tax_amount) from
5285                   oks_k_lines_b oksline, okc_k_lines_b okcline
5286                   where okcline.id = oksline.cle_id
5287                     and okcline.cle_id = l_bcl_cle_id
5288                     and okcline.date_cancelled is null )
5289    where  topline.cle_id = l_bcl_cle_id;
5290 
5291    END IF ;  -- l_terminate = TRUE
5292 
5293    g_credit_amount :=0;
5294    g_bsl_id := null;
5295    g_bcl_id := null;
5296 
5297    /* Added for BUG 3364773.If all lines  are already in TERMINATED
5298       status , then change the hdr status to terminate too
5299    */
5300    OPEN  check_all_lines_terminated(p_id);
5301    FETCH check_all_lines_terminated into l_dummy;
5302 
5303    IF (l_line_failed = FALSE)  AND
5304       ((( l_terminate = TRUE) OR
5305       ((l_terminate = FALSE) AND (check_all_lines_terminated%NOTFOUND)))) THEN
5306 
5307 
5308      OPEN hdr_det(p_id);
5309      FETCH hdr_det into l_hdr_parameter_rec.p_orig_end_date,
5310                         l_hdr_parameter_rec.p_contract_number,
5311                         l_hdr_parameter_rec.p_contract_modifier;
5312      CLOSE hdr_det;
5313 
5314      l_hdr_parameter_rec.p_contract_id := p_id;
5315      l_hdr_parameter_rec.p_termination_date := p_termination_date;
5316      l_hdr_parameter_rec.p_termination_reason := p_reason_code;
5317 
5318      IF (p_termination_date < l_max_term_date) THEN
5319        l_hdr_parameter_rec.p_termination_date := l_max_term_date;
5320      END IF;
5321 
5322      --dbms_output.put_line ('before terminate_hdr ');
5323      OKC_TERMINATE_PVT.terminate_chr
5324                ( p_api_version                 => 1.0,
5325                  p_init_msg_list               => OKC_API.G_FALSE,
5326                  x_return_status               => x_return_status,
5327                  x_msg_count                   => l_msg_count,
5328                  x_msg_data                    => l_msg_data,
5329                  p_terminate_in_parameters_rec => l_hdr_parameter_rec
5330                  );
5331 
5332      ---For BUG#3372535 check for status S and W
5333      l_okc_status  := x_return_status;
5334 
5335      IF x_return_status NOT IN ( OKC_API.G_RET_STS_SUCCESS, 'W') Then
5336        raise G_EXCEPTION_HALT_VALIDATION;
5337      END IF;
5338 
5339      l_true_value_tbl(1).p_cp_line_id       := 0;
5340      l_true_value_tbl(1).p_top_line_id      := 0;
5341      l_true_value_tbl(1).p_hdr_id           := p_id ;
5342      l_true_value_tbl(1).p_termination_date := p_termination_date;
5343      l_true_value_tbl(1).p_terminate_reason := p_reason_code;
5344      l_true_value_tbl(1).p_override_amount  := l_termination_amount;
5345      l_true_value_tbl(1).p_con_terminate_amount := l_con_termination_amount;
5346      l_true_value_tbl(1).p_termination_amount   := l_termination_amount;
5347      l_true_value_tbl(1).p_suppress_credit      := p_suppress_credit;
5348      l_true_valUe_tbl(1).p_full_credit          := p_full_credit ;
5349 
5350      True_value(l_true_value_tbl , x_return_status );
5351 
5352      IF x_return_status <> OKC_API.G_RET_STS_SUCCESS Then
5353        raise G_EXCEPTION_HALT_VALIDATION;
5354      END IF;
5355 
5356    END IF ;  -- l_terminate = TRUE|| ((l_terminate = FALSE) AND (check_all_lines_terminated%NOTFOUND)))
5357 
5358    update oks_k_headers_b hdr set hdr.tax_amount =  ( select sum(tax_amount) from
5359                   oks_k_lines_b oksline, okc_k_lines_b okcline
5360                   where okcline.id = oksline.cle_id
5361                   and okcline.dnz_chr_id = p_id
5362                   and okcline.date_cancelled is null
5363                   and lse_id in (1,12,19,46) )
5364    where  hdr.chr_id = p_id;
5365 
5366  END IF;    --End of Main IF Statement
5367 
5368 
5369  ----this is added if okc passes status as 'W' , form based on status oly shows the warning.
5370  ---without this statment x_return_status returned from okc is overridden by next sub program.
5371  ---added for bug#3372535
5372 
5373  x_return_status := nvl(l_okc_status, 'S');
5374 
5375 EXCEPTION
5376  WHEN G_EXCEPTION_HALT_VALIDATION THEN
5377     DBMS_TRANSACTION.ROLLBACK_SAVEPOINT('BEFORE_PRE_TERMINATE');
5378  WHEN OTHERS THEN
5379     x_return_status   :=   OKC_API.G_RET_STS_UNEXP_ERROR;
5380     OKC_API.set_message(G_APP_NAME,G_UNEXPECTED_ERROR, G_SQLCODE_TOKEN, SQLCODE,G_SQLERRM_TOKEN, SQLERRM);
5381 END;
5382 
5383 
5384 PROCEDURE pre_terminate_service
5385   ( P_CALLEDFROM                   IN         NUMBER   DEFAULT Null,
5386     p_api_version                  IN         NUMBER,
5387     p_init_msg_list                IN         VARCHAR2 DEFAULT OKC_API.G_FALSE,
5388     x_return_status                OUT NOCOPY VARCHAR2,
5389     x_msg_count                    OUT NOCOPY NUMBER,
5390     x_msg_data                     OUT NOCOPY VARCHAR2,
5391     p_k_line_id                    IN         NUMBER,
5392     p_termination_date             IN         DATE,
5393     p_termination_amount           IN         NUMBER   DEFAULT NULL,  -- user input for termination
5394     p_con_termination_amount       IN         NUMBER   DEFAULT NULL,  -- actual value to be terminated
5395     --p_existing_credit              IN         NUMBER   DEFAULT NULL,
5396     p_termination_flag             IN         NUMBER   DEFAULT NULL, -- 1 - regular, 2- simulation
5397     p_suppress_credit              IN         VARCHAR2,
5398     p_full_credit                  IN         VARCHAR2,
5399     x_amount                       OUT NOCOPY NUMBER )
5400 
5401 IS
5402 
5403 CURSOR l_lse_csr Is
5404 SELECT LIne.lse_id,
5405        rline.termn_method,
5406        rline.usage_type,
5407        rline.usage_period
5408   FROM okc_k_lines_b  line,
5409        oks_k_lines_b  rline
5410  WHERE line.id = p_k_line_id
5411    AND rline.cle_id = line.id;
5412 
5413 CURSOR l_rel_csr Is
5414 SELECT obj.id
5415   From OKC_K_REL_OBJS_V obj,
5416        OKC_K_LINES_B  ln
5417  Where obj.cle_id = ln.id
5418    And ln.cle_id =  p_k_line_id;
5419 
5420 CURSOR l_line_csr (p_id in NUMBER ) is
5421  SELECT start_date
5422  FROM  okc_k_lines_b
5423  WHERE id = p_id ;
5424 
5425 
5426 l_lse_id                 NUMBER;
5427 l_amount                 NUMBER ;
5428 l_id                     NUMBER;
5429 l_msg_cnt                NUMBER;
5430 l_msg_data               VARCHAR2(2000);
5431 l_termination_method     VARCHAR2(20);
5432 l_return_status          VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
5433 l_usage_type             VARCHAR2(10);
5434 l_usage_period           VARCHAR2(10);
5435 l_termination_date       DATE;
5436 l_sub_line_id            NUMBER ;
5437 
5438 
5439 
5440 BEGIN
5441 
5442   IF ((nvl(p_termination_amount,1) > 0) OR
5443           (nvl(p_con_termination_amount,1) > 0)) THEN
5444 
5445     OPEN  l_lse_csr ;
5446     FETCH l_lse_csr into l_lse_id,l_termination_method,
5447                          l_usage_type,l_usage_period ;
5448     CLOSE l_lse_csr;
5449 
5450     IF (l_lse_id = 19) THEN
5451       OPEN  l_rel_csr;
5452       FETCH l_rel_csr into l_id;
5453 
5454       IF (l_rel_csr%found) THEN
5455 
5456        --errorout('EXTD. WARRANTY ROUTINE');
5457 
5458         Pre_terminate_extwar
5459             (
5460               P_CALLEdFROM             => p_calledfrom,
5461               P_LINE_ID                => p_k_line_id,
5462               P_TERMINATION_DATE       => p_termination_date,
5463               P_SUPPRESS_CREDIT        => p_suppress_credit,
5464               P_TERMINATION_AMOUNT     => p_termination_amount,
5465               P_CON_TERMINATION_AMOUNT => p_con_termination_amount,
5466               P_COV_LINE               => 'N'  ,         -- NOT a coverage line
5467               P_FULL_CREDIT            => p_full_credit,
5468               --P_EXISTING_CREDIT        => p_existing_credit,
5469               X_AMOUNT                 => l_amount,
5470               X_RETURN_STATUS          => l_return_status
5471              );
5472 
5473       ELSE
5474         --errorout('SERVICE ROUTINE');
5475 
5476          Pre_terminate_srvc
5477            (
5478              P_CALLEDFROM             => p_calledfrom,
5479              P_K_LINE_ID              => p_k_line_id,
5480              P_TERMINATION_DATE       => p_termination_date,
5481              P_FLAG                   => p_termination_flag, -- 1 - regular, 2- simulation
5482              P_TERMINATION_AMOUNT     => p_termination_amount,
5483              P_CON_TERMINATION_AMOUNT => p_con_termination_amount,
5484              --P_EXISTING_CREDIT        => p_existing_credit,
5485              P_SUPPRESS_CREDIT        => p_suppress_credit,
5486              P_FULL_CREDIT            => P_FULL_CREDIT,
5487              X_AMOUNT                 => l_amount,
5488              X_RETURN_STATUS          => l_return_status
5489             );
5490 
5491 
5492       END IF;
5493       CLOSE l_rel_csr;
5494 
5495     ELSIF (l_lse_id = 12) THEN
5496       IF ( l_termination_method = 'VOLUME') THEN
5497          pre_vol_based_terminate(
5498             P_CALLEDFROM                   => p_calledfrom,
5499             P_API_VERSION                  => 1,
5500             P_INIT_MSG_LIST                => OKC_API.G_FALSE,
5501             X_RETURN_STATUS                => l_return_status,
5502             X_MSG_COUNT                    => l_msg_cnt,
5503             X_MSG_DATA                     => l_msg_data,
5504             P_K_LINE_ID                    => p_k_line_id,
5505             P_CP_LINE_ID                   => NULL,
5506             P_TERMINATION_DATE             => p_termination_date,
5507             P_TERMINATION_AMOUNT           => p_termination_amount,  --user i/p
5508             P_CON_TERMINATION_AMOUNT       => p_con_termination_amount,-- actual
5509             --P_EXISTING_CREDIT              => p_existing_credit,
5510             P_SUPPRESS_CREDIT              => p_suppress_credit,
5511             P_USAGE_TYPE                   => l_usage_type,
5512             P_USAGE_PERIOD                 => l_usage_period,
5513             X_AMOUNT                       => l_amount);
5514 
5515       ELSE
5516         Pre_terminate_srvc
5517            (
5518              P_CALLEDFROM             => p_calledfrom,
5519              P_K_LINE_ID              => p_k_line_id,
5520              P_TERMINATION_DATE       => p_termination_date,
5521              P_FLAG                   => p_termination_flag,
5522              P_TERMINATION_AMOUNT     => p_termination_amount,
5523              P_CON_TERMINATION_AMOUNT => p_con_termination_amount,
5524              --P_EXISTING_CREDIT        => p_existing_credit,
5525              P_SUPPRESS_CREDIT        => p_suppress_credit,
5526              P_FULL_CREDIT            => p_full_credit,
5527              X_AMOUNT                 => l_amount,
5528              X_RETURN_STATUS          => l_return_status
5529             );
5530 
5531       END IF;
5532     ELSE
5533       Pre_terminate_srvc
5534            (
5535              P_CALLEDFROM             => p_calledfrom,
5536              P_K_LINE_ID              => p_k_line_id,
5537              P_TERMINATION_DATE       => p_termination_date,
5538              P_FLAG                   => p_termination_flag, -- 1 - regular, 2-simulation
5539              P_TERMINATION_AMOUNT     => p_termination_amount,
5540              P_CON_TERMINATION_AMOUNT => p_con_termination_amount,
5541              --P_EXISTING_CREDIT        => p_existing_credit,
5542              P_SUPPRESS_CREDIT        => p_suppress_credit,
5543              P_FULL_CREDIT            => p_full_credit,
5544              X_AMOUNT                 => l_amount,
5545              X_RETURN_STATUS          => l_return_status
5546             );
5547 
5548     END IF;
5549    END IF;
5550 /************************************
5551   IF p_full_credit = 'Y' then
5552      OPEN  l_line_csr(p_k_line_id);
5553      Fetch l_line_csr into l_termination_date;
5554      CLOSE l_line_csr;
5555   ELSE
5556      l_termination_date := p_termination_date ;
5557   END IF;
5558 *************************************/
5559   If p_full_credit = 'Y' then
5560      l_sub_line_id := -100 ;
5561   Else
5562      l_sub_line_id := NULL;
5563   End If;
5564 
5565 
5566    OKS_BILL_SCH.Terminate_bill_sch(
5567           P_TOP_LINE_ID         => p_k_line_id,
5568           P_SUB_LINE_ID         => l_sub_line_id,
5569           P_TERM_DT             => p_termination_date,
5570           X_RETURN_STATUS       => l_return_status,
5571           X_MSG_COUNT           => l_msg_cnt,
5572           X_MSG_DATA            => l_msg_data);
5573 
5574 
5575 x_amount := nvl(l_amount,0);
5576 
5577 X_return_status := l_return_status;
5578 EXCEPTION
5579   WHEN  G_EXCEPTION_HALT_VALIDATION THEN
5580     x_return_status   :=   l_return_status;
5581   WHEN  Others THEN
5582     x_return_status   :=   OKC_API.G_RET_STS_UNEXP_ERROR;
5583     OKC_API.set_message(G_APP_NAME,G_UNEXPECTED_ERROR, G_SQLCODE_TOKEN, SQLCODE,G_SQLERRM_TOKEN, SQLERRM);
5584 
5585 END ;
5586 
5587 /* Procedure to calculate AMCV */
5588 
5589 -------------------------------------------------------------------------
5590 -- Begin partial period computation logic
5591 -- Developer Mani Choudhary
5592 -- Date 12-JUN-2005
5593 -- Added parameters p_period_type and p_period_start
5594 -------------------------------------------------------------------------
5595 PROCEDURE OKS_REG_GET_AMCV
5596  (
5597   P_CALLEDFROM      IN           NUMBER    DEFAULT NULL,
5598   X_RETURN_STATUS  OUT   NOCOPY  VARCHAR2,
5599   P_CLE_ID          IN           NUMBER,
5600   P_COV_START_DATE  IN           DATE,
5601   P_COV_END_DATE    IN           DATE,
5602   P_COUNTER_DATE    IN           DATE,
5603   P_COUNTER_VALUE   IN           NUMBER,
5604   P_PERIOD_TYPE     IN           VARCHAR2,
5605   P_PERIOD_START    IN           VARCHAR2,
5606   P_USAGE_PERIOD    IN           VARCHAR2,
5607   X_VOLUME         OUT   NOCOPY  NUMBER
5608  )
5609  IS
5610 l_mth                NUMBER := 1;
5611 l_prddays            NUMBER;
5612 l_TotVol             NUMBER;
5613 l_Date_Billed_From   DATE;
5614 l_Date_Billed_To     DATE;
5615 
5616 Cursor l_date_csr Is
5617   SELECT  Min(bsl.DATE_BILLED_FROM)
5618          ,Max(bsl.DATE_BILLED_TO)
5619   FROM  oks_bill_cont_lines bcl  ,
5620         oks_bill_sub_lines  bsl
5621   WHERE  bsl.cle_id = p_cle_id
5622   AND    bsl.bcl_id = bcl.id
5623   AND    bcl.bill_action  = 'RI';
5624 
5625 Cursor l_tot_csr Is
5626   SELECT NVL(Sum(NVL(bsd.Result,0)),0)
5627   FROM   oks_bill_cont_lines    bcl,
5628          oks_bill_sub_lines     bsl,
5629          oks_bill_sub_line_dtls bsd
5630   WHERE  bsl.cle_id = p_cle_id
5631   AND    bsl.bcl_id = bcl.id
5632   AND    bcl.bill_action ='RI'
5633   AND    bsd.bsl_id = bsl.id;
5634 
5635 BEGIN
5636   x_Volume        := 0;
5637   l_totvol := 0;
5638   x_Return_Status := OKC_API.G_RET_STS_SUCCESS;
5639 
5640   IF ((p_calledfrom <> 3) OR
5641      ((p_calledfrom = 3) AND ( p_counter_date IS NULL))) THEN  -- Ignore for termination if counter is captured
5642     OPEN  l_tot_csr;
5643     FETCH l_tot_csr into l_totvol;
5644     CLOSE l_tot_csr;
5645   END IF;
5646 
5647   IF (p_counter_date IS NULL) THEN
5648     OPEN  l_date_csr;
5649     FETCH l_date_csr into l_date_billed_from,l_date_billed_to;
5650     CLOSE l_date_csr;
5651 
5652   ELSE
5653     l_date_billed_from := p_cov_start_date;
5654     l_date_billed_to   := p_counter_date;
5655     l_totvol           := l_totvol + p_counter_value;
5656   END IF;
5657 
5658 
5659   --x_Volume := Round(l_TotVol / l_prddays,0);
5660 
5661   -------------------------------------------------------------------------
5662   -- Begin partial period computation logic
5663   -- Developer Mani Choudhary
5664   -- Date 12-JUN-2005
5665   -- Calling get_target_qty_service to consider the period type
5666   -------------------------------------------------------------------------
5667   IF p_period_type IS NOT NULL AND
5668      p_period_start IS NOT NULL AND
5669      l_date_billed_from IS NOT NULL AND   --mchoudha Fix for bug#5166216
5670      l_date_billed_to IS NOT NULL
5671   THEN
5672      l_prddays :=  OKS_TIME_MEASURES_PUB.get_target_qty_service (
5673                                                         p_start_date   => l_Date_Billed_From,
5674                                                         p_end_date     => l_Date_Billed_To,
5675                                                         p_price_uom    => p_usage_period,
5676                                                         p_period_type  => p_period_type,
5677                                                         p_round_dec    => 18
5678                                                         );
5679       IF nvl(l_prddays,0) = 0 THEN
5680         RAISE G_EXCEPTION_HALT_VALIDATION;
5681       END IF;
5682 
5683   ELSE
5684     l_prddays := trunc(l_Date_Billed_To) - trunc(l_Date_Billed_From) + 1;
5685   END IF;
5686 
5687   x_Volume := l_TotVol / l_prddays;
5688 
5689 EXCEPTION
5690   WHEN G_EXCEPTION_HALT_VALIDATION THEN
5691     FND_FILE.PUT_LINE( FND_FILE.LOG, 'Error in OKS_REG_GET_AMCV -- G_Exception_halt_validation raised' );
5692     x_return_status := OKC_API.G_RET_STS_UNEXP_ERROR;
5693   WHEN OTHERS THEN
5694     FND_FILE.PUT_LINE( FND_FILE.LOG, 'Error in OKS_REG_GET_AMCV -- Others Exception raised' );
5695     OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,
5696                         G_SQLCODE_TOKEN,SQLCODE,G_SQLERRM_TOKEN,SQLERRM);
5697     x_return_status := OKC_API.G_RET_STS_UNEXP_ERROR;
5698 
5699 END  OKS_REG_GET_AMCV;
5700 
5701 --------------------------------------------------------------------------------
5702                            --Usage_qty_to_bill
5703 -------------------------------------------------------------------------------
5704 -------------------------------------------------------------------------
5705 -- Begin partial period computation logic
5706 -- Developer Mani Choudhary
5707 -- Date 11-JUN-2005
5708 -- Added additional period_type period_start parameters
5709 -------------------------------------------------------------------------
5710 Procedure Usage_qty_to_bill
5711 (
5712   P_calledfrom            IN                NUMBER DEFAULT NULL,
5713   P_cle_id                IN                NUMBER,
5714   P_Usage_type            IN                VARCHAR2,
5715   P_estimation_flag       IN                VARCHAR2,
5716   P_estimation_method     IN                VARCHAR2,
5717   P_default_qty           IN                NUMBER,
5718   P_cov_start_date        IN                DATE,
5719   P_cov_end_date          IN                DATE,
5720   P_cov_prd_start_date    IN                DATE,
5721   P_cov_prd_end_date      IN                DATE,
5722   p_usage_period          IN                VARCHAR2,
5723   p_time_uom_code         IN                VARCHAR2,
5724   p_settle_interval       IN                VARCHAR2,
5725   p_minimum_quantity      IN                NUMBER,
5726   p_usg_est_start_date    IN                DATE,
5727   p_period_type           IN                VARCHAR2, --period type
5728   p_period_start          IN                VARCHAR2, --period start
5729   X_qty                   OUT        NOCOPY NUMBER,
5730   X_Uom_Code              OUT        NOCOPY VARCHAR2,
5731   X_flag                  OUT        NOCOPY VARCHAR2,
5732   X_end_reading           OUT        NOCOPY NUMBER,
5733   X_start_reading         OUT        NOCOPY NUMBER,
5734   X_base_reading          OUT        NOCOPY NUMBER,
5735   X_estimated_qty         OUT        NOCOPY NUMBER,
5736   X_actual_qty            OUT        NOCOPY NUMBER,
5737   X_counter_value_id      OUT        NOCOPY NUMBER,
5738   X_counter_group_id      OUT        NOCOPY NUMBER,
5739   X_return_status         OUT        NOCOPY VARCHAR2
5740 )
5741 IS
5742 /* ONE is added to count to consider the unbilled current period */
5743 CURSOR billed_period_csr (p_cle_id   IN   NUMBER) IS
5744  SELECT count(*) + 1
5745     FROM   oks_bill_sub_lines
5746     WHERE  cle_id = p_cle_id;
5747 
5748 CURSOR  total_billed_qty (p_cle_id   IN  NUMBER)  IS
5749   SELECT sum(result)
5750     FROM oks_bill_sub_line_dtls bsd,
5751          oks_bill_sub_lines     bsl
5752     WHERE bsl.cle_id = p_cle_id
5753     AND   bsd.bsl_id = bsl.id;
5754 
5755 CURSOR l_max_date_end (p_cle_id IN NUMBER, p_date  IN DATE) IS
5756   SELECT MAX(date_end)
5757     FROM oks_level_elements
5758     WHERE cle_id = p_cle_id
5759     AND   trunc(date_end) < trunc(p_date)
5760     AND   date_completed is NOT NULL;
5761 
5762 
5763 CURSOR l_min_date_start (p_cle_id IN NUMBER, p_date  IN DATE) IS
5764   SELECT MIN(date_start)
5765     FROM oks_level_elements
5766     WHERE cle_id = p_cle_id
5767     AND   trunc(date_start) >  trunc(p_date)
5768     AND   date_completed is NOT NULL;
5769 
5770 --Added this cursor to fetch uom code.
5771 --Will be used for partial periods
5772 -------------------------------------------
5773  CURSOR l_Billuom_csr(p_date IN DATE) IS
5774  SELECT UOM_CODE
5775  FROM   oks_stream_levels_b
5776  WHERE  cle_id = p_cle_id
5777  AND    trunc(p_date) >= START_DATE
5778  AND    trunc(p_date) <= END_DATE;
5779 ---------------------------------------------
5780 
5781 
5782   l_uom_code              VARCHAR2(30);
5783   l_return_status         VARCHAR2(10);
5784   l_billing_based_on      VARCHAR2(10);
5785   l_msg_data              VARCHAR2(2000);
5786   l_qty                   NUMBER;
5787   l_est_reading           NUMBER;
5788   l_estimated_qty         NUMBER;
5789   l_end_reading           NUMBER;
5790   l_start_reading         NUMBER;
5791   l_base_reading          NUMBER;
5792   l_counter_value_id      NUMBER;
5793   l_counter_group_id      NUMBER;
5794   l_temp                  NUMBER;
5795   l_bsl_count             NUMBER;
5796   l_total_min_qty         NUMBER;
5797   l_total_bill_qty        NUMBER;
5798   l_called_From           NUMBER;
5799   l_counter_value         NUMBER;
5800   l_counter_id            NUMBER;
5801   l_msg_cnt               NUMBER;
5802   l_est_period_start_rdg  NUMBER;
5803   l_counter_date          DATE;
5804   l_cov_start_date        DATE;
5805   l_cov_end_date          DATE;
5806   l_cov_prd_start_date    DATE;
5807   l_cov_prd_end_date      DATE;
5808   l_temp_qty              NUMBER;
5809 BEGIN
5810 
5811 /******
5812   ------------commented as part of bug# 5178204
5813   IF (p_settle_interval = 'BILLING PERIOD') THEN
5814     l_called_From := 3;  --call counter api for settlement
5815   ELSE
5816     l_called_From := 1;  --call counter api for settlement
5817   END IF;
5818 ****/
5819 
5820   IF (p_calledfrom = 3) THEN
5821     l_cov_prd_end_date    := p_cov_prd_end_date - 1  ;
5822     l_cov_prd_start_date  := p_cov_prd_start_date    ;
5823   ELSE
5824     l_cov_prd_end_date    := p_cov_prd_end_date     ;
5825     l_cov_prd_start_date  := p_cov_prd_start_date   ;
5826   END IF;
5827 
5828   X_estimated_qty   := 0;
5829   l_qty             := 0;
5830 
5831 
5832   IF (p_calledfrom <> 3) OR
5833      ((p_calledfrom = 3) AND (l_cov_prd_start_date < l_cov_prd_end_date)) THEN
5834 
5835            ------------commented as part of bug# 5178204
5836            -------------P_CALLEDFROM        => l_called_from,
5837     COUNTER_VALUES
5838           (
5839            P_CALLEDFROM        => p_calledfrom,
5840            P_START_DATE        => l_cov_prd_start_date,
5841            P_END_DATE          => l_cov_prd_end_date,
5842            P_CLE_ID            => p_cle_id,
5843            P_USAGE_TYPE        => p_usage_type,
5844            X_VALUE             => l_qty,
5845            X_COUNTER_VALUE     => l_counter_value,
5846            X_COUNTER_DATE      => l_counter_date,
5847            X_UOM_CODE          => l_uom_code,
5848            X_END_READING       => l_end_reading,
5849            X_START_READING     => l_start_Reading,
5850            X_BASE_READING      => l_base_Reading,
5851            X_COUNTER_VALUE_ID  => l_counter_value_id,
5852            X_COUNTER_GROUP_ID  => l_counter_group_id,
5853            X_COUNTER_ID        => l_counter_id,
5854            X_RETURN_STATUS     => l_return_status
5855            );
5856   END IF;
5857 
5858   X_actual_qty  :=  nvl(l_qty,0);
5859   IF (p_usage_type = 'VRT') THEN
5860     /*
5861     IF  (nvl(p_estimation_flag,'N') = 'N' )
5862                                     AND (p_estimation_method is NULL) THEN
5863      Above condition is not required as it is defaulted if any of the below
5864      conditions are not satisfied.
5865     */
5866 
5867     IF (nvl(p_estimation_flag,'N') = 'N')
5868                                     AND (p_estimation_method = 'AMCV') THEN
5869 
5870       -------------------------------------------------------------------------
5871       -- Begin partial period computation logic
5872       -- Developer Mani Choudhary
5873       -- Date 12-JUN-2005
5874       -- Added parameters p_period_type and p_period_start
5875       -------------------------------------------------------------------------
5876       OKS_REG_GET_AMCV
5877         (
5878          P_CALLEDFROM     => p_calledfrom ,
5879          X_RETURN_STATUS  => l_return_status,
5880          P_CLE_ID         => p_cle_id,
5881          P_COV_START_DATE => p_cov_start_date,
5882          P_COV_END_DATE   => p_cov_end_date,
5883          P_COUNTER_DATE   => l_counter_date ,
5884          P_COUNTER_VALUE  => l_qty,
5885          P_PERIOD_TYPE    => p_period_type,
5886          P_PERIOD_START   => p_period_start,
5887          P_USAGE_PERIOD   => p_usage_period,
5888          X_VOLUME         => l_estimated_qty
5889         );
5890         IF l_return_status <> 'S'  THEN
5891           RAISE G_EXCEPTION_HALT_VALIDATION;
5892         END IF;
5893       IF ((nvl(l_qty,0) = 0) AND (p_calledfrom in (1,2))) THEN
5894         --partial periods estimation AMCV
5895         --------------------------
5896         IF p_period_type IS NOT NULL AND
5897            p_period_start IS NOT NULL
5898         THEN
5899           l_qty := OKS_TIME_MEASURES_PUB.get_target_qty_service (
5900                                                         p_start_date   => p_cov_prd_start_date,
5901                                                         p_end_date     => p_cov_prd_end_date,
5902                                                         p_price_uom    => p_usage_period,
5903                                                         p_period_type  => p_period_type,
5904                                                         p_round_dec    => 18
5905                                                         );
5906           --for bug#5334983: no exception needs to be raised-the quantity will be zero
5907           --IF nvl(l_qty,0) = 0 THEN
5908           --  RAISE G_EXCEPTION_HALT_VALIDATION;
5909           --END IF;
5910           l_qty := Round(l_qty*l_estimated_qty,0);
5911         ELSE
5912           l_qty := Round((trunc(p_cov_prd_end_date) -
5913                         trunc(p_cov_prd_start_date) + 1) * (l_estimated_qty) ,0) ;
5914         END IF;
5915         --------------------------
5916         X_estimated_qty  := l_qty;
5917       ELSIF  (p_calledfrom = 3 ) THEN
5918         --IF (l_counter_date is NULL) THEN
5919         --  l_cov_start_date := p_cov_prd_start_date;
5920         --  OPEN   l_max_date_end(p_cle_id,p_cov_prd_end_date);
5921         --  FETCH  l_max_date_end into l_cov_end_date;
5922         --  CLOSE  l_max_date_end;
5923         IF (l_counter_date is NOT NULL) THEN
5924           --l_cov_end_date := p_cov_prd_end_date;
5925           OPEN   l_min_date_start(p_cle_id,l_counter_date);
5926           FETCH  l_min_date_start into l_cov_start_date;
5927           CLOSE  l_min_date_start;
5928 
5929           OPEN   l_max_date_end(p_cle_id,p_cov_prd_end_date);
5930           FETCH  l_max_date_end into l_cov_end_date;
5931           CLOSE  l_max_date_end;
5932           --Partial periods for estimation AMCV
5933           ---------------------------------
5934           IF p_period_type IS NOT NULL AND
5935              p_period_start IS NOT NULL
5936           THEN
5937             l_temp_qty := OKS_TIME_MEASURES_PUB.get_target_qty_service (
5938                                                         p_start_date   => l_cov_start_date,
5939                                                         p_end_date     => l_cov_end_date,
5940                                                         p_price_uom    => p_usage_period,
5941                                                         p_period_type  => p_period_type,
5942                                                         p_round_dec    => 18
5943                                                         );
5944             --for bug#5334983: no exception needs to be raised-the quantity will be zero
5945             --IF nvl(l_temp_qty,0) = 0 THEN
5946             --  RAISE G_EXCEPTION_HALT_VALIDATION;
5947             --END IF;
5948             l_estimated_qty := Round(l_temp_qty*l_estimated_qty,0);
5949           ELSE
5950 
5951             l_estimated_qty := Round((trunc(l_cov_end_date) -
5952                         trunc(l_cov_start_date) + 1) * (l_estimated_qty) ,0) ;
5953           END IF;
5954           ---------------------------------
5955           IF (l_estimated_qty < 0) THEN
5956             l_estimated_qty := 0;
5957           END IF;
5958           l_qty := l_qty + l_estimated_qty;
5959         END IF;
5960       END IF;
5961 
5962       x_flag := 'M';
5963 
5964       --X_estimated_qty  := l_qty;
5965 
5966     ELSIF (nvl(p_estimation_flag,'N') = 'N')
5967                                     AND (p_estimation_method = 'CSR' ) THEN
5968       IF ((nvl(l_qty,0) = 0) AND (p_calledfrom in (1,2))) THEN
5969        --CALL COUNTER API HERE to get the estimated qty for the entire period
5970         CSI_CTR_EST_CTR_READING_GRP.ESTIMATE_COUNTER_READING(
5971            P_API_VERSION_NUMBER           => 1.0,
5972            P_INIT_MSG_LIST                => FND_API.G_FALSE,
5973            P_COMMIT                       => FND_API.G_FALSE,
5974            P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL,
5975            P_COUNTER_ID                   => l_counter_id,
5976            P_ESTIMATION_PERIOD_START_DATE => p_cov_prd_start_date,
5977            P_ESTIMATION_PERIOD_END_DATE   => p_cov_prd_end_date,
5978            P_AVG_CALCULATION_START_DATE   => p_usg_est_start_date,
5979            P_NUMBER_OF_READINGS           => NULL,
5980            X_ESTIMATED_USAGE_QTY          => l_qty,
5981            X_ESTIMATED_METER_READING      => l_est_reading,
5982            X_ESTIMATED_PERIOD_START_RDG   => l_est_period_start_rdg,
5983            X_RETURN_STATUS                => l_return_status,
5984            X_MSG_COUNT                    => l_msg_cnt,
5985            X_MSG_DATA                     => l_msg_data
5986             );
5987         X_estimated_qty  := l_qty;
5988       ELSIF (p_calledfrom = 3) THEN
5989         /* This elsif is required to estimated during termination */
5990         IF (l_counter_date is NULL) THEN
5991           l_cov_start_date := p_cov_prd_start_date;
5992           OPEN   l_max_date_end(p_cle_id,p_cov_prd_end_date);
5993           FETCH  l_max_date_end into l_cov_end_date;
5994           CLOSE  l_max_date_end;
5995         ELSIF (l_counter_date is NOT NULL) THEN
5996           --l_cov_end_date := p_cov_prd_end_date;
5997           OPEN   l_min_date_start(p_cle_id,l_counter_date);
5998           FETCH  l_min_date_start into l_cov_start_date;
5999           CLOSE  l_min_date_start;
6000 
6001           OPEN   l_max_date_end(p_cle_id,p_cov_prd_end_date);
6002           FETCH  l_max_date_end into l_cov_end_date;
6003           CLOSE  l_max_date_end;
6004 
6005           IF ((l_cov_start_date <  p_usg_est_start_date) AND
6006             (p_usg_est_start_date <= l_cov_end_date)) THEN
6007             l_cov_start_date := p_usg_est_start_date + 1 ;
6008           END IF;
6009         END IF;
6010 
6011 
6012         CSI_CTR_EST_CTR_READING_GRP.ESTIMATE_COUNTER_READING(
6013            P_API_VERSION_NUMBER           => 1.0,
6014            P_INIT_MSG_LIST                => FND_API.G_FALSE,
6015            P_COMMIT                       => FND_API.G_FALSE,
6016            P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL,
6017            P_COUNTER_ID                   => l_counter_id,
6018            P_ESTIMATION_PERIOD_START_DATE => l_cov_start_date,
6019            P_ESTIMATION_PERIOD_END_DATE   => l_cov_end_date,
6020            P_AVG_CALCULATION_START_DATE   => p_usg_est_start_date,
6021            P_NUMBER_OF_READINGS           => NULL,
6022            X_ESTIMATED_USAGE_QTY          => l_estimated_qty,
6023            X_ESTIMATED_METER_READING      => l_est_reading,
6024            X_ESTIMATED_PERIOD_START_RDG   => l_est_period_start_rdg,
6025            X_RETURN_STATUS                => l_return_status,
6026            X_MSG_COUNT                    => l_msg_cnt,
6027            X_MSG_DATA                     => l_msg_data
6028             );
6029 
6030         X_estimated_qty  := l_estimated_qty;
6031         l_qty  := nvl(l_qty,0)  +  nvl(l_estimated_qty,0);
6032 
6033 
6034       END IF;
6035 
6036     ELSIF (nvl(p_estimation_flag,'N')='Y')
6037                                     AND (p_estimation_method = 'AMCV') THEN
6038       IF (nvl(l_qty,0) = 0) THEN
6039       -------------------------------------------------------------------------
6040       -- Begin partial period computation logic
6041       -- Developer Mani Choudhary
6042       -- Date 12-JUN-2005
6043       -- Added parameters p_period_type and p_period_start
6044       -------------------------------------------------------------------------
6045         OKS_REG_GET_AMCV
6046           (
6047            P_CALLEDFROM     => p_calledfrom ,
6048            X_RETURN_STATUS  => l_return_status,
6049            P_CLE_ID         => p_cle_id,
6050            P_COV_START_DATE => p_cov_start_date,
6051            P_COV_END_DATE   => p_cov_end_date,
6052            P_COUNTER_DATE   => l_counter_date ,
6053            P_COUNTER_VALUE  => l_qty,
6054            P_PERIOD_TYPE    => p_period_type,
6055            P_PERIOD_START   => p_period_start,
6056            P_USAGE_PERIOD   => p_usage_period,
6057            X_VOLUME         => l_estimated_qty
6058           );
6059         IF l_return_status <> 'S'  THEN
6060           RAISE G_EXCEPTION_HALT_VALIDATION;
6061         END IF;
6062 
6063       -- IF called from termination then replace p_cov_end_date by x_counter_date
6064 
6065         IF (p_calledfrom in (1,2)) THEN
6066           --partial periods estimation AMCV
6067           --------------------------
6068           IF p_period_type IS NOT NULL AND
6069              p_period_start IS NOT NULL
6070           THEN
6071             l_qty := OKS_TIME_MEASURES_PUB.get_target_qty_service (
6072                                                         p_start_date   => p_cov_prd_start_date,
6073                                                         p_end_date     => p_cov_prd_end_date,
6074                                                         p_price_uom    => p_usage_period,
6075                                                         p_period_type  => p_period_type,
6076                                                         p_round_dec    => 18
6077                                                         );
6078             --for bug#5334983: no exception needs to be raised-the quantity will be zero
6079             --IF nvl(l_qty,0) = 0 THEN
6080             --  RAISE G_EXCEPTION_HALT_VALIDATION;
6081             --END IF;
6082             l_qty := Round(l_qty*l_estimated_qty,0);
6083           ELSE
6084 
6085             l_qty := Round((trunc(p_cov_prd_end_date) -
6086                       trunc(p_cov_prd_start_date) + 1) * (l_estimated_qty) ,0) ;
6087           END IF;
6088           ------------------------
6089           X_estimated_qty := l_qty;
6090         END IF;
6091       ELSIF (l_counter_date < p_cov_end_date) THEN
6092       -------------------------------------------------------------------------
6093       -- Begin partial period computation logic
6094       -- Developer Mani Choudhary
6095       -- Date 12-JUN-2005
6096       -- Added parameters p_period_type and p_period_start
6097       -------------------------------------------------------------------------
6098         OKS_REG_GET_AMCV
6099           (
6100            P_CALLEDFROM     => p_calledfrom ,
6101            X_RETURN_STATUS  => l_return_status,
6102            P_CLE_ID         => p_cle_id,
6103            P_COV_START_DATE => p_cov_start_date,
6104            P_COV_END_DATE   => p_cov_end_date,
6105            P_COUNTER_DATE   => l_counter_date ,
6106            P_COUNTER_VALUE  => l_qty , --l_counter_value,
6107            P_PERIOD_TYPE    => p_period_type,
6108            P_PERIOD_START   => p_period_start,
6109            P_USAGE_PERIOD   => p_usage_period,
6110            X_VOLUME         => l_estimated_qty
6111           );
6112         IF l_return_status <> 'S'  THEN
6113           RAISE G_EXCEPTION_HALT_VALIDATION;
6114         END IF;
6115 
6116 
6117         IF (p_calledfrom = 3) THEN
6118           --Partial periods for estimation AMCV
6119           ---------------------------------
6120           IF p_period_type IS NOT NULL AND
6121              p_period_start IS NOT NULL
6122           THEN
6123             --mchoudha fix for bug#5207605
6124             --Est Start date has to be counter date + 1
6125             --Est end date has to be termination date -1
6126             l_temp_qty := OKS_TIME_MEASURES_PUB.get_target_qty_service (
6127                                                         p_start_date   => l_counter_date+1,
6128                                                         p_end_date     => p_cov_prd_end_date-1,
6129                                                         p_price_uom    => p_usage_period,
6130                                                         p_period_type  => p_period_type,
6131                                                         p_round_dec    => 18
6132                                                         );
6133             --for bug#5334983: no exception needs to be raised-the quantity will be zero
6134             --IF nvl(l_temp_qty,0) = 0 THEN
6135             --  RAISE G_EXCEPTION_HALT_VALIDATION;
6136             --END IF;
6137             l_estimated_qty := Round(l_temp_qty*l_estimated_qty,0);
6138           ELSE
6139 
6140 
6141             l_estimated_qty := Round( (trunc(p_cov_prd_end_date - 1) -
6142                         trunc(l_counter_date)  ) * (l_estimated_qty) ,0) ;
6143           END IF;
6144           ------------------------------------
6145           /*
6146              Est qty cannot be -ve.This may happen if
6147              counter capture date and termn date is same
6148           */
6149           IF (l_estimated_qty < 0) THEN
6150             l_estimated_qty := 0;
6151           END IF;
6152         ELSE
6153           --Partial periods for estimation AMCV
6154           ---------------------------------
6155           IF p_period_type IS NOT NULL AND
6156              p_period_start IS NOT NULL
6157           THEN
6158             --mchoudha fix for bug#5207605
6159             --Est Start date has to be counter date + 1
6160             l_temp_qty := OKS_TIME_MEASURES_PUB.get_target_qty_service (
6161                                                         p_start_date   => l_counter_date+1,
6162                                                         p_end_date     => p_cov_prd_end_date,
6163                                                         p_price_uom    => p_usage_period,
6164                                                         p_period_type  => p_period_type,
6165                                                         p_round_dec    => 18
6166                                                         );
6167             --for bug#5334983: no exception needs to be raised-the quantity will be zero
6168             --IF nvl(l_temp_qty,0) = 0 THEN
6169             --  RAISE G_EXCEPTION_HALT_VALIDATION;
6170             --END IF;
6171 
6172             l_estimated_qty := Round(l_temp_qty*l_estimated_qty,0);
6173           ELSE
6174 
6175 
6176             l_estimated_qty := Round( (trunc(p_cov_prd_end_date  ) -
6177                         trunc(l_counter_date)  ) * (l_estimated_qty) ,0) ;
6178           END IF;
6179           ---------------------------------------
6180         END IF;
6181         l_qty :=  nvl(l_qty,0) + nvl(l_estimated_qty,0) ;
6182         X_estimated_qty := l_estimated_qty;
6183         x_flag := 'M';
6184 
6185       END IF;
6186     ELSIF (nvl(p_estimation_flag,'N')='Y')
6187                                     AND (p_estimation_method = 'CSR') THEN
6188       IF (P_calledfrom = 3 ) THEN
6189         l_cov_end_date := p_cov_prd_end_date - 1;   -- Estimation not req for Termination Date
6190         l_cov_start_date := l_counter_date+ 1;
6191         IF ((l_cov_start_date <  p_usg_est_start_date) AND
6192             (p_usg_est_start_date <= l_cov_end_date)) THEN
6193           l_cov_start_date := p_usg_est_start_date + 1 ;
6194         END IF;
6195       ELSE
6196         l_cov_start_date := l_counter_date+ 1;
6197         l_cov_end_date   := p_cov_prd_end_date;
6198       END IF;
6199 
6200       IF (nvl(l_qty,0) = 0) THEN
6201         CSI_CTR_EST_CTR_READING_GRP.ESTIMATE_COUNTER_READING(
6202            P_API_VERSION_NUMBER           => 1.0,
6203            P_INIT_MSG_LIST                => FND_API.G_FALSE,
6204            P_COMMIT                       => FND_API.G_FALSE,
6205            P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL,
6206            P_COUNTER_ID                   => l_counter_id,
6207            P_ESTIMATION_PERIOD_START_DATE => p_cov_prd_start_date,
6208            P_ESTIMATION_PERIOD_END_DATE   => l_cov_end_date,
6209            P_AVG_CALCULATION_START_DATE   => p_usg_est_start_date,
6210            P_NUMBER_OF_READINGS           => NULL,
6211            X_ESTIMATED_USAGE_QTY          => l_qty,
6212            X_ESTIMATED_METER_READING      => l_est_reading,
6213            X_ESTIMATED_PERIOD_START_RDG   => l_est_period_start_rdg,
6214            X_RETURN_STATUS                => l_return_status,
6215            X_MSG_COUNT                    => l_msg_cnt,
6216            X_MSG_DATA                     => l_msg_data
6217             );
6218 
6219         X_estimated_qty  := l_qty;
6220        --CALL COUNTER API HERE to get the estimated qty for the entire period
6221       ELSIF (l_counter_date < p_cov_end_date) THEN
6222         CSI_CTR_EST_CTR_READING_GRP.ESTIMATE_COUNTER_READING(
6223            P_API_VERSION_NUMBER           => 1.0,
6224            P_INIT_MSG_LIST                => FND_API.G_FALSE,
6225            P_COMMIT                       => FND_API.G_FALSE,
6226            P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL,
6227            P_COUNTER_ID                   => l_counter_id,
6228            P_ESTIMATION_PERIOD_START_DATE => l_cov_start_date,
6229            P_ESTIMATION_PERIOD_END_DATE   => l_cov_end_date,
6230            P_AVG_CALCULATION_START_DATE   => p_usg_est_start_date,
6231            P_NUMBER_OF_READINGS           => NULL,
6232            X_ESTIMATED_USAGE_QTY          => l_estimated_qty,
6233            X_ESTIMATED_METER_READING      => l_est_reading,
6234            X_ESTIMATED_PERIOD_START_RDG   => l_est_period_start_rdg,
6235            X_RETURN_STATUS                => l_return_status,
6236            X_MSG_COUNT                    => l_msg_cnt,
6237            X_MSG_DATA                     => l_msg_data
6238             );
6239 
6240         l_qty := nvl(l_qty,0) + nvl(l_estimated_qty,0);
6241         X_estimated_qty  := l_estimated_qty;
6242        --CALL COUNTER API HERE to get the estimated qty for the partial period
6243        -- Add estimated partial qty to l_qty
6244       END IF;
6245     END IF;
6246 
6247     /*Doing Settlement Here in Billing*/
6248     IF ((p_settle_interval = 'BP') AND (nvl(x_actual_qty,0) <> 0)) THEN
6249       OPEN   billed_period_csr(p_cle_id);
6250       FETCH  billed_period_csr  INTO l_bsl_count;
6251       CLOSE  billed_period_csr;
6252 
6253       l_total_min_qty := l_bsl_count * p_minimum_quantity;
6254 
6255       OPEN  total_billed_qty(p_cle_id);
6256       FETCH total_billed_qty  INTO l_total_bill_qty;
6257       CLOSE total_billed_qty;
6258 
6259       --IF (nvl(l_total_min_qty,0) > nvl(l_counter_value,0)  ) THEN
6260       IF (l_end_reading < l_base_reading) THEN
6261         X_flag := 'S';
6262         IF ( l_total_min_qty > l_base_reading) THEN
6263           l_qty  := l_total_min_qty - l_base_reading;
6264           X_flag := 'S';
6265           --l_end_reading := l_base_reading;
6266         ELSE
6267           --IF (l_base_reading < l_total_min_qty) THEN
6268           --  l_qty  :=  l_base_reading - l_total_min_qty;
6269           --  X_flag := 'S';
6270           --ELSE
6271             --l_qty  :=  l_end_reading - l_base_reading;
6272           IF (l_end_reading < l_total_min_qty) THEN
6273             l_qty  :=  l_total_min_qty - l_base_reading;
6274             X_flag := 'S';
6275           END IF;
6276           --l_end_reading := l_base_reading;
6277           --END IF;
6278         END IF;
6279       END IF;
6280       --END IF;
6281     END IF;
6282   END IF;
6283 
6284   /*
6285     If l_qty is zero after doing estimation and reading counter , then
6286     set l_qty to default_qty
6287   */
6288   -- BUG FIX 3519287 .Added QTY condition
6289   IF (nvl(l_qty,0) = 0) THEN
6290     IF((l_counter_date IS NULL) OR (p_usage_type = 'QTY')) THEN
6291       l_qty  := nvl(p_default_qty,0);
6292       x_flag := 'D';
6293     ELSE
6294       l_qty := 0;
6295     END IF;
6296   END IF;
6297 
6298   --IF ((p_calledfrom = 1) OR
6299   --   ((p_calledfrom = 3) AND ( x_flag = 'D'))) THEN
6300 
6301   IF ( x_flag = 'D') THEN
6302     -------------------------------------------------------------------------
6303     -- Begin partial period computation logic
6304     -- Developer Mani Choudhary
6305     -- Date 11-JUN-2005
6306     -- call oks_bill_rec_pub.Get_prorated_Usage_Qty to get the prorated usage
6307     -------------------------------------------------------------------------
6308     IF p_period_type IS NOT NULL AND
6309        p_period_start IS NOT NULL
6310     THEN
6311         OPEN l_Billuom_csr(p_cov_prd_end_date);
6312         FETCH l_Billuom_csr INTO l_uom_code;
6313         CLOSE l_Billuom_csr;
6314         IF (p_calledfrom = 3) THEN
6315 
6316           l_qty := OKS_BILL_REC_PUB.Get_Prorated_Usage_Qty
6317                        (
6318                        p_start_date  => p_cov_prd_start_date,
6319                        p_end_date    => p_cov_prd_end_date-1,
6320                        p_qty         => l_qty,
6321                        p_usage_uom   => p_usage_period,
6322                        p_billing_uom => l_uom_code,
6323                        p_period_type => p_period_type
6324                                );
6325           --for bug#5334983: no exception needs to be raised-the quantity will be zero
6326           --IF nvl(l_qty,0) = 0 THEN
6327           --  RAISE G_EXCEPTION_HALT_VALIDATION;
6328           --END IF;
6329         ELSE
6330           l_qty := OKS_BILL_REC_PUB.Get_Prorated_Usage_Qty
6331                        (
6332                        p_start_date  => p_cov_prd_start_date,
6333                        p_end_date    => p_cov_prd_end_date,
6334                        p_qty         => l_qty,
6335                        p_usage_uom   => p_usage_period,
6336                        p_billing_uom => l_uom_code,
6337                        p_period_type => p_period_type
6338                                );
6339           --for bug#5334983: no exception needs to be raised-the quantity will be zero
6340           --IF nvl(l_qty,0) = 0 THEN
6341           --  RAISE G_EXCEPTION_HALT_VALIDATION;
6342           --END IF;
6343 
6344         END IF;
6345 	 /* Commented for Bug#15897052 */
6346         --l_qty:= Round(l_qty,0);
6347     ELSE
6348         --Existing logic
6349         l_temp := OKS_TIME_MEASURES_PUB.GET_TARGET_QTY
6350                                (
6351                                p_start_date  => p_cov_prd_start_date,
6352                                p_source_qty  => 1,
6353                                p_source_uom  => p_usage_period,
6354                                p_target_uom  => p_time_uom_code,
6355                                p_round_dec   => 0
6356                                );
6357 /* Added p_calledfrom =2 by sjanakir as part of Bug# 6697952 */
6358        IF (p_calledfrom = 1 OR p_calledfrom =2) THEN
6359 	 /* Modified for Bug#15897052 */
6360          l_qty := ((l_qty * (p_cov_prd_end_date -
6361                              p_cov_prd_start_date + 1))/l_temp) ;
6362 
6363        ELSIF (p_calledfrom = 3) THEN
6364 	 /* Modified for Bug#15897052 */
6365          l_qty := ((l_qty * (p_cov_prd_end_date -
6366                              p_cov_prd_start_date ))/l_temp) ;
6367        END IF;
6368     END IF;  --period type and period start are not NULL
6369   END IF;
6370 
6371   l_billing_based_on  := fnd_profile.value('OKS_USAGE_BILLING_BASED_ON');
6372 
6373   X_qty               :=  nvl(l_qty,0);
6374   x_start_reading     := l_start_reading;
6375   IF(l_billing_based_on  = 'A') THEN
6376     x_end_reading       := nvl(l_end_reading,0)+ nvl(x_estimated_qty,0);
6377   ELSE
6378     x_end_reading       := nvl(l_end_reading,0);
6379   END IF;
6380   x_counter_value_id  := l_counter_value_id;
6381   x_counter_group_id  := l_counter_group_id;
6382   x_base_reading      := nvl(l_base_Reading,0);
6383 
6384 EXCEPTION
6385   WHEN G_EXCEPTION_HALT_VALIDATION THEN
6386     x_return_status := OKC_API.G_RET_STS_ERROR ;
6387   WHEN OTHERS THEN
6388      x_return_status := OKC_API.G_RET_STS_ERROR ;
6389 END Usage_qty_to_bill;
6390 
6391 --------------------------------------------------------------------------------
6392                            -- COunter_values
6393 -------------------------------------------------------------------------------
6394 Procedure Counter_Values
6395  (
6396   P_calledfrom        IN                NUMBER DEFAULT NULL,
6397   P_start_date        IN                DATE,
6398   P_end_date          IN                DATE,
6399   P_cle_id            IN                NUMBER,
6400   P_Usage_type        IN                VARCHAR2,
6401   X_Value            OUT        NOCOPY  NUMBER,
6402   X_Counter_Value    OUT        NOCOPY  NUMBER,
6403   X_Counter_Date     OUT        NOCOPY  DATE,
6404   X_Uom_Code         OUT        NOCOPY  VARCHAR2,
6405   X_end_reading      OUT        NOCOPY  NUMBER,
6406   X_start_reading    OUT        NOCOPY  NUMBER,
6407   X_base_reading     OUT        NOCOPY  NUMBER,
6408   X_counter_value_id OUT        NOCOPY  NUMBER,
6409   X_counter_group_id OUT        NOCOPY  NUMBER,
6410   X_counter_id       OUT        NOCOPY  NUMBER,
6411   X_return_status    OUT        NOCOPY  VARCHAR2
6412  )
6413  Is
6414 Cursor l_ctr_csr(p_counter_id Number)  Is    /*Removed the parameter p_override_valid_flag for bug 10390945*/
6415   SELECT  net_reading
6416          ,value_timestamp
6417          ,counter_value_id
6418          ,counter_group_id
6419        FROM   Cs_ctr_counter_values_v
6420        WHERE  counter_id = p_counter_id
6421        --AND    decode(p_override_valid_flag,'YES',nvl(override_valid_flag,'N'),'1') = decode(p_override_valid_flag,'YES','Y','1')  /*Removed for bug 10390945*/
6422        AND    nvl(valid_flag,'N') = 'N'
6423        /* Modified by sjanakir for Bug # 7168765
6424        Order by value_timestamp desc; */
6425        ORDER BY counter_value_id DESC;
6426 
6427 
6428  l_ctr_rec    l_ctr_csr%rowtype;
6429 
6430 Cursor l_actual_counter_csr(p_cle_id IN NUMBER) Is
6431   SELECT  nvl(end_reading,0)  qty
6432         FROM    Oks_bill_sub_line_dtls ldtl
6433                 ,oks_bill_sub_lines line
6434         WHERE   line.cle_id = p_cle_id
6435         AND     ldtl.bsl_id = line.id
6436         AND     nvl(end_reading,0) > 0
6437         AND     trunc(date_billed_from) < trunc(p_start_date)
6438         Order By date_billed_to desc ;
6439 
6440 Cursor l_actual_counter_csr_prv (p_cle_id IN NUMBER , p_start_date IN DATE) IS
6441   SELECT  nvl(end_reading,0) qty
6442        FROM    Oks_bsd_pr ldtl
6443               ,oks_bsl_pr line
6444        WHERE   line.cle_id = p_cle_id
6445        AND     ldtl.bsl_id = line.id
6446        AND     nvl(end_reading,0) > 0
6447        AND     trunc(date_billed_from) < trunc(p_start_date)
6448        Order By date_billed_to desc ,ldtl.creation_date  desc ;
6449 
6450 
6451 Cursor l_actual_csr(p_cle_id IN NUMBER) Is
6452   SELECT NVL(sign(ldtl.amount)*Result,0) + nvl(base_reading,0)  qty
6453        FROM    Oks_bill_sub_line_dtls ldtl
6454               ,oks_bill_sub_lines line
6455        WHERE   line.cle_id = p_cle_id
6456        AND     ldtl.bsl_id = line.id
6457        ORDER BY date_billed_to desc  ;
6458 
6459 
6460 
6461 Cursor l_actual_csr_prv(p_cle_id IN NUMBER) Is
6462   SELECT  NVL(sign(ldtl.amount)*Result,0) +  nvl(base_reading,0)  qty
6463        FROM    Oks_bsd_pr         ldtl
6464               ,oks_bsl_pr         line
6465        WHERE   line.cle_id = p_cle_id
6466        AND     ldtl.bsl_id = line.id
6467        ORDER BY date_billed_to desc;
6468 
6469 
6470 Cursor l_ccr_id_csr Is
6471   SELECT end_reading read,ccr_id,cgr_id
6472        FROM    Oks_bill_sub_line_dtls ldtl
6473                 ,oks_bill_sub_lines line
6474         WHERE   line.cle_id = p_cle_id
6475         AND     ldtl.bsl_id = line.id
6476 -- Bug#4730007 nechatur 1-Dec-2005
6477        -- Order By ldtl.id desc;
6478           Order By date_billed_to desc ;
6479 -- End Bug#4730007
6480 
6481 
6482 Cursor l_ccr_id_csr_prv (p_cle_id IN NUMBER , p_start_date IN DATE) IS
6483   SELECT end_reading read,ccr_id,cgr_id
6484       FROM    Oks_bsd_pr ldtl
6485                    ,oks_bsl_pr line
6486       WHERE   line.cle_id = p_cle_id
6487       AND     ldtl.bsl_id = line.id
6488       AND     trunc(line.date_billed_from) < trunc(p_start_date)
6489 -- Bug#4730007 nechatur 1-Dec-2005
6490        -- Order By ldtl.id desc;
6491           Order By date_billed_to desc ;
6492 -- End Bug#4730007
6493 
6494 /*modified for ER 16432647*/
6495 Cursor l_initial_csr Is
6496   SELECT nvl(okslin.base_Reading,0)  Base_reading
6497      FROM oks_k_lines_b okslin, Cs_ctr_counter_values_v ccr
6498      WHERE okslin.cle_id = p_cle_id
6499        AND okslin.counter_value_id = ccr.counter_value_id (+)
6500        AND nvl(ccr.valid_flag,'N') = 'N';
6501 
6502 
6503 Cursor l_ctr_value_csr(p_counter_id Number) Is   /*Removed the parameter p_override_valid_flag for bug 10390945*/
6504      SELECT  (ccr.counter_value_id)              /*Modified for bug:9778692*/
6505        FROM   Cs_ctr_counter_values_v ccr
6506        WHERE  ccr.counter_id = p_counter_id
6507        AND     trunc(ccr.Value_timestamp)  Between trunc(P_start_date) And trunc(P_end_date)
6508 ---       AND    decode(p_override_valid_flag,'YES',ccr.override_valid_flag,'1') =  decode(p_override_valid_flag,'YES','Y','1')  /*Removed for bug 10390945 */
6509        AND    nvl(valid_flag,'N') = 'N'
6510        ORDER BY value_timestamp desc,counter_value_id DESC;  /*Added for bug:9778692*/
6511 
6512 
6513 Cursor l_value_csr(p_max_ctr_id Number) Is
6514    SELECT  net_reading
6515           ,counter_group_id
6516           ,value_timestamp
6517        FROM   Cs_ctr_counter_values_v
6518        WHERE  counter_value_id = p_max_ctr_id
6519        AND    nvl(valid_flag,'N') = 'N';
6520 
6521 
6522 
6523 
6524 Cursor l_ctr_daily_csr(p_counter_id Number)  Is      /*Removed the parameter p_override_valid_flag for bug 10390945*/
6525     SELECT  (ccr.counter_value_id)                /*Modified for bug:9778692*/
6526        FROM   Cs_ctr_counter_values_v ccr
6527        WHERE  ccr.counter_id = p_counter_id
6528        AND     trunc(ccr.Value_timestamp) = trunc(P_start_date)
6529 ---       AND    decode(p_override_valid_flag,'YES',ccr.override_valid_flag,'1') = decode(p_override_valid_flag,'YES','Y','1')    /*Removed for bug 10390945*/
6530        AND    nvl(valid_flag,'N') = 'N'
6531         ORDER BY value_timestamp desc,counter_value_id DESC;  /*Added for bug:9778692*/
6532 
6533 Cursor inv_item_csr Is
6534     SELECT item.object1_id1
6535     FROM   OKC_K_ITEMS item
6536     WHERE  item.cle_id = p_cle_id;
6537 
6538 Cursor uom_csr(p_counter_id Number) Is
6539     SELECT uom_code
6540     FROM   CSI_COUNTERS_BC_V             /*Added for bug:8969993*/
6541     WHERE counter_id = p_counter_id;
6542 
6543  --Bug#5235116
6544 
6545 Cursor l_actual_term_csr(p_cle_id IN NUMBER) Is
6546   SELECT NVL(sign(ldtl.amount)*Result,0) + nvl(base_reading,0)  qty
6547        FROM    Oks_bill_sub_line_dtls ldtl
6548               ,oks_bill_sub_lines line
6549        WHERE   line.cle_id = p_cle_id
6550        AND     ldtl.bsl_id = line.id
6551        AND    trunc(date_billed_from) < trunc(p_start_date)
6552        ORDER BY date_billed_to desc;
6553 
6554  --End Bug#5235116
6555 
6556  l_counter_qty             NUMBER;
6557  l_qty                     NUMBER;
6558  l_init                    NUMBER;
6559  l_inv_rec                 INV_ITEM_CSR%ROWTYPE;
6560  l_max_ctr_id              NUMBER;
6561  l_ctr_grp_id              NUMBER;
6562  l_end_read                NUMBER;
6563  l_ccr_id                  NUMBER;
6564  l_cgr_id                  NUMBER;
6565  ---l_counter_validate_flag   VARCHAR2(4);    /*removed for bug 10390945*/
6566  l_billing_based_on        VARCHAR2(15);
6567  l_value_timestamp         DATE;
6568 BEGIN
6569   X_return_status := OKC_API.G_RET_STS_SUCCESS;
6570   G_LOG_YES_NO := Fnd_profile.value('OKS_BILLING_REPORT_AND_LOG'); ---- added by ansraj for 14016663
6571   ---l_counter_validate_flag := fnd_profile.value('OKS_COUNTER_VALIDATE');
6572 /*OKS_COUNTER_VALIDATE profile is no more used in R12 as the counter readings in IB are validated using disabled_flag and the same is implemented in OKS to validate the counter readings*/
6573   l_billing_based_on      := fnd_profile.value('OKS_USAGE_BILLING_BASED_ON');
6574 
6575   OPEN  inv_item_csr;
6576   FETCH inv_item_csr into l_inv_rec;
6577   CLOSE inv_item_csr;
6578 
6579   x_counter_id  := l_inv_rec.object1_id1;
6580 
6581   --Bug#5235116
6582  /*****************************
6583   IF (p_calledfrom = 3) THEN
6584     --Called from Settlement
6585     OPEN  l_actual_csr(p_cle_id);
6586     FETCH l_actual_csr into l_qty;
6587     CLOSE l_actual_csr;
6588 
6589     OPEN  l_ccr_id_csr;
6590     FETCH l_ccr_id_csr into l_end_read,l_ccr_id,l_cgr_id;
6591     CLOSE l_ccr_id_csr;
6592 
6593     OPEN  l_initial_csr;
6594     FETCH l_initial_csr into l_init;
6595     CLOSE l_initial_csr;
6596 
6597 *******************************/
6598 
6599   IF (p_calledfrom = 4) THEN  -- Changed '3' to '4' -- '3' refers to termination and '4' refers to settlement
6600     /* Called from Settlement */
6601     OPEN  l_actual_csr(p_cle_id);
6602     FETCH l_actual_csr into l_qty;
6603     CLOSE l_actual_csr;
6604 
6605     OPEN  l_ccr_id_csr;
6606     FETCH l_ccr_id_csr into l_end_read,l_ccr_id,l_cgr_id;
6607     CLOSE l_ccr_id_csr;
6608 
6609     OPEN  l_initial_csr;
6610     FETCH l_initial_csr into l_init;
6611     CLOSE l_initial_csr;
6612 
6613   ELSIF (p_calledfrom = 3) THEN
6614    --Called from termination
6615     OPEN  l_actual_term_csr(p_cle_id);
6616     FETCH l_actual_term_csr into l_qty;
6617     CLOSE l_actual_term_csr;
6618 
6619     OPEN  l_ccr_id_csr;
6620     FETCH l_ccr_id_csr into l_end_read,l_ccr_id,l_cgr_id;
6621     CLOSE l_ccr_id_csr;
6622 
6623     OPEN  l_initial_csr;
6624     FETCH l_initial_csr into l_init;
6625     CLOSE l_initial_csr;
6626 
6627  --End Bug#5235116
6628 
6629   ELSIF (p_calledfrom = 1) THEN
6630     IF ( nvl(l_billing_based_on,'X') = 'A') THEN
6631       OPEN  l_actual_csr(p_cle_id);
6632       FETCH l_actual_csr into l_qty;
6633       CLOSE l_actual_csr;
6634     ELSE
6635       OPEN  l_actual_counter_csr(p_cle_id);
6636       FETCH l_actual_counter_csr into l_qty;
6637       CLOSE l_actual_counter_csr;
6638     END IF;
6639 
6640     OPEN  l_ccr_id_csr;
6641     FETCH l_ccr_id_csr into l_end_read,l_ccr_id,l_cgr_id;
6642     CLOSE l_ccr_id_csr;
6643 
6644     OPEN  l_initial_csr;
6645     FETCH l_initial_csr into l_init;
6646     CLOSE l_initial_csr;
6647 
6648   ELSIF (P_calledfrom = 2) THEN
6649     /* l_actual_csr and l_ccr_id_csr of normal mode is required to be called
6650        in preview mode also.This is necessary because user may run JAN
6651        billing in actual mode and then run feb billing in preview mode.
6652        If the call to l_actual_csr and l_ccr_id_csr is not made then preview
6653        mode does not reflect proper reading
6654      */
6655 
6656     IF ( nvl(l_billing_based_on,'X') = 'A') THEN
6657       OPEN  l_actual_csr_prv(p_cle_id);
6658       FETCH l_actual_csr_prv into l_qty;
6659       CLOSE l_actual_csr_prv;
6660     ELSE
6661       OPEN  l_actual_counter_csr_prv(p_cle_id,p_start_date);
6662       FETCH l_actual_counter_csr_prv into l_qty;
6663       CLOSE l_actual_counter_csr_prv;
6664     END IF;
6665 
6666     IF (l_qty is NULL) THEN
6667       IF ( nvl(l_billing_based_on,'X') = 'A') THEN
6668         OPEN  l_actual_csr(p_cle_id);
6669         FETCH l_actual_csr into l_qty;
6670         CLOSE l_actual_csr;
6671       ELSE
6672         OPEN  l_actual_counter_csr(p_cle_id);
6673         FETCH l_actual_counter_csr into l_qty;
6674         CLOSE l_actual_counter_csr;
6675       END IF;
6676     END IF;
6677 
6678     OPEN  l_ccr_id_csr_prv (p_cle_id,p_start_date);
6679     FETCH l_ccr_id_csr_prv into l_end_read,l_ccr_id,l_cgr_id;
6680     CLOSE l_ccr_id_csr_prv;
6681 
6682     IF (l_end_read is NULL) THEN
6683       OPEN  l_ccr_id_csr ;
6684       FETCH l_ccr_id_csr INTO l_end_read,l_ccr_id ,l_cgr_id;
6685       CLOSE l_ccr_id_csr;
6686     END IF;
6687 
6688     OPEN  l_initial_csr;
6689     FETCH l_initial_csr into l_init;
6690     CLOSE l_initial_csr;
6691 
6692   END IF;
6693 
6694 
6695   OPEN  uom_csr(l_inv_rec.object1_id1);
6696   FETCH uom_csr into X_uom_code;
6697   CLOSE Uom_csr;
6698 
6699   IF (P_Usage_type = 'QTY') THEN
6700 
6701     OPEN  l_ctr_csr(l_inv_rec.object1_id1);      ---Modified for bug 10390945
6702     FETCH l_ctr_csr into l_ctr_rec;
6703     CLOSE l_ctr_csr;
6704 
6705     IF (l_ctr_rec.net_reading Is Null) THEN
6706       X_Value := 0; -- Null;
6707 
6708     ELSE
6709       IF ( l_ccr_id Is Not Null) THEN
6710         IF l_ctr_rec.counter_value_id = l_ccr_id THEN
6711           X_value := 0 ;
6712           IF ( nvl(l_qty,0) = 0  ) THEN
6713             X_base_reading     := nvl(l_init,0) ;
6714           ELSE
6715             X_base_reading     := nvl(l_qty,0) ;
6716           END IF;
6717         ELSE
6718           --IF ( l_qty is NULL) THEN
6719           IF ( nvl(l_qty,0) = 0 ) THEN
6720             X_value := nvl(l_ctr_rec.net_reading,0)  -  nvl(l_init,0);
6721             X_base_reading     := nvl(l_init,0) ;
6722           ELSE
6723             X_value := nvl(l_ctr_rec.net_reading,0)  - nvl(l_qty,0);
6724             X_base_reading     := nvl(l_qty,0) ;
6725           END IF;
6726         END IF;
6727       ELSE
6728         --IF ( l_qty is NULL) THEN
6729         IF ( nvl(l_qty,0) = 0 ) THEN
6730           X_value := nvl(l_ctr_rec.net_reading,0) -  nvl(l_init,0);
6731           X_base_reading     := nvl(l_init,0) ;
6732         ELSE
6733           X_value := nvl(l_ctr_rec.net_reading,0) - nvl(l_qty,0);
6734           X_base_reading     := nvl(l_qty,0) ;
6735         END IF;
6736 
6737       END IF;
6738     END IF;
6739 
6740     X_start_reading    := nvl(l_end_read,l_init);
6741     X_end_reading      := nvl(l_ctr_rec.net_reading,nvl(l_end_read,l_init));
6742     X_counter_value_id := nvl(l_ctr_rec.counter_value_id,l_ccr_id);
6743     X_counter_group_id := nvl(l_ctr_rec.counter_group_id,l_cgr_id);
6744     X_Counter_Date     := l_ctr_rec.value_timestamp;
6745     X_Counter_Value    := nvl(l_ctr_rec.net_reading,0);
6746 
6747   ELSIF (P_Usage_type = 'VRT')  Then
6748 
6749     IF (Trunc(P_start_date) = Trunc(P_end_date))  THEN
6750       OPEN  l_ctr_daily_csr(l_inv_rec.object1_id1);    ---Modified for bug 10390945
6751       FETCH l_ctr_daily_csr into l_max_ctr_id;
6752       IF (l_ctr_daily_csr%notfound) THEN
6753         l_max_ctr_id := Null;
6754       END IF;
6755       CLOSE l_ctr_daily_csr;
6756       IF (l_max_ctr_id is not null)  THEN
6757         OPEN  l_value_csr(l_max_ctr_id);
6758         FETCH l_value_csr into l_counter_qty ,l_ctr_grp_id,l_value_timestamp;
6759         CLOSE l_value_csr;
6760       END IF;
6761 
6762     ELSE
6763       l_max_ctr_id := NULL;
6764       OPEN  l_ctr_value_csr(l_inv_rec.object1_id1);     ----Modified for bug 10390945
6765       FETCH l_ctr_value_csr into l_max_ctr_id;
6766       IF (l_ctr_value_csr%notfound)  THEN
6767         l_max_ctr_id := Null;
6768       END IF;
6769       CLOSE l_ctr_value_csr;
6770 
6771       IF (l_max_ctr_id is not null)  THEN
6772         OPEN  l_value_csr(l_max_ctr_id);
6773         FETCH l_value_csr into l_counter_qty ,l_ctr_grp_id,l_value_timestamp;
6774         CLOSE l_value_csr;
6775       END IF;
6776     END IF;
6777 
6778     IF (l_max_ctr_id Is Null) THEN
6779       X_value := 0 ;
6780       IF ( nvl(l_qty,0) = 0  ) THEN
6781         X_base_reading     := nvl(l_init,0) ;
6782       ELSE
6783         X_base_reading     := nvl(l_qty,0) ;
6784       END IF;
6785     ELSE
6786       --IF (l_qty is NULL ) THEN
6787       IF ( nvl(l_qty,0) = 0 ) THEN
6788         X_value := nvl(l_counter_qty,0)  - nvl(l_init,0);
6789         X_base_reading     := nvl(l_init,0) ;
6790       ELSE
6791         X_value := nvl(l_counter_qty,0)  - nvl(l_qty,0);
6792         X_base_reading     := nvl(l_qty,0) ;
6793       END IF;
6794     END IF;
6795 
6796 IF G_LOG_YES_NO = 'YES' THEN --- g_log_yes_no added  by ansraj for 14016663
6797     FND_FILE.PUT_LINE(FND_FILE.LOG, 'counter '||l_counter_qty);
6798     FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_qty '||l_qty);
6799     FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_init '||l_init);
6800 END IF;
6801 
6802     X_start_reading := nvl(l_end_read,l_init);
6803     /* Counter values should not be honored till it is greater than intial reading */
6804     IF ( nvl(l_counter_qty,0) < nvl(l_init,0)) THEN
6805       X_end_reading   := nvl(l_end_read,nvl(l_init,0));
6806       X_Value         := 0;
6807     ELSE
6808       X_end_reading   := nvl(l_counter_qty,nvl(l_end_read,l_init));
6809     END IF;
6810     X_counter_value_id := nvl(l_max_ctr_id,l_ccr_id);
6811     X_counter_group_id := nvl(l_ctr_grp_id , l_cgr_id);
6812     X_Counter_Date     := l_value_timestamp;
6813     X_Counter_Value    := nvl(l_counter_qty,0);
6814 
6815   END IF;
6816 EXCEPTION
6817   WHEN OTHERS THEN
6818     x_return_status := OKC_API.G_RET_STS_UNEXP_ERROR;
6819     OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,
6820                           G_SQLCODE_TOKEN,SQLCODE,G_SQLERRM_TOKEN,SQLERRM);
6821 END counter_values;
6822 
6823 
6824 
6825   ---------------------------------------------------------------------------
6826   -- FUNCTION get_bill_amount_period
6827   ---------------------------------------------------------------------------
6828 
6829   PROCEDURE get_bill_amount_period (
6830     P_CALLEDFROM                   IN          NUMBER,
6831     p_con_start_date               IN          DATE,
6832     p_con_end_date                 IN          DATE,
6833     p_bill_calc_period             IN          VARCHAR2,
6834     p_con_amount                   IN          NUMBER,
6835     p_bill_start_date              IN          DATE,
6836     p_bill_end_date                IN          DATE,
6837     p_stat                         IN          NUMBER,
6838     x_amount                       OUT NOCOPY  NUMBER
6839   ) IS
6840     l_bill_amount           NUMBER ;
6841     l_bill_amount1          NUMBER ;
6842     l_diff                  NUMBER;
6843     l_diff1                 NUMBER;
6844     l_bill_calc_period      VARCHAR2(20);
6845     l_num_of_full_periods   NUMBER := 0;
6846     l_remainder             NUMBER;
6847     l_prorate_amount        NUMBER;
6848 
6849 Cursor l_uom_csr(p_code Varchar2) Is
6850       Select tce_code
6851              ,quantity
6852       From   Okc_time_code_units_v
6853       Where  uom_code = p_code
6854       And    active_flag = 'Y';
6855 
6856 l_tce_code   varchar2(10);
6857 l_qty        number;
6858 
6859 BEGIN
6860   OPEN  l_uom_csr(p_bill_calc_period);
6861   FETCH l_uom_csr into l_tce_code,l_qty;
6862   CLOSE l_uom_csr;
6863 
6864   IF ( p_stat = G_NONREGULAR)  THEN
6865     l_tce_code  := 'DAY';
6866     l_qty := 1;
6867   END IF;
6868 
6869   IF( p_bill_calc_period is Null) THEN
6870     l_diff := 1;
6871 
6872   ELSE
6873     IF ((UPPER(l_tce_code) = 'MONTH') And (l_qty = 1)) THEN
6874       l_num_of_full_periods := trunc(Months_between((p_con_end_date+1),p_bill_start_date));
6875       l_diff := abs(trunc((p_con_end_date+1) - add_months(p_bill_start_date,l_num_of_full_periods)));
6876     ELSIF ((UPPER(l_tce_code) = 'DAY')   and (l_qty = 7)) THEN
6877       l_num_of_full_periods := TRUNC(((p_con_end_date+1) - p_bill_start_date)/l_qty);
6878       l_diff := abs(trunc((p_con_end_date + 1) -(p_bill_start_date + (l_num_of_full_periods*l_qty))));
6879     ELSIF ((UPPER(l_tce_code) = 'DAY') and (l_qty = 1)) THEN
6880       l_num_of_full_periods := 0;
6881       l_diff := abs(TRUNC(p_con_end_date - p_bill_start_date)) + 1;
6882     ELSIF ((UPPER(l_tce_code) = 'MONTH') And (l_qty = 3)) THEN
6883       l_num_of_full_periods := TRUNC(TRUNC(Months_between(p_con_end_date+1,p_bill_start_date))/l_qty);
6884       l_diff := abs(trunc((p_con_end_date+1) - add_months(p_bill_start_date,(l_num_of_full_periods*l_qty))));
6885 
6886     ELSIF (( UPPER(l_tce_code) = 'YEAR') And (l_qty = 1)) THEN
6887       l_num_of_full_periods := TRUNC(TRUNC(Months_between((p_con_end_date+1),p_bill_start_date))/12);
6888       l_diff := abs(trunc((p_con_end_date+1) - add_months(p_bill_start_date,(l_num_of_full_periods*12))));
6889     END IF;
6890   END IF;
6891 
6892 
6893   IF ( nvl(P_CALLEDFROM,0) = -99)  THEN   --- for termination
6894     l_prorate_amount :=p_con_amount/abs(trunc((p_bill_start_date - p_con_end_date ) +1));
6895   ELSE
6896     l_prorate_amount :=p_con_amount/abs(trunc((p_con_end_date - p_bill_start_date) +1));
6897   END IF;
6898 
6899   IF (l_num_of_full_periods = 0) THEN
6900     l_diff :=  abs(trunc((p_bill_end_date  -  p_bill_start_date) )) + 1;
6901     l_bill_amount := (l_prorate_amount * l_diff);
6902   ELSE
6903     l_bill_amount := (p_con_amount - (l_prorate_amount * l_diff))/l_num_of_full_periods;
6904   END IF;
6905 
6906     --FND_FILE.PUT_LINE(FND_FILE.LOG,'bill_amount '||l_bill_amount);
6907 
6908   IF (p_stat = Null)  THEN
6909     x_amount := Null;
6910   END If;
6911 
6912   x_amount := l_bill_amount;
6913 
6914 EXCEPTION
6915    WHEN  OTHERS THEN
6916     Null;
6917     OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN, SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
6918 END get_bill_amount_period;
6919 
6920 
6921 ------------------------------------------------------------------------
6922   -- FUNCTION update_bsl
6923 ------------------------------------------------------------------------
6924 PROCEDURE update_bsl
6925 (
6926    x_ret_stat       OUT NOCOPY  VARCHAR2,
6927    p_dnz_chr_id     IN          NUMBER,
6928    p_bsl_id         IN          NUMBER,
6929    p_bcl_id         IN          NUMBER,
6930    P_AMOUNT         IN          NUMBER,
6931    P_currency_code  IN          Varchar2,
6932    P_PRV            IN          NUMBER
6933 ) IS
6934 
6935 l_amount_holder      NUMBER       := 0;
6936 l_cur_holder         VARCHAR2(10) ;
6937 
6938 round_amount NUMBER  := 0;
6939 l_euro_conversion    VARCHAR2(10);
6940 l_euro_currency_code VARCHAR2(10);
6941 l_con_rate           NUMBER;
6942 l_con_date           DATE;
6943 l_con_type           VARCHAR2(20);
6944 cvn_not_found        EXCEPTION;
6945 
6946 Cursor l_get_conversion_rule (p_chr_id IN NUMBER) is
6947  SELECT conversion_rate        con_rate,
6948         conversion_rate_date   con_date,
6949         conversion_type        con_type
6950     FROM okc_k_headers_b
6951     WHERE id = p_chr_id;
6952 
6953 
6954 BEGIN
6955 
6956   x_ret_stat := 'S';
6957 
6958   l_euro_conversion := okc_currency_api.IS_EURO_CONVERSION_NEEDED
6959                                                    (p_currency_code);
6960 
6961   IF (l_euro_conversion = 'Y') Then
6962     OPEN  l_get_conversion_rule(p_dnz_chr_id);
6963     FETCH l_get_conversion_rule into l_con_rate,l_con_date,l_con_type;
6964     IF (l_get_conversion_rule%NOTFOUND) THEN
6965       RAISE  cvn_not_found;
6966     END IF;
6967     CLOSE l_get_conversion_rule;
6968 
6969     l_euro_currency_code :=okc_currency_api.GET_EURO_CURRENCY_CODE
6970                                                    (p_currency_code);
6971   END IF;
6972 
6973   round_amount := 0;
6974 
6975   IF (l_euro_conversion = 'Y') THEN
6976 
6977     l_cur_holder   := l_euro_currency_code;
6978     okc_currency_api.CONVERT_AMOUNT
6979            (P_FROM_CURRENCY         => p_currency_code,
6980             P_TO_CURRENCY           => l_euro_currency_code,
6981             P_CONVERSION_DATE       => l_con_date,
6982             P_CONVERSION_TYPE       => l_con_type,
6983             P_AMOUNT                => abs(P_AMOUNT) ,
6984             X_CONVERSION_RATE       => l_con_rate   ,
6985             X_CONVERTED_AMOUNT      => l_amount_holder
6986             );
6987 
6988 
6989       ---Added as passing abs val to convert_amount
6990         IF nvl(p_AMOUNT,0) < 0  Then
6991            l_amount_holder := -1 * l_amount_holder;
6992         END IF;
6993 
6994   ELSE
6995     l_amount_holder :=   P_AMOUNT;
6996     l_cur_holder    :=   p_currency_code;
6997   END IF;
6998 
6999   /**
7000             * function added to round off the amount depending on the
7001             * precision set in fnd_currency  -- Hari 08/03/2001
7002   **/
7003 
7004   round_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(
7005                                                  l_amount_holder,
7006                                                  l_cur_holder );
7007 
7008 
7009   IF (P_PRV = 1 ) THEN
7010 
7011     UPDATE oks_bill_cont_lines
7012     SET    amount = nvl(amount,0) + round_amount,
7013            currency_code = l_cur_holder
7014     WHERE id = p_bcl_id;
7015 
7016     UPDATE oks_bill_sub_lines
7017     SET    amount = round_amount,
7018            average = 0
7019     WHERE id = p_bsl_id;
7020 
7021     UPDATE oks_bill_sub_line_dtls
7022     SET    amount = round_amount
7023     WHERE bsl_id = p_bsl_id;
7024 
7025   ELSIF (P_PRV = 2) THEN
7026 
7027     UPDATE oks_bcl_pr
7028     SET    amount = nvl(amount,0) + round_amount,
7029            currency_code = l_cur_holder
7030     WHERE id = p_bcl_id;
7031 
7032     UPDATE oks_bsl_pr
7033     SET    amount = round_amount,
7034            average = 0
7035     WHERE id = p_bsl_id;
7036 
7037     UPDATE oks_bsd_pr
7038     SET    amount = round_amount
7039     WHERE bsl_id = p_bsl_id;
7040 
7041   END IF;
7042 
7043 EXCEPTION
7044  WHEN  cvn_not_found  THEN
7045    x_ret_stat := 'E';
7046  WHEN  OTHERS THEN
7047    x_ret_stat := 'E';
7048    OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN,SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
7049 
7050 END update_bsl;
7051 
7052 
7053 ------------------------------------------------------------------------
7054   -- FUNCTION update_bcl
7055 ------------------------------------------------------------------------
7056 PROCEDURE update_bcl
7057 (
7058    P_CALLEDFROM     IN          NUMBER,
7059    x_ret_stat       OUT NOCOPY  VARCHAR2,
7060    p_bcl_id         IN          NUMBER,
7061    P_SENT_YN        IN          VARCHAR2,
7062    P_BILL_ACTION    IN          VARCHAR2,
7063    P_AMOUNT         IN          NUMBER,
7064    P_CURRENCY_CODE  IN          VARCHAR2,
7065    P_PRV            IN          NUMBER
7066 ) IS
7067 
7068 SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
7069 l_bclv_tbl_in    l_bclv_tbl_type_in;
7070 l_bclv_tbl_out   l_bclv_tbl_type_in;
7071 
7072 SUBTYPE l_bcl_pr_tbl_type  is OKS_BCL_PRINT_PREVIEW_PVT.bcl_pr_tbl_type;
7073 l_bcl_pr_tbl_in    l_bcl_pr_tbl_type;
7074 l_bcl_pr_tbl_out   l_bcl_pr_tbl_type;
7075 
7076 l_ret_stat       Varchar2(20);
7077 l_msg_cnt        Number;
7078 l_msg_data       Varchar2(2000);
7079 
7080 BEGIN
7081 
7082 
7083   IF (P_PRV = 1 ) THEN
7084     l_bclv_tbl_in(1).ID                    := p_bcl_id;
7085     l_bclv_tbl_in(1).currency_code         := p_currency_code;
7086 
7087     IF (p_amount IS not Null) THEN
7088       l_bclv_tbl_in(1).AMOUNT := nvl(p_amount,0);
7089     END If;
7090     IF (p_bill_action IS not Null) THEN
7091       l_bclv_tbl_in(1).bill_action := G_billaction_tr;
7092     END If;
7093     IF (p_sent_yn IS not Null)  THEN
7094       l_bclv_tbl_in(1).sent_yn := p_sent_yn;
7095     END If;
7096 
7097     UPDATE oks_bill_cont_lines
7098     SET    amount = nvl(p_amount,0) + nvl(amount,0),
7099            bill_Action = nvl(p_bill_action,bill_action) ,
7100            currency_code = p_currency_code,
7101            sent_yn     = nvl(p_sent_yn , sent_yn)
7102       WHERE id = l_bclv_tbl_in(1).ID   ;
7103 
7104 
7105   ELSIF (P_PRV = 2) THEN
7106     l_bcl_pr_tbl_in(1).ID                    := p_bcl_id;
7107 
7108     IF (p_amount IS not Null)  THEN
7109       l_bcl_pr_tbl_in(1).AMOUNT := nvl(p_amount,0);
7110     END If;
7111     IF (p_bill_action IS not Null)  THEN
7112       l_bcl_pr_tbl_in(1).bill_action := G_billaction_tr;
7113     END If;
7114     IF (p_sent_yn IS not Null)  THEN
7115       l_bcl_pr_tbl_in(1).sent_yn := p_sent_yn;
7116     END If;
7117 
7118     UPDATE oks_bcl_pr
7119     SET    amount = nvl(p_amount,0) + nvl(amount,0),
7120            bill_Action = nvl(p_bill_action,bill_action) ,
7121            currency_code = p_currency_code,
7122            sent_yn     = nvl(p_sent_yn , sent_yn)
7123       WHERE id = l_bcl_pr_tbl_in(1).ID   ;
7124 
7125 
7126 
7127   END IF;
7128               x_ret_stat := l_ret_stat;
7129      EXCEPTION
7130            When  Others Then
7131                Null;
7132                OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN,SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
7133  END update_bcl;
7134 
7135 
7136 ------------------------------------------------------------------------
7137   -- FUNCTION Insert_bcl
7138 ------------------------------------------------------------------------
7139 PROCEDURE insert_bcl
7140 (
7141  P_CALLEDFROM        IN NUMBER,
7142  x_return_stat      OUT     NOCOPY VARCHAR2,
7143  p_CLE_ID            IN            NUMBER,
7144  p_DATE_BILLED_FROM  IN            DATE,
7145  P_DATE_BILLED_TO    IN            DATE,
7146  P_DATE_NEXT_INVOICE IN            DATE,
7147  P_BILL_ACTION       IN            VARCHAR2 ,
7148  P_OKL_FLAG          IN            NUMBER,
7149  P_PRV               IN            NUMBER,
7150  P_MSG_COUNT         IN OUT NOCOPY NUMBER,
7151  P_MSG_DATA          IN OUT NOCOPY VARCHAR2,
7152  X_BCL_ID            IN OUT NOCOPY NUMBER
7153  )IS
7154 SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
7155    l_bclv_tbl_in   l_bclv_tbl_type_in;
7156    l_bclv_tbl_out   l_bclv_tbl_type_in;
7157 
7158 SUBTYPE l_bcl_pr_tbl_type is OKS_BCL_PRINT_PREVIEW_PVT.bcl_pr_tbl_type;
7159    l_bcl_pr_tbl_in  l_bcl_pr_tbl_type;
7160    l_bcl_pr_tbl_out l_bcl_pr_tbl_type;
7161 
7162 l_ret_stat    Varchar2(20);
7163 l_msg_cnt  Number;
7164 l_msg_data Varchar2(2000);
7165 
7166 l_bcl_id   NUMBER;
7167 
7168 --- -55 check for OKL issue raise on bug # 4634345
7169 Cursor l_bcl_csr(p_cle_id_in Number,p_date_billed_from_in date,p_date_billed_to_in date) Is
7170   Select  Id
7171          From    Oks_bill_cont_lines
7172          Where   Cle_id = p_cle_id_in
7173          And trunc(date_billed_from) = trunc(p_date_billed_from_in)
7174          And trunc(date_billed_to)   = trunc(p_date_billed_to_in)
7175             And bill_action = p_bill_action
7176          And (btn_id is null OR btn_id = -55)
7177          Order by Date_billed_from desc;
7178 
7179 Cursor l_bcl_pr_csr(p_cle_id_in Number,p_date_billed_from_in date,p_date_billed_to_in date) Is
7180   Select  Id
7181          From    Oks_bcl_pr
7182          Where   Cle_id = p_cle_id_in
7183          And     trunc(date_billed_from) = trunc(p_date_billed_from_in)
7184          And     trunc(date_billed_to)   = trunc(p_date_billed_to_in)
7185             And     bill_action = p_bill_action
7186          And (btn_id is null OR btn_id = -55)
7187          Order by Date_billed_from desc;
7188 
7189 BEGIN
7190 
7191  IF (P_PRV = 1 ) THEN   --NORMAL PROCESSING
7192    l_bclv_tbl_in(1).CLE_ID  := p_cle_id;
7193    l_bclv_tbl_in(1).DATE_BILLED_FROM := p_DATE_BILLED_FROM;
7194    l_bclv_tbl_in(1).DATE_BILLED_TO := P_DATE_BILLED_TO;
7195    l_bclv_tbl_in(1).Date_Next_Invoice := p_date_next_invoice;
7196    l_bclv_tbl_in(1).BILL_ACTION := p_bill_action;
7197    l_bclv_tbl_in(1).sent_yn := 'N';
7198 
7199    IF (P_OKL_FLAG = 1 ) THEN   --Check for OKL contract
7200      l_bclv_tbl_in(1).BTN_ID  := -55;
7201    END IF;
7202 
7203    OPEN  l_bcl_csr(p_cle_id ,p_date_billed_from ,p_date_billed_to );
7204    FETCH l_bcl_csr into l_bcl_id;
7205 
7206    IF (l_bcl_csr%NOTFOUND) THEN
7207      OKS_BILLCONTLINE_PUB.insert_Bill_Cont_Line(
7208        P_API_VERSION                  =>  1.0,
7209        P_INIT_MSG_LIST                =>  'T',
7210        X_RETURN_STATUS                =>   l_ret_stat,
7211        X_MSG_COUNT                    =>   l_msg_cnt,
7212        X_MSG_DATA                     =>   l_msg_data,
7213        P_BCLV_TBL                     =>   l_bclv_tbl_in,
7214        X_BCLV_TBL                     =>   l_bclv_tbl_out
7215        );
7216 
7217       x_bcl_id := l_bclv_tbl_out(1).id;
7218    ELSE
7219       x_bcl_id := l_bcl_id;
7220    END IF;
7221    CLOSE l_bcl_csr;
7222  ELSIF (P_PRV = 2) THEN
7223    l_bcl_pr_tbl_in(1).ID      := get_seq_id;
7224    l_bcl_pr_tbl_in(1).CLE_ID  := p_cle_id;
7225    l_bcl_pr_tbl_in(1).DATE_BILLED_FROM := p_DATE_BILLED_FROM;
7226    l_bcl_pr_tbl_in(1).DATE_BILLED_TO := P_DATE_BILLED_TO;
7227    l_bcl_pr_tbl_in(1).Date_Next_Invoice := p_date_next_invoice;
7228    l_bcl_pr_tbl_in(1).BILL_ACTION := p_bill_action;
7229    l_bcl_pr_tbl_in(1).sent_yn := 'N';
7230    l_bcl_pr_tbl_in(1).created_by := FND_GLOBAL.user_id;
7231    l_bcl_pr_tbl_in(1).last_updated_by := FND_GLOBAL.user_id;
7232    l_bcl_pr_tbl_in(1).creation_date  := sysdate;
7233    l_bcl_pr_tbl_in(1).last_update_date := sysdate;
7234    l_bcl_pr_tbl_in(1).object_version_number := 1;
7235    l_bcl_pr_tbl_in(1).amount := 0;
7236    l_bcl_pr_tbl_in(1).date_next_invoice  := NULL;
7237    l_bcl_pr_tbl_in(1).last_update_login  := NULL;
7238    l_bcl_pr_tbl_in(1).attribute_category  := NULL;
7239    l_bcl_pr_tbl_in(1).attribute1  := NULL;
7240    l_bcl_pr_tbl_in(1).attribute2  := NULL;
7241    l_bcl_pr_tbl_in(1).attribute3  := NULL;
7242    l_bcl_pr_tbl_in(1).attribute4  := NULL;
7243    l_bcl_pr_tbl_in(1).attribute5  := NULL;
7244    l_bcl_pr_tbl_in(1).attribute6  := NULL;
7245    l_bcl_pr_tbl_in(1).attribute7  := NULL;
7246    l_bcl_pr_tbl_in(1).attribute8  := NULL;
7247    l_bcl_pr_tbl_in(1).attribute9  := NULL;
7248    l_bcl_pr_tbl_in(1).attribute10  := NULL;
7249    l_bcl_pr_tbl_in(1).attribute11  := NULL;
7250    l_bcl_pr_tbl_in(1).attribute12  := NULL;
7251    l_bcl_pr_tbl_in(1).attribute13  := NULL;
7252    l_bcl_pr_tbl_in(1).attribute14  := NULL;
7253    l_bcl_pr_tbl_in(1).attribute15  := NULL;
7254    l_bcl_pr_tbl_in(1).security_group_id  := NULL;
7255 
7256    IF (P_OKL_FLAG = 1 ) THEN   --Check for OKL contract
7257      l_bcl_pr_tbl_in(1).BTN_ID  := -55;
7258    ELSE
7259      l_bcl_pr_tbl_in(1).BTN_ID  := NULL;
7260    END IF;
7261 
7262    OPEN  l_bcl_pr_csr(p_cle_id ,p_date_billed_from ,p_date_billed_to );
7263    FETCH l_bcl_pr_csr into l_bcl_id;
7264 
7265    IF (l_bcl_pr_csr%NOTFOUND)  THEN
7266      OKS_BCL_PRINT_PREVIEW_PUB.insert_bcl_pr(
7267        P_API_VERSION          => 1.0,
7268        P_INIT_MSG_LIST        => 'T',
7269        X_RETURN_STATUS        => l_ret_stat,
7270        X_MSG_COUNT            => l_msg_cnt,
7271        X_MSG_DATA             => l_msg_data,
7272        P_BCL_PR_TBL           => l_bcl_pr_tbl_in,
7273        X_BCL_PR_TBL           => l_bcl_pr_tbl_out);
7274 
7275        x_bcl_id := l_bcl_pr_tbl_out(1).id;
7276    ELSE
7277       x_bcl_id := l_bcl_id;
7278    END IF;
7279    CLOSE l_bcl_pr_csr;
7280  END IF;
7281  IF (l_ret_stat <> 'S' ) THEN
7282       get_message(l_msg_cnt  => l_msg_cnt,
7283                   l_msg_data => l_msg_data);
7284       p_msg_count := l_msg_cnt;
7285       p_msg_data  := l_msg_data;
7286  END IF;
7287 
7288    x_return_stat := l_ret_stat;
7289 EXCEPTION
7290    When  Others Then
7291         OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN,SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
7292 
7293 END insert_bcl;
7294 
7295 
7296 
7297 PROCEDURE get_bcl_id
7298 (
7299  P_CALLEDFROM       IN           NUMBER,
7300  X_RETURN_STAT     OUT   NOCOPY  VARCHAR2,
7301  P_CLE_ID           IN           NUMBER,
7302  P_DATE_BILLED_FROM IN           DATE,
7303  P_DATE_BILLED_TO   IN           DATE,
7304  P_BILL_ACTION      IN           VARCHAR2 ,
7305  X_BCL_ID          OUT   NOCOPY  NUMBER,
7306  X_BCL_AMOUNT      OUT   NOCOPY  NUMBER,
7307  P_PRV              IN           NUMBER
7308  )IS
7309 SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
7310    l_bclv_tbl_in   l_bclv_tbl_type_in;
7311    l_bclv_tbl_out   l_bclv_tbl_type_in;
7312 
7313 SUBTYPE l_bcl_pr_tbl_type is OKS_BCL_PRINT_PREVIEW_PVT.bcl_pr_tbl_type;
7314    l_bcl_pr_tbl_in  l_bcl_pr_tbl_type;
7315    l_bcl_pr_tbl_out l_bcl_pr_tbl_type;
7316 
7317 l_ret_stat    Varchar2(20);
7318 l_msg_cnt  Number;
7319 l_msg_data Varchar2(2000);
7320 
7321 Cursor l_bcl_csr(p_cle_id_in Number,p_date_billed_from_in date,p_date_billed_to_in date) Is
7322   Select  Id,amount
7323          From    Oks_bill_cont_lines
7324          Where   Cle_id = p_cle_id_in
7325          And trunc(date_billed_from) = trunc(p_date_billed_from_in)
7326          And trunc(date_billed_to)   = trunc(p_date_billed_to_in)
7327             And     bill_Action = p_bill_action
7328          And ( btn_id is null or btn_id = -55)
7329          Order by Date_billed_from desc;
7330 
7331 Cursor l_bcl_pr_csr(p_cle_id_in Number,p_date_billed_from_in date,p_date_billed_to_in date) Is
7332   Select  Id,amount
7333          From    Oks_bcl_pr
7334          Where   Cle_id = p_cle_id_in
7335          And     trunc(date_billed_from) = trunc(p_date_billed_from_in)
7336          And     trunc(date_billed_to)   = trunc(p_date_billed_to_in)
7337          And     bill_Action = p_bill_action
7338          And ( btn_id is null or btn_id = -55)
7339          Order by Date_billed_from desc;
7340 
7341 BEGIN
7342 
7343  IF (P_PRV = 1 ) THEN
7344    open  l_bcl_csr(p_cle_id ,p_date_billed_from ,p_date_billed_to );
7345    Fetch l_bcl_csr into x_bcl_id,x_bcl_amount;
7346    close l_bcl_csr;
7347  ELSIF (P_PRV = 2) THEN
7348    open  l_bcl_pr_csr(p_cle_id ,p_date_billed_from ,p_date_billed_to );
7349    Fetch l_bcl_pr_csr into x_bcl_id,x_bcl_amount;
7350    close l_bcl_pr_csr;
7351  END IF;
7352 
7353    x_return_stat := OKC_API.G_RET_STS_SUCCESS;
7354 EXCEPTION
7355    When  Others Then
7356           Null;
7357         OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN,SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
7358    x_return_stat := OKC_API.G_RET_STS_ERROR;
7359 END get_bcl_id;
7360 
7361 
7362 
7363 ------------------------------------------------------------------------
7364   -- FUNCTION Insert_all_subline
7365 ------------------------------------------------------------------------
7366 PROCEDURE insert_all_subline
7367 (
7368  P_CALLEDFROM        IN             NUMBER,
7369  X_RETURN_STAT      OUT     NOCOPY  VARCHAR2,
7370  P_COVERED_TBL       IN OUT NOCOPY  COVERED_TBL,
7371  P_CURRENCY_CODE     IN             VARCHAR2,
7372  P_DNZ_CHR_ID        IN             NUMBER,
7373  P_PRV               IN             NUMBER,
7374  P_MSG_COUNT         IN OUT NOCOPY  NUMBER,
7375  P_MSG_DATA          IN OUT NOCOPY  VARCHAR2
7376  )
7377  IS
7378 Cursor l_get_conversion_rule (p_chr_id IN NUMBER) is
7379  SELECT conversion_rate          con_rate,
7380         conversion_rate_date     con_date,
7381         conversion_type          con_type
7382    FROM okc_k_headers_b
7383    WHERE  id = p_chr_id;
7384 
7385 
7386 
7387 
7388 SUBTYPE l_bclv_tbl_type_in  is OKS_bcl_PVT.bclv_tbl_type;
7389    l_bclv_tbl_in   l_bclv_tbl_type_in;
7390    l_bclv_tbl_out   l_bclv_tbl_type_in;
7391 SUBTYPE l_bslv_tbl_type_in  is OKS_bsl_PVT.bslv_tbl_type;
7392    l_bslv_tbl_in   l_bslv_tbl_type_in;
7393    l_bslv_tbl_out   l_bslv_tbl_type_in;
7394 SUBTYPE l_bsdv_tbl_type_in  is OKS_bsd_PVT.bsdv_tbl_type;
7395    l_bsdv_tbl_in   l_bsdv_tbl_type_in;
7396    l_bsdv_tbl_out   l_bsdv_tbl_type_in;
7397 
7398 
7399 SUBTYPE l_bcl_pr_tbl_type  is OKS_BCL_PRINT_PREVIEW_PVT.bcl_pr_tbl_type;
7400    l_bcl_pr_tbl_in   l_bcl_pr_tbl_type;
7401    l_bcl_pr_tbl_out  l_bcl_pr_tbl_type;
7402 SUBTYPE l_bsl_pr_tbl_type  is OKS_BSL_PRINT_PREVIEW_PVT.bsl_pr_tbl_type;
7403    l_bsl_pr_tbl_in   l_bsl_pr_tbl_type;
7404    l_bsl_pr_tbl_out  l_bsl_pr_tbl_type;
7405 SUBTYPE l_bsd_pr_tbl_type  is OKS_BSD_PRINT_PREVIEW_PVT.bsd_pr_tbl_type;
7406    l_bsd_pr_tbl_in   l_bsd_pr_tbl_type;
7407    l_bsd_pr_tbl_out  l_bsd_pr_tbl_type;
7408 
7409 
7410 l_ret_stat    Varchar2(20) := OKC_API.G_RET_STS_SUCCESS;
7411 l_msg_cnt  Number;
7412 l_msg_data Varchar2(2000);
7413 
7414 l_amount NUMBER      := 0;
7415 l_amount_holder      NUMBER       := 0;
7416 l_cur_holder         VARCHAR2(10) ;
7417 
7418 round_amount NUMBER  := 0;
7419 l_cntr NUMBER;
7420 l_ret_stat_buf VARCHAR2(20) := 'S';
7421 l_index number;
7422 l_euro_conversion    VARCHAR2(10);
7423 l_euro_currency_code VARCHAR2(10);
7424 l_con_rate           NUMBER;
7425 l_con_date           DATE;
7426 l_con_type           VARCHAR2(20);
7427 cvn_not_found        EXCEPTION;
7428 
7429 
7430 BEGIN
7431   x_return_stat   := l_ret_stat;
7432   l_cntr := 0;
7433 
7434   l_euro_conversion := okc_currency_api.IS_EURO_CONVERSION_NEEDED
7435                                                    (p_currency_code);
7436 
7437 
7438   IF (l_euro_conversion = 'Y') THEN
7439     OPEN  l_get_conversion_rule(p_dnz_chr_id);
7440     FETCH l_get_conversion_rule into l_con_rate,l_con_date,l_con_type;
7441     IF (l_get_conversion_rule%NOTFOUND) THEN
7442       RAISE  cvn_not_found;
7443     END IF;
7444     CLOSE l_get_conversion_rule;
7445 
7446     l_euro_currency_code :=okc_currency_api.GET_EURO_CURRENCY_CODE
7447                                                    (p_currency_code);
7448   END IF;
7449 
7450   FOR l_cntr IN 1..p_covered_tbl.count
7451   LOOP
7452 
7453     round_amount := 0;
7454 
7455     ----for bug#3544124 commented if condition
7456     ----IF nvl(p_covered_tbl(l_cntr).AMOUNT,0) > 0  Then
7457 
7458       IF (l_euro_conversion = 'Y') Then
7459 
7460        l_cur_holder   := l_euro_currency_code;
7461        okc_currency_api.CONVERT_AMOUNT
7462            (P_FROM_CURRENCY         => p_currency_code,
7463             P_TO_CURRENCY           => l_euro_currency_code,
7464             P_CONVERSION_DATE       => l_con_date,
7465             P_CONVERSION_TYPE       => l_con_type,
7466             P_AMOUNT                => abs(p_covered_tbl(l_cntr).AMOUNT) ,
7467             X_CONVERSION_RATE       => l_con_rate   ,
7468             X_CONVERTED_AMOUNT      => l_amount_holder
7469             );
7470 
7471         ---Added as passing abs val to convert_amount
7472         IF nvl(p_covered_tbl(l_cntr).AMOUNT,0) < 0  Then
7473            l_amount_holder := -1 * l_amount_holder;
7474         END IF;
7475 
7476       ELSE
7477         l_amount_holder :=   p_covered_tbl(l_cntr).AMOUNT;
7478         l_cur_holder    :=   p_currency_code;
7479       END IF;
7480 
7481       /**
7482             * function added to round off the amount depending on the
7483             * precision set in fnd_currency  -- Hari 08/03/2001
7484        **/
7485 
7486       round_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(
7487                                                  l_amount_holder,
7488                                                  l_cur_holder );
7489 
7490     -----END IF;---- for amount > 0
7491 
7492     IF (P_PRV = 1 ) THEN
7493       l_bslv_tbl_in(1).CLE_ID          :=p_covered_tbl(l_cntr).id;
7494       l_bslv_tbl_in(1).bcl_id          :=p_covered_tbl(l_cntr).bcl_id;
7495       l_bslv_tbl_in(1).DATE_BILLED_FROM:=p_covered_tbl(l_cntr).DATE_BILLED_FROM;
7496       l_bslv_tbl_in(1).DATE_BILLED_TO  :=p_covered_tbl(l_cntr).DATE_BILLED_TO;
7497       l_bslv_tbl_in(1).AMOUNT          :=round_amount;
7498       l_bslv_tbl_in(1).AVERAGE         :=p_covered_tbl(l_cntr).AVERAGE;
7499 
7500       OKS_BILLSUBLINE_PUB.insert_Bill_subLine_Pub
7501         (
7502           P_API_VERSION                  =>  1.0,
7503           P_INIT_MSG_LIST                =>  'T',
7504           X_RETURN_STATUS                =>   l_ret_stat,
7505           X_MSG_COUNT                    =>   l_msg_cnt,
7506           X_MSG_DATA                     =>   l_msg_data,
7507           P_BSLV_TBL                     =>   l_bslv_tbl_in,
7508           X_BSLV_TBL                     =>   l_bslv_tbl_out
7509          );
7510 
7511 
7512       IF (l_ret_stat = 'S') THEN
7513         l_bsdv_tbl_in(1).bsl_id         := l_bslv_tbl_out(1).id;
7514         l_bsdv_tbl_in(1).amount         := round_amount;
7515         l_bsdv_tbl_in(1).unit_of_measure:= p_covered_tbl(l_cntr).UNIT_OF_MEASURE;
7516         l_bsdv_tbl_in(1).amcv_yn        := p_covered_tbl(l_cntr).AMCV_YN;
7517         l_bsdv_tbl_in(1).result         := p_covered_tbl(l_cntr).RESULT;
7518         l_bsdv_tbl_in(1).fixed          := p_covered_tbl(l_cntr).FIXED;
7519         l_bsdv_tbl_in(1).actual         := p_covered_tbl(l_cntr).ACTUAL;
7520         l_bsdv_tbl_in(1).default_default:=p_covered_tbl(l_cntr).DEFAULT_DEFAULT;
7521         l_bsdv_tbl_in(1).adjustment_level:= p_covered_tbl(l_cntr).ADJUSTMENT_LEVEL;
7522         l_bsdv_tbl_in(1).adjustment_minimum:= p_covered_tbl(l_cntr).ADJUSTMENT_MINIMUM;
7523         l_bsdv_tbl_in(1).bsl_id_averaged:=p_covered_tbl(l_cntr).BSL_ID_AVERAGED;
7524         l_bsdv_tbl_in(1).bsd_id         := p_covered_tbl(l_cntr).BSD_ID;
7525         l_bsdv_tbl_in(1).bsd_id_applied := p_covered_tbl(l_cntr).BSD_ID_APPLIED;
7526         l_bsdv_tbl_in(1).start_reading  := p_covered_tbl(l_cntr).start_reading;
7527         l_bsdv_tbl_in(1).end_reading    := p_covered_tbl(l_cntr).end_reading;
7528         l_bsdv_tbl_in(1).base_reading   := p_covered_tbl(l_cntr).base_reading;
7529         l_bsdv_tbl_in(1).estimated_quantity:= p_covered_tbl(l_cntr).estimated_quantity;
7530         l_bsdv_tbl_in(1).ccr_id         := p_covered_tbl(l_cntr).ccr_id;
7531         l_bsdv_tbl_in(1).cgr_id         := p_covered_tbl(l_cntr).cgr_id;
7532 
7533 
7534         OKS_BSL_det_PUB.insert_bsl_det_Pub
7535           (
7536             P_API_VERSION                  =>  1.0,
7537             P_INIT_MSG_LIST                =>  'T',
7538             X_RETURN_STATUS                =>   l_ret_stat,
7539             X_MSG_COUNT                    =>   l_msg_cnt,
7540             X_MSG_DATA                     =>   l_msg_data,
7541             P_BSDV_TBL                     =>   l_bsdv_tbl_in,
7542             X_BSDV_TBL                     =>   l_bsdv_tbl_out
7543            );
7544        END If;
7545 
7546        p_covered_tbl(l_cntr).x_stat := l_ret_stat;
7547 
7548        IF ( l_ret_stat <> 'S') THEN
7549          l_ret_stat_buf := l_ret_stat;
7550          get_message(l_msg_cnt  => l_msg_cnt,
7551                      l_msg_data => l_msg_data);
7552          p_msg_count := l_msg_cnt;
7553          p_msg_data  := l_msg_data;
7554        ELSE
7555          l_amount := nvl(l_amount,0) + nvl(round_amount,0);
7556        END If;
7557 
7558      ELSIF (P_PRV = 2 ) THEN
7559        l_bsl_pr_tbl_in(1).id                    := get_seq_id;
7560        l_bsl_pr_tbl_in(1).CLE_ID                := p_covered_tbl(l_cntr).id;
7561        l_bsl_pr_tbl_in(1).bcl_id                := p_covered_tbl(l_cntr).bcl_id;
7562        l_bsl_pr_tbl_in(1).DATE_BILLED_FROM      := p_covered_tbl(l_cntr).DATE_BILLED_FROM;
7563        l_bsl_pr_tbl_in(1).DATE_BILLED_TO        := p_covered_tbl(l_cntr).DATE_BILLED_TO;
7564        l_bsl_pr_tbl_in(1).AMOUNT                := nvl(round_amount,0);
7565        l_bsl_pr_tbl_in(1).AVERAGE               := nvl(p_covered_tbl(l_cntr).AVERAGE,0);
7566        l_bsl_pr_tbl_in(1).OBJECT_VERSION_NUMBER := 1;
7567        l_bsl_pr_tbl_in(1).CREATED_BY := FND_GLOBAL.user_id;
7568        l_bsl_pr_tbl_in(1).LAST_UPDATED_BY := FND_GLOBAL.user_id;
7569        l_bsl_pr_tbl_in(1).CREATION_DATE := sysdate;
7570        l_bsl_pr_tbl_in(1).LAST_UPDATE_DATE := sysdate;
7571        l_bsl_pr_tbl_in(1).LAST_UPDATE_LOGIN := NULL;
7572        l_bsl_pr_tbl_in(1).ATTRIBUTE_CATEGORY := NULL;
7573        l_bsl_pr_tbl_in(1).ATTRIBUTE1 := NULL;
7574        l_bsl_pr_tbl_in(1).ATTRIBUTE2 := NULL;
7575        l_bsl_pr_tbl_in(1).ATTRIBUTE3 := NULL;
7576        l_bsl_pr_tbl_in(1).ATTRIBUTE4 := NULL;
7577        l_bsl_pr_tbl_in(1).ATTRIBUTE5 := NULL;
7578        l_bsl_pr_tbl_in(1).ATTRIBUTE6 := NULL;
7579        l_bsl_pr_tbl_in(1).ATTRIBUTE7 := NULL;
7580        l_bsl_pr_tbl_in(1).ATTRIBUTE8 := NULL;
7581        l_bsl_pr_tbl_in(1).ATTRIBUTE9 := NULL;
7582        l_bsl_pr_tbl_in(1).ATTRIBUTE10 := NULL;
7583        l_bsl_pr_tbl_in(1).ATTRIBUTE11 := NULL;
7584        l_bsl_pr_tbl_in(1).ATTRIBUTE12 := NULL;
7585        l_bsl_pr_tbl_in(1).ATTRIBUTE13 := NULL;
7586        l_bsl_pr_tbl_in(1).ATTRIBUTE14 := NULL;
7587        l_bsl_pr_tbl_in(1).ATTRIBUTE15 := NULL;
7588        l_bsl_pr_tbl_in(1).SECURITY_GROUP_ID := NULL;
7589        l_bsl_pr_tbl_in(1).DATE_TO_INTERFACE := NULL;
7590 
7591        OKS_BILLSubLINE_PRV_PUB.insert_bsl_pr
7592         (
7593           P_API_VERSION                  =>  1.0,
7594           P_INIT_MSG_LIST                =>  'T',
7595           X_RETURN_STATUS                =>   l_ret_stat,
7596           X_MSG_COUNT                    =>   l_msg_cnt,
7597           X_MSG_DATA                     =>   l_msg_data,
7598           P_BSL_PR_TBL                   =>   l_bsl_pr_tbl_in,
7599           X_BSL_PR_TBL                   =>   l_bsl_pr_tbl_out
7600          );
7601 
7602          IF (l_ret_stat = 'S') THEN
7603            l_bsd_pr_tbl_in(1).id                    := get_seq_id;
7604            l_bsd_pr_tbl_in(1).bsl_id                := l_bsl_pr_tbl_out(1).id;
7605            l_bsd_pr_tbl_in(1).AMOUNT                := round_amount;
7606            l_bsd_pr_tbl_in(1).UNIT_OF_MEASURE       := p_covered_tbl(l_cntr).UNIT_OF_MEASURE;
7607            l_bsd_pr_tbl_in(1).AMCV_YN               := p_covered_tbl(l_cntr).AMCV_YN;
7608            l_bsd_pr_tbl_in(1).RESULT                := p_covered_tbl(l_cntr).RESULT;
7609            l_bsd_pr_tbl_in(1).FIXED                 := p_covered_tbl(l_cntr).FIXED;
7610            l_bsd_pr_tbl_in(1).ACTUAL                := p_covered_tbl(l_cntr).ACTUAL;
7611            l_bsd_pr_tbl_in(1).DEFAULT_DEFAULT       := p_covered_tbl(l_cntr).DEFAULT_DEFAULT;
7612            l_bsd_pr_tbl_in(1).ADJUSTMENT_LEVEL      := p_covered_tbl(l_cntr).ADJUSTMENT_LEVEL;
7613            l_bsd_pr_tbl_in(1).ADJUSTMENT_MINIMUM    := p_covered_tbl(l_cntr).ADJUSTMENT_MINIMUM;
7614            l_bsd_pr_tbl_in(1).BSL_ID_AVERAGED       := l_bsl_pr_tbl_out(1).id; --p_covered_tbl(l_cntr).BSL_ID_AVERAGED;
7615            l_bsd_pr_tbl_in(1).BSD_ID                := p_covered_tbl(l_cntr).BSD_ID;
7616            l_bsd_pr_tbl_in(1).BSD_ID_APPLIED        := p_covered_tbl(l_cntr).BSD_ID_APPLIED;
7617            l_bsd_pr_tbl_in(1).start_reading         := p_covered_tbl(l_cntr).start_reading;
7618            l_bsd_pr_tbl_in(1).end_reading           := p_covered_tbl(l_cntr).end_reading;
7619            l_bsd_pr_tbl_in(1).base_reading          := p_covered_tbl(l_cntr).base_reading;
7620            l_bsd_pr_tbl_in(1).ccr_id                := p_covered_tbl(l_cntr).ccr_id;
7621            l_bsd_pr_tbl_in(1).cgr_id                := p_covered_tbl(l_cntr).cgr_id;
7622            l_bsd_pr_tbl_in(1).OBJECT_VERSION_NUMBER := 1;
7623            l_bsd_pr_tbl_in(1).created_by := FND_GLOBAL.user_id;
7624            l_bsd_pr_tbl_in(1).last_updated_by := FND_GLOBAL.user_id;
7625            l_bsd_pr_tbl_in(1).last_update_date := sysdate;
7626            l_bsd_pr_tbl_in(1).creation_date := sysdate;
7627            l_bsd_pr_tbl_in(1).last_update_login := NULL;
7628            l_bsd_pr_tbl_in(1).attribute_category := NULL;
7629            l_bsd_pr_tbl_in(1).attribute1 := NULL;
7630            l_bsd_pr_tbl_in(1).attribute2 := NULL;
7631            l_bsd_pr_tbl_in(1).attribute3 := NULL;
7632            l_bsd_pr_tbl_in(1).attribute4 := NULL;
7633            l_bsd_pr_tbl_in(1).attribute5 := NULL;
7634            l_bsd_pr_tbl_in(1).attribute6 := NULL;
7635            l_bsd_pr_tbl_in(1).attribute7 := NULL;
7636            l_bsd_pr_tbl_in(1).attribute8 := NULL;
7637            l_bsd_pr_tbl_in(1).attribute9 := NULL;
7638            l_bsd_pr_tbl_in(1).attribute10 := NULL;
7639            l_bsd_pr_tbl_in(1).attribute11 := NULL;
7640            l_bsd_pr_tbl_in(1).attribute12 := NULL;
7641            l_bsd_pr_tbl_in(1).attribute13 := NULL;
7642            l_bsd_pr_tbl_in(1).attribute14 := NULL;
7643            l_bsd_pr_tbl_in(1).attribute15 := NULL;
7644            l_bsd_pr_tbl_in(1).security_group_id := NULL;
7645 
7646 
7647            OKS_BSD_PRV_PUB.insert_bsd_pr
7648            (
7649             P_API_VERSION                  =>  1.0,
7650             P_INIT_MSG_LIST                =>  'T',
7651             X_RETURN_STATUS                =>   l_ret_stat,
7652             X_MSG_COUNT                    =>   l_msg_cnt,
7653             X_MSG_DATA                     =>   l_msg_data,
7654             P_BSD_PR_TBL                   =>   l_bsd_pr_tbl_in,
7655             X_BSD_PR_TBL                   =>   l_bsd_pr_tbl_out
7656             );
7657          END IF;
7658          p_covered_tbl(l_cntr).x_stat := l_ret_stat;
7659 
7660          IF (l_ret_stat <> 'S') THEN
7661            l_ret_stat_buf := l_ret_stat;
7662            get_message(l_msg_cnt  => l_msg_cnt,
7663                        l_msg_data => l_msg_data);
7664            p_msg_count := l_msg_cnt;
7665            p_msg_data := l_msg_data;
7666          ELSE
7667            l_amount := nvl(l_amount,0) + nvl(round_amount,0);
7668          END If;
7669        END IF;
7670      END LOOP;
7671 
7672      --l_amount := nvl(l_amount,0) + nvl(p_covered_tbl(1).bcl_amount,0);
7673 
7674     --- to fix bug# 1716684
7675      IF(l_ret_stat_buf = 'S' and p_covered_tbl.count > 0) THEN
7676 
7677      ---for bug#3544124 removed condition from if statment   l_amount > 0 )
7678 
7679        Update_bcl
7680         (
7681           P_CALLEDFROM,
7682           l_ret_stat,
7683           p_covered_tbl(1).bcl_id,
7684           Null,
7685           Null,
7686           l_amount,
7687           l_cur_holder,
7688           P_PRV
7689         );
7690        IF (l_ret_stat <> 'S') THEN
7691          l_ret_stat_buf := l_ret_stat;
7692          get_message(l_msg_cnt  => l_msg_cnt,
7693                      l_msg_data => l_msg_data);
7694        END If;
7695      END If;
7696 
7697      p_covered_tbl.delete;
7698      x_return_stat := l_ret_stat_buf;
7699 
7700 EXCEPTION
7701    WHEN  cvn_not_found  THEN
7702          x_return_stat := 'E';
7703    WHEN  OTHERS THEN
7704           Null;
7705         OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN,SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
7706 
7707 END insert_all_subline;
7708 
7709 ---------------------------------------------------------------------------------------
7710            -- Get_bill_profile
7711 ------------------------------------------------------------------------------------
7712 PROCEDURE Get_Bill_profile
7713 ( p_dnz_chr_id   IN          NUMBER,
7714   x_bill_profile OUT NOCOPY  VARCHAR2
7715 ) IS
7716 
7717 Cursor l_bill_profile_id_csr(id_in IN Number) IS
7718        Select rule_information1 profile_id
7719        From   OKC_RULES_B  RL
7720              ,OKC_RULE_GROUPS_B RG
7721        Where   RG.dnz_chr_id = id_in
7722        And     RG.cle_id Is Null
7723        And     RG.id = RL.rgp_id
7724        And     rule_information_category = 'BPF';
7725 
7726 Cursor l_bill_profile_csr(id_in Number) Is
7727        Select Summarised_yn
7728        From   OKS_BILLING_PROFILES_V
7729        Where  id = id_in;
7730 l_profile_id NUMBER;
7731 
7732 BEGIN
7733      OPEN l_bill_profile_id_csr(p_dnz_chr_id);
7734      FETCH l_bill_profile_id_csr into l_profile_id;
7735      Close l_bill_profile_id_csr;
7736 
7737      OPEN l_bill_profile_csr(l_profile_id);
7738      FETCH l_bill_profile_csr into x_bill_profile;
7739      Close l_bill_profile_csr;
7740 
7741 EXCEPTION
7742    When  Others Then
7743           Null;
7744         OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN,SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
7745 
7746 END Get_Bill_profile;
7747 
7748 
7749 /*================================================================================
7750 Adjust_Neg_Price -- API to reconcile for the Service Billing and Usage Type 'NPR'
7751 after the Negotiated Price has been changed
7752 ==================================================================================*/
7753 
7754 Procedure Adjust_Negotiated_Price
7755 (
7756         p_calledfrom             IN             NUMBER,
7757         p_contract_id            IN             NUMBER,
7758         x_msg_count             OUT     NOCOPY  NUMBER,
7759         x_msg_data              OUT     NOCOPY  VARCHAR2,
7760         x_return_status         OUT     NOCOPY  VARCHAR2
7761 )
7762 
7763 IS
7764 
7765 Cursor  l_bill_line_csr
7766 IS
7767 Select  line.id,
7768                 line.dnz_chr_id,
7769                 line.cle_id,
7770                 line.lse_id,
7771                 line.start_date,
7772                 line.end_date,
7773                 line.price_negotiated,
7774                 line.date_terminated,
7775                 hdr.currency_code,
7776                 rules.rule_information2 Billing_freq,
7777                 rules.rule_information4 First_inv_dt,
7778                 rules.rule_information3  First_billto_dt,
7779                 rules.rule_information5  Primary_dur,
7780                 rules.rule_information7  Secondary_dur,
7781                 rules.rule_information6 Primary_period,
7782                 rules.rule_information8 Secondary_period
7783 
7784 From            OKC_K_LINES_B   line,
7785                 OKC_K_HEADERS_B Hdr,
7786                 OKC_RULES_B             rules,
7787                 OKC_RULE_GROUPS_B       rlgrp
7788 
7789 Where           line.lse_id in (1,12)
7790 And             line.dnz_chr_id = Hdr.id
7791 And             rlgrp.cle_id  = line.id
7792 And             rules.rgp_id = rlgrp.id
7793 And             rules.rule_information_category = 'SBG'
7794 And             (Hdr.scs_code = 'SERVICE' OR
7795             /* This code is added for warranty lines that are renewed */
7796             (Hdr.scs_code = 'WARRANTY' AND Hdr.chr_id_renewed IS not Null))
7797 And             Hdr.Template_yn = 'N'
7798 And             OKC_ASSENT_PUB.line_operation_allowed(line.id,'INVOICE')= 'T'
7799 And             Hdr.id = p_contract_id
7800 --And           Hdr.Contract_number = NVL(p_contract_number,hdr.contract_number)
7801 
7802 For Update;
7803 
7804 -- This cursor gives all the covered lines of service or usage lines
7805 CURSOR  l_subline_Csr(p_cle_id  Number)
7806 IS
7807 Select  id,
7808                 cle_id,
7809                 price_negotiated,
7810                 start_date,
7811                 end_date,
7812                 date_terminated
7813 From            OKC_K_LINES_B
7814 Where           cle_id = p_cle_id
7815 And             lse_id in (8,7,9,10,11,13,35);
7816 
7817 CURSOR  l_billed_csr (p_cle_id IN NUMBER)
7818 IS
7819 Select  *
7820 From            oks_bill_cont_lines
7821 where           cle_id = p_cle_id
7822 and             bill_action in ('RI', 'AD')
7823 order   by cle_id, date_billed_from;
7824 
7825 CURSOR  l_amount_csr(id_in  Number)
7826 IS
7827 Select  nvl(sum(NVL(amount,0)),0)  amount_billed
7828 From            oks_bill_sub_lines
7829 Where           cle_id = id_in;
7830 
7831 Cursor  l_inv_item_csr(p_cle_id Number)
7832 Is
7833 Select  item.Object1_id1,
7834         mtl.usage_item_flag,
7835         mtl.service_item_flag
7836 From    Okc_K_items Item,
7837         mtl_system_items_b  mtl
7838 Where   item.cle_id = p_cle_id
7839 And     mtl.inventory_item_id = item.object1_id1;
7840 
7841 Cursor  l_usage_csr(p_id Number)
7842 Is
7843 Select  Rule_information10 Usage_Type
7844 From            OKC_RULES_B RL,
7845                 OKC_RULE_GROUPS_B RG
7846 Where           RG.cle_id = p_id
7847 And             RG.id = RL.rgp_id
7848 And             rule_information_category = 'QRE';
7849 
7850 Cursor  qty_uom_csr(p_cle_id  Number)
7851 Is
7852 Select   okc.Number_of_items,
7853          tl.Unit_of_measure uom_code
7854 From   OKC_K_ITEMS_V OKC,
7855        mtl_units_of_measure_tl tl
7856 Where  okc.cle_id = P_cle_id
7857 And    tl.uom_code = OKC.uom_code
7858 AND    tl.language = USERENV('LANG');
7859 
7860 
7861 Cursor l_okl_contract_csr(p_chr_id IN NUMBER) is
7862   Select 1 from okc_k_rel_objs
7863   where  rty_code in ('OKLSRV','OKLUBB')
7864   and    jtot_object1_code = 'OKL_SERVICE'
7865   and    object1_id1 = to_char(p_chr_id);
7866 
7867 Cursor  get_bcl_id_cur(p_bcl_rec_cle_id IN NUMBER, p_bcl_rec_date_billed_to IN DATE)
7868 IS
7869 Select  id
7870 From            OKS_BILL_CONT_LINES
7871 Where           cle_id = p_bcl_rec_cle_id
7872 And             date_billed_to = p_bcl_rec_date_billed_to
7873 And             bill_action = 'AD';
7874 
7875 l_bill_rec                      l_bill_line_csr%rowtype;
7876 l_billed_rec            l_billed_csr%rowtype;
7877 l_amount_rec            l_amount_csr%rowtype;
7878 qty_uom_rec                     qty_uom_csr%rowtype;
7879 l_item_rec                      l_inv_item_csr%rowtype;
7880 l_cov_tbl                       OKS_BILL_REC_PUB.COVERED_TBL;
7881 
7882 
7883 l_date_billed_from      DATE;
7884 l_date_billed_to        DATE;
7885 l_date_next_invoice     DATE;
7886 l_stat                  NUMBER;
7887 l_error                 Varchar2(1) := 'F';
7888 l_status                Varchar2(1);
7889 l_usage_type            Varchar2(10);
7890 l_msg_data              Varchar2(2000);
7891 l_msg_count             NUMBER;
7892 l_msg_cnt               NUMBER;
7893 l_amount_billed         NUMBER := 0;
7894 l_inv_item_id           NUMBER;
7895 l_billed_cle_id         NUMBER;
7896 l_st_bcl_id                     NUMBER;
7897 l_billed_cnt            NUMBER := 0;
7898 l_ad_cnt                        NUMBER := 0;
7899 l_okl_flag                      NUMBER := 0;
7900 l_bcl_id                NUMBER;
7901 l_line_cnt                      NUMBER := 1;
7902 l_ptr                   NUMBER := 1;
7903 l_return_status         Varchar2(1) := OKC_API.G_RET_STS_SUCCESS;
7904 l_api_name                      CONSTANT VARCHAR2(30) := 'Adjust_Negotiated_Price';
7905 
7906 BEGIN
7907 
7908         x_return_status := 'S';
7909 
7910         FOR l_bill_rec in l_bill_line_csr
7911         LOOP
7912                 DBMS_TRANSACTION.SAVEPOINT('BEFORE_TRANSACTION');
7913 
7914                 If l_error = 'T' Then
7915                         Fetch l_bill_line_csr Into l_bill_rec;
7916                         Exit WHEN l_bill_line_csr%notFOUND;
7917                 End If;
7918 
7919 
7920                 l_okl_flag := 0; --- check for OKL contract
7921                 Open l_okl_contract_csr (l_bill_rec.dnz_chr_id);
7922                 Fetch l_okl_contract_csr into l_okl_flag;
7923                 Close l_okl_contract_csr;
7924 
7925                 l_billed_cnt :=1;
7926                 For l_billed_rec in l_billed_csr(l_bill_rec.id)
7927                 Loop
7928                         If l_billed_cnt = 1 Then
7929                                 l_billed_cle_id := l_billed_rec.cle_id;
7930                                 l_date_billed_from := l_billed_rec.date_billed_from;
7931                         End If;
7932 
7933                         l_date_billed_to := l_billed_rec.date_billed_to;
7934                         l_date_next_invoice :=  l_billed_rec.date_next_invoice;
7935                         l_billed_cnt := l_billed_cnt + 1;
7936 
7937                 End Loop;
7938 
7939                 Open l_usage_csr(l_bill_rec.id);
7940                 Fetch l_usage_csr into l_usage_type;
7941                 Close l_usage_csr;
7942 
7943                 Open l_inv_item_csr(l_bill_rec.id);
7944                 Fetch l_inv_item_csr into l_item_rec;
7945                 Close l_inv_item_csr;
7946 
7947                 l_ptr := 1;
7948                 l_ad_cnt := 0;
7949                 l_cov_tbl.delete;
7950 
7951                 For l_covlvl_rec in l_subline_csr(l_bill_rec.id)
7952                 Loop  -- subline loop
7953                         Exit WHEN l_subline_csr%notFOUND;
7954 
7955                         If l_date_billed_to < l_covlvl_rec.end_date Then
7956                                 goto end_of_process;
7957                         END If;
7958 
7959                         --Gets the calculation period for the covered level
7960                         Open qty_uom_csr(l_bill_rec.id);
7961                         Fetch qty_uom_csr into qty_uom_rec;
7962                         Close qty_uom_csr;
7963 
7964 
7965                         Select count(*) into l_ad_cnt
7966                         From OKS_BILL_CONT_LINES
7967                         Where cle_id = l_billed_cle_id
7968                         And     Bill_Action = 'AD';
7969 
7970 
7971                         If l_ad_cnt = 0 Then
7972 
7973                                 OKS_BILL_REC_PUB.Insert_bcl
7974                                 (
7975                                         p_calledfrom            => p_calledfrom,
7976                                         x_return_stat           => l_return_status,
7977                                         p_cle_id                => l_billed_cle_id,
7978                                         p_date_billed_from      => l_date_billed_from,
7979                                         p_date_billed_to        => l_date_billed_to,
7980                                         p_date_next_invoice     => l_date_next_invoice,
7981                                         p_bill_action           => 'AD',
7982                                         p_okl_flag             => l_okl_flag,
7983                                         p_prv                   => 1,
7984                                         p_msg_count            => l_msg_count,
7985                                         p_msg_data             => l_msg_data,
7986                                         x_bcl_id               => l_bcl_id
7987                                 );
7988 
7989                                 x_return_status := l_return_status;
7990 
7991                                 If (l_return_status <> OKC_API.G_RET_STS_SUCCESS) Then
7992                                       If (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) Then
7993                                           x_return_status := l_return_status;
7994                                           raise G_EXCEPTION_HALT_VALIDATION;
7995                                       ELSE
7996                                                 x_return_status := l_return_status;
7997                                                 FOR I IN 1..FND_MSG_PUB.Count_Msg
7998                                                 LOOP
7999                                                         FND_FILE.PUT_LINE( FND_FILE.LOG,(FND_MSG_PUB.Get(p_encoded =>FND_API.G_FALSE )));
8000                                                 END LOOP;
8001                                         END If;
8002                                 ELSIf l_return_status = OKC_API.G_RET_STS_SUCCESS Then
8003                                         Open get_bcl_id_cur(l_billed_cle_id, l_date_billed_to) ;
8004                                         Fetch get_bcl_id_cur into l_st_bcl_id;
8005                                         Close get_bcl_id_cur;
8006                                 END If;
8007 
8008 
8009                         END If; -- l_ad_cnt = 0
8010 
8011                         /* Starting from Regular Service Billing */
8012                         If      (l_item_rec.usage_item_flag = 'N')
8013                                 OR (l_item_rec.service_item_flag = 'Y')
8014                                 OR (l_item_rec.usage_item_flag = 'Y' AND l_usage_type = 'NPR')
8015                         Then
8016 
8017                                 Open l_amount_csr(l_covlvl_rec.id);
8018                                 Fetch l_amount_csr into l_amount_rec;
8019                                 Close l_amount_csr;
8020 
8021                                 /* The above cursor is for get the billed amount so far.
8022                                 To support , middle of the contract amount changes.
8023                                 select sum(amount) into l_amount_billed
8024                                 oks_bill_sub_lines_v
8025                                 cle_id = l_covlvl_rec.id;
8026                                 */
8027 
8028 
8029                                 l_cov_tbl(l_ptr).id                 := l_covlvl_rec.id;
8030                                 l_cov_tbl(l_ptr).bcl_id             := l_st_bcl_id;
8031                                 l_cov_tbl(l_ptr).date_billed_from   := l_covlvl_rec.start_date;
8032                                 l_cov_tbl(l_ptr).date_billed_to     := l_covlvl_rec.end_date;
8033                                 l_cov_tbl(l_ptr).average            := 0;
8034                                 l_cov_tbl(l_ptr).unit_of_measure    := qty_uom_rec.uom_code;
8035                                 l_cov_tbl(l_ptr).fixed              := 0 ;
8036                                 l_cov_tbl(l_ptr).actual             := Null;
8037                                 l_cov_tbl(l_ptr).default_default    := 0;
8038                                 l_cov_tbl(l_ptr).amcv_yn            := 'N';
8039                                 l_cov_tbl(l_ptr).adjustment_level   := 0 ;
8040                                 l_cov_tbl(l_ptr).result             := qty_uom_rec.number_of_items;
8041                                 l_cov_tbl(l_ptr).x_stat             := Null;
8042                                 l_cov_tbl(l_ptr).amount                 := 0;
8043 
8044                                 /* End of Adjust Regular Service and Usage NPR Billing*/
8045                                 If l_amount_rec.amount_billed <> l_covlvl_rec.price_negotiated Then
8046                                         l_amount_billed := (l_covlvl_rec.price_negotiated - nvl(l_amount_rec.amount_billed, 0));
8047                                         l_cov_tbl(l_ptr).amount := nvl(l_amount_billed,0);
8048                                 END If;
8049                         END If; -- l_item_rec.usage_item_flag = 'N'
8050 
8051                         l_ptr := l_ptr + 1;
8052 
8053                 End Loop; -- subline loop
8054 
8055                 OKS_BILL_REC_PUB.Insert_all_subline
8056                 (
8057                         p_calledfrom    => p_calledfrom,
8058                         x_return_stat   => l_return_status,
8059                         p_covered_tbl   => l_cov_tbl,
8060                         p_currency_code => l_bill_rec.currency_code,
8061                      p_dnz_chr_id    => l_bill_rec.dnz_chr_id,
8062                p_prv           => 1,
8063                p_msg_count     => l_msg_count   ,
8064                p_msg_data      => l_msg_data
8065                 );
8066 
8067                 FND_FILE.PUT_LINE( FND_FILE.LOG, 'after insert into sublines '||'        '||l_return_status );
8068 
8069                 If (l_return_status <> OKC_API.G_RET_STS_SUCCESS) Then
8070                         If (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) Then
8071                                 x_return_status := l_return_status;
8072                                 FND_FILE.PUT_LINE( FND_FILE.LOG, 'insert into table failed  Contract line id :'||'  '||l_bill_rec.id);
8073                                 l_error := 'T';
8074                                 DBMS_TRANSACTION.ROLLBACK_SAVEPOINT('BEFORE_TRANSACTION');
8075                                 raise G_EXCEPTION_HALT_VALIDATION;
8076                         ELSE
8077                                 x_return_status := l_return_status;
8078                                 FOR I IN 1..FND_MSG_PUB.Count_Msg
8079                                 LOOP
8080                                         FND_FILE.PUT_LINE( FND_FILE.LOG,(FND_MSG_PUB.Get(p_encoded =>FND_API.G_FALSE )));
8081                                 END LOOP;
8082 
8083                                 FND_FILE.PUT_LINE( FND_FILE.LOG, 'insert into table failed  Contract line id :'||'  '||l_bill_rec.id);
8084                                 l_error := 'T';
8085                                 DBMS_TRANSACTION.ROLLBACK_SAVEPOINT('BEFORE_TRANSACTION');
8086                         END If;
8087                 End If;
8088 
8089                 <<end_of_process>>
8090                 l_line_cnt := l_line_cnt + 1;
8091         END LOOP;
8092 
8093 EXCEPTION
8094 
8095 WHEN    OKC_API.G_EXCEPTION_ERROR       Then
8096         x_return_status := OKC_API.HANDLE_EXCEPTIONS
8097                                 (
8098                                         l_api_name,
8099                                         G_PKG_NAME,
8100                                         'OKC_API.G_RET_STS_ERROR',
8101                                         x_msg_count,
8102                                         x_msg_data,
8103                                         '_PUB'
8104                                 );
8105 
8106 WHEN    OKC_API.G_EXCEPTION_UNEXPECTED_ERROR    Then
8107         x_return_status := OKC_API.HANDLE_EXCEPTIONS
8108                                 (
8109                                         l_api_name,
8110                                         G_PKG_NAME,
8111                                         'OKC_API.G_RET_STS_UNEXP_ERROR',
8112                                         x_msg_count,
8113                                         x_msg_data,
8114                                         '_PUB');
8115 WHEN    OTHERS  Then
8116         x_return_status := OKC_API.HANDLE_EXCEPTIONS
8117                                 (
8118                                         l_api_name,
8119                                         G_PKG_NAME,
8120                                         'OTHERS',
8121                                         x_msg_count,
8122                                         x_msg_data,
8123                                         '_PUB'
8124                                 );
8125 
8126 END Adjust_Negotiated_Price;
8127 
8128 
8129 
8130 
8131 PROCEDURE pre_terminate_cp
8132    (
8133     P_CALLEDFROM                  IN          NUMBER DEFAULT NUll,
8134     P_CLE_ID                      IN          NUMBER,
8135     P_TERMINATION_DATE            IN          DATE,
8136     P_TERMINATE_REASON            IN          VARCHAR2,
8137     P_OVERRIDE_AMOUNT             IN          NUMBER,
8138     P_CON_TERMINATE_AMOUNT        IN          NUMBER,
8139     --P_EXISTING_CREDIT           IN          NUMBER,
8140     P_TERMINATION_AMOUNT          IN          NUMBER,
8141     P_SUPPRESS_CREDIT             IN          VARCHAR2,
8142     P_FULL_CREDIT                 IN          VARCHAR2,
8143     P_Term_Date_flag              IN          VARCHAR2,
8144     P_Term_Cancel_source          IN          VARCHAR2,
8145     X_RETURN_STATUS               OUT NOCOPY  VARCHAR2
8146   )IS
8147 
8148  Cursor l_rel_csr (p_line_id in Number) Is
8149    SELECT id FROM   OKC_K_REL_OBJS_V
8150    WHERE  cle_id = p_line_id ;
8151 
8152  Cursor check_term_cur(p_cle_id NUMBER) is
8153   SELECT   bsl.id,
8154               bcl.cle_id  top_line_id
8155   FROM oks_bill_cont_lines bcl,
8156        oks_bill_sub_lines  bsl
8157   WHERE bsl.bcl_id = bcl.id
8158   AND   bcl.bill_action  = 'TR'
8159   AND   bsl.cle_id = p_cle_id;
8160 
8161  Cursor check_lse_id(p_cle_id in NUMBER) is
8162   SELECT  line1.lse_id, line1.cle_id ,rline.termn_method,
8163           rline.usage_type, rline.usage_period,
8164           line1.end_date,line1.sts_code,
8165           hdr.id
8166    FROM     okc_k_lines_b line2,
8167             okc_k_lines_b line1,
8168             okc_k_headers_b  hdr,
8169             oks_k_lines_b rline
8170    WHERE line1.id = p_cle_id
8171    AND   line2.id = line1.cle_id
8172    AND   rline.cle_id = line2.id
8173    AND   hdr.id = line1.dnz_chr_id
8174    AND   hdr.sts_code <> 'QA_HOLD'
8175    AND   (exists ( SELECT 1 from okc_assents a
8176                     where Hdr.scs_code = a.scs_code
8177                       and line1.sts_code = a.sts_code
8178                       and line1.sts_code <> 'HOLD'
8179                       and a.opn_code = 'INVOICE'
8180                       and a.allowed_yn = 'Y')
8181            OR
8182           (line1.sts_code = 'HOLD'));
8183 
8184 
8185  Cursor sub_line_billed_cur (p_id IN NUMBER) is
8186    SELECT bsl.id FROM oks_bill_sub_lines bsl,
8187                       oks_bill_cont_lines bcl
8188    WHERE bsl.cle_id = p_id
8189    AND   bsl.bcl_id = bcl.id
8190    AND   bcl.bill_action = 'RI';
8191 
8192  Cursor bcl_amount (p_bcl_id in number) is
8193  Select amount from oks_bill_cont_lines
8194   where id = p_bcl_id;
8195 
8196  Cursor bsl_amount (p_bsl_id in number) is
8197  Select amount from oks_bill_sub_lines
8198   where id = p_bsl_id;
8199 
8200  --04-JAN-2006 mchoudha for bug#4738970
8201  --Added this cursor to fetch the ste_code
8202  CURSOR cur_status (p_code in varchar2) is
8203  SELECT ste_code
8204  FROM okc_statuses_b
8205  WHERE code = p_code;
8206 
8207 
8208  --Bug#5276678
8209  Cursor neg_bcl_amount_line (p_cle_id in number) is
8210  Select nvl(sum(decode(sign(trunc(bsl.date_billed_from) -   trunc(p_termination_date))  ,-1,
8211            ((trunc(bsl.date_billed_to) -  trunc(p_termination_date) + 1) * bsl.amount) /
8212             (trunc(bsl.date_billed_to) - trunc(bsl.date_billed_from) + 1) ,bsl.amount      )),0)
8213    -- nvl(sum(bsl.amount),0)
8214    from oks_bill_cont_lines bcl,
8215         oks_bill_sub_lines  bsl
8216    where bcl.cle_id = p_cle_id
8217    and   bsl.bcl_id = bcl.id
8218    and   bcl.bill_action <> 'TR'
8219  --and   trunc(bsl.date_billed_from) >= trunc(p_termination_date)
8220    and   trunc(bsl.date_billed_to) >= trunc(p_termination_date)		--bug#5276678
8221    and   bsl.amount < 0;
8222 
8223 Cursor pos_bcl_amount_line (p_cle_id in number) is
8224  Select nvl(sum(decode(sign(trunc(bsl.date_billed_from) -   trunc(p_termination_date))  ,-1,
8225            ((trunc(bsl.date_billed_to) -  trunc(p_termination_date) + 1) * bsl.amount) /
8226             (trunc(bsl.date_billed_to) - trunc(bsl.date_billed_from) + 1) ,bsl.amount      )),0)
8227    from oks_bill_cont_lines bcl,
8228         oks_bill_sub_lines  bsl
8229    where bcl.cle_id = p_cle_id
8230    and   bsl.bcl_id = bcl.id
8231    and   bcl.bill_action <> 'TR'
8232  --and   trunc(bsl.date_billed_from) >= trunc(p_termination_date)
8233    and   trunc(bsl.date_billed_to) >= trunc(p_termination_date)		--bug#5276678
8234    and   bsl.amount > 0;
8235 
8236 --End bug#5276678
8237 
8238 
8239 Cursor check_avg_csr(p_cle_id in number) is
8240  Select 1 from oks_bill_cont_lines
8241   where cle_id = p_cle_id
8242   and   bill_action = 'AV';
8243 
8244  l_bcl_credit_amount            NUMBER;
8245  l_bsl_credit_amount            NUMBER;
8246  l_bsl_id                       NUMBER;
8247  l_bcl_id                       NUMBER;
8248 
8249  l_clev_tbl_in                  OKC_CONTRACT_PUB.clev_tbl_type;
8250  l_clev_tbl_out                 OKC_CONTRACT_PUB.clev_tbl_type;
8251  l_api_version     CONSTANT     NUMBER     := 1.0;
8252  l_init_msg_list   CONSTANT     VARCHAR2(1):= 'F';
8253  l_msg_cnt                      NUMBER;
8254  l_msg_data                     VARCHAR2(2000);
8255  l_return_status                VARCHAR2(10);
8256 
8257  l_check_term_cur               CHECK_TERM_CUR%ROWTYPE;
8258  l_lse_id                       OKC_K_LINES_B.lse_id%TYPE;
8259  l_id                           NUMBER;
8260  l_amount                       NUMBER;
8261  l_term_date                    DATE;
8262  l_ctr                          BOOLEAN;
8263  l_billed                       BOOLEAN;
8264  l_number                       NUMBER;
8265  l_con_terminate_amount         NUMBER;
8266  l_termination_amount           NUMBER;
8267  l_top_line_id                  NUMBER;
8268  l_hdr_id                      NUMBER;
8269  l_term_method                  VARCHAR2(20);
8270  l_usage_type                   VARCHAR2(10);
8271  l_usage_period                 VARCHAR2(10);
8272  l_last_day_term        date;
8273  l_true_value_tbl               L_TRUE_VAL_TBL  ;
8274  --04-JAN-2006 mchoudha for bug#4738970
8275  --Added these variables
8276  l_ter_status_code              varchar2(30);
8277  l_status                       fnd_lookups.lookup_code%type;
8278  l_status_code                  varchar2(30);
8279  l_can_status_code              varchar2(30);
8280 
8281  --bug#5276678
8282 
8283  l_override_amount              NUMBER;
8284  l_con_termination_amount       NUMBER;
8285  l_neg_amount                   NUMBER;
8286 
8287  --End bug#5276678
8288 
8289 
8290 BEGIN
8291 
8292    /*Added for bug 11702843*/
8293   DBMS_TRANSACTION.SAVEPOINT('BEFORE_PRE_TERMINATE_CP');
8294    /*Added for bug 11702843*/
8295 
8296   X_return_status := 'S';
8297   l_ctr           := TRUE;
8298 
8299 
8300   IF nvl( P_Term_Date_flag,'N') = 'Y' Then
8301     l_CON_TERMINATE_AMOUNT       := 0;
8302     l_TERMINATION_AMOUNT         := 0;
8303   else
8304     l_CON_TERMINATE_AMOUNT       := P_CON_TERMINATE_AMOUNT;
8305     l_TERMINATION_AMOUNT         := P_TERMINATION_AMOUNT;
8306   End if;
8307 
8308   --bug#5276678
8309 
8310   l_override_amount := p_override_amount;
8311 
8312   --End bug#5276678
8313 
8314   OPEN check_term_cur(p_cle_id);
8315   FETCH check_term_cur into l_check_term_cur;
8316 
8317   IF check_term_cur%NOTFOUND THEN
8318     OPEN  check_lse_id(p_cle_id);
8319     FETCH check_lse_id into l_lse_id,l_top_line_id ,l_term_method,
8320                             l_usage_type,l_usage_period,l_last_day_term,l_status_code,
8321                             l_hdr_id;
8322     IF (check_lse_id%FOUND) THEN
8323       IF (l_lse_id = 25) THEN
8324         OPEN  l_rel_csr(p_cle_id);
8325         FETCH l_rel_csr into l_id;
8326         IF (l_rel_csr%FOUND) THEN
8327           l_ctr := FALSE;
8328         ELSE
8329           l_ctr := TRUE;
8330         END IF;
8331         CLOSE l_rel_csr;
8332       END IF;
8333 
8334 
8335       IF (l_ctr = FALSE) THEN
8336         Pre_terminate_extwar (
8337           P_CALLEDFROM             => p_calledfrom,
8338           P_LINE_ID                => p_cle_id,
8339           P_TERMINATION_DATE       => p_termination_date,
8340           P_SUPPRESS_CREDIT        => p_suppress_credit,
8341           P_TERMINATION_AMOUNT     => p_override_amount,
8342           P_CON_TERMINATION_AMOUNT => l_con_terminate_amount,
8343           P_COV_LINE               => 'Y',-- IS a coverage line
8344           P_FULL_CREDIT            => p_full_credit,
8345           --P_EXISTING_CREDIT        => p_existing_credit,
8346           X_AMOUNT                 => l_amount,
8347           X_RETURN_STATUS          => X_return_status
8348           );
8349 
8350         IF x_return_status <> 'S' THEN
8351           Raise G_EXCEPTION_HALT_VALIDATION;
8352         END IF;
8353     -- Added code for Bug # 3666203
8354        IF ((p_override_amount  is NOT NULL) and (g_credit_amount <> p_override_amount )) THEN
8355            OPEN  bsl_amount(g_bsl_id);
8356            FETCH bsl_amount into l_bsl_credit_amount;
8357            CLOSE bsl_amount ;
8358 
8359            OPEN  bcl_amount(g_bcl_id);
8360            FETCH bcl_amount into l_bcl_credit_amount;
8361            CLOSE bcl_amount ;
8362            If g_credit_amount < p_override_amount then
8363               l_bsl_credit_amount :=  l_bsl_credit_amount + ((-1)*(p_override_amount - g_credit_amount)) ;
8364               l_bsl_id := g_bsl_id;
8365 
8366               UPDATE oks_bill_sub_lines
8367                  SET amount = l_bsl_credit_amount
8368                WHERE id = l_bsl_id ;
8369 
8370               l_bcl_credit_amount :=  l_bcl_credit_amount + ((-1)*(p_override_amount - g_credit_amount)) ;
8371               l_bcl_id := g_bcl_id;
8372 
8373               UPDATE oks_bill_cont_lines
8374                  SET amount = l_bcl_credit_amount
8375                WHERE id = l_bcl_id ;
8376               g_credit_amount := 0;
8377            Elsif g_credit_amount > p_override_amount then
8378 
8379               l_bsl_credit_amount :=  l_bsl_credit_amount + (g_credit_amount - p_override_amount ) ;
8380               l_bsl_id := g_bsl_id;
8381 
8382               UPDATE oks_bill_sub_lines
8383                  SET amount = l_bsl_credit_amount
8384                WHERE id  = l_bsl_id ;
8385 
8386               l_bcl_credit_amount :=  l_bcl_credit_amount + (g_credit_amount - p_override_amount) ;
8387               l_bcl_id := g_bcl_id;
8388 
8389               UPDATE oks_bill_cont_lines
8390                  SET amount = l_bcl_credit_amount
8391                WHERE id    = l_bcl_id ;
8392               g_credit_amount := 0;
8393            End If;
8394        END IF;
8395     -- End of code for Bug # 3666203
8396        --04-JAN-2006 mchoudha for bug#4738970
8397        -- bug#5210918. Changed l_status to l_status_code
8398 
8399         IF l_status_code in ('ACTIVE','HOLD','SIGNED','ENTERED','EXPIRED')
8400             --bug#5135382: added 1 to l_last_day_term to take care of expired status
8401             AND (p_termination_date <= Nvl(l_last_day_term+1,p_termination_date))
8402             AND p_termination_date <= sysdate THEN
8403 
8404              IF l_status_code in ('ACTIVE','HOLD','SIGNED','EXPIRED') then
8405                 OKC_ASSENT_PUB.get_default_status( x_return_status => l_return_status,
8406                                                      p_status_type   => 'TERMINATED',
8407                                                      x_status_code   => l_ter_status_code );
8408 
8409                 IF l_return_status <> 'S' THEN
8410                    Raise G_EXCEPTION_HALT_VALIDATION;
8411                 END IF;
8412 
8413 
8414                 l_clev_tbl_in(1).sts_code := l_ter_status_code;
8415              ELSIF l_status_code = 'ENTERED' then
8416                 OKC_ASSENT_PUB.get_default_status( x_return_status => l_return_status,
8417                                                     p_status_type   => 'CANCELLED',
8418                                                     x_status_code   => l_can_status_code);
8419 
8420                 IF l_return_status <> 'S' THEN
8421                   Raise G_EXCEPTION_HALT_VALIDATION;
8422                 END IF;
8423                 l_clev_tbl_in(1).sts_code := l_can_status_code;
8424              END IF;
8425         END IF;
8426 
8427         --END added by mchoudha
8428 
8429         -- Code change for bug # 3393329 starts  --
8430         -- The following code was added as part of bug #3393329.Since
8431         -- the direct update was done before , the minor version number
8432         -- were not getting updated in contract. Before this code was added
8433         -- update to okc_k_lines_b was done directly.
8434         l_clev_tbl_in( 1 ).id              := p_cle_id;
8435         l_clev_tbl_in( 1 ).date_terminated := p_termination_date;
8436         l_clev_tbl_in( 1 ).trn_code        := p_terminate_reason;
8437         l_clev_tbl_in( 1 ).term_cancel_source  :=  P_Term_Cancel_source;
8438         okc_contract_pub.update_contract_line(
8439                p_api_version                   => l_api_version,
8440                p_init_msg_list                 => l_init_msg_list,
8441                p_restricted_update             => okc_api.g_true,
8442                x_return_status                 => l_return_status,
8443                x_msg_count                     => l_msg_cnt,
8444                x_msg_data                      => l_msg_data,
8445                p_clev_tbl                      => l_clev_tbl_in,
8446                x_clev_tbl                      => l_clev_tbl_out);
8447 
8448 /* Modified by sjanakir for Bug#6912454 */
8449 
8450         IF l_return_status <> 'S' THEN
8451            Raise G_EXCEPTION_HALT_VALIDATION;
8452         END IF;
8453         -- Code change for bug # 3393329 ends  --
8454 
8455        -- Added true value code for order originated contracts.----
8456        --xyz
8457        l_true_value_tbl(1).p_cp_line_id           := p_cle_id; --Sub line id
8458        l_true_value_tbl(1).p_top_line_id          := 0;        --Top line id
8459        l_true_value_tbl(1).p_hdr_id               := 0 ;       --Header id ;
8460        l_true_value_tbl(1).p_termination_date     := p_termination_date;
8461        l_true_value_tbl(1).p_terminate_reason     := p_terminate_reason;
8462        l_true_value_tbl(1).p_override_amount      := p_override_amount;
8463        l_true_value_tbl(1).p_con_terminate_amount := l_con_terminate_amount;
8464        l_true_value_tbl(1).p_termination_amount   := l_termination_amount;
8465        l_true_value_tbl(1).p_suppress_credit      := p_suppress_credit;
8466        l_true_valUe_tbl(1).p_full_credit          := p_full_credit ;
8467 
8468 /* Modified by sjanakir for Bug#6912454 */
8469        True_value(l_true_value_tbl , l_return_status );
8470        x_return_status := l_return_status;
8471        ------------------------------------------------------------
8472       ELSE
8473 
8474         l_billed := FALSE;
8475 
8476         IF (l_lse_id = 13) THEN
8477           OPEN sub_line_billed_cur(p_cle_id);
8478           FETCH sub_line_billed_cur into l_number;
8479           IF (sub_line_billed_cur%FOUND) THEN
8480             l_billed := TRUE;
8481           ELSE
8482             l_billed := FALSE;
8483           END IF;
8484           CLOSE sub_line_billed_cur;
8485 
8486            --bug#5276678
8487 	   -- bug#5276678 commenting IF condition
8488 
8489            IF ( p_override_amount IS NULL) THEN
8490 
8491 	     l_termination_amount := nvl(l_con_termination_amount,0);
8492 
8493 
8494 /*
8495             OPEN  neg_bcl_amount_line(l_top_line_id);
8496             FETCH neg_bcl_amount_line into l_neg_amount;
8497 	    CLOSE neg_bcl_amount_line;
8498 
8499            --IF (nvl(l_neg_amount,0) < 0) THEN
8500 
8501 	     OPEN  pos_bcl_amount_line(l_top_line_id);
8502              FETCH pos_bcl_amount_line into l_con_termination_amount;
8503              CLOSE pos_bcl_amount_line;
8504 
8505 	     l_override_amount := nvl(l_con_termination_amount,0) +  nvl(l_neg_amount,0) ;
8506 
8507 	   --END IF;
8508 */
8509 
8510            END IF;
8511 
8512          --End bug#5276678
8513 
8514         END IF;
8515 
8516         l_term_date := p_termination_date;
8517 
8518         Terminate_cp(
8519            P_CALLEDFROM           => p_calledfrom,
8520            P_TOP_LINE_ID          => l_top_line_id,
8521            P_CP_LINE_ID           => p_cle_id,
8522            P_TERMINATION_DATE     => l_term_date,--p_termination_date
8523            P_TERMINATE_REASON     => p_terminate_reason,
8524          --P_OVERRIDE_AMOUNT      => p_override_amount,
8525            P_OVERRIDE_AMOUNT      => l_override_amount, --bug#5276678
8526            P_CON_TERMINATE_AMOUNT => l_con_terminate_amount,
8527            --P_EXISTING_CREDIT      => p_existing_credit,
8528            P_TERMINATION_AMOUNT   => l_termination_amount ,
8529            P_SUPPRESS_CREDIT      => p_suppress_credit,
8530            P_FULL_CREDIT          => p_full_credit,
8531            P_TERM_METHOD          => l_term_method,
8532            P_USAGE_TYPE           => l_usage_type,
8533            P_USAGE_PERIOD         => l_usage_period,
8534            P_Term_Cancel_source   => P_Term_Cancel_source,
8535            X_RETURN_STATUS        => X_return_status);
8536 
8537         IF x_return_status <> 'S' Then
8538           Raise G_EXCEPTION_HALT_VALIDATION;
8539         END IF;
8540       END IF;
8541     END IF;
8542     CLOSE check_lse_id;
8543 
8544   END IF;  -- check_term_cur%NOTFOUND
8545   CLOSE check_term_cur;
8546 
8547    update oks_k_lines_b topline set topline.tax_amount =  ( select sum(tax_amount) from
8548                   oks_k_lines_b oksline, okc_k_lines_b okcline
8549                   where okcline.id = oksline.cle_id
8550                     and okcline.cle_id = l_top_line_id
8551                     and okcline.date_cancelled is null )
8552    where  topline.cle_id = l_top_line_id;
8553 
8554    update oks_k_headers_b hdr set hdr.tax_amount =  ( select sum(tax_amount) from
8555                   oks_k_lines_b oksline, okc_k_lines_b okcline
8556                   where okcline.id = oksline.cle_id
8557                   and okcline.dnz_chr_id = l_hdr_id
8558                   and okcline.date_cancelled is null
8559                   and lse_id in (1,12,19,46) )
8560    where  hdr.chr_id =  l_hdr_id;
8561 
8562 -- BUG#3312595 mchoudha : added exception to catch any
8563  --exceptions raised
8564 
8565 
8566 EXCEPTION
8567    WHEN G_EXCEPTION_HALT_VALIDATION THEN
8568    /*Added for bug 11702843*/
8569    x_return_status := OKC_API.G_RET_STS_ERROR ;
8570    DBMS_TRANSACTION.ROLLBACK_SAVEPOINT('BEFORE_PRE_TERMINATE_CP');
8571    /*Added for bug 11702843*/
8572 
8573 END pre_terminate_cp;
8574 
8575 
8576 PROCEDURE terminate_cp
8577   (
8578     P_CALLEDFROM                  IN          NUMBER DEFAULT Null,
8579     P_TOP_LINE_ID                 IN          NUMBER,
8580     P_CP_LINE_ID                  IN          NUMBER,
8581     P_TERMINATION_DATE            IN          DATE,
8582     P_TERMINATE_REASON            IN          VARCHAR2,
8583     P_OVERRIDE_AMOUNT             IN          NUMBER,
8584     P_CON_TERMINATE_AMOUNT        IN          NUMBER,
8585     --P_EXISTING_CREDIT             IN          NUMBER,
8586     P_TERMINATION_AMOUNT          IN          NUMBER ,
8587     P_SUPPRESS_CREDIT             IN          VARCHAR2,
8588     P_FULL_CREDIT                 IN          VARCHAR2,
8589     P_TERM_METHOD                 IN          VARCHAR2,
8590     P_USAGE_TYPE                  IN          VARCHAR2,
8591     P_USAGE_PERIOD                IN          VARCHAR2,
8592     P_Term_Cancel_source          IN          VARCHAR2,
8593     X_RETURN_STATUS               OUT NOCOPY  VARCHAR2
8594   )IS
8595 
8596 CURSOR max_date_billed_to_Cur (p_cle_id  IN  NUMBER)
8597 IS
8598  SELECT max(date_billed_to)
8599   FROM oks_bill_sub_lines
8600   WHERE cle_id = p_cle_id;
8601 
8602 Cursor l_line_csr(p_id in NUMBER) is
8603 SELECT start_date,end_date,sts_code,
8604        lse_id,
8605        dnz_chr_id
8606   FROM okc_k_lines_b
8607   WHERE id = p_id ;
8608 
8609 
8610 Cursor l_usage_csr(p_id in NUMBER) is
8611 SELECT usage_type
8612 FROM oks_k_lines_b
8613 WHERE cle_id = p_id ;
8614 
8615 -- BUG#3312595 mchoudha: Cursor to check for service request
8616 -- against the subline
8617 
8618 /***************************************************
8619 Cursor cur_subline_sr(p_id IN NUMBER) IS
8620 SELECT 'x'
8621 FROM CS_INCIDENTS_ALL_B  sr,
8622      okc_k_items cim
8623 where cim.cle_id = p_id
8624 and   sr.customer_product_id =  cim.object1_id1
8625 and   sr.status_flag = 'O';
8626 ***************************************************/
8627 
8628 Cursor cur_subline_sr(p_id IN NUMBER) IS
8629 SELECT 'x'
8630 FROM  okc_k_items cim,
8631       CS_INCIDENTS_ALL_B  sr
8632 where cim.cle_id = p_cp_line_id
8633 and   sr.contract_service_id = p_top_line_id
8634 and   sr.customer_product_id = to_number(cim.object1_id1)
8635 and   sr.status_flag = 'O' ;
8636 
8637 Cursor cur_lineno(p_id IN NUMBER) IS
8638 SELECT p.line_number||'.'||s.line_number,
8639        hdr.contract_number
8640 FROM   okc_k_lines_b p,
8641        okc_k_lines_b s,
8642        okc_k_headers_b hdr
8643 WHERE  s.id=p_id
8644 AND    p.id=s.cle_id
8645 AND    hdr.id=p.dnz_chr_id;
8646 
8647 
8648 -- End BUG#3312595 mchoudha
8649 
8650 
8651  Cursor bcl_amount (p_bcl_id in number) is
8652  Select amount from oks_bill_cont_lines
8653   where id = p_bcl_id;
8654 
8655  Cursor bsl_amount (p_bsl_id in number) is
8656  Select amount from oks_bill_sub_lines
8657   where id = p_bsl_id;
8658 
8659  l_bcl_credit_amount            NUMBER;
8660  l_bsl_credit_amount            NUMBER;
8661  l_bsl_id                       NUMBER;
8662  l_bcl_id                       NUMBER;
8663 
8664 l_return_status                       VARCHAR2(20);
8665 l_msg_cnt                             NUMBER;
8666 l_msg_data                            VARCHAR2(2000);
8667 l_amount                              NUMBER;
8668 l_api_version         CONSTANT        NUMBER     := 1.0;
8669 l_init_msg_list       CONSTANT        VARCHAR2(1):= 'F';
8670 l_termination_date                    DATE;
8671 l_termn_date                    DATE;
8672 l_lse_id                              NUMBER;
8673 l_max_date                            DATE;
8674 l_status_flag                         VARCHAR2(1);
8675 l_line_number                         VARCHAR2(500);
8676 l_contract_number                     VARCHAR2(120);
8677  --04-JAN-2006 mchoudha for bug#4738970
8678  --Added these variables
8679  l_ter_status_code              varchar2(30);
8680  l_status                       fnd_lookups.lookup_code%type;
8681  l_can_status_code              varchar2(30);
8682  l_last_day_term                        DATE;
8683  l_usage_type                          VARCHAR2(10);
8684 
8685 l_true_value_tbl                      L_TRUE_VAL_TBL  ;
8686 l_clev_tbl_in                  OKC_CONTRACT_PUB.clev_tbl_type;
8687 l_clev_tbl_out                 OKC_CONTRACT_PUB.clev_tbl_type;
8688 
8689 
8690    -------------------------------------------------------------------------
8691 -- Begin partial period computation logic
8692 -- Developer Mani Choudhary
8693 -- Date 30-MAY-2005
8694 -- local variables for partal periods
8695 -------------------------------------------------------------------------
8696 l_hdr_id              NUMBER;
8697 l_price_uom           OKS_K_HEADERS_B.PRICE_UOM%TYPE;
8698 l_period_start        OKS_K_HEADERS_B.PERIOD_START%TYPE;
8699 l_period_type         OKS_K_HEADERS_B.PERIOD_TYPE%TYPE;
8700 
8701 
8702 BEGIN
8703 
8704 -- BUG#3312595 mchoudha: Checking for service request
8705 -- against the subline
8706 
8707     OPEN l_line_csr(p_cp_line_id);
8708     FETCH l_line_csr into l_termn_date,l_last_day_term,l_status,l_lse_id,l_hdr_id ;
8709     CLOSE l_line_csr ;
8710 
8711     -- Open Service Request Check should not be done for
8712     -- P_CALLEDFROM = -1. -1 is used for IB Integration
8713     -- No Other Callers should pass -1 as P_CALLEDFROM
8714 
8715     IF NVL(P_CALLEDFROM,0) <> -1
8716     THEN
8717 
8718     --BUG 4477943 IF condition to check lse_id for products only
8719     IF l_lse_id IN(9,18,25)
8720     THEN
8721 
8722     OPEN cur_subline_sr(p_top_line_id);
8723     FETCH cur_subline_sr into l_status_flag;
8724     CLOSE cur_subline_sr;
8725 
8726     if(l_status_flag = 'x') THEN
8727 
8728       OPEN cur_lineno(p_cp_line_id);
8729       FETCH cur_lineno into l_line_number,l_contract_number;
8730       CLOSE cur_lineno;
8731 
8732 
8733       OKC_API.set_message(p_app_name      => g_app_name,
8734                           p_msg_name      => 'OKC_SR_PENDING',
8735                           p_token1        => 'NUMBER',
8736                           p_token1_value  => l_contract_number,
8737                           p_token2        => 'LINENO',
8738                           p_token2_value  =>  l_line_number);
8739 
8740       l_return_status := okc_api.g_ret_sts_error;
8741       raise  G_EXCEPTION_HALT_VALIDATION;
8742     end if;
8743 
8744     END IF; -- if lse_id in(9,18,25)
8745 
8746     END IF; -- For P_CALLEDFROM
8747 
8748 -- END BUG#3312595 mchoudha
8749 
8750     -------------------------------------------------------------------------
8751     -- Begin partial period computation logic
8752     -- Developer Mani Choudhary
8753     -- Date 30-MAY-2005
8754     -- Call oks_renew_util_pub.get_period_defaults to fetch period start and period type
8755     -------------------------------------------------------------------------
8756     IF l_hdr_id IS NOT NULL  and p_full_credit <> 'Y' THEN
8757       OKS_RENEW_UTIL_PUB.Get_Period_Defaults
8758                 (
8759                  p_hdr_id        =>l_hdr_id,
8760                  p_org_id        => NULL,
8761                  x_period_start  => l_period_start,
8762                  x_period_type   => l_period_type,
8763                  x_price_uom     => l_price_uom,
8764                  x_return_status => l_return_status);
8765 
8766       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
8767          fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_cp.ppc_defaults',
8768          'After calling OKS_RENEW_UTIL_PUB.Get_Period_Defaults l_period_start ' ||l_period_start||' ,l_period_type '||l_period_type);
8769       END IF;
8770 
8771 
8772       IF l_return_status <> 'S' THEN
8773         RAISE G_EXCEPTION_HALT_VALIDATION;
8774       END IF;
8775 
8776     END IF;
8777     --For usage , the period start should be SERVICE
8778     IF l_lse_id = 13 AND l_period_start is NOT NULL THEN
8779       l_period_start := 'SERVICE';
8780       l_usage_type := NULL;
8781       Open l_usage_csr(P_TOP_LINE_ID);
8782       Fetch l_usage_csr into l_usage_type;
8783       Close l_usage_csr;
8784     END IF;
8785     -------------------------------------------------------------------------
8786     -- End partial period computation logic
8787     -------------------------------------------------------------------------
8788     IF (p_full_credit = 'Y') THEN
8789       l_termination_date := l_termn_date ;
8790     ELSIF (p_full_credit = 'N')  THEN
8791       l_termination_date := p_termination_date;
8792     END IF;
8793 
8794     l_return_status := OKC_API.G_RET_STS_SUCCESS;
8795 
8796     IF (p_term_method = 'VOLUME') THEN
8797       pre_vol_based_terminate(
8798              P_CALLEDFROM               => 1 ,
8799              P_API_VERSION              => 1.0,
8800              P_INIT_MSG_LIST            => l_init_msg_list ,
8801              X_RETURN_STATUS            => l_return_status ,
8802              X_MSG_COUNT                => l_msg_cnt ,
8803              X_MSG_DATA                 => l_msg_data ,
8804              P_K_LINE_ID                => p_top_line_id ,
8805              P_CP_LINE_ID               => p_cp_line_id,
8806              P_TERMINATION_DATE         => l_termination_date ,
8807              P_TERMINATION_AMOUNT       => p_termination_amount ,
8808              P_CON_TERMINATION_AMOUNT   => p_con_terminate_amount ,
8809              --P_EXISTING_CREDIT          => p_existing_credit ,
8810              P_SUPPRESS_CREDIT          => p_suppress_credit ,
8811              P_USAGE_TYPE               => p_usage_type ,
8812              P_USAGE_PERIOD             => p_usage_period ,
8813              X_AMOUNT                   => l_amount ) ;
8814     ELSE
8815 
8816       OPEN  max_date_billed_to_Cur(p_cp_line_id);
8817       FETCH max_date_billed_to_Cur into l_max_date;
8818       CLOSE max_date_billed_to_Cur;
8819       IF l_period_start IS NOT NULL AND
8820          l_period_type IS NOT NULL
8821      THEN
8822         IF l_lse_id <> 13 OR nvl(l_usage_type,'XYZ') = 'NPR' THEN
8823           IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
8824              fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_cp.Service',
8825              'calling OKS_BILL_REC_PUB.Terminate_PPC with parameters  l_period_start '||l_period_start||', l_period_type '||l_period_type
8826              ||' P_override_amount ' ||p_override_amount||'p_con_terminate_amount '||p_con_terminate_amount||' ,P_suppress_credit'||P_suppress_credit);
8827           END IF;
8828           OKS_BILL_REC_PUB.Terminate_PPC
8829                (P_termination_date => l_termination_date,
8830                 p_end_date         => l_max_date,
8831                 P_top_line_id      => p_top_line_id,
8832                 P_cp_line_id       => p_cp_line_id,
8833                 P_period_start     => l_period_start,
8834                 P_period_type      => l_period_type,
8835                 P_suppress_credit  => p_suppress_credit,
8836                 P_override_amount  => p_override_amount,
8837                 p_con_terminate_amount => p_con_terminate_amount,
8838                 x_return_status    => l_return_status);
8839           IF l_return_status <> 'S' THEN
8840              RAISE G_EXCEPTION_HALT_VALIDATION;
8841           END IF;
8842         ELSE
8843           --This procedure will be called for usage amount based termination
8844           IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
8845              fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_cp.Usage',
8846              'calling OKS_BILL_REC_PUB.Create_trx_records with parameters  l_period_start '||l_period_start||', l_period_type '||l_period_type
8847              ||' P_override_amount ' ||p_override_amount||'p_con_terminate_amount '||p_con_terminate_amount||' ,P_suppress_credit'||P_suppress_credit);
8848           END IF;
8849           Create_trx_records(
8850                 p_called_from          => 1,
8851                 p_top_line_id          => p_top_line_id,
8852                 p_cov_line_id          => p_cp_line_id,
8853                 p_date_from            => l_termination_date,
8854                 p_date_to              => l_max_date,
8855                 p_amount               => 0,
8856                 p_override_amount      => p_override_amount,
8857                 p_suppress_credit      => p_suppress_credit,
8858                 p_con_terminate_amount => p_con_terminate_amount,
8859                 --p_existing_credit      => p_existing_credit,
8860                 p_bill_action          => 'TR',
8861                 p_period_start         => l_period_start,
8862                 p_period_type          => l_period_type,
8863                 x_return_status        => l_return_status,
8864                 P_termination_date => l_termination_date --skuchima 11691332
8865                 );
8866            IF l_return_status <> 'S' THEN
8867              RAISE G_EXCEPTION_HALT_VALIDATION;
8868            END IF;
8869         END IF;
8870       ELSE
8871 
8872         Create_trx_records(
8873            p_called_from          => 1,
8874            p_top_line_id          => p_top_line_id,
8875            p_cov_line_id          => p_cp_line_id,
8876            p_date_from            => l_termination_date,
8877            p_date_to              => l_max_date,
8878            p_amount               => 0,
8879            p_override_amount      => p_override_amount,
8880            p_suppress_credit      => p_suppress_credit,
8881            p_con_terminate_amount => p_con_terminate_amount,
8882            --p_existing_credit      => p_existing_credit,
8883            p_bill_action          => 'TR',
8884            x_return_status        => l_return_status,
8885            P_termination_date => l_termination_date --skuchima 11691332
8886              );
8887          IF l_return_status <> 'S' THEN
8888            RAISE G_EXCEPTION_HALT_VALIDATION;
8889          END IF;
8890       END IF;
8891     END IF;
8892 
8893 
8894 
8895 
8896     OKS_BILL_SCH.terminate_bill_sch(
8897           P_TOP_LINE_ID         => p_top_line_id,
8898           P_SUB_LINE_ID         => p_cp_line_id,
8899           P_TERM_DT             => l_termination_date,
8900           X_RETURN_STATUS       => l_return_status,
8901           X_MSG_COUNT           => l_msg_cnt,
8902           X_MSG_DATA            => l_msg_data);
8903 
8904     IF (l_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
8905       x_return_status := l_return_status;
8906       Raise G_EXCEPTION_HALT_VALIDATION;
8907     END IF;
8908 
8909 
8910        --04-JAN-2006 mchoudha for bug#4738970
8911 
8912         IF l_status in ('ACTIVE','HOLD','SIGNED','ENTERED','EXPIRED')
8913             --bug#5135382 added 1 to l_last_day_term to take care of expired status
8914             AND (p_termination_date <= Nvl(l_last_day_term+1,p_termination_date))
8915             AND p_termination_date <= sysdate THEN
8916 
8917              IF l_status in ('ACTIVE','HOLD','SIGNED','EXPIRED') then
8918                 OKC_ASSENT_PUB.get_default_status( x_return_status => l_return_status,
8919                                                      p_status_type   => 'TERMINATED',
8920                                                      x_status_code   => l_ter_status_code );
8921 
8922                 IF l_return_status <> 'S' THEN
8923                    Raise G_EXCEPTION_HALT_VALIDATION;
8924                 END IF;
8925 
8926 
8927                 l_clev_tbl_in(1).sts_code := l_ter_status_code;
8928              ELSIF l_status = 'ENTERED' then
8929                 OKC_ASSENT_PUB.get_default_status( x_return_status => l_return_status,
8930                                                     p_status_type   => 'CANCELLED',
8931                                                     x_status_code   => l_can_status_code);
8932 
8933                 IF l_return_status <> 'S' THEN
8934                   Raise G_EXCEPTION_HALT_VALIDATION;
8935                 END IF;
8936                 l_clev_tbl_in(1).sts_code := l_can_status_code;
8937              END IF;
8938         END IF;
8939 
8940         --END added by mchoudha
8941 
8942      -- Code change for bug # 3393329 starts  --
8943      -- The following code was added as part of bug #3393329.Since
8944      -- the direct update was done before , the minor version number
8945      -- were not getting updated in contract. Before this code was added
8946      -- update to okc_k_lines_b was done directly.
8947      l_clev_tbl_in( 1 ).id              := p_cp_line_id;
8948      l_clev_tbl_in( 1 ).date_terminated := p_termination_date;
8949      l_clev_tbl_in( 1 ).trn_code        := p_terminate_reason;
8950      l_clev_tbl_in( 1 ).term_cancel_source  :=  P_Term_Cancel_source;
8951      okc_contract_pub.update_contract_line(
8952                p_api_version                   => l_api_version,
8953                p_init_msg_list                 => l_init_msg_list,
8954                p_restricted_update             => okc_api.g_true,
8955                x_return_status                 => l_return_status,
8956                x_msg_count                     => l_msg_cnt,
8957                x_msg_data                      => l_msg_data,
8958                p_clev_tbl                      => l_clev_tbl_in,
8959                x_clev_tbl                      => l_clev_tbl_out);
8960 /* Modified by sjanakir for Bug#6912454 */
8961 
8962     IF l_return_status <> 'S' THEN
8963        Raise G_EXCEPTION_HALT_VALIDATION;
8964     END IF;
8965     -- Code change for bug # 3393329 ends  --
8966 
8967     -- Added code for Bug # 3666203
8968        IF ((p_override_amount  is NOT NULL) and (g_credit_amount <> p_override_amount )) THEN
8969            OPEN  bsl_amount(g_bsl_id);
8970            FETCH bsl_amount into l_bsl_credit_amount;
8971            CLOSE bsl_amount ;
8972 
8973            OPEN  bcl_amount(g_bcl_id);
8974            FETCH bcl_amount into l_bcl_credit_amount;
8975            CLOSE bcl_amount ;
8976            If g_credit_amount < p_override_amount then
8977               l_bsl_credit_amount :=  l_bsl_credit_amount + ((-1)*(p_override_amount - g_credit_amount)) ;
8978               l_bsl_id := g_bsl_id;
8979 
8980               UPDATE oks_bill_sub_lines
8981                  SET amount = l_bsl_credit_amount
8982                WHERE id = l_bsl_id ;
8983 
8984               l_bcl_credit_amount :=  l_bcl_credit_amount + ((-1)*(p_override_amount - g_credit_amount)) ;
8985               l_bcl_id := g_bcl_id;
8986 
8987               UPDATE oks_bill_cont_lines
8988                  SET amount = l_bcl_credit_amount
8989                WHERE id = l_bcl_id ;
8990               g_credit_amount := 0;
8991            Elsif g_credit_amount > p_override_amount then
8992 
8993               l_bsl_credit_amount :=  l_bsl_credit_amount + (g_credit_amount - p_override_amount ) ;
8994               l_bsl_id := g_bsl_id;
8995 
8996               UPDATE oks_bill_sub_lines
8997                  SET amount = l_bsl_credit_amount
8998                WHERE id  = l_bsl_id ;
8999 
9000               l_bcl_credit_amount :=  l_bcl_credit_amount + (g_credit_amount - p_override_amount) ;
9001               l_bcl_id := g_bcl_id;
9002 
9003               UPDATE oks_bill_cont_lines
9004                  SET amount = l_bcl_credit_amount
9005                WHERE id    = l_bcl_id ;
9006               g_credit_amount := 0;
9007            End If;
9008        END IF;
9009       g_bsl_id := 0;
9010       g_bcl_id := 0;
9011     -- End of code for Bug # 3666203
9012     --mchoudha 11510+ Usage Tax and Price Display
9013     --added the condition P_USAGE_TYPE = 'NPR'
9014     If l_lse_id <> 13 OR P_USAGE_TYPE = 'NPR' then
9015        l_true_value_tbl(1).p_cp_line_id       := p_cp_line_id;
9016        l_true_value_tbl(1).p_top_line_id      := 0;   --Top line id
9017        l_true_value_tbl(1).p_hdr_id           := 0 ;  --Header id ;
9018        l_true_value_tbl(1).p_termination_date := p_termination_date;
9019        l_true_value_tbl(1).p_terminate_reason := p_terminate_reason;
9020        l_true_value_tbl(1).p_override_amount  := p_override_amount;
9021        l_true_value_tbl(1).p_con_terminate_amount := p_con_terminate_amount;
9022        l_true_value_tbl(1).p_termination_amount   := p_termination_amount;
9023        l_true_value_tbl(1).p_suppress_credit      := p_suppress_credit;
9024        l_true_valUe_tbl(1).p_full_credit          := p_full_credit ;
9025        /* Modified by sjanakir for Bug#6912454 */
9026        True_value(l_true_value_tbl , l_return_status );
9027     End IF;
9028 
9029      x_return_status := l_return_status;
9030 
9031   EXCEPTION
9032    WHEN  G_EXCEPTION_HALT_VALIDATION THEN
9033           x_return_status := l_return_status;
9034    WHEN  OTHERS THEN
9035           OKC_API.set_message(G_APP_NAME, G_UNEXPECTED_ERROR,G_SQLCODE_TOKEN, SQLCODE, G_SQLERRM_TOKEN,SQLERRM);
9036   END terminate_cp;
9037 
9038 Procedure get_termination_details ( p_level IN VARCHAR2 ,
9039                                     p_id IN NUMBER ,
9040                                     x_unbilled OUT NOCOPY NUMBER ,
9041                                     x_credited OUT NOCOPY NUMBER ,
9042                                     x_suppressed OUT NOCOPY NUMBER ,
9043                                     x_overridden OUT NOCOPY NUMBER ,
9044                                     x_billed OUT NOCOPY NUMBER ,
9045                                     x_return_status OUT NOCOPY VARCHAR2 ) is
9046 
9047 --mchoudha 11510+ Usage Tax and Price Display
9048 --added NOT Exists clause and lse_id 13
9049 CURSOR l_HDR_CSR IS
9050 SELECT nvl(SUM(ubt_amount),0) unbilled
9051       ,nvl(SUM(credit_amount),0) credited
9052       ,nvl(SUM(suppressed_credit),0) suppressed
9053       ,nvl(SUM(override_amount),0) overridden
9054   FROM OKC_K_LINES_B OKCL
9055       ,OKS_K_LINES_B OKSL
9056  WHERE OKCL.dnz_chr_id = p_id
9057    AND OKCL.lse_id in ( 7,8,9,10,11,13,25,35,46)
9058    AND OKCL.id = OKSL.cle_id
9059    AND NOT exists (select 'x' from okc_k_lines_b cle,
9060                                    oks_k_lines_b kln
9061                    where cle.id = OKCL.cle_id
9062                    and   kln.cle_id = cle.id
9063                    and   kln.usage_type in ('VRT','QTY','FRT'));
9064 
9065 --mchoudha 11510+ Usage Tax and Price Display
9066 --added NOT Exists clause and lse_id 13
9067 CURSOR L_HDR_BILLED_CSR IS
9068 SELECT nvl(SUM(BSL.amount),0) billed
9069 FROM OKC_K_LINES_B OKCL
9070     ,OKS_BILL_CONT_LINES BCL
9071     ,OKS_BILL_SUB_LINES BSL
9072 WHERE OKCL.dnz_chr_id = p_id
9073   AND OKCL.lse_id in ( 7,8,9,10,11,13,25,35,46)
9074   AND OKCL.id = BSL.cle_id
9075   AND BSL.bcl_id = BCL.id
9076   AND BCL.bill_action = 'RI'
9077   AND NOT exists (select 'x' from okc_k_lines_b cle,
9078                                   oks_k_lines_b kln
9079                    where cle.id = OKCL.cle_id
9080                    and   kln.cle_id = cle.id
9081                    and   kln.usage_type in ('VRT','QTY','FRT'));
9082 
9083 
9084 
9085 --mchoudha 11510+ Usage Tax and Price Display
9086 --added NOT Exists clause and lse_id 13
9087 CURSOR l_TOP_LINE_CSR IS
9088 SELECT nvl(SUM(ubt_amount),0) unbilled
9089       ,nvl(SUM(credit_amount),0) credited
9090       ,nvl(SUM(suppressed_credit),0) suppressed
9091       ,nvl(SUM(override_amount),0) overridden
9092   FROM OKC_K_LINES_B OKCL
9093       ,OKS_K_LINES_B OKSL
9094  WHERE (    OKCL.cle_id = p_id
9095         OR (OKCL.id = p_id and OKCL.lse_id = 46 ))
9096    AND OKCL.lse_id in ( 7,8,9,10,11,13,25,35,46)
9097    AND OKCL.id = OKSL.cle_id
9098    AND NOT exists (select 'x' from okc_k_lines_b cle,
9099                                   oks_k_lines_b kln
9100                    where cle.id = OKCL.cle_id
9101                    and   kln.cle_id = cle.id
9102                    and   kln.usage_type in ('VRT','QTY','FRT'));
9103 
9104 
9105 --mchoudha 11510+ Usage Tax and Price Display
9106 --added NOT Exists clause and lse_id 13
9107 CURSOR l_TOP_LINE_BILLED_CSR IS
9108 SELECT nvl(SUM(BSL.amount),0) billed
9109 FROM OKC_K_LINES_B OKCL
9110     ,OKS_BILL_CONT_LINES BCL
9111     ,OKS_BILL_SUB_LINES BSL
9112 WHERE ( OKCL.cle_id = p_id
9113       OR ( OKCL.ID = p_id and OKCL.lse_id = 46))
9114   AND OKCL.lse_id in ( 7,8,9,10,11,13,25,35,46)
9115   AND OKCL.id = BSL.cle_id
9116   AND BSL.bcl_id = BCL.id
9117   AND BCL.bill_action = 'RI'
9118   AND NOT exists (select 'x' from okc_k_lines_b cle,
9119                                   oks_k_lines_b kln
9120                    where cle.id = OKCL.cle_id
9121                    and   kln.cle_id = cle.id
9122                    and   kln.usage_type in ('VRT','QTY','FRT'));
9123 
9124 
9125 
9126 CURSOR l_SUB_LINE_CSR IS
9127 SELECT nvl(SUM(ubt_amount),0) unbilled
9128       ,nvl(SUM(credit_amount),0) credited
9129       ,nvl(SUM(suppressed_credit),0) suppressed
9130       ,nvl(SUM(override_amount),0) overridden
9131   FROM OKS_K_LINES_B OKSL
9132  WHERE OKSL.cle_id = p_id;
9133 
9134 CURSOR l_SUB_LINE_BILLED_CSR IS
9135 SELECT nvl(SUM(BSL.amount),0) billed
9136 FROM OKS_BILL_CONT_LINES BCL
9137     ,OKS_BILL_SUB_LINES BSL
9138 WHERE BSL.cle_id = p_id
9139   AND BSL.bcl_id = BCL.id
9140   AND BCL.bill_action = 'RI';
9141 l_unbilled NUMBER ;
9142 l_credited NUMBER ;
9143 l_suppressed NUMBER ;
9144 l_overridden NUMBER ;
9145 l_billed NUMBER ;
9146 l_return_status varchar2(10);
9147 
9148 BEGIN
9149 x_return_status := 'S';
9150  If p_level = 'H' then
9151     Open l_HDR_CSR;
9152     Fetch l_HDR_CSR into l_unbilled , l_credited, l_suppressed,l_overridden ;
9153     Close l_HDR_CSR;
9154     Open L_HDR_BILLED_CSR;
9155     Fetch L_HDR_BILLED_CSR into l_billed ;
9156     Close L_HDR_BILLED_CSR;
9157  Elsif p_level = 'T' then
9158     Open l_TOP_LINE_CSR;
9159     Fetch l_TOP_LINE_CSR into l_unbilled , l_credited, l_suppressed,l_overridden ;
9160     Close l_TOP_LINE_CSR ;
9161     Open l_TOP_LINE_BILLED_CSR;
9162     Fetch l_TOP_LINE_BILLED_CSR into l_billed ;
9163     Close l_TOP_LINE_BILLED_CSR;
9164  Elsif p_level = 'S' then
9165     Open l_SUB_LINE_CSR;
9166     Fetch l_SUB_LINE_CSR into l_unbilled , l_credited, l_suppressed,l_overridden ;
9167     Close l_SUB_LINE_CSR;
9168     Open l_SUB_LINE_BILLED_CSR;
9169     Fetch l_SUB_LINE_BILLED_CSR into l_billed ;
9170     Close l_SUB_LINE_BILLED_CSR;
9171  End If;
9172  x_unbilled   := l_unbilled ;
9173  x_credited   := l_credited ;
9174  x_suppressed := l_suppressed;
9175  x_overridden := l_overridden;
9176  x_billed     := l_billed ;
9177 END get_termination_details ;
9178 
9179 PROCEDURE TRUE_VALUE ( p_true_value_tbl   IN            L_TRUE_VAL_TBL ,
9180                        x_return_status   OUT  NOCOPY    VARCHAR2 )Is
9181 
9182  CURSOR l_hdr_csr (p_top_line_id in NUMBER ) is
9183  SELECT id top_line_id ,
9184         dnz_chr_id hdr_id,
9185         lse_id lse_id,
9186         price_negotiated price_negotiated
9187    FROM okc_k_lines_b okcl
9188   WHERE dnz_chr_id = p_top_line_id
9189     AND okcl.lse_id in (1,12,14,19,46)
9190     AND okcl.date_cancelled is null --LLC BUG FIX 4742661
9191     AND okcl.date_terminated is null ;
9192 
9193 --mchoudha 11510+ Usage Tax and Price Display
9194 --added for usage lines lse_id 13
9195  CURSOR l_top_line_csr (p_top_line_id in NUMBER ) is
9196  SELECT id          sub_line_id ,
9197         dnz_chr_id  hdr_id,
9198         start_date  start_date
9199    FROM okc_k_lines_b okcl
9200   WHERE cle_id = p_top_line_id
9201     AND okcl.lse_id in (7,8,9,10,11,13,35,25)
9202     AND okcl.date_cancelled is NULL --LLC BUG FIX 4742661
9203     AND okcl.date_terminated is null ;
9204 
9205  CURSOR l_sub_line_csr (p_sub_line_id in NUMBER ) is
9206  SELECT Price_negotiated orginal_amount ,
9207         cle_id top_line_id ,
9208         dnz_chr_id hdr_id
9209    FROM okc_k_lines_b okcl
9210   WHERE id = p_sub_line_id ;
9211 
9212  CURSOR l_bill_amount_csr(p_sub_line_id in NUMBER ) is
9213  SELECT sum(nvl(amount,0)) bill_amount
9214    FROM oks_level_elements
9215   WHERE cle_id = p_sub_line_id ;
9216 
9217  CURSOR l_credit_csr(p_sub_line_id in NUMBER ) is
9218  SELECT sum(bsl.amount) suppressed_credit
9219    FROM oks_bill_sub_lines bsl,
9220         oks_bill_cont_lines bcl
9221   WHERE bsl.cle_id = p_sub_line_id
9222     AND bcl.id = bsl.bcl_id
9223     AND btn_id is null
9224     AND bcl.bill_action = 'TR';
9225 
9226  CURSOR l_suppressed_credit_csr(p_sub_line_id in NUMBER )  is
9227  SELECT sum(bsl.amount) suppressed_credit
9228    FROM oks_bill_sub_lines bsl,
9229         oks_bill_cont_lines bcl
9230   WHERE bsl.cle_id = p_sub_line_id
9231     AND bcl.id = bsl.bcl_id
9232     AND btn_id = -44
9233     AND bcl.bill_action = 'TR';
9234 
9235  CURSOR l_lse_id_csr (p_id IN number )is
9236  SELECT lse_id ,
9237         price_negotiated,
9238         dnz_chr_id
9239    FROM OKC_K_LINES_B OKCL
9240   WHERE OKCL.id = p_id ;
9241 
9242  CURSOR L_HDR_CURR_CODE (p_id in number ) is
9243  SELECT currency_code
9244    FROM okc_k_headers_b
9245   WHERE id = p_id;
9246 
9247  CURSOR l_line_curr_code (p_id in number ) is
9248  SELECT hdr.currency_code ,hdr.id
9249    FROM okc_k_lines_b lines,
9250         okc_k_headers_b hdr
9251   WHERE lines.id = p_id
9252     AND hdr.id = lines.dnz_chr_id;
9253 
9254  CURSOR l_check_for_full_credit(p_top_line_id in NUMBER ) is
9255  SELECT count(oks.full_credit)
9256    FROM OKC_K_LINES_B OKC,
9257         OKS_K_LINES_B OKS
9258   WHERE OKC.cle_ID = p_top_line_id
9259     AND OKC.id = OKS.CLE_id
9260     and OKC.lse_id in (7,8,9,10,11,13,18,25,35)
9261     and OKC.date_cancelled is NULL --LLC BUG FIX 4742661
9262     and OKC.date_terminated is not null
9263     and ( oks.full_credit is null
9264           OR oks.full_credit = 'N' );
9265 
9266 --mchoudha 11510+ Usage Tax and Price Display
9267 --added the following two cursors to retrieve the usage type
9268 Cursor l_usage_type_csr(p_sub_line_id in number) Is
9269 Select kln.usage_type
9270 from   okc_k_lines_b cle1,
9271        okc_k_lines_b cle2,
9272        oks_k_lines_b kln
9273 where  cle1.id = p_sub_line_id
9274 and    cle2.id = cle1.cle_id
9275 and    kln.cle_id = cle2.id;
9276 
9277 Cursor l_usage_type_csr1(p_top_line_id in number) Is
9278 Select usage_type
9279 from   oks_k_lines_b
9280 where  cle_id = p_top_line_id;
9281 
9282 Cursor l_ubt_csr(p_id in number) IS
9283 SELECT ubt_amount
9284 FROM   OKS_K_LINES_B
9285 WHERE  cle_id = p_id;
9286 
9287 /*Added for bug:10285124*/
9288 Cursor sub_line_tax_check(p_sub_line_id in number ) is
9289 Select tax_amount
9290 from oks_k_lines_b
9291 where cle_id = p_sub_line_id;
9292 
9293 
9294 
9295  l_top_line_rec           l_top_line_csr%ROWTYPE;
9296  l_hdr_rec                l_hdr_csr%ROWTYPE;
9297  l_lse_id_rec             l_lse_id_csr%ROWTYPE;
9298  l_original_amt           NUMBER ;
9299  l_bill_amt               NUMBER ;
9300  l_credit                 NUMBER ;
9301  l_suppressed_credit      NUMBER ;
9302  l_overridden             NUMBER ;
9303  l_true_value             NUMBER ;
9304  l_terminated_amt         NUMBER ;
9305  l_ubt_amount             NUMBER ;
9306  l_top_line_id            NUMBER ;
9307  l_hdr_id                 NUMBER ;
9308  l_termn_level            VARCHAR2(10);
9309  l_return_status          VARCHAR2(1) := 'S';
9310  l_amount                 NUMBER := 0;
9311  l_tot_bill_amt           NUMBER ;
9312  l_tot_ubt_amount         NUMBER :=0 ;
9313  l_tot_credit             NUMBER :=0 ;
9314  l_tot_suppressed_credit  NUMBER :=0 ;
9315  l_tot_overridden         NUMBER :=0 ;
9316  l_tot_true_value         NUMBER ;
9317  l_lse_id                 NUMBER :=0 ;
9318  l_price_negotiated       NUMBER :=0 ;
9319  G_RAIL_REC               OKS_TAX_UTIL_PVT.ra_rec_type;
9320  x_msg_count              NUMBER;
9321  x_msg_data               VARCHAR2(2000);
9322  l_currency_code          Varchar2(15);
9323  l_full_yn                Varchar2(3);
9324  l_full_count             NUMBER;   -- Changed datatype from VARCHAR2(3) to NUMBER for Bug#12938937
9325  l_tax_value              NUMBER;
9326  l_tot_tax_value          NUMBER := 0;
9327  l_process                BOOLEAN;
9328  --mchoudha 11510+ Usage Tax and Price Display
9329  l_usage_type             VARCHAR2(3);
9330  l_header_id              NUMBER;
9331  -------------------------------------------------------------------------
9332 -- Begin partial period computation logic
9333 -- Developer Mani Choudhary
9334 -- Date 04-MAY-2005
9335 -------------------------------------------------------------------------
9336 l_price_uom         OKS_K_HEADERS_B.PRICE_UOM%TYPE;
9337 l_period_start      OKS_K_HEADERS_B.PERIOD_START%TYPE;
9338 l_period_type       OKS_K_HEADERS_B.PERIOD_TYPE%TYPE;
9339 -------------------------------------------------------------------------
9340 /*Added for bug:10285124*/
9341  l_flag                number;
9342 BEGIN
9343 
9344  x_return_status := 'S';
9345 --mchoudha 11510+ Usage Tax and Price Display
9346  l_usage_type    := NULL;
9347 
9348  IF p_true_value_tbl(1).p_cp_line_id  > 0 and
9349     p_true_value_tbl(1).p_top_line_id = 0 and
9350     p_true_value_tbl(1).p_hdr_id      = 0 then
9351       l_termn_level := 'CP' ;
9352       open  l_line_curr_code(p_true_value_tbl(1).p_cp_line_id);
9353       fetch l_line_curr_code into l_currency_code,l_header_id;
9354       close l_line_curr_code ;
9355  ELSIF p_true_value_tbl(1).p_cp_line_id = 0 and
9356     p_true_value_tbl(1).p_top_line_id   > 0 and
9357     p_true_value_tbl(1).p_hdr_id        = 0 then
9358       l_termn_level := 'TL' ;
9359       open  l_line_curr_code(p_true_value_tbl(1).p_top_line_id);
9360       fetch l_line_curr_code into l_currency_code,l_header_id;
9361       close l_line_curr_code ;
9362  ELSIF p_true_value_tbl(1).p_cp_line_id = 0 and
9363     p_true_value_tbl(1).p_top_line_id   > 0 and
9364     p_true_value_tbl(1).p_hdr_id        > 0 then
9365       l_termn_level := 'TL' ;
9366       open  l_hdr_curr_code(p_true_value_tbl(1).p_hdr_id);
9367       fetch l_hdr_curr_code into l_currency_code;
9368       close l_hdr_curr_code ;
9369       l_header_id:= p_true_value_tbl(1).p_hdr_id;
9370  ELSIF p_true_value_tbl(1).p_cp_line_id = 0 and
9371     p_true_value_tbl(1).p_top_line_id   = 0 and
9372     p_true_value_tbl(1).p_hdr_id        > 0 then
9373       l_termn_level := 'HD' ;
9374       open  l_hdr_curr_code(p_true_value_tbl(1).p_hdr_id);
9375       fetch l_hdr_curr_code into l_currency_code;
9376       close l_hdr_curr_code ;
9377       l_header_id:= p_true_value_tbl(1).p_hdr_id;
9378  END IF;
9379 
9380  IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
9381     fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.TRUE_VALUE',
9382    ' l_termn_level = ' || l_termn_level);
9383  END IF;
9384 
9385  --npalepu removed the following code for bug # 5335312.
9386  --Reverted back the changes made for the bug # 5161038
9387  /* --mchoudha Fix for bug#5161038
9388  okc_context.set_okc_org_context(p_chr_id =>l_header_id ); */
9389 
9390  OKS_RENEW_UTIL_PUB.Get_Period_Defaults
9391                 (
9392                  p_hdr_id        => l_header_id,
9393                  p_org_id        => NULL,
9394                  x_period_start  => l_period_start,
9395                  x_period_type   => l_period_type,
9396                  x_price_uom     => l_price_uom,
9397                  x_return_status => x_return_status);
9398 
9399  IF x_return_status <> 'S' THEN
9400    RAISE G_EXCEPTION_HALT_VALIDATION;
9401  END IF;
9402 
9403  IF l_termn_level = 'CP' then
9404     OPEN  l_lse_id_csr (p_true_value_tbl(1).p_cp_line_id);
9405     FETCH l_lse_id_csr into l_lse_id_rec;
9406     CLOSE l_lse_id_csr;
9407     --mchoudha 11510+ Usage Tax and Price Display
9408     if(l_lse_id_rec.lse_id = 13) Then
9409       Open l_usage_type_csr(p_true_value_tbl(1).p_cp_line_id);
9410       Fetch l_usage_type_csr into l_usage_type;
9411       Close l_usage_type_csr;
9412     End If;
9413     --mchoudha 11510+ Usage Tax and Price Display
9414     --added the condition l_usage_type='NPR'
9415     IF l_lse_id_rec.lse_id <>13 OR l_usage_type='NPR' then
9416       -------------------------------------------------------------------------
9417       --        True value processing for Covered Product Termination        --
9418       -------------------------------------------------------------------------
9419       OPEN  l_sub_line_csr(p_true_value_tbl(1).p_cp_line_id ) ;
9420       FETCH l_sub_line_csr into l_original_amt , l_top_line_id , l_hdr_id ;
9421       CLOSE l_sub_line_csr ;
9422 
9423       OPEN l_bill_amount_csr(p_true_value_tbl(1).p_cp_line_id ) ;
9424       FETCH l_bill_amount_csr into l_bill_amt ;
9425       CLOSE l_bill_amount_csr  ;
9426 
9427       OPEN l_credit_csr(p_true_value_tbl(1).p_cp_line_id ) ;
9428       FETCH l_credit_csr into l_credit ;
9429       CLOSE l_credit_csr ;
9430 
9431       OPEN l_suppressed_credit_csr(p_true_value_tbl(1).p_cp_line_id ) ;
9432       FETCH l_suppressed_credit_csr into l_suppressed_credit ;
9433       CLOSE l_suppressed_credit_csr ;
9434 
9435       l_true_value := nvl(l_bill_amt,0) +
9436                       nvl(l_credit,0)   + nvl(l_suppressed_credit,0) ;
9437 
9438       l_ubt_amount := nvl(l_original_amt,0)  - nvl(l_bill_amt,0) ;
9439       IF l_period_type is not null and l_period_start is not null
9440          --AND Nvl(p_true_value_tbl(1).p_override_amount,0) = 0  /* Added for Bug#14513858 */
9441         AND p_true_value_tbl(1).p_override_amount IS NULL  /* Modified for Bug#16015733 */
9442 	THEN
9443          OPEN l_ubt_csr(p_true_value_tbl(1).p_cp_line_id);
9444          FETCH l_ubt_csr into l_ubt_amount;
9445          CLOSE l_ubt_csr;
9446       END IF;
9447 --takintoy bug 4293344
9448 --Changed if condition operator to >= instead of =
9449       IF p_true_value_tbl(1).p_override_amount >= 0 then
9450          l_overridden := nvl(p_true_value_tbl(1).p_con_terminate_amount,0)
9451                       - nvl(p_true_value_tbl(1).p_termination_amount,0);
9452       ELSE
9453          l_overridden := 0;
9454       END IF;
9455 
9456       l_true_value := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_true_value,l_currency_code);
9457 
9458       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
9459          fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.TRUE_VALUE',
9460          'Before updating okc_k_lines_b for subline l_true_value = ' || l_true_value);
9461       END IF;
9462 
9463       UPDATE OKC_K_LINES_B
9464          SET price_negotiated = l_true_value
9465        WHERE id = p_true_value_tbl(1).p_cp_line_id;
9466 
9467       ----Credit Amounts on the contract header are to be populated correctly if there are negative amount sublines and also in case of full credit ..So modified the below condi for bug 11773025
9468       If l_credit < 0 OR (l_credit>0 AND (l_bill_amt < 0 OR p_true_value_tbl(1).p_full_credit ='Y')) THEN  --- 11773025
9469          l_credit := l_credit * -1;
9470       End IF;
9471      ----Suppressed Amounts on the contract header are to be populated correctly if there are negative amount sublines ..So modified the below condi for bug 11773025
9472       If l_suppressed_credit < 0  OR l_bill_amt <0 then   ---Added the condition l_original_amt <0 for bug 11773025
9473          l_suppressed_credit := l_suppressed_credit * -1 ;
9474       End if;
9475 
9476       G_RAIL_REC.AMOUNT := l_true_value;
9477       G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG := null;
9478 
9479  /*Added for bug:10285124*/
9480       open sub_line_tax_check(p_true_value_tbl(1).p_cp_line_id);
9481       fetch sub_line_tax_check into l_flag;
9482       close sub_line_tax_check;
9483  /*Added for bug:10285124*/
9484     If (l_flag >0) then     /*Added for bug:10285124*/
9485       OKS_TAX_UTIL_PVT.Get_Tax
9486          ( p_api_version      => 1.0,
9487            p_init_msg_list    => OKC_API.G_TRUE,
9488            p_chr_id           => l_hdr_id,
9489            p_cle_id           => p_true_value_tbl(1).p_cp_line_id,
9490            px_rail_rec        => G_RAIL_REC,
9491            x_msg_count        => x_msg_count,
9492            x_msg_data         => x_msg_data,
9493            x_return_status    => l_return_status);
9494 /* Modified by sjanakir for Bug#6912454 */
9495       IF ( l_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
9496             RAISE G_EXCEPTION_HALT_VALIDATION;
9497       END IF;
9498   End If;    /*Added for bug:10285124*/
9499 
9500       If G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG = 'N' then
9501          l_tax_value := nvl(G_RAIL_REC.TAX_VALUE,0);       /*Modified for bug:10285124*/
9502       Else
9503          l_tax_value := 0;
9504       End If;
9505 
9506       l_ubt_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_ubt_amount,
9507                                                              l_currency_code);
9508       l_credit := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_credit,
9509                                                          l_currency_code);
9510       l_suppressed_credit :=
9511                   OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_suppressed_credit,
9512                                                          l_currency_code);
9513       l_overridden := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_overridden,
9514                                                              l_currency_code);
9515 
9516       UPDATE OKS_K_LINES_B
9517          SET UBT_AMOUNT        = l_ubt_amount ,
9518              CREDIT_AMOUNT     = l_credit ,
9519              SUPPRESSED_CREDIT = l_suppressed_credit ,
9520              OVERRIDE_AMOUNT   = l_overridden ,
9521              TAX_AMOUNT        = l_tax_value,
9522              FULL_CREDIT       = nvl(p_true_value_tbl(1).p_full_credit,'N')
9523        WHERE cle_id = p_true_value_tbl(1).p_cp_line_id;
9524 
9525       UPDATE OKC_K_LINES_B
9526          SET price_negotiated=(SELECT SUM(price_negotiated)
9527                                 FROM okc_k_lines_b
9528                                WHERE cle_id = l_top_line_id
9529                                AND   date_cancelled is null) --LLC BUG FIX 4742661
9530        WHERE id = l_top_line_id ;
9531 
9532     ---if only subline is terminated, then also update top line ubt, credit_amt,suppressed_credit field
9533     --so that actual line amt can be found.
9534 
9535      Update OKS_K_lines_b
9536          set ubt_amount = nvl(ubt_amount,0) + nvl(l_ubt_amount,0),
9537              CREDIT_AMOUNT     = nvl(CREDIT_AMOUNT,0) + nvl(l_credit,0) ,
9538              SUPPRESSED_CREDIT = nvl(SUPPRESSED_CREDIT,0) + nvl(l_suppressed_credit,0)
9539      WHERE cle_id = l_top_line_id;
9540 
9541       UPDATE OKC_K_HEADERS_B
9542          SET estimated_amount=(SELECT SUM(price_negotiated)
9543                                  FROM OKC_K_LINES_B
9544                                 WHERE dnz_chr_id = l_hdr_id
9545                                   AND lse_id in ( 1,12,14,19,46))
9546        WHERE id = l_hdr_id ;
9547       End If;
9548       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
9549          fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.TRUE_VALUE',
9550          'After updating all tables for true values in case of cp');
9551       END IF;
9552  ELSIF l_termn_level = 'TL' then
9553       -------------------------------------------------------------------------
9554       --           True value processing for Top Line Termination            --
9555       -------------------------------------------------------------------------
9556       l_usage_type:= NULL;
9557       OPEN  l_lse_id_csr(p_true_value_tbl(1).p_top_line_id);
9558       FETCH l_lse_id_csr into l_lse_id_rec ;
9559       CLOSE l_lse_id_csr;
9560       l_process := FALSE;
9561     --mchoudha 11510+ Usage Tax and Price Display
9562       If l_lse_id_rec.lse_id = 12 Then
9563         OPEN l_usage_type_csr1(p_true_value_tbl(1).p_top_line_id);
9564         Fetch l_usage_type_csr1 into l_usage_type;
9565         Close l_usage_type_csr1;
9566       End If;
9567     --mchoudha 11510+ Usage Tax and Price Display
9568     --added the condition l_usage_type='NPR'
9569       IF (l_lse_id_rec.lse_id in (1,19))  OR (l_usage_type='NPR' AND l_lse_id_rec.lse_id = 12) then
9570          FOR l_top_line_rec in l_top_line_csr(p_true_value_tbl(1).p_top_line_id)
9571          LOOP
9572             l_process := TRUE;
9573 
9574             OPEN  l_sub_line_csr(l_top_line_rec.sub_line_id) ;
9575             FETCH l_sub_line_csr into l_original_amt , l_top_line_id ,
9576                   l_hdr_id ;
9577             CLOSE l_sub_line_csr ;
9578 
9579             OPEN  l_bill_amount_csr(l_top_line_rec.sub_line_id) ;
9580             FETCH l_bill_amount_csr into l_bill_amt ;
9581             CLOSE l_bill_amount_csr  ;
9582 
9583             OPEN  l_credit_csr(l_top_line_rec.sub_line_id) ;
9584             FETCH l_credit_csr into l_credit ;
9585             CLOSE l_credit_csr ;
9586 
9587             OPEN  l_suppressed_credit_csr(l_top_line_rec.sub_line_id) ;
9588             FETCH l_suppressed_credit_csr into l_suppressed_credit ;
9589             CLOSE l_suppressed_credit_csr ;
9590 
9591             l_true_value := nvl(l_bill_amt,0) +
9592                             nvl(l_credit,0)   + nvl(l_suppressed_credit,0) ;
9593 
9594             l_ubt_amount := nvl(l_original_amt,0)  - nvl(l_bill_amt,0) ;
9595             IF l_period_type is not null and l_period_start is not null
9596              --AND Nvl(p_true_value_tbl(1).p_override_amount,0) = 0  /* Added for Bug#14513858 */
9597              AND p_true_value_tbl(1).p_override_amount IS NULL  /* Modified for Bug#16015733 */
9598 	    THEN
9599               OPEN l_ubt_csr(l_top_line_rec.sub_line_id);
9600               FETCH l_ubt_csr into l_ubt_amount;
9601               CLOSE l_ubt_csr;
9602             END IF;
9603 
9604             IF p_true_value_tbl(1).p_full_credit ='Y' then
9605                  PRE_TERMINATE_AMOUNT
9606                    (p_calledfrom     => 1.0 ,
9607                     p_id             => l_top_line_rec.sub_line_id ,
9608                     p_terminate_date => l_top_line_rec.start_date,
9609                     p_flag           => 3 ,
9610                     x_amount         => l_amount ,
9611                     --x_manual_credit  => l_manual_credit,
9612                     x_return_status  => l_return_status );
9613             ELSIF p_true_value_tbl(1).p_full_credit ='N' then
9614                  PRE_TERMINATE_AMOUNT
9615                    (p_calledfrom     => 1.0 ,
9616                     p_id             => l_top_line_rec.sub_line_id ,
9617                     p_terminate_date => p_true_value_tbl(1).p_termination_date ,
9618                     p_flag           => 3 ,
9619                     x_amount         => l_amount ,
9620                     --x_manual_credit  => l_manual_credit,
9621                     x_return_status  => l_return_status );
9622             END IF;
9623 
9624     --takintoy bug 4293344
9625     --Changed if condition operator to >= instead of =
9626             IF p_true_value_tbl(1).p_override_amount >= 0 then
9627                /* Modified for Bug#16015733 */
9628                IF l_amount = 0 THEN
9629                   l_overridden := nvl(p_true_value_tbl(1).p_con_terminate_amount,0)
9630                        - nvl(p_true_value_tbl(1).p_termination_amount,0);
9631                ELSE
9632                   l_overridden := l_amount ;
9633                END IF;
9634             ELSE
9635                l_overridden := 0;
9636             END IF;
9637 
9638             l_true_value :=OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_true_value,
9639                                                                l_currency_code);
9640             UPDATE OKC_K_LINES_B
9641                SET price_negotiated = l_true_value
9642              WHERE id = l_top_line_rec.sub_line_id;
9643 
9644             G_RAIL_REC.AMOUNT := l_true_value;
9645             G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG := null;
9646 
9647     /*Added for bug:10285124*/
9648       open sub_line_tax_check(l_top_line_rec.sub_line_id);
9649       fetch sub_line_tax_check into l_flag;
9650       close sub_line_tax_check;
9651      /*Added for bug:10285124*/
9652 
9653       If (l_flag >0) then     /*Added for bug:10285124*/
9654             OKS_TAX_UTIL_PVT.Get_Tax
9655                  ( p_api_version      => 1.0,
9656                    p_init_msg_list    => OKC_API.G_TRUE,
9657                    p_chr_id           => l_hdr_id,
9658                    p_cle_id           => l_top_line_rec.sub_line_id,
9659                    px_rail_rec        => G_RAIL_REC,
9660                    x_msg_count        => x_msg_count,
9661                    x_msg_data         => x_msg_data,
9662                    x_return_status    => l_return_status);
9663 /* Modified by sjanakir for Bug#6912454 */
9664             IF ( l_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
9665                 RAISE G_EXCEPTION_HALT_VALIDATION;
9666             END IF;
9667     End IF;     /*Added for bug:10285124*/
9668 
9669             If G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG = 'N' then
9670                l_tax_value     := nvl(G_RAIL_REC.tax_value,0) ;   /*Modified for bug:10285124*/
9671                l_tot_tax_value := nvl(l_tax_value,0) + l_tot_tax_value;
9672             Else
9673                l_tax_value     := 0 ;
9674                l_tot_tax_value := nvl(l_tax_value,0) + l_tot_tax_value;
9675             End If;
9676 
9677                ----Credit Amounts on the contract header are to be populated correctly if there are negative amount lines and also in case of full credit ..So modified the below condi for bug 11773025
9678             If l_credit < 0  OR (l_credit>0 AND (l_bill_amt < 0 OR p_true_value_tbl(1).p_full_credit ='Y')) THEN   ---Modified the condition for bug 11773025
9679                l_credit := l_credit * -1;
9680             End IF;
9681                ----Suppressed Amounts on the contract header are to be populated correctly if there are negative amount lines ..So modified the below condi for bug 11773025
9682             If l_suppressed_credit < 0  OR l_bill_amt < 0 then    --Added the condition l_original_amt for bug 11773025
9683                l_suppressed_credit := l_suppressed_credit * -1 ;
9684             End if;
9685 
9686             l_ubt_amount :=OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_ubt_amount,l_currency_code);
9687             l_credit := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_credit,
9688                                                               l_currency_code);
9689             l_suppressed_credit :=
9690                    OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_suppressed_credit,
9691                                                           l_currency_code);
9692             l_overridden := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_overridden,
9693                                                                l_currency_code);
9694 
9695             UPDATE OKS_K_LINES_B
9696                SET UBT_AMOUNT       = l_ubt_amount ,
9697                    CREDIT_AMOUNT    = l_credit ,
9698                    SUPPRESSED_CREDIT= l_suppressed_credit ,
9699                    OVERRIDE_AMOUNT  = l_overridden,
9700                    TAX_AMOUNT       = l_tax_value,
9701                    FULL_CREDIT      = nvl(p_true_value_tbl(1).p_full_credit,'N')
9702             WHERE cle_id = l_top_line_rec.sub_line_id;
9703 
9704             l_tot_ubt_amount        := l_tot_ubt_amount + nvl(l_ubt_amount,0) ;
9705             l_tot_credit            := l_tot_credit + nvl(l_credit,0);
9706             l_tot_suppressed_credit := l_tot_suppressed_credit +
9707                                        nvl(l_suppressed_credit,0);
9708             l_tot_overridden        := l_tot_overridden + nvl(l_overridden,0);
9709 
9710          END LOOP;
9711 
9712          If l_process then
9713             UPDATE OKC_K_LINES_B
9714                SET price_negotiated=(SELECT SUM(price_negotiated)
9715                                        FROM okc_k_lines_b
9716                                       WHERE cle_id = l_top_line_id
9717                                       AND   date_cancelled is null) --LLC BUG FIX 4742661
9718              WHERE id = p_true_value_tbl(1).p_top_line_id ;
9719 
9720             l_tot_ubt_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_ubt_amount,
9721                                                            l_currency_code);
9722             l_tot_credit := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_credit,
9723                                                            l_currency_code);
9724             l_tot_suppressed_credit :=
9725                  OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_suppressed_credit,
9726                                                         l_currency_code);
9727             l_tot_overridden := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_overridden,
9728                                                         l_currency_code);
9729 
9730             open l_check_for_full_credit(p_true_value_tbl(1).p_top_line_id);
9731             fetch l_check_for_full_credit into l_full_count;
9732             close l_check_for_full_credit;
9733             If l_full_count >  0 then
9734                l_full_yn := 'N';
9735             else
9736                l_full_yn := nvl(p_true_value_tbl(1).p_full_credit,'N');
9737             End If;
9738 
9739             -- Update of tax amount for top line is comented out as part of BUG#3278807
9740             --changed for copy bug.
9741              UPDATE OKS_K_LINES_B
9742                SET UBT_AMOUNT        = nvl(UBT_AMOUNT,0) + nvl(l_tot_ubt_amount,0) ,
9743                    CREDIT_AMOUNT     = nvl(CREDIT_AMOUNT,0) + nvl(l_tot_credit,0) ,
9744                    SUPPRESSED_CREDIT = nvl(SUPPRESSED_CREDIT,0) + nvl(l_tot_suppressed_credit,0) ,
9745                    OVERRIDE_AMOUNT   = l_tot_overridden,
9746                    FULL_CREDIT       = l_full_yn
9747                    --TAX_AMOUNT        = l_tot_tax_value
9748              WHERE cle_id = p_true_value_tbl(1).p_top_line_id ;
9749 
9750         End If;
9751       ELSIF l_lse_id_rec.lse_id = 46 then
9752          l_original_amt := l_lse_id_rec.price_negotiated;
9753          l_hdr_id := l_lse_id_rec.dnz_chr_id;
9754 
9755          OPEN  l_bill_amount_csr(p_true_value_tbl(1).p_top_line_id) ;
9756          FETCH l_bill_amount_csr into l_tot_bill_amt ;
9757          CLOSE l_bill_amount_csr  ;
9758 
9759          OPEN  l_credit_csr(p_true_value_tbl(1).p_top_line_id) ;
9760          FETCH l_credit_csr into l_tot_credit ;
9761          CLOSE l_credit_csr ;
9762 
9763          OPEN  l_suppressed_credit_csr(p_true_value_tbl(1).p_top_line_id) ;
9764          FETCH l_suppressed_credit_csr into l_tot_suppressed_credit ;
9765          CLOSE l_suppressed_credit_csr ;
9766 
9767          l_tot_true_value:=nvl(l_tot_bill_amt,0) +
9768                            nvl(l_tot_credit,0)+nvl(l_tot_suppressed_credit,0) ;
9769 
9770          If l_tot_credit < 0 then
9771               l_tot_credit := l_tot_credit * -1 ;
9772          End If;
9773 
9774          If l_tot_suppressed_credit < 0 then
9775               l_tot_suppressed_credit := l_tot_suppressed_credit * -1 ;
9776          End If;
9777 
9778          l_tot_ubt_amount := nvl(l_lse_id_rec.price_negotiated,0) -
9779                              nvl(l_tot_bill_amt,0) ;
9780 
9781          l_tot_overridden :=p_true_value_tbl(1).p_con_terminate_amount -
9782                             nvl(l_tot_credit,0) -  nvl(l_tot_suppressed_credit,0);
9783 
9784          l_tot_true_value:=OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_true_value,
9785                                                              l_currency_code);
9786          UPDATE OKC_K_LINES_B
9787             SET price_negotiated = l_tot_true_value
9788           WHERE id = p_true_value_tbl(1).p_top_line_id;
9789 
9790          G_RAIL_REC.AMOUNT := l_true_value;
9791          G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG := null;
9792 
9793   /*Added for bug:10285124*/
9794       open sub_line_tax_check(p_true_value_tbl(1).p_top_line_id);
9795       fetch sub_line_tax_check into l_flag;
9796       close sub_line_tax_check;
9797  /*Added for bug:10285124*/
9798 
9799     If (l_flag >0) then   /*Added for bug:10285124*/
9800          OKS_TAX_UTIL_PVT.Get_Tax
9801                ( p_api_version      => 1.0,
9802                  p_init_msg_list    => OKC_API.G_TRUE,
9803                  p_chr_id           => l_hdr_id,
9804                  p_cle_id           => p_true_value_tbl(1).p_top_line_id,
9805                  px_rail_rec        => G_RAIL_REC,
9806                  x_msg_count        => x_msg_count,
9807                  x_msg_data         => x_msg_data,
9808                  x_return_status    => l_return_status);
9809 /* Modified by sjanakir for Bug#6912454 */
9810          IF ( l_return_status <> OKC_API.G_RET_STS_SUCCESS) THEN
9811                 RAISE G_EXCEPTION_HALT_VALIDATION;
9812          END IF;
9813    End IF;   /*Added for bug:10285124*/
9814 
9815          If G_RAIL_REC.AMOUNT_INCLUDES_TAX_FLAG = 'N' then
9816             l_tax_value     := nvl(G_RAIL_REC.tax_value,0) ;  /*Modified for bug:10285124*/
9817             l_tot_tax_value := nvl(l_tax_value,0) + l_tot_tax_value;
9818          Else
9819             l_tax_value     := 0 ;
9820             l_tot_tax_value := nvl(l_tax_value,0) + l_tot_tax_value;
9821          End If;
9822 
9823          l_tot_ubt_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_ubt_amount,
9824                                                           l_currency_code);
9825          l_tot_credit := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_credit,
9826                                                           l_currency_code);
9827          l_tot_suppressed_credit :=
9828               OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_suppressed_credit,
9829                                                      l_currency_code);
9830          l_tot_overridden := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_tot_overridden,
9831                                                      l_currency_code);
9832 
9833 
9834          UPDATE OKS_K_LINES_B
9835             SET UBT_AMOUNT        = l_tot_ubt_amount ,
9836                 CREDIT_AMOUNT     = l_tot_credit ,
9837                 SUPPRESSED_CREDIT = l_tot_suppressed_credit ,
9838                 OVERRIDE_AMOUNT   = l_tot_overridden,
9839                 FULL_CREDIT       = nvl(p_true_value_tbl(1).p_full_credit,'N'),
9840                 TAX_AMOUNT        = l_tot_tax_value
9841           WHERE cle_id = p_true_value_tbl(1).p_top_line_id;
9842 
9843       END IF;
9844 
9845     --mchoudha 11510+ Usage Tax and Price Display
9846     --added lse_id 12
9847       IF p_true_value_tbl(1).p_hdr_id = 0 then
9848          IF l_lse_id_rec.lse_id in ( 1,12,19,46 ) then
9849             UPDATE OKC_K_HEADERS_B
9850                SET estimated_amount=(SELECT SUM(price_negotiated)
9851                                        FROM OKC_K_LINES_B
9852                                       WHERE dnz_chr_id = l_hdr_id
9853                                         AND lse_id in ( 1,12,14,19,46))
9854                                       WHERE id = l_hdr_id ;
9855          END IF;
9856       END IF;
9857       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
9858          fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.TRUE_VALUE',
9859          'After updating all tables for true values in case of top line');
9860       END IF;
9861 
9862  ELSIF l_termn_level = 'HD' then
9863       -------------------------------------------------------------------------
9864       --             True value processing for Header Termination            --
9865       -------------------------------------------------------------------------
9866       UPDATE OKC_K_HEADERS_B
9867          SET estimated_amount=(SELECT SUM(price_negotiated)
9868                                  FROM OKC_K_LINES_B
9869                                 WHERE dnz_chr_id = p_true_value_tbl(1).p_hdr_id
9870                                   AND lse_id in ( 1,12,14,19,46))
9871                                 WHERE id = p_true_value_tbl(1).p_hdr_id ;
9872       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
9873          fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.TRUE_VALUE',
9874          'After updating all tables for true values in case of header');
9875       END IF;
9876 
9877  END IF;
9878 
9879  EXCEPTION
9880  WHEN G_EXCEPTION_HALT_VALIDATION THEN
9881     /*Added for bug 11702843*/
9882         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
9883 	OKC_API.SET_MESSAGE(p_app_name     =>'OKS',p_msg_name     =>'OKS_MD_TERMINATION_FAILED');
9884        x_return_status :=OKC_API.G_RET_STS_ERROR;
9885     /*Added for bug 11702843*/
9886  WHEN OTHERS THEN
9887      x_return_status := OKC_API.G_RET_STS_ERROR ;
9888 
9889 END TRUE_VALUE ;
9890 
9891  Procedure prorate_price_breaks ( P_BSL_ID        IN         NUMBER,
9892                                   P_BREAK_AMOUNT  IN         NUMBER,
9893                                   P_TOT_AMOUNT    IN         NUMBER,
9894                                   X_RETURN_STATUS OUT NOCOPY VARCHAR2 ) is
9895  CURSOR price_break_csr (P_BSL_ID  IN  NUMBER) is
9896   SELECT pb.id, pb.amount,pb.unit_price , pb.quantity ,
9897          hdr.currency_code
9898   FROM OKS_PRICE_BREAKS  pb,
9899        OKC_K_HEADERS_B   hdr
9900   WHERE  pb.bsl_id = P_BSL_ID
9901   AND    pb.chr_id = hdr.id
9902   ORDER BY pb.quantity_from;
9903 
9904   l_amount        NUMBER;
9905   l_unit_price    NUMBER;
9906   l_id            NUMBER;
9907   l_running_total NUMBER;
9908   l_currency_code VARCHAR2(10);
9909  BEGIN
9910    l_running_total := 0;
9911    FOR cur in price_break_csr(p_bsl_id)
9912    LOOP
9913      l_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(((p_tot_amount * cur.amount) / p_break_amount),cur.currency_code);
9914      l_unit_price := round((l_amount / cur.quantity) ,29);
9915 
9916      l_running_total := l_running_total + l_amount;
9917      l_id            := cur.id;
9918      l_currency_code := cur.currency_code;
9919 
9920      UPDATE oks_price_breaks
9921      SET amount = l_amount,
9922          unit_price = l_unit_price
9923      WHERE id = cur.id;
9924    END LOOP;
9925 
9926    IF ( l_running_total  <>  P_TOT_AMOUNT) THEN
9927      UPDATE oks_price_breaks
9928      SET amount = OKS_EXTWAR_UTIL_PVT.round_currency_amt(amount + (P_TOT_AMOUNT - l_running_total),l_currency_code),
9929          unit_price =  round((amount + (P_TOT_AMOUNT - l_running_total)) / quantity,29)
9930      WHERE id = l_id;
9931    END IF;
9932  END prorate_price_breaks ;
9933 
9934 
9935 ------------------------------------------------------------------------
9936 -- Begin partial period computation logic
9937 -- Developer Mani Choudhary
9938 -- Date 27-MAY-2005
9939 -- DESCRIPTION:
9940 --This procedure calculates the termination amount between the termination date
9941 --and the subline end date and also creates the credit or invoicing transactions
9942 --by calling create_trx_records. Calculate the unbilled amount and the subline
9943 --new extended price. Compare the unbilled amount with termination amount and
9944 --subline new extended price with the billed amount. If the unbilled amount <
9945 --termination amount , then the difference is issued as credit.If the subline
9946 --new extended price is greater than the billed amount , then  the difference
9947 --is issued as the invoice amount from the line start date to termination date-1.
9948 ---------------------------------------------------------------------------------
9949 Procedure  Terminate_Ppc
9950 (
9951                            P_called_from          IN NUMBER DEFAULT NULL,
9952                            P_end_date             IN DATE,
9953                            P_termination_date     IN DATE,
9954                            P_top_line_id          IN NUMBER,
9955                            P_cp_line_id           IN NUMBER,
9956                            P_suppress_credit      IN VARCHAR2,
9957                            P_period_type          IN VARCHAR2,
9958                            P_period_start         IN VARCHAR2,
9959                            P_override_amount      IN NUMBER,
9960                            P_con_terminate_amount IN NUMBER,
9961                            X_return_status        OUT NOCOPY VARCHAR2
9962 )
9963 IS
9964 
9965   --Cursor to get the details of the subline
9966  CURSOR covlvl_line(p_cle_id IN   NUMBER,p_cp_line_id IN NUMBER) is
9967   SELECT line.id,
9968          line.start_date,
9969          line.end_date,
9970          line.lse_id,
9971          line.cle_id,
9972          line.dnz_chr_id,
9973          line.price_unit*itm.number_of_items unit_price,
9974          rline.price_uom,
9975          line.price_negotiated total_amount,
9976          rline.toplvl_uom_code
9977    FROM  okc_k_lines_b   line,
9978          oks_k_lines_b   rline,
9979          okc_k_items     itm
9980    WHERE line.cle_id = p_cle_id
9981    AND   line.lse_id in (7,8,9,10,11,13,25,35)
9982    AND   line.id     = nvl(p_cp_line_id,line.id)
9983    AND   rline.cle_id    = line.id
9984    AND   itm.cle_id = line.id
9985    AND   line.date_cancelled is NULL --LLC BUG FIX 4742661
9986    AND   line.date_terminated IS NULL;
9987 
9988 --Cursor to fetch the total Billed amount,maximum date Billed
9989 -- to and maximum date billed from
9990 Cursor total_billed_csr (p_cle_id   IN NUMBER)
9991 IS
9992 SELECT sum(bsl.amount) ,
9993        max(bsl.date_billed_From),
9994        max(bsl.date_billed_to)
9995    FROM  oks_bill_sub_lines bsl
9996    WHERE bsl.cle_id = p_cle_id;
9997 
9998 --Cursor to get the billing details for the subline
9999 Cursor bsl_cur (p_cle_id IN NUMBER)
10000 IS
10001 SELECT bsl.id,
10002        bsl.date_billed_from,
10003        bsl.date_billed_to,
10004        bsl.amount,
10005        bsd.result
10006   FROM oks_bill_cont_lines    bcl,
10007        oks_bill_sub_lines     bsl,
10008        oks_bill_sub_line_dtls bsd
10009   WHERE bcl.id  =  bsl.bcl_id
10010   AND   bsl.cle_id = p_cle_id
10011   AND   bsd.bsl_id = bsl.id
10012  ORDER by bsl.date_billed_to desc;
10013 
10014 Cursor max_billto_date_csr(p_cle_id   IN NUMBER)
10015 IS
10016 SELECT max(TRUNC(lvl.date_end))
10017 FROM   oks_level_elements lvl
10018 WHERE  cle_id = p_cle_id;
10019 
10020  CURSOR L_HDR_CURR_CODE (p_id in number ) is
10021  SELECT currency_code
10022    FROM okc_k_headers_b
10023   WHERE id = p_id;
10024 
10025 CURSOR l_line_BS_csr(p_line_id  NUMBER,p_date in date) IS
10026   SELECT id,trunc(date_start) date_start,
10027          amount,trunc(date_end) date_end
10028          FROM oks_level_elements
10029          WHERE cle_id = p_line_id
10030          AND   TRUNC(date_start) > p_date
10031          ORDER BY date_start;
10032 
10033 CURSOR om_period_csr(p_id IN NUMBER) IS
10034 select oel.service_period,oel.service_duration,
10035        oel.ordered_quantity
10036 from   okc_k_rel_objs rel,
10037        oe_order_lines_all oel
10038 where  rel.cle_id = p_id
10039 and    oel.line_id  = rel.object1_id1;
10040 
10041 
10042 l_unbilled_amount      NUMBER;
10043 l_new_price            NUMBER;
10044 l_termination_amount   NUMBER;
10045 l_quantity             NUMBER;
10046 l_billed_amount        NUMBER;
10047 l_credit_amount        NUMBER;
10048 l_term_amount          NUMBER;
10049 l_max_date_billed_from DATE;
10050 l_max_date_billed_to   DATE;
10051 l_duration             NUMBER;
10052 l_uom                  VARCHAR2(30);
10053 l_unit_price           NUMBER;
10054 l_line_end_date        DATE;
10055 l_termination_date     DATE;
10056 l_currency_code        Varchar2(15);
10057 l_ubt_amount           NUMBER;
10058 l_invoice_amount       NUMBER;
10059 l_inv_amount           NUMBER;
10060  l_source_uom_quantity      NUMBER;
10061  l_source_tce_code          VARCHAR2(30);
10062  l_target_uom_quantity      NUMBER;
10063  l_target_tce_code          VARCHAR2(30);
10064  l_return_status     VARCHAR2(1);
10065 l_service_period          VARCHAR2(30);
10066 l_service_duration        NUMBER;
10067 l_ordered_quantity        NUMBER;
10068 
10069 BEGIN
10070 
10071 x_return_status := 'S';
10072 --If the override amount is not null then create termination records with
10073 --override amount
10074 IF  p_override_amount is NOT NULL THEN
10075 
10076       Create_trx_records(
10077          p_called_from          => 1, --changed for 13846967 by ansraj
10078          p_top_line_id          => p_top_line_id,
10079          p_cov_line_id          => p_cp_line_id,
10080          p_date_from            => P_termination_date,
10081          p_date_to              => p_end_date,
10082          p_amount               => 0,
10083          p_override_amount      => p_override_amount,
10084          p_suppress_credit      => p_suppress_credit,
10085          p_con_terminate_amount => p_con_terminate_amount,
10086          p_bill_action          => 'TR',
10087          x_return_status        => x_return_status,
10088          P_termination_date => p_termination_date  --skuchima 11691332
10089         );
10090 
10091       IF x_return_status <> 'S' THEN
10092         RAISE G_EXCEPTION_HALT_VALIDATION;
10093       END IF;
10094 
10095 ELSE --p_override_amount is null
10096 
10097  --Loop through the sublines and find the subline details
10098 
10099   FOR cur in covlvl_line(p_top_line_id,p_cp_line_id) LOOP
10100 
10101    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10102       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10103      'p_top_line_id '||p_top_line_id||' ,p_cp_line_id '||p_cp_line_id);
10104    END IF;
10105 
10106    IF p_termination_date < cur.start_date THEN
10107       l_termination_date := cur.start_date;
10108    ELSE
10109       l_termination_date :=  p_termination_date;
10110    END IF;
10111    Open max_billto_date_csr(cur.id);
10112    Fetch max_billto_date_csr into l_line_end_date;
10113    Close max_billto_date_csr;
10114    IF (l_line_end_date >= l_termination_date) THEN
10115 
10116    --If price uom is null then fetch the uom based on effective dates
10117    --IF (cur.price_uom IS NULL) THEN
10118      OKC_TIME_UTIL_PUB.Get_Duration(cur.start_date,l_line_end_date,l_duration,l_uom,x_return_status);
10119 
10120      IF x_return_status <> 'S' THEN
10121        RAISE G_EXCEPTION_HALT_VALIDATION;
10122      END IF;
10123    --END IF;
10124    --mchoudha Added for partial periods CR-003
10125    Open om_period_csr(cur.id);
10126    Fetch om_period_csr into l_service_period,l_service_duration,l_ordered_quantity;
10127    Close om_period_csr;
10128    IF  l_service_period IS  NULL THEN  --OKS case
10129 
10130     -- IF cur.Unit_Price IS NULL THEN
10131     --   l_unit_price := cur.total_amount/l_duration;
10132     -- ELSE
10133       --mchoudha for bug#4961636
10134       --The unit price will be directly picked from the DB for the product/item
10135       IF cur.lse_id in (7,9,25) THEN
10136         --commenting this for bug#5306921
10137         --The conversion from pricelist uom to price uom is not required as per bug#4686993
10138        /* IF cur.toplvl_uom_code <> nvl(cur.price_uom,l_uom) THEN
10139           l_unit_price := OKS_BILL_SCH.Get_Converted_price(
10140                                             p_price_uom        =>nvl(cur.price_uom,l_uom),
10141                                             p_pl_uom           =>cur.toplvl_uom_code ,
10142                                             p_period_start     =>p_period_start,
10143                                             p_period_type      =>p_period_type,
10144                                             p_price_negotiated =>cur.total_amount,
10145                                             p_unit_price       =>cur.unit_price,
10146                                             p_start_date       =>cur.start_date,
10147                                             p_end_date         =>l_line_end_date
10148                                             );
10149           IF l_unit_price is NULL THEN
10150             RAISE G_EXCEPTION_HALT_VALIDATION;
10151           END IF;
10152         ELSE*/
10153         --Fix for bug#5623498 unit price will be recalculated based on duration based uom if pricelist is not present
10154         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10155                        (p_start_date   => cur.start_date,
10156                         p_end_date     => l_line_end_date,
10157                         p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10158                         p_period_type  => p_period_type,
10159                         p_period_start => p_period_start);
10160         l_unit_price := cur.total_amount / l_quantity;
10161        -- END IF;
10162        -- Calculation of Termination amount using Partial Periods Method
10163        -- Termination Period Start Date  = l_termination_date
10164        -- Termination Period End of date = p_line_end_date
10165        -- Determine the termination duration quantity in terms of price list uom for bug#5306921
10166         l_quantity:= OKS_TIME_MEASURES_PUB.get_quantity(p_start_date   => l_termination_date,
10167                                                   p_end_date     => l_line_end_date,
10168                                                   p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10169                                                   p_period_type  => p_period_type,
10170                                                   p_period_start => p_period_start);
10171 
10172       ELSE  --for manual covered levels added for bug#4961636
10173         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10174                        (p_start_date   => cur.start_date,
10175                         p_end_date     => l_line_end_date,
10176                         p_source_uom   => l_uom,
10177                         p_period_type  => p_period_type,
10178                         p_period_start => p_period_start);
10179 
10180         l_unit_price := cur.total_amount/l_quantity;
10181      -- Calculation of Termination amount using Partial Periods Method
10182      -- Termination Period Start Date  = l_termination_date
10183      -- Termination Period End of date = p_line_end_date
10184      -- Determine the termination duration quantity in terms of effectivity uom for bug#5306921
10185      l_quantity:= OKS_TIME_MEASURES_PUB.get_quantity(p_start_date   => l_termination_date,
10186                                                   p_end_date     => l_line_end_date,
10187                                                   p_source_uom   => l_uom,
10188                                                   p_period_type  => p_period_type,
10189                                                   p_period_start => p_period_start);
10190 
10191       END IF;
10192 
10193     -- END IF;
10194    ELSE
10195         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10196                        (p_start_date   => cur.start_date,
10197                         p_end_date     => l_line_end_date,
10198                         p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10199                         p_period_type  => p_period_type,
10200                         p_period_start => p_period_start);
10201         l_unit_price := cur.total_amount / l_quantity;
10202 
10203      -- Calculation of Termination amount using Partial Periods Method
10204      -- Termination Period Start Date  = l_termination_date
10205      -- Termination Period End of date = p_line_end_date
10206      -- Determine the termination duration quantity in terms of price list uom for bug#5306921
10207      l_quantity:= OKS_TIME_MEASURES_PUB.get_quantity(p_start_date   => l_termination_date,
10208                                                   p_end_date     => l_line_end_date,
10209                                                   p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10210                                                   p_period_type  => p_period_type,
10211                                                   p_period_start => p_period_start);
10212 
10213    END IF;
10214 
10215      IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10216         fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10217        'After calling OKS_TIME_MEASURES_PUB.get_quantity with end date '||l_line_end_date||' ,p_source_uom '
10218        ||nvl(cur.price_uom,l_uom)||' p_period_start ' ||p_period_start||' ,p_period_type '||p_period_type
10219        ||' l_termination_date '||to_char(l_termination_date)||'result l_quantity '||l_quantity);
10220      END IF;
10221 
10222 -- Get the total billed amount and max date Billed to and
10223 -- max date billed from of the subline
10224 
10225   IF nvl(l_quantity,0) = 0 THEN
10226       RAISE G_EXCEPTION_HALT_VALIDATION;
10227   END IF;
10228 
10229   l_termination_amount := l_quantity * l_unit_price;
10230 
10231   IF l_termination_amount > cur.total_amount and cur.total_amount >0 THEN ----Added the condition cur.total_amount>0 for bug 11773025
10232     l_termination_amount :=  cur.total_amount;
10233   END IF;
10234 
10235   IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10236         fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10237        ' l_termination_amount '||l_termination_amount);
10238   END IF;
10239 
10240   OPEN   total_billed_csr(cur.id);
10241   FETCH  total_billed_csr into  l_billed_amount,
10242                                 l_max_date_billed_from,
10243                                 l_max_date_billed_to;
10244   CLOSE  total_billed_csr;
10245 
10246 -- Calculate the unbilled amount
10247   l_unbilled_amount := cur.total_Amount - l_billed_Amount ;
10248 
10249    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10250       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10251      ' l_unbilled_amount '||l_unbilled_amount);
10252    END IF;
10253 
10254    open  l_hdr_curr_code(cur.dnz_chr_id);
10255    fetch l_hdr_curr_code into l_currency_code;
10256    close l_hdr_curr_code ;
10257 --Calculate the new subline extended amount
10258   l_new_price := cur.total_amount - l_termination_amount;
10259   IF (cur.total_amount <0 and l_unbilled_amount >= l_termination_amount) OR (cur.total_amount >0 AND l_termination_amount >= l_unbilled_amount) THEN    ---Modified the condition for bug 11773025
10260    l_ubt_amount := l_unbilled_amount;
10261   ELSE
10262    l_ubt_amount := cur.total_amount - l_new_price;
10263   END IF;
10264 
10265   l_new_price := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_new_price,l_currency_code);
10266   l_ubt_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_ubt_amount,l_currency_code);
10267 
10268   UPDATE OKC_K_LINES_B
10269   SET price_negotiated = l_new_price
10270   WHERE id = cur.id;
10271 
10272   UPDATE OKS_K_LINES_B
10273   SET UBT_AMOUNT = l_ubt_amount
10274   WHERE cle_id = cur.id;
10275 
10276 
10277   IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10278       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10279      ' l_new_price '||l_new_price);
10280   END IF;
10281     -----Credit Amounts passed to the AR are incorrect in case of Negative Amount Lines.So modified the cond for bug 11773025
10282     IF  (cur.total_amount <0 and l_unbilled_amount >= l_termination_amount AND l_max_date_billed_to >= l_termination_date)
10283 	OR (l_termination_amount > l_unbilled_amount AND l_max_date_billed_to >= l_termination_date) THEN -- Modified for Bug#13618980
10284      IF cur.total_amount <0 then
10285      l_credit_amount := l_unbilled_amount -  l_termination_amount;
10286      ELSE
10287      l_credit_amount := l_termination_amount -  l_unbilled_amount;
10288      END IF;
10289 
10290    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10291       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10292      ' l_credit_amount '||l_credit_amount);
10293    END IF;
10294 
10295 /*Added for bug 14250216    */
10296 l_credit_amount := OKS_EXTWAR_UTIL_PVT.round_currency_amt(l_credit_amount,l_currency_code);
10297 /*Added for bug 14250216    */
10298 
10299       --Loop through All the sublines and tie the credit amount
10300       --starting from the last billed period  upwards.
10301       FOR bsl_rec in bsl_cur(cur.id)
10302       LOOP
10303         IF (l_credit_amount <= 0) THEN
10304           EXIT;
10305         END IF;
10306 
10307         /*Added for bug 11773025*/
10308        -----Credit Amounts passed to the AR are incorrect in case of Negative Amount Lines.So modified the cond for bug 11773025
10309         IF cur.total_amount < 0  AND bsl_rec.amount <0 THEN
10310            bsl_rec.amount := bsl_rec.amount * -1;
10311         END IF;
10312        /*Added for bug 11773025*/
10313 
10314         IF (l_credit_amount >= bsl_rec.amount) THEN
10315           l_term_amount :=  bsl_rec.amount;
10316         ELSE
10317           --last iteration: This is a partial amount
10318           l_term_amount :=  l_credit_amount;
10319         END IF;
10320 
10321         /*Added for bug 11773025*/
10322        -----Credit Amounts passed to the AR are incorrect in case of Negative Amount Lines.So modified the cond for bug 11773025
10323         IF cur.total_amount < 0  AND bsl_rec.amount <0 THEN
10324            l_term_amount := l_term_amount * -1;
10325         END IF;
10326        /*Added for bug 11773025*/
10327 
10328         IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10329            fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10330            ' l_credit_amount '||l_term_amount);
10331         END IF;
10332         Create_trx_records(
10333                p_called_from           => p_called_from,
10334                p_top_line_id           => P_top_line_id,
10335                p_cov_line_id           => cur.id,
10336                p_date_from             => bsl_rec.date_billed_from,
10337                p_date_to               => bsl_rec.date_billed_to,
10338                p_amount                => NULL,
10339                p_override_amount       => l_term_amount,
10340                p_suppress_credit       => p_suppress_credit,
10341                p_con_terminate_amount  => bsl_rec.amount,
10342                p_bill_action           => 'TR',
10343                x_return_status         => x_return_status,
10344                P_termination_date => l_termination_date   --skuchima 11691332
10345                );
10346 
10347         IF x_return_status <> 'S' THEN
10348           RAISE G_EXCEPTION_HALT_VALIDATION;
10349         END IF;
10350 
10351         l_credit_amount := l_credit_amount - bsl_rec.amount;
10352 
10353       END LOOP;
10354 
10355      ELSIF l_termination_amount < l_unbilled_amount AND
10356            l_termination_date <= nvl(l_max_date_billed_to,cur.start_date-1) THEN
10357 
10358       l_invoice_amount := l_unbilled_amount - l_termination_amount;
10359       IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10360            fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10361            ' l_invoice_amount '||l_invoice_amount);
10362       END IF;
10363       FOR bs_rec in l_line_BS_csr(cur.id,l_max_date_billed_to)
10364       LOOP
10365         IF (l_invoice_amount <= 0) THEN
10366           EXIT;
10367         END IF;
10368 
10369         IF (l_invoice_amount >= bs_rec.amount) THEN
10370           l_inv_amount :=  bs_rec.amount;
10371         ELSE
10372           --last iteration: This is a partial amount
10373           l_inv_amount :=  l_invoice_amount;
10374         END IF;
10375         IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10376            fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Terminate_Ppc',
10377            ' l_inv_amount '||l_inv_amount);
10378         END IF;
10379        Create_trx_records(
10380                p_called_from           => 1,
10381                p_top_line_id           => P_top_line_id,
10382                p_cov_line_id           => cur.id,
10383                p_date_from             => bs_rec.date_start,
10384                p_date_to               => bs_rec.date_end,
10385                p_amount                => bs_rec.amount,
10386                p_override_amount       => 0,
10387                p_suppress_credit       => 'N',
10388                p_con_terminate_amount  => l_inv_amount ,
10389                p_period_type           => p_period_type,
10390                p_period_start          => p_period_start,
10391                p_bill_action           => 'RI',
10392                x_return_status         => x_return_status,
10393                P_termination_date      => l_termination_date --skuchima 11691332
10394                );
10395         IF x_return_status <> 'S' THEN
10396           RAISE G_EXCEPTION_HALT_VALIDATION;
10397         END IF;
10398 
10399         l_invoice_amount := l_invoice_amount - bs_rec.amount;
10400 
10401       END LOOP;
10402 
10403     END IF;
10404 
10405     END IF; -- l_line_end_date >= l_termination_date
10406   END LOOP;
10407 
10408 END IF; --p_override_amount is not null
10409 
10410 EXCEPTION
10411    WHEN G_EXCEPTION_HALT_VALIDATION THEN
10412         x_return_status := OKC_API.G_RET_STS_ERROR ;
10413    WHEN OTHERS THEN
10414         OKC_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
10415                             p_msg_name     => G_UNEXPECTED_ERROR,
10416                             p_token1       => G_SQLCODE_TOKEN,
10417                             p_token1_value => sqlcode,
10418                             p_token2       => G_SQLERRM_TOKEN,
10419                             p_token2_value => sqlerrm);
10420 
10421 
10422 END Terminate_Ppc;
10423 
10424 ------------------------------------------------------------------------
10425 -- Begin partial period computation logic
10426 -- Developer Mani Choudhary
10427 -- Date 27-MAY-2005
10428 -- DESCRIPTION:
10429 -- This procedure will calculate the credit amount used for previewing
10430 -- if the period start and period type are not null.
10431 -------------------------------------------------------------------------
10432 Procedure Get_Term_Amt_Ppc (
10433                              P_called_from       IN NUMBER DEFAULT NULL,
10434                              P_line_id           IN NUMBER,
10435                              P_cov_line          IN VARCHAR2,
10436                              P_termination_date  IN DATE,
10437                              p_period_type       IN VARCHAR2,
10438                              p_period_start      IN VARCHAR2,
10439                              x_amount            OUT NOCOPY NUMBER,
10440                              x_return_status     OUT NOCOPY VARCHAR2
10441                            )
10442 IS
10443 
10444 CURSOR covlvl_csr(p_id IN   NUMBER) is
10445 SELECT line.id,
10446        line.lse_id,
10447        line.start_date,
10448        line.end_date,
10449        line.price_unit*itm.number_of_items unit_price,
10450        rline.price_uom,
10451        rline.toplvl_uom_code,
10452        line.price_negotiated total_amount
10453 FROM   okc_k_lines_b   line,
10454        oks_k_lines_b   rline,
10455        okc_k_items     itm
10456 WHERE  line.id = p_id
10457   AND  rline.cle_id = line.id
10458   AND  itm.cle_id = line.id
10459   AND  line.date_cancelled is NULL --LLC BUG FIX 4742661
10460   AND  line.date_terminated is NULL;
10461 
10462 CURSOR line_csr(p_cle_id IN   NUMBER) is
10463 SELECT line.id,
10464        line.lse_id,
10465        line.start_date,
10466        line.end_date,
10467        line.price_unit*itm.number_of_items unit_price,
10468        rline.price_uom,
10469        rline.toplvl_uom_code,
10470        line.price_negotiated total_amount
10471 FROM   okc_k_lines_b   line,
10472        oks_k_lines_b   rline,
10473        okc_k_items     itm
10474 WHERE  line.cle_id = p_cle_id
10475   AND  itm.cle_id = line.id
10476   AND  rline.cle_id = line.id
10477   AND  line.date_cancelled is NULL --LLC BUG FIX 4742661
10478   AND  line.date_terminated is NULL;
10479 
10480 Cursor total_billed_csr (p_cle_id   IN NUMBER)
10481 IS
10482 SELECT sum(bsl.amount) ,
10483        max(bsl.date_billed_From),
10484        max(bsl.date_billed_to)
10485   FROM oks_bill_sub_lines bsl
10486  WHERE bsl.cle_id = p_cle_id;
10487 
10488 CURSOR om_period_csr(p_id IN NUMBER) IS
10489 select oel.service_period,oel.service_duration,
10490        oel.ordered_quantity
10491 from   okc_k_rel_objs rel,
10492        oe_order_lines_all oel
10493 where  rel.cle_id = p_id
10494 and    oel.line_id  = rel.object1_id1;
10495 
10496 Cursor max_billto_date_csr(p_cle_id   IN NUMBER)
10497 IS
10498 SELECT max(TRUNC(lvl.date_end))
10499 FROM   oks_level_elements lvl
10500 WHERE  cle_id = p_cle_id;
10501 
10502 l_quantity             NUMBER;
10503 l_termination_amount   NUMBER;
10504 covlvl_rec             covlvl_csr%rowtype;
10505 l_billed_amount        NUMBER;
10506 l_max_date_billed_from DATE;
10507 l_max_date_billed_to   DATE;
10508 l_unbilled_amount      NUMBER;
10509 l_credit_amount        NUMBER;
10510 l_duration             NUMBER;
10511 l_uom                  VARCHAR2(30);
10512 l_unit_price           NUMBER;
10513 l_line_end_date        DATE;
10514 l_termination_date     DATE;
10515  l_source_uom_quantity      NUMBER;
10516  l_source_tce_code          VARCHAR2(30);
10517  l_target_uom_quantity      NUMBER;
10518  l_target_tce_code          VARCHAR2(30);
10519  l_return_status     VARCHAR2(1);
10520 l_service_period          VARCHAR2(30);
10521 l_service_duration        NUMBER;
10522 l_ordered_quantity        NUMBER;
10523 BEGIN
10524 
10525 x_return_status:='S';
10526 
10527 IF  p_cov_line = 'Y'  THEN
10528 
10529    x_amount:= 0;
10530 
10531    Open covlvl_csr(p_line_id);
10532    Fetch covlvl_csr into covlvl_rec;
10533    Close covlvl_csr;
10534 
10535    IF p_termination_date < covlvl_rec.start_date THEN
10536       l_termination_date := covlvl_rec.start_date;
10537    ELSE
10538       l_termination_date :=  p_termination_date;
10539    END IF;
10540 
10541    Open max_billto_date_csr(p_line_id);
10542    Fetch max_billto_date_csr into l_line_end_date;
10543    Close max_billto_date_csr;
10544    IF (l_line_end_date >= l_termination_date) THEN
10545    --If price uom is null then fetch the uom based on effective dates
10546    --IF (covlvl_rec.price_uom IS NULL) THEN
10547      OKC_TIME_UTIL_PUB.Get_Duration(covlvl_rec.start_date,l_line_end_date,l_duration,l_uom,x_return_status);
10548      IF x_return_status <> 'S' THEN
10549        RAISE G_EXCEPTION_HALT_VALIDATION;
10550      END IF;
10551   -- END IF;
10552   --mchoudha Added for partial periods CR-003
10553    Open om_period_csr(covlvl_rec.id);
10554    Fetch om_period_csr into l_service_period,l_service_duration,l_ordered_quantity;
10555    Close om_period_csr;
10556 
10557    IF l_service_period IS NULL THEN
10558      --IF covlvl_rec.Unit_Price IS NULL THEN
10559      --  l_unit_price := covlvl_rec.total_amount/l_duration;
10560      --ELSE
10561     --mchoudha for bug#4961636
10562     --The unit price will be directly picked from the DB for the product/item
10563       IF covlvl_rec.lse_id in (7,9,25) THEN
10564         --commenting this for bug#5306921
10565         --The conversion from pricelist uom to price uom is not required as per bug#4686993
10566         /*IF covlvl_rec.toplvl_uom_code <> nvl(covlvl_rec.price_uom,l_uom) THEN
10567           l_unit_price := OKS_BILL_SCH.Get_Converted_price(
10568                                             p_price_uom        =>nvl(covlvl_rec.price_uom,l_uom),
10569                                             p_pl_uom           =>covlvl_rec.toplvl_uom_code ,
10570                                             p_period_start     =>p_period_start,
10571                                             p_period_type      =>p_period_type,
10572                                             p_price_negotiated =>covlvl_rec.total_amount,
10573                                             p_unit_price       =>covlvl_rec.unit_price,
10574                                             p_start_date       =>covlvl_rec.start_date,
10575                                             p_end_date         =>l_line_end_date
10576                                             );
10577           IF l_unit_price is NULL THEN
10578             RAISE G_EXCEPTION_HALT_VALIDATION;
10579           END IF;
10580         ELSE*/
10581         --Fix for bug#5623498 unit price will be recalculated based on
10582 	--duration based uom if pricelist is not present
10583         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10584                        (p_start_date   => covlvl_rec.start_date,
10585                         p_end_date     => l_line_end_date,
10586                         p_source_uom   => nvl(covlvl_rec.toplvl_uom_code,l_uom),
10587                         p_period_type  => p_period_type,
10588                         p_period_start => p_period_start);
10589         l_unit_price := covlvl_rec.total_amount / l_quantity;
10590 
10591         --END IF;
10592           --determine the termination duration  quantity based on price list uom for bug#5306921
10593           l_quantity:=OKS_TIME_MEASURES_PUB.get_quantity(p_start_date   => l_termination_date,
10594                                                          p_end_date     => l_line_end_date,
10595                                                          p_source_uom   => nvl(covlvl_rec.toplvl_uom_code,l_uom),
10596                                                          p_period_type  => p_period_type,
10597                                                          p_period_start => p_period_start);
10598 
10599       ELSE  --for manual covered levels added for bug#4961636
10600          l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10601                        (p_start_date   => covlvl_rec.start_date,
10602                         p_end_date     => l_line_end_date,
10603                         p_source_uom   => l_uom,
10604                         p_period_type  => p_period_type,
10605                         p_period_start => p_period_start);
10606 
10607         l_unit_price := covlvl_rec.total_amount/l_quantity;
10608         --determine the termination duration  quantity based on effectivity uom for bug#5306921
10609         l_quantity:=OKS_TIME_MEASURES_PUB.get_quantity(p_start_date   => l_termination_date,
10610                                                        p_end_date     => l_line_end_date,
10611                                                        p_source_uom   => l_uom,
10612                                                        p_period_type  => p_period_type,
10613                                                        p_period_start => p_period_start);
10614 
10615       END IF;
10616 
10617     --END IF;
10618 
10619    ELSE  --OM iStore  Case
10620         --The conversion from pricelist uom to price uom is not required as per bug#4686993
10621         --So deriving the quantity based on price list uom
10622         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10623                        (p_start_date   => covlvl_rec.start_date,
10624                         p_end_date     => l_line_end_date,
10625                         p_source_uom   => nvl(covlvl_rec.toplvl_uom_code,l_uom),
10626                         p_period_type  => p_period_type,
10627                         p_period_start => p_period_start);
10628         l_unit_price := covlvl_rec.total_amount / l_quantity;
10629         --determine the termination duration  quantity based on price list uom for bug#5306921
10630         l_quantity:=OKS_TIME_MEASURES_PUB.get_quantity(p_start_date   => l_termination_date,
10631                                                          p_end_date     => l_line_end_date,
10632                                                          p_source_uom   => nvl(covlvl_rec.toplvl_uom_code,l_uom),
10633                                                          p_period_type  => p_period_type,
10634                                                          p_period_start => p_period_start);
10635 
10636    END IF;
10637 
10638 
10639 
10640      IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10641       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.cov_line',
10642      'After calling OKS_TIME_MEASURES_PUB.get_quantity with p_source_uom '|| nvl(covlvl_rec.price_uom,l_uom)||' p_period_start ' ||p_period_start||' ,p_period_type '||p_period_type
10643      ||'result l_quantity '||l_quantity);
10644      END IF;
10645 
10646    IF nvl(l_quantity,0) = 0 THEN
10647     RAISE G_EXCEPTION_HALT_VALIDATION;
10648    END IF;
10649    l_termination_amount := l_quantity * l_unit_price;
10650    --Added for CR003
10651    IF l_termination_amount > covlvl_rec.total_amount and covlvl_rec.total_amount >0 THEN   -----Added the condition covlvl_rec.total_amount for bug 11773025
10652      l_termination_amount :=  covlvl_rec.total_amount;
10653    END IF;
10654 
10655    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10656       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.cov_line',
10657      'l_termination_amount '||l_termination_amount);
10658    END IF;
10659 
10660    --Get the total billed amount and max date Billed to and max date billed from of the
10661    --subline
10662   OPEN total_billed_csr(covlvl_rec.id);
10663   FETCH total_billed_csr into l_billed_amount,
10664                               l_max_date_billed_from,
10665                               l_max_date_billed_to;
10666   CLOSE total_billed_csr;
10667 
10668    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10669       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.cov_line',
10670      'l_billed_Amount '||l_billed_Amount||', total amount = '||covlvl_rec.total_Amount);
10671    END IF;
10672 
10673   l_unbilled_amount := covlvl_rec.total_Amount - l_billed_Amount;
10674 
10675   IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10676       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.cov_line',
10677      'l_unbilled_amount '||l_unbilled_amount);
10678    END IF;
10679 
10680 /*Modified the condi for bug 11773025*/
10681 -----Amounts calculated when click on review button are incorrect incase of negative amount lines..So modified the cond for bug 11773025
10682   IF covlvl_rec.total_amount< 0 THEN
10683      -- Modified for Bug#13618980
10684      IF (l_unbilled_amount > l_termination_amount AND l_max_date_billed_to >= l_termination_date) THEN
10685         x_amount := l_termination_amount - l_unbilled_amount;
10686      END IF;
10687      -- Modified for Bug#13618980
10688   ELSIF (l_termination_amount > l_unbilled_amount  AND l_max_date_billed_to >= l_termination_date) THEN
10689      x_amount := l_termination_amount - l_unbilled_amount;
10690   END IF;
10691 /*Modified the condi for bug 11773025*/
10692 
10693   IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10694       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.cov_line',
10695      'x_amount '||x_amount);
10696   END IF;
10697   END IF; --l_line_end_date >= l_termination_date
10698 
10699 ELSE  -- Check for p_cov_line = 'Y'
10700    x_amount:= 0;
10701 
10702    FOR cur in  line_csr(p_line_id) LOOP
10703 
10704      IF p_termination_date < cur.start_date THEN
10705        l_termination_date := cur.start_date;
10706      ELSE
10707        l_termination_date :=  p_termination_date;
10708      END IF;
10709      Open max_billto_date_csr(cur.id);
10710      Fetch max_billto_date_csr into l_line_end_date;
10711      Close max_billto_date_csr;
10712      IF (l_line_end_date >= l_termination_date) THEN
10713 
10714      --If price uom is null then fetch the uom based on effective dates
10715     -- IF (cur.price_uom IS NULL) THEN
10716        OKC_TIME_UTIL_PUB.Get_Duration(cur.start_date,l_line_end_date,l_duration,l_uom,x_return_status);
10717 
10718        IF x_return_status <> 'S' THEN
10719          RAISE G_EXCEPTION_HALT_VALIDATION;
10720        END IF;
10721 
10722     -- END IF;
10723 
10724    --mchoudha Added for partial periods CR-003
10725    Open om_period_csr(cur.id);
10726    Fetch om_period_csr into l_service_period,l_service_duration,l_ordered_quantity;
10727    Close om_period_csr;
10728 
10729    IF  l_service_period IS NULL THEN
10730 
10731      --IF cur.Unit_Price IS NULL THEN
10732      --  l_unit_price := cur.total_amount/l_duration;
10733      --ELSE
10734      --mchoudha for bug#4961636
10735      --The unit price will be directly picked from the DB for the product/item
10736      IF cur.lse_id in (7,9,25) THEN
10737        --commenting this for bug#5306921
10738        --The conversion from pricelist uom to price uom is not required as per bug#4686993
10739       /*IF cur.toplvl_uom_code <> nvl(cur.price_uom,l_uom) THEN
10740         l_unit_price := OKS_BILL_SCH.Get_Converted_price(
10741                                             p_price_uom        =>nvl(cur.price_uom,l_uom),
10742                                             p_pl_uom           =>cur.toplvl_uom_code ,
10743                                             p_period_start     =>p_period_start,
10744                                             p_period_type      =>p_period_type,
10745                                             p_price_negotiated =>cur.total_amount,
10746                                             p_unit_price       =>cur.unit_price,
10747                                             p_start_date       =>cur.start_date,
10748                                             p_end_date         =>l_line_end_date
10749                                             );
10750         IF l_unit_price is NULL THEN
10751           RAISE G_EXCEPTION_HALT_VALIDATION;
10752         END IF;
10753       ELSE*/
10754         --Fix for bug#5623498 unit price will be recalculated based on duration based uom
10755 	--if pricelist is not present
10756         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10757                        (p_start_date   => cur.start_date,
10758                         p_end_date     => l_line_end_date,
10759                         p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10760                         p_period_type  => p_period_type,
10761                         p_period_start => p_period_start);
10762         l_unit_price := cur.total_amount / l_quantity;
10763 
10764       --END IF;
10765         --determine the termination duration  quantity based on price list uom
10766         l_quantity:= OKS_TIME_MEASURES_PUB.get_quantity(
10767                                           p_start_date   => l_termination_date,
10768                                           p_end_date     => l_line_end_date,
10769                                           p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10770                                           p_period_type  => p_period_type,
10771                                           p_period_start => p_period_start);
10772 
10773      ELSE  --for manual covered levels added for bug#4961636
10774         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10775                        (p_start_date   => cur.start_date,
10776                         p_end_date     => l_line_end_date,
10777                         p_source_uom   => l_uom,
10778                         p_period_type  => p_period_type,
10779                         p_period_start => p_period_start);
10780 
10781         l_unit_price := cur.total_amount/l_quantity;
10782         --determine the termination duration  quantity based on effectivity uom
10783         l_quantity:= OKS_TIME_MEASURES_PUB.get_quantity(
10784                                           p_start_date   => l_termination_date,
10785                                           p_end_date     => l_line_end_date,
10786                                           p_source_uom   => l_uom,
10787                                           p_period_type  => p_period_type,
10788                                           p_period_start => p_period_start);
10789 
10790      END IF;
10791     --END IF;
10792    ELSE  --OM iStore  Case
10793         l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
10794                        (p_start_date   => cur.start_date,
10795                         p_end_date     => l_line_end_date,
10796                         p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10797                         p_period_type  => p_period_type,
10798                         p_period_start => p_period_start);
10799         l_unit_price := cur.total_amount / l_quantity;
10800         --determine the termination duration  quantity based on price list uom
10801         l_quantity:= OKS_TIME_MEASURES_PUB.get_quantity(
10802                                           p_start_date   => l_termination_date,
10803                                           p_end_date     => l_line_end_date,
10804                                           p_source_uom   => nvl(cur.toplvl_uom_code,l_uom),
10805                                           p_period_type  => p_period_type,
10806                                           p_period_start => p_period_start);
10807 
10808    END IF;
10809 
10810 
10811      IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10812         fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.line',
10813         'After calling OKS_TIME_MEASURES_PUB.get_quantity with p_source_uom '||nvl(cur.price_uom,l_uom)||' p_period_start ' ||p_period_start||' ,p_period_type '||p_period_type
10814         ||'result l_quantity '||l_quantity);
10815      END IF;
10816 
10817 
10818      IF nvl(l_quantity,0) = 0 THEN
10819        RAISE G_EXCEPTION_HALT_VALIDATION;
10820      END IF;
10821      l_termination_amount := l_quantity * l_unit_price;
10822 
10823      --Added for CR003
10824      IF l_termination_amount > cur.total_amount and cur.total_amount>0 THEN   ---Added condition cur.total_amount for bug 11773025
10825        l_termination_amount :=  cur.total_amount;
10826      END IF;
10827 
10828 
10829      IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10830       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.line',
10831      'l_termination_amount '||l_termination_amount);
10832      END IF;
10833      --Get the total billed amount and max date Billed to and max date billed from of the
10834      --subline
10835      OPEN  total_billed_csr(cur.id);
10836      FETCH total_billed_csr into  l_billed_amount,
10837                                    l_max_date_billed_from,
10838                                    l_max_date_billed_to;
10839      CLOSE total_billed_csr;
10840 
10841      l_unbilled_amount := cur.total_Amount - nvl(l_billed_Amount,0);
10842 
10843 /*Modified the condition for bug 11773025*/
10844 -----Amounts calculated when click on review button are incorrect incase of negative amount lines..So modified the cond for bug 11773025
10845   l_credit_amount :=  0;  --Added for Bug#14588465 to re-initialize l_credit_amount variable
10846   IF cur.total_amount< 0 THEN
10847     -- Modified IF clause for Bug#13618980
10848      IF (l_unbilled_amount > l_termination_amount AND l_max_date_billed_to >= l_termination_date) THEN
10849       l_credit_amount := l_termination_amount - l_unbilled_amount;
10850      END IF;
10851     -- Modified IF clause for Bug#13618980
10852   ELSIF (l_termination_amount >= l_unbilled_amount AND l_max_date_billed_to >= l_termination_date) THEN     ---modifed condition for bug 12940223
10853     l_credit_amount := l_termination_amount - l_unbilled_amount;
10854   END IF;
10855 /*Modified the condition for bug 11773025*/
10856 
10857 
10858      IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10859       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.line',
10860      'l_credit_amount '||l_credit_amount);
10861      End IF;
10862 
10863      --Sum of credit amounts for all the sublines of the topline
10864      x_amount:= nvl(x_amount,0) + nvl(l_credit_amount,0);
10865 
10866 
10867      IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10868       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.line',
10869      'x_amount '||x_amount);
10870     End If;
10871      END IF; --l_line_end_date >= l_termination_date
10872    END LOOP;
10873 
10874    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
10875       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_Term_Amt_Ppc.line',
10876      'x_amount '||x_amount);
10877    END IF;
10878 
10879  END IF;
10880 
10881 EXCEPTION
10882    WHEN G_EXCEPTION_HALT_VALIDATION THEN
10883          x_return_status := OKC_API.G_RET_STS_ERROR ;
10884    WHEN OTHERS THEN
10885         OKC_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
10886                             p_msg_name     => G_UNEXPECTED_ERROR,
10887                             p_token1       => G_SQLCODE_TOKEN,
10888                             p_token1_value => sqlcode,
10889                             p_token2       => G_SQLERRM_TOKEN,
10890                             p_token2_value => sqlerrm);
10891 
10892 END get_term_amt_ppc;
10893 
10894 ------------------------------------------------------------------------
10895 -- Begin partial period computation logic
10896 -- Developer Mani Choudhary
10897 -- Date 27-MAY-2005
10898 -- DESCRIPTION:
10899 -- This function will be called for Usage Line Types of Fixed and to
10900 -- compute Minimum and Default for Actual per Period and Actual by Quantity.
10901 ---------------------------------------------------------------------------
10902 Function Get_Prorated_Usage_Qty
10903                        (
10904                        p_start_date  IN DATE,
10905                        p_end_date    IN DATE,
10906                        p_qty         IN NUMBER,
10907                        p_usage_uom   IN VARCHAR2,
10908                        p_billing_uom IN VARCHAR2,
10909                        p_period_type IN VARCHAR2
10910                        )
10911 RETURN NUMBER
10912 IS
10913 /*Declaration Section*/
10914 CURSOR cs_validate_uom(p_uom_code IN VARCHAR2)
10915  is
10916   SELECT 1
10917    FROM MTL_UNITS_OF_MEASURE_TL TL, okc_time_code_units_v okc
10918   WHERE TL.uom_code    = okc.uom_code
10919     AND TL.uom_code    = p_uom_code
10920     --AND TL.uom_class = 'Time'  commented for bug#5585356
10921     AND okc.active_flag = 'Y'
10922    AND TL.LANGUAGE = USERENV('LANG');
10923 
10924 
10925 cr_validate_uom  cs_validate_uom%ROWTYPE;
10926 l_prorated_qty     NUMBER;
10927 l_usage_quantity   NUMBER;
10928 l_bill_quantity    NUMBER;
10929 x_msg_count        NUMBER;
10930 x_msg_data         VARCHAR2(1000);
10931 
10932 INVALID_PERIOD_TYPE_EXCEPTION  EXCEPTION;
10933 INVALID_DATE_EXCEPTION         EXCEPTION;
10934 INVALID_UOM_EXCEPTION          EXCEPTION;
10935 G_EXCEPTION_HALT_VALIDATION    EXCEPTION;
10936 BEGIN
10937     --Begin Validation
10938     --1) Validate dates
10939     IF (p_start_date IS NULL)OR(p_end_date IS NULL)OR(p_start_date > p_end_date)
10940     THEN
10941       RAISE INVALID_DATE_EXCEPTION;
10942     END IF;
10943 
10944     --2)Validate p_usage_uom
10945     OPEN cs_validate_uom(p_usage_uom);
10946     FETCH cs_validate_uom INTO cr_validate_uom;
10947     IF cs_validate_uom%NOTFOUND
10948     THEN
10949       RAISE INVALID_UOM_EXCEPTION;
10950     END IF;
10951     CLOSE cs_validate_uom;
10952 
10953     --3)Validate p_billing_uom
10954     OPEN cs_validate_uom(p_billing_uom);
10955     FETCH cs_validate_uom INTO cr_validate_uom;
10956     IF cs_validate_uom%NOTFOUND
10957     THEN
10958       RAISE INVALID_UOM_EXCEPTION;
10959     END IF;
10960     CLOSE cs_validate_uom;
10961 
10962 
10963     --4)Validate period type
10964     IF upper(p_period_type) NOT IN ('ACTUAL','FIXED')
10965     THEN
10966       RAISE INVALID_PERIOD_TYPE_EXCEPTION;
10967     END IF;
10968 
10969     --End Validation
10970     l_prorated_qty := 0;
10971 
10972     --Fetch the quantity wrt usage uom
10973     l_usage_quantity := OKS_TIME_MEASURES_PUB.get_target_qty_service (
10974                                                         p_start_date   => p_start_date,
10975                                                         p_end_date     => p_end_date,
10976                                                         p_price_uom    => p_usage_uom,
10977                                                         p_period_type  => p_period_type,
10978                                                         p_round_dec    => 18
10979                                                         );
10980 
10981     --FND_FILE.PUT_LINE( FND_FILE.LOG,'l_usage_quantity '||l_usage_quantity);
10982     IF l_usage_quantity <= 0 THEN
10983       RAISE  G_EXCEPTION_HALT_VALIDATION;
10984     END IF;
10985 
10986     l_prorated_qty := p_qty * l_usage_quantity;
10987     --FND_FILE.PUT_LINE( FND_FILE.LOG,'l_prorated_qty '||l_prorated_qty);
10988 RETURN l_prorated_qty;
10989 
10990 EXCEPTION
10991 WHEN INVALID_PERIOD_TYPE_EXCEPTION THEN
10992       FND_FILE.PUT_LINE( FND_FILE.LOG,'Invalid Period type '||p_period_type);
10993       return NULL;
10994 WHEN INVALID_UOM_EXCEPTION THEN
10995       FND_FILE.PUT_LINE( FND_FILE.LOG,'Invalid unit of measure code');
10996       IF cs_validate_uom%ISOPEN THEN
10997         CLOSE cs_validate_uom;
10998       END IF;
10999       return NULL;
11000 WHEN INVALID_DATE_EXCEPTION THEN
11001       FND_FILE.PUT_LINE( FND_FILE.LOG,'Invalid Effective Dates');
11002       return NULL;
11003 WHEN G_EXCEPTION_HALT_VALIDATION THEN
11004       FND_MSG_PUB.Count_And_Get
11005       (
11006         p_count =>      x_msg_count,
11007         p_data  =>      x_msg_data
11008       );
11009       FND_FILE.PUT_LINE( FND_FILE.LOG,'x_msg_count '||x_msg_count||'x_msg_data:  '||x_msg_data);
11010       RETURN NULL;
11011 WHEN OTHERS THEN
11012         --set the error message and return with NULL to notify the
11013         --caller of error
11014       FND_FILE.PUT_LINE( FND_FILE.LOG,'sqlcode '||sqlcode||' with error message '||sqlerrm);
11015       RETURN NULL;
11016 END Get_Prorated_Usage_Qty;
11017 -------------------------------------------------------------------------
11018 
11019 ------------------------------------------------------------------------
11020 -- Begin partial period computation logic
11021 -- Developer Mani Choudhary
11022 -- Date 27-MAY-2005
11023 -- DESCRIPTION:
11024 -- This new procedure will calculate the partial termination amount between
11025 -- termination date and the billing period end using Time measure APIs for USAGE only.
11026 ---------------------------------------------------------------------------
11027 Function Get_partial_term_amount
11028                        (
11029                        p_start_date        IN DATE,
11030                        p_end_date          IN DATE,
11031                        P_termination_date  IN DATE,
11032                        P_amount            IN NUMBER,
11033                        P_uom               IN VARCHAR2,
11034                        P_period_start      IN VARCHAR2,
11035                        P_period_type       IN VARCHAR2
11036                        )
11037 RETURN NUMBER
11038 IS
11039 /*Declaration Section*/
11040 CURSOR cs_validate_uom(p_uom_code IN VARCHAR2)
11041  is
11042   SELECT 1
11043    FROM MTL_UNITS_OF_MEASURE_TL TL, okc_time_code_units_v okc
11044   WHERE TL.uom_code    = okc.uom_code
11045     AND TL.uom_code    = p_uom_code
11046     --AND TL.uom_class = 'Time' commented for bug#5585356
11047     AND okc.active_flag = 'Y'
11048    AND TL.LANGUAGE = USERENV('LANG');
11049 
11050 cr_validate_uom  cs_validate_uom%ROWTYPE;
11051 l_quantity         NUMBER;
11052 l_total_quantity   NUMBER;
11053 x_amount           NUMBER;
11054 
11055 INVALID_DATE_EXCEPTION         EXCEPTION;
11056 INVALID_UOM_EXCEPTION          EXCEPTION;
11057 BEGIN
11058 
11059     --Begin Validation
11060     --1) Validate dates
11061     IF (P_termination_date IS NULL)OR(P_termination_date > p_end_date)OR(P_termination_date < p_start_date)
11062     THEN
11063       RAISE INVALID_DATE_EXCEPTION;
11064     END IF;
11065 
11066     --2)Validate p_usage_uom
11067     OPEN cs_validate_uom(P_uom);
11068     FETCH cs_validate_uom INTO cr_validate_uom;
11069     IF cs_validate_uom%NOTFOUND
11070     THEN
11071       RAISE INVALID_UOM_EXCEPTION;
11072     END IF;
11073     CLOSE cs_validate_uom;
11074 
11075 
11076 
11077    --Get the partial period between p_termination_date and p_end_date
11078     l_quantity := OKS_TIME_MEASURES_PUB.get_quantity
11079                           (p_start_date   => p_termination_date,
11080                            p_end_date     => p_end_date,
11081                            p_source_uom   => P_uom,
11082                            p_period_type  => P_period_type,
11083                            p_period_start => p_period_start
11084                            );
11085 
11086    IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
11087       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_partial_term_amount',
11088      'After calling OKS_TIME_MEASURES_PUB.get_quantity with p_source_uom'||P_uom||' p_period_start ' ||p_period_start||' ,p_period_type '||p_period_type||' P_termination_date '||to_char(P_termination_date)
11089      ||'result l_quantity '||l_quantity);
11090    END IF;
11091 
11092     IF nvl(l_quantity,0) = 0 THEN
11093       RAISE G_EXCEPTION_HALT_VALIDATION;
11094     END IF;
11095 
11096     l_total_quantity := OKS_TIME_MEASURES_PUB.get_quantity
11097                           (p_start_date   => p_start_date,
11098                            p_end_date     => p_end_date,
11099                            p_source_uom   => P_uom,
11100                            p_period_type  => P_period_type,
11101                            p_period_start => p_period_start
11102                            );
11103 
11104     IF fnd_log.level_statement >= fnd_log.g_current_runtime_level THEN
11105       fnd_log.string(fnd_log.level_statement,G_MODULE_CURRENT||'.Get_partial_term_amount',
11106      'After calling OKS_TIME_MEASURES_PUB.get_quantity with p_source_uom'||P_uom||'  p_period_start ' ||p_period_start||' ,p_period_type '||p_period_type
11107      ||'result l_total_quantity '||l_total_quantity);
11108     END IF;
11109 
11110     IF nvl(l_total_quantity,0) = 0 THEN
11111       RAISE G_EXCEPTION_HALT_VALIDATION;
11112     END IF;
11113 
11114     x_amount := l_quantity * p_amount / l_total_quantity;
11115 
11116 
11117     RETURN x_amount;
11118 
11119 EXCEPTION
11120 WHEN G_EXCEPTION_HALT_VALIDATION THEN
11121      RETURN NULL;
11122 WHEN INVALID_UOM_EXCEPTION THEN
11123       OKC_API.SET_MESSAGE(p_app_name    => 'OKS',
11124                          p_msg_name     => 'OKS_INVD_UOM_CODE',
11125                          p_token1       => 'OKS_API_NAME',
11126                          p_token1_value => 'OKS_BILL_REC_PUB.Get_partial_term_amount',
11127                          p_token2       => 'UOM_CODE',
11128                          p_token2_value => P_uom);
11129       IF cs_validate_uom%ISOPEN THEN
11130         CLOSE cs_validate_uom;
11131       END IF;
11132       return NULL;
11133 WHEN INVALID_DATE_EXCEPTION THEN
11134       OKC_API.set_message('OKS','OKS_INVALID_START_END_DATES');
11135       return NULL;
11136 WHEN OTHERS THEN
11137         --set the error message and return with NULL to notify the
11138         --caller of error
11139         OKC_API.SET_MESSAGE(p_app_name     => G_APP_NAME,
11140                             p_msg_name     => G_UNEXPECTED_ERROR,
11141                             p_token1       => G_SQLCODE_TOKEN,
11142                             p_token1_value => sqlcode,
11143                             p_token2       => G_SQLERRM_TOKEN,
11144                             p_token2_value => sqlerrm);
11145 
11146        RETURN NULL;
11147 END Get_partial_term_amount;
11148 
11149 
11150 END OKS_BILL_REC_PUB ;
11151 
11152