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.24 2007/12/07 08:18:15 cgopinee 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 
1405     BEGIN
1406 
1407         --log key input parameters
1408         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1409             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
1410         END IF;
1411 
1412         x_return_status := FND_API.G_RET_STS_SUCCESS;
1413 
1414 	    --can have four values, DRT:Derive for Revenue Type and Retain Other
1415         --YES:Derive,  NO:Drop,  R:Retain, defaults to R
1416         l_prof_enable_sc := nvl(FND_PROFILE.value('OKS_ENABLE_SALES_CREDIT'), 'R');
1417 
1418         --lookup for revenue type : Select name , id1 from OKX_SALES_CRED_TYPES_V order by NAME;
1419         --1:Quota Sales Credit, 2:Non-quota Sales Credit
1420         l_prof_rev_type := FND_PROFILE.VALUE('OKS_REVENUE_TYPE');
1421 
1422         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1423             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);
1424         END IF;
1425 
1426         IF (l_prof_enable_sc = 'R') THEN
1427             --for R:Retain, do nothing
1428             IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1429                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end_1', 'Profile OKS_ENABLE_SALES_CREDIT=R(Retain), no processing required');
1430             END IF;
1431             RETURN;
1432         ELSIF (l_prof_enable_sc = 'NO') THEN
1433             --for NO:Drop, delete all existing sales credits and return
1434             DELETE FROM oks_k_sales_credits
1435                 WHERE chr_id = p_chr_id;
1436             IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1437                 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');
1438             END IF;
1439             RETURN;
1440         ELSIF(l_prof_enable_sc = 'YES') THEN
1441             --for YES:Derive, delete all existing sales credits and derive specified type of sales credit
1442             IF (l_prof_rev_type IS NULL) THEN
1443                 --without the profile setup we cannot recreate the sales credit
1444                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1445                 FND_MESSAGE.set_token('PROFILE', 'OKS_REVENUE_TYPE');
1446                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1447                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.rev_type_chk', FALSE);
1448                 END IF;
1449                 FND_MSG_PUB.add;
1450                 RAISE FND_API.g_exc_error;
1451             ELSE
1452                 DELETE FROM oks_k_sales_credits
1453                     WHERE chr_id = p_chr_id;
1454             END IF;
1455         ELSIF (l_prof_enable_sc = 'DRT') THEN
1456             --for DRT:Derive for Revenue Type and Retain Other, delete and derive specified type of sales credit
1457             IF (l_prof_rev_type IS NULL) THEN
1458                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1459                 FND_MESSAGE.set_token('PROFILE', 'OKS_REVENUE_TYPE');
1460                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1461                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.rev_type_chk', FALSE);
1462                 END IF;
1463                 FND_MSG_PUB.add;
1464                 RAISE FND_API.g_exc_error;
1465             ELSE
1466                 DELETE FROM oks_k_sales_credits
1467                     WHERE chr_id = p_chr_id AND sales_credit_type_id1 = l_prof_rev_type;
1468             END IF;
1469         END IF; --of ELSIF (l_prof_enable_sc = 'DRT') THEN
1470 
1471         --we come here only if l_prof_enable_sc = 'YES' or 'DRT'
1472         --derive the sales credits for the specified revenue type
1473 
1474         --get the contract org and customer/subscriber party id
1475         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1476             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);
1477         END IF;
1478 
1479         OPEN c_k_hdr(p_chr_id);
1480         FETCH c_k_hdr INTO l_org_id, l_party_id;
1481         CLOSE c_k_hdr;
1482         IF (l_org_id IS NULL) THEN
1483             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
1484             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
1485             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1486                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_k_org_party', FALSE);
1487             END IF;
1488             FND_MSG_PUB.ADD;
1489             RAISE FND_API.g_exc_error;
1490         END IF;
1491 
1492         --get the winning salesrep either from JTF or profile option
1493         l_prof_use_jtf := FND_PROFILE.VALUE('OKS_USE_JTF');
1494 
1495         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1496             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);
1497         END IF;
1498 
1499         IF (l_prof_use_jtf = 'YES') THEN
1500             --get the salesrep from JTF Territory setup
1501             --note this procedure will throw an error if no salesrep is setup in JTF
1502             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1503                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_salesrep', 'calling get_salesrep_from_jtf');
1504             END IF;
1505 
1506             fnd_file.put_line(FND_FILE.LOG,'Calling get_salesrep_from_jtf');
1507             get_salesrep_from_jtf(
1508                 p_org_id => l_org_id,
1509                 p_party_id => l_party_id,
1510                 x_winning_res_id => l_resource_id,
1511                 x_return_status => x_return_status,
1512                 x_msg_count => x_msg_count,
1513                 x_msg_data => x_msg_data);
1514 
1515             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1516                 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);
1517             END IF;
1518 
1519             --get the salesrep id corresponding to this resource and k org
1520             OPEN c_res_salesrep(l_resource_id, l_org_id);
1521             FETCH c_res_salesrep INTO l_salesrep_id;
1522             CLOSE c_res_salesrep;
1523 
1524             IF l_salesrep_id IS NULL THEN
1525                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_SREP_FOR_RES');
1526                 FND_MESSAGE.set_token('RESOURCE_ID', l_resource_id);
1527                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1528                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_jtf_salesrep', FALSE);
1529                 END IF;
1530                 FND_MSG_PUB.ADD;
1531                 RAISE FND_API.g_exc_error;
1532             END IF;
1533 
1534         ELSE
1535             --get the salesrep from profile option
1536             l_salesrep_id := FND_PROFILE.value('OKS_SALESPERSON_ID');
1537 
1538             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1539                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_salesrep', 'salesrep from profile option OKS_SALESPERSON_ID='||l_salesrep_id);
1540             END IF;
1541 
1542             IF l_salesrep_id IS NULL THEN
1543                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1544                 FND_MESSAGE.set_token('PROFILE', 'OKS_SALESPERSON_ID');
1545                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1546                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_prof_salesrep', FALSE);
1547                 END IF;
1548                 FND_MSG_PUB.add;
1549                 RAISE FND_API.g_exc_error;
1550             END IF;
1551 
1552         END IF; --of IF (l_prof_use_jtf = 'YES') THEN
1553 
1554         --now check if the salesrep belongs to the same org as the contract
1555         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1556             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);
1557         END IF;
1558 
1559         OPEN c_check_org_match(l_salesrep_id, l_org_id);/*bugfix 6672863*/
1560         FETCH c_check_org_match INTO l_dummy_org_id, l_salesrep_name;
1561         CLOSE c_check_org_match;
1562 
1563         IF (nvl(l_dummy_org_id,-99) <> l_org_id) THEN
1564             --as per bug # 2968069, if salesrep does not belong to the same org as the contract
1565             --we proceed without creating sales credit or adding the salerep to the contract
1566             --Note this can only happen for FND_PROFILE.VALUE('OKS_USE_JTF') = NO
1567             OPEN c_get_org_name(l_org_id);
1568             FETCH c_get_org_name INTO l_org_name;
1569             CLOSE c_get_org_name;
1570 
1571             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_SALESREP_ORG_MATCH');
1572             FND_MESSAGE.set_token('SALESREP_NAME', l_salesrep_name);
1573             FND_MESSAGE.set_token('ORG_NAME', l_org_name);
1574             IF (FND_LOG.level_event >= FND_LOG.g_current_runtime_level) THEN
1575                 FND_LOG.message(FND_LOG.level_event, l_mod_name || '.org_id_match', FALSE);
1576             END IF;
1577             FND_MSG_PUB.add;
1578             x_return_status := OKC_API.g_ret_sts_warning;
1579             RETURN;
1580         END IF;
1581 
1582         --get the party role id, rle_code for vendor/merchant
1583         OPEN c_get_ven_mer_id(p_chr_id);
1584         FETCH c_get_ven_mer_id INTO l_cpl_id, l_rle_code;
1585         CLOSE c_get_ven_mer_id;
1586 
1587         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1588             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);
1589         END IF;
1590 
1591         IF (l_cpl_id IS NULL) THEN
1592             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_VENDOR_MERCHANT');
1593             FND_MESSAGE.set_token('CONTRACT_ID', to_char(p_chr_id));
1594             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1595                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_ven_mer_id', FALSE);
1596             END IF;
1597             FND_MSG_PUB.add;
1598             RAISE FND_API.g_exc_error;
1599         END IF;
1600 
1601         --get the first cro_code from vendor/merchant contact sources that are based on the
1602         --jtf object OKX_SALEPERS. There can be many contact sources based on OKX_SALEPERS, we
1603         --will just choose the first one
1604         OPEN c_get_cro_code(l_rle_code);
1605         FETCH c_get_cro_code INTO l_cro_code;
1606         CLOSE c_get_cro_code;
1607 
1608         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1609             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);
1610         END IF;
1611 
1612         IF (l_cro_code IS NULL) THEN
1613             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_K_SRC_FOR_SREP');
1614             FND_MESSAGE.set_token('RLE_CODE', l_rle_code);
1615             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1616                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_cro_code', FALSE);
1617             END IF;
1618             FND_MSG_PUB.add;
1619             RAISE FND_API.g_exc_error;
1620         END IF;
1621 
1622         --get the sales group id for the salesrep
1623         --function returns -1 for errors
1624         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1625             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);
1626         END IF;
1627 
1628         l_sales_group_id := JTF_RS_INTEGRATION_PUB.get_default_sales_group(
1629                                 p_salesrep_id => l_salesrep_id,
1630                                 p_org_id => l_org_id,
1631                                 p_date => sysdate);
1632 
1633         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1634             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);
1635         END IF;
1636 
1637         --just log the fact that no salesgroup was found, no error thrown
1638         IF (l_sales_group_id = -1) THEN
1639             IF (FND_LOG.level_event >= FND_LOG.g_current_runtime_level) THEN
1640                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_SALES_GROUP');
1641                 FND_MESSAGE.set_token('SALESREP_ID', to_char(l_salesrep_id));
1642                 FND_LOG.message(FND_LOG.level_event, l_mod_name || '.get_sales_group', TRUE);
1643             END IF;
1644         END IF;
1645 
1646         --delete any old vendor/merchant contacts based on jtf object OKX_SALEPERS
1647         DELETE FROM okc_contacts
1648             WHERE dnz_chr_id = p_chr_id
1649             AND cpl_id = l_cpl_id
1650             AND cro_code IN (SELECT cro_code FROM okc_contact_sources
1651                                 WHERE buy_or_sell = 'S' AND rle_code = l_rle_code
1652                                 AND jtot_object_code = 'OKX_SALEPERS');
1653 
1654         --add this salesrep as a contact for vendor/merchant with the cro_code found above
1655         l_created_by := FND_GLOBAL.USER_ID;
1656         l_date := sysdate;
1657         l_login_id := FND_GLOBAL.LOGIN_ID;
1658 
1659         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1660             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_contact', 'deleted old contacts, creating new contact for salesrep id='||l_salesrep_id);
1661         END IF;
1662 
1663         INSERT INTO OKC_CONTACTS(
1664             id,
1665             cpl_id,
1666             cro_code,
1667             dnz_chr_id,
1668             object1_id1,
1669             object1_id2,
1670             jtot_object1_code,
1671             object_version_number,
1672             created_by,
1673             creation_date,
1674             last_updated_by,
1675             last_update_date,
1676             last_update_login,
1677             sales_group_id)
1678         VALUES(
1679             okc_p_util.raw_to_number(sys_guid()),
1680             l_cpl_id,
1681             l_cro_code,
1682             p_chr_id,
1683             l_salesrep_id,
1684             '#',
1685             'OKX_SALEPERS',
1686             1,
1687             l_created_by,
1688             l_date,
1689             l_created_by,
1690             l_date,
1691             l_login_id,
1692             l_sales_group_id);
1693 
1694         --create sales credit for this salesperson
1695         l_prof_rev_type_dist := nvl(FND_PROFILE.VALUE('OKS_REVENUE_TYPE_DIST'), '0');
1696         l_percent := to_number(l_prof_rev_type_dist);
1697 
1698         IF (l_percent < 0 OR l_percent > 100) THEN
1699             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVD_PROFILE_VALUE');
1700             FND_MESSAGE.set_token('PROFILE', 'OKS_REVENUE_TYPE_DIST');
1701             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1702                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_rev_type_dist', FALSE);
1703             END IF;
1704             FND_MSG_PUB.add;
1705             RAISE FND_API.g_exc_error;
1706         END IF;
1707 
1708         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1709             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);
1710         END IF;
1711 
1712         INSERT INTO oks_k_sales_credits(
1713             id,
1714             percent,
1715             chr_id,
1716             cle_id,
1717             ctc_id,
1718             sales_credit_type_id1,
1719             sales_credit_type_id2,
1720             object_version_number,
1721             created_by,
1722             creation_date,
1723             last_updated_by,
1724             last_update_date,
1725             security_group_id,
1726             sales_group_id)
1727         VALUES (
1728             okc_p_util.raw_to_number(sys_guid()),
1729             l_percent,
1730             p_chr_id,
1731             null,
1732             l_salesrep_id,
1733             l_prof_rev_type,
1734             '#',
1735             1,
1736             l_created_by,
1737             l_date,
1738             l_created_by,
1739             l_date,
1740             null,
1741             l_sales_group_id);
1742 
1743         OPEN c_get_top_lines(p_chr_id);
1744         LOOP
1745             FETCH c_get_top_lines BULK COLLECT INTO l_id_tbl LIMIT G_BULK_FETCH_LIMIT;
1746 
1747             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1748                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.line_sales_credit', 'l_id_tbl.count='||l_id_tbl.count);
1749             END IF;
1750 
1751             EXIT WHEN (l_id_tbl.count = 0);
1752             FORALL i in l_id_tbl.first..l_id_tbl.last
1753                 INSERT INTO oks_k_sales_credits(
1754                     id,
1755                     percent,
1756                     chr_id,
1757                     cle_id,
1758                     ctc_id,
1759                     sales_credit_type_id1,
1760                     sales_credit_type_id2,
1761                     object_version_number,
1762                     created_by,
1763                     creation_date,
1764                     last_updated_by,
1765                     last_update_date,
1766                     security_group_id,
1767                     sales_group_id)
1768                 VALUES (
1769                     okc_p_util.raw_to_number(sys_guid()),
1770                     l_percent,
1771                     p_chr_id,
1772                     l_id_tbl(i),
1773                     l_salesrep_id,
1774                     l_prof_rev_type,
1775                     '#',
1776                     1,
1777                     l_created_by,
1778                     l_date,
1779                     l_created_by,
1780                     l_date,
1781                     null,
1782                     l_sales_group_id);
1783 
1784         END LOOP;
1785         CLOSE c_get_top_lines;
1786         l_id_tbl.delete;
1787 
1788         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1789             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
1790         END IF;
1791 
1792     EXCEPTION
1793         WHEN FND_API.g_exc_error THEN
1794             x_return_status := FND_API.g_ret_sts_error;
1795             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1796                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
1797             END IF;
1798             IF ( c_res_salesrep%isopen ) THEN
1799                 CLOSE c_res_salesrep;
1800             END IF;
1801             IF ( c_check_org_match%isopen ) THEN
1802                 CLOSE c_check_org_match;
1803             END IF;
1804             IF ( c_get_org_name%isopen ) THEN
1805                 CLOSE c_get_org_name;
1806             END IF;
1807             IF ( c_get_ven_mer_id%isopen ) THEN
1808                 CLOSE c_get_ven_mer_id;
1809             END IF;
1810             IF ( c_get_cro_code%isopen ) THEN
1811                 CLOSE c_get_cro_code;
1812             END IF;
1813             IF ( c_get_top_lines%isopen ) THEN
1814                 CLOSE c_get_top_lines;
1815             END IF;
1816             RAISE;
1817 
1818         WHEN FND_API.g_exc_unexpected_error THEN
1819             x_return_status := FND_API.g_ret_sts_unexp_error ;
1820             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1821                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
1822             END IF;
1823             IF ( c_res_salesrep%isopen ) THEN
1824                 CLOSE c_res_salesrep;
1825             END IF;
1826             IF ( c_check_org_match%isopen ) THEN
1827                 CLOSE c_check_org_match;
1828             END IF;
1829             IF ( c_get_org_name%isopen ) THEN
1830                 CLOSE c_get_org_name;
1831             END IF;
1832             IF ( c_get_ven_mer_id%isopen ) THEN
1833                 CLOSE c_get_ven_mer_id;
1834             END IF;
1835             IF ( c_get_cro_code%isopen ) THEN
1836                 CLOSE c_get_cro_code;
1837             END IF;
1838             IF ( c_get_top_lines%isopen ) THEN
1839                 CLOSE c_get_top_lines;
1840             END IF;
1841             RAISE;
1842 
1843         WHEN OTHERS THEN
1844             x_return_status := FND_API.g_ret_sts_unexp_error ;
1845             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
1846                 --first log the sqlerrm
1847                 l_error_text := substr (SQLERRM, 1, 240);
1848                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
1849                 --then add it to the message api list
1850                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
1851             END IF;
1852             IF ( c_res_salesrep%isopen ) THEN
1853                 CLOSE c_res_salesrep;
1854             END IF;
1855             IF ( c_check_org_match%isopen ) THEN
1856                 CLOSE c_check_org_match;
1857             END IF;
1858             IF ( c_get_org_name%isopen ) THEN
1859                 CLOSE c_get_org_name;
1860             END IF;
1861             IF ( c_get_ven_mer_id%isopen ) THEN
1862                 CLOSE c_get_ven_mer_id;
1863             END IF;
1864             IF ( c_get_cro_code%isopen ) THEN
1865                 CLOSE c_get_cro_code;
1866             END IF;
1867             IF ( c_get_top_lines%isopen ) THEN
1868                 CLOSE c_get_top_lines;
1869             END IF;
1870             RAISE;
1871 
1872     END PROCESS_SALES_CREDIT;
1873 
1874 
1875     /*
1876     Internal procedure for recreating coverage and subscription entitities. This can be done
1877     only after the contract dates have been adjusted
1878     Parameters
1879         p_chr_id            : id of the renewed contract
1880     */
1881     PROCEDURE RECREATE_COV_SUBSCR
1882     (
1883      p_chr_id IN NUMBER,
1884      x_msg_count OUT NOCOPY NUMBER,
1885      x_msg_data OUT NOCOPY VARCHAR2,
1886      x_return_status OUT NOCOPY VARCHAR2
1887     )
1888     IS
1889     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_COV_SUBSCR';
1890     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
1891     l_error_text VARCHAR2(512);
1892 
1893     l_id_tbl                num_tbl_type;
1894     l_old_id_tbl            num_tbl_type;
1895     l_lse_id_tbl            num_tbl_type;
1896 
1897     CURSOR c_subscr_service_lines(cp_chr_id IN NUMBER) IS
1898         SELECT id, nvl(orig_system_id1, cle_id_renewed) old_id, lse_id
1899         FROM okc_k_lines_b
1900         WHERE dnz_chr_id = cp_chr_id AND cle_id IS NULL
1901         AND lse_id IN (1,19,46);
1902 
1903     BEGIN
1904 
1905         --log key input parameters
1906         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1907             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
1908         END IF;
1909 
1910         x_return_status := FND_API.G_RET_STS_SUCCESS;
1911 
1912         OPEN c_subscr_service_lines(p_chr_id);
1913         LOOP
1914             FETCH c_subscr_service_lines BULK COLLECT INTO l_id_tbl, l_old_id_tbl, l_lse_id_tbl LIMIT G_BULK_FETCH_LIMIT;
1915 
1916             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1917                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.subcr_service_loop', 'l_id_tbl.count='||l_id_tbl.count);
1918             END IF;
1919             EXIT WHEN (l_id_tbl.count = 0);
1920 
1921             FOR i IN l_id_tbl.first..l_id_tbl.last LOOP
1922                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1923                     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));
1924                 END IF;
1925 
1926                 --recreate coverage entities
1927                 IF( l_lse_id_tbl(i) IN (1,19) ) THEN
1928 
1929                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1930                         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));
1931                     END IF;
1932 
1933                     OKS_COVERAGES_PVT.copy_coverage(
1934                         p_api_version => 1.0,
1935                         p_init_msg_list => FND_API.G_FALSE,
1936                         x_return_status => x_return_status,
1937                         x_msg_count => x_msg_count,
1938                         x_msg_data => x_msg_data,
1939                         p_contract_line_id => l_id_tbl(i));
1940 
1941                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1942                         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);
1943                     END IF;
1944 
1945                     IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
1946                         RAISE FND_API.g_exc_unexpected_error;
1947                     ELSIF x_return_status = FND_API.g_ret_sts_error THEN
1948                         RAISE FND_API.g_exc_error;
1949                     END IF;
1950 
1951                 ELSIF (l_lse_id_tbl(i) = 46) THEN
1952 
1953                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1954                         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');
1955                     END IF;
1956 
1957                     OKS_SUBSCRIPTION_PUB.copy_subscription(
1958                         p_api_version => 1.0,
1959                         p_init_msg_list => FND_API.G_FALSE,
1960                         x_return_status => x_return_status,
1961                         x_msg_count => x_msg_count,
1962                         x_msg_data => x_msg_data,
1963                         p_source_cle_id => l_old_id_tbl(i),
1964                         p_target_cle_id => l_id_tbl(i),
1965                         p_intent => 'RENEW');
1966 
1967                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
1968                         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);
1969                     END IF;
1970 
1971                     IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
1972                         RAISE FND_API.g_exc_unexpected_error;
1973                     ELSIF x_return_status = FND_API.g_ret_sts_error THEN
1974                         RAISE FND_API.g_exc_error;
1975                     END IF;
1976                 END IF; --of elsif ELSIF (l_lse_id_tbl(i) = 46) THEN
1977             END LOOP; --of FOR i IN l_id_tbl.first..l_id_tbl.last LOOP
1978 
1979         END LOOP; --of top line bulk fetch loop
1980         CLOSE c_subscr_service_lines;
1981         l_id_tbl.delete;
1982         l_old_id_tbl.delete;
1983         l_lse_id_tbl.delete;
1984 
1985         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
1986             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
1987         END IF;
1988 
1989     EXCEPTION
1990         WHEN FND_API.g_exc_error THEN
1991             x_return_status := FND_API.g_ret_sts_error;
1992             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
1993                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
1994             END IF;
1995             IF ( c_subscr_service_lines%isopen ) THEN
1996                 CLOSE c_subscr_service_lines;
1997             END IF;
1998             RAISE;
1999 
2000         WHEN FND_API.g_exc_unexpected_error THEN
2001             x_return_status := FND_API.g_ret_sts_unexp_error ;
2002             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2003                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2004             END IF;
2005             IF ( c_subscr_service_lines%isopen ) THEN
2006                 CLOSE c_subscr_service_lines;
2007             END IF;
2008             RAISE;
2009 
2010         WHEN OTHERS THEN
2011             x_return_status := FND_API.g_ret_sts_unexp_error ;
2012             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2013                 --first log the sqlerrm
2014                 l_error_text := substr (SQLERRM, 1, 240);
2015                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2016                 --then add it to the message api list
2017                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2018             END IF;
2019             IF ( c_subscr_service_lines%isopen ) THEN
2020                 CLOSE c_subscr_service_lines;
2021             END IF;
2022             RAISE;
2023     END RECREATE_COV_SUBSCR;
2024 
2025     /*
2026     Internal procedure for copying the usage price locks during renewal. Note: the copy API does not
2027     copy any usage price locks during copy, only after we have obtained the renewal price list, can we
2028     copy the usage price locks.
2029     Parameters
2030         p_chr_id            : id of the renewed contract
2031         p_org_id            : org id of the renewed contract
2032         p_contract_number   : number of the renewed contract
2033 
2034     The logic of copying usage price locks is simple
2035         1. Get the old line's price list and locked price list id and the new line's price list
2036         2. If old locked price list id is not null (i.e., old line had a price lock)
2037            and new price list = old price list
2038            a. Call QP api to lock price list (with the new contract number)
2039            b. Update new oks lines with the price lock information (locked price list id
2040               and locked price list line id)
2041     */
2042     PROCEDURE COPY_USAGE_PRICE_LOCKS
2043     (
2044      p_chr_id IN NUMBER,
2045      p_org_id IN NUMBER,
2046      p_contract_number IN VARCHAR2,
2047      x_msg_count OUT NOCOPY NUMBER,
2048      x_msg_data OUT NOCOPY VARCHAR2,
2049      x_return_status OUT NOCOPY VARCHAR2
2050     )
2051     IS
2052     l_api_name CONSTANT VARCHAR2(30) := 'COPY_USAGE_PRICE_LOCKS';
2053     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2054     l_error_text VARCHAR2(512);
2055 
2056     l_old_lpll_tbl              num_tbl_type;
2057     l_new_cid_tbl               num_tbl_type;
2058 
2059     l_locked_price_list_id      NUMBER;
2060     l_locked_price_list_line_id NUMBER;
2061 
2062     l_new_lpl_tbl               num_tbl_type;
2063     l_new_lpll_tbl              num_tbl_type;
2064     l_old_break_uom_tbl         chr_tbl_type;
2065 
2066     CURSOR c_get_usage_price_locks(cp_chr_id IN NUMBER) IS
2067         SELECT olds.locked_price_list_line_id, newc.id, nvl(olds.break_uom, 'X')
2068         FROM okc_k_lines_b oldc, oks_k_lines_b olds, okc_k_lines_b newc
2069         WHERE newc.dnz_chr_id = cp_chr_id AND newc.lse_id IN (12, 13)
2070         AND oldc.id = newc.orig_system_id1 AND olds.cle_id = oldc.id
2071         AND olds.locked_price_list_id IS NOT NULL
2072         AND nvl(oldc.price_list_id, -99) = nvl(newc.price_list_id, -98);
2073 
2074     BEGIN
2075 
2076         --log key input parameters
2077         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2078             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);
2079         END IF;
2080 
2081         x_return_status := FND_API.G_RET_STS_SUCCESS;
2082 
2083         OPEN c_get_usage_price_locks(p_chr_id);
2084         LOOP
2085             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;
2086 
2087             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2088                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.usage_locks_loop', 'l_new_cid_tbl.count='||l_new_cid_tbl.count);
2089             END IF;
2090             EXIT WHEN (l_new_cid_tbl.count = 0);
2091 
2092             --lock prices for each usage line
2093             FOR i IN l_new_cid_tbl.first..l_new_cid_tbl.last LOOP
2094                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2095                     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));
2096                 END IF;
2097 
2098                 l_locked_price_list_id := null;
2099                 l_locked_price_list_line_id := null;
2100                 l_new_lpl_tbl(i) := null;
2101                 l_new_lpll_tbl(i) := null;
2102 
2103                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2104                     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);
2105                 END IF;
2106 
2107                 --no bulk price lock api
2108                 QP_LOCK_PRICELIST_GRP.lock_price(
2109                     p_source_list_line_id => l_old_lpll_tbl(i),
2110                     p_list_source_code => 'OKS',
2111                     p_orig_system_header_ref => p_contract_number,
2112                     p_org_id => p_org_id,
2113                     x_locked_price_list_id => l_locked_price_list_id,
2114                     x_locked_list_line_id => l_locked_price_list_line_id,
2115                     x_return_status => x_return_status,
2116                     x_msg_count => x_msg_count,
2117                     x_msg_data => x_msg_data);
2118 
2119                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2120                     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||
2121                     ' ,x_locked_price_list_id='||l_locked_price_list_id||' ,x_locked_list_line_id='||l_locked_price_list_line_id);
2122                 END IF;
2123 
2124                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2125                     RAISE FND_API.g_exc_unexpected_error;
2126                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2127                     RAISE FND_API.g_exc_error;
2128                 END IF;
2129 
2130                 l_new_lpl_tbl(i) := l_locked_price_list_id;
2131                 l_new_lpll_tbl(i) := l_locked_price_list_line_id;
2132                 --we select X if it was null to keep pl/sql table indexes in sync
2133                 IF (l_old_break_uom_tbl(i) = 'X') THEN
2134                     l_old_break_uom_tbl(i) := null;
2135                 END IF;
2136 
2137             END LOOP; --of FOR i IN l_new_cid_tbl.first..l_new_cid_tbl.last LOOP
2138 
2139             FORALL i IN l_new_cid_tbl.first..l_new_cid_tbl.last
2140                 UPDATE oks_k_lines_b
2141                     SET locked_price_list_id = l_new_lpl_tbl(i),
2142                         locked_price_list_line_id = l_new_lpll_tbl(i),
2143                         break_uom = l_old_break_uom_tbl(i)
2144                     WHERE cle_id = l_new_cid_tbl(i);
2145 
2146         END LOOP; --of top line bulk fetch loop
2147         CLOSE c_get_usage_price_locks;
2148         l_old_lpll_tbl.delete;
2149         l_new_cid_tbl.delete;
2150         l_new_lpl_tbl.delete;
2151         l_new_lpll_tbl.delete;
2152         l_old_break_uom_tbl.delete;
2153 
2154         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2155             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
2156         END IF;
2157 
2158     EXCEPTION
2159         WHEN FND_API.g_exc_error THEN
2160             x_return_status := FND_API.g_ret_sts_error;
2161             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2162                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2163             END IF;
2164             IF ( c_get_usage_price_locks%isopen ) THEN
2165                 CLOSE c_get_usage_price_locks;
2166             END IF;
2167             RAISE;
2168 
2169         WHEN FND_API.g_exc_unexpected_error THEN
2170             x_return_status := FND_API.g_ret_sts_unexp_error ;
2171             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2172                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2173             END IF;
2174             IF ( c_get_usage_price_locks%isopen ) THEN
2175                 CLOSE c_get_usage_price_locks;
2176             END IF;
2177             RAISE;
2178 
2179         WHEN OTHERS THEN
2180             x_return_status := FND_API.g_ret_sts_unexp_error ;
2181             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2182                 --first log the sqlerrm
2183                 l_error_text := substr (SQLERRM, 1, 240);
2184                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2185                 --then add it to the message api list
2186                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2187             END IF;
2188             IF ( c_get_usage_price_locks%isopen ) THEN
2189                 CLOSE c_get_usage_price_locks;
2190             END IF;
2191             RAISE;
2192     END COPY_USAGE_PRICE_LOCKS;
2193 
2194     /*
2195     Internal procedure for recreating header billing schedule. Called after the contract line dates
2196     have been adjusted and the contract has been repriced using the renewal pricing method.
2197     Parameters
2198         p_chr_id                : id of the renewed contract
2199         p_old_chr_id            : id of the source contract
2200     */
2201     PROCEDURE RECREATE_HDR_BILLING
2202     (
2203      p_chr_id IN NUMBER,
2204      p_old_chr_id IN NUMBER,
2205      x_msg_count OUT NOCOPY NUMBER,
2206      x_msg_data OUT NOCOPY VARCHAR2,
2207      x_return_status OUT NOCOPY VARCHAR2
2208     )
2209     IS
2210     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_HDR_BILLING';
2211     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2212     l_error_text VARCHAR2(512);
2213 
2214     CURSOR c_hdr_strlvl(cp_chr_id NUMBER) IS
2215         SELECT id, chr_id, cle_id, dnz_chr_id, sequence_no, uom_code, start_date, level_periods,
2216         uom_per_period, advance_periods, level_amount, invoice_offset_days, interface_offset_days,
2217         comments, due_arr_yn, amount, lines_detailed_yn
2218         FROM oks_stream_levels_b
2219         WHERE chr_id = cp_chr_id;
2220 
2221     TYPE hdr_strlvl_tbl IS TABLE OF c_hdr_strlvl%ROWTYPE INDEX BY BINARY_INTEGER;
2222 
2223     l_hdr_strlvl_tbl    hdr_strlvl_tbl;
2224     l_sllv_tbl          OKS_SLL_PVT.sllv_tbl_type;
2225     x_sllv_tbl          OKS_SLL_PVT.sllv_tbl_type;
2226     l_sll_ctr           NUMBER := 0;
2227 
2228     BEGIN
2229         --log key input parameters
2230         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2231             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);
2232         END IF;
2233         x_return_status := FND_API.G_RET_STS_SUCCESS;
2234 
2235         OPEN c_hdr_strlvl(p_old_chr_id);
2236         LOOP
2237             FETCH c_hdr_strlvl BULK COLLECT INTO l_hdr_strlvl_tbl LIMIT G_BULK_FETCH_LIMIT;
2238             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2239                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_hdr_sll', 'l_hdr_strlvl_tbl.count=' || l_hdr_strlvl_tbl.count);
2240             END IF;
2241             EXIT WHEN (l_hdr_strlvl_tbl.count = 0);
2242 
2243             FOR i IN l_hdr_strlvl_tbl.first..l_hdr_strlvl_tbl.last LOOP
2244                 l_sll_ctr := l_sll_ctr + 1;
2245                 l_sllv_tbl(l_sll_ctr).id := OKC_API.g_miss_num;
2246                 l_sllv_tbl(l_sll_ctr).chr_id := p_chr_id;
2247                 l_sllv_tbl(l_sll_ctr).cle_id := null;
2248                 l_sllv_tbl(l_sll_ctr).dnz_chr_id := p_chr_id;
2249                 l_sllv_tbl(l_sll_ctr).sequence_no := l_hdr_strlvl_tbl(i).sequence_no;
2250                 l_sllv_tbl(l_sll_ctr).uom_code := l_hdr_strlvl_tbl(i).uom_code;
2251                 l_sllv_tbl(l_sll_ctr).start_date := l_hdr_strlvl_tbl(i).start_date;
2252                 l_sllv_tbl(l_sll_ctr).level_periods := l_hdr_strlvl_tbl(i).level_periods;
2253                 l_sllv_tbl(l_sll_ctr).uom_per_period := l_hdr_strlvl_tbl(i).uom_per_period;
2254                 l_sllv_tbl(l_sll_ctr).advance_periods := l_hdr_strlvl_tbl(i).advance_periods;
2255                 l_sllv_tbl(l_sll_ctr).level_amount := l_hdr_strlvl_tbl(i).level_amount;
2256                 l_sllv_tbl(l_sll_ctr).invoice_offset_days := l_hdr_strlvl_tbl(i).invoice_offset_days;
2257                 l_sllv_tbl(l_sll_ctr).interface_offset_days := l_hdr_strlvl_tbl(i).interface_offset_days;
2258                 l_sllv_tbl(l_sll_ctr).comments := l_hdr_strlvl_tbl(i).comments;
2259                 l_sllv_tbl(l_sll_ctr).due_arr_yn := l_hdr_strlvl_tbl(i).due_arr_yn;
2260                 l_sllv_tbl(l_sll_ctr).amount := l_hdr_strlvl_tbl(i).amount;
2261                 l_sllv_tbl(l_sll_ctr).lines_detailed_yn := l_hdr_strlvl_tbl(i).lines_detailed_yn;
2262             END LOOP;
2263 
2264         END LOOP; --hdr bulk fetch loop
2265         CLOSE c_hdr_strlvl;
2266         l_hdr_strlvl_tbl.delete;
2267 
2268         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2269             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_hdr_sll', 'l_sllv_tbl.count=' || l_sllv_tbl.count);
2270         END IF;
2271 
2272         IF (l_sllv_tbl.count > 0) THEN
2273 
2274             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2275                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_hdr_sll', 'calling OKS_CONTRACT_SLL_PUB.create_sll');
2276             END IF;
2277 
2278             OKS_CONTRACT_SLL_PUB.create_sll (
2279                 p_api_version => 1,
2280                 p_init_msg_list => FND_API.G_FALSE,
2281                 x_return_status => x_return_status,
2282                 x_msg_count => x_msg_count,
2283                 x_msg_data => x_msg_data,
2284                 p_sllv_tbl => l_sllv_tbl,
2285                 x_sllv_tbl => x_sllv_tbl,
2286                 p_validate_yn => 'N');
2287 
2288             l_sllv_tbl.delete;
2289             x_sllv_tbl.delete;
2290 
2291             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2292                 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);
2293             END IF;
2294 
2295             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2296                 RAISE FND_API.g_exc_unexpected_error;
2297             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2298                 RAISE FND_API.g_exc_error;
2299             END IF;
2300 
2301             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2302                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_hdr_sll', 'calling OKS_BILL_SCH.create_hdr_schedule');
2303             END IF;
2304 
2305             OKS_BILL_SCH.create_hdr_schedule(
2306                 p_contract_id => p_chr_id,
2307                 x_return_status => x_return_status,
2308                 x_msg_count => x_msg_count,
2309                 x_msg_data => x_msg_data);
2310 
2311             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2312                 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);
2313             END IF;
2314 
2315             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2316                 RAISE FND_API.g_exc_unexpected_error;
2317             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2318                 RAISE FND_API.g_exc_error;
2319             END IF;
2320 
2321         END IF; --of IF (l_sllv_tbl.count > 0) THEN
2322 
2323 
2324         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2325             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
2326         END IF;
2327 
2328     EXCEPTION
2329         WHEN FND_API.g_exc_error THEN
2330             x_return_status := FND_API.g_ret_sts_error;
2331             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2332                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2333             END IF;
2334             IF (c_hdr_strlvl%isopen) THEN
2335                 CLOSE c_hdr_strlvl;
2336             END IF;
2337             RAISE;
2338 
2339         WHEN FND_API.g_exc_unexpected_error THEN
2340             x_return_status := FND_API.g_ret_sts_unexp_error ;
2341             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2342                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2343             END IF;
2344             IF (c_hdr_strlvl%isopen) THEN
2345                 CLOSE c_hdr_strlvl;
2346             END IF;
2347             RAISE;
2348 
2349         WHEN OTHERS THEN
2350             x_return_status := FND_API.g_ret_sts_unexp_error ;
2351             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2352                 --first log the sqlerrm
2353                 l_error_text := substr (SQLERRM, 1, 240);
2354                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2355                 --then add it to the message api list
2356                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2357             END IF;
2358             IF (c_hdr_strlvl%isopen) THEN
2359                 CLOSE c_hdr_strlvl;
2360             END IF;
2361             RAISE;
2362     END RECREATE_HDR_BILLING;
2363 
2364 
2365     /*
2366     Internal procedure for recreating line billing schedule. Called after the contract line dates
2367     have been adjusted and the contract has been repriced using the renewal pricing method.
2368     Parameters
2369         p_chr_id                : id of the renewed contract
2370         p_old_chr_id            : id of the source contract
2371     */
2372     PROCEDURE RECREATE_LINE_BILLING
2373     (
2374      p_chr_id IN NUMBER,
2375      x_msg_count OUT NOCOPY NUMBER,
2376      x_msg_data OUT NOCOPY VARCHAR2,
2377      x_return_status OUT NOCOPY VARCHAR2
2378     )
2379     IS
2380     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_LINE_BILLING';
2381     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2382     l_error_text VARCHAR2(512);
2383 
2384     CURSOR c_get_top_lines(cp_chr_id IN NUMBER) IS
2385         SELECT renc.id, renc.inv_rule_id, renc.orig_system_id1, rens.billing_schedule_type,
2386         nvl(renc.price_negotiated,0) new_line_amt,
2387         (nvl(oldc.price_negotiated, 0) + nvl(olds.ubt_amount, 0) +
2388          nvl(olds.credit_amount, 0) + nvl(olds.suppressed_credit, 0) ) old_line_amt
2389         FROM okc_k_lines_b renc, oks_k_lines_b rens,
2390             okc_k_lines_b oldc, oks_k_lines_b olds
2391         WHERE renc.dnz_chr_id = cp_chr_id
2392         AND renc.cle_id IS NULL AND renc.lse_id IN (1,12,19,46) AND rens.cle_id = renc.id
2393         AND oldc.id = renc.orig_system_id1
2394         AND olds.cle_id = renc.orig_system_id1;
2395 
2396     TYPE top_line_tbl IS TABLE OF c_get_top_lines%ROWTYPE INDEX BY BINARY_INTEGER;
2397 
2398     CURSOR c_line_strlvl(cp_cle_id NUMBER) IS
2399         SELECT id, chr_id, cle_id, dnz_chr_id, sequence_no, uom_code, start_date, end_date,
2400         level_periods, uom_per_period, advance_periods, level_amount, invoice_offset_days,
2401         interface_offset_days, comments, due_arr_yn, amount, lines_detailed_yn
2402         FROM oks_stream_levels_b
2403         WHERE cle_id = cp_cle_id;
2404 
2405     TYPE line_strlvl_tbl IS TABLE OF c_line_strlvl%ROWTYPE INDEX BY BINARY_INTEGER;
2406 
2407     l_top_line_tbl      top_line_tbl;
2408     l_line_strlvl_tbl   line_strlvl_tbl;
2409 
2410     l_line_sllv_tbl     OKS_BILL_SCH.streamlvl_tbl;
2411     l_bil_sch_out_tbl   OKS_BILL_SCH.itembillsch_tbl;
2412     l_line_sll_ctr      NUMBER := 0;
2413 
2414     BEGIN
2415         --log key input parameters
2416         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2417             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
2418         END IF;
2419         x_return_status := FND_API.G_RET_STS_SUCCESS;
2420 
2421         OPEN c_get_top_lines(p_chr_id);
2422         LOOP
2423             FETCH  c_get_top_lines BULK COLLECT INTO l_top_line_tbl LIMIT G_BULK_FETCH_LIMIT;
2424 
2425             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2426                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_toplines', 'l_top_line_tbl.count=' || l_top_line_tbl.count);
2427             END IF;
2428 
2429             EXIT WHEN (l_top_line_tbl.count = 0);
2430 
2431             --for each topline and it's sublines recreate the billing schedule
2432             FOR i IN l_top_line_tbl.first..l_top_line_tbl.last LOOP
2433 
2434                  IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2435                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.topline_billing', 'i='||i||
2436                     ' id='||l_top_line_tbl(i).id||
2437                     ' ,billing_schedule_type='||l_top_line_tbl(i).billing_schedule_type||
2438                     ' ,inv_rule_id='||l_top_line_tbl(i).inv_rule_id||
2439                     ',new_line_amt='||l_top_line_tbl(i).new_line_amt||
2440                     ',old_line_amt='||l_top_line_tbl(i).old_line_amt);
2441                 END IF;
2442 
2443                 --initialize before every top line
2444                 l_line_sll_ctr := 0;
2445                 l_line_sllv_tbl.delete;
2446                 l_line_strlvl_tbl.delete;
2447 
2448                 --get the old billing schedule rule for the topline
2449                 OPEN c_line_strlvl(l_top_line_tbl(i).orig_system_id1);
2450                 LOOP
2451                     FETCH c_line_strlvl BULK COLLECT INTO l_line_strlvl_tbl LIMIT G_BULK_FETCH_LIMIT;
2452 
2453                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2454                         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);
2455                     END IF;
2456 
2457                     EXIT WHEN (l_line_strlvl_tbl.count = 0);
2458 
2459 
2460                     FOR j IN l_line_strlvl_tbl.first..l_line_strlvl_tbl.last LOOP
2461                         l_line_sll_ctr := l_line_sll_ctr + 1;
2462 
2463                         l_line_sllv_tbl(l_line_sll_ctr).id := FND_API.g_miss_num;
2464                         l_line_sllv_tbl(l_line_sll_ctr).chr_id := FND_API.g_miss_num;
2465                         l_line_sllv_tbl(l_line_sll_ctr).cle_id := l_top_line_tbl(i).id;
2466                         l_line_sllv_tbl(l_line_sll_ctr).dnz_chr_id := p_chr_id;
2467                         l_line_sllv_tbl(l_line_sll_ctr).sequence_no := l_line_strlvl_tbl(j).sequence_no;
2468                         l_line_sllv_tbl(l_line_sll_ctr).uom_code := l_line_strlvl_tbl(j).uom_code;
2469                         l_line_sllv_tbl(l_line_sll_ctr).start_date := l_line_strlvl_tbl(j).start_date;
2470                         l_line_sllv_tbl(l_line_sll_ctr).end_date := l_line_strlvl_tbl(j).end_date;
2471                         l_line_sllv_tbl(l_line_sll_ctr).level_periods := l_line_strlvl_tbl(j).level_periods;
2472                         l_line_sllv_tbl(l_line_sll_ctr).uom_per_period := l_line_strlvl_tbl(j).uom_per_period;
2473                         l_line_sllv_tbl(l_line_sll_ctr).advance_periods := l_line_strlvl_tbl(j).advance_periods;
2474                         l_line_sllv_tbl(l_line_sll_ctr).level_amount := l_line_strlvl_tbl(j).level_amount;
2475                         l_line_sllv_tbl(l_line_sll_ctr).invoice_offset_days := l_line_strlvl_tbl(j).invoice_offset_days;
2476                         l_line_sllv_tbl(l_line_sll_ctr).interface_offset_days := l_line_strlvl_tbl(j).interface_offset_days;
2477                         l_line_sllv_tbl(l_line_sll_ctr).comments := l_line_strlvl_tbl(j).comments;
2478                         l_line_sllv_tbl(l_line_sll_ctr).due_arr_yn := l_line_strlvl_tbl(j).due_arr_yn;
2479                         l_line_sllv_tbl(l_line_sll_ctr).amount := l_line_strlvl_tbl(j).amount;
2480                         l_line_sllv_tbl(l_line_sll_ctr).lines_detailed_yn := l_line_strlvl_tbl(j).lines_detailed_yn;
2481 
2482                         --set the comments to 99 if old line amt <> new line amt for E and P
2483                         --some billing code depends on this
2484                         IF l_top_line_tbl(i).billing_schedule_type IN ('E', 'P') THEN
2485                             IF ( l_top_line_tbl(i).old_line_amt <> l_top_line_tbl(i).new_line_amt ) THEN
2486                                 l_line_sllv_tbl(l_line_sll_ctr).comments := '99';
2487                             ELSE
2488                                 l_line_sllv_tbl(l_line_sll_ctr).comments := NULL;
2489                             END IF;
2490                         END IF;
2491 
2492                     END LOOP; --topline sll loopFOR j IN l_line_strlvl_tbl.first..l_line_strlvl_tbl.last LOOP
2493 
2494                 END LOOP; --topline sll bulk fecth
2495                 CLOSE c_line_strlvl;
2496                 l_line_strlvl_tbl.delete;
2497 
2498                 --call the billing api to create the billing schedule for the topline and it's sublines
2499                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2500                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.topline_billing', 'calling OKS_BILL_SCH.create_bill_sch_rules');
2501                 END IF;
2502 
2503                 OKS_BILL_SCH.create_bill_sch_rules(
2504                     p_billing_type => l_top_line_tbl(i).billing_schedule_type,
2505                     p_sll_tbl => l_line_sllv_tbl,
2506                     p_invoice_rule_id => l_top_line_tbl(i).inv_rule_id,
2507                     x_bil_sch_out_tbl => l_bil_sch_out_tbl,
2508                     x_return_status => x_return_status);
2509 
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', 'after call to OKS_BILL_SCH.create_bill_sch_rules, x_return_status='||x_return_status);
2512                 END IF;
2513 
2514                 l_bil_sch_out_tbl.delete;
2515                 l_line_sllv_tbl.delete;
2516 
2517                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2518                     RAISE FND_API.g_exc_unexpected_error;
2519                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2520                     RAISE FND_API.g_exc_error;
2521                 END IF;
2522 
2523             END LOOP; --topline loop --FOR i IN l_top_line_tbl.first..l_top_line_tbl.last LOOP
2524 
2525         END LOOP; --topline bulk fetch loop
2526         CLOSE c_get_top_lines;
2527         l_top_line_tbl.delete;
2528 
2529         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2530             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
2531         END IF;
2532 
2533     EXCEPTION
2534         WHEN FND_API.g_exc_error THEN
2535             x_return_status := FND_API.g_ret_sts_error;
2536             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2537                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2538             END IF;
2539             IF (c_get_top_lines%isopen) THEN
2540                 CLOSE c_get_top_lines;
2541             END IF;
2542             IF (c_line_strlvl%isopen) THEN
2543                 CLOSE c_line_strlvl;
2544             END IF;
2545             RAISE;
2546 
2547         WHEN FND_API.g_exc_unexpected_error THEN
2548             x_return_status := FND_API.g_ret_sts_unexp_error ;
2549             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2550                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2551             END IF;
2552             IF (c_get_top_lines%isopen) THEN
2553                 CLOSE c_get_top_lines;
2554             END IF;
2555             IF (c_line_strlvl%isopen) THEN
2556                 CLOSE c_line_strlvl;
2557             END IF;
2558             RAISE;
2559 
2560         WHEN OTHERS THEN
2561             x_return_status := FND_API.g_ret_sts_unexp_error ;
2562             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2563                 --first log the sqlerrm
2564                 l_error_text := substr (SQLERRM, 1, 240);
2565                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2566                 --then add it to the message api list
2567                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2568             END IF;
2569             IF (c_get_top_lines%isopen) THEN
2570                 CLOSE c_get_top_lines;
2571             END IF;
2572             IF (c_line_strlvl%isopen) THEN
2573                 CLOSE c_line_strlvl;
2574             END IF;
2575             RAISE;
2576     END RECREATE_LINE_BILLING;
2577 
2578     /*
2579     Internal procedure for recreating header billing schedule. Called after the contract line dates
2580     have been adjusted and the contract has been repriced using the renewal pricing method.
2581     Parameters
2582         p_chr_id                : id of the renewed contract
2583         p_old_chr_id            : id of the source contract
2584     */
2585     PROCEDURE RECREATE_BILLING_FROM_BP
2586     (
2587      p_chr_id IN NUMBER,
2588      p_billing_profile_id IN NUMBER,
2589      x_msg_count OUT NOCOPY NUMBER,
2590      x_msg_data OUT NOCOPY VARCHAR2,
2591      x_return_status OUT NOCOPY VARCHAR2
2592     )
2593     IS
2594     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_BILLING_FROM_BP';
2595     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2596     l_error_text VARCHAR2(512);
2597 
2598     CURSOR c_bp_toplines (cp_chr_id IN NUMBER) IS
2599         SELECT a.id, a.start_date, a.end_date, nvl(b.billing_schedule_type, 'XX'),
2600         a.lse_id, nvl(b.usage_type, 'XX')
2601         FROM   okc_k_lines_b a, oks_k_lines_b b
2602         WHERE  a.dnz_chr_id = cp_chr_id AND a.id = b.cle_id
2603           AND  a.cle_id IS NULL;
2604 
2605     CURSOR c_chk_accounting_rule(l_id NUMBER) IS
2606         SELECT RULE_ID
2607         FROM RA_RULES
2608         WHERE TYPE IN ('A', 'ACC_DUR')
2609         AND RULE_ID = l_id;
2610 
2611     CURSOR c_chk_invoice_rule(l_id NUMBER) IS
2612         SELECT RULE_ID
2613         FROM RA_RULES
2614         WHERE  TYPE = 'I'
2615         AND RULE_ID = l_id;
2616 
2617     l_id_tbl            num_tbl_type;
2618     l_start_dt_tbl      date_tbl_type;
2619     l_end_dt_tbl        date_tbl_type;
2620     l_bsch_typ_tbl      chr_tbl_type;
2621     l_lse_id_tbl        num_tbl_type;
2622     l_usage_typ_tbl     chr_tbl_type;
2623 
2624     l_rec               OKS_BILLING_PROFILES_PUB.billing_profile_rec;
2625     l_sll_tbl_out       OKS_BILLING_PROFILES_PUB.stream_level_tbl;
2626 
2627     l_sll_tbl           OKS_BILL_SCH.streamlvl_tbl;
2628     l_bil_sch_out_tbl   OKS_BILL_SCH.itembillsch_tbl;
2629 
2630     l_invoice_rule_id   NUMBER;
2631     l_account_rule_id   NUMBER;
2632     l_rule_id           NUMBER;
2633     l_var_usg_typ_flag  BOOLEAN := FALSE;
2634 
2635     BEGIN
2636         --log key input parameters
2637         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2638             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);
2639         END IF;
2640         x_return_status := FND_API.G_RET_STS_SUCCESS;
2641 
2642         OPEN c_bp_toplines(p_chr_id);
2643         LOOP
2644 
2645             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;
2646             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2647                 FND_LOG.string(FND_LOG.level_statement, l_mod_name ,'get_toplines : l_id_tbl.count=' || l_id_tbl.count);
2648             END IF;
2649             EXIT WHEN (l_id_tbl.count = 0);
2650 
2651             --for each topline
2652             FOR i IN l_id_tbl.first..l_id_tbl.last LOOP
2653                 l_rec.cle_id := l_id_tbl(i);
2654                 l_rec.chr_id := p_chr_id;
2655                 l_rec.billing_profile_id := p_billing_profile_id;
2656                 l_rec.start_date := l_start_dt_tbl(i);
2657                 l_rec.end_date := l_end_dt_tbl(i);
2658 
2659                 IF (l_bsch_typ_tbl(i) = 'XX') THEN
2660                     l_bsch_typ_tbl(i) := NULL;
2661                 END IF;
2662 
2663                 --get the billing profile based sll
2664                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2665                     FND_LOG.string(FND_LOG.level_statement, l_mod_name,'get_tl_bill_sch : i='||i||
2666                     ' calling OKS_BILLING_PROFILES_PUB.get_billing_schedule, l_rec.cle_id='||l_rec.cle_id||
2667                     ' ,l_rec.start_date='||l_rec.start_date||' ,l_rec.end_date='||l_rec.end_date);
2668                 END IF;
2669 
2670                 OKS_BILLING_PROFILES_PUB.get_billing_schedule(
2671                     p_api_version => 1.0,
2672                     p_init_msg_list => FND_API.G_FALSE,
2673                     p_billing_profile_rec => l_rec,
2674                     x_sll_tbl_out => l_sll_tbl_out,
2675                     x_return_status => x_return_status,
2676                     x_msg_count => x_msg_count,
2677                     x_msg_data => x_msg_data);
2678 
2679                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2680                     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);
2681                 END IF;
2682 
2683                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2684                     RAISE FND_API.g_exc_unexpected_error;
2685                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2686                     RAISE FND_API.g_exc_error;
2687                 END IF;
2688 
2689                 --note  OKS_BILLING_PROFILES_PUB.get_billing_schedule always returns
2690                 --one row and the  values for invoice_rule_id/account_rule_id don't change for
2691                 --every line, they are the same for a given billing profile.
2692                 --we can therefore validate and update invoice_rule_id/account_rule_id only once
2693                 IF l_sll_tbl_out.COUNT > 0 THEN
2694 
2695                     FOR j IN l_sll_tbl_out.first..l_sll_tbl_out.last LOOP
2696 
2697                         IF( l_invoice_rule_id IS NULL) THEN
2698                             l_invoice_rule_id := l_sll_tbl_out(j).invoice_rule_id;
2699                         END IF;
2700                         IF (l_account_rule_id IS NULL) THEN
2701                             l_account_rule_id := l_sll_tbl_out(j).account_rule_id;
2702                         END IF;
2703 
2704                         l_sll_tbl(j).cle_id := l_sll_tbl_out(j).cle_id;
2705                         l_sll_tbl(j).dnz_chr_id := p_chr_id;
2706                         l_sll_tbl(j).sequence_no := l_sll_tbl_out(j).seq_no;
2707                         l_sll_tbl(j).start_date := l_sll_tbl_out(j).start_date;
2708                         l_sll_tbl(j).level_periods := l_sll_tbl_out(j).target_quantity;
2709                         l_sll_tbl(j).uom_per_period := l_sll_tbl_out(j).duration;
2710                         l_sll_tbl(j).level_amount := l_sll_tbl_out(j).amount;
2711                         l_sll_tbl(j).invoice_offset_days := l_sll_tbl_out(j).invoice_offset;
2712                         l_sll_tbl(j).interface_offset_days := l_sll_tbl_out(j).interface_offset;
2713                         l_sll_tbl(j).uom_code := l_sll_tbl_out(j).timeunit;
2714 
2715                     END LOOP;
2716 
2717                     --create billing schedule for the topline and it's sublines
2718                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2719                         FND_LOG.string(FND_LOG.level_statement, l_mod_name,'create_tl_bill_sch: i='||i||' calling OKS_BILL_SCH.create_bill_sch_rules');
2720                     END IF;
2721 
2722                     --for usage lines with variable usage type (Actual by Qty, Actual by Period)
2723                     --the invoice rule has to be "Arrears" (-3), irrespective of the billing profile value
2724                     IF ( (l_lse_id_tbl(i) = 12) AND (l_usage_typ_tbl(i) IN ('VRT', 'QTY'))
2725                          AND (l_invoice_rule_id <> -3) ) THEN
2726 
2727                         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2728                             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));
2729                         END IF;
2730 
2731                         l_var_usg_typ_flag := TRUE;
2732                         OKS_BILL_SCH.create_bill_sch_rules(
2733                             p_billing_type => l_bsch_typ_tbl(i),
2734                             p_sll_tbl => l_sll_tbl,
2735                             p_invoice_rule_id => -3,
2736                             x_bil_sch_out_tbl => l_bil_sch_out_tbl,
2737                             x_return_status => x_return_status);
2738                     ELSE
2739                         OKS_BILL_SCH.create_bill_sch_rules(
2740                             p_billing_type => l_bsch_typ_tbl(i),
2741                             p_sll_tbl => l_sll_tbl,
2742                             p_invoice_rule_id => l_invoice_rule_id,
2743                             x_bil_sch_out_tbl => l_bil_sch_out_tbl,
2744                             x_return_status => x_return_status);
2745                     END IF;
2746 
2747                     IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2748                         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);
2749                     END IF;
2750 
2751                     IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2752                         RAISE FND_API.g_exc_unexpected_error;
2753                     ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2754                         RAISE FND_API.g_exc_error;
2755                     END IF;
2756 
2757                 END IF; --of IF l_sll_tbl_out.COUNT > 0 THEN
2758                 l_sll_tbl_out.delete;
2759                 l_sll_tbl.delete;
2760                 l_bil_sch_out_tbl.delete;
2761 
2762             END LOOP; --of  FOR i IN l_id_tbl.first..l_id_tbl.last LOOP --toplines
2763 
2764         END LOOP; --bulk fetch loop for toplines
2765         CLOSE c_bp_toplines;
2766         l_id_tbl.delete;
2767         l_start_dt_tbl.delete;
2768         l_end_dt_tbl.delete;
2769         l_bsch_typ_tbl.delete;
2770 
2771       -- bug 5112991
2772       -- If there are NO top lines then bypass the below code
2773       IF l_id_tbl.count <> 0 THEN
2774 
2775         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2776             FND_LOG.string(FND_LOG.level_statement, l_mod_name, 'validate_invoice_rule : l_invoice_rule_id='||l_invoice_rule_id);
2777         END IF;
2778 
2779         l_rule_id := NULL;
2780         OPEN c_chk_invoice_rule(l_invoice_rule_id);
2781         FETCH c_chk_invoice_rule INTO l_rule_id;
2782         CLOSE c_chk_invoice_rule;
2783 
2784         IF(l_rule_id IS NULL) THEN
2785             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_INVOICE_RULE');
2786             FND_MESSAGE.set_token('INVOICE_RULE_ID', l_invoice_rule_id);
2787             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2788                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.validate_invoice_rule', FALSE);
2789             END IF;
2790             FND_MSG_PUB.ADD;
2791             RAISE FND_API.g_exc_error;
2792         END IF;
2793 
2794         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2795             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.validate_accounting_rule', 'l_account_rule_id='||l_account_rule_id);
2796         END IF;
2797 
2798         l_rule_id := NULL;
2799         OPEN c_chk_accounting_rule(l_account_rule_id);
2800         FETCH c_chk_accounting_rule INTO l_rule_id;
2801         CLOSE c_chk_accounting_rule;
2802 
2803         IF(l_rule_id IS NULL) THEN
2804             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_ACCTG_RULE');
2805             FND_MESSAGE.set_token('ACCTG_RULE_ID', l_account_rule_id);
2806             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2807                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.validate_accounting_rule', FALSE);
2808             END IF;
2809             FND_MSG_PUB.ADD;
2810             RAISE FND_API.g_exc_error;
2811         END IF;
2812 
2813         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2814             FND_LOG.string(FND_LOG.level_statement, l_mod_name,'upd_inv_rul : updating invoice rule');
2815         END IF;
2816         --update okc_k_lines_b toplines with inv_rule_id
2817         UPDATE okc_k_lines_b
2818             SET inv_rule_id = l_invoice_rule_id
2819             WHERE dnz_chr_id = p_chr_id AND cle_id IS NULL;
2820 
2821         --update variarable usage type lines with "Arrears" (-3) invoice rule if billing profile's invoice
2822         --rule is different
2823         IF (l_var_usg_typ_flag) THEN
2824 
2825             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2826                 FND_LOG.string(FND_LOG.level_statement, l_mod_name,'upd_inv_rul : updating usage invoice rule');
2827             END IF;
2828 
2829             UPDATE okc_k_lines_b a
2830             SET a.inv_rule_id = -3
2831             WHERE a.dnz_chr_id = p_chr_id AND a.cle_id IS NULL AND a.lse_id = 12
2832             AND EXISTS (SELECT 1 FROM oks_k_lines_b b
2833                         WHERE b.cle_id = a.id AND b.usage_type IN ('VRT', 'QTY'));
2834         END IF;
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_acctg_rul : updating accounting rule');
2838         END IF;
2839         --update oks_k_lines_b toplines with acct_rule_id
2840         UPDATE oks_k_lines_b
2841             SET acct_rule_id = l_account_rule_id
2842             WHERE cle_id IN (SELECT id FROM okc_k_lines_b
2843                 WHERE dnz_chr_id = p_chr_id AND cle_id IS NULL);
2844 
2845       END IF;-- bug 5112991  l_id_tbl.count <> 0 THEN
2846 
2847         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2848             FND_LOG.string(FND_LOG.level_procedure, l_mod_name,'end : x_return_status='|| x_return_status);
2849         END IF;
2850 
2851     EXCEPTION
2852         WHEN FND_API.g_exc_error THEN
2853             x_return_status := FND_API.g_ret_sts_error;
2854             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
2855                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
2856             END IF;
2857             IF (c_bp_toplines%isopen) THEN
2858                 CLOSE c_bp_toplines;
2859             END IF;
2860             IF (c_chk_invoice_rule%isopen) THEN
2861                 CLOSE c_chk_invoice_rule;
2862             END IF;
2863             IF (c_chk_accounting_rule%isopen) THEN
2864                 CLOSE c_chk_accounting_rule;
2865             END IF;
2866             RAISE;
2867 
2868         WHEN FND_API.g_exc_unexpected_error THEN
2869             x_return_status := FND_API.g_ret_sts_unexp_error ;
2870             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2871                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
2872             END IF;
2873             IF (c_bp_toplines%isopen) THEN
2874                 CLOSE c_bp_toplines;
2875             END IF;
2876             IF (c_chk_invoice_rule%isopen) THEN
2877                 CLOSE c_chk_invoice_rule;
2878             END IF;
2879             IF (c_chk_accounting_rule%isopen) THEN
2880                 CLOSE c_chk_accounting_rule;
2881             END IF;
2882             RAISE;
2883 
2884         WHEN OTHERS THEN
2885             x_return_status := FND_API.g_ret_sts_unexp_error ;
2886             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
2887                 --first log the sqlerrm
2888                 l_error_text := substr (SQLERRM, 1, 240);
2889                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
2890                 --then add it to the message api list
2891                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
2892             END IF;
2893             IF (c_bp_toplines%isopen) THEN
2894                 CLOSE c_bp_toplines;
2895             END IF;
2896             IF (c_chk_invoice_rule%isopen) THEN
2897                 CLOSE c_chk_invoice_rule;
2898             END IF;
2899             IF (c_chk_accounting_rule%isopen) THEN
2900                 CLOSE c_chk_accounting_rule;
2901             END IF;
2902             RAISE;
2903     END RECREATE_BILLING_FROM_BP;
2904 
2905     /*
2906     Internal procedure for recreating the billing schedules. Called after the contract line dates
2907     have been adjusted and the contract has been repriced using the renewal pricing method.
2908     Parameters
2909         p_chr_id                : id of the renewed contract
2910         p_billing_profile_id    : number of the renewed contract
2911 
2912     The logic of recreating billing is as follows
2913         1. If renewal rules specify a billing profile
2914             a. Delete all existing billing schedules
2915             b. Recreate billing shcedule using the billing profile parameters
2916         2. If renewed contract duration = old contract duration and no billing profile id specified
2917             a. Recreate billing schedule using existing rules
2918         3. If renewed contract duration <> old contract duration and no billing profile specified
2919             a. Delete all existing billing schedules
2920             b. Such a contract will later fail QA check, as it will have no billing schedule
2921     */
2922     PROCEDURE RECREATE_BILLING
2923     (
2924      p_chr_id IN NUMBER,
2925      p_billing_profile_id IN NUMBER,
2926      x_msg_count OUT NOCOPY NUMBER,
2927      x_msg_data OUT NOCOPY VARCHAR2,
2928      x_return_status OUT NOCOPY VARCHAR2
2929     )
2930     IS
2931     l_api_name CONSTANT VARCHAR2(30) := 'RECREATE_BILLING';
2932     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
2933     l_error_text VARCHAR2(512);
2934 
2935     CURSOR c_hdr_dates(cp_chr_id IN NUMBER) IS
2936         SELECT ren.start_date, ren.end_date, old.start_date, old.end_date, old.id,
2937         rens.period_type, rens.period_start, olds.period_type, olds.period_start
2938         FROM okc_k_headers_all_b ren, okc_k_headers_all_b old,
2939         oks_k_headers_b rens, oks_k_headers_b olds
2940         WHERE ren.id = cp_chr_id
2941         AND rens.chr_id = ren.id
2942         AND old.id = ren.orig_system_id1
2943         AND olds.chr_id = old.id;
2944 
2945     l_new_start_date    DATE;
2946     l_new_end_date      DATE;
2947     l_old_start_date    DATE;
2948     l_old_end_date      DATE;
2949     l_old_chr_id        NUMBER;
2950     l_new_period_type   oks_k_headers_b.period_type%TYPE;
2951     l_new_period_start  oks_k_headers_b.period_start%TYPE;
2952     l_old_period_type   oks_k_headers_b.period_type%TYPE;
2953     l_old_period_start  oks_k_headers_b.period_start%TYPE;
2954 
2955     l_new_duration      NUMBER;
2956     l_new_period        VARCHAR2(64);
2957     l_old_duration      NUMBER;
2958     l_old_period        VARCHAR2(64);
2959 
2960     BEGIN
2961 
2962         --log key input parameters
2963         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
2964             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);
2965         END IF;
2966 
2967         x_return_status := FND_API.G_RET_STS_SUCCESS;
2968 
2969         OPEN c_hdr_dates(p_chr_id);
2970         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;
2971         CLOSE c_hdr_dates;
2972 
2973         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2974             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);
2975         END IF;
2976 
2977         OKC_TIME_UTIL_PUB.get_duration(
2978             p_start_date => l_new_start_date,
2979             p_end_date => l_new_end_date,
2980             x_duration => l_new_duration,
2981             x_timeunit => l_new_period,
2982             x_return_status => x_return_status);
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', '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);
2986         END IF;
2987         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
2988             RAISE FND_API.g_exc_unexpected_error;
2989         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
2990             RAISE FND_API.g_exc_error;
2991         END IF;
2992 
2993         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
2994             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);
2995         END IF;
2996 
2997         OKC_TIME_UTIL_PUB.get_duration(
2998             p_start_date => l_old_start_date,
2999             p_end_date => l_old_end_date,
3000             x_duration => l_old_duration,
3001             x_timeunit => l_old_period,
3002             x_return_status => x_return_status);
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', '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);
3006         END IF;
3007         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3008             RAISE FND_API.g_exc_unexpected_error;
3009         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3010             RAISE FND_API.g_exc_error;
3011         END IF;
3012 
3013         --delete billing schedules if old duration <> new duration or billing profile specified
3014         IF((l_old_duration <> l_new_duration) OR (l_old_period <> l_new_period) OR
3015             (p_billing_profile_id IS  NOT NULL) ) THEN
3016 
3017             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3018                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.delete_billing', 'p_billing_profile_id='||p_billing_profile_id);
3019             END IF;
3020 
3021             DELETE FROM oks_level_elements
3022                 WHERE dnz_chr_id = p_chr_id;
3023 
3024             DELETE FROM oks_stream_levels_b
3025                 WHERE dnz_chr_id = p_chr_id;
3026 
3027             --doing this becuase this is what OKS_BILL_SCH.del_rul_elements does
3028             --and we are replacing that call
3029             UPDATE oks_k_lines_b
3030                 SET billing_schedule_type = NULL
3031                 WHERE cle_id IN
3032                     (SELECT id FROM OKC_K_LINES_B WHERE dnz_chr_id = p_chr_id
3033                     AND lse_id IN (7,8,9,10,11,35,13,18,25));
3034 
3035             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3036                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.delete_billing', 'done');
3037             END IF;
3038 
3039         END IF; --of IF((l_old_duration <> l_new_duration....
3040 
3041 
3042         --if duration/period match and billing profile is NULL, recreate billing schedule
3043         IF((l_old_duration = l_new_duration) AND (l_old_period = l_new_period) AND
3044             (p_billing_profile_id IS NULL) ) THEN
3045 
3046             --for partial periods, the  old period type and start should also match
3047             --for billing to be recreated
3048             IF( ( nvl(l_new_period_type, 'X') = nvl(l_old_period_type, 'X') ) AND
3049                 ( nvl(l_new_period_start, 'X') = nvl(l_old_period_start, 'X') ) ) THEN
3050 
3051                 --first recreate header billing schedule
3052                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3053                     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);
3054                 END IF;
3055 
3056                 recreate_hdr_billing(
3057                     p_chr_id => p_chr_id,
3058                     p_old_chr_id => l_old_chr_id,
3059                     x_msg_count => x_msg_count,
3060                     x_msg_data => x_msg_data,
3061                     x_return_status => x_return_status);
3062 
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', 'after recreate_hdr_billing, x_return_status='||x_return_status);
3065                 END IF;
3066                 --end of contract header billing schedule
3067 
3068                 --now recreate lines billing schedule
3069                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3070                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.dur_match_bp_null', 'calling recreate_line_billing, p_chr_id='||p_chr_id);
3071                 END IF;
3072 
3073                 recreate_line_billing(
3074                     p_chr_id => p_chr_id,
3075                     x_msg_count => x_msg_count,
3076                     x_msg_data => x_msg_data,
3077                     x_return_status => x_return_status);
3078 
3079                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3080                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.dur_match_bp_null', 'after recreate_line_billing, x_return_status='||x_return_status);
3081                 END IF;
3082                 --end of lines billing schedule
3083 
3084             END IF;
3085 
3086         END IF; --of if duration/period matches and billing profile is null
3087 
3088 
3089         --if  billing profile specified, recreate billing schedule using billing profile
3090         IF (p_billing_profile_id IS NOT NULL) THEN
3091 
3092             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3093                 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);
3094             END IF;
3095 
3096             recreate_billing_from_bp(
3097                 p_chr_id => p_chr_id,
3098                 p_billing_profile_id => p_billing_profile_id,
3099                 x_msg_count => x_msg_count,
3100                 x_msg_data => x_msg_data,
3101                 x_return_status => x_return_status);
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', 'after call to recreate_billing_from_bp, x_return_status='||x_return_status);
3105             END IF;
3106 
3107         END IF; --IF (p_billing_profile_id IS NOT NULL) THEN
3108 
3109 
3110         --If duration/period don't match and billing profile is NULL, no billing schedule
3111         --is created, such a contract will later fail QA check
3112 
3113         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3114             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3115         END IF;
3116 
3117     EXCEPTION
3118         WHEN FND_API.g_exc_error THEN
3119             x_return_status := FND_API.g_ret_sts_error;
3120             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3121                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3122             END IF;
3123             IF (c_hdr_dates%isopen) THEN
3124                 CLOSE c_hdr_dates;
3125             END IF;
3126             RAISE;
3127 
3128         WHEN FND_API.g_exc_unexpected_error THEN
3129             x_return_status := FND_API.g_ret_sts_unexp_error ;
3130             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3131                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3132             END IF;
3133             IF (c_hdr_dates%isopen) THEN
3134                 CLOSE c_hdr_dates;
3135             END IF;
3136             RAISE;
3137 
3138         WHEN OTHERS THEN
3139             x_return_status := FND_API.g_ret_sts_unexp_error ;
3140             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3141                 --first log the sqlerrm
3142                 l_error_text := substr (SQLERRM, 1, 240);
3143                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3144                 --then add it to the message api list
3145                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3146             END IF;
3147             IF (c_hdr_dates%isopen) THEN
3148                 CLOSE c_hdr_dates;
3149             END IF;
3150             RAISE;
3151 
3152     END RECREATE_BILLING;
3153 
3154 
3155     /*
3156     Internal procedure for assigning a renewed contract to the contract group, if the
3157     renewed contract does not belong to the group
3158         p_chr_id                : id of the renewed contract
3159         p_chr_group_id          : id of the contract group
3160     */
3161     PROCEDURE ASSIGN_CONTRACT_GROUP
3162     (
3163      p_chr_id IN NUMBER,
3164      p_chr_group_id IN NUMBER,
3165      x_msg_count OUT NOCOPY NUMBER,
3166      x_msg_data OUT NOCOPY VARCHAR2,
3167      x_return_status OUT NOCOPY VARCHAR2
3168     )
3169     IS
3170     l_api_name CONSTANT VARCHAR2(30) := 'ASSIGN_CONTRACT_GROUP';
3171     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
3172     l_error_text VARCHAR2(512);
3173 
3174     CURSOR c_group_csr(cp_chr_id NUMBER, cp_grp_id IN NUMBER) IS
3175         SELECT cgp_parent_id id
3176         FROM   okc_k_grpings
3177         WHERE  included_chr_id = cp_chr_id
3178         AND cgp_parent_id = cp_grp_id;
3179 
3180     l_dummy             NUMBER;
3181     l_cgcv_rec_in       OKC_CONTRACT_GROUP_PUB.cgcv_rec_type;
3182     l_cgcv_rec_out      OKC_CONTRACT_GROUP_PUB.cgcv_rec_type;
3183 
3184     BEGIN
3185 
3186         --log key input parameters
3187         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3188             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);
3189         END IF;
3190 
3191         x_return_status := FND_API.G_RET_STS_SUCCESS;
3192 
3193         --check if contract already belongs to this group
3194         OPEN c_group_csr(p_chr_id, p_chr_group_id);
3195         FETCH c_group_csr INTO l_dummy;
3196         CLOSE c_group_csr;
3197 
3198         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3199             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.chk_k_grp', 'l_dummy='||l_dummy);
3200         END IF;
3201 
3202         --only assign the contract to the group is it is not a member of that group
3203         IF (l_dummy IS NULL) THEN
3204 
3205             l_cgcv_rec_in.cgp_parent_id := p_chr_group_id;
3206             l_cgcv_rec_in.included_chr_id := p_chr_id;
3207             l_cgcv_rec_in.object_version_number := FND_API.G_MISS_NUM;
3208             l_cgcv_rec_in.created_by := FND_API.G_MISS_NUM;
3209             l_cgcv_rec_in.creation_date := FND_API.G_MISS_DATE;
3210             l_cgcv_rec_in.last_updated_by := FND_API.G_MISS_NUM;
3211             l_cgcv_rec_in.last_update_date := FND_API.G_MISS_DATE;
3212             l_cgcv_rec_in.last_update_login := FND_API.G_MISS_NUM;
3213             l_cgcv_rec_in.included_cgp_id := NULL;
3214 
3215             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3216                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_k_grp', 'calling OKC_CONTRACT_GROUP_PVT.create_contract_grpngs');
3217             END IF;
3218 
3219             OKC_CONTRACT_GROUP_PVT.create_contract_grpngs(
3220                 p_api_version => 1,
3221                 p_init_msg_list => FND_API.G_FALSE,
3222                 x_return_status => x_return_status,
3223                 x_msg_count => x_msg_count,
3224                 x_msg_data => x_msg_data,
3225                 p_cgcv_rec => l_cgcv_rec_in,
3226                 x_cgcv_rec => l_cgcv_rec_out);
3227 
3228             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3229                 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);
3230             END IF;
3231             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3232                 RAISE FND_API.g_exc_unexpected_error;
3233             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3234                 RAISE FND_API.g_exc_error;
3235             END IF;
3236 
3237         END IF;
3238 
3239         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3240             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3241         END IF;
3242 
3243     EXCEPTION
3244         WHEN FND_API.g_exc_error THEN
3245             x_return_status := FND_API.g_ret_sts_error;
3246             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3247                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3248             END IF;
3249             IF (c_group_csr%isopen) THEN
3250                 CLOSE c_group_csr;
3251             END IF;
3252             RAISE;
3253 
3254         WHEN FND_API.g_exc_unexpected_error THEN
3255             x_return_status := FND_API.g_ret_sts_unexp_error ;
3256             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3257                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3258             END IF;
3259             IF (c_group_csr%isopen) THEN
3260                 CLOSE c_group_csr;
3261             END IF;
3262             RAISE;
3263 
3264         WHEN OTHERS THEN
3265             x_return_status := FND_API.g_ret_sts_unexp_error ;
3266             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3267                 --first log the sqlerrm
3268                 l_error_text := substr (SQLERRM, 1, 240);
3269                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3270                 --then add it to the message api list
3271                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3272             END IF;
3273             IF (c_group_csr%isopen) THEN
3274                 CLOSE c_group_csr;
3275             END IF;
3276             RAISE;
3277 
3278     END ASSIGN_CONTRACT_GROUP;
3279 
3280 
3281     /*
3282     Internal procedure for assigning an approval process to the contract.
3283         p_chr_id                : id of the renewed contract
3284         p_chr_group_id          : id of the contract group
3285     */
3286     PROCEDURE ASSIGN_CONTRACT_PROCESS
3287     (
3288      p_chr_id IN NUMBER,
3289      p_pdf_id IN NUMBER,
3290      x_msg_count OUT NOCOPY NUMBER,
3291      x_msg_data OUT NOCOPY VARCHAR2,
3292      x_return_status OUT NOCOPY VARCHAR2
3293     )
3294     IS
3295     l_api_name CONSTANT VARCHAR2(30) := 'ASSIGN_CONTRACT_PROCESS';
3296     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
3297     l_error_text VARCHAR2(512);
3298 
3299     CURSOR c_pdf(cp_chr_id NUMBER) IS
3300         SELECT id, pdf_id
3301         FROM   okc_k_processes
3302         WHERE  chr_id = cp_chr_id
3303         AND pdf_id IN (SELECT id FROM okc_process_defs_b WHERE pdf_type = 'WPS' AND usage = 'APPROVE');
3304 
3305     l_id                NUMBER;
3306     l_pdf_id            NUMBER;
3307     l_cpsv_rec_in       OKC_CPS_PVT.cpsv_rec_type;
3308     l_cpsv_rec_out      OKC_CPS_PVT.cpsv_rec_type;
3309 
3310     BEGIN
3311 
3312         --log key input parameters
3313         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3314             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id||' ,p_pdf_id='||p_pdf_id);
3315         END IF;
3316 
3317         x_return_status := FND_API.G_RET_STS_SUCCESS;
3318 
3319         --check if contract already belongs to this group
3320         OPEN c_pdf(p_chr_id);
3321         FETCH c_pdf INTO l_id, l_pdf_id;
3322         CLOSE c_pdf;
3323 
3324         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3325             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.chk_k_process', 'l_id='||l_id||' ,l_pdf_id='||l_pdf_id);
3326         END IF;
3327 
3328         IF (l_id IS NULL) THEN
3329 
3330             --no process record exists so, create the contract process with the GCD pdf id
3331             l_cpsv_rec_in.pdf_id := p_pdf_id;
3332             l_cpsv_rec_in.chr_id := p_chr_id;
3333             l_cpsv_rec_in.user_id := FND_GLOBAL.USER_ID;
3334             l_cpsv_rec_in.in_process_yn := FND_API.G_MISS_CHAR;
3335             l_cpsv_rec_in.object_version_number := FND_API.G_MISS_NUM;
3336             l_cpsv_rec_in.created_by := FND_API.G_MISS_NUM;
3337             l_cpsv_rec_in.creation_date := FND_API.G_MISS_DATE;
3338             l_cpsv_rec_in.last_updated_by := FND_API.G_MISS_NUM;
3339             l_cpsv_rec_in.last_update_date := FND_API.G_MISS_DATE;
3340             l_cpsv_rec_in.last_update_login := FND_API.G_MISS_NUM;
3341 
3342             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3343                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.create_k_process', 'calling OKC_CONTRACT_PVT.create_contract_process');
3344             END IF;
3345 
3346             OKC_CONTRACT_PVT.create_contract_process(
3347                 p_api_version => 1,
3348                 p_init_msg_list => FND_API.G_FALSE,
3349                 x_return_status => x_return_status,
3350                 x_msg_count => x_msg_count,
3351                 x_msg_data => x_msg_data,
3352                 p_cpsv_rec => l_cpsv_rec_in,
3353                 x_cpsv_rec => l_cpsv_rec_out);
3354 
3355             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3356                 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);
3357             END IF;
3358             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3359                 RAISE FND_API.g_exc_unexpected_error;
3360             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3361                 RAISE FND_API.g_exc_error;
3362             END IF;
3363 
3364         ELSE
3365 
3366             IF (l_pdf_id = p_pdf_id) THEN
3367 
3368                 --do nothing, as process record exists and has the same pdf as GCD pdf id
3369                 NULL;
3370 
3371             ELSE
3372 
3373                 --update the contract process record
3374                 l_cpsv_rec_in.pdf_id := p_pdf_id;
3375                 l_cpsv_rec_in.id := l_id;
3376 
3377                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3378                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_k_process', 'calling OKC_CONTRACT_PVT.update_contract_process');
3379                 END IF;
3380 
3381                 OKC_CONTRACT_PVT.update_contract_process(
3382                     p_api_version => 1,
3383                     p_init_msg_list => FND_API.G_FALSE,
3384                     x_return_status => x_return_status,
3385                     x_msg_count => x_msg_count,
3386                     x_msg_data => x_msg_data,
3387                     p_cpsv_rec => l_cpsv_rec_in,
3388                     x_cpsv_rec => l_cpsv_rec_out);
3389 
3390                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3391                     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);
3392                 END IF;
3393                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3394                     RAISE FND_API.g_exc_unexpected_error;
3395                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3396                     RAISE FND_API.g_exc_error;
3397                 END IF;
3398 
3399             END IF;
3400         END IF;
3401 
3402 
3403         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3404             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3405         END IF;
3406 
3407     EXCEPTION
3408         WHEN FND_API.g_exc_error THEN
3409             x_return_status := FND_API.g_ret_sts_error;
3410             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3411                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3412             END IF;
3413             IF (c_pdf%isopen) THEN
3414                 CLOSE c_pdf;
3415             END IF;
3416             RAISE;
3417 
3418         WHEN FND_API.g_exc_unexpected_error THEN
3419             x_return_status := FND_API.g_ret_sts_unexp_error ;
3420             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3421                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3422             END IF;
3423             IF (c_pdf%isopen) THEN
3424                 CLOSE c_pdf;
3425             END IF;
3426             RAISE;
3427 
3428         WHEN OTHERS THEN
3429             x_return_status := FND_API.g_ret_sts_unexp_error ;
3430             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3431                 --first log the sqlerrm
3432                 l_error_text := substr (SQLERRM, 1, 240);
3433                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3434                 --then add it to the message api list
3435                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3436             END IF;
3437             IF (c_pdf%isopen) THEN
3438                 CLOSE c_pdf;
3439             END IF;
3440             RAISE;
3441 
3442     END ASSIGN_CONTRACT_PROCESS;
3443 
3444     /*
3445     Internal procedure updating various contract header and line attributes as per the renewal rule
3446         p_chr_id                : id of the renewed contract
3447         p_rnrl_rec              : effective renewal rules for the renewed contract
3448         p_notify_to             : salesperson/helpdesk user id on whose behalf the workflow is launched
3449     */
3450     PROCEDURE UPDATE_RENEWED_CONTRACT
3451     (
3452      p_chr_id IN NUMBER,
3453      p_rnrl_rec IN OKS_RENEW_UTIL_PVT.rnrl_rec_type,
3454      p_notify_to IN NUMBER,
3455      x_msg_count OUT NOCOPY NUMBER,
3456      x_msg_data OUT NOCOPY VARCHAR2,
3457      x_return_status OUT NOCOPY VARCHAR2
3458     )
3459     IS
3460     l_api_name CONSTANT VARCHAR2(30) := 'UPDATE_RENEWED_CONTRACT';
3461     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
3462     l_error_text VARCHAR2(512);
3463 
3464     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
3465         SELECT a.contract_number, a.contract_number_modifier,
3466         nvl(a.org_id, a.authoring_org_id), a.qcl_id, a.estimated_amount, a.currency_code,
3467         a.payment_term_id, a.conversion_type, a.conversion_rate, a.conversion_rate_date,
3468         a.conversion_euro_rate, b.renewal_po_number, b.trxn_extension_id,
3469         b.grace_period, b.grace_duration,  c.date_renewed,  -- added for bug 6086893
3470         a.start_date, a.end_date, c.scs_code, c.sts_code, c.estimated_amount,
3471         c.start_date, c.end_date, scs.cls_code, c.id
3472         FROM okc_k_headers_all_b a, oks_k_headers_b b, okc_k_headers_all_b c, OKC_SUBCLASSES_B SCS
3473         WHERE a.id = b.chr_id
3474         AND a.id = cp_chr_id
3475         AND c.id = a.orig_system_id1
3476         AND c.scs_code = scs.code;
3477 
3478 -- added for bug 6086893
3479    l_new_start_date        okc_k_headers_all_b.start_date%TYPE;
3480    l_new_end_date          okc_k_headers_all_b.end_date%TYPE;
3481    l_old_scs_code          okc_k_headers_all_b.scs_code%TYPE;
3482    l_old_sts_code          okc_k_headers_all_b.sts_code%TYPE;
3483    l_old_estimated_amount  okc_k_headers_all_b.estimated_amount%TYPE;
3484    l_old_start_date        okc_k_headers_all_b.start_date%TYPE;
3485    l_old_end_date          okc_k_headers_all_b.end_date%TYPE;
3486    l_cls_code              okc_subclasses_b.cls_code%TYPE;
3487    l_old_k_id              okc_k_headers_all_b.id%TYPE;
3488 -- end added for bug 6086893
3489 
3490     l_k_num                     VARCHAR2(120);
3491     l_k_mod                     VARCHAR2(120);
3492     l_k_org_id                  NUMBER;
3493     l_qcl_id                    NUMBER;
3494     l_estimated_amount          NUMBER;
3495     l_k_currency_code           VARCHAR2(15);
3496     l_sob_currency_code         VARCHAR2(15);
3497     l_payment_term_id           NUMBER;
3498     l_conv_type                 VARCHAR2(30);
3499     l_conv_rate                 NUMBER;
3500     l_conv_rate_date            DATE;
3501     l_conv_euro_rate            NUMBER;
3502     --while updating PO number we will just use the first 50 characters, otherwise AR apis fail
3503     l_renewal_po_number         VARCHAR2(240);
3504     l_trxn_extension_id         NUMBER;
3505     l_grace_period              VARCHAR2(30);
3506     l_grace_duration            NUMBER;
3507     l_wf_item_key               VARCHAR2(240);
3508     l_date_renewed              DATE;
3509 
3510     l_cust_po_number            VARCHAR2(50);
3511     l_cust_po_number_req_yn     VARCHAR2(1);
3512     l_payment_instruction_type  VARCHAR2(3);
3513     l_threshold_used            VARCHAR2(1);
3514     l_renewal_type              VARCHAR2(30);
3515     l_approval_type             VARCHAR2(30);
3516 
3517     l_est_rev_percent           NUMBER;
3518     l_est_rev_date              DATE;
3519     l_renewal_po_used           VARCHAR2(1);
3520     l_renewal_pricing_type_used VARCHAR2(30);
3521     l_renewal_markup_percent_used   NUMBER;
3522     l_renewal_price_list_used   NUMBER;
3523     l_evn_threshold_amt         NUMBER;
3524     l_evn_threshold_cur         VARCHAR2(15);
3525     l_ern_threshold_amt         NUMBER;
3526     l_ern_threshold_cur         VARCHAR2(15);
3527     l_renewal_status            VARCHAR2(30);
3528 
3529     l_wf_attributes             OKS_WF_K_PROCESS_PVT.WF_ATTR_DETAILS;
3530 
3531     -- bug 4967105 (base bug 4966475)
3532     l_est_rev_date_offset  NUMBER;
3533 
3534     BEGIN
3535 
3536         --log key input parameters
3537         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3538             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
3539             OKS_RENEW_UTIL_PVT.log_rules(l_mod_name || '.effective_renewal_rules', p_rnrl_rec);
3540         END IF;
3541         x_return_status := FND_API.G_RET_STS_SUCCESS;
3542 
3543         OPEN c_k_hdr(p_chr_id);
3544         FETCH c_k_hdr INTO l_k_num, l_k_mod, l_k_org_id, l_qcl_id, l_estimated_amount,
3545             l_k_currency_code, l_payment_term_id,
3546             l_conv_type, l_conv_rate, l_conv_rate_date, l_conv_euro_rate, l_renewal_po_number,
3547             l_trxn_extension_id, l_grace_period, l_grace_duration,  l_date_renewed,
3548             -- added bug 6086893
3549             l_new_start_date, l_new_end_date, l_old_scs_code, l_old_sts_code,
3550             l_old_estimated_amount, l_old_start_date, l_old_end_date, l_cls_code,l_old_k_id;
3551             -- end added bug 6086893
3552         CLOSE c_k_hdr;
3553 
3554         l_qcl_id := nvl(p_rnrl_rec.qcl_id, l_qcl_id);
3555 
3556         --update payment term only if Credit Card is present
3557         --the check for payment term validity has been moved to Contract QA
3558         IF (l_trxn_extension_id IS NOT NULL) THEN
3559             l_payment_term_id := nvl(to_number(p_rnrl_rec.payment_terms_id1), l_payment_term_id);
3560         END IF;
3561 
3562         --always stamp the renewal po number to customer po number
3563         l_cust_po_number := substr(l_renewal_po_number, 1, 50);
3564 
3565         --p_rnrl_rec will follow the K->Party->Org->Global path for po required flag, can be null also
3566         --we need to change the l_cust_po_number_req_yn to N, if renewal type is evergreen
3567         l_cust_po_number_req_yn := p_rnrl_rec.po_required_yn;
3568 
3569         --as per lookup type OKS_PAYMENT_INST_TYPE
3570         IF ((l_renewal_po_number IS NOT NULL) OR (nvl(l_cust_po_number_req_yn, 'N') = 'Y') )  THEN
3571             l_payment_instruction_type := 'PON';
3572         ELSE
3573             --we will always null out payment instructions, except when the renewal PO number is stamped
3574             l_payment_instruction_type := NULL;
3575         END IF;
3576 
3577         --Null out the CVN (currency conversion) attributes if contract currency = set of books currency
3578         --for the contract org
3579         l_sob_currency_code := OKC_CURRENCY_API.get_ou_currency(l_k_org_id);
3580         IF( l_sob_currency_code = l_k_currency_code) THEN
3581             l_conv_type := NULL;
3582             l_conv_rate := NULL;
3583             l_conv_rate_date := NULL;
3584             l_conv_euro_rate := NULL;
3585         END IF;
3586 
3587         --determine the renewal type and corresponding approval type
3588         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3589             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.determine_renewal_type', 'calling OKS_RENEW_UTIL_PVT.get_renewal_type');
3590         END IF;
3591         OKS_RENEW_UTIL_PVT.get_renewal_type(
3592             p_api_version => 1,
3593             p_init_msg_list => FND_API.G_FALSE,
3594             x_return_status => x_return_status,
3595             x_msg_count => x_msg_count,
3596             x_msg_data => x_msg_data,
3597             p_chr_id => p_chr_id,
3598             p_amount => l_estimated_amount,
3599             p_currency_code => l_k_currency_code,
3600             p_rnrl_rec => p_rnrl_rec,
3601             x_renewal_type => l_renewal_type,
3602             x_approval_type => l_approval_type,
3603             x_threshold_used => l_threshold_used);
3604 
3605         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3606             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||
3607             ' ,x_approval_type='||l_approval_type||' ,x_threshold_used='||l_threshold_used);
3608         END IF;
3609 
3610         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3611             RAISE FND_API.g_exc_unexpected_error;
3612         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3613             RAISE FND_API.g_exc_error;
3614         END IF;
3615 
3616         --set the po required to N if renewal type is EVN
3617         IF (l_renewal_type = 'EVN') THEN
3618             l_cust_po_number_req_yn := 'N';
3619         END IF;
3620 
3621         -- bug 4967105 (base bug 4966475)
3622         IF NVL(p_rnrl_rec.revenue_estimated_duration,0) = 0 THEN
3623 	   l_est_rev_date_offset := 0;
3624 	ELSE
3625 	   l_est_rev_date_offset := 1;
3626 	END IF;
3627 
3628         l_est_rev_percent := p_rnrl_rec.revenue_estimated_percent;
3629         l_est_rev_date := OKC_TIME_UTIL_PUB.get_enddate(
3630                               p_start_date => trunc(l_date_renewed),
3631                               p_duration => p_rnrl_rec.revenue_estimated_duration,
3632                               p_timeunit => p_rnrl_rec.revenue_estimated_period) + l_est_rev_date_offset; --bug 4967105
3633 
3634         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3635             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);
3636         END IF;
3637 
3638         l_grace_period  := nvl(p_rnrl_rec.grace_period, l_grace_period);
3639         l_grace_duration  := nvl(p_rnrl_rec.grace_duration, l_grace_duration);
3640         l_renewal_po_used := nvl(l_cust_po_number_req_yn, 'N');
3641 
3642         l_renewal_pricing_type_used := p_rnrl_rec.renewal_pricing_type;
3643         IF (p_rnrl_rec.renewal_pricing_type = 'MAN') THEN
3644             l_renewal_markup_percent_used := NULL;
3645             l_renewal_price_list_used := NULL;
3646         ELSE
3647             l_renewal_markup_percent_used := p_rnrl_rec.markup_percent;
3648             l_renewal_price_list_used := p_rnrl_rec.price_list_id1;
3649         END IF;
3650 
3651         l_evn_threshold_amt := null;
3652         l_evn_threshold_cur := null;
3653         l_ern_threshold_amt := null;
3654         l_ern_threshold_cur := null;
3655 
3656         IF( (l_renewal_type = 'EVN') AND (l_threshold_used = 'Y') ) THEN
3657             l_evn_threshold_amt := p_rnrl_rec.evergreen_threshold_amt;
3658             l_evn_threshold_cur := p_rnrl_rec.base_currency;
3659         END IF;
3660         IF( (l_renewal_type = 'ERN') AND (l_threshold_used = 'Y') ) THEN
3661             l_ern_threshold_amt := p_rnrl_rec.threshold_amount;
3662             l_ern_threshold_cur := p_rnrl_rec.base_currency;
3663         END IF;
3664 
3665         --update the renewal status also, so that if the workflow is not launched
3666         --or the background process does not pick it up, K is still disabled
3667         --from authoring form
3668         l_renewal_status := 'DRAFT';
3669         IF (l_renewal_type = 'EVN') THEN
3670             IF (l_approval_type = 'Y') THEN
3671                 l_renewal_status := 'PENDING_IA';
3672             ELSIF (l_approval_type = 'N') THEN
3673                 l_renewal_status := 'PEND_ACTIVATION';
3674             END IF;
3675         ELSIF (l_renewal_type = 'ERN') THEN
3676             l_renewal_status := 'PEND_PUBLISH';
3677         END IF;
3678 
3679         l_wf_item_key := p_chr_id || to_char(SYSDATE, 'YYYYMMDDHH24MISS');
3680 
3681         --update oks with the new attributes
3682         UPDATE oks_k_headers_b
3683             SET renewal_status = l_renewal_status,
3684                 est_rev_percent = l_est_rev_percent,
3685                 est_rev_date = l_est_rev_date,
3686                 grace_duration = l_grace_duration,
3687                 grace_period = l_grace_period,
3688                 renewal_type_used = l_renewal_type,
3689                 renewal_grace_duration_used = l_grace_duration,
3690                 renewal_grace_period_used = l_grace_period,
3691                 renewal_notification_to = p_notify_to,
3692                 renewal_po_used = l_renewal_po_used,
3693                 renewal_pricing_type_used = l_renewal_pricing_type_used,
3694                 renewal_markup_percent_used = l_renewal_markup_percent_used,
3695                 renewal_price_list_used = l_renewal_price_list_used,
3696                 rev_est_percent_used = p_rnrl_rec.revenue_estimated_percent,
3697                 rev_est_duration_used = p_rnrl_rec.revenue_estimated_duration,
3698                 rev_est_period_used = p_rnrl_rec.revenue_estimated_period,
3699                 billing_profile_used = p_rnrl_rec.billing_profile_id,
3700                 ern_flag_used_yn = NULL, --obsolete column
3701                 evn_threshold_amt = l_evn_threshold_amt,
3702                 evn_threshold_cur = l_evn_threshold_cur,
3703                 ern_threshold_amt = l_ern_threshold_amt,
3704                 ern_threshold_cur = l_ern_threshold_cur,
3705                 electronic_renewal_flag = NULL, --obsolete column
3706                 approval_type_used = l_approval_type,
3707                 wf_item_key = l_wf_item_key
3708             WHERE chr_id = p_chr_id;
3709 
3710         --update OKC header with the new attributes
3711         --note that we do not update the renewal type/approval type, we carry forward the old values
3712         UPDATE okc_k_headers_all_b
3713             SET qcl_id = l_qcl_id,
3714                 payment_term_id = l_payment_term_id,
3715                 cust_po_number = l_cust_po_number,
3716                 cust_po_number_req_yn = l_cust_po_number_req_yn,
3717                 payment_instruction_type = l_payment_instruction_type,
3718                 conversion_type = l_conv_type,
3719                 conversion_rate = l_conv_rate,
3720                 conversion_rate_date = l_conv_rate_date,
3721                 conversion_euro_rate = l_conv_euro_rate
3722             WHERE id = p_chr_id;
3723 
3724         --null out OKC/OKS top lines payment instruction and po number attributes
3725         UPDATE okc_k_lines_b
3726             SET payment_instruction_type = NULL
3727             WHERE dnz_chr_id = p_chr_id AND cle_id IS NULL AND lse_id IN (1,12,19,46);
3728 
3729         UPDATE oks_k_lines_b b
3730             SET b.cust_po_number = NULL,
3731                 b.cust_po_number_req_yn = NULL
3732             WHERE b.dnz_chr_id = p_chr_id
3733             AND b.cle_id IN (SELECT a.id FROM okc_k_lines_b a
3734                 WHERE a.dnz_chr_id = p_chr_id AND a.cle_id IS NULL AND a.lse_id IN (1,12,19,46));
3735 
3736 
3737         --for new contracts the workflow process is started by the OKS TAPI
3738         --for renewed contract this procedure will start the workflow process
3739         --for Manual renewal - the workflow will not be deferred so that the users can
3740         --immediately submit it for approval, for Online and Evergreen renewals the worklfow
3741         --will be started in the deferred mode to reduce execution time for this api
3742 
3743         l_wf_attributes.contract_id := p_chr_id;
3744         l_wf_attributes.contract_number := l_k_num;
3745         l_wf_attributes.contract_modifier := l_k_mod;
3746         l_wf_attributes.negotiation_status := l_renewal_status;
3747         l_wf_attributes.item_key := l_wf_item_key;
3748         l_wf_attributes.irr_flag := l_approval_type;
3749         l_wf_attributes.process_type := l_renewal_type;
3750 
3751         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3752             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||
3753             ' ,.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);
3754         END IF;
3755 
3756         OKS_WF_K_PROCESS_PVT.launch_k_process_wf(
3757             p_api_version => 1.0,
3758             p_init_msg_list => FND_API.G_FALSE,
3759             p_wf_attributes => l_wf_attributes,
3760             x_return_status => x_return_status,
3761             x_msg_count => x_msg_count,
3762             x_msg_data => x_msg_data) ;
3763 
3764         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3765             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);
3766         END IF;
3767 
3768         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3769             RAISE FND_API.g_exc_unexpected_error;
3770         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3771             RAISE FND_API.g_exc_error;
3772         END IF;
3773 
3774       -- bug 6086893
3775       -- Added call to OKC_K_RENEW_ASMBLR_PVT.acn_assemble after the workflow is successfully launched
3776 
3777         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3778            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);
3779             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);
3780             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);
3781             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);
3782         END IF;
3783 
3784           OKC_K_RENEW_ASMBLR_PVT.acn_assemble(
3785            p_api_version           => 1,
3786            p_init_msg_list         => OKC_API.G_FALSE,
3787            x_return_status         => x_return_status,
3788            x_msg_count             => x_msg_count,
3789            x_msg_data              => x_msg_data,
3790            p_k_class               => l_cls_code,
3791            p_k_nbr_mod             => l_k_mod,
3792            p_k_number              => l_k_num,
3793            p_k_subclass            => l_old_scs_code,
3794            p_k_status_code         => l_old_sts_code,
3795            p_estimated_amount      => l_old_estimated_amount,
3796            p_new_k_end_date        => l_new_end_date,
3797            p_new_k_id              => p_chr_id,
3798            p_new_k_start_date      => l_new_start_date,
3799            p_original_k_end_date   => l_old_end_date,
3800            p_original_kid          => l_old_k_id,
3801            p_original_k_start_date => l_old_start_date);
3802 
3803 
3804         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
3805             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);
3806         END IF;
3807 
3808         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
3809             RAISE FND_API.g_exc_unexpected_error;
3810         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
3811             RAISE FND_API.g_exc_error;
3812         END IF;
3813 
3814 
3815 
3816       -- end added bug 6086893
3817 
3818         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
3819             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
3820         END IF;
3821 
3822     EXCEPTION
3823         WHEN FND_API.g_exc_error THEN
3824             x_return_status := FND_API.g_ret_sts_error;
3825             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
3826                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
3827             END IF;
3828             IF (c_k_hdr%isopen) THEN
3829                 CLOSE c_k_hdr;
3830             END IF;
3831             RAISE;
3832 
3833         WHEN FND_API.g_exc_unexpected_error THEN
3834             x_return_status := FND_API.g_ret_sts_unexp_error ;
3835             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3836                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
3837             END IF;
3838             IF (c_k_hdr%isopen) THEN
3839                 CLOSE c_k_hdr;
3840             END IF;
3841             RAISE;
3842 
3843         WHEN OTHERS THEN
3844             x_return_status := FND_API.g_ret_sts_unexp_error ;
3845             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
3846                 --first log the sqlerrm
3847                 l_error_text := substr (SQLERRM, 1, 240);
3848                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
3849                 --then add it to the message api list
3850                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
3851             END IF;
3852             IF (c_k_hdr%isopen) THEN
3853                 CLOSE c_k_hdr;
3854             END IF;
3855             RAISE;
3856 
3857     END UPDATE_RENEWED_CONTRACT;
3858 
3859 
3860 ------------------------ End  Internal procedures ----------------------------------
3861 
3862     /*
3863     R12 procedure that validates if a contract can be renewed
3864     Parameters
3865         p_chr_id : contract id of the contract being renewed
3866         p_date : start date of the renewal, if not passed defaults to end date + 1 of the source contract
3867         p_validation_level : A - do all checks including warnings, E - do only error checks
3868         x_rnrl_rec : returns the effective renewal rules for the contract
3869         x_validation_status : S - Success (OK for renewal), W - Warnings (Ok for renewal)
3870                              E - Errors (Cannot be renewed)
3871         x_validation_tbl : Validation error and warning messages
3872 
3873     The following validations are done
3874     Error Conditions
3875         1.	Contract is a template.
3876         2.	Contract status is not in ACTIVE, EXPIRED or SIGNED base statuses.
3877         3.	Contract end date is not null (perpetual contract).
3878         4.	Contract has been terminated.
3879         5.	Contract has been renewed and the renewal has not been cancelled.
3880         6.	If the user does not update access to the contract
3881         7.	If all sublines (or subscription top lines) in status ACTIVE, EXPIRED or SIGNED
3882             have already been renew consolidated.
3883         8.	The effective renewal type for contract is  'Do not renew'. If the renewal type
3884             is not defined for the contract, it is derived from Party -> Org -> Global setup
3885             by calling the get_renew_rules procedure.
3886         9.  If the contract contains any warranty lines (lse id = 14) it cannot be renewed
3887             (Currently there is no check for warranty lines, a contract having warranty lines
3888             can be renewed, but all warranty lines are dropped during copy.)
3889 
3890     Warning Conditions
3891         1.	Contract has been renewed and the renewal has been cancelled.
3892             For background (events) renewal this is an error condition.
3893         2.	All contract sublines and subscription toplines have been terminated or cancelled.
3894             For background (events) renewal this is an error condition.
3895         3.	All contract sublines and subscription toplines have an effective line renewal type
3896             code of DNR. (Effective line renewal type code = nvl(line renewal type code ,
3897             parent line renewal type code). For background (events) renewal this is an error
3898             condition.
3899 
3900     This procedure does not stop if any error/warning condition is found, all validations
3901     are always done
3902     */
3903     PROCEDURE VALIDATE_RENEWAL
3904     (
3905      p_api_version IN NUMBER DEFAULT 1,
3906      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
3907      x_return_status OUT NOCOPY VARCHAR2,
3908      x_msg_count OUT NOCOPY NUMBER,
3909      x_msg_data OUT NOCOPY VARCHAR2,
3910      p_chr_id IN NUMBER,
3911      p_date IN DATE,
3912      p_validation_level IN VARCHAR2 DEFAULT G_VALIDATE_ALL,
3913      x_rnrl_rec OUT NOCOPY OKS_RENEW_UTIL_PVT.rnrl_rec_type,
3914      x_validation_status OUT NOCOPY VARCHAR2,
3915      x_validation_tbl OUT NOCOPY validation_tbl_type
3916     )
3917     IS
3918 
3919     l_api_name CONSTANT VARCHAR2(30) := 'VALIDATE_RENEWAL';
3920     l_api_version CONSTANT NUMBER := 1;
3921     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || g_pkg_name || '.' || l_api_name;
3922     l_error_text VARCHAR2(512);
3923 
3924     --cursor to get basic contract information
3925     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
3926         SELECT  a.application_id, a.contract_number, a.contract_number_modifier,
3927             b.ste_code, b.meaning, a.scs_code, a.template_yn,
3928             a.date_terminated, a.date_renewed, a.end_date
3929         FROM okc_k_headers_all_b a, okc_statuses_v b
3930         WHERE a.id = cp_chr_id AND a.sts_code = b.code;
3931 
3932     --cursor to check if all sublines and subscr toplines have been renew consolidated
3933     CURSOR c_check_line_rencon(cp_chr_id IN NUMBER) IS
3934         SELECT kl.id
3935         FROM okc_k_lines_b kl
3936         WHERE kl.dnz_chr_id = cp_chr_id AND kl.lse_id IN (7, 8, 9, 10, 11, 13, 25, 35, 46)
3937             AND kl.id NOT IN(
3938                              SELECT ol.object_cle_id
3939                              FROM okc_operation_lines ol, okc_operation_instances oi, okc_class_operations oo
3940                              WHERE oo.cls_code = 'SERVICE' AND oo.opn_code = 'REN_CON' AND oo.id = oi.cop_id
3941                              AND ol.oie_id = oi.id AND ol.object_chr_id = cp_chr_id
3942                              AND ol.subject_chr_id IS NOT NULL
3943                              AND ol.process_flag = 'P'  AND ol.active_yn = 'Y');
3944 
3945     --cursor to check if a contract has been renewed. If the contract  has been
3946     --renewed, get the renewed contract's status, number and modifier.
3947     --we order by active_yn desc, as for cancelled renewed contracts
3948     --active_yn = 'N'. So we want to get the active renewals (such as entered/signed status etc) first
3949     CURSOR c_check_hdr_renew (cp_chr_id IN NUMBER) IS
3950         SELECT k.contract_number, k.contract_number_modifier, st.ste_code
3951         FROM okc_operation_lines ol, okc_operation_instances oi, okc_class_operations oo,
3952             okc_statuses_b st, okc_k_headers_all_b k
3953         WHERE oo.cls_code = 'SERVICE' AND oo.opn_code = 'RENEWAL'
3954             AND oo.id = oi.cop_id
3955             AND ol.oie_id = oi.id AND ol.object_chr_id = cp_chr_id
3956             AND ol.subject_chr_id IS NOT NULL AND ol.object_cle_id IS NULL
3957             AND ol.subject_cle_id IS NULL
3958             AND ol.process_flag = 'P' AND ol.subject_chr_id = k.id
3959             AND k.sts_code = st.code
3960             ORDER BY ol.active_yn DESC;
3961 
3962     --cursor to check if all sublines and subscr toplines have been cancelled or terminated
3963     CURSOR c_check_line_term_canc (cp_chr_id IN NUMBER) IS
3964         SELECT id
3965         FROM okc_k_lines_b
3966         WHERE dnz_chr_id = cp_chr_id AND lse_id IN (7, 8, 9, 10, 11, 13, 25, 35, 46)
3967             AND date_terminated IS  NULL AND date_cancelled IS NULL;
3968 
3969     --cursor to determine if any sublines or toplines exist with an effective renewal type
3970     --that is not DNR. If a topline has renewal type DNR, none of it's sublines are considered.
3971     --If topline is not DNR then sublines are checked to see if any exist with renewal type not DNR
3972     CURSOR c_check_line_dnr (cp_chr_id IN NUMBER) IS
3973         SELECT a.id
3974         FROM okc_k_lines_b a, okc_k_lines_b b
3975         WHERE a.dnz_chr_id = cp_chr_id
3976         AND b.dnz_chr_id (+)  = cp_chr_id
3977         AND a.cle_id = b.id (+)
3978         AND a.lse_id IN (7,8,9,10,11,13,25,35,46)
3979         AND decode(b.line_renewal_type_code, 'DNR', 'DNR',
3980                     NULL, nvl(a.line_renewal_type_code, 'FUL'),
3981                     nvl(a.line_renewal_type_code, b.line_renewal_type_code)) <> 'DNR';
3982 
3983     --cursor to check if the contract contains any warranty lines
3984     CURSOR c_check_line_warr (cp_chr_id IN NUMBER) IS
3985         SELECT id
3986         FROM okc_k_lines_b
3987         WHERE dnz_chr_id = cp_chr_id AND lse_id = 14;
3988 
3989     --cursor to check id there are any valid sublines and subscr toplines
3990     --we need to do line level checks only if a valid line exists, other wise we get
3991     --redundant error messages
3992     CURSOR c_check_valid_line(cp_chr_id IN NUMBER) IS
3993         SELECT id
3994         FROM okc_k_lines_b kl, okc_statuses_b st
3995         WHERE kl.dnz_chr_id = cp_chr_id AND kl.lse_id IN (7, 8, 9, 10, 11, 13, 25, 35, 46)
3996         AND kl.sts_code = st.ste_code
3997         AND st.ste_code IN ('ACTIVE', 'EXPIRED', 'SIGNED', 'CANCELLED', 'TERMINATED');
3998 
3999     l_k_app_id okc_k_headers_b.application_id%TYPE;
4000     l_k_num okc_k_headers_b.contract_number%TYPE;
4001     l_k_mod okc_k_headers_b.contract_number_modifier%TYPE;
4002     l_k_ste_code okc_statuses_b.ste_code%TYPE;
4003     l_k_ste_meaning okc_statuses_tl.meaning%TYPE;
4004     l_k_scs_code okc_k_headers_b.scs_code%TYPE;
4005     l_k_template_yn okc_k_headers_b.template_yn%TYPE;
4006     l_k_date_terminated okc_k_headers_b.date_terminated%TYPE;
4007     l_k_date_renewed okc_k_headers_b.date_renewed%TYPE;
4008     l_k_end_date okc_k_headers_b.end_date%TYPE;
4009     l_rnrl_rec OKS_RENEW_UTIL_PVT.rnrl_rec_type;
4010 
4011     l_date DATE;
4012     l_k_num_mod VARCHAR2(250);
4013     l_msg_count INTEGER := 0;
4014     l_k_is_renewed BOOLEAN := FALSE;
4015     l_k_access_level VARCHAR2(1);
4016     l_k_line_id NUMBER;
4017     l_k_ren_type oks_k_defaults.renewal_type%TYPE;
4018 
4019     l_renk_num okc_k_headers_b.contract_number%TYPE;
4020     l_renk_mod okc_k_headers_b.contract_number_modifier%TYPE;
4021     l_renk_ste_code okc_statuses_b.ste_code%TYPE;
4022     l_valid_line_exists BOOLEAN := FALSE;
4023     BEGIN
4024         --log key input parameters
4025         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4026             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);
4027         END IF;
4028 
4029         --standard api initilization and checks
4030         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
4031             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4032         END IF;
4033         IF FND_API.to_boolean(p_init_msg_list ) THEN
4034             FND_MSG_PUB.initialize;
4035         END IF;
4036         x_return_status := FND_API.G_RET_STS_SUCCESS;
4037         x_validation_status := G_VALID_STS_SUCCESS;
4038 
4039         --first get the basic contract attributes
4040         OPEN c_k_hdr(p_chr_id);
4041         FETCH c_k_hdr INTO l_k_app_id, l_k_num, l_k_mod, l_k_ste_code, l_k_ste_meaning,
4042         l_k_scs_code, l_k_template_yn, l_k_date_terminated, l_k_date_renewed, l_k_end_date;
4043 
4044         IF (c_k_hdr%notfound) THEN
4045             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
4046             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
4047             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4048                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_k_values', FALSE);
4049             END IF;
4050             FND_MSG_PUB.ADD;
4051             CLOSE c_k_hdr;
4052             RAISE FND_API.g_exc_error;
4053         END IF;
4054         CLOSE c_k_hdr;
4055 
4056         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4057             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
4058             ||', 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);
4059         END IF;
4060 
4061         -- no checks if not service contract
4062         IF ((nvl(l_k_app_id, - 99) <> 515)
4063             OR (l_k_scs_code NOT IN ('SERVICE', 'WARRANTY', 'SUBSCRIPTION')) )THEN
4064             RETURN;
4065         END IF;
4066 
4067         IF(trim(l_k_mod) IS NULL) THEN
4068             l_k_num_mod := l_k_num;
4069         ELSE
4070             l_k_num_mod := l_k_num || '-' || trim(l_k_mod);
4071         END IF;
4072 
4073         --error if contract has WARRANTY (lse_id =14) lines
4074         l_k_line_id := NULL;
4075         OPEN c_check_line_warr(p_chr_id);
4076         FETCH c_check_line_warr INTO l_k_line_id;
4077         CLOSE c_check_line_warr;
4078 
4079         IF (l_k_line_id IS NOT NULL) THEN
4080             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_WARRANTY_DNR');
4081             FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4082             l_msg_count := l_msg_count + 1;
4083             x_validation_tbl(l_msg_count).code := 'OKS_WARRANTY_DNR';
4084             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4085             x_validation_status := G_VALID_STS_ERROR;
4086             --we don't  need to do any more checks if this is a warranty contract
4087             RETURN;
4088         END IF;
4089 
4090         --error if contract is template
4091         IF (nvl(l_k_template_yn, 'X') = 'Y') THEN
4092             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_K_TEMPLATE');
4093             FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4094             l_msg_count := l_msg_count + 1;
4095             x_validation_tbl(l_msg_count).code := 'OKS_K_TEMPLATE';
4096             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4097             x_validation_status := G_VALID_STS_ERROR;
4098         END IF;
4099 
4100         --error if contract end date is not null
4101         IF (l_k_end_date IS NULL) THEN
4102             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_PERPETUAL');
4103             FND_MESSAGE.set_token('COMPONENT', l_k_num_mod);
4104             l_msg_count := l_msg_count + 1;
4105             x_validation_tbl(l_msg_count).code := 'OKS_NO_PERPETUAL';
4106             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4107             x_validation_status := G_VALID_STS_ERROR;
4108         END IF;
4109 
4110         --if p_date is null, use contract end date + 1 . If contract end date is also null use sysdate
4111         l_date := trunc(nvl(p_date, nvl(l_k_end_date, SYSDATE - 1) + 1));
4112 
4113 
4114         --error if status not in 'ACTIVE', 'EXPIRED' and 'SIGNED'
4115         IF (l_k_ste_code NOT IN ('ACTIVE', 'EXPIRED', 'SIGNED')) THEN
4116             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVALID_K_STATUS');
4117             FND_MESSAGE.set_token('STATUS', l_k_ste_meaning);
4118             l_msg_count := l_msg_count + 1;
4119             x_validation_tbl(l_msg_count).code := 'OKS_INVALID_K_STATUS';
4120             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4121             x_validation_status := G_VALID_STS_ERROR;
4122         END IF;
4123 
4124         --error if contract has been terminated for a future date
4125         IF (l_k_date_terminated IS NOT NULL) THEN
4126             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_FUTURE_TERMINATED_K');
4127             FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4128             l_msg_count := l_msg_count + 1;
4129             x_validation_tbl(l_msg_count).code := 'OKS_FUTURE_TERMINATED_K';
4130             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4131             x_validation_status := G_VALID_STS_ERROR;
4132         END IF;
4133 
4134         --check if contract has been renewed
4135         IF (l_k_date_renewed IS NOT NULL) THEN
4136             l_k_is_renewed := TRUE;
4137             --This will be checked later to figure get the status of the renewed contract
4138             --and set the appropriate message in the validation table.
4139         END IF;
4140 
4141         --error if user does not have update access for the contract
4142         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4143             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);
4144         END IF;
4145 
4146         l_k_access_level := OKC_UTIL.get_all_k_access_level(p_chr_id, l_k_app_id, l_k_scs_code);
4147 
4148         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4149             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_get_k_access_level', 'l_k_access_level=' || l_k_access_level);
4150         END IF;
4151 
4152         IF (nvl(l_k_access_level, 'X') <> 'U') THEN
4153             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_UPDATE');
4154             FND_MESSAGE.set_token('CHR', l_k_num_mod);
4155             l_msg_count := l_msg_count + 1;
4156             x_validation_tbl(l_msg_count).code := 'OKS_NO_UPDATE';
4157             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4158             x_validation_status := G_VALID_STS_ERROR;
4159         END IF;
4160 
4161         --before doing any line level validations, check if there are valid lines to begin with
4162         l_k_line_id := NULL;
4163         OPEN c_check_valid_line(p_chr_id);
4164         FETCH c_check_valid_line INTO l_k_line_id;
4165         CLOSE c_check_valid_line;
4166 
4167         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4168             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_valid_line_check', 'l_k_line_id=' || l_k_line_id);
4169         END IF;
4170 
4171         IF ( l_k_line_id IS NULL ) THEN
4172             l_valid_line_exists := FALSE;
4173         ELSE
4174             l_valid_line_exists := TRUE;
4175         END IF;
4176 
4177         IF (l_valid_line_exists) THEN
4178             --error if all sublines and subsciption toplines have been renew consolidated
4179             l_k_line_id := NULL;
4180             OPEN c_check_line_rencon(p_chr_id);
4181             FETCH c_check_line_rencon INTO l_k_line_id;
4182             CLOSE c_check_line_rencon;
4183 
4184             IF (l_k_line_id IS NULL) THEN
4185                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_K_RENEW_CONSOLIDATED');
4186                 l_msg_count := l_msg_count + 1;
4187                 x_validation_tbl(l_msg_count).code := 'OKS_K_RENEW_CONSOLIDATED';
4188                 x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4189                 x_validation_status := G_VALID_STS_ERROR;
4190             END IF;
4191         END IF;
4192 
4193         --error if effective renewal type of contract is DNR
4194         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4195             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calling_get_renew_rules', 'p_chr_id=' || p_chr_id ||', p_date='|| l_date);
4196         END IF;
4197 
4198         OKS_RENEW_UTIL_PVT.get_renew_rules(
4199                         x_return_status => x_return_status,
4200                         p_api_version => 1.0,
4201                         p_init_msg_list => FND_API.G_FALSE,
4202                         p_chr_id => p_chr_id,
4203                         p_party_id => NULL,
4204                         p_org_id => NULL,
4205                         p_date => l_date,
4206                         p_rnrl_rec => l_rnrl_rec,
4207                         x_rnrl_rec => x_rnrl_rec,
4208                         x_msg_count => x_msg_count,
4209                         x_msg_data => x_msg_data);
4210 
4211         l_k_ren_type := x_rnrl_rec.renewal_type;
4212 
4213         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4214             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);
4215         END IF;
4216 
4217         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
4218             RAISE FND_API.g_exc_unexpected_error;
4219         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
4220             RAISE FND_API.g_exc_error;
4221         END IF;
4222 
4223         IF (nvl(l_k_ren_type, 'X') = 'DNR') THEN
4224             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_DNR_MSG');
4225             l_msg_count := l_msg_count + 1;
4226             x_validation_tbl(l_msg_count).code := 'OKS_DNR_MSG';
4227             x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4228             x_validation_status := G_VALID_STS_ERROR;
4229         END IF;
4230 
4231 
4232         --error if contract has been renewed and renewed contract is in ENETERED status
4233         --warning if the renewed contract is CANCELLED. All other statuses are also
4234         --treated as error conditons
4235         --We need to do this check only if the contract has been renewed (date_renewed is not null)
4236         --or if validation_level is 'A'. This is baecause if a contract is renewed and then cancelled
4237         --the date_renewed on the source contract is nulled out
4238 
4239         IF ((l_k_is_renewed) OR (p_validation_level = G_VALIDATE_ALL) ) THEN
4240 
4241             OPEN c_check_hdr_renew (p_chr_id);
4242             FETCH c_check_hdr_renew INTO l_renk_num, l_renk_mod, l_renk_ste_code;
4243             CLOSE c_check_hdr_renew;
4244 
4245             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4246                 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);
4247             END IF;
4248 
4249             --if a renewed contract is found, set error/warning message as per status
4250             IF (l_renk_num IS NOT NULL) THEN
4251 
4252                 IF(trim(l_renk_mod) IS NULL) THEN
4253                     l_renk_num := l_renk_num;
4254                 ELSE
4255                     l_renk_num := l_renk_num || '-' || trim(l_renk_mod);
4256                 END IF;
4257 
4258                 --renewed contract is CANCELLED - warning
4259                 IF(nvl(l_renk_ste_code, 'X') = 'CANCELLED' )THEN
4260 
4261                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_ALREADY_NOT_RENEWED');
4262                     --FND_MESSAGE.set_token('CHR', l_k_num_mod);
4263                     l_msg_count := l_msg_count + 1;
4264                     x_validation_tbl(l_msg_count).code := 'OKS_ALREADY_NOT_RENEWED';
4265                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4266                     IF (x_validation_status <> G_VALID_STS_ERROR) THEN
4267                         x_validation_status := G_VALID_STS_WARNING;
4268                     END IF;
4269 
4270                 --renewed contract is ENETERED - error
4271                 ELSIF (nvl(l_renk_ste_code, 'X') = 'ENTERED' ) THEN
4272 
4273                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_RENCOPY_ENTERED');
4274                     FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4275                     FND_MESSAGE.set_token('RENCOPY', l_renk_num);
4276                     l_msg_count := l_msg_count + 1;
4277                     x_validation_tbl(l_msg_count).code := 'OKS_RENCOPY_ENTERED';
4278                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4279                     x_validation_status := G_VALID_STS_ERROR;
4280 
4281                 --all other statuses treated as ACTIVE ( ACTIVE/EXPIRED/SIGNED/QA_HOLD etc) -error
4282                 ELSE
4283                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_RENCOPY_APPROVE');
4284                     FND_MESSAGE.set_token('NUMBER', l_k_num_mod);
4285                     FND_MESSAGE.set_token('RENCOPY', l_renk_num);
4286                     l_msg_count := l_msg_count + 1;
4287                     x_validation_tbl(l_msg_count).code := 'OKS_RENCOPY_APPROVE';
4288                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4289                     x_validation_status := G_VALID_STS_ERROR;
4290 
4291                 END IF; --of IF( nvl(l_renk_ste_code, 'X') = 'CANCELLED' )THEN
4292 
4293             END IF; --IF (l_renk_num IS NOT NULL) THEN
4294 
4295         END IF; --of IF ( (l_k_is_renewed) OR (p_validation_level = G_VALIDATE_ALL) ) THEN
4296 
4297         --now do the warning checks if p_validation_level = 'A'
4298         IF (p_validation_level = G_VALIDATE_ALL) THEN
4299 
4300             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4301                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.checking_for_warnings', 'begin');
4302             END IF;
4303 
4304             IF (l_valid_line_exists) THEN
4305 
4306                 --warning if all sublines and subscr toplines have been terminated or cancelled
4307                 l_k_line_id := NULL;
4308                 OPEN c_check_line_term_canc(p_chr_id);
4309                 FETCH c_check_line_term_canc INTO l_k_line_id;
4310                 CLOSE c_check_line_term_canc;
4311 
4312                 IF (l_k_line_id IS NULL) THEN
4313                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_LINES_SUBLINES_TERMINATED');
4314                     l_msg_count := l_msg_count + 1;
4315                     x_validation_tbl(l_msg_count).code := 'OKS_LINES_SUBLINES_TERMINATED';
4316                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4317                     IF (x_validation_status <> G_VALID_STS_ERROR) THEN
4318                         x_validation_status := G_VALID_STS_WARNING;
4319                     END IF;
4320                 END IF;
4321 
4322                 --warning if all sublines and subscr toplines have effective line renewal type of DNR
4323                 l_k_line_id := NULL;
4324                 OPEN c_check_line_dnr(p_chr_id);
4325                 FETCH c_check_line_dnr INTO l_k_line_id;
4326                 CLOSE c_check_line_dnr;
4327 
4328                 IF (l_k_line_id IS NULL) THEN
4329                     FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_LINES_DNR');
4330                     l_msg_count := l_msg_count + 1;
4331                     x_validation_tbl(l_msg_count).code := 'OKS_LINES_DNR';
4332                     x_validation_tbl(l_msg_count).message := FND_MESSAGE.get;
4333                     IF (x_validation_status <> G_VALID_STS_ERROR) THEN
4334                         x_validation_status := G_VALID_STS_WARNING;
4335                     END IF;
4336                 END IF;
4337 
4338             END IF; --of  IF (l_valid_line_exists) THEN
4339 
4340         END IF; --of IF (p_validation_level = G_VALIDATE_ALL) THEN
4341 
4342         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4343             IF (x_validation_tbl.count > 0 ) THEN
4344                 FOR i IN x_validation_tbl.first..x_validation_tbl.last LOOP
4345                     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);
4346                 END LOOP;
4347             END IF;
4348             FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_validation_status=' || x_validation_status ||', x_return_status='|| x_return_status);
4349         END IF;
4350         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4351 
4352     EXCEPTION
4353         WHEN FND_API.g_exc_error THEN
4354             x_return_status := FND_API.g_ret_sts_error ;
4355 
4356             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4357                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
4358             END IF;
4359             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4360 
4361             IF (c_k_hdr%isopen) THEN
4362                 CLOSE c_k_hdr;
4363             END IF;
4364             IF (c_check_line_rencon%isopen) THEN
4365                 CLOSE c_check_line_rencon;
4366             END IF;
4367             IF (c_check_hdr_renew%isopen) THEN
4368                 CLOSE c_check_hdr_renew;
4369             END IF;
4370             IF (c_check_line_term_canc%isopen) THEN
4371                 CLOSE c_check_line_term_canc;
4372             END IF;
4373             IF (c_check_line_dnr%isopen) THEN
4374                 CLOSE c_check_line_dnr;
4375             END IF;
4376             IF (c_check_line_warr%isopen) THEN
4377                 CLOSE c_check_line_warr;
4378             END IF;
4379             IF (c_check_valid_line%isopen) THEN
4380                 CLOSE c_check_valid_line;
4381             END IF;
4382 
4383         WHEN FND_API.g_exc_unexpected_error THEN
4384             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4385 
4386             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4387                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
4388             END IF;
4389             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4390 
4391             IF (c_k_hdr%isopen) THEN
4392                 CLOSE c_k_hdr;
4393             END IF;
4394             IF (c_check_line_rencon%isopen) THEN
4395                 CLOSE c_check_line_rencon;
4396             END IF;
4397             IF (c_check_hdr_renew%isopen) THEN
4398                 CLOSE c_check_hdr_renew;
4399             END IF;
4400             IF (c_check_line_term_canc%isopen) THEN
4401                 CLOSE c_check_line_term_canc;
4402             END IF;
4403             IF (c_check_line_dnr%isopen) THEN
4404                 CLOSE c_check_line_dnr;
4405             END IF;
4406             IF (c_check_line_warr%isopen) THEN
4407                 CLOSE c_check_line_warr;
4408             END IF;
4409             IF (c_check_valid_line%isopen) THEN
4410                 CLOSE c_check_valid_line;
4411             END IF;
4412 
4413         WHEN OTHERS THEN
4414             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4415 
4416             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4417                 --first log the sqlerrm
4418                 l_error_text := substr (SQLERRM, 1, 240);
4419                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
4420                 --then add it to the message api list
4421                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
4422             END IF;
4423             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4424 
4425             IF (c_k_hdr%isopen) THEN
4426                 CLOSE c_k_hdr;
4427             END IF;
4428             IF (c_check_line_rencon%isopen) THEN
4429                 CLOSE c_check_line_rencon;
4430             END IF;
4431             IF (c_check_hdr_renew%isopen) THEN
4432                 CLOSE c_check_hdr_renew;
4433             END IF;
4434             IF (c_check_line_term_canc%isopen) THEN
4435                 CLOSE c_check_line_term_canc;
4436             END IF;
4437             IF (c_check_line_dnr%isopen) THEN
4438                 CLOSE c_check_line_dnr;
4439             END IF;
4440             IF (c_check_line_warr%isopen) THEN
4441                 CLOSE c_check_line_warr;
4442             END IF;
4443             IF (c_check_valid_line%isopen) THEN
4444                 CLOSE c_check_valid_line;
4445             END IF;
4446 
4447     END VALIDATE_RENEWAL;
4448 
4449     /*
4450     Procedure for updating  invoice_text col table OKC_K_LINES_TL
4451     with the current line start date and end date. Called during renewal,
4452     after line dates are adjusted. Uses bulk calls to get and set the invoice text
4453     Parameters
4454         p_chr_id    : id of the contract whose lines need to be updated
4455 
4456        The format of the invoice text is as follows
4457        topline = SUBSTR(l_item_desc || ':' || p_start_date || ':' || p_end_date, 1, 450);
4458 
4459        subline = SUBSTR(p_topline_item_desc || ':' || l_num_items || ':' || l_item_desc || ':' || p_start_date || ':' || p_end_date, 1, 450);
4460 
4461        bug 4712579 / bug 4992884
4462        subline for usage (lse_id = 13) is as follows
4463        subline = SUBSTR(p_topline_item_desc || ':' || l_item_desc || ':' || p_start_date || ':' || p_end_date, 1, 450);
4464 
4465 	   get invoice text from previous contract subline truncating the dates at the end
4466     */
4467 
4468     PROCEDURE UPDATE_INVOICE_TEXT
4469     (
4470      p_api_version IN NUMBER DEFAULT 1,
4471      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4472      p_commit   IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4473      x_return_status OUT NOCOPY VARCHAR2,
4474      x_msg_count OUT NOCOPY NUMBER,
4475      x_msg_data OUT NOCOPY VARCHAR2,
4476      p_chr_id IN NUMBER
4477     )
4478     IS
4479     l_api_name CONSTANT VARCHAR2(30) := 'UPDATE_INVOICE_TEXT';
4480     l_api_version CONSTANT NUMBER := 1;
4481     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
4482     l_error_text VARCHAR2(512);
4483 
4484     TYPE line_rec_type IS RECORD(
4485         sl_id           NUMBER,
4486         name            VARCHAR2(4000),
4487         descr           VARCHAR2(4000),
4488         num_of_items    NUMBER,
4489         cle_id          NUMBER,
4490         lse_id          NUMBER,
4491         start_date      VARCHAR2(100), --DATE,
4492         end_date        VARCHAR2(100)); --DATE);
4493     TYPE line_tbl_type IS TABLE OF line_rec_type INDEX BY BINARY_INTEGER;
4494 
4495     l_line_tbl      line_tbl_type;
4496     l_inv_txt_tbl   chr_tbl_type;
4497     l_sl_id_tbl     num_tbl_type;
4498     l_cle_id_tbl    num_tbl_type;
4499     l_disp_pref     VARCHAR2(255);
4500 
4501     CURSOR c_get_topline_txt (cp_chr_id IN NUMBER) IS
4502   	    SELECT
4503             --kl.lse_id, bk.inventory_item_id id1, bk.organization_id id2 , bk.organization_id inv_org_id,
4504             sl.id, bt.description name, bk.concatenated_segments description, null,
4505             null, null, to_char(kl.start_date,'DD-MON-YYYY'), to_char(kl.end_date,'DD-MON-YYYY')
4506   	    FROM mtl_system_items_b_kfv bk, mtl_system_items_tl bt,
4507             okc_k_items it, oks_k_lines_b sl, okc_k_lines_b kl
4508   	    WHERE bk.inventory_item_id = bt.inventory_item_id
4509         AND bk.organization_id = bt.organization_id
4510   		AND bt.language = USERENV('LANG')
4511         AND bk.inventory_item_id = it.object1_id1
4512         AND bk.organization_id = it.object1_id2
4513         AND it.cle_id = kl.id
4514         AND sl.cle_id = kl.id
4515         AND kl.lse_id IN (1, 12, 14, 19, 46)
4516         AND kl.cle_id IS NULL
4517         AND kl.dnz_chr_id = cp_chr_id;
4518 
4519 
4520     CURSOR c_get_subline_txt (cp_chr_id IN NUMBER) IS
4521         SELECT
4522             --kl.lse_id, iv.id1, iv.id2, iv.inv_org_id,
4523             sl.id, iv.name, iv.description, it.number_of_items,
4524             kl.cle_id, kl.lse_id, to_char(kl.START_DATE,'DD-MON-YYYY'), to_char(kl.end_date,'DD-MON-YYYY')
4525         FROM
4526         (
4527                 SELECT 'OKX_COVSYST' TYPE, T.SYSTEM_ID ID1, '#' ID2, T.NAME NAME,
4528                 T.DESCRIPTION DESCRIPTION, NULL INV_ORG_ID
4529                 FROM CSI_SYSTEMS_TL T
4530                 WHERE T.LANGUAGE = USERENV('LANG')
4531             UNION ALL
4532                 SELECT 'OKX_PARTYSITE' TYPE, PSE.PARTY_SITE_ID ID1, '#' ID2, PSE.PARTY_SITE_NAME NAME,
4533                 SUBSTR(arp_addr_label_pkg.format_address(NULL,LCN.ADDRESS1,LCN.ADDRESS2,LCN.ADDRESS3,
4534                 LCN.ADDRESS4,LCN.CITY,LCN.COUNTY,LCN.STATE,LCN.PROVINCE,LCN.POSTAL_CODE,NULL,LCN.COUNTRY,
4535                 NULL,NULL,NULL,NULL,NULL,NULL,NULL,'N','N',80,1,1
4536                 ),1,80) DESCRIPTION, NULL INV_ORG_ID
4537                 FROM HZ_PARTY_SITES PSE,HZ_LOCATIONS LCN
4538                 WHERE LCN.LOCATION_ID = PSE.LOCATION_ID
4539                 AND LCN.CONTENT_SOURCE_TYPE = 'USER_ENTERED'
4540             UNION ALL
4541                 SELECT 'OKX_PARTY' TYPE, P.PARTY_ID ID1, '#' ID2, P.PARTY_NAME NAME,
4542                 P.PARTY_NUMBER DESCRIPTION, NULL INV_ORG_ID
4543                 FROM HZ_PARTIES P
4544                 WHERE P.PARTY_TYPE IN ( 'PERSON','ORGANIZATION')
4545             UNION ALL
4546                 SELECT 'OKX_CUSTPROD' TYPE, CII.INSTANCE_ID ID1, '#' ID2, SIT.DESCRIPTION NAME,
4547                 BK.CONCATENATED_SEGMENTS DESCRIPTION, BK.ORGANIZATION_ID INV_ORG_ID
4548                 FROM CSI_ITEM_INSTANCES CII, CSI_I_PARTIES CIP,
4549                 MTL_SYSTEM_ITEMS_B_KFV BK, MTL_SYSTEM_ITEMS_TL SIT
4550                 WHERE CII.INSTANCE_ID = CIP.INSTANCE_ID AND CIP.RELATIONSHIP_TYPE_CODE = 'OWNER'
4551                 AND CIP.PARTY_SOURCE_TABLE = 'HZ_PARTIES' AND
4552                 NOT EXISTS ( SELECT 1 FROM CSI_INSTALL_PARAMETERS CIPM
4553                             WHERE CIPM.INTERNAL_PARTY_ID = CIP.PARTY_ID )
4554                 AND BK.INVENTORY_ITEM_ID = CII.INVENTORY_ITEM_ID
4555                 AND SIT.INVENTORY_ITEM_ID = BK.INVENTORY_ITEM_ID
4556                 AND SIT.ORGANIZATION_ID = BK.ORGANIZATION_ID
4557                 AND SIT.LANGUAGE = USERENV('LANG')
4558             UNION ALL
4559                 SELECT  'OKX_CUSTACCT' TYPE, CA.CUST_ACCOUNT_ID ID1, '#' ID2,
4560                 decode(CA.ACCOUNT_NAME, null, 	P.PARTY_NAME, CA.Account_NAME) NAME,
4561                 CA.ACCOUNT_NUMBER DESCRIPTION, NULL INV_ORG_ID
4562                 FROM HZ_CUST_ACCOUNTS CA,HZ_PARTIES P
4563                 WHERE CA.PARTY_ID = P.PARTY_ID
4564             UNION ALL
4565                 SELECT 'OKX_COUNTER' TYPE, CCT.COUNTER_ID ID1, '#' ID2, CCT.NAME NAME,
4566                 CCT.DESCRIPTION DESCRIPTION, NULL INV_ORG_ID
4567                 FROM CSI_COUNTERS_TL CCT WHERE CCT.LANGUAGE = USERENV('LANG')
4568             UNION ALL
4569                 SELECT 'OKX_COVITEM' TYPE, B.INVENTORY_ITEM_ID ID1, to_char(B.ORGANIZATION_ID) ID2,
4570                 T.DESCRIPTION NAME, B.CONCATENATED_SEGMENTS DESCRIPTION, B.ORGANIZATION_ID INV_ORG_ID
4571                 FROM MTL_SYSTEM_ITEMS_B_KFV B,MTL_SYSTEM_ITEMS_TL T
4572                 WHERE B.INVENTORY_ITEM_ID = T.INVENTORY_ITEM_ID AND B.ORGANIZATION_ID = T.ORGANIZATION_ID
4573                 AND T.LANGUAGE = USERENV('LANG')
4574         ) iv, okc_k_items it, oks_k_lines_b sl, okc_k_lines_b kl, okc_k_headers_all_b kh
4575         WHERE iv.type = it.jtot_object1_code -- bug 5218936
4576         AND iv.id1 = it.object1_id1
4577         AND iv.id2 = it.object1_id2
4578         AND decode(iv.inv_org_id, null, kh.inv_organization_id, iv.inv_org_id) = kh.inv_organization_id
4579         AND it.cle_id = kl.id
4580         AND sl.cle_id = kl.id
4581         AND kl.lse_id IN (7,8,9,10,11,35, 13, 18, 25)
4582         AND kl.dnz_chr_id = kh.id
4583         AND kh.id = cp_chr_id;
4584 
4585  -- bug 4992884 , invoice text for counters lse_id = 13
4586 CURSOR csr_counter_inv_text (cp_chr_id IN NUMBER, cp_sl_id IN NUMBER) IS
4587 SELECT it.concatenated_segments AS Name ,
4588        it.Description AS Description
4589  FROM csi_counters_b ccb ,
4590       csi_counters_tl cct ,
4591       cs_csi_counter_groups cg ,
4592       csi_counter_associations cca ,
4593       csi_item_instances cp ,
4594       mtl_system_items_kfv it,
4595       okc_k_items items,
4596       oks_k_lines_b kl,
4597       okc_k_headers_all_b khr
4598 WHERE ccb.counter_id = cct.counter_id
4599   AND cct.language = USERENV('LANG')
4600   AND ccb.group_id = cg.counter_group_id
4601   AND ccb.counter_id = cca.counter_id
4602   AND cca.source_object_code = 'CP'
4603   AND cca.source_object_id = cp.instance_id
4604   AND cp.inventory_item_id = it.inventory_item_id
4605   AND ccb.counter_id = items.object1_id1
4606   AND items.dnz_chr_id = khr.id
4607   AND it.organization_id = khr.inv_organization_id
4608   AND kl.cle_id = items.cle_id
4609   AND khr.id = cp_chr_id
4610   AND kl.id = cp_sl_id;
4611 
4612 l_counter_inv_text_rec csr_counter_inv_text%ROWTYPE;
4613 
4614 
4615 
4616     BEGIN
4617 
4618         --log key input parameters
4619         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4620             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
4621                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.begin', 'p_chr_id=' || p_chr_id);
4622             END IF;
4623         END IF;
4624 
4625         --standard api initilization and checks
4626         SAVEPOINT update_invoice_text_PVT;
4627         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
4628             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4629         END IF;
4630         IF FND_API.to_boolean(p_init_msg_list ) THEN
4631             FND_MSG_PUB.initialize;
4632         END IF;
4633         x_return_status := FND_API.G_RET_STS_SUCCESS;
4634 
4635         --we will first get and update the topline invoice text
4636         OPEN c_get_topline_txt(p_chr_id);
4637         LOOP
4638             FETCH c_get_topline_txt BULK COLLECT INTO l_line_tbl LIMIT G_BULK_FETCH_LIMIT;
4639 
4640             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4641                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_top_lines', 'l_line_tbl.count='||l_line_tbl.count);
4642             END IF;
4643 
4644             EXIT WHEN (l_line_tbl.count = 0);
4645 
4646             FOR i in l_line_tbl.first..l_line_tbl.last LOOP
4647                 l_sl_id_tbl(i) :=  l_line_tbl(i).sl_id;
4648                 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);
4649 
4650                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4651                     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
4652                     ||' ,l_line_tbl(i).start_date='||l_line_tbl(i).start_date||' ,l_line_tbl(i).end_date='||l_line_tbl(i).end_date);
4653                 END IF;
4654 
4655             END LOOP;
4656 
4657             --update the oks_k_lines_tl for the toplines
4658             FORALL j in l_sl_id_tbl.first..l_sl_id_tbl.last
4659                 UPDATE oks_k_lines_tl
4660                     SET invoice_text = l_inv_txt_tbl(j)
4661                     WHERE id = l_sl_id_tbl(j) AND language = USERENV('LANG');
4662 
4663             l_line_tbl.delete;
4664             l_inv_txt_tbl.delete;
4665             l_sl_id_tbl.delete;
4666 
4667         END LOOP; --topline bulk fetch loop
4668         CLOSE c_get_topline_txt;
4669         l_line_tbl.delete;
4670         l_inv_txt_tbl.delete;
4671         l_sl_id_tbl.delete;
4672 
4673         --now update the subline invoice text, this requires the top line item desc
4674         l_disp_pref := fnd_profile.VALUE('OKS_ITEM_DISPLAY_PREFERENCE');
4675         OPEN c_get_subline_txt(p_chr_id);
4676         LOOP
4677             FETCH c_get_subline_txt BULK COLLECT INTO l_line_tbl LIMIT G_BULK_FETCH_LIMIT;
4678 
4679             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4680                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_sub_lines', 'l_line_tbl.count='||l_line_tbl.count);
4681             END IF;
4682 
4683             EXIT WHEN (l_line_tbl.count = 0);
4684 
4685             FOR i in l_line_tbl.first..l_line_tbl.last LOOP
4686                 l_sl_id_tbl(i) :=  l_line_tbl(i).sl_id;
4687                 l_cle_id_tbl(i) := l_line_tbl(i).cle_id;
4688 
4689                 --discuss with Ramesh
4690                 --7,8,9,10,11,35,13,18,25
4691                 --9,18,25(auth),  8,10,11,35(renew), (7,13???)
4692                 --IF (l_line_tbl(i).lse_id IN (8, 10, 11, 35)) THEN
4693 
4694                 IF (l_line_tbl(i).lse_id = 13) THEN
4695                   -- bug 4992884
4696                   OPEN csr_counter_inv_text (cp_chr_id => p_chr_id, cp_sl_id => l_line_tbl(i).sl_id);
4697                     FETCH csr_counter_inv_text INTO l_counter_inv_text_rec;
4698                   CLOSE csr_counter_inv_text;
4699                     IF ( nvl(l_disp_pref, 'X') = 'DISPLAY_DESC') THEN
4700                        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);
4701 					ELSE
4702 					   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);
4703 					END IF;
4704                 ELSIF (l_line_tbl(i).lse_id NOT IN (9,18,25)) THEN
4705                    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);
4706                 ELSE
4707                    IF ( nvl(l_disp_pref, 'X') = 'DISPLAY_DESC') THEN
4708                         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);
4709                     ELSE
4710                         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);
4711                     END IF;
4712                 END IF;
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_loop', 'i='||i||' ,l_line_tbl(i).name='||l_line_tbl(i).name||' ,l_line_tbl(i).descr='||l_line_tbl(i).descr||
4716                     ' ,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);
4717                 END IF;
4718 
4719             END LOOP;
4720 
4721             --update the oks_k_lines_tl for the sublines using toplines inv txt
4722             FORALL j in l_sl_id_tbl.first..l_sl_id_tbl.last
4723                 UPDATE oks_k_lines_tl c
4724                     SET c.invoice_text =
4725                         (SELECT SUBSTR(a.invoice_text,1, decode(INSTR(a.invoice_text, ':'),0,
4726                         LENGTH(a.invoice_text), INSTR(a.invoice_text, ':'))) ||l_inv_txt_tbl(j)
4727                         FROM oks_k_lines_tl a, oks_k_lines_b b
4728                         WHERE a.id = b.id AND a.language = USERENV('LANG')
4729                         AND b.cle_id = l_cle_id_tbl(j))
4730                     WHERE id = l_sl_id_tbl(j) AND language = USERENV('LANG');
4731             l_line_tbl.delete;
4732             l_inv_txt_tbl.delete;
4733             l_sl_id_tbl.delete;
4734             l_cle_id_tbl.delete;
4735         END LOOP; --topline bulk fetch loop
4736         CLOSE c_get_subline_txt;
4737         l_line_tbl.delete;
4738         l_inv_txt_tbl.delete;
4739         l_sl_id_tbl.delete;
4740         l_cle_id_tbl.delete;
4741 
4742         --standard check of p_commit
4743 	    IF FND_API.to_boolean( p_commit ) THEN
4744 		    COMMIT;
4745 	    END IF;
4746         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4747             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
4748                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_return_status='|| x_return_status);
4749             END IF;
4750         END IF;
4751         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4752 
4753     EXCEPTION
4754         WHEN FND_API.g_exc_error THEN
4755             ROLLBACK TO update_invoice_text_PVT;
4756             x_return_status := FND_API.g_ret_sts_error ;
4757 
4758             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4759                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
4760             END IF;
4761             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4762 
4763         WHEN FND_API.g_exc_unexpected_error THEN
4764             ROLLBACK TO update_invoice_text_PVT;
4765             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4766 
4767             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4768                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
4769             END IF;
4770             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4771 
4772         WHEN OTHERS THEN
4773             ROLLBACK TO update_invoice_text_PVT;
4774             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4775 
4776             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4777                 --first log the sqlerrm
4778                 l_error_text := substr (SQLERRM, 1, 240);
4779                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
4780                 --then add it to the message api list
4781                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
4782             END IF;
4783             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4784 
4785     END UPDATE_INVOICE_TEXT;
4786 
4787     /*
4788     Procedure for getting the user id and name of the contact on whose behalf the
4789     contract workflow is launched during renewal
4790     Parameters
4791         p_chr_id            : id of the contract for which the workflow is launched
4792         p_hdesk_user_id     : fnd user id of the help desk user id setup in GCD. Optional,
4793                               if not passed will be derived from GCD.
4794 
4795     If no vendor/merchant contact bases on jtf object 'OKX_SALEPERS' can be found for the contract
4796     header, the help desk user is used. This behaviour is from R12 onwards, prior to this if a
4797     salesrep was not found, contract admin and then contract approver would be used.
4798     */
4799     PROCEDURE GET_USER_NAME
4800     (
4801      p_api_version IN NUMBER DEFAULT 1,
4802      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4803      x_return_status OUT NOCOPY VARCHAR2,
4804      x_msg_count OUT NOCOPY NUMBER,
4805      x_msg_data OUT NOCOPY VARCHAR2,
4806      p_chr_id IN NUMBER,
4807      p_hdesk_user_id IN NUMBER,
4808      x_user_id OUT NOCOPY NUMBER,
4809      x_user_name OUT NOCOPY VARCHAR2
4810     )
4811     IS
4812     l_api_name CONSTANT VARCHAR2(30) := 'GET_USER_NAME';
4813     l_api_version CONSTANT NUMBER := 1;
4814     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
4815     l_error_text VARCHAR2(512);
4816 
4817     --should be an outer join with party roles, so that
4818     --if we don't get a vendor/merchant party we atleast get an org
4819     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
4820         SELECT nvl(a.org_id, a.authoring_org_id), b.id
4821         FROM okc_k_headers_all_b a LEFT OUTER JOIN okc_k_party_roles_b b
4822         ON  a.id = b.dnz_chr_id
4823         AND b.cle_id IS NULL
4824         AND b.rle_code IN ('VENDOR', 'MERCHANT')
4825         WHERE a.id = cp_chr_id;
4826 
4827     CURSOR c_k_srep_user(cp_chr_id IN NUMBER, cp_cpl_id IN NUMBER, cp_org_id IN NUMBER) IS
4828         SELECT
4829         --rsc.resource_id, srp.salesrep_id, srp.org_id, ctc.cro_code,
4830         fnd.user_id, fnd.user_name
4831         FROM okc_contacts ctc,  fnd_user fnd,
4832             jtf_rs_resource_extns rsc, jtf_rs_salesreps srp
4833         WHERE ctc.dnz_chr_id = cp_chr_id
4834         AND ctc.cpl_id = cp_cpl_id
4835         AND ctc.cro_code IN (SELECT  src.cro_code FROM okc_contact_sources src
4836                                 WHERE src.rle_code IN ('VENDOR', 'MERCHANT')
4837                                 AND src.jtot_object_code = 'OKX_SALEPERS'
4838                                 AND src.buy_or_sell = 'S')
4839         AND srp.salesrep_id = to_number(ctc.object1_id1)
4840         AND nvl(srp.org_id, -99) = cp_org_id
4841         AND srp.resource_id = rsc.resource_id
4842         AND rsc.user_id = fnd.user_id;
4843 
4844     CURSOR c_fnd_user(cp_user_id IN NUMBER) IS
4845         SELECT user_name
4846         FROM fnd_user
4847         WHERE user_id = cp_user_id;
4848 
4849     l_org_id        NUMBER;
4850     l_cpl_id        NUMBER;
4851     l_user_id       NUMBER;
4852     l_user_name     VARCHAR2(100);
4853 
4854     l_rnrl_rec      OKS_RENEW_UTIL_PVT.rnrl_rec_type;
4855     x_rnrl_rec      OKS_RENEW_UTIL_PVT.rnrl_rec_type;
4856 
4857     BEGIN
4858 
4859         --log key input parameters
4860         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4861             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);
4862         END IF;
4863 
4864         --standard api initilization and checks
4865         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
4866             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4867         END IF;
4868         IF FND_API.to_boolean(p_init_msg_list ) THEN
4869             FND_MSG_PUB.initialize;
4870         END IF;
4871         x_return_status := FND_API.G_RET_STS_SUCCESS;
4872 
4873         --first get the contract org and id for the merchant/vendor record in okc_k_party_roles_b
4874         OPEN c_k_hdr(p_chr_id);
4875         FETCH c_k_hdr INTO l_org_id, l_cpl_id;
4876         CLOSE c_k_hdr;
4877 
4878         IF (l_org_id IS NULL) THEN
4879             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
4880             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
4881             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4882                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation', FALSE);
4883             END IF;
4884             FND_MSG_PUB.ADD;
4885             RAISE FND_API.g_exc_error;
4886         END IF;
4887 
4888         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4889             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);
4890         END IF;
4891 
4892         --now get the fnd user id/name for the contact of type 'OKX_SALEPERS', if a vendor/merchant party
4893         --is found
4894         IF (l_cpl_id IS NOT NULL) THEN
4895 
4896             OPEN c_k_srep_user(p_chr_id, l_cpl_id, l_org_id);
4897             FETCH c_k_srep_user INTO l_user_id, l_user_name;
4898             CLOSE c_k_srep_user;
4899 
4900             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4901                 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);
4902             END IF;
4903 
4904         END IF;
4905 
4906         --if no salesrep found, default to helpdesk user
4907         IF (l_user_id IS NULL) THEN
4908             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4909                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_helpdesk_user', 'no salesrep found in contract, getting the helpdesk user');
4910             END IF;
4911 
4912             IF (p_hdesk_user_id IS NOT NULL) THEN
4913                 l_user_id := p_hdesk_user_id;
4914             ElSE
4915                 --get the helpdesk user id from GCD
4916                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4917                     FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.calling_get_renew_rules', 'p_chr_id=' || p_chr_id ||', p_date='|| sysdate);
4918                 END IF;
4919 
4920                 OKS_RENEW_UTIL_PVT.get_renew_rules(
4921                                 x_return_status => x_return_status,
4922                                 p_api_version => 1.0,
4923                                 p_init_msg_list => FND_API.G_FALSE,
4924                                 p_chr_id => p_chr_id,
4925                                 p_party_id => NULL,
4926                                 p_org_id => NULL,
4927                                 p_date => sysdate,
4928                                 p_rnrl_rec => l_rnrl_rec,
4929                                 x_rnrl_rec => x_rnrl_rec,
4930                                 x_msg_count => x_msg_count,
4931                                 x_msg_data => x_msg_data);
4932 
4933                 IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4934                     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);
4935                 END IF;
4936                 IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
4937                     RAISE FND_API.g_exc_unexpected_error;
4938                 ELSIF x_return_status = FND_API.g_ret_sts_error THEN
4939                     RAISE FND_API.g_exc_error;
4940                 END IF;
4941 
4942                 l_user_id := x_rnrl_rec.user_id;
4943 
4944             END IF;
4945 
4946             --commented out, do not throw error if no helpdesk setup
4947             --so that renewal can continue
4948             /*
4949             IF (l_user_id IS NULL) THEN
4950                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NO_HELPDESK');
4951                 IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4952                     FND_LOG.message(FND_LOG.level_error, l_mod_name || '.get_helpdesk_user', FALSE);
4953                 END IF;
4954                 FND_MSG_PUB.ADD;
4955                 RAISE FND_API.g_exc_error;
4956             END IF;
4957             */
4958 
4959             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
4960                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.get_helpdesk_user', 'getting user name for user id='||l_user_id);
4961             END IF;
4962 
4963             OPEN c_fnd_user(l_user_id);
4964             FETCH c_fnd_user INTO l_user_name;
4965             CLOSE c_fnd_user;
4966         END IF;
4967         x_user_id := l_user_id;
4968         x_user_name := l_user_name;
4969 
4970 
4971         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
4972             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);
4973         END IF;
4974         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4975 
4976     EXCEPTION
4977         WHEN FND_API.g_exc_error THEN
4978             x_return_status := FND_API.g_ret_sts_error ;
4979 
4980             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
4981                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
4982             END IF;
4983             IF (c_k_hdr%isopen) THEN
4984                 CLOSE c_k_hdr;
4985             END IF;
4986             IF (c_k_srep_user%isopen) THEN
4987                 CLOSE c_k_srep_user;
4988             END IF;
4989             IF (c_fnd_user%isopen) THEN
4990                 CLOSE c_fnd_user;
4991             END IF;
4992             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
4993 
4994         WHEN FND_API.g_exc_unexpected_error THEN
4995             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4996 
4997             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
4998                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
4999             END IF;
5000             IF (c_k_hdr%isopen) THEN
5001                 CLOSE c_k_hdr;
5002             END IF;
5003             IF (c_k_srep_user%isopen) THEN
5004                 CLOSE c_k_srep_user;
5005             END IF;
5006             IF (c_fnd_user%isopen) THEN
5007                 CLOSE c_fnd_user;
5008             END IF;
5009             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5010 
5011         WHEN OTHERS THEN
5012             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5013 
5014             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
5015                 --first log the sqlerrm
5016                 l_error_text := substr (SQLERRM, 1, 240);
5017                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
5018                 --then add it to the message api list
5019                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
5020             END IF;
5021             IF (c_k_hdr%isopen) THEN
5022                 CLOSE c_k_hdr;
5023             END IF;
5024             IF (c_k_srep_user%isopen) THEN
5025                 CLOSE c_k_srep_user;
5026             END IF;
5027             IF (c_fnd_user%isopen) THEN
5028                 CLOSE c_fnd_user;
5029             END IF;
5030             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5031 
5032     END GET_USER_NAME;
5033 
5034 
5035     /*
5036 	From R12 onwards, this procedure should be used to renew service contracts.
5037     It will be redesigned to do the following
5038         1.	Improve performance
5039         2.	Reduce dependence on OKC code
5040         3.	Incorporate functional design changes for R12
5041         4.	Comply with current Oracle Applications coding and logging standards
5042         5.	Ease of maintenance
5043 
5044     Parameters
5045         p_chr_id                    :   id of the contract being renewed, mandatory
5046         p_new_contract_number       :   contract number for the renewed contract, optional
5047         p_new_contract_modifier     :   contract modifier for the renewed contract, optional
5048         p_new_start_date            :   start date for the renewed contract, optional
5049         p_new_end_date              :   end date for the renewed contract, optional
5050         p_new_duration              :   duration for renewed contract, optional
5051         p_new_uom_code              :   period for the renewed contract, optional
5052         p_renewal_called_from_ui    :  'Y' - called from UI, N - called from Events
5053         x_chr_id                :   id of the renewed contract
5054         x_return_status             :   S, E, U - standard values
5055 
5056     Defaulting rules
5057         1. If p_new_contract_number is not passed, uses the source contract_number
5058         2. If p_new_contract_modifier is not passed, generated this as
5059             fnd_profile.VALUE('OKC_CONTRACT_IDENTIFIER') || to_char(SYSDATE, 'DD-MON-YYYY HH24:MI:SS')
5060         3. If p_new_start_date is not passed, defaults to source contract end_date +1
5061         4. If p_new_end_date is not passed, derived from p_new_duration/p_new_uom_code
5062             and p_new_start_date. If p_new_duration/p_new_uom_code are also not passed
5063             used the source contract duration/period
5064     */
5065 
5066     PROCEDURE RENEW_CONTRACT
5067     (
5068      p_api_version IN NUMBER,
5069      p_init_msg_list IN VARCHAR2 DEFAULT FND_API.G_FALSE,
5070      p_commit   IN VARCHAR2 DEFAULT FND_API.G_FALSE,
5071      p_chr_id IN NUMBER,
5072      p_new_contract_number IN okc_k_headers_b.contract_number%TYPE,
5073      p_new_contract_modifier IN okc_k_headers_b.contract_number_modifier%TYPE,
5074      p_new_start_date IN DATE,
5075      p_new_end_date IN DATE,
5076      p_new_duration IN NUMBER,
5077      p_new_uom_code IN MTL_UNITS_OF_MEASURE_TL.uom_code%TYPE,
5078      p_renewal_called_from_ui IN VARCHAR2 DEFAULT 'Y',
5079      x_chr_id OUT NOCOPY NUMBER,
5080      x_msg_count OUT NOCOPY NUMBER,
5081      x_msg_data OUT NOCOPY VARCHAR2,
5082      x_return_status OUT NOCOPY VARCHAR2
5083      )
5084     IS
5085     l_api_name CONSTANT VARCHAR2(30) := 'RENEW_CONTRACT';
5086     l_api_version CONSTANT NUMBER := 1;
5087     l_mod_name VARCHAR2(256) := lower(G_OKS_APP_NAME) || '.plsql.' || G_PKG_NAME || '.' || l_api_name;
5088     l_error_text VARCHAR2(512);
5089 
5090     --also check if it is a service contract
5091     CURSOR c_k_hdr(cp_chr_id IN NUMBER) IS
5092         SELECT contract_number, contract_number_modifier, start_date, end_date,
5093         renewal_type_code, renewal_end_date, currency_code
5094         FROM okc_k_headers_all_b
5095         WHERE id = cp_chr_id AND application_id = 515;
5096 
5097     CURSOR c_renk_hdr(cp_chr_id IN NUMBER) IS
5098         SELECT currency_code, org_id
5099         FROM okc_k_headers_all_b WHERE id = cp_chr_id;
5100 
5101 
5102     l_k_num                 okc_k_headers_b.contract_number%TYPE;
5103     l_k_mod                 okc_k_headers_b.contract_number_modifier%TYPE;
5104     l_k_start_date          DATE;
5105     l_k_end_date            DATE;
5106     l_k_ren_type            okc_k_headers_b.renewal_type_code%TYPE;
5107     l_k_renewal_end_date    DATE;
5108     l_k_currency_code       VARCHAR2(15);
5109 
5110     l_validation_level      VARCHAR2(1);
5111     l_rnrl_rec              OKS_RENEW_UTIL_PVT.rnrl_rec_type;
5112     l_rnrl_rec_dummy        OKS_RENEW_UTIL_PVT.rnrl_rec_type;
5113     l_validation_status     VARCHAR2(1);
5114     l_validation_tbl        validation_tbl_type;
5115 
5116     l_renk_num              okc_k_headers_b.contract_number%TYPE;
5117     l_renk_mod              okc_k_headers_b.contract_number_modifier%TYPE;
5118     l_renk_start_date       DATE;
5119     l_renk_end_date         DATE;
5120     l_renk_currency_code    VARCHAR2(15);
5121     l_renk_org_id           NUMBER;
5122 
5123     l_user_id               NUMBER;
5124     l_user_name             VARCHAR2(100);
5125     l_renewal_type          VARCHAR2(30);
5126     l_approval_type         VARCHAR2(30);
5127     l_warnings              BOOLEAN := FALSE;
5128 
5129     BEGIN
5130         --log key input parameters
5131         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
5132             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
5133                 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||
5134                 ' ,p_new_contract_modifier='||p_new_contract_modifier||' ,p_new_start_date='||p_new_start_date||' ,p_new_end_date='||p_new_end_date||
5135                 ' ,p_new_duration='||p_new_duration||' ,p_new_uom_code='||p_new_uom_code||' ,p_renewal_called_from_ui='||p_renewal_called_from_ui);
5136             END IF;
5137         END IF;
5138 
5139 	 -- Put the parameters in the log file
5140          fnd_file.put_line(FND_FILE.LOG,'  ');
5141          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5142          fnd_file.put_line(FND_FILE.LOG,'Calling OKS_RENEW_CONTRACT_PVT.RENEW_CONTRACT');
5143          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5144          fnd_file.put_line(FND_FILE.LOG,'Parameters  ');
5145          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5146          fnd_file.put_line(FND_FILE.LOG,'p_api_version :  '||p_api_version);
5147          fnd_file.put_line(FND_FILE.LOG,'p_init_msg_list :  '||p_init_msg_list);
5148          fnd_file.put_line(FND_FILE.LOG,'p_commit :  '||p_commit);
5149          fnd_file.put_line(FND_FILE.LOG,'p_chr_id :  '||p_chr_id);
5150          fnd_file.put_line(FND_FILE.LOG,'p_new_contract_number :  '||p_new_contract_number);
5151          fnd_file.put_line(FND_FILE.LOG,'p_new_contract_modifier :  '||p_new_contract_modifier);
5152          fnd_file.put_line(FND_FILE.LOG,'p_new_start_date :  '||p_new_start_date);
5153          fnd_file.put_line(FND_FILE.LOG,'p_new_end_date :  '||p_new_end_date);
5154          fnd_file.put_line(FND_FILE.LOG,'p_new_duration :  '||p_new_duration);
5155          fnd_file.put_line(FND_FILE.LOG,'p_new_uom_code :  '||p_new_uom_code);
5156          fnd_file.put_line(FND_FILE.LOG,'p_renewal_called_from_ui :  '||p_renewal_called_from_ui);
5157          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5158          fnd_file.put_line(FND_FILE.LOG,'  ');
5159 
5160 
5161         --standard api initilization and checks
5162         SAVEPOINT renew_contract_PVT;
5163         IF NOT FND_API.compatible_api_call (l_api_version, p_api_version, l_api_name, G_PKG_NAME)THEN
5164             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5165         END IF;
5166         IF FND_API.to_boolean(p_init_msg_list ) THEN
5167             FND_MSG_PUB.initialize;
5168         END IF;
5169         x_return_status := FND_API.G_RET_STS_SUCCESS;
5170 
5171 	    /*
5172 	        Step 1: do basic parameter validation
5173 	    */
5174 
5175          fnd_file.put_line(FND_FILE.LOG,'  ');
5176          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5177          fnd_file.put_line(FND_FILE.LOG,'Step 1: do basic parameter validation  ');
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,'----------------------------------------------------------  ');
5180          fnd_file.put_line(FND_FILE.LOG,'  ');
5181 
5182         --Step 1 do basic parameter validation
5183         --first get the basic contract attributes
5184         OPEN c_k_hdr(p_chr_id);
5185         FETCH c_k_hdr INTO l_k_num, l_k_mod, l_k_start_date, l_k_end_date, l_k_ren_type,
5186         l_k_renewal_end_date, l_k_currency_code;
5187 
5188         --invalid contract id or if it's not a service contract
5189         IF (c_k_hdr%notfound) THEN
5190             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INV_CONTRACT');
5191             FND_MESSAGE.set_token('CONTRACT_ID', p_chr_id);
5192             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5193                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation1', FALSE);
5194             END IF;
5195             FND_MSG_PUB.ADD;
5196             CLOSE c_k_hdr;
5197             RAISE FND_API.g_exc_error;
5198         END IF;
5199         CLOSE c_k_hdr;
5200 
5201         --new start date < original end date
5202         IF (p_new_start_date IS NOT NULL) AND (p_new_start_date < l_k_end_date) THEN
5203             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_NEW_START_MORE_END');
5204             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5205                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation2', FALSE);
5206             END IF;
5207             FND_MSG_PUB.ADD;
5208             RAISE FND_API.g_exc_error;
5209         END IF;
5210 
5211         --new end date < new start date, if new start date is null use old end date + 1
5212         IF (p_new_end_date IS NOT NULL) AND (p_new_end_date < nvl(p_new_start_date, l_k_end_date + 1)) THEN
5213             FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_INVALID_END_DATE');
5214             FND_MESSAGE.set_token('START_DATE', to_char(nvl(p_new_start_date, l_k_end_date + 1)));
5215             FND_MESSAGE.set_token('END_DATE', to_char(p_new_end_date));
5216             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5217                 FND_LOG.message(FND_LOG.level_error, l_mod_name || '.basic_validation3', FALSE);
5218             END IF;
5219             FND_MSG_PUB.ADD;
5220             RAISE FND_API.g_exc_error;
5221         END IF;
5222         --end basic parameter validation
5223 
5224 	    /*
5225 	        Step 2: do renewal validation and at the same time fetch the renewal rules
5226 	    */
5227 
5228          fnd_file.put_line(FND_FILE.LOG,'  ');
5229          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5230          fnd_file.put_line(FND_FILE.LOG,'Step 2: do renewal validation and at the same time fetch the renewal rules  ');
5231          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5232          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5233          fnd_file.put_line(FND_FILE.LOG,'  ');
5234 
5235         --Step 2 do renewal validation and at the same time fetch the renewal rules
5236         --if called from UI, then user has ready seen the warnings, so we check only for errors
5237         --if called from Events, we need to check for both errors and warnings
5238         IF (p_renewal_called_from_ui = 'Y') THEN
5239             l_validation_level := G_VALIDATE_ERRORS;
5240         ELSE
5241             l_validation_level := G_VALIDATE_ALL;
5242         END IF;
5243 
5244         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5245             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))||
5246             ' ,p_validation_level='||l_validation_level);
5247         END IF;
5248 
5249         validate_renewal(
5250             p_api_version =>  1,
5251             p_init_msg_list => FND_API.G_FALSE,
5252             x_return_status => x_return_status,
5253             x_msg_count => x_msg_count,
5254             x_msg_data => x_msg_data,
5255             p_chr_id => p_chr_id,
5256             p_date => nvl(p_new_start_date, l_k_end_date + 1),
5257             p_validation_level => l_validation_level,
5258             x_rnrl_rec => l_rnrl_rec,
5259             x_validation_status => l_validation_status,
5260             x_validation_tbl => l_validation_tbl);
5261 
5262         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5263             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);
5264         END IF;
5265 
5266         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5267             RAISE FND_API.g_exc_unexpected_error;
5268         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5269             RAISE FND_API.g_exc_error;
5270         END IF;
5271 
5272         --if validation errors - stop
5273         --if validation warnings - stop if called from events
5274         IF (    (l_validation_status = G_VALID_STS_ERROR) OR
5275                 (   (l_validation_status = G_VALID_STS_WARNING) AND
5276                 (p_renewal_called_from_ui = 'N')    )
5277         ) THEN
5278             --add all validation messages to the FND_MSG_PUB stack
5279             FOR i in l_validation_tbl.FIRST..l_validation_tbl.LAST LOOP
5280                 --OKS_USER_DEFINED_MESSAGE is a special message, with the message body = MESSAGE
5281                 --This is a workaround, because we can't directly add messages to the msg API list
5282                 --using FND_MSG_PUB.add. FND_MSG_PUB.add expects messages on the message stack (FND_MESSAGE.set_name)
5283                 FND_MESSAGE.set_name(G_OKS_APP_NAME, 'OKS_USER_DEFINED_MESSAGE');
5284                 FND_MESSAGE.set_token('MESSAGE', l_validation_tbl(i).message);
5285                 FND_MSG_PUB.add;
5286             END LOOP;
5287             RAISE FND_API.g_exc_error;
5288         END IF;
5289         --end renewal validation
5290 
5291 	    /*
5292 	        Step 3: default attributes
5293 	    */
5294 
5295          fnd_file.put_line(FND_FILE.LOG,'  ');
5296          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5297          fnd_file.put_line(FND_FILE.LOG,'Step 3: default attributes ');
5298          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5299          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5300          fnd_file.put_line(FND_FILE.LOG,'  ');
5301 
5302         --Step 3 default attributes
5303         l_renk_num := nvl(p_new_contract_number, l_k_num);
5304         l_renk_mod := nvl(p_new_contract_modifier, fnd_profile.VALUE('OKC_CONTRACT_IDENTIFIER') || to_char(SYSDATE, 'DD-MON-YYYY HH24:MI:SS'));
5305         l_renk_start_date := trunc(nvl(p_new_start_date, l_k_end_date + 1));
5306         l_renk_end_date := get_end_date(
5307                             p_new_start_date => l_renk_start_date,
5308                             p_new_end_date => p_new_end_date,
5309                             p_new_duration => p_new_duration,
5310                             p_new_uom_code => p_new_uom_code,
5311                             p_old_start_date => l_k_start_date,
5312                             p_old_end_date => l_k_end_date,
5313                             p_renewal_end_date => l_k_renewal_end_date,
5314                             p_ren_type => l_k_ren_type,
5315                             x_return_status => x_return_status);
5316 
5317         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5318             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)||
5319             ' ,l_renk_end_date='||to_char(l_renk_end_date)||' ,x_return_status='||x_return_status);
5320         END IF;
5321         --end default attributes
5322 
5323 	    /*
5324 	        Step 4: copy contract
5325 	    */
5326 
5327          fnd_file.put_line(FND_FILE.LOG,'  ');
5328          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5329          fnd_file.put_line(FND_FILE.LOG,'Step 4: copy contract ');
5330          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5331          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5332          fnd_file.put_line(FND_FILE.LOG,'  ');
5333 
5334         --Step 4 copy contract
5335         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5336             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||
5337             ' ,p_to_template_yn=N, p_renew_ref_yn=Y, p_override_org=Y, p_copy_lines_yn=Y ,p_commit=F');
5338         END IF;
5339 
5340         OKS_COPY_CONTRACT_PVT.copy_contract(
5341             p_api_version  => 1,
5342             p_init_msg_list => FND_API.G_FALSE,
5343             x_return_status => x_return_status,
5344             x_msg_count => x_msg_count,
5345             x_msg_data => x_msg_data,
5346             p_commit => FND_API.G_FALSE,
5347             p_chr_id => p_chr_id,
5348             p_contract_number => l_renk_num,
5349             p_contract_number_modifier => l_renk_mod,
5350             p_to_template_yn => 'N',
5351             P_renew_ref_yn => 'Y',
5352             x_to_chr_id => x_chr_id);
5353 
5354         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5355             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);
5356         END IF;
5357 
5358         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5359             RAISE FND_API.g_exc_unexpected_error;
5360         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5361             RAISE FND_API.g_exc_error;
5362         END IF;
5363 
5364         --sales credit and copy are the 2 places we can return with a warning
5365         IF x_return_status = OKC_API.g_ret_sts_warning THEN -- 'W'
5366             l_warnings := TRUE;
5367         END IF;
5368         --end copy contract
5369 
5370 	    /*
5371 	        Step 5: if the renewed contract currency is different from original contract currency
5372 	    */
5373 
5374          fnd_file.put_line(FND_FILE.LOG,'  ');
5375          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5376          fnd_file.put_line(FND_FILE.LOG,'Step 5:if the renewed contract currency is different');
5377          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5378          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5379          fnd_file.put_line(FND_FILE.LOG,'  ');
5380 
5381         --Step 5 if the renewed contract currency is different from original contract currency
5382         --get the renewal rules again, as thresholds depend on currency
5383         OPEN c_renk_hdr(x_chr_id);
5384         FETCH c_renk_hdr INTO l_renk_currency_code, l_renk_org_id;
5385         CLOSE c_renk_hdr;
5386 
5387         IF (l_renk_currency_code <> l_k_currency_code) THEN
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 || '.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);
5391             END IF;
5392 
5393             OKS_RENEW_UTIL_PVT.get_renew_rules(
5394                             x_return_status => x_return_status,
5395                             p_api_version => 1.0,
5396                             p_init_msg_list => FND_API.G_FALSE,
5397                             p_chr_id => x_chr_id,
5398                             p_party_id => NULL,
5399                             p_org_id => NULL,
5400                             p_date => l_renk_start_date,
5401                             p_rnrl_rec => l_rnrl_rec_dummy,
5402                             x_rnrl_rec => l_rnrl_rec,
5403                             x_msg_count => x_msg_count,
5404                             x_msg_data => x_msg_data);
5405 
5406             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5407                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.after_get_renew_rules', 'x_return_status=' || x_return_status);
5408             END IF;
5409             IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5410                 RAISE FND_API.g_exc_unexpected_error;
5411             ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5412                 RAISE FND_API.g_exc_error;
5413             END IF;
5414 
5415         END IF;
5416         --end of currency/renewal rules check
5417 
5418 	    /*
5419 	        Step 6: adjust the header and line dates
5420 	    */
5421 
5422          fnd_file.put_line(FND_FILE.LOG,'  ');
5423          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5424          fnd_file.put_line(FND_FILE.LOG,'Step 6 : adjust the header and line dates');
5425          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5426          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5427          fnd_file.put_line(FND_FILE.LOG,'  ');
5428 
5429         --Step 6 adjust the header and line dates
5430         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5431             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||
5432             ' ,p_new_end_date='||l_renk_end_date||' ,p_old_start_date='||l_k_start_date);
5433         END IF;
5434         update_renewal_dates(
5435             p_chr_id  => x_chr_id,
5436             p_new_start_date => l_renk_start_date,
5437             p_new_end_date => l_renk_end_date,
5438             p_old_start_date => l_k_start_date,
5439             x_msg_count => x_msg_count,
5440             x_msg_data => x_msg_data,
5441             x_return_status => x_return_status);
5442 
5443         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5444             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_dates', 'after  update_renewal_dates, x_return_status='||x_return_status);
5445         END IF;
5446         --end of adjust dates
5447 
5448  	    /*
5449 	        Step 6.1 : Update annualized_factor for the renewed contract lines
5450 	    */
5451 
5452          fnd_file.put_line(FND_FILE.LOG,'  ');
5453          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5454          fnd_file.put_line(FND_FILE.LOG,'Step 6.1 : Update annualized_factor for the renewed contract lines');
5455          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5456          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5457          fnd_file.put_line(FND_FILE.LOG,'  ');
5458 
5459         -- Step 6.1 Update annualized_factor for the renewed contract lines
5460         -- bug 4768227
5461         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5462             FND_LOG.string(FND_LOG.level_statement, l_mod_name , ' calling  update to annualized_factor , p_new_chr_id='||x_chr_id);
5463         END IF;
5464 
5465           UPDATE okc_k_lines_b
5466              SET annualized_factor = OKS_SETUP_UTIL_PUB.Get_Annualized_Factor(start_date, end_date, lse_id)
5467            WHERE dnz_chr_id = x_chr_id;
5468 
5469         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5470             FND_LOG.string(FND_LOG.level_statement, l_mod_name , ' After calling  update to annualized_factor , p_new_chr_id='||x_chr_id);
5471         END IF;
5472 
5473 	    /*
5474 	        Step 7: update the old contract's date renewed column for the lines that are actually renewed
5475 	    */
5476 
5477          fnd_file.put_line(FND_FILE.LOG,'  ');
5478          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5479          fnd_file.put_line(FND_FILE.LOG,'Step 7 : update the old contract date renewed column for the lines that are actually renewed');
5480          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5481          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5482          fnd_file.put_line(FND_FILE.LOG,'  ');
5483 
5484         --Step 7 update the old contract's date renewed column for the lines that are actually renewed
5485         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5486             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);
5487         END IF;
5488         update_source_contract(
5489             p_new_chr_id  => x_chr_id,
5490             p_old_chr_id => p_chr_id,
5491             x_return_status => x_return_status);
5492 
5493         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5494             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_source_contract', 'after  update_source_contract, x_return_status='||x_return_status);
5495         END IF;
5496         --end of adjust date
5497 
5498 	    /*
5499 	        Step 8: adjust the invoice text that is based on the line dates
5500 	    */
5501 
5502          fnd_file.put_line(FND_FILE.LOG,'  ');
5503          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5504          fnd_file.put_line(FND_FILE.LOG,'Step 8 : adjust the invoice text that is based on the line dates');
5505          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5506          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5507          fnd_file.put_line(FND_FILE.LOG,'  ');
5508 
5509         --now adjust all date dependent entities
5510         --Step 8 adjust the invoice text that is based on the line dates
5511         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5512             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.invoice_text', 'calling  update_invoice_text, p_chr_id='||x_chr_id);
5513         END IF;
5514         update_invoice_text(
5515             p_api_version => 1,
5516             p_init_msg_list => FND_API.G_FALSE,
5517             p_commit => FND_API.G_FALSE,
5518             x_return_status => x_return_status,
5519             x_msg_count => x_msg_count,
5520             x_msg_data => x_msg_data,
5521             p_chr_id  => x_chr_id);
5522 
5523         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5524             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.invoice_text', 'after  update_invoice_text, x_return_status='||x_return_status);
5525         END IF;
5526         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5527             RAISE FND_API.g_exc_unexpected_error;
5528         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5529             RAISE FND_API.g_exc_error;
5530         END IF;
5531         --end of invoice text
5532 
5533         /*
5534          bug 4775295 : Commented call to procedure update_condition_headers
5535 
5536         --Step 9 update contract condition(event) headers
5537         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5538             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
5539             ||' ,p_old_start_date='||l_k_start_date||' ,p_old_end_date='||l_k_end_date);
5540         END IF;
5541 
5542         update_condition_headers(
5543             p_chr_id => x_chr_id,
5544             p_new_start_date => l_renk_start_date,
5545             p_new_end_date => l_renk_end_date,
5546             p_old_start_date => l_k_start_date,
5547             p_old_end_date => l_k_end_date,
5548             x_return_status => x_return_status);
5549 
5550         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5551             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.condition_header', 'after  update_condition_headers, x_return_status='||x_return_status);
5552         END IF;
5553         --end of contract condition(event) headers
5554 
5555          */
5556 
5557 	    /*
5558 	        Step 10: adjust the header and line dates
5559 	    */
5560 
5561          fnd_file.put_line(FND_FILE.LOG,'  ');
5562          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5563          fnd_file.put_line(FND_FILE.LOG,'Step 10 : Regenerate subscription schedule/details and coverage entities based on the new dates');
5564          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5565          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5566          fnd_file.put_line(FND_FILE.LOG,'  ');
5567 
5568         --Step 10 Regenerate subscription schedule/details and coverage entities based on the new dates
5569         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5570             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.coverage_subscription', 'calling  recreate_cov_subscr, p_chr_id='||x_chr_id);
5571         END IF;
5572         recreate_cov_subscr(
5573             p_chr_id => x_chr_id,
5574             x_msg_count => x_msg_count,
5575             x_msg_data => x_msg_data,
5576             x_return_status => x_return_status);
5577 
5578         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5579             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.coverage_subscription', 'after  call to recreate_cov_subscr, x_return_status='||x_return_status);
5580         END IF;
5581         --end of coverage/subscription recreation
5582 
5583 	    /*
5584 	        Step 11: adjust the header and line dates
5585 	    */
5586 
5587          fnd_file.put_line(FND_FILE.LOG,'  ');
5588          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5589          fnd_file.put_line(FND_FILE.LOG,'Step 11 : Call pricing API to reprice the contract based on new dates');
5590          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5591          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5592          fnd_file.put_line(FND_FILE.LOG,'  ');
5593 
5594         --Step 11 Call pricing API to reprice the contract based on new dates
5595         --and renewal pricing rules. This will also rollup price/tax values at the topline and header
5596         --level and stamp the pricelist on the lines.
5597         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5598             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
5599             ||' ,p_markup_percent='||l_rnrl_rec.markup_percent);
5600         END IF;
5601 
5602         reprice_contract(
5603             p_chr_id  => x_chr_id,
5604             p_price_method => l_rnrl_rec.renewal_pricing_type,
5605             p_price_list_id => l_rnrl_rec.price_list_id1,
5606             p_markup_percent => l_rnrl_rec.markup_percent,
5607             x_msg_count => x_msg_count,
5608             x_msg_data => x_msg_data,
5609             x_return_status => x_return_status);
5610 
5611         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5612             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.repricing', 'after  call to reprice_contract, x_return_status='||x_return_status);
5613         END IF;
5614         --end of repricing
5615 
5616 	    /*
5617 	        Step 12 : copy usage price locks if any
5618 	    */
5619 
5620          fnd_file.put_line(FND_FILE.LOG,'  ');
5621          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5622          fnd_file.put_line(FND_FILE.LOG,'Step 12 : copy usage price locks if any');
5623          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5624          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5625          fnd_file.put_line(FND_FILE.LOG,'  ');
5626 
5627         --Step 12 copy usage price locks if any
5628         --Can be done only after the line pricelist has been updated (in reprice_contract)
5629         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5630             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);
5631         END IF;
5632         copy_usage_price_locks(
5633             p_chr_id  => x_chr_id,
5634             p_org_id => l_renk_org_id,
5635             p_contract_number => l_renk_num,
5636             x_msg_count => x_msg_count,
5637             x_msg_data => x_msg_data,
5638             x_return_status => x_return_status);
5639 
5640         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5641             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);
5642         END IF;
5643         --end of copy usage price locks
5644 
5645 
5646 	    /*
5647 	        Step 13: adjust the header and line dates
5648 	    */
5649 
5650          fnd_file.put_line(FND_FILE.LOG,'  ');
5651          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5652          fnd_file.put_line(FND_FILE.LOG,'Step 13 : Recreate billing schedules for the lines/header');
5653          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5654          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5655          fnd_file.put_line(FND_FILE.LOG,'  ');
5656 
5657         --Step 13 Recreate billing schedules for the lines/header.
5658         --Can be done only after dates adjustment and repricing the contract
5659         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5660             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.billing', 'calling  recreate_billing, p_chr_id='||x_chr_id);
5661         END IF;
5662         recreate_billing(
5663             p_chr_id  => x_chr_id,
5664             p_billing_profile_id => l_rnrl_rec.billing_profile_id,
5665             x_msg_count => x_msg_count,
5666             x_msg_data => x_msg_data,
5667             x_return_status => x_return_status);
5668         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5669             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.billing', 'after  call to recreate_billing, x_return_status='||x_return_status);
5670         END IF;
5671         --end of billing
5672 
5673 	    /*
5674 	        Step 14 : Process Sales credits
5675 	    */
5676 
5677          fnd_file.put_line(FND_FILE.LOG,'  ');
5678          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5679          fnd_file.put_line(FND_FILE.LOG,'Step 14 : Process Sales credits');
5680          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5681          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5682          fnd_file.put_line(FND_FILE.LOG,'  ');
5683 
5684         --Step 14 Process Sales credits
5685         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5686             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.sales_credits', 'calling  process_sales_credit, p_chr_id='||x_chr_id);
5687         END IF;
5688 
5689         fnd_file.put_line(FND_FILE.LOG,'Calling process_sales_credit ');
5690 
5691         process_sales_credit(
5692             p_chr_id  => x_chr_id,
5693             x_msg_count => x_msg_count,
5694             x_msg_data => x_msg_data,
5695             x_return_status => x_return_status);
5696 
5697         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5698             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.sales_credits', 'after  call to process_sales_credit, x_return_status='||x_return_status);
5699         END IF;
5700 
5701         --sales credit and copy are the 2 places we can return with a warning
5702         IF x_return_status = OKC_API.g_ret_sts_warning THEN -- 'W'
5703             l_warnings := TRUE;
5704         END IF;
5705         --end of sales credits
5706 
5707 	    /*
5708 	        Step 15 : get the user id and name (salesperson) of the contact
5709 	    */
5710 
5711          fnd_file.put_line(FND_FILE.LOG,'  ');
5712          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5713          fnd_file.put_line(FND_FILE.LOG,'Step 15 : get the user id and name (salesperson) of the contact');
5714          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5715          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5716          fnd_file.put_line(FND_FILE.LOG,'  ');
5717 
5718         --Step 15 get the user id and name (salesperson) of the contact who will be
5719         --the performer for the workflow. Can be done only after 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 || '.get_user_name', 'calling  get_user_name, p_chr_id='||x_chr_id||' ,p_hdesk_user_id='||l_rnrl_rec.user_id);
5722         END IF;
5723 
5724         get_user_name(
5725             p_api_version =>  1,
5726             p_init_msg_list => FND_API.G_FALSE,
5727             x_return_status => x_return_status,
5728             x_msg_count => x_msg_count,
5729             x_msg_data => x_msg_data,
5730             p_chr_id => x_chr_id,
5731             p_hdesk_user_id => l_rnrl_rec.user_id,
5732             x_user_id => l_user_id,
5733             x_user_name => l_user_name);
5734 
5735         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5736             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);
5737         END IF;
5738         IF x_return_status = FND_API.g_ret_sts_unexp_error THEN
5739             RAISE FND_API.g_exc_unexpected_error;
5740         ELSIF x_return_status = FND_API.g_ret_sts_error THEN
5741             RAISE FND_API.g_exc_error;
5742         END IF;
5743         --end of get user name
5744 
5745 	    /*
5746 	        Step 16: check and assign contract to contract group specified in GCD
5747 	    */
5748 
5749          fnd_file.put_line(FND_FILE.LOG,'  ');
5750          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5751          fnd_file.put_line(FND_FILE.LOG,'Step 16 : check and assign contract to contract group specified in GCD');
5752          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5753          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5754          fnd_file.put_line(FND_FILE.LOG,'  ');
5755 
5756         --Step 16 check and assign contract to contract group specified in GCD
5757         IF( l_rnrl_rec.cgp_renew_id IS NOT NULL) THEN
5758             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5759                 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);
5760             END IF;
5761             assign_contract_group(
5762                 p_chr_id  => x_chr_id,
5763                 p_chr_group_id => l_rnrl_rec.cgp_renew_id,
5764                 x_msg_count => x_msg_count,
5765                 x_msg_data => x_msg_data,
5766                 x_return_status => x_return_status);
5767             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5768                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.contract_group', 'after  call to assign_contract_group, x_return_status='||x_return_status);
5769             END IF;
5770         END IF;
5771         --end of contract group
5772 	    /*
5773 	        Step 17: check and update/create contract approval process specified in GCD
5774 	    */
5775 
5776          fnd_file.put_line(FND_FILE.LOG,'  ');
5777          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5778          fnd_file.put_line(FND_FILE.LOG,'Step 17 : check and update/create contract approval process specified in GCD');
5779          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5780          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5781          fnd_file.put_line(FND_FILE.LOG,'  ');
5782 
5783         --Step 17 check and update/create contract approval process specified in GCD
5784         IF( l_rnrl_rec.pdf_id IS NOT NULL) THEN
5785             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5786                 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);
5787             END IF;
5788             assign_contract_process(
5789                 p_chr_id  => x_chr_id,
5790                 p_pdf_id => l_rnrl_rec.pdf_id,
5791                 x_msg_count => x_msg_count,
5792                 x_msg_data => x_msg_data,
5793                 x_return_status => x_return_status);
5794             IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5795                 FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.contract_process', 'after  call to assign_contract_process, x_return_status='||x_return_status);
5796             END IF;
5797         END IF;
5798         --end of contract approval process
5799 
5800 	    /*
5801 	        Step 18: update contract (OKC and OKS) with the renewal rules
5802 	    */
5803 
5804          fnd_file.put_line(FND_FILE.LOG,'  ');
5805          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5806          fnd_file.put_line(FND_FILE.LOG,'Step 18 : update contract (OKC and OKS) with the renewal rules');
5807          fnd_file.put_line(FND_FILE.LOG,'Start Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5808          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5809          fnd_file.put_line(FND_FILE.LOG,'  ');
5810 
5811         --Step 18 update contract (OKC and OKS) with the renewal rules, inlcuding determination
5812         --renewal type and launching/modification of workflow
5813         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5814             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);
5815         END IF;
5816         update_renewed_contract(
5817             p_chr_id  => x_chr_id,
5818             p_rnrl_rec => l_rnrl_rec,
5819             p_notify_to => l_user_id,
5820             x_msg_count => x_msg_count,
5821             x_msg_data => x_msg_data,
5822             x_return_status => x_return_status);
5823         IF (FND_LOG.level_statement >= FND_LOG.g_current_runtime_level) THEN
5824             FND_LOG.string(FND_LOG.level_statement, l_mod_name || '.update_contract', 'after  call to update_renewed_contract, x_return_status='||x_return_status);
5825         END IF;
5826         --end of update contract
5827 
5828         IF (l_warnings) THEN
5829            x_return_status :=  OKC_API.G_RET_STS_WARNING;
5830         END IF;
5831 
5832         --standard check of p_commit
5833 	    IF FND_API.to_boolean( p_commit ) THEN
5834 		    COMMIT;
5835 	    END IF;
5836 
5837          fnd_file.put_line(FND_FILE.LOG,'  ');
5838          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5839          fnd_file.put_line(FND_FILE.LOG,'Completed Calling OKS_RENEW_CONTRACT_PVT.RENEW_CONTRACT');
5840          fnd_file.put_line(FND_FILE.LOG,'End Time : '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SSSS'));
5841          fnd_file.put_line(FND_FILE.LOG,'----------------------------------------------------------  ');
5842          fnd_file.put_line(FND_FILE.LOG,'  ');
5843 
5844         IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
5845             IF (FND_LOG.test(FND_LOG.level_procedure, l_mod_name)) THEN
5846                 FND_LOG.string(FND_LOG.level_procedure, l_mod_name || '.end', 'x_chr_id=' || x_chr_id ||', x_return_status='|| x_return_status);
5847             END IF;
5848         END IF;
5849         FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5850 
5851     EXCEPTION
5852         WHEN FND_API.g_exc_error THEN
5853             ROLLBACK TO renew_contract_PVT;
5854             x_return_status := FND_API.g_ret_sts_error ;
5855 
5856             IF (FND_LOG.level_error >= FND_LOG.g_current_runtime_level) THEN
5857                 FND_LOG.string(FND_LOG.level_error, l_mod_name || '.end_error', 'x_return_status=' || x_return_status);
5858             END IF;
5859             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5860             IF (c_k_hdr%isopen) THEN
5861                 CLOSE c_k_hdr;
5862             END IF;
5863             IF (c_renk_hdr%isopen) THEN
5864                 CLOSE c_renk_hdr;
5865             END IF;
5866 
5867         WHEN FND_API.g_exc_unexpected_error THEN
5868             ROLLBACK TO renew_contract_PVT;
5869             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5870 
5871             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
5872                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_unexpected_error', 'x_return_status=' || x_return_status);
5873             END IF;
5874             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5875             IF (c_k_hdr%isopen) THEN
5876                 CLOSE c_k_hdr;
5877             END IF;
5878             IF (c_renk_hdr%isopen) THEN
5879                 CLOSE c_renk_hdr;
5880             END IF;
5881 
5882         WHEN OTHERS THEN
5883             ROLLBACK TO renew_contract_PVT;
5884             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5885 
5886             IF (FND_LOG.level_unexpected >= FND_LOG.g_current_runtime_level) THEN
5887                 --first log the sqlerrm
5888                 l_error_text := substr (SQLERRM, 1, 240);
5889                 FND_LOG.string(FND_LOG.level_unexpected, l_mod_name || '.end_other_error', l_error_text);
5890                 --then add it to the message api list
5891                 FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name, l_error_text);
5892             END IF;
5893             FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data );
5894             IF (c_k_hdr%isopen) THEN
5895                 CLOSE c_k_hdr;
5896             END IF;
5897             IF (c_renk_hdr%isopen) THEN
5898                 CLOSE c_renk_hdr;
5899             END IF;
5900 
5901     END RENEW_CONTRACT;
5902 
5903 
5904 END OKS_RENEW_CONTRACT_PVT;