DBA Data[Home] [Help]

PACKAGE BODY: APPS.JTF_ASSIGN_PUB

Source


1 PACKAGE BODY JTF_ASSIGN_PUB AS
2 /* $Header: jtfamprb.pls 120.18 2010/07/02 11:24:28 vakulkar ship $ */
3 
4 -- ********************************************************************************
5 
6 -- Start of Comments
7 --
8 --      Package Name    : JTF_ASSIGN_PUB
9 --      Purpose         : Joint Task Force Core Foundation Assignment Manager
10 --                        Public APIs. This package is for finding the
11 --                        a resource based on the customer preferences
12 --                        or territory preferences and the availability of
13 --                        the resource in the specified time frame.
14 --      Procedures      : (See below for specification)
15 --      Notes           : This package is publicly available for use
16 --      History         : 11/02/99 ** VVUYYURU ** Vijay Vuyyuru ** created
17 --                        Created the procedure for Tasks
18 --
19 --                      : 12/02/99 ** VVUYYURU ** Vijay Vuyyuru **
20 --                        Added the procedure for Service Requests
21 --
22 --                      : 01/02/00 ** VVUYYURU ** Vijay Vuyyuru **
23 --                        Added the procedure for Opportunities
24 --
25 --                      : 01/12/00 ** VVUYYURU ** Vijay Vuyyuru **
26 --                        Added the procedure for Leads
27 --
28 --                      : 02/02/00 ** VVUYYURU ** Vijay Vuyyuru **
29 --                        Added the procedure for Defect Management System
30 --
31 --                      : 03/20/00 ** VVUYYURU ** Vijay Vuyyuru **
32 --                        Added Dynamic SQL for the cs_contacts_v
33 --
34 --                      : 03/23/00 ** VVUYYURU ** Vijay Vuyyuru **
35 --                        Added Dynamic SQL for the cs_incidents_all_vl
36 --
37 --                      : 04/05/00 ** VVUYYURU ** Vijay Vuyyuru **
38 --                        Added Functions to get the default values of
39 --                        FND_API variables
40 --
41 --                      : 04/14/00 ** VVUYYURU ** Vijay Vuyyuru **
42 --                        Added the procedure for Escalations
43 --
44 --                      : 07/20/00 ** VVUYYURU ** Vijay Vuyyuru **
45 --                        Added the new procedure for Leads
46 --                        Replaced the old procedure for Leads
47 --
48 --                      : 10/16/00 ** VVUYYURU ** Vijay Vuyyuru **
49 --                        Added the new record definition for Defects
50 --                        Changed the specification and body with the
51 --                        enhanced DEFECTS code
52 --
53 --                      : 01/09/01 ** VVUYYURU ** Vijay Vuyyuru **
54 --                        Added the code to fetch Installed Base
55 --                        Preferred Engineers
56 --
57 --                      : 02/02/01 ** VVUYYURU ** Vijay Vuyyuru **
58 --                        Added the code to sort the OUT pl/sql table
59 --                        data using the Resource Location.
60 --                        Implemented for TASKS
61 --
62 --                      : 03/21/01 ** VVUYYURU ** Vijay Vuyyuru **
63 --                        Added the code to resolve the Calendar NOT working
64 --                        due to the Uneven Record Numbers which are given in case
65 --                        of NO SHIFT from Calendar which in turn causes error in
66 --                        the Resource Location Sorting.
67 --
68 --                      : 04/16/01 ** VVUYYURU ** Vijay Vuyyuru **
69 --                        Territory changed some record definitions!
70 --                        Hence made relevant code changes for TASKS, SR, SR-TASK
71 --
72 --                      : 01/14/02 ** VVUYYURU ** Vijay Vuyyuru **
73 --                        Added two more parameters to the main API and SR API
74 --                        p_contract_id and p_customer_product_id
75 --                        This is to ensure that the SR need not be saved
76 --                        to fetch the IB and Contracts Preferred Resources
77 --
78 --                      : 02/11/02 ** VVUYYURU ** Vijay Vuyyuru **
79 --                        Added Contract Coverage Type to SR / SR-TASK API
80 --
81 --                      : 05/06/02 ** VVUYYURU ** Vijay Vuyyuru **
82 --                        Added separate procedures for Contracts and IB
83 --                        Modularized the process for Preferred Engineers.
84 --                      : 12/22/05 ** MPADHIAR ** Manas padhiary **
85 --                        Removed Comment to Show error message Bug # 2919389
86 --
87 -- End of Comments
88 
89 -- *******************************************************************************
90 
91 
92 
93 
94 
95 
96 -- *******************************************************************************
97 
98 -- Start of comments
99 
100 -- Functions       : These functions are to get the FND_API default values.
101 
102 -- End of comments
103 
104 -- *******************************************************************************
105 
106   g_contracts_tbl                       JTF_ASSIGN_PUB.AssignResources_tbl_type ;
107   g_ib_tbl                              JTF_ASSIGN_PUB.AssignResources_tbl_type ;
108   g_excluded_resource_tbl               JTF_ASSIGN_PUB.excluded_tbl_type ;
109   g_continuous_work                     VARCHAR2(10);
110 
111 
112   FUNCTION am_miss_num RETURN NUMBER IS
113 
114   BEGIN
115     RETURN (FND_API.g_miss_num);
116   END am_miss_num;
117 
118 
119   FUNCTION am_miss_char RETURN VARCHAR2 IS
120 
121   BEGIN
122     RETURN (FND_API.g_miss_char);
123   END am_miss_char;
124 
125 
126   FUNCTION am_miss_date RETURN DATE IS
127 
128   BEGIN
129     RETURN (FND_API.g_miss_date);
130   END am_miss_date;
131 
132 
133   FUNCTION am_false RETURN VARCHAR2 IS
134 
135   BEGIN
136     RETURN (FND_API.g_false);
137   END am_false;
138 
139 
140   FUNCTION am_true RETURN VARCHAR2 IS
141 
142   BEGIN
143     RETURN (FND_API.g_true);
144   END am_true;
145 
146 
147   FUNCTION am_valid_level_full RETURN VARCHAR2 IS
148 
149   BEGIN
150     RETURN (FND_API.g_valid_level_full);
151   END am_valid_level_full;
152 
153 
154 
155 
156 
157 -- *******************************************************************************
158 
159 -- Start of comments
160 
161 -- Function       : This function is to get the changed Resource Type.
162 --                  It appends/removes the string "RS_"
163 
164 -- End of comments
165 
166 -- *******************************************************************************
167 
168 
169 
170   FUNCTION resource_type_change(p_res_type VARCHAR2) RETURN VARCHAR2 IS
171     l_res_type VARCHAR2(30);
172 
173   BEGIN
174 
175     IF (p_res_type = 'RS_EMPLOYEE') THEN
176       l_res_type := 'EMPLOYEE';
177     ELSIF (p_res_type = 'RS_PARTY') THEN
178       l_res_type := 'PARTY';
179     ELSIF (p_res_type = 'RS_PARTNER') THEN
180       l_res_type := 'PARTNER';
181     ELSIF (p_res_type = 'RS_SUPPLIER') THEN
182       l_res_type := 'SUPPLIER_CONTACT';
183     ELSIF (p_res_type = 'RS_SUPPLIER_CONTACT') THEN
184       l_res_type := 'SUPPLIER_CONTACT';
185     ELSIF (p_res_type = 'RS_OTHER') THEN
186       l_res_type := 'OTHER';
187     ELSIF (p_res_type = 'RS_TBH') THEN
188       l_res_type := 'TBH';
189     ELSIF (p_res_type = 'EMPLOYEE') THEN
190       l_res_type := 'RS_EMPLOYEE';
191     ELSIF (p_res_type = 'PARTY') THEN
192       l_res_type := 'RS_PARTY';
193     ELSIF (p_res_type = 'PARTNER') THEN
194       l_res_type := 'RS_PARTNER';
195     ELSIF (p_res_type = 'SUPPLIER_CONTACT') THEN
196       l_res_type := 'RS_SUPPLIER_CONTACT';
197     ELSIF (p_res_type = 'OTHER') THEN
198       l_res_type := 'RS_OTHER';
199     ELSIF (p_res_type = 'TBH') THEN
200       l_res_type := 'RS_TBH';
201     END IF;
202 
203     RETURN(l_res_type);
204   END resource_type_change;
205 
206 
207 
208 
209 
210 -- *******************************************************************************
211 
212 -- Start of comments
213 
214 -- Function       : This procedure is to get back the Record of Tables
215 --                  into the normal Table of Records.
216 
217 -- End of comments
218 
219 -- *******************************************************************************
220 
221 
222 
223   /* Procedure to write back data from Tables to Table Type variable */
224 
225   PROCEDURE table_copy_in
226     (
227       l_engineer_id                 IN  JTF_NUMBER_TABLE,
228       l_resource_type               IN  JTF_VARCHAR2_TABLE_100,
229       l_primary_flag                IN  JTF_VARCHAR2_TABLE_100,
230       l_resource_class              IN  JTF_VARCHAR2_TABLE_100,
231       l_con_preferred_engineers_tbl OUT NOCOPY JTF_ASSIGN_PUB.prfeng_tbl_type
232     )
233   IS
234 
235     l_ddindx BINARY_INTEGER;
236     l_indx   BINARY_INTEGER;
237 
238   BEGIN
239     IF l_engineer_id IS NOT NULL AND
240        l_engineer_id.count > 0 THEN
241       IF l_engineer_id.count > 0 THEN
242         l_indx := l_engineer_id.first;
243         l_ddindx := 0;
244         WHILE true LOOP
245           l_con_preferred_engineers_tbl(l_ddindx).engineer_id    := l_engineer_id(l_indx);
246           l_con_preferred_engineers_tbl(l_ddindx).resource_type  := l_resource_type(l_indx);
247           l_con_preferred_engineers_tbl(l_ddindx).primary_flag   := nvl(l_primary_flag(l_indx), 'N');
248           l_con_preferred_engineers_tbl(l_ddindx).resource_class := nvl(l_resource_class(l_indx), 'R');
249                                                                     -- resource class is R for Preferred
250                                                                     -- P for Primary and
251                                                                     -- E for Excluded
252           l_ddindx := l_ddindx+1;
253           IF l_engineer_id.last = l_indx THEN
254             exit;
255           END IF;
256           l_indx := l_engineer_id.next(l_indx);
257         END LOOP;
258       END IF;
259     END IF;
260   END table_copy_in;
261 
262 
263 -- procedure to copy resources from one table to another
264 PROCEDURE table_copy(p_from_table     IN JTF_ASSIGN_PUB.AssignResources_tbl_type,
265                      x_to_table       IN OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type)
266 IS
267 
268 i number := 0;
269 k number := 0;
270 BEGIN
271 
272    if( x_to_table.count <= 0)
273    then
274       i := 0;
275    else
276     i := x_to_table.last + 1;
277    end if;
278 
279    IF(p_from_table.count > 0)
280    THEN
281       k := p_from_table.first;
282 
283       While(k <= p_from_table.last)
284       loop
285           x_to_table(i) := p_from_table(k);
286           i := i + 1;
287           k := k + 1;
288       end loop;
289    END IF;
290 
291 END table_copy;
292 
293 
294 -- procedure to remove excluded resources
295 PROCEDURE remove_excluded(x_res_tbl     IN OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
296                           x_exc_res     IN OUT NOCOPY JTF_ASSIGN_PUB.excluded_tbl_type)
297 IS
298 
299 i number := 0;
300 k number := 0;
301 l_count  number := 0;
302 l_exclude varchar2(1) := 'N';
303 l_resource_tbl JTF_ASSIGN_PUB.AssignResources_tbl_type;
304 BEGIN
305    l_resource_tbl.delete;
306    if(x_exc_res.count > 0 and x_res_tbl.count > 0)
307    then
308       for  i in x_res_tbl.first..x_res_tbl.last
309       loop
310          l_exclude := 'N';
311          for k in x_exc_res.first..x_exc_res.last
312          loop
313             if(x_exc_res(k).resource_id = x_res_tbl(i).resource_id and
314                                     x_exc_res(k).resource_type = x_res_tbl(i).resource_type)
315             then
316                l_exclude := 'Y';
317                exit;
318             end if;
319          end loop; -- end of loop for x_exc_res
320         if(l_exclude = 'N')
321          then
322             l_resource_tbl(l_count) := x_res_tbl(i);
323             l_count := l_count + 1;
324          end if; -- end if l_exclude check
325       end loop; -- end of loop for x_res_tbl
326      x_res_tbl.delete;
327      x_res_tbl := l_resource_tbl;
328    end if; -- end of count check for both tables
329 
330 END remove_excluded;
331 
332 
333 
334 -- *******************************************************************************
335 
336 -- Start of comments
337 
338 -- Function       : This procedure is to sort the pl/sql table of resources
339 --                  based on the Resource Location.
340 
341 -- End of comments
342 
343 -- *******************************************************************************
344 
345 
346 
347   PROCEDURE quick_sort_resource_loc
348     (
349       p_left   INTEGER,
350       p_right  INTEGER,
351       p_list   IN OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type
352     )
353   IS
354 
355     i       INTEGER;
356     j       INTEGER;
357     l_left  INTEGER := p_left;
358     l_right INTEGER := p_right;
359     l_current_node JTF_ASSIGN_PUB.AssignResources_rec_type;
360     l_dummy_node   JTF_ASSIGN_PUB.AssignResources_rec_type;
361 
362   BEGIN
363 
364     IF (l_right > l_left) THEN
365       l_current_node := p_list(l_right);
366       i := l_left -1;
367       j := l_right;
368       LOOP
369         LOOP
370           i := i +1;
371           --dbms_output.put_line('Entered LOOP 1');
372           --dbms_output.put_line('i is : '||to_char(i));
373           IF (p_list(i).location < l_current_node.location) THEN
374             null;
375           ELSE
376             exit;
377           END IF;
378 
379           IF (i >= p_list.count) THEN
380             exit;
381           END IF;
382         END LOOP;
383 
384         LOOP
385           j := j -1;
386           --dbms_output.put_line('Entered LOOP 2');
387           --dbms_output.put_line('j is : '||to_char(j));
388           IF (p_list(j).location > l_current_node.location) THEN
389             null;
390           ELSE
391             exit;
392           END IF;
393 
394           IF (j <= 0) THEN
395             exit;
396           END IF;
397 
398         END LOOP;
399 
400         IF (i >= j) THEN
401           exit;
402         END IF;
403 
404         l_dummy_node  := p_list(i);
405         p_list(i)     := p_list(j);
406         p_list(j)     := l_dummy_node;
407       END LOOP;
408 
409       l_dummy_node    := p_list(i);
410       p_list(i)       := p_list(l_right);
411       p_list(l_right) := l_dummy_node;
412 
413       quick_sort_resource_loc(l_left, i-1,     p_list);
414       quick_sort_resource_loc(i+1,    l_right, p_list);
415 
416     END IF;
417   END quick_sort_resource_loc;
418 
419 
420  -- *******************************************************************************
421 
422 -- Start of comments
423 
424 -- bug/Enhancement : 6453896
425 -- Function        : This procedure was added to sort territories dependend on
426 --		     Territory Ranking returned by Territories product.
427 --		     It is used in Get_ASSIGN_RESOURCES procedure. This procedure
428 --		     will sort only when rank is returned by Territories.
429 -- Added by	   : sdwived2
430 --
431 -- End of comments
432 
433 -- *******************************************************************************
434 
435 
436   PROCEDURE quick_sort_terr_rank
437     ( p_left   INTEGER,
438       p_right  INTEGER,
439       p_list   IN OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type
440     )
441   IS
442 
443     i       INTEGER;
444     j       INTEGER;
445     l_left  INTEGER := p_left;
446     l_right INTEGER := p_right;
447     l_current_node JTF_ASSIGN_PUB.AssignResources_rec_type;
448     l_dummy_node   JTF_ASSIGN_PUB.AssignResources_rec_type;
449 
450   BEGIN
451 
452     IF (l_right > l_left) THEN
453       l_current_node := p_list(l_right);
454       i := l_left -1;
455       j := l_right;
456       LOOP
457         LOOP
458           i := i +1;
459           dbms_output.put_line('Entered LOOP 1');
460           dbms_output.put_line('i is : '||to_char(i));
461           dbms_output.put_line('i current node : '||l_current_node.terr_rank);
462           IF (p_list(i).terr_rank > l_current_node.terr_rank) THEN
463             null;
464           ELSE
465             exit;
466           END IF;
467 
468           IF (i >= p_list.count) THEN
469             exit;
470           END IF;
471         END LOOP;
472 
473         LOOP
474           j := j -1;
475           dbms_output.put_line('Entered LOOP 2');
476           dbms_output.put_line('j is : '||to_char(j));
477           dbms_output.put_line('j current node : '||l_current_node.terr_rank);
478           IF (j <= 0) THEN
479             dbms_output.put_line('when j <=0');
480             exit;
481           END IF;
482           IF (p_list(j).terr_rank < l_current_node.terr_rank)  THEN
483             dbms_output.put_line('Inside if');
484             null;
485           ELSE
486            dbms_output.put_line('Inside else before exit');
487             exit;
488           END IF;
489 
490 
491 
492         END LOOP;
493 
494         IF (i >= j) THEN
495           dbms_output.put_line('when i >=j');
496           exit;
497         END IF;
498         dbms_output.put_line('inside j loop before ');
499         dbms_output.put_line('inside j loop before '||p_list(i).terr_rank);
500         dbms_output.put_line('inside j loop before '||p_list(j).terr_rank);
501         l_dummy_node  := p_list(i);
502         p_list(i)     := p_list(j);
503         p_list(j)     := l_dummy_node;
504         dbms_output.put_line('inside j loop after '||p_list(i).terr_rank);
505         dbms_output.put_line('inside j loop after '||p_list(j).terr_rank);
506       END LOOP;
507 
508       l_dummy_node    := p_list(i);
509       p_list(i)       := p_list(l_right);
510       p_list(l_right) := l_dummy_node;
511         dbms_output.put_line('  after i  '||p_list(i).terr_rank);
512         dbms_output.put_line(' after l_right  '||p_list(l_right).terr_rank);
513         dbms_output.put_line('Values passed are  left '||l_left);
514         dbms_output.put_line('Values passed are  right '||to_char(i-1));
515 
516       quick_sort_terr_rank(l_left, i-1,     p_list);
517       quick_sort_terr_rank(i+1,    l_right, p_list);
518 
519     END IF;
520   END quick_sort_terr_rank;
521 
522 /**************** Start of addition by SBARAT on 11/01/2005 for Enh 4112155**************/
523 
524 -- *******************************************************************************
525 
526 -- Start of comments
527 
528 -- Function       : This procedure is to assign values of component/subcomponent fields
529 --                  to Territory's record type dynamically, so that there will not be
530 --                  any compilation error if reqd patch of Territory not applied in env.
531 
532 -- End of comments
533 
534 -- *******************************************************************************
535 
536 
537   PROCEDURE Terr_Qual_Dyn_Assign
538      (
539        p_sr_assign_rec       IN     JTF_ASSIGN_PUB.JTF_Serv_Req_rec_type,
540        p_sr_task_assign_rec  IN     JTF_ASSIGN_PUB.JTF_Srv_Task_rec_type
541      )
542   IS
543 
544   BEGIN
545 
546   EXECUTE IMMEDIATE
547   '
548   BEGIN
549 
550   JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type.SQUAL_NUM23:=:1;
551   JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type.SQUAL_NUM24:=:2;
552 
553   JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type.SQUAL_NUM23:=:3;
554   JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type.SQUAL_NUM24:=:4;
555 
556   END;
557   '
558   USING
559     IN  p_sr_assign_rec.ITEM_COMPONENT,
560     IN  p_sr_assign_rec.ITEM_SUBCOMPONENT,
561     IN  p_sr_task_assign_rec.ITEM_COMPONENT,
562     IN  p_sr_task_assign_rec.ITEM_SUBCOMPONENT;
563 
564   EXCEPTION
565         When OTHERS Then
566           NULL;
567 
568   END Terr_Qual_Dyn_Assign;
569 
570 
571 
572 -- *******************************************************************************
573 
574 -- Start of comments
575 
576 -- Function       : This procedure is to check dynamically whether a value passed to
577 --                  component,subcomponent fields. Used dynamic SQL to overcome AM's
578 --                  build dependancy on Territory for these fields.
579 
580 -- End of comments
581 
582 -- *******************************************************************************
583 
584 
585   PROCEDURE Terr_Qual_Dyn_Check
586      (
587       p_sr_comp_sub        OUT  NOCOPY  Varchar2,
588       p_sr_task_comp_sub   OUT  NOCOPY  Varchar2)
589   IS
590 
591   BEGIN
592 
593   EXECUTE IMMEDIATE
594   '
595   BEGIN
596   If (JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type.SQUAL_NUM23 IS NOT NULL)
597      Or (JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type.SQUAL_NUM24 IS NOT NULL)
598   Then
599   :1:=''P'';
600   End If;
601 
602   If (JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type.SQUAL_NUM23 IS NOT NULL)
603      Or (JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type.SQUAL_NUM24 IS NOT NULL)
604   Then
605   :2:=''P'';
606   End If;
607   End;
608   '
609   USING
610     OUT p_sr_comp_sub,
611     OUT p_sr_task_comp_sub;
612 
613   EXCEPTION
614        When OTHERS Then
615             NULL;
616   END Terr_Qual_Dyn_Check;
617 
618 /**************** End of addition by SBARAT on 11/01/2005 for Enh 4112155**************/
619 
620 
621 -- *******************************************************************************
622 
623 -- Start of comments
624 
625 -- Function       : This procedure is to fetch the Resources in a particular
626 --                  group based on the ID of the Group which has been passed.
627 
628 -- End of comments
629 
630 -- *******************************************************************************
631 
632 
633 
634   PROCEDURE get_group_resource(p_group_id              IN NUMBER,
635                                x_assign_resources_tbl  IN OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type)
636   IS
637 
638     CURSOR check_mem_cur
639            (
640              l_group_id    NUMBER,
641              l_resource_id NUMBER
642            ) IS
643       SELECT 'Y'
644         FROM  jtf_rs_group_members mem
645        WHERE  mem.group_id    = l_group_id
646          AND  mem.resource_id = l_resource_id
647          AND  nvl(mem.delete_flag , 'N') <> 'Y';
648 
649     l_found   VARCHAR2(1) := 'N';
650     l_count   NUMBER      := 0;
651     i         NUMBER      := 0;
652     l_assign_resource_tbl JTF_ASSIGN_PUB.AssignResources_tbl_type;
653 
654 
655   BEGIN
656     --FOR i IN 1..x_assign_resources_tbl.COUNT
657     -- changed loop criterion for bug 3284857. on 1st Dec 2003
658    IF(x_assign_resources_tbl.COUNT > 0)
659    THEN
660     FOR i IN x_assign_resources_tbl.FIRST..x_assign_resources_tbl.LAST
661     LOOP
662 
663       IF (x_assign_resources_tbl(i).resource_type not in ('RS_TEAM', 'RS_GROUP')) THEN
664 
665         l_found := 'N';
666         OPEN check_mem_cur(p_group_id,
667                            x_assign_resources_tbl(i).resource_id);
668         FETCH check_mem_cur into l_found;
669         CLOSE check_mem_cur;
670 
671         IF (l_found = 'Y') THEN
672 
673           l_count := l_count + 1;
674           l_assign_resource_tbl(l_count).resource_id           := x_assign_resources_tbl(i).resource_id;
675           l_assign_resource_tbl(l_count).resource_type         := x_assign_resources_tbl(i).resource_type;
676           l_assign_resource_tbl(l_count).terr_rsc_id           := x_assign_resources_tbl(i).terr_rsc_id;
677           l_assign_resource_tbl(l_count).role                  := x_assign_resources_tbl(i).role ;
678           l_assign_resource_tbl(l_count).start_date            := x_assign_resources_tbl(i).start_date;
679           l_assign_resource_tbl(l_count).end_date              := x_assign_resources_tbl(i).end_date;
680           l_assign_resource_tbl(l_count).shift_construct_id    := x_assign_resources_tbl(i).shift_construct_id;
681           l_assign_resource_tbl(l_count).terr_id               := x_assign_resources_tbl(i).terr_id ;
682           l_assign_resource_tbl(l_count).terr_name             := x_assign_resources_tbl(i).terr_name;
683 	  -- ================code added for bug 6453896=============
684 	  l_assign_resource_tbl(l_count).terr_rank             := x_assign_resources_tbl(i).terr_rank;
685 	  -- ================End for addition of code===============
686           l_assign_resource_tbl(l_count).primary_contact_flag  := x_assign_resources_tbl(i).primary_contact_flag;
687           l_assign_resource_tbl(l_count).primary_flag          := x_assign_resources_tbl(i).primary_flag;
688           l_assign_resource_tbl(l_count).travel_time           := x_assign_resources_tbl(i).travel_time;
689           l_assign_resource_tbl(l_count).travel_uom            := x_assign_resources_tbl(i).travel_uom;
690           l_assign_resource_tbl(l_count).preference_type       := x_assign_resources_tbl(i).preference_type;
691           l_assign_resource_tbl(l_count).full_access_flag      := x_assign_resources_tbl(i).full_access_flag;
692           l_assign_resource_tbl(l_count).group_id              := x_assign_resources_tbl(i).group_id;
693           l_assign_resource_tbl(l_count).location              := x_assign_resources_tbl(i).location;
694           l_assign_resource_tbl(l_count).trans_object_id       := x_assign_resources_tbl(i).trans_object_id;
695           l_assign_resource_tbl(l_count).support_site_id       := x_assign_resources_tbl(i).support_site_id;
696           l_assign_resource_tbl(l_count).support_site_name     := x_assign_resources_tbl(i).support_site_name;
697           l_assign_resource_tbl(l_count).web_availability_flag := x_assign_resources_tbl(i).web_availability_flag;
698           l_assign_resource_tbl(l_count).skill_level           := x_assign_resources_tbl(i).skill_level;
699           l_assign_resource_tbl(l_count).skill_name            := x_assign_resources_tbl(i).skill_name;
700           l_assign_resource_tbl(l_count).resource_source       := x_assign_resources_tbl(i).resource_source;
701 
702         END IF;
703 
704       /******************** Start of addition by SBARAT on 04/05/2006 for bug# 5205277********************/
705       ELSE
706 
707           l_count := l_count + 1;
708           l_assign_resource_tbl(l_count).resource_id           := x_assign_resources_tbl(i).resource_id;
709           l_assign_resource_tbl(l_count).resource_type         := x_assign_resources_tbl(i).resource_type;
710           l_assign_resource_tbl(l_count).terr_rsc_id           := x_assign_resources_tbl(i).terr_rsc_id;
711           l_assign_resource_tbl(l_count).role                  := x_assign_resources_tbl(i).role ;
712           l_assign_resource_tbl(l_count).start_date            := x_assign_resources_tbl(i).start_date;
713           l_assign_resource_tbl(l_count).end_date              := x_assign_resources_tbl(i).end_date;
714           l_assign_resource_tbl(l_count).shift_construct_id    := x_assign_resources_tbl(i).shift_construct_id;
715           l_assign_resource_tbl(l_count).terr_id               := x_assign_resources_tbl(i).terr_id ;
716           l_assign_resource_tbl(l_count).terr_name             := x_assign_resources_tbl(i).terr_name;
717 	  -- ================code added for bug 6453896=============
718 	  l_assign_resource_tbl(l_count).terr_rank             := x_assign_resources_tbl(i).terr_rank;
719 	  -- ================End for addition of code===============
720           l_assign_resource_tbl(l_count).primary_contact_flag  := x_assign_resources_tbl(i).primary_contact_flag;
721           l_assign_resource_tbl(l_count).primary_flag          := x_assign_resources_tbl(i).primary_flag;
722           l_assign_resource_tbl(l_count).travel_time           := x_assign_resources_tbl(i).travel_time;
723           l_assign_resource_tbl(l_count).travel_uom            := x_assign_resources_tbl(i).travel_uom;
724           l_assign_resource_tbl(l_count).preference_type       := x_assign_resources_tbl(i).preference_type;
725           l_assign_resource_tbl(l_count).full_access_flag      := x_assign_resources_tbl(i).full_access_flag;
726           l_assign_resource_tbl(l_count).group_id              := x_assign_resources_tbl(i).group_id;
727           l_assign_resource_tbl(l_count).location              := x_assign_resources_tbl(i).location;
728           l_assign_resource_tbl(l_count).trans_object_id       := x_assign_resources_tbl(i).trans_object_id;
729           l_assign_resource_tbl(l_count).support_site_id       := x_assign_resources_tbl(i).support_site_id;
730           l_assign_resource_tbl(l_count).support_site_name     := x_assign_resources_tbl(i).support_site_name;
731           l_assign_resource_tbl(l_count).web_availability_flag := x_assign_resources_tbl(i).web_availability_flag;
732           l_assign_resource_tbl(l_count).skill_level           := x_assign_resources_tbl(i).skill_level;
733           l_assign_resource_tbl(l_count).skill_name            := x_assign_resources_tbl(i).skill_name;
734           l_assign_resource_tbl(l_count).resource_source       := x_assign_resources_tbl(i).resource_source;
735 
736       END IF;
737       /******************** End of addition by SBARAT on 04/05/2006 for bug# 5205277********************/
738 
739     END LOOP;
740 
741     x_assign_resources_tbl.delete;
742     x_assign_resources_tbl := l_assign_resource_tbl;
743    end if; -- end of count check
744   END get_group_resource;
745 
746 
747 
748 
749 
750 -- *******************************************************************************
751 
752 -- Start of comments
753 
754 -- Function       : This procedure is to fetch the Groups and Teams of any
755 --                  particular USAGE Type.
756 
757 -- End of comments
758 
759 -- *******************************************************************************
760 
761 
762 
763   PROCEDURE get_usage_resource(p_usage                 IN VARCHAR2,
764                                x_assign_resources_tbl  IN OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type)
765   IS
766 
767     CURSOR grp_usg_cur
768            (
769              l_group_id IN NUMBER,
770              l_usage    IN VARCHAR2
771            ) IS
772       SELECT 'Y'
773         FROM jtf_rs_group_usages
774        WHERE group_id = l_group_id
775          AND usage    = l_usage;
776 
777     CURSOR team_usg_cur
778            (
779              l_team_id IN NUMBER,
780              l_usage   IN VARCHAR2
781            ) IS
782       SELECT 'Y'
783         FROM jtf_rs_team_usages
784        WHERE team_id = l_team_id
785          AND usage   = l_usage;
786 
787     l_found   VARCHAR2(1) := 'N';
788     l_count   NUMBER      := 0;
789     i         NUMBER      := 0;
790     l_assign_resource_tbl JTF_ASSIGN_PUB.AssignResources_tbl_type;
791 
792   BEGIN
793     i := x_assign_resources_tbl.FIRST;
794 
795 --    FOR i IN x_assign_resources_tbl.FIRST..x_assign_resources_tbl.COUNT
796     WHILE (i <=  x_assign_resources_tbl.LAST)
797     LOOP
798 
799       IF (x_assign_resources_tbl(i).resource_type in ('RS_TEAM' , 'RS_GROUP')) THEN
800 
801         l_found := 'N';
802         IF (x_assign_resources_tbl(i).resource_type  = 'RS_GROUP') THEN
803 
804            OPEN  grp_usg_cur(x_assign_resources_tbl(i).resource_id,
805                              p_usage);
806            FETCH grp_usg_cur into l_found;
807            CLOSE grp_usg_cur;
808         ELSIF (x_assign_resources_tbl(i).resource_type  = 'RS_TEAM') THEN
809            OPEN  team_usg_cur( x_assign_resources_tbl(i).resource_id,
810                                p_usage);
811            FETCH team_usg_cur into l_found;
812            CLOSE team_usg_cur;
813         END IF;
814 
815         IF (l_found = 'Y') THEN
816 
817           l_count := l_count + 1;
818           l_assign_resource_tbl(l_count).resource_id           := x_assign_resources_tbl(i).resource_id;
819           l_assign_resource_tbl(l_count).resource_type         := x_assign_resources_tbl(i).resource_type;
820           l_assign_resource_tbl(l_count).terr_rsc_id           := x_assign_resources_tbl(i).terr_rsc_id;
821           l_assign_resource_tbl(l_count).role                  := x_assign_resources_tbl(i).role ;
822           l_assign_resource_tbl(l_count).start_date            := x_assign_resources_tbl(i).start_date;
823           l_assign_resource_tbl(l_count).end_date              := x_assign_resources_tbl(i).end_date;
824           l_assign_resource_tbl(l_count).shift_construct_id    := x_assign_resources_tbl(i).shift_construct_id;
825           l_assign_resource_tbl(l_count).terr_id               := x_assign_resources_tbl(i).terr_id ;
826           l_assign_resource_tbl(l_count).terr_name             := x_assign_resources_tbl(i).terr_name;
827           l_assign_resource_tbl(l_count).terr_rank             := x_assign_resources_tbl(i).terr_rank;
828           l_assign_resource_tbl(l_count).primary_contact_flag  := x_assign_resources_tbl(i).primary_contact_flag;
829           l_assign_resource_tbl(l_count).primary_flag          := x_assign_resources_tbl(i).primary_flag;
830           l_assign_resource_tbl(l_count).travel_time           := x_assign_resources_tbl(i).travel_time;
831           l_assign_resource_tbl(l_count).travel_uom            := x_assign_resources_tbl(i).travel_uom;
832           l_assign_resource_tbl(l_count).preference_type       := x_assign_resources_tbl(i).preference_type;
833           l_assign_resource_tbl(l_count).primary_contact_flag  := x_assign_resources_tbl(i).primary_contact_flag;
834           l_assign_resource_tbl(l_count).full_access_flag      := x_assign_resources_tbl(i).full_access_flag;
835           l_assign_resource_tbl(l_count).group_id              := x_assign_resources_tbl(i).group_id;
836           l_assign_resource_tbl(l_count).location              := x_assign_resources_tbl(i).location;
837           l_assign_resource_tbl(l_count).trans_object_id       := x_assign_resources_tbl(i).trans_object_id;
838           l_assign_resource_tbl(l_count).support_site_id       := x_assign_resources_tbl(i).support_site_id;
839           l_assign_resource_tbl(l_count).support_site_name     := x_assign_resources_tbl(i).support_site_name;
840           l_assign_resource_tbl(l_count).web_availability_flag := x_assign_resources_tbl(i).web_availability_flag;
841           l_assign_resource_tbl(l_count).skill_level           := x_assign_resources_tbl(i).skill_level;
842           l_assign_resource_tbl(l_count).skill_name            := x_assign_resources_tbl(i).skill_name;
843           l_assign_resource_tbl(l_count).resource_source       := x_assign_resources_tbl(i).resource_source;
844 
845 
846         END IF;
847 
848         i := x_assign_resources_tbl.next(i);
849 
850       ELSE
851         l_count := l_count + 1;
852         l_assign_resource_tbl(l_count).resource_id           := x_assign_resources_tbl(i).resource_id;
853         l_assign_resource_tbl(l_count).resource_type         := x_assign_resources_tbl(i).resource_type;
854         l_assign_resource_tbl(l_count).terr_rsc_id           := x_assign_resources_tbl(i).terr_rsc_id;
855         l_assign_resource_tbl(l_count).role                  := x_assign_resources_tbl(i).role ;
856         l_assign_resource_tbl(l_count).start_date            := x_assign_resources_tbl(i).start_date;
857         l_assign_resource_tbl(l_count).end_date              := x_assign_resources_tbl(i).end_date;
858         l_assign_resource_tbl(l_count).shift_construct_id    := x_assign_resources_tbl(i).shift_construct_id;
859         l_assign_resource_tbl(l_count).terr_id               := x_assign_resources_tbl(i).terr_id ;
860         l_assign_resource_tbl(l_count).terr_name             := x_assign_resources_tbl(i).terr_name;
861         l_assign_resource_tbl(l_count).terr_rank             := x_assign_resources_tbl(i).terr_rank;
862         l_assign_resource_tbl(l_count).primary_contact_flag  := x_assign_resources_tbl(i).primary_contact_flag;
863         l_assign_resource_tbl(l_count).primary_flag          := x_assign_resources_tbl(i).primary_flag;
864         l_assign_resource_tbl(l_count).travel_time           := x_assign_resources_tbl(i).travel_time;
865         l_assign_resource_tbl(l_count).travel_uom            := x_assign_resources_tbl(i).travel_uom;
866         l_assign_resource_tbl(l_count).preference_type       := x_assign_resources_tbl(i).preference_type;
867         l_assign_resource_tbl(l_count).primary_contact_flag  := x_assign_resources_tbl(i).primary_contact_flag;
868         l_assign_resource_tbl(l_count).full_access_flag      := x_assign_resources_tbl(i).full_access_flag;
869         l_assign_resource_tbl(l_count).group_id              := x_assign_resources_tbl(i).group_id;
870         l_assign_resource_tbl(l_count).location              := x_assign_resources_tbl(i).location;
871         l_assign_resource_tbl(l_count).trans_object_id       := x_assign_resources_tbl(i).trans_object_id;
872         l_assign_resource_tbl(l_count).support_site_id       := x_assign_resources_tbl(i).support_site_id;
873         l_assign_resource_tbl(l_count).support_site_name     := x_assign_resources_tbl(i).support_site_name;
874         l_assign_resource_tbl(l_count).web_availability_flag := x_assign_resources_tbl(i).web_availability_flag;
875         l_assign_resource_tbl(l_count).skill_level           := x_assign_resources_tbl(i).skill_level;
876         l_assign_resource_tbl(l_count).skill_name            := x_assign_resources_tbl(i).skill_name;
877         l_assign_resource_tbl(l_count).resource_source       := x_assign_resources_tbl(i).resource_source;
878 
879 
880         i := x_assign_resources_tbl.next(i);
881       END IF;
882 
883     END LOOP;
884 
885     x_assign_resources_tbl.delete;
886     x_assign_resources_tbl := l_assign_resource_tbl;
887 
888 
889   END get_usage_resource;
890 
891 
892 -- Calendar call to determine availability of a resource
893 -- The api will be called after Contracts/IB/Territory preferred/qualified resource has been selected
894 -- The api will be called only when p_calendar_flag = 'Y'. None of the other api's should now call JTF_CALENDAR_PUB.GET_AVAILABLE_SLOT
895 PROCEDURE get_available_resources
896             (
897               p_init_msg_list                 IN  VARCHAR2,
898               p_calendar_flag                 IN  VARCHAR2,
899               p_effort_duration               IN  NUMBER,
900               p_effort_uom                    IN  VARCHAR2,
901               p_planned_start_date            IN  DATE,
902               p_planned_end_date              IN  DATE,
903               p_continuous_task               IN  VARCHAR2,
904               x_return_status                 IN  OUT NOCOPY VARCHAR2,
905               x_msg_count                     IN  OUT NOCOPY NUMBER,
906               x_msg_data                      IN  OUT NOCOPY VARCHAR2,
907               x_assign_resources_tbl          IN  OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type
908             )
909   IS
910 
911 
912     l_return_status_1               VARCHAR2(10);
913     l_api_name                      VARCHAR2(100)  := 'GET_AVAILABLE_RESOURCES';
914     l_api_name_1                    VARCHAR2(60)   := 'GET_AVAILABLE_RESOURCES';
915     l_api_version                   NUMBER         := 1.0;
916     l_status                        VARCHAR2(30);
917     l_industry                      VARCHAR2(30);
918 
919     l_return_status                 VARCHAR2(10);
920     l_msg_count                     NUMBER;
921     l_msg_data                      VARCHAR2(2000);
922 
923 
924     l_current_record                INTEGER  := 0;
925     l_current_rec                   INTEGER  := 0;
926     l_pref_record                   INTEGER  := 0;
927 
928     l_travel_time                   NUMBER        := 0;
929     l_travel_uom                    VARCHAR2(10)  := 'HR';
930 
931     l_x_planned_start_date          DATE;
932     l_x_planned_end_date            DATE;
933     l_x_shift_construct_id          NUMBER;
934     l_x_availability_type           VARCHAR2(60);
935     l_uom_hour                      VARCHAR2(200);
936     l_effort_duration               NUMBER;
937 
938     l_resources_tbl                 JTF_ASSIGN_PUB.AssignResources_tbl_type;
939 
940 
941 
942   BEGIN
943     -- initialize the variables
944     x_return_status := fnd_api.g_ret_sts_success;
945     l_current_rec := 0;
946     l_resources_tbl.delete;
947 
948     -- Added by SBARAT on 21/04/2005 for Bug-4300801
949     -- This conversion is done only before calling JTF_CALENDAR_PUB
950      /* to handle the conversion of duration to hour */
951     l_uom_hour  := nvl(fnd_profile.value('JTF_AM_TASK_HOUR'), 'HR');
952     if(nvl(p_effort_uom, l_uom_hour) <> l_uom_hour)
953     then
954          l_effort_duration :=  inv_convert.inv_um_convert(
955                                    item_id => NULL,
956                                    precision => 2,
957                                    from_quantity => p_effort_duration,
958                                    from_unit => p_effort_uom,
959                                    to_unit   => l_uom_hour, --'HR',
960                                    from_name => NULL,
961                                    to_name   => NULL);
962     else
963         l_effort_duration := p_effort_duration;
964     end if;
965 
966     -- if the in table has any resources, then this check should continue
967     IF x_assign_resources_tbl.COUNT > 0 THEN
968 
969       l_current_record := x_assign_resources_tbl.FIRST;
970 
971       WHILE l_current_record <= x_assign_resources_tbl.LAST
972       LOOP
973         -- if the calendar flag = Y then this should continue. This is a doublecheck as the calling api will check this
974         -- also
975         IF (p_calendar_flag = 'Y') THEN
976           -- change the API Name temporarily so that in case of unexpected error
977           -- it is properly caught
978           l_api_name := l_api_name||'-JTF_CALENDAR_PUB';
979           l_return_status_1 := x_return_status ;
980 
981           -- This api returns the first available slot for the work duration within the available dates
982           JTF_CALENDAR_PUB.GET_AVAILABLE_SLOT(
983                P_API_VERSION        => l_api_version,
984                P_INIT_MSG_LIST      => p_init_msg_list,
985                P_RESOURCE_ID        => x_assign_resources_tbl(l_current_record).resource_id,
986                P_RESOURCE_TYPE      => x_assign_resources_tbl(l_current_record).resource_type,
987                P_START_DATE_TIME    => p_planned_start_date,
988                P_END_DATE_TIME      => p_planned_end_date,
989                P_DURATION           => l_effort_duration, --p_effort_duration,
990                X_RETURN_STATUS      => x_return_status,
991                X_MSG_COUNT          => x_msg_count,
992                X_MSG_DATA           => x_msg_data,
993                X_SLOT_START_DATE    => l_x_planned_start_date,
994                X_SLOT_END_DATE      => l_x_planned_end_date,
995                X_SHIFT_CONSTRUCT_ID => l_x_shift_construct_id,
996                X_AVAILABILITY_TYPE  => l_x_availability_type
997             );
998 
999           -- set back the API name to original name
1000           l_api_name := l_api_name_1;
1001 
1002           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
1003             -- Unexpected Execution Error from call to Calendar
1004             fnd_message.set_name('JTF', 'JTF_AM_ERROR_CAL_API');
1005             fnd_msg_pub.add;
1006             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
1007 			   --  12/22/05 ** MPADHIAR ** Manas padhiary **
1008 			   --  Removed Comment to Show error message Bug # 2919389
1009                RAISE fnd_api.g_exc_error;
1010             ELSE
1011 			   RAISE fnd_api.g_exc_unexpected_error;
1012             END IF;
1013           END IF;
1014 
1015           -- Put the records into the PL/SQL Table.
1016           -- if the returned shift_construct_id is not null then the resource has an available slot.
1017           IF (l_x_shift_construct_id IS NOT NULL) THEN
1018               l_resources_tbl(l_current_rec).terr_rsc_id := NULL;
1019               l_resources_tbl(l_current_rec).resource_id :=
1020                                     x_assign_resources_tbl(l_current_record).resource_id;
1021               l_resources_tbl(l_current_rec).resource_type:=
1022                                    x_assign_resources_tbl(l_current_record).resource_type;
1023               l_resources_tbl(l_current_rec).role         := NULL;
1024 
1025               IF (l_travel_uom like 'HR%') THEN
1026                  l_resources_tbl(l_current_rec).start_date :=
1027                                      l_x_planned_start_date + l_travel_time/24;
1028                  l_resources_tbl(l_current_rec).end_date   :=
1029                                      l_x_planned_end_date   + l_travel_time/24;
1030               ELSIF (l_travel_uom like 'MI%') THEN
1031                  l_resources_tbl(l_current_rec).start_date :=
1032                                      l_x_planned_start_date + l_travel_time/1440;
1033                  l_resources_tbl(l_current_rec).end_date   :=
1034                                      l_x_planned_end_date   + l_travel_time/1440;
1035               ELSIF (l_travel_uom like 'S%') THEN
1036                  l_resources_tbl(l_current_rec).start_date :=
1037                                      l_x_planned_start_date + l_travel_time/86400;
1038                  l_resources_tbl(l_current_rec).end_date   :=
1039                                      l_x_planned_end_date   + l_travel_time/86400;
1040               END IF;
1041 
1042               l_resources_tbl(l_current_rec).shift_construct_id    := l_x_shift_construct_id;
1043               l_resources_tbl(l_current_rec).terr_id               := x_assign_resources_tbl(l_current_record).terr_id;
1044               l_resources_tbl(l_current_rec).terr_name             := x_assign_resources_tbl(l_current_record).terr_name;
1045 	      -- ================code added for bug 6453896=============
1046               l_resources_tbl(l_current_rec).terr_rank             := x_assign_resources_tbl(l_current_record).terr_rank;
1047 	      -- ================End for addition of code===============
1048               l_resources_tbl(l_current_rec).preference_type       := x_assign_resources_tbl(l_current_record).preference_type;
1049               l_resources_tbl(l_current_rec).primary_flag          := x_assign_resources_tbl(l_current_record).primary_flag;
1050               l_resources_tbl(l_current_rec).primary_contact_flag  := x_assign_resources_tbl(l_current_record).primary_contact_flag;
1051               l_resources_tbl(l_current_rec).full_access_flag      := x_assign_resources_tbl(l_current_record).full_access_flag;
1052               l_resources_tbl(l_current_rec).group_id              := x_assign_resources_tbl(l_current_record).group_id;
1053               l_resources_tbl(l_current_rec).location              := x_assign_resources_tbl(l_current_record).location;
1054               l_resources_tbl(l_current_rec).trans_object_id       := x_assign_resources_tbl(l_current_record).trans_object_id;
1055               l_resources_tbl(l_current_rec).support_site_id       := x_assign_resources_tbl(l_current_record).support_site_id;
1056               l_resources_tbl(l_current_rec).support_site_name     := x_assign_resources_tbl(l_current_record).support_site_name;
1057               l_resources_tbl(l_current_rec).web_availability_flag := x_assign_resources_tbl(l_current_record).web_availability_flag;
1058               l_resources_tbl(l_current_rec).skill_level           := x_assign_resources_tbl(l_current_record).skill_level;
1059               l_resources_tbl(l_current_rec).skill_name            := x_assign_resources_tbl(l_current_record).skill_name;
1060               l_resources_tbl(l_current_rec).resource_source        := x_assign_resources_tbl(l_current_record).resource_source;
1061 
1062               l_current_rec := l_current_rec + 1;
1063           END IF; -- End of shift_construct_id not null check
1064 
1065         END IF; -- Calendar Flag is NO
1066 
1067         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
1068       END LOOP;
1069 
1070       -- assign the available resources back to the out table
1071       x_assign_resources_tbl.delete;
1072       x_assign_resources_tbl := l_resources_tbl;
1073 
1074     END IF;
1075 
1076   EXCEPTION
1077     WHEN FND_API.G_EXC_ERROR THEN
1078       x_return_status := FND_API.G_RET_STS_ERROR ;
1079       FND_MSG_PUB.Count_And_Get
1080       (
1081         p_count => x_msg_count,
1082         p_data  => x_msg_data
1083       );
1084 
1085     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1086       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1087       FND_MSG_PUB.Count_And_Get
1088       (
1089         p_count => x_msg_count,
1090         p_data  => x_msg_data
1091       );
1092 
1093     WHEN OTHERS THEN
1094       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
1095       fnd_message.set_token('P_SQLCODE',SQLCODE);
1096       fnd_message.set_token('P_SQLERRM',SQLERRM);
1097       fnd_message.set_token('P_API_NAME',l_api_name);
1098       FND_MSG_PUB.add;
1099 
1100       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1101       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
1102         FND_MSG_PUB.Add_Exc_Msg
1103         (
1104           G_PKG_NAME,
1105           l_api_name
1106         );
1107       END IF;
1108 
1109       FND_MSG_PUB.Count_And_Get
1110       (
1111         p_count => x_msg_count,
1112         p_data  => x_msg_data
1113        );
1114 
1115   END get_available_resources;
1116 
1117 
1118   -- Overloaded procedure for calendar call
1119 -- The api will be called after Contracts/IB/Territory preferred/qualified resource has been selected
1120 -- The api will be called only when p_calendar_flag = 'Y'. None of the other api's should now call JTF_CALENDAR_PUB.GET_AVAILABLE_SLOT
1121 PROCEDURE get_available_resources
1122             (
1123               p_init_msg_list                 IN  VARCHAR2,
1124               p_calendar_flag                 IN  VARCHAR2,
1125               p_effort_duration               IN  NUMBER,
1126               p_effort_uom                    IN  VARCHAR2,
1127               p_breakdown                     IN  NUMBER,
1128               p_breakdown_uom                 IN  VARCHAR2,
1129               p_planned_start_date            IN  DATE,
1130               p_planned_end_date              IN  DATE,
1131               p_continuous_task               IN  VARCHAR2,
1132               x_return_status                 IN  OUT NOCOPY VARCHAR2,
1133               x_msg_count                     IN  OUT NOCOPY NUMBER,
1134               x_msg_data                      IN  OUT NOCOPY VARCHAR2,
1135               x_assign_resources_tbl          IN  OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
1136 	      --Added for Bug # 5573916
1137 	      p_calendar_check                IN  VARCHAR2 DEFAULT 'Y'
1138 	      --Added for Bug # 5573916 Ends here
1139             )
1140   IS
1141 
1142 
1143     l_return_status_1               VARCHAR2(10);
1144     l_api_name                      VARCHAR2(100)  := 'GET_AVAILABLE_RESOURCES';
1145     l_api_name_1                    VARCHAR2(60)   := 'GET_AVAILABLE_RESOURCES';
1146     l_api_version                   NUMBER         := 1.0;
1147     l_status                        VARCHAR2(30);
1148     l_industry                      VARCHAR2(30);
1149 
1150     l_return_status                 VARCHAR2(10);
1151     l_msg_count                     NUMBER;
1152     l_msg_data                      VARCHAR2(2000);
1153 
1154 
1155     l_current_record                INTEGER  := 0;
1156     l_current_rec                   INTEGER  := 0;
1157     l_pref_record                   INTEGER  := 0;
1158 
1159     l_travel_time                   NUMBER        := 0;
1160     l_travel_uom                    VARCHAR2(10)  := 'HR';
1161 
1162     l_x_planned_start_date          DATE;
1163     l_x_planned_end_date            DATE;
1164     l_x_shift_construct_id          NUMBER;
1165     l_x_availability_type           VARCHAR2(60);
1166 
1167     l_resources_tbl                 JTF_ASSIGN_PUB.AssignResources_tbl_type;
1168 
1169      TYPE l_slots_rec               IS RECORD
1170     (slot_value                     NUMBER);
1171 
1172     Type l_slots_tbl               IS TABLE of l_slots_rec
1173                                    INDEX BY BINARY_INTEGER;
1174     l_slots                        l_slots_tbl;
1175 
1176     l_effort_duration              NUMBER := p_effort_duration;
1177     l_effort_duration_1            NUMBER := p_effort_duration;
1178     l_breakdown                    NUMBER := p_breakdown;
1179     i                              NUMBER ;
1180     l_temp_start_date              DATE;
1181     l_temp_end_date                DATE;
1182     l_avail_count                  NUMBER;
1183     l_uom_hour                     VARCHAR2(200);
1184     l_avail_resource               JTF_ASSIGN_PUB.Avail_tbl_type;
1185     l_temp_count                   NUMBER := 0;
1186     l_planned_end_date             DATE;
1187 
1188 
1189     /*procedure ts(v varchar2)
1190     is
1191       pragma autonomous_transaction;
1192     begin
1193       insert into test_values values(v);
1194       commit;
1195     end;*/
1196   BEGIN
1197     -- initialize the variables
1198     x_return_status := fnd_api.g_ret_sts_success;
1199     l_current_rec := 0;
1200     l_resources_tbl.delete;
1201     JTF_ASSIGN_PUB.g_resource_avail.delete;
1202 
1203 
1204     /* to handle the conversion of duration to hour */
1205     l_uom_hour  := nvl(fnd_profile.value('JTF_AM_TASK_HOUR'), 'HR');
1206 
1207 
1208 
1209     if(nvl(p_effort_uom, l_uom_hour) <> l_uom_hour)
1210     then
1211          l_effort_duration :=  inv_convert.inv_um_convert(
1212                                    item_id => NULL,
1213                                    precision => 2,
1214                                    from_quantity => p_effort_duration,
1215                                    from_unit => p_effort_uom,
1216                                    to_unit   => l_uom_hour, --'HR',
1217                                    from_name => NULL,
1218                                    to_name   => NULL);
1219     else
1220 
1221          l_effort_duration := p_effort_duration;
1222 
1223     end if;
1224 
1225     l_effort_duration := nvl(l_effort_duration, 1);
1226 
1227 
1228      /* to handle the conversion of breakdown duration to hour */
1229     if(nvl(p_breakdown_uom, l_uom_hour) <> l_uom_hour)
1230         AND p_breakdown is not null
1231     then
1232          l_breakdown :=  inv_convert.inv_um_convert(
1233                                    item_id => NULL,
1234                                    precision => 2,
1235                                    from_quantity => p_breakdown,
1236                                    from_unit => p_breakdown_uom,
1237                                    to_unit   => l_uom_hour, --'HR',
1238                                    from_name => NULL,
1239                                    to_name   => NULL);
1240     else
1241         l_breakdown := p_breakdown;
1242     end if;
1243 
1244     -- if the in table has any resources, then this check should continue
1245     IF x_assign_resources_tbl.COUNT > 0 THEN
1246 	--Added for Bug # 5573916
1247 	--Calendar check won't be done if p_calendar_check = 'N' Where as p_calendar_flag will be continued to used as
1248 	--filter resource based on available calendar time slot
1249 	IF (p_calendar_check  = 'N') THEN
1250              l_current_rec := x_assign_resources_tbl.FIRST;
1251 	     WHILE l_current_rec <= x_assign_resources_tbl.LAST
1252 		  LOOP
1253 		    x_assign_resources_tbl(l_current_rec).terr_rsc_id           := NULL;
1254 		    x_assign_resources_tbl(l_current_rec).role                  := NULL;
1255 		    x_assign_resources_tbl(l_current_rec).start_date            := NULL;
1256 		    x_assign_resources_tbl(l_current_rec).end_date              := NULL;
1257 		    x_assign_resources_tbl(l_current_rec).shift_construct_id    := NULL;
1258 		    l_current_rec := l_current_rec + 1;
1259              END LOOP;
1260 	ELSE
1261         --Added for Bug # 5573916 Ends here
1262 
1263     -- break up the duration if breakdown is given
1264     IF( (p_breakdown is not null) and (nvl(l_breakdown , 0) > 0) AND (nvl(l_breakdown,0) < l_effort_duration))
1265     THEN
1266        l_effort_duration_1 := l_effort_duration;
1267        i := 0;
1268        While (l_effort_duration_1 > 0)
1269        LOOP
1270           l_slots(i).slot_value := l_breakdown;
1271           l_effort_duration_1 := l_effort_duration_1 - l_breakdown;
1272           IF(l_effort_duration_1 > l_breakdown)
1273           THEN
1274               l_breakdown := l_breakdown;
1275           ELSE
1276               l_breakdown := l_effort_duration_1;
1277           END IF;
1278           i := i + 1;
1279         END LOOP;  -- end of l_effort_duration check
1280      END IF; -- end of breakdown check
1281 
1282 
1283      IF(l_effort_duration <= nvl(l_breakdown,l_effort_duration))
1284      THEN
1285 
1286        l_current_record := x_assign_resources_tbl.FIRST;
1287        l_avail_count    := 0;
1288        jtf_assign_pub.g_resource_avail.delete;
1289 
1290 
1291 
1292 
1293       IF p_planned_start_date=p_planned_end_date
1294       then
1295               l_planned_end_date := p_planned_start_date + 1/86400;
1296               l_effort_duration := 1/86400;
1297       else
1298          l_planned_end_date := p_planned_end_date;
1299       end if;
1300 
1301        WHILE l_current_record <= x_assign_resources_tbl.LAST
1302        LOOP
1303         -- if the calendar flag = Y then this should continue. This is a doublecheck as the calling api will check this
1304         -- also
1305         -- Irrespective of whether the calendar flag is Y or N
1306         -- IF (p_calendar_flag = 'Y') THEN
1307         -- change the API Name temporarily so that in case of unexpected error
1308         -- it is properly caught
1309           l_api_name := l_api_name||'-JTF_CALENDAR_PUB';
1310           l_return_status_1 := x_return_status ;
1311 
1312           -- This api returns the first available slot for the work duration within the available dates
1313 
1314 
1315 
1316 
1317           JTF_CALENDAR_PUB.GET_AVAILABLE_SLOT
1318             (
1319                P_API_VERSION        => l_api_version,
1320                P_INIT_MSG_LIST      => p_init_msg_list,
1321                P_RESOURCE_ID        => x_assign_resources_tbl(l_current_record).resource_id,
1322                P_RESOURCE_TYPE      => x_assign_resources_tbl(l_current_record).resource_type,
1323                P_START_DATE_TIME    => p_planned_start_date,
1324                P_END_DATE_TIME      => l_planned_end_date,
1325                P_DURATION           => l_effort_duration,
1326                X_RETURN_STATUS      => x_return_status,
1327                X_MSG_COUNT          => x_msg_count,
1328                X_MSG_DATA           => x_msg_data,
1329                X_SLOT_START_DATE    => l_x_planned_start_date,
1330                X_SLOT_END_DATE      => l_x_planned_end_date,
1331                X_SHIFT_CONSTRUCT_ID => l_x_shift_construct_id,
1332                X_AVAILABILITY_TYPE  => l_x_availability_type
1333             );
1334 
1335           -- set back the API name to original name
1336           l_api_name := l_api_name_1;
1337 
1338           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
1339             -- Unexpected Execution Error from call to Calendar
1340             fnd_message.set_name('JTF', 'JTF_AM_ERROR_CAL_API');
1341             fnd_msg_pub.add;
1342 
1343             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
1344               --  12/22/05 ** MPADHIAR ** Manas padhiary **
1345 			  --  Removed Comment to Show error message Bug # 2919389
1346 			  RAISE fnd_api.g_exc_error;
1347 
1348             ELSE
1349 			  RAISE fnd_api.g_exc_unexpected_error;
1350 
1351             END IF;
1352           END IF;
1353 
1354           -- Put the records into the PL/SQL Table.
1355           -- if the returned shift_construct_id is not null then the resource has an available slot.
1356           IF (l_x_shift_construct_id IS NOT NULL) THEN
1357               l_resources_tbl(l_current_rec).terr_rsc_id := NULL;
1358               l_resources_tbl(l_current_rec).resource_id :=
1359                                     x_assign_resources_tbl(l_current_record).resource_id;
1360               l_resources_tbl(l_current_rec).resource_type:=
1361                                    x_assign_resources_tbl(l_current_record).resource_type;
1362               l_resources_tbl(l_current_rec).role         := NULL;
1363 
1364               IF (l_travel_uom like 'HR%') THEN
1365                  l_resources_tbl(l_current_rec).start_date :=
1366                                      l_x_planned_start_date + l_travel_time/24;
1367                  l_resources_tbl(l_current_rec).end_date   :=
1368                                      l_x_planned_end_date   + l_travel_time/24;
1369               ELSIF (l_travel_uom like 'MI%') THEN
1370                  l_resources_tbl(l_current_rec).start_date :=
1371                                      l_x_planned_start_date + l_travel_time/1440;
1372                  l_resources_tbl(l_current_rec).end_date   :=
1373                                      l_x_planned_end_date   + l_travel_time/1440;
1374               ELSIF (l_travel_uom like 'S%') THEN
1375                  l_resources_tbl(l_current_rec).start_date :=
1376                                      l_x_planned_start_date + l_travel_time/86400;
1377                  l_resources_tbl(l_current_rec).end_date   :=
1378                                      l_x_planned_end_date   + l_travel_time/86400;
1379               END IF;
1380 
1381               l_resources_tbl(l_current_rec).shift_construct_id    := l_x_shift_construct_id;
1382               l_resources_tbl(l_current_rec).terr_id               := x_assign_resources_tbl(l_current_record).terr_id;
1383               l_resources_tbl(l_current_rec).terr_name             := x_assign_resources_tbl(l_current_record).terr_name;
1384 	      -- ================code added for bug 6453896=============
1385               l_resources_tbl(l_current_rec).terr_rank             := x_assign_resources_tbl(l_current_record).terr_rank;
1386 	      -- ================End for addition of code===============
1387               l_resources_tbl(l_current_rec).preference_type       := x_assign_resources_tbl(l_current_record).preference_type;
1388               l_resources_tbl(l_current_rec).primary_flag          := x_assign_resources_tbl(l_current_record).primary_flag;
1389               l_resources_tbl(l_current_rec).primary_contact_flag  := x_assign_resources_tbl(l_current_record).primary_contact_flag;
1390               l_resources_tbl(l_current_rec).full_access_flag      := x_assign_resources_tbl(l_current_record).full_access_flag;
1391               l_resources_tbl(l_current_rec).group_id              := x_assign_resources_tbl(l_current_record).group_id;
1392               l_resources_tbl(l_current_rec).location              := x_assign_resources_tbl(l_current_record).location;
1393               l_resources_tbl(l_current_rec).trans_object_id       := x_assign_resources_tbl(l_current_record).trans_object_id;
1394               l_resources_tbl(l_current_rec).support_site_id       := x_assign_resources_tbl(l_current_record).support_site_id;
1395               l_resources_tbl(l_current_rec).support_site_name     := x_assign_resources_tbl(l_current_record).support_site_name;
1396               l_resources_tbl(l_current_rec).web_availability_flag := x_assign_resources_tbl(l_current_record).web_availability_flag;
1397               l_resources_tbl(l_current_rec).skill_level           := x_assign_resources_tbl(l_current_record).skill_level;
1398               l_resources_tbl(l_current_rec).skill_name            := x_assign_resources_tbl(l_current_record).skill_name;
1399               l_resources_tbl(l_current_rec).resource_source       := x_assign_resources_tbl(l_current_record).resource_source;
1400 
1401               -- assign values to availability table
1402               JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).resource_id        := l_resources_tbl(l_current_rec).resource_id;
1403               JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).resource_type      := l_resources_tbl(l_current_rec).resource_type;
1404               JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).start_date         := l_resources_tbl(l_current_rec).start_date;
1405               JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).end_date           := l_resources_tbl(l_current_rec).end_date;
1406               JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).shift_construct_id := l_resources_tbl(l_current_rec).shift_construct_id;
1407 
1408               l_current_rec := l_current_rec + 1;
1409               l_avail_count := l_avail_count + 1;
1410 
1411           -- added the else part to return null if the resource does not have a shift also
1412           ELSE
1413               l_resources_tbl(l_current_rec).terr_rsc_id           := NULL;
1414               l_resources_tbl(l_current_rec).resource_id           := x_assign_resources_tbl(l_current_record).resource_id;
1415               l_resources_tbl(l_current_rec).resource_type         := x_assign_resources_tbl(l_current_record).resource_type;
1416               l_resources_tbl(l_current_rec).role                  := NULL;
1417               l_resources_tbl(l_current_rec).start_date            := NULL;
1418               l_resources_tbl(l_current_rec).end_date              := NULL;
1419               l_resources_tbl(l_current_rec).shift_construct_id    := NULL;
1420               l_resources_tbl(l_current_rec).terr_id               := x_assign_resources_tbl(l_current_record).terr_id;
1421               l_resources_tbl(l_current_rec).terr_name             := x_assign_resources_tbl(l_current_record).terr_name;
1422 	      -- ================code added for bug 6453896=============
1423               l_resources_tbl(l_current_rec).terr_rank             := x_assign_resources_tbl(l_current_record).terr_rank;
1424 	      -- ================End for addition of code===============
1425               l_resources_tbl(l_current_rec).preference_type       := x_assign_resources_tbl(l_current_record).preference_type;
1426               l_resources_tbl(l_current_rec).primary_flag          := x_assign_resources_tbl(l_current_record).primary_flag;
1427               l_resources_tbl(l_current_rec).primary_contact_flag  := x_assign_resources_tbl(l_current_record).primary_contact_flag;
1428               l_resources_tbl(l_current_rec).full_access_flag      := x_assign_resources_tbl(l_current_record).full_access_flag;
1429               l_resources_tbl(l_current_rec).group_id              := x_assign_resources_tbl(l_current_record).group_id;
1430               l_resources_tbl(l_current_rec).location              := x_assign_resources_tbl(l_current_record).location;
1431               l_resources_tbl(l_current_rec).trans_object_id       := x_assign_resources_tbl(l_current_record).trans_object_id;
1432               l_resources_tbl(l_current_rec).support_site_id       := x_assign_resources_tbl(l_current_record).support_site_id;
1433               l_resources_tbl(l_current_rec).support_site_name     := x_assign_resources_tbl(l_current_record).support_site_name;
1434               l_resources_tbl(l_current_rec).web_availability_flag := x_assign_resources_tbl(l_current_record).web_availability_flag;
1435               l_resources_tbl(l_current_rec).skill_level           := x_assign_resources_tbl(l_current_record).skill_level;
1436               l_resources_tbl(l_current_rec).skill_name            := x_assign_resources_tbl(l_current_record).skill_name;
1437               l_resources_tbl(l_current_rec).resource_source       := x_assign_resources_tbl(l_current_record).resource_source;
1438 
1439               l_current_rec := l_current_rec + 1;
1440           END IF; -- End of shift_construct_id not null check
1441 
1442         --END IF; -- Calendar Flag is NO
1443 
1444         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
1445       END LOOP;
1446 
1447       -- assign the available resources back to the out table
1448       x_assign_resources_tbl.delete;
1449       -- if calendar flag is Y then filter out the resources that do not have a available slot.
1450 
1451      IF (p_calendar_flag = 'Y') THEN
1452        l_current_rec := l_resources_tbl.first;
1453        l_current_record := 0;
1454        While(l_current_rec <= l_resources_tbl.LAST)
1455        Loop
1456          IF(l_resources_tbl(l_current_rec).shift_construct_id is not null)
1457          THEN
1458              x_assign_resources_tbl(l_current_record) := l_resources_tbl(l_current_rec);
1459 
1460              l_current_record := l_current_record + 1;
1461          END IF;
1462          l_current_rec  := l_current_rec + 1;
1463        END LOOP;
1464 
1465       ELSE  -- check p_calendar flag = Y
1466          -- else pass back all resources
1467          x_assign_resources_tbl := l_resources_tbl;
1468           l_api_name := l_api_name||'-JTF_CALENDAR_PUB';
1469           l_return_status_1 := x_return_status ;
1470       END IF; -- check p_calendar flag = Y
1471 
1472      ELSE -- check for l_effort_duration <= l_breakdown
1473      -- new logic to get resources when work has been broken down into seperate slots
1474       l_current_rec := x_assign_resources_tbl.first;
1475       l_avail_count     := 0;
1476       While(l_current_rec <= x_assign_resources_tbl.last)
1477       LOOP
1478           l_temp_start_date := p_planned_start_date;
1479           l_temp_end_date   := p_planned_end_date;
1480 
1481           FOR i in l_slots.first..l_slots.last
1482           LOOP
1483              l_api_name        := l_api_name||'-JTF_CALENDAR_PUB';
1484              l_return_status_1 := x_return_status ;
1485 
1486               l_x_shift_construct_id := null;
1487               l_x_planned_start_date := null;
1488               l_x_planned_end_date   := null;
1489               l_x_availability_type  := null;
1490              -- This api returns the first available slot for the work duration within the available dates
1491              JTF_CALENDAR_PUB.GET_AVAILABLE_SLOT
1492               (
1493                P_API_VERSION        => l_api_version,
1494                P_INIT_MSG_LIST      => p_init_msg_list,
1495                P_RESOURCE_ID        => x_assign_resources_tbl(l_current_rec).resource_id,
1496                P_RESOURCE_TYPE      => x_assign_resources_tbl(l_current_rec).resource_type,
1497                P_START_DATE_TIME    => l_temp_start_date,
1498                P_END_DATE_TIME      => l_temp_end_date,
1499                P_DURATION           => l_slots(i).slot_value,
1500                X_RETURN_STATUS      => x_return_status,
1501                X_MSG_COUNT          => x_msg_count,
1502                X_MSG_DATA           => x_msg_data,
1503                X_SLOT_START_DATE    => l_x_planned_start_date,
1504                X_SLOT_END_DATE      => l_x_planned_end_date,
1505                X_SHIFT_CONSTRUCT_ID => l_x_shift_construct_id,
1506                X_AVAILABILITY_TYPE  => l_x_availability_type
1507               );
1508 --dbms_output.put_line('Slots are ...'||to_char(l_slots(i).slot_value)||'...'||to_char(l_x_planned_start_date, 'DD-MON-YYYY HH24:MI'));
1509 --dbms_output.put_line('Slots are ...'||to_char(x_assign_resources_tbl(l_current_rec).resource_id)||'...'||to_char(l_x_planned_end_date, 'DD-MON-YYYY HH24:MI'));
1510             -- set back the API name to original name
1511             l_api_name := l_api_name_1;
1512 
1513             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
1514             -- Unexpected Execution Error from call to Calendar
1515                fnd_message.set_name('JTF', 'JTF_AM_ERROR_CAL_API');
1516                fnd_msg_pub.add;
1517                IF (x_return_status = fnd_api.g_ret_sts_error) THEN
1518                   --  12/22/05 ** MPADHIAR ** Manas padhiary **
1519 				  --  Removed Comment to Show error message Bug # 2919389
1520 				  RAISE fnd_api.g_exc_error;
1521 
1522                ELSE
1523                   RAISE fnd_api.g_exc_unexpected_error;
1524                END IF;
1525             END IF;
1526             if(l_x_shift_construct_id is not null)
1527             then
1528                JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).resource_id        := x_assign_resources_tbl(l_current_rec).resource_id;
1529                JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).resource_type      :=x_assign_resources_tbl(l_current_rec).resource_type;
1530                JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).start_date         :=  l_x_planned_start_date;
1531                JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).end_date           :=  l_x_planned_end_date;
1532                JTF_ASSIGN_PUB.g_resource_avail(l_avail_count).shift_construct_id := l_x_shift_construct_id;
1533                l_avail_count     := l_avail_count + 1;
1534                l_temp_start_date := l_x_planned_end_date;
1535                l_temp_end_date   := p_planned_end_date;
1536             else
1537               -- remove the available slots for the resource
1538                l_temp_count := 0;
1539                FOR k in JTF_ASSIGN_PUB.g_resource_avail.first..JTF_ASSIGN_PUB.g_resource_avail.last
1540                LOOP
1541                  IF(JTF_ASSIGN_PUB.g_resource_avail(k).resource_id = x_assign_resources_tbl(l_current_rec).resource_id
1542                     AND JTF_ASSIGN_PUB.g_resource_avail(k).resource_type = x_assign_resources_tbl(l_current_rec).resource_type)
1543                  THEN
1544                      null ; --l_avail_count := l_avail_count - 1;
1545                  ELSE
1546                     l_avail_resource(l_temp_count) := JTF_ASSIGN_PUB.g_resource_avail(k);
1547                     l_temp_count := l_temp_count + 1;
1548                  END IF;
1549                END LOOP;
1550                JTF_ASSIGN_PUB.g_resource_avail.delete;
1551                JTF_ASSIGN_PUB.g_resource_avail :=  l_avail_resource;
1552                l_avail_count := JTF_ASSIGN_PUB.g_resource_avail.last + 1;
1553                exit;
1554              end if; -- end of l_x_shift_contruct_id check
1555 
1556           END LOOP; -- end of check for l_slots
1557           l_current_rec := l_current_rec + 1;
1558       END LOOP; -- end of l_current_rec check
1559 
1560       IF (p_calendar_flag = 'Y') THEN
1561          l_current_rec := x_assign_resources_tbl.first;
1562          l_current_record := 0;
1563          While(l_current_rec <= x_assign_resources_tbl.LAST)
1564          Loop
1565            -- remove records that are not there in g_avail_resource
1566             FOR i IN JTF_ASSIGN_PUB.g_resource_avail.first..JTF_ASSIGN_PUB.g_resource_avail.last
1567              LOOP
1568                IF(JTF_ASSIGN_PUB.g_resource_avail(i).resource_id = x_assign_resources_tbl(l_current_rec).resource_id
1569                 AND JTF_ASSIGN_PUB.g_resource_avail(i).resource_type = x_assign_resources_tbl(l_current_rec).resource_type)
1570               THEN
1571                  l_resources_tbl(l_current_record) := x_assign_resources_tbl(l_current_rec);
1572                  l_resources_tbl(l_current_record).shift_construct_id := JTF_ASSIGN_PUB.g_resource_avail(i).shift_construct_id;
1573                  l_resources_tbl(l_current_record).start_date := JTF_ASSIGN_PUB.g_resource_avail(i).start_date;
1574                  l_resources_tbl(l_current_record).end_date := JTF_ASSIGN_PUB.g_resource_avail(i).end_date;
1575                  l_current_record := l_current_record + 1;
1576                  exit;
1577                END IF;
1578              END LOOP; -- end of loop for i in g_resource_avail first to last
1579              l_current_rec  := l_current_rec + 1;
1580           END LOOP;
1581           x_assign_resources_tbl.delete;
1582           l_current_rec := l_resources_tbl.first;
1583           l_current_record := 0;
1584           While(l_current_rec <= l_resources_tbl.LAST)
1585           Loop
1586             x_assign_resources_tbl(l_current_record) := l_resources_tbl(l_current_rec);
1587             l_current_rec  := l_current_rec + 1;
1588             l_current_record  := l_current_record + 1;
1589           END LOOP;
1590 
1591         ELSE  -- check p_calendar flag = Y
1592          -- else pass back all resources with the start date and end dates of resources with first availability slot dates
1593            l_current_rec := x_assign_resources_tbl.first;
1594            While(l_current_rec <= x_assign_resources_tbl.LAST
1595                 AND JTF_ASSIGN_PUB.g_resource_avail.count > 0)
1596            Loop
1597            -- remove records that are not there in g_avail_resource
1598               FOR i IN JTF_ASSIGN_PUB.g_resource_avail.first..JTF_ASSIGN_PUB.g_resource_avail.last
1599               LOOP
1600                 IF(JTF_ASSIGN_PUB.g_resource_avail(i).resource_id = x_assign_resources_tbl(l_current_rec).resource_id
1601                   AND JTF_ASSIGN_PUB.g_resource_avail(i).resource_type = x_assign_resources_tbl(l_current_rec).resource_type)
1602                 THEN
1603                     x_assign_resources_tbl(l_current_rec).shift_construct_id := JTF_ASSIGN_PUB.g_resource_avail(i).shift_construct_id;
1604                     x_assign_resources_tbl(l_current_rec).start_date         := JTF_ASSIGN_PUB.g_resource_avail(i).start_date;
1605                     x_assign_resources_tbl(l_current_rec).end_date           := JTF_ASSIGN_PUB.g_resource_avail(i).end_date;
1606                     exit;
1607                 END IF;
1608               END LOOP; -- end of loop for i in g_resource_avail first to last
1609               IF(x_assign_resources_tbl(l_current_rec).shift_construct_id IS NULL)
1610               THEN
1611                     x_assign_resources_tbl(l_current_rec).start_date   := NULL;
1612                     x_assign_resources_tbl(l_current_rec).end_date     := NULL;
1613               END IF;
1614               l_current_rec  := l_current_rec + 1;
1615            END LOOP;  -- end of l_current_rec check
1616          END IF; -- check p_calendar flag = Y
1617      END IF; -- check for p_effort_duration <= p_breakdown
1618    --Added for Bug # 5573916
1619    END IF;
1620    --Added for Bug # 5573916 Ends here
1621  END IF;  -- if x_assign_resurces_tbl count > 0 check
1622 
1623   EXCEPTION
1624     WHEN FND_API.G_EXC_ERROR THEN
1625       x_return_status := FND_API.G_RET_STS_ERROR ;
1626       FND_MSG_PUB.Count_And_Get
1627       (
1628         p_count => x_msg_count,
1629         p_data  => x_msg_data
1630       );
1631 
1632     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1633       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1634       FND_MSG_PUB.Count_And_Get
1635       (
1636         p_count => x_msg_count,
1637         p_data  => x_msg_data
1638       );
1639 
1640     WHEN OTHERS THEN
1641       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
1642       fnd_message.set_token('P_SQLCODE',SQLCODE);
1643       fnd_message.set_token('P_SQLERRM',SQLERRM);
1644       fnd_message.set_token('P_API_NAME',l_api_name);
1645       FND_MSG_PUB.add;
1646 
1647       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1648       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
1649         FND_MSG_PUB.Add_Exc_Msg
1650         (
1651           G_PKG_NAME,
1652           l_api_name
1653         );
1654       END IF;
1655 
1656       FND_MSG_PUB.Count_And_Get
1657       (
1658         p_count => x_msg_count,
1659         p_data  => x_msg_data
1660        );
1661 
1662   END get_available_resources;
1663 
1664 
1665 
1666 
1667 
1668 -- *******************************************************************************
1669 
1670 --  Start of comments
1671 
1672 --  Function       : This procedure is to fetch the Contracts Preferred Resources.
1673 --                   This passes out the table of records with the resources.
1674 
1675 --  End of comments
1676 
1677 -- *******************************************************************************
1678 
1679 
1680   PROCEDURE get_contracts_resources
1681             (
1682               p_init_msg_list                 IN  VARCHAR2,
1683               p_contract_id                   IN  NUMBER,
1684               p_calendar_flag                 IN  VARCHAR2,
1685               p_effort_duration               IN  NUMBER,
1686               p_effort_uom                    IN  VARCHAR2,
1687               p_planned_start_date            IN  DATE,
1688               p_planned_end_date              IN  DATE,
1689               p_resource_type                 IN  VARCHAR2,
1690               p_business_process_id           IN  NUMBER,
1691               p_business_process_date         IN  DATE,
1692               x_return_status                 IN  OUT NOCOPY VARCHAR2,
1693               x_msg_count                     IN  OUT NOCOPY NUMBER,
1694               x_msg_data                      IN  OUT NOCOPY VARCHAR2,
1695               x_assign_resources_tbl          IN  OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
1696               x_excluded_tbl                  IN  OUT NOCOPY JTF_ASSIGN_PUB.excluded_tbl_type,
1697 	      --Added for Bug # 5573916
1698 	      p_calendar_check                IN  VARCHAR2 DEFAULT 'Y'
1699 	      --Added for Bug # 5573916 Ends here
1700             )
1701   IS
1702 
1703    -- For bug# 5261565. Checking only whether 'OKS' is installed or not.
1704    -- This is the product that should be minimum installed at the site
1705    -- to get back the resources from contract setup. Don't need to check
1706    -- 'OKC' and 'OKL'. Got it confirmed from Contract team as well.
1707 
1708     CURSOR cur_contracts_installed IS
1709       SELECT application_id
1710         FROM fnd_application
1711        WHERE application_short_name = 'OKS';  -- IN ('OKC', 'OKL', 'OKS');
1712     l_cur_contracts_installed cur_contracts_installed%ROWTYPE;
1713 
1714     l_return_status_1               VARCHAR2(10);
1715     l_api_name                      VARCHAR2(100)  := 'GET_CONTRACTS_RESOURCES';
1716     l_api_name_1                    VARCHAR2(60)  := 'GET_CONTRACTS_RESOURCES';
1717     l_api_version                   NUMBER        := 1.0;
1718     l_status                        VARCHAR2(30);
1719     l_industry                      VARCHAR2(30);
1720 
1721     l_return_status                 VARCHAR2(10);
1722     l_msg_count                     NUMBER;
1723     l_msg_data                      VARCHAR2(2000);
1724 
1725     l_engineer_id                   JTF_NUMBER_TABLE;
1726     l_resource_type                 JTF_VARCHAR2_TABLE_100;
1727     l_primary_flag                  JTF_VARCHAR2_TABLE_100;
1728     l_resource_class                JTF_VARCHAR2_TABLE_100;
1729 
1730     l_current_record                INTEGER  := 0;
1731     l_current_rec                   INTEGER  := 0;
1732     l_pref_record                   INTEGER  := 0;
1733 
1734     l_travel_time                   NUMBER        := 0;
1735     l_travel_uom                    VARCHAR2(10)  := 'HR';
1736 
1737     l_x_planned_start_date          DATE;
1738     l_x_planned_end_date            DATE;
1739     l_x_shift_construct_id          NUMBER;
1740     l_x_availability_type           VARCHAR2(60);
1741 
1742     l_con_preferred_engineers_tbl   JTF_ASSIGN_PUB.prfeng_tbl_type;
1743     l_preferred_engineers_tbl       JTF_ASSIGN_PUB.Preferred_Engineers_tbl_type;
1744 
1745     l_business_process_date         DATE    := p_business_process_date;
1746     l_excl_record                   NUMBER  := 0;
1747 
1748   BEGIN
1749 
1750     x_return_status := fnd_api.g_ret_sts_success;
1751 
1752     OPEN  cur_contracts_installed;
1753     FETCH cur_contracts_installed INTO l_cur_contracts_installed;
1754     CLOSE cur_contracts_installed;
1755 
1756     -- default the date to sysdate if it is null
1757     IF(p_business_process_id is not null and p_business_process_date is null)
1758     THEN
1759        l_business_process_date := sysdate;
1760     END IF;
1761 
1762     IF (l_cur_contracts_installed.application_id IS NULL) THEN
1763       fnd_message.set_name('JTF', 'JTF_AM_CONTRACTS_NOT_INSTALLED');
1764       fnd_msg_pub.add;
1765       RAISE fnd_api.g_exc_error;
1766     ELSE
1767 
1768       IF FND_INSTALLATION.GET
1769            (
1770               l_cur_contracts_installed.application_id,
1771               l_cur_contracts_installed.application_id,
1772               l_status,
1773               l_industry
1774            ) THEN
1775         IF ( UPPER(l_status) <> 'I' ) THEN
1776           fnd_message.set_name('JTF', 'JTF_AM_CONTRACTS_NOT_INSTALLED');
1777           fnd_msg_pub.add;
1778           RAISE fnd_api.g_exc_error;
1779         END IF;
1780       ELSE
1781         fnd_message.set_name('JTF', 'JTF_AM_CONTRACTS_NOT_INSTALLED');
1782         fnd_msg_pub.add;
1783         RAISE fnd_api.g_exc_error;
1784       END IF;
1785     END IF;
1786 
1787     IF(x_excluded_tbl.count > 0)
1788     THEN
1789        l_excl_record    := x_excluded_tbl.last + 1;
1790    ELSE
1791        l_excl_record    := 0;
1792     END IF;
1793 
1794 
1795     IF (p_contract_id IS NOT NULL) THEN
1796 
1797         /*  Also at a later point of time add code to check
1798             for coverage start and end dates */
1799 
1800       EXECUTE IMMEDIATE
1801       '
1802       DECLARE
1803 
1804         l_con_preferred_engineers_tbl  OKS_ENTITLEMENTS_PUB.prfeng_tbl_type;
1805         l_engineer_id                  JTF_NUMBER_TABLE;
1806         l_resource_type                JTF_VARCHAR2_TABLE_100;
1807         l_primary_flag                 JTF_VARCHAR2_TABLE_100;
1808         l_resource_class               JTF_VARCHAR2_TABLE_100;
1809         l_return_status                VARCHAR2(10);
1810         l_msg_count                    NUMBER;
1811         l_msg_data                     VARCHAR2(2000);
1812 
1813         l_ddindx BINARY_INTEGER;
1814         l_indx   BINARY_INTEGER;
1815 
1816       BEGIN
1817 
1818         OKS_ENTITLEMENTS_PUB.Get_Preferred_Engineers
1819         (
1820            p_api_version         => :1,
1821            p_init_msg_list       => :2,
1822            p_contract_line_id    => :3,
1823            p_business_process_id => :9,
1824            p_request_date        => :10,
1825            x_return_status       => l_return_status,
1826            x_msg_count           => l_msg_count,
1827            x_msg_data            => l_msg_data,
1828            x_prf_engineers       => l_con_preferred_engineers_tbl
1829         );
1830 
1831         :4 := l_msg_count;
1832         :5 := l_msg_data;
1833         :6 := l_return_status;
1834 
1835 
1836         IF NOT (l_return_status = fnd_api.g_ret_sts_success) THEN
1837           -- Unexpected Execution Error from call to Contracts API
1838           fnd_message.set_name('||''''||'JTF'||''''||','||''''||'JTF_AM_ERROR_CONTRACTS_API'||''''||');'||
1839          'fnd_msg_pub.add;
1840             IF (l_return_status = fnd_api.g_ret_sts_error) THEN
1841               RAISE fnd_api.g_exc_error;
1842             ELSE
1843               RAISE fnd_api.g_exc_unexpected_error;
1844             END IF;
1845         END IF;
1846 
1847         /* Procedure for Copying out the TABLE contents
1848            INTO local variables to process */
1849 
1850         IF l_con_preferred_engineers_tbl IS NULL OR
1851            l_con_preferred_engineers_tbl.count = 0 THEN
1852           l_engineer_id     := JTF_NUMBER_TABLE();
1853           l_resource_type   := JTF_VARCHAR2_TABLE_100();
1854           l_primary_flag    := JTF_VARCHAR2_TABLE_100();
1855           l_resource_class  := JTF_VARCHAR2_TABLE_100();
1856 
1857         ELSE
1858           l_engineer_id     := JTF_NUMBER_TABLE();
1859           l_resource_type   := JTF_VARCHAR2_TABLE_100();
1860           l_primary_flag    := JTF_VARCHAR2_TABLE_100();
1861           l_resource_class  := JTF_VARCHAR2_TABLE_100();
1862 
1863           IF l_con_preferred_engineers_tbl.count > 0 THEN
1864             l_engineer_id.extend(l_con_preferred_engineers_tbl.count);
1865             l_resource_type.extend(l_con_preferred_engineers_tbl.count);
1866             l_primary_flag.extend(l_con_preferred_engineers_tbl.count);
1867             l_resource_class.extend(l_con_preferred_engineers_tbl.count);
1868 
1869             l_ddindx := l_con_preferred_engineers_tbl.first;
1870             l_indx   := 1;
1871             WHILE true LOOP
1872               l_engineer_id(l_indx)     := l_con_preferred_engineers_tbl(l_ddindx).engineer_id;
1873               l_resource_type(l_indx)   := l_con_preferred_engineers_tbl(l_ddindx).resource_type;
1874               l_primary_flag(l_indx)    := l_con_preferred_engineers_tbl(l_ddindx).primary_flag;
1875               l_resource_class(l_indx)  := l_con_preferred_engineers_tbl(l_ddindx).resource_class;
1876 
1877               l_indx := l_indx+1;
1878 
1879               IF l_con_preferred_engineers_tbl.last = l_ddindx THEN
1880                 exit;
1881               END IF;
1882 
1883               l_ddindx := l_con_preferred_engineers_tbl.next(l_ddindx);
1884             END LOOP;
1885           END IF;
1886         END IF;
1887 
1888         :7 := l_engineer_id;
1889         :8 := l_resource_type;
1890         :11 := l_primary_flag;
1891         :12 := l_resource_class;
1892 
1893       END;
1894       '
1895       USING IN  l_api_version,
1896             IN  p_init_msg_list,
1897             IN  p_contract_id,
1898             IN  p_business_process_id,
1899             IN  l_business_process_date,
1900             OUT l_msg_count,
1901             OUT l_msg_data,
1902             OUT l_return_status,
1903             OUT l_engineer_id,
1904             OUT l_resource_type,
1905             OUT l_primary_flag,
1906             OUT l_resource_class;
1907 
1908       table_copy_in ( l_engineer_id,
1909                       l_resource_type,
1910                       l_primary_flag,
1911                       l_resource_class,
1912                       l_con_preferred_engineers_tbl
1913                     );
1914 
1915       x_return_status  := l_return_status;
1916       x_msg_count      := l_msg_count;
1917       x_msg_data       := l_msg_data;
1918 
1919     END IF; -- p_contract_id IS NOT NULL
1920 
1921 
1922     l_pref_record := 0;
1923     If(x_excluded_tbl.count > 0)
1924     THEN
1925        l_excl_record := x_excluded_tbl.last + 1;
1926     ELSE
1927        l_excl_record := 0;
1928     END IF;
1929 
1930 
1931     IF ( l_con_preferred_engineers_tbl.COUNT > 0 ) THEN
1932 
1933       l_current_record := l_con_preferred_engineers_tbl.FIRST;
1934 
1935       WHILE l_current_record <= l_con_preferred_engineers_tbl.LAST
1936       LOOP
1937        IF(l_con_preferred_engineers_tbl(l_current_record).resource_class in ('P', 'R'))
1938        THEN
1939           IF(
1940             ((p_resource_type = 'RS_INDIVIDUAL' OR p_resource_type is null)
1941               AND (l_con_preferred_engineers_tbl(l_current_record).resource_type in ('RS_EMPLOYEE',
1942                                                                                      'RS_PARTY',
1943                                                                                      'RS_PARTNER')))
1944             OR ((p_resource_type = 'RS_GROUP' OR p_resource_type is null)
1945                  AND (l_con_preferred_engineers_tbl(l_current_record).resource_type = 'RS_GROUP'))
1946             OR ((p_resource_type = 'RS_TEAM' OR p_resource_type is null)
1947                  AND (l_con_preferred_engineers_tbl(l_current_record).resource_type = 'RS_TEAM'))
1948             ) THEN
1949               l_pref_record    := l_pref_record + 1;
1950               l_preferred_engineers_tbl(l_pref_record).engineer_id     :=
1951                                         l_con_preferred_engineers_tbl(l_current_record).engineer_id;
1952               l_preferred_engineers_tbl(l_pref_record).resource_type   :=
1953                                         l_con_preferred_engineers_tbl(l_current_record).resource_type;
1954               IF(( l_con_preferred_engineers_tbl(l_current_record).resource_class = 'P')
1955                  or (l_con_preferred_engineers_tbl(l_current_record).primary_flag = 'Y'))
1956               THEN
1957                   l_preferred_engineers_tbl(l_pref_record).primary_flag   := 'Y';
1958               END IF;
1959               l_preferred_engineers_tbl(l_pref_record).preference_type := 'C';
1960 
1961            END IF;
1962         -- keep the excluded resources in the excluded table
1963         ELSIF(l_con_preferred_engineers_tbl(l_current_record).resource_class = 'E')
1964         THEN
1965           x_excluded_tbl(l_excl_record).resource_id := l_con_preferred_engineers_tbl(l_current_record).engineer_id;
1966           x_excluded_tbl(l_excl_record).resource_type := l_con_preferred_engineers_tbl(l_current_record).resource_type;
1967           l_excl_record := l_excl_record + 1;
1968         END IF;
1969         l_current_record := l_con_preferred_engineers_tbl.NEXT(l_current_record);
1970       END LOOP;
1971     END IF;
1972 
1973 
1974     --l_current_rec := x_assign_resources_tbl.LAST + 1;
1975     IF(x_assign_resources_tbl.COUNT > 0)
1976     THEN
1977        l_current_rec := x_assign_resources_tbl.LAST + 1;
1978     ELSE
1979       l_current_rec := 0;
1980     END IF;
1981 
1982 
1983 
1984     IF l_preferred_engineers_tbl.COUNT > 0 THEN
1985        l_current_record := l_preferred_engineers_tbl.FIRST;
1986        WHILE(l_current_record <=  l_preferred_engineers_tbl.LAST)
1987        LOOP
1988           x_assign_resources_tbl(l_current_rec).terr_rsc_id           := NULL;
1989           x_assign_resources_tbl(l_current_rec).resource_id           :=
1990                                  l_preferred_engineers_tbl(l_current_record).engineer_id;
1991           x_assign_resources_tbl(l_current_rec).resource_type         :=
1992                                  l_preferred_engineers_tbl(l_current_record).resource_type;
1993           x_assign_resources_tbl(l_current_rec).role                  := NULL;
1994           x_assign_resources_tbl(l_current_rec).start_date            := NULL;
1995           x_assign_resources_tbl(l_current_rec).end_date              := NULL;
1996           x_assign_resources_tbl(l_current_rec).shift_construct_id    := NULL;
1997           x_assign_resources_tbl(l_current_rec).terr_id               := NULL;
1998           x_assign_resources_tbl(l_current_rec).terr_name             := NULL;
1999           x_assign_resources_tbl(l_current_rec).preference_type       :=
2000                                  l_preferred_engineers_tbl(l_current_record).preference_type;
2001           x_assign_resources_tbl(l_current_rec).primary_flag       :=
2002                                  l_preferred_engineers_tbl(l_current_record).primary_flag;
2003           x_assign_resources_tbl(l_current_rec).resource_source := 'CONTRACTS';
2004 
2005           l_current_rec    := l_current_rec + 1;
2006           l_current_record := l_preferred_engineers_tbl.NEXT(l_current_record);
2007        END LOOP;
2008 
2009 
2010         -- The calendar flag check will not be done any more. The first available slot will be fetched
2011         -- This is for the preformance bug 3301417. If the calendar flag is Y then the resources will
2012         -- filtered based on availability in the procedure get_available_slot. This change is being done on
2013         -- 16 June 2004
2014       -- IF (p_calendar_flag = 'Y') THEN
2015           -- change the API Name temporarily so that in case of unexpected error
2016           -- it is properly caught
2017           l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
2018           l_return_status_1 := x_return_status ;
2019            -- call the api to check resource availability
2020            get_available_resources
2021             (
2022               p_init_msg_list                 =>  'F',
2023               p_calendar_flag                 =>   p_calendar_flag,
2024               p_effort_duration               =>   p_effort_duration,
2025               p_effort_uom                    =>   p_effort_uom,
2026               p_planned_start_date            =>   p_planned_start_date,
2027               p_planned_end_date              =>   p_planned_end_date,
2028               p_breakdown                     =>   null,
2029               p_breakdown_uom                 =>   null,
2030               p_continuous_task               =>   jtf_assign_pub.g_continuous_work,
2031               x_return_status                 =>   x_return_status,
2032               x_msg_count                     =>   x_msg_count,
2033               x_msg_data                      =>   x_msg_data,
2034               x_assign_resources_tbl          =>   x_assign_resources_tbl,
2035 	      --Added for Bug # 5573916
2036 	      p_calendar_check                =>   p_calendar_check
2037 	      --Added for Bug # 5573916 Ends here
2038 	      );
2039 
2040           -- set back the API name to original name
2041           l_api_name := l_api_name_1;
2042 
2043             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
2044             -- Unexpected Execution Error from call to Get_contracts_resources
2045                fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
2046                fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
2047                fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_CONTRACTS_RESOURCES');
2048                fnd_msg_pub.add;
2049                IF (x_return_status = fnd_api.g_ret_sts_error) THEN
2050                   RAISE fnd_api.g_exc_error;
2051                ELSE
2052                   RAISE fnd_api.g_exc_unexpected_error;
2053                END IF;
2054              END IF; -- end of x_return_status check
2055          -- end if; -- if p_calendar_flag = Y
2056      end if;    --l_preferred_engineers_tbl.COUNT > 0
2057 
2058 
2059   EXCEPTION
2060     WHEN FND_API.G_EXC_ERROR THEN
2061       x_return_status := FND_API.G_RET_STS_ERROR ;
2062       FND_MSG_PUB.Count_And_Get
2063       (
2064         p_count => x_msg_count,
2065         p_data  => x_msg_data
2066       );
2067 
2068     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2069       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2070       FND_MSG_PUB.Count_And_Get
2071       (
2072         p_count => x_msg_count,
2073         p_data  => x_msg_data
2074       );
2075 
2076     WHEN OTHERS THEN
2077       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
2078       fnd_message.set_token('P_SQLCODE',SQLCODE);
2079       fnd_message.set_token('P_SQLERRM',SQLERRM);
2080       fnd_message.set_token('P_API_NAME',l_api_name);
2081       FND_MSG_PUB.add;
2082 
2083       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2084       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
2085         FND_MSG_PUB.Add_Exc_Msg
2086         (
2087           G_PKG_NAME,
2088           l_api_name
2089         );
2090       END IF;
2091 
2092       FND_MSG_PUB.Count_And_Get
2093       (
2094         p_count => x_msg_count,
2095         p_data  => x_msg_data
2096        );
2097 
2098   END get_contracts_resources;
2099 
2100 
2101 
2102 
2103 
2104 -- *******************************************************************************
2105 
2106 --  Start of comments
2107 
2108 --  Function       : This procedure is to fetch the Install Base Preferred Resources.
2109 --                   This passes out the table of records with the resources.
2110 
2111 --  End of comments
2112 
2113 -- *******************************************************************************
2114 
2115 
2116 
2117   PROCEDURE get_ib_resources
2118             (
2119               p_init_msg_list                 IN  VARCHAR2,
2120               p_customer_product_id           IN  NUMBER,
2121               p_calendar_flag                 IN  VARCHAR2,
2122               p_effort_duration               IN  NUMBER,
2123               p_effort_uom                    IN  VARCHAR2,
2124               p_planned_start_date            IN  DATE,
2125               p_planned_end_date              IN  DATE,
2126               p_resource_type                 IN  VARCHAR2,
2127               x_return_status                 IN  OUT NOCOPY  VARCHAR2,
2128               x_msg_count                     IN  OUT NOCOPY  NUMBER,
2129               x_msg_data                      IN  OUT NOCOPY  VARCHAR2,
2130               x_assign_resources_tbl          IN  OUT NOCOPY  JTF_ASSIGN_PUB.AssignResources_tbl_type,
2131               x_excluded_tbl                  IN  OUT NOCOPY JTF_ASSIGN_PUB.excluded_tbl_type,
2132 	      --Added for Bug # 5573916
2133 	      p_calendar_check                IN  VARCHAR2 DEFAULT 'Y'
2134 	      --Added for Bug # 5573916 Ends here
2135             )
2136   IS
2137 
2138     l_return_status_1              VARCHAR2(10);
2139     l_api_name                     VARCHAR2(100)  := 'GET_IB_RESOURCES';
2140     l_api_name_1                   VARCHAR2(60)  := 'GET_IB_RESOURCES';
2141     l_api_version                  NUMBER  := 1.0;
2142 
2143     l_return_status                 VARCHAR2(10);
2144     l_msg_count                     NUMBER;
2145     l_msg_data                      VARCHAR2(2000);
2146 
2147     l_engineer_id                   JTF_NUMBER_TABLE;
2148     l_resource_type                 JTF_VARCHAR2_TABLE_100;
2149 
2150     l_current_record                INTEGER  := 0;
2151     l_current_rec                   INTEGER  := 0;
2152     l_pref_record                   INTEGER  := 0;
2153     l_excl_record                   INTEGER  := 0;
2154 
2155     l_travel_time                   NUMBER        := 0;
2156     l_travel_uom                    VARCHAR2(10)  := 'HR';
2157 
2158     l_x_planned_start_date          DATE;
2159     l_x_planned_end_date            DATE;
2160     l_x_shift_construct_id          NUMBER;
2161     l_x_availability_type           VARCHAR2(60);
2162 
2163     l_ib_preferred_engineers_tbl    JTF_ASSIGN_PUB.prfeng_tbl_type;
2164     l_preferred_engineers_tbl       JTF_ASSIGN_PUB.Preferred_Engineers_tbl_type;
2165 
2166     TYPE DYNAMIC_CUR_TYP            IS REF CURSOR;
2167     cur_csi_utility                 DYNAMIC_CUR_TYP;
2168     cur_cs_contacts                 DYNAMIC_CUR_TYP;
2169     l_dynamic_sql4                  VARCHAR2(2000);
2170     l_dynamic_sql2                  VARCHAR2(2000);
2171 
2172     l_cs_contacts_cp                VARCHAR2(5)  := 'CP';
2173     l_cs_contacts_y                 VARCHAR2(2)  := 'Y' ;
2174     l_cp_id                         NUMBER       :=  p_customer_product_id;
2175     l_flag                          VARCHAR2(2)  := 'Y' ;
2176     l_cs_contacts_rsc_id            NUMBER;
2177     l_cs_contacts_rsc_cat           VARCHAR2(60);
2178     l_primary                       VARCHAR2(2);
2179     l_preferred                     VARCHAR2(2);
2180 
2181     CURSOR cur_ib_resources IS
2182       SELECT resource_id,
2183              'RS_'||category category
2184         FROM  jtf_rs_resource_extns_vl
2185        WHERE  source_id = l_cs_contacts_rsc_id
2186          AND  category  = l_cs_contacts_rsc_cat;
2187 
2188     l_cur_ib_resources  cur_ib_resources%ROWTYPE;
2189 
2190 
2191     CURSOR cur_ib_resources_grp IS
2192       SELECT group_id resource_id,
2193              'RS_GROUP'
2194         FROM jtf_rs_groups_b
2195        WHERE group_id = l_cs_contacts_rsc_id;
2196 
2197 
2198     CURSOR cur_ib_resources_team IS
2199       SELECT team_id resource_id,
2200              'RS_TEAM'
2201         FROM jtf_rs_teams_b
2202        WHERE team_id = l_cs_contacts_rsc_id;
2203 
2204     CURSOR cur_ib_supp_resources IS
2205       SELECT resource_id,
2206              'RS_'||category category
2207         FROM  jtf_rs_resource_extns_vl
2208        WHERE  source_id = l_cs_contacts_rsc_id
2209          AND  category  = 'SUPPLIER_CONTACT';
2210 
2211 
2212   BEGIN
2213 
2214     x_return_status := fnd_api.g_ret_sts_success;
2215 
2216     l_pref_record := 0;
2217 
2218     -- added by sudarsana on 30th nov 2001
2219     l_dynamic_sql4 := 'SELECT CSI_UTILITY_GRP.ib_active_flag() FROM DUAL';
2220 
2221     BEGIN
2222       OPEN  cur_csi_utility FOR l_dynamic_sql4;
2223       FETCH cur_csi_utility INTO l_flag;
2224       CLOSE cur_csi_utility;
2225 
2226     EXCEPTION WHEN OTHERS THEN
2227       l_flag := 'N';
2228     END;
2229 
2230 
2231     -- IF CSI_UTILITY_GRP.ib_active_flag() = 'Y' THEN
2232 
2233     IF (l_flag = 'Y') THEN
2234           l_dynamic_sql2 := 'SELECT  PARTY_ID resource_id, PARTY_SOURCE_TABLE resource_category, primary_flag, preferred_flag'||
2235                            ' FROM    CSI_I_PARTIES'||
2236                            ' WHERE   INSTANCE_ID  = :2'||
2237                           -- ' AND   PARTY_SOURCE_TABLE in ('''||'EMPLOYEE'||''''||','''||'HZ_PARTIES'||''''||','''||'GROUP'||''')'||
2238                            --' AND   PREFERRED_FLAG = :4' ;
2239                            -- changed this line to get excluded resources
2240                            ' AND   PREFERRED_FLAG in('''||'Y'||''''||','''||'E'||''')'||
2241                            ' AND   sysdate <= nvl(active_end_date, sysdate)' ;
2242 
2243       OPEN cur_cs_contacts FOR l_dynamic_sql2 USING --l_cs_contacts_cp,
2244                                                       l_cp_id;
2245                                                     --l_cs_contacts_emp,
2246                                                     --l_cs_contacts_y;
2247     ELSE
2248 
2249       l_dynamic_sql2  :=  ' SELECT resource_id, resource_category, primary_flag, preferred_flag '||
2250                           ' FROM   cs_contacts_v'||
2251                           ' WHERE  source_object_code = :1  AND '||
2252                           ' source_object_id   = :2  AND '||
2253                        -- ' resource_category  = :3  AND '||
2254                           ' preferred_flag     = :4';
2255 
2256 
2257       OPEN cur_cs_contacts FOR l_dynamic_sql2 USING l_cs_contacts_cp,
2258                                                     l_cp_id,
2259                                                  -- l_cs_contacts_emp,
2260                                                     l_cs_contacts_y;
2261     END IF; -- end of CSI_UTILITY_GRP check
2262 
2263 
2264     LOOP
2265 
2266       FETCH cur_cs_contacts INTO  l_cs_contacts_rsc_id,
2267                                   l_cs_contacts_rsc_cat,
2268                                   l_primary,
2269                                   l_preferred;
2270       EXIT WHEN cur_cs_contacts%NOTFOUND;
2271        if(l_primary is null)
2272        then
2273            l_primary := 'N';
2274        end if;
2275 
2276         -- IF cond for category added by sudarsana 30 nov 2001 to map to resource_manager
2277 
2278         IF((l_cs_contacts_rsc_cat = 'HZ_PARTIES') AND
2279            (p_resource_type is null OR p_resource_type = 'RS_INDIVIDUAL')) THEN
2280 
2281 
2282           l_cs_contacts_rsc_cat := 'PARTY';
2283 
2284           OPEN  cur_ib_resources;
2285           LOOP
2286             FETCH cur_ib_resources INTO l_cur_ib_resources;
2287             EXIT WHEN cur_ib_resources%NOTFOUND;
2288 
2289             l_ib_preferred_engineers_tbl(l_pref_record).engineer_id   := l_cur_ib_resources.resource_id;
2290             l_ib_preferred_engineers_tbl(l_pref_record).resource_type := l_cur_ib_resources.category;
2291             l_ib_preferred_engineers_tbl(l_pref_record).primary_flag  := l_primary;
2292             l_ib_preferred_engineers_tbl(l_pref_record).preferred_flag  := l_preferred;
2293             l_pref_record := l_pref_record + 1;
2294           END LOOP;
2295           CLOSE cur_ib_resources;
2296 
2297           l_cs_contacts_rsc_cat := 'PARTNER';
2298 
2299           OPEN  cur_ib_resources;
2300           LOOP
2301             FETCH cur_ib_resources INTO l_cur_ib_resources;
2302             EXIT WHEN cur_ib_resources%NOTFOUND;
2303 
2304             l_ib_preferred_engineers_tbl(l_pref_record).engineer_id   := l_cur_ib_resources.resource_id;
2305             l_ib_preferred_engineers_tbl(l_pref_record).resource_type := l_cur_ib_resources.category;
2306             l_ib_preferred_engineers_tbl(l_pref_record).primary_flag  := l_primary;
2307             l_ib_preferred_engineers_tbl(l_pref_record).preferred_flag  := l_preferred;
2308             l_pref_record := l_pref_record + 1;
2309           END LOOP;
2310           CLOSE cur_ib_resources;
2311 
2312         ELSIF((l_cs_contacts_rsc_cat = 'PO_VENDORS') AND
2313               ( p_resource_type =  'RS_INDIVIDUAL' OR  p_resource_type is null)) THEN
2314 
2315           OPEN  cur_ib_supp_resources;
2316           LOOP
2317             FETCH cur_ib_supp_resources INTO l_cur_ib_resources;
2318             EXIT WHEN cur_ib_supp_resources%NOTFOUND;
2319 
2320             l_ib_preferred_engineers_tbl(l_pref_record).engineer_id   := l_cur_ib_resources.resource_id;
2321             l_ib_preferred_engineers_tbl(l_pref_record).resource_type := l_cur_ib_resources.category;
2322             l_ib_preferred_engineers_tbl(l_pref_record).primary_flag  := l_primary;
2323             l_ib_preferred_engineers_tbl(l_pref_record).preferred_flag  := l_preferred;
2324             l_pref_record := l_pref_record + 1;
2325           END LOOP;
2326           CLOSE cur_ib_supp_resources;
2327         ELSIF((l_cs_contacts_rsc_cat = 'GROUP') AND
2328               ( p_resource_type =  'RS_GROUP' OR  p_resource_type is null)) THEN
2329 
2330           OPEN  cur_ib_resources_grp;
2331           LOOP
2332             FETCH cur_ib_resources_grp INTO l_cur_ib_resources;
2333             EXIT WHEN cur_ib_resources_grp%NOTFOUND;
2334 
2335             l_ib_preferred_engineers_tbl(l_pref_record).engineer_id   := l_cur_ib_resources.resource_id;
2336             l_ib_preferred_engineers_tbl(l_pref_record).resource_type := l_cur_ib_resources.category;
2337             l_ib_preferred_engineers_tbl(l_pref_record).primary_flag  := l_primary;
2338             l_ib_preferred_engineers_tbl(l_pref_record).preferred_flag  := l_preferred;
2339             l_pref_record := l_pref_record + 1;
2340           END LOOP;
2341           CLOSE cur_ib_resources_grp;
2342 
2343         ELSIF((l_cs_contacts_rsc_cat = 'TEAM') AND
2344               (p_resource_type =  'RS_TEAM' OR p_resource_type is null)) THEN
2345 
2346           OPEN  cur_ib_resources_team;
2347           LOOP
2348             FETCH cur_ib_resources_team INTO l_cur_ib_resources;
2349             EXIT WHEN cur_ib_resources_team%NOTFOUND;
2350 
2351             l_ib_preferred_engineers_tbl(l_pref_record).engineer_id   := l_cur_ib_resources.resource_id;
2352             l_ib_preferred_engineers_tbl(l_pref_record).resource_type := l_cur_ib_resources.category;
2353             l_ib_preferred_engineers_tbl(l_pref_record).primary_flag  := l_primary;
2354             l_ib_preferred_engineers_tbl(l_pref_record).preferred_flag  := l_preferred;
2355             l_pref_record := l_pref_record + 1;
2356           END LOOP;
2357           CLOSE cur_ib_resources_team;
2358 
2359         ELSIF(p_resource_type =  'RS_INDIVIDUAL' OR p_resource_type is null) THEN
2360 
2361           OPEN  cur_ib_resources;
2362           LOOP
2363             FETCH cur_ib_resources INTO l_cur_ib_resources;
2364             EXIT WHEN cur_ib_resources%NOTFOUND;
2365 
2366             l_ib_preferred_engineers_tbl(l_pref_record).engineer_id   := l_cur_ib_resources.resource_id;
2367             l_ib_preferred_engineers_tbl(l_pref_record).resource_type := l_cur_ib_resources.category;
2368             l_ib_preferred_engineers_tbl(l_pref_record).primary_flag  := l_primary;
2369             l_ib_preferred_engineers_tbl(l_pref_record).preferred_flag  := l_preferred;
2370             l_pref_record := l_pref_record + 1;
2371           END LOOP;
2372           CLOSE cur_ib_resources;
2373         END IF;
2374     END LOOP;
2375     CLOSE cur_cs_contacts;
2376 
2377 
2378     IF ( l_ib_preferred_engineers_tbl.COUNT > 0 ) THEN
2379 
2380       l_current_record := l_ib_preferred_engineers_tbl.FIRST;
2381       IF(x_excluded_tbl.count > 0)
2382       THEN
2383          l_excl_record    := x_excluded_tbl.last + 1;
2384       ELSE
2385          l_excl_record    := 0;
2386       END IF;
2387 
2388       WHILE l_current_record <= l_ib_preferred_engineers_tbl.LAST
2389       LOOP
2390         IF(l_ib_preferred_engineers_tbl(l_current_record).preferred_flag = 'Y')
2391         THEN
2392             l_preferred_engineers_tbl(l_pref_record).engineer_id     :=
2393                                   l_ib_preferred_engineers_tbl(l_current_record).engineer_id;
2394             l_preferred_engineers_tbl(l_pref_record).resource_type   :=
2395                                   l_ib_preferred_engineers_tbl(l_current_record).resource_type;
2396             l_preferred_engineers_tbl(l_pref_record).preference_type := 'I';
2397             l_preferred_engineers_tbl(l_pref_record).primary_flag    :=
2398                                   l_ib_preferred_engineers_tbl(l_current_record).primary_flag;
2399 
2400             l_pref_record    := l_pref_record + 1;
2401         ELSIF(l_ib_preferred_engineers_tbl(l_current_record).preferred_flag = 'E')
2402         THEN
2403             x_excluded_tbl(l_excl_record).resource_id     :=
2404                                   l_ib_preferred_engineers_tbl(l_current_record).engineer_id;
2405             x_excluded_tbl(l_excl_record).resource_type   :=
2406                                   l_ib_preferred_engineers_tbl(l_current_record).resource_type;
2407 
2408 
2409             l_excl_record    := l_excl_record + 1;
2410 
2411 
2412        END IF; -- end of check for preferred_flag
2413        l_current_record := l_ib_preferred_engineers_tbl.NEXT(l_current_record);
2414       END LOOP;
2415     END IF;
2416 
2417     IF(x_assign_resources_tbl.COUNT > 0)
2418     THEN
2419        l_current_rec := x_assign_resources_tbl.LAST + 1;
2420     ELSE
2421       l_current_rec := 0;
2422     END IF;
2423 
2424 
2425 
2426 
2427     IF l_preferred_engineers_tbl.COUNT > 0 THEN
2428        l_current_record := l_preferred_engineers_tbl.FIRST;
2429        WHILE(l_current_record <=  l_preferred_engineers_tbl.LAST)
2430        LOOP
2431           x_assign_resources_tbl(l_current_rec).terr_rsc_id           := NULL;
2432           x_assign_resources_tbl(l_current_rec).resource_id           :=
2433                                  l_preferred_engineers_tbl(l_current_record).engineer_id;
2434           x_assign_resources_tbl(l_current_rec).resource_type         :=
2435                                  l_preferred_engineers_tbl(l_current_record).resource_type;
2436           x_assign_resources_tbl(l_current_rec).role                  := NULL;
2437           x_assign_resources_tbl(l_current_rec).start_date            := NULL;
2438           x_assign_resources_tbl(l_current_rec).end_date              := NULL;
2439           x_assign_resources_tbl(l_current_rec).shift_construct_id    := NULL;
2440           x_assign_resources_tbl(l_current_rec).terr_id               := NULL;
2441           x_assign_resources_tbl(l_current_rec).terr_name             := NULL;
2442           x_assign_resources_tbl(l_current_rec).preference_type       :=
2443                                  l_preferred_engineers_tbl(l_current_record).preference_type;
2444           x_assign_resources_tbl(l_current_rec).primary_flag       :=
2445                                  l_preferred_engineers_tbl(l_current_record).primary_flag;
2446           x_assign_resources_tbl(l_current_rec).resource_source       := 'IB';
2447 
2448           l_current_rec    := l_current_rec + 1;
2449           l_current_record := l_preferred_engineers_tbl.NEXT(l_current_record);
2450        END LOOP;
2451 
2452         -- The calendar flag check will not be done any more. The first available slot will be fetched
2453         -- This is for the preformance bug 3301417. If the calendar flag is Y then the resources will
2454         -- filtered based on availability in the procedure get_available_slot. This change is being done on
2455         -- 16 June 2004
2456         -- IF (p_calendar_flag = 'Y') THEN
2457         -- change the API Name temporarily so that in case of unexpected error
2458         -- it is properly caught
2459           l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
2460           l_return_status_1 := x_return_status ;
2461            -- call the api to check resource availability
2462            get_available_resources
2463             (
2464               p_init_msg_list                 =>  'F',
2465               p_calendar_flag                 =>   p_calendar_flag,
2466               p_effort_duration               =>   p_effort_duration,
2467               p_effort_uom                    =>   p_effort_uom,
2468               p_planned_start_date            =>   p_planned_start_date,
2469               p_planned_end_date              =>   p_planned_end_date,
2470               p_continuous_task               =>   jtf_assign_pub.g_continuous_work,
2471               p_breakdown                     =>   null,
2472               p_breakdown_uom                 =>   null,
2473               x_return_status                 =>   x_return_status,
2474               x_msg_count                     =>   x_msg_count,
2475               x_msg_data                      =>   x_msg_data,
2476               x_assign_resources_tbl          =>   x_assign_resources_tbl,
2477 	      --Added for Bug # 5573916
2478 	      p_calendar_check                =>   p_calendar_check
2479 	      --Added for Bug # 5573916 Ends here
2480 	      );
2481 
2482           -- set back the API name to original name
2483           l_api_name := l_api_name_1;
2484 
2485             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
2486             -- Unexpected Execution Error from call to Get_contracts_resources
2487                fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
2488                fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
2489                fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_IB_RESOURCES');
2490                fnd_msg_pub.add;
2491                IF (x_return_status = fnd_api.g_ret_sts_error) THEN
2492                   RAISE fnd_api.g_exc_error;
2493                ELSE
2494                   RAISE fnd_api.g_exc_unexpected_error;
2495                END IF;
2496              END IF; -- end of x_return_status check
2497            -- end if; -- end if p_calendar_flag
2498      end if;    --l_preferred_engineers_tbl.COUNT > 0
2499 
2500 
2501   EXCEPTION
2502     WHEN FND_API.G_EXC_ERROR THEN
2503       x_return_status := FND_API.G_RET_STS_ERROR ;
2504       FND_MSG_PUB.Count_And_Get
2505       (
2506         p_count => x_msg_count,
2507         p_data  => x_msg_data
2508       );
2509 
2510     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2511       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2512       FND_MSG_PUB.Count_And_Get
2513       (
2514         p_count => x_msg_count,
2515         p_data  => x_msg_data
2516       );
2517 
2518     WHEN OTHERS THEN
2519       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
2520       fnd_message.set_token('P_SQLCODE',SQLCODE);
2521       fnd_message.set_token('P_SQLERRM',SQLERRM);
2522       fnd_message.set_token('P_API_NAME',l_api_name);
2523       FND_MSG_PUB.add;
2524 
2525       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2526       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
2527         FND_MSG_PUB.Add_Exc_Msg
2528         (
2529           G_PKG_NAME,
2530           l_api_name
2531         );
2532       END IF;
2533 
2534       FND_MSG_PUB.Count_And_Get
2535       (
2536         p_count => x_msg_count,
2537         p_data  => x_msg_data
2538        );
2539 
2540   END get_ib_resources;
2541 
2542 
2543 -- *******************************************************************************
2544 
2545 -- Start of comments
2546 
2547 --      API name        : GET_ASSIGN_TASK_RESOURCES
2548 --      Type            : Public
2549 --      Function        : Determine the resources based on the customer
2550 --                        preferences or territory preferences and the
2551 --                        availability. This is when the calling doc is TASK.
2552 --      Pre-reqs        : None
2553 
2554 --      Parameters      :
2555 
2556 --      IN              : p_api_version         IN      NUMBER  Required
2557 --                        p_init_msg_list       IN      VARCHAR2 Optional
2558 --                                              DEFAULT = FND_API.G_FALSE
2559 --                        p_commit              IN      VARCHAR2 optional
2560 --                                              DEFAULT = FND_API.G_FALSE
2561 
2562 --     Assignment Manager Specific Parameters
2563 
2564 --     This determines the Resource Type required by the
2565 --     calling document
2566 --     p_resource_type                          VARCHAR2(30)
2567 --     p_role                                   VARCHAR2(30)
2568 
2569 --     This determines the number of resources required
2570 --     by the calling document
2571 --     p_no_of_resources                        NUMBER,
2572 
2573 --     This is for sending out the qualified resource directly
2574 --     to the calling form. Defaulted to the PROFILE value
2575 --     p_auto_select_flag                       VARCHAR2(1)
2576 --                                              : value of  Y or N
2577 
2578 --     This is to set the preference of CONTRACTS PREFERRED ENGINEERS
2579 --     Defaulted to the PROFILE value
2580 --     p_contracts_preferred_engineer           VARCHAR2(1)
2581 --                                              : value of  Y or N
2582 
2583 --     This is to set the preference of INSTALL BASE PREFERRED ENGINEERS
2584 --     Defaulted to the PROFILE value
2585 --     p_ib_preferred_engineer                  VARCHAR2(1)
2586 --                                              : value of  Y or N
2587 
2588 --     The Effort required is determined by these two parameters
2589 --     p_effort_duration                        NUMBER
2590 --     p_effort_uom                             VARCHAR2(10)
2591 
2592 --     The Dates in which the resource is required, is determined
2593 --     by these two parameters
2594 --     p_start_date                             DATE
2595 --     p_end_date                               DATE
2596 
2597 --     The Territory Manager is accessed based on the value set
2598 --     p_territory_flag                         VARCHAR2(1)
2599 --                                              : value of  Y or N
2600 
2601 --     The Resource Availability is checked based on the value set
2602 --     p_calendar_flag                          VARCHAR2(1)
2603 --                                              : value of  Y or N
2604 
2605 
2606 --     This parameter contains the Calling Document ID
2607 --     In this case it is a TASK_ID.
2608 --     p_task_id                                 NUMBER  -- REQUIRED
2609 
2610 --     This parameter contains list of qualifier columns from the
2611 --     UI which have been selected to re-query the resources.
2612 --     Strictly for the use of User Interface of Assignment Manager.
2613 --     p_column_list                             VARCHAR2
2614 
2615 --     OUT              : x_return_status        OUT     VARCHAR2(1)
2616 --                        x_msg_count            OUT     NUMBER
2617 --                        x_msg_data             OUT     VARCHAR2(2000)
2618 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
2619 --                                                       AssignResources_tbl_type
2620 
2621 
2622 --      Version         : Current version        1.0
2623 --                        Initial version        1.0
2624 --
2625 --      Notes           :
2626 --
2627 
2628 -- End of comments
2629 
2630 -- *********************************************************************************
2631 
2632 
2633 
2634   /* Procedure Body with the parameters when the
2635      Source Document is TASK */
2636 
2637 
2638   PROCEDURE GET_ASSIGN_TASK_RESOURCES
2639     (
2640         p_api_version                         IN  NUMBER,
2641         p_init_msg_list                       IN  VARCHAR2,
2642         p_resource_type                       IN  VARCHAR2,
2643         p_role                                IN  VARCHAR2,
2644         p_no_of_resources                     IN  NUMBER,
2645         p_auto_select_flag                    IN  VARCHAR2,
2646         p_contracts_preferred_engineer        IN  VARCHAR2,
2647         p_ib_preferred_engineer               IN  VARCHAR2,
2648         p_effort_duration                     IN  NUMBER,
2649         p_effort_uom                          IN  VARCHAR2,
2650         p_start_date                          IN  DATE,
2651         p_end_date                            IN  DATE,
2652         p_territory_flag                      IN  VARCHAR2,
2653         p_calendar_flag                       IN  VARCHAR2,
2654         p_web_availability_flag               IN  VARCHAR2,
2655         p_task_id                             IN  JTF_TASKS_VL.TASK_ID%TYPE,
2656         p_column_list                         IN  VARCHAR2,
2657         p_business_process_id                 IN  NUMBER,
2658         p_business_process_date               IN  DATE,
2659         p_filter_excluded_resource            IN  VARCHAR2,
2660         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
2661         x_return_status                       OUT NOCOPY VARCHAR2,
2662         x_msg_count                           OUT NOCOPY NUMBER,
2663         x_msg_data                            OUT NOCOPY VARCHAR2,
2664 	--Added for Bug # 5573916
2665 	p_calendar_check                IN  VARCHAR2 DEFAULT 'Y'
2666 	--Added for Bug # 5573916 Ends here
2667     )
2668   IS
2669 
2670     l_return_status_1                     VARCHAR2(10);
2671     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_TASK_RESOURCES';
2672     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_TASK_RESOURCES';
2673     l_api_version                         NUMBER        := 1.0;
2674     l_planned_start_date                  DATE;
2675     l_planned_end_date                    DATE;
2676     l_effort_duration                     NUMBER;
2677     l_effort_uom                          VARCHAR2(30);
2678     l_task_id                             JTF_TASKS_VL.TASK_ID%TYPE := p_task_id;
2679     l_task_source_code                    JTF_TASKS_VL.SOURCE_OBJECT_TYPE_CODE%TYPE;
2680     l_task_source_id                      JTF_TASKS_VL.SOURCE_OBJECT_ID%TYPE;
2681     l_contract_id                         NUMBER;
2682     l_cp_id                               NUMBER;
2683     l_contract_flag                       VARCHAR2(1)   := 'N';
2684 
2685     l_travel_time                         NUMBER        := 0;
2686     l_travel_uom                          VARCHAR2(10)  := 'HR';
2687 
2688     l_current_record                      INTEGER       := 0;
2689     l_current_rec                         INTEGER       := 0;
2690     l_pref_record                         INTEGER       := 0;
2691 
2692     l_assign_resources_rec                JTF_TERRITORY_PUB.JTF_Task_Rec_Type;
2693     l_assign_resources_sr_rec             JTF_TERRITORY_PUB.JTF_Srv_Task_rec_type;
2694     l_assign_resources_tbl                JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
2695 
2696     l_engineer_id                         JTF_NUMBER_TABLE;
2697     l_resource_type                       JTF_VARCHAR2_TABLE_100;
2698 
2699     l_return_status                       VARCHAR2(10);
2700     l_msg_count                           NUMBER;
2701     l_msg_data                            VARCHAR2(2000);
2702 
2703     l_ib_preferred_engineers_tbl          JTF_ASSIGN_PUB.prfeng_tbl_type;
2704     l_con_preferred_engineers_tbl         JTF_ASSIGN_PUB.prfeng_tbl_type;
2705     l_preferred_engineers_tbl             JTF_ASSIGN_PUB.Preferred_Engineers_tbl_type;
2706 
2707     -- tables for excluded resource
2708     l_excluded_resource_tbl               JTF_ASSIGN_PUB.excluded_tbl_type;
2709     l_contracts_tbl                       JTF_ASSIGN_PUB.AssignResources_tbl_type;
2710     l_ib_tbl                              JTF_ASSIGN_PUB.AssignResources_tbl_type;
2711 
2712     l_status                              VARCHAR2(30);
2713     l_industry                            VARCHAR2(30);
2714 
2715     l_x_planned_start_date                DATE;
2716     l_x_planned_end_date                  DATE;
2717     l_x_shift_construct_id                NUMBER;
2718     l_x_availability_type                 VARCHAR2(60);
2719 
2720     l_column_list                         VARCHAR2(4000);
2721 
2722     l_dynamic_sql                         VARCHAR2(2000);
2723     l_dynamic_sql1                        VARCHAR2(2000);
2724     l_dynamic_sql2                        VARCHAR2(2000);
2725     l_dynamic_sql3                        VARCHAR2(2000);
2726 
2727 
2728     TYPE DYNAMIC_CUR_TYP  IS REF CURSOR;
2729 
2730     cur_task          DYNAMIC_CUR_TYP;
2731     cur_srv_task      DYNAMIC_CUR_TYP;
2732     cur_cs_incidents  DYNAMIC_CUR_TYP;
2733     cur_cs_contacts   DYNAMIC_CUR_TYP;
2734 
2735 
2736     CURSOR cur_task_id IS
2737       SELECT source_object_type_code,
2738              source_object_id,
2739              planned_start_date,
2740              planned_end_date,
2741              planned_effort,
2742              planned_effort_uom
2743       FROM   jtf_tasks_vl
2744       WHERE  task_id = l_task_id;
2745     l_cur_task_id cur_task_id%ROWTYPE;
2746 
2747 
2748     l_cs_contacts_cp       VARCHAR2(5) := 'CP';
2749     l_cs_contacts_y        VARCHAR2(2) := 'Y' ;
2750     l_cs_contacts_emp      VARCHAR2(10) := 'EMPLOYEE';
2751     l_cs_contacts_rsc_id   NUMBER;
2752     l_cs_contacts_rsc_cat  VARCHAR2(60);
2753 
2754 
2755     CURSOR cur_ib_resources IS
2756       SELECT resource_id,
2757              'RS_'||category category
2758       FROM   jtf_rs_resource_extns_vl
2759       WHERE  source_id = l_cs_contacts_rsc_id AND
2760              category  = l_cs_contacts_rsc_cat;
2761     l_cur_ib_resources cur_ib_resources%ROWTYPE;
2762 
2763 
2764     cur_support_site_name  DYNAMIC_CUR_TYP;
2765 
2766     l_support_site         VARCHAR2(15) := 'SUPPORT_SITE';
2767     l_rsc_type             VARCHAR2(30);
2768     l_rsc_id               NUMBER;
2769 
2770 
2771     CURSOR cur_support_site_id (p_rsc_id NUMBER, p_rsc_type VARCHAR2) IS
2772       SELECT support_site_id
2773       FROM   jtf_rs_resource_extns_vl
2774       WHERE  resource_id = p_rsc_id AND
2775              category    = p_rsc_type;
2776 
2777 
2778     CURSOR cur_web_availability (p_res_id NUMBER, p_res_type VARCHAR2) IS
2779       SELECT resource_id
2780       FROM   jtf_rs_web_available_v
2781       WHERE  resource_id = p_res_id AND
2782              category    = p_res_type;
2783 
2784 
2785   BEGIN
2786 
2787     SAVEPOINT get_assign_task_resources;
2788 
2789     -- Started Assignment Manager API for TASKS
2790 
2791 
2792     /* Standard call to check for call compatibility */
2793 
2794     IF NOT fnd_api.compatible_api_call (l_api_version,
2795                                         p_api_version,
2796                                         l_api_name,
2797                                         g_pkg_name) THEN
2798       RAISE fnd_api.g_exc_unexpected_error;
2799     END IF;
2800 
2801 
2802     /* Initialize message list if p_init_msg_list is set to TRUE */
2803 
2804     IF fnd_api.to_boolean (p_init_msg_list) THEN
2805       fnd_msg_pub.initialize;
2806     END IF;
2807 
2808     x_return_status := fnd_api.g_ret_sts_success;
2809 
2810 
2811 
2812     /* Get TASK source and the related information for contracts and
2813        contracts primary and secondary engineers, if they exist */
2814 
2815   If(l_task_id IS NOT NULL)
2816   -- this has been added as in form startup we now do a autoquery. So if no task id is passed instead of throwing the
2817   -- message that invalid id has been passed in we will just not do any processing
2818   THEN
2819     OPEN  cur_task_id;
2820     FETCH cur_task_id INTO l_cur_task_id;
2821     IF  ( cur_task_id%NOTFOUND ) THEN
2822       fnd_message.set_name('JTF', 'JTF_AM_INVALID_TASK_ID');
2823       fnd_msg_pub.add;
2824       RAISE fnd_api.g_exc_error;
2825     ELSE
2826       l_task_source_code    := l_cur_task_id.source_object_type_code;
2827       l_task_source_id      := l_cur_task_id.source_object_id;
2828       l_planned_start_date  := l_cur_task_id.planned_start_date;
2829       l_planned_end_date    := l_cur_task_id.planned_end_date;
2830       l_effort_duration     := l_cur_task_id.planned_effort;
2831       l_effort_uom          := l_cur_task_id.planned_effort_uom;
2832     END IF;
2833     CLOSE cur_task_id;
2834 
2835 
2836     IF (l_task_source_id IS NOT NULL AND
2837         l_task_source_code = 'SR') THEN
2838 
2839       /*
2840         CURSOR cur_cs_incidents IS
2841           SELECT contract_service_id,
2842                  customer_product_id,
2843                  expected_resolution_date
2844           FROM   cs_incidents_all_vl
2845           WHERE  incident_id = l_task_source_id;
2846       */
2847 
2848       l_dynamic_sql1 :=  ' SELECT contract_service_id, customer_product_id, expected_resolution_date'||
2849                          ' FROM   cs_incidents_all_vl'||
2850                          ' WHERE  incident_id = :1';
2851 
2852       -- dbms_output.put_line('Select1 is : '||l_dynamic_sql1);
2853 
2854       OPEN  cur_cs_incidents FOR  l_dynamic_sql1 USING l_task_source_id;
2855       FETCH cur_cs_incidents INTO l_contract_id,
2856                                   l_cp_id,
2857                                   l_planned_end_date;
2858 
2859       IF  ( cur_cs_incidents%NOTFOUND ) THEN
2860         fnd_message.set_name('JTF', 'JTF_AM_INVALID_SR_ID');
2861         fnd_msg_pub.add;
2862         RAISE fnd_api.g_exc_error;
2863       ELSE
2864         l_contract_flag       := 'Y';
2865         l_planned_start_date  := SYSDATE;
2866       END IF;
2867 
2868       CLOSE cur_cs_incidents;
2869     END IF;
2870 
2871     /* Defaulting the variable values to IN parameters,
2872        if the IN paramaters have values given */
2873 
2874     IF (p_start_date IS NOT NULL) THEN
2875       l_planned_start_date := p_start_date;
2876     ELSE
2877       l_planned_start_date := SYSDATE;
2878     END IF;
2879 
2880     IF (p_end_date IS NOT NULL) THEN
2881       l_planned_end_date := p_end_date;
2882     ELSE
2883       l_planned_end_date := SYSDATE + 14;
2884     END IF;
2885 
2886     IF (p_effort_duration IS NOT NULL) THEN
2887       l_effort_duration := p_effort_duration;
2888     END IF;
2889 
2890     IF (p_effort_uom IS NOT NULL) THEN
2891       l_effort_uom := p_effort_uom;
2892     END IF;
2893 
2894     /* Check if the Contracts Preferred Engineers Profile is SET
2895        If it is SET then get the available preferred engineers
2896        into the table of records */
2897 
2898     IF (p_contracts_preferred_engineer = 'Y') THEN
2899 
2900       -- Process for the contracts preferred engineers
2901 
2902          get_contracts_resources
2903           (
2904             p_init_msg_list           =>  p_init_msg_list,
2905             p_contract_id             =>  l_contract_id,
2906             p_calendar_flag           =>  p_calendar_flag,
2907             p_effort_duration         =>  l_effort_duration,
2908             p_effort_uom              =>  l_effort_uom,
2909             p_planned_start_date      =>  l_planned_start_date,
2910             p_planned_end_date        =>  l_planned_end_date,
2911             p_resource_type           =>  p_resource_type,
2912             p_business_process_id     =>  p_business_process_id,
2913             p_business_process_date   =>  p_business_process_date,
2914             x_return_status           =>  x_return_status,
2915             x_msg_count               =>  x_msg_count,
2916             x_msg_data                =>  x_msg_data,
2917             x_assign_resources_tbl    =>  l_contracts_tbl,
2918             x_excluded_tbl            =>  l_excluded_resource_tbl,
2919 	    --Added for Bug # 5573916
2920 	    p_calendar_check          =>  p_calendar_check
2921 	    --Added for Bug # 5573916 Ends here
2922           );
2923 
2924           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
2925             -- Unexpected Execution Error from call to Get_contracts_resources
2926             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
2927             fnd_message.set_token('P_PROC_NAME','GET_CONTRACTS_RESOURCES');
2928             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_TASK_RESOURCES');
2929             fnd_msg_pub.add;
2930             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
2931               RAISE fnd_api.g_exc_error;
2932             ELSE
2933               RAISE fnd_api.g_exc_unexpected_error;
2934             END IF;
2935           END IF;
2936 
2937     END IF; -- p_contracts_preferred_engineer = 'Y'
2938 
2939     /* Check if the Installed Base Preferred Engineers Profile is SET
2940        If it is SET then get the available preferred engineers
2941        into the table of records */
2942 
2943     IF (p_ib_preferred_engineer = 'Y') THEN
2944       -- Process for the Installed Base preferred engineers
2945      -- changed to ib code to call the common procedure and remove the old code - 4th april 2003
2946       get_ib_resources
2947             (
2948               p_init_msg_list           =>  p_init_msg_list,
2949               p_customer_product_id     =>  l_cp_id,
2950               p_calendar_flag           =>  p_calendar_flag,
2951               p_effort_duration         =>  l_effort_duration,
2952               p_effort_uom              =>  l_effort_uom,
2953               p_planned_start_date      =>  l_planned_start_date,
2954               p_planned_end_date        =>  l_planned_end_date,
2955               p_resource_type           =>  p_resource_type,
2956               x_return_status           =>  x_return_status,
2957               x_msg_count               =>  x_msg_count,
2958               x_msg_data                =>  x_msg_data,
2959               x_assign_resources_tbl    =>  l_ib_tbl,
2960               x_excluded_tbl            =>  l_excluded_resource_tbl,
2961 	      --Added for Bug # 5573916
2962 	      p_calendar_check          =>  p_calendar_check
2963 	      --Added for Bug # 5573916 Ends here
2964             );
2965 
2966           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
2967             -- Unexpected Execution Error from call to Get_contracts_resources
2968             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
2969             fnd_message.set_token('P_PROC_NAME','GET_IB_RESOURCES');
2970             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_TASK_RESOURCES');
2971             fnd_msg_pub.add;
2972             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
2973               RAISE fnd_api.g_exc_error;
2974             ELSE
2975               RAISE fnd_api.g_exc_unexpected_error;
2976             END IF;
2977           END IF;
2978 
2979      END IF; -- p_ib_preferred_engineer = 'Y'
2980 
2981 
2982    -- remove excluded resources
2983    IF(p_filter_excluded_resource = 'Y')
2984    THEN
2985      IF(p_contracts_preferred_engineer = 'Y')
2986       THEN
2987           remove_excluded(x_res_tbl  => l_contracts_tbl,
2988                           x_exc_res  => l_excluded_resource_tbl);
2989       END IF;
2990       IF(p_ib_preferred_engineer = 'Y')
2991       THEN
2992           remove_excluded(x_res_tbl  => l_ib_tbl,
2993                           x_exc_res  => l_excluded_resource_tbl);
2994       END IF;
2995     END IF;
2996 
2997     -- after the preferred engineers are obtained from contracts/ib, select the resources
2998     -- that are to be returned
2999     /* Check if the Contracts Preferred Engineers Profile is SET If it is SET then get the available
3000        preferred engineers into the table of records */
3001         IF (p_contracts_preferred_engineer = 'Y') THEN
3002             table_copy(l_contracts_tbl, x_assign_resources_tbl);
3003         END IF; -- p_contracts_preferred_engineer = 'Y'
3004 
3005     /* Check if the Installed Base Preferred Engineers Profile is SET
3006        If it is SET then get the available preferred engineers
3007        into the table of records */
3008        IF (p_ib_preferred_engineer = 'Y') THEN
3009              table_copy(l_ib_tbl, x_assign_resources_tbl);
3010        END IF; -- p_ib_preferred_engineer = 'Y'
3011 
3012     /* Actual Flow of Assignment Manager */
3013 
3014     /* Initialize the record number to add the records of
3015        Contracts or Installed Base Preferred Engineers to the table of records */
3016 
3017     /* If this table has rows then there are preferred engineers existing */
3018 
3019     IF x_assign_resources_tbl.COUNT > 0 THEN
3020       -- removed the processing here as the calendar check is already being done inside the
3021       -- common procedures calls
3022        null;
3023 
3024     ELSE -- l_preferred_engineers_tbl.COUNT <= 0
3025 
3026       -- If there are NO preferred engineers then call territory API
3027 
3028       IF (l_task_source_code = 'SR') THEN
3029         fnd_message.set_name('JTF', 'JTF_AM_TASK_CREATED_BY_SR');
3030         fnd_msg_pub.add;
3031         RAISE fnd_api.g_exc_error;
3032 
3033         /* Query the View which is a combination of Task and Service Request
3034            to get the data into the Record Type to pass it to the Territory API */
3035 
3036 
3037         IF (p_column_list IS NULL) THEN
3038           l_column_list := '*';
3039         ELSE
3040           l_column_list := p_column_list;
3041         END IF;
3042 
3043         /*
3044           SELECT * INTO l_assign_resources_sr_rec
3045           FROM   jtf_terr_srv_task_v -- (cs_sr_task_territory_v)
3046           WHERE  task_id            = l_task_id AND
3047                  service_request_id = l_task_source_id;
3048         */
3049 
3050         l_dynamic_sql :=   'SELECT '||
3051                            l_column_list||
3052                            ' FROM cs_sr_task_territory_v'||
3053                            ' WHERE task_id            = :1 AND
3054                                    service_request_id = :2';
3055 
3056         OPEN cur_srv_task FOR l_dynamic_sql USING l_task_id, l_task_source_id;
3057         FETCH cur_srv_task INTO l_assign_resources_sr_rec;
3058         -- EXIT WHEN cur_srv_task%NOTFOUND;
3059         CLOSE cur_srv_task;
3060 
3061         IF (p_territory_flag = 'Y') THEN
3062 
3063           -- change the API Name temporarily so that in case of unexpected error
3064           -- it is properly caught
3065           l_api_name := l_api_name||'-JTF_TERR_SERVICE_PUB';
3066 
3067           JTF_TERR_SERVICE_PUB.Get_WinningTerrMembers
3068           (
3069              p_api_version_number  => l_api_version,
3070              p_init_msg_list       => p_init_msg_list,
3071              p_TerrSrvTask_Rec     => l_assign_resources_sr_rec,
3072              p_Resource_Type       => p_resource_type,
3073              p_Role                => p_role,
3074              x_return_status       => x_return_status,
3075              x_msg_count           => x_msg_count,
3076              x_msg_data            => x_msg_data,
3077              x_TerrResource_tbl    => l_assign_resources_tbl
3078           );
3079 
3080           -- set back the API name to original name
3081           l_api_name := l_api_name_1;
3082 
3083           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
3084             -- Unexpected Execution Error from call to Territory Manager
3085             fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
3086             fnd_msg_pub.add;
3087             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
3088               RAISE fnd_api.g_exc_error;
3089             ELSE
3090               RAISE fnd_api.g_exc_unexpected_error;
3091             END IF;
3092           END IF;
3093           -- added 2 april 2003 by sudarsana to conver RS_SUPPLIER TO RS_SUPPLIER_CONTACT
3094           IF(l_assign_resources_tbl.COUNT > 0)
3095           THEN
3096              l_current_record := l_assign_resources_tbl.FIRST;
3097              WHILE l_current_record <= l_assign_resources_tbl.LAST
3098              LOOP
3099                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
3100                 THEN
3101                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
3102                 END IF;
3103                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
3104              END LOOP;
3105           END IF;
3106 
3107           IF l_assign_resources_tbl.COUNT > 0 THEN
3108 
3109             l_current_record := l_assign_resources_tbl.FIRST;
3110 
3111             -- FOR i IN 1 .. l_assign_resources_tbl.COUNT
3112 
3113             WHILE l_current_record <= l_assign_resources_tbl.LAST
3114             LOOP
3115                 -- Check the calendar for resource availability
3116                 -- Call Calendar API
3117                 -- IF the resource is available then accept the values and
3118                 -- finally check for the WORKFLOW profile option
3119 
3120                 -- removed the calendar check from here 26th September 2003. Calendar check will be done in a seperate
3121                 -- procedure. The call is made after resources are copied to x_assign_resources_tbl
3122                 x_assign_resources_tbl(l_current_record).terr_rsc_id           :=
3123                                        l_assign_resources_tbl(l_current_record).terr_rsc_id;
3124                 x_assign_resources_tbl(l_current_record).resource_id           :=
3125                                        l_assign_resources_tbl(l_current_record).resource_id;
3126                 x_assign_resources_tbl(l_current_record).resource_type         :=
3127                                        l_assign_resources_tbl(l_current_record).resource_type;
3128                 x_assign_resources_tbl(l_current_record).role                  :=
3129                                        l_assign_resources_tbl(l_current_record).role;
3130                 x_assign_resources_tbl(l_current_record).start_date            :=
3131                                        l_assign_resources_tbl(l_current_record).start_date;
3132                 x_assign_resources_tbl(l_current_record).end_date              :=
3133                                        l_assign_resources_tbl(l_current_record).end_date;
3134                 x_assign_resources_tbl(l_current_record).shift_construct_id    := NULL;
3135                 x_assign_resources_tbl(l_current_record).terr_id               :=
3136                                        l_assign_resources_tbl(l_current_record).terr_id;
3137                 x_assign_resources_tbl(l_current_record).terr_name             :=
3138                                        l_assign_resources_tbl(l_current_record).terr_name;
3139 		-- ================code added for bug 6453896=============
3140 	        x_assign_resources_tbl(l_current_record).terr_rank             :=
3141                                        l_assign_resources_tbl(l_current_record).ABSOLUTE_RANK;
3142 		-- ================End for addition of code===============
3143                 x_assign_resources_tbl(l_current_record).primary_contact_flag  :=
3144                                        l_assign_resources_tbl(l_current_record).primary_contact_flag;
3145                  x_assign_resources_tbl(l_current_record).primary_flag  :=
3146                                          l_assign_resources_tbl(l_current_record).primary_contact_flag;
3147                 x_assign_resources_tbl(l_current_record).resource_source       := 'TERR';
3148                  l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
3149              END LOOP;
3150 
3151              -- If resource availability is to be checked then the calendar api is called. This is done in s seperate
3152              -- procedure get_available_resources
3153              -- The calendar flag check will not be done any more. The first available slot will be fetched
3154              -- This is for the preformance bug 3301417. If the calendar flag is Y then the resources will
3155              -- filtered based on availability in the procedure get_available_slot. This change is being done on
3156              -- 16 June 2004
3157             -- IF (p_calendar_flag = 'Y') THEN
3158              -- change the API Name temporarily so that in case of unexpected error
3159              -- it is properly caught
3160               l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
3161               l_return_status_1 := x_return_status ;
3162               -- call the api to check resource availability
3163               get_available_resources
3164               (
3165                 p_init_msg_list                 =>  'F',
3166                 p_calendar_flag                 =>  p_calendar_flag,
3167                 p_effort_duration               =>  l_effort_duration,
3168                 p_effort_uom                    =>  l_effort_uom,
3169                 p_planned_start_date            =>  l_planned_start_date,
3170                 p_planned_end_date              =>  l_planned_end_date,
3171                 p_breakdown                     =>   null,
3172                 p_breakdown_uom                 =>   null,
3173                 p_continuous_task               =>  jtf_assign_pub.g_continuous_work,
3174                 x_return_status                 =>  x_return_status,
3175                 x_msg_count                     =>  x_msg_count,
3176                 x_msg_data                      =>  x_msg_data,
3177                 x_assign_resources_tbl          =>  x_assign_resources_tbl,
3178 		--Added for Bug # 5573916
3179 	        p_calendar_check                =>  p_calendar_check
3180 	        --Added for Bug # 5573916 Ends here
3181 		);
3182 
3183               -- set back the API name to original name
3184                l_api_name := l_api_name_1;
3185 
3186                IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
3187               -- Unexpected Execution Error from call to Get_contracts_resources
3188                  fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
3189                  fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
3190                  fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_TASK_RESOURCES');
3191                  fnd_msg_pub.add;
3192                  IF (x_return_status = fnd_api.g_ret_sts_error) THEN
3193                   RAISE fnd_api.g_exc_error;
3194                  ELSE
3195                    RAISE fnd_api.g_exc_unexpected_error;
3196                  END IF;
3197               END IF; -- end of x_return_status check
3198             -- end if; -- if p_calendar_flag = Y
3199 
3200             IF ( p_calendar_flag = 'Y' AND
3201                  x_assign_resources_tbl.count = 0 ) THEN
3202               fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
3203               fnd_msg_pub.add;
3204 --              RAISE fnd_api.g_exc_error;
3205             END IF;
3206 
3207              -- remove excluded resources
3208            IF(p_filter_excluded_resource = 'Y')
3209            THEN
3210               remove_excluded(x_res_tbl  => x_assign_resources_tbl,
3211                               x_exc_res  => l_excluded_resource_tbl);
3212            END IF;
3213 
3214           ELSE   -- No resources returned from the Territory API
3215             fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
3216             fnd_msg_pub.add;
3217 --            RAISE fnd_api.g_exc_error;
3218           END IF;
3219 
3220         ELSE  -- Territory Flag is NO
3221           fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
3222           fnd_msg_pub.add;
3223 --          RAISE fnd_api.g_exc_error;
3224         END IF;
3225 
3226       ELSE  -- l_task_source_code <> 'SR'
3227 
3228         /* If Source Code is NOT a SERVICE REQUEST
3229            Query the Task View to get the data into the Record Type
3230            to pass it to the Territory API */
3231 
3232         IF (p_column_list IS NULL) THEN
3233           l_column_list := '*';
3234         ELSE
3235           l_column_list := p_column_list;
3236         END IF;
3237 
3238         l_dynamic_sql :=  'SELECT '||
3239                           l_column_list||
3240                           ' FROM jtf_task_territory_v'||
3241                           ' WHERE task_id = :1 AND rownum < 2';
3242 
3243         OPEN cur_task FOR l_dynamic_sql USING l_task_id;
3244         FETCH cur_task INTO l_assign_resources_rec.TASK_ID,
3245                             l_assign_resources_rec.PARTY_ID,
3246                             l_assign_resources_rec.COUNTRY,
3247                             l_assign_resources_rec.PARTY_SITE_ID,
3248                             l_assign_resources_rec.CITY,
3249                             l_assign_resources_rec.POSTAL_CODE,
3250                             l_assign_resources_rec.STATE,
3251                             l_assign_resources_rec.AREA_CODE,
3252                             l_assign_resources_rec.COUNTY,
3253                             l_assign_resources_rec.COMP_NAME_RANGE,
3254                             l_assign_resources_rec.PROVINCE,
3255                             l_assign_resources_rec.NUM_OF_EMPLOYEES,
3256                             l_assign_resources_rec.TASK_TYPE_ID,
3257                             l_assign_resources_rec.TASK_STATUS_ID,
3258                             l_assign_resources_rec.TASK_PRIORITY_ID,
3259                             l_assign_resources_rec.ATTRIBUTE1,
3260                             l_assign_resources_rec.ATTRIBUTE2,
3261                             l_assign_resources_rec.ATTRIBUTE3,
3262                             l_assign_resources_rec.ATTRIBUTE4,
3263                             l_assign_resources_rec.ATTRIBUTE5,
3264                             l_assign_resources_rec.ATTRIBUTE6,
3265                             l_assign_resources_rec.ATTRIBUTE7,
3266                             l_assign_resources_rec.ATTRIBUTE8,
3267                             l_assign_resources_rec.ATTRIBUTE9,
3268                             l_assign_resources_rec.ATTRIBUTE10,
3269                             l_assign_resources_rec.ATTRIBUTE11,
3270                             l_assign_resources_rec.ATTRIBUTE12,
3271                             l_assign_resources_rec.ATTRIBUTE13,
3272                             l_assign_resources_rec.ATTRIBUTE14,
3273                             l_assign_resources_rec.ATTRIBUTE15;
3274         -- EXIT WHEN cur_task%NOTFOUND;
3275         CLOSE cur_task;
3276 
3277         IF (p_territory_flag = 'Y') THEN
3278 
3279           -- change the API Name temporarily so that in case of unexpected error
3280           -- it is properly caught
3281           l_api_name := l_api_name||'-JTF_TERR_TASK_PUB';
3282 
3283           JTF_TERR_TASK_PUB.Get_WinningTerrMembers
3284           (
3285             p_api_version_number  => l_api_version,
3286             p_init_msg_list       => p_init_msg_list,
3287             p_TerrTask_rec        => l_assign_resources_rec,
3288             p_Resource_Type       => p_resource_type,
3289             p_Role                => p_role,
3290             x_return_status       => x_return_status,
3291             x_msg_count           => x_msg_count,
3292             x_msg_data            => x_msg_data,
3293             x_TerrResource_tbl    => l_assign_resources_tbl
3294           );
3295 
3296           -- set back the API name to original name
3297           l_api_name := l_api_name_1;
3298 
3299           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
3300             -- Unexpected Execution Error from call to Territory Manager for TASKS
3301             fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
3302             fnd_msg_pub.add;
3303             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
3304               RAISE fnd_api.g_exc_error;
3305             ELSE
3306               RAISE fnd_api.g_exc_unexpected_error;
3307             END IF;
3308           END IF;
3309 
3310 
3311 
3312           -- to handle RS_SUPPLIER returned from territories -- added on 2nd april2003 by sudarsana
3313 
3314           IF(l_assign_resources_tbl.COUNT > 0)
3315           THEN
3316              l_current_record := l_assign_resources_tbl.FIRST;
3317              WHILE l_current_record <= l_assign_resources_tbl.LAST
3318              LOOP
3319                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
3320                 THEN
3321                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
3322                 END IF;
3323                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
3324              END LOOP;
3325           END IF;
3326 
3327         -- removed the calendar check here. calling the procedure for calendar check in the next step
3328         -- calendar check removed 29th September 2003
3329 
3330           IF l_assign_resources_tbl.COUNT > 0 THEN
3331 
3332             l_current_rec    := 0;
3333             l_current_record := l_assign_resources_tbl.FIRST;
3334 
3335             WHILE l_current_record <= l_assign_resources_tbl.LAST
3336             LOOP
3337                 x_assign_resources_tbl(l_current_record).terr_rsc_id           :=
3338                                        l_assign_resources_tbl(l_current_record).terr_rsc_id;
3339                 x_assign_resources_tbl(l_current_record).resource_id           :=
3340                                        l_assign_resources_tbl(l_current_record).resource_id;
3341                 x_assign_resources_tbl(l_current_record).resource_type         :=
3342                                        l_assign_resources_tbl(l_current_record).resource_type;
3343                 x_assign_resources_tbl(l_current_record).role                  :=
3344                                        l_assign_resources_tbl(l_current_record).role;
3345                 x_assign_resources_tbl(l_current_record).start_date            :=
3346                                        l_assign_resources_tbl(l_current_record).start_date;
3347                 x_assign_resources_tbl(l_current_record).end_date              :=
3348                                        l_assign_resources_tbl(l_current_record).end_date;
3349                 x_assign_resources_tbl(l_current_record).shift_construct_id    := NULL;
3350                 x_assign_resources_tbl(l_current_record).terr_id               :=
3351                                        l_assign_resources_tbl(l_current_record).terr_id;
3352                 x_assign_resources_tbl(l_current_record).terr_name             :=
3353                                        l_assign_resources_tbl(l_current_record).terr_name;
3354 		-- ================code added for bug 6453896=============
3355                 x_assign_resources_tbl(l_current_record).terr_rank             :=
3356                                        l_assign_resources_tbl(l_current_record).ABSOLUTE_RANK;
3357 		-- ================End for addition of code===============
3358                 x_assign_resources_tbl(l_current_record).primary_contact_flag  :=
3359                                        l_assign_resources_tbl(l_current_record).primary_contact_flag;
3360                 x_assign_resources_tbl(l_current_record).primary_flag :=
3361                                          l_assign_resources_tbl(l_current_record).primary_contact_flag;
3362                 x_assign_resources_tbl(l_current_record).resource_source       := 'TERR';
3363 
3364                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
3365             END LOOP;
3366 
3367           -- added calendar call out
3368           -- this has been done as now the calendar check is done in a seperate procedure GET_AVAILABLE_RESOURCE
3369           -- changed on 29th September 2003
3370            -- The calendar flag check will not be done any more. The first available slot will be fetched
3371           -- This is for the preformance bug 3301417. If the calendar flag is Y then the resources will
3372            -- filtered based on availability in the procedure get_available_slot. This change is being done on
3373            -- 16 June 2004
3374            --IF (p_calendar_flag = 'Y') THEN
3375              -- change the API Name temporarily so that in case of unexpected error
3376              -- it is properly caught
3377               l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
3378               l_return_status_1 := x_return_status ;
3379               -- call the api to check resource availability
3380               get_available_resources
3381               (
3382                 p_init_msg_list                 =>  'F',
3383                 p_calendar_flag                 =>  p_calendar_flag,
3384                 p_effort_duration               =>  l_effort_duration,
3385                 p_effort_uom                    =>  l_effort_uom,
3386                 p_planned_start_date            =>  l_planned_start_date,
3387                 p_planned_end_date              =>  l_planned_end_date,
3388                 p_breakdown                     =>   null,
3389                 p_breakdown_uom                 =>   null,
3390                 p_continuous_task               =>  jtf_assign_pub.g_continuous_work,
3391                 x_return_status                 =>  x_return_status,
3392                 x_msg_count                     =>  x_msg_count,
3393                 x_msg_data                      =>  x_msg_data,
3394                 x_assign_resources_tbl          =>  x_assign_resources_tbl,
3395 		--Added for Bug # 5573916
3396 	        p_calendar_check                =>  p_calendar_check
3397 	        --Added for Bug # 5573916 Ends here
3398 		);
3399 
3400               -- set back the API name to original name
3401                l_api_name := l_api_name_1;
3402 
3403                IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
3404               -- Unexpected Execution Error from call to Get_contracts_resources
3405                  fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
3406                  fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
3407                  fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_TASK_RESOURCES');
3408                  fnd_msg_pub.add;
3409                  IF (x_return_status = fnd_api.g_ret_sts_error) THEN
3410                   RAISE fnd_api.g_exc_error;
3411                  ELSE
3412                    RAISE fnd_api.g_exc_unexpected_error;
3413                  END IF;
3414               END IF; -- end of x_return_status check
3415             -- end if; -- if p_calendar_flag = Y
3416 
3417             IF ( p_calendar_flag = 'Y' AND
3418                  x_assign_resources_tbl.count = 0 ) THEN
3419               fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
3420               fnd_msg_pub.add;
3421 --              RAISE fnd_api.g_exc_error;
3422             END IF;
3423 
3424           ELSE   -- No resources returned from the Territory API
3425             fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
3426             fnd_msg_pub.add;
3427 --            RAISE fnd_api.g_exc_error;
3428           END IF;
3429 
3430         ELSE  -- Territory Flag is NO
3431 
3432 -- Since Territory Flag (along with Contracts and IB Flags) is unchecked
3433 -- removed code which fetches Resources even if Calendar Flag is checked.
3434 -- This was done as we don't want to fetch all resources blindly any time.
3435 -- Fix for Bug 3308883.
3436 
3437           fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
3438           fnd_msg_pub.add;
3439         END IF; -- End of Territory_Flag = 'Y'
3440 
3441       END IF; -- End of l_task_source_code = 'SR'
3442 
3443     END IF; -- End of l_preferred_engineers_tbl.COUNT > 0
3444 
3445     -- Start of enhancement to add SUPPORT SITE ID and NAME to the OUT Table
3446 
3447     l_dynamic_sql3 := ' SELECT a.city city '||
3448                       ' FROM   hz_locations a, hz_party_sites b,  hz_party_site_uses c '||
3449                       ' WHERE  c.site_use_type = :1  AND '||
3450                       ' b.party_site_id        = :2  AND '||
3451                       ' a.location_id          = b.location_id   AND '||
3452                       ' c.party_site_id        = b.party_site_id ';
3453 
3454     IF x_assign_resources_tbl.COUNT > 0 THEN
3455 
3456       l_current_record := x_assign_resources_tbl.FIRST;
3457 
3458       WHILE l_current_record <= x_assign_resources_tbl.LAST
3459       LOOP
3460 
3461         l_rsc_type := resource_type_change(x_assign_resources_tbl(l_current_record).resource_type);
3462 
3463         OPEN  cur_support_site_id (x_assign_resources_tbl(l_current_record).resource_id,
3464                                    l_rsc_type);
3465         FETCH cur_support_site_id INTO x_assign_resources_tbl(l_current_record).support_site_id;
3466 
3467         IF (x_assign_resources_tbl(l_current_record).support_site_id IS NOT NULL) THEN
3468 
3469           OPEN  cur_support_site_name FOR l_dynamic_sql3
3470                 USING l_support_site,
3471                       x_assign_resources_tbl(l_current_record).support_site_id;
3472 
3473           FETCH cur_support_site_name INTO x_assign_resources_tbl(l_current_record).support_site_name;
3474           IF (  cur_support_site_name % NOTFOUND ) THEN
3475             x_assign_resources_tbl(l_current_record).support_site_name := NULL;
3476           END IF;
3477           CLOSE cur_support_site_name;
3478         ELSE
3479           x_assign_resources_tbl(l_current_record).support_site_id   := NULL;
3480           x_assign_resources_tbl(l_current_record).support_site_name := NULL;
3481 
3482         END IF;
3483 
3484         CLOSE cur_support_site_id;
3485 
3486         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
3487       END LOOP;
3488     END IF;
3489 
3490     -- End of enhancement
3491 
3492 
3493 
3494 
3495     -- Start of enhancement to add Web Availability to the OUT Table
3496 
3497 
3498     IF (x_assign_resources_tbl.COUNT > 0) THEN
3499 
3500       l_current_record := x_assign_resources_tbl.FIRST;
3501 
3502       WHILE l_current_record <= x_assign_resources_tbl.LAST
3503       LOOP
3504 
3505         IF ( UPPER(p_web_availability_flag) = 'Y') THEN
3506           l_rsc_type := resource_type_change(x_assign_resources_tbl(l_current_record).resource_type);
3507 
3508           OPEN  cur_web_availability (x_assign_resources_tbl(l_current_record).resource_id,
3509                                       l_rsc_type);
3510           FETCH cur_web_availability INTO l_rsc_id;
3511 
3512           IF (cur_web_availability%FOUND) THEN
3513             x_assign_resources_tbl(l_current_record).web_availability_flag := 'Y';
3514           ELSE
3515             x_assign_resources_tbl(l_current_record).web_availability_flag := 'N';
3516           END IF;
3517 
3518           CLOSE cur_web_availability;
3519         ELSE
3520           x_assign_resources_tbl(l_current_record).web_availability_flag := NULL;
3521         END IF; --p_web_availability_flag = 'Y'
3522 
3523         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
3524       END LOOP;
3525     END IF;
3526 
3527     -- End of enhancement
3528 
3529 
3530 end if; -- End of l_task_id is not null
3531 
3532 
3533     /* Standard call to get message count and
3534        the message information */
3535 
3536     FND_MSG_PUB.Count_And_Get
3537     (
3538       p_count => x_msg_count,
3539       p_data  => x_msg_data
3540     );
3541 
3542 
3543 
3544   EXCEPTION
3545     WHEN FND_API.G_EXC_ERROR THEN
3546       x_return_status := FND_API.G_RET_STS_ERROR ;
3547       FND_MSG_PUB.Count_And_Get
3548       (
3549         p_count => x_msg_count,
3550         p_data  => x_msg_data
3551       );
3552 
3553     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3554       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3555       FND_MSG_PUB.Count_And_Get
3556       (
3557         p_count => x_msg_count,
3558         p_data  => x_msg_data
3559       );
3560 
3561     WHEN OTHERS THEN
3562       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
3563       fnd_message.set_token('P_SQLCODE',SQLCODE);
3564       fnd_message.set_token('P_SQLERRM',SQLERRM);
3565       fnd_message.set_token('P_API_NAME',l_api_name);
3566       FND_MSG_PUB.add;
3567       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3568       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
3569         FND_MSG_PUB.Add_Exc_Msg
3570         (
3571           G_PKG_NAME,
3572           l_api_name
3573         );
3574       END IF;
3575 
3576       FND_MSG_PUB.Count_And_Get
3577       (
3578         p_count => x_msg_count,
3579         p_data  => x_msg_data
3580        );
3581 
3582   END GET_ASSIGN_TASK_RESOURCES;
3583 
3584 
3585 
3586 -- *******************************************************************************
3587 
3588 -- Start of comments
3589 
3590 --      API name        : _RESOURCES
3591 --      Type            : Public
3592 --      Function        : Determine the resources based on the customer
3593 --                        preferences or territory preferences and the
3594 --                        availability. This is when the calling doc is
3595 --                        SERVICE REQUEST.
3596 --      Pre-reqs        : None
3597 
3598 --      Parameters      :
3599 
3600 --      IN              : p_api_version         IN      NUMBER  Required
3601 --                        p_init_msg_list       IN      VARCHAR2 Optional
3602 --                                              DEFAULT = FND_API.G_FALSE
3603 --                        p_commit              IN      VARCHAR2 optional
3604 --                                              DEFAULT = FND_API.G_FALSE
3605 
3606 --     Assignment Manager Specific Parameters
3607 
3608 --     This determines the Resource Type required by the
3609 --     calling document
3610 --     p_resource_type                          VARCHAR2(30)
3611 --     p_role                                   VARCHAR2(30)
3612 
3613 --     This determines the number of resources required
3614 --     by the calling document
3615 --     p_no_of_resources                        NUMBER,
3616 
3617 --     This is for sending out the qualified resource directly
3618 --     to the calling form. Defaulted to the PROFILE value
3619 --     p_auto_select_flag                       VARCHAR2(1)
3620 --                                              : value of  Y or N
3621 
3622 --     This is to set the preference of CONTRACTS PREFERRED ENGINEERS
3623 --     Defaulted to the PROFILE value
3624 --     p_contracts_preferred_engineer           VARCHAR2(1)
3625 --                                              : value of  Y or N
3626 
3627 --     This is to set the preference of INSTALL BASE PREFERRED ENGINEERS
3628 --     Defaulted to the PROFILE value
3629 --     p_ib_preferred_engineer                  VARCHAR2(1)
3630 --                                              : value of  Y or N
3631 
3632 --     This is to fetch the CONTRACTS PREFERRED ENGINEERS
3633 --     p_contract_id                            NUMBER
3634 
3635 --     This is to fetch the INSTALL BASE PREFERRED ENGINEERS
3636 --     p_customer_product_id                    NUMBER
3637 
3638 --     The Effort required is determined by these two parameters
3639 --     p_effort_duration                        NUMBER
3640 --     p_effort_uom                             VARCHAR2(10)
3641 
3642 --     The Dates in which the resource is required, is determined
3643 --     by these two parameters
3644 --     p_start_date                             DATE
3645 --     p_end_date                               DATE
3646 
3647 --     The Territory Manager is accessed based on the value set
3648 --     p_territory_flag                         VARCHAR2(1)
3649 --                                              : value of  Y or N
3650 
3651 --     The Resource Availability is checked based on the value set
3652 --     p_calendar_flag                          VARCHAR2(1)
3653 --                                              : value of  Y or N
3654 
3655 --     This parameter contains the Calling Document ID
3656 --     In this case it is a SR_ID.
3657 --     p_sr_id                                   NUMBER  -- REQUIRED
3658 
3659 
3660 --     These parameters contain the Qualifier Values for
3661 --     the Calling Document
3662 --     p_sr_rec                                  JTF_TERRITORY_PUB.
3663 --                                               JTF_Serv_Req_rec_type
3664 --     p_sr_task_rec                             JTF_TERRITORY_PUB.
3665 --                                               JTF_Srv_Task_rec_type
3666 
3667 
3668 --     OUT              : x_return_status        OUT     VARCHAR2(1)
3669 --                        x_msg_count            OUT     NUMBER
3670 --                        x_msg_data             OUT     VARCHAR2(2000)
3671 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
3672 --                                                       AssignResources_tbl_type
3673 
3674 
3675 --      Version         : Current version        1.0
3676 --                        Initial version        1.0
3677 --
3678 --      Notes           :
3679 --
3680 
3681 -- End of comments
3682 
3683 -- *********************************************************************************
3684 
3685 
3686 
3687 --      Procedure definition with the parameters when the
3688 --      Source Document is SERVICE REQUEST
3689 
3690   PROCEDURE GET_ASSIGN_SR_RESOURCES
3691     (
3692         p_api_version                         IN  NUMBER,
3693         p_init_msg_list                       IN  VARCHAR2,
3694         p_resource_type                       IN  VARCHAR2,
3695         p_role                                IN  VARCHAR2,
3696         p_no_of_resources                     IN  NUMBER,
3697         p_auto_select_flag                    IN  VARCHAR2,
3698         p_contracts_preferred_engineer        IN  VARCHAR2,
3699         p_ib_preferred_engineer               IN  VARCHAR2,
3700         p_contract_id                         IN  NUMBER,
3701         p_customer_product_id                 IN  NUMBER,
3702         p_effort_duration                     IN  NUMBER,
3703         p_effort_uom                          IN  VARCHAR2,
3704         p_start_date                          IN  DATE,
3705         p_end_date                            IN  DATE,
3706         p_territory_flag                      IN  VARCHAR2,
3707         p_calendar_flag                       IN  VARCHAR2,
3708         p_web_availability_flag               IN  VARCHAR2,
3709         p_category_id                         IN  NUMBER,
3710         p_inventory_item_id                   IN  NUMBER,
3711 	p_inventory_org_id                    IN  NUMBER,
3712 	p_problem_code                        IN  VARCHAR2 ,
3713         p_sr_id                               IN  NUMBER,
3714         p_sr_rec                              IN  JTF_TERRITORY_PUB.JTF_Serv_Req_rec_type,
3715         p_sr_task_rec                         IN  JTF_TERRITORY_PUB.JTF_Srv_Task_rec_type,
3716         p_business_process_id                 IN  NUMBER,
3717         p_business_process_date               IN  DATE,
3718         p_filter_excluded_resource            IN  VARCHAR2,
3719         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
3720         x_return_status                       OUT NOCOPY VARCHAR2,
3721         x_msg_count                           OUT NOCOPY NUMBER,
3722         x_msg_data                            OUT NOCOPY VARCHAR2,
3723 	--Added for Bug # 5386560
3724 	p_inventory_component_id              IN  NUMBER   DEFAULT NULL,
3725         --Added for Bug # 5386560 Ends here
3726 	--Added for Bug # 5573916
3727 	p_calendar_check                IN  VARCHAR2 DEFAULT 'Y'
3728 	--Added for Bug # 5573916 Ends here
3729     )
3730   IS
3731 
3732     l_return_status_1                     VARCHAR2(10);
3733     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_SR_RESOURCES';
3734     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_SR_RESOURCES';
3735 
3736     l_api_version                         NUMBER        := 1.0;
3737     l_sr_id                               NUMBER;
3738     l_p_resource_type                     VARCHAR2(30)  := p_resource_type;
3739     l_planned_start_date                  DATE;
3740     l_planned_end_date                    DATE;
3741     l_expected_end_date                   DATE;     -- Added by SBARAT on 10/12/2004 for Bug 4052202
3742     l_effort_duration                     NUMBER;
3743     l_effort_uom                          VARCHAR2(30);
3744     l_contract_id                         NUMBER;
3745     l_cp_id                               NUMBER;
3746     l_contract_flag                       VARCHAR2(1)   := 'N';
3747     l_terr_cal_flag                       VARCHAR2(1)   := 'N';
3748 
3749     l_travel_time                         NUMBER        := 0;
3750     l_travel_uom                          VARCHAR2(10)  := 'HR';
3751 
3752     l_current_record                      INTEGER;
3753     l_current_rec                         INTEGER       := 0;
3754     l_pref_record                         INTEGER       := 0;
3755 
3756     l_assign_resources_tbl                JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
3757     l_tbl                                 JTF_ASSIGN_PUB.AssignResources_tbl_type;
3758 
3759 
3760     l_return_status                       VARCHAR2(10);
3761     l_msg_count                           NUMBER;
3762     l_msg_data                            VARCHAR2(2000);
3763 
3764     l_pref_res_order                      VARCHAR2(20) := 'BOTH';
3765     l_ib_preferred_engineers_tbl          JTF_ASSIGN_PUB.prfeng_tbl_type;
3766     l_con_preferred_engineers_tbl         JTF_ASSIGN_PUB.prfeng_tbl_type;
3767     l_preferred_engineers_tbl             JTF_ASSIGN_PUB.Preferred_Engineers_tbl_type;
3768 
3769     -- tables to handle excluded resource feature
3770     l_excluded_resource_tbl               JTF_ASSIGN_PUB.excluded_tbl_type;
3771     l_contracts_tbl                       JTF_ASSIGN_PUB.AssignResources_tbl_type;
3772     l_ib_tbl                              JTF_ASSIGN_PUB.AssignResources_tbl_type;
3773 
3774 
3775     l_x_planned_start_date                DATE;
3776     l_x_planned_end_date                  DATE;
3777     l_x_shift_construct_id                NUMBER;
3778     l_x_availability_type                 VARCHAR2(60);
3779 
3780     l_dynamic_sql1                        VARCHAR2(2000);
3781     l_dynamic_sql3                        VARCHAR2(2000);
3782 
3783     l_sr_comp_sub                         VARCHAR2(10);  -- Added by SBARAT on 11/01/2005 for Enh 4112155
3784     l_sr_task_comp_sub                    VARCHAR2(10);  -- Added by SBARAT on 11/01/2005 for Enh 4112155
3785 
3786 
3787     TYPE DYNAMIC_CUR_TYP   IS REF CURSOR;
3788     cur_cs_incidents       DYNAMIC_CUR_TYP;
3789     cur_support_site_name  DYNAMIC_CUR_TYP;
3790 
3791     l_support_site         VARCHAR2(15) := 'SUPPORT_SITE';
3792     l_rsc_type             VARCHAR2(30);
3793     l_rsc_id               NUMBER;
3794 
3795 
3796     CURSOR cur_support_site_id (p_rsc_id NUMBER, p_rsc_type VARCHAR2) IS
3797       SELECT support_site_id
3798       FROM   jtf_rs_resource_extns_vl
3799       WHERE  resource_id = p_rsc_id AND
3800              category    = p_rsc_type;
3801 
3802 
3803 
3804     CURSOR cur_web_availability (p_res_id NUMBER, p_res_type VARCHAR2) IS
3805       SELECT resource_id
3806       FROM   jtf_rs_web_available_v
3807       WHERE  resource_id = p_res_id AND
3808              category    = p_res_type;
3809 
3810 
3811 
3812     CURSOR cur_resource_skills (p_res_id NUMBER) IS
3813       SELECT skill_level, level_name
3814       FROM   jtf_rs_resource_skills a,
3815              jtf_rs_skill_levels_vl  b
3816       WHERE  a.skill_level_id = b.skill_level_id AND
3817              a.resource_id    = p_res_id AND
3818              (category_id     = p_category_id OR category_id IS NULL)   AND
3819              product_id       = p_inventory_item_id AND
3820              product_org_id   = p_inventory_org_id  AND
3821              component_id     IS NULL AND
3822              subcomponent_id  IS NULL;
3823 
3824     l_skill_level    NUMBER;
3825     l_skill_name     VARCHAR2(60);
3826     l_skill_ret_sts  VARCHAR2(1);
3827     l_skill_tbl      JTF_AM_FILTER_RESOURCE_PVT.skill_param_tbl_type;
3828     l_group_filter   VARCHAR2(100) := 'YES';
3829 
3830     -- added record type for service security check
3831     l_sr_sec_rec     JTF_AM_FILTER_RESOURCE_PVT.sr_rec_type;
3832 
3833  /*procedure ts(v varchar2)
3834     is
3835       pragma autonomous_transaction;
3836     begin
3837       insert into test_values values(v);
3838       commit;
3839     end;*/
3840   BEGIN
3841 
3842     SAVEPOINT get_assign_sr_resources;
3843 
3844 
3845     -- Started Assignment Manager API for SERVICE REQUESTS
3846 
3847 
3848     /* Standard call to check for call compatibility */
3849 
3850     IF NOT fnd_api.compatible_api_call (l_api_version,
3851                                         p_api_version,
3852                                         l_api_name,
3853                                         g_pkg_name) THEN
3854       RAISE fnd_api.g_exc_unexpected_error;
3855     END IF;
3856 
3857     /* Initialize message list if p_init_msg_list is set to TRUE */
3858 
3859     IF fnd_api.to_boolean (p_init_msg_list) THEN
3860       fnd_msg_pub.initialize;
3861     END IF;
3862 
3863 
3864     x_return_status := fnd_api.g_ret_sts_success;
3865 
3866 
3867     IF ( UPPER(p_resource_type) = 'RS_INDIVIDUAL') THEN
3868       l_p_resource_type := 'RS_INDIVIDUAL';
3869     ELSIF(p_resource_type is null) THEN
3870       l_p_resource_type := null;
3871     ELSE
3872       l_p_resource_type := p_resource_type;
3873     END IF;
3874 
3875 
3876     IF (p_sr_id IS NOT NULL ) THEN
3877       l_sr_id := p_sr_id;
3878     ELSIF (p_sr_rec.service_request_id IS NOT NULL) THEN
3879       l_sr_id := p_sr_rec.service_request_id;
3880     ELSIF (p_sr_task_rec.service_request_id IS NOT NULL) THEN
3881       l_sr_id := p_sr_task_rec.service_request_id;
3882     END IF;
3883 
3884     -- assign values to l_sr_sec_rec for SR Security Check
3885     l_sr_sec_rec.incident_id  := l_sr_id;
3886     IF(p_sr_rec.incident_type_id is not null)
3887     THEN
3888        l_sr_sec_rec.incident_type_id  := p_sr_rec.incident_type_id;
3889     ELSE
3890        l_sr_sec_rec.incident_type_id  := p_sr_task_rec.incident_type_id;
3891     END IF;
3892 
3893     /* Defaulting the variable values to IN parameters,
3894        if the IN paramaters have values given */
3895 
3896     IF (p_start_date IS NOT NULL) THEN
3897       l_planned_start_date := p_start_date;
3898     ELSE
3899       l_planned_start_date := SYSDATE;
3900     END IF;
3901 
3902     IF (p_end_date IS NOT NULL) THEN
3903       l_planned_end_date := p_end_date;
3904     ELSE
3905       l_planned_end_date := SYSDATE + 14;
3906     END IF;
3907 
3908     IF (p_effort_duration IS NOT NULL) THEN
3909       l_effort_duration := p_effort_duration;
3910     END IF;
3911 
3912     IF (p_effort_uom IS NOT NULL) THEN
3913       l_effort_uom := p_effort_uom;
3914     END IF;
3915 
3916     -- Get the Profile value to determine the order of preferred resources
3917 
3918     --l_pref_res_order  := FND_PROFILE.VALUE_SPECIFIC ( 'JTF_AM_PREF_RES_ORDER' ); --Commented out by SBARAT on 12/10/2004, Bug-3830061
3919     l_pref_res_order  := FND_PROFILE.VALUE ( 'JTF_AM_PREF_RES_ORDER' ); --Added by SBARAT on 12/10/2004, Bug-3830061
3920 
3921 
3922     IF ( p_contracts_preferred_engineer = 'Y'  OR
3923          p_ib_preferred_engineer        = 'Y') THEN
3924 
3925 
3926       l_contract_id := p_contract_id;
3927       l_cp_id       := p_customer_product_id;
3928 
3929 
3930       -- Code to fetch the Preferred Resources for saved SR
3931       IF (l_contract_id IS NULL AND
3932           l_cp_id       IS NULL AND
3933           l_sr_id       IS NOT NULL) THEN
3934 
3935         l_dynamic_sql1 :=  ' SELECT contract_service_id, customer_product_id, expected_resolution_date'||
3936                            ' FROM   cs_incidents_all_vl'||
3937                            ' WHERE  incident_id = :1';
3938 
3939         OPEN  cur_cs_incidents FOR  l_dynamic_sql1 USING l_sr_id;
3940         FETCH cur_cs_incidents INTO l_contract_id,
3941                                     l_cp_id,
3942                                     l_expected_end_date;   -- Added by SBARAT on 10/12/2004 for Bug 4052202
3943                                     --l_planned_end_date;  -- Commented by SBARAT on 10/12/2004 for Bug 4052202
3944 
3945         IF ( cur_cs_incidents%NOTFOUND ) THEN
3946           fnd_message.set_name('JTF', 'JTF_AM_INVALID_SR_ID');
3947           fnd_msg_pub.add;
3948           RAISE fnd_api.g_exc_error;
3949           /*
3950           ELSE
3951             l_contract_flag       := 'Y';
3952           */
3953         END IF;
3954 
3955         CLOSE cur_cs_incidents;
3956 
3957         /**********Start of addition by SBARAT on 10/12/2004 for Bug 4052202**********/
3958 
3959         IF ( l_expected_end_date IS NOT NULL) And (l_expected_end_date >= l_planned_start_date)
3960         THEN
3961 
3962             l_planned_end_date := l_expected_end_date;
3963 
3964         END IF;
3965 
3966         /**********End of Addition by SBARAT on 10/12/2004 for Bug 4052202**********/
3967 
3968       END IF;  -- end of l_contract_id and l_cp_id null check
3969 
3970     END IF;
3971 
3972 
3973 
3974    -- initiliaze the table type variables
3975    l_excluded_resource_tbl.delete;
3976    l_contracts_tbl.delete;
3977    l_ib_tbl.delete;
3978 
3979    -- get the contracts preferred and excluded engineers
3980    IF (p_contracts_preferred_engineer = 'Y') THEN
3981       get_contracts_resources
3982           (
3983             p_init_msg_list           =>  p_init_msg_list,
3984             p_contract_id             =>  l_contract_id,
3985             p_calendar_flag           =>  p_calendar_flag,
3986             p_effort_duration         =>  l_effort_duration,
3987             p_effort_uom              =>  l_effort_uom,
3988             p_planned_start_date      =>  l_planned_start_date,
3989             p_planned_end_date        =>  l_planned_end_date,
3990             p_resource_type           =>  l_p_resource_type,
3991             p_business_process_id     =>  p_business_process_id,
3992             p_business_process_date   =>  p_business_process_date,
3993             x_return_status           =>  x_return_status,
3994             x_msg_count               =>  x_msg_count,
3995             x_msg_data                =>  x_msg_data,
3996             x_assign_resources_tbl    =>  l_contracts_tbl,
3997             x_excluded_tbl            =>  l_excluded_resource_tbl,
3998 	    --Added for Bug # 5573916
3999 	    p_calendar_check          =>  p_calendar_check
4000 	    --Added for Bug # 5573916 Ends here
4001           );
4002 
4003           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4004             -- Unexpected Execution Error from call to Get_contracts_resources
4005             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
4006             fnd_message.set_token('P_PROC_NAME','GET_CONTRACTS_RESOURCES');
4007             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_SR_RESOURCES');
4008             fnd_msg_pub.add;
4009             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4010               RAISE fnd_api.g_exc_error;
4011             ELSE
4012               RAISE fnd_api.g_exc_unexpected_error;
4013             END IF;
4014           END IF;
4015     END IF;
4016 
4017    -- get the ib preferred and excluded engineers
4018     IF (p_ib_preferred_engineer = 'Y') THEN
4019 
4020           get_ib_resources
4021             (
4022               p_init_msg_list           =>  p_init_msg_list,
4023               p_customer_product_id     =>  l_cp_id,
4024               p_calendar_flag           =>  p_calendar_flag,
4025               p_effort_duration         =>  l_effort_duration,
4026               p_effort_uom              =>  l_effort_uom,
4027               p_planned_start_date      =>  l_planned_start_date,
4028               p_planned_end_date        =>  l_planned_end_date,
4029               p_resource_type           =>  l_p_resource_type,
4030               x_return_status           =>  x_return_status,
4031               x_msg_count               =>  x_msg_count,
4032               x_msg_data                =>  x_msg_data,
4033               x_assign_resources_tbl    =>  l_ib_tbl,
4034               x_excluded_tbl            =>  l_excluded_resource_tbl,
4035 	      --Added for Bug # 5573916
4036 	      p_calendar_check          =>  p_calendar_check
4037 	      --Added for Bug # 5573916 Ends here
4038             );
4039 
4040           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4041             -- Unexpected Execution Error from call to Get_contracts_resources
4042             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
4043             fnd_message.set_token('P_PROC_NAME','GET_IB_RESOURCES');
4044             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_SR_RESOURCES');
4045             fnd_msg_pub.add;
4046             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4047               RAISE fnd_api.g_exc_error;
4048             ELSE
4049               RAISE fnd_api.g_exc_unexpected_error;
4050             END IF;
4051           END IF;
4052 
4053     END IF; -- p_ib_preferred_engineer = 'Y'
4054 
4055    -- remove excluded resources , added on 3rd July 2003
4056   IF(p_filter_excluded_resource = 'Y')
4057   THEN
4058      IF(p_contracts_preferred_engineer = 'Y')
4059       THEN
4060           remove_excluded(x_res_tbl  => l_contracts_tbl,
4061                           x_exc_res  => l_excluded_resource_tbl);
4062       END IF;
4063       IF(p_ib_preferred_engineer = 'Y')
4064       THEN
4065           remove_excluded(x_res_tbl  => l_ib_tbl,
4066                           x_exc_res  => l_excluded_resource_tbl);
4067       END IF;
4068    END IF;
4069 
4070 
4071    -- pass returned resources through security check
4072    IF(l_contracts_tbl.count > 0)
4073    THEN
4074        JTF_AM_FILTER_RESOURCE_PVT.SERVICE_SECURITY_CHECK
4075          (   p_api_version                  =>1.0,
4076              x_assign_resources_tbl         =>l_contracts_tbl,
4077              p_sr_tbl                       =>l_sr_sec_rec,
4078              x_return_status                =>x_return_status ,
4079              x_msg_count                    =>x_msg_count,
4080              x_msg_data                     =>x_msg_data);
4081 
4082       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4083             -- Unexpected Execution Error from call to Get_contracts_resources
4084             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
4085             fnd_message.set_token('P_PROC_NAME','SERVICE_SECURITY_CHECK');
4086             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_SR_RESOURCES');
4087             fnd_msg_pub.add;
4088             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4089               RAISE fnd_api.g_exc_error;
4090             ELSE
4091               RAISE fnd_api.g_exc_unexpected_error;
4092             END IF;
4093       END IF;
4094 
4095    end IF; -- end of security check for contracts resource
4096 
4097    IF(l_ib_tbl.count > 0)
4098    THEN
4099        JTF_AM_FILTER_RESOURCE_PVT.SERVICE_SECURITY_CHECK
4100          (   p_api_version                  =>1.0,
4101              x_assign_resources_tbl         =>l_ib_tbl,
4102              p_sr_tbl                       =>l_sr_sec_rec,
4103              x_return_status                =>x_return_status ,
4104              x_msg_count                    =>x_msg_count,
4105              x_msg_data                     =>x_msg_data);
4106 
4107       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4108             -- Unexpected Execution Error from call to Get_contracts_resources
4109             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
4110             fnd_message.set_token('P_PROC_NAME','SERVICE_SECURITY_CHECK');
4111             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_SR_RESOURCES');
4112             fnd_msg_pub.add;
4113             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4114               RAISE fnd_api.g_exc_error;
4115             ELSE
4116               RAISE fnd_api.g_exc_unexpected_error;
4117             END IF;
4118       END IF;
4119 
4120    end IF; -- end of security check for contracts resource
4121 
4122 
4123 
4124 
4125 
4126 
4127     -- after the preferred engineers are obtained from contracts/ib, select the resources
4128     -- that are to be returned based on the value of the profile for search order and after
4129     -- filtering out excluded resources
4130     IF ( upper(l_pref_res_order) = 'CONTRACTS' )
4131     THEN
4132         IF (p_contracts_preferred_engineer = 'Y')
4133         THEN
4134             table_copy(l_contracts_tbl, x_assign_resources_tbl);
4135         END IF; -- p_contracts_preferred_engineer = 'Y'
4136 
4137 
4138         IF ( x_assign_resources_tbl.COUNT <= 0 )
4139         THEN
4140           IF (p_ib_preferred_engineer = 'Y')
4141           THEN
4142               table_copy(l_ib_tbl, x_assign_resources_tbl);
4143           END IF; -- p_ib_preferred_engineer = 'Y'
4144         END IF; -- x_assign_resources_tbl.COUNT <= 0
4145 
4146     ELSIF ( upper(l_pref_res_order) = 'IB' )
4147     THEN
4148 
4149         IF (p_ib_preferred_engineer = 'Y')
4150         THEN
4151             table_copy(l_ib_tbl, x_assign_resources_tbl);
4152         END IF; -- p_ib_preferred_engineer = 'Y'
4153 
4154         IF ( x_assign_resources_tbl.COUNT <= 0 )
4155         THEN
4156           IF (p_contracts_preferred_engineer = 'Y')
4157           THEN
4158               table_copy(l_contracts_tbl, x_assign_resources_tbl);
4159           END IF; -- p_contracts_preferred_engineer = 'Y'
4160         END IF; -- x_assign_resources_tbl.COUNT <= 0
4161 
4162     ELSE  -- l_pref_res_order = 'BOTH'
4163       /* Check if the Contracts Preferred Engineers Profile is SET If it is SET then get the available
4164          preferred engineers into the table of records */
4165         IF (p_contracts_preferred_engineer = 'Y')
4166         THEN
4167             table_copy(l_contracts_tbl, x_assign_resources_tbl);
4168         END IF; -- p_contracts_preferred_engineer = 'Y'
4169 
4170 
4171       /* Check if the Installed Base Preferred Engineers Profile is SET
4172          If it is SET then get the available preferred engineers
4173          into the table of records */
4174         IF (p_ib_preferred_engineer = 'Y')
4175         THEN
4176              table_copy(l_ib_tbl, x_assign_resources_tbl);
4177         END IF; -- p_ib_preferred_engineer = 'Y'
4178 
4179      END IF; -- l_pref_res_order = 'CONTRACTS' / 'IB' / 'BOTH'
4180 
4181     /* Actual Flow of Assignment Manager */
4182    -- if the Contracts and IB Preferred Engineers are nor found then Territory Qualified Resources are fetched
4183     IF x_assign_resources_tbl.COUNT > 0
4184     THEN
4185       NULL;
4186     ELSE -- x_assign_resources_tbl.COUNT <= 0
4187       -- If there are NO preferred engineers then call territory API
4188 
4189       /* Trace the Service Request Record Type
4190          to pass it to the Territory API */
4191 
4192   /**************** Start of addition by SBARAT on 11/01/2005 for Enh 4112155**************/
4193 
4194       JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type:=p_sr_rec;
4195       JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type:=p_sr_task_rec;
4196 
4197       Terr_Qual_Dyn_Check(l_sr_comp_sub, l_sr_task_comp_sub);
4198 
4199       JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type:=Null;
4200       JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type:=Null;
4201 
4202   /**************** End of addition by SBARAT on 11/01/2005 for Enh 4112155**************/
4203 
4204       IF
4205         (
4206           p_sr_rec.SERVICE_REQUEST_ID   IS NOT NULL OR
4207           p_sr_rec.PARTY_ID             IS NOT NULL OR
4208           p_sr_rec.COUNTRY              IS NOT NULL OR
4209           p_sr_rec.PARTY_SITE_ID        IS NOT NULL OR
4210           p_sr_rec.CITY                 IS NOT NULL OR
4211           p_sr_rec.POSTAL_CODE          IS NOT NULL OR
4212           p_sr_rec.STATE                IS NOT NULL OR
4213           p_sr_rec.AREA_CODE            IS NOT NULL OR
4214           p_sr_rec.COUNTY               IS NOT NULL OR
4215           p_sr_rec.COMP_NAME_RANGE      IS NOT NULL OR
4216           p_sr_rec.PROVINCE             IS NOT NULL OR
4217           p_sr_rec.NUM_OF_EMPLOYEES     IS NOT NULL OR
4218           p_sr_rec.INCIDENT_TYPE_ID     IS NOT NULL OR
4219           p_sr_rec.INCIDENT_SEVERITY_ID IS NOT NULL OR
4220           p_sr_rec.INCIDENT_URGENCY_ID  IS NOT NULL OR
4221           p_sr_rec.PROBLEM_CODE         IS NOT NULL OR
4222           p_sr_rec.INCIDENT_STATUS_ID   IS NOT NULL OR
4223           p_sr_rec.PLATFORM_ID          IS NOT NULL OR
4224           p_sr_rec.SUPPORT_SITE_ID      IS NOT NULL OR
4225           p_sr_rec.CUSTOMER_SITE_ID     IS NOT NULL OR
4226           p_sr_rec.SR_CREATION_CHANNEL  IS NOT NULL OR
4227           p_sr_rec.INVENTORY_ITEM_ID    IS NOT NULL OR
4228           p_sr_rec.ATTRIBUTE1           IS NOT NULL OR
4229           p_sr_rec.ATTRIBUTE2           IS NOT NULL OR
4230           p_sr_rec.ATTRIBUTE3           IS NOT NULL OR
4231           p_sr_rec.ATTRIBUTE4           IS NOT NULL OR
4232           p_sr_rec.ATTRIBUTE5           IS NOT NULL OR
4233           p_sr_rec.ATTRIBUTE6           IS NOT NULL OR
4234           p_sr_rec.ATTRIBUTE7           IS NOT NULL OR
4235           p_sr_rec.ATTRIBUTE8           IS NOT NULL OR
4236           p_sr_rec.ATTRIBUTE9           IS NOT NULL OR
4237           p_sr_rec.ATTRIBUTE10          IS NOT NULL OR
4238           p_sr_rec.ATTRIBUTE11          IS NOT NULL OR
4239           p_sr_rec.ATTRIBUTE12          IS NOT NULL OR
4240           p_sr_rec.ATTRIBUTE13          IS NOT NULL OR
4241           p_sr_rec.ATTRIBUTE14          IS NOT NULL OR
4242           p_sr_rec.ATTRIBUTE15          IS NOT NULL OR
4243           p_sr_rec.SQUAL_NUM12          IS NOT NULL OR
4244           p_sr_rec.SQUAL_NUM13          IS NOT NULL OR
4245           p_sr_rec.SQUAL_NUM14          IS NOT NULL OR
4246           p_sr_rec.SQUAL_NUM15          IS NOT NULL OR
4247           p_sr_rec.SQUAL_NUM16          IS NOT NULL OR
4248           p_sr_rec.SQUAL_NUM17          IS NOT NULL OR
4249           p_sr_rec.SQUAL_NUM18          IS NOT NULL OR
4250           p_sr_rec.SQUAL_NUM19          IS NOT NULL OR
4251           p_sr_rec.SQUAL_NUM30          IS NOT NULL OR
4252           p_sr_rec.SQUAL_CHAR11         IS NOT NULL OR
4253           p_sr_rec.SQUAL_CHAR12         IS NOT NULL OR
4254           p_sr_rec.SQUAL_CHAR13         IS NOT NULL OR
4255           p_sr_rec.SQUAL_CHAR20         IS NOT NULL OR
4256           p_sr_rec.SQUAL_CHAR21         IS NOT NULL OR
4257           p_sr_rec.DAY_OF_WEEK          IS NOT NULL OR
4258           p_sr_rec.TIME_OF_DAY          IS NOT NULL OR
4259           l_sr_comp_sub                 IS NOT NULL OR        -- Added by SBARAT on 11/01/2005 for Enh 4112155
4260           p_sr_rec.SQUAL_NUM60          IS NOT NULL           -- code added for system qualifier
4261         ) THEN
4262 
4263 /*ts('p_sr_rec.SERVICE_REQUEST_ID'||p_sr_rec.SERVICE_REQUEST_ID);
4264 ts('p_sr_rec.PARTY_ID'||p_sr_rec.PARTY_ID);
4265 ts('p_sr_rec.COUNTRY'||p_sr_rec.COUNTRY);
4266 ts('p_sr_rec.PARTY_SITE_ID'||p_sr_rec.PARTY_SITE_ID);
4267 ts('p_sr_rec.CITY'||p_sr_rec.CITY);
4268 ts('p_sr_rec.INCIDENT_TYPE_ID'||p_sr_rec.INCIDENT_TYPE_ID);
4269 ts('p_sr_rec.INCIDENT_SEVERITY_ID'||p_sr_rec.INCIDENT_SEVERITY_ID);
4270 ts('p_sr_rec.INCIDENT_URGENCY_ID'||p_sr_rec.INCIDENT_URGENCY_ID);
4271 ts('p_sr_rec.PROBLEM_CODE'||p_sr_rec.PROBLEM_CODE);
4272 ts('p_sr_rec.INCIDENT_STATUS_ID'||p_sr_rec.INCIDENT_STATUS_ID);
4273 ts('p_sr_rec.DAY_OF_WEEK'||p_sr_rec.DAY_OF_WEEK);
4274 ts('p_sr_rec.TIME_OF_DAY'||p_sr_rec.TIME_OF_DAY);
4275 ts('p_sr_rec.CUSTOMER_SITE_ID'||p_sr_rec.CUSTOMER_SITE_ID);ts('p_sr_rec.INVENTORY_ITEM_ID'||p_sr_rec.INVENTORY_ITEM_ID);
4276 ts('p_sr_rec.SR_CREATION_CHANNEL'||p_sr_rec.SR_CREATION_CHANNEL);ts('p_sr_rec.PLATFORM_ID'||p_sr_rec.PLATFORM_ID);*/
4277         IF (p_territory_flag = 'Y') THEN
4278 
4279           if(l_p_resource_type = 'RS_INDIVIDUAL')
4280           then
4281                 l_p_resource_type := null;
4282           end if;
4283 
4284           -- change the API Name temporarily so that in case of unexpected error
4285           -- it is properly caught
4286           l_api_name := l_api_name||'-JTF_TERR_SERVICE_PUB';
4287 
4288           JTF_TERR_SERVICE_PUB.Get_WinningTerrMembers
4289             (
4290                p_api_version_number  => l_api_version,
4291                p_init_msg_list       => p_init_msg_list,
4292                p_TerrServReq_Rec     => p_sr_rec,
4293                p_Resource_Type       => l_p_resource_type,
4294                p_Role                => p_role,
4295                x_return_status       => x_return_status,
4296                x_msg_count           => x_msg_count,
4297                x_msg_data            => x_msg_data,
4298                x_TerrResource_tbl    => l_assign_resources_tbl
4299             );
4300 
4301           -- set back the API name to original name
4302           l_api_name := l_api_name_1;
4303 
4304           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4305             -- Unexpected Execution Error from call to Territory Manager
4306             fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
4307             fnd_msg_pub.add;
4308             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4309               RAISE fnd_api.g_exc_error;
4310             ELSE
4311               RAISE fnd_api.g_exc_unexpected_error;
4312             END IF;
4313           END IF;
4314 
4315           -- added 2 april 2003 by sudarsana to convert RS_SUPPLIER TO RS_SUPPLIER_CONTACT
4316           IF(l_assign_resources_tbl.COUNT > 0)
4317           THEN
4318              l_current_record := l_assign_resources_tbl.FIRST;
4319              WHILE l_current_record <= l_assign_resources_tbl.LAST
4320              LOOP
4321                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
4322                 THEN
4323                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
4324                 END IF;
4325                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
4326              END LOOP;
4327           END IF;
4328 
4329         ELSE  -- Territory Flag is NO
4330 
4331 -- Since Territory Flag (along with Contracts and IB Flags) is unchecked
4332 -- removed code which fetches Resources even if Calendar Flag is checked.
4333 -- This was done as we don't want to fetch all resources blindly any time.
4334 -- Fix for Bug 3308883.
4335 
4336           fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
4337           fnd_msg_pub.add;
4338         END IF; -- End of p_territory_flag = 'Y'
4339 
4340       ELSIF
4341         (
4342           p_sr_task_rec.TASK_ID              IS NOT NULL OR
4343           p_sr_task_rec.SERVICE_REQUEST_ID   IS NOT NULL OR
4344           p_sr_task_rec.PARTY_ID             IS NOT NULL OR
4345           p_sr_task_rec.COUNTRY              IS NOT NULL OR
4346           p_sr_task_rec.PARTY_SITE_ID        IS NOT NULL OR
4347           p_sr_task_rec.CITY                 IS NOT NULL OR
4348           p_sr_task_rec.POSTAL_CODE          IS NOT NULL OR
4349           p_sr_task_rec.STATE                IS NOT NULL OR
4350           p_sr_task_rec.AREA_CODE            IS NOT NULL OR
4351           p_sr_task_rec.COUNTY               IS NOT NULL OR
4352           p_sr_task_rec.COMP_NAME_RANGE      IS NOT NULL OR
4353           p_sr_task_rec.PROVINCE             IS NOT NULL OR
4354           p_sr_task_rec.NUM_OF_EMPLOYEES     IS NOT NULL OR
4355           p_sr_task_rec.TASK_TYPE_ID         IS NOT NULL OR
4356           p_sr_task_rec.TASK_STATUS_ID       IS NOT NULL OR
4357           p_sr_task_rec.TASK_PRIORITY_ID     IS NOT NULL OR
4358           p_sr_task_rec.INCIDENT_TYPE_ID     IS NOT NULL OR
4359           p_sr_task_rec.INCIDENT_SEVERITY_ID IS NOT NULL OR
4360           p_sr_task_rec.INCIDENT_URGENCY_ID  IS NOT NULL OR
4361           p_sr_task_rec.PROBLEM_CODE         IS NOT NULL OR
4362           p_sr_task_rec.INCIDENT_STATUS_ID   IS NOT NULL OR
4363           p_sr_task_rec.PLATFORM_ID          IS NOT NULL OR
4364           p_sr_task_rec.SUPPORT_SITE_ID      IS NOT NULL OR
4365           p_sr_task_rec.CUSTOMER_SITE_ID     IS NOT NULL OR
4366           p_sr_task_rec.SR_CREATION_CHANNEL  IS NOT NULL OR
4367           p_sr_task_rec.INVENTORY_ITEM_ID    IS NOT NULL OR
4368           p_sr_task_rec.ATTRIBUTE1           IS NOT NULL OR
4369           p_sr_task_rec.ATTRIBUTE2           IS NOT NULL OR
4370           p_sr_task_rec.ATTRIBUTE3           IS NOT NULL OR
4371           p_sr_task_rec.ATTRIBUTE4           IS NOT NULL OR
4372           p_sr_task_rec.ATTRIBUTE5           IS NOT NULL OR
4373           p_sr_task_rec.ATTRIBUTE6           IS NOT NULL OR
4374           p_sr_task_rec.ATTRIBUTE7           IS NOT NULL OR
4375           p_sr_task_rec.ATTRIBUTE8           IS NOT NULL OR
4376           p_sr_task_rec.ATTRIBUTE9           IS NOT NULL OR
4377           p_sr_task_rec.ATTRIBUTE10          IS NOT NULL OR
4378           p_sr_task_rec.ATTRIBUTE11          IS NOT NULL OR
4379           p_sr_task_rec.ATTRIBUTE12          IS NOT NULL OR
4380           p_sr_task_rec.ATTRIBUTE13          IS NOT NULL OR
4381           p_sr_task_rec.ATTRIBUTE14          IS NOT NULL OR
4382           p_sr_task_rec.ATTRIBUTE15          IS NOT NULL OR
4383           p_sr_task_rec.SQUAL_NUM12          IS NOT NULL OR
4384           p_sr_task_rec.SQUAL_NUM13          IS NOT NULL OR
4385           p_sr_task_rec.SQUAL_NUM14          IS NOT NULL OR
4386           p_sr_task_rec.SQUAL_NUM15          IS NOT NULL OR
4387           p_sr_task_rec.SQUAL_NUM16          IS NOT NULL OR
4388           p_sr_task_rec.SQUAL_NUM17          IS NOT NULL OR
4389           p_sr_task_rec.SQUAL_NUM18          IS NOT NULL OR
4390           p_sr_task_rec.SQUAL_NUM19          IS NOT NULL OR
4391           p_sr_task_rec.SQUAL_NUM30          IS NOT NULL OR
4392           p_sr_task_rec.SQUAL_CHAR11         IS NOT NULL OR
4393           p_sr_task_rec.SQUAL_CHAR12         IS NOT NULL OR
4394           p_sr_task_rec.SQUAL_CHAR13         IS NOT NULL OR
4395           p_sr_task_rec.SQUAL_CHAR20         IS NOT NULL OR
4396           p_sr_task_rec.SQUAL_CHAR21         IS NOT NULL OR
4397           p_sr_task_rec.DAY_OF_WEEK          IS NOT NULL OR
4398           p_sr_task_rec.TIME_OF_DAY          IS NOT NULL OR
4399           l_sr_task_comp_sub                 IS NOT NULL OR       -- Added by SBARAT on 11/01/2005 for Enh 4112155
4400 		  p_sr_task_rec.SQUAL_NUM60               IS NOT NULL           -- code added for system qualifier
4401 
4402         ) THEN
4403 
4404 
4405         IF (p_territory_flag = 'Y') THEN
4406 
4407           IF (l_p_resource_type  = 'RS_INDIVIDUAL') THEN
4408               l_p_resource_type := NULL;
4409           END IF;
4410 
4411           -- change the API Name temporarily so that in case of unexpected error
4412           -- it is properly caught
4413           l_api_name := l_api_name||'-JTF_TERR_SERVICE_PUB';
4414 
4415           JTF_TERR_SERVICE_PUB.Get_WinningTerrMembers
4416           (
4417              p_api_version_number  => l_api_version,
4418              p_init_msg_list       => p_init_msg_list,
4419              p_TerrSrvTask_Rec     => p_sr_task_rec,
4420              p_Resource_Type       => l_p_resource_type,
4421              p_Role                => p_role,
4422              x_return_status       => x_return_status,
4423              x_msg_count           => x_msg_count,
4424              x_msg_data            => x_msg_data,
4425              x_TerrResource_tbl    => l_assign_resources_tbl
4426           );
4427 
4428           -- set back the API name to original name
4429           l_api_name := l_api_name_1;
4430 
4431           -- dbms_output.put_line('Count of TM :: '||l_assign_resources_tbl.count);
4432 
4433           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4434             -- Unexpected Execution Error from call to Territory Manager
4435             fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
4436             fnd_msg_pub.add;
4437             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4438               RAISE fnd_api.g_exc_error;
4439             ELSE
4440               RAISE fnd_api.g_exc_unexpected_error;
4441             END IF;
4442           END IF;
4443 
4444            -- added 2 april 2003 by sudarsana to conver RS_SUPPLIER TO RS_SUPPLIER_CONTACT
4445 
4446           IF(l_assign_resources_tbl.COUNT > 0)
4447           THEN
4448              l_current_record := l_assign_resources_tbl.FIRST;
4449              WHILE l_current_record <= l_assign_resources_tbl.LAST
4450              LOOP
4451                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
4452                 THEN
4453                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
4454                 END IF;
4455                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
4456              END LOOP;
4457           END IF;
4458 
4459         ELSE  -- Territory Flag is NO
4460 
4461 -- Since Territory Flag (along with Contracts and IB Flags) is unchecked
4462 -- removed code which fetches Resources even if Calendar Flag is checked.
4463 -- This was done as we don't want to fetch all resources blindly any time.
4464 -- Fix for Bug 3308883.
4465 
4466           fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
4467           fnd_msg_pub.add;
4468         END IF; -- End of p_territory_flag = 'Y'
4469 
4470       END IF; -- End of p_sr_rec IS NOT NULL or p_sr_task_rec IS NOT NULL
4471 
4472       -- removed the calendar check here. calling the procedure for calendar check in the next step
4473       -- calendar check removed 29th September 2003
4474       IF l_assign_resources_tbl.COUNT > 0 THEN
4475 
4476         l_current_rec    := 0;
4477         l_current_record := l_assign_resources_tbl.FIRST;
4478 
4479         WHILE(l_current_record <=  l_assign_resources_tbl.LAST)
4480         LOOP
4481             x_assign_resources_tbl(l_current_rec).terr_rsc_id           :=
4482                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
4483             x_assign_resources_tbl(l_current_rec).resource_id           :=
4484                                    l_assign_resources_tbl(l_current_record).resource_id;
4485             x_assign_resources_tbl(l_current_rec).resource_type         :=
4486                                    l_assign_resources_tbl(l_current_record).resource_type;
4487             x_assign_resources_tbl(l_current_rec).role                  :=
4488                                    l_assign_resources_tbl(l_current_record).role;
4489             x_assign_resources_tbl(l_current_rec).start_date            :=
4490                                    l_assign_resources_tbl(l_current_record).start_date;
4491             x_assign_resources_tbl(l_current_rec).end_date              :=
4492                                    l_assign_resources_tbl(l_current_record).end_date;
4493             x_assign_resources_tbl(l_current_rec).shift_construct_id    := NULL;
4494             x_assign_resources_tbl(l_current_rec).terr_id               :=
4495                                    l_assign_resources_tbl(l_current_record).terr_id;
4496             x_assign_resources_tbl(l_current_rec).terr_name             :=
4497                                    l_assign_resources_tbl(l_current_record).terr_name;
4498 	    -- ================code added for bug 6453896=============
4499 	    x_assign_resources_tbl(l_current_rec).terr_rank             :=
4500                                 l_assign_resources_tbl(l_current_record).ABSOLUTE_RANK;
4501 	    -- ================End for addition of code===============
4502             x_assign_resources_tbl(l_current_rec).primary_contact_flag  :=
4503                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
4504             x_assign_resources_tbl(l_current_rec).primary_flag  :=
4505                                        l_assign_resources_tbl(l_current_record).primary_contact_flag;
4506             x_assign_resources_tbl(l_current_rec).resource_source       := 'TERR';
4507 
4508             l_current_rec    := l_current_rec + 1;
4509             l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
4510        END LOOP;
4511 
4512 
4513       -- added calendar call out
4514       -- this has been done as now the calendar check is done in a seperate procedure GET_AVAILABLE_RESOURCE
4515       -- changed on 29th September 2003
4516        -- The calendar flag check will not be done any more. The first available slot will be fetched
4517         -- This is for the preformance bug 3301417. If the calendar flag is Y then the resources will
4518         -- filtered based on availability in the procedure get_available_slot. This change is being done on
4519         -- 16 June 2004
4520        --IF (p_calendar_flag = 'Y') THEN
4521           -- change the API Name temporarily so that in case of unexpected error
4522           -- it is properly caught
4523           l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
4524           l_return_status_1 := x_return_status ;
4525            -- call the api to check resource availability
4526 
4527 
4528            get_available_resources
4529             (
4530               p_init_msg_list                 =>  'F',
4531               p_calendar_flag                 =>   p_calendar_flag,
4532               p_effort_duration               =>  l_effort_duration,
4533               p_effort_uom                    =>  l_effort_uom,
4534               p_planned_start_date            =>  l_planned_start_date,
4535               p_planned_end_date              =>  l_planned_end_date,
4536               p_breakdown                     =>   null,
4537               p_breakdown_uom                 =>   null,
4538               p_continuous_task               =>   jtf_assign_pub.g_continuous_work,
4539               x_return_status                 =>   x_return_status,
4540               x_msg_count                     =>   x_msg_count,
4541               x_msg_data                      =>   x_msg_data,
4542               x_assign_resources_tbl          =>   x_assign_resources_tbl,
4543 	      --Added for Bug # 5573916
4544 	      p_calendar_check                =>   p_calendar_check
4545 	      --Added for Bug # 5573916 Ends here
4546 	      );
4547 
4548           -- set back the API name to original name
4549           l_api_name := l_api_name_1;
4550 
4551             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4552             -- Unexpected Execution Error from call to Get_contracts_resources
4553                fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
4554                fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
4555                fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_SR_RESOURCES');
4556                fnd_msg_pub.add;
4557                IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4558                   RAISE fnd_api.g_exc_error;
4559                ELSE
4560                   RAISE fnd_api.g_exc_unexpected_error;
4561                END IF;
4562              END IF; -- end of x_return_status check
4563        --  end if; -- if p_calendar_flag = Y
4564 
4565 
4566         IF ( p_calendar_flag = 'Y' AND
4567           x_assign_resources_tbl.count = 0 ) THEN
4568           fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
4569           fnd_msg_pub.add;
4570 --          RAISE fnd_api.g_exc_error;
4571         END IF;
4572 
4573 
4574          -- remove excluded resources from territory qualifeid resource list
4575          -- added 4th July 2003
4576          IF(p_filter_excluded_resource = 'Y')
4577          THEN
4578               remove_excluded(x_res_tbl  => x_assign_resources_tbl,
4579                               x_exc_res  => l_excluded_resource_tbl);
4580          END IF;
4581 
4582 
4583           IF(x_assign_resources_tbl.count > 0)
4584           THEN
4585              JTF_AM_FILTER_RESOURCE_PVT.SERVICE_SECURITY_CHECK
4586               (   p_api_version                  =>1.0,
4587                   x_assign_resources_tbl         =>x_assign_resources_tbl,
4588                   p_sr_tbl                       =>l_sr_sec_rec,
4589                   x_return_status                =>x_return_status ,
4590                   x_msg_count                    =>x_msg_count,
4591                   x_msg_data                     =>x_msg_data);
4592 
4593               IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
4594              -- Unexpected Execution Error from call to Get_contracts_resources
4595                   fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
4596                   fnd_message.set_token('P_PROC_NAME','SERVICE_SECURITY_CHECK');
4597                   fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_SR_RESOURCES');
4598                   fnd_msg_pub.add;
4599                   IF (x_return_status = fnd_api.g_ret_sts_error) THEN
4600                     RAISE fnd_api.g_exc_error;
4601                   ELSE
4602                     RAISE fnd_api.g_exc_unexpected_error;
4603                   END IF;
4604               END IF;
4605           END IF; -- end of security check
4606       ELSE   -- No resources returned from the Territory API
4607 
4608         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
4609         fnd_msg_pub.add;
4610 --        RAISE fnd_api.g_exc_error;
4611       END IF;
4612 
4613 
4614       -- Logic for getting resources belonging to group id passed in
4615       -- get the profile value to see if you have to filter by group membership
4616       -- to fix bug 2789319 on 3rd april 2003
4617       --l_group_filter :=  nvl(FND_PROFILE.VALUE_SPECIFIC ('JTF_AM_GROUP_MEMBER_FILTER'), 'YES'); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
4618       l_group_filter :=  nvl(FND_PROFILE.VALUE ('JTF_AM_GROUP_MEMBER_FILTER'), 'YES'); -- Added by SBARAT on 12/10/2004, Bug-3830061
4619 
4620 
4621       IF(l_group_filter = 'YES')
4622       THEN
4623          if((p_sr_rec.squal_num17 is not null) ) -- AND (p_resource_type = 'RS_INDIVIDUAL'))
4624          then
4625             get_group_resource(p_sr_rec.squal_num17 ,
4626                             x_assign_resources_tbl );
4627          elsif((p_sr_task_rec.squal_num17 is not null) ) --  AND (p_resource_type = 'RS_INDIVIDUAL'))
4628          then
4629             get_group_resource(p_sr_task_rec.squal_num17 ,
4630                             x_assign_resources_tbl );
4631          END IF;
4632 
4633       end if;
4634 
4635 
4636     END IF; -- End of x_assign_resources_tbl.COUNT > 0
4637 
4638 
4639 
4640 
4641 
4642     -- Start of enhancement for showing Individual Resources ONLY
4643 
4644 
4645     IF ( UPPER(p_resource_type) = 'RS_INDIVIDUAL' ) THEN
4646 
4647 
4648       -- Reconstructing the table to further do the enhancements
4649 
4650       IF ( x_assign_resources_tbl.COUNT > 0 ) THEN
4651 
4652         l_current_record := x_assign_resources_tbl.FIRST;
4653         l_current_rec    := 1;
4654 
4655         WHILE l_current_record <= x_assign_resources_tbl.LAST
4656         LOOP
4657 
4658           l_tbl(l_current_rec).resource_id           :=
4659                                  x_assign_resources_tbl(l_current_record).resource_id;
4660           l_tbl(l_current_rec).resource_type         :=
4661                                  x_assign_resources_tbl(l_current_record).resource_type;
4662 
4663           l_tbl(l_current_rec).start_date            :=
4664                                  x_assign_resources_tbl(l_current_record).start_date;
4665           l_tbl(l_current_rec).end_date              :=
4666                                  x_assign_resources_tbl(l_current_record).end_date;
4667           l_tbl(l_current_rec).shift_construct_id    :=
4668                                  x_assign_resources_tbl(l_current_record).shift_construct_id;
4669 
4670           l_tbl(l_current_rec).terr_rsc_id           :=
4671                                  x_assign_resources_tbl(l_current_record).terr_rsc_id;
4672           l_tbl(l_current_rec).role                  :=
4673                                  x_assign_resources_tbl(l_current_record).role;
4674           l_tbl(l_current_rec).terr_id               :=
4675                                  x_assign_resources_tbl(l_current_record).terr_id;
4676           l_tbl(l_current_rec).terr_name             :=
4677                                  x_assign_resources_tbl(l_current_record).terr_name;
4678 	  -- ================code added for bug 6453896=============
4679 	  l_tbl(l_current_rec).terr_rank             :=
4680                                 x_assign_resources_tbl(l_current_record).terr_rank;
4681 	  -- ================End for addition of code===============
4682           l_tbl(l_current_rec).primary_contact_flag  :=
4683                                  x_assign_resources_tbl(l_current_record).primary_contact_flag;
4684           l_tbl(l_current_rec).preference_type       :=
4685                                  x_assign_resources_tbl(l_current_record).preference_type;
4686           l_tbl(l_current_rec).group_id              :=
4687                                  x_assign_resources_tbl(l_current_record).group_id;
4688 
4689           l_tbl(l_current_rec).primary_flag          :=
4690                                  x_assign_resources_tbl(l_current_record).primary_flag;
4691 
4692           l_tbl(l_current_rec).resource_source       :=
4693                                  x_assign_resources_tbl(l_current_record).resource_source;
4694 
4695           l_current_rec    := l_current_rec + 1;
4696           l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
4697 
4698         END LOOP;
4699 
4700 
4701 
4702       END IF; --End of x_assign_resources_tbl.COUNT > 0
4703 
4704       -- End of reconstruction
4705 
4706 
4707 
4708 
4709       IF ( l_tbl.COUNT > 0 ) THEN
4710 
4711         l_current_record := l_tbl.FIRST;
4712         l_current_rec    := 0;
4713 
4714         -- Added by sroychou for rebuilding
4715         x_assign_resources_tbl.delete;
4716 
4717         WHILE l_current_record <= l_tbl.LAST
4718         LOOP
4719           IF ( UPPER(l_tbl(l_current_record).resource_type) NOT IN ( 'RS_GROUP', 'RS_TEAM')) THEN
4720 
4721             x_assign_resources_tbl(l_current_rec).resource_id           :=
4722                                    l_tbl(l_current_record).resource_id;
4723             x_assign_resources_tbl(l_current_rec).resource_type         :=
4724                                    l_tbl(l_current_record).resource_type;
4725 
4726             x_assign_resources_tbl(l_current_rec).start_date            :=
4727                                    l_tbl(l_current_record).start_date;
4728             x_assign_resources_tbl(l_current_rec).end_date              :=
4729                                    l_tbl(l_current_record).end_date;
4730             x_assign_resources_tbl(l_current_rec).shift_construct_id    :=
4731                                    l_tbl(l_current_record).shift_construct_id;
4732 
4733             x_assign_resources_tbl(l_current_rec).terr_rsc_id           :=
4734                                    l_tbl(l_current_record).terr_rsc_id;
4735             x_assign_resources_tbl(l_current_rec).role                  :=
4736                                    l_tbl(l_current_record).role;
4737             x_assign_resources_tbl(l_current_rec).terr_id               :=
4738                                    l_tbl(l_current_record).terr_id;
4739             x_assign_resources_tbl(l_current_rec).terr_name             :=
4740                                    l_tbl(l_current_record).terr_name;
4741 	    -- ================code added for bug 6453896=============
4742 	    x_assign_resources_tbl(l_current_rec).terr_rank             :=
4743                                 l_tbl(l_current_record).terr_rank;
4744 	    -- ================End for addition of code===============
4745             x_assign_resources_tbl(l_current_rec).primary_contact_flag  :=
4746                                    l_tbl(l_current_record).primary_contact_flag;
4747             x_assign_resources_tbl(l_current_rec).preference_type       :=
4748                                    l_tbl(l_current_record).preference_type;
4749             x_assign_resources_tbl(l_current_rec).group_id              :=
4750                                    l_tbl(l_current_record).group_id;
4751 
4752             x_assign_resources_tbl(l_current_rec).primary_flag          :=
4753                                    l_tbl(l_current_record).primary_flag;
4754 
4755             x_assign_resources_tbl(l_current_rec).resource_source       :=
4756                                    l_tbl(l_current_record).resource_source;
4757 
4758             l_current_rec := l_current_rec + 1;
4759 
4760           END IF;
4761           l_current_record := l_tbl.NEXT(l_current_record);
4762 
4763         END LOOP;
4764 
4765       END IF; --End of l_tbl.COUNT > 0
4766 
4767     END IF;   --End of p_resource_type = 'RS_INDIVIDUAL'
4768 
4769     -- End of enhancement
4770 
4771 
4772 
4773 
4774     -- Start of enhancement to add SUPPORT SITE ID and NAME to the OUT Table
4775 
4776     l_dynamic_sql3 := ' SELECT a.city city '||
4777                       ' FROM   hz_locations a, hz_party_sites b,  hz_party_site_uses c '||
4778                       ' WHERE  c.site_use_type = :1  AND '||
4779                       ' b.party_site_id        = :2 AND '||
4780                       ' a.location_id          = b.location_id   AND '||
4781                       ' c.party_site_id        = b.party_site_id ';
4782 
4783     IF x_assign_resources_tbl.COUNT > 0 THEN
4784 
4785       l_current_record := x_assign_resources_tbl.FIRST;
4786 
4787       WHILE l_current_record <= x_assign_resources_tbl.LAST
4788       LOOP
4789 
4790         l_rsc_type := resource_type_change(x_assign_resources_tbl(l_current_record).resource_type);
4791 
4792         OPEN  cur_support_site_id (x_assign_resources_tbl(l_current_record).resource_id,
4793                                    l_rsc_type);
4794         FETCH cur_support_site_id INTO x_assign_resources_tbl(l_current_record).support_site_id;
4795 
4796         IF (x_assign_resources_tbl(l_current_record).support_site_id IS NOT NULL) THEN
4797 
4798           OPEN  cur_support_site_name FOR l_dynamic_sql3
4799                 USING l_support_site,
4800                       x_assign_resources_tbl(l_current_record).support_site_id;
4801 
4802           FETCH cur_support_site_name INTO x_assign_resources_tbl(l_current_record).support_site_name;
4803           IF (  cur_support_site_name % NOTFOUND ) THEN
4804             x_assign_resources_tbl(l_current_record).support_site_name := NULL;
4805           END IF;
4806           CLOSE cur_support_site_name;
4807         ELSE
4808           x_assign_resources_tbl(l_current_record).support_site_id   := NULL;
4809           x_assign_resources_tbl(l_current_record).support_site_name := NULL;
4810 
4811         END IF;
4812 
4813         CLOSE cur_support_site_id;
4814 
4815         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
4816       END LOOP;
4817     END IF;
4818 
4819     -- End of enhancement
4820 
4821 
4822 
4823 
4824     -- Start of enhancement to add Web Availability to the OUT Table
4825 
4826 
4827     IF (x_assign_resources_tbl.COUNT > 0) THEN
4828 
4829       l_current_record := x_assign_resources_tbl.FIRST;
4830 
4831       WHILE l_current_record <= x_assign_resources_tbl.LAST
4832       LOOP
4833 
4834         IF ( UPPER(p_web_availability_flag) = 'Y') THEN
4835           l_rsc_type := resource_type_change(x_assign_resources_tbl(l_current_record).resource_type);
4836 
4837           OPEN  cur_web_availability (x_assign_resources_tbl(l_current_record).resource_id,
4838                                       l_rsc_type);
4839           FETCH cur_web_availability INTO l_rsc_id;
4840 
4841           IF (cur_web_availability%FOUND) THEN
4842             x_assign_resources_tbl(l_current_record).web_availability_flag := 'Y';
4843           ELSE
4844             x_assign_resources_tbl(l_current_record).web_availability_flag := 'N';
4845           END IF;
4846 
4847           CLOSE cur_web_availability;
4848         ELSE
4849           x_assign_resources_tbl(l_current_record).web_availability_flag := NULL;
4850         END IF; --p_web_availability_flag = 'Y'
4851 
4852         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
4853       END LOOP;
4854     END IF;
4855 
4856     -- End of enhancement
4857 
4858 
4859 
4860     -- Start of enhancement for skill level using Product Code
4861 
4862    /* IF ( p_category_id       IS NOT NULL OR
4863          p_inventory_item_id IS NOT NULL OR
4864          p_inventory_org_id  IS NOT NULL  ) THEN
4865 
4866 
4867       IF ( x_assign_resources_tbl.COUNT > 0 ) THEN
4868 
4869         l_current_record := x_assign_resources_tbl.FIRST;
4870 
4871         WHILE l_current_record <= x_assign_resources_tbl.LAST
4872         LOOP
4873 
4874           OPEN  cur_resource_skills (x_assign_resources_tbl(l_current_record).resource_id);
4875           FETCH cur_resource_skills INTO l_skill_level, l_skill_name;
4876           IF (  cur_resource_skills%FOUND  ) THEN
4877 
4878             IF ( l_skill_level > 0 ) THEN
4879               x_assign_resources_tbl(l_current_record).skill_level  := l_skill_level;
4880               x_assign_resources_tbl(l_current_record).skill_name   := l_skill_name;
4881             ELSE
4882               x_assign_resources_tbl(l_current_record).skill_level  := NULL;
4883               x_assign_resources_tbl(l_current_record).skill_name   := NULL;
4884             END IF; -- End of l_skill_level > 0
4885 
4886           END IF; -- End of cur_resource_skills%FOUND
4887           CLOSE cur_resource_skills;
4888 
4889 
4890           l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
4891 
4892         END LOOP;
4893 
4894       END IF; --End of x_assign_resources_tbl.COUNT > 0
4895 
4896     END IF;   --End of p_category_id IS NOT NULL
4897     */
4898 
4899 
4900     -- skills bank filter call to api  JTF_AM_FILTER_RESOURCE_PVT
4901 
4902      -- initialize values of the skill table
4903        l_skill_tbl(1).document_type   := 'SR';
4904        l_skill_tbl(1).category_id     := p_category_id;
4905        l_skill_tbl(1).product_id      := p_inventory_item_id;
4906        l_skill_tbl(1).product_org_id  := p_inventory_org_id;
4907        --Added for Bug # 5386560
4908        l_skill_tbl(1).component_id    := p_inventory_component_id;
4909        --Added for Bug # 5386560 Ends here
4910 
4911     -- changed problem code value to be talken from parameter instead of territory qualifier
4912     -- done by sudarsana on 24th april 2003
4913        l_skill_tbl(1).problem_code    := p_problem_code;
4914 
4915        -- change the API Name temporarily so that in case of unexpected error
4916        -- it is properly caught
4917        l_api_name := l_api_name||'-JTF_AM_FILTER_RESOURCE_PVT';
4918      JTF_AM_FILTER_RESOURCE_PVT.SEARCH_SKILL
4919                        ( p_api_version => 1.0,
4920                          x_assign_resources_tbl => x_assign_resources_tbl,
4921                          p_skill_param_tbl => l_skill_tbl,
4922                          x_return_status   => l_skill_ret_sts,
4923                          x_msg_count       => x_msg_count,
4924                          x_msg_data        => x_msg_data );
4925 
4926     -- set back the API name to original name
4927     l_api_name := l_api_name_1;
4928     if(l_skill_ret_sts <>  fnd_api.g_ret_sts_success)
4929     then
4930       IF (l_skill_ret_sts = fnd_api.g_ret_sts_error) THEN
4931         RAISE fnd_api.g_exc_error;
4932       ELSE
4933         RAISE fnd_api.g_exc_unexpected_error;
4934       END IF;
4935     end if;
4936 
4937     -- End of enhancement
4938 
4939 
4940 
4941 
4942     /* Standard call to get message count and
4943        the message information */
4944 
4945     FND_MSG_PUB.Count_And_Get
4946     (
4947       p_count => x_msg_count,
4948       p_data  => x_msg_data
4949     );
4950 
4951 
4952 
4953   EXCEPTION
4954     WHEN FND_API.G_EXC_ERROR THEN
4955       x_return_status := FND_API.G_RET_STS_ERROR ;
4956       FND_MSG_PUB.Count_And_Get
4957       (
4958         p_count => x_msg_count,
4959         p_data  => x_msg_data
4960       );
4961 
4962     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4963       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4964       FND_MSG_PUB.Count_And_Get
4965       (
4966         p_count => x_msg_count,
4967         p_data  => x_msg_data
4968       );
4969 
4970     WHEN OTHERS THEN
4971       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
4972       fnd_message.set_token('P_SQLCODE',SQLCODE);
4973       fnd_message.set_token('P_SQLERRM',SQLERRM);
4974       fnd_message.set_token('P_API_NAME',l_api_name);
4975       FND_MSG_PUB.add;
4976 
4977       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4978       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
4979         FND_MSG_PUB.Add_Exc_Msg
4980         (
4981           G_PKG_NAME,
4982           l_api_name
4983         );
4984       END IF;
4985 
4986       FND_MSG_PUB.Count_And_Get
4987       (
4988         p_count => x_msg_count,
4989         p_data  => x_msg_data
4990        );
4991 
4992   END GET_ASSIGN_SR_RESOURCES;
4993 
4994 
4995 /************** Addition by SBARAT on 01/11/2004 for Enh-3919046 ***********/
4996 
4997 -- *******************************************************************************
4998 
4999 -- Start of comments
5000 
5001 --      API name        : GET_ASSIGN_DR_RESOURCES
5002 --      Type            : Public
5003 --      Function        : Determine the resources based on the customer
5004 --                        preferences or territory preferences and the
5005 --                        availability. This is when the calling doc is
5006 --                        DEPOT REPAIR.
5007 --      Pre-reqs        : None
5008 
5009 --      Parameters      :
5010 
5011 --      IN              : p_api_version         IN      NUMBER  Required
5012 --                        p_init_msg_list       IN      VARCHAR2 Optional
5013 --                                              DEFAULT = FND_API.G_FALSE
5014 
5015 --     Assignment Manager Specific Parameters
5016 
5017 --     This determines the Resource Type required by the
5018 --     calling document
5019 --     p_resource_type                          VARCHAR2(30)
5020 --     p_role                                   VARCHAR2(30)
5021 
5022 --     This determines the number of resources required
5023 --     by the calling document
5024 --     p_no_of_resources                        NUMBER,
5025 
5026 --     This is for sending out the qualified resource directly
5027 --     to the calling form. Defaulted to the PROFILE value
5028 --     p_auto_select_flag                       VARCHAR2(1)
5029 --                                              : value of  Y or N
5030 
5031 --     This is to set the preference of CONTRACTS PREFERRED ENGINEERS
5032 --     Defaulted to the PROFILE value
5033 --     p_contracts_preferred_engineer           VARCHAR2(1)
5034 --                                              : value of  Y or N
5035 
5036 --     This is to set the preference of INSTALL BASE PREFERRED ENGINEERS
5037 --     Defaulted to the PROFILE value
5038 --     p_ib_preferred_engineer                  VARCHAR2(1)
5039 --                                              : value of  Y or N
5040 
5041 --     This is to fetch the CONTRACTS PREFERRED ENGINEERS
5042 --     p_contract_id                            NUMBER
5043 
5044 --     This is to fetch the INSTALL BASE PREFERRED ENGINEERS
5045 --     p_customer_product_id                    NUMBER
5046 
5047 --     The Effort required is determined by these two parameters
5048 --     p_effort_duration                        NUMBER
5049 --     p_effort_uom                             VARCHAR2(10)
5050 
5051 --     The Dates in which the resource is required, is determined
5052 --     by these two parameters
5053 --     p_start_date                             DATE
5054 --     p_end_date                               DATE
5055 
5056 --     The Territory Manager is accessed based on the value set
5057 --     p_territory_flag                         VARCHAR2(1)
5058 --                                              : value of  Y or N
5059 
5060 --     The Resource Availability is checked based on the value set
5061 --     p_calendar_flag                          VARCHAR2(1)
5062 --                                              : value of  Y or N
5063 
5064 --     This parameter contains the Calling Document ID
5065 --     In this case it is a DR_ID.
5066 --     p_dr_id                                   NUMBER  -- REQUIRED
5067 
5068 
5069 --     These parameters contain the Qualifier Values for
5070 --     the Calling Document
5071 --     p_dr_rec                                  JTF_ASSIGN_PUB.
5072 --                                               JTF_DR_rec_type
5073 
5074 --     OUT              : x_return_status        OUT     VARCHAR2(1)
5075 --                        x_msg_count            OUT     NUMBER
5076 --                        x_msg_data             OUT     VARCHAR2(2000)
5077 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
5078 --                                                       AssignResources_tbl_type
5079 
5080 
5081 --      Version         : Current version        1.0
5082 --                        Initial version        1.0
5083 --
5084 --      Notes           :
5085 --
5086 
5087 -- End of comments
5088 
5089 -- *********************************************************************************
5090 
5091 
5092 
5093 --      Procedure definition with the parameters when the
5094 --      Source Document is DEPOT REPAIR
5095 
5096 
5097   PROCEDURE GET_ASSIGN_DR_RESOURCES
5098     (   p_api_version                         IN  NUMBER,
5099         p_init_msg_list                       IN  VARCHAR2 ,
5100         p_resource_type                       IN  VARCHAR2 ,
5101         p_role                                IN  VARCHAR2 ,
5102         p_no_of_resources                     IN  NUMBER   ,
5103         p_auto_select_flag                    IN  VARCHAR2 ,
5104         p_contracts_preferred_engineer        IN  VARCHAR2 ,
5105         p_ib_preferred_engineer               IN  VARCHAR2 ,
5106         p_contract_id                         IN  NUMBER   ,
5107         p_customer_product_id                 IN  NUMBER   ,
5108         p_effort_duration                     IN  NUMBER   ,
5109         p_effort_uom                          IN  VARCHAR2 ,
5110         p_start_date                          IN  DATE     ,
5111         p_end_date                            IN  DATE     ,
5112         p_territory_flag                      IN  VARCHAR2 ,
5113         p_calendar_flag                       IN  VARCHAR2 ,
5114         p_web_availability_flag               IN  VARCHAR2 ,
5115         p_category_id                         IN  NUMBER   ,
5116         p_inventory_item_id                   IN  NUMBER   ,
5117         p_inventory_org_id                    IN  NUMBER   ,
5118         p_problem_code                        IN  VARCHAR2 ,
5119         p_dr_id                               IN  NUMBER,
5120         p_column_list                         IN  VARCHAR2 ,
5121         p_dr_rec                              IN  JTF_ASSIGN_PUB.JTF_DR_rec_type ,
5122         p_business_process_id                 IN  NUMBER,
5123         p_business_process_date               IN  DATE,
5124         p_filter_excluded_resource            IN  VARCHAR2,
5125         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
5126         x_return_status                       OUT NOCOPY VARCHAR2,
5127         x_msg_count                           OUT NOCOPY NUMBER,
5128         x_msg_data                            OUT NOCOPY VARCHAR2,
5129 	--Added for Bug # 5573916
5130 	p_calendar_check                IN  VARCHAR2 DEFAULT 'Y'
5131 	--Added for Bug # 5573916 Ends here
5132     )
5133   IS
5134 
5135     l_return_status_1                     VARCHAR2(10);
5136     l_api_name			            VARCHAR2(100)	:= 'GET_ASSIGN_DR_RESOURCES';
5137     l_api_name_1  	                  VARCHAR2(60)	:= 'GET_ASSIGN_DR_RESOURCES';
5138 
5139     l_api_version           	            NUMBER        := 1.0;
5140     l_no_of_resources                     NUMBER        := p_no_of_resources;
5141     l_auto_select_flag                    VARCHAR2(1)   := p_auto_select_flag;
5142     l_contracts_preferred_engineer        VARCHAR2(1)   := p_contracts_preferred_engineer;
5143     l_ib_preferred_engineer               VARCHAR2(1)   := p_ib_preferred_engineer;
5144     l_territory_flag                      VARCHAR2(1)   := p_territory_flag;
5145     l_calendar_flag                       VARCHAR2(1)   := p_calendar_flag;
5146 
5147     l_web_availability_flag               VARCHAR2(1)   := p_web_availability_flag;
5148 
5149     l_contracts_profile                   VARCHAR2(1);
5150     l_ib_profile                          VARCHAR2(1);
5151     l_auto_select_profile                 VARCHAR2(1);
5152     l_workflow_profile                    VARCHAR2(60);
5153 
5154     l_current_record                      INTEGER;
5155     l_dynamic_cursor                      INTEGER;
5156     l_dynamic_sql                         VARCHAR2(4000);
5157 
5158     l_return_code                         VARCHAR2(60);
5159     l_wf_return_status                    VARCHAR2(60);
5160     l_wf_msg_count                        NUMBER;
5161     l_wf_msg_data                         VARCHAR2(2000);
5162 
5163     l_bind_data_id                        NUMBER;
5164     l_workflow_key                        NUMBER;
5165 
5166     l_dr_rec                              JTF_TERRITORY_PUB.JTF_Srv_Task_rec_type;
5167     l_tbl                                 JTF_ASSIGN_PUB.AssignResources_tbl_type;
5168     l_assign_resources_tbl                JTF_ASSIGN_PUB.AssignResources_tbl_type;
5169     l_winningterrmember_tbl               JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
5170 
5171     l_dr_id                               NUMBER;
5172     l_p_resource_type                     VARCHAR2(30)  := p_resource_type;
5173     l_planned_start_date                  DATE;
5174     l_planned_end_date                    DATE;
5175     l_expected_end_date                   DATE;     -- Added by SBARAT on 10/12/2004 for Bug 4052202
5176     l_effort_duration                     NUMBER;
5177     l_effort_uom                          VARCHAR2(30);
5178     l_contract_id                         NUMBER;
5179     l_cp_id                               NUMBER;
5180     l_contract_flag                       VARCHAR2(1)   := 'N';
5181     l_terr_cal_flag                       VARCHAR2(1)   := 'N';
5182 
5183     l_travel_time                         NUMBER        := 0;
5184     l_travel_uom                          VARCHAR2(10)  := 'HR';
5185 
5186     l_current_rec                         INTEGER       := 0;
5187     l_pref_record                         INTEGER       := 0;
5188 
5189 
5190     l_return_status                       VARCHAR2(10);
5191     l_msg_count                           NUMBER;
5192     l_msg_data                            VARCHAR2(2000);
5193 
5194     l_pref_res_order                      VARCHAR2(20) := 'BOTH';
5195     l_ib_preferred_engineers_tbl          JTF_ASSIGN_PUB.prfeng_tbl_type;
5196     l_con_preferred_engineers_tbl         JTF_ASSIGN_PUB.prfeng_tbl_type;
5197     l_preferred_engineers_tbl             JTF_ASSIGN_PUB.Preferred_Engineers_tbl_type;
5198 
5199     -- tables to handle excluded resource feature
5200     l_excluded_resource_tbl               JTF_ASSIGN_PUB.excluded_tbl_type;
5201     l_contracts_tbl                       JTF_ASSIGN_PUB.AssignResources_tbl_type;
5202     l_ib_tbl                              JTF_ASSIGN_PUB.AssignResources_tbl_type;
5203 
5204 
5205     l_x_planned_start_date                DATE;
5206     l_x_planned_end_date                  DATE;
5207     l_x_shift_construct_id                NUMBER;
5208     l_x_availability_type                 VARCHAR2(60);
5209 
5210     l_dynamic_sql1                        VARCHAR2(2000);
5211     l_dynamic_sql3                        VARCHAR2(2000);
5212 
5213     TYPE DYNAMIC_CUR_TYP   IS REF CURSOR;
5214     cur_cs_incidents       DYNAMIC_CUR_TYP;
5215     cur_support_site_name  DYNAMIC_CUR_TYP;
5216 
5217     l_support_site         VARCHAR2(15) := 'SUPPORT_SITE';
5218     l_rsc_type             VARCHAR2(30);
5219     l_rsc_id               NUMBER;
5220 
5221     l_value                               VARCHAR2(100);
5222     l_count                               NUMBER:= 0;
5223     l_usage                               VARCHAR2(2000);
5224     l_uom_hour                            VARCHAR2(2000);
5225 
5226     l_skill_level    NUMBER;
5227     l_skill_name     VARCHAR2(60);
5228     l_skill_ret_sts  VARCHAR2(1);
5229     l_skill_tbl      JTF_AM_FILTER_RESOURCE_PVT.skill_param_tbl_type;
5230     l_group_filter   VARCHAR2(100) := 'YES';
5231 
5232     -- Record type for service security check
5233     l_dr_sec_rec     JTF_AM_FILTER_RESOURCE_PVT.sr_rec_type;
5234 
5235 
5236     CURSOR cur_resource_type IS
5237       SELECT object_code
5238       FROM   jtf_object_usages
5239       WHERE  object_user_code = 'RESOURCES' AND
5240              object_code      = p_resource_type;
5241     l_cur_resource_type cur_resource_type%ROWTYPE;
5242 
5243 
5244     CURSOR cur_res_location(p_rid NUMBER, p_rtype VARCHAR2) IS
5245       SELECT DECODE(source_postal_code, NULL, '00000', source_postal_code)
5246       FROM   jtf_rs_resource_extns_vl
5247       WHERE  resource_id     = p_rid AND
5248              'RS_'||category = p_rtype;
5249 
5250 
5251     --Bug# 4455803 MOAC.
5252     CURSOR check_date_cur(l_resource_id in number,
5253                           l_resource_type in varchar2)
5254         IS
5255      SELECT 'Y'
5256        FROM  jtf_task_resources_vl
5257       where   resource_id = l_resource_id
5258         and   resource_type = l_resource_type
5259         and   nvl(trunc(end_date_active), trunc(sysdate)) >= trunc(sysdate);
5260 
5261 
5262     CURSOR cur_support_site_id (p_rsc_id NUMBER, p_rsc_type VARCHAR2) IS
5263       SELECT support_site_id
5264       FROM   jtf_rs_resource_extns_vl
5265       WHERE  resource_id = p_rsc_id AND
5266              category    = p_rsc_type;
5267 
5268 
5269     CURSOR cur_web_availability (p_res_id NUMBER, p_res_type VARCHAR2) IS
5270       SELECT resource_id
5271       FROM   jtf_rs_web_available_v
5272       WHERE  resource_id = p_res_id AND
5273              category    = p_res_type;
5274 
5275 
5276     CURSOR cur_resource_skills (p_res_id NUMBER) IS
5277       SELECT skill_level, level_name
5278       FROM   jtf_rs_resource_skills a,
5279              jtf_rs_skill_levels_vl  b
5280       WHERE  a.skill_level_id = b.skill_level_id AND
5281              a.resource_id    = p_res_id AND
5282              (category_id     = p_category_id OR category_id IS NULL)   AND
5283              product_id       = p_inventory_item_id AND
5284              product_org_id   = p_inventory_org_id  AND
5285              component_id     IS NULL AND
5286              subcomponent_id  IS NULL;
5287 
5288   BEGIN
5289 
5290     SAVEPOINT jtf_assign_pub;
5291 
5292     -- Started Assignment Manager Public API
5293 
5294     /* Standard call to check for call compatibility */
5295 
5296     IF NOT fnd_api.compatible_api_call (l_api_version,
5297                                         p_api_version,
5298                                         l_api_name,
5299                                         g_pkg_name) THEN
5300       RAISE fnd_api.g_exc_unexpected_error;
5301     END IF;
5302 
5303 
5304     /* Initialize message list if p_init_msg_list is set to TRUE */
5305 
5306     IF fnd_api.to_boolean (p_init_msg_list) THEN
5307       fnd_msg_pub.initialize;
5308     END IF;
5309 
5310     x_return_status := fnd_api.g_ret_sts_success;
5311 
5312     /* Paramater Validation */
5313 
5314     IF (p_resource_type IS NOT NULL) THEN
5315       OPEN  cur_resource_type;
5316       FETCH cur_resource_type INTO l_cur_resource_type;
5317       IF ( cur_resource_type%NOTFOUND) THEN
5318         fnd_message.set_name('JTF', 'JTF_AM_INVALID_RESOURCE_TYPE');
5319         fnd_msg_pub.add;
5320         RAISE fnd_api.g_exc_error;
5321       END IF;
5322       CLOSE cur_resource_type;
5323     END IF;
5324 
5325 
5326     /* Getting the Profile values defined for the Assignment Manager */
5327 
5328     l_contracts_profile       := FND_PROFILE.VALUE ( 'ACTIVATE_CONTRACTS_PREFERRED_ENGINEERS' );
5329     l_auto_select_profile     := FND_PROFILE.VALUE ( 'ACTIVATE_AUTO_SELECT' );
5330     l_workflow_profile        := FND_PROFILE.VALUE ( 'ACTIVATE_WORKFLOW_NAME' );
5331     l_ib_profile              := FND_PROFILE.VALUE ( 'ACTIVATE_IB_PREFERRED_ENGINEERS');
5332     l_usage                   := fnd_profile.value ( 'JTF_AM_USAGE');
5333 
5334     /* Assigning the DEFAULT values for the Parameters */
5335 
5336     IF (p_auto_select_flag IS NULL) THEN
5337       l_auto_select_flag  := l_auto_select_profile; -- PROFILE VALUE is the default value
5338     ELSE
5339       l_auto_select_flag  := p_auto_select_flag;
5340     END IF;
5341 
5342 
5343     IF (p_contracts_preferred_engineer IS NULL) THEN
5344       l_contracts_preferred_engineer  := l_contracts_profile; -- PROFILE VALUE is the default value
5345     ELSE
5346       l_contracts_preferred_engineer  := p_contracts_preferred_engineer;
5347     END IF;
5348 
5349 
5350     IF (p_ib_preferred_engineer IS NULL) THEN
5351       l_ib_preferred_engineer  := l_ib_profile; -- PROFILE VALUE is the default value
5352     ELSE
5353       l_ib_preferred_engineer  := p_ib_preferred_engineer;
5354     END IF;
5355 
5356 
5357     IF (p_no_of_resources IS NULL) THEN
5358       l_no_of_resources  := 1;  -- 1 is the default value
5359     ELSE
5360       l_no_of_resources  := p_no_of_resources;
5361     END IF;
5362 
5363 
5364     IF ( (UPPER(p_territory_flag) = 'N') OR (p_territory_flag IS NULL)) THEN
5365       l_territory_flag  := 'N';
5366     ELSE
5367       l_territory_flag  := 'Y';  -- YES is the default value
5368     END IF;
5369 
5370 
5371     IF ( (UPPER(p_calendar_flag) = 'N') OR (p_calendar_flag IS NULL)) THEN
5372       l_calendar_flag  := 'N';
5373     ELSE
5374       l_calendar_flag  := 'Y';  -- YES is the default value
5375     END IF;
5376 
5377 
5378     IF ( p_web_availability_flag IS NULL) THEN
5379       l_web_availability_flag  := 'Y';
5380     ELSE
5381       l_web_availability_flag  := p_web_availability_flag;
5382     END IF;
5383 
5384 
5385      /* To handle the conversion of duration to hour */
5386 
5387     l_uom_hour  := nvl(fnd_profile.value('JTF_AM_TASK_HOUR'), 'HR');
5388     IF(nvl(p_effort_uom, l_uom_hour) <> l_uom_hour)
5389     THEN
5390          l_effort_duration :=  inv_convert.inv_um_convert(
5391                                    item_id => NULL,
5392                                    precision => 2,
5393                                    from_quantity => p_effort_duration,
5394                                    from_unit => p_effort_uom,
5395                                    to_unit   => l_uom_hour,
5396                                    from_name => NULL,
5397                                    to_name   => NULL);
5398     ELSE
5399         l_effort_duration := p_effort_duration;
5400     END IF;
5401 
5402     /* This assigning is being done because of the limitation for
5403        the direct use of the variables FND_API.MISS_NUM, MISS_CHAR etc. */
5404 
5405 
5406     /* Assigning values to the Depot Repair Record Type */
5407 
5408     l_dr_rec.TASK_ID              :=  p_dr_rec.TASK_ID;
5409     l_dr_rec.SERVICE_REQUEST_ID   :=  p_dr_rec.SERVICE_REQUEST_ID;
5410     l_dr_rec.PARTY_ID             :=  p_dr_rec.PARTY_ID;
5411     l_dr_rec.COUNTRY              :=  p_dr_rec.COUNTRY;
5412     l_dr_rec.PARTY_SITE_ID        :=  p_dr_rec.PARTY_SITE_ID;
5413     l_dr_rec.CITY                 :=  p_dr_rec.CITY;
5414     l_dr_rec.POSTAL_CODE          :=  p_dr_rec.POSTAL_CODE;
5415     l_dr_rec.STATE                :=  p_dr_rec.STATE;
5416     l_dr_rec.AREA_CODE            :=  p_dr_rec.AREA_CODE;
5417     l_dr_rec.COUNTY               :=  p_dr_rec.COUNTY;
5418     l_dr_rec.COMP_NAME_RANGE      :=  p_dr_rec.COMP_NAME_RANGE;
5419     l_dr_rec.PROVINCE             :=  p_dr_rec.PROVINCE;
5420     l_dr_rec.NUM_OF_EMPLOYEES     :=  p_dr_rec.NUM_OF_EMPLOYEES;
5421     l_dr_rec.TASK_TYPE_ID         :=  p_dr_rec.TASK_TYPE_ID;
5422     l_dr_rec.TASK_STATUS_ID       :=  p_dr_rec.TASK_STATUS_ID;
5423     l_dr_rec.TASK_PRIORITY_ID     :=  p_dr_rec.TASK_PRIORITY_ID;
5424     l_dr_rec.INCIDENT_TYPE_ID     :=  p_dr_rec.INCIDENT_TYPE_ID;
5425     l_dr_rec.INCIDENT_SEVERITY_ID :=  p_dr_rec.INCIDENT_SEVERITY_ID;
5426     l_dr_rec.INCIDENT_URGENCY_ID  :=  p_dr_rec.INCIDENT_URGENCY_ID;
5427     l_dr_rec.PROBLEM_CODE         :=  p_dr_rec.PROBLEM_CODE;
5428     l_dr_rec.INCIDENT_STATUS_ID   :=  p_dr_rec.INCIDENT_STATUS_ID;
5429     l_dr_rec.PLATFORM_ID          :=  p_dr_rec.PLATFORM_ID;
5430     l_dr_rec.SUPPORT_SITE_ID      :=  p_dr_rec.SUPPORT_SITE_ID;
5431     l_dr_rec.CUSTOMER_SITE_ID     :=  p_dr_rec.CUSTOMER_SITE_ID;
5432     l_dr_rec.SR_CREATION_CHANNEL  :=  p_dr_rec.SR_CREATION_CHANNEL;
5433     l_dr_rec.INVENTORY_ITEM_ID    :=  p_dr_rec.INVENTORY_ITEM_ID;
5434     l_dr_rec.ATTRIBUTE1           :=  p_dr_rec.ATTRIBUTE1;
5435     l_dr_rec.ATTRIBUTE2           :=  p_dr_rec.ATTRIBUTE2;
5436     l_dr_rec.ATTRIBUTE3           :=  p_dr_rec.ATTRIBUTE3;
5437     l_dr_rec.ATTRIBUTE4           :=  p_dr_rec.ATTRIBUTE4;
5438     l_dr_rec.ATTRIBUTE5           :=  p_dr_rec.ATTRIBUTE5;
5439     l_dr_rec.ATTRIBUTE6           :=  p_dr_rec.ATTRIBUTE6;
5440     l_dr_rec.ATTRIBUTE7           :=  p_dr_rec.ATTRIBUTE7;
5441     l_dr_rec.ATTRIBUTE8           :=  p_dr_rec.ATTRIBUTE8;
5442     l_dr_rec.ATTRIBUTE9           :=  p_dr_rec.ATTRIBUTE9;
5443     l_dr_rec.ATTRIBUTE10          :=  p_dr_rec.ATTRIBUTE10;
5444     l_dr_rec.ATTRIBUTE11          :=  p_dr_rec.ATTRIBUTE11;
5445     l_dr_rec.ATTRIBUTE12          :=  p_dr_rec.ATTRIBUTE12;
5446     l_dr_rec.ATTRIBUTE13          :=  p_dr_rec.ATTRIBUTE13;
5447     l_dr_rec.ATTRIBUTE14          :=  p_dr_rec.ATTRIBUTE14;
5448     l_dr_rec.ATTRIBUTE15          :=  p_dr_rec.ATTRIBUTE15;
5449     l_dr_rec.ORGANIZATION_ID      :=  p_dr_rec.ORGANIZATION_ID;
5450     l_dr_rec.SQUAL_NUM12          :=  p_dr_rec.SQUAL_NUM12;
5451     l_dr_rec.SQUAL_NUM13          :=  p_dr_rec.SQUAL_NUM13;
5452     l_dr_rec.SQUAL_NUM14          :=  p_dr_rec.SQUAL_NUM14;
5453     l_dr_rec.SQUAL_NUM15          :=  p_dr_rec.SQUAL_NUM15;
5454     l_dr_rec.SQUAL_NUM16          :=  p_dr_rec.SQUAL_NUM16;
5455     l_dr_rec.SQUAL_NUM17          :=  p_dr_rec.SQUAL_NUM17;
5456     l_dr_rec.SQUAL_NUM18          :=  p_dr_rec.SQUAL_NUM18;
5457     l_dr_rec.SQUAL_NUM19          :=  p_dr_rec.SQUAL_NUM19;
5458     l_dr_rec.SQUAL_NUM30          :=  p_dr_rec.SQUAL_NUM30;
5459     l_dr_rec.SQUAL_CHAR11         :=  p_dr_rec.SQUAL_CHAR11;
5460     l_dr_rec.SQUAL_CHAR12         :=  p_dr_rec.SQUAL_CHAR12;
5461     l_dr_rec.SQUAL_CHAR13         :=  p_dr_rec.SQUAL_CHAR13;
5462     l_dr_rec.SQUAL_CHAR20         :=  p_dr_rec.SQUAL_CHAR20;
5463     l_dr_rec.SQUAL_CHAR21         :=  p_dr_rec.SQUAL_CHAR21;
5464 
5465 
5466     IF ( UPPER(p_resource_type) = 'RS_INDIVIDUAL') THEN
5467       l_p_resource_type := 'RS_INDIVIDUAL';
5468     ELSIF(p_resource_type is null) THEN
5469       l_p_resource_type := null;
5470     ELSE
5471       l_p_resource_type := p_resource_type;
5472     END IF;
5473 
5474 
5475     IF (p_dr_id IS NOT NULL ) THEN
5476       l_dr_id := p_dr_id;
5477     ELSIF (p_dr_rec.service_request_id IS NOT NULL) THEN
5478       l_dr_id := p_dr_rec.service_request_id;
5479     END IF;
5480 
5481     -- Assign values to l_dr_sec_rec for DR Security Check
5482     l_dr_sec_rec.incident_id  := l_dr_id;
5483     l_dr_sec_rec.incident_type_id  := p_dr_rec.incident_type_id;
5484 
5485 
5486     /* Defaulting the variable values to IN parameters,
5487        if the IN paramaters have values given */
5488 
5489     IF (p_start_date IS NOT NULL) THEN
5490       l_planned_start_date := p_start_date;
5491     ELSE
5492       l_planned_start_date := SYSDATE;
5493     END IF;
5494 
5495     IF (p_end_date IS NOT NULL) THEN
5496       l_planned_end_date := p_end_date;
5497     ELSE
5498       l_planned_end_date := SYSDATE + 14;
5499     END IF;
5500 
5501     IF (p_effort_duration IS NOT NULL) THEN
5502       l_effort_duration := p_effort_duration;
5503     END IF;
5504 
5505     IF (p_effort_uom IS NOT NULL) THEN
5506       l_effort_uom := p_effort_uom;
5507     END IF;
5508 
5509     -- Get the Profile value to determine the order of preferred resources
5510 
5511     l_pref_res_order  := FND_PROFILE.VALUE ( 'JTF_AM_PREF_RES_ORDER' );
5512 
5513 
5514     IF ( p_contracts_preferred_engineer = 'Y'  OR
5515          p_ib_preferred_engineer        = 'Y') THEN
5516 
5517 
5518       l_contract_id := p_contract_id;
5519       l_cp_id       := p_customer_product_id;
5520 
5521 
5522       -- Code to fetch the Preferred Resources for saved DR
5523       IF (l_contract_id IS NULL AND
5524           l_cp_id       IS NULL AND
5525           l_dr_id       IS NOT NULL) THEN
5526 
5527         l_dynamic_sql1 :=  ' SELECT contract_service_id, customer_product_id, expected_resolution_date'||
5528                            ' FROM   cs_incidents_all_vl'||
5529                            ' WHERE  incident_id = :1';
5530 
5531         OPEN  cur_cs_incidents FOR  l_dynamic_sql1 USING l_dr_id;
5532         FETCH cur_cs_incidents INTO l_contract_id,
5533                                     l_cp_id,
5534                                     l_expected_end_date;    --Added by SBARAT on 10/12/2004 for bug 4052202
5535                                     --l_planned_end_date;   --Commented out by SBARAT on 10/12/2004 for bug 4052202
5536 
5537         IF ( cur_cs_incidents%NOTFOUND ) THEN
5538           fnd_message.set_name('JTF', 'JTF_AM_INVALID_DR_ID');
5539           fnd_msg_pub.add;
5540           RAISE fnd_api.g_exc_error;
5541         END IF;
5542 
5543         CLOSE cur_cs_incidents;
5544 
5545         /**********Start of addition by SBARAT on 10/12/2004 for Bug 4052202**********/
5546         --since this API for DR is same as SR_TASK, added this check to handle
5547         --NULL or l_expected_end_date < l_planned_start_date as done for SR, SR_TASK
5548 
5549         IF ( l_expected_end_date IS NOT NULL) And (l_expected_end_date >= l_planned_start_date)
5550         THEN
5551 
5552             l_planned_end_date := l_expected_end_date;
5553 
5554         END IF;
5555 
5556        /**********End of Addition by SBARAT on 10/12/2004 for Bug 4052202**********/
5557 
5558       END IF;  -- end of l_contract_id and l_cp_id null check
5559 
5560     END IF;
5561 
5562     /* Actual Flow of Assignment Manager*/
5563 
5564    -- initiliaze the table type variables
5565    l_excluded_resource_tbl.delete;
5566    l_contracts_tbl.delete;
5567    l_ib_tbl.delete;
5568 
5569    -- get the contracts preferred and excluded engineers
5570    IF (p_contracts_preferred_engineer = 'Y') THEN
5571       get_contracts_resources
5572           (
5573             p_init_msg_list           =>  p_init_msg_list,
5574             p_contract_id             =>  l_contract_id,
5575             p_calendar_flag           =>  p_calendar_flag,
5576             p_effort_duration         =>  l_effort_duration,
5577             p_effort_uom              =>  l_effort_uom,
5578             p_planned_start_date      =>  l_planned_start_date,
5579             p_planned_end_date        =>  l_planned_end_date,
5580             p_resource_type           =>  l_p_resource_type,
5581             p_business_process_id     =>  p_business_process_id,
5582             p_business_process_date   =>  p_business_process_date,
5583             x_return_status           =>  x_return_status,
5584             x_msg_count               =>  x_msg_count,
5585             x_msg_data                =>  x_msg_data,
5586             x_assign_resources_tbl    =>  l_contracts_tbl,
5587             x_excluded_tbl            =>  l_excluded_resource_tbl,
5588 	    --Added for Bug # 5573916
5589 	    p_calendar_check          =>  p_calendar_check
5590 	    --Added for Bug # 5573916 Ends here
5591           );
5592 
5593           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
5594             -- Unexpected Execution Error from call to Get_contracts_resources
5595             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
5596             fnd_message.set_token('P_PROC_NAME','GET_CONTRACTS_RESOURCES');
5597             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_DR_RESOURCES');
5598             fnd_msg_pub.add;
5599             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
5600               RAISE fnd_api.g_exc_error;
5601             ELSE
5602               RAISE fnd_api.g_exc_unexpected_error;
5603             END IF;
5604           END IF;
5605     END IF;
5606 
5607    -- get the ib preferred and excluded engineers
5608     IF (p_ib_preferred_engineer = 'Y') THEN
5609 
5610           get_ib_resources
5611             (
5612               p_init_msg_list           =>  p_init_msg_list,
5613               p_customer_product_id     =>  l_cp_id,
5614               p_calendar_flag           =>  p_calendar_flag,
5615               p_effort_duration         =>  l_effort_duration,
5616               p_effort_uom              =>  l_effort_uom,
5617               p_planned_start_date      =>  l_planned_start_date,
5618               p_planned_end_date        =>  l_planned_end_date,
5619               p_resource_type           =>  l_p_resource_type,
5620               x_return_status           =>  x_return_status,
5621               x_msg_count               =>  x_msg_count,
5622               x_msg_data                =>  x_msg_data,
5623               x_assign_resources_tbl    =>  l_ib_tbl,
5624               x_excluded_tbl            =>  l_excluded_resource_tbl,
5625 	      --Added for Bug # 5573916
5626 	      p_calendar_check          =>  p_calendar_check
5627 	      --Added for Bug # 5573916 Ends here
5628             );
5629 
5630           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
5631             -- Unexpected Execution Error from call to Get_contracts_resources
5632             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
5633             fnd_message.set_token('P_PROC_NAME','GET_IB_RESOURCES');
5634             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_DR_RESOURCES');
5635             fnd_msg_pub.add;
5636             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
5637               RAISE fnd_api.g_exc_error;
5638             ELSE
5639               RAISE fnd_api.g_exc_unexpected_error;
5640             END IF;
5641           END IF;
5642 
5643     END IF; -- p_ib_preferred_engineer = 'Y'
5644 
5645    -- remove excluded resources
5646   IF(p_filter_excluded_resource = 'Y')
5647   THEN
5648      IF(p_contracts_preferred_engineer = 'Y')
5649       THEN
5650           remove_excluded(x_res_tbl  => l_contracts_tbl,
5651                           x_exc_res  => l_excluded_resource_tbl);
5652       END IF;
5653       IF(p_ib_preferred_engineer = 'Y')
5654       THEN
5655           remove_excluded(x_res_tbl  => l_ib_tbl,
5656                           x_exc_res  => l_excluded_resource_tbl);
5657       END IF;
5658    END IF;
5659 
5660 
5661    -- pass returned resources through security check
5662    IF(l_contracts_tbl.count > 0)
5663    THEN
5664        JTF_AM_FILTER_RESOURCE_PVT.SERVICE_SECURITY_CHECK
5665          (   p_api_version                  =>1.0,
5666              x_assign_resources_tbl         =>l_contracts_tbl,
5667              p_sr_tbl                       =>l_dr_sec_rec,
5668              x_return_status                =>x_return_status ,
5669              x_msg_count                    =>x_msg_count,
5670              x_msg_data                     =>x_msg_data);
5671 
5672       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
5673             -- Unexpected Execution Error from call to Get_contracts_resources
5674             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
5675             fnd_message.set_token('P_PROC_NAME','SERVICE_SECURITY_CHECK');
5676             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_DR_RESOURCES');
5677             fnd_msg_pub.add;
5678             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
5679               RAISE fnd_api.g_exc_error;
5680             ELSE
5681               RAISE fnd_api.g_exc_unexpected_error;
5682             END IF;
5683       END IF;
5684 
5685    end IF; -- end of security check for contracts resource
5686 
5687    IF(l_ib_tbl.count > 0)
5688    THEN
5689        JTF_AM_FILTER_RESOURCE_PVT.SERVICE_SECURITY_CHECK
5690          (   p_api_version                  =>1.0,
5691              x_assign_resources_tbl         =>l_ib_tbl,
5692              p_sr_tbl                       =>l_dr_sec_rec,
5693              x_return_status                =>x_return_status ,
5694              x_msg_count                    =>x_msg_count,
5695              x_msg_data                     =>x_msg_data);
5696 
5697       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
5698             -- Unexpected Execution Error from call to Get_contracts_resources
5699             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
5700             fnd_message.set_token('P_PROC_NAME','SERVICE_SECURITY_CHECK');
5701             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_DR_RESOURCES');
5702             fnd_msg_pub.add;
5703             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
5704               RAISE fnd_api.g_exc_error;
5705             ELSE
5706               RAISE fnd_api.g_exc_unexpected_error;
5707             END IF;
5708       END IF;
5709 
5710    end IF; -- end of security check for contracts resource
5711 
5712 
5713     -- after the preferred engineers are obtained from contracts/ib, select the resources
5714     -- that are to be returned based on the value of the profile for search order and after
5715     -- filtering out excluded resources
5716     IF ( upper(l_pref_res_order) = 'CONTRACTS' )
5717     THEN
5718         IF (p_contracts_preferred_engineer = 'Y')
5719         THEN
5720             table_copy(l_contracts_tbl, l_assign_resources_tbl);
5721         END IF; -- p_contracts_preferred_engineer = 'Y'
5722 
5723 
5724         IF ( l_assign_resources_tbl.COUNT <= 0 )
5725         THEN
5726           IF (p_ib_preferred_engineer = 'Y')
5727           THEN
5728               table_copy(l_ib_tbl, l_assign_resources_tbl);
5729           END IF; -- p_ib_preferred_engineer = 'Y'
5730         END IF; -- l_assign_resources_tbl.COUNT <= 0
5731 
5732     ELSIF ( upper(l_pref_res_order) = 'IB' )
5733     THEN
5734 
5735         IF (p_ib_preferred_engineer = 'Y')
5736         THEN
5737             table_copy(l_ib_tbl, l_assign_resources_tbl);
5738         END IF; -- p_ib_preferred_engineer = 'Y'
5739 
5740         IF ( l_assign_resources_tbl.COUNT <= 0 )
5741         THEN
5742           IF (p_contracts_preferred_engineer = 'Y')
5743           THEN
5744               table_copy(l_contracts_tbl, l_assign_resources_tbl);
5745           END IF; -- p_contracts_preferred_engineer = 'Y'
5746         END IF; -- l_assign_resources_tbl.COUNT <= 0
5747 
5748     ELSE  -- l_pref_res_order = 'BOTH'
5749 
5750       /* Check if the Contracts Preferred Engineers Profile is SET If it is SET then get the available
5751          preferred engineers into the table of records */
5752 
5753         IF (p_contracts_preferred_engineer = 'Y')
5754         THEN
5755             table_copy(l_contracts_tbl, l_assign_resources_tbl);
5756         END IF; -- p_contracts_preferred_engineer = 'Y'
5757 
5758 
5759       /* Check if the Installed Base Preferred Engineers Profile is SET
5760          If it is SET then get the available preferred engineers
5761          into the table of records */
5762 
5763         IF (p_ib_preferred_engineer = 'Y')
5764         THEN
5765              table_copy(l_ib_tbl, l_assign_resources_tbl);
5766         END IF; -- p_ib_preferred_engineer = 'Y'
5767 
5768      END IF; -- l_pref_res_order = 'CONTRACTS' / 'IB' / 'BOTH'
5769 
5770 
5771    -- if the Contracts and IB Preferred Engineers are nor found then Territory Qualified Resources are fetched
5772 
5773     IF l_assign_resources_tbl.COUNT <= 0
5774     THEN
5775 	   IF (p_territory_flag = 'Y') THEN
5776 
5777           IF (l_p_resource_type  = 'RS_INDIVIDUAL') THEN
5778               l_p_resource_type := NULL;
5779           END IF;
5780 
5781           -- change the API Name temporarily so that in case of unexpected error
5782           -- it is properly caught
5783 
5784           l_api_name := l_api_name||'-JTF_TERR_SERVICE_PUB';
5785 
5786           JTF_TERR_SERVICE_PUB.Get_WinningTerrMembers
5787           (
5788              p_api_version_number  => l_api_version,
5789              p_init_msg_list       => p_init_msg_list,
5790              p_TerrSrvTask_Rec     => l_dr_rec,
5791              p_Resource_Type       => l_p_resource_type,
5792              p_Role                => p_role,
5793              x_return_status       => x_return_status,
5794              x_msg_count           => x_msg_count,
5795              x_msg_data            => x_msg_data,
5796              x_TerrResource_tbl    => l_winningterrmember_tbl
5797           );
5798 
5799           -- set back the API name to original name
5800           l_api_name := l_api_name_1;
5801 
5802           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
5803             -- Unexpected Execution Error from call to Territory Manager
5804             fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
5805             fnd_msg_pub.add;
5806             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
5807               RAISE fnd_api.g_exc_error;
5808             ELSE
5809               RAISE fnd_api.g_exc_unexpected_error;
5810             END IF;
5811           END IF;
5812 
5813           IF(l_winningterrmember_tbl.COUNT > 0)
5814           THEN
5815              l_current_record := l_winningterrmember_tbl.FIRST;
5816              WHILE l_current_record <= l_winningterrmember_tbl.LAST
5817              LOOP
5818                 IF(l_winningterrmember_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
5819                 THEN
5820                    l_winningterrmember_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
5821                 END IF;
5822                 l_current_record := l_winningterrmember_tbl.NEXT(l_current_record);
5823              END LOOP;
5824           END IF;
5825 
5826         ELSE  -- Territory Flag is NO
5827 
5828           fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
5829           fnd_msg_pub.add;
5830 
5831         END IF; -- End of p_territory_flag = 'Y'
5832 
5833       IF l_winningterrmember_tbl.COUNT > 0 THEN
5834 
5835         l_current_rec    := 0;
5836         l_current_record := l_winningterrmember_tbl.FIRST;
5837 
5838         WHILE(l_current_record <=  l_winningterrmember_tbl.LAST)
5839         LOOP
5840             l_assign_resources_tbl(l_current_rec).terr_rsc_id           :=
5841                                    l_winningterrmember_tbl(l_current_record).terr_rsc_id;
5842             l_assign_resources_tbl(l_current_rec).resource_id           :=
5843                                    l_winningterrmember_tbl(l_current_record).resource_id;
5844             l_assign_resources_tbl(l_current_rec).resource_type         :=
5845                                    l_winningterrmember_tbl(l_current_record).resource_type;
5846             l_assign_resources_tbl(l_current_rec).role                  :=
5847                                    l_winningterrmember_tbl(l_current_record).role;
5848             l_assign_resources_tbl(l_current_rec).start_date            :=
5849                                    l_winningterrmember_tbl(l_current_record).start_date;
5850             l_assign_resources_tbl(l_current_rec).end_date              :=
5851                                    l_winningterrmember_tbl(l_current_record).end_date;
5852             l_assign_resources_tbl(l_current_rec).shift_construct_id    := NULL;
5853             l_assign_resources_tbl(l_current_rec).terr_id               :=
5854                                    l_winningterrmember_tbl(l_current_record).terr_id;
5855             l_assign_resources_tbl(l_current_rec).terr_name             :=
5856                                    l_winningterrmember_tbl(l_current_record).terr_name;
5857             l_assign_resources_tbl(l_current_rec).primary_contact_flag  :=
5858                                    l_winningterrmember_tbl(l_current_record).primary_contact_flag;
5859             l_assign_resources_tbl(l_current_rec).primary_flag  :=
5860                                        l_winningterrmember_tbl(l_current_record).primary_contact_flag;
5861             l_assign_resources_tbl(l_current_rec).resource_source       := 'TERR';
5862 
5863             l_current_rec    := l_current_rec + 1;
5864             l_current_record := l_winningterrmember_tbl.NEXT(l_current_record);
5865        END LOOP;
5866 
5867           l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
5868           l_return_status_1 := x_return_status ;
5869 
5870            -- call the api to check resource availability
5871 
5872            get_available_resources
5873             (
5874               p_init_msg_list                 =>  'F',
5875               p_calendar_flag                 =>   p_calendar_flag,
5876               p_effort_duration               =>  l_effort_duration,
5877               p_effort_uom                    =>  l_effort_uom,
5878               p_planned_start_date            =>  l_planned_start_date,
5879               p_planned_end_date              =>  l_planned_end_date,
5880               p_breakdown                     =>   null,
5881               p_breakdown_uom                 =>   null,
5882               p_continuous_task               =>   jtf_assign_pub.g_continuous_work,
5883               x_return_status                 =>   x_return_status,
5884               x_msg_count                     =>   x_msg_count,
5885               x_msg_data                      =>   x_msg_data,
5886               x_assign_resources_tbl          =>   l_assign_resources_tbl,
5887 	      --Added for Bug # 5573916
5888 	      p_calendar_check                =>   p_calendar_check
5889 	      --Added for Bug # 5573916 Ends here
5890 	      );
5891 
5892           -- set back the API name to original name
5893 
5894           l_api_name := l_api_name_1;
5895 
5896             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
5897             -- Unexpected Execution Error from call to Get_contracts_resources
5898                fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
5899                fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
5900                fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_DR_RESOURCES');
5901                fnd_msg_pub.add;
5902                IF (x_return_status = fnd_api.g_ret_sts_error) THEN
5903                   RAISE fnd_api.g_exc_error;
5904                ELSE
5905                   RAISE fnd_api.g_exc_unexpected_error;
5906                END IF;
5907              END IF; -- end of x_return_status check
5908 
5909         IF ( p_calendar_flag = 'Y' AND
5910           l_assign_resources_tbl.count = 0 ) THEN
5911           fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
5912           fnd_msg_pub.add;
5913         END IF;
5914 
5915 
5916          -- remove excluded resources from territory qualifeid resource list
5917 
5918          IF(p_filter_excluded_resource = 'Y')
5919          THEN
5920               remove_excluded(x_res_tbl  => l_assign_resources_tbl,
5921                               x_exc_res  => l_excluded_resource_tbl);
5922          END IF;
5923 
5924           IF(l_assign_resources_tbl.count > 0)
5925           THEN
5926              JTF_AM_FILTER_RESOURCE_PVT.SERVICE_SECURITY_CHECK
5927               (   p_api_version                  =>1.0,
5928                   x_assign_resources_tbl         =>l_assign_resources_tbl,
5929                   p_sr_tbl                       =>l_dr_sec_rec,
5930                   x_return_status                =>x_return_status ,
5931                   x_msg_count                    =>x_msg_count,
5932                   x_msg_data                     =>x_msg_data);
5933 
5934               IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
5935              -- Unexpected Execution Error from call to Get_contracts_resources
5936                   fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
5937                   fnd_message.set_token('P_PROC_NAME','SERVICE_SECURITY_CHECK');
5938                   fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_DR_RESOURCES');
5939                   fnd_msg_pub.add;
5940                   IF (x_return_status = fnd_api.g_ret_sts_error) THEN
5941                     RAISE fnd_api.g_exc_error;
5942                   ELSE
5943                     RAISE fnd_api.g_exc_unexpected_error;
5944                   END IF;
5945               END IF;
5946           END IF; -- end of security check
5947       ELSE   -- No resources returned from the Territory API
5948 
5949         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
5950         fnd_msg_pub.add;
5951       END IF;
5952 
5953 
5954       -- Logic for getting resources belonging to group id passed in
5955       -- get the profile value to see if you have to filter by group membership
5956 
5957       l_group_filter :=  nvl(FND_PROFILE.VALUE ('JTF_AM_GROUP_MEMBER_FILTER'), 'YES');
5958 
5959       IF(l_group_filter = 'YES')
5960       THEN
5961          IF((p_dr_rec.squal_num17 is not null) )
5962          THEN
5963             get_group_resource(p_dr_rec.squal_num17 ,
5964                             l_assign_resources_tbl );
5965          END IF;
5966 
5967       END IF;
5968 
5969     END IF; -- End of l_assign_resources_tbl.COUNT <= 0
5970 
5971 
5972     -- Start of enhancement for showing Individual Resources ONLY
5973 
5974     IF ( UPPER(p_resource_type) = 'RS_INDIVIDUAL' ) THEN
5975 
5976       -- Reconstructing the table to further do the enhancements
5977 
5978       IF ( l_assign_resources_tbl.COUNT > 0 ) THEN
5979 
5980         l_current_record := l_assign_resources_tbl.FIRST;
5981         l_current_rec    := 1;
5982 
5983         WHILE l_current_record <= l_assign_resources_tbl.LAST
5984         LOOP
5985 
5986           l_tbl(l_current_rec).resource_id           :=
5987                                  l_assign_resources_tbl(l_current_record).resource_id;
5988           l_tbl(l_current_rec).resource_type         :=
5989                                  l_assign_resources_tbl(l_current_record).resource_type;
5990 
5991           l_tbl(l_current_rec).start_date            :=
5992                                  l_assign_resources_tbl(l_current_record).start_date;
5993           l_tbl(l_current_rec).end_date              :=
5994                                  l_assign_resources_tbl(l_current_record).end_date;
5995           l_tbl(l_current_rec).shift_construct_id    :=
5996                                  l_assign_resources_tbl(l_current_record).shift_construct_id;
5997 
5998           l_tbl(l_current_rec).terr_rsc_id           :=
5999                                  l_assign_resources_tbl(l_current_record).terr_rsc_id;
6000           l_tbl(l_current_rec).role                  :=
6001                                  l_assign_resources_tbl(l_current_record).role;
6002           l_tbl(l_current_rec).terr_id               :=
6003                                  l_assign_resources_tbl(l_current_record).terr_id;
6004           l_tbl(l_current_rec).terr_name             :=
6005                                  l_assign_resources_tbl(l_current_record).terr_name;
6006           l_tbl(l_current_rec).primary_contact_flag  :=
6007                                  l_assign_resources_tbl(l_current_record).primary_contact_flag;
6008           l_tbl(l_current_rec).preference_type       :=
6009                                  l_assign_resources_tbl(l_current_record).preference_type;
6010           l_tbl(l_current_rec).group_id              :=
6011                                  l_assign_resources_tbl(l_current_record).group_id;
6012 
6013           l_tbl(l_current_rec).primary_flag          :=
6014                                  l_assign_resources_tbl(l_current_record).primary_flag;
6015 
6016           l_tbl(l_current_rec).resource_source       :=
6017                                  l_assign_resources_tbl(l_current_record).resource_source;
6018 
6019           l_current_rec    := l_current_rec + 1;
6020           l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
6021 
6022         END LOOP;
6023 
6024       END IF; --End of l_assign_resources_tbl.COUNT > 0
6025 
6026       -- End of reconstruction
6027 
6028 
6029       IF ( l_tbl.COUNT > 0 ) THEN
6030 
6031         l_current_record := l_tbl.FIRST;
6032         l_current_rec    := 0;
6033 
6034         l_assign_resources_tbl.delete;
6035 
6036         WHILE l_current_record <= l_tbl.LAST
6037         LOOP
6038           IF ( UPPER(l_tbl(l_current_record).resource_type) NOT IN ( 'RS_GROUP', 'RS_TEAM')) THEN
6039 
6040             l_assign_resources_tbl(l_current_rec).resource_id           :=
6041                                    l_tbl(l_current_record).resource_id;
6042             l_assign_resources_tbl(l_current_rec).resource_type         :=
6043                                    l_tbl(l_current_record).resource_type;
6044 
6045             l_assign_resources_tbl(l_current_rec).start_date            :=
6046                                    l_tbl(l_current_record).start_date;
6047             l_assign_resources_tbl(l_current_rec).end_date              :=
6048                                    l_tbl(l_current_record).end_date;
6049             l_assign_resources_tbl(l_current_rec).shift_construct_id    :=
6050                                    l_tbl(l_current_record).shift_construct_id;
6051 
6052             l_assign_resources_tbl(l_current_rec).terr_rsc_id           :=
6053                                    l_tbl(l_current_record).terr_rsc_id;
6054             l_assign_resources_tbl(l_current_rec).role                  :=
6055                                    l_tbl(l_current_record).role;
6056             l_assign_resources_tbl(l_current_rec).terr_id               :=
6057                                    l_tbl(l_current_record).terr_id;
6058             l_assign_resources_tbl(l_current_rec).terr_name             :=
6059                                    l_tbl(l_current_record).terr_name;
6060             l_assign_resources_tbl(l_current_rec).primary_contact_flag  :=
6061                                    l_tbl(l_current_record).primary_contact_flag;
6062             l_assign_resources_tbl(l_current_rec).preference_type       :=
6063                                    l_tbl(l_current_record).preference_type;
6064             l_assign_resources_tbl(l_current_rec).group_id              :=
6065                                    l_tbl(l_current_record).group_id;
6066 
6067             l_assign_resources_tbl(l_current_rec).primary_flag          :=
6068                                    l_tbl(l_current_record).primary_flag;
6069 
6070             l_assign_resources_tbl(l_current_rec).resource_source       :=
6071                                    l_tbl(l_current_record).resource_source;
6072 
6073             l_current_rec := l_current_rec + 1;
6074 
6075           END IF;
6076           l_current_record := l_tbl.NEXT(l_current_record);
6077 
6078         END LOOP;
6079 
6080       END IF; --End of l_tbl.COUNT > 0
6081 
6082     END IF;   --End of p_resource_type = 'RS_INDIVIDUAL'
6083 
6084 
6085     -- To add SUPPORT SITE ID and NAME to the OUT Table
6086 
6087     l_dynamic_sql3 := ' SELECT a.city city '||
6088                       ' FROM   hz_locations a, hz_party_sites b,  hz_party_site_uses c '||
6089                       ' WHERE  c.site_use_type = :1  AND '||
6090                       ' b.party_site_id        = :2 AND '||
6091                       ' a.location_id          = b.location_id   AND '||
6092                       ' c.party_site_id        = b.party_site_id ';
6093 
6094     IF l_assign_resources_tbl.COUNT > 0 THEN
6095 
6096       l_current_record := l_assign_resources_tbl.FIRST;
6097 
6098       WHILE l_current_record <= l_assign_resources_tbl.LAST
6099       LOOP
6100 
6101         l_rsc_type := resource_type_change(l_assign_resources_tbl(l_current_record).resource_type);
6102 
6103         OPEN  cur_support_site_id (l_assign_resources_tbl(l_current_record).resource_id,
6104                                    l_rsc_type);
6105         FETCH cur_support_site_id INTO l_assign_resources_tbl(l_current_record).support_site_id;
6106 
6107         IF (l_assign_resources_tbl(l_current_record).support_site_id IS NOT NULL) THEN
6108 
6109           OPEN  cur_support_site_name FOR l_dynamic_sql3
6110                 USING l_support_site,
6111                       l_assign_resources_tbl(l_current_record).support_site_id;
6112 
6113           FETCH cur_support_site_name INTO l_assign_resources_tbl(l_current_record).support_site_name;
6114           IF (  cur_support_site_name % NOTFOUND ) THEN
6115             l_assign_resources_tbl(l_current_record).support_site_name := NULL;
6116           END IF;
6117           CLOSE cur_support_site_name;
6118         ELSE
6119           l_assign_resources_tbl(l_current_record).support_site_id   := NULL;
6120           l_assign_resources_tbl(l_current_record).support_site_name := NULL;
6121 
6122         END IF;
6123 
6124         CLOSE cur_support_site_id;
6125 
6126         l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
6127       END LOOP;
6128     END IF;
6129 
6130 
6131     -- To add Web Availability to the OUT Table
6132 
6133     IF (l_assign_resources_tbl.COUNT > 0) THEN
6134 
6135       l_current_record := l_assign_resources_tbl.FIRST;
6136 
6137       WHILE l_current_record <= l_assign_resources_tbl.LAST
6138       LOOP
6139 
6140         IF ( UPPER(p_web_availability_flag) = 'Y') THEN
6141           l_rsc_type := resource_type_change(l_assign_resources_tbl(l_current_record).resource_type);
6142 
6143           OPEN  cur_web_availability (l_assign_resources_tbl(l_current_record).resource_id,
6144                                       l_rsc_type);
6145           FETCH cur_web_availability INTO l_rsc_id;
6146 
6147           IF (cur_web_availability%FOUND) THEN
6148             l_assign_resources_tbl(l_current_record).web_availability_flag := 'Y';
6149           ELSE
6150             l_assign_resources_tbl(l_current_record).web_availability_flag := 'N';
6151           END IF;
6152 
6153           CLOSE cur_web_availability;
6154         ELSE
6155           l_assign_resources_tbl(l_current_record).web_availability_flag := NULL;
6156         END IF; --p_web_availability_flag = 'Y'
6157 
6158         l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
6159       END LOOP;
6160     END IF;
6161 
6162 
6163   -- skills bank filter call to api  JTF_AM_FILTER_RESOURCE_PVT
6164   -- initialize values of the skill table
6165        l_skill_tbl(1).document_type   := 'DR';
6166        l_skill_tbl(1).category_id     := p_category_id;
6167        l_skill_tbl(1).product_id      := p_inventory_item_id;
6168        l_skill_tbl(1).product_org_id  := p_inventory_org_id;
6169       l_skill_tbl(1).problem_code    := p_problem_code;
6170 
6171        -- change the API Name temporarily so that in case of unexpected error
6172        -- it is properly caught
6173        l_api_name := l_api_name||'-JTF_AM_FILTER_RESOURCE_PVT';
6174 
6175      JTF_AM_FILTER_RESOURCE_PVT.SEARCH_SKILL
6176                        ( p_api_version => 1.0,
6177                          x_assign_resources_tbl => l_assign_resources_tbl,
6178                          p_skill_param_tbl => l_skill_tbl,
6179                          x_return_status   => l_skill_ret_sts,
6180                          x_msg_count       => x_msg_count,
6181                          x_msg_data        => x_msg_data );
6182 
6183     -- set back the API name to original name
6184     l_api_name := l_api_name_1;
6185 
6186     if(l_skill_ret_sts <>  fnd_api.g_ret_sts_success)
6187     then
6188       IF (l_skill_ret_sts = fnd_api.g_ret_sts_error) THEN
6189         RAISE fnd_api.g_exc_error;
6190       ELSE
6191         RAISE fnd_api.g_exc_unexpected_error;
6192       END IF;
6193     end if;
6194 
6195 
6196       -- added this to filter by usage
6197 
6198       IF ((l_assign_resources_tbl.count > 0 ) AND
6199           (nvl(l_usage, fnd_api.g_miss_char)  <> 'ALL' ) AND
6200           (l_usage is not null)
6201          )
6202       THEN
6203           get_usage_resource(l_usage ,
6204                              l_assign_resources_tbl);
6205       END IF;
6206 
6207 
6208       IF l_assign_resources_tbl.COUNT > 0 THEN
6209 
6210         l_current_record := l_assign_resources_tbl.FIRST;
6211 
6212         IF ( UPPER(l_auto_select_flag) = 'Y' ) THEN
6213 
6214             l_no_of_resources := least(nvl(l_assign_resources_tbl.count, 0),l_no_of_resources) ;
6215 
6216           WHILE (l_count < l_no_of_resources)
6217           LOOP
6218 
6219           open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
6220                               l_assign_resources_tbl(l_current_record).resource_type);
6221           fetch check_date_cur into l_value;
6222           if (check_date_cur%found)
6223           then
6224 
6225             l_count := l_count + 1;
6226 
6227             x_assign_resources_tbl(l_count).terr_rsc_id           :=
6228                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
6229             x_assign_resources_tbl(l_count).resource_id           :=
6230                                    l_assign_resources_tbl(l_current_record).resource_id;
6231             x_assign_resources_tbl(l_count).resource_type         :=
6232                                    l_assign_resources_tbl(l_current_record).resource_type;
6233             x_assign_resources_tbl(l_count).role                  :=
6234                                    l_assign_resources_tbl(l_current_record).role;
6235             x_assign_resources_tbl(l_count).start_date            :=
6236                                    l_assign_resources_tbl(l_current_record).start_date;
6237             x_assign_resources_tbl(l_count).end_date              :=
6238                                    l_assign_resources_tbl(l_current_record).end_date;
6239             x_assign_resources_tbl(l_count).shift_construct_id    :=
6240                                    l_assign_resources_tbl(l_current_record).shift_construct_id;
6241             x_assign_resources_tbl(l_count).terr_id               :=
6242                                    l_assign_resources_tbl(l_current_record).terr_id;
6243             x_assign_resources_tbl(l_count).terr_name             :=
6244                                    l_assign_resources_tbl(l_current_record).terr_name;
6245             x_assign_resources_tbl(l_count).preference_type       :=
6246                                    l_assign_resources_tbl(l_current_record).preference_type;
6247             x_assign_resources_tbl(l_count).primary_contact_flag  :=
6248                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
6249             x_assign_resources_tbl(l_count).group_id              :=
6250                                    l_assign_resources_tbl(l_current_record).group_id;
6251 
6252             x_assign_resources_tbl(l_count).support_site_id       :=
6253                                    l_assign_resources_tbl(l_current_record).support_site_id;
6254             x_assign_resources_tbl(l_count).support_site_name     :=
6255                                    l_assign_resources_tbl(l_current_record).support_site_name;
6256             x_assign_resources_tbl(l_count).web_availability_flag :=
6257                                    l_assign_resources_tbl(l_current_record).web_availability_flag;
6258 
6259             x_assign_resources_tbl(l_count).skill_level           :=
6260                                    l_assign_resources_tbl(l_current_record).skill_level;
6261             x_assign_resources_tbl(l_count).skill_name            :=
6262                                    l_assign_resources_tbl(l_current_record).skill_name;
6263             x_assign_resources_tbl(l_count).primary_flag            :=
6264                                    l_assign_resources_tbl(l_current_record).primary_flag;
6265             x_assign_resources_tbl(l_count).resource_source       :=
6266                                    l_assign_resources_tbl(l_current_record).resource_source;
6267             end if;
6268             close check_date_cur;
6269             l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
6270           END LOOP;
6271 
6272         ELSE  -- Auto Select Flag is NO
6273 
6274           WHILE l_current_record <= l_assign_resources_tbl.LAST
6275           LOOP
6276 
6277              open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
6278                               l_assign_resources_tbl(l_current_record).resource_type);
6279              fetch check_date_cur into l_value;
6280              if (check_date_cur%found)
6281              then
6282                l_count := l_count + 1;
6283 
6284                x_assign_resources_tbl(l_count).terr_rsc_id           :=
6285                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
6286                x_assign_resources_tbl(l_count).resource_id           :=
6287                                    l_assign_resources_tbl(l_current_record).resource_id;
6288                x_assign_resources_tbl(l_count).resource_type         :=
6289                                    l_assign_resources_tbl(l_current_record).resource_type;
6290                x_assign_resources_tbl(l_count).role                  :=
6291                                    l_assign_resources_tbl(l_current_record).role;
6292                x_assign_resources_tbl(l_count).start_date            :=
6293                                    l_assign_resources_tbl(l_current_record).start_date;
6294                x_assign_resources_tbl(l_count).end_date              :=
6295                                    l_assign_resources_tbl(l_current_record).end_date;
6296                x_assign_resources_tbl(l_count).shift_construct_id    :=
6297                                    l_assign_resources_tbl(l_current_record).shift_construct_id;
6298                x_assign_resources_tbl(l_count).terr_id               :=
6299                                    l_assign_resources_tbl(l_current_record).terr_id;
6300                x_assign_resources_tbl(l_count).terr_name             :=
6301                                    l_assign_resources_tbl(l_current_record).terr_name;
6302                x_assign_resources_tbl(l_count).preference_type       :=
6303                                    l_assign_resources_tbl(l_current_record).preference_type;
6304                x_assign_resources_tbl(l_count).primary_contact_flag  :=
6305                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
6306                x_assign_resources_tbl(l_count).group_id              :=
6307                                    l_assign_resources_tbl(l_current_record).group_id;
6308 
6309                x_assign_resources_tbl(l_count).support_site_id       :=
6310                                    l_assign_resources_tbl(l_current_record).support_site_id;
6311                x_assign_resources_tbl(l_count).support_site_name     :=
6312                                    l_assign_resources_tbl(l_current_record).support_site_name;
6313                x_assign_resources_tbl(l_count).web_availability_flag :=
6314                                    l_assign_resources_tbl(l_current_record).web_availability_flag;
6315 
6316                x_assign_resources_tbl(l_count).skill_level           :=
6317                                    l_assign_resources_tbl(l_current_record).skill_level;
6318                x_assign_resources_tbl(l_count).skill_name            :=
6319                                    l_assign_resources_tbl(l_current_record).skill_name;
6320                x_assign_resources_tbl(l_count).primary_flag            :=
6321                                    l_assign_resources_tbl(l_current_record).primary_flag;
6322                x_assign_resources_tbl(l_count).resource_source       :=
6323                                    l_assign_resources_tbl(l_current_record).resource_source;
6324                end if;
6325                close check_date_cur;
6326                l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
6327           END LOOP;
6328         END IF;   -- Auto Select Flag
6329 
6330       ELSE
6331         -- No resources returned from the Assignment Manager API for SERVICE REQUESTS
6332         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
6333         fnd_msg_pub.add;
6334       END IF;
6335 
6336 
6337       -- Raise Workflow Event
6338       -- Workflow Test
6339       Begin
6340          jtf_assign_pub.g_assign_resources_tbl.delete;
6341          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
6342          jtf_am_wf_events_pub.assign_dr_resource
6343                     (P_API_VERSION           =>  1.0,
6344                      P_INIT_MSG_LIST         =>  'F',
6345                      P_COMMIT                =>  'F',
6346                      P_CONTRACT_ID           =>  p_contract_id   ,
6347                      P_CUSTOMER_PRODUCT_ID   =>  p_customer_product_id   ,
6348                      P_CATEGORY_ID           =>  p_category_id   ,
6349                      P_INVENTORY_ITEM_ID     =>  p_inventory_item_id   ,
6350                      P_INVENTORY_ORG_ID      =>  p_inventory_org_id   ,
6351                      P_PROBLEM_CODE          =>  p_problem_code ,
6352                      P_DR_REC                =>  p_dr_rec,
6353                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
6354                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
6355                      X_RETURN_STATUS         =>  l_wf_return_status,
6356                      X_MSG_COUNT             =>  l_wf_msg_count,
6357                      X_MSG_DATA              =>  l_wf_msg_data
6358                      );
6359 
6360 
6361          IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
6362             -- Unexpected Execution Error from call to assign_dr_resource
6363             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
6364             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
6365             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_RESOURCES');
6366             fnd_msg_pub.add;
6367 
6368         ELSE
6369             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
6370         END IF;
6371 
6372 
6373          Exception
6374             When Others Then
6375                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
6376                fnd_message.set_token('P_SQLCODE',SQLCODE);
6377                fnd_message.set_token('P_SQLERRM',SQLERRM);
6378                fnd_message.set_token('P_API_NAME',l_api_name);
6379                FND_MSG_PUB.add;
6380       End;
6381 
6382 
6383 
6384     -- To Plugin the Workflow enabling the user
6385     -- to further filter the resources
6386 
6387     SELECT jtf_calendars_s.NEXTVAL INTO l_workflow_key
6388     FROM   dual;
6389 
6390     IF (JTF_USR_HKS.ok_to_execute
6391           (
6392             'JTF_ASSIGN_PUB',
6393             'GET_ASSIGN_DR_RESOURCES',
6394             'W',
6395             'W'
6396           )
6397        ) THEN
6398 
6399       IF (JTF_ASSIGN_CUHK.ok_to_launch_workflow
6400             (
6401               p_api_version     => l_api_version,
6402               p_init_msg_list   => p_init_msg_list,
6403               x_return_status   => x_return_status,
6404               x_msg_count       => x_msg_count,
6405               x_msg_data        => x_msg_data
6406             )
6407          ) THEN
6408 
6409 
6410         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
6411           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
6412           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
6413           fnd_msg_pub.add;
6414           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
6415             RAISE fnd_api.g_exc_error;
6416           ELSE
6417             RAISE fnd_api.g_exc_unexpected_error;
6418           END IF;
6419         END IF;
6420 
6421 
6422         l_bind_data_id := JTF_USR_HKS.get_bind_data_id;
6423 
6424         JTF_USR_HKS.WrkFlowLaunch
6425           (
6426             'JTF_ASSIGN_WF',
6427             l_workflow_profile,
6428             'ASSIGN WF - '|| TO_CHAR(l_workflow_key),
6429             l_bind_data_id,
6430             l_return_code
6431           );
6432 
6433         JTF_USR_HKS.purge_bind_data
6434           (
6435             l_bind_data_id,
6436             'W'
6437           );
6438 
6439 
6440         IF (l_return_code = fnd_api.g_ret_sts_error) THEN
6441           -- Execution Error from call to Assignment Manager Workflow Hook
6442           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
6443           fnd_msg_pub.add;
6444           RAISE fnd_api.g_exc_error;
6445         ELSIF (l_return_code = fnd_api.g_ret_sts_unexp_error) THEN
6446           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
6447           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
6448           fnd_msg_pub.add;
6449           RAISE fnd_api.g_exc_unexpected_error;
6450         END IF;
6451 
6452       END IF;  -- End of JTF_ASSIGN_CUHK
6453 
6454     END IF;    -- End of JTF_USR_HKS
6455 
6456 
6457     /* Standard call to get message count and
6458        the message information */
6459 
6460     FND_MSG_PUB.Count_And_Get
6461     (
6462       p_count => x_msg_count,
6463       p_data  => x_msg_data
6464     );
6465 
6466 
6467     x_return_status := fnd_api.g_ret_sts_success;
6468 
6469   EXCEPTION
6470     WHEN FND_API.G_EXC_ERROR THEN
6471       x_return_status := FND_API.G_RET_STS_ERROR ;
6472       FND_MSG_PUB.Count_And_Get
6473       (
6474         p_count => x_msg_count,
6475         p_data  => x_msg_data
6476       );
6477 
6478     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6479       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6480       FND_MSG_PUB.Count_And_Get
6481       (
6482         p_count => x_msg_count,
6483         p_data  => x_msg_data
6484       );
6485 
6486     WHEN OTHERS THEN
6487       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
6488       fnd_message.set_token('P_SQLCODE',SQLCODE);
6489       fnd_message.set_token('P_SQLERRM',SQLERRM);
6490       fnd_message.set_token('P_API_NAME',l_api_name);
6491       FND_MSG_PUB.add;
6492 
6493       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6494       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
6495         FND_MSG_PUB.Add_Exc_Msg
6496         (
6497           G_PKG_NAME,
6498           l_api_name
6499         );
6500       END IF;
6501 
6502       FND_MSG_PUB.Count_And_Get
6503       (
6504         p_count => x_msg_count,
6505         p_data  => x_msg_data
6506        );
6507 
6508   END GET_ASSIGN_DR_RESOURCES;
6509 
6510  /*************** End of addition by SBARAT on 01/11/2004 for Enh-3919046*********/
6511 
6512 
6513 -- *******************************************************************************
6514 
6515 -- Start of comments
6516 
6517 --      API name        : GET_ASSIGN_OPPR_RESOURCES
6518 --      Type            : Public
6519 --      Function        : Determine the resources based on the customer
6520 --                        preferences or territory preferences and the
6521 --                        availability. This is when the calling doc is
6522 --                        OPPORTUNITIES.
6523 --      Pre-reqs        : None
6524 
6525 --      Parameters      :
6526 
6527 --      IN              : p_api_version         IN      NUMBER  Required
6528 --                        p_init_msg_list       IN      VARCHAR2 Optional
6529 --                                              DEFAULT = FND_API.G_FALSE
6530 --                        p_commit              IN      VARCHAR2 optional
6531 --                                              DEFAULT = FND_API.G_FALSE
6532 
6533 --     Assignment Manager Specific Parameters
6534 
6535 --     This determines the Resource Type required by the
6536 --     calling document
6537 --     p_resource_type                          VARCHAR2(30)
6538 --     p_role                                   VARCHAR2(30)
6539 
6540 --     This determines the number of resources required
6541 --     by the calling document
6542 --     p_no_of_resources                        NUMBER,
6543 
6544 --     This is for sending out the qualified resource directly
6545 --     to the calling form. Defaulted to the PROFILE value
6546 --     p_auto_select_flag                       VARCHAR2(1)
6547 --                                              : value of  Y or N
6548 
6549 --     The Effort required is determined by these two parameters
6550 --     p_effort_duration                        NUMBER
6551 --     p_effort_uom                             VARCHAR2(10)
6552 
6553 --     The Dates in which the resource is required, is determined
6554 --     by these two parameters
6555 --     p_start_date                             DATE
6556 --     p_end_date                               DATE
6557 
6558 --     The Territory Manager is accessed based on the value set
6559 --     p_territory_flag                         VARCHAR2(1)
6560 --                                              : value of  Y or N
6561 
6562 --     The Resource Availability is checked based on the value set
6563 --     p_calendar_flag                          VARCHAR2(1)
6564 --                                              : value of  Y or N
6565 
6566 
6567 --     This parameter contains the Qualifier values for the
6568 --     Calling Document.
6569 --     p_opportunity_rec                         JTF_ASSIGN_PUB.
6570 --                                               JTF_Oppor_rec_type
6571 --                                               REQUIRED
6572 
6573 
6574 --     OUT              : x_return_status        OUT     VARCHAR2(1)
6575 --                        x_msg_count            OUT     NUMBER
6576 --                        x_msg_data             OUT     VARCHAR2(2000)
6577 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
6578 --                                                       AssignResources_tbl_type
6579 
6580 
6581 --     Version          : Current version        1.0
6582 --                        Initial version        1.0
6583 --
6584 --     Notes            :
6585 --
6586 
6587 -- End of comments
6588 
6589 -- *********************************************************************************
6590 
6591 
6592 
6593 --      Procedure definition with the parameters when the
6594 --      Source Document is OPPORTUNITIES
6595 
6596   PROCEDURE GET_ASSIGN_OPPR_RESOURCES
6597     (
6598         p_api_version                         IN  NUMBER,
6599         p_init_msg_list                       IN  VARCHAR2 ,
6600         p_resource_type                       IN  VARCHAR2 ,
6601         p_role                                IN  VARCHAR2 ,
6602         p_no_of_resources                     IN  NUMBER   ,
6603         p_auto_select_flag                    IN  VARCHAR2 ,
6604         p_effort_duration                     IN  NUMBER   ,
6605         p_effort_uom                          IN  VARCHAR2 ,
6606         p_start_date                          IN  DATE     ,
6607         p_end_date                            IN  DATE     ,
6608         p_territory_flag                      IN  VARCHAR2,
6609         p_calendar_flag                       IN  VARCHAR2,
6610         p_opportunity_rec                     IN  JTF_ASSIGN_PUB.JTF_Oppor_rec_type,
6611         p_business_process_id                 IN  NUMBER,
6612         p_business_process_date               IN  DATE,
6613         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
6614         x_return_status                       OUT NOCOPY VARCHAR2,
6615         x_msg_count                           OUT NOCOPY NUMBER,
6616         x_msg_data                            OUT NOCOPY VARCHAR2
6617     )
6618   IS
6619 
6620     l_return_status_1                     VARCHAR2(10);
6621     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_OPPR_RESOURCES';
6622     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_OPPR_RESOURCES';
6623     l_api_version                         NUMBER        := 1.0;
6624     l_planned_start_date                  DATE;
6625     l_planned_end_date                    DATE;
6626     l_effort_duration                     NUMBER;
6627     l_effort_uom                          VARCHAR2(30);
6628     l_travel_time                         NUMBER        := 0;
6629     l_travel_uom                          VARCHAR2(30)  := 'HR';
6630 
6631     l_current_record                      INTEGER;
6632     l_total_records                       INTEGER;
6633 
6634     l_auto_select_profile                 VARCHAR2(03);
6635     l_auto_select_flag                    VARCHAR2(03);
6636     l_workflow_profile                    VARCHAR2(60);
6637 
6638     l_return_code                         VARCHAR2(60);
6639     l_bind_data_id                        NUMBER;
6640     l_workflow_key                        NUMBER;
6641 
6642     l_assign_resources_tbl                JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
6643     l_opportunity_rec                     JTF_TERRITORY_PUB.JTF_Oppor_rec_type;
6644 
6645     l_x_planned_start_date                DATE;
6646     l_x_planned_end_date                  DATE;
6647     l_x_shift_construct_id                NUMBER;
6648     l_x_availability_type                 VARCHAR2(60);
6649 
6650 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
6651 
6652     l_wf_return_status                    VARCHAR2(60);
6653     l_wf_msg_count                        NUMBER;
6654     l_wf_msg_data                         VARCHAR2(2000);
6655 
6656 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
6657 
6658     /*
6659     l_dynamic_sql                         VARCHAR2(2000);
6660     l_column_list                         VARCHAR2(2000);
6661     TYPE OPPR_CUR_TYP IS REF CURSOR;
6662     cur_oppr  OPPR_CUR_TYP;
6663     */
6664 
6665     --Bug# 4455803 MOAC.
6666     CURSOR check_date_cur(l_resource_id in number,
6667                           l_resource_type in varchar2)
6668         IS
6669      SELECT 'Y'
6670        --FROM  jtf_rs_all_resources_vl
6671        FROM  jtf_task_resources_vl
6672       where   resource_id = l_resource_id
6673         and   resource_type = l_resource_type
6674         and   nvl(trunc(end_date_active), trunc(sysdate)) >= trunc(sysdate);
6675 
6676     l_value varchar2(100);
6677 
6678     l_count number := 0;
6679     l_temp_table   JTF_ASSIGN_PUB.AssignResources_tbl_type;
6680 
6681   BEGIN
6682 
6683     SAVEPOINT get_assign_oppr_resources;
6684 
6685     -- Started Assignment Manager API for OPPORTUNITIES
6686 
6687 
6688     /* Standard call to check for call compatibility */
6689 
6690     IF NOT fnd_api.compatible_api_call (l_api_version,
6691                                         p_api_version,
6692                                         l_api_name,
6693                                         g_pkg_name) THEN
6694       RAISE fnd_api.g_exc_unexpected_error;
6695     END IF;
6696 
6697 
6698     /* Initialize message list if p_init_msg_list is set to TRUE */
6699 
6700     IF fnd_api.to_boolean (p_init_msg_list) THEN
6701       fnd_msg_pub.initialize;
6702     END IF;
6703 
6704     x_return_status := fnd_api.g_ret_sts_success;
6705 
6706 
6707 
6708     /* Getting the Auto Select Profile value defined for the Assignment Manager */
6709 
6710     --l_auto_select_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_AUTO_SELECT' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
6711     l_auto_select_profile := FND_PROFILE.VALUE ( 'ACTIVATE_AUTO_SELECT' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
6712 
6713 
6714 
6715     /* Assigning the DEFAULT value to the Auto Select Parameter */
6716 
6717 
6718     IF (p_auto_select_flag IS NULL) THEN
6719       l_auto_select_flag  := l_auto_select_profile;
6720                              -- PROFILE VALUE is the default value
6721     ELSE
6722       l_auto_select_flag  := p_auto_select_flag;
6723     END IF;
6724 
6725 
6726 
6727     /* Query the Opportunities View to get the data into
6728        the Record Type to pass it to the Territory API */
6729 
6730     /*
6731     IF (p_column_list IS NULL) THEN
6732       l_column_list := '*';
6733     ELSE
6734       l_column_list := p_column_list;
6735     END IF;
6736 
6737 
6738     l_dynamic_sql :=  'SELECT '||
6739                        l_column_list||
6740                        ' FROM jtf_terr_opportunities_v'||
6741                        ' WHERE lead_id = :1';
6742 
6743     OPEN cur_oppr FOR l_dynamic_sql USING p_opportunity_id;
6744     FETCH cur_oppr INTO l_assign_resources_rec;
6745     IF (cur_oppr%NOTFOUND) THEN
6746       fnd_message.set_name('JTF', 'JTF_AM_INVALID_OPPR_ID');
6747       fnd_msg_pub.add;
6748       RAISE fnd_api.g_exc_error;
6749     END IF;
6750     CLOSE cur_oppr;
6751     */
6752 
6753 
6754 
6755     /* Defaulting the variable values to IN parameters,
6756        if the IN paramaters have values given */
6757 
6758     IF (p_start_date IS NOT NULL) THEN
6759       l_planned_start_date := p_start_date;
6760     ELSE
6761       l_planned_start_date := SYSDATE;
6762     END IF;
6763 
6764     IF (p_end_date IS NOT NULL) THEN
6765       l_planned_end_date := p_end_date;
6766     ELSE
6767       l_planned_end_date := SYSDATE;
6768     END IF;
6769 
6770     IF (p_effort_duration IS NOT NULL) THEN
6771       l_effort_duration := p_effort_duration;
6772     END IF;
6773 
6774     IF (p_effort_uom IS NOT NULL) THEN
6775       l_effort_uom := p_effort_uom;
6776     END IF;
6777 
6778 
6779 
6780     /* Assigning values to the Opportunity Record Type */
6781 
6782 
6783     l_opportunity_rec.LEAD_ID                        := p_opportunity_rec.LEAD_ID;
6784     l_opportunity_rec.LEAD_LINE_ID                   := p_opportunity_rec.LEAD_LINE_ID;
6785     l_opportunity_rec.CITY                           := p_opportunity_rec.CITY;
6786     l_opportunity_rec.POSTAL_CODE                    := p_opportunity_rec.POSTAL_CODE;
6787     l_opportunity_rec.STATE                          := p_opportunity_rec.STATE;
6788     l_opportunity_rec.PROVINCE                       := p_opportunity_rec.PROVINCE;
6789     l_opportunity_rec.COUNTY                         := p_opportunity_rec.COUNTY;
6790     l_opportunity_rec.COUNTRY                        := p_opportunity_rec.COUNTRY;
6791     l_opportunity_rec.INTEREST_TYPE_ID               := p_opportunity_rec.INTEREST_TYPE_ID;
6792     l_opportunity_rec.PRIMARY_INTEREST_ID            := p_opportunity_rec.PRIMARY_INTEREST_ID;
6793     l_opportunity_rec.SECONDARY_INTEREST_ID          := p_opportunity_rec.SECONDARY_INTEREST_ID;
6794     l_opportunity_rec.CONTACT_INTEREST_TYPE_ID       := p_opportunity_rec.CONTACT_INTEREST_TYPE_ID;
6795     l_opportunity_rec.CONTACT_PRIMARY_INTEREST_ID    := p_opportunity_rec.CONTACT_PRIMARY_INTEREST_ID;
6796     l_opportunity_rec.CONTACT_SECONDARY_INTEREST_ID  := p_opportunity_rec.CONTACT_SECONDARY_INTEREST_ID;
6797     l_opportunity_rec.PARTY_SITE_ID                  := p_opportunity_rec.PARTY_SITE_ID;
6798     l_opportunity_rec.AREA_CODE                      := p_opportunity_rec.AREA_CODE;
6799     l_opportunity_rec.PARTY_ID                       := p_opportunity_rec.PARTY_ID;
6800     l_opportunity_rec.COMP_NAME_RANGE                := p_opportunity_rec.COMP_NAME_RANGE;
6801     l_opportunity_rec.PARTNER_ID                     := p_opportunity_rec.PARTNER_ID;
6802     l_opportunity_rec.NUM_OF_EMPLOYEES               := p_opportunity_rec.NUM_OF_EMPLOYEES;
6803     l_opportunity_rec.CATEGORY_CODE                  := p_opportunity_rec.CATEGORY_CODE;
6804     l_opportunity_rec.PARTY_RELATIONSHIP_ID          := p_opportunity_rec.PARTY_RELATIONSHIP_ID;
6805     l_opportunity_rec.SIC_CODE                       := p_opportunity_rec.SIC_CODE;
6806     l_opportunity_rec.TARGET_SEGMENT_CURRENT         := p_opportunity_rec.TARGET_SEGMENT_CURRENT;
6807     l_opportunity_rec.TOTAL_AMOUNT                   := p_opportunity_rec.TOTAL_AMOUNT;
6808     l_opportunity_rec.CURRENCY_CODE                  := p_opportunity_rec.CURRENCY_CODE;
6809     l_opportunity_rec.PRICING_DATE                   := p_opportunity_rec.PRICING_DATE;
6810     l_opportunity_rec.CHANNEL_CODE                   := p_opportunity_rec.CHANNEL_CODE;
6811     l_opportunity_rec.INVENTORY_ITEM_ID              := p_opportunity_rec.INVENTORY_ITEM_ID;
6812     l_opportunity_rec.OPP_INTEREST_TYPE_ID           := p_opportunity_rec.OPP_INTEREST_TYPE_ID;
6813     l_opportunity_rec.OPP_PRIMARY_INTEREST_ID        := p_opportunity_rec.OPP_PRIMARY_INTEREST_ID;
6814     l_opportunity_rec.OPP_SECONDARY_INTEREST_ID      := p_opportunity_rec.OPP_SECONDARY_INTEREST_ID;
6815     l_opportunity_rec.OPCLSS_INTEREST_TYPE_ID        := p_opportunity_rec.OPCLSS_INTEREST_TYPE_ID;
6816     l_opportunity_rec.OPCLSS_PRIMARY_INTEREST_ID     := p_opportunity_rec.OPCLSS_PRIMARY_INTEREST_ID;
6817     l_opportunity_rec.OPCLSS_SECONDARY_INTEREST_ID   := p_opportunity_rec.OPCLSS_SECONDARY_INTEREST_ID;
6818     l_opportunity_rec.ATTRIBUTE1                     := p_opportunity_rec.ATTRIBUTE1;
6819     l_opportunity_rec.ATTRIBUTE2                     := p_opportunity_rec.ATTRIBUTE2;
6820     l_opportunity_rec.ATTRIBUTE3                     := p_opportunity_rec.ATTRIBUTE3;
6821     l_opportunity_rec.ATTRIBUTE4                     := p_opportunity_rec.ATTRIBUTE4;
6822     l_opportunity_rec.ATTRIBUTE5                     := p_opportunity_rec.ATTRIBUTE5;
6823     l_opportunity_rec.ATTRIBUTE6                     := p_opportunity_rec.ATTRIBUTE6;
6824     l_opportunity_rec.ATTRIBUTE7                     := p_opportunity_rec.ATTRIBUTE7;
6825     l_opportunity_rec.ATTRIBUTE8                     := p_opportunity_rec.ATTRIBUTE8;
6826     l_opportunity_rec.ATTRIBUTE9                     := p_opportunity_rec.ATTRIBUTE9;
6827     l_opportunity_rec.ATTRIBUTE10                    := p_opportunity_rec.ATTRIBUTE10;
6828     l_opportunity_rec.ATTRIBUTE11                    := p_opportunity_rec.ATTRIBUTE11;
6829     l_opportunity_rec.ATTRIBUTE12                    := p_opportunity_rec.ATTRIBUTE12;
6830     l_opportunity_rec.ATTRIBUTE13                    := p_opportunity_rec.ATTRIBUTE13;
6831     l_opportunity_rec.ATTRIBUTE14                    := p_opportunity_rec.ATTRIBUTE14;
6832     l_opportunity_rec.ATTRIBUTE15                    := p_opportunity_rec.ATTRIBUTE15;
6833     l_opportunity_rec.ORG_ID                         := p_opportunity_rec.ORG_ID;
6834 
6835 
6836 
6837 
6838 
6839     /* Actual Flow of Assignment Manager */
6840 
6841 
6842     IF (p_territory_flag = 'Y') THEN
6843 
6844       -- change the API Name temporarily so that in case of unexpected error
6845       -- it is properly caught
6846       l_api_name := l_api_name||'-JTF_TERR_SALES_PUB';
6847 
6848       JTF_TERR_SALES_PUB.Get_WinningTerrMembers
6849         (
6850           p_api_version_number  => l_api_version,
6851           p_init_msg_list       => p_init_msg_list,
6852           p_TerrOppor_Rec       => l_opportunity_rec,
6853           p_Resource_Type       => p_resource_type,
6854           p_Role                => p_role,
6855           x_return_status       => x_return_status,
6856           x_msg_count           => x_msg_count,
6857           x_msg_data            => x_msg_data,
6858           x_TerrResource_tbl    => l_assign_resources_tbl
6859         );
6860 
6861         -- set back the API name to original name
6862         l_api_name := l_api_name_1;
6863 
6864 
6865       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
6866         -- Unexpected Execution Error from call to Territory Manager
6867         fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
6868         fnd_msg_pub.add;
6869         IF (x_return_status = fnd_api.g_ret_sts_error) THEN
6870           RAISE fnd_api.g_exc_error;
6871         ELSE
6872           RAISE fnd_api.g_exc_unexpected_error;
6873         END IF;
6874       END IF;
6875 
6876 
6877        -- added 2 april 2003 by sudarsana to conver RS_SUPPLIER TO RS_SUPPLIER_CONTACT
6878 
6879           IF(l_assign_resources_tbl.COUNT > 0)
6880           THEN
6881              l_current_record := l_assign_resources_tbl.FIRST;
6882              WHILE l_current_record <= l_assign_resources_tbl.LAST
6883              LOOP
6884                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
6885                 THEN
6886                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
6887                 END IF;
6888                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
6889              END LOOP;
6890           END IF;
6891 
6892         IF l_assign_resources_tbl.COUNT > 0 THEN
6893 
6894           l_current_record := l_assign_resources_tbl.FIRST;
6895 
6896         -- removed the calendar check here. calling the procedure for calendar check in the next step
6897         -- calendar check removed 29th September 2003
6898 
6899           WHILE l_current_record <= l_assign_resources_tbl.LAST
6900           LOOP
6901             -- Check the calendar for resource availability
6902             -- Call Calendar API
6903             -- IF the resource is available then accept the values and
6904             -- check for the WORKFLOW profile option
6905 
6906             -- The following IF statement is to implement Auto Select Feature
6907               open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
6908                                   l_assign_resources_tbl(l_current_record).resource_type);
6909               fetch check_date_cur into l_value;
6910               if (check_date_cur%found)
6911               then
6912 
6913                   l_count := l_count + 1;
6914                   x_assign_resources_tbl(l_count).terr_rsc_id           :=
6915                                      l_assign_resources_tbl(l_current_record).terr_rsc_id;
6916                   x_assign_resources_tbl(l_count).resource_id           :=
6917                                      l_assign_resources_tbl(l_current_record).resource_id;
6918                   x_assign_resources_tbl(l_count).resource_type         :=
6919                                      l_assign_resources_tbl(l_current_record).resource_type;
6920                   x_assign_resources_tbl(l_count).role                  :=
6921                                      l_assign_resources_tbl(l_current_record).role;
6922                   x_assign_resources_tbl(l_count).start_date            :=
6923                                      l_assign_resources_tbl(l_current_record).start_date;
6924                   x_assign_resources_tbl(l_count).end_date              :=
6925                                      l_assign_resources_tbl(l_current_record).end_date;
6926                   x_assign_resources_tbl(l_count).shift_construct_id    := NULL;
6927                   x_assign_resources_tbl(l_count).terr_id               :=
6928                                      l_assign_resources_tbl(l_current_record).terr_id;
6929                   x_assign_resources_tbl(l_count).terr_name             :=
6930                                      l_assign_resources_tbl(l_current_record).terr_name;
6931                   x_assign_resources_tbl(l_count).primary_contact_flag  :=
6932                                      l_assign_resources_tbl(l_current_record).primary_contact_flag;
6933                   x_assign_resources_tbl(l_count).full_access_flag      :=
6934                                      l_assign_resources_tbl(l_current_record).full_access_flag;
6935                   x_assign_resources_tbl(l_count).group_id              :=
6936                                      l_assign_resources_tbl(l_current_record).group_id;
6937                   x_assign_resources_tbl(l_count).primary_flag              :=
6938                                      l_assign_resources_tbl(l_current_record).primary_contact_flag;
6939                   x_assign_resources_tbl(l_count).resource_source       := 'TERR';
6940 
6941                 END IF; -- end of check_date_cur
6942                 close check_date_cur;
6943                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
6944         END LOOP;
6945 
6946       -- added calendar call out
6947       -- this has been done as now the calendar check is done in a seperate procedure GET_AVAILABLE_RESOURCE
6948       -- changed on 29th September 2003
6949         IF (p_calendar_flag = 'Y') THEN
6950              -- change the API Name temporarily so that in case of unexpected error
6951              -- it is properly caught
6952               l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
6953               l_return_status_1 := x_return_status ;
6954               -- call the api to check resource availability
6955               get_available_resources
6956               (
6957                 p_init_msg_list                 =>  'F',
6958                 p_calendar_flag                 =>  p_calendar_flag,
6959                 p_effort_duration               =>  l_effort_duration,
6960                 p_effort_uom                    =>  l_effort_uom,
6961                 p_planned_start_date            =>  l_planned_start_date,
6962                 p_planned_end_date              =>  l_planned_end_date,
6963                 p_continuous_task               =>  jtf_assign_pub.g_continuous_work,
6964                 x_return_status                 =>  x_return_status,
6965                 x_msg_count                     =>  x_msg_count,
6966                 x_msg_data                      =>  x_msg_data,
6967                 x_assign_resources_tbl          =>  x_assign_resources_tbl);
6968 
6969               -- set back the API name to original name
6970                l_api_name := l_api_name_1;
6971 
6972                IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
6973               -- Unexpected Execution Error from call to Get_contracts_resources
6974                  fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
6975                  fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
6976                  fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_OPPR_RESOURCES');
6977                  fnd_msg_pub.add;
6978                  IF (x_return_status = fnd_api.g_ret_sts_error) THEN
6979                   RAISE fnd_api.g_exc_error;
6980                  ELSE
6981                    RAISE fnd_api.g_exc_unexpected_error;
6982                  END IF;
6983               END IF; -- end of x_return_status check
6984         end if; -- if p_calendar_flag = Y
6985 
6986         IF ( p_calendar_flag = 'Y' AND
6987              x_assign_resources_tbl.count = 0 ) THEN
6988           fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
6989           fnd_msg_pub.add;
6990 --          RAISE fnd_api.g_exc_error;
6991         END IF;
6992 
6993         -- check auto assignment
6994         -- if auto assignment is Y then return only the number of resources that have been requested
6995         IF(l_auto_select_flag = 'Y')
6996         THEN
6997           l_temp_table.delete;
6998           l_temp_table := x_assign_resources_tbl;
6999           x_assign_resources_tbl.delete;
7000           l_count := 0;
7001           l_current_record := l_temp_table.FIRST;
7002           l_total_records := p_no_of_resources;
7003 
7004           WHILE l_current_record <= l_temp_table.LAST
7005           LOOP
7006              If(l_count < l_total_records)
7007              THEN
7008                  x_assign_resources_tbl(l_count) := l_temp_table(l_current_record);
7009                  l_count := l_count + 1;
7010              end if; -- end of count check
7011              l_current_record := l_temp_table.NEXT(l_current_record);
7012           END LOOP; -- end of courrent record check
7013 
7014          END IF; -- end of auto select flag
7015 
7016       ELSE   -- No resources returned from the Territory API
7017         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
7018         fnd_msg_pub.add;
7019 --        RAISE fnd_api.g_exc_error;
7020       END IF;
7021 
7022     ELSE  -- Territory Flag is NO
7023       fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
7024       fnd_msg_pub.add;
7025 --      RAISE fnd_api.g_exc_error;
7026     END IF;
7027 
7028 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7029 
7030       -- raise workfow event
7031       -- workflow test
7032       Begin
7033          jtf_assign_pub.g_assign_resources_tbl.delete;
7034          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
7035          jtf_am_wf_events_pub.assign_oppr_resource
7036                     (P_API_VERSION           =>  1.0,
7037                      P_INIT_MSG_LIST         =>  'F',
7038                      P_COMMIT                =>  'F',
7039                      P_OPPR_REC	         =>  p_opportunity_rec,
7040                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
7041                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
7042                      X_RETURN_STATUS         =>  l_wf_return_status,
7043                      X_MSG_COUNT             =>  l_wf_msg_count,
7044                      X_MSG_DATA              =>  l_wf_msg_data
7045                      );
7046 
7047 
7048         IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
7049             -- Unexpected Execution Error from call to assign_sr_resource
7050             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
7051             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
7052             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_OPPR_RESOURCES');
7053             fnd_msg_pub.add;
7054 
7055         ELSE
7056 		x_assign_resources_tbl.delete;
7057             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
7058         END IF;
7059 
7060 
7061       Exception
7062             When OTHERS Then
7063                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
7064                fnd_message.set_token('P_SQLCODE',SQLCODE);
7065                fnd_message.set_token('P_SQLERRM',SQLERRM);
7066                fnd_message.set_token('P_API_NAME',l_api_name);
7067                FND_MSG_PUB.add;
7068       End;
7069 
7070 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7071 
7072 
7073     -- To Plugin the Workflow enabling the user
7074     -- to further filter the resources
7075 
7076     /* Getting the Workflow Profile value defined for the Assignment Manager */
7077 
7078     --l_workflow_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_WORKFLOW_NAME' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
7079     l_workflow_profile := FND_PROFILE.VALUE ( 'ACTIVATE_WORKFLOW_NAME' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
7080 
7081     SELECT jtf_calendars_s.NEXTVAL INTO l_workflow_key
7082     FROM   dual;
7083 
7084     IF (JTF_USR_HKS.ok_to_execute
7085           (
7086             'JTF_ASSIGN_PUB',
7087             'GET_ASSIGN_OPPR_RESOURCES',
7088             'W',
7089             'W'
7090           )
7091        ) THEN
7092 
7093       IF (JTF_ASSIGN_CUHK.ok_to_launch_workflow
7094             (
7095               p_api_version     => l_api_version,
7096               p_init_msg_list   => p_init_msg_list,
7097               x_return_status   => x_return_status,
7098               x_msg_count       => x_msg_count,
7099               x_msg_data        => x_msg_data
7100             )
7101          ) THEN
7102 
7103         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
7104           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
7105           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
7106           fnd_msg_pub.add;
7107           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
7108             RAISE fnd_api.g_exc_error;
7109           ELSE
7110             RAISE fnd_api.g_exc_unexpected_error;
7111           END IF;
7112         END IF;
7113 
7114         l_bind_data_id := JTF_USR_HKS.get_bind_data_id;
7115 
7116         JTF_USR_HKS.WrkFlowLaunch
7117           (
7118             'JTF_ASSIGN_WF',
7119             l_workflow_profile,
7120             'ASSIGN WF - '|| TO_CHAR(l_workflow_key),
7121             l_bind_data_id,
7122             l_return_code
7123           );
7124 
7125         JTF_USR_HKS.purge_bind_data
7126           (
7127             l_bind_data_id,
7128             'W'
7129           );
7130 
7131         IF (l_return_code = fnd_api.g_ret_sts_error) THEN
7132           -- Execution Error from call to Assignment Manager Workflow Hook
7133           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
7134           fnd_msg_pub.add;
7135           RAISE fnd_api.g_exc_error;
7136         ELSIF (l_return_code = fnd_api.g_ret_sts_unexp_error) THEN
7137           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
7138           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
7139           fnd_msg_pub.add;
7140           RAISE fnd_api.g_exc_unexpected_error;
7141         END IF;
7142 
7143       END IF;  -- End of JTF_ASSIGN_CUHK
7144 
7145     END IF;    -- End of JTF_USR_HKS
7146 
7147 
7148 
7149 
7150 
7151     /* Standard call to get message count and
7152        the message information */
7153 
7154     FND_MSG_PUB.Count_And_Get
7155     (
7156       p_count => x_msg_count,
7157       p_data  => x_msg_data
7158     );
7159 
7160 
7161 
7162   EXCEPTION
7163     WHEN FND_API.G_EXC_ERROR THEN
7164       x_return_status := FND_API.G_RET_STS_ERROR ;
7165       FND_MSG_PUB.Count_And_Get
7166       (
7167         p_count => x_msg_count,
7168         p_data  => x_msg_data
7169       );
7170 
7171     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7172       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7173       FND_MSG_PUB.Count_And_Get
7174       (
7175         p_count => x_msg_count,
7176         p_data  => x_msg_data
7177       );
7178 
7179     WHEN OTHERS THEN
7180       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
7181       fnd_message.set_token('P_SQLCODE',SQLCODE);
7182       fnd_message.set_token('P_SQLERRM',SQLERRM);
7183       fnd_message.set_token('P_API_NAME',l_api_name);
7184       FND_MSG_PUB.add;
7185 
7186       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7187       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
7188         FND_MSG_PUB.Add_Exc_Msg
7189         (
7190           G_PKG_NAME,
7191           l_api_name
7192         );
7193       END IF;
7194 
7195       FND_MSG_PUB.Count_And_Get
7196       (
7197         p_count => x_msg_count,
7198         p_data  => x_msg_data
7199        );
7200 
7201   END GET_ASSIGN_OPPR_RESOURCES;
7202 
7203 
7204 
7205 -- *******************************************************************************
7206 
7207 -- Start of comments
7208 
7209 --      API name        : GET_ASSIGN_LEAD_RESOURCES (For BULK Record)
7210 --      Type            : Public
7211 --      Function        : Determine the resources based on the customer
7212 --                        preferences or territory preferences and the
7213 --                        availability. This is when the calling doc is
7214 --                        LEADS.
7215 --      Pre-reqs        : None
7216 
7217 --      Parameters      :
7218 
7219 --      IN              : p_api_version         IN      NUMBER  Required
7220 --                        p_init_msg_list       IN      VARCHAR2 Optional
7221 --                                              DEFAULT JTF_ASSIGN_PUB.AM_FALSE
7222 --                        p_commit              IN      VARCHAR2 optional
7223 --                                              DEFAULT JTF_ASSIGN_PUB.AM_FALSE
7224 
7225 --     Assignment Manager Specific Parameters
7226 
7227 --     This determines the Resource Type required by the
7228 --     calling document
7229 --     p_resource_type                          VARCHAR2(30)
7230 --     p_role                                   VARCHAR2(30)
7231 
7232 --     This determines the number of resources required
7233 --     by the calling document
7234 --     p_no_of_resources                        NUMBER,
7235 
7236 --     This is for sending out the qualified resource directly
7237 --     to the calling form. Defaulted to the PROFILE value
7238 --     p_auto_select_flag                       VARCHAR2(1)
7239 --                                              : value of  Y or N
7240 
7241 --     The Effort required is determined by these two parameters
7242 --     p_effort_duration                        NUMBER
7243 --     p_effort_uom                             VARCHAR2(10)
7244 
7245 --     The Dates in which the resource is required, is determined
7246 --     by these two parameters
7247 --     p_start_date                             DATE
7248 --     p_end_date                               DATE
7249 
7250 --     The Territory Manager is accessed based on the value set
7251 --     p_territory_flag                         VARCHAR2(1)
7252 --                                              : value of  Y or N
7253 
7254 --     The Resource Availability is checked based on the value set
7255 --     p_calendar_flag                          VARCHAR2(1)
7256 --                                              : value of  Y or N
7257 
7258 
7259 --     This parameter contains the values of the Qualifiers
7260 --     defined for the Sales Leads.
7261 --     p_lead_rec                               JTF_TERRITORY_PUB.
7262 --                                              JTF_Lead_BULK_rec_type
7263 --                                              REQUIRED
7264 
7265 --     OUT              : x_return_status        OUT     VARCHAR2(1)
7266 --                        x_msg_count            OUT     NUMBER
7267 --                        x_msg_data             OUT     VARCHAR2(2000)
7268 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
7269 --                                                       AssignResources_tbl_type
7270 
7271 
7272 --      Version         : Current version        1.0
7273 --                        Initial version        1.0
7274 --
7275 --      Notes           :
7276 --
7277 
7278 -- End of comments
7279 
7280 -- *********************************************************************************
7281 
7282 
7283 
7284 --      Procedure definition with the parameters when the
7285 --      Source Document is SALES LEADS
7286 
7287   PROCEDURE GET_ASSIGN_LEAD_RESOURCES
7288     (
7289         p_api_version                         IN  NUMBER,
7290         p_init_msg_list                       IN  VARCHAR2 ,
7291         p_resource_type                       IN  VARCHAR2 ,
7292         p_role                                IN  VARCHAR2 ,
7293         p_no_of_resources                     IN  NUMBER   ,
7294         p_auto_select_flag                    IN  VARCHAR2 ,
7295         p_effort_duration                     IN  NUMBER   ,
7296         p_effort_uom                          IN  VARCHAR2 ,
7297         p_start_date                          IN  DATE     ,
7298         p_end_date                            IN  DATE     ,
7299         p_territory_flag                      IN  VARCHAR2,
7300         p_calendar_flag                       IN  VARCHAR2,
7301         p_lead_rec                            IN  JTF_TERRITORY_PUB.JTF_Lead_BULK_rec_type,
7302 --      x_assign_resources_bulk_rec           OUT NOCOPY JTF_TERRITORY_PUB.WINNING_BULK_REC_TYPE,
7303         p_business_process_id                 IN  NUMBER,
7304         p_business_process_date               IN  DATE,
7305         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
7306         x_return_status                       OUT NOCOPY VARCHAR2,
7307         x_msg_count                           OUT NOCOPY NUMBER,
7308         x_msg_data                            OUT NOCOPY VARCHAR2
7309     )
7310   IS
7311 
7312     l_return_status_1                     VARCHAR2(10);
7313     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_LEAD_RESOURCES';
7314     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_LEAD_RESOURCES';
7315     l_api_version                         NUMBER        := 1.0;
7316     l_planned_start_date                  DATE;
7317     l_planned_end_date                    DATE;
7318     l_effort_duration                     NUMBER;
7319     l_effort_uom                          VARCHAR2(30);
7320     l_travel_time                         NUMBER        := 0;
7321     l_travel_uom                          VARCHAR2(30)  := 'HR';
7322 
7323     l_current_record                      INTEGER;
7324     l_total_records                       INTEGER;
7325 
7326     l_auto_select_profile                 VARCHAR2(03);
7327     l_auto_select_flag                    VARCHAR2(03);
7328     l_workflow_profile                    VARCHAR2(60);
7329 
7330     l_return_code                         VARCHAR2(60);
7331     l_bind_data_id                        NUMBER;
7332     l_workflow_key                        NUMBER;
7333 
7334     l_assign_resources_bulk_rec           JTF_TERRITORY_PUB.WINNING_BULK_REC_TYPE;
7335     --l_lead_rec                          JTF_TERRITORY_PUB.JTF_Lead_BULK_rec_type;
7336     --l_assign_resources_tbl              JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
7337 
7338     l_x_planned_start_date                DATE;
7339     l_x_planned_end_date                  DATE;
7340     l_x_shift_construct_id                NUMBER;
7341     l_x_availability_type                 VARCHAR2(60);
7342 
7343 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7344 
7345     l_wf_return_status                    VARCHAR2(60);
7346     l_wf_msg_count                        NUMBER;
7347     l_wf_msg_data                         VARCHAR2(2000);
7348 
7349 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7350 
7351      --Bug# 4455803 MOAC.
7352      CURSOR check_date_cur(l_resource_id in number,
7353                           l_resource_type in varchar2)
7354         IS
7355      SELECT 'Y'
7356        --FROM  jtf_rs_all_resources_vl
7357        FROM  jtf_task_resources_vl
7358       where   resource_id = l_resource_id
7359         and   resource_type = l_resource_type
7360         and   nvl(trunc(end_date_active), trunc(sysdate)) >= trunc(sysdate);
7361 
7362     l_value varchar2(100);
7363 
7364     l_count number := 0;
7365     l_temp_table  JTF_ASSIGN_PUB.AssignResources_tbl_type;
7366   BEGIN
7367 
7368     SAVEPOINT get_assign_lead_resources;
7369 
7370     -- Started Assignment Manager API for SALES LEADS
7371 
7372 
7373     /* Standard call to check for call compatibility */
7374 
7375     IF NOT fnd_api.compatible_api_call (l_api_version,
7376                                         p_api_version,
7377                                         l_api_name,
7378                                         g_pkg_name) THEN
7379       RAISE fnd_api.g_exc_unexpected_error;
7380     END IF;
7381 
7382 
7383     /* Initialize message list if p_init_msg_list is set to TRUE */
7384 
7385     IF fnd_api.to_boolean (p_init_msg_list) THEN
7386       fnd_msg_pub.initialize;
7387     END IF;
7388 
7389     x_return_status := fnd_api.g_ret_sts_success;
7390 
7391 
7392 
7393     /* Getting the Auto Select Profile value defined for the Assignment Manager */
7394 
7395     --l_auto_select_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_AUTO_SELECT' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
7396     l_auto_select_profile := FND_PROFILE.VALUE ( 'ACTIVATE_AUTO_SELECT' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
7397 
7398 
7399 
7400     /* Assigning the DEFAULT value to the Auto Select Parameter */
7401 
7402 
7403     IF (p_auto_select_flag IS NULL) THEN
7404       l_auto_select_flag  := l_auto_select_profile;
7405                              -- PROFILE VALUE is the default value
7406     ELSE
7407       l_auto_select_flag  := p_auto_select_flag;
7408     END IF;
7409 
7410 
7411 
7412 
7413     /* Defaulting the Calendar variable values to IN parameters,
7414        if the IN paramaters have values given */
7415 
7416 
7417     IF (p_start_date IS NOT NULL) THEN
7418       l_planned_start_date := p_start_date;
7419     ELSE
7420       l_planned_start_date := SYSDATE;
7421     END IF;
7422 
7423     IF (p_end_date IS NOT NULL) THEN
7424       l_planned_end_date := p_end_date;
7425     ELSE
7426       l_planned_end_date := SYSDATE;
7427     END IF;
7428 
7429     IF (p_effort_duration IS NOT NULL) THEN
7430       l_effort_duration  := p_effort_duration;
7431     END IF;
7432 
7433     IF (p_effort_uom IS NOT NULL) THEN
7434       l_effort_uom := p_effort_uom;
7435     END IF;
7436 
7437 
7438 
7439 
7440     /* Actual Flow of Assignment Manager */
7441 
7442 
7443     IF (p_territory_flag = 'Y') THEN
7444 
7445       -- change the API Name temporarily so that in case of unexpected error
7446       -- it is properly caught
7447       l_api_name := l_api_name||'-JTF_TERR_SALES_PUB';
7448 
7449       JTF_TERR_SALES_PUB.Get_WinningTerrMembers
7450         (
7451           p_api_version_number  => l_api_version,
7452           p_init_msg_list       => p_init_msg_list,
7453           p_TerrLead_Rec        => p_lead_rec,
7454           p_Resource_Type       => p_resource_type,
7455           p_Role                => p_role,
7456           x_return_status       => x_return_status,
7457           x_msg_count           => x_msg_count,
7458           x_msg_data            => x_msg_data,
7459           x_winners_rec         => l_assign_resources_bulk_rec
7460         );
7461 
7462         -- set back the API name to original name
7463         l_api_name := l_api_name_1;
7464 
7465       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
7466         -- Unexpected Execution Error from call to Territory Manager
7467         fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
7468         fnd_msg_pub.add;
7469         IF (x_return_status = fnd_api.g_ret_sts_error) THEN
7470           RAISE fnd_api.g_exc_error;
7471         ELSE
7472           RAISE fnd_api.g_exc_unexpected_error;
7473         END IF;
7474       END IF;
7475 
7476 
7477       IF l_assign_resources_bulk_rec.terr_id.COUNT > 0 THEN
7478 
7479         l_current_record := l_assign_resources_bulk_rec.terr_id.FIRST;
7480 
7481 
7482 
7483         WHILE l_current_record <= l_assign_resources_bulk_rec.terr_id.LAST
7484         LOOP
7485             -- removed the calendar check here. calling the procedure for calendar check in the next step
7486             -- calendar check removed 29th September 2003
7487 
7488              open check_date_cur(l_assign_resources_bulk_rec.resource_id(l_current_record),
7489                               l_assign_resources_bulk_rec.resource_type(l_current_record));
7490              fetch check_date_cur into l_value;
7491              if (check_date_cur%found)
7492              then
7493 
7494                l_count := l_count + 1;
7495                x_assign_resources_tbl(l_count).terr_rsc_id           :=
7496                                      l_assign_resources_bulk_rec.terr_rsc_id(l_current_record);
7497                x_assign_resources_tbl(l_count).resource_id           :=
7498                                      l_assign_resources_bulk_rec.resource_id(l_current_record);
7499                x_assign_resources_tbl(l_count).resource_type         :=
7500                                      l_assign_resources_bulk_rec.resource_type(l_current_record);
7501                x_assign_resources_tbl(l_count).role                  :=
7502                                      l_assign_resources_bulk_rec.role(l_current_record);
7503 
7504                x_assign_resources_tbl(l_count).start_date            := NULL;
7505 --                                   l_assign_resources_bulk_rec.start_date(l_current_record);
7506                x_assign_resources_tbl(l_count).end_date              := NULL;
7507 --                                   l_assign_resources_bulk_rec.end_date(l_current_record);
7508 
7509                x_assign_resources_tbl(l_count).shift_construct_id    := NULL;
7510 
7511                x_assign_resources_tbl(l_count).terr_id               :=
7512                                      l_assign_resources_bulk_rec.terr_id(l_current_record);
7513                x_assign_resources_tbl(l_count).terr_rank             :=
7514                                      l_assign_resources_bulk_rec.absolute_rank(l_current_record);
7515                x_assign_resources_tbl(l_count).primary_contact_flag  :=
7516                                      l_assign_resources_bulk_rec.primary_contact_flag(l_current_record);
7517                x_assign_resources_tbl(l_count).full_access_flag      :=
7518                                      l_assign_resources_bulk_rec.full_access_flag(l_current_record);
7519                x_assign_resources_tbl(l_count).group_id              :=
7520                                      l_assign_resources_bulk_rec.group_id(l_current_record);
7521                x_assign_resources_tbl(l_count).trans_object_id       :=
7522                                      l_assign_resources_bulk_rec.trans_object_id(l_current_record);
7523                x_assign_resources_tbl(l_count).primary_flag       :=
7524                                      l_assign_resources_bulk_rec.primary_contact_flag(l_current_record);
7525                x_assign_resources_tbl(l_count).resource_source       := 'TERR';
7526 
7527               END IF;
7528               close check_date_cur;
7529               l_current_record := l_current_record+1;
7530           --l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
7531         END LOOP;
7532 
7533 
7534       -- added calendar call out
7535       -- this has been done as now the calendar check is done in a seperate procedure GET_AVAILABLE_RESOURCE
7536       -- changed on 29th September 2003
7537       IF (p_calendar_flag = 'Y') THEN
7538              -- change the API Name temporarily so that in case of unexpected error
7539              -- it is properly caught
7540               l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
7541               l_return_status_1 := x_return_status ;
7542               -- call the api to check resource availability
7543               get_available_resources
7544               (
7545                 p_init_msg_list                 =>  'F',
7546                 p_calendar_flag                 =>  p_calendar_flag,
7547                 p_effort_duration               =>  l_effort_duration,
7548                 p_effort_uom                    =>  l_effort_uom,
7549                 p_planned_start_date            =>  l_planned_start_date,
7550                 p_planned_end_date              =>  l_planned_end_date,
7551                 p_continuous_task               =>  jtf_assign_pub.g_continuous_work,
7552                 x_return_status                 =>  x_return_status,
7553                 x_msg_count                     =>  x_msg_count,
7554                 x_msg_data                      =>  x_msg_data,
7555                 x_assign_resources_tbl          =>  x_assign_resources_tbl);
7556 
7557               -- set back the API name to original name
7558                l_api_name := l_api_name_1;
7559 
7560                IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
7561               -- Unexpected Execution Error from call to Get_contracts_resources
7562                  fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
7563                  fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
7564                  fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_LEAD_RESOURCES');
7565                  fnd_msg_pub.add;
7566                  IF (x_return_status = fnd_api.g_ret_sts_error) THEN
7567                   RAISE fnd_api.g_exc_error;
7568                  ELSE
7569                    RAISE fnd_api.g_exc_unexpected_error;
7570                  END IF;
7571               END IF; -- end of x_return_status check
7572         end if; -- if p_calendar_flag = Y
7573 
7574         IF ( p_calendar_flag = 'Y' AND
7575              x_assign_resources_tbl.count = 0 ) THEN
7576           fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
7577           fnd_msg_pub.add;
7578 --          RAISE fnd_api.g_exc_error;
7579         END IF;
7580 
7581 
7582        -- If auto_select is Y then ensure correct number of resources are returned
7583        IF(l_auto_select_flag = 'Y')
7584         THEN
7585           l_temp_table.delete;
7586           l_temp_table := x_assign_resources_tbl;
7587           x_assign_resources_tbl.delete;
7588           l_count := 0;
7589           l_current_record := l_temp_table.FIRST;
7590           l_total_records := p_no_of_resources;
7591 
7592           WHILE l_current_record <= l_temp_table.LAST
7593           LOOP
7594              If(l_count < l_total_records)
7595              THEN
7596                  x_assign_resources_tbl(l_count) := l_temp_table(l_current_record);
7597                  l_count := l_count + 1;
7598              end if; -- end of count check
7599              l_current_record := l_temp_table.NEXT(l_current_record);
7600           END LOOP; -- end of courrent record check
7601 
7602          END IF; -- end of auto select flag
7603 
7604       ELSE   -- No resources returned from the Territory API
7605         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
7606         fnd_msg_pub.add;
7607 --        RAISE fnd_api.g_exc_error;
7608       END IF;
7609 
7610     ELSE  -- Territory Flag is NO
7611       fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
7612       fnd_msg_pub.add;
7613 --      RAISE fnd_api.g_exc_error;
7614     END IF;
7615 
7616 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7617 
7618       -- raise workfow event
7619       -- workflow test
7620       Begin
7621          jtf_assign_pub.g_assign_resources_tbl.delete;
7622          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
7623          jtf_am_wf_events_pub.assign_lead_resource
7624                     (P_API_VERSION           =>  1.0,
7625                      P_INIT_MSG_LIST         =>  'F',
7626                      P_COMMIT                =>  'F',
7627                      P_LEAD_REC              =>  Null,
7628                      P_LEAD_BULK_REC	   =>  p_lead_rec,
7629                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
7630                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
7631                      X_RETURN_STATUS         =>  l_wf_return_status,
7632                      X_MSG_COUNT             =>  l_wf_msg_count,
7633                      X_MSG_DATA              =>  l_wf_msg_data
7634                      );
7635 
7636 
7637         IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
7638             -- Unexpected Execution Error from call to assign_sr_resource
7639             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
7640             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
7641             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_LEAD_RESOURCES');
7642             fnd_msg_pub.add;
7643 
7644         ELSE
7645 		x_assign_resources_tbl.delete;
7646             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
7647         END IF;
7648 
7649 
7650       Exception
7651             When OTHERS Then
7652                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
7653                fnd_message.set_token('P_SQLCODE',SQLCODE);
7654                fnd_message.set_token('P_SQLERRM',SQLERRM);
7655                fnd_message.set_token('P_API_NAME',l_api_name);
7656                FND_MSG_PUB.add;
7657       End;
7658 
7659 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7660 
7661     -- To Plugin the Workflow enabling the user
7662     -- to further filter the resources
7663 
7664     /* Getting the Workflow Profile value defined for the Assignment Manager */
7665 
7666     --l_workflow_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_WORKFLOW_NAME' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
7667     l_workflow_profile := FND_PROFILE.VALUE ( 'ACTIVATE_WORKFLOW_NAME' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
7668 
7669     SELECT jtf_calendars_s.NEXTVAL INTO l_workflow_key
7670     FROM   dual;
7671 
7672     IF (JTF_USR_HKS.ok_to_execute
7673           (
7674             'JTF_ASSIGN_PUB',
7675             'GET_ASSIGN_LEAD_RESOURCES',
7676             'W',
7677             'W'
7678           )
7679        ) THEN
7680 
7681       IF (JTF_ASSIGN_CUHK.ok_to_launch_workflow
7682             (
7683               p_api_version     => l_api_version,
7684               p_init_msg_list   => p_init_msg_list,
7685               x_return_status   => x_return_status,
7686               x_msg_count       => x_msg_count,
7687               x_msg_data        => x_msg_data
7688             )
7689          ) THEN
7690 
7691         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
7692           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
7693           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
7694           fnd_msg_pub.add;
7695           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
7696             RAISE fnd_api.g_exc_error;
7697           ELSE
7698             RAISE fnd_api.g_exc_unexpected_error;
7699           END IF;
7700         END IF;
7701 
7702         l_bind_data_id := JTF_USR_HKS.get_bind_data_id;
7703 
7704         JTF_USR_HKS.WrkFlowLaunch
7705           (
7706             'JTF_ASSIGN_WF',
7707             l_workflow_profile,
7708             'ASSIGN WF - '|| TO_CHAR(l_workflow_key),
7709             l_bind_data_id,
7710             l_return_code
7711           );
7712 
7713         JTF_USR_HKS.purge_bind_data
7714           (
7715             l_bind_data_id,
7716             'W'
7717           );
7718 
7719         IF (l_return_code = fnd_api.g_ret_sts_error) THEN
7720           -- Execution Error from call to Assignment Manager Workflow Hook
7721           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
7722           fnd_msg_pub.add;
7723           RAISE fnd_api.g_exc_error;
7724         ELSIF (l_return_code = fnd_api.g_ret_sts_unexp_error) THEN
7725           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
7726           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
7727           fnd_msg_pub.add;
7728           RAISE fnd_api.g_exc_unexpected_error;
7729         END IF;
7730 
7731       END IF;  -- End of JTF_ASSIGN_CUHK
7732 
7733     END IF;    -- End of JTF_USR_HKS
7734 
7735 
7736 
7737 
7738     /* Standard call to get message count and
7739        the message information */
7740 
7741     FND_MSG_PUB.Count_And_Get
7742     (
7743       p_count => x_msg_count,
7744       p_data  => x_msg_data
7745     );
7746 
7747 
7748 
7749   EXCEPTION
7750     WHEN FND_API.G_EXC_ERROR THEN
7751       x_return_status := FND_API.G_RET_STS_ERROR ;
7752       FND_MSG_PUB.Count_And_Get
7753       (
7754         p_count => x_msg_count,
7755         p_data  => x_msg_data
7756       );
7757 
7758     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7759       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7760       FND_MSG_PUB.Count_And_Get
7761       (
7762         p_count => x_msg_count,
7763         p_data  => x_msg_data
7764       );
7765 
7766     WHEN OTHERS THEN
7767       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
7768       fnd_message.set_token('P_SQLCODE',SQLCODE);
7769       fnd_message.set_token('P_SQLERRM',SQLERRM);
7770       fnd_message.set_token('P_API_NAME',l_api_name);
7771       FND_MSG_PUB.add;
7772 
7773       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7774       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
7775         FND_MSG_PUB.Add_Exc_Msg
7776         (
7777           G_PKG_NAME,
7778           l_api_name
7779         );
7780       END IF;
7781 
7782       FND_MSG_PUB.Count_And_Get
7783       (
7784         p_count => x_msg_count,
7785         p_data  => x_msg_data
7786        );
7787 
7788   END GET_ASSIGN_LEAD_RESOURCES;
7789 
7790 
7791 
7792 -- *******************************************************************************
7793 
7794 -- PLEASE DO NOT USE THIS API
7795 -- BEING SUPPORTED ONLY FOR BACKWARD COMPATIBILITY
7796 
7797 --      API name        : GET_ASSIGN_LEAD_RESOURCES (For SINGLE Record)
7798 --      Type            : Public
7799 --      Function        : Determine the resources based on the customer
7800 --                        preferences or territory preferences and the
7801 --                        availability. This is when the calling doc is
7802 --                        LEADS.
7803 --     This parameter contains the values of the Qualifiers
7804 --     defined for the Sales Leads.
7805 --     p_lead_rec                               JTF_ASSIGN_PUB.
7806 --                                              JTF_Lead_rec_type
7807 --                                              REQUIRED
7808 
7809 --     OUT              : x_return_status        OUT     VARCHAR2(1)
7810 --                        x_msg_count            OUT     NUMBER
7811 --                        x_msg_data             OUT     VARCHAR2(2000)
7812 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
7813 --                                                       AssignResources_tbl_type
7814 
7815 
7816 --      Version         : Current version        1.0
7817 --                        Initial version        1.0
7818 --
7819 --      Notes           :
7820 --
7821 
7822 -- End of comments
7823 
7824 -- *********************************************************************************
7825 
7826 --      Procedure definition with the parameters when the
7827 --      Source Document is SALES LEADS
7828 
7829   PROCEDURE GET_ASSIGN_LEAD_RESOURCES
7830     (
7831         p_api_version                         IN  NUMBER,
7832         p_init_msg_list                       IN  VARCHAR2 ,
7833         p_resource_type                       IN  VARCHAR2 ,
7834         p_role                                IN  VARCHAR2 ,
7835         p_no_of_resources                     IN  NUMBER   ,
7836         p_auto_select_flag                    IN  VARCHAR2 ,
7837         p_effort_duration                     IN  NUMBER   ,
7838         p_effort_uom                          IN  VARCHAR2 ,
7839         p_start_date                          IN  DATE     ,
7840         p_end_date                            IN  DATE     ,
7841         p_territory_flag                      IN  VARCHAR2 ,
7842         p_calendar_flag                       IN  VARCHAR2 ,
7843         p_lead_rec                            IN  JTF_ASSIGN_PUB.JTF_Lead_rec_type,
7844         p_business_process_id                 IN  NUMBER,
7845         p_business_process_date               IN  DATE,
7846         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
7847         x_return_status                       OUT NOCOPY VARCHAR2,
7848         x_msg_count                           OUT NOCOPY NUMBER,
7849         x_msg_data                            OUT NOCOPY VARCHAR2
7850     )
7851   IS
7852 
7853     l_return_status_1                     VARCHAR2(10);
7854     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_LEAD_RESOURCES';
7855     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_LEAD_RESOURCES';
7856     l_api_version                         NUMBER        := 1.0;
7857     l_planned_start_date                  DATE;
7858     l_planned_end_date                    DATE;
7859     l_effort_duration                     NUMBER;
7860     l_effort_uom                          VARCHAR2(30);
7861     l_travel_time                         NUMBER        := 0;
7862     l_travel_uom                          VARCHAR2(30)  := 'HR';
7863 
7864     l_current_record                      INTEGER;
7865     l_total_records                       INTEGER;
7866 
7867     l_auto_select_profile                 VARCHAR2(03);
7868     l_auto_select_flag                    VARCHAR2(03);
7869     l_workflow_profile                    VARCHAR2(60);
7870 
7871     l_return_code                         VARCHAR2(60);
7872     l_bind_data_id                        NUMBER;
7873     l_workflow_key                        NUMBER;
7874 
7875     l_assign_resources_tbl                JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
7876     l_lead_rec                            JTF_TERRITORY_PUB.JTF_Lead_rec_type;
7877 
7878     l_x_planned_start_date                DATE;
7879     l_x_planned_end_date                  DATE;
7880     l_x_shift_construct_id                NUMBER;
7881     l_x_availability_type                 VARCHAR2(60);
7882 
7883 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7884 
7885     l_wf_return_status                    VARCHAR2(60);
7886     l_wf_msg_count                        NUMBER;
7887     l_wf_msg_data                         VARCHAR2(2000);
7888 
7889 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
7890 
7891 
7892   BEGIN
7893 
7894     SAVEPOINT get_assign_lead_resources;
7895 
7896     -- Started Assignment Manager API for SALES LEADS
7897 
7898 
7899     /* Standard call to check for call compatibility */
7900 
7901     IF NOT fnd_api.compatible_api_call (l_api_version,
7902                                         p_api_version,
7903                                         l_api_name,
7904                                         g_pkg_name) THEN
7905       RAISE fnd_api.g_exc_unexpected_error;
7906     END IF;
7907 
7908 
7909     /* Initialize message list if p_init_msg_list is set to TRUE */
7910 
7911     IF fnd_api.to_boolean (p_init_msg_list) THEN
7912       fnd_msg_pub.initialize;
7913     END IF;
7914 
7915     x_return_status := fnd_api.g_ret_sts_success;
7916 
7917 
7918     /* Getting the Auto Select Profile value defined for the Assignment Manager */
7919 
7920     --l_auto_select_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_AUTO_SELECT' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
7921     l_auto_select_profile := FND_PROFILE.VALUE ( 'ACTIVATE_AUTO_SELECT' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
7922 
7923 
7924 
7925     /* Assigning the DEFAULT value to the Auto Select Parameter */
7926 
7927 
7928     IF (p_auto_select_flag IS NULL) THEN
7929       l_auto_select_flag  := l_auto_select_profile;
7930                              -- PROFILE VALUE is the default value
7931     ELSE
7932       l_auto_select_flag  := p_auto_select_flag;
7933     END IF;
7934 
7935 
7936     /* Defaulting the Calendar variable values to IN parameters,
7937        if the IN paramaters have values given */
7938 
7939 
7940     IF (p_start_date IS NOT NULL) THEN
7941       l_planned_start_date := p_start_date;
7942     ELSE
7943       l_planned_start_date := SYSDATE;
7944     END IF;
7945 
7946     IF (p_end_date IS NOT NULL) THEN
7947       l_planned_end_date := p_end_date;
7948     ELSE
7949       l_planned_end_date := SYSDATE;
7950     END IF;
7951 
7952     IF (p_effort_duration IS NOT NULL) THEN
7953       l_effort_duration  := p_effort_duration;
7954     END IF;
7955 
7956     IF (p_effort_uom IS NOT NULL) THEN
7957       l_effort_uom := p_effort_uom;
7958     END IF;
7959 
7960 
7961 
7962     /* Assigning values to the Lead Record Type */
7963 
7964 
7965     l_lead_rec.SALES_LEAD_ID                 :=  p_lead_rec.SALES_LEAD_ID;
7966     l_lead_rec.SALES_LEAD_LINE_ID            :=  p_lead_rec.SALES_LEAD_LINE_ID;
7967     l_lead_rec.CITY                          :=  p_lead_rec.CITY;
7968     l_lead_rec.POSTAL_CODE                   :=  p_lead_rec.POSTAL_CODE;
7969     l_lead_rec.STATE                         :=  p_lead_rec.STATE;
7970     l_lead_rec.PROVINCE                      :=  p_lead_rec.PROVINCE;
7971     l_lead_rec.COUNTY                        :=  p_lead_rec.COUNTY;
7972     l_lead_rec.COUNTRY                       :=  p_lead_rec.COUNTRY;
7973     l_lead_rec.INTEREST_TYPE_ID              :=  p_lead_rec.INTEREST_TYPE_ID;
7974     l_lead_rec.PRIMARY_INTEREST_ID           :=  p_lead_rec.PRIMARY_INTEREST_ID;
7975     l_lead_rec.SECONDARY_INTEREST_ID         :=  p_lead_rec.SECONDARY_INTEREST_ID;
7976     l_lead_rec.CONTACT_INTEREST_TYPE_ID      :=  p_lead_rec.CONTACT_INTEREST_TYPE_ID;
7977     l_lead_rec.CONTACT_PRIMARY_INTEREST_ID   :=  p_lead_rec.CONTACT_PRIMARY_INTEREST_ID;
7978     l_lead_rec.CONTACT_SECONDARY_INTEREST_ID :=  p_lead_rec.CONTACT_SECONDARY_INTEREST_ID;
7979     l_lead_rec.PARTY_SITE_ID                 :=  p_lead_rec.PARTY_SITE_ID;
7980     l_lead_rec.AREA_CODE                     :=  p_lead_rec.AREA_CODE;
7981     l_lead_rec.PARTY_ID                      :=  p_lead_rec.PARTY_ID;
7982     l_lead_rec.COMP_NAME_RANGE               :=  p_lead_rec.COMP_NAME_RANGE;
7983     l_lead_rec.PARTNER_ID                    :=  p_lead_rec.PARTNER_ID;
7984     l_lead_rec.NUM_OF_EMPLOYEES              :=  p_lead_rec.NUM_OF_EMPLOYEES;
7985     l_lead_rec.CATEGORY_CODE                 :=  p_lead_rec.CATEGORY_CODE;
7986     l_lead_rec.PARTY_RELATIONSHIP_ID         :=  p_lead_rec.PARTY_RELATIONSHIP_ID;
7987     l_lead_rec.SIC_CODE                      :=  p_lead_rec.SIC_CODE;
7988     l_lead_rec.BUDGET_AMOUNT                 :=  p_lead_rec.BUDGET_AMOUNT;
7989     l_lead_rec.CURRENCY_CODE                 :=  p_lead_rec.CURRENCY_CODE;
7990     l_lead_rec.PRICING_DATE                  :=  p_lead_rec.PRICING_DATE;
7991     l_lead_rec.SOURCE_PROMOTION_ID           :=  p_lead_rec.SOURCE_PROMOTION_ID;
7992     l_lead_rec.INVENTORY_ITEM_ID             :=  p_lead_rec.INVENTORY_ITEM_ID;
7993     l_lead_rec.LEAD_INTEREST_TYPE_ID         :=  p_lead_rec.LEAD_INTEREST_TYPE_ID;
7994     l_lead_rec.LEAD_PRIMARY_INTEREST_ID      :=  p_lead_rec.LEAD_PRIMARY_INTEREST_ID;
7995     l_lead_rec.LEAD_SECONDARY_INTEREST_ID    :=  p_lead_rec.LEAD_SECONDARY_INTEREST_ID;
7996     l_lead_rec.PURCHASE_AMOUNT               :=  p_lead_rec.PURCHASE_AMOUNT;
7997     l_lead_rec.ATTRIBUTE1                    :=  p_lead_rec.ATTRIBUTE1;
7998     l_lead_rec.ATTRIBUTE2                    :=  p_lead_rec.ATTRIBUTE2;
7999     l_lead_rec.ATTRIBUTE3                    :=  p_lead_rec.ATTRIBUTE3;
8000     l_lead_rec.ATTRIBUTE4                    :=  p_lead_rec.ATTRIBUTE4;
8001     l_lead_rec.ATTRIBUTE5                    :=  p_lead_rec.ATTRIBUTE5;
8002     l_lead_rec.ATTRIBUTE6                    :=  p_lead_rec.ATTRIBUTE6;
8003     l_lead_rec.ATTRIBUTE7                    :=  p_lead_rec.ATTRIBUTE7;
8004     l_lead_rec.ATTRIBUTE8                    :=  p_lead_rec.ATTRIBUTE8;
8005     l_lead_rec.ATTRIBUTE9                    :=  p_lead_rec.ATTRIBUTE9;
8006     l_lead_rec.ATTRIBUTE10                   :=  p_lead_rec.ATTRIBUTE10;
8007     l_lead_rec.ATTRIBUTE11                   :=  p_lead_rec.ATTRIBUTE11;
8008     l_lead_rec.ATTRIBUTE12                   :=  p_lead_rec.ATTRIBUTE12;
8009     l_lead_rec.ATTRIBUTE13                   :=  p_lead_rec.ATTRIBUTE13;
8010     l_lead_rec.ATTRIBUTE14                   :=  p_lead_rec.ATTRIBUTE14;
8011     l_lead_rec.ATTRIBUTE15                   :=  p_lead_rec.ATTRIBUTE15;
8012 
8013 
8014 
8015     /* Actual Flow of Assignment Manager */
8016 
8017 
8018     IF (p_territory_flag = 'Y') THEN
8019 
8020       -- change the API Name temporarily so that in case of unexpected error
8021       -- it is properly caught
8022       l_api_name := l_api_name||'-JTF_TERR_SALES_PUB';
8023 
8024       JTF_TERR_SALES_PUB.Get_WinningTerrMembers
8025         (
8026           p_api_version_number  => l_api_version,
8027           p_init_msg_list       => p_init_msg_list,
8028           p_TerrLead_Rec        => l_lead_rec,
8029           p_Resource_Type       => p_resource_type,
8030           p_Role                => p_role,
8031           x_return_status       => x_return_status,
8032           x_msg_count           => x_msg_count,
8033           x_msg_data            => x_msg_data,
8034           x_TerrResource_tbl    => l_assign_resources_tbl
8035         );
8036 
8037         -- set back the API name to original name
8038         l_api_name := l_api_name_1;
8039 
8040       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
8041         -- Unexpected Execution Error from call to Territory Manager
8042         fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
8043         fnd_msg_pub.add;
8044         IF (x_return_status = fnd_api.g_ret_sts_error) THEN
8045           RAISE fnd_api.g_exc_error;
8046         ELSE
8047           RAISE fnd_api.g_exc_unexpected_error;
8048         END IF;
8049       END IF;
8050 
8051       -- added 2 april 2003 by sudarsana to conver RS_SUPPLIER TO RS_SUPPLIER_CONTACT
8052 
8053           IF(l_assign_resources_tbl.COUNT > 0)
8054           THEN
8055              l_current_record := l_assign_resources_tbl.FIRST;
8056              WHILE l_current_record <= l_assign_resources_tbl.LAST
8057              LOOP
8058                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
8059                 THEN
8060                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
8061                 END IF;
8062                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
8063              END LOOP;
8064           END IF;
8065 
8066       IF l_assign_resources_tbl.COUNT > 0 THEN
8067 
8068         l_current_record := l_assign_resources_tbl.FIRST;
8069 
8070 
8071         IF (l_auto_select_flag = 'Y') THEN
8072           l_total_records := p_no_of_resources;
8073         ELSE
8074           l_total_records := l_assign_resources_tbl.LAST;
8075         END IF;
8076 
8077 
8078         WHILE l_current_record <= l_assign_resources_tbl.LAST
8079         LOOP
8080           -- Check the calendar for resource availability
8081             -- Call Calendar API
8082             -- IF the resource is available then accept the values and
8083             -- check for the WORKFLOW profile option
8084 
8085           IF (p_calendar_flag = 'Y') THEN
8086 
8087           -- change the API Name temporarily so that in case of unexpected error
8088           -- it is properly caught
8089           l_api_name := l_api_name||'-JTF_CALENDAR_PUB';
8090           l_return_status_1 := x_return_status ;
8091 
8092             JTF_CALENDAR_PUB.GET_AVAILABLE_SLOT
8093               (
8094                 P_API_VERSION        => l_api_version,
8095                 P_INIT_MSG_LIST      => p_init_msg_list,
8096                 P_RESOURCE_ID        => l_assign_resources_tbl(l_current_record).resource_id,
8097                 P_RESOURCE_TYPE      => l_assign_resources_tbl(l_current_record).resource_type,
8098                 P_START_DATE_TIME    => l_planned_start_date,
8099                 P_END_DATE_TIME      => l_planned_end_date,
8100                 P_DURATION           => l_effort_duration,
8101                 X_RETURN_STATUS      => x_return_status,
8102                 X_MSG_COUNT          => x_msg_count,
8103                 X_MSG_DATA           => x_msg_data,
8104                 X_SLOT_START_DATE    => l_x_planned_start_date,
8105                 X_SLOT_END_DATE      => l_x_planned_end_date,
8106                 X_SHIFT_CONSTRUCT_ID => l_x_shift_construct_id,
8107                 X_AVAILABILITY_TYPE  => l_x_availability_type
8108               );
8109 
8110             -- set back the API name to original name
8111             l_api_name := l_api_name_1;
8112 
8113             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
8114               -- Unexpected Execution Error from call to Calendar
8115               fnd_message.set_name('JTF', 'JTF_AM_ERROR_CAL_API');
8116               fnd_msg_pub.add;
8117               IF (x_return_status = fnd_api.g_ret_sts_error) THEN
8118 			  	 --  12/22/05 ** MPADHIAR ** Manas padhiary **
8119 				 --  Removed Comment to Show error message Bug # 2919389
8120                 RAISE fnd_api.g_exc_error;
8121               ELSE
8122                 RAISE fnd_api.g_exc_unexpected_error;
8123               END IF;
8124             END IF;
8125 
8126 
8127             IF (l_x_shift_construct_id IS NOT NULL) THEN
8128 
8129               -- The following IF statement is to implement Auto Select Feature
8130               IF (l_current_record <= l_total_records) THEN
8131 
8132                 x_assign_resources_tbl(l_current_record).terr_rsc_id           :=
8133                                        l_assign_resources_tbl(l_current_record).terr_rsc_id;
8134                 x_assign_resources_tbl(l_current_record).resource_id           :=
8135                                        l_assign_resources_tbl(l_current_record).resource_id;
8136                 x_assign_resources_tbl(l_current_record).resource_type         :=
8137                                        l_assign_resources_tbl(l_current_record).resource_type;
8138                 x_assign_resources_tbl(l_current_record).role                  :=
8139                                        l_assign_resources_tbl(l_current_record).role;
8140 
8141                 IF (l_travel_uom like 'HR%') THEN
8142                   x_assign_resources_tbl(l_current_record).start_date      :=
8143                                          l_x_planned_start_date + l_travel_time/24;
8144                   x_assign_resources_tbl(l_current_record).end_date        :=
8145                                          l_x_planned_end_date   + l_travel_time/24;
8146                 ELSIF (l_travel_uom like 'MI%') THEN
8147                   x_assign_resources_tbl(l_current_record).start_date      :=
8148                                          l_x_planned_start_date + l_travel_time/1440;
8149                   x_assign_resources_tbl(l_current_record).end_date        :=
8150                                          l_x_planned_end_date   + l_travel_time/1440;
8151                 ELSIF (l_travel_uom like 'S%') THEN
8152                   x_assign_resources_tbl(l_current_record).start_date      :=
8153                                          l_x_planned_start_date + l_travel_time/86400;
8154                   x_assign_resources_tbl(l_current_record).end_date        :=
8155                                          l_x_planned_end_date   + l_travel_time/86400;
8156                 END IF;
8157 
8158                 x_assign_resources_tbl(l_current_record).shift_construct_id    := l_x_shift_construct_id;
8159                 x_assign_resources_tbl(l_current_record).terr_id               :=
8160                                        l_assign_resources_tbl(l_current_record).terr_id;
8161                 x_assign_resources_tbl(l_current_record).terr_name             :=
8162                                        l_assign_resources_tbl(l_current_record).terr_name;
8163                 x_assign_resources_tbl(l_current_record).primary_contact_flag  :=
8164                                        l_assign_resources_tbl(l_current_record).primary_contact_flag;
8165                 x_assign_resources_tbl(l_current_record).full_access_flag      :=
8166                                        l_assign_resources_tbl(l_current_record).full_access_flag;
8167                 x_assign_resources_tbl(l_current_record).group_id              :=
8168                                        l_assign_resources_tbl(l_current_record).group_id;
8169                 x_assign_resources_tbl(l_current_record).resource_source       := 'TERR';
8170 
8171               END IF;
8172             END IF;
8173 
8174           ELSE    -- Calendar Flag is NO
8175 
8176             -- The following IF statement is to implement Auto Select Feature
8177             IF (l_current_record <= l_total_records) THEN
8178               x_assign_resources_tbl(l_current_record).terr_rsc_id           :=
8179                                      l_assign_resources_tbl(l_current_record).terr_rsc_id;
8180               x_assign_resources_tbl(l_current_record).resource_id           :=
8181                                      l_assign_resources_tbl(l_current_record).resource_id;
8182               x_assign_resources_tbl(l_current_record).resource_type         :=
8183                                      l_assign_resources_tbl(l_current_record).resource_type;
8184               x_assign_resources_tbl(l_current_record).role                  :=
8185                                      l_assign_resources_tbl(l_current_record).role;
8186               x_assign_resources_tbl(l_current_record).start_date            :=
8187                                      l_assign_resources_tbl(l_current_record).start_date;
8188               x_assign_resources_tbl(l_current_record).end_date              :=
8189                                      l_assign_resources_tbl(l_current_record).end_date;
8190               x_assign_resources_tbl(l_current_record).shift_construct_id    := NULL;
8191               x_assign_resources_tbl(l_current_record).terr_id               :=
8192                                      l_assign_resources_tbl(l_current_record).terr_id;
8193               x_assign_resources_tbl(l_current_record).terr_name             :=
8194                                      l_assign_resources_tbl(l_current_record).terr_name;
8195               x_assign_resources_tbl(l_current_record).primary_contact_flag  :=
8196                                       l_assign_resources_tbl(l_current_record).primary_contact_flag;
8197               x_assign_resources_tbl(l_current_record).full_access_flag      :=
8198                                      l_assign_resources_tbl(l_current_record).full_access_flag;
8199               x_assign_resources_tbl(l_current_record).group_id              :=
8200                                      l_assign_resources_tbl(l_current_record).group_id;
8201             END IF;
8202 
8203           END IF; -- End of Calendar Flag
8204 
8205           l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
8206         END LOOP;
8207 
8208 
8209         IF ( p_calendar_flag = 'Y' AND
8210              x_assign_resources_tbl.count = 0 ) THEN
8211           fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
8212           fnd_msg_pub.add;
8213 --          RAISE fnd_api.g_exc_error;
8214         END IF;
8215 
8216       ELSE   -- No resources returned from the Territory API
8217         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
8218         fnd_msg_pub.add;
8219 --        RAISE fnd_api.g_exc_error;
8220       END IF;
8221 
8222     ELSE  -- Territory Flag is NO
8223       fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
8224       fnd_msg_pub.add;
8225 --      RAISE fnd_api.g_exc_error;
8226     END IF;
8227 
8228 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
8229 
8230       -- raise workfow event
8231       -- workflow test
8232       Begin
8233          jtf_assign_pub.g_assign_resources_tbl.delete;
8234          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
8235          jtf_am_wf_events_pub.assign_lead_resource
8236                     (P_API_VERSION           =>  1.0,
8237                      P_INIT_MSG_LIST         =>  'F',
8238                      P_COMMIT                =>  'F',
8239                      P_LEAD_REC              =>  p_lead_rec,
8240                      P_LEAD_BULK_REC	   =>  Null,
8241                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
8242                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
8243                      X_RETURN_STATUS         =>  l_wf_return_status,
8244                      X_MSG_COUNT             =>  l_wf_msg_count,
8245                      X_MSG_DATA              =>  l_wf_msg_data
8246                      );
8247 
8248 
8249         IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
8250             -- Unexpected Execution Error from call to assign_sr_resource
8251             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
8252             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
8253             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_LEAD_RESOURCES');
8254             fnd_msg_pub.add;
8255 
8256         ELSE
8257 		x_assign_resources_tbl.delete;
8258             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
8259         END IF;
8260 
8261 
8262       Exception
8263             When OTHERS Then
8264                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
8265                fnd_message.set_token('P_SQLCODE',SQLCODE);
8266                fnd_message.set_token('P_SQLERRM',SQLERRM);
8267                fnd_message.set_token('P_API_NAME',l_api_name);
8268                FND_MSG_PUB.add;
8269       End;
8270 
8271 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
8272 
8273     -- To Plugin the Workflow enabling the user
8274     -- to further filter the resources
8275 
8276     /* Getting the Workflow Profile value defined for the Assignment Manager */
8277 
8278     --l_workflow_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_WORKFLOW_NAME' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
8279     l_workflow_profile := FND_PROFILE.VALUE ( 'ACTIVATE_WORKFLOW_NAME' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
8280 
8281     SELECT jtf_calendars_s.NEXTVAL INTO l_workflow_key
8282     FROM   dual;
8283 
8284     IF (JTF_USR_HKS.ok_to_execute
8285           (
8286             'JTF_ASSIGN_PUB',
8287             'GET_ASSIGN_LEAD_RESOURCES',
8288             'W',
8289             'W'
8290           )
8291        ) THEN
8292 
8293       IF (JTF_ASSIGN_CUHK.ok_to_launch_workflow
8294             (
8295               p_api_version     => l_api_version,
8296               p_init_msg_list   => p_init_msg_list,
8297               x_return_status   => x_return_status,
8298               x_msg_count       => x_msg_count,
8299               x_msg_data        => x_msg_data
8300             )
8301          ) THEN
8302 
8303         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
8304           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
8305           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
8306           fnd_msg_pub.add;
8307           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
8308             RAISE fnd_api.g_exc_error;
8309           ELSE
8310             RAISE fnd_api.g_exc_unexpected_error;
8311           END IF;
8312         END IF;
8313 
8314         l_bind_data_id := JTF_USR_HKS.get_bind_data_id;
8315 
8316         JTF_USR_HKS.WrkFlowLaunch
8317           (
8318             'JTF_ASSIGN_WF',
8319             l_workflow_profile,
8320             'ASSIGN WF - '|| TO_CHAR(l_workflow_key),
8321             l_bind_data_id,
8322             l_return_code
8323           );
8324 
8325         JTF_USR_HKS.purge_bind_data
8326           (
8327             l_bind_data_id,
8328             'W'
8329           );
8330 
8331 
8332         IF (l_return_code = fnd_api.g_ret_sts_error) THEN
8333           -- Execution Error from call to Assignment Manager Workflow Hook
8334           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
8335           fnd_msg_pub.add;
8336           RAISE fnd_api.g_exc_error;
8337         ELSIF (l_return_code = fnd_api.g_ret_sts_unexp_error) THEN
8338           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
8339           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
8340           fnd_msg_pub.add;
8341           RAISE fnd_api.g_exc_unexpected_error;
8342         END IF;
8343 
8344       END IF;  -- End of JTF_ASSIGN_CUHK
8345 
8346     END IF;    -- End of JTF_USR_HKS
8347 
8348 
8349 
8350     /* Standard call to get message count and
8351        the message information */
8352 
8353     FND_MSG_PUB.Count_And_Get
8354     (
8355       p_count => x_msg_count,
8356       p_data  => x_msg_data
8357     );
8358 
8359 
8360 
8361   EXCEPTION
8362     WHEN FND_API.G_EXC_ERROR THEN
8363       x_return_status := FND_API.G_RET_STS_ERROR ;
8364       FND_MSG_PUB.Count_And_Get
8365       (
8366         p_count => x_msg_count,
8367         p_data  => x_msg_data
8368       );
8369 
8370     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8371       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8372       FND_MSG_PUB.Count_And_Get
8373       (
8374         p_count => x_msg_count,
8375         p_data  => x_msg_data
8376       );
8377 
8378     WHEN OTHERS THEN
8379       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
8380       fnd_message.set_token('P_SQLCODE',SQLCODE);
8381       fnd_message.set_token('P_SQLERRM',SQLERRM);
8382       fnd_message.set_token('P_API_NAME',l_api_name);
8383       FND_MSG_PUB.add;
8384 
8385       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8386       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
8387         FND_MSG_PUB.Add_Exc_Msg
8388         (
8389           G_PKG_NAME,
8390           l_api_name
8391         );
8392       END IF;
8393 
8394       FND_MSG_PUB.Count_And_Get
8395       (
8396         p_count => x_msg_count,
8397         p_data  => x_msg_data
8398        );
8399 
8400   END GET_ASSIGN_LEAD_RESOURCES;
8401 
8402 
8403 
8404 
8405 
8406 
8407 
8408 
8409 -- *******************************************************************************
8410 
8411 -- Start of comments
8412 
8413 --      API name        : GET_ASSIGN_ACCOUNT_RESOURCES
8414 --      Type            : Public
8415 --      Function        : Determine the resources based on the customer
8416 --                        preferences or territory preferences and the
8417 --                        availability. This is when the calling doc is
8418 --                        ACCOUNTS.
8419 --      Pre-reqs        : None
8420 
8421 --      Parameters      :
8422 
8423 --      IN              : p_api_version         IN      NUMBER  Required
8424 --                        p_init_msg_list       IN      VARCHAR2 Optional
8425 --                                              DEFAULT JTF_ASSIGN_PUB.AM_FALSE
8426 --                        p_commit              IN      VARCHAR2 optional
8427 --                                              DEFAULT JTF_ASSIGN_PUB.AM_FALSE
8428 
8429 --     Assignment Manager Specific Parameters
8430 
8431 --     This determines the Resource Type required by the
8432 --     calling document
8433 --     p_resource_type                          VARCHAR2(30)
8434 --     p_role                                   VARCHAR2(30)
8435 
8436 --     This determines the number of resources required
8437 --     by the calling document
8438 --     p_no_of_resources                        NUMBER,
8439 --     This is for sending out the qualified resource directly
8440 --     to the calling form. Defaulted to the PROFILE value
8441 --     p_auto_select_flag                       VARCHAR2(1)
8442 --                                              : value of  Y or N
8443 
8444 --     The Effort required is determined by these two parameters
8445 --     p_effort_duration                        NUMBER
8446 --     p_effort_uom                             VARCHAR2(10)
8447 
8448 --     The Dates in which the resource is required, is determined
8449 --     by these two parameters
8450 --     p_start_date                             DATE
8451 --     p_end_date                               DATE
8452 
8453 --     The Territory Manager is accessed based on the value set
8454 --     p_territory_flag                         VARCHAR2(1)
8455 --                                              : value of  Y or N
8456 
8457 --     The Resource Availability is checked based on the value set
8458 --     p_calendar_flag                          VARCHAR2(1)
8459 --                                              : value of  Y or N
8460 
8461 
8462 --     This parameter contains the values of the Qualifiers
8463 --     defined for the Accounts.
8464 --     p_account_rec                            JTF_ASSIGN_PUB.
8465 --                                              JTF_Account_rec_type
8466 --                                              REQUIRED
8467 
8468 --     OUT              : x_return_status        OUT     VARCHAR2(1)
8469 --                        x_msg_count            OUT     NUMBER
8470 --                        x_msg_data             OUT     VARCHAR2(2000)
8471 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
8472 --                                                       AssignResources_tbl_type
8473 
8474 
8475 --      Version         : Current version        1.0
8476 --                        Initial version        1.0
8477 --
8478 --      Notes           :
8479 --
8480 
8481 -- End of comments
8482 
8483 -- *********************************************************************************
8484 
8485 
8486 
8487 --      Procedure definition with the parameters when the
8488 --      Source Document is ACCOUNTS
8489 
8490 
8491   PROCEDURE GET_ASSIGN_ACCOUNT_RESOURCES
8492     (
8493         p_api_version                         IN  NUMBER,
8494         p_init_msg_list                       IN  VARCHAR2 ,
8495         p_resource_type                       IN  VARCHAR2 ,
8496         p_role                                IN  VARCHAR2 ,
8497         p_no_of_resources                     IN  NUMBER   ,
8498         p_auto_select_flag                    IN  VARCHAR2 ,
8499         p_effort_duration                     IN  NUMBER   ,
8500         p_effort_uom                          IN  VARCHAR2 ,
8501         p_start_date                          IN  DATE     ,
8502         p_end_date                            IN  DATE     ,
8503         p_territory_flag                      IN  VARCHAR2,
8504         p_calendar_flag                       IN  VARCHAR2 ,
8505         p_account_rec                         IN  JTF_ASSIGN_PUB.JTF_Account_rec_type,
8506         p_business_process_id                 IN  NUMBER,
8507         p_business_process_date               IN  DATE,
8508         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
8509         x_return_status                       OUT NOCOPY VARCHAR2,
8510         x_msg_count                           OUT NOCOPY NUMBER,
8511         x_msg_data                            OUT NOCOPY VARCHAR2
8512     )
8513   IS
8514 
8515     l_return_status_1                     VARCHAR2(10);
8516     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_ACCOUNT_RESOURCES';
8517     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_ACCOUNT_RESOURCES';
8518     l_api_version                         NUMBER        := 1.0;
8519     l_planned_start_date                  DATE;
8520     l_planned_end_date                    DATE;
8521     l_effort_duration                     NUMBER;
8522     l_effort_uom                          VARCHAR2(30);
8523     l_travel_time                         NUMBER        := 0;
8524     l_travel_uom                          VARCHAR2(30)  := 'HR';
8525 
8526     l_current_record                      INTEGER;
8527     l_total_records                       INTEGER;
8528 
8529     l_auto_select_profile                 VARCHAR2(03);
8530     l_auto_select_flag                    VARCHAR2(03);
8531     l_workflow_profile                    VARCHAR2(60);
8532 
8533     l_return_code                         VARCHAR2(60);
8534     l_bind_data_id                        NUMBER;
8535     l_workflow_key                        NUMBER;
8536 
8537     l_assign_resources_tbl                JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
8538     l_account_rec                         JTF_TERRITORY_PUB.JTF_Account_rec_type;
8539 
8540     l_x_planned_start_date                DATE;
8541     l_x_planned_end_date                  DATE;
8542     l_x_shift_construct_id                NUMBER;
8543     l_x_availability_type                 VARCHAR2(60);
8544 
8545 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
8546 
8547     l_wf_return_status                    VARCHAR2(60);
8548     l_wf_msg_count                        NUMBER;
8549     l_wf_msg_data                         VARCHAR2(2000);
8550 
8551 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
8552 
8553      --Bug# 4455803 MOAC.
8554      CURSOR check_date_cur(l_resource_id in number,
8555                           l_resource_type in varchar2)
8556         IS
8557      SELECT 'Y'
8558        --FROM  jtf_rs_all_resources_vl
8559        FROM  jtf_task_resources_vl
8560       where   resource_id = l_resource_id
8561         and   resource_type = l_resource_type
8562         and   nvl(trunc(end_date_active), trunc(sysdate)) >= trunc(sysdate);
8563 
8564     l_value varchar2(100);
8565 
8566     l_count        number := 0;
8567     l_temp_table   JTF_ASSIGN_PUB.AssignResources_tbl_type;
8568 
8569   BEGIN
8570 
8571     SAVEPOINT get_assign_account_resources;
8572 
8573     -- Started Assignment Manager API for ACCOUNTS
8574 
8575 
8576     /* Standard call to check for call compatibility */
8577 
8578     IF NOT fnd_api.compatible_api_call (l_api_version,
8579                                         p_api_version,
8580                                         l_api_name,
8581                                         g_pkg_name) THEN
8582       RAISE fnd_api.g_exc_unexpected_error;
8583     END IF;
8584 
8585 
8586     /* Initialize message list if p_init_msg_list is set to TRUE */
8587 
8588     IF fnd_api.to_boolean (p_init_msg_list) THEN
8589       fnd_msg_pub.initialize;
8590     END IF;
8591 
8592     x_return_status := fnd_api.g_ret_sts_success;
8593 
8594 
8595 
8596     /* Getting the Auto Select Profile value defined for the Assignment Manager */
8597 
8598     --l_auto_select_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_AUTO_SELECT' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
8599     l_auto_select_profile := FND_PROFILE.VALUE ( 'ACTIVATE_AUTO_SELECT' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
8600 
8601 
8602 
8603     /* Assigning the DEFAULT value to the Auto Select Parameter */
8604 
8605 
8606     IF (p_auto_select_flag IS NULL) THEN
8607       l_auto_select_flag  := l_auto_select_profile;
8608                              -- PROFILE VALUE is the default value
8609     ELSE
8610       l_auto_select_flag  := p_auto_select_flag;
8611     END IF;
8612 
8613 
8614 
8615 
8616     /* Defaulting the Calendar variable values to IN parameters,
8617        if the IN paramaters have values given */
8618 
8619 
8620     IF (p_start_date IS NOT NULL) THEN
8621       l_planned_start_date := p_start_date;
8622     ELSE
8623       l_planned_start_date := SYSDATE;
8624     END IF;
8625 
8626     IF (p_end_date IS NOT NULL) THEN
8627       l_planned_end_date := p_end_date;
8628     ELSE
8629       l_planned_end_date := SYSDATE;
8630     END IF;
8631 
8632     IF (p_effort_duration IS NOT NULL) THEN
8633       l_effort_duration  := p_effort_duration;
8634     END IF;
8635 
8636     IF (p_effort_uom IS NOT NULL) THEN
8637       l_effort_uom := p_effort_uom;
8638     END IF;
8639 
8640 
8641     /* Assigning values to the Account Record Type */
8642 
8643 
8644     l_account_rec.CITY                          :=  p_account_rec.CITY;
8645     l_account_rec.POSTAL_CODE                   :=  p_account_rec.POSTAL_CODE;
8646     l_account_rec.STATE                         :=  p_account_rec.STATE;
8647     l_account_rec.PROVINCE                      :=  p_account_rec.PROVINCE;
8648     l_account_rec.COUNTY                        :=  p_account_rec.COUNTY;
8649     l_account_rec.COUNTRY                       :=  p_account_rec.COUNTRY;
8650     l_account_rec.INTEREST_TYPE_ID              :=  p_account_rec.INTEREST_TYPE_ID;
8651     l_account_rec.PRIMARY_INTEREST_ID           :=  p_account_rec.PRIMARY_INTEREST_ID;
8652     l_account_rec.SECONDARY_INTEREST_ID         :=  p_account_rec.SECONDARY_INTEREST_ID;
8653     l_account_rec.CONTACT_INTEREST_TYPE_ID      :=  p_account_rec.CONTACT_INTEREST_TYPE_ID;
8654     l_account_rec.CONTACT_PRIMARY_INTEREST_ID   :=  p_account_rec.CONTACT_PRIMARY_INTEREST_ID;
8655     l_account_rec.CONTACT_SECONDARY_INTEREST_ID :=  p_account_rec.CONTACT_SECONDARY_INTEREST_ID;
8656     l_account_rec.PARTY_SITE_ID                 :=  p_account_rec.PARTY_SITE_ID;
8657     l_account_rec.AREA_CODE                     :=  p_account_rec.AREA_CODE;
8658     l_account_rec.PARTY_ID                      :=  p_account_rec.PARTY_ID;
8659     l_account_rec.COMP_NAME_RANGE               :=  p_account_rec.COMP_NAME_RANGE;
8660     l_account_rec.PARTNER_ID                    :=  p_account_rec.PARTNER_ID;
8661     l_account_rec.NUM_OF_EMPLOYEES              :=  p_account_rec.NUM_OF_EMPLOYEES;
8662     l_account_rec.CATEGORY_CODE                 :=  p_account_rec.CATEGORY_CODE;
8663     l_account_rec.PARTY_RELATIONSHIP_ID         :=  p_account_rec.PARTY_RELATIONSHIP_ID;
8664     l_account_rec.SIC_CODE                      :=  p_account_rec.SIC_CODE;
8665     l_account_rec.ATTRIBUTE1                    :=  p_account_rec.ATTRIBUTE1;
8666     l_account_rec.ATTRIBUTE2                    :=  p_account_rec.ATTRIBUTE2;
8667     l_account_rec.ATTRIBUTE3                    :=  p_account_rec.ATTRIBUTE3;
8668     l_account_rec.ATTRIBUTE4                    :=  p_account_rec.ATTRIBUTE4;
8669     l_account_rec.ATTRIBUTE5                    :=  p_account_rec.ATTRIBUTE5;
8670     l_account_rec.ATTRIBUTE6                    :=  p_account_rec.ATTRIBUTE6;
8671     l_account_rec.ATTRIBUTE7                    :=  p_account_rec.ATTRIBUTE7;
8672     l_account_rec.ATTRIBUTE8                    :=  p_account_rec.ATTRIBUTE8;
8673     l_account_rec.ATTRIBUTE9                    :=  p_account_rec.ATTRIBUTE9;
8674     l_account_rec.ATTRIBUTE10                   :=  p_account_rec.ATTRIBUTE10;
8675     l_account_rec.ATTRIBUTE11                   :=  p_account_rec.ATTRIBUTE11;
8676     l_account_rec.ATTRIBUTE12                   :=  p_account_rec.ATTRIBUTE12;
8677     l_account_rec.ATTRIBUTE13                   :=  p_account_rec.ATTRIBUTE13;
8678     l_account_rec.ATTRIBUTE14                   :=  p_account_rec.ATTRIBUTE14;
8679     l_account_rec.ATTRIBUTE15                   :=  p_account_rec.ATTRIBUTE15;
8680     l_account_rec.ORG_ID                        :=  p_account_rec.ORG_ID;
8681 
8682 
8683 
8684     /* Actual Flow of Assignment Manager */
8685 
8686 
8687     IF (p_territory_flag = 'Y') THEN
8688 
8689      -- change the API Name temporarily so that in case of unexpected error
8690      -- it is properly caught
8691      l_api_name := l_api_name||'-JTF_TERR_SALES_PUB';
8692 
8693       JTF_TERR_SALES_PUB.Get_WinningTerrMembers
8694         (
8695           p_api_version_number  => l_api_version,
8696           p_init_msg_list       => p_init_msg_list,
8697           p_TerrAccount_Rec     => l_account_rec,
8698           p_Resource_Type       => p_resource_type,
8699           p_Role                => p_role,
8700           x_return_status       => x_return_status,
8701           x_msg_count           => x_msg_count,
8702           x_msg_data            => x_msg_data,
8703           x_TerrResource_tbl    => l_assign_resources_tbl
8704         );
8705 
8706       -- set back the API name to original name
8707       l_api_name := l_api_name_1;
8708 
8709       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
8710         -- Unexpected Execution Error from call to Territory Manager
8711         fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
8712         fnd_msg_pub.add;
8713         IF (x_return_status = fnd_api.g_ret_sts_error) THEN
8714           RAISE fnd_api.g_exc_error;
8715         ELSE
8716           RAISE fnd_api.g_exc_unexpected_error;
8717         END IF;
8718       END IF;
8719 
8720 
8721        -- added 2 april 2003 by sudarsana to conver RS_SUPPLIER TO RS_SUPPLIER_CONTACT
8722 
8723       IF(l_assign_resources_tbl.COUNT > 0)
8724       THEN
8725              l_current_record := l_assign_resources_tbl.FIRST;
8726              WHILE l_current_record <= l_assign_resources_tbl.LAST
8727              LOOP
8728                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
8729                 THEN
8730                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
8731                 END IF;
8732                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
8733              END LOOP;
8734        END IF;
8735 
8736       IF l_assign_resources_tbl.COUNT > 0 THEN
8737 
8738         l_current_record := l_assign_resources_tbl.FIRST;
8739 
8740 
8741         IF (l_auto_select_flag = 'Y') THEN
8742           l_total_records := p_no_of_resources;
8743         ELSE
8744           l_total_records := l_assign_resources_tbl.LAST;
8745         END IF;
8746 
8747         -- removed the calendar check here. calling the procedure for calendar check in the next step
8748         -- calendar check removed 29th September 2003
8749 
8750         WHILE l_current_record <= l_assign_resources_tbl.LAST
8751         LOOP
8752           -- Check the calendar for resource availability
8753             -- Call Calendar API
8754             -- IF the resource is available then accept the values and
8755             -- check for the WORKFLOW profile option
8756             open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
8757                               l_assign_resources_tbl(l_current_record).resource_type);
8758             fetch check_date_cur into l_value;
8759             if (check_date_cur%found)
8760             then
8761 
8762                l_count := l_count + 1;
8763                x_assign_resources_tbl(l_count).terr_rsc_id           :=
8764                                      l_assign_resources_tbl(l_current_record).terr_rsc_id;
8765                x_assign_resources_tbl(l_count).resource_id           :=
8766                                       l_assign_resources_tbl(l_current_record).resource_id;
8767                x_assign_resources_tbl(l_count).resource_type         :=
8768                                      l_assign_resources_tbl(l_current_record).resource_type;
8769                x_assign_resources_tbl(l_count).role                  :=
8770                                      l_assign_resources_tbl(l_current_record).role;
8771                x_assign_resources_tbl(l_count).start_date            :=
8772                                      l_assign_resources_tbl(l_current_record).start_date;
8773                x_assign_resources_tbl(l_count).end_date              :=
8774                                      l_assign_resources_tbl(l_current_record).end_date;
8775                x_assign_resources_tbl(l_count).shift_construct_id    := NULL;
8776                x_assign_resources_tbl(l_count).terr_id               :=
8777                                      l_assign_resources_tbl(l_current_record).terr_id;
8778                x_assign_resources_tbl(l_count).terr_name             :=
8779                                      l_assign_resources_tbl(l_current_record).terr_name;
8780                x_assign_resources_tbl(l_count).primary_contact_flag  :=
8781                                       l_assign_resources_tbl(l_current_record).primary_contact_flag;
8782                x_assign_resources_tbl(l_count).full_access_flag      :=
8783                                      l_assign_resources_tbl(l_current_record).full_access_flag;
8784                x_assign_resources_tbl(l_count).group_id              :=
8785                                      l_assign_resources_tbl(l_current_record).group_id;
8786                x_assign_resources_tbl(l_count).primary_flag              :=
8787                                      l_assign_resources_tbl(l_current_record).primary_contact_flag;
8788                x_assign_resources_tbl(l_count).resource_source       := 'TERR';
8789 
8790               end if;
8791               close check_date_cur;
8792 
8793              l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
8794         END LOOP;
8795 
8796        -- added calendar call out
8797        -- this has been done as now the calendar check is done in a seperate procedure GET_AVAILABLE_RESOURCE
8798        -- changed on 29th September 2003
8799         IF (p_calendar_flag = 'Y') THEN
8800              -- change the API Name temporarily so that in case of unexpected error
8801              -- it is properly caught
8802               l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
8803               l_return_status_1 := x_return_status ;
8804               -- call the api to check resource availability
8805               get_available_resources
8806               (
8807                 p_init_msg_list                 =>  'F',
8808                 p_calendar_flag                 =>  p_calendar_flag,
8809                 p_effort_duration               =>  l_effort_duration,
8810                 p_effort_uom                    =>  l_effort_uom,
8811                 p_planned_start_date            =>  l_planned_start_date,
8812                 p_planned_end_date              =>  l_planned_end_date,
8813                 p_continuous_task               =>  jtf_assign_pub.g_continuous_work,
8814                 x_return_status                 =>  x_return_status,
8815                 x_msg_count                     =>  x_msg_count,
8816                 x_msg_data                      =>  x_msg_data,
8817                 x_assign_resources_tbl          =>  x_assign_resources_tbl);
8818 
8819               -- set back the API name to original name
8820                l_api_name := l_api_name_1;
8821 
8822                IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
8823               -- Unexpected Execution Error from call to Get_contracts_resources
8824                  fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
8825                  fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
8826                  fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_ACCOUNT_RESOURCES');
8827                  fnd_msg_pub.add;
8828                  IF (x_return_status = fnd_api.g_ret_sts_error) THEN
8829                   RAISE fnd_api.g_exc_error;
8830                  ELSE
8831                    RAISE fnd_api.g_exc_unexpected_error;
8832                  END IF;
8833               END IF; -- end of x_return_status check
8834             end if; -- if p_calendar_flag = Y
8835 
8836         IF ( p_calendar_flag = 'Y' AND
8837              x_assign_resources_tbl.count = 0 ) THEN
8838           fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
8839           fnd_msg_pub.add;
8840 --          RAISE fnd_api.g_exc_error;
8841         END IF;
8842 
8843        -- to implement auto selection
8844        IF(l_auto_select_flag = 'Y')
8845         THEN
8846           l_temp_table.delete;
8847           l_temp_table := x_assign_resources_tbl;
8848           x_assign_resources_tbl.delete;
8849           l_count := 0;
8850           l_current_record := l_temp_table.FIRST;
8851           l_total_records := p_no_of_resources;
8852 
8853           WHILE l_current_record <= l_temp_table.LAST
8854           LOOP
8855              If(l_count < l_total_records)
8856              THEN
8857                  x_assign_resources_tbl(l_count) := l_temp_table(l_current_record);
8858                  l_count := l_count + 1;
8859              end if; -- end of count check
8860              l_current_record := l_temp_table.NEXT(l_current_record);
8861           END LOOP; -- end of courrent record check
8862 
8863          END IF; -- end of auto select flag
8864       ELSE   -- No resources returned from the Territory API
8865         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
8866         fnd_msg_pub.add;
8867 --        RAISE fnd_api.g_exc_error;
8868       END IF;
8869 
8870     ELSE  -- Territory Flag is NO
8871       fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
8872       fnd_msg_pub.add;
8873 --      RAISE fnd_api.g_exc_error;
8874     END IF;
8875 
8876 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
8877 
8878       -- raise workfow event
8879       -- workflow test
8880       Begin
8881          jtf_assign_pub.g_assign_resources_tbl.delete;
8882          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
8883          jtf_am_wf_events_pub.assign_acc_resource
8884                     (P_API_VERSION           =>  1.0,
8885                      P_INIT_MSG_LIST         =>  'F',
8886                      P_COMMIT                =>  'F',
8887                      P_ACCOUNT_REC           =>  p_account_rec,
8888                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
8889                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
8890                      X_RETURN_STATUS         =>  l_wf_return_status,
8891                      X_MSG_COUNT             =>  l_wf_msg_count,
8892                      X_MSG_DATA              =>  l_wf_msg_data
8893                      );
8894 
8895 
8896         IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
8897             -- Unexpected Execution Error from call to assign_sr_resource
8898             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
8899             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
8900             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_ACCOUNT_RESOURCES');
8901             fnd_msg_pub.add;
8902 
8903         ELSE
8904 		x_assign_resources_tbl.delete;
8905             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
8906         END IF;
8907 
8908 
8909       Exception
8910             When OTHERS Then
8911                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
8912                fnd_message.set_token('P_SQLCODE',SQLCODE);
8913                fnd_message.set_token('P_SQLERRM',SQLERRM);
8914                fnd_message.set_token('P_API_NAME',l_api_name);
8915                FND_MSG_PUB.add;
8916       End;
8917 
8918 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
8919 
8920 
8921     -- To Plugin the Workflow enabling the user
8922     -- to further filter the resources
8923 
8924     /* Getting the Workflow Profile value defined for the Assignment Manager */
8925 
8926     --l_workflow_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_WORKFLOW_NAME' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
8927     l_workflow_profile := FND_PROFILE.VALUE ( 'ACTIVATE_WORKFLOW_NAME' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
8928 
8929     SELECT jtf_calendars_s.NEXTVAL INTO l_workflow_key
8930     FROM   dual;
8931 
8932     IF (JTF_USR_HKS.ok_to_execute
8933           (
8934             'JTF_ASSIGN_PUB',
8935             'GET_ASSIGN_ACCOUNT_RESOURCES',
8936             'W',
8937             'W'
8938           )
8939        ) THEN
8940 
8941       IF (JTF_ASSIGN_CUHK.ok_to_launch_workflow
8942             (
8943               p_api_version     => l_api_version,
8944               p_init_msg_list   => p_init_msg_list,
8945               x_return_status   => x_return_status,
8946               x_msg_count       => x_msg_count,
8947               x_msg_data        => x_msg_data
8948             )
8949          ) THEN
8950 
8951         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
8952           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
8953           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
8954           fnd_msg_pub.add;
8955           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
8956             RAISE fnd_api.g_exc_error;
8957           ELSE
8958             RAISE fnd_api.g_exc_unexpected_error;
8959           END IF;
8960         END IF;
8961 
8962         l_bind_data_id := JTF_USR_HKS.get_bind_data_id;
8963 
8964         JTF_USR_HKS.WrkFlowLaunch
8965           (
8966             'JTF_ASSIGN_WF',
8967             l_workflow_profile,
8968             'ASSIGN WF - '|| TO_CHAR(l_workflow_key),
8969             l_bind_data_id,
8970             l_return_code
8971           );
8972 
8973         JTF_USR_HKS.purge_bind_data
8974           (
8975             l_bind_data_id,
8976             'W'
8977           );
8978 
8979         IF (l_return_code = fnd_api.g_ret_sts_error) THEN
8980           -- Execution Error from call to Assignment Manager Workflow Hook
8981           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
8982           fnd_msg_pub.add;
8983           RAISE fnd_api.g_exc_error;
8984         ELSIF (l_return_code = fnd_api.g_ret_sts_unexp_error) THEN
8985           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
8986           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
8987           fnd_msg_pub.add;
8988           RAISE fnd_api.g_exc_unexpected_error;
8989         END IF;
8990 
8991       END IF;  -- End of JTF_ASSIGN_CUHK
8992 
8993     END IF;    -- End of JTF_USR_HKS
8994 
8995 
8996 
8997 
8998     /* Standard call to get message count and
8999        the message information */
9000 
9001     FND_MSG_PUB.Count_And_Get
9002     (
9003       p_count => x_msg_count,
9004       p_data  => x_msg_data
9005     );
9006 
9007 
9008 
9009   EXCEPTION
9010     WHEN FND_API.G_EXC_ERROR THEN
9011       x_return_status := FND_API.G_RET_STS_ERROR ;
9012       FND_MSG_PUB.Count_And_Get
9013       (
9014         p_count => x_msg_count,
9015         p_data  => x_msg_data
9016       );
9017 
9018     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9019       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9020       FND_MSG_PUB.Count_And_Get
9021       (
9022         p_count => x_msg_count,
9023         p_data  => x_msg_data
9024       );
9025 
9026     WHEN OTHERS THEN
9027       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
9028       fnd_message.set_token('P_SQLCODE',SQLCODE);
9029       fnd_message.set_token('P_SQLERRM',SQLERRM);
9030       fnd_message.set_token('P_API_NAME',l_api_name);
9031       FND_MSG_PUB.add;
9032 
9033       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9034       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
9035         FND_MSG_PUB.Add_Exc_Msg
9036         (
9037           G_PKG_NAME,
9038           l_api_name
9039         );
9040       END IF;
9041 
9042       FND_MSG_PUB.Count_And_Get
9043       (
9044         p_count => x_msg_count,
9045         p_data  => x_msg_data
9046       );
9047 
9048   END GET_ASSIGN_ACCOUNT_RESOURCES;
9049 
9050 
9051 
9052 
9053 
9054 
9055 
9056 
9057 
9058 
9059 
9060 
9061 -- *******************************************************************************
9062 
9063 -- Start of comments
9064 
9065 --      API name        : GET_ASSIGN_DEFECT_RESOURCES
9066 --      Type            : Public
9067 --      Function        : Determine the resources based on the customer
9068 --                        preferences or territory preferences and the
9069 --                        availability. This is when the calling doc is
9070 --                        DEFECT MANAGEMENT SYSTEM.
9071 --      Pre-reqs        : None
9072 
9073 --      Parameters      :
9074 
9075 --      IN              : p_api_version         IN      NUMBER  Required
9076 --                        p_init_msg_list       IN      VARCHAR2 Optional
9077 --                                              DEFAULT = FND_API.G_FALSE
9078 --     Assignment Manager Specific Parameters
9079 
9080 --     This determines the Resource Type required by the
9081 --     calling document
9082 --     p_resource_type                          VARCHAR2(30)
9083 --     p_role                                   VARCHAR2(30)
9084 
9085 --     This determines the number of resources required
9086 --     by the calling document
9087 --     p_no_of_resources                        NUMBER,
9088 
9089 --     This is for sending out the qualified resource directly
9090 --     to the calling form. Defaulted to the PROFILE value
9091 --     p_auto_select_flag                       VARCHAR2(1)
9092 --                                              : value of  Y or N
9093 
9094 --     The Effort required is determined by these two parameters
9095 --     p_effort_duration                        NUMBER
9096 --     p_effort_uom                             VARCHAR2(10)
9097 
9098 --     The Dates in which the resource is required, is determined
9099 --     by these two parameters
9100 --     p_start_date                             DATE
9101 --     p_end_date                               DATE
9102 
9103 --     The Territory Manager is accessed based on the value set
9104 --     p_territory_flag                         VARCHAR2(1)
9105 --                                              : value of  Y or N
9106 
9107 --     The Resource Availability is checked based on the value set
9108 --     p_calendar_flag                          VARCHAR2(1)
9109 --                                              : value of  Y or N
9110 
9111 
9112 --     This parameter contains the Qualifier Values
9113 --     of the Calling Document
9114 --     p_defect_rec                             JTF_TERRITORY_PUB.
9115 --                                              JTF_DEF_MGMT_rec_type
9116 
9117 
9118 --     OUT             : x_return_status        OUT     VARCHAR2(1)
9119 --                       x_msg_count            OUT     NUMBER
9120 --                       x_msg_data             OUT     VARCHAR2(2000)
9121 --                       x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
9122 --                                                      AssignResources_tbl_type
9123 
9124 
9125 --      Version        : Current version        1.0
9126 --                       Initial version        1.0
9127 --
9128 --      Notes          :
9129 --
9130 
9131 -- End of comments
9132 -- *********************************************************************************
9133 
9134 
9135 
9136 --      Procedure definition with the parameters when the
9137 --      Source Document is DEFECT MANAGEMENT SYSTEM
9138 
9139   PROCEDURE GET_ASSIGN_DEFECT_RESOURCES
9140     (
9141         p_api_version                         IN  NUMBER,
9142         p_init_msg_list                       IN  VARCHAR2,
9143         p_resource_type                       IN  VARCHAR2,
9144         p_role                                IN  VARCHAR2,
9145         p_no_of_resources                     IN  NUMBER,
9146         p_auto_select_flag                    IN  VARCHAR2,
9147         p_effort_duration                     IN  NUMBER,
9148         p_effort_uom                          IN  VARCHAR2,
9149         p_start_date                          IN  DATE,
9150         p_end_date                            IN  DATE,
9151         p_territory_flag                      IN  VARCHAR2,
9152         p_calendar_flag                       IN  VARCHAR2,
9153         p_defect_rec                          IN  JTF_TERRITORY_PUB.JTF_DEF_MGMT_rec_type,
9154         p_business_process_id                 IN  NUMBER,
9155         p_business_process_date               IN  DATE,
9156         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
9157         x_return_status                       OUT NOCOPY VARCHAR2,
9158         x_msg_count                           OUT NOCOPY NUMBER,
9159         x_msg_data                            OUT NOCOPY VARCHAR2
9160     )
9161   IS
9162     l_return_status_1                     VARCHAR2(10);
9163     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_DEFECT_RESOURCES';
9164     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_DEFECT_RESOURCES';
9165     l_api_version                         NUMBER        := 1.0;
9166     l_planned_start_date                  DATE;
9167     l_planned_end_date                    DATE;
9168     l_travel_time                         NUMBER        := 0;
9169     l_travel_uom                          VARCHAR2(30)  := 'HR';
9170 
9171     l_current_record                      INTEGER;
9172     l_total_records                       INTEGER;
9173 
9174     l_assign_resources_tbl                JTF_TERRITORY_PUB.WinningTerrMember_tbl_type;
9175 --  l_assign_resources_rec                JTF_TERRITORY_PUB.JTF_DEF_MGMT_rec_type;
9176 
9177     l_x_planned_start_date                DATE;
9178     l_x_planned_end_date                  DATE;
9179     l_x_shift_construct_id                NUMBER;
9180     l_x_availability_type                 VARCHAR2(60);
9181 
9182     l_count                               NUMBER := 1;
9183 
9184 
9185 
9186   BEGIN
9187     SAVEPOINT get_assign_defect_resources;
9188 
9189     -- Started Assignment Manager API for DEFECT MANAGEMENT SYSTEM
9190 
9191 
9192 
9193     /* Standard call to check for call compatibility */
9194 
9195     IF NOT fnd_api.compatible_api_call (l_api_version,
9196                                         p_api_version,
9197                                         l_api_name,
9198                                         g_pkg_name) THEN
9199       RAISE fnd_api.g_exc_unexpected_error;
9200     END IF;
9201 
9202 
9203 
9204     /* Initialize message list if p_init_msg_list is set to TRUE */
9205 
9206     IF fnd_api.to_boolean (p_init_msg_list) THEN
9207       fnd_msg_pub.initialize;
9208     END IF;
9209 
9210     x_return_status := fnd_api.g_ret_sts_success;
9211 
9212 
9213 
9214 
9215     /* Defaulting the variable values to IN parameters,
9216        if the IN paramaters have values given */
9217 
9218 
9219     IF (p_start_date IS NOT NULL) THEN
9220       l_planned_start_date := p_start_date;
9221     ELSE
9222       l_planned_start_date := SYSDATE;
9223     END IF;
9224 
9225     IF (p_end_date IS NOT NULL) THEN
9226       l_planned_end_date := p_end_date;
9227     ELSE
9228       l_planned_end_date := SYSDATE;
9229     END IF;
9230 
9231 
9232 
9233 
9234     /* Actual Flow of Assignment Manager */
9235 
9236 
9237     IF (p_territory_flag = 'Y') THEN
9238 
9239       -- change the API Name temporarily so that in case of unexpected error
9240       -- it is properly caught
9241       l_api_name := l_api_name||'-JTF_TERR_DEF_MGMT_PUB';
9242 
9243       JTF_TERR_DEF_MGMT_PUB.Get_WinningTerrMembers
9244         (
9245           p_api_version_number  => l_api_version,
9246           p_init_msg_list       => p_init_msg_list,
9247           p_TerrDefMgmt_Rec     => p_defect_rec,
9248           p_Resource_Type       => p_resource_type,
9249           p_Role                => p_role,
9250           x_return_status       => x_return_status,
9251           x_msg_count           => x_msg_count,
9252           x_msg_data            => x_msg_data,
9253           x_TerrResource_tbl    => l_assign_resources_tbl
9254         );
9255 
9256         -- set back the API name to original name
9257         l_api_name := l_api_name_1;
9258 
9259       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
9260         -- Unexpected Execution Error from call to Territory Manager
9261         fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
9262         fnd_msg_pub.add;
9263         IF (x_return_status = fnd_api.g_ret_sts_error) THEN
9264           RAISE fnd_api.g_exc_error;
9265         ELSE
9266           RAISE fnd_api.g_exc_unexpected_error;
9267         END IF;
9268       END IF;
9269 
9270     ELSE  -- Territory Flag is NO
9271       fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
9272       fnd_msg_pub.add;
9273 --      RAISE fnd_api.g_exc_error;
9274     END IF;
9275 
9276      -- added 2 april 2003 by sudarsana to conver RS_SUPPLIER TO RS_SUPPLIER_CONTACT
9277 
9278       IF(l_assign_resources_tbl.COUNT > 0)
9279       THEN
9280              l_current_record := l_assign_resources_tbl.FIRST;
9281              WHILE l_current_record <= l_assign_resources_tbl.LAST
9282              LOOP
9283                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
9284                 THEN
9285                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
9286                 END IF;
9287                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
9288              END LOOP;
9289       END IF;
9290 
9291     IF l_assign_resources_tbl.COUNT > 0 THEN
9292 
9293 
9294       x_assign_resources_tbl.delete;
9295 
9296       l_current_record := l_assign_resources_tbl.FIRST;
9297 
9298       IF (p_auto_select_flag = 'Y') THEN
9299         l_total_records := p_no_of_resources;
9300       ELSE
9301         l_total_records := l_assign_resources_tbl.LAST;
9302       END IF;
9303 
9304       -- added processing with l_count to fix bug for Defects 2490634
9305       -- on 6th aug 2002
9306 
9307       WHILE l_current_record <= l_assign_resources_tbl.LAST
9308       LOOP
9309 
9310         -- removed the calendar check here. calling the procedure for calendar check in the next step
9311         -- calendar check removed 29th September 2003
9312         -- removed the autoselect check. This will be done in the GET_ASSIGN_RESOURCES api
9313         --IF (l_count <= l_total_records) THEN
9314             x_assign_resources_tbl(l_count).terr_rsc_id           :=
9315                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
9316             x_assign_resources_tbl(l_count).resource_id           :=
9317                                    l_assign_resources_tbl(l_current_record).resource_id;
9318             x_assign_resources_tbl(l_count).resource_type         :=
9319                                    l_assign_resources_tbl(l_current_record).resource_type;
9320 
9321             x_assign_resources_tbl(l_count).start_date            :=
9322                                    l_assign_resources_tbl(l_current_record).start_date;
9323             x_assign_resources_tbl(l_count).end_date              :=
9324                                    l_assign_resources_tbl(l_current_record).end_date;
9325             x_assign_resources_tbl(l_count).shift_construct_id    := NULL;
9326 
9327             x_assign_resources_tbl(l_count).role                  :=
9328                                    l_assign_resources_tbl(l_current_record).role;
9329             x_assign_resources_tbl(l_count).primary_contact_flag  :=
9330                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
9331 
9332             x_assign_resources_tbl(l_count).terr_id               :=
9333                                    l_assign_resources_tbl(l_current_record).terr_id;
9334             x_assign_resources_tbl(l_count).terr_name             :=
9335                                    l_assign_resources_tbl(l_current_record).terr_name;
9336             x_assign_resources_tbl(l_count).terr_rank             :=
9337                                    l_assign_resources_tbl(l_current_record).absolute_rank;
9338             x_assign_resources_tbl(l_count).primary_flag             :=
9339                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
9340             x_assign_resources_tbl(l_count).resource_source       := 'TERR';
9341 
9342             l_count := l_count + 1;
9343           --END IF;
9344           l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
9345       END LOOP;
9346 
9347       -- added calendar call out
9348       -- this has been done as now the calendar check is done in a seperate procedure GET_AVAILABLE_RESOURCE
9349       -- changed on 29th September 2003
9350       IF (p_calendar_flag = 'Y') THEN
9351           -- change the API Name temporarily so that in case of unexpected error
9352           -- it is properly caught
9353           l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
9354           l_return_status_1 := x_return_status ;
9355            -- call the api to check resource availability
9356            get_available_resources
9357             (
9358               p_init_msg_list                 =>  'F',
9359               p_calendar_flag                 =>   p_calendar_flag,
9360               p_effort_duration               =>  p_effort_duration,
9361               p_effort_uom                    =>  p_effort_uom,
9362               p_planned_start_date            =>  l_planned_start_date,
9363               p_planned_end_date              =>  l_planned_end_date,
9364               p_continuous_task               =>   jtf_assign_pub.g_continuous_work,
9365               x_return_status                 =>   x_return_status,
9366               x_msg_count                     =>   x_msg_count,
9367               x_msg_data                      =>   x_msg_data,
9368               x_assign_resources_tbl          =>   x_assign_resources_tbl);
9369 
9370           -- set back the API name to original name
9371           l_api_name := l_api_name_1;
9372 
9373             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
9374             -- Unexpected Execution Error from call to Get_contracts_resources
9375                fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
9376                fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
9377                fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_DEFECT_RESOURCES');
9378                fnd_msg_pub.add;
9379                IF (x_return_status = fnd_api.g_ret_sts_error) THEN
9380                   RAISE fnd_api.g_exc_error;
9381                ELSE
9382                   RAISE fnd_api.g_exc_unexpected_error;
9383                END IF;
9384              END IF; -- end of x_return_status check
9385       end if; -- if p_calendar_flag = Y
9386 
9387 
9388       IF ( p_calendar_flag = 'Y' AND
9389            x_assign_resources_tbl.count = 0 ) THEN
9390         fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
9391         fnd_msg_pub.add;
9392 --        RAISE fnd_api.g_exc_error;
9393       END IF;
9394 
9395     ELSE   -- No resources returned from the Territory API
9396       fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
9397       fnd_msg_pub.add;
9398 --      RAISE fnd_api.g_exc_error;
9399     END IF;
9400 
9401 
9402     /* Standard call to get message count and
9403        the message information */
9404 
9405     FND_MSG_PUB.Count_And_Get
9406     (
9407       p_count => x_msg_count,
9408       p_data  => x_msg_data
9409     );
9410 
9411 
9412   EXCEPTION
9413     WHEN FND_API.G_EXC_ERROR THEN
9414       x_return_status := FND_API.G_RET_STS_ERROR ;
9415       FND_MSG_PUB.Count_And_Get
9416       (
9417         p_count => x_msg_count,
9418         p_data  => x_msg_data
9419       );
9420 
9421     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9422       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9423       FND_MSG_PUB.Count_And_Get
9424       (
9425         p_count => x_msg_count,
9426         p_data  => x_msg_data
9427       );
9428 
9429     WHEN OTHERS THEN
9430       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
9431       fnd_message.set_token('P_SQLCODE',SQLCODE);
9432       fnd_message.set_token('P_SQLERRM',SQLERRM);
9433       fnd_message.set_token('P_API_NAME',l_api_name);
9434       FND_MSG_PUB.add;
9435 
9436       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9437       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
9438         FND_MSG_PUB.Add_Exc_Msg
9439         (
9440           G_PKG_NAME,
9441           l_api_name
9442         );
9443       END IF;
9444 
9445       FND_MSG_PUB.Count_And_Get
9446       (
9447         p_count => x_msg_count,
9448         p_data  => x_msg_data
9449        );
9450 
9451   END GET_ASSIGN_DEFECT_RESOURCES;
9452 
9453 
9454 
9455 
9456 
9457 
9458 -- *******************************************************************************
9459 
9460 -- Start of comments
9461 
9462 --      API name        : GET_ASSIGN_ESC_RESOURCES
9463 --      Type            : Public
9464 --      Function        : Determine the resources based on the
9465 --                        territory preferences and the availability.
9466 --                        This is when the calling doc is ESCALATIONS.
9467 --      Pre-reqs        : None
9468 
9469 --      Parameters      :
9470 
9471 --      IN              : p_api_version         IN      NUMBER  Required
9472 --                        p_init_msg_list       IN      VARCHAR2 Optional
9473 --                        p_commit              IN      VARCHAR2 optional
9474 
9475 --     Assignment Manager Specific Parameters
9476 
9477 --     This determines the Resource Type required by the
9478 --     calling document
9479 --     p_resource_type                          VARCHAR2(30)
9480 --     p_role                                   VARCHAR2(30)
9481 
9482 --     This determines the number of resources required
9483 --     by the calling document
9484 --     p_no_of_resources                        NUMBER,
9485 
9486 --     This is for sending out the qualified resource directly
9487 --     to the calling form. Defaulted to the PROFILE value
9488 --     p_auto_select_flag                       VARCHAR2(1)
9489 --                                              : value of  Y or N
9490 
9491 --     The Effort required is determined by these two parameters
9492 --     p_effort_duration                        NUMBER
9493 --     p_effort_uom                             VARCHAR2(10)
9494 
9495 --     The Dates in which the resource is required, is determined
9496 --     by these two parameters
9497 --     p_start_date                             DATE
9498 --     p_end_date                               DATE
9499 
9500 --     The Territory Manager is accessed based on the value set
9501 --     p_territory_flag                         VARCHAR2(1)
9502 --                                              : value of  Y or N
9503 
9504 --     The Resource Availability is checked based on the value set
9505 --     p_calendar_flag                          VARCHAR2(1)
9506 --                                              : value of  Y or N
9507 
9508 
9509 --     This parameter contains the values of the Qualifiers
9510 --     defined for the Escalations.
9511 --     p_esc_tbl                                JTF_ASSIGN_PUB.
9512 --                                              Escalations_tbl_type
9513 --                                              REQUIRED
9514 
9515 
9516 --     OUT              : x_return_status        OUT     VARCHAR2(1)
9517 --                        x_msg_count            OUT     NUMBER
9518 --                        x_msg_data             OUT     VARCHAR2(2000)
9519 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
9520 --                                                       AssignResources_tbl_type
9521 
9522 
9523 --      Version         : Current version        1.0
9524 --                        Initial version        1.0
9525 --
9526 --      Notes           :
9527 --
9528 
9529 -- End of comments
9530 
9531 -- *********************************************************************************
9532 
9533 
9534   /* Procedure Body with the parameters when the
9535      Source Document is ESCALATIONS */
9536 
9537 
9538   PROCEDURE GET_ASSIGN_ESC_RESOURCES
9539     (
9540         p_api_version                         IN  NUMBER,
9541         p_init_msg_list                       IN  VARCHAR2 ,
9542         p_resource_type                       IN  VARCHAR2 ,
9543         p_role                                IN  VARCHAR2 ,
9544         p_no_of_resources                     IN  NUMBER   ,
9545         p_auto_select_flag                    IN  VARCHAR2 ,
9546         p_effort_duration                     IN  NUMBER   ,
9547         p_effort_uom                          IN  VARCHAR2 ,
9548         p_start_date                          IN  DATE     ,
9549         p_end_date                            IN  DATE     ,
9550         p_territory_flag                      IN  VARCHAR2 ,
9551         p_calendar_flag                       IN  VARCHAR2 ,
9552         p_web_availability_flag               IN  VARCHAR2 ,
9553         p_esc_tbl                             IN  JTF_ASSIGN_PUB.Escalations_tbl_type,
9554         p_business_process_id                 IN  NUMBER,
9555         p_business_process_date               IN  DATE,
9556         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
9557         x_return_status                       OUT NOCOPY VARCHAR2,
9558         x_msg_count                           OUT NOCOPY NUMBER,
9559         x_msg_data                            OUT NOCOPY VARCHAR2
9560     )
9561     IS
9562 
9563     l_return_status_1                     VARCHAR2(10);
9564     l_api_name                            VARCHAR2(100)  := 'GET_ASSIGN_ESC_RESOURCES';
9565     l_api_name_1                          VARCHAR2(60)  := 'GET_ASSIGN_ESC_RESOURCES';
9566     l_api_version                         NUMBER        := 1.0;
9567     l_planned_start_date                  DATE;
9568     l_planned_end_date                    DATE;
9569     l_effort_duration                     NUMBER;
9570     l_effort_uom                          VARCHAR2(30);
9571 
9572     l_esc_source_code                     JTF_TASKS_VL.SOURCE_OBJECT_TYPE_CODE%TYPE;
9573     l_esc_source_id                       JTF_TASKS_VL.SOURCE_OBJECT_ID%TYPE;
9574     l_reference_code                      JTF_TASK_REFERENCES_VL.REFERENCE_CODE%TYPE;
9575 
9576     l_territory_id                        NUMBER;
9577     l_esc_territory_id                    NUMBER;
9578 
9579     l_object_type_code                    JTF_TASK_REFERENCES_VL.OBJECT_TYPE_CODE%TYPE;
9580     l_object_id                           JTF_TASK_REFERENCES_VL.OBJECT_ID%TYPE;
9581 
9582     l_travel_time                         NUMBER       := 0;
9583     l_travel_uom                          VARCHAR2(30) := 'HR';
9584 
9585     l_current_record                      INTEGER;
9586     l_current_esc_record                  INTEGER DEFAULT 0;
9587     l_esc_record                          INTEGER;
9588     l_total_records                       INTEGER;
9589 
9590     l_auto_select_profile                 VARCHAR2(03);
9591     l_auto_select_flag                    VARCHAR2(03);
9592     l_workflow_profile                    VARCHAR2(60);
9593 
9594     l_return_code                         VARCHAR2(60);
9595     l_bind_data_id                        NUMBER;
9596     l_workflow_key                        NUMBER;
9597 
9598     l_dynamic_sql                         VARCHAR2(2000);
9599     l_dynamic_sql1                        VARCHAR2(2000);
9600 
9601     l_assign_resources_tbl                JTF_TERRITORY_GET_PUB.QualifyingRsc_out_tbl_type;
9602 
9603     l_x_planned_start_date                DATE;
9604     l_x_planned_end_date                  DATE;
9605     l_x_shift_construct_id                NUMBER;
9606     l_x_availability_type                 VARCHAR2(60);
9607 
9608 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
9609 
9610     l_wf_return_status                    VARCHAR2(60);
9611     l_wf_msg_count                        NUMBER;
9612     l_wf_msg_data                         VARCHAR2(2000);
9613     l_esc_count					NUMBER;
9614 
9615 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
9616 
9617 
9618     /*
9619     CURSOR cur_esc_id IS
9620       SELECT source_object_type_code,
9621              source_object_id,
9622              planned_start_date,
9623              planned_end_date,
9624              planned_effort,
9625              planned_effort_uom
9626       FROM   jtf_tasks_vl
9627       WHERE  task_id = l_esc_id;
9628     l_cur_esc_id cur_esc_id%ROWTYPE;
9629 
9630 
9631     CURSOR cur_reference_code IS
9632       SELECT reference_code,
9633              object_type_code,
9634              object_id
9635       FROM   jtf_task_references_vl
9636       WHERE  task_id = l_esc_id;
9637     l_cur_reference_code cur_reference_code%ROWTYPE;
9638     */
9639 
9640 
9641     CURSOR cur_source_task IS
9642       SELECT owner_territory_id
9643       FROM   jtf_tasks_vl
9644       WHERE  task_id = l_object_id;
9645 
9646 
9647     TYPE DYNAMIC_CUR_TYP IS REF CURSOR;
9648 
9649     cur_source_sr               DYNAMIC_CUR_TYP;
9650     cur_source_defect           DYNAMIC_CUR_TYP;
9651 
9652     cur_support_site_name       DYNAMIC_CUR_TYP;
9653 
9654     l_support_site              VARCHAR2(15) := 'SUPPORT_SITE';
9655     l_rsc_type                  VARCHAR2(30);
9656     l_rsc_id                    NUMBER;
9657     l_web_availability_flag     VARCHAR2(1)   := p_web_availability_flag;
9658 
9659 
9660     CURSOR cur_support_site_id (p_rsc_id NUMBER, p_rsc_type VARCHAR2) IS
9661       SELECT support_site_id
9662       FROM   jtf_rs_resource_extns_vl
9663       WHERE  resource_id = p_rsc_id AND
9664              category    = p_rsc_type;
9665 
9666 
9667     CURSOR cur_web_availability (p_res_id NUMBER, p_res_type VARCHAR2) IS
9668       SELECT resource_id
9669       FROM   jtf_rs_web_available_v
9670       WHERE  resource_id = p_res_id AND
9671              category    = p_res_type;
9672 
9673    --Bug# 4455803 MOAC.
9674    CURSOR check_date_cur(l_resource_id in number,
9675                           l_resource_type in varchar2)
9676         IS
9677      SELECT 'Y'
9678        --FROM  jtf_rs_all_resources_vl
9679        FROM  jtf_task_resources_vl
9680       where   resource_id = l_resource_id
9681         and   resource_type = l_resource_type
9682         and   nvl(trunc(end_date_active), trunc(sysdate)) >= trunc(sysdate);
9683 
9684     l_value varchar2(100);
9685 
9686     l_count number := 0;
9687     l_temp_table    JTF_ASSIGN_PUB.AssignResources_tbl_type;
9688 
9689   BEGIN
9690 
9691     SAVEPOINT get_assign_esc_resources;
9692 
9693     -- Started Assignment Manager API for ESCALATIONS
9694 
9695 
9696     /* Standard call to check for call compatibility */
9697 
9698     IF NOT fnd_api.compatible_api_call (l_api_version,
9699                                         p_api_version,
9700                                         l_api_name,
9701                                         g_pkg_name) THEN
9702       RAISE fnd_api.g_exc_unexpected_error;
9703     END IF;
9704 
9705 
9706     /* Initialize message list if p_init_msg_list is set to TRUE */
9707 
9708     IF fnd_api.to_boolean (p_init_msg_list) THEN
9709       fnd_msg_pub.initialize;
9710     END IF;
9711 
9712     x_return_status := fnd_api.g_ret_sts_success;
9713 
9714 
9715 
9716     /* Get TASK source and the related information */
9717 
9718     /*
9719     OPEN  cur_esc_id;
9720     FETCH cur_esc_id INTO l_cur_esc_id;
9721     IF  ( cur_esc_id%NOTFOUND ) THEN
9722       fnd_message.set_name('JTF', 'JTF_AM_INVALID_ESC_ID');
9723       fnd_msg_pub.add;
9724       RAISE fnd_api.g_exc_error;
9725     ELSE
9726       l_esc_source_code     := l_cur_esc_id.source_object_type_code;
9727       l_esc_source_id       := l_cur_esc_id.source_object_id;
9728       l_planned_start_date  := l_cur_esc_id.planned_start_date;
9729       l_planned_end_date    := l_cur_esc_id.planned_end_date;
9730       l_effort_duration     := l_cur_esc_id.planned_effort;
9731       l_effort_uom          := l_cur_esc_id.planned_effort_uom;
9732     END IF;
9733     CLOSE cur_esc_id;
9734 
9735 
9736     IF  ( l_esc_source_code <> 'ESC' ) THEN
9737       fnd_message.set_name('JTF', 'JTF_AM_INVALID_ESC_ID');
9738       fnd_msg_pub.add;
9739       RAISE fnd_api.g_exc_error;
9740     ELSE
9741       OPEN  cur_reference_code;
9742       FETCH cur_reference_code INTO l_cur_reference_code;
9743       IF (  cur_reference_code%NOTFOUND) THEN
9744         fnd_message.set_name('JTF', 'JTF_AM_INVALID_ESC_ID');
9745         fnd_msg_pub.add;
9746         RAISE fnd_api.g_exc_error;
9747       ELSE
9748         l_reference_code   := l_cur_reference_code.reference_code;
9749         l_object_type_code := l_cur_reference_code.object_type_code;
9750         l_object_id        := l_cur_reference_code.object_id;
9751 
9752         IF ( l_reference_code <> 'ESC' ) THEN
9753           fnd_message.set_name('JTF', 'JTF_AM_INVALID_ESC_REF');
9754           fnd_msg_pub.add;
9755           RAISE fnd_api.g_exc_error;
9756         END IF;
9757       END IF;
9758       CLOSE cur_reference_code;
9759     END IF;
9760 
9761     */
9762 
9763 
9764     /* Getting the Auto Select Profile value defined for the Assignment Manager */
9765 
9766     --l_auto_select_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_AUTO_SELECT' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
9767     l_auto_select_profile := FND_PROFILE.VALUE ( 'ACTIVATE_AUTO_SELECT' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
9768 
9769 
9770 
9771     /* Assigning the DEFAULT value to the Auto Select Parameter */
9772 
9773 
9774     IF (p_auto_select_flag IS NULL) THEN
9775       l_auto_select_flag  := l_auto_select_profile;
9776                              -- PROFILE VALUE is the default value
9777     ELSE
9778       l_auto_select_flag  := p_auto_select_flag;
9779     END IF;
9780 
9781 
9782     IF ( p_web_availability_flag IS NULL) THEN
9783       l_web_availability_flag  := 'Y';
9784     ELSE
9785       l_web_availability_flag  := p_web_availability_flag;
9786     END IF;
9787 
9788 
9789     /* Defaulting the values for variables to pass to the
9790        Calendar, to ensure that the resource is working */
9791 
9792     l_planned_start_date  := SYSDATE;
9793     l_planned_end_date    := SYSDATE;
9794     l_effort_duration     := NULL;
9795     l_effort_uom          := NULL;
9796 
9797 
9798     /* Defaulting the variable values to IN parameters,
9799        if the IN paramaters have values given */
9800 
9801     IF (p_start_date IS NOT NULL) THEN
9802       l_planned_start_date := p_start_date;
9803     END IF;
9804 
9805     IF (p_end_date IS NOT NULL) THEN
9806       l_planned_end_date := p_end_date;
9807     END IF;
9808 
9809     IF (p_effort_duration IS NOT NULL) THEN
9810       l_effort_duration := p_effort_duration;
9811     END IF;
9812 
9813     IF (p_effort_uom IS NOT NULL) THEN
9814       l_effort_uom := p_effort_uom;
9815     END IF;
9816 
9817 
9818 
9819     /* Actual Flow of Assignment Manager */
9820 
9821 
9822     IF ( p_esc_tbl.count > 0 ) THEN
9823 
9824       l_esc_record := p_esc_tbl.FIRST;
9825 
9826       WHILE (l_esc_record <= p_esc_tbl.LAST)
9827       LOOP
9828 
9829         l_object_type_code := p_esc_tbl(l_esc_record).source_object_type;
9830         l_object_id        := p_esc_tbl(l_esc_record).source_object_id;
9831 
9832 
9833         IF (l_object_type_code = 'TASK') THEN
9834 
9835           /* Since the Object Code is TASK, OPEN the appropriate cursor
9836              to get the Territory ID to pass it to the Territory API */
9837 
9838           OPEN  cur_source_task;
9839           FETCH cur_source_task INTO l_territory_id;
9840 
9841           IF  ( cur_source_task%NOTFOUND ) THEN
9842             fnd_message.set_name('JTF', 'JTF_AM_INVALID_TASK_ID');
9843             fnd_msg_pub.add;
9844             RAISE fnd_api.g_exc_error;
9845           END IF;
9846 
9847           CLOSE cur_source_task;
9848 
9849         ELSIF (l_object_type_code = 'SR') THEN
9850 
9851           /* Since the Object Code is SERVICE REQUEST, OPEN the appropriate
9852              cursor to get the Territory ID to pass it to the Territory API */
9853 
9854           l_dynamic_sql  :=  ' SELECT territory_id'||
9855                              ' FROM   cs_incidents_all_vl'||
9856                              ' WHERE  incident_id = :1';
9857 
9858 
9859           OPEN  cur_source_sr FOR l_dynamic_sql USING l_object_id;
9860           FETCH cur_source_sr INTO l_territory_id;
9861 
9862           IF  ( cur_source_sr%NOTFOUND ) THEN
9863             fnd_message.set_name('JTF', 'JTF_AM_INVALID_SR_ID');
9864             fnd_msg_pub.add;
9865             RAISE fnd_api.g_exc_error;
9866           END IF;
9867 
9868           CLOSE cur_source_sr;
9869 
9870 
9871         ELSIF (l_object_type_code = 'DF') THEN
9872 
9873           /* Since the Object Code is DEFECTS, OPEN the appropriate cursor
9874              to get the Territory ID to pass it to the Territory API */
9875 
9876 
9877           l_dynamic_sql  :=  ' SELECT territory_id'||
9878                              ' FROM   css_def_defects_all'||
9879                              ' WHERE  defect_id = :1';
9880 
9881           -- dbms_output.put_line('Select is : '||l_dynamic_sql);
9882 
9883           OPEN  cur_source_defect FOR l_dynamic_sql USING l_object_id;
9884           FETCH cur_source_defect INTO l_territory_id;
9885 
9886           IF  ( cur_source_defect%NOTFOUND ) THEN
9887             fnd_message.set_name('JTF', 'JTF_AM_INVALID_DEFECT_ID');
9888             fnd_msg_pub.add;
9889             RAISE fnd_api.g_exc_error;
9890           END IF;
9891 
9892           CLOSE cur_source_defect;
9893 
9894 
9895         END IF; -- End of l_object_type_code = 'TASK'
9896 
9897 
9898 
9899 
9900         IF (p_territory_flag = 'Y') THEN
9901 
9902           IF (l_territory_id IS NOT NULL) THEN
9903 
9904           -- change the API Name temporarily so that in case of unexpected error
9905           -- it is properly caught
9906           l_api_name := l_api_name||'-JTF_TERRITORY_GET_PUB';
9907 
9908             JTF_TERRITORY_GET_PUB.Get_Escalation_Territory
9909               (
9910                 p_api_version         => l_api_version,
9911                 p_init_msg_list       => p_init_msg_list,
9912                 x_return_status       => x_return_status,
9913                 x_msg_count           => x_msg_count,
9914                 x_msg_data            => x_msg_data,
9915                 p_terr_id             => l_territory_id,
9916                 x_escalation_terr_id  => l_esc_territory_id
9917               );
9918 
9919            -- set back the API name to original name
9920            l_api_name := l_api_name_1;
9921 
9922 
9923             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
9924               -- Unexpected Execution Error from call to Territory Manager
9925               fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
9926               fnd_msg_pub.add;
9927               IF (x_return_status = fnd_api.g_ret_sts_error) THEN
9928                 RAISE fnd_api.g_exc_error;
9929               ELSE
9930                 RAISE fnd_api.g_exc_unexpected_error;
9931               END IF;
9932             END IF;
9933 
9934             l_assign_resources_tbl.DELETE;
9935 
9936            IF ( l_esc_territory_id IS NOT NULL) THEN
9937 
9938             -- change the API Name temporarily so that in case of unexpected error
9939             -- it is properly caught
9940             l_api_name := l_api_name||'-JTF_TERRITORY_GET_PUB';
9941 
9942               JTF_TERRITORY_GET_PUB.Get_Escalation_TerrMembers
9943                 (
9944                   p_api_version_number     => l_api_version,
9945                   p_init_msg_list          => p_init_msg_list,
9946                   p_commit                 => NULL,
9947                   x_return_status          => x_return_status,
9948                   x_msg_count              => x_msg_count,
9949                   x_msg_data               => x_msg_data,
9950                   p_terr_id                => l_esc_territory_id,
9951                   x_QualifyingRsc_out_tbl  => l_assign_resources_tbl
9952                 );
9953 
9954             -- set back the API name to original name
9955             l_api_name := l_api_name_1;
9956 
9957               IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
9958                 -- Unexpected Execution Error from call to Territory Manager
9959                 fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
9960                 fnd_msg_pub.add;
9961                 IF (x_return_status = fnd_api.g_ret_sts_error) THEN
9962                   RAISE fnd_api.g_exc_error;
9963                 ELSE
9964                   RAISE fnd_api.g_exc_unexpected_error;
9965                 END IF;
9966               END IF;
9967 
9968             --ELSE
9969               --fnd_message.set_name('JTF', 'JTF_AM_NO_ESC_TERR');
9970               --fnd_msg_pub.add;
9971               --RAISE fnd_api.g_exc_error;
9972             END IF;
9973 
9974           END IF;  -- IF (l_territory_id IS NOT NULL) THEN
9975 
9976 
9977           IF ( l_assign_resources_tbl.COUNT = 0 ) THEN
9978 
9979           -- change the API Name temporarily so that in case of unexpected error
9980           -- it is properly caught
9981           l_api_name := l_api_name||'-JTF_TERRITORY_GET_PUB';
9982 
9983             JTF_TERRITORY_GET_PUB.Get_Escalation_TerrMembers
9984               (
9985                 p_api_version_number     => l_api_version,
9986                 p_init_msg_list          => p_init_msg_list,
9987                 p_commit                 => NULL,
9988                 x_return_status          => x_return_status,
9989                 x_msg_count              => x_msg_count,
9990                 x_msg_data               => x_msg_data,
9991                 p_terr_id                => 1,
9992                 x_QualifyingRsc_out_tbl  => l_assign_resources_tbl
9993               );
9994 
9995             -- set back the API name to original name
9996             l_api_name := l_api_name_1;
9997 
9998             IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
9999               -- Unexpected Execution Error from call to Territory Manager
10000               fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
10001               fnd_msg_pub.add;
10002               IF (x_return_status = fnd_api.g_ret_sts_error) THEN
10003                 RAISE fnd_api.g_exc_error;
10004               ELSE
10005                 RAISE fnd_api.g_exc_unexpected_error;
10006               END IF;
10007             END IF;
10008           END IF;
10009 
10010 
10011 
10012            -- added 2 april 2003 by sudarsana to conver RS_SUPPLIER TO RS_SUPPLIER_CONTACT
10013 
10014           IF(l_assign_resources_tbl.COUNT > 0)
10015           THEN
10016              l_current_record := l_assign_resources_tbl.FIRST;
10017              WHILE (l_current_record <= l_assign_resources_tbl.LAST)
10018              LOOP
10019                 IF(l_assign_resources_tbl(l_current_record).resource_type = 'RS_SUPPLIER')
10020                 THEN
10021                    l_assign_resources_tbl(l_current_record).resource_type := 'RS_SUPPLIER_CONTACT';
10022                 END IF;
10023                 l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
10024              END LOOP;
10025           END IF;
10026 
10027 
10028           IF l_assign_resources_tbl.COUNT > 0 THEN
10029 
10030             l_current_record := l_assign_resources_tbl.FIRST;
10031 
10032 
10033             IF (l_auto_select_flag = 'Y') THEN
10034               l_total_records := p_no_of_resources;
10035             ELSE
10036               l_total_records := l_assign_resources_tbl.LAST;
10037             END IF;
10038 
10039            -- removed the calendar check here. calling the procedure for calendar check in the next step
10040            -- calendar check removed 29th September 2003
10041             WHILE l_current_record <= l_assign_resources_tbl.LAST
10042             LOOP
10043 
10044               -- removed the calendar check here. calling the procedure for calendar check in the next step
10045               -- calendar check removed 29th September 2003
10046                 open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
10047                               l_assign_resources_tbl(l_current_record).resource_type);
10048                  fetch check_date_cur into l_value;
10049                  if (check_date_cur%found)
10050                  then
10051 
10052                    l_count := l_count + 1;
10053 
10054                    x_assign_resources_tbl(l_count).terr_rsc_id           :=
10055                                          l_assign_resources_tbl(l_current_record).terr_rsc_id;
10056                    x_assign_resources_tbl(l_count).resource_id           :=
10057                                          l_assign_resources_tbl(l_current_record).resource_id;
10058                    x_assign_resources_tbl(l_count).resource_type         :=
10059                                          l_assign_resources_tbl(l_current_record).resource_type;
10060                    x_assign_resources_tbl(l_count).role                  :=
10061                                          l_assign_resources_tbl(l_current_record).role;
10062                    x_assign_resources_tbl(l_count).start_date            := NULL;
10063                    x_assign_resources_tbl(l_count).end_date              := NULL;
10064                    x_assign_resources_tbl(l_count).shift_construct_id    := NULL;
10065                    x_assign_resources_tbl(l_count).terr_id               :=
10066                                          l_assign_resources_tbl(l_current_record).terr_id;
10067                    x_assign_resources_tbl(l_count).terr_name             :=
10068                                          l_assign_resources_tbl(l_current_record).terr_name;
10069                    x_assign_resources_tbl(l_count).primary_contact_flag  :=
10070                                          l_assign_resources_tbl(l_current_record).primary_contact_flag;
10071                    x_assign_resources_tbl(l_count).primary_flag  :=
10072                                          l_assign_resources_tbl(l_current_record).primary_contact_flag;
10073                    x_assign_resources_tbl(l_count).resource_source       := 'TERR';
10074                  END IF;
10075                  close check_date_cur;
10076               l_current_esc_record := l_current_esc_record + 1;
10077               l_current_record     := l_assign_resources_tbl.NEXT(l_current_record);
10078             END LOOP;
10079 
10080           -- added calendar call out
10081           -- this has been done as now the calendar check is done in a seperate procedure GET_AVAILABLE_RESOURCE
10082           -- changed on 29th September 2003
10083           -- The calendar flag check will not be done any more. The first available slot will be fetched
10084           -- This is for the preformance bug 3301417. If the calendar flag is Y then the resources will
10085           -- filtered based on availability in the procedure get_available_slot. This change is being done on
10086           -- 16 June 2004
10087           --IF (p_calendar_flag = 'Y') THEN
10088              -- change the API Name temporarily so that in case of unexpected error
10089              -- it is properly caught
10090               l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
10091               l_return_status_1 := x_return_status ;
10092               -- call the api to check resource availability
10093               get_available_resources
10094               (
10095                 p_init_msg_list                 =>  'F',
10096                 p_calendar_flag                 =>  p_calendar_flag,
10097                 p_effort_duration               =>  l_effort_duration,
10098                 p_effort_uom                    =>  l_effort_uom,
10099                 p_planned_start_date            =>  l_planned_start_date,
10100                 p_planned_end_date              =>  l_planned_end_date,
10101                 p_breakdown                     =>   null,
10102                 p_breakdown_uom                 =>   null,
10103                 p_continuous_task               =>  jtf_assign_pub.g_continuous_work,
10104                 x_return_status                 =>  x_return_status,
10105                 x_msg_count                     =>  x_msg_count,
10106                 x_msg_data                      =>  x_msg_data,
10107                 x_assign_resources_tbl          =>  x_assign_resources_tbl);
10108 
10109               -- set back the API name to original name
10110                l_api_name := l_api_name_1;
10111 
10112                IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
10113               -- Unexpected Execution Error from call to Get_contracts_resources
10114                  fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
10115                  fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
10116                  fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_ESC_RESOURCES');
10117                  fnd_msg_pub.add;
10118                  IF (x_return_status = fnd_api.g_ret_sts_error) THEN
10119                   RAISE fnd_api.g_exc_error;
10120                  ELSE
10121                    RAISE fnd_api.g_exc_unexpected_error;
10122                  END IF;
10123               END IF; -- end of x_return_status check
10124            -- end if; -- if p_calendar_flag = Y
10125 
10126             IF ( p_calendar_flag = 'Y' AND
10127                  x_assign_resources_tbl.count = 0 ) THEN
10128               fnd_message.set_name('JTF', 'JTF_AM_RESOURCE_NOT_AVAILABLE');
10129               fnd_msg_pub.add;
10130 --              RAISE fnd_api.g_exc_error;
10131             END IF;
10132 
10133               -- to implement auto selection
10134             IF(l_auto_select_flag = 'Y')
10135             THEN
10136                l_temp_table.delete;
10137                l_temp_table := x_assign_resources_tbl;
10138                x_assign_resources_tbl.delete;
10139                l_count := 0;
10140                l_current_record := l_temp_table.FIRST;
10141                l_total_records := p_no_of_resources;
10142 
10143                WHILE l_current_record <= l_temp_table.LAST
10144                LOOP
10145                  If(l_count < l_total_records)
10146                  THEN
10147                     x_assign_resources_tbl(l_count) := l_temp_table(l_current_record);
10148                     l_count := l_count + 1;
10149                  end if; -- end of count check
10150                  l_current_record := l_temp_table.NEXT(l_current_record);
10151                END LOOP; -- end of courrent record check
10152 
10153             END IF; -- end of auto select flag
10154 
10155           ELSE   -- No resources returned from the Territory API
10156             fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
10157             fnd_msg_pub.add;
10158 --            RAISE fnd_api.g_exc_error;
10159           END IF;
10160 
10161         ELSE  -- Territory Flag is NO
10162           fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
10163           fnd_msg_pub.add;
10164 --          RAISE fnd_api.g_exc_error;
10165         END IF;
10166 
10167         l_esc_record := p_esc_tbl.NEXT(l_esc_record);
10168 
10169       END LOOP;
10170 
10171     ELSE -- p_esc_tbl.count <= 0
10172 
10173       -- change the API Name temporarily so that in case of unexpected error
10174       -- it is properly caught
10175       l_api_name := l_api_name||'-JTF_TERRITORY_GET_PUB';
10176 
10177       JTF_TERRITORY_GET_PUB.Get_Escalation_TerrMembers
10178         (
10179           p_api_version_number     => l_api_version,
10180           p_init_msg_list          => p_init_msg_list,
10181           p_commit                 => NULL,
10182           x_return_status          => x_return_status,
10183           x_msg_count              => x_msg_count,
10184           x_msg_data               => x_msg_data,
10185           p_terr_id                => 1,
10186           x_QualifyingRsc_out_tbl  => l_assign_resources_tbl
10187         );
10188 
10189         -- set back the API name to original name
10190         l_api_name := l_api_name_1;
10191 
10192       IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
10193          -- Unexpected Execution Error from call to Territory Manager
10194          fnd_message.set_name('JTF', 'JTF_AM_ERROR_TERR_API');
10195          fnd_msg_pub.add;
10196          IF (x_return_status = fnd_api.g_ret_sts_error) THEN
10197            RAISE fnd_api.g_exc_error;
10198          ELSE
10199            RAISE fnd_api.g_exc_unexpected_error;
10200          END IF;
10201       END IF;
10202 
10203 
10204       IF l_assign_resources_tbl.COUNT > 0 THEN
10205 
10206         l_current_record     := l_assign_resources_tbl.FIRST;
10207         l_current_esc_record := 0;
10208 
10209 
10210         IF (l_auto_select_flag = 'Y') THEN
10211           l_total_records := p_no_of_resources;
10212         ELSE
10213           l_total_records := l_assign_resources_tbl.LAST;
10214         END IF;
10215 
10216 
10217         WHILE (l_current_record <= l_assign_resources_tbl.LAST ) AND
10218               (l_count < l_total_records)
10219         LOOP
10220           open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
10221                               l_assign_resources_tbl(l_current_record).resource_type);
10222           fetch check_date_cur into l_value;
10223           if (check_date_cur%found)
10224           then
10225 
10226             l_count := l_count + 1;
10227 
10228             x_assign_resources_tbl(l_count).terr_rsc_id           :=
10229                                  l_assign_resources_tbl(l_current_record).terr_rsc_id;
10230             x_assign_resources_tbl(l_count).resource_id           :=
10231                                  l_assign_resources_tbl(l_current_record).resource_id;
10232             x_assign_resources_tbl(l_count).resource_type         :=
10233                                  l_assign_resources_tbl(l_current_record).resource_type;
10234             x_assign_resources_tbl(l_count).role                  :=
10235                                  l_assign_resources_tbl(l_current_record).role;
10236             x_assign_resources_tbl(l_count).start_date            := NULL;
10237             x_assign_resources_tbl(l_count).end_date              := NULL;
10238             x_assign_resources_tbl(l_count).shift_construct_id    := NULL;
10239             x_assign_resources_tbl(l_count).terr_id               :=
10240                                  l_assign_resources_tbl(l_current_record).terr_id;
10241             x_assign_resources_tbl(l_count).terr_name             :=
10242                                  l_assign_resources_tbl(l_current_record).terr_name;
10243             x_assign_resources_tbl(l_count).primary_contact_flag  :=
10244                                  l_assign_resources_tbl(l_current_record).primary_contact_flag;
10245             x_assign_resources_tbl(l_count).primary_flag  :=
10246                                  l_assign_resources_tbl(l_current_record).primary_contact_flag;
10247             x_assign_resources_tbl(l_count).resource_source       := 'TERR';
10248 
10249            end if;
10250            close check_date_cur;
10251           l_current_esc_record := l_current_esc_record + 1;
10252           l_current_record     := l_assign_resources_tbl.NEXT(l_current_record);
10253 
10254         END LOOP;
10255 
10256       ELSE  -- l_assign_resources_tbl.COUNT <= 0
10257         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
10258         fnd_msg_pub.add;
10259 --        RAISE fnd_api.g_exc_error;
10260       END IF;
10261 
10262 
10263       /*
10264       fnd_message.set_name('JTF', 'JTF_AM_EMPTY_ESC_TBL');
10265       fnd_msg_pub.add;
10266       RAISE fnd_api.g_exc_error;
10267       */
10268 
10269     END IF; -- End of l_esc_tbl.count > 0
10270 
10271 
10272 
10273 
10274     -- Start of enhancement to add SUPPORT SITE ID and NAME to the OUT Table
10275 
10276     l_dynamic_sql1 := ' SELECT a.city city '||
10277                       ' FROM   hz_locations a, hz_party_sites b,  hz_party_site_uses c '||
10278                       ' WHERE  c.site_use_type = :1  AND '||
10279                       ' b.party_site_id        = :2  AND '||
10280                       ' a.location_id          = b.location_id   AND '||
10281                       ' c.party_site_id        = b.party_site_id ';
10282 
10283     IF x_assign_resources_tbl.COUNT > 0 THEN
10284 
10285       l_current_record := x_assign_resources_tbl.FIRST;
10286 
10287       WHILE l_current_record <= x_assign_resources_tbl.LAST
10288       LOOP
10289 
10290         l_rsc_type := resource_type_change(x_assign_resources_tbl(l_current_record).resource_type);
10291 
10292         OPEN  cur_support_site_id (x_assign_resources_tbl(l_current_record).resource_id,
10293                                    l_rsc_type);
10294 
10295         FETCH cur_support_site_id INTO x_assign_resources_tbl(l_current_record).support_site_id;
10296 
10297         IF (x_assign_resources_tbl(l_current_record).support_site_id IS NOT NULL) THEN
10298 
10299           OPEN  cur_support_site_name FOR l_dynamic_sql1
10300                 USING l_support_site,
10301                       x_assign_resources_tbl(l_current_record).support_site_id;
10302 
10303           FETCH cur_support_site_name INTO x_assign_resources_tbl(l_current_record).support_site_name;
10304           IF (  cur_support_site_name % NOTFOUND ) THEN
10305             x_assign_resources_tbl(l_current_record).support_site_name := NULL;
10306           END IF;
10307           CLOSE cur_support_site_name;
10308         ELSE
10309           x_assign_resources_tbl(l_current_record).support_site_id   := NULL;
10310           x_assign_resources_tbl(l_current_record).support_site_name := NULL;
10311 
10312         END IF;
10313 
10314         CLOSE cur_support_site_id;
10315 
10316         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
10317       END LOOP;
10318     END IF;
10319 
10320     -- End of enhancement
10321 
10322 
10323 
10324 
10325     -- Start of enhancement to add Web Availability to the OUT Table
10326 
10327 
10328     IF (x_assign_resources_tbl.COUNT > 0) THEN
10329 
10330       l_current_record := x_assign_resources_tbl.FIRST;
10331 
10332       WHILE l_current_record <= x_assign_resources_tbl.LAST
10333       LOOP
10334 
10335         IF ( UPPER(l_web_availability_flag) = 'Y') THEN
10336           l_rsc_type := resource_type_change(x_assign_resources_tbl(l_current_record).resource_type);
10337 
10338           OPEN  cur_web_availability (x_assign_resources_tbl(l_current_record).resource_id,
10339                                       l_rsc_type);
10340           FETCH cur_web_availability INTO l_rsc_id;
10341 
10342           IF (cur_web_availability%FOUND) THEN
10343             x_assign_resources_tbl(l_current_record).web_availability_flag := 'Y';
10344           ELSE
10345             x_assign_resources_tbl(l_current_record).web_availability_flag := 'N';
10346           END IF;
10347 
10348           CLOSE cur_web_availability;
10349         ELSE
10350           x_assign_resources_tbl(l_current_record).web_availability_flag := NULL;
10351         END IF; --l_web_availability_flag = 'Y'
10352 
10353         l_current_record := x_assign_resources_tbl.NEXT(l_current_record);
10354       END LOOP;
10355     END IF;
10356 
10357     -- End of enhancement
10358 
10359 /********************** Start of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
10360 
10361       -- raise workfow event
10362       -- workflow test
10363       Begin
10364          jtf_assign_pub.g_assign_resources_tbl.delete;
10365          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
10366 
10367 	   IF (p_esc_tbl.count>0) THEN
10368 
10369  		l_esc_count := p_esc_tbl.FIRST;
10370 
10371 		WHILE (l_esc_count <= p_esc_tbl.LAST)
10372 		LOOP
10373 	         jtf_am_wf_events_pub.assign_esc_resource
10374                     (P_API_VERSION           =>  1.0,
10375                      P_INIT_MSG_LIST         =>  'F',
10376                      P_COMMIT                =>  'F',
10377                      P_ESC_REC           	   =>  p_esc_tbl(l_esc_count),
10378                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
10379                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
10380                      X_RETURN_STATUS         =>  l_wf_return_status,
10381                      X_MSG_COUNT             =>  l_wf_msg_count,
10382                      X_MSG_DATA              =>  l_wf_msg_data
10383                      );
10384 
10385 
10386         		IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
10387             		-- Unexpected Execution Error from call to assign_sr_resource
10388             		fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
10389             		fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
10390             		fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ESC_RESOURCES');
10391             		fnd_msg_pub.add;
10392         		ELSE
10393 				x_assign_resources_tbl.delete;
10394             		x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
10395 
10396 			END IF;
10397 
10398 			l_esc_count := p_esc_tbl.NEXT(l_esc_count);
10399 
10400 		END LOOP;
10401 	   ELSE
10402 
10403 	     	jtf_am_wf_events_pub.assign_esc_resource
10404             	  (P_API_VERSION           =>  1.0,
10405                      P_INIT_MSG_LIST         =>  'F',
10406                      P_COMMIT                =>  'F',
10407                      P_ESC_REC           	   =>  NULL,
10408                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
10409                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
10410                      X_RETURN_STATUS         =>  l_wf_return_status,
10411                      X_MSG_COUNT             =>  l_wf_msg_count,
10412                      X_MSG_DATA              =>  l_wf_msg_data
10413                      );
10414 
10415 
10416         	IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
10417             	-- Unexpected Execution Error from call to assign_sr_resource
10418             	fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
10419             	fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
10420             	fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ESC_RESOURCES');
10421             	fnd_msg_pub.add;
10422         	ELSE
10423 			x_assign_resources_tbl.delete;
10424             	x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
10425 		END IF;
10426 
10427    	   END IF;
10428 
10429       Exception
10430             When OTHERS Then
10431                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
10432                fnd_message.set_token('P_SQLCODE',SQLCODE);
10433                fnd_message.set_token('P_SQLERRM',SQLERRM);
10434                fnd_message.set_token('P_API_NAME',l_api_name);
10435                FND_MSG_PUB.add;
10436       End;
10437 
10438 /********************** End of Addition for Enh. No 3076744 by SBARAT, 23/09/2004 ************************/
10439 
10440 
10441     -- To Plugin the Workflow enabling the user
10442     -- to further filter the resources
10443 
10444 
10445     /* Getting the Workflow Profile value defined for the Assignment Manager */
10446 
10447     --l_workflow_profile := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_WORKFLOW_NAME' ); -- Commented out by SBARAT on 12/10/2004, Bug-3830061
10448     l_workflow_profile := FND_PROFILE.VALUE ( 'ACTIVATE_WORKFLOW_NAME' ); -- Added by SBARAT on 12/10/2004, Bug-3830061
10449 
10450 
10451     SELECT jtf_calendars_s.NEXTVAL INTO l_workflow_key
10452     FROM   dual;
10453 
10454     IF (JTF_USR_HKS.ok_to_execute
10455           (
10456             'JTF_ASSIGN_PUB',
10457             'GET_ASSIGN_ESC_RESOURCES',
10458             'W',
10459             'W'
10460           )
10461        ) THEN
10462 
10463       IF (JTF_ASSIGN_CUHK.ok_to_launch_workflow
10464             (
10465               p_api_version     => l_api_version,
10466               p_init_msg_list   => p_init_msg_list,
10467               x_return_status   => x_return_status,
10468               x_msg_count       => x_msg_count,
10469               x_msg_data        => x_msg_data
10470             )
10471          ) THEN
10472 
10473         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
10474           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
10475           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
10476           fnd_msg_pub.add;
10477           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
10478             RAISE fnd_api.g_exc_error;
10479           ELSE
10480             RAISE fnd_api.g_exc_unexpected_error;
10481           END IF;
10482         END IF;
10483 
10484 
10485         l_bind_data_id := JTF_USR_HKS.get_bind_data_id;
10486 
10487         JTF_USR_HKS.WrkFlowLaunch
10488           (
10489             'JTF_ASSIGN_WF',
10490             l_workflow_profile,
10491             'ASSIGN WF - '|| TO_CHAR(l_workflow_key),
10492             l_bind_data_id,
10493             l_return_code
10494           );
10495 
10496         JTF_USR_HKS.purge_bind_data
10497           (
10498             l_bind_data_id,
10499             'W'
10500           );
10501 
10502 
10503         IF (l_return_code = fnd_api.g_ret_sts_error) THEN
10504           -- Execution Error from call to Assignment Manager Workflow Hook
10505           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
10506           fnd_msg_pub.add;
10507           RAISE fnd_api.g_exc_error;
10508         ELSIF (l_return_code = fnd_api.g_ret_sts_unexp_error) THEN
10509           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
10510           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
10511           fnd_msg_pub.add;
10512           RAISE fnd_api.g_exc_unexpected_error;
10513         END IF;
10514 
10515       END IF;  -- End of JTF_ASSIGN_CUHK
10516 
10517     END IF;    -- End of JTF_USR_HKS
10518 
10519 
10520 
10521     /* Standard call to get message count and
10522        the message information */
10523 
10524     FND_MSG_PUB.Count_And_Get
10525     (
10526       p_count => x_msg_count,
10527       p_data  => x_msg_data
10528     );
10529 
10530 
10531 
10532   EXCEPTION
10533     WHEN FND_API.G_EXC_ERROR THEN
10534       x_return_status := FND_API.G_RET_STS_ERROR ;
10535       FND_MSG_PUB.Count_And_Get
10536       (
10537         p_count => x_msg_count,
10538         p_data  => x_msg_data
10539       );
10540 
10541     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10542       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10543       FND_MSG_PUB.Count_And_Get
10544       (
10545         p_count => x_msg_count,
10546         p_data  => x_msg_data
10547       );
10548 
10549     WHEN OTHERS THEN
10550       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
10551       fnd_message.set_token('P_SQLCODE',SQLCODE);
10552       fnd_message.set_token('P_SQLERRM',SQLERRM);
10553       fnd_message.set_token('P_API_NAME',l_api_name);
10554       FND_MSG_PUB.add;
10555 
10556       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10557       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
10558         FND_MSG_PUB.Add_Exc_Msg
10559         (
10560           G_PKG_NAME,
10561           l_api_name
10562         );
10563       END IF;
10564 
10565       FND_MSG_PUB.Count_And_Get
10566       (
10567         p_count => x_msg_count,
10568         p_data  => x_msg_data
10569        );
10570 
10571   END GET_ASSIGN_ESC_RESOURCES;
10572 
10573 
10574 
10575 -- *******************************************************************************
10576 
10577 -- Start of comments
10578 
10579 --      API name        : GET_ASSIGN_RESOURCES
10580 --      Type            : Public
10581 --      Function        : Determine the resources based on the customer
10582 --                        preferences or territory preferences and the
10583 --                        availability.
10584 --      Pre-reqs        : None
10585 
10586 --      Parameters      :
10587 
10588 --      IN              : p_api_version         IN      NUMBER  Required
10589 --                        p_init_msg_list       IN      VARCHAR2 Optional
10590 --                                              DEFAULT JTF_ASSIGN_PUB.AM_FALSE
10591 --                        p_commit              IN      VARCHAR2 optional
10592 --                                              DEFAULT JTF_ASSIGN_PUB.AM_FALSE
10593 
10594 --     Assignment Manager Specific Parameters
10595 
10596 --     This determines the Resource, Resource Type
10597 --     and Resource Role required by the calling document
10598 --     p_resource_id                            NUMBER
10599 --     p_resource_type                          VARCHAR2(30)
10600 --     p_role                                   VARCHAR2(30)
10601 
10602 --     This determines the number of resources required
10603 --     by the calling document
10604 --     Defaulted to 1
10605 --     p_no_of_resources                        NUMBER
10606 
10607 --     This is for sending out the qualified resource directly
10608 --     to the calling form.
10609 --     Defaulted to 'Y'(Profile Value)
10610 --     p_auto_select_flag                       VARCHAR2(1)
10611 --                                              : value of  Y or N
10612 
10613 --     This is to set the preference of CONTRACTS PREFERRED ENGINEERS
10614 --     Defaulted to 'N'(Profile Value)
10615 --     p_contracts_preferred_engineer           VARCHAR2(1)
10616 --                                              : value of  Y or N
10617 
10618 --     This is to set the preference of INSTALL BASE PREFERRED ENGINEERS
10619 --     Defaulted to 'N'(Profile Value)
10620 --     p_ib_preferred_engineer                  VARCHAR2(1)
10621 --                                              : value of  Y or N
10622 
10623 --     This is to fetch the CONTRACTS PREFERRED ENGINEERS
10624 --     p_contract_id                            NUMBER
10625 
10626 --     This is to fetch the INSTALL BASE PREFERRED ENGINEERS
10627 --     p_customer_product_id                    NUMBER
10628 
10629 --     The Effort required is determined by these two parameters
10630 --     p_effort_duration                        NUMBER
10631 --     p_effort_uom                             VARCHAR2(10)
10632 
10633 --     The Dates in which the resource is required,
10634 --     is determined by these two parameters
10635 --     p_start_date                             DATE
10636 --     p_end_date                               DATE
10637 
10638 --     The Territory Manager is accessed based on the value set
10639 --     Defaulted to Y
10640 --     p_territory_flag                         VARCHAR2(1)
10641 --                                              : value of  Y or N
10642 
10643 --     The Resource Availability is checked based on the value set
10644 --     Defaulted to Y
10645 --     p_calendar_flag                          VARCHAR2(1)
10646 --                                              : value of  Y or N
10647 
10648 --     This parameter contains the Calling Document ID
10649 --     which could be a TASK_ID or a SERVICE_REQUEST_ID
10650 --     or a OPPORTUNITY_ID or a LEAD_ID etc.
10651 --     p_calling_doc_id                         NUMBER  -- REQUIRED
10652 
10653 --     This parameter contains the Calling Document Type
10654 --     which could be :
10655 --        'TASK' when the calling doc is TASK
10656 --     or 'SR'   when the calling doc is SERVICE REQUEST
10657 --     or 'DEF'  when the calling doc is DEFECT MANAGEMENT
10658 --     or 'OPPR' when the calling doc is OPPORTUNITIES
10659 --     It is mandatory to enter a value for this parameter
10660 --     to find proper qualified resources
10661 --     p_calling_doc_type                        VARCHAR2
10662 
10663 --     This parameter contains list of qualifier columns from the
10664 --     UI which have been selected to re-query the resources.
10665 --     Strictly for the use of User Interface of Assignment Manager.
10666 --     p_column_list                             VARCHAR2
10667 
10668 --     These parameters contain the Qualifier Values for
10669 --     the Calling Document
10670 --     p_sr_rec                                  JTF_ASSIGN_PUB.
10671 --                                               JTF_Serv_Req_rec_type
10672 --     p_sr_task_rec                             JTF_ASSIGN_PUB.
10673 --                                               JTF_Srv_Task_rec_type
10674 --     p_defect_rec                              JTF_ASSIGN_PUB.
10675 --                                               JTF_Def_Mgmt_rec_type
10676 
10677 --     OUT              : x_return_status        OUT     VARCHAR2(1)
10678 --                        x_msg_count            OUT     NUMBER
10679 --                        x_msg_data             OUT     VARCHAR2(2000)
10680 --                        x_assign_resources_tbl OUT     JTF_ASSIGN_PUB.
10681 --                                                       AssignResources_tbl_type
10682 
10683 
10684 --     Version          : Current version        1.0
10685 --                        Initial version        1.0
10686 --
10687 --     Notes            :
10688 --
10689 
10690 -- End of comments
10691 
10692 -- *********************************************************************************
10693 
10694 
10695 
10696   /*  Main Procedure definition with the parameters
10697       This procedure in turn calls the relevant procedure to
10698       process the requests for assignment of resources */
10699 
10700 
10701   PROCEDURE GET_ASSIGN_RESOURCES
10702     (   p_api_version                         IN  NUMBER,
10703         p_init_msg_list                       IN  VARCHAR2 ,
10704         p_commit                              IN  VARCHAR2 ,
10705         p_resource_id                         IN  NUMBER   ,
10706         p_resource_type                       IN  VARCHAR2 ,
10707         p_role                                IN  VARCHAR2 ,
10708         p_no_of_resources                     IN  NUMBER   ,
10709         p_auto_select_flag                    IN  VARCHAR2 ,
10710         p_contracts_preferred_engineer        IN  VARCHAR2 ,
10711         p_ib_preferred_engineer               IN  VARCHAR2 ,
10712         p_contract_id                         IN  NUMBER   ,
10713         p_customer_product_id                 IN  NUMBER   ,
10714         p_effort_duration                     IN  NUMBER   ,
10715         p_effort_uom                          IN  VARCHAR2 ,
10716         p_start_date                          IN  DATE     ,
10717         p_end_date                            IN  DATE     ,
10718         p_territory_flag                      IN  VARCHAR2 ,
10719         p_calendar_flag                       IN  VARCHAR2 ,
10720         p_web_availability_flag               IN  VARCHAR2 ,
10721         p_category_id                         IN  NUMBER   ,
10722         p_inventory_item_id                   IN  NUMBER   ,
10723         p_inventory_org_id                    IN  NUMBER   ,
10724  	p_problem_code                        IN  VARCHAR2 ,
10725         p_calling_doc_id                      IN  NUMBER,
10726         p_calling_doc_type                    IN  VARCHAR2,
10727         p_column_list                         IN  VARCHAR2 ,
10728         p_sr_rec                              IN  JTF_ASSIGN_PUB.JTF_Serv_Req_rec_type ,
10729         p_sr_task_rec                         IN  JTF_ASSIGN_PUB.JTF_Srv_Task_rec_type ,
10730         p_defect_rec                          IN  JTF_ASSIGN_PUB.JTF_Def_Mgmt_rec_type ,
10731         p_business_process_id                 IN  NUMBER,
10732         p_business_process_date               IN  DATE,
10733         p_filter_excluded_resource            IN  VARCHAR2,
10734         x_assign_resources_tbl                OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type,
10735         x_return_status                       OUT NOCOPY VARCHAR2,
10736         x_msg_count                           OUT NOCOPY NUMBER,
10737         x_msg_data                            OUT NOCOPY VARCHAR2,
10738 	--Added for Bug # 5386560
10739 	p_inventory_component_id              IN  NUMBER DEFAULT NULL,
10740 	--Added for Bug # 5386560 Ends here
10741 	--Added for Bug # 5573916
10742 	p_calendar_check                IN  VARCHAR2 DEFAULT 'Y'
10743 	--Added for Bug # 5573916 Ends here
10744     )
10745   IS
10746 
10747     l_return_status_1                     VARCHAR2(10);
10748     l_api_name			                         VARCHAR2(100)	:= 'GET_ASSIGN_RESOURCES';
10749     l_api_name_1  	                       VARCHAR2(60)	:= 'GET_ASSIGN_RESOURCES';
10750     l_api_version           	             NUMBER        := 1.0;
10751     l_no_of_resources                     NUMBER        := p_no_of_resources;
10752     l_auto_select_flag                    VARCHAR2(1)   := p_auto_select_flag;
10753     l_contracts_preferred_engineer        VARCHAR2(1)   := p_contracts_preferred_engineer;
10754     l_ib_preferred_engineer               VARCHAR2(1)   := p_ib_preferred_engineer;
10755     l_territory_flag                      VARCHAR2(1)   := p_territory_flag;
10756     l_calendar_flag                       VARCHAR2(1)   := p_calendar_flag;
10757     l_calling_doc_type                    VARCHAR2(10)  := p_calling_doc_type;
10758 
10759     l_web_availability_flag               VARCHAR2(1)   := p_web_availability_flag;
10760 
10761     l_contracts_profile                   VARCHAR2(1);
10762     l_ib_profile                          VARCHAR2(1);
10763     l_auto_select_profile                 VARCHAR2(1);
10764     l_workflow_profile                    VARCHAR2(60);
10765 
10766     l_current_record                      INTEGER;
10767     l_dynamic_cursor                      INTEGER;
10768     l_dynamic_sql                         VARCHAR2(4000);
10769 
10770     l_return_code                         VARCHAR2(60);
10771     l_wf_return_status                    VARCHAR2(60);
10772     l_wf_msg_count                        NUMBER;
10773     l_wf_msg_data                         VARCHAR2(2000);
10774 
10775     l_bind_data_id                        NUMBER;
10776     l_workflow_key                        NUMBER;
10777 
10778     l_sr_rec                              JTF_TERRITORY_PUB.JTF_Serv_Req_rec_type;
10779     l_sr_task_rec                         JTF_TERRITORY_PUB.JTF_Srv_Task_rec_type;
10780     l_defect_rec                          JTF_TERRITORY_PUB.JTF_Def_Mgmt_rec_type;
10781     l_assign_resources_tbl                JTF_ASSIGN_PUB.AssignResources_tbl_type;
10782     t_assign_resources_tbl                JTF_ASSIGN_PUB.AssignResources_tbl_type; --6453896
10783 
10784 
10785 
10786     CURSOR cur_resource_type IS
10787       SELECT object_code
10788       FROM   jtf_object_usages
10789       WHERE  object_user_code = 'RESOURCES' AND
10790              object_code      = p_resource_type;
10791     l_cur_resource_type cur_resource_type%ROWTYPE;
10792 
10793 
10794     CURSOR cur_res_location(p_rid NUMBER, p_rtype VARCHAR2) IS
10795       SELECT DECODE(source_postal_code, NULL, '00000', source_postal_code)
10796       FROM   jtf_rs_resource_extns_vl
10797       WHERE  resource_id     = p_rid AND
10798              'RS_'||category = p_rtype;
10799 
10800 
10801     /*
10802     CURSOR cur_effort_uom IS
10803       SELECT uom_code
10804       FROM   mtl_units_of_measure_vl
10805       WHERE  uom_code = p_effort_uom;
10806     l_cur_effort_uom cur_effort_uom%ROWTYPE;
10807     */
10808 
10809     --Bug# 4455803 MOAC.
10810     CURSOR check_date_cur(l_resource_id in number,
10811                           l_resource_type in varchar2)
10812         IS
10813      SELECT 'Y'
10814        --FROM  jtf_rs_all_resources_vl
10815        FROM  jtf_task_resources_vl
10816       where   resource_id = l_resource_id
10817         and   resource_type = l_resource_type
10818         and   nvl(trunc(end_date_active), trunc(sysdate)) >= trunc(sysdate);
10819 
10820     l_value varchar2(100);
10821 
10822     l_count number := 0;
10823 
10824     l_usage varchar2(2000);
10825      -- new variable to handle uom code issue for effort durations
10826     l_effort_duration                    NUMBER;
10827     l_uom_hour                           VARCHAR2(2000);
10828     l_sort_profile			 VARCHAR2(2);
10829 
10830  /*procedure ts(v varchar2)
10831     is
10832       pragma autonomous_transaction;
10833     begin
10834       insert into test_values values(v);
10835       commit;
10836     end;*/
10837   BEGIN
10838     SAVEPOINT jtf_assign_pub;
10839 
10840     -- Started Assignment Manager Public API
10841 
10842 
10843     /* Standard call to check for call compatibility */
10844 
10845     IF NOT fnd_api.compatible_api_call (l_api_version,
10846                                         p_api_version,
10847                                         l_api_name,
10848                                         g_pkg_name) THEN
10849       RAISE fnd_api.g_exc_unexpected_error;
10850     END IF;
10851 
10852 
10853     /* Initialize message list if p_init_msg_list is set to TRUE */
10854 
10855     IF fnd_api.to_boolean (p_init_msg_list) THEN
10856       fnd_msg_pub.initialize;
10857     END IF;
10858 
10859 
10860 
10861     /* Paramater Validation */
10862 
10863 
10864     IF (p_resource_type IS NOT NULL) THEN
10865       OPEN  cur_resource_type;
10866       FETCH cur_resource_type INTO l_cur_resource_type;
10867       IF ( cur_resource_type%NOTFOUND) THEN
10868         fnd_message.set_name('JTF', 'JTF_AM_INVALID_RESOURCE_TYPE');
10869         fnd_msg_pub.add;
10870         RAISE fnd_api.g_exc_error;
10871       END IF;
10872       CLOSE cur_resource_type;
10873     END IF;
10874 
10875 
10876     /*
10877     IF (p_effort_uom IS NOT NULL) THEN
10878       OPEN  cur_effort_uom;
10879       FETCH cur_effort_uom INTO l_cur_effort_uom;
10880       IF ( cur_effort_uom%NOTFOUND) THEN
10881         fnd_message.set_name('JTF', 'JTF_AM_INVALID_EFFORT_UOM');
10882         fnd_msg_pub.add;
10883         RAISE fnd_api.g_exc_error;
10884       END IF;
10885       CLOSE cur_effort_uom;
10886     END IF;
10887     */
10888 
10889 
10890     /* Getting the Profile values defined for the Assignment Manager */
10891 
10892 
10893     /**************Start of commenting out by SBARAT on 12/10/2004, Bug-3830061***************/
10894 
10895     /*
10896     l_contracts_profile       := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_CONTRACTS_PREFERRED_ENGINEERS' );
10897     l_auto_select_profile     := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_AUTO_SELECT' );
10898     l_workflow_profile        := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_WORKFLOW_NAME' );
10899     l_ib_profile              := FND_PROFILE.VALUE_SPECIFIC ( 'ACTIVATE_IB_PREFERRED_ENGINEERS');
10900     */
10901 
10902     /**************End of commenting out by SBARAT on 12/10/2004, Bug-3830061**************/
10903 
10904     /**************Start of addition by SBARAT on 12/10/2004, Bug-3830061****************/
10905 
10906     l_contracts_profile       := FND_PROFILE.VALUE ( 'ACTIVATE_CONTRACTS_PREFERRED_ENGINEERS' );
10907     l_auto_select_profile     := FND_PROFILE.VALUE ( 'ACTIVATE_AUTO_SELECT' );
10908     l_workflow_profile        := FND_PROFILE.VALUE ( 'ACTIVATE_WORKFLOW_NAME' );
10909     l_ib_profile              := FND_PROFILE.VALUE ( 'ACTIVATE_IB_PREFERRED_ENGINEERS');
10910 
10911     /**************End of addition by SBARAT on 12/10/2004, Bug-3830061****************/
10912 
10913     -- get the profile value for usage
10914     l_usage                   := fnd_profile.value('JTF_AM_USAGE');
10915 
10916     /* Assigning the DEFAULT values for the Parameters */
10917 
10918 
10919     IF (p_auto_select_flag IS NULL) THEN
10920       l_auto_select_flag  := l_auto_select_profile;
10921                              -- PROFILE VALUE is the default value
10922     ELSE
10923       l_auto_select_flag  := p_auto_select_flag;
10924     END IF;
10925 
10926 
10927     IF (p_contracts_preferred_engineer IS NULL) THEN
10928       l_contracts_preferred_engineer  := l_contracts_profile;
10929                                          -- PROFILE VALUE is the default value
10930     ELSE
10931       l_contracts_preferred_engineer  := p_contracts_preferred_engineer;
10932     END IF;
10933 
10934 
10935     IF (p_ib_preferred_engineer IS NULL) THEN
10936       l_ib_preferred_engineer  := l_ib_profile;
10937                                   -- PROFILE VALUE is the default value
10938     ELSE
10939       l_ib_preferred_engineer  := p_ib_preferred_engineer;
10940     END IF;
10941 
10942 
10943     IF (p_no_of_resources IS NULL) THEN
10944       l_no_of_resources  := 1;  -- 1 is the default value
10945     ELSE
10946       l_no_of_resources  := p_no_of_resources;
10947     END IF;
10948 
10949 
10950     IF ( (UPPER(p_territory_flag) = 'N')
10951        -- added this line to handle null value as N
10952          OR (p_territory_flag IS NULL)) THEN
10953       l_territory_flag  := 'N';
10954     ELSE
10955       l_territory_flag  := 'Y';  -- YES is the default value
10956     END IF;
10957 
10958 
10959     IF ( (UPPER(p_calendar_flag) = 'N')
10960       -- added this line to handle null value as N
10961          OR (p_calendar_flag IS NULL)) THEN
10962       l_calendar_flag  := 'N';
10963     ELSE
10964       l_calendar_flag  := 'Y';  -- YES is the default value
10965     END IF;
10966 
10967 
10968     IF ( p_web_availability_flag IS NULL) THEN
10969       l_web_availability_flag  := 'Y';
10970     ELSE
10971       l_web_availability_flag  := p_web_availability_flag;
10972     END IF;
10973 
10974 
10975      /* to handle the conversion of duration to hour */
10976     l_uom_hour  := nvl(fnd_profile.value('JTF_AM_TASK_HOUR'), 'HR');
10977 
10978 
10979    --Commented out by SBARAT on 21/04/2005 for Bug-4300801
10980    --It was causing double conversion since same conversion is being done
10981    --in the overloaded procedure GET_AVAILABLE_RESOURCES
10982   /*if(nvl(p_effort_uom, l_uom_hour) <> l_uom_hour)
10983     then
10984          l_effort_duration :=  inv_convert.inv_um_convert(
10985                                    item_id => NULL,
10986                                    precision => 2,
10987                                    from_quantity => p_effort_duration,
10988                                    from_unit => p_effort_uom,
10989                                    to_unit   => l_uom_hour, --'HR',
10990                                    from_name => NULL,
10991                                    to_name   => NULL);
10992     else
10993         l_effort_duration := p_effort_duration;
10994     end if;*/
10995 
10996     l_effort_duration := p_effort_duration;    --Added by SBARAT on 21/04/2005 for Bug-4300801
10997 
10998     /* This assigning is being done because of the limitation for
10999        the direct use of the variables FND_API.MISS_NUM, MISS_CHAR etc. */
11000 
11001 
11002     /* Assigning values to the Service Request Record Type */
11003 
11004 /**************** Start of addition by SBARAT on 11/01/2005 for Enh 4112155**************/
11005 
11006     Terr_Qual_Dyn_Assign(p_sr_rec, p_sr_task_rec);
11007 
11008     l_sr_rec:=JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type;
11009     l_sr_task_rec:=JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type;
11010 
11011     JTF_ASSIGN_PUB.G_Terr_Serv_Req_Rec_Type:=Null;
11012     JTF_ASSIGN_PUB.G_Terr_Srv_Task_Rec_Type:=Null;
11013 
11014 /**************** End of addition by SBARAT on 11/01/2005 for Enh 4112155**************/
11015 
11016     l_sr_rec.SERVICE_REQUEST_ID   :=  p_sr_rec.SERVICE_REQUEST_ID;
11017     l_sr_rec.PARTY_ID             :=  p_sr_rec.PARTY_ID;
11018     l_sr_rec.COUNTRY              :=  p_sr_rec.COUNTRY;
11019     l_sr_rec.PARTY_SITE_ID        :=  p_sr_rec.PARTY_SITE_ID;
11020     l_sr_rec.CITY                 :=  p_sr_rec.CITY;
11021     l_sr_rec.POSTAL_CODE          :=  p_sr_rec.POSTAL_CODE;
11022     l_sr_rec.STATE                :=  p_sr_rec.STATE;
11023     l_sr_rec.AREA_CODE            :=  p_sr_rec.AREA_CODE;
11024     l_sr_rec.COUNTY               :=  p_sr_rec.COUNTY;
11025     l_sr_rec.COMP_NAME_RANGE      :=  p_sr_rec.COMP_NAME_RANGE;
11026     l_sr_rec.PROVINCE             :=  p_sr_rec.PROVINCE;
11027     l_sr_rec.NUM_OF_EMPLOYEES     :=  p_sr_rec.NUM_OF_EMPLOYEES;
11028     l_sr_rec.INCIDENT_TYPE_ID     :=  p_sr_rec.INCIDENT_TYPE_ID;
11029     l_sr_rec.INCIDENT_SEVERITY_ID :=  p_sr_rec.INCIDENT_SEVERITY_ID;
11030     l_sr_rec.INCIDENT_URGENCY_ID  :=  p_sr_rec.INCIDENT_URGENCY_ID;
11031     l_sr_rec.PROBLEM_CODE         :=  p_sr_rec.PROBLEM_CODE;
11032     l_sr_rec.INCIDENT_STATUS_ID   :=  p_sr_rec.INCIDENT_STATUS_ID;
11033     l_sr_rec.PLATFORM_ID          :=  p_sr_rec.PLATFORM_ID;
11034     l_sr_rec.SUPPORT_SITE_ID      :=  p_sr_rec.SUPPORT_SITE_ID;
11035     l_sr_rec.CUSTOMER_SITE_ID     :=  p_sr_rec.CUSTOMER_SITE_ID;
11036     l_sr_rec.SR_CREATION_CHANNEL  :=  p_sr_rec.SR_CREATION_CHANNEL;
11037     l_sr_rec.INVENTORY_ITEM_ID    :=  p_sr_rec.INVENTORY_ITEM_ID;
11038     l_sr_rec.ATTRIBUTE1           :=  p_sr_rec.ATTRIBUTE1;
11039     l_sr_rec.ATTRIBUTE2           :=  p_sr_rec.ATTRIBUTE2;
11040     l_sr_rec.ATTRIBUTE3           :=  p_sr_rec.ATTRIBUTE3;
11041     l_sr_rec.ATTRIBUTE4           :=  p_sr_rec.ATTRIBUTE4;
11042     l_sr_rec.ATTRIBUTE5           :=  p_sr_rec.ATTRIBUTE5;
11043     l_sr_rec.ATTRIBUTE6           :=  p_sr_rec.ATTRIBUTE6;
11044     l_sr_rec.ATTRIBUTE7           :=  p_sr_rec.ATTRIBUTE7;
11045     l_sr_rec.ATTRIBUTE8           :=  p_sr_rec.ATTRIBUTE8;
11046     l_sr_rec.ATTRIBUTE9           :=  p_sr_rec.ATTRIBUTE9;
11047     l_sr_rec.ATTRIBUTE10          :=  p_sr_rec.ATTRIBUTE10;
11048     l_sr_rec.ATTRIBUTE11          :=  p_sr_rec.ATTRIBUTE11;
11049     l_sr_rec.ATTRIBUTE12          :=  p_sr_rec.ATTRIBUTE12;
11050     l_sr_rec.ATTRIBUTE13          :=  p_sr_rec.ATTRIBUTE13;
11051     l_sr_rec.ATTRIBUTE14          :=  p_sr_rec.ATTRIBUTE14;
11052     l_sr_rec.ATTRIBUTE15          :=  p_sr_rec.ATTRIBUTE15;
11053     l_sr_rec.ORGANIZATION_ID      :=  p_sr_rec.ORGANIZATION_ID;
11054     l_sr_rec.SQUAL_NUM12          :=  p_sr_rec.SQUAL_NUM12;
11055     l_sr_rec.SQUAL_NUM13          :=  p_sr_rec.SQUAL_NUM13;
11056     l_sr_rec.SQUAL_NUM14          :=  p_sr_rec.SQUAL_NUM14;
11057     l_sr_rec.SQUAL_NUM15          :=  p_sr_rec.SQUAL_NUM15;
11058     l_sr_rec.SQUAL_NUM16          :=  p_sr_rec.SQUAL_NUM16;
11059     l_sr_rec.SQUAL_NUM17          :=  p_sr_rec.SQUAL_NUM17;
11060     l_sr_rec.SQUAL_NUM18          :=  p_sr_rec.SQUAL_NUM18;
11061     l_sr_rec.SQUAL_NUM19          :=  p_sr_rec.SQUAL_NUM19;
11062     l_sr_rec.SQUAL_NUM30          :=  p_sr_rec.SQUAL_NUM30;
11063     l_sr_rec.SQUAL_CHAR11         :=  p_sr_rec.SQUAL_CHAR11;
11064     l_sr_rec.SQUAL_CHAR12         :=  p_sr_rec.SQUAL_CHAR12;
11065     l_sr_rec.SQUAL_CHAR13         :=  p_sr_rec.SQUAL_CHAR13;
11066     l_sr_rec.DAY_OF_WEEK          :=  p_sr_rec.DAY_OF_WEEK;
11067     l_sr_rec.TIME_OF_DAY          :=  p_sr_rec.TIME_OF_DAY;
11068 
11069     -- added by sudarsana for terr rec type change
11070     l_sr_rec.SQUAL_CHAR20         :=  p_sr_rec.SQUAL_CHAR20;
11071     -- Added by vvuyyuru for Contracts Coverage Type
11072     l_sr_rec.SQUAL_CHAR21         :=  p_sr_rec.SQUAL_CHAR21;
11073 	-- code for system qualifier
11074 	l_sr_rec.SQUAL_NUM60         :=  p_sr_rec.SQUAL_NUM60;
11075 
11076 
11077 
11078     /* Assigning values to the Service Request + Task Record Type */
11079 
11080     l_sr_task_rec.TASK_ID              :=  p_sr_task_rec.TASK_ID;
11081     l_sr_task_rec.SERVICE_REQUEST_ID   :=  p_sr_task_rec.SERVICE_REQUEST_ID;
11082     l_sr_task_rec.PARTY_ID             :=  p_sr_task_rec.PARTY_ID;
11083     l_sr_task_rec.COUNTRY              :=  p_sr_task_rec.COUNTRY;
11084     l_sr_task_rec.PARTY_SITE_ID        :=  p_sr_task_rec.PARTY_SITE_ID;
11085     l_sr_task_rec.CITY                 :=  p_sr_task_rec.CITY;
11086     l_sr_task_rec.POSTAL_CODE          :=  p_sr_task_rec.POSTAL_CODE;
11087     l_sr_task_rec.STATE                :=  p_sr_task_rec.STATE;
11088     l_sr_task_rec.AREA_CODE            :=  p_sr_task_rec.AREA_CODE;
11089     l_sr_task_rec.COUNTY               :=  p_sr_task_rec.COUNTY;
11090     l_sr_task_rec.COMP_NAME_RANGE      :=  p_sr_task_rec.COMP_NAME_RANGE;
11091     l_sr_task_rec.PROVINCE             :=  p_sr_task_rec.PROVINCE;
11092     l_sr_task_rec.NUM_OF_EMPLOYEES     :=  p_sr_task_rec.NUM_OF_EMPLOYEES;
11093     l_sr_task_rec.TASK_TYPE_ID         :=  p_sr_task_rec.TASK_TYPE_ID;
11094     l_sr_task_rec.TASK_STATUS_ID       :=  p_sr_task_rec.TASK_STATUS_ID;
11095     l_sr_task_rec.TASK_PRIORITY_ID     :=  p_sr_task_rec.TASK_PRIORITY_ID;
11096     l_sr_task_rec.INCIDENT_TYPE_ID     :=  p_sr_task_rec.INCIDENT_TYPE_ID;
11097     l_sr_task_rec.INCIDENT_SEVERITY_ID :=  p_sr_task_rec.INCIDENT_SEVERITY_ID;
11098     l_sr_task_rec.INCIDENT_URGENCY_ID  :=  p_sr_task_rec.INCIDENT_URGENCY_ID;
11099     l_sr_task_rec.PROBLEM_CODE         :=  p_sr_task_rec.PROBLEM_CODE;
11100     l_sr_task_rec.INCIDENT_STATUS_ID   :=  p_sr_task_rec.INCIDENT_STATUS_ID;
11101     l_sr_task_rec.PLATFORM_ID          :=  p_sr_task_rec.PLATFORM_ID;
11102     l_sr_task_rec.SUPPORT_SITE_ID      :=  p_sr_task_rec.SUPPORT_SITE_ID;
11103     l_sr_task_rec.CUSTOMER_SITE_ID     :=  p_sr_task_rec.CUSTOMER_SITE_ID;
11104     l_sr_task_rec.SR_CREATION_CHANNEL  :=  p_sr_task_rec.SR_CREATION_CHANNEL;
11105     l_sr_task_rec.INVENTORY_ITEM_ID    :=  p_sr_task_rec.INVENTORY_ITEM_ID;
11106     l_sr_task_rec.ATTRIBUTE1           :=  p_sr_task_rec.ATTRIBUTE1;
11107     l_sr_task_rec.ATTRIBUTE2           :=  p_sr_task_rec.ATTRIBUTE2;
11108     l_sr_task_rec.ATTRIBUTE3           :=  p_sr_task_rec.ATTRIBUTE3;
11109     l_sr_task_rec.ATTRIBUTE4           :=  p_sr_task_rec.ATTRIBUTE4;
11110     l_sr_task_rec.ATTRIBUTE5           :=  p_sr_task_rec.ATTRIBUTE5;
11111     l_sr_task_rec.ATTRIBUTE6           :=  p_sr_task_rec.ATTRIBUTE6;
11112     l_sr_task_rec.ATTRIBUTE7           :=  p_sr_task_rec.ATTRIBUTE7;
11113     l_sr_task_rec.ATTRIBUTE8           :=  p_sr_task_rec.ATTRIBUTE8;
11114     l_sr_task_rec.ATTRIBUTE9           :=  p_sr_task_rec.ATTRIBUTE9;
11115     l_sr_task_rec.ATTRIBUTE10          :=  p_sr_task_rec.ATTRIBUTE10;
11116     l_sr_task_rec.ATTRIBUTE11          :=  p_sr_task_rec.ATTRIBUTE11;
11117     l_sr_task_rec.ATTRIBUTE12          :=  p_sr_task_rec.ATTRIBUTE12;
11118     l_sr_task_rec.ATTRIBUTE13          :=  p_sr_task_rec.ATTRIBUTE13;
11119     l_sr_task_rec.ATTRIBUTE14          :=  p_sr_task_rec.ATTRIBUTE14;
11120     l_sr_task_rec.ATTRIBUTE15          :=  p_sr_task_rec.ATTRIBUTE15;
11121     l_sr_task_rec.ORGANIZATION_ID      :=  p_sr_task_rec.ORGANIZATION_ID;
11122     l_sr_task_rec.SQUAL_NUM12          :=  p_sr_task_rec.SQUAL_NUM12;
11123     l_sr_task_rec.SQUAL_NUM13          :=  p_sr_task_rec.SQUAL_NUM13;
11124     l_sr_task_rec.SQUAL_NUM14          :=  p_sr_task_rec.SQUAL_NUM14;
11125     l_sr_task_rec.SQUAL_NUM15          :=  p_sr_task_rec.SQUAL_NUM15;
11126     l_sr_task_rec.SQUAL_NUM16          :=  p_sr_task_rec.SQUAL_NUM16;
11127     l_sr_task_rec.SQUAL_NUM17          :=  p_sr_task_rec.SQUAL_NUM17;
11128     l_sr_task_rec.SQUAL_NUM18          :=  p_sr_task_rec.SQUAL_NUM18;
11129     l_sr_task_rec.SQUAL_NUM19          :=  p_sr_task_rec.SQUAL_NUM19;
11130     l_sr_task_rec.SQUAL_NUM30          :=  p_sr_task_rec.SQUAL_NUM30;
11131     l_sr_task_rec.SQUAL_CHAR11         :=  p_sr_task_rec.SQUAL_CHAR11;
11132     l_sr_task_rec.SQUAL_CHAR12         :=  p_sr_task_rec.SQUAL_CHAR12;
11133     l_sr_task_rec.SQUAL_CHAR13         :=  p_sr_task_rec.SQUAL_CHAR13;
11134     -- added by sudarsana for terr rec type change
11135     l_sr_task_rec.SQUAL_CHAR20         :=  p_sr_task_rec.SQUAL_CHAR20;
11136     -- Added by vvuyyuru for Contracts Coverage Type
11137     l_sr_task_rec.SQUAL_CHAR21         :=  p_sr_task_rec.SQUAL_CHAR21;
11138     l_sr_task_rec.DAY_OF_WEEK          :=  p_sr_task_rec.DAY_OF_WEEK;
11139     l_sr_task_rec.TIME_OF_DAY          :=  p_sr_task_rec.TIME_OF_DAY;
11140 -- code for system qualifier
11141 	l_sr_task_rec.SQUAL_NUM60         :=   p_sr_task_rec.SQUAL_NUM60;
11142 
11143 
11144 
11145 
11146     /* Assigning values to the Defect Record Type */
11147 
11148     l_defect_rec.SQUAL_CHAR01          :=  p_defect_rec.SQUAL_CHAR01;
11149     l_defect_rec.SQUAL_CHAR02          :=  p_defect_rec.SQUAL_CHAR02;
11150     l_defect_rec.SQUAL_CHAR03          :=  p_defect_rec.SQUAL_CHAR03;
11151     l_defect_rec.SQUAL_CHAR04          :=  p_defect_rec.SQUAL_CHAR04;
11152     l_defect_rec.SQUAL_CHAR05          :=  p_defect_rec.SQUAL_CHAR05;
11153     l_defect_rec.SQUAL_CHAR06          :=  p_defect_rec.SQUAL_CHAR06;
11154     l_defect_rec.SQUAL_CHAR07          :=  p_defect_rec.SQUAL_CHAR07;
11155     l_defect_rec.SQUAL_CHAR08          :=  p_defect_rec.SQUAL_CHAR08;
11156     l_defect_rec.SQUAL_CHAR09          :=  p_defect_rec.SQUAL_CHAR09;
11157     l_defect_rec.SQUAL_CHAR10          :=  p_defect_rec.SQUAL_CHAR10;
11158     l_defect_rec.SQUAL_CHAR11          :=  p_defect_rec.SQUAL_CHAR11;
11159     l_defect_rec.SQUAL_CHAR12          :=  p_defect_rec.SQUAL_CHAR12;
11160     l_defect_rec.SQUAL_CHAR13          :=  p_defect_rec.SQUAL_CHAR13;
11161     l_defect_rec.SQUAL_CHAR14          :=  p_defect_rec.SQUAL_CHAR14;
11162     l_defect_rec.SQUAL_CHAR15          :=  p_defect_rec.SQUAL_CHAR15;
11163     l_defect_rec.SQUAL_CHAR16          :=  p_defect_rec.SQUAL_CHAR16;
11164     l_defect_rec.SQUAL_CHAR17          :=  p_defect_rec.SQUAL_CHAR17;
11165     l_defect_rec.SQUAL_CHAR18          :=  p_defect_rec.SQUAL_CHAR18;
11166     l_defect_rec.SQUAL_CHAR19          :=  p_defect_rec.SQUAL_CHAR19;
11167     l_defect_rec.SQUAL_CHAR20          :=  p_defect_rec.SQUAL_CHAR20;
11168     l_defect_rec.SQUAL_CHAR21          :=  p_defect_rec.SQUAL_CHAR21;
11169     l_defect_rec.SQUAL_CHAR22          :=  p_defect_rec.SQUAL_CHAR22;
11170     l_defect_rec.SQUAL_CHAR23          :=  p_defect_rec.SQUAL_CHAR23;
11171     l_defect_rec.SQUAL_CHAR24          :=  p_defect_rec.SQUAL_CHAR24;
11172     l_defect_rec.SQUAL_CHAR25          :=  p_defect_rec.SQUAL_CHAR25;
11173 
11174     l_defect_rec.SQUAL_NUM01           :=  p_defect_rec.SQUAL_NUM01;
11175     l_defect_rec.SQUAL_NUM02           :=  p_defect_rec.SQUAL_NUM02;
11176     l_defect_rec.SQUAL_NUM03           :=  p_defect_rec.SQUAL_NUM03;
11177     l_defect_rec.SQUAL_NUM04           :=  p_defect_rec.SQUAL_NUM04;
11178     l_defect_rec.SQUAL_NUM05           :=  p_defect_rec.SQUAL_NUM05;
11179     l_defect_rec.SQUAL_NUM06           :=  p_defect_rec.SQUAL_NUM06;
11180     l_defect_rec.SQUAL_NUM07           :=  p_defect_rec.SQUAL_NUM07;
11181     l_defect_rec.SQUAL_NUM08           :=  p_defect_rec.SQUAL_NUM08;
11182     l_defect_rec.SQUAL_NUM09           :=  p_defect_rec.SQUAL_NUM09;
11183     l_defect_rec.SQUAL_NUM10           :=  p_defect_rec.SQUAL_NUM10;
11184     l_defect_rec.SQUAL_NUM11           :=  p_defect_rec.SQUAL_NUM11;
11185     l_defect_rec.SQUAL_NUM12           :=  p_defect_rec.SQUAL_NUM12;
11186     l_defect_rec.SQUAL_NUM13           :=  p_defect_rec.SQUAL_NUM13;
11187     l_defect_rec.SQUAL_NUM14           :=  p_defect_rec.SQUAL_NUM14;
11188     l_defect_rec.SQUAL_NUM15           :=  p_defect_rec.SQUAL_NUM15;
11189     l_defect_rec.SQUAL_NUM16           :=  p_defect_rec.SQUAL_NUM16;
11190     l_defect_rec.SQUAL_NUM17           :=  p_defect_rec.SQUAL_NUM17;
11191     l_defect_rec.SQUAL_NUM18           :=  p_defect_rec.SQUAL_NUM18;
11192     l_defect_rec.SQUAL_NUM19           :=  p_defect_rec.SQUAL_NUM19;
11193     l_defect_rec.SQUAL_NUM20           :=  p_defect_rec.SQUAL_NUM20;
11194     l_defect_rec.SQUAL_NUM21           :=  p_defect_rec.SQUAL_NUM21;
11195     l_defect_rec.SQUAL_NUM22           :=  p_defect_rec.SQUAL_NUM22;
11196     l_defect_rec.SQUAL_NUM23           :=  p_defect_rec.SQUAL_NUM23;
11197     l_defect_rec.SQUAL_NUM24           :=  p_defect_rec.SQUAL_NUM24;
11198     l_defect_rec.SQUAL_NUM25           :=  p_defect_rec.SQUAL_NUM25;
11199 
11200     l_defect_rec.ATTRIBUTE1              :=  p_defect_rec.ATTRIBUTE1;
11201     l_defect_rec.ATTRIBUTE2              :=  p_defect_rec.ATTRIBUTE2;
11202     l_defect_rec.ATTRIBUTE3              :=  p_defect_rec.ATTRIBUTE3;
11203     l_defect_rec.ATTRIBUTE4              :=  p_defect_rec.ATTRIBUTE4;
11204     l_defect_rec.ATTRIBUTE5              :=  p_defect_rec.ATTRIBUTE5;
11205     l_defect_rec.ATTRIBUTE6              :=  p_defect_rec.ATTRIBUTE6;
11206     l_defect_rec.ATTRIBUTE7              :=  p_defect_rec.ATTRIBUTE7;
11207     l_defect_rec.ATTRIBUTE8              :=  p_defect_rec.ATTRIBUTE8;
11208     l_defect_rec.ATTRIBUTE9              :=  p_defect_rec.ATTRIBUTE9;
11209     l_defect_rec.ATTRIBUTE10             :=  p_defect_rec.ATTRIBUTE10;
11210     l_defect_rec.ATTRIBUTE11             :=  p_defect_rec.ATTRIBUTE11;
11211     l_defect_rec.ATTRIBUTE12             :=  p_defect_rec.ATTRIBUTE12;
11212     l_defect_rec.ATTRIBUTE13             :=  p_defect_rec.ATTRIBUTE13;
11213     l_defect_rec.ATTRIBUTE14             :=  p_defect_rec.ATTRIBUTE14;
11214     l_defect_rec.ATTRIBUTE15             :=  p_defect_rec.ATTRIBUTE15;
11215 
11216 
11217 
11218 
11219     /* Actual Flow of Assignment Manager
11220        Calls to be made now to get the qualified resources */
11221 
11222     IF ( UPPER ( p_calling_doc_type ) NOT IN (
11223                                                 'TASK',
11224                                                 'SR'  ,
11225                                                 'DEF'
11226                                              )
11227        ) THEN
11228 
11229       fnd_message.set_name('JTF', 'JTF_AM_INVALID_DOC_TYPE');
11230       fnd_msg_pub.add;
11231       RAISE fnd_api.g_exc_error;
11232 
11233     END IF;
11234 
11235 
11236     IF ( UPPER(l_calling_doc_type) = 'SR' ) THEN
11237       GET_ASSIGN_SR_RESOURCES
11238         (
11239           p_api_version                    => l_api_version,
11240           p_init_msg_list                  => p_init_msg_list,
11241           p_resource_type                  => p_resource_type,
11242           p_role                           => p_role,
11243           p_no_of_resources                => l_no_of_resources,
11244           p_auto_select_flag               => l_auto_select_flag,
11245           p_contracts_preferred_engineer   => l_contracts_preferred_engineer,
11246           p_ib_preferred_engineer          => l_ib_preferred_engineer,
11247           p_contract_id                    => p_contract_id,
11248           p_customer_product_id            => p_customer_product_id,
11249           p_effort_duration                => l_effort_duration, --p_effort_duration,
11250           p_effort_uom                     => p_effort_uom,
11251           p_start_date                     => p_start_date,
11252           p_end_date                       => p_end_date,
11253           p_territory_flag                 => l_territory_flag,
11254           p_calendar_flag                  => l_calendar_flag,
11255           p_web_availability_flag          => l_web_availability_flag,
11256           p_category_id                    => p_category_id,
11257           p_inventory_item_id              => p_inventory_item_id,
11258           p_inventory_org_id               => p_inventory_org_id,
11259           --Added for Bug # 5386560
11260 	  p_inventory_component_id         => p_inventory_component_id,
11261           --Added for Bug # 5386560 Ends here
11262 	  p_problem_code                   => p_problem_code ,
11263           p_sr_id                          => p_calling_doc_id,
11264           p_sr_rec                         => l_sr_rec,
11265           p_sr_task_rec                    => l_sr_task_rec,
11266           p_business_process_id            => p_business_process_id,
11267           p_business_process_date          => p_business_process_date,
11268           p_filter_excluded_resource       => p_filter_excluded_resource,
11269           x_assign_resources_tbl           => l_assign_resources_tbl,
11270           x_return_status                  => x_return_status,
11271           x_msg_count                      => x_msg_count,
11272           x_msg_data                       => x_msg_data,
11273 	  --Added for Bug # 5573916
11274 	  p_calendar_check                 => p_calendar_check
11275 	  --Added for Bug # 5573916 Ends here
11276         );
11277 
11278         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
11279           fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
11280           fnd_message.set_token('P_PROC_NAME','GET_ASSIGN_SR_RESOURCES');
11281           fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_RESOURCES');
11282           fnd_msg_pub.add;
11283           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
11284             RAISE fnd_api.g_exc_error;
11285           ELSE
11286             RAISE fnd_api.g_exc_unexpected_error;
11287           END IF;
11288         END IF;
11289 
11290       -- added this to filter by usage
11291       IF ((l_assign_resources_tbl.count > 0 ) AND
11292           (nvl(l_usage, fnd_api.g_miss_char)  <> 'ALL' ) AND
11293           (l_usage is not null)
11294          )
11295       THEN
11296           get_usage_resource(l_usage ,
11297                              l_assign_resources_tbl);
11298       END IF;
11299 
11300 
11301       IF l_assign_resources_tbl.COUNT > 0 THEN
11302 
11303         l_current_record := l_assign_resources_tbl.FIRST;
11304 
11305         IF ( UPPER(l_auto_select_flag) = 'Y' ) THEN
11306 
11307           --l_no_of_resources := l_assign_resources_tbl.count;
11308           -- added this condition to avoid pl/sql numeric error. if no of resources was greater than table count
11309           -- it still used to go into loop . 13 july 2004
11310             l_no_of_resources := least(nvl(l_assign_resources_tbl.count, 0),l_no_of_resources) ;
11311 
11312           --WHILE (l_current_record <= l_no_of_resources)
11313           WHILE (l_count < l_no_of_resources)
11314           LOOP
11315 
11316              -- add check to see whether the resource is end dated or not
11317           -- added by sudarsana 21 feb 02
11318           open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
11319                               l_assign_resources_tbl(l_current_record).resource_type);
11320           fetch check_date_cur into l_value;
11321           if (check_date_cur%found)
11322           then
11323 
11324             l_count := l_count + 1;
11325 
11326             x_assign_resources_tbl(l_count).terr_rsc_id           :=
11327                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
11328             x_assign_resources_tbl(l_count).resource_id           :=
11329                                    l_assign_resources_tbl(l_current_record).resource_id;
11330             x_assign_resources_tbl(l_count).resource_type         :=
11331                                    l_assign_resources_tbl(l_current_record).resource_type;
11332             x_assign_resources_tbl(l_count).role                  :=
11333                                    l_assign_resources_tbl(l_current_record).role;
11334             x_assign_resources_tbl(l_count).start_date            :=
11335                                    l_assign_resources_tbl(l_current_record).start_date;
11336             x_assign_resources_tbl(l_count).end_date              :=
11337                                    l_assign_resources_tbl(l_current_record).end_date;
11338             x_assign_resources_tbl(l_count).shift_construct_id    :=
11339                                    l_assign_resources_tbl(l_current_record).shift_construct_id;
11340             x_assign_resources_tbl(l_count).terr_id               :=
11341                                    l_assign_resources_tbl(l_current_record).terr_id;
11342             x_assign_resources_tbl(l_count).terr_name             :=
11343                                    l_assign_resources_tbl(l_current_record).terr_name;
11344 	    -- ================code added for bug 6453896=============
11345 	    x_assign_resources_tbl(l_count).terr_rank             :=
11346                                 l_assign_resources_tbl(l_current_record).terr_rank;
11347             -- ================End for addition of code===============
11348             x_assign_resources_tbl(l_count).preference_type       :=
11349                                    l_assign_resources_tbl(l_current_record).preference_type;
11350             x_assign_resources_tbl(l_count).primary_contact_flag  :=
11351                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
11352             x_assign_resources_tbl(l_count).group_id              :=
11353                                    l_assign_resources_tbl(l_current_record).group_id;
11354 
11355             x_assign_resources_tbl(l_count).support_site_id       :=
11356                                    l_assign_resources_tbl(l_current_record).support_site_id;
11357             x_assign_resources_tbl(l_count).support_site_name     :=
11358                                    l_assign_resources_tbl(l_current_record).support_site_name;
11359             x_assign_resources_tbl(l_count).web_availability_flag :=
11360                                    l_assign_resources_tbl(l_current_record).web_availability_flag;
11361 
11362             x_assign_resources_tbl(l_count).skill_level           :=
11363                                    l_assign_resources_tbl(l_current_record).skill_level;
11364             x_assign_resources_tbl(l_count).skill_name            :=
11365                                    l_assign_resources_tbl(l_current_record).skill_name;
11366             x_assign_resources_tbl(l_count).primary_flag            :=
11367                                    l_assign_resources_tbl(l_current_record).primary_flag;
11368             x_assign_resources_tbl(l_count).resource_source       :=
11369                                    l_assign_resources_tbl(l_current_record).resource_source;
11370             end if;
11371             close check_date_cur;
11372             l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
11373           END LOOP;
11374 
11375         ELSE  -- Auto Select Flag is NO
11376 
11377           WHILE l_current_record <= l_assign_resources_tbl.LAST
11378           LOOP
11379              -- add check to see whether the resource is end dated or not
11380              -- added by sudarsana 21 feb 02
11381              open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
11382                               l_assign_resources_tbl(l_current_record).resource_type);
11383              fetch check_date_cur into l_value;
11384              if (check_date_cur%found)
11385              then
11386                l_count := l_count + 1;
11387 
11388                x_assign_resources_tbl(l_count).terr_rsc_id           :=
11389                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
11390                x_assign_resources_tbl(l_count).resource_id           :=
11391                                    l_assign_resources_tbl(l_current_record).resource_id;
11392                x_assign_resources_tbl(l_count).resource_type         :=
11393                                    l_assign_resources_tbl(l_current_record).resource_type;
11394                x_assign_resources_tbl(l_count).role                  :=
11395                                    l_assign_resources_tbl(l_current_record).role;
11396                x_assign_resources_tbl(l_count).start_date            :=
11397                                    l_assign_resources_tbl(l_current_record).start_date;
11398                x_assign_resources_tbl(l_count).end_date              :=
11399                                    l_assign_resources_tbl(l_current_record).end_date;
11400                x_assign_resources_tbl(l_count).shift_construct_id    :=
11401                                    l_assign_resources_tbl(l_current_record).shift_construct_id;
11402                x_assign_resources_tbl(l_count).terr_id               :=
11403                                    l_assign_resources_tbl(l_current_record).terr_id;
11404                x_assign_resources_tbl(l_count).terr_name             :=
11405                                    l_assign_resources_tbl(l_current_record).terr_name;
11406 	       -- ================code added for bug 6453896=============
11407 	       x_assign_resources_tbl(l_count).terr_rank             :=
11408                                 l_assign_resources_tbl(l_current_record).terr_rank;
11409 	       -- ================End for addition of code===============
11410                x_assign_resources_tbl(l_count).preference_type       :=
11411                                    l_assign_resources_tbl(l_current_record).preference_type;
11412                x_assign_resources_tbl(l_count).primary_contact_flag  :=
11413                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
11414                x_assign_resources_tbl(l_count).group_id              :=
11415                                    l_assign_resources_tbl(l_current_record).group_id;
11416 
11417                x_assign_resources_tbl(l_count).support_site_id       :=
11418                                    l_assign_resources_tbl(l_current_record).support_site_id;
11419                x_assign_resources_tbl(l_count).support_site_name     :=
11420                                    l_assign_resources_tbl(l_current_record).support_site_name;
11421                x_assign_resources_tbl(l_count).web_availability_flag :=
11422                                    l_assign_resources_tbl(l_current_record).web_availability_flag;
11423 
11424                x_assign_resources_tbl(l_count).skill_level           :=
11425                                    l_assign_resources_tbl(l_current_record).skill_level;
11426                x_assign_resources_tbl(l_count).skill_name            :=
11427                                    l_assign_resources_tbl(l_current_record).skill_name;
11428                x_assign_resources_tbl(l_count).primary_flag            :=
11429                                    l_assign_resources_tbl(l_current_record).primary_flag;
11430                x_assign_resources_tbl(l_count).resource_source       :=
11431                                    l_assign_resources_tbl(l_current_record).resource_source;
11432                end if;
11433                close check_date_cur;
11434                l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
11435           END LOOP;
11436         END IF;   -- Auto Select Flag
11437 
11438       ELSE
11439         -- No resources returned from the Assignment Manager API for SERVICE REQUESTS
11440         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
11441         fnd_msg_pub.add;
11442 --        RAISE fnd_api.g_exc_error;
11443       END IF;
11444 
11445 
11446       -- raise workfow event
11447       -- workflow test
11448       begin
11449          jtf_assign_pub.g_assign_resources_tbl.delete;
11450          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
11451          jtf_am_wf_events_pub.assign_sr_resource
11452                     (P_API_VERSION           =>  1.0,
11453                      P_INIT_MSG_LIST         =>  'F',
11454                      P_COMMIT                =>  'F',
11455                      P_CONTRACT_ID           =>  p_contract_id   ,
11456                      P_CUSTOMER_PRODUCT_ID   =>  p_customer_product_id   ,
11457                      P_CATEGORY_ID           =>  p_category_id   ,
11458                      P_INVENTORY_ITEM_ID     =>  p_inventory_item_id   ,
11459                      P_INVENTORY_ORG_ID      =>  p_inventory_org_id   ,
11460 		     --Added for Bug # 5386560
11461 		     P_INVENTORY_COMPONENT_ID =>  p_inventory_component_id   ,
11462 		     --Added for Bug # 5386560 Ends here
11463                      P_PROBLEM_CODE          =>  p_problem_code ,
11464                      P_SR_REC                =>  p_sr_rec,
11465                      P_SR_TASK_REC           =>  p_sr_task_rec,
11466                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
11467                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
11468                      X_RETURN_STATUS         =>  l_wf_return_status,
11469                      X_MSG_COUNT             =>  l_wf_msg_count,
11470                      X_MSG_DATA              =>  l_wf_msg_data
11471                      );
11472 
11473 
11474          IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
11475             -- Unexpected Execution Error from call to assign_sr_resource
11476             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
11477             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
11478             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_RESOURCES');
11479             fnd_msg_pub.add;
11480             /* Not raising the errors as req by tele service team
11481              IF (x_return_status = fnd_api.g_ret_sts_error) THEN
11482               RAISE fnd_api.g_exc_error;
11483             ELSE
11484               RAISE fnd_api.g_exc_unexpected_error;
11485             END IF;
11486             */
11487         ELSE
11488             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
11489         END IF;
11490 
11491 
11492          exception
11493             when others then
11494                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
11495                fnd_message.set_token('P_SQLCODE',SQLCODE);
11496                fnd_message.set_token('P_SQLERRM',SQLERRM);
11497                fnd_message.set_token('P_API_NAME',l_api_name);
11498                FND_MSG_PUB.add;
11499       end;
11500 
11501       -- ================code added for bug 6453896==========================
11502       -- =============== This code is added for sorting table based on territory ranking===========
11503       l_sort_profile := nvl(fnd_profile.value('JTF_AM_SORT_TERR_RANK'),'N');
11504       If l_sort_profile ='Y'
11505       then
11506 		t_assign_resources_tbl :=x_assign_resources_tbl;
11507 		x_assign_resources_tbl.delete;
11508 
11509 		quick_sort_terr_rank
11510 		(
11511 		 t_assign_resources_tbl.FIRST,
11512 		 t_assign_resources_tbl.LAST,
11513 		 t_assign_resources_tbl
11514 		);
11515 		x_assign_resources_tbl:=t_assign_resources_tbl;
11516       End If;
11517       -- ================End for addition of code===============
11518 
11519     ELSIF ( UPPER(l_calling_doc_type) = 'TASK' ) THEN
11520 
11521       GET_ASSIGN_TASK_RESOURCES
11522         (
11523           p_api_version                    => l_api_version,
11524           p_init_msg_list                  => p_init_msg_list,
11525           p_resource_type                  => p_resource_type,
11526           p_role                           => p_role,
11527           p_no_of_resources                => l_no_of_resources,
11528           p_auto_select_flag               => l_auto_select_flag,
11529           p_contracts_preferred_engineer   => l_contracts_preferred_engineer,
11530           p_ib_preferred_engineer          => l_ib_preferred_engineer,
11531           p_effort_duration                => l_effort_duration, --p_effort_duration,
11532           p_effort_uom                     => p_effort_uom,
11533           p_start_date                     => p_start_date,
11534           p_end_date                       => p_end_date,
11535           p_territory_flag                 => l_territory_flag,
11536           p_calendar_flag                  => l_calendar_flag,
11537           p_web_availability_flag          => l_web_availability_flag,
11538           p_task_id                        => p_calling_doc_id,
11539           p_column_list                    => p_column_list,
11540           p_business_process_id            => p_business_process_id,
11541           p_business_process_date          => p_business_process_date,
11542           x_assign_resources_tbl           => l_assign_resources_tbl,
11543           x_return_status                  => x_return_status,
11544           x_msg_count                      => x_msg_count,
11545           x_msg_data                       => x_msg_data,
11546 	  --Added for Bug # 5573916
11547 	  p_calendar_check                 => p_calendar_check
11548 	  --Added for Bug # 5573916 Ends here
11549         );
11550 
11551         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
11552           fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
11553           fnd_message.set_token('P_PROC_NAME','GET_ASSIGN_TASK_RESOURCES');
11554           fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_RESOURCES');
11555           fnd_msg_pub.add;
11556           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
11557             RAISE fnd_api.g_exc_error;
11558           ELSE
11559             RAISE fnd_api.g_exc_unexpected_error;
11560           END IF;
11561         END IF;
11562 
11563       /* Begin code to implement the sorting of resources by postal code */
11564       -- added this to filter by usage
11565       IF ((l_assign_resources_tbl.count > 0 ) AND
11566           (nvl(l_usage, fnd_api.g_miss_char)  <> 'ALL' ) AND
11567           (l_usage is not null)
11568           --(l_usage is not null or l_usage <> 'ALL')
11569          )
11570       THEN
11571           get_usage_resource(l_usage ,
11572                              l_assign_resources_tbl);
11573       END IF;
11574 
11575       /* This LOOP is to add the resource postal code to the
11576          list of qualified resources */
11577 
11578       l_current_record   := l_assign_resources_tbl.FIRST;
11579 
11580       WHILE (l_current_record <= l_assign_resources_tbl.LAST)
11581       LOOP
11582 
11583         OPEN  cur_res_location(l_assign_resources_tbl(l_current_record).resource_id,
11584                                l_assign_resources_tbl(l_current_record).resource_type);
11585         FETCH cur_res_location INTO l_assign_resources_tbl(l_current_record).location;
11586         CLOSE cur_res_location;
11587 
11588         l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
11589       END LOOP;
11590 
11591 
11592 
11593       /* Calling the procedure to sort PL SQL table rows */
11594 
11595      /*
11596       quick_sort_resource_loc
11597       (
11598         l_assign_resources_tbl.FIRST,
11599         l_assign_resources_tbl.LAST,
11600         l_assign_resources_tbl
11601       );
11602      */
11603 
11604       /* End of the code for the sorting by Resource Location */
11605 
11606 
11607 
11608       IF l_assign_resources_tbl.COUNT > 0 THEN
11609 
11610         l_current_record := l_assign_resources_tbl.FIRST;
11611 
11612         IF ( UPPER(l_auto_select_flag) = 'Y' ) THEN
11613 
11614           -- added this condition to avoid pl/sql numeric error. if no of resources was greater than table count
11615           -- it still used to go into loop . 13 july 2004
11616             l_no_of_resources := least(nvl(l_assign_resources_tbl.count, 0),l_no_of_resources) ;
11617 
11618           --WHILE (l_current_record <= l_no_of_resources)
11619           WHILE (l_count < l_no_of_resources)
11620           LOOP
11621           -- add check to see whether the resource is end dated or not
11622           -- added by sudarsana 21 feb 02
11623           open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
11624                               l_assign_resources_tbl(l_current_record).resource_type);
11625           fetch check_date_cur into l_value;
11626           if (check_date_cur%found)
11627           then
11628             l_count := l_count + 1;
11629             x_assign_resources_tbl(l_count).terr_rsc_id           :=
11630                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
11631             x_assign_resources_tbl(l_count).resource_id           :=
11632                                    l_assign_resources_tbl(l_current_record).resource_id;
11633             x_assign_resources_tbl(l_count).resource_type         :=
11634                                    l_assign_resources_tbl(l_current_record).resource_type;
11635             x_assign_resources_tbl(l_count).role                  :=
11636                                    l_assign_resources_tbl(l_current_record).role;
11637             x_assign_resources_tbl(l_count).start_date            :=
11638                                    l_assign_resources_tbl(l_current_record).start_date;
11639             x_assign_resources_tbl(l_count).end_date              :=
11640                                    l_assign_resources_tbl(l_current_record).end_date;
11641             x_assign_resources_tbl(l_count).shift_construct_id    :=
11642                                    l_assign_resources_tbl(l_current_record).shift_construct_id;
11643             x_assign_resources_tbl(l_count).terr_id               :=
11644                                    l_assign_resources_tbl(l_current_record).terr_id;
11645             x_assign_resources_tbl(l_count).terr_name             :=
11646                                    l_assign_resources_tbl(l_current_record).terr_name;
11647 	    -- ================code added for bug 6453896=============
11648             x_assign_resources_tbl(l_count).terr_rank             :=
11649                                 l_assign_resources_tbl(l_current_record).terr_rank;
11650 	    -- ================End for addition of code===============
11651             x_assign_resources_tbl(l_count).preference_type       :=
11652                                    l_assign_resources_tbl(l_current_record).preference_type;
11653             x_assign_resources_tbl(l_count).primary_contact_flag  :=
11654                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
11655             x_assign_resources_tbl(l_count).location              :=
11656                                    l_assign_resources_tbl(l_current_record).location;
11657 
11658             x_assign_resources_tbl(l_count).support_site_id       :=
11659                                    l_assign_resources_tbl(l_current_record).support_site_id;
11660             x_assign_resources_tbl(l_count).support_site_name     :=
11661                                    l_assign_resources_tbl(l_current_record).support_site_name;
11662             x_assign_resources_tbl(l_count).web_availability_flag :=
11663                                    l_assign_resources_tbl(l_current_record).web_availability_flag;
11664             x_assign_resources_tbl(l_count).primary_flag            :=
11665                                    l_assign_resources_tbl(l_current_record).primary_flag;
11666             x_assign_resources_tbl(l_count).resource_source       :=
11667                                    l_assign_resources_tbl(l_current_record).resource_source;
11668 
11669 
11670            end if;
11671            close check_date_cur;
11672            l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
11673           END LOOP;
11674 
11675         ELSE  -- Auto Select Flag is NO
11676 
11677           WHILE l_current_record <= l_assign_resources_tbl.LAST
11678           LOOP
11679 
11680              -- add check to see whether the resource is end dated or not
11681           -- added by sudarsana 21 feb 02
11682           open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
11683                               l_assign_resources_tbl(l_current_record).resource_type);
11684           fetch check_date_cur into l_value;
11685           if (check_date_cur%found)
11686           then
11687             l_count := l_count + 1;
11688             x_assign_resources_tbl(l_count).terr_rsc_id           :=
11689                                    l_assign_resources_tbl(l_current_record).terr_rsc_id;
11690             x_assign_resources_tbl(l_count).resource_id           :=
11691                                    l_assign_resources_tbl(l_current_record).resource_id;
11692             x_assign_resources_tbl(l_count).resource_type         :=
11693                                    l_assign_resources_tbl(l_current_record).resource_type;
11694             x_assign_resources_tbl(l_count).role                  :=
11695                                    l_assign_resources_tbl(l_current_record).role;
11696             x_assign_resources_tbl(l_count).start_date            :=
11697                                    l_assign_resources_tbl(l_current_record).start_date;
11698             x_assign_resources_tbl(l_count).end_date              :=
11699                                    l_assign_resources_tbl(l_current_record).end_date;
11700             x_assign_resources_tbl(l_count).shift_construct_id    :=
11701                                    l_assign_resources_tbl(l_current_record).shift_construct_id;
11702             x_assign_resources_tbl(l_count).terr_id               :=
11703                                    l_assign_resources_tbl(l_current_record).terr_id;
11704             x_assign_resources_tbl(l_count).terr_name             :=
11705                                    l_assign_resources_tbl(l_current_record).terr_name;
11706 	    -- ================code added for bug 6453896=============
11707             x_assign_resources_tbl(l_count).terr_rank             :=
11708                                 l_assign_resources_tbl(l_current_record).terr_rank;
11709 	    -- ================End for addition of code===============
11710             x_assign_resources_tbl(l_count).preference_type       :=
11711                                    l_assign_resources_tbl(l_current_record).preference_type;
11712             x_assign_resources_tbl(l_count).primary_contact_flag  :=
11713                                    l_assign_resources_tbl(l_current_record).primary_contact_flag;
11714             x_assign_resources_tbl(l_count).location              :=
11715                                    l_assign_resources_tbl(l_current_record).location;
11716 
11717             x_assign_resources_tbl(l_count).support_site_id       :=
11718                                    l_assign_resources_tbl(l_current_record).support_site_id;
11719             x_assign_resources_tbl(l_count).support_site_name     :=
11720                                    l_assign_resources_tbl(l_current_record).support_site_name;
11721             x_assign_resources_tbl(l_count).web_availability_flag :=
11722                                    l_assign_resources_tbl(l_current_record).web_availability_flag;
11723             x_assign_resources_tbl(l_count).primary_flag            :=
11724                                    l_assign_resources_tbl(l_current_record).primary_flag;
11725             x_assign_resources_tbl(l_count).resource_source            :=
11726                                    l_assign_resources_tbl(l_current_record).resource_source;
11727             end if;
11728             close check_date_cur;
11729             l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
11730           END LOOP;
11731         END IF;   -- Auto Select Flag
11732 
11733       ELSE   -- No resources returned from the Assignment Manager API for TASKS
11734         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
11735         fnd_msg_pub.add;
11736 --        RAISE fnd_api.g_exc_error;
11737       END IF;
11738 
11739       -- raise workfow event
11740       -- workflow test
11741       Begin
11742          jtf_assign_pub.g_assign_resources_tbl.delete;
11743          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
11744          jtf_am_wf_events_pub.assign_task_resource
11745                     (P_API_VERSION           =>  1.0,
11746                      P_INIT_MSG_LIST         =>  'F',
11747                      P_COMMIT                =>  'F',
11748                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
11749                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
11750                      P_TASK_ID		   =>  p_calling_doc_id,
11751                      P_CONTRACT_ID           =>  p_contract_id   ,
11752                      P_CUSTOMER_PRODUCT_ID   =>  p_customer_product_id   ,
11753                      P_CATEGORY_ID           =>  p_category_id   ,
11754                      X_RETURN_STATUS         =>  l_wf_return_status,
11755                      X_MSG_COUNT             =>  l_wf_msg_count,
11756                      X_MSG_DATA              =>  l_wf_msg_data
11757                      );
11758 
11759 
11760         IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
11761             -- Unexpected Execution Error from call to assign_sr_resource
11762             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
11763             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
11764             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_RESOURCES');
11765             fnd_msg_pub.add;
11766 
11767         ELSE
11768 		x_assign_resources_tbl.delete;
11769             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
11770         END IF;
11771 
11772       Exception
11773             When OTHERS Then
11774                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
11775                fnd_message.set_token('P_SQLCODE',SQLCODE);
11776                fnd_message.set_token('P_SQLERRM',SQLERRM);
11777                fnd_message.set_token('P_API_NAME',l_api_name);
11778                FND_MSG_PUB.add;
11779       End;
11780 
11781       -- ================code added for bug 6453896==========================
11782       -- =============== This code is added for sorting table based on territory ranking===========
11783       l_sort_profile := nvl(fnd_profile.value('JTF_AM_SORT_TERR_RANK'),'N');
11784       If l_sort_profile ='Y'
11785       then
11786 		t_assign_resources_tbl :=x_assign_resources_tbl;
11787 		x_assign_resources_tbl.delete;
11788 
11789 		quick_sort_terr_rank
11790 		(
11791 		 t_assign_resources_tbl.FIRST,
11792 		 t_assign_resources_tbl.LAST,
11793 		 t_assign_resources_tbl
11794 		);
11795 		x_assign_resources_tbl:=t_assign_resources_tbl;
11796       End If;
11797       -- ================End for addition of code===============
11798 
11799     ELSIF ( UPPER(l_calling_doc_type) = 'DEF' ) THEN
11800       GET_ASSIGN_DEFECT_RESOURCES
11801         (
11802           p_api_version                    => l_api_version,
11803           p_init_msg_list                  => p_init_msg_list,
11804           p_resource_type                  => p_resource_type,
11805           p_role                           => p_role,
11806           p_no_of_resources                => l_no_of_resources,
11807           p_auto_select_flag               => l_auto_select_flag,
11808           p_effort_duration                => l_effort_duration, --p_effort_duration,
11809           p_effort_uom                     => p_effort_uom,
11810           p_start_date                     => p_start_date,
11811           p_end_date                       => p_end_date,
11812           p_territory_flag                 => l_territory_flag,
11813           p_calendar_flag                  => l_calendar_flag,
11814           p_defect_rec                     => l_defect_rec,
11815           p_business_process_id            => p_business_process_id,
11816           p_business_process_date          => p_business_process_date,
11817           x_assign_resources_tbl           => l_assign_resources_tbl,
11818           x_return_status                  => x_return_status,
11819           x_msg_count                      => x_msg_count,
11820           x_msg_data                       => x_msg_data
11821         );
11822 
11823         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
11824           fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
11825           fnd_message.set_token('P_PROC_NAME','GET_ASSIGN_DEFECT_RESOURCES');
11826           fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_RESOURCES');
11827           fnd_msg_pub.add;
11828           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
11829             RAISE fnd_api.g_exc_error;
11830           ELSE
11831             RAISE fnd_api.g_exc_unexpected_error;
11832           END IF;
11833         END IF;
11834 
11835       -- added this to filter by usage
11836       IF ((l_assign_resources_tbl.count > 0 ) AND
11837           (nvl(l_usage, fnd_api.g_miss_char)  <> 'ALL' ) AND
11838           (l_usage is not null)
11839           --(l_usage is not null or l_usage <> 'ALL')
11840          )
11841       THEN
11842           get_usage_resource(l_usage ,
11843                              l_assign_resources_tbl);
11844       END IF;
11845 
11846       IF l_assign_resources_tbl.COUNT > 0 THEN
11847 
11848 
11849         l_current_record := l_assign_resources_tbl.FIRST;
11850 
11851         -- added the autoselect check here after removing from the GET_ASSIGN_DEFECT_RESOURCES api
11852         -- on 29th september 2003
11853          IF (p_auto_select_flag = 'Y') THEN
11854               l_no_of_resources  := p_no_of_resources;
11855          ELSE
11856               l_no_of_resources  := l_assign_resources_tbl.LAST;
11857          END IF;
11858 
11859         -- added processing with l_count to fix defect bug 2490634
11860         -- on 6th aug 2002
11861         x_assign_resources_tbl.delete;
11862         l_count := l_current_record;
11863         WHILE l_current_record <= l_assign_resources_tbl.LAST
11864         LOOP
11865 
11866           IF(l_count <= l_no_of_resources)
11867           THEN
11868           -- add check to see whether the resource is end dated or not
11869           -- added by sudarsana 21 feb 02
11870              open check_date_cur(l_assign_resources_tbl(l_current_record).resource_id,
11871                               l_assign_resources_tbl(l_current_record).resource_type);
11872              fetch check_date_cur into l_value;
11873              if (check_date_cur%found)
11874              then
11875                 x_assign_resources_tbl(l_count).terr_rsc_id           :=
11876                                  l_assign_resources_tbl(l_current_record).terr_rsc_id;
11877                 x_assign_resources_tbl(l_count).resource_id           :=
11878                                  l_assign_resources_tbl(l_current_record).resource_id;
11879                 x_assign_resources_tbl(l_count).resource_type         :=
11880                                  l_assign_resources_tbl(l_current_record).resource_type;
11881 
11882                 x_assign_resources_tbl(l_count).start_date            :=
11883                                  l_assign_resources_tbl(l_current_record).start_date;
11884                 x_assign_resources_tbl(l_count).end_date              :=
11885                                  l_assign_resources_tbl(l_current_record).end_date;
11886                 x_assign_resources_tbl(l_count).shift_construct_id    :=
11887                                  l_assign_resources_tbl(l_current_record).shift_construct_id;
11888 
11889                 x_assign_resources_tbl(l_count).role                  :=
11890                                  l_assign_resources_tbl(l_current_record).role;
11891                 x_assign_resources_tbl(l_count).preference_type       :=
11892                                  l_assign_resources_tbl(l_current_record).preference_type;
11893                 x_assign_resources_tbl(l_count).primary_contact_flag  :=
11894                                  l_assign_resources_tbl(l_current_record).primary_contact_flag;
11895 
11896                 x_assign_resources_tbl(l_count).terr_id               :=
11897                                  l_assign_resources_tbl(l_current_record).terr_id;
11898                 x_assign_resources_tbl(l_count).terr_name             :=
11899                                  l_assign_resources_tbl(l_current_record).terr_name;
11900                 x_assign_resources_tbl(l_count).terr_rank             :=
11901                                  l_assign_resources_tbl(l_current_record).terr_rank;
11902                 x_assign_resources_tbl(l_count).primary_flag            :=
11903                                    l_assign_resources_tbl(l_current_record).primary_flag;
11904                 x_assign_resources_tbl(l_count).resource_source            :=
11905                                    l_assign_resources_tbl(l_current_record).resource_source;
11906                 l_count := l_count + 1;
11907              end if; -- end of check_date_cur found check
11908              close check_date_cur;
11909           END IF; -- end of check l_count against l_no_of_resources
11910           l_current_record := l_assign_resources_tbl.NEXT(l_current_record);
11911         END LOOP;
11912 
11913       ELSE   -- No resources returned from the Assignment Manager API for DEFECTS
11914         fnd_message.set_name('JTF', 'JTF_AM_NO_RESOURCES_FOUND');
11915         fnd_msg_pub.add;
11916 --        RAISE fnd_api.g_exc_error;
11917       END IF;
11918 
11919       -- raise workfow event
11920       -- workflow test
11921       Begin
11922          jtf_assign_pub.g_assign_resources_tbl.delete;
11923          jtf_assign_pub.g_assign_resources_tbl := x_assign_resources_tbl;
11924          jtf_am_wf_events_pub.assign_def_resource
11925                     (P_API_VERSION           =>  1.0,
11926                      P_INIT_MSG_LIST         =>  'F',
11927                      P_COMMIT                =>  'F',
11928                      P_CONTRACT_ID           =>  p_contract_id   ,
11929                      P_CUSTOMER_PRODUCT_ID   =>  p_customer_product_id   ,
11930                      P_CATEGORY_ID           =>  p_category_id   ,
11931                      P_DEF_MGMT_REC          =>  p_defect_rec,
11932                      P_BUSINESS_PROCESS_ID   =>  p_business_process_id,
11933                      P_BUSINESS_PROCESS_DATE =>  p_business_process_date,
11934                      X_RETURN_STATUS         =>  l_wf_return_status,
11935                      X_MSG_COUNT             =>  l_wf_msg_count,
11936                      X_MSG_DATA              =>  l_wf_msg_data
11937                      );
11938 
11939 
11940         IF NOT (l_wf_return_status = fnd_api.g_ret_sts_success) THEN
11941             -- Unexpected Execution Error from call to assign_sr_resource
11942             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
11943             fnd_message.set_token('P_PROC_NAME','JTF_AM_WF_EVENTS_PUB');
11944             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_RESOURCES');
11945             fnd_msg_pub.add;
11946 
11947         ELSE
11948 		x_assign_resources_tbl.delete;
11949             x_assign_resources_tbl := jtf_assign_pub.g_assign_resources_tbl;
11950         END IF;
11951 
11952 
11953       Exception
11954             When OTHERS Then
11955                fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
11956                fnd_message.set_token('P_SQLCODE',SQLCODE);
11957                fnd_message.set_token('P_SQLERRM',SQLERRM);
11958                fnd_message.set_token('P_API_NAME',l_api_name);
11959                FND_MSG_PUB.add;
11960       End;
11961 
11962 
11963     END IF;  -- End of UPPER(l_calling_doc_type)= 'SR'- 'TASK'- 'DEF'
11964 
11965 
11966 
11967     -- To Plugin the Workflow enabling the user
11968     -- to further filter the resources
11969 
11970     SELECT jtf_calendars_s.NEXTVAL INTO l_workflow_key
11971     FROM   dual;
11972 
11973     IF (JTF_USR_HKS.ok_to_execute
11974           (
11975             'JTF_ASSIGN_PUB',
11976             'GET_ASSIGN_RESOURCES',
11977             'W',
11978             'W'
11979           )
11980        ) THEN
11981 
11982       IF (JTF_ASSIGN_CUHK.ok_to_launch_workflow
11983             (
11984               p_api_version     => l_api_version,
11985               p_init_msg_list   => p_init_msg_list,
11986               x_return_status   => x_return_status,
11987               x_msg_count       => x_msg_count,
11988               x_msg_data        => x_msg_data
11989             )
11990          ) THEN
11991 
11992 
11993         IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
11994           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
11995           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
11996           fnd_msg_pub.add;
11997           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
11998             RAISE fnd_api.g_exc_error;
11999           ELSE
12000             RAISE fnd_api.g_exc_unexpected_error;
12001           END IF;
12002         END IF;
12003 
12004 
12005         l_bind_data_id := JTF_USR_HKS.get_bind_data_id;
12006 
12007         JTF_USR_HKS.WrkFlowLaunch
12008           (
12009             'JTF_ASSIGN_WF',
12010             l_workflow_profile,
12011             'ASSIGN WF - '|| TO_CHAR(l_workflow_key),
12012             l_bind_data_id,
12013             l_return_code
12014           );
12015 
12016         JTF_USR_HKS.purge_bind_data
12017           (
12018             l_bind_data_id,
12019             'W'
12020           );
12021 
12022 
12023         IF (l_return_code = fnd_api.g_ret_sts_error) THEN
12024           -- Execution Error from call to Assignment Manager Workflow Hook
12025           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
12026           fnd_msg_pub.add;
12027           RAISE fnd_api.g_exc_error;
12028         ELSIF (l_return_code = fnd_api.g_ret_sts_unexp_error) THEN
12029           -- Unexpected Execution Error from call to Assignment Manager Workflow Hook
12030           fnd_message.set_name('JTF', 'JTF_AM_ERROR_WF_API');
12031           fnd_msg_pub.add;
12032           RAISE fnd_api.g_exc_unexpected_error;
12033         END IF;
12034 
12035       END IF;  -- End of JTF_ASSIGN_CUHK
12036 
12037     END IF;    -- End of JTF_USR_HKS
12038 
12039 
12040     /* Standard call to get message count and
12041        the message information */
12042 
12043     FND_MSG_PUB.Count_And_Get
12044     (
12045       p_count => x_msg_count,
12046       p_data  => x_msg_data
12047     );
12048 
12049 
12050     x_return_status := fnd_api.g_ret_sts_success;
12051 
12052 
12053 
12054   EXCEPTION
12055     WHEN FND_API.G_EXC_ERROR THEN
12056       x_return_status := FND_API.G_RET_STS_ERROR ;
12057       FND_MSG_PUB.Count_And_Get
12058       (
12059         p_count => x_msg_count,
12060         p_data  => x_msg_data
12061       );
12062 
12063     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12064       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12065       FND_MSG_PUB.Count_And_Get
12066       (
12067         p_count => x_msg_count,
12068         p_data  => x_msg_data
12069       );
12070 
12071     WHEN OTHERS THEN
12072       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
12073       fnd_message.set_token('P_SQLCODE',SQLCODE);
12074       fnd_message.set_token('P_SQLERRM',SQLERRM);
12075       fnd_message.set_token('P_API_NAME',l_api_name);
12076       FND_MSG_PUB.add;
12077 
12078       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12079       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
12080         FND_MSG_PUB.Add_Exc_Msg
12081         (
12082           G_PKG_NAME,
12083           l_api_name
12084         );
12085       END IF;
12086 
12087       FND_MSG_PUB.Count_And_Get
12088       (
12089         p_count => x_msg_count,
12090         p_data  => x_msg_data
12091        );
12092 
12093   END GET_ASSIGN_RESOURCES;
12094 
12095  -- this is a procedure added on 2nd July 2002 to get the Excluded Resources for the AM UI
12096  -- when working in assisted Mode
12097  PROCEDURE GET_EXCLUDED_RESOURCES
12098     (   p_api_version                         IN  NUMBER,
12099         p_init_msg_list                       IN  VARCHAR2 DEFAULT JTF_ASSIGN_PUB.AM_FALSE,
12100         p_commit                              IN  VARCHAR2 DEFAULT JTF_ASSIGN_PUB.AM_FALSE,
12101         p_contract_id                         IN  NUMBER   DEFAULT NULL,
12102         p_customer_product_id                 IN  NUMBER   DEFAULT NULL,
12103         p_calling_doc_id                      IN  NUMBER,
12104         p_calling_doc_type                    IN  VARCHAR2,
12105         p_sr_rec                              IN  JTF_ASSIGN_PUB.JTF_Serv_Req_rec_type DEFAULT pkg_sr_rec,
12106         p_sr_task_rec                         IN  JTF_ASSIGN_PUB.JTF_Srv_Task_rec_type DEFAULT pkg_sr_task_rec,
12107         p_dr_rec                              IN  JTF_ASSIGN_PUB.JTF_DR_rec_type DEFAULT pkg_dr_rec, --Added by SBARAT on  01/11/2004 for Enh-3919046
12108         p_business_process_id                 IN  NUMBER,
12109         p_business_process_date               IN  DATE,
12110         x_excluded_resouurce_tbl              OUT NOCOPY JTF_ASSIGN_PUB.excluded_tbl_type,
12111         x_return_status                       OUT NOCOPY VARCHAR2,
12112         x_msg_count                           OUT NOCOPY NUMBER,
12113         x_msg_data                            OUT NOCOPY VARCHAR2
12114     )
12115  IS
12116    l_return_status_1                     VARCHAR2(10);
12117    l_api_name                            VARCHAR2(100)  := 'GET_EXCLUDED_RESOURCES';
12118    l_api_name_1                          VARCHAR2(60)  := 'GET_EXCLUDED_RESOURCES';
12119    l_api_version                         NUMBER        := 1.0;
12120 
12121     -- tables to habdle excluded resource feature
12122     l_excluded_resource_tbl               JTF_ASSIGN_PUB.excluded_tbl_type;
12123     l_contracts_tbl                       JTF_ASSIGN_PUB.AssignResources_tbl_type;
12124     l_ib_tbl                              JTF_ASSIGN_PUB.AssignResources_tbl_type;
12125 
12126     l_contract_id                         NUMBER := p_contract_id;
12127     l_cp_id                               NUMBER := p_customer_product_id;
12128 
12129     l_sr_id                               NUMBER;
12130     l_task_id                             NUMBER;
12131     l_dr_id                               NUMBER;
12132     l_task_source_code                    JTF_TASKS_VL.SOURCE_OBJECT_TYPE_CODE%TYPE;
12133     l_task_source_id                      JTF_TASKS_VL.SOURCE_OBJECT_ID%TYPE;
12134 
12135     l_return_status                       VARCHAR2(10);
12136     l_msg_count                           NUMBER;
12137     l_msg_data                            VARCHAR2(2000);
12138 
12139      l_dynamic_sql1                        VARCHAR2(2000);
12140 
12141      TYPE DYNAMIC_CUR_TYP   IS REF CURSOR;
12142      cur_cs_contacts   DYNAMIC_CUR_TYP;
12143      cur_cs_incidents  DYNAMIC_CUR_TYP;
12144 
12145       CURSOR cur_task_id IS
12146       SELECT source_object_type_code,
12147              source_object_id,
12148              planned_start_date,
12149              planned_end_date,
12150              planned_effort,
12151              planned_effort_uom
12152       FROM   jtf_tasks_vl
12153       WHERE  task_id = l_task_id;
12154 
12155       l_cur_task_id cur_task_id%ROWTYPE;
12156 
12157 BEGIN
12158 
12159     /* Standard call to check for call compatibility */
12160 
12161     IF NOT fnd_api.compatible_api_call (l_api_version,
12162                                         p_api_version,
12163                                         l_api_name,
12164                                         g_pkg_name) THEN
12165       RAISE fnd_api.g_exc_unexpected_error;
12166     END IF;
12167 
12168 
12169     /* Initialize message list if p_init_msg_list is set to TRUE */
12170 
12171     IF fnd_api.to_boolean (p_init_msg_list) THEN
12172       fnd_msg_pub.initialize;
12173     END IF;
12174 
12175     x_return_status := fnd_api.g_ret_sts_success;
12176 
12177     -- If p_document_type = TASK then get the contract_service_id and the customer_product_id from the table
12178     IF(p_calling_doc_type = 'TASK')
12179     THEN
12180       l_task_id := p_calling_doc_id;
12181       If(l_task_id IS NOT NULL)
12182       -- this has been added as in form startup we now do a autoquery. So if no task id is passed instead of throwing the
12183       -- message that invalid id has been passed in we will just not do any processing
12184       THEN
12185          OPEN  cur_task_id;
12186          FETCH cur_task_id INTO l_cur_task_id;
12187          IF  ( cur_task_id%NOTFOUND )
12188          THEN
12189              null;
12190          ELSE
12191              l_task_source_code    := l_cur_task_id.source_object_type_code;
12192              l_task_source_id      := l_cur_task_id.source_object_id;
12193          END IF;
12194          CLOSE cur_task_id;
12195 
12196 
12197          IF (l_task_source_id IS NOT NULL AND l_task_source_code = 'SR')
12198          THEN
12199               l_dynamic_sql1 :=  ' SELECT contract_service_id, customer_product_id'||
12200                                  ' FROM   cs_incidents_all_vl'||
12201                                  ' WHERE  incident_id = :1';
12202 
12203               OPEN  cur_cs_incidents FOR  l_dynamic_sql1 USING l_task_source_id;
12204               FETCH cur_cs_incidents INTO l_contract_id,
12205                                           l_cp_id;
12206           END IF;
12207 
12208        ELSIF(p_calling_doc_type = 'SR')
12209          -- If document type = SR then get the contract_service_id and customer_product_id from the parameters if passed
12210          -- Else get it from the cs_incidents_all table
12211        THEN
12212        l_sr_id       := p_calling_doc_id;
12213        l_contract_id := p_contract_id;
12214        l_cp_id       := p_customer_product_id;
12215         -- Code to fetch the Preferred Resources for saved SR
12216          IF (l_contract_id IS NULL AND
12217              l_cp_id       IS NULL AND
12218              l_sr_id       IS NOT NULL) THEN
12219 
12220             l_dynamic_sql1 :=  ' SELECT contract_service_id, customer_product_id'||
12221                                ' FROM   cs_incidents_all_vl'||
12222                                ' WHERE  incident_id = :1';
12223 
12224             OPEN  cur_cs_incidents FOR  l_dynamic_sql1 USING l_sr_id;
12225             FETCH cur_cs_incidents INTO l_contract_id,
12226                                         l_cp_id;
12227 
12228             IF ( cur_cs_incidents%NOTFOUND ) THEN
12229                null;
12230             END IF;
12231 
12232             CLOSE cur_cs_incidents;
12233           END IF;  -- end of l_contract_id and l_cp_id null check
12234 
12235        /********** Added by SBARAT on 01/11/2004 for Enh-3919046 ***********/
12236 
12237        ELSIF(p_calling_doc_type = 'DR')
12238          -- If document type = DR then get the contract_service_id and customer_product_id from the parameters if passed
12239          -- Else get it from the cs_incidents_all table
12240        THEN
12241        l_dr_id       := p_calling_doc_id;
12242        l_contract_id := p_contract_id;
12243        l_cp_id       := p_customer_product_id;
12244         -- Code to fetch the Preferred Resources for saved DR
12245          IF (l_contract_id IS NULL AND
12246              l_cp_id       IS NULL AND
12247              l_dr_id       IS NOT NULL) THEN
12248 
12249             l_dynamic_sql1 :=  ' SELECT contract_service_id, customer_product_id'||
12250                                ' FROM   cs_incidents_all_vl'||
12251                                ' WHERE  incident_id = :1';
12252 
12253             OPEN  cur_cs_incidents FOR  l_dynamic_sql1 USING l_dr_id;
12254             FETCH cur_cs_incidents INTO l_contract_id,
12255                                         l_cp_id;
12256 
12257             IF ( cur_cs_incidents%NOTFOUND ) THEN
12258                null;
12259             END IF;
12260 
12261             CLOSE cur_cs_incidents;
12262           END IF;  -- end of l_contract_id and l_cp_id null check
12263 
12264        /********* End of addition by SBARAT on 01/11/2004 for Enh-3919046 ********/
12265 
12266        END IF;
12267 
12268     END IF; -- end of calling doc type check
12269 
12270 
12271 
12272   -- call the get_contract_resources and get_ib_resources to get the excluded resources
12273   IF(l_contract_id is not null)
12274   THEN
12275          get_contracts_resources
12276           (
12277             p_init_msg_list           =>  p_init_msg_list,
12278             p_contract_id             =>  l_contract_id,
12279             p_calendar_flag           =>  'N',
12280             p_effort_duration         =>  null,
12281             p_effort_uom              =>  null,
12282             p_planned_start_date      =>  null,
12283             p_planned_end_date        =>  null,
12284             p_resource_type           =>  null,
12285             p_business_process_id     =>  p_business_process_id,
12286             p_business_process_date   =>  p_business_process_date,
12287             x_return_status           =>  x_return_status,
12288             x_msg_count               =>  x_msg_count,
12289             x_msg_data                =>  x_msg_data,
12290             x_assign_resources_tbl    =>  l_contracts_tbl,
12291             x_excluded_tbl            =>  l_excluded_resource_tbl
12292           );
12293 
12294           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
12295             -- Unexpected Execution Error from call to Get_contracts_resources
12296             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
12297             fnd_message.set_token('P_PROC_NAME','GET_CONTRACTS_RESOURCES');
12298             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_SR_RESOURCES');
12299             fnd_msg_pub.add;
12300             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
12301               RAISE fnd_api.g_exc_error;
12302             ELSE
12303               RAISE fnd_api.g_exc_unexpected_error;
12304             END IF;
12305           END IF;
12306    END IF;
12307 
12308 
12309    IF(l_cp_id is not null)
12310    THEN
12311        get_ib_resources
12312             (
12313               p_init_msg_list           =>  p_init_msg_list,
12314               p_customer_product_id     =>  l_cp_id,
12315               p_calendar_flag           =>  'N',
12316               p_effort_duration         =>  null,
12317               p_effort_uom              =>  null,
12318               p_planned_start_date      =>  null,
12319               p_planned_end_date        =>  null,
12320               p_resource_type           =>  null,
12321               x_return_status           =>  x_return_status,
12322               x_msg_count               =>  x_msg_count,
12323               x_msg_data                =>  x_msg_data,
12324               x_assign_resources_tbl    =>  l_ib_tbl,
12325               x_excluded_tbl            =>  l_excluded_resource_tbl
12326             );
12327 
12328           IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
12329             -- Unexpected Execution Error from call to Get_contracts_resources
12330             fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
12331             fnd_message.set_token('P_PROC_NAME','GET_IB_RESOURCES');
12332             fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_ASSIGN_TASK_RESOURCES');
12333             fnd_msg_pub.add;
12334             IF (x_return_status = fnd_api.g_ret_sts_error) THEN
12335               RAISE fnd_api.g_exc_error;
12336             ELSE
12337               RAISE fnd_api.g_exc_unexpected_error;
12338             END IF;
12339           END IF;
12340     END IF;
12341 
12342 
12343    -- assign the excluded resources to the out table
12344     x_excluded_resouurce_tbl.delete;
12345     x_excluded_resouurce_tbl := l_excluded_resource_tbl;
12346 
12347    EXCEPTION
12348     WHEN FND_API.G_EXC_ERROR THEN
12349       x_return_status := FND_API.G_RET_STS_ERROR ;
12350       FND_MSG_PUB.Count_And_Get
12351       (
12352         p_count => x_msg_count,
12353         p_data  => x_msg_data
12354       );
12355 
12356     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12357       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12358       FND_MSG_PUB.Count_And_Get
12359       (
12360         p_count => x_msg_count,
12361         p_data  => x_msg_data
12362       );
12363 
12364     WHEN OTHERS THEN
12365       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
12366       fnd_message.set_token('P_SQLCODE',SQLCODE);
12367       fnd_message.set_token('P_SQLERRM',SQLERRM);
12368       fnd_message.set_token('P_API_NAME',l_api_name);
12369       FND_MSG_PUB.add;
12370 
12371       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12372       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
12373         FND_MSG_PUB.Add_Exc_Msg
12374         (
12375           G_PKG_NAME,
12376           l_api_name
12377         );
12378       END IF;
12379 
12380       FND_MSG_PUB.Count_And_Get
12381       (
12382         p_count => x_msg_count,
12383         p_data  => x_msg_data
12384        );
12385  END  GET_EXCLUDED_RESOURCES;
12386 
12387 
12388  -- this is a wrapper for get_available_resource
12389  -- this is to be used only from AM UI to get the available slots for the resources fetched in
12390  -- Unassisted mode
12391  PROCEDURE GET_RESOURCE_AVAILABILITY
12392             ( p_api_version                   IN  NUMBER,
12393               p_init_msg_list                 IN  VARCHAR2 DEFAULT JTF_ASSIGN_PUB.AM_FALSE,
12394               p_commit                        IN  VARCHAR2 DEFAULT JTF_ASSIGN_PUB.AM_FALSE,
12395               p_calendar_flag                 IN  VARCHAR2,
12396               p_effort_duration               IN  NUMBER,
12397               p_effort_uom                    IN  VARCHAR2,
12398               p_breakdown                     IN  NUMBER,
12399               p_breakdown_uom                 IN  VARCHAR2,
12400               p_planned_start_date            IN  DATE,
12401               p_planned_end_date              IN  DATE,
12402               p_continuous_task               IN  VARCHAR2 DEFAULT 'N',
12403               x_return_status                 IN  OUT NOCOPY VARCHAR2,
12404               x_msg_count                     IN  OUT NOCOPY NUMBER,
12405               x_msg_data                      IN  OUT NOCOPY VARCHAR2,
12406               x_assign_resources_tbl          IN  OUT NOCOPY JTF_ASSIGN_PUB.AssignResources_tbl_type
12407             )
12408  IS
12409   l_return_status_1                     VARCHAR2(10);
12410    l_api_name                            VARCHAR2(100)  := 'GET_EXCLUDED_RESOURCES';
12411    l_api_name_1                          VARCHAR2(60)  := 'GET_EXCLUDED_RESOURCES';
12412    l_api_version                         NUMBER        := 1.0;
12413 
12414  BEGIN
12415    /* Standard call to check for call compatibility */
12416     IF NOT fnd_api.compatible_api_call (l_api_version,
12417                                         p_api_version,
12418                                         l_api_name,
12419                                         g_pkg_name)
12420     THEN
12421          RAISE fnd_api.g_exc_unexpected_error;
12422     END IF;
12423 
12424 
12425     /* Initialize message list if p_init_msg_list is set to TRUE */
12426     IF fnd_api.to_boolean (p_init_msg_list)
12427     THEN
12428       fnd_msg_pub.initialize;
12429     END IF;
12430 
12431     x_return_status := fnd_api.g_ret_sts_success;
12432 
12433     l_api_name := l_api_name||'-GET_AVAILABLE_RESOURCE';
12434     l_return_status_1 := x_return_status ;
12435     -- call the api to check resource availability
12436      get_available_resources
12437       (
12438        p_init_msg_list                 =>  'F',
12439        p_calendar_flag                 =>   p_calendar_flag,
12440        p_effort_duration               =>   p_effort_duration,
12441        p_effort_uom                    =>   p_effort_uom,
12442        p_breakdown                     =>   p_breakdown,
12443        p_breakdown_uom                 =>   p_breakdown_uom,
12444        p_planned_start_date            =>   p_planned_start_date,
12445        p_planned_end_date              =>   p_planned_end_date,
12446        p_continuous_task               =>   jtf_assign_pub.g_continuous_work,
12447        x_return_status                 =>   x_return_status,
12448        x_msg_count                     =>   x_msg_count,
12449        x_msg_data                      =>   x_msg_data,
12450        x_assign_resources_tbl          =>   x_assign_resources_tbl);
12451 
12452        -- set back the API name to original name
12453        l_api_name := l_api_name_1;
12454 
12455        IF NOT (x_return_status = fnd_api.g_ret_sts_success) THEN
12456             -- Unexpected Execution Error from call to Get_contracts_resources
12457           fnd_message.set_name('JTF', 'JTF_AM_GENERIC_API_ERROR');
12458           fnd_message.set_token('P_PROC_NAME','GET_AVAILABLE_RESOURCE');
12459           fnd_message.set_token('P_API_NAME','JTF_ASSIGN_PUB.GET_CONTRACTS_RESOURCES');
12460           fnd_msg_pub.add;
12461           IF (x_return_status = fnd_api.g_ret_sts_error) THEN
12462               RAISE fnd_api.g_exc_error;
12463           ELSE
12464              RAISE fnd_api.g_exc_unexpected_error;
12465           END IF;
12466       END IF; -- end of x_return_status check
12467 
12468 
12469 
12470 
12471   EXCEPTION
12472     WHEN FND_API.G_EXC_ERROR THEN
12473       x_return_status := FND_API.G_RET_STS_ERROR ;
12474       FND_MSG_PUB.Count_And_Get
12475       (
12476         p_count => x_msg_count,
12477         p_data  => x_msg_data
12478       );
12479 
12480     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12481       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12482       FND_MSG_PUB.Count_And_Get
12483       (
12484         p_count => x_msg_count,
12485         p_data  => x_msg_data
12486       );
12487 
12488     WHEN OTHERS THEN
12489       fnd_message.set_name ('JTF', 'JTF_AM_UNEXP_ERROR');
12490       fnd_message.set_token('P_SQLCODE',SQLCODE);
12491       fnd_message.set_token('P_SQLERRM',SQLERRM);
12492       fnd_message.set_token('P_API_NAME',l_api_name);
12493       FND_MSG_PUB.add;
12494 
12495       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12496       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )THEN
12497         FND_MSG_PUB.Add_Exc_Msg
12498         (
12499           G_PKG_NAME,
12500           l_api_name
12501         );
12502       END IF;
12503 
12504       FND_MSG_PUB.Count_And_Get
12505       (
12506         p_count => x_msg_count,
12507         p_data  => x_msg_data
12508        );
12509 
12510  END GET_RESOURCE_AVAILABILITY;
12511 
12512 END JTF_ASSIGN_PUB;