DBA Data[Home] [Help]

PACKAGE BODY: APPS.HR_GB_PROCESS_P11D_ENTRIES_PKG

Source


1 PACKAGE BODY HR_GB_PROCESS_P11D_ENTRIES_PKG as
2 /* $Header: pygbp11d.pkb 120.43.12020000.3 2012/07/06 10:43:01 vmaripal ship $ */
3     g_package            constant VARCHAR2(33) := '  hr_gb_process_p11d_entries_pkg.';
4    procedure  clob_to_blob(p_clob clob,
5                            p_blob in out nocopy Blob)
6    is
7         l_length_clob number;
8         l_offset integer;
9         l_varchar_buffer varchar2(32000);
10         l_raw_buffer raw(32000);
11         l_buffer_len number:= 32000;
12         l_chunk_len number;
13         l_blob blob;
14    begin
15         l_length_clob := dbms_lob.getlength(p_clob);
16         l_offset := 1;
17         while l_length_clob > 0 loop
18               hr_utility.trace('l_length_clob '|| l_length_clob);
19               if l_length_clob < l_buffer_len then
20                  l_chunk_len := l_length_clob;
21               else
22                   l_chunk_len := l_buffer_len;
23               end if;
24               DBMS_LOB.READ(p_clob,l_chunk_len,l_offset,l_varchar_buffer);
25               l_raw_buffer := utl_raw.cast_to_raw(l_varchar_buffer);
26               dbms_lob.write(p_blob,l_chunk_len, l_offset, l_raw_buffer);
27               l_offset := l_offset + l_chunk_len;
28               l_length_clob := l_length_clob - l_chunk_len;
29               hr_utility.trace('l_length_blob '|| dbms_lob.getlength(p_blob));
30         end loop;
31    end;
32 
33    procedure delete_entries(errbuf              out nocopy VARCHAR2,
34                             retcode             out nocopy NUMBER,
35                             p_element_type_id   in pay_element_types_f.element_type_id%type,
36                             p_start_date        in VARCHAR2,
37                             p_end_date          in VARCHAR2,
38                             p_bus_grp_id        in pay_element_types_f.business_group_id%type,
39                             p_assignment_set_id in Number   )
40    is
41         l_ben_start_date_id           pay_input_values_f.input_value_id%type;
42         l_ben_end_date_id             pay_input_values_f.input_value_id%type;
43         l_element_name                pay_element_types_f.element_name%type;
44         l_count                       NUMBER default 0;
45         l_effective_start_date_dummy  DATE;
46         l_effective_end_date_dummy    DATE;
47         l_del_warning_dummy           BOOLEAN;
48         e_p11d_element_err            exception;
49         e_p11d_ben_st_dt_err          exception;
50         e_p11d_ben_end_dt_err         exception;
51         cursor csr_get_element_name(v_element_type_id pay_element_types_f.element_type_id%type)
52         is
53         select element_name
54         from   pay_element_types_f pet
55         where  pet.element_type_id = v_element_type_id;
56 
57         cursor csr_get_benefit_date_id(v_ben_date_type   VARCHAR2,
58                                        v_element_type_id pay_element_types_f.element_type_id%type)
59         is
60         select input_value_id
61         from   pay_input_values_f piv
62         where  piv.element_type_id = v_element_type_id
63         and    piv.NAME = v_ben_date_type;
64 
65         cursor csr_get_del_element_entry_id(v_ben_start_date_id  pay_input_values_f.input_value_id%type,
66                                             v_start_date         VARCHAR2,
67                                             v_ben_end_date_id    pay_input_values_f.input_value_id%type,
68                                             v_end_date           VARCHAR2,
69                                             v_bus_grp_id         pay_element_types_f.business_group_id%type,
70                                             v_element_type_id    pay_element_types_f.element_type_id%type)
71         is
72         select /*+ ordered */
73               pee.element_entry_id, pee.object_version_number, pee.effective_start_date
74         from  pay_element_links_f pel,
75               pay_element_entries_f pee,
76               pay_element_entry_values_f peev_sd,
77               pay_element_entry_values_f peev_ed
78         where pel.element_type_id = v_element_type_id
79         and   pee.element_link_id = pel.element_link_id
80         and   pee.effective_start_date between pel.effective_start_date and pel.effective_end_date
81         and   pee.effective_end_date between pel.effective_start_date and pel.effective_end_date
82         and   peev_sd.element_entry_id = pee.element_entry_id
83         and   peev_ed.element_entry_id = pee.element_entry_id
84         and   peev_sd.element_entry_id = peev_ed.element_entry_id
85         and   peev_sd.input_value_id = v_ben_start_date_id
86         and   peev_sd.screen_entry_value >= v_start_date
87         and   peev_ed.input_value_id = v_ben_end_date_id
88         and   peev_ed.screen_entry_value <= v_end_date
89         and exists( select /*+ no_unnest */
90                                    1
91                     from  per_all_assignments_f paa
92                     where paa.assignment_id = pee.assignment_id
93                     and   paa.business_group_id = v_bus_grp_id);
94 
95         cursor csr_del_entries_assignset(v_ben_start_date_id pay_input_values_f.input_value_id%type,
96                                          v_start_date        VARCHAR2,
97                                          v_ben_end_date_id   pay_input_values_f.input_value_id%type,
98                                          v_end_date          VARCHAR2,
99                                          v_bus_grp_id        pay_element_types_f.business_group_id%type,
100                                          v_element_type_id   pay_element_types_f.element_type_id%type,
101                                          v_assignment_set_id Number)
102         is
103         select /*+ ordered */
104               pee.element_entry_id, pee.object_version_number, pee.effective_start_date
105         from  pay_element_links_f pel,
106               pay_element_entries_f pee,
107               pay_element_entry_values_f peev_sd,
108               pay_element_entry_values_f peev_ed
109         where pel.element_type_id = v_element_type_id
110         and   pee.element_link_id = pel.element_link_id
111         and   pee.effective_start_date between pel.effective_start_date and pel.effective_end_date
112         and   pee.effective_end_date between pel.effective_start_date and pel.effective_end_date
113         and   peev_sd.element_entry_id = pee.element_entry_id
114         and   peev_ed.element_entry_id = pee.element_entry_id
115         and   peev_sd.element_entry_id = peev_ed.element_entry_id
116         and   peev_sd.input_value_id = v_ben_start_date_id
117         and   peev_sd.screen_entry_value >= v_start_date
118         and   peev_ed.input_value_id = v_ben_end_date_id
119         and   peev_ed.screen_entry_value <= v_end_date
120         and exists( select /*+ no_unnest */
121                            1
122                     from  per_all_assignments_f paa,
123                           hr_assignment_sets has,
124                           hr_assignment_set_amendments hasa
125                     where paa.assignment_id = pee.assignment_id
126                     and   paa.business_group_id = v_bus_grp_id
127                     and   (    has.assignment_set_id = v_assignment_set_id
128                            and nvl(has.payroll_id, paa.payroll_id) = paa.payroll_id
129                            and has.assignment_set_id = hasa.assignment_set_id(+)
130                            and nvl(hasa.include_or_exclude, 'I') = 'I'
131                            and nvl(hasa.assignment_id, paa.assignment_id) = paa.assignment_id));
132 
133    -- BEGIN of the procedure delete_entries
134    begin
135         -- initialize the return code to 0
136         retcode := 0;
137         -- get the element name
138         open csr_get_element_name(p_element_type_id);
139         fetch csr_get_element_name into l_element_name;
140         if csr_get_element_name%notfound
141         then
142             close csr_get_element_name;
143             hr_utility.TRACE('Incorrect element type id '|| p_element_type_id);
144             raise e_p11d_element_err;
145         end if;
146         close csr_get_element_name;
147         hr_utility.TRACE('Value of element name='|| l_element_name);
148         -- get the benefit start date id
149         open csr_get_benefit_date_id(c_ben_start_date_string, p_element_type_id);
150         fetch csr_get_benefit_date_id into l_ben_start_date_id;
151         if csr_get_benefit_date_id%notfound
152         then
153             close csr_get_benefit_date_id;
154             hr_utility.TRACE('No Benefit Start date defined for element ');
155             raise e_p11d_ben_st_dt_err;
156         end if;
157         close csr_get_benefit_date_id;
158         hr_utility.TRACE('Value of benefit start date id = '|| to_char(l_ben_start_date_id) );
159         -- get the benefit END date id;
160         open csr_get_benefit_date_id(c_ben_end_date_string, p_element_type_id);
161         fetch csr_get_benefit_date_id into l_ben_end_date_id;
162         if csr_get_benefit_date_id%notfound
163         then
164             close csr_get_benefit_date_id;
165             hr_utility.TRACE('No Benefit End date defined for element type id ');
166             raise e_p11d_ben_end_dt_err;
167         end if;
168         close csr_get_benefit_date_id;
169         hr_utility.TRACE('Value of benefit end date  id ='|| to_char(l_ben_end_date_id) );
170         -- get the elementry id WHERE the start date is greater than the p_start_date
171         -- AND END date is less than the p_end_date. Only those element entry id's will
172         -- be returned WHERE the business group id is p_bus_grp_id
173         hr_utility.TRACE('p_start_date '|| p_start_date);
174         hr_utility.TRACE('p_end_date '|| p_end_date);
175         hr_utility.TRACE('p_bus_grp_id '|| p_bus_grp_id);
176         hr_utility.TRACE('p_element_type_id '|| p_element_type_id);
177         if p_assignment_set_id is null then
178            for del_element_entry in csr_get_del_element_entry_id(
179                                       l_ben_start_date_id,
180                                       p_start_date,
181                                       l_ben_end_date_id,
182                                       p_end_date,
183                                       p_bus_grp_id,
184                                       p_element_type_id)
185            loop
186                l_count := l_count + 1;
187                hr_utility.TRACE('Delete entries for element entry id '|| to_char(del_element_entry.element_entry_id) );
188                pay_element_entry_api.delete_element_entry(p_datetrack_delete_mode => 'ZAP',
189                                                           p_effective_date        => del_element_entry.effective_start_date,
190                                                           p_element_entry_id      => del_element_entry.element_entry_id,
191                                                           p_object_version_number => del_element_entry.object_version_number,
192                                                           p_effective_start_date  => l_effective_start_date_dummy,
193                                                           p_effective_end_date    => l_effective_end_date_dummy,
194                                                           p_delete_warning        => l_del_warning_dummy);
195                if mod(l_count, c_commit_num) = 0
196                then
197                    commit;
198                    hr_utility.TRACE('Commiting delete, counter = '|| to_char(l_count) );
199                end if;
200            end loop;
201         else
202            for del_element_entry in csr_del_entries_assignset(
203                                       l_ben_start_date_id,
204                                       p_start_date,
205                                       l_ben_end_date_id,
206                                       p_end_date,
207                                       p_bus_grp_id,
208                                       p_element_type_id,
209                                       p_assignment_set_id)
210            loop
211                l_count := l_count + 1;
212                hr_utility.TRACE('Delete entries for element entry id '|| to_char(del_element_entry.element_entry_id) );
213                pay_element_entry_api.delete_element_entry(p_datetrack_delete_mode => 'ZAP',
214                                                           p_effective_date        => del_element_entry.effective_start_date,
215                                                           p_element_entry_id      => del_element_entry.element_entry_id,
216                                                           p_object_version_number => del_element_entry.object_version_number,
217                                                           p_effective_start_date  => l_effective_start_date_dummy,
218                                                           p_effective_end_date    => l_effective_end_date_dummy,
219                                                           p_delete_warning        => l_del_warning_dummy);
220                if mod(l_count, c_commit_num) = 0
221                then
222                    commit;
223                    hr_utility.TRACE('Commiting delete, counter = '|| to_char(l_count) );
224                end if;
225            end loop;
226         end if;
227         commit;
228    exception
229      when e_p11d_element_err
230      then
231          hr_utility.set_message(800, 'HR_78046_GB_P11D_ELEMENT_ERR');
232          hr_utility.set_message_token(800, 'ELEMENT', l_element_name);
233          errbuf := hr_utility.get_message;
234          hr_utility.raise_error;
235          retcode := 1;
236          rollback;
237      when e_p11d_ben_st_dt_err
238      then
239          hr_utility.set_message(800, 'HR_78047_GB_P11D_BEN_ST_DT_ERR');
240          hr_utility.set_message_token(800, 'ELEMENT', l_element_name);
241          errbuf := hr_utility.get_message;
242          hr_utility.raise_error;
243          retcode := 1;
244          rollback;
245      when e_p11d_ben_end_dt_err
246      then
247          hr_utility.set_message(800, 'HR_78048_GB_P11D_BEN_ED_DT_ERR');
248          hr_utility.set_message_token(800, 'ELEMENT', l_element_name);
249          errbuf := hr_utility.get_message;
250          hr_utility.raise_error;
251          retcode := 1;
252          rollback;
253      when OTHERS
254      then
255          if csr_get_element_name%isopen
256          then
257              close csr_get_element_name;
258          end if;
259          if csr_get_benefit_date_id%isopen
260          then
261              close csr_get_benefit_date_id;
262          end if;
263          if csr_get_del_element_entry_id%isopen
264          then
265              close csr_get_del_element_entry_id;
266          end if;
267          hr_utility.set_message(800, 'HR_78045_GB_P11D_DEL_ERR');
268          hr_utility.set_message_token(800, 'ERRORMSG', sqlerrm);
269          errbuf := hr_utility.get_message;
270          hr_utility.raise_error;
271          retcode := 1;
272          rollback;
273    end delete_entries;
274 
275    function get_null_error(p_token1 in VARCHAR2,
276                            p_token2 in VARCHAR2,
277                            p_token3 in VARCHAR2)return VARCHAR2
278    is
279         l_errmsg                      VARCHAR2(1000);
280    begin
281         hr_utility.set_message(800, 'HR_78052_GB_P11D_VALUES_NULL');
282         hr_utility.set_message_token(800, '1INPUT_VALUE', p_token1);
283         hr_utility.set_message_token(800, '2INPUT_VALUE', p_token2);
284         -- hr_utility.set_message_token (800, '3INPUT_VALUE', p_token3);
285         l_errmsg := hr_utility.get_message;
286         return l_errmsg;
287    end get_null_error;
288 
289    function get_incorrect_val_error(p_token1 in VARCHAR2,
290                                     p_token2 in VARCHAR2,
291                                     p_token3 in VARCHAR2) return VARCHAR2
292    is
293         l_errmsg                      VARCHAR2(1000);
294    begin
295         hr_utility.set_message(800, 'HR_78051_GB_P11D_INCORRECT_VAL');
296         hr_utility.set_message_token(800, '1INPUT_VALUE', p_token1);
297         hr_utility.set_message_token(800, '2INPUT_VALUE', p_token2);
298         hr_utility.set_message_token(800, '3INPUT_VALUE', p_token3);
299         l_errmsg := hr_utility.get_message;
300         return l_errmsg;
301    end get_incorrect_val_error;
302 
303    function get_error_message(p_applid in NUMBER,
304                               p_message_name in VARCHAR2) return VARCHAR2
305    is
306         l_errmsg                      VARCHAR2(1000);
307    begin
308         hr_utility.set_message(p_applid, p_message_name);
309         l_errmsg := hr_utility.get_message;
310         return l_errmsg;
311    end get_error_message;
312 
313    function get_loan_amount(p_archive_payroll_action_id in VARCHAR2,
314                             p_employers_ref_no          in VARCHAR2,
315                             --p_employers_name      IN   VARCHAR2,
316                             p_person_id                 in VARCHAR2) return VARCHAR2
317    is
318         l_loan_amount                 NUMBER;
319    begin
320         select sum(nvl(pai.action_information7, 0) )
321         into  l_loan_amount
322         from  pay_action_information pai_comp,
323               pay_action_information pai_person,
324               pay_action_information pai,
325               pay_assignment_actions paa,
326               pay_payroll_actions ppa
327         where ppa.payroll_action_id = p_archive_payroll_action_id
328         and   ppa.payroll_action_id = paa.payroll_action_id
329         and   pai_comp.action_context_id = paa.assignment_action_id
330         and   pai_comp.action_information_category = 'EMEA PAYROLL INFO'
331         and   pai_comp.action_context_type = 'AAP'
332         and   pai_comp.action_information6 = p_employers_ref_no
333         -- AND pai_comp.action_information7 = p_employers_name
334         and   pai_person.action_context_id = paa.assignment_action_id
335         and   pai_person.action_information_category = 'ADDRESS DETAILS'
336         and   pai_person.action_context_type = 'AAP'
337         and   pai_person.action_information14 = 'Employee Address'
338         and   pai_person.action_information1 = p_person_id
339         and   pai.action_context_id = paa.assignment_action_id
340         and   pai.action_context_type = 'AAP'
341         and   pai.action_information_category = 'INT FREE AND LOW INT LOANS';
342       return to_char(l_loan_amount);
343    end get_loan_amount;
344 
345    function get_global_value(p_global_name in VARCHAR2,
346                              p_benefit_end_date in VARCHAR2 default '0001/01/01 00:00:00') return VARCHAR2
347    is
348         l_global_value ff_globals_f.global_value%type;
349         cursor csr_get_value
350         is
351         select global_value
352         from   ff_globals_f
353         where  legislation_code = 'GB'
354         and    GLOBAL_NAME = p_global_name
355         and    fnd_date.canonical_to_date(p_benefit_end_date)
356                between effective_start_date and effective_end_date;
357    begin
358         hr_utility.TRACE('p_benefit_end_date '|| p_benefit_end_date);
359         hr_utility.TRACE('p_global_name '|| p_global_name);
360         open csr_get_value;
361         fetch csr_get_value into l_global_value;
362         close csr_get_value;
363         return l_global_value;
364    end;
365 
366    function sum_and_set_global_var(p_varable_name in VARCHAR2,
367                                    p_variable_value in VARCHAR2) return NUMBER
368    is
369         l_present_val NUMBER;
370         l_new_val     NUMBER;
371         l_dummy       NUMBER;
372    begin
373         hr_utility.TRACE(' p_variable_value '|| p_variable_value);
374         l_present_val := per_formula_functions.get_number(p_varable_name);
375         hr_utility.TRACE(' l_present_val '|| l_present_val);
376         if l_present_val is null
377         then
378             if p_variable_value is null
379             then
380                 l_new_val := null;
381             else
382                 l_new_val := nvl(to_number(p_variable_value), 0);
383             end if;
384             hr_utility.TRACE('l_new_val '|| l_new_val);
385         else
386 --Added the below logic for the bug fix 8864717.
387 --
388             if ((l_present_val = p_variable_value) and (PAY_GB_P11D_ARCHIVE_SS.g_updated_flag = 'Y'))
389             then
390                 l_new_val := l_present_val ;
391             else
392                 l_new_val := l_present_val + nvl(to_number(p_variable_value), 0);
393             end if;
394 --
395             hr_utility.TRACE(' l_new_val '|| l_new_val);
396         end if;
397         l_dummy := per_formula_functions.set_number(p_varable_name, l_new_val);
398         hr_utility.TRACE(' l_dummy '|| l_dummy);
399         return l_new_val;
400    end;
401 
402    function max_and_set_global_var(p_variable_name     in Varchar2,
403                                    p_variable_datatype in Varchar2,
404                                    p_variable_value    in Varchar2) return varchar2
405    is
406         l_present_val varchar2(20);
407         l_warnmsg     varchar2(20);
408         l_dummy       NUMBER;
409    begin
410         l_warnmsg := '0';
411         if p_variable_datatype = 'N' then
412            -- we need to store the value st away
413            -- rates remain same for a vehicle type
414            -- !! the above statm is not true as second assignment (multi)
415            -- might not have this benefit type hence it will be zero,
416            -- so we don't want to store zero.
417            if nvl(per_formula_functions.get_number(p_variable_name),0) < nvl(to_number(p_variable_value),0) then
418               l_dummy := per_formula_functions.set_number(p_variable_name, p_variable_value);
419            end if;
420         end if;
421         if p_variable_datatype = 'T' then
422            -- we need to store the Y value
423            -- as this will be called only when the value is Y
424            l_dummy := per_formula_functions.set_text(p_variable_name, p_variable_value);
425         end if;
426         if p_variable_datatype = 'D' then
427            -- this is for date, we need to store the max date
428            -- this is used for date free fuel withdrawn
429            -- if p_variable_value is null or zero this won't be called
430            l_present_val := per_formula_functions.get_text(p_variable_name);
431            hr_utility.TRACE(' l_present_val '|| l_present_val);
432            if l_present_val is null
433            then
434                l_dummy := per_formula_functions.set_text(p_variable_name, p_variable_value);
435            else
436                if fnd_date.canonical_to_date(l_present_val) <>
437                   fnd_date.canonical_to_date(p_variable_value)
438                then
439                    l_warnmsg := '1';
440                    if fnd_date.canonical_to_date(l_present_val) <
441                       fnd_date.canonical_to_date(p_variable_value)
442                    then
443                        l_dummy := per_formula_functions.set_text(p_variable_name, p_variable_value);
444                    end if;
445                end if;
446            end if;
447         end if;
448         return l_warnmsg;
449    end;
450 
451    function get_lookup_desc (p_lookup_type    varchar2,
452                              p_lookup_code    varchar2,
453                              p_effective_date varchar2) return varchar2
454    is
455         l_description varchar2(150);
456    begin
457         /*Bug No. 3237648*/
458         /*Fetching from hr_lookups instead of fnd_lookup_values*/
459         select description into l_description
460         from  hr_lookups hlu
461         where hlu.lookup_type = p_lookup_type
462         and  hlu.lookup_code = p_lookup_code
463         and  hlu.ENABLED_FLAG = 'Y'
464         and  fnd_date.canonical_to_date(p_effective_date) between
465                  nvl(hlu.START_DATE_ACTIVE,fnd_date.canonical_to_date(p_effective_date))
466              and nvl(hlu.END_DATE_ACTIVE,fnd_date.canonical_to_date(p_effective_date));
467         return l_description ;
468     exception
469     when others then
470          l_description  := null;
471          return l_description ;
472     end;
473 
474     function check_desc_and_set_global_var(p_varable_name   in VARCHAR2,
475                                            p_variable_value in VARCHAR2,
476                                            p_lookup_type    in VARCHAR2 default null,
477                                            p_effective_date in VARCHAR2 default null) return NUMBER
478     is
479          l_present_val VARCHAR2(150);
480          l_new_val     VARCHAR2(150);
481          l_dummy       NUMBER;
482     begin
483          hr_utility.TRACE('.. p_varable_name ...'|| p_varable_name);
484          hr_utility.TRACE('.. p_variable_value ...'|| p_variable_value);
485          l_present_val := per_formula_functions.get_text(p_varable_name);
486          hr_utility.TRACE('..l_present_val.. '|| l_present_val);
487          if p_lookup_type is null
488          then
489             if l_present_val is null
490             then
491                 l_new_val := p_variable_value;
492                 l_dummy := per_formula_functions.set_text(p_varable_name, l_new_val);
493             else
494                 hr_utility.TRACE('..Checking present Value with variable value '|| l_present_val);
495                 if l_present_val <> nvl(p_variable_value, l_present_val)
496                 then
497                     l_new_val := 'Multiple';
498                     l_dummy := per_formula_functions.set_text(p_varable_name, l_new_val);
499                 end if;
500             end if;
501          else
502             l_new_val := get_lookup_desc (p_lookup_type,
503                                           p_variable_value,
504                                           p_effective_date || ' 00:00:00'); -- since it accepts date in canonical format
505             if l_present_val is null
506             then
507                 hr_utility.trace('New vale ' || l_new_val);
508                 l_dummy := per_formula_functions.set_text(p_varable_name, l_new_val);
509             else
510                 hr_utility.TRACE('..Checking present Value with variable value '|| l_present_val);
511                 if l_present_val <> nvl(l_new_val, l_present_val)
512                 then
513                     l_new_val := 'Multiple';
514                     l_dummy := per_formula_functions.set_text(p_varable_name, l_new_val);
515                 end if;
516             end if;
517          end if;
518          return l_dummy;
519    end;
520 
521    function get_and_push_message(p_application  in VARCHAR2,
522                                  p_message      in VARCHAR2,
523                                  p_stack_level  in VARCHAR2 default 'A',
524                                  p_token_name1  in VARCHAR2 default null,
525                                  p_token_value1 in VARCHAR2 default null,
526                                  p_token_name2  in VARCHAR2 default null,
527                                  p_token_value2 in VARCHAR2 default null,
528                                  p_token_name3  in VARCHAR2 default null,
529                                  p_token_value3 in VARCHAR2 default null,
530                                  p_token_name4  in VARCHAR2 default null,
531                                  p_token_value4 in VARCHAR2 default null,
532                                  p_token_name5  in VARCHAR2 default null,
533                                  p_token_value5 in VARCHAR2 default null) return VARCHAR2
534    is
535         l_error_str                   VARCHAR2(300);
536         l_application_id  Number;
537    begin
538         --
539         -- Keep this code as simple as possible.
540         --
541         if p_application = 'PER' then
542            l_application_id :=  800;
543         elsif p_application = 'PAY' then
544            l_application_id :=  801;
545         else
546            select application_id
547            into  l_application_id
548            from  fnd_application
549            where APPLICATION_SHORT_NAME = p_application;
550         end if;
551 
552         pay_core_utils.push_message(l_application_id, p_message, p_stack_level);
553         --
554         if p_token_name1 is not null and p_token_value1 is not null
555         then
556             pay_core_utils.push_token(p_token_name1, p_token_value1);
557         end if;
558         --
559         if  p_token_name2 is not null and p_token_value2 is not null
560         then
561             pay_core_utils.push_token(p_token_name2, p_token_value2);
562         end if;
563         --
564         if  p_token_name3 is not null and p_token_value3 is not null
565         then
566             pay_core_utils.push_token(p_token_name3, p_token_value3);
567         end if;
568         --
569         if  p_token_name4 is not null and p_token_value4 is not null
570         then
571             pay_core_utils.push_token(p_token_name4, p_token_value4);
572         end if;
573         --
574         if  p_token_name5 is not null and p_token_value5 is not null
575         then
576             pay_core_utils.push_token(p_token_name5, p_token_value5);
577         end if;
578         --
579         -- The above pushed the message into the stack
580         -- Now Just return the message to ff : name itself if the text is NULL,
581         -- otherwise return the (truncated) message text.
582         --
583         l_error_str := fffunc.gfm(
584                         p_application,
585                         p_message,
586                         p_token_name1,
587                         p_token_value1,
588                         p_token_name2,
589                         p_token_value2,
590                         p_token_name3,
591                         p_token_value3,
592                         p_token_name4,
593                         p_token_value4,
594                         p_token_name5,
595                         p_token_value5);
596         return l_error_str;
597    end;
598 
599    function get_assignment_number(p_action_id  in number,
600                                   p_lowest     in boolean  default false,
601                                   p_person_id  in varchar2 default null,
602                                   p_emp_ref    in varchar2 default null) return varchar2 is
603      l_asg_id  number;
604      l_asg_no  varchar2(25);
605 
606      cursor asg_id is
607      select assignment_id
608      from   pay_assignment_actions
609      where  assignment_action_id = p_action_id;
610 
611      cursor asg_low is
612      select min(paa2.assignment_id)
613        from pay_assignment_actions paa,
614             pay_assignment_actions paa2,
615             pay_action_information pai_comp,
616             pay_action_information pai_person
617       where paa.assignment_action_id = p_action_id
618         and paa2.payroll_action_id = paa.payroll_action_id
619         and pai_comp.action_context_id = paa2.assignment_action_id
620         and pai_comp.action_information_category = 'EMEA PAYROLL INFO'
621         and pai_person.action_context_id = paa2.assignment_action_id
622         and pai_person.action_information_category = 'ADDRESS DETAILS'
623         and pai_person.action_information14 = 'Employee Address'
624         and pai_person.action_information1 = p_person_id
625         and pai_comp.action_information6 = p_emp_ref;
626 
627      cursor asg_no is
628      select paa.assignment_number
629      from   per_assignments_f paa
630      where  paa.assignment_id = l_asg_id
631      and    paa.effective_end_date = (select max(paa2.effective_end_date)
632                                         from per_assignments_f paa2
633                                        where paa2.assignment_id = l_asg_id);
634 
635      cursor emp_no is
636      select employee_number
637      from   per_all_people_f
638      where  person_id = p_person_id;
639 
640    begin
641         if not p_lowest then
642            open asg_id;
643            fetch asg_id into l_asg_id;
644            close asg_id;
645         else
646            open asg_low;
647            fetch asg_low into l_asg_id;
648            close asg_low;
649         end if;
650 
651         open asg_no;
652         fetch asg_no into l_asg_no;
653         close asg_no;
654 
655         if l_asg_no is null then
656            open emp_no;
657            fetch emp_no into l_asg_no;
658            close emp_no;
659         end if;
660         return l_asg_no;
661    end;
662 
663 
664    function fetch_p11d_rep_data(p_assignment_action_id NUMBER) return l_typ_p11d_fields_rec
665    is
666         l_h_ce      VARCHAR2(10);
667         l_h_count   NUMBER;
668         l_f_count   NUMBER;
669         l_pactid    NUMBER;
670         l_rep_run   VARCHAR2(10);
671         l_p11d_fields l_typ_p11d_fields_rec;
672         l_h_sum_max_amt_outstanding  number;
673 
674         procedure populate_stored_fields is
675         begin
676              -- populating the person id
677              select action_information1
678              into  g_person_id
679              from  pay_action_information pai_person
680              where pai_person.action_context_id = p_assignment_action_id
681              and   pai_person.action_information_category = 'ADDRESS DETAILS'
682              and   pai_person.action_context_type = 'AAP'
683              and   pai_person.action_information14 = 'Employee Address';
684 
685              select action_information6,
686                     action_information7
687              into  g_emp_ref_no,
688                    g_employer_name
689              from  pay_action_information pai_comp
690              where pai_comp.action_context_id = p_assignment_action_id
691              and   pai_comp.action_context_type = 'AAP'
692              and   pai_comp.action_information_category = 'EMEA PAYROLL INFO';
693 
694              l_p11d_fields.employers_ref_no := g_emp_ref_no;
695              l_p11d_fields.employers_name   := replace(g_employer_name,'&','&');
696              hr_utility.trace('l_p11d_fields.employers_name' || l_p11d_fields.employers_name);
697 
698              select decode(action_information4, 'Y', 'Y', 'N'),
699                     action_information8, -- P11D changes 07/08 last_name
700                     action_information6, -- P11D changes 07/08 first_name
701                     substr(action_information15,9,2) ||
702                     substr(action_information15,6,2) ||
703                     substr(action_information15,1,4),
704                     action_information17
705              into  l_p11d_fields.director_flag,
706                    l_p11d_fields.sur_name,
707                    l_p11d_fields.fore_name,
708                    l_p11d_fields.date_of_birth,
709                    l_p11d_fields.gender
710              from  pay_action_information pai_gb
711              where pai_gb.action_context_id = p_assignment_action_id
712              and   pai_gb.action_context_type = 'AAP'
713              and   pai_gb.action_information_category = 'GB EMPLOYEE DETAILS';
714              --
715              select action_information1,
716                     action_information4,
717                     action_information10 -- emp number
718                 -- action_information14 -- assign num
719              into  l_p11d_fields.full_name,
720                    l_p11d_fields.national_ins_no,
721                    l_p11d_fields.employee_number
722              from  pay_action_information pai_emp
723              where pai_emp.action_context_id = p_assignment_action_id
724              and   pai_emp.action_context_type = 'AAP'
725              and   pai_emp.action_information_category = 'EMPLOYEE DETAILS';
726 
727              l_p11d_fields.employee_number := get_assignment_number(p_assignment_action_id, true, g_person_id, g_emp_ref_no);
728 
729              select payroll_action_id
730              into  l_pactid
731              from  pay_assignment_actions
732              where assignment_action_id = p_assignment_action_id;
733 
734              PAY_GB_P11D_ARCHIVE_SS.get_parameters(p_payroll_action_id => l_pactid,
735                                                    p_token_name        => 'Rep_Run',
736                                                    p_token_value       => l_rep_run);
737 
738              if to_number(l_rep_run) < 2005
739              then
740                  select action_information1,
741                         decode(action_information2,'0',null,to_char(to_number(action_information2),'FM999,999,990.00')),
742                         decode(action_information2,'0',null,to_char(to_number(action_information3),'FM999,999,990.00')),
743                         to_char(to_number(nvl(action_information4,'0')),'FM999,999,990.00'),
744                         action_information5,
745                         to_char(to_number(nvl(action_information6,'0')),'FM999,999,990.00'),
746                         to_char(to_number(nvl(action_information7,'0')),'FM999,999,990.00'),
747                         decode(action_information8,'0',null,to_char(to_number(action_information8),'FM999,999,990.00')),
748                         decode(action_information8,'0',null,to_char(to_number(action_information9),'FM999,999,990.00')),
749                         to_char(to_number(nvl(action_information10,'0')),'FM999,999,990.00'),
750                         to_char(to_number(nvl(action_information11,'0')),'FM999,999,990.00'),
751                         to_char(to_number(nvl(action_information12,'0')),'FM999,999,990.00'),
752                         to_char(to_number(nvl(action_information13,'0')),'FM999,999,990.00'),
753                         to_char(to_number(nvl(action_information14,'0')),'FM999,999,990.00'),
754                         to_char(to_number(nvl(action_information15,'0')),'FM999,999,990.00'),
755                         decode(action_information16,'0',null,to_char(to_number(action_information16),'FM999,999,990.00')),
756                         decode(action_information16,'0',null,to_char(to_number(action_information17),'FM999,999,990.00')),
757                         to_char(to_number(nvl(action_information18,'0')),'FM999,999,990.00'),
758                         to_char(to_number(nvl(action_information19,'0')),'FM999,999,990.00'),
759                         decode(action_information20,'0',null,to_char(to_number(action_information20),'FM999,999,990.00')),
760                         decode(action_information20,'0',null,to_char(to_number(action_information21),'FM999,999,990.00')),
761                         to_char(to_number(nvl(action_information22,'0')),'FM999,999,990.00'),
762                         action_information23,
763                         decode(action_information24,'0',null,to_char(to_number(action_information24),'FM999,999,990.00')),
764                         decode(action_information24,'0',null,to_char(to_number(action_information25),'FM999,999,990.00')),
765                         to_char(to_number(nvl(action_information26,'0')),'FM999,999,990.00'),
766                         decode(action_information27, 'Y', 'Y', 'N'),
767                         to_char(to_number(nvl(action_information28,'0')),'FM999,999,990.00'),
768                         to_char(to_number(nvl(action_information29,'0')),'FM999,999,990.00'),
769                         action_information30
770                  into   l_p11d_fields.a_desc, l_p11d_fields.a_cost, l_p11d_fields.a_amg,
771                         l_p11d_fields.a_ce, l_p11d_fields.b_desc, l_p11d_fields.b_ce,
772                         l_p11d_fields.b_tnp, l_p11d_fields.c_cost, l_p11d_fields.c_amg,
773                         l_p11d_fields.c_ce, l_p11d_fields.d_ce, l_p11d_fields.e_ce,
774                         l_p11d_fields.f_tcce, l_p11d_fields.f_tfce, l_p11d_fields.g_ce,
775                         l_p11d_fields.i_cost, l_p11d_fields.i_amg, l_p11d_fields.i_ce,
776                         l_p11d_fields.j_ce, l_p11d_fields.k_cost, l_p11d_fields.k_amg,
777                         l_p11d_fields.k_ce, l_p11d_fields.l_desc, l_p11d_fields.l_cost,
778                         l_p11d_fields.l_amg, l_p11d_fields.l_ce, l_p11d_fields.m_shares,
779                         l_h_ce, l_h_count, l_f_count
780                  from   pay_action_information pai_emp
781                  where  pai_emp.action_context_id = p_assignment_action_id
782                  and    pai_emp.action_context_type = 'AAP'
783                  and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTA';
784 
785                  select action_information1,
786                         decode(action_information2,'0',null,to_char(to_number(action_information2),'FM999,999,990.00')),
787                         decode(action_information2,'0',null,to_char(to_number(action_information3),'FM999,999,990.00')),
788                         to_char(to_number(nvl(action_information4,'0')),'FM999,999,990.00'),
789                         action_information5,
790                         decode(action_information6,'0',null,to_char(to_number(action_information6),'FM999,999,990.00')),
791                         decode(action_information6,'0',null,to_char(to_number(action_information7),'FM999,999,990.00')),
792                         to_char(to_number(nvl(action_information8,'0')),'FM999,999,990.00'),
793                         to_char(to_number(nvl(action_information9,'0')),'FM999,999,990.00'),
794                         decode(action_information10,'0',null,to_char(to_number(action_information10),'FM999,999,990.00')),
795                         decode(action_information10,'0',null,to_char(to_number(action_information11),'FM999,999,990.00')),
796                         to_char(to_number(nvl(action_information12,'0')),'FM999,999,990.00'),
797                         decode(action_information13,'0',null,to_char(to_number(action_information13),'FM999,999,990.00')),
798                         decode(action_information13,'0',null,to_char(to_number(action_information14),'FM999,999,990.00')),
799                         to_char(to_number(nvl(action_information15,'0')),'FM999,999,990.00'),
800                         decode(action_information16, 'Y', 'Y', 'N'),
801                         decode(action_information17,'0',null,to_char(to_number(action_information17),'FM999,999,990.00')),
802                         decode(action_information17,'0',null,to_char(to_number(action_information18),'FM999,999,990.00')),
803                         to_char(to_number(nvl(action_information19,'0')),'FM999,999,990.00'),
804                         decode(action_information20,'0',null,to_char(to_number(action_information20),'FM999,999,990.00')),
805                         decode(action_information20,'0',null,to_char(to_number(action_information21),'FM999,999,990.00')),
806                         to_char(to_number(nvl(action_information22,'0')),'FM999,999,990.00'),
807                         decode(action_information23,'0',null,to_char(to_number(action_information23),'FM999,999,990.00')),
808                         decode(action_information23,'0',null,to_char(to_number(action_information24),'FM999,999,990.00')),
809                         to_char(to_number(nvl(action_information25,'0')),'FM999,999,990.00'),
810                         action_information26,
811                         decode(action_information27,'0',null,to_char(to_number(action_information27),'FM999,999,990.00')),
812                         decode(action_information27,'0',null,to_char(to_number(action_information28),'FM999,999,990.00')),
813                         to_char(to_number(nvl(action_information29,'0')),'FM999,999,990.00')
814                  into   l_p11d_fields.n_desc, l_p11d_fields.n_cost, l_p11d_fields.n_amg,
815                         l_p11d_fields.n_ce, l_p11d_fields.na_desc, l_p11d_fields.na_cost,
816                         l_p11d_fields.na_amg, l_p11d_fields.na_ce,
817                         l_p11d_fields.n_taxpaid, l_p11d_fields.o1_cost,
818                         l_p11d_fields.o1_amg, l_p11d_fields.o1_ce, l_p11d_fields.o2_cost,
819                         l_p11d_fields.o2_amg, l_p11d_fields.o2_ce,
820                         l_p11d_fields.o_toi,
821                         l_p11d_fields.o3_cost, l_p11d_fields.o3_amg, l_p11d_fields.o3_ce,
822                         l_p11d_fields.o4_cost, l_p11d_fields.o4_amg, l_p11d_fields.o4_ce,
823                         l_p11d_fields.o5_cost, l_p11d_fields.o5_amg, l_p11d_fields.o5_ce,
824                         l_p11d_fields.o6_desc, l_p11d_fields.o6_cost,
825                         l_p11d_fields.o6_amg, l_p11d_fields.o6_ce
826                  from   pay_action_information pai_emp
827                  where  pai_emp.action_context_id = p_assignment_action_id
828                  and    pai_emp.action_context_type = 'AAP'
829                  and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTB';
830 
831                  hr_utility.trace('Fetching from Result C');
832                  select substr(action_information10,9,2) || ' ' ||
833                         substr(action_information10,6,2) || ' ' ||
834                         substr(action_information10,1,4) ,
835                         decode (action_information11,null,'N','Y'),to_number(nvl(ACTION_INFORMATION23,'0'))
836                  into  l_p11d_fields.f_date_free,l_p11d_fields.f_rein_yr,
837                        l_h_sum_max_amt_outstanding
838                  from  pay_action_information pai_emp
839                  where pai_emp.action_context_id = p_assignment_action_id
840                  and   pai_emp.action_context_type = 'AAP'
841                  and   pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTC';
842 
843              ELSE  /* For year 04/05 onwards */
844                  select action_information1,
845                         decode(action_information2,'0',null,to_char(to_number(action_information2),'FM999,999,990.00')),
846                         decode(action_information2,'0',null,to_char(to_number(action_information3),'FM999,999,990.00')),
847                         to_char(to_number(nvl(action_information4,'0')),'FM999,999,990.00'),
848                         action_information5,
849                         to_char(to_number(nvl(action_information6,'0')),'FM999,999,990.00'),
850                         to_char(to_number(nvl(action_information7,'0')),'FM999,999,990.00'),
851                         decode(action_information8,'0',null,to_char(to_number(action_information8),'FM999,999,990.00')),
852                         decode(action_information8,'0',null,to_char(to_number(action_information9),'FM999,999,990.00')),
853                         to_char(to_number(nvl(action_information10,'0')),'FM999,999,990.00'),
854                         to_char(to_number(nvl(action_information11,'0')),'FM999,999,990.00'),
855                         to_char(to_number(nvl(action_information12,'0')),'FM999,999,990.00'),
856                         to_char(to_number(nvl(action_information13,'0')),'FM999,999,990.00'),
857                         to_char(to_number(nvl(action_information14,'0')),'FM999,999,990.00'),
858                         to_char(to_number(nvl(action_information15,'0')),'FM999,999,990.00'),
859                         decode(action_information16,'0',null,to_char(to_number(action_information16),'FM999,999,990.00')),
860                         decode(action_information16,'0',null,to_char(to_number(action_information17),'FM999,999,990.00')),
861                         to_char(to_number(nvl(action_information18,'0')),'FM999,999,990.00'),
862                         to_char(to_number(nvl(action_information19,'0')),'FM999,999,990.00'),
863                         decode(action_information20,'0',null,to_char(to_number(action_information20),'FM999,999,990.00')),
864                         decode(action_information20,'0',null,to_char(to_number(action_information21),'FM999,999,990.00')),
865                         to_char(to_number(nvl(action_information22,'0')),'FM999,999,990.00'),
866                         action_information23,
867                         decode(action_information24,'0',null,to_char(to_number(action_information26) + to_number(action_information25),'FM999,999,990.00')), --Changed for bug 8204969
868                         decode(action_information24,'0',null,to_char(to_number(action_information25),'FM999,999,990.00')),
869                         to_char(to_number(nvl(action_information26,'0')),'FM999,999,990.00'),
870                         to_char(to_number(nvl(action_information28,'0')),'FM999,999,990.00'),
871                         to_char(to_number(nvl(action_information29,'0')),'FM999,999,990.00'),
872                         action_information30
873                  into   l_p11d_fields.a_desc, l_p11d_fields.a_cost, l_p11d_fields.a_amg,
874                         l_p11d_fields.a_ce, l_p11d_fields.b_desc, l_p11d_fields.b_ce,
875                         l_p11d_fields.b_tnp, l_p11d_fields.c_cost, l_p11d_fields.c_amg,
876                         l_p11d_fields.c_ce, l_p11d_fields.d_ce, l_p11d_fields.e_ce,
877                         l_p11d_fields.f_tcce, l_p11d_fields.f_tfce, l_p11d_fields.g_ce,
878                         l_p11d_fields.i_cost, l_p11d_fields.i_amg, l_p11d_fields.i_ce,
879                         l_p11d_fields.j_ce, l_p11d_fields.k_cost, l_p11d_fields.k_amg,
880                         l_p11d_fields.k_ce, l_p11d_fields.l_desc, l_p11d_fields.l_cost,
881                         l_p11d_fields.l_amg, l_p11d_fields.l_ce,
882                         l_h_ce, l_h_count, l_f_count
883                  from   pay_action_information pai_emp
884                  where  pai_emp.action_context_id = p_assignment_action_id
885                  and    pai_emp.action_context_type = 'AAP'
886                  and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTA';
887 
888                  select action_information1,
889                         decode(action_information2,'0',null,to_char(to_number(action_information2),'FM999,999,990.00')),
890                         decode(action_information2,'0',null,to_char(to_number(action_information3),'FM999,999,990.00')),
891                         to_char(to_number(nvl(action_information4,'0')),'FM999,999,990.00'),
892                         action_information5,
893                         decode(action_information6,'0',null,to_char(to_number(action_information6),'FM999,999,990.00')),
894                         decode(action_information6,'0',null,to_char(to_number(action_information7),'FM999,999,990.00')),
895                         to_char(to_number(nvl(action_information8,'0')),'FM999,999,990.00'),
896                         to_char(to_number(nvl(action_information9,'0')),'FM999,999,990.00'),
897                         decode(action_information10,'0',null,to_char(to_number(action_information10),'FM999,999,990.00')),
898                         decode(action_information10,'0',null,to_char(to_number(action_information11),'FM999,999,990.00')),
899                         to_char(to_number(nvl(action_information12,'0')),'FM999,999,990.00'),
900                         decode(action_information13,'0',null,to_char(to_number(action_information13),'FM999,999,990.00')),
901                         decode(action_information13,'0',null,to_char(to_number(action_information14),'FM999,999,990.00')),
902                         to_char(to_number(nvl(action_information15,'0')),'FM999,999,990.00'),
903                         decode(action_information16, 'Y', 'Y', 'N'),
904                         decode(action_information17,'0',null,to_char(to_number(action_information17),'FM999,999,990.00')),
905                         decode(action_information17,'0',null,to_char(to_number(action_information18),'FM999,999,990.00')),
906                         to_char(to_number(nvl(action_information19,'0')),'FM999,999,990.00'),
907                         decode(action_information20,'0',null,to_char(to_number(action_information20),'FM999,999,990.00')),
908                         decode(action_information20,'0',null,to_char(to_number(action_information21),'FM999,999,990.00')),
909                         to_char(to_number(nvl(action_information22,'0')),'FM999,999,990.00'),
910                         decode(action_information23,'0',null,to_char(to_number(action_information23),'FM999,999,990.00')),
911                         decode(action_information23,'0',null,to_char(to_number(action_information24),'FM999,999,990.00')),
912                         to_char(to_number(nvl(action_information25,'0')),'FM999,999,990.00'),
913                         action_information26,
914                         decode(action_information27,'0',null,to_char(to_number(action_information27),'FM999,999,990.00')),
915                         decode(action_information27,'0',null,to_char(to_number(action_information28),'FM999,999,990.00')),
916                         to_char(to_number(nvl(action_information29,'0')),'FM999,999,990.00'),
917                         to_char(to_number(nvl(action_information30,'0')),'FM999,999,990.00') -- P11D changes 07/08
918                  into   l_p11d_fields.n_desc, l_p11d_fields.n_cost, l_p11d_fields.n_amg,
919                         l_p11d_fields.n_ce, l_p11d_fields.na_desc, l_p11d_fields.na_cost,
920                         l_p11d_fields.na_amg, l_p11d_fields.na_ce,
921                         l_p11d_fields.n_taxpaid, l_p11d_fields.o1_cost,
922                         l_p11d_fields.o1_amg, l_p11d_fields.o1_ce, l_p11d_fields.o2_cost,
923                         l_p11d_fields.o2_amg, l_p11d_fields.o2_ce,
924                         l_p11d_fields.o_toi,
925                         l_p11d_fields.o3_cost, l_p11d_fields.o3_amg, l_p11d_fields.o3_ce,
926                         l_p11d_fields.o4_cost, l_p11d_fields.o4_amg, l_p11d_fields.o4_ce,
927                         l_p11d_fields.o5_cost, l_p11d_fields.o5_amg, l_p11d_fields.o5_ce,
928                         l_p11d_fields.o6_desc, l_p11d_fields.o6_cost,
929                         l_p11d_fields.o6_amg, l_p11d_fields.o6_ce, l_p11d_fields.g_cef
930                  from   pay_action_information pai_emp
931                  where  pai_emp.action_context_id = p_assignment_action_id
932                  and    pai_emp.action_context_type = 'AAP'
933                  and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTB';
934 
935                  hr_utility.trace('Fetching from Result C');
936                  select to_number(nvl(ACTION_INFORMATION23,'0'))
937                  into   l_h_sum_max_amt_outstanding
938                  from   pay_action_information pai_emp
939                  where  pai_emp.action_context_id = p_assignment_action_id
940                  and    pai_emp.action_context_type = 'AAP'
941                  and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTC';
942              end if;
943 
944         end;
945         procedure populate_car_fields
946         is
947              cursor csr_car_benefits(p_pactid    NUMBER,
948                                      p_person_id VARCHAR2,
949                                      p_emp_ref   VARCHAR2,
950                                      p_emp_name  VARCHAR2)
951              is
952              select
953                    substr(pai_emp.action_information3,9,2) || ' ' ||
954                    substr(pai_emp.action_information3,6,2) || ' ' ||
955                    substr(pai_emp.action_information3,1,4) f_start,
956                    substr(pai_emp.action_information4,9,2) || ' ' ||
957                    substr(pai_emp.action_information4,6,2) || ' ' ||
958                    substr(pai_emp.action_information4,1,4) f_end,
959                    pai_emp.action_information6 || ' ' ||
960                    pai_emp.action_information7 f_make,
961                    substr(pai_emp.action_information8,9,2) || ' ' ||
962                    substr(pai_emp.action_information8,6,2) || ' ' ||
963                    substr(pai_emp.action_information8,1,4) f_dreg,
964                    to_char(to_number(pai_emp.action_information9),'FM999,999,990.00') f_lprice,
965                    to_char(to_number(pai_emp.action_information10),'FM999,999,990.00') f_cc,
966                    to_char(to_number(pai_emp.action_information11),'FM999,999,990.00') f_fcc,
967                    decode( pai_emp.action_information12,'0',null,PAY_GB_P11D_MAGTAPE.get_description(
968                            pai_emp.action_information12,'GB_FUEL_TYPE',pai_emp.action_information4)) f_fuel,
969                    pai_emp.action_information13 f_efig,
970                     /* bug 8277887 checking the flag if co2 emisiion is either zero or null */
971                    decode(NVL(pai_emp.action_information13,'0'),'0','Y', 'N') f_nfig,
972                    to_char(to_number(pai_emp.action_information15),'FM999,999,990.00') f_oprice,
973                    to_char(to_number(pai_emp.action_information16),'FM999,999,990.00') f_cost,
974                    to_char(to_number(pai_emp.action_information17),'FM999,999,990.00') f_amg,
975                    substr(action_information26,9,2) || ' ' ||
976                    substr(action_information26,6,2) || ' ' ||
977                    substr(action_information26,1,4) f_date_free,
978                    decode(action_information27,'Y','Y','N') f_rein_yr,
979                    pai_emp.action_information18 f_esize
980              from  pay_action_information pai_emp
981              where pai_emp.action_information_category = 'CAR AND CAR FUEL 2003_04'
982              and   pai_emp.action_context_type = 'AAP'
983              and   pai_emp.action_context_id in( select paa.assignment_action_id
984                                                  from  pay_action_information pai_comp,
985                                                        pay_action_information pai_person,
986                                                        pay_assignment_actions paa,
987                                                        pay_payroll_actions ppa
988                                                  where ppa.payroll_action_id = p_pactid
989                                                  and   paa.payroll_action_id = ppa.payroll_action_id
990                                                  and   pai_comp.action_context_id = paa.assignment_action_id
991                                                  and   pai_comp.action_information_category = 'EMEA PAYROLL INFO'
992                                                  and   pai_comp.action_context_type = 'AAP'
993                                                  and   pai_person.action_context_id = paa.assignment_action_id
994                                                  and   pai_person.action_information_category = 'ADDRESS DETAILS'
995                                                  and   pai_person.action_context_type = 'AAP'
996                                                  and   pai_person.action_information14 = 'Employee Address'
997                                                  and   pai_person.action_information1 = p_person_id
998                                                  and   pai_comp.action_information6 = p_emp_ref
999                                                  and   pai_comp.action_information7 = p_emp_name)
1000              order by pai_emp.action_information3 desc, -- ben st dt
1001                       pai_emp.action_information4 desc, -- ben end dt
1002                       pai_emp.action_information10 desc, -- cc for car
1003                       pai_emp.action_information11 desc, -- cc for fuel
1004                       pai_emp.action_information1 desc, -- ele entrty id
1005                       pai_emp.action_information2 desc; -- effec date
1006 
1007              l_rec_count         INTEGER := 0;
1008              l_tax_year_start    varchar2(10);
1009              l_tax_year_end      varchar2(10);
1010              l_ben_start         varchar2(10);
1011              l_ben_end           varchar2(10);
1012              l_tax_year          varchar2(4);
1013              l_date_reg          varchar2(10);
1014         begin
1015              hr_utility.trace('l_f_count ' || l_f_count);
1016              if l_f_count > 0
1017              then
1018                  hr_utility.trace('l_payroll_action_id ' || g_payroll_action_id);
1019                  hr_utility.trace('l_person_id ' || g_person_id);
1020                  l_tax_year := pay_gb_p11d_magtape.get_parameters(g_payroll_action_id,'Rep_Run');
1021                  l_tax_year_end := l_tax_year || '0405';
1022                  l_tax_year_start := to_char(to_number(l_tax_year) - 1) || '0406';
1023                  for car_benefits in csr_car_benefits(g_payroll_action_id,
1024                                                       g_person_id,
1025                                                       g_emp_ref_no,
1026                                                       g_employer_name)
1027                  loop
1028                      l_rec_count := l_rec_count + 1;
1029                      hr_utility.trace('l_rec_count ' || l_rec_count);
1030                      l_ben_start := car_benefits.f_start;
1031                      l_ben_end   := car_benefits.f_end;
1032                      l_ben_start := substr(l_ben_start,7,4) || substr(l_ben_start,4,2) || substr(l_ben_start,1,2);
1033                      l_ben_end   := substr(l_ben_end,7,4) || substr(l_ben_end,4,2) || substr(l_ben_end,1,2);
1034                      l_date_reg  := substr(car_benefits.f_dreg,7,4) || substr(car_benefits.f_dreg,4,2) || substr(car_benefits.f_dreg,1,2);
1035                      if l_rec_count = 1
1036                      then
1037                          hr_utility.trace('car_benefits.f_make ' || car_benefits.f_make);
1038                          hr_utility.trace('l_rec_count in 1 ' || l_rec_count);
1039                          if to_number(l_ben_start) > to_number(l_tax_year_start)
1040                          then
1041                              l_p11d_fields.f1_start := car_benefits.f_start;
1042                          else
1043                              l_p11d_fields.f1_start := ' ';
1044                          end if;
1045                          if to_number(l_ben_end) < to_number(l_tax_year_end)
1046                          then
1047                              l_p11d_fields.f1_end := car_benefits.f_end;
1048                          else
1049                              l_p11d_fields.f1_end := ' ';
1050                          end if;
1051                         l_p11d_fields.f1_make := car_benefits.f_make;
1052                         l_p11d_fields.f1_dreg := car_benefits.f_dreg;
1053                         l_p11d_fields.f1_lprice := car_benefits.f_lprice;
1054                         l_p11d_fields.f1_cc := car_benefits.f_cc;
1055                         l_p11d_fields.f1_fcc  := car_benefits.f_fcc;
1056                         l_p11d_fields.f1_fuel := car_benefits.f_fuel;
1057                         /* l_p11d_fields.f1_efig := car_benefits.f_efig; */
1058 			 /* added below if else for bug 8277887 */
1059                         if to_number(nvl(car_benefits.f_efig,0)) > 0 then
1060                         l_p11d_fields.f1_efig := car_benefits.f_efig;
1061                         else
1062                           l_p11d_fields.f1_efig := ' ';
1063                         end if;
1064                         l_p11d_fields.f1_nfig := car_benefits.f_nfig;
1065                         l_p11d_fields.f1_oprice := car_benefits.f_oprice;
1066                         -- l_p11d_fields.f1_aprice := car_benefits.f_aprice;
1067                         l_p11d_fields.f1_cost := car_benefits.f_cost;
1068                         l_p11d_fields.f1_amg := car_benefits.f_amg;
1069                         if car_benefits.f_esize = '9999' and
1070                            to_number(l_date_reg) > 19980101 and
1071                            to_number(nvl(car_benefits.f_efig,0)) > 0 then
1072                            l_p11d_fields.f1_esize := ' ';
1073                          else
1074                           l_p11d_fields.f1_esize := car_benefits.f_esize;
1075                         end if;
1076                         l_p11d_fields.f1_date_free := car_benefits.f_date_free;
1077                         l_p11d_fields.f1_rein_yr := car_benefits.f_rein_yr;
1078                      elsif l_rec_count = 2
1079                      then
1080                          hr_utility.trace('car_benefits.f_make ' || car_benefits.f_make);
1081                          hr_utility.trace('l_rec_count in 2' || l_rec_count);
1082                          if to_number(l_ben_start) > to_number(l_tax_year_start)
1083                          then
1084                              l_p11d_fields.f2_start := car_benefits.f_start;
1085                          else
1086                              l_p11d_fields.f2_start := ' ';
1087                          end if;
1088                          if to_number(l_ben_end) < to_number(l_tax_year_end)
1089                          then
1090                              l_p11d_fields.f2_end := car_benefits.f_end;
1091                          else
1092                              l_p11d_fields.f2_end := ' ';
1093                          end if;
1094                          l_p11d_fields.f2_make := car_benefits.f_make;
1095                          l_p11d_fields.f2_dreg := car_benefits.f_dreg;
1096                          l_p11d_fields.f2_lprice := car_benefits.f_lprice;
1097                          l_p11d_fields.f2_cc := car_benefits.f_cc;
1098                          l_p11d_fields.f2_fcc := car_benefits.f_fcc;
1099                          l_p11d_fields.f2_fuel := car_benefits.f_fuel;
1100                         /* l_p11d_fields.f2_efig := car_benefits.f_efig; */
1101 			/* added below if else for bug 8277887 */
1102                         if to_number(nvl(car_benefits.f_efig,0)) > 0 then
1103                          l_p11d_fields.f2_efig := car_benefits.f_efig;
1104                         else
1105                         l_p11d_fields.f2_efig := ' ';
1106                         end if;
1107                          l_p11d_fields.f2_nfig := car_benefits.f_nfig;
1108                          l_p11d_fields.f2_oprice := car_benefits.f_oprice;
1109                          --l_p11d_fields.f2_aprice := car_benefits.f_aprice;
1110                          l_p11d_fields.f2_cost := car_benefits.f_cost;
1111                          l_p11d_fields.f2_amg := car_benefits.f_amg;
1112                          if car_benefits.f_esize = '9999' and
1113                             to_number(l_date_reg) > 19980101 and
1114                             to_number(nvl(car_benefits.f_efig,0)) > 0 then
1115                             l_p11d_fields.f2_esize := ' ';
1116                          else
1117                             l_p11d_fields.f2_esize := car_benefits.f_esize;
1118                          end if;
1119                          l_p11d_fields.f2_date_free := car_benefits.f_date_free;
1120                          l_p11d_fields.f2_rein_yr := car_benefits.f_rein_yr;
1121                          exit;
1122                      end if;
1123                 end loop;
1124             end if;
1125         end;
1126 
1127         procedure populate_interest_fields
1128         is
1129              cursor csr_int_benefits(p_pactid    NUMBER,
1130                                      p_person_id VARCHAR2,
1131                                      p_emp_ref   VARCHAR2,
1132                                      p_emp_name  VARCHAR2)
1133              is
1134              select
1135                    pai_emp.action_information5 h_njb,
1136                    to_char(to_number(pai_emp.action_information6),'FM999,999,990.00') h_ayb,
1137                    to_char(to_number(pai_emp.action_information7),'FM999,999,990.00') h_mao,
1138                    decode(pai_emp.action_information8,null,'NIL','0','NIL',to_char(to_number(pai_emp.action_information8),'FM999,999,990.00')) h_ip,
1139                    substr(pai_emp.action_information9,9,2) || decode(pai_emp.action_information9,null,null,' ') ||
1140                    substr(pai_emp.action_information9,6,2) || decode(pai_emp.action_information9,null,null,' ') ||
1141                    substr(pai_emp.action_information9,1,4) h_dlm,
1142                    substr(pai_emp.action_information10,9,2) ||decode(pai_emp.action_information10,null,null,' ') ||
1143                    substr(pai_emp.action_information10,6,2) ||decode(pai_emp.action_information10,null,null,' ') ||
1144                    substr(pai_emp.action_information10,1,4) h_dld,
1145                    to_char(to_number(nvl(pai_emp.action_information11,'0')),'FM999,999,990.00') h_ce,
1146                    to_char(to_number(nvl(pai_emp.action_information16,'0')),'FM999,999,990.00') h_aye
1147              from  pay_action_information pai_emp
1148              where pai_emp.action_information_category = 'INT FREE AND LOW INT LOANS'
1149              and   pai_emp.action_context_type = 'AAP'
1150              and   pai_emp.action_context_id in ( select paa.assignment_action_id
1151                                                   from   pay_action_information pai_comp,
1152                                                          pay_action_information pai_person,
1153                                                          pay_assignment_actions paa,
1154                                                          pay_payroll_actions ppa
1155                                                    where ppa.payroll_action_id = p_pactid
1156                                                    and   paa.payroll_action_id = ppa.payroll_action_id
1157                                                    and   pai_comp.action_context_id = paa.assignment_action_id
1158                                                    and   pai_comp.action_information_category = 'EMEA PAYROLL INFO'
1159                                                    and   pai_comp.action_context_type = 'AAP'
1160                                                    and   pai_person.action_context_id = paa.assignment_action_id
1161                                                    and   pai_person.action_information_category = 'ADDRESS DETAILS'
1162                                                    and   pai_person.action_context_type = 'AAP'
1163                                                    and   pai_person.action_information14 = 'Employee Address'
1164                                                    and   pai_person.action_information1 = p_person_id
1165                                                    and   pai_comp.action_information6 = p_emp_ref
1166                                                    and   pai_comp.action_information7 = p_emp_name)
1167              and   to_number(nvl(pai_emp.action_information11,'0')) > 0 -- report only int free lons where CE is greater than 0
1168              order by pai_emp.action_information3, -- ben st dt
1169                       pai_emp.action_information4, -- ben end dt
1170                       pai_emp.action_information9, -- dt loan made
1171                       pai_emp.action_information10, -- dt loan disc
1172                       pai_emp.action_information11, -- cc
1173                       pai_emp.action_information1, -- ele entrty id
1174                       pai_emp.action_information2; -- effec date
1175              l_rec_count         INTEGER := 0;
1176         begin
1177              hr_utility.trace('l_h_count ' || l_h_count);
1178              hr_utility.trace('max outstd ' || l_h_sum_max_amt_outstanding);
1179              if l_h_count > 2  and
1180              -- Added this extra and condition for 3558538
1181              -- this will ensure that only int free lons where CE is greater than 0
1182              -- are reported
1183                  l_h_sum_max_amt_outstanding > 5000
1184              then
1185                  l_p11d_fields.h2_ce := 'See Attached';
1186                  l_p11d_fields.h1_ce := l_h_ce;
1187                  hr_utility.trace('See att ' );
1188              elsif l_h_count > 0  and
1189              -- Added this extra and condition for 3558538
1190              -- this will ensure that only int free lons where CE is greater than 0
1191              -- are reported
1192                  l_h_sum_max_amt_outstanding > 5000
1193              then
1194                  hr_utility.trace('l_payroll_action_id ' || g_payroll_action_id);
1195                  hr_utility.trace('l_person_id ' || g_person_id);
1196                  for int_benefits in csr_int_benefits(g_payroll_action_id,
1197                                                       g_person_id,
1198                                                       g_emp_ref_no,
1199                                                       g_employer_name)
1200                  loop
1201                      l_rec_count := l_rec_count + 1;
1202                      hr_utility.trace('l_rec_count ' || l_rec_count);
1203                      if l_rec_count = 1
1204                      then
1205                          hr_utility.trace('int_benefits.h_njb ' || int_benefits.h_njb);
1206                          l_p11d_fields.h1_njb := int_benefits.h_njb;
1207                          l_p11d_fields.h1_ayb := int_benefits.h_ayb;
1208                          l_p11d_fields.h1_mao := int_benefits.h_mao;
1209                          l_p11d_fields.h1_ip := int_benefits.h_ip;
1210                          l_p11d_fields.h1_dlm := int_benefits.h_dlm;
1211                          l_p11d_fields.h1_dld := int_benefits.h_dld;
1212                          l_p11d_fields.h1_ce := int_benefits.h_ce;
1213                          l_p11d_fields.h1_aye := int_benefits.h_aye;
1214                       elsif l_rec_count = 2
1215                       then
1216                          hr_utility.trace('int_benefits.h_njb ' || int_benefits.h_njb);
1217                          l_p11d_fields.h2_njb := int_benefits.h_njb;
1218                          l_p11d_fields.h2_ayb := int_benefits.h_ayb;
1219                          l_p11d_fields.h2_mao := int_benefits.h_mao;
1220                          l_p11d_fields.h2_ip := int_benefits.h_ip;
1221                          l_p11d_fields.h2_dlm := int_benefits.h_dlm;
1222                          l_p11d_fields.h2_dld := int_benefits.h_dld;
1223                          l_p11d_fields.h2_ce := int_benefits.h_ce;
1224                          l_p11d_fields.h2_aye := int_benefits.h_aye;
1225                          exit;
1226                       end if;
1227                  end loop;
1228              end if;
1229         end;
1230    begin
1231         hr_utility.trace('Inside fetch p11d rep_data p_assignment_action_id' || p_assignment_action_id);
1232         select payroll_action_id
1233         into  g_payroll_action_id
1234         from  pay_assignment_actions
1235         where assignment_action_id = p_assignment_action_id;
1236 
1237         hr_utility.trace('g_payroll_action_id ' || g_payroll_action_id);
1238         populate_stored_fields;
1239         hr_utility.trace('Calling populate car fields ' );
1240         populate_car_fields;
1241         hr_utility.trace('Calling interest fields ' );
1242         populate_interest_fields;
1243         return l_p11d_fields;
1244    end;
1245 --
1246    function fetch_p11d_rep_data_blob(p_assignment_action_id Number) return BLOB
1247    is
1248 
1249         l_p11d_fields l_typ_p11d_fields_rec;
1250         l_xfdf_string varchar2(31000);
1251         l_xfdf_clob   CLOB;
1252         l_xfdf_blob   BLOB;
1253         l_rep_run     VARCHAR2(10);
1254         l_pactid      NUMBER;
1255 		l_fuel_new    VARCHAR2(1);
1256 		l_fuel1_new	VARCHAR2(1);
1257 
1258    begin
1259 
1260         hr_utility.trace('p_assignment_action_id ' || p_assignment_action_id);
1261         l_p11d_fields := fetch_p11d_rep_data(p_assignment_action_id);
1262 
1263         select payroll_action_id
1264         into   l_pactid
1265         from   pay_assignment_actions
1266         where  assignment_action_id = p_assignment_action_id;
1267 
1268         PAY_GB_P11D_ARCHIVE_SS.get_parameters(p_payroll_action_id  => l_pactid,
1269                                               p_token_name         => 'Rep_Run',
1270                                               p_token_value        => l_rep_run);
1271 
1272         if to_number(l_rep_run) < 2005
1273         then
1274             l_xfdf_string := '<?xml version = "1.0" encoding = "UTF-8"?>
1275             <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
1276             <fields> ' ||
1277             ' <field name="'|| 'DIRECTOR_FLAG'   ||'"><value>' ||    l_p11d_fields.DIRECTOR_FLAG       || '</value></field> ' ||
1278             ' <field name="'|| 'FULL_NAME'       ||'"><value>' ||    l_p11d_fields.FULL_NAME           || '</value></field> ' ||
1279             ' <field name="'|| 'EMPLOYEE_NUMBER' ||'"><value>' ||    l_p11d_fields.EMPLOYEE_NUMBER     || '</value></field> ' ||
1280             ' <field name="'|| 'NATIONAL_INS_NO' ||'"><value>' ||    l_p11d_fields.NATIONAL_INS_NO     || '</value></field> ' ||
1281             ' <field name="'|| 'EMPLOYERS_REF_NO'||'"><value>' ||    l_p11d_fields.EMPLOYERS_REF_NO    || '</value></field> ' ||
1282             ' <field name="'|| 'EMPLOYERS_NAME'  ||'"><value>' ||    l_p11d_fields.EMPLOYERS_NAME      || '</value></field> ' ||
1283             ' <field name="'|| 'A_DESC'          ||'"><value>' ||    l_p11d_fields.A_DESC              || '</value></field> ' ||
1284             ' <field name="'|| 'A_COST'          ||'"><value>' ||    l_p11d_fields.A_COST              || '</value></field> ' ||
1285             ' <field name="'|| 'A_AMG'           ||'"><value>' ||    l_p11d_fields.A_AMG               || '</value></field> ' ||
1286             ' <field name="'|| 'A_CE'            ||'"><value>' ||    l_p11d_fields.A_CE                || '</value></field> ' ||
1287             ' <field name="'|| 'B_DESC'          ||'"><value>' ||    l_p11d_fields.B_DESC              || '</value></field> ' ||
1288             ' <field name="'|| 'B_CE'            ||'"><value>' ||    l_p11d_fields.B_CE                || '</value></field> ' ||
1289             ' <field name="'|| 'B_TNP'           ||'"><value>' ||    l_p11d_fields.B_TNP               || '</value></field> ' ||
1290             ' <field name="'|| 'C_COST'          ||'"><value>' ||    l_p11d_fields.C_COST              || '</value></field> ' ||
1291             ' <field name="'|| 'C_AMG'           ||'"><value>' ||    l_p11d_fields.C_AMG               || '</value></field> ' ||
1292             ' <field name="'|| 'C_CE'            ||'"><value>' ||    l_p11d_fields.C_CE                || '</value></field> ' ||
1293             ' <field name="'|| 'D_CE'            ||'"><value>' ||    l_p11d_fields.D_CE                || '</value></field> ' ||
1294             ' <field name="'|| 'E_CE'            ||'"><value>' ||    l_p11d_fields.E_CE                || '</value></field> ' ||
1295             ' <field name="'|| 'F1_MAKE'         ||'"><value>' ||    l_p11d_fields.F1_MAKE             || '</value></field> ' ||
1296             ' <field name="'|| 'F1_DREG1'        ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,0,2)  || '</value></field> ' ||
1297             ' <field name="'|| 'F1_DREG2'        ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,4,2)  || '</value></field> ' ||
1298             ' <field name="'|| 'F1_DREG3'        ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,7)    || '</value></field> ' ||
1299             ' <field name="'|| 'F1_EFIG'         ||'"><value>' ||    l_p11d_fields.F1_EFIG             || '</value></field> ' ||
1300             ' <field name="'|| 'F1_NFIG'         ||'"><value>' ||    l_p11d_fields.F1_NFIG             || '</value></field> ' ||
1301             ' <field name="'|| 'F1_ESIZE'        ||'"><value>' ||    l_p11d_fields.F1_ESIZE            || '</value></field> ' ||
1302             ' <field name="'|| 'F1_FUEL'         ||'"><value>' ||    l_p11d_fields.F1_FUEL             || '</value></field> ' ||
1303             ' <field name="'|| 'F1_START1'       ||'"><value>' ||   substr(l_p11d_fields.F1_START,0,2) || '</value></field> ' ||
1304             ' <field name="'|| 'F1_START2'       ||'"><value>' ||   substr(l_p11d_fields.F1_START,4,2) || '</value></field> ' ||
1305             ' <field name="'|| 'F1_START3'       ||'"><value>' ||   substr(l_p11d_fields.F1_START,7)   || '</value></field> ' ||
1306             ' <field name="'|| 'F1_END1'         ||'"><value>' ||   substr(l_p11d_fields.F1_END,0,2)   || '</value></field> ' ||
1307             ' <field name="'|| 'F1_END2'         ||'"><value>' ||   substr(l_p11d_fields.F1_END,4,2)   || '</value></field> ' ||
1308             ' <field name="'|| 'F1_END3'         ||'"><value>' ||   substr(l_p11d_fields.F1_END,7)     || '</value></field> ' ||
1309             ' <field name="'|| 'F1_LPRICE'       ||'"><value>' ||    l_p11d_fields.F1_LPRICE           || '</value></field> ' ||
1310             ' <field name="'|| 'F1_OPRICE'       ||'"><value>' ||    l_p11d_fields.F1_OPRICE           || '</value></field> ' ||
1311        --   ' <field name="'|| 'F1_APRICE'       ||'"><value>' ||    l_p11d_fields.F1_APRICE           || '</value></field> ' ||
1312             ' <field name="'|| 'F1_COST'         ||'"><value>' ||    l_p11d_fields.F1_COST             || '</value></field> ' ||
1313             ' <field name="'|| 'F1_AMG'          ||'"><value>' ||    l_p11d_fields.F1_AMG              || '</value></field> ' ||
1314             ' <field name="'|| 'F1_CC'           ||'"><value>' ||    l_p11d_fields.F1_CC               || '</value></field> ' ||
1315             ' <field name="'|| 'F1_FCC'          ||'"><value>' ||    l_p11d_fields.F1_FCC              || '</value></field> ' ||
1316             ' <field name="'|| 'F2_MAKE'         ||'"><value>' ||    l_p11d_fields.F2_MAKE             || '</value></field> ' ||
1317             ' <field name="'|| 'F2_DREG1'        ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,0,2)  || '</value></field> ' ||
1318             ' <field name="'|| 'F2_DREG2'        ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,4,2)  || '</value></field> ' ||
1319             ' <field name="'|| 'F2_DREG3'        ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,7)    || '</value></field> ' ||
1320             ' <field name="'|| 'F2_EFIG'         ||'"><value>' ||    l_p11d_fields.F2_EFIG             || '</value></field> ' ||
1321             ' <field name="'|| 'F2_NFIG'         ||'"><value>' ||    l_p11d_fields.F2_NFIG             || '</value></field> ' ||
1322             ' <field name="'|| 'F2_ESIZE'        ||'"><value>' ||    l_p11d_fields.F2_ESIZE            || '</value></field> ' ||
1323             ' <field name="'|| 'F2_FUEL'         ||'"><value>' ||    l_p11d_fields.F2_FUEL             || '</value></field> ' ||
1324             ' <field name="'|| 'F2_START1'       ||'"><value>' ||   substr(l_p11d_fields.F2_START,0,2) || '</value></field> ' ||
1325             ' <field name="'|| 'F2_START2'       ||'"><value>' ||   substr(l_p11d_fields.F2_START,4,2) || '</value></field> ' ||
1326             ' <field name="'|| 'F2_START3'       ||'"><value>' ||   substr(l_p11d_fields.F2_START,7)   || '</value></field> ' ||
1327             ' <field name="'|| 'F2_END1'         ||'"><value>' ||   substr(l_p11d_fields.F2_END,0,2)   || '</value></field> ' ||
1328             ' <field name="'|| 'F2_END2'         ||'"><value>' ||   substr(l_p11d_fields.F2_END,4,2)   || '</value></field> ' ||
1329             ' <field name="'|| 'F2_END3'         ||'"><value>' ||   substr(l_p11d_fields.F2_END,7)     || '</value></field> ' ||
1330             ' <field name="'|| 'F2_LPRICE'       ||'"><value>' ||    l_p11d_fields.F2_LPRICE           || '</value></field> ' ||
1331             ' <field name="'|| 'F2_OPRICE'       ||'"><value>' ||    l_p11d_fields.F2_OPRICE           || '</value></field> ' ||
1332             ' <field name="'|| 'F2_APRICE'       ||'"><value>' ||    l_p11d_fields.F2_APRICE           || '</value></field> ' ||
1333             ' <field name="'|| 'F2_COST'         ||'"><value>' ||    l_p11d_fields.F2_COST             || '</value></field> ' ||
1334             ' <field name="'|| 'F2_AMG'          ||'"><value>' ||    l_p11d_fields.F2_AMG              || '</value></field> ' ||
1335             ' <field name="'|| 'F2_CC'           ||'"><value>' ||    l_p11d_fields.F2_CC               || '</value></field> ' ||
1336             ' <field name="'|| 'F2_FCC'          ||'"><value>' ||    l_p11d_fields.F2_FCC              || '</value></field> ' ||
1337             ' <field name="'|| 'F_DATE_FREE1'    ||'"><value>' ||    substr(l_p11d_fields.F_DATE_FREE,0,2) || '</value></field> ' ||
1338             ' <field name="'|| 'F_DATE_FREE2'    ||'"><value>' ||    substr(l_p11d_fields.F_DATE_FREE,4,2) || '</value></field> ' ||
1339             ' <field name="'|| 'F_DATE_FREE3'    ||'"><value>' ||    substr(l_p11d_fields.F_DATE_FREE,7)   || '</value></field> ' ||
1340             ' <field name="'|| 'F_REIN_YR'       ||'"><value>' ||    l_p11d_fields.F_REIN_YR           || '</value></field> ' ||
1341             ' <field name="'|| 'F_TCCE'          ||'"><value>' ||    l_p11d_fields.F_TCCE              || '</value></field> ' ||
1342             ' <field name="'|| 'F_TFCE'          ||'"><value>' ||    l_p11d_fields.F_TFCE              || '</value></field> ' ||
1343             ' <field name="'|| 'G_CE'            ||'"><value>' ||    l_p11d_fields.G_CE                || '</value></field> ' ||
1344             ' <field name="'|| 'H1_NJB'          ||'"><value>' ||    l_p11d_fields.H1_NJB              || '</value></field> ' ||
1345             ' <field name="'|| 'H1_AYB'          ||'"><value>' ||    l_p11d_fields.H1_AYB              || '</value></field> ' ||
1346             ' <field name="'|| 'H1_AYE'          ||'"><value>' ||    l_p11d_fields.H1_AYE              || '</value></field> ' ||
1347             ' <field name="'|| 'H1_MAO'          ||'"><value>' ||    l_p11d_fields.H1_MAO              || '</value></field> ' ||
1348             ' <field name="'|| 'H1_IP'           ||'"><value>' ||    l_p11d_fields.H1_IP               || '</value></field> ' ||
1349             ' <field name="'|| 'H1_DLM1'         ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,0,2)  || '</value></field> ' ||
1350             ' <field name="'|| 'H1_DLM2'         ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,4,2)  || '</value></field> ' ||
1351             ' <field name="'|| 'H1_DLM3'         ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,7)    || '</value></field> ' ||
1352             ' <field name="'|| 'H1_DLD1'         ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,0,2)  || '</value></field> ' ||
1353             ' <field name="'|| 'H1_DLD2'         ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,4,2)  || '</value></field> ' ||
1354             ' <field name="'|| 'H1_DLD3'         ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,7)    || '</value></field> ' ||
1355             ' <field name="'|| 'H1_CE'           ||'"><value>' ||    nvl(l_p11d_fields.H1_CE,'0.00')   || '</value></field> ' ||
1356             ' <field name="'|| 'H2_NJB'          ||'"><value>' ||    l_p11d_fields.H2_NJB              || '</value></field> ' ||
1357             ' <field name="'|| 'H2_AYB'          ||'"><value>' ||    l_p11d_fields.H2_AYB              || '</value></field> ' ||
1358             ' <field name="'|| 'H2_AYE'          ||'"><value>' ||    l_p11d_fields.H2_AYE              || '</value></field> ' ||
1359             ' <field name="'|| 'H2_MAO'          ||'"><value>' ||    l_p11d_fields.H2_MAO              || '</value></field> ' ||
1360             ' <field name="'|| 'H2_IP'           ||'"><value>' ||    l_p11d_fields.H2_IP               || '</value></field> ' ||
1361             ' <field name="'|| 'H2_DLM1'         ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,0,2)  || '</value></field> ' ||
1362             ' <field name="'|| 'H2_DLM2'         ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,4,2)  || '</value></field> ' ||
1363             ' <field name="'|| 'H2_DLM3'         ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,7)    || '</value></field> ' ||
1364             ' <field name="'|| 'H2_DLD1'         ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,0,2)  || '</value></field> ' ||
1365             ' <field name="'|| 'H2_DLD2'         ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,4,2)  || '</value></field> ' ||
1366             ' <field name="'|| 'H2_DLD3'         ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,7)    || '</value></field> ' ||
1367             ' <field name="'|| 'H2_CE'           ||'"><value>' ||    nvl(l_p11d_fields.H2_CE,'0.00')   || '</value></field> ' ||
1368             ' <field name="'|| 'I_COST'          ||'"><value>' ||    l_p11d_fields.I_COST              || '</value></field> ' ||
1369             ' <field name="'|| 'I_AMG'           ||'"><value>' ||    l_p11d_fields.I_AMG               || '</value></field> ' ||
1370             ' <field name="'|| 'I_CE'            ||'"><value>' ||    l_p11d_fields.I_CE                || '</value></field> ' ||
1371             ' <field name="'|| 'J_CE'            ||'"><value>' ||    l_p11d_fields.J_CE                || '</value></field> ' ||
1372             ' <field name="'|| 'K_COST'          ||'"><value>' ||    l_p11d_fields.K_COST              || '</value></field> ' ||
1373             ' <field name="'|| 'K_AMG'           ||'"><value>' ||    l_p11d_fields.K_AMG               || '</value></field> ' ||
1374             ' <field name="'|| 'K_CE'            ||'"><value>' ||    l_p11d_fields.K_CE                || '</value></field> ' ||
1375             ' <field name="'|| 'L_DESC'          ||'"><value>' ||    l_p11d_fields.L_DESC              || '</value></field> ' ||
1376             ' <field name="'|| 'L_COST'          ||'"><value>' ||    l_p11d_fields.L_COST              || '</value></field> ' ||
1377             ' <field name="'|| 'L_AMG'           ||'"><value>' ||    l_p11d_fields.L_AMG               || '</value></field> ' ||
1378             ' <field name="'|| 'L_CE'            ||'"><value>' ||    l_p11d_fields.L_CE                || '</value></field> ' ||
1379             ' <field name="'|| 'M_SHARES'        ||'"><value>' ||    l_p11d_fields.M_SHARES            || '</value></field> ' ||
1380             ' <field name="'|| 'N_COST'          ||'"><value>' ||    l_p11d_fields.N_COST              || '</value></field> ' ||
1381             ' <field name="'|| 'N_AMG'           ||'"><value>' ||    l_p11d_fields.N_AMG               || '</value></field> ' ||
1382             ' <field name="'|| 'N_CE'            ||'"><value>' ||    l_p11d_fields.N_CE                || '</value></field> ' ||
1383             ' <field name="'|| 'N_DESC'          ||'"><value>' ||    replace(l_p11d_fields.N_DESC,'&','&')              || '</value></field> ' ||
1384             ' <field name="'|| 'NA_COST'         ||'"><value>' ||    l_p11d_fields.NA_COST             || '</value></field> ' ||
1385             ' <field name="'|| 'NA_AMG'          ||'"><value>' ||    l_p11d_fields.NA_AMG              || '</value></field> ' ||
1386             ' <field name="'|| 'NA_CE'           ||'"><value>' ||    l_p11d_fields.NA_CE               || '</value></field> ' ||
1387             ' <field name="'|| 'NA_DESC'         ||'"><value>' ||    l_p11d_fields.NA_DESC             || '</value></field> ' ||
1388             ' <field name="'|| 'N_TAXPAID'       ||'"><value>' ||    l_p11d_fields.N_TAXPAID           || '</value></field> ' ||
1389             ' <field name="'|| 'O1_COST'         ||'"><value>' ||    l_p11d_fields.O1_COST             || '</value></field> ' ||
1390             ' <field name="'|| 'O1_AMG'          ||'"><value>' ||    l_p11d_fields.O1_AMG              || '</value></field> ' ||
1391             ' <field name="'|| 'O1_CE'           ||'"><value>' ||    l_p11d_fields.O1_CE               || '</value></field> ' ||
1392             ' <field name="'|| 'O2_COST'         ||'"><value>' ||    l_p11d_fields.O2_COST             || '</value></field> ' ||
1393             ' <field name="'|| 'O2_AMG'          ||'"><value>' ||    l_p11d_fields.O2_AMG              || '</value></field> ' ||
1394             ' <field name="'|| 'O2_CE'           ||'"><value>' ||    l_p11d_fields.O2_CE               || '</value></field> ' ||
1395             ' <field name="'|| 'O3_COST'         ||'"><value>' ||    l_p11d_fields.O3_COST             || '</value></field> ' ||
1396             ' <field name="'|| 'O3_AMG'          ||'"><value>' ||    l_p11d_fields.O3_AMG              || '</value></field> ' ||
1397             ' <field name="'|| 'O3_CE'           ||'"><value>' ||    l_p11d_fields.O3_CE               || '</value></field> ' ||
1398             ' <field name="'|| 'O4_COST'         ||'"><value>' ||    l_p11d_fields.O4_COST             || '</value></field> ' ||
1399             ' <field name="'|| 'O4_AMG'          ||'"><value>' ||    l_p11d_fields.O4_AMG              || '</value></field> ' ||
1400             ' <field name="'|| 'O4_CE'           ||'"><value>' ||    l_p11d_fields.O4_CE               || '</value></field> ' ||
1401             ' <field name="'|| 'O5_COST'         ||'"><value>' ||    l_p11d_fields.O5_COST             || '</value></field> ' ||
1402             ' <field name="'|| 'O5_AMG'          ||'"><value>' ||    l_p11d_fields.O5_AMG              || '</value></field> ' ||
1403             ' <field name="'|| 'O5_CE'           ||'"><value>' ||    l_p11d_fields.O5_CE               || '</value></field> ' ||
1404             ' <field name="'|| 'O6_COST'         ||'"><value>' ||    l_p11d_fields.O6_COST             || '</value></field> ' ||
1405             ' <field name="'|| 'O6_AMG'          ||'"><value>' ||    l_p11d_fields.O6_AMG              || '</value></field> ' ||
1406             ' <field name="'|| 'O6_CE'           ||'"><value>' ||    l_p11d_fields.O6_CE               || '</value></field> ' ||
1407             ' <field name="'|| 'O6_DESC'         ||'"><value>' ||    l_p11d_fields.O6_DESC             || '</value></field> ' ||
1408             ' <field name="'|| 'O_TOI'           ||'"><value>' ||    l_p11d_fields.O_TOI               || '</value></field> ' ||
1409             '</fields>  </xfdf>';
1410     elsif to_number(l_rep_run) >= 2005 and to_number(l_rep_run) < 2008 then -- P11D changes 07/08
1411 
1412             l_xfdf_string := '<?xml version = "1.0" encoding = "UTF-8"?>
1413             <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
1414             <fields> ' ||
1415             ' <field name="'|| 'DIRECTOR_FLAG'    ||'"><value>' ||    l_p11d_fields.DIRECTOR_FLAG       || '</value></field> ' ||
1416             ' <field name="'|| 'FULL_NAME'        ||'"><value>' ||    l_p11d_fields.FULL_NAME           || '</value></field> ' ||
1417             ' <field name="'|| 'EMPLOYEE_NUMBER'  ||'"><value>' ||    l_p11d_fields.EMPLOYEE_NUMBER     || '</value></field> ' ||
1418             ' <field name="'|| 'NATIONAL_INS_NO'  ||'"><value>' ||    l_p11d_fields.NATIONAL_INS_NO     || '</value></field> ' ||
1419             ' <field name="'||'NI_1'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,1,1) || '</value></field> ' ||
1420             ' <field name="'||'NI_2'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,2,1) || '</value></field> ' ||
1421             ' <field name="'||'NI_3'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,3,1) || '</value></field> ' ||
1422             ' <field name="'||'NI_4'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,4,1) || '</value></field> ' ||
1423             ' <field name="'||'NI_5'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,5,1) || '</value></field> ' ||
1424             ' <field name="'||'NI_6'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,6,1) || '</value></field> ' ||
1425             ' <field name="'||'NI_7'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,7,1) || '</value></field> ' ||
1426             ' <field name="'||'NI_8'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,8,1) || '</value></field> ' ||
1427             ' <field name="'||'NI_9'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,9)   || '</value></field> ' ||
1428             ' <field name="'||'GENDER'            ||'"><value>' ||    l_p11d_fields.gender                   || '</value></field> ' ||
1429             ' <field name="'||'DB1'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,1,1)    || '</value></field> ' ||
1430             ' <field name="'||'DB2'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,2,1)    || '</value></field> ' ||
1431             ' <field name="'||'DB3'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,3,1)    || '</value></field> ' ||
1432             ' <field name="'||'DB4'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,4,1)    || '</value></field> ' ||
1433             ' <field name="'||'DB5'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,5,1)    || '</value></field> ' ||
1434             ' <field name="'||'DB6'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,6,1)    || '</value></field> ' ||
1435             ' <field name="'||'DB7'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,7,1)    || '</value></field> ' ||
1436             ' <field name="'||'DB8'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,8,1)    || '</value></field> ' ||
1437             ' <field name="'|| 'EMPLOYERS_REF_NO' ||'"><value>' ||    l_p11d_fields.EMPLOYERS_REF_NO    || '</value></field> ' ||
1438             ' <field name="'|| 'EMPLOYERS_NAME'   ||'"><value>' ||    l_p11d_fields.EMPLOYERS_NAME      || '</value></field> ' ||
1439             ' <field name="'|| 'A_DESC'           ||'"><value>' ||    l_p11d_fields.A_DESC              || '</value></field> ' ||
1440             ' <field name="'|| 'A_COST'           ||'"><value>' ||    l_p11d_fields.A_COST              || '</value></field> ' ||
1441             ' <field name="'|| 'A_AMG'            ||'"><value>' ||    l_p11d_fields.A_AMG               || '</value></field> ' ||
1442             ' <field name="'|| 'A_CE'             ||'"><value>' ||    l_p11d_fields.A_CE                || '</value></field> ' ||
1443             ' <field name="'|| 'B_DESC'           ||'"><value>' ||    l_p11d_fields.B_DESC              || '</value></field> ' ||
1444             ' <field name="'|| 'B_CE'             ||'"><value>' ||    l_p11d_fields.B_CE                || '</value></field> ' ||
1445             ' <field name="'|| 'B_TNP'            ||'"><value>' ||    l_p11d_fields.B_TNP               || '</value></field> ' ||
1446             ' <field name="'|| 'C_COST'           ||'"><value>' ||    l_p11d_fields.C_COST              || '</value></field> ' ||
1447             ' <field name="'|| 'C_AMG'            ||'"><value>' ||    l_p11d_fields.C_AMG               || '</value></field> ' ||
1448             ' <field name="'|| 'C_CE'             ||'"><value>' ||    l_p11d_fields.C_CE                || '</value></field> ' ||
1449             ' <field name="'|| 'D_CE'             ||'"><value>' ||    l_p11d_fields.D_CE                || '</value></field> ' ||
1450             ' <field name="'|| 'E_CE'             ||'"><value>' ||    l_p11d_fields.E_CE                || '</value></field> ' ||
1451             ' <field name="'|| 'F1_MAKE'          ||'"><value>' ||    l_p11d_fields.F1_MAKE             || '</value></field> ' ||
1452             ' <field name="'|| 'F1_DREG1'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,0,2)  || '</value></field> ' ||
1453             ' <field name="'|| 'F1_DREG2'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,4,2)  || '</value></field> ' ||
1454             ' <field name="'|| 'F1_DREG3'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,7)    || '</value></field> ' ||
1455             ' <field name="'|| 'F1_EFIG'          ||'"><value>' ||    l_p11d_fields.F1_EFIG             || '</value></field> ' ||
1456             ' <field name="'|| 'F1_NFIG'          ||'"><value>' ||    l_p11d_fields.F1_NFIG             || '</value></field> ' ||
1457             ' <field name="'|| 'F1_ESIZE'         ||'"><value>' ||    l_p11d_fields.F1_ESIZE            || '</value></field> ' ||
1458             ' <field name="'|| 'F1_FUEL'          ||'"><value>' ||    l_p11d_fields.F1_FUEL             || '</value></field> ' ||
1459             ' <field name="'|| 'F1_START1'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,0,2) || '</value></field> ' ||
1460             ' <field name="'|| 'F1_START2'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,4,2) || '</value></field> ' ||
1461             ' <field name="'|| 'F1_START3'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,7)   || '</value></field> ' ||
1462             ' <field name="'|| 'F1_END1'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,0,2)   || '</value></field> ' ||
1463             ' <field name="'|| 'F1_END2'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,4,2)   || '</value></field> ' ||
1464             ' <field name="'|| 'F1_END3'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,7)     || '</value></field> ' ||
1465             ' <field name="'|| 'F1_LPRICE'        ||'"><value>' ||    l_p11d_fields.F1_LPRICE           || '</value></field> ' ||
1466             ' <field name="'|| 'F1_OPRICE'        ||'"><value>' ||    l_p11d_fields.F1_OPRICE           || '</value></field> ' ||
1467        --   ' <field name="'|| 'F1_APRICE'        ||'"><value>' ||    l_p11d_fields.F1_APRICE           || '</value></field> ' ||
1468             ' <field name="'|| 'F1_COST'          ||'"><value>' ||    l_p11d_fields.F1_COST             || '</value></field> ' ||
1469             ' <field name="'|| 'F1_AMG'           ||'"><value>' ||    l_p11d_fields.F1_AMG              || '</value></field> ' ||
1470             ' <field name="'|| 'F1_DATE_FREE1'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,0,2)  || '</value></field> ' ||
1471             ' <field name="'|| 'F1_DATE_FREE2'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,4,2)  || '</value></field> ' ||
1472             ' <field name="'|| 'F1_DATE_FREE3'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,7)    || '</value></field> ' ||
1473             ' <field name="'|| 'F1_REIN_YR'       ||'"><value>' ||    l_p11d_fields.F1_REIN_YR          || '</value></field> ' ||
1474             ' <field name="'|| 'F1_CC'            ||'"><value>' ||    l_p11d_fields.F1_CC               || '</value></field> ' ||
1475             ' <field name="'|| 'F1_FCC'           ||'"><value>' ||    l_p11d_fields.F1_FCC              || '</value></field> ' ||
1476             ' <field name="'|| 'F2_MAKE'          ||'"><value>' ||    l_p11d_fields.F2_MAKE             || '</value></field> ' ||
1477             ' <field name="'|| 'F2_DREG1'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,0,2)  || '</value></field> ' ||
1478             ' <field name="'|| 'F2_DREG2'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,4,2)  || '</value></field> ' ||
1479             ' <field name="'|| 'F2_DREG3'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,7)    || '</value></field> ' ||
1480             ' <field name="'|| 'F2_EFIG'          ||'"><value>' ||    l_p11d_fields.F2_EFIG             || '</value></field> ' ||
1481             ' <field name="'|| 'F2_NFIG'          ||'"><value>' ||    l_p11d_fields.F2_NFIG             || '</value></field> ' ||
1482             ' <field name="'|| 'F2_ESIZE'         ||'"><value>' ||    l_p11d_fields.F2_ESIZE            || '</value></field> ' ||
1483             ' <field name="'|| 'F2_FUEL'          ||'"><value>' ||    l_p11d_fields.F2_FUEL             || '</value></field> ' ||
1484             ' <field name="'|| 'F2_START1'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,0,2) || '</value></field> ' ||
1485             ' <field name="'|| 'F2_START2'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,4,2) || '</value></field> ' ||
1486             ' <field name="'|| 'F2_START3'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,7)   || '</value></field> ' ||
1487             ' <field name="'|| 'F2_END1'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,0,2)   || '</value></field> ' ||
1488             ' <field name="'|| 'F2_END2'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,4,2)   || '</value></field> ' ||
1489             ' <field name="'|| 'F2_END3'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,7)     || '</value></field> ' ||
1490             ' <field name="'|| 'F2_LPRICE'        ||'"><value>' ||    l_p11d_fields.F2_LPRICE           || '</value></field> ' ||
1491             ' <field name="'|| 'F2_OPRICE'        ||'"><value>' ||    l_p11d_fields.F2_OPRICE           || '</value></field> ' ||
1492             ' <field name="'|| 'F2_APRICE'        ||'"><value>' ||    l_p11d_fields.F2_APRICE           || '</value></field> ' ||
1493             ' <field name="'|| 'F2_COST'          ||'"><value>' ||    l_p11d_fields.F2_COST             || '</value></field> ' ||
1494             ' <field name="'|| 'F2_AMG'           ||'"><value>' ||    l_p11d_fields.F2_AMG              || '</value></field> ' ||
1495             ' <field name="'|| 'F2_DATE_FREE1'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,0,2) || '</value></field> ' ||
1496             ' <field name="'|| 'F2_DATE_FREE2'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,4,2) || '</value></field> ' ||
1497             ' <field name="'|| 'F2_DATE_FREE3'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,7)   || '</value></field> ' ||
1498             ' <field name="'|| 'F2_REIN_YR'       ||'"><value>' ||    l_p11d_fields.F2_REIN_YR          || '</value></field> ' ||
1499             ' <field name="'|| 'F2_CC'            ||'"><value>' ||    l_p11d_fields.F2_CC               || '</value></field> ' ||
1500             ' <field name="'|| 'F2_FCC'           ||'"><value>' ||    l_p11d_fields.F2_FCC              || '</value></field> ' ||
1501             ' <field name="'|| 'F_TCCE'           ||'"><value>' ||    l_p11d_fields.F_TCCE              || '</value></field> ' ||
1502             ' <field name="'|| 'F_TFCE'           ||'"><value>' ||    l_p11d_fields.F_TFCE              || '</value></field> ' ||
1503             ' <field name="'|| 'G_CE'             ||'"><value>' ||    l_p11d_fields.G_CE                || '</value></field> ' ||
1504             ' <field name="'|| 'H1_NJB'           ||'"><value>' ||    l_p11d_fields.H1_NJB              || '</value></field> ' ||
1505             ' <field name="'|| 'H1_AYB'           ||'"><value>' ||    l_p11d_fields.H1_AYB              || '</value></field> ' ||
1506             ' <field name="'|| 'H1_AYE'           ||'"><value>' ||    l_p11d_fields.H1_AYE              || '</value></field> ' ||
1507             ' <field name="'|| 'H1_MAO'           ||'"><value>' ||    l_p11d_fields.H1_MAO              || '</value></field> ' ||
1508             ' <field name="'|| 'H1_IP'            ||'"><value>' ||    l_p11d_fields.H1_IP               || '</value></field> ' ||
1509             ' <field name="'|| 'H1_DLM1'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,0,2)  || '</value></field> ' ||
1510             ' <field name="'|| 'H1_DLM2'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,4,2)  || '</value></field> ' ||
1511             ' <field name="'|| 'H1_DLM3'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,7)    || '</value></field> ' ||
1512             ' <field name="'|| 'H1_DLD1'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,0,2)  || '</value></field> ' ||
1513             ' <field name="'|| 'H1_DLD2'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,4,2)  || '</value></field> ' ||
1514             ' <field name="'|| 'H1_DLD3'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,7)    || '</value></field> ' ||
1515             ' <field name="'|| 'H1_CE'            ||'"><value>' ||    nvl(l_p11d_fields.H1_CE,'0.00')   || '</value></field> ' ||
1516             ' <field name="'|| 'H2_NJB'           ||'"><value>' ||    l_p11d_fields.H2_NJB              || '</value></field> ' ||
1517             ' <field name="'|| 'H2_AYB'           ||'"><value>' ||    l_p11d_fields.H2_AYB              || '</value></field> ' ||
1518             ' <field name="'|| 'H2_AYE'           ||'"><value>' ||    l_p11d_fields.H2_AYE              || '</value></field> ' ||
1519             ' <field name="'|| 'H2_MAO'           ||'"><value>' ||    l_p11d_fields.H2_MAO              || '</value></field> ' ||
1520             ' <field name="'|| 'H2_IP'            ||'"><value>' ||    l_p11d_fields.H2_IP               || '</value></field> ' ||
1521             ' <field name="'|| 'H2_DLM1'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,0,2)  || '</value></field> ' ||
1522             ' <field name="'|| 'H2_DLM2'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,4,2)  || '</value></field> ' ||
1523             ' <field name="'|| 'H2_DLM3'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,7)    || '</value></field> ' ||
1524             ' <field name="'|| 'H2_DLD1'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,0,2)  || '</value></field> ' ||
1525             ' <field name="'|| 'H2_DLD2'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,4,2)  || '</value></field> ' ||
1526             ' <field name="'|| 'H2_DLD3'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,7)    || '</value></field> ' ||
1527             ' <field name="'|| 'H2_CE'            ||'"><value>' ||    nvl(l_p11d_fields.H2_CE,'0.00')   || '</value></field> ' ||
1528             ' <field name="'|| 'I_COST'           ||'"><value>' ||    l_p11d_fields.I_COST              || '</value></field> ' ||
1529             ' <field name="'|| 'I_AMG'            ||'"><value>' ||    l_p11d_fields.I_AMG               || '</value></field> ' ||
1530             ' <field name="'|| 'I_CE'             ||'"><value>' ||    l_p11d_fields.I_CE                || '</value></field> ' ||
1531             ' <field name="'|| 'J_CE'             ||'"><value>' ||    l_p11d_fields.J_CE                || '</value></field> ' ||
1532             ' <field name="'|| 'K_COST'           ||'"><value>' ||    l_p11d_fields.K_COST              || '</value></field> ' ||
1533             ' <field name="'|| 'K_AMG'            ||'"><value>' ||    l_p11d_fields.K_AMG               || '</value></field> ' ||
1534             ' <field name="'|| 'K_CE'             ||'"><value>' ||    l_p11d_fields.K_CE                || '</value></field> ' ||
1535             ' <field name="'|| 'L_DESC'           ||'"><value>' ||    l_p11d_fields.L_DESC              || '</value></field> ' ||
1536             ' <field name="'|| 'L_COST'           ||'"><value>' ||    l_p11d_fields.L_COST              || '</value></field> ' ||
1537             ' <field name="'|| 'L_AMG'            ||'"><value>' ||    l_p11d_fields.L_AMG               || '</value></field> ' ||
1538             ' <field name="'|| 'L_CE'             ||'"><value>' ||    l_p11d_fields.L_CE                || '</value></field> ' ||
1539             ' <field name="'|| 'M_COST'           ||'"><value>' ||    l_p11d_fields.N_COST              || '</value></field> ' ||
1540             ' <field name="'|| 'M_AMG'            ||'"><value>' ||    l_p11d_fields.N_AMG               || '</value></field> ' ||
1541             ' <field name="'|| 'M_CE'             ||'"><value>' ||    l_p11d_fields.N_CE                || '</value></field> ' ||
1542             ' <field name="'|| 'M_DESC'           ||'"><value>' ||    replace(l_p11d_fields.N_DESC,'&','&')  || '</value></field> ' ||
1543             ' <field name="'|| 'MA_COST'          ||'"><value>' ||    l_p11d_fields.NA_COST             || '</value></field> ' ||
1544             ' <field name="'|| 'MA_AMG'           ||'"><value>' ||    l_p11d_fields.NA_AMG              || '</value></field> ' ||
1545             ' <field name="'|| 'MA_CE'            ||'"><value>' ||    l_p11d_fields.NA_CE               || '</value></field> ' ||
1546             ' <field name="'|| 'MA_DESC'          ||'"><value>' ||    l_p11d_fields.NA_DESC             || '</value></field> ' ||
1547             ' <field name="'|| 'M_TAXPAID'        ||'"><value>' ||    l_p11d_fields.N_TAXPAID           || '</value></field> ' ||
1548             ' <field name="'|| 'N1_COST'          ||'"><value>' ||    l_p11d_fields.O1_COST             || '</value></field> ' ||
1549             ' <field name="'|| 'N1_AMG'           ||'"><value>' ||    l_p11d_fields.O1_AMG              || '</value></field> ' ||
1550             ' <field name="'|| 'N1_CE'            ||'"><value>' ||    l_p11d_fields.O1_CE               || '</value></field> ' ||
1551             ' <field name="'|| 'N2_COST'          ||'"><value>' ||    l_p11d_fields.O2_COST             || '</value></field> ' ||
1552             ' <field name="'|| 'N2_AMG'           ||'"><value>' ||    l_p11d_fields.O2_AMG              || '</value></field> ' ||
1553             ' <field name="'|| 'N2_CE'            ||'"><value>' ||    l_p11d_fields.O2_CE               || '</value></field> ' ||
1554             ' <field name="'|| 'N3_COST'          ||'"><value>' ||    l_p11d_fields.O3_COST             || '</value></field> ' ||
1555             ' <field name="'|| 'N3_AMG'           ||'"><value>' ||    l_p11d_fields.O3_AMG              || '</value></field> ' ||
1556             ' <field name="'|| 'N3_CE'            ||'"><value>' ||    l_p11d_fields.O3_CE               || '</value></field> ' ||
1557             ' <field name="'|| 'N4_COST'          ||'"><value>' ||    l_p11d_fields.O4_COST             || '</value></field> ' ||
1558             ' <field name="'|| 'N4_AMG'           ||'"><value>' ||    l_p11d_fields.O4_AMG              || '</value></field> ' ||
1559             ' <field name="'|| 'N4_CE'            ||'"><value>' ||    l_p11d_fields.O4_CE               || '</value></field> ' ||
1560             ' <field name="'|| 'N5_COST'          ||'"><value>' ||    l_p11d_fields.O5_COST             || '</value></field> ' ||
1561             ' <field name="'|| 'N5_AMG'           ||'"><value>' ||    l_p11d_fields.O5_AMG              || '</value></field> ' ||
1562             ' <field name="'|| 'N5_CE'            ||'"><value>' ||    l_p11d_fields.O5_CE               || '</value></field> ' ||
1563             ' <field name="'|| 'N6_COST'          ||'"><value>' ||    l_p11d_fields.O6_COST             || '</value></field> ' ||
1564             ' <field name="'|| 'N6_AMG'           ||'"><value>' ||    l_p11d_fields.O6_AMG              || '</value></field> ' ||
1565             ' <field name="'|| 'N6_CE'            ||'"><value>' ||    l_p11d_fields.O6_CE               || '</value></field> ' ||
1566             ' <field name="'|| 'N6_DESC'          ||'"><value>' ||    l_p11d_fields.O6_DESC             || '</value></field> ' ||
1567             ' <field name="'|| 'N_TOI'            ||'"><value>' ||    l_p11d_fields.O_TOI               || '</value></field> ' ||
1568             '</fields>  </xfdf>';
1569 
1570 		-- P11d changes 07/08
1571 		   elsif to_number(l_rep_run) >= 2008 and to_number(l_rep_run) < 2012 then -- P11D changes 07/08
1572 
1573             l_xfdf_string := '<?xml version = "1.0" encoding = "UTF-8"?>
1574             <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
1575             <fields> ' ||
1576             ' <field name="'|| 'DIRECTOR_FLAG'    ||'"><value>' ||    l_p11d_fields.DIRECTOR_FLAG       || '</value></field> ' ||
1577             ' <field name="'|| 'SUR_NAME'        ||'"><value>' ||     l_p11d_fields.SUR_NAME           || '</value></field> ' ||
1578             ' <field name="'|| 'FORE_NAME'        ||'"><value>' ||    l_p11d_fields.FORE_NAME           || '</value></field> ' ||
1579             ' <field name="'|| 'EMPLOYEE_NUMBER'  ||'"><value>' ||    l_p11d_fields.EMPLOYEE_NUMBER     || '</value></field> ' ||
1580             ' <field name="'|| 'NATIONAL_INS_NO'  ||'"><value>' ||    l_p11d_fields.NATIONAL_INS_NO     || '</value></field> ' ||
1581             ' <field name="'||'NI_1'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,1,1) || '</value></field> ' ||
1582             ' <field name="'||'NI_2'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,2,1) || '</value></field> ' ||
1583             ' <field name="'||'NI_3'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,3,1) || '</value></field> ' ||
1584             ' <field name="'||'NI_4'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,4,1) || '</value></field> ' ||
1585             ' <field name="'||'NI_5'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,5,1) || '</value></field> ' ||
1586             ' <field name="'||'NI_6'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,6,1) || '</value></field> ' ||
1587             ' <field name="'||'NI_7'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,7,1) || '</value></field> ' ||
1588             ' <field name="'||'NI_8'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,8,1) || '</value></field> ' ||
1589             ' <field name="'||'NI_9'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,9)   || '</value></field> ' ||
1590             ' <field name="'||'GENDER'            ||'"><value>' ||    l_p11d_fields.gender                   || '</value></field> ' ||
1591             ' <field name="'||'DB1'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,1,1)    || '</value></field> ' ||
1592             ' <field name="'||'DB2'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,2,1)    || '</value></field> ' ||
1593             ' <field name="'||'DB3'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,3,1)    || '</value></field> ' ||
1594             ' <field name="'||'DB4'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,4,1)    || '</value></field> ' ||
1595             ' <field name="'||'DB5'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,5,1)    || '</value></field> ' ||
1596             ' <field name="'||'DB6'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,6,1)    || '</value></field> ' ||
1597             ' <field name="'||'DB7'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,7,1)    || '</value></field> ' ||
1598             ' <field name="'||'DB8'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,8,1)    || '</value></field> ' ||
1599             ' <field name="'|| 'EMPLOYERS_REF_NO' ||'"><value>' ||    l_p11d_fields.EMPLOYERS_REF_NO    || '</value></field> ' ||
1600             ' <field name="'|| 'EMPLOYERS_NAME'   ||'"><value>' ||    l_p11d_fields.EMPLOYERS_NAME      || '</value></field> ' ||
1601             ' <field name="'|| 'A_DESC'           ||'"><value>' ||    l_p11d_fields.A_DESC              || '</value></field> ' ||
1602             ' <field name="'|| 'A_COST'           ||'"><value>' ||    l_p11d_fields.A_COST              || '</value></field> ' ||
1603             ' <field name="'|| 'A_AMG'            ||'"><value>' ||    l_p11d_fields.A_AMG               || '</value></field> ' ||
1604             ' <field name="'|| 'A_CE'             ||'"><value>' ||    l_p11d_fields.A_CE                || '</value></field> ' ||
1605             ' <field name="'|| 'B_DESC'           ||'"><value>' ||    l_p11d_fields.B_DESC              || '</value></field> ' ||
1606             ' <field name="'|| 'B_CE'             ||'"><value>' ||    l_p11d_fields.B_CE                || '</value></field> ' ||
1607             ' <field name="'|| 'B_TNP'            ||'"><value>' ||    l_p11d_fields.B_TNP               || '</value></field> ' ||
1608             ' <field name="'|| 'C_COST'           ||'"><value>' ||    l_p11d_fields.C_COST              || '</value></field> ' ||
1609             ' <field name="'|| 'C_AMG'            ||'"><value>' ||    l_p11d_fields.C_AMG               || '</value></field> ' ||
1610             ' <field name="'|| 'C_CE'             ||'"><value>' ||    l_p11d_fields.C_CE                || '</value></field> ' ||
1611             ' <field name="'|| 'D_CE'             ||'"><value>' ||    l_p11d_fields.D_CE                || '</value></field> ' ||
1612             ' <field name="'|| 'E_CE'             ||'"><value>' ||    l_p11d_fields.E_CE                || '</value></field> ' ||
1613             ' <field name="'|| 'F1_MAKE'          ||'"><value>' ||    l_p11d_fields.F1_MAKE             || '</value></field> ' ||
1614             ' <field name="'|| 'F1_DREG1'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,0,2)  || '</value></field> ' ||
1615             ' <field name="'|| 'F1_DREG2'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,4,2)  || '</value></field> ' ||
1616             ' <field name="'|| 'F1_DREG3'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,7)    || '</value></field> ' ||
1617             ' <field name="'|| 'F1_EFIG'          ||'"><value>' ||    l_p11d_fields.F1_EFIG             || '</value></field> ' ||
1618             ' <field name="'|| 'F1_NFIG'          ||'"><value>' ||    l_p11d_fields.F1_NFIG             || '</value></field> ' ||
1619             ' <field name="'|| 'F1_ESIZE'         ||'"><value>' ||    l_p11d_fields.F1_ESIZE            || '</value></field> ' ||
1620             ' <field name="'|| 'F1_FUEL'          ||'"><value>' ||    l_p11d_fields.F1_FUEL             || '</value></field> ' ||
1621             ' <field name="'|| 'F1_START1'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,0,2) || '</value></field> ' ||
1622             ' <field name="'|| 'F1_START2'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,4,2) || '</value></field> ' ||
1623             ' <field name="'|| 'F1_START3'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,7)   || '</value></field> ' ||
1624             ' <field name="'|| 'F1_END1'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,0,2)   || '</value></field> ' ||
1625             ' <field name="'|| 'F1_END2'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,4,2)   || '</value></field> ' ||
1626             ' <field name="'|| 'F1_END3'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,7)     || '</value></field> ' ||
1627             ' <field name="'|| 'F1_LPRICE'        ||'"><value>' ||    l_p11d_fields.F1_LPRICE           || '</value></field> ' ||
1628             ' <field name="'|| 'F1_OPRICE'        ||'"><value>' ||    l_p11d_fields.F1_OPRICE           || '</value></field> ' ||
1629        --   ' <field name="'|| 'F1_APRICE'        ||'"><value>' ||    l_p11d_fields.F1_APRICE           || '</value></field> ' ||
1630             ' <field name="'|| 'F1_COST'          ||'"><value>' ||    l_p11d_fields.F1_COST             || '</value></field> ' ||
1631             ' <field name="'|| 'F1_AMG'           ||'"><value>' ||    l_p11d_fields.F1_AMG              || '</value></field> ' ||
1632             ' <field name="'|| 'F1_DATE_FREE1'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,0,2)  || '</value></field> ' ||
1633             ' <field name="'|| 'F1_DATE_FREE2'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,4,2)  || '</value></field> ' ||
1634             ' <field name="'|| 'F1_DATE_FREE3'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,7)    || '</value></field> ' ||
1635             ' <field name="'|| 'F1_REIN_YR'       ||'"><value>' ||    l_p11d_fields.F1_REIN_YR          || '</value></field> ' ||
1636             ' <field name="'|| 'F1_CC'            ||'"><value>' ||    l_p11d_fields.F1_CC               || '</value></field> ' ||
1637             ' <field name="'|| 'F1_FCC'           ||'"><value>' ||    l_p11d_fields.F1_FCC              || '</value></field> ' ||
1638             ' <field name="'|| 'F2_MAKE'          ||'"><value>' ||    l_p11d_fields.F2_MAKE             || '</value></field> ' ||
1639             ' <field name="'|| 'F2_DREG1'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,0,2)  || '</value></field> ' ||
1640             ' <field name="'|| 'F2_DREG2'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,4,2)  || '</value></field> ' ||
1641             ' <field name="'|| 'F2_DREG3'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,7)    || '</value></field> ' ||
1642             ' <field name="'|| 'F2_EFIG'          ||'"><value>' ||    l_p11d_fields.F2_EFIG             || '</value></field> ' ||
1643             ' <field name="'|| 'F2_NFIG'          ||'"><value>' ||    l_p11d_fields.F2_NFIG             || '</value></field> ' ||
1644             ' <field name="'|| 'F2_ESIZE'         ||'"><value>' ||    l_p11d_fields.F2_ESIZE            || '</value></field> ' ||
1645             ' <field name="'|| 'F2_FUEL'          ||'"><value>' ||    l_p11d_fields.F2_FUEL             || '</value></field> ' ||
1646             ' <field name="'|| 'F2_START1'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,0,2) || '</value></field> ' ||
1647             ' <field name="'|| 'F2_START2'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,4,2) || '</value></field> ' ||
1648             ' <field name="'|| 'F2_START3'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,7)   || '</value></field> ' ||
1649             ' <field name="'|| 'F2_END1'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,0,2)   || '</value></field> ' ||
1650             ' <field name="'|| 'F2_END2'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,4,2)   || '</value></field> ' ||
1651             ' <field name="'|| 'F2_END3'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,7)     || '</value></field> ' ||
1652             ' <field name="'|| 'F2_LPRICE'        ||'"><value>' ||    l_p11d_fields.F2_LPRICE           || '</value></field> ' ||
1653             ' <field name="'|| 'F2_OPRICE'        ||'"><value>' ||    l_p11d_fields.F2_OPRICE           || '</value></field> ' ||
1654             ' <field name="'|| 'F2_APRICE'        ||'"><value>' ||    l_p11d_fields.F2_APRICE           || '</value></field> ' ||
1655             ' <field name="'|| 'F2_COST'          ||'"><value>' ||    l_p11d_fields.F2_COST             || '</value></field> ' ||
1656             ' <field name="'|| 'F2_AMG'           ||'"><value>' ||    l_p11d_fields.F2_AMG              || '</value></field> ' ||
1657             ' <field name="'|| 'F2_DATE_FREE1'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,0,2) || '</value></field> ' ||
1658             ' <field name="'|| 'F2_DATE_FREE2'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,4,2) || '</value></field> ' ||
1659             ' <field name="'|| 'F2_DATE_FREE3'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,7)   || '</value></field> ' ||
1660             ' <field name="'|| 'F2_REIN_YR'       ||'"><value>' ||    l_p11d_fields.F2_REIN_YR          || '</value></field> ' ||
1661             ' <field name="'|| 'F2_CC'            ||'"><value>' ||    l_p11d_fields.F2_CC               || '</value></field> ' ||
1662             ' <field name="'|| 'F2_FCC'           ||'"><value>' ||    l_p11d_fields.F2_FCC              || '</value></field> ' ||
1663             ' <field name="'|| 'F_TCCE'           ||'"><value>' ||    l_p11d_fields.F_TCCE              || '</value></field> ' ||
1664             ' <field name="'|| 'F_TFCE'           ||'"><value>' ||    l_p11d_fields.F_TFCE              || '</value></field> ' ||
1665             ' <field name="'|| 'G_CE'             ||'"><value>' ||    l_p11d_fields.G_CE                || '</value></field> ' ||
1666             ' <field name="'|| 'G_CEF'            ||'"><value>' ||    l_p11d_fields.G_CEF               || '</value></field> ' ||
1667             ' <field name="'|| 'H1_NJB'           ||'"><value>' ||    l_p11d_fields.H1_NJB              || '</value></field> ' ||
1668             ' <field name="'|| 'H1_AYB'           ||'"><value>' ||    l_p11d_fields.H1_AYB              || '</value></field> ' ||
1669             ' <field name="'|| 'H1_AYE'           ||'"><value>' ||    l_p11d_fields.H1_AYE              || '</value></field> ' ||
1670             ' <field name="'|| 'H1_MAO'           ||'"><value>' ||    l_p11d_fields.H1_MAO              || '</value></field> ' ||
1671             ' <field name="'|| 'H1_IP'            ||'"><value>' ||    l_p11d_fields.H1_IP               || '</value></field> ' ||
1672             ' <field name="'|| 'H1_DLM1'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,0,2)  || '</value></field> ' ||
1673             ' <field name="'|| 'H1_DLM2'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,4,2)  || '</value></field> ' ||
1674             ' <field name="'|| 'H1_DLM3'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,7)    || '</value></field> ' ||
1675             ' <field name="'|| 'H1_DLD1'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,0,2)  || '</value></field> ' ||
1676             ' <field name="'|| 'H1_DLD2'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,4,2)  || '</value></field> ' ||
1677             ' <field name="'|| 'H1_DLD3'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,7)    || '</value></field> ' ||
1678             ' <field name="'|| 'H1_CE'            ||'"><value>' ||    nvl(l_p11d_fields.H1_CE,'0.00')   || '</value></field> ' ||
1679             ' <field name="'|| 'H2_NJB'           ||'"><value>' ||    l_p11d_fields.H2_NJB              || '</value></field> ' ||
1680             ' <field name="'|| 'H2_AYB'           ||'"><value>' ||    l_p11d_fields.H2_AYB              || '</value></field> ' ||
1681             ' <field name="'|| 'H2_AYE'           ||'"><value>' ||    l_p11d_fields.H2_AYE              || '</value></field> ' ||
1682             ' <field name="'|| 'H2_MAO'           ||'"><value>' ||    l_p11d_fields.H2_MAO              || '</value></field> ' ||
1683             ' <field name="'|| 'H2_IP'            ||'"><value>' ||    l_p11d_fields.H2_IP               || '</value></field> ' ||
1684             ' <field name="'|| 'H2_DLM1'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,0,2)  || '</value></field> ' ||
1685             ' <field name="'|| 'H2_DLM2'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,4,2)  || '</value></field> ' ||
1686             ' <field name="'|| 'H2_DLM3'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,7)    || '</value></field> ' ||
1687             ' <field name="'|| 'H2_DLD1'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,0,2)  || '</value></field> ' ||
1688             ' <field name="'|| 'H2_DLD2'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,4,2)  || '</value></field> ' ||
1689             ' <field name="'|| 'H2_DLD3'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,7)    || '</value></field> ' ||
1690             ' <field name="'|| 'H2_CE'            ||'"><value>' ||    nvl(l_p11d_fields.H2_CE,'0.00')   || '</value></field> ' ||
1691             ' <field name="'|| 'I_COST'           ||'"><value>' ||    l_p11d_fields.I_COST              || '</value></field> ' ||
1692             ' <field name="'|| 'I_AMG'            ||'"><value>' ||    l_p11d_fields.I_AMG               || '</value></field> ' ||
1693             ' <field name="'|| 'I_CE'             ||'"><value>' ||    l_p11d_fields.I_CE                || '</value></field> ' ||
1694             ' <field name="'|| 'J_CE'             ||'"><value>' ||    l_p11d_fields.J_CE                || '</value></field> ' ||
1695             ' <field name="'|| 'K_COST'           ||'"><value>' ||    l_p11d_fields.K_COST              || '</value></field> ' ||
1696             ' <field name="'|| 'K_AMG'            ||'"><value>' ||    l_p11d_fields.K_AMG               || '</value></field> ' ||
1697             ' <field name="'|| 'K_CE'             ||'"><value>' ||    l_p11d_fields.K_CE                || '</value></field> ' ||
1698             ' <field name="'|| 'L_DESC'           ||'"><value>' ||    l_p11d_fields.L_DESC              || '</value></field> ' ||
1699             ' <field name="'|| 'L_COST'           ||'"><value>' ||    l_p11d_fields.L_COST              || '</value></field> ' ||
1700             ' <field name="'|| 'L_AMG'            ||'"><value>' ||    l_p11d_fields.L_AMG               || '</value></field> ' ||
1701             ' <field name="'|| 'L_CE'             ||'"><value>' ||    l_p11d_fields.L_CE                || '</value></field> ' ||
1702             ' <field name="'|| 'M_COST'           ||'"><value>' ||    l_p11d_fields.N_COST              || '</value></field> ' ||
1703             ' <field name="'|| 'M_AMG'            ||'"><value>' ||    l_p11d_fields.N_AMG               || '</value></field> ' ||
1704             ' <field name="'|| 'M_CE'             ||'"><value>' ||    l_p11d_fields.N_CE                || '</value></field> ' ||
1705             ' <field name="'|| 'M_DESC'           ||'"><value>' ||    replace(l_p11d_fields.N_DESC,'&','&')  || '</value></field> ' ||
1706             ' <field name="'|| 'MA_COST'          ||'"><value>' ||    l_p11d_fields.NA_COST             || '</value></field> ' ||
1707             ' <field name="'|| 'MA_AMG'           ||'"><value>' ||    l_p11d_fields.NA_AMG              || '</value></field> ' ||
1708             ' <field name="'|| 'MA_CE'            ||'"><value>' ||    l_p11d_fields.NA_CE               || '</value></field> ' ||
1709             ' <field name="'|| 'MA_DESC'          ||'"><value>' ||    l_p11d_fields.NA_DESC             || '</value></field> ' ||
1710             ' <field name="'|| 'M_TAXPAID'        ||'"><value>' ||    l_p11d_fields.N_TAXPAID           || '</value></field> ' ||
1711             ' <field name="'|| 'N1_COST'          ||'"><value>' ||    l_p11d_fields.O1_COST             || '</value></field> ' ||
1712             ' <field name="'|| 'N1_AMG'           ||'"><value>' ||    l_p11d_fields.O1_AMG              || '</value></field> ' ||
1713             ' <field name="'|| 'N1_CE'            ||'"><value>' ||    l_p11d_fields.O1_CE               || '</value></field> ' ||
1714             ' <field name="'|| 'N2_COST'          ||'"><value>' ||    l_p11d_fields.O2_COST             || '</value></field> ' ||
1715             ' <field name="'|| 'N2_AMG'           ||'"><value>' ||    l_p11d_fields.O2_AMG              || '</value></field> ' ||
1716             ' <field name="'|| 'N2_CE'            ||'"><value>' ||    l_p11d_fields.O2_CE               || '</value></field> ' ||
1717             ' <field name="'|| 'N3_COST'          ||'"><value>' ||    l_p11d_fields.O3_COST             || '</value></field> ' ||
1718             ' <field name="'|| 'N3_AMG'           ||'"><value>' ||    l_p11d_fields.O3_AMG              || '</value></field> ' ||
1719             ' <field name="'|| 'N3_CE'            ||'"><value>' ||    l_p11d_fields.O3_CE               || '</value></field> ' ||
1720             ' <field name="'|| 'N4_COST'          ||'"><value>' ||    l_p11d_fields.O4_COST             || '</value></field> ' ||
1721             ' <field name="'|| 'N4_AMG'           ||'"><value>' ||    l_p11d_fields.O4_AMG              || '</value></field> ' ||
1722             ' <field name="'|| 'N4_CE'            ||'"><value>' ||    l_p11d_fields.O4_CE               || '</value></field> ' ||
1723             ' <field name="'|| 'N5_COST'          ||'"><value>' ||    l_p11d_fields.O5_COST             || '</value></field> ' ||
1724             ' <field name="'|| 'N5_AMG'           ||'"><value>' ||    l_p11d_fields.O5_AMG              || '</value></field> ' ||
1725             ' <field name="'|| 'N5_CE'            ||'"><value>' ||    l_p11d_fields.O5_CE               || '</value></field> ' ||
1726             ' <field name="'|| 'N6_COST'          ||'"><value>' ||    l_p11d_fields.O6_COST             || '</value></field> ' ||
1727             ' <field name="'|| 'N6_AMG'           ||'"><value>' ||    l_p11d_fields.O6_AMG              || '</value></field> ' ||
1728             ' <field name="'|| 'N6_CE'            ||'"><value>' ||    l_p11d_fields.O6_CE               || '</value></field> ' ||
1729             ' <field name="'|| 'N6_DESC'          ||'"><value>' ||    l_p11d_fields.O6_DESC             || '</value></field> ' ||
1730             ' <field name="'|| 'N_TOI'            ||'"><value>' ||    l_p11d_fields.O_TOI               || '</value></field> ' ||
1731             '</fields>  </xfdf>';
1732 
1733 			--P11D 2012/13
1734 			else
1735 				if (l_p11d_fields.F1_FUEL = 'B' or l_p11d_fields.F1_FUEL = 'C' or
1736 									l_p11d_fields.F1_FUEL = 'H' or l_p11d_fields.F1_FUEL = 'G' or
1737 									l_p11d_fields.F1_FUEL = 'P' ) then
1738 					l_fuel_new :='A';
1739 					elsif (l_p11d_fields.F1_FUEL = 'L' or (l_p11d_fields.F1_FUEL = 'D' )) then
1740 					l_fuel_new :='D';
1741 					else
1742 					l_fuel_new := l_p11d_fields.F1_FUEL;
1743 				end if;
1744 
1745 				if (l_p11d_fields.F2_FUEL = 'B' or l_p11d_fields.F2_FUEL = 'C' or
1746                         l_p11d_fields.F2_FUEL = 'H' or l_p11d_fields.F2_FUEL = 'G' or
1747                         l_p11d_fields.F2_FUEL = 'P' ) then
1748 					l_fuel1_new :='A';
1749 					elsif (l_p11d_fields.F2_FUEL = 'L' or (l_p11d_fields.F2_FUEL = 'D' )) then
1750 					l_fuel1_new :='D';
1751 					else
1752 					l_fuel1_new := l_p11d_fields.F2_FUEL;
1753 				end if;
1754 
1755 			 l_xfdf_string := '<?xml version = "1.0" encoding = "UTF-8"?>
1756             <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
1757             <fields> ' ||
1758             ' <field name="'|| 'DIRECTOR_FLAG'    ||'"><value>' ||    l_p11d_fields.DIRECTOR_FLAG       || '</value></field> ' ||
1759             ' <field name="'|| 'SUR_NAME'        ||'"><value>' ||     l_p11d_fields.SUR_NAME           || '</value></field> ' ||
1760             ' <field name="'|| 'FORE_NAME'        ||'"><value>' ||    l_p11d_fields.FORE_NAME           || '</value></field> ' ||
1761             ' <field name="'|| 'EMPLOYEE_NUMBER'  ||'"><value>' ||    l_p11d_fields.EMPLOYEE_NUMBER     || '</value></field> ' ||
1762             ' <field name="'|| 'NATIONAL_INS_NO'  ||'"><value>' ||    l_p11d_fields.NATIONAL_INS_NO     || '</value></field> ' ||
1763             ' <field name="'||'NI_1'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,1,1) || '</value></field> ' ||
1764             ' <field name="'||'NI_2'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,2,1) || '</value></field> ' ||
1765             ' <field name="'||'NI_3'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,3,1) || '</value></field> ' ||
1766             ' <field name="'||'NI_4'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,4,1) || '</value></field> ' ||
1767             ' <field name="'||'NI_5'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,5,1) || '</value></field> ' ||
1768             ' <field name="'||'NI_6'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,6,1) || '</value></field> ' ||
1769             ' <field name="'||'NI_7'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,7,1) || '</value></field> ' ||
1770             ' <field name="'||'NI_8'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,8,1) || '</value></field> ' ||
1771             ' <field name="'||'NI_9'              ||'"><value>' ||substr(l_p11d_fields.NATIONAL_INS_NO ,9)   || '</value></field> ' ||
1772             ' <field name="'||'GENDER'            ||'"><value>' ||    l_p11d_fields.gender                   || '</value></field> ' ||
1773             ' <field name="'||'DB1'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,1,1)    || '</value></field> ' ||
1774             ' <field name="'||'DB2'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,2,1)    || '</value></field> ' ||
1775             ' <field name="'||'DB3'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,3,1)    || '</value></field> ' ||
1776             ' <field name="'||'DB4'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,4,1)    || '</value></field> ' ||
1777             ' <field name="'||'DB5'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,5,1)    || '</value></field> ' ||
1778             ' <field name="'||'DB6'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,6,1)    || '</value></field> ' ||
1779             ' <field name="'||'DB7'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,7,1)    || '</value></field> ' ||
1780             ' <field name="'||'DB8'               ||'"><value>' ||substr(l_p11d_fields.date_of_birth,8,1)    || '</value></field> ' ||
1781             ' <field name="'|| 'EMPLOYERS_REF_NO' ||'"><value>' ||    l_p11d_fields.EMPLOYERS_REF_NO    || '</value></field> ' ||
1782             ' <field name="'|| 'EMPLOYERS_NAME'   ||'"><value>' ||    l_p11d_fields.EMPLOYERS_NAME      || '</value></field> ' ||
1783             ' <field name="'|| 'A_DESC'           ||'"><value>' ||    l_p11d_fields.A_DESC              || '</value></field> ' ||
1784             ' <field name="'|| 'A_COST'           ||'"><value>' ||    l_p11d_fields.A_COST              || '</value></field> ' ||
1785             ' <field name="'|| 'A_AMG'            ||'"><value>' ||    l_p11d_fields.A_AMG               || '</value></field> ' ||
1786             ' <field name="'|| 'A_CE'             ||'"><value>' ||    l_p11d_fields.A_CE                || '</value></field> ' ||
1787             ' <field name="'|| 'B_DESC'           ||'"><value>' ||    l_p11d_fields.B_DESC              || '</value></field> ' ||
1788             ' <field name="'|| 'B_CE'             ||'"><value>' ||    l_p11d_fields.B_CE                || '</value></field> ' ||
1789             ' <field name="'|| 'B_TNP'            ||'"><value>' ||    l_p11d_fields.B_TNP               || '</value></field> ' ||
1790             ' <field name="'|| 'C_COST'           ||'"><value>' ||    l_p11d_fields.C_COST              || '</value></field> ' ||
1791             ' <field name="'|| 'C_AMG'            ||'"><value>' ||    l_p11d_fields.C_AMG               || '</value></field> ' ||
1792             ' <field name="'|| 'C_CE'             ||'"><value>' ||    l_p11d_fields.C_CE                || '</value></field> ' ||
1793             ' <field name="'|| 'D_CE'             ||'"><value>' ||    l_p11d_fields.D_CE                || '</value></field> ' ||
1794             ' <field name="'|| 'E_CE'             ||'"><value>' ||    l_p11d_fields.E_CE                || '</value></field> ' ||
1795             ' <field name="'|| 'F1_MAKE'          ||'"><value>' ||    l_p11d_fields.F1_MAKE             || '</value></field> ' ||
1796             ' <field name="'|| 'F1_DREG1'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,0,2)  || '</value></field> ' ||
1797             ' <field name="'|| 'F1_DREG2'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,4,2)  || '</value></field> ' ||
1798             ' <field name="'|| 'F1_DREG3'         ||'"><value>' ||   substr(l_p11d_fields.F1_DREG,7)    || '</value></field> ' ||
1799             ' <field name="'|| 'F1_EFIG'          ||'"><value>' ||    l_p11d_fields.F1_EFIG             || '</value></field> ' ||
1800             ' <field name="'|| 'F1_NFIG'          ||'"><value>' ||    l_p11d_fields.F1_NFIG             || '</value></field> ' ||
1801             ' <field name="'|| 'F1_ESIZE'         ||'"><value>' ||    l_p11d_fields.F1_ESIZE            || '</value></field> ' ||
1802             ' <field name="'|| 'F1_FUEL'          ||'"><value>' ||    l_fuel_new                        || '</value></field> ' ||
1803             ' <field name="'|| 'F1_START1'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,0,2) || '</value></field> ' ||
1804             ' <field name="'|| 'F1_START2'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,4,2) || '</value></field> ' ||
1805             ' <field name="'|| 'F1_START3'        ||'"><value>' ||   substr(l_p11d_fields.F1_START,7)   || '</value></field> ' ||
1806             ' <field name="'|| 'F1_END1'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,0,2)   || '</value></field> ' ||
1807             ' <field name="'|| 'F1_END2'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,4,2)   || '</value></field> ' ||
1808             ' <field name="'|| 'F1_END3'          ||'"><value>' ||   substr(l_p11d_fields.F1_END,7)     || '</value></field> ' ||
1809             ' <field name="'|| 'F1_LPRICE'        ||'"><value>' ||    l_p11d_fields.F1_LPRICE           || '</value></field> ' ||
1810             ' <field name="'|| 'F1_OPRICE'        ||'"><value>' ||    l_p11d_fields.F1_OPRICE           || '</value></field> ' ||
1811        --   ' <field name="'|| 'F1_APRICE'        ||'"><value>' ||    l_p11d_fields.F1_APRICE           || '</value></field> ' ||
1812             ' <field name="'|| 'F1_COST'          ||'"><value>' ||    l_p11d_fields.F1_COST             || '</value></field> ' ||
1813             ' <field name="'|| 'F1_AMG'           ||'"><value>' ||    l_p11d_fields.F1_AMG              || '</value></field> ' ||
1814             ' <field name="'|| 'F1_DATE_FREE1'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,0,2)  || '</value></field> ' ||
1815             ' <field name="'|| 'F1_DATE_FREE2'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,4,2)  || '</value></field> ' ||
1816             ' <field name="'|| 'F1_DATE_FREE3'    ||'"><value>' ||    substr(l_p11d_fields.F1_DATE_FREE,7)    || '</value></field> ' ||
1817             ' <field name="'|| 'F1_REIN_YR'       ||'"><value>' ||    l_p11d_fields.F1_REIN_YR          || '</value></field> ' ||
1818             ' <field name="'|| 'F1_CC'            ||'"><value>' ||    l_p11d_fields.F1_CC               || '</value></field> ' ||
1819             ' <field name="'|| 'F1_FCC'           ||'"><value>' ||    l_p11d_fields.F1_FCC              || '</value></field> ' ||
1820             ' <field name="'|| 'F2_MAKE'          ||'"><value>' ||    l_p11d_fields.F2_MAKE             || '</value></field> ' ||
1821             ' <field name="'|| 'F2_DREG1'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,0,2)  || '</value></field> ' ||
1822             ' <field name="'|| 'F2_DREG2'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,4,2)  || '</value></field> ' ||
1823             ' <field name="'|| 'F2_DREG3'         ||'"><value>' ||   substr(l_p11d_fields.F2_DREG,7)    || '</value></field> ' ||
1824             ' <field name="'|| 'F2_EFIG'          ||'"><value>' ||    l_p11d_fields.F2_EFIG             || '</value></field> ' ||
1825             ' <field name="'|| 'F2_NFIG'          ||'"><value>' ||    l_p11d_fields.F2_NFIG             || '</value></field> ' ||
1826             ' <field name="'|| 'F2_ESIZE'         ||'"><value>' ||    l_p11d_fields.F2_ESIZE            || '</value></field> ' ||
1827             ' <field name="'|| 'F2_FUEL'          ||'"><value>' ||    l_fuel1_new			            || '</value></field> ' ||
1828             ' <field name="'|| 'F2_START1'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,0,2) || '</value></field> ' ||
1829             ' <field name="'|| 'F2_START2'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,4,2) || '</value></field> ' ||
1830             ' <field name="'|| 'F2_START3'        ||'"><value>' ||   substr(l_p11d_fields.F2_START,7)   || '</value></field> ' ||
1831             ' <field name="'|| 'F2_END1'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,0,2)   || '</value></field> ' ||
1832             ' <field name="'|| 'F2_END2'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,4,2)   || '</value></field> ' ||
1833             ' <field name="'|| 'F2_END3'          ||'"><value>' ||   substr(l_p11d_fields.F2_END,7)     || '</value></field> ' ||
1834             ' <field name="'|| 'F2_LPRICE'        ||'"><value>' ||    l_p11d_fields.F2_LPRICE           || '</value></field> ' ||
1835             ' <field name="'|| 'F2_OPRICE'        ||'"><value>' ||    l_p11d_fields.F2_OPRICE           || '</value></field> ' ||
1836             ' <field name="'|| 'F2_APRICE'        ||'"><value>' ||    l_p11d_fields.F2_APRICE           || '</value></field> ' ||
1837             ' <field name="'|| 'F2_COST'          ||'"><value>' ||    l_p11d_fields.F2_COST             || '</value></field> ' ||
1838             ' <field name="'|| 'F2_AMG'           ||'"><value>' ||    l_p11d_fields.F2_AMG              || '</value></field> ' ||
1839             ' <field name="'|| 'F2_DATE_FREE1'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,0,2) || '</value></field> ' ||
1840             ' <field name="'|| 'F2_DATE_FREE2'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,4,2) || '</value></field> ' ||
1841             ' <field name="'|| 'F2_DATE_FREE3'    ||'"><value>' ||    substr(l_p11d_fields.F2_DATE_FREE,7)   || '</value></field> ' ||
1842             ' <field name="'|| 'F2_REIN_YR'       ||'"><value>' ||    l_p11d_fields.F2_REIN_YR          || '</value></field> ' ||
1843             ' <field name="'|| 'F2_CC'            ||'"><value>' ||    l_p11d_fields.F2_CC               || '</value></field> ' ||
1844             ' <field name="'|| 'F2_FCC'           ||'"><value>' ||    l_p11d_fields.F2_FCC              || '</value></field> ' ||
1845             ' <field name="'|| 'F_TCCE'           ||'"><value>' ||    l_p11d_fields.F_TCCE              || '</value></field> ' ||
1846             ' <field name="'|| 'F_TFCE'           ||'"><value>' ||    l_p11d_fields.F_TFCE              || '</value></field> ' ||
1847             ' <field name="'|| 'G_CE'             ||'"><value>' ||    l_p11d_fields.G_CE                || '</value></field> ' ||
1848             ' <field name="'|| 'G_CEF'            ||'"><value>' ||    l_p11d_fields.G_CEF               || '</value></field> ' ||
1849             ' <field name="'|| 'H1_NJB'           ||'"><value>' ||    l_p11d_fields.H1_NJB              || '</value></field> ' ||
1850             ' <field name="'|| 'H1_AYB'           ||'"><value>' ||    l_p11d_fields.H1_AYB              || '</value></field> ' ||
1851             ' <field name="'|| 'H1_AYE'           ||'"><value>' ||    l_p11d_fields.H1_AYE              || '</value></field> ' ||
1852             ' <field name="'|| 'H1_MAO'           ||'"><value>' ||    l_p11d_fields.H1_MAO              || '</value></field> ' ||
1853             ' <field name="'|| 'H1_IP'            ||'"><value>' ||    l_p11d_fields.H1_IP               || '</value></field> ' ||
1854             ' <field name="'|| 'H1_DLM1'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,0,2)  || '</value></field> ' ||
1855             ' <field name="'|| 'H1_DLM2'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,4,2)  || '</value></field> ' ||
1856             ' <field name="'|| 'H1_DLM3'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLM,7)    || '</value></field> ' ||
1857             ' <field name="'|| 'H1_DLD1'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,0,2)  || '</value></field> ' ||
1858             ' <field name="'|| 'H1_DLD2'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,4,2)  || '</value></field> ' ||
1859             ' <field name="'|| 'H1_DLD3'          ||'"><value>' ||    substr(l_p11d_fields.H1_DLD,7)    || '</value></field> ' ||
1860             ' <field name="'|| 'H1_CE'            ||'"><value>' ||    nvl(l_p11d_fields.H1_CE,'0.00')   || '</value></field> ' ||
1861             ' <field name="'|| 'H2_NJB'           ||'"><value>' ||    l_p11d_fields.H2_NJB              || '</value></field> ' ||
1862             ' <field name="'|| 'H2_AYB'           ||'"><value>' ||    l_p11d_fields.H2_AYB              || '</value></field> ' ||
1863             ' <field name="'|| 'H2_AYE'           ||'"><value>' ||    l_p11d_fields.H2_AYE              || '</value></field> ' ||
1864             ' <field name="'|| 'H2_MAO'           ||'"><value>' ||    l_p11d_fields.H2_MAO              || '</value></field> ' ||
1865             ' <field name="'|| 'H2_IP'            ||'"><value>' ||    l_p11d_fields.H2_IP               || '</value></field> ' ||
1866             ' <field name="'|| 'H2_DLM1'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,0,2)  || '</value></field> ' ||
1867             ' <field name="'|| 'H2_DLM2'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,4,2)  || '</value></field> ' ||
1868             ' <field name="'|| 'H2_DLM3'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLM,7)    || '</value></field> ' ||
1869             ' <field name="'|| 'H2_DLD1'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,0,2)  || '</value></field> ' ||
1870             ' <field name="'|| 'H2_DLD2'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,4,2)  || '</value></field> ' ||
1871             ' <field name="'|| 'H2_DLD3'          ||'"><value>' ||    substr(l_p11d_fields.H2_DLD,7)    || '</value></field> ' ||
1872             ' <field name="'|| 'H2_CE'            ||'"><value>' ||    nvl(l_p11d_fields.H2_CE,'0.00')   || '</value></field> ' ||
1873             ' <field name="'|| 'I_COST'           ||'"><value>' ||    l_p11d_fields.I_COST              || '</value></field> ' ||
1874             ' <field name="'|| 'I_AMG'            ||'"><value>' ||    l_p11d_fields.I_AMG               || '</value></field> ' ||
1875             ' <field name="'|| 'I_CE'             ||'"><value>' ||    l_p11d_fields.I_CE                || '</value></field> ' ||
1876             ' <field name="'|| 'J_CE'             ||'"><value>' ||    l_p11d_fields.J_CE                || '</value></field> ' ||
1877             ' <field name="'|| 'K_COST'           ||'"><value>' ||    l_p11d_fields.K_COST              || '</value></field> ' ||
1878             ' <field name="'|| 'K_AMG'            ||'"><value>' ||    l_p11d_fields.K_AMG               || '</value></field> ' ||
1879             ' <field name="'|| 'K_CE'             ||'"><value>' ||    l_p11d_fields.K_CE                || '</value></field> ' ||
1880             ' <field name="'|| 'L_DESC'           ||'"><value>' ||    l_p11d_fields.L_DESC              || '</value></field> ' ||
1881             ' <field name="'|| 'L_COST'           ||'"><value>' ||    l_p11d_fields.L_COST              || '</value></field> ' ||
1882             ' <field name="'|| 'L_AMG'            ||'"><value>' ||    l_p11d_fields.L_AMG               || '</value></field> ' ||
1883             ' <field name="'|| 'L_CE'             ||'"><value>' ||    l_p11d_fields.L_CE                || '</value></field> ' ||
1884             ' <field name="'|| 'M_COST'           ||'"><value>' ||    l_p11d_fields.N_COST              || '</value></field> ' ||
1885             ' <field name="'|| 'M_AMG'            ||'"><value>' ||    l_p11d_fields.N_AMG               || '</value></field> ' ||
1886             ' <field name="'|| 'M_CE'             ||'"><value>' ||    l_p11d_fields.N_CE                || '</value></field> ' ||
1887             ' <field name="'|| 'M_DESC'           ||'"><value>' ||    replace(l_p11d_fields.N_DESC,'&','&')  || '</value></field> ' ||
1888             ' <field name="'|| 'MA_COST'          ||'"><value>' ||    l_p11d_fields.NA_COST             || '</value></field> ' ||
1889             ' <field name="'|| 'MA_AMG'           ||'"><value>' ||    l_p11d_fields.NA_AMG              || '</value></field> ' ||
1890             ' <field name="'|| 'MA_CE'            ||'"><value>' ||    l_p11d_fields.NA_CE               || '</value></field> ' ||
1891             ' <field name="'|| 'MA_DESC'          ||'"><value>' ||    l_p11d_fields.NA_DESC             || '</value></field> ' ||
1892             ' <field name="'|| 'M_TAXPAID'        ||'"><value>' ||    l_p11d_fields.N_TAXPAID           || '</value></field> ' ||
1893             ' <field name="'|| 'N1_COST'          ||'"><value>' ||    l_p11d_fields.O1_COST             || '</value></field> ' ||
1894             ' <field name="'|| 'N1_AMG'           ||'"><value>' ||    l_p11d_fields.O1_AMG              || '</value></field> ' ||
1895             ' <field name="'|| 'N1_CE'            ||'"><value>' ||    l_p11d_fields.O1_CE               || '</value></field> ' ||
1896             ' <field name="'|| 'N2_COST'          ||'"><value>' ||    l_p11d_fields.O2_COST             || '</value></field> ' ||
1897             ' <field name="'|| 'N2_AMG'           ||'"><value>' ||    l_p11d_fields.O2_AMG              || '</value></field> ' ||
1898             ' <field name="'|| 'N2_CE'            ||'"><value>' ||    l_p11d_fields.O2_CE               || '</value></field> ' ||
1899             ' <field name="'|| 'N3_COST'          ||'"><value>' ||    l_p11d_fields.O3_COST             || '</value></field> ' ||
1900             ' <field name="'|| 'N3_AMG'           ||'"><value>' ||    l_p11d_fields.O3_AMG              || '</value></field> ' ||
1901             ' <field name="'|| 'N3_CE'            ||'"><value>' ||    l_p11d_fields.O3_CE               || '</value></field> ' ||
1902             ' <field name="'|| 'N4_COST'          ||'"><value>' ||    l_p11d_fields.O4_COST             || '</value></field> ' ||
1903             ' <field name="'|| 'N4_AMG'           ||'"><value>' ||    l_p11d_fields.O4_AMG              || '</value></field> ' ||
1904             ' <field name="'|| 'N4_CE'            ||'"><value>' ||    l_p11d_fields.O4_CE               || '</value></field> ' ||
1905             ' <field name="'|| 'N5_COST'          ||'"><value>' ||    l_p11d_fields.O5_COST             || '</value></field> ' ||
1906             ' <field name="'|| 'N5_AMG'           ||'"><value>' ||    l_p11d_fields.O5_AMG              || '</value></field> ' ||
1907             ' <field name="'|| 'N5_CE'            ||'"><value>' ||    l_p11d_fields.O5_CE               || '</value></field> ' ||
1908             ' <field name="'|| 'N6_COST'          ||'"><value>' ||    l_p11d_fields.O6_COST             || '</value></field> ' ||
1909             ' <field name="'|| 'N6_AMG'           ||'"><value>' ||    l_p11d_fields.O6_AMG              || '</value></field> ' ||
1910             ' <field name="'|| 'N6_CE'            ||'"><value>' ||    l_p11d_fields.O6_CE               || '</value></field> ' ||
1911             ' <field name="'|| 'N6_DESC'          ||'"><value>' ||    l_p11d_fields.O6_DESC             || '</value></field> ' ||
1912             ' <field name="'|| 'N_TOI'            ||'"><value>' ||    l_p11d_fields.O_TOI               || '</value></field> ' ||
1913             '</fields>  </xfdf>';
1914 
1915 
1916         end if;
1917 
1918         dbms_lob.createtemporary(l_xfdf_clob,false,DBMS_LOB.CALL);
1919         dbms_lob.open(l_xfdf_clob,dbms_lob.lob_readwrite);
1920 
1921 --Start of the fix for the EAP bug 9383416
1922 if (validate_display_output(p_assignment_action_id) = 1) then
1923         dbms_lob.writeAppend( l_xfdf_clob, length(l_xfdf_string ), l_xfdf_string  );
1924     end if;
1925 --End of the fix for the EAP bug 9383416
1926 
1927         DBMS_LOB.CREATETEMPORARY(l_xfdf_blob,true);
1928         -- clob_to_blob(l_xfdf_string,l_xfdf_blob);
1929         clob_to_blob(l_xfdf_clob,l_xfdf_blob);
1930         -- insert into temp values          (l_xfdf_clob);
1931         dbms_lob.close(l_xfdf_clob);
1932         dbms_lob.freetemporary(l_xfdf_clob);
1933         hr_utility.trace('P11D.......................');
1934         return l_xfdf_blob;
1935    exception
1936    when OTHERS then
1937           HR_UTILITY.TRACE('sqleerm ' || sqlerrm);
1938           HR_UTILITY.RAISE_ERROR;
1939    end;
1940 --
1941 /*------------------------------------------------------------------------
1942 Below validate_display_output function returns 1 if ANY of the following
1943 conditions is true:
1944 1. Emp has INT FREE AND LOW INT LOANS Max Outstanding Value > 5000
1945 2. Emp has MILEAGE ALLOWANCE AND PPAYMENT Cash Equivalant Value > 0
1946 3. Emp has any other element.
1947 ------------------------------------------------------------------------*/
1948 
1949 --Start of the fix for the EAP bug 9383416
1950   function validate_display_output(p_assignment_action_id Number) return number
1951   is
1952 
1953   cursor get_loan_amount (c_asg_act_id in number, c_emp_ref in varchar2) is
1954      select /*+ ORDERED use_nl(paa,pai,pai_person)
1955 		           use_index(pai_person,pay_action_information_n2)
1956 			   use_index(pai,pay_action_information_n2) */
1957     		sum(to_number(nvl(pai.action_information7,0)))
1958 	 from   pay_assignment_actions  paa,
1959        		pay_action_information  pai,
1960        		pay_action_information  pai_person
1961 	 where  pai.action_context_id = paa.assignment_action_id
1962 	 and    pai.action_context_type = 'AAP'
1963 	 and    pai.action_information_category = 'INT FREE AND LOW INT LOANS'
1964 	 and    pai_person.action_context_id = paa.assignment_action_id
1965 	 and    paa.assignment_action_id = c_asg_act_id
1966 	 and    pai_person.action_information_category = 'GB EMPLOYEE DETAILS'
1967 	 and    upper(pai_person.action_information13) = upper(c_emp_ref)
1968 	 and    pai_person.action_context_type = 'AAP';
1969 
1970     cursor get_ben_value (c_asg_act_id in number, c_emp_ref in varchar2) is
1971        select /*+ ORDERED use_nl(paa,pai,pai_a,pai_person)
1972                     use_index(pai_person,pay_action_information_n2)
1973                     use_index(pai,pay_action_information_n2)
1974                     use_index(pai_a,pay_action_information_n2)*/
1975               pai_a.action_information12
1976        from   pay_assignment_actions  paa,
1977               pay_action_information  pai,
1978               pay_action_information  pai_a,
1979               pay_action_information  pai_person
1980        where  paa.assignment_action_id = c_asg_act_id
1981        and    pai.action_context_id = paa.assignment_action_id
1982        and    pai.action_context_type = 'AAP'
1983        and    pai.action_information_category = pai.action_information_category
1984        and    pai_person.action_context_id = paa.assignment_action_id
1985        and    pai_person.action_information_category = 'GB EMPLOYEE DETAILS'
1986        and    pai_person.action_context_type = 'AAP'
1987        and    upper(pai_person.action_information13) = upper(c_emp_ref)
1988        and    pai_a.action_context_id = paa.assignment_action_id
1989        and    pai_a.action_context_type = 'AAP'
1990        and    pai_a.action_information_category = 'GB P11D ASSIGNMENT RESULTA'
1991        and    pai.action_information_category = 'MILEAGE ALLOWANCE AND PPAYMENT';
1992 
1993     cursor get_threshold_val (c_rep_run in varchar2) is
1994         select to_number(global_value)
1995           from ff_globals_f
1996          where global_name = 'P11D_LOW_INT_LOAN_THRESHOLD'
1997            and to_date('05-04-' || c_rep_run,'DD-MM-YYYY') between effective_start_date and effective_end_date;
1998 
1999     cursor get_emp_ref is
2000         select action_information6
2001           from pay_action_information pai_comp
2002          where pai_comp.action_context_id = p_assignment_action_id
2003            and pai_comp.action_information_category = 'EMEA PAYROLL INFO';
2004 
2005     cursor get_pactid is
2006         select payroll_action_id
2007           from pay_assignment_actions
2008          where assignment_action_id = p_assignment_action_id;
2009 
2010     cursor get_other_ben_exists (c_asg_act_id in number) is
2011         select 1
2012           from pay_action_information pai
2013          where pai.action_context_id = c_asg_act_id
2014            and pai.action_context_type = 'AAP'
2015            and pai.action_information_category in ('ASSETS TRANSFERRED',
2016                             'PAYMENTS MADE FOR EMP',
2017                             'VOUCHERS OR CREDIT CARDS',
2018                             'LIVING ACCOMMODATION',
2019                             'CAR AND CAR FUEL 2003_04',
2020                             'VANS 2007',
2021                             'PVT MED TREATMENT OR INSURANCE',
2022                             'RELOCATION EXPENSES',
2023                             'SERVICES SUPPLIED',
2024                             'ASSETS AT EMP DISPOSAL',
2025                             'OTHER ITEMS',
2026                             'OTHER ITEMS NON 1A',
2027                             'EXPENSES PAYMENTS',
2028                             'MARORS');
2029 
2030 /***************************************************************************************************************
2031 ** Added the below cursor for the bug fix 9676798.
2032 ** Below cursor returns other asg_action_id's for the same payroll_action_id that have p11d benefits attached.
2033 ** These asg_action_id's will be then checked for their validity as if it is the current asg_action_id.
2034 **************************************************************************************************************/
2035 --Modified the below cursor for the bug 9799823
2036     cursor get_other_asg_action_exists (c_asg_act_id number,
2037                                         c_pact_id number,
2038                                         c_person_id number)is
2039         select paa.assignment_action_id
2040           from pay_action_information pai,
2041                pay_assignment_actions paa,
2042                per_all_assignments_f paaf
2043          where paa.payroll_action_id = c_pact_id
2044            and pai.action_context_id = paa.assignment_action_id
2045            and paa.assignment_action_id <> c_asg_act_id
2046            and paaf.person_id = c_person_id
2047            and paaf.assignment_id = paa.assignment_id
2048            and pai.action_context_type = 'AAP'
2049            and pai.action_information_category in ('ASSETS TRANSFERRED',
2050                             'PAYMENTS MADE FOR EMP',
2051                             'VOUCHERS OR CREDIT CARDS',
2052                             'LIVING ACCOMMODATION',
2053                             'CAR AND CAR FUEL 2003_04',
2054                             'VANS 2007',
2055                             'PVT MED TREATMENT OR INSURANCE',
2056                             'RELOCATION EXPENSES',
2057                             'SERVICES SUPPLIED',
2058                             'ASSETS AT EMP DISPOSAL',
2059                             'OTHER ITEMS',
2060                             'OTHER ITEMS NON 1A',
2061                             'EXPENSES PAYMENTS',
2062                             'MARORS',
2063                             'INT FREE AND LOW INT LOANS',
2064                             'MILEAGE ALLOWANCE AND PPAYMENT');
2065 
2066          l_h_sum_max_amt_outstanding Number;
2067          l_loan_threshold Number;
2068          l_pactid number;
2069          l_rep_run varchar2(10);
2070          l_emp_ref varchar2(150);  --fixed for the bug 9450379
2071          l_ben_value number;
2072          l_other_ben_exists number;
2073          l_other_asg_action_id number;
2074          l_asgactid number;
2075          l_h_sum_max_amt_out_other Number;
2076          l_ben_value_other number;
2077          l_other_ben_exists_other number;
2078          l_return number;
2079          l_person_id number;            --Added for the bug 9799823
2080 
2081         begin
2082             hr_utility.trace('Entering validate_display_output function');
2083             hr_utility.trace('p_assignment_action_id: '||p_assignment_action_id);
2084             l_asgactid:= p_assignment_action_id;
2085         open get_pactid ;
2086             fetch get_pactid into l_pactid;
2087             hr_utility.trace('l_pactid: '||l_pactid);
2088         close get_pactid ;
2089 
2090         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2091          p_payroll_action_id           => l_pactid,
2092          p_token_name                  => 'Rep_Run',
2093          p_token_value                 => l_rep_run);
2094 
2095         open get_emp_ref;
2096             fetch get_emp_ref into l_emp_ref;
2097             hr_utility.trace('l_emp_ref: '||l_emp_ref);
2098         close get_emp_ref;
2099 
2100         open get_loan_amount(l_asgactid, l_emp_ref);
2101             fetch get_loan_amount into l_h_sum_max_amt_outstanding;
2102             hr_utility.trace('l_h_sum_max_amt_outstanding: '||l_h_sum_max_amt_outstanding);
2103         close get_loan_amount;
2104 
2105         open get_threshold_val(l_rep_run);
2106             fetch get_threshold_val into l_loan_threshold;
2107             hr_utility.trace('l_loan_threshold: '||l_loan_threshold);
2108         close get_threshold_val;
2109 
2110         open get_ben_value(l_asgactid, l_emp_ref);
2111             fetch get_ben_value into l_ben_value;
2112             hr_utility.trace('l_ben_value: '||l_ben_value);
2113         close get_ben_value;
2114 
2115         open get_other_ben_exists (l_asgactid);
2116             fetch get_other_ben_exists into l_other_ben_exists;
2117             hr_utility.trace('l_other_ben_exists: '||l_other_ben_exists);
2118         close get_other_ben_exists;
2119 
2120      if ((l_h_sum_max_amt_outstanding > l_loan_threshold)
2121          or (l_ben_value > 0 ) or (l_other_ben_exists = 1)) then
2122             hr_utility.trace('returning 1');
2123            return 1;
2124      else
2125 
2126         l_person_id := get_person_id (l_asgactid);     --Added for the bug 9799823
2127 
2128 --Added the below code for the bug fix 9676798
2129 	open get_other_asg_action_exists (l_asgactid, l_pactid, l_person_id);     --Modified for the bug 9799823
2130         loop
2131         fetch get_other_asg_action_exists into l_other_asg_action_id;
2132           if l_other_asg_action_id is not null then
2133              hr_utility.trace('l_other_asg_action_id: '||l_other_asg_action_id);
2134 
2135              open get_loan_amount(l_other_asg_action_id, l_emp_ref);
2136              fetch get_loan_amount into l_h_sum_max_amt_out_other;
2137                    hr_utility.trace('l_h_sum_max_amt_out_other: '||l_h_sum_max_amt_out_other);
2138              close get_loan_amount;
2139 
2140              open get_ben_value(l_other_asg_action_id, l_emp_ref);
2141              fetch get_ben_value into l_ben_value_other;
2142                    hr_utility.trace('l_ben_value_other: '||l_ben_value_other);
2143              close get_ben_value;
2144 
2145              open get_other_ben_exists (l_other_asg_action_id);
2146              fetch get_other_ben_exists into l_other_ben_exists_other;
2147                    hr_utility.trace('l_other_ben_exists_other: '||l_other_ben_exists_other);
2148              close get_other_ben_exists;
2149 
2150              if ((l_h_sum_max_amt_out_other > l_loan_threshold)
2151                  or (l_ben_value_other > 0 ) or (l_other_ben_exists_other = 1)) then
2152                    hr_utility.trace('Other asg returning 1');
2153                          l_return := 1;
2154              end if;
2155              else
2156                    hr_utility.trace('Inside else returning 0');
2157                          l_return := 0;
2158              end if;
2159         exit when (get_other_asg_action_exists%NOTFOUND) or (l_return = 1);
2160         end loop;
2161             hr_utility.trace('Closing the cursor');
2162         close get_other_asg_action_exists;
2163      end if;
2164      if (l_return = 1) then
2165          return 1;
2166      else
2167          return 0;
2168      end if;
2169 --Modification for the bug fix 9676798 ends here
2170 
2171          hr_utility.trace('Leaving validate_display_output function');
2172    end validate_display_output;
2173 --End of the fix for the EAP bug 9383416
2174 
2175    function get_p11d_year return VARCHAR2
2176    is
2177         l_year              VARCHAR2(4);
2178         l_effective_date    DATE;
2179         cursor csr_year
2180         is
2181         --select to_char(ses.effective_date, 'YYYY'), ses.effective_date
2182         select to_char(sysdate,'YYYY'), ses.effective_date
2183         from   fnd_sessions ses
2184         where  ses.session_id = userenv('sessionid');
2185    begin
2186         open csr_year;
2187         fetch csr_year into l_year, l_effective_date;
2188         close csr_year;
2189         /*
2190         if l_effective_date > to_date('06-04-'|| l_year, 'DD-MM-YYYY')
2191         then
2192             null;
2193         else
2194             l_year := to_char(to_number(l_year) - 1);
2195         end if;
2196         */
2197         return l_year;
2198    end;
2199 
2200    function get_ben_start_date return date
2201    is
2202         l_year              VARCHAR2(4);
2203    begin
2204         l_year := get_p11d_year;
2205         return to_date('06-04-'|| to_char(to_number(l_year) - 1), 'DD-MM-YYYY');
2206    end;
2207 
2208    function get_ben_end_date return DATE
2209    is
2210         l_year              VARCHAR2(4);
2211    begin
2212         l_year := get_p11d_year;
2213         return to_date('05-04-'|| l_year, 'DD-MM-YYYY');
2214    end;
2215 
2216    procedure update_leg_process_status(errbuf              out nocopy VARCHAR2,
2217                                        retcode             out nocopy NUMBER,
2218                                        p_payroll_action_id in  Number,
2219                                        p_new_status        in  Varchar2)
2220    is
2221         l_param_string VARCHAR2(2000);
2222         l_param_string_before_val VARCHAR2(2000);
2223         l_param_string_after_val VARCHAR2(2000);
2224         Old_Archive exception;
2225    begin
2226         retcode := 0;
2227         select legislative_parameters
2228         into   l_param_string
2229         from   pay_payroll_actions
2230         where  payroll_action_id = p_payroll_action_id;
2231         -- check if the archiver has Status in it
2232         -- if not then it is old archiver 2002-2003 archiver.
2233         -- the one prior to self service being supplied.
2234         -- so this cannot be run on them, hence we should error it
2235         if instr(l_param_string,'Status=') = 0
2236         then
2237             raise Old_Archive;
2238         end if;
2239         l_param_string_before_val := substr(l_param_string,1,instr(l_param_string, ' Status=')-2) ;
2240         l_param_string_after_val  := substr(l_param_string,instr(l_param_string, 'Status=') + length('Status=')+1) ;
2241         update pay_payroll_actions
2242         set  legislative_parameters =l_param_string_before_val ||  p_new_status ||
2243             ' Status=' || p_new_status || l_param_string_after_val
2244         where payroll_action_id = p_payroll_action_id; --8875;
2245    exception
2246    when Old_Archive then
2247         retcode := 1;
2248         hr_utility.set_message(800, 'HR_78078_P11D_STAT_INCOR_ARCH');
2249         errbuf := hr_utility.get_message;
2250         hr_utility.raise_error;
2251    when OTHERS then
2252         retcode := 1;
2253         hr_utility.set_message(800, 'HR_78077_P11D_STATUS_CHG_ERR');
2254         hr_utility.set_message_token(800, 'ERRORMSG', sqlerrm);
2255         errbuf := hr_utility.get_message;
2256         hr_utility.raise_error;
2257    end;
2258 
2259    function get_lookup_meaning(p_lookup_type varchar2,
2260                                p_lookup_code varchar2,
2261                                p_effective_date date) return varchar2
2262    is
2263         l_meaning varchar2(100);
2264         /*Bug No. 3237648*/
2265         /*Fetching from hr_lookups instead of fnd_lookup_values*/
2266         cursor csr_meaning is
2267         select meaning
2268         from   hr_lookups hlu
2269         where  hlu.lookup_type = p_lookup_type
2270         and    hlu.lookup_code = p_lookup_code
2271         and    hlu.enabled_flag='Y'
2272         and    p_effective_date between
2273                    nvl( hlu.START_DATE_ACTIVE,p_effective_date)
2274                and nvl( hlu.END_DATE_ACTIVE , p_effective_date);
2275    begin
2276         open csr_meaning;
2277         fetch csr_meaning into l_meaning;
2278         close csr_meaning;
2279         return l_meaning;
2280    end;
2281 
2282    function fetch_arch_param_details(p_payroll_action_id in  Number) return varchar2
2283    is
2284         l_benefit_end_date     VARCHAR2(20);
2285         l_benefit_start_date   VARCHAR2(20);
2286         l_payroll_id           NUMBER;
2287         l_payroll              varchar2(100);
2288         l_person_id            NUMBER;
2289         l_person               varchar2(100);
2290         l_rep_run              varchar2(10);
2291         l_consolidation_set_id NUMBER;
2292         l_consolidation_set    varchar2(100);
2293         l_tax_reference        VARCHAR2(200);
2294         l_assignment_set_id    NUMBER;
2295         l_assignment_set       VARCHAR2(200);
2296         l_run_type_code        varchar2(10);
2297         l_run_type_meaning     varchar2(20);
2298         l_status_code          varchar2(10);
2299         l_status_meaning       varchar2(20);
2300         l_return_string varchar2(1000);
2301         cursor csr_payroll(p_effective_date date)
2302         is
2303         select PAYROLL_NAME
2304         from  pay_payrolls_f
2305         where PAYROLL_ID =  l_payroll_id
2306         and   p_effective_date between
2307                   nvl(effective_start_date,p_effective_date)
2308               and nvl(effective_start_date,p_effective_date);
2309 
2310         cursor csr_person(p_effective_date date)
2311         is
2312         select FULL_NAME
2313         from  per_people_f
2314         where person_ID =  l_person_id
2315         and   p_effective_date between
2316                   nvl(effective_start_date,p_effective_date)
2317               and nvl(effective_start_date,p_effective_date);
2318 
2319         cursor csr_consolidation_set
2320         is
2321         select CONSOLIDATION_SET_NAME
2322         from   PAY_CONSOLIDATION_SETS
2323         where  CONSOLIDATION_SET_ID   = l_consolidation_set_id;
2324 
2325         cursor csr_assignment_set is
2326         select ASSIGNMENT_SET_NAME
2327         from   HR_ASSIGNMENT_SETS_V
2328         where  ASSIGNMENT_SET_ID =l_assignment_set_id;
2329 
2330         l_effective_date date;
2331    begin
2332         begin
2333              select effective_date into l_effective_date
2334              from   fnd_sessions
2335              where  SESSION_ID = userenv('sessionid');
2336         exception
2337         when Others then
2338              l_effective_date := sysdate;
2339         end;
2340         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2341          p_payroll_action_id           => p_payroll_action_id,
2342          p_token_name                  => 'PAYROLL',
2343          p_token_value                 => l_payroll_id);
2344         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2345          p_payroll_action_id           => p_payroll_action_id,
2346          p_token_name                  => 'PERSON',
2347          p_token_value                 => l_person_id);
2348         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2349          p_payroll_action_id           => p_payroll_action_id,
2350          p_token_name                  => 'CONSOLIDATION_SET',
2351          p_token_value                 => l_consolidation_set_id);
2352         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2353          p_payroll_action_id           => p_payroll_action_id,
2354          p_token_name                  => 'TAX_REFERENCE',
2355          p_token_value                 => l_tax_reference);
2356         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2357          p_payroll_action_id           => p_payroll_action_id,
2358          p_token_name                  => 'ASSIGNMENT_SET_ID',
2359          p_token_value                 => l_assignment_set_id);
2360         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2361          p_payroll_action_id           => p_payroll_action_id,
2362          p_token_name                  => 'BENEFIT_START_DATE',
2363          p_token_value                 => l_benefit_start_date);
2364         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2365          p_payroll_action_id           => p_payroll_action_id,
2366          p_token_name                  => 'BENEFIT_END_DATE',
2367          p_token_value                 => l_benefit_end_date);
2368         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2369          p_payroll_action_id           => p_payroll_action_id,
2370          p_token_name                  => 'Rep_Run',
2371          p_token_value                 => l_rep_run);
2372         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2373          p_payroll_action_id           => p_payroll_action_id,
2374          p_token_name                  => 'Status',
2375          p_token_value                 => l_status_code);
2376         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2377          p_payroll_action_id           => p_payroll_action_id,
2378          p_token_name                  => 'Run_Type',
2379          p_token_value                 => l_run_type_code);
2380         l_run_type_meaning := get_lookup_meaning('GB_P11D_RUN_TYPE',
2381                                                  l_run_type_code,
2382                                                  fnd_date.canonical_to_date(l_benefit_end_date));
2383         l_status_meaning := get_lookup_meaning('GB_P11D_LEGISLATIVE_RUN_STATUS',
2384                                                l_status_code,
2385                                                fnd_date.canonical_to_date(l_benefit_end_date));
2386         l_return_string := l_rep_run || '   ' || l_run_type_meaning || '   '  ||
2387                            l_status_meaning ||  '   ' ||
2388                            fnd_date.date_to_displaydate(fnd_date.canonical_to_date(l_benefit_start_date))||
2389                            '/' ||
2390                           fnd_date.date_to_displaydate(fnd_date.canonical_to_date(l_benefit_end_date));
2391 
2392         if l_payroll_id is not null
2393         then
2394             open csr_payroll(l_effective_date);
2395             fetch csr_payroll into l_payroll;
2396             close csr_payroll;
2397             l_return_string := l_return_string || l_payroll || '/';
2398         else
2399             l_return_string := l_return_string || '/';
2400         end if;
2401 
2402         if l_person_id is not null
2403         then
2404             open csr_person(l_effective_date);
2405             fetch csr_person into l_person;
2406             close csr_person;
2407             l_return_string := l_return_string || l_person || '/' ;
2408         else
2409             l_return_string := l_return_string || '/';
2410         end if;
2411 
2412         if l_tax_reference is not null
2413         then
2414             l_return_string := l_return_string || l_tax_reference || '/' ;
2415         else
2416             l_return_string := l_return_string || '/';
2417         end if;
2418 
2419         if l_consolidation_set_id is not null
2420         then
2421             open csr_consolidation_set;
2422             fetch csr_consolidation_set into l_consolidation_set;
2423             close csr_consolidation_set;
2424             l_return_string := l_return_string || l_consolidation_set || '/' ;
2425         else
2426             l_return_string := l_return_string || '/' ;
2427         end if;
2428 
2429         if l_assignment_set_id is not null
2430         then
2431             open csr_assignment_set;
2432             fetch csr_assignment_set into l_assignment_set;
2433             close csr_assignment_set;
2434             l_return_string := l_return_string || l_assignment_set ;
2435         end if;
2436         return l_return_string;
2437    end;
2438 
2439    function fetch_leg_process_status(p_payroll_action_id in  Number) return varchar2
2440    is
2441         l_benefit_end_date   VARCHAR2(20);
2442         l_benefit_start_date VARCHAR2(20);
2443         l_status_code        varchar2(10);
2444         l_status_meaning     varchar2(20);
2445    begin
2446         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2447          p_payroll_action_id           => p_payroll_action_id,
2448          p_token_name                  => 'BENEFIT_END_DATE',
2449          p_token_value                 => l_benefit_end_date);
2450         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2451          p_payroll_action_id           => p_payroll_action_id,
2452          p_token_name                  => 'Status',
2453          p_token_value                 => l_status_code);
2454         l_status_meaning := get_lookup_meaning('GB_P11D_LEGISLATIVE_RUN_STATUS',
2455                                                l_status_code,
2456                                                fnd_date.canonical_to_date(l_benefit_end_date));
2457         return l_status_meaning;
2458    end;
2459 
2460    function fetch_leg_process_runtype(p_payroll_action_id in  Number) return varchar2
2461    is
2462         l_benefit_end_date            VARCHAR2(20);
2463         l_run_type_code               varchar2(10);
2464         l_run_type_meaning            varchar2(20);
2465    begin
2466         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2467          p_payroll_action_id           => p_payroll_action_id,
2468          p_token_name                  => 'BENEFIT_END_DATE',
2469          p_token_value                 => l_benefit_end_date);
2470         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
2471          p_payroll_action_id           => p_payroll_action_id,
2472          p_token_name                  => 'Run_Type',
2473          p_token_value                 => l_run_type_code);
2474         l_run_type_meaning := get_lookup_meaning('GB_P11D_RUN_TYPE',
2475                                                  l_run_type_code,
2476                                                  fnd_date.canonical_to_date(l_benefit_end_date));
2477         return l_run_type_meaning;
2478    end;
2479 
2480    function fetch_leg_process_notes(p_payroll_action_id in  Number) return varchar2
2481    is
2482         l_notes            VARCHAR2(200);
2483    begin
2484         -- we know notes is the last parameter hence we are using simple substr
2485         -- to fetch it's value
2486         -- if we add further params then the below would not work!
2487         select substr(legislative_parameters,
2488                instr(legislative_parameters, 'NOTES=') + (length('NOTES=')))
2489         into  l_notes
2490         from  pay_payroll_actions
2491         where payroll_action_id = p_payroll_action_id;
2492         return l_notes;
2493    exception
2494    when others then
2495         l_notes := null;
2496         return l_notes;
2497    end;
2498 
2499    function get_pactid(p_assignment_action_id NUmber) return number
2500    is
2501         cursor csr_pactid
2502         is
2503         select payroll_action_id
2504         from   pay_assignment_actions
2505         where  assignment_action_id = p_assignment_action_id;
2506         l_pactid number;
2507    begin
2508         open csr_pactid ;
2509         fetch csr_pactid into l_pactid;
2510         close csr_pactid ;
2511         return l_pactid;
2512    end;
2513 
2514    function get_person_id (p_assignment_action_id NUmber) return number
2515    is
2516         cursor csr_person_id
2517         is
2518         select action_information1
2519         from  pay_action_information pai_person
2520         where pai_person.action_context_id = p_assignment_action_id
2521         and   pai_person.action_information_category = 'ADDRESS DETAILS'
2522         and   pai_person.action_information14 = 'Employee Address';
2523         l_person_id number;
2524    begin
2525         open csr_person_id ;
2526         fetch csr_person_id into l_person_id;
2527         close csr_person_id ;
2528         return l_person_id;
2529    end;
2530 
2531    procedure get_employer_details(p_assignment_action_id Number,
2532                                   p_emp_ref_no out nocopy  varchar2,
2533                                   p_employer_name out nocopy varchar2)
2534    is
2535         cursor csr_employer_details
2536         is
2537         select action_information6,
2538                action_information7
2539         from   pay_action_information pai_comp
2540         where  pai_comp.action_context_id = p_assignment_action_id
2541         and    pai_comp.action_information_category = 'EMEA PAYROLL INFO';
2542    begin
2543         open  csr_employer_details;
2544         fetch csr_employer_details into p_emp_ref_no,
2545                                         p_employer_name;
2546         close csr_employer_details;
2547    end;
2548 
2549    --P11D 08/09
2550    -- Procedure to fetch the sur and fore names
2551    PROCEDURE get_sur_fore_names(p_assignment_action_id in NUMBER,
2552                                 p_sur_name out nocopy VARCHAR2,
2553                                 p_fore_name out nocopy VARCHAR2) IS
2554 
2555    cursor cur_sur_fore_name(c_assignment_action_id NUMBER) is
2556 
2557    select action_information6,
2558           action_information8
2559    from  pay_action_information pai_gb
2560    where pai_gb.action_context_id = c_assignment_action_id
2561    and   pai_gb.action_context_type = 'AAP'
2562    and   pai_gb.action_information_category = 'GB EMPLOYEE DETAILS';
2563 
2564    begin
2565 
2566    open cur_sur_fore_name(p_assignment_action_id);
2567    fetch  cur_sur_fore_name into p_fore_name,p_sur_name;
2568    close cur_sur_fore_name;
2569    END;
2570 
2571    procedure get_employee_details(p_assignment_action_id Number,
2572                                   p_full_name out nocopy  varchar2,
2573                                   p_national_ins_no out nocopy  varchar2,
2574                                   p_employee_number out nocopy varchar2)
2575    is
2576         cursor csr_employee_details
2577         is
2578         select action_information1,
2579                action_information4,
2580                action_information10
2581         from   pay_action_information pai_emp
2582         where  pai_emp.action_context_id = p_assignment_action_id
2583         and    pai_emp.action_information_category = 'EMPLOYEE DETAILS';
2584    begin
2585         open  csr_employee_details;
2586         fetch csr_employee_details into p_full_name,
2587                                         p_national_ins_no,
2588                                         p_employee_number;
2589         close csr_employee_details;
2590    end;
2591 
2592    function fetch_ws1_ref_cursor (p_assignment_action_id Number,
2593                                   p_record_num out nocopy NUmber) return ref_cursor_typ
2594    is
2595         -- per_gb_xfdftableType is explicitly created type
2596         -- only modification needed is it could be of type blob
2597         -- i tried thta but could not access the blob valus in java routine.
2598         -- can be sorted later
2599         l_xfdf_str_tab per_gb_xfdftableType := per_gb_xfdftableType( );
2600         l_xfdf_str varchar2(32000);
2601         l_ret_ref_cursor ref_cursor_typ;
2602         l_offset integer;
2603         l_varchar_buffer varchar2(32000);
2604         l_raw_buffer raw(32000);
2605         l_buffer_len number:= 32000;
2606         l_chunk_len number;
2607 
2608         cursor csr_context_id (p_pactid    NUMBER,
2609                                p_person_id NUMBER,
2610                                p_emp_ref   VARCHAR2,
2611                                p_emp_name  VARCHAR2)
2612         is
2613         select /*+ ORDERED use_nl(ppa, paa, pai_comp, pai_person, pai_car)
2614                            use_index(pai_comp,pay_action_information_n2)
2615                            use_index(pai_person,pay_action_information_n2)
2616                            use_index(pai_car,pay_action_information_n2) */
2617                pai_car.action_context_id
2618         from   pay_payroll_actions ppa,
2619                pay_assignment_actions paa,
2620                pay_action_information pai_comp,
2621                pay_action_information pai_person,
2622                pay_action_information pai_car
2623         where  ppa.payroll_action_id = p_pactid
2624         and    paa.payroll_action_id = ppa.payroll_action_id
2625         and    pai_comp.action_context_id = paa.assignment_action_id
2626         and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
2627         and    pai_comp.action_context_type = 'AAP'
2628         and    pai_person.action_context_id = paa.assignment_action_id
2629         and    pai_person.action_information_category = 'ADDRESS DETAILS'
2630         and    pai_person.action_context_type = 'AAP'
2631         and    pai_person.action_information14 = 'Employee Address'
2632         and    pai_person.action_information1 = to_char(p_person_id)
2633         and    pai_comp.action_information6 = p_emp_ref
2634         and    pai_comp.action_information7 = p_emp_name
2635         and    pai_car.action_context_id = paa.assignment_action_id
2636         and    pai_car.action_information_category = 'LIVING ACCOMMODATION'
2637         and    pai_car.action_context_type = 'AAP'
2638         group by pai_car.action_context_id;
2639 
2640         cursor csr_living_acco (p_context_id NUMBER)
2641         is
2642         select pai_ben.ACTION_INFORMATION5 address,
2643                decode(months_between(
2644                 fnd_date.canonical_to_date(pai_ben.action_information4)+1,
2645                 fnd_date.canonical_to_date(pai_ben.action_information3)),12,'Y','N') full_year,
2646                to_char(to_number(nvl(pai_ben.action_information6,0)),'FM999,999,990.00') rent_employer,
2647                to_char(to_number(nvl(pai_ben.action_information7,0)),'FM999,999,990.00') annual_value,
2648                to_char(to_number(nvl(pai_ben.action_information18,0)),'FM999,999,990.00')Basic_Charge_Cost,
2649                to_char(to_number(nvl(pai_ben.action_information9,0)),'FM999,999,990.00') amg,
2650                to_char(to_number(nvl(pai_ben.action_information19,0)),'FM999,999,990.00')Basic_Charge,
2651                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2652                    nvl(pai_ben.action_information8,0)),'FM999,999,990.00'),null)gross_amount,
2653                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2654                    nvl(pai_ben.action_information13,0)),'FM999,999,990.00'),null) emp_share_of_cost,
2655                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2656                    nvl(pai_ben.action_information20,0)),'FM999,999,990.00'),null) cost_of_acco,
2657                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2658                    nvl(pai_ben.action_information22,0)),'FM999,999,990.00'),null) excess_of_cost,
2659                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2660                    nvl(pai_ben.action_information14,0)),'FM999,999,990.00'),null) INTEREST_VALUE,
2661                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2662                    nvl(pai_ben.action_information21,0)),'FM999,999,990.00'),null) INTEREST_AMOUNT,
2663                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2664                    nvl(pai_ben.action_information16,0)),'FM999,999,990.00'),null) rent_employee,
2665                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2666                    nvl(pai_ben.action_information9,0)),'FM999,999,990.00'),null) RENT_IN_AMG,
2667                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2668                    nvl(pai_ben.action_information17,0)),'FM999,999,990.00'),null) ADDITIONAL_CHARGE,
2669                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2670                    nvl(pai_ben.action_information19,0)),'FM999,999,990.00'),null) BASIC_CHARGE_2,
2671                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2672                    nvl(pai_ben.action_information16,0) - nvl(pai_ben.action_information9,0)),'FM999,999,990.00'),null) ACTUAL_RENT,
2673                decode(sign(pai_ben.action_information22),1,to_char(to_number(
2674                    nvl(pai_ben.action_information19,0)+nvl(pai_ben.action_information17,0)),'FM999,999,990.00'),null) TOTAL ,
2675                decode(sign(pai_ben.action_information22),
2676                       1,decode(months_between(
2677                         fnd_date.canonical_to_date(pai_ben.action_information4)+1,
2678                         fnd_date.canonical_to_date(pai_ben.action_information3)),12,null,
2679                       to_char(to_number(nvl(pai_ben.action_information15,0)))),null) NUMBER_OF_DAYS
2680         from  pay_action_information pai_ben
2681         where pai_ben.action_information_category = 'LIVING ACCOMMODATION'
2682         and   pai_ben.action_context_id = p_context_id
2683         and   pai_ben.action_context_type = 'AAP';
2684 
2685         l_emp_ref_no varchar2(150);
2686         l_employer_name varchar2(150);
2687         l_full_name varchar2(150);
2688         l_sur_name varchar2(150); -- P11D 08/09
2689         l_fore_name varchar2(150); -- P11D 08/09
2690         l_national_ins_no varchar2(150);
2691         l_employee_number varchar2(150);
2692         l_pactid number;
2693         l_person_id number;
2694         l_loop_count Number;
2695   begin
2696         get_employer_details(p_assignment_action_id,
2697                              l_emp_ref_no,
2698                              l_employer_name);
2699         get_employee_details(p_assignment_action_id,
2700                              l_full_name,
2701                              l_national_ins_no,
2702                              l_employee_number);
2703         -- P11D 08/09
2704         -- Fetch sur and fore names
2705         get_sur_fore_names(p_assignment_action_id,
2706                            l_sur_name,
2707                            l_fore_name);
2708         -- P11D 08/09
2709         l_pactid := get_pactid(p_assignment_action_id);
2710         l_person_id := get_person_id(p_assignment_action_id );
2711         l_loop_count := 0;
2712         for context_rec in csr_context_id(l_pactid,
2713                                           l_person_id,
2714                                           l_emp_ref_no,
2715                                           l_employer_name) loop
2716         for living_acco in  csr_living_acco(context_rec.action_context_id)
2717         loop
2718             l_loop_count := l_loop_count+1;
2719             l_xfdf_str_tab.extend;
2720             l_employee_number := get_assignment_number(context_rec.action_context_id);
2721             l_xfdf_str_tab(l_loop_count) := '<?xml version = "1.0" encoding = "UTF-8"?>
2722             <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
2723             <fields> ' ||
2724            ' <field name="'||'EMPLOYERS_NAME'||'"><value>' ||replace(l_employer_name,'&','&') || '</value></field> ' ||
2725            ' <field name="'||'FULL_NAME'||'"><value>' ||l_full_name || '</value></field>  ' ||
2726            -- P11D 08/09
2727            ' <field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
2728            ' <field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
2729            -- P11D 08/09
2730            ' <field name="'||'EMPLOYERS_REF_NO'||'"><value>' ||l_emp_ref_no || '</value></field>  ' ||
2731            ' <field name="'||'EMPLOYEE_NUMBER'||'"><value>' ||l_employee_number || '</value></field>  ' ||
2732            -- ' <field name="'||'NATIONAL_INS_NO'||'"><value>' ||l_national_ins_no || '</value></field>  ' ||
2733            ' <field name="'||'NI_1'||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
2734            ' <field name="'||'NI_2'||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
2735            ' <field name="'||'NI_3'||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
2736            ' <field name="'||'NI_4'||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
2737            ' <field name="'||'NI_5'||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
2738            ' <field name="'||'NI_6'||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
2739            ' <field name="'||'NI_7'||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
2740            ' <field name="'||'NI_8'||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
2741            ' <field name="'||'NI_9'||'"><value>' ||substr(l_national_ins_no,9) || '</value></field>  '   ||
2742            ' <field name="'||'ADDRESS'||'"><value>' ||living_acco.address || '</value></field>  ' ||
2743            ' <field name="'||'FULL_YEAR'||'"><value>' ||living_acco.full_year || '</value></field>  ' ||
2744            ' <field name="'||'FULL_YEAR_Y'||'"><value>' ||living_acco.full_year || '</value></field>  ' ||
2745            ' <field name="'||'FULL_YEAR_N'||'"><value>' ||living_acco.full_year || '</value></field>  ' ||
2746            ' <field name="'||'RENT_EMPLOYER'||'"><value>' ||living_acco.rent_employer || '</value></field>  ' ||
2747            ' <field name="'||'ANNUAL_VALUE'||'"><value>' ||living_acco.annual_value || '</value></field>  ' ||
2748            ' <field name="'||'BASIC_CHARGE_COST'||'"><value>' ||living_acco.Basic_Charge_Cost || '</value></field>  ' ||
2749            ' <field name="'||'AMG'||'"><value>' ||living_acco.amg || '</value></field>  ' ||
2750            ' <field name="'||'BASIC_CHARGE'||'"><value>' ||living_acco.Basic_Charge || '</value></field>  ' ||
2751            ' <field name="'||'GROSS_AMOUNT'||'"><value>' ||living_acco.gross_amount || '</value></field>  ' ||
2752            ' <field name="'||'EMP_SHARE_OF_COST'||'"><value>' ||living_acco.emp_share_of_cost || '</value></field>  ' ||
2753            ' <field name="'||'COST_OF_ACCO'||'"><value>' ||living_acco.cost_of_acco || '</value></field>  ' ||
2754            ' <field name="'||'EXCESS_OF_COST'||'"><value>' ||living_acco.excess_of_cost || '</value></field>  ' ||
2755            ' <field name="'||'INTEREST_VALUE'||'"><value>' ||living_acco.INTEREST_VALUE || '</value></field>  ' ||
2756            ' <field name="'||'NUMBER_OF_DAYS'||'"><value>' ||living_acco.NUMBER_OF_DAYS || '</value></field>  ' ||
2757            ' <field name="'||'INTEREST_AMOUNT'||'"><value>' ||living_acco.INTEREST_AMOUNT || '</value></field>  ' ||
2758            ' <field name="'||'RENT_EMPLOYEE'||'"><value>' ||living_acco.rent_employee || '</value></field>  ' ||
2759            ' <field name="'||'RENT_IN_AMG'||'"><value>' ||living_acco.RENT_IN_AMG || '</value></field>  ' ||
2760            ' <field name="'||'ACTUAL_RENT'||'"><value>' ||living_acco.ACTUAL_RENT || '</value></field>  ' ||
2761            ' <field name="'||'ADDITIONAL_CHARGE'||'"><value>' ||living_acco.ADDITIONAL_CHARGE || '</value></field>  ' ||
2762            ' <field name="'||'BASIC_CHARGE_2'||'"><value>' ||living_acco.BASIC_CHARGE_2 || '</value></field>  ' ||
2763            ' <field name="'||'TOTAL'||'"><value>' ||living_acco.TOTAL || '</value></field>  ' ||
2764            '</fields>  </xfdf>';
2765         end loop;
2766         end loop;
2767         open l_ret_ref_cursor for
2768         select *
2769         from table (cast(l_xfdf_str_tab as per_gb_xfdftableType));
2770         p_record_num := l_loop_count;
2771         return l_ret_ref_cursor;
2772    end ;
2773 
2774 -------------------------------------------------------------------------------------------
2775 -- Function: rep_assignment_actions
2776 -- Description: Create a cursor depending on the parameters that are passed in
2777 --              and return the cursor to calling procedure (eg PER_P11d_PAPER_REP)
2778 --              using a Ref Cursor
2779 -------------------------------------------------------------------------------------------
2780    function rep_assignment_actions(p_payroll_action_id Number,
2781                                    p_assignment_action_id  Number,
2782                                    p_organization_id Number,
2783                                    p_location_code Varchar2,
2784                                    p_org_hierarchy Number,
2785                                    p_assignment_set_id Number,
2786                                    p_sort_order1 Varchar2,
2787                                    p_sort_order2 Varchar2,
2788                                    p_chunk_size  Number,
2789                                    p_chunk_number Number,
2790                                    p_person_type Varchar2) return ref_cursor_typ
2791    is
2792         l_ret_ref_cursor ref_cursor_typ;
2793         l_select varchar2(500);
2794         l_from   varchar2(500);
2795         l_where  varchar2(10000);
2796         l_group  varchar2(200);
2797         l_order  varchar2(100);
2798         l_sql    varchar2(15000);
2799         l_payroll_action_id Number;
2800         l_assignment_action_id  Number;
2801         l_organization_id Number;
2802         l_location_code Varchar2(100);
2803         l_org_hierarchy Number;
2804         l_assignment_set_id Number;
2805         l_sort_order1 Varchar2(100);
2806         l_sort_order2 Varchar2(100);
2807         l_pay_effective_date date;
2808         l_start    number;
2809         l_end      number;
2810         l_person_type Varchar2(20);
2811        --
2812         cursor pay_effective_date(c_payroll_action_id in number)
2813         is
2814         select pay.effective_date
2815         from   pay_payroll_actions pay
2816         where  pay.payroll_action_id = c_payroll_action_id;
2817      --
2818    begin
2819         if p_payroll_action_id = 0
2820         then
2821             l_payroll_action_id :=null;
2822         else
2823             l_payroll_action_id := p_payroll_action_id;
2824             --
2825             open pay_effective_date(l_payroll_action_id);
2826             fetch pay_effective_date into l_pay_effective_date;
2827             close pay_effective_date;
2828         end if;
2829 
2830         if p_assignment_action_id = 0
2831         then
2832             l_assignment_action_id := null;
2833         else
2834             l_assignment_action_id :=p_assignment_action_id;
2835         end if;
2836 
2837         if p_location_code = '0'
2838         then
2839             l_location_code :=null;
2840         else
2841             l_location_code :=p_location_code;
2842         end if;
2843 
2844         if p_org_hierarchy = 0
2845         then
2846             l_org_hierarchy :=null;
2847         else
2848             l_org_hierarchy :=p_org_hierarchy;
2849         end if;
2850 
2851         if p_assignment_set_id = 0
2852         then
2853             l_assignment_set_id :=null;
2854         else
2855             l_assignment_set_id :=p_assignment_set_id;
2856         end if;
2857 
2858         if p_sort_order1 = '0'
2859         then
2860             l_sort_order1 :=null;
2861         else
2862             l_sort_order1 :=p_sort_order1;
2863         end if;
2864 
2865         if p_sort_order2 = '0'
2866         then
2867             l_sort_order2 :=null;
2868         else
2869             l_sort_order2 :=p_sort_order2;
2870         end if;
2871 
2872         if p_person_type = '0'
2873         then
2874             l_person_type := null;
2875         else
2876             l_person_type := p_person_type;
2877         end if;
2878 
2879     /* since this query is fired just once in a session,
2880     instead of using the using clause,
2881     we are going to create the sql stmt by concatenating the values
2882     below is the example of it being dynamic
2883     */
2884  -- All above Commented out, start proper code here..
2885  --
2886         l_select := 'select asg_id
2887                      from (select asg_id, rownum as row_num
2888                            from (select /*+ ORDERED use_nl(paa,paf,emp,pai_payroll)
2889                                             use_index(pai_person,pay_action_information_n2)
2890                                             use_index(pai,pay_action_information_n2) */
2891                                         paf.person_id, max(paa.assignment_action_id) as asg_id ';
2892         l_from   := 'from   pay_assignment_actions paa,
2893                             per_all_assignments_f  paf,
2894                             pay_action_information emp,
2895                             pay_action_information pai_payroll ';
2896         l_where  := 'where  paa.payroll_action_id = ' || l_payroll_action_id || '
2897                      and    paa.action_status = ''C''
2898                      and    paa.assignment_id = paf.assignment_id
2899                      and    emp.action_information_category = ''EMPLOYEE DETAILS''
2900                      and    emp.action_context_id = paa.assignment_action_id
2901                      and    emp.action_context_type = ''AAP''
2902                      and    pai_payroll.action_information_category = ''GB EMPLOYEE DETAILS''
2903                      and    pai_payroll.action_context_id = paa.assignment_action_id
2904                      and    pai_payroll.action_context_type = ''AAP'' ';
2905         if l_assignment_action_id is not null
2906         then
2907             l_where :=  l_where || 'and   paa.assignment_action_id = ' || l_assignment_action_id ;
2908         end if;
2909 
2910         if l_person_type is not null
2911         then
2912             l_from := l_from || ' ,per_all_people_f pap
2913                                   ,per_person_types ppt ';
2914             l_where := l_where || 'and    pap.person_id = paf.person_id
2915                                    and    pap.person_type_id = ppt.person_type_id
2916                                    and    ppt.system_person_type = ''EX_EMP'' ';
2917         end if;
2918 
2919         if l_organization_id is not null
2920         then
2921             l_where := l_where || ' and   emp.action_information2 = ' || l_organization_id ;
2922         end if;
2923 
2924         if l_location_code is not null
2925         then
2926             l_where := l_where || ' and   nvl(emp.action_information30,''0'')= ''' || l_location_code || ''' ' ;
2927         end if;
2928 
2929         if l_org_hierarchy is not null
2930         then
2931             l_where := l_where || ' and   emp.action_information2 in(select organization_id_child
2932                                                                      from   per_org_structure_elements
2933                                                                      where  business_group_id = ' || l_org_hierarchy ||
2934                                                                    ' union
2935                                                                      select ' || l_org_hierarchy  || ' from dual)';
2936         end if;
2937 
2938         if l_assignment_set_id is not null
2939         then
2940             l_from := l_from  || ',hr_assignment_sets has
2941                                   ,hr_assignment_set_amendments hasa ';
2942             l_where := l_where ||
2943                         ' and    has.assignment_set_id  = ' || l_assignment_set_id ||
2944                         ' and    has.assignment_set_id = hasa.assignment_set_id(+)
2945                           and    ((    has.payroll_id is null
2946                                    and hasa.include_or_exclude = ''I''
2947                                    and hasa.assignment_id = paa.assignment_id
2948                                   )
2949                                   OR
2950                                  (     has.payroll_id is not null
2951                                    and has.payroll_id  = pai_payroll.ACTION_INFORMATION5
2952                                    and nvl(hasa.include_or_exclude, ''I'') = ''I''
2953                                    and nvl(hasa.assignment_id, paa.assignment_id) = paa.assignment_id
2954                                 )) ';
2955         end if;
2956 
2957         l_group  := ' group by paf.person_id, pai_payroll.action_information13 ';
2958 
2959         if l_sort_order1 is not null
2960         then
2961             if l_sort_order1 = 'NAME'
2962             then
2963                 l_order := ' ORDER BY emp.action_information1';
2964                 l_select := l_select || ',emp.action_information1 ';
2965                 l_group  := l_group || ',emp.action_information1 ';
2966             elsif l_sort_order1 = 'NUMBER'
2967             then
2968                 l_order := ' ORDER BY emp.action_information10';
2969                 l_select := l_select || ',emp.action_information10 ';
2970                 l_group  := l_group || ',emp.action_information10 ';
2971             end if;
2972         else -- sort order 1 is null
2973             if l_sort_order2 is not null
2974             then
2975                 if l_sort_order2 = 'NAME'
2976                 then
2977                     l_order := ' ORDER BY emp.action_information1';
2978                     l_select := l_select || ',emp.action_information1 ';
2979                     l_group  := l_group || ',emp.action_information1 ';
2980                 elsif l_sort_order2 = 'NUMBER'
2981                 then
2982                     l_order := ' ORDER BY emp.action_information10';
2983                     l_select := l_select || ',emp.action_information10 ';
2984                     l_group  := l_group || ',emp.action_information10 ';
2985                 end if;
2986             else -- sort order 2 is also null!
2987                 l_order := ' ORDER BY emp.action_information1';
2988                 l_select := l_select || ',emp.action_information1 ';
2989                 l_group  := l_group || ',emp.action_information1 ';
2990             end if;
2991         end if;
2992 
2993         l_start := ((p_chunk_number * p_chunk_size) - p_chunk_size) + 1;
2994         l_end   := (p_chunk_number * p_chunk_size);
2995         l_order := l_order || ')) where row_num between ' || l_start || ' and ' || l_end;
2996         /****************************************************************************/
2997         l_sql :=  l_select || l_from || l_where || l_group || l_order;
2998 
2999         open l_ret_ref_cursor
3000         for l_sql;
3001 
3002         return l_ret_ref_cursor;
3003    --
3004    end rep_assignment_actions;
3005 --------------------------------------------------------------------------------------------
3006    function fetch_ws2_ref_cursor (p_assignment_action_id Number,
3007                                   p_record_num out nocopy NUmber) return ref_cursor_typ
3008    is
3009         -- per_gb_xfdftableType is explicitly created type
3010         -- only modification needed is it could be of type blob
3011         -- i tried thta but could not access the blob valus in java routine.
3012         -- can be sorted later
3013         l_xfdf_str_tab per_gb_xfdftableType := per_gb_xfdftableType( );
3014         l_xfdf_str varchar2(32000);
3015         l_ret_ref_cursor ref_cursor_typ;
3016         l_offset integer;
3017         l_varchar_buffer varchar2(32000);
3018         l_raw_buffer raw(32000);
3019         l_buffer_len number:= 32000;
3020         l_chunk_len number;
3021         l_car_max_price number;
3022 
3023         cursor csr_engine_discount(p_size number,
3024                                    p_date date) is
3025         select to_number(i.value)
3026         from   pay_user_tables t,
3027                pay_user_rows_f r,
3028                pay_user_columns c,
3029                pay_user_column_instances_f i
3030         where  t.user_table_name = 'GB_CC_SCALE'
3031         and    t.user_table_id = r.user_table_id
3032         and    t.user_table_id = c.user_table_id
3033         and    c.user_column_name = 'BEFORE_JAN_1_1998'
3034         and    i.user_row_id = r.user_row_id
3035         and    i.user_column_id = c.user_column_id
3036         and    p_size between to_number(r.row_low_range_or_name) and to_number(r.row_high_range)
3037         and    p_date between r.effective_start_date and r.effective_end_date
3038         and    p_date between i.effective_start_date and i.effective_end_date;
3039 
3040         cursor csr_context_id (p_pactid    NUMBER,
3041                                p_person_id NUMBER,
3042                                p_emp_ref   VARCHAR2,
3043                                p_emp_name  VARCHAR2,
3044                                p_category  VARCHAR2)
3045         is
3046         select /*+ ORDERED use_nl(ppa, paa, pai_comp, pai_person, pai_car)
3047                            use_index(pai_comp,pay_action_information_n2)
3048                            use_index(pai_person,pay_action_information_n2)
3049                            use_index(pai_car,pay_action_information_n2) */
3050                pai_car.action_context_id
3051         from   pay_payroll_actions ppa,
3052                pay_assignment_actions paa,
3053                pay_action_information pai_comp,
3054                pay_action_information pai_person,
3055                pay_action_information pai_car
3056         where  ppa.payroll_action_id = p_pactid
3057         and    paa.payroll_action_id = ppa.payroll_action_id
3058         and    pai_comp.action_context_id = paa.assignment_action_id
3059         and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
3060         and    pai_comp.action_context_type = 'AAP'
3061         and    pai_person.action_context_id = paa.assignment_action_id
3062         and    pai_person.action_information_category = 'ADDRESS DETAILS'
3063         and    pai_person.action_context_type = 'AAP'
3064         and    pai_person.action_information14 = 'Employee Address'
3065         and    pai_person.action_information1 = to_char(p_person_id)
3066         and    pai_comp.action_information6 = p_emp_ref
3067         and    pai_comp.action_information7 = p_emp_name
3068         and    pai_car.action_context_id = paa.assignment_action_id
3069         and    pai_car.action_information_category = p_category
3070         and    pai_car.action_context_type = 'AAP'
3071         group by pai_car.action_context_id;
3072 
3073         cursor csr_car_entries (p_context_id Number)
3074         is
3075         select pai_emp.action_information3 ben_start,
3076                pai_emp.action_information4 ben_end,
3077                decode(months_between(
3078                 fnd_date.canonical_to_date(pai_emp.action_information4)+1,
3079                 fnd_date.canonical_to_date(pai_emp.action_information3)),12,'Y','N') FULL_YR_FLAG,
3080                -- pai_emp.ACTION_INFORMATION5,
3081                pai_emp.action_information6 make,
3082                pai_emp.action_information7 model,
3083                pai_emp.action_information8 date_registered,
3084                substr(pai_emp.action_information8,9,2) || '/' ||
3085                substr(pai_emp.action_information8,6,2) || '/' ||
3086                substr(pai_emp.action_information8,1,4) f_dreg,
3087                pai_emp.action_information13 CO2,
3088                substr(pai_emp.action_information3,9,2) || '/' ||
3089                substr(pai_emp.action_information3,6,2) || '/' ||
3090                substr(pai_emp.action_information3,1,4) f_start,
3091                substr(pai_emp.action_information4,9,2) || '/' ||
3092                substr(pai_emp.action_information4,6,2) || '/' ||
3093                substr(pai_emp.action_information4,1,4) f_end,
3094                to_char(to_number(nvl(pai_emp.action_information9,0)),'FM999,999,990.00') f_lprice,
3095                to_char(to_number(nvl(pai_emp.action_information10,0)),'FM999,999,990.00') f_cc,
3096                to_char(to_number(nvl(pai_emp.action_information11,0)),'FM999,999,990.00') f_fcc,
3097                decode( pai_emp.action_information12,'0',null,PAY_GB_P11D_MAGTAPE.get_description(
3098                      pai_emp.action_information12,'GB_FUEL_TYPE',pai_emp.action_information4)) f_fuel,
3099                -- DECODE(pai_emp.action_information13, NULL, 'On', 'Off') f_nfig,
3100                to_char(to_number(nvl(pai_emp.action_information16,0)),'FM999,999,990.00') f_oprice,
3101                to_char(to_number(nvl(pai_emp.action_information9,0) +nvl(pai_emp.action_information16,0)),
3102                       'FM999,999,990.00') TOTAL_INIT_PRICE,
3103                to_char(to_number(nvl(pai_emp.action_information17,0)),'FM999,999,990.00') f_aprice,
3104                to_char(to_number(nvl(pai_emp.action_information9,0) + nvl(pai_emp.action_information16,0) +
3105                       nvl(pai_emp.action_information17,0)),'FM999,999,990.00') TOTAL_PRICE,
3106                to_char(to_number(nvl(pai_emp.action_information18,0)),'FM999,999,990.00') f_cost,
3107                to_char(to_number(nvl(pai_emp.action_information9,0) + nvl(pai_emp.action_information16,0) +
3108                       nvl(pai_emp.action_information17,0) + nvl(pai_emp.action_information18,0)),
3109                       'FM999,999,990.00') FINAL_PRICE,
3110                to_char(to_number(nvl(pai_emp.action_information19,0)),'FM999,999,990.00') f_amg,
3111                pai_emp.action_information20 f_esize,
3112                to_char(to_number(nvl(pai_emp.action_information21,0)),'FM999,999,990.00') benefit_charge,
3113                to_char(to_number(nvl(pai_emp.action_information22,0)),'FM999,999,990') unavailable,
3114                to_char(to_number(nvl(pai_emp.action_information23,0)),'FM999,999,990.00') unavailable_value,
3115                to_char(to_number(nvl(pai_emp.action_information21,0) - nvl(pai_emp.action_information26,0) -
3116                       nvl(pai_emp.action_information27,0) - nvl(pai_emp.action_information23,0)),
3117                       'FM999,999,990.00') BENEFIT_AFTER_UNAVAIL,
3118                nvl(pai_emp.action_information24,'N') FUEL_BENEFIT,
3119                to_char(to_number(nvl(pai_emp.action_information25,0)),'FM999,999,990') BENEFIT_PERCENT,
3120                to_char(to_number(nvl(pai_emp.action_information26,0)),'FM999,999,990.00') STD_DISC,
3121                to_char(to_number(decode(pai_emp.action_information27,0,null,pai_emp.action_information27))
3122                       ,'FM999,999,990.00') ADD_DISC,
3123                to_char(to_number(decode(nvl(pai_emp.action_information27,0) +
3124                       nvl(pai_emp.action_information26,0),0,null, nvl(pai_emp.action_information27,0) +
3125                       nvl(pai_emp.action_information26,0))),'FM999,999,990.00') FULL_DISC,
3126                to_char(to_number(pai_emp.action_information28),'FM999,999,990.00') FUEL_SCALE,
3127                nvl(pai_emp.action_information21,0) ben_charge,
3128                nvl(pai_emp.action_information26,0) stand_disc,
3129                nvl(pai_emp.action_information27,0) additional_disc
3130         from   pay_action_information pai_emp
3131         where  pai_emp.action_information_category = 'CAR AND CAR FUEL'
3132         and    pai_emp.action_context_id = p_context_id
3133         and    pai_emp.action_context_type = 'AAP';
3134 
3135         cursor csr_car_entries_0304 (p_context_id Number)
3136         is
3137         select pai_emp.action_information3 ben_start,
3138                pai_emp.action_information4 ben_end,
3139                decode(
3140                   months_between(
3141                     fnd_date.canonical_to_date(pai_emp.action_information4)+1,
3142                     fnd_date.canonical_to_date(pai_emp.action_information3)),12,'Y','N') FULL_YR_FLAG,
3143                   --pai_emp.ACTION_INFORMATION5 co2,
3144                pai_emp.action_information6 make,
3145                pai_emp.action_information7 model,
3146                pai_emp.action_information8 date_registered,
3147                substr(pai_emp.action_information8,9,2) || ' ' ||
3148                substr(pai_emp.action_information8,6,2) || ' ' ||
3149                substr(pai_emp.action_information8,1,4) f_dreg,
3150                pai_emp.action_information13 co2,
3151                substr(pai_emp.action_information3,9,2) || ' ' ||
3152                substr(pai_emp.action_information3,6,2) || ' ' ||
3153                substr(pai_emp.action_information3,1,4) f_start,
3154                substr(pai_emp.action_information4,9,2) || ' ' ||
3155                substr(pai_emp.action_information4,6,2) || ' ' ||
3156                substr(pai_emp.action_information4,1,4) f_end,
3157                to_char(to_number(nvl(pai_emp.action_information9,0)),'FM999,999,990.00') f_lprice,
3158                to_char(to_number(nvl(pai_emp.action_information10,0)),'FM999,999,990.00') f_cc,
3159                to_char(to_number(nvl(pai_emp.action_information11,0)),'FM999,999,990.00') f_fcc,
3160                decode(pai_emp.action_information12,'0',null,PAY_GB_P11D_MAGTAPE.get_description(
3161                       pai_emp.action_information12,'GB_FUEL_TYPE',pai_emp.action_information4)) f_fuel,
3162                to_char(to_number(nvl(pai_emp.action_information15,0)),'FM999,999,990.00') f_oprice,
3163                to_char(to_number(nvl(pai_emp.action_information9,0) +
3164                       nvl(pai_emp.action_information15,0)),'FM999,999,990.00') TOTAL_INIT_PRICE,
3165                to_char(to_number(nvl(pai_emp.action_information16,0)),'FM999,999,990.00') f_cost,
3166                to_char(least(l_car_max_price,(to_number(nvl(pai_emp.action_information9,0) +
3167                        nvl(pai_emp.action_information15,0) -
3168                        nvl(pai_emp.action_information16,0)))),'FM999,999,990.00') FINAL_PRICE,
3169                to_char(to_number(nvl(pai_emp.action_information17,0)),'FM999,999,990.00') f_amg,
3170                pai_emp.action_information18 f_esize,
3171                to_char(to_number(nvl(pai_emp.action_information19,0)),'FM999,999,990') car_benefit_year,
3172                to_char(to_number(nvl(pai_emp.action_information20,0)),'FM999,999,990') unavailable,
3173                to_char(to_number(nvl(pai_emp.action_information21,0)),'FM999,999,990') unavailable_value,
3174                to_char(to_number(nvl(pai_emp.action_information19,0) -
3175                       nvl(pai_emp.action_information21,0)),'FM999,999,990.00') CAR_BENEFIT_AVAILABLE,
3176                to_char(to_number(nvl(pai_emp.action_information22,0)),'FM999,999,990') BENEFIT_PERCENT,
3177                to_char(to_number(nvl(pai_emp.action_information23,0)),'FM999,999,990') STD_DISC,
3178                to_char(to_number(nvl(pai_emp.action_information24,0)),'FM999,999,990') ROUND_NORMAL_CO2,
3179                -- Added substring function to get fuel benefit value from action_information25 (P11D 07/08 changes)
3180                nvl(substr(pai_emp.action_information25,1,instr(pai_emp.action_information25,':')-1),'N') FUEL_BENEFIT,
3181                to_char(to_number(pai_emp.action_information29),'FM999,999,990') FUEL_BENEFIT_YEAR,
3182                to_char(to_number(nvl(pai_emp.action_information23,0) +
3183                       nvl(pai_emp.action_information24,0)),'FM999,999,990') FULL_DISC,
3184                to_char(to_number(nvl(pai_emp.action_information22,0) -
3185                       nvl(pai_emp.action_information23,0) -
3186                       nvl(pai_emp.action_information24,0)), 'FM999,999,990') TOTAL_BENIFIT,
3187                to_char(to_number(nvl(pai_emp.action_information22,0) -
3188                       nvl(pai_emp.action_information23,0)),   'FM999,999,990') TOTAL_BENIFIT_2,
3189                decode (pai_emp.action_information26,null,null,
3190                       decode ( pai_emp.action_information27,'Y',null,
3191                       substr(pai_emp.action_information26,9,2) || ' ' ||
3192                       substr(pai_emp.action_information26,6,2) || ' ' ||
3193                       substr(pai_emp.action_information26,1,4))) f_withdraw,
3194                to_char(to_number(nvl(pai_emp.action_information28,0))) additional_days,
3195                decode (pai_emp.action_information26,null,to_char(to_number(nvl(pai_emp.action_information20,0))),
3196                       to_char(to_number(nvl(pai_emp.action_information20,0) +
3197                       nvl(pai_emp.action_information28,0)) ) ) total_days ,
3198                to_char(to_number(nvl(pai_emp.action_information30,0)),'FM999,999,990') fuel_unavailable,
3199 			   pai_emp.action_information4 full_end_date
3200         from   pay_action_information pai_emp
3201         where  pai_emp.action_information_category = 'CAR AND CAR FUEL 2003_04'
3202         and    pai_emp.action_context_id = p_context_id
3203         and    pai_emp.action_context_type = 'AAP';
3204 
3205         -- car_rec csr_car_entries%rowtype;
3206         l_BEN_ST_DATE  varchar2(20);
3207         l_BEN_ED_DATE  varchar2(20);
3208         l_car_count    number;
3209         l_ONLY_CAR_FLAG varchar2(5);
3210         l_FUEL_FLAG varchar2(5);
3211         l_fuel_scale varchar2(20);
3212         l_FUEL_BENEFIT_YEAR VARCHAR2 (20);
3213         l_CAR_NUMBERS  number;
3214         l_MOD_CO2     number;
3215         l_PERCENT_1  varchar2(20);
3216         l_BEN_1      varchar2(20);
3217         l_PERCENT_2  varchar2(20);
3218         l_BEN_2 varchar2(20);
3219         l_PERCENT_3 varchar2(20);
3220         l_BEN_3 varchar2(20);
3221         l_PERCENT_4 varchar2(20);
3222         l_BEN_4 varchar2(20);
3223         l_BEN_5 varchar2(20);
3224         l_STD_DISC_1 varchar2(20);
3225         l_STD_DISC_2 varchar2(20);
3226         l_STD_DISC_3 varchar2(20);
3227         l_STD_DISC_4 varchar2(20);
3228         l_STD_DISC_5 varchar2(20);
3229         l_full_DISC varchar2(20);
3230         l_EXTRA_CO2  number;
3231         l_NORMAL_CO2  number;
3232         l_ROUND_NORMAL_CO2  number;
3233         l_CAR_BENEFIT_1  Number;
3234         l_CAR_BENEFIT_2  Number;
3235         l_CAR_BENEFIT_3  Number;
3236         -- ,4,5 can be varchar2 as these are not calc and are just read from benefit
3237         --charge
3238         l_CAR_BENEFIT_4  varchar2(20);
3239         l_CAR_BENEFIT_5  varchar2(20);
3240         l_CAR_BENEFIT_6  varchar2(20); -- P11D 08/09
3241         l_emp_ref_no varchar2(150);
3242         l_employer_name varchar2(150);
3243         l_full_name varchar2(150);
3244         l_sur_name varchar2(150); --  P11D 08/09
3245         l_fore_name varchar2(150); -- P11D 08/09
3246         l_national_ins_no varchar2(150);
3247         l_employee_number varchar2(150);
3248         l_pactid number;
3249         l_person_id number;
3250         l_loop_count Number;
3251         l_esize varchar2(20);
3252         l_UNAVIALABLE_VAL_2 varchar2(20);
3253         l_rep_run varchar2(10);
3254         l_add_days varchar2(10);
3255         l_tot_days varchar2(10);
3256         l_unavailble_days varchar2(10);
3257         l_fuel_new varchar2(1);
3258    begin
3259         get_employer_details(p_assignment_action_id,
3260                              l_emp_ref_no,
3261                              l_employer_name);
3262         get_employee_details(p_assignment_action_id,
3263                              l_full_name,
3264                              l_national_ins_no,
3265                              l_employee_number);
3266         -- P11D 08/09
3267         -- Fetch sur and fore names
3268         get_sur_fore_names(p_assignment_action_id,
3269                            l_sur_name,
3270                            l_fore_name);
3271         -- P11D 08/09
3272         l_pactid := get_pactid(p_assignment_action_id);
3273         l_person_id := get_person_id (p_assignment_action_id );
3274         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
3275          p_payroll_action_id           => l_pactid,
3276          p_token_name                  => 'Rep_Run',
3277          p_token_value                 => l_rep_run);
3278 
3279         select to_number(global_value)
3280         into  l_car_max_price
3281         from  ff_globals_f
3282         where global_name = 'NI_CAR_MAX_PRICE'
3283         and   to_date('05-04-' || l_rep_run,'DD-MM-YYYY') between effective_start_date and effective_end_date;
3284 
3285         l_loop_count := 0;
3286         if l_rep_run = '2003'
3287         then
3288             for context_rec in csr_context_id(l_pactid,
3289                                               l_person_id,
3290                                               l_emp_ref_no,
3291                                               l_employer_name,
3292                                               'CAR AND CAR FUEL') loop
3293             for car_rec in  csr_car_entries(context_rec.action_context_id)
3294             loop
3295                 l_loop_count := l_loop_count+1;
3296                 -- Initialising the vars to null;
3297                 l_BEN_ST_DATE  := null;
3298                 l_BEN_ED_DATE  := null;
3299                 l_car_count    := null;
3300                 l_ONLY_CAR_FLAG :=null;
3301                 l_FUEL_FLAG :=null;
3302                 l_CAR_NUMBERS  := null;
3303                 l_MOD_CO2     := null;
3304                 l_PERCENT_1  := null;
3305                 l_BEN_1      := null;
3306                 l_PERCENT_2  := null;
3307                 l_BEN_2 := null;
3308                 l_PERCENT_3 := null;
3309                 l_BEN_3 := null;
3310                 l_PERCENT_4 := null;
3311                 l_BEN_4 := null;
3312                 l_BEN_5 := null;
3313                 l_STD_DISC_1 := null;
3314                 l_STD_DISC_2 := null;
3315                 l_STD_DISC_3 := null;
3316                 l_STD_DISC_4 := null;
3317                 l_STD_DISC_5 := null;
3318                 l_full_DISC  := null;
3319                 l_EXTRA_CO2  := null;
3320                 l_NORMAL_CO2  := null;
3321                 l_ROUND_NORMAL_CO2  := null;
3322                 l_CAR_BENEFIT_1  := null;
3323                 l_CAR_BENEFIT_2  := null;
3324                 l_CAR_BENEFIT_3  := null;
3325                 l_CAR_BENEFIT_4  := null;
3326                 l_CAR_BENEFIT_5  := null;
3327                 l_full_DISC := car_rec.FULL_DISC;
3328                 if car_rec.FULL_YR_FLAG = 'Y'
3329                 then
3330                     l_BEN_ST_DATE := null;
3331                     l_BEN_ED_DATE := null;
3332                 else
3333                     l_BEN_ST_DATE := car_rec.f_start;
3334                     l_BEN_ED_DATE := car_rec.f_end;
3335                 end if;
3336                 hr_utility.trace('A4');
3337                 select action_information30
3338                 into   l_car_count
3339                 from   pay_action_information pai_emp
3340                 where  pai_emp.action_context_id = p_assignment_action_id
3341                 and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTA';
3342 
3343                 hr_utility.trace('A5');
3344                 if l_car_count = 1
3345                 then
3346                     l_ONLY_CAR_FLAG := 'Y';
3347                 else
3348                     l_ONLY_CAR_FLAG := 'N';
3349                     l_CAR_NUMBERS := l_car_count;
3350                 end if   ;
3351                 hr_utility.trace('A6');
3352                 hr_utility.trace('date_registered ' ||car_rec.date_registered);
3353                 -- calculating benefit charge section
3354                 if fnd_date.canonical_to_date( car_rec.date_registered)
3355                    >= to_date('01-01-1998','dd-mm-yyyy') and car_rec.CO2 is not null
3356                 then
3357                     hr_utility.trace('A');
3358                     l_MOD_CO2 := floor(car_rec.CO2/5) * 5;
3359                     hr_utility.trace('b');
3360                     if (car_rec.f_fuel = 'B' or car_rec.f_fuel = 'C' or
3361                         car_rec.f_fuel = 'H' or car_rec.f_fuel = 'L' or
3362                         car_rec.f_fuel = 'P')
3363                     then
3364                         hr_utility.trace('c');
3365                         l_PERCENT_1 := car_rec.BENEFIT_PERCENT;
3366                         l_BEN_1 := car_rec.benefit_charge;
3367                         hr_utility.trace('d');
3368                     elsif ( car_rec.f_fuel ='D' )
3369                     then
3370                         hr_utility.trace('e');
3371                         l_PERCENT_2 := car_rec.BENEFIT_PERCENT;
3372                         l_BEN_2 := car_rec.benefit_charge;
3373 					            elsif car_rec.f_fuel = 'E'
3374                         then
3375                            l_PERCENT_2 :=0;
3376                            l_STD_DISC_2   := 0;
3377                     end if;
3378 
3379                     hr_utility.trace('f');
3380                     if  car_rec.f_fuel  = 'H'
3381                     then
3382                         l_STD_DISC_1   := car_rec.STD_DISC;
3383                     elsif ( car_rec.f_fuel = 'B' or car_rec.f_fuel = 'C')
3384                     then
3385                         l_STD_DISC_2   := car_rec.STD_DISC;
3386                     end if;
3387                     hr_utility.trace('g');
3388                     if l_MOD_CO2 <= 145
3389                     then
3390                         l_EXTRA_CO2 := 165 -l_MOD_CO2;
3391                         l_NORMAL_CO2 := l_EXTRA_CO2/20;
3392                         l_ROUND_NORMAL_CO2 := floor(l_NORMAL_CO2);
3393                     end if;
3394                     hr_utility.trace('h');
3395                     l_CAR_BENEFIT_1   := car_rec.ben_charge - car_rec.stand_disc - car_rec.additional_disc;
3396                 elsif fnd_date.canonical_to_date( car_rec.date_registered)
3397                       >= to_date('01-01-1998','dd-mm-yyyy') and car_rec.co2 is null
3398                 then
3399                     hr_utility.trace('i');
3400                     if (car_rec.f_fuel = 'B' or car_rec.f_fuel = 'C' or
3401                         car_rec.f_fuel = 'H' or car_rec.f_fuel = 'L' or
3402                         car_rec.f_fuel = 'P' )
3403                     then
3404                         l_PERCENT_3 := car_rec.BENEFIT_PERCENT;
3405                         l_BEN_3 := car_rec.benefit_charge;
3406                     elsif car_rec.f_fuel ='D'
3407                     then
3408                         l_PERCENT_4 := car_rec.BENEFIT_PERCENT;
3409                         l_BEN_4 := car_rec.benefit_charge;
3410                     elsif car_rec.f_fuel ='E'
3411                     then
3412                         l_BEN_5 := car_rec.benefit_charge;
3413                     end if;
3414 
3415                      if  car_rec.f_fuel  = 'H'
3416                      then
3417                         l_STD_DISC_4   := car_rec.STD_DISC;
3418                     elsif ( car_rec.f_fuel = 'B' or car_rec.f_fuel = 'C')
3419                     then
3420                         l_STD_DISC_5   := car_rec.STD_DISC;
3421                     elsif car_rec.f_fuel = 'E'
3422                     then
3423                         l_STD_DISC_3   := car_rec.STD_DISC;
3424                     end if;
3425                     hr_utility.trace('j');
3426                     if car_rec.f_fuel ='E'
3427                     then
3428                        l_CAR_BENEFIT_2  := 0;
3429                     else
3430                        l_CAR_BENEFIT_2  := car_rec.ben_charge - car_rec.stand_disc;
3431                     end if;
3432                     l_full_DISC := null;
3433                elsif fnd_date.canonical_to_date( car_rec.date_registered)
3434                      < to_date('01-01-1998','dd-mm-yyyy')
3435                then
3436                    hr_utility.trace('K');
3437                    -- hr_utility.trace('car_rec.benefit_charge '|| car_rec.benefit_charge);
3438                    l_full_disc := null;
3439                    if car_rec.f_esize <= 1400
3440                    then
3441                        l_CAR_BENEFIT_3 :=  car_rec.benefit_charge;
3442                    elsif car_rec.f_esize <= 2000
3443                    then
3444                             l_CAR_BENEFIT_4 :=  car_rec.benefit_charge;
3445                    elsif car_rec.f_esize > 2000
3446                     then
3447                        l_CAR_BENEFIT_5 :=  car_rec.benefit_charge;
3448                    end if;
3449                end if;
3450                hr_utility.trace('k');
3451                -- fuel benefit
3452                l_esize := null;
3453                l_UNAVIALABLE_VAL_2 := null;
3454                if car_rec.FUEL_BENEFIT = 'Y'
3455                then
3456                        l_esize := car_rec.f_esize;
3457                    l_UNAVIALABLE_VAL_2 := car_rec.unavailable_value;
3458                    if car_rec.f_fuel = 'P'
3459                    then
3460                                    l_FUEL_FLAG := 'P';
3461                    else
3462                        l_FUEL_FLAG := 'D';
3463                    end if;
3464                end if;
3465                hr_utility.trace('L');
3466                l_xfdf_str_tab.extend;
3467                l_xfdf_str_tab(l_loop_count) := '<?xml version = "1.0" encoding = "UTF-8"?>
3468                    <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
3469                    <fields> ' ||
3470                    '<field name="'||'EMPLOYERS_NAME'||'"><value>'||replace(l_employer_name,'&','&')       ||'</value></field>'||
3471                    '<field name="'||'FULL_NAME'||'"><value>'||l_full_name||'</value></field>'||
3472                    '<field name="'||'EMPLOYERS_REF_NO'||'"><value>'||l_emp_ref_no||'</value></field>'||
3473                    '<field name="'||'EMPLOYEE_NUMBER'||'"><value>'||l_employee_number||'</value></field>'||
3474                    '<field name="'||'NATIONAL_INS_NO'||'"><value>'||l_national_ins_no||'</value></field>'||
3475                    '<field name="'||'MAKE'||'"><value>'||car_rec.make||'</value></field>'||
3476                    '<field name="'||'MODEL'||'"><value>'||car_rec.model||'</value></field>'||
3477                    '<field name="'||'DATE_FIRST_REG'||'"><value>'||car_rec.f_dreg||'</value></field>'||
3478                    '<field name="'||'CO2'||'"><value>'||car_rec.CO2||'</value></field>'||
3479                    '<field name="'||'FULL_YR_FLAG'||'"><value>'||car_rec.FULL_YR_FLAG||'</value></field>'||
3480                    '<field name="'||'BEN_ST_DATE'||'"><value>'||l_BEN_ST_DATE||'</value></field>'||
3481                    '<field name="'||'BEN_ED_DATE'||'"><value>'||l_BEN_ED_DATE||'</value></field>'||
3482                    '<field name="'||'UNAVAILABLE'||'"><value>'||car_rec.unavailable||'</value></field>'||
3483                    '<field name="'||'ONLY_CAR_FLAG'||'"><value>'||l_ONLY_CAR_FLAG||'</value></field>'||
3484                    '<field name="'||'CAR_NUMBERS'||'"><value>'||l_CAR_NUMBERS||'</value></field>'||
3485                    '<field name="'||'LIST_PRICE'||'"><value>'||car_rec.f_lprice||'</value></field>'||
3486                    '<field name="'||'OPTIONAL_EXTRAS'||'"><value>'||car_rec.f_oprice||'</value></field>'||
3487                    '<field name="'||'TOTAL_INIT_PRICE'||'"><value>'||car_rec.TOTAL_INIT_PRICE||'</value></field>'||
3488                    '<field name="'||'EXTRAS_LATER'||'"><value>'||car_rec.f_aprice||'</value></field>'||
3489                    '<field name="'||'TOTAL_PRICE'||'"><value>'||car_rec.TOTAL_PRICE||'</value></field>'||
3490                    '<field name="'||'CONTRIBUTIONS'||'"><value>'||car_rec.f_cost||'</value></field>'||
3491                    '<field name="'||'FINAL_PRICE'||'"><value>'||car_rec.FINAL_PRICE||'</value></field>'||
3492                    '<field name="'||'FINAL_PRICE_DUP'||'"><value>'||car_rec.FINAL_PRICE||'</value></field>'||
3493                    '<field name="'||'FUEL'||'"><value>'||car_rec.f_fuel||'</value></field>'||
3494                    '<field name="'||'MOD_CO2'||'"><value>'||l_MOD_CO2||'</value></field>'||
3495                    '<field name="'||'PERCENT_1'||'"><value>'||l_PERCENT_1||'</value></field>'||
3496                    '<field name="'||'BEN_1'||'"><value>'||l_BEN_1||'</value></field>'||
3497                    '<field name="'||'BEN_2'||'"><value>'||l_BEN_2||'</value></field>'||
3498                    '<field name="'||'PERCENT_2'||'"><value>'||l_PERCENT_2||'</value></field>'||
3499                    '<field name="'||'STD_DISC_1'||'"><value>'||l_STD_DISC_1||'</value></field>'||
3500                    '<field name="'||'STD_DISC_2'||'"><value>'||l_STD_DISC_2||'</value></field>'||
3501                    '<field name="'||'EXTRA_CO2'||'"><value>'||l_EXTRA_CO2||'</value></field>'||
3502                    '<field name="'||'NORMAL_CO2'||'"><value>'||l_NORMAL_CO2||'</value></field>'||
3503                    '<field name="'||'ROUND_NORMAL_CO2'||'"><value>'||l_ROUND_NORMAL_CO2||'</value></field>' ||
3504                    '<field name="'||'ADDITIONAL_DISC'||'"><value>'||car_rec.ADD_DISC||'</value></field>'||
3505                    '<field name="'||'FULL_DISCOUNT'||'"><value>'||l_FULL_DISC||'</value></field>'||
3506                    '<field name="'||'CAR_BENEFIT_1'||'"><value>'||to_char(to_number(l_CAR_BENEFIT_1),'FM999,999,990.00')||'</value></field>'||
3507                    '<field name="'||'PERCENT_3'||'"><value>'||l_PERCENT_3||'</value></field>'||
3508                    '<field name="'||'PERCENT_4'||'"><value>'||l_PERCENT_4||'</value></field>'||
3509                    '<field name="'||'BEN_3'||'"><value>'||l_BEN_3||'</value></field>'||
3510                    '<field name="'||'BEN_4'||'"><value>'||l_BEN_4||'</value></field>'||
3511                    '<field name="'||'BEN_5'||'"><value>'||l_BEN_5||'</value></field>'||
3512                    '<field name="'||'STD_DISC_3'||'"><value>'||l_STD_DISC_3||'</value></field>'||
3513                    '<field name="'||'STD_DISC_4'||'"><value>'||l_STD_DISC_4||'</value></field>'||
3514                    '<field name="'||'STD_DISC_5'||'"><value>'||l_STD_DISC_5||'</value></field>'||
3515                    '<field name="'||'CAR_BENEFIT_2'||'"><value>'||to_char(to_number(l_CAR_BENEFIT_2),'FM999,999,990.00')||'</value></field>'||
3516                    '<field name="'||'CAR_BENEFIT_3'||'"><value>'||l_CAR_BENEFIT_3 ||'</value></field>'||
3517                    '<field name="'||'CAR_BENEFIT_4'||'"><value>'||l_CAR_BENEFIT_4 ||'</value></field>'||
3518                    '<field name="'||'CAR_BENEFIT_5'||'"><value>'||l_CAR_BENEFIT_5 ||'</value></field>'||
3519                    '<field name="'||'UNAVAILABLE_VAL_1'||'"><value>'||car_rec.unavailable_value||'</value></field>'||
3520                    '<field name="'||'BENEFIT_AFTER_UNAVAIL'||'"><value>'||car_rec.BENEFIT_AFTER_UNAVAIL||'</value></field>'||
3521                    '<field name="'||'PRIVATE_USE_PAYMENT'||'"><value>'||car_rec.f_amg||'</value></field>'||
3522                    '<field name="'||'CASH_EQUIVALENT_CAR'||'"><value>'||car_rec.f_cc||'</value></field>'||
3523                    '<field name="'||'FUEL_FLAG'||'"><value>'||l_FUEL_FLAG||'</value></field>'||
3524                    '<field name="'||'ENGINE_CC'||'"><value>'||l_esize||'</value></field>'||
3525                    '<field name="'||'FUEL_SCALE'||'"><value>'||car_rec.FUEL_SCALE||'</value></field>'||
3526                    '<field name="'||'UNAVIALABLE_VAL_2'||'"><value>'||l_UNAVIALABLE_VAL_2||'</value></field>'||
3527                    '<field name="'||'FUEL_BENEFIT_CHARGE'||'"><value>'||car_rec.f_fcc||'</value></field>'||
3528                    '</fields>  </xfdf>';
3529             end loop;
3530             end loop;
3531             /* change from  l_rep_run = '2004' to l_rep_run > 2003 */
3532         elsif to_number(l_rep_run) < 2007 then
3533             for context_rec in csr_context_id(l_pactid,
3534                                               l_person_id,
3535                                               l_emp_ref_no,
3536                                               l_employer_name,
3537                                               'CAR AND CAR FUEL 2003_04') loop
3538             for car_rec in  csr_car_entries_0304(context_rec.action_context_id)
3539             loop
3540                 l_employee_number := get_assignment_number(context_rec.action_context_id);
3541                 l_loop_count := l_loop_count+1;
3542                 --Initialising the vars to null;
3543                 l_BEN_ST_DATE  := null;
3544                 l_BEN_ED_DATE  := null;
3545                 l_car_count    := null;
3546                 l_ONLY_CAR_FLAG :=null;
3547                 l_FUEL_FLAG :=null;
3548                 l_CAR_NUMBERS  := null;
3549                 l_MOD_CO2     := null;
3550                 l_PERCENT_1  := null;
3551                 l_BEN_1      := null;
3552                 l_PERCENT_2  := null;
3553                 l_BEN_2 := null;
3554                 l_PERCENT_3 := null;
3555                 l_BEN_3 := null;
3556                 l_PERCENT_4 := null;
3557                 l_BEN_4 := null;
3558                 l_BEN_5 := null;
3559                 l_STD_DISC_1 := null;
3560                 l_STD_DISC_2 := null;
3561                 l_STD_DISC_3 := null;
3562                 l_STD_DISC_4 := null;
3563                 l_STD_DISC_5 := null;
3564                 l_full_DISC  := null;
3565                 l_EXTRA_CO2  := null;
3566                 l_NORMAL_CO2  := null;
3567                 l_ROUND_NORMAL_CO2  := null;
3568                 l_CAR_BENEFIT_1  := null;
3569                 l_CAR_BENEFIT_2  := null;
3570                 l_CAR_BENEFIT_3  := null;
3571                 l_CAR_BENEFIT_4  := null;
3572                 l_CAR_BENEFIT_5  := null;
3573                 l_esize          := null;
3574                 l_add_days       := null;
3575                 l_tot_days       := null;
3576                 l_unavailble_days := null;
3577                 l_FUEL_BENEFIT_YEAR := null;
3578                 /*l_full_DISC := car_rec.FULL_DISC;  */
3579                 if car_rec.FULL_YR_FLAG = 'Y'
3580                                 then
3581                     l_BEN_ST_DATE := null;
3582                     l_BEN_ED_DATE := null;
3583                 else
3584                     l_BEN_ST_DATE := car_rec.f_start;
3585                     l_BEN_ED_DATE := car_rec.f_end;
3586                 end if;
3587                 -- hr_utility.trace_on(null,'CAR');
3588                 hr_utility.trace('A4');
3589                 select action_information30
3590                 into   l_car_count
3591                 from   pay_action_information pai_emp
3592                 where  pai_emp.action_context_id = p_assignment_action_id
3593                 and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTA';
3594                 hr_utility.trace('A5');
3595                 if l_car_count = 1
3596                                 then
3597                     l_ONLY_CAR_FLAG := 'Y';
3598                 else
3599                     l_ONLY_CAR_FLAG := 'N';
3600                     l_CAR_NUMBERS := l_car_count;
3601                 end if   ;
3602                 hr_utility.trace('A6');
3603                 hr_utility.trace('date_registered ' ||car_rec.date_registered);
3604                 -- calculating benefit charge section
3605                 if fnd_date.canonical_to_date( car_rec.date_registered)
3606                    >= to_date('01-01-1998','dd-mm-yyyy') and car_rec.CO2 is not null
3607                 then
3608                     hr_utility.trace('a');
3609                     l_MOD_CO2 := floor(car_rec.CO2/5) * 5;
3610                     hr_utility.trace('b');
3611                     l_PERCENT_1 := car_rec.BENEFIT_PERCENT;
3612                     --l_BEN_1 := car_rec.benefit_charge;
3613                     hr_utility.trace('c');
3614                     l_STD_DISC_1   := car_rec.STD_DISC;
3615                     hr_utility.trace('d');
3616                     if (car_rec.f_fuel = 'B' or car_rec.f_fuel = 'H') -- Bug #4293824
3617                     then
3618                                             if car_rec.CO2 <= 120
3619                                                 then
3620                              l_EXTRA_CO2 := 145 - car_rec.CO2;
3621                         end if;
3622                     end if;
3623                     l_ROUND_NORMAL_CO2 := car_rec.ROUND_NORMAL_CO2;
3624                     l_full_DISC        := car_rec.FULL_DISC;
3625                     l_CAR_BENEFIT_1    := car_rec.TOTAL_BENIFIT ;
3626                 elsif fnd_date.canonical_to_date( car_rec.date_registered)
3627                       >= to_date('01-01-1998','dd-mm-yyyy') and car_rec.CO2 is null
3628                 then
3629                     hr_utility.trace('e');
3630                        -- below is the code snippet for P11D change 6.1.4.2
3631                     if car_rec.f_fuel = 'E' then
3632                        l_PERCENT_2 := 0;
3633                        l_STD_DISC_2   := 0;
3634                        l_CAR_BENEFIT_2 := 0;
3635                    else
3636                        l_STD_DISC_2   := car_rec.STD_DISC;
3637                        l_PERCENT_2 := car_rec.BENEFIT_PERCENT;
3638                        l_CAR_BENEFIT_2 := car_rec.TOTAL_BENIFIT_2;
3639                    end if;
3640                    hr_utility.trace('f');
3641 
3642 
3643                 elsif (fnd_date.canonical_to_date( car_rec.date_registered)<  to_date('01-01-1998','dd-mm-yyyy'))
3644                                 then
3645                     hr_utility.trace('g');
3646                     -- hr_utility.trace('car_rec.benefit_charge '|| car_rec.benefit_charge);
3647                     l_full_disc := null;
3648                     l_esize := car_rec.f_esize;
3649                     	if car_rec.f_esize <= 1400
3650                     	then
3651                         	l_CAR_BENEFIT_3 :=  15;
3652 	                    elsif car_rec.f_esize <= 2000
3653 	                    then
3654                        	 l_CAR_BENEFIT_3 :=  22;
3655                     	elsif car_rec.f_esize > 2000
3656                    	 	then
3657                       											  l_CAR_BENEFIT_3 :=  32;
3658                    	 	else
3659                   	                          l_CAR_BENEFIT_3 :=  32;
3660                    	 	end if;
3661 								end if;
3662                 hr_utility.trace('h');
3663                 -- fuel benefit
3664                 if car_rec.FUEL_BENEFIT = 'Y'
3665                                 then
3666                     L_FUEL_BENEFIT_YEAR := car_rec.FUEL_BENEFIT_YEAR;
3667                     l_add_days          := car_rec.additional_days;
3668                     l_tot_days        := car_rec.total_days;
3669                     l_unavailble_days   := car_rec.unavailable;
3670                 end if;
3671                 hr_utility.trace('i');
3672                 l_xfdf_str_tab.extend;
3673                 l_xfdf_str_tab(l_loop_count) := '<?xml version = "1.0" encoding = "UTF-8"?>
3674                     <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
3675                     <fields> ' ||
3676                     '<field name="'||'EMPLOYERS_NAME'||'"><value>'||replace(l_employer_name,'&','&')       ||'</value></field>'||
3677                     '<field name="'||'FULL_NAME'||'"><value>'||l_full_name||'</value></field>'||
3678                     '<field name="'||'EMPLOYERS_REF_NO'||'"><value>'||l_emp_ref_no||'</value></field>'||
3679                     '<field name="'||'EMPLOYEE_NUMBER'||'"><value>'||l_employee_number||'</value></field>'||
3680                     -- '<field name="'||'NATIONAL_INS_NO'||'"><value>'||l_national_ins_no||'</value></field>'||
3681                     '<field name="'||'NI_1'||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
3682                     '<field name="'||'NI_2'||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
3683                     '<field name="'||'NI_3'||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
3684                     '<field name="'||'NI_4'||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
3685                     '<field name="'||'NI_5'||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
3686                     '<field name="'||'NI_6'||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
3687                     '<field name="'||'NI_7'||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
3688                     '<field name="'||'NI_8'||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
3689                     '<field name="'||'NI_9'||'"><value>' ||substr(l_national_ins_no,9) || '</value></field>  '   ||
3690                     '<field name="'||'MAKE'||'"><value>'||car_rec.make||'</value></field>'||
3691                     '<field name="'||'MODEL'||'"><value>'||car_rec.model||'</value></field>'||
3692                     '<field name="'||'DATE_FIRST_REG1'||'"><value>'||substr(car_rec.f_dreg,0,2)||'</value></field>'||
3693                     '<field name="'||'DATE_FIRST_REG2'||'"><value>'||substr(car_rec.f_dreg,4,2)||'</value></field>'||
3694                     '<field name="'||'DATE_FIRST_REG3'||'"><value>'||substr(car_rec.f_dreg,7)||'</value></field>'||
3695                     '<field name="'||'ONLY_CAR_FLAG'||'"><value>'||l_ONLY_CAR_FLAG||'</value></field>'||
3696                     --'<field name="'||'CO2'||'"><value>'||car_rec.CO2||'</value></field>'||
3697                     --'<field name="'||'FULL_YR_FLAG'||'"><value>'||car_rec.FULL_YR_FLAG||'</value></field>'||
3698                     '<field name="'||'CAR_NUMBERS'||'"><value>'||l_CAR_NUMBERS||'</value></field>'||
3699                     '<field name="'||'LIST_PRICE'||'"><value>'||car_rec.f_lprice||'</value></field>'||
3700                     '<field name="'||'EXTRAS_LATER'||'"><value>'||car_rec.f_oprice||'</value></field>'||
3701                     '<field name="'||'TOTAL_INIT_PRICE'||'"><value>'||car_rec.TOTAL_INIT_PRICE||'</value></field>'||
3702                     /*'<field name="'||'BEN_ST_DATE'||'"><value>'||l_BEN_ST_DATE||'</value></field>'||
3703                     '<field name="'||'BEN_ED_DATE'||'"><value>'||l_BEN_ED_DATE||'</value></field>'||
3704                     '<field name="'||'UNAVAILABLE'||'"><value>'||car_rec.unavailable||'</value></field>'||*/
3705                     '<field name="'||'CONTRIBUTIONS'||'"><value>'||car_rec.f_cost||'</value></field>'||
3706                     '<field name="'||'FINAL_PRICE'||'"><value>'||car_rec.FINAL_PRICE||'</value></field>'||
3707                     '<field name="'||'CO2'||'"><value>'||car_rec.co2||'</value></field>'||
3708                     '<field name="'||'FUEL'||'"><value>'||car_rec.f_fuel||'</value></field>'||
3709                     '<field name="'||'MOD_CO2'||'"><value>'||l_MOD_CO2||'</value></field>'||
3710                     '<field name="'||'PERCENT_1'||'"><value>'||l_PERCENT_1||'</value></field>'||
3711                     '<field name="'||'STD_DISC_1'||'"><value>'||l_STD_DISC_1||'</value></field>'||
3712                     '<field name="'||'EXTRA_CO2'||'"><value>'||l_EXTRA_CO2||'</value></field>'||
3713                     '<field name="'||'ROUND_NORMAL_CO2'||'"><value>'||l_ROUND_NORMAL_CO2||'</value></field>' ||
3714                     '<field name="'||'FULL_DISCOUNT'||'"><value>'||l_full_DISC||'</value></field>'||
3715                     '<field name="'||'CAR_BENEFIT_1'||'"><value>'||l_CAR_BENEFIT_1||'</value></field>'||
3716                     '<field name="'||'PERCENT_2'||'"><value>'||l_PERCENT_2||'</value></field>'||
3717                     '<field name="'||'STD_DISC_2'||'"><value>'||l_STD_DISC_2||'</value></field>'||
3718                     '<field name="'||'CAR_BENEFIT_2'||'"><value>'||l_CAR_BENEFIT_2||'</value></field>'||
3719                     '<field name="'||'ENGINE_CC'||'"><value>'||l_esize||'</value></field>'||
3720                     '<field name="'||'CAR_BENEFIT_3'||'"><value>'||l_CAR_BENEFIT_3||'</value></field>'||
3721                     '<field name="'||'CAR_BENEFIT_YEAR'||'"><value>'||car_rec.car_benefit_year||'</value></field>'||
3722                    '<field name="'||'BEN_ST_DATE1'||'"><value>'||substr(l_BEN_ST_DATE,0,2)||'</value></field>'||
3723                     '<field name="'||'BEN_ST_DATE2'||'"><value>'||substr(l_BEN_ST_DATE,4,2)||'</value></field>'||
3724                     '<field name="'||'BEN_ST_DATE3'||'"><value>'||substr(l_BEN_ST_DATE,7)||'</value></field>'||
3725                     '<field name="'||'BEN_ED_DATE1'||'"><value>'||substr(l_BEN_ED_DATE,0,2)||'</value></field>'||
3726                     '<field name="'||'BEN_ED_DATE2'||'"><value>'||substr(l_BEN_ED_DATE,4,2)||'</value></field>'||
3727                     '<field name="'||'BEN_ED_DATE3'||'"><value>'||substr(l_BEN_ED_DATE,7)||'</value></field>'||
3728                     '<field name="'||'UNAVAILABLE'||'"><value>'||car_rec.unavailable||'</value></field>'||
3729                     '<field name="'||'UNAVAILABLE_VAL_1'||'"><value>'||car_rec.unavailable_value||'</value></field>'||
3730                     '<field name="'||'CAR_BENEFIT_AVAILABLE'||'"><value>'||car_rec.CAR_BENEFIT_AVAILABLE||'</value></field>'||
3731                     '<field name="'||'PRIVATE_USE_PAYMENT'||'"><value>'||car_rec.f_amg||'</value></field>'||
3732                     '<field name="'||'CASH_EQUIVALENT_CAR'||'"><value>'||car_rec.f_cc||'</value></field>'||
3733                     '<field name="'||'FUEL_BENEFIT_YEAR'||'"><value>'||l_FUEL_BENEFIT_YEAR||'</value></field>'||
3734                     '<field name="'||'UNAVAILABLE_2'||'"><value>'||l_unavailble_days||'</value></field>'||
3735                     '<field name="'||'WITHDRAW_DATE1'||'"><value>'||substr(car_rec.f_withdraw,0,2)||'</value></field>'||
3736                     '<field name="'||'WITHDRAW_DATE2'||'"><value>'||substr(car_rec.f_withdraw,4,2)||'</value></field>'||
3737                     '<field name="'||'WITHDRAW_DATE3'||'"><value>'||substr(car_rec.f_withdraw,7)||'</value></field>'||
3738                     '<field name="'||'ADD_DAYS'||'"><value>'||l_add_days||'</value></field>'||
3739                     '<field name="'||'TOT_DAYS'||'"><value>'||l_tot_days||'</value></field>'||
3740                     '<field name="'||'UNAVIALABLE_VAL_2'||'"><value>'||car_rec.fuel_unavailable||'</value></field>'||
3741                     '<field name="'||'FUEL_BENEFIT_CHARGE'||'"><value>'||car_rec.f_fcc||'</value></field>'||
3742                     '</fields>  </xfdf>';
3743             end loop;
3744             end loop;
3745         else -- for RepRun 2007+
3746             for context_rec in csr_context_id(l_pactid,
3747                                               l_person_id,
3748                                               l_emp_ref_no,
3749                                               l_employer_name,
3750                                               'CAR AND CAR FUEL 2003_04') loop
3751             for car_rec in  csr_car_entries_0304(context_rec.action_context_id)
3752             loop
3753                 l_employee_number := get_assignment_number(context_rec.action_context_id);
3754                 l_loop_count := l_loop_count+1;
3755                 --Initialising the vars to null;
3756                 l_BEN_ST_DATE  := null;
3757                 l_BEN_ED_DATE  := null;
3758                 l_car_count    := null;
3759                 l_ONLY_CAR_FLAG :=null;
3760                 l_FUEL_FLAG :=null;
3761                 l_CAR_NUMBERS  := null;
3762                 l_MOD_CO2     := null;
3763                 l_PERCENT_1  := null;
3764                 l_BEN_1      := null;
3765                 l_PERCENT_2  := null;
3766                 l_BEN_2 := null;
3767                 l_PERCENT_3 := null;
3768                 l_BEN_3 := null;
3769                 l_PERCENT_4 := null;
3770                 l_BEN_4 := null;
3771                 l_BEN_5 := null;
3772                 l_STD_DISC_1 := null;
3773                 l_STD_DISC_2 := null;
3774                 l_STD_DISC_3 := null;
3775                 l_STD_DISC_4 := null;
3776                 l_STD_DISC_5 := null;
3777                 l_full_DISC  := null;
3778                 l_EXTRA_CO2  := null;
3779                 l_NORMAL_CO2  := null;
3780                 l_ROUND_NORMAL_CO2  := null;
3781                 l_CAR_BENEFIT_1  := null;
3782                 l_CAR_BENEFIT_2  := null;
3783                 l_CAR_BENEFIT_3  := null;
3784                 l_CAR_BENEFIT_4  := null;
3785                 l_CAR_BENEFIT_5  := null;
3786                 l_CAR_BENEFIT_6  := null;
3787                 l_esize          := null;
3788                 l_add_days       := null;
3789                 l_tot_days       := null;
3790                 l_unavailble_days := null;
3791                 l_FUEL_BENEFIT_YEAR := null;
3792                 /*l_full_DISC := car_rec.FULL_DISC;  */
3793                 if car_rec.FULL_YR_FLAG = 'Y'
3794                                 then
3795                     l_BEN_ST_DATE := null;
3796                     l_BEN_ED_DATE := null;
3797                 else
3798                     l_BEN_ST_DATE := car_rec.f_start;
3799                     l_BEN_ED_DATE := car_rec.f_end;
3800                 end if;
3801                 -- hr_utility.trace_on(null,'CAR');
3802                 hr_utility.trace('A4');
3803                 select action_information30
3804                 into   l_car_count
3805                 from   pay_action_information pai_emp
3806                 where  pai_emp.action_context_id = p_assignment_action_id
3807                 and    pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTA';
3808                 hr_utility.trace('A5');
3809                 if l_car_count = 1
3810                                 then
3811                     l_ONLY_CAR_FLAG := 'Y';
3812                 else
3813                     l_ONLY_CAR_FLAG := 'N';
3814                     l_CAR_NUMBERS := l_car_count;
3815                 end if   ;
3816                 hr_utility.trace('A6');
3817                 hr_utility.trace('date_registered ' ||car_rec.date_registered);
3818                 -- calculating benefit charge section
3819                 if (to_number(l_rep_run)) > 2008 then --P11D 08/09
3820                 if fnd_date.canonical_to_date( car_rec.date_registered)
3821                    >= to_date('01-01-1998','dd-mm-yyyy') and car_rec.CO2 is not null
3822                    and car_rec.CO2 > 120 -- P11D 08/09
3823 		   --hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_CO2_EMISSIONS',l_BEN_ED_DATE)
3824                 then
3825                     hr_utility.trace('a');
3826 									-- Bug 13618076: Co2 value should not be rounded for values between 121 to 124
3827 										if car_rec.CO2 > 125 then
3828 	                    l_MOD_CO2 := floor(car_rec.CO2/5) * 5;
3829 										else
3830 											l_MOD_CO2	:= car_rec.CO2;
3831 										end if;
3832                     --P11D 2010/11 Removed this check for Bug: 10432169
3833 		    /*
3834                      if l_MOD_CO2 < 130 then
3835                        l_MOD_CO2 := 130;
3836                     end if;
3837 		    */
3838                     --P11D 08/09
3839                     hr_utility.trace('b');
3840                     l_PERCENT_1 := car_rec.BENEFIT_PERCENT;
3841                     hr_utility.trace('c');
3842                     l_STD_DISC_1   := car_rec.STD_DISC;
3843                     l_ROUND_NORMAL_CO2 := car_rec.ROUND_NORMAL_CO2;
3844                     l_full_DISC        := car_rec.FULL_DISC;
3845                     l_CAR_BENEFIT_1    := car_rec.TOTAL_BENIFIT ;
3846                 -- P11D 08/09
3847 		/* bug 8277887 car_rec.CO2 is not null to nvl(car_rec.CO2,0) > 0 */
3848                 elsif fnd_date.canonical_to_date(car_rec.date_registered)
3849                    >= to_date('01-01-1998','dd-mm-yyyy') and nvl(car_rec.CO2,0) > 0
3850                    and car_rec.CO2 <= 120  then -- P11D 08/09
3851 		   -- hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_CO2_EMISSIONS',l_BEN_ED_DATE)
3852 		/* The below if condition will check for all the fuel types except for type 'D'   Bug:10432169 */
3853 		if  car_rec.f_fuel = 'B' OR car_rec.f_fuel = 'C' OR car_rec.f_fuel = 'G' OR
3854                          car_rec.f_fuel = 'H' OR car_rec.f_fuel = 'P' OR ( car_rec.f_fuel = 'L' AND  fnd_date.canonical_to_date(car_rec.date_registered)
3855                           < to_date('01-01-2006','dd-mm-yyyy')) then
3856 										          if car_rec.CO2 <= 75 then
3857           											l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_PERCENTAGE_LOWER',car_rec.full_end_date) ;
3858 															else
3859 																l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_PERCENTAGE',car_rec.full_end_date) ;
3860 														end if;
3861 							/* The below if condition checks for the fuel type L and date registered should be greater than 1-1-2006 Bug:10432169  */
3862 					elsif (car_rec.f_fuel = 'L' AND  fnd_date.canonical_to_date(car_rec.date_registered)
3863           	             >= to_date('01-01-2006','dd-mm-yyyy')) OR  car_rec.f_fuel = 'D' then
3864 															if car_rec.CO2 <= 75 then
3865 																l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_DIESEL_PERCENTAGE_LOWER',car_rec.full_end_date) ;
3866 															else
3867 																l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_DIESEL_PERCENTAGE',car_rec.full_end_date) ;
3868 															end if;
3869                       end if;
3870 
3871 
3872 
3873 
3874 IF (to_number(l_rep_run)) > 2011 THEN
3875 									if  car_rec.f_fuel = 'B' OR car_rec.f_fuel = 'C' OR car_rec.f_fuel = 'G' OR
3876                          car_rec.f_fuel = 'H' OR car_rec.f_fuel = 'P'  then
3877 										          if car_rec.CO2 <= 75 then
3878           											l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_PERCENTAGE_LOWER',car_rec.full_end_date) ;
3879 															else
3880 																l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_PERCENTAGE',car_rec.full_end_date) ;
3881 														end if;
3882 
3883 							        elsif car_rec.f_fuel = 'L' OR  car_rec.f_fuel = 'D' then
3884 															if car_rec.CO2 <= 75 then
3885 																l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_DIESEL_PERCENTAGE_LOWER',car_rec.full_end_date) ;
3886 															else
3887 																l_CAR_BENEFIT_6    := hr_gb_process_p11d_entries_pkg.get_global_value('P11D_QUALEC_DIESEL_PERCENTAGE',car_rec.full_end_date) ;
3888 															end if;
3889 									end if;
3890 END IF;
3891 
3892                 -- P11D 08/09
3893 		/* bug 8277887 changed car_rec.co2 is null to nvl(car_rec.CO2,0) = 0 */
3894                elsif fnd_date.canonical_to_date( car_rec.date_registered)
3895                       >= to_date('01-01-1998','dd-mm-yyyy') and nvl(car_rec.CO2,0) = 0
3896                 then
3897                    hr_utility.trace('e');
3898                    --l_PERCENT_2 := car_rec.BENEFIT_PERCENT; --chnage this as below for 6.1.4.2 P11D change
3899                    if car_rec.f_fuel = 'E' then
3900                        l_PERCENT_2 := 0;
3901                        l_STD_DISC_2   := 0;
3902                        l_CAR_BENEFIT_2 := 0;
3903                    else
3904                        l_STD_DISC_2   := car_rec.STD_DISC;
3905                        l_PERCENT_2 := car_rec.BENEFIT_PERCENT;
3906                        l_CAR_BENEFIT_2 := car_rec.TOTAL_BENIFIT_2;
3907                    end if;
3908                    hr_utility.trace('f');
3909 
3910 
3911                 elsif (fnd_date.canonical_to_date( car_rec.date_registered)<  to_date('01-01-1998','dd-mm-yyyy'))
3912                                 then
3913                     hr_utility.trace('g');
3914                     -- hr_utility.trace('car_rec.benefit_charge '|| car_rec.benefit_charge);
3915                     l_full_disc := null;
3916                     l_esize := car_rec.f_esize;
3917 	                    open csr_engine_discount(to_number(l_esize),
3918   	                                           fnd_date.canonical_to_date(car_rec.ben_end));
3919     	                fetch csr_engine_discount into l_CAR_BENEFIT_3;
3920       	              close csr_engine_discount;
3921 										end if;
3922 
3923 
3924                 else
3925 
3926 		if fnd_date.canonical_to_date( car_rec.date_registered)
3927                    >= to_date('01-01-1998','dd-mm-yyyy') and car_rec.CO2 is not null
3928                 then
3929                     hr_utility.trace('a');
3930                     l_MOD_CO2 := floor(car_rec.CO2/5) * 5;
3931                     hr_utility.trace('b');
3932                     l_PERCENT_1 := car_rec.BENEFIT_PERCENT;
3933                     hr_utility.trace('c');
3934                     l_STD_DISC_1   := car_rec.STD_DISC;
3935                     l_ROUND_NORMAL_CO2 := car_rec.ROUND_NORMAL_CO2;
3936                     l_full_DISC        := car_rec.FULL_DISC;
3937                     l_CAR_BENEFIT_1    := car_rec.TOTAL_BENIFIT ;
3938                 elsif fnd_date.canonical_to_date( car_rec.date_registered)
3939                       >= to_date('01-01-1998','dd-mm-yyyy') and car_rec.CO2 is null
3940                 then
3941                    hr_utility.trace('e');
3942                    --l_PERCENT_2 := car_rec.BENEFIT_PERCENT; -- below is the code snippet for P11D change 6.1.4.2
3943                    if car_rec.f_fuel = 'E' then
3944                        l_PERCENT_2 := 0;
3945                        l_STD_DISC_2   := 0;
3946                        l_CAR_BENEFIT_2 := 0;
3947                    else
3948                        l_STD_DISC_2   := car_rec.STD_DISC;
3949                        l_PERCENT_2 := car_rec.BENEFIT_PERCENT;
3950                        l_CAR_BENEFIT_2 := car_rec.TOTAL_BENIFIT_2;
3951                    end if;
3952                    hr_utility.trace('f');
3953 
3954 
3955                 elsif (fnd_date.canonical_to_date( car_rec.date_registered)<  to_date('01-01-1998','dd-mm-yyyy'))
3956                                 then
3957                     hr_utility.trace('g');
3958                     -- hr_utility.trace('car_rec.benefit_charge '|| car_rec.benefit_charge);
3959                     l_full_disc := null;
3960                     l_esize := car_rec.f_esize;
3961                     open csr_engine_discount(to_number(l_esize),
3962  	                                           fnd_date.canonical_to_date(car_rec.ben_end));
3963    	                fetch csr_engine_discount into l_CAR_BENEFIT_3;
3964      	              close csr_engine_discount;
3965                  end if;
3966 		end if;
3967 -- P11D 2012-13
3968 				IF (to_number(l_rep_run)) > 2011 THEN
3969 						if (car_rec.f_fuel = 'B' or car_rec.f_fuel = 'C' or
3970 												car_rec.f_fuel = 'H' or car_rec.f_fuel = 'G' or
3971 												car_rec.f_fuel = 'P' ) then
3972 						l_fuel_new :='A';
3973 						elsif (car_rec.f_fuel = 'L' or (car_rec.f_fuel = 'D' )) then
3974 						l_fuel_new :='D';
3975 						else
3976 						l_fuel_new := car_rec.f_fuel;
3977 						end if;
3978 				ELSE
3979 
3980 						l_fuel_new := car_rec.f_fuel;
3981 				END IF;
3982 
3983 
3984                 hr_utility.trace('h');
3985                 -- fuel benefit
3986                 if car_rec.FUEL_BENEFIT = 'Y'
3987                                 then
3988                     L_FUEL_BENEFIT_YEAR := car_rec.FUEL_BENEFIT_YEAR;
3989                     l_add_days          := car_rec.additional_days;
3990                     l_tot_days        := car_rec.total_days;
3991                     l_unavailble_days   := car_rec.unavailable;
3992                 end if;
3993                 hr_utility.trace('i');
3994                 l_xfdf_str_tab.extend;
3995                 l_xfdf_str_tab(l_loop_count) := '<?xml version = "1.0" encoding = "UTF-8"?>
3996                     <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
3997                     <fields> ' ||
3998                     '<field name="'||'EMPLOYERS_NAME'||'"><value>'||replace(l_employer_name,'&','&')       ||'</value></field>'||
3999                     '<field name="'||'FULL_NAME'||'"><value>'||l_full_name||'</value></field>'||
4000                     -- P11D 08/09
4001                     ' <field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
4002                     ' <field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
4003                     -- P11D 08/09
4004                     '<field name="'||'EMPLOYERS_REF_NO'||'"><value>'||l_emp_ref_no||'</value></field>'||
4005                     '<field name="'||'EMPLOYEE_NUMBER'||'"><value>'||l_employee_number||'</value></field>'||
4006                     -- '<field name="'||'NATIONAL_INS_NO'||'"><value>'||l_national_ins_no||'</value></field>'||
4007                     '<field name="'||'NI_1'||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
4008                     '<field name="'||'NI_2'||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
4009                     '<field name="'||'NI_3'||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
4010                     '<field name="'||'NI_4'||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
4011                     '<field name="'||'NI_5'||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
4012                     '<field name="'||'NI_6'||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
4013                     '<field name="'||'NI_7'||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
4014                     '<field name="'||'NI_8'||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
4015                     '<field name="'||'NI_9'||'"><value>' ||substr(l_national_ins_no,9) || '</value></field>  '   ||
4016                     '<field name="'||'MAKE'||'"><value>'||car_rec.make||'</value></field>'||
4017                     '<field name="'||'MODEL'||'"><value>'||car_rec.model||'</value></field>'||
4018                     -- P11D changes 07/08 added make_model field
4019                     '<field name="'||'MAKE_MODEL'||'"><value>'||car_rec.make||' '||car_rec.model||'</value></field>'||
4020                     '<field name="'||'DATE_FIRST_REG1'||'"><value>'||substr(car_rec.f_dreg,0,2)||'</value></field>'||
4021                     '<field name="'||'DATE_FIRST_REG2'||'"><value>'||substr(car_rec.f_dreg,4,2)||'</value></field>'||
4022                     '<field name="'||'DATE_FIRST_REG3'||'"><value>'||substr(car_rec.f_dreg,7)||'</value></field>'||
4023                     '<field name="'||'ONLY_CAR_FLAG'||'"><value>'||l_ONLY_CAR_FLAG||'</value></field>'||
4024                     '<field name="'||'ONLY_CAR_FLAG_Y'||'"><value>'||l_ONLY_CAR_FLAG||'</value></field>'||
4025                     '<field name="'||'ONLY_CAR_FLAG_N'||'"><value>'||l_ONLY_CAR_FLAG||'</value></field>'||
4026                     --'<field name="'||'CO2'||'"><value>'||car_rec.CO2||'</value></field>'||
4027                     --'<field name="'||'FULL_YR_FLAG'||'"><value>'||car_rec.FULL_YR_FLAG||'</value></field>'||
4028                     '<field name="'||'CAR_NUMBERS'||'"><value>'||l_CAR_NUMBERS||'</value></field>'||
4029                     '<field name="'||'LIST_PRICE'||'"><value>'||car_rec.f_lprice||'</value></field>'||
4030                     '<field name="'||'EXTRAS_LATER'||'"><value>'||car_rec.f_oprice||'</value></field>'||
4031                     '<field name="'||'TOTAL_INIT_PRICE'||'"><value>'||car_rec.TOTAL_INIT_PRICE||'</value></field>'||
4032                     '<field name="'||'CONTRIBUTIONS'||'"><value>'||car_rec.f_cost||'</value></field>'||
4033                     '<field name="'||'FINAL_PRICE'||'"><value>'||car_rec.FINAL_PRICE||'</value></field>'||
4034                     '<field name="'||'CO2'||'"><value>'||car_rec.co2||'</value></field>'||
4035                     '<field name="'||'FUEL'||'"><value>'||l_fuel_new||'</value></field>'||
4036                     '<field name="'||'MOD_CO2'||'"><value>'||l_MOD_CO2||'</value></field>'||
4037                     '<field name="'||'PERCENT_1'||'"><value>'||l_PERCENT_1||'</value></field>'||
4038                     '<field name="'||'STD_DISC_1'||'"><value>'||l_STD_DISC_1||'</value></field>'||
4039                     '<field name="'||'EXTRA_CO2'||'"><value>'||l_EXTRA_CO2||'</value></field>'||
4040                     '<field name="'||'ROUND_NORMAL_CO2'||'"><value>'||l_ROUND_NORMAL_CO2||'</value></field>' ||
4041                     '<field name="'||'FULL_DISCOUNT'||'"><value>'||l_full_DISC||'</value></field>'||
4042                     '<field name="'||'CAR_BENEFIT_1'||'"><value>'||l_CAR_BENEFIT_1||'</value></field>'||
4043                     -- P11D 08/09
4044                     '<field name="'||'CAR_BENEFIT_6'||'"><value>'||l_CAR_BENEFIT_6||'</value></field>'||
4045                     -- P11D 08/09
4046                     '<field name="'||'PERCENT_2'||'"><value>'||l_PERCENT_2||'</value></field>'||
4047                     '<field name="'||'STD_DISC_2'||'"><value>'||l_STD_DISC_2||'</value></field>'||
4048                     '<field name="'||'CAR_BENEFIT_2'||'"><value>'||l_CAR_BENEFIT_2||'</value></field>'||
4049                     '<field name="'||'ENGINE_CC'||'"><value>'||l_esize||'</value></field>'||
4050                     '<field name="'||'CAR_BENEFIT_3'||'"><value>'||l_CAR_BENEFIT_3||'</value></field>'||
4051                     '<field name="'||'CAR_BENEFIT_YEAR'||'"><value>'||car_rec.car_benefit_year||'</value></field>'||
4052                    '<field name="'||'BEN_ST_DATE1'||'"><value>'||substr(l_BEN_ST_DATE,0,2)||'</value></field>'||
4053                     '<field name="'||'BEN_ST_DATE2'||'"><value>'||substr(l_BEN_ST_DATE,4,2)||'</value></field>'||
4054                     '<field name="'||'BEN_ST_DATE3'||'"><value>'||substr(l_BEN_ST_DATE,7)||'</value></field>'||
4055                     '<field name="'||'BEN_ED_DATE1'||'"><value>'||substr(l_BEN_ED_DATE,0,2)||'</value></field>'||
4056                     '<field name="'||'BEN_ED_DATE2'||'"><value>'||substr(l_BEN_ED_DATE,4,2)||'</value></field>'||
4057                     '<field name="'||'BEN_ED_DATE3'||'"><value>'||substr(l_BEN_ED_DATE,7)||'</value></field>'||
4058                     '<field name="'||'UNAVAILABLE'||'"><value>'||car_rec.unavailable||'</value></field>'||
4059                     '<field name="'||'UNAVAILABLE_VAL_1'||'"><value>'||car_rec.unavailable_value||'</value></field>'||
4060                     '<field name="'||'CAR_BENEFIT_AVAILABLE'||'"><value>'||car_rec.CAR_BENEFIT_AVAILABLE||'</value></field>'||
4061                     '<field name="'||'PRIVATE_USE_PAYMENT'||'"><value>'||car_rec.f_amg||'</value></field>'||
4062                     '<field name="'||'CASH_EQUIVALENT_CAR'||'"><value>'||car_rec.f_cc||'</value></field>'||
4063                     '<field name="'||'FUEL_BENEFIT_YEAR'||'"><value>'||l_FUEL_BENEFIT_YEAR||'</value></field>'||
4064                     '<field name="'||'UNAVAILABLE_2'||'"><value>'||l_unavailble_days||'</value></field>'||
4065                     '<field name="'||'WITHDRAW_DATE1'||'"><value>'||substr(car_rec.f_withdraw,0,2)||'</value></field>'||
4066                     '<field name="'||'WITHDRAW_DATE2'||'"><value>'||substr(car_rec.f_withdraw,4,2)||'</value></field>'||
4067                     '<field name="'||'WITHDRAW_DATE3'||'"><value>'||substr(car_rec.f_withdraw,7)||'</value></field>'||
4068                     '<field name="'||'ADD_DAYS'||'"><value>'||l_add_days||'</value></field>'||
4069                     '<field name="'||'TOT_DAYS'||'"><value>'||l_tot_days||'</value></field>'||
4070                     '<field name="'||'UNAVIALABLE_VAL_2'||'"><value>'||car_rec.fuel_unavailable||'</value></field>'||
4071                     '<field name="'||'FUEL_BENEFIT_CHARGE'||'"><value>'||car_rec.f_fcc||'</value></field>'||
4072                     '</fields>  </xfdf>';
4073             end loop;
4074             end loop;
4075         end if;
4076         open l_ret_ref_cursor for
4077         select *
4078         from table (cast(l_xfdf_str_tab as per_gb_xfdftableType));
4079         p_record_num := l_loop_count;
4080         return l_ret_ref_cursor;
4081    end ;
4082 
4083    function fetch_ws3_ref_cursor (p_assignment_action_id Number,
4084                                   p_record_num out nocopy NUmber) return ref_cursor_typ
4085    is
4086         -- per_gb_xfdftableType is explicitly created type
4087         -- only modification needed is it could be of type blob
4088         -- i tried thta but could not access the blob valus in java routine.
4089         -- can be sorted later
4090         l_xfdf_str_tab per_gb_xfdftableType := per_gb_xfdftableType( );
4091         l_xfdf_str varchar2(32000);
4092         l_ret_ref_cursor ref_cursor_typ;
4093 
4094         cursor csr_vans_entries(p_pactid Number,
4095                                 p_person_id Number,
4096                                 p_emp_ref Varchar2 ,
4097                                 p_emp_name Varchar2)
4098         is
4099         -- formatting in the ws is handled by the
4100         -- format feature in pdf. xdo is now supporting it
4101         -- but has limittaion where the fields are repating
4102         -- and hence the format clause is used for STD_UNAVILA_VAL
4103         select decode(ACTION_INFORMATION6, null,'Y','Y','N','N','Y') exclusive_flag,
4104                ACTION_INFORMATION7 dreg,
4105                nvl(ACTION_INFORMATION18,500) standard_charge,
4106                nvl(ACTION_INFORMATION8,0)  UNAVAILABLE_1,
4107                nvl(ACTION_INFORMATION9,0)  UNAVAILABLE_2,
4108                nvl(ACTION_INFORMATION10,0)  UNAVAILABLE_3,
4109                nvl(ACTION_INFORMATION11,0)  UNAVAILABLE_4,
4110                nvl(ACTION_INFORMATION12,0) UNAVAILABLE_VAL,
4111                nvl(ACTION_INFORMATION13,0) NUM_SHARE,
4112                nvl(ACTION_INFORMATION14,0) PVT_USE_PAYMENT,
4113                nvl(ACTION_INFORMATION15,0) CASH_EQUIVALENT
4114         from  pay_action_information
4115         where action_information_category = 'VANS 2002_03'
4116         and   action_context_id in (select paa.assignment_action_id
4117                                     from   pay_action_information pai_comp,
4118                                            pay_action_information pai_person,
4119                                            pay_assignment_actions paa,
4120                                            pay_payroll_actions ppa
4121                                     where  ppa.payroll_action_id = p_pactid
4122                                     and    paa.payroll_action_id = ppa.payroll_action_id
4123                                     and    pai_comp.action_context_id = paa.assignment_action_id
4124                                     and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
4125                                     and    pai_person.action_context_id = paa.assignment_action_id
4126                                     and    pai_person.action_information_category = 'ADDRESS DETAILS'
4127                                     and    pai_person.action_information14 = 'Employee Address'
4128                                     and    pai_person.action_information1 = to_char(p_person_id)
4129                                     and    pai_comp.action_information6 = p_emp_ref
4130                                     and    pai_comp.action_information7 = p_emp_name);
4131 
4132         cursor csr_vans_entries_05(p_pactid Number,
4133                                    p_person_id Number,
4134                                    p_emp_ref Varchar2 ,
4135                                    p_emp_name Varchar2)
4136         is
4137         select nvl(action_information5,' ') registration_number,
4138                action_information6          date_registered,
4139                -- count vans --
4140                -- number of worksheet --
4141                nvl(action_information24,0)  van_charged,  -- A
4142                action_information3          van_from_b,   --
4143                action_information4          van_to_b,     --
4144                action_information7          van_unavil_b, -- B
4145                action_information16         van_from_c,   --
4146                action_information17         van_to_c,     --
4147                action_information18         van_unavil_c, -- C
4148                action_information19         van_from_d,   --
4149                action_information20         van_to_d,     --
4150                action_information21         van_unavil_d, -- D
4151                nvl(action_information8,0)   van_tot_day_unavil, -- E
4152                nvl(action_information9,0)   van_unavil_value,   -- F
4153                nvl(action_information10,0)  van_reduce_value,   -- G
4154                nvl(action_information11,0)  van_sh_pcent_reduc, -- H
4155                nvl(action_information12,0)  van_sh_reduction,   -- J
4156                action_information13         van_explanation,    --
4157                nvl(action_information10,0) -
4158                nvl(action_information12,0)  van_reduce_share,   -- K
4159                nvl(action_information14,0)  van_private_uses,   -- L
4160                nvl(action_information15,0)  van_benefit_charge, -- M
4161                action_context_id
4162         from   pay_action_information
4163         where  action_information_category = 'VANS 2005'
4164         and    action_context_type = 'AAP'
4165         and    action_context_id in (select paa.assignment_action_id
4166                                     from   pay_action_information pai_comp,
4167                                            pay_action_information pai_person,
4168                                            pay_assignment_actions paa,
4169                                            pay_payroll_actions ppa
4170                                     where  ppa.payroll_action_id = p_pactid
4171                                     and    paa.payroll_action_id = ppa.payroll_action_id
4172                                     and    pai_comp.action_context_id = paa.assignment_action_id
4173                                     and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
4174                                     and    pai_comp.action_context_type = 'AAP'
4175                                     and    pai_person.action_context_id = paa.assignment_action_id
4176                                     and    pai_person.action_information_category = 'ADDRESS DETAILS'
4177                                     and    pai_person.action_context_type = 'AAP'
4178                                     and    pai_person.action_information14 = 'Employee Address'
4179                                     and    pai_person.action_information1 = to_char(p_person_id)
4180                                     and    pai_comp.action_information6 = p_emp_ref
4181                                     and    pai_comp.action_information7 = p_emp_name);
4182 
4183         cursor csr_vans_05_count(p_pactid Number,
4184                                  p_person_id Number,
4185                                  p_emp_ref Varchar2 ,
4186                                  p_emp_name Varchar2)
4187         is
4188         select count(*)
4189         from   pay_action_information
4190         where  action_information_category = 'VANS 2005'
4191         and    action_context_type = 'AAP'
4192         and    action_context_id in (select paa.assignment_action_id
4193                                     from   pay_action_information pai_comp,
4194                                            pay_action_information pai_person,
4195                                            pay_assignment_actions paa,
4196                                            pay_payroll_actions ppa
4197                                     where  ppa.payroll_action_id = p_pactid
4198                                     and    paa.payroll_action_id = ppa.payroll_action_id
4199                                     and    pai_comp.action_context_id = paa.assignment_action_id
4200                                     and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
4201                                     and    pai_comp.action_context_type = 'AAP'
4202                                     and    pai_person.action_context_id = paa.assignment_action_id
4203                                     and    pai_person.action_information_category = 'ADDRESS DETAILS'
4204                                     and    pai_person.action_context_type = 'AAP'
4205                                     and    pai_person.action_information14 = 'Employee Address'
4206                                     and    pai_person.action_information1 = to_char(p_person_id)
4207                                     and    pai_comp.action_information6 = p_emp_ref
4208                                     and    pai_comp.action_information7 = p_emp_name);
4209 
4210         -- P11D Changes 07/08
4211         -- Cursor to fetch the VANS 2007 element details
4212         cursor csr_vans_entries_07(p_pactid Number,
4213                                    p_person_id Number,
4214                                    p_emp_ref Varchar2 ,
4215                                    p_emp_name Varchar2)
4216         is
4217         select nvl(action_information5,' ') registration_number,
4218                -- count vans --
4219                -- number of worksheet --
4220                nvl(action_information24,0)  van_charged,  -- A
4221                action_information3          van_from_b,   --
4222                action_information4          van_to_b,     --
4223                action_information6          van_unavil_b, -- B
4224                action_information15         van_from_c,   --
4225                action_information16         van_to_c,     --
4226                action_information17         van_unavil_c, -- C
4227                action_information18         van_from_d,   --
4228                action_information19         van_to_d,     --
4229                action_information20         van_unavil_d, -- D
4230                nvl(action_information7,0)   van_tot_day_unavil, -- E
4231                nvl(action_information8,0)   van_unavil_value,   -- F
4232                nvl(action_information9,0)  van_reduce_value,   -- G
4233                nvl(action_information10,0)  van_sh_pcent_reduc, -- H
4234                nvl(action_information11,0)  van_sh_reduction,   -- J
4235                action_information12         van_explanation,    --
4236                nvl(action_information9,0) -
4237                nvl(action_information11,0)  van_reduce_share,   -- K
4238                nvl(action_information13,0)  van_private_uses,   -- L
4239                nvl(action_information14,0)  van_benefit_charge, -- M
4240                nvl(action_information24,0)  van_benefit_chare_tax, --P
4241                action_information25         van_fuel_withdrawn, --R date
4242                nvl(action_information26,0)  van_days_after_fuel_wd, --R
4243                nvl(action_information27,0)  van_total_days_no_fuel, -- S
4244                nvl(action_information28,0)  van_reduction, --T
4245                (nvl(action_information24,0)
4246                - nvl(action_information28,0)) van_fuel_charge_reduction, --V
4247                nvl(action_information29,0)    van_reduction_sharing, --W
4248                nvl(action_information30,0)    van_feul_ben_charge, --X
4249                action_context_id
4250         from   pay_action_information
4251         where  action_information_category = 'VANS 2007'
4252         and    action_context_type = 'AAP'
4253         and    action_context_id in (select paa.assignment_action_id
4254                                     from   pay_action_information pai_comp,
4255                                            pay_action_information pai_person,
4256                                            pay_assignment_actions paa,
4257                                            pay_payroll_actions ppa
4258                                     where  ppa.payroll_action_id = p_pactid
4259                                     and    paa.payroll_action_id = ppa.payroll_action_id
4260                                     and    pai_comp.action_context_id = paa.assignment_action_id
4261                                     and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
4262                                     and    pai_comp.action_context_type = 'AAP'
4263                                     and    pai_person.action_context_id = paa.assignment_action_id
4264                                     and    pai_person.action_information_category = 'ADDRESS DETAILS'
4265                                     and    pai_person.action_context_type = 'AAP'
4266                                     and    pai_person.action_information14 = 'Employee Address'
4267                                     and    pai_person.action_information1 = to_char(p_person_id)
4268                                     and    pai_comp.action_information6 = p_emp_ref
4269                                     and    pai_comp.action_information7 = p_emp_name);
4270         -- P11D Changes 07/08
4271         -- Cursor to count the VANS 2007 element details
4272         cursor csr_vans_07_count(p_pactid Number,
4273                                  p_person_id Number,
4274                                  p_emp_ref Varchar2 ,
4275                                  p_emp_name Varchar2)
4276         is
4277         select count(*)
4278         from   pay_action_information
4279         where  action_information_category = 'VANS 2007'
4280         and    action_context_type = 'AAP'
4281         and    action_context_id in (select paa.assignment_action_id
4282                                     from   pay_action_information pai_comp,
4283                                            pay_action_information pai_person,
4284                                            pay_assignment_actions paa,
4285                                            pay_payroll_actions ppa
4286                                     where  ppa.payroll_action_id = p_pactid
4287                                     and    paa.payroll_action_id = ppa.payroll_action_id
4288                                     and    pai_comp.action_context_id = paa.assignment_action_id
4289                                     and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
4290                                     and    pai_comp.action_context_type = 'AAP'
4291                                     and    pai_person.action_context_id = paa.assignment_action_id
4292                                     and    pai_person.action_information_category = 'ADDRESS DETAILS'
4293                                     and    pai_person.action_context_type = 'AAP'
4294                                     and    pai_person.action_information14 = 'Employee Address'
4295                                     and    pai_person.action_information1 = to_char(p_person_id)
4296                                     and    pai_comp.action_information6 = p_emp_ref
4297                                     and    pai_comp.action_information7 = p_emp_name);
4298 
4299 
4300         l_emp_ref_no varchar2(150);
4301         l_employer_name varchar2(150);
4302         l_full_name varchar2(150);
4303         l_sur_name varchar2(150); --  P11D 08/09
4304         l_fore_name varchar2(150); -- P11D 08/09
4305         l_national_ins_no varchar2(150);
4306         l_employee_number varchar2(150);
4307         l_pactid number;
4308         l_person_id number;
4309         l_rep_run varchar2(10);
4310         l_loop_count number;
4311         l_vans_count  number;
4312         l_only_van   varchar2(2);
4313         l_after_flag varchar2(2);
4314         l_date_from  varchar2(15);
4315         l_date_to    varchar2(15);
4316         l_days_after_fuel_wd varchar2(30);
4317         l_fuel_charge_reduction varchar2(30);
4318    begin
4319         get_employer_details(p_assignment_action_id,
4320                              l_emp_ref_no,
4321                              l_employer_name);
4322         get_employee_details(p_assignment_action_id,
4323                              l_full_name,
4324                              l_national_ins_no,
4325                              l_employee_number);
4326         -- P11D 08/09
4327         -- Fetch sur and fore names
4328         get_sur_fore_names(p_assignment_action_id,
4329                            l_sur_name,
4330                            l_fore_name);
4331         -- P11D 08/09
4332         l_pactid := get_pactid(p_assignment_action_id);
4333         l_person_id := get_person_id (p_assignment_action_id );
4334         l_loop_count := 0;
4335 
4336         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
4337          p_payroll_action_id           => l_pactid,
4338          p_token_name                  => 'Rep_Run',
4339          p_token_value                 => l_rep_run);
4340 
4341         l_date_from := '06-04-' || to_char(to_number(l_rep_run) - 1 );
4342         l_date_to   := '05-04-' || l_rep_run;
4343 
4344         if to_number(l_rep_run) < '2006'
4345         then
4346             for van_entries in  csr_vans_entries(l_pactid,
4347                                                  l_person_id,
4348                                                  l_emp_ref_no,
4349                                                  l_employer_name)
4350             loop
4351                 l_loop_count := l_loop_count+1;
4352                 hr_utility.trace('a');
4353                 -- l_ws3_info_tab(l_loop_count).l_vans_entries := van_entries;
4354                 l_xfdf_str_tab.extend;
4355                 l_xfdf_str_tab(l_loop_count) := '<?xml version = "1.0" encoding = "UTF-8"?>
4356                     <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
4357                     <fields> ' ||
4358                     '<field name="'||'Employer'||'"><value>' ||replace(l_employer_name,'&','&')  || '</value></field> ' ||
4359                     '<field name="'||'Employee'||'"><value>' ||l_full_name || '</value></field>  ' ||
4360                     '<field name="'||'PAYE_tax'||'"><value>' ||l_emp_ref_no || '</value></field>  ' ||
4361                     '<field name="'||'Works_no'||'"><value>' ||l_employee_number || '</value></field>  ' ||
4362                     -- ' <field name="'||'Nat_Ins_Num'||'"><value>' ||l_national_ins_no || '</value></field>  '  ||
4363                     '<field name="'||'NI_1'||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
4364                     '<field name="'||'NI_2'||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
4365                     '<field name="'||'NI_3'||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
4366                     '<field name="'||'NI_4'||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
4367                     '<field name="'||'NI_5'||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
4368                     '<field name="'||'NI_6'||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
4369                     '<field name="'||'NI_7'||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
4370                     '<field name="'||'NI_8'||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
4371                     '<field name="'||'NI_9'||'"><value>' ||substr(l_national_ins_no,9)   || '</value></field>  '  ||
4372                     '<field name="'||'exclusive_flag'||'"><value>' ||van_entries.exclusive_flag || '</value></field>  '  ;
4373                      hr_utility.trace('aa');
4374                 if van_entries.exclusive_flag = 'Y'
4375                 then -- we fill section 1
4376                     if fnd_date.canonical_to_date(van_entries.dreg) > to_date('05-04-2000','dd-mm-yyyy')
4377                     then
4378                         l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count) ||
4379                         '<field name="'||'date_flag'||'"><value>' ||'Y' || '</value></field>  '  ;
4380                     else
4381                         l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4382                         '<field name="'||'date_flag'||'"><value>' ||'N' || '</value></field>  '  ;
4383                     end if;
4384                     hr_utility.trace('2');
4385                     l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4386                         '<field name="'||'standard_charge'||'"><value>' ||van_entries.standard_charge || '</value></field>  '  ||
4387                         '<field name="'||'UNAVAILABLE_1'  ||'"><value>' ||van_entries.UNAVAILABLE_1   || '</value></field>  '  ||
4388                         '<field name="'||'UNAVAILABLE_2'  ||'"><value>' ||van_entries.UNAVAILABLE_2   || '</value></field>  '  ||
4389                         '<field name="'||'UNAVAILABLE_3'  ||'"><value>' ||van_entries.UNAVAILABLE_3   || '</value></field>  '  ||
4390                         '<field name="'||'UNAVAILABLE_4'  ||'"><value>' ||van_entries.UNAVAILABLE_4   || '</value></field>  '  ||
4391                         '<field name="'||'TOT_UNAVAILABLE'||'"><value>' ||(nvl(van_entries.UNAVAILABLE_1,0) + nvl(van_entries.UNAVAILABLE_2,0) +
4392                         nvl(van_entries.UNAVAILABLE_3,0) + nvl(van_entries.UNAVAILABLE_4,0))  || '</value></field>  '  ||
4393                         '<field name="'||'UNAVAILABLE_VAL'||'"><value>' ||van_entries.UNAVAILABLE_VAL || '</value></field>  '  ||
4394                         '<field name="'||'STD_RED'        ||'"><value>' || (nvl(van_entries.standard_charge,0) - nvl(van_entries.UNAVAILABLE_VAL,0)) || '</value></field>  ' ||
4395                         '<field name="'||'PVT_USE_PAYMENT'||'"><value>' ||van_entries.PVT_USE_PAYMENT || '</value></field>  '  ||
4396                         '<field name="'||'NON_SHARED_BEN' ||'"><value>' ||van_entries.CASH_EQUIVALENT || '</value></field>  '  ||
4397                         '<field name="'||'CE'             ||'"><value>' ||van_entries.CASH_EQUIVALENT || '</value></field>  '  ;
4398                     hr_utility.trace('3');
4399                 else -- else of van_entries.exclusive_flag = 'Y'
4400                     hr_utility.trace('4');
4401                     if fnd_date.canonical_to_date(van_entries.dreg) > to_date('05-04-2000','dd-mm-yyyy')
4402                     then
4403                         l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4404                         '<field name="'||'S_DATE_FLAG'||'"><value>' ||'Y' || '</value></field>  '  ;
4405                     else
4406                         l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4407                         '<field name="'||'S_DATE_FLAG'||'"><value>' ||'N' || '</value></field>  '  ;
4408                     end if;
4409                     l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4410                     '<field name="'||'S_STANDARD_CHARGE'||'"><value>' ||van_entries.standard_charge || '</value></field>  '  ;
4411                     hr_utility.trace('5');
4412                     if nvl(van_entries.UNAVAILABLE_VAL,0) = 0
4413                     then
4414                         l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4415                         '<field name="'||'S_FULL_YEAR_FLAG'||'"><value>' ||'Y' || '</value></field>  '  ;
4416                         hr_utility.trace('7');
4417                     else
4418                         hr_utility.trace('6');
4419                         l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4420                         '<field name="'||'S_FULL_YEAR_FLAG' ||'"><value>' ||'N'                       || '</value></field>  '  ||
4421                         '<field name="'||'S_UNAVAILABLE_1'  ||'"><value>' ||van_entries.UNAVAILABLE_1 || '</value></field>  '  ||
4422                         '<field name="'||'S_UNAVAILABLE_2'  ||'"><value>' ||van_entries.UNAVAILABLE_2 || '</value></field>  '  ||
4423                         '<field name="'||'S_UNAVAILABLE_3'  ||'"><value>' ||van_entries.UNAVAILABLE_3 || '</value></field>  '  ||
4424                         '<field name="'||'S_UNAVAILABLE_4'  ||'"><value>' ||van_entries.UNAVAILABLE_4 || '</value></field>  '  ||
4425                         '<field name="'||'S_TOT_UNAVAILABLE'||'"><value>' ||(nvl(van_entries.UNAVAILABLE_1,0) +
4426                         nvl(van_entries.UNAVAILABLE_2,0) + nvl(van_entries.UNAVAILABLE_3,0) + nvl(van_entries.UNAVAILABLE_4,0))  || '</value></field>  ' ||
4427                         '<field name="'||'S_UNAVAIL_VAL'    ||'"><value>' ||van_entries.UNAVAILABLE_VAL || '</value></field>  '  ;
4428                     end if;
4429                     hr_utility.trace('8');
4430                     l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  ||
4431                     '<field name="'||'S_STD_UNAVAIL'    ||'"><value>' ||
4432                     to_char(to_number((nvl(van_entries.standard_charge,0) - nvl(van_entries.UNAVAILABLE_VAL,0))),
4433                                     'FM999,999,990.00') || '</value></field>  '  ||
4434                     '<field name="'||'S_NUM_SHARE'      ||'"><value>' || van_entries.NUM_SHARE  || '</value></field>  '  ||
4435                     '<field name="'||'S_CHRG_PER_SHARE' ||'"><value>' ||
4436                     ((nvl(van_entries.standard_charge,0) - nvl(van_entries.UNAVAILABLE_VAL,0))/van_entries.NUM_SHARE)
4437                                    || '</value></field>  ' ||
4438                     '<field name="'||'S_STD_CHARGE'||'"><value>' ||
4439                     ((nvl(van_entries.standard_charge,0) - nvl(van_entries.UNAVAILABLE_VAL,0))/van_entries.NUM_SHARE)
4440                                    || '</value></field>  '  ||
4441                     '<field name="'||'S_PVT_USE_PAYMENT'||'"><value>' ||van_entries.PVT_USE_PAYMENT || '</value></field>  '  ||
4442                     '<field name="'||'SHARED_BEN'       ||'"><value>' ||van_entries.CASH_EQUIVALENT || '</value></field>  '  ||
4443                     '<field name="'||'CE'               ||'"><value>' ||van_entries.CASH_EQUIVALENT || '</value></field>  '  ;
4444                 end if; -- end of checking for van_entries.exclusive_flag = 'Y'
4445                 hr_utility.trace('9');
4446                 l_xfdf_str_tab(l_loop_count) := l_xfdf_str_tab(l_loop_count)  || '</fields>  </xfdf>';
4447             end loop;
4448         elsif to_number(l_rep_run) >= 2006 and to_number(l_rep_run) < 2008 then -- P11D changes 07/08 -- l_rep_run > 2005, so 2006 onwards.
4449             open csr_vans_05_count(l_pactid,
4450                                    l_person_id,
4451                                    l_emp_ref_no,
4452                                    l_employer_name);
4453             fetch csr_vans_05_count into l_vans_count;
4454             close csr_vans_05_count;
4455 
4456             --l_only_van := 'Y';
4457             if l_vans_count > 1 then
4458                l_only_van := 'N';
4459             else
4460                l_vans_count := null;
4461                l_only_van := 'Y';
4462             end if;
4463 
4464             for vans_entries in  csr_vans_entries_05(l_pactid,
4465                                                      l_person_id,
4466                                                      l_emp_ref_no,
4467                                                      l_employer_name)
4468             loop
4469                 l_loop_count := l_loop_count+1;
4470                 hr_utility.trace('Vans');
4471                 l_after_flag := 'N';
4472                 if to_number(l_rep_run) < 2007 then
4473                    if fnd_date.canonical_to_date(vans_entries.date_registered) > to_date('05-04-2002','dd-mm-yyyy')
4474                    then
4475                        l_after_flag := 'Y';
4476                    end if;
4477                 else -- >= 2007
4478                    if fnd_date.canonical_to_date(vans_entries.date_registered) > to_date('05-04-2003','dd-mm-yyyy')
4479                    then
4480                        l_after_flag := 'Y';
4481                    end if;
4482                 end if;
4483 
4484                 /* check date from and date to */
4485                 if fnd_date.canonical_to_date(vans_entries.van_from_b) = to_date(l_date_from,'DD-MM-YYYY') and
4486                    fnd_date.canonical_to_date(vans_entries.van_to_b)  = to_date(l_date_to,'DD-MM-YYYY')
4487                 then
4488                     vans_entries.van_from_b := null;
4489                     vans_entries.van_to_b   := null;
4490                 end if;
4491                 /* end check  */
4492                 l_employee_number := get_assignment_number(vans_entries.action_context_id);
4493 
4494                 l_xfdf_str_tab.extend;
4495                 l_xfdf_str_tab(l_loop_count) := '<?xml version = "1.0" encoding = "UTF-8"?>
4496                     <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
4497                     <fields> ' ||
4498                     '<field name="'||'Employer'   ||'"><value>' ||replace(l_employer_name,'&','&')      || '</value></field>  ' ||
4499                     '<field name="'||'Employee'   ||'"><value>' ||l_full_name                               || '</value></field>  ' ||
4500                     '<field name="'||'PAYE_tax'   ||'"><value>' ||l_emp_ref_no                              || '</value></field>  ' ||
4501                     '<field name="'||'Works_no'   ||'"><value>' ||l_employee_number                         || '</value></field>  ' ||
4502                     '<field name="'||'NI_1'       ||'"><value>' ||substr(l_national_ins_no,1,1)             || '</value></field>  ' ||
4503                     '<field name="'||'NI_2'       ||'"><value>' ||substr(l_national_ins_no,2,1)             || '</value></field>  ' ||
4504                     '<field name="'||'NI_3'       ||'"><value>' ||substr(l_national_ins_no,3,1)             || '</value></field>  ' ||
4505                     '<field name="'||'NI_4'       ||'"><value>' ||substr(l_national_ins_no,4,1)             || '</value></field>  ' ||
4506                     '<field name="'||'NI_5'       ||'"><value>' ||substr(l_national_ins_no,5,1)             || '</value></field>  ' ||
4507                     '<field name="'||'NI_6'       ||'"><value>' ||substr(l_national_ins_no,6,1)             || '</value></field>  ' ||
4508                     '<field name="'||'NI_7'       ||'"><value>' ||substr(l_national_ins_no,7,1)             || '</value></field>  ' ||
4509                     '<field name="'||'NI_8'       ||'"><value>' ||substr(l_national_ins_no,8,1)             || '</value></field>  ' ||
4510                     '<field name="'||'NI_9'       ||'"><value>' ||substr(l_national_ins_no,9)               || '</value></field>  ' ||
4511                     '<field name="'||'REG_NO'     ||'"><value>' ||vans_entries.registration_number          || '</value></field>  ' ||
4512                     '<field name="'||'REG_DAY'    ||'"><value>' ||substr(vans_entries.date_registered,9,2)  || '</value></field>  ' ||
4513                     '<field name="'||'REG_MONTH'  ||'"><value>' ||substr(vans_entries.date_registered,6,2)  || '</value></field>  ' ||
4514                     '<field name="'||'REG_YEAR'   ||'"><value>' ||substr(vans_entries.date_registered,1,4)  || '</value></field>  ' ||
4515                     '<field name="'||'ONLY_VAN'   ||'"><value>' ||l_only_van                                || '</value></field>  ' ||
4516                     '<field name="'||'ONLY_VAN_Y' ||'"><value>' ||l_only_van                                || '</value></field>  ' ||
4517                     '<field name="'||'ONLY_VAN_N' ||'"><value>' ||l_only_van                                || '</value></field>  ' ||
4518                     '<field name="'||'VAN_COUNT'  ||'"><value>' ||l_vans_count                              || '</value></field>  ' ||
4519                     '<field name="'||'VAN_AFTER'  ||'"><value>' ||l_after_flag                              || '</value></field>  ' ||
4520                     '<field name="'||'VAN_AFTER_Y'||'"><value>' ||l_after_flag                              || '</value></field>  ' ||
4521                     '<field name="'||'VAN_AFTER_N'||'"><value>' ||l_after_flag                              || '</value></field>  ' ||
4522                     '<field name="'||'VAN_A'      ||'"><value>' ||vans_entries.van_charged                  || '</value></field>  ' ||
4523                     '<field name="'||'VAN_B_FD'   ||'"><value>' ||substr(vans_entries.van_from_b,9,2)       || '</value></field>  ' ||
4524                     '<field name="'||'VAN_B_FM'   ||'"><value>' ||substr(vans_entries.van_from_b,6,2)       || '</value></field>  ' ||
4525                     '<field name="'||'VAN_B_FY'   ||'"><value>' ||substr(vans_entries.van_from_b,1,4)       || '</value></field>  ' ||
4526                     '<field name="'||'VAN_B_TD'   ||'"><value>' ||substr(vans_entries.van_to_b,9,2)         || '</value></field>  ' ||
4527                     '<field name="'||'VAN_B_TM'   ||'"><value>' ||substr(vans_entries.van_to_b,6,2)         || '</value></field>  ' ||
4528                     '<field name="'||'VAN_B_TY'   ||'"><value>' ||substr(vans_entries.van_to_b,1,4)         || '</value></field>  ' ||
4529                     '<field name="'||'VAN_B'      ||'"><value>' ||vans_entries.van_unavil_b                 || '</value></field>  ' ||
4530                     '<field name="'||'VAN_C_FD'   ||'"><value>' ||substr(vans_entries.van_from_c,9,2)       || '</value></field>  ' ||
4531                     '<field name="'||'VAN_C_FM'   ||'"><value>' ||substr(vans_entries.van_from_c,6,2)       || '</value></field>  ' ||
4532                     '<field name="'||'VAN_C_FY'   ||'"><value>' ||substr(vans_entries.van_from_c,1,4)       || '</value></field>  ' ||
4533                     '<field name="'||'VAN_C_TD'   ||'"><value>' ||substr(vans_entries.van_to_c,9,2)         || '</value></field>  ' ||
4534                     '<field name="'||'VAN_C_TM'   ||'"><value>' ||substr(vans_entries.van_to_c,6,2)         || '</value></field>  ' ||
4535                     '<field name="'||'VAN_C_TY'   ||'"><value>' ||substr(vans_entries.van_to_c,1,4)         || '</value></field>  ' ||
4536                     '<field name="'||'VAN_C'      ||'"><value>' ||vans_entries.van_unavil_c                 || '</value></field>  ' ||
4537                     '<field name="'||'VAN_D_FD'   ||'"><value>' ||substr(vans_entries.van_from_d,9,2)       || '</value></field>  ' ||
4538                     '<field name="'||'VAN_D_FM'   ||'"><value>' ||substr(vans_entries.van_from_d,6,2)       || '</value></field>  ' ||
4539                     '<field name="'||'VAN_D_FY'   ||'"><value>' ||substr(vans_entries.van_from_d,1,4)       || '</value></field>  ' ||
4540                     '<field name="'||'VAN_D_TD'   ||'"><value>' ||substr(vans_entries.van_to_d,9,2)         || '</value></field>  ' ||
4541                     '<field name="'||'VAN_D_TM'   ||'"><value>' ||substr(vans_entries.van_to_d,6,2)         || '</value></field>  ' ||
4542                     '<field name="'||'VAN_D_TY'   ||'"><value>' ||substr(vans_entries.van_to_d,1,4)         || '</value></field>  ' ||
4543                     '<field name="'||'VAN_D'      ||'"><value>' ||vans_entries.van_unavil_d                 || '</value></field>  ' ||
4544                     '<field name="'||'VAN_E'      ||'"><value>' ||vans_entries.van_tot_day_unavil           || '</value></field>  ' ||
4545                     '<field name="'||'VAN_F'      ||'"><value>' ||vans_entries.van_unavil_value             || '</value></field>  ' ||
4546                     '<field name="'||'VAN_G'      ||'"><value>' ||vans_entries.van_reduce_value             || '</value></field>  ' ||
4547                     '<field name="'||'VAN_G2'     ||'"><value>' ||vans_entries.van_reduce_value             || '</value></field>  ' ||
4548                     '<field name="'||'VAN_H'      ||'"><value>' ||vans_entries.van_sh_pcent_reduc           || '</value></field>  ' ||
4549                     '<field name="'||'VAN_J'      ||'"><value>' ||vans_entries.van_sh_reduction             || '</value></field>  ' ||
4550                     '<field name="'||'VAN_EXP'    ||'"><value>' ||vans_entries.van_explanation              || '</value></field>  ' ||
4551                     '<field name="'||'VAN_K'      ||'"><value>' ||vans_entries.van_reduce_share             || '</value></field>  ' ||
4552                     '<field name="'||'VAN_L'      ||'"><value>' ||vans_entries.van_private_uses             || '</value></field>  ' ||
4553                     '<field name="'||'VAN_M'      ||'"><value>' ||vans_entries.van_benefit_charge           || '</value></field>  ' ||
4554                     '</fields>  </xfdf>';
4555             end loop;
4556         else -- after 2008 onwords -- P11D changes 07/08
4557             open csr_vans_07_count(l_pactid,
4558                                    l_person_id,
4559                                    l_emp_ref_no,
4560                                    l_employer_name);
4561             fetch csr_vans_07_count into l_vans_count;
4562             close csr_vans_07_count;
4563 
4564             --l_only_van := 'Y';
4565             if l_vans_count > 1 then
4566                l_only_van := 'N';
4567             else
4568                l_vans_count := null;
4569                l_only_van := 'Y';
4570             end if;
4571 
4572             for vans_entries in  csr_vans_entries_07(l_pactid,
4573                                                      l_person_id,
4574                                                      l_emp_ref_no,
4575                                                      l_employer_name)
4576             loop
4577                 l_loop_count := l_loop_count+1;
4578                 /* bug 7146755  added the below variables to check if the employee receives no fuel benefit
4579                 then display the fields fuel_charge_reduction and days_after_fuel_wd to be null */
4580                 l_fuel_charge_reduction := vans_entries.van_fuel_charge_reduction;
4581                 l_days_after_fuel_wd  :=vans_entries.van_days_after_fuel_wd;
4582 
4583                 if (vans_entries.van_feul_ben_charge = '0')
4584                 THEN
4585                 l_fuel_charge_reduction := NULL;
4586                 l_days_after_fuel_wd  :=NULL;
4587                 END IF;
4588                 --end of bug 7146755
4589                 hr_utility.trace('Vans 2007');
4590                 l_after_flag := 'N';
4591                /* if to_number(l_rep_run) < 2007 then
4592                    if fnd_date.canonical_to_date(vans_entries.date_registered) > to_date('05-04-2002','dd-mm-yyyy')
4593                    then
4594                        l_after_flag := 'Y';
4595                    end if;
4596                 else -- >= 2007
4597                    if fnd_date.canonical_to_date(vans_entries.date_registered) > to_date('05-04-2003','dd-mm-yyyy')
4598                    then
4599                        l_after_flag := 'Y';
4600                    end if;
4601                 end if; */
4602 
4603                 /* check date from and date to */
4604 
4605                 if fnd_date.canonical_to_date(vans_entries.van_from_b) = to_date(l_date_from,'DD-MM-YYYY') and
4606                    fnd_date.canonical_to_date(vans_entries.van_to_b)  = to_date(l_date_to,'DD-MM-YYYY')
4607                 then
4608                     vans_entries.van_from_b := null;
4609                     vans_entries.van_to_b   := null;
4610                 end if;
4611                 /* end check  */
4612                 l_employee_number := get_assignment_number(vans_entries.action_context_id);
4613 
4614                 l_xfdf_str_tab.extend;
4615                 /* bug 7146755 reporting the variables l_fuel_charge_reduction and l_ days_after_fuel_wd
4616                 instead of vans_entries.van_fuel_charge_reduction and vans_entries.days_after_fuel_wd */
4617                 l_xfdf_str_tab(l_loop_count) := '<?xml version = "1.0" encoding = "UTF-8"?>
4618                     <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
4619                     <fields> ' ||
4620                     '<field name="'||'Employer'   ||'"><value>' ||replace(l_employer_name,'&','&')      || '</value></field>  ' ||
4621                     '<field name="'||'Employee'   ||'"><value>' ||l_full_name                               || '</value></field>  ' ||
4622                     -- P11D 08/09
4623                     ' <field name="'||'SUR_NAME'  ||'"><value>' ||l_sur_name                                || '</value></field> '  ||
4624                     ' <field name="'||'FORE_NAME' ||'"><value>' ||l_fore_name                               || '</value></field> '  ||
4625                     -- P11D 08/09
4626                     '<field name="'||'PAYE_tax'   ||'"><value>' ||l_emp_ref_no                              || '</value></field>  ' ||
4627                     '<field name="'||'Works_no'   ||'"><value>' ||l_employee_number                         || '</value></field>  ' ||
4628                     '<field name="'||'NI_1'       ||'"><value>' ||substr(l_national_ins_no,1,1)             || '</value></field>  ' ||
4629                     '<field name="'||'NI_2'       ||'"><value>' ||substr(l_national_ins_no,2,1)             || '</value></field>  ' ||
4630                     '<field name="'||'NI_3'       ||'"><value>' ||substr(l_national_ins_no,3,1)             || '</value></field>  ' ||
4631                     '<field name="'||'NI_4'       ||'"><value>' ||substr(l_national_ins_no,4,1)             || '</value></field>  ' ||
4632                     '<field name="'||'NI_5'       ||'"><value>' ||substr(l_national_ins_no,5,1)             || '</value></field>  ' ||
4633                     '<field name="'||'NI_6'       ||'"><value>' ||substr(l_national_ins_no,6,1)             || '</value></field>  ' ||
4634                     '<field name="'||'NI_7'       ||'"><value>' ||substr(l_national_ins_no,7,1)             || '</value></field>  ' ||
4635                     '<field name="'||'NI_8'       ||'"><value>' ||substr(l_national_ins_no,8,1)             || '</value></field>  ' ||
4636                     '<field name="'||'NI_9'       ||'"><value>' ||substr(l_national_ins_no,9)               || '</value></field>  ' ||
4637                     '<field name="'||'REG_NO'     ||'"><value>' ||vans_entries.registration_number          || '</value></field>  ' ||
4638                     '<field name="'||'ONLY_VAN'   ||'"><value>' ||l_only_van                                || '</value></field>  ' ||
4639                     '<field name="'||'ONLY_VAN_Y' ||'"><value>' ||l_only_van                                || '</value></field>  ' ||
4640                     '<field name="'||'ONLY_VAN_N' ||'"><value>' ||l_only_van                                || '</value></field>  ' ||
4641                     '<field name="'||'VAN_COUNT'  ||'"><value>' ||l_vans_count                              || '</value></field>  ' ||
4642                     '<field name="'||'VAN_AFTER'  ||'"><value>' ||l_after_flag                              || '</value></field>  ' ||
4643                     '<field name="'||'VAN_A'      ||'"><value>' ||vans_entries.van_charged                  || '</value></field>  ' ||
4644                     '<field name="'||'VAN_B_FD'   ||'"><value>' ||substr(vans_entries.van_from_b,9,2)       || '</value></field>  ' ||
4645                     '<field name="'||'VAN_B_FM'   ||'"><value>' ||substr(vans_entries.van_from_b,6,2)       || '</value></field>  ' ||
4646                     '<field name="'||'VAN_B_FY'   ||'"><value>' ||substr(vans_entries.van_from_b,1,4)       || '</value></field>  ' ||
4647                     '<field name="'||'VAN_B_TD'   ||'"><value>' ||substr(vans_entries.van_to_b,9,2)         || '</value></field>  ' ||
4648                     '<field name="'||'VAN_B_TM'   ||'"><value>' ||substr(vans_entries.van_to_b,6,2)         || '</value></field>  ' ||
4649                     '<field name="'||'VAN_B_TY'   ||'"><value>' ||substr(vans_entries.van_to_b,1,4)         || '</value></field>  ' ||
4650                     '<field name="'||'VAN_B'      ||'"><value>' ||vans_entries.van_unavil_b                 || '</value></field>  ' ||
4651                     '<field name="'||'VAN_C_FD'   ||'"><value>' ||substr(vans_entries.van_from_c,9,2)       || '</value></field>  ' ||
4652                     '<field name="'||'VAN_C_FM'   ||'"><value>' ||substr(vans_entries.van_from_c,6,2)       || '</value></field>  ' ||
4653                     '<field name="'||'VAN_C_FY'   ||'"><value>' ||substr(vans_entries.van_from_c,1,4)       || '</value></field>  ' ||
4654                     '<field name="'||'VAN_C_TD'   ||'"><value>' ||substr(vans_entries.van_to_c,9,2)         || '</value></field>  ' ||
4655                     '<field name="'||'VAN_C_TM'   ||'"><value>' ||substr(vans_entries.van_to_c,6,2)         || '</value></field>  ' ||
4656                     '<field name="'||'VAN_C_TY'   ||'"><value>' ||substr(vans_entries.van_to_c,1,4)         || '</value></field>  ' ||
4657                     '<field name="'||'VAN_C'      ||'"><value>' ||vans_entries.van_unavil_c                 || '</value></field>  ' ||
4658                     '<field name="'||'VAN_D_FD'   ||'"><value>' ||substr(vans_entries.van_from_d,9,2)       || '</value></field>  ' ||
4659                     '<field name="'||'VAN_D_FM'   ||'"><value>' ||substr(vans_entries.van_from_d,6,2)       || '</value></field>  ' ||
4660                     '<field name="'||'VAN_D_FY'   ||'"><value>' ||substr(vans_entries.van_from_d,1,4)       || '</value></field>  ' ||
4661                     '<field name="'||'VAN_D_TD'   ||'"><value>' ||substr(vans_entries.van_to_d,9,2)         || '</value></field>  ' ||
4662                     '<field name="'||'VAN_D_TM'   ||'"><value>' ||substr(vans_entries.van_to_d,6,2)         || '</value></field>  ' ||
4663                     '<field name="'||'VAN_D_TY'   ||'"><value>' ||substr(vans_entries.van_to_d,1,4)         || '</value></field>  ' ||
4664                     '<field name="'||'VAN_D'      ||'"><value>' ||vans_entries.van_unavil_d                 || '</value></field>  ' ||
4665                     '<field name="'||'VAN_E'      ||'"><value>' ||vans_entries.van_tot_day_unavil           || '</value></field>  ' ||
4666                     '<field name="'||'VAN_F'      ||'"><value>' ||vans_entries.van_unavil_value             || '</value></field>  ' ||
4667                     '<field name="'||'VAN_G'      ||'"><value>' ||vans_entries.van_reduce_value             || '</value></field>  ' ||
4668                     '<field name="'||'VAN_G2'     ||'"><value>' ||vans_entries.van_reduce_value             || '</value></field>  ' ||
4669                     '<field name="'||'VAN_H'      ||'"><value>' ||vans_entries.van_sh_pcent_reduc           || '</value></field>  ' ||
4670                     '<field name="'||'VAN_J'      ||'"><value>' ||vans_entries.van_sh_reduction             || '</value></field>  ' ||
4671                     '<field name="'||'VAN_EXP'    ||'"><value>' ||vans_entries.van_explanation              || '</value></field>  ' ||
4672                     '<field name="'||'VAN_K'      ||'"><value>' ||vans_entries.van_reduce_share             || '</value></field>  ' ||
4673                     '<field name="'||'VAN_L'      ||'"><value>' ||vans_entries.van_private_uses             || '</value></field>  ' ||
4674                     '<field name="'||'VAN_M'      ||'"><value>' ||vans_entries.van_benefit_charge           || '</value></field>  ' ||
4675                     '<field name="'||'VAN_R_FD'   ||'"><value>' ||substr(vans_entries.van_fuel_withdrawn,9,2)       || '</value></field>  ' ||
4676                     '<field name="'||'VAN_R_FM'   ||'"><value>' ||substr(vans_entries.van_fuel_withdrawn,6,2)       || '</value></field>  ' ||
4677                     '<field name="'||'VAN_R_FY'   ||'"><value>' ||substr(vans_entries.van_fuel_withdrawn,1,4)       || '</value></field>  ' ||
4678                     '<field name="'||'VAN_R'      ||'"><value>' ||l_days_after_fuel_wd       || '</value></field>  ' ||
4679                     '<field name="'||'VAN_S'      ||'"><value>' ||vans_entries.van_total_days_no_fuel       || '</value></field>  ' ||
4680                     '<field name="'||'VAN_T'      ||'"><value>' ||vans_entries.van_reduction                || '</value></field>  ' ||
4681                     '<field name="'||'VAN_V'      ||'"><value>' ||l_fuel_charge_reduction    || '</value></field>  ' ||
4682                     '<field name="'||'VAN_W'      ||'"><value>' ||vans_entries.van_reduction_sharing        || '</value></field>  ' ||
4683                     '<field name="'||'VAN_X'      ||'"><value>' ||vans_entries.van_feul_ben_charge          || '</value></field>  ' ||
4684                     '</fields>  </xfdf>';
4685             end loop;
4686         end if;
4687 
4688         hr_utility.trace('10');
4689         open l_ret_ref_cursor for
4690         select *
4691         from table (cast(l_xfdf_str_tab as per_gb_xfdftableType));
4692         p_record_num := l_loop_count;
4693         return l_ret_ref_cursor;
4694    end ;
4695 
4696    function fetch_ws4_ref_cursor (p_assignment_action_id Number,
4697                                   p_record_num out nocopy NUmber) return ref_cursor_typ
4698    is
4699         -- per_gb_xfdftableType is explicitly created type
4700         -- only modification needed is it could be of type blob
4701         -- i tried thta but could not access the blob valus in java routine.
4702         -- can be sorted later
4703         l_xfdf_str_tab per_gb_xfdftableType := per_gb_xfdftableType( );
4704         l_xfdf_str varchar2(32000);
4705         l_ret_ref_cursor ref_cursor_typ;
4706         cursor csr_int_entries (p_pactid Number,
4707                                 p_person_id Number,
4708                                 p_emp_ref Varchar2 ,
4709                                 p_emp_name Varchar2)
4710         is
4711         select ACTION_INFORMATION7            Maximum_Amount_Outstanding,
4712                ' '                            Currency, -- 'GBP'Currency, -- as we currently support just GBP
4713                to_number(nvl(ACTION_INFORMATION6,'0'))  Amount_Outstanding_at_5th_Apri,
4714                to_number(nvl(ACTION_INFORMATION16,'0')) Amount_Outstanding_at_Year_End,
4715                ACTION_INFORMATION18           Official_Rate_of_Interest,
4716                ACTION_INFORMATION8            Total_Amount_of_Interest_Paid,
4717                ACTION_INFORMATION11           Cash_Equivalent,
4718                ACTION_INFORMATION19           Annual_Interest_Value,
4719                ACTION_INFORMATION20           Interest_Value,
4720                action_context_id
4721         from  pay_action_information
4722         where action_information_category = 'INT FREE AND LOW INT LOANS'
4723         and   action_context_type = 'AAP'
4724         and   action_context_id in(select paa.assignment_action_id
4725                                     from   pay_action_information pai_comp,
4726                                            pay_action_information pai_person,
4727                                            pay_assignment_actions paa,
4728                                            pay_payroll_actions ppa
4729                                     where  ppa.payroll_action_id = p_pactid
4730                                     and    paa.payroll_action_id = ppa.payroll_action_id
4731                                     and    pai_comp.action_context_id = paa.assignment_action_id
4732                                     and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
4733                                     and    pai_comp.action_context_type = 'AAP'
4734                                     and    pai_person.action_context_id = paa.assignment_action_id
4735                                     and    pai_person.action_information_category = 'ADDRESS DETAILS'
4736                                     and    pai_person.action_context_type = 'AAP'
4737                                     and    pai_person.action_information14 = 'Employee Address'
4738                                     and    pai_person.action_information1 = to_char(p_person_id)
4739                                     and    pai_comp.action_information6 = p_emp_ref
4740                                     and    pai_comp.action_information7 = p_emp_name)
4741         and   to_number(nvl(ACTION_INFORMATION11,'0')) > 0;
4742 
4743         l_emp_ref_no varchar2(150);
4744         l_employer_name varchar2(150);
4745         l_full_name varchar2(150);
4746         l_sur_name varchar2(150); --  P11D 08/09
4747         l_fore_name varchar2(150); -- P11D 08/09
4748         l_national_ins_no varchar2(150);
4749         l_employee_number varchar2(150);
4750         l_pactid number;
4751         l_person_id number;
4752         l_loop_count Number;
4753         l_field_num Number;
4754         l_tab_index Number;
4755         l_amt_sum Number;
4756         l_AMT_AS_PER_MTH Number;
4757         l_int Number;
4758         l_h_sum_max_amt_outstanding Number;
4759         l_rep_run varchar2(10);
4760         l_loan_threshold Number;
4761    begin
4762         hr_utility.trace('calling get_employer_details');
4763         get_employer_details(p_assignment_action_id,
4764                              l_emp_ref_no,
4765                              l_employer_name);
4766         hr_utility.trace('calling get_employee_details');
4767         get_employee_details(p_assignment_action_id,
4768                              l_full_name,
4769                              l_national_ins_no,
4770                              l_employee_number);
4771         -- P11D 08/09
4772         -- Fetch sur and fore names
4773         get_sur_fore_names(p_assignment_action_id,
4774                            l_sur_name,
4775                            l_fore_name);
4776          -- P11D 08/09
4777         l_pactid := get_pactid(p_assignment_action_id);
4778         l_person_id := get_person_id (p_assignment_action_id );
4779         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
4780          p_payroll_action_id           => l_pactid,
4781          p_token_name                  => 'Rep_Run',
4782          p_token_value                 => l_rep_run);
4783 
4784         l_loop_count := 0;
4785         hr_utility.trace('l_pactid ' || l_pactid);
4786         hr_utility.trace('l_person_id '|| l_person_id);
4787         hr_utility.trace('l_emp_ref_no '|| l_emp_ref_no);
4788         hr_utility.trace('l_employer_name '|| l_employer_name);
4789         l_tab_index := 0;
4790 
4791         select to_number(nvl(ACTION_INFORMATION23,'0'))
4792         into  l_h_sum_max_amt_outstanding
4793         from  pay_action_information pai_emp
4794         where pai_emp.action_context_id = p_assignment_action_id
4795         and   pai_emp.action_information_category = 'GB P11D ASSIGNMENT RESULTC';
4796 
4797         if to_number(l_rep_run) < 2007 then
4798            l_loan_threshold := 5000;
4799         else
4800            -- Fetch the minimum outstanding
4801            select to_number(global_value)
4802            into  l_loan_threshold
4803            from  ff_globals_f
4804            where global_name = 'P11D_LOW_INT_LOAN_THRESHOLD'
4805            and   to_date('05-04-' || l_rep_run,'DD-MM-YYYY') between effective_start_date and effective_end_date;
4806         end if;
4807 
4808         if l_h_sum_max_amt_outstanding > l_loan_threshold
4809         then
4810             for int_entries in  csr_int_entries(l_pactid,
4811                                                 l_person_id,
4812                                                 l_emp_ref_no,
4813                                                 l_employer_name)
4814             loop
4815                 l_employee_number := get_assignment_number(int_entries.action_context_id);
4816                 l_loop_count := l_loop_count+1;
4817                 hr_utility.trace('l_loop_count '  ||l_loop_count);
4818                 --we need 5 records in a single pdf template so if more than 5 exists
4819                 --we need extra pages ceil(l_loop_count/5) will give the index number
4820                 --for the l_ws4_info_tab
4821                 if l_tab_index =ceil(l_loop_count/5)
4822                 then
4823                     -- means same template input
4824                       null;
4825                 else -- new template input
4826                     if l_tab_index <> 0 then
4827                        -- this will end the first tempalet input.
4828                        l_xfdf_str_tab(l_tab_index) := l_xfdf_str_tab(l_tab_index) || '</fields>  </xfdf>';
4829                     end if;
4830                     -- assign the new index number of the template
4831                     l_tab_index := ceil(l_loop_count/5);
4832                     l_xfdf_str_tab.extend;
4833                     l_xfdf_str_tab(l_tab_index) := '<?xml version = "1.0" encoding = "UTF-8"?>
4834                         <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
4835                         <fields> ' ||
4836                         '<field name="'||'EMPLOYERS_NAME'||'"><value>' ||replace(l_employer_name,'&','&')  || '</value></field> ' ||
4837                         '<field name="'||'FULL_NAME'||'"><value>' ||l_full_name || '</value></field>  ' ||
4838                          -- P11D 08/09
4839                         ' <field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
4840                         ' <field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
4841                          -- P11D 08/09
4842                         '<field name="'||'EMPLOYERS_REF_NO'||'"><value>' ||l_emp_ref_no || '</value></field>  ' ||
4843                         '<field name="'||'EMPLOYEE_NUMBER'||'"><value>' ||l_employee_number || '</value></field>  ' ||
4844                         '<field name="'||'NI_1'||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
4845                         '<field name="'||'NI_2'||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
4846                         '<field name="'||'NI_3'||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
4847                         '<field name="'||'NI_4'||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
4848                         '<field name="'||'NI_5'||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
4849                         '<field name="'||'NI_6'||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
4850                         '<field name="'||'NI_7'||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
4851                         '<field name="'||'NI_8'||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
4852                         '<field name="'||'NI_9'||'"><value>' ||substr(l_national_ins_no,9) || '</value></field>  ' ;
4853                 end if;
4854                 hr_utility.trace('l_tab_index '  ||l_tab_index);
4855                 l_field_num := mod(l_loop_count,5);
4856                 if l_field_num = 0
4857                 then
4858                     l_field_num := 5;
4859                 end if;
4860                 hr_utility.trace('l_field_num '  ||l_field_num);
4861                 l_xfdf_str_tab(l_tab_index) :=  l_xfdf_str_tab(l_tab_index)  ||
4862                     '<field name="'||'MAX_OUT_'  || l_field_num ||'"><value>' ||int_entries.Maximum_Amount_Outstanding || '</value></field>  ' ||
4863                     '<field name="'||'CURRENCY_' || l_field_num ||'"><value>' ||int_entries.Currency || '</value></field>  ' ||
4864                     '<field name="'||'AMT_YRST_' || l_field_num ||'"><value>' ||int_entries.Amount_Outstanding_at_5th_Apri || '</value></field>  ' ||
4865                     '<field name="'||'AMT_YREND_'|| l_field_num ||'"><value>' ||int_entries.Amount_Outstanding_at_Year_End || '</value></field>  ' ;
4866                 l_amt_sum   :=  nvl(int_entries.Amount_Outstanding_at_5th_Apri,0) +
4867                                 nvl(int_entries.Amount_Outstanding_at_Year_End,0);
4868                 l_xfdf_str_tab(l_tab_index) :=        l_xfdf_str_tab(l_tab_index)  ||
4869                     '<field name="'||'AMT_SUM_'  || l_field_num ||'"><value>' ||l_amt_sum || '</value></field>  ' ||
4870                     '<field name="'||'AMT_AVG_'  || l_field_num ||'"><value>' ||(l_amt_sum/2) || '</value></field>  ' ||
4871                     '<field name="'||'TAX_MTH_'  || l_field_num ||'"><value>' ||(int_entries.Interest_Value) || '</value></field>  ' ;
4872                 l_AMT_AS_PER_MTH := (l_amt_sum/2)*(int_entries.Interest_Value/12);
4873                 l_xfdf_str_tab(l_tab_index) := l_xfdf_str_tab(l_tab_index)  ||
4874                     '<field name="'||'ACT_MTH_'|| l_field_num ||'"><value>' ||l_AMT_AS_PER_MTH || '</value></field>  ' ||
4875                     '<field name="'||'RATE_'   || l_field_num ||'"><value>' ||int_entries.Official_Rate_of_Interest || '</value></field>  ' ;
4876                 l_int := round(l_AMT_AS_PER_MTH *int_entries.Official_Rate_of_Interest/100,2);
4877                 l_xfdf_str_tab(l_tab_index) := l_xfdf_str_tab(l_tab_index)  ||
4878                     '<field name="'||'INT_'     || l_field_num ||'"><value>' ||l_int || '</value></field>  ' ||
4879                     '<field name="'||'EMP_INT_' || l_field_num ||'"><value>' ||int_entries.Total_Amount_of_Interest_Paid || '</value></field>  ' ||
4880                     '<field name="'||'CE_'      || l_field_num ||'"><value>' ||int_entries.Cash_Equivalent || '</value></field>  ' ;
4881             end loop;
4882         -- this will ensure the xfdf sytring ends with the correct fields
4883         end if;
4884         hr_utility.trace('After cursor close');
4885         hr_utility.trace('l_tab_index' || l_tab_index);
4886         if l_tab_index <> 0
4887         then
4888             l_xfdf_str_tab(l_tab_index) := l_xfdf_str_tab(l_tab_index) || '</fields>  </xfdf>';
4889         end if;
4890         open l_ret_ref_cursor for
4891         select *
4892         from table (cast(l_xfdf_str_tab as per_gb_xfdftableType));
4893         p_record_num := l_tab_index;
4894         return l_ret_ref_cursor;
4895    end ;
4896 
4897    function fetch_ws5_data_blob (p_assignment_action_id Number) return blob
4898    is
4899         l_xfdf_str clob;
4900         l_xfdf_blob_str blob;
4901         cursor csr_relocation_entries(p_pactid Number,
4902                                       p_person_id Number,
4903                                       p_emp_ref Varchar2 ,
4904                                       p_emp_name Varchar2)
4905         is
4906         select
4907               to_char(to_number(nvl(pai_ben.action_information9,0)),'FM999,999,990.00') GROSS_AMOUNT,
4908               to_char(to_number(nvl(pai_ben.action_information10,0)),'FM999,999,990.00') COST,
4909               to_char(to_number(nvl(pai_ben.action_information11,0)),'FM999,999,990.00') PAID_BY_EMPLOYEE,
4910               to_char(to_number(nvl(pai_ben.action_information10,0) - nvl(pai_ben.action_information11,0)),'FM999,999,990.00')QUALIFYING_BENEFITS,
4911               to_char(to_number(nvl(pai_ben.action_information13,0)),'FM999,999,990.00') COST_OF_ACCO,
4912               to_char(to_number(nvl(pai_ben.action_information9,0) + nvl(pai_ben.action_information13,0) +
4913               nvl(pai_ben.action_information10,0) - nvl(pai_ben.action_information11,0)),
4914                     'FM999,999,990.00') TOTAL,
4915               to_char(to_number(nvl(pai_ben.action_information14,0)),'FM999,999,990.00') EARLIER_YEARS,
4916               to_char(to_number(nvl(pai_ben.action_information15,0)),'FM999,999,990.00') AMOUNT_EXEMPTED,
4917               to_char(to_number(nvl(pai_ben.action_information5,0)),'FM999,999,990.00') FINAL_AMOUNT,
4918               action_context_id
4919         from  pay_action_information pai_ben
4920         where pai_ben.action_information_category = 'RELOCATION EXPENSES'
4921         and   pai_ben.action_context_type = 'AAP'
4922         and   pai_ben.action_context_id in ( select paa.assignment_action_id
4923                                              from   pay_action_information pai_comp,
4924                                                     pay_action_information pai_person,
4925                                                     pay_assignment_actions paa,
4926                                                     pay_payroll_actions ppa
4927                                              where  ppa.payroll_action_id = p_pactid
4928                                              and    paa.payroll_action_id = ppa.payroll_action_id
4929                                              and    pai_comp.action_context_id = paa.assignment_action_id
4930                                              and    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
4931                                              and    pai_comp.action_context_type = 'AAP'
4932                                              and    pai_person.action_context_id = paa.assignment_action_id
4933                                              and    pai_person.action_information_category = 'ADDRESS DETAILS'
4934                                              and    pai_person.action_context_type = 'AAP'
4935                                              and    pai_person.action_information14 = 'Employee Address'
4936                                              and    pai_person.action_information1 = to_char(p_person_id)
4937                                              and    pai_comp.action_information6 = p_emp_ref
4938                                              and    pai_comp.action_information7 = p_emp_name);
4939         l_emp_ref_no varchar2(150);
4940         l_employer_name varchar2(150);
4941         l_full_name varchar2(150);
4942         l_sur_name varchar2(150); --  P11D 08/09
4943         l_fore_name varchar2(150); -- P11D 08/09
4944         l_national_ins_no varchar2(150);
4945         l_employee_number varchar2(150);
4946         l_pactid number;
4947         l_person_id number;
4948         l_loop_count Number;
4949    begin
4950         get_employer_details(p_assignment_action_id,
4951                              l_emp_ref_no,
4952                              l_employer_name);
4953         get_employee_details(p_assignment_action_id,
4954                              l_full_name,
4955                              l_national_ins_no,
4956                              l_employee_number);
4957         -- P11D 08/09
4958         -- Fetch sur and fore names
4959         get_sur_fore_names(p_assignment_action_id,
4960                            l_sur_name,
4961                            l_fore_name);
4962          -- P11D 08/09
4963         l_pactid := get_pactid(p_assignment_action_id);
4964         l_person_id := get_person_id (p_assignment_action_id );
4965         l_loop_count := 0;
4966         dbms_lob.createtemporary(l_xfdf_str,false,DBMS_LOB.CALL);
4967         dbms_lob.open(l_xfdf_str,dbms_lob.lob_readwrite);
4968         for relocation_entries in  csr_relocation_entries(l_pactid,
4969                                                           l_person_id,
4970                                                           l_emp_ref_no,
4971                                                           l_employer_name)
4972         loop
4973             l_employee_number := get_assignment_number(relocation_entries.action_context_id);
4974             l_loop_count := l_loop_count+1;
4975             if l_loop_count = 1
4976             then
4977                 dbms_lob.writeAppend( l_xfdf_str,
4978                 length(
4979                 '<?xml version = "1.0" encoding = "UTF-8"?>
4980                  <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
4981                  <fields> ' ||
4982                 '<field name="'||'EMPLOYERS_NAME'   ||'"><value>' ||replace(l_employer_name,'&','&')  ||'</value></field> ' ||
4983                 '<field name="'||'FULL_NAME'        ||'"><value>' ||l_full_name                   || '</value></field>  ' ||
4984                  -- P11D 08/09
4985                 ' <field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
4986                 ' <field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
4987                  -- P11D 08/09
4988                 '<field name="'||'EMPLOYERS_REF_NO' ||'"><value>' ||l_emp_ref_no                  || '</value></field>  ' ||
4989                 '<field name="'||'EMPLOYEE_NUMBER'  ||'"><value>' ||l_employee_number             || '</value></field>  ' ||
4990                 '<field name="'||'NI_1'             ||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
4991                 '<field name="'||'NI_2'             ||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
4992                 '<field name="'||'NI_3'             ||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
4993                 '<field name="'||'NI_4'             ||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
4994                 '<field name="'||'NI_5'             ||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
4995                 '<field name="'||'NI_6'             ||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
4996                 '<field name="'||'NI_7'             ||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
4997                 '<field name="'||'NI_8'             ||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
4998                 '<field name="'||'NI_9'             ||'"><value>' ||substr(l_national_ins_no,9)   || '</value></field>  '),
4999                 '<?xml version = "1.0" encoding = "UTF-8"?>
5000                  <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
5001                  <fields> ' ||
5002                 '<field name="'||'EMPLOYERS_NAME'   ||'"><value>' ||replace(l_employer_name,'&','&')  ||'</value></field> ' ||
5003                 '<field name="'||'FULL_NAME'        ||'"><value>' ||l_full_name                   || '</value></field>  ' ||
5004                 -- P11D 08/09
5005                 ' <field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
5006                 ' <field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
5007                  -- P11D 08/09
5008 		'<field name="'||'EMPLOYERS_REF_NO' ||'"><value>' ||l_emp_ref_no                  || '</value></field>  ' ||
5009                 '<field name="'||'EMPLOYEE_NUMBER'  ||'"><value>' ||l_employee_number             || '</value></field>  ' ||
5010                 '<field name="'||'NI_1'             ||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
5011                 '<field name="'||'NI_2'             ||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
5012                 '<field name="'||'NI_3'             ||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
5013                 '<field name="'||'NI_4'             ||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
5014                 '<field name="'||'NI_5'             ||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
5015                 '<field name="'||'NI_6'             ||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
5016                 '<field name="'||'NI_7'             ||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
5017                 '<field name="'||'NI_8'             ||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
5018                 '<field name="'||'NI_9'             ||'"><value>' ||substr(l_national_ins_no,9)   || '</value></field>  ');
5019             end if;
5020             dbms_lob.writeAppend( l_xfdf_str,
5021             length(
5022             '<field name="'||'GROSS_AMOUNT'       ||'"><value>' ||relocation_entries.GROSS_AMOUNT        || '</value></field>  ' ||
5023             '<field name="'||'COST'               ||'"><value>' ||relocation_entries.COST                || '</value></field>  ' ||
5024             '<field name="'||'PAID_BY_EMPLOYEE'   ||'"><value>' ||relocation_entries.PAID_BY_EMPLOYEE    || '</value></field>  ' ||
5025             '<field name="'||'QUALIFYING_BENEFITS'||'"><value>' ||relocation_entries.QUALIFYING_BENEFITS || '</value></field>  ' ||
5026             '<field name="'||'COST_OF_ACCO'       ||'"><value>' ||relocation_entries.COST_OF_ACCO        || '</value></field>  ' ||
5027             '<field name="'||'TOTAL'              ||'"><value>' ||relocation_entries.TOTAL               || '</value></field>  ' ||
5028             '<field name="'||'EARLIER_YEARS'      ||'"><value>' ||relocation_entries.EARLIER_YEARS       || '</value></field>  ' ||
5029             '<field name="'||'AMOUNT_EXEMPTED'    ||'"><value>' ||relocation_entries.AMOUNT_EXEMPTED     || '</value></field>  ' ||
5030             '<field name="'||'FINAL_AMOUNT'       ||'"><value>' ||relocation_entries.FINAL_AMOUNT        || '</value></field>  '),
5031             '<field name="'||'GROSS_AMOUNT'       ||'"><value>' ||relocation_entries.GROSS_AMOUNT        || '</value></field>  ' ||
5032             '<field name="'||'COST'               ||'"><value>' ||relocation_entries.COST                || '</value></field>  ' ||
5033             '<field name="'||'PAID_BY_EMPLOYEE'   ||'"><value>' ||relocation_entries.PAID_BY_EMPLOYEE    || '</value></field>  ' ||
5034             '<field name="'||'QUALIFYING_BENEFITS'||'"><value>' ||relocation_entries.QUALIFYING_BENEFITS || '</value></field>  ' ||
5035             '<field name="'||'COST_OF_ACCO'       ||'"><value>' ||relocation_entries.COST_OF_ACCO        || '</value></field>  ' ||
5036             '<field name="'||'TOTAL'              ||'"><value>' ||relocation_entries.TOTAL               || '</value></field>  ' ||
5037             '<field name="'||'EARLIER_YEARS'      ||'"><value>' ||relocation_entries.EARLIER_YEARS       || '</value></field>  ' ||
5038             '<field name="'||'AMOUNT_EXEMPTED'    ||'"><value>' ||relocation_entries.AMOUNT_EXEMPTED     || '</value></field>  ' ||
5039             '<field name="'||'FINAL_AMOUNT'       ||'"><value>' ||relocation_entries.FINAL_AMOUNT        || '</value></field>  ');
5040         end loop;
5041         if l_loop_count <> 0
5042         then
5043             dbms_lob.writeAppend( l_xfdf_str,length('</fields>  </xfdf>'),'</fields>  </xfdf>');
5044         end if;
5045         DBMS_LOB.CREATETEMPORARY(l_xfdf_blob_str,true);
5046         clob_to_blob(l_xfdf_str,l_xfdf_blob_str);
5047         dbms_lob.close(l_xfdf_str);
5048         dbms_lob.freetemporary(l_xfdf_str);
5049         return  l_xfdf_blob_str;
5050    end;
5051 
5052    function fetch_ws6_ref_cursor (p_assignment_action_id Number,
5053                                   p_record_num out nocopy NUmber) return ref_cursor_typ
5054    is
5055         -- per_gb_xfdftableType is explicitly created type
5056         -- only modification needed is it could be of type blob
5057         -- i tried thta but could not access the blob valus in java routine.
5058         -- can be sorted later
5059         l_xfdf_str_tab per_gb_xfdftableType := per_gb_xfdftableType( );
5060         l_xfdf_str varchar2(32000);
5061         l_ret_ref_cursor ref_cursor_typ;
5062         l_offset integer;
5063         l_varchar_buffer varchar2(32000);
5064         l_raw_buffer raw(32000);
5065         l_buffer_len number:= 32000;
5066         l_chunk_len number;
5067 
5068         cursor csr_amap_entries (p_pactid Number,
5069                                  p_person_id Number,
5070                                  p_emp_ref Varchar2 ,
5071                                  p_emp_name Varchar2)
5072         is
5073         select nvl(ACTION_INFORMATION12,0)  C_MILEAGE_ALLOW_PAYMENTS,
5074                nvl(ACTION_INFORMATION13,0)  B_MILEAGE_ALLOW_PAYMENTS,
5075                nvl(ACTION_INFORMATION14,0)  M_MILEAGE_ALLOW_PAYMENTS,
5076                nvl(ACTION_INFORMATION16,0)  C_TAX_DEDUCTED_PAYMENTS,
5077                nvl(ACTION_INFORMATION17,0)  B_TAX_DEDUCTED_PAYMENTS,
5078                nvl(ACTION_INFORMATION18,0)  M_TAX_DEDUCTED_PAYMENTS,
5079                (nvl(ACTION_INFORMATION12,0) - nvl(ACTION_INFORMATION16,0)) C_NET_ALLOWANCE,
5080                (nvl(ACTION_INFORMATION13,0) - nvl(ACTION_INFORMATION17,0)) B_NET_ALLOWANCE,
5081                (nvl(ACTION_INFORMATION14,0) - nvl(ACTION_INFORMATION18,0)) M_NET_ALLOWANCE,
5082                nvl(ACTION_INFORMATION1,0)  C_BUSINESS_MILES,
5083                nvl(ACTION_INFORMATION2,0)  M_BUSINESS_MILES,
5084                nvl(ACTION_INFORMATION3,0)  B_BUSINESS_MILES,
5085                nvl(ACTION_INFORMATION4,0) c_reimbursement_rate1,
5086                nvl(ACTION_INFORMATION6,0) m_reimbursement_rate1,
5087                nvl(ACTION_INFORMATION8,0) b_reimbursement_rate1,
5088                nvl(ACTION_INFORMATION5,0) c_reimbursement_rate2,
5089                nvl(ACTION_INFORMATION7,0) m_reimbursement_rate2,
5090                nvl(ACTION_INFORMATION9,0) b_reimbursement_rate2,
5091                nvl(ACTION_INFORMATION19,0) PASSEN_PAYMENTS,
5092                nvl(ACTION_INFORMATION20,0) PASSEN_BUSINESS_MILES,
5093                nvl(ACTION_INFORMATION21,0) PASSENGER_BUS_MILES_AMOUNT
5094         from pay_action_information
5095         where action_information_category = 'GB P11D ASSIGNMENT RESULTC'
5096         and   action_context_type = 'AAP'
5097         and   action_context_id = p_assignment_action_id;/*Removed sub query for assignment action id*/
5098 
5099 --Start of the fix for the EAP bug 9383416
5100 cursor get_ben_value (c_emp_ref in varchar2) is
5101 
5102        select /*+ ORDERED use_nl(paa,pai,pai_a,pai_person)
5103                     use_index(pai_person,pay_action_information_n2)
5104                     use_index(pai,pay_action_information_n2)
5105                     use_index(pai_a,pay_action_information_n2)*/
5106               pai_a.action_information12
5107        from   pay_assignment_actions  paa,
5108               pay_action_information  pai,
5109               pay_action_information  pai_a,
5110               pay_action_information  pai_person
5111        where  paa.assignment_action_id = p_assignment_action_id
5112        and    pai.action_context_id = paa.assignment_action_id
5113        and    pai.action_context_type = 'AAP'
5114        and    pai.action_information_category = pai.action_information_category
5115        and    pai_person.action_context_id = paa.assignment_action_id
5116        and    pai_person.action_information_category = 'GB EMPLOYEE DETAILS'
5117        and    pai_person.action_context_type = 'AAP'
5118        and    upper(pai_person.action_information13) = upper(c_emp_ref)
5119        and    pai_a.action_context_id = paa.assignment_action_id
5120        and    pai_a.action_context_type = 'AAP'
5121        and    pai_a.action_information_category = 'GB P11D ASSIGNMENT RESULTA'
5122        and    pai.action_information_category = 'MILEAGE ALLOWANCE AND PPAYMENT';
5123 --End of the fix for the EAP bug 9383416
5124 
5125 --Added the below cursor for the bug 11727875
5126     cursor get_other_asg_mil_ben (c_asg_act_id number,
5127                                   c_pact_id number,
5128                                   c_person_id number)is
5129         select pai_a.action_information12                --Modified for the bug 9799823
5130           from pay_action_information pai,
5131                pay_assignment_actions paa,
5132                pay_action_information pai_a,
5133                per_all_assignments_f paaf
5134          where paa.payroll_action_id = c_pact_id
5135            and pai.action_context_id = paa.assignment_action_id
5136            and paa.assignment_action_id <> c_asg_act_id
5137            and paaf.person_id = c_person_id
5138            and paaf.assignment_id = paa.assignment_id
5139            and pai.action_context_type = 'AAP'
5140            and pai_a.action_context_id = paa.assignment_action_id
5141            and pai_a.action_context_type = 'AAP'
5142            and pai_a.action_information_category = 'GB P11D ASSIGNMENT RESULTA'
5143            and pai.action_information_category = 'MILEAGE ALLOWANCE AND PPAYMENT';
5144 
5145         l_emp_ref_no varchar2(150);
5146         l_employer_name varchar2(150);
5147         l_full_name varchar2(150);
5148         l_sur_name varchar2(150); --  P11D 08/09
5149         l_fore_name varchar2(150); -- P11D 08/09
5150         l_national_ins_no varchar2(150);
5151         l_employee_number varchar2(150);
5152         l_pactid number;
5153         l_rep_run  varchar2(10);
5154         l_person_id number;
5155         type ws6_info_rec_typ is record (
5156           l_net_allowance Varchar2(20),
5157           l_bus_mile_1_amount Varchar2(20),
5158           l_bus_mile_2_amount Varchar2(20),
5159           l_total_tax_payment Varchar2(20),
5160           l_vehiclecheck      Varchar2(10),
5161           l_taxable_passen_payment Number,
5162           l_total_payment    Number,
5163           l_total_approved_maps Varchar2(20),
5164           l_business_miles_1 Number,
5165           l_business_miles_2 Number,
5166           l_c_mileage_allow_payments Number,
5167           l_c_tax_deducted_payments Number,
5168           l_c_business_miles    Number,
5169           l_c_reimbrs1_rate1   Number,
5170           l_c_reimbrs2_rate2   Number,
5171           l_c_net_allowance Number,
5172           l_passen_payments Number,
5173           l_passenger_bus_miles_amount  Number,
5174           l_passen_business_miles   Number);
5175 
5176         type ws6_info_rec_tab_typ is table of ws6_info_rec_typ index by Binary_integer;
5177         l_ws6_info_tab ws6_info_rec_tab_typ;
5178         l_loop_count Number;
5179         l_sum_PASSEN_PAYMENTS Varchar2(15);
5180         l_sum_PASSEN_BUSINESS_MILES Varchar2(15);
5181         l_sum_PASS_BUS_MILES_AMOUNT Varchar2(15);
5182         l_sum_Taxable_passen_payments Varchar2(15);
5183         l_miles1 Number :=0;
5184         l_miles2 Number :=0;
5185         l_mile1_amount Number :=0;
5186         l_mile2_amount Number :=0;
5187         l_Carvan  Varchar2(5);
5188         l_MCycle  Varchar2(5);
5189         l_BCycle  Varchar2(5);
5190         l_cnt     Number:=0;
5191         l_ben_value Number;
5192    begin
5193         get_employer_details(p_assignment_action_id,
5194                              l_emp_ref_no,
5195                              l_employer_name);
5196         get_employee_details(p_assignment_action_id,
5197                              l_full_name,
5198                              l_national_ins_no,
5199                              l_employee_number);
5200         -- P11D 08/09
5201         -- Fetch sur and fore names
5202         get_sur_fore_names(p_assignment_action_id,
5203                            l_sur_name,
5204                            l_fore_name);
5205          -- P11D 08/09
5206         l_pactid := get_pactid(p_assignment_action_id);
5207         l_person_id := get_person_id (p_assignment_action_id );
5208         l_employee_number := get_assignment_number(p_assignment_action_id,true,l_person_id,l_emp_ref_no);
5209         l_loop_count := 0;
5210         l_sum_PASSEN_PAYMENTS := 0;
5211         l_sum_PASSEN_BUSINESS_MILES := 0;
5212         l_sum_PASS_BUS_MILES_AMOUNT := 0;
5213         l_sum_Taxable_passen_payments:=0;
5214 
5215 --Start of the fix for the EAP bug 9383416
5216         open get_ben_value(l_emp_ref_no);
5217         fetch get_ben_value into l_ben_value;
5218         close get_ben_value;
5219 
5220 --Modifications for the bug 11727875 starts
5221 if (nvl(l_ben_value,0) <= 0)
5222 then
5223         open get_other_asg_mil_ben(p_assignment_action_id, l_pactid, l_person_id);
5224         fetch get_other_asg_mil_ben into l_ben_value;
5225         close get_other_asg_mil_ben;
5226 end if;
5227 --Modifications for the bug 11727875 ends
5228 
5229         if (l_ben_value > 0) then
5230 --End of the fix for the EAP bug 9383416
5231 
5232         for amap_entries in  csr_amap_entries(l_pactid,
5233                                               l_person_id,
5234                                               l_emp_ref_no,
5235                                               l_employer_name)
5236         loop
5237             l_Carvan := '';
5238             l_BCycle := '';
5239             l_MCycle := '';
5240             l_cnt    := 0;
5241             if amap_entries.C_BUSINESS_MILES > 0
5242             then
5243                 l_cnt := l_cnt + 1;
5244                 l_Carvan := 'CAR';
5245             end if;
5246             if amap_entries.M_BUSINESS_MILES > 0
5247             then
5248                 l_cnt := l_cnt + 1;
5249                 l_MCycle := 'MCY';
5250             end if;
5251             if amap_entries.B_BUSINESS_MILES > 0
5252             then
5253                 l_cnt := l_cnt + 1;
5254                 l_BCycle := 'BCY';
5255             end if;
5256             for i in 1 .. l_cnt
5257             loop
5258                 l_loop_count := l_loop_count+1;
5259                 -- l_ws6_info_tab(l_loop_count).l_amap_entries := amap_entries;
5260                 -- hardcoding 10000, as currently this si the fig,
5261                 -- if this changes then we may archve max limit as well.
5262                 HR_UTILITY.TRaCE('A');
5263                 if l_BCycle = 'BCY'
5264                 then
5265                     l_ws6_info_tab(l_loop_count).l_c_mileage_allow_payments := amap_entries.B_MILEAGE_ALLOW_PAYMENTS;
5266                     l_ws6_info_tab(l_loop_count).l_c_tax_deducted_payments  := amap_entries.B_TAX_DEDUCTED_PAYMENTS;
5267                     l_ws6_info_tab(l_loop_count).l_c_net_allowance          := amap_entries.B_NET_ALLOWANCE;
5268                     l_ws6_info_tab(l_loop_count).l_c_business_miles         := amap_entries.B_BUSINESS_MILES;
5269                     l_ws6_info_tab(l_loop_count).l_c_reimbrs1_rate1    := amap_entries.b_reimbursement_rate1;
5270                     l_ws6_info_tab(l_loop_count).l_c_reimbrs2_rate2    := amap_entries.b_reimbursement_rate2;
5271                     l_ws6_info_tab(l_loop_count).l_vehiclecheck           := 'B';
5272                     l_BCycle := '';
5273                 elsif l_MCycle = 'MCY'
5274                 then
5275                     l_ws6_info_tab(l_loop_count).l_c_mileage_allow_payments := amap_entries.M_MILEAGE_ALLOW_PAYMENTS;
5276                     l_ws6_info_tab(l_loop_count).l_c_tax_deducted_payments  := amap_entries.M_TAX_DEDUCTED_PAYMENTS;
5277                     l_ws6_info_tab(l_loop_count).l_c_net_allowance          := amap_entries.M_NET_ALLOWANCE;
5278                     l_ws6_info_tab(l_loop_count).l_c_business_miles       := amap_entries.M_BUSINESS_MILES;
5279                     l_ws6_info_tab(l_loop_count).l_c_reimbrs1_rate1  := amap_entries.m_reimbursement_rate1;
5280                     l_ws6_info_tab(l_loop_count).l_c_reimbrs2_rate2    := amap_entries.m_reimbursement_rate2;
5281                     l_ws6_info_tab(l_loop_count).l_vehiclecheck           := 'M';
5282                     l_MCycle := '';
5283                 elsif l_Carvan = 'CAR'
5284                 then
5285                     l_ws6_info_tab(l_loop_count).l_c_mileage_allow_payments := amap_entries.C_MILEAGE_ALLOW_PAYMENTS;
5286                     l_ws6_info_tab(l_loop_count).l_c_tax_deducted_payments := amap_entries.C_TAX_DEDUCTED_PAYMENTS;
5287                     l_ws6_info_tab(l_loop_count).l_c_net_allowance       := amap_entries.C_NET_ALLOWANCE;
5288                     l_ws6_info_tab(l_loop_count).l_c_business_miles      := amap_entries.C_BUSINESS_MILES;
5289                     l_ws6_info_tab(l_loop_count).l_c_reimbrs1_rate1    := amap_entries.c_reimbursement_rate1;
5290                     l_ws6_info_tab(l_loop_count).l_c_reimbrs2_rate2    :=  amap_entries.c_reimbursement_rate2;
5291                     l_ws6_info_tab(l_loop_count).l_vehiclecheck           := 'C';
5292                     l_Carvan :='';
5293                 end if;
5294                 l_ws6_info_tab(l_loop_count).l_passen_payments := amap_entries.PASSEN_PAYMENTS;
5295                 l_ws6_info_tab(l_loop_count).l_passenger_bus_miles_amount := amap_entries.PASSENGER_BUS_MILES_AMOUNT;
5296                 l_ws6_info_tab(l_loop_count).l_passen_business_miles := amap_entries.PASSEN_BUSINESS_MILES;
5297                 -- l_ws6_info_tab(l_loop_count).l_amap_entries.Taxable_payments := 0;
5298                 l_mile1_amount  :=0;
5299                 l_mile2_amount :=0;
5300                 if l_ws6_info_tab(l_loop_count).l_c_business_miles > 10000
5301                 then
5302                     l_miles1 := 10000;
5303                     HR_UTILITY.TRaCE('B');
5304                     l_miles2 := l_ws6_info_tab(l_loop_count).l_c_business_miles - 10000;
5305                 else
5306                     HR_UTILITY.TRaCE('C');
5307                     l_miles1 := l_ws6_info_tab(l_loop_count).l_c_business_miles;
5308                     l_miles2 := 0;
5309                 end if;
5310                 HR_UTILITY.TRaCE('D');
5311                 l_mile1_amount := l_ws6_info_tab(l_loop_count).l_c_reimbrs1_rate1 * l_miles1;
5312                 l_mile2_amount := l_ws6_info_tab(l_loop_count).l_c_reimbrs2_rate2 * l_miles2;
5313                 l_ws6_info_tab(l_loop_count).l_bus_mile_1_amount:= to_char(to_number(l_mile1_amount),'FM999,999,990.00');
5314                 HR_UTILITY.TRaCE('E');
5315                 l_ws6_info_tab(l_loop_count).l_bus_mile_2_amount:= to_char(to_number(l_mile2_amount),'FM999,999,990.00');
5316                 l_ws6_info_tab(l_loop_count).l_total_approved_maps:= to_char(to_number((l_mile1_amount+l_mile2_amount)),'FM999,999,990.00');
5317                 HR_UTILITY.TRaCE('EE');
5318                 l_ws6_info_tab(l_loop_count).l_total_payment :=
5319                    greatest(l_ws6_info_tab(l_loop_count).l_c_net_allowance -l_mile1_amount -  l_mile2_amount,0);
5320                 HR_UTILITY.TRaCE('FF');
5321                 l_ws6_info_tab(l_loop_count).l_business_miles_1:= l_miles1;
5322                 l_ws6_info_tab(l_loop_count).l_business_miles_2:= l_miles2;
5323                 --hr_uitility.trace('gg');
5324                 l_ws6_info_tab(l_loop_count).l_taxable_passen_payment :=
5325                    greatest(l_ws6_info_tab(l_loop_count).l_passen_payments -
5326                    l_ws6_info_tab(l_loop_count).l_passenger_bus_miles_amount,0);
5327                 l_ws6_info_tab(l_loop_count).l_total_tax_payment:=
5328                    to_char(to_number((l_ws6_info_tab(l_loop_count).l_total_payment +
5329                    l_ws6_info_tab(l_loop_count).l_taxable_passen_payment)),'FM999,999,990.00');
5330             end loop;
5331         end loop;
5332 end if; --Added end if to fix the EAP bug 9383416
5333 
5334 
5335         for i in 1 .. l_loop_count
5336         loop
5337             if l_loop_count > 1 and i <> 1
5338             then
5339                 l_ws6_info_tab(i).l_passen_payments := 0;
5340                 l_ws6_info_tab(i).l_passen_business_miles :=0;
5341                 l_ws6_info_tab(i).l_passenger_bus_miles_amount :=0;
5342                 l_ws6_info_tab(i).l_taxable_passen_payment :=0;
5343                 -- l_ws6_info_tab(i).l_total_tax_payment := to_char(to_number(l_ws6_info_tab(i).l_total_tax_payment ),'FM999,999,990.00');
5344                 l_ws6_info_tab(i).l_total_tax_payment := to_char(to_number(l_ws6_info_tab(l_loop_count).l_total_payment),'FM999,999,990.00');
5345             end if;
5346         end loop;
5347 
5348 
5349         for i in 1 .. l_loop_count
5350         loop
5351             l_xfdf_str_tab.extend;
5352             l_xfdf_str_tab(i) := '<?xml version = "1.0" encoding = "UTF-8"?>
5353                 <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
5354                 <fields> ' ||
5355                 '<field name="'||'Employer'||'"><value>' ||replace(l_employer_name,'&','&')  || '</value></field> ' ||
5356                 '<field name="'||'Employee'||'"><value>' ||l_full_name || '</value></field>  ' ||
5357                 -- P11D 08/09
5358                 '<field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
5359                 '<field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
5360                 -- P11D 08/09
5361                 '<field name="'||'PAYE_tax'||'"><value>' ||l_emp_ref_no || '</value></field>  ' ||
5362                 '<field name="'||'Works_no'||'"><value>' ||l_employee_number || '</value></field>  ' ||
5363                 --'<field name="'||'Nat_Ins_Num'||'"><value>' ||l_national_ins_no || '</value></field>  ' ||
5364                 '<field name="'||'NI_1'||'"><value>' ||substr(l_national_ins_no,1,1) || '</value></field>  ' ||
5365                 '<field name="'||'NI_2'||'"><value>' ||substr(l_national_ins_no,2,1) || '</value></field>  ' ||
5366                 '<field name="'||'NI_3'||'"><value>' ||substr(l_national_ins_no,3,1) || '</value></field>  ' ||
5367                 '<field name="'||'NI_4'||'"><value>' ||substr(l_national_ins_no,4,1) || '</value></field>  ' ||
5368                 '<field name="'||'NI_5'||'"><value>' ||substr(l_national_ins_no,5,1) || '</value></field>  ' ||
5369                 '<field name="'||'NI_6'||'"><value>' ||substr(l_national_ins_no,6,1) || '</value></field>  ' ||
5370                 '<field name="'||'NI_7'||'"><value>' ||substr(l_national_ins_no,7,1) || '</value></field>  ' ||
5371                 '<field name="'||'NI_8'||'"><value>' ||substr(l_national_ins_no,8,1) || '</value></field>  ' ||
5372                 '<field name="'||'NI_9'||'"><value>' ||substr(l_national_ins_no,9) || '</value></field>  '  ||
5373                 '<field name="'||'Mileage'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_c_mileage_allow_payments),'FM999,999,990.00') || '</value></field>  ' ||
5374                 '<field name="'||'Mileage_2'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_c_tax_deducted_payments),'FM999,999,990.00') || '</value></field>  ' ||
5375                 '<field name="'||'Mileage_3'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_c_net_allowance),'FM999,999,990.00') || '</value></field>  ' ||
5376                 '<field name="'||'VehicleCheck'||'"><value>' || l_ws6_info_tab(i).l_vehiclecheck  || '</value></field>  ' ||
5377                 '<field name="'||'VehicleCheck_C'||'"><value>' || l_ws6_info_tab(i).l_vehiclecheck  || '</value></field>  ' ||
5378                 '<field name="'||'VehicleCheck_M'||'"><value>' || l_ws6_info_tab(i).l_vehiclecheck  || '</value></field>  ' ||
5379                 '<field name="'||'VehicleCheck_B'||'"><value>' || l_ws6_info_tab(i).l_vehiclecheck  || '</value></field>  ' ||
5380                 '<field name="'||'MilesTravelledBox'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_c_business_miles),'FM999,999,990.00')  || '</value></field>  ' ||
5381                 '<field name="'||'Rates'||'"><value>'   || l_ws6_info_tab(i).l_c_reimbrs1_rate1 * 100  || '</value></field>  ' ||
5382                 '<field name="'||'Rates_2'||'"><value>' || l_ws6_info_tab(i).l_c_reimbrs2_rate2 * 100  || '</value></field>  ' ||
5383                 '<field name="'||'Rates_3'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_business_miles_1),'FM999,999,990.00') || '</value></field>  ' ||
5384                 '<field name="'||'Rates_4'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_business_miles_2),'FM999,999,990.00') || '</value></field>  ' ||
5385                 '<field name="'||'4A'||'"><value>' ||l_ws6_info_tab(i).l_bus_mile_1_amount || '</value></field>  ' ||
5386                 '<field name="'||'4B'||'"><value>' ||l_ws6_info_tab(i).l_bus_mile_2_amount || '</value></field>  ' ||
5387                 '<field name="'||'4C'||'"><value>' ||l_ws6_info_tab(i).l_total_approved_maps || '</value></field>  ' ||
5388                 '<field name="'||'Payments'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_passen_payments),'FM999,999,990.00') || '</value></field>  ' ||
5389                 '<field name="'||'Payments_1'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_passen_business_miles),'FM999,999,990.00') || '</value></field>  ' ||
5390                 '<field name="'||'5M'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_passenger_bus_miles_amount),'FM999,999,990.00') || '</value></field>  ' ||
5391                 '<field name="'||'Taxable'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_total_payment),'FM999,999,990.00') || '</value></field>  ' ||
5392                 '<field name="'||'Taxable_1'||'"><value>' ||to_char(to_number(l_ws6_info_tab(i).l_taxable_passen_payment),'FM999,999,990.00') || '</value></field>  ' ||
5393                 '<field name="'||'Taxable_2'||'"><value>' ||l_ws6_info_tab(i).l_total_tax_payment || '</value></field>  ' ||
5394                 '</fields>  </xfdf>';
5395         end loop;
5396 
5397         open l_ret_ref_cursor for
5398         select *
5399         from table (cast(l_xfdf_str_tab as per_gb_xfdftableType));
5400         p_record_num := l_loop_count;
5401 
5402         return l_ret_ref_cursor;
5403    end ;
5404 
5405    /*To fetch no. of successful assignments for a payroll run*/
5406    function fetch_numberof_assignments(p_payroll_action_id Number) return number
5407    is
5408         cursor csr_pactid
5409         is
5410         select count(1)
5411         from  pay_assignment_actions
5412         where payroll_action_id = p_payroll_action_id
5413         and   action_status='C';
5414 
5415         l_pactid number;
5416    begin
5417         open csr_pactid ;
5418         fetch csr_pactid into l_pactid;
5419         close csr_pactid ;
5420         return l_pactid;
5421    end;
5422 
5423    function fetch_summary_xfdf_blob (p_assignment_action_id Number,
5424                                      p_print_Style varchar2) return blob --bug 8241399
5425 				     -- p_print style parameter added to suppress additional blank page
5426    is
5427         l_xfdf_str clob;
5428         l_xfdf_blob_str blob;
5429         l_xfdf_intermetiate_var varchar2(20000);
5430         g_max_line    constant number := 46;
5431         g_desc_length constant number := 41;
5432         g_desc_size   constant number := 0.66;
5433 
5434         cursor csr_summary_entries (p_pactid Number,
5435                                     p_person_id Number,
5436                                     p_emp_ref Varchar2 ,
5437                                     p_emp_name Varchar2)
5438         is
5439         select decode(action_information_category,
5440                       'ASSETS TRANSFERRED','A',
5441                       'PAYMENTS MADE FOR EMP','B',
5442                       'VOUCHERS OR CREDIT CARDS','C',
5443                       'PVT MED TREATMENT OR INSURANCE','I',
5444                       'SERVICES SUPPLIED','K',
5445                       'ASSETS AT EMP DISPOSAL','L',
5446                       'P11D SHARES','M',
5447                       'OTHER ITEMS','N',
5448                       'OTHER ITEMS NON 1A','N',
5449                       'EXPENSES PAYMENTS','0') SECTION_TITLE,
5450                decode(action_information_category,
5451                       'ASSETS TRANSFERRED','Assets Transferred',
5452                       'PAYMENTS MADE FOR EMP','Payments made on behalf of employee',
5453                       'VOUCHERS OR CREDIT CARDS','Vouchers or credit cards',
5454                       'PVT MED TREATMENT OR INSURANCE','Private medical treatment or insurance',
5455                       'SERVICES SUPPLIED','Services Supplied',
5456                       'ASSETS AT EMP DISPOSAL','Assets placed at employee''s disposal',
5457                       'P11D SHARES','Shares',
5458                       'OTHER ITEMS','Other Items',
5459                       'OTHER ITEMS NON 1A','Other Items Non 1A',
5460                       'EXPENSES PAYMENTS','Expenses') SECTION_HEADING,
5461                decode(action_information_category,
5462                       'ASSETS TRANSFERRED',get_lookup_meaning(
5463                                            'GB_ASSET_TYPE',ACTION_INFORMATION6,
5464                                            fnd_date.canonical_to_date(ACTION_INFORMATION4))
5465                                            || ' ' || ACTION_INFORMATION5,
5466                       'PAYMENTS MADE FOR EMP',get_lookup_meaning(
5467                                            'GB_PAYMENTS_MADE',ACTION_INFORMATION6,
5468                                            fnd_date.canonical_to_date(ACTION_INFORMATION4))
5469                                            || ' ' || ACTION_INFORMATION5,
5470                       'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION5,
5471                       'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION10,
5472                       'SERVICES SUPPLIED',ACTION_INFORMATION10,
5473                       'ASSETS AT EMP DISPOSAL',get_lookup_meaning(
5474                                             'GB_ASSETS',ACTION_INFORMATION5,
5475                                             fnd_date.canonical_to_date(ACTION_INFORMATION4))
5476                                             || ' ' || ACTION_INFORMATION6,
5477                       'P11D SHARES','Share Related Benefits',
5478                       'OTHER ITEMS',replace(get_lookup_meaning(
5479                                            'GB_OTHER_ITEMS',ACTION_INFORMATION5,
5480                                            fnd_date.canonical_to_date(ACTION_INFORMATION4))
5481                                            || ' ' || ACTION_INFORMATION6,'&','&'),
5482                       'OTHER ITEMS NON 1A',get_lookup_meaning(
5483                                            'GB_OTHER_ITEMS_NON_1A',ACTION_INFORMATION5,
5484                                            fnd_date.canonical_to_date(ACTION_INFORMATION4))
5485                                            || ' ' || ACTION_INFORMATION6,
5486                       'EXPENSES PAYMENTS',get_lookup_meaning(
5487                                            'GB_EXPENSE_TYPE',ACTION_INFORMATION5,
5488                                            fnd_date.canonical_to_date(ACTION_INFORMATION4))
5489                                            || ' ' || ACTION_INFORMATION9) LINE_DETAIL,
5490                decode(action_information_category,
5491                             'ASSETS TRANSFERRED',ACTION_INFORMATION7,
5492                             'PAYMENTS MADE FOR EMP',null,
5493                             'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION6,
5494                             'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION5,
5495                             'SERVICES SUPPLIED',ACTION_INFORMATION5,
5496                             'ASSETS AT EMP DISPOSAL',ACTION_INFORMATION7,
5497                             'P11D SHARES',null,
5498                             'OTHER ITEMS',ACTION_INFORMATION7,
5499                             'OTHER ITEMS NON 1A',ACTION_INFORMATION7,
5500                             'EXPENSES PAYMENTS',ACTION_INFORMATION6) LINE_COL1,
5501                decode(action_information_category,
5502                             'ASSETS TRANSFERRED',ACTION_INFORMATION8,
5503                             'PAYMENTS MADE FOR EMP',ACTION_INFORMATION7,
5504                             'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION7,
5505                             'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION6,
5506                             'SERVICES SUPPLIED',ACTION_INFORMATION6,
5507                             'ASSETS AT EMP DISPOSAL',ACTION_INFORMATION8,
5508                             'P11D SHARES',null,
5509                             'OTHER ITEMS',ACTION_INFORMATION8,
5510                             'OTHER ITEMS NON 1A',ACTION_INFORMATION8,
5511                             'EXPENSES PAYMENTS',ACTION_INFORMATION7) LINE_COL2,
5512                decode(action_information_category,
5513                             'ASSETS TRANSFERRED',ACTION_INFORMATION9,
5514                             'PAYMENTS MADE FOR EMP',ACTION_INFORMATION8,
5515                             'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION11,
5516                             'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION7,
5517                             'SERVICES SUPPLIED',ACTION_INFORMATION7,
5518                             'ASSETS AT EMP DISPOSAL',ACTION_INFORMATION9,
5519                             'P11D SHARES',decode(ACTION_INFORMATION5,'Y','Yes'),
5520                             'OTHER ITEMS',ACTION_INFORMATION9,
5521                             'OTHER ITEMS NON 1A',ACTION_INFORMATION9,
5522                             'EXPENSES PAYMENTS',ACTION_INFORMATION8) LINE_COL3,
5523                to_char(fnd_date.canonical_to_date(ACTION_INFORMATION3),'DD-MON-YYYY') LINE_START_DATE,
5524                to_char(fnd_date.canonical_to_date(ACTION_INFORMATION4),'DD-MON-YYYY') LINE_END_DATE
5525         from  pay_action_information
5526         where action_information_category in(
5527                             'ASSETS TRANSFERRED',
5528                             'PAYMENTS MADE FOR EMP',
5529                             'VOUCHERS OR CREDIT CARDS',
5530                             'PVT MED TREATMENT OR INSURANCE',
5531                             'SERVICES SUPPLIED',
5532                             'ASSETS AT EMP DISPOSAL',
5533                             'P11D SHARES',
5534                             'OTHER ITEMS',
5535                             'OTHER ITEMS NON 1A',
5536                             'EXPENSES PAYMENTS')
5537         and action_context_type = 'AAP'
5538         and action_context_id in ( select paa.assignment_action_id
5539                                    from  pay_action_information pai_comp,
5540                                          pay_action_information pai_person,
5541                                          pay_assignment_actions paa,
5542                                          pay_payroll_actions ppa
5543                                    where ppa.payroll_action_id = p_pactid
5544                                    and   paa.payroll_action_id = ppa.payroll_action_id
5545                                    and   pai_comp.action_context_id = paa.assignment_action_id
5546                                    and   pai_comp.action_information_category = 'EMEA PAYROLL INFO'
5547                                    and   pai_person.action_context_id = paa.assignment_action_id
5548                                    and   pai_person.action_information_category = 'ADDRESS DETAILS'
5549                                    and   pai_person.action_information14 = 'Employee Address'
5550                                    and   pai_person.action_information1 = to_char(p_person_id)
5551                                    and   pai_comp.action_information6 = p_emp_ref
5552                                    and   pai_comp.action_information7 = p_emp_name)
5553         order by decode(action_information_category,
5554                             'ASSETS TRANSFERRED','A',
5555                             'PAYMENTS MADE FOR EMP','B',
5556                             'VOUCHERS OR CREDIT CARDS','C',
5557                             'PVT MED TREATMENT OR INSURANCE','I',
5558                             'SERVICES SUPPLIED','K',
5559                             'ASSETS AT EMP DISPOSAL','L',
5560                             'P11D SHARES','M',
5561                             'OTHER ITEMS','N',
5562                             'OTHER ITEMS NON 1A','N',
5563                             'EXPENSES PAYMENTS','O') asc,
5564                  decode(action_information_category,
5565                             'ASSETS TRANSFERRED','Assets Transferred',
5566                             'PAYMENTS MADE FOR EMP','Payments made on behalf of employee',
5567                             'VOUCHERS OR CREDIT CARDS','Vouchers or credit cards',
5568                             'PVT MED TREATMENT OR INSURANCE','Private medical treatment or insurance',
5569                             'SERVICES SUPPLIED','Services Supplied',
5570                             'ASSETS AT EMP DISPOSAL','Assets placed at employee''s disposal',
5571                             'P11D SHARES','Shares',
5572                             'OTHER ITEMS','Other Items',
5573                             'OTHER ITEMS NON 1A','Other Items Non 1A',
5574                             'EXPENSES PAYMENTS','Expenses') asc ;
5575 
5576         cursor csr_summary_entries_0405 (p_pactid Number,
5577                                          p_person_id Number,
5578                                          p_emp_ref Varchar2 ,
5579                                          p_emp_name Varchar2)
5580         is
5581         select decode(action_information_category,
5582                             'ASSETS TRANSFERRED','A',
5583                             'PAYMENTS MADE FOR EMP','B',
5584                             'VOUCHERS OR CREDIT CARDS','C',
5585                             'PVT MED TREATMENT OR INSURANCE','I',
5586                             'SERVICES SUPPLIED','K',
5587                             'ASSETS AT EMP DISPOSAL','L',
5588                             'OTHER ITEMS','M',
5589                             'OTHER ITEMS NON 1A','M',
5590                             'EXPENSES PAYMENTS','N') SECTION_TITLE,
5591                decode(action_information_category,
5592                             'ASSETS TRANSFERRED','Assets Transferred',
5593                             'PAYMENTS MADE FOR EMP','Payments made on behalf of employee',
5594                             'VOUCHERS OR CREDIT CARDS','Vouchers or credit cards',
5595                             'PVT MED TREATMENT OR INSURANCE','Private medical treatment or insurance',
5596                             'SERVICES SUPPLIED','Services Supplied',
5597                             'ASSETS AT EMP DISPOSAL','Assets placed at employee''s disposal',
5598                             'OTHER ITEMS','Other Items',
5599                             'OTHER ITEMS NON 1A','Other Items Non 1A',
5600                             'EXPENSES PAYMENTS','Expenses') SECTION_HEADING,
5601                decode(action_information_category,
5602                             'ASSETS TRANSFERRED',get_lookup_meaning(
5603                                                 'GB_ASSET_TYPE', ACTION_INFORMATION6,
5604                                                 fnd_date.canonical_to_date(ACTION_INFORMATION4))
5605                                                  || ' ' || ACTION_INFORMATION5,
5606                             'PAYMENTS MADE FOR EMP',get_lookup_meaning(
5607                                                 'GB_PAYMENTS_MADE', ACTION_INFORMATION6,
5608                                                 fnd_date.canonical_to_date(ACTION_INFORMATION4))
5609                                                  || ' ' || ACTION_INFORMATION5,
5610                             'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION5,
5611                             'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION10,
5612                             'SERVICES SUPPLIED',ACTION_INFORMATION10,
5613                             'ASSETS AT EMP DISPOSAL',get_lookup_meaning(
5614                                                 'GB_ASSETS', ACTION_INFORMATION5,
5615                                                 fnd_date.canonical_to_date(ACTION_INFORMATION4))
5616                                                  || ' ' || ACTION_INFORMATION6,
5617                             'OTHER ITEMS',replace(get_lookup_meaning(
5618                                                 'GB_OTHER_ITEMS',ACTION_INFORMATION5,
5619                                                 fnd_date.canonical_to_date(ACTION_INFORMATION4))
5620                                           || ' ' || ACTION_INFORMATION6,'&','&'),
5621                             'OTHER ITEMS NON 1A',get_lookup_meaning(
5622                                                 'GB_OTHER_ITEMS_NON_1A', ACTION_INFORMATION5,
5623                                                 fnd_date.canonical_to_date(ACTION_INFORMATION4))
5624                                           || ' ' || ACTION_INFORMATION6,
5625                             'EXPENSES PAYMENTS',get_lookup_meaning(
5626                                                 'GB_EXPENSE_TYPE',ACTION_INFORMATION5,
5627                                                 fnd_date.canonical_to_date(ACTION_INFORMATION4))
5628                                           || ' ' || ACTION_INFORMATION9) LINE_DETAIL,
5629                decode(action_information_category,
5630                             'ASSETS TRANSFERRED',ACTION_INFORMATION7,
5631                             'PAYMENTS MADE FOR EMP',null,
5632                             'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION6,
5633                             'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION5,
5634                             'SERVICES SUPPLIED',ACTION_INFORMATION5,
5635                             'ASSETS AT EMP DISPOSAL',to_char(to_number(ACTION_INFORMATION8)+to_number(ACTION_INFORMATION9)), --Changed for bug 8204969
5636                             'OTHER ITEMS',ACTION_INFORMATION7,
5637                             'OTHER ITEMS NON 1A',ACTION_INFORMATION7,
5638                             'EXPENSES PAYMENTS',ACTION_INFORMATION6) LINE_COL1,
5639                decode(action_information_category,
5640                             'ASSETS TRANSFERRED',ACTION_INFORMATION8,
5641                             'PAYMENTS MADE FOR EMP',ACTION_INFORMATION7,
5642                             'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION7,
5643                             'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION6,
5644                             'SERVICES SUPPLIED',ACTION_INFORMATION6,
5645                             'ASSETS AT EMP DISPOSAL',ACTION_INFORMATION8,
5646                             'OTHER ITEMS',ACTION_INFORMATION8,
5647                             'OTHER ITEMS NON 1A',ACTION_INFORMATION8,
5648                             'EXPENSES PAYMENTS',ACTION_INFORMATION7) LINE_COL2,
5649                decode(action_information_category,
5650                             'ASSETS TRANSFERRED',ACTION_INFORMATION9,
5651                             'PAYMENTS MADE FOR EMP',ACTION_INFORMATION8,
5652                             'VOUCHERS OR CREDIT CARDS',ACTION_INFORMATION11,
5653                             'PVT MED TREATMENT OR INSURANCE',ACTION_INFORMATION7,
5654                             'SERVICES SUPPLIED',ACTION_INFORMATION7,
5655                             'ASSETS AT EMP DISPOSAL',ACTION_INFORMATION9,
5656                             'OTHER ITEMS',ACTION_INFORMATION9,
5657                             'OTHER ITEMS NON 1A',ACTION_INFORMATION9,
5658                             'EXPENSES PAYMENTS',ACTION_INFORMATION8) LINE_COL3,
5659                to_char(fnd_date.canonical_to_date(ACTION_INFORMATION3),'DD-MON-YYYY') LINE_START_DATE,
5660                to_char(fnd_date.canonical_to_date(ACTION_INFORMATION4),'DD-MON-YYYY') LINE_END_DATE
5661                from  pay_action_information
5662                where action_information_category in(
5663                             'ASSETS TRANSFERRED',
5664                             'PAYMENTS MADE FOR EMP',
5665                             'VOUCHERS OR CREDIT CARDS',
5666                             'PVT MED TREATMENT OR INSURANCE',
5667                             'SERVICES SUPPLIED',
5668                             'ASSETS AT EMP DISPOSAL',
5669                             'OTHER ITEMS',
5670                             'OTHER ITEMS NON 1A',
5671                             'EXPENSES PAYMENTS')
5672                and action_context_type = 'AAP'
5673                and action_context_id in ( select paa.assignment_action_id
5674                                           from  pay_action_information pai_comp,
5675                                                 pay_action_information pai_person,
5676                                                 pay_assignment_actions paa,
5677                                                 pay_payroll_actions ppa
5678                                           where ppa.payroll_action_id = p_pactid
5679                                           and   paa.payroll_action_id = ppa.payroll_action_id
5680                                           and   pai_comp.action_context_id = paa.assignment_action_id
5681                                           and   pai_comp.action_information_category = 'EMEA PAYROLL INFO'
5682                                           and   pai_person.action_context_id = paa.assignment_action_id
5683                                           and   pai_person.action_information_category = 'ADDRESS DETAILS'
5684                                           and   pai_person.action_information14 = 'Employee Address'
5685                                           and   pai_person.action_information1 = to_char(p_person_id)
5686                                           and   pai_comp.action_information6 = p_emp_ref
5687                                           and   pai_comp.action_information7 = p_emp_name)
5688                order by
5689                      decode(action_information_category,
5690                             'ASSETS TRANSFERRED','A',
5691                             'PAYMENTS MADE FOR EMP','B',
5692                             'VOUCHERS OR CREDIT CARDS','C',
5693                             'PVT MED TREATMENT OR INSURANCE','I',
5694                             'SERVICES SUPPLIED','K',
5695                             'ASSETS AT EMP DISPOSAL','L',
5696                             'OTHER ITEMS','M',
5697                             'OTHER ITEMS NON 1A','M',
5698                             'EXPENSES PAYMENTS','N') asc,
5699                     decode(action_information_category,
5700                             'ASSETS TRANSFERRED','Assets Transferred',
5701                             'PAYMENTS MADE FOR EMP','Payments made on behalf of employee',
5702                             'VOUCHERS OR CREDIT CARDS','Vouchers or credit cards',
5703                             'PVT MED TREATMENT OR INSURANCE','Private medical treatment or insurance',
5704                             'SERVICES SUPPLIED','Services Supplied',
5705                             'ASSETS AT EMP DISPOSAL','Assets placed at employee''s disposal',
5706                             'OTHER ITEMS','Other Items',
5707                             'OTHER ITEMS NON 1A','Other Items Non 1A',
5708                             'EXPENSES PAYMENTS','Expenses') asc ;
5709         l_emp_ref_no varchar2(150);
5710         l_employer_name varchar2(150);
5711         l_full_name varchar2(150);
5712         l_sur_name varchar2(150); --  P11D 08/09
5713         l_fore_name varchar2(150); -- P11D 08/09
5714         l_national_ins_no varchar2(150);
5715         l_employee_number varchar2(150);
5716         l_pactid number;
5717         l_person_id number;
5718         l_loop_count Number;
5719         l_tot_col1 Number;
5720         l_tot_col2 Number;
5721         l_tot_col3 Number;
5722         l_prev_section  varchar2(150);
5723         l_rep_run   VARCHAR2(10);
5724         l_line_count number;
5725         l_temp       number;
5726         l_odd_page   boolean;
5727 
5728         function get_desc_line(p_desc varchar2) return number is
5729            l_length number;
5730            l_offset number  := 0;
5731            l_word   varchar2(100);
5732            x        number := 1;
5733            l_number number := 0;
5734            l_count  number := 0;
5735            l_ret    number := 1;
5736         begin
5737            if length(p_desc) < g_desc_length then
5738               l_ret := 1;
5739            else
5740               l_length := length(p_desc);
5741               loop
5742                  l_number := l_number+1;
5743                  l_offset := instr(p_desc, ' ', x, 1);
5744                  if l_offset > 0 then
5745                     l_word := substr(p_desc, x, l_offset-x);
5746                     if length(l_word) < g_desc_length  then
5747                         if length(l_word) + l_count > g_desc_length then
5748                            l_count := length(l_word) + 1;
5749                            l_ret := l_ret + 1;
5750                         else
5751                            l_count := l_count + length(l_word) + 1;
5752                         end if;
5753                     else
5754                         l_ret := l_ret + ceil(length(l_word)/g_desc_length);
5755                         l_count := length(l_word) - floor(length(l_word)/g_desc_length) * g_desc_length + 1;
5756                     end if;
5757                  else
5758                     l_word := substr(p_desc, x);
5759                     if length(l_word) < g_desc_length  then
5760                         if length(l_word) + l_count > g_desc_length then
5761                            l_count := length(l_word) + 1;
5762                            l_ret := l_ret + 1;
5763                         else
5764                            l_count := l_count + length(l_word) + 1;
5765                         end if;
5766                     else
5767                         l_ret := l_ret + ceil(length(l_word)/g_desc_length);
5768                         l_count := length(l_word) - floor(length(l_word)/g_desc_length) * g_desc_length + 1;
5769                     end if;
5770                     exit;
5771                  end if;
5772                  x := l_offset+1;
5773               end loop;
5774            end if;
5775            return l_ret;
5776         end;
5777 
5778         procedure set_line_desc(p_section_heading  varchar2) is
5779              l_line_desc varchar2(1000);
5780         begin
5781              /* Reference to Share can be removed when do work for P11D 05/06 */
5782              if p_section_heading = 'Shares'
5783              then
5784                  -- we need blank line as per spec
5785                  l_line_desc := ' <field name="'||'COL1_SDATE'||'"><value>' ||'Start Date'|| '</value></field>  ' ||
5786                  '<field name="'||'COL1_EDATE'||'"><value>' ||'End Date'|| '</value></field>  ' ||
5787                  '<field name="'||'LINE_DESC'||'"><value>' ||null || '</value></field>  ' ||
5788                  '<field name="'||'COL1_HEADING'||'"><value>' ||null || '</value></field>  ' ||
5789                  '<field name="'||'COL2_HEADING'||'"><value>' ||null || '</value></field>  ' ||
5790                  '<field name="'||'COL3_HEADING'||'"><value>' ||null || '</value></field>  ' ;
5791              elsif p_section_heading = 'Payments made on behalf of employee' then
5792                  l_line_desc := ' <field name="'||'COL1_SDATE'||'"><value>' ||'Start Date'|| '</value></field>  ' ||
5793                  '<field name="'||'COL1_EDATE'||'"><value>' ||'End Date'|| '</value></field>  ' ||
5794                  '<field name="'||'LINE_DESC'||'"><value>' ||'Description' || '</value></field>  ' ||
5795                  '<field name="'||'COL1_HEADING'||'"><value>' ||null || '</value></field>  ' ||
5796                  '<field name="'||'COL2_HEADING'||'"><value>' ||'Value' || '</value></field>  ' ||
5797                  '<field name="'||'COL3_HEADING'||'"><value>' ||'Tax Notional' || '</value></field>  ' ;
5798              elsif p_section_heading = 'Other Items Non 1A' then
5799                  l_line_desc := ' <field name="'||'COL1_SDATE'||'"><value>' ||'Start Date'|| '</value></field>  ' ||
5800                  '<field name="'||'COL1_EDATE'||'"><value>' ||'End Date'|| '</value></field>  ' ||
5801                  '<field name="'||'LINE_DESC'||'"><value>' ||'Description Non 1A' || '</value></field>  ' ||
5802                  '<field name="'||'COL1_HEADING'||'"><value>' ||'Value' || '</value></field>  ' ||
5803                  '<field name="'||'COL2_HEADING'||'"><value>' ||'Made Good' || '</value></field>  ' ||
5804                  '<field name="'||'COL3_HEADING'||'"><value>' ||'Benefit' || '</value></field>  ' ;
5805              elsif p_section_heading = 'Assets placed at employee''s disposal' then
5806                  l_line_desc := ' <field name="'||'COL1_SDATE'||'"><value>' ||'Start Date'|| '</value></field>  ' ||
5807                  '<field name="'||'COL1_EDATE'||'"><value>' ||'End Date'|| '</value></field>  ' ||
5808                  '<field name="'||'LINE_DESC'||'"><value>' ||'Description' || '</value></field>  ' ||
5809                  '<field name="'||'COL1_HEADING'||'"><value>' ||'Annual Value' || '</value></field>  ' ||
5810                  '<field name="'||'COL2_HEADING'||'"><value>' ||'Made Good' || '</value></field>  ' ||
5811                  '<field name="'||'COL3_HEADING'||'"><value>' ||'Benefit' || '</value></field>  ' ;
5812              else
5813                  l_line_desc := ' <field name="'||'COL1_SDATE'||'"><value>' ||'Start Date'|| '</value></field>  ' ||
5814                  '<field name="'||'COL1_EDATE'||'"><value>' ||'End Date'|| '</value></field>  ' ||
5815                  '<field name="'||'LINE_DESC'||'"><value>' ||'Description' || '</value></field>  ' ||
5816                  '<field name="'||'COL1_HEADING'||'"><value>' ||'Value' || '</value></field>  ' ||
5817                  '<field name="'||'COL2_HEADING'||'"><value>' ||'Made Good' || '</value></field>  ' ||
5818                  '<field name="'||'COL3_HEADING'||'"><value>' ||'Benefit' || '</value></field>  ' ;
5819              end if;
5820              dbms_lob.writeAppend( l_xfdf_str, length(l_line_desc) ,l_line_desc );
5821         end;
5822 
5823         procedure add_total_lines is
5824         begin
5825              if  l_prev_section = 'Shares'
5826              then
5827                   -- then we do print totals
5828                  null;
5829              elsif l_prev_section = 'Payments made on behalf of employee'
5830              then
5831                  l_xfdf_intermetiate_var :=
5832                        '<field name="'||'LINE_TOTAL'||'"><value>' ||'Totals' || '</value></field>  ' ||
5833                        '<field name="'||'TOT_COL1'||'"><value>' ||l_TOT_COL1 || '</value></field>  ' ||
5834                        '<field name="'||'TOT_COL2'||'"><value>' ||to_char(to_number(nvl(l_TOT_COL2,0)),'FM999,999,990.00') || '</value></field>  ' ||
5835                        '<field name="'||'TOT_COL3'||'"><value>' ||to_char(to_number(nvl(l_TOT_COL3,0)),'FM999,999,990.00') || '</value></field>  ' ;
5836                  dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5837              else
5838                  l_xfdf_intermetiate_var :=
5839                  ' <field name="'||'LINE_TOTAL'||'"><value>' ||'Totals' || '</value></field>  ' ||
5840                  ' <field name="'||'TOT_COL1'||'"><value>' ||to_char(to_number(nvl(l_TOT_COL1,0)),'FM999,999,990.00') || '</value></field>  ' ||
5841                  ' <field name="'||'TOT_COL2'||'"><value>' ||to_char(to_number(nvl(l_TOT_COL2,0)),'FM999,999,990.00') || '</value></field>  ' ||
5842                  ' <field name="'||'TOT_COL3'||'"><value>' ||to_char(to_number(nvl(l_TOT_COL3,0)),'FM999,999,990.00') || '</value></field>  ' ;
5843                  dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5844              end if;
5845         end;
5846 
5847         procedure add_detail_line(p_SECTION_HEADING varchar2,
5848                                   p_LINE_DETAIL varchar2,
5849                                   p_LINE_COL1 varchar2,
5850                                   p_LINE_COL2 varchar2,
5851                                   p_LINE_COL3 varchar2,
5852                                   p_LINE_SDATE varchar2,
5853                                   p_LINE_EDATE varchar2) is
5854         begin
5855              if p_SECTION_HEADING = 'Payments made on behalf of employee' or
5856                 p_SECTION_HEADING ='Shares'
5857              then
5858                  -- no defaulting of values reqd
5859                  if p_SECTION_HEADING = 'Payments made on behalf of employee'
5860                  then
5861                      l_xfdf_intermetiate_var :=
5862                      '<field name="'||'LINE_START_D'||'"><value>' ||p_LINE_SDATE   || '</value></field>  ' ||
5863                      '<field name="'||'LINE_END_D'||'"><value>'   ||p_LINE_EDATE   || '</value></field>  ' ||
5864                      '<field name="'||'LINE_DETAIL'||'"><value>' ||p_LINE_DETAIL || '</value></field>  ' ||
5865                      '<field name="'||'LINE_COL1'||'"><value>'   ||null   || '</value></field>  ' ||
5866                      '<field name="'||'LINE_COL2'||'"><value>'   ||to_char(to_number(nvl(p_LINE_COL2,0)),'FM999,999,990.00')   || '</value></field>  ' ||
5867                      '<field name="'||'LINE_COL3'||'"><value>'   ||to_char(to_number(nvl(p_LINE_COL3,0)),'FM999,999,990.00')   || '</value></field>  ' ;
5868                      dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5869                  else -- this mean it is shares entry
5870                      l_xfdf_intermetiate_var :=
5871                      '<field name="'||'LINE_START_D'||'"><value>' ||p_LINE_SDATE   || '</value></field>  ' ||
5872                      '<field name="'||'LINE_END_D'||'"><value>'   ||p_LINE_EDATE   || '</value></field>  ' ||
5873                      '<field name="'||'LINE_DETAIL'||'"><value>' ||p_LINE_DETAIL || '</value></field>  ' ||
5874                      '<field name="'||'LINE_COL1'||'"><value>'   ||p_LINE_COL1   || '</value></field>  ' ||
5875                      '<field name="'||'LINE_COL2'||'"><value>'   ||p_LINE_COL2   || '</value></field>  ' ||
5876                      '<field name="'||'LINE_COL3'||'"><value>'   ||p_LINE_COL3   || '</value></field>  ' ;
5877                      dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5878                  end if;
5879              else
5880                  l_xfdf_intermetiate_var :=
5881                  '<field name="'||'LINE_START_D'||'"><value>' ||p_LINE_SDATE   || '</value></field>  ' ||
5882                  '<field name="'||'LINE_END_D'||'"><value>'   ||p_LINE_EDATE   || '</value></field>  ' ||
5883                  '<field name="'||'LINE_DETAIL'||'"><value>' ||p_LINE_DETAIL || '</value></field>  ' ;
5884                  dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5885                  if nvl(p_LINE_COL1,nvl(p_LINE_COL3,0)) =  0
5886                  then
5887                      l_xfdf_intermetiate_var :=
5888                      '<field name="'||'LINE_COL1'||'"><value>'||to_char(to_number(nvl(p_LINE_COL3,0)),'FM999,999,990.00') || '</value></field>  ' ;
5889                      dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5890                  else
5891                      l_xfdf_intermetiate_var :=
5892                      '<field name="'||'LINE_COL1'||'"><value>'   ||to_char(to_number(nvl(p_LINE_COL1,nvl(p_LINE_COL3,0))),'FM999,999,990.00')   || '</value></field>  ' ;
5893                      dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5894                  end if;
5895                  l_xfdf_intermetiate_var :=
5896                  '<field name="'||'LINE_COL2'||'"><value>'   ||to_char(to_number(nvl(p_LINE_COL2,0)),'FM999,999,990.00')   || '</value></field>  ' ||
5897                  '<field name="'||'LINE_COL3'||'"><value>'   ||to_char(to_number(nvl(p_LINE_COL3,0)),'FM999,999,990.00')   || '</value></field>  ' ;
5898                  dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5899             end if;
5900         end;
5901    begin
5902         --hr_utility.trace_on(null,'KT');
5903         hr_utility.trace('calling get_employer_details');
5904         get_employer_details(p_assignment_action_id,
5905                              l_emp_ref_no,
5906                               l_employer_name);
5907         hr_utility.trace('calling get_employee_details');
5908         get_employee_details(p_assignment_action_id,
5909                              l_full_name,
5910                              l_national_ins_no,
5911                              l_employee_number);
5912         -- P11D 08/09
5913         -- Fetch sur and fore names
5914         get_sur_fore_names(p_assignment_action_id,
5915                            l_sur_name,
5916                            l_fore_name);
5917         -- P11D 08/09
5918         l_pactid := get_pactid(p_assignment_action_id);
5919         l_person_id := get_person_id (p_assignment_action_id );
5920         l_employee_number := get_assignment_number(p_assignment_action_id, true, l_person_id, l_emp_ref_no);
5921         l_loop_count := 0;
5922         -- hr_utility.trace_on(null,'SUMM');
5923         hr_utility.trace('l_pactid ' || l_pactid);
5924         hr_utility.trace('l_person_id '|| l_person_id);
5925         hr_utility.trace('l_emp_ref_no '|| l_emp_ref_no);
5926         hr_utility.trace('l_employer_name '|| l_employer_name);
5927         hr_utility.trace('opening the cursor');
5928         l_tot_col1 := null;
5929         l_tot_col2 := null;
5930         l_tot_col3 := null;
5931         l_prev_section := null;
5932         hr_utility.trace('l_prev_section ' || l_prev_section);
5933         dbms_lob.createtemporary(l_xfdf_str,false,DBMS_LOB.CALL);
5934         dbms_lob.open(l_xfdf_str,dbms_lob.lob_readwrite);
5935 
5936 
5937         PAY_GB_P11D_ARCHIVE_SS.get_parameters(
5938          p_payroll_action_id => l_pactid,
5939          p_token_name        => 'Rep_Run',
5940          p_token_value       => l_rep_run);
5941         l_loop_count := 0;
5942 
5943         l_odd_page := TRUE;
5944         l_line_count := 0;
5945         if l_odd_page then
5946            hr_utility.trace('Current page is odd page');
5947         else
5948            hr_utility.trace('Current page is even page');
5949         end if;
5950         if to_number(l_rep_run) < 2005
5951         then
5952             for summary_entries in  csr_summary_entries(l_pactid,
5953                                                         l_person_id,
5954                                                         l_emp_ref_no,
5955                                                         l_employer_name)
5956             loop
5957                 l_loop_count := l_loop_count+1;
5958                 if l_loop_count = 1
5959                 then
5960                     -- setting the emp details page title...
5961                     l_xfdf_intermetiate_var  := '<?xml version = "1.0" encoding = "UTF-8"?>
5962                     <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
5963                     <fields> ' ||
5964                     '<field name="'||'BEN_START_YEAR'||'"><value>' ||(to_number(l_rep_run) - 1) || '</value></field> ' ||
5965                     '<field name="'||'BEN_END_YEAR'||'"><value>' ||to_number(l_rep_run)  || '</value></field> ' ||
5966                     '<field name="'||'EMPLOYERS_NAME'||'"><value>' ||replace(l_employer_name,'&','&')       || '</value></field> ' ||
5967                     '<field name="'||'FULL_NAME'||'"><value>' ||l_full_name || '</value></field>  ' ||
5968                     '<field name="'||'EMPLOYERS_REF_NO'||'"><value>' ||l_emp_ref_no || '</value></field>  ' ||
5969                     '<field name="'||'EMPLOYEE_NUMBER'||'"><value>' ||l_employee_number || '</value></field>  ' ||
5970                     '<field name="'||'NATIONAL_INS_NO'||'"><value>' ||l_national_ins_no || '</value></field>  ' ;
5971                     dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5972                     -- add new section heading and section title
5973                     if summary_entries.SECTION_HEADING = 'Other Items Non 1A'
5974                     then
5975                         l_xfdf_intermetiate_var:=
5976                         '<field name="'||'SECTION_TITLE'||'"><value>' ||summary_entries.SECTION_TITLE || '</value></field>  ' ||
5977                         '<field name="'||'SECTION_HEADING'||'"><value>' ||'Other Items' || '</value></field>  ' ;
5978                         dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5979                     else
5980                         l_xfdf_intermetiate_var:=
5981                         '<field name="'||'SECTION_TITLE'||'"><value>' ||summary_entries.SECTION_TITLE || '</value></field>  ' ||
5982                         '<field name="'||'SECTION_HEADING'||'"><value>' ||summary_entries.SECTION_HEADING || '</value></field>  ' ;
5983                         dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
5984                     end if;
5985                     -- add new line desc
5986                     set_line_desc(summary_entries.SECTION_HEADING);
5987                     hr_utility.trace('l_loop_count '  ||l_loop_count);
5988                     l_prev_section := summary_entries.SECTION_HEADING;
5989                     l_line_count := l_line_count + 2;
5990                 end if;
5991                 hr_utility.trace('l_prev_section ' || l_prev_section);
5992                 hr_utility.trace('summary_entries.SECTION_HEADING ' || summary_entries.SECTION_HEADING);
5993                 if l_prev_section = summary_entries.SECTION_HEADING
5994                 then
5995                     -- we just add the line details
5996                     add_detail_line(summary_entries.SECTION_HEADING,
5997                                     summary_entries.LINE_DETAIL,
5998                                     summary_entries.LINE_COL1,
5999                                     summary_entries.LINE_COL2,
6000                                     summary_entries.LINE_COL3,
6001                                     summary_entries.LINE_START_DATE,
6002                                     summary_entries.LINE_END_DATE);
6003                     l_temp := get_desc_line(summary_entries.LINE_DETAIL);
6004                     hr_utility.trace(summary_entries.LINE_DETAIL || ' : ' || l_temp);
6005                     if (l_temp > 1) then
6006                         l_line_count := l_line_count + (l_temp * g_desc_size);
6007                     else
6008                         l_line_count := l_line_count + 1;
6009                     end if;
6010                     -- we need to sum the totals
6011                     if summary_entries.SECTION_HEADING <> 'Shares'
6012                     then
6013                         if summary_entries.SECTION_HEADING ='Payments made on behalf of employee'
6014                         then
6015                             l_TOT_COL1 := null;
6016                             l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6017                             l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6018                         else
6019                             l_TOT_COL1 := nvl(l_TOT_COL1,0) + nvl(summary_entries.LINE_COL1,nvl(summary_entries.LINE_COL3,0));
6020                             l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6021                             l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6022                         end if;
6023                     end if;
6024                 else
6025                     hr_utility.trace('l_loop_count ' || l_loop_count);
6026                     add_total_lines;
6027                     l_line_count := l_line_count + 1;
6028                     hr_utility.trace('adding new section heading and title');
6029                     -- add new section heading and title
6030                     -- we do not add heading if the SECTION_HEADING is Other Items Non 1A
6031                     -- and prev was Other Items
6032                     if l_prev_section = 'Other Items' and  summary_entries.SECTION_HEADING = 'Other Items Non 1A'
6033                     then
6034                         -- we do not add section title and section heading
6035                         null;
6036                     else
6037                         l_xfdf_intermetiate_var  :=
6038                             '<field name="'||'SECTION_TITLE'||'"><value>' ||summary_entries.SECTION_TITLE || '</value></field>  ' ||
6039                             '<field name="'||'SECTION_HEADING'||'"><value>' ||summary_entries.SECTION_HEADING || '</value></field>  ' ;
6040                         dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6041                         l_line_count := l_line_count + 1;
6042                     end if;
6043                     -- add new line desc
6044                     set_line_desc(summary_entries.SECTION_HEADING);
6045                     l_line_count := l_line_count + 1;
6046                     -- add the line details
6047                     add_detail_line(summary_entries.SECTION_HEADING,
6048                                     summary_entries.LINE_DETAIL,
6049                                     summary_entries.LINE_COL1,
6050                                     summary_entries.LINE_COL2,
6051                                     summary_entries.LINE_COL3,
6052                                     summary_entries.LINE_START_DATE,
6053                                     summary_entries.LINE_END_DATE);
6054                     l_temp := get_desc_line(summary_entries.LINE_DETAIL);
6055                     hr_utility.trace(summary_entries.LINE_DETAIL || ' : ' || l_temp);
6056                     if (l_temp > 1) then
6057                         l_line_count := l_line_count + (l_temp * g_desc_size);
6058                     else
6059                         l_line_count := l_line_count + 1;
6060                     end if;
6061                     -- resetting tot cols
6062                     l_tot_col1 := null;
6063                     l_tot_col2 := null;
6064                     l_tot_col3 := null;
6065                     l_prev_section := summary_entries.SECTION_HEADING;
6066                     -- we need to sum the totals
6067                     if summary_entries.SECTION_HEADING <> 'Shares'
6068                     then
6069                         if summary_entries.SECTION_HEADING ='Payments made on behalf of employee'
6070                         then
6071                             l_TOT_COL1 := null;
6072                             l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6073                             l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6074                         else
6075                             l_TOT_COL1 := nvl(l_TOT_COL1,0) +
6076                                           nvl(summary_entries.LINE_COL1,nvl(summary_entries.LINE_COL3,0));
6077                             l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6078                             l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6079                         end if;
6080                     end if;
6081                 end if;
6082                 if l_line_count > g_max_line then
6083                    l_odd_page := not(l_odd_page);
6084                    l_line_count := l_line_count - g_max_line;
6085                 end if;
6086             end loop;
6087         else
6088             /* This code is for year 04/05 onwards */
6089             for summary_entries in  csr_summary_entries_0405(l_pactid,
6090                                                              l_person_id,
6091                                                              l_emp_ref_no,
6092                                                              l_employer_name)
6093             loop
6094                 l_loop_count := l_loop_count+1;
6095                 if l_loop_count = 1
6096                 then
6097                     -- setting the emp details page title...
6098                     l_xfdf_intermetiate_var  := '<?xml version = "1.0" encoding = "UTF-8"?>
6099                         <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
6100                         <fields> ' ||
6101                         '<field name="'||'BEN_START_YEAR'||'"><value>' ||(to_number(l_rep_run) - 1) || '</value></field> ' ||
6102                         '<field name="'||'BEN_END_YEAR'||'"><value>' ||to_number(l_rep_run)  || '</value></field> ' ||
6103                         '<field name="'||'EMPLOYERS_NAME'||'"><value>' ||replace(l_employer_name,'&','&')       || '</value></field> ' ||
6104                         '<field name="'||'FULL_NAME'||'"><value>' ||l_full_name || '</value></field>  ' ||
6105                         -- P11D 08/09
6106                         ' <field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
6107                         ' <field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
6108                         -- P11D 08/09
6109                         '<field name="'||'EMPLOYERS_REF_NO'||'"><value>' ||l_emp_ref_no || '</value></field>  ' ||
6110                         '<field name="'||'EMPLOYEE_NUMBER'||'"><value>' ||l_employee_number || '</value></field>  ' ||
6111                         '<field name="'||'NATIONAL_INS_NO'||'"><value>' ||l_national_ins_no || '</value></field>  ' ;
6112                     dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6113                     -- add new section heading and section title
6114                     if summary_entries.SECTION_HEADING = 'Other Items Non 1A'
6115                     then
6116                         l_xfdf_intermetiate_var:=
6117                             '<field name="'||'SECTION_TITLE'||'"><value>' ||summary_entries.SECTION_TITLE || '</value></field>  ' ||
6118                             '<field name="'||'SECTION_HEADING'||'"><value>' ||'Other Items' || '</value></field>  ' ;
6119                         dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6120                     else
6121                         l_xfdf_intermetiate_var:=
6122                            '<field name="'||'SECTION_TITLE'||'"><value>' ||summary_entries.SECTION_TITLE || '</value></field>  ' ||
6123                            '<field name="'||'SECTION_HEADING'||'"><value>' ||summary_entries.SECTION_HEADING || '</value></field>  ' ;
6124                         dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6125                     end if;
6126                     -- add new line desc
6127 
6128                     set_line_desc(summary_entries.SECTION_HEADING);
6129                     hr_utility.trace('l_loop_count '  ||l_loop_count);
6130                     l_prev_section := summary_entries.SECTION_HEADING;
6131                     l_line_count := l_line_count + 2;
6132                     hr_utility.trace('Writing line_desc + header : ' || l_line_count);
6133                 end if;
6134                 hr_utility.trace('l_prev_section ' || l_prev_section);
6135                 hr_utility.trace('summary_entries.SECTION_HEADING ' || summary_entries.SECTION_HEADING);
6136                 if l_prev_section = summary_entries.SECTION_HEADING
6137                 then
6138                     -- we just add the line details
6139                     add_detail_line(summary_entries.SECTION_HEADING,
6140                                     summary_entries.LINE_DETAIL,
6141                                     summary_entries.LINE_COL1,
6142                                     summary_entries.LINE_COL2,
6143                                     summary_entries.LINE_COL3,
6144                                     summary_entries.LINE_START_DATE,
6145                                     summary_entries.LINE_END_DATE);
6146                     l_temp := get_desc_line(summary_entries.LINE_DETAIL);
6147                     hr_utility.trace(summary_entries.LINE_DETAIL || ' : ' || l_temp);
6148                     if (l_temp > 1) then
6149                         l_line_count := l_line_count + (l_temp * g_desc_size);
6150                     else
6151                         l_line_count := l_line_count + 1;
6152                     end if;
6153                     hr_utility.trace('Writing line details : ' || l_line_count);
6154                     -- we need to sum the totals
6155                     if summary_entries.SECTION_HEADING <> 'Shares'
6156                     then
6157                         if summary_entries.SECTION_HEADING ='Payments made on behalf of employee'
6158                         then
6159                             l_TOT_COL1 := null;
6160                             l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6161                             l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6162                         else
6163                             l_TOT_COL1 := nvl(l_TOT_COL1,0) +
6164                                 nvl(summary_entries.LINE_COL1,nvl(summary_entries.LINE_COL3,0));
6165                             l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6166                             l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6167                         end if;
6168                     end if;
6169                 else
6170                     hr_utility.trace('l_loop_count ' || l_loop_count);
6171                     add_total_lines;
6172                     l_line_count := l_line_count + 1;
6173                     hr_utility.trace('Writing total line : ' || l_line_count);
6174                     hr_utility.trace('adding new section heading and title');
6175                     -- add new section heading and title
6176                     -- we do not add heading if the SECTION_HEADING is Other Items Non 1A
6177                     -- and prev was Other Items
6178                     if l_prev_section = 'Other Items' and
6179                        summary_entries.SECTION_HEADING = 'Other Items Non 1A'
6180                     then
6181                         -- we do not add section title and section heading
6182                         null;
6183                     else
6184                         l_xfdf_intermetiate_var  :=
6185                            '<field name="'||'SECTION_TITLE'||'"><value>' ||summary_entries.SECTION_TITLE || '</value></field>  ' ||
6186                            '<field name="'||'SECTION_HEADING'||'"><value>' ||summary_entries.SECTION_HEADING || '</value></field>  ' ;
6187                         dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6188                         l_line_count := l_line_count + 1;
6189                         hr_utility.trace('Writing header : ' || l_line_count);
6190                     end if;
6191                     -- add new line desc
6192                     set_line_desc(summary_entries.SECTION_HEADING);
6193                     l_line_count := l_line_count + 1;
6194                     hr_utility.trace('Writing line_desc : ' || l_line_count);
6195                     -- add the line details
6196                     add_detail_line(summary_entries.SECTION_HEADING,
6197                                     summary_entries.LINE_DETAIL,
6198                                     summary_entries.LINE_COL1,
6199                                     summary_entries.LINE_COL2,
6200                                     summary_entries.LINE_COL3,
6201                                     summary_entries.LINE_START_DATE,
6202                                     summary_entries.LINE_END_DATE);
6203                     l_temp := get_desc_line(summary_entries.LINE_DETAIL);
6204                     hr_utility.trace(summary_entries.LINE_DETAIL || ' : ' || l_temp);
6205                     if (l_temp > 1) then
6206                         l_line_count := l_line_count + (l_temp * g_desc_size);
6207                     else
6208                         l_line_count := l_line_count + 1;
6209                     end if;
6210                     hr_utility.trace('Writing line details : ' || l_line_count);
6211                     -- resetting tot cols
6212                     l_tot_col1 := null;
6213                     l_tot_col2 := null;
6214                     l_tot_col3 := null;
6215                     l_prev_section := summary_entries.SECTION_HEADING;
6216                     -- we need to sum the totals
6217                     -- no need to check for Shares
6218                     /* if summary_entries.SECTION_HEADING <> 'Shares' then */
6219                     if  summary_entries.SECTION_HEADING ='Payments made on behalf of employee'
6220                     then
6221                         l_TOT_COL1 := null;
6222                         l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6223                         l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6224                     else
6225                         l_TOT_COL1 := nvl(l_TOT_COL1,0) +
6226                                 nvl(summary_entries.LINE_COL1,nvl(summary_entries.LINE_COL3,0));
6227                         l_TOT_COL2 := nvl(l_TOT_COL2,0) + nvl(summary_entries.LINE_COL2,0);
6228                         l_TOT_COL3 := nvl(l_TOT_COL3,0) + nvl(summary_entries.LINE_COL3,0);
6229                     end if;
6230                    /* end if; */
6231                 end if;
6232                 if l_line_count > g_max_line then
6233                     hr_utility.trace('Line more than max line : ' || l_line_count);
6234                     if l_odd_page then
6235                        hr_utility.trace('Current page is odd page, resetting it to even');
6236                     else
6237                        hr_utility.trace('Current page is even page, resetting it to odd');
6238                     end if;
6239                    l_odd_page := not(l_odd_page);
6240                    l_line_count := l_line_count - g_max_line;
6241                    hr_utility.trace('New line count is : ' || l_line_count);
6242                 end if;
6243             end loop;
6244         end if;
6245        if l_loop_count <> 0
6246         then
6247             -- add last total line
6248             add_total_lines;
6249             l_line_count := l_line_count + 1;
6250             hr_utility.trace('Writing total line : ' || l_line_count);
6251             hr_utility.trace('Final Line : ' || l_line_count);
6252             if l_odd_page then
6253                hr_utility.trace('Current page is Odd page');
6254             else
6255                hr_utility.trace('Current page is Even page');
6256             end if;
6257 
6258             if l_line_count > g_max_line then
6259                hr_utility.trace('Line more than max line : ' || l_line_count);
6260                if l_odd_page then
6261                   hr_utility.trace('Current page is odd page, resetting it to even');
6262                else
6263                   hr_utility.trace('Current page is even page, resetting it to odd');
6264                end if;
6265                l_odd_page := not(l_odd_page);
6266                l_line_count := l_line_count - g_max_line;
6267                hr_utility.trace('New line count is : ' || l_line_count);
6268             end if;
6269 
6270             if l_odd_page and p_print_Style = 'Double Sided Printing' then --bug 8241399
6271                -- If print option is two sided printing we are appending the dummy XML data
6272                hr_utility.trace('Current page is Odd page');
6273                l_temp := ceil((g_max_line - l_line_count)/4) + 1;
6274                hr_utility.trace('Current line is : ' || l_line_count);
6275                hr_utility.trace('Writing more record (4 line / record) : ' || l_temp);
6276                for l_line_count in 0..l_temp loop
6277                   l_xfdf_intermetiate_var  :=
6278                       '<field name="'||'SECTION_TITLE'  ||'"><value>' ||null|| '</value></field>  ' ||
6279                       '<field name="'||'SECTION_HEADING'||'"><value>' ||null|| '</value></field>  ' ||
6280                       '<field name="'||'COL1_SDATE'     ||'"><value>' ||null|| '</value></field>  ' ||
6281                       '<field name="'||'COL1_EDATE'     ||'"><value>' ||null|| '</value></field>  ' ||
6282                       '<field name="'||'LINE_DESC'      ||'"><value>' ||null|| '</value></field>  ' ||
6283                       '<field name="'||'COL1_HEADING'   ||'"><value>' ||null|| '</value></field>  ' ||
6284                       '<field name="'||'COL2_HEADING'   ||'"><value>' ||null|| '</value></field>  ' ||
6285                       '<field name="'||'COL3_HEADING'   ||'"><value>' ||null|| '</value></field>  ' ||
6286                       '<field name="'||'LINE_START_D'   ||'"><value>' ||null|| '</value></field>  ' ||
6287                       '<field name="'||'LINE_END_D'     ||'"><value>' ||null|| '</value></field>  ' ||
6288                       '<field name="'||'LINE_DETAIL'    ||'"><value>' ||null|| '</value></field>  ' ||
6289                       '<field name="'||'LINE_COL1'      ||'"><value>' ||null|| '</value></field>  ' ||
6290                       '<field name="'||'LINE_COL2'      ||'"><value>' ||null|| '</value></field>  ' ||
6291                       '<field name="'||'LINE_COL3'      ||'"><value>' ||null|| '</value></field>  ' ||
6292                       '<field name="'||'LINE_TOTAL'     ||'"><value>' ||null|| '</value></field>  ' ||
6293                       '<field name="'||'TOT_COL1'       ||'"><value>' ||null|| '</value></field>  ' ||
6294                       '<field name="'||'TOT_COL2'       ||'"><value>' ||null|| '</value></field>  ' ||
6295                       '<field name="'||'TOT_COL3'       ||'"><value>' ||null|| '</value></field>  ' ;
6296                      dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6297                end loop;
6298             end if;
6299             -- this will ensure the xfdf string ends with the correct fields
6300             l_xfdf_intermetiate_var  :=  '</fields>  </xfdf>';
6301             dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6302         end if;
6303         DBMS_LOB.CREATETEMPORARY(l_xfdf_blob_str,true);
6304         clob_to_blob(l_xfdf_str,l_xfdf_blob_str);
6305         dbms_lob.close(l_xfdf_str);
6306         dbms_lob.freetemporary(l_xfdf_str);
6307         hr_utility.trace('blob length '|| dbms_lob.getlength(l_xfdf_blob_str));
6308         --   Adding this causes error in xdo so commented this
6309         --   DBMS_LOB.FREETEMPORARY(l_xfdf_blob_str);
6310         --hr_utility.trace_off;
6311         return l_xfdf_blob_str;
6312    end ;
6313   /*Added this function to generate pdf report for Address*/
6314   /*Bug No. 3201848*/
6315  /* Bug 8571876 - Created function to replace special characters with XML symbols */
6316  FUNCTION replace_xml_symbols(p_string IN VARCHAR2) RETURN VARCHAR2 AS
6317 
6318  l_string   VARCHAR2(3200);
6319 
6320  BEGIN
6321 	l_string :=  p_string;
6322 
6323 	l_string := replace(l_string, '&', '&');
6324 	l_string := replace(l_string, '<', '<');
6325 	l_string := replace(l_string, '>', '>');
6326       l_string := replace(l_string, '''',''');
6327 	l_string := replace(l_string, '"', '"');
6328 
6329    RETURN l_string;
6330    EXCEPTION when no_data_found then
6331      null;
6332    END replace_xml_symbols;
6333 
6334   function fetch_address_xfdf_blob (p_assignment_action_id Number,
6335                                     p_print_Style varchar2, --bug 8241399
6336                          -- p_print style parameter added to suppress additional blank page
6337 				    p_priv_mark varchar2) return blob --bug 8942337
6338                          --p_priv_mark parameter added to print user defined Data
6339 			 --Privacy Marking on the address page
6340   is
6341 
6342 
6343         cursor csr_address_entries
6344         is
6345         select address_line1,
6346                address_line2,
6347                address_line3,
6348                town_or_city,
6349                postal_code,
6350                region_1
6351        from   per_addresses
6352        where  person_id = (
6353        select distinct person_id
6354        from   pay_assignment_actions paa,
6355               per_all_assignments_f  paf
6356        where  paa.assignment_action_id = p_assignment_action_id
6357        and    paf.assignment_id = paa.assignment_id)
6358        and    primary_flag = 'Y'
6359        and    sysdate between date_from and nvl(date_to, hr_general.end_of_time);
6360 
6361        cursor csr_person_name
6362        is
6363        select emp.action_information1             emp_name
6364        from   pay_action_information  emp
6365        where  emp.action_information_category   = 'EMPLOYEE DETAILS'
6366        and    emp.action_context_id             = p_assignment_action_id
6367        and    emp.action_context_type           = 'AAP';
6368 
6369         /*
6370         select emp.action_information1             emp_name
6371               ,adr.action_information5             adr_adress1
6372               ,adr.action_information6             adr_adress2
6373               ,adr.action_information7             adr_adress3
6374               ,adr.action_information8             adr_town
6375               ,adr.action_information12           adr_county
6376               ,adr.action_information9            adr_code
6377         from   pay_assignment_actions  paa
6378               ,pay_action_information  emp    -- Employee Details
6379               ,pay_action_information  adr    -- Address Details
6380               ,pay_action_information  prl    -- EMEA Payroll Info
6381         where  paa.assignment_action_id   = p_assignment_action_id
6382         and    emp.action_information_category   = 'EMPLOYEE DETAILS'
6383         and    emp.action_context_id             = paa.assignment_action_id
6384         and    emp.action_context_type           = 'AAP'
6385 --
6386         and    prl.action_information_category   = 'EMEA PAYROLL INFO'
6387         and    prl.action_context_id             = paa.assignment_action_id
6388         and    prl.action_context_type           = 'AAP'
6389 --
6390         and    adr.action_information_category   = 'ADDRESS DETAILS'
6391         and    adr.action_context_id             = paa.assignment_action_id
6392         and    adr.action_context_type           = 'AAP'
6393         and    adr.action_information14          = 'Employee Address';
6394         */
6395 --
6396          cursor csr_lookup(p_code Varchar2)
6397          is
6398          select hlu.meaning hlu_meaning
6399          from   hr_lookups hlu
6400          where  hlu.lookup_type='GB_COUNTY'
6401          and    hlu.lookup_code=p_code
6402          and    hlu.enabled_flag='Y';
6403 
6404         --Cursor Added for bug 8942337
6405          cursor csr_get_priv_mark
6406          is
6407          select meaning
6408          from   hr_lookups
6409          where  lookup_type='GB_P11D_PRI_MARKINGS'
6410          and    lookup_code=p_priv_mark
6411          and    enabled_flag='Y';
6412 
6413          l_xfdf_str clob;
6414          l_xfdf_blob_str blob;
6415          l_xfdf_intermetiate_var varchar2(20000);
6416          l_adress1 pay_action_information.action_information5%type;
6417          l_adress2 pay_action_information.action_information6%type;
6418          l_adress3 pay_action_information.action_information7%type;
6419          l_town    pay_action_information.action_information8%type;
6420          l_county  pay_action_information.action_information12%type;
6421          l_employee_name pay_action_information.action_information1%type;
6422          l_sur_name varchar2(150); --  P11D 08/09
6423          l_fore_name varchar2(150); -- P11D 08/09
6424          l_code pay_action_information.action_information9%type;
6425          l_meaning hr_lookups.meaning%type;
6426        --Parameter Added for bug 8942337
6427          l_priv_mark_meaning     hr_lookups.meaning%type;
6428 
6429    begin
6430 
6431         dbms_lob.createtemporary(l_xfdf_str,false,DBMS_LOB.CALL);
6432         dbms_lob.open(l_xfdf_str,dbms_lob.lob_readwrite);
6433 
6434         open csr_address_entries;
6435         fetch csr_address_entries into l_adress1,l_adress2,l_adress3,l_town,l_county,l_code;
6436         close csr_address_entries;
6437 
6438         open csr_person_name;
6439         fetch csr_person_name into l_employee_name;
6440         close csr_person_name;
6441 
6442          -- P11D 08/09
6443          -- Fetch sur and fore names
6444          get_sur_fore_names(p_assignment_action_id,
6445                             l_sur_name,
6446                             l_fore_name);
6447          -- P11D 08/09
6448 
6449         if l_code is not null
6450         then
6451            open csr_lookup(l_code);
6452            fetch csr_lookup into l_meaning;
6453            close csr_lookup;
6454         end if;
6455 
6456       --For bug 8942337
6457         open csr_get_priv_mark;
6458         fetch csr_get_priv_mark into l_priv_mark_meaning;
6459         close csr_get_priv_mark;
6460 
6461  -- <field name="F_Value"><value>Private and Confidential</value></field>' || -- Added for bug 8723038
6462 
6463 	l_xfdf_intermetiate_var  := '<?xml version = "1.0" encoding = "UTF-8"?>
6464 	           <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><fields> '||
6465 		   ' <field name="'||'F_Value'||'"><value>' || l_priv_mark_meaning || '</value></field> ' || --Bug 8942337
6466                    ' <field name="'||'F_EMP_NAME'||'"><value>' || l_employee_name || '</value></field> ' ||
6467                    -- P11D 08/09
6468                    ' <field name="'|| 'SUR_NAME' ||'"><value>' ||l_sur_name  || '</value></field> ' ||
6469                    ' <field name="'|| 'FORE_NAME'||'"><value>' ||l_fore_name || '</value></field> ' ||
6470                    -- P11D 08/09
6471 			 --/* Bug 8571876 - Added replace_xml_symbols function in address columns */
6472                    ' <field name="'||'F_ADDR1'||'"><value>'  || replace_xml_symbols(l_adress1) || '</value></field>  ' ||
6473                    ' <field name="'||'F_ADDR2'||'"><value>'  || replace_xml_symbols(l_adress2) || '</value></field>  ' ||
6474                    ' <field name="'||'F_ADDR3'||'"><value>'  || replace_xml_symbols(l_adress3) || '</value></field>  ' ||
6475                    ' <field name="'||'F_TOWN'||'"><value>'   || replace_xml_symbols(l_town) || '</value></field>  ' ||
6476                    ' <field name="'||'F_COUNTY'||'"><value>' || replace_xml_symbols(ltrim(l_meaning || ' '|| l_county)) || '</value></field>  ' ||
6477                    '</fields>';
6478         If p_print_Style = 'Double Sided Printing' then --bug 8241399
6479 	-- If print option is two sided printing we are appending the dummy XML data
6480 		 l_xfdf_intermetiate_var  := l_xfdf_intermetiate_var || '<fields> ' ||
6481                    ' <field name="'||'F_EMP_NAME'||'"><value>' || null || '</value></field> ' ||
6482                    -- P11D 08/09
6483                    ' <field name="'|| 'SUR_NAME' ||'"><value>' ||null  || '</value></field> ' ||
6484                    ' <field name="'|| 'FORE_NAME'||'"><value>' ||null || '</value></field> ' ||
6485                    -- P11D 08/09
6486                    ' <field name="'||'F_ADDR1'||'"><value>'  || null || '</value></field>  ' ||
6487                    ' <field name="'||'F_ADDR2'||'"><value>'  || null || '</value></field>  ' ||
6488                    ' <field name="'||'F_ADDR3'||'"><value>'  || null || '</value></field>  ' ||
6489                    ' <field name="'||'F_TOWN'||'"><value>'   || null || '</value></field>  ' ||
6490                    ' <field name="'||'F_COUNTY'||'"><value>' || null || '</value></field>  ' ||
6491                    '</fields>';
6492         end if;
6493 	l_xfdf_intermetiate_var := l_xfdf_intermetiate_var ||'</xfdf>';
6494 
6495 --Start of the fix for the EAP bug 9383416
6496 if (validate_display_output(p_assignment_action_id) = 1) then
6497         dbms_lob.writeAppend( l_xfdf_str, length(l_xfdf_intermetiate_var) ,l_xfdf_intermetiate_var );
6498 end if;
6499 --End of the fix for the EAP bug 9383416
6500 
6501         DBMS_LOB.CREATETEMPORARY(l_xfdf_blob_str,true);
6502         clob_to_blob(l_xfdf_str,l_xfdf_blob_str);
6503         dbms_lob.close(l_xfdf_str);
6504         dbms_lob.freetemporary(l_xfdf_str);
6505         --   Adding this causes error in xdo so commented this
6506         --   DBMS_LOB.FREETEMPORARY(l_xfdf_blob_str);
6507         return l_xfdf_blob_str;
6508    end;
6509 
6510    FUNCTION write_magtape_records (p_arch_payroll_action_id   NUMBER,
6511                                    p_emp_ref_no               VARCHAR2,
6512                                    p_person_id                VARCHAR2,
6513                                    p_assignment_number    OUT NOCOPY VARCHAR2,
6514                                    p_INT_MAX_AMT_OUTSTANDING OUT NOCOPY VARCHAR2) RETURN NUMBER
6515    -- return 1 if the record is to be written to magtape file
6516    -- else 0
6517    IS
6518         CURSOR get_assignment_action_id
6519         IS
6520         SELECT /*+ ordered  */
6521                paa.assignment_action_id,
6522                paf.ASSIGNMENT_TYPE
6523                --  Added paf.ASSIGNMENT_TYPE as we need to write primary assign number in
6524                --  magtape if the primary and sec both have the p11d benefits
6525                -- In case if only secondary have benefits then we may write
6526                -- any  secondary assignment number in the magtape
6527         FROM   per_assignments_f paf,
6528                pay_assignment_actions paa,
6529                pay_action_information pai_comp
6530         WHERE  paf.person_id = p_person_id
6531         AND    paa.assignment_id = paf.assignment_id
6532         AND    paa.payroll_action_id = p_arch_payroll_action_id
6533         AND    pai_comp.action_context_id = paa.assignment_action_id
6534         AND    pai_comp.action_information_category = 'EMEA PAYROLL INFO'
6535         AND    pai_comp.action_information6 = p_emp_ref_no
6536         order by paf.ASSIGNMENT_TYPE, paa.assignment_action_id;
6537 
6538 
6539         CURSOR sum_marors_values
6540         IS
6541         SELECT /*+ ordered  */
6542               SUM (TO_NUMBER (NVL (pai.action_information7, '0')))
6543         FROM  per_assignments_f paf,
6544               pay_assignment_actions paa,
6545               pay_action_information pai_comp,
6546               pay_action_information pai
6547         WHERE paf.person_id = p_person_id
6548         AND   paa.payroll_action_id = p_arch_payroll_action_id
6549         AND   paa.assignment_id = paf.assignment_id
6550         AND   pai_comp.action_context_id = paa.assignment_action_id
6551         AND   pai_comp.action_information_category = 'EMEA PAYROLL INFO'
6552         AND   pai_comp.action_information6 = p_emp_ref_no
6553         AND   pai.action_context_id = paa.assignment_action_id
6554         AND   pai.action_information_category = 'MARORS'
6555         AND   pai.action_context_id = pai_comp.action_context_id;
6556 
6557         CURSOR fetch_ben_values (p_assignment_action_id   NUMBER,
6558                                  p_action_info_catg       VARCHAR2)
6559         IS
6560         SELECT *
6561         FROM   pay_action_information pai
6562         WHERE  pai.action_context_id = p_assignment_action_id
6563         AND    pai.action_information_category = p_action_info_catg;
6564 
6565         l_ce_values_a            fetch_ben_values%ROWTYPE;
6566         l_ce_values_b            fetch_ben_values%ROWTYPE;
6567         l_ce_values_c            fetch_ben_values%ROWTYPE;
6568         l_emp_details            fetch_ben_values%ROWTYPE;
6569         l_assignment_action_id   NUMBER (15);
6570         l_write_to_magtape       NUMBER  := 0;
6571         l_marors_ce              NUMBER;
6572         l_assignment_number      varchar2(30) ;
6573         l_dummy                  varchar2(30);
6574         l_rep_run                varchar2(10);
6575 
6576    BEGIN
6577 
6578         PAY_GB_P11D_ARCHIVE_SS.get_parameters(p_payroll_action_id => p_arch_payroll_action_id,
6579                                               p_token_name        => 'Rep_Run',
6580                                               p_token_value       => l_rep_run);
6581 
6582         OPEN get_assignment_action_id;
6583         FETCH get_assignment_action_id INTO l_assignment_action_id,l_dummy ;
6584         CLOSE get_assignment_action_id;
6585 
6586         OPEN fetch_ben_values (l_assignment_action_id,
6587                                'GB P11D ASSIGNMENT RESULTA');
6588         FETCH fetch_ben_values INTO l_ce_values_a;
6589         CLOSE fetch_ben_values;
6590 
6591         OPEN fetch_ben_values (l_assignment_action_id,
6592                                'GB P11D ASSIGNMENT RESULTB');
6593         FETCH fetch_ben_values INTO l_ce_values_b;
6594         CLOSE fetch_ben_values;
6595 
6596         OPEN fetch_ben_values (l_assignment_action_id,
6597                                'GB P11D ASSIGNMENT RESULTC');
6598         FETCH fetch_ben_values INTO l_ce_values_c;
6599         CLOSE fetch_ben_values;
6600 
6601         OPEN fetch_ben_values (l_assignment_action_id,
6602                                'EMPLOYEE DETAILS');
6603         FETCH fetch_ben_values INTO l_emp_details;
6604         CLOSE fetch_ben_values;
6605 
6606         p_assignment_number :=NVL(UPPER(l_emp_details.action_information14), ' ');
6607 
6608         IF to_number(l_rep_run) < 2005
6609         THEN
6610             IF    TO_NUMBER (l_ce_values_a.action_information4) > 0
6611                OR TO_NUMBER (l_ce_values_a.action_information6) > 0
6612                OR TO_NUMBER (l_ce_values_a.action_information7) > 0
6613                OR TO_NUMBER (l_ce_values_a.action_information10) > 0
6614                OR TO_NUMBER (l_ce_values_a.action_information11) > 0
6615                OR TO_NUMBER (l_ce_values_a.action_information12) > 0
6616                OR TO_NUMBER (l_ce_values_a.action_information13) > 0
6617                OR TO_NUMBER (l_ce_values_a.action_information14) > 0
6618                OR TO_NUMBER (l_ce_values_a.action_information15) > 0
6619                OR TO_NUMBER (l_ce_values_a.action_information18) > 0
6620                OR TO_NUMBER (l_ce_values_a.action_information19) > 0
6621                OR TO_NUMBER (l_ce_values_a.action_information22) > 0
6622                OR TO_NUMBER (l_ce_values_a.action_information26) > 0
6623                OR l_ce_values_a.action_information27 = 'Y'
6624                -- moving the check for free loans below as it is base don combinaiton
6625                -- of values
6626                /* OR TO_NUMBER (l_ce_values_a.action_information28) >= 0 -- this is int
6627                    -- free loan awaiting direction from IR */
6628                OR TO_NUMBER (l_ce_values_b.action_information4) > 0
6629                OR TO_NUMBER (l_ce_values_b.action_information8) > 0
6630                OR TO_NUMBER (l_ce_values_b.action_information9) > 0
6631                OR TO_NUMBER (l_ce_values_b.action_information12) > 0
6632                OR TO_NUMBER (l_ce_values_b.action_information15) > 0
6633                OR TO_NUMBER (l_ce_values_b.action_information19) > 0
6634                OR TO_NUMBER (l_ce_values_b.action_information22) > 0
6635                OR TO_NUMBER (l_ce_values_b.action_information25) > 0
6636                OR TO_NUMBER (l_ce_values_b.action_information29) > 0
6637             THEN
6638                 l_write_to_magtape := 1;
6639             END IF;
6640             -- chk for marors
6641             IF TO_NUMBER (nvl(l_ce_values_c.action_information15,'0')) >= 1 and
6642                TO_NUMBER (nvl(l_ce_values_c.action_information22,'0')) <> 0
6643             THEN
6644                 l_write_to_magtape := 1;
6645             END IF;
6646 
6647             if TO_NUMBER (l_ce_values_a.action_information28) > 0 and
6648                TO_NUMBER (l_ce_values_c.action_information23) > 5000
6649             then
6650                 l_write_to_magtape := 1;
6651             end if;
6652             p_INT_MAX_AMT_OUTSTANDING := nvl(l_ce_values_c.action_information23,'0');
6653         ELSE
6654             IF    TO_NUMBER (l_ce_values_a.action_information4) > 0
6655                OR TO_NUMBER (l_ce_values_a.action_information6) > 0
6656                OR TO_NUMBER (l_ce_values_a.action_information7) > 0
6657                OR TO_NUMBER (l_ce_values_a.action_information10) > 0
6658                OR TO_NUMBER (l_ce_values_a.action_information11) > 0
6659                OR TO_NUMBER (l_ce_values_a.action_information12) > 0
6660                OR TO_NUMBER (l_ce_values_a.action_information13) > 0
6661                OR TO_NUMBER (l_ce_values_a.action_information14) > 0
6662                OR TO_NUMBER (l_ce_values_a.action_information15) > 0
6663                OR TO_NUMBER (l_ce_values_a.action_information18) > 0
6664                OR TO_NUMBER (l_ce_values_a.action_information19) > 0
6665                OR TO_NUMBER (l_ce_values_a.action_information22) > 0
6666                OR TO_NUMBER (l_ce_values_a.action_information26) > 0
6667                -- moving the check for free loans below as it is base don combinaiton
6668                -- of values
6669                /* OR TO_NUMBER (l_ce_values_a.action_information28) >= 0 -- this is int
6670                -- free loan awaiting direction from IR */
6671                OR TO_NUMBER (l_ce_values_b.action_information4) > 0
6672                OR TO_NUMBER (l_ce_values_b.action_information8) > 0
6673                OR TO_NUMBER (l_ce_values_b.action_information9) > 0
6674                OR TO_NUMBER (l_ce_values_b.action_information12) > 0
6675                OR TO_NUMBER (l_ce_values_b.action_information15) > 0
6676                OR TO_NUMBER (l_ce_values_b.action_information19) > 0
6677                OR TO_NUMBER (l_ce_values_b.action_information22) > 0
6678                OR TO_NUMBER (l_ce_values_b.action_information25) > 0
6679                OR TO_NUMBER (l_ce_values_b.action_information29) > 0
6680             THEN
6681                 l_write_to_magtape := 1;
6682             END IF;
6683             -- chk for marors
6684             IF TO_NUMBER (nvl(l_ce_values_c.action_information13,'0')) >= 1 and
6685                TO_NUMBER (nvl(l_ce_values_c.action_information20,'0')) <> 0
6686             THEN
6687                 l_write_to_magtape := 1;
6688             END IF;
6689 
6690             if TO_NUMBER (l_ce_values_a.action_information27) > 0 and
6691                TO_NUMBER (l_ce_values_c.action_information21) > 5000
6692             then
6693                 l_write_to_magtape := 1;
6694             end if;
6695             p_INT_MAX_AMT_OUTSTANDING := nvl(l_ce_values_c.action_information21,'0');
6696         END IF;
6697         RETURN l_write_to_magtape;
6698    END;
6699 end hr_gb_process_p11d_entries_pkg;