DBA Data[Home] [Help]

PACKAGE BODY: APPS.AMW_FINSTMT_CERT_BES_PKG

Source


1 PACKAGE BODY AMW_FINSTMT_CERT_BES_PKG AS
2 /* $Header: amwfbusb.pls 120.28 2007/12/10 06:41:35 srbalasu noship $  */
3 
4 --G_USER_ID NUMBER   := FND_GLOBAL.USER_ID;
5 --G_LOGIN_ID NUMBER  := FND_GLOBAL.CONC_LOGIN_ID;
6 
7 G_PKG_NAME    CONSTANT VARCHAR2 (30) := 'AMW_FINSTMT_CERT_BES_PKG';
8 G_API_NAME   CONSTANT VARCHAR2 (15) := 'amwfbusb.pls';
9 
10 
11 G_START_DATE DATE;
12 G_END_DATE DATE;
13 
14 l_index number;
15 
16 --l_result_ok CONSTANT VARCHAR2 (30) := 'SUCCESS';
17 --l_result_err CONSTANT VARCHAR2 (30) := 'ERROR';
18 
19 
20 PROCEDURE Initialize(p_certification_id NUMBER) IS
21 l_cert_id  NUMBER ;
22 l_error_message varchar2(4000);
23 
24 BEGIN
25 
26 
27 l_cert_id := p_certification_id;
28 
29 GetGLPeriodfor_FinCertEvalSum
30 (P_Certification_ID => l_cert_id,
31  P_start_date => G_START_DATE,
32  P_end_date => G_END_DATE
33 );
34 
35 
36 EXCEPTION WHEN OTHERS THEN
37     fnd_file.put_line (fnd_file.LOG, SUBSTR ('Unexpected Error in Initialize'
38               || SUBSTR (SQLERRM, 1, 100), 1, 200));
39     l_error_message := 'Error '||TO_CHAR(SQLCODE)||': '||SQLERRM ;
40 
41 END Initialize;
42 
43 
44 /* **************************** DELETE_ROWS in case of refresh data for a particular certification ******************* */
45 
46 procedure DELETE_ROWS ( x_fin_certification_id    NUMBER, x_table_name VARCHAR2  )
47 IS
48 l_sql_stmt VARCHAR2(2000);
49 begin
50 
51   l_sql_stmt := 'DELETE FROM ' || x_table_name || ' WHERE  fin_certification_id     =  : 1 ';
52 
53  EXECUTE IMMEDIATE l_sql_stmt USING x_fin_certification_id;
54 
55 
56 EXCEPTION
57  WHEN NO_DATA_FOUND THEN
58     /*** Record doesn't exist ***/
59     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.DELETE_FIN_CERT_SUM_ROWS');
60 WHEN OTHERS THEN
61  fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
62   fnd_file.put_line(fnd_file.LOG,  'fin_certification_id  ' || x_fin_certification_id  );
63 
64  RAISE ;
65  RETURN;
66 
67 end DELETE_ROWS ;
68 
69 
70 PROCEDURE  Is_Eval_Change
71 (
72     old_opinion_log_id          IN    	NUMBER,
73     new_opinion_log_id          IN    	NUMBER,
74     x_change_flag	    OUT NOCOPY  VARCHAR2
75 )
76 IS
77 
78 l_old_opinion_value_code amw_opinion_values_b.opinion_value_code%type;
79 l_new_opinion_value_code amw_opinion_values_b.opinion_value_code%type;
80 
81 CURSOR Get_Opinion_Value(p_opinion_log_id NUMBER)IS
82 SELECT OPINION_VALUE_CODE INTO l_old_opinion_value_code
83 FROM AMW_OPINION_VALUES_B value,
84 AMW_OPINION_LOG_DETAILS detail,
85 AMW_OPINION_COMPONTS_B comp
86 WHERE detail.opinion_log_id = p_opinion_log_id
87 AND detail.opinion_component_id = comp.opinion_component_id
88 AND comp.opinion_component_code = 'OVERALL'
89 AND detail.opinion_value_id = value.opinion_value_id;
90 
91 BEGIN
92 
93 IF(old_opinion_log_id <> 0 and  old_opinion_log_id IS NOT NULL ) THEN
94 OPEN Get_Opinion_Value(old_opinion_log_id);
95 FETCH Get_Opinion_Value INTO l_old_opinion_value_code;
96 CLOSE Get_Opinion_Value;
97 END IF;
98 
99 OPEN Get_Opinion_Value(new_opinion_log_id);
100 FETCH Get_Opinion_Value INTO l_new_opinion_value_code;
101 CLOSE Get_Opinion_Value;
102 
103 IF ( (old_opinion_log_id = 0 OR old_opinion_log_id is null) and (l_new_opinion_value_code is null) ) THEN
104 	x_change_flag := 'N';
105 ELSIF ( (old_opinion_log_id = 0 OR old_opinion_log_id is null) and l_new_opinion_value_code = 'EFFECTIVE') THEN
106 	x_change_flag := 'F';
107 ELSIF( (old_opinion_log_id = 0 OR old_opinion_log_id is null) and l_new_opinion_value_code <> 'EFFECTIVE') THEN
108 	x_change_flag := 'B';
109 ELSIF( l_old_opinion_value_code = 'EFFECTIVE' and  l_new_opinion_value_code = 'EFFECTIVE') THEN
110 	x_change_flag := 'N';
111 ELSIF( l_old_opinion_value_code = 'EFFECTIVE' and l_new_opinion_value_code <> 'EFFECTIVE') THEN
112        x_change_flag := 'B';
113 ELSIF( l_old_opinion_value_code <> 'EFFECTIVE' and l_new_opinion_value_code = 'EFFECTIVE') THEN
114 	x_change_flag := 'F';
115 ELSIF (l_old_opinion_value_code <> 'EFFECTIVE' and l_new_opinion_value_code <> 'EFFECTIVE') THEN
116 	x_change_flag := 'N';
117 END IF;
118 
119 END Is_Eval_Change;
120 
121 FUNCTION Get_Ratio_Fin_Cert
122        	( P_CERTIFICATION_ID IN NUMBER,
123                   P_FINANCIAL_STATEMENT_ID IN NUMBER,
124                   P_STATEMENT_GROUP_ID IN NUMBER ,
125                   P_ACCOUNT_ID      IN NUMBER,
126                   P_FINANCIAL_ITEM_ID IN NUMBER,
127                   P_OBJECT_TYPE IN VARCHAR2,
128                   P_STMT VARCHAR2)
129  RETURN NUMBER
130  IS
131  	l_stmt1  VARCHAR2(300);
132 	l_stmt2  VARCHAR2(200);
133 	l_sql_stmt VARCHAR2(4000);
134 	l_ratio_num NUMBER;
135 
136  BEGIN
137  	l_stmt1 := ' AND TEMP.STATEMENT_GROUP_ID = :2 AND TEMP.FINANCIAL_STATEMENT_ID = :3 AND TEMP.FINANCIAL_ITEM_ID = :4)';
138         l_stmt2 := ' AND TEMP.NATURAL_ACCOUNT_ID = :2)';
139 
140         IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
141         l_sql_stmt := p_stmt || l_stmt1;
142 
143         EXECUTE IMMEDIATE l_sql_stmt INTO l_ratio_num USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
144         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
145         l_sql_stmt := p_stmt || l_stmt2;
146 
147         EXECUTE IMMEDIATE l_sql_stmt INTO l_ratio_num USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
148         END IF;
149 
150         return l_ratio_num;
151 
152  EXCEPTION
153   WHEN NO_DATA_FOUND THEN
154     /*** Record doesn't exist ***/
155     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_Ratio_Fin_Cert');
156     RETURN 0;
157   WHEN OTHERS THEN
158     /*** Raise any other error ***/
159     fnd_file.put_line(fnd_file.LOG, 'Unexpected error IN ' || G_PKG_NAME || '.Get_Ratio_Fin_Cert');
160     fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
161     RAISE;
162  END Get_Ratio_Fin_Cert;
163 
164 
165 
166  FUNCTION GetTotalProcesses
167 (
168 P_CERTIFICATION_ID in number,
169 P_STATEMENT_GROUP_ID in number,
170 P_FINANCIAL_STATEMENT_ID in number,
171 P_FINANCIAL_ITEM_ID in number,
172 P_ACCOUNT_GROUP_ID in number,
173 P_ACCOUNT_ID in number,
174 P_OBJECT_TYPE in varchar2 ) RETURN NUMBER
175 
176 IS
177 	l_stmt  VARCHAR2(300);
178 	l_stmt1  VARCHAR2(300);
179 	l_stmt2  VARCHAR2(200);
180 	l_sql_stmt VARCHAR2(4000);
181 
182 	X_TOTAL_NUMBER_OF_PROCESSES NUMBER;
183 BEGIN
184 
185         l_stmt1 := ' AND fin.STATEMENT_GROUP_ID = :2 AND fin.FINANCIAL_STATEMENT_ID = :3 AND fin.FINANCIAL_ITEM_ID = :4)';
186         l_stmt2 := ' AND fin.NATURAL_ACCOUNT_ID = :2)';
187 
188         l_stmt := ' select count(1) from (SELECT DISTINCT ORGANIZATION_ID, PROCESS_ID
189         FROM amw_fin_cert_scope fin WHERE FIN_CERTIFICATION_ID= :1 AND PROCESS_ID IS NOT NULL';
190 
191         IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
192         l_sql_stmt := l_stmt || l_stmt1;
193 
194         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_NUMBER_OF_PROCESSES USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
195 
196         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
197         l_sql_stmt := l_stmt || l_stmt2;
198 
199         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_NUMBER_OF_PROCESSES USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
200         END IF;
201 
202         RETURN X_TOTAL_NUMBER_OF_PROCESSES;
203 
204  EXCEPTION
205   WHEN NO_DATA_FOUND THEN
206     /*** Record doesn't exist ***/
207     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.GetTotalProcesses');
208     RETURN 0;
209   WHEN OTHERS THEN
210   fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.GetTotalProcesses');
211   fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
212     RAISE;
213 END GetTotalProcesses ;
214 
215 
216 FUNCTION Get_Proc_Certified_Done
217 (
218 P_CERTIFICATION_ID in number,
219 P_STATEMENT_GROUP_ID in number,
220 P_FINANCIAL_STATEMENT_ID in number,
221 P_FINANCIAL_ITEM_ID in number,
222 P_ACCOUNT_GROUP_ID in number,
223 P_ACCOUNT_ID in number,
224 P_OBJECT_TYPE in varchar2 ) RETURN NUMBER
225 
226 IS
227 	l_stmt VARCHAR2(2000);
228 	l_stmt1 VARCHAR2(100);
229 	l_stmt2 VARCHAR2(100);
230 	l_sql_stmt VARCHAR2(2000);
231 
232 	X_PROCS_FOR_CERT_DONE Number;
233 	--l_start_time date;
234 	--l_end_time date;
235 BEGIN
236 
237 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
238 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
239 
240 l_stmt := 'select count(1)  from (
241  	Select distinct  fin.PROCESS_ID, fin.ORGANIZATION_ID
242 	FROM
243 	AMW_OPINION_MV aov,
244 	amw_fin_cert_scope fin,
245 	AMW_FIN_PROC_CERT_RELAN rel
246 	WHERE
247 	 rel.FIN_STMT_CERT_ID = ' || P_CERTIFICATION_ID || '
248 	and    fin.FIN_CERTIFICATION_ID = :1
249 	and    rel.end_date is null
250 	and    aov.PK2_VALUE = rel.PROC_CERT_ID
251  	and    aov.PK3_VALUE = fin.ORGANIZATION_ID
252  	and    aov.PK1_VALUE = fin.process_ID
253                 and    fin.process_id is not null
254                 and   aov.OPINION_TYPE_CODE = ''CERTIFICATION''
255 	and   aov.opinion_component_code = ''OVERALL''
256 	and   aov.object_name = ''AMW_ORG_PROCESS''';
257 
258         IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
259         l_sql_stmt := l_stmt || l_stmt1;
260 
261        -- l_start_time := sysdate;
262         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROCS_FOR_CERT_DONE USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
263        -- l_end_time := sysdate;
264 
265 
266         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
267         l_sql_stmt := l_stmt || l_stmt2;
268 
269 
270          -- l_start_time := sysdate;
271         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROCS_FOR_CERT_DONE USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
272         --l_end_time := sysdate;
273 
274         END IF;
275 
276         RETURN X_PROCS_FOR_CERT_DONE ;
277 
278  EXCEPTION
279   WHEN NO_DATA_FOUND THEN
280     /*** Record doesn't exist ***/
281     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_Proc_Certified_Done');
282     RETURN 0;
283   WHEN OTHERS THEN
284     /*** Raise any other error ***/
285     fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_Proc_Certified_Done');
286     fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
287     RAISE;
288 
289 END Get_Proc_Certified_Done;
290 
291 
292 FUNCTION  Get_Proc_Verified
293 (
294 P_CERTIFICATION_ID in number,
295 P_STATEMENT_GROUP_ID in number,
296 P_FINANCIAL_STATEMENT_ID in number,
297 P_FINANCIAL_ITEM_ID in number,
298 P_ACCOUNT_GROUP_ID in number,
299 P_ACCOUNT_ID in number,
300 P_OBJECT_TYPE in varchar2) RETURN NUMBER
301 
302 IS
303 l_stmt VARCHAR2(2000);
304 	l_stmt1 VARCHAR2(100);
305 	l_stmt2 VARCHAR2(100);
306 	l_sql_stmt VARCHAR2(2000);
307 
308 	X_PROC_VERIFIED Number;
309 BEGIN
310 
311 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
312 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
313 
314         l_stmt := 'SELECT COUNT(1) FROM
315  	(Select distinct  fin.PROCESS_ID, fin.ORGANIZATION_ID
316 	FROM
317 	AMW_OPINION_MV aov,
318 	amw_fin_cert_scope fin
319 	WHERE aov.OPINION_TYPE_CODE = ''EVALUATION''
320         and aov.object_name = ''AMW_ORG_PROCESS''
321         and aov.opinion_component_code = ''OVERALL''
322         and aov.PK3_VALUE = fin.ORGANIZATION_ID
323         and aov.PK1_VALUE = fin.PROCESS_ID
324         --fix bug 5724066
325 	and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
326         and fin.process_id is not null
327         and fin.FIN_CERTIFICATION_ID = :1 ';
328 
329 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
330         l_sql_stmt := l_stmt || l_stmt1;
331 
332         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_VERIFIED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
333 
334 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
335         l_sql_stmt := l_stmt || l_stmt2;
336         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_VERIFIED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
337 END IF;
338 
339         RETURN X_PROC_VERIFIED;
340  EXCEPTION
341   WHEN NO_DATA_FOUND THEN
342     /*** Record doesn't exist ***/
343     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_Proc_Verified');
344     RETURN 0;
345   WHEN OTHERS THEN
346     /*** Raise any other error ***/
347     fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_Proc_Verified');
348     fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
349     RAISE;
350 
351 END Get_Proc_Verified;
352 
353 FUNCTION Get_ORG_WITH_INEFF_CTRLS
354 (
355 P_CERTIFICATION_ID in number,
356 P_STATEMENT_GROUP_ID in number,
357 P_FINANCIAL_STATEMENT_ID in number,
358 P_FINANCIAL_ITEM_ID in number,
359 P_ACCOUNT_GROUP_ID in number,
360 P_ACCOUNT_ID in number,
361 P_OBJECT_TYPE in varchar2) RETURN NUMBER
362 
363 IS
364 	l_stmt VARCHAR2(2000);
365 	l_stmt1 VARCHAR2(100);
366 	l_stmt2 VARCHAR2(100);
367 	l_sql_stmt VARCHAR2(2000);
368 
369 	X_ORG_WITH_INEFF_CTRLS  Number;
370 BEGIN
371 
372 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
373 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
374 
375 l_stmt := 'select count(1) from (
376 select distinct fin.ORGANIZATION_ID
377 FROM
378 AMW_OPINION_MV aov,
379 amw_fin_cert_scope fin
380 WHERE
381  aov.AUTHORED_DATE in (select max(aov2.authored_date)
382                        from AMW_OPINIONS aov2
383                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
384                        and aov2.pk1_value = aov.pk1_value )
385 and aov.OPINION_TYPE_CODE = ''EVALUATION''
386 and aov.object_name = ''AMW_ORGANIZATION''
387 and aov.OPINION_VALUE_CODE <> ''EFFECTIVE''
388 and aov.opinion_component_code = ''OVERALL''
389 and aov.pk1_value = fin.organization_id
390 --fix bug 5724066
391 and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
392 and fin.FIN_CERTIFICATION_ID= :1 ';
393 
394 
395 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
396         l_sql_stmt := l_stmt || l_stmt1;
397         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_WITH_INEFF_CTRLS USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
398 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
399         l_sql_stmt := l_stmt || l_stmt2;
400         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_WITH_INEFF_CTRLS USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
401 END IF;
402 
403         RETURN X_ORG_WITH_INEFF_CTRLS;
404 
405 EXCEPTION
406   WHEN NO_DATA_FOUND THEN
407     /*** Record doesn't exist ***/
408     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_ORG_WITH_INEFF_CTRLS');
409     RETURN 0;
410   WHEN OTHERS THEN
411     /*** Raise any other error ***/
412     fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_ORG_WITH_INEFF_CTRLS');
413     fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
414     RAISE;
415 
416 END Get_ORG_WITH_INEFF_CTRLS;
417 
418 FUNCTION Get_ORG_EVALUATED
419 (
420 P_CERTIFICATION_ID in number,
421 P_STATEMENT_GROUP_ID in number,
422 P_FINANCIAL_STATEMENT_ID in number,
423 P_FINANCIAL_ITEM_ID in number,
424 P_ACCOUNT_GROUP_ID in number,
425 P_ACCOUNT_ID in number,
426 P_OBJECT_TYPE in varchar2) RETURN NUMBER
427 
428 IS
429 	l_stmt VARCHAR2(2000);
430 	l_stmt1 VARCHAR2(100);
431 	l_stmt2 VARCHAR2(100);
432 	l_sql_stmt VARCHAR2(2000);
433 
434 	X_ORG_EVALUATED  Number;
435 
436 BEGIN
437 
438 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
439 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
440 
441 l_stmt := 'select count(1) from (
442 select distinct fin.ORGANIZATION_ID
443 FROM
444 AMW_OPINION_MV aov,
445 amw_fin_cert_scope fin
446 WHERE aov.OPINION_TYPE_CODE = ''EVALUATION''
447 and aov.object_name = ''AMW_ORGANIZATION''
448 and aov.opinion_component_code = ''OVERALL''
449 and aov.pk1_value = fin.organization_id
450 --fix bug 5724066
451 and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
452 and fin.FIN_CERTIFICATION_ID= :1 ';
453 
454 
455 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
456         l_sql_stmt := l_stmt || l_stmt1;
457         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_EVALUATED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
458 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
459         l_sql_stmt := l_stmt || l_stmt2;
460 
461         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_EVALUATED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
462 END IF;
463 
464                 RETURN X_ORG_EVALUATED;
465 
466  EXCEPTION
467   WHEN NO_DATA_FOUND THEN
468     /*** Record doesn't exist ***/
469     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_ORG_EVALUATED');
470     RETURN 0;
471   WHEN OTHERS THEN
472     /*** Raise any other error ***/
473     fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_ORG_EVALUATED');
474 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
475     RAISE;
476 
477 END Get_ORG_EVALUATED;
478 
479 FUNCTION Get_TOTAL_ORGS
480 (
481 P_CERTIFICATION_ID in number,
482 P_STATEMENT_GROUP_ID in number,
483 P_FINANCIAL_STATEMENT_ID in number,
484 P_FINANCIAL_ITEM_ID in number,
485 P_ACCOUNT_GROUP_ID in number,
486 P_ACCOUNT_ID in number,
487 P_OBJECT_TYPE in varchar2 ) RETURN NUMBER
488 
489 IS
490 l_stmt VARCHAR2(2000);
491 	l_stmt1 VARCHAR2(100);
492 	l_stmt2 VARCHAR2(100);
493 	l_sql_stmt VARCHAR2(2000);
494 
495 	X_TOTAL_ORGS Number;
496 
497 
498 BEGIN
499 
500 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
501 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
502 
503 l_stmt := 'select COUNT(1)
504   from (select distinct fin.organization_id
505         from amw_fin_cert_scope fin
506         where
507         fin.organization_id is not null
508         and  fin.FIN_CERTIFICATION_ID= :1 ';
509 
510 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
511         l_sql_stmt := l_stmt || l_stmt1;
512 
513         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_ORGS USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
514 
515 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
516         l_sql_stmt := l_stmt || l_stmt2;
517         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_ORGS USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
518 
519 END IF;
520 
521 RETURN X_TOTAL_ORGS;
522 
523  EXCEPTION
524   WHEN NO_DATA_FOUND THEN
525     /*** Record doesn't exist ***/
526     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_TOTAL_ORGS');
527     RETURN 0;
528   WHEN OTHERS THEN
529     /*** Raise any other error ***/
530     fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_TOTAL_ORGS');
531 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
532     RAISE;
533 
534 END Get_TOTAL_ORGS;
535 
536 FUNCTION Get_ORG_CERTIFIED
537 (
538 P_CERTIFICATION_ID in number,
539 P_STATEMENT_GROUP_ID in number,
540 P_FINANCIAL_STATEMENT_ID in number,
541 P_FINANCIAL_ITEM_ID in number,
542 P_ACCOUNT_GROUP_ID in number,
543 P_ACCOUNT_ID in number,
544 P_OBJECT_TYPE in varchar2 ) RETURN NUMBER
545 
546 
547 IS
548 l_stmt VARCHAR2(2000);
549 	l_stmt1 VARCHAR2(100);
550 	l_stmt2 VARCHAR2(100);
551 	l_sql_stmt VARCHAR2(2000);
552 
553 	X_ORG_CERTIFIED  Number;
554 BEGIN
555 
556 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
557 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
558 
559 l_stmt := 'select count(1) from ( select distinct fin.organization_id FROM
560 AMW_OPINION_MV aov,
561 AMW_FIN_CERT_SCOPE fin
562 WHERE
563 aov.AUTHORED_DATE in (select max(aov2.authored_date)
564                        from AMW_OPINIONS aov2
565                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
566                        and aov2.pk1_value = aov.pk1_value )
567 and aov.OPINION_TYPE_CODE = ''CERTIFICATION''
568 and aov.object_name = ''AMW_ORGANIZATION''
569 and aov.opinion_component_code = ''OVERALL''
570 and aov.PK1_VALUE = fin.organization_id
571 and fin.organization_id is not null
572 and fin.FIN_CERTIFICATION_ID= :1 ';
573 
574 
575 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
576         l_sql_stmt := l_stmt || l_stmt1;
577 
578         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_CERTIFIED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
579 
580 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
581         l_sql_stmt := l_stmt || l_stmt2;
582 
583         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_CERTIFIED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
584 END IF;
585 
586 RETURN X_ORG_CERTIFIED;
587  EXCEPTION
588   WHEN NO_DATA_FOUND THEN
589     /*** Record doesn't exist ***/
590     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_ORG_CERTIFIED');
591     RETURN 0;
592   WHEN OTHERS THEN
593     /*** Raise any other error ***/
594 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_ORG_CERTIFIED');
595 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
596     RAISE;
597 
598 END Get_ORG_CERTIFIED;
599 
600 FUNCTION Get_PROC_WITH_INEFF_CTRLS
601 (
602 P_CERTIFICATION_ID in number,
603 P_STATEMENT_GROUP_ID in number,
604 P_FINANCIAL_STATEMENT_ID in number,
605 P_FINANCIAL_ITEM_ID in number,
606 P_ACCOUNT_GROUP_ID in number,
607 P_ACCOUNT_ID in number,
608 P_OBJECT_TYPE in varchar2) RETURN NUMBER
609 
610 IS
611 l_stmt VARCHAR2(2000);
612 	l_stmt1 VARCHAR2(100);
613 	l_stmt2 VARCHAR2(100);
614 	l_sql_stmt VARCHAR2(2000);
615 
616 	X_PROC_WITH_INEFF_CTRLS  Number;
617 
618 BEGIN
619 
620 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
621 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
622 
623 l_stmt := 'select count(1) from (
624 Select distinct  fin.process_id, fin.ORGANIZATION_ID
625 FROM
626 	AMW_OPINION_MV aov,
627 	amw_fin_cert_scope fin
628 WHERE
629 aov.AUTHORED_DATE in (select max(aov2.authored_date)
630                        from AMW_OPINIONS aov2
631                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
632                        and aov2.pk1_value = aov.pk1_value
633                        and aov2.pk3_value = aov.pk3_value)
634 and aov.OPINION_TYPE_CODE = ''EVALUATION''
635 and aov.object_name = ''AMW_ORG_PROCESS''
636 and aov.OPINION_VALUE_CODE <> ''EFFECTIVE''
637 and aov.opinion_component_code = ''OVERALL''
638 and aov.PK3_VALUE = fin.ORGANIZATION_ID
639 and aov.PK1_VALUE = fin.process_id
640 --fix bug 5724066
641 and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
642 and fin.process_id is not null
643 and fin.FIN_CERTIFICATION_ID= :1 ';
644 
645 
646 
647 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
648         l_sql_stmt := l_stmt || l_stmt1;
649 
650         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_WITH_INEFF_CTRLS USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
651 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
652         l_sql_stmt := l_stmt || l_stmt2;
653 
654         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_WITH_INEFF_CTRLS USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
655 END IF;
656 
657          RETURN X_PROC_WITH_INEFF_CTRLS;
658 
659   EXCEPTION
660   WHEN NO_DATA_FOUND THEN
661     /*** Record doesn't exist ***/
662     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_PROC_WITH_INEFF_CTRLS');
663     RETURN 0;
664   WHEN OTHERS THEN
665     /*** Raise any other error ***/
666 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_PROC_WITH_INEFF_CTRLS');
667 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
668     RAISE;
669 
670 END Get_PROC_WITH_INEFF_CTRLS;
671 
672 FUNCTION Get_UNMITIGATED_RISKS
673 (
674 P_CERTIFICATION_ID in number,
675 P_STATEMENT_GROUP_ID in number,
676 P_FINANCIAL_STATEMENT_ID in number,
677 P_FINANCIAL_ITEM_ID in number,
678 P_ACCOUNT_GROUP_ID in number,
679 P_ACCOUNT_ID in number,
680 P_OBJECT_TYPE in varchar2 ) RETURN NUMBER
681 
682 IS
683 l_stmt VARCHAR2(2000);
684 	l_stmt1 VARCHAR2(100);
685 	l_stmt2 VARCHAR2(100);
686 	l_sql_stmt VARCHAR2(2000);
687 
688 	X_UNMITIGATED_RISKS  Number;
689 BEGIN
690 
691 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
692 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
693 
694 l_stmt := 'select count(1)  from (
695 select distinct  fin.risk_id ,fin.organization_id, fin.Process_ID
696 FROM
697 	amw_opinion_log_mv aov,
698 	amw_fin_item_acc_risk fin,
699     	amw_risk_associations risks
700 WHERE risks.pk1 = fin.fin_certification_id
701 and risks.object_type = ''PROCESS_FINCERT''
702 and risks.risk_id = fin.risk_id
703 and risks.pk2 = fin.organization_id
704 and risks.pk3 = fin.process_id
705 and aov.opinion_log_id = risks.pk4
706 and aov.OPINION_TYPE_CODE = ''EVALUATION''
707 and aov.object_name = ''AMW_ORG_PROCESS_RISK''
708 and aov.opinion_component_code = ''OVERALL''
709 and aov.OPINION_VALUE_CODE <> ''EFFECTIVE''
710 and aov.AUTHORED_DATE in (select max(aov2.authored_date)
711                        from AMW_OPINIONS aov2
712                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
713                        and aov2.pk1_value = aov.pk1_value
714                        and aov2.pk3_value = aov.pk3_value
715                        and aov2.pk4_value = aov.pk4_value
716                        )
717 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
718 and fin.FIN_CERTIFICATION_ID= :1 ';
719 
720 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
721         l_sql_stmt := l_stmt || l_stmt1;
722 
723 
724 
725         EXECUTE IMMEDIATE l_sql_stmt INTO X_UNMITIGATED_RISKS USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
726 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
727         l_sql_stmt := l_stmt || l_stmt2;
728 
729 
730         EXECUTE IMMEDIATE l_sql_stmt INTO X_UNMITIGATED_RISKS USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
731 END IF;
732 
733         RETURN X_UNMITIGATED_RISKS;
734 
735 EXCEPTION
736   WHEN NO_DATA_FOUND THEN
737     /*** Record doesn't exist ***/
738     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_UNMITIGATED_RISKS');
739     RETURN 0;
740   WHEN OTHERS THEN
741     /*** Raise any other error ***/
742 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_UNMITIGATED_RISKS');
743 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
744     RAISE;
745 END Get_UNMITIGATED_RISKS;
746 
747 FUNCTION Get_Total_RISKS
748 (
749 P_CERTIFICATION_ID in number,
750 P_STATEMENT_GROUP_ID in number,
751 P_FINANCIAL_STATEMENT_ID in number,
752 P_FINANCIAL_ITEM_ID in number,
753 P_ACCOUNT_GROUP_ID in number,
754 P_ACCOUNT_ID in number,
755 P_OBJECT_TYPE in varchar2 ) RETURN NUMBER
756 
757 IS
758 	l_stmt VARCHAR2(2000);
759 	l_stmt1 VARCHAR2(100);
760 	l_stmt2 VARCHAR2(100);
761 	l_sql_stmt VARCHAR2(2000);
762 
763 	X_TOTAL_RISKS Number;
764 BEGIN
765 
766 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
767 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
768 
769 l_stmt := 'select count(1)
770 from (
771 select distinct risk_id, organization_id, process_id
772 FROM
773 	amw_fin_item_acc_risk fin
774 WHERE   fin.object_type =  ''' || P_OBJECT_TYPE || '''' || '
775 AND     fin.FIN_CERTIFICATION_ID= :1 ';
776 
777 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
778         l_sql_stmt := l_stmt || l_stmt1;
779 
780 
781         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_RISKS USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
782 
783 
784         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
785         l_sql_stmt := l_stmt || l_stmt2;
786 
787 
788         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_RISKS USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
789 
790         END IF;
791                 RETURN X_TOTAL_RISKS;
792 EXCEPTION
793   WHEN NO_DATA_FOUND THEN
794     /*** Record doesn't exist ***/
795     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_Total_RISKS');
796     RETURN 0;
797   WHEN OTHERS THEN
798     /*** Raise any other error ***/
799 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_Total_RISKS');
800 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
801     RAISE;
802 END Get_Total_RISKS;
803 
804 FUNCTION Get_RISKS_VERIFIED
805 (
806 P_CERTIFICATION_ID in number,
807 P_STATEMENT_GROUP_ID in number,
808 P_FINANCIAL_STATEMENT_ID in number,
809 P_FINANCIAL_ITEM_ID in number,
810 P_ACCOUNT_GROUP_ID in number,
811 P_ACCOUNT_ID in number,
812 P_OBJECT_TYPE in varchar2) RETURN NUMBER
813 
814 IS
815 	l_stmt VARCHAR2(2000);
816 	l_stmt1 VARCHAR2(100);
817 	l_stmt2 VARCHAR2(100);
818 	l_sql_stmt VARCHAR2(2000);
819 
820 	X_RISKS_VERIFIED  Number;
821 BEGIN
822 
823 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
824 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
825 
826 l_stmt := 'select count(1)  from (
827 select distinct  fin.risk_id ,fin.organization_id, fin.Process_ID
828 FROM
829 	amw_opinion_log_mv aov,
830 	amw_fin_item_acc_risk fin,
831 	amw_risk_associations risks
832 WHERE risks.pk1 = fin.fin_certification_id
833 and risks.object_type = ''PROCESS_FINCERT''
834 and risks.risk_id = fin.risk_id
835 and risks.pk2 = fin.organization_id
836 and risks.pk3 = fin.process_id
837 and aov.opinion_log_id = risks.pk4
838 and aov.OPINION_TYPE_CODE = ''EVALUATION''
839 and aov.object_name = ''AMW_ORG_PROCESS_RISK''
840 and aov.opinion_component_code = ''OVERALL''
841 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
842 and fin.FIN_CERTIFICATION_ID= :1 ';
843 
844 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
845         l_sql_stmt := l_stmt || l_stmt1;
846 
847 
848         EXECUTE IMMEDIATE l_sql_stmt INTO X_RISKS_VERIFIED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
849 
850 
851         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
852         l_sql_stmt := l_stmt || l_stmt2;
853 
854 
855         EXECUTE IMMEDIATE l_sql_stmt INTO X_RISKS_VERIFIED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
856 
857         END IF;
858                 RETURN X_RISKS_VERIFIED;
859 EXCEPTION
860   WHEN NO_DATA_FOUND THEN
861     /*** Record doesn't exist ***/
862     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_RISKS_VERIFIED');
863     RETURN 0;
864   WHEN OTHERS THEN
865     /*** Raise any other error ***/
866 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_RISKS_VERIFIED');
867 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
868     RAISE;
869 
870 END Get_RISKS_VERIFIED;
871 
872 
873 FUNCTION Get_INEFFECTIVE_CONTROLS
874 (
875 P_CERTIFICATION_ID in number,
876 P_STATEMENT_GROUP_ID in number,
877 P_FINANCIAL_STATEMENT_ID in number,
878 P_FINANCIAL_ITEM_ID in number,
879 P_ACCOUNT_GROUP_ID in number,
880 P_ACCOUNT_ID in number,
881 P_OBJECT_TYPE in varchar2) RETURN NUMBER
882 
883 IS
884 	l_stmt VARCHAR2(2000);
885 	l_stmt1 VARCHAR2(100);
886 	l_stmt2 VARCHAR2(100);
887 	l_sql_stmt VARCHAR2(2000);
888 
889 	X_INEFFECTIVE_CONTROLS  Number;
890 
891 
892 BEGIN
893 
894 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
895 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
896 
897 /****replace with the following query that uses opinon_log_id in amw_fin_item_acc_ctrl table directly
898 l_stmt := 'select count(1) from(
899 select distinct  fin.control_id, fin.organization_id
900 FROM
901 AMW_OPINION_MV aov,
902 amw_fin_item_acc_ctrl fin
903 WHERE
904 aov.AUTHORED_DATE in (select max(aov2.authored_date)
905                        from AMW_OPINIONS aov2
906                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
907                        and aov2.pk1_value = aov.pk1_value
908                        and aov2.pk3_value = aov.pk3_value)
909 AND aov.OPINION_TYPE_CODE = ''EVALUATION''
910 AND aov.object_name = ''AMW_ORG_CONTROL''
911 and aov.OPINION_VALUE_CODE <> ''EFFECTIVE''
912 and aov.opinion_component_code = ''OVERALL''
913 AND aov.pk1_value = fin.control_id
914 AND aov.pk3_value = fin.organization_id
915 --fix bug 5724066
916 and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
917 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
918 and fin.fin_certification_id = :1 ';
919 ****************/
920 
921 
922 /*** use opinion_log_id in amw_control_associations instead of opinion_log_id in amw_fin_item_acc_ctrl
923 **** so that it consists with VO query and also reduces the maintainence   ***********************/
924 l_stmt := 'select count(1) from(
925 select distinct  fin.control_id, fin.organization_id
926 FROM
927 amw_opinion_log_mv aov,
928 amw_fin_item_acc_ctrl fin,
929 amw_control_associations ctrls
930 WHERE
931 aov.opinion_log_id = ctrls.pk5
932 and ctrls.pk1 = fin.fin_certification_id
933 and   ctrls.object_type =   ''RISK_FINCERT''
934 and ctrls.control_id = fin.control_id
935 and ctrls.pk2 = fin.organization_id
936 and aov.OPINION_TYPE_CODE = ''EVALUATION''
937 and aov.object_name = ''AMW_ORG_CONTROL''
938 and aov.OPINION_VALUE_CODE <> ''EFFECTIVE''
939 and aov.opinion_component_code = ''OVERALL''
940 and aov.AUTHORED_DATE in (select max(aov2.authored_date)
941                        from AMW_OPINIONS aov2
942                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
943                        and aov2.pk1_value = aov.pk1_value
944                        and aov2.pk3_value = aov.pk3_value)
945 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
946 and fin.fin_certification_id = :1 ';
947 
948 
949 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
950         l_sql_stmt := l_stmt || l_stmt1;
951 
952         EXECUTE IMMEDIATE l_sql_stmt INTO X_INEFFECTIVE_CONTROLS USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
953 
954 
955         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
956         l_sql_stmt := l_stmt || l_stmt2;
957 
958 
959         EXECUTE IMMEDIATE l_sql_stmt INTO X_INEFFECTIVE_CONTROLS USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
960 
961         END IF;
962          RETURN X_INEFFECTIVE_CONTROLS;
963 EXCEPTION
964   WHEN NO_DATA_FOUND THEN
965     /*** Record doesn't exist ***/
966     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_INEFFECTIVE_CONTROLS');
967     RETURN 0;
968   WHEN OTHERS THEN
969     /*** Raise any other error ***/
970 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_INEFFECTIVE_CONTROLS');
971 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
972     RAISE;
973 
974 END Get_INEFFECTIVE_CONTROLS;
975 
976 FUNCTION Get_CONTROLS_VERIFIED
977 (
978 P_CERTIFICATION_ID in number,
979 P_STATEMENT_GROUP_ID in number,
980 P_FINANCIAL_STATEMENT_ID in number,
981 P_FINANCIAL_ITEM_ID in number,
982 P_ACCOUNT_GROUP_ID in number,
983 P_ACCOUNT_ID in number,
984 P_OBJECT_TYPE in varchar2) RETURN NUMBER
985 
986 IS
987 	l_stmt VARCHAR2(2000);
988 	l_stmt1 VARCHAR2(100);
989 	l_stmt2 VARCHAR2(100);
990 	l_sql_stmt VARCHAR2(2000);
991 
992 	X_CONTROLS_VERIFIED  Number;
993 
994 
995 BEGIN
996 
997 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
998 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
999 
1000 /*****replace with the following query ,which uses  opinion_log_id directly in amw_fin_item_acc_ctrl table
1001 l_stmt := 'select count(1) from(
1002 select distinct  fin.control_id, fin.organization_id
1003 FROM
1004 AMW_OPINION_MV aov,
1005 amw_fin_item_acc_ctrl fin
1006 WHERE aov.OPINION_TYPE_CODE = ''EVALUATION''
1007 AND aov.object_name = ''AMW_ORG_CONTROL''
1008 and aov.opinion_component_code = ''OVERALL''
1009 AND aov.pk1_value = fin.control_id
1010 AND aov.pk3_value = fin.organization_id
1011 --fix bug 5724066
1012 and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
1013 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
1014 and fin.fin_certification_id = :1 ';
1015 ******/
1016 l_stmt := 'select count(1) from(
1017 select distinct  fin.control_id, fin.organization_id
1018 FROM
1019 AMW_OPINION_LOG_MV aov,
1020 amw_fin_item_acc_ctrl fin,
1021 amw_control_associations ctrls
1022 WHERE
1023 aov.opinion_log_id = ctrls.pk5
1024 and ctrls.pk1 = fin.fin_certification_id
1025 and ctrls.object_type =   ''RISK_FINCERT''
1026 and ctrls.control_id = fin.control_id
1027 and ctrls.PK2   = fin.organization_id
1028 and aov.OPINION_TYPE_CODE = ''EVALUATION''
1029 and  aov.object_name = ''AMW_ORG_CONTROL''
1030 and  aov.opinion_component_code = ''OVERALL''
1031 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
1032 and fin.fin_certification_id = :1 ';
1033 
1034 
1035 
1036 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
1037         l_sql_stmt := l_stmt || l_stmt1;
1038 
1039 
1040         EXECUTE IMMEDIATE l_sql_stmt INTO X_CONTROLS_VERIFIED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
1041         --RETURN X_CONTROLS_VERIFIED;
1042 
1043         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
1044         l_sql_stmt := l_stmt || l_stmt2;
1045 
1046 
1047         EXECUTE IMMEDIATE l_sql_stmt INTO X_CONTROLS_VERIFIED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
1048         --RETURN X_CONTROLS_VERIFIED;
1049         END IF;
1050         RETURN X_CONTROLS_VERIFIED;
1051  EXCEPTION
1052   WHEN NO_DATA_FOUND THEN
1053     /*** Record doesn't exist ***/
1054     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_CONTROLS_VERIFIED');
1055     RETURN 0;
1056   WHEN OTHERS THEN
1057     /*** Raise any other error ***/
1058 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_CONTROLS_VERIFIED');
1059 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
1060     RAISE;
1061 
1062 
1063 END Get_CONTROLS_VERIFIED;
1064 
1065 
1066 
1067 FUNCTION Get_TOTAL_CONTROLS
1068 (
1069 P_CERTIFICATION_ID in number,
1070 P_STATEMENT_GROUP_ID in number,
1071 P_FINANCIAL_STATEMENT_ID in number,
1072 P_FINANCIAL_ITEM_ID in number,
1073 P_ACCOUNT_GROUP_ID in number,
1074 P_ACCOUNT_ID in number,
1075 P_OBJECT_TYPE in varchar2) RETURN NUMBER
1076 
1077 IS
1078 	l_stmt VARCHAR2(2000);
1079 	l_stmt1 VARCHAR2(100);
1080 	l_stmt2 VARCHAR2(100);
1081 	l_sql_stmt VARCHAR2(2000);
1082 
1083 	X_TOTAL_CONTROLS Number;
1084 
1085 
1086 BEGIN
1087 
1088 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
1089 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
1090 
1091 l_stmt := 'select count(1)
1092 from (
1093 select distinct organization_id, control_id
1094 FROM
1095 amw_fin_item_acc_ctrl fin
1096 WHERE fin.object_type =  ''' || p_object_type || '''' || '
1097       and fin.FIN_CERTIFICATION_ID= :1 ';
1098 
1099 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
1100         l_sql_stmt := l_stmt || l_stmt1;
1101 
1102         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_CONTROLS USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
1103         --RETURN X_TOTAL_CONTROLS;
1104 
1105         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
1106         l_sql_stmt := l_stmt || l_stmt2;
1107 
1108         EXECUTE IMMEDIATE l_sql_stmt INTO X_TOTAL_CONTROLS USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
1109         --RETURN X_TOTAL_CONTROLS;
1110         END IF;
1111 
1112         RETURN X_TOTAL_CONTROLS;
1113 
1114 EXCEPTION
1115   WHEN NO_DATA_FOUND THEN
1116     /*** Record doesn't exist ***/
1117     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_TOTAL_CONTROLS');
1118     RETURN 0;
1119   WHEN OTHERS THEN
1120     /*** Raise any other error ***/
1121 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_TOTAL_CONTROLS');
1122 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
1123     RAISE;
1124 
1125 END Get_TOTAL_CONTROLS;
1126 
1127 /*
1128 PROCEDURE Get_Fin_Evaluation
1129 ( P_CERTIFICATION_ID IN NUMBER,
1130 P_FINANCIAL_ITEM_ID  IN NUMBER,
1131 P_ACCOUNT_ID  	     IN NUMBER,
1132 P_OBJECT_TYPE 	     IN VARCHAR2,
1133 X_FIN_EVALUATION     OUT  NOCOPY NUMBER
1134 )IS
1135 BEGIN
1136 IF (P_OBJECT_TYPE = 'FINANCIAL ITEM') THEN
1137 	SELECT 	max(aov.opinion_log_id) INTO X_FIN_EVALUATION
1138 	FROM 	AMW_OPINION_LOG_MV aov
1139        	WHERE 	aov.object_name = 'AMW_FINANCIAL_ITEM'
1140         AND 	aov.opinion_type_code = 'EVALUATION'
1141         AND 	aov.pk1_value = P_FINANCIAL_ITEM_ID
1142         AND 	aov.pk2_value = P_CERTIFICATION_ID
1143         AND 	aov.authored_date = (select max(aov2.authored_date)
1144                        	             from AMW_OPINIONS aov2
1145                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
1146                                      and aov2.pk2_value = aov.pk2_value
1147                                      and aov2.pk1_value = aov.pk1_value);
1148 
1149 ELSIF (P_OBJECT_TYPE = 'ACCOUNT') THEN
1150        SELECT 	max(aov.opinion_log_id) INTO X_FIN_EVALUATION
1151 	FROM 	AMW_OPINION_LOG_MV aov
1152        	WHERE 	aov.object_name = 'AMW_KEY_ACCOUNT'
1153         AND 	aov.opinion_type_code = 'EVALUATION'
1154         AND 	aov.pk1_value = P_ACCOUNT_ID
1155         AND 	aov.pk2_value = P_CERTIFICATION_ID
1156         AND 	aov.authored_date = (select max(aov2.authored_date)
1157                        	             from AMW_OPINIONS aov2
1158                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
1159                                      and aov2.pk2_value = aov.pk2_value
1160                                      and aov2.pk1_value = aov.pk1_value);
1161 END IF;
1162 
1163 EXCEPTION
1164   WHEN NO_DATA_FOUND THEN
1165     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_Fin_Evaluation');
1166     raise;
1167   WHEN OTHERS THEN
1168   fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_Fin_Evaluation');
1169   fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
1170     RAISE;
1171 END Get_Fin_Evaluation;
1172 **********/
1173 
1174 FUNCTION Get_PROC_CERT_WITH_ISSUES
1175 (
1176 P_CERTIFICATION_ID in number,
1177 P_STATEMENT_GROUP_ID in number,
1178 P_FINANCIAL_STATEMENT_ID in number,
1179 P_FINANCIAL_ITEM_ID in number,
1180 P_ACCOUNT_GROUP_ID in number,
1181 P_ACCOUNT_ID in number,
1182 P_OBJECT_TYPE in varchar2) RETURN NUMBER
1183 
1184 IS
1185 	l_stmt VARCHAR2(2000);
1186 	l_stmt1 VARCHAR2(100);
1187 	l_stmt2 VARCHAR2(100);
1188 	l_sql_stmt VARCHAR2(2000);
1189 
1190 	l_start_time date;
1191 	l_end_time date;
1192 
1193 	X_PROC_CERT_WITH_ISSUES Number;
1194 
1195 BEGIN
1196 
1197 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
1198 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
1199 
1200 l_stmt := 'select count(1)  from (
1201 Select distinct  fin.process_ID, fin.ORGANIZATION_ID
1202 FROM
1203 	AMW_OPINION_MV aov,
1204 	amw_fin_cert_scope fin,
1205 	amw_fin_proc_cert_relan rel
1206 WHERE
1207 rel.FIN_STMT_CERT_ID = ' || P_CERTIFICATION_ID || '
1208 and fin.FIN_CERTIFICATION_ID= :1
1209 and fin.process_id is not null
1210 and rel.end_date is null
1211 and  aov.PK2_VALUE = rel.PROC_CERT_ID
1212 and aov.PK3_VALUE = fin.ORGANIZATION_ID
1213 and aov.PK1_VALUE = fin.PROCESS_ID
1214 and aov.OPINION_TYPE_CODE = ''CERTIFICATION''
1215 and aov.object_name = ''AMW_ORG_PROCESS''
1216 and aov.OPINION_VALUE_CODE <> ''EFFECTIVE''
1217 and aov.opinion_component_code = ''OVERALL''
1218 and aov.authored_date = (select max(aov2.authored_date)
1219                                      from AMW_OPINIONS  aov2
1220                                      where aov2.object_opinion_type_id
1221                                            = aov.object_opinion_type_id
1222                                      and aov2.pk3_value = aov.pk3_value
1223                                      AND aov2.pk2_value in
1224                                         (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
1225                                          where fin_stmt_cert_id = fin.FIN_CERTIFICATION_ID
1226                                          and end_date is null)
1227                                      and aov2.pk1_value = aov.pk1_value) ';
1228 
1229 
1230 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
1231         l_sql_stmt := l_stmt || l_stmt1;
1232 
1233 
1234 
1235       --  l_start_time := sysdate;
1236         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_CERT_WITH_ISSUES USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
1237       -- l_end_time := sysdate;
1238       --  RETURN X_PROC_CERT_WITH_ISSUES;
1239 
1240         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
1241          l_sql_stmt := l_stmt || l_stmt2;
1242 
1243 
1244     --  l_start_time := sysdate;
1245         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_CERT_WITH_ISSUES USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
1246 
1247       -- l_end_time := sysdate;
1248 
1249       -- RETURN X_PROC_CERT_WITH_ISSUES;
1250         END IF;
1251 
1252         RETURN X_PROC_CERT_WITH_ISSUES;
1253 EXCEPTION
1254   WHEN NO_DATA_FOUND THEN
1255     /*** Record doesn't exist ***/
1256     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_PROC_CERT_WITH_ISSUES');
1257     RETURN 0;
1258   WHEN OTHERS THEN
1259     /*** Raise any other error ***/
1260 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_PROC_CERT_WITH_ISSUES');
1261 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
1262     RAISE;
1263 
1264 END Get_PROC_CERT_WITH_ISSUES;
1265 
1266 
1267 ---------------------------******** Get Information for Dashboard ********----------------
1268 PROCEDURE  Get_global_proc_not_certified
1269 (
1270     p_certification_id          IN    	NUMBER,
1271     x_global_proc_not_certified OUT NOCOPY Number
1272 ) IS
1273 BEGIN
1274   SELECT count(1) INTO x_global_proc_not_certified
1275      	FROM ( SELECT distinct proc.organization_id, proc.process_id
1276      	       FROM AMW_FIN_PROCESS_EVAL_SUM proc
1277      	       WHERE proc.fin_certification_id = p_certification_id
1278                    AND proc.organization_id = NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'), -999)
1279                    AND not exists (SELECT 'Y'
1280                              FROM AMW_OPINION_MV aov,
1281                              	  AMW_FIN_PROC_CERT_RELAN rel
1282                              WHERE aov.object_name = 'AMW_ORG_PROCESS'
1283                               AND aov.opinion_type_code = 'CERTIFICATION'
1284                               AND aov.opinion_component_code = 'OVERALL'
1285                               AND aov.pk3_value = proc.organization_id
1286                               AND aov.pk2_value = rel.proc_cert_id
1287                               AND rel.fin_stmt_cert_id = p_certification_id
1288                               AND rel.end_date is null
1289                               AND aov.pk1_value = proc.process_id));
1290 END Get_global_proc_not_certified;
1291 
1292 
1293 
1294 PROCEDURE  Get_local_proc_not_certified
1295 (
1296     p_certification_id          IN    	NUMBER,
1297     x_local_proc_not_certified OUT NOCOPY Number
1298 )IS
1299 BEGIN
1300 
1301  	-- local_proc_not_certified
1302         SELECT count(1) INTO x_local_proc_not_certified
1303            FROM ( SELECT distinct proc.organization_id, proc.process_id
1304      	       FROM AMW_FIN_PROCESS_EVAL_SUM proc
1305      	       WHERE proc.fin_certification_id = p_certification_id
1306                    AND proc.organization_id <> NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'),-999)
1307                    AND not exists (SELECT 'Y'
1308                              FROM AMW_OPINION_MV aov,
1309                              	  AMW_FIN_PROC_CERT_RELAN rel
1310                              WHERE aov.object_name = 'AMW_ORG_PROCESS'
1311                               AND aov.opinion_type_code = 'CERTIFICATION'
1312                               AND aov.opinion_component_code = 'OVERALL'
1313                               AND aov.pk3_value = proc.organization_id
1314                               AND aov.pk2_value = rel.proc_cert_id
1315                               AND rel.fin_stmt_cert_id = p_certification_id
1316                               AND rel.end_date is null
1317                               AND aov.pk1_value = proc.process_id));
1318 
1319 
1320 END Get_local_proc_not_certified;
1321 
1322 PROCEDURE  Get_global_proc_with_issue
1323 (
1324     p_certification_id          IN    	NUMBER,
1325     x_global_proc_with_issue OUT NOCOPY Number
1326 )IS
1327 BEGIN
1328 
1329 ---- global_proc_with_issue
1330 	SELECT count(1) INTO x_global_proc_with_issue
1331  		FROM  (SELECT DISTINCT proc.organization_id, proc.process_id
1332  		       FROM AMW_FIN_PROCESS_EVAL_SUM proc,
1333  		            AMW_OPINION_MV aov,
1334  		            AMW_FIN_PROC_CERT_RELAN rel
1335  		       WHERE proc.fin_certification_id = p_certification_id
1336                    	AND proc.organization_id = NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'), -999)
1337                    	AND aov.object_name = 'AMW_ORG_PROCESS'
1338                    	AND aov.opinion_component_code = 'OVERALL'
1339                    	AND aov.opinion_type_code = 'CERTIFICATION'
1340                    	AND aov.pk3_value = proc.organization_id
1341                    	AND aov.pk2_value = rel.proc_cert_id
1342                    	AND rel.fin_stmt_cert_id = p_certification_id
1343                    	AND rel.end_date is null
1344                    	AND aov.pk1_value = proc.process_id
1345                    	AND aov.OPINION_VALUE_CODE <> 'EFFECTIVE'
1346                    	 AND aov.authored_date = (select max(aov2.authored_date)
1347                        	             from AMW_OPINIONS aov2
1348                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
1349                                      and aov2.pk3_value = aov.pk3_value
1350                                      and aov2.pk1_value = aov.pk1_value));
1351 END Get_global_proc_with_issue;
1352 
1353 PROCEDURE  Get_local_proc_with_issue
1354 (
1355     p_certification_id          IN    	NUMBER,
1356     x_local_proc_with_issue OUT NOCOPY Number
1357 )IS
1358 BEGIN
1359 	------local_proc_with_issue
1360         SELECT count(1) INTO x_local_proc_with_issue
1361           FROM  (SELECT DISTINCT proc.organization_id, proc.process_id
1362  		       FROM AMW_FIN_PROCESS_EVAL_SUM proc,
1363  		            AMW_OPINION_MV aov,
1364  		            AMW_FIN_PROC_CERT_RELAN rel
1365  		 WHERE proc.fin_certification_id = p_certification_id
1366                    	AND proc.organization_id <> NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'), -999)
1367                    	AND aov.object_name = 'AMW_ORG_PROCESS'
1368                    	AND aov.opinion_type_code = 'CERTIFICATION'
1369                    	AND aov.opinion_component_code = 'OVERALL'
1370                    	AND aov.pk3_value = proc.organization_id
1371                    	AND aov.pk2_value = rel.proc_cert_id
1372                    	AND rel.fin_stmt_cert_id = p_certification_id
1373                    	AND rel.end_date is null
1374                    	AND aov.pk1_value = proc.process_id
1375                    	AND aov.OPINION_VALUE_CODE <> 'EFFECTIVE'
1376                    	 AND aov.authored_date = (select max(aov2.authored_date)
1377                        	             from AMW_OPINIONS aov2
1378                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
1379                                      and aov2.pk3_value = aov.pk3_value
1380                                      and aov2.pk1_value = aov.pk1_value));
1381 END Get_local_proc_with_issue;
1382 
1383 
1384 PROCEDURE  Get_global_proc_ineff_ctrl
1385 (
1386     p_certification_id          IN    	NUMBER,
1387     x_global_proc_ineff_ctrl OUT NOCOPY Number
1388 ) IS
1389 BEGIN
1390 	-------------global_proc_with_ineff_ctrl IS
1391     	 SELECT count(1) INTO x_global_proc_ineff_ctrl
1392     	 from (select distinct proc.organization_id, proc.process_id
1393     	 FROM AMW_FIN_PROCESS_EVAL_SUM proc,
1394     	      AMW_OPINION_MV aov
1395     	 WHERE  proc.fin_certification_id = p_certification_id
1396     	 AND	proc.organization_id = aov.pk3_value
1397     	 AND	proc.process_id      = aov.pk1_value
1398     	 --fix bug 5724066
1399 	 AND    aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
1400     	 AND    proc.organization_id = NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'), -999)
1401     	 AND 	aov.object_name = 'AMW_ORG_PROCESS'
1402          AND 	aov.opinion_type_code = 'EVALUATION'
1403          AND    aov.opinion_component_code = 'OVERALL'
1404          AND 	aov.authored_date = (select max(aov2.authored_date)
1405 			      		   from AMW_OPINIONS aov2
1406 			      		  where aov2.object_opinion_type_id = aov.object_opinion_type_id
1407 			      		  and aov2.pk3_value = aov.pk3_value
1408                             		  		  and aov2.pk1_value = aov.pk1_value)
1409          AND	 aov.OPINION_VALUE_CODE <> 'EFFECTIVE');
1410 
1411 END Get_global_proc_ineff_ctrl;
1412 
1413 PROCEDURE  Get_local_proc_ineff_ctrl
1414 (
1415     p_certification_id          IN    	NUMBER,
1416     x_local_proc_ineff_ctrl OUT NOCOPY Number
1417 ) IS
1418 BEGIN
1419 	------ local_proc_with_ineff_ctrl
1420         SELECT count(1) INTO x_local_proc_ineff_ctrl
1421         from (select distinct proc.organization_id, proc.process_id
1422     	 FROM AMW_FIN_PROCESS_EVAL_SUM proc,
1423     	      AMW_OPINION_MV aov
1424     	 WHERE  proc.fin_certification_id = p_certification_id
1425     	 AND	proc.organization_id = aov.pk3_value
1426     	 AND	proc.process_id      = aov.pk1_value
1427     	 --fix bug 5724066
1428 	 AND    aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
1429     	 AND    proc.organization_id <>  NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'), -999)
1430     	 AND 	aov.object_name = 'AMW_ORG_PROCESS'
1431          AND 	aov.opinion_type_code = 'EVALUATION'
1432          AND 	aov.opinion_component_code = 'OVERALL'
1433          AND 	aov.authored_date = (select max(aov2.authored_date)
1434 			      		   from AMW_OPINIONS aov2
1435 			      		  where aov2.object_opinion_type_id = aov.object_opinion_type_id
1436 			      		  and aov2.pk3_value = aov.pk3_value
1437                             		  and aov2.pk1_value = aov.pk1_value)
1438          AND	 aov.OPINION_VALUE_CODE <> 'EFFECTIVE');
1439 END Get_local_proc_ineff_ctrl;
1440 
1441 PROCEDURE  Get_unmitigated_risks
1442 (
1443     p_certification_id          IN    	NUMBER,
1444     x_unmitigated_risks OUT NOCOPY Number
1445 )IS
1446 BEGIN
1447 	----unmitigated_risks IS
1448 	/*** remove due to ratio number mismatch
1449     	SELECT count(1)  INTO x_unmitigated_risks
1450     	from (select distinct fin.organization_id, fin.process_id, fin.risk_id
1451 	FROM 	AMW_OPINION_MV aov,
1452 		AMW_FIN_ITEM_ACC_RISK fin
1453 	WHERE
1454 		fin.object_type = 'FINANCIAL STATEMENT'
1455 	AND	fin.FIN_CERTIFICATION_ID = p_certification_id
1456 	AND	aov.AUTHORED_DATE in (select max(aov2.authored_date)
1457                        from AMW_OPINIONS aov2
1458                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
1459                        and aov2.pk1_value = aov.pk1_value
1460                        and aov2.pk3_value = aov.pk3_value
1461                        and aov2.pk4_value = aov.pk4_value)
1462 	AND aov.OPINION_TYPE_CODE = 'EVALUATION'
1463 	AND aov.object_name = 'AMW_ORG_PROCESS_RISK'
1464 	AND aov.opinion_component_code = 'OVERALL'
1465 	AND aov.pk1_value = fin.risk_id
1466 	AND aov.pk3_value = fin.organization_id
1467 	AND aov.pk4_value = fin.process_ID
1468 	--fix bug 5724066
1469 	AND aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
1470 	AND aov.OPINION_VALUE_CODE <> 'EFFECTIVE');
1471 	*******/
1472 
1473 	----unmitigated_risks IS
1474     	SELECT count(1)  INTO x_unmitigated_risks
1475 	from AMW_RISK_ASSOCIATIONS risks,
1476 	AMW_OPINION_LOG_MV op
1477 	where risks.pk1 = p_certification_id
1478 	and risks.pk4 = op.opinion_log_id (+)
1479 	and op.OPINION_VALUE_CODE <> 'EFFECTIVE'
1480 	and risks.object_type = 'PROCESS_FINCERT';
1481 
1482 END Get_unmitigated_risks;
1483 
1484 
1485 PROCEDURE  Get_ineffective_controls
1486 (
1487     p_certification_id          IN    	NUMBER,
1488     x_ineffective_controls	 OUT NOCOPY Number
1489 )IS
1490 BEGIN
1491 	---------ineffective_controls
1492       	/***************replace to use opinion_log_id in amw_fin_item_acc_ctrl table
1493       	SELECT count(1) INTO x_ineffective_controls
1494       	from(select distinct  fin.control_id, fin.organization_id
1495 	FROM 	AMW_OPINION_MV aov,
1496 		AMW_FIN_ITEM_ACC_CTRL fin
1497 	WHERE	fin.fin_certification_id = p_certification_id
1498 	AND	fin.object_type = 'FINANCIAL STATEMENT'
1499 	AND	 aov.pk1_value = fin.control_id
1500 	AND 	aov.pk3_value = fin.organization_id
1501 	--fix bug 5724066
1502 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
1503 	AND 	aov.OPINION_TYPE_CODE = 'EVALUATION'
1504 	AND 	aov.object_name = 'AMW_ORG_CONTROL'
1505 	AND 	aov.opinion_component_code = 'OVERALL'
1506 	AND	aov.AUTHORED_DATE in (select max(aov2.authored_date)
1507                        from AMW_OPINIONS aov2
1508                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
1509                        and aov2.pk1_value = aov.pk1_value
1510                        and aov2.pk3_value = aov.pk3_value)
1511 	AND	 aov.OPINION_VALUE_CODE <> 'EFFECTIVE');
1512 	**********************/
1513 	SELECT count(1) INTO x_ineffective_controls
1514       	from(select distinct  fin.control_id, fin.organization_id
1515 	FROM 	AMW_OPINION_LOG_MV aov,
1516 		AMW_FIN_ITEM_ACC_CTRL fin
1517 	WHERE	fin.fin_certification_id = p_certification_id
1518 	AND	fin.object_type = 'FINANCIAL STATEMENT'
1519 	AND     aov.opinion_log_id = fin.OPINION_LOG_ID
1520 	AND 	aov.OPINION_TYPE_CODE = 'EVALUATION'
1521 	AND 	aov.object_name = 'AMW_ORG_CONTROL'
1522 	AND 	aov.opinion_component_code = 'OVERALL'
1523 	AND	aov.AUTHORED_DATE in (select max(aov2.authored_date)
1524                        from AMW_OPINIONS aov2
1525                        where aov2.object_opinion_type_id = aov.object_opinion_type_id
1526                        and aov2.pk1_value = aov.pk1_value
1527                        and aov2.pk3_value = aov.pk3_value)
1528 	AND	 aov.OPINION_VALUE_CODE <> 'EFFECTIVE');
1529 
1530 
1531 END Get_ineffective_controls;
1532 
1533 PROCEDURE  Get_orgs_pending_in_scope
1534 (
1535     p_certification_id          IN    	NUMBER,
1536     x_orgs_pending_in_scope     OUT NOCOPY Number
1537 )IS
1538 BEGIN
1539 	-----orgs_pending_in_scope IS
1540         SELECT 	count(distinct fin.organization_id) INTO x_orgs_pending_in_scope
1541         FROM 	AMW_FIN_CERT_SCOPE fin
1542 	WHERE 	fin.FIN_CERTIFICATION_ID = p_certification_id
1543 	AND	fin.organization_id is not null
1544 	AND 	not exists ( SELECT 'Y'
1545                              FROM AMW_OPINION_MV aov
1546                              WHERE aov.object_name = 'AMW_ORG_PROCESS'
1547                              AND aov.opinion_type_code = 'CERTIFICATION'
1548                              AND aov.opinion_component_code = 'OVERALL'
1549                              AND aov.pk3_value = fin.organization_id
1550                              AND aov.pk2_value = p_certification_id
1551                              AND aov.pk1_value = fin.process_id);
1552 
1553 END Get_orgs_pending_in_scope;
1554 
1555 
1556 
1557  ----------------------------- ********************************** ----------------------
1558 procedure insert_fin_cert_eval_sum(
1559 p_api_version_number        IN   NUMBER   := 1.0,
1560 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
1561 p_commit                    IN   VARCHAR2 := FND_API.g_false,
1562 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
1563  X_FIN_CERTIFICATION_ID                       IN         NUMBER,
1564  X_FINANCIAL_STATEMENT_ID                     IN         NUMBER,
1565  X_FINANCIAL_ITEM_ID                          IN         NUMBER,
1566  X_ACCOUNT_GROUP_ID                           IN         NUMBER,
1567  X_NATURAL_ACCOUNT_ID                         IN         NUMBER,
1568  X_OBJECT_TYPE                                IN         VARCHAR,
1569  X_PROC_PENDING_CERTIFICATION                 IN         NUMBER,
1570  X_TOTAL_NUMBER_OF_PROCESSES                  IN         NUMBER,
1571  X_PROC_CERTIFIED_WITH_ISSUES                 IN         NUMBER,
1572  X_PROCS_FOR_CERT_DONE                        IN         NUMBER,
1573  x_proc_evaluated                             IN         NUMBER,
1574  X_ORG_WITH_INEFFECTIVE_CTRLS                 IN         NUMBER,
1575 -- X_ORG_CERTIFIED                            IN         NUMBER,
1576  x_orgs_FOR_CERT_DONE                         IN         NUMBER,
1577  x_orgs_evaluated                             IN         NUMBER,
1578   x_total_orgs			 IN         NUMBER,
1579  X_PROC_WITH_INEFFECTIVE_CTRLS                IN         NUMBER,
1580  X_UNMITIGATED_RISKS                          IN         NUMBER,
1581  X_RISKS_VERIFIED                             IN         NUMBER,
1582   X_TOTAL_RISKS			 IN         NUMBER,
1583  X_INEFFECTIVE_CONTROLS                       IN         NUMBER,
1584  X_CONTROLS_VERIFIED                          IN         NUMBER,
1585   X_TOTAL_CONTROLS			IN         NUMBER,
1586  X_OPEN_ISSUES                                IN         NUMBER,
1587  X_PRO_PENDING_CERT_PRCNT                     IN         NUMBER,
1588  X_PROCESSES_WITH_ISSUES_PRCNT                IN         NUMBER,
1589  X_ORG_WITH_INEFF_CTRLS_PRCNT                 IN         NUMBER,
1590  X_PROC_WITH_INEFF_CTRLS_PRCNT                IN         NUMBER,
1591  X_UNMITIGATED_RISKS_PRCNT                    IN         NUMBER,
1592  X_INEFFECTIVE_CTRLS_PRCNT                    IN         NUMBER,
1593  X_OBJ_CONTEXT                                IN         NUMBER,
1594  X_CREATED_BY                                 IN         NUMBER,
1595  X_CREATION_DATE                              IN         DATE,
1596  X_LAST_UPDATED_BY                            IN         NUMBER,
1597  X_LAST_UPDATE_DATE                           IN         DATE,
1598  X_LAST_UPDATE_LOGIN                          IN         NUMBER,
1599  X_SECURITY_GROUP_ID                          IN         NUMBER,
1600  X_OBJECT_VERSION_NUMBER                      IN         NUMBER,
1601  x_return_status             OUT  nocopy VARCHAR2,
1602 x_msg_count                 OUT  nocopy NUMBER,
1603 x_msg_data                  OUT  nocopy VARCHAR2
1604  )
1605 IS
1606 M_COUNT NUMBER := 0;
1607 
1608 
1609 l_api_name           CONSTANT VARCHAR2(30) := 'insert_fin_cert_eval_sum';
1610 l_api_version_number CONSTANT NUMBER       := 1.0;
1611 
1612 l_return_status VARCHAR2(32767);
1613 l_msg_count NUMBER;
1614 l_msg_data VARCHAR2(32767);
1615 
1616 BEGIN
1617 
1618 SAVEPOINT insert_fin_cert_eval_sum;
1619 
1620  -- Standard call to check for call compatibility.
1621         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
1622                                              p_api_version_number,
1623                                              l_api_name,
1624                                              G_PKG_NAME)
1625         THEN
1626                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1627         END IF;
1628 
1629         -- Initialize message list if p_init_msg_list is set to TRUE.
1630         IF FND_API.to_Boolean( p_init_msg_list )
1631         THEN
1632                 FND_MSG_PUB.initialize;
1633         END IF;
1634 
1635         -- Initialize API return status to SUCCESS
1636         x_return_status := FND_API.G_RET_STS_SUCCESS;
1637 
1638 SELECT COUNT(1) INTO M_COUNT FROM amw_fin_cert_eval_sum
1639         WHERE FIN_CERTIFICATION_ID = X_FIN_CERTIFICATION_ID
1640         AND FINANCIAL_STATEMENT_ID = X_FINANCIAL_STATEMENT_ID
1641         AND NVL(FINANCIAL_ITEM_ID,0) = NVL(X_FINANCIAL_ITEM_ID,0)
1642         AND NVL(NATURAL_ACCOUNT_ID,0)     = NVL(X_NATURAL_ACCOUNT_ID,0)
1643         AND NVL(ACCOUNT_GROUP_ID,0)       = NVL(X_ACCOUNT_GROUP_ID,0)
1644         AND OBJECT_TYPE            = X_OBJECT_TYPE;
1645 
1646 
1647  IF (M_COUNT is null or M_COUNT = 0) then
1648 insert into amw_fin_cert_eval_sum(
1649 FIN_CERTIFICATION_ID                   ,
1650 FINANCIAL_STATEMENT_ID                 ,
1651 FINANCIAL_ITEM_ID                      ,
1652 NATURAL_ACCOUNT_ID                     ,
1653 ACCOUNT_GROUP_ID                       ,
1654 OBJECT_TYPE                            ,
1655 PROC_PENDING_CERTIFICATION             ,
1656 TOTAL_NUMBER_OF_PROCESSES              ,
1657 PROC_CERTIFIED_WITH_ISSUES             ,
1658 PROCS_FOR_CERT_DONE                   ,
1659 proc_evaluated                        ,
1660 ORG_WITH_INEFFECTIVE_CONTROLS          ,
1661 orgs_FOR_CERT_DONE                    ,
1662 --org_certified                         ,
1663 orgs_evaluated                         ,
1664 total_number_of_orgs  ,
1665 PROC_WITH_INEFFECTIVE_CONTROLS         ,
1666 UNMITIGATED_RISKS                      ,
1667 RISKS_VERIFIED                         ,
1668 TOTAL_NUMBER_OF_RISKS ,
1669 INEFFECTIVE_CONTROLS                   ,
1670 CONTROLS_VERIFIED                      ,
1671 TOTAL_NUMBER_OF_CTRLS	,
1672 OPEN_ISSUES                            ,
1673 PRO_PENDING_CERT_PRCNT                 ,
1674 PROCESSES_WITH_ISSUES_PRCNT            ,
1675 ORG_WITH_INEFF_CONTROLS_PRCNT  ,
1676 PROC_WITH_INEFF_CONTROLS_PRCNT         ,
1677 UNMITIGATED_RISKS_PRCNT                ,
1678 INEFFECTIVE_CONTROLS_PRCNT             ,
1679 OBJ_CONTEXT                            ,
1680 CREATED_BY                             ,
1681 CREATION_DATE                          ,
1682 LAST_UPDATED_BY                        ,
1683 LAST_UPDATE_DATE                       ,
1684 LAST_UPDATE_LOGIN                      ,
1685 SECURITY_GROUP_ID                      ,
1686 OBJECT_VERSION_NUMBER
1687 )
1688 values
1689 (
1690  X_FIN_CERTIFICATION_ID,
1691  X_FINANCIAL_STATEMENT_ID,
1692  X_FINANCIAL_ITEM_ID     ,
1693  X_NATURAL_ACCOUNT_ID    ,
1694  X_ACCOUNT_GROUP_ID      ,
1695  X_OBJECT_TYPE           ,
1696  X_PROC_PENDING_CERTIFICATION,
1697  X_TOTAL_NUMBER_OF_PROCESSES ,
1698  X_PROC_CERTIFIED_WITH_ISSUES,
1699 X_TOTAL_NUMBER_OF_PROCESSES ,
1700 -- X_PROCS_FOR_CERT_DONE  -- was replaced by total processes,
1701 X_PROC_EVALUATED         ,
1702  X_ORG_WITH_INEFFECTIVE_CTRLS,
1703  --X_ORG_CERTIFIED             ,
1704  x_orgs_FOR_CERT_DONE                     ,
1705  x_orgs_evaluated                         ,
1706  x_total_orgs	,
1707  X_PROC_WITH_INEFFECTIVE_CTRLS,
1708  X_UNMITIGATED_RISKS          ,
1709  X_RISKS_VERIFIED             ,
1710  X_TOTAL_RISKS	,
1711  X_INEFFECTIVE_CONTROLS       ,
1712  X_CONTROLS_VERIFIED          ,
1713  X_TOTAL_CONTROLS	,
1714  X_OPEN_ISSUES                ,
1715  round(nvl(x_proc_pending_certification, 0) / decode(nvl(x_total_number_of_processes, 0), 0, 1, x_total_number_of_processes), 2) * 100,
1716  round(nvl(x_proc_certified_with_issues, 0)/ decode(nvl(x_total_number_of_processes, 0), 0, 1, x_total_number_of_processes), 2) * 100,
1717  round(nvl(x_org_with_ineffective_ctrls, 0) / decode(nvl(x_total_orgs, 0), 0, 1, x_total_orgs), 2) * 100,
1718  round(nvl(x_proc_with_ineffective_ctrls, 0) / decode(nvl(x_total_number_of_processes, 0), 0, 1, x_total_number_of_processes), 2) * 100,
1719  round(nvl(x_unmitigated_risks, 0) / decode(nvl(x_total_risks, 0), 0, 1, x_total_risks), 2)* 100,
1720  round(nvl(x_ineffective_controls, 0) / decode(nvl(x_total_controls, 0), 0, 1,  x_total_controls), 2)* 100,
1721  X_OBJ_CONTEXT                ,
1722  X_CREATED_BY                 ,
1723  X_CREATION_DATE              ,
1724  X_LAST_UPDATED_BY            ,
1725  X_LAST_UPDATE_DATE           ,
1726  X_LAST_UPDATE_LOGIN          ,
1727  X_SECURITY_GROUP_ID          ,
1728  X_OBJECT_VERSION_NUMBER
1729 );
1730 
1731 else -- update
1732 
1733   update amw_fin_cert_eval_sum set
1734   FIN_CERTIFICATION_ID
1735 = X_FIN_CERTIFICATION_ID,
1736 FINANCIAL_STATEMENT_ID
1737  = X_FINANCIAL_STATEMENT_ID,
1738 FINANCIAL_ITEM_ID
1739  = X_FINANCIAL_ITEM_ID     ,
1740 NATURAL_ACCOUNT_ID
1741  = X_NATURAL_ACCOUNT_ID,
1742 ACCOUNT_GROUP_ID
1743  = X_ACCOUNT_GROUP_ID      ,
1744 OBJECT_TYPE
1745  = X_OBJECT_TYPE           ,
1746 PROC_PENDING_CERTIFICATION
1747  = X_PROC_PENDING_CERTIFICATION,
1748 TOTAL_NUMBER_OF_PROCESSES
1749  = X_TOTAL_NUMBER_OF_PROCESSES,
1750 PROC_CERTIFIED_WITH_ISSUES
1751  = X_PROC_CERTIFIED_WITH_ISSUES,
1752 PROCS_FOR_CERT_DONE
1753 = X_TOTAL_NUMBER_OF_PROCESSES ,
1754 -- X_PROCS_FOR_CERT_DONE  -- was replaced by total processes
1755 PROC_EVALUATED
1756 = X_PROC_EVALUATED ,
1757 ORG_WITH_INEFFECTIVE_CONTROLS
1758  = X_ORG_WITH_INEFFECTIVE_CTRLS,
1759 orgs_FOR_CERT_DONE
1760  = X_orgs_FOR_CERT_DONE                     ,
1761 orgs_evaluated
1762  = X_orgs_evaluated                         ,
1763  total_number_of_orgs
1764  = x_total_orgs	,
1765 PROC_WITH_INEFFECTIVE_CONTROLS
1766 = X_PROC_WITH_INEFFECTIVE_CTRLS,
1767 UNMITIGATED_RISKS
1768  = X_UNMITIGATED_RISKS          ,
1769 RISKS_VERIFIED
1770  = X_RISKS_VERIFIED             ,
1771  TOTAL_NUMBER_OF_RISKS
1772  =  X_TOTAL_RISKS,
1773 INEFFECTIVE_CONTROLS
1774  = X_INEFFECTIVE_CONTROLS,
1775 CONTROLS_VERIFIED
1776  = X_CONTROLS_VERIFIED        ,
1777  TOTAL_NUMBER_OF_CTRLS
1778  = X_TOTAL_CONTROLS	,
1779 OPEN_ISSUES
1780  = X_OPEN_ISSUES                ,
1781 pro_pending_cert_prcnt= round(nvl(x_proc_pending_certification, 0) / decode(nvl(x_total_number_of_processes, 0), 0, 1,x_total_number_of_processes), 2) * 100,
1782 processes_with_issues_prcnt= round(nvl(x_proc_certified_with_issues, 0)/ decode(nvl(x_total_number_of_processes, 0), 0, 1, x_total_number_of_processes), 2) * 100,
1783 org_with_ineff_controls_prcnt =  round(nvl(x_org_with_ineffective_ctrls, 0) / decode(nvl(x_total_orgs, 0), 0, 1, x_total_orgs), 2) * 100,
1784 proc_with_ineff_controls_prcnt= round(nvl(x_proc_with_ineffective_ctrls, 0) / decode(nvl(x_total_number_of_processes, 0), 0, 1, x_total_number_of_processes), 2) * 100,
1785 unmitigated_risks_prcnt =  round(nvl(x_unmitigated_risks, 0) / decode(nvl(x_total_risks, 0), 0, 1, x_total_risks), 2)* 100,
1786 ineffective_controls_prcnt  = round(nvl(x_ineffective_controls, 0) / decode(nvl(x_total_controls, 0), 0, 1,  x_total_controls), 2)* 100,
1787 OBJ_CONTEXT
1788  = X_OBJ_CONTEXT                ,
1789 CREATED_BY
1790  = X_CREATED_BY                 ,
1791 CREATION_DATE
1792  = X_CREATION_DATE           ,
1793 LAST_UPDATED_BY
1794  = X_LAST_UPDATED_BY      ,
1795 LAST_UPDATE_DATE
1796  = X_LAST_UPDATE_DATE     ,
1797 LAST_UPDATE_LOGIN
1798  = X_LAST_UPDATE_LOGIN    ,
1799 SECURITY_GROUP_ID
1800  = X_SECURITY_GROUP_ID      ,
1801 OBJECT_VERSION_NUMBER
1802  = X_OBJECT_VERSION_NUMBER
1803  WHERE FIN_CERTIFICATION_ID = X_FIN_CERTIFICATION_ID
1804    AND FINANCIAL_STATEMENT_ID = X_FINANCIAL_STATEMENT_ID
1805         AND NVL(FINANCIAL_ITEM_ID,0) = NVL(X_FINANCIAL_ITEM_ID,0)
1806         AND NVL(NATURAL_ACCOUNT_ID,0)     = NVL(X_NATURAL_ACCOUNT_ID,0)
1807         AND NVL(ACCOUNT_GROUP_ID,0)       = NVL(X_ACCOUNT_GROUP_ID,0)
1808         AND OBJECT_TYPE            = X_OBJECT_TYPE;
1809  end if;
1810 
1811  x_return_status := FND_API.G_RET_STS_SUCCESS;
1812 
1813  EXCEPTION
1814      WHEN NO_DATA_FOUND THEN
1815      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
1816      x_return_status := FND_API.G_RET_STS_ERROR;
1817      x_msg_count := 1;
1818      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
1819       WHEN OTHERS THEN
1820        ROLLBACK TO insert_fin_cert_eval_sum;
1821       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
1822       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
1823         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1824         x_msg_count := 1;
1825         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
1826                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
1827                 FND_MSG_PUB.Count_And_Get(
1828                 p_encoded =>  FND_API.G_FALSE,
1829                 p_count   =>  x_msg_count,
1830                 p_data    =>  x_msg_data);
1831 
1832 END insert_fin_cert_eval_sum;
1833 
1834 
1835 
1836  ----------------------------- ********************************** ----------------------
1837 -- this procedure build a flat table for financial certification, item, account, process
1838 -- it contains the whole structure of financial information no matter weather process associated to it or not
1839 -- it contains 3 layesr joins for 4 situations.
1840 -- layer 1: join certification  with fin_stmnt_items
1841 -- layter 2: join resultset of layer1 with key_account
1842 -- layer 3: join resultset of layer2 with process hierarchy via account_association
1843 
1844 --situation 1: accounts have one or more children linking to a process
1845 --situation 2: account directly assocates with a process and also links to one or more financial items
1846 --situation 3: --- add all of childen accounts which associate with the top account which directly links to an item
1847 --- e.g A2 is a child of A1. A1 links to an financial item which relates to fin certification
1848 --  and P1 is associated with A2. so we want to add one record which contains A2, P1 info. in scope table
1849 
1850 --situation 4:
1851 -- account has sub-account, but account itself doesn't associate with any item. His parent/parent's parent links to -- -- item.
1852 --- and its sub-account links to a process. e.g A1-A2-A3, A3-P1. so this query make A2-P
1853 
1854 ---for performance reason, we split a big query into 4 queries based on 4 situtation.
1855 ----------------------------- ********************************** ----------------------
1856 
1857 PROCEDURE INSERT_FIN_CERT_SCOPE(
1858 p_api_version_number        IN   NUMBER   := 1.0,
1859 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
1860 p_commit                    IN   VARCHAR2 := FND_API.g_false,
1861 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
1862 p_certification_id  IN       NUMBER,
1863 x_return_status             OUT  nocopy VARCHAR2,
1864 x_msg_count                 OUT  nocopy NUMBER,
1865 x_msg_data                  OUT  nocopy VARCHAR2
1866 ) IS
1867 
1868 L_COUNT NUMBER;
1869 L_COUNT2 NUMBER;
1870 l_api_name           CONSTANT VARCHAR2(30) := 'INSERT_FIN_CERT_SCOPE';
1871 l_api_version_number CONSTANT NUMBER       := 1.0;
1872 
1873 l_return_status VARCHAR2(32767);
1874 l_msg_count NUMBER;
1875 l_msg_data VARCHAR2(32767);
1876 
1877 BEGIN
1878 
1879 SAVEPOINT INSERT_FIN_CERT_SCOPE;
1880 
1881  -- Standard call to check for call compatibility.
1882         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
1883                                              p_api_version_number,
1884                                              l_api_name,
1885                                              G_PKG_NAME)
1886         THEN
1887                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1888         END IF;
1889 
1890         -- Initialize message list if p_init_msg_list is set to TRUE.
1891         IF FND_API.to_Boolean( p_init_msg_list )
1892         THEN
1893                 FND_MSG_PUB.initialize;
1894         END IF;
1895 
1896         -- Initialize API return status to SUCCESS
1897         x_return_status := FND_API.G_RET_STS_SUCCESS;
1898 
1899 SELECT COUNT(1) INTO L_COUNT FROM AMW_FIN_CERT_SCOPE
1900 WHERE FIN_CERTIFICATION_ID = P_CERTIFICATION_ID;
1901 
1902 /** even if there is no process attached to an account. if the account belongs to the financial
1903 **  certification, we should add it to the amw_fin_cert_scope table
1904 SELECT COUNT(1) INTO L_COUNT2 FROM AMW_FIN_PROCESS_EVAL_SUM
1905 WHERE FIN_CERTIFICATION_ID = P_CERTIFICATION_ID;
1906 
1907 
1908 IF (L_COUNT2 = 0 OR L_COUNT2 IS NULL) THEN
1909 RETURN;
1910 END IF;
1911 ****/
1912 
1913 IF (L_COUNT = 0 OR L_COUNT IS NULL) THEN
1914 ----add those accounts that have one or more children linking to a process
1915 insert into amw_fin_cert_scope(
1916 FIN_CERT_SCOPE_ID ,
1917 FIN_CERTIFICATION_ID ,
1918 STATEMENT_GROUP_ID ,
1919 FINANCIAL_STATEMENT_ID,
1920 FINANCIAL_ITEM_ID,
1921 ACCOUNT_GROUP_ID ,
1922 NATURAL_ACCOUNT_ID                     ,
1923 ORGANIZATION_ID				,
1924 PROCESS_ID				,
1925 CREATED_BY                             ,
1926 CREATION_DATE                          ,
1927 LAST_UPDATED_BY                        ,
1928 LAST_UPDATE_DATE                       ,
1929 LAST_UPDATE_LOGIN                      ,
1930 SECURITY_GROUP_ID                      ,
1931 OBJECT_VERSION_NUMBER )
1932 SELECT AMW_FIN_CERT_SCOPE_S.NEXTVAL, P_CERTIFICATION_ID, itemaccmerge.statement_group_id, itemaccmerge.financial_statement_id, itemaccmerge.financial_item_id,
1933 itemaccmerge.account_group_id, itemaccmerge.natural_account_id,itemaccmerge.organization_id, case when proc.child_process_id = -2 then itemaccmerge.process_id else proc.child_process_id end process_id,
1934 1, sysdate, 1, sysdate, 1, null, 1
1935 FROM
1936 	AMW_FIN_PROCESS_FLAT proc,
1937 
1938 	(SELECT temp.STATEMENT_GROUP_ID, temp.FINANCIAL_STATEMENT_ID, temp.FINANCIAL_ITEM_ID,
1939  		temp.ACCOUNT_GROUP_ID,
1940  		case when temp.NATURAL_ACCOUNT_ID = -1 then temp.child_natural_account_id else temp.NATURAL_ACCOUNT_ID end natural_account_id,
1941  		ACCREL.PK1 organization_id, ACCREL.PK2 process_id
1942  	 FROM
1943  		(SELECT NATURAL_ACCOUNT_ID, PK1, PK2 FROM AMW_ACCT_ASSOCIATIONS
1944  		 WHERE OBJECT_TYPE = 'PROCESS_ORG'
1945  		 AND APPROVAL_DATE IS NOT NULL
1946  		 AND DELETION_APPROVAL_DATE IS NULL
1947  		 ) ACCREL,
1948 
1949 	   	 (select temp2.statement_group_id, temp2.financial_statement_id, temp2.financial_item_id,
1950  		  temp2.account_group_id, temp2.natural_account_id, flat.child_natural_account_id
1951 		  from AMW_FIN_KEY_ACCT_FLAT flat,
1952  		  (select distinct temp1.statement_group_id, temp1.financial_statement_id,
1953 		   case when temp1.financial_item_id = -1 then temp1.child_financial_item_id
1954 		   else temp1.financial_item_id end financial_item_id, itemaccrel.account_group_id, itemaccrel.natural_account_id
1955 		  from  AMW_FIN_ITEMS_KEY_ACC ITEMACCREL,
1956 
1957       			(select -1 financial_item_id, itemb.financial_item_id child_financial_item_id, itemb.statement_group_id, itemb.financial_statement_id
1958 			FROM AMW_CERTIFICATION_B cert,
1959      	     		     AMW_FIN_STMNT_ITEMS_B itemb
1960 			WHERE cert.CERTIFICATION_ID = P_CERTIFICATION_ID
1961 			and cert.statement_group_id = itemb.statement_group_id
1962 			and cert.financial_statement_id = itemb.financial_statement_id
1963 		UNION ALL
1964 			select itemb.financial_item_id, itemflat.child_financial_item_id, itemb.statement_group_id, itemb.financial_statement_id
1965 			from AMW_FIN_ITEM_FLAT itemflat,
1966      	     		        AMW_FIN_STMNT_ITEMS_B itemb,
1967              		     	        AMW_CERTIFICATION_B cert
1968 			where
1969 				cert.CERTIFICATION_ID = P_CERTIFICATION_ID
1970 				and cert.statement_group_id = itemb.statement_group_id
1971 				and cert.financial_statement_id = itemb.financial_statement_id
1972 				and itemflat.parent_financial_item_id = itemb.financial_item_id
1973 				and itemflat.statement_group_id = itemb.statement_group_id
1974 				and itemflat.financial_statement_id = itemb.financial_statement_id) temp1
1975 			where
1976     				temp1.statement_group_id = ITEMACCREL.statement_group_id (+)
1977    				and temp1.financial_statement_id = ITEMACCREL.financial_statement_id (+)
1978    				and temp1.child_financial_item_id = ITEMACCREL.financial_item_id (+)) temp2
1979  		   where temp2.account_group_id = flat.account_group_id
1980  		   and temp2.natural_account_id = flat.parent_natural_account_id) temp
1981  	WHERE
1982  		ACCREL.NATURAL_ACCOUNT_ID  = temp.CHILD_NATURAL_ACCOUNT_ID) itemaccmerge
1983 		-- only insert those account whose childen have a link to the process
1984 		--ACCREL.NATURAL_ACCOUNT_ID (+) = temp.CHILD_NATURAL_ACCOUNT_ID) itemaccmerge
1985 WHERE proc.organization_id (+) = itemaccmerge.organization_id
1986 and proc.parent_process_id (+) = itemaccmerge.process_id
1987 and proc.fin_certification_id (+) = p_certification_id;
1988 
1989 -- add account which has link to a item and also directly assocates with a process
1990 insert into amw_fin_cert_scope(
1991 FIN_CERT_SCOPE_ID ,
1992 FIN_CERTIFICATION_ID ,
1993 STATEMENT_GROUP_ID,
1994 FINANCIAL_STATEMENT_ID ,
1995 FINANCIAL_ITEM_ID,
1996 ACCOUNT_GROUP_ID                       ,
1997 NATURAL_ACCOUNT_ID                     ,
1998 ORGANIZATION_ID				,
1999 PROCESS_ID				,
2000 CREATED_BY                             ,
2001 CREATION_DATE                          ,
2002 LAST_UPDATED_BY                        ,
2003 LAST_UPDATE_DATE                       ,
2004 LAST_UPDATE_LOGIN                      ,
2005 SECURITY_GROUP_ID                      ,
2006 OBJECT_VERSION_NUMBER )
2007 SELECT AMW_FIN_CERT_SCOPE_S.NEXTVAL, P_CERTIFICATION_ID, itemaccmerge.statement_group_id, itemaccmerge.financial_statement_id, itemaccmerge.financial_item_id,
2008 itemaccmerge.account_group_id, itemaccmerge.natural_account_id,itemaccmerge.organization_id, case when proc.child_process_id = -2 then itemaccmerge.process_id else proc.child_process_id end process_id,
2009 1, sysdate, 1, sysdate, 1, null, 1
2010 FROM
2011 	AMW_FIN_PROCESS_FLAT proc,
2012 
2013 	(SELECT temp.STATEMENT_GROUP_ID, temp.FINANCIAL_STATEMENT_ID, temp.FINANCIAL_ITEM_ID,
2014  temp.ACCOUNT_GROUP_ID,temp.NATURAL_ACCOUNT_ID,ACCREL.PK1 organization_id, ACCREL.PK2 process_id
2015  	 FROM
2016  		(SELECT NATURAL_ACCOUNT_ID, PK1, PK2 FROM AMW_ACCT_ASSOCIATIONS
2017  		 WHERE OBJECT_TYPE = 'PROCESS_ORG'
2018  		 AND APPROVAL_DATE IS NOT NULL
2019  		 AND DELETION_APPROVAL_DATE IS NULL
2020  		 ) ACCREL,
2021 
2022 	   	(select distinct temp1.statement_group_id, temp1.financial_statement_id,
2023 		   case when temp1.financial_item_id = -1 then temp1.child_financial_item_id
2024 		   else temp1.financial_item_id end financial_item_id, itemaccrel.account_group_id, itemaccrel.natural_account_id
2025 		  from  AMW_FIN_ITEMS_KEY_ACC ITEMACCREL,
2026 
2027       			(select -1 financial_item_id, itemb.financial_item_id child_financial_item_id, itemb.statement_group_id, itemb.financial_statement_id
2028 			FROM AMW_CERTIFICATION_B cert,
2029      	     		     AMW_FIN_STMNT_ITEMS_B itemb
2030 			WHERE cert.CERTIFICATION_ID = P_CERTIFICATION_ID
2031 			and cert.statement_group_id = itemb.statement_group_id
2032 			and cert.financial_statement_id = itemb.financial_statement_id
2033 		UNION ALL
2034 			select itemb.financial_item_id, itemflat.child_financial_item_id, itemb.statement_group_id, itemb.financial_statement_id
2035 			from AMW_FIN_ITEM_FLAT itemflat,
2036      	     		        AMW_FIN_STMNT_ITEMS_B itemb,
2037              		     	        AMW_CERTIFICATION_B cert
2038 			where
2039 				cert.CERTIFICATION_ID = P_CERTIFICATION_ID
2040 				and cert.statement_group_id = itemb.statement_group_id
2041 				and cert.financial_statement_id = itemb.financial_statement_id
2042 				and itemflat.parent_financial_item_id = itemb.financial_item_id
2043 				and itemflat.statement_group_id = itemb.statement_group_id
2044 				and itemflat.financial_statement_id = itemb.financial_statement_id) temp1
2045 			where
2046     				temp1.statement_group_id = ITEMACCREL.statement_group_id (+)
2047    				and temp1.financial_statement_id = ITEMACCREL.financial_statement_id (+)
2048    				and temp1.child_financial_item_id = ITEMACCREL.financial_item_id (+)) temp
2049  	WHERE
2050 		ACCREL.NATURAL_ACCOUNT_ID (+) = temp.NATURAL_ACCOUNT_ID) itemaccmerge
2051 WHERE proc.organization_id (+) = itemaccmerge.organization_id
2052 and proc.parent_process_id (+) = itemaccmerge.process_id
2053 and proc.fin_certification_id (+) = p_certification_id;
2054 
2055 --- add all of childen accounts which associate with the top account which directly links to an item
2056 --- e.g A2 is a child of A1. A1 links to an financial item which relates to fin certification
2057 --  and P1 is associated with A2. so we want to add one record which contains A2, P1 info. in scope table
2058 insert into amw_fin_cert_scope(
2059 FIN_CERT_SCOPE_ID 			,
2060 FIN_CERTIFICATION_ID                   ,
2061 STATEMENT_GROUP_ID		       ,
2062 FINANCIAL_STATEMENT_ID                 ,
2063 FINANCIAL_ITEM_ID                      ,
2064 ACCOUNT_GROUP_ID                       ,
2065 NATURAL_ACCOUNT_ID                     ,
2066 ORGANIZATION_ID				,
2067 PROCESS_ID				,
2068 CREATED_BY                             ,
2069 CREATION_DATE                          ,
2070 LAST_UPDATED_BY                        ,
2071 LAST_UPDATE_DATE                       ,
2072 LAST_UPDATE_LOGIN                      ,
2073 SECURITY_GROUP_ID                      ,
2074 OBJECT_VERSION_NUMBER )
2075 SELECT AMW_FIN_CERT_SCOPE_S.NEXTVAL, P_CERTIFICATION_ID, itemaccmerge.statement_group_id, itemaccmerge.financial_statement_id, itemaccmerge.financial_item_id,
2076 itemaccmerge.account_group_id, itemaccmerge.natural_account_id,itemaccmerge.organization_id, case when proc.child_process_id = -2 then itemaccmerge.process_id else proc.child_process_id end process_id,
2077 1, sysdate, 1, sysdate, 1, null, 1
2078 FROM
2079 	AMW_FIN_PROCESS_FLAT proc,
2080 
2081 	(SELECT temp.STATEMENT_GROUP_ID, temp.FINANCIAL_STATEMENT_ID, temp.FINANCIAL_ITEM_ID,
2082  		temp.ACCOUNT_GROUP_ID,
2083  		temp.child_natural_account_id natural_account_id,
2084  		ACCREL.PK1 organization_id, ACCREL.PK2 process_id
2085  	 FROM
2086  		(SELECT NATURAL_ACCOUNT_ID, PK1, PK2 FROM AMW_ACCT_ASSOCIATIONS
2087  		 WHERE OBJECT_TYPE = 'PROCESS_ORG'
2088  		 AND APPROVAL_DATE IS NOT NULL
2089  		 AND DELETION_APPROVAL_DATE IS NULL
2090  		 ) ACCREL,
2091 
2092 	   	(select temp2.statement_group_id, temp2.financial_statement_id, temp2.financial_item_id,
2093   		flat.account_group_id, flat.child_natural_account_id
2094   		from AMW_FIN_KEY_ACCT_FLAT flat,
2095  		        (select distinct temp1.statement_group_id, temp1.financial_statement_id,
2096 		   case when temp1.financial_item_id = -1 then temp1.child_financial_item_id
2097 		   else temp1.financial_item_id end financial_item_id, itemaccrel.account_group_id, itemaccrel.natural_account_id
2098 		  from  AMW_FIN_ITEMS_KEY_ACC ITEMACCREL,
2099 
2100       			(select -1 financial_item_id, itemb.financial_item_id child_financial_item_id, itemb.statement_group_id, itemb.financial_statement_id
2101 			FROM AMW_CERTIFICATION_B cert,
2102      	     		     AMW_FIN_STMNT_ITEMS_B itemb
2103 			WHERE cert.CERTIFICATION_ID = P_CERTIFICATION_ID
2104 			and cert.statement_group_id = itemb.statement_group_id
2105 			and cert.financial_statement_id = itemb.financial_statement_id
2106 		UNION ALL
2107 			select itemb.financial_item_id, itemflat.child_financial_item_id, itemb.statement_group_id, itemb.financial_statement_id
2108 			from AMW_FIN_ITEM_FLAT itemflat,
2109      	     		        AMW_FIN_STMNT_ITEMS_B itemb,
2110              		     	        AMW_CERTIFICATION_B cert
2111 			where
2112 				cert.CERTIFICATION_ID = P_CERTIFICATION_ID
2113 				and cert.statement_group_id = itemb.statement_group_id
2114 				and cert.financial_statement_id = itemb.financial_statement_id
2115 				and itemflat.parent_financial_item_id = itemb.financial_item_id
2116 				and itemflat.statement_group_id = itemb.statement_group_id
2117 				and itemflat.financial_statement_id = itemb.financial_statement_id) temp1
2118 			where
2119     				temp1.statement_group_id = ITEMACCREL.statement_group_id (+)
2120    				and temp1.financial_statement_id = ITEMACCREL.financial_statement_id (+)
2121    				and temp1.child_financial_item_id = ITEMACCREL.financial_item_id (+))temp2
2122  		where temp2.account_group_id = flat.account_group_id
2123  		and temp2.natural_account_id = flat.parent_natural_account_id) temp
2124  	WHERE
2125 		ACCREL.NATURAL_ACCOUNT_ID (+)  = temp.CHILD_NATURAL_ACCOUNT_ID)  itemaccmerge
2126 WHERE proc.organization_id (+) = itemaccmerge.organization_id
2127 and proc.parent_process_id (+) = itemaccmerge.process_id
2128 and proc.fin_certification_id (+) = p_certification_id;
2129 
2130 
2131 
2132 -- account has sub-account, but account itself doesn't associate with any item. His parent/parent's parent links to -- -- item.
2133 --- and its sub-account links to a process. e.g A1-A2-A3, A3-P1. so this query make A2-P
2134 insert into amw_fin_cert_scope(
2135 FIN_CERT_SCOPE_ID 			,
2136 FIN_CERTIFICATION_ID                   ,
2137 STATEMENT_GROUP_ID		       ,
2138 FINANCIAL_STATEMENT_ID                 ,
2139 FINANCIAL_ITEM_ID                      ,
2140 ACCOUNT_GROUP_ID                       ,
2141 NATURAL_ACCOUNT_ID                     ,
2142 ORGANIZATION_ID				,
2143 PROCESS_ID				,
2144 CREATED_BY                             ,
2145 CREATION_DATE                          ,
2146 LAST_UPDATED_BY                        ,
2147 LAST_UPDATE_DATE                       ,
2148 LAST_UPDATE_LOGIN                      ,
2149 SECURITY_GROUP_ID                      ,
2150 OBJECT_VERSION_NUMBER )
2151 SELECT AMW_FIN_CERT_SCOPE_S.NEXTVAL, P_CERTIFICATION_ID, null statement_group_id, null financial_statement_id,
2152 null financial_item_id,
2153 itemaccmerge.account_group_id, itemaccmerge.natural_account_id,itemaccmerge.organization_id,
2154 case when proc.child_process_id = -2 then itemaccmerge.process_id else proc.child_process_id end process_id,
2155 1, sysdate, 1, sysdate, 1, null, 1
2156 FROM
2157 	AMW_FIN_PROCESS_FLAT proc,
2158 
2159 	(SELECT temp.ACCOUNT_GROUP_ID,
2160  		temp.NATURAL_ACCOUNT_ID,
2161  		ACCREL.PK1 organization_id, ACCREL.PK2 process_id
2162  	 FROM
2163  		(SELECT NATURAL_ACCOUNT_ID, PK1, PK2 FROM AMW_ACCT_ASSOCIATIONS
2164  		 WHERE OBJECT_TYPE = 'PROCESS_ORG'
2165  		 AND APPROVAL_DATE IS NOT NULL
2166  		 AND DELETION_APPROVAL_DATE IS NULL
2167  		 ) ACCREL,
2168 
2169 	   	(select flat.account_group_id, flat.parent_natural_account_id natural_account_id, flat.child_natural_account_id
2170  		from
2171  			(select flat.account_group_id, flat.parent_natural_account_id, flat.child_natural_account_id
2172 				from AMW_FIN_KEY_ACCT_FLAT flat
2173 			start with (account_group_id, parent_natural_account_id) in
2174 			(select account_group_id, natural_account_id
2175  			      from amw_fin_cert_scope
2176  			      where fin_certification_id = P_CERTIFICATION_ID)
2177  			connect by parent_natural_account_id = prior child_natural_account_id
2178  			           and account_group_id = prior account_group_id) flat
2179  	       where not exists (
2180  		select 'Y'
2181  		from AMW_FIN_CERT_SCOPE  temp2
2182  		where flat.account_group_id = temp2.account_group_id
2183  		and   flat.parent_natural_account_id = temp2.natural_account_id
2184  		and   temp2.fin_certification_id = P_CERTIFICATION_ID) ) temp
2185  	WHERE
2186 		ACCREL.NATURAL_ACCOUNT_ID (+) = temp.CHILD_NATURAL_ACCOUNT_ID)  itemaccmerge
2187 WHERE proc.organization_id (+) = itemaccmerge.organization_id
2188 and proc.parent_process_id (+) = itemaccmerge.process_id
2189 and proc.fin_certification_id(+) = P_CERTIFICATION_ID;
2190 
2191 END IF;
2192 
2193 x_return_status := FND_API.G_RET_STS_SUCCESS;
2194 
2195 EXCEPTION
2196      WHEN NO_DATA_FOUND THEN
2197      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
2198      x_return_status := FND_API.G_RET_STS_ERROR;
2199      x_msg_count := 1;
2200      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
2201       WHEN OTHERS THEN
2202        ROLLBACK TO INSERT_FIN_CERT_SCOPE;
2203       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
2204       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
2205         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2206         x_msg_count := 1;
2207         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
2208                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2209                 FND_MSG_PUB.Count_And_Get(
2210                 p_encoded =>  FND_API.G_FALSE,
2211                 p_count   =>  x_msg_count,
2212                 p_data    =>  x_msg_data);
2213 
2214 END INSERT_FIN_CERT_SCOPE;
2215 
2216 --------------------------------------- ******************************** ----------------------------
2217 
2218 PROCEDURE INSERT_FIN_CTRL(
2219 p_api_version_number        IN   NUMBER   := 1.0,
2220 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
2221 p_commit                    IN   VARCHAR2 := FND_API.g_false,
2222 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
2223 p_certification_id  IN       NUMBER,
2224 x_return_status             OUT  nocopy VARCHAR2,
2225 x_msg_count                 OUT  nocopy NUMBER,
2226 x_msg_data                  OUT  nocopy VARCHAR2
2227 ) IS
2228 L_COUNT NUMBER;
2229 l_error_message VARCHAR2(4000);
2230 l_api_name           CONSTANT VARCHAR2(30) := 'INSERT_FIN_CTRL';
2231 l_api_version_number CONSTANT NUMBER       := 1.0;
2232 
2233 l_return_status VARCHAR2(32767);
2234 l_msg_count NUMBER;
2235 l_msg_data VARCHAR2(32767);
2236 
2237 BEGIN
2238 
2239 SAVEPOINT INSERT_FIN_CTRL;
2240 
2241  -- Standard call to check for call compatibility.
2242         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2243                                              p_api_version_number,
2244                                              l_api_name,
2245                                              G_PKG_NAME)
2246         THEN
2247                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2248         END IF;
2249 
2250         -- Initialize message list if p_init_msg_list is set to TRUE.
2251         IF FND_API.to_Boolean( p_init_msg_list )
2252         THEN
2253                 FND_MSG_PUB.initialize;
2254         END IF;
2255 
2256         -- Initialize API return status to SUCCESS
2257         x_return_status := FND_API.G_RET_STS_SUCCESS;
2258 
2259 SELECT COUNT(1) INTO L_COUNT FROM AMW_FIN_ITEM_ACC_CTRL
2260 WHERE FIN_CERTIFICATION_ID = P_CERTIFICATION_ID;
2261 
2262 IF (L_COUNT = 0 OR L_COUNT IS NULL) THEN
2263 
2264 insert into amw_fin_item_acc_ctrl
2265 (
2266 OBJECT_TYPE,
2267 FIN_CERTIFICATION_ID,
2268 STATEMENT_GROUP_ID ,
2269 FINANCIAL_STATEMENT_ID,
2270 FINANCIAL_ITEM_ID,
2271 ACCOUNT_GROUP_ID ,
2272 NATURAL_ACCOUNT_ID ,
2273 ORGANIZATION_ID ,
2274 CONTROL_ID ,
2275 CONTROL_REV_ID ,
2276 OPINION_LOG_ID,
2277 CREATED_BY ,
2278 CREATION_DATE  ,
2279 LAST_UPDATED_BY  ,
2280 LAST_UPDATE_DATE  ,
2281 LAST_UPDATE_LOGIN  ,
2282 SECURITY_GROUP_ID ,
2283 OBJECT_VERSION_NUMBER )
2284 SELECT distinct 'ACCOUNT' OBJECT_TYPE , fin_certification_id, statement_group_id, financial_statement_id, null financial_item_id,
2285 account_group_id, natural_account_id, organization_id, control_id, control_rev_id, pk5 opinion_log_id, 1, sysdate, 1, sysdate, 1, null, 1
2286 from amw_fin_cert_scope scp,
2287      amw_control_associations ctrl
2288 where ctrl.pk1 = scp.fin_certification_id
2289 and ctrl.object_type = 'RISK_FINCERT'
2290 and scp.natural_account_id is not null
2291 and scp.organization_id = ctrl.pk2
2292 and scp.process_id = ctrl.pk3
2293 and ctrl.pk1 = p_certification_id
2294 union all
2295 select distinct 'FINANCIAL ITEM' OBJECT_TYPE, fin_certification_id, statement_group_id, financial_statement_id, financial_item_id,
2296 null account_group_id, null natural_account_id, organization_id, control_id,  control_rev_id, pk5 opinion_log_id, 1, sysdate, 1, sysdate, 1, null, 1
2297 from amw_fin_cert_scope scp,
2298      amw_control_associations ctrl
2299 where ctrl.pk1 = scp.fin_certification_id
2300 and ctrl.object_type = 'RISK_FINCERT'
2301 and scp.organization_id = ctrl.pk2
2302 and scp.process_id = ctrl.pk3
2303 and ctrl.pk1 = p_certification_id
2304 union all
2305 select distinct  'FINANCIAL STATEMENT' OBJECT_TYPE, fin_certification_id, statement_group_id, financial_statement_id, null financial_item_id,
2306 null account_group_id, null natural_account_id, organization_id, control_id, control_rev_id, pk5 opinion_log_id, 1, sysdate, 1, sysdate, 1, null, 1
2307 from amw_fin_cert_scope scp,
2308      amw_control_associations ctrl
2309 where ctrl.pk1 = scp.fin_certification_id
2310 and ctrl.object_type = 'RISK_FINCERT'
2311 and scp.organization_id = ctrl.pk2
2312 and scp.process_id = ctrl.pk3
2313 and ctrl.pk1 = p_certification_id;
2314 
2315 if(p_commit <> FND_API.g_false)
2316 then commit;
2317 end if;
2318 
2319 END IF;
2320 x_return_status := FND_API.G_RET_STS_SUCCESS;
2321 
2322 EXCEPTION
2323      WHEN NO_DATA_FOUND THEN
2324      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
2325      x_return_status := FND_API.G_RET_STS_ERROR;
2326      x_msg_count := 1;
2327      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
2328       WHEN OTHERS THEN
2329        ROLLBACK TO INSERT_FIN_CTRL;
2330       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
2331       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
2332         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2333         x_msg_count := 1;
2334         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
2335                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2336                 FND_MSG_PUB.Count_And_Get(
2337                 p_encoded =>  FND_API.G_FALSE,
2338                 p_count   =>  x_msg_count,
2339                 p_data    =>  x_msg_data);
2340  		RETURN;
2341 END INSERT_FIN_CTRL;
2342 
2343 PROCEDURE INSERT_FIN_RISK(
2344 p_api_version_number        IN   NUMBER   := 1.0,
2345 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
2346 p_commit                    IN   VARCHAR2 := FND_API.g_false,
2347 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
2348 p_certification_id  IN       NUMBER,
2349 x_return_status             OUT  nocopy VARCHAR2,
2350 x_msg_count                 OUT  nocopy NUMBER,
2351 x_msg_data                  OUT  nocopy VARCHAR2
2352 )IS
2353 L_COUNT NUMBER;
2354 
2355 l_api_name           CONSTANT VARCHAR2(30) := 'INSERT_FIN_RISK';
2356 l_api_version_number CONSTANT NUMBER       := 1.0;
2357 
2358 l_return_status VARCHAR2(32767);
2359 l_msg_count NUMBER;
2360 l_msg_data VARCHAR2(32767);
2361 
2362 BEGIN
2363 SAVEPOINT INSERT_FIN_RISK;
2364 
2365  -- Standard call to check for call compatibility.
2366         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2367                                              p_api_version_number,
2368                                              l_api_name,
2369                                              G_PKG_NAME)
2370         THEN
2371                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2372         END IF;
2373 
2374         -- Initialize message list if p_init_msg_list is set to TRUE.
2375         IF FND_API.to_Boolean( p_init_msg_list )
2376         THEN
2377                 FND_MSG_PUB.initialize;
2378         END IF;
2379 
2380         -- Initialize API return status to SUCCESS
2381         x_return_status := FND_API.G_RET_STS_SUCCESS;
2382 
2383 SELECT COUNT(1) INTO L_COUNT FROM AMW_FIN_ITEM_ACC_RISK
2384 WHERE FIN_CERTIFICATION_ID = P_CERTIFICATION_ID;
2385 
2386 IF (L_COUNT = 0 OR L_COUNT IS NULL) THEN
2387 
2388 insert into amw_fin_item_acc_risk(
2389 OBJECT_TYPE,
2390 FIN_CERTIFICATION_ID,
2391 STATEMENT_GROUP_ID,
2392 FINANCIAL_STATEMENT_ID,
2393 FINANCIAL_ITEM_ID,
2394 ACCOUNT_GROUP_ID,
2395 NATURAL_ACCOUNT_ID,
2396 ORGANIZATION_ID,
2397 PROCESS_ID,
2398 RISK_ID,
2399 RISK_REV_ID,
2400 OPINION_LOG_ID,
2401 CREATED_BY ,
2402 CREATION_DATE  ,
2403 LAST_UPDATED_BY  ,
2404 LAST_UPDATE_DATE  ,
2405 LAST_UPDATE_LOGIN  ,
2406 SECURITY_GROUP_ID ,
2407 OBJECT_VERSION_NUMBER )
2408 SELECT distinct 'ACCOUNT' OBJECT_TYPE , fin_certification_id, statement_group_id, financial_statement_id, null financial_item_id,
2409 account_group_id, natural_account_id, organization_id, process_id, risk_id, risk_rev_id, pk4 opinion_log_id, 1, sysdate, 1, sysdate, 1, null, 1
2410 from amw_fin_cert_scope scp,
2411      amw_risk_associations risk
2412 where risk.pk1 = scp.fin_certification_id
2413 and  risk.object_type = 'PROCESS_FINCERT'
2414 and scp.natural_account_id is not null
2415 and scp.organization_id = risk.pk2
2416 and scp.process_id = risk.pk3
2417 and risk.pk1 = p_certification_id
2418 union all
2419 select distinct 'FINANCIAL ITEM' OBJECT_TYPE, fin_certification_id, statement_group_id, financial_statement_id, financial_item_id,
2420 null account_group_id, null natural_account_id, organization_id, process_id, risk_id, risk_rev_id, pk4 opinion_log_id, 1, sysdate, 1, sysdate, 1, null, 1
2421 from amw_fin_cert_scope scp,
2422      amw_risk_associations risk
2423 where risk.pk1 = scp.fin_certification_id
2424 and risk.object_type = 'PROCESS_FINCERT'
2425 and scp.organization_id = risk.pk2
2426 and scp.process_id = risk.pk3
2427 and risk.pk1 = p_certification_id
2428 union all
2429 select distinct  'FINANCIAL STATEMENT' OBJECT_TYPE, fin_certification_id, statement_group_id, financial_statement_id, null financial_item_id,
2430 null account_group_id, null natural_account_id, organization_id, process_id, risk_id, risk_rev_id, pk4 opinion_log_id, 1, sysdate, 1, sysdate, 1, null, 1
2431 from amw_fin_cert_scope scp,
2432      amw_risk_associations risk
2433 where risk.pk1 = scp.fin_certification_id
2434 and risk.object_type = 'PROCESS_FINCERT'
2435 and scp.organization_id = risk.pk2
2436 and scp.process_id = risk.pk3
2437 and risk.pk1 = p_certification_id;
2438 
2439 if(p_commit <> FND_API.g_false)
2440 then commit;
2441 end if;
2442 
2443 END IF;
2444 x_return_status := FND_API.G_RET_STS_SUCCESS;
2445 EXCEPTION
2446      WHEN NO_DATA_FOUND THEN
2447      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
2448      x_return_status := FND_API.G_RET_STS_ERROR;
2449      x_msg_count := 1;
2450      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
2451       WHEN OTHERS THEN
2452        ROLLBACK TO INSERT_FIN_RISK;
2453       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
2454       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
2455         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2456         x_msg_count := 1;
2457         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
2458                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2459                 FND_MSG_PUB.Count_And_Get(
2460                 p_encoded =>  FND_API.G_FALSE,
2461                 p_count   =>  x_msg_count,
2462                 p_data    =>  x_msg_data);
2463                 RETURN;
2464 
2465 END INSERT_FIN_RISK;
2466 
2467 
2468 Procedure GetGLPeriodfor_FinCertEvalSum
2469 (P_Certification_ID in number,
2470 P_start_date out NOCOPY  date,
2471 P_end_date out NOCOPY  date)
2472 IS
2473 BEGIN
2474  Select
2475 GL_PERIODS.START_DATE ,
2476 GL_PERIODS.END_DATE
2477 into P_start_date, P_end_Date
2478 from
2479 AMW_CERTIFICATION_VL CERTIFICATION,
2480 amw_gl_periods_v GL_PERIODS
2481 WHERE
2482 GL_PERIODS.PERIOD_NAME = CERTIFICATION.CERTIFICATION_PERIOD_NAME
2483 AND GL_PERIODS.PERIOD_SET_NAME = CERTIFICATION.CERTIFICATION_PERIOD_SET_NAME
2484 and CERTIFICATION.OBJECT_TYPE='FIN_STMT'
2485 AND CERTIFICATION.CERTIFICATION_ID = P_Certification_ID;
2486 
2487 
2488   EXCEPTION
2489   WHEN NO_DATA_FOUND THEN
2490     /*** Record doesn't exist ***/
2491     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.GetGLPeriodfor_FinCertEvalSum');
2492     RETURN;
2493   WHEN OTHERS THEN
2494     /*** Raise any other error ***/
2495 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.GetGLPeriodfor_FinCertEvalSum');
2496 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
2497     RAISE;
2498 
2499 END GetGLPeriodfor_FinCertEvalSum;
2500 
2501 
2502 PROCEDURE POPULATE_PROC_HIERARCHY(
2503 p_api_version_number        IN   NUMBER   := 1.0,
2504 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
2505 p_commit                    IN   VARCHAR2 := FND_API.g_false,
2506 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
2507 P_CERTIFICATION_ID NUMBER,
2508 P_PROCESS_ID NUMBER,
2509 P_ORGANIZATION_ID NUMBER,
2510 p_account_process_flag VARCHAR2,
2511 x_return_status             OUT  nocopy VARCHAR2,
2512 x_msg_count                 OUT  nocopy NUMBER,
2513 x_msg_data                  OUT  nocopy VARCHAR2
2514  )IS
2515 
2516   l_count NUMBER;
2517 
2518   l_api_name           CONSTANT VARCHAR2(30) := 'POPULATE_PROC_HIERARCHY';
2519 l_api_version_number CONSTANT NUMBER       := 1.0;
2520 
2521 l_return_status VARCHAR2(32767);
2522 l_msg_count NUMBER;
2523 l_msg_data VARCHAR2(32767);
2524 
2525   BEGIN
2526 
2527   SAVEPOINT POPULATE_PROC_HIERARCHY;
2528 
2529  -- Standard call to check for call compatibility.
2530         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2531                                              p_api_version_number,
2532                                              l_api_name,
2533                                              G_PKG_NAME)
2534         THEN
2535                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2536         END IF;
2537 
2538         -- Initialize message list if p_init_msg_list is set to TRUE.
2539         IF FND_API.to_Boolean( p_init_msg_list )
2540         THEN
2541                 FND_MSG_PUB.initialize;
2542         END IF;
2543 
2544         -- Initialize API return status to SUCCESS
2545         x_return_status := FND_API.G_RET_STS_SUCCESS;
2546 
2547   SELECT COUNT(1) INTO l_count FROM AMW_FIN_PROCESS_FLAT
2548   WHERE PARENT_PROCESS_ID = P_PROCESS_ID
2549   AND ORGANIZATION_ID = P_ORGANIZATION_ID
2550   AND FIN_CERTIFICATION_ID = P_CERTIFICATION_ID;
2551 
2552   --process directly associates to the account which belongs to this financial statement
2553   -- to simplify the query, try amw_org_hierarchy_denorm
2554   IF(l_count = 0 or l_count is null) THEN
2555   		IF  p_account_process_flag = 'Y' THEN
2556    INSERT INTO AMW_FIN_PROCESS_FLAT
2557                     (
2558                      FIN_CERTIFICATION_ID,
2559                      PARENT_PROCESS_ID,
2560                      CHILD_PROCESS_ID,
2561                      ORGANIZATION_ID,
2562                      CREATED_BY ,
2563                      CREATION_DATE  ,
2564                      LAST_UPDATED_BY  ,
2565                      LAST_UPDATE_DATE  ,
2566                      LAST_UPDATE_LOGIN  ,
2567                      SECURITY_GROUP_ID ,
2568                      OBJECT_VERSION_NUMBER )
2569                      		(select P_CERTIFICATION_ID, P_PROCESS_ID, child_id, P_ORGANIZATION_ID, 1, sysdate, 1, sysdate, 1, null, 1
2570 												from amw_approved_hierarchies
2571 												start with parent_id = P_PROCESS_ID AND ORGANIZATION_ID = P_ORGANIZATION_ID
2572 															and start_date is not null and end_date is null
2573 												CONNECT BY PRIOR CHILD_ID = PARENT_ID AND ORGANIZATION_ID = P_ORGANIZATION_ID
2574 					  									and start_date is not null and end_date is null
2575 												UNION
2576 												 select P_CERTIFICATION_ID, P_PROCESS_ID,  -2, P_ORGANIZATION_ID, 1, sysdate, 1, sysdate, 1, null, 1 from dual);
2577 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2578 /*
2579                      select P_CERTIFICATION_ID, process_id,  parent_child_id, organization_id, 1, sysdate, 1, sysdate, 1, null, 1
2580                      from amw_org_hierarchy_denorm
2581                      where organization_id = P_ORGANIZATION_ID
2582                   	and hierarchy_type = 'A'
2583                   	and process_id = P_PROCESS_ID
2584                   	and (up_down_ind = 'D'
2585                   	or (parent_child_id = -2 and  up_down_ind= 'U'));
2586 */
2587                  ELSE
2588                     INSERT INTO AMW_FIN_PROCESS_FLAT
2589                     (
2590                      FIN_CERTIFICATION_ID,
2591                      PARENT_PROCESS_ID,
2592                      CHILD_PROCESS_ID,
2593                      ORGANIZATION_ID,
2594                      CREATED_BY ,
2595                      CREATION_DATE  ,
2596                      LAST_UPDATED_BY  ,
2597                      LAST_UPDATE_DATE  ,
2598                      LAST_UPDATE_LOGIN  ,
2599                      SECURITY_GROUP_ID ,
2600                      OBJECT_VERSION_NUMBER )
2601                      	(select P_CERTIFICATION_ID, P_PROCESS_ID, child_id, P_ORGANIZATION_ID, 1, sysdate, 1, sysdate, 1, null, 1
2602 												from amw_approved_hierarchies
2603 												start with parent_id = P_PROCESS_ID AND ORGANIZATION_ID = P_ORGANIZATION_ID
2604 															and start_date is not null and end_date is null
2605 												CONNECT BY PRIOR CHILD_ID = PARENT_ID AND ORGANIZATION_ID = P_ORGANIZATION_ID
2606 					  									and start_date is not null and end_date is null);
2607 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2608 /*
2609                      select P_CERTIFICATION_ID, process_id,  parent_child_id, organization_id, 1, sysdate, 1, sysdate, 1, null, 1
2610                       from amw_org_hierarchy_denorm
2611                   	where organization_id = P_ORGANIZATION_ID
2612                   	and hierarchy_type = 'A'
2613                   	and process_id = P_PROCESS_ID
2614                   	and up_down_ind = 'D';
2615 */
2616                      END IF;
2617 
2618     END IF;
2619 
2620 --process directly associates to the account which belongs to this financial statement
2621 -- to be deleted because it's a less efficient solution
2622 -- Note: select p_process_id is very important. it's different from select parent_id
2623 /*
2624   IF  p_account_process_flag = 'Y' THEN
2625   INSERT INTO AMW_FIN_PROCESS_FLAT
2626   (CERTIFICATION_ID,
2627    PARENT_PROCESS_ID,
2628    CHILD_PROCESS_ID,
2629    ORGANIZATION_ID)
2630    (SELECT  distinct P_CERTIFICATION_ID, P_PROCESS_ID, child_id, organization_id
2631    FROM    AMW_APPROVED_HIERARCHIES
2632    START WITH parent_id = P_PROCESS_ID
2633    AND organization_id = P_ORGANIZATION_ID
2634    CONNECT BY prior child_id = parent_id
2635    AND  prior organization_id = organization_id
2636    UNION ALL
2637    SELECT P_CERTIFICATION_ID, P_PROCESS_ID, -1, P_ORGANIZATION_ID FROM DUAL);
2638   -- sub processes of the process which directly links to the account
2639   ELSE
2640   INSERT INTO AMW_FIN_PROCESS_FLAT
2641   (CERTIFICATION_ID,
2642    PARENT_PROCESS_ID,
2643    CHILD_PROCESS_ID,
2644    ORGANIZATION_ID)
2645    SELECT  distinct P_CERTIFICATION_ID, P_PROCESS_ID, child_id, organization_id
2646    FROM    AMW_APPROVED_HIERARCHIES
2647    START WITH parent_id = P_PROCESS_ID
2648    AND organization_id = P_ORGANIZATION_ID
2649    CONNECT BY prior child_id = parent_id
2650    AND  prior organization_id = organization_id;
2651    END IF;
2652 
2653       **********************/
2654 
2655 if(p_commit <> FND_API.g_false)
2656 then commit;
2657 end if;
2658 x_return_status := FND_API.G_RET_STS_SUCCESS;
2659 
2660 EXCEPTION
2661      WHEN NO_DATA_FOUND THEN
2662      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
2663      x_return_status := FND_API.G_RET_STS_ERROR;
2664      x_msg_count := 1;
2665      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
2666       WHEN OTHERS THEN
2667        ROLLBACK TO POPULATE_PROC_HIERARCHY;
2668       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
2669       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
2670         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2671         x_msg_count := 1;
2672         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
2673                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
2674                 FND_MSG_PUB.Count_And_Get(
2675                 p_encoded =>  FND_API.G_FALSE,
2676                 p_count   =>  x_msg_count,
2677                 p_data    =>  x_msg_data);
2678                 RETURN;
2679 
2680  END POPULATE_PROC_HIERARCHY;
2681 
2682 
2683 
2684 --------------populate all of financial certification related summary tables -------------------------
2685 FUNCTION Populate_Fin_Stmt_Cert_Sum
2686 ( p_subscription_guid   in     raw,
2687   p_event               in out NOCOPY wf_event_t)
2688 return varchar2
2689 IS
2690 
2691  l_key                    varchar2(240) := p_event.GetEventKey();
2692  l_org_id                 NUMBER;
2693  l_user_id 	            NUMBER;
2694  l_resp_id 	            NUMBER;
2695  l_resp_appl_id           NUMBER;
2696  l_security_group_id      NUMBER;
2697  l_fin_cert_id NUMBER;
2698 
2699  l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_Stmt_Cert_Sum';
2700 l_api_version_number CONSTANT NUMBER       := 1.0;
2701 
2702 l_return_status VARCHAR2(32767);
2703 l_msg_count NUMBER;
2704 l_msg_data VARCHAR2(32767);
2705 
2706 
2707 
2708 BEGIN
2709 
2710 SAVEPOINT Populate_Fin_Stmt_Cert_Sum;
2711 
2712 fnd_file.put_line(fnd_file.LOG, 'start oracle.apps.amw.certification.create event subcription: Populate_Fin_Stmt_Cert_Sum' || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2713 
2714   l_org_id := p_event.GetValueForParameter('ORG_ID');
2715   l_user_id := p_event.GetValueForParameter('USER_ID');
2716   l_resp_id := p_event.GetValueForParameter('RESP_ID');
2717   l_resp_appl_id := p_event.GetValueForParameter('RESP_APPL_ID');
2718   l_security_group_id := p_event.GetValueForParameter('SECURITY_GROUP_ID');
2719 
2720   fnd_global.apps_initialize (l_user_id, l_resp_id, l_resp_appl_id, l_security_group_id);
2721 
2722   --l_org_id := p_event.GetValueForParameter('ORG_ID');
2723    l_fin_cert_id :=  p_event.GetValueForParameter('FIN_CERTIFICATION_ID');
2724 
2725   fnd_file.put_line(fnd_file.LOG, 'fin_certification_id = ' || l_fin_cert_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2726 
2727   IF (l_fin_cert_id  IS NOT NULL) THEN
2728    AMW_FINSTMT_CERT_BES_PKG.Master_Fin_Proc_Eval_Sum
2729    ( p_certification_id => l_fin_cert_id,
2730     --p_start_date => c_cert_Rec.start_date,
2731      p_commit => FND_API.g_true,
2732      x_return_status    => l_return_status,
2733      x_msg_count   => l_msg_count,
2734     x_msg_data    => l_msg_data);
2735   END IF;
2736 
2737   fnd_file.put_line(fnd_file.LOG, 'finish populating summary table with certification_id = ' || l_fin_cert_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2738 
2739 RETURN FND_API.G_RET_STS_SUCCESS;
2740 
2741 EXCEPTION
2742  WHEN OTHERS THEN
2743     ROLLBACK TO Populate_Fin_Stmt_Cert_Sum;
2744      WF_CORE.CONTEXT('AMW_FINSTMT_CERT_BES_PKG', 'Populate_Fin_Stmt_Cert_Sum', p_event.getEventName(), p_subscription_guid);
2745      WF_EVENT.setErrorInfo(p_event, 'ERROR');
2746      RETURN FND_API.G_RET_STS_UNEXP_ERROR;
2747 
2748 end Populate_Fin_Stmt_Cert_Sum;
2749 
2750 FUNCTION Certification_Update
2751 ( p_subscription_guid   in     raw,
2752   p_event               in out NOCOPY wf_event_t
2753 ) return VARCHAR2
2754 IS
2755 
2756  l_key                    varchar2(240) := p_event.GetEventKey();
2757  l_org_id                 NUMBER;
2758  l_user_id 	            NUMBER;
2759  l_resp_id 	            NUMBER;
2760  l_resp_appl_id           NUMBER;
2761  l_security_group_id      NUMBER;
2762 
2763   l_opinion_log_id  NUMBER;
2764 
2765 l_return_status VARCHAR2(32767);
2766 l_msg_count NUMBER;
2767 l_msg_data VARCHAR2(32767);
2768 
2769 BEGIN
2770 
2771 fnd_file.put_line(fnd_file.LOG, 'start oracle.apps.amw.opinion.certification.update event subcription: Certification_Update' || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2772 
2773   /*
2774   l_org_id := p_event.GetValueForParameter('ORG_ID');
2775   l_user_id := p_event.GetValueForParameter('USER_ID');
2776   l_resp_id := p_event.GetValueForParameter('RESP_ID');
2777   l_resp_appl_id := p_event.GetValueForParameter('RESP_APPL_ID');
2778   l_security_group_id := p_event.GetValueForParameter('SECURITY_GROUP_ID');
2779 
2780   fnd_global.apps_initialize (l_user_id, l_resp_id, l_resp_appl_id, l_security_group_id);
2781   */
2782 
2783 
2784 
2785    l_opinion_log_id :=  p_event.GetValueForParameter('OPINION_LOG_ID');
2786 
2787 fnd_file.put_line(fnd_file.LOG, 'before evaluation_update.opinion_log_id=' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2788 
2789 	Certification_Update_Handler(
2790    	 p_opinion_log_id  => l_opinion_log_id,
2791      	x_return_status => l_return_status,
2792     	x_msg_count    => l_msg_count,
2793    	 x_msg_data     => l_msg_data);
2794 
2795 fnd_file.put_line(fnd_file.LOG, 'after evaluation_update.opinion_log_id=' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2796 
2797  RETURN l_return_status;
2798 
2799 
2800 
2801 EXCEPTION
2802  WHEN OTHERS THEN
2803      WF_CORE.CONTEXT('AMW_FINSTMT_CERT_BES_PKG', 'Certification_Update', p_event.getEventName(), p_subscription_guid);
2804      WF_EVENT.setErrorInfo(p_event, FND_API.G_RET_STS_UNEXP_ERROR);
2805      RETURN l_return_status;
2806 
2807 END  Certification_Update;
2808 
2809 
2810 FUNCTION Evaluation_Update
2811 ( p_subscription_guid   in     raw,
2812   p_event               in out NOCOPY wf_event_t
2813 ) return VARCHAR2
2814 IS
2815 
2816 
2817  l_key                    varchar2(240) := p_event.GetEventKey();
2818  l_org_id                 NUMBER;
2819  l_user_id 	            NUMBER;
2820  l_resp_id 	            NUMBER;
2821  l_resp_appl_id           NUMBER;
2822  l_security_group_id      NUMBER;
2823 
2824   l_opinion_log_id  NUMBER;
2825 
2826 l_return_status VARCHAR2(32767);
2827 l_msg_count NUMBER;
2828 l_msg_data VARCHAR2(32767);
2829 
2830 BEGIN
2831 
2832 fnd_file.put_line(fnd_file.LOG, 'start oracle.apps.amw.opinion.evaluation.update event subcription: evaluation_update' || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2833 
2834   /*
2835   l_org_id := p_event.GetValueForParameter('ORG_ID');
2836   l_user_id := p_event.GetValueForParameter('USER_ID');
2837   l_resp_id := p_event.GetValueForParameter('RESP_ID');
2838   l_resp_appl_id := p_event.GetValueForParameter('RESP_APPL_ID');
2839   l_security_group_id := p_event.GetValueForParameter('SECURITY_GROUP_ID');
2840 
2841   fnd_global.apps_initialize (l_user_id, l_resp_id, l_resp_appl_id, l_security_group_id);
2842   */
2843 
2844 
2845   l_opinion_log_id :=  p_event.GetValueForParameter('OPINION_LOG_ID');
2846 
2847 fnd_file.put_line(fnd_file.LOG, 'before evaluation_update.opinion_log_id=' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2848 
2849 	Evaluation_Update_Handler(
2850    	 p_opinion_log_id  => l_opinion_log_id,
2851      	x_return_status => l_return_status,
2852     	x_msg_count    => l_msg_count,
2853    	 x_msg_data     => l_msg_data);
2854 
2855 fnd_file.put_line(fnd_file.LOG, 'after evaluation_update.opinion_log_id=' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2856 
2857  RETURN l_return_status;
2858 
2859 
2860 EXCEPTION
2861  WHEN OTHERS THEN
2862      WF_CORE.CONTEXT('AMW_FINSTMT_CERT_BES_PKG', 'Evaluation_Update', p_event.getEventName(), p_subscription_guid);
2863      WF_EVENT.setErrorInfo(p_event, FND_API.G_RET_STS_UNEXP_ERROR);
2864      RETURN l_return_status;
2865 
2866 END  Evaluation_Update;
2867 
2868 PROCEDURE Certification_Update_Handler(
2869    p_api_version_number        IN   NUMBER   := 1.0,
2870 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
2871 p_commit                    IN   VARCHAR2 := FND_API.g_false,
2872 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
2873     p_opinion_log_id  IN       NUMBER,
2874      x_return_status             OUT  nocopy VARCHAR2,
2875     x_msg_count                 OUT  nocopy NUMBER,
2876     x_msg_data                  OUT  nocopy VARCHAR2
2877 )
2878 IS
2879 
2880  CURSOR Get_Obj_Name (p_opinion_log_id NUMBER) IS
2881  SELECT obj.obj_name , oplog.pk1_value, oplog.pk2_value, oplog.pk3_value,
2882  	oplog.pk4_value, oplog.pk5_value, oplog.opinion_id
2883  FROM FND_OBJECTS obj,
2884       AMW_OBJECT_OPINION_TYPES oot,
2885       AMW_OPINIONS_LOG oplog
2886  WHERE oplog.opinion_log_id = p_opinion_log_id
2887  AND   oplog.object_opinion_type_id = oot.object_opinion_type_id
2888  AND   oot.object_id = obj.object_id;
2889 
2890  l_opinion_log_id number;
2891  l_opinion_id number;
2892  l_object_name varchar2(100);
2893  l_pk1 number;
2894  l_pk2 number;
2895  l_pk3 number;
2896  l_pk4 number;
2897  l_pk5 number;
2898 
2899  l_api_name           CONSTANT VARCHAR2(30) := 'Certification_Update_Handler';
2900 l_api_version_number CONSTANT NUMBER       := 1.0;
2901 
2902 l_return_status VARCHAR2(32767);
2903 l_msg_count NUMBER;
2904 l_msg_data VARCHAR2(32767);
2905 
2906 BEGIN
2907 
2908 SAVEPOINT Certification_Update_Handler;
2909 fnd_file.put_line(fnd_file.LOG, 'start Certification_Update_Handler::' || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2910 
2911  -- Standard call to check for call compatibility.
2912         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2913                                              p_api_version_number,
2914                                              l_api_name,
2915                                              G_PKG_NAME)
2916         THEN
2917                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2918         END IF;
2919 
2920         -- Initialize message list if p_init_msg_list is set to TRUE.
2921         IF FND_API.to_Boolean( p_init_msg_list )
2922         THEN
2923                 FND_MSG_PUB.initialize;
2924         END IF;
2925 
2926         -- Initialize API return status to SUCCESS
2927         x_return_status := FND_API.G_RET_STS_SUCCESS;
2928           g_refresh_flag := 'N';
2929 
2930  l_opinion_log_id := p_opinion_log_id;
2931   open Get_Obj_Name(l_opinion_log_id);
2932   fetch Get_Obj_Name into l_object_name, l_pk1, l_pk2, l_pk3, l_pk4, l_pk5, l_opinion_id;
2933   close Get_Obj_Name;
2934 
2935   IF (l_object_name = 'AMW_ORGANIZATION') THEN
2936   fnd_file.put_line(fnd_file.LOG, 'before organization_change_handler: org_id' || l_pk3 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2937   ORGANIZATION_CHANGE_HANDLER(
2938   	p_org_id => l_pk3 ,
2939  	p_opinion_log_id => l_opinion_log_id,
2940  	p_action => 'CERTIFICATION',
2941  	x_return_status   => l_return_status,
2942 	x_msg_count  => l_msg_count,
2943 	x_msg_data    => l_msg_data);
2944 
2945     UPDATE AMW_FIN_ORG_EVAL_SUM
2946     SET
2947     LAST_UPDATE_DATE = sysdate,
2948     last_updated_by = fnd_global.user_id,
2949     last_update_login = fnd_global.conc_login_id,
2950     CERT_OPINION_LOG_ID  = l_opinion_log_id,
2951     CERT_OPINION_ID   = l_opinion_id
2952     WHERE ORGANIZATION_ID = l_pk1
2953     AND   FIN_CERTIFICATION_ID IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
2954    				  WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
2955  if(p_commit <> FND_API.g_false)
2956 then commit;
2957 end if;
2958 
2959   fnd_file.put_line(fnd_file.LOG, 'after organization_change_handler: org_id' || l_pk3 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2960 
2961   ELSIF (l_object_name = 'AMW_ORG_PROCESS') THEN
2962   fnd_file.put_line(fnd_file.LOG, 'after process_change_handler: org_id' || l_pk3 || 'process_id' || l_pk1 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2963          PROCESS_CHANGE_HANDLER(
2964   	p_org_id => l_pk3 ,
2965  	p_process_id => l_pk1 ,
2966  	p_opinion_log_id => l_opinion_log_id,
2967  	p_action => 'CERTIFICATION',
2968  	x_return_status   => l_return_status,
2969 	x_msg_count  => l_msg_count,
2970 	x_msg_data    => l_msg_data);
2971 
2972    UPDATE AMW_FIN_PROCESS_EVAL_SUM SET
2973    LAST_UPDATE_DATE = sysdate,
2974    last_updated_by = fnd_global.user_id,
2975    last_update_login = fnd_global.conc_login_id,
2976    CERT_OPINION_LOG_ID = l_opinion_log_id,
2977    CERT_OPINION_ID   = l_opinion_id
2978    WHERE ORGANIZATION_ID = l_pk3
2979    AND   PROCESS_ID = l_pk1
2980    AND   FIN_CERTIFICATION_ID IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
2981    				  WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
2982 
2983  if(p_commit <> FND_API.g_false)
2984 then commit;
2985 end if;
2986 
2987  fnd_file.put_line(fnd_file.LOG, 'after process_change_handler: org_id' || l_pk3 || 'process_id' || l_pk1 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
2988 
2989  END IF;
2990 
2991   x_return_status := FND_API.G_RET_STS_SUCCESS;
2992 
2993  EXCEPTION
2994      WHEN NO_DATA_FOUND THEN
2995      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
2996      x_return_status := FND_API.G_RET_STS_ERROR;
2997      x_msg_count := 1;
2998      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
2999       WHEN OTHERS THEN
3000        ROLLBACK TO Certification_Update_Handler;
3001       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
3002       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
3003         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3004         x_msg_count := 1;
3005         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
3006                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
3007                 FND_MSG_PUB.Count_And_Get(
3008                 p_encoded =>  FND_API.G_FALSE,
3009                 p_count   =>  x_msg_count,
3010                 p_data    =>  x_msg_data);
3011                 RETURN;
3012 
3013 END Certification_Update_Handler;
3014 
3015 
3016 PROCEDURE Evaluation_Update_Handler(
3017 p_api_version_number        IN   NUMBER   := 1.0,
3018 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
3019 p_commit                    IN   VARCHAR2 := FND_API.g_false,
3020 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
3021 p_opinion_log_id  IN       NUMBER,
3022 x_return_status             OUT  nocopy VARCHAR2,
3023 x_msg_count                 OUT  nocopy NUMBER,
3024 x_msg_data                  OUT  nocopy VARCHAR2
3025 )
3026 IS
3027 
3028  CURSOR Get_Obj_Name (p_opinion_log_id NUMBER)
3029  IS
3030  SELECT obj.obj_name , oplog.pk1_value, oplog.pk2_value, oplog.pk3_value,
3031  	oplog.pk4_value, oplog.pk5_value, oplog.opinion_id
3032  FROM 	FND_OBJECTS obj,
3033       	AMW_OBJECT_OPINION_TYPES oot,
3034       	AMW_OPINIONS_LOG oplog
3035  WHERE oplog.opinion_log_id = p_opinion_log_id
3036  	AND   oplog.object_opinion_type_id = oot.object_opinion_type_id
3037  	AND   oot.object_id = obj.object_id;
3038 
3039  l_opinion_log_id number;
3040  l_opinion_id number;
3041  l_object_name varchar2(100);
3042  l_pk1 number;
3043  l_pk2 number;
3044  l_pk3 number;
3045  l_pk4 number;
3046  l_pk5 number;
3047 
3048  l_api_name           CONSTANT VARCHAR2(30) := 'Evaluation_Update_Handler';
3049 l_api_version_number CONSTANT NUMBER       := 1.0;
3050 
3051 l_index number := 0;
3052 
3053 l_return_status VARCHAR2(32767);
3054 l_msg_count NUMBER;
3055 l_msg_data VARCHAR2(32767);
3056 
3057 BEGIN
3058 
3059 SAVEPOINT Evaluation_Update_Handler;
3060 fnd_file.put_line(fnd_file.LOG, 'start evaluation_update_handler::' || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3061 
3062  -- Standard call to check for call compatibility.
3063         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
3064                                              p_api_version_number,
3065                                              l_api_name,
3066                                              G_PKG_NAME)
3067         THEN
3068                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3069         END IF;
3070 
3071         -- Initialize message list if p_init_msg_list is set to TRUE.
3072         IF FND_API.to_Boolean( p_init_msg_list )
3073         THEN
3074                 FND_MSG_PUB.initialize;
3075         END IF;
3076 
3077         -- Initialize API return status to SUCCESS
3078         x_return_status := FND_API.G_RET_STS_SUCCESS;
3079          g_refresh_flag := 'N';
3080 
3081  l_opinion_log_id := p_opinion_log_id;
3082   open Get_Obj_Name(l_opinion_log_id);
3083   fetch Get_Obj_Name into l_object_name, l_pk1, l_pk2, l_pk3, l_pk4, l_pk5, l_opinion_id;
3084   close Get_Obj_Name;
3085 
3086   IF (l_object_name = 'AMW_ORGANIZATION') THEN
3087   fnd_file.put_line(fnd_file.LOG, 'before organization_change_handler: org_id' || l_pk3 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3088   ORGANIZATION_CHANGE_HANDLER(
3089   	p_org_id => l_pk3 ,
3090  	p_opinion_log_id => l_opinion_log_id,
3091  	p_action => 'EVALUATION',
3092  	x_return_status   => l_return_status,
3093 	x_msg_count  => l_msg_count,
3094 	x_msg_data    => l_msg_data);
3095 
3096     UPDATE AMW_FIN_ORG_EVAL_SUM
3097     SET
3098     LAST_UPDATE_DATE = sysdate,
3099     last_updated_by = fnd_global.user_id,
3100     last_update_login = fnd_global.conc_login_id,
3101     EVAL_OPINION_LOG_ID  = l_opinion_log_id,
3102     EVAL_OPINION_ID   = l_opinion_id
3103     WHERE ORGANIZATION_ID = l_pk1
3104     AND   FIN_CERTIFICATION_ID IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
3105    				  WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
3106  if(p_commit <> FND_API.g_false) then
3107  commit;
3108 end if;
3109 
3110   fnd_file.put_line(fnd_file.LOG, 'after organization_change_handler: org_id' || l_pk3 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3111 
3112   ELSIF (l_object_name = 'AMW_ORG_PROCESS') THEN
3113   fnd_file.put_line(fnd_file.LOG, 'after process_change_handler: org_id' || l_pk3 || 'process_id' || l_pk1 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3114          PROCESS_CHANGE_HANDLER(
3115   	p_org_id => l_pk3 ,
3116  	p_process_id => l_pk1 ,
3117  	p_opinion_log_id => l_opinion_log_id,
3118  	p_action => 'EVALUATION',
3119  	x_return_status   => l_return_status,
3120 	x_msg_count  => l_msg_count,
3121 	x_msg_data    => l_msg_data);
3122 
3123    UPDATE AMW_FIN_PROCESS_EVAL_SUM SET
3124    LAST_UPDATE_DATE = sysdate,
3125    last_updated_by = fnd_global.user_id,
3126    last_update_login = fnd_global.conc_login_id,
3127    EVAL_OPINION_LOG_ID = l_opinion_log_id,
3128    EVAL_OPINION_ID   = l_opinion_id
3129    WHERE ORGANIZATION_ID = l_pk3
3130    AND   PROCESS_ID = l_pk1
3131    AND   FIN_CERTIFICATION_ID IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
3132    				  WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
3133 
3134  if(p_commit <> FND_API.g_false)
3135 then commit;
3136 end if;
3137 
3138    fnd_file.put_line(fnd_file.LOG, 'after process_change_handler: org_id' || l_pk3 || 'process_id' || l_pk1 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3139 
3140   ELSIF (l_object_name = 'AMW_ORG_PROCESS_RISK') THEN
3141 fnd_file.put_line(fnd_file.LOG, 'before risk_evaluation_handler: org_id=' || l_pk3 || 'risk_id=' || l_pk1 || 'process_id=' || l_pk4 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3142    RISK_EVALUATION_HANDLER
3143    (p_risk_id => l_pk1,
3144    p_org_id => l_pk3,
3145    p_process_id => l_pk4,
3146    p_opinion_log_id => l_opinion_log_id,
3147    x_return_status   => l_return_status,
3148    x_msg_count  => l_msg_count,
3149    x_msg_data    => l_msg_data);
3150 
3151    UPDATE AMW_RISK_ASSOCIATIONS SET
3152    LAST_UPDATE_DATE = sysdate,
3153    last_updated_by = fnd_global.user_id,
3154    last_update_login = fnd_global.conc_login_id,
3155    pk4 = l_opinion_log_id
3156    WHERE OBJECT_TYPE = 'PROCESS_FINCERT'
3157    AND   risk_id = l_pk1
3158    AND   pk2= l_pk3
3159    AND   pk3= l_pk4
3160    AND   pk1  IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
3161    				  WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
3162 if(p_commit <> FND_API.g_false)
3163 then commit;
3164 end if;
3165 
3166   fnd_file.put_line(fnd_file.LOG, 'after risk_evaluation_handler: org_id=' || l_pk3 || 'risk_id=' || l_pk1 || 'process_id=' || l_pk4 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3167 
3168 ELSIF (l_object_name = 'AMW_ORG_CONTROL') THEN
3169   fnd_file.put_line(fnd_file.LOG, 'before control_evaluation_handler: org_id=' || l_pk3 || 'control_id=' || l_pk1  || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3170    CONTROL_EVALUATION_HANDLER
3171    (p_ctrl_id => l_pk1,
3172     p_org_id => l_pk3,
3173     p_opinion_log_id => l_opinion_log_id,
3174     x_return_status   => l_return_status,
3175     x_msg_count  => l_msg_count,
3176     x_msg_data    => l_msg_data);
3177 
3178    UPDATE AMW_CONTROL_ASSOCIATIONS SET
3179    LAST_UPDATE_DATE = sysdate,
3180    last_updated_by = fnd_global.user_id,
3181    last_update_login = fnd_global.conc_login_id,
3182    pk5 = l_opinion_log_id
3183    WHERE OBJECT_TYPE = 'RISK_FINCERT'
3184    AND   control_id = l_pk1
3185    AND   pk2 = l_pk3
3186    AND   pk1  IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
3187    				  WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
3188 
3189 if(p_commit <> FND_API.g_false)
3190 then commit;
3191 end if;
3192 
3193   fnd_file.put_line(fnd_file.LOG, 'after control_evaluation_handler: org_id=' || l_pk3 || 'risk_id=' || l_pk1 || 'process_id=' || l_pk4 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3194 
3195 ELSIF (l_object_name = 'AMW_ORG_AP_CONTROL') THEN
3196   fnd_file.put_line(fnd_file.LOG, 'before ap_evaluation_handler: org_id=' || l_pk3 || 'control_id=' || l_pk1  || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3197 
3198    UPDATE AMW_AP_ASSOCIATIONS
3199    SET 	LAST_UPDATE_DATE = sysdate,
3200    	last_updated_by = fnd_global.user_id,
3201    	last_update_login = fnd_global.conc_login_id,
3202 	/**05.02.2006 npanandi: fixing the below bug, since it is actually
3203 	   pk5 that stores the opinionLogId
3204 	pk4 = l_opinion_log_id
3205 	 **/
3206    	pk5 = l_opinion_log_id
3207    WHERE OBJECT_TYPE = 'CONTROL_FINCERT'
3208    	AND   audit_procedure_id = l_pk1
3209    	AND   pk2 = l_pk3  -- organization_id
3210    	AND   pk1  IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
3211    				  WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
3212 
3213 if(p_commit <> FND_API.g_false)
3214 then commit;
3215 end if;
3216 
3217   fnd_file.put_line(fnd_file.LOG, 'after ap_evaluation_handler: org_id=' || l_pk3 || 'risk_id=' || l_pk1 || 'process_id=' || l_pk4 || ' opinion_log_id= ' || l_opinion_log_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3218 
3219 
3220   END IF;
3221 
3222 --IF (G_REFRESH_FLAG = 'Y') THEN
3223 --l_index := m_certification_list.FIRST;
3224 --WHILE  l_index <= m_certification_list.LAST LOOP
3225 
3226 --AMW_FINSTMT_CERT_BES_PKG.Master_Fin_Proc_Eval_Sum
3227 --   ( p_certification_id => m_certification_list(l_index),
3228 --     p_mode => 'REFRESH',
3229 --     x_return_status    => l_return_status,
3230 --     x_msg_count   => l_msg_count,
3231 --    x_msg_data    => l_msg_data);
3232 
3233 --l_index := l_index + 1;
3234 
3235 --END LOOP;
3236 
3237 --END IF;
3238 
3239 if(p_commit <> FND_API.g_false)
3240 then commit;
3241 end if;
3242 
3243 
3244 
3245   x_return_status := FND_API.G_RET_STS_SUCCESS;
3246 
3247  EXCEPTION
3248      WHEN NO_DATA_FOUND THEN
3249      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
3250      x_return_status := FND_API.G_RET_STS_ERROR;
3251      x_msg_count := 1;
3252      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
3253       WHEN OTHERS THEN
3254        ROLLBACK TO Evaluation_Update_Handler;
3255       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
3256       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
3257         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3258         x_msg_count := 1;
3259         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
3260                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
3261                 FND_MSG_PUB.Count_And_Get(
3262                 p_encoded =>  FND_API.G_FALSE,
3263                 p_count   =>  x_msg_count,
3264                 p_data    =>  x_msg_data);
3265                 RETURN;
3266 
3267 END Evaluation_Update_Handler;
3268 
3269 
3270 
3271 FUNCTION Evaluation_Create
3272 ( p_subscription_guid   in     raw,
3273   p_event               in out NOCOPY wf_event_t
3274 ) return VARCHAR2
3275 IS
3276 BEGIN
3277 NULL;
3278 END Evaluation_Create;
3279 
3280 
3281 FUNCTION Update_Fin_Stmt_Cert_Sum
3282 ( p_subscription_guid   in     raw,
3283   p_event               in out NOCOPY wf_event_t
3284 ) return VARCHAR2
3285 IS
3286 
3287 l_key                    varchar2(240) := p_event.GetEventKey();
3288  l_org_id                 NUMBER;
3289  l_user_id 	            NUMBER;
3290  l_resp_id 	            NUMBER;
3291  l_resp_appl_id           NUMBER;
3292  l_security_group_id      NUMBER;
3293  l_fin_cert_id NUMBER;
3294 
3295  l_api_name           CONSTANT VARCHAR2(30) := 'Update_Fin_Stmt_Cert_Sum';
3296 l_api_version_number CONSTANT NUMBER       := 1.0;
3297 
3298 l_return_status VARCHAR2(32767);
3299 l_msg_count NUMBER;
3300 l_msg_data VARCHAR2(32767);
3301 
3302 BEGIN
3303 
3304 SAVEPOINT Update_Fin_Stmt_Cert_Sum;
3305 
3306 fnd_file.put_line(fnd_file.LOG, 'start oracle.apps.amw.certification.update event subcription: Update_Fin_Stmt_Cert_Sum ' || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3307 
3308   l_org_id := p_event.GetValueForParameter('ORG_ID');
3309   l_user_id := p_event.GetValueForParameter('USER_ID');
3310   l_resp_id := p_event.GetValueForParameter('RESP_ID');
3311   l_resp_appl_id := p_event.GetValueForParameter('RESP_APPL_ID');
3312   l_security_group_id := p_event.GetValueForParameter('SECURITY_GROUP_ID');
3313 
3314   fnd_global.apps_initialize (l_user_id, l_resp_id, l_resp_appl_id, l_security_group_id);
3315 
3316   --l_org_id := p_event.GetValueForParameter('ORG_ID');
3317    l_fin_cert_id :=  p_event.GetValueForParameter('FIN_CERTIFICATION_ID');
3318 
3319   fnd_file.put_line(fnd_file.LOG, 'fin_certification_id = ' || l_fin_cert_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3320 
3321    AMW_FINSTMT_CERT_BES_PKG.Master_Fin_Proc_Eval_Sum
3322    (p_certification_id => l_fin_cert_id,
3323      p_commit  => FND_API.g_true,
3324      x_return_status    => l_return_status,
3325      x_msg_count   => l_msg_count,
3326     x_msg_data    => l_msg_data);
3327 
3328   fnd_file.put_line(fnd_file.LOG, 'finish updating summary table with certification_id = ' || l_fin_cert_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3329 
3330 RETURN FND_API.G_RET_STS_SUCCESS;
3331 
3332 EXCEPTION
3333  WHEN OTHERS THEN
3334      WF_CORE.CONTEXT('AMW_FINSTMT_CERT_BES_PKG', 'Update_Fin_Stmt_Cert_Sum', p_event.getEventName(), p_subscription_guid);
3335      WF_EVENT.setErrorInfo(p_event, 'ERROR' || l_msg_data);
3336      RETURN FND_API.G_RET_STS_UNEXP_ERROR;
3337 
3338 END Update_Fin_Stmt_Cert_Sum;
3339 
3340 
3341 PROCEDURE Master_Fin_Proc_Eval_Sum(
3342 p_api_version_number        IN   NUMBER   := 1.0,
3343 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
3344 p_commit                    IN   VARCHAR2 := FND_API.g_false,
3345 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
3346 p_certification_id  		IN       NUMBER,
3347 p_start_date 	IN DATE := null,
3348 p_mode			IN VARCHAR2 := 'NEW',
3349 x_return_status             OUT  nocopy VARCHAR2,
3350 x_msg_count                 OUT  nocopy NUMBER,
3351 x_msg_data                  OUT  nocopy VARCHAR2
3352 ) IS
3353 l_error_message varchar2(4000);
3354 l_start_time date;
3355 l_end_time date;
3356 
3357 l_api_name           CONSTANT VARCHAR2(30) := 'Master_Fin_Proc_Eval_Sum';
3358 l_api_version_number CONSTANT NUMBER       := 1.0;
3359 
3360 l_return_status VARCHAR2(32767);
3361 l_msg_count NUMBER;
3362 l_msg_data VARCHAR2(32767);
3363 l_start_date date;
3364 
3365 BEGIN
3366 
3367 SAVEPOINT Master_Fin_Proc_Eval_Sum;
3368 
3369 dbms_mview.refresh('AMW_OPINION_MV', '?');
3370 dbms_mview.refresh('AMW_OPINION_LOG_MV', '?');
3371 
3372  l_start_date := p_start_date;
3373  IF(p_mode = 'NEW') THEN
3374   Populate_All_Fin_Proc_Eval_Sum
3375   (p_certification_id => p_certification_id,
3376    x_return_status     =>  l_return_status,
3377    x_msg_count         =>  l_msg_count,
3378    x_msg_data          =>  l_msg_data);
3379    fnd_file.put_line(fnd_file.LOG, 'finish populating amw_fin_process_eval_sum table:p_certification_id = ' || p_certification_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3380  ELSE
3381  Refresh_All_Fin_Proc_Eval_Sum
3382   (p_certification_id => p_certification_id,
3383    x_return_status     =>  l_return_status,
3384    x_msg_count         =>  l_msg_count,
3385    x_msg_data          =>  l_msg_data);
3386    fnd_file.put_line(fnd_file.LOG, 'finish refreshing Refresh_All_Fin_Proc_Eval_Sum:p_certification_id = ' || p_certification_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3387   END IF;
3388 
3389   Populate_Fin_Risk_Ass_Sum
3390   (p_certification_id => p_certification_id,
3391    x_return_status     =>  l_return_status,
3392    x_msg_count         =>  l_msg_count,
3393    x_msg_data          =>  l_msg_data);
3394     fnd_file.put_line(fnd_file.LOG, 'finish populating amw_risk_associations table:p_certification_id = ' || p_certification_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3395   Populate_Fin_Ctrl_Ass_Sum
3396   (p_certification_id => p_certification_id,
3397    x_return_status     =>  l_return_status,
3398    x_msg_count         =>  l_msg_count,
3399    x_msg_data          =>  l_msg_data);
3400    fnd_file.put_line(fnd_file.LOG, 'finish populating amw_control_associations table:p_certification_id = ' || p_certification_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3401   Populate_Fin_AP_Ass_Sum
3402   (p_certification_id => p_certification_id,
3403    x_return_status     =>  l_return_status,
3404    x_msg_count         =>  l_msg_count,
3405    x_msg_data          =>  l_msg_data);
3406    fnd_file.put_line(fnd_file.LOG, 'finish populating amw_ap_associations table:p_certification_id = ' || p_certification_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3407     l_start_time := sysdate;
3408   INSERT_FIN_CERT_SCOPE
3409   (p_certification_id => p_certification_id,
3410    x_return_status     =>  l_return_status,
3411    x_msg_count         =>  l_msg_count,
3412    x_msg_data          =>  l_msg_data);
3413    l_end_time := sysdate;
3414    fnd_file.put_line(fnd_file.LOG, 'finish populating amw_fin_cert_scope table:p_certification_id = ' || p_certification_id || ' elapsed time is ' || (l_end_time-l_start_time)*24*60*60);
3415 
3416   INSERT_FIN_RISK
3417   (p_certification_id => p_certification_id,
3418    x_return_status     =>  l_return_status,
3419    x_msg_count         =>  l_msg_count,
3420    x_msg_data          =>  l_msg_data);
3421   fnd_file.put_line(fnd_file.LOG, 'finish populating amw_fin_item_acc_risks table:p_certification_id = ' || p_certification_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3422 
3423   INSERT_FIN_CTRL
3424   (p_certification_id => p_certification_id,
3425    x_return_status     =>  l_return_status,
3426    x_msg_count         =>  l_msg_count,
3427    x_msg_data          =>  l_msg_data);
3428 fnd_file.put_line(fnd_file.LOG, 'finish populating amw_fin_item_acc_ctrls table:p_certification_id = ' || p_certification_id || to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
3429 
3430   l_start_time := sysdate;
3431 
3432    Populate_Cert_General_Sum
3433             (p_certification_id => p_certification_id,
3434              p_start_date => l_start_date,
3435              x_return_status    => l_return_status,
3436      	     x_msg_count   => l_msg_count,
3437     	     x_msg_data    => l_msg_data);
3438 
3439    l_end_time := sysdate;
3440 fnd_file.put_line(fnd_file.LOG, 'finish populating amw_cert_dashboard_sum  table:p_certification_id = ' || p_certification_id || ' elapsed time is ' || (l_end_time-l_start_time)*24*60*60);
3441       l_start_time := sysdate;
3442   Populate_All_Fin_Org_Eval_Sum
3443    (p_certification_id => p_certification_id,
3444    x_return_status     =>  l_return_status,
3445    x_msg_count         =>  l_msg_count,
3446    x_msg_data          =>  l_msg_data);
3447 
3448     l_end_time := sysdate;
3449 fnd_file.put_line(fnd_file.LOG, 'finish populating amw_fin_org_eval_sum table:p_certification_id = ' || p_certification_id ||  ' elapsed time is ' || (l_end_time-l_start_time)*24*60*60);
3450       l_start_time := sysdate;
3451 
3452   build_amw_fin_cert_eval_sum
3453    (p_certification_id => p_certification_id,
3454    x_return_status     =>  l_return_status,
3455    x_msg_count         =>  l_msg_count,
3456    x_msg_data          =>  l_msg_data);
3457 
3458    l_end_time := sysdate;
3459   fnd_file.put_line(fnd_file.LOG, 'finish populating amw_fin_cert_eval_sum table:p_certification_id = ' || p_certification_id || ' elapsed time is ' || (l_end_time-l_start_time)*24*60*60);
3460 
3461 EXCEPTION
3462      WHEN NO_DATA_FOUND THEN
3463      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
3464      x_return_status := FND_API.G_RET_STS_ERROR;
3465      x_msg_count := 1;
3466      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
3467       WHEN OTHERS THEN
3468        ROLLBACK TO Master_Fin_Proc_Eval_Sum;
3469       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
3470       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
3471         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3472         x_msg_count := 1;
3473         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
3474                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
3475                 FND_MSG_PUB.Count_And_Get(
3476                 p_encoded =>  FND_API.G_FALSE,
3477                 p_count   =>  x_msg_count,
3478                 p_data    =>  x_msg_data);
3479                 RETURN;
3480 
3481 
3482 END Master_Fin_Proc_Eval_Sum;
3483 
3484 PROCEDURE Refresh_All_Fin_Proc_Eval_Sum(
3485 p_api_version_number        IN   NUMBER   := 1.0,
3486 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
3487 p_commit                    IN   VARCHAR2 := FND_API.g_false,
3488 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
3489 p_certification_id  IN       NUMBER,
3490 x_return_status             OUT  nocopy VARCHAR2,
3491 x_msg_count                 OUT  nocopy NUMBER,
3492 x_msg_data                  OUT  nocopy VARCHAR2
3493 )
3494 IS
3495 
3496 CURSOR c_process IS
3497 SELECT organization_id, process_id, revision_number, process_org_rev_id, account_process_flag
3498 FROM AMW_FIN_PROCESS_EVAL_SUM
3499 WHERE FIN_CERTIFICATION_ID = p_certification_id;
3500 
3501    l_error_message varchar2(4000);
3502 
3503  l_api_name           CONSTANT VARCHAR2(30) := 'Refresh_All_Fin_Proc_Eval_Sum';
3504 l_api_version_number CONSTANT NUMBER       := 1.0;
3505 
3506 l_return_status VARCHAR2(32767);
3507 l_msg_count NUMBER;
3508 l_msg_data VARCHAR2(32767);
3509 
3510 
3511 
3512 BEGIN
3513 
3514     SAVEPOINT Refresh_All_Fin_Proc_Eval_Sum;
3515 
3516     -- Standard call to check for call compatibility.
3517         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
3518                                              p_api_version_number,
3519                                              l_api_name,
3520                                              G_PKG_NAME)
3521         THEN
3522                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3523         END IF;
3524 
3525         -- Initialize message list if p_init_msg_list is set to TRUE.
3526         IF FND_API.to_Boolean( p_init_msg_list )
3527         THEN
3528                 FND_MSG_PUB.initialize;
3529         END IF;
3530 
3531         -- Initialize API return status to SUCCESS
3532         x_return_status := FND_API.G_RET_STS_SUCCESS;
3533 
3534 
3535         FOR process_rec IN c_process LOOP
3536          	exit when c_process%notfound;
3537 
3538           Populate_Fin_Process_Eval_Sum
3539           ( p_certification_id => p_certification_id,
3540             p_start_date => null,
3541             p_end_date =>  null,
3542             p_process_org_rev_id => process_rec.process_org_rev_id,
3543             p_process_id => process_rec.process_id,
3544             p_revision_number	 => process_rec.revision_number,
3545             p_organization_id => process_rec.organization_id,
3546             p_account_process_flag => process_rec.account_process_flag,
3547           x_return_status   => l_return_status,
3548           x_msg_count  => l_msg_count,
3549           x_msg_data    => l_msg_data);
3550 
3551         END LOOP;
3552 
3553 if(p_commit <> FND_API.g_false)
3554 then commit;
3555 end if;
3556 
3557 
3558 x_return_status := FND_API.G_RET_STS_SUCCESS;
3559 
3560 EXCEPTION
3561      WHEN NO_DATA_FOUND THEN
3562      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
3563      x_return_status := FND_API.G_RET_STS_ERROR;
3564      x_msg_count := 1;
3565      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
3566       WHEN OTHERS THEN
3567        ROLLBACK TO Refresh_All_Fin_Proc_Eval_Sum;
3568       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
3569       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
3570         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3571         x_msg_count := 1;
3572         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
3573                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
3574                 FND_MSG_PUB.Count_And_Get(
3575                 p_encoded =>  FND_API.G_FALSE,
3576                 p_count   =>  x_msg_count,
3577                 p_data    =>  x_msg_data);
3578                 RETURN;
3579 
3580 END Refresh_All_Fin_Proc_Eval_Sum;
3581 
3582 
3583 PROCEDURE Populate_All_Fin_Proc_Eval_Sum(
3584 p_api_version_number        IN   NUMBER   := 1.0,
3585 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
3586 p_commit                    IN   VARCHAR2 := FND_API.g_false,
3587 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
3588 p_certification_id  IN       NUMBER,
3589 x_return_status             OUT  nocopy VARCHAR2,
3590 x_msg_count                 OUT  nocopy NUMBER,
3591 x_msg_data                  OUT  nocopy VARCHAR2
3592 )
3593 IS
3594 
3595  -- select all the processes based on the certification_id
3596 ---this is for later use after we uptake multiple process certifications
3597 /*CURSOR c_process(p_certification_id NUMBER) IS
3598 SELECT 	distinct aa.pk1 organization_id, aa.pk2 process_id, po.revision_number, po.process_org_rev_id
3599         FROM   	AMW_ACCT_ASSOCIATIONS aa,
3600                	AMW_FIN_ITEMS_KEY_ACC fika,
3601 	      	AMW_PROCESS_ORGANIZATION po,
3602 		AMW_CERTIFICATION_B cert
3603         WHERE  	aa.object_type = 'PROCESS_ORG'
3604 	AND     aa.pk1 = po.organization_id
3605         AND     aa.pk2 = po.process_id
3606         AND     aa.approval_date is not null
3607         AND 	aa.approval_date = po.approval_date
3608    	AND 	aa.deletion_approval_date is null
3609    	AND	po.approval_end_date is null
3610    	AND po.revision_number = (select max(revision_number) from amw_process_organization
3611        where organization_id = aa.pk1 and process_id = aa.pk2 and approval_date = aa.approval_date)
3612     	AND    	fika.statement_group_id =  cert.statement_group_id
3613 	AND     fika.financial_statement_id = cert.financial_statement_id
3614 	AND     cert.certification_id = p_certification_id
3615     	AND    	aa.natural_account_id in
3616 		( select  acc.child_natural_account_id
3617 		  from AMW_FIN_KEY_ACCT_FLAT acc
3618 		  where acc.parent_natural_account_id = fika.natural_account_id
3619 		  and acc.account_group_id = fika.account_group_id
3620 		  union all
3621 		  select acc.natural_account_id
3622 		  from amw_fin_key_accounts_b acc
3623 	  	  where acc.natural_account_id = fika.natural_account_id
3624 		  and acc.account_group_id = fika.account_group_id
3625 		); */
3626 
3627 -- psomanat : 01/09/2007 : Modified the cursor for performance for bug 5683030
3628 /*CURSOR c_process(p_certification_id NUMBER) IS
3629 SELECT 	distinct aa.pk1 organization_id, aa.pk2 process_id, po.revision_number, po.process_org_rev_id
3630         FROM   	AMW_ACCT_ASSOCIATIONS aa,
3631                	AMW_FIN_ITEMS_KEY_ACC fika,
3632 	      	AMW_PROCESS_ORGANIZATION po,
3633 		AMW_CERTIFICATION_B cert
3634         WHERE  cert.certification_id = p_certification_id
3635         	AND aa.object_type = 'PROCESS_ORG'
3636 	AND     aa.pk1 = po.organization_id
3637         AND     aa.pk2 = po.process_id
3638         AND     aa.approval_date is not null
3639        -- AND 	aa.approval_date = po.approval_date
3640    --	AND 	aa.deletion_approval_date is null
3641    --	AND     po.approval_date = aa.approval_date
3642     	AND     po.approval_status = 'A'
3643     	AND	po.approval_end_date is null
3644     	AND      po.approval_date is not null
3645 		09.12.2006 npanandi: LORAL issue, only include those Processes
3646 	      which are not disassociated from the Organizations, and whose disassociations
3647 		  are not yet approved
3648 
3649 		and po.deletion_date is NULL 09.12.2006 npanandi: ends fix for LORAL issue
3650     	AND    	fika.statement_group_id =  cert.statement_group_id
3651 	AND     fika.financial_statement_id = cert.financial_statement_id
3652     	 AND EXISTS
3653      	 ( SELECT CHILD_NATURAL_ACCOUNT_ID
3654        	 FROM AMW_FIN_KEY_ACCT_FLAT ACC
3655         	WHERE
3656          	ACC.CHILD_NATURAL_ACCOUNT_ID = AA.NATURAL_ACCOUNT_ID
3657          	AND ACC.PARENT_NATURAL_ACCOUNT_ID = FIKA.NATURAL_ACCOUNT_ID
3658          	AND ACC.ACCOUNT_GROUP_ID = FIKA.ACCOUNT_GROUP_ID
3659         	UNION ALL
3660         	SELECT ACC.NATURAL_ACCOUNT_ID
3661         	FROM AMW_FIN_KEY_ACCOUNTS_B ACC
3662         	WHERE
3663         	ACC.NATURAL_ACCOUNT_ID = AA.NATURAL_ACCOUNT_ID
3664          	AND ACC.NATURAL_ACCOUNT_ID = FIKA.NATURAL_ACCOUNT_ID
3665          	AND ACC.ACCOUNT_GROUP_ID = FIKA.ACCOUNT_GROUP_ID
3666       	); */
3667 -- psomanat : 01/09/2007 : Modified the cursor for performance for bug 5683030
3668 CURSOR c_process(p_certification_id NUMBER) IS
3669 SELECT  PO.ORGANIZATION_ID,
3670         PO.PROCESS_ID,
3671         PO.REVISION_NUMBER,
3672         PO.PROCESS_ORG_REV_ID
3673 FROM AMW_PROCESS_ORGANIZATION PO ,amw_audit_units_v aauv
3674 WHERE /*02.13.07 npanandi: bug 5043879 fix for
3675         including only those orgs that are active */
3676       po.organization_id = aauv.organization_id
3677   and
3678 /*02.13.07 npanandi: bug 5043879 fix ends*/
3679        EXISTS (
3680     SELECT AA.PK1
3681     FROM  AMW_ACCT_ASSOCIATIONS AA,
3682      (  SELECT ACC.CHILD_NATURAL_ACCOUNT_ID NATURAL_ACCOUNT_ID
3683         FROM AMW_FIN_KEY_ACCT_FLAT ACC,
3684              AMW_CERTIFICATION_B CERT,
3685              AMW_FIN_ITEMS_KEY_ACC FIKA
3686         WHERE CERT.CERTIFICATION_ID = p_certification_id
3687             AND   FIKA.STATEMENT_GROUP_ID = CERT.STATEMENT_GROUP_ID
3688             AND   FIKA.FINANCIAL_STATEMENT_ID = CERT.FINANCIAL_STATEMENT_ID
3689             AND   FIKA.NATURAL_ACCOUNT_ID = ACC.PARENT_NATURAL_ACCOUNT_ID
3690             AND   FIKA.ACCOUNT_GROUP_ID   = ACC.ACCOUNT_GROUP_ID
3691         UNION ALL
3692         SELECT ACC.NATURAL_ACCOUNT_ID
3693         FROM AMW_FIN_KEY_ACCOUNTS_B ACC,
3694              AMW_CERTIFICATION_B CERT,
3695              AMW_FIN_ITEMS_KEY_ACC FIKA
3696             WHERE CERT.CERTIFICATION_ID = p_certification_id
3697             AND   FIKA.STATEMENT_GROUP_ID = CERT.STATEMENT_GROUP_ID
3698             AND   FIKA.FINANCIAL_STATEMENT_ID = CERT.FINANCIAL_STATEMENT_ID
3699             AND FIKA.ACCOUNT_GROUP_ID = ACC.ACCOUNT_GROUP_ID
3700             AND FIKA.NATURAL_ACCOUNT_ID = ACC.NATURAL_ACCOUNT_ID
3701      ) CF
3702     WHERE AA.NATURAL_ACCOUNT_ID = CF.NATURAL_ACCOUNT_ID
3703     AND AA.OBJECT_TYPE = 'PROCESS_ORG'
3704     AND   AA.PK1 = PO.ORGANIZATION_ID
3705     AND   AA.PK2 = PO.PROCESS_ID
3706     AND   AA.APPROVAL_DATE IS NOT NULL
3707 )
3708 AND   PO.APPROVAL_STATUS = 'A'
3709 AND   PO.APPROVAL_END_DATE IS NULL
3710 AND   PO.APPROVAL_DATE IS NOT NULL
3711 AND   PO.DELETION_DATE IS NULL;
3712 
3713 
3714 ---for later use when multiple process certification is uptaken
3715 /*CURSOR c_child_processes(p_proc_id NUMBER, p_org_id NUMBER) IS
3716  SELECT  distinct temp.child_process_id, temp.organization_id, orgproc.revision_number,
3717          orgproc.process_org_rev_id
3718         FROM    AMW_FIN_PROCESS_FLAT temp,
3719         	AMW_PROCESS_ORGANIZATION orgproc
3720         WHERE   temp.parent_process_id = p_proc_id
3721         AND     temp.organization_id = p_org_id
3722         AND 	temp.certification_id = p_certification_id
3723         AND 	orgproc.process_id = temp.child_process_id
3724         AND 	orgproc.organization_id = temp.organization_id
3725         AND 	orgproc.approval_date is not null
3726         AND	orgproc.approval_end_date is null
3727         AND     orgproc.revision_number = (select max(revision_number) from AMW_PROCESS_ORGANIZATION orgproc2
3728         	where orgproc2.process_id = orgproc.process_id
3729         	and orgproc2.organization_id = orgproc.organization_id);
3730 --------------------------------------------------------*/
3731 /*
3732 CURSOR c_child_processes(p_proc_id NUMBER, p_org_id NUMBER) IS
3733  SELECT  distinct proc.child_process_id, proc.organization_id
3734         FROM    AMW_FIN_PROCESS_FLAT proc,
3735         WHERE   proc.child_process_id <> -2
3736         AND     proc.parent_process_id = p_proc_id
3737         AND     proc.organization_id = p_org_id
3738         AND 	proc.fin_certification_id = p_certification_id;
3739 */
3740 
3741 CURSOR c_child_processes(p_proc_id NUMBER, p_org_id NUMBER) IS
3742  SELECT  distinct temp.child_process_id, temp.organization_id, orgproc.revision_number,
3743          orgproc.process_org_rev_id
3744         FROM    AMW_FIN_PROCESS_FLAT temp,
3745         	AMW_PROCESS_ORGANIZATION orgproc, amw_audit_units_v aauv
3746         WHERE   /*02.13.07 npanandi: bug 5043879 fix for
3747                   including only those orgs that are active */
3748               orgproc.organization_id = aauv.organization_id
3749           and
3750                 /*02.13.07 npanandi: bug 5043879 fix ends*/
3751                 temp.child_process_id <> -2
3752         AND     temp.parent_process_id = p_proc_id
3753         AND     temp.organization_id = p_org_id
3754         AND 	temp.fin_certification_id = p_certification_id
3755         AND 	orgproc.process_id = temp.child_process_id
3756         AND 	orgproc.organization_id = temp.organization_id
3757         AND 	orgproc.approval_date is not null
3758         AND	orgproc.approval_end_date is null
3759 		/**09.12.2006 npanandi: LORAL issue, only include those Processes
3760 	      which are not disassociated from the Organizations, and whose disassociations
3761 		  are not yet approved
3762 		**/
3763 		and orgproc.deletion_date is NULL /*09.12.2006 npanandi: ends fix for LORAL issue**/;
3764 
3765 
3766     -- org process certified
3767     CURSOR org_processes_certified(p_cert_id NUMBER,p_process_id NUMBER) IS
3768         SELECT  count(distinct aov.pk3_value)
3769         FROM    AMW_OPINION_MV aov
3770         WHERE   aov.object_name = 'AMW_ORG_PROCESS'
3771         AND     aov.opinion_type_code = 'CERTIFICATION'
3772         AND 	aov.opinion_component_code = 'OVERALL'
3773         AND     aov.pk3_value <> NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'),-999)
3774         AND     aov.pk1_value = p_process_id
3775 	AND     aov.pk3_value in (select distinct evalsum.organization_id
3776                                 from amw_fin_process_eval_sum evalsum
3777                                 where evalsum.fin_certification_id = p_cert_id
3778 				  and evalsum.process_id=p_process_id)
3779 	AND     aov.pk2_value in (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
3780            			where fin_stmt_cert_id = p_cert_id and end_date is null );
3781 
3782     CURSOR total_org_processes(p_cert_id NUMBER,p_process_id NUMBER) IS
3783         select count(distinct evalsum.organization_id)
3784         from   amw_fin_process_eval_sum evalsum
3785         where  evalsum.fin_certification_id = p_cert_id
3786 	and    evalsum.process_id=p_process_id
3787         and    evalsum.organization_id <>
3788 		NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'),-999);
3789 
3790 
3791     --Begin variant process
3792 
3793     CURSOR var_procs(p_cert_id NUMBER) IS
3794         SELECT  fin_certification_id,organization_id,process_id
3795         FROM    AMW_FIN_PROCESS_EVAL_SUM
3796         WHERE   fin_certification_id = p_cert_id
3797         AND     organization_id = NVL(fnd_profile.value('AMW_GLOBAL_ORG_ID'),-999);
3798 
3799     CURSOR org_var_processes_certified(p_cert_id NUMBER,p_start_date DATE,p_end_date DATE,p_process_id NUMBER) IS
3800         select count(1) from
3801           (SELECT  distinct aov.pk3_value,aov.pk1_value
3802            FROM    AMW_OPINION_MV aov
3803            WHERE   aov.object_name = 'AMW_ORG_PROCESS'
3804            AND     aov.opinion_type_code = 'CERTIFICATION'
3805            AND 	   aov.opinion_component_code = 'OVERALL'
3806 	   AND     aov.pk2_value in (
3807 			select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
3808            		where fin_stmt_cert_id = p_cert_id
3809            		and end_date is null)
3810            AND     aov.pk1_value in (SELECT  varproc.process_id
3811                                      FROM    AMW_PROCESS varproc
3812                                      WHERE   varproc.standard_variation = p_process_id
3813                                      AND     varproc.standard_process_flag = 'N'
3814                                      AND     varproc.process_id in (select distinct evalsum.process_id
3815                                                                     from amw_fin_process_eval_sum evalsum
3816                                                                     where evalsum.fin_certification_id = p_cert_id)));
3817 
3818 
3819     CURSOR total_org_var_processes(p_cert_id NUMBER,p_process_id NUMBER) IS
3820         select count(1) from
3821           (select distinct evalsum.organization_id,evalsum.process_id
3822            from amw_fin_process_eval_sum evalsum
3823            where evalsum.fin_certification_id = p_cert_id
3824            and   evalsum.process_id in (select varproc.process_id
3825                                         FROM    AMW_PROCESS varproc
3826                                         WHERE   varproc.standard_variation = p_process_id
3827                                         AND     varproc.standard_process_flag = 'N'));
3828 
3829     -- end variant process
3830 
3831     --l_start_date DATE;
3832     --l_end_date   DATE;
3833     l_org_var_processes_certified NUMBER;
3834     l_org_processes_certified NUMBER;
3835     l_total_org_var_processes     NUMBER;
3836     l_total_org_processes     NUMBER;
3837 
3838     l_error_message varchar2(4000);
3839 
3840  l_api_name           CONSTANT VARCHAR2(30) := 'Populate_All_Fin_Proc_Eval_Sum';
3841 l_api_version_number CONSTANT NUMBER       := 1.0;
3842 
3843 l_return_status VARCHAR2(32767);
3844 l_msg_count NUMBER;
3845 l_msg_data VARCHAR2(32767);
3846 
3847 BEGIN
3848 
3849     SAVEPOINT Populate_All_Fin_Proc_Eval_Sum;
3850 
3851     -- Standard call to check for call compatibility.
3852         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
3853                                              p_api_version_number,
3854                                              l_api_name,
3855                                              G_PKG_NAME)
3856         THEN
3857                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3858         END IF;
3859 
3860         -- Initialize message list if p_init_msg_list is set to TRUE.
3861         IF FND_API.to_Boolean( p_init_msg_list )
3862         THEN
3863                 FND_MSG_PUB.initialize;
3864         END IF;
3865 
3866         -- Initialize API return status to SUCCESS
3867         x_return_status := FND_API.G_RET_STS_SUCCESS;
3868 
3869 
3870  FOR process_rec IN c_process(p_certification_id) LOOP
3871          	exit when c_process%notfound;
3872           POPULATE_PROC_HIERARCHY
3873           (     p_certification_id => p_certification_id,
3874                 P_PROCESS_ID => process_rec.process_id,
3875                 P_ORGANIZATION_ID => process_rec.organization_id,
3876                 p_account_process_flag => 'Y',
3877          	x_return_status   => l_return_status,
3878 		x_msg_count  => l_msg_count,
3879 		x_msg_data    => l_msg_data);
3880 
3881           Populate_Fin_Process_Eval_Sum
3882           ( p_certification_id => p_certification_id,
3883             p_start_date => null,
3884             p_end_date =>  null,
3885             p_process_org_rev_id => process_rec.process_org_rev_id,
3886             p_process_id => process_rec.process_id,
3887             p_revision_number	 => process_rec.revision_number,
3888             p_organization_id => process_rec.organization_id,
3889             p_account_process_flag => 'Y',
3890           x_return_status   => l_return_status,
3891           x_msg_count  => l_msg_count,
3892           x_msg_data    => l_msg_data);
3893 
3894           --Populate data for child processes
3895 	  FOR child_rec IN c_child_processes(process_rec.process_id,process_rec.organization_id) LOOP
3896 	    	exit when c_child_processes%notfound;
3897 
3898 	    POPULATE_PROC_HIERARCHY
3899 	     (     p_certification_id => p_certification_id,
3900                 P_PROCESS_ID => child_rec.child_process_id,
3901                 P_ORGANIZATION_ID => child_rec.organization_id,
3902                 p_account_process_flag => 'Y',
3903          	x_return_status   => l_return_status,
3904 	x_msg_count  => l_msg_count,
3905 	x_msg_data    => l_msg_data);
3906 
3907             Populate_Fin_Process_Eval_Sum
3908             ( p_certification_id => p_certification_id,
3909             p_start_date => null,
3910             p_end_date =>  null,
3911             p_process_org_rev_id => child_rec.process_org_rev_id,
3912             p_process_id => child_rec.child_process_id,
3913             p_revision_number	 => child_rec.revision_number,
3914             p_organization_id => child_rec.organization_id,
3915             p_account_process_flag => 'N',
3916           x_return_status   => l_return_status,
3917           x_msg_count  => l_msg_count,
3918           x_msg_data    => l_msg_data);
3919 
3920           END LOOP;
3921         END LOOP;
3922 
3923 
3924         -- Handle varient processes
3925         FOR var_rec IN var_procs(p_certification_id) LOOP
3926                exit when var_procs%notfound;
3927 
3928           OPEN org_var_processes_certified(var_rec.fin_certification_id,null,null,var_rec.process_id);
3929           FETCH org_var_processes_certified INTO l_org_var_processes_certified;
3930           CLOSE org_var_processes_certified;
3931 
3932           OPEN org_processes_certified(var_rec.fin_certification_id,var_rec.process_id);
3933           FETCH org_processes_certified INTO l_org_processes_certified;
3934           CLOSE org_processes_certified;
3935 
3936           l_org_processes_certified := l_org_processes_certified+
3937 					l_org_var_processes_certified;
3938 
3939           OPEN total_org_var_processes(var_rec.fin_certification_id,var_rec.process_id);
3940           FETCH total_org_var_processes INTO l_total_org_var_processes;
3941           CLOSE total_org_var_processes;
3942 
3943           OPEN total_org_processes(var_rec.fin_certification_id,var_rec.process_id);
3944           FETCH total_org_processes INTO l_total_org_processes;
3945           CLOSE total_org_processes;
3946 
3947           l_total_org_processes := l_total_org_processes+
3948 				    l_total_org_var_processes;
3949 
3950           UPDATE AMW_FIN_PROCESS_EVAL_SUM
3951           SET  LAST_UPDATE_DATE = sysdate,
3952    		last_updated_by = fnd_global.user_id,
3953    		last_update_login = fnd_global.conc_login_id,
3954              TOTAL_NUMBER_OF_ORG_PROCS = l_total_org_processes,
3955               NUMBER_OF_ORG_PROCS_CERTIFIED = l_org_processes_certified,
3956               ORG_PROCS_CERTIFIED_PRCNT =
3957 	     round(((l_org_processes_certified/decode(l_total_org_processes,0,1,l_total_org_processes)) *100),0)
3958           WHERE FIN_CERTIFICATION_ID = var_rec.fin_certification_id
3959           AND   ORGANIZATION_ID = var_rec.organization_id
3960           AND   PROCESS_ID = var_rec.process_id;
3961 
3962         END LOOP;
3963 if(p_commit <> FND_API.g_false)
3964 then commit;
3965 end if;
3966 
3967 
3968 
3969  x_return_status := FND_API.G_RET_STS_SUCCESS;
3970 
3971 EXCEPTION
3972      WHEN NO_DATA_FOUND THEN
3973      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
3974      x_return_status := FND_API.G_RET_STS_ERROR;
3975      x_msg_count := 1;
3976      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
3977       WHEN OTHERS THEN
3978        ROLLBACK TO Populate_All_Fin_Proc_Eval_Sum;
3979       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
3980       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
3981         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3982         x_msg_count := 1;
3983         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
3984                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
3985                 FND_MSG_PUB.Count_And_Get(
3986                 p_encoded =>  FND_API.G_FALSE,
3987                 p_count   =>  x_msg_count,
3988                 p_data    =>  x_msg_data);
3989                 RETURN;
3990 
3991 
3992 END Populate_All_Fin_Proc_Eval_Sum;
3993 
3994 
3995 PROCEDURE  Populate_Fin_Process_Eval_Sum(
3996 p_api_version_number        IN   NUMBER   := 1.0,
3997 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
3998 p_commit                    IN   VARCHAR2 := FND_API.g_false,
3999 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
4000 p_certification_id          IN      NUMBER,
4001 p_start_date                IN      DATE,
4002 p_end_date                  IN      DATE,
4003 p_process_org_rev_id	IN   	NUMBER,
4004 p_process_id   		IN	NUMBER,
4005 p_revision_number		IN	NUMBER,
4006 p_organization_id		IN 	NUMBER,
4007 p_account_process_flag      IN      VARCHAR2,
4008 x_return_status             OUT  nocopy VARCHAR2,
4009 x_msg_count                 OUT  nocopy NUMBER,
4010 x_msg_data                  OUT  nocopy VARCHAR2
4011 )
4012 IS
4013 
4014 
4015      CURSOR sub_processes_certified IS
4016     	SELECT  count(distinct aov.pk1_value)
4017       	FROM  	AMW_OPINION_MV aov
4018         WHERE 	aov.object_name = 'AMW_ORG_PROCESS'
4019         AND 	aov.opinion_type_code = 'CERTIFICATION'
4020         AND 	aov.opinion_component_code = 'OVERALL'
4021         AND 	aov.pk3_value = p_organization_id
4022 	AND     aov.pk2_value in (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
4023            			where fin_stmt_cert_id = p_certification_id
4024            			and end_date is null)
4025         AND     aov.pk1_value in (select distinct(orgrel.child_process_id)
4026                                   from AMW_FIN_PROCESS_FLAT orgrel
4027                                   where orgrel.parent_process_id = p_process_id
4028                                   and orgrel.organization_id = p_organization_id
4029                                   and orgrel.fin_certification_id = p_certification_id
4030                                   );
4031 
4032     CURSOR total_sub_processes IS
4033         SELECT  count(distinct child_process_id)
4034         FROM    AMW_FIN_PROCESS_FLAT
4035     	WHERE   parent_process_id = p_process_id
4036         AND     organization_id = p_organization_id
4037         AND 	child_process_id <> -2
4038         AND   fin_certification_id = p_certification_id;
4039 
4040 
4041     CURSOR certification_result IS
4042         SELECT 	aov.opinion_id
4043         FROM 	AMW_OPINION_MV aov
4044         WHERE 	aov.object_name = 'AMW_ORG_PROCESS'
4045         AND 	aov.opinion_type_code = 'CERTIFICATION'
4046         AND 	aov.opinion_component_code = 'OVERALL'
4047         AND 	aov.pk3_value = p_organization_id
4048 	AND     aov.pk2_value in (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
4049            			where fin_stmt_cert_id = p_certification_id
4050            			and end_date is null)
4051         AND 	aov.pk1_value = p_process_id
4052         AND 	aov.authored_date = (select max(aov2.authored_date)
4053                                      from AMW_OPINIONS aov2
4054                                      where aov2.object_opinion_type_id = aov.object_opinion_type_id
4055                                      and aov2.pk3_value = aov.pk3_value
4056                                      and aov2.pk1_value = aov.pk1_value
4057 				     and aov2.pk2_value in
4058 				       (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
4059            			        where fin_stmt_cert_id = p_certification_id
4060            			        and end_date is null));
4061 
4062 
4063 
4064     CURSOR certification_result_history(p_opinion_id number) IS
4065         SELECT 	max(opinion_log_id)
4066         FROM 	AMW_OPINIONS_LOG
4067         WHERE 	opinion_id = p_opinion_id;
4068 
4069 
4070     CURSOR last_evaluation IS
4071         SELECT 	distinct aov.opinion_id
4072 	FROM 	AMW_OPINION_MV aov
4073        	WHERE 	aov.object_name = 'AMW_ORG_PROCESS'
4074         AND 	aov.opinion_type_code = 'EVALUATION'
4075         AND 	aov.opinion_component_code = 'OVERALL'
4076         AND 	aov.pk3_value = p_organization_id
4077         AND 	aov.pk1_value = p_process_id
4078         --fix bug 5724066
4079 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
4080         AND 	aov.authored_date = (select max(aov2.authored_date)
4081                        	             from AMW_OPINIONS aov2
4082                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
4083                                      and aov2.pk3_value = aov.pk3_value
4084                                      and aov2.pk1_value = aov.pk1_value);
4085 
4086      CURSOR last_evaluation_history(p_opinion_id number) IS
4087      select max(opinion_log_id)
4088      from amw_opinions_log
4089      where opinion_id = p_opinion_id;
4090 
4091 
4092 CURSOR unmitigated_risks IS
4093 SELECT 	count(1)
4094        	FROM
4095        		AMW_RISK_ASSOCIATIONS ara,
4096             		AMW_FIN_PROCESS_FLAT orgrel,
4097            		 AMW_OPINION_MV aov
4098         WHERE   ara.object_type = 'PROCESS_ORG'
4099         AND      ara.pk1 = p_organization_id
4100         AND     orgrel.fin_certification_id = p_certification_id
4101         AND     ara.pk1 = orgrel.organization_id
4102         AND     ((ara.pk2 = p_process_id and orgrel.parent_process_id = ara.pk2 and orgrel.child_process_id = -2)
4103         	or (ara.pk2 = orgrel.child_process_id and orgrel.parent_process_id = p_process_id))
4104         AND 	ara.approval_date is not null
4105         AND 	ara.deletion_approval_date is null
4106         AND 	aov.object_name = 'AMW_ORG_PROCESS_RISK'
4107         AND 	aov.opinion_type_code = 'EVALUATION'
4108         AND 	aov.opinion_component_code = 'OVERALL'
4109         AND 	aov.pk3_value = ara.pk1
4110         AND 	aov.pk4_value = ara.pk2
4111         AND 	aov.pk1_value = ara.risk_id
4112         --fix bug 5724066
4113 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
4114         AND 	aov.authored_date = (select max(aov2.authored_date)
4115                                      from AMW_OPINIONS  aov2
4116                                      where aov2.object_opinion_type_id = aov.object_opinion_type_id
4117                                      and aov2.pk4_value = aov.pk4_value
4118                                      and aov2.pk3_value = aov.pk3_value
4119                                      and aov2.pk1_value = aov.pk1_value)
4120       	AND 	aov.OPINION_VALUE_CODE <> 'EFFECTIVE';
4121 
4122 
4123 
4124   CURSOR total_risks IS
4125   SELECT count(1)
4126        	FROM
4127        		AMW_RISK_ASSOCIATIONS ara,
4128             AMW_FIN_PROCESS_FLAT orgrel
4129         WHERE   ara.object_type = 'PROCESS_ORG'
4130         AND 	ara.pk1 = p_organization_id
4131         and     orgrel.fin_certification_id = p_certification_id
4132         and     orgrel.organization_id = ara.pk1
4133         AND     ((ara.pk2 = p_process_id and orgrel.parent_process_id = ara.pk2 and orgrel.child_process_id = -2)
4134             or (ara.pk2 = orgrel.child_process_id and orgrel.parent_process_id = p_process_id))
4135         AND 	ara.approval_date is not null
4136         AND 	ara.deletion_approval_date is null;
4137 
4138 
4139  --why distinct? because the same risk could be evaluated in different projects. so in opinion_mv has several records corresponding to
4140  -- one risk.
4141  CURSOR risks_verified IS
4142   SELECT 	count(1)
4143  from (select distinct ara.pk1, ara.pk2, ara.risk_id
4144        	FROM
4145        		AMW_RISK_ASSOCIATIONS ara,
4146                 	AMW_OPINION_MV aov,
4147                 	AMW_FIN_PROCESS_FLAT orgrel
4148         WHERE   ara.object_type = 'PROCESS_ORG'
4149         AND 	ara.pk1 = p_organization_id
4150         AND     orgrel.fin_certification_id = p_certification_id
4151         and     orgrel.organization_id = ara.pk1
4152         AND     ( (ara.pk2 = p_process_id and ara.pk2 = orgrel.parent_process_id and orgrel.child_process_id = -2)
4153         	or (ara.pk2 = orgrel.child_process_id and orgrel.parent_process_id = p_process_id))
4154         AND 	ara.approval_date is not null
4155         AND 	ara.deletion_approval_date is null
4156         AND 	aov.object_name = 'AMW_ORG_PROCESS_RISK'
4157         AND 	aov.opinion_type_code = 'EVALUATION'
4158         AND 	aov.opinion_component_code = 'OVERALL'
4159         AND 	aov.pk3_value = ara.pk1
4160         AND 	aov.pk4_value = ara.pk2
4161         AND 	aov.pk1_value = ara.risk_id
4162         --fix bug 5724066
4163 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
4164         );
4165 
4166 
4167  CURSOR ineffective_controls IS
4168   SELECT 	count(distinct aca.control_id)
4169         FROM
4170 		AMW_CONTROL_ASSOCIATIONS aca,
4171         AMW_FIN_PROCESS_FLAT orgrel,
4172         AMW_OPINION_MV aov
4173         WHERE
4174         aca.object_type = 'RISK_ORG'
4175         and aca.pk1   = p_organization_id
4176         and orgrel.fin_certification_id = p_certification_id
4177         and aca.pk1 = orgrel.organization_id
4178         and     aca.approval_date is not null
4179         and     aca.deletion_approval_date is null
4180         and     ( (aca.pk2 = p_process_id  and aca.pk2 = orgrel.parent_process_id and orgrel.child_process_id = -2)
4181                 or ( aca.pk2 = orgrel.child_process_id and orgrel.parent_process_id = p_process_id))
4182         AND 	aov.object_name = 'AMW_ORG_CONTROL'
4183         AND 	aov.opinion_type_code = 'EVALUATION'
4184         AND 	aov.opinion_component_code = 'OVERALL'
4185         AND 	aov.pk3_value = aca.pk1
4186         AND 	aov.pk1_value = aca.control_id
4187         --fix bug 5724066
4188 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
4189         AND 	aov.authored_date = (select max(aov2.authored_date)
4190                                      from AMW_OPINIONS  aov2
4191                                     where aov2.object_opinion_type_id = aov.object_opinion_type_id
4192                                      and aov2.pk3_value = aov.pk3_value
4193                                      and aov2.pk1_value = aov.pk1_value)
4194         AND 	aov.OPINION_VALUE_CODE <> 'EFFECTIVE';
4195 
4196 
4197 
4198  CURSOR total_controls IS
4199  SELECT 	count(distinct aca.control_id)
4200         FROM
4201 		AMW_CONTROL_ASSOCIATIONS aca,
4202         		AMW_FIN_PROCESS_FLAT orgrel
4203         WHERE   aca.object_type = 'RISK_ORG'
4204         and     aca.pk1 = p_organization_id
4205         and     orgrel.fin_certification_id = p_certification_id
4206         and     aca.pk1 = orgrel.organization_id
4207         and     aca.approval_date is not null
4208         and     aca.deletion_approval_date is null
4209         and     (( aca.pk2 = p_process_id and  orgrel.parent_process_id = aca.pk2 and orgrel.child_process_id = -2)
4210                 or (aca.pk2 = orgrel.child_process_id and orgrel.parent_process_id = p_process_id));
4211 
4212 
4213 CURSOR verified_controls IS
4214 SELECT 	count(distinct aca.control_id)
4215         FROM
4216 		AMW_CONTROL_ASSOCIATIONS aca,
4217         		AMW_FIN_PROCESS_FLAT orgrel,
4218        		AMW_OPINION_MV aov
4219         WHERE
4220         aca.object_type = 'RISK_ORG'
4221         and aca.pk1   = p_organization_id
4222         and orgrel.fin_certification_id = p_certification_id
4223         and orgrel.organization_id = aca.pk1
4224         and     aca.approval_date is not null
4225         and     aca.deletion_approval_date is null
4226         and     (( aca.pk2 = p_process_id  and aca.pk2 =  orgrel.parent_process_id and orgrel.child_process_id = -2)
4227             or (aca.pk2 = orgrel.child_process_id and orgrel.parent_process_id = p_process_id))
4228         AND 	aov.object_name = 'AMW_ORG_CONTROL'
4229         AND 	aov.opinion_type_code = 'EVALUATION'
4230         AND 	aov.opinion_component_code = 'OVERALL'
4231         AND 	aov.pk3_value = aca.pk1
4232         AND 	aov.pk1_value = aca.control_id
4233         --fix bug 5724066
4234 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
4235         ;
4236 
4237 
4238    l_sub_processes_certified		NUMBER;
4239    l_total_sub_processes		NUMBER;
4240    l_org_processes_certified		NUMBER;
4241    l_total_org_processes		NUMBER;
4242    l_cert_opinion_id			NUMBER;
4243    l_eval_opinion_id			NUMBER;
4244    l_last_evaluation			NUMBER;
4245    l_unmitigated_risks			NUMBER;
4246    l_total_risks			NUMBER;
4247    l_verified_risks			NUMBER;
4248    l_ineffective_controls		NUMBER;
4249    l_total_controls			NUMBER;
4250    l_verified_controls			NUMBER;
4251    l_open_findings                      NUMBER;
4252    l_count				NUMBER;
4253    l_cert_opinion_log_id		NUMBER;
4254    l_eval_opinion_log_id		NUMBER;
4255 
4256     l_certification_id                  NUMBER;
4257     l_process_id   		 	NUMBER;
4258     l_organization_id		 	NUMBER;
4259 
4260     l_error_message varchar2(4000);
4261 
4262 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_Process_Eval_Sum';
4263 l_api_version_number CONSTANT NUMBER       := 1.0;
4264 
4265 l_return_status VARCHAR2(32767);
4266 l_msg_count NUMBER;
4267 l_msg_data VARCHAR2(32767);
4268 
4269 l_new NUMBER;
4270 
4271 BEGIN
4272 
4273 SAVEPOINT Populate_Fin_Process_Eval_Sum;
4274 
4275  -- Standard call to check for call compatibility.
4276         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
4277                                              p_api_version_number,
4278                                              l_api_name,
4279                                              G_PKG_NAME)
4280         THEN
4281                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4282         END IF;
4283 
4284         -- Initialize message list if p_init_msg_list is set to TRUE.
4285         IF FND_API.to_Boolean( p_init_msg_list )
4286         THEN
4287                 FND_MSG_PUB.initialize;
4288         END IF;
4289 
4290         -- Initialize API return status to SUCCESS
4291         x_return_status := FND_API.G_RET_STS_SUCCESS;
4292 
4293 
4294          SELECT count(1) into l_count
4295      FROM AMW_FIN_PROCESS_EVAL_SUM sum
4296      WHERE sum.fin_certification_id = p_certification_id
4297      AND   sum.organization_id = p_organization_id
4298      AND   sum.process_id = p_process_id
4299      AND EXISTS ( SELECT 'Y' FROM AMW_FIN_CERT_SCOPE scp
4300      		  WHERE scp.fin_certification_id = sum.fin_certification_id
4301      		  and scp.organization_id = sum.organization_id
4302      		  and scp.process_id = sum.process_id
4303      		  and scp.process_id is not null);
4304 
4305 
4306      SELECT COUNT(1) INTO l_new FROM AMW_FIN_CERT_SCOPE
4307         WHERE FIN_CERTIFICATION_ID = p_certification_id;
4308 
4309      	IF( l_count = 0 and l_new <> 0 ) THEN RETURN;
4310      	END IF;
4311 
4312 	 l_certification_id   := p_certification_id;
4313     	 l_process_id   := p_process_id;
4314     	 l_organization_id := 	p_organization_id;
4315 
4316     	 l_total_org_processes := 0;
4317     	 l_org_processes_certified := 0;
4318 
4319 
4320     OPEN sub_processes_certified;
4321     FETCH sub_processes_certified INTO l_sub_processes_certified;
4322     CLOSE sub_processes_certified;
4323 
4324     OPEN total_sub_processes;
4325     FETCH total_sub_processes INTO l_total_sub_processes;
4326     CLOSE total_sub_processes;
4327 
4328 
4329     OPEN certification_result;
4330     FETCH certification_result INTO l_cert_opinion_id;
4331     CLOSE certification_result;
4332 
4333     OPEN last_evaluation;
4334     FETCH last_evaluation INTO l_eval_opinion_id;
4335     CLOSE last_evaluation;
4336 
4337     OPEN unmitigated_risks;
4338     FETCH unmitigated_risks INTO l_unmitigated_risks;
4339     CLOSE unmitigated_risks;
4340 
4341  OPEN total_risks;
4342  FETCH total_risks INTO l_total_risks;
4343  CLOSE total_risks;
4344 
4345  OPEN risks_verified;
4346  FETCH risks_verified INTO l_verified_risks;
4347  CLOSE risks_verified;
4348 
4349     OPEN ineffective_controls;
4350     FETCH ineffective_controls INTO l_ineffective_controls;
4351     CLOSE ineffective_controls;
4352 
4353     OPEN total_controls;
4354  FETCH total_controls INTO l_total_controls;
4355  CLOSE total_controls;
4356 
4357  OPEN verified_controls;
4358  FETCH verified_controls INTO l_verified_controls;
4359  CLOSE verified_controls;
4360 
4361     OPEN certification_result_history(l_cert_opinion_id);
4362     FETCH certification_result_history INTO l_cert_opinion_log_id;
4363     CLOSE certification_result_history;
4364 
4365     OPEN last_evaluation_history(l_eval_opinion_id);
4366     FETCH last_evaluation_history INTO l_eval_opinion_log_id;
4367     CLOSE last_evaluation_history;
4368 
4369 
4370     BEGIN
4371         l_open_findings := amw_findings_pkg.calculate_open_findings (
4372                'AMW_PROJ_FINDING',
4373                'PROJ_ORG_PROC', p_process_id,
4374                'PROJ_ORG', p_organization_id,
4375                null, null,
4376                null, null,
4377                null, null );
4378     EXCEPTION
4379         WHEN OTHERS THEN
4380             null;
4381     END;
4382 
4383 
4384 
4385    --first time to insert the process records for this certification
4386     IF (l_new = 0 and l_count = 0) THEN
4387 
4388        INSERT INTO AMW_FIN_PROCESS_EVAL_SUM(
4389          FIN_CERTIFICATION_ID,
4390          PROCESS_ID,
4391          ORGANIZATION_ID,
4392          CERT_OPINION_ID,
4393          EVAL_OPINION_ID,
4394          UNMITIGATED_RISKS,
4395          RISKS_VERIFIED,
4396          TOTAL_NUMBER_OF_RISKS,
4397          INEFFECTIVE_CONTROLS,
4398          CONTROLS_VERIFIED,
4399          TOTAL_NUMBER_OF_CTRLS,
4400          UNMITIGATED_RISKS_PRCNT,
4401          INEFFECTIVE_CONTROLS_PRCNT,
4402          TOTAL_NUMBER_OF_SUB_PROCS,
4403          NUMBER_OF_SUB_PROCS_CERTIFIED,
4404          SUB_PROCS_CERTIFIED_PRCNT,
4405          TOTAL_NUMBER_OF_ORG_PROCS,
4406          NUMBER_OF_ORG_PROCS_CERTIFIED,
4407          ORG_PROCS_CERTIFIED_PRCNT,
4408          OPEN_FINDINGS,
4409          ACCOUNT_PROCESS_FLAG,
4410          CREATED_BY,
4411          CREATION_DATE,
4412          LAST_UPDATE_DATE,
4413          LAST_UPDATED_BY,
4414          LAST_UPDATE_LOGIN,
4415          CERT_OPINION_LOG_ID,
4416          EVAL_OPINION_LOG_ID,
4417          REVISION_NUMBER,
4418          PROCESS_ORG_REV_ID)
4419         VALUES
4420         (     p_certification_id,
4421               p_process_id,
4422               p_organization_id,
4423               l_cert_opinion_id,
4424               l_eval_opinion_id,
4425               l_unmitigated_risks,
4426               l_verified_risks,
4427               l_total_risks,
4428               l_ineffective_controls,
4429               l_verified_controls,
4430               l_total_controls,
4431               round((nvl(l_unmitigated_risks, 0)/decode(nvl(l_total_risks,0),0,1,l_total_risks) *100),0),
4432               round((nvl(l_ineffective_controls, 0)/decode(nvl(l_total_controls,0),0,1,l_total_controls) *100),0),
4433               l_total_sub_processes,
4434               l_sub_processes_certified,
4435               round((nvl(l_sub_processes_certified, 0)/decode(nvl(l_total_sub_processes,0),0,1,l_total_sub_processes) *100),0),
4436               l_total_org_processes,
4437               l_org_processes_certified,
4438               round((nvl(l_org_processes_certified, 0)/decode(nvl(l_total_org_processes,0),0,1,l_total_org_processes) *100),0),
4439               l_open_findings,
4440               p_account_process_flag,
4441               FND_GLOBAL.USER_ID,
4442               SYSDATE,
4443               SYSDATE,
4444               FND_GLOBAL.USER_ID,
4445               FND_GLOBAL.USER_ID,
4446               l_cert_opinion_log_id,
4447               l_eval_opinion_log_id,
4448               p_revision_number,
4449               p_process_org_rev_id);
4450 
4451     ELSE
4452 
4453        UPDATE AMW_FIN_PROCESS_EVAL_SUM
4454        SET
4455          CERT_OPINION_ID = l_cert_opinion_id,
4456          EVAL_OPINION_ID = l_eval_opinion_id,
4457          UNMITIGATED_RISKS =  l_unmitigated_risks,
4458          RISKS_VERIFIED = l_verified_risks,
4459          TOTAL_NUMBER_OF_RISKS = l_total_risks,
4460          INEFFECTIVE_CONTROLS = l_ineffective_controls,
4461          CONTROLS_VERIFIED = l_verified_controls,
4462          TOTAL_NUMBER_OF_CTRLS = l_total_controls,
4463          UNMITIGATED_RISKS_PRCNT =  round((nvl(l_unmitigated_risks, 0)/decode(nvl(l_total_risks,0),0,1,l_total_risks) *100),0),
4464          INEFFECTIVE_CONTROLS_PRCNT =  round((nvl(l_ineffective_controls, 0)/decode(nvl(l_total_controls,0),0,1,l_total_controls) *100),0),
4465          TOTAL_NUMBER_OF_SUB_PROCS =  l_total_sub_processes,
4466          NUMBER_OF_SUB_PROCS_CERTIFIED =  l_sub_processes_certified,
4467          SUB_PROCS_CERTIFIED_PRCNT =  round((nvl(l_sub_processes_certified,0)/decode(nvl(l_total_sub_processes,0),0,1,l_total_sub_processes) *100),0),
4468          TOTAL_NUMBER_OF_ORG_PROCS = l_total_org_processes,
4469          NUMBER_OF_ORG_PROCS_CERTIFIED = l_org_processes_certified,
4470          ORG_PROCS_CERTIFIED_PRCNT = round((nvl(l_org_processes_certified, 0)/decode(nvl(l_total_org_processes,0),0,1,l_total_org_processes) *100),0),
4471          OPEN_FINDINGS =  l_open_findings,
4472          CREATED_BY = FND_GLOBAL.USER_ID,
4473          CREATION_DATE = SYSDATE,
4474          LAST_UPDATE_DATE = SYSDATE,
4475          LAST_UPDATED_BY = FND_GLOBAL.USER_ID,
4476          LAST_UPDATE_LOGIN  = fnd_global.conc_login_id,
4477          CERT_OPINION_LOG_ID = l_cert_opinion_log_id,
4478          EVAL_OPINION_LOG_ID =  l_eval_opinion_log_id
4479        WHERE FIN_CERTIFICATION_ID = p_certification_id
4480        AND 	PROCESS_ID = p_process_id
4481        AND 	ORGANIZATION_ID = p_organization_id
4482        AND      PROCESS_ORG_REV_ID = p_process_org_rev_id;
4483 
4484     END IF;
4485 
4486 x_return_status := FND_API.G_RET_STS_SUCCESS;
4487 
4488 EXCEPTION
4489      WHEN NO_DATA_FOUND THEN
4490      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
4491      x_return_status := FND_API.G_RET_STS_ERROR;
4492      x_msg_count := 1;
4493      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
4494       WHEN OTHERS THEN
4495        ROLLBACK TO Populate_Fin_Process_Eval_Sum;
4496       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
4497       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
4498         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4499         x_msg_count := 1;
4500         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
4501                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
4502                 FND_MSG_PUB.Count_And_Get(
4503                 p_encoded =>  FND_API.G_FALSE,
4504                 p_count   =>  x_msg_count,
4505                 p_data    =>  x_msg_data);
4506                 RETURN;
4507 
4508 
4509 END Populate_Fin_Process_Eval_Sum;
4510 
4511 /********************delete becuase put ceritification loop in master_... procedure *****************
4512 PROCEDURE Populate_All_Fin_Risk_Ass_Sum(
4513 p_api_version_number        IN   NUMBER   := 1.0,
4514 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
4515 p_commit                    IN   VARCHAR2 := FND_API.g_false,
4516 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
4517 p_certification_id  IN       NUMBER,
4518 x_return_status             OUT  nocopy VARCHAR2,
4519 x_msg_count                 OUT  nocopy NUMBER,
4520 x_msg_data                  OUT  nocopy VARCHAR2
4521 ) IS
4522 
4523 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_All_Fin_Risk_Ass_Sum';
4524 l_api_version_number CONSTANT NUMBER       := 1.0;
4525 
4526 l_return_status VARCHAR2(32767);
4527 l_msg_count NUMBER;
4528 l_msg_data VARCHAR2(32767);
4529 
4530 BEGIN
4531 
4532 SAVEPOINT Populate_All_Fin_Risk_Ass_Sum;
4533 
4534 Populate_Fin_Risk_Ass_Sum
4535 (p_certification_id => p_certification_id,
4536 x_return_status   => l_return_status,
4537 x_msg_count  => l_msg_count,
4538 x_msg_data    => l_msg_data);
4539 
4540 
4541  x_return_status := FND_API.G_RET_STS_SUCCESS;
4542 
4543  EXCEPTION
4544      WHEN NO_DATA_FOUND THEN
4545      IF c_cert%ISOPEN THEN
4546       	close c_cert;
4547       END IF;
4548      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
4549      x_return_status := FND_API.G_RET_STS_ERROR;
4550      x_msg_count := 1;
4551      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
4552       WHEN OTHERS THEN
4553       IF c_cert%ISOPEN THEN
4554       	close c_cert;
4555       END IF;
4556        ROLLBACK TO Populate_All_Fin_Risk_Ass_Sum;
4557       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
4558       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
4559         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4560         x_msg_count := 1;
4561         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
4562                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
4563                 FND_MSG_PUB.Count_And_Get(
4564                 p_encoded =>  FND_API.G_FALSE,
4565                 p_count   =>  x_msg_count,
4566                 p_data    =>  x_msg_data);
4567                 RETURN;
4568 
4569 END Populate_All_Fin_Risk_Ass_Sum;
4570 
4571 *********************************************/
4572 
4573 
4574 -------------populate risk association which related to financial certification ----
4575 PROCEDURE Populate_Fin_Risk_Ass_Sum(
4576 p_api_version_number        IN   NUMBER   := 1.0,
4577 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
4578 p_commit                    IN   VARCHAR2 := FND_API.g_false,
4579 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
4580 p_certification_id  IN       NUMBER,
4581 x_return_status             OUT  nocopy VARCHAR2,
4582 x_msg_count                 OUT  nocopy NUMBER,
4583 x_msg_data                  OUT  nocopy VARCHAR2
4584 ) IS
4585 
4586 CURSOR c_finrisks IS
4587 SELECT
4588 	risks.risk_id,
4589 	risks.PK1,
4590 	risks.PK2,
4591 	risks.ASSOCIATION_CREATION_DATE,
4592 	risks.APPROVAL_DATE,
4593 	risks.DELETION_DATE,
4594 	risks.DELETION_APPROVAL_DATE,
4595 	risk.RISK_REV_ID
4596 FROM
4597 	AMW_RISK_ASSOCIATIONS risks,
4598 	AMW_FIN_PROCESS_EVAL_SUM eval,
4599 	AMW_RISKS_B risk
4600 WHERE
4601 	eval.fin_certification_id = p_certification_id
4602 	and risk.risk_id = risks.risk_id
4603 	and risk.CURR_APPROVED_FLAG = 'Y'
4604 	and risks.object_type='PROCESS_ORG'
4605 	and risks.PK1 = eval.organization_id
4606 	and risks.PK2 = eval.process_id
4607 	and risks.approval_date is not null
4608 	and risks.approval_date <= sysdate
4609 	and risks.deletion_approval_date is null
4610 UNION ALL
4611 SELECT
4612 	risks.risk_id,
4613 	risks.PK1,
4614 	risks.PK2,
4615 	risks.ASSOCIATION_CREATION_DATE,
4616 	risks.APPROVAL_DATE,
4617 	risks.DELETION_DATE,
4618 	risks.DELETION_APPROVAL_DATE,
4619 	risk.RISK_REV_ID
4620 FROM
4621 	AMW_RISK_ASSOCIATIONS risks,
4622 	AMW_FIN_PROCESS_EVAL_SUM eval,
4623 	AMW_RISKS_B risk
4624 WHERE
4625 	eval.fin_certification_id = p_certification_id
4626 	and risk.risk_id = risks.risk_id
4627 	and risk.CURR_APPROVED_FLAG = 'Y'
4628 	and risks.object_type='ENTITY_RISK'
4629 	and risks.PK1 = eval.organization_id
4630 	and risks.approval_date is not null
4631 	and risks.approval_date <= sysdate
4632 	and risks.deletion_approval_date is null;
4633 
4634 	--in risk association table, if type = 'PROCESS_FINCERT', pk1=certification_id, pk2=organization_id, pk3=process_id, pk4=opinion_log_id
4635 	CURSOR last_evaluation(l_risk_id number, l_organization_id number, l_process_id number)  IS
4636         SELECT 	distinct aov.opinion_log_id
4637 	FROM 	AMW_OPINION_LOG_MV aov
4638        	WHERE 	aov.object_name = 'AMW_ORG_PROCESS_RISK'
4639         AND 	aov.opinion_type_code = 'EVALUATION'
4640         AND 	aov.pk3_value = l_organization_id
4641         AND 	aov.pk4_value = l_process_id
4642         AND	aov.pk1_value = l_risk_id
4643         --fix bug 5724066
4644         AND aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
4645 	AND 	aov.authored_date = (select max(aov2.authored_date)
4646                        	             from AMW_OPINIONS aov2
4647                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
4648                                      and aov2.pk3_value = aov.pk3_value
4649                                      and aov2.pk1_value = aov.pk1_value
4650                                      and aov2.pk4_value = aov.pk4_value);
4651 
4652 
4653 
4654 l_count NUMBER;
4655 m_opinion_log_id NUMBER;
4656 l_error_message varchar2(4000);
4657 
4658 
4659 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_Risk_Ass_Sum';
4660 l_api_version_number CONSTANT NUMBER       := 1.0;
4661 
4662 l_return_status VARCHAR2(32767);
4663 l_msg_count NUMBER;
4664 l_msg_data VARCHAR2(32767);
4665 
4666 BEGIN
4667 
4668 	SAVEPOINT Populate_Fin_Risk_Ass_Sum;
4669 
4670  -- Standard call to check for call compatibility.
4671         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
4672                                              p_api_version_number,
4673                                              l_api_name,
4674                                              G_PKG_NAME)
4675         THEN
4676                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4677         END IF;
4678 
4679         -- Initialize message list if p_init_msg_list is set to TRUE.
4680         IF FND_API.to_Boolean( p_init_msg_list )
4681         THEN
4682                 FND_MSG_PUB.initialize;
4683         END IF;
4684 
4685         -- Initialize API return status to SUCCESS
4686         x_return_status := FND_API.G_RET_STS_SUCCESS;
4687 
4688 
4689 	SELECT COUNT(1) INTO l_count FROM AMW_RISK_ASSOCIATIONS
4690 	WHERE object_type = 'PROCESS_FINCERT'
4691 	AND pk1 = p_certification_id;
4692 
4693 	IF (l_count = 0) THEN
4694 	FOR risk_rec IN c_finrisks LOOP
4695 	exit when c_finrisks%notfound;
4696 
4697 		m_opinion_log_id := null;
4698 		OPEN last_evaluation(risk_rec.risk_id, risk_rec.pk1, risk_rec.pk2);
4699 		FETCH last_evaluation INTO m_opinion_log_id;
4700 		CLOSE last_evaluation;
4701 
4702 
4703 
4704 		INSERT INTO AMW_RISK_ASSOCIATIONS(
4705  			       RISK_ASSOCIATION_ID,
4706 			       RISK_ID,
4707 			       PK1,
4708 			       PK2,
4709 			       PK3,
4710 			       PK4,
4711 			       CREATED_BY,
4712 			       CREATION_DATE,
4713 			       LAST_UPDATE_DATE,
4714 			       LAST_UPDATED_BY,
4715 			       LAST_UPDATE_LOGIN,
4716 			       OBJECT_VERSION_NUMBER,
4717 			       OBJECT_TYPE,
4718 			       ASSOCIATION_CREATION_DATE,
4719 			       APPROVAL_DATE,
4720 			       DELETION_DATE,
4721 			       DELETION_APPROVAL_DATE,
4722 			       RISK_REV_ID)
4723 			 VALUES ( amw_risk_associations_s.nextval,
4724 			         risk_rec.risk_id,
4725 			         p_certification_id,
4726 			         risk_rec.PK1,
4727 			         risk_rec.PK2,
4728 			         m_opinion_log_id,
4729 			         FND_GLOBAL.USER_ID,
4730 			       	 SYSDATE,
4731 			         SYSDATE,
4732 			         FND_GLOBAL.USER_ID,
4733 			         FND_GLOBAL.USER_ID,
4734 			         1,
4735 			         'PROCESS_FINCERT',
4736 			         risk_rec.ASSOCIATION_CREATION_DATE,
4737 			         risk_rec.APPROVAL_DATE,
4738 				 risk_rec.DELETION_DATE,
4739 				 risk_rec.DELETION_APPROVAL_DATE,
4740 				 risk_rec.RISK_REV_ID);
4741 
4742 		END LOOP;
4743 
4744 	   if(p_commit <> FND_API.g_false)
4745            then commit;
4746        end if;
4747     /**05.02.2006 npanandi: bug 5201579 fix --- need to have an update DML here**/
4748 	else /**this means that l_count is not zero, so update**/
4749 	   FOR risk_rec IN c_finrisks LOOP
4750 	   exit when c_finrisks%notfound;
4751 
4752 	      m_opinion_log_id := null;
4753 		  OPEN last_evaluation(risk_rec.risk_id, risk_rec.pk1, risk_rec.pk2);
4754 		  FETCH last_evaluation INTO m_opinion_log_id;
4755 		  CLOSE last_evaluation;
4756 
4757 		  update AMW_RISK_ASSOCIATIONS /**need only to update the opinionLogId here**/
4758  		     set PK4 = m_opinion_log_id,
4759 			     LAST_UPDATE_DATE = sysdate,
4760 			     LAST_UPDATED_BY = fnd_global.USER_ID,
4761 			     LAST_UPDATE_LOGIN = fnd_global.LOGIN_ID,
4762 			     OBJECT_VERSION_NUMBER = OBJECT_VERSION_NUMBER+1
4763 		   where object_type = 'PROCESS_FINCERT'
4764 		     and risk_id = risk_rec.risk_id
4765 			 and PK2 = risk_rec.PK1
4766 			 and PK3 = risk_rec.PK2
4767 			 and pk1 in (select certification_id from amw_certification_b
4768 			              where certification_status in ('ACTIVE','DRAFT'));
4769 		END LOOP;
4770 	    if(p_commit <> FND_API.g_false)
4771            then commit;
4772         end if;
4773 	/**05.02.2006 npanandi: bug 5201579 fix ends here**/
4774 	    	END IF;
4775 x_return_status := FND_API.G_RET_STS_SUCCESS;
4776 
4777 
4778 EXCEPTION
4779      WHEN NO_DATA_FOUND THEN
4780      IF c_finrisks%ISOPEN THEN
4781       	close c_finrisks;
4782          END IF;
4783      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
4784      x_return_status := FND_API.G_RET_STS_ERROR;
4785      x_msg_count := 1;
4786      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
4787       WHEN OTHERS THEN
4788       IF c_finrisks%ISOPEN THEN
4789       	close c_finrisks;
4790          END IF;
4791        ROLLBACK TO Populate_Fin_Risk_Ass_Sum;
4792       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
4793       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
4794         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4795         x_msg_count := 1;
4796         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
4797                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
4798                 FND_MSG_PUB.Count_And_Get(
4799                 p_encoded =>  FND_API.G_FALSE,
4800                 p_count   =>  x_msg_count,
4801                 p_data    =>  x_msg_data);
4802                 RETURN;
4803 
4804 END Populate_Fin_Risk_Ass_Sum;
4805 
4806 -------------populate all of financial certification which has one or more control associations
4807 /****************loop in master procedure *******************
4808 PROCEDURE Populate_All_Fin_Ctrl_Ass_Sum(
4809 p_api_version_number        IN   NUMBER   := 1.0,
4810 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
4811 p_commit                    IN   VARCHAR2 := FND_API.g_false,
4812 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
4813 p_certification_id  IN       NUMBER,
4814 x_return_status             OUT  nocopy VARCHAR2,
4815 x_msg_count                 OUT  nocopy NUMBER,
4816 x_msg_data                  OUT  nocopy VARCHAR2
4817 )IS
4818  CURSOR c_cert IS
4819         SELECT 	cert.certification_id, period.start_date,period.end_date
4820         FROM 	AMW_CERTIFICATION_B cert,
4821 		AMW_GL_PERIODS_V period
4822         WHERE 	cert.certification_period_name = period.period_name
4823         AND 	cert.certification_period_set_name = period.period_set_name
4824         AND     cert.object_type='FIN_STMT'
4825         AND     cert.certification_status in ('ACTIVE','DRAFT');
4826 
4827 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_All_Fin_Ctrl_Ass_Sum';
4828 l_api_version_number CONSTANT NUMBER       := 1.0;
4829 
4830 l_return_status VARCHAR2(32767);
4831 l_msg_count NUMBER;
4832 l_msg_data VARCHAR2(32767);
4833 
4834 BEGIN
4835 
4836 SAVEPOINT Populate_All_Fin_Ctrl_Ass_Sum;
4837 
4838  -- Standard call to check for call compatibility.
4839         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
4840                                              p_api_version_number,
4841                                              l_api_name,
4842                                              G_PKG_NAME)
4843         THEN
4844                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4845         END IF;
4846 
4847         -- Initialize message list if p_init_msg_list is set to TRUE.
4848         IF FND_API.to_Boolean( p_init_msg_list )
4849         THEN
4850                 FND_MSG_PUB.initialize;
4851         END IF;
4852 
4853         -- Initialize API return status to SUCCESS
4854         x_return_status := FND_API.G_RET_STS_SUCCESS;
4855 
4856    IF p_certification_id  IS NOT NULL THEN
4857    Populate_Fin_Ctrl_Ass_Sum
4858      (	p_certification_id => p_certification_id,
4859 	x_return_status   => l_return_status,
4860 	x_msg_count  => l_msg_count,
4861 	x_msg_data    => l_msg_data);
4862    ELSE
4863           FOR cert_rec IN c_cert LOOP
4864         	exit when c_cert%notfound;
4865         	 Populate_Fin_Ctrl_Ass_Sum
4866         	 (
4867         	 p_certification_id => p_certification_id,
4868 	x_return_status   => l_return_status,
4869 	x_msg_count  => l_msg_count,
4870 	x_msg_data    => l_msg_data
4871 	);
4872         END LOOP;
4873    END IF;
4874 
4875    x_return_status := FND_API.G_RET_STS_SUCCESS;
4876 
4877    EXCEPTION
4878      WHEN NO_DATA_FOUND THEN
4879      IF c_cert%ISOPEN THEN
4880       	close c_cert;
4881       END IF;
4882      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
4883      x_return_status := FND_API.G_RET_STS_ERROR;
4884      x_msg_count := 1;
4885      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
4886       WHEN OTHERS THEN
4887       IF c_cert%ISOPEN THEN
4888       	close c_cert;
4889       END IF;
4890            ROLLBACK TO Populate_All_Fin_Ctrl_Ass_Sum;
4891       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
4892       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
4893         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4894         x_msg_count := 1;
4895         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
4896                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
4897                 FND_MSG_PUB.Count_And_Get(
4898                 p_encoded =>  FND_API.G_FALSE,
4899                 p_count   =>  x_msg_count,
4900                 p_data    =>  x_msg_data);
4901                 RETURN;
4902 
4903 
4904  END Populate_All_Fin_Ctrl_Ass_Sum;
4905 
4906  ****************************************/
4907 
4908 -------------populate control association which related to financial certification ----
4909 PROCEDURE Populate_Fin_Ctrl_Ass_Sum(
4910 p_api_version_number        IN   NUMBER   := 1.0,
4911 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
4912 p_commit                    IN   VARCHAR2 := FND_API.g_false,
4913 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
4914 p_certification_id  IN       NUMBER,
4915 x_return_status             OUT  nocopy VARCHAR2,
4916 x_msg_count                 OUT  nocopy NUMBER,
4917 x_msg_data                  OUT  nocopy VARCHAR2
4918 ) IS
4919 CURSOR c_fincontrols IS
4920 
4921 SELECT
4922 	controls.control_id,
4923 	controls.PK1,
4924 	controls.PK2,
4925 	controls.PK3,
4926 	controls.ASSOCIATION_CREATION_DATE,
4927 	controls.APPROVAL_DATE,
4928 	controls.DELETION_DATE,
4929 	controls.DELETION_APPROVAL_DATE,
4930 	control.CONTROL_REV_ID
4931 FROM
4932 	AMW_RISK_ASSOCIATIONS risks,
4933 	AMW_CONTROL_ASSOCIATIONS controls,
4934 	AMW_CONTROLS_B control
4935 WHERE
4936 	controls.object_type='RISK_ORG'
4937 	and control.CURR_APPROVED_FLAG = 'Y'
4938 	and control.control_id = controls.control_id
4939 	and risks.PK1 = p_certification_id
4940 	and risks.PK2 = controls.PK1
4941 	and risks.PK3 = controls.PK2
4942 	and controls.PK3 = risks.risk_id
4943 	and risks.object_type = 'PROCESS_FINCERT'
4944 	and controls.approval_date is not null
4945 	and controls.approval_date <= sysdate
4946     	and controls.deletion_approval_date is null
4947 
4948 UNION ALL
4949 SELECT
4950 	controls.control_id,
4951 	controls.PK1,
4952 	controls.PK2,
4953 	controls.PK3,
4954 	controls.ASSOCIATION_CREATION_DATE,
4955 	controls.APPROVAL_DATE,
4956 	controls.DELETION_DATE,
4957 	controls.DELETION_APPROVAL_DATE,
4958 	control.CONTROL_REV_ID
4959 FROM
4960 	AMW_RISK_ASSOCIATIONS risks,
4961 	AMW_CONTROL_ASSOCIATIONS controls,
4962 	AMW_CONTROLS_B control
4963 WHERE
4964 	controls.object_type='ENTITY_CONTROL'
4965 	and control.CURR_APPROVED_FLAG = 'Y'
4966 	and control.control_id = controls.control_id
4967 	and risks.PK1 = p_certification_id
4968 	and risks.PK2 = controls.PK1
4969 	--and risks.PK3 IS NULL
4970 	--and controls.PK3 = risks.risk_id -- controls.pk3 is null
4971 	and risks.object_type = 'PROCESS_FINCERT'
4972 	and controls.approval_date is not null
4973 	and controls.approval_date <= sysdate
4974     	and controls.deletion_approval_date is null;
4975 
4976 
4977 
4978 --in control association table, if type = 'RISK_FINCERT', pk1=certification_id, pk2=organization_id, pk3=process_id, pk4=risk_id, pk5=opinion_log_id
4979 	CURSOR last_evaluation(l_organization_id number, l_control_id number)  IS
4980         SELECT 	distinct aov.opinion_log_id
4981 	FROM 	AMW_OPINION_LOG_MV aov
4982        	WHERE 	aov.object_name = 'AMW_ORG_CONTROL'
4983         AND 	aov.opinion_type_code = 'EVALUATION'
4984         AND 	aov.pk3_value = l_organization_id
4985         AND	aov.pk1_value = l_control_id
4986         --fix bug 5724066
4987 	AND aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
4988         AND 	aov.authored_date = (select max(aov2.authored_date)
4989                        	             from AMW_OPINIONS aov2
4990                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
4991                                      and aov2.pk3_value = aov.pk3_value
4992                                      and aov2.pk1_value = aov.pk1_value);
4993 
4994 	l_count NUMBER;
4995 	m_opinion_log_id NUMBER;
4996 
4997 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_Ctrl_Ass_Sum';
4998 l_api_version_number CONSTANT NUMBER       := 1.0;
4999 
5000 l_return_status VARCHAR2(32767);
5001 l_msg_count NUMBER;
5002 l_msg_data VARCHAR2(32767);
5003 
5004 BEGIN
5005 
5006 	SAVEPOINT Populate_Fin_Ctrl_Ass_Sum;
5007 
5008  -- Standard call to check for call compatibility.
5009         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
5010                                              p_api_version_number,
5011                                              l_api_name,
5012                                              G_PKG_NAME)
5013         THEN
5014                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5015         END IF;
5016 
5017         -- Initialize message list if p_init_msg_list is set to TRUE.
5018         IF FND_API.to_Boolean( p_init_msg_list )
5019         THEN
5020                 FND_MSG_PUB.initialize;
5021         END IF;
5022 
5023         -- Initialize API return status to SUCCESS
5024         x_return_status := FND_API.G_RET_STS_SUCCESS;
5025 
5026 
5027 	SELECT COUNT(1) INTO l_count FROM AMW_CONTROL_ASSOCIATIONS
5028 	WHERE OBJECT_TYPE = 'RISK_FINCERT'
5029 	and PK1 = p_certification_id;
5030 
5031 	IF (l_count = 0) THEN
5032 	FOR control_rec IN c_fincontrols LOOP
5033 	exit when c_fincontrols%notfound;
5034 
5035 	m_opinion_log_id := null;
5036 	OPEN last_evaluation(control_rec.pk1, control_rec.control_id);
5037 	FETCH last_evaluation INTO m_opinion_log_id;
5038 	CLOSE last_evaluation;
5039 
5040 		INSERT INTO AMW_CONTROL_ASSOCIATIONS(
5041  			       CONTROL_ASSOCIATION_ID,
5042 			       CONTROL_ID,
5043 			       PK1,
5044 			       PK2,
5045 			       PK3,
5046 			       PK4,
5047 			       PK5,
5048 			       CREATED_BY,
5049 			       CREATION_DATE,
5050 			       LAST_UPDATE_DATE,
5051 			       LAST_UPDATED_BY,
5052 			       LAST_UPDATE_LOGIN,
5053 			       OBJECT_VERSION_NUMBER,
5054 			       OBJECT_TYPE,
5055 			       ASSOCIATION_CREATION_DATE,
5056 			       APPROVAL_DATE,
5057 			       DELETION_DATE,
5058 			       DELETION_APPROVAL_DATE,
5059 			       CONTROL_REV_ID)
5060 			 VALUES (AMW_CONTROL_ASSOCIATIONS_S.nextval,
5061 			         control_rec.control_id,
5062 			         p_certification_id,
5063 			         control_rec.PK1,
5064 			         control_rec.PK2,
5065 			         control_rec.PK3,
5066 			         m_opinion_log_id,
5067 			         FND_GLOBAL.USER_ID,
5068 			       	 SYSDATE,
5069 			         SYSDATE,
5070 			         FND_GLOBAL.USER_ID,
5071 			         FND_GLOBAL.USER_ID,
5072 			         1,
5073 			         'RISK_FINCERT',
5074 			         control_rec.ASSOCIATION_CREATION_DATE,
5075 	 		         control_rec.APPROVAL_DATE,
5076 			         control_rec.DELETION_DATE,
5077 			        control_rec.DELETION_APPROVAL_DATE,
5078 			        control_rec.CONTROL_REV_ID);
5079 
5080 
5081 		END LOOP;
5082 
5083 		ELSE
5084 	FOR control_rec IN c_fincontrols LOOP
5085 	exit when c_fincontrols%notfound;
5086 
5087 	m_opinion_log_id := null;
5088 	OPEN last_evaluation(control_rec.pk1, control_rec.control_id);
5089 	FETCH last_evaluation INTO m_opinion_log_id;
5090 	CLOSE last_evaluation;
5091 
5092   UPDATE AMW_CONTROL_ASSOCIATIONS SET
5093    LAST_UPDATE_DATE = sysdate,
5094    last_updated_by = fnd_global.user_id,
5095    last_update_login = fnd_global.conc_login_id,
5096    pk5 = m_opinion_log_id
5097    WHERE OBJECT_TYPE = 'RISK_FINCERT'
5098    AND  control_id = control_rec.control_id
5099    AND   pk2 = control_rec.pk1
5100    AND   pk1  IN (SELECT CERTIFICATION_ID FROM  AMW_CERTIFICATION_B
5101    WHERE CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT'));
5102 
5103 END LOOP;
5104 
5105 
5106 if(p_commit <> FND_API.g_false)
5107 then commit;
5108 end if;
5109 	END IF;
5110 	   x_return_status := FND_API.G_RET_STS_SUCCESS;
5111 
5112 EXCEPTION
5113      WHEN NO_DATA_FOUND THEN
5114      IF c_fincontrols%ISOPEN THEN
5115       	close c_fincontrols;
5116       END IF;
5117      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
5118      x_return_status := FND_API.G_RET_STS_ERROR;
5119      x_msg_count := 1;
5120      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
5121       WHEN OTHERS THEN
5122       IF c_fincontrols%ISOPEN THEN
5123       	close c_fincontrols;
5124       END IF;
5125        ROLLBACK TO Populate_Fin_Ctrl_Ass_Sum;
5126       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
5127       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
5128         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5129         x_msg_count := 1;
5130         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
5131                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
5132                 FND_MSG_PUB.Count_And_Get(
5133                 p_encoded =>  FND_API.G_FALSE,
5134                 p_count   =>  x_msg_count,
5135                 p_data    =>  x_msg_data);
5136                 RETURN;
5137 
5138 END Populate_Fin_Ctrl_Ass_Sum;
5139 
5140 -------------populate all of financial certification that has one or more control associations  ----
5141 /********************delete becuase the loop is put in to master procedure
5142 PROCEDURE Populate_All_Fin_AP_Ass_Sum(
5143 p_api_version_number        IN   NUMBER   := 1.0,
5144 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
5145 p_commit                    IN   VARCHAR2 := FND_API.g_false,
5146 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
5147 p_certification_id  IN       NUMBER,
5148 x_return_status             OUT  nocopy VARCHAR2,
5149 x_msg_count                 OUT  nocopy NUMBER,
5150 x_msg_data                  OUT  nocopy VARCHAR2
5151 ) IS
5152  CURSOR c_cert IS
5153         SELECT 	cert.certification_id, period.start_date,period.end_date
5154         FROM 	AMW_CERTIFICATION_B cert,
5155 		AMW_GL_PERIODS_V period
5156         WHERE 	cert.certification_period_name = period.period_name
5157         AND 	cert.certification_period_set_name = period.period_set_name
5158         AND     cert.object_type='FIN_STMT'
5159         AND     cert.certification_status in ('ACTIVE','DRAFT');
5160 
5161 
5162 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_All_Fin_AP_Ass_Sum';
5163 l_api_version_number CONSTANT NUMBER       := 1.0;
5164 
5165 l_return_status VARCHAR2(32767);
5166 l_msg_count NUMBER;
5167 l_msg_data VARCHAR2(32767);
5168 l_certification_id NUMBER;
5169 
5170 BEGIN
5171 
5172 SAVEPOINT Populate_All_Fin_AP_Ass_Sum;
5173 
5174 l_certification_id := p_certification_id;
5175 
5176  -- Standard call to check for call compatibility.
5177         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
5178                                              p_api_version_number,
5179                                              l_api_name,
5180                                              G_PKG_NAME)
5181         THEN
5182                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5183         END IF;
5184 
5185         -- Initialize message list if p_init_msg_list is set to TRUE.
5186         IF FND_API.to_Boolean( p_init_msg_list )
5187         THEN
5188                 FND_MSG_PUB.initialize;
5189         END IF;
5190 
5191         -- Initialize API return status to SUCCESS
5192         x_return_status := FND_API.G_RET_STS_SUCCESS;
5193 
5194  IF p_certification_id IS NOT NULL THEN
5195    Populate_Fin_AP_Ass_Sum
5196    (	p_certification_id => l_certification_id,
5197 	x_return_status   => l_return_status,
5198 	x_msg_count  => l_msg_count,
5199 	x_msg_data    => l_msg_data);
5200    ELSE
5201           FOR cert_rec IN c_cert LOOP
5202         	exit when c_cert%notfound;
5203         	 Populate_Fin_AP_Ass_Sum
5204         	   (	p_certification_id => l_certification_id,
5205 		x_return_status   => l_return_status,
5206 		x_msg_count  => l_msg_count,
5207 		x_msg_data    => l_msg_data);
5208         END LOOP;
5209    END IF;
5210 
5211    x_return_status := FND_API.G_RET_STS_SUCCESS;
5212 
5213    EXCEPTION
5214      WHEN NO_DATA_FOUND THEN
5215      IF c_cert%ISOPEN THEN
5216       	close c_cert;
5217       END IF;
5218      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
5219      x_return_status := FND_API.G_RET_STS_ERROR;
5220      x_msg_count := 1;
5221      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
5222       WHEN OTHERS THEN
5223       IF c_cert%ISOPEN THEN
5224       	close c_cert;
5225       END IF;
5226        ROLLBACK TO Populate_All_Fin_AP_Ass_Sum;
5227       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
5228       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
5229         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5230         x_msg_count := 1;
5231         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
5232                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
5233                 FND_MSG_PUB.Count_And_Get(
5234                 p_encoded =>  FND_API.G_FALSE,
5235                 p_count   =>  x_msg_count,
5236                 p_data    =>  x_msg_data);
5237                 RETURN;
5238 
5239 END Populate_All_Fin_AP_Ass_Sum;
5240 **************************************************/
5241 
5242 
5243 -------------populate control association which related to financial certification ----
5244 PROCEDURE Populate_Fin_AP_Ass_Sum(
5245 p_api_version_number        IN   NUMBER   := 1.0,
5246 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
5247 p_commit                    IN   VARCHAR2 := FND_API.g_false,
5248 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
5249 p_certification_id  IN       NUMBER,
5250 x_return_status             OUT  nocopy VARCHAR2,
5251 x_msg_count                 OUT  nocopy NUMBER,
5252 x_msg_data                  OUT  nocopy VARCHAR2
5253 ) IS
5254 
5255 CURSOR c_finap IS
5256 SELECT
5257 	ap.AUDIT_PROCEDURE_ID,
5258 	ap.PK1,
5259 	ap.PK2,
5260 	ap.PK3,
5261 	ap.ASSOCIATION_CREATION_DATE,
5262 	ap.APPROVAL_DATE,
5263 	ap.DELETION_DATE,
5264 	ap.DELETION_APPROVAL_DATE,
5265 	apb.AUDIT_PROCEDURE_REV_ID
5266 FROM
5267 	AMW_AP_ASSOCIATIONS ap,
5268 	AMW_CONTROL_ASSOCIATIONS controls,
5269 	AMW_AUDIT_PROCEDURES_B apb
5270 WHERE
5271 	ap.object_type='CTRL_ORG'
5272 	and apb.CURR_APPROVED_FLAG = 'Y'
5273 	and ap.audit_procedure_id = apb.audit_procedure_id
5274 	and controls.PK1 = p_certification_id /*certificationId*/
5275 	and controls.PK2 = ap.PK1 /*organizationId*/
5276 	and controls.PK3 = ap.PK2 /*processId*/
5277 	and controls.control_id = ap.PK3 /*controlId*/
5278 	and controls.object_type = 'RISK_FINCERT'
5279 	and ap.association_creation_date is not null
5280 	and ap.deletion_date is null
5281 UNION ALL
5282 SELECT
5283 	ap.AUDIT_PROCEDURE_ID,
5284 	ap.PK1,
5285 	ap.PK2,
5286 	ap.PK3,
5287 	ap.ASSOCIATION_CREATION_DATE,
5288 	ap.APPROVAL_DATE,
5289 	ap.DELETION_DATE,
5290 	ap.DELETION_APPROVAL_DATE,
5291 	apb.AUDIT_PROCEDURE_REV_ID
5292 FROM
5293 	AMW_AP_ASSOCIATIONS ap,
5294 	AMW_CONTROL_ASSOCIATIONS controls,
5295 	AMW_AUDIT_PROCEDURES_B apb
5296 WHERE
5297 	ap.object_type='ENTITY_CTRL_AP'
5298 	and apb.CURR_APPROVED_FLAG = 'Y'
5299 	and ap.audit_procedure_id = apb.audit_procedure_id
5300 	and controls.PK1 = p_certification_id
5301 	and controls.PK2 = ap.PK1
5302 	--and controls.PK3 = ap.PK2
5303 	and controls.PK3 is null /**it is null here because the corresponding Ctrl is an EntityControl**/
5304 	and controls.control_id = ap.PK3
5305 	and controls.object_type = 'RISK_FINCERT'
5306 	and ap.association_creation_date is not null
5307 	and ap.deletion_date is null;
5308 
5309 --need check opinion framework doc
5310 --in ap association table, if type = 'CTRL_FINCERT', pk1=certification_id, pk2=organization_id, pk3=process_id, pk4=control_id, pk5=opinion_id
5311 CURSOR last_evaluation(l_audit_procedure_id number, l_organization_id number, l_control_id number)  IS
5312 SELECT 	distinct aov.opinion_id
5313 FROM 	AMW_OPINION_MV aov
5314 WHERE
5315                 aov.object_name = 'AMW_ORG_AP_CONTROL'
5316         AND 	aov.opinion_type_code = 'EVALUATION'
5317         AND 	aov.opinion_component_code = 'OVERALL'
5318         AND 	aov.pk3_value = l_organization_id
5319         AND 	aov.pk4_value = l_audit_procedure_id
5320         AND	aov.pk1_value = l_control_id
5321         --fix bug 5724066
5322 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
5323         AND 	aov.authored_date = (select max(aov2.authored_date)
5324                        	             from AMW_OPINIONS aov2
5325                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
5326                                      and aov2.pk3_value = aov.pk3_value
5327                                      and aov2.pk1_value = aov.pk1_value
5328                                      and aov2.pk4_value = aov.pk4_value);
5329 
5330 
5331 	l_count NUMBER;
5332 	m_opinion_id NUMBER;
5333 
5334 
5335 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_AP_Ass_Sum';
5336 l_api_version_number CONSTANT NUMBER  := 1.0;
5337 
5338 l_return_status VARCHAR2(32767);
5339 l_msg_count NUMBER;
5340 l_msg_data VARCHAR2(32767);
5341 
5342 BEGIN
5343 
5344    SAVEPOINT Populate_Fin_AP_Ass_Sum;
5345 
5346  -- Standard call to check for call compatibility.
5347 
5348         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
5349                                              p_api_version_number,
5350                                              l_api_name,
5351                                              G_PKG_NAME)
5352         THEN
5353                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5354         END IF;
5355 
5356         -- Initialize message list if p_init_msg_list is set to TRUE.
5357         IF FND_API.to_Boolean( p_init_msg_list )
5358         THEN
5359                 FND_MSG_PUB.initialize;
5360         END IF;
5361 
5362         -- Initialize API return status to SUCCESS
5363         x_return_status := FND_API.G_RET_STS_SUCCESS;
5364 
5365 
5366 
5367 	SELECT COUNT(1) INTO l_count FROM AMW_AP_ASSOCIATIONS
5368 	WHERE OBJECT_TYPE = 'CTRL_FINCERT'
5369 	and PK1 = p_certification_id;
5370 
5371 	IF (l_count = 0) THEN
5372 	   FOR ap_rec IN c_finap LOOP
5373 	   exit when c_finap%notfound;
5374 
5375 	      m_opinion_id := null;
5376 	      OPEN last_evaluation(ap_rec.audit_procedure_id, ap_rec.pk1, ap_rec.pk3);
5377 	         FETCH last_evaluation INTO m_opinion_id;
5378 	      CLOSE last_evaluation;
5379 
5380 
5381 		  INSERT INTO AMW_AP_ASSOCIATIONS(
5382 			       AP_ASSOCIATION_ID,
5383  			       AUDIT_PROCEDURE_ID,
5384 			       PK1, /*certificationId*/
5385 			       PK2, /*organizationId*/
5386 			       PK3, /*processId*/
5387 			       PK4, /*controlId*/
5388 			       PK5, /*opinionLogId*/
5389 			       CREATED_BY,
5390 			       CREATION_DATE,
5391 			       LAST_UPDATE_DATE,
5392 			       LAST_UPDATED_BY,
5393 			       LAST_UPDATE_LOGIN,
5394 			       OBJECT_VERSION_NUMBER,
5395 			       OBJECT_TYPE,
5396 			       ASSOCIATION_CREATION_DATE,
5397 			       APPROVAL_DATE,
5398 			       DELETION_DATE,
5399 			       DELETION_APPROVAL_DATE,
5400 			       AUDIT_PROCEDURE_REV_ID)
5401 			 VALUES (AMW_AP_ASSOCIATIONS_S.nextval,
5402 			         ap_rec.audit_procedure_id,
5403 			         p_certification_id, /*certificationId*/
5404 			         ap_rec.PK1, /*organizationId*/
5405 			         ap_rec.PK2, /*processId*/
5406 			         ap_rec.PK3, /*controlId*/
5407 			         m_opinion_id, /*opinionLogId*/
5408 			         FND_GLOBAL.USER_ID,
5409 			         SYSDATE,
5410 			         SYSDATE,
5411 			         FND_GLOBAL.USER_ID,
5412 			         FND_GLOBAL.USER_ID,
5413 			         1,
5414 			         'CTRL_FINCERT',
5415 			         ap_rec.ASSOCIATION_CREATION_DATE,
5416 	 		         ap_rec.APPROVAL_DATE,
5417 			         ap_rec.DELETION_DATE,
5418 			         ap_rec.DELETION_APPROVAL_DATE,
5419 			         ap_rec.AUDIT_PROCEDURE_REV_ID);
5420        END LOOP;
5421        if(p_commit <> FND_API.g_false)
5422           then commit;
5423        end if;
5424 	/**05.02.2006 npanandi: bug 5201579 fix here for updating, if count=0**/
5425 	else
5426 	   FOR ap_rec IN c_finap LOOP
5427 	   exit when c_finap%notfound;
5428 
5429 	      m_opinion_id := null;
5430 	      OPEN last_evaluation(ap_rec.audit_procedure_id, ap_rec.pk1, ap_rec.pk3);
5431 	         FETCH last_evaluation INTO m_opinion_id;
5432 	      CLOSE last_evaluation;
5433 
5434 
5435 		  update AMW_AP_ASSOCIATIONS
5436 			 set PK5 = m_opinion_id
5437 			    ,LAST_UPDATE_DATE = SYSDATE
5438 			    ,LAST_UPDATED_BY = FND_GLOBAL.USER_ID
5439 			    ,LAST_UPDATE_LOGIN = FND_GLOBAL.LOGIN_ID
5440 			    ,OBJECT_VERSION_NUMBER = OBJECT_VERSION_NUMBER+1
5441 		   where object_type = 'CTRL_FINCERT'
5442 		     and AUDIT_PROCEDURE_ID = ap_rec.audit_procedure_id
5443 			 AND PK2 = AP_REC.PK1 /**organizationId**/
5444 			 and pk4 = ap_rec.pk3 /**controlId**/
5445 			 AND PK1 IN (SELECT CERTIFICATION_ID
5446 			               FROM AMW_CERTIFICATION_B
5447                           WHERE CERTIFICATION_STATUS IN ('ACTIVE','DRAFT'));
5448        END LOOP;
5449        if(p_commit <> FND_API.g_false)
5450           then commit;
5451        end if;
5452 	  /**05.02.2006 npanandi: bug 5201579 fix ends here**/
5453 	END IF;
5454 	x_return_status := FND_API.G_RET_STS_SUCCESS;
5455 
5456 EXCEPTION
5457      WHEN NO_DATA_FOUND THEN
5458      IF c_finap%ISOPEN THEN
5459       	close c_finap;
5460       END IF;
5461      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
5462      x_return_status := FND_API.G_RET_STS_ERROR;
5463      x_msg_count := 1;
5464      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
5465       WHEN OTHERS THEN
5466       IF c_finap%ISOPEN THEN
5467       	close c_finap;
5468       END IF;
5469       ROLLBACK TO Populate_Fin_AP_Ass_Sum;
5470       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
5471       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
5472         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5473         x_msg_count := 1;
5474         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
5475                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
5476                 FND_MSG_PUB.Count_And_Get(
5477                 p_encoded =>  FND_API.G_FALSE,
5478                 p_count   =>  x_msg_count,
5479                 p_data    =>  x_msg_data);
5480                 RETURN;
5481 
5482 END Populate_Fin_AP_Ass_Sum;
5483 
5484 
5485 PROCEDURE Populate_All_Fin_Org_Eval_Sum(
5486 p_api_version_number        IN   NUMBER   := 1.0,
5487 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
5488 p_commit                    IN   VARCHAR2 := FND_API.g_false,
5489 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
5490 p_certification_id  IN       NUMBER,
5491 x_return_status             OUT  nocopy VARCHAR2,
5492 x_msg_count                 OUT  nocopy NUMBER,
5493 x_msg_data                  OUT  nocopy VARCHAR2
5494 ) IS
5495 
5496     CURSOR c_cert IS
5497         SELECT 	cert.certification_id, period.start_date, period.end_date
5498         FROM 	AMW_CERTIFICATION_B cert,
5499 		AMW_GL_PERIODS_V period
5500         WHERE 	cert.certification_period_name = period.period_name
5501         AND 	cert.certification_period_set_name = period.period_set_name
5502         AND     cert.object_type='FIN_STMT'
5503         AND     cert.certification_status in ('ACTIVE','DRAFT');
5504 
5505 
5506    -- select all the processes based on the certification_id
5507     CURSOR c_org  IS
5508     	SELECT distinct organization_id
5509     	FROM AMW_FIN_PROCESS_EVAL_SUM
5510     	WHERE FIN_CERTIFICATION_ID = p_certification_id;
5511 
5512 l_subsidiary_tbl amw_scope_pvt.sub_tbl_type;
5513 l_lob_tbl amw_scope_pvt.lob_tbl_type;
5514 l_org_tbl amw_scope_pvt.org_tbl_type;
5515 l_proc_tbl amw_scope_pvt.process_tbl_type;
5516 
5517 l_sub_vs AMW_AUDIT_UNITS_V.subsidiary_valueset%TYPE;
5518  l_lob_vs AMW_AUDIT_UNITS_V.subsidiary_valueset%TYPE;
5519 
5520  l_position NUMBER;
5521 
5522     l_start_date DATE;
5523     l_end_date DATE;
5524 
5525 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_All_Fin_Org_Eval_Sum';
5526 l_api_version_number CONSTANT NUMBER       := 1.0;
5527 
5528 l_return_status VARCHAR2(32767);
5529 l_msg_count NUMBER;
5530 l_msg_data VARCHAR2(32767);
5531 
5532 BEGIN
5533 
5534 SAVEPOINT Populate_All_Fin_Org_Eval_Sum;
5535 
5536  -- Standard call to check for call compatibility.
5537         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
5538                                              p_api_version_number,
5539                                              l_api_name,
5540                                              G_PKG_NAME)
5541         THEN
5542                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5543         END IF;
5544 
5545         -- Initialize message list if p_init_msg_list is set to TRUE.
5546         IF FND_API.to_Boolean( p_init_msg_list )
5547         THEN
5548                 FND_MSG_PUB.initialize;
5549         END IF;
5550 
5551         -- Initialize API return status to SUCCESS
5552         x_return_status := FND_API.G_RET_STS_SUCCESS;
5553 
5554 	DELETE FROM AMW_ENTITY_HIERARCHIES
5555 	WHERE ENTITY_TYPE = 'FINSTMT_CERTIFICATION'
5556 	AND ENTITY_ID = p_certification_id;
5557 
5558 
5559         l_sub_vs := FND_PROFILE.value('AMW_SUBSIDIARY_AUDIT_UNIT');
5560         l_lob_vs := FND_PROFILE.value('AMW_LOB_AUDIT_UNITS');
5561 
5562          l_position := 1;
5563 
5564         -- DELETE_ROWS(p_certification_id, 'AMW_FIN_ORG_EVAL_SUM');
5565 
5566 	 FOR org_rec IN c_org LOOP
5567 	 exit when c_org%notfound;
5568 
5569           Populate_Fin_Org_Eval_Sum(
5570           p_certification_id => p_certification_id,
5571           p_start_date => null,
5572           p_end_date => null,
5573           p_organization_id => org_rec.organization_id,
5574           x_return_status  => l_return_status,
5575 	  x_msg_count => l_msg_count,
5576 	  x_msg_data  => l_msg_data
5577 	);
5578 
5579 
5580 	      /***05.25.2006 npanandi: bug 5142819 test -- added following
5581               begin, exception, end block to capture any errors locally
5582               and proceed from there, instead of erroring out the whole
5583               Concurrent Program***/
5584           begin
5585              SELECT company_code, lob_code
5586 		       into l_subsidiary_tbl(l_position).subsidiary_code, l_lob_tbl(l_position).lob_code
5587                FROM amw_audit_units_v
5588               WHERE organization_id = org_rec.organization_id;
5589 
5590              l_org_tbl(l_position).org_id := org_rec.organization_id;
5591              l_position := l_position + 1;
5592 		  exception
5593              when no_data_found then
5594                 null; /***do nothing, let the loop continue
5595                           this ~~~~MOST PROBABLY~~~~ has to do with the fact
5596                           that there was an error in
5597                           Populate_Fin_Org_Eval_Sum as well
5598                        ***/
5599           end;
5600 
5601 	END LOOP;
5602 
5603   IF(l_subsidiary_tbl.count <> 0 ) THEN
5604 	AMW_SCOPE_PVT.add_scope
5605 	(
5606                     p_entity_id       => p_certification_id,
5607                     p_entity_type     => 'FINSTMT_CERTIFICATION',
5608                     p_sub_vs       => l_sub_vs,
5609                     p_lob_vs     => l_lob_vs,
5610                     p_subsidiary_tbl    => l_subsidiary_tbl,
5611                     p_lob_tbl      =>l_lob_tbl ,
5612                     p_org_tbl   => l_org_tbl,
5613                     p_process_tbl          => l_proc_tbl,
5614                     x_return_status         => l_return_status,
5615                     x_msg_count     => l_msg_count,
5616                     x_msg_data    => l_msg_data
5617 	);
5618 END IF;
5619 
5620 x_return_status := FND_API.G_RET_STS_SUCCESS;
5621 
5622 EXCEPTION
5623      WHEN NO_DATA_FOUND THEN
5624      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
5625      x_return_status := FND_API.G_RET_STS_ERROR;
5626      x_msg_count := 1;
5627      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
5628       WHEN OTHERS THEN
5629        ROLLBACK TO Populate_All_Fin_Org_Eval_Sum;
5630       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
5631       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
5632         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5633         x_msg_count := 1;
5634         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
5635                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
5636                 FND_MSG_PUB.Count_And_Get(
5637                 p_encoded =>  FND_API.G_FALSE,
5638                 p_count   =>  x_msg_count,
5639                 p_data    =>  x_msg_data);
5640                 RETURN;
5641 
5642 END Populate_All_Fin_Org_Eval_Sum;
5643 
5644 
5645 
5646 PROCEDURE  Populate_Fin_Org_Eval_Sum(
5647 p_api_version_number        IN   NUMBER   := 1.0,
5648 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
5649 p_commit                    IN   VARCHAR2 := FND_API.g_false,
5650 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
5651 p_certification_id          IN      NUMBER,
5652 p_start_date                IN      DATE,
5653 p_end_date			IN      DATE,
5654 p_organization_id		IN 	NUMBER,
5655 x_return_status             OUT  nocopy VARCHAR2,
5656 x_msg_count                 OUT  nocopy NUMBER,
5657 x_msg_data                  OUT  nocopy VARCHAR2
5658 )
5659 IS
5660     CURSOR last_evaluation IS
5661         SELECT 	distinct aov.opinion_id
5662 	FROM 	AMW_OPINION_MV aov
5663        	WHERE 	aov.object_name = 'AMW_ORGANIZATION'
5664         AND 	aov.opinion_type_code = 'EVALUATION'
5665         AND 	aov.opinion_component_code = 'OVERALL'
5666         AND 	aov.pk1_value = p_organization_id
5667         --fix bug 5724066
5668 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
5669         AND 	aov.authored_date = (select max(aov2.authored_date)
5670                        	             from AMW_OPINIONS aov2
5671                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
5672                                      and aov2.pk1_value = aov.pk1_value);
5673 
5674    CURSOR last_evaluation_history(l_opinion_id number) IS
5675         SELECT 	max(aov.opinion_log_id)
5676 	FROM 	AMW_OPINIONS_LOG aov
5677        	WHERE 	aov.opinion_id = l_opinion_id;
5678 
5679     CURSOR last_certification IS
5680        SELECT aov.opinion_id
5681         FROM    AMW_OPINION_MV aov
5682         WHERE   aov.object_name = 'AMW_ORGANIZATION'
5683         AND     aov.opinion_type_code = 'CERTIFICATION'
5684         AND     aov.opinion_component_code = 'OVERALL'
5685        AND     aov.pk1_value = p_organization_id
5686         AND     aov.authored_date = (select max(aov2.authored_date)
5687                                      from AMW_OPINIONS aov2
5688                                      where aov2.object_opinion_type_id = aov.object_opinion_type_id
5689                                      and aov2.pk1_value = aov.pk1_value)
5690         AND     aov.pk2_value in (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
5691                                 where fin_stmt_cert_id = p_certification_id
5692                                  and end_date is null );
5693 
5694    CURSOR last_certification_history(l_opinion_id number) IS
5695    	SELECT max(opinion_log_id)
5696    	FROM 	AMW_OPINIONS_LOG aov
5697        	WHERE 	aov.opinion_id = l_opinion_id;
5698 
5699     CURSOR total_num_of_proc IS
5700 	SELECT COUNT(DISTINCT PROCESS_ID) FROM AMW_FIN_PROCESS_EVAL_SUM
5701 	WHERE FIN_CERTIFICATION_ID = p_certification_id AND ORGANIZATION_ID = p_organization_id;
5702 
5703 
5704 CURSOR pending_proc IS
5705     SELECT count(distinct proeval.process_id)
5706     FROM AMW_FIN_PROCESS_EVAL_SUM proeval
5707     WHERE proeval.fin_certification_id = p_certification_id
5708     AND	  proeval.organization_id = p_organization_id
5709     AND       proeval.cert_opinion_log_id is null;
5710 
5711 
5712     CURSOR proc_with_issue IS
5713     SELECT count(distinct proeval.process_id)
5714     FROM AMW_FIN_PROCESS_EVAL_SUM proeval,
5715     	 AMW_OPINION_MV aov
5716     WHERE proeval.fin_certification_id = p_certification_id
5717     AND	  proeval.organization_id = p_organization_id
5718     AND     aov.object_name = 'AMW_ORG_PROCESS'
5719         AND     aov.opinion_type_code = 'CERTIFICATION'
5720         AND 	aov.opinion_component_code = 'OVERALL'
5721         AND     aov.pk3_value = p_organization_id
5722 	AND     aov.pk2_value in (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
5723            			where fin_stmt_cert_id = p_certification_id
5724            			 and end_date is null )
5725         AND     aov.pk1_value = proeval.process_id
5726 	AND     aov.authored_date = (select max(aov2.authored_date)
5727                        	             from AMW_OPINIONS  aov2
5728                                	     where aov2.object_opinion_type_id
5729 					   = aov.object_opinion_type_id
5730                                      and aov2.pk3_value = aov.pk3_value
5731 				     AND aov2.pk2_value in
5732 					(select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
5733            				 where fin_stmt_cert_id = p_certification_id
5734            				 and end_date is null)
5735                                      and aov2.pk1_value = aov.pk1_value)
5736         AND     aov.OPINION_VALUE_CODE <> 'EFFECTIVE';
5737 
5738     CURSOR proc_without_issue IS
5739     SELECT count(distinct proeval.process_id)
5740     FROM AMW_FIN_PROCESS_EVAL_SUM proeval,
5741     	 AMW_OPINION_MV aov
5742     WHERE proeval.fin_certification_id = p_certification_id
5743     AND	  proeval.organization_id = p_organization_id
5744     AND     aov.object_name = 'AMW_ORG_PROCESS'
5745         AND     aov.opinion_type_code = 'CERTIFICATION'
5746         AND 	aov.opinion_component_code = 'OVERALL'
5747         AND     aov.pk3_value = p_organization_id
5748 	AND     aov.pk2_value in (select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
5749            			where fin_stmt_cert_id = p_certification_id
5750            			and end_date is null)
5751         AND     aov.pk1_value = proeval.process_id
5752 	AND     aov.authored_date = (select max(aov2.authored_date)
5753                        	             from AMW_OPINIONS  aov2
5754                                	     where aov2.object_opinion_type_id
5755 					   = aov.object_opinion_type_id
5756                                      and aov2.pk3_value = aov.pk3_value
5757 				     AND aov2.pk2_value in
5758 					(select proc_cert_Id from AMW_FIN_PROC_CERT_RELAN
5759            				 where fin_stmt_cert_id = p_certification_id
5760            				 and end_date is null)
5761                                      and aov2.pk1_value = aov.pk1_value)
5762         AND     aov.OPINION_VALUE_CODE = 'EFFECTIVE';
5763 
5764     CURSOR proc_with_ineff_ctrl IS
5765      SELECT count(distinct proeval.process_id)
5766      FROM AMW_FIN_PROCESS_EVAL_SUM proeval,
5767     	  AMW_OPINION_MV aov
5768     WHERE proeval.fin_certification_id = p_certification_id
5769     AND	  proeval.organization_id = p_organization_id
5770      AND     aov.object_name = 'AMW_ORG_PROCESS'
5771         AND     aov.opinion_type_code = 'EVALUATION'
5772         AND 	aov.opinion_component_code = 'OVERALL'
5773         AND     aov.pk3_value = p_organization_id
5774         AND     aov.pk1_value = proeval.process_id
5775         --fix bug 5724066
5776 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
5777         AND     aov.authored_date =
5778 		      (select max(aov2.authored_date)
5779 		       from   AMW_OPINIONS  aov2
5780 		       where aov2.object_opinion_type_id = aov.object_opinion_type_id
5781 			 and aov2.pk3_value = aov.pk3_value
5782                          and aov2.pk1_value = aov.pk1_value)
5783            AND aov.OPINION_VALUE_CODE <> 'EFFECTIVE';
5784 
5785   CURSOR verified_processes IS
5786    SELECT count(distinct proeval.process_id)
5787      FROM AMW_FIN_PROCESS_EVAL_SUM proeval,
5788     	  AMW_OPINION_MV aov
5789     WHERE proeval.fin_certification_id = p_certification_id
5790     AND	  proeval.organization_id = p_organization_id
5791      AND     aov.object_name = 'AMW_ORG_PROCESS'
5792         AND     aov.opinion_type_code = 'EVALUATION'
5793         AND     aov.opinion_component_code = 'OVERALL'
5794         AND     aov.pk3_value = p_organization_id
5795         AND     aov.pk1_value = proeval.process_id
5796         --fix bug 5724066
5797 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC');
5798 
5799 
5800 
5801     CURSOR unmitigated_risks IS
5802      SELECT 	count(distinct ara.risk_association_id)
5803      FROM 	AMW_RISK_ASSOCIATIONS ara,
5804                 AMW_OPINION_MV aov
5805     WHERE	ara.object_type = 'PROCESS_FINCERT'
5806     AND		ara.pk1= p_certification_id
5807     AND		ara.pk2= p_organization_id
5808     AND 	aov.object_name = 'AMW_ORG_PROCESS_RISK'
5809     AND 	aov.opinion_type_code = 'EVALUATION'
5810     AND 	aov.opinion_component_code = 'OVERALL'
5811     AND 	aov.pk3_value = ara.pk2
5812     AND 	aov.pk4_value = ara.pk3
5813     AND 	aov.pk1_value = ara.risk_id
5814     --fix bug 5724066
5815     AND         aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
5816     AND 	aov.authored_date = (select max(aov2.authored_date)
5817                                      from AMW_OPINIONS  aov2
5818                                      where aov2.object_opinion_type_id = aov.object_opinion_type_id
5819                                      and aov2.pk4_value = aov.pk4_value
5820                                      and aov2.pk3_value = aov.pk3_value
5821                                      and aov2.pk1_value = aov.pk1_value)
5822       	AND 	aov.OPINION_VALUE_CODE <> 'EFFECTIVE';
5823 
5824 
5825     CURSOR total_risks IS
5826         SELECT 	count(distinct ara.risk_association_id)
5827         FROM AMW_RISK_ASSOCIATIONS ara
5828         WHERE	ara.object_type = 'PROCESS_FINCERT'
5829     	AND		ara.pk1= p_certification_id
5830     	AND		ara.pk2= p_organization_id;
5831 
5832 CURSOR verified_risks IS
5833 SELECT 	count(distinct ara.risk_association_id)
5834      FROM 	AMW_RISK_ASSOCIATIONS ara,
5835                 	AMW_OPINION_MV aov
5836     WHERE	ara.object_type = 'PROCESS_FINCERT'
5837     AND		ara.pk1= p_certification_id
5838     AND		ara.pk2= p_organization_id
5839     AND 	aov.object_name = 'AMW_ORG_PROCESS_RISK'
5840     AND 	aov.opinion_type_code = 'EVALUATION'
5841     AND 	aov.opinion_component_code = 'OVERALL'
5842     AND 	aov.pk3_value = ara.pk2
5843     AND 	aov.pk4_value = ara.pk3
5844     AND 	aov.pk1_value = ara.risk_id
5845     --fix bug 5724066
5846     AND aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC');
5847 
5848     CURSOR ineffective_controls IS
5849     SELECT 	count(distinct aca.control_id)
5850     FROM	AMW_CONTROL_ASSOCIATIONS aca ,
5851                 AMW_OPINION_MV aov
5852     WHERE	aca.object_type = 'RISK_FINCERT'
5853     AND		aca.pk1 = p_certification_id
5854     AND		aca.pk2 = p_organization_id
5855     AND 	aov.object_name = 'AMW_ORG_CONTROL'
5856     AND 	aov.opinion_type_code = 'EVALUATION'
5857     AND 	aov.opinion_component_code = 'OVERALL'
5858     AND 	aov.pk3_value = p_organization_id
5859     AND 	aov.pk1_value = aca.control_id
5860     --fix bug 5724066
5861     AND         aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
5862     AND 	aov.authored_date = (select max(aov2.authored_date)
5863                                      from AMW_OPINIONS  aov2
5864                                      where aov2.object_opinion_type_id = aov.object_opinion_type_id
5865                                      and aov2.pk3_value = aov.pk3_value
5866                                      and aov2.pk1_value = aov.pk1_value)
5867     AND 	aov.OPINION_VALUE_CODE <> 'EFFECTIVE';
5868 
5869 
5870     CURSOR total_controls IS
5871         SELECT 	count(distinct aca.control_id)
5872         FROM AMW_CONTROL_ASSOCIATIONS aca
5873         WHERE	aca.object_type = 'RISK_FINCERT'
5874     AND		aca.pk1 = p_certification_id
5875     AND		aca.pk2 = p_organization_id;
5876 
5877     CURSOR verified_controls IS
5878     SELECT 	count(distinct aca.control_id)
5879     FROM	AMW_CONTROL_ASSOCIATIONS aca ,
5880                 AMW_OPINION_MV aov
5881     WHERE	aca.object_type = 'RISK_FINCERT'
5882     AND		aca.pk1 = p_certification_id
5883     AND		aca.pk2 = p_organization_id
5884     AND 	aov.object_name = 'AMW_ORG_CONTROL'
5885     AND 	aov.opinion_type_code = 'EVALUATION'
5886     AND 	aov.opinion_component_code = 'OVERALL'
5887     AND 	aov.pk3_value = p_organization_id
5888     AND 	aov.pk1_value = aca.control_id
5889     --fix bug 5724066
5890     AND aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC');
5891 
5892     CURSOR c_org IS
5893         SELECT  subsidiary_valueset, company_code, lob_valueset, lob_code
5894         FROM    amw_audit_units_v
5895         WHERE   organization_id = p_organization_id;
5896 
5897     l_eval_opinion_id		NUMBER;
5898     l_proc_pending_cert		NUMBER;
5899     l_total_num_of_procs	NUMBER;
5900     l_proc_with_issue		NUMBER;
5901     l_proc_without_issue	NUMBER;
5902     l_proc_certified		NUMBER;
5903     l_proc_with_ineff_ctrl	NUMBER;
5904     l_proc_verified		NUMBER;
5905     l_unmitigated_risks	NUMBER;
5906     l_total_risks		NUMBER;
5907     l_verified_risks		NUMBER;
5908     l_ineff_controls		NUMBER;
5909     l_total_controls		NUMBER;
5910     l_verified_controls		NUMBER;
5911     l_open_findings		NUMBER;
5912     l_sub_vs			VARCHAR2(150);
5913     l_lob_vs			VARCHAR2(150);
5914     l_sub_code			VARCHAR2(150);
5915     l_lob_code			VARCHAR2(150);
5916     l_eval_opinion_log_id	NUMBER;
5917     l_cert_opinion_id		NUMBER;
5918     l_cert_opinion_log_id	NUMBER;
5919 
5920 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_Org_Eval_Sum';
5921 l_api_version_number CONSTANT NUMBER       := 1.0;
5922 
5923 l_return_status VARCHAR2(32767);
5924 l_msg_count NUMBER;
5925 l_msg_data VARCHAR2(32767);
5926 
5927 BEGIN
5928 
5929 
5930 SAVEPOINT Populate_Fin_Org_Eval_Sum;
5931 
5932  -- Standard call to check for call compatibility.
5933         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
5934                                              p_api_version_number,
5935                                              l_api_name,
5936                                              G_PKG_NAME)
5937         THEN
5938                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5939         END IF;
5940 
5941         -- Initialize message list if p_init_msg_list is set to TRUE.
5942         IF FND_API.to_Boolean( p_init_msg_list )
5943         THEN
5944                 FND_MSG_PUB.initialize;
5945         END IF;
5946 
5947         -- Initialize API return status to SUCCESS
5948         x_return_status := FND_API.G_RET_STS_SUCCESS;
5949 
5950 
5951 
5952 
5953     --fnd_file.put_line (fnd_file.LOG, 'p_certification_id='||to_char(p_certification_id));
5954     --fnd_file.put_line(fnd_file.LOG, 'p_organization_id='||to_char(p_organization_id));
5955    -- fnd_file.put_line(fnd_file.LOG, 'before last_evaludation:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
5956 
5957     OPEN last_evaluation;
5958     FETCH last_evaluation INTO l_eval_opinion_id;
5959     CLOSE last_evaluation;
5960    --fnd_file.put_line(fnd_file.LOG, 'after last_evaludation:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
5961 
5962     OPEN total_num_of_proc;
5963     FETCH total_num_of_proc INTO l_total_num_of_procs;
5964     CLOSE total_num_of_proc;
5965    -- fnd_file.put_line(fnd_file.LOG, 'after total_num_of_proc:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
5966 
5967 
5968   OPEN proc_with_issue;
5969   FETCH proc_with_issue INTO l_proc_with_issue;
5970   CLOSE proc_with_issue;
5971    -- fnd_file.put_line(fnd_file.LOG, 'after proc_with_issue:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
5972 
5973  OPEN proc_without_issue;
5974  FETCH proc_without_issue INTO l_proc_without_issue;
5975  CLOSE proc_without_issue;
5976    -- fnd_file.put_line(fnd_file.LOG, 'after proc_without_issue:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
5977 
5978     l_proc_certified := l_proc_with_issue + l_proc_without_issue;
5979 
5980 --replace the calculation with one cursor to easy debug process.
5981 --    l_proc_pending_cert := l_total_num_of_procs-l_proc_certified;
5982 
5983     OPEN pending_proc;
5984     FETCH pending_proc INTO l_proc_pending_cert;
5985     CLOSE pending_proc;
5986 
5987 
5988     OPEN proc_with_ineff_ctrl;
5989     FETCH proc_with_ineff_ctrl INTO l_proc_with_ineff_ctrl;
5990     CLOSE proc_with_ineff_ctrl;
5991     --fnd_file.put_line(fnd_file.LOG, 'after proc_with_ineff_ctrl:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
5992 
5993 
5994      OPEN verified_processes;
5995      FETCH verified_processes INTO l_proc_verified;
5996      CLOSE verified_processes;
5997 
5998     OPEN unmitigated_risks;
5999     FETCH unmitigated_risks INTO l_unmitigated_risks;
6000     CLOSE unmitigated_risks;
6001     --fnd_file.put_line(fnd_file.LOG, 'after unmitigated:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6002 
6003 
6004     OPEN total_risks;
6005     FETCH total_risks INTO l_total_risks;
6006     CLOSE total_risks;
6007     --fnd_file.put_line(fnd_file.LOG, 'after total_risks:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6008 
6009 
6010      OPEN verified_risks;
6011      FETCH verified_risks INTO l_verified_risks;
6012      CLOSE verified_risks;
6013 
6014 
6015     OPEN ineffective_controls;
6016     FETCH ineffective_controls INTO l_ineff_controls;
6017     CLOSE ineffective_controls;
6018     --fnd_file.put_line(fnd_file.LOG, 'after ineffective_controls:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6019 
6020     OPEN total_controls;
6021     FETCH total_controls INTO l_total_controls;
6022     CLOSE total_controls;
6023     --fnd_file.put_line(fnd_file.LOG, 'after total_controls:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6024 
6025      OPEN verified_controls;
6026      FETCH verified_controls INTO l_verified_controls;
6027      CLOSE verified_controls;
6028 
6029     OPEN c_org;
6030     FETCH c_org INTO l_sub_vs, l_sub_code, l_lob_vs, l_lob_code;
6031     CLOSE c_org;
6032    -- fnd_file.put_line(fnd_file.LOG, 'after c_org:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6033 
6034     BEGIN
6035         l_open_findings := amw_findings_pkg.calculate_open_findings (
6036                'AMW_PROJ_FINDING',
6037 	       'PROJ_ORG', p_organization_id,
6038 	       null, null,
6039 	       null, null,
6040 	       null, null,
6041 	       null, null );
6042    EXCEPTION
6043         WHEN OTHERS THEN
6044             null;
6045     END;
6046 
6047     OPEN last_evaluation_history(l_eval_opinion_id);
6048     FETCH last_evaluation_history INTO l_eval_opinion_log_id;
6049     CLOSE last_evaluation_history;
6050    -- fnd_file.put_line(fnd_file.LOG, 'after last_evaludation_history:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6051 
6052     OPEN last_certification;
6053     FETCH last_certification INTO l_cert_opinion_id;
6054     CLOSE last_certification;
6055     --fnd_file.put_line(fnd_file.LOG, 'after last_certification:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6056 
6057     OPEN last_certification_history(l_cert_opinion_id);
6058     FETCH last_certification_history INTO l_cert_opinion_log_id;
6059     CLOSE last_certification_history;
6060     --fnd_file.put_line(fnd_file.LOG, 'after last_certification_history'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6061 
6062    -- fnd_file.put_line(fnd_file.LOG, 'after open_findings:'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
6063 
6064     /**05.25.2006 npanandi: bug 5250100 test**/
6065     if((l_sub_vs is null) OR (l_sub_code is null)) then
6066        G_ORG_ERROR := 'Y';
6067        /**05.25.2006 npanandi: need x_return_status in
6068           populate_all_fin_org_eval_sum procedure**/
6069        x_return_status := FND_API.G_RET_STS_ERROR;
6070        fnd_file.put_line(fnd_file.LOG, '********** WARNING: Unexpected error in OrganizationId: '||p_organization_id||', processing not done for this Organization' );
6071     else --- do the updates/inserts
6072       /**05.25.2006 npanandi: bug 5250100 test ends**/
6073 
6074 	  UPDATE  AMW_FIN_ORG_EVAL_SUM
6075          SET EVAL_OPINION_ID = l_eval_opinion_id,
6076              PROC_PENDING_CERTIFICATION = l_proc_pending_cert,
6077 		     TOTAL_NUMBER_OF_PROCS = l_total_num_of_procs,
6078 		     PROC_CERTIFIED_WITH_ISSUES = l_proc_with_issue,
6079 		     PROC_VERIFIED = l_proc_verified,
6080 		     PROC_CERTIFIED  = l_proc_certified,
6081 		     PROC_WITH_INEFFECTIVE_CONTROLS = l_proc_with_ineff_ctrl,
6082 		     UNMITIGATED_RISKS = l_unmitigated_risks,
6083 		     RISKS_VERIFIED = l_verified_risks,
6084 		     TOTAL_NUMBER_OF_RISKS = l_total_risks,
6085 		     INEFFECTIVE_CONTROLS = l_ineff_controls,
6086 		     CONTROLS_VERIFIED = l_verified_controls,
6087 		     TOTAL_NUMBER_OF_CTRLS = l_total_controls,
6088 		     PROC_PENDING_CERT_PRCNT =
6089 		      round(nvl(l_proc_pending_cert, 0) /decode(nvl(l_total_num_of_procs,0), 0,1,l_total_num_of_procs),2)*100,
6090 		     PROCESSES_WITH_ISSUES_PRCNT =
6091 	              round(nvl(l_proc_with_issue, 0) /decode(nvl(l_total_num_of_procs, 0), 0,1,l_total_num_of_procs),2)*100,
6092 		     PROC_WITH_INEFF_CONTROLS_PRCNT =
6093 		      round(nvl(l_proc_with_ineff_ctrl, 0) /decode(nvl(l_total_num_of_procs,0), 0,1,l_total_num_of_procs),2)*100,
6094 		     UNMITIGATED_RISKS_PRCNT = round(nvl(l_unmitigated_risks, 0) /decode(nvl(l_total_risks, 0),0,1,l_total_risks), 2)*100,
6095 		     INEFFECTIVE_CONTROLS_PRCNT = round(nvl(l_ineff_controls, 0) /decode(nvl(l_total_controls, 0),0,1,l_total_controls), 2)*100,
6096 		     OPEN_FINDINGS = l_open_findings,
6097              LAST_UPDATE_DATE = sysdate,
6098              LAST_UPDATED_BY = FND_GLOBAL.USER_ID,
6099              LAST_UPDATE_LOGIN = fnd_global.conc_login_id,
6100 		     OBJECT_VERSION_NUMBER = OBJECT_VERSION_NUMBER+1,
6101 		     SUBSIDIARY_VS = l_sub_vs,
6102 		     SUBSIDIARY_CODE = l_sub_code,
6103 		     LOB_VS = l_lob_vs,
6104 		     LOB_CODE = l_lob_code,
6105 		     CERT_OPINION_ID  = l_cert_opinion_id,
6106 		     EVAL_OPINION_LOG_ID  = l_eval_opinion_log_id,
6107 		     CERT_OPINION_LOG_ID  = l_cert_opinion_log_id
6108        WHERE fin_certification_id = p_certification_id
6109          AND organization_id = p_organization_id;
6110 
6111 
6112      IF (SQL%NOTFOUND) THEN
6113         INSERT INTO AMW_FIN_ORG_EVAL_SUM(
6114 	      FIN_CERTIFICATION_ID,
6115 	      ORGANIZATION_ID,
6116 	      EVAL_OPINION_ID,
6117 	      PROC_PENDING_CERTIFICATION,
6118 	      TOTAL_NUMBER_OF_PROCS,
6119 	      PROC_CERTIFIED_WITH_ISSUES,
6120 	      PROC_VERIFIED,
6121 	      PROC_CERTIFIED,
6122 	      PROC_WITH_INEFFECTIVE_CONTROLS,
6123 	      UNMITIGATED_RISKS,
6124 	      RISKS_VERIFIED,
6125 	      TOTAL_NUMBER_OF_RISKS,
6126 	      INEFFECTIVE_CONTROLS,
6127 	      CONTROLS_VERIFIED,
6128 	      TOTAL_NUMBER_OF_CTRLS,
6129 	      PROC_PENDING_CERT_PRCNT,
6130 	      PROCESSES_WITH_ISSUES_PRCNT,
6131 	      PROC_WITH_INEFF_CONTROLS_PRCNT,
6132 	      UNMITIGATED_RISKS_PRCNT,
6133 	      INEFFECTIVE_CONTROLS_PRCNT,
6134 	      OPEN_FINDINGS,
6135 	      CREATED_BY,
6136 	      CREATION_DATE,
6137 	      LAST_UPDATED_BY,
6138 	      LAST_UPDATE_DATE,
6139 	      LAST_UPDATE_LOGIN,
6140 	      OBJECT_VERSION_NUMBER,
6141 	      SUBSIDIARY_VS,
6142 	      SUBSIDIARY_CODE,
6143 	      LOB_VS,
6144 	      LOB_CODE,
6145 	      CERT_OPINION_ID ,
6146 	      EVAL_OPINION_LOG_ID ,
6147 	      CERT_OPINION_LOG_ID)
6148        SELECT p_certification_id,
6149               	      p_organization_id,
6150 	      l_eval_opinion_id,
6151 	      l_proc_pending_cert,
6152 	      l_total_num_of_procs,
6153 	      l_proc_with_issue,
6154 	      l_proc_verified,
6155 	      l_proc_certified,
6156 	      l_proc_with_ineff_ctrl,
6157 	      l_unmitigated_risks,
6158 	      l_verified_risks,
6159 	      l_total_risks,
6160 	      l_ineff_controls,
6161 	      l_verified_controls,
6162 	      l_total_controls,
6163 	      round(nvl(l_proc_pending_cert, 0)/decode(nvl(l_total_num_of_procs, 0), 0,1,l_total_num_of_procs),2)*100,
6164 	      round(nvl(l_proc_with_issue, 0)/decode(nvl(l_total_num_of_procs, 0), 0,1,l_total_num_of_procs),2)*100,
6165 	      round(nvl(l_proc_with_ineff_ctrl, 0)/decode(nvl(l_total_num_of_procs, 0), 0,1,l_total_num_of_procs),2)*100,
6166 	      round(nvl(l_unmitigated_risks, 0)/decode(nvl(l_total_risks, 0),0,1,l_total_risks), 2)*100,
6167 	      round(nvl(l_ineff_controls, 0) /decode(nvl(l_total_controls, 0),0,1,l_total_controls), 2)*100,
6168 	      l_open_findings,
6169               FND_GLOBAL.USER_ID,
6170               SYSDATE,
6171               FND_GLOBAL.USER_ID,
6172               SYSDATE,
6173               FND_GLOBAL.USER_ID,
6174 	      1,
6175 	      l_sub_vs,
6176 	      l_sub_code,
6177 	      l_lob_vs,
6178 	      l_lob_code,
6179 	      l_cert_opinion_id,
6180 	      l_eval_opinion_log_id,
6181 	      l_cert_opinion_log_id
6182         FROM  DUAL;
6183     END IF;
6184 
6185   end if; /***05.25.2006 npanandi: end of if clause for bug 5250100 testing***/
6186 
6187   if(p_commit <> FND_API.g_false) then
6188      commit;
6189   end if;
6190 
6191   x_return_status := FND_API.G_RET_STS_SUCCESS;
6192 
6193 EXCEPTION
6194      WHEN NO_DATA_FOUND THEN
6195      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
6196      x_return_status := FND_API.G_RET_STS_ERROR;
6197      x_msg_count := 1;
6198      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
6199       WHEN OTHERS THEN
6200        ROLLBACK TO Populate_Fin_Org_Eval_Sum;
6201       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
6202       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
6203         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6204         x_msg_count := 1;
6205         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
6206                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
6207                 FND_MSG_PUB.Count_And_Get(
6208                 p_encoded =>  FND_API.G_FALSE,
6209                 p_count   =>  x_msg_count,
6210                 p_data    =>  x_msg_data);
6211                 RETURN;
6212 END Populate_Fin_Org_Eval_Sum;
6213 
6214 
6215 PROCEDURE build_amw_fin_cert_eval_sum(
6216 p_api_version_number        IN   NUMBER   := 1.0,
6217 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
6218 p_commit                    IN   VARCHAR2 := FND_API.g_false,
6219 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
6220 p_certification_id  IN       NUMBER,
6221 x_return_status             OUT  nocopy VARCHAR2,
6222 x_msg_count                 OUT  nocopy NUMBER,
6223 x_msg_data                  OUT  nocopy VARCHAR2
6224 ) IS
6225 
6226 ---CURSOR TO GET ALL OF FINANCIAL ITEMS WHICH RELATE TO THIS FINANCIAL CERTIFICATION
6227 CURSOR Get_all_items IS
6228 SELECT DISTINCT ITEM.STATEMENT_GROUP_ID, ITEM.FINANCIAL_STATEMENT_ID, ITEM.FINANCIAL_ITEM_ID
6229 FROM AMW_FIN_CERT_SCOPE ITEM
6230 WHERE ITEM.FIN_CERTIFICATION_ID = P_CERTIFICATION_ID
6231 AND ITEM.FINANCIAL_ITEM_ID IS NOT NULL;
6232 
6233 --CURSOR TO GET ALL OF ACCOUNTS WHICH RELATE TO THIS FINANCIAL CERTIFICATION
6234 CURSOR 	Get_all_accts IS
6235 SELECT DISTINCT finitemAcc.statement_group_id, finitemAcc.financial_statement_id, finitemAcc.financial_item_id,  finitemAcc.account_group_id, finitemAcc.natural_account_id
6236 FROM AMW_FIN_CERT_SCOPE finitemAcc
6237 WHERE finitemAcc.FIN_CERTIFICATION_ID = P_CERTIFICATION_ID
6238 AND finitemAcc.natural_account_id is not null;
6239 
6240 ---CURSOR TO GET ALL OF CERTIFICATION WITH 'DRAFT', 'ACTIVE' STATUS
6241 cursor Get_Cert_for_processing
6242    is
6243    select
6244         certifcationb.CERTIFICATION_ID ,
6245         certifcationb.FINANCIAL_STATEMENT_ID,
6246         certifcationb.STATEMENT_GROUP_ID
6247    FROM
6248         AMW_CERTIFICATION_B certifcationb
6249    where
6250         certifcationb.OBJECT_TYPE='FIN_STMT'
6251     and certifcationb.CERTIFICATION_STATUS in ('ACTIVE', 'DRAFT');
6252 
6253 
6254 
6255      l_certification_id NUMBER;
6256 
6257   l_api_name           CONSTANT VARCHAR2(30) := 'build_amw_fin_cert_eval_sum';
6258 l_api_version_number CONSTANT NUMBER       := 1.0;
6259 
6260 l_return_status VARCHAR2(32767);
6261 l_msg_count NUMBER;
6262 l_msg_data VARCHAR2(32767);
6263 
6264 BEGIN
6265 
6266    SAVEPOINT build_amw_fin_cert_eval_sum;
6267 
6268  -- Standard call to check for call compatibility.
6269         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
6270                                              p_api_version_number,
6271                                              l_api_name,
6272                                              G_PKG_NAME)
6273         THEN
6274                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6275         END IF;
6276 
6277         -- Initialize message list if p_init_msg_list is set to TRUE.
6278         IF FND_API.to_Boolean( p_init_msg_list )
6279         THEN
6280                 FND_MSG_PUB.initialize;
6281         END IF;
6282 
6283         -- Initialize API return status to SUCCESS
6284         x_return_status := FND_API.G_RET_STS_SUCCESS;
6285 
6286 
6287 
6288      l_certification_id := P_CERTIFICATION_ID;
6289 
6290   --DELETE_ROWS(P_CERTIFICATION_ID, 'AMW_FIN_CERT_EVAL_SUM');
6291 
6292  FOR Get_all_items_Rec in Get_all_items LOOP
6293 	 exit when Get_all_items %notfound;
6294 	compute_values_for_eval_sum
6295                  (P_CERTIFICATION_ID => l_certification_id,
6296                   P_FINANCIAL_STATEMENT_ID => Get_all_items_Rec.FINANCIAL_STATEMENT_ID ,
6297                   P_STATEMENT_GROUP_ID => Get_all_items_Rec.STATEMENT_GROUP_ID ,
6298                   P_FINANCIAL_ITEM_ID => Get_all_items_Rec.FINANCIAL_ITEM_ID,
6299                   P_ACCOUNT_GROUP_ID   => NULL,
6300                   P_ACCOUNT_ID         => NULL,
6301                   P_OBJECT_TYPE => 'FINANCIAL ITEM',
6302                   x_return_status    => l_return_status,
6303      	x_msg_count   => l_msg_count,
6304     	x_msg_data    => l_msg_data);
6305 
6306 
6307                  amw_fin_coso_views_pvt.create_fin_ctrl_objectives
6308      	(P_CERTIFICATION_ID => l_certification_id,
6309                  P_FINANCIAL_STATEMENT_ID => Get_all_items_Rec.FINANCIAL_STATEMENT_ID ,
6310                   P_STATEMENT_GROUP_ID => Get_all_items_Rec.STATEMENT_GROUP_ID ,
6311                   P_FINANCIAL_ITEM_ID => Get_all_items_Rec.FINANCIAL_ITEM_ID,
6312                   P_ACCOUNT_GROUP_ID   => NULL,
6313                   P_ACCOUNT_ID         => NULL,
6314                   P_OBJECT_TYPE => 'FINANCIAL ITEM');
6315 
6316                  amw_fin_coso_views_pvt.create_fin_ctrl_Assertions
6317                  (P_CERTIFICATION_ID => l_certification_id,
6318                  P_FINANCIAL_STATEMENT_ID => Get_all_items_Rec.FINANCIAL_STATEMENT_ID ,
6319                   P_STATEMENT_GROUP_ID => Get_all_items_Rec.STATEMENT_GROUP_ID ,
6320                   P_FINANCIAL_ITEM_ID => Get_all_items_Rec.FINANCIAL_ITEM_ID,
6321                   P_ACCOUNT_GROUP_ID   => NULL,
6322                   P_ACCOUNT_ID         => NULL,
6323                   P_OBJECT_TYPE => 'FINANCIAL ITEM');
6324 
6325                 amw_fin_coso_views_pvt.create_fin_ctrl_components
6326                 (P_CERTIFICATION_ID => l_certification_id,
6327                  P_FINANCIAL_STATEMENT_ID => Get_all_items_Rec.FINANCIAL_STATEMENT_ID ,
6328                   P_STATEMENT_GROUP_ID => Get_all_items_Rec.STATEMENT_GROUP_ID ,
6329                   P_FINANCIAL_ITEM_ID => Get_all_items_Rec.FINANCIAL_ITEM_ID,
6330                   P_ACCOUNT_GROUP_ID   => NULL,
6331                   P_ACCOUNT_ID         => NULL,
6332                   P_OBJECT_TYPE => 'FINANCIAL ITEM');
6333 
6334 
6335 
6336  END LOOP;
6337 
6338   FOR Get_all_accts_Rec IN Get_all_accts LOOP
6339            exit when Get_all_accts%notfound;
6340            compute_values_for_eval_sum
6341                  (P_CERTIFICATION_ID => l_certification_id,
6342                   P_STATEMENT_GROUP_ID => Get_all_accts_Rec.STATEMENT_GROUP_ID ,
6343                   P_FINANCIAL_STATEMENT_ID => Get_all_accts_Rec.FINANCIAL_STATEMENT_ID,
6344                   P_FINANCIAL_ITEM_ID => Get_all_accts_Rec.financial_item_id,
6345                   P_ACCOUNT_ID         => Get_all_accts_Rec.natural_account_id,
6346                   P_ACCOUNT_GROUP_ID   => Get_all_accts_Rec.account_group_id,
6347                   P_OBJECT_TYPE => 'ACCOUNT',
6348                    x_return_status    => l_return_status,
6349      		x_msg_count   => l_msg_count,
6350     		x_msg_data    => l_msg_data);
6351 
6352 
6353           amw_fin_coso_views_pvt.create_fin_ctrl_objectives
6354            	(P_CERTIFICATION_ID => l_certification_id,
6355                   P_STATEMENT_GROUP_ID => Get_all_accts_Rec.STATEMENT_GROUP_ID ,
6356                   P_FINANCIAL_STATEMENT_ID => Get_all_accts_Rec.FINANCIAL_STATEMENT_ID,
6357                   P_FINANCIAL_ITEM_ID => Get_all_accts_Rec.financial_item_id,
6358                   P_ACCOUNT_ID         => Get_all_accts_Rec.natural_account_id,
6359                   P_ACCOUNT_GROUP_ID   => Get_all_accts_Rec.account_group_id,
6360                   P_OBJECT_TYPE => 'ACCOUNT');
6361 
6362          amw_fin_coso_views_pvt.create_fin_ctrl_Assertions
6363          	(P_CERTIFICATION_ID => l_certification_id,
6364                   P_STATEMENT_GROUP_ID => Get_all_accts_Rec.STATEMENT_GROUP_ID ,
6365                   P_FINANCIAL_STATEMENT_ID => Get_all_accts_Rec.FINANCIAL_STATEMENT_ID,
6366                   P_FINANCIAL_ITEM_ID => Get_all_accts_Rec.financial_item_id,
6367                   P_ACCOUNT_ID         => Get_all_accts_Rec.natural_account_id,
6368                   P_ACCOUNT_GROUP_ID   => Get_all_accts_Rec.account_group_id,
6369                   P_OBJECT_TYPE => 'ACCOUNT');
6370 
6371          amw_fin_coso_views_pvt.create_fin_ctrl_components
6372          	(P_CERTIFICATION_ID => l_certification_id,
6373                   P_STATEMENT_GROUP_ID => Get_all_accts_Rec.STATEMENT_GROUP_ID ,
6374                   P_FINANCIAL_STATEMENT_ID => Get_all_accts_Rec.FINANCIAL_STATEMENT_ID,
6375                   P_FINANCIAL_ITEM_ID => Get_all_accts_Rec.financial_item_id,
6376                   P_ACCOUNT_ID         => Get_all_accts_Rec.natural_account_id,
6377                   P_ACCOUNT_GROUP_ID   => Get_all_accts_Rec.account_group_id,
6378                   P_OBJECT_TYPE => 'ACCOUNT');
6379 
6380 
6381 
6382   END LOOP;
6383 
6384 
6385  x_return_status := FND_API.G_RET_STS_SUCCESS;
6386 
6387  EXCEPTION
6388      WHEN NO_DATA_FOUND THEN
6389      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
6390      x_return_status := FND_API.G_RET_STS_ERROR;
6391      x_msg_count := 1;
6392      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
6393       WHEN OTHERS THEN
6394        ROLLBACK TO build_amw_fin_cert_eval_sum;
6395       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
6396       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
6397         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6398         x_msg_count := 1;
6399         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
6400                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
6401                 FND_MSG_PUB.Count_And_Get(
6402                 p_encoded =>  FND_API.G_FALSE,
6403                 p_count   =>  x_msg_count,
6404                 p_data    =>  x_msg_data);
6405                 RETURN;
6406 
6407 
6408 END build_amw_fin_cert_eval_sum;
6409 
6410 /*********************************************************************************************/
6411 /**************************************Add by Dong -----------------------------------------*/
6412 /********************************* combine statement item and account calls into one --------*/
6413 /********************************************************************************************/
6414 PROCEDURE compute_values_for_eval_sum(
6415 p_api_version_number        IN   NUMBER   := 1.0,
6416 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
6417 p_commit                    IN   VARCHAR2 := FND_API.g_false,
6418 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
6419 p_certification_id IN NUMBER,
6420 p_financial_statement_id IN NUMBER ,
6421 p_statement_group_id IN NUMBER ,
6422 p_financial_item_id IN NUMBER,
6423 p_account_group_id IN NUMBER,
6424 p_account_id   IN NUMBER,
6425 p_object_type IN VARCHAR2,
6426 x_return_status             OUT  nocopy VARCHAR2,
6427 x_msg_count                 OUT  nocopy NUMBER,
6428 x_msg_data                  OUT  nocopy VARCHAR2
6429 ) IS
6430 
6431 
6432 -- variables for totals
6433       M_PROC_PENDING_CERTIFICATION number :=0;
6434       M_TOTAL_NUMBER_OF_PROCESSES  number :=0;
6435       M_PROC_CERTIFIED_WITH_ISSUES number :=0;
6436       M_PROCS_FOR_CERT_DONE number :=0;
6437       M_PROC_VERIFIED              number :=0;
6438 
6439       M_org_with_ineffective_ctrls  number :=0;
6440       M_org_certified              number :=0;
6441       M_org_evaluated 		number :=0;
6442       M_total_orgs		number :=0;
6443 
6444       M_proc_with_ineffective_ctrls  number :=0;
6445 
6446       M_unmitigated_risks          number :=0;
6447       M_risks_verified             number :=0;
6448       M_total_risks		 number :=0;
6449 
6450       M_ineffective_controls       number :=0;
6451       M_controls_verified          number :=0;
6452       M_total_controls		 number :=0;
6453 
6454       M_open_issues                number :=0;
6455 
6456       M_PRO_PENDING_CERT_PRCNT number :=0;
6457       M_PROCESSES_WITH_ISSUES_PRCNT number :=0;
6458       M_ORG_WITH_INEFF_CTRLS_PRCNT number :=0;
6459       M_PROC_WITH_INEFF_CTRLS_PRCNT number :=0;
6460       M_UNMITIGATED_RISKS_PRCNT number  :=0;
6461       M_INEFFECTIVE_CONTROLS_PRCNT number :=0;
6462 
6463 
6464 
6465       --l_eval_opinion_id NUMBER;
6466      -- l_eval_opinion_log_id NUMBER;
6467 
6468       l_stmt  VARCHAR2(4000);
6469 
6470  	l_deleteme date;
6471 
6472       L_FINANCIAL_STATEMENT_ID NUMBER ;
6473       L_CERTIFICATION_ID NUMBER ;
6474       L_STATEMENT_GROUP_ID NUMBER ;
6475       L_FINANCIAL_ITEM_ID NUMBER ;
6476       L_ACCOUNT_ID NUMBER;
6477       L_OBJECT_TYPE VARCHAR2(30) ;
6478       L_ACCOUNT_GROUP_ID NUMBER ;
6479 
6480       l_start_time date;
6481       l_end_time date;
6482 
6483 l_api_name           CONSTANT VARCHAR2(30) := 'compute_values_for_eval_sum';
6484 l_api_version_number CONSTANT NUMBER       := 1.0;
6485 
6486 l_return_status VARCHAR2(32767);
6487 l_msg_count NUMBER;
6488 l_msg_data VARCHAR2(32767);
6489 
6490 BEGIN
6491 
6492 SAVEPOINT compute_values_for_eval_sum;
6493 
6494  -- Standard call to check for call compatibility.
6495         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
6496                                              p_api_version_number,
6497                                              l_api_name,
6498                                              G_PKG_NAME)
6499         THEN
6500                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6501         END IF;
6502 
6503         -- Initialize message list if p_init_msg_list is set to TRUE.
6504         IF FND_API.to_Boolean( p_init_msg_list )
6505         THEN
6506                 FND_MSG_PUB.initialize;
6507         END IF;
6508 
6509         -- Initialize API return status to SUCCESS
6510         x_return_status := FND_API.G_RET_STS_SUCCESS;
6511 
6512 
6513 
6514       L_FINANCIAL_STATEMENT_ID  := P_FINANCIAL_STATEMENT_ID;
6515       L_CERTIFICATION_ID  := P_CERTIFICATION_ID;
6516       L_STATEMENT_GROUP_ID := P_STATEMENT_GROUP_ID;
6517       L_FINANCIAL_ITEM_ID  := P_FINANCIAL_ITEM_ID;
6518       L_ACCOUNT_ID  := P_ACCOUNT_ID;
6519       L_OBJECT_TYPE  := p_object_type;
6520       L_ACCOUNT_GROUP_ID  :=  p_account_group_id;
6521 
6522 
6523           l_start_time := sysdate;
6524          M_TOTAL_NUMBER_OF_PROCESSES := GetTotalProcesses
6525                   (P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6526                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6527                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6528                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6529                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6530                   P_ACCOUNT_ID    => L_ACCOUNT_ID,
6531                   P_OBJECT_TYPE => L_OBJECT_TYPE);
6532            l_end_time := sysdate;
6533 
6534        IF (M_TOTAL_NUMBER_OF_PROCESSES IS NOT NULL OR
6535            M_TOTAL_NUMBER_OF_PROCESSES <> 0)
6536        THEN
6537 
6538 
6539 
6540 
6541         l_start_time := sysdate;
6542 
6543        M_PROCS_FOR_CERT_DONE := Get_Proc_Certified_Done(
6544        	 P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6545                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6546                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6547                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6548                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6549                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6550                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6551 
6552         l_end_time := sysdate;
6553 
6554            l_start_time := sysdate;
6555          M_PROC_VERIFIED := Get_Proc_Verified(
6556         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6557                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6558                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6559                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6560                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6561                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6562                   P_OBJECT_TYPE => P_OBJECT_TYPE );
6563 
6564         l_end_time := sysdate;
6565 
6566 
6567  M_PROC_PENDING_CERTIFICATION := M_TOTAL_NUMBER_OF_PROCESSES - M_PROCS_FOR_CERT_DONE ;
6568 
6569                     l_start_time := sysdate;
6570 	-- CountProcswithIssues_finitem
6571 	 M_PROC_CERTIFIED_WITH_ISSUES := Get_PROC_CERT_WITH_ISSUES(
6572         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6573                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6574                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6575                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6576                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6577                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6578                   P_OBJECT_TYPE => P_OBJECT_TYPE );
6579 
6580 
6581  	l_end_time := sysdate;
6582 
6583 
6584 
6585                        l_start_time := sysdate;
6586 	--  CountOrgsIneffCtrl_finitem
6587 	M_ORG_WITH_INEFFECTIVE_CTRLS :=  Get_ORG_WITH_INEFF_CTRLS(
6588         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6589                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6590                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6591                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6592                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6593                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6594                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6595 
6596                    l_end_time := sysdate;
6597 
6598 
6599                      l_start_time := sysdate;
6600        --CountOrgsEvaluated_finitem
6601         M_ORG_EVALUATED := Get_ORG_EVALUATED (
6602         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6603                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6604                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6605                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6606                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6607                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6608                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6609 
6610                   l_end_time := sysdate;
6611 
6612             M_TOTAL_ORGS := Get_TOTAL_ORGS (
6613         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6614                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6615                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6616                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6617                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6618                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6619                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6620 
6621 
6622 	 l_start_time := sysdate;
6623 	--CountOrgsCertified_finitem
6624 	M_ORG_CERTIFIED := Get_ORG_CERTIFIED (
6625         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6626                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6627                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6628                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6629                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6630                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6631                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6632 
6633 	 l_end_time := sysdate;
6634 
6635 	 l_start_time := sysdate;
6636 --OPEN CountProcsIneffCtrl_finitem
6637 	M_PROC_WITH_INEFFECTIVE_CTRLS := Get_PROC_WITH_INEFF_CTRLS (
6638         	 P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6639                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6640                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6641                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6642                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6643                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6644                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6645 
6646 	 l_end_time := sysdate;
6647 
6648 
6649  	 l_start_time := sysdate;
6650 --OPEN CountIneffectiveCtrls_finitem
6651 	M_INEFFECTIVE_CONTROLS := Get_INEFFECTIVE_CONTROLS (
6652         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6653                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6654                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6655                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6656                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6657                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6658                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6659 
6660 	 l_end_time := sysdate;
6661 
6662 
6663 	 l_start_time := sysdate;
6664 -- CountUnmittigatedRisk_finitem
6665 M_UNMITIGATED_RISKS := Get_UNMITIGATED_RISKS (
6666         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6667                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6668                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6669                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6670                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6671                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6672                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6673 
6674  	 l_end_time := sysdate;
6675 
6676             l_start_time := sysdate;
6677 --  CountRisksVerified_finitem
6678 M_RISKS_VERIFIED := Get_RISKS_VERIFIED(
6679         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6680                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6681                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6682                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6683                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6684                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6685                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6686 
6687                    l_end_time := sysdate;
6688 
6689 
6690          M_TOTAL_RISKS := Get_Total_RISKS(
6691         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6692                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6693                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6694                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6695                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6696                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6697                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6698 
6699  	 l_start_time := sysdate;
6700 	M_CONTROLS_VERIFIED := Get_CONTROLS_VERIFIED(
6701         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6702                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6703                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6704                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6705                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6706                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6707                   P_OBJECT_TYPE => P_OBJECT_TYPE);
6708 
6709 		  l_end_time := sysdate;
6710 
6711 
6712 	M_TOTAL_CONTROLS := Get_TOTAL_CONTROLS(
6713         	  P_CERTIFICATION_ID => L_CERTIFICATION_ID,
6714                   P_FINANCIAL_STATEMENT_ID => L_FINANCIAL_STATEMENT_ID,
6715                   P_STATEMENT_GROUP_ID => L_STATEMENT_GROUP_ID ,
6716                   P_FINANCIAL_ITEM_ID => L_FINANCIAL_ITEM_ID,
6717                   P_ACCOUNT_GROUP_ID  => L_ACCOUNT_GROUP_ID,
6718                   P_ACCOUNT_ID    => P_ACCOUNT_ID,
6719                   P_OBJECT_TYPE => P_OBJECT_TYPE );
6720 
6721 
6722  END IF;  -- end of total process <>0
6723 
6724 
6725          -- insert a fin item record
6726                insert_fin_cert_eval_sum(
6727                 X_FIN_CERTIFICATION_ID                       => P_CERTIFICATION_ID,
6728                 X_FINANCIAL_STATEMENT_ID                     => P_FINANCIAL_STATEMENT_ID,
6729                 X_FINANCIAL_ITEM_ID                          => P_FINANCIAL_ITEM_ID,
6730                 X_ACCOUNT_GROUP_ID                           => P_ACCOUNT_GROUP_ID,
6731                 X_NATURAL_ACCOUNT_ID                         => P_ACCOUNT_ID,
6732                 X_OBJECT_TYPE                                => P_OBJECT_TYPE,
6733                 X_PROC_PENDING_CERTIFICATION                 => M_PROC_PENDING_CERTIFICATION,
6734                 X_TOTAL_NUMBER_OF_PROCESSES                  => M_TOTAL_NUMBER_OF_PROCESSES,
6735                 X_PROC_CERTIFIED_WITH_ISSUES                 => M_PROC_CERTIFIED_WITH_ISSUES,
6736                 X_PROCS_FOR_CERT_DONE                        => M_PROCS_FOR_CERT_DONE,
6737                 x_proc_evaluated                             => M_PROC_VERIFIED,
6738                 X_ORG_WITH_INEFFECTIVE_CTRLS                 => M_org_with_ineffective_ctrls,
6739                 -- X_ORG_CERTIFIED                           => M_org_certified,
6740                 x_orgs_FOR_CERT_DONE                         => M_org_certified,
6741                 x_orgs_evaluated                             => M_org_evaluated,
6742                 x_total_orgs		=>  M_total_orgs,
6743                 X_PROC_WITH_INEFFECTIVE_CTRLS                => M_proc_with_ineffective_ctrls,
6744                 X_UNMITIGATED_RISKS                          => M_unmitigated_risks,
6745                 X_RISKS_VERIFIED                             => M_risks_verified,
6746                 X_TOTAL_RISKS		=> M_total_risks,
6747                 X_INEFFECTIVE_CONTROLS                       => M_ineffective_controls,
6748                 X_CONTROLS_VERIFIED                          => M_controls_verified,
6749                 X_TOTAL_CONTROLS     => M_total_controls,
6750                 X_OPEN_ISSUES                                => M_open_issues,
6751                 --X_PRO_PENDING_CERT_PRCNT                     => M_PRO_PENDING_CERT_PRCNT,
6752                 --X_PROCESSES_WITH_ISSUES_PRCNT                => M_PROCESSES_WITH_ISSUES_PRCNT,
6753                 --X_ORG_WITH_INEFF_CTRLS_PRCNT                 => M_ORG_WITH_INEFF_CTRLS_PRCNT,
6754                 --X_PROC_WITH_INEFF_CTRLS_PRCNT                => M_PROC_WITH_INEFF_CTRLS_PRCNT,
6755                 --X_UNMITIGATED_RISKS_PRCNT                    => M_UNMITIGATED_RISKS_PRCNT,
6756                 --X_INEFFECTIVE_CTRLS_PRCNT                    => M_INEFFECTIVE_CONTROLS_PRCNT,
6757                 X_PRO_PENDING_CERT_PRCNT                     => null,
6758                 X_PROCESSES_WITH_ISSUES_PRCNT                => null,
6759                 X_ORG_WITH_INEFF_CTRLS_PRCNT                 => null,
6760                 X_PROC_WITH_INEFF_CTRLS_PRCNT                => null,
6761                 X_UNMITIGATED_RISKS_PRCNT                    => null,
6762                 X_INEFFECTIVE_CTRLS_PRCNT                    => null,
6763                 X_OBJ_CONTEXT                                => NULL,
6764                 --X_CREATED_BY                               => g_user_id,
6765                 X_CREATED_BY                                 => 1,
6766                 X_CREATION_DATE                              => SYSDATE,
6767                 --X_LAST_UPDATED_BY                          => g_user_id,
6768                 X_LAST_UPDATED_BY                            => 1,
6769                 X_LAST_UPDATE_DATE                           => SYSDATE,
6770                 --X_LAST_UPDATE_LOGIN                        => g_login_id,
6771                 X_LAST_UPDATE_LOGIN                          => 1,
6772                 X_SECURITY_GROUP_ID                          => NULL,
6773                 X_OBJECT_VERSION_NUMBER                      => NULL,
6774                 x_return_status    => l_return_status,
6775      		x_msg_count   => l_msg_count,
6776     		x_msg_data    => l_msg_data);
6777 
6778 x_return_status := FND_API.G_RET_STS_SUCCESS;
6779 
6780 EXCEPTION
6781      WHEN NO_DATA_FOUND THEN
6782      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
6783      x_return_status := FND_API.G_RET_STS_ERROR;
6784      x_msg_count := 1;
6785      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
6786       WHEN OTHERS THEN
6787        ROLLBACK TO compute_values_for_eval_sum;
6788       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
6789       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
6790         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6791         x_msg_count := 1;
6792         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
6793                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
6794                 FND_MSG_PUB.Count_And_Get(
6795                 p_encoded =>  FND_API.G_FALSE,
6796                 p_count   =>  x_msg_count,
6797                 p_data    =>  x_msg_data);
6798                 RETURN;
6799 
6800 END compute_values_for_eval_sum;
6801 
6802 ------------populate scorecard info  --------------------------------
6803 /*******************************delete becuase loop is put into master proceudre ****************
6804 PROCEDURE Populate_All_Cert_General_Sum(
6805 p_api_version_number        IN   NUMBER   := 1.0,
6806 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
6807 p_commit                    IN   VARCHAR2 := FND_API.g_false,
6808 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
6809 p_certification_id  IN       NUMBER,
6810 x_return_status             OUT  nocopy VARCHAR2,
6811 x_msg_count                 OUT  nocopy NUMBER,
6812 x_msg_data                  OUT  nocopy VARCHAR2
6813 ) IS
6814 -- select all processes in scope for the certification
6815     CURSOR c_cert IS
6816         SELECT cert.CERTIFICATION_ID, period.start_date
6817           FROM AMW_CERTIFICATION_B cert, AMW_GL_PERIODS_V period
6818          WHERE cert.object_type = 'FIN_STMT' and cert.certification_period_name = period.period_name
6819            AND cert.certification_period_set_name = period.period_set_name
6820            and cert.CERTIFICATION_STATUS in ('ACTIVE', 'DRAFT');
6821 
6822 CURSOR c_start_date IS
6823     	SELECT period.start_date
6824           FROM AMW_CERTIFICATION_B cert, AMW_GL_PERIODS_V period
6825          WHERE cert.object_type = 'FIN_STMT' and cert.certification_period_name = period.period_name
6826            AND cert.certification_period_set_name = period.period_set_name
6827            and cert.CERTIFICATION_STATUS in ('ACTIVE', 'DRAFT')
6828            AND cert.certification_id = p_certification_id;
6829 
6830     l_start_date DATE;
6831 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_All_Cert_General_Sum';
6832 l_api_version_number CONSTANT NUMBER       := 1.0;
6833 
6834 l_return_status VARCHAR2(32767);
6835 l_msg_count NUMBER;
6836 l_msg_data VARCHAR2(32767);
6837 
6838 BEGIN
6839 
6840 SAVEPOINT Populate_All_Cert_General_Sum;
6841 
6842  -- Standard call to check for call compatibility.
6843         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
6844                                              p_api_version_number,
6845                                              l_api_name,
6846                                              G_PKG_NAME)
6847         THEN
6848                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6849         END IF;
6850 
6851         -- Initialize message list if p_init_msg_list is set to TRUE.
6852         IF FND_API.to_Boolean( p_init_msg_list )
6853         THEN
6854                 FND_MSG_PUB.initialize;
6855         END IF;
6856 
6857         -- Initialize API return status to SUCCESS
6858         x_return_status := FND_API.G_RET_STS_SUCCESS;
6859 
6860 
6861         OPEN c_start_date;
6862         FETCH c_start_date INTO l_start_date;
6863         CLOSE c_start_date;
6864 
6865    IF p_certification_id IS NOT NULL THEN
6866 
6867         IF l_start_date is not null THEN
6868             Populate_Cert_General_Sum
6869             (p_certification_id => p_certification_id,
6870              p_start_date => l_start_date,
6871              x_return_status    => l_return_status,
6872      	     x_msg_count   => l_msg_count,
6873     	     x_msg_data    => l_msg_data);
6874         END IF;
6875 
6876         ELSE
6877         FOR cert_rec IN c_cert LOOP
6878              exit when c_cert%notfound;
6879 
6880              IF l_start_date is not null THEN
6881             Populate_Cert_General_Sum
6882             (p_certification_id => cert_rec.certification_id,
6883              p_start_date => cert_rec.start_date,
6884              x_return_status    => l_return_status,
6885      	     x_msg_count   => l_msg_count,
6886     	     x_msg_data    => l_msg_data);
6887             END IF;
6888 
6889         END LOOP;
6890     END IF;
6891 
6892 
6893 x_return_status := FND_API.G_RET_STS_SUCCESS;
6894 
6895 EXCEPTION
6896      WHEN NO_DATA_FOUND THEN
6897           IF c_cert%ISOPEN THEN
6898       	close c_cert;
6899       END IF;
6900      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
6901      x_return_status := FND_API.G_RET_STS_ERROR;
6902      x_msg_count := 1;
6903      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
6904       WHEN OTHERS THEN
6905           IF c_cert%ISOPEN THEN
6906       	close c_cert;
6907       END IF;
6908         ROLLBACK TO Populate_All_Cert_General_Sum;
6909       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
6910       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
6911         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6912         x_msg_count := 1;
6913         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
6914                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
6915                 FND_MSG_PUB.Count_And_Get(
6916                 p_encoded =>  FND_API.G_FALSE,
6917                 p_count   =>  x_msg_count,
6918                 p_data    =>  x_msg_data);
6919                 RETURN;
6920 
6921 END Populate_All_Cert_General_Sum;
6922 
6923 ***********************/
6924 
6925 
6926 PROCEDURE  Populate_Cert_General_Sum(
6927 p_api_version_number        IN   NUMBER   := 1.0,
6928 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
6929 p_commit                    IN   VARCHAR2 := FND_API.g_false,
6930 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
6931 p_certification_id          IN    	NUMBER,
6932 p_start_date		IN  	DATE,
6933 x_return_status             OUT  nocopy VARCHAR2,
6934 x_msg_count                 OUT  nocopy NUMBER,
6935 x_msg_data                  OUT  nocopy VARCHAR2
6936 )
6937 IS
6938 
6939          CURSOR new_risks_added IS
6940           SELECT count(1)
6941 	  FROM AMW_RISK_ASSOCIATIONS
6942          WHERE association_creation_date >= (SELECT period.start_date
6943           FROM AMW_CERTIFICATION_B cert, AMW_GL_PERIODS_V period
6944          WHERE cert.object_type = 'FIN_STMT' and cert.certification_period_name = period.period_name
6945            AND cert.certification_period_set_name = period.period_set_name
6946            AND cert.certification_id = p_certification_id)
6947            AND object_type = 'PROCESS_FINCERT'
6948            AND pk1 = p_certification_id;
6949 
6950 
6951 
6952  CURSOR new_controls_added IS
6953     	SELECT count(1)
6954     	FROM AMW_CONTROL_ASSOCIATIONS
6955     	WHERE association_creation_date >= (SELECT period.start_date
6956           FROM AMW_CERTIFICATION_B cert, AMW_GL_PERIODS_V period
6957          WHERE cert.object_type = 'FIN_STMT' and cert.certification_period_name = period.period_name
6958            AND cert.certification_period_set_name = period.period_set_name
6959            AND cert.certification_id = p_certification_id)
6960     	and pk1 = p_certification_id
6961     	and object_type = 'RISK_FINCERT';
6962 
6963 
6964     CURSOR orgs_in_scope IS
6965         SELECT count(distinct fin.organization_id)
6966         FROM AMW_FIN_CERT_SCOPE fin
6967         where fin.FIN_CERTIFICATION_ID= p_certification_id;
6968 --------------------------------------------------------------------------
6969 
6970     l_new_risks_added                	NUMBER;
6971     l_new_controls_added             	NUMBER;
6972     l_global_proc_not_certified      	NUMBER;
6973     l_global_proc_with_issue 	     	NUMBER;
6974     l_local_proc_not_certified 	     	NUMBER;
6975     l_local_proc_with_issue          	NUMBER;
6976     l_global_proc_ineff_ctrl NUMBER;
6977     l_local_proc_ineff_ctrl 	NUMBER;
6978     l_unmitigated_risks 		NUMBER;
6979     l_ineffective_controls 		NUMBER;
6980     l_orgs_in_scope			NUMBER;
6981     l_orgs_pending_in_scope		NUMBER;
6982 
6983     l_certification_id NUMBER ;
6984 
6985 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Cert_General_Sum';
6986 l_api_version_number CONSTANT NUMBER       := 1.0;
6987 
6988 l_return_status VARCHAR2(32767);
6989 l_msg_count NUMBER;
6990 l_msg_data VARCHAR2(32767);
6991 
6992 BEGIN
6993 
6994 SAVEPOINT Populate_Cert_General_Sum;
6995 
6996  -- Standard call to check for call compatibility.
6997         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
6998                                              p_api_version_number,
6999                                              l_api_name,
7000                                              G_PKG_NAME)
7001         THEN
7002                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7003         END IF;
7004 
7005         -- Initialize message list if p_init_msg_list is set to TRUE.
7006         IF FND_API.to_Boolean( p_init_msg_list )
7007         THEN
7008                 FND_MSG_PUB.initialize;
7009         END IF;
7010 
7011         -- Initialize API return status to SUCCESS
7012         x_return_status := FND_API.G_RET_STS_SUCCESS;
7013 
7014 
7015     l_certification_id := p_certification_id;
7016 
7017     --fnd_file.put_line (fnd_file.LOG, 'p_certification_id='||to_char(p_certification_id));
7018     --fnd_file.put_line(fnd_file.LOG, 'before new_risks_added :'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
7019 
7020     OPEN new_risks_added;
7021     FETCH new_risks_added INTO l_new_risks_added;
7022     CLOSE new_risks_added;
7023 
7024     --fnd_file.put_line(fnd_file.LOG, 'before new_controls_added :'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
7025     OPEN new_controls_added;
7026     FETCH new_controls_added INTO l_new_controls_added;
7027     CLOSE new_controls_added;
7028 
7029     --fnd_file.put_line(fnd_file.LOG, 'before orgs_in_scope :'||to_char(sysdate, 'hh:mi:ss dd-mon-rrrr'));
7030     OPEN orgs_in_scope;
7031     FETCH orgs_in_scope INTO l_orgs_in_scope;
7032     CLOSE orgs_in_scope;
7033 
7034 
7035 Get_global_proc_not_certified(p_certification_id => l_certification_id, x_global_proc_not_certified => l_global_proc_not_certified);
7036 Get_local_proc_not_certified(p_certification_id => l_certification_id, x_local_proc_not_certified => l_local_proc_not_certified);
7037 
7038 Get_global_proc_with_issue(p_certification_id => l_certification_id, x_global_proc_with_issue => l_global_proc_with_issue);
7039 Get_local_proc_with_issue(p_certification_id => l_certification_id,
7040 			  x_local_proc_with_issue => l_local_proc_with_issue);
7041 
7042 Get_global_proc_ineff_ctrl(p_certification_id => l_certification_id,
7043 				x_global_proc_ineff_ctrl => l_global_proc_ineff_ctrl);
7044 Get_local_proc_ineff_ctrl(p_certification_id => l_certification_id,
7045 			  x_local_proc_ineff_ctrl => l_local_proc_ineff_ctrl);
7046 
7047 Get_unmitigated_risks(p_certification_id => l_certification_id,
7048  		      x_unmitigated_risks => l_unmitigated_risks);
7049 Get_ineffective_controls(p_certification_id => l_certification_id,
7050  		         x_ineffective_controls => l_ineffective_controls);
7051 Get_orgs_pending_in_scope(p_certification_id => l_certification_id,
7052  		         x_orgs_pending_in_scope => l_orgs_pending_in_scope);
7053 
7054     UPDATE  AMW_CERT_DASHBOARD_SUM
7055        SET NEW_RISKS_ADDED = l_new_risks_added,
7056            NEW_CONTROLS_ADDED = l_new_controls_added,
7057            PROCESSES_NOT_CERT = l_global_proc_not_certified,
7058            PROCESSES_CERT_ISSUES = l_global_proc_with_issue,
7059            ORG_PROCESS_NOT_CERT = l_local_proc_not_certified,
7060            ORG_PROCESS_CERT_ISSUES = l_local_proc_with_issue,
7061            PROC_INEFF_CONTROL = l_global_proc_ineff_ctrl,
7062            ORG_PROC_INEFF_CONTROL = l_local_proc_ineff_ctrl,
7063            UNMITIGATED_RISKS = l_unmitigated_risks,
7064            INEFFECTIVE_CONTROLS = l_ineffective_controls,
7065            ORGS_IN_SCOPE = l_orgs_in_scope,
7066            ORGS_PENDING_IN_SCOPE = l_orgs_pending_in_scope,
7067            PERIOD_START_DATE = p_start_date,
7068            LAST_UPDATE_DATE = sysdate,
7069            LAST_UPDATED_BY = FND_GLOBAL.USER_ID,
7070 	   LAST_UPDATE_LOGIN = fnd_global.conc_login_id
7071      WHERE certification_id = p_certification_id;
7072 
7073     IF (SQL%NOTFOUND) THEN
7074        INSERT INTO AMW_CERT_DASHBOARD_SUM (
7075 	  CERTIFICATION_ID,
7076                   NEW_RISKS_ADDED,
7077                   NEW_CONTROLS_ADDED,
7078                   PROCESSES_NOT_CERT,
7079                   PROCESSES_CERT_ISSUES,
7080                   ORG_PROCESS_NOT_CERT,
7081                   ORG_PROCESS_CERT_ISSUES,
7082                   PROC_INEFF_CONTROL,
7083                   ORG_PROC_INEFF_CONTROL,
7084                   UNMITIGATED_RISKS,
7085                   INEFFECTIVE_CONTROLS,
7086                   ORGS_IN_SCOPE,
7087                   ORGS_PENDING_IN_SCOPE,
7088                   PERIOD_START_DATE,
7089 	          CREATED_BY,
7090 	          CREATION_DATE,
7091                   LAST_UPDATE_DATE,
7092                   LAST_UPDATED_BY,
7093 	          LAST_UPDATE_LOGIN)
7094 	SELECT p_certification_id,
7095        	       l_new_risks_added,
7096        	       l_new_controls_added,
7097        	       l_global_proc_not_certified,
7098        	       l_global_proc_with_issue,
7099        	       l_local_proc_not_certified,
7100        	       l_local_proc_with_issue,
7101 	       l_global_proc_ineff_ctrl,
7102                l_local_proc_ineff_ctrl,
7103                l_unmitigated_risks,
7104                l_ineffective_controls,
7105                l_orgs_in_scope,
7106                l_orgs_pending_in_scope,
7107                p_start_date,
7108                FND_GLOBAL.USER_ID,
7109                SYSDATE,
7110                SYSDATE,
7111 	       FND_GLOBAL.USER_ID,
7112 	       FND_GLOBAL.USER_ID
7113 	FROM  DUAL;
7114     END IF;
7115 
7116 if(p_commit <> FND_API.g_false)
7117 then commit;
7118 end if;
7119 
7120 x_return_status := FND_API.G_RET_STS_SUCCESS;
7121 
7122 EXCEPTION
7123      WHEN NO_DATA_FOUND THEN
7124      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
7125      x_return_status := FND_API.G_RET_STS_ERROR;
7126      x_msg_count := 1;
7127      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
7128       WHEN OTHERS THEN
7129        ROLLBACK TO Populate_Cert_General_Sum;
7130       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
7131       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
7132         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7133         x_msg_count := 1;
7134         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
7135                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
7136                 FND_MSG_PUB.Count_And_Get(
7137                 p_encoded =>  FND_API.G_FALSE,
7138                 p_count   =>  x_msg_count,
7139                 p_data    =>  x_msg_data);
7140                 RETURN;
7141 
7142 END Populate_Cert_General_Sum;
7143 
7144 PROCEDURE reset_amw_fin_cert_eval_sum(p_certification_id in number)
7145 is
7146   begin
7147 
7148   SAVEPOINT reset_amw_fin_cert_eval_sum;
7149 
7150   if p_certification_id is not null then
7151    update amw_fin_cert_eval_sum
7152    set
7153    	LAST_UPDATE_DATE = sysdate,
7154    	last_updated_by = fnd_global.user_id,
7155    	last_update_login = fnd_global.conc_login_id,
7156       PROC_PENDING_CERTIFICATION = 0,
7157       TOTAL_NUMBER_OF_PROCESSES  =0,
7158       PROC_CERTIFIED_WITH_ISSUES =0,
7159       -- PROC_VERIFIED              =0 ,
7160       PROCS_FOR_CERT_DONE       =0 ,
7161       proc_evaluated            =0 ,
7162       org_with_ineffective_controls  =0,
7163       -- org_certified              =0,
7164       orgs_FOR_CERT_DONE             =0,
7165       orgs_evaluated                    =0,
7166       proc_with_ineffective_controls  =0,
7167       unmitigated_risks          =0,
7168       risks_verified             =0,
7169       ineffective_controls       =0,
7170       controls_verified          =0,
7171       open_issues                =0,
7172       PRO_PENDING_CERT_PRCNT =0,
7173       PROCESSES_WITH_ISSUES_PRCNT =0,
7174       ORG_WITH_INEFF_CONTROLS_PRCNT =0,
7175       PROC_WITH_INEFF_CONTROLS_PRCNT =0,
7176       UNMITIGATED_RISKS_PRCNT =0,
7177       INEFFECTIVE_CONTROLS_PRCNT =0,
7178        total_number_of_risks = 0,
7179       total_number_of_ctrls = 0,
7180       total_number_of_orgs = 0
7181       WHERE fin_certification_id = p_certification_id;
7182   else
7183     update amw_fin_cert_eval_sum
7184     set
7185       LAST_UPDATE_DATE = sysdate,
7186       last_updated_by = fnd_global.user_id,
7187       last_update_login = fnd_global.conc_login_id,
7188       PROC_PENDING_CERTIFICATION = 0,
7189       TOTAL_NUMBER_OF_PROCESSES  =0,
7190       PROC_CERTIFIED_WITH_ISSUES =0,
7191       --PROC_VERIFIED              =0 ,
7192        PROCS_FOR_CERT_DONE            =0 ,
7193      proc_evaluated                    =0 ,
7194       org_with_ineffective_controls  =0,
7195       -- org_certified              =0,
7196       orgs_FOR_CERT_DONE        =0,
7197       orgs_evaluated            =0,
7198       proc_with_ineffective_controls  =0,
7199       unmitigated_risks          =0,
7200       risks_verified             =0,
7201       ineffective_controls       =0,
7202       controls_verified          =0,
7203       open_issues                =0,
7204       PRO_PENDING_CERT_PRCNT =0,
7205       PROCESSES_WITH_ISSUES_PRCNT =0,
7206       ORG_WITH_INEFF_CONTROLS_PRCNT =0,
7207       PROC_WITH_INEFF_CONTROLS_PRCNT =0,
7208       UNMITIGATED_RISKS_PRCNT =0,
7209       INEFFECTIVE_CONTROLS_PRCNT =0,
7210        total_number_of_risks = 0,
7211       total_number_of_ctrls = 0,
7212       total_number_of_orgs = 0
7213       WHERE fin_certification_id IN
7214       (select certifcationb.CERTIFICATION_ID
7215        		FROM    AMW_CERTIFICATION_B certifcationb
7216        		where certifcationb.OBJECT_TYPE='FIN_STMT'
7217        		and certifcationb.CERTIFICATION_STATUS in ('ACTIVE', 'DRAFT'));
7218    end if;
7219 
7220 END reset_amw_fin_cert_eval_sum;
7221 
7222 PROCEDURE reset_amw_fin_proc_eval_sum(p_certification_id in number)
7223 IS
7224 BEGIN
7225 SAVEPOINT reset_amw_fin_proc_eval_sum;
7226 
7227      IF(p_certification_id is not null) then
7228      	UPDATE  AMW_FIN_PROCESS_EVAL_SUM
7229        SET
7230        	   LAST_UPDATE_DATE = sysdate,
7231    	   last_updated_by = fnd_global.user_id,
7232    	   last_update_login = fnd_global.conc_login_id,
7233            CERT_OPINION_ID = 0,
7234            EVAL_OPINION_ID = 0,
7235            CERT_OPINION_LOG_ID = 0,
7236            EVAL_OPINION_LOG_ID = 0,
7237            UNMITIGATED_RISKS = 0,
7238            INEFFECTIVE_CONTROLS = 0,
7239            NUMBER_OF_SUB_PROCS_CERTIFIED = 0,
7240            TOTAL_NUMBER_OF_SUB_PROCS = 0,
7241            SUB_PROCS_CERTIFIED_PRCNT = 0,
7242            NUMBER_OF_ORG_PROCS_CERTIFIED = 0,
7243            TOTAL_NUMBER_OF_ORG_PROCS = 0,
7244            ORG_PROCS_CERTIFIED_PRCNT = 0,
7245            OPEN_FINDINGS = 0,
7246            risks_verified = 0,
7247            controls_verified = 0,
7248            total_number_of_risks = 0,
7249            total_number_of_ctrls = 0
7250      WHERE fin_certification_id = p_certification_id;
7251      ELSE
7252      	UPDATE  AMW_FIN_PROCESS_EVAL_SUM
7253         SET
7254            LAST_UPDATE_DATE = sysdate,
7255    	   last_updated_by = fnd_global.user_id,
7256    	   last_update_login = fnd_global.conc_login_id,
7257            CERT_OPINION_ID = 0,
7258            EVAL_OPINION_ID = 0,
7259            CERT_OPINION_LOG_ID = 0,
7260            EVAL_OPINION_LOG_ID = 0,
7261            UNMITIGATED_RISKS = 0,
7262            INEFFECTIVE_CONTROLS = 0,
7263            NUMBER_OF_SUB_PROCS_CERTIFIED = 0,
7264            TOTAL_NUMBER_OF_SUB_PROCS = 0,
7265            SUB_PROCS_CERTIFIED_PRCNT = 0,
7266            NUMBER_OF_ORG_PROCS_CERTIFIED = 0,
7267            TOTAL_NUMBER_OF_ORG_PROCS = 0,
7268            ORG_PROCS_CERTIFIED_PRCNT = 0,
7269            OPEN_FINDINGS = 0,
7270             risks_verified = 0,
7271            controls_verified = 0,
7272            total_number_of_risks = 0,
7273            total_number_of_ctrls = 0
7274       WHERE fin_certification_id IN
7275       (select
7276           certifcationb.CERTIFICATION_ID
7277        FROM    AMW_CERTIFICATION_B certifcationb
7278        where
7279            certifcationb.OBJECT_TYPE='FIN_STMT'
7280        and certifcationb.CERTIFICATION_STATUS in ('ACTIVE', 'DRAFT'));
7281 
7282     END IF;
7283 END reset_amw_fin_proc_eval_sum;
7284 
7285 
7286 
7287 PROCEDURE reset_amw_fin_org_eval_sum(p_certification_id in number)
7288 IS
7289 BEGIN
7290 SAVEPOINT reset_amw_fin_org_eval_sum;
7291 
7292 	IF p_certification_id is not null THEN
7293 	UPDATE
7294 	  AMW_FIN_ORG_EVAL_SUM
7295           SET
7296            LAST_UPDATE_DATE = sysdate,
7297    	   last_updated_by = fnd_global.user_id,
7298    	   last_update_login = fnd_global.conc_login_id,
7299            EVAL_OPINION_ID = 0,
7300            CERT_OPINION_LOG_ID = 0,
7301            CERT_OPINION_ID = 0,
7302            EVAL_OPINION_LOG_ID = 0,
7303            PROC_PENDING_CERTIFICATION = 0,
7304            TOTAL_NUMBER_OF_PROCS = 0,
7305            PROC_CERTIFIED_WITH_ISSUES = 0,
7306            PROC_VERIFIED = 0,
7307            PROC_WITH_INEFFECTIVE_CONTROLS = 0,
7308            UNMITIGATED_RISKS = 0,
7309            RISKS_VERIFIED = 0,
7310            INEFFECTIVE_CONTROLS = 0,
7311            CONTROLS_VERIFIED = 0,
7312            PROC_PENDING_CERT_PRCNT =0,
7313            PROCESSES_WITH_ISSUES_PRCNT =0,
7314            PROC_WITH_INEFF_CONTROLS_PRCNT = 0,
7315            UNMITIGATED_RISKS_PRCNT = 0,
7316            INEFFECTIVE_CONTROLS_PRCNT = 0,
7317            OPEN_FINDINGS = 0,
7318            total_number_of_risks = 0,
7319            total_number_of_ctrls = 0,
7320           proc_certified = 0
7321      WHERE fin_certification_id = p_certification_id;
7322      ELSE
7323        UPDATE  AMW_FIN_ORG_EVAL_SUM
7324           SET
7325           LAST_UPDATE_DATE = sysdate,
7326    	  last_updated_by = fnd_global.user_id,
7327    	  last_update_login = fnd_global.conc_login_id,
7328            EVAL_OPINION_ID = 0,
7329            CERT_OPINION_LOG_ID = 0,
7330            CERT_OPINION_ID = 0,
7331            EVAL_OPINION_LOG_ID = 0,
7332            PROC_PENDING_CERTIFICATION = 0,
7333            TOTAL_NUMBER_OF_PROCS = 0,
7334            PROC_CERTIFIED_WITH_ISSUES = 0,
7335            PROC_VERIFIED = 0,
7336            PROC_WITH_INEFFECTIVE_CONTROLS = 0,
7337            UNMITIGATED_RISKS = 0,
7338            RISKS_VERIFIED = 0,
7339            INEFFECTIVE_CONTROLS = 0,
7340            CONTROLS_VERIFIED = 0,
7341            PROC_PENDING_CERT_PRCNT =0,
7342            PROCESSES_WITH_ISSUES_PRCNT =0,
7343            PROC_WITH_INEFF_CONTROLS_PRCNT = 0,
7344            UNMITIGATED_RISKS_PRCNT = 0,
7345            INEFFECTIVE_CONTROLS_PRCNT = 0,
7346            OPEN_FINDINGS = 0,
7347            total_number_of_risks = 0,
7348           total_number_of_ctrls = 0,
7349           proc_certified = 0
7350       WHERE fin_certification_id IN
7351       (select
7352           certifcationb.CERTIFICATION_ID
7353        FROM    AMW_CERTIFICATION_B certifcationb
7354        where
7355            certifcationb.OBJECT_TYPE='FIN_STMT'
7356        and certifcationb.CERTIFICATION_STATUS in ('ACTIVE', 'DRAFT'));
7357 
7358       END IF;
7359 END reset_amw_fin_org_eval_sum;
7360 
7361 PROCEDURE reset_amw_cert_dashboard_sum(p_certification_id in number)
7362 IS
7363 BEGIN
7364 SAVEPOINT reset_amw_cert_dashboard_sum;
7365 
7366 	IF p_certification_id is not null THEN
7367 	UPDATE  AMW_CERT_DASHBOARD_SUM
7368           SET
7369           LAST_UPDATE_DATE = sysdate,
7370    	  last_updated_by = fnd_global.user_id,
7371    	  last_update_login = fnd_global.conc_login_id,
7372            NEW_RISKS_ADDED = 0,
7373            NEW_CONTROLS_ADDED = 0,
7374            PROCESSES_NOT_CERT = 0,
7375            PROCESSES_CERT_ISSUES = 0,
7376            ORG_PROCESS_NOT_CERT = 0,
7377            ORG_PROCESS_CERT_ISSUES = 0,
7378            PROC_INEFF_CONTROL = 0,
7379            ORG_PROC_INEFF_CONTROL = 0,
7380            UNMITIGATED_RISKS = 0,
7381            INEFFECTIVE_CONTROLS = 0,
7382            ORGS_IN_SCOPE = 0,
7383            ORGS_PENDING_IN_SCOPE = 0,
7384            ORGS_PENDING_CERTIFICATION = 0
7385      WHERE certification_id = p_certification_id;
7386      ELSE
7387      	UPDATE  AMW_CERT_DASHBOARD_SUM
7388           SET
7389            LAST_UPDATE_DATE = sysdate,
7390            last_updated_by = fnd_global.user_id,
7391            last_update_login = fnd_global.conc_login_id,
7392            NEW_RISKS_ADDED = 0,
7393            NEW_CONTROLS_ADDED = 0,
7394            PROCESSES_NOT_CERT = 0,
7395            PROCESSES_CERT_ISSUES = 0,
7396            ORG_PROCESS_NOT_CERT = 0,
7397            ORG_PROCESS_CERT_ISSUES = 0,
7398            PROC_INEFF_CONTROL = 0,
7399            ORG_PROC_INEFF_CONTROL = 0,
7400            UNMITIGATED_RISKS = 0,
7401            INEFFECTIVE_CONTROLS = 0,
7402            ORGS_IN_SCOPE = 0,
7403            ORGS_PENDING_IN_SCOPE = 0,
7404            ORGS_PENDING_CERTIFICATION = 0
7405 	WHERE certification_id IN
7406       		(select certifcationb.CERTIFICATION_ID
7407        		FROM    AMW_CERTIFICATION_B certifcationb
7408        		where certifcationb.OBJECT_TYPE='FIN_STMT'
7409        		and certifcationb.CERTIFICATION_STATUS in ('ACTIVE', 'DRAFT'));
7410        	END IF;
7411 
7412 END reset_amw_cert_dashboard_sum;
7413 
7414 PROCEDURE reset_fin_all(p_certification_id in number)
7415 IS
7416 	l_certification_id number ;
7417 BEGIN
7418 	SAVEPOINT reset_fin_all;
7419 
7420 	l_certification_id := p_certification_id;
7421 
7422 	reset_amw_fin_cert_eval_sum(l_certification_id);
7423 	reset_amw_fin_proc_eval_sum(l_certification_id);
7424 	reset_amw_fin_org_eval_sum(l_certification_id);
7425 	reset_amw_cert_dashboard_sum(l_certification_id);
7426 
7427 END reset_fin_all;
7428 
7429 --******************************************************************************************************
7430 /* ************************* Code to be executed for updating 4 financial certification summary tables
7431 *************************** when business evevnt is rised on opinion changes
7432 --******************************************************************************************************/
7433 PROCEDURE RISK_EVALUATION_HANDLER(
7434 p_api_version_number        IN   NUMBER   := 1.0,
7435 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
7436 p_commit                    IN   VARCHAR2 := FND_API.g_false,
7437 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
7438 p_risk_id 	                 number,
7439  p_org_id number,
7440  p_process_id number,
7441  p_opinion_log_id number,
7442  x_return_status             OUT  nocopy VARCHAR2,
7443 x_msg_count                 OUT  nocopy NUMBER,
7444 x_msg_data                  OUT  nocopy VARCHAR2)
7445 IS
7446 
7447 ---CURSOR TO GET ALL OF FINANCIAL ITEMS WHICH RELATE TO THIS RISK
7448 CURSOR Get_all_items(l_risk_id number, l_org_id number, l_process_id number)IS
7449 SELECT DISTINCT item.FIN_CERTIFICATION_ID, item.STATEMENT_GROUP_ID, item.FINANCIAL_STATEMENT_ID, item.FINANCIAL_ITEM_ID
7450 FROM AMW_FIN_ITEM_ACC_RISK ITEM,
7451      AMW_CERTIFICATION_B cert
7452 WHERE ITEM.RISK_ID = l_risk_id
7453 AND  ITEM.ORGANIZATION_ID = l_org_id
7454 AND  ITEM.PROCESS_ID = l_process_id
7455 AND ITEM.FINANCIAL_ITEM_ID IS NOT NULL
7456 AND  item.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
7457 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
7458 
7459 --CURSOR TO GET ALL OF ACCOUNTS WHICH RELATE TO THIS RISK
7460 CURSOR 	Get_all_accts (l_risk_id number, l_org_id number, l_process_id number)IS
7461 SELECT DISTINCT finitemAcc.fin_certification_id, finitemAcc.account_group_id, finitemAcc.natural_account_id
7462 FROM AMW_FIN_ITEM_ACC_RISK finitemAcc,
7463      AMW_CERTIFICATION_B cert
7464 WHERE finitemAcc.RISK_ID = l_risk_id
7465 AND   finitemAcc.ORGANIZATION_ID = l_org_id
7466 AND   finitemAcc.PROCESS_ID = l_process_id
7467 AND finitemAcc.natural_account_id is not null
7468 AND  finitemAcc.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
7469 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
7470 
7471 --CURSOR TO GET ALL OF FINANCIAL CERTIFICATION WHICH HAS PROCESSES THAT RELATES TO THIS RISK
7472 CURSOR Get_all_fin_cert(l_org_id number, l_process_id number) IS
7473 SELECT DISTINCT proc.FIN_CERTIFICATION_ID
7474 FROM AMW_FIN_PROCESS_EVAL_SUM proc,
7475      AMW_CERTIFICATION_B cert
7476 WHERE proc.organization_id = l_org_id
7477 AND proc.PROCESS_ID = l_process_id
7478 AND  proc.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
7479 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
7480 
7481 --CURSOR TO GET OLD EVAL OPINION ID
7482 CURSOR Get_old_opinion_id(l_cert_id number, l_org_id number, l_process_id number) IS
7483 SELECT PK4
7484 FROM AMW_RISK_ASSOCIATIONS
7485 WHERE OBJECT_TYPE = 'PROCESS_FINCERT'
7486 AND PK1 = l_cert_id
7487 AND PK2 = l_org_id
7488 AND PK3 = l_process_id
7489 AND RISK_ID = p_risk_id;
7490 
7491 
7492  CURSOR Get_Dashboard_Risk(l_cert_id NUMBER) IS
7493         SELECT UNMITIGATED_RISKS FROM AMW_CERT_DASHBOARD_SUM
7494           WHERE CERTIFICATION_ID = l_cert_id;
7495 
7496 CURSOR Get_Process_Risk(l_cert_id NUMBER, l_org_id NUMBER, l_process_id NUMBER) IS
7497         SELECT UNMITIGATED_RISKS, RISKS_VERIFIED, TOTAL_NUMBER_OF_RISKS
7498         FROM AMW_FIN_PROCESS_EVAL_SUM
7499           WHERE FIN_CERTIFICATION_ID = l_cert_id
7500           AND ORGANIZATION_ID = l_org_id
7501           AND PROCESS_ID = l_process_id;
7502 
7503 CURSOR Get_Org_Risk(l_cert_id NUMBER, l_org_id NUMBER) IS
7504         SELECT UNMITIGATED_RISKS, RISKS_VERIFIED, TOTAL_NUMBER_OF_RISKS
7505         FROM  AMW_FIN_ORG_EVAL_SUM
7506           WHERE FIN_CERTIFICATION_ID = l_cert_id
7507           AND ORGANIZATION_ID = l_org_id;
7508 
7509 CURSOR Get_Item_Risk(l_cert_id NUMBER, l_stmt_id NUMBER, l_item_id NUMBER) IS
7510        SELECT UNMITIGATED_RISKS, RISKS_VERIFIED,  TOTAL_NUMBER_OF_RISKS
7511          FROM  AMW_FIN_CERT_EVAL_SUM
7512          WHERE FIN_CERTIFICATION_ID = l_cert_id
7513     AND FINANCIAL_STATEMENT_ID = l_stmt_id
7514     AND FINANCIAL_ITEM_ID = l_item_id
7515     AND OBJECT_TYPE = 'FINANCIAL ITEM';
7516 
7517 CURSOR Get_Acct_Risk(l_cert_id NUMBER, l_acct_group_id NUMBER, l_acct_id NUMBER) IS
7518          SELECT UNMITIGATED_RISKS, RISKS_VERIFIED, TOTAL_NUMBER_OF_RISKS
7519          FROM  AMW_FIN_CERT_EVAL_SUM
7520        WHERE FIN_CERTIFICATION_ID = l_cert_id
7521        AND ACCOUNT_GROUP_ID = l_acct_group_id
7522        AND NATURAL_ACCOUNT_ID = l_acct_id
7523       AND OBJECT_TYPE = 'ACCOUNT';
7524 
7525 M_dashboard_unmitigated_risks          number;
7526 
7527 M_item_unmitigated_risks          number;
7528 M_item_risks_verified             number;
7529 M_item_risks_total	number;
7530 
7531 M_acc_unmitigated_risks          number ;
7532 M_acc_risks_verified             number ;
7533 M_acc_risks_total            number ;
7534 
7535 M_org_unmitigated_risks          number ;
7536 M_org_risks_verified             number ;
7537 M_org_risks_total            number ;
7538 
7539 M_gen_unmitigated_risks          number ;
7540 
7541 
7542 M_proc_unmitigated_risks          number ;
7543 M_proc_risks_verified             number ;
7544 M_proc_risks_total	number;
7545 
7546 
7547 M_opinion_log_id AMW_OPINIONS_LOG.OPINION_LOG_ID%TYPE;
7548 
7549 M_change_flag VARCHAR2(1);
7550 M_new_flag VARCHAR2(1) := 'N';
7551 
7552 l_api_name           CONSTANT VARCHAR2(30) := 'RISK_EVALUATION_HANDLER';
7553 l_api_version_number CONSTANT NUMBER       := 1.0;
7554 
7555 l_return_status VARCHAR2(32767);
7556 l_msg_count NUMBER;
7557 l_msg_data VARCHAR2(32767);
7558 
7559 BEGIN
7560 
7561 SAVEPOINT RISK_EVALUATION_HANDLER;
7562 
7563  -- Standard call to check for call compatibility.
7564         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
7565                                              p_api_version_number,
7566                                              l_api_name,
7567                                              G_PKG_NAME)
7568         THEN
7569                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7570         END IF;
7571 
7572         -- Initialize message list if p_init_msg_list is set to TRUE.
7573         IF FND_API.to_Boolean( p_init_msg_list )
7574         THEN
7575                 FND_MSG_PUB.initialize;
7576         END IF;
7577 
7578         -- Initialize API return status to SUCCESS
7579         x_return_status := FND_API.G_RET_STS_SUCCESS;
7580 
7581 
7582 
7583 FOR Get_all_items_Rec in Get_all_items(p_risk_id, p_org_id , p_process_id) LOOP
7584 	 exit when Get_all_items %notfound;
7585 
7586     OPEN Get_old_opinion_id(Get_all_items_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
7587     FETCH Get_old_opinion_id INTO M_opinion_log_id;
7588     CLOSE Get_old_opinion_id;
7589 
7590  M_item_unmitigated_risks := 0;
7591  M_item_risks_verified := 0;
7592  M_item_risks_total := 0;
7593 
7594    OPEN Get_Item_Risk(Get_all_items_Rec.FIN_CERTIFICATION_ID, Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
7595    Get_all_items_Rec.FINANCIAL_ITEM_ID);
7596    FETCH Get_Item_Risk  INTO M_item_unmitigated_risks, M_item_risks_verified, M_item_risks_total;
7597    CLOSE Get_Item_Risk;
7598 
7599 	 Is_Eval_Change(
7600     		old_opinion_log_id  => M_opinion_log_id,
7601     		new_opinion_log_id  =>  P_opinion_log_id,
7602     		x_change_flag	=> M_change_flag);
7603 
7604     IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  -- a new evaluation
7605     M_new_flag := 'Y';
7606 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7607 *********IF(M_item_risks_verified + 1 > M_item_risks_total)                  ****/
7608  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7609 IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_item_risks_verified + 1 <  M_item_risks_total)  )  THEN
7610   	g_refresh_flag := 'Y';
7611   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
7612 ELSE
7613 ********/
7614 UPDATE AMW_FIN_CERT_EVAL_SUM
7615         SET
7616         last_update_date = sysdate,
7617         last_updated_by = fnd_global.user_id,
7618         last_update_login = fnd_global.conc_login_id,
7619         risks_verified = least(risks_verified + 1, total_number_of_risks)
7620         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
7621         AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
7622         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
7623         AND OBJECT_TYPE = 'FINANCIAL ITEM';
7624 --END IF;   -- end of  if not
7625    END IF;  --enf of a new evaluation
7626 
7627  --- M_change_flag = 'F' and M_new_flag = 'N'  means the Opinion is changed from Ineffective to Efective
7628  --- M_change_flag = 'B' means the Opinion is changed from Efective to Ineffective
7629 
7630         IF (M_change_flag = 'F'  and M_new_flag = 'N') THEN
7631 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7632 *********(IF(M_item_unmitigated_risks -1  < 0) 		 *******************/
7633  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7634 IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_item_unmitigated_risks -1  >  0)   )  THEN
7635   	g_refresh_flag := 'Y';
7636   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
7637 ELSE
7638 *************/
7639 UPDATE AMW_FIN_CERT_EVAL_SUM
7640         SET
7641         last_update_date = sysdate,
7642         last_updated_by = fnd_global.user_id,
7643         last_update_login = fnd_global.conc_login_id,
7644         unmitigated_risks = greatest(0, unmitigated_risks -1),
7645         unmitigated_risks_prcnt = round( (greatest(0, m_item_unmitigated_risks -1))/decode(nvl(m_item_risks_total, 0), 0, 1, m_item_risks_total), 2)*100
7646         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
7647 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
7648         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
7649         AND OBJECT_TYPE = 'FINANCIAL ITEM';
7650 --END IF;
7651 
7652         ELSIF (M_change_flag = 'B') THEN
7653 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7654 ***********IF(M_item_unmitigated_risks + 1 > M_item_risks_verified)  ********************/
7655  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7656  IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_item_unmitigated_risks + 1 < M_item_risks_verified)   )  THEN
7657   	g_refresh_flag := 'Y';
7658   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
7659 ELSE
7660 ***********/
7661 UPDATE AMW_FIN_CERT_EVAL_SUM
7662         SET
7663         last_update_date = sysdate,
7664         last_updated_by = fnd_global.user_id,
7665         last_update_login = fnd_global.conc_login_id,
7666         unmitigated_risks = least(unmitigated_risks + 1,  risks_verified),
7667         unmitigated_risks_prcnt = round((least(m_item_unmitigated_risks + 1, risks_verified))/decode(nvl(m_item_risks_total, 0), 0, 1, m_item_risks_total), 2)*100
7668         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
7669 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
7670         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
7671         AND OBJECT_TYPE = 'FINANCIAL ITEM';
7672 --END IF;
7673 
7674         END IF;
7675 
7676         UPDATE AMW_FIN_ITEM_ACC_RISK
7677         SET
7678         LAST_UPDATE_DATE = sysdate,
7679    	last_updated_by = fnd_global.user_id,
7680    	last_update_login = fnd_global.conc_login_id,
7681    	OPINION_LOG_ID = P_opinion_log_id
7682          WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
7683 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
7684         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
7685         AND OBJECT_TYPE = 'FINANCIAL ITEM';
7686 
7687 
7688   END LOOP;
7689 
7690  FOR Get_all_accts_Rec IN Get_all_accts(p_risk_id, p_org_id , p_process_id) LOOP
7691            exit when Get_all_accts%notfound;
7692 
7693     OPEN Get_old_opinion_id(Get_all_accts_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
7694     FETCH Get_old_opinion_id INTO M_opinion_log_id;
7695     CLOSE Get_old_opinion_id;
7696 
7697 
7698 	  Is_Eval_Change(
7699     		old_opinion_log_id  => M_opinion_log_id,
7700     		new_opinion_log_id  =>  P_opinion_log_id,
7701     		x_change_flag	=> M_change_flag);
7702 
7703 
7704    OPEN Get_Acct_Risk(Get_all_accts_Rec.FIN_CERTIFICATION_ID, Get_all_accts_Rec.ACCOUNT_GROUP_ID,
7705    Get_all_accts_Rec.NATURAL_ACCOUNT_ID);
7706    FETCH Get_Acct_Risk  INTO M_acc_unmitigated_risks, M_acc_risks_verified, M_acc_risks_total;
7707    CLOSE Get_Acct_Risk;
7708 
7709    IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  -- a new evaluation
7710     M_new_flag := 'Y';
7711 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7712 **********IF(M_acc_risks_verified + 1 > M_acc_risks_total) ***********************/
7713  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7714   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR (M_acc_risks_verified + 1 < M_acc_risks_total)    )  THEN
7715   	g_refresh_flag := 'Y';
7716   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
7717 ELSE
7718 *************/
7719 UPDATE AMW_FIN_CERT_EVAL_SUM
7720         SET
7721         last_update_date = sysdate,
7722         last_updated_by = fnd_global.user_id,
7723         last_update_login = fnd_global.conc_login_id,
7724         risks_verified = least(risks_verified + 1, total_number_of_risks)
7725        WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
7726 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
7727 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
7728         AND OBJECT_TYPE = 'ACCOUNT';
7729 --END IF;
7730    END IF;
7731 
7732         IF(M_change_flag = 'F'  and M_new_flag = 'N') THEN
7733 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7734 **********    IF(M_acc_unmitigated_risks - 1 < 0 ) *******************************/
7735    /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7736   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_acc_unmitigated_risks - 1 >  0 )     )  THEN
7737   	g_refresh_flag := 'Y';
7738   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
7739 ELSE
7740 **********/
7741  UPDATE AMW_FIN_CERT_EVAL_SUM
7742         SET
7743         last_update_date = sysdate,
7744         last_updated_by = fnd_global.user_id,
7745         last_update_login = fnd_global.conc_login_id,
7746         unmitigated_risks = greatest(0, unmitigated_risks -1) ,
7747         unmitigated_risks_prcnt = round((greatest(0, m_acc_unmitigated_risks - 1))/ decode(nvl(m_acc_risks_total,0), 0,1, m_acc_risks_total), 2) * 100
7748         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
7749 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
7750 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
7751         AND OBJECT_TYPE = 'ACCOUNT';
7752 --END IF;
7753 
7754 
7755         ELSIF (M_change_flag = 'B') THEN
7756 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7757 **********    IF( M_acc_unmitigated_risks + 1 > M_acc_risks_verified )  *******************************/
7758  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7759   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR ( M_acc_unmitigated_risks + 1 < M_acc_risks_verified )      )  THEN
7760   	g_refresh_flag := 'Y';
7761   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
7762 ELSE
7763 ***************/
7764 UPDATE AMW_FIN_CERT_EVAL_SUM
7765         SET
7766         last_update_date = sysdate,
7767    	last_updated_by = fnd_global.user_id,
7768    	last_update_login = fnd_global.conc_login_id,
7769         unmitigated_risks = least(unmitigated_risks + 1 ,risks_verified),
7770         unmitigated_risks_prcnt = round((least(risks_verified, m_acc_unmitigated_risks + 1))/ decode(nvl(m_acc_risks_total,0), 0,1, m_acc_risks_total), 2) * 100
7771         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
7772 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
7773 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
7774         AND OBJECT_TYPE = 'ACCOUNT';
7775 --END IF;
7776         END IF;
7777 
7778         UPDATE AMW_FIN_ITEM_ACC_RISK
7779         SET
7780         LAST_UPDATE_DATE = sysdate,
7781    	last_updated_by = fnd_global.user_id,
7782    	last_update_login = fnd_global.conc_login_id,
7783    	OPINION_LOG_ID = P_opinion_log_id
7784          WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
7785 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
7786 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
7787         AND OBJECT_TYPE = 'ACCOUNT';
7788 
7789 
7790  END LOOP;
7791 
7792 
7793  ---modify the org, dashboard and process eval summary tables
7794  FOR Get_all_fin_cert_Rec IN Get_all_fin_cert(p_org_id , p_process_id) LOOP
7795            exit when Get_all_fin_cert%notfound;
7796 
7797     OPEN Get_old_opinion_id(Get_all_fin_cert_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
7798     FETCH Get_old_opinion_id INTO M_opinion_log_id;
7799     CLOSE Get_old_opinion_id;
7800 
7801     OPEN Get_Dashboard_Risk(Get_all_fin_cert_Rec.fin_certification_id);
7802        FETCH Get_Dashboard_Risk INTO M_dashboard_unmitigated_risks;
7803        CLOSE Get_Dashboard_Risk;
7804 
7805       OPEN Get_Process_Risk(Get_all_fin_cert_Rec.fin_certification_id, p_org_id, p_process_id);
7806        FETCH Get_Process_Risk INTO M_proc_unmitigated_risks, M_proc_risks_verified, M_proc_risks_total;
7807        CLOSE Get_Process_Risk;
7808 
7809       OPEN Get_Org_Risk(Get_all_fin_cert_Rec.fin_certification_id, p_org_id);
7810        FETCH Get_Org_Risk INTO M_org_unmitigated_risks, M_org_risks_verified, M_org_risks_total;
7811        CLOSE Get_Org_Risk;
7812 
7813 Is_Eval_Change(
7814     		old_opinion_log_id  => M_opinion_log_id,
7815     		new_opinion_log_id  =>  P_opinion_log_id,
7816     		x_change_flag	=> M_change_flag);
7817 
7818 
7819      IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  -- a new evaluation
7820      M_new_flag := 'Y';
7821 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7822 **********   IF(M_proc_risks_verified + 1 > M_proc_risks_total) *******************************/
7823  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7824   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR (M_proc_risks_verified + 1 < M_proc_risks_total)   )  THEN
7825   	g_refresh_flag := 'Y';
7826   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
7827 ELSE
7828 ************/
7829 UPDATE AMW_FIN_PROCESS_EVAL_SUM
7830         SET
7831         last_update_date = sysdate,
7832         last_updated_by = fnd_global.user_id,
7833         last_update_login = fnd_global.conc_login_id,
7834         risks_verified = least(risks_verified + 1,  total_number_of_risks)
7835         WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
7836   	AND ORGANIZATION_ID = p_org_id
7837   	AND PROCESS_ID = p_process_id;
7838 -- END IF;
7839 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7840 **********IF(M_org_risks_verified + 1 > M_org_risks_total) *******************************/
7841  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7842   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR(M_org_risks_verified + 1 <  M_org_risks_total)    )  THEN
7843   	g_refresh_flag := 'Y';
7844   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
7845 ELSE
7846 *********/
7847 UPDATE AMW_FIN_ORG_EVAL_SUM
7848         SET
7849         last_update_date = sysdate,
7850         last_updated_by = fnd_global.user_id,
7851         last_update_login = fnd_global.conc_login_id,
7852         risks_verified =  least(risks_verified + 1,  total_number_of_risks)
7853       WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
7854         	AND ORGANIZATION_ID = p_org_id;
7855 --END IF;
7856    END IF;  -- end of a new evaluation
7857 
7858    IF(M_change_flag = 'F' and M_new_flag = 'N' ) THEN
7859 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7860 **********IF( M_dashboard_unmitigated_risks -1 < 0 ) *******************************/
7861  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7862   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( M_dashboard_unmitigated_risks -1 > 0 )  )  THEN
7863   	g_refresh_flag := 'Y';
7864   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
7865 ELSE
7866 *************/
7867 UPDATE AMW_CERT_DASHBOARD_SUM
7868  	SET
7869  	LAST_UPDATE_DATE = sysdate,
7870    	last_updated_by = fnd_global.user_id,
7871    	last_update_login = fnd_global.conc_login_id,
7872  	unmitigated_risks = greatest(0, unmitigated_risks -1 )
7873  	WHERE CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id;
7874 --END IF;
7875 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7876 **********        IF(M_proc_unmitigated_risks -1 < 0 )    *******************************/
7877  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7878   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR(M_proc_unmitigated_risks -1 > 0 )  )  THEN
7879   	g_refresh_flag := 'Y';
7880   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
7881 ELSE
7882 ************/
7883 UPDATE AMW_FIN_PROCESS_EVAL_SUM
7884  	SET
7885  	LAST_UPDATE_DATE = sysdate,
7886    	last_updated_by = fnd_global.user_id,
7887    	last_update_login = fnd_global.conc_login_id,
7888  	UNMITIGATED_RISKS = UNMITIGATED_RISKS - 1
7889   	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
7890   	AND ORGANIZATION_ID = p_org_id
7891   	AND PROCESS_ID = p_process_id;
7892 --END IF;
7893 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7894 **********        IF(M_org_unmitigated_risks -1 < 0 )   *******************************/
7895  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7896   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR(M_org_unmitigated_risks -1 > 0 )   )  THEN
7897   	g_refresh_flag := 'Y';
7898   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
7899 ELSE
7900 *********/
7901 UPDATE AMW_FIN_ORG_EVAL_SUM
7902  	SET
7903  	last_update_date = sysdate,
7904    	last_updated_by = fnd_global.user_id,
7905    	last_update_login = fnd_global.conc_login_id,
7906  	unmitigated_risks = greatest(0, unmitigated_risks -1),
7907  	unmitigated_risks_prcnt = round((greatest(0, m_org_unmitigated_risks - 1))/decode(nvl(m_org_risks_total, 0), 0, 1, m_org_risks_total), 2)*100
7908  	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
7909         	AND ORGANIZATION_ID = p_org_id;
7910 -- END IF;
7911    ELSIF(M_change_flag = 'B') THEN
7912     	UPDATE AMW_CERT_DASHBOARD_SUM
7913     	SET
7914     	LAST_UPDATE_DATE = sysdate,
7915    	last_updated_by = fnd_global.user_id,
7916    	last_update_login = fnd_global.conc_login_id,
7917     	UNMITIGATED_RISKS = UNMITIGATED_RISKS + 1
7918  	WHERE CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id;
7919 
7920 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7921 **********        IF(M_proc_unmitigated_risks + 1  >  M_proc_risks_verified ) *******************************/
7922  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7923   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR(M_proc_unmitigated_risks + 1  <   M_proc_risks_verified )  )  THEN
7924   	g_refresh_flag := 'Y';
7925   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
7926 ELSE
7927 ************/
7928 UPDATE AMW_FIN_PROCESS_EVAL_SUM
7929  	SET
7930  	last_update_date = sysdate,
7931    	last_updated_by = fnd_global.user_id,
7932    	last_update_login = fnd_global.conc_login_id,
7933  	unmitigated_risks = least(unmitigated_risks +1,  risks_verified)
7934   	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
7935   	AND ORGANIZATION_ID = p_org_id
7936   	AND PROCESS_ID = p_process_id;
7937 --END IF;
7938 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
7939 **********        IF(M_org_unmitigated_risks + 1  >  M_org_risks_verified )  *******************************/
7940  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
7941 IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR(M_org_unmitigated_risks + 1  <  M_org_risks_verified )   )  THEN
7942   	g_refresh_flag := 'Y';
7943   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
7944 ELSE
7945 ***************/
7946 UPDATE AMW_FIN_ORG_EVAL_SUM
7947   	SET
7948   	last_update_date = sysdate,
7949    	last_updated_by = fnd_global.user_id,
7950    	last_update_login = fnd_global.conc_login_id,
7951   	unmitigated_risks = least(unmitigated_risks + 1,risks_verified),
7952   	unmitigated_risks_prcnt = round((least(m_org_unmitigated_risks + 1, risks_verified))/decode(nvl(m_org_risks_total, 0), 0, 1, m_org_risks_total), 2)*100
7953  	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
7954         AND ORGANIZATION_ID = p_org_id;
7955 --END IF;
7956   END IF;
7957 
7958 END LOOP;
7959 
7960 
7961 x_return_status := FND_API.G_RET_STS_SUCCESS;
7962 
7963 EXCEPTION
7964      WHEN NO_DATA_FOUND THEN
7965      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
7966      x_return_status := FND_API.G_RET_STS_ERROR;
7967      x_msg_count := 1;
7968      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
7969       WHEN OTHERS THEN
7970        ROLLBACK TO RISK_EVALUATION_HANDLER;
7971       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
7972       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
7973         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7974         x_msg_count := 1;
7975         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
7976                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
7977                 FND_MSG_PUB.Count_And_Get(
7978                 p_encoded =>  FND_API.G_FALSE,
7979                 p_count   =>  x_msg_count,
7980                 p_data    =>  x_msg_data);
7981                 RETURN;
7982 
7983 END RISK_EVALUATION_HANDLER;
7984 
7985 PROCEDURE CONTROL_EVALUATION_HANDLER(
7986 p_api_version_number        IN   NUMBER   := 1.0,
7987 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
7988 p_commit                    IN   VARCHAR2 := FND_API.g_false,
7989 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
7990 p_ctrl_id 		IN 	NUMBER,
7991 p_org_id 		IN 	NUMBER,
7992 p_opinion_log_id 	IN	NUMBER,
7993 x_return_status             OUT  nocopy VARCHAR2,
7994 x_msg_count                 OUT  nocopy NUMBER,
7995 x_msg_data                  OUT  nocopy VARCHAR2
7996 )
7997 IS
7998 
7999 ---CURSOR TO GET ALL OF FINANCIAL ITEMS WHICH RELATE TO THIS CONTROL
8000 CURSOR Get_all_items(l_control_id number, l_org_id number)IS
8001 SELECT DISTINCT item.FIN_CERTIFICATION_ID, item.STATEMENT_GROUP_ID, item.FINANCIAL_STATEMENT_ID, item.FINANCIAL_ITEM_ID
8002 FROM AMW_FIN_ITEM_ACC_CTRL ITEM,
8003      AMW_CERTIFICATION_B cert
8004 WHERE ITEM.CONTROL_ID = l_control_id
8005 AND  ITEM.ORGANIZATION_ID = l_org_id
8006 AND  ITEM.FINANCIAL_ITEM_ID IS NOT NULL
8007 AND  item.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
8008 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
8009 
8010 --CURSOR TO GET ALL OF ACCOUNTS WHICH RELATE TO THIS RISK
8011 CURSOR 	Get_all_accts (l_control_id number, l_org_id number)IS
8012 SELECT DISTINCT finitemAcc.fin_certification_id, finitemAcc.account_group_id, finitemAcc.natural_account_id
8013 FROM AMW_FIN_ITEM_ACC_CTRL finitemAcc,
8014      AMW_CERTIFICATION_B cert
8015 WHERE finitemAcc.CONTROL_ID = l_control_id
8016 AND   finitemAcc.ORGANIZATION_ID = l_org_id
8017 AND finitemAcc.natural_account_id is not null
8018 AND  finitemAcc.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
8019 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
8020 
8021 --CURSOR TO GET ALL OF FINANCIAL CERTIFICATION WHICH HAS PROCESSES THAT RELATES TO THIS CONTROL
8022 CURSOR Get_all_fin_cert(l_org_id number) IS
8023 SELECT DISTINCT proc.FIN_CERTIFICATION_ID
8024 FROM AMW_FIN_PROCESS_EVAL_SUM proc,
8025      AMW_CERTIFICATION_B cert
8026 WHERE proc.organization_id = l_org_id
8027 AND  proc.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
8028 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
8029 
8030 --CURSOR TO GET OLD EVAL OPINION ID
8031 CURSOR Get_old_opinion_id(l_cert_id number, l_org_id number) IS
8032 SELECT PK5
8033 FROM AMW_CONTROL_ASSOCIATIONS
8034 WHERE OBJECT_TYPE = 'RISK_FINCERT'
8035 AND PK1 = l_cert_id
8036 AND PK2 = l_org_id
8037 AND CONTROL_ID = p_ctrl_id;
8038 
8039 --Cursor to get old values
8040    CURSOR Get_Item_Ctrl(l_cert_id NUMBER, l_stmt_id NUMBER, l_item_id NUMBER) IS
8041        SELECT INEFFECTIVE_CONTROLS, CONTROLS_VERIFIED, TOTAL_NUMBER_OF_CTRLS
8042          FROM  AMW_FIN_CERT_EVAL_SUM
8043          WHERE FIN_CERTIFICATION_ID = l_cert_id
8044     AND FINANCIAL_STATEMENT_ID = l_stmt_id
8045     AND FINANCIAL_ITEM_ID = l_item_id
8046     AND OBJECT_TYPE = 'FINANCIAL ITEM';
8047 
8048 
8049 
8050     CURSOR Get_Acct_Ctrl(l_cert_id NUMBER, l_acct_group_id NUMBER, l_acct_id NUMBER) IS
8051      SELECT INEFFECTIVE_CONTROLS, CONTROLS_VERIFIED, TOTAL_NUMBER_OF_CTRLS
8052       FROM  AMW_FIN_CERT_EVAL_SUM
8053          WHERE FIN_CERTIFICATION_ID = l_cert_id
8054           AND ACCOUNT_GROUP_ID = l_acct_group_id
8055        AND NATURAL_ACCOUNT_ID = l_acct_id
8056       AND OBJECT_TYPE = 'ACCOUNT';
8057 
8058    CURSOR Get_Dashboard_Ctrl(l_cert_id NUMBER) IS
8059         SELECT INEFFECTIVE_CONTROLS FROM AMW_CERT_DASHBOARD_SUM
8060           WHERE CERTIFICATION_ID = l_cert_id;
8061 
8062  CURSOR Get_Org_Ctrl(l_cert_id NUMBER, l_org_id NUMBER) IS
8063         SELECT  INEFFECTIVE_CONTROLS, CONTROLS_VERIFIED, TOTAL_NUMBER_OF_CTRLS
8064         FROM  AMW_FIN_ORG_EVAL_SUM
8065           WHERE FIN_CERTIFICATION_ID = l_cert_id
8066           AND ORGANIZATION_ID = l_org_id;
8067 
8068 CURSOR Get_Proc_Ctrl(l_cert_id NUMBER, l_org_id NUMBER) IS
8069         SELECT INEFFECTIVE_CONTROLS, CONTROLS_VERIFIED, TOTAL_NUMBER_OF_CTRLS
8070         FROM AMW_FIN_PROCESS_EVAL_SUM
8071           WHERE FIN_CERTIFICATION_ID = l_cert_id
8072           AND ORGANIZATION_ID = l_org_id;
8073 
8074 M_item_ctrls_verified  number;
8075 M_item_ineff_ctrls number;
8076 M_item_ctrls_total number;
8077 
8078 M_acc_ineff_ctrls  number;
8079 M_acc_ctrls_verified number;
8080 M_acc_ctrls_total number;
8081 
8082 M_org_ineff_ctrls number;
8083 M_org_ctrls_verified number;
8084 M_org_ctrls_total number;
8085 
8086 M_proc_ineff_ctrls number;
8087 M_proc_ctrls_verified number;
8088 M_proc_ctrls_total number;
8089 
8090 M_dashboard_ineff_ctrls number;
8091 
8092 
8093 M_opinion_log_id AMW_OPINIONS_LOG.OPINION_LOG_ID%TYPE;
8094 
8095 /*****************************************
8096 M_new_flag='Y', it's a new evaluation.
8097 M_change_flag='F', it's from ineffective to effective.
8098 M_change_flag='B', it's from effective to ineffective
8099 ******************************************/
8100 
8101 M_change_flag VARCHAR2(1);
8102 M_new_flag VARCHAR2(1) := 'N';
8103 
8104 l_api_name           CONSTANT VARCHAR2(30) := 'CONTROL_EVALUATION_HANDLER';
8105 l_api_version_number CONSTANT NUMBER       := 1.0;
8106 
8107 l_return_status VARCHAR2(32767);
8108 l_msg_count NUMBER;
8109 l_msg_data VARCHAR2(32767);
8110 
8111 BEGIN
8112 
8113 SAVEPOINT CONTROL_EVALUATION_HANDLER;
8114 
8115  -- Standard call to check for call compatibility.
8116         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
8117                                              p_api_version_number,
8118                                              l_api_name,
8119                                              G_PKG_NAME)
8120         THEN
8121                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8122         END IF;
8123 
8124         -- Initialize message list if p_init_msg_list is set to TRUE.
8125         IF FND_API.to_Boolean( p_init_msg_list )
8126         THEN
8127                 FND_MSG_PUB.initialize;
8128         END IF;
8129 
8130         -- Initialize API return status to SUCCESS
8131         x_return_status := FND_API.G_RET_STS_SUCCESS;
8132 
8133 
8134 
8135 FOR Get_all_items_Rec in Get_all_items(p_ctrl_id, p_org_id) LOOP
8136 	 exit when Get_all_items %notfound;
8137 
8138     OPEN Get_old_opinion_id(Get_all_items_Rec.FIN_CERTIFICATION_ID, p_org_id);
8139     FETCH Get_old_opinion_id INTO M_opinion_log_id;
8140     CLOSE Get_old_opinion_id;
8141 
8142    M_item_ineff_ctrls := 0;
8143    M_item_ctrls_verified := 0;
8144    M_item_ctrls_total := 0;
8145 
8146    OPEN Get_Item_Ctrl(Get_all_items_Rec.FIN_CERTIFICATION_ID, Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
8147    Get_all_items_Rec.FINANCIAL_ITEM_ID);
8148    FETCH Get_Item_Ctrl  INTO M_item_ineff_ctrls, M_item_ctrls_verified, M_item_ctrls_total;
8149    CLOSE Get_Item_Ctrl;
8150 
8151     Is_Eval_Change(
8152     		old_opinion_log_id  => M_opinion_log_id,
8153     		new_opinion_log_id  =>  P_opinion_log_id,
8154     		x_change_flag	=> M_change_flag);
8155 
8156 
8157 IF ((M_opinion_log_id IS NULL) OR (M_opinion_log_id = 0)) THEN -- a new evaluation
8158     M_new_flag := 'Y';
8159 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8160 **********      (M_item_ctrls_verified + 1 > M_item_ctrls_total)     *******************************/
8161  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8162   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR(M_item_ctrls_verified + 1 < M_item_ctrls_total)   )  THEN
8163   	g_refresh_flag := 'Y';
8164   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
8165 ELSE
8166 *************/
8167 UPDATE AMW_FIN_CERT_EVAL_SUM
8168         SET
8169         last_update_date = sysdate,
8170         last_updated_by = fnd_global.user_id,
8171         last_update_login = fnd_global.conc_login_id,
8172         controls_verified = least(controls_verified + 1, total_number_of_ctrls)
8173         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
8174 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
8175         AND FINANCIAL_ITEM_ID = get_all_items_rec.FINANCIAL_ITEM_ID
8176         AND OBJECT_TYPE = 'FINANCIAL ITEM';
8177 --END IF;
8178        END IF;
8179 
8180         IF (M_change_flag = 'F' ) AND (M_new_flag = 'N') THEN
8181 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8182 **********      (M_item_ineff_ctrls  - 1 < 0 )      *******************************/
8183  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8184   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR(M_item_ineff_ctrls  - 1 > 0 )    )  THEN
8185   	g_refresh_flag := 'Y';
8186   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
8187 ELSE
8188 *************/
8189         UPDATE AMW_FIN_CERT_EVAL_SUM
8190         SET
8191         last_update_date = sysdate,
8192         last_updated_by = fnd_global.user_id,
8193         last_update_login = fnd_global.conc_login_id,
8194         ineffective_controls = greatest(0, ineffective_controls -1),
8195         ineffective_controls_prcnt = round( (greatest(0, m_item_ineff_ctrls -1))/decode(nvl(m_item_ctrls_total, 0), 0, 1, m_item_ctrls_total), 2)*100
8196         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
8197 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
8198         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
8199         AND OBJECT_TYPE = 'FINANCIAL ITEM';
8200 --END IF;
8201 
8202         ELSIF (M_change_flag = 'B') THEN
8203 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8204 **********    ( M_item_ineff_ctrls  + 1 > M_item_ctrls_verified)     *******************************/
8205  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8206   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y')  OR( M_item_ineff_ctrls  + 1 < M_item_ctrls_verified)    )  THEN
8207   	g_refresh_flag := 'Y';
8208   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
8209 ELSE
8210 *************/
8211 UPDATE AMW_FIN_CERT_EVAL_SUM
8212         SET
8213         last_update_date = sysdate,
8214    	last_updated_by = fnd_global.user_id,
8215    	last_update_login = fnd_global.conc_login_id,
8216         ineffective_controls = least(ineffective_controls + 1,  controls_verified),
8217         ineffective_controls_prcnt = round( (least(m_item_ineff_ctrls + 1, controls_verified))/decode(nvl(m_item_ctrls_total, 0), 0, 1, m_item_ctrls_total), 2)*100
8218         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
8219 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
8220         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
8221         AND OBJECT_TYPE = 'FINANCIAL ITEM';
8222 --END IF;
8223         END IF;
8224 
8225          amw_fin_coso_views_pvt.Update_item_ctrl_components (
8226 	P_CERTIFICATION_ID => Get_all_items_Rec.FIN_CERTIFICATION_ID,
8227                   P_FINANCIAL_STATEMENT_ID =>  Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
8228                   P_STATEMENT_GROUP_ID => Get_all_items_Rec.STATEMENT_GROUP_ID,
8229                   P_FINANCIAL_ITEM_ID => Get_all_items_Rec.FINANCIAL_ITEM_ID,
8230                   P_CONTROL_ID   => p_ctrl_id,
8231                   P_ORG_ID  => p_org_id,
8232                   P_CHANGE_FLAG => M_change_flag,
8233                   P_NEW_FLAG => M_new_flag) ;
8234 
8235         amw_fin_coso_views_pvt.Update_item_ctrl_Assertions (
8236                   P_CERTIFICATION_ID => Get_all_items_Rec.FIN_CERTIFICATION_ID,
8237                   P_FINANCIAL_STATEMENT_ID =>  Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
8238                   P_STATEMENT_GROUP_ID => Get_all_items_Rec.STATEMENT_GROUP_ID,
8239                   P_FINANCIAL_ITEM_ID => Get_all_items_Rec.FINANCIAL_ITEM_ID,
8240                   P_CONTROL_ID   => p_ctrl_id,
8241                   P_ORG_ID  => p_org_id,
8242                   P_CHANGE_FLAG => M_change_flag,
8243                   P_NEW_FLAG => M_new_flag) ;
8244 
8245         amw_fin_coso_views_pvt.Update_item_ctrl_objectives(
8246 		  P_CERTIFICATION_ID => Get_all_items_Rec.FIN_CERTIFICATION_ID,
8247                   P_FINANCIAL_STATEMENT_ID =>  Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
8248                   P_STATEMENT_GROUP_ID => Get_all_items_Rec.STATEMENT_GROUP_ID,
8249                   P_FINANCIAL_ITEM_ID => Get_all_items_Rec.FINANCIAL_ITEM_ID,
8250                   P_CONTROL_ID   => p_ctrl_id,
8251                   P_ORG_ID  => p_org_id,
8252                   P_CHANGE_FLAG => M_change_flag,
8253                   P_NEW_FLAG => M_new_flag) ;
8254 
8255                   UPDATE AMW_FIN_ITEM_ACC_CTRL
8256                   SET
8257        	 LAST_UPDATE_DATE = sysdate,
8258    	last_updated_by = fnd_global.user_id,
8259    	last_update_login = fnd_global.conc_login_id,
8260        	OPINION_LOG_ID = P_opinion_log_id
8261        WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
8262 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
8263         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
8264         AND OBJECT_TYPE = 'FINANCIAL ITEM';
8265 
8266 
8267   END LOOP;
8268 
8269  FOR Get_all_accts_Rec IN Get_all_accts(p_ctrl_id, p_org_id) LOOP
8270            exit when Get_all_accts%notfound;
8271 
8272     OPEN Get_old_opinion_id(Get_all_accts_Rec.FIN_CERTIFICATION_ID, p_org_id);
8273     FETCH Get_old_opinion_id INTO M_opinion_log_id;
8274     CLOSE Get_old_opinion_id;
8275 
8276     M_acc_ineff_ctrls := 0;
8277     M_acc_ctrls_verified := 0;
8278     M_acc_ctrls_total := 0;
8279 
8280    OPEN Get_Acct_Ctrl(Get_all_accts_Rec.FIN_CERTIFICATION_ID, Get_all_accts_Rec.ACCOUNT_GROUP_ID,
8281    Get_all_accts_Rec.NATURAL_ACCOUNT_ID);
8282    FETCH Get_Acct_Ctrl  INTO M_acc_ineff_ctrls, M_acc_ctrls_verified, M_acc_ctrls_total;
8283    CLOSE Get_Acct_Ctrl;
8284 
8285      Is_Eval_Change(
8286     		old_opinion_log_id  => M_opinion_log_id,
8287     		new_opinion_log_id  =>  P_opinion_log_id,
8288     		x_change_flag	=> M_change_flag);
8289 
8290 
8291    IF ((M_opinion_log_id IS NULL) OR (M_opinion_log_id = 0)) THEN -- a new evaluation
8292     M_new_flag := 'Y';
8293 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8294 **********    (M_acc_ctrls_verified + 1 > M_acc_ctrls_total)     *******************************/
8295  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8296   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(M_acc_ctrls_verified + 1 <  M_acc_ctrls_total)    )  THEN
8297   	g_refresh_flag := 'Y';
8298   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
8299 ELSE
8300 ************/
8301         UPDATE AMW_FIN_CERT_EVAL_SUM
8302         SET
8303         last_update_date = sysdate,
8304        last_updated_by = fnd_global.user_id,
8305        last_update_login = fnd_global.conc_login_id,
8306         controls_verified = least(controls_verified + 1, total_number_of_ctrls )
8307          WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
8308 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
8309 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
8310         AND OBJECT_TYPE = 'ACCOUNT';
8311 --END IF;
8312         END IF;
8313 
8314         IF(M_change_flag = 'F' ) AND (M_new_flag = 'N') THEN
8315 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8316 **********   (M_acc_ineff_ctrls - 1 < 0 )     *******************************/
8317  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8318   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_acc_ineff_ctrls - 1 >  0 )   )  THEN
8319   	g_refresh_flag := 'Y';
8320   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
8321 ELSE
8322 ****************/
8323 UPDATE AMW_FIN_CERT_EVAL_SUM
8324         SET
8325         last_update_date = sysdate,
8326    	last_updated_by = fnd_global.user_id,
8327    	last_update_login = fnd_global.conc_login_id,
8328         ineffective_controls = greatest(0, ineffective_controls - 1),
8329         ineffective_controls_prcnt = round( (greatest(0, m_acc_ineff_ctrls - 1))/decode(nvl(m_acc_ctrls_total, 0), 0, 1, m_acc_ctrls_total), 2)*100
8330         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
8331 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
8332 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
8333         AND OBJECT_TYPE = 'ACCOUNT';
8334 --END IF;
8335 
8336         ELSIF (M_change_flag = 'B') THEN
8337 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8338 **********   IF(M_acc_ineff_ctrls  + 1 >  M_acc_ctrls_verified)    *******************************/
8339   /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8340   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_acc_ineff_ctrls  + 1 <  M_acc_ctrls_verified)   )  THEN
8341   	g_refresh_flag := 'Y';
8342   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
8343 ELSE
8344 ***********/
8345  UPDATE AMW_FIN_CERT_EVAL_SUM
8346         SET
8347         last_update_date = sysdate,
8348    	last_updated_by = fnd_global.user_id,
8349    	last_update_login = fnd_global.conc_login_id,
8350         ineffective_controls = least(ineffective_controls + 1, controls_verified),
8351         ineffective_controls_prcnt = round( (least(m_acc_ineff_ctrls + 1, controls_verified)) /decode(nvl(m_acc_ctrls_total, 0), 0, 1, m_acc_ctrls_total), 2)*100
8352         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
8353 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
8354 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
8355         AND OBJECT_TYPE = 'ACCOUNT';
8356 --END IF;
8357         END IF;
8358 
8359 
8360         amw_fin_coso_views_pvt.Update_acc_ctrl_components (
8361         	  P_CERTIFICATION_ID => Get_all_accts_Rec.FIN_CERTIFICATION_ID,
8362                   P_ACCOUNT_GROUP_ID  => Get_all_accts_Rec.ACCOUNT_GROUP_ID,
8363                   P_ACCOUNT_ID   => Get_all_accts_Rec.NATURAL_ACCOUNT_ID,
8364                   P_CONTROL_ID   => p_ctrl_id,
8365                   P_ORG_ID  => p_org_id ,
8366                   P_CHANGE_FLAG => M_change_flag,
8367                   P_NEW_FLAG  => M_new_flag);
8368 
8369         amw_fin_coso_views_pvt.Update_acc_ctrl_Assertions (
8370                   P_CERTIFICATION_ID => Get_all_accts_Rec.FIN_CERTIFICATION_ID,
8371                   P_ACCOUNT_GROUP_ID  => Get_all_accts_Rec.ACCOUNT_GROUP_ID,
8372                   P_ACCOUNT_ID   => Get_all_accts_Rec.NATURAL_ACCOUNT_ID,
8373                   P_CONTROL_ID   => p_ctrl_id,
8374                   P_ORG_ID  => p_org_id ,
8375                   P_CHANGE_FLAG => M_change_flag,
8376                   P_NEW_FLAG  => M_new_flag);
8377 
8378         amw_fin_coso_views_pvt.Update_acc_ctrl_objectives (
8379         	 P_CERTIFICATION_ID => Get_all_accts_Rec.FIN_CERTIFICATION_ID,
8380                   P_ACCOUNT_GROUP_ID  => Get_all_accts_Rec.ACCOUNT_GROUP_ID,
8381                   P_ACCOUNT_ID   => Get_all_accts_Rec.NATURAL_ACCOUNT_ID,
8382                   P_CONTROL_ID   => p_ctrl_id,
8383                   P_ORG_ID  => p_org_id ,
8384                   P_CHANGE_FLAG => M_change_flag,
8385                   P_NEW_FLAG  => M_new_flag);
8386 
8387                     UPDATE AMW_FIN_ITEM_ACC_CTRL
8388                   SET
8389        	 LAST_UPDATE_DATE = sysdate,
8390    	last_updated_by = fnd_global.user_id,
8391    	last_update_login = fnd_global.conc_login_id,
8392        	OPINION_LOG_ID = P_opinion_log_id
8393       WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
8394 	AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
8395 	AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
8396         AND OBJECT_TYPE = 'ACCOUNT';
8397 
8398  END LOOP;
8399 
8400 
8401  ---modify the org, dashboard and process eval summary tables
8402  FOR Get_all_fin_cert_Rec IN Get_all_fin_cert(p_org_id) LOOP
8403            exit when Get_all_fin_cert%notfound;
8404 
8405     OPEN Get_old_opinion_id(Get_all_fin_cert_Rec.FIN_CERTIFICATION_ID, p_org_id);
8406     FETCH Get_old_opinion_id INTO M_opinion_log_id;
8407     CLOSE Get_old_opinion_id;
8408 
8409    OPEN Get_Org_Ctrl(Get_all_fin_cert_Rec.FIN_CERTIFICATION_ID, p_org_id);
8410    FETCH Get_Org_Ctrl  INTO M_org_ineff_ctrls, M_org_ctrls_verified, M_org_ctrls_total;
8411    CLOSE Get_Org_Ctrl;
8412 
8413    OPEN Get_Dashboard_Ctrl(Get_all_fin_cert_Rec.fin_certification_id);
8414    FETCH Get_Dashboard_Ctrl  INTO M_dashboard_ineff_ctrls;
8415    CLOSE Get_Dashboard_Ctrl;
8416 
8417    OPEN Get_Proc_Ctrl(Get_all_fin_cert_Rec.fin_certification_id, p_org_id);
8418    FETCH Get_Proc_Ctrl  INTO M_proc_ineff_ctrls, M_proc_ctrls_verified, M_proc_ctrls_total;
8419    CLOSE Get_Proc_Ctrl;
8420 
8421 	  Is_Eval_Change(
8422     		old_opinion_log_id  => M_opinion_log_id,
8423     		new_opinion_log_id  =>  P_opinion_log_id,
8424     		x_change_flag	=> M_change_flag);
8425 
8426 
8427  IF ((M_opinion_log_id IS NULL) OR (M_opinion_log_id = 0)) THEN -- a new evaluation
8428     M_new_flag := 'Y';
8429 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8430 **********   IF(M_proc_ctrls_verified + 1 > M_proc_ctrls_total)    *******************************/
8431  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8432   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_proc_ctrls_verified + 1 < M_proc_ctrls_total)   )  THEN
8433   	g_refresh_flag := 'Y';
8434   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
8435 ELSE
8436 *************/
8437       UPDATE AMW_FIN_PROCESS_EVAL_SUM
8438  	SET
8439  	last_update_date = sysdate,
8440    	last_updated_by = fnd_global.user_id,
8441    	last_update_login = fnd_global.conc_login_id,
8442  	controls_verified = least(controls_verified + 1, total_number_of_ctrls )
8443   	WHERE FIN_CERTIFICATION_ID = get_all_fin_cert_rec.fin_certification_id
8444   	AND ORGANIZATION_ID = p_org_id;
8445 --END IF;
8446 
8447 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8448 **********  (M_org_ctrls_verified + 1 > M_org_ctrls_total)    *******************************/
8449  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8450   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_org_ctrls_verified + 1 < M_org_ctrls_total)   )  THEN
8451   	g_refresh_flag := 'Y';
8452   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
8453 ELSE
8454 ***********/
8455 UPDATE AMW_FIN_ORG_EVAL_SUM
8456  	SET
8457  	LAST_UPDATE_DATE = sysdate,
8458    	last_updated_by = fnd_global.user_id,
8459    	last_update_login = fnd_global.conc_login_id,
8460  	controls_verified = least(controls_verified + 1, total_number_of_ctrls )
8461   	WHERE FIN_CERTIFICATION_ID = get_all_fin_cert_rec.fin_certification_id
8462   	AND ORGANIZATION_ID = p_org_id;
8463 --END IF;
8464 
8465 END IF;
8466 
8467    IF(M_change_flag = 'F' ) AND (M_new_flag = 'N') THEN
8468 
8469 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8470 **********  (M_dashboard_ineff_ctrls -1 < 0 )     *******************************/
8471   /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8472   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_dashboard_ineff_ctrls -1 > 0 )    )  THEN
8473   	g_refresh_flag := 'Y';
8474   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
8475 ELSE
8476 *****************/
8477 UPDATE AMW_CERT_DASHBOARD_SUM
8478  	SET
8479  	LAST_UPDATE_DATE = sysdate,
8480    	last_updated_by = fnd_global.user_id,
8481    	last_update_login = fnd_global.conc_login_id,
8482  	ineffective_controls = greatest(0, ineffective_controls - 1)
8483  	WHERE CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id;
8484 -- END IF;
8485 
8486  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8487 **********  (M_proc_ineff_ctrls -1 < 0 )     *******************************/
8488  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8489   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_proc_ineff_ctrls -1 > 0 )   )  THEN
8490   	g_refresh_flag := 'Y';
8491   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
8492 ELSE
8493 ************/
8494 UPDATE AMW_FIN_PROCESS_EVAL_SUM
8495  	SET
8496  	LAST_UPDATE_DATE = sysdate,
8497    	last_updated_by = fnd_global.user_id,
8498    	last_update_login = fnd_global.conc_login_id,
8499 	ineffective_controls = greatest(0, ineffective_controls - 1)
8500   	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
8501   	AND ORGANIZATION_ID = p_org_id;
8502 --END IF;
8503 
8504  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8505 **********  (M_org_ineff_ctrls - 1  < 0 )      *******************************/
8506 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8507   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (M_org_ineff_ctrls - 1  > 0 )    )  THEN
8508   	g_refresh_flag := 'Y';
8509   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
8510 ELSE
8511 ************/
8512 UPDATE AMW_FIN_ORG_EVAL_SUM
8513  	SET
8514  	last_update_date = sysdate,
8515    	last_updated_by = fnd_global.user_id,
8516    	last_update_login = fnd_global.conc_login_id,
8517  	ineffective_controls = greatest(0, ineffective_controls - 1),
8518        	 ineffective_controls_prcnt = round( (greatest(0, m_org_ineff_ctrls - 1))/decode(nvl(m_org_ctrls_total, 0), 0, 1, m_org_ctrls_total), 2)*100
8519  	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
8520         AND ORGANIZATION_ID = p_org_id;
8521 -- END IF;
8522 
8523    ELSIF(M_change_flag = 'B') THEN
8524 
8525     	UPDATE AMW_CERT_DASHBOARD_SUM
8526     	SET
8527     	last_update_date = sysdate,
8528    	last_updated_by = fnd_global.user_id,
8529    	last_update_login = fnd_global.conc_login_id,
8530     	ineffective_controls = ineffective_controls + 1
8531  	WHERE CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id;
8532 
8533 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8534 ********** (M_org_ineff_ctrls +  1  >  M_org_ctrls_verified )     *******************************/
8535 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8536   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(M_org_ineff_ctrls +  1  <  M_org_ctrls_verified )    )  THEN
8537   	g_refresh_flag := 'Y';
8538   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
8539 ELSE
8540 ***********/
8541  UPDATE AMW_FIN_ORG_EVAL_SUM
8542   	SET
8543   	last_update_date = sysdate,
8544    	last_updated_by = fnd_global.user_id,
8545    	last_update_login = fnd_global.conc_login_id,
8546  	 ineffective_controls = least(ineffective_controls + 1,  controls_verified),
8547        	 ineffective_controls_prcnt = round((least(m_org_ineff_ctrls + 1, controls_verified))/decode(nvl(m_org_ctrls_total, 0), 0, 1, m_org_ctrls_total), 2)*100
8548  	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
8549         AND ORGANIZATION_ID = p_org_id;
8550  --END IF;
8551 
8552 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8553 **********(M_proc_ineff_ctrls +  1  >  M_proc_ctrls_verified )     *******************************/
8554 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8555   IF NOT ( (m_certification_list.exists(Get_all_fin_cert_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(M_proc_ineff_ctrls +  1  <   M_proc_ctrls_verified )     )  THEN
8556   	g_refresh_flag := 'Y';
8557   	m_certification_list(Get_all_fin_cert_Rec.fin_certification_id) := Get_all_fin_cert_Rec.fin_certification_id;
8558 ELSE
8559 ********/
8560   UPDATE AMW_FIN_PROCESS_EVAL_SUM
8561  	SET
8562  	last_update_date = sysdate,
8563    	last_updated_by = fnd_global.user_id,
8564    	last_update_login = fnd_global.conc_login_id,
8565  	 ineffective_controls = least(ineffective_controls + 1,  controls_verified),
8566  	 ineffective_controls_prcnt = round((least(ineffective_controls + 1, controls_verified))/decode(nvl(total_number_of_ctrls , 0), 0, 1, total_number_of_ctrls), 2)*100
8567   	WHERE FIN_CERTIFICATION_ID = Get_all_fin_cert_Rec.fin_certification_id
8568   	AND ORGANIZATION_ID = p_org_id;
8569 --  END IF;
8570 
8571   END IF;
8572 
8573 END LOOP;
8574 
8575 
8576 
8577 x_return_status := FND_API.G_RET_STS_SUCCESS;
8578 
8579 EXCEPTION
8580      WHEN NO_DATA_FOUND THEN
8581      IF Get_all_fin_cert%ISOPEN THEN
8582      close Get_all_fin_cert;
8583      end if;
8584      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
8585      x_return_status := FND_API.G_RET_STS_ERROR;
8586      x_msg_count := 1;
8587      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
8588       WHEN OTHERS THEN
8589       IF Get_all_fin_cert%ISOPEN THEN
8590        close Get_all_fin_cert;
8591       end if;
8592        ROLLBACK TO CONTROL_EVALUATION_HANDLER;
8593       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
8594       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
8595         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8596         x_msg_count := 1;
8597         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
8598                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
8599                 FND_MSG_PUB.Count_And_Get(
8600                 p_encoded =>  FND_API.G_FALSE,
8601                 p_count   =>  x_msg_count,
8602                 p_data    =>  x_msg_data);
8603                 RAISE;
8604                 RETURN;
8605 
8606 
8607 END CONTROL_EVALUATION_HANDLER;
8608 
8609 PROCEDURE PROCESS_CHANGE_HANDLER
8610 (
8611 p_api_version_number        IN   NUMBER   := 1.0,
8612 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
8613 p_commit                    IN   VARCHAR2 := FND_API.g_false,
8614 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
8615  p_org_id number,
8616  p_process_id number,
8617  p_opinion_log_id number,
8618  p_action varchar2,
8619  x_return_status             OUT  nocopy VARCHAR2,
8620 x_msg_count                 OUT  nocopy NUMBER,
8621 x_msg_data                  OUT  nocopy VARCHAR2)
8622 IS
8623 
8624 ---CURSOR TO GET ALL OF FINANCIAL ITEMS WHICH RELATE TO THIS PROCESS
8625 CURSOR Get_all_items( l_org_id number, l_process_id number)IS
8626 SELECT DISTINCT fin.FIN_CERTIFICATION_ID, fin.STATEMENT_GROUP_ID, fin.FINANCIAL_STATEMENT_ID, fin.FINANCIAL_ITEM_ID
8627 FROM AMW_FIN_CERT_SCOPE fin,
8628      AMW_CERTIFICATION_B cert
8629 WHERE fin.ORGANIZATION_ID = l_org_id
8630 AND  fin.PROCESS_ID = l_process_id
8631 AND  fin.FINANCIAL_ITEM_ID IS NOT NULL
8632 AND  fin.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
8633 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
8634 
8635 ---CURSOR TO GET ALL OF ACCOUNTS WHICH RELATE TO THIS PROCESS
8636 CURSOR 	Get_all_accts (l_org_id number, l_process_id number)IS
8637 SELECT DISTINCT fin.fin_certification_id, fin.account_group_id, fin.natural_account_id
8638 FROM AMW_FIN_CERT_SCOPE fin,
8639      AMW_CERTIFICATION_B cert
8640 WHERE fin.ORGANIZATION_ID = l_org_id
8641 AND   fin.PROCESS_ID = l_process_id
8642 AND   fin.natural_account_id is not null
8643 AND  fin.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
8644 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
8645 
8646 
8647 --CURSOR TO GET ALL OF FINANCIAL CERTIFICATION WHICH CONTAINS THIS PROCESSE
8648 CURSOR Get_all_fin_cert(l_org_id number, l_process_id number) IS
8649 SELECT proc.FIN_CERTIFICATION_ID
8650 FROM AMW_FIN_PROCESS_EVAL_SUM proc,
8651      AMW_CERTIFICATION_B cert
8652 WHERE proc.organization_id = l_org_id
8653 AND proc.PROCESS_ID = l_process_id
8654 AND proc.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
8655 AND cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
8656 
8657 
8658 --CURSOR TO GET ALL OF FINCERCIAL CERTIFICATION WHICH THIS PROCESS AS SUB-PROCESS
8659 CURSOR Get_all_fin_process(l_org_id number, l_process_id number) IS
8660 SELECT proc.FIN_CERTIFICATION_ID, proc.PARENT_PROCESS_ID
8661 FROM AMW_FIN_PROCESS_FLAT proc,
8662      AMW_CERTIFICATION_B cert
8663 WHERE proc.ORGANIZATION_ID = l_org_id
8664 AND proc.CHILD_PROCESS_ID = l_process_id
8665 AND cert.CERTIFICATION_ID = proc.FIN_CERTIFICATION_ID
8666 AND cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
8667 
8668 --CURSOR TO GET OLD EVAL OPINION LOG ID
8669 CURSOR Get_old_opinion_id(l_cert_id number, l_org_id number, l_process_id number) IS
8670 SELECT EVAL_OPINION_LOG_ID
8671 FROM AMW_FIN_PROCESS_EVAL_SUM
8672 WHERE  FIN_CERTIFICATION_ID = l_cert_id
8673 AND ORGANIZATION_ID = l_org_id
8674 AND PROCESS_ID = l_process_id;
8675 
8676 --CURSOR TO GET OLD CERT OPINION LOG ID
8677 CURSOR Get_old_cert_opinion_id(l_cert_id number, l_org_id number, l_process_id number) IS
8678     SELECT CERT_OPINION_LOG_ID
8679 	FROM AMW_FIN_PROCESS_EVAL_SUM
8680 	WHERE  FIN_CERTIFICATION_ID = l_cert_id
8681 	AND ORGANIZATION_ID = l_org_id
8682 	AND PROCESS_ID = l_process_id;
8683 
8684 --CURSOR TO GET PROCESS INFO FOR AN ITEM when an evaluation
8685      CURSOR Get_Item_Proc(l_cert_id NUMBER, l_stmt_id NUMBER, l_item_id NUMBER) IS
8686          SELECT PROC_WITH_INEFFECTIVE_CONTROLS, PROC_EVALUATED,TOTAL_NUMBER_OF_PROCESSES
8687          FROM  AMW_FIN_CERT_EVAL_SUM
8688          WHERE FIN_CERTIFICATION_ID = l_cert_id
8689     AND FINANCIAL_STATEMENT_ID = l_stmt_id
8690     AND FINANCIAL_ITEM_ID = l_item_id
8691     AND OBJECT_TYPE = 'FINANCIAL ITEM';
8692 
8693 --CURSOR TO GET PROCESS INFO FOR AN ACCOUNT
8694 CURSOR Get_Acct_Proc(l_cert_id NUMBER, l_acct_group_id NUMBER, l_acct_id NUMBER) IS
8695         SELECT PROC_WITH_INEFFECTIVE_CONTROLS, PROC_EVALUATED,TOTAL_NUMBER_OF_PROCESSES
8696          FROM  AMW_FIN_CERT_EVAL_SUM
8697        WHERE FIN_CERTIFICATION_ID = l_cert_id
8698        AND ACCOUNT_GROUP_ID = l_acct_group_id
8699        AND NATURAL_ACCOUNT_ID = l_acct_id
8700       AND OBJECT_TYPE = 'ACCOUNT';
8701 
8702  --CURSOR TO GET PROCESS INFO FOR AN ITEM when certification is modified
8703    CURSOR Get_Cert_Item_Proc(l_cert_id NUMBER, l_stmt_id NUMBER, l_item_id NUMBER) IS
8704        SELECT TOTAL_NUMBER_OF_PROCESSES, PROC_PENDING_CERTIFICATION, PROC_CERTIFIED_WITH_ISSUES
8705          FROM  AMW_FIN_CERT_EVAL_SUM
8706          WHERE FIN_CERTIFICATION_ID = l_cert_id
8707     AND FINANCIAL_STATEMENT_ID = l_stmt_id
8708     AND FINANCIAL_ITEM_ID = l_item_id
8709     AND OBJECT_TYPE = 'FINANCIAL ITEM';
8710 
8711  --CURSOR TO GET PROCESS INFO FOR AN ACCOUNT when certification is modified
8712    CURSOR Get_Cert_Acct_Proc(l_cert_id NUMBER, l_acct_group_id NUMBER, l_acct_id NUMBER) IS
8713         SELECT TOTAL_NUMBER_OF_PROCESSES, PROC_PENDING_CERTIFICATION, PROC_CERTIFIED_WITH_ISSUES
8714          FROM  AMW_FIN_CERT_EVAL_SUM
8715        WHERE FIN_CERTIFICATION_ID = l_cert_id
8716        AND ACCOUNT_GROUP_ID = l_acct_group_id
8717        AND NATURAL_ACCOUNT_ID = l_acct_id
8718       AND OBJECT_TYPE = 'ACCOUNT';
8719 
8720  --CURSOR TO GET FIN-CERT ID IN ORG_SUM TABLE
8721 CURSOR Get_All_Org_Cert(l_org_id number) IS
8722 SELECT  fin_certification_id, PROC_PENDING_CERTIFICATION, PROC_CERTIFIED_WITH_ISSUES,
8723 	PROC_CERTIFIED, PROC_VERIFIED, TOTAL_NUMBER_OF_PROCS, PROC_WITH_INEFFECTIVE_CONTROLS
8724 FROM amw_fin_org_eval_sum
8725 WHERE ORGANIZATION_ID = l_org_id;
8726 
8727 CURSOR Get_Dashboard_Info(l_cert_id number) IS
8728 SELECT PROCESSES_NOT_CERT, PROCESSES_CERT_ISSUES, ORG_PROCESS_NOT_CERT, ORG_PROCESS_CERT_ISSUES,
8729 PROC_INEFF_CONTROL, ORG_PROC_INEFF_CONTROL
8730 FROM amw_cert_dashboard_sum
8731 WHERE certification_id = l_cert_id;
8732 
8733 CURSOR Get_parent_process(l_opinion_log_id  number, l_org_id number, l_process_id number) IS
8734 SELECT fin.fin_certification_id, fin.organization_id, fin.process_id, fin.number_of_sub_procs_certified, fin.total_number_of_sub_procs
8735 FROM    amw_fin_process_eval_sum fin
8736        WHERE fin.organization_id = l_org_id
8737 	 AND fin.process_id in (
8738 	          SELECT proc.parent_process_id
8739 		    FROM amw_fin_process_flat proc
8740 		    WHERE proc.fin_certification_id = fin.fin_certification_id
8741 		    AND 	proc.organization_id = l_org_id
8742 		    AND 	proc.child_process_id = l_process_id)
8743 	AND  fin.fin_certification_id in (select rel.fin_stmt_cert_id from amw_fin_proc_cert_relan rel, amw_opinions_log opin
8744 		       where rel.proc_cert_id = opin.pk2_value
8745 		       and opin.opinion_log_id = l_opinion_log_id
8746 		       and rel.end_date is null);
8747 
8748 M_dashbd_proc_not_cert number;
8749 M_dashbd_proc_cert_issue number;
8750 M_dashbd_org_proc_not_cert number;
8751 M_dashbd_org_proc_cert_issue number;
8752 M_dashbd_proc_ineff_ctrl number;
8753 M_dashbd_org_proc_ineff_ctrl number;
8754 
8755 M_item_proc_pending_cert  number;
8756 M_item_total_number_process number;
8757 M_item_proc_cert_with_issue number;
8758 M_item_proc_with_ineff_ctrl number;
8759 M_item_proc_evaluated number;
8760 
8761 M_acc_proc_pending_cert  number;
8762 M_acc_total_number_process number;
8763 M_acc_proc_cert_with_issue number;
8764 M_acc_proc_with_ineff_ctrl number;
8765 M_acc_proc_evaluated number;
8766 
8767 
8768 M_proc_num_of_sub_procs_cert number;
8769 M_proc_total_num_of_sub_proc number;
8770 
8771 M_org_proc_with_ineff_ctrl number;
8772 M_org_total_num_of_process number;
8773 M_org_total_number_process number;
8774 M_org_proc_pending_cert number;
8775 M_org_proc_cert_with_issue number;
8776 
8777 M_opinion_log_id AMW_OPINIONS_LOG.OPINION_LOG_ID%TYPE;
8778 
8779 M_change_flag VARCHAR2(1);
8780 M_new_flag VARCHAR2(1) := 'N';
8781 
8782 l_error_message varchar2(4000);
8783 
8784 
8785 l_api_name           CONSTANT VARCHAR2(30) := 'PROCESS_CHANGE_HANDLER';
8786 l_api_version_number CONSTANT NUMBER       := 1.0;
8787 
8788 l_return_status VARCHAR2(32767);
8789 l_msg_count NUMBER;
8790 l_msg_data VARCHAR2(32767);
8791 
8792 BEGIN
8793 
8794 SAVEPOINT PROCESS_CHANGE_HANDLER;
8795 
8796  -- Standard call to check for call compatibility.
8797         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
8798                                              p_api_version_number,
8799                                              l_api_name,
8800                                              G_PKG_NAME)
8801         THEN
8802                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8803         END IF;
8804 
8805         -- Initialize message list if p_init_msg_list is set to TRUE.
8806         IF FND_API.to_Boolean( p_init_msg_list )
8807         THEN
8808                 FND_MSG_PUB.initialize;
8809         END IF;
8810 
8811         -- Initialize API return status to SUCCESS
8812         x_return_status := FND_API.G_RET_STS_SUCCESS;
8813 
8814 --modify fin_cert_eval summary table
8815 IF (p_action = 'EVALUATION') THEN
8816 
8817 FOR Get_all_items_Rec in Get_all_items(p_org_id , p_process_id) LOOP
8818 	 exit when Get_all_items %notfound;
8819 
8820 
8821     OPEN Get_old_opinion_id(Get_all_items_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
8822     FETCH Get_old_opinion_id INTO M_opinion_log_id;
8823     CLOSE Get_old_opinion_id;
8824 
8825    M_item_total_number_process := 0;
8826    M_item_proc_evaluated := 0;
8827    M_item_proc_with_ineff_ctrl := 0;
8828 
8829   OPEN Get_Item_Proc(Get_all_items_Rec.FIN_CERTIFICATION_ID, Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
8830    Get_all_items_Rec.FINANCIAL_ITEM_ID);
8831    FETCH Get_Item_Proc  INTO M_item_proc_with_ineff_ctrl, M_item_proc_evaluated, M_item_total_number_process;
8832    CLOSE Get_Item_Proc;
8833 
8834 	Is_Eval_Change (
8835     		old_opinion_log_id  => M_opinion_log_id ,
8836     		new_opinion_log_id  => p_opinion_log_id,
8837     		x_change_flag	=> M_change_flag);
8838 
8839 
8840     IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  -- a new evaluation
8841      M_new_flag := 'Y';
8842 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8843 **********(M_item_proc_evaluated + 1 > M_item_total_number_process)    *******************************/
8844 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8845   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(M_item_proc_evaluated + 1 < M_item_total_number_process) )  THEN
8846   	g_refresh_flag := 'Y';
8847   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
8848 ELSE
8849 *************/
8850 UPDATE AMW_FIN_CERT_EVAL_SUM
8851         SET
8852         proc_evaluated = least(proc_evaluated + 1,total_number_of_processes),
8853         last_update_date = sysdate,
8854         last_updated_by = fnd_global.user_id,
8855         last_update_login = fnd_global.conc_login_id
8856         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
8857        AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
8858         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
8859         AND OBJECT_TYPE = 'FINANCIAL ITEM';
8860 -- END IF;
8861 
8862  END IF;
8863 
8864 
8865         IF (M_change_flag = 'B') THEN
8866  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8867 **********( M_item_proc_with_ineff_ctrl + 1 > M_item_proc_evaluated )    *******************************/
8868 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8869   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( M_item_proc_with_ineff_ctrl + 1 < M_item_proc_evaluated )  )  THEN
8870   	g_refresh_flag := 'Y';
8871   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
8872 ELSE
8873 ****************/
8874 UPDATE AMW_FIN_CERT_EVAL_SUM
8875         SET
8876         proc_with_ineffective_controls = least(proc_with_ineffective_controls + 1, controls_verified),
8877         proc_with_ineff_controls_prcnt = round( (least(m_item_proc_with_ineff_ctrl + 1, controls_verified))/decode(nvl(m_item_total_number_process, 0), 0, 1, m_item_total_number_process), 2) * 100,
8878         last_update_date = sysdate,
8879         last_updated_by = fnd_global.user_id,
8880         last_update_login = fnd_global.conc_login_id
8881         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
8882        AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
8883         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
8884         AND OBJECT_TYPE = 'FINANCIAL ITEM';
8885 --END IF;
8886 
8887         ELSIF (M_change_flag = 'F'  and M_new_flag = 'N' )  THEN
8888  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8889 **********( M_item_proc_with_ineff_ctrl -1 < 0  )    *******************************/
8890 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8891   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( M_item_proc_with_ineff_ctrl -1 > 0  )  )  THEN
8892   	g_refresh_flag := 'Y';
8893   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
8894 ELSE
8895 **************/
8896 UPDATE AMW_FIN_CERT_EVAL_SUM
8897         SET
8898         proc_with_ineffective_controls = greatest(0, proc_with_ineffective_controls - 1),
8899         proc_with_ineff_controls_prcnt = round( (greatest(0,m_item_proc_with_ineff_ctrl - 1))/decode(nvl(m_item_total_number_process, 0), 0, 1, m_item_total_number_process), 2) * 100,
8900         last_update_date = sysdate,
8901         last_updated_by = fnd_global.user_id,
8902         last_update_login = fnd_global.conc_login_id
8903         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
8904         AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
8905         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
8906         AND OBJECT_TYPE = 'FINANCIAL ITEM';
8907 -- END IF;
8908         END IF;
8909     END LOOP;
8910 
8911 
8912     FOR Get_all_accts_Rec IN Get_all_accts(p_org_id , p_process_id) LOOP
8913            exit when Get_all_accts%notfound;
8914 
8915      OPEN Get_old_opinion_id(Get_all_accts_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
8916     FETCH Get_old_opinion_id INTO M_opinion_log_id;
8917     CLOSE Get_old_opinion_id;
8918 
8919       M_acc_proc_with_ineff_ctrl := 0;
8920     M_acc_proc_evaluated := 0;
8921     M_acc_total_number_process := 0;
8922 
8923    OPEN Get_Acct_Proc(Get_all_accts_Rec.FIN_CERTIFICATION_ID, Get_all_accts_Rec.ACCOUNT_GROUP_ID,
8924    Get_all_accts_Rec.NATURAL_ACCOUNT_ID);
8925    FETCH Get_Acct_Proc  INTO M_acc_proc_with_ineff_ctrl, M_acc_proc_evaluated,  M_acc_total_number_process;
8926    CLOSE Get_Acct_Proc;
8927 
8928 	Is_Eval_Change(
8929     		old_opinion_log_id  =>  M_opinion_log_id ,
8930     		new_opinion_log_id  =>  p_opinion_log_id,
8931     		x_change_flag	=> M_change_flag);
8932 
8933 
8934 
8935     IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  --  a new evaluation
8936      M_new_flag := 'Y';
8937  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8938 **********(M_acc_proc_evaluated + 1 > M_acc_total_number_process)   *******************************/
8939 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8940   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(M_acc_proc_evaluated + 1 <  M_acc_total_number_process)   )  THEN
8941   	g_refresh_flag := 'Y';
8942   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
8943 ELSE
8944 **************/
8945 UPDATE AMW_FIN_CERT_EVAL_SUM
8946         SET
8947         proc_evaluated = least(proc_evaluated + 1, total_number_of_processes),
8948         last_update_date = sysdate,
8949         last_updated_by = fnd_global.user_id,
8950         last_update_login = fnd_global.conc_login_id
8951         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
8952        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
8953        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
8954        AND OBJECT_TYPE = 'ACCOUNT';
8955  --   END IF;
8956 END IF;
8957 
8958         IF (M_change_flag = 'B') THEN
8959  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8960 **********(  M_acc_proc_with_ineff_ctrl + 1 > M_acc_proc_evaluated)    *******************************/
8961 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8962   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(  M_acc_proc_with_ineff_ctrl + 1 <  M_acc_proc_evaluated)   )  THEN
8963   	g_refresh_flag := 'Y';
8964   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
8965 ELSE
8966 ***************/
8967 UPDATE AMW_FIN_CERT_EVAL_SUM
8968         SET
8969         proc_with_ineffective_controls = least(proc_with_ineffective_controls + 1,controls_verified),
8970         proc_with_ineff_controls_prcnt = round( (least(m_item_proc_with_ineff_ctrl + 1, controls_verified))/decode(nvl(m_acc_total_number_process, 0), 0, 1, m_acc_total_number_process), 2) * 100,
8971         last_update_date = sysdate,
8972         last_updated_by = fnd_global.user_id,
8973         last_update_login = fnd_global.conc_login_id
8974         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
8975        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
8976        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
8977        AND OBJECT_TYPE = 'ACCOUNT';
8978 -- END IF;
8979 
8980         ELSIF (M_change_flag = 'F'  and M_new_flag = 'N' )  THEN
8981  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
8982 **********(  M_acc_proc_with_ineff_ctrl -1 < 0 ) *******************************/
8983 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
8984   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(  M_acc_proc_with_ineff_ctrl -1 > 0 )   )  THEN
8985   	g_refresh_flag := 'Y';
8986   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
8987 ELSE
8988 ***********/
8989  UPDATE AMW_FIN_CERT_EVAL_SUM
8990         SET
8991         proc_with_ineffective_controls = greatest(0,proc_with_ineffective_controls - 1),
8992         proc_with_ineff_controls_prcnt = round( (greatest(0,m_acc_proc_with_ineff_ctrl - 1))/decode(nvl(m_acc_total_number_process, 0), 0, 1, m_acc_total_number_process), 2) * 100,
8993         last_update_date = sysdate,
8994         last_updated_by = fnd_global.user_id,
8995         last_update_login = fnd_global.conc_login_id
8996         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
8997         AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
8998         AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
8999         AND OBJECT_TYPE = 'ACCOUNT';
9000 --END IF;
9001         END IF;
9002 
9003 
9004  END LOOP;
9005 
9006  --update amw_fin_org_eval_sum and amw_cert_dashboard_sum tables
9007  FOR Get_All_Org_Cert_Rec in Get_All_Org_Cert(p_org_id) LOOP
9008  	exit when Get_All_Org_Cert %notfound;
9009 
9010     OPEN Get_old_cert_opinion_id(Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
9011     FETCH Get_old_cert_opinion_id INTO M_opinion_log_id;
9012     CLOSE Get_old_cert_opinion_id;
9013 
9014       Is_Eval_Change(
9015     		old_opinion_log_id  =>  M_opinion_log_id ,
9016     		new_opinion_log_id  =>  p_opinion_log_id,
9017     		x_change_flag	=> M_change_flag);
9018 
9019     OPEN Get_Dashboard_Info(Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID);
9020     FETCH Get_Dashboard_Info INTO M_dashbd_proc_not_cert,M_dashbd_proc_cert_issue,
9021 				  M_dashbd_org_proc_not_cert,M_dashbd_org_proc_cert_issue,
9022 				  M_dashbd_proc_ineff_ctrl,M_dashbd_org_proc_ineff_ctrl;
9023     CLOSE Get_Dashboard_Info;
9024 
9025      IF(M_opinion_log_id is NOT NULL AND M_change_flag = 'F' ) THEN
9026  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9027 **********( get_all_org_cert_rec.proc_verified  + 1  >  get_all_org_cert_rec.total_number_of_procs) *******************************/
9028  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9029   IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( Get_All_Org_Cert_Rec.PROC_VERIFIED  + 1  <  Get_All_Org_Cert_Rec.TOTAL_NUMBER_OF_PROCS)   )  THEN
9030   	g_refresh_flag := 'Y';
9031   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9032 ELSE
9033 ***********/
9034 UPDATE AMW_FIN_ORG_EVAL_SUM SET
9035         LAST_UPDATE_DATE = sysdate,
9036         last_updated_by = fnd_global.user_id,
9037         last_update_login = fnd_global.conc_login_id,
9038         proc_verified =  least(proc_verified + 1, total_number_of_procs )
9039         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9040 	AND ORGANIZATION_ID  = p_org_id;
9041 --END IF;
9042 
9043    ELSIF( M_opinion_log_id is NULL AND M_change_flag = 'B' ) THEN
9044  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9045 **********(( get_all_org_cert_rec.proc_verified  + 1  >  get_all_org_cert_rec.total_number_of_procs)
9046 ********* or ( get_all_org_cert_rec.proc_with_ineffective_controls + 1 > get_all_org_cert_rec.proc_verified))*******************************/
9047 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9048   IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(( get_all_org_cert_rec.proc_verified  + 1  <  get_all_org_cert_rec.total_number_of_procs)
9049         AND ( get_all_org_cert_rec.proc_with_ineffective_controls + 1 < get_all_org_cert_rec.proc_verified))   )  THEN
9050   	g_refresh_flag := 'Y';
9051   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9052 ELSE
9053 *************/
9054 UPDATE AMW_FIN_ORG_EVAL_SUM SET
9055         last_update_date = sysdate,
9056         last_updated_by = fnd_global.user_id,
9057         last_update_login = fnd_global.conc_login_id,
9058         proc_verified =  least(proc_verified + 1,total_number_of_procs),
9059         proc_with_ineffective_controls = least(proc_with_ineffective_controls + 1,proc_verified),
9060         proc_with_ineff_controls_prcnt = round( (least(proc_with_ineffective_controls + 1, proc_verified))/decode(nvl(total_number_of_procs, 0), 0, 1, total_number_of_procs), 2)*100
9061         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9062 	AND ORGANIZATION_ID  = p_org_id;
9063 --END IF;
9064 
9065 	IF p_org_id = fnd_profile.value('AMW_GLOBAL_ORG_ID') THEN
9066 	  UPDATE amw_cert_dashboard_sum
9067              SET last_update_date = sysdate,
9068                  last_updated_by = fnd_global.user_id,
9069 	         last_update_login = fnd_global.conc_login_id,
9070 		 proc_ineff_control = proc_ineff_control+1
9071   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9072         ELSE
9073 	  UPDATE amw_cert_dashboard_sum
9074              SET last_update_date = sysdate,
9075                  last_updated_by = fnd_global.user_id,
9076 	         last_update_login = fnd_global.conc_login_id,
9077 		 org_proc_ineff_control = org_proc_ineff_control+1
9078   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9079         END IF;
9080 
9081      ELSIF(M_opinion_log_id is NOT NULL AND M_change_flag = 'F' ) THEN
9082 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9083 **********( get_all_org_cert_rec.proc_with_ineffective_controls  - 1 < 0 )*******************************/
9084 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9085   IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( get_all_org_cert_rec.proc_with_ineffective_controls  - 1 > 0 )   )  THEN
9086   	g_refresh_flag := 'Y';
9087   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9088 ELSE
9089 ****************/
9090 UPDATE AMW_FIN_ORG_EVAL_SUM SET
9091         last_update_date = sysdate,
9092         last_updated_by = fnd_global.user_id,
9093         last_update_login = fnd_global.conc_login_id,
9094         proc_with_ineffective_controls = greatest(0, proc_with_ineffective_controls - 1),
9095         proc_with_ineff_controls_prcnt = round( (greatest(0, proc_with_ineffective_controls - 1))/decode(nvl(total_number_of_procs, 0), 0, 1, total_number_of_procs), 2)*100
9096         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9097 	AND ORGANIZATION_ID  = p_org_id;
9098 --END IF;
9099 	IF p_org_id = fnd_profile.value('AMW_GLOBAL_ORG_ID') THEN
9100 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9101 **********( m_dashbd_proc_ineff_ctrl -1 < 0 ) *******************************/
9102 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9103   IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_dashbd_proc_ineff_ctrl -1 > 0 )   )  THEN
9104   	g_refresh_flag := 'Y';
9105   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9106 ELSE
9107 ***********/
9108  UPDATE amw_cert_dashboard_sum
9109              SET last_update_date = sysdate,
9110                  last_updated_by = fnd_global.user_id,
9111 	         last_update_login = fnd_global.conc_login_id,
9112 		 proc_ineff_control = greatest(0,proc_ineff_control-1)
9113   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9114 -- END IF;
9115         ELSE
9116 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9117 **********( m_dashbd_org_proc_ineff_ctrl -1 < 0 )*******************************/
9118 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9119   IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_dashbd_org_proc_ineff_ctrl -1 > 0 )  )  THEN
9120   	g_refresh_flag := 'Y';
9121   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9122 ELSE
9123 *************/
9124 UPDATE amw_cert_dashboard_sum
9125              SET last_update_date = sysdate,
9126                  last_updated_by = fnd_global.user_id,
9127 	         last_update_login = fnd_global.conc_login_id,
9128 		 org_proc_ineff_control = greatest(0, org_proc_ineff_control-1)
9129   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9130 --        END IF;
9131 END IF;
9132  	ELSIF(M_opinion_log_id is NOT NULL AND M_change_flag = 'B' ) THEN
9133 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9134 **********( get_all_org_cert_rec.proc_with_ineffective_controls  + 1  >  get_all_org_cert_rec.proc_verified) *******************************/
9135 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9136   IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( get_all_org_cert_rec.proc_with_ineffective_controls  + 1  <  get_all_org_cert_rec.proc_verified)  )  THEN
9137   	g_refresh_flag := 'Y';
9138   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9139 ELSE
9140 **************/
9141 UPDATE AMW_FIN_ORG_EVAL_SUM SET
9142         last_update_date = sysdate,
9143         last_updated_by = fnd_global.user_id,
9144         last_update_login = fnd_global.conc_login_id,
9145         proc_with_ineffective_controls = least(proc_with_ineffective_controls + 1, proc_verified),
9146         proc_with_ineff_controls_prcnt = round( (least(proc_with_ineffective_controls + 1, proc_verified) )/decode(nvl(total_number_of_procs, 0), 0, 1, total_number_of_procs), 2)*100
9147         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9148 	AND ORGANIZATION_ID  = p_org_id;
9149 --END IF;
9150 
9151 	IF p_org_id = fnd_profile.value('AMW_GLOBAL_ORG_ID') THEN
9152 	  UPDATE amw_cert_dashboard_sum
9153              SET last_update_date = sysdate,
9154                  last_updated_by = fnd_global.user_id,
9155 	         last_update_login = fnd_global.conc_login_id,
9156 		 proc_ineff_control = proc_ineff_control + 1
9157   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9158         ELSE
9159 	  UPDATE amw_cert_dashboard_sum
9160              SET last_update_date = sysdate,
9161                  last_updated_by = fnd_global.user_id,
9162 	         last_update_login = fnd_global.conc_login_id,
9163 		 org_proc_ineff_control = org_proc_ineff_control + 1
9164   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9165         END IF;
9166 
9167 	END IF; -- end of a list of ifs
9168 
9169 END LOOP; --end of amw_fin_org_eval_sum and amw_cert_dashboard_sum
9170 
9171 END IF;  --end of  p_action = 'EVALUATION'
9172 
9173 IF (p_action = 'CERTIFICATION') THEN
9174 
9175 FOR Get_all_items_Rec in Get_all_items(p_org_id , p_process_id) LOOP
9176 	 exit when Get_all_items %notfound;
9177 
9178     OPEN Get_old_cert_opinion_id(Get_all_items_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
9179     FETCH Get_old_cert_opinion_id INTO M_opinion_log_id;
9180     CLOSE Get_old_cert_opinion_id;
9181 
9182     Is_Eval_Change(
9183     		old_opinion_log_id  =>  M_opinion_log_id ,
9184     		new_opinion_log_id  =>  p_opinion_log_id,
9185     		x_change_flag	=> M_change_flag);
9186 
9187 	M_item_total_number_process := 0;
9188     	M_item_proc_pending_cert := 0;
9189     	M_item_proc_cert_with_issue := 0;
9190 
9191  OPEN Get_Cert_Item_Proc(Get_all_items_Rec.FIN_CERTIFICATION_ID, Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
9192    Get_all_items_Rec.FINANCIAL_ITEM_ID);
9193    FETCH Get_Cert_Item_Proc  INTO M_item_total_number_process, M_item_proc_pending_cert, M_item_proc_cert_with_issue;
9194    CLOSE Get_Cert_Item_Proc;
9195 
9196         IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  -- a new certification
9197           M_new_flag := 'Y';
9198 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9199 **********( m_item_proc_pending_cert  - 1 < 0 ) *******************************/
9200 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9201   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_item_proc_pending_cert  - 1 > 0 )   )  THEN
9202   	g_refresh_flag := 'Y';
9203   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
9204 ELSE
9205 ***********/
9206  UPDATE AMW_FIN_CERT_EVAL_SUM SET
9207         last_update_date = sysdate,
9208          last_updated_by = fnd_global.user_id,
9209         last_update_login = fnd_global.conc_login_id,
9210         proc_pending_certification =  greatest(0, proc_pending_certification - 1),
9211         procs_for_cert_done = least(procs_for_cert_done + 1,  total_number_of_processes ),
9212         pro_pending_cert_prcnt = round( (greatest(0,proc_pending_certification -1) )/decode(nvl(total_number_of_processes, 0), 0, 1, total_number_of_processes) ,2)*100
9213         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
9214 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
9215         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
9216         AND OBJECT_TYPE = 'FINANCIAL ITEM';
9217 --END IF;
9218         END IF;
9219 
9220 
9221         IF (M_change_flag = 'F'  and M_new_flag = 'N' ) THEN
9222 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9223 **********( m_item_proc_cert_with_issue  - 1 < 0 )  *******************************/
9224 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9225   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_item_proc_cert_with_issue  - 1 > 0 )    )  THEN
9226   	g_refresh_flag := 'Y';
9227   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
9228 ELSE
9229 *************/
9230       UPDATE AMW_FIN_CERT_EVAL_SUM SET
9231         last_update_date = sysdate,
9232          last_updated_by = fnd_global.user_id,
9233        last_update_login = fnd_global.conc_login_id,
9234         proc_certified_with_issues = least(0,proc_certified_with_issues -1),
9235         processes_with_issues_prcnt = round( (least(0, m_item_proc_cert_with_issue -1))/decode(nvl(m_item_total_number_process, 0), 0, 1, m_item_total_number_process), 2)*100
9236         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
9237 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
9238         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
9239         AND OBJECT_TYPE = 'FINANCIAL ITEM';
9240 --END IF;
9241 
9242 
9243         ELSIF (M_change_flag = 'B') THEN
9244 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9245 **********( m_item_proc_cert_with_issue  +  1 >  m_item_total_number_process )  *******************************/
9246 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9247   IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_item_proc_cert_with_issue  +  1 <  m_item_total_number_process )   )  THEN
9248   	g_refresh_flag := 'Y';
9249   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
9250 ELSE
9251 ****************/
9252  UPDATE AMW_FIN_CERT_EVAL_SUM SET
9253         last_update_date = sysdate,
9254          last_updated_by = fnd_global.user_id,
9255        last_update_login = fnd_global.conc_login_id,
9256         proc_certified_with_issues = least(proc_certified_with_issues + 1, total_number_of_processes ),
9257         processes_with_issues_prcnt = round( (least(m_item_proc_cert_with_issue + 1, total_number_of_processes))/decode(nvl(m_item_total_number_process, 0), 0, 1, m_item_total_number_process), 2)*100
9258         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
9259 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
9260         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
9261         AND OBJECT_TYPE = 'FINANCIAL ITEM';
9262 --END IF;
9263         END IF;
9264 
9265   END LOOP;
9266 
9267   FOR Get_all_accts_Rec IN Get_all_accts(p_org_id , p_process_id) LOOP
9268            exit when Get_all_accts%notfound;
9269 
9270      OPEN Get_old_cert_opinion_id(Get_all_accts_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
9271     FETCH Get_old_cert_opinion_id INTO M_opinion_log_id;
9272     CLOSE Get_old_cert_opinion_id;
9273 
9274 
9275 
9276     		M_acc_total_number_process := 0;
9277     		M_acc_proc_pending_cert := 0;
9278     		M_acc_proc_cert_with_issue := 0;
9279 
9280 OPEN Get_Cert_Acct_Proc(Get_all_accts_Rec.FIN_CERTIFICATION_ID, Get_all_accts_Rec.ACCOUNT_GROUP_ID,
9281 Get_all_accts_Rec.NATURAL_ACCOUNT_ID);
9282 FETCH Get_Cert_Acct_Proc  INTO  M_acc_total_number_process, M_acc_proc_pending_cert, M_acc_proc_cert_with_issue;
9283 CLOSE Get_Cert_Acct_Proc;
9284 
9285 	Is_Eval_Change(
9286     		old_opinion_log_id  =>  M_opinion_log_id ,
9287     		new_opinion_log_id  =>  p_opinion_log_id,
9288     		x_change_flag	=> M_change_flag);
9289 
9290         IF(M_opinion_log_id IS NULL OR M_opinion_log_id = 0 ) THEN  --  a new certification
9291          M_new_flag := 'Y';
9292 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9293 **********(m_acc_proc_pending_cert - 1 < 0 )   *******************************/
9294  /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9295   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_acc_proc_pending_cert - 1 > 0 )   )  THEN
9296   	g_refresh_flag := 'Y';
9297   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
9298 ELSE
9299 ***************/
9300 UPDATE AMW_FIN_CERT_EVAL_SUM SET
9301         last_update_date = sysdate,
9302          last_updated_by = fnd_global.user_id,
9303        last_update_login = fnd_global.conc_login_id,
9304         proc_pending_certification =  greatest(0, proc_pending_certification - 1),
9305         procs_for_cert_done = least(procs_for_cert_done + 1,total_number_of_processes),
9306         pro_pending_cert_prcnt = round( (greatest(0,proc_pending_certification -1))/decode(nvl(total_number_of_processes, 0), 0, 1, total_number_of_processes), 2)*100
9307         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
9308        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
9309        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
9310        AND OBJECT_TYPE = 'ACCOUNT';
9311         END IF;
9312 -- END IF;
9313         IF (M_change_flag = 'F'  and M_new_flag = 'N' ) THEN
9314 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9315 **********(m_acc_proc_cert_with_issue - 1 < 0 )    *******************************/
9316 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9317   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_acc_proc_cert_with_issue - 1 > 0 )   )  THEN
9318   	g_refresh_flag := 'Y';
9319   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
9320 ELSE
9321 ***********/
9322  UPDATE AMW_FIN_CERT_EVAL_SUM SET
9323          last_update_date = sysdate,
9324          last_updated_by = fnd_global.user_id,
9325         last_update_login = fnd_global.conc_login_id,
9326         proc_certified_with_issues = greatest(0,proc_certified_with_issues -1),
9327         processes_with_issues_prcnt = round( (greatest(0,m_acc_proc_cert_with_issue -1))/decode(nvl(m_acc_total_number_process, 0), 0, 1, m_acc_total_number_process), 2)*100
9328        WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
9329        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
9330        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
9331        AND OBJECT_TYPE = 'ACCOUNT';
9332 --END IF;
9333 
9334         ELSIF (M_change_flag = 'B') THEN
9335 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9336 **********(m_acc_proc_cert_with_issue + 1 > m_acc_total_number_process ) *******************************/
9337 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9338   IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_acc_proc_cert_with_issue + 1 < m_acc_total_number_process )    )  THEN
9339   	g_refresh_flag := 'Y';
9340   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
9341 ELSE
9342 ********************/
9343 UPDATE AMW_FIN_CERT_EVAL_SUM SET
9344          last_update_date = sysdate,
9345          last_updated_by = fnd_global.user_id,
9346        last_update_login = fnd_global.conc_login_id,
9347         proc_certified_with_issues = least(proc_certified_with_issues + 1,total_number_of_processes),
9348         processes_with_issues_prcnt = round( (least(m_acc_proc_cert_with_issue + 1, total_number_of_processes))/decode(nvl(m_acc_total_number_process, 0), 0, 1, m_acc_total_number_process), 2)*100
9349       WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
9350        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
9351        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
9352        AND OBJECT_TYPE = 'ACCOUNT';
9353 -- END IF;
9354 
9355         END IF;
9356 
9357  END LOOP; --end of account loop
9358 
9359 
9360   --update amw_fin_org_eval_sum and amw_cert_dashboard_sum tables
9361  FOR Get_All_Org_Cert_Rec in Get_All_Org_Cert(p_org_id) LOOP
9362  	exit when Get_All_Org_Cert %notfound;
9363 
9364     OPEN Get_old_cert_opinion_id(Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID, p_org_id, p_process_id);
9365     FETCH Get_old_cert_opinion_id INTO M_opinion_log_id;
9366     CLOSE Get_old_cert_opinion_id;
9367 
9368       Is_Eval_Change(
9369     		old_opinion_log_id  =>  M_opinion_log_id ,
9370     		new_opinion_log_id  =>  p_opinion_log_id,
9371     		x_change_flag	=> M_change_flag);
9372 
9373     OPEN Get_Dashboard_Info(Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID);
9374     FETCH Get_Dashboard_Info INTO M_dashbd_proc_not_cert,M_dashbd_proc_cert_issue,
9375 				  M_dashbd_org_proc_not_cert,M_dashbd_org_proc_cert_issue,
9376 				  M_dashbd_proc_ineff_ctrl,M_dashbd_org_proc_ineff_ctrl;
9377     CLOSE Get_Dashboard_Info;
9378 
9379 
9380      IF(M_opinion_log_id IS NULL AND M_change_flag = 'F' ) THEN
9381 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9382 **********(( get_all_org_cert_rec.proc_certified  + 1  >  get_all_org_cert_rec.total_number_of_procs)
9383         or (get_all_org_cert_rec.proc_pending_certification -1 < 0 ) )  *******************************/
9384 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9385   IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(( get_all_org_cert_rec.proc_certified  + 1  <  get_all_org_cert_rec.total_number_of_procs)
9386         AND  (get_all_org_cert_rec.proc_pending_certification -1 > 0 ) )    )  THEN
9387   	g_refresh_flag := 'Y';
9388   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9389 ELSE
9390 *****************/
9391 UPDATE AMW_FIN_ORG_EVAL_SUM SET
9392         last_update_date = sysdate,
9393         last_updated_by = fnd_global.user_id,
9394         last_update_login = fnd_global.conc_login_id,
9395         proc_certified =  least(proc_certified + 1,total_number_of_procs ),
9396         proc_pending_certification = greatest(0, proc_pending_certification - 1)
9397         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9398 	AND ORGANIZATION_ID  = p_org_id;
9399 --END IF;
9400 
9401    ELSIF(M_opinion_log_id is NULL AND M_change_flag = 'B' ) THEN
9402 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9403 **********(( get_all_org_cert_rec.proc_certified  + 1  >  get_all_org_cert_rec.total_number_of_procs)
9404         or ( get_all_org_cert_rec.proc_certified_with_issues + 1 > get_all_org_cert_rec.proc_certified)
9405         or ( get_all_org_cert_rec.proc_pending_certification - 1 < 0 ))*******************************/
9406 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9407 IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(( get_all_org_cert_rec.proc_certified  + 1  <  get_all_org_cert_rec.total_number_of_procs)
9408         and ( get_all_org_cert_rec.proc_certified_with_issues + 1 < get_all_org_cert_rec.proc_certified)
9409         and ( get_all_org_cert_rec.proc_pending_certification - 1 >  0 ))    )  THEN
9410   	g_refresh_flag := 'Y';
9411   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9412 ELSE
9413 **************/
9414 UPDATE AMW_FIN_ORG_EVAL_SUM SET
9415         last_update_date = sysdate,
9416         last_updated_by = fnd_global.user_id,
9417         last_update_login = fnd_global.conc_login_id,
9418         proc_certified =  least(proc_certified + 1,total_number_of_procs),
9419         proc_pending_certification = greatest(0, proc_pending_certification - 1),
9420         proc_certified_with_issues = least(proc_certified_with_issues + 1, proc_certified),
9421         proc_pending_cert_prcnt = round( (least(proc_pending_certification + 1, total_number_of_procs))/decode(nvl(total_number_of_procs, 0), 0, 1, total_number_of_procs), 2)*100,
9422         processes_with_issues_prcnt = round( (least(proc_certified_with_issues + 1, proc_certified))/decode(nvl(total_number_of_procs, 0), 0, 1, total_number_of_procs), 2)*100
9423         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9424 	AND ORGANIZATION_ID  = p_org_id;
9425 --END IF;
9426 	IF p_org_id = fnd_profile.value('AMW_GLOBAL_ORG_ID') THEN
9427 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9428 **********( M_dashbd_proc_not_cert -1 < 0 )*******************************/
9429 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9430 IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( M_dashbd_proc_not_cert -1 > 0 ) )  THEN
9431   	g_refresh_flag := 'Y';
9432   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9433 ELSE
9434 *******************/
9435   UPDATE amw_cert_dashboard_sum
9436              SET last_update_date = sysdate,
9437                  last_updated_by = fnd_global.user_id,
9438 	         last_update_login = fnd_global.conc_login_id,
9439 		 processes_cert_issues = processes_cert_issues +1,
9440 		 processes_not_cert = processes_not_cert - 1
9441   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9442 -- END IF;
9443         ELSE
9444 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9445 **********( m_dashbd_org_proc_not_cert -1 < 0 ) *******************************/
9446 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9447 IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_dashbd_org_proc_not_cert -1 > 0 )  )  THEN
9448   	g_refresh_flag := 'Y';
9449   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9450 ELSE
9451 ***************/
9452 UPDATE amw_cert_dashboard_sum
9453              SET last_update_date = sysdate,
9454                  last_updated_by = fnd_global.user_id,
9455 	         last_update_login = fnd_global.conc_login_id,
9456 		 org_process_cert_issues = org_process_cert_issues+1,
9457 		 org_process_not_cert = greatest(0, org_process_not_cert -1)
9458   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9459 --        END IF;
9460 END IF;
9461      ELSIF(M_opinion_log_id is NOT NULL AND M_change_flag = 'F' ) THEN
9462 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9463 **********(get_all_org_cert_rec.proc_certified_with_issues - 1 < 0 )*******************************/
9464 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9465 IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(get_all_org_cert_rec.proc_certified_with_issues - 1 > 0 ) )  THEN
9466   	g_refresh_flag := 'Y';
9467   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9468 ELSE
9469 *************/
9470 UPDATE AMW_FIN_ORG_EVAL_SUM SET
9471         last_update_date = sysdate,
9472         last_updated_by = fnd_global.user_id,
9473         last_update_login = fnd_global.conc_login_id,
9474         proc_certified_with_issues = greatest(0, proc_certified_with_issues -  1),
9475         processes_with_issues_prcnt = round( (greatest(0, proc_certified_with_issues - 1))/decode(nvl(total_number_of_procs, 0), 0, 1, total_number_of_procs), 2)*100
9476         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9477 	AND ORGANIZATION_ID  = p_org_id;
9478 --END IF;
9479 	IF p_org_id = fnd_profile.value('AMW_GLOBAL_ORG_ID') THEN
9480 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9481 **********( m_dashbd_proc_cert_issue -1 < 0 ) *******************************/
9482 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9483 IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_dashbd_proc_cert_issue -1 > 0 )  )  THEN
9484   	g_refresh_flag := 'Y';
9485   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9486 ELSE
9487 *****************/
9488  UPDATE amw_cert_dashboard_sum
9489              set last_update_date = sysdate,
9490                  last_updated_by = fnd_global.user_id,
9491 	         last_update_login = fnd_global.conc_login_id,
9492 		 processes_cert_issues = greatest(0, processes_cert_issues  - 1)
9493   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9494 --END IF;
9495         ELSE
9496 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9497 **********( m_dashbd_org_proc_cert_issue -1 < 0 ) *******************************/
9498 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9499 IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR( m_dashbd_org_proc_cert_issue -1 > 0 )   )  THEN
9500   	g_refresh_flag := 'Y';
9501   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9502 ELSE
9503 ****************/
9504 UPDATE amw_cert_dashboard_sum
9505              set last_update_date = sysdate,
9506                  last_updated_by = fnd_global.user_id,
9507 	         last_update_login = fnd_global.conc_login_id,
9508 		 org_process_cert_issues = greatest(0, org_process_cert_issues - 1)
9509   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9510 --        END IF;
9511 END IF;
9512  	ELSIF(M_opinion_log_id is NOT NULL AND M_change_flag = 'B' ) THEN
9513 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9514 **********(get_all_org_cert_rec.proc_certified_with_issues +  1 < get_all_org_cert_rec.proc_certified ) *******************************/
9515 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9516 IF NOT ( (m_certification_list.exists(get_all_org_cert_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(get_all_org_cert_rec.proc_certified_with_issues +  1 < get_all_org_cert_rec.proc_certified ) )  THEN
9517   	g_refresh_flag := 'Y';
9518   	m_certification_list(get_all_org_cert_rec.fin_certification_id) := get_all_org_cert_rec.fin_certification_id;
9519 ELSE
9520 ***************/
9521  UPDATE AMW_FIN_ORG_EVAL_SUM SET
9522         last_update_date = sysdate,
9523         last_updated_by = fnd_global.user_id,
9524         last_update_login = fnd_global.conc_login_id,
9525         proc_certified_with_issues = least(proc_certified_with_issues +  1,  total_number_of_procs),
9526         processes_with_issues_prcnt = round( (least(proc_certified_with_issues + 1, total_number_of_procs))/decode(nvl(total_number_of_procs, 0), 0, 1, total_number_of_procs), 2)*100
9527         WHERE FIN_CERTIFICATION_ID = Get_All_Org_Cert_Rec.FIN_CERTIFICATION_ID
9528 	AND ORGANIZATION_ID  = p_org_id;
9529 --END IF;
9530 	IF p_org_id = fnd_profile.value('AMW_GLOBAL_ORG_ID') THEN
9531 	  UPDATE amw_cert_dashboard_sum
9532              SET last_update_date = sysdate,
9533                  last_updated_by = fnd_global.user_id,
9534 	         last_update_login = fnd_global.conc_login_id,
9535 		 processes_cert_issues = processes_cert_issues  + 1
9536   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9537         ELSE
9538 	  UPDATE amw_cert_dashboard_sum
9539              SET last_update_date = sysdate,
9540                  last_updated_by = fnd_global.user_id,
9541 	         last_update_login = fnd_global.conc_login_id,
9542 		 org_process_cert_issues = greatest(0, org_process_cert_issues - 1)
9543   	   WHERE certification_id = get_all_org_cert_rec.fin_certification_id;
9544         END IF;
9545 
9546 	END IF; -- end of a list of ifs
9547 
9548  END LOOP; --end of amw_fin_org_eval_sum and amw_cert_dashboard_sum
9549 
9550  --update amw_fin_process_eval_sum table
9551  FOR Get_parent_process_Rec in Get_parent_process(p_opinion_log_id, p_org_id, p_process_id ) LOOP
9552  	exit when Get_parent_process %notfound;
9553 
9554  OPEN Get_old_cert_opinion_id(Get_parent_process_Rec.fin_certification_id, p_org_id, p_process_id);
9555     FETCH Get_old_cert_opinion_id INTO M_opinion_log_id;
9556     CLOSE Get_old_cert_opinion_id;
9557 
9558 IF(m_opinion_log_id IS NULL OR m_opinion_log_id = 0 ) THEN -- a new certification
9559 /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b  *******************************************
9560 **********(( get_all_org_cert_rec.number_of_sub_procs_certified  + 1  >  get_all_org_cert_rec.total_number_of_procs) ********/
9561 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9562   IF NOT ( (m_certification_list.exists(get_parent_process_rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR (get_parent_process_rec.number_of_sub_procs_certified  + 1  <  get_parent_process_rec.total_number_of_sub_procs) )THEN
9563   	g_refresh_flag := 'Y';
9564   	m_certification_list(get_parent_process_rec.fin_certification_id) := get_parent_process_rec.fin_certification_id;
9565 ELSE
9566 ***************/
9567 UPDATE amw_fin_process_eval_sum
9568 SET
9569         last_update_date = sysdate,
9570         last_updated_by = fnd_global.user_id,
9571         last_update_login = fnd_global.conc_login_id,
9572        number_of_sub_procs_certified = number_of_sub_procs_certified + 1
9573         WHERE fin_certification_id = get_parent_process_rec.fin_certification_id
9574 	AND organization_id  = get_parent_process_rec.organization_id
9575 	AND process_id = get_parent_process_rec.process_id;
9576 --END IF;
9577 END IF;
9578 
9579  END LOOP;
9580 
9581   END IF; --end of p_action = CERTIFICATION
9582 
9583 
9584   x_return_status := FND_API.G_RET_STS_SUCCESS;
9585 
9586   EXCEPTION
9587      WHEN NO_DATA_FOUND THEN
9588      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
9589      x_return_status := FND_API.G_RET_STS_ERROR;
9590      x_msg_count := 1;
9591      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
9592       WHEN OTHERS THEN
9593        ROLLBACK TO PROCESS_CHANGE_HANDLER;
9594       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
9595       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
9596         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9597         x_msg_count := 1;
9598         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
9599                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
9600                 FND_MSG_PUB.Count_And_Get(
9601                 p_encoded =>  FND_API.G_FALSE,
9602                 p_count   =>  x_msg_count,
9603                 p_data    =>  x_msg_data);
9604                 RETURN;
9605 
9606 END PROCESS_CHANGE_HANDLER;
9607 
9608 
9609 PROCEDURE ORGANIZATION_CHANGE_HANDLER
9610 (
9611 p_api_version_number        IN   NUMBER   := 1.0,
9612 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
9613 p_commit                    IN   VARCHAR2 := FND_API.g_false,
9614 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
9615 p_org_id 		IN 	NUMBER,
9616 p_opinion_log_id 	IN	NUMBER,
9617 p_action 		IN 	VARCHAR2,
9618 x_return_status             OUT  nocopy VARCHAR2,
9619 x_msg_count                 OUT  nocopy NUMBER,
9620 x_msg_data                  OUT  nocopy VARCHAR2
9621 )
9622 IS
9623 ---CURSOR TO GET ALL OF FINANCIAL ITEMS WHICH RELATE TO THIS ORGANIZATION
9624 CURSOR Get_all_items( l_org_id number)IS
9625 SELECT DISTINCT fin.FIN_CERTIFICATION_ID, fin.STATEMENT_GROUP_ID, fin.FINANCIAL_STATEMENT_ID, fin.FINANCIAL_ITEM_ID
9626 FROM AMW_FIN_CERT_SCOPE fin,
9627      AMW_CERTIFICATION_B cert
9628 WHERE fin.ORGANIZATION_ID = l_org_id
9629 AND  fin.FINANCIAL_ITEM_ID IS NOT NULL
9630 AND  fin.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
9631 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
9632 
9633 ---CURSOR TO GET ALL OF ACCOUNTS WHICH RELATE TO THIS ORGANIZATION
9634 CURSOR 	Get_all_accts (l_org_id number)IS
9635 SELECT DISTINCT fin.fin_certification_id, fin.account_group_id, fin.natural_account_id
9636 FROM AMW_FIN_CERT_SCOPE fin,
9637      AMW_CERTIFICATION_B cert
9638 WHERE fin.ORGANIZATION_ID = l_org_id
9639 AND   fin.natural_account_id is not null
9640 AND  fin.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
9641 AND  cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
9642 
9643 
9644 --CURSOR TO GET ALL OF FINANCIAL CERTIFICATION WHICH RELATE THIS ORGANIZATION
9645 CURSOR Get_all_fin_cert(l_org_id number) IS
9646 SELECT org.FIN_CERTIFICATION_ID
9647 FROM AMW_FIN_ORG_EVAL_SUM org,
9648      AMW_CERTIFICATION_B cert
9649 WHERE org.organization_id = l_org_id
9650 AND org.FIN_CERTIFICATION_ID = cert.CERTIFICATION_ID
9651 AND cert.CERTIFICATION_STATUS IN ('ACTIVE', 'DRAFT');
9652 
9653 
9654 --CURSOR TO GET OLD EVAL OPINION LOG ID
9655 CURSOR Get_old_opinion_id(l_cert_id number, l_org_id number) IS
9656 SELECT DISTINCT EVAL_OPINION_LOG_ID
9657 FROM AMW_FIN_ORG_EVAL_SUM
9658 WHERE  FIN_CERTIFICATION_ID = l_cert_id
9659 AND ORGANIZATION_ID = l_org_id;
9660 
9661 --CURSOR TO GET OLD CERT OPINION LOG ID
9662 CURSOR Get_old_cert_opinion_id(l_cert_id number, l_org_id number) IS
9663 SELECT DISTINCT CERT_OPINION_LOG_ID
9664 FROM AMW_FIN_ORG_EVAL_SUM
9665 WHERE  FIN_CERTIFICATION_ID = l_cert_id
9666 AND ORGANIZATION_ID = l_org_id;
9667 
9668 --CURSOR TO GET ORGANIZATION INFORMATION FOR ITEM
9669     CURSOR Get_Item_Org(l_cert_id number, l_stmt_id number, l_item_id number) IS
9670     SELECT ORG_WITH_INEFFECTIVE_CONTROLS, ORGS_EVALUATED, TOTAL_NUMBER_OF_ORGS
9671     FROM AMW_FIN_CERT_EVAL_SUM
9672     WHERE FIN_CERTIFICATION_ID = l_cert_id
9673     AND FINANCIAL_STATEMENT_ID = l_stmt_id
9674     AND FINANCIAL_ITEM_ID = l_item_id
9675     AND OBJECT_TYPE = 'FINANCIAL ITEM';
9676 
9677 --CURSOR TO GET ORGANIZATION INFORMATION FOR ACCOUNT
9678     CURSOR Get_Acc_Org(l_cert_id number, l_acct_group_id  number, l_acct_id number) IS
9679       SELECT ORG_WITH_INEFFECTIVE_CONTROLS, ORGS_EVALUATED, TOTAL_NUMBER_OF_ORGS
9680     FROM AMW_FIN_CERT_EVAL_SUM
9681     WHERE FIN_CERTIFICATION_ID =  l_cert_id
9682        AND ACCOUNT_GROUP_ID = l_acct_group_id
9683        AND NATURAL_ACCOUNT_ID = l_acct_id
9684        AND OBJECT_TYPE = 'ACCOUNT';
9685 
9686 
9687 M_item_org_with_ineff_ctrl number;
9688 M_item_org_evaluated number;
9689 M_item_total_orgs number;
9690 
9691 M_item_proc_pending_cert  number;
9692 M_item_total_number_process number;
9693 M_item_proc_cert_with_issue number;
9694 
9695 M_acc_org_with_ineff_ctrl number;
9696 M_acc_org_evaluated number;
9697 M_acc_total_orgs number;
9698 
9699 M_acc_proc_pending_cert  number;
9700 M_acc_total_number_process number;
9701 M_acc_proc_cert_with_issue number;
9702 M_acc_proc_with_ineff_ctrl number;
9703 
9704 M_opinion_log_id AMW_OPINIONS_LOG.OPINION_LOG_ID%TYPE;
9705 
9706 M_change_flag VARCHAR2(1);
9707 M_new_flag VARCHAR2(1) := 'N';
9708 
9709 l_error_message varchar2(4000);
9710 
9711 
9712 l_api_name           CONSTANT VARCHAR2(30) := 'ORGANIZATION_CHANGE_HANDLER';
9713 l_api_version_number CONSTANT NUMBER       := 1.0;
9714 
9715 l_return_status VARCHAR2(32767);
9716 l_msg_count NUMBER;
9717 l_msg_data VARCHAR2(32767);
9718 
9719 BEGIN
9720 
9721 SAVEPOINT ORGANIZATION_CHANGE_HANDLER;
9722 
9723  -- Standard call to check for call compatibility.
9724         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
9725                                              p_api_version_number,
9726                                              l_api_name,
9727                                              G_PKG_NAME)
9728         THEN
9729                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9730         END IF;
9731 
9732         -- Initialize message list if p_init_msg_list is set to TRUE.
9733         IF FND_API.to_Boolean( p_init_msg_list )
9734         THEN
9735                 FND_MSG_PUB.initialize;
9736         END IF;
9737 
9738         -- Initialize API return status to SUCCESS
9739         x_return_status := FND_API.G_RET_STS_SUCCESS;
9740 
9741 
9742 
9743 --modify fin_cert_eval summary table
9744 IF (p_action = 'EVALUATION') THEN
9745 FOR Get_all_items_Rec in Get_all_items(p_org_id) LOOP
9746 	 exit when Get_all_items %notfound;
9747 
9748 
9749     OPEN Get_old_opinion_id(Get_all_items_Rec.FIN_CERTIFICATION_ID, p_org_id);
9750     FETCH Get_old_opinion_id INTO M_opinion_log_id;
9751     CLOSE Get_old_opinion_id;
9752 
9753     OPEN Get_Item_Org(Get_all_items_Rec.FIN_CERTIFICATION_ID, Get_all_items_Rec.FINANCIAL_STATEMENT_ID,
9754     Get_all_items_Rec.FINANCIAL_ITEM_ID);
9755     FETCH Get_Item_Org  INTO M_item_org_with_ineff_ctrl, M_item_org_evaluated, M_item_total_orgs;
9756     CLOSE Get_Item_Org;
9757 
9758 	Is_Eval_Change (
9759     		old_opinion_log_id  => M_opinion_log_id ,
9760     		new_opinion_log_id  => p_opinion_log_id,
9761     		x_change_flag	=> M_change_flag);
9762 
9763 
9764     IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  -- a new evaluation
9765          M_new_flag := 'Y';
9766  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9767 **********(m_item_org_evaluated + 1 > m_item_total_orgs)*******************************/
9768 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9769 IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_item_org_evaluated + 1 < m_item_total_orgs) )  THEN
9770   	g_refresh_flag := 'Y';
9771   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
9772 ELSE
9773 ****************/
9774 UPDATE AMW_FIN_CERT_EVAL_SUM
9775         SET
9776         orgs_evaluated = least(orgs_evaluated + 1, total_number_of_orgs),
9777         last_update_date = sysdate,
9778         last_updated_by = fnd_global.user_id,
9779         last_update_login = fnd_global.conc_login_id
9780         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
9781         AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
9782         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
9783         AND OBJECT_TYPE = 'FINANCIAL ITEM';
9784 --    END IF;
9785 END IF;
9786         IF (M_change_flag = 'B') THEN
9787  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9788 **********(m_item_org_with_ineff_ctrl + 1 >  m_item_org_evaluated) *******************************/
9789 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9790 IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_item_org_with_ineff_ctrl + 1 <  m_item_org_evaluated)  )  THEN
9791   	g_refresh_flag := 'Y';
9792   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
9793 ELSE
9794 *************/
9795 UPDATE AMW_FIN_CERT_EVAL_SUM
9796         SET
9797         org_with_ineffective_controls = least(org_with_ineffective_controls + 1, orgs_evaluated),
9798         org_with_ineff_controls_prcnt = round( (least(org_with_ineffective_controls + 1, orgs_evaluated, total_number_of_orgs) )/decode(nvl(total_number_of_orgs, 0), 0, 1, total_number_of_orgs), 2) * 100,
9799         last_update_date = sysdate,
9800         last_updated_by = fnd_global.user_id,
9801         last_update_login = fnd_global.conc_login_id
9802         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
9803        AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
9804         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
9805         AND OBJECT_TYPE = 'FINANCIAL ITEM';
9806 -- END IF;
9807         ELSIF (M_change_flag = 'F'  and M_new_flag = 'N')  THEN
9808  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9809 **********(m_item_org_with_ineff_ctrl - 1 < 0 ) *******************************/
9810 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9811 IF NOT ( (m_certification_list.exists(Get_all_items_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_item_org_with_ineff_ctrl - 1 > 0 )  )  THEN
9812   	g_refresh_flag := 'Y';
9813   	m_certification_list(Get_all_items_Rec.fin_certification_id) := Get_all_items_Rec.fin_certification_id;
9814 ELSE
9815 ***************/
9816 UPDATE AMW_FIN_CERT_EVAL_SUM
9817         SET
9818         org_with_ineffective_controls = greatest(0,org_with_ineffective_controls - 1),
9819         org_with_ineff_controls_prcnt = round( (greatest(0, m_item_org_with_ineff_ctrl - 1) )/decode(nvl(m_item_total_orgs, 0), 0, 1, m_item_total_orgs), 2) * 100,
9820         last_update_date = sysdate,
9821         last_updated_by = fnd_global.user_id,
9822         last_update_login = fnd_global.conc_login_id
9823         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
9824         AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
9825         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
9826         AND OBJECT_TYPE = 'FINANCIAL ITEM';
9827 --END IF;
9828         END IF;
9829 
9830     END LOOP;
9831 
9832     FOR Get_all_accts_Rec IN Get_all_accts(p_org_id) LOOP
9833            exit when Get_all_accts%notfound;
9834 
9835      OPEN Get_old_opinion_id(Get_all_accts_Rec.FIN_CERTIFICATION_ID, p_org_id);
9836     FETCH Get_old_opinion_id INTO M_opinion_log_id;
9837     CLOSE Get_old_opinion_id;
9838 
9839 
9840 
9841    OPEN Get_Acc_Org(Get_all_accts_Rec.FIN_CERTIFICATION_ID, Get_all_accts_Rec.ACCOUNT_GROUP_ID,
9842    Get_all_accts_Rec.NATURAL_ACCOUNT_ID);
9843    FETCH Get_Acc_Org  INTO M_acc_org_with_ineff_ctrl, M_acc_org_evaluated, M_acc_total_orgs;
9844    CLOSE Get_Acc_Org;
9845 
9846    Is_Eval_Change(
9847     		old_opinion_log_id  =>  M_opinion_log_id ,
9848     		new_opinion_log_id  =>  p_opinion_log_id,
9849     		x_change_flag	=> M_change_flag);
9850 
9851         IF(M_opinion_log_id = 0 OR M_opinion_log_id IS NULL) THEN  --  a new evaluation
9852          M_new_flag := 'Y';
9853  /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9854 **********(m_acc_org_evaluated + 1 > m_acc_total_orgs)  *******************************/
9855 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9856 IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_acc_org_evaluated + 1 < m_acc_total_orgs)   )  THEN
9857   	g_refresh_flag := 'Y';
9858   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
9859 ELSE
9860 ****************/
9861    UPDATE AMW_FIN_CERT_EVAL_SUM
9862         SET
9863         orgs_evaluated = least(orgs_evaluated + 1, total_number_of_orgs),
9864         last_update_date = sysdate,
9865         last_updated_by = fnd_global.user_id,
9866         last_update_login = fnd_global.conc_login_id
9867         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
9868        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
9869        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
9870        AND OBJECT_TYPE = 'ACCOUNT';
9871 --    END IF;
9872 END IF;
9873         IF (M_change_flag = 'B') THEN
9874   /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9875 **********(m_acc_org_with_ineff_ctrl + 1 > m_acc_org_evaluated) *******************************/
9876 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9877 IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_acc_org_with_ineff_ctrl + 1 < m_acc_org_evaluated)  )  THEN
9878   	g_refresh_flag := 'Y';
9879   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
9880 ELSE
9881 **************/
9882   UPDATE AMW_FIN_CERT_EVAL_SUM
9883         SET
9884         org_with_ineffective_controls = least(org_with_ineffective_controls + 1, orgs_evaluated),
9885         proc_with_ineff_controls_prcnt = round( (least(m_acc_org_with_ineff_ctrl + 1, orgs_evaluated))/decode(nvl(m_acc_total_orgs, 0), 0, 1, m_acc_total_orgs), 2) * 100,
9886         last_update_date = sysdate,
9887         last_updated_by = fnd_global.user_id,
9888         last_update_login = fnd_global.conc_login_id
9889         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
9890        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
9891        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
9892        AND OBJECT_TYPE = 'ACCOUNT';
9893 -- END IF;
9894         ELSIF (M_change_flag = 'F'  and M_new_flag = 'N' )  THEN
9895    /********* If the display format is a/b/c, then a >= 0 and b>= a and c>= b
9896 **********(m_acc_org_with_ineff_ctrl - 1 < 0 )   *******************************/
9897 /********* remove 'IF' logic now because the refresh logic is disabled temporarily
9898 IF NOT ( (m_certification_list.exists(Get_all_accts_Rec.fin_certification_id)) OR (g_refresh_flag = 'Y') OR(m_acc_org_with_ineff_ctrl - 1 > 0 )    )  THEN
9899   	g_refresh_flag := 'Y';
9900   	m_certification_list(Get_all_accts_Rec.fin_certification_id) := Get_all_accts_Rec.fin_certification_id;
9901 ELSE
9902 *******************/
9903  UPDATE AMW_FIN_CERT_EVAL_SUM
9904         SET
9905         org_with_ineffective_controls = greatest(0, org_with_ineffective_controls - 1),
9906         proc_with_ineff_controls_prcnt = round( (m_acc_org_with_ineff_ctrl - 1)/decode(nvl(m_acc_total_orgs, 0), 0, 1, m_acc_total_orgs), 2) * 100,
9907         last_update_date = sysdate,
9908         last_updated_by = fnd_global.user_id,
9909         last_update_login = fnd_global.conc_login_id
9910         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
9911         AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
9912         AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
9913         AND OBJECT_TYPE = 'ACCOUNT';
9914 --END IF;
9915         END IF;
9916 
9917 
9918  END LOOP;
9919 
9920 END IF;
9921 
9922 /**** Since we don't store pending or certified with issue informaton about organization in fin-cert
9923 ****** this part is saved for future use **************/
9924 IF (p_action = 'CERTIFICATION') THEN
9925  null;
9926 /**************
9927 
9928 FOR Get_all_items_Rec in Get_all_items(p_org_id) LOOP
9929 	 exit when Get_all_items %notfound;
9930 
9931 IF(M_opinion_log_id IS NULL OR M_opinion_log_id = 0 ) THEN -- a new certification
9932          M_new_flag := 'Y';
9933 
9934         UPDATE AMW_FIN_CERT_EVAL_SUM SET
9935         LAST_UPDATE_DATE = sysdate,
9936          last_updated_by = fnd_global.user_id,
9937         last_update_login = fnd_global.conc_login_id,
9938         orgs_for_cert_done =  least(orgs_for_cert_done + 1, total_number_of_orgs)
9939         WHERE FIN_CERTIFICATION_ID = Get_all_items_Rec.FIN_CERTIFICATION_ID
9940 	AND FINANCIAL_STATEMENT_ID = Get_all_items_Rec.FINANCIAL_STATEMENT_ID
9941         AND FINANCIAL_ITEM_ID = Get_all_items_Rec.FINANCIAL_ITEM_ID
9942         AND OBJECT_TYPE = 'FINANCIAL ITEM';
9943         END IF;
9944 
9945   END LOOP;
9946 
9947   FOR Get_all_accts_Rec IN Get_all_accts(p_org_id) LOOP
9948            exit when Get_all_accts%notfound;
9949 
9950 
9951         IF(M_opinion_log_id IS NULL OR M_opinion_log_id = 0 ) THEN  --new certification
9952     		 M_new_flag := 'Y';
9953 
9954         UPDATE AMW_FIN_CERT_EVAL_SUM SET
9955         LAST_UPDATE_DATE = sysdate,
9956          last_updated_by = fnd_global.user_id,
9957         last_update_login = fnd_global.conc_login_id,
9958         orgs_for_cert_done =  least(orgs_for_cert_done + 1, total_number_of_orgs)
9959         WHERE FIN_CERTIFICATION_ID = Get_all_accts_Rec.FIN_CERTIFICATION_ID
9960        AND ACCOUNT_GROUP_ID = Get_all_accts_Rec.ACCOUNT_GROUP_ID
9961        AND NATURAL_ACCOUNT_ID = Get_all_accts_Rec.NATURAL_ACCOUNT_ID
9962        AND OBJECT_TYPE = 'ACCOUNT';
9963         END IF;
9964 
9965  END LOOP; --end of account loop
9966    ******************/
9967 
9968   END IF; --end of p_action = CERTIFICATION
9969 
9970 
9971  -- nothing need to modify in process_eval summary tables
9972 
9973  ---nothing need to modify in dashboard, org_eval summary tables
9974 
9975   x_return_status := FND_API.G_RET_STS_SUCCESS;
9976 
9977 EXCEPTION
9978      WHEN NO_DATA_FOUND THEN
9979      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
9980      x_return_status := FND_API.G_RET_STS_ERROR;
9981      x_msg_count := 1;
9982      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
9983       WHEN OTHERS THEN
9984        ROLLBACK TO ORGANIZATION_CHANGE_HANDLER;
9985       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
9986       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
9987         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9988         x_msg_count := 1;
9989         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
9990                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
9991                 FND_MSG_PUB.Count_And_Get(
9992                 p_encoded =>  FND_API.G_FALSE,
9993                 p_count   =>  x_msg_count,
9994                 p_data    =>  x_msg_data);
9995                 RETURN;
9996 
9997 END ORGANIZATION_CHANGE_HANDLER;
9998 
9999 ---------------------------------The following procedures are only for migration purpose------------
10000 ---------------------------------name convention is the regular procedure name_M ------------------
10001 PROCEDURE Populate_Fin_Risk_Ass_Sum_M(
10002 p_api_version_number        IN   NUMBER   := 1.0,
10003 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
10004 p_commit                    IN   VARCHAR2 := FND_API.g_false,
10005 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
10006 p_certification_id  IN       NUMBER,
10007 x_return_status             OUT  nocopy VARCHAR2,
10008 x_msg_count                 OUT  nocopy NUMBER,
10009 x_msg_data                  OUT  nocopy VARCHAR2
10010 ) IS
10011 
10012 CURSOR c_finrisks IS
10013 SELECT
10014 	risks.risk_id,
10015 	risks.PK1,
10016 	risks.PK2,
10017 	risks.ASSOCIATION_CREATION_DATE,
10018 	risks.APPROVAL_DATE,
10019 	risks.DELETION_DATE,
10020 	risks.DELETION_APPROVAL_DATE,
10021 	risk.RISK_REV_ID
10022 FROM
10023 	AMW_RISK_ASSOCIATIONS risks,
10024 	AMW_FIN_PROCESS_EVAL_SUM eval,
10025 	AMW_RISKS_B risk
10026 WHERE
10027 	eval.fin_certification_id = p_certification_id
10028 	and risk.risk_id = risks.risk_id
10029 	and risk.CURR_APPROVED_FLAG = 'Y'
10030 	and risks.object_type='PROCESS_ORG'
10031 	and risks.PK1 = eval.organization_id
10032 	and risks.PK2 = eval.process_id
10033 	and risks.approval_date is not null
10034 	and risks.approval_date <= sysdate
10035 	and risks.deletion_approval_date is null
10036 UNION ALL
10037 SELECT
10038 	risks.risk_id,
10039 	risks.PK1,
10040 	risks.PK2,
10041 	risks.ASSOCIATION_CREATION_DATE,
10042 	risks.APPROVAL_DATE,
10043 	risks.DELETION_DATE,
10044 	risks.DELETION_APPROVAL_DATE,
10045 	risk.RISK_REV_ID
10046 FROM
10047 	AMW_RISK_ASSOCIATIONS risks,
10048 	AMW_FIN_PROCESS_EVAL_SUM eval,
10049 	AMW_RISKS_B risk
10050 WHERE
10051 	eval.fin_certification_id = p_certification_id
10052 	and risk.risk_id = risks.risk_id
10053 	and risk.CURR_APPROVED_FLAG = 'Y'
10054 	and risks.object_type='ENTITY_RISK'
10055 	and risks.PK1 = eval.organization_id
10056 	and risks.approval_date is not null
10057 	and risks.approval_date <= sysdate
10058 	and risks.deletion_approval_date is null;
10059 
10060 	--in risk association table, if type = 'PROCESS_FINCERT', pk1=certification_id, pk2=organization_id, pk3=process_id, pk4=opinion_log_id
10061 	CURSOR last_evaluation(l_risk_id number, l_organization_id number, l_process_id number)  IS
10062         select distinct ao.opinion_log_id
10063 	from    AMW_OPINIONS_LOG ao,
10064      		AMW_OBJECT_OPINION_TYPES aoot,
10065      		AMW_OPINION_TYPES_B aot,
10066      		FND_OBJECTS fo
10067 	where   ao.OBJECT_OPINION_TYPE_ID = aoot.OBJECT_OPINION_TYPE_ID
10068 		and aoot.OPINION_TYPE_ID = aot.OPINION_TYPE_ID
10069 		and aoot.OBJECT_ID = fo.OBJECT_ID
10070 		and fo.obj_name = 'AMW_ORG_PROCESS_RISK'
10071        		and aot.opinion_type_code = 'EVALUATION'
10072         	and ao.pk3_value = l_organization_id
10073         	and ao.pk4_value = l_process_id
10074         	and ao.pk1_value = l_risk_id
10075         	--fix bug 5724066
10076 	        AND ao.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
10077         	and ao.authored_date = (select max(aov2.authored_date)
10078                        	             from AMW_OPINIONS aov2
10079                                	     where aov2.object_opinion_type_id = ao.object_opinion_type_id
10080                                      and aov2.pk3_value = ao.pk3_value
10081                                      and aov2.pk1_value = ao.pk1_value
10082                                      and aov2.pk4_value = ao.pk4_value);
10083 
10084 l_count NUMBER;
10085 m_opinion_log_id NUMBER;
10086 l_error_message varchar2(4000);
10087 
10088 
10089 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_Risk_Ass_Sum_M';
10090 l_api_version_number CONSTANT NUMBER       := 1.0;
10091 
10092 l_return_status VARCHAR2(32767);
10093 l_msg_count NUMBER;
10094 l_msg_data VARCHAR2(32767);
10095 
10096 BEGIN
10097 
10098 	SAVEPOINT Populate_Fin_Risk_Ass_Sum_M;
10099 
10100  -- Standard call to check for call compatibility.
10101         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
10102                                              p_api_version_number,
10103                                              l_api_name,
10104                                              G_PKG_NAME)
10105         THEN
10106                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10107         END IF;
10108 
10109         -- Initialize message list if p_init_msg_list is set to TRUE.
10110         IF FND_API.to_Boolean( p_init_msg_list )
10111         THEN
10112                 FND_MSG_PUB.initialize;
10113         END IF;
10114 
10115         -- Initialize API return status to SUCCESS
10116         x_return_status := FND_API.G_RET_STS_SUCCESS;
10117 
10118 
10119 	SELECT COUNT(1) INTO l_count FROM AMW_RISK_ASSOCIATIONS
10120 	WHERE object_type = 'PROCESS_FINCERT'
10121 	AND pk1 = p_certification_id;
10122 
10123 	IF (l_count = 0) THEN
10124 	FOR risk_rec IN c_finrisks LOOP
10125 	exit when c_finrisks%notfound;
10126 
10127 		m_opinion_log_id := null;
10128 		OPEN last_evaluation(risk_rec.risk_id, risk_rec.pk1, risk_rec.pk2);
10129 		FETCH last_evaluation INTO m_opinion_log_id;
10130 		CLOSE last_evaluation;
10131 
10132 
10133 
10134 		INSERT INTO AMW_RISK_ASSOCIATIONS(
10135  			       RISK_ASSOCIATION_ID,
10136 			       RISK_ID,
10137 			       PK1,
10138 			       PK2,
10139 			       PK3,
10140 			       PK4,
10141 			       CREATED_BY,
10142 			       CREATION_DATE,
10143 			       LAST_UPDATE_DATE,
10144 			       LAST_UPDATED_BY,
10145 			       LAST_UPDATE_LOGIN,
10146 			       OBJECT_VERSION_NUMBER,
10147 			       OBJECT_TYPE,
10148 			       ASSOCIATION_CREATION_DATE,
10149 			       APPROVAL_DATE,
10150 			       DELETION_DATE,
10151 			       DELETION_APPROVAL_DATE,
10152 			       RISK_REV_ID)
10153 			 VALUES ( amw_risk_associations_s.nextval,
10154 			         risk_rec.risk_id,
10155 			         p_certification_id,
10156 			         risk_rec.PK1,
10157 			         risk_rec.PK2,
10158 			         m_opinion_log_id,
10159 			         FND_GLOBAL.USER_ID,
10160 			       	 SYSDATE,
10161 			         SYSDATE,
10162 			         FND_GLOBAL.USER_ID,
10163 			         FND_GLOBAL.USER_ID,
10164 			         1,
10165 			         'PROCESS_FINCERT',
10166 			         risk_rec.ASSOCIATION_CREATION_DATE,
10167 			         risk_rec.APPROVAL_DATE,
10168 				 risk_rec.DELETION_DATE,
10169 				 risk_rec.DELETION_APPROVAL_DATE,
10170 				 risk_rec.RISK_REV_ID);
10171 
10172 		END LOOP;
10173 if(p_commit <> FND_API.g_false)
10174 then commit;
10175 end if;
10176 
10177     	END IF;
10178 x_return_status := FND_API.G_RET_STS_SUCCESS;
10179 
10180 
10181 EXCEPTION
10182      WHEN NO_DATA_FOUND THEN
10183      IF c_finrisks%ISOPEN THEN
10184       	close c_finrisks;
10185          END IF;
10186      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
10187      x_return_status := FND_API.G_RET_STS_ERROR;
10188      x_msg_count := 1;
10189      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
10190       WHEN OTHERS THEN
10191       IF c_finrisks%ISOPEN THEN
10192       	close c_finrisks;
10193          END IF;
10194        ROLLBACK TO Populate_Fin_Risk_Ass_Sum_M;
10195       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
10196       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
10197         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10198         x_msg_count := 1;
10199         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
10200                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
10201                 FND_MSG_PUB.Count_And_Get(
10202                 p_encoded =>  FND_API.G_FALSE,
10203                 p_count   =>  x_msg_count,
10204                 p_data    =>  x_msg_data);
10205                 RETURN;
10206 
10207 END Populate_Fin_Risk_Ass_Sum_M;
10208 
10209 
10210 PROCEDURE Populate_Fin_Ctrl_Ass_Sum_M(
10211 p_api_version_number        IN   NUMBER   := 1.0,
10212 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
10213 p_commit                    IN   VARCHAR2 := FND_API.g_false,
10214 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
10215 p_certification_id  IN       NUMBER,
10216 x_return_status             OUT  nocopy VARCHAR2,
10217 x_msg_count                 OUT  nocopy NUMBER,
10218 x_msg_data                  OUT  nocopy VARCHAR2
10219 ) IS
10220 CURSOR c_fincontrols IS
10221 
10222 SELECT
10223 	controls.control_id,
10224 	controls.PK1,
10225 	controls.PK2,
10226 	controls.PK3,
10227 	controls.ASSOCIATION_CREATION_DATE,
10228 	controls.APPROVAL_DATE,
10229 	controls.DELETION_DATE,
10230 	controls.DELETION_APPROVAL_DATE,
10231 	control.CONTROL_REV_ID
10232 FROM
10233 	AMW_RISK_ASSOCIATIONS risks,
10234 	AMW_CONTROL_ASSOCIATIONS controls,
10235 	AMW_CONTROLS_B control
10236 WHERE
10237 	controls.object_type='RISK_ORG'
10238 	and control.CURR_APPROVED_FLAG = 'Y'
10239 	and control.control_id = controls.control_id
10240 	and risks.PK1 = p_certification_id
10241 	and risks.PK2 = controls.PK1
10242 	and risks.PK3 = controls.PK2
10243 	and controls.PK3 = risks.risk_id
10244 	and risks.object_type = 'PROCESS_FINCERT'
10245 UNION ALL
10246 SELECT
10247 	controls.control_id,
10248 	controls.PK1,
10249 	controls.PK2,
10250 	controls.PK3,
10251 	controls.ASSOCIATION_CREATION_DATE,
10252 	controls.APPROVAL_DATE,
10253 	controls.DELETION_DATE,
10254 	controls.DELETION_APPROVAL_DATE,
10255 	control.CONTROL_REV_ID
10256 FROM
10257 	AMW_RISK_ASSOCIATIONS risks,
10258 	AMW_CONTROL_ASSOCIATIONS controls,
10259 	AMW_CONTROLS_B control
10260 WHERE
10261 	controls.object_type='ENTITY_CONTROL'
10262 	and control.CURR_APPROVED_FLAG = 'Y'
10263 	and control.control_id = controls.control_id
10264 	and risks.PK1 = p_certification_id
10265 	and risks.PK2 = controls.PK1
10266 	and risks.PK3 IS NULL
10267 	and controls.PK3 = risks.risk_id
10268 	and risks.object_type = 'PROCESS_FINCERT';
10269 
10270 
10271 
10272 --in control association table, if type = 'RISK_FINCERT', pk1=certification_id, pk2=organization_id, pk3=process_id, pk4=risk_id, pk5=opinion_log_id
10273 	CURSOR last_evaluation(l_organization_id number, l_control_id number)  IS
10274         select distinct ao.opinion_log_id
10275 	from
10276      		AMW_OPINIONS_LOG ao,
10277      		AMW_OBJECT_OPINION_TYPES aoot,
10278      		AMW_OPINION_TYPES_B aot,
10279      		FND_OBJECTS fo
10280 	where ao.OBJECT_OPINION_TYPE_ID = aoot.OBJECT_OPINION_TYPE_ID
10281 		and aoot.OPINION_TYPE_ID = aot.OPINION_TYPE_ID
10282 		and aoot.OBJECT_ID = fo.OBJECT_ID
10283 		and fo.obj_name = 'AMW_ORG_CONTROL'
10284        		and aot.opinion_type_code = 'EVALUATION'
10285         	and ao.pk3_value = l_organization_id
10286         	and ao.pk1_value = l_control_id
10287         	--fix bug 5724066
10288 	        and ao.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
10289         	and ao.authored_date = (select max(aov2.authored_date)
10290                        	             from AMW_OPINIONS aov2
10291                                	     where aov2.object_opinion_type_id = ao.object_opinion_type_id
10292                                      and aov2.pk3_value = ao.pk3_value
10293                                      and aov2.pk1_value = ao.pk1_value);
10294 
10295 	l_count NUMBER;
10296 	m_opinion_log_id NUMBER;
10297 
10298 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_Ctrl_Ass_Sum_M';
10299 l_api_version_number CONSTANT NUMBER       := 1.0;
10300 
10301 l_return_status VARCHAR2(32767);
10302 l_msg_count NUMBER;
10303 l_msg_data VARCHAR2(32767);
10304 
10305 BEGIN
10306 
10307 	SAVEPOINT Populate_Fin_Ctrl_Ass_Sum_M;
10308 
10309  -- Standard call to check for call compatibility.
10310         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
10311                                              p_api_version_number,
10312                                              l_api_name,
10313                                              G_PKG_NAME)
10314         THEN
10315                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10316         END IF;
10317 
10318         -- Initialize message list if p_init_msg_list is set to TRUE.
10319         IF FND_API.to_Boolean( p_init_msg_list )
10320         THEN
10321                 FND_MSG_PUB.initialize;
10322         END IF;
10323 
10324         -- Initialize API return status to SUCCESS
10325         x_return_status := FND_API.G_RET_STS_SUCCESS;
10326 
10327 
10328 	SELECT COUNT(1) INTO l_count FROM AMW_CONTROL_ASSOCIATIONS
10329 	WHERE OBJECT_TYPE = 'RISK_FINCERT'
10330 	and PK1 = p_certification_id;
10331 
10332 	IF (l_count = 0) THEN
10333 	FOR control_rec IN c_fincontrols LOOP
10334 	exit when c_fincontrols%notfound;
10335 
10336 	m_opinion_log_id := null;
10337 	OPEN last_evaluation(control_rec.pk1, control_rec.control_id);
10338 	FETCH last_evaluation INTO m_opinion_log_id;
10339 	CLOSE last_evaluation;
10340 
10341 		INSERT INTO AMW_CONTROL_ASSOCIATIONS(
10342  			       CONTROL_ASSOCIATION_ID,
10343 			       CONTROL_ID,
10344 			       PK1,
10345 			       PK2,
10346 			       PK3,
10347 			       PK4,
10348 			       PK5,
10349 			       CREATED_BY,
10350 			       CREATION_DATE,
10351 			       LAST_UPDATE_DATE,
10352 			       LAST_UPDATED_BY,
10353 			       LAST_UPDATE_LOGIN,
10354 			       OBJECT_VERSION_NUMBER,
10355 			       OBJECT_TYPE,
10356 			       ASSOCIATION_CREATION_DATE,
10357 			       APPROVAL_DATE,
10358 			       DELETION_DATE,
10359 			       DELETION_APPROVAL_DATE,
10360 			       CONTROL_REV_ID)
10361 			 VALUES (AMW_CONTROL_ASSOCIATIONS_S.nextval,
10362 			         control_rec.control_id,
10363 			         p_certification_id,
10364 			         control_rec.PK1,
10365 			         control_rec.PK2,
10366 			         control_rec.PK3,
10367 			         m_opinion_log_id,
10368 			         FND_GLOBAL.USER_ID,
10369 			       	 SYSDATE,
10370 			         SYSDATE,
10371 			         FND_GLOBAL.USER_ID,
10372 			         FND_GLOBAL.USER_ID,
10373 			         1,
10374 			         'RISK_FINCERT',
10375 			         control_rec.ASSOCIATION_CREATION_DATE,
10376 	 		         control_rec.APPROVAL_DATE,
10377 			         control_rec.DELETION_DATE,
10378 			        control_rec.DELETION_APPROVAL_DATE,
10379 			        control_rec.CONTROL_REV_ID);
10380 
10381 		END LOOP;
10382 if(p_commit <> FND_API.g_false)
10383 then commit;
10384 end if;
10385 	END IF;
10386 	   x_return_status := FND_API.G_RET_STS_SUCCESS;
10387 
10388 EXCEPTION
10389      WHEN NO_DATA_FOUND THEN
10390      IF c_fincontrols%ISOPEN THEN
10391       	close c_fincontrols;
10392       END IF;
10393      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
10394      x_return_status := FND_API.G_RET_STS_ERROR;
10395      x_msg_count := 1;
10396      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
10397       WHEN OTHERS THEN
10398       IF c_fincontrols%ISOPEN THEN
10399       	close c_fincontrols;
10400       END IF;
10401        ROLLBACK TO Populate_Fin_Ctrl_Ass_Sum_M;
10402       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
10403       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
10404         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10405         x_msg_count := 1;
10406         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
10407                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
10408                 FND_MSG_PUB.Count_And_Get(
10409                 p_encoded =>  FND_API.G_FALSE,
10410                 p_count   =>  x_msg_count,
10411                 p_data    =>  x_msg_data);
10412                 RETURN;
10413 
10414 END Populate_Fin_Ctrl_Ass_Sum_M;
10415 
10416 
10417 -------------populate control association which related to financial certification ----
10418 PROCEDURE Populate_Fin_AP_Ass_Sum_M(
10419 p_api_version_number        IN   NUMBER   := 1.0,
10420 p_init_msg_list             IN   VARCHAR2 := FND_API.g_false,
10421 p_commit                    IN   VARCHAR2 := FND_API.g_false,
10422 p_validation_level          IN   NUMBER   := fnd_api.g_valid_level_full,
10423 p_certification_id  IN       NUMBER,
10424 x_return_status             OUT  nocopy VARCHAR2,
10425 x_msg_count                 OUT  nocopy NUMBER,
10426 x_msg_data                  OUT  nocopy VARCHAR2
10427 ) IS
10428 
10429 CURSOR c_finap IS
10430 SELECT
10431 	ap.AUDIT_PROCEDURE_ID,
10432 	ap.PK1,
10433 	ap.PK2,
10434 	ap.PK3,
10435 	ap.ASSOCIATION_CREATION_DATE,
10436 	ap.APPROVAL_DATE,
10437 	ap.DELETION_DATE,
10438 	ap.DELETION_APPROVAL_DATE,
10439 	apb.AUDIT_PROCEDURE_REV_ID
10440 FROM
10441 	AMW_AP_ASSOCIATIONS ap,
10442 	AMW_CONTROL_ASSOCIATIONS controls,
10443 	AMW_AUDIT_PROCEDURES_B apb
10444 WHERE
10445 	ap.object_type='CTRL_ORG'
10446 	and apb.CURR_APPROVED_FLAG = 'Y'
10447 	and ap.audit_procedure_id = apb.audit_procedure_id
10448 	and controls.PK1 = p_certification_id
10449 	and controls.PK2 = ap.PK1
10450 	and controls.PK2 = ap.PK2
10451 	and controls.control_id = ap.PK3
10452 	and controls.object_type = 'RISK_FINCERT'
10453 UNION ALL
10454 SELECT
10455 	ap.AUDIT_PROCEDURE_ID,
10456 	ap.PK1,
10457 	ap.PK2,
10458 	ap.PK3,
10459 	ap.ASSOCIATION_CREATION_DATE,
10460 	ap.APPROVAL_DATE,
10461 	ap.DELETION_DATE,
10462 	ap.DELETION_APPROVAL_DATE,
10463 	apb.AUDIT_PROCEDURE_REV_ID
10464 FROM
10465 	AMW_AP_ASSOCIATIONS ap,
10466 	AMW_CONTROL_ASSOCIATIONS controls,
10467 	AMW_AUDIT_PROCEDURES_B apb
10468 WHERE
10469 	ap.object_type='ENTITY_CTRL_AP'
10470 	and apb.CURR_APPROVED_FLAG = 'Y'
10471 	and ap.audit_procedure_id = apb.audit_procedure_id
10472 	and controls.PK1 = p_certification_id
10473 	and controls.PK2 = ap.PK1
10474 	--and controls.PK3 = ap.PK2
10475 	and controls.PK3 is null
10476 	and controls.control_id = ap.PK3
10477 	and controls.object_type = 'RISK_FINCERT';
10478 
10479 --need check opinion framework doc
10480 --in ap association table, if type = 'CTRL_FINCERT', pk1=certification_id, pk2=organization_id, pk3=process_id, pk4=control_id, pk5=opinion_id
10481 CURSOR last_evaluation(l_audit_procedure_id number, l_organization_id number, l_control_id number)  IS
10482 SELECT 	distinct aov.opinion_id
10483 FROM 	AMW_OPINION_M_V aov
10484 WHERE
10485                 aov.object_name = 'AMW_ORG_AP_CONTROL'
10486         AND 	aov.opinion_type_code = 'EVALUATION'
10487         AND 	aov.opinion_component_code = 'OVERALL'
10488         AND 	aov.pk3_value = l_organization_id
10489         AND 	aov.pk4_value = l_audit_procedure_id
10490         AND	aov.pk1_value = l_control_id
10491         --fix bug 5724066
10492 	AND     aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = 'CANC')
10493         AND 	aov.authored_date = (select max(aov2.authored_date)
10494                        	             from AMW_OPINIONS aov2
10495                                	     where aov2.object_opinion_type_id = aov.object_opinion_type_id
10496                                      and aov2.pk3_value = aov.pk3_value
10497                                      and aov2.pk1_value = aov.pk1_value
10498                                      and aov2.pk4_value = aov.pk4_value);
10499 
10500 
10501 	l_count NUMBER;
10502 	m_opinion_id NUMBER;
10503 
10504 
10505 l_api_name           CONSTANT VARCHAR2(30) := 'Populate_Fin_AP_Ass_Sum_M';
10506 l_api_version_number CONSTANT NUMBER  := 1.0;
10507 
10508 l_return_status VARCHAR2(32767);
10509 l_msg_count NUMBER;
10510 l_msg_data VARCHAR2(32767);
10511 
10512 BEGIN
10513 
10514    SAVEPOINT Populate_Fin_AP_Ass_Sum_M;
10515 
10516  -- Standard call to check for call compatibility.
10517 
10518         IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
10519                                              p_api_version_number,
10520                                              l_api_name,
10521                                              G_PKG_NAME)
10522         THEN
10523                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10524         END IF;
10525 
10526         -- Initialize message list if p_init_msg_list is set to TRUE.
10527         IF FND_API.to_Boolean( p_init_msg_list )
10528         THEN
10529                 FND_MSG_PUB.initialize;
10530         END IF;
10531 
10532         -- Initialize API return status to SUCCESS
10533         x_return_status := FND_API.G_RET_STS_SUCCESS;
10534 
10535 
10536 
10537 	SELECT COUNT(1) INTO l_count FROM AMW_AP_ASSOCIATIONS
10538 	WHERE OBJECT_TYPE = 'CTRL_FINCERT'
10539 	and PK1 = p_certification_id;
10540 
10541 	IF (l_count = 0) THEN
10542 	FOR ap_rec IN c_finap LOOP
10543 	exit when c_finap%notfound;
10544 
10545 	m_opinion_id := null;
10546 	OPEN last_evaluation(ap_rec.audit_procedure_id, ap_rec.pk1, ap_rec.pk3);
10547 	FETCH last_evaluation INTO m_opinion_id;
10548 	CLOSE last_evaluation;
10549 
10550 
10551 		INSERT INTO AMW_AP_ASSOCIATIONS(
10552 			       AP_ASSOCIATION_ID,
10553  			       AUDIT_PROCEDURE_ID,
10554 			       PK1,
10555 			       PK2,
10556 			       PK3,
10557 			       PK4,
10558 			       PK5,
10559 			       CREATED_BY,
10560 			       CREATION_DATE,
10561 			       LAST_UPDATE_DATE,
10562 			       LAST_UPDATED_BY,
10563 			       LAST_UPDATE_LOGIN,
10564 			       OBJECT_VERSION_NUMBER,
10565 			       OBJECT_TYPE,
10566 			       ASSOCIATION_CREATION_DATE,
10567 			       APPROVAL_DATE,
10568 			       DELETION_DATE,
10569 			       DELETION_APPROVAL_DATE,
10570 			       AUDIT_PROCEDURE_REV_ID)
10571 			 VALUES (AMW_AP_ASSOCIATIONS_S.nextval,
10572 			         ap_rec.audit_procedure_id,
10573 			         p_certification_id,
10574 			         ap_rec.PK1,
10575 			         ap_rec.PK2,
10576 			         ap_rec.PK3,
10577 			         m_opinion_id,
10578 			         FND_GLOBAL.USER_ID,
10579 			         SYSDATE,
10580 			         SYSDATE,
10581 			         FND_GLOBAL.USER_ID,
10582 			         FND_GLOBAL.USER_ID,
10583 			         1,
10584 			         'CTRL_FINCERT',
10585 			         ap_rec.ASSOCIATION_CREATION_DATE,
10586 	 		         ap_rec.APPROVAL_DATE,
10587 			         ap_rec.DELETION_DATE,
10588 			         ap_rec.DELETION_APPROVAL_DATE,
10589 			         ap_rec.AUDIT_PROCEDURE_REV_ID);
10590 
10591 
10592 		END LOOP;
10593 if(p_commit <> FND_API.g_false)
10594 then commit;
10595 end if;
10596 	END IF;
10597 	x_return_status := FND_API.G_RET_STS_SUCCESS;
10598 
10599 EXCEPTION
10600      WHEN NO_DATA_FOUND THEN
10601      IF c_finap%ISOPEN THEN
10602       	close c_finap;
10603       END IF;
10604      fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name );
10605      x_return_status := FND_API.G_RET_STS_ERROR;
10606      x_msg_count := 1;
10607      x_msg_data := 'NO DATA FOUND IN ' || G_PKG_NAME || '.' || l_api_name ;
10608       WHEN OTHERS THEN
10609       IF c_finap%ISOPEN THEN
10610       	close c_finap;
10611       END IF;
10612       ROLLBACK TO Populate_Fin_AP_Ass_Sum_M;
10613       fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.' || l_api_name );
10614       fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
10615         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10616         x_msg_count := 1;
10617         x_msg_data := SUBSTR (SQLERRM, 1, 2000);
10618                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME, l_api_name);
10619                 FND_MSG_PUB.Count_And_Get(
10620                 p_encoded =>  FND_API.G_FALSE,
10621                 p_count   =>  x_msg_count,
10622                 p_data    =>  x_msg_data);
10623                 RETURN;
10624 
10625 END Populate_Fin_AP_Ass_Sum_M;
10626 
10627 
10628 FUNCTION  Get_Proc_Verified_M
10629 (
10630 P_CERTIFICATION_ID in number,
10631 P_STATEMENT_GROUP_ID in number,
10632 P_FINANCIAL_STATEMENT_ID in number,
10633 P_FINANCIAL_ITEM_ID in number,
10634 P_ACCOUNT_GROUP_ID in number,
10635 P_ACCOUNT_ID in number,
10636 P_OBJECT_TYPE in varchar2) RETURN NUMBER
10637 
10638 IS
10639 l_stmt VARCHAR2(2000);
10640 	l_stmt1 VARCHAR2(100);
10641 	l_stmt2 VARCHAR2(100);
10642 	l_sql_stmt VARCHAR2(2000);
10643 
10644 	X_PROC_VERIFIED Number;
10645 BEGIN
10646 
10647 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
10648 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
10649 
10650         l_stmt := 'SELECT COUNT(1) FROM
10651  	(Select distinct  fin.PROCESS_ID, fin.ORGANIZATION_ID
10652 	FROM
10653 	AMW_OPINION_M_V aov,
10654 	amw_fin_cert_scope fin
10655 	WHERE aov.OPINION_TYPE_CODE = ''EVALUATION''
10656         and aov.object_name = ''AMW_ORG_PROCESS''
10657         and aov.opinion_component_code = ''OVERALL''
10658         and aov.PK3_VALUE = fin.ORGANIZATION_ID
10659         and aov.PK1_VALUE = fin.PROCESS_ID
10660         --fix bug 5724066
10661 	and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
10662         and fin.process_id is not null
10663         and fin.FIN_CERTIFICATION_ID = :1 ';
10664 
10665 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
10666         l_sql_stmt := l_stmt || l_stmt1;
10667 
10668         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_VERIFIED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
10669 
10670 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
10671         l_sql_stmt := l_stmt || l_stmt2;
10672         EXECUTE IMMEDIATE l_sql_stmt INTO X_PROC_VERIFIED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
10673 END IF;
10674 
10675         RETURN X_PROC_VERIFIED;
10676  EXCEPTION
10677   WHEN NO_DATA_FOUND THEN
10678     /*** Record doesn't exist ***/
10679     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_Proc_Verified_M');
10680     RETURN 0;
10681   WHEN OTHERS THEN
10682     /*** Raise any other error ***/
10683     fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_Proc_Verified_M');
10684     fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
10685     RAISE;
10686 
10687 END Get_Proc_Verified_M;
10688 
10689 FUNCTION Get_ORG_EVALUATED_M
10690 (
10691 P_CERTIFICATION_ID in number,
10692 P_STATEMENT_GROUP_ID in number,
10693 P_FINANCIAL_STATEMENT_ID in number,
10694 P_FINANCIAL_ITEM_ID in number,
10695 P_ACCOUNT_GROUP_ID in number,
10696 P_ACCOUNT_ID in number,
10697 P_OBJECT_TYPE in varchar2) RETURN NUMBER
10698 
10699 IS
10700 	l_stmt VARCHAR2(2000);
10701 	l_stmt1 VARCHAR2(100);
10702 	l_stmt2 VARCHAR2(100);
10703 	l_sql_stmt VARCHAR2(2000);
10704 
10705 	X_ORG_EVALUATED  Number;
10706 
10707 BEGIN
10708 
10709 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
10710 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
10711 
10712 l_stmt := 'select count(1) from (
10713 select distinct fin.ORGANIZATION_ID
10714 FROM
10715 AMW_OPINION_M_V aov,
10716 amw_fin_cert_scope fin
10717 WHERE aov.OPINION_TYPE_CODE = ''EVALUATION''
10718 and aov.object_name = ''AMW_ORGANIZATION''
10719 and aov.opinion_component_code = ''OVERALL''
10720 and aov.pk1_value = fin.organization_id
10721 --fix bug 5724066
10722 and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
10723 and fin.FIN_CERTIFICATION_ID= :1 ';
10724 
10725 
10726 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
10727         l_sql_stmt := l_stmt || l_stmt1;
10728         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_EVALUATED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
10729 ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
10730         l_sql_stmt := l_stmt || l_stmt2;
10731 
10732         EXECUTE IMMEDIATE l_sql_stmt INTO X_ORG_EVALUATED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
10733 END IF;
10734 
10735                 RETURN X_ORG_EVALUATED;
10736 
10737  EXCEPTION
10738   WHEN NO_DATA_FOUND THEN
10739     /*** Record doesn't exist ***/
10740     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_ORG_EVALUATED_M');
10741     RETURN 0;
10742   WHEN OTHERS THEN
10743     /*** Raise any other error ***/
10744     fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_ORG_EVALUATED_M');
10745 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
10746     RAISE;
10747 
10748 END Get_ORG_EVALUATED_M;
10749 
10750 FUNCTION Get_RISKS_VERIFIED_M
10751 (
10752 P_CERTIFICATION_ID in number,
10753 P_STATEMENT_GROUP_ID in number,
10754 P_FINANCIAL_STATEMENT_ID in number,
10755 P_FINANCIAL_ITEM_ID in number,
10756 P_ACCOUNT_GROUP_ID in number,
10757 P_ACCOUNT_ID in number,
10758 P_OBJECT_TYPE in varchar2) RETURN NUMBER
10759 
10760 IS
10761 	l_stmt VARCHAR2(2000);
10762 	l_stmt1 VARCHAR2(100);
10763 	l_stmt2 VARCHAR2(100);
10764 	l_sql_stmt VARCHAR2(2000);
10765 
10766 	X_RISKS_VERIFIED  Number;
10767 BEGIN
10768 
10769 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
10770 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
10771 
10772 /*********** replace with the following query that uses opinion_log_id directly
10773 l_stmt := 'select count(1)  from (
10774 select distinct  fin.risk_id ,fin.organization_id, fin.Process_ID
10775 FROM
10776 	AMW_OPINION_M_V aov,
10777 	amw_fin_item_acc_risk fin
10778 WHERE
10779 aov.OPINION_TYPE_CODE = ''EVALUATION''
10780 and aov.object_name = ''AMW_ORG_PROCESS_RISK''
10781 and aov.opinion_component_code = ''OVERALL''
10782 and aov.pk1_value = fin.risk_id
10783 and aov.pk3_value = fin.organization_id
10784 and aov.pk4_value = fin.process_ID
10785 --fix bug 5724066
10786 and aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
10787 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
10788 and fin.FIN_CERTIFICATION_ID= :1 ';
10789 ************/
10790 
10791 l_stmt := 'select count(1)  from (
10792 select distinct  fin.risk_id ,fin.organization_id, fin.Process_ID
10793 FROM
10794 	amw_opinion_m_v aov,
10795 	amw_opinions_log aol,
10796 	amw_fin_item_acc_risk fin
10797 WHERE
10798 aov.OPINION_TYPE_CODE = ''EVALUATION''
10799 and aov.object_name = ''AMW_ORG_PROCESS_RISK''
10800 and aov.opinion_component_code = ''OVERALL''
10801 and aol.opinion_log_id = fin.opinion_log_id
10802 and aol.opinion_id = aov.opinion_id
10803 and aol.opinion_set_id = aov.opinion_set_id
10804 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
10805 and fin.FIN_CERTIFICATION_ID= :1 ';
10806 
10807 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
10808         l_sql_stmt := l_stmt || l_stmt1;
10809 
10810         EXECUTE IMMEDIATE l_sql_stmt INTO X_RISKS_VERIFIED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
10811 
10812 
10813         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
10814         l_sql_stmt := l_stmt || l_stmt2;
10815 
10816         EXECUTE IMMEDIATE l_sql_stmt INTO X_RISKS_VERIFIED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
10817 
10818         END IF;
10819                 RETURN X_RISKS_VERIFIED;
10820 EXCEPTION
10821   WHEN NO_DATA_FOUND THEN
10822     /*** Record doesn't exist ***/
10823     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_RISKS_VERIFIED_M');
10824     RETURN 0;
10825   WHEN OTHERS THEN
10826     /*** Raise any other error ***/
10827 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_RISKS_VERIFIED_M');
10828 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
10829     RAISE;
10830 
10831 END Get_RISKS_VERIFIED_M;
10832 
10833 FUNCTION Get_CONTROLS_VERIFIED_M
10834 (
10835 P_CERTIFICATION_ID in number,
10836 P_STATEMENT_GROUP_ID in number,
10837 P_FINANCIAL_STATEMENT_ID in number,
10838 P_FINANCIAL_ITEM_ID in number,
10839 P_ACCOUNT_GROUP_ID in number,
10840 P_ACCOUNT_ID in number,
10841 P_OBJECT_TYPE in varchar2) RETURN NUMBER
10842 
10843 IS
10844 	l_stmt VARCHAR2(2000);
10845 	l_stmt1 VARCHAR2(100);
10846 	l_stmt2 VARCHAR2(100);
10847 	l_sql_stmt VARCHAR2(2000);
10848 
10849 	X_CONTROLS_VERIFIED  Number;
10850 
10851 
10852 BEGIN
10853 
10854 l_stmt1 := ' AND FIN.STATEMENT_GROUP_ID = :2 AND FIN.FINANCIAL_STATEMENT_ID = :3 AND FIN.FINANCIAL_ITEM_ID = :4)';
10855 l_stmt2 := ' AND FIN.NATURAL_ACCOUNT_ID = :2)';
10856 
10857 /********replace to use opinion_log_id in amw_fin_item_acc_ctrl
10858 l_stmt := 'select count(1) from(
10859 select distinct  fin.control_id, fin.organization_id
10860 FROM
10861 AMW_OPINION_M_V aov,
10862 amw_fin_item_acc_ctrl fin
10863 WHERE aov.OPINION_TYPE_CODE = ''EVALUATION''
10864 AND aov.object_name = ''AMW_ORG_CONTROL''
10865 and aov.opinion_component_code = ''OVERALL''
10866 AND aov.pk1_value = fin.control_id
10867 AND aov.pk3_value = fin.organization_id
10868 --fix bug 5724066
10869 AND aov.pk2_value not in (select audit_project_id from amw_audit_projects where audit_project_status = ''CANC'')
10870 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
10871 and fin.fin_certification_id = :1 ';
10872 ****************************/
10873 
10874 l_stmt := 'select count(1) from(
10875 select distinct  fin.control_id, fin.organization_id
10876 FROM
10877 amw_opinion_m_v aov,
10878 amw_opinions_log aol,
10879 amw_fin_item_acc_ctrl fin
10880 WHERE aov.OPINION_TYPE_CODE = ''EVALUATION''
10881 and  aov.object_name = ''AMW_ORG_CONTROL''
10882 and aov.opinion_component_code = ''OVERALL''
10883 and aol.opinion_log_id = fin.OPINION_LOG_ID
10884 and aol.opinion_id = aov.opinion_id
10885 and aol.opinion_set_id = aov.opinion_set_id
10886 and fin.object_type = ''' || P_OBJECT_TYPE || '''' || '
10887 and fin.fin_certification_id = :1 ';
10888 
10889 IF P_OBJECT_TYPE = 'FINANCIAL ITEM' THEN
10890         l_sql_stmt := l_stmt || l_stmt1;
10891 
10892         EXECUTE IMMEDIATE l_sql_stmt INTO X_CONTROLS_VERIFIED USING P_CERTIFICATION_ID, P_STATEMENT_GROUP_ID, P_FINANCIAL_STATEMENT_ID, P_FINANCIAL_ITEM_ID ;
10893         --RETURN X_CONTROLS_VERIFIED;
10894 
10895         ELSIF P_OBJECT_TYPE = 'ACCOUNT' THEN
10896         l_sql_stmt := l_stmt || l_stmt2;
10897 
10898         EXECUTE IMMEDIATE l_sql_stmt INTO X_CONTROLS_VERIFIED USING P_CERTIFICATION_ID, P_ACCOUNT_ID ;
10899         --RETURN X_CONTROLS_VERIFIED;
10900         END IF;
10901         RETURN X_CONTROLS_VERIFIED;
10902  EXCEPTION
10903   WHEN NO_DATA_FOUND THEN
10904     /*** Record doesn't exist ***/
10905     fnd_file.put_line(fnd_file.LOG, 'NO DATA FOUND IN ' || G_PKG_NAME || '.Get_CONTROLS_VERIFIED_M');
10906     RETURN 0;
10907   WHEN OTHERS THEN
10908     /*** Raise any other error ***/
10909 fnd_file.put_line(fnd_file.LOG, 'Unexpected error in ' || G_PKG_NAME || '.Get_CONTROLS_VERIFIED_M');
10910 fnd_file.put_line(fnd_file.LOG, SUBSTR (SQLERRM, 1, 2000));
10911     RAISE;
10912 
10913 
10914 END Get_CONTROLS_VERIFIED_M;
10915 
10916 
10917 END AMW_FINSTMT_CERT_BES_PKG;