DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKS_RENEW_CONTRACT_PVT

Source


1 PACKAGE BODY OKS_RENEW_CONTRACT_PVT AS
2 /* $Header: OKSRRENKB.pls 120.28 2011/05/20 06:24:24 vgujarat ship $*/
3 
4 ------------------------ Internal Type Declarations ---------------------------------
5 TYPE num_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
6 TYPE date_tbl_type IS TABLE OF DATE INDEX BY BINARY_INTEGER;
7 TYPE chr_tbl_type IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;
8 ------------------------ Start Internal procedures ----------------------------------
9 
10     /* Internal function, determines the end date of the renewed contract */
11 
12     FUNCTION GET_END_DATE
13     (
14      p_new_start_date IN DATE,
15      p_new_end_date IN DATE,
16      p_new_duration IN NUMBER,
17      p_new_uom_code IN MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE,
18      p_old_start_date IN DATE,
19      p_old_end_date IN DATE,
20      p_renewal_end_date IN DATE,
21      p_ren_type  IN okc_k_headers_b.renewal_type_code%TYPE,
22      x_return_status OUT NOCOPY VARCHAR2
23     )RETURN DATE
24     IS
25     l_api_name CONSTANT VARCHAR2(30) := 'GET_END_DATE';
26     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
27     l_error_text VARCHAR(512);
28 
29     l_end_date DATE;
30     l_duration NUMBER;
31     l_uom_code MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE;
32     BEGIN
33 
34         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
35             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_new_start_date='||p_new_start_date||' ,p_new_end_date='||p_new_end_date||' ,p_new_duration='||p_new_duration||' ,p_new_uom_code='||p_new_uom_code||
36             ' , p_old_start_date='||p_old_start_date||' ,p_old_end_date='||p_old_end_date||' ,p_renewal_end_date='||p_renewal_end_date||' ,p_ren_type='||p_ren_type);
37         END IF;
38         x_return_status := FND_API.G_RET_STS_SUCCESS;
39 
40         --trivial case, end date is given
41         l_end_date := trunc(p_new_end_date);
42 
43         --if end date is null determine it from duration/period
44         IF (l_end_date IS NULL) THEN
45 
46             l_duration := p_new_duration;
47             l_uom_code := p_new_uom_code;
48             --if duration/period are also null, use the old contract's duration period
49             IF(l_duration IS NULL OR l_uom_code IS NULL) THEN
50 
51                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
52                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_duration', 'before call to   OKC_TIME_UTIL_PUB.get_duration p_start_date='||to_char(p_old_start_date)||' ,p_end_date='||to_char(p_old_end_date));
53                 END IF;
54 
55                 OKC_TIME_UTIL_PUB.get_duration(
56                     p_start_date => trunc(p_old_start_date),
57                     p_end_date => trunc(p_old_end_date),
58                     x_duration => l_duration,
59                     x_timeunit => l_uom_code,
60                     x_return_status => x_return_status);
61 
62                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
63                     RAISE FND_API.g_exc_unexpected_error;
64                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
65                     RAISE FND_API.g_exc_error;
66                 END IF;
67 
68                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
69                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_duration', 'after call to   OKC_TIME_UTIL_PUB.get_duration, l_duration='||l_duration||' ,l_uom_code='||l_uom_code);
70                 END IF;
71             END IF; --of (l_duration IS NULL OR l_uom_code IS NULL) THEN
72 
73             --now determine the end date from duration/period
74             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
75                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.derive_end_date', 'before call to   OKC_TIME_UTIL_PUB.get_enddate, p_start_date='||to_char(p_new_start_date)||' ,p_timeunit='||l_uom_code||' ,p_duration='||l_duration);
76             END IF;
77 
78             l_end_date := OKC_TIME_UTIL_PUB.get_enddate(
79                                 p_start_date => trunc(p_new_start_date),
80                                 p_timeunit => l_uom_code,
81                                 p_duration => l_duration);
82 
83             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
84                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.derive_end_date', 'after call to   OKC_TIME_UTIL_PUB.get_enddate, l_end_date='||to_char(l_end_date));
85             END IF;
86 
87         END IF; --of IF (l_end_date IS NULL) THEN
88 
89         --Truncate the end date, if EVN and ultimate end date is specified and end date is
90         --greater than ultimate end date
91         IF (nvl(p_ren_type, 'X') = 'EVN') AND (p_renewal_end_date IS NOT NULL) THEN
92 
93             --first check that the ultimate end date should not be before the start date
94             IF(trunc(p_renewal_end_date) < p_new_start_date) THEN
95 
96                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NEW_START_MORE_FINAL_END');
97                 FND_MESSAGE.set_token('START_DATE', to_char(trunc(p_new_start_date), 'DD-MON-YYYY'));
98                 FND_MESSAGE.set_token('REN_UP_TO_DATE', to_char(trunc(p_renewal_end_date), 'DD-MON-YYYY'));
99 
100                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
101                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.derive_end_date', FALSE);
102                 END IF;
103                 FND_MSG_PUB.ADD;
104                 RAISE FND_API.g_exc_error;
105 
106             END IF;
107 
108             --now truncate end date to ultimate end date if required
109             IF (trunc(l_end_date) > trunc(p_renewal_end_date)) THEN
110                 l_end_date := trunc(p_renewal_end_date);
111 
112                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
113                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.truncate', 'truncated end date, l_end_date='||to_char(l_end_date));
114                 END IF;
115             END IF;
116 
117         END IF;
118 
119         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
120             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', '  l_end_date='||to_char(l_end_date));
121         END IF;
122 
123         RETURN l_end_date;
124     EXCEPTION
125         WHEN FND_API.g_exc_error THEN
126             x_return_status := FND_API.g_ret_sts_error;
127             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
128                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
129             END IF;
130             RAISE;
131 
132         WHEN FND_API.g_exc_unexpected_error THEN
133             x_return_status := FND_API.g_ret_sts_unexp_error ;
134             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
135                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
136             END IF;
137             RAISE;
138 
139         WHEN OTHERS THEN
140             x_return_status := FND_API.g_ret_sts_unexp_error ;
141             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
142                 --first log the sqlerrm
143                 l_error_text := substr (SQLERRM, 1, 240);
144                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
145                 --then add it to the message api list
146                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
147             END IF;
148             RAISE;
149 
150     END GET_END_DATE;
151 
152 
153     /* 	Internal Procedure
154     This is a new module that will be called for all service contracts after copying the
155     old contract. It will update the new contract header and lines start dates and end dates
156     based on renewal type for the line (Full Duration, Keep Duration, Do not renew). It uses
157     bulk operations to maximize performance. Since only start_date and end_date are updated in OKC_K_LINES_B and OKC_K_HEADERS_B, TAPI is ignored. It replaces the existing OKC procedure OKC_RENEW_PVT.update_renewal_dates.
158 
159     Parameters
160         p_chr_id                :   id of the renewed contract
161         p_new_start_date        :   header start date for the renewed contract
162         p_new_end_date          :   header end date for the renewed contract
163         p_old_end_date          :   header start date for the source contract
164     */
165 
166     PROCEDURE UPDATE_RENEWAL_DATES
167     (
168      p_chr_id  IN NUMBER,
169      p_new_start_date IN DATE,
170      p_new_end_date IN DATE,
171      p_old_start_date IN DATE,
172      x_msg_count OUT NOCOPY NUMBER,
173      x_msg_data OUT NOCOPY VARCHAR2,
174      x_return_status OUT NOCOPY VARCHAR2
175     )
176     IS
177 	TYPE line_rec IS RECORD
178 		(id			    NUMBER,
179 		cle_id			NUMBER,
180 		lrt			    okc_k_lines_b.line_renewal_type_code%TYPE,
181 		old_start_date	DATE,
182 		old_end_date	DATE,
183 		new_start_date	DATE,
184 		new_end_date	DATE);
185 
186 	TYPE line_rec_tbl_type IS TABLE OF line_rec INDEX BY BINARY_INTEGER;
187 
188     l_api_name CONSTANT VARCHAR2(30) := 'UPDATE_RENEWAL_DATES';
189     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
190     l_error_text VARCHAR2(512);
191 
192     l_dummy			        NUMBER;
193     l_lines_tbl		        line_rec_tbl_type;
194 	l_cached_tbl		    line_rec_tbl_type;
195 	l_id_tbl		        num_tbl_type;
196 
197 	l_start_date_tbl	    date_tbl_type;
198 	l_end_date_tbl		    date_tbl_type;
199 
200 	l_parent_new_start_date	DATE;
201 	l_parent_new_end_date	DATE;
202 	l_parent_old_start_date	DATE;
203 	l_parent_rec		    line_rec;
204 
205 	l_additional_days	    NUMBER;
206 	l_duration		        NUMBER;
207 
208 
209 
210 	cursor c_kep_lines(cp_chr_id IN NUMBER) IS
211         SELECT id from okc_k_lines_b
212         where dnz_chr_id = cp_chr_id and nvl(line_renewal_type_code,'X') = 'KEP';
213 
214     cursor c_lines(cp_chr_id in number) is
215 		select id, cle_id, line_renewal_type_code, start_date, end_date, null, null
216 		from okc_k_lines_b
217 		start with (dnz_chr_id = cp_chr_id and cle_id is null)
218 		connect by prior id = cle_id;
219 
220 
221 	-- this local function gets the parent line information
222 	FUNCTION GET_PARENT_REC(p_cle_id in number) return line_rec
223 	is
224 	l_rec		line_rec := null;
225 	BEGIN
226 
227         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
228             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.get_parent_rec.begin', 'p_cle_id='||p_cle_id);
229         END IF;
230         x_return_status := FND_API.G_RET_STS_SUCCESS;
231 
232         -- first check the l_cached_tbl as it is going to be smaller or null
233         IF (l_cached_tbl.count > 0) THEN
234             for i in l_cached_tbl.first..l_cached_tbl.last loop
235                 if (l_cached_tbl(i).id = p_cle_id) then
236                     l_rec := l_cached_tbl(i);
237                 end if;
238             end loop;
239         END IF;
240 
241 		--now check the current lines_tbl
242         IF (l_lines_tbl.count > 0) THEN
243             for i in l_lines_tbl.first..l_lines_tbl.last loop
244                 if (l_lines_tbl(i).id = p_cle_id) then
245                     l_rec := l_lines_tbl(i);
246                 end if;
247             end loop;
248         END IF;
249 
250         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
251             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.get_parent_rec.end', 'l_rec.id='||l_rec.id||' ,l_rec.lrt='||l_rec.lrt);
252         END IF;
253 
254 		RETURN l_rec;
255 	END GET_PARENT_REC;
256 
257 	--this local procedure populates all the parent lines of a given
258 	--line in l_cached_tbl. l_cached_tbl is used to preserve
259 	--parent line info across successive bulk fetches. It is called after
260     --every bulk fecth cycle
261 	PROCEDURE POPULATE_CACHE(p_line_rec in line_rec)
262 	IS
263 	l_current_rec	    line_rec;
264     l_cached_tbl_tmp    line_rec_tbl_type;
265 	n		            number;
266 	BEGIN
267 
268         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
269             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.populate_cache.begin', 'p_line_rec.id='||p_line_rec.id||' ,p_line_rec.cle_id='||p_line_rec.cle_id);
270         END IF;
271         x_return_status := FND_API.G_RET_STS_SUCCESS;
272 
273 		l_current_rec := p_line_rec;
274         --we will first populate a temp cache
275         l_cached_tbl_tmp.delete;
276 		l_cached_tbl_tmp(1) := l_current_rec;
277 
278 		loop
279 			if (l_current_rec.cle_id is null) then
280                 --this is a top line, so no more parents
281 				exit;
282 			else
283                 --get the parent rec from l_cached_tbl/l_lines_tbl
284 				l_current_rec := get_parent_rec(l_current_rec.cle_id);
285 				if (l_current_rec.id is not null) then
286 					--add it to the bottom of l_cached_tbl_tmp
287                     n := l_cached_tbl_tmp.count;
288 					l_cached_tbl_tmp(n+1) := l_current_rec;
289 				end if;
290 			end if;
291 		end loop;
292 
293         --now use the temp cache as the real cache for the next bulk fetch cycle
294         l_cached_tbl := l_cached_tbl_tmp;
295 	    l_cached_tbl_tmp.delete;
296 
297         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
298             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.populate_cache.end', 'l_cached_tbl.count='||l_cached_tbl.count);
299         END IF;
300 
301 	end POPULATE_CACHE;
302 
303     --this local procedure deletes any DNR top lines and child lines and any associated entities
304     --this is only necessary till the new copy API begins to filter out DNR lines for renewal
305     --commented this procedure as the new copy API is taking care of this
306     /*
307     PROCEDURE DELETE_DNR_LINES
308     IS
309 
310 	cursor c_dnr_lines(cp_chr_id in number) is
311 		select id from okc_k_lines_b
312 		where dnz_chr_id = cp_chr_id and nvl(line_renewal_type_code,'X') = 'DNR';
313 
314     l_dnr_lines_tbl		    num_tbl_type;
315 
316     BEGIN
317 
318         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
319             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.delete_dnr_lines.begin', 'p_chr_id='||p_chr_id);
320         END IF;
321         x_return_status := FND_API.G_RET_STS_SUCCESS;
322 
323         open c_dnr_lines(p_chr_id);
324         loop
325             fetch c_dnr_lines bulk collect into l_dnr_lines_tbl limit G_BULK_FETCH_LIMIT;
326 
327             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
328                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.delete_dnr_lines.bulk_fetch', 'l_dnr_lines_tbl.count='||l_dnr_lines_tbl.count);
329             END IF;
330 
331             exit when (l_dnr_lines_tbl.count = 0);
332 
333             for i in l_dnr_lines_tbl.first..l_dnr_lines_tbl.last loop
334                 -- this will delete all associated okc/oks lines
335                 -- and other entiries such as billing schedule, coverages etc
336                 -- OKC code was not doing this
337 
338                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
339                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.delete_dnr_lines.delete_line', 'calling delete_contract_line, p_line_id='||l_dnr_lines_tbl(i)||' x_return_status='||x_return_status);
340                 END IF;
341                 delete_contract_line(
342                         p_api_version => 1,
343                         p_init_msg_list => FND_API.G_FALSE,
344                         p_commit => FND_API.G_FALSE,
345                         p_line_id => l_dnr_lines_tbl(i),
346                         x_return_status => x_return_status,
347                         x_msg_count => x_msg_count,
348                         x_msg_data => x_msg_data);
349 
350                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
351                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.delete_dnr_lines.delete_line', 'after call to delete_contract_line, x_return_status='||x_return_status);
352                 END IF;
353 
354                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
355                     RAISE FND_API.g_exc_unexpected_error;
356                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
357                     RAISE FND_API.g_exc_error;
358                 END IF;
359 
360             END LOOP;
361 
362         END LOOP;
363         CLOSE c_dnr_lines;
364         l_dnr_lines_tbl.delete;
365 
366         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
367             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.delete_dnr_lines.end', 'x_return_status='||x_return_status);
368         END IF;
369 
370     EXCEPTION
371         WHEN FND_API.g_exc_error THEN
372             x_return_status := FND_API.g_ret_sts_error;
373             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
374                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.delete_dnr_lines.end_error', 'x_return_status=' || x_return_status);
375             END IF;
376             RAISE;
377 
378         WHEN FND_API.g_exc_unexpected_error THEN
379             x_return_status := FND_API.g_ret_sts_unexp_error ;
380             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
381                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.delete_dnr_lines.end_unexpected_error', 'x_return_status=' || x_return_status);
382             END IF;
383             RAISE;
384 
385         WHEN OTHERS THEN
386             x_return_status := FND_API.g_ret_sts_unexp_error ;
387             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
388                 --first log the sqlerrm
389                 l_error_text := substr (SQLERRM, 1, 240);
390                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.delete_dnr_lines.end_other_error', l_error_text);
391                 --then add it to the message api list
392                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
393             END IF;
394             IF (c_dnr_lines%isopen) THEN
395                 CLOSE c_dnr_lines;
396             END IF;
397             RAISE;
398 
399     END DELETE_DNR_LINES;
400     */
401 
402     BEGIN
403         --main update procedure begins here
404         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
405             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id='||p_chr_id||' ,p_new_start_date='||p_new_start_date||' ,p_new_end_date='||p_new_end_date||'  ,p_old_start_date='||p_old_start_date);
406         END IF;
407         x_return_status := FND_API.G_RET_STS_SUCCESS;
408 
409         --becuase of bug 2689096, we need to capture the time component also for each
410         --start date and end date
411         --If time component is nulled out, duration can be different and this  will affect pricing
412 
413         --first update contract header
414         update okc_k_headers_all_b SET
415             start_date = to_date(to_char(p_new_start_date, 'DD/MM/YYYY')|| to_char(start_date,'HH24:MI:SS'), 'DD/MM/YYYYHH24:MI:SS'),
416             end_date = to_date(to_char(p_new_end_date, 'DD/MM/YYYY')|| to_char(end_date,'HH24:MI:SS'), 'DD/MM/YYYYHH24:MI:SS')
417             WHERE id = p_chr_id;
418 
419         --delete DNR lines
420         --commented this call as the new copy API is taking care of this
421         --delete_dnr_lines;
422 
423 
424         --now check if there are any Keep Duraton lines, if there are no Keep Duration lines
425         --a simple update will do
426         open c_kep_lines(p_chr_id);
427         fetch c_kep_lines into l_dummy;
428         close c_kep_lines;
429 
430         --no keep duration lines
431         IF (l_dummy IS NULL) THEN
432             UPDATE okc_k_lines_b SET
433             start_date = to_date(to_char(p_new_start_date, 'DD/MM/YYYY')|| to_char(start_date,'HH24:MI:SS'), 'DD/MM/YYYYHH24:MI:SS'),
434             end_date = to_date(to_char(p_new_end_date, 'DD/MM/YYYY')|| to_char(end_date,'HH24:MI:SS'), 'DD/MM/YYYYHH24:MI:SS')
435             WHERE dnz_chr_id = p_chr_id;
436 
437             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
438                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.check_keep', 'end  no keep duration lines, done with date updates ,x_return_status='||x_return_status);
439             END IF;
440             RETURN;
441 
442         END IF;
443 
444 
445         --we come to this step only if there are some keep duration lines
446         OPEN c_lines(p_chr_id);
447         LOOP
448             --fetch okc lines in heirarchial order
449             FETCH c_lines BULK COLLECT INTO l_lines_tbl LIMIT G_BULK_FETCH_LIMIT;
450 
451             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
452                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.c_lines_bulk_fetch', 'l_lines_tbl.count='||l_lines_tbl.count);
453             END IF;
454 
455             EXIT WHEN (l_lines_tbl.count = 0);
456 
457             for i in l_lines_tbl.first..l_lines_tbl.last loop
458 
459                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
460                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_begin', 'i='||i||' ,l_lines_tbl(i).cle_id='||l_lines_tbl(i).cle_id);
461                 END IF;
462 
463                 --determine the parent lrt and parent dates
464                 --for top lines
465                 if l_lines_tbl(i).cle_id is null then
466                     --always set the current line's lrt
467                     l_lines_tbl(i).lrt := nvl(l_lines_tbl(i).lrt, 'FUL');
468                     -- for toplines the parent is the contract header
469                     l_parent_new_start_date := trunc(p_new_start_date);
470                     l_parent_new_end_date := trunc(p_new_end_date);
471                     l_parent_old_start_date := trunc(p_old_start_date);
472                 -- for other lines
473                 else
474                     l_parent_rec := get_parent_rec(l_lines_tbl(i).cle_id);
475                     --always set the current line's lrt
476                     l_lines_tbl(i).lrt := nvl(l_lines_tbl(i).lrt, l_parent_rec.lrt);
477                     l_parent_new_start_date := trunc(l_parent_rec.new_start_date);
478                     l_parent_new_end_date := trunc(l_parent_rec.new_end_date);
479                     l_parent_old_start_date := trunc(l_parent_rec.old_start_date);
480                 end if; --of if lines_tbl(i).cle_id is null then
481 
482 
483                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
484                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_parent', 'i='||i||' ,l_parent_new_start_date='||l_parent_new_start_date||' ,l_parent_new_end_date='||l_parent_new_end_date);
485                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_parent',' l_parent_old_start_date='||l_parent_old_start_date||' ,l_lines_tbl(i).lrt='||l_lines_tbl(i).lrt);
486                 END IF;
487 
488                 --determine the new dates based on renewal type
489                 if l_lines_tbl(i).lrt = 'FUL' then
490                     -- line dates are the same as parent dates
491                     l_lines_tbl(i).new_start_date := trunc(l_parent_new_start_date);
492                     l_lines_tbl(i).new_end_date := trunc(l_parent_new_end_date);
493 
494                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
495                         FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_FUL', 'i='||i||' ,l_lines_tbl(i).new_start_date='||l_lines_tbl(i).new_start_date||' ,l_lines_tbl(i).new_end_date='||l_lines_tbl(i).new_end_date);
496                     END IF;
497 
498                 elsif l_lines_tbl(i).lrt = 'KEP' then
499 
500                     --get the original start date offset
501                     l_duration := 0;
502                     l_additional_days := 0;
503 
504                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
505                         FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_KEP_1', 'i='||i||' calling OKC_TIME_UTIL_PVT.get_oracle_months_and_days, p_start_date='||l_parent_old_start_date||' ,p_end_date='||l_lines_tbl(i).old_start_date);
506                     END IF;
507 
508                     OKC_TIME_UTIL_PVT.get_oracle_months_and_days(
509                         p_start_date => trunc(l_parent_old_start_date),
510                         p_end_date => trunc(l_lines_tbl(i).old_start_date),
511                         x_month_duration => l_duration,
512                         x_day_duration => l_additional_days,
513                         x_return_status => x_return_status);
514 
515                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
516                         FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_KEP_1', 'i='||i||',after OKC_TIME_UTIL_PVT.get_oracle_months_and_days, x_return_status='||x_return_status||
517                         ',x_mth_duration='||l_duration||',x_day_duration='||l_additional_days);
518                     END IF;
519 
520                     IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
521                         RAISE FND_API.g_exc_unexpected_error;
522                     ELSIF x_return_status = FND_API.g_ret_sts_error THEN
523                         RAISE FND_API.g_exc_error;
524                     END IF;
525 
526                     -- add the offset to parent start date
527                     l_lines_tbl(i).new_start_date := ADD_MONTHS(
528                         trunc(l_parent_new_start_date), l_duration) + l_additional_days;
529 
530                     if(trunc(l_lines_tbl(i).new_start_date) <= trunc(l_parent_new_end_date)) then
531 
532                         --get the original end date offset
533                         l_duration := 0;
534                         l_additional_days := 0;
535 
536                         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
537                             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_KEP_2', 'i='||i||' calling OKC_TIME_UTIL_PVT.get_oracle_months_and_days, p_start_date='||l_parent_old_start_date||
538                             ' ,p_end_date='||l_lines_tbl(i).old_end_date||' ,x_return_status='||x_return_status);
539                         END IF;
540 
541                         OKC_TIME_UTIL_PVT.get_oracle_months_and_days(
542                             p_start_date => trunc(l_parent_old_start_date),
543                             p_end_date => trunc(l_lines_tbl(i).old_end_date),
544                             x_month_duration => l_duration,
545                             x_day_duration => l_additional_days,
546                             x_return_status => x_return_status);
547 
548                         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
549                             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_KEP_3', 'i='||i||'after OKC_TIME_UTIL_PVTget_oracle_months_and_days, x_return_status='||x_return_status||
550                             ',x_mth_duration='||l_duration||',x_day_duration='||l_additional_days);
551                         END IF;
552 
553                         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
554                             RAISE FND_API.g_exc_unexpected_error;
555                         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
556                             RAISE FND_API.g_exc_error;
557                         END IF;
558 
559                         -- add the offset to parent start date
560                         l_lines_tbl(i).new_end_date := ADD_MONTHS(
561                             trunc(l_parent_new_start_date), l_duration) + l_additional_days;
562 
563                         --chop line end date if it is greater than parent end date
564                         if (trunc(l_lines_tbl(i).new_end_date) > trunc(l_parent_new_end_date)) then
565                             l_lines_tbl(i).new_end_date := trunc(l_parent_new_end_date);
566 
567                             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
568                                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_KEP_4', 'i='||i||' chopped line end date, l_lines_tbl(i).new_end_date='||l_lines_tbl(i).new_end_date||
569                                 ' ,l_parent_new_end_date='||l_parent_new_end_date);
570                             END IF;
571 
572                         end if;
573 
574                     --line is starting beyond the parent end date
575                     else
576                         --1 day renewal
577                         l_lines_tbl(i).new_start_date := trunc(l_parent_new_start_date);
578                         l_lines_tbl(i).new_end_date := trunc(l_parent_new_start_date);
579 
580                         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
581                             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_KEP_5', 'i='||i||', 1 day renewal, l_lines_tbl(i).new_start_date > l_parent_new_end_date l_lines_tbl(i).new_st_date='||
582                             l_lines_tbl(i).new_start_date||' ,l_parent_new_end_date='||l_parent_new_end_date);
583                         END IF;
584 
585                     end if;
586                 end if; --of elsif lines_tbl(i).lrt = 'KEP' then
587 
588                 --store in non-record pl/sql table for use in subsequent FORALL update
589                 l_id_tbl(i) := l_lines_tbl(i).id;
590                 l_start_date_tbl(i) := trunc(l_lines_tbl(i).new_start_date);
591                 l_end_date_tbl(i) := trunc(l_lines_tbl(i).new_end_date);
592 
593                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
594                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calc_dates_end', 'i='||i||' ,l_id_tbl(i)='||l_id_tbl(i)||' ,l_start_date_tbl(i)='||l_start_date_tbl(i)||' ,l_end_date_tbl(i)='||l_end_date_tbl(i));
595                 END IF;
596 
597             end loop; --of for i in l_lines_tbl.first..l_lines_tbl.last loop
598 
599 
600             --now we have determined the new start/end dates of all the lines
601             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
602                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.forall_update_stmt ', 'l_id_tbl.count='||l_id_tbl.count||' ,x_return_status='||x_return_status);
603             END IF;
604 
605             --becuase of bug 2689096, we need to capture the time component also for each
606             --start date and end date
607             --If time component is nulled out, duration can be different and this  will affect pricing
608             forall i in l_id_tbl.first..l_id_tbl.last
609                 update okc_k_lines_b set
610                     start_date = to_date(to_char(l_start_date_tbl(i), 'DD/MM/YYYY')|| to_char(start_date,'HH24:MI:SS'), 'DD/MM/YYYYHH24:MI:SS'),
611                     end_date = to_date(to_char(l_end_date_tbl(i), 'DD/MM/YYYY')|| to_char(end_date,'HH24:MI:SS'), 'DD/MM/YYYYHH24:MI:SS')
612                     where id =  l_id_tbl(i);
613 
614             l_id_tbl.delete;
615             l_start_date_tbl.delete;
616             l_end_date_tbl.delete;
617 
618             -- populate the cache for next bulk fetch
619             -- this gets all the parents of the last record and stores them in a local table
620             -- this is necessary to derive the parent for the lines obtained in the next bulk fetch.
621             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
622                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calling_populate_cache', 'l_lines_tbl(l_lines_tbl.last).id='||l_lines_tbl(l_lines_tbl.last).id||' ,x_return_status='||x_return_status);
623             END IF;
624             populate_cache(l_lines_tbl(l_lines_tbl.last));
625 
626         end loop; -- main bulk fetch limit loop
627         close c_lines;
628         l_lines_tbl.delete;
629         l_id_tbl.delete;
630         l_start_date_tbl.delete;
631         l_end_date_tbl.delete;
632 
633 
634         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
635             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='||x_return_status);
636         END IF;
637 
638     EXCEPTION
639 
640         WHEN FND_API.g_exc_error THEN
641             x_return_status := FND_API.g_ret_sts_error;
642             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
643                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
644             END IF;
645             RAISE;
646 
647         WHEN FND_API.g_exc_unexpected_error THEN
648             x_return_status := FND_API.g_ret_sts_unexp_error ;
649             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
650                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
651             END IF;
652             RAISE;
653 
654         WHEN OTHERS THEN
655             x_return_status := FND_API.g_ret_sts_unexp_error ;
656             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
657                 --first log the sqlerrm
658                 l_error_text := substr (SQLERRM, 1, 240);
659                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
660                 --then add it to the message api list
661                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
662             END IF;
663             IF (c_lines%isopen) THEN
664                 CLOSE c_lines;
665             END IF;
666             IF (c_kep_lines%isopen) THEN
667                 CLOSE c_kep_lines;
668             END IF;
669 
670             RAISE;
671 
672     END UPDATE_RENEWAL_DATES;
673 
674     /*
675     Internal procedure that updates the date_renewed column for the source contract header
676     and the source contract lines are actually renewed (i.e., ignores any DNR lines)
677     */
678     PROCEDURE UPDATE_SOURCE_CONTRACT
679     (
680      p_new_chr_id  IN NUMBER,
681      p_old_chr_id IN NUMBER,
682      x_return_status OUT NOCOPY VARCHAR2
683     )
684     IS
685 
686     l_api_name CONSTANT VARCHAR2(30) := 'UPDATE_SOURCE_CONTRACT';
687     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
688     l_error_text    VARCHAR2(512);
689 
690 	l_id_tbl num_tbl_type;
691 	l_date date := sysdate;
692 
693     --gets the line in the source contract for which the lines actualluy exist in the target
694     --contract
695     CURSOR c_old_lines(cp_new_chr_id IN NUMBER, cp_old_chr_id in number) IS
696         SELECT okl.id
697         FROM okc_k_lines_b okl, okc_operation_lines ol
698         WHERE ol.object_chr_id = cp_old_chr_id
699         AND ol.subject_chr_id = cp_new_chr_id
700         AND ol.object_cle_id IS NOT NULL AND ol.subject_cle_id IS NOT NULL
701         AND ol.process_flag = 'P' AND ol.active_yn = 'Y'
702         AND ol.object_cle_id = okl.id AND okl.dnz_chr_id = cp_old_chr_id;
703 
704     BEGIN
705 
706         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
707             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_new_chr_id='||p_new_chr_id||' ,p_old_chr_id='||p_old_chr_id);
708         END IF;
709         x_return_status := FND_API.G_RET_STS_SUCCESS;
710 
711 
712 		UPDATE okc_k_headers_all_b
713             SET	date_renewed = l_date,
714 			    object_version_number = (object_version_number + 1)
715 			WHERE id = p_old_chr_id;
716 
717 		OPEN c_old_lines(p_new_chr_id, p_old_chr_id);
718 		LOOP
719 			FETCH c_old_lines BULK COLLECT INTO l_id_tbl LIMIT G_BULK_FETCH_LIMIT;
720 
721             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
722                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.bulk_fetch', 'l_id_tbl.count='||l_id_tbl.count);
723             END IF;
724 
725             EXIT WHEN (l_id_tbl.count = 0);
726 
727 			FORALL i IN l_id_tbl.first..l_id_tbl.last
728 				UPDATE okc_k_lines_b
729                     SET date_renewed = l_date,
730 					    object_version_number = (object_version_number + 1)
731 					WHERE id = l_id_tbl(i);
732 
733             l_id_tbl.delete;
734 		END LOOP;
735 		CLOSE c_old_lines;
736         l_id_tbl.delete;
737 
738         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
739             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='||x_return_status);
740         END IF;
741 
742     EXCEPTION
743         WHEN FND_API.g_exc_error THEN
744             x_return_status := FND_API.g_ret_sts_error;
745             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
746                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
747             END IF;
748             RAISE;
749 
750         WHEN FND_API.g_exc_unexpected_error THEN
751             x_return_status := FND_API.g_ret_sts_unexp_error ;
752             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
753                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
754             END IF;
755             RAISE;
756 
757         WHEN OTHERS THEN
758             x_return_status := FND_API.g_ret_sts_unexp_error ;
759             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
760                 l_error_text := substr (SQLERRM, 1, 240);
761                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
762                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
763             END IF;
764             IF (c_old_lines%isopen) THEN
765                 CLOSE c_old_lines;
766             END IF;
767             RAISE;
768 
769     END UPDATE_SOURCE_CONTRACT;
770 
771 
772     /*
773     Internal procedure that updates the date_active and date_inactive of the conditions
774     associated with the renewed contract
775     */
776 
777     PROCEDURE UPDATE_CONDITION_HEADERS
778     (
779      p_chr_id IN NUMBER,
780      p_new_start_date IN DATE,
781      p_new_end_date IN DATE,
782      p_old_start_date IN DATE,
783      p_old_end_date IN DATE,
784      x_return_status OUT NOCOPY VARCHAR2
785     )
786     IS
787 
788     l_api_name CONSTANT VARCHAR2(30) := 'UPDATE_CONDITION_HEADERS';
789     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
790     l_error_text    VARCHAR2(512);
791 
792 	cursor c_condition_headers(cp_chr_id in number) is
793 		select id, date_active, date_inactive
794 		from okc_condition_headers_b
795 		where dnz_chr_id = cp_chr_id;
796 
797 	type cond_rec is record(
798 		id		        number,
799 		date_active	    date,
800 		date_inactive	date);
801 
802 	TYPE cond_rec_tbl_type IS TABLE OF cond_rec INDEX BY BINARY_INTEGER;
803 
804 	l_cond_tbl              cond_rec_tbl_type;
805 	l_id_tbl                num_tbl_type;
806 	l_date_inactive_tbl     date_tbl_type;
807 
808     BEGIN
809 
810         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
811             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id='||p_chr_id||' ,p_new_start_date='||p_new_start_date||' ,p_new_end_date='||p_new_end_date||
812             ' ,p_old_start_date='||p_old_start_date||' ,p_old_end_date='||p_old_end_date);
813         END IF;
814         x_return_status := FND_API.G_RET_STS_SUCCESS;
815 
816 		open c_condition_headers(p_chr_id);
817 		loop
818 			fetch c_condition_headers bulk collect into l_cond_tbl limit G_BULK_FETCH_LIMIT;
819 
820             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
821                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.bulk_fetch', 'l_cond_tbl.count='||l_cond_tbl.count);
822             END IF;
823 
824             exit when (l_cond_tbl.count = 0);
825 
826 			for i in l_cond_tbl.first..l_cond_tbl.last loop
827 				l_id_tbl(i) := l_cond_tbl(i).id;
828 				l_date_inactive_tbl(i) := null;
829 
830 				if l_cond_tbl(i).date_inactive is not null then
831 					if (l_cond_tbl(i).date_inactive = p_old_end_date and
832 						l_cond_tbl(i).date_active = p_old_start_date) then
833 						l_date_inactive_tbl(i) := p_new_end_date;
834 					else
835 						l_date_inactive_tbl(i) := p_new_start_date;
836 					end if;
837 				else
838 					if l_cond_tbl(i).date_active <> p_old_start_date then
839 						l_date_inactive_tbl(i) := p_new_start_date;
840 					end if;
841 				end if;
842 			end loop;
843 
844 			forall i in l_id_tbl.first..l_id_tbl.last
845 				update okc_condition_headers_b set
846 					date_active = p_new_start_date,
847 					date_inactive = l_date_inactive_tbl(i),
848 					object_version_number = (object_version_number +1)
849 					where id = l_id_tbl(i);
850 
851             l_cond_tbl.delete;
852 			l_id_tbl.delete;
853 			l_date_inactive_tbl.delete;
854 
855 		end loop;
856 		close c_condition_headers;
857 
858         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
859             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='||x_return_status);
860         END IF;
861 
862     EXCEPTION
863         WHEN FND_API.g_exc_error THEN
864             x_return_status := FND_API.g_ret_sts_error;
865             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
866                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
867             END IF;
868             RAISE;
869 
870         WHEN FND_API.g_exc_unexpected_error THEN
871             x_return_status := FND_API.g_ret_sts_unexp_error ;
872             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
873                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
874             END IF;
875             RAISE;
876 
877         WHEN OTHERS THEN
878             x_return_status := FND_API.g_ret_sts_unexp_error ;
879             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
880                 l_error_text := substr (SQLERRM, 1, 240);
881                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
882                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
883             END IF;
884             IF (c_condition_headers%isopen) THEN
885                 CLOSE c_condition_headers;
886             END IF;
887             RAISE;
888 
889     END UPDATE_CONDITION_HEADERS;
890 
891     /*
892     Internal procedure for repricing a contract based on the renewal rules specified.
893     Updates okc_k_lines_b.price_list_id for 'LST' pricing method
894     Calls OKS_REPRICE_PVT.Call_Pricing_API to reprice the contract
895     Parameters
896         p_chr_id            : id of the contract that need to be repriced
897         p_price_method      : Pricing method, 'MAN', 'LST' or 'PCT'
898         p_price_list_id     : Price List Id for 'LST'/'PCT' pricing methods
899         p_markup_percent    : Markup percent for 'PCT' procing method
900 
901 
902     */
903     PROCEDURE REPRICE_CONTRACT
904     (
905      p_chr_id IN NUMBER,
906      p_price_method IN VARCHAR2,
907      p_price_list_id IN VARCHAR2,
908      p_markup_percent IN NUMBER,
909      x_msg_count OUT NOCOPY NUMBER,
910      x_msg_data OUT NOCOPY VARCHAR2,
911      x_return_status OUT NOCOPY VARCHAR2
912     )
913     IS
914     l_api_name CONSTANT VARCHAR2(30) := 'REPRICE_CONTRACT';
915     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
916     l_error_text VARCHAR2(512);
917     l_reprice_rec OKS_REPRICE_PVT.reprice_rec_type;
918 
919     CURSOR c_top_lines(cp_chr_id IN NUMBER) IS
920         SELECT c.cle_id, sum(nvl(c.price_negotiated,0)), sum(nvl(s.tax_amount,0))
921         FROM okc_k_lines_b c, oks_k_lines_b s
922         WHERE c.dnz_chr_id = cp_chr_id
923         --get only sublines for 1,12,19 (14:no renewal, 46:no sublines)
924         AND c.lse_id IN (7,8,9,10,11,35, 13, 25)
925         AND s.cle_id = c.id
926         GROUP BY c.cle_id;
927 
928     l_id_tbl        num_tbl_type;
929     l_price_tbl     num_tbl_type;
930     l_tax_tbl       num_tbl_type;
931 
932     BEGIN
933 
934         --log key input parameters
935         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
936             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_price_method='||p_price_method||' ,p_price_list_id='||p_price_list_id||' ,p_markup_percent='||p_markup_percent);
937         END IF;
938 
939         x_return_status := FND_API.G_RET_STS_SUCCESS;
940 
941         l_reprice_rec.contract_id := p_chr_id;
942         IF (p_price_method = 'MAN') THEN
943             l_reprice_rec.price_type := p_price_method;
944         ELSIF (p_price_method = 'LST')THEN
945             l_reprice_rec.price_type := p_price_method;
946             l_reprice_rec.price_list_id := p_price_list_id;
947             --update toplines with price list id
948             UPDATE okc_k_lines_b
949                 SET price_list_id = p_price_list_id
950                 WHERE dnz_chr_id = p_chr_id
951                 AND cle_id IS NULL;
952         ELSIF (p_price_method = 'PCT') THEN
953             l_reprice_rec.price_type := p_price_method;
954             l_reprice_rec.price_list_id := p_price_list_id;
955             l_reprice_rec.markup_percent := p_markup_percent;
956         ELSE
957             --default to manual if no renewal pricing method specified
958             --this would atleast pro-rate the old amounts, if duration has changed
959             --this should never happen as this is mandatory at GCD global level
960             l_reprice_rec.price_type := 'MAN';
961         END IF;
962 
963 
964         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
965             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.call_pricing_api', 'calling OKS_REPRICE_PVT.call_pricing_api p_reprice_rec.contract_id=' || l_reprice_rec.contract_id ||' ,.price_type='||l_reprice_rec.price_type||
966             ' ,.price_list_id='||l_reprice_rec.price_list_id||' ,.markup_percent='||l_reprice_rec.markup_percent);
967         END IF;
968 
969         OKS_REPRICE_PVT.call_pricing_api(
970             p_api_version => 1,
971             p_init_msg_list => FND_API.G_FALSE,
972             p_reprice_rec => l_reprice_rec,
973             x_return_status => x_return_status,
974             x_msg_count => x_msg_count,
975             x_msg_data => x_msg_data);
976 
977         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
978             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.call_pricing_api', 'after call to  OKS_REPRICE_PVT.call_pricing_api x_return_status=' || x_return_status);
979         END IF;
980 
981         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
982             RAISE FND_API.g_exc_unexpected_error;
983         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
984             RAISE FND_API.g_exc_error;
985         END IF;
986 
987 
988         --update the topline price_negotiated(OKC) and tax_amount(OKS) columns
989         --no need for warranty(14 - cannot be renewed) and subscription (46 - no toplines)
990         OPEN c_top_lines(p_chr_id);
991         LOOP
992             FETCH c_top_lines BULK COLLECT INTO l_id_tbl, l_price_tbl, l_tax_tbl LIMIT G_BULK_FETCH_LIMIT;
993             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
994                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.top_line_loop', 'l_id_tbl.count='|| l_id_tbl.count);
995             END IF;
996 
997             EXIT WHEN (l_id_tbl.count = 0);
998 
999             FORALL i IN l_id_tbl.first..l_id_tbl.last
1000                 UPDATE okc_k_lines_b
1001                     SET price_negotiated = l_price_tbl(i)
1002                     WHERE id = l_id_tbl(i);
1003 
1004             FORALL i IN l_id_tbl.first..l_id_tbl.last
1005                 UPDATE oks_k_lines_b
1006                     SET tax_amount = l_tax_tbl(i)
1007                     WHERE cle_id = l_id_tbl(i);
1008         END LOOP;
1009         CLOSE c_top_lines;
1010         l_id_tbl.delete;
1011         l_price_tbl.delete;
1012         l_tax_tbl.delete;
1013 
1014         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1015             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_header', 'updating okc-oks header etimated_amount and tax_amount');
1016         END IF;
1017 
1018         --update the header
1019         UPDATE okc_k_headers_all_b h
1020             SET h.estimated_amount =
1021                 (SELECT sum(price_negotiated) FROM okc_k_lines_b tl
1022                  WHERE tl.dnz_chr_id = p_chr_id AND tl.cle_id IS NULL
1023                  AND tl.lse_id IN (1,12,19,46))
1024             WHERE h.id = p_chr_id;
1025 
1026         UPDATE oks_k_headers_b h
1027             SET h.tax_amount =
1028                 (SELECT sum(stl.tax_amount) FROM okc_k_lines_b ctl, oks_k_lines_b stl
1029                  WHERE ctl.dnz_chr_id = p_chr_id AND ctl.cle_id IS NULL
1030                  AND ctl.lse_id IN (1,12,19,46) AND stl.cle_id = ctl.id)
1031             WHERE h.chr_id = p_chr_id;
1032 
1033 
1034         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1035             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
1036         END IF;
1037 
1038     EXCEPTION
1039         WHEN FND_API.g_exc_error THEN
1040             x_return_status := FND_API.g_ret_sts_error;
1041             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1042                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
1043             END IF;
1044             IF (c_top_lines%isopen) THEN
1045                 CLOSE c_top_lines;
1046             END IF;
1047             RAISE;
1048 
1049         WHEN FND_API.g_exc_unexpected_error THEN
1050             x_return_status := FND_API.g_ret_sts_unexp_error ;
1051             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1052                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
1053             END IF;
1054             IF (c_top_lines%isopen) THEN
1055                 CLOSE c_top_lines;
1056             END IF;
1057             RAISE;
1058 
1059         WHEN OTHERS THEN
1060             x_return_status := FND_API.g_ret_sts_unexp_error ;
1061             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1062                 --first log the sqlerrm
1063                 l_error_text := substr (SQLERRM, 1, 240);
1064                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
1065                 --then add it to the message api list
1066                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
1067             END IF;
1068             IF (c_top_lines%isopen) THEN
1069                 CLOSE c_top_lines;
1070             END IF;
1071             RAISE;
1072     END REPRICE_CONTRACT;
1073 
1074     /*
1075     Internal procedure for getting a winning resource from JTF Territories
1076     Calls JTF_TERR_ASSIGN_PUB.get_winners to get the resource
1077     Parameters
1078         p_org_id            : org id of the contract
1079         p_party_id          : customer or subscriber party id
1080         x_winning_res_id    : resource id of the winning salesrep
1081     */
1082     PROCEDURE GET_SALESREP_FROM_JTF
1083     (
1084      p_org_id IN NUMBER,
1085      p_party_id IN NUMBER,
1086      x_winning_res_id OUT NOCOPY NUMBER,
1087      x_msg_count OUT NOCOPY NUMBER,
1088      x_msg_data OUT NOCOPY VARCHAR2,
1089      x_return_status OUT NOCOPY VARCHAR2
1090     )
1091     IS
1092     l_api_name CONSTANT VARCHAR2(30) := 'GET_SALESREP_FROM_JTF';
1093     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
1094     l_error_text VARCHAR2(512);
1095 
1096     l_prof_terr_qual    VARCHAR2(30);
1097     l_party_name        VARCHAR2(360);
1098     l_country_code      VARCHAR2(360);
1099     l_state_code        VARCHAR2(360);
1100     l_counter           NUMBER;
1101     l_count             NUMBER;
1102 
1103     l_gen_bulk_rec      JTF_TERR_ASSIGN_PUB.bulk_trans_rec_type;
1104     l_gen_return_rec    JTF_TERR_ASSIGN_PUB.bulk_winners_rec_type;
1105 
1106     CURSOR c_vendor_details(cp_org_id IN NUMBER, cp_party_id IN NUMBER) IS
1107         SELECT hz.party_name, hrl.country, hrl.region_2
1108         FROM hr_locations hrl, hr_all_organization_units hr, hz_parties hz
1109         WHERE hrl.location_id = hr.location_id
1110         AND hr.organization_id = cp_org_id
1111         AND hz.party_id = cp_party_id;
1112 
1113     CURSOR c_customer_details(cp_party_id IN NUMBER) IS
1114         SELECT hz.party_name, hzl.country, hzl.state
1115         FROM   hz_parties hz,  hz_party_sites hzs , hz_locations hzl
1116         WHERE hz.party_id = cp_party_id
1117         AND   hzs.party_id = hz.party_id
1118         AND   hzs.identifying_address_flag = 'Y'
1119         AND   hzl.location_id = hzs.location_id;
1120 
1121     BEGIN
1122 
1123         --log key input parameters
1124         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1125             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_org_id=' || p_org_id||' ,p_party_id='||p_party_id);
1126         END IF;
1127 
1128         x_return_status := FND_API.G_RET_STS_SUCCESS;
1129 
1130         l_prof_terr_qual:= nvl(FND_PROFILE.value('OKS_SRC_TERR_QUALFIERS'), 'V');
1131         IF (l_prof_terr_qual = 'V') THEN
1132             --get the customer name and vendor country and state
1133             OPEN c_vendor_details(p_org_id, p_party_id);
1134             FETCH c_vendor_details INTO l_party_name, l_country_code, l_state_code;
1135             CLOSE c_vendor_details;
1136         ELSE
1137             --get customer name and customer country and satte
1138             OPEN c_customer_details(p_party_id);
1139             FETCH c_customer_details INTO l_party_name, l_country_code, l_state_code;
1140             CLOSE c_customer_details;
1141         END IF;
1142 
1143         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1144             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_country_state', 'profile:OKS_SRC_TERR_QUALFIERS=' || l_prof_terr_qual||' ,l_party_name='||l_party_name||' ,l_country_code='||l_country_code||' ,l_state_code='||l_state_code);
1145         END IF;
1146 
1147         l_gen_bulk_rec.trans_object_id.EXTEND;
1148         l_gen_bulk_rec.trans_detail_object_id.EXTEND;
1149         l_gen_bulk_rec.SQUAL_CHAR01.EXTEND;
1150         l_gen_bulk_rec.SQUAL_CHAR04.EXTEND;
1151         l_gen_bulk_rec.SQUAL_CHAR07.EXTEND;
1152         l_gen_bulk_rec.SQUAL_NUM01.EXTEND;
1153 
1154         l_gen_bulk_rec.trans_object_id(1) := 100;
1155         l_gen_bulk_rec.trans_detail_object_id(1) := 1000;
1156         l_gen_bulk_rec.SQUAL_CHAR01(1) := l_party_name;
1157         l_gen_bulk_rec.SQUAL_CHAR04(1) := l_state_code;
1158         l_gen_bulk_rec.SQUAL_CHAR07(1) := l_country_code;
1159         l_gen_bulk_rec.SQUAL_NUM01(1) := p_party_id;
1160 
1161         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1162             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_winners', 'calling JTF_TERR_ASSIGN_PUB.get_winners, p_use_type=RESOURCE, p_source_id=-1500, p_trans_id=-1501');
1163         END IF;
1164 
1165         fnd_file.put_line(FND_FILE.LOG,'  ');
1166         fnd_file.put_line(FND_FILE.LOG,'Calling JTF_TERR_ASSIGN_PUB.get_winners  ');
1167         fnd_file.put_line(FND_FILE.LOG,' Parameters: ');
1168         fnd_file.put_line(FND_FILE.LOG,' l_gen_bulk_rec.trans_object_id(1): '||100);
1169         fnd_file.put_line(FND_FILE.LOG,' l_gen_bulk_rec.trans_detail_object_id(1): '||1000);
1170         fnd_file.put_line(FND_FILE.LOG,' l_gen_bulk_rec.SQUAL_CHAR01(1): '||l_party_name);
1171         fnd_file.put_line(FND_FILE.LOG,' l_gen_bulk_rec.SQUAL_CHAR04(1): '||l_state_code);
1172         fnd_file.put_line(FND_FILE.LOG,' l_gen_bulk_rec.SQUAL_CHAR07(1): '||l_country_code);
1173         fnd_file.put_line(FND_FILE.LOG,' l_gen_bulk_rec.SQUAL_NUM01(1): '||p_party_id);
1174         fnd_file.put_line(FND_FILE.LOG,'  ');
1175 
1176         JTF_TERR_ASSIGN_PUB.get_winners
1177         (p_api_version_number => 1.0,
1178          p_init_msg_list => FND_API.G_FALSE,
1179          p_use_type => 'RESOURCE',
1180          p_source_id =>  - 1500,
1181          p_trans_id =>  - 1501,
1182          p_trans_rec => l_gen_bulk_rec,
1183          p_resource_type => FND_API.G_MISS_CHAR,
1184          p_role => FND_API.G_MISS_CHAR,
1185          p_top_level_terr_id => FND_API.G_MISS_NUM,
1186          p_num_winners => FND_API.G_MISS_NUM,
1187          x_return_status => x_return_status,
1188          x_msg_count => x_msg_count,
1189          x_msg_data => x_msg_data,
1190          x_winners_rec => l_gen_return_rec );
1191 
1192         fnd_file.put_line(FND_FILE.LOG,'  ');
1193         fnd_file.put_line(FND_FILE.LOG,'After Calling JTF_TERR_ASSIGN_PUB.get_winners  ');
1194         fnd_file.put_line(FND_FILE.LOG,'x_return_status :  '||x_return_status);
1195         fnd_file.put_line(FND_FILE.LOG,'x_msg_count :  '||x_msg_count);
1196         fnd_file.put_line(FND_FILE.LOG,'x_msg_data :  '||x_msg_data);
1197         fnd_file.put_line(FND_FILE.LOG,'  ');
1198 
1199         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1200             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_winners', 'after call to JTF_TERR_ASSIGN_PUB.get_winners, x_return_status='||x_return_status);
1201         END IF;
1202 
1203         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
1204             RAISE FND_API.g_exc_unexpected_error;
1205         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
1206             RAISE FND_API.g_exc_error;
1207         END IF;
1208 
1209         -- bug 5493685
1210         l_count := NVL(l_gen_return_rec.resource_id.COUNT,0);
1211         fnd_file.put_line(FND_FILE.LOG,'Resource Count : '||l_count);
1212         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1213           FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_winners','l_count : '||l_count);
1214         END IF;
1215 
1216         IF l_count > 0 THEN
1217           fnd_file.put_line(FND_FILE.LOG,'l_gen_return_rec.resource_id.FIRST : '||l_gen_return_rec.resource_id.FIRST);
1218           --set OUT parameter
1219           x_winning_res_id := l_gen_return_rec.resource_id(l_gen_return_rec.resource_id.FIRST);
1220           fnd_file.put_line(FND_FILE.LOG,'x_winning_res_id : '||x_winning_res_id);
1221              IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1222                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_winners','x_winning_res_id : '||x_winning_res_id);
1223              END IF;
1224         END IF; -- l_count > 0
1225         -- end added  bug 5493685
1226 
1227         /* Commented for bug 5493685
1228         l_counter := l_gen_return_rec.trans_object_id.FIRST;
1229         l_count := 0;
1230 
1231         WHILE (l_counter <= l_gen_return_rec.trans_object_id.LAST) LOOP
1232             --get the first resource found.
1233             IF (l_count = 0) THEN
1234                 --set OUT parameter
1235                 x_winning_res_id := l_gen_return_rec.resource_id(l_counter);
1236             END IF;
1237             l_counter := l_counter + 1;
1238             l_count := l_count + 1;
1239 
1240             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1241                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_winners_loop', 'l_counter='||l_counter||' ,l_gen_return_rec.resource_id='||l_gen_return_rec.resource_id(l_counter));
1242             END IF;
1243 
1244         END LOOP;
1245 
1246         END Commented for bug 5493685 */
1247 
1248         IF l_count = 0 THEN
1249             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_JTF_RESOURCE');
1250             FND_MESSAGE.set_token('PARTY', l_party_name);
1251             FND_MESSAGE.set_token('COUNTRY', l_country_code);
1252             FND_MESSAGE.set_token('STATE', l_state_code);
1253             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1254                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_winners_error', FALSE);
1255             END IF;
1256             FND_MSG_PUB.add;
1257             RAISE FND_API.g_exc_error;
1258         END IF;
1259 
1260         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1261             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status||' ,x_winning_res_id='||x_winning_res_id);
1262         END IF;
1263 
1264     EXCEPTION
1265         WHEN FND_API.g_exc_error THEN
1266             x_return_status := FND_API.g_ret_sts_error;
1267             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1268                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
1269             END IF;
1270             IF (c_vendor_details%isopen) THEN
1271                 CLOSE c_vendor_details;
1272             END IF;
1273             IF (c_customer_details%isopen) THEN
1274                 CLOSE c_customer_details;
1275             END IF;
1276             RAISE;
1277 
1278         WHEN FND_API.g_exc_unexpected_error THEN
1279             x_return_status := FND_API.g_ret_sts_unexp_error ;
1280             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1281                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
1282             END IF;
1283             IF (c_vendor_details%isopen) THEN
1284                 CLOSE c_vendor_details;
1285             END IF;
1286             IF (c_customer_details%isopen) THEN
1287                 CLOSE c_customer_details;
1288             END IF;
1289             RAISE;
1290 
1291         WHEN OTHERS THEN
1292             x_return_status := FND_API.g_ret_sts_unexp_error ;
1293             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1294                 --first log the sqlerrm
1295                 l_error_text := substr (SQLERRM, 1, 240);
1296                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
1297                 --then add it to the message api list
1298                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
1299             END IF;
1300             IF (c_vendor_details%isopen) THEN
1301                 CLOSE c_vendor_details;
1302             END IF;
1303             IF (c_customer_details%isopen) THEN
1304                 CLOSE c_customer_details;
1305             END IF;
1306             RAISE;
1307 
1308     END GET_SALESREP_FROM_JTF;
1309 
1310 
1311     /*
1312     Internal procedure for processing sales credits during renewal.
1313     Parameters
1314         p_chr_id            : id of the renewed contract that need to be repriced
1315     */
1316     PROCEDURE PROCESS_SALES_CREDIT
1317     (
1318      p_chr_id IN NUMBER,
1319      x_msg_count OUT NOCOPY NUMBER,
1320      x_msg_data OUT NOCOPY VARCHAR2,
1321      x_return_status OUT NOCOPY VARCHAR2
1322     )
1323     IS
1324     l_api_name CONSTANT VARCHAR2(30) := 'PROCESS_SALES_CREDIT';
1325     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
1326     l_error_text VARCHAR2(512);
1327 
1328     --gets the contract org id and customer/subscriber party id
1329     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
1330         SELECT  nvl(a.org_id, a.authoring_org_id), to_number(b.object1_id1)
1331         FROM    okc_k_headers_all_b a, okc_k_party_roles_b b
1332         WHERE   a.id = cp_chr_id
1333         AND     b.rle_code IN ('CUSTOMER', 'SUBSCRIBER')
1334         AND     b.dnz_chr_id = a.id
1335         AND     b.cle_id IS NULL;
1336 
1337     --gets the salesrep id from jtf resource id and k org id
1338     CURSOR c_res_salesrep(cp_res_id IN NUMBER, cp_org_id IN NUMBER) IS
1339         SELECT salesrep_id
1340         FROM   jtf_rs_salesreps
1341         WHERE  resource_id = cp_res_id AND org_id = cp_org_id;
1342 
1343     --same salesrep can be defined for multiple orgs - so check
1344     CURSOR c_check_org_match(cp_salesrep_id IN NUMBER, cp_org_id IN NUMBER) IS /*bugfix for 6672863 */
1345         SELECT s.org_id,  v.resource_name
1346         FROM jtf_rs_salesreps s , jtf_rs_resource_extns_vl v
1347         WHERE s.resource_id = v.resource_id
1348           AND s.salesrep_id = cp_salesrep_id
1349 	  AND org_id = cp_org_id;  /*bugfix for 6672863*/
1350 
1351     --gets the org name for the org id
1352     CURSOR c_get_org_name(cp_org_id IN NUMBER) IS
1353         SELECT hr.name
1354         FROM hr_all_organization_units hr
1355         WHERE hr.organization_id = cp_org_id;
1356 
1357 
1358     --gets the contract vendor/merhcant record id, rle_code from okc_k_party_roles_b
1359     CURSOR c_get_ven_mer_id(cp_chr_id IN NUMBER) IS
1360         SELECT id, rle_code
1361         FROM okc_k_party_roles_b
1362         WHERE dnz_chr_id = cp_chr_id AND cle_id IS NULL
1363         AND rle_code IN ('VENDOR', 'MERCHANT');
1364 
1365     --gets the cro_code for vendor/merchant contact source based on OKX_SALEPERS
1366     CURSOR c_get_cro_code(cp_rle_code IN VARCHAR2) IS
1367         SELECT cro_code
1368         FROM okc_contact_sources
1369         WHERE buy_or_sell = 'S' AND rle_code = cp_rle_code
1370         AND jtot_object_code = 'OKX_SALEPERS'
1371         AND SYSDATE BETWEEN start_date AND NVL(end_date,SYSDATE + 1) ; -- bug 5938308
1372 
1373     --gets the toplines for creating the sales credit
1374     CURSOR c_get_top_lines(cp_chr_id IN NUMBER) IS
1375         SELECT id
1376         FROM okc_k_lines_b
1377         WHERE dnz_chr_id = cp_chr_id AND cle_id IS NULL AND lse_id IN (1,12,19,46);
1378 
1379     l_prof_enable_sc        VARCHAR2(30);
1380     l_prof_rev_type         VARCHAR2(30);
1381     l_prof_use_jtf          VARCHAR2(30);
1382     l_prof_rev_type_dist    VARCHAR2(30);
1383 
1384     l_org_id                NUMBER;
1385     l_party_id              NUMBER;
1386     l_resource_id           NUMBER;
1387     l_salesrep_id           NUMBER;
1388     l_dummy_org_id          NUMBER;
1389 
1390     l_cpl_id                NUMBER;
1391     l_rle_code              VARCHAR2(30);
1392     l_cro_code              VARCHAR2(30);
1393     l_sales_group_id        NUMBER;
1394     l_ctcv_rec_in           okc_contract_party_pub.ctcv_rec_type;
1395     l_ctcv_rec_in           okc_contract_party_pub.ctcv_rec_type;
1396     l_percent               NUMBER;
1397     l_date                  DATE;
1398     l_created_by            NUMBER;
1399     l_login_id              NUMBER;
1400     l_id_tbl                num_tbl_type;
1401 
1402     l_salesrep_name         jtf_rs_resource_extns_vl.resource_name%TYPE;
1403     l_org_name              hr_all_organization_units.name%TYPE;
1404     l_return_status         VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
1405 
1406     BEGIN
1407 
1408         --log key input parameters
1409         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1410             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
1411         END IF;
1412 
1413         x_return_status := FND_API.G_RET_STS_SUCCESS;
1414 
1415 	    --can have four values, DRT:Derive for Revenue Type and Retain Other
1416         --YES:Derive,  NO:Drop,  R:Retain, defaults to R
1417         l_prof_enable_sc := nvl(FND_PROFILE.value('OKS_ENABLE_SALES_CREDIT'), 'R');
1418 
1419         --lookup for revenue type : Select name , id1 from OKX_SALES_CRED_TYPES_V order by NAME;
1420         --1:Quota Sales Credit, 2:Non-quota Sales Credit
1421         l_prof_rev_type := FND_PROFILE.VALUE('OKS_REVENUE_TYPE');
1422 
1423         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1424             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.profile_options', 'OKS_ENABLE_SALES_CREDIT=' || l_prof_enable_sc||' ,OKS_REVENUE_TYPE='||l_prof_rev_type);
1425         END IF;
1426 
1427         IF (l_prof_enable_sc = 'R') THEN
1428             --for R:Retain, do nothing
1429             IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1430                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end_1', 'Profile OKS_ENABLE_SALES_CREDIT=R(Retain), no processing required');
1431             END IF;
1432             RETURN;
1433         ELSIF (l_prof_enable_sc = 'NO') THEN
1434             --for NO:Drop, delete all existing sales credits and return
1435             DELETE FROM oks_k_sales_credits
1436                 WHERE chr_id = p_chr_id;
1437             IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1438                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end_2', 'Profile OKS_ENABLE_SALES_CREDIT=NO(Drop), deleted sales credits, no fruther processsing');
1439             END IF;
1440             RETURN;
1441         ELSIF(l_prof_enable_sc = 'YES') THEN
1442             --for YES:Derive, delete all existing sales credits and derive specified type of sales credit
1443             IF (l_prof_rev_type IS NULL) THEN
1444                 --without the profile setup we cannot recreate the sales credit
1445                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1446                 FND_MESSAGE.set_token('PROFILE', 'OKS_REVENUE_TYPE');
1447                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1448                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.rev_type_chk', FALSE);
1449                 END IF;
1450                 FND_MSG_PUB.add;
1451                 RAISE FND_API.g_exc_error;
1452             ELSE
1453                 DELETE FROM oks_k_sales_credits
1454                     WHERE chr_id = p_chr_id;
1455             END IF;
1456         ELSIF (l_prof_enable_sc = 'DRT') THEN
1457             --for DRT:Derive for Revenue Type and Retain Other, delete and derive specified type of sales credit
1458             IF (l_prof_rev_type IS NULL) THEN
1459                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1460                 FND_MESSAGE.set_token('PROFILE', 'OKS_REVENUE_TYPE');
1461                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1462                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.rev_type_chk', FALSE);
1463                 END IF;
1464                 FND_MSG_PUB.add;
1465                 RAISE FND_API.g_exc_error;
1466             ELSE
1467                 DELETE FROM oks_k_sales_credits
1468                     WHERE chr_id = p_chr_id AND sales_credit_type_id1 = l_prof_rev_type;
1469             END IF;
1470         END IF; --of ELSIF (l_prof_enable_sc = 'DRT') THEN
1471 
1472         --we come here only if l_prof_enable_sc = 'YES' or 'DRT'
1473         --derive the sales credits for the specified revenue type
1474 
1475         --get the contract org and customer/subscriber party id
1476         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1477             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_k_org_party', 'getting contract org and customer/subscriber party for p_chr_id='||p_chr_id);
1478         END IF;
1479 
1480         OPEN c_k_hdr(p_chr_id);
1481         FETCH c_k_hdr INTO l_org_id, l_party_id;
1482         CLOSE c_k_hdr;
1483         IF (l_org_id IS NULL) THEN
1484             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
1485             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
1486             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1487                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_k_org_party', FALSE);
1488             END IF;
1489             FND_MSG_PUB.ADD;
1490             RAISE FND_API.g_exc_error;
1491         END IF;
1492 
1493         --get the winning salesrep either from JTF or profile option
1494         l_prof_use_jtf := FND_PROFILE.VALUE('OKS_USE_JTF');
1495 
1496         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1497             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_k_org_party', 'l_org_id='||l_org_id||' ,l_party_id='||l_party_id||' ,Profile OKS_USE_JTF='||l_prof_use_jtf);
1498         END IF;
1499 
1500         IF (l_prof_use_jtf = 'YES') THEN
1501             --get the salesrep from JTF Territory setup
1502             --note this procedure will throw an error if no salesrep is setup in JTF
1503             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1504                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_salesrep', 'calling get_salesrep_from_jtf');
1505             END IF;
1506 
1507             fnd_file.put_line(FND_FILE.LOG,'Calling get_salesrep_from_jtf');
1508             get_salesrep_from_jtf(
1509                 p_org_id => l_org_id,
1510                 p_party_id => l_party_id,
1511                 x_winning_res_id => l_resource_id,
1512                 x_return_status => x_return_status,
1513                 x_msg_count => x_msg_count,
1514                 x_msg_data => x_msg_data);
1515 
1516             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1517                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_salesrep', 'after call to get_salesrep_from_jtf, x_return_status='||x_return_status||' ,l_resource_id='||l_resource_id);
1518             END IF;
1519 
1520             --get the salesrep id corresponding to this resource and k org
1521             OPEN c_res_salesrep(l_resource_id, l_org_id);
1522             FETCH c_res_salesrep INTO l_salesrep_id;
1523             CLOSE c_res_salesrep;
1524 
1525             IF l_salesrep_id IS NULL THEN
1526                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_SREP_FOR_RES');
1527                 FND_MESSAGE.set_token('RESOURCE_ID', l_resource_id);
1528                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1529                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_jtf_salesrep', FALSE);
1530                 END IF;
1531                 FND_MSG_PUB.ADD;
1532                 RAISE FND_API.g_exc_error;
1533             END IF;
1534 
1535         ELSE
1536             --get the salesrep from profile option
1537             l_salesrep_id := FND_PROFILE.value('OKS_SALESPERSON_ID');
1538 
1539             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1540                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_salesrep', 'salesrep from profile option OKS_SALESPERSON_ID='||l_salesrep_id);
1541             END IF;
1542 
1543             IF l_salesrep_id IS NULL THEN
1544                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1545                 FND_MESSAGE.set_token('PROFILE', 'OKS_SALESPERSON_ID');
1546                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1547                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_prof_salesrep', FALSE);
1548                 END IF;
1549                 FND_MSG_PUB.add;
1550                 RAISE FND_API.g_exc_error;
1551             END IF;
1552 
1553         END IF; --of IF (l_prof_use_jtf = 'YES') THEN
1554 
1555         --now check if the salesrep belongs to the same org as the contract
1556         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1557             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.org_id_match', 'checking salerep org,  l_salesrep_id='||l_salesrep_id||' ,l_org_id='||l_org_id);
1558         END IF;
1559 
1560         OPEN c_check_org_match(l_salesrep_id, l_org_id);/*bugfix 6672863*/
1561         FETCH c_check_org_match INTO l_dummy_org_id, l_salesrep_name;
1562         CLOSE c_check_org_match;
1563 
1564         IF (nvl(l_dummy_org_id,-99) <> l_org_id) THEN
1565             --as per bug # 2968069, if salesrep does not belong to the same org as the contract
1566             --we proceed without creating sales credit or adding the salerep to the contract
1567             --Note this can only happen for FND_PROFILE.VALUE('OKS_USE_JTF') = NO
1568             OPEN c_get_org_name(l_org_id);
1569             FETCH c_get_org_name INTO l_org_name;
1570             CLOSE c_get_org_name;
1571 
1572             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_SALESREP_ORG_MATCH');
1573             FND_MESSAGE.set_token('SALESREP_NAME', l_salesrep_name);
1574             FND_MESSAGE.set_token('ORG_NAME', l_org_name);
1575             IF (FND_LOG.level_event >= FND_LOG.g_current_runtime_level) THEN
1576                 FND_LOG.message(FND_LOG.level_event, l_mod_name || '.org_id_match', FALSE);
1577             END IF;
1578             FND_MSG_PUB.add;
1579             x_return_status := OKC_API.g_ret_sts_warning;
1580             RETURN;
1581         END IF;
1582 
1583         --get the party role id, rle_code for vendor/merchant
1584         OPEN c_get_ven_mer_id(p_chr_id);
1585         FETCH c_get_ven_mer_id INTO l_cpl_id, l_rle_code;
1586         CLOSE c_get_ven_mer_id;
1587 
1588         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1589             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_ven_mer_id', 'vendor/merhant details,  l_cpl_id='||l_cpl_id||' ,l_rle_code='||l_rle_code);
1590         END IF;
1591 
1592         IF (l_cpl_id IS NULL) THEN
1593             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_VENDOR_MERCHANT');
1594             FND_MESSAGE.set_token('CONTRACT_ID', to_char(p_chr_id));
1595             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1596                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_ven_mer_id', FALSE);
1597             END IF;
1598             FND_MSG_PUB.add;
1599             RAISE FND_API.g_exc_error;
1600         END IF;
1601 
1602         --get the first cro_code from vendor/merchant contact sources that are based on the
1603         --jtf object OKX_SALEPERS. There can be many contact sources based on OKX_SALEPERS, we
1604         --will just choose the first one
1605         OPEN c_get_cro_code(l_rle_code);
1606         FETCH c_get_cro_code INTO l_cro_code;
1607         CLOSE c_get_cro_code;
1608 
1609         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1610             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_cro_code', 'cro_code for role '||l_rle_code||' ,l_cro_code='||l_cro_code);
1611         END IF;
1612 
1613         IF (l_cro_code IS NULL) THEN
1614             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_K_SRC_FOR_SREP');
1615             FND_MESSAGE.set_token('RLE_CODE', l_rle_code);
1616             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1617                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_cro_code', FALSE);
1618             END IF;
1619             FND_MSG_PUB.add;
1620             RAISE FND_API.g_exc_error;
1621         END IF;
1622 
1623         --get the sales group id for the salesrep
1624         --function returns -1 for errors
1625         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1626             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_sales_group', 'calling jtf_rs_integration_pub.get_default_sales_group, p_salesrep_id='||l_salesrep_id||' ,p_org_id='||l_org_id||' ,p_date='||sysdate);
1627         END IF;
1628 
1629         l_sales_group_id := JTF_RS_INTEGRATION_PUB.get_default_sales_group(
1630                                 p_salesrep_id => l_salesrep_id,
1631                                 p_org_id => l_org_id,
1632                                 p_date => sysdate);
1633 
1634         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1635             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_sales_group', 'after call to JTF_RS_INTEGRATION_PUB.get_default_sales_group, l_sales_group_id='||l_sales_group_id);
1636         END IF;
1637 
1638         --just log the fact that no salesgroup was found, no error thrown
1639         IF (l_sales_group_id = -1) THEN
1640             IF (FND_LOG.level_event >= FND_LOG.g_current_runtime_level) THEN
1641                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_SALES_GROUP');
1642                 FND_MESSAGE.set_token('SALESREP_ID', to_char(l_salesrep_id));
1643                 FND_LOG.message(FND_LOG.level_event, l_mod_name || '.get_sales_group', TRUE);
1644             END IF;
1645         END IF;
1646 
1647         --delete any old vendor/merchant contacts based on jtf object OKX_SALEPERS
1648         DELETE FROM okc_contacts
1649             WHERE dnz_chr_id = p_chr_id
1650             AND cpl_id = l_cpl_id
1651             AND cro_code IN (SELECT cro_code FROM okc_contact_sources
1652                                 WHERE buy_or_sell = 'S' AND rle_code = l_rle_code
1653                                 AND jtot_object_code = 'OKX_SALEPERS');
1654 
1655         --add this salesrep as a contact for vendor/merchant with the cro_code found above
1656         l_created_by := FND_GLOBAL.USER_ID;
1657         l_date := sysdate;
1658         l_login_id := FND_GLOBAL.LOGIN_ID;
1659 
1660         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1661             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_contact', 'deleted old contacts, creating new contact for salesrep id='||l_salesrep_id);
1662         END IF;
1663 
1664         INSERT INTO OKC_CONTACTS(
1665             id,
1666             cpl_id,
1667             cro_code,
1668             dnz_chr_id,
1669             object1_id1,
1670             object1_id2,
1671             jtot_object1_code,
1672             object_version_number,
1673             created_by,
1674             creation_date,
1675             last_updated_by,
1676             last_update_date,
1677             last_update_login,
1678             sales_group_id)
1679         VALUES(
1680             okc_p_util.raw_to_number(sys_guid()),
1681             l_cpl_id,
1682             l_cro_code,
1683             p_chr_id,
1684             l_salesrep_id,
1685             '#',
1686             'OKX_SALEPERS',
1687             1,
1688             l_created_by,
1689             l_date,
1690             l_created_by,
1691             l_date,
1692             l_login_id,
1693             l_sales_group_id);
1694         /*cgopinee Bug fix for 6882512*/
1695 	OKC_CTC_PVT.update_contact_stecode(p_chr_id =>p_chr_id,
1696 	            x_return_status=>l_return_status);
1697 
1698         IF l_return_status <> OKC_API.G_RET_STS_SUCCESS THEN
1699 	   RAISE OKC_API.G_EXCEPTION_ERROR;
1700         END IF;
1701 
1702         --create sales credit for this salesperson
1703         l_prof_rev_type_dist := nvl(FND_PROFILE.VALUE('OKS_REVENUE_TYPE_DIST'), '0');
1704         l_percent := to_number(l_prof_rev_type_dist);
1705 
1706         IF (l_percent < 0 OR l_percent > 100) THEN
1707             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1708             FND_MESSAGE.set_token('PROFILE', 'OKS_REVENUE_TYPE_DIST');
1709             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1710                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_rev_type_dist', FALSE);
1711             END IF;
1712             FND_MSG_PUB.add;
1713             RAISE FND_API.g_exc_error;
1714         END IF;
1715 
1716         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1717             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.hdr_sales_credit', 'creating hdr sales credits with  OKS_REVENUE_TYPE_DIST='||l_prof_rev_type_dist);
1718         END IF;
1719 
1720         INSERT INTO oks_k_sales_credits(
1721             id,
1722             percent,
1723             chr_id,
1724             cle_id,
1725             ctc_id,
1726             sales_credit_type_id1,
1727             sales_credit_type_id2,
1728             object_version_number,
1729             created_by,
1730             creation_date,
1731             last_updated_by,
1732             last_update_date,
1733             security_group_id,
1734             sales_group_id)
1735         VALUES (
1736             okc_p_util.raw_to_number(sys_guid()),
1737             l_percent,
1738             p_chr_id,
1739             null,
1740             l_salesrep_id,
1741             l_prof_rev_type,
1742             '#',
1743             1,
1744             l_created_by,
1745             l_date,
1746             l_created_by,
1747             l_date,
1748             null,
1749             l_sales_group_id);
1750 
1751         OPEN c_get_top_lines(p_chr_id);
1752         LOOP
1753             FETCH c_get_top_lines BULK COLLECT INTO l_id_tbl LIMIT G_BULK_FETCH_LIMIT;
1754 
1755             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1756                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.line_sales_credit', 'l_id_tbl.count='||l_id_tbl.count);
1757             END IF;
1758 
1759             EXIT WHEN (l_id_tbl.count = 0);
1760             FORALL i in l_id_tbl.first..l_id_tbl.last
1761                 INSERT INTO oks_k_sales_credits(
1762                     id,
1763                     percent,
1764                     chr_id,
1765                     cle_id,
1766                     ctc_id,
1767                     sales_credit_type_id1,
1768                     sales_credit_type_id2,
1769                     object_version_number,
1770                     created_by,
1771                     creation_date,
1772                     last_updated_by,
1773                     last_update_date,
1774                     security_group_id,
1775                     sales_group_id)
1776                 VALUES (
1777                     okc_p_util.raw_to_number(sys_guid()),
1778                     l_percent,
1779                     p_chr_id,
1780                     l_id_tbl(i),
1781                     l_salesrep_id,
1782                     l_prof_rev_type,
1783                     '#',
1784                     1,
1785                     l_created_by,
1786                     l_date,
1787                     l_created_by,
1788                     l_date,
1789                     null,
1790                     l_sales_group_id);
1791 
1792         END LOOP;
1793         CLOSE c_get_top_lines;
1794         l_id_tbl.delete;
1795 
1796         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1797             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
1798         END IF;
1799 
1800     EXCEPTION
1801         WHEN FND_API.g_exc_error THEN
1802             x_return_status := FND_API.g_ret_sts_error;
1803             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1804                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
1805             END IF;
1806             IF ( c_res_salesrep%isopen ) THEN
1807                 CLOSE c_res_salesrep;
1808             END IF;
1809             IF ( c_check_org_match%isopen ) THEN
1810                 CLOSE c_check_org_match;
1811             END IF;
1812             IF ( c_get_org_name%isopen ) THEN
1813                 CLOSE c_get_org_name;
1814             END IF;
1815             IF ( c_get_ven_mer_id%isopen ) THEN
1816                 CLOSE c_get_ven_mer_id;
1817             END IF;
1818             IF ( c_get_cro_code%isopen ) THEN
1819                 CLOSE c_get_cro_code;
1820             END IF;
1821             IF ( c_get_top_lines%isopen ) THEN
1822                 CLOSE c_get_top_lines;
1823             END IF;
1824             RAISE;
1825 
1826         WHEN FND_API.g_exc_unexpected_error THEN
1827             x_return_status := FND_API.g_ret_sts_unexp_error ;
1828             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1829                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
1830             END IF;
1831             IF ( c_res_salesrep%isopen ) THEN
1832                 CLOSE c_res_salesrep;
1833             END IF;
1834             IF ( c_check_org_match%isopen ) THEN
1835                 CLOSE c_check_org_match;
1836             END IF;
1837             IF ( c_get_org_name%isopen ) THEN
1838                 CLOSE c_get_org_name;
1839             END IF;
1840             IF ( c_get_ven_mer_id%isopen ) THEN
1841                 CLOSE c_get_ven_mer_id;
1842             END IF;
1843             IF ( c_get_cro_code%isopen ) THEN
1844                 CLOSE c_get_cro_code;
1845             END IF;
1846             IF ( c_get_top_lines%isopen ) THEN
1847                 CLOSE c_get_top_lines;
1848             END IF;
1849             RAISE;
1850 
1851         WHEN OTHERS THEN
1852             x_return_status := FND_API.g_ret_sts_unexp_error ;
1853             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1854                 --first log the sqlerrm
1855                 l_error_text := substr (SQLERRM, 1, 240);
1856                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
1857                 --then add it to the message api list
1858                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
1859             END IF;
1860             IF ( c_res_salesrep%isopen ) THEN
1861                 CLOSE c_res_salesrep;
1862             END IF;
1863             IF ( c_check_org_match%isopen ) THEN
1864                 CLOSE c_check_org_match;
1865             END IF;
1866             IF ( c_get_org_name%isopen ) THEN
1867                 CLOSE c_get_org_name;
1868             END IF;
1869             IF ( c_get_ven_mer_id%isopen ) THEN
1870                 CLOSE c_get_ven_mer_id;
1871             END IF;
1872             IF ( c_get_cro_code%isopen ) THEN
1873                 CLOSE c_get_cro_code;
1874             END IF;
1875             IF ( c_get_top_lines%isopen ) THEN
1876                 CLOSE c_get_top_lines;
1877             END IF;
1878             RAISE;
1879 
1880     END PROCESS_SALES_CREDIT;
1881 
1882 
1883     /*
1884     Internal procedure for recreating coverage and subscription entitities. This can be done
1885     only after the contract dates have been adjusted
1886     Parameters
1887         p_chr_id            : id of the renewed contract
1888     */
1889     PROCEDURE RECREATE_COV_SUBSCR
1890     (
1891      p_chr_id IN NUMBER,
1892      x_msg_count OUT NOCOPY NUMBER,
1893      x_msg_data OUT NOCOPY VARCHAR2,
1894      x_return_status OUT NOCOPY VARCHAR2
1895     )
1896     IS
1897     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_COV_SUBSCR';
1898     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
1899     l_error_text VARCHAR2(512);
1900 
1901     l_id_tbl                num_tbl_type;
1902     l_old_id_tbl            num_tbl_type;
1903     l_lse_id_tbl            num_tbl_type;
1904 
1905     CURSOR c_subscr_service_lines(cp_chr_id IN NUMBER) IS
1906         SELECT id, nvl(orig_system_id1, cle_id_renewed) old_id, lse_id
1907         FROM okc_k_lines_b
1908         WHERE dnz_chr_id = cp_chr_id AND cle_id IS NULL
1909         AND lse_id IN (1,19,46);
1910 
1911     BEGIN
1912 
1913         --log key input parameters
1914         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1915             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
1916         END IF;
1917 
1918         x_return_status := FND_API.G_RET_STS_SUCCESS;
1919 
1920         OPEN c_subscr_service_lines(p_chr_id);
1921         LOOP
1922             FETCH c_subscr_service_lines BULK COLLECT INTO l_id_tbl, l_old_id_tbl, l_lse_id_tbl LIMIT G_BULK_FETCH_LIMIT;
1923 
1924             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1925                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.subcr_service_loop', 'l_id_tbl.count='||l_id_tbl.count);
1926             END IF;
1927             EXIT WHEN (l_id_tbl.count = 0);
1928 
1929             FOR i IN l_id_tbl.first..l_id_tbl.last LOOP
1930                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1931                     FND_LOG.string(FND_LOG.level_statement, l_mod_name||'.subcr_service_loop', 'i='||i||' ,l_id_tbl(i)='||l_id_tbl(i)||', l_lse_id_tbl(i)='||l_lse_id_tbl(i)||' ,l_old_id_tbl(i)='||l_old_id_tbl(i));
1932                 END IF;
1933 
1934                 --recreate coverage entities
1935                 IF( l_lse_id_tbl(i) IN (1,19) ) THEN
1936 
1937                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1938                         FND_LOG.string(FND_LOG.level_statement, l_mod_name||'.copy_coverage', 'calling OKS_COVERAGES_PVT.Copy_Coverage, p_contract_line_id='||l_id_tbl(i));
1939                     END IF;
1940 
1941                     OKS_COVERAGES_PVT.copy_coverage(
1942                         p_api_version => 1.0,
1943                         p_init_msg_list => FND_API.G_FALSE,
1944                         x_return_status => x_return_status,
1945                         x_msg_count => x_msg_count,
1946                         x_msg_data => x_msg_data,
1947                         p_contract_line_id => l_id_tbl(i));
1948 
1949                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1950                         FND_LOG.string(FND_LOG.level_statement, l_mod_name||'.copy_coverage', 'after call to OKS_COVERAGES_PVT.Copy_Coverage, x_return_status='||x_return_status);
1951                     END IF;
1952 
1953                     IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
1954                         RAISE FND_API.g_exc_unexpected_error;
1955                     ELSIF x_return_status = FND_API.g_ret_sts_error THEN
1956                         RAISE FND_API.g_exc_error;
1957                     END IF;
1958 
1959                 ELSIF (l_lse_id_tbl(i) = 46) THEN
1960 
1961                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1962                         FND_LOG.string(FND_LOG.level_statement, l_mod_name||'.copy_subscr', 'calling OKS_SUBSCRIPTION_PUB.copy_subscription, p_source_cle_id='||l_old_id_tbl(i)||' ,p_target_cle_id='||l_id_tbl(i)||', p_intent=RENEW');
1963                     END IF;
1964 
1965                     OKS_SUBSCRIPTION_PUB.copy_subscription(
1966                         p_api_version => 1.0,
1967                         p_init_msg_list => FND_API.G_FALSE,
1968                         x_return_status => x_return_status,
1969                         x_msg_count => x_msg_count,
1970                         x_msg_data => x_msg_data,
1971                         p_source_cle_id => l_old_id_tbl(i),
1972                         p_target_cle_id => l_id_tbl(i),
1973                         p_intent => 'RENEW');
1974 
1975                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1976                         FND_LOG.string(FND_LOG.level_statement, l_mod_name||'.copy_subscr', 'after call to OKS_SUBSCRIPTION_PUB.copy_subscription, x_return_status='||x_return_status);
1977                     END IF;
1978 
1979                     IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
1980                         RAISE FND_API.g_exc_unexpected_error;
1981                     ELSIF x_return_status = FND_API.g_ret_sts_error THEN
1982                         RAISE FND_API.g_exc_error;
1983                     END IF;
1984                 END IF; --of elsif ELSIF (l_lse_id_tbl(i) = 46) THEN
1985             END LOOP; --of FOR i IN l_id_tbl.first..l_id_tbl.last LOOP
1986 
1987         END LOOP; --of top line bulk fetch loop
1988         CLOSE c_subscr_service_lines;
1989         l_id_tbl.delete;
1990         l_old_id_tbl.delete;
1991         l_lse_id_tbl.delete;
1992 
1993         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1994             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
1995         END IF;
1996 
1997     EXCEPTION
1998         WHEN FND_API.g_exc_error THEN
1999             x_return_status := FND_API.g_ret_sts_error;
2000             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2001                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2002             END IF;
2003             IF ( c_subscr_service_lines%isopen ) THEN
2004                 CLOSE c_subscr_service_lines;
2005             END IF;
2006             RAISE;
2007 
2008         WHEN FND_API.g_exc_unexpected_error THEN
2009             x_return_status := FND_API.g_ret_sts_unexp_error ;
2010             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2011                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2012             END IF;
2013             IF ( c_subscr_service_lines%isopen ) THEN
2014                 CLOSE c_subscr_service_lines;
2015             END IF;
2016             RAISE;
2017 
2018         WHEN OTHERS THEN
2019             x_return_status := FND_API.g_ret_sts_unexp_error ;
2020             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2021                 --first log the sqlerrm
2022                 l_error_text := substr (SQLERRM, 1, 240);
2023                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2024                 --then add it to the message api list
2025                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2026             END IF;
2027             IF ( c_subscr_service_lines%isopen ) THEN
2028                 CLOSE c_subscr_service_lines;
2029             END IF;
2030             RAISE;
2031     END RECREATE_COV_SUBSCR;
2032 
2033     /*
2034     Internal procedure for copying the usage price locks during renewal. Note: the copy API does not
2035     copy any usage price locks during copy, only after we have obtained the renewal price list, can we
2036     copy the usage price locks.
2037     Parameters
2038         p_chr_id            : id of the renewed contract
2039         p_org_id            : org id of the renewed contract
2040         p_contract_number   : number of the renewed contract
2041 
2042     The logic of copying usage price locks is simple
2043         1. Get the old line's price list and locked price list id and the new line's price list
2044         2. If old locked price list id is not null (i.e., old line had a price lock)
2045            and new price list = old price list
2046            a. Call QP api to lock price list (with the new contract number)
2047            b. Update new oks lines with the price lock information (locked price list id
2048               and locked price list line id)
2049     */
2050     PROCEDURE COPY_USAGE_PRICE_LOCKS
2051     (
2052      p_chr_id IN NUMBER,
2053      p_org_id IN NUMBER,
2054      p_contract_number IN VARCHAR2,
2055      x_msg_count OUT NOCOPY NUMBER,
2056      x_msg_data OUT NOCOPY VARCHAR2,
2057      x_return_status OUT NOCOPY VARCHAR2
2058     )
2059     IS
2060     l_api_name CONSTANT VARCHAR2(30) := 'COPY_USAGE_PRICE_LOCKS';
2061     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2062     l_error_text VARCHAR2(512);
2063 
2064     l_old_lpll_tbl              num_tbl_type;
2065     l_new_cid_tbl               num_tbl_type;
2066 
2067     l_locked_price_list_id      NUMBER;
2068     l_locked_price_list_line_id NUMBER;
2069 
2070     l_new_lpl_tbl               num_tbl_type;
2071     l_new_lpll_tbl              num_tbl_type;
2072     l_old_break_uom_tbl         chr_tbl_type;
2073 
2074     CURSOR c_get_usage_price_locks(cp_chr_id IN NUMBER) IS
2075         SELECT olds.locked_price_list_line_id, newc.id, nvl(olds.break_uom, 'X')
2076         FROM okc_k_lines_b oldc, oks_k_lines_b olds, okc_k_lines_b newc
2077         WHERE newc.dnz_chr_id = cp_chr_id AND newc.lse_id IN (12, 13)
2078         AND oldc.id = newc.orig_system_id1 AND olds.cle_id = oldc.id
2079         AND olds.locked_price_list_id IS NOT NULL
2080         AND nvl(oldc.price_list_id, -99) = nvl(newc.price_list_id, -98);
2081 
2082     BEGIN
2083 
2084         --log key input parameters
2085         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2086             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_org_id='||p_org_id||' ,p_contract_number='||p_contract_number);
2087         END IF;
2088 
2089         x_return_status := FND_API.G_RET_STS_SUCCESS;
2090 
2091         OPEN c_get_usage_price_locks(p_chr_id);
2092         LOOP
2093             FETCH c_get_usage_price_locks BULK COLLECT INTO l_old_lpll_tbl, l_new_cid_tbl, l_old_break_uom_tbl LIMIT G_BULK_FETCH_LIMIT;
2094 
2095             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2096                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.usage_locks_loop', 'l_new_cid_tbl.count='||l_new_cid_tbl.count);
2097             END IF;
2098             EXIT WHEN (l_new_cid_tbl.count = 0);
2099 
2100             --lock prices for each usage line
2101             FOR i IN l_new_cid_tbl.first..l_new_cid_tbl.last LOOP
2102                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2103                     FND_LOG.string(FND_LOG.level_statement, l_mod_name||'usage_locks_loop', 'i='||i||' ,l_old_lpll_tbl(i)='||l_old_lpll_tbl(i));
2104                 END IF;
2105 
2106                 l_locked_price_list_id := null;
2107                 l_locked_price_list_line_id := null;
2108                 l_new_lpl_tbl(i) := null;
2109                 l_new_lpll_tbl(i) := null;
2110 
2111                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2112                     FND_LOG.string(FND_LOG.level_statement, l_mod_name||'usage_locks_loop', 'calling QP_LOCK_PRICELIST_GRP.lock_price, p_source_list_line_id='||l_old_lpll_tbl(i)||' ,p_list_source_code=OKS ,p_orig_system_header_ref='||p_contract_number);
2113                 END IF;
2114 
2115                 --no bulk price lock api
2116                 QP_LOCK_PRICELIST_GRP.lock_price(
2117                     p_source_list_line_id => l_old_lpll_tbl(i),
2118                     p_list_source_code => 'OKS',
2119                     p_orig_system_header_ref => p_contract_number,
2120                     p_org_id => p_org_id,
2121                     x_locked_price_list_id => l_locked_price_list_id,
2122                     x_locked_list_line_id => l_locked_price_list_line_id,
2123                     x_return_status => x_return_status,
2124                     x_msg_count => x_msg_count,
2125                     x_msg_data => x_msg_data);
2126 
2127                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2128                     FND_LOG.string(FND_LOG.level_statement, l_mod_name||'usage_locks_loop', 'after call to QP_LOCK_PRICELIST_GRP.lock_price, x_return_status='||x_return_status||
2129                     ' ,x_locked_price_list_id='||l_locked_price_list_id||' ,x_locked_list_line_id='||l_locked_price_list_line_id);
2130                 END IF;
2131 
2132                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2133                     RAISE FND_API.g_exc_unexpected_error;
2134                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2135                     RAISE FND_API.g_exc_error;
2136                 END IF;
2137 
2138                 l_new_lpl_tbl(i) := l_locked_price_list_id;
2139                 l_new_lpll_tbl(i) := l_locked_price_list_line_id;
2140                 --we select X if it was null to keep pl/sql table indexes in sync
2141                 IF (l_old_break_uom_tbl(i) = 'X') THEN
2142                     l_old_break_uom_tbl(i) := null;
2143                 END IF;
2144 
2145             END LOOP; --of FOR i IN l_new_cid_tbl.first..l_new_cid_tbl.last LOOP
2146 
2147             FORALL i IN l_new_cid_tbl.first..l_new_cid_tbl.last
2148                 UPDATE oks_k_lines_b
2149                     SET locked_price_list_id = l_new_lpl_tbl(i),
2150                         locked_price_list_line_id = l_new_lpll_tbl(i),
2151                         break_uom = l_old_break_uom_tbl(i)
2152                     WHERE cle_id = l_new_cid_tbl(i);
2153 
2154         END LOOP; --of top line bulk fetch loop
2155         CLOSE c_get_usage_price_locks;
2156         l_old_lpll_tbl.delete;
2157         l_new_cid_tbl.delete;
2158         l_new_lpl_tbl.delete;
2159         l_new_lpll_tbl.delete;
2160         l_old_break_uom_tbl.delete;
2161 
2162         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2163             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
2164         END IF;
2165 
2166     EXCEPTION
2167         WHEN FND_API.g_exc_error THEN
2168             x_return_status := FND_API.g_ret_sts_error;
2169             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2170                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2171             END IF;
2172             IF ( c_get_usage_price_locks%isopen ) THEN
2173                 CLOSE c_get_usage_price_locks;
2174             END IF;
2175             RAISE;
2176 
2177         WHEN FND_API.g_exc_unexpected_error THEN
2178             x_return_status := FND_API.g_ret_sts_unexp_error ;
2179             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2180                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2181             END IF;
2182             IF ( c_get_usage_price_locks%isopen ) THEN
2183                 CLOSE c_get_usage_price_locks;
2184             END IF;
2185             RAISE;
2186 
2187         WHEN OTHERS THEN
2188             x_return_status := FND_API.g_ret_sts_unexp_error ;
2189             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2190                 --first log the sqlerrm
2191                 l_error_text := substr (SQLERRM, 1, 240);
2192                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2193                 --then add it to the message api list
2194                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2195             END IF;
2196             IF ( c_get_usage_price_locks%isopen ) THEN
2197                 CLOSE c_get_usage_price_locks;
2198             END IF;
2199             RAISE;
2200     END COPY_USAGE_PRICE_LOCKS;
2201 
2202     /*
2203     Internal procedure for recreating header billing schedule. Called after the contract line dates
2204     have been adjusted and the contract has been repriced using the renewal pricing method.
2205     Parameters
2206         p_chr_id                : id of the renewed contract
2207         p_old_chr_id            : id of the source contract
2208     */
2209     PROCEDURE RECREATE_HDR_BILLING
2210     (
2211      p_chr_id IN NUMBER,
2212      p_old_chr_id IN NUMBER,
2213      x_msg_count OUT NOCOPY NUMBER,
2214      x_msg_data OUT NOCOPY VARCHAR2,
2215      x_return_status OUT NOCOPY VARCHAR2
2216     )
2217     IS
2218     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_HDR_BILLING';
2219     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2220     l_error_text VARCHAR2(512);
2221 
2222     CURSOR c_hdr_strlvl(cp_chr_id NUMBER) IS
2223         SELECT id, chr_id, cle_id, dnz_chr_id, sequence_no, uom_code, start_date, level_periods,
2224         uom_per_period, advance_periods, level_amount, invoice_offset_days, interface_offset_days,
2225         comments, due_arr_yn, amount, lines_detailed_yn
2226         FROM oks_stream_levels_b
2227         WHERE chr_id = cp_chr_id
2228         ORDER BY sequence_no;   --skuchima bug 11805576
2229 
2230     TYPE hdr_strlvl_tbl IS TABLE OF c_hdr_strlvl%ROWTYPE INDEX BY BINARY_INTEGER;
2231 
2232     l_hdr_strlvl_tbl    hdr_strlvl_tbl;
2233     l_sllv_tbl          OKS_SLL_PVT.sllv_tbl_type;
2234     x_sllv_tbl          OKS_SLL_PVT.sllv_tbl_type;
2235     l_sll_ctr           NUMBER := 0;
2236 
2237     BEGIN
2238         --log key input parameters
2239         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2240             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_old_chr_id='||p_old_chr_id);
2241         END IF;
2242         x_return_status := FND_API.G_RET_STS_SUCCESS;
2243 
2244         OPEN c_hdr_strlvl(p_old_chr_id);
2245         LOOP
2246             FETCH c_hdr_strlvl BULK COLLECT INTO l_hdr_strlvl_tbl LIMIT G_BULK_FETCH_LIMIT;
2247             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2248                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_hdr_sll', 'l_hdr_strlvl_tbl.count=' || l_hdr_strlvl_tbl.count);
2249             END IF;
2250             EXIT WHEN (l_hdr_strlvl_tbl.count = 0);
2251 
2252             FOR i IN l_hdr_strlvl_tbl.first..l_hdr_strlvl_tbl.last LOOP
2253                 l_sll_ctr := l_sll_ctr + 1;
2254                 l_sllv_tbl(l_sll_ctr).id := OKC_API.g_miss_num;
2255                 l_sllv_tbl(l_sll_ctr).chr_id := p_chr_id;
2256                 l_sllv_tbl(l_sll_ctr).cle_id := null;
2257                 l_sllv_tbl(l_sll_ctr).dnz_chr_id := p_chr_id;
2258                 l_sllv_tbl(l_sll_ctr).sequence_no := l_hdr_strlvl_tbl(i).sequence_no;
2259                 l_sllv_tbl(l_sll_ctr).uom_code := l_hdr_strlvl_tbl(i).uom_code;
2260                 l_sllv_tbl(l_sll_ctr).start_date := l_hdr_strlvl_tbl(i).start_date;
2261                 l_sllv_tbl(l_sll_ctr).level_periods := l_hdr_strlvl_tbl(i).level_periods;
2262                 l_sllv_tbl(l_sll_ctr).uom_per_period := l_hdr_strlvl_tbl(i).uom_per_period;
2263                 l_sllv_tbl(l_sll_ctr).advance_periods := l_hdr_strlvl_tbl(i).advance_periods;
2264                 l_sllv_tbl(l_sll_ctr).level_amount := l_hdr_strlvl_tbl(i).level_amount;
2265                 l_sllv_tbl(l_sll_ctr).invoice_offset_days := l_hdr_strlvl_tbl(i).invoice_offset_days;
2266                 l_sllv_tbl(l_sll_ctr).interface_offset_days := l_hdr_strlvl_tbl(i).interface_offset_days;
2267                 l_sllv_tbl(l_sll_ctr).comments := l_hdr_strlvl_tbl(i).comments;
2268                 l_sllv_tbl(l_sll_ctr).due_arr_yn := l_hdr_strlvl_tbl(i).due_arr_yn;
2269                 l_sllv_tbl(l_sll_ctr).amount := l_hdr_strlvl_tbl(i).amount;
2270                 l_sllv_tbl(l_sll_ctr).lines_detailed_yn := l_hdr_strlvl_tbl(i).lines_detailed_yn;
2271             END LOOP;
2272 
2273         END LOOP; --hdr bulk fetch loop
2274         CLOSE c_hdr_strlvl;
2275         l_hdr_strlvl_tbl.delete;
2276 
2277         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2278             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_hdr_sll', 'l_sllv_tbl.count=' || l_sllv_tbl.count);
2279         END IF;
2280 
2281         IF (l_sllv_tbl.count > 0) THEN
2282 
2283             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2284                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_hdr_sll', 'calling OKS_CONTRACT_SLL_PUB.create_sll');
2285             END IF;
2286 
2287             OKS_CONTRACT_SLL_PUB.create_sll (
2288                 p_api_version => 1,
2289                 p_init_msg_list => FND_API.G_FALSE,
2290                 x_return_status => x_return_status,
2291                 x_msg_count => x_msg_count,
2292                 x_msg_data => x_msg_data,
2293                 p_sllv_tbl => l_sllv_tbl,
2294                 x_sllv_tbl => x_sllv_tbl,
2295                 p_validate_yn => 'N');
2296 
2297             l_sllv_tbl.delete;
2298             x_sllv_tbl.delete;
2299 
2300             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2301                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_hdr_sll', 'after call to OKS_CONTRACT_SLL_PUB.create_sll, x_return_status='||x_return_status);
2302             END IF;
2303 
2304             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2305                 RAISE FND_API.g_exc_unexpected_error;
2306             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2307                 RAISE FND_API.g_exc_error;
2308             END IF;
2309 
2310             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2311                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_hdr_sll', 'calling OKS_BILL_SCH.create_hdr_schedule');
2312             END IF;
2313 
2314             OKS_BILL_SCH.create_hdr_schedule(
2315                 p_contract_id => p_chr_id,
2316                 x_return_status => x_return_status,
2317                 x_msg_count => x_msg_count,
2318                 x_msg_data => x_msg_data);
2319 
2320             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2321                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_hdr_sll', 'after call to  OKS_BILL_SCH.create_hdr_schedule, x_return_status='||x_return_status);
2322             END IF;
2323 
2324             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2325                 RAISE FND_API.g_exc_unexpected_error;
2326             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2327                 RAISE FND_API.g_exc_error;
2328             END IF;
2329 
2330         END IF; --of IF (l_sllv_tbl.count > 0) THEN
2331 
2332 
2333         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2334             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
2335         END IF;
2336 
2337     EXCEPTION
2338         WHEN FND_API.g_exc_error THEN
2339             x_return_status := FND_API.g_ret_sts_error;
2340             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2341                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2342             END IF;
2343             IF (c_hdr_strlvl%isopen) THEN
2344                 CLOSE c_hdr_strlvl;
2345             END IF;
2346             RAISE;
2347 
2348         WHEN FND_API.g_exc_unexpected_error THEN
2349             x_return_status := FND_API.g_ret_sts_unexp_error ;
2350             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2351                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2352             END IF;
2353             IF (c_hdr_strlvl%isopen) THEN
2354                 CLOSE c_hdr_strlvl;
2355             END IF;
2356             RAISE;
2357 
2358         WHEN OTHERS THEN
2359             x_return_status := FND_API.g_ret_sts_unexp_error ;
2360             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2361                 --first log the sqlerrm
2362                 l_error_text := substr (SQLERRM, 1, 240);
2363                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2364                 --then add it to the message api list
2365                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2366             END IF;
2367             IF (c_hdr_strlvl%isopen) THEN
2368                 CLOSE c_hdr_strlvl;
2369             END IF;
2370             RAISE;
2371     END RECREATE_HDR_BILLING;
2372 
2373 
2374     /*
2375     Internal procedure for recreating line billing schedule. Called after the contract line dates
2376     have been adjusted and the contract has been repriced using the renewal pricing method.
2377     Parameters
2378         p_chr_id                : id of the renewed contract
2379         p_old_chr_id            : id of the source contract
2380     */
2381     PROCEDURE RECREATE_LINE_BILLING
2382     (
2383      p_chr_id IN NUMBER,
2384      x_msg_count OUT NOCOPY NUMBER,
2385      x_msg_data OUT NOCOPY VARCHAR2,
2386      x_return_status OUT NOCOPY VARCHAR2
2387     )
2388     IS
2389     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_LINE_BILLING';
2390     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2391     l_error_text VARCHAR2(512);
2392 
2393     CURSOR c_get_top_lines(cp_chr_id IN NUMBER) IS
2394         SELECT renc.id, renc.inv_rule_id, renc.orig_system_id1, rens.billing_schedule_type,
2395         nvl(renc.price_negotiated,0) new_line_amt,
2396         (nvl(oldc.price_negotiated, 0) + nvl(olds.ubt_amount, 0) +
2397          nvl(olds.credit_amount, 0) + nvl(olds.suppressed_credit, 0) ) old_line_amt
2398         FROM okc_k_lines_b renc, oks_k_lines_b rens,
2399             okc_k_lines_b oldc, oks_k_lines_b olds
2400         WHERE renc.dnz_chr_id = cp_chr_id
2401         AND renc.cle_id IS NULL AND renc.lse_id IN (1,12,19,46) AND rens.cle_id = renc.id
2402         AND oldc.id = renc.orig_system_id1
2403         AND olds.cle_id = renc.orig_system_id1;
2404 
2405     TYPE top_line_tbl IS TABLE OF c_get_top_lines%ROWTYPE INDEX BY BINARY_INTEGER;
2406 
2407     CURSOR c_line_strlvl(cp_cle_id NUMBER) IS
2408         SELECT id, chr_id, cle_id, dnz_chr_id, sequence_no, uom_code, start_date, end_date,
2409         level_periods, uom_per_period, advance_periods, level_amount, invoice_offset_days,
2410         interface_offset_days, comments, due_arr_yn, amount, lines_detailed_yn
2411         FROM oks_stream_levels_b
2412         WHERE cle_id = cp_cle_id
2413         ORDER BY sequence_no;  ---skuchima bug 11805576
2414 
2415 
2416     TYPE line_strlvl_tbl IS TABLE OF c_line_strlvl%ROWTYPE INDEX BY BINARY_INTEGER;
2417 
2418     l_top_line_tbl      top_line_tbl;
2419     l_line_strlvl_tbl   line_strlvl_tbl;
2420 
2421     l_line_sllv_tbl     OKS_BILL_SCH.streamlvl_tbl;
2422     l_bil_sch_out_tbl   OKS_BILL_SCH.itembillsch_tbl;
2423     l_line_sll_ctr      NUMBER := 0;
2424 
2425     BEGIN
2426         --log key input parameters
2427         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2428             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
2429         END IF;
2430         x_return_status := FND_API.G_RET_STS_SUCCESS;
2431 
2432         OPEN c_get_top_lines(p_chr_id);
2433         LOOP
2434             FETCH  c_get_top_lines BULK COLLECT INTO l_top_line_tbl LIMIT G_BULK_FETCH_LIMIT;
2435 
2436             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2437                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_toplines', 'l_top_line_tbl.count=' || l_top_line_tbl.count);
2438             END IF;
2439 
2440             EXIT WHEN (l_top_line_tbl.count = 0);
2441 
2442             --for each topline and it's sublines recreate the billing schedule
2443             FOR i IN l_top_line_tbl.first..l_top_line_tbl.last LOOP
2444 
2445                  IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2446                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.topline_billing', 'i='||i||
2447                     ' id='||l_top_line_tbl(i).id||
2448                     ' ,billing_schedule_type='||l_top_line_tbl(i).billing_schedule_type||
2449                     ' ,inv_rule_id='||l_top_line_tbl(i).inv_rule_id||
2450                     ',new_line_amt='||l_top_line_tbl(i).new_line_amt||
2451                     ',old_line_amt='||l_top_line_tbl(i).old_line_amt);
2452                 END IF;
2453 
2454                 --initialize before every top line
2455                 l_line_sll_ctr := 0;
2456                 l_line_sllv_tbl.delete;
2457                 l_line_strlvl_tbl.delete;
2458 
2459                 --get the old billing schedule rule for the topline
2460                 OPEN c_line_strlvl(l_top_line_tbl(i).orig_system_id1);
2461                 LOOP
2462                     FETCH c_line_strlvl BULK COLLECT INTO l_line_strlvl_tbl LIMIT G_BULK_FETCH_LIMIT;
2463 
2464                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2465                         FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_topline_sll', 'i='||i||'l_top_line_tbl(i).orig_system_id1='||l_top_line_tbl(i).orig_system_id1||' ,l_line_strlvl_tbl.count=' || l_line_strlvl_tbl.count);
2466                     END IF;
2467 
2468                     EXIT WHEN (l_line_strlvl_tbl.count = 0);
2469 
2470 
2471                     FOR j IN l_line_strlvl_tbl.first..l_line_strlvl_tbl.last LOOP
2472                         l_line_sll_ctr := l_line_sll_ctr + 1;
2473 
2474                         l_line_sllv_tbl(l_line_sll_ctr).id := FND_API.g_miss_num;
2475                         l_line_sllv_tbl(l_line_sll_ctr).chr_id := FND_API.g_miss_num;
2476                         l_line_sllv_tbl(l_line_sll_ctr).cle_id := l_top_line_tbl(i).id;
2477                         l_line_sllv_tbl(l_line_sll_ctr).dnz_chr_id := p_chr_id;
2478                         l_line_sllv_tbl(l_line_sll_ctr).sequence_no := l_line_strlvl_tbl(j).sequence_no;
2479                         l_line_sllv_tbl(l_line_sll_ctr).uom_code := l_line_strlvl_tbl(j).uom_code;
2480                         l_line_sllv_tbl(l_line_sll_ctr).start_date := l_line_strlvl_tbl(j).start_date;
2481                         l_line_sllv_tbl(l_line_sll_ctr).end_date := l_line_strlvl_tbl(j).end_date;
2482                         l_line_sllv_tbl(l_line_sll_ctr).level_periods := l_line_strlvl_tbl(j).level_periods;
2483                         l_line_sllv_tbl(l_line_sll_ctr).uom_per_period := l_line_strlvl_tbl(j).uom_per_period;
2484                         l_line_sllv_tbl(l_line_sll_ctr).advance_periods := l_line_strlvl_tbl(j).advance_periods;
2485                         l_line_sllv_tbl(l_line_sll_ctr).level_amount := l_line_strlvl_tbl(j).level_amount;
2486                         l_line_sllv_tbl(l_line_sll_ctr).invoice_offset_days := l_line_strlvl_tbl(j).invoice_offset_days;
2487                         l_line_sllv_tbl(l_line_sll_ctr).interface_offset_days := l_line_strlvl_tbl(j).interface_offset_days;
2488                         l_line_sllv_tbl(l_line_sll_ctr).comments := l_line_strlvl_tbl(j).comments;
2489                         l_line_sllv_tbl(l_line_sll_ctr).due_arr_yn := l_line_strlvl_tbl(j).due_arr_yn;
2490                         l_line_sllv_tbl(l_line_sll_ctr).amount := l_line_strlvl_tbl(j).amount;
2491                         l_line_sllv_tbl(l_line_sll_ctr).lines_detailed_yn := l_line_strlvl_tbl(j).lines_detailed_yn;
2492 
2493                         --set the comments to 99 if old line amt <> new line amt for E and P
2494                         --some billing code depends on this
2495                         IF l_top_line_tbl(i).billing_schedule_type IN ('E', 'P') THEN
2496                             IF ( l_top_line_tbl(i).old_line_amt <> l_top_line_tbl(i).new_line_amt ) THEN
2497                                 l_line_sllv_tbl(l_line_sll_ctr).comments := '99';
2498                             ELSE
2499                                 l_line_sllv_tbl(l_line_sll_ctr).comments := NULL;
2500                             END IF;
2501                         END IF;
2502 
2503                     END LOOP; --topline sll loopFOR j IN l_line_strlvl_tbl.first..l_line_strlvl_tbl.last LOOP
2504 
2505                 END LOOP; --topline sll bulk fecth
2506                 CLOSE c_line_strlvl;
2507                 l_line_strlvl_tbl.delete;
2508 
2509                 --call the billing api to create the billing schedule for the topline and it's sublines
2510                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2511                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.topline_billing', 'calling OKS_BILL_SCH.create_bill_sch_rules');
2512                 END IF;
2513 
2514                 OKS_BILL_SCH.create_bill_sch_rules(
2515                     p_billing_type => l_top_line_tbl(i).billing_schedule_type,
2516                     p_sll_tbl => l_line_sllv_tbl,
2517                     p_invoice_rule_id => l_top_line_tbl(i).inv_rule_id,
2518                     x_bil_sch_out_tbl => l_bil_sch_out_tbl,
2519                     x_return_status => x_return_status);
2520 
2521                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2522                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.topline_billing', 'after call to OKS_BILL_SCH.create_bill_sch_rules, x_return_status='||x_return_status);
2523                 END IF;
2524 
2525                 l_bil_sch_out_tbl.delete;
2526                 l_line_sllv_tbl.delete;
2527 
2528                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2529                     RAISE FND_API.g_exc_unexpected_error;
2530                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2531                     RAISE FND_API.g_exc_error;
2532                 END IF;
2533 
2534             END LOOP; --topline loop --FOR i IN l_top_line_tbl.first..l_top_line_tbl.last LOOP
2535 
2536         END LOOP; --topline bulk fetch loop
2537         CLOSE c_get_top_lines;
2538         l_top_line_tbl.delete;
2539 
2540         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2541             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
2542         END IF;
2543 
2544     EXCEPTION
2545         WHEN FND_API.g_exc_error THEN
2546             x_return_status := FND_API.g_ret_sts_error;
2547             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2548                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2549             END IF;
2550             IF (c_get_top_lines%isopen) THEN
2551                 CLOSE c_get_top_lines;
2552             END IF;
2553             IF (c_line_strlvl%isopen) THEN
2554                 CLOSE c_line_strlvl;
2555             END IF;
2556             RAISE;
2557 
2558         WHEN FND_API.g_exc_unexpected_error THEN
2559             x_return_status := FND_API.g_ret_sts_unexp_error ;
2560             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2561                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2562             END IF;
2563             IF (c_get_top_lines%isopen) THEN
2564                 CLOSE c_get_top_lines;
2565             END IF;
2566             IF (c_line_strlvl%isopen) THEN
2567                 CLOSE c_line_strlvl;
2568             END IF;
2569             RAISE;
2570 
2571         WHEN OTHERS THEN
2572             x_return_status := FND_API.g_ret_sts_unexp_error ;
2573             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2574                 --first log the sqlerrm
2575                 l_error_text := substr (SQLERRM, 1, 240);
2576                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2577                 --then add it to the message api list
2578                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2579             END IF;
2580             IF (c_get_top_lines%isopen) THEN
2581                 CLOSE c_get_top_lines;
2582             END IF;
2583             IF (c_line_strlvl%isopen) THEN
2584                 CLOSE c_line_strlvl;
2585             END IF;
2586             RAISE;
2587     END RECREATE_LINE_BILLING;
2588 
2589     /*
2590     Internal procedure for recreating header billing schedule. Called after the contract line dates
2591     have been adjusted and the contract has been repriced using the renewal pricing method.
2592     Parameters
2593         p_chr_id                : id of the renewed contract
2594         p_old_chr_id            : id of the source contract
2595     */
2596     PROCEDURE RECREATE_BILLING_FROM_BP
2597     (
2598      p_chr_id IN NUMBER,
2599      p_billing_profile_id IN NUMBER,
2600      x_msg_count OUT NOCOPY NUMBER,
2601      x_msg_data OUT NOCOPY VARCHAR2,
2602      x_return_status OUT NOCOPY VARCHAR2
2603     )
2604     IS
2605     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_BILLING_FROM_BP';
2606     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2607     l_error_text VARCHAR2(512);
2608 
2609     CURSOR c_bp_toplines (cp_chr_id IN NUMBER) IS
2610         SELECT a.id, a.start_date, a.end_date, nvl(b.billing_schedule_type, 'XX'),
2611         a.lse_id, nvl(b.usage_type, 'XX')
2612         FROM   okc_k_lines_b a, oks_k_lines_b b
2613         WHERE  a.dnz_chr_id = cp_chr_id AND a.id = b.cle_id
2614           AND  a.cle_id IS NULL;
2615 
2616     CURSOR c_chk_accounting_rule(l_id NUMBER) IS
2617         SELECT RULE_ID
2618         FROM RA_RULES
2619         WHERE TYPE IN ('A', 'ACC_DUR')
2620         AND RULE_ID = l_id;
2621 
2622     CURSOR c_chk_invoice_rule(l_id NUMBER) IS
2623         SELECT RULE_ID
2624         FROM RA_RULES
2625         WHERE  TYPE = 'I'
2626         AND RULE_ID = l_id;
2627 
2628     l_id_tbl            num_tbl_type;
2629     l_start_dt_tbl      date_tbl_type;
2630     l_end_dt_tbl        date_tbl_type;
2631     l_bsch_typ_tbl      chr_tbl_type;
2632     l_lse_id_tbl        num_tbl_type;
2633     l_usage_typ_tbl     chr_tbl_type;
2634 
2635     l_rec               OKS_BILLING_PROFILES_PUB.billing_profile_rec;
2636     l_sll_tbl_out       OKS_BILLING_PROFILES_PUB.stream_level_tbl;
2637 
2638     l_sll_tbl           OKS_BILL_SCH.streamlvl_tbl;
2639     l_bil_sch_out_tbl   OKS_BILL_SCH.itembillsch_tbl;
2640 
2641     l_invoice_rule_id   NUMBER;
2642     l_account_rule_id   NUMBER;
2643     l_rule_id           NUMBER;
2644     l_var_usg_typ_flag  BOOLEAN := FALSE;
2645 
2646     BEGIN
2647         --log key input parameters
2648         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2649             FND_LOG.string(FND_LOG.level_procedure, l_mod_name, 'begin p_chr_id=' || p_chr_id||' , p_billing_profile_id='||p_billing_profile_id);
2650         END IF;
2651         x_return_status := FND_API.G_RET_STS_SUCCESS;
2652 
2653         OPEN c_bp_toplines(p_chr_id);
2654         LOOP
2655 
2656             FETCH c_bp_toplines BULK COLLECT INTO l_id_tbl, l_start_dt_tbl, l_end_dt_tbl, l_bsch_typ_tbl, l_lse_id_tbl, l_usage_typ_tbl LIMIT G_BULK_FETCH_LIMIT;
2657             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2658                 FND_LOG.string(FND_LOG.level_statement, l_mod_name ,'get_toplines : l_id_tbl.count=' || l_id_tbl.count);
2659             END IF;
2660             EXIT WHEN (l_id_tbl.count = 0);
2661 
2662             --for each topline
2663             FOR i IN l_id_tbl.first..l_id_tbl.last LOOP
2664                 l_rec.cle_id := l_id_tbl(i);
2665                 l_rec.chr_id := p_chr_id;
2666                 l_rec.billing_profile_id := p_billing_profile_id;
2667                 l_rec.start_date := l_start_dt_tbl(i);
2668                 l_rec.end_date := l_end_dt_tbl(i);
2669 
2670                 IF (l_bsch_typ_tbl(i) = 'XX') THEN
2671                     l_bsch_typ_tbl(i) := NULL;
2672                 END IF;
2673 
2674                 --get the billing profile based sll
2675                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2676                     FND_LOG.string(FND_LOG.level_statement, l_mod_name,'get_tl_bill_sch : i='||i||
2677                     ' calling OKS_BILLING_PROFILES_PUB.get_billing_schedule, l_rec.cle_id='||l_rec.cle_id||
2678                     ' ,l_rec.start_date='||l_rec.start_date||' ,l_rec.end_date='||l_rec.end_date);
2679                 END IF;
2680 
2681                 OKS_BILLING_PROFILES_PUB.get_billing_schedule(
2682                     p_api_version => 1.0,
2683                     p_init_msg_list => FND_API.G_FALSE,
2684                     p_billing_profile_rec => l_rec,
2685                     x_sll_tbl_out => l_sll_tbl_out,
2686                     x_return_status => x_return_status,
2687                     x_msg_count => x_msg_count,
2688                     x_msg_data => x_msg_data);
2689 
2690                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2691                     FND_LOG.string(FND_LOG.level_statement, l_mod_name,' get_tl_bill_sch: i='||i||' after call to OKS_BILLING_PROFILES_PUB.get_billing_schedule, x_return_status='||x_return_status||' ,l_sll_tbl_out.count='||l_sll_tbl_out.count);
2692                 END IF;
2693 
2694                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2695                     RAISE FND_API.g_exc_unexpected_error;
2696                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2697                     RAISE FND_API.g_exc_error;
2698                 END IF;
2699 
2700                 --note  OKS_BILLING_PROFILES_PUB.get_billing_schedule always returns
2701                 --one row and the  values for invoice_rule_id/account_rule_id don't change for
2702                 --every line, they are the same for a given billing profile.
2703                 --we can therefore validate and update invoice_rule_id/account_rule_id only once
2704                 IF l_sll_tbl_out.COUNT > 0 THEN
2705 
2706                     FOR j IN l_sll_tbl_out.first..l_sll_tbl_out.last LOOP
2707 
2708                         IF( l_invoice_rule_id IS NULL) THEN
2709                             l_invoice_rule_id := l_sll_tbl_out(j).invoice_rule_id;
2710                         END IF;
2711                         IF (l_account_rule_id IS NULL) THEN
2712                             l_account_rule_id := l_sll_tbl_out(j).account_rule_id;
2713                         END IF;
2714 
2715                         l_sll_tbl(j).cle_id := l_sll_tbl_out(j).cle_id;
2716                         l_sll_tbl(j).dnz_chr_id := p_chr_id;
2717                         l_sll_tbl(j).sequence_no := l_sll_tbl_out(j).seq_no;
2718                         l_sll_tbl(j).start_date := l_sll_tbl_out(j).start_date;
2719                         l_sll_tbl(j).level_periods := l_sll_tbl_out(j).target_quantity;
2720                         l_sll_tbl(j).uom_per_period := l_sll_tbl_out(j).duration;
2721                         l_sll_tbl(j).level_amount := l_sll_tbl_out(j).amount;
2722                         l_sll_tbl(j).invoice_offset_days := l_sll_tbl_out(j).invoice_offset;
2723                         l_sll_tbl(j).interface_offset_days := l_sll_tbl_out(j).interface_offset;
2724                         l_sll_tbl(j).uom_code := l_sll_tbl_out(j).timeunit;
2725 
2726                     END LOOP;
2727 
2728                     --create billing schedule for the topline and it's sublines
2729                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2730                         FND_LOG.string(FND_LOG.level_statement, l_mod_name,'create_tl_bill_sch: i='||i||' calling OKS_BILL_SCH.create_bill_sch_rules');
2731                     END IF;
2732 
2733                     --for usage lines with variable usage type (Actual by Qty, Actual by Period)
2734                     --the invoice rule has to be "Arrears" (-3), irrespective of the billing profile value
2735                     IF ( (l_lse_id_tbl(i) = 12) AND (l_usage_typ_tbl(i) IN ('VRT', 'QTY'))
2736                          AND (l_invoice_rule_id <> -3) ) THEN
2737 
2738                         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2739                             FND_LOG.string(FND_LOG.level_statement, l_mod_name,'var_usage_chk: i='||i||'  ,l_invoice_rule_id='||l_invoice_rule_id||' ,l_usage_typ_tbl(i)='||l_usage_typ_tbl(i)||' ,id='||l_id_tbl(i));
2740                         END IF;
2741 
2742                         l_var_usg_typ_flag := TRUE;
2743                         OKS_BILL_SCH.create_bill_sch_rules(
2744                             p_billing_type => l_bsch_typ_tbl(i),
2745                             p_sll_tbl => l_sll_tbl,
2746                             p_invoice_rule_id => -3,
2747                             x_bil_sch_out_tbl => l_bil_sch_out_tbl,
2748                             x_return_status => x_return_status);
2749                     ELSE
2750                         OKS_BILL_SCH.create_bill_sch_rules(
2751                             p_billing_type => l_bsch_typ_tbl(i),
2752                             p_sll_tbl => l_sll_tbl,
2753                             p_invoice_rule_id => l_invoice_rule_id,
2754                             x_bil_sch_out_tbl => l_bil_sch_out_tbl,
2755                             x_return_status => x_return_status);
2756                     END IF;
2757 
2758                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2759                         FND_LOG.string(FND_LOG.level_statement, l_mod_name, 'create_tl_bill_sch: i='||i||' after call to OKS_BILL_SCH.create_bill_sch_rules, x_return_status='||x_return_status);
2760                     END IF;
2761 
2762                     IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2763                         RAISE FND_API.g_exc_unexpected_error;
2764                     ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2765                         RAISE FND_API.g_exc_error;
2766                     END IF;
2767 
2768                 END IF; --of IF l_sll_tbl_out.COUNT > 0 THEN
2769                 l_sll_tbl_out.delete;
2770                 l_sll_tbl.delete;
2771                 l_bil_sch_out_tbl.delete;
2772 
2773             END LOOP; --of  FOR i IN l_id_tbl.first..l_id_tbl.last LOOP --toplines
2774 
2775         END LOOP; --bulk fetch loop for toplines
2776         CLOSE c_bp_toplines;
2777         l_id_tbl.delete;
2778         l_start_dt_tbl.delete;
2779         l_end_dt_tbl.delete;
2780         l_bsch_typ_tbl.delete;
2781 
2782       -- bug 5112991
2783       -- If there are NO top lines then bypass the below code
2784       IF l_id_tbl.count <> 0 THEN
2785 
2786         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2787             FND_LOG.string(FND_LOG.level_statement, l_mod_name, 'validate_invoice_rule : l_invoice_rule_id='||l_invoice_rule_id);
2788         END IF;
2789 
2790         l_rule_id := NULL;
2791         OPEN c_chk_invoice_rule(l_invoice_rule_id);
2792         FETCH c_chk_invoice_rule INTO l_rule_id;
2793         CLOSE c_chk_invoice_rule;
2794 
2795         IF(l_rule_id IS NULL) THEN
2796             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_INVOICE_RULE');
2797             FND_MESSAGE.set_token('INVOICE_RULE_ID', l_invoice_rule_id);
2798             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2799                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.validate_invoice_rule', FALSE);
2800             END IF;
2801             FND_MSG_PUB.ADD;
2802             RAISE FND_API.g_exc_error;
2803         END IF;
2804 
2805         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2806             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.validate_accounting_rule', 'l_account_rule_id='||l_account_rule_id);
2807         END IF;
2808 
2809         l_rule_id := NULL;
2810         OPEN c_chk_accounting_rule(l_account_rule_id);
2811         FETCH c_chk_accounting_rule INTO l_rule_id;
2812         CLOSE c_chk_accounting_rule;
2813 
2814         IF(l_rule_id IS NULL) THEN
2815             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_ACCTG_RULE');
2816             FND_MESSAGE.set_token('ACCTG_RULE_ID', l_account_rule_id);
2817             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2818                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.validate_accounting_rule', FALSE);
2819             END IF;
2820             FND_MSG_PUB.ADD;
2821             RAISE FND_API.g_exc_error;
2822         END IF;
2823 
2824         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2825             FND_LOG.string(FND_LOG.level_statement, l_mod_name,'upd_inv_rul : updating invoice rule');
2826         END IF;
2827         --update okc_k_lines_b toplines with inv_rule_id
2828         UPDATE okc_k_lines_b
2829             SET inv_rule_id = l_invoice_rule_id
2830             WHERE dnz_chr_id = p_chr_id AND cle_id IS NULL;
2831 
2832         --update variarable usage type lines with "Arrears" (-3) invoice rule if billing profile's invoice
2833         --rule is different
2834         IF (l_var_usg_typ_flag) THEN
2835 
2836             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2837                 FND_LOG.string(FND_LOG.level_statement, l_mod_name,'upd_inv_rul : updating usage invoice rule');
2838             END IF;
2839 
2840             UPDATE okc_k_lines_b a
2841             SET a.inv_rule_id = -3
2842             WHERE a.dnz_chr_id = p_chr_id AND a.cle_id IS NULL AND a.lse_id = 12
2843             AND EXISTS (SELECT 1 FROM oks_k_lines_b b
2844                         WHERE b.cle_id = a.id AND b.usage_type IN ('VRT', 'QTY'));
2845         END IF;
2846 
2847         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2848             FND_LOG.string(FND_LOG.level_statement, l_mod_name,'upd_acctg_rul : updating accounting rule');
2849         END IF;
2850         --update oks_k_lines_b toplines with acct_rule_id
2851         UPDATE oks_k_lines_b
2852             SET acct_rule_id = l_account_rule_id
2853             WHERE cle_id IN (SELECT id FROM okc_k_lines_b
2854                 WHERE dnz_chr_id = p_chr_id AND cle_id IS NULL);
2855 
2856       END IF;-- bug 5112991  l_id_tbl.count <> 0 THEN
2857 
2858         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2859             FND_LOG.string(FND_LOG.level_procedure, l_mod_name,'end : x_return_status='|| x_return_status);
2860         END IF;
2861 
2862     EXCEPTION
2863         WHEN FND_API.g_exc_error THEN
2864             x_return_status := FND_API.g_ret_sts_error;
2865             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2866                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2867             END IF;
2868             IF (c_bp_toplines%isopen) THEN
2869                 CLOSE c_bp_toplines;
2870             END IF;
2871             IF (c_chk_invoice_rule%isopen) THEN
2872                 CLOSE c_chk_invoice_rule;
2873             END IF;
2874             IF (c_chk_accounting_rule%isopen) THEN
2875                 CLOSE c_chk_accounting_rule;
2876             END IF;
2877             RAISE;
2878 
2879         WHEN FND_API.g_exc_unexpected_error THEN
2880             x_return_status := FND_API.g_ret_sts_unexp_error ;
2881             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2882                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2883             END IF;
2884             IF (c_bp_toplines%isopen) THEN
2885                 CLOSE c_bp_toplines;
2886             END IF;
2887             IF (c_chk_invoice_rule%isopen) THEN
2888                 CLOSE c_chk_invoice_rule;
2889             END IF;
2890             IF (c_chk_accounting_rule%isopen) THEN
2891                 CLOSE c_chk_accounting_rule;
2892             END IF;
2893             RAISE;
2894 
2895         WHEN OTHERS THEN
2896             x_return_status := FND_API.g_ret_sts_unexp_error ;
2897             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2898                 --first log the sqlerrm
2899                 l_error_text := substr (SQLERRM, 1, 240);
2900                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2901                 --then add it to the message api list
2902                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2903             END IF;
2904             IF (c_bp_toplines%isopen) THEN
2905                 CLOSE c_bp_toplines;
2906             END IF;
2907             IF (c_chk_invoice_rule%isopen) THEN
2908                 CLOSE c_chk_invoice_rule;
2909             END IF;
2910             IF (c_chk_accounting_rule%isopen) THEN
2911                 CLOSE c_chk_accounting_rule;
2912             END IF;
2913             RAISE;
2914     END RECREATE_BILLING_FROM_BP;
2915 
2916     /*
2917     Internal procedure for recreating the billing schedules. Called after the contract line dates
2918     have been adjusted and the contract has been repriced using the renewal pricing method.
2919     Parameters
2920         p_chr_id                : id of the renewed contract
2921         p_billing_profile_id    : number of the renewed contract
2922 
2923     The logic of recreating billing is as follows
2924         1. If renewal rules specify a billing profile
2925             a. Delete all existing billing schedules
2926             b. Recreate billing shcedule using the billing profile parameters
2927         2. If renewed contract duration = old contract duration and no billing profile id specified
2928             a. Recreate billing schedule using existing rules
2929         3. If renewed contract duration <> old contract duration and no billing profile specified
2930             a. Delete all existing billing schedules
2931             b. Such a contract will later fail QA check, as it will have no billing schedule
2932     */
2933     PROCEDURE RECREATE_BILLING
2934     (
2935      p_chr_id IN NUMBER,
2936      p_billing_profile_id IN NUMBER,
2937      x_msg_count OUT NOCOPY NUMBER,
2938      x_msg_data OUT NOCOPY VARCHAR2,
2939      x_return_status OUT NOCOPY VARCHAR2
2940     )
2941     IS
2942     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_BILLING';
2943     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2944     l_error_text VARCHAR2(512);
2945 
2946     CURSOR c_hdr_dates(cp_chr_id IN NUMBER) IS
2947         SELECT ren.start_date, ren.end_date, old.start_date, old.end_date, old.id,
2948         rens.period_type, rens.period_start, olds.period_type, olds.period_start
2949         FROM okc_k_headers_all_b ren, okc_k_headers_all_b old,
2950         oks_k_headers_b rens, oks_k_headers_b olds
2951         WHERE ren.id = cp_chr_id
2952         AND rens.chr_id = ren.id
2953         AND old.id = ren.orig_system_id1
2954         AND olds.chr_id = old.id;
2955 
2956     l_new_start_date    DATE;
2957     l_new_end_date      DATE;
2958     l_old_start_date    DATE;
2959     l_old_end_date      DATE;
2960     l_old_chr_id        NUMBER;
2961     l_new_period_type   oks_k_headers_b.period_type%TYPE;
2962     l_new_period_start  oks_k_headers_b.period_start%TYPE;
2963     l_old_period_type   oks_k_headers_b.period_type%TYPE;
2964     l_old_period_start  oks_k_headers_b.period_start%TYPE;
2965 
2966     l_new_duration      NUMBER;
2967     l_new_period        VARCHAR2(64);
2968     l_old_duration      NUMBER;
2969     l_old_period        VARCHAR2(64);
2970 
2971     BEGIN
2972 
2973         --log key input parameters
2974         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2975             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_billing_profile_id='||p_billing_profile_id);
2976         END IF;
2977 
2978         x_return_status := FND_API.G_RET_STS_SUCCESS;
2979 
2980         OPEN c_hdr_dates(p_chr_id);
2981         FETCH c_hdr_dates INTO l_new_start_date, l_new_end_date, l_old_start_date, l_old_end_date, l_old_chr_id, l_new_period_type, l_new_period_start, l_old_period_type, l_old_period_start;
2982         CLOSE c_hdr_dates;
2983 
2984         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2985             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_duration_new', 'calling OKC_TIME_UTIL_PUB.get_duration, l_new_start_date='||l_new_start_date||' ,l_new_end_date='||l_new_end_date);
2986         END IF;
2987 
2988         OKC_TIME_UTIL_PUB.get_duration(
2989             p_start_date => l_new_start_date,
2990             p_end_date => l_new_end_date,
2991             x_duration => l_new_duration,
2992             x_timeunit => l_new_period,
2993             x_return_status => x_return_status);
2994 
2995         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2996             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_duration_new', 'after call to OKC_TIME_UTIL_PUB.get_duration, x_return_status='||x_return_status||' ,l_new_duration='||l_new_duration||' ,l_new_period='||l_new_period);
2997         END IF;
2998         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2999             RAISE FND_API.g_exc_unexpected_error;
3000         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3001             RAISE FND_API.g_exc_error;
3002         END IF;
3003 
3004         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3005             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_duration_old', 'calling OKC_TIME_UTIL_PUB.get_duration, l_old_start_date='||l_old_start_date||' ,l_old_end_date='||l_old_end_date);
3006         END IF;
3007 
3008         OKC_TIME_UTIL_PUB.get_duration(
3009             p_start_date => l_old_start_date,
3010             p_end_date => l_old_end_date,
3011             x_duration => l_old_duration,
3012             x_timeunit => l_old_period,
3013             x_return_status => x_return_status);
3014 
3015         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3016             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_duration_old', 'after call to OKC_TIME_UTIL_PUB.get_duration, x_return_status='||x_return_status||' ,l_old_duration='||l_old_duration||' ,l_old_period='||l_old_period);
3017         END IF;
3018         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3019             RAISE FND_API.g_exc_unexpected_error;
3020         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3021             RAISE FND_API.g_exc_error;
3022         END IF;
3023 
3024         --delete billing schedules if old duration <> new duration or billing profile specified
3025         IF((l_old_duration <> l_new_duration) OR (l_old_period <> l_new_period) OR
3026             (p_billing_profile_id IS  NOT NULL) ) THEN
3027 
3028             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3029                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.delete_billing', 'p_billing_profile_id='||p_billing_profile_id);
3030             END IF;
3031 
3032             DELETE FROM oks_level_elements
3033                 WHERE dnz_chr_id = p_chr_id;
3034 
3035             DELETE FROM oks_stream_levels_b
3036                 WHERE dnz_chr_id = p_chr_id;
3037 
3038             --doing this becuase this is what OKS_BILL_SCH.del_rul_elements does
3039             --and we are replacing that call
3040             UPDATE oks_k_lines_b
3041                 SET billing_schedule_type = NULL
3042                 WHERE cle_id IN
3043                     (SELECT id FROM OKC_K_LINES_B WHERE dnz_chr_id = p_chr_id
3044                     AND lse_id IN (7,8,9,10,11,35,13,18,25));
3045 
3046             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3047                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.delete_billing', 'done');
3048             END IF;
3049 
3050         END IF; --of IF((l_old_duration <> l_new_duration....
3051 
3052 
3053         --if duration/period match and billing profile is NULL, recreate billing schedule
3054         IF((l_old_duration = l_new_duration) AND (l_old_period = l_new_period) AND
3055             (p_billing_profile_id IS NULL) ) THEN
3056 
3057             --for partial periods, the  old period type and start should also match
3058             --for billing to be recreated
3059             IF( ( nvl(l_new_period_type, 'X') = nvl(l_old_period_type, 'X') ) AND
3060                 ( nvl(l_new_period_start, 'X') = nvl(l_old_period_start, 'X') ) ) THEN
3061 
3062                 --first recreate header billing schedule
3063                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3064                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.dur_match_bp_null', 'calling recreate_hdr_billing, p_chr_id='||p_chr_id||' ,p_old_chr_id='||l_old_chr_id);
3065                 END IF;
3066 
3067                 recreate_hdr_billing(
3068                     p_chr_id => p_chr_id,
3069                     p_old_chr_id => l_old_chr_id,
3070                     x_msg_count => x_msg_count,
3071                     x_msg_data => x_msg_data,
3072                     x_return_status => x_return_status);
3073 
3074                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3075                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.dur_match_bp_null', 'after recreate_hdr_billing, x_return_status='||x_return_status);
3076                 END IF;
3077                 --end of contract header billing schedule
3078 
3079                 --now recreate lines billing schedule
3080                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3081                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.dur_match_bp_null', 'calling recreate_line_billing, p_chr_id='||p_chr_id);
3082                 END IF;
3083 
3084                 recreate_line_billing(
3085                     p_chr_id => p_chr_id,
3086                     x_msg_count => x_msg_count,
3087                     x_msg_data => x_msg_data,
3088                     x_return_status => x_return_status);
3089 
3090                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3091                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.dur_match_bp_null', 'after recreate_line_billing, x_return_status='||x_return_status);
3092                 END IF;
3093                 --end of lines billing schedule
3094 
3095             END IF;
3096 
3097         END IF; --of if duration/period matches and billing profile is null
3098 
3099 
3100         --if  billing profile specified, recreate billing schedule using billing profile
3101         IF (p_billing_profile_id IS NOT NULL) THEN
3102 
3103             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3104                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.bp_not_null', 'calling recreate_billing_from_bp, p_chr_id='||p_chr_id||' ,p_billing_profile_id='||p_billing_profile_id);
3105             END IF;
3106 
3107             recreate_billing_from_bp(
3108                 p_chr_id => p_chr_id,
3109                 p_billing_profile_id => p_billing_profile_id,
3110                 x_msg_count => x_msg_count,
3111                 x_msg_data => x_msg_data,
3112                 x_return_status => x_return_status);
3113 
3114             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3115                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.bp_not_null', 'after call to recreate_billing_from_bp, x_return_status='||x_return_status);
3116             END IF;
3117 
3118         END IF; --IF (p_billing_profile_id IS NOT NULL) THEN
3119 
3120 
3121         --If duration/period don't match and billing profile is NULL, no billing schedule
3122         --is created, such a contract will later fail QA check
3123 
3124         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3125             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3126         END IF;
3127 
3128     EXCEPTION
3129         WHEN FND_API.g_exc_error THEN
3130             x_return_status := FND_API.g_ret_sts_error;
3131             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3132                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3133             END IF;
3134             IF (c_hdr_dates%isopen) THEN
3135                 CLOSE c_hdr_dates;
3136             END IF;
3137             RAISE;
3138 
3139         WHEN FND_API.g_exc_unexpected_error THEN
3140             x_return_status := FND_API.g_ret_sts_unexp_error ;
3141             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3142                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3143             END IF;
3144             IF (c_hdr_dates%isopen) THEN
3145                 CLOSE c_hdr_dates;
3146             END IF;
3147             RAISE;
3148 
3149         WHEN OTHERS THEN
3150             x_return_status := FND_API.g_ret_sts_unexp_error ;
3151             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3152                 --first log the sqlerrm
3153                 l_error_text := substr (SQLERRM, 1, 240);
3154                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3155                 --then add it to the message api list
3156                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3157             END IF;
3158             IF (c_hdr_dates%isopen) THEN
3159                 CLOSE c_hdr_dates;
3160             END IF;
3161             RAISE;
3162 
3163     END RECREATE_BILLING;
3164 
3165 
3166     /*
3167     Internal procedure for assigning a renewed contract to the contract group, if the
3168     renewed contract does not belong to the group
3169         p_chr_id                : id of the renewed contract
3170         p_chr_group_id          : id of the contract group
3171     */
3172     PROCEDURE ASSIGN_CONTRACT_GROUP
3173     (
3174      p_chr_id IN NUMBER,
3175      p_chr_group_id IN NUMBER,
3176      x_msg_count OUT NOCOPY NUMBER,
3177      x_msg_data OUT NOCOPY VARCHAR2,
3178      x_return_status OUT NOCOPY VARCHAR2
3179     )
3180     IS
3181     l_api_name CONSTANT VARCHAR2(30) := 'ASSIGN_CONTRACT_GROUP';
3182     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
3183     l_error_text VARCHAR2(512);
3184 
3185     CURSOR c_group_csr(cp_chr_id NUMBER, cp_grp_id IN NUMBER) IS
3186         SELECT cgp_parent_id id
3187         FROM   okc_k_grpings
3188         WHERE  included_chr_id = cp_chr_id
3189         AND cgp_parent_id = cp_grp_id;
3190 
3191     l_dummy             NUMBER;
3192     l_cgcv_rec_in       OKC_CONTRACT_GROUP_PUB.cgcv_rec_type;
3193     l_cgcv_rec_out      OKC_CONTRACT_GROUP_PUB.cgcv_rec_type;
3194 
3195     BEGIN
3196 
3197         --log key input parameters
3198         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3199             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_chr_group_id='||p_chr_group_id);
3200         END IF;
3201 
3202         x_return_status := FND_API.G_RET_STS_SUCCESS;
3203 
3204         --check if contract already belongs to this group
3205         OPEN c_group_csr(p_chr_id, p_chr_group_id);
3206         FETCH c_group_csr INTO l_dummy;
3207         CLOSE c_group_csr;
3208 
3209         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3210             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.chk_k_grp', 'l_dummy='||l_dummy);
3211         END IF;
3212 
3213         --only assign the contract to the group is it is not a member of that group
3214         IF (l_dummy IS NULL) THEN
3215 
3216             l_cgcv_rec_in.cgp_parent_id := p_chr_group_id;
3217             l_cgcv_rec_in.included_chr_id := p_chr_id;
3218             l_cgcv_rec_in.object_version_number := FND_API.G_MISS_NUM;
3219             l_cgcv_rec_in.created_by := FND_API.G_MISS_NUM;
3220             l_cgcv_rec_in.creation_date := FND_API.G_MISS_DATE;
3221             l_cgcv_rec_in.last_updated_by := FND_API.G_MISS_NUM;
3222             l_cgcv_rec_in.last_update_date := FND_API.G_MISS_DATE;
3223             l_cgcv_rec_in.last_update_login := FND_API.G_MISS_NUM;
3224             l_cgcv_rec_in.included_cgp_id := NULL;
3225 
3226             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3227                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_k_grp', 'calling OKC_CONTRACT_GROUP_PVT.create_contract_grpngs');
3228             END IF;
3229 
3230             OKC_CONTRACT_GROUP_PVT.create_contract_grpngs(
3231                 p_api_version => 1,
3232                 p_init_msg_list => FND_API.G_FALSE,
3233                 x_return_status => x_return_status,
3234                 x_msg_count => x_msg_count,
3235                 x_msg_data => x_msg_data,
3236                 p_cgcv_rec => l_cgcv_rec_in,
3237                 x_cgcv_rec => l_cgcv_rec_out);
3238 
3239             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3240                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_k_grp', 'after call to OKC_CONTRACT_GROUP_PVT.create_contract_grpngs, x_return_status='||x_return_status);
3241             END IF;
3242             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3243                 RAISE FND_API.g_exc_unexpected_error;
3244             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3245                 RAISE FND_API.g_exc_error;
3246             END IF;
3247 
3248         END IF;
3249 
3250         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3251             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3252         END IF;
3253 
3254     EXCEPTION
3255         WHEN FND_API.g_exc_error THEN
3256             x_return_status := FND_API.g_ret_sts_error;
3257             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3258                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3259             END IF;
3260             IF (c_group_csr%isopen) THEN
3261                 CLOSE c_group_csr;
3262             END IF;
3263             RAISE;
3264 
3265         WHEN FND_API.g_exc_unexpected_error THEN
3266             x_return_status := FND_API.g_ret_sts_unexp_error ;
3267             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3268                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3269             END IF;
3270             IF (c_group_csr%isopen) THEN
3271                 CLOSE c_group_csr;
3272             END IF;
3273             RAISE;
3274 
3275         WHEN OTHERS THEN
3276             x_return_status := FND_API.g_ret_sts_unexp_error ;
3277             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3278                 --first log the sqlerrm
3279                 l_error_text := substr (SQLERRM, 1, 240);
3280                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3281                 --then add it to the message api list
3282                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3283             END IF;
3284             IF (c_group_csr%isopen) THEN
3285                 CLOSE c_group_csr;
3286             END IF;
3287             RAISE;
3288 
3289     END ASSIGN_CONTRACT_GROUP;
3290 
3291 
3292     /*
3293     Internal procedure for assigning an approval process to the contract.
3294         p_chr_id                : id of the renewed contract
3295         p_chr_group_id          : id of the contract group
3296     */
3297     PROCEDURE ASSIGN_CONTRACT_PROCESS
3298     (
3299      p_chr_id IN NUMBER,
3300      p_pdf_id IN NUMBER,
3301      x_msg_count OUT NOCOPY NUMBER,
3302      x_msg_data OUT NOCOPY VARCHAR2,
3303      x_return_status OUT NOCOPY VARCHAR2
3304     )
3305     IS
3306     l_api_name CONSTANT VARCHAR2(30) := 'ASSIGN_CONTRACT_PROCESS';
3307     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
3308     l_error_text VARCHAR2(512);
3309 
3310     CURSOR c_pdf(cp_chr_id NUMBER) IS
3311         SELECT id, pdf_id
3312         FROM   okc_k_processes
3313         WHERE  chr_id = cp_chr_id
3314         AND pdf_id IN (SELECT id FROM okc_process_defs_b WHERE pdf_type = 'WPS' AND usage = 'APPROVE');
3315 
3316     l_id                NUMBER;
3317     l_pdf_id            NUMBER;
3318     l_cpsv_rec_in       OKC_CPS_PVT.cpsv_rec_type;
3319     l_cpsv_rec_out      OKC_CPS_PVT.cpsv_rec_type;
3320 
3321     BEGIN
3322 
3323         --log key input parameters
3324         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3325             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_pdf_id='||p_pdf_id);
3326         END IF;
3327 
3328         x_return_status := FND_API.G_RET_STS_SUCCESS;
3329 
3330         --check if contract already belongs to this group
3331         OPEN c_pdf(p_chr_id);
3332         FETCH c_pdf INTO l_id, l_pdf_id;
3333         CLOSE c_pdf;
3334 
3335         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3336             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.chk_k_process', 'l_id='||l_id||' ,l_pdf_id='||l_pdf_id);
3337         END IF;
3338 
3339         IF (l_id IS NULL) THEN
3340 
3341             --no process record exists so, create the contract process with the GCD pdf id
3342             l_cpsv_rec_in.pdf_id := p_pdf_id;
3343             l_cpsv_rec_in.chr_id := p_chr_id;
3344             l_cpsv_rec_in.user_id := FND_GLOBAL.USER_ID;
3345             l_cpsv_rec_in.in_process_yn := FND_API.G_MISS_CHAR;
3346             l_cpsv_rec_in.object_version_number := FND_API.G_MISS_NUM;
3347             l_cpsv_rec_in.created_by := FND_API.G_MISS_NUM;
3348             l_cpsv_rec_in.creation_date := FND_API.G_MISS_DATE;
3349             l_cpsv_rec_in.last_updated_by := FND_API.G_MISS_NUM;
3350             l_cpsv_rec_in.last_update_date := FND_API.G_MISS_DATE;
3351             l_cpsv_rec_in.last_update_login := FND_API.G_MISS_NUM;
3352 
3353             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3354                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_k_process', 'calling OKC_CONTRACT_PVT.create_contract_process');
3355             END IF;
3356 
3357             OKC_CONTRACT_PVT.create_contract_process(
3358                 p_api_version => 1,
3359                 p_init_msg_list => FND_API.G_FALSE,
3360                 x_return_status => x_return_status,
3361                 x_msg_count => x_msg_count,
3362                 x_msg_data => x_msg_data,
3363                 p_cpsv_rec => l_cpsv_rec_in,
3364                 x_cpsv_rec => l_cpsv_rec_out);
3365 
3366             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3367                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_k_process', 'after call to OKC_CONTRACT_PVT.create_contract_process, x_return_status='||x_return_status);
3368             END IF;
3369             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3370                 RAISE FND_API.g_exc_unexpected_error;
3371             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3372                 RAISE FND_API.g_exc_error;
3373             END IF;
3374 
3375         ELSE
3376 
3377             IF (l_pdf_id = p_pdf_id) THEN
3378 
3379                 --do nothing, as process record exists and has the same pdf as GCD pdf id
3380                 NULL;
3381 
3382             ELSE
3383 
3384                 --update the contract process record
3385                 l_cpsv_rec_in.pdf_id := p_pdf_id;
3386                 l_cpsv_rec_in.id := l_id;
3387 
3388                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3389                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_k_process', 'calling OKC_CONTRACT_PVT.update_contract_process');
3390                 END IF;
3391 
3392                 OKC_CONTRACT_PVT.update_contract_process(
3393                     p_api_version => 1,
3394                     p_init_msg_list => FND_API.G_FALSE,
3395                     x_return_status => x_return_status,
3396                     x_msg_count => x_msg_count,
3397                     x_msg_data => x_msg_data,
3398                     p_cpsv_rec => l_cpsv_rec_in,
3399                     x_cpsv_rec => l_cpsv_rec_out);
3400 
3401                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3402                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_k_process', 'after call to OKC_CONTRACT_PVT.update_contract_process, x_return_status='||x_return_status);
3403                 END IF;
3404                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3405                     RAISE FND_API.g_exc_unexpected_error;
3406                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3407                     RAISE FND_API.g_exc_error;
3408                 END IF;
3409 
3410             END IF;
3411         END IF;
3412 
3413 
3414         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3415             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3416         END IF;
3417 
3418     EXCEPTION
3419         WHEN FND_API.g_exc_error THEN
3420             x_return_status := FND_API.g_ret_sts_error;
3421             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3422                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3423             END IF;
3424             IF (c_pdf%isopen) THEN
3425                 CLOSE c_pdf;
3426             END IF;
3427             RAISE;
3428 
3429         WHEN FND_API.g_exc_unexpected_error THEN
3430             x_return_status := FND_API.g_ret_sts_unexp_error ;
3431             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3432                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3433             END IF;
3434             IF (c_pdf%isopen) THEN
3435                 CLOSE c_pdf;
3436             END IF;
3437             RAISE;
3438 
3439         WHEN OTHERS THEN
3440             x_return_status := FND_API.g_ret_sts_unexp_error ;
3441             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3442                 --first log the sqlerrm
3443                 l_error_text := substr (SQLERRM, 1, 240);
3444                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3445                 --then add it to the message api list
3446                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3447             END IF;
3448             IF (c_pdf%isopen) THEN
3449                 CLOSE c_pdf;
3450             END IF;
3451             RAISE;
3452 
3453     END ASSIGN_CONTRACT_PROCESS;
3454 
3455     /*
3456     Internal procedure updating various contract header and line attributes as per the renewal rule
3457         p_chr_id                : id of the renewed contract
3458         p_rnrl_rec              : effective renewal rules for the renewed contract
3459         p_notify_to             : salesperson/helpdesk user id on whose behalf the workflow is launched
3460     */
3461     PROCEDURE UPDATE_RENEWED_CONTRACT
3462     (
3463      p_chr_id IN NUMBER,
3464      p_rnrl_rec IN OKS_RENEW_UTIL_PVT.rnrl_rec_type,
3465      p_notify_to IN NUMBER,
3466      x_msg_count OUT NOCOPY NUMBER,
3467      x_msg_data OUT NOCOPY VARCHAR2,
3468      x_return_status OUT NOCOPY VARCHAR2
3469     )
3470     IS
3471     l_api_name CONSTANT VARCHAR2(30) := 'UPDATE_RENEWED_CONTRACT';
3472     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
3473     l_error_text VARCHAR2(512);
3474 
3475     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
3476         SELECT a.contract_number, a.contract_number_modifier,
3477         nvl(a.org_id, a.authoring_org_id), a.qcl_id, a.estimated_amount, a.currency_code,
3478         a.payment_term_id, a.conversion_type, a.conversion_rate, a.conversion_rate_date,
3479         a.conversion_euro_rate, b.renewal_po_number, b.trxn_extension_id,
3480         b.grace_period, b.grace_duration,  c.date_renewed,  -- added for bug 6086893
3481         a.start_date, a.end_date, c.scs_code, c.sts_code, c.estimated_amount,
3482         c.start_date, c.end_date, scs.cls_code, c.id
3483         FROM okc_k_headers_all_b a, oks_k_headers_b b, okc_k_headers_all_b c, OKC_SUBCLASSES_B SCS
3484         WHERE a.id = b.chr_id
3485         AND a.id = cp_chr_id
3486         AND c.id = a.orig_system_id1
3487         AND c.scs_code = scs.code;
3488 /*added for bug 9020536*/
3489    CURSOR Rules_Details(BP_ID NUMBER) IS
3490      SELECT INVOICE_OBJECT1_ID1,ACCOUNT_OBJECT1_ID1
3491      FROM oks_billing_profiles_v
3492      WHERE id=p_rnrl_rec.billing_profile_id;
3493 /*added for bug 9020536*/
3494 
3495 
3496 -- added for bug 6086893
3497    l_new_start_date        okc_k_headers_all_b.start_date%TYPE;
3498    l_new_end_date          okc_k_headers_all_b.end_date%TYPE;
3499    l_old_scs_code          okc_k_headers_all_b.scs_code%TYPE;
3500    l_old_sts_code          okc_k_headers_all_b.sts_code%TYPE;
3501    l_old_estimated_amount  okc_k_headers_all_b.estimated_amount%TYPE;
3502    l_old_start_date        okc_k_headers_all_b.start_date%TYPE;
3503    l_old_end_date          okc_k_headers_all_b.end_date%TYPE;
3504    l_cls_code              okc_subclasses_b.cls_code%TYPE;
3505    l_old_k_id              okc_k_headers_all_b.id%TYPE;
3506 -- end added for bug 6086893
3507 
3508     l_k_num                     VARCHAR2(120);
3509     l_k_mod                     VARCHAR2(120);
3510     l_k_org_id                  NUMBER;
3511     l_qcl_id                    NUMBER;
3512     l_estimated_amount          NUMBER;
3513     l_k_currency_code           VARCHAR2(15);
3514     l_sob_currency_code         VARCHAR2(15);
3515     l_payment_term_id           NUMBER;
3516     l_conv_type                 VARCHAR2(30);
3517     l_conv_rate                 NUMBER;
3518     l_conv_rate_date            DATE;
3519     l_conv_euro_rate            NUMBER;
3520     --while updating PO number we will just use the first 50 characters, otherwise AR apis fail
3521     l_renewal_po_number         VARCHAR2(240);
3522     l_trxn_extension_id         NUMBER;
3523     l_grace_period              VARCHAR2(30);
3524     l_grace_duration            NUMBER;
3525     l_wf_item_key               VARCHAR2(240);
3526     l_date_renewed              DATE;
3527 
3528     l_cust_po_number            VARCHAR2(50);
3529     l_cust_po_number_req_yn     VARCHAR2(1);
3530     l_payment_instruction_type  VARCHAR2(3);
3531     l_threshold_used            VARCHAR2(1);
3532     l_renewal_type              VARCHAR2(30);
3533     l_approval_type             VARCHAR2(30);
3534 
3535     l_est_rev_percent           NUMBER;
3536     l_est_rev_date              DATE;
3537     l_renewal_po_used           VARCHAR2(1);
3538     l_renewal_pricing_type_used VARCHAR2(30);
3539     l_renewal_markup_percent_used   NUMBER;
3540     l_renewal_price_list_used   NUMBER;
3541     l_evn_threshold_amt         NUMBER;
3542     l_evn_threshold_cur         VARCHAR2(15);
3543     l_ern_threshold_amt         NUMBER;
3544     l_ern_threshold_cur         VARCHAR2(15);
3545     l_renewal_status            VARCHAR2(30);
3546 
3547     l_wf_attributes             OKS_WF_K_PROCESS_PVT.WF_ATTR_DETAILS;
3548 
3549     -- bug 4967105 (base bug 4966475)
3550     l_est_rev_date_offset  NUMBER;
3551 /*added for bug 9020536*/
3552     inv_id     NUMBER;
3553     acct_id    NUMBER;
3554 /*added for bug 9020536*/
3555 
3556 
3557     BEGIN
3558 
3559         --log key input parameters
3560         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3561             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
3562             OKS_RENEW_UTIL_PVT.log_rules(l_mod_name || '.effective_renewal_rules', p_rnrl_rec);
3563         END IF;
3564         x_return_status := FND_API.G_RET_STS_SUCCESS;
3565 
3566         OPEN c_k_hdr(p_chr_id);
3567         FETCH c_k_hdr INTO l_k_num, l_k_mod, l_k_org_id, l_qcl_id, l_estimated_amount,
3568             l_k_currency_code, l_payment_term_id,
3569             l_conv_type, l_conv_rate, l_conv_rate_date, l_conv_euro_rate, l_renewal_po_number,
3570             l_trxn_extension_id, l_grace_period, l_grace_duration,  l_date_renewed,
3571             -- added bug 6086893
3572             l_new_start_date, l_new_end_date, l_old_scs_code, l_old_sts_code,
3573             l_old_estimated_amount, l_old_start_date, l_old_end_date, l_cls_code,l_old_k_id;
3574             -- end added bug 6086893
3575         CLOSE c_k_hdr;
3576 
3577         l_qcl_id := nvl(p_rnrl_rec.qcl_id, l_qcl_id);
3578 
3579         --update payment term only if Credit Card is present
3580         --the check for payment term validity has been moved to Contract QA
3581         IF (l_trxn_extension_id IS NOT NULL) THEN
3582             l_payment_term_id := nvl(to_number(p_rnrl_rec.payment_terms_id1), l_payment_term_id);
3583         END IF;
3584 
3585         --always stamp the renewal po number to customer po number
3586         l_cust_po_number := substr(l_renewal_po_number, 1, 50);
3587 
3588         --p_rnrl_rec will follow the K->Party->Org->Global path for po required flag, can be null also
3589         --we need to change the l_cust_po_number_req_yn to N, if renewal type is evergreen
3590         l_cust_po_number_req_yn := p_rnrl_rec.po_required_yn;
3591 
3592         --as per lookup type OKS_PAYMENT_INST_TYPE
3593         IF ((l_renewal_po_number IS NOT NULL) OR (nvl(l_cust_po_number_req_yn, 'N') = 'Y') )  THEN
3594             l_payment_instruction_type := 'PON';
3595         ELSE
3596             --we will always null out payment instructions, except when the renewal PO number is stamped
3597             l_payment_instruction_type := NULL;
3598         END IF;
3599 
3600         --Null out the CVN (currency conversion) attributes if contract currency = set of books currency
3601         --for the contract org
3602         l_sob_currency_code := OKC_CURRENCY_API.get_ou_currency(l_k_org_id);
3603         IF( l_sob_currency_code = l_k_currency_code) THEN
3604             l_conv_type := NULL;
3605             l_conv_rate := NULL;
3606             l_conv_rate_date := NULL;
3607             l_conv_euro_rate := NULL;
3608         END IF;
3609 
3610         --determine the renewal type and corresponding approval type
3611         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3612             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.determine_renewal_type', 'calling OKS_RENEW_UTIL_PVT.get_renewal_type');
3613         END IF;
3614         OKS_RENEW_UTIL_PVT.get_renewal_type(
3615             p_api_version => 1,
3616             p_init_msg_list => FND_API.G_FALSE,
3617             x_return_status => x_return_status,
3618             x_msg_count => x_msg_count,
3619             x_msg_data => x_msg_data,
3620             p_chr_id => p_chr_id,
3621             p_amount => l_estimated_amount,
3622             p_currency_code => l_k_currency_code,
3623             p_rnrl_rec => p_rnrl_rec,
3624             x_renewal_type => l_renewal_type,
3625             x_approval_type => l_approval_type,
3626             x_threshold_used => l_threshold_used);
3627 
3628         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3629             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.determine_renewal_type', 'after call to OKS_RENEW_UTIL_PVT.get_renewal_type, x_renewal_type='||l_renewal_type||
3630             ' ,x_approval_type='||l_approval_type||' ,x_threshold_used='||l_threshold_used);
3631         END IF;
3632 
3633         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3634             RAISE FND_API.g_exc_unexpected_error;
3635         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3636             RAISE FND_API.g_exc_error;
3637         END IF;
3638 
3639         --set the po required to N if renewal type is EVN
3640         IF (l_renewal_type = 'EVN') THEN
3641             l_cust_po_number_req_yn := 'N';
3642         END IF;
3643 
3644         -- bug 4967105 (base bug 4966475)
3645         IF NVL(p_rnrl_rec.revenue_estimated_duration,0) = 0 THEN
3646 	   l_est_rev_date_offset := 0;
3647 	ELSE
3648 	   l_est_rev_date_offset := 1;
3649 	END IF;
3650 
3651         l_est_rev_percent := p_rnrl_rec.revenue_estimated_percent;
3652         l_est_rev_date := OKC_TIME_UTIL_PUB.get_enddate(
3653                               p_start_date => trunc(l_date_renewed),
3654                               p_duration => p_rnrl_rec.revenue_estimated_duration,
3655                               p_timeunit => p_rnrl_rec.revenue_estimated_period) + l_est_rev_date_offset; --bug 4967105
3656 
3657         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3658             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_enddate', 'after call to OKC_TIME_UTIL_PUB.get_enddate, l_est_rev_date='||l_est_rev_date);
3659         END IF;
3660 
3661         l_grace_period  := nvl(p_rnrl_rec.grace_period, l_grace_period);
3662         l_grace_duration  := nvl(p_rnrl_rec.grace_duration, l_grace_duration);
3663         l_renewal_po_used := nvl(l_cust_po_number_req_yn, 'N');
3664 
3665         l_renewal_pricing_type_used := p_rnrl_rec.renewal_pricing_type;
3666         IF (p_rnrl_rec.renewal_pricing_type = 'MAN') THEN
3667             l_renewal_markup_percent_used := NULL;
3668             l_renewal_price_list_used := NULL;
3669         ELSE
3670             l_renewal_markup_percent_used := p_rnrl_rec.markup_percent;
3671             l_renewal_price_list_used := p_rnrl_rec.price_list_id1;
3672         END IF;
3673 
3674         l_evn_threshold_amt := null;
3675         l_evn_threshold_cur := null;
3676         l_ern_threshold_amt := null;
3677         l_ern_threshold_cur := null;
3678 
3679         IF( (l_renewal_type = 'EVN') AND (l_threshold_used = 'Y') ) THEN
3680             l_evn_threshold_amt := p_rnrl_rec.evergreen_threshold_amt;
3681             l_evn_threshold_cur := p_rnrl_rec.base_currency;
3682         END IF;
3683         IF( (l_renewal_type = 'ERN') AND (l_threshold_used = 'Y') ) THEN
3684             l_ern_threshold_amt := p_rnrl_rec.threshold_amount;
3685             l_ern_threshold_cur := p_rnrl_rec.base_currency;
3686         END IF;
3687 
3688         --update the renewal status also, so that if the workflow is not launched
3689         --or the background process does not pick it up, K is still disabled
3690         --from authoring form
3691         l_renewal_status := 'DRAFT';
3692         IF (l_renewal_type = 'EVN') THEN
3693             IF (l_approval_type = 'Y') THEN
3694                 l_renewal_status := 'PENDING_IA';
3695             ELSIF (l_approval_type = 'N') THEN
3696                 l_renewal_status := 'PEND_ACTIVATION';
3697             END IF;
3698         ELSIF (l_renewal_type = 'ERN') THEN
3699             l_renewal_status := 'PEND_PUBLISH';
3700         END IF;
3701 
3702         l_wf_item_key := p_chr_id || to_char(SYSDATE, 'YYYYMMDDHH24MISS');
3703 
3704   /*added for bug 9020536 */
3705        OPEN Rules_Details(p_rnrl_rec.billing_profile_id);
3706         FETCH Rules_Details INTO inv_id,acct_id;
3707         CLOSE Rules_Details;
3708   /*added for bug 9020536 */
3709 
3710 
3711 
3712         --update oks with the new attributes
3713         UPDATE oks_k_headers_b
3714             SET renewal_status = l_renewal_status,
3715                 acct_rule_id   = Nvl(acct_id,acct_rule_id),  --added for bug 9020536
3716                 est_rev_percent = l_est_rev_percent,
3717                 est_rev_date = l_est_rev_date,
3718                 grace_duration = l_grace_duration,
3719                 grace_period = l_grace_period,
3720                 renewal_type_used = l_renewal_type,
3721                 renewal_grace_duration_used = l_grace_duration,
3722                 renewal_grace_period_used = l_grace_period,
3723                 renewal_notification_to = p_notify_to,
3724                 renewal_po_used = l_renewal_po_used,
3725                 renewal_pricing_type_used = l_renewal_pricing_type_used,
3726                 renewal_markup_percent_used = l_renewal_markup_percent_used,
3727                 renewal_price_list_used = l_renewal_price_list_used,
3728                 rev_est_percent_used = p_rnrl_rec.revenue_estimated_percent,
3729                 rev_est_duration_used = p_rnrl_rec.revenue_estimated_duration,
3730                 rev_est_period_used = p_rnrl_rec.revenue_estimated_period,
3731                 billing_profile_used = p_rnrl_rec.billing_profile_id,
3732                 ern_flag_used_yn = NULL, --obsolete column
3733                 evn_threshold_amt = l_evn_threshold_amt,
3734                 evn_threshold_cur = l_evn_threshold_cur,
3735                 ern_threshold_amt = l_ern_threshold_amt,
3736                 ern_threshold_cur = l_ern_threshold_cur,
3737                 electronic_renewal_flag = NULL, --obsolete column
3738                 approval_type_used = l_approval_type,
3739                 wf_item_key = l_wf_item_key
3740             WHERE chr_id = p_chr_id;
3741 
3742         --update OKC header with the new attributes
3743         --note that we do not update the renewal type/approval type, we carry forward the old values
3744         UPDATE okc_k_headers_all_b
3745             SET qcl_id = l_qcl_id,
3746                 inv_rule_id=Nvl(inv_id,inv_rule_id),  --added for bug 9020536
3747                 payment_term_id = l_payment_term_id,
3748                 cust_po_number = l_cust_po_number,
3749                 cust_po_number_req_yn = l_cust_po_number_req_yn,
3750                 payment_instruction_type = l_payment_instruction_type,
3751                 conversion_type = l_conv_type,
3752                 conversion_rate = l_conv_rate,
3753                 conversion_rate_date = l_conv_rate_date,
3754                 conversion_euro_rate = l_conv_euro_rate
3755             WHERE id = p_chr_id;
3756 
3757         --null out OKC/OKS top lines payment instruction and po number attributes
3758         UPDATE okc_k_lines_b
3759             SET payment_instruction_type = NULL,
3760                     inv_rule_id=Nvl(inv_id,inv_rule_id)  --added for bug 9020536
3761             WHERE dnz_chr_id = p_chr_id AND cle_id IS NULL AND lse_id IN (1,12,19,46);
3762 
3763         UPDATE oks_k_lines_b b
3764             SET b.cust_po_number = NULL,
3765                 b.cust_po_number_req_yn = NULL,
3766                 b.acct_rule_id=Nvl(acct_id,acct_rule_id)  --added for bug 9020536
3767             WHERE b.dnz_chr_id = p_chr_id
3768             AND b.cle_id IN (SELECT a.id FROM okc_k_lines_b a
3769                 WHERE a.dnz_chr_id = p_chr_id AND a.cle_id IS NULL AND a.lse_id IN (1,12,19,46));
3770 
3771 
3772         --for new contracts the workflow process is started by the OKS TAPI
3773         --for renewed contract this procedure will start the workflow process
3774         --for Manual renewal - the workflow will not be deferred so that the users can
3775         --immediately submit it for approval, for Online and Evergreen renewals the worklfow
3776         --will be started in the deferred mode to reduce execution time for this api
3777 
3778         l_wf_attributes.contract_id := p_chr_id;
3779         l_wf_attributes.contract_number := l_k_num;
3780         l_wf_attributes.contract_modifier := l_k_mod;
3781         l_wf_attributes.negotiation_status := l_renewal_status;
3782         l_wf_attributes.item_key := l_wf_item_key;
3783         l_wf_attributes.irr_flag := l_approval_type;
3784         l_wf_attributes.process_type := l_renewal_type;
3785 
3786         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3787             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.wfprocess', 'calling  OKS_WF_K_PROCESS_PVT.launch_k_process_wf p_wf_attributes: .contract_id='||p_chr_id||
3788             ' ,.contract_number='||l_k_num||' ,.contract_modifier='||l_k_mod||' ,.negotiation_status='||l_renewal_status||' ,.item_key='||l_wf_item_key||' ,.irr_flag='||l_approval_type||' ,.process_type='||l_renewal_type);
3789         END IF;
3790 
3791         OKS_WF_K_PROCESS_PVT.launch_k_process_wf(
3792             p_api_version => 1.0,
3793             p_init_msg_list => FND_API.G_FALSE,
3794             p_wf_attributes => l_wf_attributes,
3795             x_return_status => x_return_status,
3796             x_msg_count => x_msg_count,
3797             x_msg_data => x_msg_data) ;
3798 
3799         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3800             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.wfprocess', 'after call to OKS_WF_K_PROCESS_PVT.launch_k_process_wf, x_return_status='||x_return_status);
3801         END IF;
3802 
3803         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3804             RAISE FND_API.g_exc_unexpected_error;
3805         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3806             RAISE FND_API.g_exc_error;
3807         END IF;
3808 
3809       -- bug 6086893
3810       -- Added call to OKC_K_RENEW_ASMBLR_PVT.acn_assemble after the workflow is successfully launched
3811 
3812         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3813            FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.acn_assemble', 'BEFORE call to OKC_K_RENEW_ASMBLR_PVT.acn_assemble , p_k_class='||l_cls_code||' ,p_k_nbr_mod= '||l_k_mod||' ,p_k_number= '||l_k_num);
3814             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.acn_assemble','p_k_subclass= '||l_old_scs_code||' ,p_k_status_code= '||l_old_sts_code||' ,p_estimated_amount= '||l_old_estimated_amount);
3815             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.acn_assemble','p_new_k_end_date= '||l_new_end_date||' ,p_new_k_id= '||p_chr_id||' ,p_new_k_start_date= '||l_new_start_date||' ,p_original_k_end_date= '||l_old_end_date);
3816             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.acn_assemble','p_original_kid= '||l_old_k_id||' ,p_original_k_start_date= '||l_old_start_date);
3817         END IF;
3818 
3819           OKC_K_RENEW_ASMBLR_PVT.acn_assemble(
3820            p_api_version           => 1,
3821            p_init_msg_list         => OKC_API.G_FALSE,
3822            x_return_status         => x_return_status,
3823            x_msg_count             => x_msg_count,
3824            x_msg_data              => x_msg_data,
3825            p_k_class               => l_cls_code,
3826            p_k_nbr_mod             => l_k_mod,
3827            p_k_number              => l_k_num,
3828            p_k_subclass            => l_old_scs_code,
3829            p_k_status_code         => l_old_sts_code,
3830            p_estimated_amount      => l_old_estimated_amount,
3831            p_new_k_end_date        => l_new_end_date,
3832            p_new_k_id              => p_chr_id,
3833            p_new_k_start_date      => l_new_start_date,
3834            p_original_k_end_date   => l_old_end_date,
3835            p_original_kid          => l_old_k_id,
3836            p_original_k_start_date => l_old_start_date);
3837 
3838 
3839         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3840             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.acn_assemble', 'after call to OKC_K_RENEW_ASMBLR_PVT.acn_assemble , x_return_status='||x_return_status);
3841         END IF;
3842 
3843         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3844             RAISE FND_API.g_exc_unexpected_error;
3845         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3846             RAISE FND_API.g_exc_error;
3847         END IF;
3848 
3849 
3850 
3851       -- end added bug 6086893
3852 
3853         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3854             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3855         END IF;
3856 
3857     EXCEPTION
3858         WHEN FND_API.g_exc_error THEN
3859             x_return_status := FND_API.g_ret_sts_error;
3860             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3861                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3862             END IF;
3863             IF (c_k_hdr%isopen) THEN
3864                 CLOSE c_k_hdr;
3865             END IF;
3866             RAISE;
3867 
3868         WHEN FND_API.g_exc_unexpected_error THEN
3869             x_return_status := FND_API.g_ret_sts_unexp_error ;
3870             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3871                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3872             END IF;
3873             IF (c_k_hdr%isopen) THEN
3874                 CLOSE c_k_hdr;
3875             END IF;
3876             RAISE;
3877 
3878         WHEN OTHERS THEN
3879             x_return_status := FND_API.g_ret_sts_unexp_error ;
3880             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3881                 --first log the sqlerrm
3882                 l_error_text := substr (SQLERRM, 1, 240);
3883                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3884                 --then add it to the message api list
3885                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3886             END IF;
3887             IF (c_k_hdr%isopen) THEN
3888                 CLOSE c_k_hdr;
3889             END IF;
3890             RAISE;
3891 
3892     END UPDATE_RENEWED_CONTRACT;
3893 
3894 
3895 ------------------------ End  Internal procedures ----------------------------------
3896 
3897     /*
3898     R12 procedure that validates if a contract can be renewed
3899     Parameters
3900         p_chr_id : contract id of the contract being renewed
3901         p_date : start date of the renewal, if not passed defaults to end date + 1 of the source contract
3902         p_validation_level : A - do all checks including warnings, E - do only error checks
3903         x_rnrl_rec : returns the effective renewal rules for the contract
3904         x_validation_status : S - Success (OK for renewal), W - Warnings (Ok for renewal)
3905                              E - Errors (Cannot be renewed)
3906         x_validation_tbl : Validation error and warning messages
3907 
3908     The following validations are done
3909     Error Conditions
3910         1.	Contract is a template.
3911         2.	Contract status is not in ACTIVE, EXPIRED or SIGNED base statuses.
3912         3.	Contract end date is not null (perpetual contract).
3913         4.	Contract has been terminated.
3914         5.	Contract has been renewed and the renewal has not been cancelled.
3915         6.	If the user does not update access to the contract
3916         7.	If all sublines (or subscription top lines) in status ACTIVE, EXPIRED or SIGNED
3917             have already been renew consolidated.
3918         8.	The effective renewal type for contract is  'Do not renew'. If the renewal type
3919             is not defined for the contract, it is derived from Party -> Org -> Global setup
3920             by calling the get_renew_rules procedure.
3921         9.  If the contract contains any warranty lines (lse id = 14) it cannot be renewed
3922             (Currently there is no check for warranty lines, a contract having warranty lines
3923             can be renewed, but all warranty lines are dropped during copy.)
3924 
3925     Warning Conditions
3926         1.	Contract has been renewed and the renewal has been cancelled.
3927             For background (events) renewal this is an error condition.
3928         2.	All contract sublines and subscription toplines have been terminated or cancelled.
3929             For background (events) renewal this is an error condition.
3930         3.	All contract sublines and subscription toplines have an effective line renewal type
3931             code of DNR. (Effective line renewal type code = nvl(line renewal type code ,
3932             parent line renewal type code). For background (events) renewal this is an error
3933             condition.
3934 
3935     This procedure does not stop if any error/warning condition is found, all validations
3936     are always done
3937     */
3938     PROCEDURE VALIDATE_RENEWAL
3939     (
3940      p_api_version IN NUMBER DEFAULT 1,
3941      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
3942      x_return_status OUT NOCOPY VARCHAR2,
3943      x_msg_count OUT NOCOPY NUMBER,
3944      x_msg_data OUT NOCOPY VARCHAR2,
3945      p_chr_id IN NUMBER,
3946      p_date IN DATE,
3947      p_validation_level IN VARCHAR2 DEFAULT G_VALIDATE_ALL,
3948      x_rnrl_rec OUT NOCOPY OKS_RENEW_UTIL_PVT.rnrl_rec_type,
3949      x_validation_status OUT NOCOPY VARCHAR2,
3950      x_validation_tbl OUT NOCOPY validation_tbl_type
3951     )
3952     IS
3953 
3954     l_api_name CONSTANT VARCHAR2(30) := 'VALIDATE_RENEWAL';
3955     l_api_version CONSTANT NUMBER := 1;
3956     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || g_pkg_name || '.' || l_api_name;
3957     l_error_text VARCHAR2(512);
3958 
3959     --cursor to get basic contract information
3960     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
3961         SELECT  a.application_id, a.contract_number, a.contract_number_modifier,
3962             b.ste_code, b.meaning, a.scs_code, a.template_yn,
3963             a.date_terminated, a.date_renewed, a.end_date
3964         FROM okc_k_headers_all_b a, okc_statuses_v b
3965         WHERE a.id = cp_chr_id AND a.sts_code = b.code;
3966 
3967     --cursor to check if all sublines and subscr toplines have been renew consolidated
3968     CURSOR c_check_line_rencon(cp_chr_id IN NUMBER) IS
3969         SELECT kl.id
3970         FROM okc_k_lines_b kl
3971         WHERE kl.dnz_chr_id = cp_chr_id AND kl.lse_id IN (7, 8, 9, 10, 11, 13, 25, 35, 46)
3972             AND kl.id NOT IN(
3973                              SELECT ol.object_cle_id
3974                              FROM okc_operation_lines ol, okc_operation_instances oi, okc_class_operations oo
3975                              WHERE oo.cls_code = 'SERVICE' AND oo.opn_code = 'REN_CON' AND oo.id = oi.cop_id
3976                              AND ol.oie_id = oi.id AND ol.object_chr_id = cp_chr_id
3977                              AND ol.subject_chr_id IS NOT NULL
3978                              AND ol.process_flag = 'P'  AND ol.active_yn = 'Y');
3979 
3980     --cursor to check if a contract has been renewed. If the contract  has been
3981     --renewed, get the renewed contract's status, number and modifier.
3982     --we order by active_yn desc, as for cancelled renewed contracts
3983     --active_yn = 'N'. So we want to get the active renewals (such as entered/signed status etc) first
3984     CURSOR c_check_hdr_renew (cp_chr_id IN NUMBER) IS
3985         SELECT k.contract_number, k.contract_number_modifier, st.ste_code
3986         FROM okc_operation_lines ol, okc_operation_instances oi, okc_class_operations oo,
3987             okc_statuses_b st, okc_k_headers_all_b k
3988         WHERE oo.cls_code = 'SERVICE' AND oo.opn_code = 'RENEWAL'
3989             AND oo.id = oi.cop_id
3990             AND ol.oie_id = oi.id AND ol.object_chr_id = cp_chr_id
3991             AND ol.subject_chr_id IS NOT NULL AND ol.object_cle_id IS NULL
3992             AND ol.subject_cle_id IS NULL
3993             AND ol.process_flag = 'P' AND ol.subject_chr_id = k.id
3994             AND k.sts_code = st.code
3995             ORDER BY ol.active_yn DESC;
3996 
3997     --cursor to check if all sublines and subscr toplines have been cancelled or terminated
3998     CURSOR c_check_line_term_canc (cp_chr_id IN NUMBER) IS
3999         SELECT id
4000         FROM okc_k_lines_b
4001         WHERE dnz_chr_id = cp_chr_id AND lse_id IN (7, 8, 9, 10, 11, 13, 25, 35, 46)
4002             AND date_terminated IS  NULL AND date_cancelled IS NULL;
4003 
4004     --cursor to determine if any sublines or toplines exist with an effective renewal type
4005     --that is not DNR. If a topline has renewal type DNR, none of it's sublines are considered.
4006     --If topline is not DNR then sublines are checked to see if any exist with renewal type not DNR
4007     CURSOR c_check_line_dnr (cp_chr_id IN NUMBER) IS
4008         SELECT a.id
4009         FROM okc_k_lines_b a, okc_k_lines_b b
4010         WHERE a.dnz_chr_id = cp_chr_id
4011         AND b.dnz_chr_id (+)  = cp_chr_id
4012         AND a.cle_id = b.id (+)
4013         AND a.lse_id IN (7,8,9,10,11,13,25,35,46)
4014         AND decode(b.line_renewal_type_code, 'DNR', 'DNR',
4015                     NULL, nvl(a.line_renewal_type_code, 'FUL'),
4016                     nvl(a.line_renewal_type_code, b.line_renewal_type_code)) <> 'DNR';
4017 
4018     --cursor to check if the contract contains any warranty lines
4019     CURSOR c_check_line_warr (cp_chr_id IN NUMBER) IS
4020         SELECT id
4021         FROM okc_k_lines_b
4022         WHERE dnz_chr_id = cp_chr_id AND lse_id = 14;
4023 
4024     --cursor to check id there are any valid sublines and subscr toplines
4025     --we need to do line level checks only if a valid line exists, other wise we get
4026     --redundant error messages
4027     CURSOR c_check_valid_line(cp_chr_id IN NUMBER) IS
4028         SELECT id
4029         FROM okc_k_lines_b kl, okc_statuses_b st
4030         WHERE kl.dnz_chr_id = cp_chr_id AND kl.lse_id IN (7, 8, 9, 10, 11, 13, 25, 35, 46)
4031         AND kl.sts_code = st.ste_code
4032         AND st.ste_code IN ('ACTIVE', 'EXPIRED', 'SIGNED', 'CANCELLED', 'TERMINATED');
4033 
4034     l_k_app_id okc_k_headers_b.application_id%TYPE;
4035     l_k_num okc_k_headers_b.contract_number%TYPE;
4036     l_k_mod okc_k_headers_b.contract_number_modifier%TYPE;
4037     l_k_ste_code okc_statuses_b.ste_code%TYPE;
4038     l_k_ste_meaning okc_statuses_tl.meaning%TYPE;
4039     l_k_scs_code okc_k_headers_b.scs_code%TYPE;
4040     l_k_template_yn okc_k_headers_b.template_yn%TYPE;
4041     l_k_date_terminated okc_k_headers_b.date_terminated%TYPE;
4042     l_k_date_renewed okc_k_headers_b.date_renewed%TYPE;
4043     l_k_end_date okc_k_headers_b.end_date%TYPE;
4044     l_rnrl_rec OKS_RENEW_UTIL_PVT.rnrl_rec_type;
4045 
4046     l_date DATE;
4047     l_k_num_mod VARCHAR2(250);
4048     l_msg_count INTEGER := 0;
4049     l_k_is_renewed BOOLEAN := FALSE;
4050     l_k_access_level VARCHAR2(1);
4051     l_k_line_id NUMBER;
4052     l_k_ren_type oks_k_defaults.renewal_type%TYPE;
4053 
4054     l_renk_num okc_k_headers_b.contract_number%TYPE;
4055     l_renk_mod okc_k_headers_b.contract_number_modifier%TYPE;
4056     l_renk_ste_code okc_statuses_b.ste_code%TYPE;
4057     l_valid_line_exists BOOLEAN := FALSE;
4058     BEGIN
4059         --log key input parameters
4060         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4061             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id ||' ,p_date='|| p_date ||' ,p_validation_level='|| p_validation_level);
4062         END IF;
4063 
4064         --standard api initilization and checks
4065         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
4066             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4067         END IF;
4068         IF FND_API.to_boolean(p_init_msg_list ) THEN
4069             FND_MSG_PUB.initialize;
4070         END IF;
4071         x_return_status := FND_API.G_RET_STS_SUCCESS;
4072         x_validation_status := G_VALID_STS_SUCCESS;
4073 
4074         --first get the basic contract attributes
4075         OPEN c_k_hdr(p_chr_id);
4076         FETCH c_k_hdr INTO l_k_app_id, l_k_num, l_k_mod, l_k_ste_code, l_k_ste_meaning,
4077         l_k_scs_code, l_k_template_yn, l_k_date_terminated, l_k_date_renewed, l_k_end_date;
4078 
4079         IF (c_k_hdr%notfound) THEN
4080             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
4081             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
4082             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4083                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_k_values', FALSE);
4084             END IF;
4085             FND_MSG_PUB.ADD;
4086             CLOSE c_k_hdr;
4087             RAISE FND_API.g_exc_error;
4088         END IF;
4089         CLOSE c_k_hdr;
4090 
4091         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4092             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_c_k_hdr', 'l_k_app_id=' || l_k_app_id ||' ,l_k_num='|| l_k_num ||' ,l_k_mod='|| l_k_mod ||' ,l_k_ste_code='|| l_k_ste_code ||' ,l_k_ste_meaning='|| l_k_ste_meaning
4093             ||', l_k_scs_code='|| l_k_scs_code ||' ,l_k_template_yn='|| l_k_template_yn ||', l_k_date_terminated='|| l_k_date_terminated ||' ,l_k_date_renewed='|| l_k_date_renewed ||' ,l_k_end_date='|| l_k_end_date);
4094         END IF;
4095 
4096         -- no checks if not service contract
4097         IF ((nvl(l_k_app_id, - 99) <> 515)
4098             OR (l_k_scs_code NOT IN ('SERVICE', 'WARRANTY', 'SUBSCRIPTION')) )THEN
4099             RETURN;
4100         END IF;
4101 
4102         IF(trim(l_k_mod) IS NULL) THEN
4103             l_k_num_mod := l_k_num;
4104         ELSE
4105             l_k_num_mod := l_k_num || '-' || trim(l_k_mod);
4106         END IF;
4107 
4108         --error if contract has WARRANTY (lse_id =14) lines
4109         l_k_line_id := NULL;
4110         OPEN c_check_line_warr(p_chr_id);
4111         FETCH c_check_line_warr INTO l_k_line_id;
4112         CLOSE c_check_line_warr;
4113 
4114         IF (l_k_line_id IS NOT NULL) THEN
4115             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_WARRANTY_DNR');
4116             FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4117             l_msg_count := l_msg_count + 1;
4118             x_validation_tbl(l_msg_count).code := 'OKS_WARRANTY_DNR';
4119             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4120             x_validation_status := G_VALID_STS_ERROR;
4121             --we don't  need to do any more checks if this is a warranty contract
4122             RETURN;
4123         END IF;
4124 
4125         --error if contract is template
4126         IF (nvl(l_k_template_yn, 'X') = 'Y') THEN
4127             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_K_TEMPLATE');
4128             FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4129             l_msg_count := l_msg_count + 1;
4130             x_validation_tbl(l_msg_count).code := 'OKS_K_TEMPLATE';
4131             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4132             x_validation_status := G_VALID_STS_ERROR;
4133         END IF;
4134 
4135         --error if contract end date is not null
4136         IF (l_k_end_date IS NULL) THEN
4137             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_PERPETUAL');
4138             FND_MESSAGE.set_token('COMPONENT', l_k_num_mod);
4139             l_msg_count := l_msg_count + 1;
4140             x_validation_tbl(l_msg_count).code := 'OKS_NO_PERPETUAL';
4141             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4142             x_validation_status := G_VALID_STS_ERROR;
4143         END IF;
4144 
4145         --if p_date is null, use contract end date + 1 . If contract end date is also null use sysdate
4146         l_date := trunc(nvl(p_date, nvl(l_k_end_date, SYSDATE - 1) + 1));
4147 
4148 
4149         --error if status not in 'ACTIVE', 'EXPIRED' and 'SIGNED'
4150         IF (l_k_ste_code NOT IN ('ACTIVE', 'EXPIRED', 'SIGNED')) THEN
4151             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVALID_K_STATUS');
4152             FND_MESSAGE.set_token('STATUS', l_k_ste_meaning);
4153             l_msg_count := l_msg_count + 1;
4154             x_validation_tbl(l_msg_count).code := 'OKS_INVALID_K_STATUS';
4155             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4156             x_validation_status := G_VALID_STS_ERROR;
4157         END IF;
4158 
4159         --error if contract has been terminated for a future date
4160         IF (l_k_date_terminated IS NOT NULL) THEN
4161             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_FUTURE_TERMINATED_K');
4162             FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4163             l_msg_count := l_msg_count + 1;
4164             x_validation_tbl(l_msg_count).code := 'OKS_FUTURE_TERMINATED_K';
4165             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4166             x_validation_status := G_VALID_STS_ERROR;
4167         END IF;
4168 
4169         --check if contract has been renewed
4170         IF (l_k_date_renewed IS NOT NULL) THEN
4171             l_k_is_renewed := TRUE;
4172             --This will be checked later to figure get the status of the renewed contract
4173             --and set the appropriate message in the validation table.
4174         END IF;
4175 
4176         --error if user does not have update access for the contract
4177         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4178             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calling_get_k_access_level', 'p_chr_id=' || p_chr_id ||', l_k_app_id='|| l_k_app_id ||' ,l_k_scs_code='|| l_k_scs_code);
4179         END IF;
4180 
4181         l_k_access_level := OKC_UTIL.get_all_k_access_level(p_chr_id, l_k_app_id, l_k_scs_code);
4182 
4183         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4184             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_get_k_access_level', 'l_k_access_level=' || l_k_access_level);
4185         END IF;
4186 
4187         IF (nvl(l_k_access_level, 'X') <> 'U') THEN
4188             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_UPDATE');
4189             FND_MESSAGE.set_token('CHR', l_k_num_mod);
4190             l_msg_count := l_msg_count + 1;
4191             x_validation_tbl(l_msg_count).code := 'OKS_NO_UPDATE';
4192             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4193             x_validation_status := G_VALID_STS_ERROR;
4194         END IF;
4195 
4196         --before doing any line level validations, check if there are valid lines to begin with
4197         l_k_line_id := NULL;
4198         OPEN c_check_valid_line(p_chr_id);
4199         FETCH c_check_valid_line INTO l_k_line_id;
4200         CLOSE c_check_valid_line;
4201 
4202         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4203             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_valid_line_check', 'l_k_line_id=' || l_k_line_id);
4204         END IF;
4205 
4206         IF ( l_k_line_id IS NULL ) THEN
4207             l_valid_line_exists := FALSE;
4208         ELSE
4209             l_valid_line_exists := TRUE;
4210         END IF;
4211 
4212         IF (l_valid_line_exists) THEN
4213             --error if all sublines and subsciption toplines have been renew consolidated
4214             l_k_line_id := NULL;
4215             OPEN c_check_line_rencon(p_chr_id);
4216             FETCH c_check_line_rencon INTO l_k_line_id;
4217             CLOSE c_check_line_rencon;
4218 
4219             IF (l_k_line_id IS NULL) THEN
4220                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_K_RENEW_CONSOLIDATED');
4221                 l_msg_count := l_msg_count + 1;
4222                 x_validation_tbl(l_msg_count).code := 'OKS_K_RENEW_CONSOLIDATED';
4223                 x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4224                 x_validation_status := G_VALID_STS_ERROR;
4225             END IF;
4226         END IF;
4227 
4228         --error if effective renewal type of contract is DNR
4229         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4230             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calling_get_renew_rules', 'p_chr_id=' || p_chr_id ||', p_date='|| l_date);
4231         END IF;
4232 
4233         OKS_RENEW_UTIL_PVT.get_renew_rules(
4234                         x_return_status => x_return_status,
4235                         p_api_version => 1.0,
4236                         p_init_msg_list => FND_API.G_FALSE,
4237                         p_chr_id => p_chr_id,
4238                         p_party_id => NULL,
4239                         p_org_id => NULL,
4240                         p_date => l_date,
4241                         p_rnrl_rec => l_rnrl_rec,
4242                         x_rnrl_rec => x_rnrl_rec,
4243                         x_msg_count => x_msg_count,
4244                         x_msg_data => x_msg_data);
4245 
4246         l_k_ren_type := x_rnrl_rec.renewal_type;
4247 
4248         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4249             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_get_renew_rules', 'x_return_status=' || x_return_status ||' ,l_k_ren_type='|| l_k_ren_type);
4250         END IF;
4251 
4252         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
4253             RAISE FND_API.g_exc_unexpected_error;
4254         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
4255             RAISE FND_API.g_exc_error;
4256         END IF;
4257 
4258         IF (nvl(l_k_ren_type, 'X') = 'DNR') THEN
4259             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_DNR_MSG');
4260             l_msg_count := l_msg_count + 1;
4261             x_validation_tbl(l_msg_count).code := 'OKS_DNR_MSG';
4262             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4263             x_validation_status := G_VALID_STS_ERROR;
4264         END IF;
4265 
4266 
4267         --error if contract has been renewed and renewed contract is in ENETERED status
4268         --warning if the renewed contract is CANCELLED. All other statuses are also
4269         --treated as error conditons
4270         --We need to do this check only if the contract has been renewed (date_renewed is not null)
4271         --or if validation_level is 'A'. This is baecause if a contract is renewed and then cancelled
4272         --the date_renewed on the source contract is nulled out
4273 
4274         IF ((l_k_is_renewed) OR (p_validation_level = G_VALIDATE_ALL) ) THEN
4275 
4276             OPEN c_check_hdr_renew (p_chr_id);
4277             FETCH c_check_hdr_renew INTO l_renk_num, l_renk_mod, l_renk_ste_code;
4278             CLOSE c_check_hdr_renew;
4279 
4280             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4281                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.checking_for_renewals', 'l_renk_num=' || l_renk_num ||' ,l_renk_mod='|| l_renk_mod ||' ,l_renk_ste_code='|| l_renk_ste_code);
4282             END IF;
4283 
4284             --if a renewed contract is found, set error/warning message as per status
4285             IF (l_renk_num IS NOT NULL) THEN
4286 
4287                 IF(trim(l_renk_mod) IS NULL) THEN
4288                     l_renk_num := l_renk_num;
4289                 ELSE
4290                     l_renk_num := l_renk_num || '-' || trim(l_renk_mod);
4291                 END IF;
4292 
4293                 --renewed contract is CANCELLED - warning
4294                 IF(nvl(l_renk_ste_code, 'X') = 'CANCELLED' )THEN
4295 
4296                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_ALREADY_NOT_RENEWED');
4297                     --FND_MESSAGE.set_token('CHR', l_k_num_mod);
4298                     l_msg_count := l_msg_count + 1;
4299                     x_validation_tbl(l_msg_count).code := 'OKS_ALREADY_NOT_RENEWED';
4300                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4301                     IF (x_validation_status <> G_VALID_STS_ERROR) THEN
4302                         x_validation_status := G_VALID_STS_WARNING;
4303                     END IF;
4304 
4305                 --renewed contract is ENETERED - error
4306                 ELSIF (nvl(l_renk_ste_code, 'X') = 'ENTERED' ) THEN
4307 
4308                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_RENCOPY_ENTERED');
4309                     FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4310                     FND_MESSAGE.set_token('RENCOPY', l_renk_num);
4311                     l_msg_count := l_msg_count + 1;
4312                     x_validation_tbl(l_msg_count).code := 'OKS_RENCOPY_ENTERED';
4313                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4314                     x_validation_status := G_VALID_STS_ERROR;
4315 
4316                 --all other statuses treated as ACTIVE ( ACTIVE/EXPIRED/SIGNED/QA_HOLD etc) -error
4317                 ELSE
4318                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_RENCOPY_APPROVE');
4319                     FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4320                     FND_MESSAGE.set_token('RENCOPY', l_renk_num);
4321                     l_msg_count := l_msg_count + 1;
4322                     x_validation_tbl(l_msg_count).code := 'OKS_RENCOPY_APPROVE';
4323                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4324                     x_validation_status := G_VALID_STS_ERROR;
4325 
4326                 END IF; --of IF( nvl(l_renk_ste_code, 'X') = 'CANCELLED' )THEN
4327 
4328             END IF; --IF (l_renk_num IS NOT NULL) THEN
4329 
4330         END IF; --of IF ( (l_k_is_renewed) OR (p_validation_level = G_VALIDATE_ALL) ) THEN
4331 
4332         --now do the warning checks if p_validation_level = 'A'
4333         IF (p_validation_level = G_VALIDATE_ALL) THEN
4334 
4335             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4336                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.checking_for_warnings', 'begin');
4337             END IF;
4338 
4339             IF (l_valid_line_exists) THEN
4340 
4341                 --warning if all sublines and subscr toplines have been terminated or cancelled
4342                 l_k_line_id := NULL;
4343                 OPEN c_check_line_term_canc(p_chr_id);
4344                 FETCH c_check_line_term_canc INTO l_k_line_id;
4345                 CLOSE c_check_line_term_canc;
4346 
4347                 IF (l_k_line_id IS NULL) THEN
4348                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_LINES_SUBLINES_TERMINATED');
4349                     l_msg_count := l_msg_count + 1;
4350                     x_validation_tbl(l_msg_count).code := 'OKS_LINES_SUBLINES_TERMINATED';
4351                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4352                     IF (x_validation_status <> G_VALID_STS_ERROR) THEN
4353                         x_validation_status := G_VALID_STS_WARNING;
4354                     END IF;
4355                 END IF;
4356 
4357                 --warning if all sublines and subscr toplines have effective line renewal type of DNR
4358                 l_k_line_id := NULL;
4359                 OPEN c_check_line_dnr(p_chr_id);
4360                 FETCH c_check_line_dnr INTO l_k_line_id;
4361                 CLOSE c_check_line_dnr;
4362 
4363                 IF (l_k_line_id IS NULL) THEN
4364                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_LINES_DNR');
4365                     l_msg_count := l_msg_count + 1;
4366                     x_validation_tbl(l_msg_count).code := 'OKS_LINES_DNR';
4367                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4368                     IF (x_validation_status <> G_VALID_STS_ERROR) THEN
4369                         x_validation_status := G_VALID_STS_WARNING;
4370                     END IF;
4371                 END IF;
4372 
4373             END IF; --of  IF (l_valid_line_exists) THEN
4374 
4375         END IF; --of IF (p_validation_level = G_VALIDATE_ALL) THEN
4376 
4377         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4378             IF (x_validation_tbl.count > 0 ) THEN
4379                 FOR i IN x_validation_tbl.first..x_validation_tbl.last LOOP
4380                     FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.validation_mesg', 'i=' || i ||' , code='|| x_validation_tbl(i).code ||' ,message='|| x_validation_tbl(i).message);
4381                 END LOOP;
4382             END IF;
4383             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_validation_status=' || x_validation_status ||', x_return_status='|| x_return_status);
4384         END IF;
4385         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4386 
4387     EXCEPTION
4388         WHEN FND_API.g_exc_error THEN
4389             x_return_status := FND_API.g_ret_sts_error ;
4390 
4391             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4392                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
4393             END IF;
4394             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4395 
4396             IF (c_k_hdr%isopen) THEN
4397                 CLOSE c_k_hdr;
4398             END IF;
4399             IF (c_check_line_rencon%isopen) THEN
4400                 CLOSE c_check_line_rencon;
4401             END IF;
4402             IF (c_check_hdr_renew%isopen) THEN
4403                 CLOSE c_check_hdr_renew;
4404             END IF;
4405             IF (c_check_line_term_canc%isopen) THEN
4406                 CLOSE c_check_line_term_canc;
4407             END IF;
4408             IF (c_check_line_dnr%isopen) THEN
4409                 CLOSE c_check_line_dnr;
4410             END IF;
4411             IF (c_check_line_warr%isopen) THEN
4412                 CLOSE c_check_line_warr;
4413             END IF;
4414             IF (c_check_valid_line%isopen) THEN
4415                 CLOSE c_check_valid_line;
4416             END IF;
4417 
4418         WHEN FND_API.g_exc_unexpected_error THEN
4419             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4420 
4421             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4422                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
4423             END IF;
4424             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4425 
4426             IF (c_k_hdr%isopen) THEN
4427                 CLOSE c_k_hdr;
4428             END IF;
4429             IF (c_check_line_rencon%isopen) THEN
4430                 CLOSE c_check_line_rencon;
4431             END IF;
4432             IF (c_check_hdr_renew%isopen) THEN
4433                 CLOSE c_check_hdr_renew;
4434             END IF;
4435             IF (c_check_line_term_canc%isopen) THEN
4436                 CLOSE c_check_line_term_canc;
4437             END IF;
4438             IF (c_check_line_dnr%isopen) THEN
4439                 CLOSE c_check_line_dnr;
4440             END IF;
4441             IF (c_check_line_warr%isopen) THEN
4442                 CLOSE c_check_line_warr;
4443             END IF;
4444             IF (c_check_valid_line%isopen) THEN
4445                 CLOSE c_check_valid_line;
4446             END IF;
4447 
4448         WHEN OTHERS THEN
4449             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4450 
4451             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4452                 --first log the sqlerrm
4453                 l_error_text := substr (SQLERRM, 1, 240);
4454                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
4455                 --then add it to the message api list
4456                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
4457             END IF;
4458             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4459 
4460             IF (c_k_hdr%isopen) THEN
4461                 CLOSE c_k_hdr;
4462             END IF;
4463             IF (c_check_line_rencon%isopen) THEN
4464                 CLOSE c_check_line_rencon;
4465             END IF;
4466             IF (c_check_hdr_renew%isopen) THEN
4467                 CLOSE c_check_hdr_renew;
4468             END IF;
4469             IF (c_check_line_term_canc%isopen) THEN
4470                 CLOSE c_check_line_term_canc;
4471             END IF;
4472             IF (c_check_line_dnr%isopen) THEN
4473                 CLOSE c_check_line_dnr;
4474             END IF;
4475             IF (c_check_line_warr%isopen) THEN
4476                 CLOSE c_check_line_warr;
4477             END IF;
4478             IF (c_check_valid_line%isopen) THEN
4479                 CLOSE c_check_valid_line;
4480             END IF;
4481 
4482     END VALIDATE_RENEWAL;
4483 
4484     /*
4485     Procedure for updating  invoice_text col table OKC_K_LINES_TL
4486     with the current line start date and end date. Called during renewal,
4487     after line dates are adjusted. Uses bulk calls to get and set the invoice text
4488     Parameters
4489         p_chr_id    : id of the contract whose lines need to be updated
4490 
4491        The format of the invoice text is as follows
4492        topline = SUBSTR(l_item_desc || ':' || p_start_date || ':' || p_end_date, 1, 450);
4493 
4494        subline = SUBSTR(p_topline_item_desc || ':' || l_num_items || ':' || l_item_desc || ':' || p_start_date || ':' || p_end_date, 1, 450);
4495 
4496        bug 4712579 / bug 4992884
4497        subline for usage (lse_id = 13) is as follows
4498        subline = SUBSTR(p_topline_item_desc || ':' || l_item_desc || ':' || p_start_date || ':' || p_end_date, 1, 450);
4499 
4500 	   get invoice text from previous contract subline truncating the dates at the end
4501     */
4502 
4503     PROCEDURE UPDATE_INVOICE_TEXT
4504     (
4505      p_api_version IN NUMBER DEFAULT 1,
4506      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4507      p_commit   IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4508      x_return_status OUT NOCOPY VARCHAR2,
4509      x_msg_count OUT NOCOPY NUMBER,
4510      x_msg_data OUT NOCOPY VARCHAR2,
4511      p_chr_id IN NUMBER
4512     )
4513     IS
4514     l_api_name CONSTANT VARCHAR2(30) := 'UPDATE_INVOICE_TEXT';
4515     l_api_version CONSTANT NUMBER := 1;
4516     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
4517     l_error_text VARCHAR2(512);
4518 
4519     TYPE line_rec_type IS RECORD(
4520         sl_id           NUMBER,
4521         name            VARCHAR2(4000),
4522         descr           VARCHAR2(4000),
4523         num_of_items    NUMBER,
4524         cle_id          NUMBER,
4525         lse_id          NUMBER,
4526         start_date      VARCHAR2(100), --DATE,
4527         end_date        VARCHAR2(100)); --DATE);
4528     TYPE line_tbl_type IS TABLE OF line_rec_type INDEX BY BINARY_INTEGER;
4529 
4530     l_line_tbl      line_tbl_type;
4531     l_inv_txt_tbl   chr_tbl_type;
4532     l_sl_id_tbl     num_tbl_type;
4533     l_cle_id_tbl    num_tbl_type;
4534     l_disp_pref     VARCHAR2(255);
4535 
4536     CURSOR c_get_topline_txt (cp_chr_id IN NUMBER) IS
4537   	    SELECT
4538             --kl.lse_id, bk.inventory_item_id id1, bk.organization_id id2 , bk.organization_id inv_org_id,
4539             sl.id, bt.description name, bk.concatenated_segments description, null,
4540             null, null, to_char(kl.start_date,'DD-MON-YYYY'), to_char(kl.end_date,'DD-MON-YYYY')
4541   	    FROM mtl_system_items_b_kfv bk, mtl_system_items_tl bt,
4542             okc_k_items it, oks_k_lines_b sl, okc_k_lines_b kl
4543   	    WHERE bk.inventory_item_id = bt.inventory_item_id
4544         AND bk.organization_id = bt.organization_id
4545   		AND bt.language = USERENV('LANG')
4546         AND bk.inventory_item_id = it.object1_id1
4547         AND bk.organization_id = it.object1_id2
4548         AND it.cle_id = kl.id
4549         AND sl.cle_id = kl.id
4550         AND kl.lse_id IN (1, 12, 14, 19, 46)
4551         AND kl.cle_id IS NULL
4552         AND kl.dnz_chr_id = cp_chr_id;
4553 
4554 
4555     CURSOR c_get_subline_txt (cp_chr_id IN NUMBER) IS
4556         SELECT
4557             --kl.lse_id, iv.id1, iv.id2, iv.inv_org_id,
4558             sl.id, iv.name, iv.description, it.number_of_items,
4559             kl.cle_id, kl.lse_id, to_char(kl.START_DATE,'DD-MON-YYYY'), to_char(kl.end_date,'DD-MON-YYYY')
4560         FROM
4561         (
4562                 SELECT 'OKX_COVSYST' TYPE, T.SYSTEM_ID ID1, '#' ID2, T.NAME NAME,
4563                 T.DESCRIPTION DESCRIPTION, NULL INV_ORG_ID
4564                 FROM CSI_SYSTEMS_TL T
4565                 WHERE T.LANGUAGE = USERENV('LANG')
4566             UNION ALL
4567                 SELECT 'OKX_PARTYSITE' TYPE, PSE.PARTY_SITE_ID ID1, '#' ID2, PSE.PARTY_SITE_NAME NAME,
4568                 SUBSTR(arp_addr_label_pkg.format_address(NULL,LCN.ADDRESS1,LCN.ADDRESS2,LCN.ADDRESS3,
4569                 LCN.ADDRESS4,LCN.CITY,LCN.COUNTY,LCN.STATE,LCN.PROVINCE,LCN.POSTAL_CODE,NULL,LCN.COUNTRY,
4570                 NULL,NULL,NULL,NULL,NULL,NULL,NULL,'N','N',80,1,1
4571                 ),1,80) DESCRIPTION, NULL INV_ORG_ID
4572                 FROM HZ_PARTY_SITES PSE,HZ_LOCATIONS LCN
4573                 WHERE LCN.LOCATION_ID = PSE.LOCATION_ID
4574                 AND LCN.CONTENT_SOURCE_TYPE = 'USER_ENTERED'
4575             UNION ALL
4576                 SELECT 'OKX_PARTY' TYPE, P.PARTY_ID ID1, '#' ID2, P.PARTY_NAME NAME,
4577                 P.PARTY_NUMBER DESCRIPTION, NULL INV_ORG_ID
4578                 FROM HZ_PARTIES P
4579                 WHERE P.PARTY_TYPE IN ( 'PERSON','ORGANIZATION')
4580             UNION ALL
4581                 SELECT 'OKX_CUSTPROD' TYPE, CII.INSTANCE_ID ID1, '#' ID2, SIT.DESCRIPTION NAME,
4582                 BK.CONCATENATED_SEGMENTS DESCRIPTION, BK.ORGANIZATION_ID INV_ORG_ID
4583                 FROM CSI_ITEM_INSTANCES CII, CSI_I_PARTIES CIP,
4584                 MTL_SYSTEM_ITEMS_B_KFV BK, MTL_SYSTEM_ITEMS_TL SIT
4585                 WHERE CII.INSTANCE_ID = CIP.INSTANCE_ID AND CIP.RELATIONSHIP_TYPE_CODE = 'OWNER'
4586                 AND CIP.PARTY_SOURCE_TABLE = 'HZ_PARTIES' AND
4587                 NOT EXISTS ( SELECT 1 FROM CSI_INSTALL_PARAMETERS CIPM
4588                             WHERE CIPM.INTERNAL_PARTY_ID = CIP.PARTY_ID )
4589                 AND BK.INVENTORY_ITEM_ID = CII.INVENTORY_ITEM_ID
4590                 AND SIT.INVENTORY_ITEM_ID = BK.INVENTORY_ITEM_ID
4591                 AND SIT.ORGANIZATION_ID = BK.ORGANIZATION_ID
4592                 AND SIT.LANGUAGE = USERENV('LANG')
4593             UNION ALL
4594                 SELECT  'OKX_CUSTACCT' TYPE, CA.CUST_ACCOUNT_ID ID1, '#' ID2,
4595                 decode(CA.ACCOUNT_NAME, null, 	P.PARTY_NAME, CA.Account_NAME) NAME,
4596                 CA.ACCOUNT_NUMBER DESCRIPTION, NULL INV_ORG_ID
4597                 FROM HZ_CUST_ACCOUNTS CA,HZ_PARTIES P
4598                 WHERE CA.PARTY_ID = P.PARTY_ID
4599             UNION ALL
4600                 SELECT 'OKX_COUNTER' TYPE, CCT.COUNTER_ID ID1, '#' ID2, CCT.NAME NAME,
4601                 CCT.DESCRIPTION DESCRIPTION, NULL INV_ORG_ID
4602                 FROM CSI_COUNTERS_TL CCT WHERE CCT.LANGUAGE = USERENV('LANG')
4603             UNION ALL
4604                 SELECT 'OKX_COVITEM' TYPE, B.INVENTORY_ITEM_ID ID1, to_char(B.ORGANIZATION_ID) ID2,
4605                 T.DESCRIPTION NAME, B.CONCATENATED_SEGMENTS DESCRIPTION, B.ORGANIZATION_ID INV_ORG_ID
4606                 FROM MTL_SYSTEM_ITEMS_B_KFV B,MTL_SYSTEM_ITEMS_TL T
4607                 WHERE B.INVENTORY_ITEM_ID = T.INVENTORY_ITEM_ID AND B.ORGANIZATION_ID = T.ORGANIZATION_ID
4608                 AND T.LANGUAGE = USERENV('LANG')
4609         ) iv, okc_k_items it, oks_k_lines_b sl, okc_k_lines_b kl, okc_k_headers_all_b kh
4610         WHERE iv.type = it.jtot_object1_code -- bug 5218936
4611         AND iv.id1 = it.object1_id1
4612         AND iv.id2 = it.object1_id2
4613         AND decode(iv.inv_org_id, null, kh.inv_organization_id, iv.inv_org_id) = kh.inv_organization_id
4614         AND it.cle_id = kl.id
4615         AND sl.cle_id = kl.id
4616         AND kl.lse_id IN (7,8,9,10,11,35, 13, 18, 25)
4617         AND kl.dnz_chr_id = kh.id
4618         AND kh.id = cp_chr_id;
4619 
4620  -- bug 4992884 , invoice text for counters lse_id = 13
4621 CURSOR csr_counter_inv_text (cp_chr_id IN NUMBER, cp_sl_id IN NUMBER) IS
4622 SELECT it.concatenated_segments AS Name ,
4623        it.Description AS Description
4624  FROM csi_counters_b ccb ,
4625       csi_counters_tl cct ,
4626       cs_csi_counter_groups cg ,
4627       csi_counter_associations cca ,
4628       csi_item_instances cp ,
4629       mtl_system_items_kfv it,
4630       okc_k_items items,
4631       oks_k_lines_b kl,
4632       okc_k_headers_all_b khr
4633 WHERE ccb.counter_id = cct.counter_id
4634   AND cct.language = USERENV('LANG')
4635   AND ccb.group_id = cg.counter_group_id
4636   AND ccb.counter_id = cca.counter_id
4637   AND cca.source_object_code = 'CP'
4638   AND cca.source_object_id = cp.instance_id
4639   AND cp.inventory_item_id = it.inventory_item_id
4640   AND ccb.counter_id = items.object1_id1
4641   AND items.dnz_chr_id = khr.id
4642   AND it.organization_id = khr.inv_organization_id
4643   AND kl.cle_id = items.cle_id
4644   AND khr.id = cp_chr_id
4645   AND kl.id = cp_sl_id;
4646 
4647 l_counter_inv_text_rec csr_counter_inv_text%ROWTYPE;
4648 
4649 
4650 
4651     BEGIN
4652 
4653         --log key input parameters
4654         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4655             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
4656                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
4657             END IF;
4658         END IF;
4659 
4660         --standard api initilization and checks
4661         SAVEPOINT update_invoice_text_PVT;
4662         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
4663             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4664         END IF;
4665         IF FND_API.to_boolean(p_init_msg_list ) THEN
4666             FND_MSG_PUB.initialize;
4667         END IF;
4668         x_return_status := FND_API.G_RET_STS_SUCCESS;
4669 
4670         --we will first get and update the topline invoice text
4671         OPEN c_get_topline_txt(p_chr_id);
4672         LOOP
4673             FETCH c_get_topline_txt BULK COLLECT INTO l_line_tbl LIMIT G_BULK_FETCH_LIMIT;
4674 
4675             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4676                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_top_lines', 'l_line_tbl.count='||l_line_tbl.count);
4677             END IF;
4678 
4679             EXIT WHEN (l_line_tbl.count = 0);
4680 
4681             FOR i in l_line_tbl.first..l_line_tbl.last LOOP
4682                 l_sl_id_tbl(i) :=  l_line_tbl(i).sl_id;
4683                 l_inv_txt_tbl(i) := SUBSTR(l_line_tbl(i).name || ':' || l_line_tbl(i).start_date || ':' || l_line_tbl(i).end_date, 1, 450);
4684 
4685                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4686                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_top_lines_loop', 'i='||i||' ,l_line_tbl(i).name='||l_line_tbl(i).name
4687                     ||' ,l_line_tbl(i).start_date='||l_line_tbl(i).start_date||' ,l_line_tbl(i).end_date='||l_line_tbl(i).end_date);
4688                 END IF;
4689 
4690             END LOOP;
4691 
4692             --update the oks_k_lines_tl for the toplines
4693             FORALL j in l_sl_id_tbl.first..l_sl_id_tbl.last
4694                 UPDATE oks_k_lines_tl
4695                     SET invoice_text = l_inv_txt_tbl(j)
4696                     WHERE id = l_sl_id_tbl(j) AND language = USERENV('LANG');
4697 
4698             l_line_tbl.delete;
4699             l_inv_txt_tbl.delete;
4700             l_sl_id_tbl.delete;
4701 
4702         END LOOP; --topline bulk fetch loop
4703         CLOSE c_get_topline_txt;
4704         l_line_tbl.delete;
4705         l_inv_txt_tbl.delete;
4706         l_sl_id_tbl.delete;
4707 
4708         --now update the subline invoice text, this requires the top line item desc
4709         l_disp_pref := fnd_profile.VALUE('OKS_ITEM_DISPLAY_PREFERENCE');
4710         OPEN c_get_subline_txt(p_chr_id);
4711         LOOP
4712             FETCH c_get_subline_txt BULK COLLECT INTO l_line_tbl LIMIT G_BULK_FETCH_LIMIT;
4713 
4714             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4715                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_sub_lines', 'l_line_tbl.count='||l_line_tbl.count);
4716             END IF;
4717 
4718             EXIT WHEN (l_line_tbl.count = 0);
4719 
4720             FOR i in l_line_tbl.first..l_line_tbl.last LOOP
4721                 l_sl_id_tbl(i) :=  l_line_tbl(i).sl_id;
4722                 l_cle_id_tbl(i) := l_line_tbl(i).cle_id;
4723 
4724                 --discuss with Ramesh
4725                 --7,8,9,10,11,35,13,18,25
4726                 --9,18,25(auth),  8,10,11,35(renew), (7,13???)
4727                 --IF (l_line_tbl(i).lse_id IN (8, 10, 11, 35)) THEN
4728 
4729                 IF (l_line_tbl(i).lse_id = 13) THEN
4730                   -- bug 4992884
4731                   OPEN csr_counter_inv_text (cp_chr_id => p_chr_id, cp_sl_id => l_line_tbl(i).sl_id);
4732                     FETCH csr_counter_inv_text INTO l_counter_inv_text_rec;
4733                   CLOSE csr_counter_inv_text;
4734                     IF ( nvl(l_disp_pref, 'X') = 'DISPLAY_DESC') THEN
4735                        l_inv_txt_tbl(i) := SUBSTR(l_line_tbl(i).num_of_items || ':' || l_counter_inv_text_rec.name|| ':'|| l_line_tbl(i).start_date || ':' || l_line_tbl(i).end_date, 1, 450);
4736 					ELSE
4737 					   l_inv_txt_tbl(i) := SUBSTR(l_line_tbl(i).num_of_items || ':' || l_counter_inv_text_rec.description|| ':'|| l_line_tbl(i).start_date || ':' || l_line_tbl(i).end_date, 1, 450);
4738 					END IF;
4739                 ELSIF (l_line_tbl(i).lse_id NOT IN (9,18,25)) THEN
4740                    l_inv_txt_tbl(i) := SUBSTR(l_line_tbl(i).num_of_items || ':' || l_line_tbl(i).descr|| ':'|| l_line_tbl(i).start_date || ':' || l_line_tbl(i).end_date, 1, 450);
4741                 ELSE
4742                    IF ( nvl(l_disp_pref, 'X') = 'DISPLAY_DESC') THEN
4743                         l_inv_txt_tbl(i) := SUBSTR(l_line_tbl(i).num_of_items || ':' || l_line_tbl(i).name|| ':'|| l_line_tbl(i).start_date || ':' || l_line_tbl(i).end_date, 1, 450);
4744                     ELSE
4745                         l_inv_txt_tbl(i) := SUBSTR(l_line_tbl(i).num_of_items || ':' || l_line_tbl(i).descr|| ':'|| l_line_tbl(i).start_date || ':' || l_line_tbl(i).end_date, 1, 450);
4746                     END IF;
4747                 END IF;
4748 
4749                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4750                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_sub_lines_loop', 'i='||i||' ,l_line_tbl(i).name='||l_line_tbl(i).name||' ,l_line_tbl(i).descr='||l_line_tbl(i).descr||
4751                     ' ,l_line_tbl(i).num_of_items='||l_line_tbl(i).num_of_items||' ,l_line_tbl(i).start_date='||l_line_tbl(i).start_date||' ,l_line_tbl(i).end_date='||l_line_tbl(i).end_date);
4752                 END IF;
4753 
4754             END LOOP;
4755 
4756             --update the oks_k_lines_tl for the sublines using toplines inv txt
4757             FORALL j in l_sl_id_tbl.first..l_sl_id_tbl.last
4758                 UPDATE oks_k_lines_tl c
4759                     SET c.invoice_text =
4760                         (SELECT SUBSTR(a.invoice_text,1, decode(INSTR(a.invoice_text, ':'),0,
4761                         LENGTH(a.invoice_text), INSTR(a.invoice_text, ':'))) ||l_inv_txt_tbl(j)
4762                         FROM oks_k_lines_tl a, oks_k_lines_b b
4763                         WHERE a.id = b.id AND a.language = USERENV('LANG')
4764                         AND b.cle_id = l_cle_id_tbl(j))
4765                     WHERE id = l_sl_id_tbl(j) AND language = USERENV('LANG');
4766             l_line_tbl.delete;
4767             l_inv_txt_tbl.delete;
4768             l_sl_id_tbl.delete;
4769             l_cle_id_tbl.delete;
4770         END LOOP; --topline bulk fetch loop
4771         CLOSE c_get_subline_txt;
4772         l_line_tbl.delete;
4773         l_inv_txt_tbl.delete;
4774         l_sl_id_tbl.delete;
4775         l_cle_id_tbl.delete;
4776 
4777         --standard check of p_commit
4778 	    IF FND_API.to_boolean( p_commit ) THEN
4779 		    COMMIT;
4780 	    END IF;
4781         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4782             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
4783                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
4784             END IF;
4785         END IF;
4786         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4787 
4788     EXCEPTION
4789         WHEN FND_API.g_exc_error THEN
4790             ROLLBACK TO update_invoice_text_PVT;
4791             x_return_status := FND_API.g_ret_sts_error ;
4792 
4793             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4794                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
4795             END IF;
4796             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4797 
4798         WHEN FND_API.g_exc_unexpected_error THEN
4799             ROLLBACK TO update_invoice_text_PVT;
4800             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4801 
4802             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4803                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
4804             END IF;
4805             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4806 
4807         WHEN OTHERS THEN
4808             ROLLBACK TO update_invoice_text_PVT;
4809             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4810 
4811             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4812                 --first log the sqlerrm
4813                 l_error_text := substr (SQLERRM, 1, 240);
4814                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
4815                 --then add it to the message api list
4816                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
4817             END IF;
4818             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4819 
4820     END UPDATE_INVOICE_TEXT;
4821 
4822     /*
4823     Procedure for getting the user id and name of the contact on whose behalf the
4824     contract workflow is launched during renewal
4825     Parameters
4826         p_chr_id            : id of the contract for which the workflow is launched
4827         p_hdesk_user_id     : fnd user id of the help desk user id setup in GCD. Optional,
4828                               if not passed will be derived from GCD.
4829 
4830     If no vendor/merchant contact bases on jtf object 'OKX_SALEPERS' can be found for the contract
4831     header, the help desk user is used. This behaviour is from R12 onwards, prior to this if a
4832     salesrep was not found, contract admin and then contract approver would be used.
4833     */
4834     PROCEDURE GET_USER_NAME
4835     (
4836      p_api_version IN NUMBER DEFAULT 1,
4837      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4838      x_return_status OUT NOCOPY VARCHAR2,
4839      x_msg_count OUT NOCOPY NUMBER,
4840      x_msg_data OUT NOCOPY VARCHAR2,
4841      p_chr_id IN NUMBER,
4842      p_hdesk_user_id IN NUMBER,
4843      x_user_id OUT NOCOPY NUMBER,
4844      x_user_name OUT NOCOPY VARCHAR2
4845     )
4846     IS
4847     l_api_name CONSTANT VARCHAR2(30) := 'GET_USER_NAME';
4848     l_api_version CONSTANT NUMBER := 1;
4849     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
4850     l_error_text VARCHAR2(512);
4851 
4852     --should be an outer join with party roles, so that
4853     --if we don't get a vendor/merchant party we atleast get an org
4854     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
4855         SELECT nvl(a.org_id, a.authoring_org_id), b.id
4856         FROM okc_k_headers_all_b a LEFT OUTER JOIN okc_k_party_roles_b b
4857         ON  a.id = b.dnz_chr_id
4858         AND b.cle_id IS NULL
4859         AND b.rle_code IN ('VENDOR', 'MERCHANT')
4860         WHERE a.id = cp_chr_id;
4861 
4862     CURSOR c_k_srep_user(cp_chr_id IN NUMBER, cp_cpl_id IN NUMBER, cp_org_id IN NUMBER) IS
4863         SELECT
4864         --rsc.resource_id, srp.salesrep_id, srp.org_id, ctc.cro_code,
4865         fnd.user_id, fnd.user_name
4866         FROM okc_contacts ctc,  fnd_user fnd,
4867             jtf_rs_resource_extns rsc, jtf_rs_salesreps srp
4868         WHERE ctc.dnz_chr_id = cp_chr_id
4869         AND ctc.cpl_id = cp_cpl_id
4870         AND ctc.cro_code IN (SELECT  src.cro_code FROM okc_contact_sources src
4871                                 WHERE src.rle_code IN ('VENDOR', 'MERCHANT')
4872                                 AND src.jtot_object_code = 'OKX_SALEPERS'
4873                                 AND src.buy_or_sell = 'S')
4874         AND srp.salesrep_id = to_number(ctc.object1_id1)
4875         AND nvl(srp.org_id, -99) = cp_org_id
4876         AND srp.resource_id = rsc.resource_id
4877         AND rsc.user_id = fnd.user_id;
4878 
4879     CURSOR c_fnd_user(cp_user_id IN NUMBER) IS
4880         SELECT user_name
4881         FROM fnd_user
4882         WHERE user_id = cp_user_id;
4883 
4884     l_org_id        NUMBER;
4885     l_cpl_id        NUMBER;
4886     l_user_id       NUMBER;
4887     l_user_name     VARCHAR2(100);
4888 
4889     l_rnrl_rec      OKS_RENEW_UTIL_PVT.rnrl_rec_type;
4890     x_rnrl_rec      OKS_RENEW_UTIL_PVT.rnrl_rec_type;
4891 
4892     BEGIN
4893 
4894         --log key input parameters
4895         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4896             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_hdesk_user_id='||p_hdesk_user_id);
4897         END IF;
4898 
4899         --standard api initilization and checks
4900         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
4901             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4902         END IF;
4903         IF FND_API.to_boolean(p_init_msg_list ) THEN
4904             FND_MSG_PUB.initialize;
4905         END IF;
4906         x_return_status := FND_API.G_RET_STS_SUCCESS;
4907 
4908         --first get the contract org and id for the merchant/vendor record in okc_k_party_roles_b
4909         OPEN c_k_hdr(p_chr_id);
4910         FETCH c_k_hdr INTO l_org_id, l_cpl_id;
4911         CLOSE c_k_hdr;
4912 
4913         IF (l_org_id IS NULL) THEN
4914             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
4915             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
4916             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4917                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation', FALSE);
4918             END IF;
4919             FND_MSG_PUB.ADD;
4920             RAISE FND_API.g_exc_error;
4921         END IF;
4922 
4923         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4924             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_org_cpl', 'l_org_id=' || l_org_id||' ,l_cpl_id='||l_cpl_id);
4925         END IF;
4926 
4927         --now get the fnd user id/name for the contact of type 'OKX_SALEPERS', if a vendor/merchant party
4928         --is found
4929         IF (l_cpl_id IS NOT NULL) THEN
4930 
4931             OPEN c_k_srep_user(p_chr_id, l_cpl_id, l_org_id);
4932             FETCH c_k_srep_user INTO l_user_id, l_user_name;
4933             CLOSE c_k_srep_user;
4934 
4935             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4936                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_k_user', 'l_user_id='||l_user_id||' , l_user_name='||l_user_name);
4937             END IF;
4938 
4939         END IF;
4940 
4941         --if no salesrep found, default to helpdesk user
4942         IF (l_user_id IS NULL) THEN
4943             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4944                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_helpdesk_user', 'no salesrep found in contract, getting the helpdesk user');
4945             END IF;
4946 
4947             IF (p_hdesk_user_id IS NOT NULL) THEN
4948                 l_user_id := p_hdesk_user_id;
4949             ElSE
4950                 --get the helpdesk user id from GCD
4951                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4952                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calling_get_renew_rules', 'p_chr_id=' || p_chr_id ||', p_date='|| sysdate);
4953                 END IF;
4954 
4955                 OKS_RENEW_UTIL_PVT.get_renew_rules(
4956                                 x_return_status => x_return_status,
4957                                 p_api_version => 1.0,
4958                                 p_init_msg_list => FND_API.G_FALSE,
4959                                 p_chr_id => p_chr_id,
4960                                 p_party_id => NULL,
4961                                 p_org_id => NULL,
4962                                 p_date => sysdate,
4963                                 p_rnrl_rec => l_rnrl_rec,
4964                                 x_rnrl_rec => x_rnrl_rec,
4965                                 x_msg_count => x_msg_count,
4966                                 x_msg_data => x_msg_data);
4967 
4968                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4969                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_get_renew_rules', 'x_return_status=' || x_return_status ||' ,l_user_id='|| x_rnrl_rec.user_id);
4970                 END IF;
4971                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
4972                     RAISE FND_API.g_exc_unexpected_error;
4973                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
4974                     RAISE FND_API.g_exc_error;
4975                 END IF;
4976 
4977                 l_user_id := x_rnrl_rec.user_id;
4978 
4979             END IF;
4980 
4981             --commented out, do not throw error if no helpdesk setup
4982             --so that renewal can continue
4983             /*
4984             IF (l_user_id IS NULL) THEN
4985                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_HELPDESK');
4986                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4987                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_helpdesk_user', FALSE);
4988                 END IF;
4989                 FND_MSG_PUB.ADD;
4990                 RAISE FND_API.g_exc_error;
4991             END IF;
4992             */
4993 
4994             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4995                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_helpdesk_user', 'getting user name for user id='||l_user_id);
4996             END IF;
4997 
4998             OPEN c_fnd_user(l_user_id);
4999             FETCH c_fnd_user INTO l_user_name;
5000             CLOSE c_fnd_user;
5001         END IF;
5002         x_user_id := l_user_id;
5003         x_user_name := l_user_name;
5004 
5005 
5006         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
5007             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status||' ,x_user_id='||x_user_id||' ,x_user_name='||x_user_name);
5008         END IF;
5009         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5010 
5011     EXCEPTION
5012         WHEN FND_API.g_exc_error THEN
5013             x_return_status := FND_API.g_ret_sts_error ;
5014 
5015             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5016                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
5017             END IF;
5018             IF (c_k_hdr%isopen) THEN
5019                 CLOSE c_k_hdr;
5020             END IF;
5021             IF (c_k_srep_user%isopen) THEN
5022                 CLOSE c_k_srep_user;
5023             END IF;
5024             IF (c_fnd_user%isopen) THEN
5025                 CLOSE c_fnd_user;
5026             END IF;
5027             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5028 
5029         WHEN FND_API.g_exc_unexpected_error THEN
5030             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5031 
5032             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
5033                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
5034             END IF;
5035             IF (c_k_hdr%isopen) THEN
5036                 CLOSE c_k_hdr;
5037             END IF;
5038             IF (c_k_srep_user%isopen) THEN
5039                 CLOSE c_k_srep_user;
5040             END IF;
5041             IF (c_fnd_user%isopen) THEN
5042                 CLOSE c_fnd_user;
5043             END IF;
5044             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5045 
5046         WHEN OTHERS THEN
5047             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5048 
5049             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
5050                 --first log the sqlerrm
5051                 l_error_text := substr (SQLERRM, 1, 240);
5052                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
5053                 --then add it to the message api list
5054                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
5055             END IF;
5056             IF (c_k_hdr%isopen) THEN
5057                 CLOSE c_k_hdr;
5058             END IF;
5059             IF (c_k_srep_user%isopen) THEN
5060                 CLOSE c_k_srep_user;
5061             END IF;
5062             IF (c_fnd_user%isopen) THEN
5063                 CLOSE c_fnd_user;
5064             END IF;
5065             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5066 
5067     END GET_USER_NAME;
5068 
5069 
5070     /*
5071 	From R12 onwards, this procedure should be used to renew service contracts.
5072     It will be redesigned to do the following
5073         1.	Improve performance
5074         2.	Reduce dependence on OKC code
5075         3.	Incorporate functional design changes for R12
5076         4.	Comply with current Oracle Applications coding and logging standards
5077         5.	Ease of maintenance
5078 
5079     Parameters
5080         p_chr_id                    :   id of the contract being renewed, mandatory
5081         p_new_contract_number       :   contract number for the renewed contract, optional
5082         p_new_contract_modifier     :   contract modifier for the renewed contract, optional
5083         p_new_start_date            :   start date for the renewed contract, optional
5084         p_new_end_date              :   end date for the renewed contract, optional
5085         p_new_duration              :   duration for renewed contract, optional
5086         p_new_uom_code              :   period for the renewed contract, optional
5087         p_renewal_called_from_ui    :  'Y' - called from UI, N - called from Events
5088         x_chr_id                :   id of the renewed contract
5089         x_return_status             :   S, E, U - standard values
5090 
5091     Defaulting rules
5092         1. If p_new_contract_number is not passed, uses the source contract_number
5093         2. If p_new_contract_modifier is not passed, generated this as
5094             fnd_profile.VALUE('OKC_CONTRACT_IDENTIFIER') || to_char(SYSDATE, 'DD-MON-YYYY HH24:MI:SS')
5095         3. If p_new_start_date is not passed, defaults to source contract end_date +1
5096         4. If p_new_end_date is not passed, derived from p_new_duration/p_new_uom_code
5097             and p_new_start_date. If p_new_duration/p_new_uom_code are also not passed
5098             used the source contract duration/period
5099     */
5100 
5101     PROCEDURE RENEW_CONTRACT
5102     (
5103      p_api_version IN NUMBER,
5104      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
5105      p_commit   IN VARCHAR2 DEFAULT FND_API.G_FALSE,
5106      p_chr_id IN NUMBER,
5107      p_new_contract_number IN okc_k_headers_b.contract_number%TYPE,
5108      p_new_contract_modifier IN okc_k_headers_b.contract_number_modifier%TYPE,
5109      p_new_start_date IN DATE,
5110      p_new_end_date IN DATE,
5111      p_new_duration IN NUMBER,
5112      p_new_uom_code IN MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE,
5113      p_renewal_called_from_ui IN VARCHAR2 DEFAULT 'Y',
5114      x_chr_id OUT NOCOPY NUMBER,
5115      x_msg_count OUT NOCOPY NUMBER,
5116      x_msg_data OUT NOCOPY VARCHAR2,
5117      x_return_status OUT NOCOPY VARCHAR2
5118      )
5119     IS
5120     l_api_name CONSTANT VARCHAR2(30) := 'RENEW_CONTRACT';
5121     l_api_version CONSTANT NUMBER := 1;
5122     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
5123     l_error_text VARCHAR2(512);
5124 
5125     --also check if it is a service contract
5126     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
5127         SELECT contract_number, contract_number_modifier, start_date, end_date,
5128         renewal_type_code, renewal_end_date, currency_code
5129         FROM okc_k_headers_all_b
5130         WHERE id = cp_chr_id AND application_id = 515;
5131 
5132     CURSOR c_renk_hdr(cp_chr_id IN NUMBER) IS
5133         SELECT currency_code, org_id
5134         FROM okc_k_headers_all_b WHERE id = cp_chr_id;
5135 
5136 
5137     l_k_num                 okc_k_headers_b.contract_number%TYPE;
5138     l_k_mod                 okc_k_headers_b.contract_number_modifier%TYPE;
5139     l_k_start_date          DATE;
5140     l_k_end_date            DATE;
5141     l_k_ren_type            okc_k_headers_b.renewal_type_code%TYPE;
5142     l_k_renewal_end_date    DATE;
5143     l_k_currency_code       VARCHAR2(15);
5144 
5145     l_validation_level      VARCHAR2(1);
5146     l_rnrl_rec              OKS_RENEW_UTIL_PVT.rnrl_rec_type;
5147     l_rnrl_rec_dummy        OKS_RENEW_UTIL_PVT.rnrl_rec_type;
5148     l_validation_status     VARCHAR2(1);
5149     l_validation_tbl        validation_tbl_type;
5150 
5151     l_renk_num              okc_k_headers_b.contract_number%TYPE;
5152     l_renk_mod              okc_k_headers_b.contract_number_modifier%TYPE;
5153     l_renk_start_date       DATE;
5154     l_renk_end_date         DATE;
5155     l_renk_currency_code    VARCHAR2(15);
5156     l_renk_org_id           NUMBER;
5157 
5158     l_user_id               NUMBER;
5159     l_user_name             VARCHAR2(100);
5160     l_renewal_type          VARCHAR2(30);
5161     l_approval_type         VARCHAR2(30);
5162     l_warnings              BOOLEAN := FALSE;
5163 
5164     BEGIN
5165         --log key input parameters
5166         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
5167             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
5168                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_api_version=' || p_api_version ||' ,p_commit='|| p_commit ||' ,p_chr_id='|| p_chr_id||' , p_new_contract_number='||p_new_contract_number||
5169                 ' ,p_new_contract_modifier='||p_new_contract_modifier||' ,p_new_start_date='||p_new_start_date||' ,p_new_end_date='||p_new_end_date||
5170                 ' ,p_new_duration='||p_new_duration||' ,p_new_uom_code='||p_new_uom_code||' ,p_renewal_called_from_ui='||p_renewal_called_from_ui);
5171             END IF;
5172         END IF;
5173 
5174 	 -- Put the parameters in the log file
5175          fnd_file.put_line(FND_FILE.LOG,'  ');
5176          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5177          fnd_file.put_line(FND_FILE.LOG,'Calling OKS_RENEW_CONTRACT_PVT.RENEW_CONTRACT');
5178          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5179          fnd_file.put_line(FND_FILE.LOG,'Parameters  ');
5180          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5181          fnd_file.put_line(FND_FILE.LOG,'p_api_version :  '||p_api_version);
5182          fnd_file.put_line(FND_FILE.LOG,'p_init_msg_list :  '||p_init_msg_list);
5183          fnd_file.put_line(FND_FILE.LOG,'p_commit :  '||p_commit);
5184          fnd_file.put_line(FND_FILE.LOG,'p_chr_id :  '||p_chr_id);
5185          fnd_file.put_line(FND_FILE.LOG,'p_new_contract_number :  '||p_new_contract_number);
5186          fnd_file.put_line(FND_FILE.LOG,'p_new_contract_modifier :  '||p_new_contract_modifier);
5187          fnd_file.put_line(FND_FILE.LOG,'p_new_start_date :  '||p_new_start_date);
5188          fnd_file.put_line(FND_FILE.LOG,'p_new_end_date :  '||p_new_end_date);
5189          fnd_file.put_line(FND_FILE.LOG,'p_new_duration :  '||p_new_duration);
5190          fnd_file.put_line(FND_FILE.LOG,'p_new_uom_code :  '||p_new_uom_code);
5191          fnd_file.put_line(FND_FILE.LOG,'p_renewal_called_from_ui :  '||p_renewal_called_from_ui);
5192          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5193          fnd_file.put_line(FND_FILE.LOG,'  ');
5194 
5195 
5196         --standard api initilization and checks
5197         SAVEPOINT renew_contract_PVT;
5198         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
5199             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5200         END IF;
5201         IF FND_API.to_boolean(p_init_msg_list ) THEN
5202             FND_MSG_PUB.initialize;
5203         END IF;
5204         x_return_status := FND_API.G_RET_STS_SUCCESS;
5205 
5206 	    /*
5207 	        Step 1: do basic parameter validation
5208 	    */
5209 
5210          fnd_file.put_line(FND_FILE.LOG,'  ');
5211          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5212          fnd_file.put_line(FND_FILE.LOG,'Step 1: do basic parameter validation  ');
5213          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5214          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5215          fnd_file.put_line(FND_FILE.LOG,'  ');
5216 
5217         --Step 1 do basic parameter validation
5218         --first get the basic contract attributes
5219         OPEN c_k_hdr(p_chr_id);
5220         FETCH c_k_hdr INTO l_k_num, l_k_mod, l_k_start_date, l_k_end_date, l_k_ren_type,
5221         l_k_renewal_end_date, l_k_currency_code;
5222 
5223         --invalid contract id or if it's not a service contract
5224         IF (c_k_hdr%notfound) THEN
5225             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
5226             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
5227             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5228                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation1', FALSE);
5229             END IF;
5230             FND_MSG_PUB.ADD;
5231             CLOSE c_k_hdr;
5232             RAISE FND_API.g_exc_error;
5233         END IF;
5234         CLOSE c_k_hdr;
5235 
5236         --new start date < original end date
5237         IF (p_new_start_date IS NOT NULL) AND (p_new_start_date < l_k_end_date) THEN
5238             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NEW_START_MORE_END');
5239             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5240                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation2', FALSE);
5241             END IF;
5242             FND_MSG_PUB.ADD;
5243             RAISE FND_API.g_exc_error;
5244         END IF;
5245 
5246         --new end date < new start date, if new start date is null use old end date + 1
5247         IF (p_new_end_date IS NOT NULL) AND (p_new_end_date < nvl(p_new_start_date, l_k_end_date + 1)) THEN
5248             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVALID_END_DATE');
5249             FND_MESSAGE.set_token('START_DATE', to_char(nvl(p_new_start_date, l_k_end_date + 1)));
5250             FND_MESSAGE.set_token('END_DATE', to_char(p_new_end_date));
5251             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5252                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation3', FALSE);
5253             END IF;
5254             FND_MSG_PUB.ADD;
5255             RAISE FND_API.g_exc_error;
5256         END IF;
5257         --end basic parameter validation
5258 
5259 	    /*
5260 	        Step 2: do renewal validation and at the same time fetch the renewal rules
5261 	    */
5262 
5263          fnd_file.put_line(FND_FILE.LOG,'  ');
5264          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5265          fnd_file.put_line(FND_FILE.LOG,'Step 2: do renewal validation and at the same time fetch the renewal rules  ');
5266          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5267          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5268          fnd_file.put_line(FND_FILE.LOG,'  ');
5269 
5270         --Step 2 do renewal validation and at the same time fetch the renewal rules
5271         --if called from UI, then user has ready seen the warnings, so we check only for errors
5272         --if called from Events, we need to check for both errors and warnings
5273         IF (p_renewal_called_from_ui = 'Y') THEN
5274             l_validation_level := G_VALIDATE_ERRORS;
5275         ELSE
5276             l_validation_level := G_VALIDATE_ALL;
5277         END IF;
5278 
5279         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5280             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.renewal_validation', 'calling OKS_RENEW_UTIL_PVT.validate_renewal, p_chr_id='||p_chr_id||' ,p_date='||to_char(nvl(p_new_start_date, l_k_end_date + 1))||
5281             ' ,p_validation_level='||l_validation_level);
5282         END IF;
5283 
5284         validate_renewal(
5285             p_api_version =>  1,
5286             p_init_msg_list => FND_API.G_FALSE,
5287             x_return_status => x_return_status,
5288             x_msg_count => x_msg_count,
5289             x_msg_data => x_msg_data,
5290             p_chr_id => p_chr_id,
5291             p_date => nvl(p_new_start_date, l_k_end_date + 1),
5292             p_validation_level => l_validation_level,
5293             x_rnrl_rec => l_rnrl_rec,
5294             x_validation_status => l_validation_status,
5295             x_validation_tbl => l_validation_tbl);
5296 
5297         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5298             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.renewal_validation', 'after call to OKS_RENEW_UTIL_PVT.validate_renewal, x_return_status='||x_return_status||' ,x_validation_status='||l_validation_status);
5299         END IF;
5300 
5301         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5302             RAISE FND_API.g_exc_unexpected_error;
5303         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5304             RAISE FND_API.g_exc_error;
5305         END IF;
5306 
5307         --if validation errors - stop
5308         --if validation warnings - stop if called from events
5309         IF (    (l_validation_status = G_VALID_STS_ERROR) OR
5310                 (   (l_validation_status = G_VALID_STS_WARNING) AND
5311                 (p_renewal_called_from_ui = 'N')    )
5312         ) THEN
5313             --add all validation messages to the FND_MSG_PUB stack
5314             FOR i in l_validation_tbl.FIRST..l_validation_tbl.LAST LOOP
5315                 --OKS_USER_DEFINED_MESSAGE is a special message, with the message body = MESSAGE
5316                 --This is a workaround, because we can't directly add messages to the msg API list
5317                 --using FND_MSG_PUB.add. FND_MSG_PUB.add expects messages on the message stack (FND_MESSAGE.set_name)
5318                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_USER_DEFINED_MESSAGE');
5319                 FND_MESSAGE.set_token('MESSAGE', l_validation_tbl(i).message);
5320                 FND_MSG_PUB.add;
5321             END LOOP;
5322             RAISE FND_API.g_exc_error;
5323         END IF;
5324         --end renewal validation
5325 
5326 	    /*
5327 	        Step 3: default attributes
5328 	    */
5329 
5330          fnd_file.put_line(FND_FILE.LOG,'  ');
5331          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5332          fnd_file.put_line(FND_FILE.LOG,'Step 3: default attributes ');
5333          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5334          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5335          fnd_file.put_line(FND_FILE.LOG,'  ');
5336 
5337         --Step 3 default attributes
5338         l_renk_num := nvl(p_new_contract_number, l_k_num);
5339         l_renk_mod := nvl(p_new_contract_modifier, fnd_profile.VALUE('OKC_CONTRACT_IDENTIFIER') || to_char(SYSDATE, 'DD-MON-YYYY HH24:MI:SS'));
5340         l_renk_start_date := trunc(nvl(p_new_start_date, l_k_end_date + 1));
5341         l_renk_end_date := get_end_date(
5342                             p_new_start_date => l_renk_start_date,
5343                             p_new_end_date => p_new_end_date,
5344                             p_new_duration => p_new_duration,
5345                             p_new_uom_code => p_new_uom_code,
5346                             p_old_start_date => l_k_start_date,
5347                             p_old_end_date => l_k_end_date,
5348                             p_renewal_end_date => l_k_renewal_end_date,
5349                             p_ren_type => l_k_ren_type,
5350                             x_return_status => x_return_status);
5351 
5352         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5353             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.default_attributes', 'done   l_renk_num='||l_renk_num||' ,l_renk_mod='||l_renk_mod||' ,l_renk_start_date='||to_char(l_renk_start_date)||
5354             ' ,l_renk_end_date='||to_char(l_renk_end_date)||' ,x_return_status='||x_return_status);
5355         END IF;
5356         --end default attributes
5357 
5358 	    /*
5359 	        Step 4: copy contract
5360 	    */
5361 
5362          fnd_file.put_line(FND_FILE.LOG,'  ');
5363          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5364          fnd_file.put_line(FND_FILE.LOG,'Step 4: copy contract ');
5365          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5366          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5367          fnd_file.put_line(FND_FILE.LOG,'  ');
5368 
5369         --Step 4 copy contract
5370         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5371             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.copy_contract', 'calling OKC_COPY_CONTRACT_PUB.copy_contract, p_chr_id='||p_chr_id||' ,p_contract_number='||l_renk_num||' ,p_contract_number_modifier='||l_renk_mod||
5372             ' ,p_to_template_yn=N, p_renew_ref_yn=Y, p_override_org=Y, p_copy_lines_yn=Y ,p_commit=F');
5373         END IF;
5374 
5375         OKS_COPY_CONTRACT_PVT.copy_contract(
5376             p_api_version  => 1,
5377             p_init_msg_list => FND_API.G_FALSE,
5378             x_return_status => x_return_status,
5379             x_msg_count => x_msg_count,
5380             x_msg_data => x_msg_data,
5381             p_commit => FND_API.G_FALSE,
5382             p_chr_id => p_chr_id,
5383             p_contract_number => l_renk_num,
5384             p_contract_number_modifier => l_renk_mod,
5385             p_to_template_yn => 'N',
5386             P_renew_ref_yn => 'Y',
5387             x_to_chr_id => x_chr_id);
5388 
5389         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5390             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.copy_contract', 'after call to OKC_COPY_CONTRACT_PUB.copy_contract, x_return_status='||x_return_status||' ,x_chr_id='||x_chr_id);
5391         END IF;
5392 
5393         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5394             RAISE FND_API.g_exc_unexpected_error;
5395         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5396             RAISE FND_API.g_exc_error;
5397         END IF;
5398 
5399         --sales credit and copy are the 2 places we can return with a warning
5400         IF x_return_status = OKC_API.g_ret_sts_warning THEN -- 'W'
5401             l_warnings := TRUE;
5402         END IF;
5403         --end copy contract
5404 
5405 	    /*
5406 	        Step 5: if the renewed contract currency is different from original contract currency
5407 	    */
5408 
5409          fnd_file.put_line(FND_FILE.LOG,'  ');
5410          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5411          fnd_file.put_line(FND_FILE.LOG,'Step 5:if the renewed contract currency is different');
5412          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5413          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5414          fnd_file.put_line(FND_FILE.LOG,'  ');
5415 
5416         --Step 5 if the renewed contract currency is different from original contract currency
5417         --get the renewal rules again, as thresholds depend on currency
5418         OPEN c_renk_hdr(x_chr_id);
5419         FETCH c_renk_hdr INTO l_renk_currency_code, l_renk_org_id;
5420         CLOSE c_renk_hdr;
5421 
5422         IF (l_renk_currency_code <> l_k_currency_code) THEN
5423 
5424             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5425                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calling_get_renew_rules', 'p_chr_id=' || x_chr_id ||', p_date='|| l_renk_start_date||' ,l_k_currency_code='||l_k_currency_code||' ,l_renk_currency_code='||l_renk_currency_code);
5426             END IF;
5427 
5428             OKS_RENEW_UTIL_PVT.get_renew_rules(
5429                             x_return_status => x_return_status,
5430                             p_api_version => 1.0,
5431                             p_init_msg_list => FND_API.G_FALSE,
5432                             p_chr_id => x_chr_id,
5433                             p_party_id => NULL,
5434                             p_org_id => NULL,
5435                             p_date => l_renk_start_date,
5436                             p_rnrl_rec => l_rnrl_rec_dummy,
5437                             x_rnrl_rec => l_rnrl_rec,
5438                             x_msg_count => x_msg_count,
5439                             x_msg_data => x_msg_data);
5440 
5441             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5442                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_get_renew_rules', 'x_return_status=' || x_return_status);
5443             END IF;
5444             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5445                 RAISE FND_API.g_exc_unexpected_error;
5446             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5447                 RAISE FND_API.g_exc_error;
5448             END IF;
5449 
5450         END IF;
5451         --end of currency/renewal rules check
5452 
5453 	    /*
5454 	        Step 6: adjust the header and line dates
5455 	    */
5456 
5457          fnd_file.put_line(FND_FILE.LOG,'  ');
5458          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5459          fnd_file.put_line(FND_FILE.LOG,'Step 6 : adjust the header and line dates');
5460          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5461          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5462          fnd_file.put_line(FND_FILE.LOG,'  ');
5463 
5464         --Step 6 adjust the header and line dates
5465         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5466             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_dates', 'calling  update_renewal_dates, p_chr_id='||x_chr_id||' ,p_new_start_date='||l_renk_start_date||
5467             ' ,p_new_end_date='||l_renk_end_date||' ,p_old_start_date='||l_k_start_date);
5468         END IF;
5469         update_renewal_dates(
5470             p_chr_id  => x_chr_id,
5471             p_new_start_date => l_renk_start_date,
5472             p_new_end_date => l_renk_end_date,
5473             p_old_start_date => l_k_start_date,
5474             x_msg_count => x_msg_count,
5475             x_msg_data => x_msg_data,
5476             x_return_status => x_return_status);
5477 
5478         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5479             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_dates', 'after  update_renewal_dates, x_return_status='||x_return_status);
5480         END IF;
5481         --end of adjust dates
5482 
5483  	    /*
5484 	        Step 6.1 : Update annualized_factor for the renewed contract lines
5485 	    */
5486 
5487          fnd_file.put_line(FND_FILE.LOG,'  ');
5488          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5489          fnd_file.put_line(FND_FILE.LOG,'Step 6.1 : Update annualized_factor for the renewed contract lines');
5490          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5491          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5492          fnd_file.put_line(FND_FILE.LOG,'  ');
5493 
5494         -- Step 6.1 Update annualized_factor for the renewed contract lines
5495         -- bug 4768227
5496         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5497             FND_LOG.string(FND_LOG.level_statement, l_mod_name , ' calling  update to annualized_factor , p_new_chr_id='||x_chr_id);
5498         END IF;
5499 
5500           UPDATE okc_k_lines_b
5501              SET annualized_factor = OKS_SETUP_UTIL_PUB.Get_Annualized_Factor(start_date, end_date, lse_id)
5502            WHERE dnz_chr_id = x_chr_id;
5503 
5504         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5505             FND_LOG.string(FND_LOG.level_statement, l_mod_name , ' After calling  update to annualized_factor , p_new_chr_id='||x_chr_id);
5506         END IF;
5507 
5508 	    /*
5509 	        Step 7: update the old contract's date renewed column for the lines that are actually renewed
5510 	    */
5511 
5512          fnd_file.put_line(FND_FILE.LOG,'  ');
5513          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5514          fnd_file.put_line(FND_FILE.LOG,'Step 7 : update the old contract date renewed column for the lines that are actually renewed');
5515          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5516          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5517          fnd_file.put_line(FND_FILE.LOG,'  ');
5518 
5519         --Step 7 update the old contract's date renewed column for the lines that are actually renewed
5520         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5521             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_source_contract', 'calling  update_source_contract, p_new_chr_id='||x_chr_id||' ,p_old_chr_id='||p_chr_id);
5522         END IF;
5523         update_source_contract(
5524             p_new_chr_id  => x_chr_id,
5525             p_old_chr_id => p_chr_id,
5526             x_return_status => x_return_status);
5527 
5528         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5529             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_source_contract', 'after  update_source_contract, x_return_status='||x_return_status);
5530         END IF;
5531         --end of adjust date
5532 
5533 	    /*
5534 	        Step 8: adjust the invoice text that is based on the line dates
5535 	    */
5536 
5537          fnd_file.put_line(FND_FILE.LOG,'  ');
5538          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5539          fnd_file.put_line(FND_FILE.LOG,'Step 8 : adjust the invoice text that is based on the line dates');
5540          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5541          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5542          fnd_file.put_line(FND_FILE.LOG,'  ');
5543 
5544         --now adjust all date dependent entities
5545         --Step 8 adjust the invoice text that is based on the line dates
5546         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5547             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.invoice_text', 'calling  update_invoice_text, p_chr_id='||x_chr_id);
5548         END IF;
5549         update_invoice_text(
5550             p_api_version => 1,
5551             p_init_msg_list => FND_API.G_FALSE,
5552             p_commit => FND_API.G_FALSE,
5553             x_return_status => x_return_status,
5554             x_msg_count => x_msg_count,
5555             x_msg_data => x_msg_data,
5556             p_chr_id  => x_chr_id);
5557 
5558         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5559             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.invoice_text', 'after  update_invoice_text, x_return_status='||x_return_status);
5560         END IF;
5561         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5562             RAISE FND_API.g_exc_unexpected_error;
5563         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5564             RAISE FND_API.g_exc_error;
5565         END IF;
5566         --end of invoice text
5567 
5568         /*
5569          bug 4775295 : Commented call to procedure update_condition_headers
5570 
5571         --Step 9 update contract condition(event) headers
5572         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5573             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.condition_header', 'calling  update_condition_headers, p_chr_id='||x_chr_id||' ,p_new_start_date='||l_renk_start_date||' ,p_new_end_date='||l_renk_end_date
5574             ||' ,p_old_start_date='||l_k_start_date||' ,p_old_end_date='||l_k_end_date);
5575         END IF;
5576 
5577         update_condition_headers(
5578             p_chr_id => x_chr_id,
5579             p_new_start_date => l_renk_start_date,
5580             p_new_end_date => l_renk_end_date,
5581             p_old_start_date => l_k_start_date,
5582             p_old_end_date => l_k_end_date,
5583             x_return_status => x_return_status);
5584 
5585         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5586             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.condition_header', 'after  update_condition_headers, x_return_status='||x_return_status);
5587         END IF;
5588         --end of contract condition(event) headers
5589 
5590          */
5591 
5592 	    /*
5593 	        Step 10: adjust the header and line dates
5594 	    */
5595 
5596          fnd_file.put_line(FND_FILE.LOG,'  ');
5597          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5598          fnd_file.put_line(FND_FILE.LOG,'Step 10 : Regenerate subscription schedule/details and coverage entities based on the new dates');
5599          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5600          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5601          fnd_file.put_line(FND_FILE.LOG,'  ');
5602 
5603         --Step 10 Regenerate subscription schedule/details and coverage entities based on the new dates
5604         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5605             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.coverage_subscription', 'calling  recreate_cov_subscr, p_chr_id='||x_chr_id);
5606         END IF;
5607         recreate_cov_subscr(
5608             p_chr_id => x_chr_id,
5609             x_msg_count => x_msg_count,
5610             x_msg_data => x_msg_data,
5611             x_return_status => x_return_status);
5612 
5613         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5614             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.coverage_subscription', 'after  call to recreate_cov_subscr, x_return_status='||x_return_status);
5615         END IF;
5616         --end of coverage/subscription recreation
5617 
5618 	    /*
5619 	        Step 11: adjust the header and line dates
5620 	    */
5621 
5622          fnd_file.put_line(FND_FILE.LOG,'  ');
5623          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5624          fnd_file.put_line(FND_FILE.LOG,'Step 11 : Call pricing API to reprice the contract based on new dates');
5625          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5626          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5627          fnd_file.put_line(FND_FILE.LOG,'  ');
5628 
5629         --Step 11 Call pricing API to reprice the contract based on new dates
5630         --and renewal pricing rules. This will also rollup price/tax values at the topline and header
5631         --level and stamp the pricelist on the lines.
5632         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5633             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.repricing', 'calling  reprice_contract, p_chr_id='||x_chr_id||' ,p_price_method='||l_rnrl_rec.renewal_pricing_type||' ,p_price_list_id='||l_rnrl_rec.price_list_id1
5634             ||' ,p_markup_percent='||l_rnrl_rec.markup_percent);
5635         END IF;
5636 
5637         reprice_contract(
5638             p_chr_id  => x_chr_id,
5639             p_price_method => l_rnrl_rec.renewal_pricing_type,
5640             p_price_list_id => l_rnrl_rec.price_list_id1,
5641             p_markup_percent => l_rnrl_rec.markup_percent,
5642             x_msg_count => x_msg_count,
5643             x_msg_data => x_msg_data,
5644             x_return_status => x_return_status);
5645 
5646         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5647             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.repricing', 'after  call to reprice_contract, x_return_status='||x_return_status);
5648         END IF;
5649         --end of repricing
5650 
5651 	    /*
5652 	        Step 12 : copy usage price locks if any
5653 	    */
5654 
5655          fnd_file.put_line(FND_FILE.LOG,'  ');
5656          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5657          fnd_file.put_line(FND_FILE.LOG,'Step 12 : copy usage price locks if any');
5658          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5659          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5660          fnd_file.put_line(FND_FILE.LOG,'  ');
5661 
5662         --Step 12 copy usage price locks if any
5663         --Can be done only after the line pricelist has been updated (in reprice_contract)
5664         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5665             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.usage_price_locks', 'calling  copy_usage_price_locks, p_chr_id='||x_chr_id||' ,p_contract_number='||l_renk_num);
5666         END IF;
5667         copy_usage_price_locks(
5668             p_chr_id  => x_chr_id,
5669             p_org_id => l_renk_org_id,
5670             p_contract_number => l_renk_num,
5671             x_msg_count => x_msg_count,
5672             x_msg_data => x_msg_data,
5673             x_return_status => x_return_status);
5674 
5675         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5676             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.usage_price_locks', 'after  call to copy_usage_price_locks, x_return_status='||x_return_status);
5677         END IF;
5678         --end of copy usage price locks
5679 
5680 
5681 	    /*
5682 	        Step 13: adjust the header and line dates
5683 	    */
5684 
5685          fnd_file.put_line(FND_FILE.LOG,'  ');
5686          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5687          fnd_file.put_line(FND_FILE.LOG,'Step 13 : Recreate billing schedules for the lines/header');
5688          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5689          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5690          fnd_file.put_line(FND_FILE.LOG,'  ');
5691 
5692         --Step 13 Recreate billing schedules for the lines/header.
5693         --Can be done only after dates adjustment and repricing the contract
5694         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5695             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.billing', 'calling  recreate_billing, p_chr_id='||x_chr_id);
5696         END IF;
5697         recreate_billing(
5698             p_chr_id  => x_chr_id,
5699             p_billing_profile_id => l_rnrl_rec.billing_profile_id,
5700             x_msg_count => x_msg_count,
5701             x_msg_data => x_msg_data,
5702             x_return_status => x_return_status);
5703         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5704             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.billing', 'after  call to recreate_billing, x_return_status='||x_return_status);
5705         END IF;
5706         --end of billing
5707 
5708 	    /*
5709 	        Step 14 : Process Sales credits
5710 	    */
5711 
5712          fnd_file.put_line(FND_FILE.LOG,'  ');
5713          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5714          fnd_file.put_line(FND_FILE.LOG,'Step 14 : Process Sales credits');
5715          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5716          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5717          fnd_file.put_line(FND_FILE.LOG,'  ');
5718 
5719         --Step 14 Process Sales credits
5720         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5721             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.sales_credits', 'calling  process_sales_credit, p_chr_id='||x_chr_id);
5722         END IF;
5723 
5724         fnd_file.put_line(FND_FILE.LOG,'Calling process_sales_credit ');
5725 
5726         process_sales_credit(
5727             p_chr_id  => x_chr_id,
5728             x_msg_count => x_msg_count,
5729             x_msg_data => x_msg_data,
5730             x_return_status => x_return_status);
5731 
5732         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5733             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.sales_credits', 'after  call to process_sales_credit, x_return_status='||x_return_status);
5734         END IF;
5735 
5736         --sales credit and copy are the 2 places we can return with a warning
5737         IF x_return_status = OKC_API.g_ret_sts_warning THEN -- 'W'
5738             l_warnings := TRUE;
5739         END IF;
5740         --end of sales credits
5741 
5742 	    /*
5743 	        Step 15 : get the user id and name (salesperson) of the contact
5744 	    */
5745 
5746          fnd_file.put_line(FND_FILE.LOG,'  ');
5747          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5748          fnd_file.put_line(FND_FILE.LOG,'Step 15 : get the user id and name (salesperson) of the contact');
5749          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5750          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5751          fnd_file.put_line(FND_FILE.LOG,'  ');
5752 
5753         --Step 15 get the user id and name (salesperson) of the contact who will be
5754         --the performer for the workflow. Can be done only after sales credits
5755         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5756             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_user_name', 'calling  get_user_name, p_chr_id='||x_chr_id||' ,p_hdesk_user_id='||l_rnrl_rec.user_id);
5757         END IF;
5758 
5759         get_user_name(
5760             p_api_version =>  1,
5761             p_init_msg_list => FND_API.G_FALSE,
5762             x_return_status => x_return_status,
5763             x_msg_count => x_msg_count,
5764             x_msg_data => x_msg_data,
5765             p_chr_id => x_chr_id,
5766             p_hdesk_user_id => l_rnrl_rec.user_id,
5767             x_user_id => l_user_id,
5768             x_user_name => l_user_name);
5769 
5770         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5771             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_user_name', 'after call to  get_user_name, x_return_status='||x_return_status||' ,l_user_id='||l_user_id||' ,l_user_name='||l_user_name);
5772         END IF;
5773         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5774             RAISE FND_API.g_exc_unexpected_error;
5775         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5776             RAISE FND_API.g_exc_error;
5777         END IF;
5778         --end of get user name
5779 
5780 	    /*
5781 	        Step 16: check and assign contract to contract group specified in GCD
5782 	    */
5783 
5784          fnd_file.put_line(FND_FILE.LOG,'  ');
5785          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5786          fnd_file.put_line(FND_FILE.LOG,'Step 16 : check and assign contract to contract group specified in GCD');
5787          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5788          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5789          fnd_file.put_line(FND_FILE.LOG,'  ');
5790 
5791         --Step 16 check and assign contract to contract group specified in GCD
5792         IF( l_rnrl_rec.cgp_renew_id IS NOT NULL) THEN
5793             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5794                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.contract_group', 'calling assign_contract_group p_chr_id='||p_chr_id||' ,p_chr_group_id='||l_rnrl_rec.cgp_renew_id);
5795             END IF;
5796             assign_contract_group(
5797                 p_chr_id  => x_chr_id,
5798                 p_chr_group_id => l_rnrl_rec.cgp_renew_id,
5799                 x_msg_count => x_msg_count,
5800                 x_msg_data => x_msg_data,
5801                 x_return_status => x_return_status);
5802             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5803                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.contract_group', 'after  call to assign_contract_group, x_return_status='||x_return_status);
5804             END IF;
5805         END IF;
5806         --end of contract group
5807 	    /*
5808 	        Step 17: check and update/create contract approval process specified in GCD
5809 	    */
5810 
5811          fnd_file.put_line(FND_FILE.LOG,'  ');
5812          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5813          fnd_file.put_line(FND_FILE.LOG,'Step 17 : check and update/create contract approval process specified in GCD');
5814          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5815          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5816          fnd_file.put_line(FND_FILE.LOG,'  ');
5817 
5818         --Step 17 check and update/create contract approval process specified in GCD
5819         IF( l_rnrl_rec.pdf_id IS NOT NULL) THEN
5820             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5821                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.contract_process', 'calling assign_contract_process p_chr_id='||p_chr_id||' ,p_pdf_id='||l_rnrl_rec.pdf_id);
5822             END IF;
5823             assign_contract_process(
5824                 p_chr_id  => x_chr_id,
5825                 p_pdf_id => l_rnrl_rec.pdf_id,
5826                 x_msg_count => x_msg_count,
5827                 x_msg_data => x_msg_data,
5828                 x_return_status => x_return_status);
5829             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5830                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.contract_process', 'after  call to assign_contract_process, x_return_status='||x_return_status);
5831             END IF;
5832         END IF;
5833         --end of contract approval process
5834 
5835 	    /*
5836 	        Step 18: update contract (OKC and OKS) with the renewal rules
5837 	    */
5838 
5839          fnd_file.put_line(FND_FILE.LOG,'  ');
5840          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5841          fnd_file.put_line(FND_FILE.LOG,'Step 18 : update contract (OKC and OKS) with the renewal rules');
5842          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5843          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5844          fnd_file.put_line(FND_FILE.LOG,'  ');
5845 
5846         --Step 18 update contract (OKC and OKS) with the renewal rules, inlcuding determination
5847         --renewal type and launching/modification of workflow
5848         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5849             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_contract', 'calling update_renewed_contract p_chr_id='||p_chr_id||' ,p_notify_to='||l_user_id);
5850         END IF;
5851         update_renewed_contract(
5852             p_chr_id  => x_chr_id,
5853             p_rnrl_rec => l_rnrl_rec,
5854             p_notify_to => l_user_id,
5855             x_msg_count => x_msg_count,
5856             x_msg_data => x_msg_data,
5857             x_return_status => x_return_status);
5858         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5859             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_contract', 'after  call to update_renewed_contract, x_return_status='||x_return_status);
5860         END IF;
5861         --end of update contract
5862 
5863         IF (l_warnings) THEN
5864            x_return_status :=  OKC_API.G_RET_STS_WARNING;
5865         END IF;
5866 
5867         --standard check of p_commit
5868 	    IF FND_API.to_boolean( p_commit ) THEN
5869 		    COMMIT;
5870 	    END IF;
5871 
5872          fnd_file.put_line(FND_FILE.LOG,'  ');
5873          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5874          fnd_file.put_line(FND_FILE.LOG,'Completed Calling OKS_RENEW_CONTRACT_PVT.RENEW_CONTRACT');
5875          fnd_file.put_line(FND_FILE.LOG,'End Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5876          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5877          fnd_file.put_line(FND_FILE.LOG,'  ');
5878 
5879         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
5880             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
5881                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_chr_id=' || x_chr_id ||', x_return_status='|| x_return_status);
5882             END IF;
5883         END IF;
5884         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5885 
5886     EXCEPTION
5887         WHEN FND_API.g_exc_error THEN
5888             ROLLBACK TO renew_contract_PVT;
5889             x_return_status := FND_API.g_ret_sts_error ;
5890 
5891             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5892                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
5893             END IF;
5894             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5895             IF (c_k_hdr%isopen) THEN
5896                 CLOSE c_k_hdr;
5897             END IF;
5898             IF (c_renk_hdr%isopen) THEN
5899                 CLOSE c_renk_hdr;
5900             END IF;
5901 
5902         WHEN FND_API.g_exc_unexpected_error THEN
5903             ROLLBACK TO renew_contract_PVT;
5904             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5905 
5906             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
5907                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
5908             END IF;
5909             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5910             IF (c_k_hdr%isopen) THEN
5911                 CLOSE c_k_hdr;
5912             END IF;
5913             IF (c_renk_hdr%isopen) THEN
5914                 CLOSE c_renk_hdr;
5915             END IF;
5916 
5917         WHEN OTHERS THEN
5918             ROLLBACK TO renew_contract_PVT;
5919             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5920 
5921             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
5922                 --first log the sqlerrm
5923                 l_error_text := substr (SQLERRM, 1, 240);
5924                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
5925                 --then add it to the message api list
5926                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
5927             END IF;
5928             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5929             IF (c_k_hdr%isopen) THEN
5930                 CLOSE c_k_hdr;
5931             END IF;
5932             IF (c_renk_hdr%isopen) THEN
5933                 CLOSE c_renk_hdr;
5934             END IF;
5935 
5936     END RENEW_CONTRACT;
5937 
5938 
5939 END OKS_RENEW_CONTRACT_PVT;