DBA Data[Home] [Help]

PACKAGE BODY: APPS.PER_ZA_EMPLOYMENT_EQUITY_PKG

Source


1 PACKAGE BODY PER_ZA_EMPLOYMENT_EQUITY_PKG as
2 /* $Header: perzaeer.pkb 120.45.12020000.2 2012/10/15 09:24:19 abdash ship $ */
3 /*
4 ==============================================================================
5 This package loads data into table per_za_employment_equity for use by
6 the Employment Equity Reports
7 
8 MODIFICATION HISTORY
9 
10 Name           Date        Version Bug     Text
11 -------------- ----------- ------- ------- -----------------------------
12 R. Kingham     22 May 2000   110.0         Initial Version
13 R. Kingham     15 Jun 2000   110.1         Added Extra functionality for EEQ.
14 D. Son         20 Jun 2001   110.2         Removed data load dbms_sql procedures.
15                                            Replaced with lookup functions and
16                                            insert procedures.
17 F.D. Loubser   11 Sep 2001   115.4         Almost complete rewrite for 11i.
18 F.D. Loubser   10 Dec 2001   115.7         Business_group_id on user table
19 F.D. Loubser    1 Feb 2002   115.8         QA fixes
20 F.D. Loubser    1 Feb 2002   115.9         Added checkfile
21 F.D. Loubser    7 Feb 2002   115.10        Removed low list skip optimization
22 F.D. Loubser   14 Feb 2002   115.11        Added multiple legal entity
23 J.N. Louw       5 Apr 2002   115.12        Bug 2306877
24                                            Fixed default select statements
25                                            each section EQ1 to 6
26 F.D. Loubser    9 May 2002   115.13        g_cat_flex variable too small
27 Nirupa S       05 Dec 2002  115.14 2686695 In accordance with the APPS-wide
28                                            performance changes for 11.5.9,
29                                            NOCOPY has been added to all
30                                            OUT and IN OUT  parameters.
31 Nirupa S       10 Dec 2002  115.14 2686695 Added exception handling for
32                                            NOCOPY in calc_highest_and_lowest_avg
33 A.Sengar       11 Dec 2002  115.16 2665394 Modified the query to improve
34                                            performance.
35 Nageswara      17 Nov 2004  115.17 3962073 modified query to fetch Employee Type
36                                            when user has entered value.
37                                            Supressed GSCC warnings
38 Nageswara      24 Nov 2004  115.18 4027769 modified select query to all disabilities
39                                            'F','P' as 'Y' (Disabled)
40 Kaladhaur P    28-Jun-2005  115.20 4445926 Fix GSCC Error: File.Sql.8 and File.Sql.18
41 Kaladhaur P    28-Jun-2005  115.21 4413678 Function get_avg_5_lowest_salary has been
42                                            modified to process employees with race
43                                            not equal to 'Not Used'.
44 A. Mahanty     19-Dec-2005  115.24 4872110 R12 Performance Bug fix. Modified the query in
45                                            function get_avg_5_lowest_salary
46 A. Mahanty     21-Dec-2005  115.25 4872110 R12 Performance Bug fix. Modified the queries in
47                                            function populate_ee_table
48 Kaladhaur P    22-May-2006  115.26 4413678 Function get_avg_5_lowest_salary has been
49                                            modified to process employees with race
50                                            not equal to 'Not Used'.
51 Kaladhaur P    20-Jun-2006  115.27 5228238 Query in function get_lookup_code has been
52                                            modified to fetch one row for lookup meaning
53                                            'Not Applicable'.
54 Kaladhaur P    04-Jul-2006  115.28 4413678 Modified the comments.
55 R Pahune       24-Jul-2006  115.30 5406242 Employment equity enhancment.
56 R Pahune       08-jan-2008  115.43 6773326 To get correct Employment Category due to
57                                            changes for WSP.
58 R Babla        18-Feb-2008  115.49 6817148 Changes in populate_ee_table to consider the
59                                            new segment added for foreign national
60 R Babla        23-Jul-2008  115.50 7277745/Changes done to consider chinese as Africans
61                                    7237663 and to cater for employer contribution
62 R Babla        30-Jul-2008  115.51 7277745/Changes done to add 'Normal' to the balance name
63                                    7237663 for Normal ER contribution
64 P Arusia       25-Aug-2008  115.52 7277745 Corrected the procedure
65                                            init_g_cat_lev_table to add l_er_annual_income
66                                            to net annual income
67 R Babla        30-Jul-2008  115.54 7360563 Changes done in cursor c_assignments of
68                                            proc init_g_cat_lev_table so as not to
69                                            include employee in differential report
70                                            which has any of occupational cat/ level
71                                            or function type as 'Not Applicable'
72 R Babla        16-Sep-2009  115.56 8911880 Changes done in procedure init_g_cat_lev_table
73                                            to ensure the income differential report doesnt annualize
74                                            the income for employees who did not work for completing
75                                            reporting year + show actual remuneration for fluctuating
76                                            income.
77 NCHINNAM       17-Nov-2009  115.57 8486088 1. Commented 'EQ1' - Occupational Categories
78                                            2. Changed Termination Categories
79                                            3. Changed foreign national logic
80 NCHINNAM       17-Nov-2009  115.58         Fixed GSCC Errors
81 R Babla        24-Nov-2009  115.59 9112237 Added init_g_cat_lev_new_table and supporting procedures from
82                                            reporting year 2009
83 R Babla        24-Nov-2009  115.60 9112237 Fixed GSCC Warnings
84 NCHINNAM       01-Dec-2009  115.61 9112237 Added new procedures from
85                                            reporting year 2009
86 NCHINNAM       03-Dec-2009  115.62 9112237 renaming the reason is moved to dt.
87 R Babla        18-Dec-2009  115.63 9112237 Modified query for inserting rows if row present for foreigner
88                                            non permanent, but not present for non foreigner non permanent
89                                            in procedure init_g_cat_lev_new_table
90 R Babla        01-Apr-2010  115.64 9462039 Modified procedures to remove the dependency on Occupation Category
91                                            from reporting year 2009
92 ABDASH     15-Oct-2012  115.65 14690729 Modified the query for 'EQ8' in procedure "populate_ee_table_new"
93                                             to avoid duplication of employees when multiple leaving_reasons
94                                             are mapped to a single termination category.
95 ==============================================================================
96 */
97 
98 -- Global types
99 type r_assignments is record
100 (
101    payroll_id            per_all_assignments_f.payroll_id%type,
102    legal_entity_id       hr_all_organization_units.organization_id%type,
103    legal_entity       hr_all_organization_units.name%type,
104    occupational_level_id    hr_lookups.lookup_code%type,
105    occupational_category_id  hr_lookups.lookup_code%type,
106    occupational_level    hr_lookups.meaning%type,
107    occupational_category  hr_lookups.meaning%type,
108    race                  per_all_people_f.per_information4%type,
109    sex                   per_all_people_f.sex%type,
110    annual_income         number
111 );
112 
113 type r_averages is record
114 (
115    high number,
116    low  number
117 );
118 
119 -- Added for Employment equity report enhancement
120 type r_Encome_diff_rec is record
121 (
122    legal_entity_id     hr_all_organization_units.organization_id%type,
123    legal_entity     hr_all_organization_units.NAME%type,
124    occupational_code_id   hr_lookups.lookup_code%type,
125    occupational_code   hr_lookups.meaning%type,
126    ma                  number,
127    mc                  number,
128    mi                  number,
129    mw                  number,
130    fa                  number,
131    fc                  number,
132    fi                  number,
133    fw                  number,
134    total               number,
135    ma_inc              number,
136    mc_inc              number,
137    mi_inc              number,
138    mw_inc              number,
139    fa_inc              number,
140    fc_inc              number,
141    fi_inc              number,
142    fw_inc              number,
143    total_inc           number
144 );
145 
146 type t_assignments is table of r_assignments index by binary_integer;
147 type t_averages    is table of r_averages    index by binary_integer;
148 
149 TYPE t_E_differential IS TABLE OF r_Encome_diff_rec INDEX BY binary_integer;
150 
151 
152 type rec_assignments is record
153 (
154    payroll_id            per_all_assignments_f.payroll_id%type,
155    legal_entity_id       hr_all_organization_units.organization_id%type,
156    legal_entity       hr_all_organization_units.name%type,
157    occupational_level_id    hr_lookups.lookup_code%type,
158    occupational_category_id  hr_lookups.lookup_code%type,
159    occupational_level    hr_lookups.meaning%type,
160    occupational_category  hr_lookups.meaning%type,
161    race                  per_all_people_f.per_information4%type,
162    sex                   per_all_people_f.sex%type,
163    employment_type       varchar2(30), --Permanent/Non Permanent
164    foreigner             varchar2(30), --Foreigner or not
165    annual_income         number
166 );
167 type t_new_assignments is table of rec_assignments index by binary_integer;
168 g_new_assignments_table   t_new_assignments;
169 g_lev_Enc_Diff_table_F t_E_differential; --For Permanent foreigners
170 g_lev_Enc_Diff_table_T t_E_differential; --For Temporary non foreigner workers
171 g_lev_Enc_Diff_table_TF t_E_differential; --For Temporary foreigners workers
172 
173 
174 
175 -- Global variables
176 g_package            constant varchar2(30) := 'per_za_employment_equity_pkg.';
177 g_assignments_table  t_assignments;
178 g_cat_averages_table t_averages;
179 g_lev_averages_table t_averages;
180 
181 g_cat_Enc_Diff_table t_E_differential;
182 g_lev_Enc_Diff_table t_E_differential;
183 
184 g_grade_name         per_grades.name%type;
185 g_grade_report_date  date;
186 g_grade_asg_id       per_all_assignments_f.assignment_id%type;
187 
188 g_position_name      per_all_positions.name%type;
189 g_pos_report_date    date;
190 g_pos_asg_id         per_all_assignments_f.assignment_id%type;
191 
192 g_job_name           per_jobs.name%type;
193 g_job_report_date    date;
194 g_job_asg_id         per_all_assignments_f.assignment_id%type;
195 
196 g_lev_name           hr_lookups.meaning%type;
197 g_lev_report_date    date;
198 g_lev_asg_id         per_all_assignments_f.assignment_id%type;
199 
200 g_cat_name           hr_lookups.meaning%type;
201 g_cat_report_date    date;
202 g_cat_asg_id         per_all_assignments_f.assignment_id%type;
203 
204 g_lev_flex           pay_user_column_instances_f.value%type := null;
205 g_lev_segment        pay_user_column_instances_f.value%type := null;
206 g_cat_flex           pay_user_column_instances_f.value%type := null;
207 g_cat_segment        pay_user_column_instances_f.value%type := null;
208 g_f_type_name        hr_lookups.meaning%type;
209 g_Func_flex          pay_user_column_instances_f.value%type := null;
210 g_Func_segment       pay_user_column_instances_f.value%type := null;
211 
212 g_high1 number := 0;
213 g_high2 number := 0;
214 g_high3 number := 0;
215 g_high4 number := 0;
216 g_high5 number := 0;
217 
218 g_low1 number := 999999999999999;
219 g_low2 number := 999999999999999;
220 g_low3 number := 999999999999999;
221 g_low4 number := 999999999999999;
222 g_low5 number := 999999999999999;
223 
224 g_all_high_avg number := -9999;
225 g_all_low_avg  number := -9999;
226 
227 -- This procedure resets the list of highest and lowest values.
228 procedure reset_high_low_lists is
229 
230 l_proc constant varchar2(60) := g_package || 'reset_high_low_lists';
231 
232 begin
233 
234    hr_utility.set_location('Entering ' || l_proc, 10);
235 
236    g_high1 := 0;
237    g_high2 := 0;
238    g_high3 := 0;
239    g_high4 := 0;
240    g_high5 := 0;
241 
242    g_low1  := 999999999999999;
243    g_low2  := 999999999999999;
244    g_low3  := 999999999999999;
245    g_low4  := 999999999999999;
246    g_low5  := 999999999999999;
247 
248 end reset_high_low_lists;
249 
250 -- This procedure returns the average of the 5 highes and lowest values from the lists.
251 procedure calc_highest_and_lowest_avg
252 (
253    p_high_avg out nocopy number,
254    p_low_avg  out nocopy number
255 )  is
256 
257 l_high_avg number  := 0;
258 l_low_avg  number  := 0;
259 l_count    integer := 0;
260 
261 begin
262 
263    if g_high1 <> 0 then
264       l_count    := 1;
265       l_high_avg := g_high1;
266    end if;
267    if g_high2 <> 0 then
268       l_count    := l_count + 1;
269       l_high_avg := l_high_avg + g_high2;
270    end if;
271    if g_high3 <> 0 then
272       l_count    := l_count + 1;
273       l_high_avg := l_high_avg + g_high3;
274    end if;
275    if g_high4 <> 0 then
276       l_count    := l_count + 1;
277       l_high_avg := l_high_avg + g_high4;
278    end if;
279    if g_high5 <> 0 then
280       l_count    := l_count + 1;
281       l_high_avg := l_high_avg + g_high5;
282    end if;
283    if l_count = 0 then
284       p_high_avg := 0;
285    else
286       p_high_avg := l_high_avg / l_count;
287    end if;
288 
289    l_count := 0;
290    if g_low1 <> 999999999999999 then
291       l_count   := 1;
292       l_low_avg := g_low1;
293    end if;
294    if g_low2 <> 999999999999999 then
295       l_count   := l_count + 1;
296       l_low_avg := l_low_avg + g_low2;
297    end if;
298    if g_low3 <> 999999999999999 then
299       l_count   := l_count + 1;
300       l_low_avg := l_low_avg + g_low3;
301    end if;
302    if g_low4 <> 999999999999999 then
303       l_count   := l_count + 1;
304       l_low_avg := l_low_avg + g_low4;
305    end if;
306    if g_low5 <> 999999999999999 then
307       l_count   := l_count + 1;
308       l_low_avg := l_low_avg + g_low5;
309    end if;
310    if l_count = 0 then
311       p_low_avg := 0;
312    else
313       p_low_avg := l_low_avg / l_count;
314    end if;
315 --
316 exception
317    when others then
318     p_high_avg := null;
319     p_low_avg  := null;
320 --
321 
322 end calc_highest_and_lowest_avg;
323 
324 -- This procedure maintains a list of the 5 highest and lowest values passed to it.
325 procedure get_highest_and_lowest(p_value in number) is
326 
327 l_proc constant varchar2(60) := g_package || 'get_highest_and_lowest';
328 
329 begin
330 
331    hr_utility.set_location('Entering ' || l_proc, 10);
332    hr_utility.set_location('p_value ' || to_char(p_value), 20);
333 
334    -- Ignore the value if it is zero
335    if p_value <> 0 then
336 
337       -- Determine whether the value belongs in the highest list
338       if p_value > g_high5 then
339 
340          if p_Value > g_high4 then
341 
342             if p_value > g_high3 then
343 
344                if p_value > g_high2 then
345 
346                   if p_value > g_high1 then
347 
348                      g_high5 := g_high4;
349                      g_high4 := g_high3;
350                      g_high3 := g_high2;
351                      g_high2 := g_high1;
352                      g_high1 := p_value;
353 
354                   else
355 
356                      g_high5 := g_high4;
357                      g_high4 := g_high3;
358                      g_high3 := g_high2;
359                      g_high2 := p_value;
360 
361                   end if;
362 
363                else
364 
365                   g_high5 := g_high4;
366                   g_high4 := g_high3;
367                   g_high3 := p_value;
368 
369                end if;
370 
371             else
372 
373                g_high5 := g_high4;
374                g_high4 := p_value;
375 
376             end if;
377 
378          else
379 
380             g_high5 := p_value;
381 
382          end if;
383 
384       end if;
385 
386       hr_utility.set_location('g_high1 ' || to_char(g_high1), 40);
387       hr_utility.set_location('g_high2 ' || to_char(g_high2), 50);
388       hr_utility.set_location('g_high3 ' || to_char(g_high3), 60);
389       hr_utility.set_location('g_high4 ' || to_char(g_high4), 70);
390       hr_utility.set_location('g_high5 ' || to_char(g_high5), 80);
391 
392       -- Determine whether the value belongs in the lowest list
393       if p_value < g_low5 then
394 
395          if p_value < g_low4 then
396 
397             if p_value < g_low3 then
398 
399                if p_value < g_low2 then
400 
401                   if p_value < g_low1 then
402 
403                      g_low5 := g_low4;
404                      g_low4 := g_low3;
405                      g_low3 := g_low2;
406                      g_low2 := g_low1;
407                      g_low1 := p_value;
408 
409                   else
410 
411                      g_low5 := g_low4;
412                      g_low4 := g_low3;
413                      g_low3 := g_low2;
414                      g_low2 := p_value;
415 
416                   end if;
417 
418                else
419 
420                   g_low5 := g_low4;
421                   g_low4 := g_low3;
422                   g_low3 := p_value;
423 
424                end if;
425 
426             else
427 
428                g_low5 := g_low4;
429                g_low4 := p_value;
430 
431             end if;
432 
433          else
434 
435             g_low5 := p_value;
436 
437          end if;
438 
439       end if;
440 
441       hr_utility.set_location('g_low1 ' || to_char(g_low1), 90);
442       hr_utility.set_location('g_low2 ' || to_char(g_low2), 100);
443       hr_utility.set_location('g_low3 ' || to_char(g_low3), 110);
444       hr_utility.set_location('g_low4 ' || to_char(g_low4), 120);
445       hr_utility.set_location('g_low5 ' || to_char(g_low5), 130);
446 
447    end if; -- Zero check
448 
449 end get_highest_and_lowest;
450 
451 -- This function returns the number of days the assignment's status was Active Assignment
452 -- Note: Suspended Assignment is not seen as active in this case, since it is not
453 --       income generating
454 function get_active_days
455 (
456    p_assignment_id number,
457    p_report_start  date,
458    p_report_end    date
459 )  return number is
460 
461 l_count number;
462 
463 begin
464 
465    select sum
466           (
467              decode(sign(p_report_end - paaf.effective_end_date), 1, paaf.effective_end_date, p_report_end)
468              -
469              decode(sign(p_report_start - paaf.effective_start_date), 1, p_report_start, paaf.effective_start_date)
470              + 1
471           )
472    into   l_count
473    from   per_assignment_status_types past,
474           per_all_assignments_f       paaf
475    where  paaf.assignment_id = p_assignment_id
476    and    past.assignment_status_type_id = paaf.assignment_status_type_id
477    and    past.per_system_status = 'ACTIVE_ASSIGN'
478    and    paaf.effective_start_date <= p_report_end
479    and    paaf.effective_end_date   >= p_report_start;
480 
481    return l_count;
482 
483 exception
484    when no_data_found then
485       return 0;
486 
487 end get_active_days;
488 
489 -- This function returns the termination reason from the user tables.
490 function get_termination_reason
491 (
492    p_business_group_id in per_all_assignments_f.business_group_id%type,
493    p_report_date       in per_all_assignments_f.effective_end_date%type,
494    p_reason_code       in per_periods_of_service.leaving_reason%type
495 )  return varchar2 is
496 
497 l_termination_reason pay_user_column_instances_f.value%type;
498 
499 begin
500 
501    select pucifcat.value
502    into   l_termination_reason
503    from   pay_user_column_instances_f pucifcat,
504           pay_user_rows_f             purfcat,
505           pay_user_columns            puccat,
506           pay_user_rows_f             purfqc,
507           pay_user_column_instances_f pucifqc,
508           pay_user_columns            pucqc,
509           pay_user_tables             put
510    where  put.user_table_name = 'ZA_TERMINATION_CATEGORIES'
511    and    put.business_group_id is null
512    and    put.legislation_code = 'ZA'
513    and    pucqc.user_table_id = put.user_table_id
514    and    pucqc.user_column_name = 'Lookup Code'
515    and    pucifqc.user_column_id = pucqc.user_column_id
516    and    pucifqc.business_group_id = p_business_group_id
517    and    p_report_date between pucifqc.effective_start_date and pucifqc.effective_end_date
518    and    pucifqc.value = p_reason_code
519    and    purfqc.user_table_id = put.user_table_id
520    and    purfqc.user_row_id = pucifqc.user_row_id
521    and    purfqc.business_group_id = pucifqc.business_group_id
522    and    p_report_date between purfqc.effective_start_date and purfqc.effective_end_date
523    and    puccat.user_table_id = put.user_table_id
524    and    puccat.user_column_name = 'Termination Category'
525    and    purfcat.user_table_id = put.user_table_id
526    and    purfcat.business_group_id = pucifqc.business_group_id
527    and    p_report_date between purfcat.effective_start_date and purfcat.effective_end_date
528    and    purfcat.row_low_range_or_name = purfqc.row_low_range_or_name
529    and    pucifcat.user_column_id = puccat.user_column_id
530    and    pucifcat.user_row_id = purfcat.user_row_id
531    and    p_report_date between pucifcat.effective_start_date and pucifcat.effective_end_date
532    and    pucifcat.business_group_id = pucifqc.business_group_id
533    and    pucifcat.value in
534    (
535       'Resignation',
536       'Non-Renewal of Contract',
537       'Dismissal - Operational Requirements',
538       'Dismissal - Misconduct',
539       'Dismissal - Incapacity',
540       'Other'
541    );
542 
543    return l_termination_reason;
544 
545 exception
546    when no_data_found then
547       return 'No Leaving Reason';
548 
549 end get_termination_reason;
550 
551 -- This procedure resets all the data structures for the Income Differentials report.
552 procedure reset_tables is
553 
554 l_proc constant varchar2(60) := g_package || 'reset_tables';
555 
556 begin
557 
558    -- hr_utility.trace_on(null, 'T');
559    hr_utility.set_location('Entering ' || l_proc, 10);
560 
561    g_assignments_table.delete;
562    g_all_high_avg := -9999;
563    g_all_low_avg  := -9999;
564    g_cat_averages_table.delete;
565    g_lev_averages_table.delete;
566    g_cat_Enc_Diff_table.delete;
567    g_lev_Enc_Diff_table.delete;
568    DELETE FROM per_za_employment_equity
569     Where report_id IN
570                    ( 'ED1', 'ED2', 'ED1I','ED2I');
571 
572 end reset_tables;
573 
574 -- This function returns the average 5 highest paid employees per category or level.
575 function get_avg_5_highest_salary
576 (
577    p_report_date            in per_all_assignments_f.effective_end_date%type,
578    p_business_group_id      in per_all_assignments_f.business_group_id%type,
579    p_legent_param           in per_assignment_extra_info.aei_information7%type := null,
580    p_legal_entity_id        in per_assignment_extra_info.aei_information7%type := null,
581    p_occupational_level_cat in hr_lookups.meaning%type,
582    p_lookup_code            in hr_lookups.lookup_code%type,
583    p_occupational_type      in varchar2, -- CAT = Category, LEV = Level
584    p_salary_method          in varchar2  -- SAL = Salary Basis Method, BAL = Payroll Balances Method
585 )  return number is
586 
587 l_avg_5_highest_salary number;
588 l_index                number;
589 l_proc                 constant varchar2(60) := g_package || 'get_avg_5_highest_salary';
590 
591 begin
592 
593    hr_utility.set_location('Entering ' || l_proc, 10);
594 
595    -- The index is calculted by multiplying the legal entity id by 100 and then adding the lookup code
596    -- This should always give a unique value, since the lookup code is less than 100
597    begin
598 
599       l_index := p_legal_entity_id * 100 + p_lookup_code;
600       hr_utility.set_location('LEV/CAT INDEX ' || l_index, 25);
601 
602    exception
603       when others then
604          raise_application_error(-20006, 'The lookup code in the ZA_EMP_EQ_OCCUPATIONAL_LEV and ZA_EMP_EQ_OCCUPATIONAL_CAT lookups must be numeric.');
605 
606    end;
607 
608    -- First populate the cache tables, if necessary
609    -- Note: No check is made for the validity of the table data, since it is assumed that the
610    --       reset_tables procedure was called before this procedure.
611    -- This does not actually calculate the value, it just calls the lowest procedure
612    -- to populate the cache
613    if p_occupational_type = 'LEV' then
614 
615       -- Check whether the averages for the current occupational level already exist.
616       if not g_lev_averages_table.exists(l_index) then
617 
618          hr_utility.set_location('Step ' || l_proc, 20);
619          l_avg_5_highest_salary := get_avg_5_lowest_salary
620                                 (
621                                    p_report_date            => p_report_date,
622                                    p_business_group_id      => p_business_group_id,
623                                    p_legal_entity_id        => p_legal_entity_id,
624                                    p_occupational_level_cat => p_occupational_level_cat,
625                                    p_lookup_code            => p_lookup_code,
626                                    p_occupational_type      => p_occupational_type,
627                                    p_salary_method          => p_salary_method
628                                 );
629       end if;
630 
631    elsif p_occupational_type = 'CAT' then
632 
633       -- Check whether the averages for the current occupational category already exist.
634       if not g_cat_averages_table.exists(l_index) then
635 
636          hr_utility.set_location('Step ' || l_proc, 23);
637          l_avg_5_highest_salary := get_avg_5_lowest_salary
638                                 (
639                                    p_report_date            => p_report_date,
640                                    p_business_group_id      => p_business_group_id,
641                                    p_legal_entity_id        => p_legal_entity_id,
642                                    p_occupational_level_cat => p_occupational_level_cat,
643                                    p_lookup_code            => p_lookup_code,
644                                    p_occupational_type      => p_occupational_type,
645                                    p_salary_method          => p_salary_method
646                                 );
647       end if;
648 
649    elsif p_occupational_type is null then
650 
651       -- Check whether the averages already exist.
652       if g_all_high_avg = -9999 then
653 
654          hr_utility.set_location('Step ' || l_proc, 24);
655          l_avg_5_highest_salary := get_avg_5_lowest_salary
656                                 (
657                                    p_report_date            => p_report_date,
658                                    p_business_group_id      => p_business_group_id,
659                                    p_legal_entity_id        => p_legal_entity_id,
660                                    p_occupational_level_cat => p_occupational_level_cat,
661                                    p_lookup_code            => p_lookup_code,
662                                    p_occupational_type      => p_occupational_type,
663                                    p_salary_method          => p_salary_method
664                                 );
665 
666       end if;
667 
668    end if;
669 
670    hr_utility.set_location('Lookup Code ' || p_lookup_code, 25);
671 
672    -- Check Occupational Type
673    if p_occupational_type = 'LEV' then
674 
675       l_avg_5_highest_salary := g_lev_averages_table(l_index).high;
676       hr_utility.set_location('LEV ' || to_char(l_avg_5_highest_salary), 30);
677 
678    elsif p_occupational_type = 'CAT' then
679 
680       l_avg_5_highest_salary := g_cat_averages_table(l_index).high;
681       hr_utility.set_location('CAT ' || to_char(l_avg_5_highest_salary), 40);
682 
683    -- Average 5 highest salaries for all employees, irrespective of category or levels
684    -- elsif p_occupational_type not in ('CAT','LEV')
685    elsif p_occupational_type is null then
686 
687       l_avg_5_highest_salary := g_all_high_avg;
688       hr_utility.set_location('TOTAL ' || to_char(l_avg_5_highest_salary), 50);
689 
690    end if;
691 
692    hr_utility.set_location('Exiting ' || l_proc, 60);
693    return l_avg_5_highest_salary;
694 
695 end get_avg_5_highest_salary;
696 
697 -- This function returns the average 5 lowest paid employees per category or level.
698 function get_avg_5_lowest_salary
699 (
700    p_report_date            in per_all_assignments_f.effective_end_date%type,
701    p_business_group_id      in per_all_assignments_f.business_group_id%type,
702    p_legent_param           in per_assignment_extra_info.aei_information7%type := null,
703    p_legal_entity_id        in per_assignment_extra_info.aei_information7%type := null,
704    p_occupational_level_cat in hr_lookups.meaning%type,
705    p_lookup_code            in hr_lookups.lookup_code%type,
706    p_occupational_type      in varchar2, -- LEV = Levels, CAT = Categories
707    p_salary_method          in varchar2  -- SAL = Salary Basis Method, BAL = Payroll Balances Method
708 )  return number is
709 
710 cursor c_assignments is
711    select paaf.assignment_id,
712           paaf.person_id, -- Bug 4413678
713           paaf.payroll_id,
714           paei.aei_information7,
715           per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)    occupational_level,
716           per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id) occupational_category,
717           paaf.pay_basis_id
718    from   per_assignment_extra_info   paei,
719           per_assignment_status_types past,
720           per_all_assignments_f       paaf
721    where  paaf.business_group_id = p_business_group_id
722    and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
723    and    past.assignment_status_type_id = paaf.assignment_status_type_id
724    and    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
725    and    paei.assignment_id = paaf.assignment_id
726    and    paei.information_type = 'ZA_SPECIFIC_INFO'
727    and    paei.aei_information7 = nvl(p_legent_param, paei.aei_information7)
728    and    paei.aei_information7 is not null
729    and    nvl(paei.aei_information6, 'N') <> 'Y'
730    order  by paaf.payroll_id;
731 
732 l_avg_5_lowest_salary number;
733 l_old_payroll_id      per_all_assignments_f.payroll_id%type := -9999;
734 l_rowind              pls_integer;
735 l_active_days         number;
736 l_ee_income           number;
737 l_ee_annual_income    number;
738 l_report_start        date;
739 l_report_end          date;
740 l_report_date         date;
741 l_difference          number;
742 l_period_frequency    per_time_period_types.number_per_fiscal_year%type;
743 l_ee_balance_type_id  pay_balance_types.balance_type_id%type;
744 l_eea_balance_type_id pay_balance_types.balance_type_id%type;
745 l_input_value_id      pay_input_values_f.input_value_id%type;
746 l_index               number;
747 l_proc                constant varchar2(60) := g_package || 'get_avg_5_lowest_salary';
748 l_race                per_all_people_f.per_information4%type; -- Bug 4413678
749 
750 begin
751 
752    hr_utility.set_location('Entering ' || l_proc, 10);
753 
754    -- Determine whether we need to populate the cache tables
755    -- Note: No check is made for the validity of the table data, since it is assumed that the
756    --       reset_tables procedure was called before this procedure.
757    if g_assignments_table.count = 0 then
758 
759       hr_utility.set_location('Setup assignments cache', 20);
760       g_assignments_table.delete;
761 
762       if p_salary_method = 'BAL' then
763 
764          -- Get the balance type id's for the normal and annual balances
765          begin
766 
767             select balance_type_id
768             into   l_ee_balance_type_id
769             from   pay_balance_types
770             where  balance_name = 'Total Employment Equityable Income'
771             and    legislation_code = 'ZA'
772             and    business_group_id is null;
773 
774             select balance_type_id
775             into   l_eea_balance_type_id
776             from   pay_balance_types
777             where  balance_name = 'Total Employment Equityable Annual Income'
778             and    legislation_code = 'ZA'
779             and    business_group_id is null;
780 
781          exception
782             when no_data_found then
783                raise_application_error(-20000, 'The Employment Equitable balances do not exist.');
784 
785          end;
786 
787       end if;
788 
789       if p_salary_method = 'ELE' then
790 
791          -- Get the ZA Employment Equity Remuneration element details
792          begin
793 
794             select pivf.input_value_id
795             into   l_input_value_id
796             from   pay_input_values_f         pivf,
797                    pay_element_types_f        petf
798             where  petf.element_name = 'ZA Employment Equity Remuneration'
799             and    petf.business_group_id is null
800             and    petf.legislation_code = 'ZA'
801             and    p_report_date between petf.effective_start_date and petf.effective_end_date
802             and    pivf.element_type_id = petf.element_type_id
803             and    pivf.name = 'Remuneration'
804             and    p_report_date between pivf.effective_start_date and pivf.effective_end_date;
805 
806          exception
807             when no_data_found then
808                raise_application_error(-20004, 'The ZA Employment Equity Remuneration element does not exist.');
809 
810          end;
811 
812       end if;
813 
814       -- Loop through the assignments cursor and populate the assignments table
815       for l_assignment in c_assignments loop
816 
817          hr_utility.set_location('ASG ' || l_assignment.assignment_id, 21);
818 
819          -- Bug 4413678: Begin
820          Select per_information4
821          into l_race
822          From per_all_people_f papf
823          Where papf.person_id = l_assignment.person_id
824                and p_report_date between papf.effective_start_date and papf.effective_end_date;
825          -- Bug 4413678: End
826 
827 
828          if l_assignment.payroll_id is not null and l_race <> 'N' then -- Bug 4413678: Added l_race <> 'Not Used'
829 
830             g_assignments_table(l_assignment.assignment_id).payroll_id            := l_assignment.payroll_id;
831             g_assignments_table(l_assignment.assignment_id).legal_entity_id       := l_assignment.aei_information7;
832             g_assignments_table(l_assignment.assignment_id).occupational_level    := l_assignment.occupational_level;
833             g_assignments_table(l_assignment.assignment_id).occupational_category := l_assignment.occupational_category;
834 
835             hr_utility.set_location('LEGENT ' || l_assignment.aei_information7, 22);
836 
837             -- Check for a new payroll_id and cache the new payroll details in the payrolls table
838             if l_assignment.payroll_id <> l_old_payroll_id then
839 
840                -- Get the start date and end date of the report
841                begin
842 
843 
844                   l_report_date := p_report_date;
845                   l_difference := 0;
846 
847                   while (l_difference < 355 or l_difference > 375) loop
848 
849                      select ptpf.end_date + 1,
850                             ptpl.end_date
851                      into   l_report_start,
852                             l_report_end
853                      from   per_time_periods ptpf,
854                             per_time_periods ptpl
855                      where  ptpl.payroll_id = l_assignment.payroll_id
856                      and    l_report_date between ptpl.start_date and ptpl.end_date
857                      and    ptpf.payroll_id = l_assignment.payroll_id
858                      and    add_months(l_report_date, -12) + 1 between ptpf.start_date and ptpf.end_date;
859 
860                      l_difference := l_report_end - l_report_start + 1;
861 
862                      if (l_difference < 355 or l_difference > 375) then
863 
864                         l_report_date := l_report_date - 1;
865 
866                      end if;
867 
868                   end loop;
869 
870                exception
871                   when no_data_found then
872                      begin
873                              select ptpl.end_date
874                              into   l_report_end
875                              from   per_time_periods ptpl
876                              where  ptpl.payroll_id = l_assignment.payroll_id
877                              and    p_report_date between ptpl.start_date and ptpl.end_date;
878 
879                      exception
880                              when no_data_found then
881                                       Null;
882                      end;
883 
884                      l_report_start := add_months(l_report_end, -12) + 1;
885 
886                end;
887 
888                -- Get the payroll period frequency
889                begin
890 
891                   select ptpt.number_per_fiscal_year
892                   into   l_period_frequency
893                   from   per_time_period_types ptpt,
894                          pay_all_payrolls_f    payr
895                   where  payr.payroll_id = l_assignment.payroll_id
896                   and    p_report_date between payr.effective_start_date and payr.effective_end_date
897                   and    ptpt.period_type = payr.period_type;
898 
899                exception
900                   when no_data_found then
901                      raise_application_error(-20005, 'The Payroll Period Frequency does not exist.');
902 
903                end;
904 
905                l_old_payroll_id := l_assignment.payroll_id;
906 
907             end if;
908 
909             hr_utility.set_location('REP_START ' || to_char(l_report_start, 'DD\MM\YYYY'), 22);
910             hr_utility.set_location('REP_END   ' || to_char(l_report_end, 'DD\MM\YYYY'), 23);
911             hr_utility.set_location('FREQ      ' || l_period_frequency, 24);
912 
913             if p_salary_method = 'BAL' then
914 
915                -- Get the amount of days the assignment status was Active Assignment
916                l_active_days := get_active_days
917                                 (
918                                    p_assignment_id => l_assignment.assignment_id,
919                                    p_report_start  => l_report_start,
920                                    p_report_end    => l_report_end
921                                 );
922 
923                hr_utility.set_location('ACT_DAYS ' || l_active_days, 25);
924 
925                -- Get the Employment Equitable Income
926                begin
927 
928                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
929                   into   l_ee_income
930                   from   pay_balance_feeds_f         pbff,
931                          pay_run_result_values       prrv,
932                          pay_run_results             prr,
933                          pay_payroll_actions         ppa,
934                          pay_assignment_actions      paa,
935                          per_assignments_f       asg     --Bug 4872110
936                   -- BUG 2665394 ADDED THE TABLE TO IMPROVE THE PERFORMANCE
937                   where  paa.assignment_id = l_assignment.assignment_id
938                   and    ppa.payroll_action_id = paa.payroll_action_id
939                   and    ppa.date_earned between l_report_start and l_report_end
940                   and    prr.assignment_action_id = paa.assignment_action_id
941                   and    prrv.run_result_id = prr.run_result_id
942                   and    pbff.balance_type_id = l_ee_balance_type_id
943                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
944                   and    prrv.input_value_id = pbff.input_value_id
945                   -- BUG 2665394 ADDED THE JOINS TO IMPROVE THE PERFORMANCE
946                   and    paa.assignment_id = asg.assignment_id
947                   and    asg.payroll_id = ppa.payroll_id;
948 
949                exception
950                   when no_data_found then
951                      l_ee_income := 0;
952 
953                end;
954 
955                -- Get the Employment Equitable Annual Income
956                begin
957 
958                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
959                   into   l_ee_annual_income
960                   from   pay_balance_feeds_f         pbff,
961                          pay_run_result_values       prrv,
962                          pay_run_results             prr,
963                          pay_payroll_actions         ppa,
964                          pay_assignment_actions      paa,
965                          per_assignments_f       asg     --Bug 4872110
966                   -- BUG 2665394 ADDED THE TABLE TO IMPROVE THE PERFORMANCE
967                   where  paa.assignment_id = l_assignment.assignment_id
968                   and    ppa.payroll_action_id = paa.payroll_action_id
969                   and    ppa.date_earned between l_report_start and l_report_end
970                   and    prr.assignment_action_id = paa.assignment_action_id
971                   and    prrv.run_result_id = prr.run_result_id
972                   and    pbff.balance_type_id = l_eea_balance_type_id
973                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
974                   and    prrv.input_value_id = pbff.input_value_id
975                   -- BUG 2665394 ADDED THE JOINS TO IMPROVE THE PERFORMANCE
976                   and    paa.assignment_id = asg.assignment_id
977                   and    asg.payroll_id = ppa.payroll_id;
978 
979                exception
980                   when no_data_found then
981                      l_ee_annual_income := 0;
982 
983                end;
984 
985                hr_utility.set_location('EE_INC ' || l_ee_income, 26);
986                hr_utility.set_location('EE_ANN ' || l_ee_annual_income, 27);
987                hr_utility.set_location('STminEND ' || (l_report_end - l_report_start + 1), 28);
988 
989                -- Calculate the annual income = annualize normal income + annual income
990                g_assignments_table(l_assignment.assignment_id).annual_income :=
991                   (
992                      (l_report_end - l_report_start + 1) / l_active_days * l_ee_income
993                   ) + l_ee_annual_income;
994 
995                hr_utility.set_location('ANSWER ' || g_assignments_table(l_assignment.assignment_id).annual_income, 29);
996 
997             elsif p_salary_method = 'SAL' then
998 
999                -- Get the annual salary basis for the current period
1000                begin
1001 
1002                   select ppp.proposed_salary_n * ppb.pay_annualization_factor
1003                   into   g_assignments_table(l_assignment.assignment_id).annual_income
1004                   from   per_pay_proposals ppp,
1005                          per_pay_bases     ppb
1006                   where  ppb.pay_basis_id = l_assignment.pay_basis_id
1007                   and    ppp.assignment_id = l_assignment.assignment_id
1008                   and    ppp.approved = 'Y'
1009                   and    ppp.change_date =
1010                   (
1011                      select max(ppp2.change_date)
1012                      from   per_pay_proposals ppp2
1013                      where  ppp2.assignment_id = l_assignment.assignment_id
1014                      and    ppp2.change_date <= p_report_date
1015                      and    ppp2.approved = 'Y'
1016                   );
1017 
1018                exception
1019                   when no_data_found then
1020                      g_assignments_table(l_assignment.assignment_id).annual_income := 0;
1021 
1022                end;
1023 
1024             elsif p_salary_method = 'ELE' then
1025 
1026                begin
1027 
1028                   select peevf.screen_entry_value * l_period_frequency
1029                   into   g_assignments_table(l_assignment.assignment_id).annual_income
1030                   from   pay_element_entry_values_f peevf,
1031                          pay_element_entries_f      peef
1032                   where  peef.assignment_id = l_assignment.assignment_id
1033                   and    p_report_date between peef.effective_start_date and peef.effective_end_date
1034                   and    peevf.element_entry_id = peef.element_entry_id
1035                   and    peevf.input_value_id = l_input_value_id
1036                   and    p_report_date between peevf.effective_start_date and peevf.effective_end_date;
1037 
1038                exception
1039                   when no_data_found then
1040                      g_assignments_table(l_assignment.assignment_id).annual_income := 0;
1041 
1042                end;
1043 
1044             end if;   -- p_salary_method
1045 
1046          end if;   -- (l_assignment.payroll_id is not null)
1047 
1048       end loop;   -- c_assignments
1049 
1050    end if;   -- g_assignments_table.count = 0
1051 
1052    -- The index is calculted by multiplying the legal entity id by 100 and then adding the lookup code
1053    -- This should always give a unique value, since the lookup code is less than 100
1054    begin
1055 
1056       l_index := p_legal_entity_id * 100 + p_lookup_code;
1057       hr_utility.set_location('LEV/CAT INDEX ' || l_index, 25);
1058 
1059    exception
1060       when others then
1061          raise_application_error(-20006, 'The lookup code in the ZA_EMP_EQ_OCCUPATIONAL_LEV and ZA_EMP_EQ_OCCUPATIONAL_CAT lookups must be numeric.');
1062 
1063    end;
1064 
1065    if p_occupational_type = 'LEV' then
1066 
1067       hr_utility.set_location('LEV cache check' || p_lookup_code, 30);
1068 
1069       -- Check whether the averages for the current occupational level already exist.
1070       if not g_lev_averages_table.exists(l_index) then
1071 
1072          hr_utility.set_location('LEV cache' || p_lookup_code, 40);
1073          reset_high_low_lists;
1074 
1075          -- Loop through assignments cache table to look for current occupational level
1076          l_rowind := g_assignments_table.first;
1077          loop
1078 
1079             exit when l_rowind is null;
1080 
1081             -- If the occupational category of the assignment is the same as the one we are
1082             -- looking for then add the value to the highest and lowest list
1083             if  g_assignments_table(l_rowind).occupational_level = p_occupational_level_cat
1084             and g_assignments_table(l_rowind).legal_entity_id = p_legal_entity_id then
1085 
1086                get_highest_and_lowest(g_assignments_table(l_rowind).annual_income);
1087 
1088             end if;
1089 
1090             l_rowind := g_assignments_table.next(l_rowind);
1091 
1092          end loop;
1093 
1094          -- Calculate the average of the 5 highest and lowest values in the list,
1095          -- and add the answers to the occupational level average cache table
1096          calc_highest_and_lowest_avg
1097          (
1098             g_lev_averages_table(l_index).high,
1099             g_lev_averages_table(l_index).low
1100          );
1101          l_avg_5_lowest_salary := g_lev_averages_table(l_index).low;
1102 
1103       else
1104 
1105          l_avg_5_lowest_salary := g_lev_averages_table(l_index).low;
1106 
1107       end if;
1108 
1109    elsif p_occupational_type = 'CAT' then
1110 
1111       hr_utility.set_location('CAT cache check' || p_lookup_code, 50);
1112 
1113       -- Check whether the averages for the current occupational category already exist.
1114       if not g_cat_averages_table.exists(l_index) then
1115 
1116          hr_utility.set_location('CAT cache' || p_lookup_code, 60);
1117          reset_high_low_lists;
1118 
1119          -- Loop through assignments cache table to look for current occupational category
1120          l_rowind := g_assignments_table.first;
1121          loop
1122 
1123             exit when l_rowind is null;
1124 
1125             -- If the occupational category of the assignment is the same as the one we are
1126             -- looking for then add the value to the highest and lowest list
1127             if  g_assignments_table(l_rowind).occupational_category = p_occupational_level_cat
1128             and g_assignments_table(l_rowind).legal_entity_id = p_legal_entity_id then
1129 
1130                get_highest_and_lowest(g_assignments_table(l_rowind).annual_income);
1131 
1132             end if;
1133 
1134             l_rowind := g_assignments_table.next(l_rowind);
1135 
1136          end loop;
1137 
1138          -- Calculate the average of the 5 highest and lowest values in the list,
1139          -- and add the answers to the occupational category average cache table
1140          calc_highest_and_lowest_avg
1141          (
1142             g_cat_averages_table(l_index).high,
1143             g_cat_averages_table(l_index).low
1144          );
1145          l_avg_5_lowest_salary := g_cat_averages_table(l_index).low;
1146 
1147       else
1148 
1149          l_avg_5_lowest_salary := g_cat_averages_table(l_index).low;
1150 
1151       end if;
1152 
1153    --  elsif p_occupational_type not in ('CAT', 'LEV')
1154    elsif p_occupational_type is null then
1155 
1156       hr_utility.set_location('TOTAL cache check', 50);
1157 
1158       -- Check whether the averages already exist.
1159       if g_all_high_avg = -9999 then
1160 
1161          hr_utility.set_location('TOTAL cache', 60);
1162          reset_high_low_lists;
1163 
1164          -- Loop through assignments cache table to look for current occupational category
1165          l_rowind := g_assignments_table.first;
1166          loop
1167 
1168             exit when l_rowind is null;
1169 
1170             -- Add the value to the highest and lowest list
1171             get_highest_and_lowest(g_assignments_table(l_rowind).annual_income);
1172 
1173             l_rowind := g_assignments_table.next(l_rowind);
1174 
1175          end loop;
1176 
1177          -- Calculate the average of the 5 highest and lowest values in the list,
1178          -- and add the answers to the occupational level average cache table
1179          calc_highest_and_lowest_avg
1180          (
1181             g_all_high_avg,
1182             g_all_low_avg
1183          );
1184          l_avg_5_lowest_salary := g_all_low_avg;
1185 
1186       else
1187 
1188          l_avg_5_lowest_salary := g_all_low_avg;
1189 
1190       end if;
1191 
1192    end if;
1193 
1194    return l_avg_5_lowest_salary;
1195 
1196 end get_avg_5_lowest_salary;
1197 
1198 -- This function returns the person's legislated employment type (permanent or non-permanent)
1199 -- The employee has to work for a continuous period of 3 months in order to be seen as permanent
1200 function get_ee_employment_type_name
1201 (
1202    p_report_date          in per_all_people_f.start_date%type,
1203    p_period_of_service_id in per_all_assignments_f.period_of_service_id%type
1204 )  return varchar2 is
1205 
1206 l_ee_employment_type_name    varchar2(13);
1207 l_date_start                 per_periods_of_service.date_start%type;
1208 l_date_end                   date;
1209 l_actual_termination_date    per_periods_of_service.actual_termination_date%type;
1210 l_projected_termination_date per_periods_of_service.projected_termination_date%type;
1211 
1212 begin
1213 
1214    select date_start,
1215           actual_termination_date,
1216           projected_termination_date
1217    into   l_date_start,
1218           l_actual_termination_date,
1219           l_projected_termination_date
1220    from   per_periods_of_service
1221    where  period_of_service_id = p_period_of_service_id;
1222 
1223    if l_actual_termination_date is null then
1224 
1225       if l_projected_termination_date is null then
1226 
1227          l_date_end := to_date('30-12-4712', 'DD-MM-YYYY');
1228 
1229       else
1230 
1231          -- If the report date is after the projected termination date,
1232          -- then the projected termination date is discarded
1233          if p_report_date > l_projected_termination_date then
1234 
1235             l_date_end := to_date('30-12-4712', 'DD-MM-YYYY');
1236 
1237          else   -- Use the projected termination date
1238 
1239             l_date_end := l_projected_termination_date;
1240 
1241          end if;
1242 
1243       end if;
1244 
1245    else
1246 
1247       l_date_end := l_actual_termination_date;
1248 
1249    end if;
1250 
1251    if months_between(l_date_end + 1, l_date_start) < 3 then
1252       l_ee_employment_type_name := 'Non-Permanent';
1253    else
1254       l_ee_employment_type_name := 'Permanent';
1255    end if;
1256 
1257    return l_ee_employment_type_name;
1258 
1259 exception
1260    when no_data_found then
1261       return 'Non-Permanent';
1262 
1263 end get_ee_employment_type_name;
1264 
1265 -- This function returns the occupational category from the common lookups table.
1266 function get_occupational_category
1267 (
1268    p_report_date       in per_all_assignments_f.effective_end_date%type,
1269    p_assignment_id     in per_all_assignments_f.assignment_id%type,
1270    p_job_id            in per_all_assignments_f.job_id%type,
1271    p_grade_id          in per_all_assignments_f.grade_id%type,
1272    p_position_id       in per_all_assignments_f.position_id%type,
1273    p_business_group_id in per_all_assignments_f.business_group_id%type
1274 )  return varchar2 is
1275 
1276 begin
1277 
1278    -- Check whether we have cached the location of Occupational data
1279    if g_cat_flex is null then
1280 
1281       cache_occupational_location(p_report_date, p_business_group_id);
1282 
1283    end if;
1284 
1285    -- Check whether the current assignment's value is cached already
1286    if  p_report_date   = g_cat_report_date
1287    and p_assignment_id = g_cat_asg_id then
1288 
1289       return g_cat_name;
1290 
1291    else
1292 
1293       g_cat_report_date := p_report_date;
1294       g_cat_asg_id      := p_assignment_id;
1295 
1296       g_cat_name := get_occupational_data
1297                     (
1298                        p_type        => 'CAT',
1299                        p_flex        => g_cat_flex,
1300                        p_segment     => g_cat_segment,
1301                        p_job_id      => p_job_id,
1302                        p_grade_id    => p_grade_id,
1303                        p_position_id => p_position_id
1304                     );
1305 
1306       return g_cat_name;
1307 
1308    end if;
1309 
1310 end get_occupational_category;
1311 
1312 -- This function returns the occupational levels from the user tables.
1313 function get_occupational_level
1314 (
1315    p_report_date       in per_all_assignments_f.effective_end_date%type,
1316    p_assignment_id     in per_all_assignments_f.assignment_id%type,
1317    p_job_id            in per_all_assignments_f.job_id%type,
1318    p_grade_id          in per_all_assignments_f.grade_id%type,
1319    p_position_id       in per_all_assignments_f.position_id%type,
1320    p_business_group_id in per_all_assignments_f.business_group_id%type,
1321    p_year              in number
1322 )  return varchar2 is
1323 
1324 begin
1325 
1326    -- Check whether we have cached the location of Occupational data
1327    if g_lev_flex is null then
1328 
1329       cache_occupational_location(p_report_date, p_business_group_id,p_year);
1330 
1331    end if;
1332 
1333    -- Check whether the current assignment's value is cached already
1334    if  p_report_date   = g_lev_report_date
1335    and p_assignment_id = g_lev_asg_id then
1336 
1337       return g_lev_name;
1338 
1339    else
1340 
1341       g_lev_report_date := p_report_date;
1342       g_lev_asg_id      := p_assignment_id;
1343 
1344       g_lev_name := get_occupational_data
1345                     (
1346                        p_type        => 'LEV',
1347                        p_flex        => g_lev_flex,
1348                        p_segment     => g_lev_segment,
1349                        p_job_id      => p_job_id,
1350                        p_grade_id    => p_grade_id,
1351                        p_position_id => p_position_id
1352                     );
1353 
1354       return g_lev_name;
1355 
1356    end if;
1357 
1358 end get_occupational_level;
1359 
1360 function get_occupational_cat_data
1361 (
1362    p_type        in varchar2,
1363    p_flex        in varchar2,
1364    p_segment     in varchar2,
1365    p_job_id      in per_all_assignments_f.job_id%type,
1366    p_grade_id    in per_all_assignments_f.grade_id%type,
1367    p_position_id in per_all_assignments_f.position_id%type
1368 )  return varchar2 is
1369 
1370 l_name hr_lookups.meaning%type;
1371 l_sql varchar2(32767);
1372 begin
1373 
1374    hr_utility.set_location('p_job_id '||p_job_id , 30);
1375    hr_utility.set_location('p_grade_id '||p_grade_id , 30);
1376    hr_utility.set_location('p_position_id '||p_position_id , 30);
1377 
1378    if p_flex = 'Job' then
1379       begin
1380 
1381          if p_job_id is not null then
1382 --            hr_utility.set_location('Security_grp_Id    :' || fnd_global.lookup_security_group('ZA_WSP_OCCUPATIONAL_CATEGORIES',3),30);
1383             l_sql := 'select decode(flv1.attribute1,null,flv1.meaning, flv2.meaning) from fnd_lookup_values flv1, fnd_lookup_values flv2, per_job_definitions pjd, per_jobs pj where pj.job_id = '
1384                      || to_char(p_job_id)
1385                      || '  and pjd.job_definition_id = pj.job_definition_id '
1386                      || ' and flv1.lookup_type = '||'''ZA_WSP_OCCUPATIONAL_CATEGORIES'''
1387                      || ' and flv1.lookup_code = pjd.' || p_segment
1388                      || ' and   flv2.lookup_type(+) = '||'''ZA_EMP_EQ_OCCUPATIONAL_CAT'''
1389                      || ' and   flv2.lookup_code(+) = flv1.attribute1'
1390                      || ' and   flv1.security_group_id = fnd_global.lookup_security_group(flv1.lookup_type,3)'
1391                      || ' and   flv1.language = userenv('||'''LANG'''||')'
1392                      || ' and   flv2.language(+) = userenv('||'''LANG'''||')';
1393 
1394 
1395             execute immediate l_sql into l_name;
1396          else
1397 
1398             l_name := null;
1399 
1400          end if;
1401 
1402       exception
1403          when no_data_found then
1404             l_name := null;
1405 
1406       end;
1407 
1408    elsif p_flex = 'Grade' then
1409 
1410       begin
1411          if p_grade_id is not null then
1412 
1413             l_sql := 'select decode(flv1.attribute1,null,flv1.meaning, flv2.meaning) from fnd_lookup_values flv1, fnd_lookup_values flv2, per_grade_definitions pgd, per_grades pg where pg.grade_id = '
1414                      || to_char(p_grade_id)
1415                      || ' and  pgd.grade_definition_id = pg.grade_definition_id '
1416                      || ' and flv1.lookup_type = '||'''ZA_WSP_OCCUPATIONAL_CATEGORIES'''
1417                      || ' and  flv1.lookup_code = pgd.' || p_segment
1418                      || ' and   flv2.lookup_type(+) = '||'''ZA_EMP_EQ_OCCUPATIONAL_CAT'''
1419                      || ' and   flv2.lookup_code(+) = flv1.attribute1'
1420                        || ' and   flv1.security_group_id = fnd_global.lookup_security_group(flv1.lookup_type,3)'
1421                      || ' and   flv1.language = userenv('||'''LANG'''||')'
1422                      || ' and   flv2.language(+) = userenv('||'''LANG'''||')';
1423 
1424             execute immediate l_sql into l_name;
1425 
1426          else
1427 
1428             l_name := null;
1429 
1430          end if;
1431 
1432       exception
1433          when no_data_found then
1434             l_name := null;
1435 
1436       end;
1437 
1438    elsif p_flex = 'Position' then
1439 
1440       begin
1441          if p_position_id is not null then
1442 
1443             l_sql := 'select decode(flv1.attribute1,null,flv1.meaning, flv2.meaning) from fnd_lookup_values flv1, fnd_lookup_values flv2, per_position_definitions ppd, per_all_positions pap where pap.position_id = '
1444                      || to_char(p_position_id)
1445                      || '  and ppd.position_definition_id = pap.position_definition_id '
1446                      || '  and flv1.lookup_type = '||'''ZA_WSP_OCCUPATIONAL_CATEGORIES'''
1447                      || ' and flv1.lookup_code = ppd.' || p_segment
1448                      || ' and   flv2.lookup_type(+) = '||'''ZA_EMP_EQ_OCCUPATIONAL_CAT'''
1449                      || ' and   flv2.lookup_code(+) = flv1.attribute1'
1450                      || ' and   flv1.security_group_id = fnd_global.lookup_security_group(flv1.lookup_type,3)'
1451                      || ' and   flv1.language = userenv('||'''LANG'''||')'
1452                      || ' and   flv2.language(+) = userenv('||'''LANG'''||')';
1453 
1454 
1455             execute immediate l_sql into l_name;
1456 
1457          else
1458 
1459             l_name := null;
1460 
1461          end if;
1462 
1463       exception
1464          when no_data_found then
1465             l_name := null;
1466 
1467       end;
1468 
1469    else
1470 
1471       raise_application_error(-20002, 'The Occupational data in the User Table ZA_OCCUPATIONAL_TYPES refers to an invalid Flexfield.');
1472 
1473    end if;
1474 
1475 RETURN l_name;
1476 
1477 END get_occupational_cat_data;
1478 
1479 
1480 -- This function retrieves the occupational data via dynamic sql from the appropriate flexfield segment
1481 /*
1482 08-Jan-2008
1483 Logic for changes in the get_occupational_data
1484 if the p_type = 'CAT' -- Category
1485 check if the lookup_type 'ZA_WSP_OCCUPATIONAL_CATEGORIES' present in db.
1486 if it is present the Grade/Job/Position flexfields will have the
1487 cotegory code from the ZA_WSP_OCCUPATIONAL_CATEGORIES stored on the flexfield.
1488 And the corresponding the Employment Equity code we have to get from
1489 ZA_EMP_EQ_OCCUPATIONAL_CAT .
1490 
1491 */
1492 
1493 function get_occupational_data
1494 (
1495    p_type        in varchar2,
1496    p_flex        in varchar2,
1497    p_segment     in varchar2,
1498    p_job_id      in per_all_assignments_f.job_id%type,
1499    p_grade_id    in per_all_assignments_f.grade_id%type,
1500    p_position_id in per_all_assignments_f.position_id%type
1501 )  return varchar2 is
1502 
1503 l_sql  varchar2(32767);
1504 l_indicator  NUMBER;
1505 l_name hr_lookups.meaning%type;
1506 l_code hr_lookups.lookup_code%type;
1507 l_lookup_type hr_lookups.lookup_type%type;
1508 
1509 begin
1510    -- Added 08-Jan-2008
1511    l_indicator := 0;
1512    hr_utility.set_location('p_type '||p_type,20);
1513    hr_utility.set_location('p_flex '||p_flex,20);
1514    hr_utility.set_location('p_segment'||p_segment,20);
1515    IF p_type = 'CAT' then
1516      Select COUNT(*)
1517      INTO   l_indicator
1518      FROM   hr_lookups
1519      WHERE  lookup_type = 'ZA_WSP_OCCUPATIONAL_CATEGORIES';
1520    END IF ;
1521 
1522    hr_utility.set_location('l_indicator'||l_indicator, 20);
1523 
1524    IF l_indicator > 0 THEN
1525       l_name := get_occupational_cat_data
1526                   (
1527                    p_type        => p_type,
1528                    p_flex        => p_flex,
1529                    p_segment     => p_segment,
1530                    p_job_id      => p_job_id,
1531                    p_grade_id    => p_grade_id,
1532                    p_position_id => p_position_id
1533                    );
1534 
1535    RETURN l_name;
1536    END IF ;
1537    --End
1538 
1539    if p_flex = 'Job' then
1540 
1541       begin
1542          if p_job_id is not null then
1543             l_sql := 'select hl.meaning from hr_lookups hl, per_job_definitions pjd, per_jobs pj where pj.job_id = '
1544                      || to_char(p_job_id)
1545                      || '  and pjd.job_definition_id = pj.job_definition_id and hl.application_id = 800 and hl.lookup_type = ''ZA_EMP_EQ_OCCUPATIONAL_'
1546                      || p_type || ''' and hl.lookup_code = pjd.' || p_segment;
1547 
1548             execute immediate l_sql into l_name;
1549 
1550          else
1551 
1552             l_name := null;
1553 
1554          end if;
1555 
1556       exception
1557          when no_data_found then
1558             l_name := null;
1559 
1560       end;
1561 
1562    elsif p_flex = 'Grade' then
1563 
1564       begin
1565          if p_grade_id is not null then
1566             l_sql := 'select hl.meaning from hr_lookups hl, per_grade_definitions pgd, per_grades pg where pg.grade_id = '
1567                      || to_char(p_grade_id)
1568                      || '  and pgd.grade_definition_id = pg.grade_definition_id and hl.application_id = 800 and hl.lookup_type = ''ZA_EMP_EQ_OCCUPATIONAL_'
1569                      || p_type || ''' and hl.lookup_code = pgd.' || p_segment;
1570 
1571             execute immediate l_sql into l_name;
1572 
1573          else
1574 
1575             l_name := null;
1576 
1577          end if;
1578 
1579       exception
1580          when no_data_found then
1581             l_name := null;
1582 
1583       end;
1584 
1585    elsif p_flex = 'Position' then
1586 
1587       begin
1588          if p_position_id is not null then
1589             l_sql := 'select hl.meaning from hr_lookups hl, per_position_definitions ppd, per_all_positions pap where pap.position_id = '
1590                      || to_char(p_position_id)
1591                      || '  and ppd.position_definition_id = pap.position_definition_id and hl.application_id = 800 and hl.lookup_type = ''ZA_EMP_EQ_OCCUPATIONAL_'
1592                      || p_type || ''' and hl.lookup_code = ppd.' || p_segment;
1593 
1594             execute immediate l_sql into l_name;
1595 
1596          else
1597 
1598             l_name := null;
1599 
1600          end if;
1601 
1602       exception
1603          when no_data_found then
1604             l_name := null;
1605 
1606       end;
1607 
1608    else
1609 
1610       raise_application_error(-20002, 'The Occupational data in the User Table ZA_OCCUPATIONAL_TYPES refers to an invalid Flexfield.');
1611 
1612    end if;
1613 
1614    return l_name;
1615 
1616 end get_occupational_data;
1617 
1618 -- This procedure caches the location of the occupational category and level data.
1619 procedure cache_occupational_location
1620 (
1621    p_report_date       in date,
1622    p_business_group_id in per_all_assignments_f.business_group_id%type,
1623    p_year              in number
1624 )  is
1625 
1626 l_user_table_id       pay_user_tables.user_table_id%type;
1627 l_user_column_id_flex pay_user_columns.user_column_id%type;
1628 l_user_column_id_seg  pay_user_columns.user_column_id%type;
1629 l_user_row_id_cat     pay_user_rows_f.user_row_id%type;
1630 l_user_row_id_lev     pay_user_rows_f.user_row_id%type;
1631 l_user_row_id_func    pay_user_rows_f.user_row_id%type;
1632 l_temp                varchar2(9);
1633 
1634 begin
1635 --   hr_utility.trace_on(null,'PERZAEER');
1636 
1637    select user_table_id
1638    into   l_user_table_id
1639    from   pay_user_tables
1640    where  user_table_name = 'ZA_OCCUPATIONAL_TYPES'
1641    and    business_group_id is null
1642    and    legislation_code = 'ZA';
1643 
1644    hr_utility.set_location('l_user_table_id'||l_user_table_id, 10);
1645 
1646    select user_column_id
1647    into   l_user_column_id_flex
1648    from   pay_user_columns
1649    where  user_table_id = l_user_table_id
1650    and    business_group_id is null
1651    and    legislation_code = 'ZA'
1652    and    user_column_name = 'Flexfield';
1653 
1654    select user_column_id
1655    into   l_user_column_id_seg
1656    from   pay_user_columns
1657    where  user_table_id = l_user_table_id
1658    and    business_group_id is null
1659    and    legislation_code = 'ZA'
1660    and    user_column_name = 'Segment';
1661 
1662    --Added if condition for Bug 9462039 as Occupational Categories not required from 2009 reporting year
1663    if p_year < 2009 then
1664            select user_row_id
1665            into   l_user_row_id_cat
1666            from   pay_user_rows_f
1667            where  user_table_id = l_user_table_id
1668            and    row_low_range_or_name = 'Occupational Categories'
1669            and    p_report_date between effective_start_date and effective_end_date;
1670 
1671            select value
1672            into   g_cat_flex
1673            from   pay_user_column_instances_f
1674            where  user_row_id    = l_user_row_id_cat
1675            and    user_column_id = l_user_column_id_flex
1676            and    business_group_id = p_business_group_id
1677            and    p_report_date between effective_start_date and effective_end_date;
1678 
1679            select value
1680            into   g_cat_segment
1681            from   pay_user_column_instances_f
1682            where  user_row_id    = l_user_row_id_cat
1683            and    user_column_id = l_user_column_id_seg
1684            and    business_group_id = p_business_group_id
1685            and    p_report_date between effective_start_date and effective_end_date;
1686 
1687            hr_utility.set_location('l_user_row_id_cat'||l_user_row_id_cat, 10);
1688            hr_utility.set_location('g_cat_flex'||g_cat_flex, 10);
1689            hr_utility.set_location('g_cat_segment'||g_cat_segment, 10);
1690 
1691    end if;
1692 
1693    select user_row_id
1694    into   l_user_row_id_lev
1695    from   pay_user_rows_f
1696    where  user_table_id = l_user_table_id
1697    and    row_low_range_or_name = 'Occupational Levels'
1698    and    p_report_date between effective_start_date and effective_end_date;
1699 
1700    select user_row_id
1701    into   l_user_row_id_func
1702    from   pay_user_rows_f
1703    where  user_table_id = l_user_table_id
1704    and    row_low_range_or_name = 'Function Type'
1705    and    p_report_date between effective_start_date and effective_end_date;
1706 
1707 
1708    select value
1709    into   g_lev_flex
1710    from   pay_user_column_instances_f
1711    where  user_row_id    = l_user_row_id_lev
1712    and    user_column_id = l_user_column_id_flex
1713    and    business_group_id = p_business_group_id
1714    and    p_report_date between effective_start_date and effective_end_date;
1715 
1716    select value
1717    into   g_Func_flex
1718    from   pay_user_column_instances_f
1719    where  user_row_id    = l_user_row_id_func
1720    and    user_column_id = l_user_column_id_flex
1721    and    business_group_id = p_business_group_id
1722    and    p_report_date between effective_start_date and effective_end_date;
1723 
1724    select value
1725    into   g_lev_segment
1726    from   pay_user_column_instances_f
1727    where  user_row_id    = l_user_row_id_lev
1728    and    user_column_id = l_user_column_id_seg
1729    and    business_group_id = p_business_group_id
1730    and    p_report_date between effective_start_date and effective_end_date;
1731 
1732 
1733    select value
1734    into   g_Func_segment
1735    from   pay_user_column_instances_f
1736    where  user_row_id    = l_user_row_id_func
1737    and    user_column_id = l_user_column_id_seg
1738    and    business_group_id = p_business_group_id
1739    and    p_report_date between effective_start_date and effective_end_date;
1740 
1741    hr_utility.set_location('l_user_table_id'||l_user_table_id, 10);
1742    hr_utility.set_location('l_user_column_id_flex'||l_user_column_id_flex, 10);
1743    hr_utility.set_location('l_user_column_id_seg'||l_user_column_id_seg, 10);
1744    hr_utility.set_location('l_user_row_id_lev'||l_user_row_id_lev, 10);
1745    hr_utility.set_location('l_user_row_id_func'||l_user_row_id_func, 10);
1746    hr_utility.set_location('g_lev_flex'||g_lev_flex, 10);
1747    hr_utility.set_location('g_Func_flex'||g_Func_flex, 10);
1748    hr_utility.set_location('g_lev_segment'||g_lev_segment, 10);
1749    hr_utility.set_location('g_Func_segment'||g_Func_segment, 10);
1750    -- Verify the validity of the segments
1751    begin
1752 
1753       l_temp := substr(g_lev_segment, 8);
1754       if substr(g_lev_segment, 1, 7) <> 'SEGMENT' or to_number(l_temp) < 1 or to_number(l_temp) > 30 then
1755          raise_application_error(-20003, 'The Occupational data in the User Table ZA_OCCUPATIONAL_TYPES refers to an invalid Segment.');
1756       end if;
1757 
1758       --Added if condition for Bug 9462039 as Occupational Categories not required from 2009 reporting year
1759       if p_year < 2009 then
1760           l_temp := substr(g_cat_segment, 8);
1761           if substr(g_cat_segment, 1, 7) <> 'SEGMENT' or to_number(l_temp) < 1 or to_number(l_temp) > 30 then
1762              raise_application_error(-20003, 'The Occupational data in the User Table ZA_OCCUPATIONAL_TYPES refers to an invalid Segment.');
1763           end if;
1764        end if;
1765 
1766    exception
1767       when invalid_number then
1768          raise_application_error(-20003, 'The Occupational data in the User Table ZA_OCCUPATIONAL_TYPES refers to an invalid Segment.');
1769 
1770    end;
1771 
1772 exception
1773    when no_data_found then
1774       raise_application_error(-20001, 'The Occupational data does not exist in the User Table ZA_OCCUPATIONAL_TYPES.');
1775 
1776 end cache_occupational_location;
1777 
1778 -- This function returns the lookup_code from the user tables.
1779 function get_lookup_code
1780 (
1781    p_meaning in hr_lookups.meaning%type
1782 )  return varchar2 is
1783 
1784 l_lookup_code hr_lookups.lookup_code%type;
1785 
1786 begin
1787 
1788    select distinct hl.lookup_code
1789    into   l_lookup_code
1790    from   hr_lookups hl
1791    where  hl.lookup_type in ('ZA_EMP_EQ_OCCUPATIONAL_CAT', 'ZA_EMP_EQ_OCCUPATIONAL_LEV')
1792    and    hl.meaning = p_meaning;
1793 
1794    return l_lookup_code;
1795 
1796 end get_lookup_code;
1797 
1798 -- This function populates an entity's sex and race and category matches.
1799 procedure populate_ee_table
1800 (
1801    p_report_code       in varchar2,
1802    p_report_date       in per_all_assignments_f.effective_end_date%type,
1803    p_business_group_id in per_all_assignments_f.business_group_id%type,
1804    p_legal_entity_id   in per_assignment_extra_info.aei_information7%type := null
1805 )  is
1806 
1807 l_counter number;
1808 l_reason  varchar2(200);
1809 
1810 begin
1811 
1812    -- Note EQ1 is for the following 2 reports:
1813    --    2. Occupational Categories (including employees with disabilities)
1814    --    3. Occupational Categories (only employees with disabilities)
1815    if p_report_code = 'EQ1' then
1816 
1817       -- Note: The date effective select on per_all_assignments_f is ok in this case, since an assignment
1818       --       record always exist at the same time as an employee record with status EMP
1819       insert into per_za_employment_equity
1820       (
1821          report_id,
1822          reporting_date,
1823          business_group_id,
1824          legal_entity_id,
1825          legal_entity,
1826          disability,
1827          employment_type,
1828          level_cat_code,
1829          level_cat,
1830          ma,
1831          mc,
1832          mi,
1833          mw,
1834          fa,
1835          fc,
1836          fi,
1837          fw,
1838          total
1839       )
1840       select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
1841                     decode(papf.PER_INFORMATION3,null,null,
1842                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
1843                                -to_char(p_report_date,'YYYYMMDD'))
1844                            ,-1,null,'F'))
1845                     )      report_code,
1846              p_report_date                                                              reporting_date,
1847              paaf.business_group_id,
1848              paei.aei_information7                                                      legal_entity_id,
1849              haou.name                                                                  legal_entity,
1850              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)    disability, --3962073
1851              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
1852              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id) employment_type,  -- Bug 3962073
1853              hl.lookup_code                                                             meaning_code,
1854              nvl(per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Category') occupational_category,
1855              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
1856              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
1857              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
1858              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
1859              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
1860              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
1861              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
1862              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
1863              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
1864              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
1865              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
1866              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
1867              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
1868              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
1869              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
1870              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
1871       from   hr_lookups                hl,
1872              hr_lookups                hl1,
1873              hr_lookups                hl2,
1874              hr_all_organization_units haou,
1875              per_assignment_extra_info paei,
1876              per_all_assignments_f     paaf,
1877              per_all_people_f          papf
1878       where  papf.business_group_id = p_business_group_id
1879       and    p_report_date between papf.effective_start_date and papf.effective_end_date
1880       and    papf.current_employee_flag = 'Y'
1881       and    paaf.person_id = papf.person_id
1882       and    paaf.primary_flag = 'Y'
1883       and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
1884       and    paei.assignment_id = paaf.assignment_id
1885       and    paei.information_type = 'ZA_SPECIFIC_INFO'
1886       and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
1887       and    paei.aei_information7 is not null
1888       and    nvl(paei.aei_information6, 'N') <> 'Y'
1889       and    haou.organization_id = paei.aei_information7
1890       and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
1891       and    hl.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
1892       AND    hl.lookup_code <> '15' -- Not Applicable.
1893       and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
1894       AND    hl1.lookup_code <> '15' -- Not Applicable.
1895       and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
1896       and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
1897       AND    hl2.lookup_code <> '15' -- Not Applicable.
1898       and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
1899       group  by paaf.business_group_id,
1900              paei.aei_information7,
1901              haou.name,
1902              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
1903              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)), -- Bug 3962073
1904              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id),  -- Bug 3962073
1905              hl.lookup_code,
1906              nvl(per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Category'),
1907              p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
1908                               decode(papf.PER_INFORMATION3,null,null,
1909                               decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
1910                                -to_char(p_report_date,'YYYYMMDD'))
1911                            ,-1,null,'F'))
1912                     );
1913 
1914       commit;
1915 
1916       -- Inserts non-associated occupational categories with zero values
1917       insert into per_za_employment_equity
1918       (
1919          report_id,
1920          reporting_date,
1921          business_group_id,
1922          legal_entity_id,
1923          legal_entity,
1924          disability,
1925          employment_type,
1926          level_cat_code,
1927          level_cat,
1928          MA,
1929          MC,
1930          MI,
1931          MW,
1932          FA,
1933          FC,
1934          FI,
1935          FW,
1936          total
1937       )
1938       select 'EQ1'            report_id,
1939              p_report_date    reporting_date,
1940              p_business_group_id business_group_id,
1941              haou.organization_id legal_entity_id,
1942              haou.name        legal_entity,
1943              'Y'              disability,
1944              'Permanent'      employment_type,
1945              hl.lookup_code   level_cat_code,
1946              hl.meaning       level_cat,
1947              0                MA,
1948              0                MC,
1949              0                MI,
1950              0                MW,
1951              0                FA,
1952              0                FC,
1953              0                FI,
1954              0                FW,
1955              0                total
1956       from   hr_lookups hl
1957          ,   hr_all_organization_units haou
1958       where not exists
1959       (
1960          select 'X'
1961          from   per_za_employment_equity pzee
1962          where  pzee.level_cat_code    = hl.lookup_code
1963          and    pzee.report_id         = 'EQ1'
1964          and    pzee.business_group_id = p_business_group_id           --Bug 4872110
1965          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
1966          and    pzee.disability        = 'Y'
1967          and    pzee.employment_type   = 'Permanent'
1968       )
1969       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
1970       and haou.business_group_id = p_business_group_id          --Bug 4872110
1971       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
1972 
1973 -- Inseting 0 VALUES FOR FOREIGN nationals
1974       insert into per_za_employment_equity
1975       (
1976          report_id,
1977          reporting_date,
1978          business_group_id,
1979          legal_entity_id,
1980          legal_entity,
1981          disability,
1982          employment_type,
1983          level_cat_code,
1984          level_cat,
1985          MA,
1986          MC,
1987          MI,
1988          MW,
1989          FA,
1990          FC,
1991          FI,
1992          FW,
1993          total
1994       )
1995       select 'EQ1F'            report_id,
1996              p_report_date    reporting_date,
1997              p_business_group_id business_group_id,
1998              haou.organization_id legal_entity_id,
1999              haou.name        legal_entity,
2000              'Y'              disability,
2001              'Permanent'      employment_type,
2002              hl.lookup_code   level_cat_code,
2003              hl.meaning       level_cat,
2004              0                MA,
2005              0                MC,
2006              0                MI,
2007              0                MW,
2008              0                FA,
2009              0                FC,
2010              0                FI,
2011              0                FW,
2012              0                total
2013       from   hr_lookups hl
2014          ,   hr_all_organization_units haou
2015       where not exists
2016       (
2017          select 'X'
2018          from   per_za_employment_equity pzee
2019          where  pzee.level_cat_code    = hl.lookup_code
2020          and    pzee.report_id         = 'EQ1F'
2021          and    pzee.business_group_id = p_business_group_id           --Bug 4872110
2022          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2023          and    pzee.disability        = 'Y'
2024          and    pzee.employment_type   = 'Permanent'
2025       )
2026       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
2027       and haou.business_group_id = p_business_group_id          --Bug 4872110
2028       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2029 
2030       commit;
2031 
2032    -- Note EQ2 is for the following 2 reports:
2033    --    4. Occupational Levels (including employees with disabilities)
2034    --    5. Occupational Levels (only employees with disabilities)
2035    elsif p_report_code = 'EQ2' then
2036 
2037       -- Populate with Occupational Level Totals
2038       insert into per_za_employment_equity
2039       (
2040          report_id,
2041          reporting_date,
2042          business_group_id,
2043          legal_entity_id,
2044          legal_entity,
2045          disability,
2046          employment_type,
2047          level_cat_code,
2048          level_cat,
2049          MA,
2050          MC,
2051          MI,
2052          MW,
2053          FA,
2054          FC,
2055          FI,
2056          FW,
2057          total
2058       )
2059       select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2060                     decode(papf.PER_INFORMATION3,null,null,
2061                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2062                                -to_char(p_report_date,'YYYYMMDD'))
2063                            ,-1,null,'F'))
2064              ) report_code,
2065              p_report_date                                                              reporting_date,
2066              paaf.business_group_id,
2067              paei.aei_information7                                                      legal_entity_id,
2068              haou.name                                                                  legal_entity,
2069              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)      disability, --3962073
2070              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
2071              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id) employment_type,  -- Bug 3962073
2072              hl.lookup_code                                                             meaning_code,
2073              nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level') occupational_level,
2074              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
2075              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
2076              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
2077              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
2078              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
2079              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
2080              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
2081              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
2082              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2083              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
2084              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
2085              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
2086              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2087              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
2088              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
2089              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
2090       from   hr_lookups                hl,
2091              hr_lookups                hl1,
2092              hr_lookups                hl2,
2093              hr_all_organization_units haou,
2094              per_assignment_extra_info paei,
2095              per_all_assignments_f     paaf,
2096              per_all_people_f          papf
2097       where  papf.business_group_id = p_business_group_id
2098       and    p_report_date between papf.effective_start_date and papf.effective_end_date
2099       and    papf.current_employee_flag = 'Y'
2100       and    paaf.person_id = papf.person_id
2101       and    paaf.primary_flag = 'Y'
2102       and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
2103       and    paei.assignment_id = paaf.assignment_id
2104       and    paei.information_type = 'ZA_SPECIFIC_INFO'
2105       and    paei.aei_information7 = nvl(p_legal_entity_id,paei.aei_information7)
2106       and    paei.aei_information7 is not null
2107       and    nvl(paei.aei_information6, 'N') <> 'Y'
2108       and    haou.organization_id = paei.aei_information7
2109       and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2110       and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2111       AND    hl.lookup_code <> '15' -- Not Applicable.
2112       and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
2113       AND    hl1.lookup_code <> '15' -- Operation / core function
2114       and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2115       and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
2116       and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2117       AND    hl2.lookup_code <> '15' -- Not Applicable.
2118       group  by paaf.business_group_id,
2119              paei.aei_information7,
2120              haou.name,
2121              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
2122              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)), -- Bug 3962073
2123              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id), -- Bug 3962073
2124              hl.lookup_code,
2125              nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level'),
2126              p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2127                     decode(papf.PER_INFORMATION3,null,null,
2128                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2129                                -to_char(p_report_date,'YYYYMMDD'))
2130                            ,-1,null,'F')));
2131 
2132       commit;
2133 
2134       -- Inserts non-associated occupational levels with zero values
2135       insert into per_za_employment_equity
2136       (
2137          report_id,
2138          reporting_date,
2139          business_group_id,
2140          legal_entity_id,
2141          legal_entity,
2142          disability,
2143          employment_type,
2144          level_cat_code,
2145          level_cat,
2146          MA,
2147          MC,
2148          MI,
2149          MW,
2150          FA,
2151          FC,
2152          FI,
2153          FW,
2154          total
2155       )
2156       select 'EQ2'            report_id,
2157              p_report_date    reporting_date,
2158              p_business_group_id business_group_id,
2159              haou.organization_id legal_entity_id,
2160              haou.name        legal_entity,
2161              'Y'              disability,
2162              'Permanent'      employment_type,
2163              hl.lookup_code   level_cat_code,
2164              hl.meaning       level_cat,
2165              0                MA,
2166              0                MC,
2167              0                MI,
2168              0                MW,
2169              0                FA,
2170              0                FC,
2171              0                FI,
2172              0                FW,
2173              0                total
2174       from   hr_lookups hl
2175          ,   hr_all_organization_units haou
2176       where not exists
2177       (
2178          select 'X'
2179          from   per_za_employment_equity pzee
2180          where  pzee.level_cat_code    = hl.lookup_code
2181          and    pzee.report_id         = 'EQ2'
2182          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2183          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2184          and    pzee.disability        = 'Y'
2185          and    pzee.employment_type   = 'Permanent'
2186       )
2187       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2188       and haou.business_group_id = p_business_group_id          --Bug 4872110
2189       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2190 
2191 
2192 -- inserting 0 values for the Foreign Nationals
2193       insert into per_za_employment_equity
2194       (
2195          report_id,
2196          reporting_date,
2197          business_group_id,
2198          legal_entity_id,
2199          legal_entity,
2200          disability,
2201          employment_type,
2202          level_cat_code,
2203          level_cat,
2204          MA,
2205          MC,
2206          MI,
2207          MW,
2208          FA,
2209          FC,
2210          FI,
2211          FW,
2212          total
2213       )
2214       select 'EQ2F'            report_id,
2215              p_report_date    reporting_date,
2216              p_business_group_id business_group_id,
2217              haou.organization_id legal_entity_id,
2218              haou.name        legal_entity,
2219              'Y'              disability,
2220              'Permanent'      employment_type,
2221              hl.lookup_code   level_cat_code,
2222              hl.meaning       level_cat,
2223              0                MA,
2224              0                MC,
2225              0                MI,
2226              0                MW,
2227              0                FA,
2228              0                FC,
2229              0                FI,
2230              0                FW,
2231              0                total
2232       from   hr_lookups hl
2233          ,   hr_all_organization_units haou
2234       where not exists
2235       (
2236          select 'X'
2237          from   per_za_employment_equity pzee
2238          where  pzee.level_cat_code    = hl.lookup_code
2239          and    pzee.report_id         = 'EQ2F'
2240          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2241          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2242          and    pzee.disability        = 'Y'
2243          and    pzee.employment_type   = 'Permanent'
2244       )
2245       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2246       and haou.business_group_id = p_business_group_id          --Bug 4872110
2247       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2248 
2249 
2250       commit;
2251 
2252 -- For employment equity enhancement
2253    elsif p_report_code = 'EQ3' then
2254 
2255       -- Populate with New Hires
2256       insert into per_za_employment_equity
2257       (
2258          report_id,
2259          reporting_date,
2260          business_group_id,
2261          legal_entity_id,
2262          legal_entity,
2263          disability,
2264          employment_type,
2265          level_cat_code,
2266          level_cat,
2267          MA,
2268          MC,
2269          MI,
2270          MW,
2271          FA,
2272          FC,
2273          FI,
2274          FW,
2275          total
2276       )
2277       select tpa.report_code,
2278              tpa.reporting_date,
2279              tpa.business_group_id,
2280              tpa.legal_entity_id,
2281              tpa.legal_entity,
2282              tpa.disability,
2283              tpa.employment_type,
2284              tpa.meaning_code,
2285              tpa.occupational_level,
2286              sum(tpa.male_african)    MA,
2287              sum(tpa.male_coloured)   MC,
2288              sum(tpa.male_indian)     MI,
2289              sum(tpa.male_white)      MW,
2290              sum(tpa.female_african)  FA,
2291              sum(tpa.female_coloured) FC,
2292              sum(tpa.female_indian)   FI,
2293              sum(tpa.female_white)    FW,
2294              sum(tpa.total)           total
2295       from
2296       (
2297          select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2298                     decode(papf.PER_INFORMATION3,null,null,
2299                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2300                                -to_char(p_report_date,'YYYYMMDD'))
2301                            ,-1,null,'F')))     report_code,
2302                 p_report_date                                                                                                                                       reporting_date,
2303                 paaf.business_group_id,
2304                 paei.aei_information7                                                                                                                               legal_entity_id,
2305                 haou.name                                                                                                                                           legal_entity,
2306                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability, -- 3962073
2307                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
2308                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id)                                                         employment_type, -- Bug 3962073
2309                 hl.lookup_code                                                                                                                                      meaning_code,
2310                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level') occupational_level,
2311                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
2312                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
2313                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
2314                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
2315                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
2316                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
2317                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
2318                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
2319                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2320                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
2321                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
2322                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
2323                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2324                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
2325                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
2326                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
2327          from   hr_lookups                hl,
2328                 hr_lookups                hl1,
2329                 hr_lookups                hl2,
2330                 hr_all_organization_units haou,
2331                 per_assignment_extra_info paei,
2332                 per_all_assignments_f     paaf,
2333                 per_all_people_f          papf
2334          where  papf.business_group_id = p_business_group_id
2335          and    papf.current_employee_flag = 'Y'
2336          and    p_report_date between papf.effective_start_date and papf.effective_end_date
2337          and    paaf.person_id = papf.person_id
2338          and    paaf.primary_flag = 'Y'
2339          and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
2340          and    paei.assignment_id = paaf.assignment_id
2341          and    paei.information_type = 'ZA_SPECIFIC_INFO'
2342          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
2343          and    paei.aei_information7 is not null
2344          and    nvl(paei.aei_information6, 'N') <> 'Y'
2345          and    haou.organization_id = paei.aei_information7
2346          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2347          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2348          AND    hl.lookup_code <> '15' -- Not Applicable.
2349          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
2350          AND    hl1.lookup_code = '1' -- Operation / core function
2351          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2352          and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
2353          and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2354          AND    hl2.lookup_code <> '15' -- Not Applicable.
2355          group  by paaf.business_group_id,
2356                 paei.aei_information7,
2357                 haou.name,
2358                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
2359                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)),
2360                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id),
2361                 hl.lookup_code,
2362                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level'),
2363                 p_report_code ||  decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2364                     decode(papf.PER_INFORMATION3,null,null,
2365                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2366                                -to_char(p_report_date,'YYYYMMDD'))
2367                            ,-1,null,'F')))
2368       ) tpa
2369       group  by tpa.report_code,
2370              tpa.reporting_date,
2371              tpa.business_group_id,
2372              tpa.legal_entity_id,
2373              tpa.legal_entity,
2374              tpa.disability,
2375              tpa.employment_type,
2376              tpa.meaning_code,
2377              tpa.occupational_level;
2378 
2379       commit;
2380 
2381       -- Inserts non-associated occupational levels with zero values
2382       insert into per_za_employment_equity
2383       (
2384          report_id,
2385          reporting_date,
2386          business_group_id,
2387          legal_entity_id,
2388          legal_entity,
2389          disability,
2390          employment_type,
2391          level_cat_code,
2392          level_cat,
2393          MA,
2394          MC,
2395          MI,
2396          MW,
2397          FA,
2398          FC,
2399          FI,
2400          FW,
2401          total
2402       )
2403       select 'EQ3'            report_id,
2404              p_report_date    reporting_date,
2405              p_business_group_id business_group_id,
2406              haou.organization_id legal_entity_id,
2407              haou.name        legal_entity,
2408              'Y'              disability,
2409              'Permanent'      employment_type,
2410              hl.lookup_code   level_cat_code,
2411              hl.meaning       level_cat,
2412              0                MA,
2413              0                MC,
2414              0                MI,
2415              0                MW,
2416              0                FA,
2417              0                FC,
2418              0                FI,
2419              0                FW,
2420              0                total
2421       from   hr_lookups hl
2422          ,   hr_all_organization_units haou
2423       where not exists
2424       (
2425          select 'X'
2426          from   per_za_employment_equity pzee
2427          where  pzee.level_cat_code    = hl.lookup_code
2428          and    pzee.report_id         = 'EQ3'
2429          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2430          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2431          and    pzee.disability        = 'Y'
2432          and    pzee.employment_type   = 'Permanent'
2433       )
2434       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2435       and haou.business_group_id = p_business_group_id          --Bug 4872110
2436       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2437 
2438       -- Inserts non-associated occupational levels with zero values
2439       insert into per_za_employment_equity
2440       (
2441          report_id,
2442          reporting_date,
2443          business_group_id,
2444          legal_entity_id,
2445          legal_entity,
2446          disability,
2447          employment_type,
2448          level_cat_code,
2449          level_cat,
2450          MA,
2451          MC,
2452          MI,
2453          MW,
2454          FA,
2455          FC,
2456          FI,
2457          FW,
2458          total
2459       )
2460       select 'EQ3F'            report_id,
2461              p_report_date    reporting_date,
2462              p_business_group_id business_group_id,
2463              haou.organization_id legal_entity_id,
2464              haou.name        legal_entity,
2465              'Y'              disability,
2466              'Permanent'      employment_type,
2467              hl.lookup_code   level_cat_code,
2468              hl.meaning       level_cat,
2469              0                MA,
2470              0                MC,
2471              0                MI,
2472              0                MW,
2473              0                FA,
2474              0                FC,
2475              0                FI,
2476              0                FW,
2477              0                total
2478       from   hr_lookups hl
2479          ,   hr_all_organization_units haou
2480       where not exists
2481       (
2482          select 'X'
2483          from   per_za_employment_equity pzee
2484          where  pzee.level_cat_code    = hl.lookup_code
2485          and    pzee.report_id         = 'EQ3F'
2486          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2487          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2488          and    pzee.disability        = 'Y'
2489          and    pzee.employment_type   = 'Permanent'
2490       )
2491       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2492       and haou.business_group_id = p_business_group_id          --Bug 4872110
2493       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2494 
2495       commit;
2496 
2497    -- Note EQ4 is for the following report:
2498    --    2.3.1. Operational / core Functiona (report the total number of new recruits into each occupational level during
2499    --       the twelve months preceding this report)
2500    elsif p_report_code = 'EQ4' then
2501 
2502       -- Populate with New Hires
2503       insert into per_za_employment_equity
2504       (
2505          report_id,
2506          reporting_date,
2507          business_group_id,
2508          legal_entity_id,
2509          legal_entity,
2510          disability,
2511          employment_type,
2512          level_cat_code,
2513          level_cat,
2514          MA,
2515          MC,
2516          MI,
2517          MW,
2518          FA,
2519          FC,
2520          FI,
2521          FW,
2522          total
2523       )
2524       select tpa.report_code,
2525              tpa.reporting_date,
2526              tpa.business_group_id,
2527              tpa.legal_entity_id,
2528              tpa.legal_entity,
2529              tpa.disability,
2530              tpa.employment_type,
2531              tpa.meaning_code,
2532              tpa.occupational_level,
2533              sum(tpa.male_african)    MA,
2534              sum(tpa.male_coloured)   MC,
2535              sum(tpa.male_indian)     MI,
2536              sum(tpa.male_white)      MW,
2537              sum(tpa.female_african)  FA,
2538              sum(tpa.female_coloured) FC,
2539              sum(tpa.female_indian)   FI,
2540              sum(tpa.female_white)    FW,
2541              sum(tpa.total)           total
2542       from
2543       (
2544          select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2545                     decode(papf.PER_INFORMATION3,null,null,
2546                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2547                                -to_char(p_report_date,'YYYYMMDD'))
2548                            ,-1,null,'F')))     report_code,
2549                 p_report_date                                                                                                                                       reporting_date,
2550                 paaf.business_group_id,
2551                 paei.aei_information7                                                                                                                               legal_entity_id,
2552                 haou.name                                                                                                                                           legal_entity,
2553                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability, -- 3962073
2554                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
2555                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id)                                                         employment_type, -- Bug 3962073
2556                 hl.lookup_code                                                                                                                                      meaning_code,
2557                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level') occupational_level,
2558                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
2559                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
2560                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
2561                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
2562                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
2563                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
2564                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
2565                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
2566                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2567                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
2568                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
2569                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
2570                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2571                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
2572                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
2573                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
2574          from   hr_lookups                hl,
2575                 hr_lookups                hl1,
2576                 hr_lookups                hl2,
2577                 hr_all_organization_units haou,
2578                 per_assignment_extra_info paei,
2579                 per_all_assignments_f     paaf,
2580                 per_all_people_f          papf
2581          where  papf.business_group_id = p_business_group_id
2582          and    papf.current_employee_flag = 'Y'
2583          and    p_report_date between papf.effective_start_date and papf.effective_end_date
2584          and    paaf.person_id = papf.person_id
2585          and    paaf.primary_flag = 'Y'
2586          and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
2587          and    paei.assignment_id = paaf.assignment_id
2588          and    paei.information_type = 'ZA_SPECIFIC_INFO'
2589          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
2590          and    paei.aei_information7 is not null
2591          and    nvl(paei.aei_information6, 'N') <> 'Y'
2592          and    haou.organization_id = paei.aei_information7
2593          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2594          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2595          AND    hl.lookup_code <> '15' -- Not Applicable.
2596          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
2597          AND    hl1.lookup_code = '2' -- Support function
2598          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2599          and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
2600          and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2601          AND    hl2.lookup_code <> '15' -- Not Applicable.
2602          group  by paaf.business_group_id,
2603                 paei.aei_information7,
2604                 haou.name,
2605                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
2606                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)),
2607                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id),
2608                 hl.lookup_code,
2609                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level'),
2610                 p_report_code ||  decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2611                     decode(papf.PER_INFORMATION3,null,null,
2612                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2613                                -to_char(p_report_date,'YYYYMMDD'))
2614                            ,-1,null,'F')))
2615       ) tpa
2616       group  by tpa.report_code,
2617              tpa.reporting_date,
2618              tpa.business_group_id,
2619              tpa.legal_entity_id,
2620              tpa.legal_entity,
2621              tpa.disability,
2622              tpa.employment_type,
2623              tpa.meaning_code,
2624              tpa.occupational_level;
2625 
2626       commit;
2627 
2628       -- Inserts non-associated occupational levels with zero values
2629       insert into per_za_employment_equity
2630       (
2631          report_id,
2632          reporting_date,
2633          business_group_id,
2634          legal_entity_id,
2635          legal_entity,
2636          disability,
2637          employment_type,
2638          level_cat_code,
2639          level_cat,
2640          MA,
2641          MC,
2642          MI,
2643          MW,
2644          FA,
2645          FC,
2646          FI,
2647          FW,
2648          total
2649       )
2650       select 'EQ4'            report_id,
2651              p_report_date    reporting_date,
2652              p_business_group_id business_group_id,
2653              haou.organization_id legal_entity_id,
2654              haou.name        legal_entity,
2655              'Y'              disability,
2656              'Permanent'      employment_type,
2657              hl.lookup_code   level_cat_code,
2658              hl.meaning       level_cat,
2659              0                MA,
2660              0                MC,
2661              0                MI,
2662              0                MW,
2663              0                FA,
2664              0                FC,
2665              0                FI,
2666              0                FW,
2667              0                total
2668       from   hr_lookups hl
2669          ,   hr_all_organization_units haou
2670       where not exists
2671       (
2672          select 'X'
2673          from   per_za_employment_equity pzee
2674          where  pzee.level_cat_code    = hl.lookup_code
2675          and    pzee.report_id         = 'EQ4'
2676          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2677          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2678          and    pzee.disability        = 'Y'
2679          and    pzee.employment_type   = 'Permanent'
2680       )
2681       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2682       and haou.business_group_id = p_business_group_id          --Bug 4872110
2683       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2684 
2685       -- Inserts non-associated occupational levels with zero values
2686       insert into per_za_employment_equity
2687       (
2688          report_id,
2689          reporting_date,
2690          business_group_id,
2691          legal_entity_id,
2692          legal_entity,
2693          disability,
2694          employment_type,
2695          level_cat_code,
2696          level_cat,
2697          MA,
2698          MC,
2699          MI,
2700          MW,
2701          FA,
2702          FC,
2703          FI,
2704          FW,
2705          total
2706       )
2707       select 'EQ4F'            report_id,
2708              p_report_date    reporting_date,
2709              p_business_group_id business_group_id,
2710              haou.organization_id legal_entity_id,
2711              haou.name        legal_entity,
2712              'Y'              disability,
2713              'Permanent'      employment_type,
2714              hl.lookup_code   level_cat_code,
2715              hl.meaning       level_cat,
2716              0                MA,
2717              0                MC,
2718              0                MI,
2719              0                MW,
2720              0                FA,
2721              0                FC,
2722              0                FI,
2723              0                FW,
2724              0                total
2725       from   hr_lookups hl
2726          ,   hr_all_organization_units haou
2727       where not exists
2728       (
2729          select 'X'
2730          from   per_za_employment_equity pzee
2731          where  pzee.level_cat_code    = hl.lookup_code
2732          and    pzee.report_id         = 'EQ4F'
2733          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2734          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2735          and    pzee.disability        = 'Y'
2736          and    pzee.employment_type   = 'Permanent'
2737       )
2738       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2739       and haou.business_group_id = p_business_group_id          --Bug 4872110
2740       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2741 
2742       commit;
2743 
2744 -- End for Emplyment Equity enhancement
2745 
2746 
2747    -- Note EQ5 is for the following report:
2748    --    6. Recruitment (report the total number of new recruits into each occupational level during
2749    --       the twelve months preceding this report)
2750    elsif p_report_code = 'EQ5' then
2751 
2752       -- Populate with New Hires
2753       insert into per_za_employment_equity
2754       (
2755          report_id,
2756          reporting_date,
2757          business_group_id,
2758          legal_entity_id,
2759          legal_entity,
2760          disability,
2761          employment_type,
2762          level_cat_code,
2763          level_cat,
2764          MA,
2765          MC,
2766          MI,
2767          MW,
2768          FA,
2769          FC,
2770          FI,
2771          FW,
2772          total
2773       )
2774       select tpa.report_code,
2775              tpa.reporting_date,
2776              tpa.business_group_id,
2777              tpa.legal_entity_id,
2778              tpa.legal_entity,
2779              tpa.disability,
2780              tpa.employment_type,
2781              tpa.meaning_code,
2782              tpa.occupational_level,
2783              sum(tpa.male_african)    MA,
2784              sum(tpa.male_coloured)   MC,
2785              sum(tpa.male_indian)     MI,
2786              sum(tpa.male_white)      MW,
2787              sum(tpa.female_african)  FA,
2788              sum(tpa.female_coloured) FC,
2789              sum(tpa.female_indian)   FI,
2790              sum(tpa.female_white)    FW,
2791              sum(tpa.total)           total
2792       from
2793       (
2794          select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2795                     decode(papf.PER_INFORMATION3,null,null,
2796                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2797                                -to_char(p_report_date,'YYYYMMDD'))
2798                            ,-1,null,'F')))     report_code,
2799                 p_report_date                                                                                                                                       reporting_date,
2800                 paaf.business_group_id,
2801                 paei.aei_information7                                                                                                                               legal_entity_id,
2802                 haou.name                                                                                                                                           legal_entity,
2803                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability, -- 3962073
2804                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
2805                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id)                                                         employment_type, -- Bug 3962073
2806                 hl.lookup_code                                                                                                                                      meaning_code,
2807                 nvl(per_za_employment_equity_pkg.get_occupational_level(ppos.date_start, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level') occupational_level,
2808                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
2809                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
2810                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
2811                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
2812                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
2813                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
2814                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
2815                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
2816                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2817                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
2818                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
2819                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
2820                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
2821                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
2822                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
2823                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
2824          from   hr_lookups                hl,
2825                 hr_lookups                hl1,
2826                 hr_lookups                hl2,
2827                 hr_all_organization_units haou,
2828                 per_assignment_extra_info paei,
2829                 per_all_assignments_f     paaf,
2830                 per_periods_of_service    ppos,
2831                 per_all_people_f          papf
2832          where  papf.business_group_id = p_business_group_id
2833          and    papf.current_employee_flag = 'Y'
2834          and    ppos.person_id = papf.person_id
2835          and    ppos.date_start between add_months(p_report_date, -12) + 1 and p_report_date
2836          and    papf.effective_start_date = ppos.date_start
2837          and    paaf.person_id = papf.person_id
2838          and    paaf.primary_flag = 'Y'
2839          and    paaf.effective_start_date = ppos.date_start
2840          and    paei.assignment_id = paaf.assignment_id
2841          and    paei.information_type = 'ZA_SPECIFIC_INFO'
2842          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
2843          and    paei.aei_information7 is not null
2844          and    nvl(paei.aei_information6, 'N') <> 'Y'
2845          and    haou.organization_id = paei.aei_information7
2846          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2847          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(ppos.date_start, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2848          AND    hl.lookup_code <> '15' -- Not Applicable.
2849          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
2850          and    hl1.lookup_code <> '15' -- Not Applicable.
2851          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2852          and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
2853          and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
2854          AND    hl2.lookup_code <> '15' -- Not Applicable.
2855          group  by paaf.business_group_id,
2856                 paei.aei_information7,
2857                 haou.name,
2858                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
2859                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)),
2860                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id),
2861                 hl.lookup_code,
2862                 nvl(per_za_employment_equity_pkg.get_occupational_level(ppos.date_start, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level'),
2863                 p_report_code ||  decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
2864                     decode(papf.PER_INFORMATION3,null,null,
2865                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
2866                                -to_char(p_report_date,'YYYYMMDD'))
2867                            ,-1,null,'F')))
2868       ) tpa
2869       group  by tpa.report_code,
2870              tpa.reporting_date,
2871              tpa.business_group_id,
2872              tpa.legal_entity_id,
2873              tpa.legal_entity,
2874              tpa.disability,
2875              tpa.employment_type,
2876              tpa.meaning_code,
2877              tpa.occupational_level;
2878 
2879       commit;
2880 
2881       -- Inserts non-associated occupational levels with zero values
2882       insert into per_za_employment_equity
2883       (
2884          report_id,
2885          reporting_date,
2886          business_group_id,
2887          legal_entity_id,
2888          legal_entity,
2889          disability,
2890          employment_type,
2891          level_cat_code,
2892          level_cat,
2893          MA,
2894          MC,
2895          MI,
2896          MW,
2897          FA,
2898          FC,
2899          FI,
2900          FW,
2901          total
2902       )
2903       select 'EQ5'            report_id,
2904              p_report_date    reporting_date,
2905              p_business_group_id business_group_id,
2906              haou.organization_id legal_entity_id,
2907              haou.name        legal_entity,
2908              'Y'              disability,
2909              'Permanent'      employment_type,
2910              hl.lookup_code   level_cat_code,
2911              hl.meaning       level_cat,
2912              0                MA,
2913              0                MC,
2914              0                MI,
2915              0                MW,
2916              0                FA,
2917              0                FC,
2918              0                FI,
2919              0                FW,
2920              0                total
2921       from   hr_lookups hl
2922          ,   hr_all_organization_units haou
2923       where not exists
2924       (
2925          select 'X'
2926          from   per_za_employment_equity pzee
2927          where  pzee.level_cat_code    = hl.lookup_code
2928          and    pzee.report_id         = 'EQ5'
2929          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2930          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2931          and    pzee.disability        = 'Y'
2932          and    pzee.employment_type   = 'Permanent'
2933       )
2934       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2935       and haou.business_group_id = p_business_group_id          --Bug 4872110
2936       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2937 
2938       -- Inserts non-associated occupational levels with zero values
2939       insert into per_za_employment_equity
2940       (
2941          report_id,
2942          reporting_date,
2943          business_group_id,
2944          legal_entity_id,
2945          legal_entity,
2946          disability,
2947          employment_type,
2948          level_cat_code,
2949          level_cat,
2950          MA,
2951          MC,
2952          MI,
2953          MW,
2954          FA,
2955          FC,
2956          FI,
2957          FW,
2958          total
2959       )
2960       select 'EQ5F'            report_id,
2961              p_report_date    reporting_date,
2962              p_business_group_id business_group_id,
2963              haou.organization_id legal_entity_id,
2964              haou.name        legal_entity,
2965              'Y'              disability,
2966              'Permanent'      employment_type,
2967              hl.lookup_code   level_cat_code,
2968              hl.meaning       level_cat,
2969              0                MA,
2970              0                MC,
2971              0                MI,
2972              0                MW,
2973              0                FA,
2974              0                FC,
2975              0                FI,
2976              0                FW,
2977              0                total
2978       from   hr_lookups hl
2979          ,   hr_all_organization_units haou
2980       where not exists
2981       (
2982          select 'X'
2983          from   per_za_employment_equity pzee
2984          where  pzee.level_cat_code    = hl.lookup_code
2985          and    pzee.report_id         = 'EQ5F'
2986          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
2987          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
2988          and    pzee.disability        = 'Y'
2989          and    pzee.employment_type   = 'Permanent'
2990       )
2991       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
2992       and haou.business_group_id = p_business_group_id          --Bug 4872110
2993       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
2994 
2995       commit;
2996 
2997    -- Note EQ6 is for the following report:
2998    --    7. Promotion (report the total number of promotions into each occupational level during
2999    --       the twelve months preceding this report)
3000    elsif p_report_code = 'EQ6' then
3001 
3002       -- Populate with Promotions
3003       insert into per_za_employment_equity
3004       (
3005          report_id,
3006          reporting_date,
3007          business_group_id,
3008          legal_entity_id,
3009          legal_entity,
3010          disability,
3011          employment_type,
3012          level_cat_code,
3013          level_cat,
3014          MA,
3015          MC,
3016          MI,
3017          MW,
3018          FA,
3019          FC,
3020          FI,
3021          FW,
3022          total
3023       )
3024       select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
3025                                 decode(papf.PER_INFORMATION3,null,null,
3026                                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
3027                                        - to_char(p_report_date,'YYYYMMDD'))
3028                                     ,-1,null,'F')))     report_code,
3029              p_report_date                                                              reporting_date,
3030              paaf.business_group_id,
3031              paei.aei_information7                                                      legal_entity_id,
3032              haou.name                                                                  legal_entity,
3033              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)     disability, --3962073
3034              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
3035              -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id) employment_type,
3036              hl.lookup_code                                                             lookup_code,
3037              nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level') occupational_level,
3038              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
3039              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
3040              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
3041              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
3042              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
3043              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
3044              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
3045              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
3046              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
3047              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
3048              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
3049              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
3050              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
3051              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
3052              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
3053              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
3054       from   hr_lookups                hl,
3055              hr_lookups                hl1,
3056              hr_lookups                hl2,
3057              hr_all_organization_units haou,
3058              per_assignment_extra_info paei,
3059              per_all_assignments_f     paaf,
3060              per_periods_of_service    ppos,
3061              per_all_people_f          papf
3062       where  papf.business_group_id = p_business_group_id
3063       and    papf.current_employee_flag = 'Y'
3064       and    ppos.person_id = papf.person_id
3065       and    nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY')) > add_months(p_report_date, -12) + 1
3066       and    ppos.date_start < p_report_date
3067       and    papf.effective_start_date = ppos.date_start
3068       and    paaf.person_id = papf.person_id
3069       and    paaf.primary_flag = 'Y'
3070       and    paaf.effective_start_date between ppos.date_start and nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
3071       and    paaf.effective_start_date > add_months(p_report_date, -12) + 1
3072       and    paaf.effective_start_date <= p_report_date
3073       and    paei.assignment_id = paaf.assignment_id
3074       and    paei.information_type = 'ZA_SPECIFIC_INFO'
3075       and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
3076       and    paei.aei_information7 is not null
3077       and    nvl(paei.aei_information6, 'N') <> 'Y'
3078       and    haou.organization_id = paei.aei_information7
3079       and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
3080       and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3081       AND    hl.lookup_code <> '15' -- Not Applicable.
3082       and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
3083       and    hl1.lookup_code <> '15' -- Not Applicable.
3084       and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3085       and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
3086       and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3087       AND    hl2.lookup_code <> '15' -- Not Applicable.
3088       and    nvl(per_za_employment_equity_pkg.get_lookup_code(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)), '9999999999') <
3089       any
3090       (
3091          select per_za_employment_equity_pkg.get_lookup_code(per_za_employment_equity_pkg.get_occupational_level(paaf1.effective_start_date, paaf1.assignment_id, paaf1.job_id, paaf1.grade_id, paaf1.position_id, paaf.business_group_id)) lookup_code
3092          from   per_all_assignments_f paaf1
3093          where  paaf1.person_id = papf.person_id
3094          and    paaf1.primary_flag = 'Y'
3095          and    per_za_employment_equity_pkg.get_lookup_code(per_za_employment_equity_pkg.get_occupational_level(paaf1.effective_start_date, paaf1.assignment_id, paaf1.job_id, paaf1.grade_id, paaf1.position_id, paaf.business_group_id)) is not null
3096          and    paaf1.effective_end_date + 1 = paaf.effective_start_date
3097       )
3098       group  by paaf.business_group_id,
3099              paei.aei_information7,
3100              haou.name,
3101              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
3102              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id)),
3103              -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id),
3104              hl.lookup_code,
3105              nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level'),
3106              p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
3107                     decode(papf.PER_INFORMATION3,null,null,
3108                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
3109                                -to_char(p_report_date,'YYYYMMDD'))
3110                            ,-1,null,'F'))) ;
3111 
3112       commit;
3113 
3114       -- Inserts non-associated occupational levels with zero values
3115       insert into per_za_employment_equity
3116       (
3117          report_id,
3118          reporting_date,
3119          business_group_id,
3120          legal_entity_id,
3121          legal_entity,
3122          disability,
3123          employment_type,
3124          level_cat_code,
3125          level_cat,
3126          MA,
3127          MC,
3128          MI,
3129          MW,
3130          FA,
3131          FC,
3132          FI,
3133          FW,
3134          total
3135       )
3136       select 'EQ6'            report_id,
3137              p_report_date    reporting_date,
3138              p_business_group_id business_group_id,
3139              haou.organization_id legal_entity_id,
3140              haou.name        legal_entity,
3141              'Y'              disability,
3142              'Permanent'      employment_type,
3143              hl.lookup_code   level_cat_code,
3144              hl.meaning       level_cat,
3145              0                MA,
3146              0                MC,
3147              0                MI,
3148              0                MW,
3149              0                FA,
3150              0                FC,
3151              0                FI,
3152              0                FW,
3153              0                total
3154       from   hr_lookups hl
3155          ,   hr_all_organization_units haou
3156       where not exists
3157       (
3158          select 'X'
3159          from   per_za_employment_equity pzee
3160          where  pzee.level_cat_code    = hl.lookup_code
3161          and    pzee.report_id         = 'EQ6'
3162          and    pzee.business_group_id = p_business_group_id   --Bug 4872110
3163          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
3164          and    pzee.disability        = 'Y'
3165          and    pzee.employment_type   = 'Permanent'
3166       )
3167       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
3168       and haou.business_group_id = p_business_group_id          --Bug 4872110
3169       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
3170 
3171       -- Inserts non-associated occupational levels with zero values
3172       insert into per_za_employment_equity
3173       (
3174          report_id,
3175          reporting_date,
3176          business_group_id,
3177          legal_entity_id,
3178          legal_entity,
3179          disability,
3180          employment_type,
3181          level_cat_code,
3182          level_cat,
3183          MA,
3184          MC,
3185          MI,
3186          MW,
3187          FA,
3188          FC,
3189          FI,
3190          FW,
3191          total
3192       )
3193       select 'EQ6F'            report_id,
3194              p_report_date    reporting_date,
3195              p_business_group_id business_group_id,
3196              haou.organization_id legal_entity_id,
3197              haou.name        legal_entity,
3198              'Y'              disability,
3199              'Permanent'      employment_type,
3200              hl.lookup_code   level_cat_code,
3201              hl.meaning       level_cat,
3202              0                MA,
3203              0                MC,
3204              0                MI,
3205              0                MW,
3206              0                FA,
3207              0                FC,
3208              0                FI,
3209              0                FW,
3210              0                total
3211       from   hr_lookups hl
3212          ,   hr_all_organization_units haou
3213       where not exists
3214       (
3215          select 'X'
3216          from   per_za_employment_equity pzee
3217          where  pzee.level_cat_code    = hl.lookup_code
3218          and    pzee.report_id         = 'EQ6F'
3219          and    pzee.business_group_id = p_business_group_id   --Bug 4872110
3220          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
3221          and    pzee.disability        = 'Y'
3222          and    pzee.employment_type   = 'Permanent'
3223       )
3224       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
3225       and haou.business_group_id = p_business_group_id          --Bug 4872110
3226       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
3227 
3228 
3229       commit;
3230 
3231    -- Note EQ5 is for the following report:
3232    --    8.1 Termination (report the total number of terminations in each occupational level during
3233    --        the twelve months preceding this report)
3234    elsif p_report_code = 'EQ7' then
3235 
3236       -- Populate with Terminations
3237       insert into per_za_employment_equity
3238       (
3239          report_id,
3240          reporting_date,
3241          business_group_id,
3242          legal_entity_id,
3243          legal_entity,
3244          disability,
3245          employment_type,
3246          level_cat_code,
3247          level_cat,
3248          MA,
3249          MC,
3250          MI,
3251          MW,
3252          FA,
3253          FC,
3254          FI,
3255          FW,
3256          total
3257       )
3258       select tpa.report_code,
3259              tpa.reporting_date,
3260              tpa.business_group_id,
3261              tpa.legal_entity_id,
3262              tpa.legal_entity,
3263              tpa.disability,
3264              tpa.employment_type,
3265              tpa.meaning_code,
3266              tpa.occupational_level,
3267              sum(tpa.male_african)    MA,
3268              sum(tpa.male_coloured)   MC,
3269              sum(tpa.male_indian)     MI,
3270              sum(tpa.male_white)      MW,
3271              sum(tpa.female_african)  FA,
3272              sum(tpa.female_coloured) FC,
3273              sum(tpa.female_indian)   FI,
3274              sum(tpa.female_white)    FW,
3275              sum(tpa.total)           total
3276       from
3277       (
3278          select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
3279                     decode(papf.PER_INFORMATION3,null,null,
3280                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
3281                                -to_char(p_report_date,'YYYYMMDD'))
3282                            ,-1,null,'F'))
3283                     )       report_code,
3284                 p_report_date                                                              reporting_date,
3285                 paaf.business_group_id,
3286                 paei.aei_information7                                                      legal_entity_id,
3287                 haou.name                                                                  legal_entity,
3288                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability,  --3962073
3289                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
3290                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id) employment_type, -- Bug 3962073
3291                 hl.lookup_code                                                             meaning_code,
3292                 nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_end_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level') occupational_level,
3293                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
3294                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
3295                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
3296                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
3297                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
3298                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
3299                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
3300                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
3301                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
3302                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
3303                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
3304                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
3305                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
3306                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
3307                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
3308                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
3309          from   hr_lookups                hl,
3310                 hr_lookups                hl1,
3311                 hr_lookups                hl2,
3312                 hr_all_organization_units haou,
3313                 per_assignment_extra_info paei,
3314                 per_all_assignments_f     paaf,
3315                 per_periods_of_service    ppos,
3316                 per_all_people_f          papf
3317          where  papf.business_group_id = p_business_group_id
3318          and    papf.current_employee_flag = 'Y'
3319          and    ppos.person_id = papf.person_id
3320          and    nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY')) between add_months(p_report_date, -12) + 1 and p_report_date
3321          and    papf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
3322          and    paaf.person_id = papf.person_id
3323          and    paaf.primary_flag = 'Y'
3324          and    paaf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
3325          and    paei.assignment_id = paaf.assignment_id
3326          and    paei.information_type = 'ZA_SPECIFIC_INFO'
3327          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
3328          and    paei.aei_information7 is not null
3329          and    nvl(paei.aei_information6, 'N') <> 'Y'
3330          and    haou.organization_id = paei.aei_information7
3331          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
3332          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(paaf.effective_end_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3333          AND    hl.lookup_code <> '15' -- Not Applicable.
3334          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
3335          and    hl1.lookup_code <> '15' -- Not Applicable
3336          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3337          and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
3338          and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3339          AND    hl2.lookup_code <> '15' -- Not Applicable.
3340          group  by paaf.business_group_id,
3341                 paei.aei_information7,
3342                 haou.name,
3343                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
3344                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)),
3345                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id),
3346                 hl.lookup_code,
3347                 nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_end_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Level'),
3348                 p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
3349                     decode(papf.PER_INFORMATION3,null,null,
3350                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
3351                                -to_char(p_report_date,'YYYYMMDD'))
3352                            ,-1,null,'F'))
3353                     )
3354       ) tpa
3355       group  by tpa.report_code,
3356              tpa.reporting_date,
3357              tpa.business_group_id,
3358              tpa.legal_entity_id,
3359              tpa.legal_entity,
3360              tpa.disability,
3361              tpa.employment_type,
3362              tpa.meaning_code,
3363              tpa.occupational_level;
3364 
3365       commit;
3366 
3367       -- Inserts non-associated occupational levels with zero values
3368       insert into per_za_employment_equity
3369       (
3370          report_id,
3371          reporting_date,
3372          business_group_id,
3373          legal_entity_id,
3374          legal_entity,
3375          disability,
3376          employment_type,
3377          level_cat_code,
3378          level_cat,
3379          MA,
3380          MC,
3381          MI,
3382          MW,
3383          FA,
3384          FC,
3385          FI,
3386          FW,
3387          total
3388       )
3389       select 'EQ7'            report_id,
3390              p_report_date    reporting_date,
3391              p_business_group_id business_group_id,
3392              haou.organization_id legal_entity_id,
3393              haou.name        legal_entity,
3394              'Y'              disability,
3395              'Permanent'      employment_type,
3396              hl.lookup_code   level_cat_code,
3397              hl.meaning       level_cat,
3398              0                MA,
3399              0                MC,
3400              0                MI,
3401              0                MW,
3402              0                FA,
3403              0                FC,
3404              0                FI,
3405              0                FW,
3406              0                total
3407       from   hr_lookups hl
3408          ,   hr_all_organization_units haou
3409       where not exists
3410       (
3411          select 'X'
3412          from   per_za_employment_equity pzee
3413          where  pzee.level_cat_code    = hl.lookup_code
3414          and    pzee.report_id         = 'EQ7'
3415          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
3416          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
3417          and    pzee.disability        = 'Y'
3418          and    pzee.employment_type   = 'Permanent'
3419       )
3420       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
3421       and haou.business_group_id = p_business_group_id  --Bug 4872110
3422       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
3423 
3424       insert into per_za_employment_equity
3425       (
3426          report_id,
3427          reporting_date,
3428          business_group_id,
3429          legal_entity_id,
3430          legal_entity,
3431          disability,
3432          employment_type,
3433          level_cat_code,
3434          level_cat,
3435          MA,
3436          MC,
3437          MI,
3438          MW,
3439          FA,
3440          FC,
3441          FI,
3442          FW,
3443          total
3444       )
3445       select 'EQ7F'            report_id,
3446              p_report_date    reporting_date,
3447              p_business_group_id business_group_id,
3448              haou.organization_id legal_entity_id,
3449              haou.name        legal_entity,
3450              'Y'              disability,
3451              'Permanent'      employment_type,
3452              hl.lookup_code   level_cat_code,
3453              hl.meaning       level_cat,
3454              0                MA,
3455              0                MC,
3456              0                MI,
3457              0                MW,
3458              0                FA,
3459              0                FC,
3460              0                FI,
3461              0                FW,
3462              0                total
3463       from   hr_lookups hl
3464          ,   hr_all_organization_units haou
3465       where not exists
3466       (
3467          select 'X'
3468          from   per_za_employment_equity pzee
3469          where  pzee.level_cat_code    = hl.lookup_code
3470          and    pzee.report_id         = 'EQ7F'
3471          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
3472          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
3473          and    pzee.disability        = 'Y'
3474          and    pzee.employment_type   = 'Permanent'
3475       )
3476       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
3477       and haou.business_group_id = p_business_group_id  --Bug 4872110
3478       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
3479 
3480       commit;
3481 
3482    -- Note EQ6 is for the following report:
3483    --    8.2 Termination Categories (report the total number of terminations in each termination
3484    --        category during the twelve months preceding this report)
3485    elsif p_report_code = 'EQ8' then
3486 
3487       -- Populate with Termination Reason totals
3488       insert into per_za_employment_equity
3489       (
3490          report_id,
3491          reporting_date,
3492          business_group_id,
3493          legal_entity_id,
3494          legal_entity,
3495          disability,
3496          employment_type,
3497          level_cat_code,
3498          level_cat,
3499          MA,
3500          MC,
3501          MI,
3502          MW,
3503          FA,
3504          FC,
3505          FI,
3506          FW,
3507          total
3508       )
3509       select tpa.report_code,
3510              tpa.reporting_date,
3511              tpa.business_group_id,
3512              tpa.legal_entity_id,
3513              tpa.legal_entity,
3514              tpa.disability,
3515              tpa.employment_type,
3516              decode
3517              (
3518                 tpa.termination_reason,
3519                 'Resignation', 1,
3520                 'Non-Renewal of Contract', 2,
3521                 'Dismissal - Operational Requirements', 3,
3522                 'Dismissal - Misconduct', 4,
3523                 'Dismissal - Incapacity', 5,
3524                 'Other', 6,
3525                 null
3526              )  meaning_code,
3527              decode
3528              (
3529                 tpa.termination_reason,
3530                 'Dismissal - Operational Requirements', 'Dismissal - Operational Requirements (Retrenchment)',
3531                 tpa.termination_reason
3532              ),
3533              sum(tpa.male_african)    MA,
3534              sum(tpa.male_coloured)   MC,
3535              sum(tpa.male_indian)     MI,
3536              sum(tpa.male_white)      MW,
3537              sum(tpa.female_african)  FA,
3538              sum(tpa.female_coloured) FC,
3539              sum(tpa.female_indian)   FI,
3540              sum(tpa.female_white)    FW,
3541              sum(tpa.total)           total
3542       from
3543       (
3544          select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
3545                     decode(papf.PER_INFORMATION3,null,null,
3546                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
3547                                -to_char(p_report_date,'YYYYMMDD'))
3548                            ,-1,null,'F'))
3549                     )      report_code,
3550                 p_report_date                                                              reporting_date,
3551                 paaf.business_group_id,
3552                 paei.aei_information7                                                      legal_entity_id,
3553                 haou.name                                                                  legal_entity,
3554                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)     disability, --3962073
3555                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
3556                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id) employment_type, -- Bug 3962073
3557                 ppos.leaving_reason                                                        meaning_code,
3558                 nvl(per_za_employment_equity_pkg.get_termination_reason(paaf.business_group_id, p_report_date, ppos.leaving_reason), 'No Leaving Reason') termination_reason,
3559                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
3560                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
3561                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
3562                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
3563                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
3564                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
3565                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
3566                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
3567                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
3568                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
3569                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
3570                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
3571                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
3572                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
3573                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
3574                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
3575          from   hr_lookups                hl,
3576                 hr_lookups                hl1,
3577                 hr_lookups                hl2,
3578                 hr_all_organization_units haou,
3579                 per_assignment_extra_info paei,
3580                 per_all_assignments_f     paaf,
3581                 per_periods_of_service    ppos,
3582                 per_all_people_f          papf
3583          where  papf.business_group_id = p_business_group_id
3584          and    papf.current_employee_flag = 'Y'
3585          and    ppos.person_id = papf.person_id
3586          and    nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY')) between add_months(p_report_date, -12) + 1 and p_report_date
3587          and    papf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
3588          and    paaf.person_id = papf.person_id
3589          and    paaf.primary_flag = 'Y'
3590          and    paaf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
3591          and    paei.assignment_id = paaf.assignment_id
3592          and    paei.information_type = 'ZA_SPECIFIC_INFO'
3593          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
3594          and    paei.aei_information7 is not null
3595          and    nvl(paei.aei_information6, 'N') <> 'Y'
3596          and    haou.organization_id = paei.aei_information7
3597          and    nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)) = 'Permanent'
3598          and    nvl(per_za_employment_equity_pkg.get_termination_reason(paaf.business_group_id, p_report_date, ppos.leaving_reason), 'No Leaving Reason') <> 'No Leaving Reason'
3599          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
3600          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3601          and    hl.lookup_code <> '15' -- Not Applicable
3602          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
3603          and    hl1.lookup_code <> '15' -- Not Applicable
3604          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3605          and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
3606          and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
3607          and    hl2.lookup_code <> '15' -- Not Applicable
3608          group  by paaf.business_group_id,
3609                 paei.aei_information7,
3610                 haou.name,
3611                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
3612                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)),
3613                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id),
3614                 ppos.leaving_reason,
3615                 nvl(per_za_employment_equity_pkg.get_termination_reason(paaf.business_group_id, p_report_date, ppos.leaving_reason), 'No Leaving Reason'),
3616                 p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
3617                     decode(papf.PER_INFORMATION3,null,null,
3618                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION8,1,10),'0001/01/01'),'/','')
3619                                -to_char(p_report_date,'YYYYMMDD'))
3620                            ,-1,null,'F'))
3621                     )
3622       ) tpa
3623       group by tpa.report_code,
3624                tpa.reporting_date,
3625                tpa.business_group_id,
3626                tpa.legal_entity_id,
3627                tpa.legal_entity,
3628                tpa.disability,
3629                tpa.employment_type,
3630                tpa.meaning_code,
3631                tpa.termination_reason;
3632 
3633       commit;
3634 
3635       -- Insert zeroes for any Termination Categories that weren't used
3636       for l_counter in 1..6 loop
3637 
3638          -- The hard coded names of the legislative Termination Categories (not stored anywhere)
3639          if    l_counter = 1 then
3640             l_reason := 'Resignation';
3641          elsif l_counter = 2 then
3642             l_reason := 'Non-Renewal of Contract';
3643          elsif l_counter = 3 then
3644             l_reason := 'Dismissal - Operational Requirements';
3645          elsif l_counter = 4 then
3646             l_reason := 'Dismissal - Misconduct';
3647          elsif l_counter = 5 then
3648             l_reason := 'Dismissal - Incapacity';
3649          else
3650             l_reason := 'Other';
3651          end if;
3652 
3653          insert into per_za_employment_equity
3654          (
3655             report_id,
3656             reporting_date,
3657             business_group_id,
3658             legal_entity_id,
3659             legal_entity,
3660             disability,
3661             employment_type,
3662             level_cat_code,
3663             level_cat,
3664             MA,
3665             MC,
3666             MI,
3667             MW,
3668             FA,
3669             FC,
3670             FI,
3671             FW,
3672             total
3673          )
3674          select 'EQ8'                 report_id,
3675                 p_report_date         reporting_date,
3676                 p_business_group_id   business_group_id,
3677                 haou.organization_id  legal_entity_id,
3678                 haou.name             legal_entity,
3679                 'Y'                   disability,
3680                 'Permanent'           employment_type,
3681                 decode
3682                 (
3683                    l_reason,
3684                    'Resignation', 1,
3685                    'Non-Renewal of Contract', 2,
3686                    'Dismissal - Operational Requirements', 3,
3687                    'Dismissal - Misconduct', 4,
3688                    'Dismissal - Incapacity', 5,
3689                    'Other', 6,
3690                    null
3691                 )                     level_cat_code,
3692                decode
3693                (
3694                 l_reason,
3695                 'Dismissal - Operational Requirements', 'Dismissal - Operational Requirements (Retrenchment)',
3696                 l_reason
3697                )              level_cat,
3698                 0                     MA,
3699                 0                     MC,
3700                 0                     MI,
3701                 0                     MW,
3702                 0                     FA,
3703                 0                     FC,
3704                 0                     FI,
3705                 0                     FW,
3706                 0                     total
3707          from   hr_all_organization_units haou
3708          where not exists
3709          (
3710             select 'X'
3711             from   per_za_employment_equity pzee
3712             where  pzee.level_cat         = l_reason
3713             and    pzee.report_id         = 'EQ8'
3714             and    pzee.business_group_id = p_business_group_id  --Bug 4872110
3715             and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
3716             and    pzee.disability        = 'Y'
3717             and    pzee.employment_type   = 'Permanent'
3718          )
3719          and haou.business_group_id = p_business_group_id   --Bug 4872110
3720          and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
3721 
3722 
3723          insert into per_za_employment_equity
3724          (
3725             report_id,
3726             reporting_date,
3727             business_group_id,
3728             legal_entity_id,
3729             legal_entity,
3730             disability,
3731             employment_type,
3732             level_cat_code,
3733             level_cat,
3734             MA,
3735             MC,
3736             MI,
3737             MW,
3738             FA,
3739             FC,
3740             FI,
3741             FW,
3742             total
3743          )
3744          select 'EQ8F'                 report_id,
3745                 p_report_date         reporting_date,
3746                 p_business_group_id   business_group_id,
3747                 haou.organization_id  legal_entity_id,
3748                 haou.name             legal_entity,
3749                 'Y'                   disability,
3750                 'Permanent'           employment_type,
3751                 decode
3752                 (
3753                    l_reason,
3754                    'Resignation', 1,
3755                    'Non-Renewal of Contract', 2,
3756                    'Dismissal - Operational Requirements', 3,
3757                    'Dismissal - Misconduct', 4,
3758                    'Dismissal - Incapacity', 5,
3759                    'Other', 6,
3760                    null
3761                 )                     level_cat_code,
3762                decode
3763                (
3764                 l_reason,
3765                 'Dismissal - Operational Requirements', 'Dismissal - Operational Requirements (Retrenchment)',
3766                 l_reason
3767                )              level_cat,
3768                 0                     MA,
3769                 0                     MC,
3770                 0                     MI,
3771                 0                     MW,
3772                 0                     FA,
3773                 0                     FC,
3774                 0                     FI,
3775                 0                     FW,
3776                 0                     total
3777          from   hr_all_organization_units haou
3778          where not exists
3779          (
3780             select 'X'
3781             from   per_za_employment_equity pzee
3782             where  pzee.level_cat         = l_reason
3783             and    pzee.report_id         = 'EQ8F'
3784             and    pzee.business_group_id = p_business_group_id  --Bug 4872110
3785             and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
3786             and    pzee.disability        = 'Y'
3787             and    pzee.employment_type   = 'Permanent'
3788          )
3789          and haou.business_group_id = p_business_group_id   --Bug 4872110
3790          and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
3791 
3792       end loop;
3793 
3794       commit;
3795 
3796    end if;
3797 
3798 end populate_ee_table;
3799 
3800 
3801 function get_functional_type
3802 (
3803    p_report_date       in per_all_assignments_f.effective_end_date%type,
3804    p_assignment_id     in per_all_assignments_f.assignment_id%type,
3805    p_job_id            in per_all_assignments_f.job_id%type,
3806    p_grade_id          in per_all_assignments_f.grade_id%type,
3807    p_position_id       in per_all_assignments_f.position_id%type,
3808    p_business_group_id in per_all_assignments_f.business_group_id%type,
3809    p_year              in number
3810 )  return VARCHAR2 is
3811 
3812 begin
3813    -- Check whether we have cached the location of Occupational data
3814    if g_func_flex is null then
3815 
3816       cache_occupational_location(p_report_date, p_business_group_id,p_year);
3817 
3818    end if;
3819 
3820    -- Check whether the current assignment's value is cached already
3821 /*   if  p_report_date   = g_cat_report_date
3822    and p_assignment_id = g_cat_asg_id then
3823 
3824       return g_f_type_name;
3825 
3826    else
3827 
3828       g_cat_report_date := p_report_date;
3829       g_cat_asg_id      := p_assignment_id;
3830 */
3831       g_f_type_name := get_functional_data
3832                     (
3833                        p_flex        => g_Func_flex,
3834                        p_segment     => g_Func_segment,
3835                        p_job_id      => p_job_id,
3836                        p_grade_id    => p_grade_id,
3837                        p_position_id => p_position_id
3838                     );
3839 
3840       return g_f_type_name;
3841 
3842 --   end if;
3843 
3844 END get_functional_type;
3845 
3846 -- This function retrieves the functional data via dynamic sql from the appropriate flexfield segment
3847 function get_functional_data
3848 (
3849    p_flex        in varchar2,
3850    p_segment     in varchar2,
3851    p_job_id      in per_all_assignments_f.job_id%type,
3852    p_grade_id    in per_all_assignments_f.grade_id%type,
3853    p_position_id in per_all_assignments_f.position_id%type
3854 )  return varchar2 is
3855 
3856 l_sql  varchar2(32767);
3857 l_name hr_lookups.meaning%type;
3858 
3859 begin
3860 
3861    if p_flex = 'Job' then
3862 
3863       begin
3864 
3865          if p_job_id is not null then
3866 
3867             l_sql := 'select hl.meaning from hr_lookups hl, per_job_definitions pjd, per_jobs pj where pj.job_id = '
3868                      || to_char(p_job_id)
3869                      || '  and pjd.job_definition_id = pj.job_definition_id and hl.application_id = 800 and hl.lookup_type = ''ZA_EE_FUNCTION_TYPE'
3870                      || ''' and hl.lookup_code = pjd.' || p_segment;
3871 
3872             execute immediate l_sql into l_name;
3873 
3874          else
3875 
3876             l_name := null;
3877 
3878          end if;
3879 
3880       exception
3881          when no_data_found then
3882             l_name := null;
3883 
3884       end;
3885 
3886    elsif p_flex = 'Grade' then
3887 
3888       begin
3889 
3890          if p_grade_id is not null then
3891 
3892             l_sql := 'select hl.meaning from hr_lookups hl, per_grade_definitions pgd, per_grades pg where pg.grade_id = '
3893                      || to_char(p_grade_id)
3894                      || '  and pgd.grade_definition_id = pg.grade_definition_id and hl.application_id = 800 and hl.lookup_type = ''ZA_EE_FUNCTION_TYPE'
3895                      || ''' and hl.lookup_code = pgd.' || p_segment;
3896 
3897             execute immediate l_sql into l_name;
3898 
3899          else
3900 
3901             l_name := null;
3902 
3903          end if;
3904 
3905       exception
3906          when no_data_found then
3907             l_name := null;
3908 
3909       end;
3910 
3911    elsif p_flex = 'Position' then
3912 
3913       begin
3914 
3915          if p_position_id is not null then
3916 
3917             l_sql := 'select hl.meaning from hr_lookups hl, per_position_definitions ppd, per_all_positions pap where pap.position_id = '
3918                      || to_char(p_position_id)
3919                      || '  and ppd.position_definition_id = pap.position_definition_id and hl.application_id = 800 and hl.lookup_type = ''ZA_EE_FUNCTION_TYPE'
3920                      || ''' and hl.lookup_code = ppd.' || p_segment;
3921 
3922             execute immediate l_sql into l_name;
3923 
3924          else
3925 
3926             l_name := null;
3927 
3928          end if;
3929 
3930       exception
3931          when no_data_found then
3932             l_name := null;
3933 
3934       end;
3935 
3936    else
3937 
3938       raise_application_error(-20002, 'The Functional Type in the User Table ZA_OCCUPATIONAL_TYPES refers to an invalid Flexfield.');
3939 
3940    end if;
3941 
3942    return l_name;
3943 
3944 end get_functional_data;
3945 
3946    procedure populate_ee_table_EEWF
3947    (
3948       p_report_date       in per_all_assignments_f.effective_end_date%type,
3949       p_business_group_id in per_all_assignments_f.business_group_id%type,
3950       p_legal_entity_id   in per_assignment_extra_info.aei_information7%type := null
3951    )  is
3952 
3953    begin
3954     DELETE FROM per_za_employment_equity
3955     Where REPORT_ID IN ('EQ1','EQ2','EQ3','EQ4','EQ5','EQ6','EQ7','EQ8',
3956                        'EQ1F','EQ2F','EQ3F','EQ4F','EQ5F','EQ6F','EQ7F','EQ8F'
3957                        );
3958 
3959     populate_ee_table (
3960                        p_report_code       =>'EQ1'
3961                      , p_report_date       =>p_report_date
3962                      , p_business_group_id =>p_business_group_id
3963                      , p_legal_entity_id   =>p_legal_entity_id
3964                        );
3965 
3966     populate_ee_table (
3967                        p_report_code       =>'EQ2'
3968                      , p_report_date       =>p_report_date
3969                      , p_business_group_id =>p_business_group_id
3970                      , p_legal_entity_id   =>p_legal_entity_id
3971                        );
3972 
3973     populate_ee_table (
3974                        p_report_code       =>'EQ3'
3975                      , p_report_date       =>p_report_date
3976                      , p_business_group_id =>p_business_group_id
3977                      , p_legal_entity_id   =>p_legal_entity_id
3978                        );
3979 
3980     populate_ee_table (
3981                        p_report_code       =>'EQ4'
3982                      , p_report_date       =>p_report_date
3983                      , p_business_group_id =>p_business_group_id
3984                      , p_legal_entity_id   =>p_legal_entity_id
3985                        );
3986 
3987     populate_ee_table (
3988                        p_report_code       =>'EQ5'
3989                      , p_report_date       =>p_report_date
3990                      , p_business_group_id =>p_business_group_id
3991                      , p_legal_entity_id   =>p_legal_entity_id
3992                        );
3993 
3994     populate_ee_table (
3995                        p_report_code       =>'EQ6'
3996                      , p_report_date       =>p_report_date
3997                      , p_business_group_id =>p_business_group_id
3998                      , p_legal_entity_id   =>p_legal_entity_id
3999                        );
4000 
4001     populate_ee_table (
4002                        p_report_code       =>'EQ7'
4003                      , p_report_date       =>p_report_date
4004                      , p_business_group_id =>p_business_group_id
4005                      , p_legal_entity_id   =>p_legal_entity_id
4006                        );
4007 
4008     populate_ee_table (
4009                        p_report_code       =>'EQ8'
4010                      , p_report_date       =>p_report_date
4011                      , p_business_group_id =>p_business_group_id
4012                      , p_legal_entity_id   =>p_legal_entity_id
4013                        );
4014 
4015       -- Inserts non-associated occupational categories with zero values
4016       insert into per_za_employment_equity
4017       (
4018          report_id,
4019          reporting_date,
4020          business_group_id,
4021          legal_entity_id,
4022          legal_entity,
4023          disability,
4024          employment_type,
4025          level_cat_code,
4026          level_cat,
4027          MA,
4028          MC,
4029          MI,
4030          MW,
4031          FA,
4032          FC,
4033          FI,
4034          FW,
4035          total
4036       )
4037       select          substr(report_id,1,3),
4038          reporting_date,
4039          business_group_id,
4040          legal_entity_id,
4041          legal_entity,
4042          disability,
4043          employment_type,
4044          level_cat_code,
4045          level_cat,
4046          0,
4047          0,
4048          0,
4049          0,
4050          0,
4051          0,
4052          0,
4053          0,
4054          0
4055       from   per_za_employment_equity pzee1
4056       Where  pzee1.business_group_id = p_business_group_id
4057       AND    pzee1.legal_entity_id = nvl(p_legal_entity_id, pzee1.legal_entity_id)
4058       AND    pzee1.report_id IN ('EQ1F','EQ2F','EQ3F','EQ4F','EQ5F','EQ6F','EQ7F','EQ8F')
4059       AND    not exists
4060       (
4061          select 'X'
4062          from   per_za_employment_equity pzee
4063          where  pzee.business_group_id   = pzee1.business_group_id           --Bug 4872110
4064          AND    pzee.legal_entity_id    = pzee1.legal_entity_id
4065          AND    pzee.report_id ||'F'     = pzee1.report_id
4066          AND    pzee1.level_cat_code     = pzee.level_cat_code
4067          AND    pzee1.level_cat          = pzee.level_cat
4068          and    nvl(pzee.disability,'X') = nvl(pzee1.disability,'X')
4069          and    pzee.employment_type     = pzee1.employment_type
4070       );
4071 
4072 
4073       -- Inserts non-associated occupational categories with zero values
4074       insert into per_za_employment_equity
4075       (
4076          report_id,
4077          reporting_date,
4078          business_group_id,
4079          legal_entity_id,
4080          legal_entity,
4081          disability,
4082          employment_type,
4083          level_cat_code,
4084          level_cat,
4085          MA,
4086          MC,
4087          MI,
4088          MW,
4089          FA,
4090          FC,
4091          FI,
4092          FW,
4093          total
4094       )
4095       select          report_id||'F' report_id,
4096          reporting_date,
4097          business_group_id,
4098          legal_entity_id,
4099          legal_entity,
4100          disability,
4101          employment_type,
4102          level_cat_code,
4103          level_cat,
4104          0,
4105          0,
4106          0,
4107          0,
4108          0,
4109          0,
4110          0,
4111          0,
4112          0
4113       from   per_za_employment_equity pzee1
4114       Where  pzee1.business_group_id = p_business_group_id
4115       AND    pzee1.legal_entity_id = nvl(p_legal_entity_id, pzee1.legal_entity_id)
4116       AND    pzee1.report_id IN ('EQ1','EQ2','EQ3','EQ4','EQ5','EQ6','EQ7','EQ8')
4117       AND    not exists
4118       (
4119          select 'X'
4120          from   per_za_employment_equity pzee
4121          where  pzee.business_group_id   = pzee1.business_group_id           --Bug 4872110
4122          AND    pzee.legal_entity_id    = pzee1.legal_entity_id
4123          AND    pzee1.report_id ||'F'     = pzee.report_id
4124          AND    pzee1.level_cat_code     = pzee.level_cat_code
4125          AND    pzee1.level_cat          = pzee.level_cat
4126          and    nvl(pzee.disability,'X') = nvl(pzee1.disability,'X')
4127          and    pzee.employment_type     = pzee1.employment_type
4128       );
4129 
4130   commit;
4131 
4132 
4133    End populate_ee_table_EEWF;
4134 
4135 -- Procedure to insert data into global tables.
4136 -- used in Employment Equity Encome Differential report
4137 --
4138 PROCEDURE ins_g_Enc_Diff_table(p_mi_inc     IN number
4139                              , p_mc_inc     IN number
4140                              , p_ma_inc     IN number
4141                              , p_mw_inc     IN number
4142                              , p_fa_inc     IN number
4143                              , p_fc_inc     IN number
4144                              , p_fi_inc     IN number
4145                              , p_fw_inc     IN number
4146                              , p_total_inc  IN number
4147                              , p_ma         IN number
4148                              , p_mc         IN number
4149                              , p_mi         IN number
4150                              , p_mw         IN number
4151                              , p_fa         IN number
4152                              , p_fc         IN number
4153                              , p_fi         IN number
4154                              , p_fw         IN number
4155                              , p_total      IN number
4156                              , p_cat_index  IN number
4157                              , p_lev_index  IN number
4158                              , p_legal_entity_id       IN hr_all_organization_units.organization_id%type
4159                              , p_occupational_level IN hr_lookups.meaning%type
4160                              , p_occupational_category IN hr_lookups.meaning%type
4161                              , p_occupational_level_id IN hr_lookups.lookup_code%type
4162                              , p_occupational_category_id IN hr_lookups.lookup_code%type
4163                               ) is
4164 
4165 begin
4166 
4167             IF g_cat_Enc_Diff_table.EXISTS(p_cat_index) then
4168                g_cat_Enc_Diff_table(p_cat_index).mi_inc    :=    g_cat_Enc_Diff_table(p_cat_index).mi_inc    + p_mi_inc ;
4169                g_cat_Enc_Diff_table(p_cat_index).mc_inc    :=    g_cat_Enc_Diff_table(p_cat_index).mc_inc    + p_mc_inc ;
4170                g_cat_Enc_Diff_table(p_cat_index).ma_inc    :=    g_cat_Enc_Diff_table(p_cat_index).ma_inc    + p_ma_inc ;
4171                g_cat_Enc_Diff_table(p_cat_index).mw_inc    :=    g_cat_Enc_Diff_table(p_cat_index).mw_inc    + p_mw_inc ;
4172                g_cat_Enc_Diff_table(p_cat_index).fa_inc    :=    g_cat_Enc_Diff_table(p_cat_index).fa_inc    + p_fa_inc ;
4173                g_cat_Enc_Diff_table(p_cat_index).fc_inc    :=    g_cat_Enc_Diff_table(p_cat_index).fc_inc    + p_fc_inc ;
4174                g_cat_Enc_Diff_table(p_cat_index).fi_inc    :=    g_cat_Enc_Diff_table(p_cat_index).fi_inc    + p_fi_inc ;
4175                g_cat_Enc_Diff_table(p_cat_index).fw_inc    :=    g_cat_Enc_Diff_table(p_cat_index).fw_inc    + p_fw_inc ;
4176                g_cat_Enc_Diff_table(p_cat_index).total_inc :=    g_cat_Enc_Diff_table(p_cat_index).total_inc + p_total_inc;
4177 
4178                g_cat_Enc_Diff_table(p_cat_index).mi    :=    g_cat_Enc_Diff_table(p_cat_index).mi    + p_mi ;
4179                g_cat_Enc_Diff_table(p_cat_index).mc    :=    g_cat_Enc_Diff_table(p_cat_index).mc    + p_mc ;
4180                g_cat_Enc_Diff_table(p_cat_index).ma    :=    g_cat_Enc_Diff_table(p_cat_index).ma    + p_ma ;
4181                g_cat_Enc_Diff_table(p_cat_index).mw    :=    g_cat_Enc_Diff_table(p_cat_index).mw    + p_mw ;
4182                g_cat_Enc_Diff_table(p_cat_index).fa    :=    g_cat_Enc_Diff_table(p_cat_index).fa    + p_fa ;
4183                g_cat_Enc_Diff_table(p_cat_index).fc    :=    g_cat_Enc_Diff_table(p_cat_index).fc    + p_fc ;
4184                g_cat_Enc_Diff_table(p_cat_index).fi    :=    g_cat_Enc_Diff_table(p_cat_index).fi    + p_fi ;
4185                g_cat_Enc_Diff_table(p_cat_index).fw    :=    g_cat_Enc_Diff_table(p_cat_index).fw    + p_fw ;
4186                g_cat_Enc_Diff_table(p_cat_index).total :=    g_cat_Enc_Diff_table(p_cat_index).total + p_total;
4187 
4188             else
4189                g_cat_Enc_Diff_table(p_cat_index).mi_inc    :=  p_mi_inc ;
4190                g_cat_Enc_Diff_table(p_cat_index).mc_inc    :=  p_mc_inc ;
4191                g_cat_Enc_Diff_table(p_cat_index).ma_inc    :=  p_ma_inc ;
4192                g_cat_Enc_Diff_table(p_cat_index).mw_inc    :=  p_mw_inc ;
4193                g_cat_Enc_Diff_table(p_cat_index).fa_inc    :=  p_fa_inc ;
4194                g_cat_Enc_Diff_table(p_cat_index).fc_inc    :=  p_fc_inc ;
4195                g_cat_Enc_Diff_table(p_cat_index).fi_inc    :=  p_fi_inc ;
4196                g_cat_Enc_Diff_table(p_cat_index).fw_inc    :=  p_fw_inc ;
4197                g_cat_Enc_Diff_table(p_cat_index).total_inc :=  p_total_inc;
4198 
4199                g_cat_Enc_Diff_table(p_cat_index).mi    :=  p_mi ;
4200                g_cat_Enc_Diff_table(p_cat_index).mc    :=  p_mc ;
4201                g_cat_Enc_Diff_table(p_cat_index).ma    :=  p_ma ;
4202                g_cat_Enc_Diff_table(p_cat_index).mw    :=  p_mw ;
4203                g_cat_Enc_Diff_table(p_cat_index).fa    :=  p_fa ;
4204                g_cat_Enc_Diff_table(p_cat_index).fc    :=  p_fc ;
4205                g_cat_Enc_Diff_table(p_cat_index).fi    :=  p_fi ;
4206                g_cat_Enc_Diff_table(p_cat_index).fw    :=  p_fw ;
4207                g_cat_Enc_Diff_table(p_cat_index).total :=  p_total;
4208                g_cat_Enc_Diff_table(p_cat_index).legal_entity_id := p_legal_entity_id;
4209                g_cat_Enc_Diff_table(p_cat_index).occupational_code := p_occupational_category;
4210                g_cat_Enc_Diff_table(p_cat_index).occupational_code_id := p_occupational_category_id;
4211             END if;
4212             IF g_lev_Enc_Diff_table.EXISTS(p_lev_index) then
4213                g_lev_Enc_Diff_table(p_lev_index).mi_inc    :=  g_lev_Enc_Diff_table(p_lev_index).mi_inc    + p_mi_inc ;
4214                g_lev_Enc_Diff_table(p_lev_index).mc_inc    :=  g_lev_Enc_Diff_table(p_lev_index).mc_inc    + p_mc_inc ;
4215                g_lev_Enc_Diff_table(p_lev_index).ma_inc    :=  g_lev_Enc_Diff_table(p_lev_index).ma_inc    + p_ma_inc ;
4216                g_lev_Enc_Diff_table(p_lev_index).mw_inc    :=  g_lev_Enc_Diff_table(p_lev_index).mw_inc    + p_mw_inc ;
4217                g_lev_Enc_Diff_table(p_lev_index).fa_inc    :=  g_lev_Enc_Diff_table(p_lev_index).fa_inc    + p_fa_inc ;
4218                g_lev_Enc_Diff_table(p_lev_index).fc_inc    :=  g_lev_Enc_Diff_table(p_lev_index).fc_inc    + p_fc_inc ;
4219                g_lev_Enc_Diff_table(p_lev_index).fi_inc    :=  g_lev_Enc_Diff_table(p_lev_index).fi_inc    + p_fi_inc ;
4220                g_lev_Enc_Diff_table(p_lev_index).fw_inc    :=  g_lev_Enc_Diff_table(p_lev_index).fw_inc    + p_fw_inc ;
4221                g_lev_Enc_Diff_table(p_lev_index).total_inc :=  g_lev_Enc_Diff_table(p_lev_index).total_inc + p_total_inc;
4222 
4223                g_lev_Enc_Diff_table(p_lev_index).ma    := g_lev_Enc_Diff_table(p_lev_index).ma    +  p_ma ;
4224                g_lev_Enc_Diff_table(p_lev_index).mc    := g_lev_Enc_Diff_table(p_lev_index).mc    +  p_mc ;
4225                g_lev_Enc_Diff_table(p_lev_index).mi    := g_lev_Enc_Diff_table(p_lev_index).mi    +  p_mi ;
4226                g_lev_Enc_Diff_table(p_lev_index).mw    := g_lev_Enc_Diff_table(p_lev_index).mw    +  p_mw ;
4227                g_lev_Enc_Diff_table(p_lev_index).fa    := g_lev_Enc_Diff_table(p_lev_index).fa    +  p_fa ;
4228                g_lev_Enc_Diff_table(p_lev_index).fc    := g_lev_Enc_Diff_table(p_lev_index).fc    +  p_fc ;
4229                g_lev_Enc_Diff_table(p_lev_index).fi    := g_lev_Enc_Diff_table(p_lev_index).fi    +  p_fi ;
4230                g_lev_Enc_Diff_table(p_lev_index).fw    := g_lev_Enc_Diff_table(p_lev_index).fw    +  p_fw ;
4231                g_lev_Enc_Diff_table(p_lev_index).total := g_lev_Enc_Diff_table(p_lev_index).total +  p_total;
4232             else
4233                g_lev_Enc_Diff_table(p_lev_index).mi_inc    :=  p_mi_inc ;
4234                g_lev_Enc_Diff_table(p_lev_index).mc_inc    :=  p_mc_inc ;
4235                g_lev_Enc_Diff_table(p_lev_index).ma_inc    :=  p_ma_inc ;
4236                g_lev_Enc_Diff_table(p_lev_index).mw_inc    :=  p_mw_inc ;
4237                g_lev_Enc_Diff_table(p_lev_index).fa_inc    :=  p_fa_inc ;
4238                g_lev_Enc_Diff_table(p_lev_index).fc_inc    :=  p_fc_inc ;
4239                g_lev_Enc_Diff_table(p_lev_index).fi_inc    :=  p_fi_inc ;
4240                g_lev_Enc_Diff_table(p_lev_index).fw_inc    :=  p_fw_inc ;
4241                g_lev_Enc_Diff_table(p_lev_index).total_inc :=  p_total_inc;
4242 
4243                g_lev_Enc_Diff_table(p_lev_index).ma    :=  p_ma ;
4244                g_lev_Enc_Diff_table(p_lev_index).mc    :=  p_mc ;
4245                g_lev_Enc_Diff_table(p_lev_index).mi    :=  p_mi ;
4246                g_lev_Enc_Diff_table(p_lev_index).mw    :=  p_mw ;
4247                g_lev_Enc_Diff_table(p_lev_index).fa    :=  p_fa ;
4248                g_lev_Enc_Diff_table(p_lev_index).fc    :=  p_fc ;
4249                g_lev_Enc_Diff_table(p_lev_index).fi    :=  p_fi ;
4250                g_lev_Enc_Diff_table(p_lev_index).fw    :=  p_fw ;
4251                g_lev_Enc_Diff_table(p_lev_index).total :=  p_total;
4252                g_lev_Enc_Diff_table(p_lev_index).legal_entity_id := p_legal_entity_id;
4253                g_lev_Enc_Diff_table(p_lev_index).occupational_code := p_occupational_level;
4254                g_lev_Enc_Diff_table(p_lev_index).occupational_code_id := p_occupational_level_id;
4255             End if;
4256 
4257 END ins_g_Enc_Diff_table;
4258 
4259 
4260 -- Procedure is used to sort the employee data
4261 -- and ready to inset into global tables
4262 
4263 Procedure cat_lev_data ( p_legal_entity_id IN hr_all_organization_units.organization_id%type
4264                        , p_occupational_level IN hr_lookups.meaning%type
4265                        , p_occupational_category IN hr_lookups.meaning%type
4266                        , p_race IN per_all_people_f.per_information4%type
4267                        , p_sex IN per_all_people_f.sex%type
4268                        , p_income IN number
4269                        , p_occupational_level_id IN hr_lookups.lookup_code%type
4270                        , p_occupational_category_id IN hr_lookups.lookup_code%type
4271                        ) is
4272 
4273  l_cat_index  pls_integer ;
4274  l_lev_index  pls_integer ;
4275 begin
4276 
4277    begin
4278 
4279    l_cat_index := p_legal_entity_id *100 + nvl(p_occupational_category_id,0);
4280    l_lev_index := p_legal_entity_id *100 + nvl(p_occupational_level_id,0);
4281    hr_utility.set_location('l_cat_index ' || l_cat_index, 25);
4282    hr_utility.set_location('l_lev_index ' || l_lev_index, 25);
4283 
4284    exception
4285       when others then
4286          raise_application_error(-20006, 'The lookup code in the ZA_EMP_EQ_OCCUPATIONAL_LEV and ZA_EMP_EQ_OCCUPATIONAL_CAT lookups must be numeric.');
4287 
4288    end;
4289 
4290      CASE p_sex||p_race
4291           WHEN  'M01' THEN --male Indian (MI)
4292               ins_g_Enc_Diff_table(p_mi_inc  =>  p_income
4293                              , p_mc_inc    => 0
4294                              , p_ma_inc    => 0
4295                              , p_mw_inc    => 0
4296                              , p_fa_inc    => 0
4297                              , p_fc_inc    => 0
4298                              , p_fi_inc    => 0
4299                              , p_fw_inc    => 0
4300                              , p_total_inc => p_income
4301                              , p_ma        => 0
4302                              , p_mc        => 0
4303                              , p_mi        => 1
4304                              , p_mw        => 0
4305                              , p_fa        => 0
4306                              , p_fc        => 0
4307                              , p_fi        => 0
4308                              , p_fw        => 0
4309                              , p_total     => 1
4310                              , p_cat_index => l_cat_index
4311                              , p_lev_index => l_lev_index
4312                              , p_legal_entity_id       => p_legal_entity_id
4313                              , p_occupational_level    => p_occupational_level
4314                              , p_occupational_category => p_occupational_category
4315                              , p_occupational_level_id    => p_occupational_level_id
4316                              , p_occupational_category_id => p_occupational_category_id
4317                              );
4318           WHEN  'M02' THEN --male African
4319               ins_g_Enc_Diff_table(p_mi_inc  => 0
4320                              , p_mc_inc    => 0
4321                              , p_ma_inc    =>  p_income
4322                              , p_mw_inc    => 0
4323                              , p_fa_inc    => 0
4324                              , p_fc_inc    => 0
4325                              , p_fi_inc    => 0
4326                              , p_fw_inc    => 0
4327                              , p_total_inc => p_income
4328                              , p_ma        => 1
4329                              , p_mc        => 0
4330                              , p_mi        => 0
4331                              , p_mw        => 0
4332                              , p_fa        => 0
4333                              , p_fc        => 0
4334                              , p_fi        => 0
4335                              , p_fw        => 0
4336                              , p_total     => 1
4337                              , p_cat_index => l_cat_index
4338                              , p_lev_index => l_lev_index
4339                              , p_legal_entity_id       => p_legal_entity_id
4340                              , p_occupational_level    => p_occupational_level
4341                              , p_occupational_category => p_occupational_category
4342                              , p_occupational_level_id    => p_occupational_level_id
4343                              , p_occupational_category_id => p_occupational_category_id
4344                              );
4345           WHEN  'M03' THEN --male Coloured
4346               ins_g_Enc_Diff_table(p_mi_inc  => 0
4347                              , p_mc_inc    =>  p_income
4348                              , p_ma_inc    => 0
4349                              , p_mw_inc    => 0
4350                              , p_fa_inc    => 0
4351                              , p_fc_inc    => 0
4352                              , p_fi_inc    => 0
4353                              , p_fw_inc    => 0
4354                              , p_total_inc => p_income
4355                              , p_ma        => 0
4356                              , p_mc        => 1
4357                              , p_mi        => 0
4358                              , p_mw        => 0
4359                              , p_fa        => 0
4360                              , p_fc        => 0
4361                              , p_fi        => 0
4362                              , p_fw        => 0
4363                              , p_total     => 1
4364                              , p_cat_index => l_cat_index
4365                              , p_lev_index => l_lev_index
4366                              , p_legal_entity_id       => p_legal_entity_id
4367                              , p_occupational_level    => p_occupational_level
4368                              , p_occupational_category => p_occupational_category
4369                              , p_occupational_level_id    => p_occupational_level_id
4370                              , p_occupational_category_id => p_occupational_category_id
4371                              );
4372           WHEN  'M04' THEN --male White
4373               ins_g_Enc_Diff_table(p_mi_inc  => 0
4374                              , p_mc_inc    => 0
4375                              , p_ma_inc    => 0
4376                              , p_mw_inc    =>  p_income
4377                              , p_fa_inc    => 0
4378                              , p_fc_inc    => 0
4379                              , p_fi_inc    => 0
4380                              , p_fw_inc    => 0
4381                              , p_total_inc => p_income
4382                              , p_ma        => 0
4383                              , p_mc        => 0
4384                              , p_mi        => 0
4385                              , p_mw        => 1
4386                              , p_fa        => 0
4387                              , p_fc        => 0
4388                              , p_fi        => 0
4389                              , p_fw        => 0
4390                              , p_total     => 1
4391                              , p_cat_index => l_cat_index
4392                              , p_lev_index => l_lev_index
4393                              , p_legal_entity_id       => p_legal_entity_id
4394                              , p_occupational_level    => p_occupational_level
4395                              , p_occupational_category => p_occupational_category
4396                              , p_occupational_level_id    => p_occupational_level_id
4397                              , p_occupational_category_id => p_occupational_category_id
4398                              );
4399           WHEN  'MZA01' THEN --male Chinese (To be reported as African)
4400               ins_g_Enc_Diff_table(p_mi_inc  => 0
4401                              , p_mc_inc    => 0
4402                              , p_ma_inc    =>  p_income
4403                              , p_mw_inc    => 0
4404                              , p_fa_inc    => 0
4405                              , p_fc_inc    => 0
4406                              , p_fi_inc    => 0
4407                              , p_fw_inc    => 0
4408                              , p_total_inc => p_income
4409                              , p_ma        => 1
4410                              , p_mc        => 0
4411                              , p_mi        => 0
4412                              , p_mw        => 0
4413                              , p_fa        => 0
4414                              , p_fc        => 0
4415                              , p_fi        => 0
4416                              , p_fw        => 0
4417                              , p_total     => 1
4418                              , p_cat_index => l_cat_index
4419                              , p_lev_index => l_lev_index
4420                              , p_legal_entity_id       => p_legal_entity_id
4421                              , p_occupational_level    => p_occupational_level
4422                              , p_occupational_category => p_occupational_category
4423                              , p_occupational_level_id    => p_occupational_level_id
4424                              , p_occupational_category_id => p_occupational_category_id
4425                              );
4426 
4427           WHEN  'F01' THEN --female Indian
4428               ins_g_Enc_Diff_table(p_mi_inc  => 0
4429                              , p_mc_inc    => 0
4430                              , p_ma_inc    => 0
4431                              , p_mw_inc    =>  0
4432                              , p_fa_inc    => 0
4433                              , p_fc_inc    => 0
4434                              , p_fi_inc    =>p_income
4435                              , p_fw_inc    => 0
4436                              , p_total_inc => p_income
4437                              , p_ma        => 0
4438                              , p_mc        => 0
4439                              , p_mi        => 0
4440                              , p_mw        => 0
4441                              , p_fa        => 0
4442                              , p_fc        => 0
4443                              , p_fi        => 1
4444                              , p_fw        => 0
4445                              , p_total     => 1
4446                              , p_cat_index => l_cat_index
4447                              , p_lev_index => l_lev_index
4448                              , p_legal_entity_id       => p_legal_entity_id
4449                              , p_occupational_level    => p_occupational_level
4450                              , p_occupational_category => p_occupational_category
4451                              , p_occupational_level_id    => p_occupational_level_id
4452                              , p_occupational_category_id => p_occupational_category_id
4453                              );
4454           WHEN  'F02' THEN --female African
4455               ins_g_Enc_Diff_table(p_mi_inc  => 0
4456                              , p_mc_inc    => 0
4457                              , p_ma_inc    => 0
4458                              , p_mw_inc    => 0
4459                              , p_fa_inc    => p_income
4460                              , p_fc_inc    => 0
4461                              , p_fi_inc    => 0
4462                              , p_fw_inc    => 0
4463                              , p_total_inc => p_income
4464                              , p_ma        => 0
4465                              , p_mc        => 0
4466                              , p_mi        => 0
4467                              , p_mw        => 0
4468                              , p_fa        => 1
4469                              , p_fc        => 0
4470                              , p_fi        => 0
4471                              , p_fw        => 0
4472                              , p_total     => 1
4473                              , p_cat_index => l_cat_index
4474                              , p_lev_index => l_lev_index
4475                              , p_legal_entity_id       => p_legal_entity_id
4476                              , p_occupational_level    => p_occupational_level
4477                              , p_occupational_category => p_occupational_category
4478                              , p_occupational_level_id    => p_occupational_level_id
4479                              , p_occupational_category_id => p_occupational_category_id
4480                              );
4481           WHEN  'F03' THEN --female Coloured
4482               ins_g_Enc_Diff_table(p_mi_inc  => 0
4483                              , p_mc_inc    => 0
4484                              , p_ma_inc    => 0
4485                              , p_mw_inc    => 0
4486                              , p_fa_inc    => 0
4487                              , p_fc_inc    => p_income
4488                              , p_fi_inc    => 0
4489                              , p_fw_inc    => 0
4490                              , p_total_inc => p_income
4491                              , p_ma        => 0
4492                              , p_mc        => 0
4493                              , p_mi        => 0
4494                              , p_mw        => 0
4495                              , p_fa        => 0
4496                              , p_fc        => 1
4497                              , p_fi        => 0
4498                              , p_fw        => 0
4499                              , p_total     => 1
4500                              , p_cat_index => l_cat_index
4501                              , p_lev_index => l_lev_index
4502                              , p_legal_entity_id       => p_legal_entity_id
4503                              , p_occupational_level    => p_occupational_level
4504                              , p_occupational_category => p_occupational_category
4505                              , p_occupational_level_id    => p_occupational_level_id
4506                              , p_occupational_category_id => p_occupational_category_id
4507                              );
4508           WHEN  'F04' THEN --female White
4509               ins_g_Enc_Diff_table(p_mi_inc  => 0
4510                              , p_mc_inc    => 0
4511                              , p_ma_inc    => 0
4512                              , p_mw_inc    => 0
4513                              , p_fa_inc    => 0
4514                              , p_fc_inc    => 0
4515                              , p_fi_inc    => 0
4516                              , p_fw_inc    => p_income
4517                              , p_total_inc => p_income
4518                              , p_ma        => 0
4519                              , p_mc        => 0
4520                              , p_mi        => 0
4521                              , p_mw        => 0
4522                              , p_fa        => 0
4523                              , p_fc        => 0
4524                              , p_fi        => 0
4525                              , p_fw        => 1
4526                              , p_total     => 1
4527                              , p_cat_index => l_cat_index
4528                              , p_lev_index => l_lev_index
4529                              , p_legal_entity_id       => p_legal_entity_id
4530                              , p_occupational_level    => p_occupational_level
4531                              , p_occupational_category => p_occupational_category
4532                              , p_occupational_level_id    => p_occupational_level_id
4533                              , p_occupational_category_id => p_occupational_category_id
4534                              );
4535           WHEN  'FZA01' THEN --female Chinese (To be reported as African)
4536               ins_g_Enc_Diff_table(p_mi_inc  => 0
4537                              , p_mc_inc    => 0
4538                              , p_ma_inc    => 0
4539                              , p_mw_inc    => 0
4540                              , p_fa_inc    => p_income
4541                              , p_fc_inc    => 0
4542                              , p_fi_inc    => 0
4543                              , p_fw_inc    => 0
4544                              , p_total_inc => p_income
4545                              , p_ma        => 0
4546                              , p_mc        => 0
4547                              , p_mi        => 0
4548                              , p_mw        => 0
4549                              , p_fa        => 1
4550                              , p_fc        => 0
4551                              , p_fi        => 0
4552                              , p_fw        => 0
4553                              , p_total     => 1
4554                              , p_cat_index => l_cat_index
4555                              , p_lev_index => l_lev_index
4556                              , p_legal_entity_id       => p_legal_entity_id
4557                              , p_occupational_level    => p_occupational_level
4558                              , p_occupational_category => p_occupational_category
4559                              , p_occupational_level_id    => p_occupational_level_id
4560                              , p_occupational_category_id => p_occupational_category_id
4561                              );
4562              else
4563              null;
4564          END case;
4565 END cat_lev_data;
4566 
4567 
4568 
4569 -- Procedure to initialise with the employee details
4570 --
4571 procedure init_g_cat_lev_table
4572 (
4573    p_report_date            in per_all_assignments_f.effective_end_date%type,
4574    p_business_group_id      in per_all_assignments_f.business_group_id%type,
4575    p_legal_entity_id        in per_assignment_extra_info.aei_information7%type := null,
4576    p_salary_method          in varchar2  -- SAL = Salary Basis Method, BAL = Payroll Balances Method
4577 )  is
4578 /* commented for Bug 8911880
4579 cursor c_assignments is
4580    select paaf.assignment_id,
4581           paaf.person_id, -- Bug 4413678
4582           paaf.payroll_id,
4583           paei.aei_information7 ,
4584           hl_cat.lookup_code      OCCUPATIONAL_CATEGORY_ID,
4585           hl_lev.lookup_code      OCCUPATIONAL_LEVEL_ID,
4586           per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)    occupational_level,
4587           per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id) occupational_category,
4588           paaf.pay_basis_id
4589    from   per_assignment_extra_info   paei,
4590           per_assignment_status_types past,
4591           per_all_assignments_f       paaf,
4592           hr_lookups                  hl_cat,
4593           hr_lookups                  hl_lev,
4594           hr_lookups                  hl_fn
4595    where  paaf.business_group_id = p_business_group_id
4596    and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
4597    and    past.assignment_status_type_id = paaf.assignment_status_type_id
4598    and    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
4599    and    paei.assignment_id = paaf.assignment_id
4600    and    paei.information_type = 'ZA_SPECIFIC_INFO'
4601    and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
4602    and    paei.aei_information7 is not null
4603    AND    hl_cat.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
4604    AND    hl_lev.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
4605    AND    hl_fn.lookup_type = 'ZA_EE_FUNCTION_TYPE'    --Added for Bug 7360563
4606    AND    hl_cat.lookup_code <> '15'
4607    AND    hl_lev.lookup_code <> '15'
4608    AND    hl_fn.lookup_code <>'15'
4609    AND    hl_cat.application_id = '800'
4610    AND    hl_lev.application_id = '800'
4611    AND    hl_fn.application_id  = '800'
4612    AND    hl_cat.meaning(+)       = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
4613    AND    hl_lev.meaning(+)       = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
4614    AND    hl_fn.meaning(+)        = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
4615    and    nvl(paei.aei_information6, 'N') <> 'Y'
4616    order  BY paei.aei_information7, paaf.payroll_id;
4617 */
4618 
4619 cursor c_assignments is
4620    select paaf.assignment_id,
4621           paaf.person_id, -- Bug 4413678
4622           paaf.payroll_id,
4623           paei.aei_information7 ,
4624           hl_cat.lookup_code      OCCUPATIONAL_CATEGORY_ID,
4625           hl_lev.lookup_code      OCCUPATIONAL_LEVEL_ID,
4626           per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)    occupational_level,
4627           per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id) occupational_category,
4628           paaf.pay_basis_id
4629    from   per_assignment_extra_info   paei,
4630           per_all_assignments_f       paaf,
4631           hr_lookups                  hl_cat,
4632           hr_lookups                  hl_lev,
4633           hr_lookups                  hl_fn
4634    where  paaf.business_group_id = p_business_group_id
4635    and    (add_months(p_report_date,-12)+1 <=paaf.effective_end_date and p_report_date >=paaf.effective_start_date)
4636    and    paaf.effective_end_date = (  select max(paaf1.effective_end_date)
4637                                        from   per_all_assignments_f paaf1,
4638                                               per_assignment_status_types past
4639                                        where  paaf1.assignment_id = paaf.assignment_id
4640                                        and    paaf1.effective_start_date <= p_report_date
4641                                        and    past.assignment_status_type_id = paaf1.assignment_status_type_id
4642                                        and    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
4643                                      )
4644    and    paei.assignment_id = paaf.assignment_id
4645    and    paei.information_type = 'ZA_SPECIFIC_INFO'
4646    and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
4647    and    paei.aei_information7 is not null
4648    AND    hl_cat.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
4649    AND    hl_lev.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
4650    AND    hl_fn.lookup_type = 'ZA_EE_FUNCTION_TYPE'    --Added for Bug 7360563
4651    AND    hl_cat.lookup_code <> '15'
4652    AND    hl_lev.lookup_code <> '15'
4653    AND    hl_fn.lookup_code <>'15'
4654    AND    hl_cat.application_id = '800'
4655    AND    hl_lev.application_id = '800'
4656    AND    hl_fn.application_id  = '800'
4657    AND    hl_cat.meaning(+)       = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
4658    AND    hl_lev.meaning(+)       = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
4659    AND    hl_fn.meaning(+)        = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
4660    and    nvl(paei.aei_information6, 'N') <> 'Y'
4661    order  BY paei.aei_information7, paaf.payroll_id;
4662 
4663 
4664 cursor c_ele_rem(l_assignment_id per_all_assignments_f.assignment_id%type
4665                  ,l_input_id pay_element_entry_values_f.input_value_id%type) is
4666    select peevf.screen_entry_value screen_value,
4667           months_between(least(peevf.effective_end_date ,p_report_date),greatest(peevf.effective_start_date-1 ,add_months(p_report_date,-12))) months_bet
4668    from   pay_element_entry_values_f peevf
4669           ,pay_element_entries_f      peef
4670    where  peef.assignment_id     = l_assignment_id
4671    and    peevf.element_entry_id = peef.element_entry_id
4672    and    peevf.input_value_id   = l_input_id
4673    and    (add_months(p_report_date,-12)+1 <=peef.effective_end_date and p_report_date>=peef.effective_start_date)
4674    and    (add_months(p_report_date,-12)+1 <=peevf.effective_end_date and p_report_date>=peevf.effective_start_date)
4675    and    peef.effective_start_date between peevf.effective_start_date and peevf.effective_end_date;
4676 
4677 l_old_payroll_id      per_all_assignments_f.payroll_id%type := -9999;
4678 v_ele_rem             c_ele_rem%rowtype;
4679 l_rowind              pls_integer;
4680 l_active_days         number;
4681 l_ee_income           number;
4682 l_ee_annual_income    number;
4683 l_report_start        date;
4684 l_report_end          date;
4685 l_report_date         date;
4686 l_difference          number;
4687 l_period_frequency    per_time_period_types.number_per_fiscal_year%type;
4688 l_ee_balance_type_id  pay_balance_types.balance_type_id%type;
4689 l_eea_balance_type_id pay_balance_types.balance_type_id%type;
4690 l_input_value_id      pay_input_values_f.input_value_id%type;
4691 l_index               number;
4692 l_proc                constant varchar2(60) := g_package || 'get_avg_5_lowest_salary';
4693 l_race                per_all_people_f.per_information4%type; -- Bug 4413678
4694 l_sex                 per_all_people_f.sex%type;
4695 --Changes for Bug 7237663
4696 l_er_income           number;
4697 l_er_annual_income    number;
4698 l_er_balance_type_id  pay_balance_types.balance_type_id%type;
4699 l_era_balance_type_id pay_balance_types.balance_type_id%type;
4700 
4701 
4702 begin
4703    --hr_utility.trace_on(null,'ZAEID');
4704    reset_tables;
4705    hr_utility.set_location('Entering ' || l_proc, 10);
4706 
4707    -- Determine whether we need to populate the cache tables
4708    -- Note: No check is made for the validity of the table data, since it is assumed that the
4709    --       reset_tables procedure was called before this procedure.
4710    if g_assignments_table.count = 0 then
4711 
4712       hr_utility.set_location('Setup assignments cache', 20);
4713       g_assignments_table.delete;
4714 
4715       if p_salary_method = 'BAL' then
4716 
4717          -- Get the balance type id's for the normal and annual balances
4718          begin
4719 
4720             select balance_type_id
4721             into   l_ee_balance_type_id
4722             from   pay_balance_types
4723             where  balance_name = 'Total Employment Equityable Income'
4724             and    legislation_code = 'ZA'
4725             and    business_group_id is null;
4726 
4727             select balance_type_id
4728             into   l_eea_balance_type_id
4729             from   pay_balance_types
4730             where  balance_name = 'Total Employment Equityable Annual Income'
4731             and    legislation_code = 'ZA'
4732             and    business_group_id is null;
4733 
4734           --Changes for Bug 7237663
4735             select balance_type_id
4736             into   l_er_balance_type_id
4737             from   pay_balance_types
4738             where  balance_name = 'Total Employment Equityable ER Normal Contributions'
4739             and    legislation_code = 'ZA'
4740             and    business_group_id is null;
4741 
4742             select balance_type_id
4743             into   l_era_balance_type_id
4744             from   pay_balance_types
4745             where  balance_name = 'Total Employment Equityable ER Annual Contributions'
4746             and    legislation_code = 'ZA'
4747             and    business_group_id is null;
4748            --End changes for Bug 7237663
4749 
4750          exception
4751             when no_data_found then
4752                raise_application_error(-20000, 'The Employment Equitable balances do not exist.');
4753 
4754          end;
4755 
4756       end if;
4757 
4758       if p_salary_method = 'ELE' then
4759 
4760          -- Get the ZA Employment Equity Remuneration element details
4761          begin
4762 
4763             select pivf.input_value_id
4764             into   l_input_value_id
4765             from   pay_input_values_f         pivf,
4766                    pay_element_types_f        petf
4767             where  petf.element_name = 'ZA Employment Equity Remuneration'
4768             and    petf.business_group_id is null
4769             and    petf.legislation_code = 'ZA'
4770             and    p_report_date between petf.effective_start_date and petf.effective_end_date
4771             and    pivf.element_type_id = petf.element_type_id
4772             and    pivf.name = 'Remuneration'
4773             and    p_report_date between pivf.effective_start_date and pivf.effective_end_date;
4774 
4775          exception
4776             when no_data_found then
4777                raise_application_error(-20004, 'The ZA Employment Equity Remuneration element does not exist.');
4778 
4779          end;
4780 
4781       end if;
4782 
4783       -- Loop through the assignments cursor and populate the assignments table
4784       for l_assignment in c_assignments loop
4785 
4786          hr_utility.set_location('ASG ' || l_assignment.assignment_id, 21);
4787 
4788          -- Bug 4413678: Begin
4789          Select per_information4, papf.sex
4790          into l_race, l_sex
4791          From per_all_people_f papf
4792          Where papf.person_id = l_assignment.person_id
4793                and p_report_date between papf.effective_start_date and papf.effective_end_date;
4794          -- Bug 4413678: End
4795 
4796 
4797          if l_assignment.payroll_id is not null and l_race <> 'N' then -- Bug 4413678: Added l_race <> 'Not Used'
4798 -- added RPAHUNE
4799             g_assignments_table(l_assignment.assignment_id).payroll_id            := l_assignment.payroll_id;
4800             g_assignments_table(l_assignment.assignment_id).legal_entity_id       := l_assignment.aei_information7;
4801             g_assignments_table(l_assignment.assignment_id).occupational_level    := l_assignment.occupational_level;
4802             g_assignments_table(l_assignment.assignment_id).occupational_category := l_assignment.occupational_category;
4803             g_assignments_table(l_assignment.assignment_id).occupational_category_ID := l_assignment.occupational_category_id;
4804             g_assignments_table(l_assignment.assignment_id).occupational_level_id    := l_assignment.occupational_level_id;
4805             g_assignments_table(l_assignment.assignment_id).race                  := l_race;
4806             g_assignments_table(l_assignment.assignment_id).sex                   := l_sex;
4807             hr_utility.set_location('LEGENT ' || l_assignment.aei_information7, 22);
4808 
4809             -- Check for a new payroll_id and cache the new payroll details in the payrolls table
4810             if l_assignment.payroll_id <> l_old_payroll_id then
4811 
4812                /* Bug 8911880
4813                -- Get the start date and end date of the report
4814                begin
4815 
4816 
4817                   l_report_date := p_report_date;
4818                   l_difference := 0;
4819 
4820                   while (l_difference < 355 or l_difference > 375) loop
4821 
4822                      select ptpf.end_date + 1,
4823                             ptpl.end_date
4824                      into   l_report_start,
4825                             l_report_end
4826                      from   per_time_periods ptpf,
4827                             per_time_periods ptpl
4828                      where  ptpl.payroll_id = l_assignment.payroll_id
4829                      and    l_report_date between ptpl.start_date and ptpl.end_date
4830                      and    ptpf.payroll_id = l_assignment.payroll_id
4831                      and    add_months(l_report_date, -12) + 1 between ptpf.start_date and ptpf.end_date;
4832 
4833                      l_difference := l_report_end - l_report_start + 1;
4834 
4835                      if (l_difference < 355 or l_difference > 375) then
4836 
4837                         l_report_date := l_report_date - 1;
4838 
4839                      end if;
4840 
4841                   end loop;
4842 
4843                exception
4844                   when no_data_found then
4845                      begin
4846                              select ptpl.end_date
4847                              into   l_report_end
4848                              from   per_time_periods ptpl
4849                              where  ptpl.payroll_id = l_assignment.payroll_id
4850                              and    p_report_date between ptpl.start_date and ptpl.end_date;
4851 
4852                      exception
4853                              when no_data_found then
4854                                       Null;
4855                      end;
4856 
4857                      l_report_start := add_months(l_report_end, -12) + 1;
4858 
4859                end;
4860                */
4861 
4862                /* Bug 8911880
4863                -- Get the payroll period frequency
4864                begin
4865 
4866                   select ptpt.number_per_fiscal_year
4867                   into   l_period_frequency
4868                   from   per_time_period_types ptpt,
4869                          pay_all_payrolls_f    payr
4870                   where  payr.payroll_id = l_assignment.payroll_id
4871                   and    p_report_date between payr.effective_start_date and payr.effective_end_date
4872                   and    ptpt.period_type = payr.period_type;
4873 
4874                exception
4875                   when no_data_found then
4876                      raise_application_error(-20005, 'The Payroll Period Frequency does not exist.');
4877 
4878                end;
4879                */
4880 
4881                l_old_payroll_id := l_assignment.payroll_id;
4882 
4883             end if;
4884 
4885 --            hr_utility.set_location('REP_START ' || to_char(l_report_start, 'DD\MM\YYYY'), 22);
4886 --            hr_utility.set_location('REP_END   ' || to_char(l_report_end, 'DD\MM\YYYY'), 23);
4887 --            hr_utility.set_location('FREQ      ' || l_period_frequency, 24);
4888 
4889             if p_salary_method = 'BAL' then
4890 
4891                /* Bug 8911880
4892                -- Get the amount of days the assignment status was Active Assignment
4893                l_active_days := get_active_days
4894                                 (
4895                                    p_assignment_id => l_assignment.assignment_id,
4896                                    p_report_start  => l_report_start,
4897                                    p_report_end    => l_report_end
4898                                 );
4899 
4900                hr_utility.set_location('ACT_DAYS ' || l_active_days, 25);
4901                */
4902 
4903                -- Get the Employment Equitable Income
4904                begin
4905 
4906                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
4907                   into   l_ee_income
4908                   from   pay_balance_feeds_f         pbff,
4909                          pay_run_result_values       prrv,
4910                          pay_run_results             prr,
4911                          pay_payroll_actions         ppa,
4912                          pay_assignment_actions      paa,
4913                          per_assignments_f       asg     --Bug 4872110
4914                   -- BUG 2665394 ADDED THE TABLE TO IMPROVE THE PERFORMANCE
4915                   where  paa.assignment_id = l_assignment.assignment_id
4916                   and    ppa.payroll_action_id = paa.payroll_action_id
4917                 --  Bug 8911880
4918                 --  and    ppa.date_earned between l_report_start and l_report_end
4919                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
4920                   and    prr.assignment_action_id = paa.assignment_action_id
4921                   and    prrv.run_result_id = prr.run_result_id
4922                   and    pbff.balance_type_id = l_ee_balance_type_id
4923                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
4924                   and    prrv.input_value_id = pbff.input_value_id
4925                   -- BUG 2665394 ADDED THE JOINS TO IMPROVE THE PERFORMANCE
4926                   and    paa.assignment_id = asg.assignment_id
4927                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
4928                   and    asg.payroll_id = ppa.payroll_id;
4929 
4930                exception
4931                   when no_data_found then
4932                      l_ee_income := 0;
4933 
4934                end;
4935 
4936                -- Get the Employment Equitable Annual Income
4937                begin
4938 
4939                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
4940                   into   l_ee_annual_income
4941                   from   pay_balance_feeds_f         pbff,
4942                          pay_run_result_values       prrv,
4943                          pay_run_results             prr,
4944                          pay_payroll_actions         ppa,
4945                          pay_assignment_actions      paa,
4946                          per_assignments_f       asg     --Bug 4872110
4947                   -- BUG 2665394 ADDED THE TABLE TO IMPROVE THE PERFORMANCE
4948                   where  paa.assignment_id = l_assignment.assignment_id
4949                   and    ppa.payroll_action_id = paa.payroll_action_id
4950                 --  Bug 8911880
4951                 --  and    ppa.date_earned between l_report_start and l_report_end
4952                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
4953                   and    prr.assignment_action_id = paa.assignment_action_id
4954                   and    prrv.run_result_id = prr.run_result_id
4955                   and    pbff.balance_type_id = l_eea_balance_type_id
4956                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
4957                   and    prrv.input_value_id = pbff.input_value_id
4958                   -- BUG 2665394 ADDED THE JOINS TO IMPROVE THE PERFORMANCE
4959                   and    paa.assignment_id = asg.assignment_id
4960                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
4961                   and    asg.payroll_id = ppa.payroll_id;
4962 
4963                exception
4964                   when no_data_found then
4965                      l_ee_annual_income := 0;
4966 
4967                end;
4968 
4969                --Changes for Bug 7237663
4970                -- Two new balances added for employer contributions
4971                -- Get the Employment Equitable ER Income
4972                begin
4973 
4974                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
4975                   into   l_er_income
4976                   from   pay_balance_feeds_f         pbff,
4977                          pay_run_result_values       prrv,
4978                          pay_run_results             prr,
4979                          pay_payroll_actions         ppa,
4980                          pay_assignment_actions      paa,
4981                          per_assignments_f       asg
4982                   where  paa.assignment_id = l_assignment.assignment_id
4983                   and    ppa.payroll_action_id = paa.payroll_action_id
4984                 --  Bug 8911880
4985                 --  and    ppa.date_earned between l_report_start and l_report_end
4986                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
4987                   and    prr.assignment_action_id = paa.assignment_action_id
4988                   and    prrv.run_result_id = prr.run_result_id
4989                   and    pbff.balance_type_id = l_er_balance_type_id
4990                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
4991                   and    prrv.input_value_id = pbff.input_value_id
4992                   and    paa.assignment_id = asg.assignment_id
4993                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
4994                   and    asg.payroll_id = ppa.payroll_id;
4995 
4996                exception
4997                   when no_data_found then
4998                      l_er_income := 0;
4999 
5000                end;
5001 
5002                -- Get the Employment Equitable ER Annual Income
5003                begin
5004 
5005                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
5006                   into   l_er_annual_income
5007                   from   pay_balance_feeds_f         pbff,
5008                          pay_run_result_values       prrv,
5009                          pay_run_results             prr,
5010                          pay_payroll_actions         ppa,
5011                          pay_assignment_actions      paa,
5012                          per_assignments_f       asg     --Bug 4872110
5013                   where  paa.assignment_id = l_assignment.assignment_id
5014                   and    ppa.payroll_action_id = paa.payroll_action_id
5015                 --  Bug 8911880
5016                 --  and    ppa.date_earned between l_report_start and l_report_end
5017                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
5018                   and    prr.assignment_action_id = paa.assignment_action_id
5019                   and    prrv.run_result_id = prr.run_result_id
5020                   and    pbff.balance_type_id = l_era_balance_type_id
5021                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
5022                   and    prrv.input_value_id = pbff.input_value_id
5023                   and    paa.assignment_id = asg.assignment_id
5024                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
5025                   and    asg.payroll_id = ppa.payroll_id;
5026 
5027                exception
5028                   when no_data_found then
5029                      l_er_annual_income := 0;
5030 
5031                end;
5032 
5033 
5034                hr_utility.set_location('EE_INC ' || l_ee_income, 26);
5035                hr_utility.set_location('EE_ANN ' || l_ee_annual_income, 27);
5036                hr_utility.set_location('ER_INC ' || l_er_income, 26);
5037                hr_utility.set_location('ER_ANN ' || l_er_annual_income, 27);
5038 
5039            --    hr_utility.set_location('STminEND ' || (l_report_end - l_report_start + 1), 28);
5040 
5041                -- Calculate the annual income = annualize normal income + annual income
5042                g_assignments_table(l_assignment.assignment_id).annual_income :=
5043                    l_ee_income + l_er_income + l_ee_annual_income + l_er_annual_income;
5044 
5045                hr_utility.set_location('ANSWER ' || g_assignments_table(l_assignment.assignment_id).annual_income, 29);
5046 
5047             elsif p_salary_method = 'SAL' then
5048 
5049                -- Get the annual salary basis for the current period
5050                begin
5051 
5052                         select input_value_id
5053                         into l_input_value_id
5054                         from per_pay_bases ppb
5055                         where ppb.pay_basis_id = l_assignment.pay_basis_id;
5056 
5057 
5058                         select nvl(sum(fnd_number.canonical_to_number(prrv.result_value)), 0)
5059                         into   g_assignments_table(l_assignment.assignment_id).annual_income
5060                         from   pay_run_result_values       prrv,
5061                                pay_run_results             prr,
5062                                pay_payroll_actions         ppa,
5063                                pay_assignment_actions      paa,
5064                                per_assignments_f       asg
5065                         where  paa.assignment_id = l_assignment.assignment_id
5066                         and    ppa.payroll_action_id = paa.payroll_action_id
5067                         and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
5068                         and    prr.assignment_action_id = paa.assignment_action_id
5069                         and    prrv.run_result_id  =  prr.run_result_id
5070                         and    prrv.input_value_id = l_input_value_id
5071                         and    paa.assignment_id   = asg.assignment_id
5072                         and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
5073                         and    asg.payroll_id = ppa.payroll_id;
5074 
5075                exception
5076                   when no_data_found then
5077                      g_assignments_table(l_assignment.assignment_id).annual_income := 0;
5078 
5079                end;
5080 
5081             elsif p_salary_method = 'ELE' then
5082 
5083                begin
5084                   g_assignments_table(l_assignment.assignment_id).annual_income:=0;
5085                   open c_ele_rem(l_assignment.assignment_id,l_input_value_id);
5086                   loop
5087                       fetch c_ele_rem into v_ele_rem;
5088                       if c_ele_rem%rowcount=0 then
5089                           g_assignments_table(l_assignment.assignment_id).annual_income:= 0;
5090                       end if;
5091                       exit when c_ele_rem%notfound;
5092                       g_assignments_table(l_assignment.assignment_id).annual_income:=
5093                              g_assignments_table(l_assignment.assignment_id).annual_income + (v_ele_rem.screen_value * v_ele_rem.months_bet);
5094                   end loop;
5095                   close c_ele_rem;
5096                end;
5097 
5098             end if;   -- p_salary_method
5099 
5100          end if;   -- (l_assignment.payroll_id is not null)
5101 
5102       end loop;   -- c_assignments
5103 
5104    end if;   -- g_assignments_table.count = 0
5105 
5106    -- The index is calculted by multiplying the legal entity id by 100 and then adding the lookup code
5107    -- This should always give a unique value, since the lookup code is less than 100
5108 -- Start of adding for Employment Equity Report Enhancement Inserting values in table.
5109    l_rowind := g_assignments_table.first;
5110    hr_utility.set_location ('l_rowind :=' || l_rowind, 20);
5111    loop
5112       exit when l_rowind is null;
5113 
5114    hr_utility.set_location ('g_assignments_table(l_rowind).legal_entity_id' ||g_assignments_table(l_rowind).legal_entity_id, 20);
5115    hr_utility.set_location ('l_rowind :=' || g_assignments_table(l_rowind).occupational_level, 20);
5116    hr_utility.set_location ('l_rowind :=' || g_assignments_table(l_rowind).occupational_category, 20);
5117    hr_utility.set_location ('l_rowind :=' || g_assignments_table(l_rowind).race, 20);
5118    hr_utility.set_location ('l_rowind :=' || g_assignments_table(l_rowind).sex, 20);
5119    hr_utility.set_location ('l_rowind :=' || g_assignments_table(l_rowind).annual_income, 20);
5120 
5121       cat_lev_data( g_assignments_table(l_rowind).legal_entity_id
5122                   , g_assignments_table(l_rowind).occupational_level
5123                   , g_assignments_table(l_rowind).occupational_category
5124                   , g_assignments_table(l_rowind).race
5125                   , g_assignments_table(l_rowind).sex
5126                   , nvl(g_assignments_table(l_rowind).annual_income,0)
5127                   , g_assignments_table(l_rowind).occupational_level_id
5128                   , g_assignments_table(l_rowind).occupational_category_ID
5129                   );
5130 
5131             l_rowind := g_assignments_table.next(l_rowind);
5132    END loop;
5133 
5134    l_rowind := g_cat_Enc_Diff_table.first;
5135    loop
5136       exit when l_rowind is null;
5137       INSERT INTO per_za_employment_equity
5138       (
5139          report_id,
5140          reporting_date,
5141          business_group_id,
5142          legal_entity_id,
5143          legal_entity,
5144          disability,
5145          employment_type,
5146          level_cat_code,
5147          level_cat,
5148          ma,
5149          mc,
5150          mi,
5151          mw,
5152          fa,
5153          fc,
5154          fi,
5155          fw,
5156          total
5157       )
5158        Select 'ED1'  -- no of employees in each categories
5159       , p_report_date
5160       , p_business_group_id
5161       , g_cat_Enc_Diff_table(l_rowind).legal_entity_id
5162       , haou.name
5163       , null
5164       , null
5165       , g_cat_Enc_Diff_table(l_rowind).occupational_code_id
5166       , g_cat_Enc_Diff_table(l_rowind).occupational_code
5167       , g_cat_Enc_Diff_table(l_rowind).ma
5168       , g_cat_Enc_Diff_table(l_rowind).mc
5169       , g_cat_Enc_Diff_table(l_rowind).mi
5170       , g_cat_Enc_Diff_table(l_rowind).mw
5171       , g_cat_Enc_Diff_table(l_rowind).fa
5172       , g_cat_Enc_Diff_table(l_rowind).fc
5173       , g_cat_Enc_Diff_table(l_rowind).fi
5174       , g_cat_Enc_Diff_table(l_rowind).fw
5175       , g_cat_Enc_Diff_table(l_rowind).total
5176       FROM hr_all_organization_units haou
5177       Where  haou.organization_id = g_cat_Enc_Diff_table(l_rowind).legal_entity_id;
5178 
5179 
5180       INSERT INTO per_za_employment_equity
5181       (
5182          report_id,
5183          reporting_date,
5184          business_group_id,
5185          legal_entity_id,
5186          legal_entity,
5187          disability,
5188          employment_type,
5189          level_cat_code,
5190          level_cat,
5191          ma,
5192          mc,
5193          mi,
5194          mw,
5195          fa,
5196          fc,
5197          fi,
5198          fw,
5199          total
5200       )
5201       Select 'ED1I'  -- income
5202       , p_report_date
5203       , p_business_group_id
5204       , g_cat_Enc_Diff_table(l_rowind).legal_entity_id
5205       , haou.name
5206       , null
5207       , null
5208       , g_cat_Enc_Diff_table(l_rowind).occupational_code_id
5209       , g_cat_Enc_Diff_table(l_rowind).occupational_code
5210       , g_cat_Enc_Diff_table(l_rowind).ma_inc
5211       , g_cat_Enc_Diff_table(l_rowind).mc_inc
5212       , g_cat_Enc_Diff_table(l_rowind).mi_inc
5213       , g_cat_Enc_Diff_table(l_rowind).mw_inc
5214       , g_cat_Enc_Diff_table(l_rowind).fa_inc
5215       , g_cat_Enc_Diff_table(l_rowind).fc_inc
5216       , g_cat_Enc_Diff_table(l_rowind).fi_inc
5217       , g_cat_Enc_Diff_table(l_rowind).fw_inc
5218       , g_cat_Enc_Diff_table(l_rowind).total_inc
5219       FROM hr_all_organization_units haou
5220       Where  haou.organization_id = g_cat_Enc_Diff_table(l_rowind).legal_entity_id;
5221 
5222      l_rowind := g_cat_Enc_Diff_table.next(l_rowind);
5223    END loop;
5224 
5225    l_rowind := g_lev_Enc_Diff_table.first;
5226    loop
5227       exit when l_rowind is null;
5228       INSERT INTO per_za_employment_equity
5229       (
5230          report_id,
5231          reporting_date,
5232          business_group_id,
5233          legal_entity_id,
5234          legal_entity,
5235          disability,
5236          employment_type,
5237          level_cat_code,
5238          level_cat,
5239          ma,
5240          mc,
5241          mi,
5242          mw,
5243          fa,
5244          fc,
5245          fi,
5246          fw,
5247          total
5248       )
5249       Select 'ED2'  -- no of employees in each categories
5250       , p_report_date
5251       , p_business_group_id
5252       , g_lev_Enc_Diff_table(l_rowind).legal_entity_id
5253       , haou.name        legal_entity
5254       , null
5255       , null
5256       , g_lev_Enc_Diff_table(l_rowind).occupational_code_id
5257       , g_lev_Enc_Diff_table(l_rowind).occupational_code
5258       , g_lev_Enc_Diff_table(l_rowind).ma
5259       , g_lev_Enc_Diff_table(l_rowind).mc
5260       , g_lev_Enc_Diff_table(l_rowind).mi
5261       , g_lev_Enc_Diff_table(l_rowind).mw
5262       , g_lev_Enc_Diff_table(l_rowind).fa
5263       , g_lev_Enc_Diff_table(l_rowind).fc
5264       , g_lev_Enc_Diff_table(l_rowind).fi
5265       , g_lev_Enc_Diff_table(l_rowind).fw
5266       , g_lev_Enc_Diff_table(l_rowind).total
5267       FROM hr_all_organization_units haou
5268       Where  haou.organization_id = g_lev_Enc_Diff_table(l_rowind).legal_entity_id;
5269 
5270       INSERT INTO per_za_employment_equity
5271       (
5272          report_id,
5273          reporting_date,
5274          business_group_id,
5275          legal_entity_id,
5276          legal_entity,
5277          disability,
5278          employment_type,
5279          level_cat_code,
5280          level_cat,
5281          ma,
5282          mc,
5283          mi,
5284          mw,
5285          fa,
5286          fc,
5287          fi,
5288          fw,
5289          total
5290       )
5291       Select 'ED2I'  -- income
5292       , p_report_date
5293       , p_business_group_id
5294       , g_lev_Enc_Diff_table(l_rowind).legal_entity_id
5295       , haou.name        legal_entity
5296       , null
5297       , null
5298       , g_lev_Enc_Diff_table(l_rowind).occupational_code_id
5299       , g_lev_Enc_Diff_table(l_rowind).occupational_code
5300       , g_lev_Enc_Diff_table(l_rowind).ma_inc
5301       , g_lev_Enc_Diff_table(l_rowind).mc_inc
5302       , g_lev_Enc_Diff_table(l_rowind).mi_inc
5303       , g_lev_Enc_Diff_table(l_rowind).mw_inc
5304       , g_lev_Enc_Diff_table(l_rowind).fa_inc
5305       , g_lev_Enc_Diff_table(l_rowind).fc_inc
5306       , g_lev_Enc_Diff_table(l_rowind).fi_inc
5307       , g_lev_Enc_Diff_table(l_rowind).fw_inc
5308       , g_lev_Enc_Diff_table(l_rowind).total_inc
5309       FROM hr_all_organization_units haou
5310       Where  haou.organization_id = g_lev_Enc_Diff_table(l_rowind).legal_entity_id;
5311 
5312      l_rowind := g_lev_Enc_Diff_table.next(l_rowind);
5313    END loop;
5314 --hr_utility.trace_off;
5315 
5316       -- Inserts non-associated occupational categories with zero values for no of employees
5317       insert into per_za_employment_equity
5318       (
5319          report_id,
5320          reporting_date,
5321          business_group_id,
5322          legal_entity_id,
5323          legal_entity,
5324          disability,
5325          employment_type,
5326          level_cat_code,
5327          level_cat,
5328          MA,
5329          MC,
5330          MI,
5331          MW,
5332          FA,
5333          FC,
5334          FI,
5335          FW,
5336          total
5337       )
5338       select 'ED1'            report_id,
5339              p_report_date    reporting_date,
5340              p_business_group_id business_group_id,
5341              haou.organization_id legal_entity_id,
5342              haou.name        legal_entity,
5343              Null              disability,
5344              Null      employment_type,
5345              hl.lookup_code   level_cat_code,
5346              hl.meaning       ,
5347              0                MA,
5348              0                MC,
5349              0                MI,
5350              0                MW,
5351              0                FA,
5352              0                FC,
5353              0                FI,
5354              0                FW,
5355              0                total
5356       from   hr_lookups hl
5357          ,   hr_all_organization_units haou
5358       where not exists
5359       (
5360          select 'X'
5361          from   per_za_employment_equity pzee
5362          where  pzee.level_cat_code    = hl.lookup_code
5363          and    pzee.report_id         = 'ED1'
5364          and    pzee.business_group_id = p_business_group_id           --Bug 4872110
5365          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
5366       )
5367       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
5368       and haou.business_group_id = p_business_group_id          --Bug 4872110
5369       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
5370 
5371       -- Inserts non-associated occupational categories with zero values for no of employees
5372       insert into per_za_employment_equity
5373       (
5374          report_id,
5375          reporting_date,
5376          business_group_id,
5377          legal_entity_id,
5378          legal_entity,
5379          disability,
5380          employment_type,
5381          level_cat_code,
5382          level_cat,
5383          MA,
5384          MC,
5385          MI,
5386          MW,
5387          FA,
5388          FC,
5389          FI,
5390          FW,
5391          total
5392       )
5393       select 'ED1I'            report_id,
5394              p_report_date    reporting_date,
5395              p_business_group_id business_group_id,
5396              haou.organization_id legal_entity_id,
5397              haou.name        legal_entity,
5398              Null              disability,
5399              Null      employment_type,
5400              hl.lookup_code   level_cat_code,
5401              hl.meaning,
5402              0                MA,
5403              0                MC,
5404              0                MI,
5405              0                MW,
5406              0                FA,
5407              0                FC,
5408              0                FI,
5409              0                FW,
5410              0                total
5411       from   hr_lookups hl
5412          ,   hr_all_organization_units haou
5413       where not exists
5414       (
5415          select 'X'
5416          from   per_za_employment_equity pzee
5417          where  pzee.level_cat_code    = hl.lookup_code
5418          and    pzee.report_id         = 'ED1I'
5419          and    pzee.business_group_id = p_business_group_id           --Bug 4872110
5420          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
5421       )
5422       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
5423       and haou.business_group_id = p_business_group_id          --Bug 4872110
5424       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
5425 
5426 -- inserting 0 values for the no of employees
5427       insert into per_za_employment_equity
5428       (
5429          report_id,
5430          reporting_date,
5431          business_group_id,
5432          legal_entity_id,
5433          legal_entity,
5434          disability,
5435          employment_type,
5436          level_cat_code,
5437          level_cat,
5438          MA,
5439          MC,
5440          MI,
5441          MW,
5442          FA,
5443          FC,
5444          FI,
5445          FW,
5446          total
5447       )
5448       select 'ED2'            report_id,
5449              p_report_date    reporting_date,
5450              p_business_group_id business_group_id,
5451              haou.organization_id legal_entity_id,
5452              haou.name        legal_entity,
5453              null              disability,
5454              null      employment_type,
5455              hl.lookup_code   level_cat_code,
5456              hl.meaning,
5457              0                MA,
5458              0                MC,
5459              0                MI,
5460              0                MW,
5461              0                FA,
5462              0                FC,
5463              0                FI,
5464              0                FW,
5465              0                total
5466       from   hr_lookups hl
5467          ,   hr_all_organization_units haou
5468       where not exists
5469       (
5470          select 'X'
5471          from   per_za_employment_equity pzee
5472          where  pzee.level_cat_code    = hl.lookup_code
5473          and    pzee.report_id         = 'ED2'
5474          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
5475          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
5476       )
5477       and hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
5478       and haou.business_group_id = p_business_group_id          --Bug 4872110
5479       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
5480 
5481 -- inserting 0 values for the Income
5482       insert into per_za_employment_equity
5483       (
5484          report_id,
5485          reporting_date,
5486          business_group_id,
5487          legal_entity_id,
5488          legal_entity,
5489          disability,
5490          employment_type,
5491          level_cat_code,
5492          level_cat,
5493          MA,
5494          MC,
5495          MI,
5496          MW,
5497          FA,
5498          FC,
5499          FI,
5500          FW,
5501          total
5502       )
5503       select 'ED2I'            report_id,
5504              p_report_date    reporting_date,
5505              p_business_group_id business_group_id,
5506              haou.organization_id legal_entity_id,
5507              haou.name        legal_entity,
5508              null              disability,
5509              null      employment_type,
5510              hl.lookup_code   level_cat_code,
5511              hl.meaning               ,
5512              0                MA,
5513              0                MC,
5514              0                MI,
5515              0                MW,
5516              0                FA,
5517              0                FC,
5518              0                FI,
5519              0                FW,
5520              0                total
5521       from   hr_lookups hl
5522          ,   hr_all_organization_units haou
5523       where not exists
5524       (
5525          select 'X'
5526          from   per_za_employment_equity pzee
5527          where  pzee.level_cat_code    = hl.lookup_code
5528          and    pzee.report_id         = 'ED2I'
5529          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
5530          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
5531       )
5532       and hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
5533       and haou.business_group_id = p_business_group_id          --Bug 4872110
5534       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
5535 
5536 commit;
5537 
5538 end init_g_cat_lev_table;
5539 
5540 
5541 --Reset the structures for Employment Equity Differential from reporting year 2010
5542 procedure reset_new_tables is
5543 
5544 l_proc constant varchar2(60) := g_package || 'reset_new_tables';
5545 
5546 begin
5547 
5548    -- hr_utility.trace_on(null, 'T');
5549    g_new_assignments_table.delete;
5550    g_all_high_avg := -9999;
5551    g_all_low_avg  := -9999;
5552    g_cat_averages_table.delete;
5553    g_lev_averages_table.delete;
5554    g_cat_Enc_Diff_table.delete;
5555    g_lev_Enc_Diff_table.delete;
5556    g_lev_Enc_Diff_table_F.delete;
5557    g_lev_Enc_Diff_table_T.delete;
5558    g_lev_Enc_Diff_table_TF.delete;
5559 
5560    DELETE FROM per_za_employment_equity
5561     Where report_id IN
5562                    ( 'ED', 'EDI', 'EDF','EDFI');
5563 
5564 end reset_new_tables;
5565 
5566 --Procedure to insert data into global tables for Employment Equity Differential report from reporting year 2009
5567 PROCEDURE ins_g_Enc_Diff_table(p_mi_inc     IN number
5568                              , p_mc_inc     IN number
5569                              , p_ma_inc     IN number
5570                              , p_mw_inc     IN number
5571                              , p_fa_inc     IN number
5572                              , p_fc_inc     IN number
5573                              , p_fi_inc     IN number
5574                              , p_fw_inc     IN number
5575                              , p_total_inc  IN number
5576                              , p_ma         IN number
5577                              , p_mc         IN number
5578                              , p_mi         IN number
5579                              , p_mw         IN number
5580                              , p_fa         IN number
5581                              , p_fc         IN number
5582                              , p_fi         IN number
5583                              , p_fw         IN number
5584                              , p_total      IN number
5585                              , p_cat_index  IN number
5586                              , p_lev_index  IN number
5587                              , p_legal_entity_id       IN hr_all_organization_units.organization_id%type
5588                              , p_occupational_level IN hr_lookups.meaning%type
5589                              , p_occupational_category IN hr_lookups.meaning%type
5590                              , p_occupational_level_id IN hr_lookups.lookup_code%type
5591                              , p_occupational_category_id IN hr_lookups.lookup_code%type
5592                              , p_table IN OUT nocopy t_E_differential
5593                               ) is
5594 
5595 begin
5596 
5597           hr_utility.set_location('Entered ins_g_Enc_Diff_table',10);
5598 
5599             --Occupational levels
5600             IF p_table.EXISTS(p_lev_index) then
5601                p_table(p_lev_index).mi_inc    :=  p_table(p_lev_index).mi_inc    + p_mi_inc ;
5602                p_table(p_lev_index).mc_inc    :=  p_table(p_lev_index).mc_inc    + p_mc_inc ;
5603                p_table(p_lev_index).ma_inc    :=  p_table(p_lev_index).ma_inc    + p_ma_inc ;
5604                p_table(p_lev_index).mw_inc    :=  p_table(p_lev_index).mw_inc    + p_mw_inc ;
5605                p_table(p_lev_index).fa_inc    :=  p_table(p_lev_index).fa_inc    + p_fa_inc ;
5606                p_table(p_lev_index).fc_inc    :=  p_table(p_lev_index).fc_inc    + p_fc_inc ;
5607                p_table(p_lev_index).fi_inc    :=  p_table(p_lev_index).fi_inc    + p_fi_inc ;
5608                p_table(p_lev_index).fw_inc    :=  p_table(p_lev_index).fw_inc    + p_fw_inc ;
5609                p_table(p_lev_index).total_inc :=  p_table(p_lev_index).total_inc + p_total_inc;
5610 
5611                p_table(p_lev_index).ma    := p_table(p_lev_index).ma    +  p_ma ;
5612                p_table(p_lev_index).mc    := p_table(p_lev_index).mc    +  p_mc ;
5613                p_table(p_lev_index).mi    := p_table(p_lev_index).mi    +  p_mi ;
5614                p_table(p_lev_index).mw    := p_table(p_lev_index).mw    +  p_mw ;
5615                p_table(p_lev_index).fa    := p_table(p_lev_index).fa    +  p_fa ;
5616                p_table(p_lev_index).fc    := p_table(p_lev_index).fc    +  p_fc ;
5617                p_table(p_lev_index).fi    := p_table(p_lev_index).fi    +  p_fi ;
5618                p_table(p_lev_index).fw    := p_table(p_lev_index).fw    +  p_fw ;
5619                p_table(p_lev_index).total := p_table(p_lev_index).total +  p_total;
5620             else
5621                p_table(p_lev_index).mi_inc    :=  p_mi_inc ;
5622                p_table(p_lev_index).mc_inc    :=  p_mc_inc ;
5623                p_table(p_lev_index).ma_inc    :=  p_ma_inc ;
5624                p_table(p_lev_index).mw_inc    :=  p_mw_inc ;
5625                p_table(p_lev_index).fa_inc    :=  p_fa_inc ;
5626                p_table(p_lev_index).fc_inc    :=  p_fc_inc ;
5627                p_table(p_lev_index).fi_inc    :=  p_fi_inc ;
5628                p_table(p_lev_index).fw_inc    :=  p_fw_inc ;
5629                p_table(p_lev_index).total_inc :=  p_total_inc;
5630 
5631                p_table(p_lev_index).ma    :=  p_ma ;
5632                p_table(p_lev_index).mc    :=  p_mc ;
5633                p_table(p_lev_index).mi    :=  p_mi ;
5634                p_table(p_lev_index).mw    :=  p_mw ;
5635                p_table(p_lev_index).fa    :=  p_fa ;
5636                p_table(p_lev_index).fc    :=  p_fc ;
5637                p_table(p_lev_index).fi    :=  p_fi ;
5638                p_table(p_lev_index).fw    :=  p_fw ;
5639                p_table(p_lev_index).total :=  p_total;
5640                p_table(p_lev_index).legal_entity_id := p_legal_entity_id;
5641                p_table(p_lev_index).occupational_code := p_occupational_level;
5642                p_table(p_lev_index).occupational_code_id := p_occupational_level_id;
5643             End if;
5644 
5645 END ins_g_Enc_Diff_table;
5646 
5647 
5648 Procedure cat_lev_data ( p_legal_entity_id IN hr_all_organization_units.organization_id%type
5649                        , p_occupational_level IN hr_lookups.meaning%type
5650                        , p_occupational_category IN hr_lookups.meaning%type
5651                        , p_race IN per_all_people_f.per_information4%type
5652                        , p_sex IN per_all_people_f.sex%type
5653                        , p_income IN number
5654                        , p_occupational_level_id IN hr_lookups.lookup_code%type
5655                        , p_occupational_category_id IN hr_lookups.lookup_code%type
5656                        , p_table IN OUT nocopy t_E_differential
5657                        ) is
5658 
5659  l_cat_index  pls_integer ;
5660  l_lev_index  pls_integer ;
5661 begin
5662 
5663    begin
5664    hr_utility.set_location('Entered cat_lev_data',25);
5665    l_cat_index := p_legal_entity_id *100 + nvl(p_occupational_category_id,0);
5666    l_lev_index := p_legal_entity_id *100 + nvl(p_occupational_level_id,0);
5667    hr_utility.set_location('l_cat_index ' || l_cat_index, 25);
5668    hr_utility.set_location('l_lev_index ' || l_lev_index, 25);
5669 
5670 
5671    exception
5672       when others then
5673          raise_application_error(-20006, 'The lookup code in the ZA_EMP_EQ_OCCUPATIONAL_LEV and ZA_EMP_EQ_OCCUPATIONAL_CAT lookups must be numeric.');
5674 
5675    end;
5676 
5677      CASE p_sex||p_race
5678           WHEN  'M01' THEN --male Indian (MI)
5679               ins_g_Enc_Diff_table(p_mi_inc  =>  p_income
5680                              , p_mc_inc    => 0
5681                              , p_ma_inc    => 0
5682                              , p_mw_inc    => 0
5683                              , p_fa_inc    => 0
5684                              , p_fc_inc    => 0
5685                              , p_fi_inc    => 0
5686                              , p_fw_inc    => 0
5687                              , p_total_inc => p_income
5688                              , p_ma        => 0
5689                              , p_mc        => 0
5690                              , p_mi        => 1
5691                              , p_mw        => 0
5692                              , p_fa        => 0
5693                              , p_fc        => 0
5694                              , p_fi        => 0
5695                              , p_fw        => 0
5696                              , p_total     => 1
5697                              , p_cat_index => l_cat_index
5698                              , p_lev_index => l_lev_index
5699                              , p_legal_entity_id       => p_legal_entity_id
5700                              , p_occupational_level    => p_occupational_level
5701                              , p_occupational_category => p_occupational_category
5702                              , p_occupational_level_id    => p_occupational_level_id
5703                              , p_occupational_category_id => p_occupational_category_id
5704                              , p_table => p_table
5705                              );
5706           WHEN  'M02' THEN --male African
5707               ins_g_Enc_Diff_table(p_mi_inc  => 0
5708                              , p_mc_inc    => 0
5709                              , p_ma_inc    =>  p_income
5710                              , p_mw_inc    => 0
5711                              , p_fa_inc    => 0
5712                              , p_fc_inc    => 0
5713                              , p_fi_inc    => 0
5714                              , p_fw_inc    => 0
5715                              , p_total_inc => p_income
5716                              , p_ma        => 1
5717                              , p_mc        => 0
5718                              , p_mi        => 0
5719                              , p_mw        => 0
5720                              , p_fa        => 0
5721                              , p_fc        => 0
5722                              , p_fi        => 0
5723                              , p_fw        => 0
5724                              , p_total     => 1
5725                              , p_cat_index => l_cat_index
5726                              , p_lev_index => l_lev_index
5727                              , p_legal_entity_id       => p_legal_entity_id
5728                              , p_occupational_level    => p_occupational_level
5729                              , p_occupational_category => p_occupational_category
5730                              , p_occupational_level_id    => p_occupational_level_id
5731                              , p_occupational_category_id => p_occupational_category_id
5732                              , p_table => p_table
5733                              );
5734           WHEN  'M03' THEN --male Coloured
5735               ins_g_Enc_Diff_table(p_mi_inc  => 0
5736                              , p_mc_inc    =>  p_income
5737                              , p_ma_inc    => 0
5738                              , p_mw_inc    => 0
5739                              , p_fa_inc    => 0
5740                              , p_fc_inc    => 0
5741                              , p_fi_inc    => 0
5742                              , p_fw_inc    => 0
5743                              , p_total_inc => p_income
5744                              , p_ma        => 0
5745                              , p_mc        => 1
5746                              , p_mi        => 0
5747                              , p_mw        => 0
5748                              , p_fa        => 0
5749                              , p_fc        => 0
5750                              , p_fi        => 0
5751                              , p_fw        => 0
5752                              , p_total     => 1
5753                              , p_cat_index => l_cat_index
5754                              , p_lev_index => l_lev_index
5755                              , p_legal_entity_id       => p_legal_entity_id
5756                              , p_occupational_level    => p_occupational_level
5757                              , p_occupational_category => p_occupational_category
5758                              , p_occupational_level_id    => p_occupational_level_id
5759                              , p_occupational_category_id => p_occupational_category_id
5760                              , p_table => p_table
5761                              );
5762           WHEN  'M04' THEN --male White
5763               ins_g_Enc_Diff_table(p_mi_inc  => 0
5764                              , p_mc_inc    => 0
5765                              , p_ma_inc    => 0
5766                              , p_mw_inc    =>  p_income
5767                              , p_fa_inc    => 0
5768                              , p_fc_inc    => 0
5769                              , p_fi_inc    => 0
5770                              , p_fw_inc    => 0
5771                              , p_total_inc => p_income
5772                              , p_ma        => 0
5773                              , p_mc        => 0
5774                              , p_mi        => 0
5775                              , p_mw        => 1
5776                              , p_fa        => 0
5777                              , p_fc        => 0
5778                              , p_fi        => 0
5779                              , p_fw        => 0
5780                              , p_total     => 1
5781                              , p_cat_index => l_cat_index
5782                              , p_lev_index => l_lev_index
5783                              , p_legal_entity_id       => p_legal_entity_id
5784                              , p_occupational_level    => p_occupational_level
5785                              , p_occupational_category => p_occupational_category
5786                              , p_occupational_level_id    => p_occupational_level_id
5787                              , p_occupational_category_id => p_occupational_category_id
5788                              , p_table => p_table
5789                              );
5790           WHEN  'MZA01' THEN --male Chinese (To be reported as African)
5791               ins_g_Enc_Diff_table(p_mi_inc  => 0
5792                              , p_mc_inc    => 0
5793                              , p_ma_inc    =>  p_income
5794                              , p_mw_inc    => 0
5795                              , p_fa_inc    => 0
5796                              , p_fc_inc    => 0
5797                              , p_fi_inc    => 0
5798                              , p_fw_inc    => 0
5799                              , p_total_inc => p_income
5800                              , p_ma        => 1
5801                              , p_mc        => 0
5802                              , p_mi        => 0
5803                              , p_mw        => 0
5804                              , p_fa        => 0
5805                              , p_fc        => 0
5806                              , p_fi        => 0
5807                              , p_fw        => 0
5808                              , p_total     => 1
5809                              , p_cat_index => l_cat_index
5810                              , p_lev_index => l_lev_index
5811                              , p_legal_entity_id       => p_legal_entity_id
5812                              , p_occupational_level    => p_occupational_level
5813                              , p_occupational_category => p_occupational_category
5814                              , p_occupational_level_id    => p_occupational_level_id
5815                              , p_occupational_category_id => p_occupational_category_id
5816                              , p_table => p_table
5817                              );
5818 
5819           WHEN  'F01' THEN --female Indian
5820               ins_g_Enc_Diff_table(p_mi_inc  => 0
5821                              , p_mc_inc    => 0
5822                              , p_ma_inc    => 0
5823                              , p_mw_inc    =>  0
5824                              , p_fa_inc    => 0
5825                              , p_fc_inc    => 0
5826                              , p_fi_inc    =>p_income
5827                              , p_fw_inc    => 0
5828                              , p_total_inc => p_income
5829                              , p_ma        => 0
5830                              , p_mc        => 0
5831                              , p_mi        => 0
5832                              , p_mw        => 0
5833                              , p_fa        => 0
5834                              , p_fc        => 0
5835                              , p_fi        => 1
5836                              , p_fw        => 0
5837                              , p_total     => 1
5838                              , p_cat_index => l_cat_index
5839                              , p_lev_index => l_lev_index
5840                              , p_legal_entity_id       => p_legal_entity_id
5841                              , p_occupational_level    => p_occupational_level
5842                              , p_occupational_category => p_occupational_category
5843                              , p_occupational_level_id    => p_occupational_level_id
5844                              , p_occupational_category_id => p_occupational_category_id
5845                              , p_table => p_table
5846                              );
5847           WHEN  'F02' THEN --female African
5848               ins_g_Enc_Diff_table(p_mi_inc  => 0
5849                              , p_mc_inc    => 0
5850                              , p_ma_inc    => 0
5851                              , p_mw_inc    => 0
5852                              , p_fa_inc    => p_income
5853                              , p_fc_inc    => 0
5854                              , p_fi_inc    => 0
5855                              , p_fw_inc    => 0
5856                              , p_total_inc => p_income
5857                              , p_ma        => 0
5858                              , p_mc        => 0
5859                              , p_mi        => 0
5860                              , p_mw        => 0
5861                              , p_fa        => 1
5862                              , p_fc        => 0
5863                              , p_fi        => 0
5864                              , p_fw        => 0
5865                              , p_total     => 1
5866                              , p_cat_index => l_cat_index
5867                              , p_lev_index => l_lev_index
5868                              , p_legal_entity_id       => p_legal_entity_id
5869                              , p_occupational_level    => p_occupational_level
5870                              , p_occupational_category => p_occupational_category
5871                              , p_occupational_level_id    => p_occupational_level_id
5872                              , p_occupational_category_id => p_occupational_category_id
5873                              , p_table => p_table
5874                              );
5875           WHEN  'F03' THEN --female Coloured
5876               ins_g_Enc_Diff_table(p_mi_inc  => 0
5877                              , p_mc_inc    => 0
5878                              , p_ma_inc    => 0
5879                              , p_mw_inc    => 0
5880                              , p_fa_inc    => 0
5881                              , p_fc_inc    => p_income
5882                              , p_fi_inc    => 0
5883                              , p_fw_inc    => 0
5884                              , p_total_inc => p_income
5885                              , p_ma        => 0
5886                              , p_mc        => 0
5887                              , p_mi        => 0
5888                              , p_mw        => 0
5889                              , p_fa        => 0
5890                              , p_fc        => 1
5891                              , p_fi        => 0
5892                              , p_fw        => 0
5893                              , p_total     => 1
5894                              , p_cat_index => l_cat_index
5895                              , p_lev_index => l_lev_index
5896                              , p_legal_entity_id       => p_legal_entity_id
5897                              , p_occupational_level    => p_occupational_level
5898                              , p_occupational_category => p_occupational_category
5899                              , p_occupational_level_id    => p_occupational_level_id
5900                              , p_occupational_category_id => p_occupational_category_id
5901                              , p_table => p_table
5902                              );
5903           WHEN  'F04' THEN --female White
5904               ins_g_Enc_Diff_table(p_mi_inc  => 0
5905                              , p_mc_inc    => 0
5906                              , p_ma_inc    => 0
5907                              , p_mw_inc    => 0
5908                              , p_fa_inc    => 0
5909                              , p_fc_inc    => 0
5910                              , p_fi_inc    => 0
5911                              , p_fw_inc    => p_income
5912                              , p_total_inc => p_income
5913                              , p_ma        => 0
5914                              , p_mc        => 0
5915                              , p_mi        => 0
5916                              , p_mw        => 0
5917                              , p_fa        => 0
5918                              , p_fc        => 0
5919                              , p_fi        => 0
5920                              , p_fw        => 1
5921                              , p_total     => 1
5922                              , p_cat_index => l_cat_index
5923                              , p_lev_index => l_lev_index
5924                              , p_legal_entity_id       => p_legal_entity_id
5925                              , p_occupational_level    => p_occupational_level
5926                              , p_occupational_category => p_occupational_category
5927                              , p_occupational_level_id    => p_occupational_level_id
5928                              , p_occupational_category_id => p_occupational_category_id
5929                              , p_table => p_table
5930                              );
5931           WHEN  'FZA01' THEN --female Chinese (To be reported as African)
5932               ins_g_Enc_Diff_table(p_mi_inc  => 0
5933                              , p_mc_inc    => 0
5934                              , p_ma_inc    => 0
5935                              , p_mw_inc    => 0
5936                              , p_fa_inc    => p_income
5937                              , p_fc_inc    => 0
5938                              , p_fi_inc    => 0
5939                              , p_fw_inc    => 0
5940                              , p_total_inc => p_income
5941                              , p_ma        => 0
5942                              , p_mc        => 0
5943                              , p_mi        => 0
5944                              , p_mw        => 0
5945                              , p_fa        => 1
5946                              , p_fc        => 0
5947                              , p_fi        => 0
5948                              , p_fw        => 0
5949                              , p_total     => 1
5950                              , p_cat_index => l_cat_index
5951                              , p_lev_index => l_lev_index
5952                              , p_legal_entity_id       => p_legal_entity_id
5953                              , p_occupational_level    => p_occupational_level
5954                              , p_occupational_category => p_occupational_category
5955                              , p_occupational_level_id    => p_occupational_level_id
5956                              , p_occupational_category_id => p_occupational_category_id
5957                              , p_table => p_table
5958                              );
5959              else
5960              null;
5961          END case;
5962 END cat_lev_data;
5963 
5964 
5965 
5966 -- Procedure to initialise with the employee details from reporting year 2009
5967 --
5968 procedure init_g_cat_lev_new_table
5969 (
5970    p_report_date            in per_all_assignments_f.effective_end_date%type,
5971    p_business_group_id      in per_all_assignments_f.business_group_id%type,
5972    p_legal_entity_id        in per_assignment_extra_info.aei_information7%type := null,
5973    p_salary_method          in varchar2  -- SAL = Salary Basis Method, BAL = Payroll Balances Method
5974 )  is
5975 /* commented for Bug 8911880
5976 cursor c_assignments is
5977    select paaf.assignment_id,
5978           paaf.person_id, -- Bug 4413678
5979           paaf.payroll_id,
5980           paei.aei_information7 ,
5981           hl_cat.lookup_code      OCCUPATIONAL_CATEGORY_ID,
5982           hl_lev.lookup_code      OCCUPATIONAL_LEVEL_ID,
5983           per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)    occupational_level,
5984           per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id) occupational_category,
5985           paaf.pay_basis_id
5986    from   per_assignment_extra_info   paei,
5987           per_assignment_status_types past,
5988           per_all_assignments_f       paaf,
5989           hr_lookups                  hl_cat,
5990           hr_lookups                  hl_lev,
5991           hr_lookups                  hl_fn
5992    where  paaf.business_group_id = p_business_group_id
5993    and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
5994    and    past.assignment_status_type_id = paaf.assignment_status_type_id
5995    and    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
5996    and    paei.assignment_id = paaf.assignment_id
5997    and    paei.information_type = 'ZA_SPECIFIC_INFO'
5998    and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
5999    and    paei.aei_information7 is not null
6000    AND    hl_cat.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
6001    AND    hl_lev.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
6002    AND    hl_fn.lookup_type = 'ZA_EE_FUNCTION_TYPE'    --Added for Bug 7360563
6003    AND    hl_cat.lookup_code <> '15'
6004    AND    hl_lev.lookup_code <> '15'
6005    AND    hl_fn.lookup_code <>'15'
6006    AND    hl_cat.application_id = '800'
6007    AND    hl_lev.application_id = '800'
6008    AND    hl_fn.application_id  = '800'
6009    AND    hl_cat.meaning(+)       = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
6010    AND    hl_lev.meaning(+)       = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
6011    AND    hl_fn.meaning(+)        = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
6012    and    nvl(paei.aei_information6, 'N') <> 'Y'
6013    order  BY paei.aei_information7, paaf.payroll_id;
6014 */
6015 
6016 --Added employment type for Year 2009 onwards
6017 cursor c_assignments is
6018    select paaf.assignment_id,
6019           paaf.person_id, -- Bug 4413678
6020           paaf.payroll_id,
6021           paei.aei_information7 ,
6022           hl_lev.lookup_code      OCCUPATIONAL_LEVEL_ID,
6023           per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)    occupational_level,
6024           nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type,
6025           paaf.pay_basis_id
6026    from   per_assignment_extra_info   paei,
6027           per_all_assignments_f       paaf,
6028           hr_lookups                  hl_lev,
6029           hr_lookups                  hl_fn
6030    where  paaf.business_group_id = p_business_group_id
6031    and    (add_months(p_report_date,-12)+1 <=paaf.effective_end_date and p_report_date >=paaf.effective_start_date)
6032    and    paaf.effective_end_date = (  select max(paaf1.effective_end_date)
6033                                        from   per_all_assignments_f paaf1,
6034                                               per_assignment_status_types past
6035                                        where  paaf1.assignment_id = paaf.assignment_id
6036                                        and    paaf1.effective_start_date <= p_report_date
6037                                        and    past.assignment_status_type_id = paaf1.assignment_status_type_id
6038                                        and    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
6039                                      )
6040    and    paei.assignment_id = paaf.assignment_id
6041    and    paei.information_type = 'ZA_SPECIFIC_INFO'
6042    and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
6043    and    paei.aei_information7 is not null
6044    AND    hl_lev.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
6045    AND    hl_fn.lookup_type = 'ZA_EE_FUNCTION_TYPE'    --Added for Bug 7360563
6046    AND    hl_lev.lookup_code <> '15'
6047    AND    hl_fn.lookup_code <>'15'
6048    AND    hl_lev.application_id = '800'
6049    AND    hl_fn.application_id  = '800'
6050    AND    hl_lev.meaning(+)       = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
6051    AND    hl_fn.meaning(+)        = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
6052    and    nvl(paei.aei_information6, 'N') <> 'Y'
6053    order  BY paei.aei_information7, paaf.payroll_id;
6054 
6055 
6056 cursor c_ele_rem(l_assignment_id per_all_assignments_f.assignment_id%type
6057                  ,l_input_id pay_element_entry_values_f.input_value_id%type) is
6058    select peevf.screen_entry_value screen_value,
6059           months_between(least(peevf.effective_end_date ,p_report_date),greatest(peevf.effective_start_date-1 ,add_months(p_report_date,-12))) months_bet
6060    from   pay_element_entry_values_f peevf
6061           ,pay_element_entries_f      peef
6062    where  peef.assignment_id     = l_assignment_id
6063    and    peevf.element_entry_id = peef.element_entry_id
6064    and    peevf.input_value_id   = l_input_id
6065    and    (add_months(p_report_date,-12)+1 <=peef.effective_end_date and p_report_date>=peef.effective_start_date)
6066    and    (add_months(p_report_date,-12)+1 <=peevf.effective_end_date and p_report_date>=peevf.effective_start_date)
6067    and    peef.effective_start_date between peevf.effective_start_date and peevf.effective_end_date;
6068 
6069 l_old_payroll_id      per_all_assignments_f.payroll_id%type := -9999;
6070 v_ele_rem             c_ele_rem%rowtype;
6071 l_rowind              pls_integer;
6072 l_active_days         number;
6073 l_ee_income           number;
6074 l_ee_annual_income    number;
6075 l_report_start        date;
6076 l_report_end          date;
6077 l_report_date         date;
6078 l_difference          number;
6079 l_period_frequency    per_time_period_types.number_per_fiscal_year%type;
6080 l_ee_balance_type_id  pay_balance_types.balance_type_id%type;
6081 l_eea_balance_type_id pay_balance_types.balance_type_id%type;
6082 l_input_value_id      pay_input_values_f.input_value_id%type;
6083 l_input_value_id2     pay_input_values_f.input_value_id%type;
6084 l_index               number;
6085 l_proc                constant varchar2(60) := g_package || 'get_avg_5_lowest_salary';
6086 l_race                per_all_people_f.per_information4%type; -- Bug 4413678
6087 l_sex                 per_all_people_f.sex%type;
6088 --Changes for Bug 7237663
6089 l_er_income           number;
6090 l_er_annual_income    number;
6091 l_er_balance_type_id  pay_balance_types.balance_type_id%type;
6092 l_era_balance_type_id pay_balance_types.balance_type_id%type;
6093 l_type varchar2(5);
6094 
6095 
6096 begin
6097    --hr_utility.trace_on(null,'ZAEID');
6098    reset_new_tables;
6099    hr_utility.set_location('Entering ' || l_proc, 10);
6100 
6101    -- Determine whether we need to populate the cache tables
6102    -- Note: No check is made for the validity of the table data, since it is assumed that the
6103    --       reset_tables procedure was called before this procedure.
6104    if g_new_assignments_table.count = 0 then
6105 
6106       hr_utility.set_location('Setup assignments cache', 20);
6107       g_new_assignments_table.delete;
6108 
6109       if p_salary_method = 'BAL' then
6110 
6111          -- Get the balance type id's for the normal and annual balances
6112          begin
6113 
6114             select balance_type_id
6115             into   l_ee_balance_type_id
6116             from   pay_balance_types
6117             where  balance_name = 'Total Employment Equityable Income'
6118             and    legislation_code = 'ZA'
6119             and    business_group_id is null;
6120 
6121             select balance_type_id
6122             into   l_eea_balance_type_id
6123             from   pay_balance_types
6124             where  balance_name = 'Total Employment Equityable Annual Income'
6125             and    legislation_code = 'ZA'
6126             and    business_group_id is null;
6127 
6128           --Changes for Bug 7237663
6129             select balance_type_id
6130             into   l_er_balance_type_id
6131             from   pay_balance_types
6132             where  balance_name = 'Total Employment Equityable ER Normal Contributions'
6133             and    legislation_code = 'ZA'
6134             and    business_group_id is null;
6135 
6136             select balance_type_id
6137             into   l_era_balance_type_id
6138             from   pay_balance_types
6139             where  balance_name = 'Total Employment Equityable ER Annual Contributions'
6140             and    legislation_code = 'ZA'
6141             and    business_group_id is null;
6142            --End changes for Bug 7237663
6143 
6144          exception
6145             when no_data_found then
6146                raise_application_error(-20000, 'The Employment Equitable balances do not exist.');
6147 
6148          end;
6149 
6150       end if;
6151 
6152       if p_salary_method = 'ELE' then
6153 
6154          -- Get the ZA Employment Equity Remuneration element details
6155          begin
6156 
6157             select pivf.input_value_id
6158             into   l_input_value_id
6159             from   pay_input_values_f         pivf,
6160                    pay_element_types_f        petf
6161             where  petf.element_name = 'ZA Employment Equity Remuneration'
6162             and    petf.business_group_id is null
6163             and    petf.legislation_code = 'ZA'
6164             and    p_report_date between petf.effective_start_date and petf.effective_end_date
6165             and    pivf.element_type_id = petf.element_type_id
6166             and    pivf.name = 'Remuneration'
6167             and    p_report_date between pivf.effective_start_date and pivf.effective_end_date;
6168 
6169          exception
6170             when no_data_found then
6171                raise_application_error(-20004, 'The ZA Employment Equity Remuneration element does not exist.');
6172 
6173          end;
6174 
6175       end if;
6176 
6177       -- Loop through the assignments cursor and populate the assignments table
6178       for l_assignment in c_assignments loop
6179 
6180          hr_utility.set_location('ASG ' || l_assignment.assignment_id, 21);
6181 
6182          -- Bug 4413678: Begin
6183          --Added for Year 2009
6184          --If date of naturalization is on or after 27-APR-94 , then foreign national
6185          Select per_information4, papf.sex,
6186                 decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
6187                     decode(papf.PER_INFORMATION11,null,null,
6188                     decode(sign(replace(substr(papf.PER_INFORMATION11,1,10),'/','')
6189                                -'19940427')
6190                            ,-1,null,'F')))
6191          into l_race, l_sex, l_type
6192          From per_all_people_f papf
6193          Where papf.person_id = l_assignment.person_id
6194                and p_report_date between papf.effective_start_date and papf.effective_end_date;
6195          -- Bug 4413678: End
6196 
6197 
6198          if l_assignment.payroll_id is not null and l_race <> 'N' then -- Bug 4413678: Added l_race <> 'Not Used'
6199 -- added RPAHUNE
6200             g_new_assignments_table(l_assignment.assignment_id).payroll_id            := l_assignment.payroll_id;
6201             g_new_assignments_table(l_assignment.assignment_id).legal_entity_id       := l_assignment.aei_information7;
6202             g_new_assignments_table(l_assignment.assignment_id).occupational_level    := l_assignment.occupational_level;
6203             --Modified for bug 9462039
6204             g_new_assignments_table(l_assignment.assignment_id).occupational_category := null;
6205             g_new_assignments_table(l_assignment.assignment_id).occupational_category_ID := null;
6206             g_new_assignments_table(l_assignment.assignment_id).occupational_level_id    := l_assignment.occupational_level_id;
6207             g_new_assignments_table(l_assignment.assignment_id).race                  := l_race;
6208             g_new_assignments_table(l_assignment.assignment_id).sex                   := l_sex;
6209             g_new_assignments_table(l_assignment.assignment_id).foreigner              := l_type;
6210             g_new_assignments_table(l_assignment.assignment_id).employment_type        := l_assignment.employment_type;
6211 
6212             hr_utility.set_location('LEGENT ' || l_assignment.aei_information7, 22);
6213 
6214             -- Check for a new payroll_id and cache the new payroll details in the payrolls table
6215             if l_assignment.payroll_id <> l_old_payroll_id then
6216 
6217                /* Bug 8911880
6218                -- Get the start date and end date of the report
6219                begin
6220 
6221 
6222                   l_report_date := p_report_date;
6223                   l_difference := 0;
6224 
6225                   while (l_difference < 355 or l_difference > 375) loop
6226 
6227                      select ptpf.end_date + 1,
6228                             ptpl.end_date
6229                      into   l_report_start,
6230                             l_report_end
6231                      from   per_time_periods ptpf,
6232                             per_time_periods ptpl
6233                      where  ptpl.payroll_id = l_assignment.payroll_id
6234                      and    l_report_date between ptpl.start_date and ptpl.end_date
6235                      and    ptpf.payroll_id = l_assignment.payroll_id
6236                      and    add_months(l_report_date, -12) + 1 between ptpf.start_date and ptpf.end_date;
6237 
6238                      l_difference := l_report_end - l_report_start + 1;
6239 
6240                      if (l_difference < 355 or l_difference > 375) then
6241 
6242                         l_report_date := l_report_date - 1;
6243 
6244                      end if;
6245 
6246                   end loop;
6247 
6248                exception
6249                   when no_data_found then
6250                      begin
6251                              select ptpl.end_date
6252                              into   l_report_end
6253                              from   per_time_periods ptpl
6254                              where  ptpl.payroll_id = l_assignment.payroll_id
6255                              and    p_report_date between ptpl.start_date and ptpl.end_date;
6256 
6257                      exception
6258                              when no_data_found then
6259                                       Null;
6260                      end;
6261 
6262                      l_report_start := add_months(l_report_end, -12) + 1;
6263 
6264                end;
6265                */
6266 
6267                /* Bug 8911880
6268                -- Get the payroll period frequency
6269                begin
6270 
6271                   select ptpt.number_per_fiscal_year
6272                   into   l_period_frequency
6273                   from   per_time_period_types ptpt,
6274                          pay_all_payrolls_f    payr
6275                   where  payr.payroll_id = l_assignment.payroll_id
6276                   and    p_report_date between payr.effective_start_date and payr.effective_end_date
6277                   and    ptpt.period_type = payr.period_type;
6278 
6279                exception
6280                   when no_data_found then
6281                      raise_application_error(-20005, 'The Payroll Period Frequency does not exist.');
6282 
6283                end;
6284                */
6285 
6286                l_old_payroll_id := l_assignment.payroll_id;
6287 
6288             end if;
6289 
6290 --            hr_utility.set_location('REP_START ' || to_char(l_report_start, 'DD\MM\YYYY'), 22);
6291 --            hr_utility.set_location('REP_END   ' || to_char(l_report_end, 'DD\MM\YYYY'), 23);
6292 --            hr_utility.set_location('FREQ      ' || l_period_frequency, 24);
6293 
6294             if p_salary_method = 'BAL' then
6295 
6296                /* Bug 8911880
6297                -- Get the amount of days the assignment status was Active Assignment
6298                l_active_days := get_active_days
6299                                 (
6300                                    p_assignment_id => l_assignment.assignment_id,
6301                                    p_report_start  => l_report_start,
6302                                    p_report_end    => l_report_end
6303                                 );
6304 
6305                hr_utility.set_location('ACT_DAYS ' || l_active_days, 25);
6306                */
6307 
6308                -- Get the Employment Equitable Income
6309                begin
6310 
6311                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
6312                   into   l_ee_income
6313                   from   pay_balance_feeds_f         pbff,
6314                          pay_run_result_values       prrv,
6315                          pay_run_results             prr,
6316                          pay_payroll_actions         ppa,
6317                          pay_assignment_actions      paa,
6318                          per_assignments_f       asg     --Bug 4872110
6319                   -- BUG 2665394 ADDED THE TABLE TO IMPROVE THE PERFORMANCE
6320                   where  paa.assignment_id = l_assignment.assignment_id
6321                   and    ppa.payroll_action_id = paa.payroll_action_id
6322                 --  Bug 8911880
6323                 --  and    ppa.date_earned between l_report_start and l_report_end
6324                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
6325                   and    prr.assignment_action_id = paa.assignment_action_id
6326                   and    prrv.run_result_id = prr.run_result_id
6327                   and    pbff.balance_type_id = l_ee_balance_type_id
6328                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
6329                   and    prrv.input_value_id = pbff.input_value_id
6330                   -- BUG 2665394 ADDED THE JOINS TO IMPROVE THE PERFORMANCE
6331                   and    paa.assignment_id = asg.assignment_id
6332                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
6333                   and    asg.payroll_id = ppa.payroll_id;
6334 
6335                exception
6336                   when no_data_found then
6337                      l_ee_income := 0;
6338 
6339                end;
6340 
6341                -- Get the Employment Equitable Annual Income
6342                begin
6343 
6344                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
6345                   into   l_ee_annual_income
6346                   from   pay_balance_feeds_f         pbff,
6347                          pay_run_result_values       prrv,
6348                          pay_run_results             prr,
6349                          pay_payroll_actions         ppa,
6350                          pay_assignment_actions      paa,
6351                          per_assignments_f       asg     --Bug 4872110
6352                   -- BUG 2665394 ADDED THE TABLE TO IMPROVE THE PERFORMANCE
6353                   where  paa.assignment_id = l_assignment.assignment_id
6354                   and    ppa.payroll_action_id = paa.payroll_action_id
6355                 --  Bug 8911880
6356                 --  and    ppa.date_earned between l_report_start and l_report_end
6357                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
6358                   and    prr.assignment_action_id = paa.assignment_action_id
6359                   and    prrv.run_result_id = prr.run_result_id
6360                   and    pbff.balance_type_id = l_eea_balance_type_id
6361                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
6362                   and    prrv.input_value_id = pbff.input_value_id
6363                   -- BUG 2665394 ADDED THE JOINS TO IMPROVE THE PERFORMANCE
6364                   and    paa.assignment_id = asg.assignment_id
6365                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
6366                   and    asg.payroll_id = ppa.payroll_id;
6367 
6368                exception
6369                   when no_data_found then
6370                      l_ee_annual_income := 0;
6371 
6372                end;
6373 
6374                --Changes for Bug 7237663
6375                -- Two new balances added for employer contributions
6376                -- Get the Employment Equitable ER Income
6377                begin
6378 
6379                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
6380                   into   l_er_income
6381                   from   pay_balance_feeds_f         pbff,
6382                          pay_run_result_values       prrv,
6383                          pay_run_results             prr,
6384                          pay_payroll_actions         ppa,
6385                          pay_assignment_actions      paa,
6386                          per_assignments_f       asg
6387                   where  paa.assignment_id = l_assignment.assignment_id
6388                   and    ppa.payroll_action_id = paa.payroll_action_id
6389                 --  Bug 8911880
6390                 --  and    ppa.date_earned between l_report_start and l_report_end
6391                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
6392                   and    prr.assignment_action_id = paa.assignment_action_id
6393                   and    prrv.run_result_id = prr.run_result_id
6394                   and    pbff.balance_type_id = l_er_balance_type_id
6395                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
6396                   and    prrv.input_value_id = pbff.input_value_id
6397                   and    paa.assignment_id = asg.assignment_id
6398                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
6399                   and    asg.payroll_id = ppa.payroll_id;
6400 
6401                exception
6402                   when no_data_found then
6403                      l_er_income := 0;
6404 
6405                end;
6406 
6407                -- Get the Employment Equitable ER Annual Income
6408                begin
6409 
6410                   select nvl(sum(fnd_number.canonical_to_number(prrv.result_value) * pbff.scale), 0)
6411                   into   l_er_annual_income
6412                   from   pay_balance_feeds_f         pbff,
6413                          pay_run_result_values       prrv,
6414                          pay_run_results             prr,
6415                          pay_payroll_actions         ppa,
6416                          pay_assignment_actions      paa,
6417                          per_assignments_f       asg     --Bug 4872110
6418                   where  paa.assignment_id = l_assignment.assignment_id
6419                   and    ppa.payroll_action_id = paa.payroll_action_id
6420                 --  Bug 8911880
6421                 --  and    ppa.date_earned between l_report_start and l_report_end
6422                   and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
6423                   and    prr.assignment_action_id = paa.assignment_action_id
6424                   and    prrv.run_result_id = prr.run_result_id
6425                   and    pbff.balance_type_id = l_era_balance_type_id
6426                   and    ppa.effective_date between pbff.effective_start_date and pbff.effective_end_date
6427                   and    prrv.input_value_id = pbff.input_value_id
6428                   and    paa.assignment_id = asg.assignment_id
6429                   and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
6430                   and    asg.payroll_id = ppa.payroll_id;
6431 
6432                exception
6433                   when no_data_found then
6434                      l_er_annual_income := 0;
6435 
6436                end;
6437 
6438 
6439                hr_utility.set_location('EE_INC ' || l_ee_income, 26);
6440                hr_utility.set_location('EE_ANN ' || l_ee_annual_income, 27);
6441                hr_utility.set_location('ER_INC ' || l_er_income, 26);
6442                hr_utility.set_location('ER_ANN ' || l_er_annual_income, 27);
6443 
6444            --    hr_utility.set_location('STminEND ' || (l_report_end - l_report_start + 1), 28);
6445 
6446                -- Calculate the annual income = annualize normal income + annual income
6447                g_new_assignments_table(l_assignment.assignment_id).annual_income :=
6448                    l_ee_income + l_er_income + l_ee_annual_income + l_er_annual_income;
6449 
6450                hr_utility.set_location('ANSWER ' || g_new_assignments_table(l_assignment.assignment_id).annual_income, 29);
6451 
6452             elsif p_salary_method = 'SAL' then
6453 
6454                -- Get the annual salary basis for the current period
6455                begin
6456 
6457                         select input_value_id
6458                         into l_input_value_id
6459                         from per_pay_bases ppb
6460                         where ppb.pay_basis_id = l_assignment.pay_basis_id;
6461 
6462 
6463                         select pivff.input_value_id --Pay Value input value ID
6464                         into  l_input_value_id2
6465                         from  pay_input_values_f pivf, --Sal Basis Input Value
6466                               pay_input_values_f pivff --Pay Value Input Value
6467                         where pivf.input_value_id = l_input_value_id
6468                         and   pivff.element_type_id = pivf.element_type_id
6469                         and   pivff.name='Pay Value'
6470                         and   p_report_date between pivf.effective_start_date and pivf.effective_end_date
6471                         and   p_report_date between pivff.effective_start_date and pivff.effective_end_date;
6472 
6473 
6474                         select nvl(sum(fnd_number.canonical_to_number(prrv.result_value)), 0)
6475                         into   g_new_assignments_table(l_assignment.assignment_id).annual_income
6476                         from   pay_run_result_values       prrv,
6477                                pay_run_results             prr,
6478                                pay_payroll_actions         ppa,
6479                                pay_assignment_actions      paa,
6480                                per_assignments_f       asg
6481                         where  paa.assignment_id = l_assignment.assignment_id
6482                         and    ppa.payroll_action_id = paa.payroll_action_id
6483                         and    ppa.date_earned between add_months(p_report_date,-12)+1 and p_report_date
6484                         and    prr.assignment_action_id = paa.assignment_action_id
6485                         and    prrv.run_result_id  =  prr.run_result_id
6486                         and    prrv.input_value_id = l_input_value_id2
6487                         and    paa.assignment_id   = asg.assignment_id
6488                         and    ppa.effective_date between asg.effective_start_date and asg.effective_end_date
6489                         and    asg.payroll_id = ppa.payroll_id;
6490 
6491                exception
6492                   when no_data_found then
6493                      g_new_assignments_table(l_assignment.assignment_id).annual_income := 0;
6494 
6495                end;
6496 
6497             elsif p_salary_method = 'ELE' then
6498 
6499                begin
6500                   g_new_assignments_table(l_assignment.assignment_id).annual_income:=0;
6501                   open c_ele_rem(l_assignment.assignment_id,l_input_value_id);
6502                   loop
6503                       fetch c_ele_rem into v_ele_rem;
6504                       if c_ele_rem%rowcount=0 then
6505                           g_new_assignments_table(l_assignment.assignment_id).annual_income:= 0;
6506                       end if;
6507                       exit when c_ele_rem%notfound;
6508                       g_new_assignments_table(l_assignment.assignment_id).annual_income:=
6509                              g_new_assignments_table(l_assignment.assignment_id).annual_income + (v_ele_rem.screen_value * v_ele_rem.months_bet);
6510                   end loop;
6511                   close c_ele_rem;
6512                end;
6513 
6514             end if;   -- p_salary_method
6515 
6516          end if;   -- (l_assignment.payroll_id is not null)
6517 
6518       end loop;   -- c_assignments
6519 
6520    end if;   -- g_new_assignments_table.count = 0
6521 
6522    -- The index is calculted by multiplying the legal entity id by 100 and then adding the lookup code
6523    -- This should always give a unique value, since the lookup code is less than 100
6524 -- Start of adding for Employment Equity Report Enhancement Inserting values in table.
6525    l_rowind := g_new_assignments_table.first;
6526    hr_utility.set_location ('l_rowind :=' || l_rowind, 20);
6527    loop
6528       exit when l_rowind is null;
6529 
6530    hr_utility.set_location ('g_new_assignments_table(l_rowind).legal_entity_id' ||g_new_assignments_table(l_rowind).legal_entity_id, 20);
6531    hr_utility.set_location ('occupational_level :=' || g_new_assignments_table(l_rowind).occupational_level, 20);
6532    hr_utility.set_location ('occupational_category :=' || g_new_assignments_table(l_rowind).occupational_category, 20);
6533    hr_utility.set_location ('race :=' || g_new_assignments_table(l_rowind).race, 20);
6534    hr_utility.set_location ('sex :=' || g_new_assignments_table(l_rowind).sex, 20);
6535    hr_utility.set_location ('employment_type :=' || g_new_assignments_table(l_rowind).employment_type, 20);
6536    hr_utility.set_location ('foreigner :=' || g_new_assignments_table(l_rowind).foreigner, 20);
6537    hr_utility.set_location ('assignment_id :=' || l_rowind, 20);
6538 
6539    if g_new_assignments_table(l_rowind).employment_type='Permanent' then
6540      if g_new_assignments_table(l_rowind).foreigner is null then
6541       --Permanent non foreigner
6542       hr_utility.set_location('Populating permanent non foreigner',25);
6543       cat_lev_data( g_new_assignments_table(l_rowind).legal_entity_id
6544                   , g_new_assignments_table(l_rowind).occupational_level
6545                   , g_new_assignments_table(l_rowind).occupational_category
6546                   , g_new_assignments_table(l_rowind).race
6547                   , g_new_assignments_table(l_rowind).sex
6548                   , nvl(g_new_assignments_table(l_rowind).annual_income,0)
6549                   , g_new_assignments_table(l_rowind).occupational_level_id
6550                   , g_new_assignments_table(l_rowind).occupational_category_ID
6551                   , g_lev_Enc_Diff_table
6552                   );
6553      else
6554       --Permanent foreigner
6555       hr_utility.set_location('Populating permanent foreigner',25);
6556       cat_lev_data( g_new_assignments_table(l_rowind).legal_entity_id
6557                   , g_new_assignments_table(l_rowind).occupational_level
6558                   , g_new_assignments_table(l_rowind).occupational_category
6559                   , g_new_assignments_table(l_rowind).race
6560                   , g_new_assignments_table(l_rowind).sex
6561                   , nvl(g_new_assignments_table(l_rowind).annual_income,0)
6562                   , g_new_assignments_table(l_rowind).occupational_level_id
6563                   , g_new_assignments_table(l_rowind).occupational_category_ID
6564                   , g_lev_Enc_Diff_table_F
6565                   );
6566      end if;
6567    else --Non permanent
6568      if g_new_assignments_table(l_rowind).foreigner is null then
6569       --Non Permanent non foreigner
6570       hr_utility.set_location('Populating non permanent non foreigner',25);
6571       cat_lev_data( g_new_assignments_table(l_rowind).legal_entity_id
6572                   , g_new_assignments_table(l_rowind).occupational_level
6573                   , g_new_assignments_table(l_rowind).occupational_category
6574                   , g_new_assignments_table(l_rowind).race
6575                   , g_new_assignments_table(l_rowind).sex
6576                   , nvl(g_new_assignments_table(l_rowind).annual_income,0)
6577                   , g_new_assignments_table(l_rowind).occupational_level_id
6578                   , g_new_assignments_table(l_rowind).occupational_category_ID
6579                   , g_lev_Enc_Diff_table_T
6580                   );
6581      else
6582       --Non Permanent foreigner
6583       hr_utility.set_location('Populating non permanent foreigner',25);
6584       cat_lev_data( g_new_assignments_table(l_rowind).legal_entity_id
6585                   , g_new_assignments_table(l_rowind).occupational_level
6586                   , g_new_assignments_table(l_rowind).occupational_category
6587                   , g_new_assignments_table(l_rowind).race
6588                   , g_new_assignments_table(l_rowind).sex
6589                   , nvl(g_new_assignments_table(l_rowind).annual_income,0)
6590                   , g_new_assignments_table(l_rowind).occupational_level_id
6591                   , g_new_assignments_table(l_rowind).occupational_category_ID
6592                   , g_lev_Enc_Diff_table_TF
6593                   );
6594      end if;
6595    end if;
6596 
6597             l_rowind := g_new_assignments_table.next(l_rowind);
6598    END loop;
6599 
6600    l_rowind := g_lev_Enc_Diff_table.first;
6601    loop
6602       exit when l_rowind is null;
6603 
6604       INSERT INTO per_za_employment_equity
6605       (
6606          report_id,
6607          reporting_date,
6608          business_group_id,
6609          legal_entity_id,
6610          legal_entity,
6611          disability,
6612          employment_type,
6613          level_cat_code,
6614          level_cat,
6615          ma,
6616          mc,
6617          mi,
6618          mw,
6619          fa,
6620          fc,
6621          fi,
6622          fw,
6623          total
6624       )
6625       Select 'ED'  -- no of employees in each categories
6626       , p_report_date
6627       , p_business_group_id
6628       , g_lev_Enc_Diff_table(l_rowind).legal_entity_id
6629       , haou.name        legal_entity
6630       , null
6631       , 'Permanent'
6632       , g_lev_Enc_Diff_table(l_rowind).occupational_code_id
6633       , g_lev_Enc_Diff_table(l_rowind).occupational_code
6634       , g_lev_Enc_Diff_table(l_rowind).ma
6635       , g_lev_Enc_Diff_table(l_rowind).mc
6636       , g_lev_Enc_Diff_table(l_rowind).mi
6637       , g_lev_Enc_Diff_table(l_rowind).mw
6638       , g_lev_Enc_Diff_table(l_rowind).fa
6639       , g_lev_Enc_Diff_table(l_rowind).fc
6640       , g_lev_Enc_Diff_table(l_rowind).fi
6641       , g_lev_Enc_Diff_table(l_rowind).fw
6642       , g_lev_Enc_Diff_table(l_rowind).total
6643       FROM hr_all_organization_units haou
6644       Where  haou.organization_id = g_lev_Enc_Diff_table(l_rowind).legal_entity_id;
6645 
6646       INSERT INTO per_za_employment_equity
6647       (
6648          report_id,
6649          reporting_date,
6650          business_group_id,
6651          legal_entity_id,
6652          legal_entity,
6653          disability,
6654          employment_type,
6655          level_cat_code,
6656          level_cat,
6657          ma,
6658          mc,
6659          mi,
6660          mw,
6661          fa,
6662          fc,
6663          fi,
6664          fw,
6665          total
6666       )
6667       Select 'EDI'  -- income
6668       , p_report_date
6669       , p_business_group_id
6670       , g_lev_Enc_Diff_table(l_rowind).legal_entity_id
6671       , haou.name        legal_entity
6672       , null
6673       , 'Permanent'
6674       , g_lev_Enc_Diff_table(l_rowind).occupational_code_id
6675       , g_lev_Enc_Diff_table(l_rowind).occupational_code
6676       , g_lev_Enc_Diff_table(l_rowind).ma_inc
6677       , g_lev_Enc_Diff_table(l_rowind).mc_inc
6678       , g_lev_Enc_Diff_table(l_rowind).mi_inc
6679       , g_lev_Enc_Diff_table(l_rowind).mw_inc
6680       , g_lev_Enc_Diff_table(l_rowind).fa_inc
6681       , g_lev_Enc_Diff_table(l_rowind).fc_inc
6682       , g_lev_Enc_Diff_table(l_rowind).fi_inc
6683       , g_lev_Enc_Diff_table(l_rowind).fw_inc
6684       , g_lev_Enc_Diff_table(l_rowind).total_inc
6685       FROM hr_all_organization_units haou
6686       Where  haou.organization_id = g_lev_Enc_Diff_table(l_rowind).legal_entity_id;
6687 
6688      l_rowind := g_lev_Enc_Diff_table.next(l_rowind);
6689    END loop;
6690 
6691    --Permanent Foreigners
6692    l_rowind := g_lev_Enc_Diff_table_F.first;
6693    loop
6694       exit when l_rowind is null;
6695       INSERT INTO per_za_employment_equity
6696       (
6697          report_id,
6698          reporting_date,
6699          business_group_id,
6700          legal_entity_id,
6701          legal_entity,
6702          disability,
6703          employment_type,
6704          level_cat_code,
6705          level_cat,
6706          ma,
6707          mc,
6708          mi,
6709          mw,
6710          fa,
6711          fc,
6712          fi,
6713          fw,
6714          total
6715       )
6716       Select 'EDF'  -- no of employees in each categories
6717       , p_report_date
6718       , p_business_group_id
6719       , g_lev_Enc_Diff_table_F(l_rowind).legal_entity_id
6720       , haou.name        legal_entity
6721       , null
6722       , 'Permanent'
6723       , g_lev_Enc_Diff_table_F(l_rowind).occupational_code_id
6724       , g_lev_Enc_Diff_table_F(l_rowind).occupational_code
6725       , g_lev_Enc_Diff_table_F(l_rowind).ma
6726       , g_lev_Enc_Diff_table_F(l_rowind).mc
6727       , g_lev_Enc_Diff_table_F(l_rowind).mi
6728       , g_lev_Enc_Diff_table_F(l_rowind).mw
6729       , g_lev_Enc_Diff_table_F(l_rowind).fa
6730       , g_lev_Enc_Diff_table_F(l_rowind).fc
6731       , g_lev_Enc_Diff_table_F(l_rowind).fi
6732       , g_lev_Enc_Diff_table_F(l_rowind).fw
6733       , g_lev_Enc_Diff_table_F(l_rowind).total
6734       FROM hr_all_organization_units haou
6735       Where  haou.organization_id = g_lev_Enc_Diff_table_F(l_rowind).legal_entity_id;
6736 
6737       INSERT INTO per_za_employment_equity
6738       (
6739          report_id,
6740          reporting_date,
6741          business_group_id,
6742          legal_entity_id,
6743          legal_entity,
6744          disability,
6745          employment_type,
6746          level_cat_code,
6747          level_cat,
6748          ma,
6749          mc,
6750          mi,
6751          mw,
6752          fa,
6753          fc,
6754          fi,
6755          fw,
6756          total
6757       )
6758       Select 'EDFI'  -- income
6759       , p_report_date
6760       , p_business_group_id
6761       , g_lev_Enc_Diff_table_F(l_rowind).legal_entity_id
6762       , haou.name        legal_entity
6763       , null
6764       , 'Permanent'
6765       , g_lev_Enc_Diff_table_F(l_rowind).occupational_code_id
6766       , g_lev_Enc_Diff_table_F(l_rowind).occupational_code
6767       , g_lev_Enc_Diff_table_F(l_rowind).ma_inc
6768       , g_lev_Enc_Diff_table_F(l_rowind).mc_inc
6769       , g_lev_Enc_Diff_table_F(l_rowind).mi_inc
6770       , g_lev_Enc_Diff_table_F(l_rowind).mw_inc
6771       , g_lev_Enc_Diff_table_F(l_rowind).fa_inc
6772       , g_lev_Enc_Diff_table_F(l_rowind).fc_inc
6773       , g_lev_Enc_Diff_table_F(l_rowind).fi_inc
6774       , g_lev_Enc_Diff_table_F(l_rowind).fw_inc
6775       , g_lev_Enc_Diff_table_F(l_rowind).total_inc
6776       FROM hr_all_organization_units haou
6777       Where  haou.organization_id = g_lev_Enc_Diff_table_F(l_rowind).legal_entity_id;
6778 
6779      l_rowind := g_lev_Enc_Diff_table_F.next(l_rowind);
6780    END loop;
6781 
6782    --Non permanent workers
6783    l_rowind := g_lev_Enc_Diff_table_T.first;
6784    loop
6785       exit when l_rowind is null;
6786       --Non permanent workers
6787       INSERT INTO per_za_employment_equity
6788       (
6789          report_id,
6790          reporting_date,
6791          business_group_id,
6792          legal_entity_id,
6793          legal_entity,
6794          disability,
6795          employment_type,
6796          level_cat_code,
6797          level_cat,
6798          ma,
6799          mc,
6800          mi,
6801          mw,
6802          fa,
6803          fc,
6804          fi,
6805          fw,
6806          total
6807       )
6808       Select 'ED'  -- no of employees in each categories
6809       , p_report_date
6810       , p_business_group_id
6811       , g_lev_Enc_Diff_table_T(l_rowind).legal_entity_id
6812       , haou.name        legal_entity
6813       , null
6814       , 'Non-Permanent'
6815       , g_lev_Enc_Diff_table_T(l_rowind).occupational_code_id
6816       , g_lev_Enc_Diff_table_T(l_rowind).occupational_code
6817       , g_lev_Enc_Diff_table_T(l_rowind).ma
6818       , g_lev_Enc_Diff_table_T(l_rowind).mc
6819       , g_lev_Enc_Diff_table_T(l_rowind).mi
6820       , g_lev_Enc_Diff_table_T(l_rowind).mw
6821       , g_lev_Enc_Diff_table_T(l_rowind).fa
6822       , g_lev_Enc_Diff_table_T(l_rowind).fc
6823       , g_lev_Enc_Diff_table_T(l_rowind).fi
6824       , g_lev_Enc_Diff_table_T(l_rowind).fw
6825       , g_lev_Enc_Diff_table_T(l_rowind).total
6826       FROM hr_all_organization_units haou
6827       Where  haou.organization_id = g_lev_Enc_Diff_table_T(l_rowind).legal_entity_id;
6828 
6829       INSERT INTO per_za_employment_equity
6830       (
6831          report_id,
6832          reporting_date,
6833          business_group_id,
6834          legal_entity_id,
6835          legal_entity,
6836          disability,
6837          employment_type,
6838          level_cat_code,
6839          level_cat,
6840          ma,
6841          mc,
6842          mi,
6843          mw,
6844          fa,
6845          fc,
6846          fi,
6847          fw,
6848          total
6849       )
6850       Select 'EDI'  -- income
6851       , p_report_date
6852       , p_business_group_id
6853       , g_lev_Enc_Diff_table_T(l_rowind).legal_entity_id
6854       , haou.name        legal_entity
6855       , null
6856       , 'Non-Permanent'
6857       , g_lev_Enc_Diff_table_T(l_rowind).occupational_code_id
6858       , g_lev_Enc_Diff_table_T(l_rowind).occupational_code
6859       , g_lev_Enc_Diff_table_T(l_rowind).ma_inc
6860       , g_lev_Enc_Diff_table_T(l_rowind).mc_inc
6861       , g_lev_Enc_Diff_table_T(l_rowind).mi_inc
6862       , g_lev_Enc_Diff_table_T(l_rowind).mw_inc
6863       , g_lev_Enc_Diff_table_T(l_rowind).fa_inc
6864       , g_lev_Enc_Diff_table_T(l_rowind).fc_inc
6865       , g_lev_Enc_Diff_table_T(l_rowind).fi_inc
6866       , g_lev_Enc_Diff_table_T(l_rowind).fw_inc
6867       , g_lev_Enc_Diff_table_T(l_rowind).total_inc
6868       FROM hr_all_organization_units haou
6869       Where  haou.organization_id = g_lev_Enc_Diff_table_T(l_rowind).legal_entity_id;
6870 
6871 
6872      l_rowind := g_lev_Enc_Diff_table_T.next(l_rowind);
6873    END loop;
6874 
6875    --Non permanent foreigners workers
6876    l_rowind := g_lev_Enc_Diff_table_TF.first;
6877    loop
6878       exit when l_rowind is null;
6879       INSERT INTO per_za_employment_equity
6880       (
6881          report_id,
6882          reporting_date,
6883          business_group_id,
6884          legal_entity_id,
6885          legal_entity,
6886          disability,
6887          employment_type,
6888          level_cat_code,
6889          level_cat,
6890          ma,
6891          mc,
6892          mi,
6893          mw,
6894          fa,
6895          fc,
6896          fi,
6897          fw,
6898          total
6899       )
6900       Select 'EDF'  -- no of employees in each categories
6901       , p_report_date
6902       , p_business_group_id
6903       , g_lev_Enc_Diff_table_TF(l_rowind).legal_entity_id
6904       , haou.name        legal_entity
6905       , null
6906       , 'Non-Permanent'
6907       , g_lev_Enc_Diff_table_TF(l_rowind).occupational_code_id
6908       , g_lev_Enc_Diff_table_TF(l_rowind).occupational_code
6909       , g_lev_Enc_Diff_table_TF(l_rowind).ma
6910       , g_lev_Enc_Diff_table_TF(l_rowind).mc
6911       , g_lev_Enc_Diff_table_TF(l_rowind).mi
6912       , g_lev_Enc_Diff_table_TF(l_rowind).mw
6913       , g_lev_Enc_Diff_table_TF(l_rowind).fa
6914       , g_lev_Enc_Diff_table_TF(l_rowind).fc
6915       , g_lev_Enc_Diff_table_TF(l_rowind).fi
6916       , g_lev_Enc_Diff_table_TF(l_rowind).fw
6917       , g_lev_Enc_Diff_table_TF(l_rowind).total
6918       FROM hr_all_organization_units haou
6919       Where  haou.organization_id = g_lev_Enc_Diff_table_TF(l_rowind).legal_entity_id;
6920 
6921       INSERT INTO per_za_employment_equity
6922       (
6923          report_id,
6924          reporting_date,
6925          business_group_id,
6926          legal_entity_id,
6927          legal_entity,
6928          disability,
6929          employment_type,
6930          level_cat_code,
6931          level_cat,
6932          ma,
6933          mc,
6934          mi,
6935          mw,
6936          fa,
6937          fc,
6938          fi,
6939          fw,
6940          total
6941       )
6942       Select 'EDFI'  -- income
6943       , p_report_date
6944       , p_business_group_id
6945       , g_lev_Enc_Diff_table_TF(l_rowind).legal_entity_id
6946       , haou.name        legal_entity
6947       , null
6948       , 'Non-Permanent'
6949       , g_lev_Enc_Diff_table_TF(l_rowind).occupational_code_id
6950       , g_lev_Enc_Diff_table_TF(l_rowind).occupational_code
6951       , g_lev_Enc_Diff_table_TF(l_rowind).ma_inc
6952       , g_lev_Enc_Diff_table_TF(l_rowind).mc_inc
6953       , g_lev_Enc_Diff_table_TF(l_rowind).mi_inc
6954       , g_lev_Enc_Diff_table_TF(l_rowind).mw_inc
6955       , g_lev_Enc_Diff_table_TF(l_rowind).fa_inc
6956       , g_lev_Enc_Diff_table_TF(l_rowind).fc_inc
6957       , g_lev_Enc_Diff_table_TF(l_rowind).fi_inc
6958       , g_lev_Enc_Diff_table_TF(l_rowind).fw_inc
6959       , g_lev_Enc_Diff_table_TF(l_rowind).total_inc
6960       FROM hr_all_organization_units haou
6961       Where  haou.organization_id = g_lev_Enc_Diff_table_TF(l_rowind).legal_entity_id;
6962 
6963      l_rowind := g_lev_Enc_Diff_table_TF.next(l_rowind);
6964    END loop;
6965 --hr_utility.trace_off;
6966 
6967 
6968 -- inserting 0 values for the no of employees
6969       insert into per_za_employment_equity
6970       (
6971          report_id,
6972          reporting_date,
6973          business_group_id,
6974          legal_entity_id,
6975          legal_entity,
6976          disability,
6977          employment_type,
6978          level_cat_code,
6979          level_cat,
6980          MA,
6981          MC,
6982          MI,
6983          MW,
6984          FA,
6985          FC,
6986          FI,
6987          FW,
6988          total
6989       )
6990       select 'ED'            report_id,
6991              p_report_date    reporting_date,
6992              p_business_group_id business_group_id,
6993              haou.organization_id legal_entity_id,
6994              haou.name        legal_entity,
6995              null              disability,
6996              'Permanent'      employment_type,
6997              hl.lookup_code   level_cat_code,
6998              hl.meaning,
6999              0                MA,
7000              0                MC,
7001              0                MI,
7002              0                MW,
7003              0                FA,
7004              0                FC,
7005              0                FI,
7006              0                FW,
7007              0                total
7008       from   hr_lookups hl
7009          ,   hr_all_organization_units haou
7010       where not exists
7011       (
7012          select 'X'
7013          from   per_za_employment_equity pzee
7014          where  pzee.level_cat_code    = hl.lookup_code
7015          and    pzee.report_id         = 'ED'
7016          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
7017          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7018          and    pzee.employment_type   = 'Permanent'
7019       )
7020       and hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7021       and haou.business_group_id = p_business_group_id          --Bug 4872110
7022       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7023 
7024 -- inserting 0 values for the Income
7025       insert into per_za_employment_equity
7026       (
7027          report_id,
7028          reporting_date,
7029          business_group_id,
7030          legal_entity_id,
7031          legal_entity,
7032          disability,
7033          employment_type,
7034          level_cat_code,
7035          level_cat,
7036          MA,
7037          MC,
7038          MI,
7039          MW,
7040          FA,
7041          FC,
7042          FI,
7043          FW,
7044          total
7045       )
7046       select 'EDI'            report_id,
7047              p_report_date    reporting_date,
7048              p_business_group_id business_group_id,
7049              haou.organization_id legal_entity_id,
7050              haou.name        legal_entity,
7051              null              disability,
7052              'Permanent'      employment_type,
7053              hl.lookup_code   level_cat_code,
7054              hl.meaning               ,
7055              0                MA,
7056              0                MC,
7057              0                MI,
7058              0                MW,
7059              0                FA,
7060              0                FC,
7061              0                FI,
7062              0                FW,
7063              0                total
7064       from   hr_lookups hl
7065          ,   hr_all_organization_units haou
7066       where not exists
7067       (
7068          select 'X'
7069          from   per_za_employment_equity pzee
7070          where  pzee.level_cat_code    = hl.lookup_code
7071          and    pzee.report_id         = 'EDI'
7072          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
7073          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7074          and    pzee.employment_type   = 'Permanent'
7075       )
7076       and hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7077       and haou.business_group_id = p_business_group_id          --Bug 4872110
7078       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7079 
7080 -- inserting 0 values for the no of employees
7081       insert into per_za_employment_equity
7082       (
7083          report_id,
7084          reporting_date,
7085          business_group_id,
7086          legal_entity_id,
7087          legal_entity,
7088          disability,
7089          employment_type,
7090          level_cat_code,
7091          level_cat,
7092          MA,
7093          MC,
7094          MI,
7095          MW,
7096          FA,
7097          FC,
7098          FI,
7099          FW,
7100          total
7101       )
7102       select 'EDF'            report_id,
7103              p_report_date    reporting_date,
7104              p_business_group_id business_group_id,
7105              haou.organization_id legal_entity_id,
7106              haou.name        legal_entity,
7107              null              disability,
7108              'Permanent'      employment_type,
7109              hl.lookup_code   level_cat_code,
7110              hl.meaning,
7111              0                MA,
7112              0                MC,
7113              0                MI,
7114              0                MW,
7115              0                FA,
7116              0                FC,
7117              0                FI,
7118              0                FW,
7119              0                total
7120       from   hr_lookups hl
7121          ,   hr_all_organization_units haou
7122       where not exists
7123       (
7124          select 'X'
7125          from   per_za_employment_equity pzee
7126          where  pzee.level_cat_code    = hl.lookup_code
7127          and    pzee.report_id         = 'EDF'
7128          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
7129          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7130          and    pzee.employment_type   = 'Permanent'
7131       )
7132       and hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7133       and haou.business_group_id = p_business_group_id          --Bug 4872110
7134       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7135 
7136 -- inserting 0 values for the Income
7137       insert into per_za_employment_equity
7138       (
7139          report_id,
7140          reporting_date,
7141          business_group_id,
7142          legal_entity_id,
7143          legal_entity,
7144          disability,
7145          employment_type,
7146          level_cat_code,
7147          level_cat,
7148          MA,
7149          MC,
7150          MI,
7151          MW,
7152          FA,
7153          FC,
7154          FI,
7155          FW,
7156          total
7157       )
7158       select 'EDFI'            report_id,
7159              p_report_date    reporting_date,
7160              p_business_group_id business_group_id,
7161              haou.organization_id legal_entity_id,
7162              haou.name        legal_entity,
7163              null              disability,
7164              'Permanent'      employment_type,
7165              hl.lookup_code   level_cat_code,
7166              hl.meaning               ,
7167              0                MA,
7168              0                MC,
7169              0                MI,
7170              0                MW,
7171              0                FA,
7172              0                FC,
7173              0                FI,
7174              0                FW,
7175              0                total
7176       from   hr_lookups hl
7177          ,   hr_all_organization_units haou
7178       where not exists
7179       (
7180          select 'X'
7181          from   per_za_employment_equity pzee
7182          where  pzee.level_cat_code    = hl.lookup_code
7183          and    pzee.report_id         = 'EDFI'
7184          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
7185          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7186          and    pzee.employment_type   = 'Permanent'
7187       )
7188       and hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7189       and haou.business_group_id = p_business_group_id          --Bug 4872110
7190       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7191 
7192 --Insert rows for non permanent
7193 --Rows present for Non foreigner non permanent, but not present for
7194 --foreigner non permanent
7195       insert into per_za_employment_equity
7196       (
7197          report_id,
7198          reporting_date,
7199          business_group_id,
7200          legal_entity_id,
7201          legal_entity,
7202          disability,
7203          employment_type,
7204          level_cat_code,
7205          level_cat,
7206          MA,
7207          MC,
7208          MI,
7209          MW,
7210          FA,
7211          FC,
7212          FI,
7213          FW,
7214          total
7215       )
7216       select  decode(report_id,'ED','EDF','EDFI'),
7217          reporting_date,
7218          business_group_id,
7219          legal_entity_id,
7220          legal_entity,
7221          disability,
7222          employment_type,
7223          level_cat_code,
7224          level_cat,
7225          0,
7226          0,
7227          0,
7228          0,
7229          0,
7230          0,
7231          0,
7232          0,
7233          0
7234       from   per_za_employment_equity pzee1
7235       Where  pzee1.business_group_id = p_business_group_id
7236       AND    pzee1.legal_entity_id = nvl(p_legal_entity_id, pzee1.legal_entity_id)
7237       AND    pzee1.report_id IN ('ED','EDI')
7238       AND    not exists
7239       (
7240          select 'X'
7241          from   per_za_employment_equity pzee
7242          where  pzee.business_group_id   = pzee1.business_group_id
7243          AND    pzee.legal_entity_id    = pzee1.legal_entity_id
7244          AND    ( pzee1.report_id ||'F'     = pzee.report_id    --row not present for ED
7245                   OR
7246                   substr(pzee1.report_id,1,2)||'FI'   = pzee.report_id)
7247          AND    pzee1.level_cat_code     = pzee.level_cat_code
7248          AND    pzee1.level_cat          = pzee.level_cat
7249          and    pzee.employment_type     = pzee1.employment_type
7250       );
7251 
7252 --Insert rows for non permanent
7253 --Rows present for foreigner non permanent, but not present for
7254 --non foreigner non permanent
7255       insert into per_za_employment_equity
7256       (
7257          report_id,
7258          reporting_date,
7259          business_group_id,
7260          legal_entity_id,
7261          legal_entity,
7262          disability,
7263          employment_type,
7264          level_cat_code,
7265          level_cat,
7266          MA,
7267          MC,
7268          MI,
7269          MW,
7270          FA,
7271          FC,
7272          FI,
7273          FW,
7274          total
7275       )
7276       select  decode(report_id,'EDF','ED','EDI'),
7277          reporting_date,
7278          business_group_id,
7279          legal_entity_id,
7280          legal_entity,
7281          disability,
7282          employment_type,
7283          level_cat_code,
7284          level_cat,
7285          0,
7286          0,
7287          0,
7288          0,
7289          0,
7290          0,
7291          0,
7292          0,
7293          0
7294       from   per_za_employment_equity pzee1
7295       Where  pzee1.business_group_id = p_business_group_id
7296       AND    pzee1.legal_entity_id = nvl(p_legal_entity_id, pzee1.legal_entity_id)
7297       AND    pzee1.report_id IN ('EDF','EDFI')
7298       AND    not exists
7299       (
7300          select 'X'
7301          from   per_za_employment_equity pzee
7302          where  pzee.business_group_id   = pzee1.business_group_id
7303          AND    pzee.legal_entity_id    = pzee1.legal_entity_id
7304          AND    ( pzee.report_id ||'F'     = pzee1.report_id    --row not present for ED
7305                   OR
7306                   (pzee1.report_id='EDFI' and pzee.report_id='EDI'))
7307          AND    pzee1.level_cat_code     = pzee.level_cat_code
7308          AND    pzee1.level_cat          = pzee.level_cat
7309          and    pzee.employment_type     = pzee1.employment_type
7310       );
7311 
7312 
7313 
7314 commit;
7315 
7316 end init_g_cat_lev_new_table;
7317 
7318 
7319 function get_termination_reason_new
7320 (
7321    p_business_group_id in per_all_assignments_f.business_group_id%type,
7322    p_report_date       in per_all_assignments_f.effective_end_date%type,
7323    p_reason_code       in per_periods_of_service.leaving_reason%type
7324 )  return varchar2 is
7325 
7326 l_termination_reason pay_user_column_instances_f.value%type;
7327 
7328 begin
7329 
7330    select pucifcat.value
7331    into   l_termination_reason
7332    from   pay_user_column_instances_f pucifcat,
7333           pay_user_rows_f             purfcat,
7334           pay_user_columns            puccat,
7335           pay_user_rows_f             purfqc,
7336           pay_user_column_instances_f pucifqc,
7337           pay_user_columns            pucqc,
7338           pay_user_tables             put
7339    where  put.user_table_name = 'ZA_TERMINATION_CATEGORIES'
7340    and    put.business_group_id is null
7341    and    put.legislation_code = 'ZA'
7342    and    pucqc.user_table_id = put.user_table_id
7343    and    pucqc.user_column_name = 'Lookup Code'
7344    and    pucifqc.user_column_id = pucqc.user_column_id
7345    and    pucifqc.business_group_id = p_business_group_id
7346    and    p_report_date between pucifqc.effective_start_date and pucifqc.effective_end_date
7347    and    pucifqc.value = p_reason_code
7348    and    purfqc.user_table_id = put.user_table_id
7349    and    purfqc.user_row_id = pucifqc.user_row_id
7350    and    purfqc.business_group_id = pucifqc.business_group_id
7351    and    p_report_date between purfqc.effective_start_date and purfqc.effective_end_date
7352    and    puccat.user_table_id = put.user_table_id
7353    and    puccat.user_column_name = 'Termination Category'
7354    and    purfcat.user_table_id = put.user_table_id
7355    and    purfcat.business_group_id = pucifqc.business_group_id
7356    and    p_report_date between purfcat.effective_start_date and purfcat.effective_end_date
7357    and    purfcat.row_low_range_or_name = purfqc.row_low_range_or_name
7358    and    pucifcat.user_column_id = puccat.user_column_id
7359    and    pucifcat.user_row_id = purfcat.user_row_id
7360    and    p_report_date between pucifcat.effective_start_date and pucifcat.effective_end_date
7361    and    pucifcat.business_group_id = pucifqc.business_group_id
7362    and    pucifcat.value in
7363    (
7364       'Resignation',
7365       'Non-Renewal of Contract',
7366       'Dismissal - Operational Requirements',
7367       'Dismissal - Misconduct',
7368       'Dismissal - Incapacity',
7369       --'Other'
7370       'Retirement',
7371       'Death'
7372    );
7373 
7374    return l_termination_reason;
7375 
7376 exception
7377    when no_data_found then
7378       return 'No Leaving Reason';
7379 
7380 end get_termination_reason_new;
7381 
7382 
7383 procedure populate_ee_table_new
7384 (
7385    p_report_code       in varchar2,
7386    p_report_date       in per_all_assignments_f.effective_end_date%type,
7387    p_business_group_id in per_all_assignments_f.business_group_id%type,
7388    p_legal_entity_id   in per_assignment_extra_info.aei_information7%type := null
7389 )  is
7390 
7391 l_counter number;
7392 l_reason  varchar2(200);
7393 l_nat_date date;
7394 
7395 begin
7396 
7397    l_nat_date := to_date('27-04-1994', 'DD-MM-YYYY');
7398 
7399    -- Note EQ1 is for the following 2 reports:
7400    --    2. Occupational Categories (including employees with disabilities)
7401    --    3. Occupational Categories (only employees with disabilities)
7402    if p_report_code = 'EQ1' then
7403 
7404       -- Note: The date effective select on per_all_assignments_f is ok in this case, since an assignment
7405       --       record always exist at the same time as an employee record with status EMP
7406       insert into per_za_employment_equity
7407       (
7408          report_id,
7409          reporting_date,
7410          business_group_id,
7411          legal_entity_id,
7412          legal_entity,
7413          disability,
7414          employment_type,
7415          level_cat_code,
7416          level_cat,
7417          ma,
7418          mc,
7419          mi,
7420          mw,
7421          fa,
7422          fc,
7423          fi,
7424          fw,
7425          total
7426       )
7427       select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
7428                     decode(papf.PER_INFORMATION11,null,null,
7429                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
7430                                -to_char(l_nat_date,'YYYYMMDD'))
7431                            ,-1,null,'F'))
7432                     )      report_code,
7433              p_report_date                                                              reporting_date,
7434              paaf.business_group_id,
7435              paei.aei_information7                                                      legal_entity_id,
7436              haou.name                                                                  legal_entity,
7437              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)    disability, --3962073
7438              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
7439              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id) employment_type,  -- Bug 3962073
7440              hl.lookup_code                                                             meaning_code,
7441              nvl(per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Category') occupational_category,
7442              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
7443              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
7444              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
7445              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
7446              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
7447              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
7448              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
7449              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
7450              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
7451              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
7452              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
7453              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
7454              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
7455              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
7456              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
7457              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
7458       from   hr_lookups                hl,
7459              hr_lookups                hl1,
7460              hr_lookups                hl2,
7461              hr_all_organization_units haou,
7462              per_assignment_extra_info paei,
7463              per_all_assignments_f     paaf,
7464              per_all_people_f          papf
7465       where  papf.business_group_id = p_business_group_id
7466       and    p_report_date between papf.effective_start_date and papf.effective_end_date
7467       and    papf.current_employee_flag = 'Y'
7468       and    paaf.person_id = papf.person_id
7469       and    paaf.primary_flag = 'Y'
7470       and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
7471       and    paei.assignment_id = paaf.assignment_id
7472       and    paei.information_type = 'ZA_SPECIFIC_INFO'
7473       and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
7474       and    paei.aei_information7 is not null
7475       and    nvl(paei.aei_information6, 'N') <> 'Y'
7476       and    haou.organization_id = paei.aei_information7
7477       and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
7478       and    hl.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
7479       AND    hl.lookup_code <> '15' -- Not Applicable.
7480       and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
7481       AND    hl1.lookup_code <> '15' -- Not Applicable.
7482       and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
7483       and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7484       AND    hl2.lookup_code <> '15' -- Not Applicable.
7485       and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
7486       group  by paaf.business_group_id,
7487              paei.aei_information7,
7488              haou.name,
7489              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
7490              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)), -- Bug 3962073
7491              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id),  -- Bug 3962073
7492              hl.lookup_code,
7493              nvl(per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id), 'No Occupational Category'),
7494              p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
7495                               decode(papf.PER_INFORMATION11,null,null,
7496                               decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
7497                                -to_char(l_nat_date,'YYYYMMDD'))
7498                            ,-1,null,'F'))
7499                     );
7500 
7501       commit;
7502 
7503       -- Inserts non-associated occupational categories with zero values
7504       insert into per_za_employment_equity
7505       (
7506          report_id,
7507          reporting_date,
7508          business_group_id,
7509          legal_entity_id,
7510          legal_entity,
7511          disability,
7512          employment_type,
7513          level_cat_code,
7514          level_cat,
7515          MA,
7516          MC,
7517          MI,
7518          MW,
7519          FA,
7520          FC,
7521          FI,
7522          FW,
7523          total
7524       )
7525       select 'EQ1'            report_id,
7526              p_report_date    reporting_date,
7527              p_business_group_id business_group_id,
7528              haou.organization_id legal_entity_id,
7529              haou.name        legal_entity,
7530              'Y'              disability,
7531              'Permanent'      employment_type,
7532              hl.lookup_code   level_cat_code,
7533              hl.meaning       level_cat,
7534              0                MA,
7535              0                MC,
7536              0                MI,
7537              0                MW,
7538              0                FA,
7539              0                FC,
7540              0                FI,
7541              0                FW,
7542              0                total
7543       from   hr_lookups hl
7544          ,   hr_all_organization_units haou
7545       where not exists
7546       (
7547          select 'X'
7548          from   per_za_employment_equity pzee
7549          where  pzee.level_cat_code    = hl.lookup_code
7550          and    pzee.report_id         = 'EQ1'
7551          and    pzee.business_group_id = p_business_group_id           --Bug 4872110
7552          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7553          and    pzee.disability        = 'Y'
7554          and    pzee.employment_type   = 'Permanent'
7555       )
7556       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
7557       and haou.business_group_id = p_business_group_id          --Bug 4872110
7558       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7559 
7560 -- Inseting 0 VALUES FOR FOREIGN nationals
7561       insert into per_za_employment_equity
7562       (
7563          report_id,
7564          reporting_date,
7565          business_group_id,
7566          legal_entity_id,
7567          legal_entity,
7568          disability,
7569          employment_type,
7570          level_cat_code,
7571          level_cat,
7572          MA,
7573          MC,
7574          MI,
7575          MW,
7576          FA,
7577          FC,
7578          FI,
7579          FW,
7580          total
7581       )
7582       select 'EQ1F'            report_id,
7583              p_report_date    reporting_date,
7584              p_business_group_id business_group_id,
7585              haou.organization_id legal_entity_id,
7586              haou.name        legal_entity,
7587              'Y'              disability,
7588              'Permanent'      employment_type,
7589              hl.lookup_code   level_cat_code,
7590              hl.meaning       level_cat,
7591              0                MA,
7592              0                MC,
7593              0                MI,
7594              0                MW,
7595              0                FA,
7596              0                FC,
7597              0                FI,
7598              0                FW,
7599              0                total
7600       from   hr_lookups hl
7601          ,   hr_all_organization_units haou
7602       where not exists
7603       (
7604          select 'X'
7605          from   per_za_employment_equity pzee
7606          where  pzee.level_cat_code    = hl.lookup_code
7607          and    pzee.report_id         = 'EQ1F'
7608          and    pzee.business_group_id = p_business_group_id           --Bug 4872110
7609          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7610          and    pzee.disability        = 'Y'
7611          and    pzee.employment_type   = 'Permanent'
7612       )
7613       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
7614       and haou.business_group_id = p_business_group_id          --Bug 4872110
7615       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7616 
7617       commit;
7618 
7619    -- Note EQ2 is for the following 2 reports:
7620    --    4. Occupational Levels (including employees with disabilities)
7621    --    5. Occupational Levels (only employees with disabilities)
7622    elsif p_report_code = 'EQ2' then
7623 
7624       -- Populate with Occupational Level Totals
7625       insert into per_za_employment_equity
7626       (
7627          report_id,
7628          reporting_date,
7629          business_group_id,
7630          legal_entity_id,
7631          legal_entity,
7632          disability,
7633          employment_type,
7634          level_cat_code,
7635          level_cat,
7636          MA,
7637          MC,
7638          MI,
7639          MW,
7640          FA,
7641          FC,
7642          FI,
7643          FW,
7644          total
7645       )
7646       select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
7647                     decode(papf.PER_INFORMATION11,null,null,
7648                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
7649                                -to_char(l_nat_date,'YYYYMMDD'))
7650                            ,-1,null,'F'))
7651              ) report_code,
7652              p_report_date                                                              reporting_date,
7653              paaf.business_group_id,
7654              paei.aei_information7                                                      legal_entity_id,
7655              haou.name                                                                  legal_entity,
7656              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)      disability, --3962073
7657              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
7658              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id) employment_type,  -- Bug 3962073
7659              hl.lookup_code                                                             meaning_code,
7660              nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level') occupational_level,
7661              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
7662              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
7663              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
7664              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
7665              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
7666              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
7667              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
7668              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
7669              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
7670              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
7671              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
7672              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
7673              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
7674              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
7675              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
7676              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
7677       from   hr_lookups                hl,
7678              hr_lookups                hl1,
7679 --             hr_lookups                hl2,
7680              hr_all_organization_units haou,
7681              per_assignment_extra_info paei,
7682              per_all_assignments_f     paaf,
7683              per_all_people_f          papf
7684       where  papf.business_group_id = p_business_group_id
7685       and    p_report_date between papf.effective_start_date and papf.effective_end_date
7686       and    papf.current_employee_flag = 'Y'
7687       and    paaf.person_id = papf.person_id
7688       and    paaf.primary_flag = 'Y'
7689       and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
7690       and    paei.assignment_id = paaf.assignment_id
7691       and    paei.information_type = 'ZA_SPECIFIC_INFO'
7692       and    paei.aei_information7 = nvl(p_legal_entity_id,paei.aei_information7)
7693       and    paei.aei_information7 is not null
7694       and    nvl(paei.aei_information6, 'N') <> 'Y'
7695       and    haou.organization_id = paei.aei_information7
7696       and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7697       and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
7698       AND    hl.lookup_code <> '15' -- Not Applicable.
7699       and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
7700       AND    hl1.lookup_code <> '15' -- Operation / core function
7701       and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
7702  --     and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
7703  --     and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
7704  --     AND    hl2.lookup_code <> '15' -- Not Applicable.
7705       group  by paaf.business_group_id,
7706              paei.aei_information7,
7707              haou.name,
7708              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
7709              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)), -- Bug 3962073
7710              -- per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id), -- Bug 3962073
7711              hl.lookup_code,
7712              nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level'),
7713              p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
7714                     decode(papf.PER_INFORMATION11,null,null,
7715                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
7716                                -to_char(l_nat_date,'YYYYMMDD'))
7717                            ,-1,null,'F')));
7718 
7719       commit;
7720 
7721       -- Inserts non-associated occupational levels with zero values
7722       insert into per_za_employment_equity
7723       (
7724          report_id,
7725          reporting_date,
7726          business_group_id,
7727          legal_entity_id,
7728          legal_entity,
7729          disability,
7730          employment_type,
7731          level_cat_code,
7732          level_cat,
7733          MA,
7734          MC,
7735          MI,
7736          MW,
7737          FA,
7738          FC,
7739          FI,
7740          FW,
7741          total
7742       )
7743       select 'EQ2'            report_id,
7744              p_report_date    reporting_date,
7745              p_business_group_id business_group_id,
7746              haou.organization_id legal_entity_id,
7747              haou.name        legal_entity,
7748              'Y'              disability,
7749              'Permanent'      employment_type,
7750              hl.lookup_code   level_cat_code,
7751              hl.meaning       level_cat,
7752              0                MA,
7753              0                MC,
7754              0                MI,
7755              0                MW,
7756              0                FA,
7757              0                FC,
7758              0                FI,
7759              0                FW,
7760              0                total
7761       from   hr_lookups hl
7762          ,   hr_all_organization_units haou
7763       where not exists
7764       (
7765          select 'X'
7766          from   per_za_employment_equity pzee
7767          where  pzee.level_cat_code    = hl.lookup_code
7768          and    pzee.report_id         = 'EQ2'
7769          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
7770          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7771          and    pzee.disability        = 'Y'
7772          and    pzee.employment_type   = 'Permanent'
7773       )
7774       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7775       and haou.business_group_id = p_business_group_id          --Bug 4872110
7776       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7777 
7778 
7779 -- inserting 0 values for the Foreign Nationals
7780       insert into per_za_employment_equity
7781       (
7782          report_id,
7783          reporting_date,
7784          business_group_id,
7785          legal_entity_id,
7786          legal_entity,
7787          disability,
7788          employment_type,
7789          level_cat_code,
7790          level_cat,
7791          MA,
7792          MC,
7793          MI,
7794          MW,
7795          FA,
7796          FC,
7797          FI,
7798          FW,
7799          total
7800       )
7801       select 'EQ2F'            report_id,
7802              p_report_date    reporting_date,
7803              p_business_group_id business_group_id,
7804              haou.organization_id legal_entity_id,
7805              haou.name        legal_entity,
7806              'Y'              disability,
7807              'Permanent'      employment_type,
7808              hl.lookup_code   level_cat_code,
7809              hl.meaning       level_cat,
7810              0                MA,
7811              0                MC,
7812              0                MI,
7813              0                MW,
7814              0                FA,
7815              0                FC,
7816              0                FI,
7817              0                FW,
7818              0                total
7819       from   hr_lookups hl
7820          ,   hr_all_organization_units haou
7821       where not exists
7822       (
7823          select 'X'
7824          from   per_za_employment_equity pzee
7825          where  pzee.level_cat_code    = hl.lookup_code
7826          and    pzee.report_id         = 'EQ2F'
7827          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
7828          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
7829          and    pzee.disability        = 'Y'
7830          and    pzee.employment_type   = 'Permanent'
7831       )
7832       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7833       and haou.business_group_id = p_business_group_id          --Bug 4872110
7834       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
7835 
7836 
7837       commit;
7838 
7839 -- For employment equity enhancement
7840    elsif p_report_code = 'EQ3' then
7841 
7842       -- Populate with New Hires
7843       insert into per_za_employment_equity
7844       (
7845          report_id,
7846          reporting_date,
7847          business_group_id,
7848          legal_entity_id,
7849          legal_entity,
7850          disability,
7851          employment_type,
7852          level_cat_code,
7853          level_cat,
7854          MA,
7855          MC,
7856          MI,
7857          MW,
7858          FA,
7859          FC,
7860          FI,
7861          FW,
7862          total
7863       )
7864       select tpa.report_code,
7865              tpa.reporting_date,
7866              tpa.business_group_id,
7867              tpa.legal_entity_id,
7868              tpa.legal_entity,
7869              tpa.disability,
7870              tpa.employment_type,
7871              tpa.meaning_code,
7872              tpa.occupational_level,
7873              sum(tpa.male_african)    MA,
7874              sum(tpa.male_coloured)   MC,
7875              sum(tpa.male_indian)     MI,
7876              sum(tpa.male_white)      MW,
7877              sum(tpa.female_african)  FA,
7878              sum(tpa.female_coloured) FC,
7879              sum(tpa.female_indian)   FI,
7880              sum(tpa.female_white)    FW,
7881              sum(tpa.total)           total
7882       from
7883       (
7884          select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
7885                     decode(papf.PER_INFORMATION11,null,null,
7886                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
7887                                -to_char(l_nat_date,'YYYYMMDD'))
7888                            ,-1,null,'F')))     report_code,
7889                 p_report_date                                                                                                                                       reporting_date,
7890                 paaf.business_group_id,
7891                 paei.aei_information7                                                                                                                               legal_entity_id,
7892                 haou.name                                                                                                                                           legal_entity,
7893                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability, -- 3962073
7894                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
7895                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id)                                                         employment_type, -- Bug 3962073
7896                 hl.lookup_code                                                                                                                                      meaning_code,
7897                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level') occupational_level,
7898                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
7899                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
7900                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
7901                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
7902                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
7903                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
7904                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
7905                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
7906                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
7907                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
7908                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
7909                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
7910                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
7911                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
7912                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
7913                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
7914          from   hr_lookups                hl,
7915                 hr_lookups                hl1,
7916 --                hr_lookups                hl2,
7917                 hr_all_organization_units haou,
7918                 per_assignment_extra_info paei,
7919                 per_all_assignments_f     paaf,
7920                 per_all_people_f          papf
7921          where  papf.business_group_id = p_business_group_id
7922          and    papf.current_employee_flag = 'Y'
7923          and    p_report_date between papf.effective_start_date and papf.effective_end_date
7924          and    paaf.person_id = papf.person_id
7925          and    paaf.primary_flag = 'Y'
7926          and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
7927          and    paei.assignment_id = paaf.assignment_id
7928          and    paei.information_type = 'ZA_SPECIFIC_INFO'
7929          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
7930          and    paei.aei_information7 is not null
7931          and    nvl(paei.aei_information6, 'N') <> 'Y'
7932          and    haou.organization_id = paei.aei_information7
7933          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
7934          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
7935          AND    hl.lookup_code <> '15' -- Not Applicable.
7936          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
7937          AND    hl1.lookup_code = '1' -- Operation / core function
7938          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
7939 --         and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
7940 --         and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
7941 --         AND    hl2.lookup_code <> '15' -- Not Applicable.
7942          group  by paaf.business_group_id,
7943                 paei.aei_information7,
7944                 haou.name,
7945                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
7946                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)),
7947                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id),
7948                 hl.lookup_code,
7949                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level'),
7950                 p_report_code ||  decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
7951                     decode(papf.PER_INFORMATION11,null,null,
7952                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
7953                                -to_char(l_nat_date,'YYYYMMDD'))
7954                            ,-1,null,'F')))
7955       ) tpa
7956       group  by tpa.report_code,
7957              tpa.reporting_date,
7958              tpa.business_group_id,
7959              tpa.legal_entity_id,
7960              tpa.legal_entity,
7961              tpa.disability,
7962              tpa.employment_type,
7963              tpa.meaning_code,
7964              tpa.occupational_level;
7965 
7966       commit;
7967 
7968       -- Inserts non-associated occupational levels with zero values
7969       insert into per_za_employment_equity
7970       (
7971          report_id,
7972          reporting_date,
7973          business_group_id,
7974          legal_entity_id,
7975          legal_entity,
7976          disability,
7977          employment_type,
7978          level_cat_code,
7979          level_cat,
7980          MA,
7981          MC,
7982          MI,
7983          MW,
7984          FA,
7985          FC,
7986          FI,
7987          FW,
7988          total
7989       )
7990       select 'EQ3'            report_id,
7991              p_report_date    reporting_date,
7992              p_business_group_id business_group_id,
7993              haou.organization_id legal_entity_id,
7994              haou.name        legal_entity,
7995              'Y'              disability,
7996              'Permanent'      employment_type,
7997              hl.lookup_code   level_cat_code,
7998              hl.meaning       level_cat,
7999              0                MA,
8000              0                MC,
8001              0                MI,
8002              0                MW,
8003              0                FA,
8004              0                FC,
8005              0                FI,
8006              0                FW,
8007              0                total
8008       from   hr_lookups hl
8009          ,   hr_all_organization_units haou
8010       where not exists
8011       (
8012          select 'X'
8013          from   per_za_employment_equity pzee
8014          where  pzee.level_cat_code    = hl.lookup_code
8015          and    pzee.report_id         = 'EQ3'
8016          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
8017          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8018          and    pzee.disability        = 'Y'
8019          and    pzee.employment_type   = 'Permanent'
8020       )
8021       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8022       and haou.business_group_id = p_business_group_id          --Bug 4872110
8023       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8024 
8025       -- Inserts non-associated occupational levels with zero values
8026       insert into per_za_employment_equity
8027       (
8028          report_id,
8029          reporting_date,
8030          business_group_id,
8031          legal_entity_id,
8032          legal_entity,
8033          disability,
8034          employment_type,
8035          level_cat_code,
8036          level_cat,
8037          MA,
8038          MC,
8039          MI,
8040          MW,
8041          FA,
8042          FC,
8043          FI,
8044          FW,
8045          total
8046       )
8047       select 'EQ3F'            report_id,
8048              p_report_date    reporting_date,
8049              p_business_group_id business_group_id,
8050              haou.organization_id legal_entity_id,
8051              haou.name        legal_entity,
8052              'Y'              disability,
8053              'Permanent'      employment_type,
8054              hl.lookup_code   level_cat_code,
8055              hl.meaning       level_cat,
8056              0                MA,
8057              0                MC,
8058              0                MI,
8059              0                MW,
8060              0                FA,
8061              0                FC,
8062              0                FI,
8063              0                FW,
8064              0                total
8065       from   hr_lookups hl
8066          ,   hr_all_organization_units haou
8067       where not exists
8068       (
8069          select 'X'
8070          from   per_za_employment_equity pzee
8071          where  pzee.level_cat_code    = hl.lookup_code
8072          and    pzee.report_id         = 'EQ3F'
8073          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
8074          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8075          and    pzee.disability        = 'Y'
8076          and    pzee.employment_type   = 'Permanent'
8077       )
8078       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8079       and haou.business_group_id = p_business_group_id          --Bug 4872110
8080       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8081 
8082       commit;
8083 
8084    -- Note EQ4 is for the following report:
8085    --    2.3.1. Operational / core Functiona (report the total number of new recruits into each occupational level during
8086    --       the twelve months preceding this report)
8087    elsif p_report_code = 'EQ4' then
8088 
8089       -- Populate with New Hires
8090       insert into per_za_employment_equity
8091       (
8092          report_id,
8093          reporting_date,
8094          business_group_id,
8095          legal_entity_id,
8096          legal_entity,
8097          disability,
8098          employment_type,
8099          level_cat_code,
8100          level_cat,
8101          MA,
8102          MC,
8103          MI,
8104          MW,
8105          FA,
8106          FC,
8107          FI,
8108          FW,
8109          total
8110       )
8111       select tpa.report_code,
8112              tpa.reporting_date,
8113              tpa.business_group_id,
8114              tpa.legal_entity_id,
8115              tpa.legal_entity,
8116              tpa.disability,
8117              tpa.employment_type,
8118              tpa.meaning_code,
8119              tpa.occupational_level,
8120              sum(tpa.male_african)    MA,
8121              sum(tpa.male_coloured)   MC,
8122              sum(tpa.male_indian)     MI,
8123              sum(tpa.male_white)      MW,
8124              sum(tpa.female_african)  FA,
8125              sum(tpa.female_coloured) FC,
8126              sum(tpa.female_indian)   FI,
8127              sum(tpa.female_white)    FW,
8128              sum(tpa.total)           total
8129       from
8130       (
8131          select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8132                     decode(papf.PER_INFORMATION11,null,null,
8133                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8134                                -to_char(l_nat_date,'YYYYMMDD'))
8135                            ,-1,null,'F')))     report_code,
8136                 p_report_date                                                                                                                                       reporting_date,
8137                 paaf.business_group_id,
8138                 paei.aei_information7                                                                                                                               legal_entity_id,
8139                 haou.name                                                                                                                                           legal_entity,
8140                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability, -- 3962073
8141                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
8142                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id)                                                         employment_type, -- Bug 3962073
8143                 hl.lookup_code                                                                                                                                      meaning_code,
8144                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level') occupational_level,
8145                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
8146                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
8147                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
8148                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
8149                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
8150                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
8151                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
8152                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
8153                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8154                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
8155                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
8156                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
8157                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8158                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
8159                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
8160                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
8161          from   hr_lookups                hl,
8162                 hr_lookups                hl1,
8163 --                hr_lookups                hl2,
8164                 hr_all_organization_units haou,
8165                 per_assignment_extra_info paei,
8166                 per_all_assignments_f     paaf,
8167                 per_all_people_f          papf
8168          where  papf.business_group_id = p_business_group_id
8169          and    papf.current_employee_flag = 'Y'
8170          and    p_report_date between papf.effective_start_date and papf.effective_end_date
8171          and    paaf.person_id = papf.person_id
8172          and    paaf.primary_flag = 'Y'
8173          and    p_report_date between paaf.effective_start_date and paaf.effective_end_date
8174          and    paei.assignment_id = paaf.assignment_id
8175          and    paei.information_type = 'ZA_SPECIFIC_INFO'
8176          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
8177          and    paei.aei_information7 is not null
8178          and    nvl(paei.aei_information6, 'N') <> 'Y'
8179          and    haou.organization_id = paei.aei_information7
8180          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8181          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8182          AND    hl.lookup_code <> '15' -- Not Applicable.
8183          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
8184          AND    hl1.lookup_code = '2' -- Support function
8185          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8186 --         and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
8187 --         and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
8188 --         AND    hl2.lookup_code <> '15' -- Not Applicable.
8189          group  by paaf.business_group_id,
8190                 paei.aei_information7,
8191                 haou.name,
8192                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
8193                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)),
8194                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id),
8195                 hl.lookup_code,
8196                 nvl(per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level'),
8197                 p_report_code ||  decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8198                     decode(papf.PER_INFORMATION11,null,null,
8199                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8200                                -to_char(l_nat_date,'YYYYMMDD'))
8201                            ,-1,null,'F')))
8202       ) tpa
8203       group  by tpa.report_code,
8204              tpa.reporting_date,
8205              tpa.business_group_id,
8206              tpa.legal_entity_id,
8207              tpa.legal_entity,
8208              tpa.disability,
8209              tpa.employment_type,
8210              tpa.meaning_code,
8211              tpa.occupational_level;
8212 
8213       commit;
8214 
8215       -- Inserts non-associated occupational levels with zero values
8216       insert into per_za_employment_equity
8217       (
8218          report_id,
8219          reporting_date,
8220          business_group_id,
8221          legal_entity_id,
8222          legal_entity,
8223          disability,
8224          employment_type,
8225          level_cat_code,
8226          level_cat,
8227          MA,
8228          MC,
8229          MI,
8230          MW,
8231          FA,
8232          FC,
8233          FI,
8234          FW,
8235          total
8236       )
8237       select 'EQ4'            report_id,
8238              p_report_date    reporting_date,
8239              p_business_group_id business_group_id,
8240              haou.organization_id legal_entity_id,
8241              haou.name        legal_entity,
8242              'Y'              disability,
8243              'Permanent'      employment_type,
8244              hl.lookup_code   level_cat_code,
8245              hl.meaning       level_cat,
8246              0                MA,
8247              0                MC,
8248              0                MI,
8249              0                MW,
8250              0                FA,
8251              0                FC,
8252              0                FI,
8253              0                FW,
8254              0                total
8255       from   hr_lookups hl
8256          ,   hr_all_organization_units haou
8257       where not exists
8258       (
8259          select 'X'
8260          from   per_za_employment_equity pzee
8261          where  pzee.level_cat_code    = hl.lookup_code
8262          and    pzee.report_id         = 'EQ4'
8263          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
8264          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8265          and    pzee.disability        = 'Y'
8266          and    pzee.employment_type   = 'Permanent'
8267       )
8268       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8269       and haou.business_group_id = p_business_group_id          --Bug 4872110
8270       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8271 
8272       -- Inserts non-associated occupational levels with zero values
8273       insert into per_za_employment_equity
8274       (
8275          report_id,
8276          reporting_date,
8277          business_group_id,
8278          legal_entity_id,
8279          legal_entity,
8280          disability,
8281          employment_type,
8282          level_cat_code,
8283          level_cat,
8284          MA,
8285          MC,
8286          MI,
8287          MW,
8288          FA,
8289          FC,
8290          FI,
8291          FW,
8292          total
8293       )
8294       select 'EQ4F'            report_id,
8295              p_report_date    reporting_date,
8296              p_business_group_id business_group_id,
8297              haou.organization_id legal_entity_id,
8298              haou.name        legal_entity,
8299              'Y'              disability,
8300              'Permanent'      employment_type,
8301              hl.lookup_code   level_cat_code,
8302              hl.meaning       level_cat,
8303              0                MA,
8304              0                MC,
8305              0                MI,
8306              0                MW,
8307              0                FA,
8308              0                FC,
8309              0                FI,
8310              0                FW,
8311              0                total
8312       from   hr_lookups hl
8313          ,   hr_all_organization_units haou
8314       where not exists
8315       (
8316          select 'X'
8317          from   per_za_employment_equity pzee
8318          where  pzee.level_cat_code    = hl.lookup_code
8319          and    pzee.report_id         = 'EQ4F'
8320          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
8321          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8322          and    pzee.disability        = 'Y'
8323          and    pzee.employment_type   = 'Permanent'
8324       )
8325       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8326       and haou.business_group_id = p_business_group_id          --Bug 4872110
8327       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8328 
8329       commit;
8330 
8331 -- End for Emplyment Equity enhancement
8332 
8333 
8334    -- Note EQ5 is for the following report:
8335    --    6. Recruitment (report the total number of new recruits into each occupational level during
8336    --       the twelve months preceding this report)
8337    elsif p_report_code = 'EQ5' then
8338 
8339       -- Populate with New Hires
8340       insert into per_za_employment_equity
8341       (
8342          report_id,
8343          reporting_date,
8344          business_group_id,
8345          legal_entity_id,
8346          legal_entity,
8347          disability,
8348          employment_type,
8349          level_cat_code,
8350          level_cat,
8351          MA,
8352          MC,
8353          MI,
8354          MW,
8355          FA,
8356          FC,
8357          FI,
8358          FW,
8359          total
8360       )
8361       select tpa.report_code,
8362              tpa.reporting_date,
8363              tpa.business_group_id,
8364              tpa.legal_entity_id,
8365              tpa.legal_entity,
8366              tpa.disability,
8367              tpa.employment_type,
8368              tpa.meaning_code,
8369              tpa.occupational_level,
8370              sum(tpa.male_african)    MA,
8371              sum(tpa.male_coloured)   MC,
8372              sum(tpa.male_indian)     MI,
8373              sum(tpa.male_white)      MW,
8374              sum(tpa.female_african)  FA,
8375              sum(tpa.female_coloured) FC,
8376              sum(tpa.female_indian)   FI,
8377              sum(tpa.female_white)    FW,
8378              sum(tpa.total)           total
8379       from
8380       (
8381          select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8382                     decode(papf.PER_INFORMATION11,null,null,
8383                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8384                                -to_char(l_nat_date,'YYYYMMDD'))
8385                            ,-1,null,'F')))     report_code,
8386                 p_report_date                                                                                                                                       reporting_date,
8387                 paaf.business_group_id,
8388                 paei.aei_information7                                                                                                                               legal_entity_id,
8389                 haou.name                                                                                                                                           legal_entity,
8390                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability, -- 3962073
8391                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
8392                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id)                                                         employment_type, -- Bug 3962073
8393                 hl.lookup_code                                                                                                                                      meaning_code,
8394                 nvl(per_za_employment_equity_pkg.get_occupational_level(ppos.date_start, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level') occupational_level,
8395                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
8396                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
8397                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
8398                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
8399                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
8400                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
8401                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
8402                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
8403                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8404                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
8405                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
8406                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
8407                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8408                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
8409                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
8410                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
8411          from   hr_lookups                hl,
8412                 hr_lookups                hl1,
8413 --                hr_lookups                hl2,
8414                 hr_all_organization_units haou,
8415                 per_assignment_extra_info paei,
8416                 per_all_assignments_f     paaf,
8417                 per_periods_of_service    ppos,
8418                 per_all_people_f          papf
8419          where  papf.business_group_id = p_business_group_id
8420          and    papf.current_employee_flag = 'Y'
8421          and    ppos.person_id = papf.person_id
8422          and    ppos.date_start between add_months(p_report_date, -12) + 1 and p_report_date
8423          and    papf.effective_start_date = ppos.date_start
8424          and    paaf.person_id = papf.person_id
8425          and    paaf.primary_flag = 'Y'
8426          and    paaf.effective_start_date = ppos.date_start
8427          and    paei.assignment_id = paaf.assignment_id
8428          and    paei.information_type = 'ZA_SPECIFIC_INFO'
8429          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
8430          and    paei.aei_information7 is not null
8431          and    nvl(paei.aei_information6, 'N') <> 'Y'
8432          and    haou.organization_id = paei.aei_information7
8433          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8434          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(ppos.date_start, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8435          AND    hl.lookup_code <> '15' -- Not Applicable.
8436          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
8437          and    hl1.lookup_code <> '15' -- Not Applicable.
8438          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8439 --         and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
8440 --         and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
8441 --         AND    hl2.lookup_code <> '15' -- Not Applicable.
8442          group  by paaf.business_group_id,
8443                 paei.aei_information7,
8444                 haou.name,
8445                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
8446                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(p_report_date, paaf.period_of_service_id)),
8447                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(ppos.date_start, paaf.period_of_service_id),
8448                 hl.lookup_code,
8449                 nvl(per_za_employment_equity_pkg.get_occupational_level(ppos.date_start, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level'),
8450                 p_report_code ||  decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8451                     decode(papf.PER_INFORMATION11,null,null,
8452                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8453                                -to_char(l_nat_date,'YYYYMMDD'))
8454                            ,-1,null,'F')))
8455       ) tpa
8456       group  by tpa.report_code,
8457              tpa.reporting_date,
8458              tpa.business_group_id,
8459              tpa.legal_entity_id,
8460              tpa.legal_entity,
8461              tpa.disability,
8462              tpa.employment_type,
8463              tpa.meaning_code,
8464              tpa.occupational_level;
8465 
8466       commit;
8467 
8468       -- Inserts non-associated occupational levels with zero values
8469       insert into per_za_employment_equity
8470       (
8471          report_id,
8472          reporting_date,
8473          business_group_id,
8474          legal_entity_id,
8475          legal_entity,
8476          disability,
8477          employment_type,
8478          level_cat_code,
8479          level_cat,
8480          MA,
8481          MC,
8482          MI,
8483          MW,
8484          FA,
8485          FC,
8486          FI,
8487          FW,
8488          total
8489       )
8490       select 'EQ5'            report_id,
8491              p_report_date    reporting_date,
8492              p_business_group_id business_group_id,
8493              haou.organization_id legal_entity_id,
8494              haou.name        legal_entity,
8495              'Y'              disability,
8496              'Permanent'      employment_type,
8497              hl.lookup_code   level_cat_code,
8498              hl.meaning       level_cat,
8499              0                MA,
8500              0                MC,
8501              0                MI,
8502              0                MW,
8503              0                FA,
8504              0                FC,
8505              0                FI,
8506              0                FW,
8507              0                total
8508       from   hr_lookups hl
8509          ,   hr_all_organization_units haou
8510       where not exists
8511       (
8512          select 'X'
8513          from   per_za_employment_equity pzee
8514          where  pzee.level_cat_code    = hl.lookup_code
8515          and    pzee.report_id         = 'EQ5'
8516          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
8517          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8518          and    pzee.disability        = 'Y'
8519          and    pzee.employment_type   = 'Permanent'
8520       )
8521       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8522       and haou.business_group_id = p_business_group_id          --Bug 4872110
8523       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8524 
8525       -- Inserts non-associated occupational levels with zero values
8526       insert into per_za_employment_equity
8527       (
8528          report_id,
8529          reporting_date,
8530          business_group_id,
8531          legal_entity_id,
8532          legal_entity,
8533          disability,
8534          employment_type,
8535          level_cat_code,
8536          level_cat,
8537          MA,
8538          MC,
8539          MI,
8540          MW,
8541          FA,
8542          FC,
8543          FI,
8544          FW,
8545          total
8546       )
8547       select 'EQ5F'            report_id,
8548              p_report_date    reporting_date,
8549              p_business_group_id business_group_id,
8550              haou.organization_id legal_entity_id,
8551              haou.name        legal_entity,
8552              'Y'              disability,
8553              'Permanent'      employment_type,
8554              hl.lookup_code   level_cat_code,
8555              hl.meaning       level_cat,
8556              0                MA,
8557              0                MC,
8558              0                MI,
8559              0                MW,
8560              0                FA,
8561              0                FC,
8562              0                FI,
8563              0                FW,
8564              0                total
8565       from   hr_lookups hl
8566          ,   hr_all_organization_units haou
8567       where not exists
8568       (
8569          select 'X'
8570          from   per_za_employment_equity pzee
8571          where  pzee.level_cat_code    = hl.lookup_code
8572          and    pzee.report_id         = 'EQ5F'
8573          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
8574          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8575          and    pzee.disability        = 'Y'
8576          and    pzee.employment_type   = 'Permanent'
8577       )
8578       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8579       and haou.business_group_id = p_business_group_id          --Bug 4872110
8580       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8581 
8582       commit;
8583 
8584    -- Note EQ6 is for the following report:
8585    --    7. Promotion (report the total number of promotions into each occupational level during
8586    --       the twelve months preceding this report)
8587    elsif p_report_code = 'EQ6' then
8588 
8589       -- Populate with Promotions
8590       insert into per_za_employment_equity
8591       (
8592          report_id,
8593          reporting_date,
8594          business_group_id,
8595          legal_entity_id,
8596          legal_entity,
8597          disability,
8598          employment_type,
8599          level_cat_code,
8600          level_cat,
8601          MA,
8602          MC,
8603          MI,
8604          MW,
8605          FA,
8606          FC,
8607          FI,
8608          FW,
8609          total
8610       )
8611       select p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8612                                 decode(papf.PER_INFORMATION11,null,null,
8613                                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8614                                        - to_char(l_nat_date,'YYYYMMDD'))
8615                                     ,-1,null,'F')))     report_code,
8616              p_report_date                                                              reporting_date,
8617              paaf.business_group_id,
8618              paei.aei_information7                                                      legal_entity_id,
8619              haou.name                                                                  legal_entity,
8620              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)     disability, --3962073
8621              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
8622              -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id) employment_type,
8623              hl.lookup_code                                                             lookup_code,
8624              nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level') occupational_level,
8625              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
8626              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
8627              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
8628              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
8629              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
8630              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
8631              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
8632              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
8633              sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8634              sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
8635              sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
8636              sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
8637              sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8638              sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
8639              sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
8640              sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
8641       from   hr_lookups                hl,
8642              hr_lookups                hl1,
8643 --             hr_lookups                hl2,
8644              hr_all_organization_units haou,
8645              per_assignment_extra_info paei,
8646              per_all_assignments_f     paaf,
8647              per_periods_of_service    ppos,
8648              per_all_people_f          papf
8649       where  papf.business_group_id = p_business_group_id
8650       and    papf.current_employee_flag = 'Y'
8651       and    ppos.person_id = papf.person_id
8652       and    nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY')) > add_months(p_report_date, -12) + 1
8653       and    ppos.date_start < p_report_date
8654       and    papf.effective_start_date = ppos.date_start
8655       and    paaf.person_id = papf.person_id
8656       and    paaf.primary_flag = 'Y'
8657       and    paaf.effective_start_date between ppos.date_start and nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
8658       and    paaf.effective_start_date > add_months(p_report_date, -12) + 1
8659       and    paaf.effective_start_date <= p_report_date
8660       and    paei.assignment_id = paaf.assignment_id
8661       and    paei.information_type = 'ZA_SPECIFIC_INFO'
8662       and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
8663       and    paei.aei_information7 is not null
8664       and    nvl(paei.aei_information6, 'N') <> 'Y'
8665       and    haou.organization_id = paei.aei_information7
8666       and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8667       and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8668       AND    hl.lookup_code <> '15' -- Not Applicable.
8669       and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
8670       and    hl1.lookup_code <> '15' -- Not Applicable.
8671       and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8672 --      and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
8673 --      and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
8674 --      AND    hl2.lookup_code <> '15' -- Not Applicable.
8675       and    nvl(per_za_employment_equity_pkg.get_lookup_code(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)), '9999999999') <
8676       any
8677       (
8678          select per_za_employment_equity_pkg.get_lookup_code(per_za_employment_equity_pkg.get_occupational_level(paaf1.effective_start_date, paaf1.assignment_id, paaf1.job_id, paaf1.grade_id, paaf1.position_id, paaf.business_group_id,2009)) lookup_code
8679          from   per_all_assignments_f paaf1
8680          where  paaf1.person_id = papf.person_id
8681          and    paaf1.primary_flag = 'Y'
8682          and    per_za_employment_equity_pkg.get_lookup_code(per_za_employment_equity_pkg.get_occupational_level(paaf1.effective_start_date, paaf1.assignment_id, paaf1.job_id, paaf1.grade_id, paaf1.position_id, paaf.business_group_id,2009)) is not null
8683          and    paaf1.effective_end_date + 1 = paaf.effective_start_date
8684       )
8685       group  by paaf.business_group_id,
8686              paei.aei_information7,
8687              haou.name,
8688              decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
8689              nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id)),
8690              -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_start_date, paaf.period_of_service_id),
8691              hl.lookup_code,
8692              nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_start_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level'),
8693              p_report_code   || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8694                     decode(papf.PER_INFORMATION11,null,null,
8695                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8696                                -to_char(l_nat_date,'YYYYMMDD'))
8697                            ,-1,null,'F'))) ;
8698 
8699       commit;
8700 
8701       -- Inserts non-associated occupational levels with zero values
8702       insert into per_za_employment_equity
8703       (
8704          report_id,
8705          reporting_date,
8706          business_group_id,
8707          legal_entity_id,
8708          legal_entity,
8709          disability,
8710          employment_type,
8711          level_cat_code,
8712          level_cat,
8713          MA,
8714          MC,
8715          MI,
8716          MW,
8717          FA,
8718          FC,
8719          FI,
8720          FW,
8721          total
8722       )
8723       select 'EQ6'            report_id,
8724              p_report_date    reporting_date,
8725              p_business_group_id business_group_id,
8726              haou.organization_id legal_entity_id,
8727              haou.name        legal_entity,
8728              'Y'              disability,
8729              'Permanent'      employment_type,
8730              hl.lookup_code   level_cat_code,
8731              hl.meaning       level_cat,
8732              0                MA,
8733              0                MC,
8734              0                MI,
8735              0                MW,
8736              0                FA,
8737              0                FC,
8738              0                FI,
8739              0                FW,
8740              0                total
8741       from   hr_lookups hl
8742          ,   hr_all_organization_units haou
8743       where not exists
8744       (
8745          select 'X'
8746          from   per_za_employment_equity pzee
8747          where  pzee.level_cat_code    = hl.lookup_code
8748          and    pzee.report_id         = 'EQ6'
8749          and    pzee.business_group_id = p_business_group_id   --Bug 4872110
8750          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8751          and    pzee.disability        = 'Y'
8752          and    pzee.employment_type   = 'Permanent'
8753       )
8754       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8755       and haou.business_group_id = p_business_group_id          --Bug 4872110
8756       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8757 
8758       -- Inserts non-associated occupational levels with zero values
8759       insert into per_za_employment_equity
8760       (
8761          report_id,
8762          reporting_date,
8763          business_group_id,
8764          legal_entity_id,
8765          legal_entity,
8766          disability,
8767          employment_type,
8768          level_cat_code,
8769          level_cat,
8770          MA,
8771          MC,
8772          MI,
8773          MW,
8774          FA,
8775          FC,
8776          FI,
8777          FW,
8778          total
8779       )
8780       select 'EQ6F'            report_id,
8781              p_report_date    reporting_date,
8782              p_business_group_id business_group_id,
8783              haou.organization_id legal_entity_id,
8784              haou.name        legal_entity,
8785              'Y'              disability,
8786              'Permanent'      employment_type,
8787              hl.lookup_code   level_cat_code,
8788              hl.meaning       level_cat,
8789              0                MA,
8790              0                MC,
8791              0                MI,
8792              0                MW,
8793              0                FA,
8794              0                FC,
8795              0                FI,
8796              0                FW,
8797              0                total
8798       from   hr_lookups hl
8799          ,   hr_all_organization_units haou
8800       where not exists
8801       (
8802          select 'X'
8803          from   per_za_employment_equity pzee
8804          where  pzee.level_cat_code    = hl.lookup_code
8805          and    pzee.report_id         = 'EQ6F'
8806          and    pzee.business_group_id = p_business_group_id   --Bug 4872110
8807          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
8808          and    pzee.disability        = 'Y'
8809          and    pzee.employment_type   = 'Permanent'
8810       )
8811       and hl.lookup_type         = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8812       and haou.business_group_id = p_business_group_id          --Bug 4872110
8813       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
8814 
8815 
8816       commit;
8817 
8818    -- Note EQ5 is for the following report:
8819    --    8.1 Termination (report the total number of terminations in each occupational level during
8820    --        the twelve months preceding this report)
8821    elsif p_report_code = 'EQ7' then
8822 
8823       -- Populate with Terminations
8824       insert into per_za_employment_equity
8825       (
8826          report_id,
8827          reporting_date,
8828          business_group_id,
8829          legal_entity_id,
8830          legal_entity,
8831          disability,
8832          employment_type,
8833          level_cat_code,
8834          level_cat,
8835          MA,
8836          MC,
8837          MI,
8838          MW,
8839          FA,
8840          FC,
8841          FI,
8842          FW,
8843          total
8844       )
8845       select tpa.report_code,
8846              tpa.reporting_date,
8847              tpa.business_group_id,
8848              tpa.legal_entity_id,
8849              tpa.legal_entity,
8850              tpa.disability,
8851              tpa.employment_type,
8852              tpa.meaning_code,
8853              tpa.occupational_level,
8854              sum(tpa.male_african)    MA,
8855              sum(tpa.male_coloured)   MC,
8856              sum(tpa.male_indian)     MI,
8857              sum(tpa.male_white)      MW,
8858              sum(tpa.female_african)  FA,
8859              sum(tpa.female_coloured) FC,
8860              sum(tpa.female_indian)   FI,
8861              sum(tpa.female_white)    FW,
8862              sum(tpa.total)           total
8863       from
8864       (
8865          select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8866                     decode(papf.PER_INFORMATION11,null,null,
8867                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8868                                -to_char(l_nat_date,'YYYYMMDD'))
8869                            ,-1,null,'F'))
8870                     )       report_code,
8871                 p_report_date                                                              reporting_date,
8872                 paaf.business_group_id,
8873                 paei.aei_information7                                                      legal_entity_id,
8874                 haou.name                                                                  legal_entity,
8875                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)       disability,  --3962073
8876                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
8877                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id) employment_type, -- Bug 3962073
8878                 hl.lookup_code                                                             meaning_code,
8879                 nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_end_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level') occupational_level,
8880                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
8881                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
8882                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
8883                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
8884                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
8885                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
8886                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
8887                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
8888                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8889                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
8890                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
8891                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
8892                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
8893                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
8894                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
8895                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
8896          from   hr_lookups                hl,
8897                 hr_lookups                hl1,
8898 --                hr_lookups                hl2,
8899                 hr_all_organization_units haou,
8900                 per_assignment_extra_info paei,
8901                 per_all_assignments_f     paaf,
8902                 per_periods_of_service    ppos,
8903                 per_all_people_f          papf
8904          where  papf.business_group_id = p_business_group_id
8905          and    papf.current_employee_flag = 'Y'
8906          and    ppos.person_id = papf.person_id
8907          and    nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY')) between add_months(p_report_date, -12) + 1 and p_report_date
8908          and    papf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
8909          and    paaf.person_id = papf.person_id
8910          and    paaf.primary_flag = 'Y'
8911          and    paaf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
8912          and    paei.assignment_id = paaf.assignment_id
8913          and    paei.information_type = 'ZA_SPECIFIC_INFO'
8914          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
8915          and    paei.aei_information7 is not null
8916          and    nvl(paei.aei_information6, 'N') <> 'Y'
8917          and    haou.organization_id = paei.aei_information7
8918          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
8919          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(paaf.effective_end_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8920          AND    hl.lookup_code <> '15' -- Not Applicable.
8921          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
8922          and    hl1.lookup_code <> '15' -- Not Applicable
8923          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
8924 --         and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
8925 --         and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
8926 --         AND    hl2.lookup_code <> '15' -- Not Applicable.
8927          group  by paaf.business_group_id,
8928                 paei.aei_information7,
8929                 haou.name,
8930                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
8931                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)),
8932                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id),
8933                 hl.lookup_code,
8934                 nvl(per_za_employment_equity_pkg.get_occupational_level(paaf.effective_end_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009), 'No Occupational Level'),
8935                 p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
8936                     decode(papf.PER_INFORMATION11,null,null,
8937                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
8938                                -to_char(l_nat_date,'YYYYMMDD'))
8939                            ,-1,null,'F'))
8940                     )
8941       ) tpa
8942       group  by tpa.report_code,
8943              tpa.reporting_date,
8944              tpa.business_group_id,
8945              tpa.legal_entity_id,
8946              tpa.legal_entity,
8947              tpa.disability,
8948              tpa.employment_type,
8949              tpa.meaning_code,
8950              tpa.occupational_level;
8951 
8952       commit;
8953 
8954       -- Inserts non-associated occupational levels with zero values
8955       insert into per_za_employment_equity
8956       (
8957          report_id,
8958          reporting_date,
8959          business_group_id,
8960          legal_entity_id,
8961          legal_entity,
8962          disability,
8963          employment_type,
8964          level_cat_code,
8965          level_cat,
8966          MA,
8967          MC,
8968          MI,
8969          MW,
8970          FA,
8971          FC,
8972          FI,
8973          FW,
8974          total
8975       )
8976       select 'EQ7'            report_id,
8977              p_report_date    reporting_date,
8978              p_business_group_id business_group_id,
8979              haou.organization_id legal_entity_id,
8980              haou.name        legal_entity,
8981              'Y'              disability,
8982              'Permanent'      employment_type,
8983              hl.lookup_code   level_cat_code,
8984              hl.meaning       level_cat,
8985              0                MA,
8986              0                MC,
8987              0                MI,
8988              0                MW,
8989              0                FA,
8990              0                FC,
8991              0                FI,
8992              0                FW,
8993              0                total
8994       from   hr_lookups hl
8995          ,   hr_all_organization_units haou
8996       where not exists
8997       (
8998          select 'X'
8999          from   per_za_employment_equity pzee
9000          where  pzee.level_cat_code    = hl.lookup_code
9001          and    pzee.report_id         = 'EQ7'
9002          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
9003          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
9004          and    pzee.disability        = 'Y'
9005          and    pzee.employment_type   = 'Permanent'
9006       )
9007       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
9008       and haou.business_group_id = p_business_group_id  --Bug 4872110
9009       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
9010 
9011       insert into per_za_employment_equity
9012       (
9013          report_id,
9014          reporting_date,
9015          business_group_id,
9016          legal_entity_id,
9017          legal_entity,
9018          disability,
9019          employment_type,
9020          level_cat_code,
9021          level_cat,
9022          MA,
9023          MC,
9024          MI,
9025          MW,
9026          FA,
9027          FC,
9028          FI,
9029          FW,
9030          total
9031       )
9032       select 'EQ7F'            report_id,
9033              p_report_date    reporting_date,
9034              p_business_group_id business_group_id,
9035              haou.organization_id legal_entity_id,
9036              haou.name        legal_entity,
9037              'Y'              disability,
9038              'Permanent'      employment_type,
9039              hl.lookup_code   level_cat_code,
9040              hl.meaning       level_cat,
9041              0                MA,
9042              0                MC,
9043              0                MI,
9044              0                MW,
9045              0                FA,
9046              0                FC,
9047              0                FI,
9048              0                FW,
9049              0                total
9050       from   hr_lookups hl
9051          ,   hr_all_organization_units haou
9052       where not exists
9053       (
9054          select 'X'
9055          from   per_za_employment_equity pzee
9056          where  pzee.level_cat_code    = hl.lookup_code
9057          and    pzee.report_id         = 'EQ7F'
9058          and    pzee.business_group_id = p_business_group_id            --Bug 4872110
9059          and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
9060          and    pzee.disability        = 'Y'
9061          and    pzee.employment_type   = 'Permanent'
9062       )
9063       and    hl.lookup_type      = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
9064       and haou.business_group_id = p_business_group_id  --Bug 4872110
9065       and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
9066 
9067       commit;
9068 
9069    -- Note EQ6 is for the following report:
9070    --    8.2 Termination Categories (report the total number of terminations in each termination
9071    --        category during the twelve months preceding this report)
9072    elsif p_report_code = 'EQ8' then
9073 
9074       -- Populate with Termination Reason totals
9075       insert into per_za_employment_equity
9076       (
9077          report_id,
9078          reporting_date,
9079          business_group_id,
9080          legal_entity_id,
9081          legal_entity,
9082          disability,
9083          employment_type,
9084          level_cat_code,
9085          level_cat,
9086          MA,
9087          MC,
9088          MI,
9089          MW,
9090          FA,
9091          FC,
9092          FI,
9093          FW,
9094          total
9095       )
9096       select tpa.report_code,
9097              tpa.reporting_date,
9098              tpa.business_group_id,
9099              tpa.legal_entity_id,
9100              tpa.legal_entity,
9101              tpa.disability,
9102              tpa.employment_type,
9103              decode
9104              (
9105                 tpa.termination_reason,
9106                 'Resignation', 1,
9107                 'Non-Renewal of Contract', 2,
9108                 'Dismissal - Operational Requirements', 3,
9109                 'Dismissal - Misconduct', 4,
9110                 'Dismissal - Incapacity', 5,
9111                 --'Other', 6,
9112                 'Retirement', 6,
9113                 'Death', 7,
9114                 null
9115              )  meaning_code,
9116              /*decode
9117              (
9118                 tpa.termination_reason,
9119                 'Dismissal - Operational Requirements', 'retrenchment -Operational requirements',
9120                 tpa.termination_reason
9121              ),*/
9122              tpa.termination_reason,
9123              sum(tpa.male_african)    MA,
9124              sum(tpa.male_coloured)   MC,
9125              sum(tpa.male_indian)     MI,
9126              sum(tpa.male_white)      MW,
9127              sum(tpa.female_african)  FA,
9128              sum(tpa.female_coloured) FC,
9129              sum(tpa.female_indian)   FI,
9130              sum(tpa.female_white)    FW,
9131              sum(tpa.total)           total
9132       from
9133       (
9134          select p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
9135                     decode(papf.PER_INFORMATION11,null,null,
9136                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
9137                                -to_char(l_nat_date,'YYYYMMDD'))
9138                            ,-1,null,'F'))
9139                     )      report_code,
9140                 p_report_date                                                              reporting_date,
9141                 paaf.business_group_id,
9142                 paei.aei_information7                                                      legal_entity_id,
9143                 haou.name                                                                  legal_entity,
9144                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag)     disability, --3962073
9145                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)) employment_type, -- Bug 3962073
9146                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id) employment_type, -- Bug 3962073
9147                 ppos.leaving_reason                                                        meaning_code,
9148                 nvl(per_za_employment_equity_pkg.get_termination_reason_new(paaf.business_group_id, p_report_date, ppos.leaving_reason), 'No Leaving Reason') termination_reason,
9149                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   male_african,
9150                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0))   male_coloured,
9151                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0))   male_indian,
9152                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0))   male_white,
9153                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0))   female_african,
9154                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0))   female_coloured,
9155                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0))   female_indian,
9156                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   female_white,
9157                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
9158                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '03', 1, 0), 0)) +
9159                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '01', 1, 0), 0)) +
9160                 sum(decode(papf.sex, 'M', decode(papf.per_information4, '04', 1, 0), 0)) +
9161                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '02', 1,'ZA01', 1, 0), 0)) +
9162                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '03', 1, 0), 0)) +
9163                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '01', 1, 0), 0)) +
9164                 sum(decode(papf.sex, 'F', decode(papf.per_information4, '04', 1, 0), 0))   total
9165          from   hr_lookups                hl,
9166                 hr_lookups                hl1,
9167 --                hr_lookups                hl2,
9168                 hr_all_organization_units haou,
9169                 per_assignment_extra_info paei,
9170                 per_all_assignments_f     paaf,
9171                 per_periods_of_service    ppos,
9172                 per_all_people_f          papf
9173          where  papf.business_group_id = p_business_group_id
9174          and    papf.current_employee_flag = 'Y'
9175          and    ppos.person_id = papf.person_id
9176          and    nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY')) between add_months(p_report_date, -12) + 1 and p_report_date
9177          and    papf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
9178          and    paaf.person_id = papf.person_id
9179          and    paaf.primary_flag = 'Y'
9180          and    paaf.effective_end_date = nvl(ppos.actual_termination_date, to_date('31-12-4712', 'DD-MM-YYYY'))
9181          and    paei.assignment_id = paaf.assignment_id
9182          and    paei.information_type = 'ZA_SPECIFIC_INFO'
9183          and    paei.aei_information7 = nvl(p_legal_entity_id, paei.aei_information7)
9184          and    paei.aei_information7 is not null
9185          and    nvl(paei.aei_information6, 'N') <> 'Y'
9186          and    haou.organization_id = paei.aei_information7
9187          and    nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)) = 'Permanent'
9188          and    nvl(per_za_employment_equity_pkg.get_termination_reason_new(paaf.business_group_id, p_report_date, ppos.leaving_reason), 'No Leaving Reason') <> 'No Leaving Reason'
9189          and    hl.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_LEV'
9190          and    hl.meaning = per_za_employment_equity_pkg.get_occupational_level(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
9191          and    hl.lookup_code <> '15' -- Not Applicable
9192          and    hl1.lookup_type = 'ZA_EE_FUNCTION_TYPE'
9193          and    hl1.lookup_code <> '15' -- Not Applicable
9194          and    hl1.meaning = per_za_employment_equity_pkg.get_functional_type(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id,2009)
9195 --         and    hl2.lookup_type = 'ZA_EMP_EQ_OCCUPATIONAL_CAT'
9196 --         and    hl2.meaning = per_za_employment_equity_pkg.get_occupational_category(p_report_date, paaf.assignment_id, paaf.job_id, paaf.grade_id, paaf.position_id, paaf.business_group_id)
9197 --         and    hl2.lookup_code <> '15' -- Not Applicable
9198          group  by paaf.business_group_id,
9199                 paei.aei_information7,
9200                 haou.name,
9201                 decode(papf.registered_disabled_flag,'F','Y','P','Y',papf.registered_disabled_flag), --3962073
9202                 nvl(decode(paei.aei_information11,'P','Permanent','N','Non-Permanent'), per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id)),
9203                 -- per_za_employment_equity_pkg.get_ee_employment_type_name(paaf.effective_end_date, paaf.period_of_service_id),
9204                 ppos.leaving_reason,
9205                 nvl(per_za_employment_equity_pkg.get_termination_reason_new(paaf.business_group_id, p_report_date, ppos.leaving_reason), 'No Leaving Reason'),
9206                 p_report_code || decode(papf.PER_INFORMATION9,'N',null,'Y','F',null,
9207                     decode(papf.PER_INFORMATION11,null,null,
9208                     decode(sign(replace(nvl(substr(papf.PER_INFORMATION11,1,10),'0001/01/01'),'/','')
9209                                -to_char(l_nat_date,'YYYYMMDD'))
9210                            ,-1,null,'F'))
9211                     )
9212       ) tpa
9213       group by tpa.report_code,
9214                tpa.reporting_date,
9215                tpa.business_group_id,
9216                tpa.legal_entity_id,
9217                tpa.legal_entity,
9218                tpa.disability,
9219                tpa.employment_type,
9220                -- tpa.meaning_code,
9221               decode
9222                (
9223                 tpa.termination_reason,
9224                 'Resignation', 1,
9225                 'Non-Renewal of Contract', 2,
9226                 'Dismissal - Operational Requirements', 3,
9227                 'Dismissal - Misconduct', 4,
9228                 'Dismissal - Incapacity', 5,
9229                 --'Other', 6,
9230                 'Retirement', 6,
9231                 'Death', 7,
9232                 null
9233                 )  ,
9234                tpa.termination_reason;
9235 
9236       commit;
9237 
9238       -- Insert zeroes for any Termination Categories that weren't used
9239       for l_counter in 1..7 loop
9240 
9241          -- The hard coded names of the legislative Termination Categories (not stored anywhere)
9242          if    l_counter = 1 then
9243             l_reason := 'Resignation';
9244          elsif l_counter = 2 then
9245             l_reason := 'Non-Renewal of Contract';
9246          elsif l_counter = 3 then
9247             l_reason := 'Dismissal - Operational Requirements';
9248          elsif l_counter = 4 then
9249             l_reason := 'Dismissal - Misconduct';
9250          elsif l_counter = 5 then
9251             l_reason := 'Dismissal - Incapacity';
9252          elsif l_counter = 6 then
9253             l_reason := 'Retirement';
9254          else
9255             l_reason := 'Death';
9256          end if;
9257 
9258          insert into per_za_employment_equity
9259          (
9260             report_id,
9261             reporting_date,
9262             business_group_id,
9263             legal_entity_id,
9264             legal_entity,
9265             disability,
9266             employment_type,
9267             level_cat_code,
9268             level_cat,
9269             MA,
9270             MC,
9271             MI,
9272             MW,
9273             FA,
9274             FC,
9275             FI,
9276             FW,
9277             total
9278          )
9279          select 'EQ8'                 report_id,
9280                 p_report_date         reporting_date,
9281                 p_business_group_id   business_group_id,
9282                 haou.organization_id  legal_entity_id,
9283                 haou.name             legal_entity,
9284                 'Y'                   disability,
9285                 'Permanent'           employment_type,
9286                 decode
9287                 (
9288                    l_reason,
9289                    'Resignation', 1,
9290                    'Non-Renewal of Contract', 2,
9291                    'Dismissal - Operational Requirements', 3,
9292                    'Dismissal - Misconduct', 4,
9293                    'Dismissal - Incapacity', 5,
9294                    --'Other', 6,
9295                    'Retirement', 6,
9296                    'Death', 7,
9297                    null
9298                 )                     level_cat_code,
9299                /*decode
9300                (
9301                 l_reason,
9302                 'Dismissal - Operational Requirements', 'retrenchment -Operational requirements',
9303                 l_reason
9304                )              level_cat,*/
9305                 l_reason       level_cat,
9306                 0                     MA,
9307                 0                     MC,
9308                 0                     MI,
9309                 0                     MW,
9310                 0                     FA,
9311                 0                     FC,
9312                 0                     FI,
9313                 0                     FW,
9314                 0                     total
9315          from   hr_all_organization_units haou
9316          where not exists
9317          (
9318             select 'X'
9319             from   per_za_employment_equity pzee
9320             where  pzee.level_cat         = l_reason
9321             and    pzee.report_id         = 'EQ8'
9322             and    pzee.business_group_id = p_business_group_id  --Bug 4872110
9323             and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
9324             and    pzee.disability        = 'Y'
9325             and    pzee.employment_type   = 'Permanent'
9326          )
9327          and haou.business_group_id = p_business_group_id   --Bug 4872110
9328          and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
9329 
9330 
9331          insert into per_za_employment_equity
9332          (
9333             report_id,
9334             reporting_date,
9335             business_group_id,
9336             legal_entity_id,
9337             legal_entity,
9338             disability,
9339             employment_type,
9340             level_cat_code,
9341             level_cat,
9342             MA,
9343             MC,
9344             MI,
9345             MW,
9346             FA,
9347             FC,
9348             FI,
9349             FW,
9350             total
9351          )
9352          select 'EQ8F'                 report_id,
9353                 p_report_date         reporting_date,
9354                 p_business_group_id   business_group_id,
9355                 haou.organization_id  legal_entity_id,
9356                 haou.name             legal_entity,
9357                 'Y'                   disability,
9358                 'Permanent'           employment_type,
9359                 decode
9360                 (
9361                    l_reason,
9362                    'Resignation', 1,
9363                    'Non-Renewal of Contract', 2,
9364                    'Dismissal - Operational Requirements', 3,
9365                    'Dismissal - Misconduct', 4,
9366                    'Dismissal - Incapacity', 5,
9367                    'Other', 6,
9368                    null
9369                 )                     level_cat_code,
9370                /*decode
9371                (
9372                 l_reason,
9373                 'Dismissal - Operational Requirements', 'Dismissal - Operational Requirements (Retrenchment)',
9374                 l_reason
9375                )              level_cat,*/
9376                 l_reason       level_cat,
9377                 0                     MA,
9378                 0                     MC,
9379                 0                     MI,
9380                 0                     MW,
9381                 0                     FA,
9382                 0                     FC,
9383                 0                     FI,
9384                 0                     FW,
9385                 0                     total
9386          from   hr_all_organization_units haou
9387          where not exists
9388          (
9389             select 'X'
9390             from   per_za_employment_equity pzee
9391             where  pzee.level_cat         = l_reason
9392             and    pzee.report_id         = 'EQ8F'
9393             and    pzee.business_group_id = p_business_group_id  --Bug 4872110
9394             and    pzee.legal_entity_id   = nvl(p_legal_entity_id, haou.organization_id)
9395             and    pzee.disability        = 'Y'
9396             and    pzee.employment_type   = 'Permanent'
9397          )
9398          and haou.business_group_id = p_business_group_id   --Bug 4872110
9399          and haou.organization_id   = nvl(p_legal_entity_id, haou.organization_id);
9400 
9401       end loop;
9402 
9403       commit;
9404 
9405    end if;
9406 
9407 end populate_ee_table_new;
9408 
9409    procedure populate_ee_table_EEWF_new
9410    (
9411       p_report_date       in per_all_assignments_f.effective_end_date%type,
9412       p_business_group_id in per_all_assignments_f.business_group_id%type,
9413       p_legal_entity_id   in per_assignment_extra_info.aei_information7%type := null
9414    )  is
9415 
9416    begin
9417     DELETE FROM per_za_employment_equity
9418     Where REPORT_ID IN ('EQ2','EQ3','EQ4','EQ5','EQ6','EQ7','EQ8',
9419                         'EQ2F','EQ3F','EQ4F','EQ5F','EQ6F','EQ7F','EQ8F'
9420                        );
9421 
9422     populate_ee_table_new (
9423                        p_report_code       =>'EQ2'
9424                      , p_report_date       =>p_report_date
9425                      , p_business_group_id =>p_business_group_id
9426                      , p_legal_entity_id   =>p_legal_entity_id
9427                        );
9428 
9429     populate_ee_table_new (
9430                        p_report_code       =>'EQ3'
9431                      , p_report_date       =>p_report_date
9432                      , p_business_group_id =>p_business_group_id
9433                      , p_legal_entity_id   =>p_legal_entity_id
9434                        );
9435 
9436     populate_ee_table_new (
9437                        p_report_code       =>'EQ4'
9438                      , p_report_date       =>p_report_date
9439                      , p_business_group_id =>p_business_group_id
9440                      , p_legal_entity_id   =>p_legal_entity_id
9441                        );
9442 
9443     populate_ee_table_new (
9444                        p_report_code       =>'EQ5'
9445                      , p_report_date       =>p_report_date
9446                      , p_business_group_id =>p_business_group_id
9447                      , p_legal_entity_id   =>p_legal_entity_id
9448                        );
9449 
9450     populate_ee_table_new (
9451                        p_report_code       =>'EQ6'
9452                      , p_report_date       =>p_report_date
9453                      , p_business_group_id =>p_business_group_id
9454                      , p_legal_entity_id   =>p_legal_entity_id
9455                        );
9456 
9457     populate_ee_table_new (
9458                        p_report_code       =>'EQ7'
9459                      , p_report_date       =>p_report_date
9460                      , p_business_group_id =>p_business_group_id
9461                      , p_legal_entity_id   =>p_legal_entity_id
9462                        );
9463 
9464     populate_ee_table_new (
9465                        p_report_code       =>'EQ8'
9466                      , p_report_date       =>p_report_date
9467                      , p_business_group_id =>p_business_group_id
9468                      , p_legal_entity_id   =>p_legal_entity_id
9469                        );
9470 
9471       -- Inserts non-associated occupational categories with zero values
9472       insert into per_za_employment_equity
9473       (
9474          report_id,
9475          reporting_date,
9476          business_group_id,
9477          legal_entity_id,
9478          legal_entity,
9479          disability,
9480          employment_type,
9481          level_cat_code,
9482          level_cat,
9483          MA,
9484          MC,
9485          MI,
9486          MW,
9487          FA,
9488          FC,
9489          FI,
9490          FW,
9491          total
9492       )
9493       select          substr(report_id,1,3),
9494          reporting_date,
9495          business_group_id,
9496          legal_entity_id,
9497          legal_entity,
9498          disability,
9499          employment_type,
9500          level_cat_code,
9501          level_cat,
9502          0,
9503          0,
9504          0,
9505          0,
9506          0,
9507          0,
9508          0,
9509          0,
9510          0
9511       from   per_za_employment_equity pzee1
9512       Where  pzee1.business_group_id = p_business_group_id
9513       AND    pzee1.legal_entity_id = nvl(p_legal_entity_id, pzee1.legal_entity_id)
9514       AND    pzee1.report_id IN ('EQ1F','EQ2F','EQ3F','EQ4F','EQ5F','EQ6F','EQ7F','EQ8F')
9515       AND    not exists
9516       (
9517          select 'X'
9518          from   per_za_employment_equity pzee
9519          where  pzee.business_group_id   = pzee1.business_group_id           --Bug 4872110
9520          AND    pzee.legal_entity_id    = pzee1.legal_entity_id
9521          AND    pzee.report_id ||'F'     = pzee1.report_id
9522          AND    pzee1.level_cat_code     = pzee.level_cat_code
9523          AND    pzee1.level_cat          = pzee.level_cat
9524          and    nvl(pzee.disability,'X') = nvl(pzee1.disability,'X')
9525          and    pzee.employment_type     = pzee1.employment_type
9526       );
9527 
9528 
9529       -- Inserts non-associated occupational categories with zero values
9530       insert into per_za_employment_equity
9531       (
9532          report_id,
9533          reporting_date,
9534          business_group_id,
9535          legal_entity_id,
9536          legal_entity,
9537          disability,
9538          employment_type,
9539          level_cat_code,
9540          level_cat,
9541          MA,
9542          MC,
9543          MI,
9544          MW,
9545          FA,
9546          FC,
9547          FI,
9548          FW,
9549          total
9550       )
9551       select          report_id||'F' report_id,
9552          reporting_date,
9553          business_group_id,
9554          legal_entity_id,
9555          legal_entity,
9556          disability,
9557          employment_type,
9558          level_cat_code,
9559          level_cat,
9560          0,
9561          0,
9562          0,
9563          0,
9564          0,
9565          0,
9566          0,
9567          0,
9568          0
9569       from   per_za_employment_equity pzee1
9570       Where  pzee1.business_group_id = p_business_group_id
9571       AND    pzee1.legal_entity_id = nvl(p_legal_entity_id, pzee1.legal_entity_id)
9572       AND    pzee1.report_id IN ('EQ1','EQ2','EQ3','EQ4','EQ5','EQ6','EQ7','EQ8')
9573       AND    not exists
9574       (
9575          select 'X'
9576          from   per_za_employment_equity pzee
9577          where  pzee.business_group_id   = pzee1.business_group_id           --Bug 4872110
9578          AND    pzee.legal_entity_id    = pzee1.legal_entity_id
9579          AND    pzee1.report_id ||'F'     = pzee.report_id
9580          AND    pzee1.level_cat_code     = pzee.level_cat_code
9581          AND    pzee1.level_cat          = pzee.level_cat
9582          and    nvl(pzee.disability,'X') = nvl(pzee1.disability,'X')
9583          and    pzee.employment_type     = pzee1.employment_type
9584       );
9585 
9586   commit;
9587 
9588 
9589    End populate_ee_table_EEWF_new;
9590 
9591 end per_za_employment_equity_pkg; -- package body