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