DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKC_VERSION_PVT

Source


1 package body okc_version_pvt as
2 /* $Header: OKCRVERB.pls 120.2.12000000.2 2007/02/06 12:51:35 skgoud ship $ */
3 
4 -- Bug# 1553916
5 -- Private procedure for taking care of attachments
6 -- 1. Version a Contract in any Status
7 -- 2. Saving a Contract as opening a contract for update in 'Active' Status
8 -- 3. Restore back to an old Version
9 --
10 
11 PROCEDURE version_attachments(
12     p_chr_id 			   IN NUMBER,
13     p_action                       IN VARCHAR2,
14     p_major_version                IN NUMBER,
15     p_api_version                  IN NUMBER,
16     p_init_msg_list                IN VARCHAR2 DEFAULT OKC_API.G_FALSE,
17     x_return_status                OUT NOCOPY VARCHAR2,
18     x_msg_count                    OUT NOCOPY NUMBER,
19     x_msg_data                     OUT NOCOPY VARCHAR2) IS
20     --
21     --
22     l_api_version                  CONSTANT NUMBER := 1;
23     l_api_name                     CONSTANT VARCHAR2(30) := 'V_version_attachments';
24     l_return_status                VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
25     l_major_version		   number(4);
26     l_minor_version		   number ;
27     l_minus_version		   number := -1;
28     l_rest_to_major_ver		   number(4);
29     l_curr_major_ver               number(4);
30     l_ver_num                      number; -- Counter variable
31     --
32     --
33   cursor c_major_ver is
34     select major_version
35       from okc_k_vers_numbers
36      where chr_id = p_chr_id;
37   --
38   cursor c_lines is
39     select id
40       from okc_k_lines_b
41      where dnz_chr_id = p_chr_id;
42   --
43   cursor c_rest_to_ver is
44     select object_version_number
45       from okc_k_vers_numbers_h
46      where chr_id = p_chr_id
47        and major_version = l_minus_version;
48 
49 begin
50 
51     l_return_status := OKC_API.START_ACTIVITY(l_api_name,
52                                               G_PKG_NAME,
53                                               p_init_msg_list,
54                                               l_api_version,
55                                               p_api_version,
56                                               '_PVT',
57                                               x_return_status);
58     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
59       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
60     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
61       RAISE OKC_API.G_EXCEPTION_ERROR;
62     END IF;
63 
64     -- added for Bug 3431701
65   Set_Attach_Session_Vars(p_chr_id);
66 
67   if (p_action = 'CREATE_VERSION' or p_action = 'SAVE_VERSION') then
68 
69     if p_major_version = -1 then
70       open c_major_ver;
71       fetch c_major_ver into l_major_version;
72       close c_major_ver;
73     else
74       l_major_version := p_major_version - 1;
75     end if;
76   --
77   -- When a contract is opened for update, it creates a -1 version of the contract to keep its last
78   -- stable ACTIVE state. This -1 version is deleted only if the contract is restored back to this version
79   -- or when we open the same contract for update next time.
80   --
81   if p_action = 'SAVE_VERSION' then
82     --
83     -- Remove Header Level Attachment
84     --
85      if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_HEADERS_B',
86  						   l_pkey1 => p_chr_id,
87                                  l_pkey2 => l_minus_version) = 'Y' then
88 
89        fnd_attached_documents2_pkg.delete_attachments(
90 						x_entity_name => 'OKC_K_HEADERS_B',
91 						x_pk1_value   => p_chr_id,
92                               x_pk2_value   => l_minus_version,
93                               x_delete_document_flag => 'Y'); --
94 
95      end if;
96     --
97     -- Remove Line Level Attachment
98     --
99      for c_lines_rec in c_lines loop
100        --
101        if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_LINES_B',
102  						     l_pkey1 => c_lines_rec.id,
103 						     l_pkey2 => l_minus_version) = 'Y' then
104 
105          fnd_attached_documents2_pkg.delete_attachments(
106 							x_entity_name => 'OKC_K_LINES_B',
107 							x_pk1_value   => c_lines_rec.id,
108                                    x_pk2_value   => l_minus_version,
109                                    x_delete_document_flag => 'Y');
110 
111        end if;
112      end loop;
113   end if;
114   --
115   -- Creating Attachments for -1 level version of the contract
116   --
117   -- version Header Level Attachments
118   --
119     if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_HEADERS_B',
120  						  l_pkey1 => p_chr_id,
121 						  l_pkey2 => l_major_version) = 'Y' then
122 
123         fnd_attached_documents2_pkg.copy_attachments(
124 						    x_from_entity_name => 'OKC_K_HEADERS_B',
125 						    x_from_pk1_value   => p_chr_id,
126 						    x_from_pk2_value   => l_major_version,
127 						    x_to_entity_name   => 'OKC_K_HEADERS_B',
128 						    x_to_pk1_value     => p_chr_id,
129 						    x_to_pk2_value     => p_major_version);
130    end if;
131   --
132   -- Version Line Level Attachments
133   --
134    for c_lines_rec in c_lines loop
135     --
136     if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_LINES_B',
137  						  l_pkey1 => c_lines_rec.id,
138 						  l_pkey2 => l_major_version) = 'Y' then
139 
140         fnd_attached_documents2_pkg.copy_attachments(
141 						    x_from_entity_name => 'OKC_K_LINES_B',
142 						    x_from_pk1_value   => c_lines_rec.id,
143 						    x_from_pk2_value   => l_major_version,
144 						    x_to_entity_name   => 'OKC_K_LINES_B',
145 						    x_to_pk1_value     => c_lines_rec.id,
146 						    x_to_pk2_value     => p_major_version);
147    end if;
148    --
149   end loop;
150   --
151   -- While restoring version all the attachments are removed up to the version that is to be restored.
152   -- It is possible that when the user opens a contract for update, he may add, remove, update attachments.
153   -- The attachments copied with version -1 are re-attached to the version that is restores.
154   --
155    elsif (p_action = 'RESTORE_VERSION') then
156     --
157       open  c_major_ver;
158       fetch c_major_ver into l_curr_major_ver;
159       close c_major_ver;
160     --
161       open  c_rest_to_ver;
162       fetch c_rest_to_ver into l_rest_to_major_ver;
163       close c_rest_to_ver;
164     --
165 --
166 -- Delete Header Level Attachments with version
167 --
168    for l_ver_num in l_rest_to_major_ver..l_curr_major_ver loop
169 
170      if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_HEADERS_B',
171  						   l_pkey1 => p_chr_id,
172                                                    l_pkey2 => l_ver_num) = 'Y' then
173 
174        fnd_attached_documents2_pkg.delete_attachments(
175 						x_entity_name => 'OKC_K_HEADERS_B',
176 						x_pk1_value   => p_chr_id,
177                                                 x_pk2_value   => l_ver_num,
178                                                 x_delete_document_flag => 'Y');
179 
180      end if;
181      --
182      if l_ver_num = l_rest_to_major_ver then
183        --
184        if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_HEADERS_B',
185  						     l_pkey1 => p_chr_id,
186 						     l_pkey2 => l_minus_version) = 'Y' then
187 
188         fnd_attached_documents2_pkg.copy_attachments(
189 						    x_from_entity_name => 'OKC_K_HEADERS_B',
190 						    x_from_pk1_value   => p_chr_id,
191 						    x_from_pk2_value   => l_minus_version,
192 						    x_to_entity_name   => 'OKC_K_HEADERS_B',
193 						    x_to_pk1_value     => p_chr_id,
194 						    x_to_pk2_value     => l_ver_num);
195        end if;
196        --
197      end if;
198    end loop;
199   --
200   -- Remove Line Level Attachments
201   --
202    for c_lines_rec in c_lines loop
203     --
204      for l_ver_num in l_rest_to_major_ver..l_curr_major_ver loop
205        if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_LINES_B',
206  						     l_pkey1 => c_lines_rec.id,
207 						     l_pkey2 => l_ver_num) = 'Y' then
208 
209          fnd_attached_documents2_pkg.delete_attachments(
210 							x_entity_name => 'OKC_K_LINES_B',
211 							x_pk1_value   => c_lines_rec.id,
212                                    x_pk2_value   => l_ver_num,
213                                    x_delete_document_flag => 'Y');
214 
215        end if;
216        --
217        --
218        if l_ver_num = l_rest_to_major_ver then
219        --
220          if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_LINES_B',
221  						       l_pkey1 => c_lines_rec.id,
222 						       l_pkey2 => l_minus_version) = 'Y' then
223 
224             fnd_attached_documents2_pkg.copy_attachments(
225 						    x_from_entity_name => 'OKC_K_LINES_B',
226 						    x_from_pk1_value   => c_lines_rec.id,
227 						    x_from_pk2_value   => l_minus_version,
228 						    x_to_entity_name   => 'OKC_K_LINES_B',
229 						    x_to_pk1_value     => c_lines_rec.id,
230 						    x_to_pk2_value     => l_ver_num);
231          end if;
232          --
233        end if;
234        --
235      end loop;
236    end loop;
237     --
238     --
239 -- Added on 03/29/2001
240 -- following delete attachment statements are required to get rid of -1 version of attachments after a restore is made.
241 --
242     --
243     -- Remove Header Level Attachment
244     --
245      if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_HEADERS_B',
246  						   l_pkey1 => p_chr_id,
247                                  l_pkey2 => l_minus_version) = 'Y' then
248 
249        fnd_attached_documents2_pkg.delete_attachments(
250 						x_entity_name => 'OKC_K_HEADERS_B',
251 						x_pk1_value   => p_chr_id,
252                               x_pk2_value   => l_minus_version,
253                               x_delete_document_flag => 'Y'); --
254 
255      end if;
256     --
257     -- Remove Line Level Attachment
258     --
259      for c_lines_rec in c_lines loop
260        --
261        if fnd_attachment_util_pkg.get_atchmt_exists (l_entity_name => 'OKC_K_LINES_B',
262  						     l_pkey1 => c_lines_rec.id,
263 						     l_pkey2 => l_minus_version) = 'Y' then
264 
265          fnd_attached_documents2_pkg.delete_attachments(
266 							x_entity_name => 'OKC_K_LINES_B',
267 							x_pk1_value   => c_lines_rec.id,
268                                    x_pk2_value   => l_minus_version,
269                                    x_delete_document_flag => 'Y');
270 
271        end if;
272      end loop;
273 -- Added on 03/29/2001
274   end if;
275  EXCEPTION
276 
277     WHEN OKC_API.G_EXCEPTION_ERROR THEN
278       x_return_status := OKC_API.HANDLE_EXCEPTIONS
279       (
280         l_api_name,
281         G_PKG_NAME,
282         'OKC_API.G_RET_STS_ERROR',
283         x_msg_count,
284         x_msg_data,
285         '_PVT'
286       );
287     WHEN OKC_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
288       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
289       (
290         l_api_name,
291         G_PKG_NAME,
292         'OKC_API.G_RET_STS_UNEXP_ERROR',
293         x_msg_count,
294         x_msg_data,
295         '_PVT'
296       );
297     WHEN OTHERS THEN
298       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
299       (
300         l_api_name,
301         G_PKG_NAME,
302         'OTHERS',
303         x_msg_count,
304         x_msg_data,
305         '_PVT'
306       );
307 end version_attachments;
308 
309 --
310 -- p_pdf id is for Process Defn id for OKS seeded procedure
311 --
312 PROCEDURE OKC_VERSION_PLSQL (p_pdf_id IN  NUMBER,
313                              x_string OUT NOCOPY VARCHAR2) IS
314   l_string     VARCHAR2(2000);
315 
316   -- Cursor to get the package.procedure name from PDF
317   CURSOR pdf_cur(l_pdf_id IN NUMBER) IS
318    SELECT
319    decode(pdf.pdf_type,'PPS',
320           pdf.package_name||'.'||pdf.procedure_name,NULL) proc_name
321    FROM okc_process_defs_v pdf
322    WHERE pdf.id = l_pdf_id;
323 
324    pdf_rec pdf_cur%ROWTYPE;
325 
326    BEGIN
327       OPEN pdf_cur(p_pdf_id);
328       FETCH pdf_cur INTO pdf_rec;
329       CLOSE pdf_cur;
330 
331       l_string := l_string||pdf_rec.proc_name;
332       x_string := l_string ;
333 
334   END OKC_VERSION_PLSQL;
335 
336 --
337 --Public procedure to version various components of a contract
338 --
339 PROCEDURE version_contract(
340     p_api_version                  IN  NUMBER,
341     p_init_msg_list                IN  VARCHAR2 DEFAULT OKC_API.G_FALSE,
342     x_return_status                OUT NOCOPY VARCHAR2,
343     x_msg_count                    OUT NOCOPY NUMBER,
344     x_msg_data                     OUT NOCOPY VARCHAR2,
345     p_cvmv_rec                     IN  cvmv_rec_type,
346     x_cvmv_rec                     OUT NOCOPY cvmv_rec_type,
347     p_commit			   IN  VARCHAR2 DEFAULT OKC_API.G_TRUE) IS
348 
349 
350     l_api_version                  CONSTANT NUMBER := 1;
351     l_api_name                     CONSTANT VARCHAR2(30) := 'V_Version_Contract';
352     l_return_status                VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
353     l_chr_id                       Number;
354     l_major_version                Number;
355     l_cvmv_rec			   cvmv_rec_type;
356     l_cls_code 			   OKC_SUBCLASSES_B.CLS_CODE%TYPE:=OKC_API.G_MISS_CHAR;
357     l_pdf_id                       NUMBER := NULL;
358     l_string                       VARCHAR2(4000);
359     proc_string                    VARCHAR2(4000);
360     l_msg_data                     varchar2(2000);
361     l_doc_type                     varchar2(30);
362     l_doc_id                       Number;
363     l_msg_count                    NUMBER;
364 
365     -- Cursor to get the class code
366     CURSOR cur_scs(p_chr_id number) is
367        SELECT cls_code
368        FROM okc_k_headers_b, okc_subclasses_b
369        WHERE id = p_chr_id and code = scs_code;
370 
371     -- Cursor created to get the PDF_ID for a particular Class
372     CURSOR c_pdf(p_cls_code VARCHAR2) IS
373     SELECT pdf_id
374     FROM okc_class_operations
375     WHERE opn_code = 'VERSIONING'
376     AND   cls_code = p_cls_code;
377   BEGIN
378     l_return_status := OKC_API.START_ACTIVITY(l_api_name,
379                                               G_PKG_NAME,
380                                               p_init_msg_list,
381                                               l_api_version,
382                                               p_api_version,
383                                               '_PVT',
384                                               x_return_status);
385     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
386       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
387     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
388       RAISE OKC_API.G_EXCEPTION_ERROR;
389     END IF;
390     -- Get the version number from okc_k_vers_numbers
391     okc_cvm_pvt.version_contract_version(
392     p_api_version                  =>p_api_version,
393     p_init_msg_list                =>p_init_msg_list,
394     x_return_status                =>l_return_status,
395     x_msg_count                    =>x_msg_count,
396     x_msg_data                     =>x_msg_data,
397     p_cvmv_rec                     =>p_cvmv_rec,
398     x_cvmv_rec                     =>l_cvmv_rec);
399 
400     --- If any errors happen abort API
401 
402     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
403       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
404     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
405       RAISE OKC_API.G_EXCEPTION_ERROR;
406     END IF;
407     --
408     l_chr_id:=l_cvmv_rec.chr_id;
409     l_major_version:=l_cvmv_rec.major_version;
410 
411     OPEN cur_scs(l_chr_id);
412     FETCH cur_scs into l_cls_code;
413     CLOSE cur_scs;
414 
415     OPEN c_pdf(l_cls_code);
416     FETCH c_pdf INTO l_pdf_id;
417     CLOSE c_pdf;
418 
419     If l_pdf_id IS NOT NULL Then
420        okc_version_plsql (p_pdf_id => l_pdf_id,
421                           x_string => l_string) ;
422     End If;
423 
424     IF l_string is NOT NULL THEN
425        proc_string := 'begin '||l_string || ' (:b1,:b2); end ;';
426         EXECUTE IMMEDIATE proc_string using l_chr_id, out l_return_status;
427 
428         IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
429             RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
430         ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
431             RAISE OKC_API.G_EXCEPTION_ERROR;
432         END IF;
433     End If;
434     --
435     -- Version Attachments - CREATE_VERSION
436     --
437     version_attachments(p_chr_id        => l_chr_id,
438                         p_action        => 'CREATE_VERSION',
439                         p_major_version => l_major_version,
440                         p_api_version   => p_api_version,
441                         x_return_status =>l_return_status,
442                         x_msg_count     =>x_msg_count,
443                         x_msg_data      =>x_msg_data);
444     --
445     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
446       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
447     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
448       RAISE OKC_API.G_EXCEPTION_ERROR;
449     END IF;
450     --
451     --version contract header
452     --
453     l_return_status:=OKC_CHR_PVT.Create_Version(l_chr_id,l_major_version);
454     --- If any errors happen abort API
455     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
456       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
457     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
458       RAISE OKC_API.G_EXCEPTION_ERROR;
459     END IF;
460 
461     --version contract lines
462 
463     l_return_status:=OKC_CLE_PVT.Create_Version(l_chr_id,l_major_version);
464     --- If any errors happen abort API
465     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
466       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
467     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
468       RAISE OKC_API.G_EXCEPTION_ERROR;
469     END IF;
470 
471     --version contract items
472     l_return_status:=OKC_CIM_PVT.Create_Version(l_chr_id,l_major_version);
473     --- If any errors happen abort API
474     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
475       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
476     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
477       RAISE OKC_API.G_EXCEPTION_ERROR;
478     END IF;
479 
480     --version contract party roles
481     l_return_status:=OKC_CPL_PVT.Create_Version(l_chr_id,l_major_version);
482     --- If any errors happen abort API
483     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
484       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
485     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
486       RAISE OKC_API.G_EXCEPTION_ERROR;
487     END IF;
488 
489     --version RuleGroup party roles
490     l_return_status:=OKC_RMP_PVT.Create_Version(l_chr_id,l_major_version);
491     --- If any errors happen abort API
492     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
493       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
494     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
495       RAISE OKC_API.G_EXCEPTION_ERROR;
496     END IF;
497 
498     --version Rule Groups
499     l_return_status:=OKC_RGP_PVT.Create_Version(l_chr_id,l_major_version);
500     --- If any errors happen abort API
501     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
502       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
503     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
504       RAISE OKC_API.G_EXCEPTION_ERROR;
505     END IF;
506 
507     --version Rules
508     l_return_status:=OKC_RUL_PVT.Create_Version(l_chr_id,l_major_version);
509     --- If any errors happen abort API
510     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
511       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
512     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
513       RAISE OKC_API.G_EXCEPTION_ERROR;
514     END IF;
515 
516 
517     --version Contacts
518     l_return_status:=OKC_CTC_PVT.Create_Version(l_chr_id,l_major_version);
519     --- If any errors happen abort API
520     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
521       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
522     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
523       RAISE OKC_API.G_EXCEPTION_ERROR;
524     END IF;
525 
526     --version cover times
527     l_return_status:=OKC_CTI_PVT.Create_Version(l_chr_id,l_major_version);
528     --- If any errors happen abort API
529     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
530       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
531     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
532       RAISE OKC_API.G_EXCEPTION_ERROR;
533     END IF;
534 
535     --version contract accesses
536     l_return_status:=OKC_CAC_PVT.Create_Version(l_chr_id,l_major_version);
537     --- If any errors happen abort API
538     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
539       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
540     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
541       RAISE OKC_API.G_EXCEPTION_ERROR;
542     END IF;
543 
544     --version outcome arguments
545     l_return_status:=OKC_OAT_PVT.Create_Version(l_chr_id,l_major_version);
546     --- If any errors happen abort API
547     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
548       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
549     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
550       RAISE OKC_API.G_EXCEPTION_ERROR;
551     END IF;
552 
553     --version outcomes
554     l_return_status:=OKC_OCE_PVT.Create_Version(l_chr_id,l_major_version);
555     --- If any errors happen abort API
556     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
557       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
558     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
559       RAISE OKC_API.G_EXCEPTION_ERROR;
560     END IF;
561 
562     --version react intervals
563     l_return_status:=OKC_RIL_PVT.Create_Version(l_chr_id,l_major_version);
564     --- If any errors happen abort API
565     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
566       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
567     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
568       RAISE OKC_API.G_EXCEPTION_ERROR;
569     END IF;
570 
571 
572     --version timevalues
573     l_return_status:=OKC_TAV_PVT.Create_Version(l_chr_id,l_major_version);
574     --- If any errors happen abort API
575     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
576       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
577     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
578       RAISE OKC_API.G_EXCEPTION_ERROR;
579     END IF;
580 
581     --version contract processes
582     l_return_status:=OKC_CPS_PVT.Create_Version(l_chr_id,l_major_version);
583     --- If any errors happen abort API
584     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
585       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
586     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
587       RAISE OKC_API.G_EXCEPTION_ERROR;
588     END IF;
589 
590     --version condition headers
591     l_return_status:=OKC_CNH_PVT.Create_Version(l_chr_id,l_major_version);
592     --- If any errors happen abort API
593     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
594       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
595     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
596       RAISE OKC_API.G_EXCEPTION_ERROR;
597     END IF;
598 
599 
600     --version condition lines
601     l_return_status:=OKC_CNL_PVT.Create_Version(l_chr_id,l_major_version);
602     --- If any errors happen abort API
603     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
604       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
605     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
606       RAISE OKC_API.G_EXCEPTION_ERROR;
607     END IF;
608 
609     --version function_expr_params
610     l_return_status:=OKC_FEP_PVT.Create_Version(l_chr_id,l_major_version);
611     --- If any errors happen abort API
612     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
613       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
614     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
615       RAISE OKC_API.G_EXCEPTION_ERROR;
616     END IF;
617 
618     --version governances
619     l_return_status:=OKC_GVE_PVT.Create_Version(l_chr_id,l_major_version);
620     --- If any errors happen abort API
621     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
622       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
623     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
624       RAISE OKC_API.G_EXCEPTION_ERROR;
625     END IF;
626 
627 
628   ------VERSION_PRICE_ADJUSTMENTS
629     l_return_status:=OKC_PAT_PVT.Create_Version(l_chr_id,l_major_version);
630     --- If any errors happen abort API
631     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
632       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
633     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
634       RAISE OKC_API.G_EXCEPTION_ERROR;
635     END IF;
636 
637      --VERSION_PRICE_ADJ_ASSOCS
638     l_return_status:=OKC_PAC_PVT.Create_Version(l_chr_id,l_major_version);
639     --- If any errors happen abort API
640     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
641       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
642     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
643       RAISE OKC_API.G_EXCEPTION_ERROR;
644     END IF;
645 
646 
647     --VERSION_PRICE_ADJ_ATTRIBS
648     l_return_status:=OKC_PAA_PVT.Create_Version(l_chr_id,l_major_version);
649     --- If any errors happen abort API
650     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
651       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
652     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
653       RAISE OKC_API.G_EXCEPTION_ERROR;
654     END IF;
655 
656     --VERSION_PRICE_ATT_VALUES
657     l_return_status:=OKC_PAV_PVT.Create_Version(l_chr_id,l_major_version);
658     --- If any errors happen abort API
659     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
660       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
661     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
662       RAISE OKC_API.G_EXCEPTION_ERROR;
663     END IF;
664 
665 
666     --VERSION SALES CREDITS
667     l_return_status:=OKC_SCR_PVT.Create_Version(l_chr_id,l_major_version);
668     --- If any errors happen abort API
669     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
670       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
671     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
672       RAISE OKC_API.G_EXCEPTION_ERROR;
673      END IF;
674 
675 
676     --VERSION PRICE HOLD BREAK LINES
677     l_return_status:=OKC_PHL_PVT.Create_Version(l_chr_id,l_major_version);
678     --- If any errors happen abort API
679     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
680       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
681     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
682       RAISE OKC_API.G_EXCEPTION_ERROR;
683      END IF;
684 
685 
686       --VERSION TERMS and Condition and Deliverables
687          okc_terms_util_grp.get_contract_document_type_id(
688                                          p_api_version    => 1,
689                                          p_init_msg_list  => FND_API.G_FALSE,
690                                          x_return_status  => l_return_status,
691                                          x_msg_data       => l_msg_data,
692                                          x_msg_count      => l_msg_count,
693                                          p_chr_id         => l_chr_id,
694                                          x_doc_id         => l_doc_id,
695                                          x_doc_type       => l_doc_type);
696 
697         --- If any errors happen abort API
698         IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
699          	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
700         ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
701                  RAISE OKC_API.G_EXCEPTION_ERROR;
702         END IF;
703 
704 OKC_TERMS_VERSION_GRP.Version_doc(
705                       p_api_version   => 1,
706                       x_return_status => l_return_status,
707                       x_msg_data      => l_msg_data,
708                       x_msg_count     => l_msg_count,
709                       p_doc_type      => l_doc_type,
710                       p_doc_id        => l_doc_id,
711                       p_version_number =>l_major_version,
712                       p_clear_amendment =>'Y');
713 
714     --- If any errors happen abort API
715     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
716      	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
717     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
718          RAISE OKC_API.G_EXCEPTION_ERROR;
719     END IF;
720 
721 
722 
723     OKC_API.END_ACTIVITY(x_msg_count, x_msg_data);
724     x_cvmv_rec:=l_cvmv_rec;
725     x_return_status:=l_return_status;
726 
727     if (p_commit = OKC_API.G_TRUE) then commit; end if;
728 
729   EXCEPTION
730 
731     WHEN OKC_API.G_EXCEPTION_ERROR THEN
732       x_return_status := OKC_API.HANDLE_EXCEPTIONS
733       (
734         l_api_name,
735         G_PKG_NAME,
736         'OKC_API.G_RET_STS_ERROR',
737         x_msg_count,
738         x_msg_data,
739         '_PVT'
740       );
741     WHEN OKC_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
742       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
743       (
744         l_api_name,
745         G_PKG_NAME,
746         'OKC_API.G_RET_STS_UNEXP_ERROR',
747         x_msg_count,
748         x_msg_data,
749         '_PVT'
750       );
751     WHEN OTHERS THEN
752       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
753       (
754         l_api_name,
755         G_PKG_NAME,
756         'OTHERS',
757         x_msg_count,
758         x_msg_data,
759         '_PVT'
760       );
761 END Version_Contract;
762 
763 --Public procedure to save contract version
764 --Added parameter p_commit 11/09/200 02:00PM
765 
766 PROCEDURE save_version(
767     p_chr_id 				IN  NUMBER,
768     p_api_version             IN  NUMBER,
769     p_init_msg_list           IN  VARCHAR2 DEFAULT OKC_API.G_FALSE,
770     x_return_status           OUT NOCOPY VARCHAR2,
771     x_msg_count               OUT NOCOPY NUMBER,
772     x_msg_data                OUT NOCOPY VARCHAR2,
773     p_commit			     IN  VARCHAR2 DEFAULT OKC_API.G_TRUE) IS
774 
775 
776     l_api_version             CONSTANT NUMBER := 1;
777     l_api_name                CONSTANT VARCHAR2(30) := 'V_save_version';
778     l_return_status           VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
779     l_major_version	      CONSTANT NUMBER := -1;
780     l_major_version2	      CONSTANT NUMBER := -2;
781     l_chr_id		      NUMBER := p_chr_id;
782     l_code                    VARCHAR2(30);
783     l_msg_data                varchar2(2000);
784     l_doc_type                varchar2(30);
785     l_doc_id                  number;
786     l_msg_count               NUMBER;
787 
788 cursor v_lock is
789 select '!' from okc_k_vers_numbers
790 where chr_id = p_chr_id
791 for update of MAJOR_VERSION, MINOR_VERSION
792 nowait;
793 
794 cursor c_curs is
795 select b.cls_code
796 from okc_k_headers_b a,okc_subclasses_b b
797 where a.scs_code=b.code and a.id = p_chr_id;
798 
799 l_dummy varchar2(1);
800 
801 
802   BEGIN
803     l_return_status := OKC_API.START_ACTIVITY(l_api_name,
804                                               G_PKG_NAME,
805                                               p_init_msg_list,
806                                               l_api_version,
807                                               p_api_version,
808                                               '_PVT',
809                                               x_return_status);
810     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
811       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
812     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
813       RAISE OKC_API.G_EXCEPTION_ERROR;
814     END IF;
815 
816   begin
817     open v_lock;
818     fetch v_lock into l_dummy;
819     close v_lock;
820   exception
821     when others then
822       OKC_API.set_message(OKC_API.G_FND_APP,OKC_API.G_FORM_UNABLE_TO_RESERVE_REC);
823       RAISE OKC_API.G_EXCEPTION_ERROR;
824   end;
825 
826     -- Version Attachments SAVE_VERSION
827     --
828   OPEN c_curs;             --added for bug 2765502
829   FETCH c_curs into l_code;
830   ClOSE c_curs;
831 
832   IF l_code = 'SERVICE' Or
833      ((l_code <>'SERVICE') And
834       (FND_PROFILE.VALUE_SPECIFIC('OKC_REVERT_OPTION') = 'ALLOW')) THEN
835        version_attachments(p_chr_id        => p_chr_id,
836                         p_action        => 'SAVE_VERSION',
837                         p_major_version => -1,
838                         p_api_version   => p_api_version,
839                         x_return_status =>l_return_status,
840                         x_msg_count     =>x_msg_count,
841                         x_msg_data      =>x_msg_data);
842       IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
843          RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
844       ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
845          RAISE OKC_API.G_EXCEPTION_ERROR;
846       END IF;
847   END IF;
848     --
849     erase_saved_version(
850     		p_chr_id => p_chr_id,
851 	    	p_api_version => p_api_version,
852     		p_init_msg_list => p_init_msg_list,
853     		x_return_status => l_return_status,
854     		x_msg_count => x_msg_count,
855     		x_msg_data => x_msg_data,
856 		p_commit => OKC_API.G_FALSE);
857     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
858       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
859     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
860       RAISE OKC_API.G_EXCEPTION_ERROR;
861     END IF;
862 
863     --version contract header
864 
865   IF FND_PROFILE.VALUE_SPECIFIC('OKC_REVERT_OPTION') <> 'ALLOW' AND l_code<>'SERVICE'  THEN
866     l_return_status:=OKC_CHR_PVT.Create_Version(l_chr_id,l_major_version2);
867 
868    --- If any errors happen abort API
869     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
870       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
871     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
872       RAISE OKC_API.G_EXCEPTION_ERROR;
873     END IF;
874 
875   ELSE
876 
877    l_return_status:=OKC_CHR_PVT.Create_Version(l_chr_id,l_major_version);
878     --- If any errors happen abort API
879     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
880       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
881     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
882       RAISE OKC_API.G_EXCEPTION_ERROR;
883     END IF;
884 
885     --version contract lines
886 
887     l_return_status:=OKC_CLE_PVT.Create_Version(l_chr_id,l_major_version);
888     --- If any errors happen abort API
889     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
890       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
891     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
892       RAISE OKC_API.G_EXCEPTION_ERROR;
893     END IF;
894 
895     --version contract items
896     l_return_status:=OKC_CIM_PVT.Create_Version(l_chr_id,l_major_version);
897     --- If any errors happen abort API
898     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
899       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
900     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
901       RAISE OKC_API.G_EXCEPTION_ERROR;
902     END IF;
903 
904     --version contract party roles
905     l_return_status:=OKC_CPL_PVT.Create_Version(l_chr_id,l_major_version);
906     --- If any errors happen abort API
907     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
908       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
909     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
910       RAISE OKC_API.G_EXCEPTION_ERROR;
911     END IF;
912 
913     --version RuleGroup party roles
914     l_return_status:=OKC_RMP_PVT.Create_Version(l_chr_id,l_major_version);
915     --- If any errors happen abort API
916     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
917       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
918     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
919       RAISE OKC_API.G_EXCEPTION_ERROR;
920     END IF;
921 
922     --version Rule Groups
923     l_return_status:=OKC_RGP_PVT.Create_Version(l_chr_id,l_major_version);
924     --- If any errors happen abort API
925     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
926       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
927     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
928       RAISE OKC_API.G_EXCEPTION_ERROR;
929     END IF;
930 
931     --version Rules
932     l_return_status:=OKC_RUL_PVT.Create_Version(l_chr_id,l_major_version);
933     --- If any errors happen abort API
934     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
935       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
936     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
937       RAISE OKC_API.G_EXCEPTION_ERROR;
938     END IF;
939 
940     --version Contacts
941     l_return_status:=OKC_CTC_PVT.Create_Version(l_chr_id,l_major_version);
942     --- If any errors happen abort API
943     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
944       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
945     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
946       RAISE OKC_API.G_EXCEPTION_ERROR;
947     END IF;
948 
949     --version cover times
950     l_return_status:=OKC_CTI_PVT.Create_Version(l_chr_id,l_major_version);
951     --- If any errors happen abort API
952     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
953       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
954     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
955       RAISE OKC_API.G_EXCEPTION_ERROR;
956     END IF;
957 
958     --version contract accesses
959     l_return_status:=OKC_CAC_PVT.Create_Version(l_chr_id,l_major_version);
960     --- If any errors happen abort API
961     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
962       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
963     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
964       RAISE OKC_API.G_EXCEPTION_ERROR;
965     END IF;
966 
967     --version outcome arguments
968     l_return_status:=OKC_OAT_PVT.Create_Version(l_chr_id,l_major_version);
969     --- If any errors happen abort API
970     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
971       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
972     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
973       RAISE OKC_API.G_EXCEPTION_ERROR;
974     END IF;
975 
976     --version outcomes
977     l_return_status:=OKC_OCE_PVT.Create_Version(l_chr_id,l_major_version);
978     --- If any errors happen abort API
979     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
980       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
981     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
982       RAISE OKC_API.G_EXCEPTION_ERROR;
983     END IF;
984 
985     --version react intervals
986     l_return_status:=OKC_RIL_PVT.Create_Version(l_chr_id,l_major_version);
987     --- If any errors happen abort API
988     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
989       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
990     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
991       RAISE OKC_API.G_EXCEPTION_ERROR;
992     END IF;
993 
994 
995     --version timevalues
996     l_return_status:=OKC_TAV_PVT.Create_Version(l_chr_id,l_major_version);
997     --- If any errors happen abort API
998     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
999       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1000     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1001       RAISE OKC_API.G_EXCEPTION_ERROR;
1002     END IF;
1003 
1004     --version contract processes
1005     l_return_status:=OKC_CPS_PVT.Create_Version(l_chr_id,l_major_version);
1006     --- If any errors happen abort API
1007     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1008       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1009     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1010       RAISE OKC_API.G_EXCEPTION_ERROR;
1011     END IF;
1012 
1013     --version condition headers
1014     l_return_status:=OKC_CNH_PVT.Create_Version(l_chr_id,l_major_version);
1015     --- If any errors happen abort API
1016     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1017       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1018     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1019       RAISE OKC_API.G_EXCEPTION_ERROR;
1020     END IF;
1021 
1022 
1023     --version condition lines
1024     l_return_status:=OKC_CNL_PVT.Create_Version(l_chr_id,l_major_version);
1025     --- If any errors happen abort API
1026     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1027       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1028     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1029       RAISE OKC_API.G_EXCEPTION_ERROR;
1030     END IF;
1031 
1032     --version function_expr_params
1033     l_return_status:=OKC_FEP_PVT.Create_Version(l_chr_id,l_major_version);
1034     --- If any errors happen abort API
1035     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1036       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1037     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1038       RAISE OKC_API.G_EXCEPTION_ERROR;
1039     END IF;
1040 
1041     --version governances
1042     l_return_status:=OKC_GVE_PVT.Create_Version(l_chr_id,l_major_version);
1043     --- If any errors happen abort API
1044     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1045       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1046     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1047       RAISE OKC_API.G_EXCEPTION_ERROR;
1048     END IF;
1049 
1050     ------VERSION_PRICE_ADJUSTMENTS
1051     l_return_status:=OKC_PAT_PVT.Create_Version(l_chr_id,l_major_version);
1052     --- If any errors happen abort API
1053     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1054       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1055     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1056       RAISE OKC_API.G_EXCEPTION_ERROR;
1057     END IF;
1058 
1059      --VERSION_PRICE_ADJ_ASSOCS
1060     l_return_status:=OKC_PAC_PVT.Create_Version(l_chr_id,l_major_version);
1061     --- If any errors happen abort API
1062     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1063       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1064     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1065       RAISE OKC_API.G_EXCEPTION_ERROR;
1066     END IF;
1067 
1068 
1069     --VERSION_PRICE_ADJ_ATTRIBS
1070     l_return_status:=OKC_PAA_PVT.Create_Version(l_chr_id,l_major_version);
1071     --- If any errors happen abort API
1072     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1073       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1074     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1075       RAISE OKC_API.G_EXCEPTION_ERROR;
1076     END IF;
1077 
1078     --VERSION_PRICE_ATT_VALUES
1079     l_return_status:=OKC_PAV_PVT.Create_Version(l_chr_id,l_major_version);
1080     --- If any errors happen abort API
1081     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1082       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1083     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1084       RAISE OKC_API.G_EXCEPTION_ERROR;
1085      END IF;
1086 
1087 
1088     --VERSION SALES CREDITS
1089     l_return_status:=OKC_SCR_PVT.Create_Version(l_chr_id,l_major_version);
1090     --- If any errors happen abort API
1091     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1092       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1093     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1094       RAISE OKC_API.G_EXCEPTION_ERROR;
1095      END IF;
1096 
1097 
1098      --VERSION PRICE HOLD BREAK LINES
1099     l_return_status:=OKC_PHL_PVT.Create_Version(l_chr_id,l_major_version);
1100     --- If any errors happen abort API
1101     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1102       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1103     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1104       RAISE OKC_API.G_EXCEPTION_ERROR;
1105      END IF;
1106 
1107 
1108 
1109          okc_terms_util_grp.get_contract_document_type_id(
1110                                          p_api_version    => 1,
1111                                          p_init_msg_list  => FND_API.G_FALSE,
1112                                          x_return_status  => l_return_status,
1113                                          x_msg_data       => l_msg_data,
1114                                          x_msg_count      => l_msg_count,
1115                                          p_chr_id         => l_chr_id,
1116                                          x_doc_id         => l_doc_id,
1117                                          x_doc_type       => l_doc_type);
1118 
1119         --- If any errors happen abort API
1120         IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1121          	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1122         ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1123                  RAISE OKC_API.G_EXCEPTION_ERROR;
1124         END IF;
1125 
1126 OKC_TERMS_VERSION_GRP.Version_doc(
1127                       p_api_version   => 1,
1128                       x_return_status => l_return_status,
1129                       x_msg_data      => l_msg_data,
1130                       x_msg_count     => l_msg_count,
1131                       p_doc_type      => l_doc_type,
1132                       p_doc_id        => l_doc_id,
1133                       p_version_number =>l_major_version,
1134                       p_clear_amendment =>'Y');
1135 
1136     --- If any errors happen abort API
1137     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1138      	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1139     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1140          RAISE OKC_API.G_EXCEPTION_ERROR;
1141     END IF;
1142 
1143 -- just to save maj/min versions somewhere
1144     insert into okc_k_vers_numbers_h
1145     (
1146 	CHR_ID
1147 	,MAJOR_VERSION
1148 	,MINOR_VERSION
1149 	,OBJECT_VERSION_NUMBER
1150 	,CREATED_BY
1151 	,CREATION_DATE
1152 	,LAST_UPDATED_BY
1153 	,LAST_UPDATE_DATE
1154 	,LAST_UPDATE_LOGIN
1155     ) select
1156 	CHR_ID
1157 	,-1 -- otherwise UK violation
1158 	,MINOR_VERSION
1159 	,MAJOR_VERSION -- comes here instead of OBJECT_VERSION_NUMBER
1160 	,CREATED_BY
1161 	,CREATION_DATE
1162 	,FND_GLOBAL.USER_ID
1163 	,sysdate
1164 	,FND_GLOBAL.LOGIN_ID
1165     from okc_k_vers_numbers
1166     where chr_id = p_chr_id;
1167 
1168   END IF;
1169 
1170     OKC_API.END_ACTIVITY(x_msg_count, x_msg_data);
1171     x_return_status:=l_return_status;
1172 
1173     if (p_commit = OKC_API.G_TRUE) then commit; end if;
1174 
1175   EXCEPTION
1176 
1177     WHEN OKC_API.G_EXCEPTION_ERROR THEN
1178       x_return_status := OKC_API.HANDLE_EXCEPTIONS
1179       (
1180         l_api_name,
1181         G_PKG_NAME,
1182         'OKC_API.G_RET_STS_ERROR',
1183         x_msg_count,
1184         x_msg_data,
1185         '_PVT'
1186       );
1187     WHEN OKC_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1188       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
1189       (
1190         l_api_name,
1191         G_PKG_NAME,
1192         'OKC_API.G_RET_STS_UNEXP_ERROR',
1193         x_msg_count,
1194         x_msg_data,
1195         '_PVT'
1196       );
1197     WHEN OTHERS THEN
1198       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
1199       (
1200         l_api_name,
1201         G_PKG_NAME,
1202         'OTHERS',
1203         x_msg_count,
1204         x_msg_data,
1205         '_PVT'
1206       );
1207 END save_version;
1208 
1209 -- Private procedure for deleteting a version of the contract
1210 -- Added as a part of Bug:
1211 --
1212 
1213 Procedure delete_version (p_chr_id 	  IN NUMBER,
1214 					 p_major_version IN NUMBER,
1215 					 p_minor_version IN NUMBER,
1216 					 p_called_from   IN VARCHAR2) IS
1217 
1218 l_major_version number := p_major_version;
1219 l_minor_version number := p_minor_version;
1220 l_msg_data                     varchar2(2000);
1221 l_return_status                varchar2(1);
1222 l_doc_type                     varchar2(30);
1223 l_doc_id                       Number;
1224 l_msg_count                    NUMBER;
1225 
1226 begin
1227    DELETE FROM OKC_K_HEADERS_TLH
1228      WHERE id = p_chr_id
1229 	  AND (MAJOR_VERSION = -2
1230 	  AND p_called_from = 'ERASE_SAVED_VERSION');
1231 
1232     DELETE FROM OKC_K_HEADERS_BH
1233      WHERE id= p_chr_id
1234 	  AND (MAJOR_VERSION = -2
1235 	  AND p_called_from = 'ERASE_SAVED_VERSION');
1236 
1237 IF SQL%NOTFOUND THEN
1238 
1239 --
1240     delete FROM OKC_CONDITION_HEADERS_TLH
1241      WHERE id in (select id from OKC_CONDITION_HEADERS_BH
1242 	 		WHERE dnz_chr_id= p_chr_id)
1243 	  and ((MAJOR_VERSION = -1
1244 	  and p_called_from = 'ERASE_SAVED_VERSION')
1245         or (p_called_from = 'RESTORE_VERSION'
1246 	  and (major_version > l_major_version
1247 	   or major_version = -1)));
1248 --
1249     delete FROM OKC_CONDITION_HEADERS_BH
1250      WHERE dnz_chr_id= p_chr_id
1251 	  and ((MAJOR_VERSION = -1
1252 	  and p_called_from = 'ERASE_SAVED_VERSION')
1253         or (p_called_from = 'RESTORE_VERSION'
1254 	  and (major_version > l_major_version
1255 	   or major_version = -1)));
1256 --
1257     delete FROM OKC_CONDITION_LINES_TLH
1258      WHERE id in (select id from OKC_CONDITION_LINES_BH
1259 	 		WHERE dnz_chr_id= p_chr_id)
1260 	  and ((MAJOR_VERSION = -1
1261 	  and p_called_from = 'ERASE_SAVED_VERSION')
1262         or (p_called_from = 'RESTORE_VERSION'
1263 	  and (major_version > l_major_version
1264 	   or major_version = -1)));
1265 --
1266     delete FROM OKC_CONDITION_LINES_BH
1267      WHERE dnz_chr_id= p_chr_id
1268 	  and ((MAJOR_VERSION = -1
1269 	  and p_called_from = 'ERASE_SAVED_VERSION')
1270         or (p_called_from = 'RESTORE_VERSION'
1271 	  and (major_version > l_major_version
1272 	   or major_version = -1)));
1273 --
1274     delete FROM OKC_CONTACTS_H
1275      WHERE dnz_chr_id= p_chr_id
1276 	  and ((MAJOR_VERSION = -1
1277 	  and p_called_from = 'ERASE_SAVED_VERSION')
1278         or (p_called_from = 'RESTORE_VERSION'
1279 	  and (major_version > l_major_version
1280 	   or major_version = -1)));
1281 --
1282     delete FROM OKC_COVER_TIMES_H
1283      WHERE dnz_chr_id= p_chr_id
1284 	  and ((MAJOR_VERSION = -1
1285 	  and p_called_from = 'ERASE_SAVED_VERSION')
1286         or (p_called_from = 'RESTORE_VERSION'
1287 	  and (major_version > l_major_version
1288 	   or major_version = -1)));
1289 --
1290     delete FROM OKC_FUNCTION_EXPR_PARAMS_H
1291      WHERE dnz_chr_id= p_chr_id
1292 	  and ((MAJOR_VERSION = -1
1293 	  and p_called_from = 'ERASE_SAVED_VERSION')
1294         or (p_called_from = 'RESTORE_VERSION'
1295 	  and (major_version > l_major_version
1296 	   or major_version = -1)));
1297 --
1298     delete FROM OKC_GOVERNANCES_H
1299      WHERE dnz_chr_id= p_chr_id
1300 	  and ((MAJOR_VERSION = -1
1301 	  and p_called_from = 'ERASE_SAVED_VERSION')
1302         or (p_called_from = 'RESTORE_VERSION'
1303 	  and (major_version > l_major_version
1304 	   or major_version = -1)));
1305 --
1306     delete FROM OKC_K_ACCESSES_H
1307      WHERE chr_id= p_chr_id
1308 	  and ((MAJOR_VERSION = -1
1309 	  and p_called_from = 'ERASE_SAVED_VERSION')
1310         or (p_called_from = 'RESTORE_VERSION'
1311 	  and (major_version > l_major_version
1312 	   or major_version = -1)));
1313 --
1314     delete FROM OKC_K_HEADERS_TLH
1315      WHERE id = p_chr_id
1316 	  and ((MAJOR_VERSION = -1
1317 	  and p_called_from = 'ERASE_SAVED_VERSION')
1318         or (p_called_from = 'RESTORE_VERSION'
1319 	  and (major_version > l_major_version
1320 	   or major_version = -1)));
1321 --
1322     delete FROM OKC_K_HEADERS_BH
1323      WHERE id= p_chr_id
1324 	  and ((MAJOR_VERSION = -1
1325 	  and p_called_from = 'ERASE_SAVED_VERSION')
1326         or (p_called_from = 'RESTORE_VERSION'
1327 	  and (major_version > l_major_version
1328 	   or major_version = -1)));
1329 
1330     delete FROM OKS_K_HEADERS_BH
1331      WHERE chr_id= p_chr_id
1332 	  and ((MAJOR_VERSION = -1
1333 	  and p_called_from = 'ERASE_SAVED_VERSION')
1334         or (p_called_from = 'RESTORE_VERSION'
1335 	  and (major_version > l_major_version
1336 	   or major_version = -1)));
1337 --
1338     delete FROM OKC_K_ITEMS_H
1339      WHERE dnz_chr_id= p_chr_id
1340 	  and ((MAJOR_VERSION = -1
1341 	  and p_called_from = 'ERASE_SAVED_VERSION')
1342         or (p_called_from = 'RESTORE_VERSION'
1343 	  and (major_version > l_major_version
1344 	   or major_version = -1)));
1345 --
1346     delete FROM OKC_K_LINES_TLH
1347      WHERE id in (select id from OKC_K_LINES_BH
1348 	 		WHERE dnz_chr_id= p_chr_id)
1349 	  and ((MAJOR_VERSION = -1
1350 	  and p_called_from = 'ERASE_SAVED_VERSION')
1351         or (p_called_from = 'RESTORE_VERSION'
1352 	  and (major_version > l_major_version
1353 	   or major_version = -1)));
1354 --
1355     delete FROM OKC_K_LINES_BH
1356      WHERE dnz_chr_id= p_chr_id
1357 	  and ((MAJOR_VERSION = -1
1358 	  and p_called_from = 'ERASE_SAVED_VERSION')
1359         or (p_called_from = 'RESTORE_VERSION'
1360 	  and (major_version > l_major_version
1361 	   or major_version = -1)));
1362 
1363  delete FROM OKS_K_LINES_TLH
1364      WHERE id in (select id from OKS_K_LINES_BH
1365 	 		WHERE dnz_chr_id= p_chr_id)
1366 	  and ((MAJOR_VERSION = -1
1367 	  and p_called_from = 'ERASE_SAVED_VERSION')
1368         or (p_called_from = 'RESTORE_VERSION'
1369 	  and (major_version > l_major_version
1370 	   or major_version = -1)));
1371 
1372     delete FROM OKS_K_LINES_BH
1373      WHERE dnz_chr_id= p_chr_id
1374 	  and ((MAJOR_VERSION = -1
1375 	  and p_called_from = 'ERASE_SAVED_VERSION')
1376         or (p_called_from = 'RESTORE_VERSION'
1377 	  and (major_version > l_major_version
1378 	   or major_version = -1)));
1379 
1380     delete FROM OKC_K_PARTY_ROLES_TLH
1381      WHERE id in (select id from OKC_K_PARTY_ROLES_BH
1382 	 		WHERE dnz_chr_id= p_chr_id)
1383 	  and ((MAJOR_VERSION = -1
1384 	  and p_called_from = 'ERASE_SAVED_VERSION')
1385         or (p_called_from = 'RESTORE_VERSION'
1386 	  and (major_version > l_major_version
1387 	   or major_version = -1)));
1388 --
1389     delete FROM OKC_K_PARTY_ROLES_BH
1390      WHERE dnz_chr_id= p_chr_id
1391 	  and ((MAJOR_VERSION = -1
1392 	  and p_called_from = 'ERASE_SAVED_VERSION')
1393         or (p_called_from = 'RESTORE_VERSION'
1394 	  and (major_version > l_major_version
1395 	   or major_version = -1)));
1396 --
1397     delete FROM OKC_K_PROCESSES_H
1398      WHERE chr_id= p_chr_id
1399 	  and ((MAJOR_VERSION = -1
1400 	  and p_called_from = 'ERASE_SAVED_VERSION')
1401         or (p_called_from = 'RESTORE_VERSION'
1402 	  and (major_version > l_major_version
1403 	   or major_version = -1)));
1404 --
1405     delete FROM OKC_OUTCOME_ARGUMENTS_H
1406      WHERE dnz_chr_id= p_chr_id
1407 	  and ((MAJOR_VERSION = -1
1408 	  and p_called_from = 'ERASE_SAVED_VERSION')
1409         or (p_called_from = 'RESTORE_VERSION'
1410 	  and (major_version > l_major_version
1411 	   or major_version = -1)));
1412 --
1413     delete FROM OKC_OUTCOMES_TLH
1414      WHERE id in (select id from OKC_OUTCOMES_BH
1415 	 		WHERE dnz_chr_id= p_chr_id)
1416 	  and ((MAJOR_VERSION = -1
1417 	  and p_called_from = 'ERASE_SAVED_VERSION')
1418         or (p_called_from = 'RESTORE_VERSION'
1419 	  and (major_version > l_major_version
1420 	   or major_version = -1)));
1421 --
1422     delete FROM OKC_OUTCOMES_BH
1423      WHERE dnz_chr_id= p_chr_id
1424 	  and ((MAJOR_VERSION = -1
1425 	  and p_called_from = 'ERASE_SAVED_VERSION')
1426         or (p_called_from = 'RESTORE_VERSION'
1427 	  and (major_version > l_major_version
1428 	   or major_version = -1)));
1429 --
1430     delete FROM OKC_REACT_INTERVALS_H
1431      WHERE dnz_chr_id= p_chr_id
1432 	  and ((MAJOR_VERSION = -1
1433 	  and p_called_from = 'ERASE_SAVED_VERSION')
1434         or (p_called_from = 'RESTORE_VERSION'
1435 	  and (major_version > l_major_version
1436 	   or major_version = -1)));
1437 --
1438     delete FROM OKC_RG_PARTY_ROLES_H
1439      WHERE dnz_chr_id= p_chr_id
1440 	  and ((MAJOR_VERSION = -1
1441 	  and p_called_from = 'ERASE_SAVED_VERSION')
1442         or (p_called_from = 'RESTORE_VERSION'
1443 	  and (major_version > l_major_version
1444 	   or major_version = -1)));
1445 --
1446     delete FROM OKC_RULE_GROUPS_TLH
1447      WHERE id in (select id from OKC_RULE_GROUPS_BH
1448 	 		WHERE dnz_chr_id= p_chr_id)
1449 	  and ((MAJOR_VERSION = -1
1450 	  and p_called_from = 'ERASE_SAVED_VERSION')
1451         or (p_called_from = 'RESTORE_VERSION'
1452 	  and (major_version > l_major_version
1453 	   or major_version = -1)));
1454 --
1455     delete FROM OKC_RULE_GROUPS_BH
1456      WHERE dnz_chr_id= p_chr_id
1457 	  and ((MAJOR_VERSION = -1
1458 	  and p_called_from = 'ERASE_SAVED_VERSION')
1459         or (p_called_from = 'RESTORE_VERSION'
1460 	  and (major_version > l_major_version
1461 	   or major_version = -1)));
1462 --
1463 --Bug 3055393
1464 /*
1465     delete FROM OKC_RULES_TLH
1466      WHERE id in (select id from OKC_RULES_BH
1467 	 		WHERE dnz_chr_id= p_chr_id)
1468 	  and ((MAJOR_VERSION = -1
1469 	  and p_called_from = 'ERASE_SAVED_VERSION')
1470         or (p_called_from = 'RESTORE_VERSION'
1471 	  and (major_version > l_major_version
1472 	   or major_version = -1)));
1473 
1474 --
1475     delete FROM OKC_RULES_BH
1476      WHERE dnz_chr_id= p_chr_id
1477 	  and ((MAJOR_VERSION = -1
1478 	  and p_called_from = 'ERASE_SAVED_VERSION')
1479         or (p_called_from = 'RESTORE_VERSION'
1480 	  and (major_version > l_major_version
1481 	   or major_version = -1)));
1482 --
1483 */
1484 --Bug 3122962
1485 /*
1486     delete FROM OKC_TIMEVALUES_TLH
1487      WHERE id in (select id from OKC_TIMEVALUES_BH
1488 	 		WHERE dnz_chr_id= p_chr_id)
1489 	  and ((MAJOR_VERSION = -1
1490 	  and p_called_from = 'ERASE_SAVED_VERSION')
1491         or (p_called_from = 'RESTORE_VERSION'
1492 	  and (major_version > l_major_version
1493 	   or major_version = -1)));
1494 */
1495 --
1496 --Bug 3122962
1497     delete FROM OKC_TIMEVALUES_H
1498      WHERE dnz_chr_id= p_chr_id
1499 	  and ((MAJOR_VERSION = -1
1500 	  and p_called_from = 'ERASE_SAVED_VERSION')
1501         or (p_called_from = 'RESTORE_VERSION'
1502 	  and (major_version > l_major_version
1503 	   or major_version = -1)));
1504 --
1505 
1506      delete FROM OKC_PRICE_ADJUSTMENTS_H
1507      WHERE chr_id= p_chr_id
1508           and ((MAJOR_VERSION = -1
1509           and p_called_from = 'ERASE_SAVED_VERSION')
1510         or (p_called_from = 'RESTORE_VERSION'
1511           and (major_version > l_major_version
1512            or major_version = -1)));
1513 --
1514 
1515     delete FROM OKC_PRICE_ADJ_ASSOCS_H
1516      WHERE pat_id  IN
1517         ( SELECT pat_id
1518           FROM OKC_PRICE_ADJUSTMENTS
1519           WHERE chr_id = p_chr_id
1520              )
1521           and ((MAJOR_VERSION = -1
1522           and p_called_from = 'ERASE_SAVED_VERSION')
1523         or (p_called_from = 'RESTORE_VERSION'
1524           and (major_version > l_major_version
1525            or major_version = -1)));
1526 --
1527     delete FROM OKC_PRICE_ADJ_ATTRIBS_H
1528      WHERE pat_id  IN
1529         ( SELECT pat_id
1530           FROM OKC_PRICE_ADJUSTMENTS
1531           WHERE chr_id = p_chr_id
1532              )
1533           and ((MAJOR_VERSION = -1
1534           and p_called_from = 'ERASE_SAVED_VERSION')
1535         or (p_called_from = 'RESTORE_VERSION'
1536           and (major_version > l_major_version
1537            or major_version = -1)));
1538 
1539 -- bug 5679233
1540 -- decomposed the delete query into following two sub queries because of performance issues
1541 
1542     delete FROM OKC_PRICE_ATT_VALUES_H
1543      WHERE (chr_id= p_chr_id )
1544           and ((MAJOR_VERSION = -1
1545           and p_called_from = 'ERASE_SAVED_VERSION')
1546         or (p_called_from = 'RESTORE_VERSION'
1547           and (major_version >= l_major_version
1548            or major_version = -1)));
1549 
1550     delete FROM OKC_PRICE_ATT_VALUES_H
1551      WHERE (CLE_ID IN (SELECT ID FROM OKC_K_LINES_B WHERE DNZ_CHR_ID=p_chr_id ) )
1552           and ((MAJOR_VERSION = -1
1553           and p_called_from = 'ERASE_SAVED_VERSION')
1554         or (p_called_from = 'RESTORE_VERSION'
1555           and (major_version >= l_major_version
1556            or major_version = -1)));
1557 
1558 -- bug 5679233 ends
1559 
1560     delete FROM OKC_PH_LINE_BREAKS_H       --price hold
1561      WHERE cle_id IN
1562          (SELECT id
1563           FROM okc_k_lines_b
1564           WHERE dnz_chr_id = p_chr_id        --price hold sub-line
1565              )
1566          and ((MAJOR_VERSION = -1
1567           and p_called_from = 'ERASE_SAVED_VERSION')
1568         or (p_called_from = 'RESTORE_VERSION'
1569           and (major_version > l_major_version
1570            or major_version = -1)));
1571 
1572 
1573 
1574          okc_terms_util_grp.get_contract_document_type_id(
1575                                          p_api_version    => 1,
1576                                          p_init_msg_list  => FND_API.G_FALSE,
1577                                          x_return_status  => l_return_status,
1578                                          x_msg_data       => l_msg_data,
1579                                          x_msg_count      => l_msg_count,
1580                                          p_chr_id         => p_chr_id,
1581                                          x_doc_id         => l_doc_id,
1582                                          x_doc_type       => l_doc_type);
1583 
1584         --- If any errors happen abort API
1585         IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1586          	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1587         ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1588                  RAISE OKC_API.G_EXCEPTION_ERROR;
1589         END IF;
1590 
1591        	IF  p_called_from = 'ERASE_SAVED_VERSION' then
1592 
1593             OKC_TERMS_VERSION_GRP.delete_doc_version(
1594                       p_api_version   => 1,
1595                       x_return_status => l_return_status,
1596                       x_msg_data      => l_msg_data,
1597                       x_msg_count     => l_msg_count,
1598                       p_doc_type      => l_doc_type,
1599                       p_doc_id        => l_doc_id,
1600                       p_version_number =>-1);
1601 
1602           --- If any errors happen abort API
1603          IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1604      	         RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1605          ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1606                  RAISE OKC_API.G_EXCEPTION_ERROR;
1607          END IF;
1608       END IF;
1609 
1610       IF  p_called_from = 'RESTORE_VERSION'  then
1611 
1612           FOR CR in (Select major_version from okc_k_headers_hv where id=p_chr_id and (major_version > p_major_version or major_version=-1)) LOOP
1613 
1614             OKC_TERMS_VERSION_GRP.delete_doc_version(
1615                       p_api_version   => 1,
1616                       x_return_status => l_return_status,
1617                       x_msg_data      => l_msg_data,
1618                       x_msg_count     => l_msg_count,
1619                       p_doc_type      => l_doc_type,
1620                       p_doc_id        => l_doc_id,
1621                       p_version_number=> cr.major_version);
1622 
1623           --- If any errors happen abort API
1624          IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1625      	         RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1626          ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1627                  RAISE OKC_API.G_EXCEPTION_ERROR;
1628          END IF;
1629          END LOOP;
1630       END IF;
1631 --
1632     delete FROM okc_k_vers_numbers_h
1633      WHERE chr_id= p_chr_id
1634 	  and ((MAJOR_VERSION = -1
1635 	  and p_called_from = 'ERASE_SAVED_VERSION')
1636         or (p_called_from = 'RESTORE_VERSION'
1637 	  and (major_version > l_major_version
1638 	  or  ((major_version = l_major_version
1639 	  and minor_version >= l_minor_version))
1640 	   or major_version = -1)));
1641 
1642 END IF;
1643 
1644 end delete_version;
1645 --
1646 --Public procedure just to erase saved contract version
1647 PROCEDURE erase_saved_version(
1648     p_chr_id 				IN NUMBER,
1649     p_api_version                  IN NUMBER,
1650     p_init_msg_list                IN VARCHAR2 DEFAULT OKC_API.G_FALSE,
1651     x_return_status                OUT NOCOPY VARCHAR2,
1652     x_msg_count                    OUT NOCOPY NUMBER,
1653     x_msg_data                     OUT NOCOPY VARCHAR2,
1654     p_commit 				IN VARCHAR2 DEFAULT OKC_API.G_TRUE) IS
1655 
1656 
1657     l_api_version                  CONSTANT NUMBER := 1;
1658     l_api_name                     CONSTANT VARCHAR2(30) := 'V_erase_saved_version';
1659     l_return_status                VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
1660 
1661     l_cls_code                     OKC_SUBCLASSES_B.CLS_CODE%TYPE:=OKC_API.G_MISS_CHAR;
1662     l_pdf_id                       NUMBER := NULL;
1663     l_string                       VARCHAR2(32000);
1664     proc_string                    VARCHAR2(32000);
1665 
1666     -- Cursor to get the class code
1667     CURSOR cur_scs(p_chr_id number) is
1668        SELECT cls_code
1669        FROM okc_k_headers_b, okc_subclasses_b
1670        WHERE id = p_chr_id and code = scs_code;
1671 
1672      -- Cursor created to get the PDF_ID for a particular Class
1673     CURSOR c_pdf(p_cls_code VARCHAR2) IS
1674     SELECT pdf_id
1675     FROM okc_class_operations
1676     WHERE opn_code = 'ERASE_VERSION'
1677     AND   cls_code = p_cls_code;
1678   BEGIN
1679     l_return_status := OKC_API.START_ACTIVITY(l_api_name,
1680                                               G_PKG_NAME,
1681                                               p_init_msg_list,
1682                                               l_api_version,
1683                                               p_api_version,
1684                                               '_PVT',
1685                                               x_return_status);
1686     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1687       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1688     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1689       RAISE OKC_API.G_EXCEPTION_ERROR;
1690     END IF;
1691 --
1692 -- All the delete lines from here are moved into private procedure delete_version
1693 --
1694    delete_version(p_chr_id =>p_chr_id,
1695 			   p_major_version => -1,
1696 			   p_minor_version => null,
1697 			   p_called_from => 'ERASE_SAVED_VERSION');
1698 --
1699     OPEN cur_scs(p_chr_id);
1700     FETCH cur_scs into l_cls_code;
1701     CLOSE cur_scs;
1702 
1703     OPEN c_pdf(l_cls_code);
1704     FETCH c_pdf INTO l_pdf_id;
1705     CLOSE c_pdf;
1706 
1707     If l_pdf_id IS NOT NULL Then
1708        okc_version_plsql (p_pdf_id => l_pdf_id,
1709                           x_string => l_string) ;
1710     End If;
1711 
1712     IF l_string is NOT NULL THEN
1713        proc_string := 'begin '||l_string || ' (:b1,:b2); end ;';
1714         EXECUTE IMMEDIATE proc_string using p_chr_id, out l_return_status;
1715 
1716         IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1717             RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1718         ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1719             RAISE OKC_API.G_EXCEPTION_ERROR;
1720         END IF;
1721     End If;
1722 
1723     OKC_API.END_ACTIVITY(x_msg_count, x_msg_data);
1724     x_return_status:=l_return_status;
1725 
1726     if (p_commit = OKC_API.G_TRUE) then commit; end if;
1727 
1728   EXCEPTION
1729 
1730     WHEN OKC_API.G_EXCEPTION_ERROR THEN
1731       x_return_status := OKC_API.HANDLE_EXCEPTIONS
1732       (
1733         l_api_name,
1734         G_PKG_NAME,
1735         'OKC_API.G_RET_STS_ERROR',
1736         x_msg_count,
1737         x_msg_data,
1738         '_PVT'
1739       );
1740     WHEN OKC_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
1741       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
1742       (
1743         l_api_name,
1744         G_PKG_NAME,
1745         'OKC_API.G_RET_STS_UNEXP_ERROR',
1746         x_msg_count,
1747         x_msg_data,
1748         '_PVT'
1749       );
1750     WHEN OTHERS THEN
1751       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
1752       (
1753         l_api_name,
1754         G_PKG_NAME,
1755         'OTHERS',
1756         x_msg_count,
1757         x_msg_data,
1758         '_PVT'
1759       );
1760 END erase_saved_version;
1761 
1762 
1763 --Public procedure to restore contract version
1764 -- Added parameter p_commit on 09/11/2000 02:00PM
1765 --
1766 PROCEDURE restore_version(
1767     p_chr_id 			   IN NUMBER,
1768     p_api_version                  IN NUMBER,
1769     p_init_msg_list                IN VARCHAR2 DEFAULT OKC_API.G_FALSE,
1770     x_return_status                OUT NOCOPY VARCHAR2,
1771     x_msg_count                    OUT NOCOPY NUMBER,
1772     x_msg_data                     OUT NOCOPY VARCHAR2,
1773     p_commit			   IN VARCHAR2 DEFAULT OKC_API.G_TRUE) IS
1774 --
1775 --
1776     l_api_version                  CONSTANT NUMBER := 1;
1777     l_api_name                     CONSTANT VARCHAR2(30) := 'V_restore_version';
1778     l_return_status                VARCHAR2(1) := OKC_API.G_RET_STS_SUCCESS;
1779     l_major_version		   number;
1780     l_minor_version		   number;
1781     l_minus_version		   number := -1;
1782     l_chr_id                       number := p_chr_id;
1783     l_msg_data                     varchar2(2000);
1784     l_doc_type                     varchar2(30);
1785     l_doc_id                       number;
1786     l_msg_count                    NUMBER;
1787 
1788 cursor v is
1789 	select object_version_number, minor_version
1790 	from okc_k_vers_numbers_h
1791      	WHERE chr_id= p_chr_id
1792 	and MAJOR_VERSION = -1;
1793 
1794 cursor v_lock is
1795 select '!' from okc_k_vers_numbers
1796 where chr_id = p_chr_id
1797 for update of MAJOR_VERSION, MINOR_VERSION
1798 nowait;
1799 /*Added for Bug 5175907 */
1800     Cursor l_status_csr Is
1801 		SELECT sts_code
1802 		FROM okc_k_headers_b
1803 		WHERE ID = p_chr_id;
1804 
1805     CURSOR version_csr(p_chr_id NUMBER) IS
1806     	SELECT to_char (major_version)||'.'||to_char(minor_version)
1807     	FROM okc_k_vers_numbers
1808     	WHERE chr_id=p_chr_id;
1809 l_dummy varchar2(1);
1810         l_version  Varchar2(240);
1811         l_contract_old_status Varchar2(30);
1812         l_contract_new_status Varchar2(30);
1813         l_hstv_rec      OKC_K_HISTORY_PVT.hstv_rec_type;
1814         x_hstv_rec      OKC_K_HISTORY_PVT.hstv_rec_type;
1815 
1816 
1817   BEGIN
1818     l_return_status := OKC_API.START_ACTIVITY(l_api_name,
1819                                               G_PKG_NAME,
1820                                               p_init_msg_list,
1821                                               l_api_version,
1822                                               p_api_version,
1823                                               '_PVT',
1824                                               x_return_status);
1825     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1826       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1827     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1828       RAISE OKC_API.G_EXCEPTION_ERROR;
1829     END IF;
1830 --
1831     open v;
1832     fetch v into  l_major_version, l_minor_version;
1833     close v;
1834 --
1835 
1836   begin
1837     open v_lock;
1838     fetch v_lock into l_dummy;
1839     close v_lock;
1840   exception
1841     when others then
1842       OKC_API.set_message(OKC_API.G_FND_APP,OKC_API.G_FORM_UNABLE_TO_RESERVE_REC);
1843       RAISE OKC_API.G_EXCEPTION_ERROR;
1844   end;
1845 
1846     -- Version Attachments - RESTORE_VERSION
1847     --
1848     version_attachments(p_chr_id        => l_chr_id,
1849                         p_action        => 'RESTORE_VERSION',
1850                         p_major_version => NULL,
1851                         p_api_version   => p_api_version,
1852                         x_return_status =>l_return_status,
1853                         x_msg_count     =>x_msg_count,
1854                         x_msg_data      =>x_msg_data);
1855     --
1856     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1857       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1858     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1859       RAISE OKC_API.G_EXCEPTION_ERROR;
1860     END IF;
1861     --
1862     -- To get the old status
1863     open l_status_csr;
1864     fetch l_status_csr into l_Contract_old_status;
1865     close l_status_csr;
1866 
1867 
1868      delete from OKC_CONDITION_HEADERS_TL
1869 	WHERE id in (select id from OKC_CONDITION_HEADERS_B
1870 	 		WHERE dnz_chr_id= p_chr_id);
1871 
1872      delete from OKC_CONDITION_HEADERS_B where DNZ_CHR_ID = p_chr_id;
1873 
1874      delete from OKC_CONDITION_LINES_TL
1875 	WHERE id in (select id from OKC_CONDITION_LINES_B
1876 	 		WHERE dnz_chr_id= p_chr_id);
1877 
1878      delete from OKC_CONDITION_LINES_B where DNZ_CHR_ID = p_chr_id;
1879 
1880      delete from OKC_CONTACTS where DNZ_CHR_ID = p_chr_id;
1881 
1882      delete from OKC_COVER_TIMES where DNZ_CHR_ID = p_chr_id;
1883 
1884      delete from OKC_FUNCTION_EXPR_PARAMS where DNZ_CHR_ID = p_chr_id;
1885 
1886      delete from OKC_GOVERNANCES where DNZ_CHR_ID = p_chr_id;
1887 
1888      delete from OKC_K_ACCESSES where CHR_ID = p_chr_id;
1889 
1890      delete from OKC_K_HEADERS_TL
1891      WHERE id = p_chr_id;
1892 
1893      delete from OKC_K_HEADERS_B where ID = p_chr_id;
1894 
1895      delete from OKC_K_ITEMS where DNZ_CHR_ID = p_chr_id;
1896 --
1897       delete from OKC_PRICE_ATT_VALUES  WHERE chr_id = p_chr_id or cle_id in (select id from okc_k_lines_b where dnz_chr_id=p_chr_id);
1898 --
1899 
1900       delete from OKC_PH_LINE_BREAKS  WHERE cle_id IN
1901                                     (SELECT id
1902                                      FROM okc_k_lines_b
1903                                      WHERE dnz_chr_id = p_chr_id);
1904                                      --price hold line breaks are for a PRICE HOLD sub-line
1905 
1906 
1907      delete from OKC_K_LINES_TL
1908      WHERE id in (select id from OKC_K_LINES_B
1909 	 		WHERE dnz_chr_id= p_chr_id);
1910 
1911      delete from OKC_K_LINES_B where DNZ_CHR_ID = p_chr_id;
1912 
1913      delete from OKC_K_PARTY_ROLES_TL
1914      WHERE id in (select id from OKC_K_PARTY_ROLES_B
1915 	 		WHERE dnz_chr_id= p_chr_id);
1916 
1917      delete from OKC_K_PARTY_ROLES_B where DNZ_CHR_ID = p_chr_id;
1918 
1919      delete from OKC_K_PROCESSES where CHR_ID = p_chr_id;
1920 
1921      delete from OKC_OUTCOME_ARGUMENTS where DNZ_CHR_ID = p_chr_id;
1922 
1923      delete from OKC_OUTCOMES_TL
1924      WHERE id in (select id from OKC_OUTCOMES_B
1925 	 		WHERE dnz_chr_id= p_chr_id);
1926 
1927      delete from OKC_OUTCOMES_B where DNZ_CHR_ID = p_chr_id;
1928 
1929      delete from OKC_REACT_INTERVALS where DNZ_CHR_ID = p_chr_id;
1930 
1931      delete from OKC_RG_PARTY_ROLES where DNZ_CHR_ID = p_chr_id;
1932 
1933      delete from OKC_RULE_GROUPS_TL
1934      WHERE id in (select id from OKC_RULE_GROUPS_B
1935 	 		WHERE dnz_chr_id= p_chr_id);
1936 
1937      delete from OKC_RULE_GROUPS_B where DNZ_CHR_ID = p_chr_id;
1938 --Bug 3055393
1939 /*
1940      delete from OKC_RULES_TL
1941      WHERE id in (select id from OKC_RULES_B
1942 	 		WHERE dnz_chr_id= p_chr_id);
1943 
1944      delete from OKC_RULES_B where DNZ_CHR_ID = p_chr_id;
1945 */
1946 --Bug 3122962
1947 /*
1948      delete from OKC_TIMEVALUES_TL
1949      WHERE id in (select id from OKC_TIMEVALUES_B
1950 	 		WHERE dnz_chr_id= p_chr_id);
1951 */
1952 --Bug 3122962
1953      delete from OKC_TIMEVALUES where DNZ_CHR_ID = p_chr_id;
1954 
1955      delete from OKC_PRICE_ADJUSTMENTS
1956          WHERE chr_id = p_chr_id;
1957 
1958      delete from OKC_PRICE_ADJ_ASSOCS
1959          WHERE pat_id_from in ( select pat_id
1960                            from OKC_PRICE_ADJUSTMENTS
1961                            where chr_id = p_chr_id);
1962 
1963      delete from OKC_PRICE_ADJ_ATTRIBS
1964          WHERE pat_id in ( select pat_id
1965                            from OKC_PRICE_ADJUSTMENTS
1966                            where chr_id = p_chr_id);
1967 
1968 -- Incorrect location for this delete statement below
1969 -- delete from OKC_PRICE_ATT_VALUES  WHERE chr_id = p_chr_id or cle_id in (select id from okc_k_lines_b where dnz_chr_id=p_chr_id);
1970 --
1971 
1972 
1973       delete from OKC_K_SALES_CREDITS  WHERE dnz_chr_id = p_chr_id;
1974 
1975 
1976 
1977 --Restore contract header
1978 
1979     l_return_status:=OKC_CHR_PVT.Restore_Version(l_chr_id,l_minus_version);
1980     --- If any errors happen abort API
1981     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1982       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1983     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1984       RAISE OKC_API.G_EXCEPTION_ERROR;
1985     END IF;
1986 
1987     --Restore contract lines
1988 
1989     l_return_status:=OKC_CLE_PVT.Restore_Version(l_chr_id,l_minus_version);
1990     --- If any errors happen abort API
1991     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
1992       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
1993     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
1994       RAISE OKC_API.G_EXCEPTION_ERROR;
1995     END IF;
1996 
1997     --Restore  contract items
1998     l_return_status:=OKC_CIM_PVT.Restore_Version(l_chr_id,l_minus_version);
1999     --- If any errors happen abort API
2000     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2001       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2002     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2003       RAISE OKC_API.G_EXCEPTION_ERROR;
2004     END IF;
2005 
2006     --Restore  contract party roles
2007     l_return_status:=OKC_CPL_PVT.Restore_Version(l_chr_id,l_minus_version);
2008     --- If any errors happen abort API
2009     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2010       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2011     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2012       RAISE OKC_API.G_EXCEPTION_ERROR;
2013     END IF;
2014 
2015     --Restore  RuleGroup party roles
2016     l_return_status:=OKC_RMP_PVT.Restore_Version(l_chr_id,l_minus_version);
2017     --- If any errors happen abort API
2018     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2019       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2020     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2021       RAISE OKC_API.G_EXCEPTION_ERROR;
2022     END IF;
2023 
2024     --Restore  Rule Groups
2025     l_return_status:=OKC_RGP_PVT.Restore_Version(l_chr_id,l_minus_version);
2026     --- If any errors happen abort API
2027     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2028       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2029     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2030       RAISE OKC_API.G_EXCEPTION_ERROR;
2031     END IF;
2032 
2033     --Restore  Rules
2034     l_return_status:=OKC_RUL_PVT.Restore_Version(l_chr_id,l_minus_version);
2035     --- If any errors happen abort API
2036     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2037       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2038     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2039       RAISE OKC_API.G_EXCEPTION_ERROR;
2040     END IF;
2041 
2042     --Restore  Contacts
2043     l_return_status:=OKC_CTC_PVT.Restore_Version(l_chr_id,l_minus_version);
2044     --- If any errors happen abort API
2045     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2046       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2047     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2048       RAISE OKC_API.G_EXCEPTION_ERROR;
2049     END IF;
2050 
2051     --Restore  cover times
2052     l_return_status:=OKC_CTI_PVT.Restore_Version(l_chr_id,l_minus_version);
2053     --- If any errors happen abort API
2054     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2055       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2056     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2057       RAISE OKC_API.G_EXCEPTION_ERROR;
2058     END IF;
2059 
2060     --Restore  contract accesses
2061     l_return_status:=OKC_CAC_PVT.Restore_Version(l_chr_id,l_minus_version);
2062     --- If any errors happen abort API
2063     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2064       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2065     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2066       RAISE OKC_API.G_EXCEPTION_ERROR;
2067     END IF;
2068 
2069     --Restore  outcome arguments
2070     l_return_status:=OKC_OAT_PVT.Restore_Version(l_chr_id,l_minus_version);
2071     --- If any errors happen abort API
2072     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2073       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2074     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2075       RAISE OKC_API.G_EXCEPTION_ERROR;
2076     END IF;
2077 
2078     --Restore  outcomes
2079     l_return_status:=OKC_OCE_PVT.Restore_Version(l_chr_id,l_minus_version);
2080     --- If any errors happen abort API
2081     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2082       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2083     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2084       RAISE OKC_API.G_EXCEPTION_ERROR;
2085     END IF;
2086 
2087     --Restore  react intervals
2088     l_return_status:=OKC_RIL_PVT.Restore_Version(l_chr_id,l_minus_version);
2089     --- If any errors happen abort API
2090     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2091       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2092     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2093       RAISE OKC_API.G_EXCEPTION_ERROR;
2094     END IF;
2095 
2096 
2097     --Restore  timevalues
2098     l_return_status:=OKC_TAV_PVT.Restore_Version(l_chr_id,l_minus_version);
2099     --- If any errors happen abort API
2100     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2101       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2102     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2103       RAISE OKC_API.G_EXCEPTION_ERROR;
2104     END IF;
2105 
2106     --Restore  contract processes
2107     l_return_status:=OKC_CPS_PVT.Restore_Version(l_chr_id,l_minus_version);
2108     --- If any errors happen abort API
2109     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2110       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2111     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2112       RAISE OKC_API.G_EXCEPTION_ERROR;
2113     END IF;
2114 
2115     -- condition headers
2116     l_return_status:=OKC_CNH_PVT.Restore_Version(l_chr_id,l_minus_version);
2117     --- If any errors happen abort API
2118     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2119       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2120     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2121       RAISE OKC_API.G_EXCEPTION_ERROR;
2122     END IF;
2123 
2124 
2125     --Restore  condition lines
2126     l_return_status:=OKC_CNL_PVT.Restore_Version(l_chr_id,l_minus_version);
2127     --- If any errors happen abort API
2128     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2129       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2130     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2131       RAISE OKC_API.G_EXCEPTION_ERROR;
2132     END IF;
2133 
2134     --Restore  function_expr_params
2135     l_return_status:=OKC_FEP_PVT.Restore_Version(l_chr_id,l_minus_version);
2136     --- If any errors happen abort API
2137     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2138       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2139     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2140       RAISE OKC_API.G_EXCEPTION_ERROR;
2141     END IF;
2142 
2143     --Restore  governances
2144     l_return_status:=OKC_GVE_PVT.Restore_Version(l_chr_id,l_minus_version);
2145     --- If any errors happen abort API
2146     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2147       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2148     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2149       RAISE OKC_API.G_EXCEPTION_ERROR;
2150     END IF;
2151 
2152       --Restore _OKC_PRICE_ADJUSTMENTS
2153     l_return_status:=OKC_PAT_PVT.Restore_Version(l_chr_id,l_minus_version);
2154     --- If any errors happen abort API
2155     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2156       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2157     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2158       RAISE OKC_API.G_EXCEPTION_ERROR;
2159     END IF;
2160 
2161      --Restore _OKC_PRICE_ADJ_ASSOCS
2162     l_return_status:=OKC_PAC_PVT.Restore_Version(l_chr_id,l_minus_version);
2163     --- If any errors happen abort API
2164     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2165       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2166     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2167       RAISE OKC_API.G_EXCEPTION_ERROR;
2168     END IF;
2169 
2170      --Restore _OKC_PRICE_ADJ_ATTRIBS
2171     l_return_status:=OKC_PAA_PVT.Restore_Version(l_chr_id,l_minus_version);
2172     --- If any errors happen abort API
2173     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2174       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2175     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2176       RAISE OKC_API.G_EXCEPTION_ERROR;
2177     END IF;
2178 
2179     --Restore _OKC_PRICE_ATT_VALUES
2180     l_return_status:=OKC_PAV_PVT.Restore_Version(l_chr_id,l_minus_version);
2181     --- If any errors happen abort API
2182     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2183       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2184     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2185       RAISE OKC_API.G_EXCEPTION_ERROR;
2186     END IF;
2187 
2188 
2189     --Restore OKC_K_SALES_CREDITS
2190     l_return_status:=OKC_SCR_PVT.Restore_Version(l_chr_id,l_minus_version);
2191     --- If any errors happen abort API
2192     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2193       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2194     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2195       RAISE OKC_API.G_EXCEPTION_ERROR;
2196     END IF;
2197 
2198     --Restore PRICE HOLD BREAK LINES
2199     l_return_status:=OKC_PHL_PVT.Restore_Version(l_chr_id,l_minus_version);
2200     --- If any errors happen abort API
2201     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2202       RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2203     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2204       RAISE OKC_API.G_EXCEPTION_ERROR;
2205      END IF;
2206 
2207          okc_terms_util_grp.get_contract_document_type_id(
2208                                          p_api_version    => 1,
2209                                          p_init_msg_list  => FND_API.G_FALSE,
2210                                          x_return_status  => l_return_status,
2211                                          x_msg_data       => l_msg_data,
2212                                          x_msg_count      => l_msg_count,
2213                                          p_chr_id         => l_chr_id,
2214                                          x_doc_id         => l_doc_id,
2215                                          x_doc_type       => l_doc_type);
2216         --- If any errors happen abort API
2217         IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2218          	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2219         ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2220                  RAISE OKC_API.G_EXCEPTION_ERROR;
2221         END IF;
2222 
2223 OKC_TERMS_VERSION_GRP.restore_doc_version(
2224                       p_api_version   => 1,
2225                       x_return_status => l_return_status,
2226                       x_msg_data      => l_msg_data,
2227                       x_msg_count     => l_msg_count,
2228                       p_doc_type      => l_doc_type,
2229                       p_doc_id        => l_doc_id,
2230                       p_version_number =>l_major_version);
2231 
2232     --- If any errors happen abort API
2233     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2234      	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2235     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2236          RAISE OKC_API.G_EXCEPTION_ERROR;
2237     END IF;
2238 
2239        oks_contract_hdr_pub.restore_version
2240                               ( 1 ,
2241                                'T',
2242                                 l_return_status   ,
2243                                 l_msg_count     ,
2244                                 l_msg_data ,
2245                                 l_chr_id );
2246       --- If any errors happen abort API
2247     IF (l_return_status = OKC_API.G_RET_STS_UNEXP_ERROR) THEN
2248      	 RAISE OKC_API.G_EXCEPTION_UNEXPECTED_ERROR;
2249     ELSIF (l_return_status = OKC_API.G_RET_STS_ERROR) THEN
2250          RAISE OKC_API.G_EXCEPTION_ERROR;
2251     END IF;
2252 
2253 
2254      delete from okc_k_vers_numbers where CHR_ID = p_chr_id;
2255      INSERT INTO okc_k_vers_numbers(
2256 	CHR_ID
2257 	,MAJOR_VERSION
2258 	,MINOR_VERSION
2259 	,OBJECT_VERSION_NUMBER
2260 	,CREATED_BY
2261 	,CREATION_DATE
2262 	,LAST_UPDATED_BY
2263 	,LAST_UPDATE_DATE
2264 	,LAST_UPDATE_LOGIN
2265 	) select
2266 	CHR_ID
2267 	,OBJECT_VERSION_NUMBER
2268 	,MINOR_VERSION
2269 	,0
2270 	,CREATED_BY
2271 	,CREATION_DATE
2272 	,FND_GLOBAL.USER_ID
2273 	,sysdate
2274 	,FND_GLOBAL.LOGIN_ID
2275 	from okc_k_vers_numbers_h
2276 	where chr_id = p_chr_id
2277 	and MAJOR_VERSION = -1;
2278 --
2279   delete_version( p_chr_id 	    => p_chr_id,
2280 			   p_called_from   => 'RESTORE_VERSION',
2281 			   p_major_version => l_major_version,
2282 			   p_minor_version => l_minor_version);
2283 --
2284 --
2285 /*Fix for bug 5175907*/
2286 --Insert History
2287 
2288          open version_csr(p_chr_id);
2289          fetch version_csr into l_version;
2290          close version_csr;
2291 
2292 
2293              -- To get the new status
2294           open l_status_csr;
2295           fetch l_status_csr into l_Contract_new_status;
2296           close l_status_csr;
2297 
2298 
2299 
2300              -- To insert record in history tables
2301   	      l_hstv_rec.chr_id := p_chr_id;
2302   	      l_hstv_rec.sts_code_from := l_contract_old_status;
2303           l_hstv_rec.sts_code_to := l_Contract_new_status;
2304           l_hstv_rec.opn_code := 'STS_CHG';
2305   	      l_hstv_rec.contract_version := l_version;
2306 
2307               OKC_K_HISTORY_PUB.create_k_history(
2308          		p_api_version          => p_api_version,
2309          		p_init_msg_list        => p_init_msg_list,
2310          		x_return_status        => x_return_status,
2311          		x_msg_count            => x_msg_count,
2312          		x_msg_data             => x_msg_data,
2313          		p_hstv_rec             => l_hstv_rec,
2314          		x_hstv_rec             => x_hstv_rec);
2315 
2316 
2317 
2318 
2319     OKC_API.END_ACTIVITY(x_msg_count, x_msg_data);
2320     x_return_status:=l_return_status;
2321 
2322     if (p_commit = OKC_API.G_TRUE) then commit; end if;
2323 
2324   EXCEPTION
2325 
2326     WHEN OKC_API.G_EXCEPTION_ERROR THEN
2327       x_return_status := OKC_API.HANDLE_EXCEPTIONS
2328       (
2329         l_api_name,
2330         G_PKG_NAME,
2331         'OKC_API.G_RET_STS_ERROR',
2332         x_msg_count,
2333         x_msg_data,
2334         '_PVT'
2335       );
2336     WHEN OKC_API.G_EXCEPTION_UNEXPECTED_ERROR THEN
2337       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
2338       (
2339         l_api_name,
2340         G_PKG_NAME,
2341         'OKC_API.G_RET_STS_UNEXP_ERROR',
2342         x_msg_count,
2343         x_msg_data,
2344         '_PVT'
2345       );
2346     WHEN OTHERS THEN
2347       x_return_status :=OKC_API.HANDLE_EXCEPTIONS
2348       (
2349         l_api_name,
2350         G_PKG_NAME,
2351         'OTHERS',
2352         x_msg_count,
2353         x_msg_data,
2354         '_PVT'
2355       );
2356 END restore_version;
2357 
2358 -- Procedure to set attachement session variables if they are null
2359   -- Currently set only set for OKCAUDET and OKSAUDET
2360   --
2361   -- If want to get rid of this hard coding, FORM should add
2362   -- parameters and user should pass attachement_funtion_name
2363   -- and attachment_funtion_type
2364   --
2365   -- added for Bug 3431701
2366   PROCEDURE Set_Attach_Session_Vars(p_chr_id NUMBER) IS
2367     l_app_id NUMBER;
2368     Cursor l_chr_csr Is
2369 	      SELECT application_id
2370 	      FROM okc_k_headers_b
2371 	      WHERE id = p_chr_id;
2372   BEGIN
2373     If (p_chr_id IS NOT NULL AND
2374 	   FND_ATTACHMENT_UTIL_PKG.function_name IS NULL
2375 	  )
2376     Then
2377       open l_chr_csr;
2378       fetch l_chr_csr into l_app_id;
2379       close l_chr_csr;
2380 
2381        -- Added for Bug 2384423
2382       If (l_app_id = 515) Then
2383 	    FND_ATTACHMENT_UTIL_PKG.function_name := 'OKSAUDET';
2384 	    FND_ATTACHMENT_UTIL_PKG.function_type := 'O';
2385       Else
2386 	    FND_ATTACHMENT_UTIL_PKG.function_name := 'OKCAUDET';
2387 	    FND_ATTACHMENT_UTIL_PKG.function_type := 'O';
2388       End If;
2389 
2390     End If;
2391   END;
2392 
2393 END okc_version_pvt;