DBA Data[Home] [Help]

PACKAGE BODY: APPS.PQP_GB_T1_PENSION_EXTRACTS

Source


1 PACKAGE BODY pqp_gb_t1_pension_extracts AS
2 --  /* $Header: pqpgbtp1.pkb 120.46.12020000.4 2012/07/05 12:15:15 amnaraya ship $ */
3 --
4 -- Exceptions
5 hr_application_error exception;
6 pragma exception_init (hr_application_error, -20001);
7 
8 
9 --
10 -- Overloaded Debug procedures
11 --
12 PROCEDURE debug
13   (p_trace_message  IN     VARCHAR2
14   ,p_trace_location IN     NUMBER
15   )
16 IS
17    l_padding VARCHAR2(12);
18    l_MAX_MESSAGE_LENGTH NUMBER:= 72;
19 BEGIN
20 
21     IF p_trace_location IS NOT NULL THEN
22 
23       l_padding := SUBSTR
24                     (RPAD(' ',LEAST(g_nested_level,5)*2,' ')
25                     ,1,l_MAX_MESSAGE_LENGTH
26                        - LEAST(LENGTH(p_trace_message)
27                               ,l_MAX_MESSAGE_LENGTH)
28                     );
29 
30      hr_utility.set_location
31       (l_padding||
32        SUBSTR(p_trace_message
33              ,GREATEST(-LENGTH(p_trace_message),-l_MAX_MESSAGE_LENGTH))
34       ,p_trace_location);
35 
36     ELSE
37 
38      hr_utility.trace(SUBSTR(p_trace_message,1,250));
39 
40     END IF;
41 
42 END debug;
43 --
44 --
45 --
46 PROCEDURE debug
47   (p_trace_number   IN     NUMBER )
48 IS
49 BEGIN
50     debug(fnd_number.number_to_canonical(p_trace_number));
51 END debug;
52 --
53 --
54 --
55 PROCEDURE debug
56   (p_trace_date     IN     DATE )
57 IS
58 BEGIN
59     debug(fnd_date.date_to_canonical(p_trace_date));
60 END debug;
61 --
62 --
63 --
64 PROCEDURE debug_enter
65   (p_proc_name IN VARCHAR2
66   ,p_trace_on  IN VARCHAR2
67   )
68 IS
69 
70   l_extract_attributes    pqp_gb_tp_pension_extracts.csr_pqp_extract_attributes%ROWTYPE;
71   l_business_group_id     per_all_assignments_f.business_group_id%TYPE;
72 
73 BEGIN
74 
75   IF  g_nested_level = 0 THEN -- swtich tracing on/off at the top level only
76 
77     -- Set the trace flag, but only the first time around
78     IF g_trace IS NULL THEN
79 
80       OPEN pqp_gb_tp_pension_extracts.csr_pqp_extract_attributes;
81       FETCH pqp_gb_tp_pension_extracts.csr_pqp_extract_attributes INTO l_extract_attributes;
82       CLOSE pqp_gb_tp_pension_extracts.csr_pqp_extract_attributes;
83 
84       l_business_group_id := fnd_global.per_business_group_id;
85 
86       BEGIN
87         g_trace := hruserdt.get_table_value
88                   (p_bus_group_id   => l_business_group_id
89                   ,p_table_name     => l_extract_attributes.user_table_name
90                   ,p_col_name       => 'Attribute Location Qualifier 1'
91                   ,p_row_value      => 'Debug'
92                   ,p_effective_date => NULL -- don't hv the date
93                   );
94       EXCEPTION
95         WHEN OTHERS THEN
96           g_trace := 'N';
97       END;
98 
99       g_trace := nvl(g_trace,'N');
100 
101       debug('UDT Trace Flag : '||g_trace);
102 
103     END IF; -- g_trace IS NULL THEN
104 
105     IF NVL(p_trace_on,'N') = 'Y'
106        OR
107        g_trace = 'Y' THEN
108 
109       hr_utility.trace_on(NULL,'REQID'); -- Pipe name REQIDnnnnnn
110 
111     END IF; -- NVL(p_trace_on,'N') = 'Y'
112     --
113   END IF; -- if nested level = 0
114 
115   g_nested_level :=  g_nested_level + 1;
116   debug('Entered: '||NVL(p_proc_name,g_proc_name),g_nested_level*100);
117 
118 END debug_enter;
119 --
120 -- debug_exit
121 --   The exception handler of top level functions must call debug_ext
122 --   with p_trace_off = 'Y'
123 --
124 PROCEDURE debug_exit
125   (p_proc_name IN VARCHAR2
126   ,p_trace_off IN VARCHAR2
127   )
128 IS
129 BEGIN
130 
131   debug('Leaving: '||NVL(p_proc_name,g_proc_name),-g_nested_level*100);
132   g_nested_level := g_nested_level - 1;
133 
134   -- debug enter sets trace ON when g_trace = 'Y' and nested level = 0
135   -- so we must turn it off for the same condition
136   -- Also turn off tracing when the override flag of p_trace_off has been passed as Y
137   IF (g_nested_level = 0
138       AND
139       g_trace = 'Y'
140      )
141      OR
142      NVL(p_trace_off,'N') = 'Y' THEN
143 
144     hr_utility.trace_off;
145 
146   END IF; -- (g_nested_level = 0
147 
148 END debug_exit;
149 
150 
151 -- 8iComp Changes: IMORTANT NOTE
152 
153 -- Removing the definition for Table Of Table datastructure
154 -- as Oracle 8i does not support this.
155 -- Later as Oracle 9i becomes the minimum pre requisite for Apps
156 -- we can move back to this logic
157 -- till then we will use a common table for keeping Leaver-restarter dates
158 -- for all the assignmets together.
159 -- The new solution is not as performant as the older one.
160 
161 -- The Following function sets the leaver-restarter rows for as assignment
162 -- in the global collection g_per_asg_leaver_dates
163 FUNCTION set_g_per_asg_leaver_dates
164             ( p_leaver_dates_type IN t_leaver_dates_type
165             ) RETURN NUMBER
166 IS
167 
168   l_return     NUMBER;
169   l_nxt_count  NUMBER;
170   l_itr        NUMBER;
171 --
172   l_proc_name           VARCHAR2(61):=
173      g_proc_name||'set_g_per_asg_leaver_dates';
174 
175 BEGIN
176   debug_enter(l_proc_name) ;
177 
178   l_nxt_count := g_per_asg_leaver_dates.COUNT;
179 
180   debug('g_per_asg_leaver_dates.COUNT: '||g_per_asg_leaver_dates.COUNT, 10);
181   debug('p_leaver_dates_type.COUNT: '||p_leaver_dates_type.COUNT);
182 
183   IF p_leaver_dates_type.COUNT  > 0 THEN
184 
185     debug(l_proc_name, 20);
186     FOR l_itr IN p_leaver_dates_type.FIRST..p_leaver_dates_type.LAST
187     LOOP
188       debug('l_itr: '|| l_itr, 30) ;
189       debug('p_leaver_dates_type(l_itr).start_date: '|| p_leaver_dates_type(l_itr).start_date) ;
190       debug('p_leaver_dates_type(l_itr).leaver_date: '|| p_leaver_dates_type(l_itr).leaver_date) ;
191       debug('p_leaver_dates_type(l_itr).restarter_date: '|| p_leaver_dates_type(l_itr).restarter_date) ;
192       debug('p_leaver_dates_type(l_itr).assignment_id: '|| p_leaver_dates_type(l_itr).assignment_id) ;
193 
194       l_nxt_count := l_nxt_count + 1 ;
195       g_per_asg_leaver_dates(l_nxt_count).start_date    :=  p_leaver_dates_type(l_itr).start_date ;
196       g_per_asg_leaver_dates(l_nxt_count).leaver_date   :=  p_leaver_dates_type(l_itr).leaver_date ;
197       g_per_asg_leaver_dates(l_nxt_count).restarter_date:=  p_leaver_dates_type(l_itr).restarter_date ;
198       g_per_asg_leaver_dates(l_nxt_count).assignment_id :=  p_leaver_dates_type(l_itr).assignment_id ;
199       debug('l_nxt_count: '|| l_nxt_count, 40) ;
200 
201     END LOOP ;
202     debug(l_proc_name, 50);
203 
204   END IF ;
205 
206   debug_exit(l_proc_name);
207 
208   return   l_itr ;
209 
210 EXCEPTION
211   WHEN OTHERS THEN
212     debug_exit(' Others in '||l_proc_name);
213     RAISE;
214 END set_g_per_asg_leaver_dates;
215 
216 -- 8iComp Changes: IMORTANT NOTE
217 
218 -- Removing the definition for Table Of Table datastructure
219 -- as Oracle 8i does not support this.
220 -- Later as Oracle 9i becomes the minimum pre requisite for Apps
221 -- we can move back to this logic
222 -- till then we will use a common table for keeping Leaver-restarter dates
223 -- for all the assignmets together.
224 -- The new solution is not as performant as the older one.
225 
226 -- The Following function get the leaver-restarter rows for an assignment
227 -- in the global collection g_per_asg_leaver_dates
228 
229 FUNCTION get_g_per_asg_leaver_dates
230             ( p_assignment_id IN NUMBER
231              ,p_leaver_dates_type OUT NOCOPY t_leaver_dates_type
232             ) RETURN NUMBER
233 IS
234   l_leaver_dates_type t_leaver_dates_type;
235   l_counter NUMBER := 0;
236   l_found   VARCHAR2(1) := 'N' ;
237 
238   l_itr        NUMBER;
239 --
240   l_proc_name           VARCHAR2(61):=
241      g_proc_name||'get_g_per_asg_leaver_dates';
242 
243 BEGIN
244 
245   debug_enter(l_proc_name);
246   debug ('p_assignment_idT: '||p_assignment_id,10);
247   debug ('g_per_asg_leaver_dates.COUNT: '|| g_per_asg_leaver_dates.COUNT);
248 
249   IF g_per_asg_leaver_dates.COUNT  > 0 THEN
250     debug(l_proc_name,20);
251 
252     FOR l_itr IN g_per_asg_leaver_dates.FIRST..g_per_asg_leaver_dates.LAST
253     LOOP
254     EXIT WHEN ( l_found = 'Y'
255                 AND g_per_asg_leaver_dates(l_itr).assignment_id <> p_assignment_id ) ;
256 
257       debug('assignment_id: '||g_per_asg_leaver_dates(l_itr).assignment_id, 30) ;
258 
259       IF (g_per_asg_leaver_dates(l_itr).assignment_id = p_assignment_id) THEN
260 
261         debug(l_proc_name, 40) ;
262         l_counter := l_counter + 1 ;
263         l_found := 'Y';
264         l_leaver_dates_type(l_counter).start_date    :=  g_per_asg_leaver_dates(l_itr).start_date ;
265         l_leaver_dates_type(l_counter).leaver_date   :=  g_per_asg_leaver_dates(l_itr).leaver_date ;
266         l_leaver_dates_type(l_counter).restarter_date:=  g_per_asg_leaver_dates(l_itr).restarter_date ;
267         l_leaver_dates_type(l_counter).assignment_id :=  g_per_asg_leaver_dates(l_itr).assignment_id ;
268         debug('l_counter: '||l_counter, 50 );
269         debug('l_found: '||l_found );
270 
271       END IF ;
272       debug(l_proc_name, 60);
273 
274     END LOOP ;
275     debug(l_proc_name, 70);
276 
277   END IF ;
278     debug(l_proc_name, 80) ;
279     p_leaver_dates_type := l_leaver_dates_type;
280     debug_exit(l_proc_name);
281 
282     return l_counter;
283 EXCEPTION
284   WHEN OTHERS THEN
285     debug_exit(' Others in '||l_proc_name);
286     RAISE;
287 END get_g_per_asg_leaver_dates;
288 
289 -- 8iComp Changes: IMORTANT NOTE
290 -- Changing he following function to use the 8iComp code.
291 -- Now it does not reference g_asg_leaver_events_table global
292 -- but references g_per_asg_leaver_dates global
293 -- and calls get_g_per_asg_leaver_dates to get the
294 -- collection for Leaver-Restarter dates for an assignment
295 
296 -- MULT-LR --
297 FUNCTION chk_effective_asg(p_assignment_id IN  NUMBER
298                           ,p_effective_date IN DATE
299                            ) RETURN VARCHAR2 IS
300 
301 l_proc_name      VARCHAR2(60)        := g_proc_name || 'chk_effective_asg';
302 l_effective      VARCHAR2(1)         := NULL ;
303 l_current        NUMBER ;
304 l_start_date     DATE ;
305 l_leaver_date    DATE ;
306 l_restarter_date DATE ;
307 
308 l_returned_count  NUMBER;
309 
310 l_asg_events     t_leaver_dates_type ;
311 
312 BEGIN
313 
314   debug_enter (l_proc_name) ;
315 
316   debug('p_assignment_id         : ' || to_char(p_assignment_id),10) ;
317   debug('g_primary_assignment_id : ' || to_char(g_primary_assignment_id)) ;
318   debug('p_effective_date        : ' || to_char(p_effective_date)) ;
319 
320 
321   -- PERF_ENHANC_3A : Performance Enhancements
322   -- check if record corresponding to p_assignment_id is present in the
323   -- collection g_asg_recalc_details.
324   -- If yes, check for matching start_date of the record and the LOS
325   -- if they match, return the value from the record, else compute the
326   -- effectiveness of the assignment
327   -- This step is to avoid unnecessary checking of assignment status for
328   -- a given LOS
329 
330   IF (g_asg_recalc_details.EXISTS(p_assignment_id)
331       AND
332       g_asg_recalc_details(p_assignment_id).eff_start_date = p_effective_date
333       ) THEN
334 
335     l_effective := g_asg_recalc_details(p_assignment_id).effective_status;
336 
337   ELSE -- check effectiveness of assignment
338 
339 
340     -- check if there is any Full time asg for the period
341     IF g_override_ft_asg_id IS NULL THEN
342       debug(l_proc_name, 12);
343       --debug('g_asg_leaver_events_table.COUNT: '||g_asg_leaver_events_table.COUNT);
344       debug('g_per_asg_leaver_dates.COUNT: '||g_per_asg_leaver_dates.COUNT);
345 
346       -- check if leaver-restarter dates have been captured
347       --IF g_asg_leaver_events_table.COUNT > 0 THEN
348 
349       -- 8iComp
350       IF g_per_asg_leaver_dates.COUNT > 0 THEN
351         debug(l_proc_name, 15);
352 
353         -- Check if the assignment is present in global collection
354         -- Primary may be available even if not to be reported.
355         -- So check the report flag for primary assignment
356         IF ( g_ext_asg_details.EXISTS(p_assignment_id)
357            AND
358            (   -- Primary Assignment
359             ( p_assignment_id = g_primary_assignment_id
360               AND
361               g_ext_asg_details(p_assignment_id).report_asg = 'Y'
362              )
363              -- Secondary Assignment
364             OR p_assignment_id <> g_primary_assignment_id
365            )
366          ) THEN
367           debug(l_proc_name, 20) ;
368 
369           -- 8iComp
370           --l_asg_events   := g_asg_leaver_events_table(p_assignment_id);
371           l_returned_count :=  get_g_per_asg_leaver_dates
372                                   ( p_assignment_id     => p_assignment_id
373                                    ,p_leaver_dates_type => l_asg_events
374                                    ) ;
375           debug('l_returned_count : '|| l_returned_count) ;
376           debug(l_proc_name, 25) ;
377 
378           debug('l_asg_events.count: '||to_char(l_asg_events.count), 30) ;
379           IF l_asg_events.count = 0 THEN
380             debug(l_proc_name, 40) ;
381 
382             -- check the effective assignment
383             -- There is no leaver event for the assignment
384             -- else it would have had an entry in the table.
385             IF ( p_effective_date
386                  BETWEEN g_ext_asg_details(p_assignment_id).start_date
387                      AND g_effective_run_date
388                 ) THEN
389               debug(l_proc_name, 50);
390               l_effective := 'Y';
391             ELSE
392               l_effective := 'N' ;
393             END IF ;   --p_effective_date BETWEEN start and run_date
394           ELSE
395             l_current := l_asg_events.FIRST ;
396             debug('l_current: '||to_char(l_current), 55) ;
397 
398             -- Iterate through the collection
399             -- and break out of loop, if the condition is satisfied
400 
401             WHILE (l_current <= l_asg_events.LAST
402                   AND l_effective IS NULL )
403             LOOP
404               debug (l_proc_name, 60+l_current/100000) ;
405 
406               l_start_date     := l_asg_events(l_current).start_date ;
407               l_leaver_date    := l_asg_events(l_current).leaver_date ;
408               l_restarter_date := l_asg_events(l_current).restarter_date ;
409 
410               debug ('l_start_date     : '||to_char(l_start_date), 70+l_current/100000) ;
411               debug ('l_leaver_date    : '||to_char(l_leaver_date) ) ;
412               debug ('l_restarter_date : '||to_char(l_restarter_date)) ;
413 
414               -- Date is before the Assignment start date
415               IF p_effective_date < l_start_date THEN
416                 debug (l_proc_name, 80 + l_current/100000) ;
417                 l_effective := 'N' ;
418               ELSIF p_effective_date BETWEEN
419                     l_start_date AND l_leaver_date THEN
420                 -- date is between a pair of start and end date.
421                 -- therefor it is effective.
422                 debug (l_proc_name, 120 + l_current/100000) ;
423                 l_effective := 'Y' ;
424               ELSIF p_effective_date > l_leaver_date
425                     AND l_restarter_date IS NULL THEN
426                 -- Date is greater than leaver date and there is
427                 -- no restarter event thereafter
428                 -- therefor asg is not affective anymore.
429                 -- not a restarter, so not effective on the date.
430                 debug (l_proc_name, 130 + l_current/100000) ;
431                 l_effective := 'N' ;
432               ELSIF l_restarter_date IS NOT NULL
433                     AND p_effective_date BETWEEN
434                         (l_leaver_date + 1)  AND (l_restarter_date - 1) THEN
435                 --date is between leaver and restarter dates.
436                 debug (l_proc_name, 165 + l_current/100000) ;
437                 l_effective := 'N' ;
438               ELSIF p_effective_date >= l_restarter_date
439                     AND l_asg_events.NEXT(l_current) IS NOT NULL THEN
440                 -- Date is greater than restarter date and there is
441                 -- another set of start-leaver date therefor
442                 -- can not decide for the effectiveness at this point.
443                 -- Loop thru for the next set of dates.
444                 debug (l_proc_name, 170 + l_current/100000) ;
445                 l_current := l_asg_events.NEXT(l_current) ;
446                 debug('l_current: '||to_char(l_current), 180 + l_current/100000 ) ;
447               ELSE   -- p_effective_date > l_restarter_date
448                    -- AND l_asg_events.NEXT(l_current) IS NULL
449 
450                 -- There are no more leaver events for the current restarter event,
451                 -- and the restarter event exists so the asg is effective through
452                 -- out the year after the restarter date
453                 debug (l_proc_name, 190 + l_current/100000) ;
454                 l_effective := 'Y' ;
455               END IF; --l_restarter_date IS NOT NULL
456 
457               debug (l_proc_name, 220 + l_current/100000) ;
458 
459             END LOOP;
460           END IF; --l_current IS NULL or l_current = ''
461 
462           debug ('l_effective      : ' ||l_effective, 230) ;
463           debug ('l_start_date     : ' ||to_char(l_start_date)) ;
464           debug ('l_leaver_date    : '||to_char(l_leaver_date) ) ;
465           debug ('l_restarter_date : '||to_char(l_restarter_date)) ;
466 
467         ELSE -- g_ext_asg_details.EXISTS(p_assignment_id)
468           debug (l_proc_name,240) ;
469           l_effective := 'N' ;
470         END IF ;
471       ELSE --  g_per_asg_leaver_dates.COUNT > 0 THEN
472         --debug ('g_asg_leaver_events_table.COUNT is ZERO', 250) ;
473 
474         IF (g_ext_asg_details.EXISTS(p_assignment_id)
475            AND
476            (   -- Primary Assignment
477             ( g_primary_assignment_id IS NOT NULL
478                AND
479                p_assignment_id = g_primary_assignment_id
480                AND
481                g_ext_asg_details(p_assignment_id).report_asg = 'Y'
482               )
483                -- Secondary Assignment
484              OR
485              (
486               g_primary_assignment_id IS NOT NULL
487               AND
488               p_assignment_id <> g_primary_assignment_id
489              )
490             )
491             AND
492             (p_effective_date
493               BETWEEN nvl(g_ext_asg_details(p_assignment_id).start_date, p_effective_date )
494                       AND
495                       nvl(g_ext_asg_details(p_assignment_id).leaver_date, g_effective_run_date)
496            OR
497            (g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL
498             AND
499             p_effective_date >= g_ext_asg_details(p_assignment_id).restarter_date
500            )
501           )
502          ) THEN
503 
504           l_effective := 'Y';
505           debug(l_proc_name, 260);
506 
507         ELSE  --g_ext_asg_details.EXISTS(p_assignment_id)
508 
509           debug(l_proc_name, 290);
510           l_effective := 'N';
511 
512         END IF;
513       END IF ; --  g_per_asg_leaver_dates.COUNT > 0 THEN
514     ELSIF g_override_ft_asg_id = p_assignment_id THEN
515 
516       l_effective := 'Y';
517       debug(l_proc_name, 310);
518 
519     ELSE  --g_override_ft_asg_id IS NULL
520 
521       debug(l_proc_name, 320);
522       l_effective := 'N';
523 
524     END IF;  --g_override_ft_asg_id IS NULL
525 
526     debug('l_effective: ' || l_effective,330);
527 
528     -- PERF_ENHANC_3A : performance enhancement
529     -- insert a new row in the collection of records for this assignment_id
530     g_asg_recalc_details(p_assignment_id).assignment_id      := p_assignment_id;
531     g_asg_recalc_details(p_assignment_id).eff_start_date     := p_effective_date;
532     g_asg_recalc_details(p_assignment_id).eff_end_date       := NULL;
533     g_asg_recalc_details(p_assignment_id).effective_status   := l_effective;
534     g_asg_recalc_details(p_assignment_id).part_time_sal_paid := NULL;
535     g_asg_recalc_details(p_assignment_id).full_time_sal_rate := NULL;
536 
537   END IF;
538 
539 
540     debug_exit(l_proc_name ) ;
541     RETURN l_effective ;
542 
543 EXCEPTION
544   WHEN OTHERS THEN
545     debug_exit(' Others in '||l_proc_name);
546     RAISE;
547 END chk_effective_asg ;
548 
549 --
550 
551 -- 8iComp Changes: IMORTANT NOTE
552 -- Changing he following function to use the 8iComp code.
553 -- Now it does not reference g_asg_leaver_events_table global
554 -- but references g_per_asg_leaver_dates global
555 -- and calls get_g_per_asg_leaver_dates to get the
556 -- collection for Leaver-Restarter dates for an assignment.
557 
558 PROCEDURE print_events_table IS
559 
560 l_proc_name      VARCHAR2(60)        := g_proc_name || 'print_events_table';
561 l_effective      VARCHAR2(1)         := 'N' ;
562 l_current        NUMBER ;
563 l_start_date     DATE ;
564 l_leaver_date    DATE ;
565 l_restarter_date DATE ;
566 l_asg_events     t_leaver_dates_type ;
567 l_asg_events_current  NUMBER ;
568 
569 BEGIN
570 
571   debug_enter (l_proc_name) ;
572   debug('g_per_asg_leaver_dates.COUNT: '|| g_per_asg_leaver_dates.COUNT,10);
573 
574   IF g_per_asg_leaver_dates.COUNT > 0 THEN
575     debug ('asg_id    start_date   leaver_date    restarter_date  ',20) ;
576 
577     FOR l_itr IN g_per_asg_leaver_dates.FIRST..g_per_asg_leaver_dates.LAST
578     LOOP
579 
580       debug(g_per_asg_leaver_dates(l_itr).assignment_id || '   '||g_per_asg_leaver_dates(l_itr).start_date||'    '||
581       g_per_asg_leaver_dates(l_itr).leaver_date ||'   '||g_per_asg_leaver_dates(l_itr).restarter_date,30 ) ;
582 
583     END LOOP ; -- outer
584 
585     debug ('outside loop ' ,210) ;
586   ELSE
587     debug ('No records to print .....' ) ;
588   END IF ;
589   debug_exit(l_proc_name ) ;
590 
591 EXCEPTION
592   WHEN OTHERS THEN
593     debug_exit(' Others in '||l_proc_name);
594     RAISE;
595 END print_events_table ;
596 
597 -- 8iComp Changes: IMORTANT NOTE
598 -- Changing he following function to use the 8iComp code.
599 -- Now it does not reference g_asg_leaver_events_table global
600 -- but references g_per_asg_leaver_dates global
601 -- and calls get_g_per_asg_leaver_dates to get the
602 -- collection for Leaver-Restarter dates for an assignment
603 
604 -- MULT-LR --
605 FUNCTION get_eff_end_date ( p_assignment_id IN  NUMBER
606                             ,p_effective_start_date IN DATE
607                             ,p_effective_end_date IN DATE
608                             ) RETURN DATE IS
609 
610 l_proc_name      VARCHAR2(60)        := g_proc_name || 'get_eff_end_date';
611 l_effective      VARCHAR2(1)         := 'N' ;
612 l_current        NUMBER ;
613 l_start_date     DATE ;
614 l_leaver_date    DATE ;
615 l_restarter_date DATE ;
616 l_asg_events     t_leaver_dates_type ;
617 l_counter        NUMBER ;
618 
619 l_return_date    DATE := NULL;
620 l_returned_count  NUMBER;
621 BEGIN
622 
623   debug_enter (l_proc_name) ;
624 
625   debug('p_assignment_id         : ' || to_char(p_assignment_id),10) ;
626   debug('g_primary_assignment_id : ' || to_char(g_primary_assignment_id)) ;
627   debug('p_effective_start_date  : ' || to_char(p_effective_start_date)) ;
628   debug('p_effective_end_date    : ' || to_char(p_effective_end_date)) ;
629 
630   --debug('g_asg_leaver_events_table.COUNT : '|| g_asg_leaver_events_table.COUNT) ;
631   debug('g_per_asg_leaver_dates.COUNT : '|| g_per_asg_leaver_dates.COUNT) ;
632 
633   -- 8iComp changes
634   --IF g_asg_leaver_events_table.COUNT > 0 THEN
635   IF g_per_asg_leaver_dates.COUNT > 0 THEN
636 
637          -- Present in global collection ..
638          -- Primary may be available even
639          -- if not to be reported.
640     IF ( g_ext_asg_details.EXISTS(p_assignment_id)
641        AND
642        (  -- Primary Assignment
643         ( p_assignment_id = g_primary_assignment_id
644           AND
645           g_ext_asg_details(p_assignment_id).report_asg = 'Y'
646          )
647          -- Secondary Assignment
648         OR p_assignment_id <> g_primary_assignment_id
649        )
650      ) THEN
651 
652       -- 8iComp
653       -- l_asg_events   := g_asg_leaver_events_table(p_assignment_id);
654       l_returned_count :=  get_g_per_asg_leaver_dates
655                                 ( p_assignment_id     => p_assignment_id
656                                  ,p_leaver_dates_type => l_asg_events
657                                  ) ;
658         debug('l_returned_count : '|| l_returned_count) ;
659       debug('l_asg_events.count: '||to_char(l_asg_events.count), 20) ;
660 
661       IF l_asg_events.count = 0  THEN
662         debug(l_proc_name, 25) ;
663         -- There is no leaver event for the assignment.
664         -- otherwise, there shud be atleast one row in the table.
665         l_return_date := p_effective_end_date ;
666 
667       ELSE  --l_asg_events.count = 0
668         debug(l_proc_name, 30) ;
669         l_current := l_asg_events.FIRST ;
670         debug('l_current: '||to_char(l_current), 40) ;
671 
672         WHILE (l_current <= l_asg_events.LAST
673               AND l_return_date IS NULL )
674         LOOP
675           debug (l_proc_name, 60+l_current/100000) ;
676           l_start_date     := l_asg_events(l_current).start_date ;
677           l_leaver_date    := l_asg_events(l_current).leaver_date ;
678           l_restarter_date := l_asg_events(l_current).restarter_date ;
679 
680           debug ('l_start_date     : '||to_char(l_start_date), 70+l_current/100000) ;
681           debug ('l_leaver_date    : '||to_char(l_leaver_date) ) ;
682           debug ('l_restarter_date : '||to_char(l_restarter_date)) ;
683 
684 
685           IF p_effective_end_date < l_start_date THEN
686             debug (l_proc_name, 80 + l_current/100000) ;
687             debug('THIS CONDITION SHOULD NOT ARISE.......') ;
688             l_return_date  := p_effective_end_date ;
689 
690           ELSIF p_effective_end_date BETWEEN
691                 l_start_date AND l_leaver_date THEN
692             -- Period end date is between a pair of start and end date
693             debug (l_proc_name, 120 + l_current/100000) ;
694             l_return_date  := p_effective_end_date ;
695 
696           ELSIF p_effective_end_date > l_leaver_date
697                 AND l_restarter_date IS NULL THEN
698             -- period end date is after asg leaver date
699             -- and there is no restarter event
700             -- therefor the asg leaver date is the end date.
701             debug (l_proc_name, 140 + l_current/100000) ;
702             l_return_date  := l_leaver_date ;
703 
704           ELSIF l_restarter_date IS NOT NULL
705                 AND p_effective_end_date BETWEEN
706                 (l_leaver_date + 1) AND (l_restarter_date - 1) THEN
707             -- period end date is between leaver and restarter date.
708             debug (l_proc_name, 170 + l_current/100000) ;
709             l_return_date := l_leaver_date ;
710 
711           ELSIF l_restarter_date IS NOT NULL
712                 AND p_effective_end_date >= l_restarter_date
713                 AND l_asg_events.NEXT(l_current) IS NOT NULL THEN
714             debug (l_proc_name, 170 + l_current/100000) ;
715             l_current := l_asg_events.NEXT(l_current) ;
716             debug('l_current: '||to_char(l_current), 180 + l_current/100000 ) ;
717           ELSE  -- l_restarter_date IS NOT NULL
718                 -- AND p_effective_end_date >= l_restarter_date
719                 -- AND l_asg_events.NEXT(l_current) IS NOT NULL THEN
720               debug (l_proc_name, 190 + l_current/100000) ;
721               l_return_date := p_effective_end_date ;
722           END IF ;
723 
724           debug (l_proc_name, 220 + l_current/100000) ;
725 
726         END LOOP;
727       END IF; --l_asg_events.count = 0
728 
729       debug ('l_return_date    : ' ||to_char(l_return_date), 230) ;
730       debug ('l_start_date     : ' ||to_char(l_start_date)) ;
731       debug ('l_leaver_date    : '||to_char(l_leaver_date) ) ;
732       debug ('l_restarter_date : '||to_char(l_restarter_date)) ;
733 
734     ELSE -- g_ext_asg_details.EXISTS(p_assignment_id)
735       debug (l_proc_name,240) ;
736       debug('THIS CONDITION SHOULD NOT ARISE ....') ;
737       l_return_date  := p_effective_end_date ;
738     END IF ;
739   ELSE -- g_per_asg_leaver_dates.COUNT > 0 THEN
740     debug ('g_per_asg_leaver_dates.COUNT is ZERO',250) ;
741 
742     IF ( g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL
743          AND (p_effective_end_date >= g_ext_asg_details(p_assignment_id).restarter_date )
744         ) THEN
745       debug (l_proc_name,260) ;
746       l_return_date := p_effective_end_date ;
747 
748     ELSE
749       debug (l_proc_name,270) ;
750       l_return_date := LEAST(p_effective_end_date
751                              ,nvl(g_ext_asg_details(p_assignment_id).leaver_date
752                                  ,p_effective_end_date
753                                  )
754                              );
755     END IF ;
756   END IF ; -- g_per_asg_leaver_dates.COUNT > 0 THEN
757  debug('l_return_date : ' ||to_char(l_return_date) ) ;
758  debug_exit(l_proc_name ) ;
759  RETURN l_return_date ;
760 
761 EXCEPTION
762   WHEN OTHERS THEN
763     debug_exit(' Others in '||l_proc_name);
764     RAISE;
765 END get_eff_end_date ;
766 
767 
768 -- 8iComp Changes: IMORTANT NOTE
769 -- Changing he following function to use the 8iComp code.
770 -- Now it does not reference g_asg_leaver_events_table global
771 -- but references g_per_asg_leaver_dates global
772 -- and calls get_g_per_asg_leaver_dates to get the
773 -- collection for Leaver-Restarter dates for an assignment
774 --
775 -- PER_LVR : New functionto check for the person level lever events.
776 --
777 FUNCTION chk_person_leaver
778   (p_assignment_id            IN      NUMBER  -- context
779    ,p_person_id               IN      NUMBER
780   ) RETURN VARCHAR2
781 IS
782 
783   -- Variable Declaration
784   l_curr_asg_id         per_all_assignments_f.assignment_id%TYPE;
785   l_prev_asg_id         per_all_assignments_f.assignment_id%TYPE;
786   l_sec_asg_id          per_all_assignments_f.assignment_id%TYPE;
787 
788   l_itr                 NUMBER(3);
789   l_next_itr            NUMBER(3);
790   l_skip_itr            NUMBER(3) := NULL;
791   l_next_of_next_itr    NUMBER(3);
792   l_prev_asg_count      NUMBER(3);
793   l_inclusion_flag      VARCHAR2(1) := 'N';
794   l_reported            VARCHAR2(1) := 'N';
795 
796   l_person_leaver_date  DATE ;
797   l_person_id           NUMBER;
798   -- QAB1: Added to temporarily store g_asg_count Value
799   l_asg_count           NUMBER;
800 
801   -- Rowtype Variable Declaration
802   l_all_sec_asgs        t_sec_asgs_type;
803 
804   -- 8iComp
805   l_insert_rec          NUMBER;
806   l_leaver_assignments  t_leaver_asgs_type;
807   l_leaver_yn           boolean;
808   i                     NUMBER;
809 
810 
811 
812   --l_sec_asg_details   csr_sec_assignments%ROWTYPE := NULL;
813 
814   l_proc_name          VARCHAR2(61):=
815      g_proc_name||'chk_person_leaver';
816 
817 BEGIN -- chk_person_leaver
818 
819   debug_enter(l_proc_name);
820   debug('p_assignment_id: '||to_char(p_assignment_id), 10) ;
821   debug('p_person_id: '||to_char(p_person_id)) ;
822 
823   -- QAB1: Store the value of g_asg_count as
824   -- it may get change during the leaver check
825   -- we need to restore this value
826   -- before returning from the function.
827   l_asg_count := g_asg_count ;
828 
829   -- Step 0) Reset the global variable, it may contain events for the previous person processed
830   --   If events will be stored in this global by the basic criteria, then the following
831   --   line will need to be commented out
832   --   Also, set the global variable for primary assignment id
833 
834   debug('Count in g_asg_events :'||to_char(g_asg_events.COUNT), 20);
835 
836   g_primary_leaver_dates.DELETE;
837   g_primary_assignment_id := p_assignment_id;
838 
839   -- Bugfix 3803760:FTSUPPLY : Resetting override ft asg id
840   --  This is needed coz if for the frist line one of
841   --  the secondary was FT, then chk_report_assignment
842   --  will return N for the primary asg below and
843   --  we will not pick up any new line and leaver restarter
844   --  events for the primary asg
845   g_override_ft_asg_id := NULL;
846   g_tab_sec_asgs.DELETE;
847 
848   IF chk_report_assignment
849        (p_assignment_id            => p_assignment_id
850        ,p_secondary_assignment_id  => l_sec_asg_id
851        ) = 'Y' THEN
852 
853 
854     -- Store all the leaver and restarter dates for the primary asg
855     store_leaver_restarter_dates
856           (p_assignment_id => p_assignment_id
857           );
858 
859     -- MULT-LR changes
860     -- g_asg_leaver_events_table(p_assignment_id) := g_primary_leaver_dates ;
861 
862     --8iComp
863     debug('inserting in new collection...', 22);
864     l_insert_rec := set_g_per_asg_leaver_dates
865                        ( p_leaver_dates_type => g_primary_leaver_dates) ;
866 
867     debug('l_insert_rec: '|| l_insert_rec, 24) ;
868 
869 
870   ELSE
871 
872     -- Bugfix 3880543:REHIRE
873     -- Setting the sec asg id as the primary asg
874     -- We will treat the sec asg as promary so all
875     --  leaver restarter events for this secondary
876     --  will b treated as primary leaver restarter events
877     g_primary_assignment_id := l_sec_asg_id;
878 
879   END IF; -- IF chk_report_assignment
880 
881   -- Step 1b) Get secondary assignments
882   l_all_sec_asgs := get_all_secondary_asgs
883                      (p_primary_assignment_id   => p_assignment_id
884                      ,p_effective_date          => g_ext_asg_details(p_assignment_id).teacher_start_date
885                      );
886 
887   -- Have we found any secondary assignments?
888   IF l_all_sec_asgs.COUNT > 0 THEN
889 
890     l_curr_asg_id := l_all_sec_asgs.FIRST;
891 
892     WHILE l_curr_asg_id IS NOT NULL
893     LOOP
894 
895       debug('Processing Secondary Asg :'||to_char(l_curr_asg_id),30);
896 
897       -- Delete the global for lever restarter dates for secondary asg.
898       g_sec_leaver_dates.DELETE;
899 
900       -- Store all the leaver and restarter dates for the secondary asg
901       store_leaver_restarter_dates
902                 (p_assignment_id => l_curr_asg_id
903                 );
904 
905       -- MULT-LR changes
906       IF l_curr_asg_id = g_primary_assignment_id THEN
907         --g_asg_leaver_events_table(l_curr_asg_id) := g_primary_leaver_dates ;
908 
909         --8iComp
910         debug('inserting in new collection...', 32);
911         l_insert_rec := set_g_per_asg_leaver_dates
912                        ( p_leaver_dates_type => g_primary_leaver_dates) ;
913         debug('l_insert_rec: '|| l_insert_rec, 34) ;
914 
915       ELSE
916         --g_asg_leaver_events_table(l_curr_asg_id) := g_sec_leaver_dates ;
917         --8iComp
918         debug('inserting in new collection...', 36);
919         l_insert_rec := set_g_per_asg_leaver_dates
920                      ( p_leaver_dates_type => g_sec_leaver_dates) ;
921         debug('l_insert_rec: '|| l_insert_rec, 38) ;
922 
923       END IF;
924 
925       -- Assign the current asg id to prev asg id
926       -- and reset curr asg id, ready for the next one
927       l_prev_asg_id := l_curr_asg_id;
928       l_curr_asg_id := NULL;
929 
930       -- Get next secondary assignment
931       l_curr_asg_id := l_all_sec_asgs.NEXT(l_prev_asg_id);
932 
933     END LOOP; -- l_curr_asg_id IS NOT NULL
934     --
935   END IF; -- l_all_sec_asgs.COUNT > 0 THEN
936 
937   -- MULT-LR --
938   -- print all the events stored so far.
939   -- Print the Events table only if Debug is switched on.
940   IF NVL(g_trace,'N') = 'Y' THEN
941     print_events_table ();
942   END IF;
943 
944   -- rest the g_primary_assignment_id
945   g_primary_assignment_id := p_assignment_id;
946 
947   debug('g_asg_events.COUNT: '||to_char(g_asg_events.COUNT),40) ;
948 
949   -- This function is basically used by TP1P report, since we are using this function for TP1 report
950   -- till now code for both report is common, following portion will used only by TP1 report
951   IF g_extract_type = 'TP1' THEN
952       IF g_per_asg_leaver_dates.COUNT > 0 THEN --chk this table has any leaver events for the person
953          FOR l_itr IN g_per_asg_leaver_dates.FIRST..g_per_asg_leaver_dates.LAST
954            loop
955 	          debug('Checking asg from g_per_asg_leaver_dates : '||g_per_asg_leaver_dates(l_itr).assignment_id,41) ;
956 	          if l_leaver_assignments.EXISTS(g_per_asg_leaver_dates(l_itr).assignment_id) then
957                      debug('asg already exist in l_leaver_assignments',42) ;
958 	             NULL;
959                    else
960 		     debug('Storing in l_leaver_assignments ', 43) ;
961                   l_leaver_assignments(g_per_asg_leaver_dates(l_itr).assignment_id) := g_per_asg_leaver_dates(l_itr).assignment_id; -- sore leaver asg ids
962 	         end if;
963           end loop;
964        end if;
965 
966    debug('l_leaver_assignments.count: '||l_leaver_assignments.count,44) ;
967    debug('Teacher assignment Count :'||g_teach_asg_count,45) ;
968 
969      if g_teach_asg_count = l_leaver_assignments.count THEN -- if all asgs of person have some leaver events
970         i := l_leaver_assignments.first;                                                          -- need to chk all asgs for restarter event
971         WHILE i IS NOT NULL
972          loop
973          debug('Checking Asg from l_leaver_assignments : '||l_leaver_assignments(i),46) ;
974          l_leaver_yn := FALSE;
975          FOR i_itr_1 IN g_per_asg_leaver_dates.FIRST..g_per_asg_leaver_dates.LAST
976            LOOP
977               IF l_leaver_assignments(i) = g_per_asg_leaver_dates(i_itr_1).assignment_id AND
978                  g_per_asg_leaver_dates(i_itr_1).restarter_date IS NULL THEN --for each asgs check coresponding restarter event
979                  l_leaver_yn := TRUE;
980 		 debug(l_leaver_assignments(i)||' Asg is leaver' , 47) ;
981               END if;
982            END LOOP ; --
983 
984        IF NOT l_leaver_yn THEN -- if any asg is not leaver person must be reported , and no need to chk further
985           l_inclusion_flag:= 'Y';
986           debug(l_leaver_assignments(i)||' Asg is not leaver for this pension Year, person should be reported' , 48);
987 	  debug('l_inclusion_flag : '||l_inclusion_flag, 49);
988           RETURN l_inclusion_flag ;
989         END if;
990         i:= l_leaver_assignments.NEXT(i);
991       END loop;
992 
993       debug(' All Asgs are leaver for this person' , 49);
994       l_inclusion_flag:= 'N'; -- all asg has leaver events and no corresponding restarter event
995    Else
996     l_inclusion_flag:= 'Y';   -- not all asg of person has leaver event
997     debug(' Not all asgs of this person are leavers ' , 50);
998     debug('l_inclusion_flag : '||l_inclusion_flag, 50);
999   END IF; --g_ext_asg_details.COUNT = l_leaver_assignments.count
1000 
1001  RETURN l_inclusion_flag ; -- following part of code is for TP1P report hence returning
1002 END IF;
1003 
1004   -- Step 1d) Sort events by Ascending Date if we have found more than 1 events
1005   IF g_asg_events.COUNT > 1 THEN
1006     sort_stored_events;
1007   END IF;
1008 
1009   IF g_asg_events.COUNT > 0 THEN
1010 
1011     -- If the first event is a primary leaver event, then we need
1012     -- a W on withdrawal conf and
1013     -- the end date should be set as the event date
1014     IF (g_asg_count
1015         +
1016         nvl(g_asg_events(g_asg_events.FIRST).asg_count_change, 0)
1017        ) <= 0 THEN
1018 
1019       l_person_leaver_date := g_asg_events(g_asg_events.FIRST).event_date ;
1020 
1021       debug('l_person_leaver_date: '||to_char(l_person_leaver_date),50) ;
1022 
1023       -- changed function with the date parameter.
1024       l_reported := chk_has_teacher_been_reported
1025                           ( p_person_id => p_person_id
1026                            ,p_leaver_date  => l_person_leaver_date
1027                            );
1028 
1029       debug('l_reported: '||l_reported,60) ;
1030 
1031       IF l_reported = 'N' THEN
1032         l_inclusion_flag := 'Y' ;
1033       END IF;
1034     END IF;
1035 
1036     debug('l_inclusion_flag: '||l_inclusion_flag, 70) ;
1037     debug('g_latest_start_date: '||to_char(g_latest_start_date)) ;
1038 
1039     IF l_inclusion_flag = 'N' THEN
1040 
1041       -- Process each event in the global collection
1042       FOR l_itr IN g_asg_events.FIRST..g_asg_events.LAST
1043       LOOP -- through the sorted stored events
1044 
1045         debug('g_asg_events(l_itr).event_date: '||to_char(g_asg_events(l_itr).event_date),80) ;
1046         -- check if the event date is before the g_latest_start_date
1047         -- coz after this date there are no more person leaver events.
1048         IF (g_asg_events(l_itr).event_date < g_latest_start_date)
1049         THEN
1050           l_prev_asg_count := g_asg_count;
1051 
1052           -- Check if this event needs to be skipped
1053           IF (l_skip_itr IS NULL
1054               OR
1055               l_itr <> l_skip_itr
1056              ) THEN
1057 
1058             g_asg_count := g_asg_count
1059                            +
1060                            nvl(g_asg_events(l_itr).asg_count_change
1061                               , 0);
1062           END IF;
1063 
1064           -- Eliminate duplicate changes as we will be re-calculating all
1065           -- data elements that are non-static
1066           -- Also eliminate the event if its a primary leaver event as
1067           -- we process these when deciding on withdrawal conf
1068           -- Now we also skip events if they are set in l_skip_itr
1069           IF (l_itr = g_asg_events.FIRST -- The event is the first one
1070               OR -- the date is not the same as previous event date
1071               g_asg_events(l_itr).event_date <>
1072                g_asg_events(g_asg_events.PRIOR(l_itr)).event_date
1073              )
1074              AND
1075              (g_asg_count > 0
1076              )
1077              AND
1078              -- Check if this event needs to be skipped
1079              (l_skip_itr IS NULL
1080               OR
1081               l_itr <> l_skip_itr
1082              ) THEN
1083 
1084             l_next_itr := g_asg_events.NEXT(l_itr);
1085 
1086             --  Bugfix 3880543:REHIRE :
1087             -- We need to pre-Evaluate the next event
1088             --  We might want to skip the next event if the
1089             --  next event date is equal to current event date+1
1090             --  as it could result in a line with start date
1091             --  greater than end date. We only want to skip
1092             --  if the next event will not result in a primary
1093             --  leaver event, meaning it will not coz g_asg_count
1094             --  to become zero.
1095             IF l_next_itr IS NOT NULL
1096                AND
1097                ( (g_asg_count
1098                   +
1099                   nvl(g_asg_events(l_next_itr).asg_count_change, 0)
1100                  ) > 0
1101                )  THEN
1102 
1103               -- The start date greater than end data problem
1104               -- can only occur in the following situation
1105               -- If the current event is a (new line) leaver event AND
1106               -- Next event is NOT a leaver event AND
1107               -- Next event date is EQUAL to current event date + 1
1108               IF (INSTR(nvl(g_asg_events(l_itr).event_type,'XX')
1109                       ,'LEAVER'
1110                       ) > 0
1111                  )
1112                  AND
1113                  (INSTR(nvl(g_asg_events(l_next_itr).event_type,'XX')
1114                        ,'LEAVER'
1115                        ) <= 0
1116                  )
1117                  AND
1118                  ( g_asg_events(l_itr).event_date + 1
1119                    =
1120                    g_asg_events(l_next_itr).event_date
1121                  ) THEN
1122                  -- We want to skip the next event, Set skip itr
1123                 l_skip_itr := l_next_itr;
1124 
1125                 l_prev_asg_count := g_asg_count;
1126 
1127                 g_asg_count := g_asg_count
1128                                +
1129                                nvl(g_asg_events(l_skip_itr).asg_count_change
1130                                 , 0);
1131 
1132                 -- Get next-of-next and treat it as the next event
1133                 l_next_of_next_itr := g_asg_events.NEXT(l_next_itr);
1134                 l_next_itr := l_next_of_next_itr;
1135 
1136               ELSE
1137                 l_skip_itr := NULL;
1138               END IF;
1139 
1140             ELSE -- Pre-Evaluating
1141 
1142               l_skip_itr := NULL;
1143 
1144             END IF; -- Pre-Evaluating
1145 
1146             -- Now doing the real processing
1147             IF l_next_itr IS NOT NULL THEN
1148 
1149               -- Also check if the next event is a Primary Leaver event
1150               IF -- If the next event will cause g_asg_count to become zero
1151                  ( (g_asg_count
1152                     +
1153                     nvl(g_asg_events(l_next_itr).asg_count_change, 0)
1154                    ) <= 0
1155                  )   THEN
1156 
1157                 l_person_leaver_date := g_asg_events(l_next_itr).event_date ;
1158 
1159                 debug('l_person_leaver_date: '||to_char(l_person_leaver_date),90) ;
1160                 -- changed function with the date parameter.
1161                 l_reported := chk_has_teacher_been_reported
1162                                      ( p_person_id  => p_person_id
1163                                       ,p_leaver_date => l_person_leaver_date
1164                                      );
1165                 debug('l_reported: '||l_reported,110) ;
1166 
1167                 IF l_reported = 'N' THEN
1168                   l_inclusion_flag := 'Y' ;
1169                   EXIT;
1170                 END IF;
1171 
1172               END IF;
1173 
1174             END IF; -- l_next_itr IS NOT NULL THEN
1175 
1176           END IF; -- if this date <> last date to eliminate duplicates and primary leaver
1177 
1178         ELSE
1179           -- There were no person level leaver events found
1180           -- that have not been reported earlier.
1181           -- so set the return value and Exit.
1182           l_inclusion_flag := 'N' ;
1183           EXIT ;
1184         END IF ; -- g_asg_events(l_itr).event_date < g_latest_start_date
1185         --
1186       END LOOP; -- through the sorted stored events
1187     END IF ; -- l_inclusion_flag = 'N' ;
1188     --
1189   END IF; -- g_asg_events.COUNT > 0 THEN
1190 
1191   -- QAB1: Restore the value of g_asg_count.
1192   g_asg_count := l_asg_count ;
1193 
1194   debug('l_inclusion_flag: '||l_inclusion_flag, 120);
1195   debug_exit(l_proc_name);
1196 
1197   RETURN l_inclusion_flag ;
1198 
1199 EXCEPTION
1200     WHEN OTHERS THEN
1201 
1202       -- Reset the global variable containing events for this person
1203       g_asg_events.DELETE;
1204       -- QAB1: reset the global.
1205       g_asg_count := l_asg_count;
1206 
1207       debug('SQLCODE :'||to_char(SQLCODE), 40);
1208       debug('SQLERRM :'||SQLERRM, 50);
1209 
1210       debug_exit(' Others in '||l_proc_name
1211                 ,'Y' -- turn trace off
1212                 );
1213       RAISE;
1214 END; -- chk_person_leaver
1215 
1216 --
1217 --
1218 Procedure Get_Udt_Data ( p_udt_name       in varchar2
1219                         ,p_effective_date in date ) Is
1220 
1221 Cursor Get_table_id ( c_udt_name in varchar2 ) Is
1222  select tbls.user_table_id
1223    from pay_user_tables  tbls
1224   where tbls.user_table_name = c_udt_name
1225     and ((business_group_id is null and legislation_code is null)
1226                       or (legislation_code is not null
1227                             and legislation_code = 'GB')
1228                       or (business_group_id is not null
1229                             and business_group_id = g_business_group_id)
1230         );
1231 
1232 Cursor Get_Col_Name ( c_user_table_id in number ) Is
1233  Select user_column_id, user_column_name
1234    from pay_user_columns
1235   where user_table_id = c_user_table_id
1236     and ((business_group_id is null and legislation_code is null)
1237                       or (legislation_code is not null
1238                             and legislation_code = 'GB')
1239                       or (business_group_id is not null
1240                             and business_group_id = g_business_group_id)
1241         )
1242   order by user_column_id;
1243 
1244 Cursor Get_Row_Name ( c_user_table_id  in number
1245                      ,c_effective_date in date ) Is
1246  Select user_row_id, row_low_range_or_name
1247    from pay_user_rows_f
1248   where user_table_id = c_user_table_id
1249     and trunc(c_effective_date) between effective_start_date
1250                                     and effective_end_date
1251     and ((business_group_id is null and legislation_code is null)
1252                       or (legislation_code is not null
1253                             and legislation_code = 'GB')
1254                       or (business_group_id is not null
1255                             and business_group_id = g_business_group_id)
1256         )
1257   order by display_sequence;
1258 
1259 Cursor Get_Matrix_Value ( c_user_column_id in number
1260                          ,c_user_row_id    in number ) Is
1261  Select value, effective_start_date, effective_end_date
1262    from pay_user_column_instances_f
1263   where user_column_id = c_user_column_id
1264     and user_row_id    = c_user_row_id
1265     and ((business_group_id is null and legislation_code is null)
1266                       or (legislation_code is not null
1267                             and legislation_code = 'GB')
1268                       or (business_group_id is not null
1269                             and business_group_id = g_business_group_id)
1270         );
1271 
1272 -- Comment this out as this is not needed as a local collection
1273 -- This has been declared in the pkg header as global collection
1274 -- g_udt_rec          t_udt_array;
1275 l_user_column_name pay_user_columns.user_column_name%TYPE;
1276 l_user_row_name    pay_user_rows_f.row_low_range_or_name%TYPE;
1277 l_matrix_value     pay_user_column_instances_f.value%TYPE;
1278 l_user_table_id    pay_user_tables.user_table_id%TYPE;
1279 l_user_column_id   pay_user_columns.user_column_id%TYPE;
1280 l_user_row_id      pay_user_rows_f.user_row_id%TYPE;
1281 l_idx              number;
1282 l_proc_name        varchar2(60) := g_proc_name || 'get_udt_data';
1283 
1284 Begin
1285    debug_enter(l_proc_name);
1286 
1287    Open  Get_table_id ( c_udt_name => p_udt_name);
1288    Fetch Get_table_id Into l_user_table_id;
1289    Close Get_table_id;
1290    l_idx := 1;
1291    For i in  Get_Col_Name (c_user_table_id => l_user_table_id) Loop
1292      debug(l_proc_name, 20);
1293      l_user_column_id   := i.user_column_id;
1294      l_user_column_name := i.user_column_name;
1295      For j in Get_Row_Name (c_user_table_id  => l_user_table_id
1296                            ,c_effective_date => p_effective_date) Loop
1297        debug(l_proc_name, 30);
1298        l_user_row_id   := j.user_row_id;
1299        l_user_row_name := j.row_low_range_or_name;
1300         For k in Get_Matrix_Value ( c_user_column_id => l_user_column_id
1301                                    ,c_user_row_id    => l_user_row_id ) Loop
1302            g_udt_rec(l_idx).column_name  := l_user_column_name;
1303            g_udt_rec(l_idx).row_name     := l_user_row_name;
1304            g_udt_rec(l_idx).matrix_value := k.value;
1305            g_udt_rec(l_idx).start_date   := Trunc(k.effective_start_date);
1306            g_udt_rec(l_idx).end_date     := Trunc(k.effective_end_date);
1307            l_idx := l_idx + 1; l_matrix_value := Null;
1308            debug(l_proc_name, 40);
1309         End Loop;
1310         l_user_row_name:=Null;
1311      End Loop;
1312    End Loop;
1313 
1314    debug_exit (l_proc_name);
1315 
1316 End Get_Udt_Data;
1317 --
1318 Procedure Get_Elements_Frm_UDT
1319                     (p_assignment_id    IN  NUMBER -- 4336613 : LARP_SPAP_3A : new param
1320                     ) Is
1321 
1322 l_counter     number;
1323 l_proc_name          varchar2(70):= g_proc_name||'Get_Elements_Frm_UDT';
1324 
1325 -- 4336613 : LARP_SPAP_3A
1326 l_error            number;
1327 
1328 Begin
1329 
1330   debug_enter(l_proc_name);
1331 
1332   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1333   -- ~ First element details from UDT for London Allowance ~
1334   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1335   l_counter :=0;
1336 
1337   -- 4336613 : LARP_SPAP_3A : add this check for all the LARP elements
1338   -- Only if present in UDT, put values to global (record) variable
1339   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1340                      ,p_row_name    => 'LARP Inner Allowance'
1341                      ) IS NOT NULL
1342       )
1343   THEN
1344 
1345     l_counter := l_counter + 1;
1346     g_udt_element_LondAll(l_counter).element_name := Get_Udt_Value
1347                                           (p_column_name => 'Attribute Location Qualifier 1'
1348                                           ,p_row_name    => 'LARP Inner Allowance'
1349                                            );
1350 
1351     g_udt_element_LondAll(l_counter).allowance_code := Get_Udt_Value
1352                                           (p_column_name => 'Allowance Code'
1353                                           ,p_row_name    => 'LARP Inner Allowance'
1354                                            );
1355 
1356     g_udt_element_LondAll(l_counter).input_value_name := Get_Udt_Value
1357                                           (p_column_name => 'Attribute Location Qualifier 2'
1358                                           ,p_row_name    => 'LARP Inner Allowance'
1359                                            );
1360 
1361     debug('LondonAll Ele Count :'||to_char(g_udt_element_LondAll.COUNT), 20);
1362   END IF;
1363 
1364 
1365   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1366   -- ~ Second element details from UDT for London Allowance ~
1367   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1368 
1369   -- 4336613 : LARP_SPAP_3A : add this check for all the LARP elements
1370   -- Only if present in UDT, put values to global (record) variable
1371   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1372                      ,p_row_name    => 'LARP Outer Allowance'
1373                      ) IS NOT NULL
1374       )
1375   THEN
1376 
1377     l_counter := l_counter + 1;
1378     g_udt_element_LondAll(l_counter).allowance_code := Get_Udt_Value
1379                                           (p_column_name => 'Allowance Code'
1380                                           ,p_row_name    => 'LARP Outer Allowance'
1381                                            );
1382     g_udt_element_LondAll(l_counter).element_name := Get_Udt_Value
1383                                           (p_column_name => 'Attribute Location Qualifier 1'
1384                                           ,p_row_name    => 'LARP Outer Allowance'
1385                                            );
1386     g_udt_element_LondAll(l_counter).input_value_name := Get_Udt_Value
1387                                           (p_column_name => 'Attribute Location Qualifier 2'
1388                                           ,p_row_name    => 'LARP Outer Allowance'
1389                                            );
1390     debug('LondonAll Ele Count :'||to_char(g_udt_element_LondAll.COUNT), 30);
1391   END IF;
1392 
1393   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1394   -- ~ Third element details from UDT for London Allowance ~
1395   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1396 
1397   -- 4336613 : LARP_SPAP_3A : add this check for all the LARP elements
1398   -- Only if present in UDT, put values to global (record) variable
1399   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1400                      ,p_row_name    => 'LARP Fringe Allowance'
1401                      ) IS NOT NULL
1402       )
1403   THEN
1404 
1405     l_counter := l_counter + 1;
1406     g_udt_element_LondAll(l_counter).allowance_code := Get_Udt_Value
1407                                           (p_column_name => 'Allowance Code'
1408                                           ,p_row_name    => 'LARP Fringe Allowance'
1409                                            );
1410     g_udt_element_LondAll(l_counter).element_name := Get_Udt_Value
1411                                           (p_column_name => 'Attribute Location Qualifier 1'
1412                                           ,p_row_name    => 'LARP Fringe Allowance'
1413                                            );
1414     g_udt_element_LondAll(l_counter).input_value_name  := Get_Udt_Value
1415                                           (p_column_name => 'Attribute Location Qualifier 2'
1416                                           ,p_row_name    => 'LARP Fringe Allowance'
1417                                            );
1418     debug('LondonAll Ele Count :'||to_char(g_udt_element_LondAll.COUNT), 40);
1419   END IF;
1420 
1421   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1422   -- ~ Fourth element details from UDT for London Allowance ~
1423   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1424 
1425   -- 4336613 : LARP_SPAP_3A : add this check for all the LARP elements
1426   -- Only if present in UDT, put values to global (record) variable
1427   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1428                      ,p_row_name    => 'LARP Inner Plus Inner Supplement'
1429                      ) IS NOT NULL
1430       )
1431   THEN
1432 
1433     l_counter := l_counter + 1;
1434     g_udt_element_LondAll(l_counter).allowance_code := Get_Udt_Value
1435                                           (p_column_name => 'Allowance Code'
1436                                           ,p_row_name    => 'LARP Inner Plus Inner Supplement'
1437                                            );
1438     g_udt_element_LondAll(l_counter).element_name := Get_Udt_Value
1439                                           (p_column_name => 'Attribute Location Qualifier 1'
1440                                           ,p_row_name    => 'LARP Inner Plus Inner Supplement'
1441                                            );
1442     g_udt_element_LondAll(l_counter).input_value_name  := Get_Udt_Value
1443                                           (p_column_name => 'Attribute Location Qualifier 2'
1444                                           ,p_row_name    => 'LARP Inner Plus Inner Supplement'
1445                                            );
1446     debug('LondonAll Ele Count :'||to_char(g_udt_element_LondAll.COUNT), 50);
1447   END IF;
1448 
1449   -- 4336613 : LARP_SPAP_3A : raise an error if no LARP entried found in the UDT
1450   IF g_udt_element_LondAll.COUNT = 0 THEN
1451     debug(' ---------  No LARP entries found in UDT ---------', 55);
1452 
1453     -- 4336613 : LARP_SPAP_3A : new error message for LARP
1454     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
1455                  (p_assignment_id => p_assignment_id
1456                  ,p_error_text    => 'BEN_94227_T1_LARP_ELE_NOTEXIST'
1457                  ,p_error_number  => 94227
1458                  );
1459   END IF;
1460 
1461 
1462   --debug('Entering the Special Allowance element from UDT');
1463   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1464   -- ~ First element details from UDT for Special Allowance ~
1465   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1466   l_counter :=0;
1467 
1468   -- 4336613 : LARP_SPAP_3A : add this check for all the SPAP elements
1469   -- Only if present in UDT, put values to global (record) variable
1470   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1471                      ,p_row_name    => 'SPAP Lower Rate'
1472                      ) IS NOT NULL
1473       )
1474   THEN
1475 
1476     l_counter := l_counter + 1;
1477     g_udt_element_SpcAll(l_counter).allowance_code := Get_Udt_Value
1478                                           (p_column_name => 'Allowance Code'
1479                                           ,p_row_name    => 'SPAP Lower Rate'
1480                                            );
1481     g_udt_element_SpcAll(l_counter).element_name := Get_Udt_Value
1482                                           (p_column_name => 'Attribute Location Qualifier 1'
1483                                           ,p_row_name    => 'SPAP Lower Rate'
1484                                            );
1485     g_udt_element_SpcAll(l_counter).input_value_name := Get_Udt_Value
1486                                           (p_column_name => 'Attribute Location Qualifier 2'
1487                                           ,p_row_name    => 'SPAP Lower Rate'
1488                                            );
1489     debug('SpcAll Ele Count :'||to_char(g_udt_element_SpcAll.COUNT), 60);
1490   END IF;
1491 
1492   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1493   -- ~ Second element details from UDT for Special Allowance ~
1494   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1495 
1496   -- 4336613 : LARP_SPAP_3A : add this check for all the SPAP elements
1497   -- Only if present in UDT, put values to global (record) variable
1498   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1499                      ,p_row_name    => 'SPAP Higher Rate'
1500                      ) IS NOT NULL
1501       )
1502   THEN
1503 
1504     l_counter := l_counter + 1;
1505     g_udt_element_SpcAll(l_counter).allowance_code := Get_Udt_Value
1506                                           (p_column_name => 'Allowance Code'
1507                                           ,p_row_name    => 'SPAP Higher Rate'
1508                                            );
1509     g_udt_element_SpcAll(l_counter).element_name := Get_Udt_Value
1510                                           (p_column_name => 'Attribute Location Qualifier 1'
1511                                           ,p_row_name    => 'SPAP Higher Rate'
1512                                            );
1513     g_udt_element_SpcAll(l_counter).input_value_name := Get_Udt_Value
1514                                           (p_column_name => 'Attribute Location Qualifier 2'
1515                                           ,p_row_name    => 'SPAP Higher Rate'
1516                                            );
1517     debug('SpcAll Ele Count :'||to_char(g_udt_element_SpcAll.COUNT), 70);
1518   END IF;
1519 
1520   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1521   -- ~ Third element details from UDT for Special Allowance ~
1522   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1523 
1524   -- 4336613 : LARP_SPAP_3A : add this check for all the SPAP elements
1525   -- Only if present in UDT, put values to global (record) variable
1526   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1527                      ,p_row_name    => 'SPAP Special Needs Lower Rate'
1528                      ) IS NOT NULL
1529       )
1530   THEN
1531 
1532     l_counter := l_counter + 1;
1533     g_udt_element_SpcAll(l_counter).allowance_code := Get_Udt_Value
1534                                           (p_column_name => 'Allowance Code'
1535                                           ,p_row_name    => 'SPAP Special Needs Lower Rate'
1536                                           );
1537     g_udt_element_SpcAll(l_counter).element_name := Get_Udt_Value
1538                                           (p_column_name => 'Attribute Location Qualifier 1'
1539                                           ,p_row_name    => 'SPAP Special Needs Lower Rate'
1540                                           );
1541     g_udt_element_SpcAll(l_counter).input_value_name  := Get_Udt_Value
1542                                           (p_column_name => 'Attribute Location Qualifier 2'
1543                                           ,p_row_name    => 'SPAP Special Needs Lower Rate'
1544                                            );
1545     debug('SpcAll Ele Count :'||to_char(g_udt_element_SpcAll.COUNT), 80);
1546   END IF;
1547 
1548   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1549   -- ~ Fourth element details from UDT for Special Allowance ~
1550   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1551 
1552   -- 4336613 : LARP_SPAP_3A : add this check for all the SPAP elements
1553   -- Only if present in UDT, put values to global (record) variable
1554   IF ( Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
1555                      ,p_row_name    => 'SPAP Special Needs Higher Rate'
1556                      ) IS NOT NULL
1557       )
1558   THEN
1559 
1560   l_counter := l_counter + 1;
1561   g_udt_element_SpcAll(l_counter).allowance_code := Get_Udt_Value
1562                                         (p_column_name => 'Allowance Code'
1563                                         ,p_row_name    => 'SPAP Special Needs Higher Rate'
1564                                          );
1565   g_udt_element_SpcAll(l_counter).element_name := Get_Udt_Value
1566                                         (p_column_name => 'Attribute Location Qualifier 1'
1567                                         ,p_row_name    => 'SPAP Special Needs Higher Rate'
1568                                          );
1569   g_udt_element_SpcAll(l_counter).input_value_name  := Get_Udt_Value
1570                                         (p_column_name => 'Attribute Location Qualifier 2'
1571                                         ,p_row_name    => 'SPAP Special Needs Higher Rate'
1572                                          );
1573   debug('SpcAll Ele Count :'||to_char(g_udt_element_SpcAll.COUNT), 90);
1574   END IF;
1575 
1576   -- 4336613 : LARP_SPAP_3A : raise an error if no SPAP entried found in the UDT
1577   IF g_udt_element_SpcAll.COUNT = 0 THEN
1578     debug(' ---------  No SPAP entries found in UDT ---------', 95);
1579 
1580     -- 4336613 : LARP_SPAP_3A : new error message for SPAP
1581     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
1582                  (p_assignment_id => p_assignment_id
1583                  ,p_error_text    => 'BEN_94228_T1_SPAP_ELE_NOTEXIST'
1584                  ,p_error_number  => 94228
1585                  );
1586   END IF;
1587 
1588 
1589   debug_exit(l_proc_name);
1590 
1591 EXCEPTION
1592   WHEN OTHERS THEN
1593     debug_exit(' Others in '||l_proc_name);
1594     RAISE;
1595 End Get_Elements_Frm_UDT;
1596 
1597 --
1598 -- ----------------------------------------------------------------------------
1599 -- |--------------------------< get_pay_bal_id >------------------------------|
1600 -- ----------------------------------------------------------------------------
1601 function get_pay_bal_id
1602   (p_balance_name      in         varchar2
1603   ,p_business_group_id in         number
1604   ,p_legislation_code  OUT NOCOPY VARCHAR2) -- 4336613 : new param added
1605   return number is
1606 --
1607   l_proc_name        varchar2(60) := g_proc_name || 'get_pay_bal_id';
1608   l_bal_type_id      csr_get_pay_bal_id%rowtype;
1609 --
1610 begin
1611 
1612   --
1613   -- hr_utility.set_location('Entering: '||l_proc_name, 5);
1614   --
1615   debug_enter(l_proc_name);
1616 
1617   open csr_get_pay_bal_id
1618     (c_balance_name      => p_balance_name
1619     ,c_business_group_id => p_business_group_id);
1620   fetch csr_get_pay_bal_id into l_bal_type_id;
1621   IF csr_get_pay_bal_id%NOTFOUND THEN
1622     debug('Not found', 20);
1623   END IF;
1624 
1625   close csr_get_pay_bal_id;
1626 
1627   debug_exit(l_proc_name);
1628 
1629   --
1630   -- hr_utility.set_location('Leaving: '||l_proc_name, 15);
1631   --
1632   p_legislation_code := l_bal_type_id.legislation_code;
1633 
1634   return l_bal_type_id.balance_type_id;
1635 
1636 EXCEPTION
1637   WHEN OTHERS THEN
1638     debug_exit(' Others in '||l_proc_name);
1639     RAISE;
1640 end get_pay_bal_id;
1641 --
1642 -- ----------------------------------------------------------------------------
1643 -- |-------------------------< get_pay_ele_ids_from_bal >---------------------|
1644 -- ----------------------------------------------------------------------------
1645 procedure get_pay_ele_ids_from_bal
1646   (p_assignment_id        in     number
1647   ,p_balance_type_id      in     number
1648   ,p_effective_date       in     date
1649   ,p_error_text           in     varchar2
1650   ,p_error_number         in     number
1651   ,p_business_group_id    in     number
1652   ,p_tab_ele_ids          out nocopy t_ele_ids_from_bal
1653   ,p_token                in     varchar2 default null
1654   ) is
1655 --
1656   l_proc_name        varchar2(60) := g_proc_name || 'get_pay_ele_ids_from_bal';
1657   l_ele_ids          csr_get_pay_ele_ids_from_bal%rowtype;
1658   l_error            number ;
1659   l_ele_type_id      number ;
1660 
1661 --
1662 begin
1663 
1664   --
1665   -- hr_utility.set_location('Entering: '||l_proc_name, 5);
1666   --
1667 
1668   debug_enter(l_proc_name);
1669 
1670   debug('p_assignment_id: '||to_char(p_assignment_id),10) ;
1671   debug('p_balance_type_id: '||to_char(p_balance_type_id)) ;
1672   debug('p_effective_date: '||to_char(p_effective_date)) ;
1673   debug('p_error_text: '||p_error_text) ;
1674   debug('p_error_number: '||to_char(p_error_number)) ;
1675   debug('p_token: '||p_token) ;
1676 
1677 
1678   open csr_get_pay_ele_ids_from_bal
1679     (c_balance_type_id      => p_balance_type_id
1680     ,c_effective_date       => p_effective_date
1681     ,c_business_group_id    => p_business_group_id
1682     );
1683   loop
1684 
1685     fetch csr_get_pay_ele_ids_from_bal into l_ele_ids;
1686     exit when csr_get_pay_ele_ids_from_bal%notfound;
1687       --
1688       p_tab_ele_ids(l_ele_ids.element_type_id).element_type_id := l_ele_ids.element_type_id;
1689       p_tab_ele_ids(l_ele_ids.element_type_id).input_value_id  := l_ele_ids.input_value_id;
1690       --
1691       debug(l_proc_name, 20);
1692   end loop;
1693 
1694   if csr_get_pay_ele_ids_from_bal%rowcount = 0
1695   and p_error_text IS NOT NULL -- don't raise an error, as in OSLA case.
1696   then
1697 
1698      debug(l_proc_name, 30);
1699      l_error := pqp_gb_tp_extract_functions.raise_extract_error
1700                       (p_business_group_id => g_business_group_id
1701                       ,p_assignment_id     => p_assignment_id
1702                       ,p_error_text        => p_error_text
1703                       ,p_error_number      => p_error_number
1704                       ,p_token1            => p_token
1705                       );
1706 
1707   end if;
1708   close csr_get_pay_ele_ids_from_bal;
1709 
1710 
1711   -- debug the element type ids fetched for a balance
1712   debug('p_balance_type_id: '||to_char(p_balance_type_id),40) ;
1713   l_ele_type_id := p_tab_ele_ids.FIRST;
1714   WHILE l_ele_type_id IS NOT NULL
1715   LOOP
1716     debug('element_type_id : '||to_char(l_ele_type_id), 50);
1717     l_ele_type_id := p_tab_ele_ids.NEXT(l_ele_type_id);
1718   END LOOP;
1719   -----
1720 
1721   debug_exit(l_proc_name);
1722 
1723 --Added by tmehra for nocopy changes Feb'03
1724 
1725 EXCEPTION
1726     WHEN OTHERS THEN
1727        debug_exit(l_proc_name);
1728        p_tab_ele_ids.delete;
1729        raise;
1730 
1731 end get_pay_ele_ids_from_bal;
1732 
1733 --
1734 Function Get_Udt_Value( p_table_name        in varchar2
1735                        ,p_column_name       in varchar2
1736                        ,p_row_name          in varchar2
1737                        ,p_effective_date    in date
1738                        ,p_business_group_id in number)     Return varchar2 Is
1739 
1740 /*
1741 Cursor Get_Matrix_Value ( c_user_table_name  in varchar
1742                          ,c_user_column_name in varchar
1743                          ,c_user_row_name    in varchar
1744                          ,c_effective_date   in date) Is
1745  select  put.user_table_name
1746         ,puc.user_column_name
1747         ,pur.row_low_range_or_name
1748         ,pci.value
1749  from    pay_user_tables             put
1750         ,pay_user_columns            puc
1751         ,pay_user_rows_f             pur
1752         ,pay_user_column_instances_f pci
1753  where   put.user_table_name       = c_user_table_name
1754    and   puc.user_table_id         = put.user_table_id
1755    and   puc.user_column_name      = c_user_column_name
1756    and   pur.row_low_range_or_name = c_user_row_name
1757    and   pur.user_table_id         = put.user_table_id
1758    and   pci.user_column_id        = puc.user_column_id
1759    and   pci.user_row_id           = pur.user_row_id
1760    and   Trunc(c_effective_date) between pur.effective_start_date
1761                                      and pur.effective_end_date
1762    and   Trunc(c_effective_date) between pci.effective_start_date
1763                                      and pci.effective_end_date
1764     and ((pci.business_group_id is null and pci.legislation_code is null)
1765                       or (pci.legislation_code is not null
1766                             and pci.legislation_code = 'GB')
1767                       or (pci.business_group_id is not null
1768                             and pci.business_group_id = NVL(p_business_group_id, g_business_group_id))
1769         )
1770  order by put.user_table_name, puc.user_column_name, pur.display_sequence;
1771 */
1772 
1773 l_return_value       pay_user_column_instances_f.value%TYPE;
1774 l_udt_row            Get_Matrix_Value%ROWTYPE;
1775 l_notfound_UDTCache  Boolean;
1776 l_effective_date     date;
1777 l_table_name         pay_user_tables.user_table_name%TYPE;
1778 l_proc_name          varchar2(70) := g_proc_name || 'get_udt_value';
1779 
1780 Begin
1781     debug_enter (l_proc_name);
1782     -- If the effective date or the UDT table name are
1783     -- null then take the ones defined in the package header.
1784     l_effective_date := NVL(p_effective_date,g_effective_date);
1785     l_table_name     := NVL(p_table_name,g_extract_udt_name);
1786     l_notfound_UDTCache := True;
1787     IF NVL(p_business_group_id, g_business_group_id) = g_business_group_id THEN
1788       -- Check if the value can be found in the cached
1789       -- PL/SQL record table for the given effective date
1790       For i In 1..g_udt_rec.count Loop
1791           debug(l_proc_name, 20);
1792           If g_udt_rec(i).column_name = p_column_name And
1793              g_udt_rec(i).row_name    = p_row_name    And
1794              (l_effective_date Between g_udt_rec(i).start_date
1795                                    and g_udt_rec(i).end_date)   Then
1796                l_return_value := g_udt_rec(i).matrix_value;
1797                l_notfound_UDTCache := False;
1798                debug(l_proc_name, 30);
1799                Exit;
1800            End If;
1801       End Loop;
1802     END IF; -- End if of p_business_group = g_business_group check ...
1803     --
1804     debug(l_proc_name, 40);
1805     -- If the value could not found in the cached PL/SQL
1806     -- table then get the value from the UDT database tables
1807     If l_notfound_UDTCache  Then
1808       debug(l_proc_name, 50);
1809       Open Get_Matrix_Value( c_user_table_name  => l_table_name
1810                             ,c_user_column_name => p_column_name
1811                             ,c_user_row_name    => p_row_name
1812                             ,c_effective_date   => l_effective_date
1813                             ,c_business_group_id => p_business_group_id);
1814       Fetch Get_Matrix_Value Into l_udt_row;
1815       If Get_Matrix_Value%NOTFOUND Then
1816          debug(l_proc_name, 60);
1817          l_return_value := Null;
1818          Close Get_Matrix_Value;
1819       Else
1820          Close Get_Matrix_Value;
1821          l_return_value := l_udt_row.value;
1822       End If;
1823     End If;
1824     debug('Return Value :'||l_return_value, 70);
1825     debug_exit (l_proc_name);
1826     Return l_return_value;
1827 EXCEPTION
1828   WHEN OTHERS THEN
1829     debug_exit(' Others in '||l_proc_name);
1830     RAISE;
1831 End Get_Udt_Value;
1832 
1833 
1834 -- 4336613 : OSLA_3A : new function for defined_balance_id, moved from calc_part_time_sal
1835 -- into a separate generic function based on balance_type_id
1836 --
1837 -- ----------------------------------------------------------------------------
1838 -- |--------------------------< get_defined_balance_id >-----------------------|
1839 -- ----------------------------------------------------------------------------
1840 
1841 PROCEDURE get_defined_balance_id(p_assignment_id     IN NUMBER
1842                                 ,p_bal_type_id       IN NUMBER
1843                                 ,p_business_group_id IN NUMBER
1844                                 ,p_tab_bal_name      IN VARCHAR2
1845                                 ,p_seed_flag         IN VARCHAR2)
1846 
1847 IS
1848 --
1849   l_proc_name      VARCHAR2(60) := g_proc_name || 'get_defined_balance_id';
1850   l_error          NUMBER;
1851   l_error_text     VARCHAR2(60);
1852   l_error_number   NUMBER;
1853 --
1854 BEGIN
1855   --
1856   debug_enter (l_proc_name);
1857 
1858   debug('p_assignment_id: '|| to_char(p_assignment_id), 10) ;
1859   debug('p_bal_type_id: '|| to_char(p_bal_type_id)) ;
1860   debug('p_business_group_id: '|| to_char(p_business_group_id)) ;
1861 
1862   IF NOT g_def_bal_id.EXISTS(p_bal_type_id) THEN  -- value is not set yet.
1863     -- get the defined balance id for the designaetd Balance type
1864     -- for Retro payments calculation
1865     debug('Setting the defined balance id ',40);
1866     OPEN csr_get_defined_balance_id
1867               ( p_balance_type_id   => p_bal_type_id
1868                ,p_dimension_name    => '_ASG_RETROELE_RUN'
1869                ,p_business_group_id => p_business_group_id
1870               );
1871     FETCH csr_get_defined_balance_id INTO g_def_bal_id(p_bal_type_id);
1872 
1873       IF csr_get_defined_balance_id%NOTFOUND THEN
1874         debug('Balance Dimension not defined',50);
1875         debug('THIS CONDITION SHOULD NEVER ARISE - RAISE ERROR',60);
1876 
1877         IF p_seed_flag = 'Y' THEN -- raise error for a seeded balance
1878           l_error_text   := 'BEN_94208_EXT_TP1_DEF_BAL_ERR';
1879           l_error_number := 94208;
1880         ELSE -- raise error for user defined balance
1881           l_error_text   := 'BEN_94246_EXT_TP1_DEF_BAL_ERR';
1882           l_error_number := 94246;
1883         END IF;
1884 
1885         l_error := pqp_gb_tp_extract_functions.raise_extract_error
1886                           (p_business_group_id => g_business_group_id
1887                           ,p_assignment_id     => p_assignment_id
1888                           ,p_error_text        => l_error_text
1889                           ,p_error_number      => l_error_number
1890                           ,p_token1            => p_tab_bal_name
1891                           );
1892 
1893       ELSE
1894         debug('g_def_bal_id(p_bal_type_id): '||to_char(g_def_bal_id(p_bal_type_id)),70);
1895       END IF ;
1896 
1897     CLOSE csr_get_defined_balance_id ;
1898   END IF ;
1899 
1900   debug('AFTER: g_def_bal_id (p_bal_type_id):'|| to_char(g_def_bal_id(p_bal_type_id)),80) ;
1901 
1902 
1903   debug_exit (l_proc_name);
1904 EXCEPTION
1905   WHEN OTHERS THEN
1906     debug_exit(' Others in '||l_proc_name);
1907   RAISE;
1908 
1909 END get_defined_balance_id;
1910 
1911 --
1912 -- ----------------------------------------------------------------------------
1913 -- |--------------------------< fetch_eles_from_bals >-----------------------|
1914 -- ----------------------------------------------------------------------------
1915 procedure fetch_eles_from_bals (p_assignment_id     in     number
1916                                ,p_effective_date    in     date
1917                                ,p_business_group_id in     number)
1918 is
1919 --
1920   l_proc_name      varchar2(60) := g_proc_name || 'fetch_eles_from_bals';
1921 
1922   l_tab_bal_name     t_varchar;
1923   l_bal_type_id      pay_balance_types.balance_type_id%type;
1924   l_legislation_code VARCHAR2(30); -- to hold legislation code of balance
1925   l_error            NUMBER ;
1926   l_seed_flag        VARCHAR2(1); -- flag to signify if balance is seeded/user defined
1927 --
1928 begin
1929   --
1930   -- hr_utility.set_location ('Entering: '||l_proc_name, 10);
1931   --
1932   debug_enter (l_proc_name);
1933 
1934   -- Get Absence balance name from the UDT
1935 
1936   l_tab_bal_name(1) := get_udt_value (p_column_name       => 'Attribute Location Qualifier 1'
1937                                      ,p_row_name          => 'Days Excluded Balance'
1938                                      ,p_business_group_id => p_business_group_id
1939                                      );
1940   -- Check whether a balance name is specified otherwise use
1941   -- the seeded generic balance for days excluded
1942 
1943   if l_tab_bal_name(1) is null then
1944 
1945      l_tab_bal_name(1) := 'Teachers Total Days Excluded';
1946 
1947   end if; -- end if of balance name specified check ...
1948 
1949   --
1950   debug(l_proc_name, 20);
1951   debug('Business Group: '||to_char(p_business_group_id));
1952   --
1953 
1954   -- Get Superannuable Salary balance name from the UDT
1955 
1956   l_tab_bal_name(2) := get_udt_value (p_column_name       => 'Attribute Location Qualifier 1'
1957                                      ,p_row_name          => 'Superannuable Salary Balance'
1958                                      ,p_business_group_id => p_business_group_id
1959                                      );
1960   -- Check whether a balance name is specified otherwise use
1961   -- the seeded generic balance for superannuable salary
1962 
1963   if l_tab_bal_name(2) is null then
1964 
1965      l_tab_bal_name(2) := 'Teachers Superannuable Salary';
1966 
1967   end if; -- end if of balance name specified check ...
1968 
1969 
1970   --4336613 : OSLA_3A : new balance seeded for OSLA calculations
1971   -- Get Teachers OSLA Payments balance name from the UDT
1972 
1973   l_tab_bal_name(3) := get_udt_value (p_column_name       => 'Attribute Location Qualifier 1'
1974                                      ,p_row_name          => 'Teachers OSLA Payments Balance'
1975                                      ,p_business_group_id => p_business_group_id
1976                                      );
1977   -- Check whether a balance name is specified otherwise use
1978   -- the seeded generic balance for superannuable salary
1979 
1980   if l_tab_bal_name(3) is null then
1981 
1982      l_tab_bal_name(3) := 'Teachers OSLA Payments';
1983 
1984   end if; -- end if of balance name specified check ...
1985   -- end of OSLA section
1986 
1987   l_tab_bal_name(4) := get_udt_value (p_column_name       => 'Attribute Location Qualifier 1'
1988                                      ,p_row_name          => 'Superannuable Claims Balance'
1989                                      ,p_business_group_id => p_business_group_id
1990                                      );
1991   -- Check whether a balance name is specified otherwise use
1992   -- the seeded generic balance for superannuable salary
1993 
1994   if l_tab_bal_name(4) is null then
1995 
1996      l_tab_bal_name(4) := 'Teachers Superannuable Claims';
1997 
1998   end if; -- end if of balance name specified check ...
1999   -- end of OSLA section
2000 
2001   -- Get Teachers OSLA Payments balance name from the UDT
2002 
2003   l_tab_bal_name(5) := get_udt_value (p_column_name       => 'Attribute Location Qualifier 1'
2004                                      ,p_row_name          => 'Teachers OSLA Claims Balance'
2005                                      ,p_business_group_id => p_business_group_id
2006                                      );
2007   -- Check whether a balance name is specified otherwise use
2008   -- the seeded generic balance for superannuable salary
2009 
2010   if l_tab_bal_name(5) is null then
2011 
2012      l_tab_bal_name(5) := 'Teachers OSLA Claims';
2013 
2014   end if; -- end if of balance name specified check ...
2015   -- end of OSLA section
2016 
2017   -- Get Teachers GTC Payments balance name from the UDT
2018 
2019   l_tab_bal_name(6) := get_udt_value (p_column_name       => 'Attribute Location Qualifier 1'
2020                                      ,p_row_name          => 'GTC Payments Balance'
2021                                      ,p_business_group_id => p_business_group_id
2022                                      );
2023   -- Check whether a balance name is specified otherwise use
2024   -- the seeded generic balance for GTC Payments
2025 
2026   if l_tab_bal_name(6) is null then
2027 
2028      l_tab_bal_name(6) := 'GTC Payments';
2029 
2030   end if; -- end if of balance name specified check ...
2031 
2032 
2033   FOR i IN 1..l_tab_bal_name.count LOOP
2034     --
2035     debug(l_proc_name, 30);
2036     debug('Balance Name: '||l_tab_bal_name(i));
2037     --
2038     -- Get balance type id for this balance name
2039     l_bal_type_id := get_pay_bal_id (p_balance_name      => l_tab_bal_name(i)
2040                                     ,p_business_group_id => p_business_group_id
2041                                     ,p_legislation_code  => l_legislation_code); --OUT
2042 
2043     debug('Balance ID: '||to_char(l_bal_type_id),40);
2044 
2045     -- store bal_type_id in global
2046     IF i = 1 THEN
2047        g_abs_bal_type_id(p_business_group_id) := l_bal_type_id;
2048     ELSIF i=2 THEN
2049       g_sal_bal_type_id(p_business_group_id) := l_bal_type_id;
2050     ELSIF i=3 THEN
2051       g_osla_bal_type_id(p_business_group_id) := l_bal_type_id; --4336613 : OSLA_3A: new global
2052     ELSIF i=4 THEN
2053       g_cl_bal_type_id(p_business_group_id) := l_bal_type_id;
2054     ELSIF i=5 THEN
2055       g_osla_cl_bal_type_id(p_business_group_id) := l_bal_type_id;
2056     ELSE
2057       g_gtc_bal_type_id(p_business_group_id) := l_bal_type_id;
2058     END IF;
2059 
2060 
2061     -- 4336613 : OSLA_3A : moved from inside calc_part_time_sal to here
2062     -- call to function to store defined balance id for i=2 and 3,
2063     -- i.e. Teachers Superannuable Sal and OSLA Payments balance
2064     IF i=2 OR i=3 THEN
2065 
2066       IF l_legislation_code IS NOT NULL THEN -- for case when user balance name is
2067       -- same as the seeded balance, leg_code will be GB for seeded balance
2068       -- and NULL for user balance
2069         l_seed_flag := 'Y'; -- not NULL,implies is a seeded balance name in UDT
2070       ELSE
2071         l_seed_flag := 'N'; -- user defined balance in the UDT
2072       END IF; -- end if of balance name specified check ...
2073 
2074       -- store balance type id
2075       get_defined_balance_id(p_assignment_id     => p_assignment_id
2076                             ,p_bal_type_id       => l_bal_type_id
2077                             ,p_business_group_id => p_business_group_id
2078                             ,p_tab_bal_name      => l_tab_bal_name(i)
2079                             ,p_seed_flag         => l_seed_flag
2080                             );
2081 
2082     END IF;
2083 
2084 
2085   END LOOP;
2086 
2087   --
2088   debug(l_proc_name, 60);
2089   --
2090   -- Get element type id's feeding absence balance
2091   --
2092   get_pay_ele_ids_from_bal (p_assignment_id        => p_assignment_id
2093                            ,p_balance_type_id      => g_abs_bal_type_id(p_business_group_id)
2094                            ,p_effective_date       => p_effective_date
2095                            ,p_error_text           => 'BEN_93025_EXT_TP1_BAL_NOFEEDS'
2096                            ,p_error_number         => 93025
2097                            ,p_business_group_id    => p_business_group_id
2098                            ,p_tab_ele_ids          => g_tab_abs_ele_ids
2099                            ,p_token                => 'Days Excluded Balance'
2100                            );
2101 
2102   debug(l_proc_name, 70);
2103   --
2104   -- Get element type id's feeding salary balance
2105   --
2106   -- Bug 3015917 : Added this as we need to cache PET Ids for Sal Balance
2107   get_pay_ele_ids_from_bal (p_assignment_id        => p_assignment_id
2108                            ,p_balance_type_id      => g_sal_bal_type_id(p_business_group_id)
2109                            ,p_effective_date       => p_effective_date
2110                            ,p_error_text           => 'BEN_93025_EXT_TP1_BAL_NOFEEDS'
2111                            ,p_error_number         => 93025
2112                            ,p_business_group_id    => p_business_group_id
2113                            ,p_tab_ele_ids          => g_tab_sal_ele_ids -- OUT
2114                            ,p_token                => 'Superannuable Salary Balance'
2115                            );
2116 
2117   -- Bug 6689648
2118   -- Superannuable Claims Balance is applicable only when Date Worked mode is Used
2119   if g_date_work_mode = 'Y' then
2120      get_pay_ele_ids_from_bal (p_assignment_id        => p_assignment_id
2121                            ,p_balance_type_id      => g_cl_bal_type_id(p_business_group_id)
2122                            ,p_effective_date       => p_effective_date
2123                            ,p_error_text           => 'BEN_93025_EXT_TP1_BAL_NOFEEDS'
2124                            ,p_error_number         => 93025
2125                            ,p_business_group_id    => p_business_group_id
2126                            ,p_tab_ele_ids          => g_tab_cl_ele_ids -- OUT
2127                            ,p_token                => 'Superannuable Claims Balance'
2128                            );
2129   end if ;
2130 
2131   -- 4336613 : OSLA_3A : fetching values in g_tab_osla_ele_ids
2132   --
2133   -- Get element type id's feeding OSLA Payments balance
2134   --
2135   get_pay_ele_ids_from_bal (p_assignment_id        => p_assignment_id
2136                            ,p_balance_type_id      => g_osla_bal_type_id(p_business_group_id)
2137                            ,p_effective_date       => p_effective_date
2138                            ,p_error_text           => NULL -- 'BEN_93025_EXT_TP1_BAL_NOFEEDS'
2139                            ,p_error_number         => NULL --  93025
2140                            ,p_business_group_id    => p_business_group_id
2141                            ,p_tab_ele_ids          => g_tab_osla_ele_ids
2142                            );
2143 
2144   -- Bug 6689648
2145   -- Teachers OSLA Claims Balance is applicable only when Date Worked mode is Used
2146   if g_date_work_mode = 'Y' then
2147      get_pay_ele_ids_from_bal (p_assignment_id        => p_assignment_id
2148 --                           ,p_balance_type_id      => g_osla_bal_type_id(p_business_group_id)  --11740690 Bug fix, changed to claims bal type
2149                            ,p_balance_type_id      => g_osla_cl_bal_type_id(p_business_group_id)
2150                            ,p_effective_date       => p_effective_date
2151                            ,p_error_text           => NULL -- 'BEN_93025_EXT_TP1_BAL_NOFEEDS'
2152                            ,p_error_number         => NULL --  93025
2153                            ,p_business_group_id    => p_business_group_id
2154                            ,p_tab_ele_ids          => g_tab_osla_cl_ele_ids
2155                            );
2156   end if ;
2157 
2158   get_pay_ele_ids_from_bal (p_assignment_id        => p_assignment_id
2159                            ,p_balance_type_id      => g_gtc_bal_type_id(p_business_group_id)
2160                            ,p_effective_date       => p_effective_date
2161                            ,p_error_text           => NULL -- 'BEN_93025_EXT_TP1_BAL_NOFEEDS'
2162                            ,p_error_number         => NULL --  93025
2163                            ,p_business_group_id    => p_business_group_id
2164                            ,p_tab_ele_ids          => g_tab_gtc_ele_ids
2165                            );
2166 
2167   -- Raise a warning if no element is feeding the
2168   -- OSLA balance.
2169   IF g_tab_osla_ele_ids.COUNT = 0 THEN
2170     debug(l_proc_name,80 )   ;
2171     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
2172                  (p_assignment_id => p_assignment_id
2173                  ,p_error_text    => 'BEN_94240_T1_OSLA_ELE_NOTEXIST'
2174                  ,p_error_number  => 94240
2175                  );
2176      debug('l_error: '|| to_char(l_error), 90 ) ;
2177   END IF;
2178 
2179   -- Bug 6689648
2180   -- Teachers OSLA Claims Balance is applicable only when Date Worked mode is Used
2181   if g_date_work_mode = 'Y' then
2182      IF g_tab_osla_cl_ele_ids.COUNT = 0 THEN
2183         debug(l_proc_name,80 )   ;
2184         l_error := pqp_gb_tp_extract_functions.raise_extract_warning
2185                  (p_assignment_id => p_assignment_id
2186                  ,p_error_text    => 'BEN_94240_T1_OSLA_ELE_NOTEXIST'
2187                  ,p_error_number  => 94240
2188                  );
2189        debug('l_error: '|| to_char(l_error), 90 ) ;
2190      END IF;
2191   end if ;
2192 
2193   debug_exit (l_proc_name);
2194 EXCEPTION
2195   WHEN OTHERS THEN
2196     debug_exit(' Others in '||l_proc_name);
2197     RAISE;
2198 end fetch_eles_from_bals;
2199 --
2200 
2201 -- ----------------------------------------------------------------------------
2202 -- |--------------------------< fetch_eles_for_t1_bals >----------------------|
2203 -- ----------------------------------------------------------------------------
2204 procedure fetch_eles_for_t1_bals (p_assignment_id  in     number
2205                                  ,p_effective_date in     date)
2206 is
2207 --
2208   l_proc_name      varchar2(60) := g_proc_name || 'fetch_eles_for_t1_bals';
2209 
2210   l_tab_bal_name   t_varchar;
2211   l_bal_type_id    pay_balance_types.balance_type_id%type;
2212   i                number;
2213 
2214 --
2215 begin
2216   --
2217   -- hr_utility.set_location ('Entering: '||l_proc_name, 10);
2218   --
2219   debug_enter (l_proc_name);
2220 
2221   g_abs_bal_type_id.DELETE;
2222   g_sal_bal_type_id.DELETE;
2223   g_cl_bal_type_id.DELETE;
2224   g_tab_abs_ele_ids.DELETE;
2225   g_tab_sal_ele_ids.DELETE;
2226 
2227   -- 4336613 : OSLA_3A : new globals for OSLA
2228   g_osla_bal_type_id.DELETE;
2229   g_osla_cl_bal_type_id.DELETE;
2230   g_tab_osla_ele_ids.DELETE;
2231   g_tab_osla_cl_ele_ids.DELETE;   -- 11740690 Bug fix
2232 
2233   -- Fetch the elements from balances for master business group first
2234 
2235   debug(l_proc_name, 10);
2236 
2237   fetch_eles_from_bals (p_assignment_id     => p_assignment_id
2238                        ,p_effective_date    => p_effective_date
2239                        ,p_business_group_id => g_business_group_id
2240                        );
2241 
2242   -- Check whether the collection g_LEA_business_groups has more than one count
2243   IF g_lea_business_groups.COUNT > 0 THEN
2244 
2245     i := g_lea_business_groups.FIRST;
2246 
2247     WHILE i IS NOT NULL
2248     LOOP
2249 
2250       debug(l_proc_name, 20);
2251 
2252       fetch_eles_from_bals (p_assignment_id     => p_assignment_id
2253                            ,p_effective_date    => p_effective_date
2254                            ,p_business_group_id => g_lea_business_groups(i).business_group_id
2255                            );
2256       i := g_lea_business_groups.NEXT(i);
2257 
2258     END LOOP;
2259 
2260   END IF; -- End if of multiple business groups exists check ...
2261 
2262   --
2263   -- hr_utility.set_location ('Leaving: '||l_proc_name, 30);
2264   --
2265   debug_exit (l_proc_name);
2266 
2267 EXCEPTION
2268   WHEN OTHERS THEN
2269     debug_exit(' Others in '||l_proc_name);
2270     RAISE;
2271 end fetch_eles_for_t1_bals;
2272 --
2273 -- ----------------------------------------------------------------------------
2274 -- |--------------------------< get_rate_type_from_udt >----------------------|
2275 -- ----------------------------------------------------------------------------
2276 procedure get_rate_type_from_udt (p_assignment_id in    number) is
2277 --
2278   l_proc_name      varchar2(60) := g_proc_name || 'get_rate_type_from_udt';
2279 
2280   cursor csr_rate_type_check (p_meaning varchar2) is
2281   select 'Y'
2282     from hr_lookups
2283   where meaning         = p_meaning
2284     and lookup_type     = 'PQP_RATE_TYPE'
2285     and enabled_flag    = 'Y'
2286     and g_effective_date between
2287           nvl(start_date_active, g_effective_date)
2288           and nvl(end_date_active, g_effective_date);
2289 
2290   l_tab_rate_type  t_varchar;
2291   l_tab_rate_name  t_varchar;
2292   l_exists         varchar2(1);
2293   l_error          number;
2294 --
2295 begin
2296   --
2297   -- hr_utility.set_location ('Entering: '||l_proc_name, 10);
2298   --
2299   debug_enter (l_proc_name);
2300 
2301   l_tab_rate_type(1) := 'Other Allowances';
2302   l_tab_rate_type(2) := 'Salary';
2303   l_tab_rate_type(3) := 'Safeguarded Salary';
2304   l_tab_rate_type(4) := 'LARP and SPAP Allowances';
2305 
2306   for i in 1..l_tab_rate_type.count loop
2307 
2308     --
2309     debug (l_proc_name, 20);
2310     --
2311     l_tab_rate_name(i) := get_udt_value (p_column_name => 'Attribute Location Qualifier 1'
2312                                         ,p_row_name    => l_tab_rate_type(i)
2313                                         );
2314 
2315     -- Check whether a rate type has been specified
2316     -- Bug fix 2786740
2317     -- Raise error only for "Salary" rate type
2318 
2319     if l_tab_rate_name(i) is null and l_tab_rate_type(i) = 'Salary' then
2320 
2321       l_error := pqp_gb_tp_extract_functions.raise_extract_error
2322                       (p_business_group_id => g_business_group_id
2323                       ,p_assignment_id     => p_assignment_id
2324                       ,p_error_text        => 'BEN_93023_EXT_TP1_NO_RATE_TYPE'
2325                       ,p_error_number      => 93023
2326                       );
2327 
2328     end if; -- end if of rate type specified check ...
2329 
2330     -- Modified for bug fix 2350695
2331     -- Check whether this rate type exists in the lookup type 'PQP_RATE_TYPE'
2332 
2333     IF l_tab_rate_name(i) IS NOT NULL THEN
2334 
2335        debug (l_proc_name, 30);
2336        open csr_rate_type_check (l_tab_rate_name(i));
2337        fetch csr_rate_type_check into l_exists;
2338 
2339        if csr_rate_type_check%notfound then
2340 
2341           close csr_rate_type_check;
2342           l_error := pqp_gb_tp_extract_functions.raise_extract_error
2343                          (p_business_group_id => g_business_group_id
2344                          ,p_assignment_id     => p_assignment_id
2345                          ,p_error_text        => 'BEN_93046_EXT_TP1_INV_RATE_TYP'
2346                          ,p_error_number      => 93046
2347                         );
2348 
2349        end if; -- end if of rate type exists in lookup check ...
2350        close csr_rate_type_check;
2351 
2352     END IF; -- End if of rate name not null check ...
2353 
2354   end loop;
2355 
2356   g_oth_rate_type := l_tab_rate_name(1);
2357   g_sal_rate_type := l_tab_rate_name(2);
2358   g_sf_rate_type  := l_tab_rate_name(3);
2359   g_lon_rate_type := l_tab_rate_name(4);
2360 
2361   --
2362   -- hr_utility.set_location ('Leaving: '||l_proc_name, 30);
2363   --
2364   debug_exit (l_proc_name);
2365 EXCEPTION
2366   WHEN OTHERS THEN
2367     debug_exit(' Others in '||l_proc_name);
2368     RAISE;
2369 end get_rate_type_from_udt;
2370 
2371 -- Commenting the following function
2372 -- as this is not required now.
2373 -- the replacement function is chk_effective_asg
2374 /*--
2375 -- chk_eff_primary_asg
2376 -- Procedure returns 'Y' if the primary is a valid TCHR assignment
2377 -- on p_effective_date.
2378 --
2379 FUNCTION chk_eff_primary_asg
2380     (p_assignment_id            IN  NUMBER
2381     ,p_effective_date           IN  DATE
2382     ) RETURN VARCHAR2
2383 IS
2384 
2385   l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
2386   l_retval              VARCHAR2(1) := 'Y';
2387   l_effective_date      DATE;
2388 
2389 
2390 
2391   l_proc_name           varchar2(60) := g_proc_name || 'chk_eff_primary_asg';
2392 
2393 BEGIN -- chk_eff_primary_asg
2394 
2395   debug_enter(l_proc_name);
2396   debug('p_assignment_id :'||to_char(p_assignment_id), 10);
2397   debug('p_effective_date :'||to_char(p_effective_date), 20);
2398 
2399 
2400   -- Bugfix 3803760:FTSUPPLY : Added the overrid ft asg logic
2401   IF g_override_ft_asg_id IS NULL THEN
2402 
2403     IF (g_ext_asg_details.EXISTS(p_assignment_id)
2404         AND
2405         g_ext_asg_details(p_assignment_id).report_asg = 'Y' -- is to be reported
2406         AND
2407         (p_effective_date
2408             BETWEEN nvl(g_ext_asg_details(p_assignment_id).start_date, p_effective_date )
2409                     AND
2410                     nvl(g_ext_asg_details(p_assignment_id).leaver_date, g_effective_run_date)
2411          OR
2412          (g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL
2413           AND
2414           p_effective_date >= g_ext_asg_details(p_assignment_id).restarter_date
2415          )
2416         )
2417        ) THEN
2418 
2419       l_retval := 'Y';
2420       debug(l_proc_name, 30);
2421 
2422     ELSE
2423 
2424       debug(l_proc_name, 50);
2425       l_retval := 'N';
2426 
2427     END IF;
2428 
2429   ELSIF g_override_ft_asg_id = p_assignment_id THEN
2430 
2431     l_retval := 'Y';
2432     debug(l_proc_name, 60);
2433 
2434   ELSE
2435 
2436     debug(l_proc_name, 70);
2437     l_retval := 'N';
2438 
2439   END IF;
2440 
2441   debug('l_retval ' ||l_retval) ;
2442   debug_exit(l_proc_name);
2443 
2444   RETURN l_retval;
2445 
2446 EXCEPTION
2447   WHEN OTHERS THEN
2448     debug_exit(' Others in '||l_proc_name);
2449     RAISE;
2450 END; -- chk_eff_primary_asg
2451 */
2452 
2453 -- This procedure will find all BGs which have the same
2454 -- LEA number and have been enabled for cross BG reporting
2455 -- and store them in global collection
2456 PROCEDURE store_cross_bg_details
2457 IS
2458 
2459   l_BG_dets             csr_all_business_groups%ROWTYPE;
2460   l_master_bg_dets      csr_multiproc_data%ROWTYPE;
2461 
2462   l_proc_name      varchar2(60) := g_proc_name || 'store_cross_bg_details';
2463 
2464 BEGIN  -- store_cross_bg_details
2465 
2466   debug_enter(l_proc_name);
2467 
2468   IF pqp_gb_tp_pension_extracts.g_parent_request_id <> -1 THEN
2469 
2470     -- We are running the TPEP, get the master bg id
2471     OPEN csr_multiproc_data(p_record_type => 'M');
2472     FETCH csr_multiproc_data INTO l_master_bg_dets;
2473 
2474     IF csr_multiproc_data%FOUND
2475        AND
2476        l_master_bg_dets.business_group_id IS NOT NULL  THEN
2477       g_master_bg_id := l_master_bg_dets.business_group_id;
2478     ELSE
2479      -- We might need an ERROR here
2480      -- If we do raise an error, plaese also
2481      -- do a CLOSE cursor here
2482      debug('This situation should never arise', 10);
2483     END IF;
2484 
2485     CLOSE csr_multiproc_data;
2486 
2487   ELSE -- We are running the Extract Process
2488 
2489     -- The Type 1 extract process has been submitted
2490     -- directly for an LEA report instead of using the new
2491     -- master process for TPA reports which sets the
2492     -- master BG in the multiproc data table.
2493     -- We can safely make this assumption and set the current
2494     -- BG as master BG.
2495     g_master_bg_id := g_business_group_id;
2496 
2497     -- Should we still leave cross BG reporting enabled
2498     -- Answer is NO
2499     g_crossbg_enabled := 'N';
2500 
2501     -- Also change the flag in Type 4 pkg
2502     pqp_gb_tp_pension_extracts.g_crossbg_enabled := 'N';
2503 
2504   END IF;
2505 
2506   debug('MasterBG :'||to_char(g_master_bg_id)||
2507         ' CurrentBG :'||to_char(g_business_group_id)
2508        , 20
2509        );
2510 
2511   g_lea_business_groups.DELETE;
2512 
2513   IF g_crossbg_enabled = 'Y' THEN
2514 
2515     -- Loop thru all the LEA BGs enabled for Cross BG reporting
2516     FOR l_BG_dets IN csr_all_business_groups(g_lea_number)
2517     LOOP -- 1
2518 
2519       g_lea_business_groups(l_BG_dets.business_group_id) := l_BG_dets;
2520 
2521       debug(l_proc_name, 30);
2522 
2523       -- Store all criteria establishments from this BG
2524       FOR l_estb_details IN pqp_gb_tp_pension_extracts.csr_estb_details
2525                               (p_business_group_id => l_BG_dets.business_group_id
2526                               ,p_lea_estb_yn => 'Y')
2527       LOOP -- 2
2528 
2529         pqp_gb_tp_pension_extracts.g_criteria_estbs(l_estb_details.location_id):= l_estb_details;
2530 
2531         debug('Establishment Details...', 40);
2532         debug(pqp_gb_tp_pension_extracts.g_criteria_estbs(l_estb_details.location_id).location_id);
2533         debug(pqp_gb_tp_pension_extracts.g_criteria_estbs(l_estb_details.location_id).lea_estb_yn);
2534         debug(pqp_gb_tp_pension_extracts.g_criteria_estbs(l_estb_details.location_id).estb_number);
2535         debug(pqp_gb_tp_pension_extracts.g_criteria_estbs(l_estb_details.location_id).estb_name);
2536         debug(pqp_gb_tp_pension_extracts.g_criteria_estbs(l_estb_details.location_id).estb_type);
2537         debug('...Establishment Details', 50);
2538 
2539       END LOOP; -- 2
2540       --
2541     END LOOP; -- 1
2542 
2543   END IF; -- g_crossbg_enabled = 'Y' THEN
2544 
2545   debug_exit (l_proc_name);
2546 
2547 EXCEPTION
2548   WHEN OTHERS THEN
2549     debug_exit(' Others in '||l_proc_name);
2550     RAISE;
2551 END; -- store_cross_bg_details
2552 
2553 --
2554 -- reset_proc_status
2555 --
2556 PROCEDURE reset_proc_status IS
2557 
2558   PRAGMA AUTONOMOUS_TRANSACTION;
2559 
2560   l_req_dets    csr_request_dets%ROWTYPE;
2561 
2562   l_proc_name VARCHAR2(61):= 'reset_proc_status';
2563 
2564 BEGIN
2565 
2566   debug_enter(l_proc_name);
2567 
2568   -- Reset the processing status to U in the multiproc data table
2569   -- if the Extract Process is running on its own
2570 
2571   -- Bugfix 3671727:ENH2
2572   --   Commenting out the cursor call coz the parent
2573   --   request id is not being set in a global in
2574   --   Type 4 code coz its needed to get the master
2575   --   rec from pqp_ext_cross_person_records
2576   /* OPEN csr_request_dets;
2577      FETCH csr_request_dets INTO l_req_dets;
2578      CLOSE csr_request_dets;
2579   */
2580 
2581   IF pqp_gb_tp_pension_extracts.g_parent_request_id = -1 THEN
2582 
2583     debug(l_proc_name, 20);
2584     UPDATE pqp_ext_cross_person_records
2585        SET processing_status = 'U'
2586           ,request_id            = fnd_global.conc_request_id
2587           ,last_updated_by       = fnd_global.user_id
2588           ,last_update_date      = SYSDATE
2589           ,last_update_login     = fnd_global.login_id
2590           ,object_version_number = (object_version_number + 1)
2591      WHERE record_type = 'X'
2592        AND ext_dfn_id  = ben_ext_thread.g_ext_dfn_id    --ENH2
2593        AND lea_number  = g_lea_number;                  --ENH1
2594 
2595   END IF;
2596 
2597   COMMIT;
2598 
2599   debug_exit(l_proc_name);
2600 EXCEPTION
2601   WHEN OTHERS THEN
2602     debug_exit(' Others in '||l_proc_name);
2603     RAISE;
2604 END reset_proc_status;
2605 
2606 --
2607 -- set_t1_extract_globals
2608 --
2609 PROCEDURE set_t1_extract_globals
2610   (p_business_group_id        IN      NUMBER
2611   ,p_effective_date           IN      DATE
2612   ,p_assignment_id            IN      NUMBER
2613   )
2614 IS
2615 
2616   l_proc_name VARCHAR2(61):= 'set_t1_extract_globals';
2617 
2618   -- 4336613 : PROSWITCH_3A :
2619   -- temporary variables to store value fetched from UDT
2620   l_calc_sal_new VARCHAR2(100);
2621   l_calendar_avg VARCHAR2(100);
2622   l_date_work_mode VARCHAR2(100);   -- rahul supply
2623 
2624   l_error        NUMBER;
2625 
2626 
2627 BEGIN
2628   debug_enter(l_proc_name);
2629 
2630   -- Set the globals in this package
2631   g_business_group_id := p_business_group_id;
2632   debug(p_effective_date);
2633   g_effective_date    := p_effective_date;
2634 
2635   -- Call the Type 4 function to set globals in Type 4 package
2636   pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
2637   pqp_gb_tp_pension_extracts.set_extract_globals
2638         (p_business_group_id
2639         ,p_effective_date
2640         ,p_assignment_id
2641         );
2642   pqp_gb_tp_pension_extracts.g_nested_level := 0;
2643 
2644   -- Set the globals in this package before exiting this function
2645   -- ********* Variables ***************
2646   g_extract_type                := pqp_gb_tp_pension_extracts.g_extract_type;
2647   g_extract_udt_name            := pqp_gb_tp_pension_extracts.g_extract_udt_name;
2648   g_criteria_location_code      := pqp_gb_tp_pension_extracts.g_criteria_location_code;
2649   g_lea_number                  := pqp_gb_tp_pension_extracts.g_lea_number;
2650   g_crossbg_enabled             := pqp_gb_tp_pension_extracts.g_crossbg_enabled;
2651   g_estb_number                 := pqp_gb_tp_pension_extracts.g_estb_number;
2652   g_originators_title           := pqp_gb_tp_pension_extracts.g_originators_title;
2653   g_header_system_element       := pqp_gb_tp_pension_extracts.g_header_system_element;
2654 
2655   /************ Collections ***********
2656   Sh we duplicate or just use the one in Type 4??
2657   Current thought is reuse from Type 4
2658 
2659   g_criteria_estbs
2660   */
2661   --ENH1:The store_cross_bg details is being called by set_extract_globals
2662   --from type4.
2663 /*
2664   -- Bugfix 3073562:GAP1:GAP2
2665   -- If its the LEA run
2666   -- AND current BG is enabled for cross BG reporting
2667   IF g_estb_number = '0000'
2668      AND
2669      g_crossbg_enabled = 'Y'
2670   THEN
2671     -- Store all BGs with same LEA Number and
2672     -- enabled for cross BG reporting
2673     store_cross_bg_details;
2674   ELSE -- Non-LEA Run
2675     g_master_bg_id := g_business_group_id;
2676   END IF;
2677 */
2678       g_tab_lon_aln_eles.DELETE;
2679       g_tab_spl_aln_eles.DELETE;
2680 
2681       g_spl_all_grd_src := 'N';
2682       g_lon_all_grd_src := 'N';
2683 
2684   -- Get UDT Data
2685   get_udt_data (p_udt_name       => 'PQP_GB_TP_TYPE1_EXTRACT_DEFINITIONS'
2686                ,p_effective_date => p_effective_date
2687                );
2688 
2689   -- Get Element information from UDT
2690   --get_elements_frm_udt (p_assignment_id => p_assignment_id);
2691 
2692   -- Get Rate Type Name from UDT
2693   get_rate_type_from_udt (p_assignment_id => p_assignment_id);
2694 
2695 -- changed for 5743209
2696    fetch_allow_eles_frm_udt (p_assignment_id  => p_assignment_id
2697                                ,p_effective_date => p_effective_date
2698                                );
2699   --CALC_PT_SAL_OPTIONS : BUG: 4135481
2700   -- Two new rows are now seeded in the UDT for the role of switches
2701   -- 1. "Part Time Salary Paid - Enable Date Earned Mode"
2702   -- 2. "Part Time Salary Paid - Enable Calendar Day Proration"
2703   -- First switch is for enabling / disabling the new logic for calculating part
2704   -- time salary (based on date earned) or revert back to previous logic (date paid).
2705   -- The second switch is for enabling / disabling calendar averaging, in case NO
2706   -- matching proration events are found.
2707 
2708   l_calc_sal_new := Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
2709                                   ,p_row_name    => 'Part Time Salary Paid - Enable Date Earned Mode'
2710                                   );
2711 
2712   debug('l_calc_sal_new: ' || l_calc_sal_new,10);
2713 
2714   l_calendar_avg := Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'
2715                                   ,p_row_name    => 'Part Time Salary Paid - Enable Calendar Day Proration'
2716                                   );
2717 
2718   debug('l_calendar_avg: ' || l_calendar_avg,20);
2719 
2720 
2721   --CALC_PT_SAL_OPTIONS :
2722   -- One new row is now seeded in the UDT for the role of switch
2723   -- 3. "Date Worked Payment Mode"
2724   -- The third switch is for enabling / disabling the new logic for calculating part time salary
2725   -- only for supply teachers (based on date worked) or revert back to previous logic (date earned or date paid).
2726   l_date_work_mode := Get_Udt_Value (p_column_name => 'Attribute Location Qualifier 1'                     -- rahul supply
2727                                     ,p_row_name    => 'Date Worked Payment Mode'
2728                                   );
2729 
2730   debug('l_date_work_mode: ' || l_date_work_mode,21);                               -- rahul supply
2731 
2732 
2733 
2734   -- The following globals will be used to provide additional options for part
2735   -- time salary computation methods in calc_part_time_sal function
2736 
2737   -- If Switch1 is not set and Switch2 is not set, then Switch1=Switch2 = 'Y'.
2738   -- If Switch1 is set and Switch2 is not set, then Switch2 = Switch1.
2739   -- If Switch1 is not set and Switch2 is set, then Switch1 = 'Y' and
2740   -- Switch2 is whatever it is set to.
2741 
2742   -- switch 1
2743   l_calc_sal_new  := nvl(l_calc_sal_new, 'Y'); -- use old/new method
2744 
2745 
2746   -- 4336613 : PROSWITCH_3A :
2747   -- raise an error if the value of g_calc_sal_new switch in UDT is not
2748   -- among 'Y', 'YES', 'N', or 'NO' (i.e. is an invalid value)
2749 
2750   IF( UPPER(l_calc_sal_new) NOT IN ('Y', 'YES', 'N', 'NO')
2751     ) THEN
2752 
2753     debug('--- Raise error : invalid switch values in UDT ---',25);
2754 
2755     -- new error message for invalid values in UDT
2756     l_error := pqp_gb_tp_extract_functions.raise_extract_error
2757                       (p_business_group_id => g_business_group_id
2758                       ,p_assignment_id     => p_assignment_id
2759                       ,p_error_text        => 'BEN_94231_TP1_INVALID_UDT_VAL'
2760                       ,p_error_number      => 94231
2761                       ,p_token1            => 'Part Time Salary Paid - Enable Date Earned Mode'
2762                       );
2763 
2764   ELSIF((UPPER(l_calc_sal_new) = 'Y')
2765          OR
2766         (UPPER(l_calc_sal_new) = 'YES')
2767        )THEN
2768        l_calc_sal_new := 'Y';
2769   ELSE
2770        l_calc_sal_new := 'N';
2771 
2772   END IF;
2773 
2774   -- setting the validated value to global
2775   g_calc_sal_new := l_calc_sal_new;
2776 
2777 
2778 
2779   -- setting globals after checking validity of values of g_calc_sal_new
2780   g_proration     := g_calc_sal_new; -- set this to Y if new method is to be used
2781 
2782   -- switch 2
2783   l_calendar_avg  := nvl(l_calendar_avg, g_proration) ;
2784 
2785 
2786   -- 4336613 : PROSWITCH_3A :
2787   -- raise an error if the value of g_calendar_avg switch in UDT is not
2788   -- among 'Y', 'YES', 'N', or 'NO' (i.e. is an invalid value)
2789 
2790   IF( UPPER(l_calendar_avg) NOT IN ('Y', 'YES', 'N', 'NO')
2791     ) THEN
2792 
2793     debug('--- Raise error : invalid switch values in UDT ---',25);
2794 
2795     -- new error message for invalid values in UDT
2796     l_error := pqp_gb_tp_extract_functions.raise_extract_error
2797                       (p_business_group_id => g_business_group_id
2798                       ,p_assignment_id     => p_assignment_id
2799                       ,p_error_text        => 'BEN_94231_TP1_INVALID_UDT_VAL'
2800                       ,p_error_number      => 94231
2801                       ,p_token1            => 'Part Time Salary Paid - Enable Calendar Day Proration'
2802                       );
2803 
2804   ELSIF((UPPER(l_calendar_avg) = 'Y')
2805          OR
2806         (UPPER(l_calendar_avg) = 'YES')
2807        )THEN
2808        l_calendar_avg := 'Y';
2809   ELSE
2810        l_calendar_avg := 'N';
2811 
2812   END IF;
2813 
2814   -- setting validated value to global
2815   g_calendar_avg := l_calendar_avg;
2816 
2817   -- Switch 3
2818 
2819   l_date_work_mode := nvl(l_date_work_mode,'N');                      -- rahul supply
2820 
2821   IF( UPPER(l_date_work_mode) NOT IN ('Y', 'YES', 'N', 'NO')          -- rahul supply
2822     ) THEN
2823 
2824     debug('--- Raise error : invalid switch values in UDT ---',25);
2825 
2826     -- new error message for invalid values in UDT
2827     l_error := pqp_gb_tp_extract_functions.raise_extract_error
2828                       (p_business_group_id => g_business_group_id
2829                       ,p_assignment_id     => p_assignment_id
2830                       ,p_error_text        => 'BEN_94231_TP1_INVALID_UDT_VAL'
2831                       ,p_error_number      => 94231
2832                       ,p_token1            => 'Date Worked Payment Mode'
2833                       );
2834 
2835   ELSIF((UPPER(l_date_work_mode) = 'Y')
2836          OR
2837         (UPPER(l_date_work_mode) = 'YES')
2838        )THEN
2839        l_date_work_mode := 'Y';
2840   ELSE
2841        l_date_work_mode := 'N';
2842 
2843   END IF;
2844 
2845   -- Setting the validated value to global
2846   g_date_work_mode := l_date_work_mode;                             -- rahul supply
2847 
2848   debug('g_calc_sal_new: ' || g_calc_sal_new,30);
2849   debug('g_proration: ' || g_proration,40);
2850   debug('g_calendar_avg: ' || g_calendar_avg,50);
2851   debug('g_date_work_mode: ' || g_date_work_mode,55);
2852 
2853 
2854 
2855   -- Get the record id for the Type 1 Detail record
2856   OPEN csr_ext_rcd_id(p_hide_flag       => 'N'
2857                      ,p_rcd_type_cd     => 'D'
2858                      );
2859   FETCH csr_ext_rcd_id INTO g_ext_dtl_rcd_id;
2860   -- Do we need to raise an error if there are 2 diplayed detail records??
2861   -- If yes, then Fetch ... , check .. and raise error
2862   -- Alternatively, modify the cursor to return the required id by querying on name.
2863   CLOSE csr_ext_rcd_id;
2864 
2865   --ENH1:The reset_proc_status details is being called by set_extract_globals
2866   --from type4.
2867   -- Reset the processing status in multiproc data table to U
2868   --  reset_proc_status;
2869 
2870   debug_exit(l_proc_name);
2871 EXCEPTION
2872   WHEN OTHERS THEN
2873     debug_exit(' Others in '||l_proc_name);
2874     RAISE;
2875 END set_t1_extract_globals;
2876 
2877 --
2878 -- set_periodic_run_dates
2879 --
2880 PROCEDURE set_periodic_run_dates
2881   IS
2882 
2883   l_proc_name VARCHAR2(61):= 'set_periodic_run_dates';
2884 
2885 BEGIN
2886 
2887   debug_enter(l_proc_name);
2888 
2889   -- Call the Type 4 function to set run dates in Type 4 package
2890   pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
2891   pqp_gb_tp_pension_extracts.set_run_effective_dates;
2892   pqp_gb_tp_pension_extracts.g_nested_level := 0;
2893 
2894   -- Set the globals in this package before exiting this function
2895   -- ********* Variables ****************
2896   g_last_effective_date     := pqp_gb_tp_pension_extracts.g_last_effective_date;
2897   g_next_effective_date     := pqp_gb_tp_pension_extracts.g_next_effective_date;
2898   g_effective_run_date      := pqp_gb_tp_pension_extracts.g_effective_run_date;
2899 
2900   -- For the sake of periodic report only
2901   --  pension year end date is being set as the run end date.
2902   g_pension_year_end_date   := g_effective_run_date;
2903 
2904   -- Changed by Raju T. on 08/05/2002 as a development bugfix
2905   -- Commented out as this is now being created differently for Type 1
2906   --  g_header_system_element   := pqp_gb_tp_pension_extracts.g_header_system_element;
2907 
2908   -- Set the start date of the pension year, i.e. 01-04-YYYY
2909   SELECT TO_DATE('01-04-'||
2910                         DECODE
2911                           (SIGN(TO_NUMBER(TO_CHAR(g_effective_run_date,'MM')) - 04)
2912                           ,-1,TO_CHAR(ADD_MONTHS(g_effective_run_date,-12),'YYYY')
2913                           ,TO_CHAR(g_effective_run_date,'YYYY'))
2914                       ,'DD-MM-YYYY')
2915                INTO g_pension_year_start_date
2916              FROM DUAL;
2917 
2918   -- Changed by Raju T. on 08/05/2002 as a development bugfix
2919   -- The last eff date and header sys element is now being created
2920   -- differently for Type 1 and being overwritten in the Type 4 Pkg
2921   -- Changes START here
2922   g_last_effective_date := GREATEST(g_last_effective_date
2923                                    ,g_pension_year_start_date
2924                                    );
2925 
2926   IF g_last_effective_date <> pqp_gb_tp_pension_extracts.g_last_effective_date THEN
2927 
2928     pqp_gb_tp_pension_extracts.g_last_effective_date := g_last_effective_date;
2929 
2930   END IF;
2931 
2932   g_header_system_element:=
2933           g_header_system_element||
2934           fnd_date.date_to_canonical(g_last_effective_date)||':'||
2935           fnd_date.date_to_canonical(g_effective_run_date) ||':'||
2936           fnd_date.date_to_canonical(g_next_effective_date)||':';
2937 
2938   -- Assign the newly created system element to the type4 global
2939   pqp_gb_tp_pension_extracts.g_header_system_element := g_header_system_element;
2940 
2941   debug('New Type 1 Header System Element :'||g_header_system_element);
2942   -- Changes END here
2943 
2944   debug_exit(l_proc_name);
2945 EXCEPTION
2946   WHEN OTHERS THEN
2947     debug_exit(' Others in '||l_proc_name);
2948     RAISE;
2949 END set_periodic_run_dates;
2950 
2951 --
2952 -- set_annual_run_dates
2953 --
2954 PROCEDURE set_annual_run_dates
2955   IS
2956 
2957   l_year        NUMBER;
2958   l_proc_name   VARCHAR2(61):= 'set_annual_run_dates';
2959 
2960 BEGIN
2961 
2962   debug_enter(l_proc_name);
2963 
2964   debug(TO_CHAR(g_effective_date,'DD-MON-YYYY'));
2965 
2966   debug('g_effective_date: '||
2967         fnd_date.date_to_canonical(g_effective_date));
2968 
2969   IF to_number(to_char(g_effective_date, 'MM'))
2970        BETWEEN 1 AND 3 THEN
2971 
2972      -- Pension year should end YY - 1
2973      l_year := to_number(to_char(g_effective_date, 'YYYY')) - 1;
2974 
2975   ELSE
2976 
2977     -- Pension year should end YY
2978     l_year := to_number(to_char(g_effective_date, 'YYYY'));
2979 
2980   END IF; -- End if of month check...
2981 
2982   g_pension_year_start_date := to_date('01/04/'||to_char(l_year), 'DD/MM/YYYY');
2983   g_pension_year_end_date   := to_date('31/03/'||to_char(l_year+1)||
2984                               '23:59:59', 'DD/MM/YYYY HH24:MI:SS');
2985 
2986 
2987   g_header_system_element:=
2988         g_header_system_element||
2989         fnd_date.date_to_canonical(g_pension_year_start_date)||':'||
2990         fnd_date.date_to_canonical(g_pension_year_end_date)||':'||
2991         fnd_date.date_to_canonical(g_effective_date)||':';
2992 
2993   -- Set the globals in this package
2994   g_last_effective_date     := g_pension_year_start_date;
2995   g_next_effective_date     := g_effective_date;
2996   g_effective_run_date      := g_pension_year_end_date;
2997 
2998   -- Now set the Type 4 globals
2999   pqp_gb_tp_pension_extracts.g_last_effective_date      := g_last_effective_date;
3000   pqp_gb_tp_pension_extracts.g_next_effective_date      := g_next_effective_date;
3001   pqp_gb_tp_pension_extracts.g_effective_run_date       := g_effective_run_date;
3002   pqp_gb_tp_pension_extracts.g_header_system_element    := g_header_system_element;
3003 
3004   debug('Header System Element :'||g_header_system_element);
3005 
3006   debug_exit(l_proc_name);
3007 EXCEPTION
3008   WHEN OTHERS THEN
3009     debug_exit(' Others in '||l_proc_name);
3010     RAISE;
3011 END set_annual_run_dates;
3012 
3013 --
3014 -- set_pay_proc_events_to_process
3015 --
3016 PROCEDURE set_pay_proc_events_to_process
3017     (p_assignment_id    IN      NUMBER
3018     ,p_status           IN      VARCHAR2
3019     ,p_start_date       IN      DATE
3020     ,p_end_date         IN      DATE
3021     )
3022   IS
3023 
3024   PRAGMA AUTONOMOUS_TRANSACTION;
3025 
3026   l_proc_name VARCHAR2(61):= 'set_pay_proc_events_to_process';
3027 
3028 BEGIN
3029   debug_enter(l_proc_name);
3030 
3031 
3032   debug('p_status: '||p_status);
3033   debug('p_start_date: '||fnd_date.date_to_canonical(p_start_date));
3034   debug('p_end_date: '||fnd_date.date_to_canonical(p_end_date));
3035   --
3036   -- Mark pay_process_events to process
3037   -- as determined by the date range. The maxmum allowed range
3038   -- is the pension year start date and a day before the current eff date
3039 
3040   UPDATE pay_process_events
3041      SET retroactive_status = p_status
3042         ,status             = p_status
3043    WHERE assignment_id = p_assignment_id
3044      AND change_type = 'REPORTS'
3045      AND effective_date -- allow all events effective as of and on pension year start date
3046           BETWEEN  GREATEST(NVL(p_start_date,g_pension_year_start_date)
3047                            ,g_pension_year_start_date)
3048               AND  LEAST(NVL(p_end_date,g_effective_run_date)
3049                         ,g_effective_run_date)
3050   ;                    -- allow all events upto end of day (eff_dt - 1)
3051 
3052   COMMIT;
3053   --
3054   debug(fnd_number.number_to_canonical(SQL%ROWCOUNT)||' PPE row(s) updated.');
3055   --
3056   debug_exit(l_proc_name);
3057   --
3058 EXCEPTION
3059   WHEN OTHERS THEN
3060     debug_exit(' Others in '||l_proc_name);
3061     RAISE;
3062 END set_pay_proc_events_to_process;
3063 
3064 --
3065 -- set_pay_proc_events_to_process
3066 -- Overloaded procedure, this one has an extra parameter p_element_entry_id
3067 --
3068 PROCEDURE set_pay_proc_events_to_process
3069             (p_assignment_id    IN      NUMBER
3070             ,p_element_entry_id IN      NUMBER
3071             ,p_status           IN      VARCHAR2
3072             ,p_start_date       IN      DATE
3073             ,p_end_date         IN      DATE
3074             )
3075   IS
3076 
3077   PRAGMA AUTONOMOUS_TRANSACTION;
3078 
3079   l_proc_name VARCHAR2(61):= 'set_pay_proc_events_to_process';
3080 
3081 BEGIN
3082   debug_enter(l_proc_name);
3083 
3084 
3085   debug('p_status: '||p_status);
3086   debug('p_start_date: '||fnd_date.date_to_canonical(p_start_date));
3087   debug('p_end_date: '||fnd_date.date_to_canonical(p_end_date));
3088 
3089   --
3090   -- Mark pay_process_events to process
3091   -- as determined by the date range. The maxmum allowed range
3092   -- is the pension year start date and a day before the current eff date
3093 
3094   -- First update for PAY_ELEMENT_ENTRIES_F
3095   UPDATE pay_process_events ppe
3096      SET ppe.retroactive_status = p_status
3097         ,ppe.status             = p_status
3098    WHERE ppe.assignment_id = p_assignment_id
3099      AND ppe.change_type = 'REPORTS'
3100      AND ppe.effective_date -- allow all events effective as of and on pension year start date
3101              BETWEEN  GREATEST(NVL(p_start_date,g_pension_year_start_date)
3102                               ,g_pension_year_start_date)
3103                  AND  LEAST(NVL(p_end_date,g_effective_run_date)
3104                       ,g_effective_run_date)
3105      AND ppe.surrogate_key = p_element_entry_id
3106      AND EXISTS (SELECT 1
3107                    FROM pay_dated_tables pdt
3108                        ,pay_event_updates peu
3109                   WHERE pdt.table_name = 'PAY_ELEMENT_ENTRIES_F'
3110                     AND peu.dated_table_id = pdt.dated_table_id
3111                     AND peu.change_type = ppe.change_type
3112                     AND peu.event_update_id = ppe.event_update_id
3113                 )
3114   ;
3115 
3116   debug(fnd_number.number_to_canonical(SQL%ROWCOUNT)||' PPE row(s) updated.');
3117 
3118   -- Now update for PAY_ELEMENT_ENTRY_VALUES_F
3119   UPDATE pay_process_events ppe
3120      SET ppe.retroactive_status = p_status
3121         ,ppe.status             = p_status
3122    WHERE ppe.assignment_id = p_assignment_id
3123      AND ppe.change_type = 'REPORTS'
3124      AND ppe.effective_date -- allow all events effective as of and on pension year start date
3125              BETWEEN  GREATEST(NVL(p_start_date,g_pension_year_start_date)
3126                               ,g_pension_year_start_date)
3127                  AND  LEAST(NVL(p_end_date,g_effective_run_date)
3128                     ,g_effective_run_date)
3129      AND EXISTS (SELECT 1
3130                    FROM pay_dated_tables pdt
3131                        ,pay_event_updates peu
3132                   WHERE pdt.table_name = 'PAY_ELEMENT_ENTRY_VALUES_F'
3133                     AND peu.dated_table_id = pdt.dated_table_id
3134                     AND peu.change_type = ppe.change_type
3135                     AND peu.event_update_id = ppe.event_update_id
3136                 )
3137      AND EXISTS (SELECT 1
3138                    FROM pay_element_entry_values_f peev
3139                   WHERE peev.element_entry_id = p_element_entry_id
3140                     AND peev.element_entry_value_id = ppe.surrogate_key
3141                 )
3142   ;
3143 
3144 
3145   COMMIT;
3146   --
3147   debug(fnd_number.number_to_canonical(SQL%ROWCOUNT)||' PPE row(s) updated.');
3148   --
3149   debug_exit(l_proc_name);
3150   --
3151 EXCEPTION
3152   WHEN OTHERS THEN
3153     debug_exit(' Others in '||l_proc_name);
3154     RAISE;
3155 END set_pay_proc_events_to_process;
3156 
3157 --
3158 -- get_events
3159 --
3160 -- Generic function to return events found by the date track
3161 -- interpreter for the given event group and assignment id
3162 --
3163 -- Bug 3015917 : Added params p_start_date and p_end_date
3164 -- Bugfix 3073562:GAP1:GAP2 Added p_business_group_id parameter
3165 --   This param needs to be passed only when the get_events proc
3166 --   is called during the criteria chk process as the asg
3167 --   collection will not hv a row for this asg id at this point.
3168 --   For all other calls, the proc will find the bg id from
3169 --   the global asg collection.
3170 FUNCTION get_events(p_event_group               IN VARCHAR2
3171                    ,p_assignment_id             IN NUMBER
3172                    ,p_element_entry_id          IN NUMBER -- DEFAULT NULL
3173                    ,p_business_group_id         IN NUMBER -- DEFAULT NULL
3174                    ,p_start_date                IN DATE
3175                    ,p_end_date                  IN DATE
3176                    ,t_proration_dates           OUT NOCOPY pay_interpreter_pkg.t_proration_dates_table_type
3177                    ,t_proration_changes         OUT NOCOPY pay_interpreter_pkg.t_proration_type_table_type
3178                    ) RETURN NUMBER
3179 IS
3180 
3181   -- Variable Declaration
3182   l_no_of_events NUMBER;
3183 
3184   -- Rowtype Variable Declaration
3185   l_event_group_details pqp_gb_tp_pension_extracts.csr_event_group_details%ROWTYPE;
3186   l_business_group_id   per_all_assignments_f.business_group_id%TYPE;
3187   l_assignment_action_id number;
3188   l_proc_name          VARCHAR2(61):=
3189      g_proc_name||'get_events';
3190 
3191  -- Added to pass assignment action ID for event PQP_GB_TP_GRADE_RULES(5547471)
3192   CURSOR c_get_aact(p_assignment_id NUMBER) IS
3193   SELECT assignment_action_id
3194   FROM pay_assignment_actions
3195   WHERE assignment_id = p_assignment_id
3196   AND rownum = 1;
3197 
3198 BEGIN
3199 
3200   debug_enter(l_proc_name);
3201 
3202   -- Bugfix 3073562:GAP1:GAP2
3203   IF p_business_group_id IS NULL THEN
3204 
3205     -- If the asg is present in the global collections
3206     -- then get the bg id from there
3207     -- otherwise use g_business_groupd_id
3208     IF g_ext_asg_details.EXISTS(p_assignment_id) THEN
3209 
3210       -- Doing NVL for no reason, just being over cautios...
3211       l_business_group_id :=
3212                 NVL(g_ext_asg_details(p_assignment_id).business_group_id
3213                    ,g_business_group_id
3214                    );
3215     ELSE -- does not exist
3216       l_business_group_id := g_business_group_id;
3217     END IF;
3218 
3219   ELSE -- Not null
3220     l_business_group_id := p_business_group_id;
3221   END IF; -- p_business_group_id IS NULL THEN
3222 
3223   debug('Business_group_id :'||to_char(l_business_group_id), 10);
3224 
3225   -- Now invoke the date track interpreter
3226   -- Bug 3015917 : Replaced old DTI call with this new DTI call
3227   IF p_event_group <> 'PQP_GB_TP_GRADE_RULES' THEN
3228   l_no_of_events := pqp_utilities.get_events
3229                      (p_assignment_id             => p_assignment_id
3230                      ,p_element_entry_id          => p_element_entry_id
3231                      ,p_business_group_id         => l_business_group_id
3232                      ,p_process_mode              => 'ENTRY_EFFECTIVE_DATE'
3233                      ,p_event_group_name          => p_event_group
3234                      ,p_start_date                => p_start_date
3235                      ,p_end_date                  => p_end_date
3236                      ,t_proration_dates          => t_proration_dates -- OUT
3237                      ,t_proration_change_type    => t_proration_changes -- OUT
3238                      );
3239    ELSE
3240  -- Added to pass assignment action ID for event PQP_GB_TP_GRADE_RULES(5547471)
3241    	  OPEN c_get_aact(p_assignment_id);
3242 	  FETCH c_get_aact INTO l_assignment_action_id;
3243 	  CLOSE c_get_aact;
3244 	  l_no_of_events := pqp_utilities.get_events
3245 			     (p_assignment_id             => p_assignment_id,
3246 			      p_assignment_action_id      => l_assignment_action_id
3247 			     ,p_element_entry_id          => p_element_entry_id
3248 			     ,p_business_group_id         => l_business_group_id
3249 			     ,p_process_mode              => 'ENTRY_EFFECTIVE_DATE'
3250 			     ,p_event_group_name          => p_event_group
3251 			     ,p_start_date                => p_start_date
3252 			     ,p_end_date                  => p_end_date
3253 			     ,t_proration_dates          => t_proration_dates -- OUT
3254 			     ,t_proration_change_type    => t_proration_changes -- OUT
3255 			     );
3256    END IF;
3257   debug_exit(l_proc_name);
3258 
3259   RETURN t_proration_dates.COUNT;
3260 
3261 EXCEPTION
3262   WHEN OTHERS THEN
3263     debug_exit(' Others in '||l_proc_name);
3264     t_proration_dates.delete;
3265     t_proration_changes.delete;
3266 
3267     RAISE;
3268 END; -- get_events
3269 
3270 --
3271 -- Get the per system status from per_assignment_status_types
3272 --
3273 FUNCTION get_status_type
3274    (p_status_type_id IN NUMBER
3275    ) RETURN VARCHAR2
3276 IS
3277 
3278   l_proc_name          VARCHAR2(61):=
3279      g_proc_name||'get_status_type';
3280 
3281   CURSOR csr_status_type IS
3282   SELECT per_system_status
3283   FROM per_assignment_status_types
3284   WHERE assignment_status_type_id = p_status_type_id;
3285 
3286   l_per_system_status   per_assignment_status_types.per_system_status%TYPE;
3287 
3288 BEGIN -- get_status_type
3289 
3290   debug_enter(l_proc_name);
3291 
3292   OPEN csr_status_type;
3293   FETCH csr_status_type INTO l_per_system_status;
3294   CLOSE csr_status_type ;
3295 
3296   debug('l_per_system_status :'||l_per_system_status, 10);
3297   debug_exit(l_proc_name);
3298 
3299   RETURN l_per_system_status;
3300 
3301 EXCEPTION
3302   WHEN OTHERS THEN
3303     debug_exit(' Others in '||l_proc_name);
3304     RAISE;
3305 END; -- get_status_type
3306 
3307 --
3308 -- Check if the assignment satisfies the basic criteria
3309 --
3310 FUNCTION chk_has_tchr_elected_pension
3311   (p_business_group_id        IN      NUMBER  -- context
3312   ,p_effective_date           IN      DATE    -- context
3313   ,p_assignment_id            IN      NUMBER  -- context
3314   ,p_asg_details              OUT NOCOPY     csr_asg_details_up%ROWTYPE
3315   ,p_asg_attributes           OUT NOCOPY     csr_pqp_asg_attributes_up%ROWTYPE
3316   ) RETURN VARCHAR2 -- Y or N
3317 IS
3318 
3319   -- Variable Declaration
3320   l_pension_start_date DATE := NULL;
3321 
3322   -- Rowtype Variable Declaration
3323   l_asg_details        csr_asg_details_up%ROWTYPE;
3324   l_pqp_asg_attributes csr_pqp_asg_attributes_up%ROWTYPE;
3325 
3326   -- Flag variable declaration
3327   l_inclusion_flag      VARCHAR2(1) := 'Y'; -- Include all Teachers
3328   l_quit_asg_loop       BOOLEAN := FALSE;
3329 
3330   l_proc_name          VARCHAR2(61):=
3331      g_proc_name||'chk_has_tchr_elected_pension';
3332 
3333 BEGIN -- chk_has_tchr_elected_pension
3334 
3335   debug_enter(l_proc_name);
3336 
3337   OPEN csr_pqp_asg_attributes_up
3338           (p_assignment_id
3339           ,p_effective_date
3340           );
3341   LOOP -- Find the first effective assignment attributes in the run date range.
3342 
3343     FETCH csr_pqp_asg_attributes_up INTO l_pqp_asg_attributes;
3344 
3345     debug('Inside AAT LOOP ',20);
3346 
3347     IF csr_pqp_asg_attributes_up%NOTFOUND THEN
3348       -- aat not found, hence this assignment does not qualify
3349 
3350        l_inclusion_flag := 'N';
3351        debug('AAT Data not found',30);
3352 
3353     ELSIF (l_pqp_asg_attributes.effective_start_date <= g_effective_run_date
3354           )
3355           AND
3356           (nvl(l_pqp_asg_attributes.tp_is_teacher,'NONT')
3357                 IN ('TCHR','TTR6')
3358           ) THEN
3359         -- Only interested in teacher aat recs effective in the run date range.
3360 
3361       -- Get assignment details
3362       OPEN csr_asg_details_up
3363                 (p_assignment_id
3364                 ,GREATEST(l_pqp_asg_attributes.effective_start_date
3365                          ,p_effective_date)
3366                 );
3367 
3368       l_quit_asg_loop := FALSE;
3369 
3370       LOOP -- Through assignment records to check if there is an assignment
3371            -- record with a valid criteria establishment
3372            -- But make sure that the check is only for the run date range.
3373 
3374           debug('Inside ASG LOOP ',40);
3375 
3376           FETCH csr_asg_details_up INTO l_asg_details;
3377 
3378           IF csr_asg_details_up%NOTFOUND THEN
3379 
3380             l_quit_asg_loop := TRUE;
3381             debug('ASG Data not found',50);
3382 
3383           ELSE -- asg record FOUND
3384 
3385             -- Bugfix 3073562:GAP1:GAP2
3386             -- Replacing the type4 func call with the type 1 function
3387             l_asg_details.ext_emp_cat_cd :=
3388                 get_translate_asg_emp_cat_code
3389                         (l_asg_details.asg_emp_cat_cd
3390                         ,GREATEST(l_pqp_asg_attributes.effective_start_date
3391                                  ,p_effective_date)
3392                         ,'Pension Extracts Employment Category Code'
3393                         ,l_asg_details.business_group_id
3394                         );
3395 
3396             -- Bugfix 3873376:SUSP : Suspended assignments fix
3397             --   We need to ignore suspended asgs to fix a bug where
3398             --   assignments suspended in the previous pension year
3399             --   are being picked up and reported
3400             l_asg_details.status_type := get_status_type(l_asg_details.status_type_id);
3401 
3402             -- If the establishment is NOT part of the criteria establishment(s)
3403             -- which we are reporting for, check the next record
3404             -- Also, if the start date is outside our date range, not look at this record.
3405           IF l_asg_details.start_date <= g_effective_run_date
3406                AND
3407                pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_asg_details.location_id)
3408                AND -- Bugfix 3873376:SUSP Ignore suspended assignments
3409                l_asg_details.status_type <> 'SUSP_ASSIGN'
3410                AND -- Bugfix 4336613 :IGNR_TERMASG_3A Ignore terminated assignments
3411                l_asg_details.status_type <> 'TERM_ASSIGN' THEN
3412 
3413             IF nvl(l_pqp_asg_attributes.tp_elected_pension,'X') = 'Y' THEN
3414 
3415               -- Do not exlude this assignment, get his pension start date.
3416               l_pension_start_date := GREATEST(l_pqp_asg_attributes.effective_start_date
3417                                           ,p_effective_date
3418                                           ,l_asg_details.start_date
3419                                           );
3420               debug('Teacher has Elected Pension',60);
3421               -- Teacher has elected pension,
3422               -- No need to check further assignments, set flag to quit the loop
3423               l_quit_asg_loop := TRUE;
3424 
3425             ELSIF nvl(l_pqp_asg_attributes.tp_elected_pension,'X') = 'N' THEN
3426 
3427               debug('Teacher has not Elected Pension',70);
3428               -- As this teacher has not elected pension, we need to
3429               -- do further checks before deciding on this assignment.
3430 
3431                 -- If this estb is part of the criteria establishment(s)
3432                 -- Then Exclude this assignment only if
3433                 --   1) This assignment belongs to a Relief Teacher and Estb Type is Voluntary.
3434                 --   2) This is a part-time assignment
3435                 --   3) This is a full-time assignment and Estb Type is Voluntary.
3436                 -- Include otherwise
3437 
3438                 IF NOT
3439                    (-- To be excluded
3440                     pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number
3441                         = '0966'
3442                     OR
3443                     (l_asg_details.ext_emp_cat_cd = 'P'
3444                      -- Bugfix 3641851:ENH7 : Added this AND clause to INCLUDE
3445                      --          part-time asgs at LEA establishment only
3446                      AND
3447                      pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_type
3448                         <> 'LEA_ESTB'
3449                     )
3450                     OR
3451                     (l_asg_details.ext_emp_cat_cd = 'F'
3452                      AND
3453                      pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_type
3454                         = 'IND_ESTB'
3455                     )
3456                    ) THEN
3457 
3458                   -- Do not exluce this assignment, get his pension start date.
3459                   l_pension_start_date := GREATEST(l_pqp_asg_attributes.effective_start_date
3460                                               ,p_effective_date
3461                                               ,l_asg_details.start_date
3462                                               );
3463 
3464                   debug('Teacher has not Elected Pension, but passes validation',80);
3465                   -- Teacher has elected pension, but passes other validation
3466                   -- No need to check further assignments, set flag to quit the loop
3467                 l_quit_asg_loop := TRUE;
3468 
3469                 END IF; -- NOT...
3470               --
3471             ELSIF l_pqp_asg_attributes.tp_elected_pension IS NULL THEN
3472 
3473               -- Check the next aat record, might be a valid one
3474               -- But first, exit the asg loop
3475               l_quit_asg_loop := TRUE;
3476               debug('Elected pension flag is NULL for this teacher',90);
3477 
3478             END IF; -- nvl(l_pqp_asg_attributes.tp_elected_pension,'N') = 'N' THEN
3479             --
3480           END IF; -- l_asg_details.start_date <= g_effective_run_date
3481           --
3482         END IF; -- csr_asg_details_up%NOTFOUND THEN
3483         --
3484         IF l_quit_asg_loop THEN
3485 
3486           debug('Quitting ASG Loop',100);
3487           EXIT;
3488 
3489         END IF;
3490         --
3491       END LOOP; -- Through assignment records to check if there is an assignment
3492 
3493       CLOSE csr_asg_details_up;
3494       debug('Pension Start Date :'||to_char(l_pension_start_date,'DD/MM/YYYY'),110);
3495 
3496     --ELSE -- csr_pqp_asg_attributes%NOTFOUND THEN
3497     --
3498     -- -- Either the aat record is with a start date higher than the date range
3499     -- -- or not a teaching record.
3500     -- -- We are not interested in this record
3501     -- -- Look at further records
3502 
3503     END IF; -- csr_pqp_asg_attributes%NOTFOUND THEN
3504 
3505     -- We do not need to look at the next record if this aat record qualifies.
3506     IF (l_pension_start_date is not null
3507           AND
3508           l_inclusion_flag = 'Y'
3509          )
3510          OR
3511          l_inclusion_flag = 'N'
3512          THEN
3513 
3514          -- Assign the pension start date to start date in asg record variable
3515          -- PS : If the original assignment start date is needed elsewhere, we might
3516          --      need to create a new column in the assignment or aat collection
3517          --      to hold the pension start date of the assignment.
3518          l_asg_details.start_date := l_pension_start_date;
3519          -- Bugfix 3641851:CBF1 : Assigning pension start date to teacher start date
3520          l_asg_details.teacher_start_date := l_pension_start_date;
3521 
3522          debug('Quitting AAT LOOP',120);
3523          EXIT;
3524 
3525     END IF; -- (l_pension_start_date is not null..
3526 
3527   END LOOP; -- Find the first effective assignment attributes in the run date range.
3528 
3529   CLOSE csr_pqp_asg_attributes_up;
3530 
3531   -- At this point,
3532   -- IF l_pension_start_date is not null and l_inclusion_flag = 'Y' THEN
3533   -- 1) the rowtype variable l_pqp_asg_attributes contains a valid EFFECTIVE aat row
3534   -- 2) the rowtype variable l_asg_details contains a valid EFFECTIVE asg row
3535   -- 3) the variable l_pension_start_date contains the start date which should be on
3536   --    the report. This has already been assigned to start date in l_asg_details.
3537   --
3538   -- These can be be passed back to the calling point
3539   -- Assign the local record variable to the return variables
3540   p_asg_details         := l_asg_details;
3541   p_asg_attributes      := l_pqp_asg_attributes;
3542 
3543   debug('Is this teacher being included by Basic Criteria ? '||l_inclusion_flag,130);
3544 
3545   debug_exit(l_proc_name);
3546   RETURN l_inclusion_flag;
3547 
3548 EXCEPTION
3549   WHEN OTHERS THEN
3550     debug_exit(' Others in '||l_proc_name);
3551     p_asg_details     := NULL;
3552     p_asg_attributes  := NULL;
3553     RAISE;
3554 END; -- chk_has_tchr_elected_pension
3555 
3556 --
3557 -- Check if the teacher's is a leaver
3558 --
3559 FUNCTION chk_is_teacher_a_leaver
3560   (p_business_group_id        IN      NUMBER
3561   ,p_effective_start_date     IN      DATE
3562   ,p_effective_end_date       IN      DATE
3563   ,p_assignment_id            IN      NUMBER
3564   ,p_leaver_date              OUT NOCOPY      DATE
3565   ) RETURN VARCHAR2 -- Y or N
3566 IS
3567 
3568   -- Variable Declaration
3569   l_leaver              VARCHAR2(1) := 'N';
3570   l_leaver_date         DATE;
3571   l_itr                 NUMBER;
3572   l_no_of_events        NUMBER(5);
3573   l_inclusion_flag      VARCHAR2(1) := 'Y';
3574   l_new_event_itr       NUMBER(5);
3575 
3576   -- Rowtype Variable Declaration
3577   l_event_group_details pqp_gb_tp_pension_extracts.csr_event_group_details%ROWTYPE;
3578   l_proration_dates     pay_interpreter_pkg.t_proration_dates_table_type;
3579   l_proration_changes   pay_interpreter_pkg.t_proration_type_table_type;
3580   l_asg_details         csr_asg_details_up%ROWTYPE;
3581   l_prev_asg_details    csr_asg_details_up%ROWTYPE;
3582   l_next_asg_details    csr_asg_details_up%ROWTYPE;
3583   l_pqp_asg_attributes       pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%ROWTYPE;
3584   l_last_pqp_asg_attributes  pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%ROWTYPE;
3585 
3586   l_temp_asg_details    csr_asg_details_up%ROWTYPE;
3587   l_temp_aat_details    csr_pqp_asg_attributes_up%ROWTYPE;
3588 
3589   l_proc_name           VARCHAR2(61):=
3590      g_proc_name||'chk_is_teacher_a_leaver';
3591 
3592 BEGIN -- chk_is_teacher_a_leaver
3593 
3594   debug_enter(l_proc_name);
3595   debug('p_effective_start_date: '||to_char(p_effective_start_date), 1) ;
3596   debug('p_effective_end_date: '||to_char(p_effective_end_date), 1) ;
3597 
3598   -- Update the events in pay_process_events to 'P' for "in process".
3599   -- Bug 3015917 : Removed set_pay_process.. as we now use the new style DTI
3600 
3601   -- Check for following leaver events here :
3602   -- Even if a leaver event is found, continue looking for more leaver evnts
3603   -- as we want the EARLIEST date of any of the leaver events
3604   --   1) Assignment Status change
3605   --   2) Location change resulting in change in Establishment from
3606   --            a) LEA estb to Non-LEA estb
3607   --            b) Non-LEA estb to LEA estb
3608   --            c) Non-LEA estb to Non-LEA estb
3609   --   3) Change in Elected pension flag resulting in the flag changing from 'Y' to 'N'
3610   --   4) Change in Teacher Status such that the status changes from
3611   --            'TCHR'/'TTR6' to 'NONT' / 'NULL'.(PS : NULL sh not be possible)
3612 
3613   -- 1) Assignment Status change
3614 
3615   -- Now invoke the date track interpreter
3616   -- Bug 3015917 : Replaced old DTI call with new style DTI call
3617   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_ASG_STATUS'
3618                               ,p_assignment_id          => p_assignment_id
3619                               ,p_business_group_id      => p_business_group_id
3620                               ,p_start_date             => p_effective_start_date
3621                               ,p_end_date               => p_effective_end_date
3622                               ,t_proration_dates        => l_proration_dates   -- OUT
3623                               ,t_proration_changes      => l_proration_changes -- OUT
3624                               );
3625 
3626   debug('Number of ASG_STATUS Events: '||
3627       fnd_number.number_to_canonical(l_no_of_events));
3628 
3629   l_itr := l_proration_dates.FIRST;
3630 
3631   WHILE l_itr <= l_proration_dates.LAST
3632   LOOP -- through the dates when some status change event happened
3633 
3634     debug('Inside ASG Dates LOOP',20);
3635     --
3636     -- eliminate duplicate dates
3637     -- compare the last value to the current one
3638     -- always process the first date
3639     IF l_itr = l_proration_dates.FIRST
3640        OR
3641        ( l_proration_dates(l_itr) <>
3642          l_proration_dates(l_proration_dates.PRIOR(l_itr))
3643        ) THEN
3644 
3645       debug('Event date  :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 25);
3646       debug('Update Type :'||l_proration_changes(l_itr), 26);
3647       debug('l_proration_dates(l_itr) - 1 :' ||to_char(l_proration_dates(l_itr) - 1),27);
3648 
3649 
3650       -- IMP : Changing to date - 1 as part of bugfix for end employment not begin picked up
3651       OPEN csr_asg_details_up
3652         (p_assignment_id
3653         ,l_proration_dates(l_itr) - 1
3654         );
3655       FETCH csr_asg_details_up INTO l_asg_details;
3656 
3657       IF csr_asg_details_up%FOUND THEN
3658 
3659         debug('Inside IF , found ASG record',30);
3660         -- Get the per_system_status for the assignment_status_type_id
3661         l_asg_details.status_type := get_status_type(l_asg_details.status_type_id);
3662 
3663         -- Get the next assignment and compare status type
3664         FETCH csr_asg_details_up INTO l_next_asg_details;
3665         l_next_asg_details.status_type := get_status_type(l_next_asg_details.status_type_id);
3666 
3667         debug('After second fetch',40);
3668         IF (csr_asg_details_up%FOUND
3669             AND
3670             l_asg_details.status_type <> l_next_asg_details.status_type
3671             AND
3672             l_next_asg_details.status_type IN ('TERM_ASSIGN','SUSP_ASSIGN','END')
3673             -- LVRDATE changes
3674             -- checking the assignment start date
3675             -- it should start on or before the period end date.
3676             AND
3677             l_next_asg_details.start_date <= p_effective_end_date
3678            )
3679            OR
3680            -- No future rows found
3681            (csr_asg_details_up%NOTFOUND
3682             AND
3683             -- But the current assignment has been suspended or is Active
3684             -- Added Active as a bugfix as END Employment does not change status
3685             -- when the termination happens on the last day of the payroll period
3686             l_asg_details.status_type IN ('SUSP_ASSIGN','ACTIVE_ASSIGN')
3687             -- Bugfix 3641851:CBF2 : Added this AND Clause
3688             --   We need to ensure that end date of asg record is <= end
3689             --   date of the period v r chking for. Ideally, DTI should
3690             --   not return a date if its outside our range, but a
3691             --   (potential) bug in DTI is causing such a situation and
3692             --   this check is a safety net, just in case.
3693             AND
3694             l_asg_details.effective_end_date <= p_effective_end_date
3695            )
3696            THEN
3697 
3698           -- Assignment has been terminated/suspended/ended
3699           l_leaver := 'Y';
3700           l_leaver_date := l_asg_details.effective_end_date;
3701 
3702           debug('Assignment is a leaver, Quitting LOOP ',50);
3703 
3704           --TERM_LSP: BUG: 4135481
3705           -- Store the assignement status change event as a new line of service event
3706 
3707           l_new_event_itr := g_asg_events.COUNT+1;
3708           debug('l_new_event_itr = '|| to_char(l_new_event_itr),55);
3709           debug('event_date: '|| to_char(l_proration_dates(l_itr)),56) ;
3710           debug('l_leaver_date: '|| to_char(l_leaver_date),57) ;
3711 
3712           g_asg_events(l_new_event_itr).event_date        := l_leaver_date;
3713           g_asg_events(l_new_event_itr).event_type        := 'PQP_GB_TP_ASG_STATUS';
3714           g_asg_events(l_new_event_itr).assignment_id     := p_assignment_id;
3715 
3716 
3717 
3718 
3719           CLOSE csr_asg_details_up;
3720           EXIT;
3721           --
3722         END IF; -- l_asg_details.status_type <> l_next_asg_details.status_type...
3723 
3724       END IF; -- csr_asg_details_up%FOUND THEN
3725       --
3726       CLOSE csr_asg_details_up;
3727 
3728     END IF; -- if this date <> last date, to eliminate duplicates
3729     --
3730     l_itr := l_proration_dates.NEXT(l_itr);
3731     --
3732   END LOOP; -- through the dates when some status change event happened
3733 
3734   l_proration_dates.DELETE;
3735   l_proration_changes.DELETE;
3736 
3737   l_asg_details         := NULL;
3738   l_next_asg_details    := NULL;
3739 
3740   debug('After Assignment Status Events check - Deleted Proration Dates',60);
3741 
3742   -- Check for the next set of events which qualify an assignment as leaver.
3743   --   2) Location change resulting in change in Establishment
3744   --            a) LEA estb to Non-LEA estb
3745   --            b) Non-LEA estb to LEA estb
3746   --            c) Non-LEA estb to Non-LEA estb
3747   debug('Now Checking for location change',70);
3748 
3749   -- Bug 3015917 : Replaced old DTI call with new style DTI call
3750   l_no_of_events := 0;
3751   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_ASG_LOCATION'
3752                               ,p_assignment_id          => p_assignment_id
3753                               ,p_business_group_id      => p_business_group_id
3754                               ,p_start_date             => p_effective_start_date
3755                               ,p_end_date               => p_effective_end_date
3756                               ,t_proration_dates        => l_proration_dates   -- OUT
3757                               ,t_proration_changes      => l_proration_changes -- OUT
3758                               );
3759 
3760 
3761   debug('Number of ASG_LOCATION Events: '||
3762    fnd_number.number_to_canonical(l_no_of_events));
3763 
3764   l_itr := l_proration_dates.FIRST;
3765   WHILE l_itr <= l_proration_dates.LAST
3766   LOOP -- through location change proration dates
3767 
3768     -- a location change event did take place, correction or update
3769     -- loop thru all the dates
3770     -- and query asg for location value
3771     -- check if location is a criteria location
3772     -- if so flag location changed and store teacher start date
3773     -- and exit else continue
3774 
3775     debug('Inside LOC events loop, Location change event found',80);
3776 
3777     --
3778     -- eliminate duplicate dates
3779     -- compare the last value to the current one
3780     -- always process the first date
3781     IF l_itr = l_proration_dates.FIRST
3782        OR
3783         ( l_proration_dates(l_itr) <>
3784           l_proration_dates(l_proration_dates.PRIOR(l_itr))
3785         )
3786        -- Bugfix 3470242:BUG3 : Need to make sure that we chk the next
3787        --        event just in case correction was returned b4 update
3788        OR
3789         (l_proration_changes(l_itr) <>
3790          l_proration_changes(l_proration_changes.PRIOR(l_itr))
3791         )
3792        THEN
3793 
3794       debug('Event date  :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 85);
3795       debug('Update Type :'||l_proration_changes(l_itr), 86);
3796 
3797       OPEN csr_asg_details_dn
3798           (p_assignment_id
3799           ,l_proration_dates(l_itr)
3800           );
3801       FETCH csr_asg_details_dn INTO l_asg_details;
3802 
3803       debug('Inside IF, After first fetch',90);
3804 
3805       IF (
3806           (csr_asg_details_dn%FOUND
3807            AND
3808            l_proration_changes(l_itr) = 'C' -- Correction
3809            AND -- asg start date > pension start date
3810                -- this is coz we don't want to pick an asg where
3811                -- the pension has started on that day.
3812                -- I.e. The correction sh have happened in the current pension year
3813            (l_asg_details.start_date >
3814               p_effective_start_date
3815                 -- g_ext_asg_details(p_assignment_id).start_date
3816             AND -- the start dat eof the asg record is <= effective run date
3817             l_asg_details.start_date <= p_effective_end_date
3818             -- l_asg_details.start_date <= g_effective_run_date
3819            )
3820           )
3821 
3822           OR
3823 
3824           (csr_asg_details_dn%FOUND
3825            AND
3826            l_proration_changes(l_itr) = 'U' -- Update
3827           )
3828          ) THEN
3829 
3830         -- Get the previous assignment record
3831         -- Currently not needed, will uncomment if needed
3832         --FETCH csr_asg_details_dn INTO l_prev_asg_details;
3833 
3834         debug('Event worth considering',100);
3835 
3836         --  Check if the locaiton change is a valid one to report as leaver
3837         -- The new establishment is not a criteria establishment
3838         IF NOT pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_asg_details.location_id) THEN
3839 
3840           -- The assignment has had a location change such that it has become a leaver
3841           l_leaver := 'Y';
3842           l_leaver_date := LEAST((l_asg_details.start_date - 1)
3843                                 ,nvl(l_leaver_date
3844                                     ,(l_asg_details.start_date - 1)
3845                                     )
3846                                 );
3847 
3848           debug('Leaver date  :'||to_char(l_leaver_date,'DD/MM/YYYY'), 105);
3849           debug('Assignment is a leaver due to location change, Quitting loop',110);
3850 
3851           CLOSE csr_asg_details_dn;
3852           EXIT;
3853 
3854         ELSE -- Location change is not a leaver event
3855 
3856           -- Bugfix 3073562:GAP10
3857           -- But as the location has changed form LEA Estb to
3858           -- another LEA Estb this is a new line of service event,
3859           -- store the event in the global collection.
3860 
3861           -- Get the previous assignment record
3862           FETCH csr_asg_details_dn INTO l_prev_asg_details;
3863 
3864           debug(l_proc_name, 120);
3865 
3866           IF l_proration_changes(l_itr) = 'U' -- Event was an update
3867              AND
3868              -- Prev rec was found. Redundant chk, sh always b found
3869              csr_asg_details_dn%FOUND
3870              AND
3871              -- Bugfix 3641851:CBF4 : Added just to avoid exception
3872              l_prev_asg_details.location_id IS NOT NULL
3873              AND
3874              -- The current and new Establishment nos. are different
3875              -- This chk is to ensure that the current location
3876              -- was not corrected after doing a datetrack update
3877              (-- Bugfix 3641851:CBF4 : Added EXISTS just to avoid exception
3878               pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_prev_asg_details.location_id)
3879               AND
3880               pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number
3881               <>
3882               pqp_gb_tp_pension_extracts.g_criteria_estbs(l_prev_asg_details.location_id).estb_number
3883              ) THEN
3884 
3885             debug('Location change is a new line of service event, storing', 130);
3886 
3887             l_new_event_itr := g_asg_events.COUNT+1;
3888 
3889             -- Store the location change event as a new line of service event
3890             g_asg_events(l_new_event_itr).event_date        := l_proration_dates(l_itr);
3891             g_asg_events(l_new_event_itr).event_type        := 'PQP_GB_TP_ASG_LOCATION';
3892             g_asg_events(l_new_event_itr).assignment_id     := p_assignment_id;
3893 
3894             -- Bugfix 3470242:BUG1 : Now using new_location_id, estb_number can
3895             --     always be sought using the location id
3896             --g_asg_events(l_new_event_itr).new_estb_number   :=
3897             --  pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number;
3898             g_asg_events(l_new_event_itr).new_location_id := l_asg_details.location_id;
3899 
3900             debug('New Location Id  :'||to_char(l_asg_details.location_id), 135);
3901 
3902           END IF; -- l_proration_changes(l_itr) = 'U'
3903           --
3904         END IF; -- Check if the locaiton change is a valid one to report as leaver
3905         --
3906       END IF; -- csr_asg_details_dn%FOUND THEN
3907       --
3908       CLOSE csr_asg_details_dn;
3909       --
3910     END IF; -- if this date <> last date to eliminate duplicates
3911     --
3912     l_itr := l_proration_dates.NEXT(l_itr);
3913 
3914     l_asg_details := NULL;
3915     l_prev_asg_details := NULL;
3916     --
3917   END LOOP; -- through location change proration dates
3918 
3919   l_proration_dates.DELETE;
3920   l_proration_changes.DELETE;
3921 
3922   debug('After Location Change check - Deleted Proration Dates',140);
3923 
3924   -- Check for the next set of events which qualify an assignment as leaver.
3925   --   3) Change in Elected pension flag resulting in the flag changing from 'Y' to 'N'
3926 
3927   -- Get the events
3928   l_no_of_events := 0;
3929   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_ELECTED_PENSION'
3930                               ,p_assignment_id          => p_assignment_id
3931                               ,p_business_group_id      => p_business_group_id
3932                               ,p_start_date             => p_effective_start_date
3933                               ,p_end_date               => p_effective_end_date
3934                               ,t_proration_dates        => l_proration_dates -- OUT
3935                               ,t_proration_changes      => l_proration_changes -- OUT
3936                               );
3937 
3938   debug('Number of '||'PQP_GB_TP_ELECTED_PENSION'||' Events: '||
3939      fnd_number.number_to_canonical(l_no_of_events), 150);
3940 
3941   -- b) Loop through the events and check if any have changed.
3942   l_itr := l_proration_dates.FIRST;
3943   WHILE l_itr <= l_proration_dates.LAST
3944   LOOP -- through change proration dates
3945 
3946     IF l_itr = l_proration_dates.FIRST
3947        OR
3948        ( l_proration_dates(l_itr) <>
3949          l_proration_dates(l_proration_dates.PRIOR(l_itr))
3950        ) THEN
3951 
3952       debug('Inside IF for Elected pension changes check',160);
3953       debug('Event date  :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 165);
3954       debug('Update Type :'||l_proration_changes(l_itr), 166);
3955 
3956       OPEN pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn
3957                         (p_assignment_id
3958                         ,l_proration_dates(l_itr)
3959                         );
3960       FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_pqp_asg_attributes;
3961       --
3962       IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
3963          AND
3964          NVL(l_pqp_asg_attributes.tp_elected_pension,'N') = 'N' THEN
3965 
3966         -- Fetch the previous set of attributes
3967         FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_last_pqp_asg_attributes;
3968         --
3969         IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
3970            AND
3971            nvl(l_last_pqp_asg_attributes.tp_elected_pension,'N') = 'Y' THEN
3972 
3973           debug('B4 checking if asg passes chk_has_tchr_elected_pension',170);
3974 
3975           -- Pension flag has changed to 'N', but check if this change to 'N'
3976           -- qualifies as leaver event. If the assignment satisfies the
3977           -- chk_has_teacher_elected_pension on this event date
3978           -- then the assignment is not a leaver, else, the assignment is a leaver
3979           l_inclusion_flag := chk_has_tchr_elected_pension
3980                                 (p_business_group_id            => p_business_group_id
3981                                 ,p_effective_date               => l_proration_dates(l_itr)
3982                                 ,p_assignment_id                => p_assignment_id
3983                                 ,p_asg_details                  => l_temp_asg_details   -- OUT
3984                                 ,p_asg_attributes               => l_temp_aat_details -- OUT
3985                                 );
3986 
3987           IF l_inclusion_flag = 'N'
3988              -- BUGFIX 2414035 : Added the following condition to fix this bug
3989              OR
3990              (l_inclusion_flag = 'Y' -- Has again become eligible in this pension year
3991               AND --  but on a future date
3992               l_temp_asg_details.start_date > l_proration_dates(l_itr)
3993              ) THEN
3994 
3995             debug('This assignment HAS opted out nocopy of the pension scheme.',180);
3996 
3997             l_leaver := 'Y';
3998             l_leaver_date := LEAST((l_pqp_asg_attributes.effective_start_date - 1)
3999                                   ,nvl(l_leaver_date
4000                                       ,(l_pqp_asg_attributes.effective_start_date - 1)
4001                                       )
4002                                   );
4003             debug('Leaver Date :'||to_char(l_leaver_date,'DD/MM/YYYY'), 185);
4004 
4005             CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4006             EXIT; -- quit loop -- no need to search for other events
4007 
4008           END IF; -- l_inclusion_flag = 'N' THEN
4009         --
4010         END IF; -- 2 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4011       --
4012       END IF; -- 1 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4013       --
4014       CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4015       --
4016       debug('Assignment attributes have had changes',190);
4017       --
4018     END IF; -- if this date <> last date to eliminate duplicates
4019     --
4020     l_itr := l_proration_dates.NEXT(l_itr);
4021     --
4022   END LOOP; -- through elected pension change proration dates
4023 
4024   l_proration_dates.DELETE;
4025   l_proration_changes.DELETE;
4026 
4027   l_pqp_asg_attributes := NULL;
4028   l_last_pqp_asg_attributes := NULL;
4029 
4030   debug('After Elected Pension Flag change check - Deleted Proration Dates',200);
4031 
4032   -- Check for the next set of events which qualify an assignment as leaver.
4033   --   4) Change in Teacher Status such that the status changes from
4034   --            'TCHR'/'TTR6' to 'NONT' / 'NULL'.(PS : NULL sh not be possible)
4035 
4036   -- Get the events
4037   l_no_of_events := 0;
4038   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_IS_TEACHER'
4039                               ,p_assignment_id          => p_assignment_id
4040                               ,p_business_group_id      => p_business_group_id
4041                               ,p_start_date             => p_effective_start_date
4042                               ,p_end_date               => p_effective_end_date
4043                               ,t_proration_dates        => l_proration_dates -- OUT
4044                               ,t_proration_changes      => l_proration_changes -- OUT
4045                               );
4046 
4047   debug('Number of '||'PQP_GB_TP_IS_TEACHER'||' Events: '||
4048      fnd_number.number_to_canonical(l_no_of_events),210);
4049 
4050   -- b) Loop through the events and check if any have changed.
4051   l_itr := l_proration_dates.FIRST;
4052   WHILE l_itr <= l_proration_dates.LAST
4053   LOOP -- through change proration dates
4054 
4055     IF (l_itr = l_proration_dates.FIRST
4056         OR
4057         ( l_proration_dates(l_itr) <>
4058           l_proration_dates(l_proration_dates.PRIOR(l_itr))
4059         )
4060        )
4061        AND
4062        (l_proration_changes(l_itr) <> 'C' -- Not a Correction
4063        ) THEN
4064 
4065       debug('Event date  :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 215);
4066       debug('Update Type :'||l_proration_changes(l_itr), 216);
4067 
4068       OPEN pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn
4069                         (p_assignment_id
4070                         ,l_proration_dates(l_itr)
4071                         );
4072       FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_pqp_asg_attributes;
4073       --
4074       IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4075          AND
4076          NVL(l_pqp_asg_attributes.tp_is_teacher,'NONT') = 'NONT' THEN
4077 
4078         -- Fetch the previous set of attributes
4079         FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_last_pqp_asg_attributes;
4080         --
4081         IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4082            AND
4083            nvl(l_last_pqp_asg_attributes.tp_is_teacher,'NONT') IN ('TCHR','TTR6') THEN
4084 
4085           debug('This assignment HAS become a Non-Teacher.',220);
4086 
4087           l_leaver := 'Y';
4088           l_leaver_date := LEAST((l_pqp_asg_attributes.effective_start_date - 1)
4089                                   ,nvl(l_leaver_date
4090                                       ,(l_pqp_asg_attributes.effective_start_date - 1)
4091                                       )
4092                                 );
4093           debug('Leaver Date :'||to_char(l_leaver_date,'DD/MM/YYYY'), 225);
4094 
4095           CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4096           EXIT; -- quit loop -- no need to search for other events
4097           --
4098         END IF; -- 2 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4099       --
4100       END IF; -- 1 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4101       --
4102       CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4103       --
4104       debug('Assignment attributes have had changes',230);
4105       --
4106     END IF; -- if this date <> last date to eliminate duplicates and ignore corrections
4107     --
4108     l_itr := l_proration_dates.NEXT(l_itr);
4109     --
4110   END LOOP; -- through elected pension change proration dates
4111 
4112   debug('After Teacher Status change check',240);
4113 
4114   -- Reset the events in pay_process_events to 'U' for "Unprocessed".
4115   -- Bug 3015917 : Removed set_pay_process.. as we now use the new style DTI
4116 
4117   l_proration_dates.DELETE;
4118   l_proration_changes.DELETE;
4119 
4120   debug('Leaver Flag :'||l_leaver,250);
4121   debug('Leaver Date :'||to_char(l_leaver_date,'dd/mm/yyyy'),260);
4122 
4123   -- Assign the leaver date value to the return parameter
4124   p_leaver_date := l_leaver_date;
4125 
4126   debug_exit(l_proc_name);
4127   RETURN l_leaver;
4128 
4129 EXCEPTION
4130   WHEN OTHERS THEN
4131     debug_exit(' Others in '||l_proc_name);
4132     p_leaver_date := NULL;
4133     RAISE;
4134 END; -- chk_is_teacher_a_leaver
4135 
4136 --
4137 -- Check if the leaver teacher is also a re-starter
4138 --
4139 FUNCTION chk_is_leaver_a_restarter
4140   (p_business_group_id        IN      NUMBER
4141   ,p_effective_start_date     IN      DATE
4142   ,p_effective_end_date       IN      DATE
4143   ,p_assignment_id            IN      NUMBER
4144   ,p_restarter_date           OUT     NOCOPY  DATE
4145   ) RETURN VARCHAR2 -- Y or N
4146 IS
4147 
4148   -- Variable Declaration
4149   l_restarter           VARCHAR2(1) := 'N';
4150   l_restarter_date      DATE;
4151   l_itr                 NUMBER;
4152   l_no_of_events        NUMBER(5);
4153   l_inclusion_flag      VARCHAR2(1) := 'N';
4154 
4155   -- Rowtype Variable Declaration
4156   l_event_group_details pqp_gb_tp_pension_extracts.csr_event_group_details%ROWTYPE;
4157   l_proration_dates     pay_interpreter_pkg.t_proration_dates_table_type;
4158   l_proration_changes   pay_interpreter_pkg.t_proration_type_table_type;
4159   l_asg_details         csr_asg_details_up%ROWTYPE;
4160   l_prev_asg_details    csr_asg_details_up%ROWTYPE;
4161   l_pqp_asg_attributes       pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%ROWTYPE;
4162   l_last_pqp_asg_attributes  pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%ROWTYPE;
4163 
4164   l_temp_asg_details    csr_asg_details_up%ROWTYPE;
4165   l_temp_aat_details    csr_pqp_asg_attributes_up%ROWTYPE;
4166 
4167   -- BUG : 3873376
4168   --RSTRT: new variables added
4169   l_asg_details_restart         csr_asg_details_up%ROWTYPE;
4170   l_pqp_asg_attributes_restart  csr_pqp_asg_attributes_up%ROWTYPE;
4171 
4172   l_proc_name           VARCHAR2(61):=
4173      g_proc_name||'chk_is_leaver_a_restarter';
4174 
4175 BEGIN -- chk_is_leaver_a_restarter
4176 
4177   debug_enter(l_proc_name);
4178   debug('p_effective_start_date '||p_effective_start_date);
4179   debug('p_effective_end_date '||p_effective_end_date);
4180 
4181   -- Update the events in pay_process_events to 'P' for "in process".
4182   -- Bug 3015917 : Removed set_pay_process.. as we now use the new style DTI
4183 
4184   -- Check for following restarter events here :
4185   -- Even if a restarter event is found, continue looking for more restarter events
4186   -- as we want the EARLIEST date of any of the restarter events
4187   --   1) Assignment Status change, back to ACTIVE from SUSPENDED ONLY
4188   --   2) Location change resulting in change in Establishment from
4189   --            a) LEA estb to Non-LEA estb
4190   --            b) Non-LEA estb to LEA estb
4191   --            c) Non-LEA estb to Non-LEA estb
4192   --   3) Change in Elected pension flag resulting in the flag changing from 'N' to 'Y'
4193   --   4) Change in Teacher Status such that the status changes from
4194   --            'NONT' / 'NULL' to 'TCHR'/'TTR6'
4195 
4196 
4197   --   1) Assignment Status change, back to ACTIVE from SUSPENDED ONLY
4198   -- Get the events
4199   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_ASG_STATUS'
4200                               ,p_assignment_id          => p_assignment_id
4201                               ,p_business_group_id      => p_business_group_id
4202                               ,p_start_date             => p_effective_start_date
4203                               ,p_end_date               => p_effective_end_date
4204                               ,t_proration_dates        => l_proration_dates -- OUT
4205                               ,t_proration_changes      => l_proration_changes -- OUT
4206                               );
4207 
4208   debug('Number of ASG_STATUS Events: '||
4209       fnd_number.number_to_canonical(l_proration_dates.COUNT));
4210 
4211   l_itr := l_proration_dates.FIRST;
4212 
4213   WHILE l_itr <= l_proration_dates.LAST
4214   LOOP -- through the dates when some status change event happened
4215 
4216     debug('Inside ASG Dates LOOP',20);
4217     --
4218     -- eliminate duplicate dates
4219     -- compare the last value to the current one
4220     -- always process the first date
4221     IF l_itr = l_proration_dates.FIRST
4222        OR
4223        ( l_proration_dates(l_itr) <>
4224          l_proration_dates(l_proration_dates.PRIOR(l_itr))
4225        ) THEN
4226 
4227       OPEN csr_asg_details_dn
4228         (p_assignment_id
4229         ,l_proration_dates(l_itr)
4230         );
4231       FETCH csr_asg_details_dn INTO l_asg_details;
4232 
4233       IF csr_asg_details_dn%FOUND THEN
4234 
4235         debug('Inside IF , found ASG record',30);
4236         -- Get the per_system_status for the assignment_status_type_id
4237         l_asg_details.status_type := get_status_type(l_asg_details.status_type_id);
4238 
4239         -- Get the previous assignment and compare status type
4240         FETCH csr_asg_details_dn INTO l_prev_asg_details;
4241         l_prev_asg_details.status_type := get_status_type(l_prev_asg_details.status_type_id);
4242 
4243         debug('After second fetch',40);
4244         debug('l_asg_details.location_id '||l_asg_details.location_id,41);
4245 
4246         IF (csr_asg_details_dn%FOUND
4247             AND
4248             l_prev_asg_details.status_type = 'SUSP_ASSIGN'
4249             AND
4250             l_asg_details.status_type = 'ACTIVE_ASSIGN'
4251             -- check if the location is a valid location
4252             AND
4253             pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_asg_details.location_id)
4254            ) THEN
4255 
4256           debug('now checking chk_has_tchr_elected_pension',42);
4257           -- BUG : 3873376
4258           --:RSTRT --check if the asg is a valid tchr assignment on restart date.
4259           l_inclusion_flag := chk_has_tchr_elected_pension
4260                                 (p_business_group_id => p_business_group_id
4261                                 ,p_effective_date    => l_asg_details.start_date
4262                                 ,p_assignment_id     => p_assignment_id
4263                                 ,p_asg_details       => l_asg_details_restart        -- OUT
4264                                 ,p_asg_attributes    => l_pqp_asg_attributes_restart -- OUT
4265                                 );
4266           debug('l_inclusion_flag :' ||l_inclusion_flag,43);
4267           debug('l_asg_details_restart.start_date :'||l_asg_details_restart.start_date);
4268 
4269           IF l_inclusion_flag = 'Y'
4270              -- Valid tchr on the same date as of restart_date and not in future.
4271              -- future events will be taken care of in the other events check
4272              AND NOT(l_asg_details_restart.start_date > l_asg_details.start_date)
4273           THEN
4274             -- Assignment has been restarted
4275             l_restarter := 'Y';
4276             l_restarter_date := l_asg_details.start_date;
4277 
4278             debug('Assignment is a restarter, Quitting LOOP ',50);
4279             CLOSE csr_asg_details_dn;
4280             EXIT;
4281             --
4282           END IF; --l_inclusion_flag = 'Y' THEN
4283 
4284         END IF; -- FOUND and previous status = 'SUSP_ASSIGN' and current status = 'ACTIVE_ASSIGN'
4285         --
4286       END IF; -- csr_asg_details_dn%FOUND THEN
4287       --
4288       CLOSE csr_asg_details_dn;
4289 
4290     END IF; -- if this date <> last date, to eliminate duplicates
4291     --
4292     l_itr := l_proration_dates.NEXT(l_itr);
4293     --
4294   END LOOP; -- through the dates when some status change event happened
4295 
4296   l_proration_dates.DELETE;
4297   l_proration_changes.DELETE;
4298 
4299   l_asg_details         := NULL;
4300   l_prev_asg_details    := NULL;
4301 
4302   l_asg_details_restart        := NULL;
4303   l_pqp_asg_attributes_restart := NULL ;
4304 
4305   debug('After Assignment Status Events check - Deleted Proration Dates',60);
4306 
4307   -- Check for the next set of events which qualify an assignment as restarter.
4308   --   2) Location change resulting in change in Establishment
4309   --            a) LEA estb to Non-LEA estb
4310   --            b) Non-LEA estb to LEA estb
4311   --            c) Non-LEA estb to Non-LEA estb
4312   debug('Now Checking for location change',70);
4313 
4314   -- Get the events
4315   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_ASG_LOCATION'
4316                               ,p_assignment_id          => p_assignment_id
4317                               ,p_business_group_id      => p_business_group_id
4318                               ,p_start_date             => p_effective_start_date
4319                               ,p_end_date               => p_effective_end_date
4320                               ,t_proration_dates        => l_proration_dates -- OUT
4321                               ,t_proration_changes      => l_proration_changes -- OUT
4322                               );
4323 
4324   debug('Number of ASG_LOCATION Events: '||
4325    fnd_number.number_to_canonical(l_proration_dates.COUNT));
4326 
4327   l_itr := l_proration_dates.FIRST;
4328   WHILE l_itr <= l_proration_dates.LAST
4329   LOOP -- through location change proration dates
4330 
4331     -- a location change event did take place, correction or update
4332     -- PS : Currently ignoring corrections, but if later there is a
4333     --      need to include corrections then
4334     --      ONLY include if asg start date is > p_effective_start_date
4335     --      coz if the asg start date is = p_effective_date then the
4336     --      asg will mostly not be reported as a leaver in the first place
4337     -- loop thru all the dates
4338     -- and query asg for location value
4339     -- check if location is a criteria location
4340     -- also confirm that the previous location was a non criteria location
4341     -- if so flag location changed and store teacher start date
4342     -- and exit else continue
4343 
4344     debug('Inside LOC events loop, Location change event found',80);
4345 
4346     --
4347     -- eliminate duplicate dates
4348     -- compare the last value to the current one
4349     -- always process the first date
4350     IF l_itr = l_proration_dates.FIRST
4351        OR
4352         ( l_proration_dates(l_itr) <>
4353           l_proration_dates(l_proration_dates.PRIOR(l_itr))
4354         )
4355        THEN
4356 
4357       OPEN csr_asg_details_dn
4358           (p_assignment_id
4359           ,l_proration_dates(l_itr)
4360           );
4361       FETCH csr_asg_details_dn INTO l_asg_details;
4362 
4363       debug('Inside IF, After first fetch',90);
4364 
4365       IF (csr_asg_details_dn%FOUND
4366           AND
4367           l_proration_changes(l_itr) <> 'C' -- Not a Correction
4368          ) THEN
4369 
4370         -- Get the previous assignment record
4371         FETCH csr_asg_details_dn INTO l_prev_asg_details;
4372 
4373         debug('Event worth considering',100);
4374 
4375         --  Check if the locaiton change is a valid one to report as restarter
4376         -- The new establishment must be a criteria establishment
4377         -- and the previous establishment must not be a criteria establishment
4378         IF (csr_asg_details_dn%FOUND
4379             AND -- the new asg location is a criteria location
4380             pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_asg_details.location_id)
4381             AND -- the previous was not a criteria location
4382             (NOT pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_prev_asg_details.location_id)
4383             )
4384            ) THEN
4385           debug('now checking chk_has_tchr_elected_pension',101);
4386           -- BUG : 3873376
4387           --:RSTRT --check if the asg is a valid tchr assignment on restart date.
4388           l_inclusion_flag := chk_has_tchr_elected_pension
4389                                 (p_business_group_id => p_business_group_id
4390                                 ,p_effective_date    => l_asg_details.start_date
4391                                 ,p_assignment_id     => p_assignment_id
4392                                 ,p_asg_details       => l_asg_details_restart        -- OUT
4393                                 ,p_asg_attributes    => l_pqp_asg_attributes_restart -- OUT
4394                                 );
4395           debug('l_inclusion_flag :' ||l_inclusion_flag,102);
4396           debug('l_asg_details_restart.start_date :' ||l_asg_details_restart.start_date);
4397 
4398           IF l_inclusion_flag = 'Y'
4399              -- Valid tchr on the same dateas the restart_Date, not in future.
4400              -- future events will be taken care of in the other events check
4401              AND NOT(l_asg_details_restart.start_date > l_asg_details.start_date)THEN
4402             -- The assignment has had a location change such that it has become a restarter
4403             l_restarter := 'Y';
4404             l_restarter_date := LEAST(l_asg_details.start_date
4405                                      ,nvl(l_restarter_date,l_asg_details.start_date)
4406                                      );
4407 
4408             debug('Assignment is a restarter due to location change, Quitting loop',110);
4409 
4410             CLOSE csr_asg_details_dn;
4411             EXIT;
4412           END IF ; --l_inclusion_flag = 'Y'
4413 
4414         END IF; -- Check if the locaiton change is a valid one to report as restarter
4415         --
4416       END IF; -- csr_asg_details_dn%FOUND THEN
4417       --
4418       CLOSE csr_asg_details_dn;
4419       --
4420     END IF; -- if this date <> last date to eliminate duplicates
4421     --
4422     l_itr := l_proration_dates.NEXT(l_itr);
4423     --
4424   END LOOP; -- through location change proration dates
4425 
4426   l_proration_dates.DELETE;
4427   l_proration_changes.DELETE;
4428 
4429   l_asg_details_restart        := NULL;
4430   l_pqp_asg_attributes_restart := NULL ;
4431 
4432 
4433   debug('After Location Change check - Deleted Proration Dates',120);
4434 
4435   -- Check for the next set of events which qualify an assignment as restarter.
4436   --   3) Change in Elected pension flag resulting in the flag changing from 'N' to 'Y'
4437 
4438   -- Get the events
4439   l_no_of_events := 0;
4440   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_ELECTED_PENSION'
4441                               ,p_assignment_id          => p_assignment_id
4442                               ,p_business_group_id      => p_business_group_id
4443                               ,p_start_date             => p_effective_start_date
4444                               ,p_end_date               => p_effective_end_date
4445                               ,t_proration_dates        => l_proration_dates -- OUT
4446                               ,t_proration_changes      => l_proration_changes -- OUT
4447                               );
4448 
4449   debug('Number of '||'PQP_GB_TP_ELECTED_PENSION'||' Events: '||
4450      fnd_number.number_to_canonical(l_no_of_events));
4451 
4452   -- b) Loop through the events and check if any have changed.
4453   l_itr := l_proration_dates.FIRST;
4454   WHILE l_itr <= l_proration_dates.LAST
4455   LOOP -- through change proration dates
4456 
4457     IF l_itr = l_proration_dates.FIRST
4458        OR
4459        ( l_proration_dates(l_itr) <>
4460          l_proration_dates(l_proration_dates.PRIOR(l_itr))
4461        ) THEN
4462 
4463       OPEN pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn
4464                         (p_assignment_id
4465                         ,l_proration_dates(l_itr)
4466                         );
4467       FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_pqp_asg_attributes;
4468       --
4469       IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4470          AND
4471          NVL(l_pqp_asg_attributes.tp_elected_pension,'N') = 'Y' THEN
4472 
4473         -- Fetch the previous set of attributes
4474         FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_last_pqp_asg_attributes;
4475         --
4476         IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4477            AND
4478            nvl(l_last_pqp_asg_attributes.tp_elected_pension,'N') = 'N' THEN
4479 
4480           debug('now checking chk_has_tchr_elected_pension',121);
4481           -- BUG : 3873376
4482           --:RSTRT --check if the asg is a valid tchr assignment on restart date.
4483           l_inclusion_flag := chk_has_tchr_elected_pension
4484                                 (p_business_group_id => p_business_group_id
4485                                 ,p_effective_date    => l_pqp_asg_attributes.effective_start_date
4486                                 ,p_assignment_id     => p_assignment_id
4487                                 ,p_asg_details       => l_asg_details_restart        -- OUT
4488                                 ,p_asg_attributes    => l_pqp_asg_attributes_restart -- OUT
4489                                 );
4490           debug('l_inclusion_flag :' ||l_inclusion_flag,122);
4491           debug('l_asg_details_restart.start_date :' ||l_asg_details_restart.start_date);
4492 
4493           IF l_inclusion_flag = 'Y'
4494              -- Valid tchr on the same dateas the restart_Date, not in future.
4495              -- future events will be taken care of in the other events check
4496              AND NOT(l_asg_details_restart.start_date > l_pqp_asg_attributes.effective_start_date) THEN
4497             -- Pension flag has changed to 'Y' from 'N'/NULL
4498             debug('This assignment HAS opted back into the pension scheme.');
4499             l_restarter := 'Y';
4500             l_restarter_date := LEAST(l_pqp_asg_attributes.effective_start_date
4501                                      ,nvl(l_restarter_date,l_pqp_asg_attributes.effective_start_date)
4502                                      );
4503 
4504             CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4505             EXIT; -- quit loop -- no need to search for other events
4506           END IF ; --l_inclusion_flag = 'Y' THEN
4507 
4508         --
4509         END IF; -- 2 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4510       --
4511       END IF; -- 1 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4512       --
4513       CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4514       --
4515       debug('Assignment attributes have had changes',130);
4516       --
4517     END IF; -- if this date <> last date to eliminate duplicates
4518     --
4519     l_itr := l_proration_dates.NEXT(l_itr);
4520     --
4521   END LOOP; -- through elected pension change proration dates
4522 
4523   l_proration_dates.DELETE;
4524   l_proration_changes.DELETE;
4525 
4526   l_pqp_asg_attributes := NULL;
4527   l_last_pqp_asg_attributes := NULL;
4528 
4529   l_asg_details_restart        := NULL;
4530   l_pqp_asg_attributes_restart := NULL ;
4531 
4532 
4533   debug('After Elected Pension Flag change check - Deleted Proration Dates',140);
4534 
4535   -- Check for the next set of events which qualify an assignment as restarter.
4536   --   4) Change in Teacher Status such that the status changes from
4537   --            'NONT' / 'NULL' to 'TCHR'/'TTR6'
4538 
4539   -- Get the events
4540   l_no_of_events := 0;
4541   l_no_of_events := get_events(p_event_group            => 'PQP_GB_TP_IS_TEACHER'
4542                               ,p_assignment_id          => p_assignment_id
4543                               ,p_business_group_id      => p_business_group_id
4544                               ,p_start_date             => p_effective_start_date
4545                               ,p_end_date               => p_effective_end_date
4546                               ,t_proration_dates        => l_proration_dates -- OUT
4547                               ,t_proration_changes      => l_proration_changes -- OUT
4548                               );
4549 
4550   debug('Number of '||'PQP_GB_TP_IS_TEACHER'||' Events: '||
4551      fnd_number.number_to_canonical(l_no_of_events));
4552 
4553   -- b) Loop through the events and check if any have changed.
4554   l_itr := l_proration_dates.FIRST;
4555   WHILE l_itr <= l_proration_dates.LAST
4556   LOOP -- through change proration dates
4557 
4558     IF (l_itr = l_proration_dates.FIRST
4559         OR
4560         ( l_proration_dates(l_itr) <>
4561           l_proration_dates(l_proration_dates.PRIOR(l_itr))
4562         )
4563        )
4564        AND
4565        (l_proration_changes(l_itr) <> 'C' -- Not a Correction
4566        ) THEN
4567 
4568       OPEN pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn
4569                         (p_assignment_id
4570                         ,l_proration_dates(l_itr)
4571                         );
4572       FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_pqp_asg_attributes;
4573       --
4574       IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4575          AND
4576          NVL(l_pqp_asg_attributes.tp_is_teacher,'NONT') IN ('TCHR','TTR6') THEN
4577 
4578         -- Fetch the previous set of attributes
4579         FETCH pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn INTO l_last_pqp_asg_attributes;
4580         --
4581         IF pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4582            AND
4583            nvl(l_last_pqp_asg_attributes.tp_is_teacher,'NONT') = 'NONT' THEN
4584 
4585           debug('now checking chk_has_tchr_elected_pension',141);
4586           -- BUG : 3873376
4587           --:RSTRT --check if the asg is a valid tchr assignment on restart date.
4588           l_inclusion_flag := chk_has_tchr_elected_pension
4589                                 (p_business_group_id => p_business_group_id
4590                                 ,p_effective_date    => l_pqp_asg_attributes.effective_start_date
4591                                 ,p_assignment_id     => p_assignment_id
4592                                 ,p_asg_details       => l_asg_details_restart        -- OUT
4593                                 ,p_asg_attributes    => l_pqp_asg_attributes_restart -- OUT
4594                                 );
4595           debug('l_inclusion_flag :' ||l_inclusion_flag,142);
4596           debug('l_asg_details_restart.start_date :' ||l_asg_details_restart.start_date);
4597 
4598           IF l_inclusion_flag = 'Y'
4599              -- Valid tchr on the same dateas the restart_Date, not in future.
4600              -- future events will be taken care of in the other events check
4601              AND NOT(l_asg_details_restart.start_date > l_pqp_asg_attributes.effective_start_date) THEN
4602 
4603             debug('The leaver HAS become a Teacher again.');
4604             l_restarter := 'Y';
4605             l_restarter_date := LEAST(l_pqp_asg_attributes.effective_start_date
4606                                    ,nvl(l_restarter_date,l_pqp_asg_attributes.effective_start_date)
4607                                    );
4608 
4609             CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4610             EXIT; -- quit loop -- no need to search for other events
4611           END IF ; -- l_inclusion_flag = 'Y' THEN
4612           --
4613         END IF; -- 2 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4614       --
4615       END IF; -- 1 pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn%FOUND
4616       --
4617       CLOSE pqp_gb_tp_pension_extracts.csr_pqp_asg_attributes_dn;
4618       --
4619       debug('Assignment attributes have had changes',150);
4620       --
4621     END IF; -- if this date <> last date to eliminate duplicates and ignore corrections
4622     --
4623     l_itr := l_proration_dates.NEXT(l_itr);
4624     --
4625   END LOOP; -- through elected pension change proration dates
4626 
4627   debug('After Teacher Status change check',160);
4628 
4629   -- Reset the events in pay_process_events to 'U' for "Unprocessed".
4630   -- Bug 3015917 : Removed set_pay_process.. as we now use the new style DTI
4631 
4632   l_proration_dates.DELETE;
4633   l_proration_changes.DELETE;
4634 
4635   l_asg_details_restart        := NULL;
4636   l_pqp_asg_attributes_restart := NULL ;
4637 
4638 
4639   debug('Restarter Flag :'||l_restarter,170);
4640   debug('Restarter Date :'||to_char(l_restarter_date,'dd/mm/yyyy'),180);
4641 
4642   -- Assign the restarter date value to the return parameter
4643   p_restarter_date := l_restarter_date;
4644 
4645   debug_exit(l_proc_name);
4646   RETURN l_restarter;
4647 
4648 EXCEPTION
4649   WHEN OTHERS THEN
4650     debug_exit(' Others in '||l_proc_name);
4651     p_restarter_date := NULL;
4652     RAISE;
4653 END; -- chk_is_leaver_a_restarter
4654 
4655 --
4656 -- Check if this teacher has already been reported in previous runs
4657 --
4658 FUNCTION chk_has_teacher_been_reported
4659    (p_person_id         IN NUMBER
4660     -- ALRD_RPT change
4661     ,p_leaver_date          IN DATE
4662    ) RETURN VARCHAR2
4663 IS
4664 
4665   -- Variable declaration
4666   l_reported_flag       VARCHAR2(1) := 'N';
4667 
4668   -- Cursor declaration
4669   CURSOR csr_person_reported IS
4670 SELECT 'Y'
4671   FROM pqp_extract_attributes  pqea
4672       ,ben_ext_rslt            rslt
4673       ,ben_ext_rslt_dtl        rdtl
4674       ,ben_ext_rcd             drcd
4675   WHERE pqea.ext_dfn_type      = g_extract_type
4676     AND rslt.ext_dfn_id        = pqea.ext_dfn_id
4677     -- Bugfix 3073562:GAP1:GAP2, now using master bg id
4678     AND rslt.business_group_id = g_master_bg_id
4679     AND rslt.ext_stat_cd NOT IN
4680           ('F' -- Job Failure
4681           ,'R' -- Rejected By User
4682           ,'X' -- Executing
4683           )
4684     AND rdtl.ext_rslt_id  = rslt.ext_rslt_id
4685     AND drcd.ext_rcd_id   = rdtl.ext_rcd_id
4686     AND drcd.rcd_type_cd  = 'D' -- detail records only
4687     -- changed the person_id check to NI Number check.
4688     --AND rdtl.person_id = p_person_id
4689     AND rdtl.val_04 IN
4690             ( SELECT national_identifier
4691                 FROM per_all_people_f per2
4692                WHERE per2.person_id = p_person_id
4693             )
4694     -- match the header element
4695     AND EXISTS
4696        ( SELECT 'Y'
4697            FROM  ben_ext_rslt_dtl rdtl1
4698           WHERE  rdtl1.business_group_id = g_master_bg_id
4699             AND  EXISTS
4700                 ( SELECT 'Y'
4701                     FROM ben_ext_rcd drcd1
4702                    WHERE drcd1.rcd_type_cd = 'H'
4703                      AND drcd1.ext_rcd_id  = rdtl1.ext_rcd_id
4704                  )
4705             AND rdtl.ext_rslt_id = rdtl1.ext_rslt_id
4706             AND SUBSTR(rdtl1.val_01
4707                      ,1
4708                      ,INSTR(rdtl1.val_01,':',1,3)--upto third occurence
4709                      )
4710               =SUBSTR(g_header_system_element
4711                      ,1
4712                      ,INSTR(g_header_system_element,':',1,3)
4713                      )
4714         )
4715     -- only in the current pension year and upto the end of last run
4716     AND rslt.eff_dt between g_pension_year_start_date and g_last_effective_date
4717     -- ALRD_RPT change
4718     -- checking for the matching date and withdrawl flag.
4719     AND to_date(rdtl.val_14,'DDMMRR') = p_leaver_date  -- Leaver Date
4720     AND rdtl.val_15 = 'W'  --Withdrawl Flag
4721     -- only need to look for one record
4722     AND ROWNUM < 2;
4723 
4724   l_proc_name          VARCHAR2(61):=
4725      g_proc_name||'chk_has_teacher_been_reported';
4726 
4727 BEGIN -- chk_has_teacher_been_reported
4728 
4729   debug_enter(l_proc_name);
4730   debug('p_person_id  : '||to_char(p_person_id),10);
4731   debug('p_leaver_date: '||to_char(p_leaver_date));
4732 
4733   -- ALRDRPT changes.
4734   -- check for the global variable
4735   -- if already set by the previous call to the function return that.
4736   -- else execute the logic to find the flag.
4737 
4738   -- ALRD_RPT changes
4739 
4740   -- PER_LVR:  removed the check
4741   -- coz we need to check for each leaver date separatly.
4742 
4743   --IF g_person_already_reported IS NULL THEN
4744 
4745     debug(l_proc_name,10) ;
4746     OPEN csr_person_reported;
4747     FETCH csr_person_reported INTO l_reported_flag;
4748     CLOSE csr_person_reported;
4749 
4750     -- PER_LVR:  removed the check
4751     --g_person_already_reported := l_reported_flag ;
4752 
4753   --ELSE
4754     --debug(l_proc_name,20) ;
4755     --l_reported_flag := g_person_already_reported ;
4756   --END IF;
4757 
4758   debug('Teacher Already Reported:'||l_reported_flag);
4759   debug_exit(l_proc_name);
4760 
4761   RETURN l_reported_flag;
4762 
4763 EXCEPTION
4764   WHEN OTHERS THEN
4765     debug_exit(' Others in '||l_proc_name);
4766     RAISE;
4767 END; -- chk_has_teacher_been_reported
4768 --
4769 -- Bugfix 3073562:GAP9a
4770 -- Added this procedure for GAP9a
4771 --
4772 PROCEDURE warn_if_supply_tchr_is_ft
4773             (p_assignment_id            IN NUMBER
4774             ,p_establishment_number     IN VARCHAR2
4775             ,p_ext_emp_cat_code         IN VARCHAR2
4776             ) IS
4777 
4778   l_error           number;
4779 
4780   l_proc_name           VARCHAR2(61):=
4781      g_proc_name||'warn_if_supply_tchr_is_ft';
4782 
4783 BEGIN -- warn_if_supply_tchr_is_ft
4784 
4785   debug_enter(l_proc_name);
4786 
4787   IF p_establishment_number = '0966' -- Supply Establishment
4788      AND -- Full time
4789      p_ext_emp_cat_code = 'F' THEN
4790 
4791     debug(l_proc_name, 20);
4792     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
4793                  (p_assignment_id => p_assignment_id
4794                  ,p_error_text    => 'BEN_93655_SUPPLY_TCHR_FULLTIME'
4795                  ,p_error_number  => 93655
4796                  );
4797   END IF;
4798 
4799   debug_exit(l_proc_name);
4800 
4801 END; -- warn_if_supply_tchr_is_ft
4802 
4803 --
4804 -- Function to get the Extract Employment category code
4805 --
4806 FUNCTION get_ext_emp_cat_cd
4807   (p_assignment_id            IN      NUMBER
4808   ,p_effective_date           IN      DATE
4809   ) RETURN VARCHAR2 -- F or P
4810 IS
4811 
4812   l_asg_details         csr_asg_details_up%ROWTYPE;
4813   l_ext_emp_cat_cd      VARCHAR2(1);
4814 
4815   l_proc_name          VARCHAR2(61):=
4816      g_proc_name||'get_ext_emp_cat_cd';
4817 
4818 BEGIN
4819 
4820   debug_enter(l_proc_name);
4821   debug('p_assignment_id :'||to_char(p_assignment_id), 10);
4822   debug('p_effective_date :'||to_char(p_effective_date, 'DD/MM/YYYY'), 20);
4823 
4824   OPEN csr_asg_details_up
4825         (p_assignment_id
4826         ,p_effective_date
4827         );
4828   FETCH csr_asg_details_up INTO l_asg_details;
4829   IF csr_asg_details_up%NOTFOUND THEN
4830     -- This situation should never happen, but
4831     -- if it does, we will assume part time
4832     debug('IMP : This situation should never happen', 30);
4833     l_ext_emp_cat_cd := 'P';
4834   ELSE -- asg record FOUND
4835     -- Bugfix 3073562:GAP1:GAP2
4836     l_ext_emp_cat_cd :=
4837         get_translate_asg_emp_cat_code
4838                 (l_asg_details.asg_emp_cat_cd
4839                 ,p_effective_date
4840                 ,'Pension Extracts Employment Category Code'
4841                 ,l_asg_details.business_group_id
4842                 );
4843   END IF;
4844 
4845   CLOSE csr_asg_details_up;
4846 
4847   debug('Extract Emp Cat Code :'||l_ext_emp_cat_cd, 40);
4848   debug_exit(l_proc_name);
4849 
4850   RETURN l_ext_emp_cat_cd;
4851 EXCEPTION
4852   WHEN OTHERS THEN
4853     CLOSE csr_asg_details_up;
4854     debug('SQLCODE :'||to_char(SQLCODE), 60);
4855     debug('SQLERRM :'||SQLERRM, 70);
4856     debug_exit(' Others in '||l_proc_name);
4857     RAISE;
4858 END get_ext_emp_cat_cd;
4859 
4860 --
4861 -- Fetch and save Secondary assignments
4862 --
4863 PROCEDURE fetch_secondary_assignments
4864    (p_primary_assignment_id     IN NUMBER
4865    ,p_person_id                 IN NUMBER
4866    ,p_effective_date            IN DATE
4867    ,p_must_be_a_leaver          IN BOOLEAN
4868    )
4869 IS
4870 
4871   -- Variable Declaration
4872   l_inclusion_flag              VARCHAR2(1) := 'Y';
4873   l_leaver                      VARCHAR2(1) := 'N';
4874   l_leaver_date                 DATE := NULL;
4875   l_restarter                   VARCHAR2(1) := 'N';
4876   l_restarter_date              DATE := NULL;
4877   l_first_time                  BOOLEAN := TRUE;
4878   l_primary_asg_leaver_date     DATE := NULL;
4879   l_new_event_itr               NUMBER(5);
4880   -- LVRDATE change
4881   l_period_end_date             DATE := NULL;
4882 
4883   -- ALRDRPT change
4884   l_already_reported            VARCHAR2(1) := 'N';
4885   --PROFILING changes
4886   l_start_time                  NUMBER;
4887   l_end_time                    NUMBER;
4888 
4889   -- Rowtype Variable Declaration
4890   l_asg_details         csr_asg_details_up%ROWTYPE;
4891   l_pqp_asg_attributes  csr_pqp_asg_attributes_up%ROWTYPE;
4892   l_sec_asgs            csr_sec_assignments%ROWTYPE;
4893   l_starter_event       stored_events_type;
4894 
4895   --
4896   l_proc_name           VARCHAR2(61):=
4897      g_proc_name||'fetch_secondary_assignments';
4898 
4899 BEGIN -- fetch_secondary_assignments
4900 
4901   debug_enter(l_proc_name);
4902 
4903   debug('g_cross_per_enabled :'||g_cross_per_enabled, 10);
4904   debug('g_crossbg_enabled :'||g_crossbg_enabled, 11);
4905   debug('p_person_id :'||to_char(p_person_id), 12);
4906 
4907   -- Fetch secondary assignments and save them only if
4908   -- Teacher has elected pension.
4909   -- However, find the leaver date for all secondary asgs.
4910   FOR l_sec_asgs IN csr_sec_assignments(p_primary_assignment_id
4911                                        ,p_person_id
4912                                        ,p_effective_date
4913                                        )
4914   LOOP
4915 
4916     debug('Inside Loop, secondary asg id :'||to_char(l_sec_asgs.assignment_id),20);
4917 
4918     -- Check if the secondary assignment qualifies to be on the Periodic Returns
4919     --    Pass g_pension_year_start_date as the effective date as we are
4920     --    checking as of start date of pension year. Basically, we are
4921     --    reporting annual returns from start of pension year.
4922     l_inclusion_flag := chk_has_tchr_elected_pension
4923                         (p_business_group_id            => l_sec_asgs.business_group_id
4924                         ,p_effective_date               => g_pension_year_start_date
4925                         ,p_assignment_id                => l_sec_asgs.assignment_id
4926                         ,p_asg_details                  => l_asg_details        -- OUT
4927                         ,p_asg_attributes               => l_pqp_asg_attributes -- OUT
4928                         );
4929 
4930     IF l_inclusion_flag = 'Y' THEN
4931     -- 1)
4932 
4933       -- Check for leaver events between pension year start date and effective run date
4934       --    Basically, we are reporting annual returns starting from the
4935       --    start of the pension year to the leaver date, and we want to check
4936       --    for people who have become leavers in the same date range.
4937       --    However as we donot want to report people who have already been reported,
4938       --    we will need to look at previous run results to exclude people who
4939       --    have already been reported.
4940       -- Dates :
4941       --   Start date should be pension year start date
4942       --   End Date should be the end date of the run date range.
4943       debug('Secondary asg passes chk_has_teacher_elected_pension, doing leaver chk',30);
4944 
4945       -- LVRDATE changes
4946       -- Changed the date passed based on Annual/Periodic Criteria
4947 
4948       debug ('g_extract_type: '|| g_extract_type) ;
4949 
4950       IF g_extract_type = 'TP1' THEN
4951         l_period_end_date := g_effective_run_date;
4952       ELSIF g_extract_type = 'TP1P' THEN
4953         l_period_end_date := g_effective_run_date + 1;
4954       END IF ;
4955 
4956       debug ('l_period_end_date: '|| l_period_end_date) ;
4957 
4958       l_leaver := chk_is_teacher_a_leaver
4959                         (p_business_group_id            => l_sec_asgs.business_group_id
4960                         ,p_effective_start_date         => GREATEST(g_pension_year_start_date
4961                                                                    ,nvl(l_asg_details.start_date
4962                                                                        ,g_pension_year_start_date
4963                                                                        )
4964                                                                    )
4965                          -- LVRDATE change: changed the date passed based on Annual/Periodic Criteria
4966                         ,p_effective_end_date           => l_period_end_date
4967                         --,p_effective_end_date           => g_effective_run_date
4968                         ,p_assignment_id                => l_sec_asgs.assignment_id
4969                         ,p_leaver_date                  => l_leaver_date -- OUT
4970                         );
4971 
4972       IF l_leaver = 'Y' THEN
4973 
4974         debug('Secondary asg is leaver storing leaver date',40);
4975 
4976         -- Bugfix 3073562:GAP6
4977         -- Adding this for secondary teaching asg support.
4978         -- This check is needed as there might not be any
4979         -- primary assignment row in g_ext_asg_details if this
4980         -- is the first secondary asg being processed.
4981         IF g_ext_asg_details.EXISTS(p_primary_assignment_id)
4982            -- Bugfix 3641851:CBF3b : Only do this if primary is to be reported
4983            AND
4984            g_ext_asg_details(p_primary_assignment_id).report_asg = 'Y' THEN
4985           l_primary_asg_leaver_date := g_ext_asg_details(p_primary_assignment_id).leaver_date;
4986         ELSE
4987           l_primary_asg_leaver_date := NULL;
4988         END IF;
4989 
4990         -- Store the leaver date
4991         -- Bugfix 3734942 : We don't need to overwrite the
4992         --   sec asg leaver date with primary leaver date.
4993         --   Infact we need the actual leaver date of the
4994         --   secondary assignment
4995         l_asg_details.leaver_date := l_leaver_date;
4996 
4997       ELSE -- l_leaver = 'N'
4998 
4999         debug('Secondary asg is NOT a leaver',50);
5000 
5001         l_asg_details.leaver_date := NULL;
5002 
5003        -- Return 'N' for Periodic Report if assignment is not terminated.
5004        -- Bug 5408932
5005         IF g_extract_type = 'TP1P' THEN
5006            l_asg_details.report_asg := 'N';
5007            l_inclusion_flag := 'N';
5008         END IF;
5009 
5010         -- PER_LVR change.
5011         -- Store the minimum of latest start date, in case it is not a leaver asg.
5012         g_latest_start_date := LEAST (g_latest_start_date,l_asg_details.start_date) ;
5013 
5014       END IF;
5015 
5016       IF l_inclusion_flag = 'Y' THEN
5017       -- 2)
5018 
5019         -- Assignment has passed all checks save the details in
5020         --   1) Type 4 global collection g_ext_asg_details
5021         --   2) Type 1 global collection g_ext_asg_details
5022         --        Has more stuff than the Type 4 counterpart
5023         --   3) Type 1 global collection g_ext_aat_details
5024         --
5025 
5026         -- Even secondary asgs which are leavers are stored in the global ASG and AAT
5027         -- collections as this will be necessary at the time of creating multiple lines
5028         -- of service even in annual with exclude leavers mode.
5029 
5030         -- Check if the leaver is also a re-starter,
5031         -- i.e. there is break in service in this pension year
5032         --   But, do this only if the leaver date is present and
5033         --   less than the g_effective_run_date
5034         l_asg_details.restarter_date := NULL;
5035 
5036         IF l_leaver = 'Y'
5037            AND
5038            l_leaver_date < g_effective_run_date THEN
5039 
5040           debug('Chk if Secondary leaver is also a restarter',60);
5041 
5042           l_restarter := chk_is_leaver_a_restarter
5043                               (p_business_group_id        => l_sec_asgs.business_group_id
5044                               ,p_effective_start_date     => (l_leaver_date + 1)
5045                               -- Bugfix 3734942 Chk for restarter event to end of run period
5046                               ,p_effective_end_date       => g_effective_run_date
5047                               ,p_assignment_id            => l_sec_asgs.assignment_id
5048                               ,p_restarter_date           => l_restarter_date -- OUT
5049                               );
5050 
5051           IF l_restarter = 'Y' THEN
5052 
5053             debug('Sec. leaver is also a restarter, restarter date :'||to_char(l_restarter_date,'DDMMYY'),70);
5054 
5055             l_asg_details.restarter_date := l_restarter_date;
5056 
5057           END IF; -- l_restarter = 'Y' THEN
5058 
5059         END IF; -- l_leaver = 'Y' AND l_leaver_date < g_effective_run_date THEN
5060 
5061         debug('Storing values to globals',80);
5062 
5063         --   1) Type 4 global collection g_ext_asg_details
5064         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).person_id          := l_asg_details.person_id;
5065         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).assignment_id      := l_asg_details.assignment_id;
5066         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).start_date         := l_asg_details.start_date;
5067         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).effective_end_date := l_asg_details.effective_end_date;
5068         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).creation_date      := l_asg_details.creation_date;
5069         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).location_id        := l_asg_details.location_id;
5070         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).asg_emp_cat_cd     := l_asg_details.asg_emp_cat_cd;
5071         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).ext_emp_cat_cd     := l_asg_details.ext_emp_cat_cd;
5072 
5073         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).estb_number        :=
5074           pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number;
5075 
5076         pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id).tp_safeguarded_grade :=
5077           l_pqp_asg_attributes.tp_safeguarded_grade;
5078 
5079         --   2) Type 1 global collection g_ext_asg_details
5080 	g_teach_asg_count := g_teach_asg_count +1;
5081         g_ext_asg_details(l_sec_asgs.assignment_id) := l_asg_details;
5082 
5083         -- 3) Type 1 global collection g_ext_aat_details
5084         g_ext_asg_attributes(l_pqp_asg_attributes.assignment_id) := l_pqp_asg_attributes;
5085 
5086         -- Bugfix 3073562:GAP9a
5087         -- Raise a warning if the assignment is at a
5088         -- supply location and full time
5089         warn_if_supply_tchr_is_ft
5090           (p_assignment_id            => l_sec_asgs.assignment_id
5091           ,p_establishment_number     =>
5092               pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number
5093           ,p_ext_emp_cat_code         => l_asg_details.ext_emp_cat_cd
5094           );
5095 
5096         -- Bugfix 3073562:GAP9b
5097         -- Increment the supply asg count if this is a supply assignment
5098         IF pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number = '0966' THEN
5099           g_supply_asg_count := g_supply_asg_count + 1;
5100           debug('Incrementing supply teacher count',90);
5101         END IF;
5102 
5103         -- Bugfix 3641851:ENH6
5104         -- Increment the part time assignment count if the asg is part time
5105         -- Bugfix 3734942
5106         -- Moved the increment of part time asg count into the
5107         -- IF logic below coz we don't always want to increment
5108         -- We only increment if this assignment is the one
5109         -- to replace the teacher start date now.
5110         -- Otherwise, we just store the event and increment
5111         -- only when the event is being processed, i.e. only
5112         -- increment part time asg count when the event actually
5113         -- happens, not from the start of the period itself
5114         --IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5115         --  g_part_time_asg_count := g_part_time_asg_count + 1;
5116         --  debug('Incrementing part time assignment count',100);
5117         --END IF;
5118 
5119         -- Setting NULL before deciding if an event is needed
5120         --  for this sec asg. NULLing as there mite be an event
5121         --  stored from prvious asg
5122         l_starter_event := NULL;
5123 
5124         -- Bugfix 3073562:GAP6
5125         IF l_first_time
5126            AND -- the primary asg is not being included
5127            (NOT g_ext_asg_details.EXISTS(p_primary_assignment_id))
5128         THEN
5129 
5130           debug('First Time and Primary not being included',110);
5131           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_primary_assignment_id) :=
5132                   pqp_gb_tp_pension_extracts.g_ext_asg_details(l_sec_asgs.assignment_id);
5133 
5134           g_ext_asg_details(p_primary_assignment_id) := g_ext_asg_details(l_sec_asgs.assignment_id);
5135           g_ext_asg_details(p_primary_assignment_id).report_asg := 'N';
5136 
5137           -- Bugfix 3470242:GAP4 : We need to store the person id of the primary asg
5138           --                  as we have to update the results data for this person id
5139           --                  while creating new LOS.
5140           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_primary_assignment_id).person_id := p_person_id;
5141           g_ext_asg_details(p_primary_assignment_id).person_id := p_person_id;
5142 
5143           g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id :=
5144                   l_sec_asgs.assignment_id;
5145 
5146           g_ext_asg_attributes(p_primary_assignment_id) :=
5147                   g_ext_asg_attributes(l_pqp_asg_attributes.assignment_id);
5148 
5149           IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5150             g_part_time_asg_count := g_part_time_asg_count + 1;
5151             debug('Incrementing part time assignment count',120);
5152           END IF;
5153 
5154           -- Bugfix 3803760:TERMASG
5155           g_asg_count := g_asg_count + 1;
5156 
5157           l_first_time := FALSE;
5158 
5159         ELSIF
5160              ( -- the primary asg is present in g_ext_asg_details
5161               g_ext_asg_details.EXISTS(p_primary_assignment_id)
5162               AND
5163               -- but not to be reported coz we r only reporting on a sec asg
5164               g_ext_asg_details(p_primary_assignment_id).report_asg = 'N'
5165               ) THEN
5166 
5167           debug('Not First Time and Primary not being included',130);
5168           -- Then we need to set the sec asgs start and leaver
5169           -- dates into the primary asgs record
5170 
5171           -- Bugfix 3734942 : We need to store the events for secondary
5172           --  and primary assignments starting in the middle of the
5173           --  period or starting after the least teacher start date
5174           --  for this person as these are new LOS events
5175 
5176           IF (g_ext_asg_details(l_sec_asgs.assignment_id).start_date
5177               >
5178               g_ext_asg_details(p_primary_assignment_id).teacher_start_date
5179              ) THEN
5180 
5181             debug('Event for this secondary asg to be recorded', 140);
5182             l_starter_event.event_type := 'SECONDARY_STARTER';
5183             l_starter_event.event_date
5184                 := g_ext_asg_details(l_sec_asgs.assignment_id).start_date;
5185             l_starter_event.assignment_id := l_sec_asgs.assignment_id;
5186             IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5187               l_starter_event.pt_asg_count_change := 1;
5188             END IF;
5189 
5190             -- Bugfix 3803760:TERMASG
5191             l_starter_event.asg_count_change := 1;
5192 
5193           ELSIF (g_ext_asg_details(l_sec_asgs.assignment_id).start_date
5194                  <  -- Sec asg start date LESS than teacher start date
5195                  g_ext_asg_details(p_primary_assignment_id).teacher_start_date
5196                 ) THEN
5197 
5198             debug('Event for the other stored secondary asg to be recorded', 150);
5199             l_starter_event.event_type := 'SECONDARY_STARTER';
5200             l_starter_event.event_date
5201                 := g_ext_asg_details(p_primary_assignment_id).teacher_start_date;
5202             l_starter_event.assignment_id :=
5203                 g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id;
5204             -- If the other sec asg is PT the count needs to be incremented
5205             IF g_ext_asg_details(l_starter_event.assignment_id).ext_emp_cat_cd = 'P' THEN
5206               -- Decrement the pt asg count coz we r moving this
5207               -- asgs start date into an event to be processed later
5208               g_part_time_asg_count := g_part_time_asg_count - 1;
5209               l_starter_event.pt_asg_count_change := 1;
5210             END IF;
5211 
5212             -- Bugfix 3803760:TERMASG
5213             g_asg_count := g_asg_count - 1;
5214             l_starter_event.asg_count_change := 1;
5215 
5216             -- Bugfix 3678324 : Now only assigning secondary assingment id
5217             --    and start date to teacher start date if it is actually
5218             --    less then the already stored teacher start date
5219             --    Previously we were incorrectly storing the sec asgs
5220             --    start date in start date of primary instead of teacher_start_date
5221             --    We were also not assigning the sec asg id into primary row
5222             g_ext_asg_details(p_primary_assignment_id).teacher_start_date
5223                 := g_ext_asg_details(l_sec_asgs.assignment_id).start_date;
5224             g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id
5225                 := l_sec_asgs.assignment_id;
5226 
5227             -- Since this assignment is being stored as teacher start
5228             --  increment the pt asg count if its PT
5229             IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5230               g_part_time_asg_count := g_part_time_asg_count + 1;
5231               debug('Incrementing part time assignment count',160);
5232             END IF;
5233 
5234             -- Bugfix 3803760:TERMASG
5235             g_asg_count := g_asg_count + 1;
5236 
5237             debug('Teacher start date :'||
5238                 to_char(g_ext_asg_details(p_primary_assignment_id).teacher_start_date, 'DD/MM/YYYY')
5239                ,170);
5240           ELSE -- dates are equal
5241             debug('Dates are equal, no event needed', 180);
5242             -- But since this assignment is also on the same
5243             -- date as teacher start, we should increment the
5244             -- pt asg count if this asg is PT. BUT we do have
5245             -- a problem now. If in the next iteration
5246             -- another asg has start date LESS than teacher
5247             -- start date, then the current sec asg will get
5248             -- pushed out and an event will be stored for it.
5249             -- We then hv no way of knowing that the pt asg count
5250             -- sh be decremented more than one times coz
5251             -- two asgs had started on the same date
5252             -- But we will still increment the Pt asg count
5253             --  coz otherwise we will not get 0953 for any
5254             --  asg that has had multi PT asgs for long
5255             -- Drawbak is it will show 0953 even when
5256             --  one of the two asgs that started together
5257             --  leaver resulting in single Pt asgs, but this
5258             --  case will be very rare comparitively
5259             IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5260               g_part_time_asg_count := g_part_time_asg_count + 1;
5261               debug('Incrementing part time assignment count',175);
5262             END IF;
5263 
5264             -- Bugfix 3803760:TERMASG
5265             g_asg_count := g_asg_count + 1;
5266 
5267           END IF;
5268 
5269           -- Not sure bout leaver date logic yet, change later.
5270           --g_ext_asg_details(p_primary_assignment_id).leaver_date :=
5271           --                LEAST(g_ext_asg_details(p_primary_assignment_id).leaver_date
5272           --                     ,g_ext_asg_details(l_sec_asgs.assignment_id).leaver_date
5273           --                     );
5274 
5275         -- Bugfix 3641851:CBF1 : Added ELSE part
5276         ELSE -- Primary assignment is present and is to be reported
5277 
5278           debug('Setting teacher start date for Primary asg',190);
5279           -- Bugfix 3641851:CBF1 : Assigning least start date of primary
5280           --    teacher start date (which sh be least of primary start date
5281           --    and previous secondary asg start date, if any) and current
5282           --    sec asg start date to teacher start date of primary asg
5283 
5284           -- Bugfix 3734942 : We need to store the events for secondary
5285           --  and primary assignments starting in the middle of the
5286           --  period or starting after the least teacher start date
5287           --  for this person as these are new LOS events
5288 
5289           -- Bugfix 3678324 : Now only assigning secondary assingment id
5290           --    and start date to teacher start date if it is actually
5291           --    less then the already stored teacher start date
5292           IF g_ext_asg_details(l_sec_asgs.assignment_id).start_date
5293              <  -- Sec asg start date LESS than teacher start date
5294              g_ext_asg_details(p_primary_assignment_id).teacher_start_date THEN
5295 
5296             debug('Sec asg start date :'||
5297                    to_char(g_ext_asg_details(l_sec_asgs.assignment_id).start_date
5298                           ,'DD/MM/YYYY')
5299                  ,191);
5300             debug('Primary Tchr Start Date :'||
5301                      to_char(g_ext_asg_details(p_primary_assignment_id).teacher_start_date
5302                                       ,'DD/MM/YYYY HH24:MI')
5303                  ,191);
5304             debug('Primary Start Date :'||
5305                      to_char(g_ext_asg_details(p_primary_assignment_id).start_date
5306                                       ,'DD/MM/YYYY HH24:MI')
5307                  ,191);
5308             debug('Sec asg id on primary row :'||
5309                      to_char(g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id)
5310                  ,191);
5311             -- IF teacher start date and start date on primary row are
5312             --    same, and secondary asg id is NULL then the primary is
5313             --    being reported, push it into an event for future processing
5314             -- ELSE it will be another secondary asg being report, push
5315             --      that sec asg into an event as primary wud hv already been
5316             --      pushed into an event in a previous iteration
5317             -- Bugfix 3823873 : Changed secondary_assignment_id IS NULL to
5318             --     is equal to primary asg id
5319             --     This should b the case if this is the first time
5320             --     v r trying to assign a id to secondary_assignment_id
5321             IF ((g_ext_asg_details(p_primary_assignment_id).teacher_start_date
5322                  =
5323                  g_ext_asg_details(p_primary_assignment_id).start_date
5324                 )
5325                 AND
5326                 (g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id
5327                  =
5328                  p_primary_assignment_id
5329                 )
5330                )
5331                THEN
5332 
5333               debug('Storing PRIMARY_STARTER event',192);
5334               -- Event for the primary asg to be recorded
5335               l_starter_event.event_type := 'PRIMARY_STARTER';
5336               l_starter_event.event_date
5337                   := g_ext_asg_details(p_primary_assignment_id).start_date;
5338               l_starter_event.assignment_id := p_primary_assignment_id;
5339               IF g_ext_asg_details(p_primary_assignment_id).ext_emp_cat_cd = 'P' THEN
5340                 g_part_time_asg_count := g_part_time_asg_count - 1;
5341                 l_starter_event.pt_asg_count_change := 1;
5342               END IF;
5343 
5344               -- Bugfix 3803760:TERMASG
5345               g_asg_count := g_asg_count - 1;
5346               l_starter_event.asg_count_change := 1;
5347 
5348             ELSE
5349 
5350               debug('Storing SECONDARY_STARTER event',193);
5351               -- Event for the other stored secondary asg to be recorded
5352               l_starter_event.event_type := 'SECONDARY_STARTER';
5353               l_starter_event.event_date
5354                 := g_ext_asg_details(p_primary_assignment_id).teacher_start_date;
5355               l_starter_event.assignment_id :=
5356                 g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id;
5357               -- If the other sec asg is PT the count needs to be incremented
5358               IF g_ext_asg_details(l_starter_event.assignment_id).ext_emp_cat_cd = 'P' THEN
5359                 -- Decrement the pt asg count coz we r moving this
5360                 -- asgs start date into an event to be processed later
5361                 g_part_time_asg_count := g_part_time_asg_count - 1;
5362                 l_starter_event.pt_asg_count_change := 1;
5363               END IF;
5364 
5365               -- Bugfix 3803760:TERMASG
5366               g_asg_count := g_asg_count - 1;
5367               l_starter_event.asg_count_change := 1;
5368 
5369               debug('Stored sec asg id :'||to_char(l_starter_event.assignment_id),194);
5370 
5371 
5372             END IF;
5373 
5374             g_ext_asg_details(p_primary_assignment_id).teacher_start_date
5375                 := g_ext_asg_details(l_sec_asgs.assignment_id).start_date;
5376             g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id
5377                 := l_sec_asgs.assignment_id;
5378 
5379             -- Since this assignment is being stored as teacher start
5380             --  increment the pt asg count if its PT
5381             IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5382               g_part_time_asg_count := g_part_time_asg_count + 1;
5383               debug('Incrementing part time assignment count',200);
5384             END IF;
5385 
5386             -- Bugfix 3803760:TERMASG
5387             g_asg_count := g_asg_count + 1;
5388 
5389             debug('Teacher start date :'||
5390                 to_char(g_ext_asg_details(p_primary_assignment_id).teacher_start_date, 'DD/MM/YYYY')
5391                ,210);
5392 
5393           ELSIF g_ext_asg_details(l_sec_asgs.assignment_id).start_date
5394                 >  -- Sec asg start date GREATER than teacher start date
5395                 g_ext_asg_details(p_primary_assignment_id).teacher_start_date THEN
5396 
5397             debug('Event for this secondary asg to be recorded', 220);
5398             l_starter_event.event_type := 'SECONDARY_STARTER';
5399             l_starter_event.event_date
5400                 := g_ext_asg_details(l_sec_asgs.assignment_id).start_date;
5401             l_starter_event.assignment_id := l_sec_asgs.assignment_id;
5402             IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5403               l_starter_event.pt_asg_count_change := 1;
5404             END IF;
5405 
5406             -- Bugfix 3803760:TERMASG
5407             l_starter_event.asg_count_change := 1;
5408 
5409           ELSE -- dates are equal
5410             debug('Dates are equal, no event needed', 230);
5411             -- But since this assignment is also on the same
5412             -- date as teacher start, we should increment the
5413             -- pt asg count if this asg is PT. BUT we do have
5414             -- a problem now. If in the next iteration
5415             -- another asg has start date LESS than teacher
5416             -- start date, then the current sec asg will get
5417             -- pushed out and an event will be stored for it.
5418             -- We then hv no way of knowing that the pt asg count
5419             -- sh be decremented more than one times coz
5420             -- two asgs had started on the same date
5421             -- But we will still increment the Pt asg count
5422             --  coz otherwise we will not get 0953 for any
5423             --  asg that has had multi PT asgs for long
5424             -- Drawbak is it will show 0953 even when
5425             --  one of the two asgs that started together
5426             --  leaver resulting in single Pt asgs, but this
5427             --  case will be very rare comparitively
5428             IF l_asg_details.ext_emp_cat_cd = 'P' THEN
5429               g_part_time_asg_count := g_part_time_asg_count + 1;
5430               debug('Incrementing part time assignment count',175);
5431             END IF;
5432 
5433             -- Bugfix 3803760:TERMASG
5434             g_asg_count := g_asg_count + 1;
5435 
5436           END IF;
5437 
5438         END IF; -- IF l_first_time
5439 
5440         debug('Teacher start date :'||
5441                 to_char(g_ext_asg_details(p_primary_assignment_id).teacher_start_date, 'DD/MM/YYYY')
5442                ,240);
5443 
5444         -- Store the starter event as a new line of service event
5445         IF l_starter_event.event_date IS NOT NULL THEN
5446           debug('Storing the starter event', 250);
5447           l_new_event_itr := g_asg_events.COUNT+1;
5448           g_asg_events(l_new_event_itr) := l_starter_event;
5449         END IF;
5450 
5451       END IF; -- 2) l_inclusion_flag = 'Y' THEN
5452       --
5453     END IF; -- 1) l_inclusion_flag = 'Y' THEN
5454 
5455     -- Reset local variables to default values
5456     -- before processing next secondary assingnment
5457     l_inclusion_flag    := 'Y';
5458     l_leaver            := 'N';
5459     l_leaver_date       := NULL;
5460 
5461   END LOOP; -- l_sec_asg_details IN csr_sec_asg_details
5462 
5463   debug_exit(l_proc_name);
5464   --
5465   RETURN;
5466   --
5467 EXCEPTION
5468   WHEN OTHERS THEN
5469     debug_exit(' Others in '||l_proc_name);
5470     RAISE;
5471 END; -- fetch_secondary_assignments
5472 
5473 --
5474 -- Return secondary assignments that are effective and in the future
5475 --
5476 FUNCTION get_all_secondary_asgs
5477    (p_primary_assignment_id     IN NUMBER
5478    ,p_effective_date            IN DATE
5479    ) RETURN t_sec_asgs_type
5480 IS
5481 
5482   -- Rowtype Variable Declaration
5483   l_sec_asgs            csr_sec_assignments%ROWTYPE;
5484   l_all_sec_asgs        t_sec_asgs_type;
5485   --
5486   l_proc_name          VARCHAR2(61):=
5487      g_proc_name||'get_all_secondary_asgs';
5488 
5489 BEGIN -- get_all_secondary_asgs
5490 
5491   debug_enter(l_proc_name);
5492 
5493   -- Fetch secondary assignments and save them only if
5494   -- Teacher has elected pension.
5495   -- However, find the leaver date for all secondary asgs.
5496   FOR l_sec_asgs IN csr_sec_assignments(p_primary_assignment_id
5497                                        ,g_ext_asg_details(p_primary_assignment_id).person_id
5498                                        ,p_effective_date
5499                                        )
5500   LOOP
5501 
5502     -- If the secondary assignment is part of the global ASG collection
5503     IF g_ext_asg_details.EXISTS(l_sec_asgs.assignment_id) THEN
5504 
5505       -- Add this to the table of valid secondary asgs
5506       l_all_sec_asgs(l_sec_asgs.assignment_id) := l_sec_asgs;
5507 
5508     END IF; -- g_ext_asg_details.EXISTS(l_sec_asgs.assignment_id) THEN
5509     --
5510   END LOOP; -- l_sec_asg_details IN csr_sec_asg_details
5511 
5512   debug_exit(l_proc_name);
5513   --
5514   RETURN l_all_sec_asgs;
5515   --
5516 EXCEPTION
5517   WHEN OTHERS THEN
5518     debug_exit(' Others in '||l_proc_name);
5519     RAISE;
5520 END; -- get_all_secondary_asgs
5521 
5522 --
5523 -- Return secondary assignments that are effective only
5524 --
5525 FUNCTION get_eff_secondary_asgs
5526    (p_primary_assignment_id     IN NUMBER
5527    ,p_effective_date            IN DATE
5528    ) RETURN t_sec_asgs_type
5529 IS
5530 
5531   -- Rowtype Variable Declaration
5532   l_sec_asgs            csr_eff_sec_assignments%ROWTYPE;
5533   l_eff_sec_asgs        t_sec_asgs_type;
5534   --
5535   l_proc_name          VARCHAR2(61):=
5536      g_proc_name||'get_eff_secondary_asgs';
5537 
5538 BEGIN -- get_eff_secondary_asgs
5539 
5540   debug_enter(l_proc_name);
5541   debug('p_primary_assignment_id ' || p_primary_assignment_id, 10) ;
5542   debug('p_effective_date ' || p_effective_date, 20) ;
5543   debug('g_effective_run_date ' || g_effective_run_date, 30) ;
5544 
5545 
5546   -- Fetch secondary assignments and save them only if
5547   -- Teacher has elected pension.
5548   -- However, find the leaver date for all secondary asgs.
5549   FOR l_sec_asgs IN csr_eff_sec_assignments(p_primary_assignment_id
5550                                            ,g_ext_asg_details(p_primary_assignment_id).person_id
5551                                            ,p_effective_date
5552                                            )
5553   LOOP
5554     debug('l_sec_asgs.assignment_id ' || l_sec_asgs.assignment_id, 50) ;
5555     -- If the secondary assignment is part of the global ASG collection
5556     IF g_ext_asg_details.EXISTS(l_sec_asgs.assignment_id) THEN
5557 
5558       debug(l_proc_name,60);
5559       debug('teacher_start_date ' || g_ext_asg_details(l_sec_asgs.assignment_id).teacher_start_date) ;
5560       debug('leaver_date ' || g_ext_asg_details(l_sec_asgs.assignment_id).leaver_date) ;
5561       debug('restarter_date ' || g_ext_asg_details(l_sec_asgs.assignment_id).restarter_date) ;
5562 
5563       -- MULT-LR --
5564       -- Use the new Function to check the effectivness of an assignment
5565       -- it takes care of multiple Leaver-Restarter events
5566       -- where as the old logic used to take into account
5567       -- only the first restarter event.
5568       IF ( chk_effective_asg ( p_assignment_id  => l_sec_asgs.assignment_id
5569                               ,p_effective_date => p_effective_date
5570                              )  = 'Y' ) THEN
5571         debug(l_proc_name,70);
5572         -- Add this to the table of valid secondary asgs
5573         l_eff_sec_asgs(l_sec_asgs.assignment_id) := l_sec_asgs;
5574 
5575       END IF ;  --Valid Tchr in the period
5576 
5577     END IF; -- g_ext_asg_details.EXISTS(l_sec_asgs.assignment_id) THEN
5578     --
5579   END LOOP; -- l_sec_asg_details IN csr_sec_asg_details
5580 
5581   debug_exit(l_proc_name);
5582   --
5583   RETURN l_eff_sec_asgs;
5584   --
5585 EXCEPTION
5586   WHEN OTHERS THEN
5587     debug_exit(' Others in '||l_proc_name);
5588     RAISE;
5589 END; -- get_eff_secondary_asgs
5590 
5591 --
5592 -- set_effective_assignments
5593 --
5594 -- Bugfix 3803760:FTSUPPLY
5595 -- New Function to check all assignments for effectiveness
5596 -- Purpose : Chcks all assignments for effectiveness and
5597 --    do the following considering the new rule where
5598 --    if any assignment is FT, only process that assignment
5599 --    and leave out the other assignments
5600 -- 1) If primary assignment is effecitve and FT, set it as
5601 --    g_override_ft_asg_id and do not get secondary assignments
5602 -- 2) If primary not FT, then call get_eff_secondary_asgs.
5603 -- 3) If any effective secondary is FT, set it as override and
5604 --    add to g_tab_sec_asgs
5605 -- 4) If no secondary is FT, store all effective secondary
5606 --    asgs in g_tab_sec_asgs
5607 --
5608 PROCEDURE set_effective_assignments
5609    (p_primary_assignment_id     IN NUMBER
5610    ,p_effective_date            IN DATE
5611    )
5612 IS
5613 
5614   -- Rowtype Variable Declaration
5615   l_sec_asgs            csr_eff_sec_assignments%ROWTYPE;
5616   l_sec_asg_id          per_all_assignments_f.assignment_id%TYPE;
5617   l_tab_eff_sec_asgs    t_sec_asgs_type;
5618   l_rep_primary_asg     VARCHAR2(1);
5619   l_prev_asg_id         per_all_assignments_f.assignment_id%TYPE;
5620 
5621   l_asg_details         csr_asg_details_up%ROWTYPE;
5622 
5623   --
5624   l_proc_name          VARCHAR2(61):=
5625      g_proc_name||'set_effective_assignments';
5626 
5627 BEGIN
5628 
5629   debug_enter(l_proc_name);
5630   debug('p_primary_assignment_id ' || p_primary_assignment_id, 10) ;
5631   debug('p_effective_date ' || p_effective_date, 20) ;
5632   debug('g_effective_run_date ' || g_effective_run_date, 30) ;
5633 
5634   g_override_ft_asg_id := NULL;
5635   g_tab_sec_asgs.DELETE;
5636 
5637   -- Bugfix 3880543:REHIRE : Done this as we need to nominate one
5638   --  sec asg for reporting if primary is not valid
5639 
5640   -- MULT-LR --
5641   -- Use the new Function to check the effectivness of an assignment
5642   -- it takes care of multiple Leaver-Restarter events
5643   -- where as the old logic used to take into account
5644   -- only the first restarter event.
5645   l_rep_primary_asg := chk_effective_asg (
5646                            p_assignment_id  => p_primary_assignment_id
5647                           ,p_effective_date => p_effective_date
5648                                           );
5649 
5650   IF (l_rep_primary_asg = 'Y'
5651      )
5652      AND
5653      (get_ext_emp_cat_cd
5654          (p_assignment_id  => p_primary_assignment_id
5655          ,p_effective_date => p_effective_date
5656          ) = 'F'
5657      ) THEN
5658 
5659     g_override_ft_asg_id := p_primary_assignment_id;
5660     debug('Primary asg is FT, setting as override', 40);
5661 
5662   ELSE -- check the secondary assignments
5663 
5664     debug('Getting effective sec asgs', 50);
5665     l_tab_eff_sec_asgs := get_eff_secondary_asgs
5666                            (p_primary_assignment_id  => p_primary_assignment_id
5667                            ,p_effective_date         => p_effective_date
5668                            );
5669 
5670     -- Bugfix 3880543:REHIRE : We are now setting the first sec asg id
5671     --  as the secondary_assignment in row for the primary.
5672     --  And then the 2nd sec asg id in as secondary_assignment
5673     --  in the row for 1st sec asg and so on
5674     l_prev_asg_id := p_primary_assignment_id;
5675     l_sec_asg_id := l_tab_eff_sec_asgs.FIRST;
5676     WHILE l_sec_asg_id IS NOT NULL
5677     LOOP
5678 
5679       debug('Processing sec asg id :'||to_char(l_sec_asg_id), 60);
5680       IF get_ext_emp_cat_cd
5681           (p_assignment_id  => l_sec_asg_id
5682           ,p_effective_date => p_effective_date
5683           ) = 'F' -- Full time
5684           THEN
5685 
5686         debug('This sec asg is FT, setting as override', 70);
5687 
5688         g_tab_sec_asgs.DELETE;
5689         g_tab_sec_asgs(l_sec_asg_id) := l_tab_eff_sec_asgs(l_sec_asg_id);
5690         g_override_ft_asg_id := l_sec_asg_id;
5691 
5692         -- Bugfix 3880543:REHIRE : Need to nomindate this sec for reporting
5693         --  coz its the FT asg
5694         g_ext_asg_details(p_primary_assignment_id).secondary_assignment_id
5695                 := g_override_ft_asg_id;
5696         EXIT;
5697 
5698       ELSE
5699 
5700         debug('Adding this sec asg to global collection', 80);
5701         g_tab_sec_asgs(l_sec_asg_id) := l_tab_eff_sec_asgs(l_sec_asg_id);
5702 
5703         -- Bugfix 3880543:REHIRE : Need to nominate one sec for reporting if
5704         --  primary OR the current secondary is not to be reported,
5705         --  The first secondary asg will b stored in secondary_assignment_id
5706         --  in the row for primary asg
5707         --  In the future if needed we can add logic to choose the
5708         --  secondary asg based on some criteria
5709         g_ext_asg_details(l_prev_asg_id).secondary_assignment_id := l_sec_asg_id;
5710 
5711       END IF;
5712 
5713       -- Set the current as previous
5714       l_prev_asg_id := l_sec_asg_id;
5715 
5716       -- Get the next secondary assignment
5717       l_sec_asg_id := l_tab_eff_sec_asgs.NEXT(l_sec_asg_id);
5718 
5719     END LOOP;
5720     debug('Total eff sec asgs :'||to_char(g_tab_sec_asgs.COUNT), 90);
5721 
5722   END IF; -- (l_rep_primary_asg
5723 
5724   debug('Override FT Assignment ID :'||to_char(nvl(g_override_ft_asg_id, -1)), 100);
5725 
5726   -- If the override asg was found re-evaluate the asg details
5727   -- as of effective date and store in g_ext_asg_details
5728   IF g_override_ft_asg_id IS NOT NULL
5729      AND
5730      -- If effective date is > teacher start date
5731      -- this sh b for a new line, we should refresh
5732      -- some columns on g_ext_asg_details for the override
5733      -- asg as new values need to be used. For the first
5734      -- line, the values should be current
5735      p_effective_date > g_ext_asg_details(p_primary_assignment_id).teacher_start_date THEN
5736 
5737     OPEN csr_asg_details_up
5738           (g_override_ft_asg_id
5739           ,p_effective_date
5740           );
5741     FETCH csr_asg_details_up INTO l_asg_details;
5742     IF csr_asg_details_up%NOTFOUND THEN
5743       -- This situation should never happen,
5744       debug('IMP : This situation should never happen', 100);
5745       NULL;
5746     ELSE -- asg record FOUND
5747 
5748       IF l_asg_details.location_id IS NOT NULL
5749          AND
5750          pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_asg_details.location_id) THEN
5751 
5752 
5753         debug('Re-evaluating override asg details', 110);
5754         -- Setting the current ext_emp_cat_cd, location_id and estb_number
5755         g_ext_asg_details(g_override_ft_asg_id).ext_emp_cat_cd := 'F';
5756         debug('l_asg_details.location_id :'||to_char(l_asg_details.location_id), 111);
5757         g_ext_asg_details(g_override_ft_asg_id).location_id := l_asg_details.location_id;
5758         debug('g_ext_asg_details(g_override_ft_asg_id).estb_number :'||g_ext_asg_details(g_override_ft_asg_id).estb_number, 111);
5759         debug('Estb number in Global :'||pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number, 112);
5760         g_ext_asg_details(g_override_ft_asg_id).estb_number :=
5761           pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number;
5762 
5763       ELSE
5764         debug('WARNING: This asg might hv multiple leaver events', 113);
5765       END IF;
5766 
5767     END IF;
5768     debug('After IF Asg_Record_Found ' , 114);
5769 
5770     CLOSE csr_asg_details_up;
5771 
5772   END IF; -- g_override_ft_asg_id IS NOT NULL
5773 
5774   debug_exit(l_proc_name);
5775   --
5776   RETURN;
5777   --
5778 EXCEPTION
5779   WHEN OTHERS THEN
5780     debug('SQLCODE :'||to_char(SQLCODE), 150);
5781     debug('SQLERRM :'||SQLERRM, 160);
5782     debug_exit(' Others in '||l_proc_name);
5783     RAISE;
5784 END; -- set_effective_assignments
5785 
5786 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5787 -- ~ To get the corresponding Pension Extract Emp Category code  ~
5788 -- ~ and Pension Extract Working Pattern Code                    ~
5789 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5790 Function Get_Translate_Asg_Emp_Cat_Code (p_asg_emp_cat_cd   in varchar2
5791                                         ,p_effective_date   in Date
5792                                         ,p_udt_column_name  in varchar2
5793                                         ,p_business_group_id IN NUMBER
5794                                         ) Return Varchar2  Is
5795 
5796  l_proc_name          varchar2(70):= g_proc_name||'Get_Translate_Asg_Emp_Cat_Code';
5797  l_udt_value VARCHAR2(1):= '?';
5798 
5799  Cursor csr_get_emp_cat_code (c_effective_date    in date
5800                              ,c_asg_emp_cat_cd    in varchar2
5801                              ,c_udt_column_name   in varchar2
5802                              ,c_business_group_id in number
5803                              ,c_legislation_code  in varchar2)  Is
5804   Select  extv.value,extv.business_group_id
5805     From   pay_user_tables   tbls
5806           ,pay_user_columns asgc
5807           ,pay_user_columns extc
5808           ,pay_user_rows_f  urws
5809           ,pay_user_column_instances_f asgv
5810           ,pay_user_column_instances_f extv
5811    where  tbls.user_table_name ='PQP_GB_TP_EMPLOYMENT_CATEGORY_TRANSALATION_TABLE'
5812      and  asgc.user_table_id = tbls.user_table_id
5813      and  extc.user_table_id = tbls.user_table_id
5814      and  asgc.user_column_name = 'Assignment Employment Category Lookup Code'
5815      and  extc.user_column_name = c_udt_column_name
5816      and  urws.user_table_id = tbls.user_table_id
5817      and  (urws.business_group_id = c_business_group_id
5818             OR
5819              (urws.business_group_id IS NULL
5820               AND urws.legislation_code = c_legislation_code)
5821             OR
5822              (urws.business_group_id IS NULL AND urws.legislation_code IS NULL)
5823             )
5824      and  c_effective_date BETWEEN urws.effective_start_date
5825                                AND urws.effective_end_date
5826      and  asgv.user_column_id = asgc.user_column_id
5827      and  c_effective_date BETWEEN asgv.effective_start_date
5828                                AND asgv.effective_end_date
5829      and  extv.user_column_id = extc.user_column_id
5830      and  c_effective_date BETWEEN extv.effective_start_date
5831                                AND extv.effective_end_date
5832      and  asgv.user_row_id = urws.user_row_id
5833      and  extv.user_row_id = asgv.user_row_id
5834      and  asgv.value = c_asg_emp_cat_cd;
5835 
5836 Begin
5837     debug_enter(l_proc_name);
5838     debug('c_effective_date : '|| to_char(NVL(p_effective_date,g_effective_date)),10);
5839     debug('p_asg_emp_cat_cd : '|| p_asg_emp_cat_cd);
5840     debug('p_udt_column_name : '|| p_udt_column_name);
5841 
5842 -- Changed the procedure to return business group level values if values are defined both at Legislation
5843 -- and Business Group Level Bug 5498514
5844     FOR l_idx in csr_get_emp_cat_code (c_effective_date    =>  NVL(p_effective_date,g_effective_date)
5845 			      ,c_asg_emp_cat_cd    =>  p_asg_emp_cat_cd
5846 			      ,c_udt_column_name   =>  p_udt_column_name
5847 			      ,c_business_group_id =>  p_business_group_id
5848 			      ,c_legislation_code  =>  g_legislation_code
5849 			       ) LOOP
5850   --  Fetch csr_get_emp_cat_code Into l_udt_value;
5851 
5852     -- Added by Babu and Raju as a fix for Invalid Emp Cat warning
5853     -- Date : 10/04/2002
5854     -- Assigns value if leg level data and no bg level data exists
5855     -- If bg level data exists always use the same
5856       IF (l_idx.business_group_id IS NULL AND l_udt_value = '?') OR
5857 	 (l_idx.business_group_id IS NOT NULL AND l_idx.value IS NOT NULL) THEN
5858 	  l_udt_value :=  l_idx.value;
5859       END IF;
5860     END LOOP;
5861 
5862     IF l_udt_value = '?' THEN
5863       l_udt_value := NULL;
5864     END IF;
5865 
5866 --    END LOOP;
5867  --   Close csr_get_emp_cat_code;
5868 
5869     debug('Return Value :'||l_udt_value, 20);
5870     debug_exit(l_proc_name);
5871     RETURN l_udt_value;
5872 
5873 Exception
5874     When No_Data_Found Then
5875      --debug('No Data Found in Translate UDT');
5876       --debug_exit;
5877       debug_exit(' No Data Found in '||l_proc_name);
5878       l_udt_value := NULL;
5879     Return l_udt_value;
5880   WHEN OTHERS THEN
5881     debug_exit(' Others in '||l_proc_name);
5882     RAISE;
5883 End Get_Translate_Asg_Emp_Cat_Code;
5884 
5885 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5886 -- ~ Function returns the special class rules for teachers, based on emp category code ~
5887 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5888 Function Get_Special_ClassRule ( p_assignment_id  in number
5889                                 ,p_effective_date in date
5890                                 ) Return varchar2 Is
5891 
5892  Cursor csr_get_empcat ( c_assignment_id  in number
5893                         ,c_effective_date in date ) Is
5894   Select paa.employment_category
5895     from per_all_assignments_f paa
5896    where paa.assignment_id = c_assignment_id
5897      and c_effective_date between paa.effective_start_date
5898                               and paa.effective_end_date;
5899  l_assig_emp_catcode     per_all_assignments_f.employment_category%TYPE;
5900  l_pension_ext_empcode   per_all_assignments_f.employment_category%TYPE;
5901  l_pension_ext_wrkpat    per_all_assignments_f.employment_category%TYPE;
5902  l_return_value          varchar2(7) :=' ';
5903  l_error_value           number;
5904  l_proc_name          varchar2(70):= g_proc_name||'Get_Special_ClassRule';
5905 
5906 Begin
5907      debug_enter(l_proc_name);
5908      Open csr_get_empcat ( c_assignment_id  => p_assignment_id
5909                           ,c_effective_date => p_effective_date);
5910      Fetch csr_get_empcat Into l_assig_emp_catcode;
5911      If csr_get_empcat%NOTFOUND Then
5912         Close csr_get_empcat;
5913         l_return_value := 'UNKNOWN';
5914      Else
5915         Close csr_get_empcat;
5916 
5917        debug('Get the Pension Ext EmpCat and WrkPat codes', 10);
5918 
5919        -- Bugfix 3073562:GAP1:GAP2
5920        -- Only use cached copies if the assignment is frm
5921        -- the current BG, otherwise fetch from DB
5922        If g_ext_asg_details(p_assignment_id).business_group_id = g_business_group_id
5923           AND
5924           l_assig_emp_catcode = g_asg_emp_cat_cd
5925           And
5926           g_ext_emp_cat_cd Is Not Null Then
5927 
5928          l_pension_ext_empcode := g_ext_emp_cat_cd;
5929          l_pension_ext_wrkpat  := g_ext_emp_wrkp_cd;
5930 
5931        Else
5932           l_pension_ext_empcode := Get_Translate_Asg_Emp_Cat_Code
5933                                     (p_asg_emp_cat_cd  => l_assig_emp_catcode
5934                                     ,p_effective_date  => p_effective_date
5935                                     ,p_udt_column_name => 'Pension Extracts Employment Category Code'
5936                                     ,p_business_group_id =>
5937                                         g_ext_asg_details(p_assignment_id).business_group_id
5938                                     );
5939           l_pension_ext_wrkpat  := Get_Translate_Asg_Emp_Cat_Code
5940                                     (p_asg_emp_cat_cd  => l_assig_emp_catcode
5941                                     ,p_effective_date  => p_effective_date
5942                                     ,p_udt_column_name => 'Pension Extracts Working Pattern Code'
5943                                     ,p_business_group_id =>
5944                                         g_ext_asg_details(p_assignment_id).business_group_id
5945                                     );
5946 
5947           l_pension_ext_empcode := nvl(l_pension_ext_empcode,'F');
5948           l_pension_ext_wrkpat := nvl(l_pension_ext_wrkpat,'R');
5949 
5950           -- Bugfix 3073562:GAP1:GAP2
5951           -- Update the globals with new values only for current BG
5952           IF g_ext_asg_details(p_assignment_id).business_group_id = g_business_group_id THEN
5953             debug(l_proc_name, 20);
5954             g_asg_emp_cat_cd  := l_assig_emp_catcode;
5955             g_ext_emp_cat_cd  := l_pension_ext_empcode;
5956             g_ext_emp_wrkp_cd := l_pension_ext_wrkpat;
5957           END IF;
5958        End If;
5959      End If;
5960 
5961      debug('Check the conditions', 30);
5962 
5963      If  l_pension_ext_empcode Is Not Null And
5964          l_pension_ext_empcode ='F' Then
5965          l_return_value := '0';
5966      Elsif l_pension_ext_empcode ='P' And
5967            l_pension_ext_wrkpat Is Not Null Then
5968 
5969            If l_pension_ext_wrkpat ='R' Then
5970               l_return_value := '7';
5971            Elsif l_pension_ext_wrkpat ='T' Then
5972               l_return_value := '8';
5973            Else
5974               l_return_value := 'INVALID';
5975            End If;
5976      Else
5977       l_return_value := 'INVALID';
5978 
5979      End If;
5980      debug('Return Value :'||l_return_value, 40);
5981      debug_exit(l_proc_name);
5982      Return l_return_value;
5983 EXCEPTION
5984   WHEN OTHERS THEN
5985     debug_exit(' Others in '||l_proc_name);
5986     RAISE;
5987 End Get_Special_ClassRule;
5988 
5989 Function Get_Allowance_Code ( p_assignment_id   in number
5990                              ,p_effective_date  in date
5991                              ,p_allowance_type  in varchar2 ) Return varchar2 Is
5992 --
5993 -- Cursor to get the element_type_id
5994 --
5995 Cursor csr_ele_type_id ( c_element_name   in varchar2
5996                         ,c_effective_date in date ) Is
5997  Select pet.element_type_id
5998    from pay_element_types_f pet
5999   where c_effective_date between pet.effective_start_date
6000                              and pet.effective_end_date
6001     and pet.element_name = c_element_name;
6002 --
6003 -- Cursor to check if the element exits in element entries
6004 --
6005 Cursor csr_ele_entries ( c_assignment_id   in number
6006                         ,c_effective_date  in date
6007                         ,c_element_type_id in number ) Is
6008  Select  pee.element_entry_id
6009    from  pay_element_entries_f pee
6010         ,pay_element_links_f   pel
6011   where  pee.assignment_id   = c_assignment_id
6012     and  pel.element_link_id = pee.element_link_id
6013     and  pel.element_type_id = c_element_type_id
6014     and  c_effective_date between pee.effective_start_date
6015                               and pee.effective_end_date
6016     and  c_effective_date between pel.effective_start_date
6017                               and pel.effective_end_date;
6018 
6019 Type t_allowance_code Is Table of varchar2(1) Index By BINARY_INTEGER;
6020 
6021 l_udt_allowance_code t_allowance_code;
6022 l_allowance_code     varchar2(1);
6023 l_element_name       pay_element_types_f.element_name%TYPE;
6024 l_input_value_name   pay_input_values_f.name%TYPE;
6025 l_element_type_id    pay_element_types_f.element_type_id%TYPE;
6026 l_element_entry_id   pay_element_entries_f.element_entry_id%TYPE;
6027 l_count_ele_entries  Number;
6028 l_return_value       char(7) := ' ';
6029 l_error_value        number;
6030 l_proc_name          varchar2(60) := g_proc_name || 'Get_Allowance_Code';
6031 
6032 -- 4336613 : LARP_SPAP_3A : new local variable to hold count of LARP/SPAP entries in UDT
6033 l_larp_spap_count    NUMBER;
6034 
6035 
6036 Begin
6037      -- hr_utility.set_location('Entering: '||l_proc_name, 5);
6038      debug_enter(l_proc_name);
6039      debug('p_allowance_type :'||p_allowance_type, 10);
6040      l_count_ele_entries := 0;
6041      For i in 1..4 Loop
6042         l_udt_allowance_code(i) := Null;
6043      End Loop;
6044      --
6045      -- check if the assignment has one or more element entry as defined in the UDT
6046      --
6047 
6048 
6049      -- 4336613 : LARP_SPAP_3A : depending on LARP/SPAP entries being considered,
6050      -- assign number of entried to l_larp_spap_count to be used in for loop
6051      -- incase none are found, raise a warning (instead of an error as was being
6052      -- done earlier)
6053      IF p_allowance_type = 'LONDON_ALLOWANCE_RULE' THEN
6054        l_larp_spap_count := g_udt_element_LondAll.COUNT;
6055      ELSIF p_allowance_type = 'SPECIAL_ALLOWANCE_RULE' THEN
6056        l_larp_spap_count := g_udt_element_SpcAll.COUNT;
6057      ELSE
6058        l_error_value := pqp_gb_tp_extract_functions.raise_extract_error
6059                          (p_business_group_id => g_business_group_id
6060                          ,p_assignment_id     => p_assignment_id
6061                          ,p_error_text        =>'BEN_93024_EXT_TP1_INVALID_ALOW'
6062                          ,p_error_number      => 93024 );
6063      END IF;
6064 
6065 
6066      For i_idx in 1..l_larp_spap_count Loop -- 4336613 : LARP_SPAP_3A : now from 1 to
6067                                             -- l_larp_spap instead of 1 to 4
6068           debug('Idx :'||to_char(i_idx), 20);
6069         If p_allowance_type = 'LONDON_ALLOWANCE_RULE' Then
6070            l_allowance_code   := g_udt_element_LondAll(i_idx).allowance_code;
6071            l_element_name     := g_udt_element_LondAll(i_idx).element_name;
6072            l_input_value_name := g_udt_element_LondAll(i_idx).input_value_name;
6073         Elsif p_allowance_type = 'SPECIAL_ALLOWANCE_RULE' Then
6074            l_allowance_code   := g_udt_element_SpcAll(i_idx).allowance_code;
6075            l_element_name     := g_udt_element_SpcAll(i_idx).element_name;
6076            l_input_value_name := g_udt_element_SpcAll(i_idx).input_value_name;
6077 
6078         End if;
6079         debug('Checking if : '||l_element_name ||' is defined', 30);
6080         Open csr_ele_type_id ( c_element_name   => l_element_name
6081                               ,c_effective_date => p_effective_date);
6082         Fetch csr_ele_type_id Into l_element_type_id;
6083 
6084         If csr_ele_type_id%NOTFOUND Then
6085            Close csr_ele_type_id;
6086            l_error_value := pqp_gb_tp_extract_functions.raise_extract_error
6087                            (p_business_group_id => g_business_group_id
6088                            ,p_assignment_id     => p_assignment_id
6089                            ,p_error_text        => 'BEN_93026_EXT_TP1_ELE_NOTEXIST'
6090                            ,p_error_number      => 93026
6091                            ,p_token1            => l_element_name
6092                            );
6093 
6094         Else
6095            Close csr_ele_type_id;
6096            debug('Checking for : '||l_element_name||' in element entries', 40);
6097            Open csr_ele_entries ( c_assignment_id   => p_assignment_id
6098                                  ,c_effective_date  => p_effective_date
6099                                  ,c_element_type_id => l_element_type_id);
6100            Fetch csr_ele_entries Into l_element_entry_id;
6101            If csr_ele_entries%FOUND Then
6102               -- Check to see if their are multiple entries for the element
6103               -- for the same pay period; if found raise error
6104               debug(l_proc_name, 50);
6105               l_count_ele_entries := l_count_ele_entries + 1;
6106               Fetch csr_ele_entries Into l_element_entry_id;
6107               If csr_ele_entries%FOUND Then
6108                  debug('More than one entry found for element :'||l_element_name, 60);
6109                  l_count_ele_entries := l_count_ele_entries + 1;
6110               Else
6111                  debug('Idx :'||to_char(i_idx)||' l_allowance_code :'||l_allowance_code, 70);
6112                  l_udt_allowance_code(i_idx):= l_allowance_code;
6113               End If;
6114            Else
6115               debug('Setting NULL', 80);
6116               l_udt_allowance_code(i_idx):= Null;
6117            End If;
6118                Close csr_ele_entries;
6119         End If;
6120         -- If the assignment has more than one element then exit and raise error
6121         Exit When l_count_ele_entries > 1;
6122 
6123      End Loop;
6124      debug(l_proc_name, 90);
6125 
6126      If l_count_ele_entries > 1 Then
6127         l_return_value := 'TOOMANY';
6128      Elsif l_count_ele_entries = 0 Then
6129         l_return_value := 'UNKNOWN';
6130      Else
6131          For i in 1..4 Loop
6132            debug(l_proc_name, 100);
6133            If l_udt_allowance_code(i) Is Not Null Then
6134               l_return_value := l_udt_allowance_code(i);
6135               Exit;
6136            End If;
6137          End Loop;
6138      End If;
6139      debug('Return value :'||l_return_value, 110);
6140      debug_exit(l_proc_name);
6141      -- hr_utility.set_location('Leaving: '||l_proc_name, 15);
6142 
6143      Return l_return_value;
6144 EXCEPTION
6145   WHEN OTHERS THEN
6146     debug_exit(' Others in '||l_proc_name);
6147     RAISE;
6148 End Get_Allowance_Code;
6149 
6150 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6151 -- ~ Function checks the Safeguarded Grade and Fast Track Indicator ~
6152 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6153 Function Get_Grade_Fasttrack_Info (p_assignment_id   In    number
6154                                   ,p_effective_date  In    date) Return char Is
6155 
6156 Cursor csr_get_aat_info (c_assignment_id  In number
6157                         ,c_effective_date In date ) Is
6158  select assignment_attribute_id
6159        ,assignment_id
6160        ,tp_is_teacher
6161        ,tp_safeguarded_grade
6162        ,tp_fast_track
6163        ,tp_elected_pension
6164        -- added the new column for the new safeguarded logic based on safeguarded rate type
6165        ,tp_safeguarded_rate_type
6166    from pqp_assignment_attributes_f
6167   where  assignment_id = c_assignment_id
6168     and  c_effective_date between effective_start_date
6169                               and effective_end_date
6170   order by effective_start_date;
6171 
6172  l_proc_name     varchar2(60) := g_proc_name || 'Get_Grade_Fasttrack_Info';
6173  l_aat_info      csr_get_aat_info%rowtype;
6174  l_return_value  char(1):= ' ';
6175 Begin
6176   -- hr_utility.set_location('Entering: '||l_proc_name, 5);
6177   debug_enter(l_proc_name);
6178   Open csr_get_aat_info
6179     (c_assignment_id  => p_assignment_id
6180     ,c_effective_date => p_effective_date
6181     );
6182   Fetch csr_get_aat_info into l_aat_info;
6183 
6184   If csr_get_aat_info%FOUND Then
6185      debug(l_proc_name, 10);
6186       -- SFG:4135481 : modified the criteria for deciding safegarded check
6187       -- Instead of using safeguarded grade , now using safeguarded rate type field.
6188       If (l_aat_info.tp_safeguarded_rate_type Is Not Null ) Then
6189         l_return_value := 'S';
6190      Elsif l_aat_info.tp_fast_track ='Y' Then
6191         l_return_value := 'F';
6192      End If;
6193   End If;
6194 
6195   Close csr_get_aat_info;
6196 
6197   debug('Return value :'||l_return_value, 20);
6198   debug_exit(l_proc_name);
6199   -- hr_utility.set_location('Leaving: '||l_proc_name, 15);
6200 
6201   Return l_return_value;
6202 
6203 EXCEPTION
6204   WHEN OTHERS THEN
6205     debug_exit(' Others in '||l_proc_name);
6206     RAISE;
6207 End Get_Grade_Fasttrack_Info;
6208 
6209 --
6210 -- ----------------------------------------------------------------------------
6211 -- |------------------------< process_element >-------------------------------|
6212 -- ----------------------------------------------------------------------------
6213 function process_element (p_assignment_id    in   number
6214                          ,p_calculation_date in   date
6215                          ,p_rate_name        in   varchar2
6216                          ,p_rate_type        in   varchar2
6217                          ,p_from_time_dim    in   varchar2
6218                          ,p_to_time_dim      in   varchar2
6219                          ,p_fte              in   varchar2
6220                          ,p_term_time_yes_no in   varchar2
6221                          )
6222   return number is
6223 --
6224   l_proc_name        varchar2(60) := g_proc_name || 'process_element';
6225   l_ele_rate         csr_ele_rate_id%rowtype;
6226   l_paa_rate         csr_paa_rate_id%rowtype;
6227   l_paa_attribute_id pqp_assignment_attributes_f.assignment_attribute_id%type := null;
6228   l_value            number := 0;
6229 --
6230 begin
6231   --
6232   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
6233   --
6234   debug_enter (l_proc_name);
6235 
6236   open csr_ele_rate_id (c_rate_name => p_rate_name
6237                        ,c_rate_type => p_rate_type);
6238   fetch csr_ele_rate_id into l_ele_rate;
6239   close csr_ele_rate_id;
6240 
6241   -- Check whether the rate id is the same in aat
6242   open csr_paa_rate_id (c_assignment_id  => p_assignment_id
6243                        ,c_effective_date => p_calculation_date
6244                        );
6245   loop
6246 
6247     debug(l_proc_name, 10);
6248     fetch csr_paa_rate_id into l_paa_rate;
6249     exit when csr_paa_rate_id%notfound;
6250 
6251     if l_paa_rate.tp_safeguarded_rate_id = l_ele_rate.rate_id
6252     then
6253 
6254        debug(l_proc_name, 15);
6255        l_paa_attribute_id := l_paa_rate.assignment_attribute_id;
6256        exit;
6257     end if; -- end if of rate id check...
6258 
6259   end loop;
6260   close csr_paa_rate_id;
6261 
6262   if l_paa_attribute_id is not null then
6263 
6264      if p_rate_type = 'SP' then
6265 
6266         --
6267         debug(l_proc_name, 20);
6268         --
6269         open csr_scale_rate(c_attribute_id   => l_paa_attribute_id
6270                            ,c_effective_date => p_calculation_date
6271                            );
6272         fetch csr_scale_rate into l_value;
6273         close csr_scale_rate;
6274 
6275      elsif p_rate_type = 'GR' then
6276 
6277        --
6278        debug(l_proc_name, 30);
6279        --
6280        open csr_grade_rate (c_attribute_id   => l_paa_attribute_id
6281                            ,c_effective_date => p_calculation_date
6282                            );
6283        fetch csr_grade_rate into l_value;
6284        close csr_grade_rate;
6285 
6286      end if; -- end if of rate type check ...
6287 
6288      if l_value is not null then
6289 
6290        --
6291        debug(l_proc_name, 40);
6292        --
6293        l_value := pqp_rates_history_calc.convert_values
6294                     (p_assignment_id   => p_assignment_id
6295                     ,p_date            => p_calculation_date
6296                     ,p_value           => l_value
6297                     ,p_to_time_dim     => p_to_time_dim
6298                     ,p_from_time_dim   => p_from_time_dim
6299                     ,p_fte             => p_fte
6300                     ,p_service_history => 'N'
6301                     ,p_term_time_yes_no => p_term_time_yes_no
6302                     );
6303 
6304      end if; -- end if of value not null check...
6305 
6306   end if; -- end if of attribute id not null check ...
6307 
6308   debug('Return Value :'||to_char(l_value), 50);
6309   --
6310   -- hr_utility.set_location('Leaving: '||l_proc_name, 50);
6311   --
6312   debug_exit (l_proc_name);
6313 
6314   return l_value;
6315 
6316 EXCEPTION
6317   WHEN OTHERS THEN
6318     debug_exit(' Others in '||l_proc_name);
6319     RAISE;
6320 end process_element;
6321 --
6322 -- ----------------------------------------------------------------------------
6323 -- |------------------------< rates_history >---------------------------------|
6324 -- ----------------------------------------------------------------------------
6325 function rates_history (p_assignment_id    in     number
6326                        ,p_calculation_date in     date
6327                        ,p_rate_type_name   in     varchar2
6328                        ,p_fte              in     varchar2
6329                        ,p_to_time_dim      in     varchar2
6330                        ,p_safeguarded_yn   in     varchar2
6331                        ,p_rate             in out nocopy number
6332                        )
6333   return number is
6334 --
6335 
6336 -- Cursor to check if an element is linked to a assignment
6337 
6338       CURSOR c_link_assign (
6339                          p_assignment_id   IN NUMBER
6340                         ,p_element_type_id IN NUMBER
6341                         ,p_date            IN DATE )IS
6342       select 'Y'
6343         from   pay_element_links_f pel
6344               ,pay_element_entries_f pee
6345               ,pay_element_types_f pet
6346        where pet.element_type_id = pel.element_type_id
6347          and pel.element_link_id = pee.element_link_id
6348          and pee.assignment_id   = p_assignment_id
6349          and pet.element_type_id = p_element_type_id
6350          and p_date between pel.effective_start_date and
6351                                         pel.effective_end_date
6352          and p_date between pee.effective_start_date and
6353                                         pee.effective_end_date
6354          and p_date between pet.effective_start_date and
6355                                         pet.effective_end_date ;
6356 
6357   l_proc_name        varchar2(60) := g_proc_name || 'rates_history';
6358   l_element_set      csr_element_set%rowtype;
6359   l_service_history  pay_element_type_extra_info.eei_information5%type;
6360   l_fte              pay_element_type_extra_info.eei_information4%type;
6361   l_pay_source_value pay_element_type_extra_info.eei_information2%type;
6362   l_qualifier        pay_element_type_extra_info.eei_information3%type;
6363   l_from_time_dim    pay_element_type_extra_info.eei_information1%type;
6364   l_element_rate     number := 0;
6365   l_total_rate       number := 0;
6366 
6367   l_calculation_type             pay_element_type_extra_info.eei_information1%type;
6368   l_calculation_value            pay_element_type_extra_info.eei_information1%type;
6369   l_input_value                  pay_element_type_extra_info.eei_information1%type;
6370   l_linked_to_assignment         pay_element_type_extra_info.eei_information1%type;
6371   l_term_time_yes_no             pay_element_type_extra_info.eei_information1%type;
6372   l_chk_assign_link              fnd_lookup_values.lookup_code%TYPE;
6373   l_sum_multiple_entries_yn      fnd_lookup_values.lookup_code%TYPE;
6374   l_lookup_input_values_yn       fnd_lookup_values.lookup_code%TYPE;
6375   l_column_name_source_type      pay_element_type_extra_info.eei_information16%TYPE;
6376   l_column_name_source_name      pay_element_type_extra_info.eei_information17%TYPE;
6377   l_row_name_source_type         pay_element_type_extra_info.eei_information18%TYPE;
6378   l_row_name_source_name         pay_element_type_extra_info.eei_information19%TYPE;
6379 
6380 
6381   -- Added a new varaibale to hold the element details .
6382   l_element_dtl     pqp_gb_tp_pension_extracts.t_allowance_eles;
6383   l_element_type_id NUMBER;
6384   l_count   NUMBER := 0;
6385   l_error NUMBER;
6386 
6387   l_rate_nc          number;
6388 --
6389 BEGIN
6390 
6391   -- nocopy changes
6392   l_rate_nc := p_rate;
6393 
6394   debug_enter(l_proc_name);
6395 
6396   debug('p_assignment_id :'||to_char(p_assignment_id),10);
6397   debug('Calculation Date :'||to_char(p_calculation_date,'dd/mm/yyyy'));
6398   debug('p_rate_type_name :'||p_rate_type_name);
6399   debug('p_fte :'||p_fte);
6400   debug('p_to_time_dim :'||p_to_time_dim);
6401   debug('p_safeguarded_yn :'||p_safeguarded_yn);
6402 
6403 
6404   BEGIN
6405     -- Get Element Attribution for the given rate type
6406     IF p_rate_type_name IS NULL THEN
6407     -- Retention elements are defined so we need to create the element set
6408     -- using the l_tab_ret_aln_eles
6409       debug('g_tab_ret_aln_eles.COUNT: '||to_char(pqp_gb_tp_pension_extracts.g_tab_ret_aln_eles.COUNT),20);
6410 
6411       l_element_dtl := pqp_gb_tp_pension_extracts.g_tab_ret_aln_eles;
6412 
6413     ELSE
6414       -- Create the element set from the rate type passed.
6415       OPEN csr_element_set (c_name => p_rate_type_name
6416                          ,c_eff_date => p_calculation_date
6417                          ,c_business_group_id =>
6418                                 g_ext_asg_details(p_assignment_id).business_group_id
6419                          );
6420       LOOP
6421         FETCH csr_element_set into l_element_set;
6422         EXIT WHEN csr_element_set%NOTFOUND;
6423 
6424         debug('element_type_id: '||to_char(l_element_type_id), 30);
6425         debug('element_type_extra_info_id: '||to_char(l_element_set.element_type_extra_info_id));
6426 
6427         l_element_dtl(l_element_set.element_type_id).element_type_id
6428                      := l_element_set.element_type_id ;
6429         l_element_dtl(l_element_set.element_type_id).element_type_extra_info_id
6430                      := l_element_set.element_type_extra_info_id ;
6431       END LOOP;
6432       CLOSE csr_element_set;
6433 
6434     END IF ;
6435 
6436     debug('l_element_dtl.COUNT: '||l_element_dtl.COUNT, 40) ;
6437     l_element_type_id := l_element_dtl.FIRST;
6438 
6439     l_count := 1 ; -- Loop counter ;
6440 
6441     WHILE l_element_type_id IS NOT NULL
6442     LOOP
6443       --
6444       debug('element_type_id: '|| l_element_type_id,50+l_count/100 );
6445       --
6446       pqp_rates_history_calc.get_element_attributes
6447         (--p_element_type_extra_info_id  => l_element_set.element_type_extra_info_id
6448          p_element_type_extra_info_id  => l_element_dtl(l_element_type_id).element_type_extra_info_id
6449         ,p_service_history             => l_service_history    -- out
6450         ,p_fte                         => l_fte                -- out
6451         ,p_pay_source_value            => l_pay_source_value   -- out
6452         ,p_qualifier                   => l_qualifier          -- out
6453         ,p_from_time_dim               => l_from_time_dim      -- out
6454         ,p_calculation_type            => l_calculation_type   -- out
6455         ,p_calculation_value           => l_calculation_value  -- out
6456         ,p_input_value                 => l_input_value        -- out
6457         ,p_linked_to_assignment        => l_linked_to_assignment -- out
6458         ,p_term_time_yes_no            => l_term_time_yes_no   -- out
6459         ,p_sum_multiple_entries_yn    => l_sum_multiple_entries_yn --out
6460         ,p_lookup_input_values_yn     => l_lookup_input_values_yn  --out
6461         ,p_column_name_source_type    => l_column_name_source_type -- out
6462         ,p_column_name_source_name    => l_column_name_source_name -- out
6463         ,p_row_name_source_type       => l_row_name_source_type  -- out
6464         ,p_row_name_source_name       => l_row_name_source_name -- out
6465         );
6466 
6467       -- The value Linked to Assignment is Yes indicates that
6468       -- the element should be considered only if it is linked to
6469       -- assignment
6470 
6471       IF l_linked_to_assignment = 'Y' THEN
6472 
6473         -- Checking whether linked to Assignment
6474 
6475         OPEN c_link_assign (
6476                         p_assignment_id   => p_assignment_id
6477                        ,p_element_type_id => l_element_type_id -- l_element_set.element_type_id
6478                        ,p_date            => p_calculation_date ) ;
6479 
6480         FETCH c_link_assign INTO l_chk_assign_link ;
6481           IF c_link_assign%NOTFOUND THEN
6482 
6483               -- The element is not linked to assignment
6484               l_chk_assign_link := 'N' ;
6485           END IF ;
6486         CLOSE c_link_assign ;
6487 
6488         debug('l_chk_assign_link' ||l_chk_assign_link, 30);
6489 
6490       ELSE -- IF l_linked_to_assignment = 'N'
6491 
6492              -- Element Need not be Linked to Assignment
6493              l_chk_assign_link := 'Y' ;
6494 
6495       END IF ; -- IF l_link_to_assign = 'Y'
6496 
6497       debug('l_chk_assign_link' || l_chk_assign_link, 40);
6498       --hr_utility.set_location('l_chk_assign_link' ||l_chk_assign_link, 40);
6499 
6500       IF l_chk_assign_link = 'Y' THEN
6501 
6502         --debug('Element Type Id :'||l_element_set.element_type_id);
6503         debug('Element Type Id :'||l_element_type_id);
6504         debug('Qualifier :'||l_qualifier);
6505         debug('Pay Source Value :'||l_pay_source_value);
6506 
6507         IF l_pay_source_value in ('SP', 'GR') THEN
6508 
6509             debug('Pay Source is SP or GR');
6510 
6511             -- Check whether process should calculate rate based on safeguarded scale
6512             IF p_safeguarded_yn = 'Y' THEN
6513 
6514               --
6515               debug(l_proc_name, 30);
6516               --
6517               l_element_rate :=
6518                 process_element -- only processes SP and GR so doesn't need entry value related params
6519                   (p_assignment_id                => p_assignment_id
6520                   ,p_calculation_date             => p_calculation_date
6521                   ,p_rate_name                    => l_qualifier
6522                   ,p_rate_type                    => l_pay_source_value
6523                   ,p_from_time_dim                => l_from_time_dim
6524                   ,p_to_time_dim                  => p_to_time_dim
6525                   ,p_fte                          => p_fte
6526                   ,p_term_time_yes_no             => l_term_time_yes_no
6527                   );
6528 
6529              debug('Element Rate for SF :'||l_element_rate);
6530 
6531            ELSE -- if not paid on safeguarded scale
6532 
6533              --
6534              debug(l_proc_name, 40);
6535              --
6536              l_element_rate := pqp_rates_history_calc.process_element
6537                                  (p_assignment_id                => p_assignment_id
6538                                  ,p_date                         => p_calculation_date
6539                                  --,p_element_type_id              => l_element_set.element_type_id
6540                                  ,p_element_type_id              => l_element_type_id
6541                                  ,p_to_time_dim                  => p_to_time_dim
6542                                  ,p_fte                          => p_fte
6543                                  ,p_service_history              => 'N'
6544                                  ,p_pay_source_value             => l_pay_source_value
6545                                  ,p_qualifier                    => l_qualifier
6546                                  ,p_from_time_dim                => l_from_time_dim
6547                                  ,p_calculation_type             => l_calculation_type
6548                                  ,p_calculation_value            => l_calculation_value
6549                                  ,p_input_value                  => l_input_value
6550                                  ,p_term_time_yes_no             => l_term_time_yes_no
6551                                  ,p_sum_multiple_entries_yn      => l_sum_multiple_entries_yn
6552                                  ,p_lookup_input_values_yn       => l_lookup_input_values_yn
6553                                  ,p_column_name_source_type      => l_column_name_source_type
6554                                  ,p_column_name_source_name      => l_column_name_source_name
6555                                  ,p_row_name_source_type         => l_row_name_source_type
6556                                  ,p_row_name_source_name         => l_row_name_source_name
6557                                  );
6558 
6559              debug('Element Rate for non-SF :'||l_element_rate);
6560 
6561            END IF; -- end if of safeguarded flag check...
6562 
6563         ELSE -- if pay source is not SP or GR
6564 
6565           --
6566           debug(l_proc_name, 50);
6567           debug('Pay Source NOT SP or GR');
6568            -- Additional parameters following rates history changes
6569           l_element_rate := pqp_rates_history_calc.process_element
6570                               (p_assignment_id                => p_assignment_id
6571                               ,p_date                         => p_calculation_date
6572                               --,p_element_type_id              => l_element_set.element_type_id
6573                               ,p_element_type_id              => l_element_type_id
6574                               ,p_to_time_dim                  => p_to_time_dim
6575                               ,p_fte                          => p_fte
6576                               ,p_service_history              => 'N'
6577                               ,p_pay_source_value             => l_pay_source_value
6578                               ,p_qualifier                    => l_qualifier
6579                               ,p_from_time_dim                => l_from_time_dim
6580                               ,p_calculation_type             => l_calculation_type
6581                               ,p_calculation_value            => l_calculation_value
6582                               ,p_input_value                  => l_input_value
6583                               ,p_term_time_yes_no             => l_term_time_yes_no
6584                               ,p_sum_multiple_entries_yn      => l_sum_multiple_entries_yn
6585                               ,p_lookup_input_values_yn       => l_lookup_input_values_yn
6586                               ,p_column_name_source_type      => l_column_name_source_type
6587                               ,p_column_name_source_name      => l_column_name_source_name
6588                               ,p_row_name_source_type         => l_row_name_source_type
6589                               ,p_row_name_source_name         => l_row_name_source_name
6590                               );
6591 
6592           debug('Element Rate for non-SF :'||l_element_rate);
6593 
6594         end if; -- end of of pay source check ...
6595 
6596         l_total_rate := l_total_rate + nvl(l_element_rate,0);
6597 
6598       END IF; -- End if of chk link to assignment check ...
6599 
6600       l_element_type_id := l_element_dtl.NEXT(l_element_type_id);
6601 
6602     END LOOP;
6603     --close csr_element_set;
6604 
6605     p_rate := round(l_total_rate, 5);
6606 
6607     debug('p_rate :'||to_char(p_rate), 60);
6608     --
6609     -- hr_utility.set_location('Leaving: '||l_proc_name, 60);
6610     --
6611     debug_exit(l_proc_name);
6612 
6613     return 0;
6614 
6615 
6616   debug('con_err: SQLCODE' || to_char(SQLCODE), 97);
6617   debug('con_err: SQLERRM' || SQLERRM, 97);
6618 
6619     --
6620   exception
6621     when hr_application_error then
6622     --
6623       debug('con_err: SQLCODE' || to_char(SQLCODE), 98);
6624       debug('con_err: SQLERRM' || SQLERRM, 98);
6625     --
6626       p_rate := 0;
6627 
6628       if csr_element_set%ISOPEN then -- BUG 4431495 : checking if cursor is open
6629         close csr_element_set;
6630       end if;
6631 
6632       debug('error message for contract missing :' || hr_utility.get_message, 98);
6633 
6634       debug('hr_application_error RAISED in Type1 rates_history function', 70);
6635 
6636       debug('Trying to raise a PQP error message', 98);
6637 
6638       -- BUG 4431495 : raising a warning message, which is being caught as an exception
6639       -- from pqp_rates_history_calc in SQLERRM, and passed on as token in dummy BEN messg
6640       -- having text as only a TOKEN
6641 
6642       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
6643                  (p_assignment_id => p_assignment_id
6644                  ,p_error_text    => 'BEN_94268_DUMMY_MESSAGE'
6645                  ,p_error_number  => 94268
6646                  ,p_token1        => SQLERRM
6647                  );
6648 
6649       debug('Have raised a PQP error message, check on extract results ', 98);
6650 
6651       debug_exit(l_proc_name);
6652       return -1;
6653 
6654     WHEN OTHERS THEN
6655     --
6656       debug('con_err: SQLCODE' || to_char(SQLCODE), 99);
6657       debug('con_err: SQLERRM' || SQLERRM, 99);
6658     --
6659 
6660       p_rate := l_rate_nc;
6661       debug_exit(' Others in '||l_proc_name);
6662       raise;
6663   --
6664   end;
6665 
6666 end rates_history;
6667 --
6668 -- RET: BUG 4135481: New function added to return the Retention Allowance Rate
6669 -- Following the Legislative Updates to Management and Retention Allowance
6670 -- Effective from 01-APR-2004.
6671 -- ----------------------------------------------------------------------------
6672 -- |------------------------< get_tp1_retention_allow_rate >--------------------|
6673 -- ----------------------------------------------------------------------------
6674 function get_tp1_retention_allow_rate (p_assignment_id in     number
6675                                       ,p_ret_allow     out    nocopy varchar2
6676                                     )
6677   return number is
6678 --
6679   l_proc_name            varchar2(60) := g_proc_name || 'get_tp1_retention_allow_rate';
6680   l_return               number;
6681   l_ret_allow            number;
6682 --
6683 begin
6684 
6685   debug_enter(l_proc_name);
6686 
6687   l_return := calc_tp1_retention_allow_rate
6688                 (p_assignment_id        => p_assignment_id
6689                 ,p_effective_start_date => greatest
6690                                              (g_pension_year_start_date
6691                                              ,g_ext_asg_details(p_assignment_id).teacher_start_date
6692                                              )
6693                 ,p_effective_end_date   => least
6694                                              (g_effective_run_date
6695                                              ,nvl(g_ext_asg_details(p_assignment_id).leaver_date,
6696                                                     g_effective_run_date)
6697                                              )
6698                 ,p_rate                 => l_ret_allow
6699                 );
6700 
6701   debug_exit(l_proc_name);
6702 
6703   if l_return <> -1 then
6704 
6705      p_ret_allow                  := trim(to_char(l_ret_allow,'09999'));
6706      return 0;
6707 
6708   else
6709 
6710     p_ret_allow := '00000';
6711     return -1;
6712 
6713   end if; -- end if of l_return check ...
6714 
6715 EXCEPTION
6716   WHEN OTHERS THEN
6717     debug_exit(' Others in '||l_proc_name
6718               ,'Y' -- turn trace off
6719               );
6720     p_ret_allow := NULL;
6721     RAISE;
6722 end get_tp1_retention_allow_rate;
6723 
6724 
6725 -- RET: BUG 4135481: New function added to return the Retention Allowance Rate
6726 -- Following the Legislative Updates to Management and Retention Allowance
6727 -- Effective from 01-APR-2004.
6728 -- ----------------------------------------------------------------------------
6729 -- |------------------------< calc_tp1_retention_allow_rate >---------------|
6730 -- ----------------------------------------------------------------------------
6731 
6732 FUNCTION calc_tp1_retention_allow_rate
6733                         (p_assignment_id        in     number
6734                         ,p_effective_start_date in     date
6735                         ,p_effective_end_date   in     date
6736                         ,p_rate                 in out nocopy number
6737                         )
6738                         RETURN NUMBER IS
6739 
6740 --
6741   l_proc_name varchar2(60) := g_proc_name ||
6742                                         'calc_tp1_retention_allow_rate';
6743 
6744 
6745   l_tab_mult_asg            t_sec_asgs_type;
6746   l_sec_effective_date      date;
6747   l_primary_esd             date;
6748   l_return                  number;
6749   l_error                   number;
6750   l_rate_nc                 number;
6751   i                         number;
6752   l_primary_eed             date;
6753   l_sec_eed                 date;
6754 
6755   l_total_rert_allowance_rate number := 0;
6756   l_retention_allowance_rate number;
6757  --
6758 
6759 
6760 BEGIN
6761 --
6762 
6763   debug_enter(l_proc_name);
6764 
6765   -- nocopy changes
6766   l_rate_nc := p_rate;
6767 
6768   --
6769   debug('p_assignment_id :'||to_char(p_assignment_id), 10);
6770   debug('Start date :'||to_char(p_effective_start_date,'DD/MM/YY'));
6771   debug('End date   :'||to_char(p_effective_end_date,'DD/MM/YY'));
6772   --
6773 
6774   -- Check if the primary asg is valid TCHR assignment
6775   -- on the p_effective_start_date
6776   -- MULT-LR --
6777   -- Use the new Function to check the effectivness of an assignment
6778   -- it takes care of multiple Leaver-Restarter events
6779   -- where as the old logic used to take into account
6780   -- only the first restarter event.
6781 
6782   IF ( chk_effective_asg (p_assignment_id   => p_assignment_id
6783                          ,p_effective_date  => p_effective_start_date
6784                          ) = 'Y'
6785       )
6786   THEN
6787   --
6788     debug(l_proc_name, 20);
6789     --     We need to calculate
6790     --     from start of primary asg as its possible that
6791     --     primary asg bcomes a teacher after secondary
6792     l_primary_esd := GREATEST(p_effective_start_date
6793                              ,g_ext_asg_details(p_assignment_id).start_date
6794                               );
6795 
6796     debug('l_primary_esd :'||to_char(l_primary_esd,'DD/MM/YY'),30);
6797 
6798     -- MULT-LR --
6799     -- Use the new Function to get the correct end date
6800     -- based on the multiple restarter events
6801     -- It takes care of multiple Leaver-Restarter events
6802     -- where as the old logic used to take into account
6803     -- only the first restarter event.
6804 
6805     -- Performance changes
6806     -- no need to call tihs function as we are checking assignment status in chk_effective_asg
6807     /*
6808     l_primary_eed := get_eff_end_date
6809                              (p_assignment_id        => p_assignment_id
6810                              ,p_effective_start_date => p_effective_start_date
6811                              ,p_effective_end_date   => p_effective_end_date
6812                              ) ;
6813     */
6814 
6815     l_primary_eed := p_effective_end_date;
6816 
6817     debug('l_primary_eed :'||to_char(l_primary_eed,'DD/MM/YY'),40);
6818 
6819     -- Call rates_history function for primary assignment
6820     -- No Rate type available, so PASS Null, this will work as an identifier
6821     -- for rates_history function to derive the Ratention Allowance Rate
6822     -- Which otherwise expects a Rate Type as input.
6823 
6824     l_return :=  rates_history
6825                         (p_assignment_id    => p_assignment_id
6826                         ,p_calculation_date => l_primary_esd
6827                         ,p_rate_type_name   => NULL
6828                         ,p_fte              => 'N'
6829                         ,p_to_time_dim      => 'A'
6830                         ,p_safeguarded_yn   => NULL
6831                         ,p_rate             => l_retention_allowance_rate
6832                         );
6833         --
6834         if l_return <> -1 then
6835           debug('l_retention_allowance_rate :'||to_char(l_retention_allowance_rate), 50);
6836           l_total_rert_allowance_rate := l_total_rert_allowance_rate + l_retention_allowance_rate;
6837                 debug('l_total_rert_allowance_rate :'||to_char(l_total_rert_allowance_rate), 60);
6838         else
6839           debug_exit(l_proc_name);
6840           p_rate      := 0;
6841           return -1;
6842         end if;
6843         --
6844 
6845   --
6846   ELSE -- primary not valid asg
6847   --
6848     debug(l_proc_name, 70);
6849     l_total_rert_allowance_rate := 0;
6850     l_return                       := 0;
6851   --
6852 
6853   END IF; -- primary not valid asg
6854 
6855   --
6856   IF l_return <> -1 THEN
6857 
6858     -- Check for multiple assignments
6859     debug(l_proc_name, 80);
6860 
6861     --
6862     -- Bugfix 3803760:FTSUPPLY : Now using g_tab_sec_asgs instead of
6863     --  calling get_eff_secondary_asgs
6864     l_tab_mult_asg := g_tab_sec_asgs;
6865 
6866     debug('l_tab_mult_asg.count :'|| to_char(l_tab_mult_asg.count),90);
6867 
6868     IF l_tab_mult_asg.count > 0 THEN
6869       --
6870       debug(l_proc_name, 110);
6871       --
6872 
6873       i := l_tab_mult_asg.FIRST;
6874 
6875       -- get the annual_salary_rate for secondary assignments
6876       -- and check for equality
6877       -- store the slaary rates and dates in global collection,
6878       -- as it may be required later.
6879 
6880       WHILE i IS NOT NULL
6881       LOOP
6882         --
6883         debug (l_proc_name||'Assignment Id:'||to_char(nvl(l_tab_mult_asg(i).assignment_id,99999)),120);
6884         l_sec_effective_date := greatest
6885                                (p_effective_start_date
6886                                ,g_ext_asg_details(l_tab_mult_asg(i).assignment_id).start_date);
6887 
6888         debug('l_sec_effective_date :'|| to_char(l_sec_effective_date,'DD/MM/YYYY'),130);
6889 
6890 
6891         -- MULT-LR --
6892         -- Use the new Function to get the correct end date
6893         -- based on the multiple restarter events
6894         -- It takes care of multiple Leaver-Restarter events
6895         -- where as the old logic used to take into account
6896         -- only the first restarter event.
6897 
6898         -- Performance changes
6899         -- no need to call tihs function as we are checking assignment status in chk_effective_asg
6900         /*
6901         l_sec_eed := get_eff_end_date ( p_assignment_id        => l_tab_mult_asg(i).assignment_id
6902                                        ,p_effective_start_date => p_effective_start_date
6903                                        ,p_effective_end_date   => p_effective_end_date
6904                                        ) ;
6905         */
6906         l_sec_eed := p_effective_end_date;
6907 
6908         debug('l_sec_eed :'||to_char(l_sec_eed,'DD/MM/YYYY'),140);
6909 
6910         -- Call rates_history function for secondary assignments
6911         -- No Rate type available, so PASS Null, this will work as an identifier
6912         -- for rates_history function to derive the Ratention Allowance Rate
6913         -- Which otherwise expects a Rate Type as input.
6914 
6915         l_return :=  rates_history
6916                         (p_assignment_id    => l_tab_mult_asg(i).assignment_id
6917                         ,p_calculation_date => l_sec_effective_date
6918                         ,p_rate_type_name   => NULL
6919                         ,p_fte              => 'N'
6920                         ,p_to_time_dim      => 'A'
6921                         ,p_safeguarded_yn   => NULL
6922                         ,p_rate             => l_retention_allowance_rate
6923                         );
6924 
6925               --
6926         if l_return <> -1 then
6927           debug('l_retention_allowance_rate :'||to_char(l_retention_allowance_rate), 150);
6928           l_total_rert_allowance_rate := l_total_rert_allowance_rate + l_retention_allowance_rate;
6929                 debug('l_total_rert_allowance_rate :'||to_char(l_total_rert_allowance_rate), 160);
6930         else
6931           debug_exit(l_proc_name);
6932           p_rate      := 0;
6933           return -1;
6934         end if;
6935         --
6936 
6937 
6938         i := l_tab_mult_asg.NEXT(i);
6939 
6940       END LOOP;
6941 
6942       debug('After loop----', 170);
6943 
6944     END IF;  -- l_tab_mult_asg.count > 0
6945 
6946     debug('Retention Allowance : '||to_char(l_total_rert_allowance_rate),175);
6947 
6948    -- Check whether retention allowance rate has exceeded the 4 digit limit ...
6949     IF l_total_rert_allowance_rate > 99999 THEN
6950 
6951             l_error := pqp_gb_tp_extract_functions.raise_extract_warning
6952                  (p_assignment_id => p_assignment_id
6953                  ,p_error_text    => 'BEN_93041_EXT_TP1_ANN_VAL_EXC'
6954                  ,p_error_number  => 93041
6955                  -- RET: token introduced in error message
6956                  ,p_token1        => 'Recruitment Retention '|| TO_CHAR(l_total_rert_allowance_rate)
6957                  ,p_token2        => TO_CHAR(99999) -- bug : 4336613
6958                  );
6959 
6960       p_rate := 99999;
6961 
6962     ELSE  -- end if of annual sal rate limit check ...
6963       debug('Total Retention Allowance Rate :'||to_char(l_total_rert_allowance_rate),180);
6964       p_rate := l_total_rert_allowance_rate;
6965 
6966     END IF ; -- end if of annual sal rate limit check ...
6967 
6968     debug('p_rate : '||to_char(p_rate),185);
6969 
6970 
6971     debug_exit (l_proc_name);
6972 
6973     RETURN 0;
6974 
6975   ELSE -- else of return <> -1 on prim asg check...
6976 
6977     debug_exit (l_proc_name);
6978     p_rate      := 0;
6979 
6980     RETURN -1;
6981 
6982   END IF ; -- end if of return <> -1 on prim asg check...
6983 
6984 -- Added by tmehra for nocopy changes Feb'03
6985 
6986 EXCEPTION
6987     WHEN OTHERS THEN
6988        debug_exit(' Others in '||l_proc_name);
6989        p_rate := l_rate_nc;
6990        raise;
6991 
6992 END calc_tp1_retention_allow_rate;
6993 
6994 --
6995 -- ----------------------------------------------------------------------------
6996 -- |------------------------< calc_annual_sal_rate >---------------------------|
6997 -- ----------------------------------------------------------------------------
6998 function calc_annual_sal_rate (p_assignment_id        in     number
6999                               ,p_calculation_date     in     date
7000                               ,p_safeguarded_yn       in     varchar2
7001                               ,p_fte                  in     varchar2
7002                               ,p_to_time_dim          in     varchar2
7003                               ,p_rate                 in out nocopy number
7004                               ,p_effective_start_date in     date
7005                               ,p_effective_end_date   in     date
7006                               )
7007   return number is
7008 --
7009   l_proc_name        varchar2(60) := g_proc_name || 'calc_annual_sal_rate';
7010   l_annual_rate      number := 0;
7011   l_lonsoc_allowance number;
7012   l_other_allowance  number;
7013   l_basic_salary     number;
7014   l_return           number;
7015   l_rate_name        per_grades.name%type;
7016 
7017   l_rate_nc          number;
7018 
7019   -- 4336613 : OSLA_3A
7020   l_grossed_osla_payment number;
7021 
7022 --
7023 begin
7024   --
7025   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
7026   --
7027   debug_enter(l_proc_name);
7028 
7029   -- nocopy changes
7030   l_rate_nc := p_rate;
7031 
7032 
7033   -- PERF_ENHANC_3A : Performance Enhancements
7034   -- check if record corresponding to p_assignment_id is present in the
7035   -- collection g_asg_recalc_details.
7036   -- If yes, check for matching start_date (a double check,although not necessary)
7037   -- If full_time_sal_rate has been calculated before, then the row
7038   -- will contain the value,return it.
7039   -- If full_time_sal_rate for this assignment has not been calculated before,
7040   -- compute it, store it in a row for this assignment_id and return it
7041   -- This step is to avoid recomputing the value for a given LOS. Originally,
7042   -- calculations were repeated for each data element
7043 
7044   IF (g_asg_recalc_details.EXISTS(p_assignment_id)
7045       AND
7046       g_asg_recalc_details(p_assignment_id).eff_start_date = p_effective_start_date
7047       AND
7048       g_asg_recalc_details(p_assignment_id).full_time_sal_rate IS NOT NULL
7049       ) THEN
7050 
7051     p_rate := g_asg_recalc_details(p_assignment_id).full_time_sal_rate;
7052     debug('full_time_sal_rate is already present !! '||p_rate, 5);
7053 
7054   ELSE -- calc_part_time_paid has to be computed for this assignment_id for this LOS
7055 
7056 
7057     -- Bug fix 2786740
7058     -- London rate type, other allowance need not have a value
7059     -- always
7060 
7061     IF g_lon_rate_type IS NOT NULL THEN
7062 
7063        -- Find annual rate for London and special needs allowance
7064        l_return := rates_history
7065                      (p_assignment_id    => p_assignment_id
7066                      ,p_calculation_date => p_calculation_date
7067                      ,p_rate_type_name   => g_lon_rate_type
7068                      ,p_fte              => p_fte
7069                      ,p_to_time_dim      => p_to_time_dim
7070                      ,p_safeguarded_yn   => p_safeguarded_yn
7071                      ,p_rate             => l_lonsoc_allowance
7072                      );
7073 
7074        if l_return <> -1 then
7075 
7076           l_annual_rate := l_annual_rate + nvl(l_lonsoc_allowance, 0);
7077 
7078        else
7079 
7080          debug_exit(l_proc_name);
7081          p_rate      := 0;
7082          return -1;
7083 
7084        end if; -- end if of london allowance return value check...
7085 
7086     END IF; -- End if of g_lon_rate_type is not null check ...
7087 
7088     --
7089     debug(l_proc_name, 20);
7090     --
7091 
7092     IF g_oth_rate_type IS NOT NULL THEN
7093 
7094        -- Find rate for Other allowance
7095        l_return := rates_history
7096                      (p_assignment_id    => p_assignment_id
7097                      ,p_calculation_date => p_calculation_date
7098                      ,p_rate_type_name   => g_oth_rate_type
7099                      ,p_fte              => p_fte
7100                      ,p_to_time_dim      => p_to_time_dim
7101                      ,p_safeguarded_yn   => p_safeguarded_yn
7102                      ,p_rate             => l_other_allowance
7103                      );
7104 
7105        if l_return <> -1 then
7106 
7107           debug('l_other_allowance: '||l_other_allowance,25);
7108           l_annual_rate                      := l_annual_rate + nvl(l_other_allowance,0);
7109           debug('l_annual_rate: '||l_annual_rate,25);
7110           g_other_allowance(p_assignment_id) := nvl(l_other_allowance, 0);
7111 
7112        else
7113 
7114          debug_exit(l_proc_name);
7115          p_rate      := 0;
7116          return -1;
7117 
7118        end if; -- end if of other allowance return value check...
7119 
7120     ELSE
7121       g_other_allowance(p_assignment_id) := 0; -- Bug 4454427 :resetting value to 0
7122 
7123     END IF; -- End if of g_oth_rate_type is not null check ...
7124 
7125     debug('g_other_allowance(p_assignment_id): '||g_other_allowance(p_assignment_id),26);
7126 
7127 
7128     -- 4336613 : OSLA_3A : OSLA grossed payment function call
7129     l_grossed_osla_payment := get_grossed_osla_payments
7130                                      (p_assignment_id        => p_assignment_id
7131                                      ,p_effective_start_date => p_effective_start_date
7132                                      ,p_effective_end_date   => p_effective_end_date
7133                                      ,p_business_group_id    => g_business_group_id
7134                                      );
7135 
7136       -- 4336613 : OSLA_3A : add OSLA payments to annual sal rate and other allowances
7137 
7138     debug('l_grossed_osla_payment: '||l_grossed_osla_payment,27);
7139     l_annual_rate                        := l_annual_rate + l_grossed_osla_payment;
7140     debug('l_annual_rate: '||l_annual_rate,27);
7141 
7142     -- if grossed OSLA payments exist (non-zero), then add to Other Allowance
7143     IF l_grossed_osla_payment <> 0 THEN
7144       -- this is to avoid adding to a NULL value
7145       -- in case other_allowance has a value, add to it. Else, assign to it.
7146       IF (g_oth_rate_type IS NOT NULL) THEN
7147         g_other_allowance(p_assignment_id) := g_other_allowance(p_assignment_id) + l_grossed_osla_payment;
7148       ELSE
7149         g_other_allowance(p_assignment_id) := l_grossed_osla_payment;
7150       END IF;
7151     END IF;
7152 
7153     debug('g_other_allowance(p_assignment_id): '||g_other_allowance(p_assignment_id),28);
7154 
7155 
7156     --
7157     debug(l_proc_name, 30);
7158     --
7159     -- Find rate for Basic Salary
7160     select decode(p_safeguarded_yn
7161                  ,'Y'
7162                  ,g_sf_rate_type
7163                  ,g_sal_rate_type)
7164       into l_rate_name
7165       from dual;
7166 
7167     debug('l_rate_name :'||l_rate_name, 40);
7168 
7169     l_return := rates_history
7170                   (p_assignment_id    => p_assignment_id
7171                   ,p_calculation_date => p_calculation_date
7172                   ,p_rate_type_name   => l_rate_name
7173                   ,p_fte              => p_fte
7174                   ,p_to_time_dim      => p_to_time_dim
7175                   ,p_safeguarded_yn   => p_safeguarded_yn
7176                   ,p_rate             => l_basic_salary
7177                   );
7178 
7179     debug('Basic Salary :'||l_basic_salary, 50);
7180 
7181     if l_return <> -1 then
7182 
7183        l_annual_rate := l_annual_rate + nvl(l_basic_salary,0);
7184 
7185     else
7186 
7187       debug_exit(l_proc_name);
7188       p_rate      := 0;
7189       return -1;
7190 
7191     end if; -- end if of basic salary return value check...
7192 
7193     debug('Total Annual Rate :'||l_annual_rate);
7194 
7195     --
7196     -- hr_utility.set_location('Leaving: '||l_proc_name, 50);
7197     --
7198     debug_exit(l_proc_name);
7199 
7200     --PERF_ENHANC_3A : performance enhancements
7201     -- computed full_time_sal_rate value being stored in the collection for future use
7202     g_asg_recalc_details(p_assignment_id).full_time_sal_rate := l_annual_rate;
7203     debug('full_time_sal_rate (1st time computation) :'|| l_annual_rate,55);
7204 
7205     p_rate := l_annual_rate;
7206 
7207 
7208   END IF; -- IF (g_asg_recalc_details.EXISTS.... )
7209 
7210   return 0;
7211 
7212 -- Added by tmehra for nocopy changes Feb'03
7213 
7214 EXCEPTION
7215     WHEN OTHERS THEN
7216        p_rate := l_rate_nc;
7217        debug_exit(' Others in '||l_proc_name);
7218        raise;
7219 
7220   --
7221 end calc_annual_sal_rate;
7222 --
7223 -- ----------------------------------------------------------------------------
7224 -- |------------------------< get_safeguarded_info >--------------------------|
7225 -- ----------------------------------------------------------------------------
7226 function get_safeguarded_info (p_assignment_id  in    number
7227                               ,p_effective_date in    date
7228                               )
7229   return varchar2 is
7230 --
7231   l_proc_name            varchar2(60) := g_proc_name || 'get_safeguarded_info';
7232   l_paa_info             csr_paa_rate_id%rowtype;
7233   l_safeguarded_yn       varchar2(1) := 'N';
7234 --
7235 begin
7236   --
7237   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
7238   --
7239   debug_enter(l_proc_name);
7240 
7241   open csr_paa_rate_id (c_assignment_id  => p_assignment_id
7242                        ,c_effective_date => p_effective_date
7243                        );
7244   fetch csr_paa_rate_id into l_paa_info;
7245   close csr_paa_rate_id;
7246 
7247   if (( l_paa_info.tp_safeguarded_rate_type is not null)
7248         and (l_paa_info.tp_safeguarded_rate_type <> 'SN' )) then
7249 
7250      l_safeguarded_yn := 'Y';
7251 
7252   end if; -- end if of tp_safeguarded_grade is not null check...
7253 
7254   --
7255   -- hr_utility.set_location('Leaving: '||l_proc_name, 20);
7256   --
7257   debug_exit(l_proc_name);
7258 
7259   return l_safeguarded_yn;
7260 
7261 EXCEPTION
7262   WHEN OTHERS THEN
7263     debug_exit(' Others in '||l_proc_name);
7264     RAISE;
7265 end get_safeguarded_info;
7266 --
7267 -- ----------------------------------------------------------------------------
7268 -- |------------------------< get_fte_for_asg >----------------------|
7269 -- ----------------------------------------------------------------------------
7270 -- Bug 3889646
7271 -- The function will fetch the FTE value
7272 -- from assignment budget values
7273 -- using fte_utilities.
7274 -- if not found then
7275 -- it calculates the fte from
7276 -- average salary calculations.
7277 
7278 FUNCTION get_fte_for_asg(p_assignment_id         IN  NUMBER
7279                         ,p_effective_start_date  IN  DATE
7280                         ,p_effective_end_date    IN  DATE
7281                         ,p_annual_sal_rate       IN  NUMBER
7282                         ,p_business_group_id     IN  NUMBER
7283                          )
7284 return NUMBER IS
7285 
7286   l_proc_name                varchar2(60) := g_proc_name || 'get_fte_for_asg';
7287   l_days_in_period           NUMBER ;
7288   l_no_of_days_in_year       NUMBER := 365;
7289   l_total_part_time_sal_paid NUMBER;
7290   l_avg_sal_for_period       NUMBER;
7291   l_fte                      NUMBER := 0;
7292 
7293 BEGIN
7294   debug_enter(l_proc_name);
7295 
7296   debug('p_assignment_id '|| to_char(p_assignment_id) ,10 ) ;
7297   debug('p_effective_start_date '|| to_char(p_effective_start_date,'DD/MM/YYYY') ) ;
7298   debug('p_effective_end_date '|| to_char(p_effective_end_date,'DD/MM/YYYY') ) ;
7299   debug('p_annual_sal_rate '|| to_char(p_annual_sal_rate) ) ;
7300   debug('p_business_group_id '|| to_char(p_business_group_id) ) ;
7301 
7302   -- Get FTE value for primary assignment
7303   l_fte := pqp_fte_utilities.get_fte_value
7304                  (p_assignment_id    => p_assignment_id
7305                  ,p_calculation_date => p_effective_start_date
7306                   );
7307   debug('l_fte '|| l_fte ,20 ) ;
7308   IF l_fte is null or l_fte = 0  THEN
7309 
7310     debug(l_proc_name, 30) ;
7311 
7312     l_days_in_period := (trunc(p_effective_end_date) - trunc(p_effective_start_date)) + 1;
7313 
7314     debug('l_days_in_period '|| to_char(l_days_in_period) ,40 ) ;
7315     IF l_days_in_period > l_no_of_days_in_year THEN
7316       l_days_in_period := l_no_of_days_in_year ;
7317     END IF ;
7318     debug('l_days_in_period :'||to_char(l_days_in_period), 50);
7319 
7320     l_total_part_time_sal_paid := calc_part_time_sal
7321                                         (p_assignment_id        => p_assignment_id
7322                                         ,p_effective_start_date => p_effective_start_date
7323                                         ,p_effective_end_date   => p_effective_end_date
7324                                         ,p_business_group_id    => p_business_group_id
7325                                         );
7326     debug('l_total_part_time_sal_paid :'||to_char(l_total_part_time_sal_paid),60);
7327 
7328     IF l_total_part_time_sal_paid = 0 THEN
7329       debug('Part Time salary paid is ZERO for the period ' , 70);
7330       debug(to_char(p_effective_start_date,'dd/mm/yyyy') || ' to ' || to_char(p_effective_end_date,'dd/mm/yyyy')) ;
7331       debug('need to RAISE a warning here') ;
7332     END IF;
7333 
7334     l_avg_sal_for_period := (p_annual_sal_rate/l_no_of_days_in_year) * l_days_in_period ;
7335     debug('l_avg_sal_for_period :'||to_char(l_avg_sal_for_period), 80);
7336 
7337     IF l_avg_sal_for_period <> 0 THEN
7338       debug(l_proc_name, 90);
7339       l_fte  := (l_total_part_time_sal_paid/l_avg_sal_for_period);
7340     ELSE
7341       debug(l_proc_name, 110);
7342       l_fte := 0;
7343     END IF ;
7344   END IF ; --l_fte = 0 or NULL
7345 
7346   debug('l_fte :'||to_char(l_fte));
7347 
7348   debug_exit (l_proc_name);
7349   RETURN l_fte;
7350 
7351 EXCEPTION
7352     WHEN OTHERS THEN
7353        debug_exit(' Others in '||l_proc_name);
7354        raise;
7355 
7356 END get_fte_for_asg;
7357 
7358 -- ----------------------------------------------------------------------------
7359 -- |------------------------< get_annual_sal_rate_date >----------------------|
7360 -- ----------------------------------------------------------------------------
7361 function get_annual_sal_rate_date (p_assignment_id        in     number
7362                                   ,p_effective_start_date in     date
7363                                   ,p_effective_end_date   in     date
7364                                   ,p_rate                 in out nocopy number
7365                                   )
7366   return number is
7367 --
7368   l_proc_name               varchar2(60) := g_proc_name || 'get_annual_sal_rate_date';
7369   l_safeguarded_yn          varchar2(1);
7370   l_annual_sal_rate         number;
7371   l_actual_ann_sal_rate     number := 0;
7372   l_actual_other_allowance  number := 0;
7373   l_fte                     number := 0;
7374   l_total_fte               number := 0;
7375   l_total_oth_alo_fte       number := 0;
7376   l_sec_annual_sal_rate     number;
7377   l_total_annual_sal_rate   number := 0;
7378   l_total_other_allowance   number := 0;
7379   l_tab_mult_asg            t_sec_asgs_type;
7380   l_sec_effective_date      date;
7381   l_primary_esd             date;
7382   l_return                  number;
7383   l_error                   number;
7384 
7385   l_rate_nc                 number;
7386   i                         number;
7387   -- new Variable added for FTE calculation changes.
7388   l_total_part_time_sal_paid number := 0;
7389   l_no_of_days_in_year       number := 365 ;
7390   l_days_in_period           number := 0 ;
7391   l_avg_sal_for_period       number := 0;
7392   l_primary_eed              date;
7393   l_sec_eed                  date;
7394   --new variable added for FTE calc changes.
7395   l_prev_annual_sal_rate     number := NULL;
7396   l_equal_sal_rate           varchar2(1) := 'Y';
7397   --Flags for showing warning for a LoS
7398   l_warn_for_sal_rate        varchar2(1) := 'N';
7399   l_warn_for_sal_paid        varchar2(1) := 'N';
7400 
7401   -- changed g_asg_sal_rate to a local variable as
7402   -- this is not being eferenced from outside this function.
7403   l_asg_sal_rate             t_asg_salary_rate_type;
7404   l_gtc_payments             number;
7405 
7406  --
7407 BEGIN
7408   -- Bug 3889646
7409   -- This function is enhanced and
7410   -- the following logic is applied.
7411   -- This function gets the salary rates
7412   -- for all assignments valid in the period.
7413   -- if all the salary rates are equal, then
7414   -- this salary rate is returned
7415   -- as Full Time Salary Rate
7416   -- else we do the average salary rate calculation
7417 
7418   -- Average salary rate calculation :
7419   -- first we check for the stored FTE
7420   -- at assignment budget values.
7421   -- if found then
7422   -- this value is used as
7423   -- the FTE for the assignment
7424   -- else FTE is calculated on the fly
7425   -- by calculating average salary rate
7426   -- and Part time salary paid.
7427   -- all the FTE calculation is done separatly
7428   -- in the get_fte_for_asg function
7429   -- then this FTE value is used as
7430   -- per the following formula
7431   -- for average salary rate calculation
7432 
7433   -- Average Salary Rate Formula
7434   -- avg sal rate = (sal_rate1 * fte1 + sal_rate2 * fte2 +....)/(fte1 + fte2 + ....)
7435 
7436   debug_enter(l_proc_name);
7437 
7438   -- nocopy changes
7439   l_rate_nc := p_rate;
7440 
7441   -- This global has been removed.
7442   -- clear the global values.
7443   -- g_asg_sal_rate.DELETE;
7444 
7445   --
7446   debug('p_assignment_id :'||to_char(p_assignment_id), 10);
7447   debug('Start date :'||to_char(p_effective_start_date,'DD/MM/YY'));
7448   debug('End date   :'||to_char(p_effective_end_date,'DD/MM/YY'));
7449 
7450   -- Get the safeguarded information and the annual rate
7451   -- for primary assignment only if the assignment qualifies
7452   -- for inclusion in the report
7453   -- Bug Fix 3073562:GAP6
7454 
7455   -- Check if the primary asg is valid TCHR assignment
7456   -- on the p_effective_start_date
7457   -- MULT-LR --
7458   -- Use the new Function to check the effectivness of an assignment
7459   -- it takes care of multiple Leaver-Restarter events
7460   -- where as the old logic used to take into account
7461   -- only the first restarter event.
7462   IF ( chk_effective_asg (
7463            p_assignment_id   => p_assignment_id
7464           ,p_effective_date  => p_effective_start_date
7465                           ) = 'Y'
7466       )
7467   THEN
7468     debug(l_proc_name, 20);
7469     -- Bugfix 3641851:CBF1 : We need to calculate the sal rate
7470     --     from start of primary asg as its possible that
7471     --     primary asg bcomes a teacher after secondary
7472     l_primary_esd := GREATEST(p_effective_start_date
7473                               ,g_ext_asg_details(p_assignment_id).start_date
7474                               );
7475     debug('l_primary_esd :'||to_char(l_primary_esd,'DD/MM/YY'),30);
7476 
7477 
7478     -- MULT-LR --
7479     -- Use the new Function to get the correct end date
7480     -- based on the multiple restarter events
7481     -- It takes care of multiple Leaver-Restarter events
7482     -- where as the old logic used to take into account
7483     -- only the first restarter event.
7484 
7485     -- Performance changes
7486     -- no need to call tihs function as we are checking assignment status in chk_effective_asg
7487     /*
7488     l_primary_eed := get_eff_end_date ( p_assignment_id        => p_assignment_id
7489                                        ,p_effective_start_date => p_effective_start_date
7490                                        ,p_effective_end_date   => p_effective_end_date
7491                                        ) ;
7492     */
7493 
7494     l_primary_eed := p_effective_end_date;
7495 
7496 
7497     debug('l_primary_eed :'||to_char(l_primary_eed,'DD/MM/YY'),40);
7498     --
7499     -- Get safeguarded information
7500 
7501     l_safeguarded_yn := get_safeguarded_info
7502                            (p_assignment_id  => p_assignment_id
7503                            -- Bugfix 3641851:CBF1 : Changed to l_primary_esd
7504                            ,p_effective_date => l_primary_esd
7505                            );
7506 
7507     --
7508     debug('Safeguarded :'||l_safeguarded_yn,50);
7509     --
7510     -- Get the annual sal rate for primary assignment
7511 
7512     l_return := calc_annual_sal_rate
7513                   (p_assignment_id        => p_assignment_id
7514                    -- Bugfix 3641851:CBF1 : Changed to l_primary_esd
7515                   ,p_calculation_date     => l_primary_esd
7516                   ,p_safeguarded_yn       => l_safeguarded_yn
7517                   ,p_fte                  => 'N'
7518                   ,p_to_time_dim          => 'A'
7519                   ,p_rate                 => l_annual_sal_rate
7520                   ,p_effective_start_date => p_effective_start_date
7521                   ,p_effective_end_date   => p_effective_end_date
7522                   );
7523     debug('l_annual_sal_rate '||to_char(l_annual_sal_rate), 60) ;
7524     debug('l_return '||to_char(l_return)) ;
7525 
7526     IF l_return <> -1 THEN  --No error
7527       IF l_annual_sal_rate <> 0 THEN
7528         -- store the values in the global
7529         -- may be required later for average salary calculation
7530         l_asg_sal_rate(p_assignment_id).salary_rate    := l_annual_sal_rate ;
7531         l_asg_sal_rate(p_assignment_id).eff_start_date := l_primary_esd ;
7532         l_asg_sal_rate(p_assignment_id).eff_end_date   := l_primary_eed ;
7533         --Store the val in another var for comparison later.
7534         l_prev_annual_sal_rate                         := l_annual_sal_rate ;
7535 
7536         IF g_other_allowance.exists(p_assignment_id) THEN
7537           debug(l_proc_name, 70);
7538           l_total_other_allowance := g_other_allowance(p_assignment_id);
7539         END IF;
7540 
7541         debug('l_total_other_allowance :'||to_char(l_total_other_allowance),80);
7542 
7543         l_gtc_payments := get_gtc_payments(p_assignment_id => p_assignment_id,
7544                                    p_effective_start_date =>p_effective_start_date,
7545                                    p_effective_end_date   => p_effective_end_date,
7546                                    p_business_group_id  => g_business_group_id
7547                                    );
7548 
7549         g_gtc_payments:= g_gtc_payments + l_gtc_payments;
7550 
7551       ELSE  --l_annual_sal_rate <> 0
7552         debug(l_proc_name, 90);
7553         -- if Annual Salary rate is ZERO
7554         -- set the warning flag to 'Y'
7555         -- will warn at the end of the function
7556         l_warn_for_sal_rate  := 'Y' ;
7557 
7558         l_asg_sal_rate(p_assignment_id).salary_rate    := 0 ;
7559         l_asg_sal_rate(p_assignment_id).eff_start_date := l_primary_esd ;
7560         l_asg_sal_rate(p_assignment_id).eff_end_date   := l_primary_eed ;
7561 
7562       END IF ;  --l_annual_sal_rate <> 0
7563     END IF;  --l_return <> -1
7564 
7565 
7566   ELSE -- primary not valid asg
7567     debug(l_proc_name, 110);
7568     l_annual_sal_rate := 0;
7569     l_return          := 0;
7570   END IF; -- primary not valid asg
7571 
7572   debug('Annual Sal Rate :'||to_char(l_annual_sal_rate), 120);
7573 
7574   --
7575   IF l_return <> -1 THEN
7576     -- Check for multiple assignments
7577     debug(l_proc_name, 130);
7578     --
7579     -- Bugfix 3803760:FTSUPPLY : Now using g_tab_sec_asgs instead of
7580     --  calling get_eff_secondary_asgs
7581     l_tab_mult_asg := g_tab_sec_asgs;
7582 
7583     debug('l_tab_mult_asg.count :'|| to_char(l_tab_mult_asg.count),140);
7584     IF l_tab_mult_asg.count > 0 THEN
7585       --
7586       debug(l_proc_name, 150);
7587       --
7588       i := l_tab_mult_asg.FIRST;
7589       -- get the annual_salary_rate for secondary assignments
7590       -- and check for equality
7591       -- store the slaary rates and dates in global collection,
7592       -- as it may be required later.
7593 
7594       WHILE i IS NOT NULL
7595       LOOP
7596         --
7597         debug (l_proc_name||'Assignment Id:'||to_char(nvl(l_tab_mult_asg(i).assignment_id,99999)),160);
7598 
7599         l_sec_effective_date := greatest
7600                                (p_effective_start_date
7601                                ,g_ext_asg_details(l_tab_mult_asg(i).assignment_id).start_date);
7602         debug('l_sec_effective_date :'|| to_char(l_sec_effective_date,'DD/MM/YYYY'),170);
7603         l_gtc_payments := 0;
7604 
7605         -- MULT-LR --
7606         -- Use the new Function to get the correct end date
7607         -- based on the multiple restarter events
7608         -- It takes care of multiple Leaver-Restarter events
7609         -- where as the old logic used to take into account
7610         -- only the first restarter event.
7611 
7612         -- Performance changes
7613         -- no need to call tihs function as we are checking assignment status in chk_effective_asg
7614         /*
7615 
7616         l_sec_eed := get_eff_end_date ( p_assignment_id        => l_tab_mult_asg(i).assignment_id
7617                                        ,p_effective_start_date => p_effective_start_date
7618                                        ,p_effective_end_date   => p_effective_end_date
7619                                        ) ;
7620         */
7621 
7622         l_sec_eed := p_effective_end_date;
7623 
7624 
7625         debug('l_sec_eed :'||to_char(l_sec_eed,'DD/MM/YYYY'),180);
7626 
7627         l_safeguarded_yn := get_safeguarded_info
7628                                (p_assignment_id  => l_tab_mult_asg(i).assignment_id
7629                                ,p_effective_date => l_sec_effective_date
7630                                );
7631         debug('l_safeguarded_yn :'||l_safeguarded_yn,190);
7632 
7633         -- Get annual sal rate for secondary assignments
7634         l_return := calc_annual_sal_rate
7635                     (p_assignment_id        => l_tab_mult_asg(i).assignment_id
7636                     ,p_calculation_date     => l_sec_effective_date
7637                     ,p_safeguarded_yn       => l_safeguarded_yn
7638                     ,p_fte                  => 'N'
7639                     ,p_to_time_dim          => 'A'
7640                     ,p_rate                 => l_sec_annual_sal_rate
7641                     ,p_effective_start_date => p_effective_start_date
7642                     ,p_effective_end_date   => p_effective_end_date
7643                     );
7644         debug('l_sec_annual_sal_rate :'||l_sec_annual_sal_rate, 195);
7645 
7646         IF l_return <> -1 THEN
7647           IF l_sec_annual_sal_rate = 0 THEN
7648 
7649             debug('RAISE A warning for ZERO sal rate ----',210);
7650             -- Set the flag for Warning Message
7651             l_warn_for_sal_rate := 'Y' ;
7652 
7653             l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).salary_rate     := 0 ;
7654             l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).eff_start_date  := l_sec_effective_date ;
7655             l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).eff_end_date    := l_sec_eed ;
7656 
7657           ELSE
7658 
7659             debug(l_proc_name, 215);
7660             l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).salary_rate     := l_sec_annual_sal_rate;
7661             l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).eff_start_date  := l_sec_effective_date ;
7662             l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).eff_end_date    := l_sec_eed ;
7663 
7664             IF g_other_allowance.exists(l_tab_mult_asg(i).assignment_id) THEN
7665               debug(l_proc_name, 220);
7666               l_total_other_allowance := l_total_other_allowance + nvl(g_other_allowance(l_tab_mult_asg(i).assignment_id), 0);
7667             END IF ;
7668 
7669             debug('l_total_other_allowance :'||to_char(l_total_other_allowance),230);
7670 
7671             --compare with the previous salary rate and set the flag to 'N'
7672             -- as soon as a different sal_rate is found.
7673             IF l_prev_annual_sal_rate is not NULL THEN
7674                debug(l_proc_name, 240);
7675 
7676                IF l_sec_annual_sal_rate <> l_prev_annual_sal_rate THEN
7677                  debug(l_proc_name, 250);
7678                  l_equal_sal_rate := 'N' ;
7679                ELSE
7680                  debug('sal_rate are equal for this iteration',260);
7681                END IF;
7682 
7683              ELSE  -- l_prev_annual_sal_rate is not Null
7684                --The first valid assignment
7685                debug(l_proc_name, 270);
7686                l_prev_annual_sal_rate := l_sec_annual_sal_rate ;
7687              END IF ; --l_prev_annual_sal_rate <> 0
7688 
7689           END IF ; --l_sec_annual_sal_rate = 0
7690 
7691         ELSE  --l_return <> -1
7692           debug('error returned from calc_annual_sal_rate',280) ;
7693           debug('need to raise a warning from here....') ;
7694           p_rate      := 0;
7695           debug_exit (l_proc_name);
7696           RETURN -1;
7697         END IF ;  --l_return <> -1
7698 
7699         l_gtc_payments := get_gtc_payments(p_assignment_id => l_tab_mult_asg(i).assignment_id,
7700                                    p_effective_start_date =>p_effective_start_date,
7701                                    p_effective_end_date   => p_effective_end_date,
7702                                    p_business_group_id  => g_business_group_id
7703                                    );
7704          g_gtc_payments := g_gtc_payments + l_gtc_payments;
7705 
7706         i := l_tab_mult_asg.NEXT(i);
7707 
7708       END LOOP;
7709       debug('After loop----', 290);
7710 
7711     END IF;  -- l_tab_mult_asg.count > 0
7712 
7713     debug('l_equal_sal_rate '|| l_equal_sal_rate, 310);
7714 
7715     -- check if the flag is still 'Y'
7716     -- return the salary rate .
7717     -- else go to the average calculation
7718     -- Added for 5460058
7719     g_equal_sal_rate  := l_equal_sal_rate;
7720     IF l_equal_sal_rate = 'Y'  THEN
7721       debug(l_proc_name, 320);
7722 
7723       IF l_prev_annual_sal_rate is NOT NULL THEN
7724         l_actual_ann_sal_rate := l_prev_annual_sal_rate ;
7725       ELSE
7726         l_actual_ann_sal_rate := 0;
7727       END IF ;
7728 
7729       l_actual_other_allowance := l_total_other_allowance ;
7730 
7731     ELSE  ----l_equal_sal_rate = 'Y'
7732     -- need to get the average salary calculation and calculate the FTE.
7733 
7734       debug(l_proc_name, 330);
7735 
7736       -- reset the two variables
7737       -- as average calcultions will be done here as well.
7738       l_actual_other_allowance := 0;
7739       l_total_other_allowance  := 0;
7740 
7741       -- MULT-LR --
7742       -- Use the new Function to check the effectivness of an assignment
7743       -- it takes care of multiple Leaver-Restarter events
7744       -- where as the old logic used to take into account
7745       -- only the first restarter event.
7746 
7747       IF ( chk_effective_asg (
7748                p_assignment_id  => p_assignment_id
7749               ,p_effective_date => p_effective_start_date
7750                               ) = 'Y'
7751           )
7752       THEN
7753         debug(l_proc_name, 340);
7754         l_fte := get_fte_for_asg(
7755                      p_assignment_id        => p_assignment_id
7756                     ,p_effective_start_date => l_asg_sal_rate(p_assignment_id).eff_start_date
7757                     ,p_effective_end_date   => l_asg_sal_rate(p_assignment_id).eff_end_date
7758                     ,p_annual_sal_rate      => l_asg_sal_rate(p_assignment_id).salary_rate
7759                     ,p_business_group_id    => g_ext_asg_details(p_assignment_id).business_group_id
7760                                 );
7761 
7762         debug('l_fte ' ||to_char(l_fte), 350);
7763         IF l_fte = 0 THEN
7764            debug('set warning for FTE = 0', 355) ;
7765           l_warn_for_sal_paid :='Y' ;
7766         END IF;
7767         --storing FTE, just in case we need it in future.
7768         l_asg_sal_rate(p_assignment_id).fte := l_fte ;
7769 
7770         l_total_annual_sal_rate := l_fte * l_asg_sal_rate(p_assignment_id).salary_rate;
7771         l_total_fte             := l_fte;
7772         debug('l_total_annual_sal_rate :'||l_total_annual_sal_rate,360);
7773         debug('l_total_fte :'||l_total_fte);
7774 
7775         IF g_other_allowance.exists(p_assignment_id) AND
7776            g_other_allowance(p_assignment_id) <> 0
7777         THEN
7778           debug(l_proc_name, 370);
7779           l_total_other_allowance  := g_other_allowance(p_assignment_id) * l_fte;
7780           l_total_oth_alo_fte      := l_fte;
7781         END IF; -- end if of other allowance check ...
7782       END IF ;  --Check if Priamry is Valid
7783 
7784       i := l_tab_mult_asg.FIRST;
7785       WHILE i IS NOT NULL
7786       LOOP
7787         --
7788         debug (l_proc_name||'Assignment Id:'||to_char(nvl(l_tab_mult_asg(i).assignment_id,99999)),380);
7789 
7790         l_fte := get_fte_for_asg(
7791                       p_assignment_id        => l_tab_mult_asg(i).assignment_id
7792                      ,p_effective_start_date => l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).eff_start_date
7793                      ,p_effective_end_date   => l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).eff_end_date
7794                      ,p_annual_sal_rate      => l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).salary_rate
7795                      ,p_business_group_id    => g_ext_asg_details(l_tab_mult_asg(i).assignment_id).business_group_id
7796                                 );
7797 
7798         debug('l_fte :'|| to_char(l_fte), 390);
7799 
7800         IF l_fte = 0 THEN
7801           debug('set warning for FTE = 0', 395) ;
7802           l_warn_for_sal_paid :='Y' ;
7803         END IF;
7804         --storing FTE, just in case we need it in future.
7805         l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).fte := l_fte ;
7806 
7807         l_total_annual_sal_rate := l_total_annual_sal_rate +
7808                                    (l_asg_sal_rate(l_tab_mult_asg(i).assignment_id).salary_rate * l_fte);
7809         l_total_fte             := l_total_fte + l_fte;
7810         debug('l_total_annual_sal_rate :'|| to_char(l_total_annual_sal_rate),410);
7811         debug('l_total_fte :'|| to_char(l_total_fte));
7812 
7813         IF g_other_allowance.exists(l_tab_mult_asg(i).assignment_id) AND
7814            g_other_allowance(l_tab_mult_asg(i).assignment_id) <> 0
7815         THEN
7816           debug(l_proc_name, 420);
7817           l_total_other_allowance := l_total_other_allowance +
7818                                      (g_other_allowance(l_tab_mult_asg(i).assignment_id) * l_fte);
7819           l_total_oth_alo_fte     := l_total_oth_alo_fte + l_fte;
7820         END IF; -- end if of other allowance exists check...
7821 
7822         i := l_tab_mult_asg.NEXT(i);
7823 
7824       END LOOP;
7825 
7826       debug (l_proc_name ||'Total Secondary Asgs: '||TO_CHAR(l_tab_mult_asg.COUNT),430);
7827 
7828       debug('l_total_fte ' ||to_char(l_total_fte), 440);
7829       debug('l_total_annual_sal_rate ' ||to_char(l_total_annual_sal_rate));
7830 
7831       IF l_total_fte <> 0 THEN
7832         debug(l_proc_name, 450);
7833         l_actual_ann_sal_rate    := l_total_annual_sal_rate/l_total_fte;
7834         debug('l_actual_ann_sal_rate ' ||to_char(l_actual_ann_sal_rate));
7835       ELSE
7836         debug(l_proc_name,460);
7837         l_actual_ann_sal_rate := 0 ;
7838         --p_rate := 0;
7839       END IF ;
7840 
7841       IF l_total_oth_alo_fte <> 0 THEN
7842         debug(l_proc_name,470);
7843         l_actual_other_allowance := l_total_other_allowance/l_total_oth_alo_fte;
7844       ELSE
7845           debug(l_proc_name,480);
7846           l_actual_other_allowance := 0;
7847       END IF; -- end if of other allowance exists check ...
7848 
7849     END IF ; --l_equal_sal_rate = 'Y'
7850 
7851     debug('l_actual_ann_sal_rate ' ||to_char(l_actual_ann_sal_rate));
7852 
7853     l_actual_ann_sal_rate :=l_actual_ann_sal_rate + NVL(g_gtc_payments,0);
7854 
7855     g_other_allowance(p_assignment_id) := round(l_actual_other_allowance);
7856     l_actual_ann_sal_rate              := round(l_actual_ann_sal_rate);
7857 
7858     debug('g_other_allowance(p_assignment_id) ' ||to_char(g_other_allowance(p_assignment_id)),490);
7859     debug('l_actual_ann_sal_rate after adding GTC payments '||to_char(l_actual_ann_sal_rate));
7860 
7861     g_gtc_payments:=0;
7862     -- 4336613 : SAL_VALIDAT_3A : Check whether annual sal rate has exceeded the 5 digit limit
7863     -- If yes, raise warning.
7864     IF l_actual_ann_sal_rate > 999999 THEN
7865 
7866       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
7867                  (p_assignment_id => p_assignment_id
7868                  ,p_error_text    => 'BEN_93041_EXT_TP1_ANN_VAL_EXC'
7869                  ,p_error_number  => 93041
7870                  -- token introduced in error message
7871                  ,p_token1        => 'Annual Salary Rate ' || TO_CHAR(l_actual_ann_sal_rate) -- bug : 4336613
7872                  ,p_token2        => TO_CHAR(999999) -- bug : 4336613
7873                  );
7874 
7875       p_rate := 999999; -- 4336613 : SAL_VALIDAT_3A : set to 99999 if > 99999
7876     ELSE  -- end if of annual sal rate limit check ...
7877       debug('Actual Annual Salary Rate :'||to_char(l_actual_ann_sal_rate),510);
7878       p_rate := l_actual_ann_sal_rate;
7879     END IF ; -- end if of annual sal rate limit check ...
7880 
7881     debug('p_rate : '||to_char(p_rate),515);
7882 
7883     debug('l_warn_for_sal_rate: '||l_warn_for_sal_rate,520) ;
7884     -- Check for the Warning flag
7885     -- Disable this warning as a part of fix#9487206
7886     /*IF l_warn_for_sal_rate = 'Y' THEN
7887       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
7888                  (p_assignment_id => p_assignment_id
7889                  ,p_error_text    => 'BEN_94044_ZERO_SAL_RATE'
7890                  ,p_error_number  => 94044
7891                  ,p_token1        => fnd_date.date_to_displaydate(p_effective_start_date)
7892                 );
7893     END IF ;*/
7894 
7895     debug('l_warn_for_sal_paid: '||l_warn_for_sal_paid,530) ;
7896     -- Check for the Warning flag
7897     IF l_warn_for_sal_paid = 'Y' THEN
7898       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
7899                  (p_assignment_id => p_assignment_id
7900                  ,p_error_text    => 'BEN_94045_ZERO_PT_SAL_PAID'
7901                  ,p_error_number  => 94045
7902                  ,p_token1        => fnd_date.date_to_displaydate(p_effective_start_date)
7903                 );
7904     END IF ;
7905 
7906 
7907     debug_exit (l_proc_name);
7908     RETURN 0;
7909 
7910   ELSE -- else of return <> -1 on prim asg check...
7911 
7912     debug_exit (l_proc_name);
7913     p_rate      := 0;
7914     RETURN -1;
7915 
7916   END IF ; -- end if of return <> -1 on prim asg check...
7917 
7918 -- Added by tmehra for nocopy changes Feb'03
7919 
7920 EXCEPTION
7921     WHEN OTHERS THEN
7922        debug_exit(' Others in '||l_proc_name);
7923        p_rate := l_rate_nc;
7924        raise;
7925 
7926 END get_annual_sal_rate_date;
7927 
7928 
7929 -- BUG 4135481
7930 -- TERM_LSP: get_terminated_payments finds the
7931 -- Final process date of employee if he/she
7932 -- has been terminated with in the reporting period and
7933 -- sums up salary paid upto Final close Date.
7934 -- NB: If for a terminated employee there is no Final Close Date
7935 -- then it picks up the Last Standard Process Date if it exists, else
7936 -- (ie. LSP date is Null and Final Close Date is Null ) then it picks up the
7937 -- actual Termination Date. Since no upper bound for payroll date
7938 -- is found the payments are done till actual termination date
7939 -- which is  calculated as per adjust_post_part_payments.
7940 Function get_terminated_payments (p_assignment_id        IN     NUMBER
7941                                  ,p_effective_start_date IN     DATE
7942                                  ,p_effective_end_date   IN     DATE
7943                                  ,p_business_group_id    IN     NUMBER
7944                                  ,p_part_payment         OUT NOCOPY NUMBER
7945                                  ,p_balance_type_id      IN NUMBER -- 4336613 : OSLA_3A
7946                                  )
7947 RETURN VARCHAR2 IS
7948 
7949 l_proc_name               VARCHAR2(60) := g_proc_name || 'get_terminated_payments';
7950 l_terminated              VARCHAR2(1);
7951 l_term_proc_date          DATE ;
7952 l_effective_date          DATE;
7953 l_total_term_payment      NUMBER := 0;
7954 l_count                   NUMBER := 0;
7955 l_term_payment            NUMBER := 0;
7956 l_effective_start_date    DATE;
7957 
7958 l_get_term_details            csr_get_termination_details%ROWTYPE;
7959 
7960 -- RETRO:BUG: 4135481
7961 l_asg_act_dtl             csr_get_asg_act_id_retro%ROWTYPE;
7962 l_supp_claim              NUMBER := 0;
7963 
7964 BEGIN
7965 
7966   debug_enter(l_proc_name);
7967   debug('p_assignment_id :' ||to_char(p_assignment_id  ),10);
7968   debug('p_effective_start_date :' ||to_char(p_effective_start_date,'DD/MM/YYYY') );
7969   debug('p_effective_end_date :' ||to_char(p_effective_end_date,'DD/MM/YYYY'));
7970 
7971   -- get the final close date (if exists)
7972   OPEN csr_get_termination_details
7973            (p_assignment_id        => p_assignment_id
7974            ,p_effective_end_date   => p_effective_end_date
7975            ,p_business_group_id    => p_business_group_id
7976            );
7977 
7978   FETCH  csr_get_termination_details INTO l_get_term_details;
7979 
7980 
7981   IF csr_get_termination_details%FOUND THEN
7982      -- employee has been terminated with in the reporting period (line of service)
7983       debug('actual_termination_date: '||
7984                 to_char(l_get_term_details.actual_termination_date,'DD-MM-YYYY'),20);
7985 
7986       l_term_proc_date := NVL(l_get_term_details.final_process_date ,
7987                                   NVL(l_get_term_details.last_standard_process_date,
7988                                       l_get_term_details.actual_termination_date));
7989 
7990       IF l_term_proc_date <> l_get_term_details.actual_termination_date THEN
7991         l_terminated := 'Y'; -- flag to check employee being terminated.
7992 
7993       ELSE
7994       --actual_termination_date = either of Last Std Process date or Final Close date
7995       --i.e. actual_termination_date  is on last day of the Pay period
7996         debug('actual_termination_date  is on last day of the Pay period',23);
7997         l_terminated := 'N';
7998         --person's payments included in regular payroll
7999       END IF;
8000 
8001 
8002       debug('termination payment date: '||
8003                   to_char( l_term_proc_date,'DD-MM-YYYY'),25);
8004 
8005 
8006 
8007   ELSE  --csr_get_lsp_date%FOUND THEN
8008     debug('No Termination Date found ', 30);
8009     l_terminated := 'N';
8010 
8011   END IF;
8012   CLOSE csr_get_termination_details;
8013 
8014 
8015   IF l_terminated ='Y' THEN
8016 
8017     -- If there is a case where person is terminated and
8018     -- there is no pay period after that current month then
8019     -- p_effective start_date is NULL after pre_payment is done.
8020     -- In that case if any days exist between terminated day and
8021     -- Last Standard process date then we want to calculate
8022     -- the terminated payment as well by setting the effective start date
8023     -- to p_effective_end_date.
8024 
8025 --    IF p_effective_start_date is NULL THEN
8026 --      l_effective_start_date := p_effective_end_date;
8027 --    ELSE
8028       l_effective_start_date := p_effective_start_date;
8029 --    END IF;
8030 
8031     debug('l_effective_start_date :'||to_char(l_effective_start_date, 'DD/MM/YYYY'),40) ;
8032 
8033     -- RETRO:BUG: 4135481/4273915
8034     -- Check if the RETRO flag g_calc_sal_new is set to 'Y'
8035 
8036     debug('g_calc_sal_new: ' ||g_calc_sal_new, 50);
8037 
8038 
8039     IF ( g_calc_sal_new = 'Y' ) THEN
8040 
8041       debug('g_def_bal_id(p_balance_type_id): '||to_char(g_def_bal_id(p_balance_type_id)),60);
8042       -- get the assignment_action_id and
8043       -- use the new seeded route to fetch the payments including the
8044       -- retro payments.
8045 
8046       l_count := 1;
8047       OPEN csr_get_asg_act_id_retro
8048                  (p_assignment_id        => p_assignment_id
8049                  ,p_effective_start_date => l_effective_start_date
8050                  ,p_effective_end_date   =>  l_term_proc_date
8051                   );
8052       LOOP
8053 
8054         FETCH csr_get_asg_act_id_retro INTO l_asg_act_dtl;
8055         EXIT WHEN csr_get_asg_act_id_retro%NOTFOUND;
8056 
8057         --
8058         debug('assignment_action_id: '||to_char(l_asg_act_dtl.assignment_action_id),70);
8059         debug('date_earned: '||to_char(l_asg_act_dtl.date_earned,'DD/MM/YYYY'));
8060         --
8061         l_term_payment := pay_balance_pkg.get_value
8062                               ( p_defined_balance_id   => g_def_bal_id(p_balance_type_id)
8063                                ,p_assignment_action_id => l_asg_act_dtl.assignment_action_id
8064                               ) ;
8065 
8066         debug('l_term_payment :'||to_char(l_term_payment), 80+l_count/10000) ;
8067 
8068         l_total_term_payment := l_total_term_payment + l_term_payment;
8069 
8070         debug('l_total_term_payment :'||to_char(l_total_term_payment),90+l_count/10000) ;
8071 
8072         l_count := l_count + 1 ;
8073       END LOOP;
8074       CLOSE csr_get_asg_act_id_retro;
8075 
8076       debug('l_total_term_payment :'||to_char(l_total_term_payment),110) ;
8077   /*
8078       IF g_date_work_mode = 'Y' AND g_supp_teacher = 'Y' THEN
8079         OPEN csr_get_supp_ded(p_balance_type_id,p_assignment_id,l_effective_start_date,l_term_proc_date);
8080         FETCH csr_get_supp_ded INTO l_supp_claim;
8081         CLOSE csr_get_supp_ded;
8082         debug('l_supp_claim :'||to_char(l_supp_claim),208);
8083         l_total_term_payment := l_total_term_payment - l_supp_claim;
8084         debug('l_total_term_payment after supply claims deduction:'||to_char(l_total_term_payment),209) ;
8085       END IF;
8086    */
8087       debug('....retro payments calc over....') ;
8088 
8089     ELSE  -- ( g_calc_sal_new = 'N' ) THEN
8090 
8091       -- get the payments using the usual balance route.
8092 
8093       OPEN csr_get_end_date (c_assignment_id        => p_assignment_id
8094                             ,c_effective_start_date => l_effective_start_date
8095                             ,c_effective_end_date   =>  l_term_proc_date
8096                           );
8097       LOOP
8098 
8099         FETCH csr_get_end_date INTO l_effective_date;
8100         EXIT when csr_get_end_date%notfound;
8101 
8102         debug('l_effective_date :'||to_char(l_effective_date, 'DD/MM/YYYY'),120) ;
8103 
8104         l_term_payment := hr_gbbal.calc_asg_proc_ptd_date
8105                            (p_assignment_id   => p_assignment_id
8106                            ,p_balance_type_id => p_balance_type_id
8107                            ,p_effective_date  => l_effective_date
8108                            );
8109 
8110 
8111         debug('l_term_payment :'||to_char(l_term_payment), 130+l_count/10000) ;
8112 
8113         l_total_term_payment := l_total_term_payment + l_term_payment;
8114 
8115         debug('l_total_term_payment :'||to_char(l_total_term_payment),140+l_count/10000) ;
8116 
8117         l_count := l_count + 1 ;
8118       END LOOP;
8119       debug('l_total_term_payment :'||to_char(l_total_term_payment),150) ;
8120       CLOSE csr_get_end_date;
8121    /*
8122       IF g_date_work_mode = 'Y' AND g_supp_teacher = 'Y' THEN
8123         OPEN csr_get_supp_ded(p_balance_type_id,p_assignment_id,l_effective_start_date,l_term_proc_date);
8124         FETCH csr_get_supp_ded INTO l_supp_claim;
8125         CLOSE csr_get_supp_ded;
8126         debug('l_supp_claim :'||to_char(l_supp_claim),208);
8127         l_total_term_payment := l_total_term_payment - l_supp_claim;
8128         debug('l_total_term_payment after supply claims deduction:'||to_char(l_total_term_payment),209) ;
8129 
8130       END IF;
8131    */
8132     END IF;  --( g_calc_sal_new = 'N' ) THEN
8133 
8134 
8135   END IF; --l_terminated ='Y' THEN
8136 
8137   debug('l_total_term_payment :'||to_char(l_total_term_payment),160) ;
8138 
8139   IF l_total_term_payment IS NULL THEN
8140     p_part_payment := 0;
8141   ELSE
8142     p_part_payment := l_total_term_payment;
8143   END IF;
8144 
8145   debug('p_part_payment: '|| to_char(p_part_payment), 170);
8146   debug_exit(l_proc_name);
8147 
8148   RETURN l_terminated;
8149 
8150 EXCEPTION
8151   WHEN OTHERS THEN
8152     debug_exit(' Others in '||l_proc_name);
8153     p_part_payment := NULL;
8154     RAISE;
8155 
8156 END get_terminated_payments;
8157 
8158 
8159 -- PTS: BUG 4135481
8160 -- This is a utility function that calculates the
8161 -- number of overlapping days in the two sets of dates.
8162 -- set1 (p_start_date1, p_end_date1)
8163 -- set2 (p_start_date2,p_end_date2)
8164 -- RETURNS 0 if no overlap period is found.
8165 FUNCTION get_overlap_days
8166           (p_start_date1 DATE
8167           ,p_end_date1   DATE
8168           ,p_start_date2 DATE
8169           ,p_end_date2   DATE
8170           ) RETURN NUMBER
8171 
8172  IS
8173 
8174  l_overlap_days NUMBER :=0 ;
8175  l_proc_name   VARCHAR2(60) := 'get_overlap_days' ;
8176 
8177 BEGIN
8178 
8179   debug_enter(l_proc_name) ;
8180 
8181   debug('p_start_date1: '||to_char(p_start_date1,'DD/MM/YYYY')) ;
8182   debug('p_end_date1: '||to_char(p_end_date1,'DD/MM/YYYY')) ;
8183   debug('p_start_date2: '||to_char(p_start_date2,'DD/MM/YYYY')) ;
8184   debug('p_end_date2: '||to_char(p_end_date2,'DD/MM/YYYY')) ;
8185 
8186   IF ( (p_end_date1 < p_start_date2) OR (p_end_date2 < p_start_date1) ) THEN
8187     debug(l_proc_name, 10);
8188     l_overlap_days := 0;
8189   ELSIF ( (p_end_date1 = p_start_date2) OR (p_end_date2 = p_start_date1) ) THEN
8190     debug(l_proc_name, 20);
8191     l_overlap_days := 1 ;
8192   ELSIF ( (p_start_date1 <= p_start_date2) AND (p_end_date2 <= p_end_date1) ) THEN
8193     debug(l_proc_name, 30);
8194     l_overlap_days := p_end_date2 - p_start_date2 + 1 ;
8195   ELSIF ( (p_start_date2 <= p_start_date1) AND (p_end_date1 <= p_end_date2) ) THEN
8196     debug(l_proc_name, 40);
8197     l_overlap_days := p_end_date1 - p_start_date1 + 1;
8198   ELSIF ( (p_start_date2 <= p_start_date1) AND (p_end_date2 <= p_end_date1) ) THEN
8199     debug(l_proc_name, 50);
8200     l_overlap_days := p_end_date2 - p_start_date1 + 1;
8201   ELSE
8202     debug(l_proc_name, 60);
8203     l_overlap_days := p_end_date1 - p_start_date2 + 1;
8204   END IF;
8205 
8206   debug('l_overlap_days: '|| l_overlap_days, 70);
8207   debug_exit(l_proc_name);
8208 
8209   RETURN abs(l_overlap_days);
8210 
8211 END get_overlap_days;
8212 
8213 
8214 -- PTS: BUG 4135481:
8215 ------<calc_payment_by_run_rslt>--------------
8216 -- function gets the sum of run result values for
8217 -- the assignment for the period
8218 -- p_start_date to p_end_date
8219 -- for the payments earned on p_date_earned (payroll run date)
8220 -- p_val returns the value of payments.
8221 -- Returns -1 in case of error
8222 FUNCTION calc_payment_by_run_rslt
8223                          ( p_assignment_id     IN NUMBER
8224                           ,p_start_date        IN DATE
8225                           ,p_end_date          IN DATE
8226                           ,p_pay_period_start  IN DATE
8227                           ,p_date_earned       IN DATE
8228                           ,p_balance_type_id   IN NUMBER
8229                           ,p_val               OUT NOCOPY NUMBER
8230                           ,p_tab_bal_ele_ids   IN t_ele_ids_from_bal -- 4336613 : OSLA_3A
8231                           ) RETURN NUMBER
8232 IS
8233 
8234 l_proc_name         VARCHAR2(60) := g_proc_name || 'calc_payment_by_run_rslt';
8235 
8236 l_element_type_id   NUMBER;
8237 l_input_val_id      NUMBER;
8238 l_total_val         NUMBER:= 0;
8239 
8240 l_asg_act_id        NUMBER;
8241 l_count             NUMBER; -- Loop counter
8242 
8243 l_overlap_days      NUMBER      := 0;
8244 l_avg_payment       NUMBER      := 0;
8245 l_proration         VARCHAR2(1) :='N' ;
8246 l_balance_val       NUMBER      := 0;
8247 l_balance_type_id   NUMBER;
8248 
8249 --l_rr                csr_get_run_result_value%ROWTYPE;
8250 l_rr1               csr_get_run_results%ROWTYPE;             -- 5403512
8251 l_element_info      csr_get_eet_info%ROWTYPE;
8252 i                     NUMBER :=0;
8253 l_qualify           VARCHAR2(1) := 'Y';
8254 --
8255 BEGIN
8256 
8257   debug_enter(l_proc_name);
8258 
8259   debug('p_start_date :' ||to_char(p_start_date, 'DD/MM/YYYY'),10) ;
8260   debug('p_end_date :' ||to_char(p_end_date, 'DD/MM/YYYY')) ;
8261   debug('p_pay_period_start :' ||to_char(p_pay_period_start, 'DD/MM/YYYY')) ;
8262   debug('p_date_earned :' ||to_char(p_date_earned, 'DD/MM/YYYY')) ;
8263   debug('p_assignment_id :' ||to_char(p_assignment_id)) ;
8264   debug('p_balance_type_id :' ||to_char(p_balance_type_id)) ;
8265   debug('p_tab_bal_ele_ids.COUNT :' ||to_char(p_tab_bal_ele_ids.COUNT)) ;
8266 
8267   -- get the assignment_action_id
8268 
8269   OPEN csr_get_asg_act_id
8270            ( p_assignment_id   => p_assignment_id
8271             ,p_date_earned     => p_date_earned
8272            );
8273   FETCH csr_get_asg_act_id INTO l_asg_act_id;
8274 
8275   IF csr_get_asg_act_id%NOTFOUND THEN
8276     CLOSE csr_get_asg_act_id;
8277     debug('Assignment action id not found....',30);
8278     l_total_val := 0 ;
8279 
8280   ELSE -- csr_get_asg_act_id%NOTFOUND THEN
8281     CLOSE csr_get_asg_act_id;
8282 
8283     -- Run Results exist so get the element Ids
8284     debug('l_asg_act_id: '||to_char(l_asg_act_id),40 );
8285 
8286     -- get_elements from element entries
8287     -- for the assignment
8288     l_count := 1 ; -- Loop counter
8289 
8290 /*    OPEN csr_get_eet_info                               -- 5403512
8291         ( c_assignment_id         => p_assignment_id
8292          ,c_effective_start_date  => p_start_date
8293          ,c_effective_end_date    => p_end_date
8294          ) ;      */
8295  --   LOOP
8296 --      FETCH csr_get_eet_info INTO l_element_info;
8297 --      EXIT WHEN csr_get_eet_info%NOTFOUND;
8298 
8299        -- check if the element is feeding the
8300        -- 'Teachers Supperannuable Salary' Balance'
8301 
8302 --      l_element_type_id := l_element_info.element_type_id;
8303 
8304 
8305       debug('element_type_id :' ||to_char(l_element_type_id),50+l_count/10000) ;
8306 
8307       -- 4336613 : OSLA_3A : check for payment type (OSLA/NULL)
8308       -- IF OSLA, then use g_tab_osla_ele_ids, else use g_tab_sal_ele_ids
8309 
8310 /*        IF p_tab_bal_ele_ids.EXISTS(l_element_type_id) THEN
8311 
8312           debug('input_value_id :' ||
8313                      to_char(p_tab_bal_ele_ids(l_element_type_id).input_value_id),60+l_count/10000) ;   */   -- 5403512
8314 
8315           l_balance_type_id := p_balance_type_id;
8316           -- Get the run_results
8317           -- for this element type id
8318  /*         FOR l_rr IN csr_get_run_result_value        -- 5403512
8319                          (  p_start_date      => p_pay_period_start --p_start_date
8320                            ,p_end_date        => p_date_earned --p_end_date
8321                            ,p_element_type_id => l_element_type_id
8322                            ,p_input_value_id  => p_tab_bal_ele_ids(l_element_type_id).input_value_id
8323                            ,p_asg_act_id      => l_asg_act_id
8324                           )   */
8325           FOR l_rr1 IN csr_get_run_results              -- 5403512
8326 	                 (  p_start_date      => p_pay_period_start --p_start_date
8327                            ,p_end_date        => p_date_earned --p_end_date
8328 			   ,p_asg_act_id      => l_asg_act_id
8329 			   ,p_balance_type_id => l_balance_type_id
8330 			 )
8331           LOOP
8332             debug('l_total_val       :' ||to_char(l_total_val), 70+l_count/10000) ;
8333             debug('l_rr.start_date   :'||to_char(l_rr1.start_date,'DD/MM/YYYY'));
8334             debug('l_rr.end_date     :'||to_char(l_rr1.end_date,'DD/MM/YYYY'));
8335             debug('l_rr.result_value :'||to_char(l_rr1.result));
8336             debug('l_rr.run_result_id       :' ||to_char(l_rr1.run_result_id));
8337 
8338             l_proration := 'Y' ;
8339             l_qualify   := 'Y' ;
8340 
8341             IF g_date_work_mode = 'Y' AND g_supp_teacher = 'Y' THEN
8342               OPEN csr_is_supp_claim(l_rr1.run_result_id,p_start_date,p_end_date);
8343               FETCH csr_is_supp_claim INTO l_qualify;
8344               CLOSE csr_is_supp_claim;
8345               debug('l_qualify       :' ||l_qualify, 79) ;
8346             END IF;
8347               debug('l_qualify       :' ||l_qualify, 80) ;
8348             IF l_qualify = 'Y' THEN
8349             debug(l_proc_name, 80);
8350 
8351             IF (l_rr1.start_date >= p_start_date
8352                AND
8353                l_rr1.end_date <= p_end_date)  THEN
8354 
8355                debug(l_proc_name, 90) ;
8356                l_total_val := l_total_val + l_rr1.result * l_rr1.scale;   -- 5403512
8357 
8358             -- CALC_PT_SAL_OPTIONS: BUG : 4135481
8359             -- perform average calculations if specified
8360             ELSIF g_calendar_avg = 'Y' THEN --(l_rr.start_date >= p_start_date
8361 
8362                debug(l_proc_name, 110) ;
8363                -- need average calculation
8364                -- get the number of days in the period that overlap with the payroll period
8365                l_overlap_days := get_overlap_days
8366                                       (p_start_date1 => p_start_date
8367                                       ,p_end_date1   => p_end_date
8368                                       ,p_start_date2 => l_rr1.start_date
8369                                       ,p_end_date2   => l_rr1.end_date
8370                                       );
8371 
8372                debug('l_overlap_days: '||to_char(l_overlap_days), 120);
8373 
8374                IF l_overlap_days <> 0 THEN
8375                  l_avg_payment := (l_rr1.result*l_overlap_days)/(l_rr1.end_date - l_rr1.start_date + 1 );
8376                  debug('l_avg_payment: '||to_char(l_avg_payment), 130);
8377                  l_total_val   := l_total_val + l_avg_payment * l_rr1.scale;
8378                  debug('l_total_val: '||to_char(l_total_val), 140);
8379                END IF ;
8380 
8381             ELSIF (l_rr1.end_date BETWEEN p_start_date
8382                                      AND p_end_date
8383                    ) THEN -- g_calendar_avg = 'N', direct addition, no average calculation
8384               debug(l_proc_name, 150) ;
8385               l_total_val   := l_total_val + l_rr1.result * l_rr1.scale;
8386             END IF; --(l_rr.start_date >= p_start_date
8387            END IF;
8388             debug('l_total_val :' ||to_char(l_total_val),160) ;
8389           END LOOP;
8390 
8391           debug('l_proration: '||l_proration, 170);
8392           -- There is no proration on this asignment, need to get the
8393           -- payroll balance and average calculation is required.
8394           IF l_proration = 'N' THEN
8395             debug(l_proc_name, 180);
8396 
8397             l_proration := 'Y'; -- 4336613 : ensuring that we pick the balance only once
8398 
8399             --get the balance value as of date earned
8400             -- and get the average value for the number of days in the period.
8401 
8402             l_balance_val := hr_gbbal.calc_asg_proc_ptd_date
8403                             (p_assignment_id   => p_assignment_id
8404                             ,p_balance_type_id => p_balance_type_id
8405                             ,p_effective_date  => p_date_earned
8406                             );
8407 
8408             debug('l_balance_val: '||to_char(l_balance_val), 190);
8409             -- get the number of days in the period that overlap with the payroll period
8410             IF l_balance_val > 0 THEN
8411 
8412               debug(l_proc_name, 210);
8413               l_overlap_days := get_overlap_days
8414                                    (p_start_date1 => p_start_date
8415                                    ,p_end_date1   => p_end_date
8416                                    ,p_start_date2 => p_pay_period_start
8417                                    ,p_end_date2   => p_date_earned
8418                                     );
8419               debug('l_overlap_days: '||to_char(l_overlap_days), 220);
8420               l_avg_payment := (l_balance_val*l_overlap_days)/(p_date_earned - p_pay_period_start + 1 );
8421 
8422             END IF; --l_balance_val > 0 THEN
8423 
8424             debug('l_avg_payment: '||to_char(l_avg_payment), 230);
8425             l_total_val   := l_total_val + l_avg_payment;
8426 
8427           END IF;  --l_proration = 'N'
8428           debug('l_total_val :' ||to_char(l_total_val), 240+l_count/10000) ;
8429 
8430  --       END IF; --p_tab_bal_ele_ids.EXISTS(l_element_type_id) THEN -- 5403512
8431 
8432         debug(l_proc_name, 250);
8433 
8434 --    END LOOP;
8435 --    CLOSE csr_get_eet_info;     -- 5403512
8436 
8437     debug('l_total_val :' ||to_char(l_total_val), 260) ;
8438   END IF; -- csr_get_asg_act_id%NOTFOUND THEN
8439 
8440 
8441   IF l_total_val IS NULL THEN
8442     l_total_val := 0;
8443   END IF;
8444 
8445   p_val := l_total_val ;
8446 
8447   debug_exit(l_proc_name);
8448   RETURN 0 ;
8449 
8450 EXCEPTION
8451     WHEN OTHERS THEN
8452        debug_exit(' Others in '||l_proc_name);
8453        p_val := NULL;
8454        raise;
8455 
8456 END calc_payment_by_run_rslt ;
8457 
8458 
8459 --
8460 --
8461 -- PTS: BUG 4135481:
8462 -----adjust_pre_part_payments------
8463 --
8464 -- The following function is used to get the prorated payments for the
8465 -- period at the begining of the Line of Service
8466 -- IN PARAMS:
8467 -- p_balance_type_id: Balance ID for the balance 'Teachers Supperannuable Salary'
8468 -- OUT PARAMS:
8469 -- p_effective_start_date : Start date for the non prorated payments
8470 -- p_effective_end_date : END date for the non prorated payments
8471 -- p_part_payment : Prorated payment for the period.
8472 
8473 FUNCTION adjust_pre_part_payments(p_assignment_id        IN NUMBER -- in
8474                                  ,p_balance_type_id      IN NUMBER
8475                                  ,p_effective_start_date IN OUT NOCOPY DATE
8476                                  ,p_effective_end_date   IN OUT NOCOPY DATE
8477                                  ,p_part_payment         OUT NOCOPY NUMBER
8478                                  ,p_tab_bal_ele_ids      IN t_ele_ids_from_bal
8479                                  ) RETURN NUMBER
8480 IS
8481 
8482 --
8483   l_proc_name           varchar2(60) := g_proc_name || 'adjust_pre_part_payments';
8484 
8485   -- Required for nocopy
8486   l_nc_effective_start_date DATE ;
8487   l_nc_effective_end_date   DATE ;
8488 
8489   l_return_start_date       DATE;
8490   l_return_end_date         DATE;
8491   l_effective_start_date    DATE;
8492   l_effective_end_date      DATE;
8493   l_temp_end_date           DATE;
8494   l_pre_payroll_date        DATE;
8495   l_pre_part_payment        NUMBER := 0 ;
8496   l_pre_payment_exist       VARCHAR2(1) := NULL ;
8497   l_return                  NUMBER := 0;
8498   l_date_earned             DATE;
8499 
8500   -- RETRO:BUG: 4135481
8501   l_retro_entry             VARCHAR2(1):= 'N' ;
8502   l_count                   NUMBER := 0;
8503   l_retro_dtl               csr_get_date_earned_retro%ROWTYPE;
8504 
8505 
8506 BEGIN
8507   --
8508   debug_enter(l_proc_name);
8509 
8510   debug('p_assignment_id :' ||to_char(p_assignment_id),10) ;
8511   debug('p_balance_type_id :' ||to_char(p_balance_type_id)) ;
8512   debug('p_effective_start_date :' ||to_char(p_effective_start_date, 'DD/MM/YYYY')) ;
8513   debug('p_effective_end_date :' ||to_char(p_effective_end_date, 'DD/MM/YYYY')) ;
8514 
8515 
8516   -- Nocopy changes
8517   l_nc_effective_start_date := p_effective_start_date;
8518   l_nc_effective_end_date   := p_effective_end_date;
8519   --
8520 
8521   -- initialize the two dates,
8522   -- we may return these dates back as it is ..
8523   l_return_start_date := p_effective_start_date;
8524   l_return_end_date   := p_effective_end_date;
8525 
8526   -- Get the recent payroll date
8527   -- and check if there are any prorated periods
8528   -- in the date range
8529   OPEN csr_get_previous_payroll_date
8530          ( p_assignment_id        => p_assignment_id
8531           ,p_effective_start_date => p_effective_start_date
8532          );
8533   FETCH csr_get_previous_payroll_date INTO l_pre_payroll_date;
8534 
8535   IF csr_get_previous_payroll_date%NOTFOUND THEN
8536     -- there is no payroll defined before this date so
8537     -- we can safely assume that this is the start of the period.
8538 
8539     l_pre_payment_exist := 'Y' ;
8540 
8541     l_effective_start_date := p_effective_start_date ;
8542     l_return_start_date    := NULL;  --p_effective_start_date ;
8543 
8544     debug('l_effective_start_date :' ||to_char(l_effective_start_date, 'DD/MM/YYYY'),20) ;
8545 
8546   ELSE  --csr_get_previous_payroll_date%NOTFOUND THEN
8547     debug('l_pre_payroll_date :' ||to_char(l_pre_payroll_date, 'DD/MM/YYYY'),30) ;
8548 
8549     -- if  p_effective_start_date is not start of some payroll period
8550     IF p_effective_start_date > l_pre_payroll_date + 1 THEN
8551        debug(l_proc_name,40) ;
8552        -- Calculate the part payment for start date
8553        l_pre_payment_exist := 'Y' ;
8554        l_effective_start_date := p_effective_start_date;
8555        l_return_start_date    := NULL ; -- nedd to set it later...
8556 
8557        debug('l_effective_start_date :' ||to_char(l_effective_start_date, 'DD/MM/YYYY'),50) ;
8558     ELSE  --p_effective_start_date > l_pre_payroll_date + 1 THEN
8559        debug(l_proc_name, 60);
8560        -- there are no pre_part_payments ..
8561        -- Period start date is the payroll period start date, so we will calculate by
8562        -- using the default logic to get the balance values.
8563        l_pre_payment_exist    := 'N' ;
8564        l_return_start_date    := p_effective_start_date ;
8565        l_return_end_date      := p_effective_end_date ;
8566     END IF  ; -- p_effective_start_date > l_pre_payroll_date + 1 THEN
8567 
8568   END IF;  --sr_get_previous_payroll_date%NOTFOUND THEN
8569 
8570   CLOSE csr_get_previous_payroll_date;
8571 
8572   debug('l_pre_payment_exist :' ||l_pre_payment_exist, 70) ;
8573   debug('l_effective_start_date :' ||to_char(l_effective_start_date, 'DD/MM/YYYY')) ;
8574   debug('l_return_start_date :' ||to_char(l_return_start_date, 'DD/MM/YYYY')) ;
8575 
8576   -- Calculate prorated Period End Date
8577   IF l_pre_payment_exist = 'Y' THEN
8578 
8579     debug(l_proc_name, 80);
8580     -- get the end date of the prorateed period....
8581     OPEN csr_get_pre_end_date (c_assignment_id        => p_assignment_id
8582                               ,c_effective_start_date => l_effective_start_date
8583                               ,c_effective_end_date   => p_effective_end_date
8584                               );
8585     FETCH csr_get_pre_end_date INTO l_temp_end_date;
8586 
8587     IF csr_get_pre_end_date%NOTFOUND THEN
8588       -- the period is less than one payroll period defined.
8589       -- hence no Pyroll defined in that period.
8590 
8591       debug(l_proc_name, 90);
8592       CLOSE csr_get_pre_end_date;
8593 
8594       l_effective_end_date := p_effective_end_date ;
8595       -- theer are no more payroll periods to get the values from.
8596       l_return_end_date := p_effective_end_date;
8597       l_return_start_date := l_effective_start_date;
8598 
8599       debug('l_effective_end_date :' ||to_char(l_effective_end_date, 'DD/MM/YYYY'),110) ;
8600 
8601     ELSE --csr_get_pre_end_date%NOT FOUND THE
8602 
8603 
8604       debug(l_proc_name, 120);
8605       CLOSE csr_get_pre_end_date;
8606 
8607       debug('l_temp_end_date :' ||to_char(l_temp_end_date, 'DD/MM/YYYY'),130) ;
8608 
8609       IF l_temp_end_date = p_effective_end_date THEN
8610         debug(l_proc_name, 135);
8611         -- There are some prorated payments
8612         l_effective_end_date := p_effective_end_date ;
8613         debug('l_effective_end_date :' ||to_char(l_effective_end_date, 'DD/MM/YYYY'),140) ;
8614 
8615         -- No more payments left....
8616         l_return_end_date := NULL;
8617 
8618       ELSIF l_temp_end_date < p_effective_end_date THEN
8619         -- There are some prorated payments  and
8620         -- there are more payments also
8621         debug(l_proc_name, 150);
8622         l_effective_end_date := l_temp_end_date ;
8623         debug('l_effective_end_date :' ||to_char(l_effective_end_date, 'DD/MM/YYYY'),160) ;
8624 
8625         -- More payments left....
8626         -- could be full apyment period or part post payments ...
8627         l_return_start_date := l_effective_end_date + 1;
8628         l_return_end_date   := p_effective_end_date ;
8629 
8630       END IF;
8631 
8632       debug('l_effective_end_date :' ||to_char(l_effective_end_date, 'DD/MM/YYYY'),170) ;
8633 
8634     END IF; -- --csr_get_pre_end_date%NOT FOUND THEN
8635 
8636     debug('l_effective_start_date :' ||to_char(l_effective_start_date, 'DD/MM/YYYY'),180) ;
8637     debug('l_effective_end_date   :' ||to_char(l_effective_end_date, 'DD/MM/YYYY')) ;
8638     debug('l_return_start_date    :' ||to_char(l_return_start_date, 'DD/MM/YYYY')) ;
8639     debug('l_return_end_date      :' ||to_char(l_return_end_date, 'DD/MM/YYYY')) ;
8640 
8641     OPEN csr_get_next_payroll_date
8642            ( p_assignment_id        => p_assignment_id
8643             ,p_effective_start_date => p_effective_start_date
8644             ) ;
8645     FETCH csr_get_next_payroll_date INTO l_date_earned;
8646 
8647     IF csr_get_next_payroll_date%FOUND THEN
8648 
8649       CLOSE csr_get_next_payroll_date;
8650 
8651       debug('l_date_earned :' ||to_char(l_date_earned,'DD/MM/YYYY'),190) ;
8652 
8653       l_return := calc_payment_by_run_rslt
8654                        ( p_assignment_id    => p_assignment_id
8655                         ,p_start_date       => l_effective_start_date --in
8656                         ,p_end_date         => l_effective_end_date --in
8657                         ,p_pay_period_start => l_pre_payroll_date + 1
8658                         ,p_date_earned      => l_date_earned
8659                         ,p_balance_type_id  => p_balance_type_id
8660                         ,p_val              => l_pre_part_payment --out
8661                         ,p_tab_bal_ele_ids  => p_tab_bal_ele_ids -- in -- 4336613 : OSLA_3A
8662                         ) ;
8663       debug('l_return :' ||to_char(l_return),210) ;
8664       debug('l_pre_part_payment :' ||to_char(l_pre_part_payment)) ;
8665 
8666     ELSE
8667       CLOSE csr_get_next_payroll_date;
8668       debug(l_proc_name, 220);
8669     END IF;
8670 
8671 
8672     p_part_payment         := l_pre_part_payment;
8673     p_effective_start_date := l_return_start_date ;
8674     p_effective_end_date   := l_return_end_date ;
8675 
8676     -- RETRO:BUG: 4135481
8677     -- check if there are any retro earnings existing
8678     -- for the period.
8679     -- If there are any raise a warning.
8680     -- Still exploring the possibility to fix this.
8681     -- so that we can show the actual payments made in the period
8682 
8683     l_count := 1 ;
8684     FOR l_retro_dtl IN csr_get_date_earned_retro
8685                            (p_assignment_id => p_assignment_id
8686                            ,p_start_date    => l_pre_payroll_date + 1
8687                            ,p_end_date      => l_date_earned
8688                            )
8689     LOOP
8690       l_retro_entry := 'Y';
8691       debug('|-----------------------------------------------|', 230+l_count/10000);
8692       debug('element_entry_id    : '||to_char(l_retro_dtl.element_entry_id));
8693       debug('element_type_id     : '||to_char(l_retro_dtl.element_type_id));
8694       debug('creator_type        : '||l_retro_dtl.creator_type);
8695       debug('effective_start_date: '||to_char(l_retro_dtl.effective_start_date,'DD/MM/YYYY'));
8696       debug('effective_end_date  : '||to_char(l_retro_dtl.effective_end_date,'DD/MM/YYYY'));
8697       debug('source_start_date   : '||to_char(l_retro_dtl.source_start_date,'DD/MM/YYYY'));
8698       debug('source_end_date     : '||to_char(l_retro_dtl.source_end_date,'DD/MM/YYYY'));
8699       debug('date_earned         : '||to_char(l_retro_dtl.date_earned,'DD/MM/YYYY'));
8700       debug('|-----------------------------------------------|', 240+l_count/10000);
8701       l_count := l_count + 1 ;
8702     END LOOP;
8703 
8704     /*    IF (l_retro_entry = 'Y') THEN                        -- changed to remove the warning.
8705       debug('RAISE A WARNING FOR RETRO ENTRIES.......', 250);
8706       -- Find the retro payments here and show them along
8707       -- with the warning message
8708       -- Still investigating the possibility to show the
8709       -- correct payments if both Proration and Retro are
8710       -- enabled and applied on the period.
8711 
8712 
8713       -- Set the global here to raise a warning from recalc_data_element
8714       -- can not raise a warning from this place,
8715       -- as we don't have the primary assignment id
8716       -- and this function can be called multiple times
8717       -- from Salary Rate/Days Excluded/parttime sal paid functions.
8718 
8719       g_raise_retro_warning := 'Y' ;
8720     END IF;     */
8721 
8722 
8723   ELSE --l_pre_payment_exist = 'Y' THEN
8724     debug(l_proc_name, 260);
8725 
8726     p_effective_start_date := l_return_start_date ;
8727     p_effective_end_date   := l_return_end_date ;
8728     p_part_payment         := 0;
8729 
8730   END IF ; --l_pre_payment_exist = 'Y' THEN
8731 
8732   debug_exit(l_proc_name);
8733 
8734   RETURN l_return ;
8735 
8736 EXCEPTION
8737     WHEN OTHERS THEN
8738        debug_exit(' Others in '||l_proc_name);
8739        p_effective_start_date := l_nc_effective_start_date  ;
8740        p_effective_end_date   := l_nc_effective_end_date ;
8741        p_part_payment         := 0;
8742        raise;
8743 
8744 END adjust_pre_part_payments;
8745 
8746 
8747 -- PTS: BUG 4135481:
8748 -----adjust_post_part_payments------
8749 --
8750 -- The following function is used to get
8751 -- the prorated payments for the period at
8752 -- the end of the Line of Service . The function
8753 -- checks first if the assigment is terminated.If
8754 -- it is then it fetches the terminated pay amount
8755 -- else it goes and calculates prorated payments
8756 -- at the end of Lines of service.
8757 
8758 FUNCTION adjust_post_part_payments(p_assignment_id        IN NUMBER
8759                                   ,p_balance_type_id      IN NUMBER
8760                                   ,p_effective_start_date IN DATE
8761                                   ,p_effective_end_date   IN DATE
8762                                   ,p_part_payment         OUT NOCOPY NUMBER
8763                                   ,p_tab_bal_ele_ids      IN t_ele_ids_from_bal
8764                                  ) RETURN NUMBER
8765 IS
8766 
8767 --
8768   l_proc_name             VARCHAR2(60) := g_proc_name || 'adjust_post_part_payments';
8769   l_post_part_payment     NUMBER := 0;
8770   l_return                NUMBER := 0;
8771   l_date_earned           DATE;
8772   --TERM_LSP:BUG :4135481 Check for terminated employees
8773   l_is_terminated         VARCHAR2(1) := 'N';
8774 
8775   -- RETRO:BUG: 4135481
8776   l_retro_entry           VARCHAR2(1):= 'N' ;
8777   l_count                 NUMBER := 0;
8778   l_retro_dtl             csr_get_date_earned_retro%ROWTYPE;
8779 --
8780 BEGIN
8781   --
8782   debug_enter(l_proc_name);
8783 
8784   debug('p_assignment_id       :' ||to_char(p_assignment_id),10) ;
8785   debug('p_balance_type_id     :' ||to_char(p_balance_type_id)) ;
8786   debug('p_effective_start_date:' ||to_char(p_effective_start_date, 'DD/MM/YYYY')) ;
8787   debug('p_effective_end_date  :' ||to_char(p_effective_end_date, 'DD/MM/YYYY')) ;
8788   debug('g_terminated_person   :' || g_terminated_person) ;
8789 
8790 
8791    -- check if period length is not ZERO days.
8792    IF( (p_effective_start_date IS NULL) -- no period exists
8793         OR ( (p_effective_start_date IS NOT NULL)
8794              AND (p_effective_start_date > p_effective_end_date)
8795            )
8796      )THEN
8797      debug(l_proc_name,30) ;
8798      -- there are no post_part_payments
8799      -- so return 0
8800      l_post_part_payment := 0 ;
8801 
8802    ELSE
8803      -- get the part_payments from run_results
8804      debug(l_proc_name,40) ;
8805 
8806      OPEN csr_get_next_payroll_date
8807           ( p_assignment_id        => p_assignment_id
8808            ,p_effective_start_date => p_effective_start_date
8809            ) ;
8810      FETCH csr_get_next_payroll_date INTO l_date_earned;
8811 
8812      IF csr_get_next_payroll_date%FOUND THEN
8813 
8814        CLOSE csr_get_next_payroll_date;
8815 
8816        debug('l_date_earned :' ||to_char(l_date_earned,'DD/MM/YYYY'),50) ;
8817 
8818        l_return := calc_payment_by_run_rslt
8819                         ( p_assignment_id    => p_assignment_id
8820                          ,p_start_date       => p_effective_start_date -- in
8821                          ,p_end_date         => p_effective_end_date   -- in
8822                          ,p_pay_period_start => p_effective_start_date -- for Avg calc ...
8823                          ,p_date_earned      => l_date_earned
8824                          ,p_balance_type_id  => p_balance_type_id
8825                          ,p_val              => l_post_part_payment --out
8826                          ,p_tab_bal_ele_ids  => p_tab_bal_ele_ids -- in -- 4336613 : OSLA_3A
8827                          ) ;
8828        debug('l_return :' ||to_char(l_return),60) ;
8829        debug('l_post_part_payment :' ||to_char(l_post_part_payment)) ;
8830 
8831 
8832        -- RETRO:BUG: 4135481
8833        -- check if there are any retro earnings existing
8834        -- for the period
8835        -- If there are any raise a warning.
8836        -- exploring the possibility to fix this.
8837        -- so that we can show the actual payments made in the period
8838        --
8839        l_count := 1 ;
8840        FOR l_retro_dtl IN csr_get_date_earned_retro
8841                            (p_assignment_id => p_assignment_id
8842                            ,p_start_date    => p_effective_start_date
8843                            ,p_end_date      => l_date_earned
8844                            )
8845        LOOP
8846          l_retro_entry := 'Y';
8847          debug('|-----------------------------------------------|', 70+l_count/10000);
8848          debug('element_entry_id    : '||to_char(l_retro_dtl.element_entry_id));
8849          debug('element_type_id     : '||to_char(l_retro_dtl.element_type_id));
8850          debug('creator_type        : '||l_retro_dtl.creator_type);
8851          debug('effective_start_date: '||to_char(l_retro_dtl.effective_start_date,'DD/MM/YYYY'));
8852          debug('effective_end_date  : '||to_char(l_retro_dtl.effective_end_date,'DD/MM/YYYY'));
8853          debug('source_start_date   : '||to_char(l_retro_dtl.source_start_date,'DD/MM/YYYY'));
8854          debug('source_end_date     : '||to_char(l_retro_dtl.source_end_date,'DD/MM/YYYY'));
8855          debug('date_earned         : '||to_char(l_retro_dtl.date_earned,'DD/MM/YYYY'));
8856          debug('|-----------------------------------------------|', 210+l_count/10000);
8857          l_count := l_count + 1 ;
8858        END LOOP;
8859 
8860        /*     IF (l_retro_entry = 'Y') THEN                         -- Changed to remove the warning.
8861          debug('......raise a warning for retro entries.......',80);
8862          -- Find the retro payments here and show them along
8863          -- with the warning message
8864          -- Still investigating the possibility to show the
8865          -- correct payments if both Proration and Retro are
8866          -- enabled and applied on the period.
8867 
8868          -- Set the global here to raise a warning from recalc_data_element.
8869          -- Can not raise a warning from this place,
8870          -- as we don't have the primary assignment id
8871          -- and this function can be called multiple times
8872          -- from Salary Rate/Days Excluded/parttime sal paid functions.
8873 
8874          g_raise_retro_warning := 'Y' ;
8875 
8876        END IF;    */
8877 
8878 
8879      ELSE
8880        CLOSE csr_get_next_payroll_date;
8881        debug(l_proc_name, 90);
8882      END IF;
8883 
8884 
8885    END IF ; --p_effective_start_date > p_effective_end_date THEN
8886 
8887 
8888   debug(l_proc_name,110) ;
8889 
8890   p_part_payment   := l_post_part_payment;
8891 
8892   debug_exit(l_proc_name);
8893 
8894   RETURN l_return ;
8895 
8896 END adjust_post_part_payments;
8897 
8898 
8899 
8900 
8901 
8902 
8903 -- CALC_PT_SAL_OPTIONS: BUG : 4135481
8904 -- this function is the older implementation of calc_part_time_sal
8905 -- this is used when g_calc_sal_new is 'N'
8906 -- ----------------------------------------------------------------------------
8907 -- |------------------------< calc_part_time_sal_old >----------------------------|
8908 -- ----------------------------------------------------------------------------
8909 function calc_part_time_sal_old (p_assignment_id           in     number
8910                                 ,p_effective_start_date    in     date
8911                                 ,p_effective_end_date      in     date
8912                                 ,p_business_group_id       in     number
8913                                 ,p_next_payroll_start_date out    nocopy date -- new parameter to track
8914                                                                 --last complete payroll run date
8915                                 ,p_sal_bal_type_id         in     number -- 4336613 : OSLA_3A
8916                                 )
8917   return number is
8918 --
8919   l_proc_name           varchar2(60) := g_proc_name || 'calc_part_time_sal_old';
8920   l_effective_date      date;
8921   l_total_part_time_sal number := 0;
8922   l_part_time_sal       number := 0;
8923   l_supp_claim          number := 0;
8924 --
8925 begin
8926   --
8927   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
8928   --
8929   debug_enter(l_proc_name);
8930 
8931   debug('p_assignment_id :' ||to_char(p_assignment_id),10) ;
8932   debug('p_effective_start_date :' ||to_char(p_effective_start_date, 'DD/MM/YYYY'),20) ;
8933   debug('p_effective_end_date :' ||to_char(p_effective_end_date, 'DD/MM/YYYY'),30) ;
8934   debug('p_sal_bal_type_id :' ||to_char(p_sal_bal_type_id),35) ;
8935 
8936   open csr_get_end_date (c_assignment_id        => p_assignment_id
8937                         ,c_effective_start_date => p_effective_start_date
8938                         ,c_effective_end_date   => p_effective_end_date
8939                         );
8940   loop
8941 
8942     fetch csr_get_end_date into l_effective_date;
8943     exit when csr_get_end_date%notfound;
8944 
8945       --
8946       debug(l_proc_name, 40);
8947       --
8948       debug('l_effective_date :'||to_char(l_effective_date, 'DD/MM/YYYY'),50) ;
8949 
8950       l_part_time_sal := hr_gbbal.calc_asg_proc_ptd_date
8951                           (p_assignment_id   => p_assignment_id
8952                           -- 4336613 : OSLA_3A : changed from g_sal_bal_type_id
8953                           ,p_balance_type_id => p_sal_bal_type_id
8954                           ,p_effective_date  => l_effective_date
8955                           );
8956       debug('l_part_time_sal :'||to_char(l_part_time_sal),60) ;
8957       l_total_part_time_sal := l_total_part_time_sal + l_part_time_sal;
8958 
8959   end loop;
8960 
8961   close csr_get_end_date;
8962 
8963 /*
8964       IF g_date_work_mode = 'Y' AND g_supp_teacher = 'Y' THEN
8965         OPEN csr_get_supp_ded(p_sal_bal_type_id,p_assignment_id,p_effective_start_date,l_effective_date);
8966         FETCH csr_get_supp_ded INTO l_supp_claim;
8967         CLOSE csr_get_supp_ded;
8968         debug('l_supp_claim :'||to_char(l_supp_claim),51);
8969         l_total_part_time_sal := l_total_part_time_sal - l_supp_claim;
8970         debug('l_total_part_time_sal after supply claims deduction:'||to_char(l_total_part_time_sal),52) ;
8971       END IF;
8972 */
8973   --We now need to set date for terminated payments
8974   --If no complete payroll exist for that person
8975   -- eg person assignment starts on 1-apr-2000 and terminates b4 30-apr-2000
8976   -- in such a case, we set the last_payroll_date = effective_start_date of assignment
8977 
8978   -- else we set the last_payroll_date = the next day of the last complete payroll end date
8979   -- eg person starts 01-apr2000 and terminates on say 15-jun-2000.
8980   -- Then we set last_payroll_date = 31-may-2000(last complete payrol run date) + 1 = 01-jun-2000
8981 
8982   -- BUG : 4273915
8983   IF l_effective_date IS NOT NULL THEN
8984     debug(l_proc_name, 70);
8985     p_next_payroll_start_date := l_effective_date + 1;
8986   ELSE
8987     -- no complete payroll periods
8988     debug(l_proc_name, 80);
8989     p_next_payroll_start_date := p_effective_start_date;
8990   END IF;
8991 
8992   debug('l_total_part_time_sal :'||to_char(l_total_part_time_sal),90);
8993   --
8994   -- hr_utility.set_location('Leaving: '||l_proc_name, 30);
8995   --
8996   debug_exit(l_proc_name);
8997 
8998   return l_total_part_time_sal;
8999 
9000 EXCEPTION
9001   WHEN OTHERS THEN
9002     debug_exit(' Others in '||l_proc_name);
9003     RAISE;
9004 END calc_part_time_sal_old;
9005 
9006 
9007 
9008 -- ----------------------------------------------------------------------------
9009 -- |------------------------< calc_part_time_sal >----------------------------|
9010 -- ----------------------------------------------------------------------------
9011 function calc_part_time_sal (p_assignment_id        in     number
9012                             ,p_effective_start_date in     date
9013                             ,p_effective_end_date   in     date
9014                             ,p_business_group_id    in     number
9015                             ,p_sal_bal_type_id      IN     NUMBER DEFAULT NULL --4336613 : OSLA_3A
9016                             ,p_cl_bal_type_id       IN     NUMBER DEFAULT NULL
9017                             ,p_tab_bal_ele_ids      IN     t_ele_ids_from_bal DEFAULT g_tab_sal_ele_ids
9018                             )
9019   return number is
9020 --
9021   l_proc_name            VARCHAR2(60) := g_proc_name || 'calc_part_time_sal';
9022   l_effective_date       DATE;
9023   l_total_part_time_sal  NUMBER := 0;
9024   l_part_time_sal        NUMBER := 0;
9025   l_cl_sal               NUMBER := 0;
9026   l_total_cl_sal         NUMBER := 0;
9027   l_effective_start_date DATE ;
9028   l_effective_end_date   DATE ;
9029   l_period_start_date    DATE ;                 -- rahul supply
9030   l_period_end_date      DATE ;                 -- rahul supply
9031   l_return               NUMBER;
9032 
9033   l_pre_part_payment     NUMBER :=0;
9034   l_post_part_payment    NUMBER :=0;
9035   l_count                NUMBER; -- Loop counter
9036 
9037   l_terminated_payment   NUMBER :=0;
9038 
9039   --TERM_LSP:BUG :4135481 Check for terminated employees
9040   l_is_terminated        VARCHAR2(1) := 'N';
9041 
9042   -- RETRO:BUG: 4135481
9043   l_asg_act_dtl          csr_get_asg_act_id_retro%ROWTYPE;
9044   l_error                NUMBER;
9045 
9046   l_next_payroll_start_date    DATE;
9047 
9048   -- 4336613 : OSLA_3A
9049   l_sal_bal_type_id      NUMBER :=0;
9050   l_cl_bal_type_id       NUMBER :=0;
9051   i                      NUMBER :=0;
9052 
9053   l_supp_claim           NUMBER := 0;
9054 --
9055 begin
9056 
9057   debug_enter(l_proc_name);
9058 
9059   debug('p_assignment_id: '||to_char(p_assignment_id), 10);
9060   debug('p_effective_start_date: '||to_char(p_effective_start_date));
9061   debug('p_effective_end_date: '||to_char(p_effective_end_date));
9062   debug('p_business_group_id: '||to_char(p_business_group_id));
9063   debug('p_sal_bal_type_id: '||to_char(p_sal_bal_type_id));
9064 
9065   -- PERF_ENHANC_3A : Performance Enhancements
9066   -- check if record corresponding to p_assignment_id is present in the
9067   -- collection g_asg_recalc_details.
9068   -- If yes, check for matching start_date (a double check,although not necessary)
9069   -- If part_time_sal_paid has been calculated before, then the row
9070   -- will contain the value,return it.
9071   -- If part_time_sal_paid for this assignment has not been calculated before,
9072   -- compute it, store it in a row for this assignment_id and return it
9073   -- This step is to avoid recomputing the value for a given LOS. Originally,
9074   -- calculations were repeated for each data element
9075 
9076 
9077   IF (p_sal_bal_type_id IS NULL -- 4336613 : OSLA_3A : if NULL implies we need to calculate
9078                                 -- PT sal paid, and not OSLA
9079       AND
9080       g_asg_recalc_details.EXISTS(p_assignment_id) -- check if row exists
9081       AND
9082       g_asg_recalc_details(p_assignment_id).eff_start_date = p_effective_start_date
9083       AND
9084       -- check below to find if PT sal paid has been calculated before
9085       g_asg_recalc_details(p_assignment_id).part_time_sal_paid IS NOT NULL
9086       ) THEN
9087 
9088     debug(l_proc_name, 30);
9089     l_total_part_time_sal := g_asg_recalc_details(p_assignment_id).part_time_sal_paid;
9090     debug('l_total_part_time_sal is already present !! '||l_total_part_time_sal, 40);
9091 
9092   ELSE -- calc_part_time_paid has to be computed for this assignment_id for this LOS
9093 
9094     debug(l_proc_name, 50);
9095 
9096     IF p_sal_bal_type_id IS NULL THEN -- calculate PT sal paid, and not OSLA
9097       debug(l_proc_name, 60);
9098       l_sal_bal_type_id := g_sal_bal_type_id(p_business_group_id);
9099       l_cl_bal_type_id  := g_cl_bal_type_id(p_business_group_id);
9100     ELSE
9101       debug(l_proc_name, 70);
9102       l_sal_bal_type_id := p_sal_bal_type_id; -- pick up from the parameter
9103       l_cl_bal_type_id  := p_cl_bal_type_id;
9104     END IF;
9105 
9106     debug('l_sal_bal_type_id : '||l_sal_bal_type_id, 80);
9107 
9108 
9109     --CALC_PT_SAL_OPTIONS: BUG : 4135481
9110     -- check if calc_part_time_sal OR calc_part_time_sal_old is to be used
9111   /*
9112   IF g_calc_sal_new <> 'Y' AND
9113        (pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number <> '0966' OR
9114        g_date_work_mode <> 'Y') THEN -- switch is YES for old method
9115    */
9116     IF g_calc_sal_new <> 'Y' THEN
9117       debug(l_proc_name, 90);
9118 
9119       l_total_part_time_sal := calc_part_time_sal_old
9120                                (p_assignment_id           => p_assignment_id
9121                                ,p_effective_start_date    => p_effective_start_date
9122                                ,p_effective_end_date      => p_effective_end_date
9123                                ,p_business_group_id       => g_business_group_id
9124                                ,p_next_payroll_start_date => l_next_payroll_start_date --BUG : 4273915
9125                                                           -- new parameter (last complete payroll run date)
9126                                ,p_sal_bal_type_id         => l_sal_bal_type_id -- 4336613 : OSLA_3A
9127                                );
9128 
9129       debug('l_total_part_time_sal :'||l_total_part_time_sal, 110);
9130       debug('l_next_payroll_start_date:'|| l_next_payroll_start_date);
9131 
9132 
9133     -- BUG : 4273915
9134     -- We now need to test for terminated person case
9135     -- If no complete payroll exist for that person
9136     -- eg person assignment starts on 1-apr-2000 and terminates b4 30-apr-2000
9137     -- in such a case, we set the last_payroll_date = effective_start_date of assignment
9138 
9139     -- else we set the last_payroll_date = the next day of the last complete payroll end date
9140     -- eg person starts 01-apr2000 and terminates on say 15-jun-2000.
9141     -- Then we set last_payroll_date = 31-may-2000(last complete payrol run date) + 1 = 01-jun-2000
9142 
9143 
9144       --Check for terminated employees
9145       IF g_terminated_person = 'Y' THEN
9146 
9147         debug(l_proc_name, 120);
9148 
9149         l_is_terminated := get_terminated_payments
9150                          ( p_assignment_id        => p_assignment_id
9151                           ,p_effective_start_date => l_next_payroll_start_date
9152                           ,p_effective_end_date   => p_effective_end_date
9153                           ,p_business_group_id    => g_ext_asg_details(p_assignment_id).business_group_id
9154                           ,p_part_payment         => l_terminated_payment
9155                           ,p_balance_type_id      => l_sal_bal_type_id -- 4336613 : OSLA_3A
9156                           ) ;
9157 
9158         l_total_part_time_sal := l_total_part_time_sal + l_terminated_payment;
9159         debug('l_is_terminated :'|| l_is_terminated ,130);
9160         debug('l_post_part_payment :'|| to_char(l_post_part_payment));
9161         debug('l_total_part_time_sal :'|| to_char(l_total_part_time_sal) );
9162 
9163       END IF;
9164 
9165 
9166     ELSE -- g_calc_sal_new = 'Y', use the new implementation of calc_part_time_sal
9167 
9168       debug('p_assignment_id :' ||to_char(p_assignment_id),140) ;
9169       debug('p_effective_start_date :' ||to_char(p_effective_start_date, 'DD/MM/YYYY')) ;
9170       debug('p_effective_end_date :' ||to_char(p_effective_end_date, 'DD/MM/YYYY')) ;
9171 
9172       l_effective_end_date   := p_effective_end_date ;
9173       l_effective_start_date := p_effective_start_date ;
9174 
9175       -- PTS: BUG 4135481: get the total salary payments in THREE parts
9176       -- (1) Pre payments : any prorated payment in the begining of the period
9177       -- (2) Payments for Full Payroll Periods
9178       -- (3) Post Payment: any prorated payments at the end of the period.
9179 
9180 
9181       --If the person is not terminated then his salary is calculated as per
9182       --regular post_part_payment method.
9183       IF g_proration = 'Y'
9184       THEN
9185       debug('g_proration is set to Y',299);
9186       debug('g_ext_asg_details(p_assignment_id).location_id  : ' || to_char(g_ext_asg_details(p_assignment_id).location_id),1212);
9187       debug('pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number :'|| pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number, 300) ;
9188       debug('g_date_work_mode is set to ' || g_date_work_mode,299);
9189  --     IF pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number <> '0966' OR g_date_work_mode <> 'Y' THEN
9190         debug(l_proc_name, 150);
9191         -- get the pre payments here. (if any)
9192         -- the function returns the modified dates for the Full Pyroll Periods
9193         -- these will be used to get the Full Payroll Period payments.
9194         l_return := adjust_pre_part_payments
9195                              ( p_assignment_id        => p_assignment_id -- in
9196                               ,p_balance_type_id      => l_sal_bal_type_id -- 4336613 : OSLA_3A : g_sal_bal_type_id(p_business_group_id)
9197                               ,p_effective_start_date => l_effective_start_date --in/out
9198                               ,p_effective_end_date   => l_effective_end_date  -- in/out
9199                               ,p_part_payment         => l_pre_part_payment -- out
9200                               ,p_tab_bal_ele_ids      => p_tab_bal_ele_ids -- in -- 4336613 : OSLA_3A
9201                               );
9202 
9203         debug('l_return :'||to_char(l_return),160) ;
9204         debug('l_pre_part_payment :'||to_char(l_pre_part_payment)) ;
9205         debug('l_effective_start_date :'||to_char(l_effective_start_date, 'DD/MM/YYYY')) ;
9206         debug('l_effective_end_date :'||to_char(l_effective_end_date, 'DD/MM/YYYY')) ;
9207  --     END IF;
9208      END IF;
9209       -- 4336613 : moved code to fetch defined balance id to global settings.
9210 
9211       l_count := 1;
9212   --    IF pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number = '0966' and g_date_work_mode = 'Y' THEN
9213      --ELSE
9214       debug('Calling csr_get_asg_act_id_retro',302);
9215 
9216       OPEN csr_get_asg_act_id_retro
9217                    (p_assignment_id        => p_assignment_id
9218                    ,p_effective_start_date => l_effective_start_date
9219                    ,p_effective_end_date   => l_effective_end_date
9220                     );
9221       LOOP
9222 
9223       FETCH csr_get_asg_act_id_retro into l_asg_act_dtl;
9224       EXIT WHEN csr_get_asg_act_id_retro%NOTFOUND;
9225       debug('calling get value for date earned mode',303);
9226       debug('Defined balance id = '||to_char(g_def_bal_id(l_sal_bal_type_id)),304);
9227 
9228       debug('assignment_action_id: '||to_char(l_asg_act_dtl.assignment_action_id), 170+l_count/10000);
9229       debug('date_earned: '||to_char(l_asg_act_dtl.date_earned,'DD/MM/YYYY'));
9230       l_part_time_sal := pay_balance_pkg.get_value
9231                          ( p_defined_balance_id   => g_def_bal_id(l_sal_bal_type_id)
9232                           ,p_assignment_action_id => l_asg_act_dtl.assignment_action_id
9233                          ) ;
9234 
9235       debug('l_part_time_sal :'||to_char(l_part_time_sal), 180+l_count/10000) ;
9236 
9237       l_count := l_count + 1 ;
9238       l_effective_date := l_asg_act_dtl.date_earned;
9239       l_total_part_time_sal := l_total_part_time_sal + l_part_time_sal;
9240       debug('l_total_part_time_sal :'||to_char(l_total_part_time_sal),190+l_count/10000) ;
9241 
9242       END loop;
9243       CLOSE csr_get_asg_act_id_retro;
9244 
9245     /*
9246       IF g_date_work_mode = 'Y' AND g_supp_teacher = 'Y' THEN
9247         OPEN csr_get_supp_ded(l_sal_bal_type_id,p_assignment_id,l_effective_start_date,l_effective_end_date);
9248         FETCH csr_get_supp_ded INTO l_supp_claim;
9249         CLOSE csr_get_supp_ded;
9250         debug('l_supp_claim :'||to_char(l_supp_claim),208);
9251         l_total_part_time_sal := l_total_part_time_sal - l_supp_claim;
9252         debug('l_total_part_time_sal after supply claims deduction:'||to_char(l_total_part_time_sal),209) ;
9253       END IF;
9254     */
9255 
9256      -- END IF;
9257 
9258 
9259      debug('l_total_part_time_sal :'||to_char(l_total_part_time_sal),210) ;
9260      debug('.....retro payments calc over.....') ;
9261 
9262      debug('l_effective_date :'||to_char(l_effective_date, 'DD/MM/YYYY'),220) ;
9263 
9264 
9265       -- PTS:BUG 4135481:  get the post payments here. (if any)
9266       IF l_effective_date IS NOT NULL THEN
9267 
9268         debug(l_proc_name, 230);
9269       -- there were few Full payroll periods
9270       -- so the start for the for post payment is the end of Full Payroll Periods.
9271         l_effective_date := l_effective_date + 1; -- next date of he last payroll date
9272 
9273       ELSE -- l_effective_date IS NOT NULL THEN
9274            -- it implies that there are no complete Payroll Periods
9275            -- so get the Post payments (if any)
9276            -- for the remaining days in the period.
9277         debug(l_proc_name, 240);
9278         l_effective_date := l_effective_start_date;
9279 
9280       END IF;
9281 
9282       debug('l_effective_start_date :' ||to_char(l_effective_start_date, 'DD/MM/YYYY'),250) ;
9283       debug('p_effective_end_date :' ||to_char(p_effective_end_date, 'DD/MM/YYYY')) ;
9284 
9285 
9286       --CALC_PT_SAL_OPTIONS: BUG : 4135481
9287       --TERM_LSP:BUG :4135481 Check for terminated employees
9288       --if the global is set to Y  then check if the person is terminated
9289       IF g_terminated_person = 'Y' THEN
9290 
9291         debug(l_proc_name, 260);
9292 
9293         l_is_terminated := get_terminated_payments
9294                          ( p_assignment_id        => p_assignment_id
9295                           ,p_effective_start_date => l_effective_date
9296                           ,p_effective_end_date   => p_effective_end_date
9297                           ,p_business_group_id    => g_ext_asg_details(p_assignment_id).business_group_id
9298                           ,p_part_payment         => l_terminated_payment
9299                           ,p_balance_type_id      => l_sal_bal_type_id -- 4336613 : OSLA_3A : g_sal_bal_type_id(p_business_group_id)
9300                           );
9301         debug('l_is_terminated :'|| l_is_terminated ,270);
9302       END IF;
9303 
9304 
9305       --If the person is not terminated then his salary is calculated as per
9306       --regular post_part_payment method.
9307 
9308       -- Extra condition has been added for bug 7269761
9309       -- In case of start date and end date are on same date calculation of
9310       -- salary for that day is done by prepayments part and no need to run post payments part for that day
9311       IF l_is_terminated = 'N' AND g_proration = 'Y' AND l_effective_date <> p_effective_end_date
9312       THEN
9313 
9314         debug(l_proc_name, 280);
9315         l_return := adjust_post_part_payments
9316                             ( p_assignment_id       => p_assignment_id
9317                             ,p_balance_type_id      => l_sal_bal_type_id -- 4336613 : OSLA_3A : parameter based
9318                             ,p_effective_start_date => l_effective_date
9319                             ,p_effective_end_date   => p_effective_end_date
9320                             ,p_part_payment         => l_post_part_payment -- out
9321                             ,p_tab_bal_ele_ids      => p_tab_bal_ele_ids -- in -- 4336613 : OSLA_3A
9322                             );
9323         debug('l_return :' ||to_char(l_return),290) ;
9324         debug('l_post_part_payment :' ||to_char(l_post_part_payment)) ;
9325       END IF;
9326 
9327 
9328       debug('l_pre_part_payment :'||to_char(l_pre_part_payment),310);
9329       debug('l_total_part_time_sal :'||to_char(l_total_part_time_sal));
9330       debug('l_post_part_payment :'||to_char(l_post_part_payment));
9331 
9332       -- PTS: BUG 4135481:  add all the payments to get the final payments
9333       l_total_part_time_sal := l_pre_part_payment + l_total_part_time_sal
9334                                + l_post_part_payment + l_terminated_payment;
9335 
9336     END IF; -- end of check if calc_part_time_sal OR calc_part_time_sal_old is to be used
9337 
9338       IF g_date_work_mode = 'Y' THEN
9339       debug('Calling csr_get_asg_act_id_dw',302);
9340 	 OPEN csr_get_asg_act_id_dw
9341               (p_assignment_id        => p_assignment_id
9342                ,p_effective_start_date => p_effective_start_date
9343                ,p_effective_end_date   => p_effective_end_date
9344               );
9345           FETCH csr_get_asg_act_id_dw into l_asg_act_dtl;
9346          CLOSE csr_get_asg_act_id_dw;
9347 
9348         OPEN csr_get_dw_value(l_cl_bal_type_id,
9349                               l_asg_act_dtl.assignment_action_id,
9350                               p_effective_start_date,
9351                               p_effective_end_date
9352                              );
9353         FETCH csr_get_dw_value INTO l_cl_sal;
9354         CLOSE csr_get_dw_value;
9355 
9356 	l_total_cl_sal := l_cl_sal;
9357         l_effective_date := p_effective_end_date;
9358         l_total_part_time_sal := l_total_part_time_sal +  l_total_cl_sal;
9359       END IF;
9360 
9361     IF l_total_part_time_sal IS NULL THEN
9362       debug(l_proc_name, 320);
9363       l_total_part_time_sal := 0;
9364     END IF;
9365 
9366     debug('l_total_part_time_sal :'||to_char(l_total_part_time_sal),330);
9367 
9368 
9369     IF p_sal_bal_type_id IS NULL THEN -- 4336613 : OSLA_3A : check if OSLA or PT sal paid
9370                                       -- IF null => PT sal paid is being computed
9371       debug(l_proc_name, 340);
9372       -- PERF_ENHANC_3A : performance enhancements
9373       -- computed part_time_sal_paid value being stored in the collection for future use
9374       g_asg_recalc_details(p_assignment_id).part_time_sal_paid := l_total_part_time_sal;
9375       debug('l_total_part_time_sal (1st time computation) :'||l_total_part_time_sal, 350);
9376 
9377     END IF;
9378     debug(l_proc_name, 350);
9379   END IF; -- IF (g_asg_recalc_details.EXISTS.... )
9380 
9381     debug(l_proc_name, 360);
9382     debug_exit(l_proc_name);
9383 
9384     return l_total_part_time_sal;
9385 
9386 EXCEPTION
9387   WHEN OTHERS THEN
9388     debug_exit(' Others in '||l_proc_name);
9389     RAISE;
9390 END calc_part_time_sal;--
9391 
9392 
9393 -- 4336613 : OSLA_3A : GET_GROSSED_OSLA_PAYMENTS
9394 -- calculates the OSLA payments for a LOS, and grosses it up for the year.
9395 --
9396 -- ----------------------------------------------------------------------------
9397 -- |------------------------< get_grossed_osla_payments >----------------------------|
9398 -- ----------------------------------------------------------------------------
9399 function get_grossed_osla_payments (p_assignment_id        in     number
9400                                    ,p_effective_start_date in     date
9401                                    ,p_effective_end_date   in     date
9402                                    ,p_business_group_id    in     number
9403                                     )
9404   return number is
9405 --
9406   l_proc_name            VARCHAR2(60) := g_proc_name || 'get_grossed_osla_payments';
9407   l_grossed_osla_payment NUMBER := 0;
9408   l_return               NUMBER;
9409   l_number_of_days number; --Added for the Bug: 13243183
9410 --
9411 begin
9412 
9413   debug_enter(l_proc_name);
9414   debug('p_effective_start_date: '||to_char(p_effective_start_date), 10);
9415   debug('p_effective_end_date: '||to_char(p_effective_end_date));
9416 
9417   debug('TOTAL number of OSLA elements: '|| to_char(g_tab_osla_ele_ids.COUNT), 20);
9418   debug('TOTAL number of OSLA Claims elements: '|| to_char(g_tab_osla_cl_ele_ids.COUNT), 20); --11740690 Bug fix
9419 
9420 --  IF g_tab_osla_ele_ids.COUNT > 0 THEN  --11740690 Bug fix
9421   IF ((g_tab_osla_ele_ids.COUNT > 0) or (g_tab_osla_cl_ele_ids.COUNT > 0)) THEN
9422 
9423     debug(l_proc_name, 30) ;
9424     -- a call to calc_part_time_sal
9425     -- the last 2 parameters have been introduced in part_time_sal to make it generic
9426     l_grossed_osla_payment := calc_part_time_sal
9427                            (p_assignment_id        => p_assignment_id
9428                            ,p_effective_start_date => p_effective_start_date
9429                            ,p_effective_end_date   => p_effective_end_date
9430                            ,p_business_group_id    => g_business_group_id
9431                            ,p_sal_bal_type_id      => g_osla_bal_type_id(p_business_group_id)
9432                            ,p_cl_bal_type_id       => g_osla_cl_bal_type_id(p_business_group_id)
9433                            ,p_tab_bal_ele_ids      => g_tab_osla_ele_ids
9434                            );
9435     debug('l_grossed_osla_payment: '||to_char(l_grossed_osla_payment), 40) ;
9436 
9437 	-- Added for the Bug: 13243183
9438     -- Teachers out of school learning payments (OSLA) should be based on 365 days, even for a leap year
9439 		l_number_of_days := (trunc(p_effective_end_date) - trunc(p_effective_start_date) + 1);
9440 		if l_number_of_days = 366 then
9441 			l_number_of_days := l_number_of_days-1;
9442 		end if;
9443 	-- Changes for Bug : 13243183 ends
9444 
9445     -- grossing up the payment to calculate the rate
9446     l_grossed_osla_payment := ((l_grossed_osla_payment * 365) / l_number_of_days); --Bug:13243183
9447 
9448   ELSE
9449     debug(l_proc_name, 50) ;
9450     l_grossed_osla_payment := 0;
9451   END IF;
9452 
9453   debug('l_grossed_osla_payment :'||to_char(l_grossed_osla_payment),60);
9454 
9455   debug_exit(l_proc_name);
9456 
9457   return l_grossed_osla_payment;
9458 
9459 EXCEPTION
9460   WHEN OTHERS THEN
9461     debug_exit(' Others in '||l_proc_name);
9462     RAISE;
9463 END get_grossed_osla_payments;
9464 
9465 -- ----------------------------------------------------------------------------
9466 -- |------------------------< get_gtc_payments >----------------------------|
9467 -- ----------------------------------------------------------------------------
9468 function get_gtc_payments         (p_assignment_id        in     number
9469                                    ,p_effective_start_date in     date
9470                                    ,p_effective_end_date   in     date
9471                                    ,p_business_group_id    in     number
9472                                     )
9473   return number is
9474 --
9475   l_proc_name            VARCHAR2(60) := g_proc_name || 'get_gtc_payments';
9476   l_gtc_payment NUMBER := 0;
9477   l_return               NUMBER;
9478   l_next_payroll_start_date  DATE;
9479 --
9480 begin
9481 
9482   debug_enter(l_proc_name);
9483   debug('p_effective_start_date: '||to_char(p_effective_start_date), 10);
9484   debug('p_effective_end_date: '||to_char(p_effective_end_date));
9485 
9486   debug('TOTAL number of GTC elements: '|| to_char(g_tab_gtc_ele_ids.COUNT), 20);
9487 
9488   IF g_tab_gtc_ele_ids.COUNT > 0 THEN
9489 
9490     debug(l_proc_name, 30) ;
9491 
9492      l_gtc_payment := calc_part_time_sal_old
9493                                (p_assignment_id           => p_assignment_id
9494                                ,p_effective_start_date    => p_effective_start_date
9495                                ,p_effective_end_date      => p_effective_end_date
9496                                ,p_business_group_id       => g_business_group_id
9497                                ,p_next_payroll_start_date => l_next_payroll_start_date
9498                                ,p_sal_bal_type_id         => g_gtc_bal_type_id(p_business_group_id)
9499                                );
9500 
9501     debug('l_gtc_payment: '||to_char(l_gtc_payment), 40) ;
9502 
9503   ELSE
9504     debug(l_proc_name, 50) ;
9505     l_gtc_payment := 0;
9506   END IF;
9507 
9508   debug('l_gtc_payment :'||to_char(l_gtc_payment),60);
9509 
9510   debug_exit(l_proc_name);
9511 
9512   return l_gtc_payment;
9513 
9514 EXCEPTION
9515   WHEN OTHERS THEN
9516     debug_exit(' Others in '||l_proc_name);
9517     RAISE;
9518 END get_gtc_payments;
9519 
9520 
9521 -- ----------------------------------------------------------------------------
9522 -- |------------------------< get_part_time_sal_date >------------------------|
9523 -- ----------------------------------------------------------------------------
9524 function get_part_time_sal_date (p_assignment_id        in     number
9525                                 ,p_effective_start_date in    date
9526                                 ,p_effective_end_date   in    date
9527                                 )
9528 return number is
9529 --
9530   l_proc_name           varchar2(60) := g_proc_name || 'get_part_time_sal_date';
9531   l_part_time_sal       number := 0;
9532   l_tab_mult_asg        t_sec_asgs_type;
9533   l_error               number;
9534   l_sec_eff_start_date  date;
9535   l_sec_eff_end_date    date;
9536   i                     number;
9537   l_eff_sec_count       NUMBER := 0 ;--Sec asg tchr on p_effective_start_date
9538   l_look_for_sec_asg    varchar2(1) := 'Y';
9539  --
9540 begin
9541   --
9542   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
9543   --
9544   debug_enter(l_proc_name);
9545   debug('p_assignment_id: '||p_assignment_id);
9546   debug('p_effective_start_date: '||p_effective_start_date);
9547   debug('p_effective_end_date: '||p_effective_end_date);
9548 
9549   debug('g_part_time_asg_count: '||g_part_time_asg_count,10);
9550   -- Bugfix 3803760:FTSUPPLY
9551   -- If override ft asg is set, always return zero
9552   IF g_override_ft_asg_id IS NOT NULL THEN
9553     debug('Override is set, returning zero', 11);
9554     debug_exit(l_proc_name);
9555     RETURN 0;
9556   -- if there are multiple concurrent part time assignment (primary or secondary) and ft sal rates are not equal
9557   ELSIF g_part_time_asg_count > 1 AND g_equal_sal_rate = 'N' THEN
9558     debug('PT Asg count > zero, returning one', 12);
9559     debug_exit(l_proc_name);
9560     RETURN 1;
9561   END IF ;
9562 
9563   -- Check for multiple teaching assignments
9564   -- Bugfix 3803760:FTSUPPLY : Now using g_tab_sec_asgs instead of
9565   --  calling get_eff_secondary_asgs
9566   l_tab_mult_asg := g_tab_sec_asgs;
9567 
9568   debug('l_tab_mult_asg.count: '||l_tab_mult_asg.count,20);
9569   l_eff_sec_count := l_tab_mult_asg.count ;
9570 
9571   -- there are concurrent assignments and ft sal rates are not equal
9572   IF l_eff_sec_count > 1 AND g_equal_sal_rate = 'N' THEN
9573 
9574      debug_exit(l_proc_name);
9575      RETURN 1;
9576 
9577    END IF;
9578 
9579   -- Call this function only if primary assignment qualifies for
9580   -- report
9581   -- Bug Fix 3073562:GAP6
9582 
9583   debug('assignemnt_id: '||p_assignment_id,50);
9584   debug('start_date: '||g_ext_asg_details(p_assignment_id).start_date);
9585   debug('teacher_start_date: '||g_ext_asg_details(p_assignment_id).teacher_start_date);
9586   debug('leaver_date: '||g_ext_asg_details(p_assignment_id).leaver_date);
9587   debug('restarter_date: '||g_ext_asg_details(p_assignment_id).restarter_date);
9588 
9589   -- MULT-LR --
9590   -- Use the new Function to check the effectivness of an assignment
9591   -- it takes care of multiple Leaver-Restarter events
9592   -- where as the old logic used to take into account
9593   -- only the first restarter event.
9594   IF ( chk_effective_asg (
9595            p_assignment_id  => p_assignment_id
9596           ,p_effective_date => p_effective_start_date
9597                           ) = 'Y'
9598       )
9599   THEN
9600     debug(l_proc_name, 60);
9601 
9602     --if only the primary is valid on the start date or the ft sal rate is same as other pt assignments.
9603     IF l_eff_sec_count = 0 OR g_equal_sal_rate = 'Y' THEN
9604       --
9605       debug(l_proc_name, 70);
9606       --
9607       l_part_time_sal := l_part_time_sal +
9608                            calc_part_time_sal
9609                            (p_assignment_id        => p_assignment_id
9610                            ,p_effective_start_date => p_effective_start_date
9611                            ,p_effective_end_date   => p_effective_end_date
9612                            ,p_business_group_id    => g_business_group_id
9613                            );
9614 
9615   --    l_part_time_sal := round(l_part_time_sal);
9616       debug('l_part_time_sal of asg '||p_assignment_id||' = '||l_part_time_sal,70);
9617       -- Look for Secondary assignments also if FT sal rate is equal
9618       IF g_equal_sal_rate = 'Y' THEN
9619         l_look_for_sec_asg := 'Y';
9620       ELSE
9621          l_look_for_sec_asg := 'N';
9622       END IF;
9623       debug('look for secondary assignments ' || l_look_for_sec_asg,70);
9624     ELSE -- secondary assignment exists
9625       debug(l_proc_name, 80);
9626       debug_exit (l_proc_name);
9627 
9628       RETURN 1;
9629 
9630     END IF; -- End if of secondary assignment check ...
9631 
9632   -- ELSE -- primary assignment does not qualify
9633   END IF; -- End if of primary assignment qualifies check ...
9634 
9635     -- Fetch the part time salary information if there is
9636     -- only one secondary assignment
9637     debug(l_proc_name, 90);
9638  --   IF l_eff_sec_count = 1 THEN
9639       IF l_look_for_sec_asg = 'Y' THEN
9640       --
9641       debug(l_proc_name, 110);
9642       --
9643       i := l_tab_mult_asg.FIRST ;
9644 
9645       WHILE i IS NOT NULL
9646       LOOP
9647       l_sec_eff_start_date := greatest
9648                                  (p_effective_start_date
9649                                  ,g_ext_asg_details(l_tab_mult_asg(i).assignment_id).start_date
9650                                  );
9651 
9652       -- MULT-LR --
9653       -- Use the new Function to get the correct end date
9654       -- based on the multiple restarter events
9655       -- It takes care of multiple Leaver-Restarter events
9656       -- where as the old logic used to take into account
9657       -- only the first restarter event.
9658 
9659       -- Performance changes
9660       -- no need to call tihs function as we are checking assignment status in chk_effective_asg
9661       /*
9662       l_sec_eff_end_date := get_eff_end_date (
9663                                 p_assignment_id        => l_tab_mult_asg(i).assignment_id
9664                                ,p_effective_start_date => p_effective_start_date
9665                                ,p_effective_end_date   => p_effective_end_date
9666                                ) ;
9667       */
9668 
9669       l_sec_eff_end_date := p_effective_end_date;
9670 
9671       debug('l_sec_eff_end_date: '|| to_char(l_sec_eff_end_date,'DD/MM/YYYY'),112) ;
9672 
9673       l_part_time_sal := l_part_time_sal +
9674                            calc_part_time_sal
9675                            (p_assignment_id        => l_tab_mult_asg(i).assignment_id
9676                            ,p_effective_start_date => l_sec_eff_start_date
9677                            ,p_effective_end_date   => l_sec_eff_end_date
9678                            ,p_business_group_id    => g_ext_asg_details(l_tab_mult_asg(i).assignment_id).business_group_id
9679                            );
9680  debug('l_part_time_sal of after processing asg '||l_tab_mult_asg(i).assignment_id||' = '||l_part_time_sal,80);
9681   --    l_part_time_sal := round(l_part_time_sal);
9682 
9683       i := l_tab_mult_asg.NEXT(i);
9684     END LOOP;
9685 
9686     ELSE -- no secondary assignments
9687 
9688   --    l_part_time_sal := 0; --condition should not arise
9689       debug (l_proc_name ||' This Condition should not arise..',115 );
9690 
9691     END IF; -- End if of secondary assignment exists check ...
9692   -- round the pt salary after adding all the asg contributions.
9693     l_part_time_sal := round(l_part_time_sal);
9694   --
9695   -- hr_utility.set_location('Leaving: '||l_proc_name, 30);
9696   --
9697   debug('l_part_time_sal :'||to_char(l_part_time_sal),120);
9698 
9699   -- 4336613 : SAL_VALIDAT_3A : Check whether part time sal value has exceeeded 5 digit limit
9700   -- If yes, raise warning.
9701   if l_part_time_sal > 999999 then
9702 
9703      l_error := pqp_gb_tp_extract_functions.raise_extract_warning
9704                      (p_assignment_id => p_assignment_id
9705                      ,p_error_text    => 'BEN_93042_EXT_TP1_PT_SAL_EXC'
9706                      ,p_error_number  => 93042
9707                      ,p_token1        => TO_CHAR(l_part_time_sal) -- bug : 4336613
9708                      );
9709      l_part_time_sal := 999999; -- 4336613 : SAL_VALIDAT_3A : set to 99999 if > 99999
9710 
9711   end if; -- end if of part time sal value maxim limit check ...
9712 
9713   debug('l_part_time_sal :'||to_char(l_part_time_sal),125);
9714 
9715   debug_exit(l_proc_name);
9716   return l_part_time_sal;
9717 
9718 EXCEPTION
9719   WHEN OTHERS THEN
9720     debug_exit(' Others in '||l_proc_name);
9721     RAISE;
9722 end get_part_time_sal_date;
9723 
9724 --
9725 -- ----------------------------------------------------------------------------
9726 -- |------------------------< calc_days_worked >------------------------------|
9727 -- ----------------------------------------------------------------------------
9728 function calc_days_worked (p_assignment_id        in     number
9729                           ,p_effective_start_date in     date
9730                           ,p_effective_end_date   in     date
9731                           ,p_annual_sal_rate      in     number
9732                           )
9733   return number is
9734 --
9735   l_proc_name     varchar2(60) := g_proc_name || 'calc_days_worked';
9736   l_part_time_sal number;
9737   l_days_worked   number := 0;
9738 --
9739 begin
9740   --
9741   -- hr_utility.set_location ('Entering: '||l_proc_name, 10);
9742   --
9743   debug_enter(l_proc_name);
9744 
9745   -- Call this function only if salary rate is non zero
9746 
9747   if p_annual_sal_rate <> 0 then
9748 
9749     -- Get part time salary paid for this period
9750 
9751     l_part_time_sal := calc_part_time_sal
9752                          (p_assignment_id        => p_assignment_id
9753                          ,p_effective_start_date => p_effective_start_date
9754                          ,p_effective_end_date   => p_effective_end_date
9755                          ,p_business_group_id    => g_ext_asg_details(p_assignment_id).business_group_id
9756                          );
9757 
9758     -- Calculate days worked
9759 
9760     l_days_worked := (l_part_time_sal/p_annual_sal_rate) * 365;
9761 
9762   else
9763 
9764      l_days_worked := 0;
9765 
9766   end if; -- end if of annual rate value check ...
9767 
9768   --
9769   -- hr_utility.set_location ('Leaving: '||l_proc_name, 20);
9770   --
9771   debug('l_days_worked :'||to_char(l_days_worked));
9772   debug_exit(l_proc_name);
9773 
9774   return l_days_worked;
9775 
9776   --
9777 EXCEPTION
9778   WHEN OTHERS THEN
9779     debug_exit(' Others in '||l_proc_name);
9780     RAISE;
9781 end calc_days_worked;
9782 --
9783 -- ----------------------------------------------------------------------------
9784 -- |------------------------< get_eev_info >----------------------------------|
9785 -- ----------------------------------------------------------------------------
9786 procedure get_eev_info (p_element_entry_id     in     number
9787                        ,p_input_value_id       in     number
9788                        ,p_effective_start_date in     date
9789                        ,p_effective_end_date   in     date
9790                        ,p_tab_eev_info         out nocopy csr_get_eev_info_date%rowtype
9791                        ) is
9792 --
9793   l_proc_name      varchar2(60) := g_proc_name || 'get_eev_info';
9794 --
9795 begin
9796   --
9797   -- hr_utility.set_location ('Entering: '||l_proc_name, 10);
9798   --
9799   debug_enter(l_proc_name);
9800 
9801   open csr_get_eev_info_date
9802     (c_element_entry_id     => p_element_entry_id
9803     ,c_input_value_id       => p_input_value_id
9804     ,c_effective_start_date => p_effective_start_date
9805     ,c_effective_end_date   => p_effective_end_date
9806     );
9807   fetch csr_get_eev_info_date into p_tab_eev_info;
9808   close csr_get_eev_info_date;
9809 
9810   --
9811   -- hr_utility.set_location ('Leaving :'||l_proc_name, 20);
9812   --
9813   debug_exit(l_proc_name);
9814 
9815 -- Added by tmehra for nocopy changes Feb'03
9816 
9817 EXCEPTION
9818     WHEN OTHERS THEN
9819        debug_exit(' Others in '||l_proc_name);
9820        p_tab_eev_info := NULL;
9821        raise;
9822 
9823 end get_eev_info;
9824 --
9825 -- ----------------------------------------------------------------------------
9826 -- |------------------------< get_days_absent >-------------------------------|
9827 -- ----------------------------------------------------------------------------
9828 function get_days_absent (p_element_type_id      in     number
9829                          ,p_element_entry_id     in     number
9830                          ,p_effective_start_date in     date
9831                          ,p_effective_end_date   in     date
9832                          )
9833   return number is
9834 --
9835   l_proc_name             varchar2(60) := g_proc_name || 'get_days_absent';
9836   l_start_dt_iv_info      csr_get_iv_info%rowtype;
9837   l_days_abs_iv_info      csr_get_iv_info%rowtype;
9838   l_start_dt_vals         csr_get_eev_info%rowtype;
9839   l_days_abs_vals         csr_get_eev_info%rowtype;
9840   l_abs_start_day         date;
9841   l_days                  number := 0;
9842   l_total_days            number := 0;
9843   l_ref_date              date;
9844 --
9845 begin
9846   --
9847   -- hr_utility.set_location ('Entering: '||l_proc_name, 10);
9848   --
9849   debug_enter(l_proc_name);
9850 
9851   -- Get input value id for Start Date
9852   open csr_get_iv_info
9853     (c_element_type_id  => p_element_type_id
9854     ,c_input_value_name => 'Start Date'
9855     );
9856   fetch csr_get_iv_info into l_start_dt_iv_info;
9857   close csr_get_iv_info;
9858 
9859   -- Get input value id for Days Absent
9860   open csr_get_iv_info
9861     (c_element_type_id  => p_element_type_id
9862     ,c_input_value_name => 'Days Absent'
9863     );
9864   fetch csr_get_iv_info into l_days_abs_iv_info;
9865   close csr_get_iv_info;
9866 
9867   -- Get eev info for start date input value
9868   --
9869   debug (l_proc_name, 20);
9870   --
9871 
9872   open csr_get_eev_info
9873     (c_element_entry_id     => p_element_entry_id
9874     ,c_input_value_id       => l_start_dt_iv_info.input_value_id
9875     ,c_effective_start_date => p_effective_start_date
9876     ,c_effective_end_date   => p_effective_end_date
9877     );
9878   loop
9879     fetch csr_get_eev_info into l_start_dt_vals;
9880     exit when csr_get_eev_info%notfound;
9881 
9882     l_abs_start_day := fnd_date.canonical_to_date(l_start_dt_vals.screen_entry_value);
9883     if trunc(p_effective_end_date) < trunc(l_abs_start_day)
9884     then
9885 
9886        exit;
9887 
9888     end if; -- end if of effective end date check ...
9889 
9890     -- Get eev info for Days absent input value
9891     --
9892     debug (l_proc_name, 30);
9893     --
9894     get_eev_info (p_element_entry_id     => p_element_entry_id
9895                  ,p_input_value_id       => l_days_abs_iv_info.input_value_id
9896                  ,p_effective_start_date => l_start_dt_vals.effective_start_date
9897                  ,p_effective_end_date   => l_start_dt_vals.effective_end_date
9898                  ,p_tab_eev_info         => l_days_abs_vals
9899                  );
9900 
9901     -- BUGFIX 2340488
9902     -- Change line below, now doing a minus 1
9903     l_ref_date := (trunc(l_abs_start_day) +
9904                     l_days_abs_vals.screen_entry_value) - 1;
9905 
9906     if l_ref_date > trunc(p_effective_end_date) then
9907 
9908        -- BUGFIX 2340488
9909        -- Changed line below, now doing a + 1
9910        l_days := (trunc(p_effective_end_date) -
9911                    trunc(l_abs_start_day)) + 1;
9912 
9913     else
9914 
9915       -- Bug fix 2419860
9916       if l_ref_date >= trunc(p_effective_start_date) and
9917          trunc(p_effective_start_date) > trunc(l_abs_start_day)
9918       then
9919 
9920          l_days := (l_ref_date - trunc(p_effective_start_date)) + 1;
9921 
9922       elsif trunc(p_effective_start_date) <= trunc(l_abs_start_day) then
9923 
9924          l_days := l_days_abs_vals.screen_entry_value;
9925 
9926       end if; -- end of of ref_date > eff_start_date check...
9927 
9928     end if; -- end if of ref date check ...
9929 
9930     l_total_days := l_total_days + l_days;
9931 
9932   end loop;
9933   close csr_get_eev_info;
9934 
9935   debug('l_total_days :'||to_char(l_total_days), 40);
9936   --
9937   -- hr_utility.set_location ('Leaving: '||l_proc_name, 40);
9938   --
9939   debug_exit(l_proc_name);
9940 
9941   return l_total_days;
9942 
9943 EXCEPTION
9944   WHEN OTHERS THEN
9945     debug_exit(' Others in '||l_proc_name);
9946     RAISE;
9947 end get_days_absent;
9948 --
9949 -- ----------------------------------------------------------------------------
9950 -- |------------------------< get_eet_info >----------------------------------|
9951 -- ----------------------------------------------------------------------------
9952 function get_eet_info (p_assignment_id        in     number
9953                       ,p_tab_ele_ids          in     t_ele_ids_from_bal
9954                       ,p_effective_start_date in     date
9955                       ,p_effective_end_date   in     date
9956                       )
9957   return number is
9958 --
9959   l_proc_name      varchar2(60) := g_proc_name || 'get_eet_info';
9960   l_eet_details    csr_get_eet_info%rowtype;
9961   l_days_absent    number := 0;
9962   l_total_days     number := 0;
9963 --
9964 begin
9965   --
9966   -- hr_utility.set_location ('Entering: '||l_proc_name, 10);
9967   --
9968   debug_enter(l_proc_name);
9969 
9970   -- Check element entries exist in  absence ele's
9971 
9972   open csr_get_eet_info
9973     (c_assignment_id        => p_assignment_id
9974     ,c_effective_start_date => p_effective_start_date
9975     ,c_effective_end_date   => p_effective_end_date
9976     );
9977   loop
9978 
9979     fetch csr_get_eet_info into l_eet_details;
9980     exit when csr_get_eet_info%notfound;
9981 
9982     if p_tab_ele_ids.exists(l_eet_details.element_type_id) then
9983 
9984        -- absence element exists
9985        -- get the no of days from the element
9986        --
9987        debug (l_proc_name, 20);
9988        --
9989        l_days_absent := get_days_absent
9990                           (p_element_type_id       => l_eet_details.element_type_id
9991                           ,p_element_entry_id      => l_eet_details.element_entry_id
9992                           ,p_effective_start_date  => p_effective_start_date
9993                           ,p_effective_end_date    => p_effective_end_date
9994                           );
9995        l_total_days := l_total_days + l_days_absent;
9996 
9997     end if; -- end if of element id exists check ...
9998 
9999   end loop;
10000   close csr_get_eet_info;
10001 
10002   debug('l_total_days :'||to_char(l_total_days), 30);
10003   --
10004   -- hr_utility.set_location ('Leaving: '||l_proc_name, 30);
10005   --
10006   debug_exit(l_proc_name);
10007 
10008   return l_total_days;
10009 
10010 EXCEPTION
10011   WHEN OTHERS THEN
10012     debug_exit(' Others in '||l_proc_name);
10013     RAISE;
10014 end get_eet_info;
10015 --
10016 -- ----------------------------------------------------------------------------
10017 -- |------------------------< get_ft_days_excluded >--------------------------|
10018 -- ----------------------------------------------------------------------------
10019 function get_ft_days_excluded (p_assignment_id        in     number
10020                               ,p_effective_start_date in     date
10021                               ,p_effective_end_date   in     date
10022                               )
10023   return number is
10024 --
10025   l_proc_name        varchar2(60) := g_proc_name || 'get_ft_days_excluded';
10026   l_absence_bal_name pay_balance_types.balance_name%type;
10027   l_days_excluded    number;
10028   l_part_time_sal    number;
10029   l_eff_start_date   date  ;
10030   l_eff_end_date     date ;
10031 
10032 --
10033 begin
10034   --
10035   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
10036   --
10037   debug_enter(l_proc_name);
10038 
10039   -- GAP9: Modified function to incorporate changes for supply teachers
10040   -- 1. Check whether the assignment location is a supply teacher establishment
10041   -- 2. If it is get the part time salary paid for the assignment.
10042   -- 3. Return 365 only if the part time salary paid value is zero
10043   --    otherwise use the existing logic to determine days excluded value
10044 
10045   l_days_excluded := NULL; -- Initialize first
10046   debug('g_supply_asg_count ' || to_char(g_supply_asg_count),10);
10047   IF g_supply_asg_count > 0 THEN
10048 
10049      debug (l_proc_name, 20);
10050      debug ('Establishment Number: '||
10051              pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number,30
10052            );
10053 
10054      -- Check whether the establishment number = 0966
10055      IF pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number = '0966'
10056      THEN
10057        debug (l_proc_name, 40);
10058 
10059        -- Calculate the part time salary paid in the entire year
10060        -- If the salary paid is Zero, then the days_excluded will be = period length
10061 
10062        l_eff_start_date  := GREATEST(g_pension_year_start_date,p_effective_start_date
10063                               ,g_ext_asg_details(p_assignment_id).teacher_start_date
10064                               );
10065 
10066        -- MULT-LR --
10067        -- Use the new Function to get the correct end date
10068        -- based on the multiple restarter events
10069        -- It takes care of multiple Leaver-Restarter events
10070        -- where as the old logic used to take into account
10071        -- only the first restarter event.
10072 
10073        -- Performance changes
10074        -- no need to call tihs function as we are checking assignment status in chk_effective_asg
10075        /*
10076        l_eff_end_date := get_eff_end_date (
10077                                 p_assignment_id        => p_assignment_id
10078                                ,p_effective_start_date => p_effective_start_date
10079                                ,p_effective_end_date   => p_effective_end_date
10080                                ) ;
10081         */
10082 
10083         l_eff_end_date := p_effective_end_date;
10084 
10085         -- Get part time salary paid value for this assignment
10086         debug ('l_eff_start_date '||fnd_date.date_to_canonical(l_eff_start_date),50);
10087         debug ('l_eff_end_date '||fnd_date.date_to_canonical(l_eff_end_date));
10088 
10089         l_part_time_sal := calc_part_time_sal
10090                            (p_assignment_id        => p_assignment_id
10091                            ,p_effective_start_date => l_eff_start_date
10092                            ,p_effective_end_date   => l_eff_end_date
10093                            ,p_business_group_id    => g_ext_asg_details(p_assignment_id).business_group_id
10094                            );
10095 
10096         debug ('Part Time Sal Paid: '||TO_CHAR(l_part_time_sal),60);
10097 
10098         IF l_part_time_sal = 0 THEN
10099            debug (l_proc_name, 70);
10100            --l_days_excluded := 365;
10101            l_days_excluded := trunc(p_effective_end_date) - trunc(p_effective_start_date) + 1;
10102         END IF; -- End if of part time salary paid is zero check ...
10103 
10104      END IF; -- End if of establishment number is supply teacher one check ...
10105 
10106   END IF; -- End if of supply asg count > 0 check ...
10107 
10108   debug ('l_days_excluded: '|| to_char(l_days_excluded),80);
10109   -- If days excluded is set above do not process further...
10110 
10111   IF l_days_excluded IS NULL THEN
10112 
10113     debug (l_proc_name, 90);
10114     debug ('ext_emp_cat_cd '|| g_ext_asg_details(p_assignment_id).ext_emp_cat_cd);
10115     IF (g_ext_asg_details(p_assignment_id).ext_emp_cat_cd ='F' ) THEN
10116 
10117       debug (l_proc_name, 110);
10118       -- Modified the function to include any days excluded from OSP / OMP
10119       -- call osp function to derive this information
10120       -- The function should return 0 anyway
10121       l_days_excluded := pqp_gb_osp_functions.get_absence_paid_days_tp
10122                          (p_assignment_id => p_assignment_id
10123                          ,p_start_date    => p_effective_start_date
10124                          ,p_end_date      => p_effective_end_date
10125                          ,p_level_of_pay  => 'NOBAND' -- No pay days
10126                          );
10127       debug ('Days Excluded from OSP/OMP: ' || l_days_excluded);
10128       -- check whether absence elements exists for this assignment for this period
10129       -- get days excluded if element exists
10130 
10131       l_days_excluded := l_days_excluded +
10132                        get_eet_info
10133                          (p_assignment_id        => p_assignment_id
10134                          ,p_tab_ele_ids          => g_tab_abs_ele_ids
10135                          ,p_effective_start_date => p_effective_start_date
10136                          ,p_effective_end_date   => p_effective_end_date
10137                          );
10138 
10139 /**************************************************************************
10140 * Added the below logic to calculate Excluded Days for Part-time employees.
10141 * This is for the bug fix 11843594.
10142 ***************************************************************************/
10143 
10144     ELSIF (g_ext_asg_details(p_assignment_id).ext_emp_cat_cd ='P' ) THEN
10145 
10146      debug ('Entered new Part Time logic');
10147      debug (l_proc_name, 20);
10148      debug ('Establishment Number: '||pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number,30);
10149 
10150        debug (l_proc_name, 40);
10151 
10152        -- Calculate the part time salary paid in the entire year
10153        -- If the salary paid is Zero, then the days_excluded will be = period length
10154 
10155        l_eff_start_date  := GREATEST(g_pension_year_start_date,p_effective_start_date
10156                               ,g_ext_asg_details(p_assignment_id).teacher_start_date
10157                               );
10158 
10159        l_eff_end_date := p_effective_end_date;
10160 
10161         debug ('l_eff_start_date '||fnd_date.date_to_canonical(l_eff_start_date),50);
10162         debug ('l_eff_end_date '||fnd_date.date_to_canonical(l_eff_end_date));
10163 
10164         -- Get part time salary paid value for this assignment
10165         l_part_time_sal := calc_part_time_sal
10166                            (p_assignment_id        => p_assignment_id
10167                            ,p_effective_start_date => l_eff_start_date
10168                            ,p_effective_end_date   => l_eff_end_date
10169                            ,p_business_group_id    => g_ext_asg_details(p_assignment_id).business_group_id
10170                            );
10171 
10172         debug ('Part Time Sal Paid: '||TO_CHAR(l_part_time_sal),60);
10173 
10174         IF l_part_time_sal = 0 THEN
10175            debug (l_proc_name, 70);
10176            l_days_excluded := trunc(p_effective_end_date) - trunc(p_effective_start_date) + 1;
10177         END IF; -- End if of part time salary paid is zero check ...
10178 
10179 -- End of the logic for the bug 11843594
10180 ---
10181 
10182     ELSE
10183       debug(l_proc_name,115) ;
10184       l_days_excluded := 0;
10185     END IF ; --  (g_ext_asg_details(p_assignment_id).ext_emp_cat_cd ='F' )
10186   END IF; -- End if of l_days_excluded is null check ...
10187 
10188   debug ('l_days_excluded: '|| to_char(l_days_excluded),120);
10189 
10190   -- Bug fix 2411951
10191   -- Floor the days excluded value to be in favour of teachers
10192 
10193   l_days_excluded := floor(l_days_excluded);
10194 
10195   debug ('Total Days Excluded: '|| to_char(l_days_excluded));
10196   --
10197   -- hr_utility.set_location('Leaving: '||l_proc_name, 20);
10198   --
10199   debug_exit(l_proc_name);
10200 
10201   return l_days_excluded;
10202 
10203 EXCEPTION
10204   WHEN OTHERS THEN
10205     debug_exit(' Others in '||l_proc_name);
10206     RAISE;
10207 end get_ft_days_excluded;
10208 
10209 -- ----------------------------------------------------------------------------
10210 -- |------------------------< get_pt_days_excluded >--------------------------|
10211 -- ----------------------------------------------------------------------------
10212 function get_pt_days_excluded (p_assignment_id        in     number
10213                               ,p_effective_start_date in     date
10214                               ,p_effective_end_date   in     date
10215                               ,p_days                 out nocopy number
10216                               )
10217   return number is
10218 --
10219   l_proc_name          varchar2(60) := g_proc_name || 'get_pt_days_excluded';
10220   l_tab_mult_asg       t_sec_asgs_type;
10221   l_safeguarded_yn     varchar2(1) := 'N';
10222   l_annual_sal_rate    number;
10223   l_sec_ann_sal_rate   number;
10224   l_sec_eff_start_date date;
10225   l_sec_eff_end_date   date;
10226   l_days_worked        number := 0;
10227   l_total_days_worked  number := 0;
10228   l_days_in_period     number := 0;
10229   l_days_excluded      number := 0;
10230   l_return             number;
10231   i                    number;
10232   l_primary_esd        date;
10233   l_primary_eed        date;
10234   l_prev_annual_sal_rate     number := NULL;     -- bug 6275363
10235   l_equal_sal_rate           varchar2(1) := 'Y'; -- bug 6275363
10236 --
10237 begin
10238   --
10239   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
10240   --
10241   debug_enter(l_proc_name);
10242   debug(l_proc_name||'p_assignment_id:'||p_assignment_id, 15);
10243   debug(l_proc_name||'p_effective_start_date:'||to_char(p_effective_start_date,'DD/MM/YY'), 15);
10244   debug(l_proc_name||'p_effective_end_date:'||to_char(p_effective_end_date,'DD/MM/YY'), 15);
10245 
10246   -- Proceed only if primary assignment qualifies
10247   -- Bug Fix 3073562:GAP6
10248 
10249 
10250   -- Check if hte primary asg is valid TCHR assignment
10251   -- on the p_effective_start_date
10252 
10253   -- MULT-LR --
10254   -- Use the new Function to check the effectivness of an assignment
10255   -- it takes care of multiple Leaver-Restarter events
10256   -- where as the old logic used to take into account
10257   -- only the first restarter event.
10258   IF ( chk_effective_asg (
10259            p_assignment_id  => p_assignment_id
10260           ,p_effective_date => p_effective_start_date
10261                           ) = 'Y'
10262       )
10263   THEN
10264 
10265 
10266      -- Bugfix 3641851:CBF1 : We need to calculate the days excluded
10267      --     from start of primary asg as its possible that
10268      --     primary asg bcomes a teacher after secondary
10269      l_primary_esd := GREATEST(p_effective_start_date
10270                               ,g_ext_asg_details(p_assignment_id).start_date
10271                               );
10272 
10273      -- MULT-LR --
10274      -- Use the new Function to get the correct end date
10275      -- based on the multiple restarter events
10276      -- It takes care of multiple Leaver-Restarter events
10277      -- where as the old logic used to take into account
10278      -- only the first restarter event.
10279 
10280      -- Performance changes
10281      -- no need to call tihs function as we are checking assignment status in chk_effective_asg
10282      /*
10283      l_primary_eed := get_eff_end_date (
10284                                 p_assignment_id        => p_assignment_id
10285                                ,p_effective_start_date => p_effective_start_date
10286                                ,p_effective_end_date   => p_effective_end_date
10287                                ) ;
10288      */
10289 
10290      l_primary_eed := p_effective_end_date;
10291 
10292      debug('l_primary_eSd :'||to_char(l_primary_esd,'DD/MM/YY'));
10293      debug('l_primary_eEd :'||to_char(l_primary_eed,'DD/MM/YY'));
10294 
10295      -- Get safeguarded information
10296 
10297      l_safeguarded_yn := get_safeguarded_info
10298                            (p_assignment_id  => p_assignment_id
10299                            -- Bugfix 3641851:CBF1 : Changed to l_primary_esd
10300                            ,p_effective_date => l_primary_esd
10301                            );
10302 
10303      --
10304      debug(l_proc_name, 20);
10305      --
10306      -- Get annual salary rate for primary assignment
10307 
10308      l_return := calc_annual_sal_rate
10309                    (p_assignment_id        => p_assignment_id
10310                    -- Bugfix 3641851:CBF1 : Changed to l_primary_esd
10311                    ,p_calculation_date     => l_primary_esd
10312                    ,p_safeguarded_yn       => l_safeguarded_yn
10313                    ,p_fte                  => 'N'
10314                    ,p_to_time_dim          => 'A'
10315                    ,p_rate                 => l_annual_sal_rate
10316                    ,p_effective_start_date => p_effective_start_date
10317                    ,p_effective_end_date   => p_effective_end_date
10318                    );
10319 
10320     -- Bugfix 3641851:CBF1 : Moved this here frm below as this
10321     --  should only be done if primary asg exists and qualifies
10322     IF l_return <> -1 THEN
10323       debug (l_proc_name, 25);
10324       l_prev_annual_sal_rate :=l_annual_sal_rate;  -- bug 6275363
10325       l_total_days_worked := calc_days_worked
10326                              (p_assignment_id        => p_assignment_id
10327                              -- Bugfix 3641851:CBF1 : Using l_primary_esd and l_primary_eed
10328                              ,p_effective_start_date => l_primary_esd
10329                              ,p_effective_end_date   => l_primary_eed
10330                              ,p_annual_sal_rate      => l_annual_sal_rate
10331                              );
10332     END IF;
10333     --
10334   ELSE -- primary assignment does not qualify
10335      l_return := 0;
10336      l_annual_sal_rate := 0;
10337   END IF; -- primary assignment qualifies check ...
10338 
10339   if l_return <> -1 then
10340 
10341      -- Get days worked for this period
10342      --
10343      debug (l_proc_name, 30);
10344      --
10345 
10346      /* Bugfix 3641851:CBF1 : Moved this into IF part above as this
10347         should only be done if primary asg exists and qualifies
10348 
10349      l_total_days_worked := calc_days_worked
10350                               (p_assignment_id        => p_assignment_id
10351                               ,p_effective_start_date => p_effective_start_date
10352                               ,p_effective_end_date   => p_effective_end_date
10353                               ,p_annual_sal_rate      => l_annual_sal_rate
10354                               );
10355      */
10356 
10357      -- Check for multiple assignments
10358 
10359      --
10360      debug(l_proc_name, 40);
10361      --
10362      -- Bugfix 3803760:FTSUPPLY : Now using g_tab_sec_asgs instead of
10363      --  calling get_eff_secondary_asgs
10364      l_tab_mult_asg := g_tab_sec_asgs;
10365 
10366      if l_tab_mult_asg.count > 0 then
10367 
10368    -- Change the for loop to while loop as the assignment_id is
10369    -- an index here
10370    -- BUG FIX 3470242:BUG2
10371 
10372         -- Get annual salary rate for secondary assignments
10373 --        for i in l_tab_mult_asg.first..l_tab_mult_asg.last loop
10374 
10375         i := l_tab_mult_asg.FIRST;
10376         WHILE i IS NOT NULL
10377         LOOP
10378 
10379             --
10380             debug (l_proc_name, 50);
10381             --
10382 
10383             -- Get safeguarded information
10384 
10385             l_safeguarded_yn := get_safeguarded_info
10386                                   (p_assignment_id  => l_tab_mult_asg(i).assignment_id
10387                                   ,p_effective_date => p_effective_start_date
10388                                   );
10389 
10390             --
10391             debug (l_proc_name||'Asg Id:'||to_char(nvl(l_tab_mult_asg(i).assignment_id,99999)), 60);
10392 
10393             l_sec_eff_start_date := greatest
10394                                       (p_effective_start_date
10395                                       ,g_ext_asg_details(l_tab_mult_asg(i).assignment_id).start_date
10396                                       );
10397             -- MULT-LR --
10398             -- Use the new Function to get the correct end date
10399             -- based on the multiple restarter events
10400             -- It takes care of multiple Leaver-Restarter events
10401             -- where as the old logic used to take into account
10402             -- only the first restarter event.
10403 
10404             -- Performance changes
10405             -- no need to call tihs function as we are checking assignment status in chk_effective_asg
10406             /*
10407             l_sec_eff_end_date := get_eff_end_date (
10408                                 p_assignment_id        => l_tab_mult_asg(i).assignment_id
10409                                ,p_effective_start_date => p_effective_start_date
10410                                ,p_effective_end_date   => p_effective_end_date
10411                                ) ;
10412             */
10413 
10414             l_sec_eff_end_date := p_effective_end_date;
10415 
10416             debug('l_sec_eff_start_date: '|| to_char(l_sec_eff_start_date,'DD/MM/YYYY'),65) ;
10417             debug('l_sec_eff_end_date: '|| to_char(l_sec_eff_end_date,'DD/MM/YYYY')) ;
10418 
10419             l_return := calc_annual_sal_rate
10420                           (p_assignment_id        => l_tab_mult_asg(i).assignment_id
10421                           ,p_calculation_date     => l_sec_eff_start_date
10422                           ,p_safeguarded_yn       => l_safeguarded_yn
10423                           ,p_fte                  => 'N'
10424                           ,p_to_time_dim          => 'A'
10425                           ,p_rate                 => l_sec_ann_sal_rate
10426                           ,p_effective_start_date => p_effective_start_date
10427                           ,p_effective_end_date   => p_effective_end_date
10428                           );
10429 
10430             if l_return <> -1 then
10431 
10432                --
10433                debug (l_proc_name, 70);
10434                --
10435                -------bug 6275363 ------------------
10436 	       --compare with the previous salary rate and set the flag to 'N'
10437                -- as soon as a different sal_rate is found.
10438 
10439 	       IF l_prev_annual_sal_rate is not NULL THEN
10440                    IF l_sec_ann_sal_rate <> l_prev_annual_sal_rate AND l_sec_ann_sal_rate <> 0 THEN
10441                       l_equal_sal_rate := 'N' ;
10442 		   ELSE
10443                       debug('sal_rate are equal for this iteration',71);
10444                    END IF;
10445                ELSE  -- l_prev_annual_sal_rate is Null
10446                      --The first valid assignment
10447                     debug(l_proc_name, 72);
10448                     l_prev_annual_sal_rate := l_sec_ann_sal_rate ;
10449                END IF ;
10450                ----------------------------------
10451 		-- Calculate days worked
10452                l_days_worked := calc_days_worked
10453                                   (p_assignment_id        => l_tab_mult_asg(i).assignment_id
10454                                   ,p_effective_start_date => l_sec_eff_start_date
10455                                   ,p_effective_end_date   => l_sec_eff_end_date
10456                                   ,p_annual_sal_rate      => l_sec_ann_sal_rate
10457                                   );
10458 
10459                l_total_days_worked := l_total_days_worked + l_days_worked;
10460 
10461             else -- secondary asg annual sal rate is in error
10462 
10463               p_days      := 0;
10464               --
10465               -- hr_utility.set_location ('Leaving: '||l_proc_name, 80);
10466               --
10467               debug (l_proc_name ||'p_days:'||p_days, 80);
10468               debug (l_proc_name, 80);
10469 
10470               debug_exit(l_proc_name);
10471 
10472               return -1;
10473 
10474             end if; -- end if of sec asg annual return check ...
10475           i := l_tab_mult_asg.NEXT(i);
10476 
10477         end loop;
10478         debug (l_proc_name ||'Total Secondary Asgs: '||to_char(l_tab_mult_asg.COUNT), 85);
10479         debug ('l_equal_sal_rate : '|| l_equal_sal_rate, 85);
10480      end if ; -- end if of multiple asg check ...
10481 
10482      /* Bug fix 13706826 added the below condition */
10483      -- This will ensure for part time assignments Excluded days reported correctly.
10484      g_equal_sal_rate := l_equal_sal_rate;
10485 
10486      --
10487      l_days_in_period := (trunc(p_effective_end_date) - trunc(p_effective_start_date)) + 1;
10488 
10489      -- Bug Fix 2411951
10490      -- Ceil the days worked figure...
10491 
10492      l_total_days_worked := ceil(l_total_days_worked);
10493 
10494      debug (l_proc_name ||'l_days_in_period:'||l_days_in_period, 85);
10495      debug (l_proc_name ||'l_total_days_worked:'||l_total_days_worked, 85);
10496      debug (l_proc_name ||'p_days:'||p_days, 85);
10497 
10498      -- DE_CALC
10499      -- Days excluded should not be more than the period length
10500      -- due to the cieling on salary values, it may be more than the period
10501      -- hence LEAST
10502      -- l_days_excluded  := LEAST (l_days_in_period,ABS(l_days_in_period - l_total_days_worked));
10503      -- the logic above is incorrect.
10504 
10505      -- bugfix : 4926143
10506      -- new logic
10507      -- if total days worked for all assignments taken together is > days_in_period,
10508      -- then days_excluded is 0
10509      -- else days_excluded = days_in_period - total_days_worked
10510 
10511      IF l_days_in_period < l_total_days_worked
10512      THEN
10513        l_days_excluded := 0;
10514      ELSE
10515        l_days_excluded := l_days_in_period - l_total_days_worked;
10516      END IF;
10517 
10518      --
10519      --
10520      -- hr_utility.set_location ('Leaving: '||l_proc_name, 80);
10521      --
10522      -- debug_exit(l_proc_name);
10523 
10524      p_days := l_days_excluded;
10525 
10526      debug (l_proc_name ||'l_days_excluded:'||l_days_excluded, 87);
10527      debug (l_proc_name ||'l_total_days_worked:'||l_total_days_worked, 87);
10528      debug (l_proc_name ||'g_part_time_asg_count:'||g_part_time_asg_count, 87);
10529      debug (l_proc_name ||'g_equal_sal_rate:'||g_equal_sal_rate, 87);
10530      debug (l_proc_name ||'g_override_ft_asg_id:'||g_override_ft_asg_id, 87);
10531      debug (l_proc_name ||'l_equal_sal_rate:'||l_equal_sal_rate, 87);
10532 
10533      -- Bugfix 12759729
10534      IF g_part_time_asg_count > 1 AND g_equal_sal_rate = 'Y' THEN
10535         p_days :=0;
10536      /* RETURN -1; Bug Fix 13687144, changed the return value to 0 */
10537         debug (l_proc_name ||'Part Time concurrent assignments with equal sal rates, excluded days is zero', 87);
10538         RETURN 0;
10539      END IF;
10540 
10541  -- bug 6275363 --------------
10542      IF p_days =0 AND l_equal_sal_rate = 'N' and
10543        g_override_ft_asg_id IS NULL and g_part_time_asg_count > 1 then
10544         return -2;
10545      ELSE
10546        RETURN 0;
10547      END IF;
10548 -------------------------------
10549   else -- primary asg annual sal is in error
10550 
10551     --
10552     -- hr_utility.set_location ('Leaving: '||l_proc_name, 80);
10553     --
10554     debug_exit(l_proc_name);
10555 
10556     p_days      := 0;
10557     return -1;
10558 
10559   end if; -- end if of prim asg annual return check ...
10560 
10561   --
10562 
10563 -- Added by tmehra for nocopy changes Feb'03
10564 
10565 EXCEPTION
10566     WHEN OTHERS THEN
10567        debug_exit(' Others in '||l_proc_name);
10568        p_days := NULL;
10569        raise;
10570 
10571 end get_pt_days_excluded;
10572 
10573 --
10574 -- ----------------------------------------------------------------------------
10575 -- |------------------------< get_days_excluded_date >------------------------|
10576 -- ----------------------------------------------------------------------------
10577 function get_days_excluded_date (p_assignment_id        in     number
10578                                 ,p_effective_start_date in     date
10579                                 ,p_effective_end_date   in     date
10580                                 ,p_emp_cat_cd           in     varchar2
10581                                 ,p_days                 out nocopy number
10582                                 )
10583   return number is
10584 --
10585   l_proc_name          varchar2(60) := g_proc_name || 'get_days_excluded_date';
10586   l_return             number := 0;
10587   l_days_excluded      number;
10588   l_tab_mult_asg       t_sec_asgs_type;
10589   l_error_msg          varchar2(2000);
10590   i                    number;
10591   l_primary_esd        date;
10592   l_primary_eed        date;
10593   l_sec_eff_start_date date;
10594   l_sec_eff_end_date   date;
10595   l_error              NUMBER;
10596 --
10597 begin
10598   --
10599   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
10600   --
10601   debug_enter(l_proc_name);
10602 
10603   -- Check whether employment category is part time or full time
10604   -- As full time employment category may have multiple assignments
10605   -- so remove the check that checks for employment category
10606   -- rather go by multiple assignment check
10607 
10608 --  if nvl(p_emp_cat_cd,g_ext_asg_details(p_assignment_id).ext_emp_cat_cd) = 'F' then
10609 
10610      --
10611      debug(l_proc_name, 20);
10612      debug(l_proc_name||'p_assignment_id:'||p_assignment_id, 15);
10613      debug(l_proc_name||'p_effective_start_date:'||to_char(p_effective_start_date,'DD/MM/YY'), 15);
10614      debug(l_proc_name||'p_effective_end_date:'||to_char(p_effective_end_date,'DD/MM/YY'), 15);
10615      debug(l_proc_name||'p_emp_cat_cd:'||p_emp_cat_cd, 15);
10616      --
10617 --     l_days_excluded := get_ft_days_excluded
10618 --                          (p_assignment_id        => p_assignment_id
10619 --                          ,p_effective_start_date => p_effective_start_date
10620 --                          ,p_effective_end_date   => p_effective_end_date
10621 --                          );
10622 
10623 --  elsif nvl(p_emp_cat_cd,g_ext_asg_details(p_assignment_id).ext_emp_cat_cd) = 'P' then
10624 
10625     --
10626     debug(l_proc_name, 30);
10627     --
10628 
10629     -- Bugfix 3803760:FTSUPPLY : Now using g_tab_sec_asgs instead of
10630     --  calling get_eff_secondary_asgs
10631     l_tab_mult_asg := g_tab_sec_asgs;
10632 
10633     if (
10634         l_tab_mult_asg.count > 1
10635        )
10636        OR
10637        ( l_tab_mult_asg.count = 1
10638          AND
10639          -- Check if hte primary asg is valid TCHR assignment
10640          -- on the p_effective_start_date
10641          -- MULT-LR --
10642          -- Use the new Function to check the effectivness of an assignment
10643          -- it takes care of multiple Leaver-Restarter events
10644          -- where as the old logic used to take into account
10645          -- only the first restarter event.
10646         ( chk_effective_asg (
10647               p_assignment_id  => p_assignment_id
10648              ,p_effective_date => p_effective_start_date
10649                              ) = 'Y'
10650         )
10651        )
10652     then
10653         debug(l_proc_name, 40);
10654 
10655         l_return := get_pt_days_excluded
10656                       (p_assignment_id        => p_assignment_id
10657                       ,p_effective_start_date => p_effective_start_date
10658                       ,p_effective_end_date   => p_effective_end_date
10659                       ,p_days                 => l_days_excluded
10660                       );
10661 
10662     else
10663 
10664        -- check whether primary assignment qualifies for report
10665        -- Bug Fix 3073562:GAP6
10666 
10667 
10668        -- Check if hte primary asg is valid TCHR assignment
10669        -- on the p_effective_start_date
10670        -- MULT-LR --
10671        -- Use the new Function to check the effectivness of an assignment
10672        -- it takes care of multiple Leaver-Restarter events
10673        -- where as the old logic used to take into account
10674        -- only the first restarter event.
10675        IF ( chk_effective_asg (
10676                 p_assignment_id  => p_assignment_id
10677                ,p_effective_date => p_effective_start_date
10678                                ) = 'Y'
10679            )
10680        THEN
10681          debug(l_proc_name, 50);
10682 
10683          -- Bugfix 3641851:CBF1 : We need to calculate the days excluded
10684          --         from start of primary asg as its possible that
10685          --     primary asg bcomes a teacher after secondary
10686          l_primary_esd := GREATEST(p_effective_start_date
10687                                   ,g_ext_asg_details(p_assignment_id).start_date
10688                                   );
10689          -- MULT-LR --
10690          -- Use the new Function to get the correct end date
10691          -- based on the multiple restarter events
10692          -- It takes care of multiple Leaver-Restarter events
10693          -- where as the old logic used to take into account
10694          -- only the first restarter event.
10695 
10696          -- Performance changes
10697          -- no need to call tihs function as we are checking assignment status in chk_effective_asg
10698          /*
10699          l_primary_eed := get_eff_end_date (
10700                                 p_assignment_id        => p_assignment_id
10701                                ,p_effective_start_date => p_effective_start_date
10702                                ,p_effective_end_date   => p_effective_end_date
10703                                ) ;
10704          */
10705 
10706          l_primary_eed := p_effective_end_date;
10707 
10708          debug('l_primary_eSd :'||to_char(l_primary_esd,'DD/MM/YY'));
10709          debug('l_primary_eEd :'||to_char(l_primary_eed,'DD/MM/YY'));
10710 
10711          l_days_excluded := get_ft_days_excluded
10712                             (p_assignment_id        => p_assignment_id
10713                             -- Bugfix 3641851:CBF1 : Using l_primary_esd and l_primary_eed
10714                             ,p_effective_start_date => l_primary_esd
10715                             ,p_effective_end_date   => l_primary_eed
10716                             );
10717        ELSE -- primary assignment does not qualify for report
10718 
10719          -- Check whether secondary assignment count is one
10720          IF l_tab_mult_asg.COUNT = 1 THEN
10721 
10722             debug(l_proc_name, 60);
10723             i := l_tab_mult_asg.FIRST;
10724             l_sec_eff_start_date := greatest
10725                                       (p_effective_start_date
10726                                       ,g_ext_asg_details(l_tab_mult_asg(i).assignment_id).start_date
10727                                       );
10728 
10729             -- MULT-LR --
10730             -- Use the new Function to get the correct end date
10731             -- based on the multiple restarter events
10732             -- It takes care of multiple Leaver-Restarter events
10733             -- where as the old logic used to take into account
10734             -- only the first restarter event.
10735 
10736             -- Performance changes
10737             -- no need to call tihs function as we are checking assignment status in chk_effective_asg
10738             /*
10739             l_sec_eff_end_date := get_eff_end_date (
10740                                       p_assignment_id        => l_tab_mult_asg(i).assignment_id
10741                                      ,p_effective_start_date => p_effective_start_date
10742                                      ,p_effective_end_date   => p_effective_end_date
10743                                                     ) ;
10744             */
10745             l_sec_eff_end_date := p_effective_end_date;
10746 
10747             debug('l_sec_eff_start_date: '|| to_char(l_sec_eff_start_date,'DD/MM/YYYY'),65) ;
10748             debug('l_sec_eff_end_date: '|| to_char(l_sec_eff_end_date,'DD/MM/YYYY')) ;
10749 
10750 
10751             l_days_excluded := get_ft_days_excluded
10752                                (p_assignment_id        => l_tab_mult_asg(i).assignment_id
10753                                ,p_effective_start_date => l_sec_eff_start_date
10754                                ,p_effective_end_date   => l_sec_eff_end_date
10755                                );
10756          ELSE
10757 
10758            debug(l_proc_name, 70);
10759            l_days_excluded := 0;
10760          END IF; -- End if of mult assignment count = 1 check ...
10761 
10762        END IF; -- End if of primary assignment qualifies check ...
10763 
10764     end if; -- end if of multiple assignment check ...
10765 
10766 --  end if; -- end if of employment category check ...
10767 
10768     debug(l_proc_name||'l_days_excluded:'||l_days_excluded, 70);
10769     debug(l_proc_name||'l_return:'||l_return, 70);
10770    --
10771   -- hr_utility.set_location ('Leaving: '||l_proc_name, 40);
10772   --
10773  -- bug  6275363------
10774   IF l_return = -2 THEN
10775     debug(l_proc_name, 71);
10776     debug_exit(l_proc_name);
10777     RETURN -2;
10778   END if;
10779 ----------------------
10780   if l_return <> -1 then
10781 
10782      -- Check whether the days excluded has exceeded the allowed 3 digit limit
10783      debug('Days Excluded = ' ||l_days_excluded, 80);
10784      if l_days_excluded > 999 then
10785         debug(l_proc_name, 85);
10786         l_days_excluded := 999;
10787 
10788      end if; -- End if of days excluded greater than the limit check ...
10789 
10790      -- Cap Number of excluded days to 365, in case
10791      -- it is 366.
10792      -- Leave the value as it is if it is > 366 and
10793      -- Raise a warning cause it is a data issue
10794      -- (Warning is being raised by the FF)
10795 
10796      IF l_days_excluded = 366 THEN
10797        debug(l_proc_name, 90);
10798        l_days_excluded := 365 ;
10799      END IF ;
10800 
10801      p_days := l_days_excluded;
10802      debug_exit(l_proc_name);
10803      return 0;
10804 
10805   else
10806     debug(l_proc_name, 110);
10807     p_days := 0;
10808     debug_exit(l_proc_name);
10809     return -1;
10810 
10811   end if; -- end if of l_return check ...
10812 
10813 -- Added by tmehra for nocopy changes Feb'03
10814 
10815 EXCEPTION
10816     WHEN OTHERS THEN
10817        debug_exit(' Others in '||l_proc_name);
10818        p_days := NULL;
10819        raise;
10820 
10821 end get_days_excluded_date;
10822 
10823 --
10824 -- Check if person has multiple person records
10825 --
10826 FUNCTION chk_does_person_hv_multi_recs
10827         (p_assignment_id        IN NUMBER
10828         ,p_business_group_id    IN NUMBER
10829         ,p_effective_date       IN DATE
10830         ,p_present_in_masterbg  OUT NOCOPY BOOLEAN
10831         ,p_person_count         OUT NOCOPY NUMBER
10832         ) RETURN BOOLEAN
10833 IS
10834 
10835 
10836   l_multiper_found      BOOLEAN := FALSE;
10837   l_present_in_masterbg BOOLEAN := FALSE;
10838   l_ext_emp_cat_cd      VARCHAR2(1);
10839   l_person_id           per_all_people_f.person_id%TYPE;
10840 
10841   l_per_details         csr_asg_details_up%ROWTYPE;
10842   l_multiper            c_multiper%ROWTYPE;
10843   l_person_count        NUMBER :=0 ;
10844 
10845   l_proc_name          VARCHAR2(61):=
10846      g_proc_name||'chk_does_person_hv_multi_recs';
10847 
10848   -- CROSSPER:
10849   -- new varaibles introduced to
10850   -- check for crossperson records
10851   -- Will set these variables accordng to the
10852   -- global dates set in TP1/TP2/TP4
10853   -- depending on the extract type
10854   -- and pass to c_multiper
10855   l_eff_start_date      DATE;
10856   l_eff_end_date        DATE;
10857 
10858 BEGIN -- chk_does_person_hv_multi_recs
10859 
10860   debug_enter(l_proc_name);
10861   debug('g_master_bg_id :'||to_char(nvl(g_master_bg_id,-1)), 05);
10862 
10863   OPEN csr_asg_details_up(p_assignment_id => p_assignment_id
10864                          ,p_effective_date => p_effective_date
10865                          );
10866   FETCH csr_asg_details_up INTO l_per_details;
10867   l_person_id := l_per_details.person_id;
10868 
10869   CLOSE csr_asg_details_up;
10870 
10871   -- Bugfix 3073562:GAP8
10872   -- If we find a full-time assignment for this person
10873   -- then we must treat each person record seperately
10874 
10875   -- Bugfix 3671727:ENH2
10876   -- This only needs to be done for the Type 1 report
10877   -- For the Type 2 and 4 we still amalgamate data
10878   IF g_extract_type IN ('TP1P', 'TP1') THEN
10879 
10880 
10881     debug(l_proc_name, 10);
10882 
10883      -- CROSSPER: set the dates
10884     l_eff_start_date := p_effective_date;
10885     l_eff_end_date   := g_pension_year_end_date ;
10886 
10887     debug(' l_per_details.asg_emp_cat_cd :'|| l_per_details.asg_emp_cat_cd,11);
10888     debug(' p_effective_date :'|| to_char(p_effective_date),12);
10889     -- First chk the assignment id passed
10890     l_ext_emp_cat_cd := get_translate_asg_emp_cat_code
10891                             (l_per_details.asg_emp_cat_cd
10892                             ,p_effective_date
10893                             ,'Pension Extracts Employment Category Code'
10894                             ,p_business_group_id
10895                             );
10896 
10897     debug('l_ext_emp_cat_cd :'|| l_ext_emp_cat_cd,13);
10898     IF l_ext_emp_cat_cd = 'F' THEN
10899       debug(l_proc_name, 20);
10900       l_multiper_found := FALSE;
10901       l_present_in_masterbg := FALSE;
10902 
10903       --Check if other asgs exists for the same person.
10904       OPEN c_multiper
10905            (p_person_id            => l_person_id
10906            ,p_effective_start_date => l_eff_start_date
10907            ,p_effective_end_date   => l_eff_end_date
10908            ,p_assignment_id        => p_assignment_id
10909            ) ;
10910       FETCH c_multiper INTO l_multiper ;
10911       IF c_multiper%FOUND THEN
10912         l_person_count := 1 ;
10913       END IF;
10914       CLOSE c_multiper ;
10915 
10916     ELSE
10917 
10918       l_ext_emp_cat_cd := NULL;
10919 
10920       FOR l_multiper IN c_multiper
10921                          (p_person_id            => l_person_id
10922                          ,p_effective_start_date => l_eff_start_date
10923                          ,p_effective_end_date   => l_eff_end_date
10924                          ,p_assignment_id        => p_assignment_id
10925                           )
10926       LOOP
10927 
10928         debug(l_proc_name, 30);
10929         l_multiper_found := TRUE;
10930         l_person_count := l_person_count + 1;
10931 
10932         IF l_multiper.business_group_id = g_master_bg_id THEN
10933           debug(l_proc_name, 40);
10934           l_present_in_masterbg := TRUE;
10935         END IF;
10936 
10937         l_ext_emp_cat_cd := get_translate_asg_emp_cat_code
10938                               (l_multiper.asg_emp_cat_cd
10939                               ,p_effective_date
10940                               ,'Pension Extracts Employment Category Code'
10941                               ,p_business_group_id
10942                               );
10943 
10944         -- Bugfix 3073562:GAP8
10945         -- If we find a full-time assignment for this person
10946         -- then we must treat each person record seperately
10947         IF l_ext_emp_cat_cd = 'F' THEN
10948           debug( 'setting l_multiplier_found =false');
10949           l_multiper_found := FALSE;
10950           l_present_in_masterbg := FALSE;
10951           debug(l_proc_name, 50);
10952           EXIT;
10953         END IF;
10954 
10955       END LOOP;
10956 
10957     END IF; -- l_ext_emp_cat_cd = 'F' THEN
10958 
10959   ELSE -- g_extract_type is TP4 or TP2
10960 
10961     -- CROSSPER:check the extract type and set the dates
10962     IF (g_extract_type = 'TP2' ) THEN
10963       l_eff_start_date := pqp_gb_tp_type2_functions.g_effective_start_date;
10964       l_eff_end_date   := pqp_gb_tp_type2_functions.g_effective_end_date;
10965     ELSIF  (g_extract_type = 'TP4' ) THEN
10966       l_eff_start_date := pqp_gb_tp_pension_extracts.g_last_effective_date ;
10967       l_eff_end_date   := pqp_gb_tp_pension_extracts.g_effective_run_date ;
10968     END IF;
10969 
10970     FOR l_multiper IN c_multiper
10971                        (p_person_id            => l_person_id
10972                        ,p_effective_start_date => l_eff_start_date
10973                        ,p_effective_end_date  => l_eff_end_date
10974                        ,p_assignment_id        => p_assignment_id
10975                         )
10976     LOOP
10977 
10978       debug(l_proc_name, 60);
10979       l_multiper_found := TRUE;
10980 
10981       IF l_multiper.business_group_id = g_master_bg_id THEN
10982         debug(l_proc_name, 70);
10983         l_present_in_masterbg := TRUE;
10984       END IF;
10985 
10986       -- If both the flags are set to TRUE, no need to check further
10987       IF l_present_in_masterbg
10988          AND
10989          l_multiper_found THEN
10990 
10991         debug(l_proc_name, 80);
10992         EXIT;
10993 
10994       END IF;
10995 
10996     END LOOP;
10997 
10998   END IF; -- g_extract_type check
10999 
11000   debug_exit(l_proc_name);
11001 
11002   -- Assign value to the OUT param value
11003   p_present_in_masterbg := l_present_in_masterbg;
11004   p_person_count        := l_person_count ;
11005 
11006   RETURN l_multiper_found;
11007 EXCEPTION
11008   WHEN OTHERS THEN
11009     p_present_in_masterbg := NULL;
11010     p_person_count        := NULL;
11011     debug_exit('Others in '||l_proc_name);
11012     RAISE;
11013 END; -- chk_does_person_hv_multi_recs
11014 
11015 --
11016 -- set_multirec_person
11017 --
11018 FUNCTION set_multirec_person(p_business_group_id        IN NUMBER
11019                             ,p_person_id                IN NUMBER
11020                             ,p_assignment_id            IN NUMBER
11021                             ,p_national_identifier      IN VARCHAR2
11022                             ,p_effective_start_date     IN DATE
11023                             ,p_effective_end_date       IN DATE
11024                             ,p_processing_status        IN VARCHAR2
11025                             ,p_request_id               IN NUMBER
11026                             ) RETURN BOOLEAN IS
11027 
11028   PRAGMA AUTONOMOUS_TRANSACTION;
11029 
11030   CURSOR csr_multirec_person IS
11031   SELECT *
11032   FROM pqp_ext_cross_person_records emd
11033   WHERE emd.record_type = 'X'
11034     AND emd.national_identifier = p_national_identifier
11035     AND emd.ext_dfn_id = ben_ext_thread.g_ext_dfn_id                  --ENH3
11036     AND emd.lea_number = g_lea_number                                 --ENH3
11037   FOR UPDATE OF processing_status NOWAIT;
11038 
11039 
11040   l_report_person       BOOLEAN := FALSE;
11041   l_multirec_per        csr_multirec_person%ROWTYPE;
11042 
11043   l_proc_name          VARCHAR2(61):=
11044      g_proc_name||'set_multirec_person';
11045 
11046 BEGIN
11047 
11048   debug_enter(l_proc_name);
11049 
11050   OPEN csr_multirec_person;
11051   FETCH csr_multirec_person INTO l_multirec_per;
11052 
11053   IF csr_multirec_person%FOUND THEN
11054 
11055     debug(l_proc_name, 10);
11056 
11057     IF nvl(l_multirec_per.processing_status,'U') = 'U' THEN
11058       BEGIN -- Attempt an update
11059 
11060         debug(l_proc_name, 20);
11061         UPDATE pqp_ext_cross_person_records
11062         SET business_group_id           = p_business_group_id
11063            ,person_id                   = p_person_id
11064            ,national_identifier         = p_national_identifier
11065            ,assignment_id               = p_assignment_id
11066            ,effective_start_date        = p_effective_start_date
11067            ,effective_end_date          = p_effective_end_date
11068            ,processing_status           = p_processing_status
11069            ,request_id                  = p_request_id
11070            ,last_updated_by             = fnd_global.user_id
11071            ,last_update_date            = SYSDATE
11072            ,last_update_login           = fnd_global.login_id
11073            ,object_version_number       = (l_multirec_per.object_version_number + 1)
11074         WHERE CURRENT OF csr_multirec_person;
11075 
11076         l_report_person := TRUE;
11077 
11078         debug(l_proc_name, 30);
11079 
11080       EXCEPTION
11081         WHEN OTHERS THEN
11082 
11083           debug(l_proc_name, 40);
11084           l_report_person := FALSE;
11085           -- IF the code is -54 then the row is locked and
11086           -- is being updated by another extract, we will not
11087           -- report this person in the current extract.
11088           -- If its anything other than 54, its a problem
11089           -- raise it.
11090           IF SQLCODE <> -54 THEN
11091             CLOSE csr_multirec_person;
11092             debug_exit(l_proc_name);
11093             RAISE;
11094           END IF;
11095       END; -- Attempt an update
11096 
11097       debug(l_proc_name, 50);
11098     ELSE
11099       debug(l_proc_name, 60);
11100       l_report_person := FALSE;
11101     END IF;
11102 
11103   ELSE -- Notfound, Need to insert
11104     debug(l_proc_name, 70);
11105     INSERT INTO pqp_ext_cross_person_records
11106     (record_type
11107     ,ext_dfn_id                                  --ENH3
11108     ,lea_number                                  --ENH3
11109     ,business_group_id
11110     ,person_id
11111     ,national_identifier
11112     ,assignment_id
11113     ,effective_start_date
11114     ,effective_end_date
11115     ,processing_status
11116     ,request_id
11117     ,created_by
11118     ,creation_date
11119     ,object_version_number
11120     )
11121     VALUES
11122     ('X'
11123     ,ben_ext_thread.g_ext_dfn_id                  --ENH3
11124     ,pqp_gb_tp_pension_extracts.g_lea_number      --ENH3
11125     ,p_business_group_id
11126     ,p_person_id
11127     ,p_national_identifier
11128     ,p_assignment_id
11129     ,p_effective_start_date
11130     ,p_effective_end_date
11131     ,p_processing_status
11132     ,p_request_id
11133     ,fnd_global.user_id
11134     ,SYSDATE
11135     ,1
11136     );
11137 
11138     l_report_person := TRUE;
11139 
11140   END IF; -- csr_multirec_person%FOUND THEN
11141 
11142   debug(l_proc_name, 80);
11143 
11144   CLOSE csr_multirec_person;
11145 
11146   COMMIT;
11147 
11148   debug_exit(l_proc_name);
11149 
11150   RETURN l_report_person;
11151 
11152 EXCEPTION
11153   WHEN OTHERS THEN
11154     debug_exit(' Others in '||l_proc_name);
11155     RAISE;
11156 END; -- set_multirec_person
11157 
11158 --
11159 -- chk_report_person
11160 --
11161 FUNCTION chk_report_person
11162   (p_business_group_id        IN      NUMBER  -- context
11163   ,p_effective_date           IN      DATE    -- context
11164   ,p_assignment_id            IN      NUMBER  -- context
11165   ) RETURN BOOLEAN
11166 IS
11167 
11168   CURSOR csr_per_details IS
11169   SELECT per.person_id                  person_id
11170         ,per.national_identifier        national_identifier
11171     FROM per_all_assignments_f asg
11172         ,per_all_people_f per
11173    WHERE asg.assignment_id = p_assignment_id
11174      AND per.person_id = asg.person_id;
11175 
11176 
11177   l_person_has_multiple_recs    BOOLEAN := FALSE;
11178   l_present_in_masterbg         BOOLEAN := FALSE;
11179   l_report_person               BOOLEAN := FALSE;
11180   l_error                       NUMBER ;
11181   l_multiper_dets               csr_multiproc_data%ROWTYPE;
11182   l_per_details                 csr_per_details%ROWTYPE;
11183 
11184 
11185   l_proc_name           VARCHAR2(61):=
11186      g_proc_name||'chk_report_person';
11187 
11188 BEGIN -- chk_report_person
11189 
11190   debug_enter(l_proc_name);
11191 
11192   -- This function does the following checks and processing
11193   -- 1) If the current bg is cross bg reporting enabled
11194   --    then we need to chk if the current BG is the Master BG
11195   --    a) If it is, then we report this person in this, Master, BG
11196   --       But before that we need to chk if this person is
11197   --       already being reported in the current (master) BG
11198   --       as it could be a cross person in same Bg scenario.
11199   --    b) If this is not the master bg, then we check if the
11200   --       person has a record in master bg
11201   --       b.1) If yes, then v leave the person alone as he will
11202   --            get processed in the extract for master bg anyway
11203   --       b.2) If not, then we chk if this person is being
11204   --            already processed in another/same extract for cross BG
11205   --            scenario. For this we look for a row in
11206   --            pqp_ext_cross_person_records for this persons NI number
11207   --            i) If row found with 'P', then leave this person
11208   --           ii) If not found or found with NULL or 'U', then we
11209   --               insert/update the row to tell other extracts that
11210   --               mite be running parallely that this person is already
11211   --               being processed
11212 
11213 
11214   debug(l_proc_name, 10);
11215   OPEN csr_per_details;
11216   FETCH csr_per_details INTO l_per_details;
11217   CLOSE csr_per_details;
11218 
11219   -- Now chk if this person has multi person recs
11220   g_person_count := 0;
11221   debug('p_assignment_id :'||to_char(p_assignment_id));
11222   debug('p_effective_date :'||to_char(p_effective_date));
11223 
11224   l_person_has_multiple_recs :=
11225           chk_does_person_hv_multi_recs
11226             (p_assignment_id        => p_assignment_id
11227             ,p_business_group_id    => p_business_group_id
11228             ,p_effective_date       => p_effective_date
11229             ,p_present_in_masterbg  => l_present_in_masterbg -- OUT
11230             ,p_person_count         => g_person_count --OUT
11231             );
11232 
11233   debug('g_person_count :'||to_char(g_person_count));
11234 
11235   -- Bugfix 3073562:GAP8
11236   -- We must not amalgamate person data from other person
11237   -- records within or across BGs if the person
11238   -- has a full-time assignment in any of the person recs
11239   IF l_person_has_multiple_recs THEN
11240     debug(l_proc_name,15);
11241     debug('l_person_has_multiple_recs has multiple recs ');
11242     g_cross_per_enabled := 'Y';
11243   ELSE
11244     debug('l_person_has_multiple_recs doesnt have multiple recs ');
11245     g_cross_per_enabled := 'N';
11246   END IF;
11247 
11248   -- Step 1) Is cross Bg reporting enabled
11249   IF g_crossbg_enabled = 'Y' THEN
11250 
11251     -- Step 2) Is this the master BG.
11252     IF g_business_group_id = g_master_bg_id THEN
11253 
11254       debug(l_proc_name, 20);
11255 
11256       -- Always report a person from master BG if
11257       -- there is a person record in the master BG
11258       l_report_person := TRUE;
11259 
11260       -- If person has multiple records, mark the
11261       -- row in multiproc_data
11262       IF l_person_has_multiple_recs THEN
11263 
11264         debug(l_proc_name, 30);
11265 
11266         -- Returns true on successful update or insert
11267         l_report_person := set_multirec_person
11268                              (p_business_group_id         => p_business_group_id
11269                              ,p_person_id                 => l_per_details.person_id
11270                              ,p_assignment_id             => p_assignment_id
11271                              ,p_national_identifier       => l_per_details.national_identifier
11272                              ,p_effective_start_date      => p_effective_date
11273                              ,p_effective_end_date        => NULL
11274                              ,p_processing_status         => 'P'
11275                              ,p_request_id                => fnd_global.conc_request_id
11276                              );
11277 
11278       END IF; -- l_person_has_multiple_recs THEN
11279 
11280     ELSE -- the report is not running in the master BG
11281 
11282       debug(l_proc_name, 40);
11283 
11284       -- Chk if this person has a rec in master BG.
11285       IF l_present_in_masterbg THEN
11286         -- IF Yes then do not report person in current BG
11287         l_report_person := FALSE;
11288         debug(l_proc_name, 50);
11289       ELSE
11290 
11291         -- If person has multiple records, try to mark the
11292         -- row in multiproc_data
11293         IF l_person_has_multiple_recs THEN
11294 
11295           -- debug('National Identifier :'||l_per_details.national_identifier, 59);
11296           debug(l_proc_name, 60);
11297           IF  csr_multiproc_data%ISOPEN THEN
11298             CLOSE csr_multiproc_data;
11299           END IF;
11300 
11301           -- Check if the person is already being processed
11302           OPEN csr_multiproc_data(p_record_type => 'X'
11303                                  ,p_national_identifier => l_per_details.national_identifier
11304                                  );
11305           FETCH csr_multiproc_data INTO l_multiper_dets;
11306 
11307           debug('Processing Status :'||l_multiper_dets.processing_status, 70);
11308 
11309           IF (csr_multiproc_data%NOTFOUND -- No row for this NI
11310               OR
11311               (csr_multiproc_data%FOUND -- Row found for this NI
11312                AND
11313                nvl(l_multiper_dets.processing_status,'U') = 'U' -- Unprocessed
11314               )
11315              ) THEN
11316 
11317             debug(l_proc_name, 80);
11318 
11319             -- Returns true on successful update or insert
11320             l_report_person:= set_multirec_person
11321                                 (p_business_group_id       => p_business_group_id
11322                                 ,p_person_id               => l_per_details.person_id
11323                                 ,p_assignment_id           => p_assignment_id
11324                                 ,p_national_identifier     => l_per_details.national_identifier
11325                                 ,p_effective_start_date    => p_effective_date
11326                                 ,p_effective_end_date      => NULL
11327                                 ,p_processing_status       => 'P'
11328                                 ,p_request_id              => fnd_global.conc_request_id
11329                                 );
11330 
11331           ELSE
11332             -- Person is already being processed by another BG that
11333             -- mite be running parallely. So do not process in this report
11334             debug(l_proc_name, 90);
11335             l_report_person := FALSE;
11336           END IF; -- (csr_multiproc_data%NOTFOUND -- No row for this NI
11337           CLOSE csr_multiproc_data;
11338 
11339         ELSE
11340           debug(l_proc_name, 95);
11341           l_report_person := TRUE;
11342         END IF; -- l_person_has_multiple_recs THEN
11343 
11344       END IF; -- l_present_in_masterbg THEN
11345 
11346     END IF; -- g_business_group_id = g_master_bg_id THEN
11347 
11348   ELSE -- g_crossbg_enabled = 'N'
11349 
11350     -- If person has multiple records, mark the
11351     -- row in multiproc_data
11352     IF l_person_has_multiple_recs THEN
11353 
11354       debug(l_proc_name, 100);
11355 
11356       -- Returns true on successful update or insert
11357       l_report_person := set_multirec_person
11358                            (p_business_group_id         => p_business_group_id
11359                            ,p_person_id                 => l_per_details.person_id
11360                            ,p_assignment_id             => p_assignment_id
11361                            ,p_national_identifier       => l_per_details.national_identifier
11362                            ,p_effective_start_date      => p_effective_date
11363                            ,p_effective_end_date        => NULL
11364                            ,p_processing_status         => 'P'
11365                            ,p_request_id                => fnd_global.conc_request_id
11366                            );
11367 
11368     ELSE
11369       debug(l_proc_name, 105);
11370       l_report_person := TRUE;
11371     END IF; -- l_person_has_multiple_recs THEN
11372 
11373     debug(l_proc_name, 110);
11374   END IF; -- g_crossbg_enabled = 'Y' THEN
11375 
11376   debug_exit(l_proc_name);
11377 
11378   RETURN l_report_person;
11379 EXCEPTION
11380   WHEN OTHERS THEN
11381     debug('SQLCODE :'||to_char(SQLCODE), 120);
11382     debug('SQLERRM :'||SQLERRM, 130);
11383     debug_exit(' Others in '||l_proc_name);
11384     RAISE;
11385 
11386 END; -- chk_report_person
11387 
11388 -- Criteria for Type 1 Periodic Leavers
11389 --
11390 FUNCTION chk_tp1_criteria_periodic
11391   (p_business_group_id        IN      NUMBER  -- context
11392   ,p_effective_date           IN      DATE    -- context
11393   ,p_assignment_id            IN      NUMBER  -- context
11394   ) RETURN VARCHAR2 -- Y or N
11395 IS
11396 
11397   -- Variable Declaration
11398   l_inclusion_flag      VARCHAR2(1) := 'Y';
11399   l_leaver              VARCHAR2(1) := 'N';
11400   l_leaver_date         DATE;
11401   l_already_reported    VARCHAR2(1) := 'N';
11402   l_restarter           VARCHAR2(1) := 'N';
11403   l_restarter_date      DATE := NULL;
11404   l_look_for_sec_asgs   BOOLEAN := FALSE;
11405   l_asg_count           NUMBER;
11406   l_error               NUMBER;
11407   -- FOR PROFILING
11408   l_start_time          NUMBER;
11409   l_end_time            NUMBER;
11410 
11411   -- Rowtype Variable Declaration
11412   l_asg_details        csr_asg_details_up%ROWTYPE;
11413   l_pqp_asg_attributes csr_pqp_asg_attributes_up%ROWTYPE;
11414   l_temp_asg_details    csr_asg_details_up%ROWTYPE;
11415 
11416   l_proc_name          VARCHAR2(61):=
11417      g_proc_name||'chk_tp1_criteria_periodic';
11418 
11419 BEGIN -- chk_tp1_criteria_periodic
11420 
11421   debug_enter(l_proc_name);
11422 
11423   debug('Assignment Id :'||to_char(p_assignment_id),10);
11424 
11425   IF g_business_group_id IS NULL THEN
11426 
11427     -- Bugifx 2848696 : Now using ben_ext_person.g_effective_date
11428     -- instead of p_effective_date
11429     set_t1_extract_globals
11430         (p_business_group_id
11431         ,ben_ext_person.g_effective_date   -- p_effective_date
11432         ,p_assignment_id
11433         );
11434 
11435     set_periodic_run_dates;
11436 
11437     -- Fetch element ids from balance's
11438     fetch_eles_for_t1_bals (p_assignment_id  => p_assignment_id
11439                            ,p_effective_date => g_pension_year_start_date
11440                            );
11441 
11442   END IF;
11443 
11444   -- Bugfix -- Bugfix 3671727: Performance enhancement
11445   --    If no location exists in the list of valid criteria
11446   --    establishments, then no point doing all checks
11447   --    Just warn once and skip every assignment
11448   IF pqp_gb_tp_pension_extracts.g_criteria_estbs.COUNT = 0 THEN
11449 
11450     debug('Setting inclusion flag to N as no locations EXIST.', 15);
11451     l_inclusion_flag := 'N';
11452 
11453     pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
11454     -- Call TP4 pkg proc to warning for no locations
11455     pqp_gb_tp_pension_extracts.warn_if_no_loc_exist
11456         (p_assignment_id => p_assignment_id) ;
11457     pqp_gb_tp_pension_extracts.g_nested_level := 0;
11458 
11459   ELSE -- Valid locations EXIST
11460 
11461     -- Reset the supply assignment count
11462     g_supply_asg_count := 0;
11463 
11464     -- Bugfix 3641851:ENH6 Reset the part time assignment count
11465     g_part_time_asg_count := 0;
11466 
11467     -- Resetting cross person reporting and person count
11468     --  Moved it here from warn_anthr_tchr_asg
11469     g_cross_per_enabled := 'Y' ;
11470     g_person_count := 0 ;
11471 
11472     --added for bug fix 3803760 (PRD)
11473     g_asg_count := 0;
11474 
11475     -- MULT-LR --
11476     -- setting it to the primary assignment id.
11477     -- in create_service_lines, it may get overwritten
11478     g_primary_assignment_id := p_assignment_id;
11479 
11480     -- Added for 5460058
11481     g_equal_sal_rate             := 'Y';
11482 
11483     -- PER_LVR : Person LEaver changes
11484     -- new date variable to keep track of the latest start date
11485     -- associated with a person record,
11486     -- after which there is no person leaver event
11487     -- initialize with the g_effective_run_date
11488     g_latest_start_date := g_effective_run_date;
11489 
11490     -- Check if this person should be reported by the current run
11491     IF chk_report_person
11492          (p_business_group_id     => p_business_group_id
11493          -- PRD : reverted the change made to date.
11494          ,p_effective_date        => g_pension_year_start_date
11495          ,p_assignment_id         => p_assignment_id
11496          ) THEN
11497 
11498       debug('g_cross_person_enabled : '|| g_cross_per_enabled);
11499       -- Bugfix 3073562:GAP10
11500       -- Reset the global which stores dates for new lines of
11501       -- service as there mite be some dates stored for the
11502       -- prev assignment processed
11503       g_asg_events.DELETE;
11504 
11505       -- 8iComp Changes
11506       -- MULT-LR changes.
11507       -- g_asg_leaver_events_table.DELETE ;
11508 
11509       g_per_asg_leaver_dates.DELETE;
11510 
11511       -- PERF_ENHANC_3A : Performance Enhancements
11512       -- this table of records will be used in recalc_data_elements to store
11513       -- details corresponding of assignment IDs. Instead of calling parttime and FT
11514       -- salary function multiple times, this data collection will be used
11515       g_asg_recalc_details.DELETE;
11516 
11517 
11518       -- Check if the assignment qualifies to be on the Periodic Returns
11519       --    Pass g_pension_year_start_date as the effective date as we are
11520       --    checking as of start date of pension year. Basically, we are
11521       --    reporting annual returns from start of pension year to
11522       --    the date a person becomes a leaver, if he becomes a leaver that is.
11523 
11524       --PROFILE changes
11525       IF(NVL(g_trace,'N') = 'Y') THEN
11526         l_start_time := dbms_utility.get_time;
11527       END IF ;
11528 
11529       l_inclusion_flag := chk_has_tchr_elected_pension
11530                             (p_business_group_id            => p_business_group_id
11531                             ,p_effective_date               => g_pension_year_start_date
11532                             ,p_assignment_id                => p_assignment_id
11533                             ,p_asg_details                  => l_asg_details        -- OUT
11534                             ,p_asg_attributes               => l_pqp_asg_attributes -- OUT
11535                             );
11536 
11537       --PROFILE changes
11538       IF (NVL(g_trace,'N') = 'Y') THEN
11539         l_end_time := dbms_utility.get_time;
11540         debug('EXECUTION_TIME: chk_has_tchr_elected_pension: '||to_char(ABS(l_end_time - l_start_time)),15) ;
11541       END IF ;
11542 
11543       IF l_inclusion_flag = 'Y' THEN
11544 
11545         debug('Teacher has elected pension, now doing leaver chk',20);
11546 
11547         -- Check for leaver events between pension year start date and effective run date
11548         --    Basically, we are reporting annual returns starting from the
11549         --    start of the pension year to the leaver date, and we want to check
11550         --    for people who have become leavers in the same date range.
11551         --    However as we donot want to report people who have already been reported,
11552         --    we will need to look at previous run results to exclude people who
11553         --    have already been reported.
11554         -- Dates :
11555         --   Start date should be pension year start date
11556         --   End Date should be the end date of the run date range.
11557         l_leaver := chk_is_teacher_a_leaver
11558                             (p_business_group_id            => p_business_group_id
11559                                -- PRD : Reverted the change made to Date
11560                             ,p_effective_start_date         => GREATEST(g_pension_year_start_date
11561                                                                        ,nvl(l_asg_details.start_date
11562                                                                            ,g_pension_year_start_date
11563                                                                            )
11564                                                                        )
11565                             ,p_effective_end_date           => g_effective_run_date + 1 -- LVRDATE
11566                             ,p_assignment_id                => p_assignment_id
11567                             ,p_leaver_date                  => l_leaver_date -- OUT
11568                             );
11569 
11570 
11571         IF l_leaver = 'Y' THEN
11572 
11573           -- Check if the leaver is also a re-starter,
11574           -- i.e. there is break in service in this pension year
11575           --   But, do this only if the leaver date is present and
11576           --   less than the g_effective_run_date
11577           l_asg_details.restarter_date := NULL;
11578 
11579           IF l_leaver = 'Y'
11580              AND
11581              l_leaver_date < g_effective_run_date THEN
11582 
11583             debug('Doing restarter chk',50);
11584 
11585             l_restarter := chk_is_leaver_a_restarter
11586                                 (p_business_group_id        => p_business_group_id
11587                                 ,p_effective_start_date     => (l_leaver_date + 1)
11588                                 ,p_effective_end_date       => g_effective_run_date
11589                                 ,p_assignment_id            => p_assignment_id
11590                                 ,p_restarter_date           => l_restarter_date -- OUT
11591                                 );
11592 
11593             IF l_restarter = 'Y' THEN
11594 
11595               debug('Restarter date :'||to_char(l_restarter_date,'DDMMYY'),60);
11596 
11597               l_asg_details.restarter_date := l_restarter_date;
11598 
11599             END IF; -- l_restarter = 'Y' THEN
11600 
11601           END IF; -- l_leaver = 'Y' AND l_leaver_date < g_effective_run_date THEN
11602         ELSE -- l_leaver = 'N' THEN
11603 
11604           debug('Not a Leaver',100);
11605           -- PER_LVR : Person Leaver changes
11606           -- keep the leaver date in the global.
11607           -- There is no person level leaver after this date
11608           -- Will use this date, when checking for person level lever event.
11609           g_latest_start_date := LEAST (g_latest_start_date, l_asg_details.start_date);
11610           -- Start of Bug fix 5408932
11611           -- If this is the only assignment then he should not be reported
11612           l_inclusion_flag := 'N';
11613 
11614           -- Check for Secondary assignments.
11615           l_look_for_sec_asgs := TRUE;
11616 
11617           -- Teacher is not a leaver. The primary assignment should not be reported.
11618           l_asg_details.report_asg := 'N';
11619           -- End of Bug fix 5408932
11620 
11621         END IF ; -- l_leaver = 'N' THEN
11622           --  Assignment has passed all checks save the details in
11623           --   1) Type 4 global collection g_ext_asg_details
11624           --   2) Type 1 global collection g_ext_asg_details
11625           --      Has more stuff than the Type 4 counterpart
11626           --   3) Type 1 global collection g_ext_aat_details
11627           --
11628           -- Bug 5408932
11629           -- Store the globals only if it is the terminated assignment
11630      IF l_inclusion_flag = 'Y' THEN
11631         -- First assign the leaver date to the asg details rowtype variable
11632         debug('Storing values in globals',70);
11633         l_asg_details.leaver_date := l_leaver_date;
11634 
11635         --   1) Type 4 global collection g_ext_asg_details
11636         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).person_id         := l_asg_details.person_id;
11637         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).assignment_id     := l_asg_details.assignment_id;
11638         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).start_date        := l_asg_details.start_date;
11639         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).effective_end_date:= l_asg_details.effective_end_date;
11640         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).creation_date     := l_asg_details.creation_date;
11641         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).location_id       := l_asg_details.location_id;
11642         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).asg_emp_cat_cd    := l_asg_details.asg_emp_cat_cd;
11643         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).ext_emp_cat_cd    := l_asg_details.ext_emp_cat_cd;
11644 
11645         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number       :=
11646             pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number;
11647 
11648         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).tp_safeguarded_grade :=
11649             l_pqp_asg_attributes.tp_safeguarded_grade;
11650 
11651         --   2) Type 1 global collection g_ext_asg_details
11652         g_ext_asg_details(p_assignment_id) := l_asg_details;
11653 
11654         -- 3) Type 1 global collection g_ext_aat_details
11655         g_ext_asg_attributes(l_pqp_asg_attributes.assignment_id) := l_pqp_asg_attributes;
11656 
11657 
11658         -- Bugfix 3073562:GAP9a
11659         -- Raise a warning if the assignment is at a
11660         -- supply location and full time
11661         warn_if_supply_tchr_is_ft
11662             (p_assignment_id            => p_assignment_id
11663             ,p_establishment_number     =>
11664                 pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number
11665             ,p_ext_emp_cat_code         => l_asg_details.ext_emp_cat_cd
11666             );
11667 
11668         -- Bugfix 3073562:GAP9b
11669         -- Increment the supply asg count if this is a supply assignment
11670         IF pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number = '0966' THEN
11671           g_supply_asg_count := g_supply_asg_count + 1;
11672           debug('Incrementing supply teacher count',75);
11673         END IF;
11674 
11675         -- Bugfix 3641851:ENH6
11676         -- Increment the part time assignment count if the asg is part time
11677         IF l_asg_details.ext_emp_cat_cd = 'P' THEN
11678           g_part_time_asg_count := g_part_time_asg_count + 1;
11679           debug('Incrementing part time assignment count',76);
11680         END IF;
11681 
11682         -- Bugfix 3803760:TERMASG : Incrementing asg count (PRD)
11683         g_asg_count := g_asg_count + 1;
11684 
11685         debug('g_asg_count: '|| to_char(g_asg_count), 75) ;
11686         debug('Fetching secondary asgs',80);
11687 
11688         debug('g_cross_person_enabled : '|| (g_cross_per_enabled),57);
11689 
11690         -- This procedure also saves the secondary assignments in the global record varialbes
11691         fetch_secondary_assignments
11692                   (p_primary_assignment_id        => p_assignment_id
11693                   ,p_person_id                    => l_asg_details.person_id
11694                   -- PRD : reverted the change made to date
11695                   ,p_effective_date               => g_pension_year_start_date
11696                   ,p_must_be_a_leaver             => TRUE
11697                   );
11698      ELSE
11699         debug('Primary Asg '|| to_char(p_assignment_id)||' is not terminated', 84) ;
11700      END IF; -- l_inclusion_flag = 'Y' Bug 5408932
11701         debug('g_asg_count: '|| to_char(g_asg_count), 85) ;
11702         debug('Number of TP1 teachers on periodic report :'||
11703           fnd_number.number_to_canonical(pqp_gb_tp_pension_extracts.g_ext_asg_details.COUNT));
11704 
11705       ELSE
11706         -- Bugfix 3073562:GAP6
11707         -- Continue looking for secondary asgs
11708         l_look_for_sec_asgs := TRUE;
11709       END IF; -- l_inclusion_flag = 'Y' THEN
11710 
11711       -- Bugfix 3073562:GAP6
11712       -- Even though this asg is not being included, we need to
11713       -- look for prospective secondary leaver assignments
11714       IF l_inclusion_flag = 'N'
11715          AND l_look_for_sec_asgs THEN
11716 
11717         debug('Primary NOT included, checking secondary asgs',110);
11718 
11719         l_asg_count := g_ext_asg_details.COUNT;
11720 
11721         -- Get person id if its NULL
11722         IF l_asg_details.person_id IS NULL THEN
11723 
11724           debug('Person Id is NULL, get it',120);
11725 
11726           OPEN csr_asg_details_up(p_assignment_id);
11727           FETCH csr_asg_details_up INTO l_temp_asg_details;
11728           CLOSE csr_asg_details_up;
11729 
11730           l_asg_details.person_id := l_temp_asg_details.person_id;
11731 
11732         END IF;
11733 
11734         debug('g_asg_count: '|| to_char(g_asg_count), 125) ;
11735 
11736         fetch_secondary_assignments
11737                 (p_primary_assignment_id        => p_assignment_id
11738                 ,p_person_id                    => l_asg_details.person_id
11739                 -- PRD : reverted the change made to date.
11740                 ,p_effective_date               => g_pension_year_start_date
11741                 ,p_must_be_a_leaver             => TRUE
11742                 );
11743 
11744         debug('g_asg_count: '|| to_char(g_asg_count), 128) ;
11745         -- If proc fetch_secondary_assignments added any new asgs to
11746         -- global collection, resulting in higher count, then it means
11747         -- we have secondary asgs for this person.
11748         IF g_ext_asg_details.COUNT > l_asg_count THEN
11749 
11750           debug('Secondary asg Teacher, report this person',130);
11751           l_inclusion_flag := 'Y';
11752 
11753         END IF;
11754 
11755       END IF; -- l_inclusion_flag = 'N' AND l_look_for_sec_asgs THEN
11756 
11757     ELSE -- chk_report_person
11758       debug(l_proc_name,140);
11759       l_inclusion_flag := 'N';
11760     END IF; -- chk_report_person
11761 
11762     debug('Inclusion Flag :'||l_inclusion_flag,150);
11763 
11764     IF l_inclusion_flag = 'Y' THEN
11765 
11766       -- PER_LVR change
11767       -- This piece of code checks for the person level leaver events
11768 
11769       debug('g_latest_start_date : '||to_char(g_latest_start_date),160 );
11770       debug('teacher_start_date: '|| to_char(g_ext_asg_details(p_assignment_id).teacher_start_date));
11771 
11772       -- 1. check if there is any one continuous assignment over the period Then person is not a leaver.
11773   --   IF  g_latest_start_date = g_ext_asg_details(p_assignment_id).teacher_start_date THEN -- bug Fix 9383926
11774       IF  g_latest_start_date <> g_effective_run_date THEN
11775         debug('There is atleast one continuous asg over the period...',170);
11776         l_inclusion_flag := 'N';
11777       ELSE
11778         -- check for Person level Leaver events.
11779         l_inclusion_flag := chk_person_leaver
11780                                 ( p_assignment_id => p_assignment_id
11781                                  ,p_person_id     => l_asg_details.person_id
11782                                  );
11783 
11784         -- 2. store_leaver_restarter_dates
11785         -- 3. sort_stored_events
11786         -- 4. browse events and store person level rehire dates as per g_asg_count status
11787         --    a). if leaver date found then chk if person has to be reported for this date
11788         --    b). if yes continue
11789         --    c). if no, stop, this needs to be reported.
11790         debug('l_inclusion_flag: '||l_inclusion_flag,180);
11791       END IF;
11792         debug('l_inclusion_flag: '||l_inclusion_flag,190);
11793 
11794     END IF; --l_inclusion_flag = 'Y'
11795 
11796     IF l_inclusion_flag = 'Y' THEN
11797 
11798       debug(l_proc_name, 210 );
11799       -- Now we know that this person wll be reported
11800       -- Raise all the errors and warnings
11801 
11802       -- Bugfix 3073562:GAP9b
11803       -- If this person has more than one supply assignments
11804       -- then raise a warning.
11805       IF g_supply_asg_count > 1 THEN
11806         l_error := pqp_gb_tp_extract_functions.raise_extract_warning
11807                    (p_assignment_id => p_assignment_id
11808                    ,p_error_text    => 'BEN_93656_SUPPLY_TCHR_MULTIASG'
11809                    ,p_error_number  => 93656
11810                    );
11811       END IF;
11812 
11813       -- The following piece of code raises a warning if
11814       -- there exist more than one lea with the same lea Number within a BG.
11815       -- the warning is raised for the first valid assignment for a single Run.
11816       -- the flag for warning is set during the global setting through set_extract_globals.
11817       pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
11818       pqp_gb_tp_pension_extracts.warn_if_multi_lea_exist (p_assignment_id => p_assignment_id);
11819       pqp_gb_tp_pension_extracts.g_nested_level := 0;
11820 
11821       -- The following proc raises a warning, if there is
11822       -- one FT teaching asg for the person
11823       -- and there are multiple person records.
11824       -- the proc checks for the flags g_cross_person_enabled and g_person_count
11825       -- and resets those flags.
11826       warn_anthr_tchr_asg(p_assignment_id => p_assignment_id);
11827 
11828       -- Bugfix 3803760:FTSUPPLY (PRD)
11829       -- Set the effective assignments as of teacher start date
11830       debug('g_asg_count: '|| to_char(g_asg_count), 255) ;
11831       set_effective_assignments
11832          (p_primary_assignment_id     => p_assignment_id
11833          ,p_effective_date            => g_ext_asg_details(p_assignment_id).teacher_start_date
11834          );
11835     debug('g_asg_count: '|| to_char(g_asg_count), 258) ;
11836     END IF ; -- l_inclusion_flag = 'Y'
11837 
11838   END IF; -- pqp_gb_tp_pension_extracts.g_criteria_estbs.COUNT = 0
11839 
11840   debug('l_inclusion_flag: '||l_inclusion_flag,260);
11841   debug_exit(l_proc_name);
11842 
11843   RETURN l_inclusion_flag;
11844 
11845 EXCEPTION
11846   WHEN OTHERS THEN
11847     debug_exit(' Others in '||l_proc_name
11848               ,'Y' -- turn trace off
11849               );
11850     RAISE;
11851 END; -- chk_tp1_criteria_periodic
11852 
11853 
11854 --
11855 -- Criteria for Type 1 Annual with facility to Include/Exclude Leavers
11856 --
11857 FUNCTION chk_tp1_criteria_annual
11858   (p_business_group_id        IN      NUMBER  -- context
11859   ,p_effective_date           IN      DATE    -- context
11860   ,p_assignment_id            IN      NUMBER  -- context
11861   ) RETURN VARCHAR2 -- Y or N
11862 IS
11863 
11864   -- Variable Declaration
11865   l_inclusion_flag      VARCHAR2(1) := 'Y';
11866   l_leaver              VARCHAR2(1) := 'N';
11867   l_leaver_date         DATE;
11868   l_already_reported    VARCHAR2(1) := 'N';
11869   l_restarter           VARCHAR2(1) := 'N';
11870   l_restarter_date      DATE := NULL;
11871   l_person_id           per_all_people_f.person_id%TYPE;
11872   l_asg_count           NUMBER;
11873   l_error               NUMBER;
11874 
11875   -- Rowtype Variable Declaration
11876   l_asg_details        csr_asg_details_up%ROWTYPE;
11877   l_per_details        csr_asg_details_up%ROWTYPE;
11878   l_pqp_asg_attributes csr_pqp_asg_attributes_up%ROWTYPE;
11879   l_temp_asg_details    csr_asg_details_up%ROWTYPE;
11880 
11881 
11882 
11883   l_proc_name          VARCHAR2(61):=
11884      g_proc_name||'chk_tp1_criteria_annual';
11885 
11886 BEGIN -- chk_tp1_criteria_annual
11887 
11888   debug_enter(l_proc_name);
11889 
11890   debug('Assignment Id :'||to_char(p_assignment_id),10);
11891   debug('Effective_date :'||to_char(p_effective_date,'dd/mm/yyyy'),15);
11892 
11893   IF g_business_group_id IS NULL THEN
11894 
11895     -- Bugifx 2848696 : Now using ben_ext_person.g_effective_date
11896     -- instead of p_effective_date
11897     set_t1_extract_globals
11898         (p_business_group_id
11899         ,ben_ext_person.g_effective_date   -- p_effective_date
11900         ,p_assignment_id
11901         );
11902 
11903 
11904     -- Bugifx 2848696 : Now using ben_ext_person.g_effective_date
11905     -- instead of p_effective_date
11906     pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
11907     g_reporting_mode := upper(pqp_gb_tp_pension_extracts.get_extract_udt_info
11908                                 ('Attribute Location Qualifier 1' -- column
11909                                 ,'Criteria'      -- row
11910                                 ,ben_ext_person.g_effective_date   -- p_effective_date
11911                                 )
11912                              );
11913     pqp_gb_tp_pension_extracts.g_nested_level := 0;
11914 
11915     set_annual_run_dates;
11916 
11917     -- Fetch element ids from balance's
11918     fetch_eles_for_t1_bals (p_assignment_id  => p_assignment_id
11919                            ,p_effective_date => g_pension_year_start_date
11920                            );
11921 
11922   END IF;
11923 
11924   -- Bugfix -- Bugfix 3671727: Performance enhancement
11925   --    If no location exists in the list of valid criteria
11926   --    establishments, then no point doing all checks
11927   --    Just warn once and skip every assignment
11928   IF pqp_gb_tp_pension_extracts.g_criteria_estbs.COUNT = 0 THEN
11929 
11930     debug('Setting inclusion flag to N as no locations EXIST.', 15);
11931     l_inclusion_flag := 'N';
11932 
11933     pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
11934     -- Call TP4 pkg proc to warning for no locations
11935     pqp_gb_tp_pension_extracts.warn_if_no_loc_exist
11936         (p_assignment_id => p_assignment_id) ;
11937     pqp_gb_tp_pension_extracts.g_nested_level := 0;
11938 
11939   ELSE -- Valid locations EXIST
11940 
11941     debug('Reporting Mode for leavers is :'||g_reporting_mode,20);
11942 
11943     -- Reset the supply assignment count
11944     g_supply_asg_count := 0;
11945 
11946     -- Bugfix 3641851:ENH6 Reset the part time assignment count
11947     g_part_time_asg_count := 0;
11948 
11949     -- Resetting cross person reporting and person count
11950     --  Moved it here from warn_anthr_tchr_asg
11951     g_cross_per_enabled := 'Y' ;
11952     g_person_count := 0 ;
11953 
11954     -- Added for bugfix 3803760:TERMASG
11955     g_asg_count := 0;
11956 
11957     g_teach_asg_count :=0;
11958 
11959     -- MULT-LR --
11960     -- setting it to the primary assignment id.
11961     -- in create_service_lines, it may get overwritten
11962     g_primary_assignment_id := p_assignment_id;
11963 
11964       -- Added for 5460058
11965     g_equal_sal_rate             := 'Y';
11966 
11967     g_supp_teacher := 'N';
11968 
11969     -- Check if this person should be reported by the current run
11970     IF chk_report_person
11971          (p_business_group_id     => p_business_group_id
11972          ,p_effective_date        => g_pension_year_start_date
11973          ,p_assignment_id         => p_assignment_id
11974          ) THEN
11975 
11976       debug('g_cross_person_enabled : '|| g_cross_per_enabled);
11977       -- Reset the global which stores dates for new lines of
11978       -- service as there mite be some dates stored for the
11979       -- prev assignment processed
11980       g_asg_events.DELETE;
11981 
11982 
11983       -- PERF_ENHANC_3A : Performance Enhancements
11984       -- this table of records will be used in recalc_data_elements to store
11985       -- details corresponding of assignment IDs. Instead of calling parttime and FT
11986       -- salary function multiple times, this data collection will be used
11987       g_asg_recalc_details.DELETE;
11988 
11989 
11990       -- 8iComp Changes
11991       -- MULT-LR changes.
11992       -- g_asg_leaver_events_table.DELETE ;
11993 
11994       g_per_asg_leaver_dates.DELETE;
11995 
11996       -- Check if the assignment qualifies to be on the Periodic Returns
11997       --    Pass g_pension_year_start_date as the effective date as we are
11998       --    checking as of start date of pension year. Basically, we are
11999       --    reporting annual returns from start of pension year to
12000       --    the date a person becomes a leaver, if he becomes a leaver that is.
12001       l_inclusion_flag := chk_has_tchr_elected_pension
12002                             (p_business_group_id            => p_business_group_id
12003                             ,p_effective_date               => g_pension_year_start_date
12004                             ,p_assignment_id                => p_assignment_id
12005                             ,p_asg_details                  => l_asg_details        -- OUT
12006                             ,p_asg_attributes               => l_pqp_asg_attributes -- OUT
12007                             );
12008 
12009       IF l_inclusion_flag = 'Y' THEN
12010       -- 1)
12011 
12012         debug('Teacher has elected pension',30);
12013 
12014         -- Check for leaver events between pension year start date and effective run date
12015         -- For annual report effective run date should be pension year end date
12016         --    Basically, we are reporting annual returns starting from the
12017         --    start of the pension year to the end of pension year, and we want
12018         --    to check for people who have become leavers in the same date range.
12019         -- Dates :
12020         --   Start date should be pension year start date
12021         --   End Date should be the end date of the run date range, i.e. end of pension year
12022         l_leaver := chk_is_teacher_a_leaver
12023                             (p_business_group_id            => p_business_group_id
12024                             ,p_effective_start_date         => GREATEST(g_pension_year_start_date
12025                                                                        ,nvl(l_asg_details.start_date
12026                                                                            ,g_pension_year_start_date
12027                                                                            )
12028                                                                        )
12029                             ,p_effective_end_date           => nvl(g_effective_run_date
12030                                                                   ,g_pension_year_end_date)
12031                             ,p_assignment_id                => p_assignment_id
12032                             ,p_leaver_date                  => l_leaver_date -- OUT
12033                             );
12034 
12035 
12036    /*     IF nvl(g_reporting_mode,'EXCLUDE') = 'EXCLUDE'
12037            AND
12038            l_leaver = 'Y' THEN
12039 
12040           -- Set the inclusion flag to 'N'o as this person is a leaver
12041           -- and we are running in EXCLUDE leavers reporting mode
12042           l_inclusion_flag := 'N';
12043 
12044         END IF; -- nvl(g_reporting_mode,'EXCLUDE') = 'EXCLUDE' THEN  */-- commenting this chk, since it is chking primary assinments 1st leaver event
12045 	                                                             --   with out considering 2ndary asgs leaver events
12046 
12047         IF l_inclusion_flag = 'Y' THEN
12048         -- 2)
12049 
12050           -- Assignment has passed all checks save the details in
12051           --   1) Type 4 global collection g_ext_asg_details
12052           --   2) Type 1 global collection g_ext_asg_details
12053           --        Has more stuff than the Type 4 counterpart
12054           --   3) Type 1 global collection g_ext_aat_details
12055           --
12056 
12057           -- Check if the leaver is also a re-starter,
12058           -- i.e. there is break in service in this pension year
12059           --   But, do this only if the leaver date is present and
12060           --   less than the g_effective_run_date
12061           l_asg_details.restarter_date := NULL;
12062 
12063           IF l_leaver = 'Y'
12064              AND
12065              l_leaver_date < g_effective_run_date THEN
12066 
12067             l_restarter := chk_is_leaver_a_restarter
12068                                 (p_business_group_id        => p_business_group_id
12069                                 ,p_effective_start_date     => (l_leaver_date + 1)
12070                                 ,p_effective_end_date       => g_effective_run_date
12071                                 ,p_assignment_id            => p_assignment_id
12072                                 ,p_restarter_date           => l_restarter_date -- OUT
12073                                 );
12074 
12075             IF l_restarter = 'Y' THEN
12076 
12077               debug('Restarter',40);
12078               l_asg_details.restarter_date := l_restarter_date;
12079 
12080             END IF; -- l_restarter = 'Y' THEN
12081 
12082           END IF; -- l_leaver = 'Y' AND l_leaver_date < g_effective_run_date THEN
12083 
12084 
12085           /* Replacing this with assignment statements for individual elements as
12086              there is a plan to extent the assignment cursor to include more cols.
12087 
12088           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id) := l_asg_details;
12089 
12090           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number :=
12091             pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number;
12092 
12093           -- already populated this in l_asg_details before received in this function
12094           -- pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).start_date := l_pension_start_date;
12095 
12096           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).tp_safeguarded_grade :=
12097             l_pqp_asg_attributes.tp_safeguarded_grade;
12098           */
12099 
12100           -- First assign the leaver date to the asg details rowtype variable
12101           IF l_leaver = 'Y' THEN
12102             l_asg_details.leaver_date := l_leaver_date;
12103           ELSE -- l_leaver = 'N'
12104             l_asg_details.leaver_date := NULL;
12105           END IF; -- l_leaver = 'Y' THEN
12106 
12107           debug('Storing values in globals',50);
12108 
12109           --   1) Type 4 global collection g_ext_asg_details
12110 
12111           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).person_id           := l_asg_details.person_id;
12112           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).assignment_id       := l_asg_details.assignment_id;
12113           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).start_date          := l_asg_details.start_date;
12114           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).effective_end_date  := l_asg_details.effective_end_date;
12115           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).creation_date       := l_asg_details.creation_date;
12116           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).location_id         := l_asg_details.location_id;
12117           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).asg_emp_cat_cd      := l_asg_details.asg_emp_cat_cd;
12118           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).ext_emp_cat_cd      := l_asg_details.ext_emp_cat_cd;
12119 
12120           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number          :=
12121             pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number;
12122 
12123           pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).tp_safeguarded_grade :=
12124             l_pqp_asg_attributes.tp_safeguarded_grade;
12125 
12126           --   2) Type 1 global collection g_ext_asg_details
12127 	  g_teach_asg_count :=g_teach_asg_count +1;
12128           g_ext_asg_details(p_assignment_id) := l_asg_details;
12129 
12130           -- 3) Type 1 global collection g_ext_aat_details
12131           g_ext_asg_attributes(l_pqp_asg_attributes.assignment_id) := l_pqp_asg_attributes;
12132 
12133           -- Bugfix 3073562:GAP9a
12134           -- Raise a warning if the assignment is at a
12135           -- supply location and full time
12136           warn_if_supply_tchr_is_ft
12137             (p_assignment_id            => p_assignment_id
12138             ,p_establishment_number     =>
12139                 pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number
12140             ,p_ext_emp_cat_code         => l_asg_details.ext_emp_cat_cd
12141             );
12142 
12143           -- Bugfix 3073562:GAP9b
12144           -- Increment the supply asg count if this is a supply assignment
12145           IF pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number = '0966' THEN
12146             g_supply_asg_count := g_supply_asg_count + 1;
12147             debug('Incrementing supply teacher count',55);
12148           END IF;
12149 
12150           -- Bugfix 3641851:ENH6
12151           -- Increment the part time assignment count if the asg is part time
12152           IF l_asg_details.ext_emp_cat_cd = 'P' THEN
12153             g_part_time_asg_count := g_part_time_asg_count + 1;
12154             debug('Incrementing part time assignment count',56);
12155           END IF;
12156 
12157           -- Bugfix 3803760:TERMASG : Incrementing asg count
12158           g_asg_count := g_asg_count + 1;
12159 
12160           debug('g_asg_count: '|| to_char(g_asg_count), 55) ;
12161 
12162           debug('Fetching secondary asgs',60);
12163           -- This procedure also saves the secondary assignments in the global record varialbes
12164           fetch_secondary_assignments
12165                     (p_primary_assignment_id        => p_assignment_id
12166                     ,p_person_id                    => l_asg_details.person_id
12167                     ,p_effective_date               => g_pension_year_start_date
12168                     ,p_must_be_a_leaver             => FALSE
12169                     );
12170           debug('g_asg_count: '|| to_char(g_asg_count), 65) ;
12171           debug('Number of TP1 teachers on annual report :'||
12172           fnd_number.number_to_canonical(pqp_gb_tp_pension_extracts.g_ext_asg_details.COUNT),70);
12173           --
12174         END IF; -- 2) l_inclusion_flag = 'Y' THEN
12175         --
12176       ELSE -- 1) inclusion flag is 'N'
12177 
12178         -- Bugfix 3073562:GAP6
12179         -- Primary asg not to be included
12180         -- but check for any secondary teaching asgs
12181 
12182         debug('Primary NOT teacher, checking secondary asgs',80);
12183 
12184         l_asg_count := g_ext_asg_details.COUNT;
12185 
12186         -- Get person id if its NULL
12187         IF l_asg_details.person_id IS NULL THEN
12188 
12189           debug('Person Id is NULL, get it',90);
12190 
12191           OPEN csr_asg_details_up(p_assignment_id);
12192           FETCH csr_asg_details_up INTO l_temp_asg_details;
12193           CLOSE csr_asg_details_up;
12194 
12195           l_asg_details.person_id := l_temp_asg_details.person_id;
12196 
12197         END IF;
12198 
12199         debug('g_asg_count: '|| to_char(g_asg_count), 95) ;
12200         fetch_secondary_assignments
12201                 (p_primary_assignment_id        => p_assignment_id
12202                 ,p_person_id                    => l_asg_details.person_id
12203                 ,p_effective_date               => g_pension_year_start_date
12204                 ,p_must_be_a_leaver             => FALSE
12205                 );
12206         debug('g_asg_count: '|| to_char(g_asg_count), 95) ;
12207         -- If proc fetch_secondary_assignments added any new asgs to
12208         -- global collection, resulting in higher count, then it means
12209         -- we have secondary asgs for this person.
12210         IF g_ext_asg_details.COUNT > l_asg_count THEN
12211 
12212           debug('Secondary asg Teacher, report this person',100);
12213           l_inclusion_flag := 'Y';
12214 
12215         END IF;
12216         --
12217       END IF; -- 1) l_inclusion_flag = 'Y' THEN
12218 
12219     ELSE -- chk_report_person
12220       debug(l_proc_name,110);
12221       l_inclusion_flag := 'N';
12222     END IF; -- chk_report_person
12223 
12224    debug('l_inclusion_flag : '|| l_inclusion_flag ,111);
12225 
12226    IF nvl(g_reporting_mode,'EXCLUDE') = 'EXCLUDE'  and l_inclusion_flag = 'Y' then
12227    -- if criteria is 'Exclude' then we have to chk persons all asgs leaver events
12228    debug('Criteria is Exclude, need to check person is leaver or not.' ,112);
12229    l_inclusion_flag := chk_person_leaver
12230                                 ( p_assignment_id => p_assignment_id
12231                                  ,p_person_id     => l_asg_details.person_id
12232                                  );
12233     End if;
12234 
12235 
12236     -- Bugfix 3073562:GAP9b
12237     -- If this person has more than one supply assignments
12238     -- then raise a warning.
12239     IF g_supply_asg_count > 1 THEN
12240       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
12241                    (p_assignment_id => p_assignment_id
12242                    ,p_error_text    => 'BEN_93656_SUPPLY_TCHR_MULTIASG'
12243                    ,p_error_number  => 93656
12244                    );
12245     END IF;
12246 
12247     debug('Inclusion Flag :'||l_inclusion_flag,120);
12248 
12249     IF l_inclusion_flag = 'Y' THEN
12250       -- The following piece of code raises a warning if
12251       -- there exist more than one lea with the same lea Number within a BG.
12252       -- the warning is raised for the first valid assignment for a single Run.
12253       -- the flag for warning is set during the global setting through set_extract_globals.
12254       pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
12255       pqp_gb_tp_pension_extracts.warn_if_multi_lea_exist (p_assignment_id => p_assignment_id);
12256       pqp_gb_tp_pension_extracts.g_nested_level := 0;
12257 
12258       -- The following proc raises a warning, if there is
12259       -- one FT teaching asg for the person
12260       -- and there are multiple person records.
12261       -- the proc checks for the flags g_cross_person_enabled and g_person_count
12262       -- and resets those flags.
12263       warn_anthr_tchr_asg(p_assignment_id => p_assignment_id);
12264 
12265       -- Bugfix 3803760:FTSUPPLY
12266       -- Set the effective assignments as of teacher start date
12267       debug('g_asg_count: '|| to_char(g_asg_count), 130) ;
12268 
12269       set_effective_assignments
12270          (p_primary_assignment_id     => p_assignment_id
12271          ,p_effective_date            => g_ext_asg_details(p_assignment_id).teacher_start_date
12272          );
12273       debug('g_asg_count: '|| to_char(g_asg_count), 140) ;
12274 
12275 
12276     END IF;
12277 
12278   END IF; -- pqp_gb_tp_pension_extracts.g_criteria_estbs.COUNT = 0
12279 
12280   debug_exit(l_proc_name);
12281 
12282   RETURN l_inclusion_flag;
12283 
12284 EXCEPTION
12285   WHEN OTHERS THEN
12286     debug_exit(' Others in '||l_proc_name
12287               ,'Y' -- turn trace off
12288               );
12289     RAISE;
12290 END; -- chk_tp1_criteria_annual
12291 --
12292 -- chk_report_assignment - overloaded
12293 --
12294 FUNCTION chk_report_assignment
12295     (p_assignment_id            IN  NUMBER
12296     -- Bugfix 3641851:CBF1 : Added new parameter effective date
12297     ,p_effective_date           IN  DATE
12298     ,p_secondary_assignment_id  OUT NOCOPY NUMBER
12299     ) RETURN VARCHAR2
12300 IS
12301 
12302  l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
12303  l_retval              VARCHAR2(1) := 'Y';
12304  l_effective_date      DATE;
12305 
12306  l_proc_name           varchar2(60) := g_proc_name || 'chk_report_assignment1';
12307 
12308 BEGIN -- chk_report_assignment
12309 
12310   debug_enter(l_proc_name);
12311 
12312   debug('p_assignment_id :'||to_char(p_assignment_id), 10);
12313 
12314   -- Bugfix 3803760:FTSUPPLY :  Added the override logic
12315   IF g_override_ft_asg_id IS NULL THEN
12316 
12317     IF (g_ext_asg_details.EXISTS(p_assignment_id)
12318         AND
12319        g_ext_asg_details(p_assignment_id).report_asg = 'Y'
12320        ) THEN
12321 
12322       debug(l_proc_name, 20);
12323       -- Bugfix 3641851:CBF1: Added date check
12324       --  When primary asg starts at a later date than secondary asg,
12325       --  the func that evaluates an attribute from primary asg records
12326       --  can fail (and raise warning) coz there mite be no row present
12327       --  for the primary asg. However, the primary is to be reported
12328       --  frm a later date so we cannot ignore it altogether.
12329       --  Therefore, we need to use the secondary asgs row for getting
12330       --  attributes if the primary is not valid at the given eff date.
12331       --
12332       l_effective_date := nvl(p_effective_date, g_ext_asg_details(p_assignment_id).teacher_start_date);
12333       debug('l_effective_date :'||to_char(l_effective_date, 'DD/MM/YYYY'), 30);
12334 
12335       -- MULT-LR --
12336       -- Use the new Function to check the effectivness of an assignment
12337       -- it takes care of multiple Leaver-Restarter events
12338       -- where as the old logic used to take into account
12339       -- only the first restarter event.
12340       IF ( chk_effective_asg (
12341                p_assignment_id  => p_assignment_id
12342               ,p_effective_date => l_effective_date
12343                               ) ='Y'
12344           ) THEN
12345 
12346         debug(l_proc_name, 40);
12347         l_assignment_id := p_assignment_id;
12348 
12349       ELSE
12350         l_assignment_id := g_ext_asg_details(p_assignment_id).secondary_assignment_id;
12351       END IF;
12352 
12353       l_retval := 'Y';
12354 
12355     ELSE
12356       debug(l_proc_name, 50);
12357       l_assignment_id := g_ext_asg_details(p_assignment_id).secondary_assignment_id;
12358       l_retval := 'N';
12359     END IF;
12360 
12361   ELSE -- g_override_ft_asg_id is NOT NULL
12362 
12363     l_assignment_id := g_override_ft_asg_id;
12364 
12365     IF g_override_ft_asg_id = p_assignment_id THEN
12366       l_retval := 'Y';
12367     ELSE
12368       l_retval := 'N';
12369     END IF;
12370 
12371   END IF;
12372 
12373   p_secondary_assignment_id := l_assignment_id;
12374 
12375   debug('p_secondary_assignment_id :'||to_char(p_secondary_assignment_id), 60);
12376   debug_exit(l_proc_name);
12377 
12378   RETURN l_retval;
12379 
12380 EXCEPTION
12381   WHEN OTHERS THEN
12382     p_secondary_assignment_id := NULL;
12383     debug_exit(' Others in '||l_proc_name);
12384     RAISE;
12385 END; -- chk_report_assignment
12386 
12387 --
12388 -- chk_report_assignment - overloaded
12389 --
12390 FUNCTION chk_report_assignment
12391     (p_assignment_id            IN  NUMBER
12392     -- Bugfix 3641851:CBF1 : Added new parameter effective date
12393     ,p_effective_date           IN  DATE
12394     ,p_report_assignment        OUT NOCOPY VARCHAR2
12395     ) RETURN NUMBER
12396 IS
12397 
12398  l_assignment_id        per_all_assignments_f.assignment_id%TYPE;
12399  l_retval               VARCHAR2(1) := 'Y';
12400  l_effective_date       DATE;
12401 
12402  l_proc_name           varchar2(60) := g_proc_name || 'chk_report_assignment2';
12403 
12404 BEGIN -- chk_report_assignment
12405 
12406   debug_enter(l_proc_name);
12407   debug('p_assignment_id :'||to_char(p_assignment_id), 10);
12408 
12409   -- Bugfix 3803760:FTSUPPLY :  Added the override logic
12410   IF g_override_ft_asg_id IS NULL THEN
12411 
12412     IF (g_ext_asg_details.EXISTS(p_assignment_id)
12413         AND
12414        g_ext_asg_details(p_assignment_id).report_asg = 'Y'
12415        ) THEN
12416 
12417       debug(l_proc_name, 20);
12418       -- Bugfix 3641851:CBF1: Added date check
12419       --  When primary asg starts at a later date than secondary asg,
12420       --  the func that evaluates an attribute from primary asg records
12421       --  can fail (and raise warning) coz there mite be no row present
12422       --  for the primary asg. However, the primary is to be reported
12423       --  frm a later date so we cannot ignore it altogether.
12424       --  Therefore, we need to use the secondary asgs row for getting
12425       --  attributes if the primary is not valid at the given eff date.
12426       --
12427       l_effective_date := nvl(p_effective_date, g_ext_asg_details(p_assignment_id).teacher_start_date);
12428       debug('l_effective_date :'||to_char(l_effective_date, 'DD/MM/YYYY'), 30);
12429 
12430       -- MULT-LR --
12431       -- Use the new Function to check the effectivness of an assignment
12432       -- it takes care of multiple Leaver-Restarter events
12433       -- where as the old logic used to take into account
12434       -- only the first restarter event.
12435       IF ( chk_effective_asg (
12436                p_assignment_id  => p_assignment_id
12437               ,p_effective_date => l_effective_date
12438                               ) ='Y'
12439           ) THEN
12440 
12441         debug(l_proc_name, 40);
12442         l_assignment_id := p_assignment_id;
12443 
12444       ELSE
12445         l_assignment_id := g_ext_asg_details(p_assignment_id).secondary_assignment_id;
12446       END IF;
12447 
12448       l_retval := 'Y';
12449 
12450     ELSE
12451       debug(l_proc_name, 50);
12452       l_assignment_id := g_ext_asg_details(p_assignment_id).secondary_assignment_id;
12453       l_retval := 'N';
12454     END IF;
12455 
12456   ELSE -- g_override_ft_asg_id is NOT NULL
12457 
12458     l_assignment_id := g_override_ft_asg_id;
12459 
12460     IF g_override_ft_asg_id = p_assignment_id THEN
12461       l_retval := 'Y';
12462     ELSE
12463       l_retval := 'N';
12464     END IF;
12465 
12466   END IF;
12467 
12468   p_report_assignment := l_retval;
12469 
12470   debug('p_report_assignment :'||p_report_assignment, 60);
12471   debug('l_assignment_id :'||to_char(l_assignment_id), 70);
12472 
12473   debug_exit(l_proc_name);
12474 
12475   RETURN l_assignment_id;
12476 
12477 EXCEPTION
12478   WHEN OTHERS THEN
12479     p_report_assignment := NULL;
12480     debug_exit(' Others in '||l_proc_name);
12481     RAISE;
12482 END; -- chk_report_assignment
12483 
12484 --
12485 -- Start Date
12486 --
12487 FUNCTION get_tp1_start_date
12488   (p_assignment_id     IN      NUMBER
12489   )
12490   RETURN VARCHAR2
12491 IS
12492   l_start_date  VARCHAR2(600);
12493 BEGIN
12494 
12495   -- Bugfix 3641851:CBF1: Now returning teacher_start_date
12496   l_start_date := to_char(nvl(g_ext_asg_details(p_assignment_id).teacher_start_date
12497                              ,g_ext_asg_details(p_assignment_id).start_date)
12498                          ,'DDMMYY'
12499                          );
12500 
12501   RETURN l_start_date;
12502 END; -- get_tp1_start_date
12503 --
12504 -- End Date
12505 --
12506 FUNCTION get_tp1_end_date
12507   (p_assignment_id     IN      NUMBER
12508   ) RETURN VARCHAR2
12509 IS
12510 
12511 BEGIN
12512 
12513   RETURN to_char(LEAST(nvl(g_ext_asg_details(p_assignment_id).leaver_date
12514                           ,g_effective_run_date
12515                           )
12516                       ,g_effective_run_date
12517                       )
12518                 ,'DDMMYY'
12519                 );
12520 END; -- get_tp1_end_date
12521 --
12522 -- Withdrawal Confirmation
12523 --
12524 FUNCTION get_tp1_withdrawal_conf
12525   (p_assignment_id     IN      NUMBER
12526   ) RETURN VARCHAR2
12527 IS
12528 
12529   l_withdrawal_conf ben_ext_rslt_dtl.val_15%TYPE := ' ';
12530   l_proc_name  varchar2(60) := g_proc_name || 'get_tp1_withdrawal_conf';
12531 
12532 BEGIN
12533 
12534   debug_enter(l_proc_name);
12535   debug('g_asg_count :'||to_char(g_asg_count), 10);
12536 
12537   IF g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL
12538      AND
12539      (LEAST(g_ext_asg_details(p_assignment_id).leaver_date
12540            ,g_effective_run_date
12541            )
12542         =
12543         g_ext_asg_details(p_assignment_id).leaver_date
12544      ) THEN
12545 
12546     l_withdrawal_conf := 'W';
12547 
12548   END IF;
12549 
12550   debug_exit(l_proc_name);
12551   RETURN l_withdrawal_conf;
12552 
12553 END; -- get_tp1_withdrawal_conf
12554 
12555 -- ----------------------------------------------------------------------------
12556 -- |------------------------< get_tp1_days_excluded >-------------------------|
12557 -- ----------------------------------------------------------------------------
12558 function get_tp1_days_excluded (p_assignment_id in     number
12559                                ,p_days_excluded out    nocopy varchar2
12560                                )
12561   return number is
12562 --
12563   l_proc_name            varchar2(60) := g_proc_name || 'get_tp1_days_excluded';
12564   l_days_excluded        number;
12565   l_return               number;
12566 --
12567 begin
12568   --
12569   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
12570   --
12571   debug_enter(l_proc_name);
12572   debug(l_proc_name||' p_assignment_id:'||p_assignment_id, 15);
12573 
12574   -- Bugfix 3803760:FTSUPPLY
12575   -- Changed start_date to teacher_start_date
12576   l_return := get_days_excluded_date
12577                 (p_assignment_id        => p_assignment_id
12578                 ,p_effective_start_date => greatest
12579                                              (g_pension_year_start_date
12580                                              ,g_ext_asg_details(p_assignment_id).teacher_start_date
12581                                              )
12582                 ,p_effective_end_date   => least
12583                                              (g_effective_run_date
12584                                              ,nvl(g_ext_asg_details(p_assignment_id).leaver_date,
12585                                                     g_effective_run_date)
12586                                              )
12587                 ,p_days                 => l_days_excluded
12588                 );
12589 
12590   --
12591   -- hr_utility.set_location('Leaving: '||l_proc_name, 20);
12592   --
12593 -- bug 6275363---------
12594   IF l_return = -2 then
12595    p_days_excluded := '+00';
12596    debug_exit(l_proc_name);
12597    RETURN 0;
12598   END if;
12599 -------------------------
12600   debug_exit(l_proc_name);
12601 
12602   if l_return <> -1 then
12603 
12604      p_days_excluded := trim(to_char(l_days_excluded,'099'));
12605      return 0;
12606 
12607   else
12608 
12609     p_days_excluded := '000';
12610     return -1;
12611 
12612   end if; -- end of of return check ...
12613 
12614 EXCEPTION
12615   WHEN OTHERS THEN
12616     debug_exit(' Others in '||l_proc_name
12617               ,'Y' -- turn trace off
12618               );
12619     p_days_excluded := NULL;
12620     RAISE;
12621 end get_tp1_days_excluded;
12622 --
12623 -- ----------------------------------------------------------------------------
12624 -- |------------------------< get_tp1_annual_ft_sal_rate >--------------------|
12625 -- ----------------------------------------------------------------------------
12626 function get_tp1_annual_ft_sal_rate (p_assignment_id in     number
12627                                     ,p_annual_rate   out    nocopy varchar2
12628                                     )
12629   return number is
12630 --
12631   l_proc_name            varchar2(60) := g_proc_name || 'get_tp1_annual_ft_sal_rate';
12632   l_return               number;
12633   l_annual_rate          number;
12634 --
12635 begin
12636   --
12637   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
12638   --
12639   debug_enter(l_proc_name);
12640 
12641   -- Bugfix 3803760:FTSUPPLY
12642   -- Changed start_date to teacher_start_date
12643   l_return := get_annual_sal_rate_date
12644                 (p_assignment_id        => p_assignment_id
12645                 ,p_effective_start_date => greatest
12646                                              (g_pension_year_start_date
12647                                              ,g_ext_asg_details(p_assignment_id).teacher_start_date
12648                                              )
12649                 ,p_effective_end_date   => least
12650                                              (g_effective_run_date
12651                                              ,nvl(g_ext_asg_details(p_assignment_id).leaver_date,
12652                                                     g_effective_run_date)
12653                                              )
12654                 ,p_rate                 => l_annual_rate
12655                 );
12656 
12657   --
12658   -- hr_utility.set_location('Leaving: '||l_proc_name, 20);
12659   --
12660   debug_exit(l_proc_name);
12661 
12662   if l_return <> -1 then
12663 
12664      g_annual_rate(p_assignment_id) := l_annual_rate;
12665      p_annual_rate                  := trim(to_char(l_annual_rate,'099999'));
12666      return 0;
12667 
12668   else
12669 
12670     p_annual_rate := '000000';
12671     return -1;
12672 
12673   end if; -- end if of l_return check ...
12674 
12675 EXCEPTION
12676   WHEN OTHERS THEN
12677     debug_exit(' Others in '||l_proc_name
12678               ,'Y' -- turn trace off
12679               );
12680     p_annual_rate := NULL;
12681     RAISE;
12682 end get_tp1_annual_ft_sal_rate;
12683 
12684 --
12685 -- ----------------------------------------------------------------------------
12686 -- |------------------------< get_tp1_pt_sal_paid >---------------------------|
12687 -- ----------------------------------------------------------------------------
12688 function get_tp1_pt_sal_paid (p_assignment_id in     number
12689                              ,p_part_time_sal out    nocopy varchar2
12690                              )
12691   return number is
12692 --
12693   l_proc_name            varchar2(60) := g_proc_name || 'get_tp1_pt_sal_paid';
12694   l_part_time_sal        number;
12695   l_return               number;
12696 --
12697 begin
12698   --
12699   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
12700   --
12701   debug_enter(l_proc_name);
12702   debug('p_assignment_id '||p_assignment_id,10) ;
12703 
12704   -- Get part time salary paid only if the employment category is part-time
12705 
12706   if g_ext_asg_details(p_assignment_id).ext_emp_cat_cd = 'P' then
12707 
12708     debug(l_proc_name,20);
12709 
12710     -- Bugfix 3803760:FTSUPPLY
12711     -- Changed start_date to teacher_start_date
12712     l_part_time_sal := get_part_time_sal_date
12713                          (p_assignment_id        => p_assignment_id
12714                          ,p_effective_start_date => greatest
12715                                                       (g_pension_year_start_date
12716                                                       ,g_ext_asg_details(p_assignment_id).teacher_start_date
12717                                                       )
12718                          ,p_effective_end_date   => least
12719                                                       (g_effective_run_date
12720                                                       ,nvl(g_ext_asg_details(p_assignment_id).leaver_date,
12721                                                              g_effective_run_date)
12722                                                       )
12723                          );
12724     debug ('l_part_time_sal '||to_char(l_part_time_sal),30);
12725 
12726     -- Check whether part time sal exceeds annual salary rate
12727 
12728     if g_annual_rate.exists(p_assignment_id) and
12729        g_annual_rate(p_assignment_id) < l_part_time_sal
12730     then
12731 
12732        -- Fill in with zeros instead of space
12733        -- Bug fix 2353106
12734        debug ('g_annual_rate(p_assignment_id) '||to_char(g_annual_rate(p_assignment_id)),40);
12735 
12736        debug_exit(l_proc_name);
12737 
12738           IF sign(l_part_time_sal) = -1 THEN
12739              p_part_time_sal := '-'|| lpad(abs(l_part_time_sal),5,'0');
12740           ELSE
12741              p_part_time_sal := lpad(l_part_time_sal,6,'0');
12742           END IF;
12743 
12744      --  p_part_time_sal := lpad(l_part_time_sal,6,'0');
12745        return -1;
12746 
12747     else
12748 
12749        -- Fill in with zeros instead of space
12750        -- Bug fix 2353106
12751        debug_exit(l_proc_name);
12752 
12753 	  IF sign(l_part_time_sal) = -1 THEN   -- added for bug 7313510
12754              p_part_time_sal := '-'|| lpad(abs(l_part_time_sal),5,'0');
12755           ELSE
12756              p_part_time_sal := lpad(l_part_time_sal,6,'0');
12757           END IF;
12758 
12759       -- p_part_time_sal := lpad(l_part_time_sal,6,'0');
12760        return 0;
12761 
12762     end if; -- end if of annual rate check ...
12763 
12764   else -- emp cat cd is not part time
12765 
12766        -- Fill in with zeros instead of space
12767        -- Bug fix 2353106
12768     debug(l_proc_name, 50) ;
12769     p_part_time_sal := '000000';
12770 
12771     debug_exit(l_proc_name);
12772     return 0;
12773 
12774   end if; -- end if of emp cat check ...
12775 
12776 EXCEPTION
12777   WHEN OTHERS THEN
12778     debug_exit(' Others in '||l_proc_name
12779               ,'Y' -- turn trace off
12780               );
12781     p_part_time_sal := NULL;
12782     RAISE;
12783 END get_tp1_pt_sal_paid;
12784 
12785 --
12786 -- Career Indicator
12787 --
12788   FUNCTION get_tp1_career_indicator
12789     (p_assignment_id     IN      NUMBER
12790     ) RETURN VARCHAR2
12791   IS
12792     l_return_value  char(7);
12793     l_proc_name          VARCHAR2(61):=
12794        g_proc_name||'get_tp1_career_indicator';
12795     l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
12796     l_report_asg          VARCHAR2(1);
12797 
12798   BEGIN -- get_tp1_career_indicator
12799 
12800       debug_enter(l_proc_name);
12801 
12802       -- Check if primary assignment is a teaching assignment
12803       l_assignment_id := chk_report_assignment
12804                            (p_assignment_id     => p_assignment_id
12805                            ,p_report_assignment => l_report_asg
12806                            );
12807 
12808       l_return_value := Get_Grade_Fasttrack_Info
12809                         (p_assignment_id  => l_assignment_id
12810                         ,p_effective_date => GREATEST(g_pension_year_start_date
12811                                                      ,g_ext_asg_details(l_assignment_id).start_date
12812                                                      )
12813                         );
12814 
12815       debug_exit(l_proc_name);
12816       RETURN l_return_value;
12817 
12818   EXCEPTION
12819     WHEN OTHERS THEN
12820       debug_exit(' Others in '||l_proc_name
12821                 ,'Y' -- turn trace off
12822                 );
12823       RAISE;
12824   END; -- get_tp1_career_indicator
12825 --
12826 -- London Allowance
12827 --
12828   FUNCTION get_tp1_london_allowance
12829     (p_assignment_id     IN      NUMBER
12830     ) RETURN VARCHAR2
12831   IS
12832     l_return_value char(7);
12833     l_proc_name          VARCHAR2(61):=
12834        g_proc_name||'get_tp1_london_allowance';
12835     l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
12836     l_report_asg          VARCHAR2(1);
12837 
12838   BEGIN -- get_tp1_london_allowance
12839 
12840       debug_enter(l_proc_name);
12841 
12842       -- Check if primary assignment is a teaching assignment
12843       l_assignment_id := chk_report_assignment
12844                            (p_assignment_id     => p_assignment_id
12845                            ,p_report_assignment => l_report_asg
12846                            );
12847 -- changed for 5743209
12848 /*
12849       l_return_value := Get_Allowance_Code
12850                         (p_assignment_id  => l_assignment_id
12851                         ,p_effective_date => GREATEST(g_pension_year_start_date
12852                                                      ,g_ext_asg_details(l_assignment_id).start_date
12853                                                      )
12854                         ,p_allowance_type => 'LONDON_ALLOWANCE_RULE'
12855                         );
12856 */
12857       l_return_value := Get_Allowance_Code_New
12858                         (p_assignment_id  => l_assignment_id
12859                         ,p_effective_date => GREATEST(g_pension_year_start_date
12860                                                      ,g_ext_asg_details(l_assignment_id).start_date
12861                                                      )
12862                         ,p_allowance_type => 'LONDON_ALLOWANCE_RULE'
12863                         );
12864 
12865       debug_exit(l_proc_name);
12866       RETURN l_return_value;
12867 
12868   EXCEPTION
12869     WHEN OTHERS THEN
12870       debug_exit(' Others in '||l_proc_name
12871                 ,'Y' -- turn trace off
12872                 );
12873       RAISE;
12874   END; -- get_tp1_london_allowance
12875 
12876 --
12877 -- Special Priority Allowance
12878 --
12879   FUNCTION get_tp1_sp_allowance
12880     (p_assignment_id     IN      NUMBER
12881     ) RETURN VARCHAR2
12882   IS
12883     l_return_value char(7);
12884     l_proc_name          VARCHAR2(61):=
12885        g_proc_name||'get_tp1_sp_allowance';
12886     l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
12887     l_report_asg          VARCHAR2(1);
12888 
12889   BEGIN -- get_tp1_sp_allowance
12890 
12891       debug_enter(l_proc_name);
12892 
12893       -- Check if primary assignment is a teaching assignment
12894       l_assignment_id := chk_report_assignment
12895                            (p_assignment_id     => p_assignment_id
12896                            ,p_report_assignment => l_report_asg
12897                            );
12898 -- changed for 5743209
12899 /*
12900       l_return_value := Get_Allowance_Code
12901                         (p_assignment_id  => l_assignment_id
12902                         ,p_effective_date => GREATEST(g_pension_year_start_date
12903                                                      ,g_ext_asg_details(l_assignment_id).start_date
12904                                                      )
12905                         ,p_allowance_type => 'SPECIAL_ALLOWANCE_RULE'
12906                         );
12907 */
12908  l_return_value := Get_Allowance_Code_New
12909                         (p_assignment_id  => l_assignment_id
12910                         ,p_effective_date => GREATEST(g_pension_year_start_date
12911                                                      ,g_ext_asg_details(l_assignment_id).start_date
12912                                                      )
12913                         ,p_allowance_type => 'SPECIAL_ALLOWANCE_RULE'
12914                         );
12915 
12916       debug_exit(l_proc_name);
12917       RETURN l_return_value;
12918   EXCEPTION
12919     WHEN OTHERS THEN
12920       debug_exit(' Others in '||l_proc_name
12921                 ,'Y' -- turn trace off
12922                 );
12923       RAISE;
12924   END; -- get_tp1_sp_allowance
12925 
12926 --
12927 -- Special Class Addition (Part-time indicator)
12928 --
12929   FUNCTION get_tp1_pt_contract_indicator
12930     (p_assignment_id     IN      NUMBER
12931     ) RETURN VARCHAR2
12932   IS
12933     l_return_value char(7);
12934     l_proc_name          VARCHAR2(61):=
12935        g_proc_name||'get_tp1_pt_contract_indicator';
12936     l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
12937     l_report_asg          VARCHAR2(1);
12938 
12939   BEGIN -- get_tp1_pt_contract_indicator
12940 
12941       debug_enter(l_proc_name);
12942 
12943       -- Check if primary assignment is a teaching assignment
12944       l_assignment_id := chk_report_assignment
12945                            (p_assignment_id     => p_assignment_id
12946                            ,p_report_assignment => l_report_asg
12947                            );
12948 
12949       l_return_value := Get_Special_ClassRule
12950                         ( p_assignment_id  => l_assignment_id
12951                         ,p_effective_date => GREATEST(g_pension_year_start_date
12952                                                      ,g_ext_asg_details(l_assignment_id).start_date
12953                                                      )
12954                         );
12955 
12956       debug_exit(l_proc_name);
12957       RETURN l_return_value;
12958   EXCEPTION
12959     WHEN OTHERS THEN
12960       debug_exit(' Others in '||l_proc_name
12961                 ,'Y' -- turn trace off
12962                 );
12963       RAISE;
12964   END; -- get_tp1_pt_contract_indicator
12965 
12966 --
12967 -- ----------------------------------------------------------------------------
12968 -- |------------------------< get_tp1_other_allowances >----------------------|
12969 -- ----------------------------------------------------------------------------
12970 function get_tp1_other_allowances (p_assignment_id in    number)
12971   return varchar2 is
12972 --
12973   l_proc_name       varchar2(61) := g_proc_name || 'get_tp1_other_allowances';
12974 
12975   -- Fill in with zeros instead of space
12976   -- Bug fix 2353106
12977 
12978   l_other_allowance varchar2(5) := '00000';
12979   l_error           number;
12980 --
12981 begin
12982   --
12983   -- hr_utility.set_location('Entering: '||l_proc_name, 10);
12984   --
12985   debug_enter(l_proc_name);
12986 
12987   if g_other_allowance.exists(p_assignment_id) then
12988 
12989      -- 4336613 : SAL_VALIDAT_3A : Check whether Other_All value has exceeeded 4 digit limit
12990      -- If yes, raise warning.
12991      debug('Other Allowance : '||to_char(g_other_allowance(p_assignment_id)),15);
12992 
12993      if g_other_allowance(p_assignment_id) > 99999 then
12994 
12995         l_error := pqp_gb_tp_extract_functions.raise_extract_warning
12996                      (p_assignment_id => p_assignment_id
12997                      ,p_error_text    => 'BEN_93043_EXT_TP1_OTH_VAL_EXC'
12998                      ,p_error_number  => 93043
12999                      ,p_token1        => TO_CHAR(g_other_allowance(p_assignment_id)) -- bug : 4336613
13000                      );
13001 
13002         g_other_allowance(p_assignment_id) := 99999;  -- 4336613 : SAL_VALIDAT_3A :
13003                                                      -- set to 9999 if > 9999
13004 
13005      end if; -- end if of other allowance max limit check ...
13006 
13007      -- Fill in with zeros instead of space
13008      -- Bug fix 2353106
13009 
13010      debug('Other Allowance : '||to_char(g_other_allowance(p_assignment_id)),20);
13011 
13012      l_other_allowance := lpad(g_other_allowance(p_assignment_id),5,'0');
13013 
13014   end if; -- end if of other allowance exists check...
13015 
13016 
13017   --
13018   -- hr_utility.set_location('Leaving: '||l_proc_name, 20);
13019   --
13020   debug_exit(l_proc_name);
13021 
13022   return l_other_allowance;
13023 EXCEPTION
13024     WHEN OTHERS THEN
13025       debug_exit(' Others in '||l_proc_name
13026                 ,'Y' -- turn trace off
13027                 );
13028       RAISE;
13029 end get_tp1_other_allowances;
13030 
13031 --
13032 -- Record Serial Number
13033 --
13034 FUNCTION get_tp1_record_serial_number
13035   (p_assignment_id     IN      NUMBER
13036   ) RETURN VARCHAR2
13037 IS
13038 
13039 BEGIN
13040 
13041     RETURN '1';
13042 END; -- get_tp1_record_serial_number
13043 
13044 --
13045 -- store_emp_cat_changes - finds and stores events due to emp cat change
13046 --
13047 PROCEDURE store_emp_cat_changes(p_assignment_id         IN NUMBER
13048                                ,p_start_date            IN DATE
13049                                ,p_end_date              IN DATE
13050                                )
13051 IS
13052 
13053   -- Variable Declaration
13054   l_no_of_events        NUMBER(5);
13055   l_itr                 NUMBER(5);
13056   l_new_event_itr       NUMBER(5);
13057 
13058   l_event_group         pay_event_groups.event_group_name%TYPE := 'PQP_GB_TP_ASG_EMP_CAT';
13059 
13060   -- Rowtype Variable Declaration
13061   l_proration_dates     pay_interpreter_pkg.t_proration_dates_table_type;
13062   l_proration_changes   pay_interpreter_pkg.t_proration_type_table_type;
13063 
13064   l_asg_details         csr_asg_details_dn%ROWTYPE;
13065   l_prev_asg_details    csr_asg_details_dn%ROWTYPE;
13066 
13067   l_proc_name          VARCHAR2(61):=
13068      g_proc_name||'store_emp_cat_changes';
13069 
13070   i                    NUMBER;
13071 
13072 BEGIN
13073 
13074   debug_enter(l_proc_name);
13075 
13076   -- a) Get events for Employment Category changes
13077   l_no_of_events := get_events(p_event_group            => l_event_group
13078                               ,p_assignment_id          => p_assignment_id
13079                               ,p_start_date             => p_start_date
13080                               ,p_end_date               => p_end_date
13081                               ,t_proration_dates        => l_proration_dates
13082                               ,t_proration_changes      => l_proration_changes
13083                               );
13084 
13085   debug('Number of '||l_event_group||' Events: '||
13086      fnd_number.number_to_canonical(l_no_of_events), 10);
13087 
13088   -- b) Loop through the events and check if any have changed.
13089   l_itr := l_proration_dates.FIRST;
13090   WHILE l_itr <= l_proration_dates.LAST
13091   LOOP -- through change proration dates
13092 
13093     debug('Date :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 20);
13094     debug('Change :'||l_proration_changes(l_itr), 30);
13095     IF l_itr = l_proration_dates.FIRST
13096        OR
13097        ( l_proration_dates(l_itr) <>
13098          l_proration_dates(l_proration_dates.PRIOR(l_itr))
13099        ) THEN
13100 
13101       OPEN csr_asg_details_dn
13102                   (p_assignment_id
13103                   ,l_proration_dates(l_itr)
13104                   );
13105       FETCH csr_asg_details_dn INTO l_asg_details;
13106 
13107 
13108       IF csr_asg_details_dn%FOUND THEN
13109 
13110         debug(l_proc_name, 40);
13111         -- Bugfix 3073562:GAP1:GAP2
13112         -- Replacing the type4 func call with the type 1 function
13113         l_asg_details.ext_emp_cat_cd :=
13114                 get_translate_asg_emp_cat_code
13115                         (l_asg_details.asg_emp_cat_cd
13116                         ,l_asg_details.start_date
13117                         ,'Pension Extracts Employment Category Code'
13118                         ,l_asg_details.business_group_id
13119                         );
13120 
13121         -- Get the previous assignment record
13122         FETCH csr_asg_details_dn INTO l_prev_asg_details;
13123         -- Bugfix 3073562:GAP1:GAP2
13124         -- Replacing the type4 func call with the type 1 function
13125         l_prev_asg_details.ext_emp_cat_cd :=
13126                 get_translate_asg_emp_cat_code
13127                         (l_prev_asg_details.asg_emp_cat_cd
13128                         ,l_prev_asg_details.start_date
13129                         ,'Pension Extracts Employment Category Code'
13130                         ,l_prev_asg_details.business_group_id
13131                         );
13132 
13133         debug('Event worth considering', 50);
13134 
13135         --  Check if the employment category change is a valid one to create a new line.
13136         IF l_asg_details.ext_emp_cat_cd <> l_prev_asg_details.ext_emp_cat_cd THEN
13137 
13138           -- c) Found a change, log in global events collection
13139           l_new_event_itr := g_asg_events.COUNT+1;
13140 
13141           g_asg_events(l_new_event_itr).event_date      := l_proration_dates(l_itr);
13142           g_asg_events(l_new_event_itr).event_type      := l_event_group;
13143           g_asg_events(l_new_event_itr).assignment_id   := p_assignment_id;
13144 
13145           -- Store the new emp cat value
13146           g_asg_events(l_new_event_itr).new_ext_emp_cat_cd := l_asg_details.ext_emp_cat_cd;
13147 
13148           -- Bugfix 3734942
13149           --  If the assignment has become part time then
13150           --   we need to increment the g_part_time_asg_count
13151           --   when this event is processed. If asg has bcom
13152           --   full time then we decrement
13153           IF l_asg_details.ext_emp_cat_cd = 'P' THEN
13154             debug('PT asg count needs incrementing when event is processed', 55);
13155             g_asg_events(l_new_event_itr).pt_asg_count_change := 1;
13156           ELSE
13157             g_asg_events(l_new_event_itr).pt_asg_count_change := -1;
13158           END IF;
13159           -- Also store the new location id coz we need it
13160           --  in new LOS code to get the new estb number
13161           --  if change in emp cat has resulted in the
13162           --  pt asg count going below 2
13163           g_asg_events(l_new_event_itr).new_location_id := l_asg_details.location_id;
13164 
13165           debug('Assignment has a had an employment category change', 60);
13166 
13167         END IF; -- Check if the locaiton change is a valid one to report as leaver
13168         --
13169       END IF; -- csr_asg_details_dn%FOUND THEN
13170       --
13171       IF csr_asg_details_dn%ISOPEN THEN
13172         CLOSE csr_asg_details_dn;
13173       END IF;
13174       --
13175     END IF; -- if this date <> last date to eliminate duplicates
13176           --
13177     l_itr := l_proration_dates.NEXT(l_itr);
13178     --
13179   END LOOP; -- through change proration dates
13180 
13181   debug_exit(l_proc_name);
13182 
13183   RETURN;
13184 
13185 EXCEPTION
13186   WHEN OTHERS THEN
13187     debug_exit(' Others in '||l_proc_name);
13188     RAISE;
13189 END; -- store_emp_cat_changes
13190 
13191 --
13192 -- store_event_grp_changes - finds and stores events due to changes
13193 -- to any given entity identified through the event group name
13194 --
13195 PROCEDURE store_event_grp_changes
13196                         (p_assignment_id        IN NUMBER
13197                         ,p_event_group          IN pay_event_groups.event_group_name%TYPE
13198                         ,p_start_date           IN DATE
13199                         ,p_end_date             IN DATE
13200                         )
13201 IS
13202 
13203   -- Variable Declaration
13204   l_no_of_events        NUMBER(5);
13205   l_itr                 NUMBER(5);
13206   l_new_event_itr       NUMBER(5);
13207 
13208   -- Rowtype Variable Declaration
13209   l_proration_dates     pay_interpreter_pkg.t_proration_dates_table_type;
13210   l_proration_changes   pay_interpreter_pkg.t_proration_type_table_type;
13211 
13212   l_proc_name          VARCHAR2(61):=
13213      g_proc_name||'store_event_grp_changes';
13214 
13215 BEGIN
13216 
13217   debug_enter(l_proc_name);
13218 
13219   -- a) Get events for changes for given event group
13220   l_no_of_events := get_events(p_event_group            => p_event_group
13221                               ,p_assignment_id          => p_assignment_id
13222                               ,p_start_date             => p_start_date
13223                               ,p_end_date               => p_end_date
13224                               ,t_proration_dates        => l_proration_dates
13225                               ,t_proration_changes      => l_proration_changes
13226                               );
13227 
13228   debug('Number of '||p_event_group||' Events: '||
13229      fnd_number.number_to_canonical(l_no_of_events), 10);
13230 
13231   -- b) Loop through the events and check if any have changed.
13232   l_itr := l_proration_dates.FIRST;
13233   WHILE l_itr <= l_proration_dates.LAST
13234   LOOP -- through change proration dates
13235 
13236     debug('Date :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 20);
13237     debug('Change :'||l_proration_changes(l_itr), 30);
13238 
13239     IF l_itr = l_proration_dates.FIRST
13240        OR
13241        ( l_proration_dates(l_itr) <>
13242          l_proration_dates(l_proration_dates.PRIOR(l_itr))
13243        ) THEN
13244 
13245         -- c) Found a change, log in global events collection
13246         l_new_event_itr := g_asg_events.COUNT+1;
13247 
13248         g_asg_events(l_new_event_itr).event_date        := l_proration_dates(l_itr);
13249         g_asg_events(l_new_event_itr).event_type        := p_event_group;
13250         g_asg_events(l_new_event_itr).assignment_id     := p_assignment_id;
13251 
13252         debug('Assignment attributes have had changes',110);
13253 
13254     END IF; -- if this date <> last date to eliminate duplicates
13255           --
13256     l_itr := l_proration_dates.NEXT(l_itr);
13257     --
13258   END LOOP; -- through change proration dates
13259 
13260   debug_exit(l_proc_name);
13261 
13262   RETURN;
13263 
13264 EXCEPTION
13265   WHEN OTHERS THEN
13266     debug_exit(' Others in '||l_proc_name);
13267     RAISE;
13268 END; -- store_event_grp_changes
13269 
13270 --
13271 -- store_element_changes
13272 --   finds and stores events due to change to a pensionable element
13273 --
13274 -- WARNING : This procedure marks and unmarks the events in PPE for itself.
13275 --           Please donot mark events before calling this proc.
13276 --
13277 PROCEDURE store_element_changes(p_assignment_id IN NUMBER
13278                                ,p_start_date    IN DATE
13279                                ,p_end_date      IN DATE
13280                                )
13281 IS
13282 
13283   -- TYPE declaration
13284   TYPE t_rate_types IS TABLE of fnd_lookups.meaning%TYPE
13285   INDEX BY BINARY_INTEGER;
13286 
13287   -- Variable Declaration
13288   l_no_of_events        NUMBER(5);
13289   l_itr                 NUMBER(5);
13290   l_rates_itr           NUMBER(5);
13291   l_new_event_itr       NUMBER(5);
13292 
13293   l_event_group         pay_event_groups.event_group_name%TYPE;
13294 
13295   -- Rowtype Variable Declaration
13296   l_proration_dates     pay_interpreter_pkg.t_proration_dates_table_type;
13297   l_proration_changes   pay_interpreter_pkg.t_proration_type_table_type;
13298 
13299   l_rate_types          t_rate_types;
13300   l_element_set         csr_element_set%ROWTYPE;
13301   l_element_entries     csr_element_entries%ROWTYPE;
13302 
13303   l_proc_name          VARCHAR2(61):=
13304      g_proc_name||'store_element_changes';
13305 
13306 BEGIN
13307 
13308   debug_enter(l_proc_name);
13309 
13310   -- Populate the collection for rate types
13311   l_rate_types(l_rate_types.COUNT+1) := g_sal_rate_type;
13312   -- Bug fix 2786740
13313   -- London Rate type and Other rate type need not have
13314   -- a value always
13315   IF g_lon_rate_type IS NOT NULL THEN
13316      l_rate_types(l_rate_types.COUNT+1) := g_lon_rate_type;
13317   END IF; -- End if of g_lon_rate_type is not null check ...
13318 
13319   IF g_oth_rate_type IS NOT NULL THEN
13320      l_rate_types(l_rate_types.COUNT+1) := g_oth_rate_type;
13321   END IF; -- End if of g_oth_rate_type is not null check ...
13322 
13323   FOR l_rates_itr IN l_rate_types.FIRST..l_rate_types.LAST
13324   LOOP -- through the rate types
13325     --
13326     debug('Rate Type :'||l_rate_types(l_rates_itr), 10);
13327 
13328     FOR l_element_set IN csr_element_set
13329                                 (c_name => l_rate_types(l_rates_itr)
13330                                 ,c_eff_date => g_ext_asg_details(p_assignment_id).start_date
13331                                 )
13332     LOOP -- Through the elements in this rate type
13333       --
13334       debug(l_proc_name, 20);
13335       FOR l_element_entries IN csr_element_entries(p_assignment_id      => p_assignment_id
13336                                                   ,p_effective_date
13337                                                      => g_ext_asg_details(p_assignment_id).start_date
13338                                                   ,p_element_type_id    => l_element_set.element_type_id
13339                                                   )
13340       LOOP -- Through element entries for this element type
13341 
13342         -- Find Element Entry Changes
13343         l_event_group := 'PQP_GB_TP_ELEMENT_ENTRY';
13344         -- a) Get events for Element Entry changes
13345         -- Bug 3015917 : Removed set_pay_process.. and modified get_events which uses
13346         --               the new style DTI
13347         l_no_of_events := get_events(p_event_group              => l_event_group
13348                                     ,p_assignment_id            => p_assignment_id
13349                                     ,p_element_entry_id         => l_element_entries.element_entry_id
13350                                     ,p_start_date               => p_start_date
13351                                     ,p_end_date                 => p_end_date
13352                                     ,t_proration_dates          => l_proration_dates
13353                                     ,t_proration_changes        => l_proration_changes
13354                                     );
13355 
13356         debug('Number of '||l_event_group||' Events for Element Entry Id'||
13357                 l_element_entries.element_entry_id||' : '||
13358                 fnd_number.number_to_canonical(l_no_of_events), 30);
13359 
13360         -- b) Loop through the events and check if any have changed.
13361         l_itr := l_proration_dates.FIRST;
13362         WHILE l_itr <= l_proration_dates.LAST
13363         LOOP -- through change proration dates
13364 
13365           debug('Date :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 40);
13366           debug('Change :'||l_proration_changes(l_itr), 50);
13367 
13368           IF l_itr = l_proration_dates.FIRST
13369              OR
13370              ( l_proration_dates(l_itr) <>
13371                l_proration_dates(l_proration_dates.PRIOR(l_itr))
13372              ) THEN
13373 
13374             -- c) Found a change, log in global events collection
13375 
13376             -- Bugfix 2882220 : Added the following if logic
13377             -- Only log this event if
13378             --   1) Its not an End Date Event
13379             --   2) Its an End DAte event but not on the g_pension_year_end_date
13380             IF l_proration_changes(l_itr) <> 'E' THEN
13381 
13382               l_new_event_itr := g_asg_events.COUNT+1;
13383 
13384               g_asg_events(l_new_event_itr).event_date    := l_proration_dates(l_itr);
13385               g_asg_events(l_new_event_itr).event_type    := l_event_group;
13386               g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
13387 
13388               debug('Event Date :'||to_char(l_proration_dates(l_itr), 'DD/MM/YYYY'), 50);
13389               debug('Change :'||l_proration_changes(l_itr), 60);
13390               debug('Element Entry change has happened, UPDATE',70);
13391 
13392             ELSIF l_proration_changes(l_itr) = 'E' -- End Dated Element Entry
13393                   AND
13394                   trunc(l_proration_dates(l_itr)) < trunc(g_pension_year_end_date) THEN
13395 
13396               l_new_event_itr := g_asg_events.COUNT+1;
13397 
13398               g_asg_events(l_new_event_itr).event_date    := l_proration_dates(l_itr) + 1;
13399               g_asg_events(l_new_event_itr).event_type    := l_event_group;
13400               g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
13401 
13402               debug('Element Entry change has happened, END DATE', 80);
13403 
13404             END IF; -- l_proration_changes(l_itr) <> 'E' THEN
13405             --
13406           END IF; -- if this date <> last date to eliminate duplicates
13407           --
13408           l_itr := l_proration_dates.NEXT(l_itr);
13409           --
13410         END LOOP; -- through change proration dates
13411         --
13412 
13413         l_proration_dates.DELETE;
13414         l_proration_changes.DELETE;
13415 
13416         -- Find Entry Value Changes for this element entry
13417         l_event_group := 'PQP_GB_TP_ENTRY_VALUE';
13418         -- a) Get events for Entry Value changes
13419         l_no_of_events := get_events(p_event_group              => l_event_group
13420                                     ,p_assignment_id            => p_assignment_id
13421                                     ,p_element_entry_id         => l_element_entries.element_entry_id
13422                                     ,p_start_date               => p_start_date
13423                                     ,p_end_date                 => p_end_date
13424                                     ,t_proration_dates          => l_proration_dates
13425                                     ,t_proration_changes        => l_proration_changes
13426                                     );
13427 
13428         debug('Number of '||l_event_group||' Events : '||
13429                 fnd_number.number_to_canonical(l_no_of_events), 90);
13430 
13431         -- b) Loop through the events and check if any have changed.
13432         l_itr := l_proration_dates.FIRST;
13433         WHILE l_itr <= l_proration_dates.LAST
13434         LOOP -- through change proration dates
13435 
13436           debug('Date :'||to_char(l_proration_dates(l_itr),'DD/MM/YYYY'), 100);
13437           debug('Change :'||l_proration_changes(l_itr), 110);
13438 
13439           IF l_itr = l_proration_dates.FIRST
13440              OR
13441              ( l_proration_dates(l_itr) <>
13442                l_proration_dates(l_proration_dates.PRIOR(l_itr))
13443              ) THEN
13444 
13445             -- c) Found a change, log in global events collection
13446             l_new_event_itr := g_asg_events.COUNT+1;
13447 
13448             g_asg_events(l_new_event_itr).event_date    := l_proration_dates(l_itr);
13449             g_asg_events(l_new_event_itr).event_type    := l_event_group;
13450             g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
13451 
13452             debug('Element Entry change has happened', 120);
13453 
13454           END IF; -- if this date <> last date to eliminate duplicates
13455                 --
13456           l_itr := l_proration_dates.NEXT(l_itr);
13457           --
13458         END LOOP; -- through change proration dates
13459         --
13460 
13461         -- UnMark Pay process events for this element entry
13462         -- Bug 3015917 : Removed set_pay_process.. as we now use the new style DTI
13463 
13464       END LOOP; -- Through element entries for this element type
13465       --
13466     END LOOP; -- Through the elements in this rate type
13467     --
13468   END LOOP; -- through the rate types
13469 
13470   debug_exit(l_proc_name);
13471 
13472   RETURN;
13473 
13474 EXCEPTION
13475   WHEN OTHERS THEN
13476     debug_exit(' Others in '||l_proc_name);
13477     RAISE;
13478 END; -- store_element_changes
13479 
13480 -- Added by sshetty
13481 PROCEDURE set_pay_process_events(p_grade_id      IN  NUMBER
13482                                  ,p_status       IN  VARCHAR2
13483                                  ,p_start_date   IN  DATE
13484                                  ,p_end_date     IN  DATE
13485                                  )
13486 IS
13487 PRAGMA AUTONOMOUS_TRANSACTION;
13488 l_proc_name VARCHAR2(61):= 'set_pay_process_events_to_process';
13489 
13490 BEGIN
13491 
13492   debug_enter(l_proc_name);
13493 
13494 UPDATE pay_process_events ppe
13495      SET ppe.retroactive_status = p_status
13496         ,ppe.status             = p_status
13497    WHERE ppe.assignment_id IS NULL
13498      AND ppe.change_type = 'REPORTS'
13499      AND ppe.effective_date -- allow all events effective as of and effective p_start_date
13500              BETWEEN p_start_date AND p_end_date
13501      AND ppe.surrogate_key = p_grade_id
13502      AND EXISTS (SELECT 1
13503                    FROM pay_dated_tables pdt
13504                        ,pay_event_updates peu
13505                   WHERE pdt.table_name = 'PAY_GRADE_RULES_F'
13506                     AND peu.dated_table_id = pdt.dated_table_id
13507                     AND peu.change_type = ppe.change_type
13508                     AND peu.event_update_id = ppe.event_update_id
13509                 )
13510   ;
13511 
13512  COMMIT;
13513 
13514  debug_exit(l_proc_name);
13515 
13516 END set_pay_process_events;
13517 /**********************************************
13518 --get_grade_sp_type
13519 *******************************************/
13520 
13521 FUNCTION get_grade_sp_type (p_tab_ele_ids IN t_ele_ids_from_bal)
13522 RETURN VARCHAR2
13523 IS
13524 
13525   l_index               pay_element_types_f.element_type_id%TYPE;
13526   l_grd_type            pay_grade_rules_f.rate_type%TYPE:='N';
13527   l_proc_name           varchar2(61) := g_proc_name || 'get_grade_sp_type';
13528 
13529 CURSOR csr_chk_pay_src(p_ele_id NUMBER) IS
13530 SELECT  petf.element_type_id
13531        ,petf.eei_information2 pay_source_value
13532   FROM pay_element_type_extra_info petf
13533  WHERE petf.element_type_id =p_ele_id
13534    AND petf.eei_information_category  ='PQP_UK_ELEMENT_ATTRIBUTION';
13535 
13536 l_chk_pay_src csr_chk_pay_src%ROWTYPE;
13537 BEGIN
13538 
13539    debug_enter(l_proc_name);
13540 
13541    FOR i IN 1..p_tab_ele_ids.COUNT
13542     LOOP
13543 
13544      debug(l_proc_name, 10);
13545      IF i=1 THEN
13546       l_index:=p_tab_ele_ids.FIRST;
13547      ELSE
13548        l_index:=p_tab_ele_ids.NEXT(l_index);
13549 
13550      END IF;
13551      OPEN  csr_chk_pay_src(p_tab_ele_ids(l_index).element_type_id);
13552      LOOP
13553      debug(l_proc_name, 20);
13554      FETCH csr_chk_pay_src INTO l_chk_pay_src;
13555      EXIT WHEN csr_chk_pay_src%NOTFOUND;
13556       IF l_chk_pay_src.pay_source_value='SP'
13557          OR l_chk_pay_src.pay_source_value='G' THEN
13558           debug(l_proc_name, 30);
13559           IF l_grd_type='N' THEN
13560            l_grd_type:=l_chk_pay_src.pay_source_value;
13561           ELSIF l_grd_type<>l_chk_pay_src.pay_source_value
13562             AND l_grd_type<>'N' THEN
13563            l_grd_type:='GSP';
13564           END IF;
13565       END IF;
13566      END LOOP;
13567      CLOSE csr_chk_pay_src;
13568 
13569 
13570     END LOOP;
13571 
13572   debug('l_grd_type :'||l_grd_type, 40);
13573   debug_exit(l_proc_name);
13574 
13575   RETURN(l_grd_type);
13576 EXCEPTION
13577 --------
13578 WHEN OTHERS THEN
13579 debug_exit(' Others in '||l_proc_name);
13580 RETURN ('N');
13581 
13582 END get_grade_sp_type;
13583 
13584 /*****************************************
13585 --store_grade_sp_changes
13586 *****************************************/
13587 -- Removed this procedure as it is no longer used.
13588 
13589 --
13590 -- get_asg_events - gets all qualifying events for the given assignment
13591 --
13592 PROCEDURE get_asg_events(p_assignment_id        IN NUMBER
13593                         ,p_start_date           IN DATE
13594                         ,p_end_date             IN DATE
13595                         )
13596 IS
13597 
13598   -- Variable Declaration
13599   l_new_event_itr       NUMBER(5);
13600 
13601   -- Rowtype Variable Declaration
13602 
13603   l_proc_name          VARCHAR2(61):=
13604      g_proc_name||'get_asg_events';
13605 
13606 BEGIN
13607 
13608   debug_enter(l_proc_name);
13609   debug('Start Date :'||to_char(p_start_date,'DD/MM/YYYY'), 10);
13610   debug('End Date :'||to_char(p_end_date,'DD/MM/YYYY'), 20);
13611 
13612   -- Update the events in pay_process_events to 'P' for "in process".
13613   -- Marking all events between
13614   --  Start     : Later or (Pension year start date +1) and pension start date of the person
13615   --  End       : pension year end date.
13616   --              Might need to change this to use earlier of pension year end date
13617   --              and leaver date.
13618   -- Bug 3015917 : Removed set_pay_proc_events_to_process as we now use the
13619   --               new style DTI call
13620 
13621   -- 1) Check for Employment Category change
13622   --    FS : A change from full to part-time service (and vice versa)
13623   store_emp_cat_changes(p_assignment_id         => p_assignment_id
13624                        ,p_start_date            => p_start_date
13625                        ,p_end_date              => p_end_date
13626                        );
13627 
13628   -- 2) Check for safeguarded salary change
13629   --    FS : The comencement or cessation of a safeguarded salary
13630   store_event_grp_changes(p_assignment_id       => p_assignment_id
13631                    ,p_event_group       => 'PQP_GB_TP_SAFEGUARDED_SALARY'
13632                    ,p_start_date            => p_start_date
13633                    ,p_end_date              => p_end_date
13634                    );
13635 
13636   -- 3) Check for elected pension flag change
13637   --    FS : A change in the teacher's pensionable employment position
13638   --      This should find events only if the elected pension flag change
13639   --      is not a leaver event, coz if the elected pension flag
13640   --      change is a leaver event, then it should have been picked
13641   --      up by the chk_is_teacher_leaver function when called
13642   --      from the criteria function.
13643   store_event_grp_changes(p_assignment_id       => p_assignment_id
13644                    ,p_event_group       => 'PQP_GB_TP_ELECTED_PENSION'
13645                    ,p_start_date            => p_start_date
13646                    ,p_end_date              => p_end_date
13647                    );
13648 
13649   --
13650   -- 4) Check for fast track flag change
13651   --    FS : A change in the teacher's fast track flag
13652   store_event_grp_changes(p_assignment_id       => p_assignment_id
13653                    ,p_event_group       => 'PQP_GB_TP_FAST_TRACK'
13654                    ,p_start_date            => p_start_date
13655                    ,p_end_date              => p_end_date
13656                    );
13657 
13658   --
13659   -- 5) Check for grade change on assignment
13660   --    FS : A change in salary scale
13661   store_event_grp_changes(p_assignment_id       => p_assignment_id
13662                    ,p_event_group       => 'PQP_GB_TP_ASG_GRADE'
13663                    ,p_start_date            => p_start_date
13664                    ,p_end_date              => p_end_date
13665                    );
13666 
13667   --
13668   -- 6) Check for (grade step)/(spinal point placement) change on assignment
13669   --    FS : A change in salary scale
13670   store_event_grp_changes(p_assignment_id       => p_assignment_id
13671                    ,p_event_group       => 'PQP_GB_TP_GRADE_STEP'
13672                    ,p_start_date            => p_start_date
13673                    ,p_end_date              => p_end_date
13674                    );
13675 
13676 
13677   -- Reset the events in pay_process_events to 'U' for "Unprocessed".
13678   -- Bug 3015917 : Removed set_pay_proc_events_to_process as we now use the
13679   --               new style DTI call
13680 
13681   -- 7) Check for Element entry and entry value changes
13682   --    FS :
13683   -- Checking this after unmarking events for this assignment as
13684   -- this proc marks(and resets) events on the basis of element_entry_id
13685   -- for pensionable rate types.
13686   -- Warning : Please DONOT mark events to 'P' in pay process events
13687   -- b4 calling this proc.
13688   store_element_changes(p_assignment_id         => p_assignment_id
13689                        ,p_start_date            => p_start_date
13690                        ,p_end_date              => p_end_date
13691                        );
13692 
13693   -- 8) Check for changes to value of a grade rate or spinal point
13694   --    FS :
13695   -- Checking this after unmarking events for this assignment as
13696   -- this proc marks(and resets) events
13697   -- Warning : Please DONOT mark events to 'P' in pay process events
13698   -- b4 calling this proc.
13699   -- Added by sshetty
13700   -- Bug 3015917 : Replaced old call to store_grade_sp_changes
13701   -- with this new call. The grade rule validations are now
13702   -- done using func chk_grd_change_affects_asg which is called
13703   -- from event qualifier : GB Grade Rule Change
13704   store_event_grp_changes(p_assignment_id   => p_assignment_id
13705                    ,p_event_group           => 'PQP_GB_TP_GRADE_RULES'
13706                    ,p_start_date            => p_start_date
13707                    ,p_end_date              => p_end_date
13708                    );
13709 
13710 
13711 /*
13712    START : Commenting out nocopy code for 2340488
13713 
13714    IMP : Commenting out nocopy Step 9 as part of bugfix for 2340488 as
13715          this is now being done for primary and secondary. And we are now
13716          looking for multiple leaver and restarter dates
13717 
13718   -- 9) Check for leaver date of the secondary assignment and store the date + 1 as
13719   --    an event to create new line of service if the secondary assignment is a leaver.
13720   IF p_assignment_id <> g_primary_assignment_id THEN
13721     --
13722     IF g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL
13723        AND
13724        -- (Leaver date + 1 ) of secondary asg is before that of the primary asg
13725        ((g_ext_asg_details(p_assignment_id).leaver_date + 1)
13726         <  -- Less Than
13727         nvl(g_ext_asg_details(g_primary_assignment_id).leaver_date
13728            ,g_effective_run_date
13729            )
13730        )
13731        THEN
13732 
13733       -- Secondary asg is a leaver, store this as an event
13734       l_new_event_itr := g_asg_events.COUNT+1;
13735 
13736       g_asg_events(l_new_event_itr).event_date    := g_ext_asg_details(p_assignment_id).leaver_date + 1;
13737       g_asg_events(l_new_event_itr).event_type    := 'SECONDARY_LEAVER';
13738       g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
13739 
13740     END IF; -- g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL THEN
13741     --
13742 
13743     -- Check if the secondary leaver bcame a restarter
13744     --   But only store if the restarter date is between his leaver date
13745     --   and least of (primary asg's leaver date and g_effective_run_date)
13746     --   the "least date bit" has been taken care of when finding the events
13747     --   so the date comparison sh almost always succeed.
13748     IF g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL
13749        AND
13750        (g_ext_asg_details(p_assignment_id).restarter_date
13751         < -- Less Than
13752         nvl(g_ext_asg_details(g_primary_assignment_id).leaver_date
13753            ,g_effective_run_date
13754            )
13755        ) THEN
13756 
13757       -- Store an event for new line of service as the secondary event has now become a restarter
13758       l_new_event_itr := g_asg_events.COUNT+1;
13759 
13760       g_asg_events(l_new_event_itr).event_date    := g_ext_asg_details(p_assignment_id).restarter_date;
13761       g_asg_events(l_new_event_itr).event_type    := 'SECONDARY_RESTARTER';
13762       g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
13763 
13764 
13765     END IF; -- g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL
13766     --
13767   END IF; -- p_assignment_id <> g_primary_assignment_id THEN
13768 
13769 END : Commenting out nocopy code for 2340488
13770 */
13771 
13772   debug_exit(l_proc_name);
13773 
13774   RETURN;
13775 
13776 EXCEPTION
13777   WHEN OTHERS THEN
13778     debug_exit(' Others in '||l_proc_name);
13779     RAISE;
13780 END; -- get_asg_events
13781 
13782 --
13783 -- sort_stored_events - sorts the stored events in g_asg_events by date in ascending order
13784 --
13785 PROCEDURE sort_stored_events
13786 IS
13787 
13788   l_saved_asg_event  stored_events_type;
13789   l_asg_events       t_asg_events_type;
13790 
13791   TYPE t_skip_indexes_type IS TABLE OF BOOLEAN
13792     INDEX BY BINARY_INTEGER;
13793 
13794   l_indexes_to_skip  t_skip_indexes_type;
13795 
13796   l_current          NUMBER;
13797   l_g_current        NUMBER:= 1;
13798   l_next             NUMBER;
13799   l_proc_name        VARCHAR2(61) := g_proc_name || 'sort_stored_events';
13800 
13801 BEGIN
13802 -- bubble sort
13803 
13804   debug_enter(l_proc_name);
13805 
13806   l_asg_events := g_asg_events;
13807 
13808   g_asg_events.DELETE;
13809 
13810   l_current := l_asg_events.FIRST;
13811   WHILE l_current < l_asg_events.LAST
13812   LOOP
13813 
13814     IF NOT l_indexes_to_skip.EXISTS(l_current) THEN
13815 
13816       l_next := l_asg_events.NEXT(l_current);
13817 
13818       WHILE l_next <= l_asg_events.LAST
13819       LOOP
13820 
13821         IF NOT l_indexes_to_skip.EXISTS(l_next) THEN
13822 
13823           IF TRUNC(l_asg_events(l_next).event_date) < --next less than current
13824              TRUNC(l_asg_events(l_current).event_date)
13825           THEN
13826           -- swap
13827              -- save next
13828              l_saved_asg_event := l_asg_events(l_next);
13829              -- overwrite next with current
13830              l_asg_events(l_next) := l_asg_events(l_current);
13831              -- overwrite current from the saved next
13832              l_asg_events(l_current) := l_saved_asg_event;
13833 
13834           ELSIF TRUNC(l_asg_events(l_next).event_date) = --next equal current
13835                 TRUNC(l_asg_events(l_current).event_date) THEN
13836             -- NON generic processing.
13837 
13838             -- concatenate next event type with current
13839                l_asg_events(l_current).event_type :=
13840                  l_asg_events(l_current).event_type||','||l_asg_events(l_next).event_type;
13841 
13842             -- Bugfix 3641851:CBF3a : Now storing new_location_id and
13843             --   new_ext_emp_cat_cd if they r not NULL in the next event.
13844             --   This is needed coz there cud b more than one events
13845             --   on same date and we need the new_location_id and
13846             --   new_ext_emp_cat_cd in the New LOS code.
13847             IF l_asg_events(l_next).new_location_id IS NOT NULL THEN
13848               l_asg_events(l_current).new_location_id := l_asg_events(l_next).new_location_id;
13849             END IF;
13850             --
13851             IF l_asg_events(l_next).new_ext_emp_cat_cd IS NOT NULL THEN
13852               l_asg_events(l_current).new_ext_emp_cat_cd := l_asg_events(l_next).new_ext_emp_cat_cd;
13853             END IF;
13854 
13855             -- Bugfix 3734942
13856             -- If the next event type is one of
13857             --          a) Emp Cat change
13858             --          b) Secondary Leaver
13859             --          c) Secondary Starter or Restarter
13860             --          e) Primary Leaver
13861             --          e) Primary Starter or Restarter
13862             -- then we need to accumulate the pt_asg_count_change
13863             -- for use at time of processing these events
13864             IF (INSTR(nvl(l_asg_events(l_next).event_type,'XX')
13865                      ,'PQP_GB_TP_ASG_EMP_CAT'
13866                      ) > 0
13867                 OR
13868                 INSTR(nvl(l_asg_events(l_next).event_type,'XX')
13869                      ,'SECONDARY_LEAVER'
13870                      ) > 0
13871                 OR
13872                 INSTR(nvl(l_asg_events(l_next).event_type,'XX')
13873                      ,'SECONDARY_RESTARTER'
13874                      ) > 0
13875                 OR
13876                 INSTR(nvl(l_asg_events(l_next).event_type,'XX')
13877                      ,'SECONDARY_STARTER'
13878                      ) > 0
13879                 -- Bugfix 3880543:REHIRE : Primary leaver and restarter events
13880                 --   sh also change the asg count
13881                 OR
13882                 INSTR(nvl(l_asg_events(l_next).event_type,'XX')
13883                      ,'PRIMARY_LEAVER'
13884                      ) > 0
13885                 OR
13886                 INSTR(nvl(l_asg_events(l_next).event_type,'XX')
13887                      ,'PRIMARY_RESTARTER'
13888                      ) > 0
13889                 OR
13890                 INSTR(nvl(l_asg_events(l_next).event_type,'XX')
13891                      ,'PRIMARY_STARTER'
13892                      ) > 0
13893                ) THEN
13894 
13895               debug('Curr Value for PT Asg Cnt Change :'||
13896                         to_char(nvl(l_asg_events(l_current).pt_asg_count_change, 0)), 30);
13897 
13898               debug('Next Value for PT Asg Cnt Change :'||
13899                         to_char(nvl(l_asg_events(l_next).pt_asg_count_change, 0)), 40);
13900 
13901               -- Bugfix 3880543:REHIRE : If the event is on the same date
13902               --  and both the events hv a change in PT asg count
13903               --  then we don't want to add them up coz there cud
13904               --  be an employment category change on the restarter
13905               --  date of an assignment from FT to PT causing
13906               --  double addition to g_part_time_asg_count
13907               IF (l_asg_events(l_next).assignment_id
13908                   = --next asg equal current
13909                   l_asg_events(l_current).assignment_id
13910                  )
13911                  AND -- Next has a Pt asg count change event
13912                  (nvl(l_asg_events(l_next).pt_asg_count_change, 0)
13913                   > 0
13914                  )
13915                  AND -- Current too has a Pt asg count change event
13916                  (nvl(l_asg_events(l_current).pt_asg_count_change, 0)
13917                   > 0
13918                  )THEN
13919 
13920                 -- Do not add the next pt_asg_count_change event into current
13921                 debug('Skipping as 2 events on same date will cause Pt asg count to double, ', 50);
13922                 NULL;
13923 
13924               ELSE
13925                 l_asg_events(l_current).pt_asg_count_change :=
13926                   nvl(l_asg_events(l_current).pt_asg_count_change, 0)
13927                   +
13928                   nvl(l_asg_events(l_next).pt_asg_count_change, 0);
13929 
13930               END IF;
13931 
13932             END IF;
13933 
13934             -- Bugfix 3803760:TERMASG
13935             l_asg_events(l_current).asg_count_change :=
13936                 nvl(l_asg_events(l_current).asg_count_change, 0)
13937                 +
13938                 nvl(l_asg_events(l_next).asg_count_change, 0);
13939 
13940 
13941             -- mark this "next" index to be skipped
13942                l_indexes_to_skip(l_next) := TRUE;
13943 
13944           END IF;
13945 
13946         END IF; -- if next index is not marked to skip
13947 
13948         l_next := l_asg_events.NEXT(l_next);
13949 
13950       END LOOP;
13951 
13952       g_asg_events(l_g_current) := l_asg_events(l_current);
13953       l_g_current := l_g_current + 1;
13954 
13955     END IF; -- if current index is not marked to skip
13956 
13957     l_current := l_asg_events.NEXT(l_current);
13958 
13959   END LOOP;
13960 
13961   IF NOT l_indexes_to_skip.EXISTS(l_asg_events.LAST) THEN
13962      g_asg_events(l_g_current) := l_asg_events(l_asg_events.LAST);
13963   END IF;
13964 
13965   debug_exit(l_proc_name);
13966 -- debug only, uncomment the following code to debug
13967 /*
13968   l_current := l_indexes_to_skip.FIRST;
13969   WHILE l_current <= l_indexes_to_skip.LAST
13970   LOOP
13971     dbms_output.put_line('Skip Index: '||fnd_number.number_to_canonical(l_current));
13972     l_current := l_indexes_to_skip.NEXT(l_current);
13973   END LOOP;
13974 */
13975 
13976 EXCEPTION
13977   WHEN OTHERS THEN
13978     debug_exit(' Others in '||l_proc_name);
13979     RAISE;
13980 END sort_stored_events;
13981 
13982 -- recalc_data_elements
13983 --
13984 PROCEDURE recalc_data_elements
13985             (p_dtl_rec          IN OUT NOCOPY csr_rslt_dtl%ROWTYPE
13986             ,p_rec_type         IN VARCHAR2
13987             ,p_event_details    IN stored_events_type
13988             ,p_assignment_id    IN per_all_assignments_f.assignment_id%TYPE
13989             )
13990 IS
13991 
13992   -- Variable Declaration
13993   l_error                NUMBER;
13994   l_days_excluded        NUMBER;
13995   l_return               NUMBER;
13996   l_annual_rate          NUMBER;
13997   l_ret_allow            NUMBER;
13998   l_part_time_sal        NUMBER;
13999   l_temp_ext_emp_cat_cd  VARCHAR(1) ;
14000   -- nocopy changes
14001   l_dtl_rec_nc           csr_rslt_dtl%ROWTYPE;
14002 
14003 
14004   -- Fill in with zeros instead of space
14005   -- Bug fix 2353106
14006 
14007   l_other_allowance      VARCHAR2(5) := '00000';
14008   l_temp_date            DATE;
14009   l_temp_date_primary    DATE;
14010   l_temp_sfgrade         pqp_assignment_attributes_f.tp_safeguarded_grade%TYPE;
14011   l_new_sfgrade          pqp_assignment_attributes_f.tp_safeguarded_grade%TYPE;
14012   l_london_allowance     ben_ext_rslt_dtl.val_21%TYPE;
14013   l_sp_allowance         ben_ext_rslt_dtl.val_22%TYPE;
14014   l_contract_indicator   ben_ext_rslt_dtl.val_23%TYPE;
14015   l_rowcount             NUMBER:=0;
14016   l_pqp_asg_attributes_up csr_pqp_asg_attributes_up%ROWTYPE;
14017   l_asg_details           csr_asg_details_up%ROWTYPE;
14018   l_temp_location_id     per_all_assignments_f.location_id%TYPE;
14019 
14020   l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
14021   l_report_primary_asg  VARCHAR2(1);
14022 
14023   -- Rowtype Variable Declaration
14024 
14025   l_proc_name          VARCHAR2(61):=
14026      g_proc_name||'recalc_data_elements';
14027 
14028   -- a number;
14029 
14030 BEGIN -- recalc_data_elements
14031 
14032   debug_enter(l_proc_name);
14033 
14034   -- nocopy changes
14035   l_dtl_rec_nc := p_dtl_rec;
14036 
14037   debug('p_assignment_id: '|| to_char(p_assignment_id), 1) ;
14038 
14039   debug('p_event_details_EVENT_TYPE: '|| (p_event_details.event_type), 1.1) ;
14040 
14041   -- RETRO:BUG: 4135481
14042   -- Reset the global here to raise a warning from recalc_data_element
14043   -- This global will be set in adjust_pre_part_payments/adjust_post_part_payments
14044   -- called from calc_part_time_sal
14045   -- if there are proration and Retro event both for the same line of service
14046   -- and we are unable to find the exact payments.
14047   -- Still exploring the way to find out the exact payments in this case.
14048   -- Till then we will raise this warning.
14049 
14050   -- REMOVE this once a solution is in place for this.
14051   g_raise_retro_warning := 'N' ;
14052 
14053    -- Added by Sharath
14054       g_supp_teacher := 'N';
14055    -- End of Sharath changes
14056   -- PERF_ENHANC_3A : Performance Enhancements
14057   -- this table of records will be used in recalc_data_elements to store
14058   -- details corresponding of assignment IDs. Instead of calling parttime and FT
14059   -- salary function multiple times, this data collection will be used
14060   g_asg_recalc_details.DELETE;
14061 
14062 
14063 
14064   -- Bugfix 2551059 : Developer : rtahilia
14065   -- At the time of doing this bugfix,
14066   -- we also discovered that all calls to_date use the format of DDMMYY
14067   -- in this procedure resulting in data being wrongly calculated
14068   -- for 2099 instead of 1999. Hence all to_date calls in this procedure
14069   -- have been changed to use the format of DDMMRR.
14070 
14071   -- PS : The serial numbers below are calcualted as
14072   --            (actual position of the data element) + 1
14073   -- This serial number gives us the column name in the table
14074   -- ben_ext_rslt_dtl where the data element will be stored.
14075   -- E.g. Salary Scale will be stored in column val_11
14076 
14077   -- Bugfix 3803760:FTSUPPLY
14078   -- Set the effective assignments as of effective start date
14079   --  of the current line of service
14080   debug('g_asg_count: '|| to_char(g_asg_count), 2) ;
14081 
14082   set_effective_assignments
14083      (p_primary_assignment_id     => p_assignment_id
14084      ,p_effective_date            => to_date(p_dtl_rec.val_13,'DDMMRR')
14085      );
14086   debug('g_asg_count: '|| to_char(g_asg_count), 6) ;
14087 
14088   -- Bugfix 3073562:GAP6
14089   -- Check if primary asg is to be reported
14090   l_report_primary_asg :=
14091         chk_report_assignment
14092           (p_assignment_id            => p_assignment_id
14093           ,p_secondary_assignment_id  => l_assignment_id
14094           -- Bugfix 3641851:CBF1 : Calling with effective start date of new line
14095           ,p_effective_date           => to_date(p_dtl_rec.val_13,'DDMMRR')
14096           );
14097 
14098 
14099   debug('p_assignment_id :'||p_assignment_id, 10);
14100   debug('l_assignment_id :'||l_assignment_id, 20);
14101   debug('l_report_primary_asg :'||l_report_primary_asg, 21);
14102   debug('p_rec_type :'||p_rec_type, 22);
14103 
14104   -- Bugfix 3880543:REHIRE : So that the unconditional refresh of
14105   --  some of the data elements works, we need to get the
14106   --  details from the asg row and apply it to the global
14107   --  collection row of l_assignment_id. But we only need
14108   --  to do this if g_override_ft_asg_id IS NULL coz if it
14109   --  is set (NOT NULL) then the refresh of asg dets in
14110   --  global collection sh hv alredy happened in the proc
14111   --  set_effective_assignments
14112   -- The refresh is mainly for the following
14113   --  a) Establishment number
14114   --  b) Employment category
14115   --  c) Grade - NOT DONE YET, mite hv to consider in future
14116   IF g_override_ft_asg_id IS NULL THEN
14117 
14118     OPEN csr_asg_details_up
14119           (l_assignment_id
14120           ,to_date(p_dtl_rec.val_13,'DDMMRR')
14121           );
14122     FETCH csr_asg_details_up INTO l_asg_details;
14123     IF csr_asg_details_up%NOTFOUND THEN
14124       -- This situation should never happen,
14125       debug('IMP : This situation should never happen', 23);
14126       NULL;
14127     ELSE -- asg record FOUND
14128 
14129       IF l_asg_details.location_id IS NOT NULL
14130          AND
14131          pqp_gb_tp_pension_extracts.g_criteria_estbs.EXISTS(l_asg_details.location_id) THEN
14132 
14133         -- Setting the current ext_emp_cat_cd, location_id and estb_number
14134         debug('Re-evaluating l_assignment_id details', 24);
14135 
14136         g_ext_asg_details(l_assignment_id).ext_emp_cat_cd :=
14137                         get_translate_asg_emp_cat_code
14138                           (l_asg_details.asg_emp_cat_cd
14139                           ,to_date(p_dtl_rec.val_13,'DDMMRR')
14140                           ,'Pension Extracts Employment Category Code'
14141                           ,l_asg_details.business_group_id
14142                           );
14143 
14144         debug('l_asg_details.location_id :'||to_char(l_asg_details.location_id), 25);
14145         g_ext_asg_details(l_assignment_id).location_id := l_asg_details.location_id;
14146 
14147         debug('Current estb number for l_assignment_id :'||
14148                         g_ext_asg_details(l_assignment_id).estb_number, 26);
14149         debug('Estb number in Global :'||
14150                         pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number, 27);
14151         g_ext_asg_details(l_assignment_id).estb_number :=
14152           pqp_gb_tp_pension_extracts.g_criteria_estbs(l_asg_details.location_id).estb_number;
14153 
14154       ELSE
14155         debug('WARNING: This asg might hv multiple leaver events', 27);
14156       END IF;
14157 
14158     END IF; -- csr_asg_details_up%NOTFOUND THEN
14159 
14160     CLOSE csr_asg_details_up;
14161 
14162   END IF;
14163 
14164   -- IF l_report_primary_asg is 'Y' then
14165   -- l_assignment_id will have the asg id of the primary assignment
14166   -- ELSE it will have the asg id of the secondary assignment
14167   --
14168   -- Guidelines on when to use l_assignment_id instead of p_assignment_id
14169   --  a) If the func u r calling does not chk if primary is to be reported
14170   --     or not, then you must pass l_assignment_id.
14171   --     E.g. Get_Grade_Fasttrack_Info
14172   --  b) If the func u r calling is intelligent and does the check on its own
14173   --     then you must pass p_assignment_id
14174   --     E.g. get_days_excluded_date
14175 
14176   -- 10) Recalc School / Type of Employment Number
14177   -- Bugfix 3073562:GAP10 : Now recalculating establishment number
14178   -- as Establishment change within LEA is a new line of service event
14179 
14180   -- Bugfix 3734942
14181   --  We need to adjust the part time asg count depending
14182   --  on the events that have occured. The count will get
14183   --  adjusted if we just add the pt_asg_count_change for
14184   --  the current event to g_part_time_asg_count coz its
14185   --  being set correctly when events are found and also
14186   --  being accumulated for multiple events on same date
14187   -- IMP Change : This logic has now moved up to create_new_lines
14188   debug('g_part_time_asg_count :'||to_char(g_part_time_asg_count), 30);
14189 
14190   IF p_rec_type = 'NEW' THEN
14191 
14192     IF g_override_ft_asg_id IS NOT NULL THEN
14193       -- Bugfix 3803760:FTSUPPLY
14194       -- If override asg has been set, we need to
14195       -- refresh estb number from this asg
14196       debug('Refreshing from override asg', 34);
14197       p_dtl_rec.val_10 :=
14198               nvl(pqp_gb_tp_pension_extracts.g_criteria_estbs
14199                       (g_ext_asg_details(l_assignment_id).location_id
14200                       ).estb_number
14201                  ,p_dtl_rec.val_10
14202                  );
14203 
14204     ELSIF g_part_time_asg_count > 1 THEN
14205       -- Bugfix 3641851:ENH6 Adding this new clause
14206       --   coz for concurrent PT asgs we report 0953
14207       -- Bugfix 3734942 : Refresh the estb number as per
14208       --  the part time asg count
14209 
14210       -- Multiple Pt asgs exist, estb number sh b 0953
14211       p_dtl_rec.val_10 := '0953';
14212 
14213     ELSE
14214       -- PT asg count is NOT GREATER than 1
14215       --  We should only refresh the estb number if
14216       --  PT count has fallen due to assignments
14217       --  leaving or emp cat change or a location
14218       --  change event has happened
14219       --  IF Some non relevant event has happened
14220       --  we should not refresh the estb number
14221       IF (INSTR(nvl(p_event_details.event_type,'XX')
14222                ,'PQP_GB_TP_ASG_LOCATION'
14223                ) > 0
14224           OR
14225           INSTR(nvl(p_event_details.event_type,'XX')
14226                ,'PQP_GB_TP_ASG_EMP_CAT'
14227                ) > 0
14228          )
14229          -- Bugfix 3880543:REHIRE : Only consider this emp cat OR Loc
14230          --  change if it happened on the asg we r reporting for
14231          AND
14232          (p_event_details.assignment_id = l_assignment_id
14233          ) THEN
14234 
14235         debug('new_location_id :'||to_char(p_event_details.new_location_id), 38);
14236         debug('val_10 :'||p_dtl_rec.val_10, 39);
14237 
14238         -- Bugfix 3470242:BUG1 : Now using new_location_id, estb_number can
14239         --     always be sought using the location id
14240         -- p_dtl_rec.val_10 := nvl(p_event_details.new_estb_number, p_dtl_rec.val_10);
14241         p_dtl_rec.val_10 := nvl(pqp_gb_tp_pension_extracts.g_criteria_estbs
14242                                 (p_event_details.new_location_id).estb_number
14243                            , p_dtl_rec.val_10
14244                            );
14245 
14246       ELSE
14247         -- Bugfix 3880543:REHIRE : Changed the above elsif to ELSE
14248         -- We now refresh unconditionally if none of the
14249         -- above conditions apply to fix the rehire problem
14250         debug('Refreshing from l_assignment_id', 40);
14251         p_dtl_rec.val_10
14252                 := nvl(pqp_gb_tp_pension_extracts.g_criteria_estbs
14253                                 (g_ext_asg_details(l_assignment_id).location_id
14254                                 ).estb_number
14255                       ,p_dtl_rec.val_10
14256                       );
14257       END IF;
14258 
14259     END IF; -- g_part_time_asg_count > 1 THEN
14260 
14261     debug('val_10 :'||p_dtl_rec.val_10, 41);
14262 
14263   END IF; -- p_rec_type = 'NEW' THEN
14264 
14265   --11.1)
14266   debug('val_20 :'||p_dtl_rec.val_20, 49);
14267 
14268   p_dtl_rec.val_20 := Get_Grade_Fasttrack_Info(p_assignment_id  => l_assignment_id
14269                                               ,p_effective_date => to_date(p_dtl_rec.val_13,'DDMMRR'));
14270 
14271   debug('val_20 :'||p_dtl_rec.val_20, 50);
14272 
14273   -- 11.2) Salary Scale
14274   --Changed by sshetty, added elsif clause
14275   --This part added by sshetty fix for a bug# 2478516
14276   --to vaidate safeguarded grade change.
14277   IF  p_rec_type = 'NEW'
14278      AND
14279      (
14280      INSTR(nvl(p_event_details.event_type,'XX')
14281            ,'PQP_GB_TP_SAFEGUARDED_SALARY'
14282            ) > 0
14283      OR
14284      INSTR(nvl(p_event_details.event_type,'XX')
14285                   ,'PRIMARY_RESTARTER'
14286            ) > 0
14287      ) THEN
14288 
14289     OPEN  csr_pqp_asg_attributes_up(l_assignment_id,
14290                                     TO_DATE(p_dtl_rec.val_13,'DDMMRR'));
14291     FETCH csr_pqp_asg_attributes_up INTO  l_pqp_asg_attributes_up;
14292 
14293     IF csr_pqp_asg_attributes_up%NOTFOUND THEN
14294       debug(l_proc_name, 60);
14295       l_pqp_asg_attributes_up.tp_safeguarded_grade := NULL;
14296     END IF;
14297 
14298     debug(l_proc_name, 70);
14299     CLOSE csr_pqp_asg_attributes_up;
14300 
14301     IF l_pqp_asg_attributes_up.tp_safeguarded_grade IS NOT NULL THEN
14302 
14303       pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).tp_safeguarded_grade
14304                            := l_pqp_asg_attributes_up.tp_safeguarded_grade;
14305       g_ext_asg_details(l_assignment_id).tp_safeguarded_grade
14306                            := l_pqp_asg_attributes_up.tp_safeguarded_grade;
14307 
14308       debug(l_proc_name, 80);
14309       IF l_report_primary_asg = 'N' THEN
14310         -- We need to keep the global record for primary asg updated as well
14311         pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).tp_safeguarded_grade
14312                            := l_pqp_asg_attributes_up.tp_safeguarded_grade;
14313         g_ext_asg_details(p_assignment_id).tp_safeguarded_grade
14314                     := l_pqp_asg_attributes_up.tp_safeguarded_grade;
14315 
14316         debug(l_proc_name, 90);
14317       END IF;
14318 
14319       p_dtl_rec.val_11 := l_pqp_asg_attributes_up.tp_safeguarded_grade;
14320 
14321     ELSE -- l_pqp_asg_attributes_up.tp_safeguarded_grade IS NOT NULL THEN
14322     --this validation is to make sure that we get the grade for that assignment
14323     --when safeguarded salary is made null.
14324 
14325       debug(l_proc_name, 100);
14326 
14327       l_temp_date :=
14328                   pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).start_date;
14329 
14330       -- Bugfix 3880543:GRD : We need to set the date in global
14331       --  collection for primary row as well, take a bakup
14332       l_temp_date_primary := g_ext_asg_details(p_assignment_id).teacher_start_date;
14333       l_temp_sfgrade :=
14334                   pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).tp_safeguarded_grade;
14335 
14336       pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).start_date
14337                                   := to_date(p_dtl_rec.val_13,'DDMMRR');
14338 
14339       -- Bugfix 3880543:GRD : We need to set the date in global
14340       --  collection for primary row as well
14341       g_ext_asg_details(p_assignment_id).teacher_start_date := to_date(p_dtl_rec.val_13,'DDMMRR');
14342 
14343       pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).tp_safeguarded_grade
14344                                   := NULL;
14345 
14346       pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
14347       l_new_sfgrade := pqp_gb_tp_pension_extracts.get_tp4_salary_scale
14348                                                  (p_assignment_id => p_assignment_id
14349                                                  );
14350       pqp_gb_tp_pension_extracts.g_nested_level := 0;
14351 
14352       pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).start_date:=
14353                                                                       l_temp_date;
14354       pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).tp_safeguarded_grade:=
14355                                                                       l_temp_sfgrade;
14356 
14357       -- Bugfix 3880543:GRD : We need to set the date in global
14358       --  collection for primary row as well, now restoring the date
14359       g_ext_asg_details(p_assignment_id).teacher_start_date:= l_temp_date_primary;
14360 
14361       IF l_new_sfgrade = 'INVALID' THEN
14362 
14363         l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14364                             (p_assignment_id     => p_assignment_id
14365                             ,p_error_text        => 'BEN_93037_EXT_TP1_INV_SAL_SCL'
14366                             ,p_error_number      => 93037
14367                             );
14368 
14369       ELSIF l_new_sfgrade = 'UNKNOWN' THEN
14370 
14371         l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14372                             (p_assignment_id     => p_assignment_id
14373                             ,p_error_text        => 'BEN_93038_EXT_TP1_UNK_SAL_SCL'
14374                             ,p_error_number      => 93038
14375                             );
14376 
14377       ELSIF l_new_sfgrade = 'TOOMANY' THEN
14378 
14379         l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14380                             (p_assignment_id     => p_assignment_id
14381                             ,p_error_text        => 'BEN_93039_EXT_TP1_TOOMANY_VALS'
14382                             ,p_error_number      => 93039
14383                             );
14384 
14385 
14386       ELSE -- valid value has been returned
14387        p_dtl_rec.val_11 := l_new_sfgrade;
14388       END IF;
14389       --
14390     END IF; -- l_pqp_asg_attributes_up.tp_safeguarded_grade IS NOT NULL THEN
14391     --
14392   END IF;
14393   --
14394   debug('val_11 :'||p_dtl_rec.val_11, 110);
14395   --
14396   IF p_rec_type = 'NEW'
14397      AND
14398      (
14399       (INSTR(nvl(p_event_details.event_type,'XX')
14400             ,'PQP_GB_TP_ASG_GRADE'
14401             ) > 0
14402        -- Bugfix 3470242:BUG1
14403        -- Before GAP4 enhancements, sal scale code cud only
14404        -- change at asg grade. But now coz sal scale code
14405        -- is derived from element entries or location EIT
14406        -- or spinal points, we must re-evaluate sal scale
14407        -- codes if any of these change for this asg.
14408        OR
14409        -- Bugfix 3470242:BUG1 : Re-evaluating for Element entry(EE) events
14410        INSTR(nvl(p_event_details.event_type,'XX')
14411                    ,'PQP_GB_TP_ELEMENT_ENTRY'
14412             ) > 0
14413        OR
14414        -- Bugfix 3470242:BUG1 : Re-evaluating for Location change event
14415        INSTR(nvl(p_event_details.event_type,'XX')
14416                    ,'PQP_GB_TP_ASG_LOCATION'
14417             ) > 0
14418        OR
14419        -- Bugfix 3470242:BUG1 : Re-evaluating for Spinal pt. change event
14420        INSTR(nvl(p_event_details.event_type,'XX')
14421                    ,'PQP_GB_TP_GRADE_STEP'
14422             ) > 0
14423        OR
14424        -- Bugfix 3880543:REHIRE : Primary leaver also needs to be considered
14425        --  now as we hv the rehire fix
14426        INSTR(nvl(p_event_details.event_type,'XX')
14427                   ,'PRIMARY_LEAVER'
14428             ) > 0
14429        OR
14430        INSTR(nvl(p_event_details.event_type,'XX')
14431                   ,'PRIMARY_RESTARTER'
14432             ) > 0
14433        OR
14434        INSTR(nvl(p_event_details.event_type,'XX')
14435                   ,'PRIMARY_STARTER'
14436             ) > 0
14437        OR
14438        INSTR(nvl(p_event_details.event_type,'XX')
14439                   ,'SECONDARY_STARTER'
14440             ) > 0
14441        OR
14442        INSTR(nvl(p_event_details.event_type,'XX')
14443                   ,'SECONDARY_LEAVER'
14444             ) > 0
14445        OR
14446        INSTR(nvl(p_event_details.event_type,'XX')
14447                   ,'SECONDARY_RESTARTER'
14448             ) > 0
14449        OR
14450        INSTR(nvl(p_event_details.event_type,'XX')
14451                   ,'PQP_GB_TP_ASG_EMP_CAT'
14452             ) > 0
14453       )
14454       AND
14455       (p_dtl_rec.val_20 <>'S'
14456       )
14457      ) THEN
14458 
14459     debug(l_proc_name, 120);
14460     -- Get the new grade value
14461     -- We need to fool the type 4 function so manipulate the global variables temporarily
14462 
14463     -- Make a copy of the original values
14464     l_temp_date := pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).start_date;
14465     -- Bugfix 3880543:GRD : We need to set the date in global
14466     --  collection for primary row as well, take a bakup
14467     l_temp_date_primary := g_ext_asg_details(p_assignment_id).teacher_start_date;
14468     l_temp_sfgrade := pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).tp_safeguarded_grade;
14469 
14470     -- Assign the effective date we need and make the sfgrade NULL
14471     pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).start_date
14472                                           := to_date(p_dtl_rec.val_13,'DDMMRR');
14473 
14474     -- Bugfix 3880543:GRD : We need to set the date in global
14475     --  collection for primary row as well
14476     g_ext_asg_details(p_assignment_id).teacher_start_date := to_date(p_dtl_rec.val_13,'DDMMRR');
14477     pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).tp_safeguarded_grade := NULL;
14478 
14479     -- Bugfix 3470242:BUG1 : Location change has happened so we need to
14480     --          replace the location_id in tp4 global collection.
14481     --          Currently not replacing in tp1 collection as its not
14482     --          used anywhere in code from here on
14483     IF INSTR(nvl(p_event_details.event_type,'XX')
14484                    ,'PQP_GB_TP_ASG_LOCATION'
14485             ) > 0  THEN
14486 
14487       debug(l_proc_name, 130);
14488       -- l_temp_location_id := pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).location_id;
14489       pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).location_id :=
14490         p_event_details.new_location_id;
14491       IF pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number = '0966' AND
14492          p_dtl_rec.val_10 <> '0966' THEN
14493          g_supp_teacher := 'Y';
14494       END IF;
14495       pqp_gb_tp_pension_extracts.g_ext_asg_details(p_assignment_id).estb_number := p_dtl_rec.val_10;
14496 
14497 
14498     END IF;
14499 
14500     pqp_gb_tp_pension_extracts.g_nested_level := g_nested_level;
14501     l_new_sfgrade := pqp_gb_tp_pension_extracts.get_tp4_salary_scale
14502                                                   (p_assignment_id => p_assignment_id
14503                                                   );
14504     pqp_gb_tp_pension_extracts.g_nested_level := 0;
14505 
14506     IF l_new_sfgrade = 'INVALID' THEN
14507 
14508       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14509                             (p_assignment_id     => p_assignment_id
14510                             ,p_error_text        => 'BEN_93037_EXT_TP1_INV_SAL_SCL'
14511                             ,p_error_number      => 93037
14512                             );
14513 
14514     ELSIF l_new_sfgrade = 'UNKNOWN' THEN
14515 
14516       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14517                             (p_assignment_id     => p_assignment_id
14518                             ,p_error_text        => 'BEN_93038_EXT_TP1_UNK_SAL_SCL'
14519                             ,p_error_number      => 93038
14520                             );
14521 
14522     ELSIF l_new_sfgrade = 'TOOMANY' THEN
14523 
14524       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14525                             (p_assignment_id     => p_assignment_id
14526                             ,p_error_text        => 'BEN_93039_EXT_TP1_TOOMANY_VALS'
14527                             ,p_error_number      => 93039
14528                             );
14529 
14530 
14531     ELSE -- valid value has been returned
14532 
14533       p_dtl_rec.val_11 := l_new_sfgrade;
14534 
14535     END IF;
14536 
14537     -- Assign back the original values to the global variables
14538     pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).start_date := l_temp_date;
14539     -- Bugfix 3880543:GRD :  We need to set the date in global
14540     --  collection for primary row as well, now restoring the date
14541     g_ext_asg_details(p_assignment_id).teacher_start_date := l_temp_date_primary;
14542     pqp_gb_tp_pension_extracts.g_ext_asg_details(l_assignment_id).tp_safeguarded_grade := l_temp_sfgrade;
14543 
14544   END IF; -- p_rec_type = 'NEW' for Salary Scale
14545   --
14546   debug('val_11 :'||p_dtl_rec.val_11, 140);
14547 
14548   -- 12) Full or Part-time Indicator
14549   IF g_override_ft_asg_id IS NOT NULL THEN
14550     -- Bugfix 3803760:FTSUPPLY
14551     -- If override asg has been set, we need to
14552     -- refresh FT Pt indicator from this asg
14553 
14554     p_dtl_rec.val_12 := g_ext_asg_details(l_assignment_id).ext_emp_cat_cd;
14555 
14556   -- ELSE Assign the new value only if the emp cat has changed
14557   ELSIF p_rec_type = 'NEW'
14558      AND
14559      (INSTR(nvl(p_event_details.event_type,'XX')
14560            ,'PQP_GB_TP_ASG_EMP_CAT'
14561            ) > 0
14562      )
14563      -- Bugfix 3880543:REHIRE : Only consider this emp cat change
14564      -- if it happened on the asg we r reporting for
14565      AND
14566      (p_event_details.assignment_id = l_assignment_id
14567      ) THEN
14568 
14569        debug('PQP_GB_TP_ASG_EMP_CAT event ',143);
14570 
14571        p_dtl_rec.val_12 := nvl(p_event_details.new_ext_emp_cat_cd,p_dtl_rec.val_12);
14572 
14573        debug('p_dtl_rec.val_12 '|| p_dtl_rec.val_12,144);
14574 
14575   ELSE
14576         debug('Refresh Emp category anyway ',145);
14577         -- get ext_emp_cat_cd from asg table as of start date of the
14578         -- current new line of service being re-evaluated
14579         p_dtl_rec.val_12 := get_ext_emp_cat_cd
14580                             (
14581                               l_assignment_id,
14582                               to_date(p_dtl_rec.val_13,'DDMMRR')
14583                             );
14584 
14585         debug('p_dtl_rec.val_12 '|| p_dtl_rec.val_12,146);
14586 
14587   END IF;
14588   --
14589   debug('val_12 :'||p_dtl_rec.val_12, 150);
14590 
14591   -- 15) Withdrawal conf
14592   -- Currently handled from procedure create_new_lines
14593 
14594   -- Added a new param p_emp_cat_cd for Bug fix 2341170,2341276
14595 
14596   -- Fix : 3823873
14597   -- Store the employment category in temporary variable and
14598   -- pass the changed emp cat to the get_days_excluded_date
14599   -- and reset after the call
14600 
14601   debug('g_ext_asg_details(p_assignment_id).ext_emp_cat_cd '||g_ext_asg_details(p_assignment_id).ext_emp_cat_cd, 152 );
14602 
14603   l_temp_ext_emp_cat_cd := g_ext_asg_details(p_assignment_id).ext_emp_cat_cd;
14604   g_ext_asg_details(p_assignment_id).ext_emp_cat_cd := p_dtl_rec.val_12;
14605 
14606   -- 16) Days Excluded
14607   l_return := get_days_excluded_date
14608                 (p_assignment_id        => p_assignment_id
14609                 ,p_effective_start_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14610                 ,p_effective_end_date   => to_date(p_dtl_rec.val_14,'DDMMRR')
14611                 ,p_emp_cat_cd           => p_dtl_rec.val_12
14612                 ,p_days                 => l_days_excluded -- OUT
14613                 );
14614   --reset the employment category.
14615   g_ext_asg_details(p_assignment_id).ext_emp_cat_cd := l_temp_ext_emp_cat_cd;
14616   debug('g_ext_asg_details(p_assignment_id).ext_emp_cat_cd '||g_ext_asg_details(p_assignment_id).ext_emp_cat_cd, 154 );
14617 
14618    if l_return = -2 THEN    -- bug 6275363
14619     p_dtl_rec.val_16 := '+00';
14620 
14621    elsif l_return <> -1 THEN
14622 
14623      p_dtl_rec.val_16 := trim(to_char(l_days_excluded,'099'));
14624 
14625     ELSE
14626 
14627     p_dtl_rec.val_16 := '000';
14628 
14629     END IF;
14630   l_return := NULL;
14631   --
14632   debug('val_16 :'||p_dtl_rec.val_16, 160);
14633   --
14634   -- 17) Annual Full-time Salary Rate
14635   l_return := get_annual_sal_rate_date
14636                 (p_assignment_id        => p_assignment_id
14637                 ,p_effective_start_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14638                 ,p_effective_end_date   => to_date(p_dtl_rec.val_14,'DDMMRR')
14639                 ,p_rate                 => l_annual_rate
14640                 );
14641 
14642   IF l_return <> -1 THEN
14643 
14644     g_annual_rate(p_assignment_id) := l_annual_rate;
14645     p_dtl_rec.val_17 := trim(to_char(l_annual_rate,'099999'));
14646 
14647   ELSE
14648 
14649     p_dtl_rec.val_17 := '000000';
14650 
14651   END IF;
14652   l_return := NULL;
14653   --
14654   debug('val_17 :'||p_dtl_rec.val_17, 170);
14655   --
14656   -- 19) Part-Time Salary Paid
14657   IF p_dtl_rec.val_12 = 'P' THEN -- Part-time employment
14658 
14659     l_part_time_sal := get_part_time_sal_date
14660                          (p_assignment_id        => p_assignment_id
14661                          ,p_effective_start_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14662                          ,p_effective_end_date   => to_date(p_dtl_rec.val_14,'DDMMRR')
14663                          );
14664 
14665     -- Check whether part time sal exceeds annual salary rate
14666     -- Fill in with zeros instead of space
14667     -- Bug fix 2353106
14668     -- Bug Fix 6140377
14669     IF sign(l_part_time_sal) = -1 THEN
14670        p_dtl_rec.val_19 := '-'|| lpad(abs(l_part_time_sal),5,'0');
14671     ELSE
14672        p_dtl_rec.val_19 := lpad(l_part_time_sal,6,'0');
14673     END IF;
14674     --
14675   ELSE -- emp cat cd is not part time
14676     -- Bug Fix 6140377
14677     p_dtl_rec.val_19 := '000000';
14678 
14679   END IF; -- p_dtl_rec.val_12 = 'P' THEN
14680   --
14681   debug('val_19 :'||p_dtl_rec.val_19, 180);
14682   --
14683   -- 20) Safeguarded Salary Fast Track
14684   ---This part is now is 11.1
14685 
14686   -- 21) London Allowance - Rate Payable
14687 /*
14688   l_london_allowance := Get_Allowance_Code(p_assignment_id  => l_assignment_id
14689                                           ,p_effective_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14690                                           ,p_allowance_type => 'LONDON_ALLOWANCE_RULE'
14691                                           );
14692 */
14693   l_london_allowance := Get_Allowance_Code_New(p_assignment_id  => l_assignment_id
14694                                           ,p_effective_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14695                                           ,p_allowance_type => 'LONDON_ALLOWANCE_RULE'
14696                                           );
14697 
14698   debug('l_london_allowance :'||l_london_allowance, 190);
14699   IF l_london_allowance = 'UNKNOWN' THEN
14700 
14701     l_london_allowance := ' ';
14702 
14703   ELSIF l_london_allowance = 'TOOMANY' THEN
14704 
14705     l_london_allowance := ' ';
14706     -- Bugfix 3516282 : Now passing assignment_id
14707     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14708                      (p_assignment_id => p_assignment_id
14709                      ,p_error_text    => 'BEN_93032_EXT_TP1_LON_ALL_MANY'
14710                      ,p_error_number  => 93032
14711                      );
14712 
14713   END IF;
14714   --
14715   debug('l_london_allowance :'||l_london_allowance, 200);
14716   --
14717   p_dtl_rec.val_21 := substr(l_london_allowance,1,1);
14718 
14719 
14720   -- 22) Special Priority - Allowance Payable Special Needs
14721 /*
14722   l_sp_allowance := Get_Allowance_Code(p_assignment_id  => l_assignment_id
14723                                       ,p_effective_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14724                                       ,p_allowance_type => 'SPECIAL_ALLOWANCE_RULE'
14725                                       );
14726 */
14727  l_sp_allowance := Get_Allowance_Code_New(p_assignment_id  => l_assignment_id
14728                                       ,p_effective_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14729                                       ,p_allowance_type => 'SPECIAL_ALLOWANCE_RULE'
14730                                       );
14731 
14732   debug('l_sp_allowance :'||l_sp_allowance, 210);
14733   IF l_sp_allowance = 'UNKNOWN' THEN
14734 
14735     l_sp_allowance := '0';
14736 
14737   ELSIF l_sp_allowance = 'TOOMANY' THEN
14738 
14739     l_sp_allowance := '0';
14740     -- Bugfix 3516282 : Now passing assignment_id
14741     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14742                      (p_assignment_id => p_assignment_id
14743                      ,p_error_text    => 'BEN_93034_EXT_TP1_SP_ALL_MANY'
14744                      ,p_error_number  => 93034
14745                      );
14746 
14747   END IF;
14748   --
14749   debug('l_sp_allowance :'||l_sp_allowance, 220);
14750   --
14751   p_dtl_rec.val_22 := substr(l_sp_allowance,1,1);
14752 
14753 
14754   -- 23) Special Class Additions (Part-time Indicator)
14755   l_contract_indicator := Get_Special_ClassRule(p_assignment_id  => l_assignment_id
14756                                                ,p_effective_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14757                                                );
14758 
14759   debug('l_contract_indicator :'||l_contract_indicator, 230);
14760 
14761   IF l_contract_indicator = 'UNKNOWN' THEN
14762 
14763     l_contract_indicator := '0';
14764 
14765   ELSIF l_contract_indicator = 'INVALID' THEN
14766 
14767     l_contract_indicator := '0';
14768     -- Bugfix 3516282 : Now passing assignment_id
14769     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14770                      (p_assignment_id => p_assignment_id
14771                      ,p_error_text    => 'BEN_93036_EXT_TP1_INV_EMP_CAT'
14772                      ,p_error_number  => 93036
14773                      );
14774 
14775   END IF;
14776   --
14777   debug('l_contract_indicator :'||l_contract_indicator, 240);
14778   --
14779   p_dtl_rec.val_23 := l_contract_indicator;
14780 
14781 
14782   -- 24) Other Allowances
14783   IF g_other_allowance.EXISTS(p_assignment_id) THEN
14784 
14785      debug('Other Allowance : '||to_char(g_other_allowance(p_assignment_id)),245);
14786 
14787      -- 4336613 : SAL_VALIDAT_3A : Check whether Other_All value has exceeeded 4 digit limit
14788      -- If yes, raise warning.
14789      if g_other_allowance(p_assignment_id) > 99999 then
14790 
14791         l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14792                      (p_assignment_id => p_assignment_id
14793                      ,p_error_text    => 'BEN_93043_EXT_TP1_OTH_VAL_EXC'
14794                      ,p_error_number  => 93043
14795                      ,p_token1        => TO_CHAR(g_other_allowance(p_assignment_id)) -- bug : 4336613
14796                      );
14797 
14798         g_other_allowance(p_assignment_id) := 99999;  -- 4336613 : SAL_VALIDAT_3A :
14799                                                     -- set to 9999 if > 9999
14800 
14801      end if; -- end if of other allowance max limit check ...
14802 
14803      -- Fill in with zeros instead of space
14804      -- Bug fix 2353106
14805 
14806      debug('Other Allowance : '||to_char(g_other_allowance(p_assignment_id)),246);
14807 
14808      l_other_allowance := lpad(g_other_allowance(p_assignment_id),5,'0');
14809 
14810   END IF; -- end if of other allowance exists check
14811   --
14812   debug('l_other_allowance :'||l_other_allowance, 250);
14813   --
14814   p_dtl_rec.val_24 := l_other_allowance;
14815 
14816   --
14817   -- 27) Annual Retention Allowance Rate
14818   -- Added for legislative updates to management and retention allowance changes
14819   l_return := calc_tp1_retention_allow_rate
14820                 (p_assignment_id        => p_assignment_id
14821                 ,p_effective_start_date => to_date(p_dtl_rec.val_13,'DDMMRR')
14822                 ,p_effective_end_date   => to_date(p_dtl_rec.val_14,'DDMMRR')
14823                 ,p_rate                 => l_ret_allow
14824                 );
14825 
14826   IF l_return <> -1 THEN
14827 
14828     p_dtl_rec.val_27 := trim(to_char(l_ret_allow,'09999'));
14829 
14830   ELSE
14831 
14832     p_dtl_rec.val_27 := '00000';
14833 
14834   END IF;
14835   l_return := NULL;
14836   --
14837   debug('val_27 :'||p_dtl_rec.val_27, 255);
14838   --
14839 
14840   -- RETRO:BUG: 4135481
14841   IF g_raise_retro_warning = 'Y' THEN
14842     -- Raise a warning here if both Prorated/Retro payments exist
14843     -- for the LoS.
14844 
14845     debug('Raising Warning for RETRO/PRORATION', 260);
14846     l_error := pqp_gb_tp_extract_functions.raise_extract_warning
14847                  (p_assignment_id => p_assignment_id
14848                  ,p_error_text    => 'BEN_94159_EXT_TP_RETRO_PAY'
14849                  ,p_error_number  => 94159
14850                  --,p_token1        => fnd_date.string_to_canonical(p_dtl_rec.val_13,'DDMMYY')
14851                  );
14852      g_raise_retro_warning := 'N' ;
14853   END IF;
14854 
14855   debug_exit(l_proc_name);
14856 
14857   RETURN;
14858 
14859 EXCEPTION
14860   WHEN OTHERS THEN
14861     debug('SQLCODE :'||to_char(SQLCODE), 260);
14862     debug('SQLERRM :'||SQLERRM, 270);
14863     debug_exit(' Others in '||l_proc_name);
14864     p_dtl_rec := l_dtl_rec_nc;
14865     RAISE;
14866 END; -- recalc_data_elements
14867 
14868 PROCEDURE upd_rslt_dtl(p_dtl_rec IN ben_ext_rslt_dtl%ROWTYPE)
14869 IS
14870 
14871   l_proc_name          VARCHAR2(61):=
14872      g_proc_name||'upd_rslt_dtl';
14873 
14874 BEGIN -- upd_rslt_dtl
14875 
14876   debug_enter(l_proc_name);
14877 
14878   UPDATE ben_ext_rslt_dtl
14879   SET VAL_01                 = p_dtl_rec.VAL_01
14880      ,VAL_02                 = p_dtl_rec.VAL_02
14881      ,VAL_03                 = p_dtl_rec.VAL_03
14882      ,VAL_04                 = p_dtl_rec.VAL_04
14883      ,VAL_05                 = p_dtl_rec.VAL_05
14884      ,VAL_06                 = p_dtl_rec.VAL_06
14885      ,VAL_07                 = p_dtl_rec.VAL_07
14886      ,VAL_08                 = p_dtl_rec.VAL_08
14887      ,VAL_09                 = p_dtl_rec.VAL_09
14888      ,VAL_10                 = p_dtl_rec.VAL_10
14889      ,VAL_11                 = p_dtl_rec.VAL_11
14890      ,VAL_12                 = p_dtl_rec.VAL_12
14891      ,VAL_13                 = p_dtl_rec.VAL_13
14892      ,VAL_14                 = p_dtl_rec.VAL_14
14893      ,VAL_15                 = p_dtl_rec.VAL_15
14894      ,VAL_16                 = p_dtl_rec.VAL_16
14895      ,VAL_17                 = p_dtl_rec.VAL_17
14896      ,VAL_19                 = p_dtl_rec.VAL_19
14897      ,VAL_18                 = p_dtl_rec.VAL_18
14898      ,VAL_20                 = p_dtl_rec.VAL_20
14899      ,VAL_21                 = p_dtl_rec.VAL_21
14900      ,VAL_22                 = p_dtl_rec.VAL_22
14901      ,VAL_23                 = p_dtl_rec.VAL_23
14902      ,VAL_24                 = p_dtl_rec.VAL_24
14903      ,VAL_25                 = p_dtl_rec.VAL_25
14904      ,VAL_26                 = p_dtl_rec.VAL_26
14905      ,VAL_27                 = p_dtl_rec.VAL_27
14906      ,VAL_28                 = p_dtl_rec.VAL_28
14907      ,VAL_29                 = p_dtl_rec.VAL_29
14908      ,VAL_30                 = p_dtl_rec.VAL_30
14909      ,VAL_31                 = p_dtl_rec.VAL_31
14910      ,VAL_32                 = p_dtl_rec.VAL_32
14911      ,VAL_33                 = p_dtl_rec.VAL_33
14912      ,VAL_34                 = p_dtl_rec.VAL_34
14913      ,VAL_35                 = p_dtl_rec.VAL_35
14914      ,VAL_36                 = p_dtl_rec.VAL_36
14915      ,VAL_37                 = p_dtl_rec.VAL_37
14916      ,VAL_38                 = p_dtl_rec.VAL_38
14917      ,VAL_39                 = p_dtl_rec.VAL_39
14918      ,VAL_40                 = p_dtl_rec.VAL_40
14919      ,VAL_41                 = p_dtl_rec.VAL_41
14920      ,VAL_42                 = p_dtl_rec.VAL_42
14921      ,VAL_43                 = p_dtl_rec.VAL_43
14922      ,VAL_44                 = p_dtl_rec.VAL_44
14923      ,VAL_45                 = p_dtl_rec.VAL_45
14924      ,VAL_46                 = p_dtl_rec.VAL_46
14925      ,VAL_47                 = p_dtl_rec.VAL_47
14926      ,VAL_48                 = p_dtl_rec.VAL_48
14927      ,VAL_49                 = p_dtl_rec.VAL_49
14928      ,VAL_50                 = p_dtl_rec.VAL_50
14929      ,VAL_51                 = p_dtl_rec.VAL_51
14930      ,VAL_52                 = p_dtl_rec.VAL_52
14931      ,VAL_53                 = p_dtl_rec.VAL_53
14932      ,VAL_54                 = p_dtl_rec.VAL_54
14933      ,VAL_55                 = p_dtl_rec.VAL_55
14934      ,VAL_56                 = p_dtl_rec.VAL_56
14935      ,VAL_57                 = p_dtl_rec.VAL_57
14936      ,VAL_58                 = p_dtl_rec.VAL_58
14937      ,VAL_59                 = p_dtl_rec.VAL_59
14938      ,VAL_60                 = p_dtl_rec.VAL_60
14939      ,VAL_61                 = p_dtl_rec.VAL_61
14940      ,VAL_62                 = p_dtl_rec.VAL_62
14941      ,VAL_63                 = p_dtl_rec.VAL_63
14942      ,VAL_64                 = p_dtl_rec.VAL_64
14943      ,VAL_65                 = p_dtl_rec.VAL_65
14944      ,VAL_66                 = p_dtl_rec.VAL_66
14945      ,VAL_67                 = p_dtl_rec.VAL_67
14946      ,VAL_68                 = p_dtl_rec.VAL_68
14947      ,VAL_69                 = p_dtl_rec.VAL_69
14948      ,VAL_70                 = p_dtl_rec.VAL_70
14949      ,VAL_71                 = p_dtl_rec.VAL_71
14950      ,VAL_72                 = p_dtl_rec.VAL_72
14951      ,VAL_73                 = p_dtl_rec.VAL_73
14952      ,VAL_74                 = p_dtl_rec.VAL_74
14953      ,VAL_75                 = p_dtl_rec.VAL_75
14954      ,OBJECT_VERSION_NUMBER  = p_dtl_rec.OBJECT_VERSION_NUMBER
14955      ,THRD_SORT_VAL          = p_dtl_rec.THRD_SORT_VAL
14956   WHERE ext_rslt_dtl_id = p_dtl_rec.ext_rslt_dtl_id;
14957 
14958   debug_exit(l_proc_name);
14959 
14960   RETURN;
14961 
14962 EXCEPTION
14963   WHEN OTHERS THEN
14964     debug_exit(' Others in '||l_proc_name);
14965     RAISE;
14966 END; -- upd_rslt_dtl
14967 
14968 PROCEDURE ins_rslt_dtl(p_dtl_rec IN OUT NOCOPY ben_ext_rslt_dtl%ROWTYPE)
14969 IS
14970 
14971 
14972 -- nocopy changes
14973 l_dtl_rec_nc     ben_ext_rslt_dtl%ROWTYPE;
14974 
14975 
14976   l_proc_name          VARCHAR2(61):=
14977      g_proc_name||'ins_rslt_dtl';
14978 
14979 BEGIN -- ins_rslt_dtl
14980 
14981   debug_enter(l_proc_name);
14982 
14983   -- nocopy changes
14984   l_dtl_rec_nc := p_dtl_rec;
14985 
14986   SELECT ben_ext_rslt_dtl_s.NEXTVAL INTO p_dtl_rec.ext_rslt_dtl_id FROM dual;
14987 
14988   INSERT INTO ben_ext_rslt_dtl
14989   (EXT_RSLT_DTL_ID
14990   ,EXT_RSLT_ID
14991   ,BUSINESS_GROUP_ID
14992   ,EXT_RCD_ID
14993   ,PERSON_ID
14994   ,VAL_01
14995   ,VAL_02
14996   ,VAL_03
14997   ,VAL_04
14998   ,VAL_05
14999   ,VAL_06
15000   ,VAL_07
15001   ,VAL_08
15002   ,VAL_09
15003   ,VAL_10
15004   ,VAL_11
15005   ,VAL_12
15006   ,VAL_13
15007   ,VAL_14
15008   ,VAL_15
15009   ,VAL_16
15010   ,VAL_17
15011   ,VAL_19
15012   ,VAL_18
15013   ,VAL_20
15014   ,VAL_21
15015   ,VAL_22
15016   ,VAL_23
15017   ,VAL_24
15018   ,VAL_25
15019   ,VAL_26
15020   ,VAL_27
15021   ,VAL_28
15022   ,VAL_29
15023   ,VAL_30
15024   ,VAL_31
15025   ,VAL_32
15026   ,VAL_33
15027   ,VAL_34
15028   ,VAL_35
15029   ,VAL_36
15030   ,VAL_37
15031   ,VAL_38
15032   ,VAL_39
15033   ,VAL_40
15034   ,VAL_41
15035   ,VAL_42
15036   ,VAL_43
15037   ,VAL_44
15038   ,VAL_45
15039   ,VAL_46
15040   ,VAL_47
15041   ,VAL_48
15042   ,VAL_49
15043   ,VAL_50
15044   ,VAL_51
15045   ,VAL_52
15046   ,VAL_53
15047   ,VAL_54
15048   ,VAL_55
15049   ,VAL_56
15050   ,VAL_57
15051   ,VAL_58
15052   ,VAL_59
15053   ,VAL_60
15054   ,VAL_61
15055   ,VAL_62
15056   ,VAL_63
15057   ,VAL_64
15058   ,VAL_65
15059   ,VAL_66
15060   ,VAL_67
15061   ,VAL_68
15062   ,VAL_69
15063   ,VAL_70
15064   ,VAL_71
15065   ,VAL_72
15066   ,VAL_73
15067   ,VAL_74
15068   ,VAL_75
15069   ,CREATED_BY
15070   ,CREATION_DATE
15071   ,LAST_UPDATE_DATE
15072   ,LAST_UPDATED_BY
15073   ,LAST_UPDATE_LOGIN
15074   ,PROGRAM_APPLICATION_ID
15075   ,PROGRAM_ID
15076   ,PROGRAM_UPDATE_DATE
15077   ,REQUEST_ID
15078   ,OBJECT_VERSION_NUMBER
15079   ,PRMY_SORT_VAL
15080   ,SCND_SORT_VAL
15081   ,THRD_SORT_VAL
15082   ,TRANS_SEQ_NUM
15083   ,RCRD_SEQ_NUM
15084   )
15085   VALUES
15086   (p_dtl_rec.EXT_RSLT_DTL_ID
15087   ,p_dtl_rec.EXT_RSLT_ID
15088   ,p_dtl_rec.BUSINESS_GROUP_ID
15089   ,p_dtl_rec.EXT_RCD_ID
15090   ,p_dtl_rec.PERSON_ID
15091   ,p_dtl_rec.VAL_01
15092   ,p_dtl_rec.VAL_02
15093   ,p_dtl_rec.VAL_03
15094   ,p_dtl_rec.VAL_04
15095   ,p_dtl_rec.VAL_05
15096   ,p_dtl_rec.VAL_06
15097   ,p_dtl_rec.VAL_07
15098   ,p_dtl_rec.VAL_08
15099   ,p_dtl_rec.VAL_09
15100   ,p_dtl_rec.VAL_10
15101   ,p_dtl_rec.VAL_11
15102   ,p_dtl_rec.VAL_12
15103   ,p_dtl_rec.VAL_13
15104   ,p_dtl_rec.VAL_14
15105   ,p_dtl_rec.VAL_15
15106   ,p_dtl_rec.VAL_16
15107   ,p_dtl_rec.VAL_17
15108   ,p_dtl_rec.VAL_19
15109   ,p_dtl_rec.VAL_18
15110   ,p_dtl_rec.VAL_20
15111   ,p_dtl_rec.VAL_21
15112   ,p_dtl_rec.VAL_22
15113   ,p_dtl_rec.VAL_23
15114   ,p_dtl_rec.VAL_24
15115   ,p_dtl_rec.VAL_25
15116   ,p_dtl_rec.VAL_26
15117   ,p_dtl_rec.VAL_27
15118   ,p_dtl_rec.VAL_28
15119   ,p_dtl_rec.VAL_29
15120   ,p_dtl_rec.VAL_30
15121   ,p_dtl_rec.VAL_31
15122   ,p_dtl_rec.VAL_32
15123   ,p_dtl_rec.VAL_33
15124   ,p_dtl_rec.VAL_34
15125   ,p_dtl_rec.VAL_35
15126   ,p_dtl_rec.VAL_36
15127   ,p_dtl_rec.VAL_37
15128   ,p_dtl_rec.VAL_38
15129   ,p_dtl_rec.VAL_39
15130   ,p_dtl_rec.VAL_40
15131   ,p_dtl_rec.VAL_41
15132   ,p_dtl_rec.VAL_42
15133   ,p_dtl_rec.VAL_43
15134   ,p_dtl_rec.VAL_44
15135   ,p_dtl_rec.VAL_45
15136   ,p_dtl_rec.VAL_46
15137   ,p_dtl_rec.VAL_47
15138   ,p_dtl_rec.VAL_48
15139   ,p_dtl_rec.VAL_49
15140   ,p_dtl_rec.VAL_50
15141   ,p_dtl_rec.VAL_51
15142   ,p_dtl_rec.VAL_52
15143   ,p_dtl_rec.VAL_53
15144   ,p_dtl_rec.VAL_54
15145   ,p_dtl_rec.VAL_55
15146   ,p_dtl_rec.VAL_56
15147   ,p_dtl_rec.VAL_57
15148   ,p_dtl_rec.VAL_58
15149   ,p_dtl_rec.VAL_59
15150   ,p_dtl_rec.VAL_60
15151   ,p_dtl_rec.VAL_61
15152   ,p_dtl_rec.VAL_62
15153   ,p_dtl_rec.VAL_63
15154   ,p_dtl_rec.VAL_64
15155   ,p_dtl_rec.VAL_65
15156   ,p_dtl_rec.VAL_66
15157   ,p_dtl_rec.VAL_67
15158   ,p_dtl_rec.VAL_68
15159   ,p_dtl_rec.VAL_69
15160   ,p_dtl_rec.VAL_70
15161   ,p_dtl_rec.VAL_71
15162   ,p_dtl_rec.VAL_72
15163   ,p_dtl_rec.VAL_73
15164   ,p_dtl_rec.VAL_74
15165   ,p_dtl_rec.VAL_75
15166   ,p_dtl_rec.CREATED_BY
15167   ,p_dtl_rec.CREATION_DATE
15168   ,p_dtl_rec.LAST_UPDATE_DATE
15169   ,p_dtl_rec.LAST_UPDATED_BY
15170   ,p_dtl_rec.LAST_UPDATE_LOGIN
15171   ,p_dtl_rec.PROGRAM_APPLICATION_ID
15172   ,p_dtl_rec.PROGRAM_ID
15173   ,p_dtl_rec.PROGRAM_UPDATE_DATE
15174   ,p_dtl_rec.REQUEST_ID
15175   ,p_dtl_rec.OBJECT_VERSION_NUMBER
15176   ,p_dtl_rec.PRMY_SORT_VAL
15177   ,p_dtl_rec.SCND_SORT_VAL
15178   ,p_dtl_rec.THRD_SORT_VAL
15179   ,p_dtl_rec.TRANS_SEQ_NUM
15180   ,p_dtl_rec.RCRD_SEQ_NUM
15181   );
15182 
15183   debug_exit(l_proc_name);
15184 
15185   RETURN;
15186 
15187 EXCEPTION
15188   WHEN OTHERS THEN
15189     debug_exit(' Others in '||l_proc_name);
15190     p_dtl_rec := l_dtl_rec_nc;
15191     RAISE;
15192 END; -- ins_rslt_dtl
15193 
15194 --
15195 -- Validate that the given date is in the range of the primary asg
15196 --
15197 FUNCTION date_in_prmy_range
15198   (p_date       IN OUT NOCOPY  DATE
15199   ,p_date_type  IN      VARCHAR2
15200   ) RETURN VARCHAR2
15201 IS
15202 
15203   -- Variable Declaration
15204   l_itr         NUMBER(5);
15205   l_valid       VARCHAR2(20) := 'Y';
15206 
15207 
15208   l_proc_name          VARCHAR2(61):=
15209      g_proc_name||'date_in_prmy_range';
15210 
15211   -- nocopy changes
15212   l_date_nc    DATE;
15213 
15214 BEGIN -- date_in_prmy_range
15215 
15216   debug_enter(l_proc_name);
15217 
15218 
15219   -- nocopy changes
15220   l_date_nc := p_date;
15221 
15222   IF g_primary_leaver_dates.COUNT > 0 THEN
15223 
15224     FOR l_itr IN g_primary_leaver_dates.FIRST..g_primary_leaver_dates.LAST
15225     LOOP
15226 
15227       debug('p_date :'||to_char(p_date,'DD/MM/YYYY'), 10);
15228 
15229       IF p_date BETWEEN g_primary_leaver_dates(l_itr).start_date
15230                      AND nvl(g_primary_leaver_dates(l_itr).leaver_date
15231                             ,g_effective_run_date
15232                             ) THEN
15233 
15234         debug('The date is valid', 20);
15235         l_valid := 'Y';
15236         EXIT;
15237 
15238       ELSIF g_primary_leaver_dates(l_itr).restarter_date IS NOT NULL
15239             AND
15240             p_date >= g_primary_leaver_dates(l_itr).restarter_date THEN
15241 
15242          debug('The date might be valid, continue checking', 30);
15243          l_valid := 'Y';
15244 
15245       ELSIF (p_date BETWEEN g_primary_leaver_dates(l_itr).leaver_date
15246                      AND nvl(g_primary_leaver_dates(l_itr).restarter_date
15247                             ,g_effective_run_date
15248                             )
15249             ) THEN
15250 
15251         debug(l_proc_name, 40);
15252         -- Date is invalid in primary date range, but return the nearest
15253         -- leaver / restarter date of the primary asg  as we want to find
15254         -- new line of service events for this secondary asg
15255         IF p_date_type = 'L' --Leaver
15256         THEN
15257 
15258           p_date := LEAST(g_primary_leaver_dates(l_itr).leaver_date
15259                          ,p_date
15260                          );
15261 
15262           l_valid := 'Y';
15263 
15264         ELSE -- ='R' for Restarter
15265 
15266           debug(l_proc_name, 50);
15267           IF g_primary_leaver_dates(l_itr).restarter_date IS NOT NULL THEN
15268 
15269             p_date := GREATEST(g_primary_leaver_dates(l_itr).restarter_date
15270                               ,p_date
15271                               );
15272             l_valid := 'Y';
15273 
15274           ELSE
15275 
15276             l_valid := 'N';
15277 
15278           END IF; -- g_primary_leaver_dates(l_itr).restarter_date IS NOT NULL THEN
15279           --
15280         END IF; -- p_date_type = 'L'
15281         --
15282         debug(l_proc_name, 60);
15283         -- Exit as validation done
15284         EXIT;
15285         --
15286       END IF; -- (p_date BETWEEN g_primary_leaver_dates(l_itr).leaver_date
15287       --
15288     END LOOP;
15289     --
15290   ELSE
15291     -- Primary asg does not have a leaver/restarter event
15292     l_valid := 'Y';
15293   END IF;
15294   --
15295   debug('l_valid :'||l_valid, 70);
15296   debug('p_date :'||to_char(p_date,'DD/MM/YYYY'), 80);
15297   debug_exit(l_proc_name);
15298 
15299   RETURN l_valid;
15300 
15301 EXCEPTION
15302     WHEN OTHERS THEN
15303       debug_exit(' Others in '||l_proc_name);
15304       p_date := l_date_nc;
15305       RAISE;
15306 END; -- date_in_prmy_range
15307 
15308 
15309 --
15310 -- Store multiple sets of leaver and restarter dates
15311 --
15312 PROCEDURE store_leaver_restarter_dates
15313   (p_assignment_id            IN      NUMBER  -- context
15314   )
15315 IS
15316 
15317   -- Variable Declaration
15318   l_new_event_itr       NUMBER(5);
15319   l_leaver_dates_itr    NUMBER(5);
15320   l_prefix              VARCHAR2(20);
15321   l_leaver              VARCHAR2(1) := 'N';
15322   l_leaver_date         DATE := NULL;
15323   l_restarter           VARCHAR2(1) := 'N';
15324   l_restarter_date      DATE := NULL;
15325   l_continue            VARCHAR2(1);
15326   l_business_group_id   per_all_assignments_f.business_group_id%TYPE;
15327   l_restarter_ext_emp_cat_cd    VARCHAR2(1);
15328   l_leaver_ext_emp_cat_cd       VARCHAR2(1);
15329 
15330   -- LVRDATE change:
15331   l_period_end_date     DATE := NULL ;
15332 
15333   l_proc_name          VARCHAR2(61):=
15334      g_proc_name||'store_leaver_restarter_dates';
15335 
15336 BEGIN -- store_leaver_restarter_dates
15337 
15338   debug_enter(l_proc_name);
15339 
15340   -- Bugfix 3073562:GAP1:GAP2
15341   l_business_group_id := nvl(g_ext_asg_details(p_assignment_id).business_group_id
15342                             ,g_business_group_id
15343                             );
15344 
15345   IF p_assignment_id = g_primary_assignment_id THEN
15346 
15347     l_prefix := 'PRIMARY_';
15348     debug('Processing Primary Assignment',20);
15349 
15350     -- Step 1) Store the first set of leaver and restarter dates in the global collection
15351     IF g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL THEN
15352 
15353       debug('Leaver Date Found',10);
15354 
15355       l_new_event_itr := g_asg_events.COUNT+1;
15356       g_asg_events(l_new_event_itr).event_date        := g_ext_asg_details(p_assignment_id).leaver_date;
15357       g_asg_events(l_new_event_itr).event_type        := l_prefix||'LEAVER';
15358       g_asg_events(l_new_event_itr).assignment_id     := p_assignment_id;
15359 
15360       -- Bugfix 3880543:REHIRE : Now also changing the pt asg count
15361       --   If a part time asg is a leaver then
15362       -- we also need to decrement the g_part_time_asg_count
15363       -- when this event is processed
15364       IF g_ext_asg_details(p_assignment_id).ext_emp_cat_cd = 'P' THEN
15365         g_asg_events(l_new_event_itr).pt_asg_count_change := -1;
15366       END IF;
15367 
15368       -- Bugfix 3803760:TERMASG
15369       g_asg_events(l_new_event_itr).asg_count_change  := -1;
15370 
15371       -- Add this leaver date to the global collection for primary assignments only
15372       l_leaver_dates_itr := g_primary_leaver_dates.COUNT+1;
15373       g_primary_leaver_dates(l_leaver_dates_itr).start_date     := g_ext_asg_details(p_assignment_id).start_date;
15374       g_primary_leaver_dates(l_leaver_dates_itr).leaver_date    := g_ext_asg_details(p_assignment_id).leaver_date;
15375       g_primary_leaver_dates(l_leaver_dates_itr).assignment_id  := p_assignment_id;
15376 
15377       IF g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL THEN
15378 
15379         debug('Restarter date found',30);
15380 
15381         l_new_event_itr := g_asg_events.COUNT+1;
15382         g_asg_events(l_new_event_itr).event_date        := g_ext_asg_details(p_assignment_id).restarter_date;
15383         g_asg_events(l_new_event_itr).event_type        := l_prefix||'RESTARTER';
15384         g_asg_events(l_new_event_itr).assignment_id     := p_assignment_id;
15385 
15386         -- Bugfix 3880543:REHIRE : Now also changing pt asg count
15387         --   If a leaver is a restarter then
15388         -- we also need to increment the g_part_time_asg_count
15389         -- when this event is processed
15390         l_restarter_ext_emp_cat_cd := NULL;
15391         l_restarter_ext_emp_cat_cd := get_ext_emp_cat_cd
15392                                         (p_assignment_id  => p_assignment_id
15393                                         ,p_effective_date => g_ext_asg_details(p_assignment_id).restarter_date
15394                                         );
15395         IF l_restarter_ext_emp_cat_cd = 'P' THEN
15396           g_asg_events(l_new_event_itr).pt_asg_count_change := 1;
15397         END IF;
15398 
15399         -- Bugfix 3803760:TERMASG
15400         g_asg_events(l_new_event_itr).asg_count_change  := 1;
15401 
15402         l_restarter_date := g_ext_asg_details(p_assignment_id).restarter_date;
15403 
15404         -- Store this restarter date in the global collection for primary assignments
15405         g_primary_leaver_dates(l_leaver_dates_itr).restarter_date := l_restarter_date;
15406 
15407         -- Step 2) Look for more leaver and restarter dates in a loop and store them
15408         --              If there is no leaver date or no restarter date
15409         --              then don't do this step.
15410         debug('B4 loop, # of events in global :'||to_char(g_asg_events.COUNT),40);
15411 
15412         -- LVRDATE changes
15413         -- Changed the date passed based on Annual/Periodic Criteria
15414         debug ('g_extract_type: '|| g_extract_type) ;
15415 
15416         IF g_extract_type = 'TP1' THEN
15417           l_period_end_date := g_effective_run_date;
15418         ELSIF g_extract_type = 'TP1P' THEN
15419           l_period_end_date := g_effective_run_date + 1;
15420         END IF ;
15421 
15422         debug ('l_period_end_date: '|| l_period_end_date) ;
15423 
15424         LOOP
15425 
15426           -- Call the find leaver proc here.
15427           l_leaver := chk_is_teacher_a_leaver
15428                         (p_business_group_id            => l_business_group_id
15429                         ,p_effective_start_date         => (l_restarter_date+1)
15430                         -- LVRDATE change: changed the date passed based on Annual/Periodic Criteria
15431                         ,p_effective_end_date           => l_period_end_date
15432                         --,p_effective_end_date           => g_effective_run_date
15433                         ,p_assignment_id                => p_assignment_id
15434                         ,p_leaver_date                  => l_leaver_date -- OUT
15435                         );
15436 
15437           IF l_leaver = 'Y' THEN
15438 
15439             l_new_event_itr := g_asg_events.COUNT+1;
15440             g_asg_events(l_new_event_itr).event_date        := l_leaver_date;
15441             g_asg_events(l_new_event_itr).event_type        := l_prefix||'LEAVER';
15442             g_asg_events(l_new_event_itr).assignment_id     := p_assignment_id;
15443 
15444             -- Bugfix 3880543:REHIRE : Now also changing the pt asg count
15445             --  If a part time asg is a leaver then
15446             -- we also need to decrement the g_part_time_asg_count
15447             -- when this event is processed
15448             l_leaver_ext_emp_cat_cd := NULL;
15449             l_leaver_ext_emp_cat_cd := get_ext_emp_cat_cd
15450                                          (p_assignment_id  => p_assignment_id
15451                                          ,p_effective_date => l_leaver_date
15452                                          );
15453             IF l_leaver_ext_emp_cat_cd = 'P' THEN
15454               g_asg_events(l_new_event_itr).pt_asg_count_change := -1;
15455             END IF;
15456 
15457             -- Bugfix 3803760:TERMASG
15458             g_asg_events(l_new_event_itr).asg_count_change  := -1;
15459 
15460             -- Add this leaver date to the global collection for primary assignments only
15461             l_leaver_dates_itr := g_primary_leaver_dates.COUNT+1;
15462             g_primary_leaver_dates(l_leaver_dates_itr).start_date     :=
15463                 g_primary_leaver_dates((l_leaver_dates_itr-1)).restarter_date;
15464             g_primary_leaver_dates(l_leaver_dates_itr).leaver_date    := l_leaver_date;
15465             g_primary_leaver_dates(l_leaver_dates_itr).assignment_id  := p_assignment_id;
15466 
15467             -- Find a restarter date
15468             l_restarter := chk_is_leaver_a_restarter
15469                                 (p_business_group_id    => l_business_group_id
15470                                 ,p_effective_start_date => (l_leaver_date + 1)
15471                                 ,p_effective_end_date   => g_effective_run_date
15472                                 ,p_assignment_id        => p_assignment_id
15473                                 ,p_restarter_date       => l_restarter_date -- OUT
15474                                 );
15475 
15476             IF l_restarter = 'Y' THEN
15477               l_new_event_itr := g_asg_events.COUNT+1;
15478               g_asg_events(l_new_event_itr).event_date    := l_restarter_date;
15479               g_asg_events(l_new_event_itr).event_type    := l_prefix||'RESTARTER';
15480               g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
15481 
15482               -- Bugfix 3880543:REHIRE : Now also changing the pt asg count
15483               --   If a leaver is a restarter then
15484               -- we also need to increment the g_part_time_asg_count
15485               -- when this event is processed
15486               l_restarter_ext_emp_cat_cd := NULL;
15487               l_restarter_ext_emp_cat_cd := get_ext_emp_cat_cd
15488                                           (p_assignment_id  => p_assignment_id
15489                                           ,p_effective_date => l_restarter_date
15490                                           );
15491               IF l_restarter_ext_emp_cat_cd = 'P' THEN
15492                 g_asg_events(l_new_event_itr).pt_asg_count_change := 1;
15493               END IF;
15494 
15495               -- Bugfix 3803760:TERMASG
15496               g_asg_events(l_new_event_itr).asg_count_change  := 1;
15497 
15498               -- Store this restarter date in the global collection for primary assignments
15499               g_primary_leaver_dates(l_leaver_dates_itr).restarter_date := l_restarter_date;
15500 
15501             ELSE -- Restarter not found, exit here
15502               EXIT;
15503             END IF; -- l_restarter = 'Y' THEN
15504             --
15505           ELSE -- Leaver event not found, exit here
15506             EXIT;
15507           END IF; -- l_leaver = 'Y' THEN
15508           -- Reset leaver and restarter flags
15509           l_leaver := 'N';
15510           l_restarter := 'N';
15511           --
15512         END LOOP;
15513         debug('After loop, # of events in global :'||to_char(g_asg_events.COUNT),50);
15514         --
15515       END IF; -- g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL THEN
15516       --
15517     END IF; -- g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL THEN
15518     --
15519   ELSE -- p_assignment_id = g_primary_assignment_id THEN
15520 
15521     -- This update was done in previous version priory to 06/07/2004
15522     -- IMPORTANT CHANGE : Currently we do not store leaver/restarter events
15523     -- for a secondary asg as we are not generating new lines of service for these
15524     -- events. We only need these leaver/restarter events for deciding the
15525     -- reporting windows of the secodary asg. and then finding out the other
15526     -- new line of service events in these windows
15527 
15528     -- Bugfix 3734942 : IMPORTANT update as of 06/07/2004
15529     --  The above IMPORTANT CHANGE statement is not valid anymore,
15530     --  we are now storing secondary leaver and restarter dates
15531     --  as new LOS events. TPA keeps confusing us every time this
15532     --  issue is raised with them.
15533 
15534     l_leaver := 'N';
15535     l_restarter := 'N';
15536     l_prefix := 'SECONDARY_';
15537     l_continue := 'Y';
15538 
15539     debug('Processing Secondary Assignment',60);
15540 
15541     l_leaver_date := g_ext_asg_details(p_assignment_id).leaver_date;
15542 
15543     -- Store all valid leaver dates
15544     IF g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL THEN
15545 -- Bugfix 3880543:REHIRE : Remmoved restriction on secondary leaver restarter
15546 --   dates. They dont hv to be in primary range now
15547 --       AND
15548 --       date_in_prmy_range
15549 --         (p_date => l_leaver_date -- IN OUT
15550 --         ,p_date_type => 'L'
15551 --         ) = 'Y' THEN
15552 
15553       debug('Leaver Date found',70);
15554       -- Secondary asg is a leaver, store this new LOS event as leaver date +1
15555       l_new_event_itr := g_asg_events.COUNT+1;
15556 
15557       -- Bugfix 3880543:REHIRE : now storing leaver date itself insted of leaver
15558       --  date + 1
15559       g_asg_events(l_new_event_itr).event_date    := l_leaver_date;
15560       g_asg_events(l_new_event_itr).event_type    := l_prefix||'LEAVER';
15561       g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
15562 
15563       -- Bugfix 3734942 : If a part time asg is a leaver then
15564       -- we also need to decrement the g_part_time_asg_count
15565       -- when this event is processed
15566       l_leaver_ext_emp_cat_cd := NULL;
15567       l_leaver_ext_emp_cat_cd := get_ext_emp_cat_cd
15568                                    (p_assignment_id  => p_assignment_id
15569                                    ,p_effective_date => l_leaver_date
15570                                    );
15571       IF l_leaver_ext_emp_cat_cd = 'P' THEN
15572         g_asg_events(l_new_event_itr).pt_asg_count_change := -1;
15573       END IF;
15574 
15575       /* IF g_ext_asg_details(p_assignment_id).ext_emp_cat_cd = 'P' THEN
15576         g_asg_events(l_new_event_itr).pt_asg_count_change := -1;
15577       END IF; */
15578 
15579       -- Bugfix 3803760:TERMASG
15580       g_asg_events(l_new_event_itr).asg_count_change  := -1;
15581 
15582       -- Add this leaver date to the global collection for secondary assignments only
15583       l_leaver_dates_itr := g_sec_leaver_dates.COUNT+1;
15584       g_sec_leaver_dates(l_leaver_dates_itr).start_date     := g_ext_asg_details(p_assignment_id).start_date;
15585       g_sec_leaver_dates(l_leaver_dates_itr).leaver_date    := l_leaver_date;
15586       g_sec_leaver_dates(l_leaver_dates_itr).assignment_id  := p_assignment_id;
15587 
15588       l_restarter_date := g_ext_asg_details(p_assignment_id).restarter_date;
15589 
15590       -- Check for restarter date
15591       IF g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL THEN
15592 
15593 -- Bugfix 3880543:REHIRE : Remmoved restriction on secondary leaver restarter
15594 --   dates. They dont hv to be in primary range now
15595 --         AND
15596 --         date_in_prmy_range
15597 --             (p_date => l_restarter_date -- IN OUT
15598 --             ,p_date_type => 'R'
15599 --             ) = 'Y' THEN
15600 
15601         debug('Restarter Date found',80);
15602 
15603         -- Secondary asg is a restarter, store this as an event
15604         l_new_event_itr := g_asg_events.COUNT+1;
15605 
15606         g_asg_events(l_new_event_itr).event_date    := l_restarter_date;
15607         g_asg_events(l_new_event_itr).event_type    := l_prefix||'RESTARTER';
15608         g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
15609 
15610         -- Bugfix 3734942 : If a leaver is a restarter then
15611         -- we also need to increment the g_part_time_asg_count
15612         -- when this event is processed
15613         l_restarter_ext_emp_cat_cd := NULL;
15614         l_restarter_ext_emp_cat_cd := get_ext_emp_cat_cd
15615                                         (p_assignment_id  => p_assignment_id
15616                                         ,p_effective_date => l_restarter_date
15617                                         );
15618         IF l_restarter_ext_emp_cat_cd = 'P' THEN
15619           g_asg_events(l_new_event_itr).pt_asg_count_change := 1;
15620         END IF;
15621 
15622         -- Bugfix 3803760:TERMASG
15623         g_asg_events(l_new_event_itr).asg_count_change  := 1;
15624 
15625         -- Store this restarter date in the global collection for primary assignments
15626         g_sec_leaver_dates(l_leaver_dates_itr).restarter_date := l_restarter_date;
15627 
15628       ELSE -- g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL
15629 
15630         -- Find the restarter date between leaver date and end of pension year.
15631         -- If not found till end of pension year, then we don't
15632         -- want to look for any more leaver / restarter dates for this secondary asg
15633         l_restarter := chk_is_leaver_a_restarter
15634                         (p_business_group_id    => l_business_group_id
15635                         ,p_effective_start_date => (l_leaver_date + 1)
15636                         ,p_effective_end_date   => g_effective_run_date
15637                         ,p_assignment_id        => p_assignment_id
15638                         ,p_restarter_date       => l_restarter_date -- OUT
15639                         );
15640 
15641         IF l_restarter = 'Y' THEN
15642 -- Bugfix 3880543:REHIRE : Remmoved restriction on secondary leaver restarter
15643 --   dates. They dont hv to be in primary range now
15644 --           AND
15645 --           date_in_prmy_range
15646 --               (p_date => l_restarter_date -- IN OUT
15647 --               ,p_date_type => 'R'
15648 --               ) = 'Y' THEN
15649 
15650           debug('First restarter date found',90);
15651 
15652           -- Secondary asg is a restarter, store this as an event
15653           l_new_event_itr := g_asg_events.COUNT+1;
15654 
15655           g_asg_events(l_new_event_itr).event_date    := l_restarter_date;
15656           g_asg_events(l_new_event_itr).event_type    := l_prefix||'RESTARTER';
15657           g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
15658 
15659           -- Bugfix 3734942 : If a leaver is a restarter then
15660           -- we also need to increment the g_part_time_asg_count
15661           -- when this event is processed
15662           l_restarter_ext_emp_cat_cd := NULL;
15663           l_restarter_ext_emp_cat_cd := get_ext_emp_cat_cd
15664                                         (p_assignment_id  => p_assignment_id
15665                                         ,p_effective_date => l_restarter_date
15666                                         );
15667           IF l_restarter_ext_emp_cat_cd = 'P' THEN
15668             g_asg_events(l_new_event_itr).pt_asg_count_change := 1;
15669           END IF;
15670 
15671           -- Bugfix 3803760:TERMASG
15672           g_asg_events(l_new_event_itr).asg_count_change  := 1;
15673 
15674           -- Store this restarter date in the global collection for primary assignments
15675           g_sec_leaver_dates(l_leaver_dates_itr).restarter_date := l_restarter_date;
15676 
15677         ELSE
15678 
15679           -- Don't look for any further leaver/restarter dates in this pension year
15680           l_continue := 'N';
15681 
15682         END IF;
15683 
15684 
15685       END IF; -- g_ext_asg_details(p_assignment_id).restarter_date IS NOT NULL
15686       --
15687     -- Bugfix 3641851:CBF3b : Moved this END IF down to encompass
15688     --    the entire leaver and restarter chk logic for secondary
15689     --    assignment. We should only chk for further leaver and
15690     --    restarter events if the asg was identified as a
15691     --    leaver by criteria in the first place, otherwise no need.
15692     -- END IF; -- g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL THEN
15693 
15694       -- Set restarter date to leaver date so we can find leaver events between this date
15695       -- and end of pension year
15696 
15697       -- Bugfix 3641851:CBF3b : Commented out as setting restarter date is not
15698       --    needed any more coz it is set above and now v r only chking for
15699       --    further leaver and restarter dates only if the asg was identified
15700       --    as a leaver by the criteria in the first place, not otherwise.
15701 /*      l_restarter_date := nvl(l_leaver_date
15702                              ,nvl(g_ext_asg_details(g_primary_assignment_id).restarter_date
15703                                  ,g_pension_year_start_date
15704                                  )
15705                              );
15706 */
15707       debug('Restarter Date :'||to_char(l_restarter_date, 'DD/MM/YYYY'), 85);
15708 
15709       IF (l_restarter_date + 2) >= g_pension_year_end_date THEN
15710         l_continue := 'N';
15711         debug('Setting l_continue to N',90);
15712       END IF;
15713 
15714       -- LVRDATE changes
15715       -- Changed the date passed based on Annual/Periodic Criteria
15716       debug ('g_extract_type: '|| g_extract_type) ;
15717 
15718       IF g_extract_type = 'TP1' THEN
15719         l_period_end_date := g_effective_run_date;
15720       ELSIF g_extract_type = 'TP1P' THEN
15721         l_period_end_date := g_effective_run_date + 1;
15722       END IF ;
15723 
15724       debug ('l_period_end_date: '|| l_period_end_date) ;
15725 
15726       -- Look for mor leaver events
15727       WHILE l_continue = 'Y'
15728       LOOP
15729 
15730         debug('Start of Loop',100);
15731         -- Call the find leaver proc here.
15732         l_leaver := chk_is_teacher_a_leaver
15733                           (p_business_group_id            => l_business_group_id
15734                           ,p_effective_start_date         => (l_restarter_date + 1)
15735                           -- LVRDATE change: changed the date passed based on Annual/Periodic Criteria
15736                           ,p_effective_end_date           => l_period_end_date
15737                           --,p_effective_end_date           => g_effective_run_date
15738                           ,p_assignment_id                => p_assignment_id
15739                           ,p_leaver_date                  => l_leaver_date -- OUT
15740                           );
15741 
15742         IF l_leaver = 'Y' THEN
15743 -- Bugfix 3880543:REHIRE : Remmoved restriction on secondary leaver restarter
15744 --   dates. They dont hv to be in primary range now
15745 --           AND
15746 --           date_in_prmy_range
15747 --             (p_date => l_leaver_date -- IN OUT
15748 --             ,p_date_type => 'L'
15749 --             ) = 'Y' THEN
15750 
15751           -- Secondary asg is a leaver, store this new LOS event as leaver date +1
15752           l_new_event_itr := g_asg_events.COUNT+1;
15753 
15754           -- Bugfix 3880543:REHIRE : now storing leaver date itself insted of leaver
15755           --  date + 1
15756           g_asg_events(l_new_event_itr).event_date    := l_leaver_date;
15757           g_asg_events(l_new_event_itr).event_type    := l_prefix||'LEAVER';
15758           g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
15759 
15760           -- Bugfix 3734942 : If a part time asg is a leaver then
15761           -- we also need to decrement the g_part_time_asg_count
15762           -- when this event is processed
15763           l_leaver_ext_emp_cat_cd := NULL;
15764           l_leaver_ext_emp_cat_cd := get_ext_emp_cat_cd
15765                                        (p_assignment_id  => p_assignment_id
15766                                        ,p_effective_date => l_leaver_date
15767                                        );
15768           IF l_leaver_ext_emp_cat_cd = 'P' THEN
15769             g_asg_events(l_new_event_itr).pt_asg_count_change := -1;
15770           END IF;
15771 
15772           -- Bugfix 3803760:TERMASG
15773           g_asg_events(l_new_event_itr).asg_count_change  := -1;
15774 
15775           -- Add this leaver date to the global collection for secondary assignments only
15776           l_leaver_dates_itr := g_sec_leaver_dates.COUNT+1;
15777 
15778           IF l_leaver_dates_itr = 1 THEN
15779             g_sec_leaver_dates(l_leaver_dates_itr).start_date :=
15780                   g_ext_asg_details(p_assignment_id).start_date;
15781           ELSE
15782             g_sec_leaver_dates(l_leaver_dates_itr).start_date :=
15783                   g_sec_leaver_dates(l_leaver_dates_itr-1).restarter_date;
15784           END IF;
15785           --
15786           g_sec_leaver_dates(l_leaver_dates_itr).leaver_date    := l_leaver_date;
15787           g_sec_leaver_dates(l_leaver_dates_itr).assignment_id  := p_assignment_id;
15788 
15789           -- Find the restarter date. If not found till end of pension year,
15790           -- then we don't stop looking
15791           l_restarter := chk_is_leaver_a_restarter
15792                           (p_business_group_id    => l_business_group_id
15793                           ,p_effective_start_date => (l_leaver_date + 1)
15794                           ,p_effective_end_date   => g_effective_run_date
15795                           ,p_assignment_id        => p_assignment_id
15796                           ,p_restarter_date       => l_restarter_date -- OUT
15797                           );
15798 
15799           IF l_restarter = 'Y' THEN
15800 -- Bugfix 3880543:REHIRE : Remmoved restriction on secondary leaver restarter
15801 --   dates. They dont hv to be in primary range now
15802 --             AND
15803 --             date_in_prmy_range
15804 --                 (p_date => l_restarter_date -- IN OUT
15805 --                 ,p_date_type => 'R'
15806 --                 ) = 'Y' THEN
15807 
15808             -- Secondary asg is a restarter, store this as an event
15809             l_new_event_itr := g_asg_events.COUNT+1;
15810 
15811             g_asg_events(l_new_event_itr).event_date    := l_restarter_date;
15812             g_asg_events(l_new_event_itr).event_type    := l_prefix||'RESTARTER';
15813             g_asg_events(l_new_event_itr).assignment_id := p_assignment_id;
15814 
15815             -- Bugfix 3734942 : If a leaver is a restarter then
15816             -- we also need to increment the g_part_time_asg_count
15817             -- when this event is processed
15818             l_restarter_ext_emp_cat_cd := NULL;
15819             l_restarter_ext_emp_cat_cd := get_ext_emp_cat_cd
15820                                         (p_assignment_id  => p_assignment_id
15821                                         ,p_effective_date => l_restarter_date
15822                                         );
15823             IF l_restarter_ext_emp_cat_cd = 'P' THEN
15824               g_asg_events(l_new_event_itr).pt_asg_count_change := 1;
15825             END IF;
15826 
15827             -- Bugfix 3803760:TERMASG
15828             g_asg_events(l_new_event_itr).asg_count_change  := 1;
15829 
15830             -- Store this restarter date in the global collection for primary assignments
15831             g_sec_leaver_dates(l_leaver_dates_itr).restarter_date := l_restarter_date;
15832 
15833           ELSE
15834 
15835             -- Don't look for any further leaver/restarter dates in this pension year
15836             l_continue := 'N';
15837             EXIT;
15838 
15839           END IF;
15840           --
15841         ELSE -- not a leaver, exit now
15842 
15843           l_continue := 'N';
15844           EXIT;
15845 
15846         END IF;
15847         --
15848       END LOOP;
15849       --
15850 
15851       -- Bugfix 3641851:CBF3b : Moved this END IF down to encompass
15852       --    the entire leaver and restarter chk logic for secondary
15853       --    assignment. We should only chk for further leaver and
15854       --    restarter events if the asg was identified as a
15855       --    leaver by criteria in the first place, otherwise no need.
15856     END IF; -- g_ext_asg_details(p_assignment_id).leaver_date IS NOT NULL THEN
15857     --
15858   END IF; -- p_assignment_id = g_primary_assignment_id THEN
15859 
15860   debug_exit(l_proc_name);
15861 
15862   RETURN;
15863 
15864 EXCEPTION
15865     WHEN OTHERS THEN
15866       debug_exit(' Others in '||l_proc_name);
15867       RAISE;
15868 END; -- store_leaver_restarter_dates
15869 
15870 --
15871 -- Store events for this asg in the PRIMARY asg's validity range
15872 --
15873 PROCEDURE get_events_in_prmy_range
15874   (p_assignment_id            IN      NUMBER  -- context
15875   )
15876 IS
15877 
15878   -- Variable Declaration
15879   -- Bugfix 3873376:ESTB
15880   l_counter            NUMBER := 0 ;
15881 
15882   l_proc_name          VARCHAR2(61):=
15883      g_proc_name||'get_events_in_prmy_range';
15884 
15885 BEGIN -- get_events_in_prmy_range
15886 
15887   debug_enter(l_proc_name);
15888 
15889   -- If there are leaver restarter events then find events during valid period
15890   IF g_primary_leaver_dates.COUNT > 0 THEN
15891 
15892     debug('Inside If, event count more than zero',20);
15893 
15894     FOR l_itr IN g_primary_leaver_dates.FIRST..g_primary_leaver_dates.LAST
15895     LOOP
15896 
15897       l_counter := l_counter + 1 ;
15898       debug('Inside Loop, calling get_asg_events, l_counter: '||l_counter,30);
15899 
15900       -- Bugfix 3873376:ESTB
15901       IF (l_counter = 1 ) THEN  --for first period.
15902 
15903         -- BUGFIX 2414035 : Changed p_end_date to leaver date - 1
15904         -- This is needed coz otherwise the Elected Pension flag
15905         -- change event gets picked up again, this time as new
15906         -- line of service event
15907         get_asg_events(p_assignment_id    => p_assignment_id
15908                       ,p_start_date       => g_primary_leaver_dates(l_itr).start_date + 1
15909                       ,p_end_date         => g_primary_leaver_dates(l_itr).leaver_date - 1
15910                       );
15911       ELSE  --(l_counter = 1 ) --for other periods.
15912         -- check from the start date to leaver date -1
15913         get_asg_events(p_assignment_id    => p_assignment_id
15914                       ,p_start_date       => g_primary_leaver_dates(l_itr).start_date - 1
15915                       ,p_end_date         => g_primary_leaver_dates(l_itr).leaver_date - 1
15916                       );
15917       END IF ; --(l_counter = 1 )
15918 
15919       IF g_primary_leaver_dates.NEXT(l_itr) IS NULL
15920          AND
15921          g_primary_leaver_dates(l_itr).restarter_date IS NOT NULL THEN
15922 
15923         debug('Inside Loop and If, calling get_asg_events last time',40);
15924         -- Get the events between the restarter date and end of period
15925         get_asg_events(p_assignment_id  => p_assignment_id -- primary assignment
15926                       -- Bugfix 3873376:ESTB
15927                       --,p_start_date     => g_primary_leaver_dates(l_itr).restarter_date + 1
15928                       --changed from restarter_date +1 to restarter_date
15929                       ,p_start_date     => g_primary_leaver_dates(l_itr).restarter_date - 1
15930                       ,p_end_date       => g_effective_run_date
15931                       );
15932       END IF;
15933 
15934     END LOOP;
15935 
15936   ELSE -- No leaver restarter events found, so find new line events for the entire year
15937 
15938       debug('No Leaver events, calling get_asg_events for pension year',50);
15939       get_asg_events(p_assignment_id    => p_assignment_id
15940                     ,p_start_date       => GREATEST(g_pension_year_start_date
15941                                                    ,g_ext_asg_details(p_assignment_id).start_date
15942                                                    ) + 1 -- find events starting from next day
15943                     ,p_end_date         => g_effective_run_date
15944                     );
15945 
15946   END IF; -- g_primary_leaver_dates.COUNT > 0 THEN
15947 
15948   debug_exit(l_proc_name);
15949 
15950   RETURN;
15951 
15952 EXCEPTION
15953     WHEN OTHERS THEN
15954       debug_exit(' Others in '||l_proc_name);
15955       RAISE;
15956 END; -- get_events_in_prmy_range
15957 
15958 --
15959 -- Store events for this asg in the SECONDARY asg's validity range
15960 --
15961 PROCEDURE get_events_in_sec_range
15962   (p_assignment_id            IN      NUMBER  -- context
15963   )
15964 IS
15965 
15966   -- Variable Declaration
15967   -- Bugfix 3873376:ESTB
15968   l_counter            NUMBER := 0 ;
15969 
15970   l_proc_name          VARCHAR2(61):=
15971      g_proc_name||'get_events_in_sec_range';
15972 
15973 BEGIN -- get_events_in_sec_range
15974 
15975   debug_enter(l_proc_name);
15976 
15977   -- If there are leaver restarter events then find events during valid period
15978   IF g_sec_leaver_dates.COUNT > 0 THEN
15979 
15980     FOR l_itr IN g_sec_leaver_dates.FIRST..g_sec_leaver_dates.LAST
15981     LOOP
15982 
15983       l_counter := l_counter + 1;
15984       debug('Start of Loop, calling get_asg_events, l_counter: '||l_counter,20);
15985 
15986 
15987       -- Step 1c) Get valid events for each secondary assignment
15988 
15989       -- Bugfix 3873376:ESTB
15990       IF (l_counter = 1) THEN  --for the first period
15991 
15992         -- BUGFIX 2414035 : Changed p_end_date to leaver date - 1
15993         -- This is needed coz otherwise the Elected Pension flag
15994         -- change event gets picked up again, this time as new
15995         -- line of service event
15996         get_asg_events(p_assignment_id    => p_assignment_id
15997                       ,p_start_date       => g_sec_leaver_dates(l_itr).start_date + 1
15998                       ,p_end_date         => g_sec_leaver_dates(l_itr).leaver_date - 1
15999                       );
16000       ELSE --(l_counter = 1) --for the next periods
16001          get_asg_events(p_assignment_id    => p_assignment_id
16002                        ,p_start_date       => g_sec_leaver_dates(l_itr).start_date - 1
16003                        ,p_end_date         => g_sec_leaver_dates(l_itr).leaver_date - 1
16004                         );
16005       END IF ; --(l_counter = 1)
16006 
16007       IF g_sec_leaver_dates.NEXT(l_itr) IS NULL
16008          AND
16009          g_sec_leaver_dates(l_itr).restarter_date IS NOT NULL THEN
16010 
16011         debug('Inside LOOP and IF, calling get_asg_events for last time',30);
16012         -- Get the events between the restarter date and end of period
16013         get_asg_events(p_assignment_id  => p_assignment_id
16014                       -- Bugfix 3873376:ESTB
16015                       --changed from restarter_date +1 to restarter_date
16016                       --,p_start_date     => g_sec_leaver_dates(l_itr).restarter_date + 1
16017                       ,p_start_date     => g_sec_leaver_dates(l_itr).restarter_date - 1
16018                       ,p_end_date       => g_effective_run_date
16019                       );
16020 
16021       END IF;
16022       --
16023     END LOOP;
16024 
16025   ELSE -- No leaver restarter events found
16026 
16027     debug('No Leaver Restarter events found, getting events in secondary range',40);
16028     -- IMP: Find leaver restarter events for the SECONDARY asg
16029     --  between the PRIMARY asg's validity period as the secondary
16030     --  asg does not have any leaver/restarter dates.
16031     --  PS : we do this coz we only report events of the secondary
16032     --       asg in the validity period of the primary asg.
16033 
16034     -- Bugfix 3641851:CBF3b : Commented out call to get_events_in_prmy_range
16035     --   This results in sec asg events even when sec asg has not bcome
16036     --   a teacher. Only get events for sec asgs validity range.
16037     --   And since there are no leaver and restarter events, the
16038     --   sec asg is valid from its start date to end of period.
16039     --get_events_in_prmy_range(p_assignment_id => p_assignment_id
16040     --                        );
16041 
16042     get_asg_events(p_assignment_id  => p_assignment_id
16043                   ,p_start_date     => GREATEST(g_pension_year_start_date
16044                                                ,g_ext_asg_details(p_assignment_id).start_date
16045                                                ) + 1 -- find events starting from next day
16046                   ,p_end_date       => g_effective_run_date
16047                   );
16048 
16049 
16050   END IF; -- g_sec_leaver_dates.COUNT > 0 THEN
16051 
16052   debug_exit(l_proc_name);
16053 
16054   RETURN;
16055 
16056 EXCEPTION
16057     WHEN OTHERS THEN
16058       debug_exit(' Others in '||l_proc_name);
16059       RAISE;
16060 END; -- get_events_in_sec_range
16061 
16062 
16063 PROCEDURE copy_rec_val(p_cur_rec IN csr_rslt_dtl_sort%ROWTYPE
16064                       ,p_prev_rec IN OUT NOCOPY csr_rslt_dtl_sort%ROWTYPE)
16065 IS
16066 
16067 BEGIN
16068   p_prev_rec.val_01 := p_cur_rec.val_01;
16069   p_prev_rec.val_02 := p_cur_rec.val_02;
16070   p_prev_rec.val_03 := p_cur_rec.val_03;
16071   p_prev_rec.val_04 := p_cur_rec.val_04;
16072   p_prev_rec.val_05 := p_cur_rec.val_05;
16073   p_prev_rec.val_06 := p_cur_rec.val_06;
16074   p_prev_rec.val_07 := p_cur_rec.val_07;
16075   p_prev_rec.val_08 := p_cur_rec.val_08;
16076   p_prev_rec.val_09 := p_cur_rec.val_09;
16077   p_prev_rec.val_10 := p_cur_rec.val_10;
16078   p_prev_rec.val_11 := p_cur_rec.val_11;
16079   p_prev_rec.val_12 := p_cur_rec.val_12;
16080   p_prev_rec.val_13 := p_cur_rec.val_13;
16081   p_prev_rec.val_14 := p_cur_rec.val_14;
16082   p_prev_rec.val_15 := p_cur_rec.val_15;
16083   p_prev_rec.val_16 := p_cur_rec.val_16;
16084   p_prev_rec.val_17 := p_cur_rec.val_17;
16085   p_prev_rec.val_18 := p_cur_rec.val_18;
16086   p_prev_rec.val_19 := p_cur_rec.val_19;
16087   p_prev_rec.val_20 := p_cur_rec.val_20;
16088   p_prev_rec.val_21 := p_cur_rec.val_21;
16089   p_prev_rec.val_22 := p_cur_rec.val_22;
16090   p_prev_rec.val_23 := p_cur_rec.val_23;
16091   p_prev_rec.val_24 := p_cur_rec.val_24;
16092   p_prev_rec.val_25 := p_cur_rec.val_25;
16093   --p_prev_rec.val_26 := p_cur_rec.val_26;
16094   p_prev_rec.val_27 := p_cur_rec.val_27;
16095   p_prev_rec.val_28 := p_cur_rec.val_28;
16096   p_prev_rec.val_29 := p_cur_rec.val_29;
16097   p_prev_rec.val_30 := p_cur_rec.val_30;
16098   p_prev_rec.val_31 := p_cur_rec.val_31;
16099   p_prev_rec.val_32 := p_cur_rec.val_32;
16100   p_prev_rec.val_33 := p_cur_rec.val_33;
16101   p_prev_rec.val_34 := p_cur_rec.val_34;
16102   p_prev_rec.val_35 := p_cur_rec.val_35;
16103   p_prev_rec.val_36 := p_cur_rec.val_36;
16104   p_prev_rec.val_37 := p_cur_rec.val_37;
16105   p_prev_rec.val_38 := p_cur_rec.val_38;
16106   p_prev_rec.val_39 := p_cur_rec.val_39;
16107   p_prev_rec.val_40 := p_cur_rec.val_40;
16108   p_prev_rec.val_41 := p_cur_rec.val_41;
16109   p_prev_rec.val_42 := p_cur_rec.val_42;
16110   p_prev_rec.val_43 := p_cur_rec.val_43;
16111   p_prev_rec.val_44 := p_cur_rec.val_44;
16112   p_prev_rec.val_45 := p_cur_rec.val_45;
16113   p_prev_rec.val_46 := p_cur_rec.val_46;
16114   p_prev_rec.val_47 := p_cur_rec.val_47;
16115   p_prev_rec.val_48 := p_cur_rec.val_48;
16116   p_prev_rec.val_49 := p_cur_rec.val_49;
16117   p_prev_rec.val_50 := p_cur_rec.val_50;
16118   p_prev_rec.val_51 := p_cur_rec.val_51;
16119   p_prev_rec.val_52 := p_cur_rec.val_52;
16120   p_prev_rec.val_53 := p_cur_rec.val_53;
16121   p_prev_rec.val_54 := p_cur_rec.val_54;
16122   p_prev_rec.val_55 := p_cur_rec.val_55;
16123   p_prev_rec.val_56 := p_cur_rec.val_56;
16124   p_prev_rec.val_57 := p_cur_rec.val_57;
16125   p_prev_rec.val_58 := p_cur_rec.val_58;
16126   p_prev_rec.val_59 := p_cur_rec.val_59;
16127   p_prev_rec.val_60 := p_cur_rec.val_60;
16128   p_prev_rec.val_61 := p_cur_rec.val_61;
16129   p_prev_rec.val_62 := p_cur_rec.val_62;
16130   p_prev_rec.val_63 := p_cur_rec.val_63;
16131   p_prev_rec.val_64 := p_cur_rec.val_64;
16132   p_prev_rec.val_65 := p_cur_rec.val_65;
16133   p_prev_rec.val_66 := p_cur_rec.val_66;
16134   p_prev_rec.val_67 := p_cur_rec.val_67;
16135   p_prev_rec.val_68 := p_cur_rec.val_68;
16136   p_prev_rec.val_69 := p_cur_rec.val_69;
16137   p_prev_rec.val_70 := p_cur_rec.val_70;
16138   p_prev_rec.val_71 := p_cur_rec.val_71;
16139   p_prev_rec.val_72 := p_cur_rec.val_72;
16140   p_prev_rec.val_73 := p_cur_rec.val_73;
16141   p_prev_rec.val_74 := p_cur_rec.val_74;
16142   p_prev_rec.val_75 := p_cur_rec.val_75;
16143 
16144 
16145 END; -- copy_rec_val
16146 
16147 --
16148 -- create_new_lines
16149 --
16150 PROCEDURE create_new_lines
16151                 (p_assignment_id IN per_all_assignments_f.assignment_id%TYPE
16152                 )
16153 IS
16154 
16155   -- Variable Declaration
16156   l_rec_serial_num      NUMBER(3);
16157   l_itr                 NUMBER(3);
16158   l_next_itr            NUMBER(3);
16159   l_skip_itr            NUMBER(3) := NULL;
16160   l_next_of_next_itr    NUMBER(3);
16161   l_prev_asg_count      NUMBER(3);
16162   l_check_new_los       VARCHAR2(1) := '1';
16163 
16164 
16165   -- Rowtype Variable Declaration
16166   l_main_rec            csr_rslt_dtl%ROWTYPE;
16167   l_new_rec             csr_rslt_dtl%ROWTYPE;
16168   l_prev_new_rec        csr_rslt_dtl%ROWTYPE;
16169   l_event_details       stored_events_type;
16170 
16171 
16172   l_proc_name          VARCHAR2(61):=
16173      g_proc_name||'create_new_lines';
16174 
16175   l_mark_for_delete     BOOLEAN := FALSE;
16176   l_prev_rec            csr_rslt_dtl_sort%ROWTYPE;
16177 
16178 
16179 BEGIN -- create_new_lines
16180 
16181   debug_enter(l_proc_name);
16182 
16183   debug('ben_ext_thread.g_ext_rslt_id :'||to_char(ben_ext_thread.g_ext_rslt_id),10);
16184   debug('g_ext_dtl_rcd_id :'||to_char(g_ext_dtl_rcd_id),20);
16185   debug('person id :'||to_char(g_ext_asg_details(p_assignment_id).person_id),30);
16186   debug('assignment id :'||to_char(p_assignment_id),40);
16187 
16188   -- Get the main detail record
16189   OPEN csr_rslt_dtl
16190               (p_person_id    => g_ext_asg_details(p_assignment_id).person_id
16191               ,p_ext_rslt_id  => ben_ext_thread.g_ext_rslt_id
16192               );
16193   FETCH csr_rslt_dtl INTO l_main_rec;
16194   IF csr_rslt_dtl%NOTFOUND THEN
16195     debug('No Data Found in csr_rslt_dtl', 50);
16196   END IF;
16197   CLOSE csr_rslt_dtl;
16198 
16199   debug('Original Start Date :'||l_main_rec.val_13,51);
16200   debug('Original End Date   :'||l_main_rec.val_14,52);
16201 
16202   --The following bugfix(sort order only) has been undone. The reason being:
16203   --DE PQP GB TP Type 1 Record Serial Number - Detail Teachers Annual Returns ( England and Wales ) has a
16204   --maximum length of 1 character. So the changes done in 115.99 are being reverted here.
16205 
16206      -- **************************
16207         -- Set the sort order. Seq 26 for record serial number.
16208         -- bug fix : 4926143
16209         -- this is being done as this column in the table is of varchar2 type, due to which
16210         -- sorting using order_by was incorrect when values are like 1,2,3,4....,9,10,11,12.
16211         -- For example, 1,2..,9,10,11,12 gets sorted as 1,10,11,12,2,3...,9.
16212         -- To fix this issue, the value in the thrd_sort_val is being stored as 3 digit numbers,
16213         -- starting from 001, 002 and so on.
16214 
16215         -- l_main_rec.thrd_sort_val := ltrim(rtrim(to_char(to_number(l_main_rec.val_26),'009')));
16216      -- **************************
16217 
16218   -- reintroduced from version 115.98
16219   l_main_rec.thrd_sort_val := l_main_rec.val_26;
16220 
16221 
16222   -- Also, increment the object version number
16223   l_main_rec.object_version_number := nvl(l_main_rec.object_version_number,0) + 1;
16224 
16225   -- Assign the main record to the previous record variable
16226   -- We do this so even if there are not new line events,
16227   -- i.e. g_asg_events.COUNT = 0, if there is a restarter date,
16228   -- then we need a new line for this. Doing this assignment will
16229   -- ensure that the new restarter line copies from the latest record.
16230   l_prev_new_rec := l_main_rec;
16231 
16232   --TERM_LSP:global value set to N for checking terminated employees
16233   g_terminated_person := 'N';
16234 
16235     IF ( l_main_rec.val_17 ='000000' AND g_asg_events.COUNT = 0) THEN
16236       debug('Main Record marked for delete');
16237       l_mark_for_delete :=TRUE;
16238     END IF;
16239 
16240 
16241   -- Have we found any events?
16242   IF g_asg_events.COUNT > 0 THEN
16243 
16244     debug('Number of events to process :'||to_char(g_asg_events.COUNT), 60);
16245 
16246     -- Bugfix 3803760:TERMASG
16247     debug('Before : g_asg_count :'||to_char(g_asg_count), 62);
16248 
16249     /* Commenting out as we will do this in the loop below
16250     l_prev_asg_count := g_asg_count;
16251     g_asg_count := g_asg_count
16252                    +
16253                    nvl(g_asg_events(g_asg_events.FIRST).asg_count_change
16254                       , 0);
16255 
16256     debug('After : g_asg_count :'||to_char(g_asg_count), 63);
16257 
16258     debug('B4 g_part_time_asg_count :'||to_char(g_part_time_asg_count), 64);
16259     g_part_time_asg_count := g_part_time_asg_count
16260                              +
16261                              nvl(g_asg_events(g_asg_events.FIRST).pt_asg_count_change, 0);
16262 
16263     debug('After g_part_time_asg_count :'||to_char(g_part_time_asg_count), 65);
16264     */
16265 
16266     -- If the first event is a primary leaver event, then we need
16267     -- a W on withdrawal conf and
16268     -- the end date should be set as the event date
16269     IF (g_asg_count
16270         +
16271         nvl(g_asg_events(g_asg_events.FIRST).asg_count_change, 0)
16272        ) <= 0 THEN
16273 
16274       -- Assign end date(seq 14) to the main record
16275       l_main_rec.val_14  := to_char(g_asg_events(g_asg_events.FIRST).event_date,'DDMMYY');
16276 
16277       debug('Setting W on main record',70);
16278       l_main_rec.val_15 := 'W';
16279 
16280     ELSE
16281 
16282       -- Assign end date(seq 14) to the main record
16283       -- Bugfix 3880543:REHIRE : Set end date to event date if this
16284       --  is a leaver event otherwise event date less one
16285       IF INSTR(nvl(g_asg_events(g_asg_events.FIRST).event_type,'XX')
16286               ,'LEAVER'
16287               ) > 0 THEN
16288         l_main_rec.val_14  := to_char(g_asg_events(g_asg_events.FIRST).event_date,'DDMMYY');
16289       ELSE
16290         l_main_rec.val_14  := to_char(g_asg_events(g_asg_events.FIRST).event_date - 1,'DDMMYY');
16291       END IF;
16292 
16293       -- Clear the Withdrawal conf flag
16294       l_main_rec.val_15 := ' ';
16295 
16296     END IF;
16297 
16298 
16299 
16300     debug('g_terminated_person  val : '||g_terminated_person,74);
16301     debug('g_asg_events(g_asg_events.FIRST).event_type: '|| (g_asg_events(g_asg_events.FIRST).event_type),75 );
16302     IF INSTR(nvl(g_asg_events(g_asg_events.FIRST).event_type,'XX'),'PQP_GB_TP_ASG_STATUS') > 0 THEN
16303 
16304       g_terminated_person := 'Y';
16305 
16306       debug('Inside event check g_terminated_person  val : '||g_terminated_person);
16307     ELSE
16308      g_terminated_person := 'N';
16309     END IF;
16310 
16311     debug('After event check g_terminated_person  val : '||g_terminated_person);
16312 
16313     debug('Main Record Start Date :'||l_main_rec.val_13,80);
16314     debug('Main Record End Date   :'||l_main_rec.val_14,90);
16315 
16316     -- Now recalculate the data elements
16317     recalc_data_elements
16318           (p_dtl_rec              => l_main_rec -- IN OUT
16319           ,p_rec_type             => 'MAIN'
16320           ,p_event_details        => NULL
16321           ,p_assignment_id        => p_assignment_id
16322           );
16323 
16324     debug('Main Record Start Date :'||l_main_rec.val_13,100);
16325     debug('Main Record End Date   :'||l_main_rec.val_14,110);
16326 
16327     IF ( l_main_rec.val_17 ='000000' ) THEN
16328       debug('Main Record marked for delete');
16329       l_mark_for_delete :=TRUE;
16330     END IF;
16331 
16332     -- Update the main record
16333 --    upd_rslt_dtl(p_dtl_rec => l_main_rec);
16334 
16335     -- Assign the main record to the previous record variable
16336     l_prev_new_rec := l_main_rec;
16337     l_check_new_los := '1';
16338 
16339     -- Process each event in the global collection
16340     FOR l_itr IN g_asg_events.FIRST..g_asg_events.LAST
16341     LOOP -- through the sorted stored events
16342 
16343       debug('Start of Loop',120);
16344 
16345       -- Bugfix 3803760:TERMASG
16346       debug('B4 g_asg_count :'||to_char(g_asg_count), 122);
16347       debug('B4 g_part_time_asg_count :'||to_char(g_part_time_asg_count), 123);
16348       l_prev_asg_count := g_asg_count;
16349       --TERM_LSP: global value set to N for checking terminated employees
16350       g_terminated_person := 'N';
16351       debug('g_asg_events(l_itr).asg_count_change :' || to_char(g_asg_events(l_itr).asg_count_change),124);
16352       debug('g_asg_events(l_itr).pt_asg_count_change :' || to_char(g_asg_events(l_itr).pt_asg_count_change),125);
16353       -- Check if this event needs to be skipped
16354       IF (l_skip_itr IS NULL
16355           OR
16356           l_itr <> l_skip_itr
16357          ) THEN
16358 
16359         g_asg_count := g_asg_count
16360                        +
16361                        nvl(g_asg_events(l_itr).asg_count_change
16362                           , 0);
16363 
16364         g_part_time_asg_count := g_part_time_asg_count
16365                                  +
16366                                  nvl(g_asg_events(l_itr).pt_asg_count_change
16367                                     , 0);
16368 
16369         debug('After g_asg_count :'||to_char(g_asg_count), 124);
16370         debug('After g_part_time_asg_count :'||to_char(g_part_time_asg_count), 125);
16371 
16372       END IF;
16373 
16374       -- Eliminate duplicate changes as we will be re-calculating all
16375       -- data elements that are non-static
16376       -- Also eliminate the event if its a primary leaver event as
16377       -- we process these when deciding on withdrawal conf
16378       -- Now we also skip events if they are set in l_skip_itr
16379       IF (l_itr = g_asg_events.FIRST -- The event is the first one
16380           OR -- the date is not the same as previous event date
16381           g_asg_events(l_itr).event_date <>
16382            g_asg_events(g_asg_events.PRIOR(l_itr)).event_date
16383          )
16384          AND
16385          (g_asg_count > 0
16386          )
16387          AND
16388          -- Check if this event needs to be skipped
16389          (l_skip_itr IS NULL
16390           OR
16391           l_itr <> l_skip_itr
16392          ) THEN
16393 
16394         debug('Processing Event Date :'||to_char(g_asg_events(l_itr).event_date,'DD/MM/YYYY'),130);
16395         debug('           Event Type :'||g_asg_events(l_itr).event_type,140);
16396 
16397         -- Copy the main or previous line of service record
16398         l_new_rec := l_prev_new_rec;
16399 
16400         -- Set the start date(seq number 13)
16401         -- Bugfix 3880543:REHIRE : If the current event is a leaver
16402         --   event then we need an event date + 1 otherwise event date
16403         IF INSTR(nvl(g_asg_events(l_itr).event_type,'XX')
16404                 ,'LEAVER'
16405                 ) > 0 THEN
16406           l_new_rec.val_13 := to_char(g_asg_events(l_itr).event_date + 1,'DDMMYY');
16407         ELSE
16408           l_new_rec.val_13 := to_char(g_asg_events(l_itr).event_date,'DDMMYY');
16409         END IF;
16410 
16411 	--Bugfix 9441225 --
16412 	-- if last event is asg leaver and it is on 31-mar-YYYY
16413         -- then new line start date will be '1-apr-yyyy'
16414         -- hence added check to prevent recalulation on wrong dates
16415 
16416         if to_date(l_new_rec.val_13, 'DDMMYY') > g_effective_run_date then
16417         debug('Exiting from events loop..', 140.1);
16418         Exit;
16419         End if;
16420         ------ End Bugfix 9441225--
16421         l_next_itr := g_asg_events.NEXT(l_itr);
16422 
16423         -- Bugfix 3880543:REHIRE :
16424         -- We need to pre-Evaluate the next event
16425         --  We might want to skip the next event if the
16426         --  next event date is equal to current event date+1
16427         --  as it could result in a line with start date
16428         --  greater than end date. We only want to skip
16429         --  if the next event will not result in a primary
16430         --  leaver event, meaning it will not coz g_asg_count
16431         --  to become zero.
16432         IF l_next_itr IS NOT NULL
16433            AND
16434            ( (g_asg_count
16435               +
16436               nvl(g_asg_events(l_next_itr).asg_count_change, 0)
16437              ) > 0
16438            ) THEN
16439 
16440 
16441           debug('Pre-Evaluating next event',141);
16442           -- The start date greater than end data problem
16443           -- can only occur in the following situation
16444           -- If the current event is a (new line) leaver event AND
16445           -- Next event is NOT a leaver event AND
16446           -- Next event date is EQUAL to current event date + 1
16447           IF (INSTR(nvl(g_asg_events(l_itr).event_type,'XX')
16448                   ,'LEAVER'
16449                   ) > 0
16450              )
16451              AND
16452              (INSTR(nvl(g_asg_events(l_next_itr).event_type,'XX')
16453                    ,'LEAVER'
16454                    ) <= 0
16455              )
16456              AND
16457              ( g_asg_events(l_itr).event_date + 1
16458                =
16459                g_asg_events(l_next_itr).event_date
16460              ) THEN
16461 
16462             debug('Pre-Evaluate: Need to skip next event',142);
16463             -- We want to skip the next event, Set skip itr
16464             l_skip_itr := l_next_itr;
16465 
16466             -- Since we have set l_skip_itr, we need to adjust the
16467             -- g_asg_count and g_part_time_asg_count now rather
16468             -- than later coz we need the updated globals before
16469             -- recalc_data_elements is called
16470             l_prev_asg_count := g_asg_count;
16471 
16472             g_asg_count := g_asg_count
16473                            +
16474                            nvl(g_asg_events(l_skip_itr).asg_count_change
16475                               , 0);
16476 
16477             g_part_time_asg_count := g_part_time_asg_count
16478                                      +
16479                                      nvl(g_asg_events(l_skip_itr).pt_asg_count_change
16480                                         , 0);
16481 
16482             debug('After g_asg_count :'||to_char(g_asg_count), 143);
16483             debug('After g_part_time_asg_count :'||to_char(g_part_time_asg_count), 144);
16484 
16485             -- Get next-of-next and treat it as the next event
16486             l_next_of_next_itr := g_asg_events.NEXT(l_next_itr);
16487             l_next_itr := l_next_of_next_itr;
16488 
16489           ELSE
16490             l_skip_itr := NULL;
16491           END IF;
16492 
16493         ELSE -- Pre-Evaluating
16494 
16495           debug('Pre-Evaluate: Last event OR next causing primary leaver',145);
16496           -- UnSet skip itr
16497           l_skip_itr := NULL;
16498 
16499         END IF; -- Pre-Evaluating
16500 
16501         -- Now doing the real processing
16502         IF l_next_itr IS NOT NULL THEN
16503 
16504           debug('Next event exists',146);
16505 
16506           -- Also check if the next event is a Primary Leaver event
16507           IF -- If the next event will cause g_asg_count to become zero
16508              ( (g_asg_count
16509                 +
16510                 nvl(g_asg_events(l_next_itr).asg_count_change, 0)
16511                ) <= 0
16512              ) THEN
16513 
16514             -- Set the end date
16515             l_new_rec.val_14 := to_char(g_asg_events(l_next_itr).event_date,'DDMMYY');
16516 
16517             debug('Setting W on New record',150);
16518             -- And the withdrawal conf flag
16519             l_new_rec.val_15 := 'W';
16520 
16521           ELSE
16522 
16523             -- Set the end date
16524             -- Bugfix 3880543:REHIRE : If the next event is a leaver
16525             --   event then we need an event date otherwise
16526             --   event date less one
16527             IF INSTR(nvl(g_asg_events(l_next_itr).event_type,'XX')
16528                 ,'LEAVER'
16529                 ) > 0 THEN
16530               l_new_rec.val_14 := to_char(g_asg_events(l_next_itr).event_date,'DDMMYY');
16531             ELSE
16532               l_new_rec.val_14 := to_char(g_asg_events(l_next_itr).event_date -1,'DDMMYY');
16533             END IF;
16534 
16535             -- And the withdrawal conf flag
16536             l_new_rec.val_15 := ' ';
16537 
16538           END IF;
16539 
16540           debug('Check if ASG_STATUS event');
16541           debug('g_terminated_person  val : '||g_terminated_person,170);
16542           debug('g_asg_events.current_event_type: '|| (g_asg_events(l_itr).event_type),175 );
16543           debug('g_asg_events.next_event_type: '|| (g_asg_events(l_next_itr).event_type),180 );
16544           IF INSTR(nvl(g_asg_events(l_next_itr).event_type,'XX'),'PQP_GB_TP_ASG_STATUS') > 0 THEN
16545 
16546             g_terminated_person := 'Y';
16547 
16548            debug('Inside event check g_terminated_person  val : '||g_terminated_person);
16549           ELSE
16550             g_terminated_person := 'N';
16551           END IF;
16552           debug('After event check g_terminated_person  val : '||g_terminated_person);
16553 
16554         ELSE -- This is the last event
16555 
16556           debug('This is the last event',160);
16557 
16558           -- Set the end date as run end date
16559           l_new_rec.val_14 := to_char(g_effective_run_date,'DDMMYY');
16560 
16561           -- Reset the Withdrawal conf flag
16562           l_new_rec.val_15 := ' ';
16563 
16564         END IF; -- l_next_itr IS NOT NULL THEN
16565 
16566 
16567   --The following bugfix(sort order only) has been undone. The reason being:
16568   --DE PQP GB TP Type 1 Record Serial Number - Detail Teachers Annual Returns ( England and Wales ) has a
16569   --maximum length of 1 character. So the changes done in 115.99 are being reverted here.
16570 
16571      -- **************************
16572         -- Record serial number
16573         -- bug fix : 4926143
16574         -- this is being done as this column in the table is of varchar2 type, due to which
16575         -- sorting using order_by was incorrect when values are like 1,2,3,4....,9,10,11,12.
16576         -- For example, 1,2..,9,10,11,12 gets sorted as 1,10,11,12,2,3...,9.
16577         -- To fix this issue, the value in the thrd_sort_val is being stored as 3 digit numbers,
16578         -- starting from 001, 002 and so on.
16579 
16580         -- l_new_rec.val_26 := ltrim(rtrim(to_char((to_number(l_prev_new_rec.val_26) + 1),'009')));
16581      -- **************************
16582 
16583         -- reintroduced from version 115.98
16584         l_new_rec.val_26 := to_char(to_number(l_prev_new_rec.val_26) + 1);
16585 
16586 
16587         -- Set the sorting order
16588         l_new_rec.thrd_sort_val := l_new_rec.val_26;
16589 
16590 
16591 
16592 
16593 
16594         -- Now recalculate the data elements in this new record
16595         recalc_data_elements
16596                    (p_dtl_rec              => l_new_rec -- IN OUT
16597                    ,p_rec_type             => 'NEW'
16598                    ,p_event_details        => g_asg_events(l_itr)
16599                    ,p_assignment_id        => p_assignment_id
16600                    );
16601 
16602 
16603         debug('New Record Start Date :'||l_new_rec.val_13,120);
16604         debug('New Record End Date   :'||l_new_rec.val_14,130);
16605 
16606      IF l_new_rec.val_17 <> '000000' THEN
16607         IF INSTR(nvl(g_asg_events(l_itr).event_type, 'XX'),'PQP_GB_TP_ELEMENT_ENTRY') > 0 THEN --8616289
16608         debug('This is element entry change event',131);
16609 	OPEN csr_chk_los_change
16610                 (p_prev_new_rec    =>  l_prev_new_rec
16611                 ,p_new_rec        => l_new_rec
16612 		);
16613         FETCH csr_chk_los_change INTO l_check_new_los;
16614         CLOSE csr_chk_los_change;
16615 
16616 /*	If l_new_rec.val_15 ='W' and l_prev_new_rec.val_15 = 'W' THEN -- change for bug 7173168
16617          l_check_new_los := '1';
16618         end if ;*/-- this part of code is not needed
16619 
16620 	   IF l_check_new_los <> '0' THEN
16621             -- Update the previous record
16622             upd_rslt_dtl(p_dtl_rec => l_prev_new_rec);
16623 
16624             -- Store this new line
16625             ins_rslt_dtl(p_dtl_rec => l_new_rec -- IN OUT
16626                       );
16627            END IF;
16628              IF to_date(l_new_rec.val_13, 'ddmmyyyy') <> to_date(l_prev_new_rec.val_14, 'ddmmyyyy') + 1 then
16629                 ins_rslt_dtl(p_dtl_rec => l_new_rec);
16630              END IF;
16631       ELSE
16632            debug('This is not a element entry change event',132);
16633             -- Update the previous record
16634             upd_rslt_dtl(p_dtl_rec => l_prev_new_rec);
16635 
16636             -- Store this new line
16637             ins_rslt_dtl(p_dtl_rec => l_new_rec -- IN OUT
16638                       );
16639       END IF;
16640     ELSE
16641 
16642        l_prev_new_rec.val_15 := 'W';
16643        upd_rslt_dtl(p_dtl_rec => l_prev_new_rec);
16644     END IF;
16645 
16646 
16647      --  CLOSE csr_chk_los_change;                          -- rahul supply
16648         -- Now assign the current new record to the previous new record
16649 --        l_prev_new_rec := l_new_rec;
16650 
16651         debug('g_effective_run_date :' || to_char(g_effective_run_date),140);
16652 
16653       --   debug('l_check_new_los :'||l_check_new_los,141);
16654     IF l_new_rec.val_17 <> '000000' THEN
16655        IF INSTR(nvl(g_asg_events(l_itr).event_type, 'XX'),'PQP_GB_TP_ELEMENT_ENTRY') > 0 THEN --8616289
16656        IF l_check_new_los <> '0' THEN
16657            l_prev_new_rec := l_new_rec;
16658          debug('l_check_new_los :'||l_check_new_los,142);
16659 	ELSE
16660            debug('l_check_new_los :'||l_check_new_los,143);
16661 
16662        IF to_date(l_new_rec.val_13, 'ddmmyyyy') = to_date(l_prev_new_rec.val_14, 'ddmmyyyy') + 1 then
16663            l_prev_new_rec.val_14 := l_new_rec.val_14;
16664 	   l_prev_new_rec.val_15 := l_new_rec.val_15;  --9813084
16665 
16666            debug('val_14 :'||l_prev_new_rec.val_14,143);
16667            debug('val_15 :'||l_prev_new_rec.val_15,143);
16668            debug('l_new_rec.val_19 :'||l_new_rec.val_19,144);
16669            debug('l_prev_new_rec.val_19 :'||l_prev_new_rec.val_19,145);
16670 
16671 	   if l_prev_new_rec.val_19 = 1 and l_new_rec.val_19 = 1 THEN     -- Bug 8946616
16672                   l_prev_new_rec.val_19 := 1;
16673            ELSE
16674         	  l_prev_new_rec.val_19 := l_prev_new_rec.val_19 + l_new_rec.val_19;
16675            END IF;
16676 
16677           debug('val_19 :'||l_prev_new_rec.val_19,146);
16678 
16679 		    IF sign(l_prev_new_rec.val_19) = -1 THEN
16680 		       l_prev_new_rec.val_19 := '-'|| lpad(abs(l_prev_new_rec.val_19),5,'0');
16681 		    ELSE
16682 		       l_prev_new_rec.val_19 := lpad(l_prev_new_rec.val_19,6,'0');
16683 		    END IF;
16684           debug('val_19 :'||l_prev_new_rec.val_19,147);
16685 	   IF l_next_itr IS NULL THEN
16686 	      debug('Before Update ',147);
16687               upd_rslt_dtl(p_dtl_rec => l_prev_new_rec);
16688 	      debug('After Update ',147);
16689            END IF;
16690         ELSE
16691           l_prev_new_rec := l_new_rec;
16692           debug('l_check_new_los :'||l_check_new_los, 147.1);
16693 	END IF;
16694     END IF;
16695       ELSE
16696           l_prev_new_rec := l_new_rec;
16697           debug('l_check_new_los :'||l_check_new_los,148);
16698       END IF;
16699     END IF;
16700 	l_check_new_los := '1';                   -- rahul supply
16701 
16702       END IF; -- if this date <> last date to eliminate duplicates and primary leaver
16703       --
16704     END LOOP; -- through the sorted stored events
16705     --
16706     -- update the last record
16707     upd_rslt_dtl(p_dtl_rec => l_prev_new_rec);	      -- rahul supply
16708 
16709   END IF; -- g_asg_events.COUNT > 0 THEN
16710 
16711 
16712   --check if flag l_mark_for_delete is true, then shift the records up by one and delete the last record
16713   IF (l_mark_for_delete) THEN
16714      FOR l_cur_rec IN csr_rslt_dtl_sort(g_ext_asg_details(p_assignment_id).person_id,ben_ext_thread.g_ext_rslt_id)
16715      LOOP
16716        debug('Processing Record:'||l_cur_rec.val_26);
16717        IF (l_cur_rec.val_26 <> 1) THEN
16718          copy_rec_val(l_cur_rec,l_prev_rec);
16719          upd_rslt_dtl(p_dtl_rec => l_prev_rec);
16720        END IF;
16721        l_prev_rec := l_cur_rec;
16722      END LOOP;
16723      -- delete the last record
16724        debug('Deleting record:'||l_prev_rec.val_26);
16725         DELETE BEN_EXT_RSLT_DTL
16726         WHERE EXT_RSLT_ID = l_prev_rec.ext_rslt_id
16727           AND PERSON_ID = l_prev_rec.person_id
16728           AND VAL_26 = l_prev_rec.val_26;
16729   END IF;
16730 
16731 
16732   debug_exit(l_proc_name);
16733 
16734   RETURN;
16735 
16736 EXCEPTION
16737   WHEN OTHERS THEN
16738     debug('SQLCODE :'||to_char(SQLCODE), 140);
16739     debug('SQLERRM :'||SQLERRM, 150);
16740     debug_exit(' Others in '||l_proc_name);
16741     RAISE;
16742 END; -- create_new_lines
16743 
16744 -- 8iComp Changes: IMORTANT NOTE
16745 -- Changing he following function to use the 8iComp code.
16746 -- Now it does not reference g_asg_leaver_events_table global
16747 -- but references g_per_asg_leaver_dates global
16748 -- and calls get_g_per_asg_leaver_dates to get the
16749 -- collection for Leaver-Restarter dates for an assignment
16750 --
16751 -- Extended Criteria to generate new lines of service
16752 --
16753 FUNCTION create_service_lines
16754   (p_assignment_id            IN      NUMBER  -- context
16755   ) RETURN VARCHAR2
16756 IS
16757 
16758   -- Variable Declaration
16759   l_curr_asg_id         per_all_assignments_f.assignment_id%TYPE;
16760   l_prev_asg_id         per_all_assignments_f.assignment_id%TYPE;
16761   l_sec_asg_id          per_all_assignments_f.assignment_id%TYPE;
16762 
16763   -- Rowtype Variable Declaration
16764   l_all_sec_asgs        t_sec_asgs_type;
16765 
16766   --l_sec_asg_details   csr_sec_assignments%ROWTYPE := NULL;
16767 
16768 
16769   -- 8iComp
16770   l_insert_rec          NUMBER;
16771   l_record_count        NUMBER;
16772 
16773   l_proc_name          VARCHAR2(61):=
16774      g_proc_name||'create_service_lines';
16775 
16776 BEGIN -- create_service_lines
16777 
16778   debug_enter(l_proc_name);
16779 
16780   -- Step 0) Reset the global variable, it may contain events for the previous person processed
16781   --   If events will be stored in this global by the basic criteria, then the following
16782   --   line will need to be commented out
16783   --   Also, set the global variable for primary assignment id
16784 
16785   -- Bugfix 3073562:GAP10
16786   -- Commenting out this line as we are now storing LEA Estb to
16787   -- LEA Estb location change as a new line of service event frm
16788   -- proc chk_is_teacher_a_leaver
16789   -- The collection is now reset from the periodic and annual criteria.
16790   -- g_asg_events.DELETE;
16791   debug('Count in g_asg_events :'||to_char(g_asg_events.COUNT), 20);
16792 
16793   g_primary_leaver_dates.DELETE;
16794   g_primary_assignment_id := p_assignment_id;
16795 
16796   -- Bugfix 3803760:FTSUPPLY : Resetting override ft asg id
16797   --  This is needed coz if for the frist line one of
16798   --  the secondary was FT, then chk_report_assignment
16799   --  will return N for the primary asg below and
16800   --  we will not pick up any new line and leaver restarter
16801   --  events for the primary asg
16802   g_override_ft_asg_id := NULL;
16803   g_tab_sec_asgs.DELETE;
16804 
16805   -- Step 1) Get events( dates, type, assignment id)
16806   -- This procedure stores into a global collection containing the following :
16807   -- a) event_date -> to be sorted ascending later
16808   -- b) event_type -> helps in deciding which columns on the
16809   --                    report need a refresh
16810   -- c) assignment_id -> assignment id
16811   --
16812 
16813   -- Step 1a) Get valid events for primary assignment
16814 
16815   -- Bugfix 3073562:GAP6
16816   -- But only do this if the primary asg is a teacher
16817   -- and is to be reported
16818   IF chk_report_assignment
16819        (p_assignment_id            => p_assignment_id
16820        ,p_secondary_assignment_id  => l_sec_asg_id
16821        ) = 'Y' THEN
16822 
16823    -- PER_LVR change
16824    -- for PERIODIC REPORT : Logic to store leaver restarter dates for the assignments
16825    -- has been moved to criteria code.
16826    -- Now store these dates, only if the Extract type is ANUAL : 'TP1'
16827    -- for annual report we are calling chk_person_leaver when criteria is Exclude
16828     IF g_extract_type = 'TP1' and nvl(g_reporting_mode,'EXCLUDE') = 'INCLUDE' THEN
16829      -- Store all the leaver and restarter dates for the primary asg
16830 
16831      store_leaver_restarter_dates
16832           (p_assignment_id => p_assignment_id
16833           );
16834 
16835       -- MULT-LR changes
16836       -- g_asg_leaver_events_table(p_assignment_id) := g_primary_leaver_dates ;
16837 
16838       --8iComp
16839       debug('inserting in new collection...', 22);
16840       l_insert_rec := set_g_per_asg_leaver_dates
16841                        ( p_leaver_dates_type => g_primary_leaver_dates) ;
16842 
16843       debug('l_insert_rec: '|| l_insert_rec, 24) ;
16844      -- 8iComp
16845    ELSE
16846      debug ('Leaver Restarter Dates are already stored ...',30);
16847      -- QAB1: restore the leaver dates collection.
16848      debug ('restoring the collection temporarily for primary dates',40) ;
16849 
16850      -- 8iComp chagnes
16851      --g_primary_leaver_dates := g_asg_leaver_events_table(p_assignment_id) ;
16852 
16853      l_record_count :=  get_g_per_asg_leaver_dates
16854                             ( p_assignment_id     => p_assignment_id
16855                              ,p_leaver_dates_type => g_primary_leaver_dates
16856                              ) ;
16857 
16858      debug('l_record_count: '|| l_record_count, 45) ;
16859 
16860      -- 8iComp Changes
16861 
16862 
16863    END IF;
16864 
16865    -- Store new line of service events for the primary assignment
16866     get_events_in_prmy_range(p_assignment_id => p_assignment_id
16867                             );
16868 
16869   ELSE
16870 
16871     -- Bugfix 3880543:REHIRE
16872     -- Setting the sec asg id as the primary asg
16873     -- We will treat the sec asg as promary so all
16874     --  leaver restarter events for this secondary
16875     --  will b treated as primary leaver restarter events
16876     g_primary_assignment_id := l_sec_asg_id;
16877 
16878   END IF; -- IF chk_report_assignment
16879 
16880   -- Step 1b) Get secondary assignments
16881   l_all_sec_asgs := get_all_secondary_asgs
16882                      (p_primary_assignment_id   => p_assignment_id
16883                      --changed from start_date to teacher_start_date.
16884                      ,p_effective_date          => g_ext_asg_details(p_assignment_id).teacher_start_date
16885                      );
16886 
16887 
16888   -- Have we found any secondary assignments?
16889   --IF l_all_sec_asgs IS NOT NULL THEN, cannot use is NOT NULL with index by tables
16890   IF l_all_sec_asgs.COUNT > 0 THEN
16891 
16892     l_curr_asg_id := l_all_sec_asgs.FIRST;
16893 
16894     WHILE l_curr_asg_id IS NOT NULL
16895     LOOP
16896 
16897       debug('Processing Secondary Asg :'||to_char(l_curr_asg_id),50);
16898       -- Get the asg details, not needed currently,
16899       -- will uncomment if needed,both here and in declaration
16900       --l_sec_asg_details := l_all_sec_asgs(l_curr_asg_id);
16901 
16902       -- PER_LVR change
16903       -- for PERIODIC REPORT : Logic to store leaver restarter dates for the assignments
16904       -- has been moved to criteria code.
16905       -- Now store these dates, only if the Extract type is ANUAL : 'TP1'
16906       -- for annual report we are calling chk_person_leaver when criteria is Exclude
16907      IF g_extract_type = 'TP1' and nvl(g_reporting_mode,'EXCLUDE') = 'INCLUDE' THEN
16908         -- Delete the global for lever restarter dates for secondary asg.
16909         g_sec_leaver_dates.DELETE;
16910 
16911         -- Store all the leaver and restarter dates for the secondary asg
16912         store_leaver_restarter_dates
16913                 (p_assignment_id => l_curr_asg_id
16914                 );
16915 
16916         -- MULT-LR changes
16917         IF l_curr_asg_id = g_primary_assignment_id THEN
16918           --g_asg_leaver_events_table(l_curr_asg_id) := g_primary_leaver_dates ;
16919 
16920           --8iComp
16921           debug('inserting in new collection...', 22);
16922           l_insert_rec := set_g_per_asg_leaver_dates
16923                        ( p_leaver_dates_type => g_primary_leaver_dates) ;
16924 
16925           debug('l_insert_rec: '|| l_insert_rec, 24) ;
16926           -- 8iComp
16927         ELSE
16928           --g_asg_leaver_events_table(l_curr_asg_id) := g_sec_leaver_dates ;
16929           --8iComp
16930           debug('inserting in new collection...', 22);
16931           l_insert_rec := set_g_per_asg_leaver_dates
16932                        ( p_leaver_dates_type => g_sec_leaver_dates) ;
16933 
16934           debug('l_insert_rec: '|| l_insert_rec, 24) ;
16935           -- 8iComp
16936         END IF;
16937 
16938       ELSE --g_extract_type = 'TP1'
16939         debug ('Leaver Restarter Dates are already stored ...',60);
16940 
16941         -- QAB1: Restore the leaver_dates collection from the
16942         -- leaver events table as this is required in the following functions
16943         -- get_events_in_prmy_range/get_events_in_sec_range
16944 
16945         IF l_curr_asg_id = g_primary_assignment_id THEN
16946           debug ('restoring the collection temporarily for primary dates',70) ;
16947 
16948           -- g_primary_leaver_dates := g_asg_leaver_events_table(l_curr_asg_id) ;
16949           -- 8iComp
16950           l_record_count :=  get_g_per_asg_leaver_dates
16951                             ( p_assignment_id     => p_assignment_id
16952                              ,p_leaver_dates_type => g_primary_leaver_dates
16953                              ) ;
16954 
16955           debug('l_record_count: '|| l_record_count, 45) ;
16956 
16957           -- 8iComp Changes
16958 
16959         ELSE
16960           debug ('restoring the collection temporarily for Secondary dates',80) ;
16961           -- g_sec_leaver_dates := g_asg_leaver_events_table(l_curr_asg_id) ;
16962 
16963           -- 8iComp
16964           -- 8iComp
16965           l_record_count :=  get_g_per_asg_leaver_dates
16966                             ( p_assignment_id     => l_curr_asg_id
16967                              ,p_leaver_dates_type => g_primary_leaver_dates
16968                              ) ;
16969 
16970           debug('l_record_count: '|| l_record_count, 45) ;
16971 
16972           -- 8iComp Changes
16973 
16974         END IF;
16975 
16976       END IF; --g_extract_type = 'TP1'
16977 
16978 
16979       -- Bugfix 3880543:REHIRE
16980       -- If curr sec asg is being treated as the primary then
16981       --  store events in primary range otherwise in sec range
16982       IF l_curr_asg_id = g_primary_assignment_id THEN
16983 
16984         -- Store new line of service events for the primary assignment
16985         get_events_in_prmy_range(p_assignment_id => l_curr_asg_id
16986                                 );
16987       ELSE
16988 
16989         -- Store new line of service events for the SECONDARY assignment
16990         get_events_in_sec_range(p_assignment_id => l_curr_asg_id
16991                                );
16992       END IF;
16993 
16994       -- Assign the current asg id to prev asg id
16995       -- and reset curr asg id, ready for the next one
16996       l_prev_asg_id := l_curr_asg_id;
16997       l_curr_asg_id := NULL;
16998 
16999       -- Get next secondary assignment
17000       l_curr_asg_id := l_all_sec_asgs.NEXT(l_prev_asg_id);
17001 
17002     END LOOP; -- l_curr_asg_id IS NOT NULL
17003     --
17004   END IF; -- l_all_sec_asgs.COUNT > 0 THEN
17005 
17006   -- MULT-LR --
17007   -- print all the events stored so far.
17008   -- Print the Events table only if Debug is switched on.
17009   IF NVL(g_trace,'N') = 'Y' THEN
17010     print_events_table ();
17011   END IF;
17012 
17013   -- Bugfix 3880543:REHIRE
17014   -- Currently there is not requirement for resetting the global
17015   --  g_primary_assignment_id to the original primary asg id as it
17016   --  is not used beyond this point. If in the future we do need
17017   --  this global beyond this point, uncomment the following statement
17018 
17019   -- MULT-LR:  uncommented the following line.
17020    g_primary_assignment_id := p_assignment_id;
17021 
17022   -- Step 1d) Sort events by Ascending Date if we have found more than 1 events
17023   IF g_asg_events.COUNT > 1 THEN
17024     sort_stored_events;
17025   END IF;
17026 
17027   -- Step 2) Create new lines of service for each event.
17028   --            This proc also updates the main record
17029   create_new_lines
17030     (p_assignment_id    => p_assignment_id
17031     );
17032 
17033 
17034   -- Step 3) Reset the global variable containing events for this person
17035   g_asg_events.DELETE;
17036 
17037   debug_exit(l_proc_name);
17038 
17039   RETURN 'DELETE';
17040 
17041 EXCEPTION
17042     WHEN OTHERS THEN
17043 
17044       -- Reset the global variable containing events for this person
17045       g_asg_events.DELETE;
17046 
17047       debug('SQLCODE :'||to_char(SQLCODE), 40);
17048       debug('SQLERRM :'||SQLERRM, 50);
17049 
17050       debug_exit(' Others in '||l_proc_name
17051                 ,'Y' -- turn trace off
17052                 );
17053       RAISE;
17054 END; -- create_service_lines
17055 --
17056 -- del_dummy_recs
17057 --
17058 PROCEDURE del_dummy_recs
17059 IS
17060 
17061   -- Variable Declaration
17062   l_ext_dtl_rcd_id      ben_ext_rcd.ext_rcd_id%TYPE;
17063 
17064   l_proc_name          VARCHAR2(61):=
17065      g_proc_name||'del_dummy_recs';
17066 
17067 BEGIN -- del_dummy_recs
17068 
17069   debug_enter(l_proc_name);
17070 
17071   -- Get the record id for the Type 1 Hidden Detail record
17072   OPEN csr_ext_rcd_id(p_hide_flag       => 'Y'
17073                      ,p_rcd_type_cd     => 'D'
17074                      );
17075   FETCH csr_ext_rcd_id INTO l_ext_dtl_rcd_id;
17076   CLOSE csr_ext_rcd_id;
17077 
17078   DELETE
17079   FROM ben_ext_rslt_dtl dtl
17080   WHERE dtl.ext_rslt_id = ben_ext_thread.g_ext_rslt_id
17081     AND dtl.ext_rcd_id = l_ext_dtl_rcd_id
17082     AND dtl.val_01 = 'DELETE';
17083 
17084   debug('Number of Dummy Records Deleted :'||to_char(SQL%ROWCOUNT));
17085 
17086   debug_exit(l_proc_name);
17087   RETURN;
17088 
17089 EXCEPTION
17090   WHEN OTHERS THEN
17091     debug_exit(' Others in '||l_proc_name);
17092   RAISE;
17093 END; -- del_dummy_recs
17094 
17095 --
17096 -- type1_post_proc_rule
17097 --
17098 FUNCTION type1_post_proc_rule
17099                 (p_ext_rslt_id  IN ben_ext_rslt_dtl.ext_rslt_id%TYPE
17100                 ) RETURN VARCHAR2
17101 IS
17102 
17103   -- Variable Declaration
17104 
17105   -- Rowtype Variable Declaration
17106 
17107   l_proc_name          VARCHAR2(61):=
17108      g_proc_name||'type1_post_proc_rule';
17109 
17110 BEGIN -- type1_post_proc_rule
17111 
17112   debug_enter(l_proc_name);
17113 
17114   -- Step 1) Delete detail records which are not being displayed.
17115   del_dummy_recs;
17116 
17117   -- Step 2) Re-calc total in the trailer
17118   -- Not needed as now hv modified the SQL in Type 4 pkg which gets the
17119   -- total number of records to ignore all details records which have
17120   -- 'DELETE' in column val_01
17121 
17122   debug_exit(l_proc_name);
17123   RETURN 'T1';
17124 
17125 EXCEPTION
17126     WHEN OTHERS THEN
17127       debug_exit(' Others in '||l_proc_name
17128                 ,'Y' -- turn trace off
17129                 );
17130       RAISE;
17131 END; -- type1_post_proc_rule
17132 
17133 --
17134 -- chk_rate_change_affects_asg
17135 --
17136 FUNCTION chk_rate_change_affects_asg
17137                 (p_assignment_id        IN NUMBER
17138                 ,p_rate_id              IN NUMBER
17139                 ,p_effective_date       IN DATE
17140                 ) RETURN BOOLEAN IS
17141 
17142   CURSOR c_rates IS
17143   SELECT * FROM pay_rates
17144   WHERE rate_id = p_rate_id;
17145 
17146   CURSOR c_ele_attr(p_element_type_id IN NUMBER) IS
17147   SELECT petei.eei_information2 pay_source_value
17148         ,petei.eei_information3 Qualifier
17149   FROM pay_element_type_extra_info petei
17150   WHERE petei.element_type_id = p_element_type_id
17151     AND petei.eei_information_category  ='PQP_UK_ELEMENT_ATTRIBUTION';
17152 
17153   CURSOR c_element_entry(p_element_type_id IN NUMBER) IS
17154   SELECT pee.element_entry_id
17155   FROM pay_element_links_f pel
17156       ,pay_element_entries_f pee
17157   where pel.element_type_id = p_element_type_id
17158     and p_effective_date between pel.effective_start_date
17159                              and pel.effective_end_date
17160     and pee.element_link_id = pel.element_link_id
17161     and p_effective_date between pee.effective_start_date
17162                              and pee.effective_end_date
17163     and pee.assignment_id = p_assignment_id;
17164 
17165 
17166 
17167   l_asg_affected        BOOLEAN := FALSE;
17168   l_itr                 NUMBER;
17169   l_current             NUMBER;
17170 
17171   l_rate_dets           c_rates%ROWTYPE;
17172   l_ele_attr            c_ele_attr%ROWTYPE;
17173   l_element_entry       c_element_entry%ROWTYPE;
17174 
17175   l_pet_ids             t_ele_ids_from_bal;
17176 
17177   l_proc_name           VARCHAR2(61):=
17178      g_proc_name||'chk_rate_change_affects_asg';
17179 
17180 BEGIN -- chk_rate_change_affects_asg
17181 
17182   debug_enter(l_proc_name);
17183 
17184   -- Get the Rate Details
17185   OPEN c_rates;
17186   FETCH c_rates INTO l_rate_dets;
17187   CLOSE c_rates;
17188 
17189   -- Replace rate type of G with GR as rates history stores GR
17190   IF l_rate_dets.rate_type = 'G' THEN
17191     debug(l_proc_name, 10);
17192     l_rate_dets.rate_type := 'GR';
17193   END IF;
17194 
17195   -- For each Element Type id, check its Element Attribution
17196   -- to see if it has the Rate Type being evaluated
17197   l_itr := 1;
17198   l_current := g_tab_sal_ele_ids.FIRST;
17199 
17200   WHILE l_itr <= g_tab_sal_ele_ids.COUNT
17201   LOOP -- through the element type ids in Salary Balance
17202 
17203     debug(l_proc_name, 20);
17204     -- Get the element attribution info for this element type id
17205     OPEN c_ele_attr(p_element_type_id => g_tab_sal_ele_ids(l_current).element_type_id);
17206     FETCH c_ele_attr INTO l_ele_attr;
17207 
17208     IF c_ele_attr%FOUND
17209        AND -- The rate Type matches
17210        l_ele_attr.pay_source_value = l_rate_dets.rate_type
17211        AND -- The Rate Name also matches
17212        l_ele_attr.qualifier = l_rate_dets.name
17213     THEN
17214 
17215       debug(l_proc_name, 30);
17216       -- Add this element type id to list of valid ele ids
17217       -- so we can check if the asg has an effective Element
17218       -- entry for this Element Type id
17219       l_pet_ids(l_pet_ids.COUNT + 1) := g_tab_sal_ele_ids(l_current);
17220 
17221     END IF;
17222 
17223     CLOSE c_ele_attr;
17224 
17225     l_current := g_tab_sal_ele_ids.NEXT(l_current);
17226     IF l_current IS NULL THEN
17227       EXIT;
17228     ELSE
17229       l_itr := l_itr + 1;
17230     END IF;
17231 
17232   END LOOP; -- through the element type ids in Salary Balance
17233 
17234   debug(l_proc_name, 40);
17235   l_itr := NULL;
17236 
17237   -- Now LOOP through element types which have the Rate Type
17238   -- in Ele Attribution to check if the assignment has an
17239   -- Element entry for this ele type.
17240   FOR l_itr IN 1..l_pet_ids.COUNT
17241   LOOP
17242 
17243     debug(l_proc_name, 50);
17244     OPEN c_element_entry(p_element_type_id => l_pet_ids(l_itr).element_type_id);
17245     FETCH c_element_entry INTO l_element_entry;
17246 
17247     IF c_element_entry%FOUND THEN
17248       debug('Setting l_asg_affected to TRUE', 60);
17249       l_asg_affected := TRUE;
17250       EXIT;
17251     END IF;
17252 
17253     CLOSE c_element_entry;
17254 
17255   END LOOP; -- l_itr INTO 1..l_pet_ids.COUNT
17256 
17257   debug_exit(l_proc_name);
17258 
17259   RETURN l_asg_affected;
17260 
17261 EXCEPTION
17262   WHEN OTHERS THEN
17263     debug('Other exception :'||SQLCODE||' '||SQLERRM, 70);
17264     l_asg_affected := FALSE;
17265     debug_exit(l_proc_name);
17266     RAISE;
17267 
17268 END chk_rate_change_affects_asg;
17269 
17270 --
17271 -- chk_grd_change_affects_asg
17272 --
17273 FUNCTION chk_grd_change_affects_asg
17274                 (p_assignment_id        IN NUMBER
17275                 ,p_grade_rule_id        IN NUMBER
17276                 ,p_effective_date       IN DATE
17277                 ) RETURN BOOLEAN IS
17278 
17279   CURSOR c_grade_rule IS
17280   SELECT effective_start_date
17281         ,effective_end_date
17282         ,rate_id
17283         ,grade_or_spinal_point_id
17284         ,rate_type
17285   FROM pay_grade_rules_f
17286   WHERE grade_rule_id = p_grade_rule_id
17287     AND p_effective_date BETWEEN effective_start_date
17288                              AND effective_end_date;
17289 
17290   CURSOR c_asg_grade IS
17291   SELECT grade_id
17292   FROM per_all_assignments_f
17293   WHERE assignment_id = p_assignment_id
17294     AND p_effective_date BETWEEN effective_start_date
17295                              AND effective_end_date;
17296 
17297   CURSOR c_asg_grade_step(p_grade_id        IN NUMBER
17298                          ,p_spinal_point_id IN NUMBER
17299                          ) IS
17300   SELECT pspp.placement_id
17301   FROM per_spinal_point_placements_f pspp
17302       ,per_grade_spines_f pgs
17303       ,per_spinal_point_steps_f psps
17304   WHERE pspp.assignment_id = p_assignment_id
17305     AND p_effective_date BETWEEN pspp.effective_start_date
17306                              AND pspp.effective_end_date
17307     AND pgs.parent_spine_id =  pspp.parent_spine_id
17308     AND pgs.grade_id = p_grade_id
17309     AND p_effective_date BETWEEN pgs.effective_start_Date
17310                              AND pgs.effective_end_Date
17311     AND psps.grade_spine_id = pgs.grade_spine_id
17312     AND psps.spinal_point_id = p_spinal_point_id
17313     AND p_effective_date BETWEEN psps.effective_start_Date
17314                              AND psps.effective_end_Date
17315     AND psps.step_id = pspp.step_id;
17316 
17317 
17318   l_grade_rule          c_grade_rule%ROWTYPE;
17319   l_asg_grade           c_asg_grade%ROWTYPE;
17320   l_asg_grade_step      c_asg_grade_step%ROWTYPE;
17321 
17322   l_asg_affected        BOOLEAN := FALSE;
17323 
17324   l_proc_name           VARCHAR2(61):=
17325      g_proc_name||'chk_grd_change_affects_asg';
17326 
17327 BEGIN -- chk_grd_change_affects_asg
17328 
17329   debug_enter(l_proc_name);
17330 
17331   OPEN c_grade_rule;
17332   FETCH c_grade_rule INTO l_grade_rule;
17333   CLOSE c_grade_rule;
17334 
17335   -- Now get the grade id, we need it anyway
17336   OPEN c_asg_grade;
17337   FETCH c_asg_grade INTO l_asg_grade;
17338   CLOSE c_asg_grade;
17339 
17340   IF l_grade_rule.rate_type = 'G' THEN -- Grade Rate
17341 
17342     debug('Rate Type is Grade Rate, id :'||l_grade_rule.grade_or_spinal_point_id, 10);
17343 
17344     -- Grade rule grade id same as asg grade id
17345     IF l_grade_rule.grade_or_spinal_point_id = l_asg_grade.grade_id THEN
17346       debug('Setting l_asg_affected to TRUE', 20);
17347       l_asg_affected := TRUE;
17348     END IF;
17349 
17350   ELSIF l_grade_rule.rate_type = 'SP' THEN
17351 
17352     debug('Rate Type is Spinal Point, id :'||l_grade_rule.grade_or_spinal_point_id, 30);
17353 
17354     OPEN c_asg_grade_step(p_grade_id        => l_asg_grade.grade_id
17355                          ,p_spinal_point_id => l_grade_rule.grade_or_spinal_point_id
17356                          );
17357     FETCH c_asg_grade_step INTO l_asg_grade_step;
17358 
17359     IF c_asg_grade_step%FOUND THEN
17360       debug('Setting l_asg_affected to TRUE', 40);
17361       l_asg_affected := TRUE;
17362     END IF;
17363 
17364     CLOSE c_asg_grade_step;
17365 
17366   ELSE -- Unrecognised Rate_Type, will return FALSE
17367     debug('Unrecognized Rate Type :'||l_grade_rule.rate_type, 50);
17368     l_asg_affected := FALSE;
17369   END IF; -- l_grade_rule.rate_type =
17370 
17371   IF l_asg_affected -- by Grade change
17372   THEN -- check if asg also affected by rate change
17373 
17374     IF NOT chk_rate_change_affects_asg
17375              (p_assignment_id        => p_assignment_id
17376              ,p_rate_id              => l_grade_rule.rate_id
17377              ,p_effective_date       => p_effective_date
17378              ) THEN
17379       l_asg_affected := FALSE;
17380     END IF;
17381     --
17382   ELSE
17383     debug('Assignment Affected :FALSE', 60);
17384   END IF;
17385 
17386   debug_exit(l_proc_name);
17387 
17388   RETURN l_asg_affected;
17389 
17390 EXCEPTION
17391   WHEN OTHERS THEN
17392     debug('Other exception :'||SQLCODE||' '||SQLERRM, 70);
17393     debug_exit(l_proc_name);
17394     RAISE;
17395 END; -- chk_grd_change_affects_asg
17396 
17397 --
17398 --
17399 --
17400 --
17401 --
17402 -- The procedure raises a warning if there is a full time
17403 -- teaching assignments.
17404 -- Coz cross person reporting is not enabled, so there may be
17405 -- another teaching assignment for this person
17406 
17407 PROCEDURE warn_anthr_tchr_asg (p_assignment_id IN NUMBER)
17408 IS
17409 l_proc_name  VARCHAR2(61):= 'warn_anthr_tchr_asg';
17410 l_error      NUMBER;
17411 
17412 BEGIN
17413     debug_enter(l_proc_name);
17414 
17415     -- Raise a warning if Cross person enable = N and
17416     -- there are multiple person records.
17417     IF (g_cross_per_enabled = 'N' AND g_person_count > 0) THEN
17418       l_error := pqp_gb_tp_extract_functions.raise_extract_warning
17419                  (p_assignment_id => p_assignment_id
17420                  ,p_error_text    => 'BEN_94022_TP1_ANTHR_TCHR_ASG'
17421                  ,p_error_number  => 94022
17422                  );
17423     END IF ;
17424 
17425   debug_exit(l_proc_name);
17426   EXCEPTION
17427     WHEN OTHERS THEN
17428      debug_exit(' Others in '||l_proc_name);
17429     RAISE;
17430 END warn_anthr_tchr_asg ;
17431 --
17432 
17433    PROCEDURE fetch_allow_eles_frm_udt
17434                (p_assignment_id  IN NUMBER
17435                ,p_effective_date IN DATE
17436                )
17437    IS
17438       --
17439 
17440       CURSOR csr_get_lon_user_rows (c_udt_id NUMBER)
17441       IS
17442       SELECT row_low_range_or_name
17443         FROM pay_user_rows_f
17444         WHERE user_table_id = c_udt_id
17445         AND p_effective_date BETWEEN effective_start_date
17446                                    AND effective_end_date
17447         AND row_low_range_or_name in ('LARP Inner Allowance','LARP Outer Allowance',
17448                                       'LARP Fringe Allowance','LARP Inner Plus Inner Supplement'
17449                                      )
17450         ORDER BY display_sequence;
17451 
17452       CURSOR csr_get_spl_user_rows (c_udt_id NUMBER)
17453       IS
17454       SELECT row_low_range_or_name
17455         FROM pay_user_rows_f
17456         WHERE user_table_id = c_udt_id
17457         AND p_effective_date BETWEEN effective_start_date
17458                                    AND effective_end_date
17459         AND row_low_range_or_name in ('SPAP Lower Rate','SPAP Higher Rate',
17460                                       'SPAP Special Needs Lower Rate','SPAP Special Needs Higher Rate'
17461                                      )
17462         ORDER BY display_sequence;
17463 
17464       l_proc_name         VARCHAR2 (80) :=    g_proc_name
17465                                            || 'fetch_allow_eles_frm_udt';
17466       l_proc_step         NUMBER;
17467       l_element_type_id   NUMBER;
17468       l_tab_mng_aln_eles  t_allowance_eles;
17469       l_tab_ret_aln_eles  t_allowance_eles;
17470 
17471       -- 115.49 : TLR
17472       l_tab_tlr_aln_eles  t_allowance_eles;
17473 
17474       l_user_row_name     pay_user_rows_f.row_low_range_or_name%TYPE;
17475       l_udt_name          pay_user_tables.user_table_name%TYPE :=
17476                            'PQP_GB_TP_TYPE1_EXTRACT_DEFINITIONS';
17477       l_return            NUMBER;
17478       l_udt_id            NUMBER;
17479       l_user_value        pay_user_column_instances_f.value%TYPE;
17480       l_error_msg         VARCHAR2(2000);
17481 
17482       -- RET1.a : new variables to store element_type_extra_info_id
17483       l_element_type_extra_info_id  pay_element_type_extra_info.element_type_extra_info_id%type;
17484       l_retval		 NUMBER;
17485       l_allow_code       VARCHAR2(1);
17486       --
17487    --
17488    BEGIN
17489       --
17490       debug_enter(l_proc_name);
17491       debug('p_effective_date: '|| p_effective_date);
17492 
17493 
17494 
17495       -- Get UDT ID
17496       l_udt_id := pqp_gb_tp_pension_extracts.get_udt_id
17497                     (p_udt_name => l_udt_name);
17498 
17499       -- Get the user rows information for this UDT
17500       --
17501       IF l_udt_id IS NOT NULL THEN
17502 
17503            debug(l_proc_name, 10);
17504 
17505         --
17506         OPEN csr_get_lon_user_rows (l_udt_id);
17507         LOOP
17508           FETCH csr_get_lon_user_rows INTO l_user_row_name;
17509           EXIT WHEN csr_get_lon_user_rows%NOTFOUND;
17510 
17511           -- Get the user value for this row if one exist
17512           -- for each type of allowance and store it in their
17513           -- respective collections
17514              debug('User Row Name: '
17515                     || l_user_row_name,20);
17516              debug('User Column Name: Management Allowance Element Type');
17517      l_return := pqp_utilities.pqp_gb_get_table_value
17518 		   (p_business_group_id => g_business_group_id
17519 		   ,p_effective_date    => p_effective_date
17520 		   ,p_table_name        => l_udt_name
17521 		   ,p_column_name       => 'Attribute Location Type'
17522 		   ,p_row_name          => l_user_row_name
17523 		   ,p_value             => l_user_value
17524 		   ,p_error_msg         => l_error_msg
17525 		  );
17526        IF upper(l_user_value) in ('G','GRADE') THEN
17527          g_lon_all_grd_src := 'Y';
17528          g_tab_lon_aln_eles.DELETE;
17529          EXIT;
17530        END IF;
17531        IF  upper(l_user_value) in ('E','ELEMENT','R','RATE TYPE') THEN
17532 	     l_return := pqp_utilities.pqp_gb_get_table_value
17533 			   (p_business_group_id => g_business_group_id
17534 			   ,p_effective_date    => p_effective_date
17535 			   ,p_table_name        => l_udt_name
17536 			   ,p_column_name       => 'Allowance Code'
17537 			   ,p_row_name          => l_user_row_name
17538 			   ,p_value             => l_allow_code
17539 			   ,p_error_msg         => l_error_msg
17540 			  );
17541        END IF;
17542 
17543        IF  upper(l_user_value) in ('E','ELEMENT') THEN
17544 
17545           l_element_type_id := pqp_gb_tp_pension_extracts.get_allow_ele_info
17546                                  (p_assignment_id  => p_assignment_id
17547                                  ,p_effective_date => p_effective_date
17548                                  ,p_table_name     => l_udt_name
17549                                  ,p_row_name       => l_user_row_name
17550                                  ,p_column_name    => 'Attribute Location Qualifier 1'
17551                                  );
17552           debug('l_element_type_id : '|| to_char(l_element_type_id));
17553 
17554       	  IF l_element_type_id IS NOT NULL
17555           THEN
17556              --
17557              -- Store it in the management allowance collection
17558              l_tab_mng_aln_eles (l_element_type_id).salary_scale_code
17559                                := l_allow_code;
17560              l_tab_mng_aln_eles (l_element_type_id).element_type_id
17561                                := l_element_type_id;
17562 	  END IF;
17563       ELSIF upper(l_user_value) in ('R','RATE TYPE') THEN-- element type id is null
17564             -- Check for rate type
17565                 debug(l_proc_name, 50);
17566 
17567             l_tab_mng_aln_eles := pqp_gb_tp_pension_extracts.get_allow_code_rt_ele_info
17568                                     (p_assignment_id  => p_assignment_id
17569                                     ,p_effective_date => p_effective_date
17570                                     ,p_table_name     => l_udt_name
17571                                     ,p_row_name       => l_user_row_name
17572                                     ,p_column_name    => 'Attribute Location Qualifier 1'
17573                                     ,p_tab_aln_eles   => l_tab_mng_aln_eles
17574                                     ,p_allowance_code => l_allow_code
17575                                     );
17576           END IF; -- End if of element type id not null check ...
17577           -- end of code for "Management Allowance Element Type" --
17578         END LOOP;
17579         CLOSE csr_get_lon_user_rows;
17580 
17581       debug('Managment collection count: '||TO_CHAR(l_tab_mng_aln_eles.COUNT));
17582       debug('Retention collection count: '||TO_CHAR(l_tab_ret_aln_eles.COUNT));
17583 
17584       g_tab_lon_aln_eles := l_tab_mng_aln_eles;
17585       l_tab_mng_aln_eles.DELETE;
17586 
17587         OPEN csr_get_spl_user_rows (l_udt_id);
17588         LOOP
17589           FETCH csr_get_spl_user_rows INTO l_user_row_name;
17590           EXIT WHEN csr_get_spl_user_rows%NOTFOUND;
17591 
17592           -- Get the user value for this row if one exist
17593           -- for each type of allowance and store it in their
17594           -- respective collections
17595              debug('User Row Name: '
17596                     || l_user_row_name,20);
17597              debug('User Column Name: Management Allowance Element Type');
17598      l_return := pqp_utilities.pqp_gb_get_table_value
17599 		   (p_business_group_id => g_business_group_id
17600 		   ,p_effective_date    => p_effective_date
17601 		   ,p_table_name        => l_udt_name
17602 		   ,p_column_name       => 'Attribute Location Type'
17603 		   ,p_row_name          => l_user_row_name
17604 		   ,p_value             => l_user_value
17605 		   ,p_error_msg         => l_error_msg
17606 		  );
17607 
17608        IF upper(l_user_value) in ('G','GRADE') THEN
17609          g_spl_all_grd_src := 'Y';
17610          g_tab_spl_aln_eles.DELETE;
17611          EXIT;
17612        END IF;
17613 
17614        IF  upper(l_user_value) in ('E','ELEMENT','R','RATE TYPE') THEN
17615 	     l_return := pqp_utilities.pqp_gb_get_table_value
17616 			   (p_business_group_id => g_business_group_id
17617 			   ,p_effective_date    => p_effective_date
17618 			   ,p_table_name        => l_udt_name
17619 			   ,p_column_name       => 'Allowance Code'
17620 			   ,p_row_name          => l_user_row_name
17621 			   ,p_value             => l_allow_code
17622 			   ,p_error_msg         => l_error_msg
17623 			  );
17624        END IF;
17625 
17626        IF  upper(l_user_value) in ('E','ELEMENT') THEN
17627 
17628           l_element_type_id := pqp_gb_tp_pension_extracts.get_allow_ele_info
17629                                  (p_assignment_id  => p_assignment_id
17630                                  ,p_effective_date => p_effective_date
17631                                  ,p_table_name     => l_udt_name
17632                                  ,p_row_name       => l_user_row_name
17633                                  ,p_column_name    => 'Attribute Location Qualifier 1'
17634                                  );
17635           debug('l_element_type_id : '|| to_char(l_element_type_id));
17636 
17637       	  IF l_element_type_id IS NOT NULL
17638           THEN
17639              --
17640              -- Store it in the management allowance collection
17641              l_tab_mng_aln_eles (l_element_type_id).salary_scale_code
17642                                := l_allow_code;
17643              l_tab_mng_aln_eles (l_element_type_id).element_type_id
17644                                := l_element_type_id;
17645 	  END IF;
17646       ELSIF upper(l_user_value) in ('R','RATE TYPE') THEN-- element type id is null
17647             -- Check for rate type
17648                 debug(l_proc_name, 50);
17649 
17650             l_tab_mng_aln_eles := pqp_gb_tp_pension_extracts.get_allow_code_rt_ele_info
17651                                     (p_assignment_id  => p_assignment_id
17652                                     ,p_effective_date => p_effective_date
17653                                     ,p_table_name     => l_udt_name
17654                                     ,p_row_name       => l_user_row_name
17655                                     ,p_column_name    => 'Attribute Location Qualifier 1'
17656                                     ,p_tab_aln_eles   => l_tab_mng_aln_eles
17657                                     ,p_allowance_code => l_allow_code
17658                                     );
17659           END IF; -- End if of element type id not null check ...
17660           -- end of code for "Management Allowance Element Type" --
17661         END LOOP;
17662         CLOSE csr_get_spl_user_rows;
17663 
17664       END IF; -- End if of udt id is not null check ...
17665 
17666       debug('Managment collection count: '||TO_CHAR(l_tab_mng_aln_eles.COUNT));
17667       debug('Retention collection count: '||TO_CHAR(l_tab_ret_aln_eles.COUNT));
17668 
17669       g_tab_spl_aln_eles := l_tab_mng_aln_eles;
17670       debug_exit(l_proc_name);
17671     --
17672     END fetch_allow_eles_frm_udt;
17673    --
17674 Function Get_Allowance_Code_New ( p_assignment_id   in number
17675                              ,p_effective_date  in date
17676                              ,p_allowance_type  in varchar2 ) Return varchar2 Is
17677    CURSOR csr_ele_entry_exists
17678      (c_assignment_id   NUMBER
17679      ,c_element_type_id NUMBER
17680      ,c_effective_date  DATE
17681      )
17682    IS
17683    SELECT 'X'
17684      FROM pay_element_entries_f pee
17685          ,pay_element_links_f   pel
17686     WHERE pee.assignment_id   = c_assignment_id
17687       AND pee.entry_type      = 'E'
17688       AND pee.element_link_id = pel.element_link_id
17689       AND c_effective_date BETWEEN pee.effective_start_date
17690                                AND pee.effective_end_date
17691       AND pel.element_type_id = c_element_type_id
17692       AND c_effective_date BETWEEN pel.effective_start_date
17693                                AND pel.effective_end_date;
17694 
17695    CURSOR csr_grade_val(c_assignment_id   NUMBER,
17696                         c_effective_date  DATE,
17697                         c_allowance_type  varchar2
17698                        ) IS
17699    SELECT decode(c_allowance_type,'LONDON_ALLOWANCE_RULE',information6,
17700                                   'SPECIAL_ALLOWANCE_RULE',information7
17701                 ) indicator
17702      FROM per_grades pgr,per_all_assignments_f paaf
17703     WHERE paaf.assignment_id = c_assignment_id
17704       AND c_effective_date BETWEEN paaf.effective_start_date AND paaf.effective_end_date
17705       AND paaf.grade_id = pgr.grade_id
17706       AND pgr.information_category = 'GB_PQP_PENSERV_GRADE_INFO';
17707 
17708 l_tab_allowances t_allowance_eles;
17709 l_return_value varchar2(1);
17710 l_exists varchar2(1);
17711 i                           NUMBER;
17712 l_error_value        number;
17713 l_count  number;
17714 l_proc_name          varchar2(60) := g_proc_name || 'Get_Allowance_Code_New';
17715 
17716 Begin
17717      -- hr_utility.set_location('Entering: '||l_proc_name, 5);
17718      debug_enter(l_proc_name);
17719      debug('p_allowance_type :'||p_allowance_type, 10);
17720 
17721 
17722 
17723      IF p_allowance_type = 'LONDON_ALLOWANCE_RULE' THEN
17724        IF g_lon_all_grd_src = 'Y' THEN
17725          NULL;
17726        ELSE
17727         l_tab_allowances := g_tab_lon_aln_eles;
17728        END IF;
17729      	OPEN csr_grade_val(p_assignment_id,p_effective_date,p_allowance_type);
17730      	FETCH csr_grade_val INTO l_return_value;
17731      	CLOSE csr_grade_val;
17732      	IF l_return_value IS NOT NULL THEN
17733            return l_return_value;
17734         END IF;
17735      ELSIF p_allowance_type = 'SPECIAL_ALLOWANCE_RULE' THEN
17736        IF g_spl_all_grd_src = 'Y' THEN
17737          NULL;
17738        ELSE
17739          l_tab_allowances := g_tab_spl_aln_eles;
17740        END IF;
17741      	OPEN csr_grade_val(p_assignment_id,p_effective_date,p_allowance_type);
17742      	FETCH csr_grade_val INTO l_return_value;
17743      	CLOSE csr_grade_val;
17744      	IF l_return_value IS NOT NULL THEN
17745            return l_return_value;
17746         END IF;
17747      ELSE
17748        l_error_value := pqp_gb_tp_extract_functions.raise_extract_error
17749                          (p_business_group_id => g_business_group_id
17750                          ,p_assignment_id     => p_assignment_id
17751                          ,p_error_text        =>'BEN_93024_EXT_TP1_INVALID_ALOW'
17752                          ,p_error_number      => 93024 );
17753      END IF;
17754 
17755 
17756            i := l_tab_allowances.FIRST;
17757            l_count := 1; -- initialize the lop counter..
17758 
17759            WHILE i IS NOT NULL
17760            LOOP
17761              OPEN csr_ele_entry_exists (p_assignment_id
17762                                        ,l_tab_allowances(i).element_type_id
17763                                        ,p_effective_date
17764                                        );
17765              FETCH csr_ele_entry_exists INTO l_exists;
17766              IF csr_ele_entry_exists%FOUND THEN
17767                 debug('Management Element Type: '||TO_CHAR(i), 160+l_count/100);
17768                 l_return_value
17769                   := TO_CHAR(l_tab_allowances(i).salary_scale_code);
17770                 CLOSE csr_ele_entry_exists;
17771                 EXIT;
17772              END IF; -- End if of row found check ...
17773              CLOSE csr_ele_entry_exists;
17774              i := l_tab_allowances.NEXT(i);
17775              l_count := l_count + 1;
17776            END LOOP;
17777      debug(l_proc_name, 90);
17778 
17779 
17780      Return l_return_value;
17781 EXCEPTION
17782   WHEN OTHERS THEN
17783     debug_exit(' Others in '||l_proc_name);
17784     RAISE;
17785 End Get_Allowance_Code_New;
17786 
17787 END pqp_gb_t1_pension_extracts;
17788