DBA Data[Home] [Help]

PACKAGE BODY: APPS.JTY_WEBADI_OTH_TERR_UPDATE_PKG

Source


1 PACKAGE BODY JTY_WEBADI_OTH_TERR_UPDATE_PKG AS
2 /* $Header: jtfowupb.pls 120.42.12010000.1 2008/07/24 18:13:00 appldev ship $ */
3 
4 --    Start of Comments
5 
6 --    ---------------------------------------------------
7 
8 --    PACKAGE NAME:   JTY_WEBADI_OTH_TERR_UPDATE_PKG
9 
10 --    ---------------------------------------------------
11 
12 
13 
14 --  PURPOSE
15 
16 --      upload other territories information from excel
17 
18 --
19 
20 --
21 
22 --  PROCEDURES:
23 
24 --       (see below for specification)
25 
26 --
27 
28 --
29 
30 --  HISTORY
31 
32 --    09/01/2005  mhtran          Package Body Created
33 --	  02/03/2005  MHTRAN		  Modified
34 --
35 
36 --    End of Comments
37 
38 --
39 
40   TYPE number_tbl_type is table of NUMBER index by PLS_INTEGER;
41   TYPE varchar2_tbl_type is table of VARCHAR2(360) index by PLS_INTEGER;
42   TYPE date_tbl_type is table of DATE index by PLS_INTEGER;
43   TYPE var_1_tbl_type is table of VARCHAR2(1) index by PLS_INTEGER;
44   TYPE var_2000_tbl_type is table of VARCHAR2(2000) index by PLS_INTEGER;
45 
46   TYPE Terr_Values_Rec_Type  IS RECORD
47    (TERR_VALUE_ID                    number_tbl_type,
48     LAST_UPDATE_DATE                 date_tbl_type,
49     LAST_UPDATED_BY                  number_tbl_type,
50     CREATION_DATE                    date_tbl_type,
51     CREATED_BY                       number_tbl_type,
52     LAST_UPDATE_LOGIN                number_tbl_type,
53     TERR_QUAL_ID                     number_tbl_type,
54     COMPARISON_OPERATOR              varchar2_tbl_type,
55     LOW_VALUE_CHAR                   varchar2_tbl_type,
56     HIGH_VALUE_CHAR                  varchar2_tbl_type,
57     LOW_VALUE_NUMBER                 number_tbl_type,
58     HIGH_VALUE_NUMBER                number_tbl_type,
59     INTEREST_TYPE_ID                 number_tbl_type,
60     PRIMARY_INTEREST_CODE_ID         number_tbl_type,
61     SECONDARY_INTEREST_CODE_ID       number_tbl_type,
62     CURRENCY_CODE                    varchar2_tbl_type,
63     ID_USED_FLAG                     var_1_tbl_type,
64     LOW_VALUE_CHAR_ID                number_tbl_type,
65     ORG_ID                           number_tbl_type,
66     VALUE1_ID                        number_tbl_type,
67     VALUE2_ID                        number_tbl_type,
68     VALUE3_ID                        number_tbl_type
69    );
70 
71   TYPE Terr_Qual_Rec_Type  IS RECORD
72     (  CONVERT_TO_ID_FLAG			 var_1_tbl_type,
73        TERR_QUAL_ID                  number_tbl_type,
74 	   qual_value_id				 number_tbl_type,
75 	   qual_value1					 varchar2_tbl_type,
76 	   qual_value2					 varchar2_tbl_type,
77 	   qual_value3					 varchar2_tbl_type,
78 	   qualifier_num				 number_tbl_type,
79 	   html_lov_sql1				 var_2000_tbl_type,
80 	   qual_cond					 varchar2_tbl_type,
81 	   qual_type					 varchar2_tbl_type,
82        LAST_UPDATE_DATE              date_tbl_type,
83        LAST_UPDATED_BY               number_tbl_type,
84        CREATION_DATE                 date_tbl_type,
85        CREATED_BY                    number_tbl_type,
86        LAST_UPDATE_LOGIN             number_tbl_type,
87        TERR_ID                       number_tbl_type,
88        QUAL_USG_ID                   number_tbl_type,
89        ORG_ID                        number_tbl_type
90      );
91 
92   TYPE Terr_values_out_rec_type IS RECORD
93     (  TERR_VALUE_ID                 number_tbl_type,
94        TERR_QUAL_ID                  number_tbl_type
95 	);
96 
97 
98 PROCEDURE get_hierarchy
99   ( p_user_sequence  IN  number,
100 	p_intf_type		 IN  varchar2 default 'U',
101 	x_return_status	 out nocopy varchar2,
102 	x_msg_data		 out nocopy varchar2
103 )
104 IS
105 
106   cursor get_hierarchy_csr
107   ( v_user_sequence number,
108 	v_intf_type		varchar2) IS
109     SELECT TERR_NAME, trim(HIERARCHY) hierarchy, ORG_ID, org_name
110 	FROM JTY_WEBADI_OTH_TERR_INTF
111 	where user_sequence = v_user_sequence
112 	  and interface_type = v_intf_type
113 	  and status is null;
114 
115 	type get_hierarchy_type is TABLE of get_hierarchy_csr%rowtype INDEX BY PLS_INTEGER;
116 
117 	l_hierarchy_tbl get_hierarchy_type;
118   	l_parent_id	 	number;
119   	l_anc_id		number;
120   	l_instr		 	number;
121   	l_count		 	number;
122   	l_terr		 	varchar2(2000);
123   	l_parent		varchar2(2000);
124 
125 BEGIN
126 
127   x_return_status := FND_API.G_RET_STS_SUCCESS;
128  /*
129   update JTY_WEBADI_OTH_TERR_INTF
130   set parent_terr_id = null
131   where user_sequence = p_user_sequence
132     and interface_type = p_intf_type
133 	and hierarchy is null;
134   */
135   --dbms_output.put_line('Start hierarchy');
136   -- get parent_id from hierarchy columns
137   open get_hierarchy_csr(p_user_sequence, p_intf_type);
138   fetch get_hierarchy_csr bulk collect into l_hierarchy_tbl;
139   close get_hierarchy_csr;
140 
141   if l_hierarchy_tbl.count > 0 then
142   for c in l_hierarchy_tbl.first..l_hierarchy_tbl.last loop
143 	  l_parent_id := 1;
144 	  l_anc_id := 1;
145 
146     if l_hierarchy_tbl(c).hierarchy is null then
147 	  -- no hierarchy found. Root node
148 	  l_parent := l_hierarchy_tbl(c).org_name;
149 
150   	  update JTY_WEBADI_OTH_TERR_INTF
151   	  set parent_terr_id = l_parent_id
152 	  , parent_terr_name = l_parent
153   	  where terr_name = l_hierarchy_tbl(c).terr_name
154   	  and user_sequence = p_user_sequence
155   	  and interface_type = p_intf_type;
156 
157     --dbms_output.put_line('hierarchy updated');
158 
159     else
160 	  l_terr := l_hierarchy_tbl(c).hierarchy;
161       l_instr := instr(l_terr,'->');
162 	  --dbms_output.put_line('l_instr,l_terr: '||l_instr||','||l_terr);
163 	loop
164 	  if l_instr = 0 then
165 	    l_parent := l_terr;
166 	    --dbms_output.put_line('l_instr = 0, l_parent: '||l_parent);
167 
168    	  	select count(*) into l_count
169    	  	from jtf_terr_all
170    	  	where name = l_parent
171    		and parent_territory_id = l_anc_id
172    		and org_id = l_hierarchy_tbl(c).org_id;
173 
174 		case l_count
175 		when 0 then
176    	      update JTY_WEBADI_OTH_TERR_INTF
177    		  set parent_terr_id = null
178    	  	  , parent_terr_name = l_parent
179    		  where terr_name = l_hierarchy_tbl(c).terr_name
180    		  and user_sequence = p_user_sequence
181    		  and interface_type = p_intf_type;
182 		when 1 then
183 		  -- lowest level parent_id found, update interface table
184 	      select terr_id into l_parent_id
185 		  from jtf_terr_all
186 		  where org_id = l_hierarchy_tbl(c).org_id
187 	      and name = l_parent
188 		  and parent_territory_id = l_anc_id;
189 
190 	      update JTY_WEBADI_OTH_TERR_INTF
191 		  set parent_terr_id = l_parent_id
192 	  	  , parent_terr_name = l_parent
193 		  where terr_name = l_hierarchy_tbl(c).terr_name
194 		  and user_sequence = p_user_sequence
195 		  and interface_type = p_intf_type;
196 		else
197 		  x_return_status := FND_API.G_RET_STS_ERROR;
198 		  fnd_message.clear;
199 		  FND_MESSAGE.set_name ('JTF', 'JTY_OTH_TERR_NON_UNIQUE_TERR');
200 		  FND_MESSAGE.set_token ('POSITION', l_parent);
201 		  X_Msg_Data := fnd_message.get();
202    	      update JTY_WEBADI_OTH_TERR_INTF
203    		  set status = x_return_status
204    		  , error_msg = X_Msg_Data
205    		  where terr_name = l_hierarchy_tbl(c).terr_name
206    		  and user_sequence = p_user_sequence
207    		  and interface_type = p_intf_type;
208 		end case;
209 		EXIT;
210 	  else
211         l_parent := substr(l_terr,1,l_instr -1);
212 	    --dbms_output.put_line('l_parent: '||l_parent);
213 
214    	  	select count(*) into l_count
215    	  	from jtf_terr_all
216    	  	where name = l_parent
217    		and parent_territory_id = l_anc_id
218    		and org_id = l_hierarchy_tbl(c).org_id;
219 
220 		case l_count
221 		when 0 then
222 		  l_parent := substr(l_terr,instr(l_terr,'->',-1)+2);
223 	    --dbms_output.put_line('case 0, l_parent: '||l_parent);
224 
225    	      update JTY_WEBADI_OTH_TERR_INTF
226    		  set parent_terr_id = null
227    	  	  , parent_terr_name = l_parent
228    		  where terr_name = l_hierarchy_tbl(c).terr_name
229    		  and user_sequence = p_user_sequence
230    		  and interface_type = p_intf_type;
231 		  exit;
232 		when 1 then
233 	      select terr_id into l_parent_id
234 		  from jtf_terr_all
235 		  where org_id = l_hierarchy_tbl(c).org_id
236 	      and name = l_parent
237 		  and parent_territory_id = l_anc_id;
238 		else
239 		  x_return_status := FND_API.G_RET_STS_ERROR;
240 		  fnd_message.clear;
241 		  FND_MESSAGE.set_name ('JTF', 'JTY_OTH_TERR_NON_UNIQUE_TERR');
242 		  FND_MESSAGE.set_token ('POSITION', l_parent);
243 		  X_Msg_Data := fnd_message.get();
244 
245    	      update JTY_WEBADI_OTH_TERR_INTF
246    		  set status = x_return_status
247    		  , error_msg = X_Msg_Data
248    		  where terr_name = l_hierarchy_tbl(c).terr_name
249    		  and user_sequence = p_user_sequence
250    		  and interface_type = p_intf_type;
251 		  exit;
252 		end case;
253 
254 		l_anc_id := l_parent_id;
255         l_terr := substr(l_terr, l_instr+2);
256 	    l_instr := instr(l_terr, '->');
257 
258 	    --dbms_output.put_line('l_instr, l_terr, l_parent, l_anc_id: '||l_instr||', '||l_terr||', '||l_parent||', '||l_anc_id);
259 
260 	  END IF; -- instr = 0
261 	end loop; -- loop through instr
262 	end if; -- hierarchy is null
263 
264   end loop;
265   end if;
266 
267   exception
268     when others then
269 	  x_return_status := FND_API.G_RET_STS_ERROR;
270 end get_hierarchy;
271 
272 PROCEDURE VALIDATE_TERRITORY_RECORDS
273 (  P_USER_SEQUENCE		   IN NUMBER,
274    P_INTF_TYPE			   IN VARCHAR2,
275    X_RETURN_STATUS		   OUT NOCOPY VARCHAR2,
276    X_MSG_DATA			   OUT NOCOPY VARCHAR2
277 ) IS
278   l_action_flag varchar2(1) := 'U';
279   l_header		varchar2(15);
280   l_count		number;
281 
282   CURSOR get_qual_csr(
283     v_user_sequence number,
284     v_intf_type	  varchar2,
285     v_header		  varchar2) IS
286 	select jqh.qualifier_name, sub.lay_seq_num
287 	from JTY_WEBADI_QUAL_HEADER jqh,
288 	(
289       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
290       1 qual_num
291       FROM JTY_WEBADI_OTH_TERR_INTF jut
292       where jut.USER_SEQUENCE = v_user_sequence
293   	  and jut.status is null
294 	  and jut.qual1_value1 is not null
295       and jut.INTERFACE_TYPE = v_intf_type
296   	  and jut.header = v_header
297       union all
298       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
299       2 qual_num
300       FROM JTY_WEBADI_OTH_TERR_INTF jut
301       where jut.USER_SEQUENCE = v_user_sequence
302   	  and jut.status is null
303 	  and jut.qual2_value1 is not null
304   	  and jut.INTERFACE_TYPE = v_intf_type
305   	  and jut.header = v_header
306       union all
307       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
308       3 qual_num
309       FROM JTY_WEBADI_OTH_TERR_INTF jut
310       where jut.USER_SEQUENCE = v_user_sequence
311   	  and jut.status is null
312 	  and jut.qual3_value1 is not null
313   	  and jut.INTERFACE_TYPE = v_intf_type
314   	  and jut.header = v_header
315       union all
316       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
317       4 qual_num
318       FROM JTY_WEBADI_OTH_TERR_INTF jut
319       where jut.USER_SEQUENCE = v_user_sequence
320   	  and jut.status is null
321 	  and jut.qual4_value1 is not null
322   	  and jut.INTERFACE_TYPE = v_intf_type
323   	  and jut.header = v_header
324       union all
325       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
326       5 qual_num
327       FROM JTY_WEBADI_OTH_TERR_INTF jut
328       where jut.USER_SEQUENCE = v_user_sequence
329   	  and jut.status is null
330 	  and jut.qual5_value1 is not null
331   	  and jut.INTERFACE_TYPE = v_intf_type
332   	  and jut.header = v_header
333       union all
334       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
335       6 qual_num
336       FROM JTY_WEBADI_OTH_TERR_INTF jut
337       where jut.USER_SEQUENCE = v_user_sequence
338   	  and jut.status is null
339 	  and jut.qual6_value1 is not null
340   	  and jut.INTERFACE_TYPE = v_intf_type
341   	  and jut.header = v_header
342       union all
343       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
344       7 qual_num
345       FROM JTY_WEBADI_OTH_TERR_INTF jut
346       where jut.USER_SEQUENCE = v_user_sequence
347   	  and jut.status is null
348 	  and jut.qual7_value1 is not null
349   	  and jut.INTERFACE_TYPE = v_intf_type
350   	  and jut.header = v_header
351       union all
352       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
353       8 qual_num
354       FROM JTY_WEBADI_OTH_TERR_INTF jut
355       where jut.USER_SEQUENCE = v_user_sequence
356   	  and jut.status is null
357 	  and jut.qual8_value1 is not null
358   	  and jut.INTERFACE_TYPE = v_intf_type
359   	  and jut.header = v_header
360       union all
361       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
362       9 qual_num
363       FROM JTY_WEBADI_OTH_TERR_INTF jut
364       where jut.USER_SEQUENCE = v_user_sequence
365   	  and jut.status is null
366 	  and jut.qual9_value1 is not null
367   	  and jut.INTERFACE_TYPE = v_intf_type
368   	  and jut.header = v_header
369       union all
370       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
371       10 qual_num
372       FROM JTY_WEBADI_OTH_TERR_INTF jut
373       where jut.USER_SEQUENCE = v_user_sequence
374   	  and jut.status is null
375 	  and jut.qual10_value1 is not null
376   	  and jut.INTERFACE_TYPE = v_intf_type
377   	  and jut.header = v_header
378       union all
379       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
380       11 qual_num
381       FROM JTY_WEBADI_OTH_TERR_INTF jut
382       where jut.USER_SEQUENCE = v_user_sequence
383   	  and jut.status is null
384 	  and jut.qual11_value1 is not null
385   	  and jut.INTERFACE_TYPE = v_intf_type
386   	  and jut.header = v_header
387       union all
388       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
389       12 qual_num
390       FROM JTY_WEBADI_OTH_TERR_INTF jut
391       where jut.USER_SEQUENCE = v_user_sequence
392   	  and jut.status is null
393 	  and jut.qual12_value1 is not null
394   	  and jut.INTERFACE_TYPE = v_intf_type
395   	  and jut.header = v_header
396       union all
397       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
398       13 qual_num
399       FROM JTY_WEBADI_OTH_TERR_INTF jut
400       where jut.USER_SEQUENCE = v_user_sequence
401   	  and jut.status is null
402 	  and jut.qual13_value1 is not null
403   	  and jut.INTERFACE_TYPE = v_intf_type
404   	  and jut.header = v_header
405       union all
406       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
407       14 qual_num
408       FROM JTY_WEBADI_OTH_TERR_INTF jut
409       where jut.USER_SEQUENCE = v_user_sequence
410   	  and jut.status is null
411 	  and jut.qual14_value1 is not null
412   	  and jut.INTERFACE_TYPE = v_intf_type
413   	  and jut.header = v_header
414       union all
415       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
416       15 qual_num
417       FROM JTY_WEBADI_OTH_TERR_INTF jut
418       where jut.USER_SEQUENCE = v_user_sequence
419   	  and jut.status is null
420 	  and jut.qual15_value1 is not null
421   	  and jut.INTERFACE_TYPE = v_intf_type
422   	  and jut.header = v_header
423       union all
424       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
428   	  and jut.status is null
425       16 qual_num
426       FROM JTY_WEBADI_OTH_TERR_INTF jut
427       where jut.USER_SEQUENCE = v_user_sequence
429 	  and jut.qual16_value1 is not null
430   	  and jut.INTERFACE_TYPE = v_intf_type
431   	  and jut.header = v_header
432       union all
433       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
434       17 qual_num
435       FROM JTY_WEBADI_OTH_TERR_INTF jut
436       where jut.USER_SEQUENCE = v_user_sequence
437   	  and jut.status is null
438 	  and jut.qual17_value1 is not null
439   	  and jut.INTERFACE_TYPE = v_intf_type
440   	  and jut.header = v_header
441       union all
442       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
443       18 qual_num
444       FROM JTY_WEBADI_OTH_TERR_INTF jut
445       where jut.USER_SEQUENCE = v_user_sequence
446   	  and jut.status is null
447 	  and jut.qual18_value1 is not null
448   	  and jut.INTERFACE_TYPE = v_intf_type
449   	  and jut.header = v_header
450       union all
451       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
452       19 qual_num
453       FROM JTY_WEBADI_OTH_TERR_INTF jut
454       where jut.USER_SEQUENCE = v_user_sequence
455   	  and jut.status is null
456 	  and jut.qual19_value1 is not null
457   	  and jut.INTERFACE_TYPE = v_intf_type
458   	  and jut.header = v_header
459       union all
460       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
461       20 qual_num
462       FROM JTY_WEBADI_OTH_TERR_INTF jut
463       where jut.USER_SEQUENCE = v_user_sequence
464   	  and jut.status is null
465 	  and jut.qual20_value1 is not null
466   	  and jut.INTERFACE_TYPE = v_intf_type
467   	  and jut.header = v_header
468       union all
469       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
470       21 qual_num
471       FROM JTY_WEBADI_OTH_TERR_INTF jut
472       where jut.USER_SEQUENCE = v_user_sequence
473   	  and jut.status is null
474 	  and jut.qual21_value1 is not null
475   	  and jut.INTERFACE_TYPE = v_intf_type
476   	  and jut.header = v_header
477       union all
478       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
479       22 qual_num
480       FROM JTY_WEBADI_OTH_TERR_INTF jut
481       where jut.USER_SEQUENCE = v_user_sequence
482   	  and jut.status is null
483 	  and jut.qual22_value1 is not null
484   	  and jut.INTERFACE_TYPE = v_intf_type
485   	  and jut.header = v_header
486       union all
487       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
488       23 qual_num
489       FROM JTY_WEBADI_OTH_TERR_INTF jut
490       where jut.USER_SEQUENCE = v_user_sequence
491   	  and jut.status is null
492 	  and jut.qual23_value1 is not null
493   	  and jut.INTERFACE_TYPE = v_intf_type
494   	  and jut.header = v_header
495       union all
496       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
497       24 qual_num
498       FROM JTY_WEBADI_OTH_TERR_INTF jut
499       where jut.USER_SEQUENCE = v_user_sequence
500   	  and jut.status is null
501 	  and jut.qual24_value1 is not null
502   	  and jut.INTERFACE_TYPE = v_intf_type
503   	  and jut.header = v_header
504       union all
505       select jut.lay_seq_num, jut.user_sequence, jut.terr_type_id,
506       25 qual_num
507       FROM JTY_WEBADI_OTH_TERR_INTF jut
508       where jut.USER_SEQUENCE = v_user_sequence
509   	  and jut.status is null
510 	  and jut.qual25_value1 is not null
511   	  and jut.INTERFACE_TYPE = v_intf_type
512   	  and jut.header = v_header) sub
513 	  where jqh.qualifier_num = sub.qual_num
514 	    and jqh.user_sequence = sub.user_sequence
515 	    and not exists (select 1 from jtf_terr_type_qual_all jttq
516 					   	   where jttq.terr_type_id = sub.terr_type_id
517 						     and jttq.qual_usg_id = jqh.qual_usg_id);
518 
519   TYPE Qual_Rec_Type  IS RECORD
520     (  lay_seq_num				 number_tbl_type,
521 	   qualifier_name				 varchar2_tbl_type );
522 
523   qual_rec 	Qual_Rec_Type;
524 BEGIN
525 
526   x_return_status := FND_API.G_RET_STS_ERROR;
527   fnd_message.clear;
528   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_INVALID_NAME');
529   X_Msg_Data := fnd_message.get();
530 
531  	update JTY_WEBADI_OTH_TERR_INTF jwot
532 	set status = x_return_status,
533 	error_msg = X_Msg_Data
534 	where jwot.terr_name is null
535 	  and jwot.USER_SEQUENCE = p_USER_SEQUENCE
536 	  and jwot.INTERFACE_TYPE = p_intf_type
537 	  and jwot.status is null;
538 
539   l_header := 'RSC';
540   l_action_flag := 'D';
541 
542   x_return_status := FND_API.G_RET_STS_ERROR;
543   fnd_message.clear;
544   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_INVALID_ID');
545   X_Msg_Data := fnd_message.get();
546 
547  	update JTY_WEBADI_OTH_TERR_INTF jwot
548 	set status = x_return_status,
549 	error_msg = X_Msg_Data
550 	where ( (jwot.TERR_ID IS NULL and jwot.header = 'TERR')
551 	   OR   (jwot.header = 'QUAL' and jwot.TERR_QUAL_ID1 IS NULL
552 			 and jwot.TERR_QUAL_ID2 IS NULL and jwot.TERR_QUAL_ID3 IS NULL
553 			 and jwot.TERR_QUAL_ID4 IS NULL and jwot.TERR_QUAL_ID5 IS NULL
554 			 and jwot.TERR_QUAL_ID6 IS NULL and jwot.TERR_QUAL_ID7 IS NULL
555 			 and jwot.TERR_QUAL_ID8 IS NULL and jwot.TERR_QUAL_ID9 IS NULL
556 			 and jwot.TERR_QUAL_ID10 IS NULL and jwot.TERR_QUAL_ID11 IS NULL
557 			 and jwot.TERR_QUAL_ID12 IS NULL and jwot.TERR_QUAL_ID13 IS NULL
561 			 and jwot.TERR_QUAL_ID20 IS NULL and jwot.TERR_QUAL_ID21 IS NULL
558 			 and jwot.TERR_QUAL_ID14 IS NULL and jwot.TERR_QUAL_ID15 IS NULL
559 			 and jwot.TERR_QUAL_ID16 IS NULL and jwot.TERR_QUAL_ID17 IS NULL
560 			 and jwot.TERR_QUAL_ID18 IS NULL and jwot.TERR_QUAL_ID19 IS NULL
562 			 and jwot.TERR_QUAL_ID22 IS NULL and jwot.TERR_QUAL_ID23 IS NULL
563 			 and jwot.TERR_QUAL_ID24 IS NULL and jwot.TERR_QUAL_ID25 IS NULL)
564 	   OR EXISTS (select 1 from jty_webadi_resources jwr
565 	   	  		  where jwot.lay_seq_num = jwr.lay_seq_num
566 				    and jwr.terr_rsc_id is null
567 					and jwr.header = l_header)
568 		  )
569 	  and jwot.USER_SEQUENCE = p_USER_SEQUENCE
570 	  and jwot.INTERFACE_TYPE = p_intf_type
571 	  and jwot.action_flag = l_action_flag
572 	  and jwot.status is null;
573 
574   l_action_flag := 'U';
575 
576   x_return_status := FND_API.G_RET_STS_ERROR;
577   fnd_message.clear;
578   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_INVALID_TEMPLATE');
579   X_Msg_Data := fnd_message.get();
580 
581   	update JTY_WEBADI_OTH_TERR_INTF jwot
582 	set jwot.status = x_return_status,
583 	jwot.error_msg = X_Msg_Data
584 	where not exists ( SELECT 1
585 				   FROM jtf_terr_all jt
586 				   WHERE jt.TERRITORY_TYPE_ID = jwot.TERR_TYPE_ID
587 					 and jt.terr_id = jwot.TERR_ID
588 		  			 AND jt.ORG_ID = jwot.ORG_ID )
589 	  and jwot.user_sequence = p_user_sequence
590 	  and jwot.interface_type = p_intf_type
591 	  and jwot.action_flag = l_action_flag
592 	  and jwot.header <> l_header
593 	  and jwot.status is null
594 	  AND jwot.TERR_ID is not null;
595 
596   x_return_status := FND_API.G_RET_STS_ERROR;
597   fnd_message.clear;
598   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_NO_TEMPLATE');
599   X_Msg_Data := fnd_message.get();
600 
601  	update JTY_WEBADI_OTH_TERR_INTF jwot
602 	set status = x_return_status,
603 	error_msg = X_Msg_Data
604 	where jwot.TERR_TYPE_ID is null
605 	  AND jwot.user_sequence = p_user_sequence
606 	  and jwot.interface_type = p_intf_type
607 	  and jwot.header <> l_header
608 	  and jwot.status is null;
609 
610 -- Commented it as a part of fix for bug #5479649
611  /*
612   x_return_status := FND_API.G_RET_STS_ERROR;
613   fnd_message.clear;
614   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_INVALID_ACCESS');
615   X_Msg_Data := fnd_message.get();
616 
617    	update JTY_WEBADI_OTH_TERR_INTF jwot
618    	set jwot.status = x_return_status,
619 	jwot.error_msg = X_Msg_Data
620 	where exists
621 	  (select 1 from JTY_WEBADI_RESOURCES jwr
622     	  where jwot.lay_seq_num = jwr.lay_seq_num
623     	  AND jwr.TRANS_ACCESS_CODE1 is null
624     	  AND jwr.TRANS_ACCESS_CODE2 is null
625     	  AND jwr.TRANS_ACCESS_CODE3 is null
626     	  AND jwr.TRANS_ACCESS_CODE4 is null
627     	  AND jwr.TRANS_ACCESS_CODE5 is null
628     	  AND jwr.TRANS_ACCESS_CODE6 is null
629     	  AND jwr.TRANS_ACCESS_CODE7 is null
630     	  AND jwr.TRANS_ACCESS_CODE8 is null
631     	  AND jwr.TRANS_ACCESS_CODE9 is null
632     	  AND jwr.TRANS_ACCESS_CODE10 is null
633     	  and jwr.USER_SEQUENCE = jwot.USER_SEQUENCE
634     	  and jwr.interface_type = jwot.interface_type
635 		  and jwr.header = jwot.header)
636 	  and jwot.user_sequence = p_user_sequence
637 	  and jwot.interface_type = p_intf_type
638 	  and jwot.header = l_header
639 	  and jwot.status is null;
640   */
641 
642 -- Fix for bug #5583243 START
643  -- if any access is left blank while uploading the data from excel,
644  -- then the upload should populate the value 'NONE' in the database.
645 
646   UPDATE  JTY_WEBADI_RESOURCES jut
647      SET    jut.TRANS_ACCESS_CODE1  = NVL(jut.TRANS_ACCESS_CODE1,'NONE')
648           , jut.TRANS_ACCESS_CODE2  = NVL(jut.TRANS_ACCESS_CODE2,'NONE')
649           , jut.TRANS_ACCESS_CODE3  = NVL(jut.TRANS_ACCESS_CODE3,'NONE')
650           , jut.TRANS_ACCESS_CODE4  = NVL(jut.TRANS_ACCESS_CODE4,'NONE')
651           , jut.TRANS_ACCESS_CODE5  = NVL(jut.TRANS_ACCESS_CODE5,'NONE')
652           , jut.TRANS_ACCESS_CODE6  = NVL(jut.TRANS_ACCESS_CODE6,'NONE')
653           , jut.TRANS_ACCESS_CODE7  = NVL(jut.TRANS_ACCESS_CODE7,'NONE')
654           , jut.TRANS_ACCESS_CODE8  = NVL(jut.TRANS_ACCESS_CODE8,'NONE')
655           , jut.TRANS_ACCESS_CODE9  = NVL(jut.TRANS_ACCESS_CODE9,'NONE')
656           , jut.TRANS_ACCESS_CODE10 = NVL(jut.TRANS_ACCESS_CODE10,'NONE')
657   WHERE jut.USER_SEQUENCE = p_user_sequence
658     AND jut.header = 'RSC'
659     AND jut.INTERFACE_TYPE = p_intf_type;
660 
661 -- Fix for bug #5583243 END
662 
663 -- Fix for bug #5479649  START
664 -- An error message should be displayed when all the access types
665 -- are NONE for all the transaction type of a resource.
666   x_return_status := FND_API.G_RET_STS_ERROR;
667   fnd_message.clear;
668   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_INVALID_ACCESS');
669   X_Msg_Data := fnd_message.get();
670   	update JTY_WEBADI_OTH_TERR_INTF jwot
671    	set jwot.status = x_return_status,
672 	jwot.error_msg = X_Msg_Data
673 	where exists
674 	  (select 1 from JTY_WEBADI_RESOURCES jwr
675     	  where jwot.lay_seq_num = jwr.lay_seq_num
676     	  AND jwr.TRANS_ACCESS_CODE1  = 'NONE'
677     	  AND jwr.TRANS_ACCESS_CODE2  = 'NONE'
678     	  AND jwr.TRANS_ACCESS_CODE3  = 'NONE'
679     	  AND jwr.TRANS_ACCESS_CODE4  = 'NONE'
680     	  AND jwr.TRANS_ACCESS_CODE5  = 'NONE'
681     	  AND jwr.TRANS_ACCESS_CODE6  = 'NONE'
682     	  AND jwr.TRANS_ACCESS_CODE7  = 'NONE'
686     	  and jwr.USER_SEQUENCE = jwot.USER_SEQUENCE
683     	  AND jwr.TRANS_ACCESS_CODE8  = 'NONE'
684     	  AND jwr.TRANS_ACCESS_CODE9  = 'NONE'
685     	  AND jwr.TRANS_ACCESS_CODE10 = 'NONE'
687     	  and jwr.interface_type = jwot.interface_type
688 		  and jwr.header = jwot.header)
689 	  and jwot.user_sequence = p_user_sequence
690 	  and jwot.interface_type = p_intf_type
691 	  and jwot.header = l_header
692 	  and jwot.status is null;
693 
694 -- Fix for bug #5479649  END.
695 
696 -- Fix for bug # 6372728 START
697  -- Should display a error message when user tries to add a duplicate reosurce.
698   x_return_status := FND_API.G_RET_STS_ERROR;
699   fnd_message.clear;
700   fnd_message.set_name ('JTF', 'JTF_TTY_DUPLICATE_RESOURCE');
701   X_Msg_Data := fnd_message.get();
702 
703    update JTY_WEBADI_OTH_TERR_INTF jwot
704     set  status = x_return_status
705        , error_msg = X_Msg_Data
706    where jwot.USER_SEQUENCE = p_USER_SEQUENCE
707      and jwot.INTERFACE_TYPE = p_intf_type
708      and jwot.status is null
709      and jwot.action_flag = 'C'
710      and jwot.header = 'RSC'
711      and exists( select 1
712                  from JTF_TERR_RSC_ALL jtr,JTY_WEBADI_RESOURCES jwr
713                  where jwr.USER_SEQUENCE = jwot.USER_SEQUENCE
714                     and jwr.header = jwot.header
715                     and jwr.INTERFACE_TYPE = jwot.INTERFACE_TYPE
716                     and jwot.lay_seq_num = jwr.lay_seq_num
717                     and jtr.TERR_ID = jwr.TERR_ID
718                     and jtr.RESOURCE_ID = jwr.RESOURCE_ID
719                     and decode(jtr.resource_type,'RS_GROUP',1,'RS_TEAM',2,'RS_ROLE',3,0) = jwr.resource_type
720                 );
721 
722 -- Fix for bug # 6372728 END
723 
724 
725   x_return_status := FND_API.G_RET_STS_ERROR;
726   fnd_message.clear;
727   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_RSC_START_DATE');
728   X_Msg_Data := fnd_message.get();
729 
730    	UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
731    	set jwot.status = x_return_status,
732 	jwot.error_msg = X_Msg_Data
733 	where exists
734 	  (select 1 from JTY_WEBADI_RESOURCES jwr
735     	  where jwot.lay_seq_num = jwr.lay_seq_num
736     	  and jwr.USER_SEQUENCE = jwot.USER_SEQUENCE
737     	  and jwr.interface_type = jwot.interface_type
738 		  and jwr.header = jwot.header
739 		  and NOT(jwr.RES_START_DATE between jwot.TERR_START_DATE and jwot.TERR_END_DATE)
740 		  --and (jwr.RES_START_DATE <= nvl(jwot.TERR_START_DATE,sysdate))
741 		  )
742 	  and jwot.user_sequence = p_user_sequence
743 	  and jwot.interface_type = p_intf_type
744 	  and jwot.header = l_header
745 	  and jwot.status is null;
746 
747   x_return_status := FND_API.G_RET_STS_ERROR;
748   fnd_message.clear;
749   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_RSC_END_DATE');
750   X_Msg_Data := fnd_message.get();
751 
752    	UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
753    	set jwot.status = x_return_status,
754 	jwot.error_msg = X_Msg_Data
755 	where exists
756 	  (select 1 from JTY_WEBADI_RESOURCES jwr
757     	  where jwot.lay_seq_num = jwr.lay_seq_num
758     	  and jwr.USER_SEQUENCE = jwot.USER_SEQUENCE
759     	  and jwr.interface_type = jwot.interface_type
760 		  and jwr.header = jwot.header
761 		  and not (jwr.RES_END_DATE between nvl(jwot.TERR_START_DATE,sysdate)
762 		  	  and NVL(jwot.TERR_END_DATE, ADD_MONTHS(NVL(jwot.TERR_START_DATE,SYSDATE), 12)))
763 		  )
764 	  and jwot.user_sequence = p_user_sequence
765 	  and jwot.interface_type = p_intf_type
766 	  and jwot.header = l_header
767 	  and jwot.status is null;
768 
769 
770   l_header := 'QUAL';
771   x_return_status := FND_API.G_RET_STS_ERROR;
772   fnd_message.clear;
773   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_INVALID_QUAL');
774   X_Msg_Data := fnd_message.get();
775 
776   open get_qual_csr(p_user_sequence, p_intf_type, l_header);
777   fetch get_qual_csr bulk collect into qual_rec.qualifier_name, qual_rec.lay_seq_num;
778   close get_qual_csr;
779 
780   if qual_rec.lay_seq_num.count > 0 then
781     forall i in qual_rec.lay_seq_num.first..qual_rec.lay_seq_num.last
782 	  update JTY_WEBADI_OTH_TERR_INTF jwot
783 	  set jwot.status = x_return_status,
784 	  	  jwot.error_msg = decode(jwot.error_msg,null,X_Msg_Data||': '||qual_rec.qualifier_name(i),
785 		  				   	 jwot.error_msg ||', ' || qual_rec.qualifier_name(i))
786 	  where jwot.lay_seq_num = qual_rec.lay_seq_num(i)
787 	    and jwot.user_sequence = p_user_sequence
788 	  	and jwot.interface_type = p_intf_type
789 	  	and jwot.header = l_header;
790   end if;
791 
792   l_header := 'TERR';
793 
794   x_return_status := FND_API.G_RET_STS_ERROR;
795   fnd_message.clear;
796   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_START_DATE');
797   X_Msg_Data := fnd_message.get();
798 
799     UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
800 	set status = x_return_status,
801 	error_msg = X_Msg_Data
802     where jwot.interface_type = p_intf_type
803       AND jwot.HEADER= l_header
804 	  and jwot.USER_SEQUENCE = p_USER_SEQUENCE
805       AND jwot.hierarchy is not null
806 	  and jwot.TERR_START_DATE is not null
807 	  and jwot.status is null
808       and ( exists ( select 1
809     		     from JTY_WEBADI_OTH_TERR_INTF jwot2
810     			 where jwot.parent_terr_id is null
811     			   and NOT(jwot.TERR_START_DATE between NVL(jwot2.terr_start_date,SYSDATE)
815     		     from JTF_TERR_ALL jta
812 				   and NVL(jwot2.TERR_END_DATE, ADD_MONTHS(NVL(jwot2.TERR_START_DATE,SYSDATE), 12)))
813     			   and jwot.parent_terr_name = jwot2.terr_name)
814      	or exists ( select 1
816     			 where jwot.parent_terr_id = jta.terr_id
817     			   and NOT(jwot.TERR_START_DATE between jta.START_DATE_ACTIVE and jta.END_DATE_ACTIVE))
818      );
819 
820   x_return_status := FND_API.G_RET_STS_ERROR;
821   fnd_message.clear;
822   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_END_DATE');
823   X_Msg_Data := fnd_message.get();
824 
825     UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
826 	set status = x_return_status,
827 	error_msg = X_Msg_Data
828     where jwot.interface_type = p_intf_type
829       AND jwot.HEADER= l_header
830 	  and jwot.USER_SEQUENCE = p_USER_SEQUENCE
831       AND jwot.hierarchy is not null
832 	  AND jwot.TERR_END_DATE is not null
833 	  and jwot.status is null
834       and (exists ( select 1
835     		     from JTY_WEBADI_OTH_TERR_INTF jwot2
836     			 where jwot.parent_terr_id is null
837     			   and NOT (jwot.TERR_END_DATE between NVL(jwot2.terr_start_date,SYSDATE)
838 				   and NVL(jwot2.TERR_END_DATE, ADD_MONTHS(NVL(jwot2.TERR_START_DATE,SYSDATE), 12)))
839     			   and jwot.parent_terr_name = jwot2.terr_name)
840      	or exists ( select 1
841     		     from JTF_TERR_ALL jta
842     			 where jwot.parent_terr_id = jta.terr_id
843     			   and NOT(jwot.TERR_END_DATE between jta.START_DATE_ACTIVE and jta.END_DATE_ACTIVE))
844      );
845 
846   x_return_status := FND_API.G_RET_STS_ERROR;
847   fnd_message.clear;
848   fnd_message.set_name ('JTF', 'JTY_OTH_TERR_NO_ACCESS');
849   X_Msg_Data := fnd_message.get();
850 
851  	update JTY_WEBADI_OTH_TERR_INTF jwot
852 	set status = x_return_status,
853 	error_msg = X_Msg_Data
854 	where (jwot.org_id is null
855 	   or  mo_global.check_access(jwot.org_id) <> 'Y')
856 	  and jwot.USER_SEQUENCE = p_USER_SEQUENCE
857 	  and jwot.INTERFACE_TYPE = p_intf_type
858 	  and jwot.status is null;
859 
860   UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
861   set (status,error_msg) = (SELECT jwot2.STATUS, jwot2.error_msg
862   	  		    FROM JTY_WEBADI_OTH_TERR_INTF jwot2
863 				where jwot.terr_name = jwot2.terr_name
864 				  and jwot2.header = l_header
865 				  and jwot.USER_SEQUENCE = jwot2.USER_SEQUENCE
866 				  and jwot.INTERFACE_TYPE = jwot2.INTERFACE_TYPE)
867   WHERE jwot.USER_SEQUENCE = p_USER_SEQUENCE
868 	and jwot.INTERFACE_TYPE = p_intf_type
869 	and jwot.header <> l_header
870 	and jwot.status is null;
871 
872   x_return_status := FND_API.G_RET_STS_SUCCESS;
873 
874   select count(*) into l_count
875   from JTY_WEBADI_OTH_TERR_INTF jwot
876   where status is not null
877     and jwot.USER_SEQUENCE = p_USER_SEQUENCE
878 	and jwot.INTERFACE_TYPE = p_intf_type;
879 
880   --dbms_output.put_line ('l_count: '||l_count);
881 
882   if l_count > 0 then
883     UPDATE  JTY_WEBADI_OTH_TERR_INTF jwot
884 	set status = x_return_status
885 	where status is null
886     and jwot.USER_SEQUENCE = p_USER_SEQUENCE
887 	and jwot.INTERFACE_TYPE = p_intf_type;
888 
889 	x_return_status := FND_API.G_RET_STS_ERROR;
890   else
891     x_return_status := FND_API.G_RET_STS_SUCCESS;
892   end if;
893 
894   EXCEPTION
895     WHEN OTHERS THEN
896 	  X_RETURN_STATUS := FND_API.G_RET_STS_ERROR;
897 END VALIDATE_TERRITORY_RECORDS;
898 
899 PROCEDURE SET_CREATE_RECORDS(
900   p_user_sequence 			 IN  NUMBER,
901   p_intf_type				 IN	 VARCHAR2)
902 IS
903   l_action_flag varchar2(1) := 'C';
904   l_intf_type	varchar2(1);
905   l_login_id number;
906 BEGIN
907 
908   l_login_id := fnd_global.login_id;
909    l_intf_type := p_intf_type;
910 
911    update JTY_WEBADI_OTH_TERR_INTF jwot
912    set LAST_UPDATE_LOGIN = l_login_id,
913    (terr_start_date,TERR_END_DATE) =
914        (SELECT NVL(jwot2.TERR_START_DATE,TRUNC(SYSDATE)),
915    	    NVL(jwot2.TERR_END_DATE, ADD_MONTHS(NVL(jwot2.TERR_START_DATE,TRUNC(SYSDATE)), 12) )
916 		FROM JTY_WEBADI_OTH_TERR_INTF jwot2
917 		WHERE jwot.terr_name = jwot2.terr_name
918 		  and jwot.user_sequence = jwot2.user_sequence
919 		  and jwot2.header = 'TERR'
920 		  and rownum = 1),
921    usage_id = (select jwot2.usage_id from
922    	   			JTY_WEBADI_OTH_TERR_INTF jwot2
923 				where jwot.user_sequence = jwot2.user_sequence
924 				and interface_type = 'D'
925 				and jwot2.usage_id is not null
926 				and rownum = 1)
927 	where jwot.user_sequence = p_user_sequence
928 	  and jwot.interface_type = l_intf_type
929 	  and jwot.action_flag = l_action_flag
930 	  and jwot.status is null;
931 
932     UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
933     SET (TERR_ID, TERR_START_DATE, TERR_END_DATE) =
934 				  (select jta.terr_id, NVL(jwot.TERR_START_DATE,jta.START_DATE_ACTIVE)
935 				  , NVL(jwot.TERR_END_DATE,jta.END_DATE_ACTIVE)
936 				   from jtf_terr_all jta
937 				   where jta.name = jwot.terr_name
938 				     --and jta.parent_territory_id = jwot.parent_terr_id
939 				     and rownum = 1)
940     where status is null
941   	  and interface_type = l_intf_type
942   	  and user_sequence = p_user_sequence
943 	  and exists (select 1
944 				   from jtf_terr_all jta
945 				   where jta.name = jwot.terr_name
946 				     --and jta.parent_territory_id = jwot.parent_terr_id
947 					 ) ;
948 
952 	, QUAL3_VALUE_ID = NULL, QUAL4_VALUE_ID = NULL
949 	--dbms_output.put_line(' # records processed for intf table:  '||SQL%ROWCOUNT);
950 	update 	JTY_WEBADI_OTH_TERR_INTF
951 	set QUAL1_VALUE_ID = NULL, QUAL2_VALUE_ID = NULL
953 	, QUAL5_VALUE_ID = NULL, QUAL6_VALUE_ID = NULL
954 	, QUAL7_VALUE_ID = NULL, QUAL8_VALUE_ID = NULL
955 	, QUAL9_VALUE_ID = NULL, QUAL10_VALUE_ID = NULL
956 	, QUAL11_VALUE_ID = NULL, QUAL12_VALUE_ID = NULL
957 	, QUAL13_VALUE_ID = NULL, QUAL14_VALUE_ID = NULL
958 	, QUAL15_VALUE_ID = NULL, QUAL16_VALUE_ID = NULL
959 	, QUAL17_VALUE_ID = NULL, QUAL18_VALUE_ID = NULL
960 	, QUAL19_VALUE_ID = NULL, QUAL20_VALUE_ID = NULL
961 	, QUAL21_VALUE_ID = NULL, QUAL22_VALUE_ID = NULL
962 	, QUAL23_VALUE_ID = NULL, QUAL24_VALUE_ID = NULL
963 	, QUAL25_VALUE_ID = NULL
964     where status is null
965   	  and interface_type = l_intf_type
966   	  and user_sequence = p_user_sequence
967 	  and action_flag = l_action_flag
968 	  and header = 'QUAL';
969 
970 	UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
971 	set jwot.terr_id = null
972 	where jwot.status is null
973   	  and jwot.interface_type = l_intf_type
974   	  and jwot.user_sequence = p_user_sequence
975 	  and jwot.action_flag = l_action_flag
976 	  and exists (select 1 from JTY_WEBADI_OTH_TERR_INTF jwot2
977 	  	  		  where jwot.terr_name = jwot2.terr_name
978 				    and jwot.action_flag = jwot2.action_flag
979 					and jwot.user_sequence = jwot2.user_sequence
980 					and jwot.interface_type = jwot2.interface_type
981 					and jwot2.header = 'TERR');
982 
983     UPDATE JTY_WEBADI_RESOURCES jwr
984     SET TERR_ID = (select jwot.terr_id
985 				   from JTY_WEBADI_OTH_TERR_INTF jwot
986 				   where jwr.lay_seq_num = jwot.lay_seq_num
987 				     and rownum = 1) ,
988 	TERR_RSC_ID = NULL,
989 	TERR_RSC_ACCESS_ID1 = NULL, TERR_RSC_ACCESS_ID2 = NULL,
990 	TERR_RSC_ACCESS_ID3 = NULL, TERR_RSC_ACCESS_ID4 = NULL,
991 	TERR_RSC_ACCESS_ID5 = NULL, TERR_RSC_ACCESS_ID6 = NULL,
992 	TERR_RSC_ACCESS_ID7 = NULL, TERR_RSC_ACCESS_ID8 = NULL,
993 	TERR_RSC_ACCESS_ID9 = NULL, TERR_RSC_ACCESS_ID10 = NULL
994     where exists
995 	  ( select 1 from JTY_WEBADI_OTH_TERR_INTF jwot
996 	  	where jwot.interface_type = l_intf_type
997   		  and jwot.user_sequence = p_user_sequence
998 		  and jwot.status is null
999 		  and jwot.action_flag = l_action_flag
1000 		  and jwot.lay_seq_num = jwr.lay_seq_num
1001 		  and jwot.header = 'RSC');
1002 
1003 END SET_CREATE_RECORDS;
1004 
1005 PROCEDURE UPDATE_TERR_QUAL_ID(
1006     P_USER_SEQUENCE			  IN  NUMBER,
1007 	P_INTF_TYPE				  IN  VARCHAR2,
1008 	P_HEADER				  IN  VARCHAR2)
1009 
1010 IS
1011 
1012   cursor get_terr_qual_id_csr(
1013   v_header			varchar2,
1014   v_user_sequence	number,
1015   v_intf_type		varchar2,
1016   v_qual_num 		number
1017   ) IS
1018   select jtq.terr_qual_id, jwot.lay_seq_num
1019   from JTY_WEBADI_QUAL_HEADER qgt , jtf_terr_qual_all jtq, JTY_WEBADI_OTH_TERR_INTF jwot
1020   WHERE qgt.qual_usg_id = jtq.qual_usg_id
1021   	and jtq.org_id = jwot.org_id
1022   	AND qgt.user_sequence = jwot.user_sequence
1023   	AND jtq.terr_id = jwot.terr_id
1024   	and jwot.header = v_header
1025     AND jwot.USER_SEQUENCE = v_user_sequence
1026     AND jwot.interface_type = v_intf_type
1027 	and qgt.qualifier_num = v_qual_num
1028 	and jwot.status is null
1029   group by jwot.terr_id, jtq.terr_qual_id, jwot.lay_seq_num;
1030 
1031     TYPE Terr_Qual_Rec_Type  IS RECORD
1032     (  TERR_QUAL_ID                  number_tbl_type,
1033 	   lay_seq_num				 	 number_tbl_type);
1034 
1035 	l_terr_qual_rec Terr_Qual_Rec_Type;
1036 
1037 BEGIN
1038 
1039   for i in 1..25 loop
1040     open get_terr_qual_id_csr(p_header, p_user_sequence, p_intf_type, i);
1041     fetch get_terr_qual_id_csr bulk collect into l_terr_qual_rec.terr_qual_id,
1042       --l_terr_qual_rec.qualifier_num,
1043 	  l_terr_qual_rec.lay_seq_num;
1044     close get_terr_qual_id_csr;
1045 
1046     if l_terr_qual_rec.lay_seq_num.count > 0 then
1047 	  --dbms_output.put_line ('i: '||i);
1048       case i
1049 	    when 1 then
1050           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1051    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1052    	    	set jwot.terr_qual_id1 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1053     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1054     	      and jwot.header = p_header
1055     		  and jwot.user_sequence = p_user_sequence
1056     		  and jwot.interface_type = p_intf_type
1057     		  and jwot.status is null;
1058     	  --dbms_output.put_line('1 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1059 	    when 2 then
1060           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1061    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1062    	    	set jwot.terr_qual_id2 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1063     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1064     	      and jwot.header = p_header
1065     		  and jwot.user_sequence = p_user_sequence
1066     		  and jwot.interface_type = p_intf_type
1067     		  and jwot.status is null;
1068     	  --dbms_output.put_line('2 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1069 	    when 3 then
1070           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1071    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1072    	    	set jwot.terr_qual_id3 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1073     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1077     		  and jwot.status is null;
1074     	      and jwot.header = p_header
1075     		  and jwot.user_sequence = p_user_sequence
1076     		  and jwot.interface_type = p_intf_type
1078     	  --dbms_output.put_line('3 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1079 	    when 4 then
1080           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1081    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1082    	    	set jwot.terr_qual_id4 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1083     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1084     	      and jwot.header = p_header
1085     		  and jwot.user_sequence = p_user_sequence
1086     		  and jwot.interface_type = p_intf_type
1087     		  and jwot.status is null;
1088     	  --dbms_output.put_line('4 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1089 	    when 5 then
1090           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1091    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1092    	    	set jwot.terr_qual_id5 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1093     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1094     	      and jwot.header = p_header
1095     		  and jwot.user_sequence = p_user_sequence
1096     		  and jwot.interface_type = p_intf_type
1097     		  and jwot.status is null;
1098     	  --dbms_output.put_line('5 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1099 	    when 6 then
1100           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1101    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1102    	    	set jwot.terr_qual_id6 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1103     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1104     	      and jwot.header = p_header
1105     		  and jwot.user_sequence = p_user_sequence
1106     		  and jwot.interface_type = p_intf_type
1107     		  and jwot.status is null;
1108     	  --dbms_output.put_line('6 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1109 	    when 7 then
1110           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1111    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1112    	    	set jwot.terr_qual_id7 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1113     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1114     	      and jwot.header = p_header
1115     		  and jwot.user_sequence = p_user_sequence
1116     		  and jwot.interface_type = p_intf_type
1117     		  and jwot.status is null;
1118     	  --dbms_output.put_line('7 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1119 	    when 8 then
1120           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1121    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1122    	    	set jwot.terr_qual_id8 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1123     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1124     	      and jwot.header = p_header
1125     		  and jwot.user_sequence = p_user_sequence
1126     		  and jwot.interface_type = p_intf_type
1127     		  and jwot.status is null;
1128     	  --dbms_output.put_line('8 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1129 	    when 9 then
1130           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1131    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1132    	    	set jwot.terr_qual_id9 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1133     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1134     	      and jwot.header = p_header
1135     		  and jwot.user_sequence = p_user_sequence
1136     		  and jwot.interface_type = p_intf_type
1137     		  and jwot.status is null;
1138     	  --dbms_output.put_line('9 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1139 	    when 10 then
1140           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1141    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1142    	    	set jwot.terr_qual_id10 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1143     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1144     	      and jwot.header = p_header
1145     		  and jwot.user_sequence = p_user_sequence
1146     		  and jwot.interface_type = p_intf_type
1147     		  and jwot.status is null;
1148     	  --dbms_output.put_line('10 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1149 	    when 11 then
1150           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1151    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1152    	    	set jwot.terr_qual_id11 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1153     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1154     	      and jwot.header = p_header
1155     		  and jwot.user_sequence = p_user_sequence
1156     		  and jwot.interface_type = p_intf_type
1157     		  and jwot.status is null;
1158     	  --dbms_output.put_line('11 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1159 	    when 12 then
1160           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1161    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1162    	    	set jwot.terr_qual_id12 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1163     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1164     	      and jwot.header = p_header
1165     		  and jwot.user_sequence = p_user_sequence
1166     		  and jwot.interface_type = p_intf_type
1167     		  and jwot.status is null;
1168     	  --dbms_output.put_line('12 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1169 	    when 13 then
1170           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1171    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1172    	    	set jwot.terr_qual_id13 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1173     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1174     	      and jwot.header = p_header
1175     		  and jwot.user_sequence = p_user_sequence
1176     		  and jwot.interface_type = p_intf_type
1177     		  and jwot.status is null;
1178     	  --dbms_output.put_line('13 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1179 	    when 14 then
1180           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1181    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1182    	    	set jwot.terr_qual_id14 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1183     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1184     	      and jwot.header = p_header
1185     		  and jwot.user_sequence = p_user_sequence
1186     		  and jwot.interface_type = p_intf_type
1187     		  and jwot.status is null;
1188     	  --dbms_output.put_line('14 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1189 	    when 15 then
1190           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1191    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1192    	    	set jwot.terr_qual_id15 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1193     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1194     	      and jwot.header = p_header
1195     		  and jwot.user_sequence = p_user_sequence
1196     		  and jwot.interface_type = p_intf_type
1197     		  and jwot.status is null;
1198     	  --dbms_output.put_line('15 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1199 	    when 16 then
1200           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1201    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1202    	    	set jwot.terr_qual_id16 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1203     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1204     	      and jwot.header = p_header
1205     		  and jwot.user_sequence = p_user_sequence
1206     		  and jwot.interface_type = p_intf_type
1207     		  and jwot.status is null;
1208     	  --dbms_output.put_line('16 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1209 	    when 17 then
1210           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1211    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1212    	    	set jwot.terr_qual_id17 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1213     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1214     	      and jwot.header = p_header
1215     		  and jwot.user_sequence = p_user_sequence
1216     		  and jwot.interface_type = p_intf_type
1217     		  and jwot.status is null;
1218     	  --dbms_output.put_line('17 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1219 	    when 18 then
1220           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1221    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1222    	    	set jwot.terr_qual_id18 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1223     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1224     	      and jwot.header = p_header
1225     		  and jwot.user_sequence = p_user_sequence
1226     		  and jwot.interface_type = p_intf_type
1227     		  and jwot.status is null;
1228     	  --dbms_output.put_line('18 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1229 	    when 19 then
1230           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1231    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1232    	    	set jwot.terr_qual_id19 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1233     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1234     	      and jwot.header = p_header
1235     		  and jwot.user_sequence = p_user_sequence
1236     		  and jwot.interface_type = p_intf_type
1237     		  and jwot.status is null;
1238     	  --dbms_output.put_line('19 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1239 	    when 20 then
1240           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1241    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1242    	    	set jwot.terr_qual_id20 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1243     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1244     	      and jwot.header = p_header
1245     		  and jwot.user_sequence = p_user_sequence
1246     		  and jwot.interface_type = p_intf_type
1247     		  and jwot.status is null;
1248     	  --dbms_output.put_line('20 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1249 	    when 21 then
1250           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1251    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1252    	    	set jwot.terr_qual_id21 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1253     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1254     	      and jwot.header = p_header
1255     		  and jwot.user_sequence = p_user_sequence
1256     		  and jwot.interface_type = p_intf_type
1257     		  and jwot.status is null;
1258     	  --dbms_output.put_line('21 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1259 	    when 22 then
1260           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1261    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1262    	    	set jwot.terr_qual_id22 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1263     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1264     	      and jwot.header = p_header
1265     		  and jwot.user_sequence = p_user_sequence
1266     		  and jwot.interface_type = p_intf_type
1267     		  and jwot.status is null;
1268     	  --dbms_output.put_line('22 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1269 	    when 23 then
1270           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1271    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1275     		  and jwot.user_sequence = p_user_sequence
1272    	    	set jwot.terr_qual_id23 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1273     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1274     	      and jwot.header = p_header
1276     		  and jwot.interface_type = p_intf_type
1277     		  and jwot.status is null;
1278     	  --dbms_output.put_line('23 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1279 	    when 24 then
1280           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1281    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1282    	    	set jwot.terr_qual_id24 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1283     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1284     	      and jwot.header = p_header
1285     		  and jwot.user_sequence = p_user_sequence
1286     		  and jwot.interface_type = p_intf_type
1287     		  and jwot.status is null;
1288     	  --dbms_output.put_line('24 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1289 	    when 25 then
1290           forall j in l_terr_qual_rec.lay_seq_num.first..l_terr_qual_rec.lay_seq_num.last
1291    	        update JTY_WEBADI_OTH_TERR_INTF jwot
1292    	    	set jwot.terr_qual_id25 = l_Terr_Qual_Rec.TERR_QUAL_ID(j)
1293     	  	where jwot.lay_seq_num = l_terr_qual_rec.lay_seq_num(j)
1294     	      and jwot.header = p_header
1295     		  and jwot.user_sequence = p_user_sequence
1296     		  and jwot.interface_type = p_intf_type
1297     		  and jwot.status is null;
1298     	  --dbms_output.put_line('25 Update terr_qual_id, actual row processed:  '||SQL%ROWCOUNT);
1299 		else null;
1300 		end case;
1301 	end if;
1302   end loop;
1303 
1304   UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
1305     SET ACTION_FLAG = 'U'
1306     WHERE LAY_SEQ_NUM not in
1307 	  ( SELECT MIN(jwot2.LAY_SEQ_NUM)
1308 	    FROM JTY_WEBADI_OTH_TERR_INTF jwot2
1309     	WHERE jwot.ACTION_FLAG = jwot2.ACTION_FLAG
1310     	and jwot.header = jwot2.header
1311     	AND jwot.USER_SEQUENCE = jwot2.USER_SEQUENCE
1312     	AND jwot.interface_type = jwot2.interface_type
1313 		AND jwot.ACTION_FLAG = jwot2.action_flag
1314     	group by jwot.terr_id)
1315     AND jwot.ACTION_FLAG = 'C'
1316     and jwot.header = p_header
1317     AND jwot.USER_SEQUENCE = p_user_sequence
1318     AND jwot.interface_type = p_intf_type;
1319 
1320 END UPDATE_TERR_QUAL_ID;
1321 
1322 PROCEDURE delete_records(
1323   P_USER_SEQUENCE 		 IN NUMBER,
1324   P_INTF_TYPE			 IN VARCHAR2,
1325   p_action_flag			 IN VARCHAR2)
1326 IS
1327 
1328   cursor get_del_terr_csr(
1329   v_user_sequence 		  number,
1330   v_action_flag			  varchar2,
1331   v_intf_type			  varchar2,
1332   v_header				  varchar2
1333   ) IS
1334   SELECT TERR_ID, lay_seq_num
1335       FROM JTY_WEBADI_OTH_TERR_INTF
1336       WHERE interface_type = v_intf_type
1337         AND action_flag = v_action_flag
1338 		AND user_sequence = v_user_sequence
1339 		AND header = v_header
1340         AND status IS NULL;
1341 
1342   cursor get_del_qual_csr(
1343   v_user_sequence 		  number,
1344   v_action_flag			  varchar2,
1345   v_intf_type			  varchar2,
1346   v_header				  varchar2
1347   ) IS
1348   select sub.terr_qual_id, sub.lay_seq_num
1349 	from ( select terr_qual_id1 terr_qual_id, lay_seq_num
1350   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1351    	 		WHERE jwot.user_sequence = v_user_sequence
1352    	   		  and jwot.action_flag = v_action_flag
1353    	   		  and jwot.interface_type = v_intf_type
1354    	   		  and jwot.header		   = v_header
1355 			  and jwot.qual1_value_id is not null
1356    	   		  and jwot.status is null
1357 			union all
1358 			select terr_qual_id2 terr_qual_id, lay_seq_num
1359   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1360    	 		WHERE jwot.user_sequence = v_user_sequence
1361    	   		  and jwot.action_flag = v_action_flag
1362    	   		  and jwot.interface_type = v_intf_type
1363    	   		  and jwot.header		   = v_header
1364 			  and jwot.qual2_value_id is not null
1365    	   		  and jwot.status is null
1366 			union all
1367 			select terr_qual_id3 terr_qual_id, lay_seq_num
1368   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1369    	 		WHERE jwot.user_sequence = v_user_sequence
1370    	   		  and jwot.action_flag = v_action_flag
1371    	   		  and jwot.interface_type = v_intf_type
1372    	   		  and jwot.header		   = v_header
1373 			  and jwot.qual3_value_id is not null
1374    	   		  and jwot.status is null
1375 			union all
1376 			select terr_qual_id4 terr_qual_id, lay_seq_num
1377   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1378    	 		WHERE jwot.user_sequence = v_user_sequence
1379    	   		  and jwot.action_flag = v_action_flag
1380    	   		  and jwot.interface_type = v_intf_type
1381    	   		  and jwot.header		   = v_header
1382 			  and jwot.qual4_value_id is not null
1383    	   		  and jwot.status is null
1384 			union all
1385 			select terr_qual_id5 terr_qual_id, lay_seq_num
1386   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1387    	 		WHERE jwot.user_sequence = v_user_sequence
1388    	   		  and jwot.action_flag = v_action_flag
1389    	   		  and jwot.interface_type = v_intf_type
1390    	   		  and jwot.header		   = v_header
1391 			  and jwot.qual5_value_id is not null
1392    	   		  and jwot.status is null
1393 			union all
1394 			select terr_qual_id6 terr_qual_id, lay_seq_num
1395   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1396    	 		WHERE jwot.user_sequence = v_user_sequence
1397    	   		  and jwot.action_flag = v_action_flag
1398    	   		  and jwot.interface_type = v_intf_type
1399    	   		  and jwot.header		   = v_header
1403 			select terr_qual_id7 terr_qual_id, lay_seq_num
1400 			  and jwot.qual6_value_id is not null
1401    	   		  and jwot.status is null
1402 			union all
1404   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1405    	 		WHERE jwot.user_sequence = v_user_sequence
1406    	   		  and jwot.action_flag = v_action_flag
1407    	   		  and jwot.interface_type = v_intf_type
1408    	   		  and jwot.header		   = v_header
1409 			  and jwot.qual7_value_id is not null
1410    	   		  and jwot.status is null
1411 			union all
1412 			select terr_qual_id8 terr_qual_id, lay_seq_num
1413   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1414    	 		WHERE jwot.user_sequence = v_user_sequence
1415    	   		  and jwot.action_flag = v_action_flag
1416    	   		  and jwot.interface_type = v_intf_type
1417    	   		  and jwot.header		   = v_header
1418 			  and jwot.qual8_value_id is not null
1419    	   		  and jwot.status is null
1420 			union all
1421 			select terr_qual_id9 terr_qual_id, lay_seq_num
1422   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1423    	 		WHERE jwot.user_sequence = v_user_sequence
1424    	   		  and jwot.action_flag = v_action_flag
1425    	   		  and jwot.interface_type = v_intf_type
1426    	   		  and jwot.header		   = v_header
1427 			  and jwot.qual9_value_id is not null
1428    	   		  and jwot.status is null
1429 			union all
1430 			select terr_qual_id10 terr_qual_id, lay_seq_num
1431   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1432    	 		WHERE jwot.user_sequence = v_user_sequence
1433    	   		  and jwot.action_flag = v_action_flag
1434    	   		  and jwot.interface_type = v_intf_type
1435    	   		  and jwot.header		   = v_header
1436 			  and jwot.qual10_value_id is not null
1437    	   		  and jwot.status is null
1438 			union all
1439 			select terr_qual_id11 terr_qual_id, lay_seq_num
1440   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1441    	 		WHERE jwot.user_sequence = v_user_sequence
1442    	   		  and jwot.action_flag = v_action_flag
1443    	   		  and jwot.interface_type = v_intf_type
1444    	   		  and jwot.header		   = v_header
1445 			  and jwot.qual11_value_id is not null
1446    	   		  and jwot.status is null
1447 			union all
1448 			select terr_qual_id12 terr_qual_id, lay_seq_num
1449   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1450    	 		WHERE jwot.user_sequence = v_user_sequence
1451    	   		  and jwot.action_flag = v_action_flag
1452    	   		  and jwot.interface_type = v_intf_type
1453    	   		  and jwot.header		   = v_header
1454 			  and jwot.qual12_value_id is not null
1455    	   		  and jwot.status is null
1456 			union all
1457 			select terr_qual_id13 terr_qual_id, lay_seq_num
1458   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1459    	 		WHERE jwot.user_sequence = v_user_sequence
1460    	   		  and jwot.action_flag = v_action_flag
1461    	   		  and jwot.interface_type = v_intf_type
1462    	   		  and jwot.header		   = v_header
1463 			  and jwot.qual13_value_id is not null
1464    	   		  and jwot.status is null
1465 			union all
1466 			select terr_qual_id14 terr_qual_id, lay_seq_num
1467   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1468    	 		WHERE jwot.user_sequence = v_user_sequence
1469    	   		  and jwot.action_flag = v_action_flag
1470    	   		  and jwot.interface_type = v_intf_type
1471    	   		  and jwot.header		   = v_header
1472 			  and jwot.qual14_value_id is not null
1473    	   		  and jwot.status is null
1474 			union all
1475 			select terr_qual_id15 terr_qual_id, lay_seq_num
1476   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1477    	 		WHERE jwot.user_sequence = v_user_sequence
1478    	   		  and jwot.action_flag = v_action_flag
1479    	   		  and jwot.interface_type = v_intf_type
1480    	   		  and jwot.header		   = v_header
1481 			  and jwot.qual15_value_id is not null
1482    	   		  and jwot.status is null
1483 			union all
1484 			select terr_qual_id16 terr_qual_id, lay_seq_num
1485   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1486    	 		WHERE jwot.user_sequence = v_user_sequence
1487    	   		  and jwot.action_flag = v_action_flag
1488    	   		  and jwot.interface_type = v_intf_type
1489    	   		  and jwot.header		   = v_header
1490 			  and jwot.qual16_value_id is not null
1491    	   		  and jwot.status is null
1492 			union all
1493 			select terr_qual_id17 terr_qual_id, lay_seq_num
1494   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1495    	 		WHERE jwot.user_sequence = v_user_sequence
1496    	   		  and jwot.action_flag = v_action_flag
1497    	   		  and jwot.interface_type = v_intf_type
1498    	   		  and jwot.header		   = v_header
1499 			  and jwot.qual17_value_id is not null
1500    	   		  and jwot.status is null
1501 			union all
1502 			select terr_qual_id18 terr_qual_id, lay_seq_num
1503   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1504    	 		WHERE jwot.user_sequence = v_user_sequence
1505    	   		  and jwot.action_flag = v_action_flag
1506    	   		  and jwot.interface_type = v_intf_type
1507    	   		  and jwot.header		   = v_header
1508 			  and jwot.qual18_value_id is not null
1509    	   		  and jwot.status is null
1510 			union all
1511 			select terr_qual_id19 terr_qual_id, lay_seq_num
1512   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1513    	 		WHERE jwot.user_sequence = v_user_sequence
1514    	   		  and jwot.action_flag = v_action_flag
1515    	   		  and jwot.interface_type = v_intf_type
1516    	   		  and jwot.header		   = v_header
1517 			  and jwot.qual19_value_id is not null
1518    	   		  and jwot.status is null
1519 			union all
1523    	   		  and jwot.action_flag = v_action_flag
1520 			select terr_qual_id20 terr_qual_id, lay_seq_num
1521   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1522    	 		WHERE jwot.user_sequence = v_user_sequence
1524    	   		  and jwot.interface_type = v_intf_type
1525    	   		  and jwot.header		   = v_header
1526 			  and jwot.qual20_value_id is not null
1527    	   		  and jwot.status is null
1528 			union all
1529 			select terr_qual_id21 terr_qual_id, lay_seq_num
1530   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1531    	 		WHERE jwot.user_sequence = v_user_sequence
1532    	   		  and jwot.action_flag = v_action_flag
1533    	   		  and jwot.interface_type = v_intf_type
1534    	   		  and jwot.header		   = v_header
1535 			  and jwot.qual21_value_id is not null
1536    	   		  and jwot.status is null
1537 			union all
1538 			select terr_qual_id22 terr_qual_id, lay_seq_num
1539   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1540    	 		WHERE jwot.user_sequence = v_user_sequence
1541    	   		  and jwot.action_flag = v_action_flag
1542    	   		  and jwot.interface_type = v_intf_type
1543    	   		  and jwot.header		   = v_header
1544 			  and jwot.qual22_value_id is not null
1545    	   		  and jwot.status is null
1546 			union all
1547 			select terr_qual_id23 terr_qual_id, lay_seq_num
1548   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1549    	 		WHERE jwot.user_sequence = v_user_sequence
1550    	   		  and jwot.action_flag = v_action_flag
1551    	   		  and jwot.interface_type = v_intf_type
1552    	   		  and jwot.header		   = v_header
1553 			  and jwot.qual23_value_id is not null
1554    	   		  and jwot.status is null
1555 			union all
1556 			select terr_qual_id24 terr_qual_id, lay_seq_num
1557   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1558    	 		WHERE jwot.user_sequence = v_user_sequence
1559    	   		  and jwot.action_flag = v_action_flag
1560    	   		  and jwot.interface_type = v_intf_type
1561    	   		  and jwot.header		   = v_header
1562 			  and jwot.qual24_value_id is not null
1563    	   		  and jwot.status is null
1564 			union all
1565 			select terr_qual_id25 terr_qual_id, lay_seq_num
1566   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1567    	 		WHERE jwot.user_sequence = v_user_sequence
1568    	   		  and jwot.action_flag = v_action_flag
1569    	   		  and jwot.interface_type = v_intf_type
1570    	   		  and jwot.header		   = v_header
1571 			  and jwot.qual25_value_id is not null
1572    	   		  and jwot.status is null
1573 			  ) sub;
1574 
1575  cursor get_del_qual_val_csr(
1576   v_user_sequence 		  number,
1577   v_action_flag			  varchar2,
1578   v_intf_type			  varchar2,
1579   v_header				  varchar2
1580   ) IS
1581   select sub.qual_value_id, sub.lay_seq_num
1582 	from ( select qual1_value_id qual_value_id, lay_seq_num
1583   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1584    	 		WHERE jwot.user_sequence = v_user_sequence
1585    	   		  and jwot.action_flag = v_action_flag
1586    	   		  and jwot.interface_type = v_intf_type
1587    	   		  and jwot.header		   = v_header
1588 			  and jwot.qual1_value_id is not null
1589    	   		  and jwot.status is null
1590 			union all
1591 			select qual2_value_id qual_value_id, lay_seq_num
1592   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1593    	 		WHERE jwot.user_sequence = v_user_sequence
1594    	   		  and jwot.action_flag = v_action_flag
1595    	   		  and jwot.interface_type = v_intf_type
1596    	   		  and jwot.header		   = v_header
1597 			  and jwot.qual2_value_id is not null
1598    	   		  and jwot.status is null
1599 			union all
1600 			select qual3_value_id qual_value_id, lay_seq_num
1601   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1602    	 		WHERE jwot.user_sequence = v_user_sequence
1603    	   		  and jwot.action_flag = v_action_flag
1604    	   		  and jwot.interface_type = v_intf_type
1605    	   		  and jwot.header		   = v_header
1606 			  and jwot.qual3_value_id is not null
1607    	   		  and jwot.status is null
1608 			union all
1609 			select qual4_value_id qual_value_id, lay_seq_num
1610   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1611    	 		WHERE jwot.user_sequence = v_user_sequence
1612    	   		  and jwot.action_flag = v_action_flag
1613    	   		  and jwot.interface_type = v_intf_type
1614    	   		  and jwot.header		   = v_header
1615 			  and jwot.qual4_value_id is not null
1616    	   		  and jwot.status is null
1617 			union all
1618 			select qual5_value_id qual_value_id, lay_seq_num
1619   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1620    	 		WHERE jwot.user_sequence = v_user_sequence
1621    	   		  and jwot.action_flag = v_action_flag
1622    	   		  and jwot.interface_type = v_intf_type
1623    	   		  and jwot.header		   = v_header
1624 			  and jwot.qual5_value_id is not null
1625    	   		  and jwot.status is null
1626 			union all
1627 			select qual6_value_id qual_value_id, lay_seq_num
1628   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1629    	 		WHERE jwot.user_sequence = v_user_sequence
1630    	   		  and jwot.action_flag = v_action_flag
1631    	   		  and jwot.interface_type = v_intf_type
1632    	   		  and jwot.header		   = v_header
1633 			  and jwot.qual6_value_id is not null
1634    	   		  and jwot.status is null
1635 			union all
1636 			select qual7_value_id qual_value_id, lay_seq_num
1637   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1638    	 		WHERE jwot.user_sequence = v_user_sequence
1639    	   		  and jwot.action_flag = v_action_flag
1640    	   		  and jwot.interface_type = v_intf_type
1644 			union all
1641    	   		  and jwot.header		   = v_header
1642 			  and jwot.qual7_value_id is not null
1643    	   		  and jwot.status is null
1645 			select qual8_value_id qual_value_id, lay_seq_num
1646   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1647    	 		WHERE jwot.user_sequence = v_user_sequence
1648    	   		  and jwot.action_flag = v_action_flag
1649    	   		  and jwot.interface_type = v_intf_type
1650    	   		  and jwot.header		   = v_header
1651 			  and jwot.qual8_value_id is not null
1652    	   		  and jwot.status is null
1653 			union all
1654 			select qual9_value_id qual_value_id, lay_seq_num
1655   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1656    	 		WHERE jwot.user_sequence = v_user_sequence
1657    	   		  and jwot.action_flag = v_action_flag
1658    	   		  and jwot.interface_type = v_intf_type
1659    	   		  and jwot.header		   = v_header
1660 			  and jwot.qual9_value_id is not null
1661    	   		  and jwot.status is null
1662 			union all
1663 			select qual10_value_id qual_value_id, lay_seq_num
1664   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1665    	 		WHERE jwot.user_sequence = v_user_sequence
1666    	   		  and jwot.action_flag = v_action_flag
1667    	   		  and jwot.interface_type = v_intf_type
1668    	   		  and jwot.header		   = v_header
1669 			  and jwot.qual10_value_id is not null
1670    	   		  and jwot.status is null
1671 			union all
1672 			select qual11_value_id qual_value_id, lay_seq_num
1673   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1674    	 		WHERE jwot.user_sequence = v_user_sequence
1675    	   		  and jwot.action_flag = v_action_flag
1676    	   		  and jwot.interface_type = v_intf_type
1677    	   		  and jwot.header		   = v_header
1678 			  and jwot.qual11_value_id is not null
1679    	   		  and jwot.status is null
1680 			union all
1681 			select qual12_value_id qual_value_id, lay_seq_num
1682   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1683    	 		WHERE jwot.user_sequence = v_user_sequence
1684    	   		  and jwot.action_flag = v_action_flag
1685    	   		  and jwot.interface_type = v_intf_type
1686    	   		  and jwot.header		   = v_header
1687 			  and jwot.qual12_value_id is not null
1688    	   		  and jwot.status is null
1689 			union all
1690 			select qual13_value_id qual_value_id, lay_seq_num
1691   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1692    	 		WHERE jwot.user_sequence = v_user_sequence
1693    	   		  and jwot.action_flag = v_action_flag
1694    	   		  and jwot.interface_type = v_intf_type
1695    	   		  and jwot.header		   = v_header
1696 			  and jwot.qual13_value_id is not null
1697    	   		  and jwot.status is null
1698 			union all
1699 			select qual14_value_id qual_value_id, lay_seq_num
1700   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1701    	 		WHERE jwot.user_sequence = v_user_sequence
1702    	   		  and jwot.action_flag = v_action_flag
1703    	   		  and jwot.interface_type = v_intf_type
1704    	   		  and jwot.header		   = v_header
1705 			  and jwot.qual14_value_id is not null
1706    	   		  and jwot.status is null
1707 			union all
1708 			select qual15_value_id qual_value_id, lay_seq_num
1709   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1710    	 		WHERE jwot.user_sequence = v_user_sequence
1711    	   		  and jwot.action_flag = v_action_flag
1712    	   		  and jwot.interface_type = v_intf_type
1713    	   		  and jwot.header		   = v_header
1714 			  and jwot.qual15_value_id is not null
1715    	   		  and jwot.status is null
1716 			union all
1717 			select qual16_value_id qual_value_id, lay_seq_num
1718   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1719    	 		WHERE jwot.user_sequence = v_user_sequence
1720    	   		  and jwot.action_flag = v_action_flag
1721    	   		  and jwot.interface_type = v_intf_type
1722    	   		  and jwot.header		   = v_header
1723 			  and jwot.qual16_value_id is not null
1724    	   		  and jwot.status is null
1725 			union all
1726 			select qual17_value_id qual_value_id, lay_seq_num
1727   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1728    	 		WHERE jwot.user_sequence = v_user_sequence
1729    	   		  and jwot.action_flag = v_action_flag
1730    	   		  and jwot.interface_type = v_intf_type
1731    	   		  and jwot.header		   = v_header
1732 			  and jwot.qual17_value_id is not null
1733    	   		  and jwot.status is null
1734 			union all
1735 			select qual18_value_id qual_value_id, lay_seq_num
1736   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1737    	 		WHERE jwot.user_sequence = v_user_sequence
1738    	   		  and jwot.action_flag = v_action_flag
1739    	   		  and jwot.interface_type = v_intf_type
1740    	   		  and jwot.header		   = v_header
1741 			  and jwot.qual18_value_id is not null
1742    	   		  and jwot.status is null
1743 			union all
1744 			select qual19_value_id qual_value_id, lay_seq_num
1745   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1746    	 		WHERE jwot.user_sequence = v_user_sequence
1747    	   		  and jwot.action_flag = v_action_flag
1748    	   		  and jwot.interface_type = v_intf_type
1749    	   		  and jwot.header		   = v_header
1750 			  and jwot.qual19_value_id is not null
1751    	   		  and jwot.status is null
1752 			union all
1753 			select qual20_value_id qual_value_id, lay_seq_num
1754   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1755    	 		WHERE jwot.user_sequence = v_user_sequence
1756    	   		  and jwot.action_flag = v_action_flag
1757    	   		  and jwot.interface_type = v_intf_type
1758    	   		  and jwot.header		   = v_header
1759 			  and jwot.qual20_value_id is not null
1763   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1760    	   		  and jwot.status is null
1761 			union all
1762 			select qual21_value_id qual_value_id, lay_seq_num
1764    	 		WHERE jwot.user_sequence = v_user_sequence
1765    	   		  and jwot.action_flag = v_action_flag
1766    	   		  and jwot.interface_type = v_intf_type
1767    	   		  and jwot.header		   = v_header
1768 			  and jwot.qual21_value_id is not null
1769    	   		  and jwot.status is null
1770 			union all
1771 			select qual22_value_id qual_value_id, lay_seq_num
1772   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1773    	 		WHERE jwot.user_sequence = v_user_sequence
1774    	   		  and jwot.action_flag = v_action_flag
1775    	   		  and jwot.interface_type = v_intf_type
1776    	   		  and jwot.header		   = v_header
1777 			  and jwot.qual22_value_id is not null
1778    	   		  and jwot.status is null
1779 			union all
1780 			select qual23_value_id qual_value_id, lay_seq_num
1781   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1782    	 		WHERE jwot.user_sequence = v_user_sequence
1783    	   		  and jwot.action_flag = v_action_flag
1784    	   		  and jwot.interface_type = v_intf_type
1785    	   		  and jwot.header		   = v_header
1786 			  and jwot.qual23_value_id is not null
1787    	   		  and jwot.status is null
1788 			union all
1789 			select qual24_value_id qual_value_id, lay_seq_num
1790   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1791    	 		WHERE jwot.user_sequence = v_user_sequence
1792    	   		  and jwot.action_flag = v_action_flag
1793    	   		  and jwot.interface_type = v_intf_type
1794    	   		  and jwot.header		   = v_header
1795 			  and jwot.qual24_value_id is not null
1796    	   		  and jwot.status is null
1797 			union all
1798 			select qual25_value_id qual_value_id, lay_seq_num
1799   	 	  	from JTY_WEBADI_OTH_TERR_INTF jwot
1800    	 		WHERE jwot.user_sequence = v_user_sequence
1801    	   		  and jwot.action_flag = v_action_flag
1802    	   		  and jwot.interface_type = v_intf_type
1803    	   		  and jwot.header		   = v_header
1804 			  and jwot.qual25_value_id is not null
1805    	   		  and jwot.status is null
1806 			  ) sub;
1807 
1808   cursor get_del_rsc_csr(
1809   v_user_sequence 		  number,
1810   v_action_flag			  varchar2,
1811   v_intf_type			  varchar2,
1812   v_header				  varchar2
1813   ) IS
1814   SELECT jwr.TERR_RSC_ID, jwot.lay_seq_num
1815       FROM JTY_WEBADI_OTH_TERR_INTF jwot,
1816 	  JTY_WEBADI_RESOURCES jwr
1817       WHERE jwot.lay_seq_num = jwr.lay_seq_num
1818 		and jwot.header = jwr.header
1819 		and jwot.user_sequence = jwr.user_sequence
1820 		AND jwot.interface_type = jwr.interface_type
1821 	    AND jwot.interface_type = v_intf_type
1822         AND jwot.action_flag = v_action_flag
1823 		AND jwot.user_sequence = v_user_sequence
1824 		AND jwot.header = v_header
1825         AND jwot.status IS NULL;
1826 
1827   TYPE DEL_TERR_REC_TYPE is RECORD
1828   ( terr_id				 number_tbl_type,
1829     lay_seq_num			 number_tbl_type);
1830 
1831   TYPE DEL_QUAL_REC_TYPE is RECORD
1832   ( terr_qual_id			 number_tbl_type,
1833 	lay_seq_num				 number_tbl_type);
1834 
1835   TYPE DEL_QUAL_VAL_REC_TYPE is RECORD
1836   ( qual_value_id			 number_tbl_type,
1837 	lay_seq_num				 number_tbl_type);
1838 
1839   TYPE DEL_RSC_REC_TYPE is RECORD
1840   ( terr_rsc_id			 number_tbl_type,
1841 	lay_seq_num			 number_tbl_type);
1842 
1843   l_del_terr_rec		 del_terr_rec_type;
1844   l_del_qual_rec		 del_qual_rec_type;
1845   l_del_rsc_rec		 	 del_rsc_rec_type;
1846   l_del_qual_val_rec		 del_qual_val_rec_type;
1847 
1848   l_header		varchar2(15);
1849   l_row_count	number;
1850   l_intf_type	VARCHAR2(1);
1851   l_count		NUMBER;
1852   l_api_version_number      CONSTANT NUMBER   := 1.0;
1853   x_return_status	 varchar2(255);
1854   X_Msg_Count		 number;
1855   X_Msg_Data		 varchar2(255);
1856   l_qual_values_count number;
1857 
1858 BEGIN
1859 
1860   -- process territory deletion
1861   x_return_status := FND_API.G_RET_STS_SUCCESS;
1862   l_header := 'TERR';
1863   open get_del_terr_csr(p_user_sequence, p_action_flag, p_intf_type, l_header);
1864   fetch get_del_terr_csr bulk collect into l_del_terr_rec.terr_id, l_del_terr_rec.lay_seq_num;
1865   close get_del_terr_csr;
1866 
1867   if l_del_terr_rec.terr_id.count > 0 then
1868   BEGIN
1869     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1870 	--Delete Territory Values
1871 	  DELETE from JTF_TERR_VALUES_ALL WHERE TERR_QUAL_ID IN
1872           ( SELECT TERR_QUAL_ID FROM JTF_TERR_QUAL_ALL
1873 		  	WHERE TERR_ID = l_del_terr_rec.terr_id(i) );
1874 
1875     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1876     --Delete Territory Qualifer records
1877       DELETE from JTF_TERR_QUAL_ALL WHERE TERR_ID = l_del_terr_rec.terr_id(i);
1878 
1879     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1880     --Delete Territory qual type usgs
1881       DELETE from JTF_TERR_QTYPE_USGS_ALL WHERE TERR_ID = l_del_terr_rec.terr_id(i);
1882 
1883     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1884     --Delete Territory usgs
1885       DELETE from JTF_TERR_USGS_ALL WHERE TERR_ID = l_del_terr_rec.terr_id(i);
1886 
1887     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1888     --Delete Territory Resource Access
1889       DELETE from JTF_TERR_RSC_ACCESS_ALL WHERE TERR_RSC_ID IN
1893     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1890           ( SELECT TERR_RSC_ID FROM JTF_TERR_RSC_ALL
1891 		  	WHERE TERR_ID = l_del_terr_rec.terr_id(i) );
1892 
1894     --Delete the Territory Resource records
1895       DELETE from JTF_TERR_RSC_ALL Where TERR_ID = l_del_terr_rec.terr_id(i);
1896 
1897     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1898     --Delete Territory record
1899       DELETE from JTF_TERR_ALL WHERE TERR_ID = l_del_terr_rec.terr_id(i);
1900 
1901 	-- update all records including the qual and rsc which will be deleted
1902     forall i in l_del_terr_rec.terr_id.first..l_del_terr_rec.terr_id.last
1903       update JTY_WEBADI_OTH_TERR_INTF
1904 	  set status = x_return_status
1905 	  where terr_id = l_del_terr_rec.terr_id(i)
1906 	    and user_sequence = p_user_sequence
1907 	    and interface_type = p_intf_type;
1908 
1909 	EXCEPTION
1910 	  when others then
1911         x_return_status := FND_API.G_RET_STS_ERROR;
1912 		fnd_message.clear;
1913         fnd_message.set_name ('JTF', 'JTY_OTH_TERR_DELETE_TERR');
1914         X_Msg_Data := fnd_message.get();
1915 
1916     	forall i in l_del_terr_rec.lay_seq_num.first..l_del_terr_rec.lay_seq_num.last
1917 	    update JTY_WEBADI_OTH_TERR_INTF
1918 		set status = x_return_status,
1919 		error_msg = x_msg_data
1920 		where lay_seq_num = l_del_terr_rec.lay_seq_num(i)
1921 		  and user_sequence = p_user_sequence
1922 		  and interface_type = p_intf_type;
1923   END;
1924   end if;
1925 
1926   -- process qualifier deletion
1927   x_return_status := FND_API.G_RET_STS_SUCCESS;
1928   l_header := 'QUAL';
1929  open get_del_qual_val_csr(p_user_sequence, p_action_flag, p_intf_type, l_header);
1930   fetch get_del_qual_val_csr bulk collect into
1931     l_del_qual_val_rec.qual_value_id, l_del_qual_val_rec.lay_seq_num;
1932   close get_del_qual_val_csr;
1933 
1934   if l_del_qual_val_rec.qual_value_id.count > 0 then
1935     BEGIN
1936 	  FORALL i in l_del_qual_val_rec.qual_value_id.first..l_del_qual_val_rec.qual_value_id.last
1937             DELETE FROM JTF_TERR_VALUES_ALL
1938     	WHERE TERR_VALUE_ID = l_del_qual_val_rec.qual_value_id(i);
1939 
1940        FOR l_del_qual_rec in get_del_qual_csr(p_user_sequence, p_action_flag, p_intf_type, l_header)
1941           Loop
1942                 SELECT COUNT(*) INTO l_qual_values_count FROM JTF_TERR_VALUES_ALL WHERE TERR_QUAL_ID = l_del_qual_rec.terr_qual_id;
1943                 IF l_qual_values_count = 0 THEN
1944                   DELETE from JTF_TERR_QUAL_ALL jtq
1945                   WHERE jtq.terr_qual_id = l_del_qual_rec.terr_qual_id;
1946                 END IF;
1947          END Loop;
1948 
1949       forall i in l_del_qual_val_rec.lay_seq_num.first..l_del_qual_val_rec.lay_seq_num.last
1950 	    update JTY_WEBADI_OTH_TERR_INTF
1951 		set status = x_return_status
1952 		where lay_seq_num = l_del_qual_val_rec.lay_seq_num(i)
1953 		  and user_sequence = p_user_sequence
1954 		  and action_flag = p_action_flag
1955 		  and header = l_header
1956 		  and interface_type = p_intf_type;
1957 
1958 	EXCEPTION
1959 	  when others then
1960         x_return_status := FND_API.G_RET_STS_ERROR;
1961 		fnd_message.clear;
1962         fnd_message.set_name ('JTF', 'JTY_OTH_TERR_DELETE_QUAL');
1963         X_Msg_Data := fnd_message.get();
1964 
1965     	forall i in l_del_qual_val_rec.lay_seq_num.first..l_del_qual_val_rec.lay_seq_num.last
1966 	    update JTY_WEBADI_OTH_TERR_INTF
1967 		set status = x_return_status,
1968 		error_msg = x_msg_data
1969 		where lay_seq_num = l_del_qual_val_rec.lay_seq_num(i)
1970 		  and user_sequence = p_user_sequence
1971 		  and action_flag = p_action_flag
1972 		  and header = l_header
1973 		  and interface_type = p_intf_type;
1974 	END;
1975   end if;
1976 
1977   x_return_status := FND_API.G_RET_STS_SUCCESS;
1978   l_header := 'RSC';
1979   open get_del_rsc_csr(p_user_sequence, p_action_flag, p_intf_type, l_header);
1980   fetch get_del_rsc_csr bulk collect into
1981     l_del_rsc_rec.terr_rsc_id, l_del_rsc_rec.lay_seq_num;
1982   close get_del_rsc_csr;
1983 
1984   if l_del_rsc_rec.terr_rsc_id.count > 0 then
1985     BEGIN
1986     --Delete Territory Resource Access
1987       forall i in l_del_rsc_rec.terr_rsc_id.first..l_del_rsc_rec.terr_rsc_id.last
1988         DELETE FROM JTF_TERR_RSC_ACCESS_ALL
1989 	    WHERE TERR_RSC_ID = l_del_rsc_rec.terr_rsc_id(i);
1990 
1991     -- Delete the Territory Resource records
1992       forall i in l_del_rsc_rec.terr_rsc_id.first..l_del_rsc_rec.terr_rsc_id.last
1993         DELETE FROM JTF_TERR_RSC_ALL
1994 	    WHERE TERR_RSC_ID = l_del_rsc_rec.terr_rsc_id(i);
1995 
1996       forall i in l_del_rsc_rec.lay_seq_num.first..l_del_rsc_rec.lay_seq_num.last
1997 	    update JTY_WEBADI_OTH_TERR_INTF
1998 		set status = x_return_status
1999 		where lay_seq_num = l_del_rsc_rec.lay_seq_num(i)
2000 		  and user_sequence = p_user_sequence
2001 		  and action_flag = p_action_flag
2002 		  and header = l_header
2003 		  and interface_type = p_intf_type;
2004 
2005 	EXCEPTION
2006 	  when others then
2007         x_return_status := FND_API.G_RET_STS_ERROR;
2008 		fnd_message.clear;
2009         fnd_message.set_name ('JTF', 'JTY_OTH_TERR_DELETE_RSC');
2010         X_Msg_Data := fnd_message.get();
2011 
2012     	forall i in l_del_rsc_rec.lay_seq_num.first..l_del_rsc_rec.lay_seq_num.last
2013 	    update JTY_WEBADI_OTH_TERR_INTF
2014 		set status = x_return_status,
2015 		error_msg = x_msg_data
2019 		  and header = l_header
2016 		where lay_seq_num = l_del_rsc_rec.lay_seq_num(i)
2017 		  and user_sequence = p_user_sequence
2018 		  and action_flag = p_action_flag
2020 		  and interface_type = p_intf_type;
2021 	END;
2022   end if;
2023 END delete_records;
2024 
2025 PROCEDURE INSERT_TERR_QUAL(
2026   p_terr_qual_rec 			IN OUT NOCOPY  Terr_Qual_Rec_Type,
2027   x_return_status			OUT NOCOPY	 VARCHAR2,
2028   x_msg_data				OUT NOCOPY	 VARCHAR2
2029 ) IS
2030 	l_OVERLAP_ALLOWED_FLAG varchar2(1) := 'Y';
2031 	l_terr_qual_rec		   terr_qual_rec_type := p_terr_qual_rec;
2032 BEGIN
2033   if (l_Terr_Qual_Rec.TERR_ID.count > 0) then
2034     --dbms_output.put_line('U: get_qual_csr: create MA, rowcount: ' || l_Terr_Qual_Rec.TERR_ID.count);
2035     forall i in l_Terr_Qual_Rec.TERR_ID.first..l_Terr_Qual_Rec.TERR_ID.last
2036       INSERT INTO JTF_TERR_QUAL_ALL(
2037               TERR_QUAL_ID,
2038               LAST_UPDATE_DATE,
2039               LAST_UPDATED_BY,
2040               CREATION_DATE,
2041               CREATED_BY,
2042               LAST_UPDATE_LOGIN,
2043               TERR_ID,
2044               QUAL_USG_ID,
2045               ORG_ID,
2046 			  OVERLAP_ALLOWED_FLAG)
2047        VALUES (
2048    	     --JTF_TERR_QUAL_s.nextval,
2049 		 l_Terr_Qual_Rec.TERR_QUAL_ID(i),
2050    	  	 l_Terr_Qual_Rec.last_update_date(i),
2051    	  	 l_Terr_Qual_Rec.last_updated_by(i),
2052    	  	 l_Terr_Qual_Rec.creation_date(i),
2053    	  	 l_Terr_Qual_Rec.created_by(i),
2054    	  	 l_Terr_Qual_Rec.last_update_login(i),
2055    	  	 l_Terr_Qual_Rec.terr_id(i),
2056    	  	 l_Terr_Qual_Rec.qual_usg_id(i),
2057    	  	 l_Terr_Qual_Rec.org_id(i),
2058 		 l_OVERLAP_ALLOWED_FLAG );
2059 --	   RETURNING TERR_QUAL_ID bulk collect into l_Terr_Qual_Rec.TERR_QUAL_ID;
2060 	--dbms_output.put_line(' U: get_qual_csr:MA, actual row processed:  '||SQL%ROWCOUNT);
2061 
2062   end if; --get_qual_csr
2063 
2064 	EXCEPTION
2065 	  WHEN OTHERS THEN
2066         x_return_status := FND_API.G_RET_STS_ERROR;
2067 		fnd_message.clear;
2068         fnd_message.set_name ('JTF', 'JTY_OTH_TERR_CREATE_QUAL');
2069         X_Msg_Data := fnd_message.get();
2070 
2071 END INSERT_TERR_QUAL;
2072 
2073 PROCEDURE INSERT_TERR_VALUES (
2074   p_terr_qual_rec 			IN Terr_Qual_Rec_Type,
2075   p_terr_values_out_rec		OUT NOCOPY	 Terr_values_out_rec_type,
2076   x_return_status			OUT NOCOPY	 VARCHAR2,
2077   x_msg_data				OUT NOCOPY	 VARCHAR2
2078 ) IS
2079   l_Terr_Values_Rec Terr_values_rec_type;
2080   l_terr_qual_rec Terr_Qual_Rec_Type := p_terr_qual_rec;
2081 
2082 BEGIN
2083   IF (l_Terr_Qual_Rec.TERR_QUAL_ID.count > 0) THEN
2084     for i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last loop
2085 
2086 	   l_Terr_Values_Rec.LAST_UPDATE_DATE(i)  := l_Terr_Qual_Rec.LAST_UPDATE_DATE(i);
2087 	   l_Terr_Values_Rec.LAST_UPDATED_BY(i)   := l_Terr_Qual_Rec.LAST_UPDATED_BY(i);
2088 	   l_Terr_Values_Rec.CREATION_DATE(i) 	  := l_Terr_Qual_Rec.CREATION_DATE(i);
2089 	   l_Terr_Values_Rec.CREATED_BY(i)    	  := l_Terr_Qual_Rec.CREATED_BY(i);
2090 	   l_Terr_Values_Rec.LAST_UPDATE_LOGIN(i) := l_Terr_Qual_Rec.LAST_UPDATE_LOGIN(i);
2091 	   l_Terr_Values_Rec.TERR_QUAL_ID(i)  	  := l_Terr_Qual_Rec.TERR_QUAL_ID(i);
2092 	   l_Terr_Values_Rec.COMPARISON_OPERATOR(i) := l_Terr_Qual_Rec.qual_cond(i);
2093        l_Terr_Values_Rec.ID_USED_FLAG(i)        := l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG(i);
2094 	   l_Terr_Values_Rec.ORG_ID(i)              := l_Terr_Qual_Rec.ORG_ID(i);
2095 	   l_Terr_Values_Rec.TERR_VALUE_ID(i) := NULL;
2096         l_Terr_Values_Rec.LOW_VALUE_CHAR(i):= NULL;
2097         l_Terr_Values_Rec.HIGH_VALUE_CHAR(i):= NULL;
2098         l_Terr_Values_Rec.LOW_VALUE_NUMBER(i):= NULL;
2099         l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i):= NULL;
2100         l_Terr_Values_Rec.INTEREST_TYPE_ID(i):= NULL;
2101         l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i):= NULL;
2102         l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i):= NULL;
2103         l_Terr_Values_Rec.CURRENCY_CODE(i):= NULL;
2104         l_Terr_Values_Rec.ID_USED_FLAG(i):= NULL;
2105         l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i):= NULL;
2106 --        l_Terr_Values_Rec.ORG_ID(i):= NULL;
2107         l_Terr_Values_Rec.VALUE1_ID(i):= NULL;
2108         l_Terr_Values_Rec.VALUE2_ID(i):= NULL;
2109         l_Terr_Values_Rec.VALUE3_ID(i):= NULL;
2110 
2111 	   case
2112 	     when l_Terr_Qual_Rec.qual_type(i) = 'CHAR' then
2113 			   --dbms_output.put_line('in ' || l_Terr_Qual_Rec.qual_type(i));
2114 		   IF l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG(i) = 'N' then
2115 			   --dbms_output.put_line('in flag' || l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG(i));
2116 		     l_Terr_Values_Rec.LOW_VALUE_CHAR(i) := l_Terr_Qual_Rec.qual_value1(i);
2117 			 if l_Terr_Qual_Rec.qual_value2.count > 0 then
2118 			   --dbms_output.put_line('in l_Terr_Qual_Rec.qual_value2.count');
2119 		       l_Terr_Values_Rec.HIGH_VALUE_CHAR(i) := l_Terr_Qual_Rec.qual_value2(i);
2120 			   --dbms_output.put_line('value: ' || NVL(l_Terr_Values_Rec.HIGH_VALUE_CHAR(i),'ABCDE'));
2121 			 else l_Terr_Values_Rec.HIGH_VALUE_CHAR(i) := NULL;
2122 			 end if;
2123 		   else
2124 		     l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
2125 		   end if;
2126          when l_Terr_Qual_Rec.qual_type(i) = 'DEP_2FIELDS_1CHAR_1ID' then
2127 		   l_Terr_Values_Rec.LOW_VALUE_CHAR(i) := l_Terr_Qual_Rec.qual_value1(i);
2128 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
2129 		     l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
2133            ('CHAR_2IDS', 'DEP_2FIELDS', 'DEP_2FIELDS_CHAR_2IDS') then
2130 		   else l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i) := NULL;
2131 		   end if;
2132          when l_Terr_Qual_Rec.qual_type(i) in
2134 		   l_Terr_Values_Rec.VALUE1_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
2135 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
2136 		     l_Terr_Values_Rec.VALUE2_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
2137 		   else l_Terr_Values_Rec.VALUE2_ID(i) := NULL;
2138 		   end if;
2139          when l_Terr_Qual_Rec.qual_type(i) = 'DEP_3FIELDS_CHAR_3IDS' then
2140 		   l_Terr_Values_Rec.VALUE1_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
2141 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
2142 		     l_Terr_Values_Rec.VALUE2_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
2143 		   else l_Terr_Values_Rec.VALUE2_ID(i) := NULL;
2144 		   end if;
2145 		   if l_Terr_Qual_Rec.qual_value3.count > 0 then
2146 		     l_Terr_Values_Rec.VALUE3_ID(i) := l_Terr_Qual_Rec.qual_value3(i);
2147 		   else l_Terr_Values_Rec.VALUE3_ID(i) := NULL;
2148 		   end if;
2149          when l_Terr_Qual_Rec.qual_type(i) = 'INTEREST_TYPE' then
2150 		   l_Terr_Values_Rec.INTEREST_TYPE_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
2151 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
2152 		     l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
2153 		   else l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i) := NULL;
2154 		   end if;
2155 		   if l_Terr_Qual_Rec.qual_value3.count > 0 then
2156 		     l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i) := l_Terr_Qual_Rec.qual_value3(i);
2157 		   else l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i) := NULL;
2158 		   end if;
2159          when l_Terr_Qual_Rec.qual_type(i) = 'NUMERIC' then
2160 		   l_Terr_Values_Rec.LOW_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value1(i);
2161 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
2162 		     l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value2(i);
2163 		   else l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := NULL;
2164 		   end if;
2165          when l_Terr_Qual_Rec.qual_type(i) = 'CURRENCY' then
2166 		   l_Terr_Values_Rec.LOW_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value1(i);
2167 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
2168 		     l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value2(i);
2169 		   else l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := NULL;
2170 		   end if;
2171 		   if l_Terr_Qual_Rec.qual_value3.count > 0 then
2172 		     l_Terr_Values_Rec.CURRENCY_CODE(i) := l_Terr_Qual_Rec.qual_value3(i);
2173 		   else l_Terr_Values_Rec.CURRENCY_CODE(i) := NULL;
2174 		   end if;
2175          else null;
2176 	   end case;
2177 	   --dbms_output.put_line('case ended');
2178 
2179 	end loop;
2180 
2181     --dbms_output.put_line('C: get_qual_csr: create TV, rowcount: ' || l_Terr_Values_Rec.TERR_QUAL_ID.count);
2182 	/*
2183 	for i in l_Terr_Values_Rec.TERR_QUAL_ID.first..l_Terr_Values_Rec.TERR_QUAL_ID.last loop
2184 	  --dbms_output.put_line(l_Terr_Values_Rec.LAST_UPDATED_BY(i)||', '||
2185         l_Terr_Values_Rec.LAST_UPDATE_DATE(i)||', '||
2186         l_Terr_Values_Rec.CREATED_BY(i)||', '||
2187         l_Terr_Values_Rec.CREATION_DATE(i)||', '||
2188         l_Terr_Values_Rec.LAST_UPDATE_LOGIN(i)||', '||
2189         l_Terr_Values_Rec.TERR_QUAL_ID(i)||', '||
2190         l_Terr_Values_Rec.COMPARISON_OPERATOR(i)||', '||
2191         l_Terr_Values_Rec.LOW_VALUE_CHAR(i)||', '||
2192         l_Terr_Values_Rec.HIGH_VALUE_CHAR(i)||', '||
2193         l_Terr_Values_Rec.LOW_VALUE_NUMBER(i)||', '||
2194         l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i)||', '||
2195         l_Terr_Values_Rec.INTEREST_TYPE_ID(i)||', '||
2196         l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i)||', '||
2197         l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i)||', '||
2198         l_Terr_Values_Rec.CURRENCY_CODE(i)||', '||
2199         l_Terr_Values_Rec.ID_USED_FLAG(i)||', '||
2200         l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i)||', '||
2201         l_Terr_Values_Rec.ORG_ID(i)||', '||
2202         l_Terr_Values_Rec.VALUE1_ID(i)||', '||
2203         l_Terr_Values_Rec.VALUE2_ID(i)||', '||
2204         l_Terr_Values_Rec.VALUE3_ID(i) );
2205 	end loop;
2206 */
2207 	forall i in l_Terr_Values_Rec.TERR_QUAL_ID.first..l_Terr_Values_Rec.TERR_QUAL_ID.last
2208       INSERT INTO JTF_TERR_VALUES_ALL(
2209         TERR_VALUE_ID,
2210         LAST_UPDATED_BY,
2211         LAST_UPDATE_DATE,
2212         CREATED_BY,
2213         CREATION_DATE,
2214         LAST_UPDATE_LOGIN,
2215         TERR_QUAL_ID,
2216         COMPARISON_OPERATOR,
2217         LOW_VALUE_CHAR,
2218         HIGH_VALUE_CHAR,
2219         LOW_VALUE_NUMBER,
2220         HIGH_VALUE_NUMBER,
2221         INTEREST_TYPE_ID,
2222         PRIMARY_INTEREST_CODE_ID,
2223         SECONDARY_INTEREST_CODE_ID,
2224         CURRENCY_CODE,
2225         ID_USED_FLAG,
2226         LOW_VALUE_CHAR_ID,
2227         ORG_ID,
2228         VALUE1_ID,
2229         VALUE2_ID,
2230         VALUE3_ID
2231        )
2232 	  VALUES (
2233         JTF_TERR_VALUES_s.nextval,
2234         l_Terr_Values_Rec.LAST_UPDATED_BY(i),
2235         l_Terr_Values_Rec.LAST_UPDATE_DATE(i),
2236         l_Terr_Values_Rec.CREATED_BY(i),
2237         l_Terr_Values_Rec.CREATION_DATE(i),
2238         l_Terr_Values_Rec.LAST_UPDATE_LOGIN(i),
2239         l_Terr_Values_Rec.TERR_QUAL_ID(i),
2240         l_Terr_Values_Rec.COMPARISON_OPERATOR(i),
2241         l_Terr_Values_Rec.LOW_VALUE_CHAR(i),
2242         l_Terr_Values_Rec.HIGH_VALUE_CHAR(i),
2243         l_Terr_Values_Rec.LOW_VALUE_NUMBER(i),
2247         l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i),
2244         l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i),
2245         l_Terr_Values_Rec.INTEREST_TYPE_ID(i),
2246         l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i),
2248         l_Terr_Values_Rec.CURRENCY_CODE(i),
2249         l_Terr_Values_Rec.ID_USED_FLAG(i),
2250         l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i),
2251         l_Terr_Values_Rec.ORG_ID(i),
2252         l_Terr_Values_Rec.VALUE1_ID(i),
2253         l_Terr_Values_Rec.VALUE2_ID(i),
2254         l_Terr_Values_Rec.VALUE3_ID(i)
2255        ) RETURNING TERR_VALUE_ID,TERR_QUAL_ID
2256 	   BULK COLLECT INTO p_terr_values_out_rec.TERR_VALUE_ID,
2257 	   		p_terr_values_out_rec.TERR_QUAL_ID;
2258 
2259 	--dbms_output.put_line(' C: get_qual_csr:TV, actual row processed:  '||SQL%ROWCOUNT);
2260 
2261     x_return_status := FND_API.G_RET_STS_SUCCESS;
2262   END IF; -- TERR_QUAL_ID count
2263 	EXCEPTION
2264 	  WHEN OTHERS THEN
2265         x_return_status := FND_API.G_RET_STS_ERROR;
2266 		fnd_message.clear;
2267         fnd_message.set_name ('JTF', 'JTY_OTH_TERR_CREATE_QUAL_VAL');
2268         X_Msg_Data := fnd_message.get();
2269 
2270 		--dbms_output.put_line(sqlerrm);
2271 
2272 END INSERT_TERR_VALUES;
2273 
2274 PROCEDURE UPDATE_TERR(
2275   p_user_sequence IN number,
2276   p_action_flag	  IN varchar2,
2277   x_return_status OUT NOCOPY varchar2,
2278   x_msg_data	  OUT NOCOPY varchar2
2279 ) IS
2280 
2281   CURSOR get_terr_all_csr (
2282     v_user_sequence		 number,
2283     v_header			 varchar2,
2284 	v_action_flag		 varchar2,
2285 	v_intf_type			 varchar2) IS
2286 	Select
2287 		NVL(TERR_ID,JTF_TERR_s.nextval) TERR_ID,
2288         LAST_UPDATE_DATE,
2289         LAST_UPDATED_BY,
2290         CREATION_DATE,
2291         CREATED_BY,
2292         LAST_UPDATE_LOGIN,
2293 		'JTF' APPLICATION_SHORT_NAME,
2294         TERR_NAME NAME,
2295         'Y' ENABLED_FLAG,
2296         NVL(TERR_START_DATE,TRUNC(SYSDATE)) START_DATE_ACTIVE,
2297         RANK,
2298         NVL(TERR_END_DATE, ADD_MONTHS(NVL(TERR_START_DATE,TRUNC(SYSDATE)), 12) )END_DATE_ACTIVE,
2299         TERR_NAME DESCRIPTION,
2300         'Y' UPDATE_FLAG,
2301         TERR_TYPE_ID TERRITORY_TYPE_ID,
2302         PARENT_TERR_ID PARENT_TERRITORY_ID,
2303         'N' TEMPLATE_FLAG,
2304         'N' ESCALATION_TERRITORY_FLAG,
2305         ATTRIBUTE_CATEGORY,
2306         ATTRIBUTE1,
2307         ATTRIBUTE2,
2308         ATTRIBUTE3,
2309         ATTRIBUTE4,
2310         ATTRIBUTE5,
2311         ATTRIBUTE6,
2312         ATTRIBUTE7,
2313         ATTRIBUTE8,
2314         ATTRIBUTE9,
2315         ATTRIBUTE10,
2316         ATTRIBUTE11,
2317         ATTRIBUTE12,
2318         ATTRIBUTE13,
2319         ATTRIBUTE14,
2320         ATTRIBUTE15,
2321         ORG_ID,
2322         NUM_WINNERS,
2323         0 NUM_QUAL,
2324 		lay_seq_num
2325 	from JTY_WEBADI_OTH_TERR_INTF
2326 	where header = v_header
2327 	  and status is null
2328 	  and USER_SEQUENCE = v_USER_SEQUENCE
2329 	  and INTERFACE_TYPE = v_intf_type
2330 	  and action_flag = v_action_flag;
2331 
2332 TYPE Terr_All_Rec_Type  IS RECORD
2333   (TERR_ID                     number_tbl_type,
2334    LAST_UPDATE_DATE            date_tbl_type,
2335    LAST_UPDATED_BY             number_tbl_type,
2336    CREATION_DATE               date_tbl_type,
2337    CREATED_BY                  number_tbl_type,
2338    LAST_UPDATE_LOGIN           number_tbl_type,
2339    APPLICATION_SHORT_NAME      varchar2_tbl_type,
2340    NAME                        var_2000_tbl_type,
2341    ENABLED_FLAG                var_1_tbl_type,
2342    START_DATE_ACTIVE           date_tbl_type,
2343    RANK                        number_tbl_type,
2344    END_DATE_ACTIVE             date_tbl_type,
2345    DESCRIPTION                 varchar2_tbl_type,
2346    UPDATE_FLAG                 var_1_tbl_type,
2347    TERRITORY_TYPE_ID           number_tbl_type,
2348    PARENT_TERRITORY_ID         number_tbl_type,
2349    TEMPLATE_FLAG               var_1_tbl_type,
2350    ESCALATION_TERRITORY_FLAG   var_1_tbl_type,
2351    ATTRIBUTE_CATEGORY          varchar2_tbl_type,
2352    ATTRIBUTE1                  varchar2_tbl_type,
2353    ATTRIBUTE2                  varchar2_tbl_type,
2354    ATTRIBUTE3                  varchar2_tbl_type,
2355    ATTRIBUTE4                  varchar2_tbl_type,
2356    ATTRIBUTE5                  varchar2_tbl_type,
2357    ATTRIBUTE6                  varchar2_tbl_type,
2358    ATTRIBUTE7                  varchar2_tbl_type,
2359    ATTRIBUTE8                  varchar2_tbl_type,
2360    ATTRIBUTE9                  varchar2_tbl_type,
2361    ATTRIBUTE10                 varchar2_tbl_type,
2362    ATTRIBUTE11                 varchar2_tbl_type,
2363    ATTRIBUTE12                 varchar2_tbl_type,
2364    ATTRIBUTE13                 varchar2_tbl_type,
2365    ATTRIBUTE14                 varchar2_tbl_type,
2366    ATTRIBUTE15                 varchar2_tbl_type,
2367    ORG_ID                      number_tbl_type ,
2368    NUM_WINNERS                 number_tbl_type,
2369    NUM_QUAL                    number_tbl_type,
2370    LAY_SEQ_NUM				   number_tbl_type
2371   );
2372 
2373   l_terr_all_rec			 Terr_All_Rec_Type;
2374   l_header varchar2(15) := 'TERR';
2375   l_intf_type	varchar2(1) := 'U';
2376 
2377 BEGIN
2378 
2379   x_return_status := FND_API.G_RET_STS_SUCCESS;
2380 
2384         l_terr_all_rec.CREATION_DATE,  l_terr_all_rec.CREATED_BY,
2381   open get_terr_all_csr( p_user_sequence, l_header,	p_action_flag, l_intf_type);
2382   fetch get_terr_all_csr bulk collect into l_terr_all_rec.TERR_ID,
2383         l_terr_all_rec.LAST_UPDATE_DATE, l_terr_all_rec.LAST_UPDATED_BY,
2385         l_terr_all_rec.LAST_UPDATE_LOGIN, l_terr_all_rec.APPLICATION_SHORT_NAME,
2386         l_terr_all_rec.NAME,  l_terr_all_rec.ENABLED_FLAG,
2387         l_terr_all_rec.START_DATE_ACTIVE, l_terr_all_rec.RANK,
2388         l_terr_all_rec.END_DATE_ACTIVE,   l_terr_all_rec.DESCRIPTION,
2389         l_terr_all_rec.UPDATE_FLAG,       l_terr_all_rec.TERRITORY_TYPE_ID,
2390         l_terr_all_rec.PARENT_TERRITORY_ID,  l_terr_all_rec.TEMPLATE_FLAG,
2391         l_terr_all_rec.ESCALATION_TERRITORY_FLAG,  l_terr_all_rec.ATTRIBUTE_CATEGORY,
2392         l_terr_all_rec.ATTRIBUTE1, l_terr_all_rec.ATTRIBUTE2, l_terr_all_rec.ATTRIBUTE3,
2393         l_terr_all_rec.ATTRIBUTE4, l_terr_all_rec.ATTRIBUTE5, l_terr_all_rec.ATTRIBUTE6,
2394         l_terr_all_rec.ATTRIBUTE7, l_terr_all_rec.ATTRIBUTE8, l_terr_all_rec.ATTRIBUTE9,
2395         l_terr_all_rec.ATTRIBUTE10,l_terr_all_rec.ATTRIBUTE11,l_terr_all_rec.ATTRIBUTE12,
2396         l_terr_all_rec.ATTRIBUTE13,l_terr_all_rec.ATTRIBUTE14,l_terr_all_rec.ATTRIBUTE15,
2397         l_terr_all_rec.ORG_ID, l_terr_all_rec.NUM_WINNERS, l_terr_all_rec.NUM_QUAL,
2398 		l_terr_all_rec.lay_seq_num;
2399   close get_terr_all_csr;
2400 
2401   if (p_action_flag = 'C' and l_terr_all_rec.NAME.count > 0) then
2402     forall i in l_terr_all_rec.NAME.first..l_terr_all_rec.NAME.last
2403       INSERT INTO JTF_TERR_ALL(
2404            TERR_ID, LAST_UPDATE_DATE, LAST_UPDATED_BY,
2405            CREATION_DATE, CREATED_BY, LAST_UPDATE_LOGIN,
2406            APPLICATION_SHORT_NAME, NAME, ENABLED_FLAG,
2407            START_DATE_ACTIVE, RANK, END_DATE_ACTIVE, DESCRIPTION,
2408 		   UPDATE_FLAG, TERRITORY_TYPE_ID, PARENT_TERRITORY_ID,
2409            TEMPLATE_FLAG, ESCALATION_TERRITORY_FLAG, ATTRIBUTE_CATEGORY,
2410            ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE3,
2411            ATTRIBUTE4, ATTRIBUTE5, ATTRIBUTE6,
2412            ATTRIBUTE7, ATTRIBUTE8, ATTRIBUTE9,
2413            ATTRIBUTE10, ATTRIBUTE11, ATTRIBUTE12,
2414            ATTRIBUTE13, ATTRIBUTE14, ATTRIBUTE15,
2415            ORG_ID, NUM_WINNERS, NUM_QUAL
2416           ) VALUES (
2417           l_terr_all_rec.TERR_ID(i),
2418 		  l_terr_all_rec.LAST_UPDATE_DATE(i), l_terr_all_rec.LAST_UPDATED_BY(i),
2419           l_terr_all_rec.CREATION_DATE(i),  l_terr_all_rec.CREATED_BY(i),
2420           l_terr_all_rec.LAST_UPDATE_LOGIN(i), l_terr_all_rec.APPLICATION_SHORT_NAME(i),
2421           l_terr_all_rec.NAME(i),  l_terr_all_rec.ENABLED_FLAG(i),
2422           l_terr_all_rec.START_DATE_ACTIVE(i), l_terr_all_rec.RANK(i),
2423           l_terr_all_rec.END_DATE_ACTIVE(i),   l_terr_all_rec.DESCRIPTION(i),
2424           l_terr_all_rec.UPDATE_FLAG(i),       l_terr_all_rec.TERRITORY_TYPE_ID(i),
2425           l_terr_all_rec.PARENT_TERRITORY_ID(i),  l_terr_all_rec.TEMPLATE_FLAG(i),
2426           l_terr_all_rec.ESCALATION_TERRITORY_FLAG(i),  l_terr_all_rec.ATTRIBUTE_CATEGORY(i),
2427           l_terr_all_rec.ATTRIBUTE1(i), l_terr_all_rec.ATTRIBUTE2(i), l_terr_all_rec.ATTRIBUTE3(i),
2428           l_terr_all_rec.ATTRIBUTE4(i), l_terr_all_rec.ATTRIBUTE5(i), l_terr_all_rec.ATTRIBUTE6(i),
2429           l_terr_all_rec.ATTRIBUTE7(i), l_terr_all_rec.ATTRIBUTE8(i), l_terr_all_rec.ATTRIBUTE9(i),
2430           l_terr_all_rec.ATTRIBUTE10(i),l_terr_all_rec.ATTRIBUTE11(i),l_terr_all_rec.ATTRIBUTE12(i),
2431           l_terr_all_rec.ATTRIBUTE13(i),l_terr_all_rec.ATTRIBUTE14(i),l_terr_all_rec.ATTRIBUTE15(i),
2432           l_terr_all_rec.ORG_ID(i), l_terr_all_rec.NUM_WINNERS(i), l_terr_all_rec.NUM_QUAL(i));
2433 
2434 	forall i in l_terr_all_rec.TERR_ID.first..l_terr_all_rec.TERR_ID.last
2435 	  update JTY_WEBADI_OTH_TERR_INTF
2436          set parent_terr_id = l_terr_all_rec.terr_id(i)
2437        where parent_terr_name = l_terr_all_rec.name(i)
2438          and USER_SEQUENCE = P_USER_SEQUENCE
2439          and interface_type = l_intf_type
2440          and parent_terr_id is null;
2441 
2442 	forall i in l_terr_all_rec.TERR_ID.first..l_terr_all_rec.TERR_ID.last
2443       update JTY_WEBADI_OTH_TERR_INTF
2444       set terr_id = l_terr_all_rec.terr_id(i)
2445       where terr_name = l_terr_all_rec.name(i)
2446         and USER_SEQUENCE = P_USER_SEQUENCE
2447         and interface_type = l_intf_type
2448         and action_flag = p_action_flag;
2449 
2450     INSERT INTO JTF_TERR_USGS_ALL(
2451            TERR_USG_ID,
2452            LAST_UPDATE_DATE,
2453            LAST_UPDATED_BY,
2454            CREATION_DATE,
2455            CREATED_BY,
2456            LAST_UPDATE_LOGIN,
2457            TERR_ID,
2458            SOURCE_ID,
2459            ORG_ID
2460           )
2461 	SELECT
2462 		JTF_TERR_USGS_s.nextval TERR_USG_ID,
2463     	LAST_UPDATE_DATE,
2464     	LAST_UPDATED_BY,
2465     	CREATION_DATE,
2466     	CREATED_BY,
2467     	LAST_UPDATE_LOGIN,
2468     	TERR_ID,
2469     	usage_id SOURCE_ID,
2470     	ORG_ID
2471 	from JTY_WEBADI_OTH_TERR_INTF
2472 	where header = l_header
2473 	  and status is null
2474 	  and USER_SEQUENCE = p_USER_SEQUENCE
2475 	  and INTERFACE_TYPE = l_intf_type
2476 	  and action_flag = p_action_flag;
2477 
2478     INSERT INTO JTF_TERR_QTYPE_USGS_ALL(
2479            TERR_QTYPE_USG_ID,
2480            LAST_UPDATED_BY,
2481            LAST_UPDATE_DATE,
2482            CREATED_BY,
2483            CREATION_DATE,
2484            LAST_UPDATE_LOGIN,
2485            TERR_ID,
2486            QUAL_TYPE_USG_ID,
2490 	    JTF_TERR_QTYPE_USGS_s.nextval TERR_QUAL_TYPE_USG_ID,
2487            ORG_ID
2488           )
2489 	  select
2491 		LAST_UPDATED_BY,
2492 		LAST_UPDATE_DATE,
2493 		CREATED_BY,
2494 		CREATION_DATE,
2495 		LAST_UPDATE_LOGIN,
2496 		TERR_ID,
2497 		QUAL_TYPE_ID QUAL_TYPE_USG_ID,
2498 		ORG_ID
2499 	  from JTY_WEBADI_QUAL_TYPE_HEADER gt,
2500 	  JTY_WEBADI_OTH_TERR_INTF jut
2501  	  where jut.header = L_header
2502 	    and jut.status is null
2503 	    and jut.USER_SEQUENCE = P_USER_SEQUENCE
2504 	    and jut.INTERFACE_TYPE = l_intf_type
2505 		and jut.action_flag = p_action_flag
2506 	    and gt.user_sequence = jut.user_sequence;
2507 
2508 	forall i in l_terr_all_rec.lay_seq_num.first..l_terr_all_rec.lay_seq_num.last
2509 		update JTY_WEBADI_OTH_TERR_INTF
2510 		set status = x_return_status
2511 		, error_msg = X_Msg_Data
2512 		where lay_seq_num = l_terr_all_rec.lay_seq_num(i)
2513 		and interface_type = l_intf_type
2514 		and header = l_header
2515 		and user_sequence = p_user_sequence;
2516 
2517   end if;
2518 
2519   x_return_status := FND_API.G_RET_STS_SUCCESS;
2520   if (p_action_flag = 'U' and l_terr_all_rec.TERR_ID.count > 0) then
2521 
2522     forall i in l_terr_all_rec.TERR_ID.first..l_terr_all_rec.TERR_ID.last
2523     Update JTF_TERR_ALL
2524     SET
2525       LAST_UPDATE_DATE = l_terr_all_rec.LAST_UPDATE_DATE(i),
2526       LAST_UPDATED_BY = l_terr_all_rec.LAST_UPDATED_BY(i),
2527       LAST_UPDATE_LOGIN = l_terr_all_rec.LAST_UPDATE_LOGIN(i),
2528       NAME = l_terr_all_rec.NAME(i),
2529       START_DATE_ACTIVE = l_terr_all_rec.START_DATE_ACTIVE(i),
2530       END_DATE_ACTIVE = l_terr_all_rec.END_DATE_ACTIVE(i),
2531       PARENT_TERRITORY_ID = l_terr_all_rec.PARENT_TERRITORY_ID(i),
2532       TERRITORY_TYPE_ID = l_terr_all_rec.TERRITORY_TYPE_ID(i),
2533       RANK = l_terr_all_rec.RANK(i),
2534       DESCRIPTION = l_terr_all_rec.DESCRIPTION(i),
2535 	  ENABLED_FLAG = l_terr_all_rec.ENABLED_FLAG(i),
2536       ATTRIBUTE_CATEGORY = l_terr_all_rec.ATTRIBUTE_CATEGORY(i),
2537       ATTRIBUTE1 = l_terr_all_rec.ATTRIBUTE1(i),
2538       ATTRIBUTE2 = l_terr_all_rec.ATTRIBUTE2(i),
2539       ATTRIBUTE3 = l_terr_all_rec.ATTRIBUTE3(i),
2540       ATTRIBUTE4 = l_terr_all_rec.ATTRIBUTE4(i),
2541       ATTRIBUTE5 = l_terr_all_rec.ATTRIBUTE5(i),
2542       ATTRIBUTE6 = l_terr_all_rec.ATTRIBUTE6(i),
2543       ATTRIBUTE7 = l_terr_all_rec.ATTRIBUTE7(i),
2544       ATTRIBUTE8 = l_terr_all_rec.ATTRIBUTE8(i),
2545       ATTRIBUTE9 = l_terr_all_rec.ATTRIBUTE9(i),
2546       ATTRIBUTE10 = l_terr_all_rec.ATTRIBUTE10(i),
2547       ATTRIBUTE11 = l_terr_all_rec.ATTRIBUTE11(i),
2548       ATTRIBUTE12 = l_terr_all_rec.ATTRIBUTE12(i),
2549       ATTRIBUTE13 = l_terr_all_rec.ATTRIBUTE13(i),
2550       ATTRIBUTE14 = l_terr_all_rec.ATTRIBUTE14(i),
2551       ATTRIBUTE15 = l_terr_all_rec.ATTRIBUTE15(i),
2552       ORG_ID      = l_terr_all_rec.ORG_ID(i),
2553       NUM_WINNERS      = l_terr_all_rec.NUM_WINNERS(i)
2554     where terr_id = l_terr_all_rec.Terr_Id(i);
2555 
2556 	forall i in l_terr_all_rec.lay_seq_num.first..l_terr_all_rec.lay_seq_num.last
2557 		update JTY_WEBADI_OTH_TERR_INTF
2558 		set status = x_return_status
2559 		, error_msg = X_Msg_Data
2560 		where lay_seq_num = l_terr_all_rec.lay_seq_num(i)
2561 		and interface_type = l_intf_type
2562 		and header = l_header
2563 		and user_sequence = p_user_sequence;
2564 
2565   end if;
2566   EXCEPTION
2567     WHEN OTHERS THEN
2568 	  X_RETURN_STATUS := FND_API.G_RET_STS_ERROR;
2569 
2570 END UPDATE_TERR;
2571 
2572 PROCEDURE UPDATE_TERR_QUAL(
2573     P_USER_SEQUENCE 		  IN  NUMBER,
2574 --	P_ACTION_FLAG			  IN VARCHAR2,
2575 	x_return_status			  OUT NOCOPY VARCHAR2,
2576 	x_msg_data				  OUT NOCOPY VARCHAR2
2577 ) IS
2578 
2579 	CURSOR get_qual_csr(
2580 	  v_user_sequence number,
2581 	  v_action_flag	  varchar2,
2582 	  v_intf_type	  varchar2,
2583 	  v_header		  varchar2) IS
2584     select JTF_TERR_QUAL_s.nextval TERR_QUAL_ID, sub.terr_id, sub.qual_value_id,
2585   	sub.qual_value1, sub.qual_value2, sub.qual_value3,
2586   	sub.org_id, sub.last_updated_by, sub.last_update_date,
2587   	sub.last_update_login, sub.creation_date, sub.created_by,
2588   	jq.qual_usg_id, jq.display_type qual_type, jq.CONVERT_TO_ID_FLAG,
2589 	jq.qualifier_num, jq.html_lov_sql1,
2590   	(case
2591   	  when jq.COMPARISON_OPERATOR = '=' then '='
2592   	  when (jq.COMPARISON_OPERATOR LIKE '%LIKE%') AND (instr(sub.qual_VALUE1,'_') > 0) or (instr(sub.qual_VALUE1,'%') > 0 and sub.qual_VALUE2 is null) then 'LIKE'
2593   	  when (jq.COMPARISON_OPERATOR LIKE '%BETWEEN%') AND (sub.qual_VALUE1 is not null and sub.qual_VALUE2 is not null) then 'BETWEEN'
2594   	  else '='
2595   	end) qual_cond
2596       from JTY_WEBADI_QUAL_HEADER jq,
2597       (
2598       select terr_id, user_sequence,
2599       1 qual_num, QUAL1_VALUE_ID qual_value_id,
2600       QUAL1_VALUE1 qual_VALUE1,
2601       QUAL1_VALUE2 qual_VALUE2,
2602       QUAL1_VALUE3 qual_VALUE3, ORG_ID,
2603       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2604       CREATED_BY, CREATION_DATE
2605       FROM JTY_WEBADI_OTH_TERR_INTF jut
2606       where jut.USER_SEQUENCE = v_user_sequence
2607   	  and jut.status is null
2608 	  and jut.terr_qual_id1 is null
2609 	  and jut.qual1_value1 is not null
2610       and jut.INTERFACE_TYPE = v_intf_type
2611 	  AND jut.ACTION_FLAG = v_action_flag
2612   	  and jut.header = v_header
2613       union all
2614       select terr_id, user_sequence,
2618       QUAL2_VALUE3 qual_VALUE3, ORG_ID,
2615       2 qual_num, QUAL2_VALUE_ID qual_value_id,
2616       QUAL2_VALUE1 qual_VALUE1,
2617       QUAL2_VALUE2 qual_VALUE2,
2619       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2620       CREATED_BY, CREATION_DATE
2621       FROM JTY_WEBADI_OTH_TERR_INTF jut
2622       where jut.USER_SEQUENCE = v_user_sequence
2623   	  and jut.status is null
2624 	  and jut.terr_qual_id2 is null
2625 	  and jut.qual2_value1 is not null
2626   	  and jut.INTERFACE_TYPE = v_intf_type
2627 	  AND jut.ACTION_FLAG = v_action_flag
2628   	  and jut.header = v_header
2629       union all
2630       select terr_id, user_sequence,
2631       3 qual_num, QUAL3_VALUE_ID qual_value_id,
2632       QUAL3_VALUE1 qual_VALUE1,
2633       QUAL3_VALUE2 qual_VALUE2,
2634       QUAL3_VALUE3 qual_VALUE3, ORG_ID,
2635       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2636       CREATED_BY, CREATION_DATE
2637       FROM JTY_WEBADI_OTH_TERR_INTF jut
2638       where jut.USER_SEQUENCE = v_user_sequence
2639   	  and jut.status is null
2640 	  and jut.terr_qual_id3 is null
2641 	  and jut.qual3_value1 is not null
2642   	  and jut.INTERFACE_TYPE = v_intf_type
2643 	  AND jut.ACTION_FLAG = v_action_flag
2644   	  and jut.header = v_header
2645       union all
2646       select terr_id, user_sequence,
2647       4 qual_num, QUAL4_VALUE_ID qual_value_id,
2648       QUAL4_VALUE1 qual_VALUE1,
2649       QUAL4_VALUE2 qual_VALUE2,
2650       QUAL4_VALUE3 qual_VALUE3, ORG_ID,
2651       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2652       CREATED_BY, CREATION_DATE
2653       FROM JTY_WEBADI_OTH_TERR_INTF jut
2654       where jut.USER_SEQUENCE = v_user_sequence
2655   	  and jut.status is null
2656 	  and jut.terr_qual_id4 is null
2657 	  and jut.qual4_value1 is not null
2658   	  and jut.INTERFACE_TYPE = v_intf_type
2659 	  AND jut.ACTION_FLAG = v_action_flag
2660   	  and jut.header = v_header
2661       union all
2662       select terr_id, user_sequence,
2663       5 qual_num, QUAL5_VALUE_ID qual_value_id,
2664       QUAL5_VALUE1 qual_VALUE1,
2665       QUAL5_VALUE2 qual_VALUE2,
2666       QUAL5_VALUE3 qual_VALUE3, ORG_ID,
2667       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2668       CREATED_BY, CREATION_DATE
2669       FROM JTY_WEBADI_OTH_TERR_INTF jut
2670       where jut.USER_SEQUENCE = v_user_sequence
2671   	  and jut.status is null
2672 	  and jut.terr_qual_id5 is null
2673 	  and jut.qual5_value1 is not null
2674   	  and jut.INTERFACE_TYPE = v_intf_type
2675 	  AND jut.ACTION_FLAG = v_action_flag
2676   	  and jut.header = v_header
2677       union all
2678       select terr_id, user_sequence,
2679       6 qual_num, QUAL6_VALUE_ID qual_value_id,
2680       QUAL6_VALUE1 qual_VALUE1,
2681       QUAL6_VALUE2 qual_VALUE2,
2682       QUAL6_VALUE3 qual_VALUE3, ORG_ID,
2683       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2684       CREATED_BY, CREATION_DATE
2685       FROM JTY_WEBADI_OTH_TERR_INTF jut
2686       where jut.USER_SEQUENCE = v_user_sequence
2687   	  and jut.status is null
2688 	  and jut.terr_qual_id6 is null
2689 	  and jut.qual6_value1 is not null
2690   	  and jut.INTERFACE_TYPE = v_intf_type
2691 	  AND jut.ACTION_FLAG = v_action_flag
2692   	  and jut.header = v_header
2693       union all
2694       select terr_id, user_sequence,
2695       7 qual_num, QUAL7_VALUE_ID qual_value_id,
2696       QUAL7_VALUE1 qual_VALUE1,
2697       QUAL7_VALUE2 qual_VALUE2,
2698       QUAL7_VALUE3 qual_VALUE3, ORG_ID,
2699       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2700       CREATED_BY, CREATION_DATE
2701       FROM JTY_WEBADI_OTH_TERR_INTF jut
2702       where jut.USER_SEQUENCE = v_user_sequence
2703   	  and jut.status is null
2704 	  and jut.terr_qual_id7 is null
2705 	  and jut.qual7_value1 is not null
2706   	  and jut.INTERFACE_TYPE = v_intf_type
2707 	  AND jut.ACTION_FLAG = v_action_flag
2708   	  and jut.header = v_header
2709       union all
2710       select terr_id, user_sequence,
2711       8 qual_num, QUAL8_VALUE_ID qual_value_id,
2712       QUAL8_VALUE1 qual_VALUE1,
2713       QUAL8_VALUE2 qual_VALUE2,
2714       QUAL8_VALUE3 qual_VALUE3, ORG_ID,
2715       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2716       CREATED_BY, CREATION_DATE
2717       FROM JTY_WEBADI_OTH_TERR_INTF jut
2718       where jut.USER_SEQUENCE = v_user_sequence
2719   	  and jut.status is null
2720 	  and jut.terr_qual_id8 is null
2721 	  and jut.qual8_value1 is not null
2722   	  and jut.INTERFACE_TYPE = v_intf_type
2723 	  AND jut.ACTION_FLAG = v_action_flag
2724   	  and jut.header = v_header
2725       union all
2726       select terr_id, user_sequence,
2727       9 qual_num, QUAL9_VALUE_ID qual_value_id,
2728       QUAL9_VALUE1 qual_VALUE1,
2729       QUAL9_VALUE2 qual_VALUE2,
2730       QUAL9_VALUE3 qual_VALUE3, ORG_ID,
2731       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2732       CREATED_BY, CREATION_DATE
2733       FROM JTY_WEBADI_OTH_TERR_INTF jut
2734       where jut.USER_SEQUENCE = v_user_sequence
2735   	  and jut.status is null
2736 	  and jut.terr_qual_id9 is null
2737 	  and jut.qual9_value1 is not null
2738   	  and jut.INTERFACE_TYPE = v_intf_type
2739 	  AND jut.ACTION_FLAG = v_action_flag
2740   	  and jut.header = v_header
2741       union all
2742       select terr_id, user_sequence,
2743       10 qual_num, QUAL10_VALUE_ID qual_value_id,
2747       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2744       QUAL10_VALUE1 qual_VALUE1,
2745       QUAL10_VALUE2 qual_VALUE2,
2746       QUAL10_VALUE3 qual_VALUE3, ORG_ID,
2748       CREATED_BY, CREATION_DATE
2749       FROM JTY_WEBADI_OTH_TERR_INTF jut
2750       where jut.USER_SEQUENCE = v_user_sequence
2751   	  and jut.status is null
2752 	  and jut.terr_qual_id10 is null
2753 	  and jut.qual10_value1 is not null
2754   	  and jut.INTERFACE_TYPE = v_intf_type
2755 	  AND jut.ACTION_FLAG = v_action_flag
2756   	  and jut.header = v_header
2757       union all
2758       select terr_id, user_sequence,
2759       11 qual_num, QUAL11_VALUE_ID qual_value_id,
2760       QUAL11_VALUE1 qual_VALUE1,
2761       QUAL11_VALUE2 qual_VALUE2,
2762       QUAL11_VALUE3 qual_VALUE3, ORG_ID,
2763       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2764       CREATED_BY, CREATION_DATE
2765       FROM JTY_WEBADI_OTH_TERR_INTF jut
2766       where jut.USER_SEQUENCE = v_user_sequence
2767   	  and jut.status is null
2768 	  and jut.terr_qual_id11 is null
2769 	  and jut.qual11_value1 is not null
2770   	  and jut.INTERFACE_TYPE = v_intf_type
2771 	  AND jut.ACTION_FLAG = v_action_flag
2772   	  and jut.header = v_header
2773       union all
2774       select terr_id, user_sequence,
2775       12 qual_num, QUAL12_VALUE_ID qual_value_id,
2776       QUAL12_VALUE1 qual_VALUE1,
2777       QUAL12_VALUE2 qual_VALUE2,
2778       QUAL12_VALUE3 qual_VALUE3, ORG_ID,
2779       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2780       CREATED_BY, CREATION_DATE
2781       FROM JTY_WEBADI_OTH_TERR_INTF jut
2782       where jut.USER_SEQUENCE = v_user_sequence
2783   	  and jut.status is null
2784 	  and jut.terr_qual_id12 is null
2785 	  and jut.qual12_value1 is not null
2786   	  and jut.INTERFACE_TYPE = v_intf_type
2787 	  AND jut.ACTION_FLAG = v_action_flag
2788   	  and jut.header = v_header
2789       union all
2790       select terr_id, user_sequence,
2791       13 qual_num, QUAL13_VALUE_ID qual_value_id,
2792       QUAL13_VALUE1 qual_VALUE1,
2793       QUAL13_VALUE2 qual_VALUE2,
2794       QUAL13_VALUE3 qual_VALUE3, ORG_ID,
2795       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2796       CREATED_BY, CREATION_DATE
2797       FROM JTY_WEBADI_OTH_TERR_INTF jut
2798       where jut.USER_SEQUENCE = v_user_sequence
2799   	  and jut.status is null
2800 	  and jut.terr_qual_id13 is null
2801 	  and jut.qual13_value1 is not null
2802   	  and jut.INTERFACE_TYPE = v_intf_type
2803 	  AND jut.ACTION_FLAG = v_action_flag
2804   	  and jut.header = v_header
2805       union all
2806       select terr_id, user_sequence,
2807       14 qual_num, QUAL14_VALUE_ID qual_value_id,
2808       QUAL14_VALUE1 qual_VALUE1,
2809       QUAL14_VALUE2 qual_VALUE2,
2810       QUAL14_VALUE3 qual_VALUE3, ORG_ID,
2811       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2812       CREATED_BY, CREATION_DATE
2813       FROM JTY_WEBADI_OTH_TERR_INTF jut
2814       where jut.USER_SEQUENCE = v_user_sequence
2815   	  and jut.status is null
2816 	  and jut.terr_qual_id14 is null
2817 	  and jut.qual14_value1 is not null
2818   	  and jut.INTERFACE_TYPE = v_intf_type
2819 	  AND jut.ACTION_FLAG = v_action_flag
2820   	  and jut.header = v_header
2821       union all
2822       select terr_id, user_sequence,
2823       15 qual_num, QUAL15_VALUE_ID qual_value_id,
2824       QUAL15_VALUE1 qual_VALUE1,
2825       QUAL15_VALUE2 qual_VALUE2,
2826       QUAL15_VALUE3 qual_VALUE3, ORG_ID,
2827       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2828       CREATED_BY, CREATION_DATE
2829       FROM JTY_WEBADI_OTH_TERR_INTF jut
2830       where jut.USER_SEQUENCE = v_user_sequence
2831   	  and jut.status is null
2832 	  and jut.terr_qual_id15 is null
2833 	  and jut.qual15_value1 is not null
2834   	  and jut.INTERFACE_TYPE = v_intf_type
2835 	  AND jut.ACTION_FLAG = v_action_flag
2836   	  and jut.header = v_header
2837       union all
2838       select terr_id, user_sequence,
2839       16 qual_num, QUAL16_VALUE_ID qual_value_id,
2840       QUAL16_VALUE1 qual_VALUE1,
2841       QUAL16_VALUE2 qual_VALUE2,
2842       QUAL16_VALUE3 qual_VALUE3, ORG_ID,
2843       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2844       CREATED_BY, CREATION_DATE
2845       FROM JTY_WEBADI_OTH_TERR_INTF jut
2846       where jut.USER_SEQUENCE = v_user_sequence
2847   	  and jut.status is null
2848 	  and jut.terr_qual_id16 is null
2849 	  and jut.qual16_value1 is not null
2850   	  and jut.INTERFACE_TYPE = v_intf_type
2851 	  AND jut.ACTION_FLAG = v_action_flag
2852   	  and jut.header = v_header
2853       union all
2854       select terr_id, user_sequence,
2855       17 qual_num, QUAL17_VALUE_ID qual_value_id,
2856       QUAL17_VALUE1 qual_VALUE1,
2857       QUAL17_VALUE2 qual_VALUE2,
2858       QUAL17_VALUE3 qual_VALUE3, ORG_ID,
2859       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2860       CREATED_BY, CREATION_DATE
2861       FROM JTY_WEBADI_OTH_TERR_INTF jut
2862       where jut.USER_SEQUENCE = v_user_sequence
2863   	  and jut.status is null
2864 	  and jut.terr_qual_id17 is null
2865 	  and jut.qual17_value1 is not null
2866   	  and jut.INTERFACE_TYPE = v_intf_type
2867 	  AND jut.ACTION_FLAG = v_action_flag
2868   	  and jut.header = v_header
2869       union all
2870       select terr_id, user_sequence,
2871       18 qual_num, QUAL18_VALUE_ID qual_value_id,
2872       QUAL18_VALUE1 qual_VALUE1,
2876       CREATED_BY, CREATION_DATE
2873       QUAL18_VALUE2 qual_VALUE2,
2874       QUAL18_VALUE3 qual_VALUE3, ORG_ID,
2875       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2877       FROM JTY_WEBADI_OTH_TERR_INTF jut
2878       where jut.USER_SEQUENCE = v_user_sequence
2879   	  and jut.status is null
2880 	  and jut.terr_qual_id18 is null
2881 	  and jut.qual18_value1 is not null
2882   	  and jut.INTERFACE_TYPE = v_intf_type
2883 	  AND jut.ACTION_FLAG = v_action_flag
2884   	  and jut.header = v_header
2885       union all
2886       select terr_id, user_sequence,
2887       19 qual_num, QUAL19_VALUE_ID qual_value_id,
2888       QUAL19_VALUE1 qual_VALUE1,
2889       QUAL19_VALUE2 qual_VALUE2,
2890       QUAL19_VALUE3 qual_VALUE3, ORG_ID,
2891       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2892       CREATED_BY, CREATION_DATE
2893       FROM JTY_WEBADI_OTH_TERR_INTF jut
2894       where jut.USER_SEQUENCE = v_user_sequence
2895   	  and jut.status is null
2896 	  and jut.terr_qual_id19 is null
2897 	  and jut.qual19_value1 is not null
2898   	  and jut.INTERFACE_TYPE = v_intf_type
2899 	  AND jut.ACTION_FLAG = v_action_flag
2900   	  and jut.header = v_header
2901       union all
2902       select terr_id, user_sequence,
2903       20 qual_num, QUAL20_VALUE_ID qual_value_id,
2904       QUAL20_VALUE1 qual_VALUE1,
2905       QUAL20_VALUE2 qual_VALUE2,
2906       QUAL20_VALUE3 qual_VALUE3, ORG_ID,
2907       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2908       CREATED_BY, CREATION_DATE
2909       FROM JTY_WEBADI_OTH_TERR_INTF jut
2910       where jut.USER_SEQUENCE = v_user_sequence
2911   	  and jut.status is null
2912 	  and jut.terr_qual_id20 is null
2913 	  and jut.qual20_value1 is not null
2914   	  and jut.INTERFACE_TYPE = v_intf_type
2915 	  AND jut.ACTION_FLAG = v_action_flag
2916   	  and jut.header = v_header
2917       union all
2918       select terr_id, user_sequence,
2919       21 qual_num, QUAL21_VALUE_ID qual_value_id,
2920       QUAL21_VALUE1 qual_VALUE1,
2921       QUAL21_VALUE2 qual_VALUE2,
2922       QUAL21_VALUE3 qual_VALUE3, ORG_ID,
2923       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2924       CREATED_BY, CREATION_DATE
2925       FROM JTY_WEBADI_OTH_TERR_INTF jut
2926       where jut.USER_SEQUENCE = v_user_sequence
2927   	  and jut.status is null
2928 	  and jut.terr_qual_id21 is null
2929 	  and jut.qual21_value1 is not null
2930   	  and jut.INTERFACE_TYPE = v_intf_type
2931 	  AND jut.ACTION_FLAG = v_action_flag
2932   	  and jut.header = v_header
2933       union all
2934       select terr_id, user_sequence,
2935       22 qual_num, QUAL22_VALUE_ID qual_value_id,
2936       QUAL22_VALUE1 qual_VALUE1,
2937       QUAL22_VALUE2 qual_VALUE2,
2938       QUAL22_VALUE3 qual_VALUE3, ORG_ID,
2939       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2940       CREATED_BY, CREATION_DATE
2941       FROM JTY_WEBADI_OTH_TERR_INTF jut
2942       where jut.USER_SEQUENCE = v_user_sequence
2943   	  and jut.status is null
2944 	  and jut.terr_qual_id22 is null
2945 	  and jut.qual22_value1 is not null
2946   	  and jut.INTERFACE_TYPE = v_intf_type
2947 	  AND jut.ACTION_FLAG = v_action_flag
2948   	  and jut.header = v_header
2949       union all
2950       select terr_id, user_sequence,
2951       23 qual_num, QUAL23_VALUE_ID qual_value_id,
2952       QUAL23_VALUE1 qual_VALUE1,
2953       QUAL23_VALUE2 qual_VALUE2,
2954       QUAL23_VALUE3 qual_VALUE3, ORG_ID,
2955       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2956       CREATED_BY, CREATION_DATE
2957       FROM JTY_WEBADI_OTH_TERR_INTF jut
2958       where jut.USER_SEQUENCE = v_user_sequence
2959   	  and jut.status is null
2960 	  and jut.terr_qual_id23 is null
2961 	  and jut.qual23_value1 is not null
2962   	  and jut.INTERFACE_TYPE = v_intf_type
2963 	  AND jut.ACTION_FLAG = v_action_flag
2964   	  and jut.header = v_header
2965       union all
2966       select terr_id, user_sequence,
2967       24 qual_num, QUAL24_VALUE_ID qual_value_id,
2968       QUAL24_VALUE1 qual_VALUE1,
2969       QUAL24_VALUE2 qual_VALUE2,
2970       QUAL24_VALUE3 qual_VALUE3, ORG_ID,
2971       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2972       CREATED_BY, CREATION_DATE
2973       FROM JTY_WEBADI_OTH_TERR_INTF jut
2974       where jut.USER_SEQUENCE = v_user_sequence
2975   	  and jut.status is null
2976 	  and jut.terr_qual_id24 is null
2977 	  and jut.qual24_value1 is not null
2978   	  and jut.INTERFACE_TYPE = v_intf_type
2979 	  AND jut.ACTION_FLAG = v_action_flag
2980   	  and jut.header = v_header
2981       union all
2982       select terr_id, user_sequence,
2983       25 qual_num, QUAL25_VALUE_ID qual_value_id,
2984       QUAL25_VALUE1 qual_valUE1,
2985       QUAL25_VALUE2 qual_valUE2,
2986       QUAL25_VALUE3 qual_valUE3, ORG_ID,
2987       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
2988       CREATED_BY, CREATION_DATE
2989       FROM JTY_WEBADI_OTH_TERR_INTF jut
2990       where jut.USER_SEQUENCE = v_user_sequence
2991   	  and jut.status is null
2992 	  and jut.terr_qual_id25 is null
2993 	  and jut.qual25_value1 is not null
2994   	  and jut.INTERFACE_TYPE = v_intf_type
2995 	  AND jut.ACTION_FLAG = v_action_flag
2996   	  and jut.header = v_header) sub
2997       where jq.user_sequence = sub.user_sequence
2998         and jq.qualifier_num = sub.qual_num;
2999 
3000 	CURSOR get_c_terr_value_csr(
3001 	  v_user_sequence number,
3005   	sub.qual_value1, sub.qual_value2, sub.qual_value3,
3002 	  v_intf_type	  varchar2,
3003 	  v_header		  varchar2) IS
3004     select sub.TERR_QUAL_ID, sub.terr_id, sub.qual_value_id,
3006   	sub.org_id, sub.last_updated_by, sub.last_update_date,
3007   	sub.last_update_login, sub.creation_date, sub.created_by,
3008   	jq.qual_usg_id, jq.display_type qual_type, jq.CONVERT_TO_ID_FLAG,
3009 	jq.qualifier_num, jq.html_lov_sql1,
3010   	(case
3011   	  when jq.COMPARISON_OPERATOR = '=' then '='
3012   	  when (jq.COMPARISON_OPERATOR LIKE '%LIKE%') AND (instr(sub.qual_VALUE1,'_') > 0) or (instr(sub.qual_VALUE1,'%') > 0 and sub.qual_VALUE2 is null) then 'LIKE'
3013   	  when (jq.COMPARISON_OPERATOR LIKE '%BETWEEN%') AND (sub.qual_VALUE1 is not null and sub.qual_VALUE2 is not null) then 'BETWEEN'
3014   	  else '='
3015   	end) qual_cond
3016       from JTY_WEBADI_QUAL_HEADER jq,
3017       (
3018       select terr_id, user_sequence,
3019 	  terr_qual_id1 terr_qual_id,
3020       1 qual_num, QUAL1_VALUE_ID qual_value_id,
3021       QUAL1_VALUE1 qual_VALUE1,
3022       QUAL1_VALUE2 qual_VALUE2,
3023       QUAL1_VALUE3 qual_VALUE3, ORG_ID,
3024       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3025       CREATED_BY, CREATION_DATE
3026       FROM JTY_WEBADI_OTH_TERR_INTF jut
3027       where jut.USER_SEQUENCE = v_user_sequence
3028   	  and jut.status is null
3029 	  and jut.terr_qual_id1 is not null
3030 	  and jut.QUAL1_VALUE_ID is null
3031 	  and jut.qual1_value1 is not null
3032       and jut.INTERFACE_TYPE = v_intf_type
3033   	  and jut.header = v_header
3034       union all
3035       select terr_id, user_sequence,
3036 	  terr_qual_id2 terr_qual_id,
3037       2 qual_num, QUAL2_VALUE_ID qual_value_id,
3038       QUAL2_VALUE1 qual_VALUE1,
3039       QUAL2_VALUE2 qual_VALUE2,
3040       QUAL2_VALUE3 qual_VALUE3, ORG_ID,
3041       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3042       CREATED_BY, CREATION_DATE
3043       FROM JTY_WEBADI_OTH_TERR_INTF jut
3044       where jut.USER_SEQUENCE = v_user_sequence
3045   	  and jut.status is null
3046 	  and jut.terr_qual_id2 is not null
3047 	  and jut.QUAL2_VALUE_ID is null
3048 	  and jut.qual2_value1 is not null
3049   	  and jut.INTERFACE_TYPE = v_intf_type
3050   	  and jut.header = v_header
3051       union all
3052       select terr_id, user_sequence,
3053 	  terr_qual_id3 terr_qual_id,
3054       3 qual_num, QUAL3_VALUE_ID qual_value_id,
3055       QUAL3_VALUE1 qual_VALUE1,
3056       QUAL3_VALUE2 qual_VALUE2,
3057       QUAL3_VALUE3 qual_VALUE3, ORG_ID,
3058       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3059       CREATED_BY, CREATION_DATE
3060       FROM JTY_WEBADI_OTH_TERR_INTF jut
3061       where jut.USER_SEQUENCE = v_user_sequence
3062   	  and jut.status is null
3063 	  and jut.terr_qual_id3 is not null
3064 	  and jut.QUAL3_VALUE_ID is null
3065 	  and jut.qual3_value1 is not null
3066   	  and jut.INTERFACE_TYPE = v_intf_type
3067   	  and jut.header = v_header
3068       union all
3069       select terr_id, user_sequence,
3070 	  terr_qual_id4 terr_qual_id,
3071       4 qual_num, QUAL4_VALUE_ID qual_value_id,
3072       QUAL4_VALUE1 qual_VALUE1,
3073       QUAL4_VALUE2 qual_VALUE2,
3074       QUAL4_VALUE3 qual_VALUE3, ORG_ID,
3075       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3076       CREATED_BY, CREATION_DATE
3077       FROM JTY_WEBADI_OTH_TERR_INTF jut
3078       where jut.USER_SEQUENCE = v_user_sequence
3079   	  and jut.status is null
3080 	  and jut.terr_qual_id4 is not null
3081 	  and jut.QUAL4_VALUE_ID is null
3082 	  and jut.qual4_value1 is not null
3083   	  and jut.INTERFACE_TYPE = v_intf_type
3084   	  and jut.header = v_header
3085       union all
3086       select terr_id, user_sequence,
3087 	  terr_qual_id5 terr_qual_id,
3088       5 qual_num, QUAL5_VALUE_ID qual_value_id,
3089       QUAL5_VALUE1 qual_VALUE1,
3090       QUAL5_VALUE2 qual_VALUE2,
3091       QUAL5_VALUE3 qual_VALUE3, ORG_ID,
3092       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3093       CREATED_BY, CREATION_DATE
3094       FROM JTY_WEBADI_OTH_TERR_INTF jut
3095       where jut.USER_SEQUENCE = v_user_sequence
3096   	  and jut.status is null
3097 	  and jut.terr_qual_id5 is not null
3098 	  and jut.QUAL5_VALUE_ID is null
3099 	  and jut.qual5_value1 is not null
3100   	  and jut.INTERFACE_TYPE = v_intf_type
3101   	  and jut.header = v_header
3102       union all
3103       select terr_id, user_sequence,
3104 	  terr_qual_id6 terr_qual_id,
3105       6 qual_num, QUAL6_VALUE_ID qual_value_id,
3106       QUAL6_VALUE1 qual_VALUE1,
3107       QUAL6_VALUE2 qual_VALUE2,
3108       QUAL6_VALUE3 qual_VALUE3, ORG_ID,
3109       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3110       CREATED_BY, CREATION_DATE
3111       FROM JTY_WEBADI_OTH_TERR_INTF jut
3112       where jut.USER_SEQUENCE = v_user_sequence
3113   	  and jut.status is null
3114 	  and jut.terr_qual_id6 is not null
3115 	  and jut.QUAL6_VALUE_ID is null
3116 	  and jut.qual6_value1 is not null
3117   	  and jut.INTERFACE_TYPE = v_intf_type
3118   	  and jut.header = v_header
3119       union all
3120       select terr_id, user_sequence,
3121 	  terr_qual_id7 terr_qual_id,
3122       7 qual_num, QUAL7_VALUE_ID qual_value_id,
3123       QUAL7_VALUE1 qual_VALUE1,
3124       QUAL7_VALUE2 qual_VALUE2,
3125       QUAL7_VALUE3 qual_VALUE3, ORG_ID,
3126       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3127       CREATED_BY, CREATION_DATE
3131 	  and jut.terr_qual_id7 is not null
3128       FROM JTY_WEBADI_OTH_TERR_INTF jut
3129       where jut.USER_SEQUENCE = v_user_sequence
3130   	  and jut.status is null
3132 	  and jut.QUAL7_VALUE_ID is null
3133 	  and jut.qual7_value1 is not null
3134   	  and jut.INTERFACE_TYPE = v_intf_type
3135   	  and jut.header = v_header
3136       union all
3137       select terr_id, user_sequence,
3138 	  terr_qual_id8 terr_qual_id,
3139       8 qual_num, QUAL8_VALUE_ID qual_value_id,
3140       QUAL8_VALUE1 qual_VALUE1,
3141       QUAL8_VALUE2 qual_VALUE2,
3142       QUAL8_VALUE3 qual_VALUE3, ORG_ID,
3143       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3144       CREATED_BY, CREATION_DATE
3145       FROM JTY_WEBADI_OTH_TERR_INTF jut
3146       where jut.USER_SEQUENCE = v_user_sequence
3147   	  and jut.status is null
3148 	  and jut.terr_qual_id8 is not null
3149 	  and jut.QUAL8_VALUE_ID is null
3150 	  and jut.qual8_value1 is not null
3151   	  and jut.INTERFACE_TYPE = v_intf_type
3152   	  and jut.header = v_header
3153       union all
3154       select terr_id, user_sequence,
3155 	  terr_qual_id9 terr_qual_id,
3156       9 qual_num, QUAL9_VALUE_ID qual_value_id,
3157       QUAL9_VALUE1 qual_VALUE1,
3158       QUAL9_VALUE2 qual_VALUE2,
3159       QUAL9_VALUE3 qual_VALUE3, ORG_ID,
3160       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3161       CREATED_BY, CREATION_DATE
3162       FROM JTY_WEBADI_OTH_TERR_INTF jut
3163       where jut.USER_SEQUENCE = v_user_sequence
3164   	  and jut.status is null
3165 	  and jut.terr_qual_id9 is not null
3166 	  and jut.QUAL9_VALUE_ID is null
3167 	  and jut.qual9_value1 is not null
3168   	  and jut.INTERFACE_TYPE = v_intf_type
3169   	  and jut.header = v_header
3170       union all
3171       select terr_id, user_sequence,
3172 	  terr_qual_id10 terr_qual_id,
3173       10 qual_num, QUAL10_VALUE_ID qual_value_id,
3174       QUAL10_VALUE1 qual_VALUE1,
3175       QUAL10_VALUE2 qual_VALUE2,
3176       QUAL10_VALUE3 qual_VALUE3, ORG_ID,
3177       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3178       CREATED_BY, CREATION_DATE
3179       FROM JTY_WEBADI_OTH_TERR_INTF jut
3180       where jut.USER_SEQUENCE = v_user_sequence
3181   	  and jut.status is null
3182 	  and jut.terr_qual_id10 is not null
3183 	  and jut.QUAL10_VALUE_ID is null
3184 	  and jut.qual10_value1 is not null
3185   	  and jut.INTERFACE_TYPE = v_intf_type
3186   	  and jut.header = v_header
3187       union all
3188       select terr_id, user_sequence,
3189 	  terr_qual_id11 terr_qual_id,
3190       11 qual_num, QUAL11_VALUE_ID qual_value_id,
3191       QUAL11_VALUE1 qual_VALUE1,
3192       QUAL11_VALUE2 qual_VALUE2,
3193       QUAL11_VALUE3 qual_VALUE3, ORG_ID,
3194       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3195       CREATED_BY, CREATION_DATE
3196       FROM JTY_WEBADI_OTH_TERR_INTF jut
3197       where jut.USER_SEQUENCE = v_user_sequence
3198   	  and jut.status is null
3199 	  and jut.terr_qual_id11 is not null
3200 	  and jut.QUAL11_VALUE_ID is null
3201 	  and jut.qual11_value1 is not null
3202   	  and jut.INTERFACE_TYPE = v_intf_type
3203   	  and jut.header = v_header
3204       union all
3205       select terr_id, user_sequence,
3206 	  terr_qual_id12 terr_qual_id,
3207       12 qual_num, QUAL12_VALUE_ID qual_value_id,
3208       QUAL12_VALUE1 qual_VALUE1,
3209       QUAL12_VALUE2 qual_VALUE2,
3210       QUAL12_VALUE3 qual_VALUE3, ORG_ID,
3211       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3212       CREATED_BY, CREATION_DATE
3213       FROM JTY_WEBADI_OTH_TERR_INTF jut
3214       where jut.USER_SEQUENCE = v_user_sequence
3215   	  and jut.status is null
3216 	  and jut.terr_qual_id12 is not null
3217 	  and jut.QUAL12_VALUE_ID is null
3218 	  and jut.qual12_value1 is not null
3219   	  and jut.INTERFACE_TYPE = v_intf_type
3220   	  and jut.header = v_header
3221       union all
3222       select terr_id, user_sequence,
3223 	  terr_qual_id13 terr_qual_id,
3224       13 qual_num, QUAL13_VALUE_ID qual_value_id,
3225       QUAL13_VALUE1 qual_VALUE1,
3226       QUAL13_VALUE2 qual_VALUE2,
3227       QUAL13_VALUE3 qual_VALUE3, ORG_ID,
3228       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3229       CREATED_BY, CREATION_DATE
3230       FROM JTY_WEBADI_OTH_TERR_INTF jut
3231       where jut.USER_SEQUENCE = v_user_sequence
3232   	  and jut.status is null
3233 	  and jut.terr_qual_id13 is not null
3234 	  and jut.QUAL13_VALUE_ID is null
3235 	  and jut.qual13_value1 is not null
3236   	  and jut.INTERFACE_TYPE = v_intf_type
3237   	  and jut.header = v_header
3238       union all
3239       select terr_id, user_sequence,
3240 	  terr_qual_id14 terr_qual_id,
3241       14 qual_num, QUAL14_VALUE_ID qual_value_id,
3242       QUAL14_VALUE1 qual_VALUE1,
3243       QUAL14_VALUE2 qual_VALUE2,
3244       QUAL14_VALUE3 qual_VALUE3, ORG_ID,
3245       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3246       CREATED_BY, CREATION_DATE
3247       FROM JTY_WEBADI_OTH_TERR_INTF jut
3248       where jut.USER_SEQUENCE = v_user_sequence
3249   	  and jut.status is null
3250 	  and jut.terr_qual_id14 is not null
3251 	  and jut.QUAL14_VALUE_ID is null
3252 	  and jut.qual14_value1 is not null
3253   	  and jut.INTERFACE_TYPE = v_intf_type
3254   	  and jut.header = v_header
3255       union all
3256       select terr_id, user_sequence,
3257 	  terr_qual_id15 terr_qual_id,
3258       15 qual_num, QUAL15_VALUE_ID qual_value_id,
3262       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3259       QUAL15_VALUE1 qual_VALUE1,
3260       QUAL15_VALUE2 qual_VALUE2,
3261       QUAL15_VALUE3 qual_VALUE3, ORG_ID,
3263       CREATED_BY, CREATION_DATE
3264       FROM JTY_WEBADI_OTH_TERR_INTF jut
3265       where jut.USER_SEQUENCE = v_user_sequence
3266   	  and jut.status is null
3267 	  and jut.terr_qual_id15 is not null
3268 	  and jut.QUAL15_VALUE_ID is null
3269 	  and jut.qual15_value1 is not null
3270   	  and jut.INTERFACE_TYPE = v_intf_type
3271   	  and jut.header = v_header
3272       union all
3273       select terr_id, user_sequence,
3274 	  terr_qual_id16 terr_qual_id,
3275       16 qual_num, QUAL16_VALUE_ID qual_value_id,
3276       QUAL16_VALUE1 qual_VALUE1,
3277       QUAL16_VALUE2 qual_VALUE2,
3278       QUAL16_VALUE3 qual_VALUE3, ORG_ID,
3279       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3280       CREATED_BY, CREATION_DATE
3281       FROM JTY_WEBADI_OTH_TERR_INTF jut
3282       where jut.USER_SEQUENCE = v_user_sequence
3283   	  and jut.status is null
3284 	  and jut.terr_qual_id16 is not null
3285 	  and jut.QUAL16_VALUE_ID is null
3286 	  and jut.qual16_value1 is not null
3287   	  and jut.INTERFACE_TYPE = v_intf_type
3288   	  and jut.header = v_header
3289       union all
3290       select terr_id, user_sequence,
3291 	  terr_qual_id17 terr_qual_id,
3292       17 qual_num, QUAL17_VALUE_ID qual_value_id,
3293       QUAL17_VALUE1 qual_VALUE1,
3294       QUAL17_VALUE2 qual_VALUE2,
3295       QUAL17_VALUE3 qual_VALUE3, ORG_ID,
3296       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3297       CREATED_BY, CREATION_DATE
3298       FROM JTY_WEBADI_OTH_TERR_INTF jut
3299       where jut.USER_SEQUENCE = v_user_sequence
3300   	  and jut.status is null
3301 	  and jut.terr_qual_id17 is not null
3302 	  and jut.QUAL17_VALUE_ID is null
3303 	  and jut.qual17_value1 is not null
3304   	  and jut.INTERFACE_TYPE = v_intf_type
3305   	  and jut.header = v_header
3306       union all
3307       select terr_id, user_sequence,
3308 	  terr_qual_id18 terr_qual_id,
3309       18 qual_num, QUAL18_VALUE_ID qual_value_id,
3310       QUAL18_VALUE1 qual_VALUE1,
3311       QUAL18_VALUE2 qual_VALUE2,
3312       QUAL18_VALUE3 qual_VALUE3, ORG_ID,
3313       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3314       CREATED_BY, CREATION_DATE
3315       FROM JTY_WEBADI_OTH_TERR_INTF jut
3316       where jut.USER_SEQUENCE = v_user_sequence
3317   	  and jut.status is null
3318 	  and jut.terr_qual_id18 is not null
3319 	  and jut.QUAL18_VALUE_ID is null
3320 	  and jut.qual18_value1 is not null
3321   	  and jut.INTERFACE_TYPE = v_intf_type
3322   	  and jut.header = v_header
3323       union all
3324       select terr_id, user_sequence,
3325 	  terr_qual_id19 terr_qual_id,
3326       19 qual_num, QUAL19_VALUE_ID qual_value_id,
3327       QUAL19_VALUE1 qual_VALUE1,
3328       QUAL19_VALUE2 qual_VALUE2,
3329       QUAL19_VALUE3 qual_VALUE3, ORG_ID,
3330       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3331       CREATED_BY, CREATION_DATE
3332       FROM JTY_WEBADI_OTH_TERR_INTF jut
3333       where jut.USER_SEQUENCE = v_user_sequence
3334   	  and jut.status is null
3335 	  and jut.terr_qual_id19 is not null
3336 	  and jut.QUAL19_VALUE_ID is null
3337 	  and jut.qual19_value1 is not null
3338   	  and jut.INTERFACE_TYPE = v_intf_type
3339   	  and jut.header = v_header
3340       union all
3341       select terr_id, user_sequence,
3342 	  terr_qual_id20 terr_qual_id,
3343       20 qual_num, QUAL20_VALUE_ID qual_value_id,
3344       QUAL20_VALUE1 qual_VALUE1,
3345       QUAL20_VALUE2 qual_VALUE2,
3346       QUAL20_VALUE3 qual_VALUE3, ORG_ID,
3347       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3348       CREATED_BY, CREATION_DATE
3349       FROM JTY_WEBADI_OTH_TERR_INTF jut
3350       where jut.USER_SEQUENCE = v_user_sequence
3351   	  and jut.status is null
3352 	  and jut.terr_qual_id20 is not null
3353 	  and jut.QUAL20_VALUE_ID is null
3354 	  and jut.qual20_value1 is not null
3355   	  and jut.INTERFACE_TYPE = v_intf_type
3356   	  and jut.header = v_header
3357       union all
3358       select terr_id, user_sequence,
3359 	  terr_qual_id21 terr_qual_id,
3360       21 qual_num, QUAL21_VALUE_ID qual_value_id,
3361       QUAL21_VALUE1 qual_VALUE1,
3362       QUAL21_VALUE2 qual_VALUE2,
3363       QUAL21_VALUE3 qual_VALUE3, ORG_ID,
3364       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3365       CREATED_BY, CREATION_DATE
3366       FROM JTY_WEBADI_OTH_TERR_INTF jut
3367       where jut.USER_SEQUENCE = v_user_sequence
3368   	  and jut.status is null
3369 	  and jut.terr_qual_id21 is not null
3370 	  and jut.QUAL21_VALUE_ID is null
3371 	  and jut.qual21_value1 is not null
3372   	  and jut.INTERFACE_TYPE = v_intf_type
3373   	  and jut.header = v_header
3374       union all
3375       select terr_id, user_sequence,
3376 	  terr_qual_id22 terr_qual_id,
3377       22 qual_num, QUAL22_VALUE_ID qual_value_id,
3378       QUAL22_VALUE1 qual_VALUE1,
3379       QUAL22_VALUE2 qual_VALUE2,
3380       QUAL22_VALUE3 qual_VALUE3, ORG_ID,
3381       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3382       CREATED_BY, CREATION_DATE
3383       FROM JTY_WEBADI_OTH_TERR_INTF jut
3384       where jut.USER_SEQUENCE = v_user_sequence
3385   	  and jut.status is null
3386 	  and jut.terr_qual_id22 is not null
3390   	  and jut.header = v_header
3387 	  and jut.QUAL22_VALUE_ID is null
3388 	  and jut.qual22_value1 is not null
3389   	  and jut.INTERFACE_TYPE = v_intf_type
3391       union all
3392       select terr_id, user_sequence,
3393 	  terr_qual_id23 terr_qual_id,
3394       23 qual_num, QUAL23_VALUE_ID qual_value_id,
3395       QUAL23_VALUE1 qual_VALUE1,
3396       QUAL23_VALUE2 qual_VALUE2,
3397       QUAL23_VALUE3 qual_VALUE3, ORG_ID,
3398       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3399       CREATED_BY, CREATION_DATE
3400       FROM JTY_WEBADI_OTH_TERR_INTF jut
3401       where jut.USER_SEQUENCE = v_user_sequence
3402   	  and jut.status is null
3403 	  and jut.terr_qual_id23 is not null
3404 	  and jut.QUAL23_VALUE_ID is null
3405 	  and jut.qual23_value1 is not null
3406   	  and jut.INTERFACE_TYPE = v_intf_type
3407   	  and jut.header = v_header
3408       union all
3409       select terr_id, user_sequence,
3410 	  terr_qual_id24 terr_qual_id,
3411       24 qual_num, QUAL24_VALUE_ID qual_value_id,
3412       QUAL24_VALUE1 qual_VALUE1,
3413       QUAL24_VALUE2 qual_VALUE2,
3414       QUAL24_VALUE3 qual_VALUE3, ORG_ID,
3415       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3416       CREATED_BY, CREATION_DATE
3417       FROM JTY_WEBADI_OTH_TERR_INTF jut
3418       where jut.USER_SEQUENCE = v_user_sequence
3419   	  and jut.status is null
3420 	  and jut.terr_qual_id24 is not null
3421 	  and jut.QUAL24_VALUE_ID is null
3422 	  and jut.qual24_value1 is not null
3423   	  and jut.INTERFACE_TYPE = v_intf_type
3424   	  and jut.header = v_header
3425       union all
3426       select terr_id, user_sequence,
3427 	  terr_qual_id25 terr_qual_id,
3428       25 qual_num, QUAL25_VALUE_ID qual_value_id,
3429       QUAL25_VALUE1 qual_valUE1,
3430       QUAL25_VALUE2 qual_valUE2,
3431       QUAL25_VALUE3 qual_valUE3, ORG_ID,
3432       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3433       CREATED_BY, CREATION_DATE
3434       FROM JTY_WEBADI_OTH_TERR_INTF jut
3435       where jut.USER_SEQUENCE = v_user_sequence
3436   	  and jut.status is null
3437 	  and jut.terr_qual_id25 is not null
3438 	  and jut.QUAL25_VALUE_ID is null
3439 	  and jut.qual25_value1 is not null
3440   	  and jut.INTERFACE_TYPE = v_intf_type
3441   	  and jut.header = v_header) sub
3442       where jq.user_sequence = sub.user_sequence
3443         and jq.qualifier_num = sub.qual_num;
3444 
3445 	CURSOR get_u_terr_value_csr(
3446 	  v_user_sequence number,
3447 	  v_action_flag	  varchar2,
3448 	  v_intf_type	  varchar2,
3449 	  v_header		  varchar2) IS
3450     select sub.TERR_QUAL_ID, sub.terr_id, sub.qual_value_id,
3451   	sub.qual_value1, sub.qual_value2, sub.qual_value3,
3452   	sub.org_id, sub.last_updated_by, sub.last_update_date,
3453   	sub.last_update_login, sub.creation_date, sub.created_by,
3454   	jq.qual_usg_id, jq.display_type qual_type, jq.CONVERT_TO_ID_FLAG,
3455 	jq.qualifier_num, jq.html_lov_sql1,
3456   	(case
3457   	  when jq.COMPARISON_OPERATOR = '=' then '='
3458   	  when (jq.COMPARISON_OPERATOR LIKE '%LIKE%') AND (instr(sub.qual_VALUE1,'_') > 0) or (instr(sub.qual_VALUE1,'%') > 0 and sub.qual_VALUE2 is null) then 'LIKE'
3459   	  when (jq.COMPARISON_OPERATOR LIKE '%BETWEEN%') AND (sub.qual_VALUE1 is not null and sub.qual_VALUE2 is not null) then 'BETWEEN'
3460   	  else '='
3461   	end) qual_cond
3462       from JTY_WEBADI_QUAL_HEADER jq,
3463       (
3464       select terr_id, user_sequence,
3465 	  terr_qual_id1 terr_qual_id,
3466       1 qual_num, QUAL1_VALUE_ID qual_value_id,
3467       QUAL1_VALUE1 qual_VALUE1,
3468       QUAL1_VALUE2 qual_VALUE2,
3469       QUAL1_VALUE3 qual_VALUE3, ORG_ID,
3470       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3471       CREATED_BY, CREATION_DATE
3472       FROM JTY_WEBADI_OTH_TERR_INTF jut
3473       where jut.USER_SEQUENCE = v_user_sequence
3474   	  and jut.status is null
3475 	  and jut.terr_qual_id1 is not null
3476 	  and jut.QUAL1_VALUE_ID is not null
3477 	  and jut.qual1_value1 is not null
3478       and jut.INTERFACE_TYPE = v_intf_type
3479 	  AND jut.ACTION_FLAG = v_action_flag
3480   	  and jut.header = v_header
3481       union all
3482       select terr_id, user_sequence,
3483 	  terr_qual_id2 terr_qual_id,
3484       2 qual_num, QUAL2_VALUE_ID qual_value_id,
3485       QUAL2_VALUE1 qual_VALUE1,
3486       QUAL2_VALUE2 qual_VALUE2,
3487       QUAL2_VALUE3 qual_VALUE3, ORG_ID,
3488       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3489       CREATED_BY, CREATION_DATE
3490       FROM JTY_WEBADI_OTH_TERR_INTF jut
3491       where jut.USER_SEQUENCE = v_user_sequence
3492   	  and jut.status is null
3493 	  and jut.terr_qual_id2 is not null
3494 	  and jut.QUAL2_VALUE_ID is not null
3495 	  and jut.qual2_value1 is not null
3496   	  and jut.INTERFACE_TYPE = v_intf_type
3497 	  AND jut.ACTION_FLAG = v_action_flag
3498   	  and jut.header = v_header
3499       union all
3500       select terr_id, user_sequence,
3501 	  terr_qual_id3 terr_qual_id,
3502       3 qual_num, QUAL3_VALUE_ID qual_value_id,
3503       QUAL3_VALUE1 qual_VALUE1,
3504       QUAL3_VALUE2 qual_VALUE2,
3505       QUAL3_VALUE3 qual_VALUE3, ORG_ID,
3506       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3507       CREATED_BY, CREATION_DATE
3508       FROM JTY_WEBADI_OTH_TERR_INTF jut
3509       where jut.USER_SEQUENCE = v_user_sequence
3510   	  and jut.status is null
3511 	  and jut.terr_qual_id3 is not null
3512 	  and jut.QUAL3_VALUE_ID is not null
3513 	  and jut.qual3_value1 is not null
3517       union all
3514   	  and jut.INTERFACE_TYPE = v_intf_type
3515 	  AND jut.ACTION_FLAG = v_action_flag
3516   	  and jut.header = v_header
3518       select terr_id, user_sequence,
3519 	  terr_qual_id4 terr_qual_id,
3520       4 qual_num, QUAL4_VALUE_ID qual_value_id,
3521       QUAL4_VALUE1 qual_VALUE1,
3522       QUAL4_VALUE2 qual_VALUE2,
3523       QUAL4_VALUE3 qual_VALUE3, ORG_ID,
3524       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3525       CREATED_BY, CREATION_DATE
3526       FROM JTY_WEBADI_OTH_TERR_INTF jut
3527       where jut.USER_SEQUENCE = v_user_sequence
3528   	  and jut.status is null
3529 	  and jut.terr_qual_id4 is not null
3530 	  and jut.QUAL4_VALUE_ID is not null
3531 	  and jut.qual4_value1 is not null
3532   	  and jut.INTERFACE_TYPE = v_intf_type
3533 	  AND jut.ACTION_FLAG = v_action_flag
3534   	  and jut.header = v_header
3535       union all
3536       select terr_id, user_sequence,
3537 	  terr_qual_id5 terr_qual_id,
3538       5 qual_num, QUAL5_VALUE_ID qual_value_id,
3539       QUAL5_VALUE1 qual_VALUE1,
3540       QUAL5_VALUE2 qual_VALUE2,
3541       QUAL5_VALUE3 qual_VALUE3, ORG_ID,
3542       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3543       CREATED_BY, CREATION_DATE
3544       FROM JTY_WEBADI_OTH_TERR_INTF jut
3545       where jut.USER_SEQUENCE = v_user_sequence
3546   	  and jut.status is null
3547 	  and jut.terr_qual_id5 is not null
3548 	  and jut.QUAL5_VALUE_ID is not null
3549 	  and jut.qual5_value1 is not null
3550   	  and jut.INTERFACE_TYPE = v_intf_type
3551 	  AND jut.ACTION_FLAG = v_action_flag
3552   	  and jut.header = v_header
3553       union all
3554       select terr_id, user_sequence,
3555 	  terr_qual_id6 terr_qual_id,
3556       6 qual_num, QUAL6_VALUE_ID qual_value_id,
3557       QUAL6_VALUE1 qual_VALUE1,
3558       QUAL6_VALUE2 qual_VALUE2,
3559       QUAL6_VALUE3 qual_VALUE3, ORG_ID,
3560       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3561       CREATED_BY, CREATION_DATE
3562       FROM JTY_WEBADI_OTH_TERR_INTF jut
3563       where jut.USER_SEQUENCE = v_user_sequence
3564   	  and jut.status is null
3565 	  and jut.terr_qual_id6 is not null
3566 	  and jut.QUAL6_VALUE_ID is not null
3567 	  and jut.qual6_value1 is not null
3568   	  and jut.INTERFACE_TYPE = v_intf_type
3569 	  AND jut.ACTION_FLAG = v_action_flag
3570   	  and jut.header = v_header
3571       union all
3572       select terr_id, user_sequence,
3573 	  terr_qual_id7 terr_qual_id,
3574       7 qual_num, QUAL7_VALUE_ID qual_value_id,
3575       QUAL7_VALUE1 qual_VALUE1,
3576       QUAL7_VALUE2 qual_VALUE2,
3577       QUAL7_VALUE3 qual_VALUE3, ORG_ID,
3578       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3579       CREATED_BY, CREATION_DATE
3580       FROM JTY_WEBADI_OTH_TERR_INTF jut
3581       where jut.USER_SEQUENCE = v_user_sequence
3582   	  and jut.status is null
3583 	  and jut.terr_qual_id7 is not null
3584 	  and jut.QUAL7_VALUE_ID is not null
3585 	  and jut.qual7_value1 is not null
3586   	  and jut.INTERFACE_TYPE = v_intf_type
3587 	  AND jut.ACTION_FLAG = v_action_flag
3588   	  and jut.header = v_header
3589       union all
3590       select terr_id, user_sequence,
3591 	  terr_qual_id8 terr_qual_id,
3592       8 qual_num, QUAL8_VALUE_ID qual_value_id,
3593       QUAL8_VALUE1 qual_VALUE1,
3594       QUAL8_VALUE2 qual_VALUE2,
3595       QUAL8_VALUE3 qual_VALUE3, ORG_ID,
3596       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3597       CREATED_BY, CREATION_DATE
3598       FROM JTY_WEBADI_OTH_TERR_INTF jut
3599       where jut.USER_SEQUENCE = v_user_sequence
3600   	  and jut.status is null
3601 	  and jut.terr_qual_id8 is not null
3602 	  and jut.QUAL8_VALUE_ID is not null
3603 	  and jut.qual8_value1 is not null
3604   	  and jut.INTERFACE_TYPE = v_intf_type
3605 	  AND jut.ACTION_FLAG = v_action_flag
3606   	  and jut.header = v_header
3607       union all
3608       select terr_id, user_sequence,
3609 	  terr_qual_id9 terr_qual_id,
3610       9 qual_num, QUAL9_VALUE_ID qual_value_id,
3611       QUAL9_VALUE1 qual_VALUE1,
3612       QUAL9_VALUE2 qual_VALUE2,
3613       QUAL9_VALUE3 qual_VALUE3, ORG_ID,
3614       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3615       CREATED_BY, CREATION_DATE
3616       FROM JTY_WEBADI_OTH_TERR_INTF jut
3617       where jut.USER_SEQUENCE = v_user_sequence
3618   	  and jut.status is null
3619 	  and jut.terr_qual_id9 is not null
3620 	  and jut.QUAL9_VALUE_ID is not null
3621 	  and jut.qual9_value1 is not null
3622   	  and jut.INTERFACE_TYPE = v_intf_type
3623 	  AND jut.ACTION_FLAG = v_action_flag
3624   	  and jut.header = v_header
3625       union all
3626       select terr_id, user_sequence,
3627 	  terr_qual_id10 terr_qual_id,
3628       10 qual_num, QUAL10_VALUE_ID qual_value_id,
3629       QUAL10_VALUE1 qual_VALUE1,
3630       QUAL10_VALUE2 qual_VALUE2,
3631       QUAL10_VALUE3 qual_VALUE3, ORG_ID,
3632       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3633       CREATED_BY, CREATION_DATE
3634       FROM JTY_WEBADI_OTH_TERR_INTF jut
3635       where jut.USER_SEQUENCE = v_user_sequence
3636   	  and jut.status is null
3637 	  and jut.terr_qual_id10 is not null
3638 	  and jut.QUAL10_VALUE_ID is not null
3639 	  and jut.qual10_value1 is not null
3640   	  and jut.INTERFACE_TYPE = v_intf_type
3641 	  AND jut.ACTION_FLAG = v_action_flag
3642   	  and jut.header = v_header
3643       union all
3644       select terr_id, user_sequence,
3648       QUAL11_VALUE2 qual_VALUE2,
3645 	  terr_qual_id11 terr_qual_id,
3646       11 qual_num, QUAL11_VALUE_ID qual_value_id,
3647       QUAL11_VALUE1 qual_VALUE1,
3649       QUAL11_VALUE3 qual_VALUE3, ORG_ID,
3650       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3651       CREATED_BY, CREATION_DATE
3652       FROM JTY_WEBADI_OTH_TERR_INTF jut
3653       where jut.USER_SEQUENCE = v_user_sequence
3654   	  and jut.status is null
3655 	  and jut.terr_qual_id11 is not null
3656 	  and jut.QUAL11_VALUE_ID is not null
3657 	  and jut.qual11_value1 is not null
3658   	  and jut.INTERFACE_TYPE = v_intf_type
3659 	  AND jut.ACTION_FLAG = v_action_flag
3660   	  and jut.header = v_header
3661       union all
3662       select terr_id, user_sequence,
3663 	  terr_qual_id12 terr_qual_id,
3664       12 qual_num, QUAL12_VALUE_ID qual_value_id,
3665       QUAL12_VALUE1 qual_VALUE1,
3666       QUAL12_VALUE2 qual_VALUE2,
3667       QUAL12_VALUE3 qual_VALUE3, ORG_ID,
3668       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3669       CREATED_BY, CREATION_DATE
3670       FROM JTY_WEBADI_OTH_TERR_INTF jut
3671       where jut.USER_SEQUENCE = v_user_sequence
3672   	  and jut.status is null
3673 	  and jut.terr_qual_id12 is not null
3674 	  and jut.QUAL12_VALUE_ID is not null
3675 	  and jut.qual12_value1 is not null
3676   	  and jut.INTERFACE_TYPE = v_intf_type
3677 	  AND jut.ACTION_FLAG = v_action_flag
3678   	  and jut.header = v_header
3679       union all
3680       select terr_id, user_sequence,
3681 	  terr_qual_id13 terr_qual_id,
3682       13 qual_num, QUAL13_VALUE_ID qual_value_id,
3683       QUAL13_VALUE1 qual_VALUE1,
3684       QUAL13_VALUE2 qual_VALUE2,
3685       QUAL13_VALUE3 qual_VALUE3, ORG_ID,
3686       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3687       CREATED_BY, CREATION_DATE
3688       FROM JTY_WEBADI_OTH_TERR_INTF jut
3689       where jut.USER_SEQUENCE = v_user_sequence
3690   	  and jut.status is null
3691 	  and jut.terr_qual_id13 is not null
3692 	  and jut.QUAL13_VALUE_ID is not null
3693 	  and jut.qual13_value1 is not null
3694   	  and jut.INTERFACE_TYPE = v_intf_type
3695 	  AND jut.ACTION_FLAG = v_action_flag
3696   	  and jut.header = v_header
3697       union all
3698       select terr_id, user_sequence,
3699 	  terr_qual_id14 terr_qual_id,
3700       14 qual_num, QUAL14_VALUE_ID qual_value_id,
3701       QUAL14_VALUE1 qual_VALUE1,
3702       QUAL14_VALUE2 qual_VALUE2,
3703       QUAL14_VALUE3 qual_VALUE3, ORG_ID,
3704       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3705       CREATED_BY, CREATION_DATE
3706       FROM JTY_WEBADI_OTH_TERR_INTF jut
3707       where jut.USER_SEQUENCE = v_user_sequence
3708   	  and jut.status is null
3709 	  and jut.terr_qual_id14 is not null
3710 	  and jut.QUAL14_VALUE_ID is not null
3711 	  and jut.qual14_value1 is not null
3712   	  and jut.INTERFACE_TYPE = v_intf_type
3713 	  AND jut.ACTION_FLAG = v_action_flag
3714   	  and jut.header = v_header
3715       union all
3716       select terr_id, user_sequence,
3717 	  terr_qual_id15 terr_qual_id,
3718       15 qual_num, QUAL15_VALUE_ID qual_value_id,
3719       QUAL15_VALUE1 qual_VALUE1,
3720       QUAL15_VALUE2 qual_VALUE2,
3721       QUAL15_VALUE3 qual_VALUE3, ORG_ID,
3722       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3723       CREATED_BY, CREATION_DATE
3724       FROM JTY_WEBADI_OTH_TERR_INTF jut
3725       where jut.USER_SEQUENCE = v_user_sequence
3726   	  and jut.status is null
3727 	  and jut.terr_qual_id15 is not null
3728 	  and jut.QUAL15_VALUE_ID is not null
3729 	  and jut.qual15_value1 is not null
3730   	  and jut.INTERFACE_TYPE = v_intf_type
3731 	  AND jut.ACTION_FLAG = v_action_flag
3732   	  and jut.header = v_header
3733       union all
3734       select terr_id, user_sequence,
3735 	  terr_qual_id16 terr_qual_id,
3736       16 qual_num, QUAL16_VALUE_ID qual_value_id,
3737       QUAL16_VALUE1 qual_VALUE1,
3738       QUAL16_VALUE2 qual_VALUE2,
3739       QUAL16_VALUE3 qual_VALUE3, ORG_ID,
3740       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3741       CREATED_BY, CREATION_DATE
3742       FROM JTY_WEBADI_OTH_TERR_INTF jut
3743       where jut.USER_SEQUENCE = v_user_sequence
3744   	  and jut.status is null
3745 	  and jut.terr_qual_id16 is not null
3746 	  and jut.QUAL16_VALUE_ID is not null
3747 	  and jut.qual16_value1 is not null
3748   	  and jut.INTERFACE_TYPE = v_intf_type
3749 	  AND jut.ACTION_FLAG = v_action_flag
3750   	  and jut.header = v_header
3751       union all
3752       select terr_id, user_sequence,
3753 	  terr_qual_id17 terr_qual_id,
3754       17 qual_num, QUAL17_VALUE_ID qual_value_id,
3755       QUAL17_VALUE1 qual_VALUE1,
3756       QUAL17_VALUE2 qual_VALUE2,
3757       QUAL17_VALUE3 qual_VALUE3, ORG_ID,
3758       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3759       CREATED_BY, CREATION_DATE
3760       FROM JTY_WEBADI_OTH_TERR_INTF jut
3761       where jut.USER_SEQUENCE = v_user_sequence
3762   	  and jut.status is null
3763 	  and jut.terr_qual_id17 is not null
3764 	  and jut.QUAL17_VALUE_ID is not null
3765 	  and jut.qual17_value1 is not null
3766   	  and jut.INTERFACE_TYPE = v_intf_type
3767 	  AND jut.ACTION_FLAG = v_action_flag
3768   	  and jut.header = v_header
3769       union all
3770       select terr_id, user_sequence,
3771 	  terr_qual_id18 terr_qual_id,
3772       18 qual_num, QUAL18_VALUE_ID qual_value_id,
3773       QUAL18_VALUE1 qual_VALUE1,
3777       CREATED_BY, CREATION_DATE
3774       QUAL18_VALUE2 qual_VALUE2,
3775       QUAL18_VALUE3 qual_VALUE3, ORG_ID,
3776       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3778       FROM JTY_WEBADI_OTH_TERR_INTF jut
3779       where jut.USER_SEQUENCE = v_user_sequence
3780   	  and jut.status is null
3781 	  and jut.terr_qual_id18 is not null
3782 	  and jut.QUAL18_VALUE_ID is not null
3783 	  and jut.qual18_value1 is not null
3784   	  and jut.INTERFACE_TYPE = v_intf_type
3785 	  AND jut.ACTION_FLAG = v_action_flag
3786   	  and jut.header = v_header
3787       union all
3788       select terr_id, user_sequence,
3789 	  terr_qual_id19 terr_qual_id,
3790       19 qual_num, QUAL19_VALUE_ID qual_value_id,
3791       QUAL19_VALUE1 qual_VALUE1,
3792       QUAL19_VALUE2 qual_VALUE2,
3793       QUAL19_VALUE3 qual_VALUE3, ORG_ID,
3794       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3795       CREATED_BY, CREATION_DATE
3796       FROM JTY_WEBADI_OTH_TERR_INTF jut
3797       where jut.USER_SEQUENCE = v_user_sequence
3798   	  and jut.status is null
3799 	  and jut.terr_qual_id19 is not null
3800 	  and jut.QUAL19_VALUE_ID is not null
3801 	  and jut.qual19_value1 is not null
3802   	  and jut.INTERFACE_TYPE = v_intf_type
3803 	  AND jut.ACTION_FLAG = v_action_flag
3804   	  and jut.header = v_header
3805       union all
3806       select terr_id, user_sequence,
3807 	  terr_qual_id20 terr_qual_id,
3808       20 qual_num, QUAL20_VALUE_ID qual_value_id,
3809       QUAL20_VALUE1 qual_VALUE1,
3810       QUAL20_VALUE2 qual_VALUE2,
3811       QUAL20_VALUE3 qual_VALUE3, ORG_ID,
3812       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3813       CREATED_BY, CREATION_DATE
3814       FROM JTY_WEBADI_OTH_TERR_INTF jut
3815       where jut.USER_SEQUENCE = v_user_sequence
3816   	  and jut.status is null
3817 	  and jut.terr_qual_id20 is not null
3818 	  and jut.QUAL20_VALUE_ID is not null
3819 	  and jut.qual20_value1 is not null
3820   	  and jut.INTERFACE_TYPE = v_intf_type
3821 	  AND jut.ACTION_FLAG = v_action_flag
3822   	  and jut.header = v_header
3823       union all
3824       select terr_id, user_sequence,
3825 	  terr_qual_id21 terr_qual_id,
3826       21 qual_num, QUAL21_VALUE_ID qual_value_id,
3827       QUAL21_VALUE1 qual_VALUE1,
3828       QUAL21_VALUE2 qual_VALUE2,
3829       QUAL21_VALUE3 qual_VALUE3, ORG_ID,
3830       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3831       CREATED_BY, CREATION_DATE
3832       FROM JTY_WEBADI_OTH_TERR_INTF jut
3833       where jut.USER_SEQUENCE = v_user_sequence
3834   	  and jut.status is null
3835 	  and jut.terr_qual_id21 is not null
3836 	  and jut.QUAL21_VALUE_ID is not null
3837 	  and jut.qual21_value1 is not null
3838   	  and jut.INTERFACE_TYPE = v_intf_type
3839 	  AND jut.ACTION_FLAG = v_action_flag
3840   	  and jut.header = v_header
3841       union all
3842       select terr_id, user_sequence,
3843 	  terr_qual_id22 terr_qual_id,
3844       22 qual_num, QUAL22_VALUE_ID qual_value_id,
3845       QUAL22_VALUE1 qual_VALUE1,
3846       QUAL22_VALUE2 qual_VALUE2,
3847       QUAL22_VALUE3 qual_VALUE3, ORG_ID,
3848       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3849       CREATED_BY, CREATION_DATE
3850       FROM JTY_WEBADI_OTH_TERR_INTF jut
3851       where jut.USER_SEQUENCE = v_user_sequence
3852   	  and jut.status is null
3853 	  and jut.terr_qual_id22 is not null
3854 	  and jut.QUAL22_VALUE_ID is not null
3855 	  and jut.qual22_value1 is not null
3856   	  and jut.INTERFACE_TYPE = v_intf_type
3857 	  AND jut.ACTION_FLAG = v_action_flag
3858   	  and jut.header = v_header
3859       union all
3860       select terr_id, user_sequence,
3861 	  terr_qual_id23 terr_qual_id,
3862       23 qual_num, QUAL23_VALUE_ID qual_value_id,
3863       QUAL23_VALUE1 qual_VALUE1,
3864       QUAL23_VALUE2 qual_VALUE2,
3865       QUAL23_VALUE3 qual_VALUE3, ORG_ID,
3866       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3867       CREATED_BY, CREATION_DATE
3868       FROM JTY_WEBADI_OTH_TERR_INTF jut
3869       where jut.USER_SEQUENCE = v_user_sequence
3870   	  and jut.status is null
3871 	  and jut.terr_qual_id23 is not null
3872 	  and jut.QUAL23_VALUE_ID is not null
3873 	  and jut.qual23_value1 is not null
3874   	  and jut.INTERFACE_TYPE = v_intf_type
3875 	  AND jut.ACTION_FLAG = v_action_flag
3876   	  and jut.header = v_header
3877       union all
3878       select terr_id, user_sequence,
3879 	  terr_qual_id24 terr_qual_id,
3880       24 qual_num, QUAL24_VALUE_ID qual_value_id,
3881       QUAL24_VALUE1 qual_VALUE1,
3882       QUAL24_VALUE2 qual_VALUE2,
3883       QUAL24_VALUE3 qual_VALUE3, ORG_ID,
3884       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3885       CREATED_BY, CREATION_DATE
3886       FROM JTY_WEBADI_OTH_TERR_INTF jut
3887       where jut.USER_SEQUENCE = v_user_sequence
3888   	  and jut.status is null
3889 	  and jut.terr_qual_id24 is not null
3890 	  and jut.QUAL24_VALUE_ID is not null
3891 	  and jut.qual24_value1 is not null
3892   	  and jut.INTERFACE_TYPE = v_intf_type
3893 	  AND jut.ACTION_FLAG = v_action_flag
3894   	  and jut.header = v_header
3895       union all
3896       select terr_id, user_sequence,
3897 	  terr_qual_id25 terr_qual_id,
3898       25 qual_num, QUAL25_VALUE_ID qual_value_id,
3899       QUAL25_VALUE1 qual_valUE1,
3900       QUAL25_VALUE2 qual_valUE2,
3901       QUAL25_VALUE3 qual_valUE3, ORG_ID,
3905       where jut.USER_SEQUENCE = v_user_sequence
3902       LAST_UPDATED_BY, LAST_UPDATE_DATE, LAST_UPDATE_LOGIN,
3903       CREATED_BY, CREATION_DATE
3904       FROM JTY_WEBADI_OTH_TERR_INTF jut
3906   	  and jut.status is null
3907 	  and jut.terr_qual_id25 is not null
3908 	  and jut.QUAL25_VALUE_ID is not null
3909 	  and jut.qual25_value1 is not null
3910   	  and jut.INTERFACE_TYPE = v_intf_type
3911 	  AND jut.ACTION_FLAG = v_action_flag
3912   	  and jut.header = v_header) sub
3913       where jq.user_sequence = sub.user_sequence
3914         and jq.qualifier_num = sub.qual_num;
3915 
3916 	CURSOR get_d_terr_value_csr(
3917 	  v_user_sequence number,
3918 	  v_action_flag	  varchar2,
3919 	  v_intf_type	  varchar2,
3920 	  v_header		  varchar2) IS
3921     select sub.TERR_QUAL_ID, sub.terr_id, sub.qual_value_id,
3922 	sub.qual_num
3923       from (
3924       select terr_id, user_sequence,
3925 	  terr_qual_id1 terr_qual_id,
3926       1 qual_num, QUAL1_VALUE_ID qual_value_id
3927       FROM JTY_WEBADI_OTH_TERR_INTF jut
3928       where jut.USER_SEQUENCE = v_user_sequence
3929   	  and jut.status is null
3930 	  and jut.terr_qual_id1 is not null
3931 	  and jut.QUAL1_VALUE_ID is not null
3932 	  and jut.qual1_value1 is null
3933       and jut.INTERFACE_TYPE = v_intf_type
3934 	  AND jut.ACTION_FLAG = v_action_flag
3935   	  and jut.header = v_header
3936       union all
3937       select terr_id, user_sequence,
3938 	  terr_qual_id2 terr_qual_id,
3939       2 qual_num, QUAL2_VALUE_ID qual_value_id
3940       FROM JTY_WEBADI_OTH_TERR_INTF jut
3941       where jut.USER_SEQUENCE = v_user_sequence
3942   	  and jut.status is null
3943 	  and jut.terr_qual_id2 is not null
3944 	  and jut.QUAL2_VALUE_ID is not null
3945 	  and jut.qual2_value1 is null
3946   	  and jut.INTERFACE_TYPE = v_intf_type
3947 	  AND jut.ACTION_FLAG = v_action_flag
3948   	  and jut.header = v_header
3949       union all
3950       select terr_id, user_sequence,
3951 	  terr_qual_id3 terr_qual_id,
3952       3 qual_num, QUAL3_VALUE_ID qual_value_id
3953       FROM JTY_WEBADI_OTH_TERR_INTF jut
3954       where jut.USER_SEQUENCE = v_user_sequence
3955   	  and jut.status is null
3956 	  and jut.terr_qual_id3 is not null
3957 	  and jut.QUAL3_VALUE_ID is not null
3958 	  and jut.qual3_value1 is null
3959   	  and jut.INTERFACE_TYPE = v_intf_type
3960 	  AND jut.ACTION_FLAG = v_action_flag
3961   	  and jut.header = v_header
3962       union all
3963       select terr_id, user_sequence,
3964 	  terr_qual_id4 terr_qual_id,
3965       4 qual_num, QUAL4_VALUE_ID qual_value_id
3966       FROM JTY_WEBADI_OTH_TERR_INTF jut
3967       where jut.USER_SEQUENCE = v_user_sequence
3968   	  and jut.status is null
3969 	  and jut.terr_qual_id4 is not null
3970 	  and jut.QUAL4_VALUE_ID is not null
3971 	  and jut.qual4_value1 is null
3972   	  and jut.INTERFACE_TYPE = v_intf_type
3973 	  AND jut.ACTION_FLAG = v_action_flag
3974   	  and jut.header = v_header
3975       union all
3976       select terr_id, user_sequence,
3977 	  terr_qual_id5 terr_qual_id,
3978       5 qual_num, QUAL5_VALUE_ID qual_value_id
3979       FROM JTY_WEBADI_OTH_TERR_INTF jut
3980       where jut.USER_SEQUENCE = v_user_sequence
3981   	  and jut.status is null
3982 	  and jut.terr_qual_id5 is not null
3983 	  and jut.QUAL5_VALUE_ID is not null
3984 	  and jut.qual5_value1 is null
3985   	  and jut.INTERFACE_TYPE = v_intf_type
3986 	  AND jut.ACTION_FLAG = v_action_flag
3987   	  and jut.header = v_header
3988       union all
3989       select terr_id, user_sequence,
3990 	  terr_qual_id6 terr_qual_id,
3991       6 qual_num, QUAL6_VALUE_ID qual_value_id
3992       FROM JTY_WEBADI_OTH_TERR_INTF jut
3993       where jut.USER_SEQUENCE = v_user_sequence
3994   	  and jut.status is null
3995 	  and jut.terr_qual_id6 is not null
3996 	  and jut.QUAL6_VALUE_ID is not null
3997 	  and jut.qual6_value1 is null
3998   	  and jut.INTERFACE_TYPE = v_intf_type
3999 	  AND jut.ACTION_FLAG = v_action_flag
4000   	  and jut.header = v_header
4001       union all
4002       select terr_id, user_sequence,
4003 	  terr_qual_id7 terr_qual_id,
4004       7 qual_num, QUAL7_VALUE_ID qual_value_id
4005       FROM JTY_WEBADI_OTH_TERR_INTF jut
4006       where jut.USER_SEQUENCE = v_user_sequence
4007   	  and jut.status is null
4008 	  and jut.terr_qual_id7 is not null
4009 	  and jut.QUAL7_VALUE_ID is not null
4010 	  and jut.qual7_value1 is null
4011   	  and jut.INTERFACE_TYPE = v_intf_type
4012 	  AND jut.ACTION_FLAG = v_action_flag
4013   	  and jut.header = v_header
4014       union all
4015       select terr_id, user_sequence,
4016 	  terr_qual_id8 terr_qual_id,
4017       8 qual_num, QUAL8_VALUE_ID qual_value_id
4018       FROM JTY_WEBADI_OTH_TERR_INTF jut
4019       where jut.USER_SEQUENCE = v_user_sequence
4020   	  and jut.status is null
4021 	  and jut.terr_qual_id8 is not null
4022 	  and jut.QUAL8_VALUE_ID is not null
4023 	  and jut.qual8_value1 is null
4024   	  and jut.INTERFACE_TYPE = v_intf_type
4025 	  AND jut.ACTION_FLAG = v_action_flag
4026   	  and jut.header = v_header
4027       union all
4028       select terr_id, user_sequence,
4029 	  terr_qual_id9 terr_qual_id,
4030       9 qual_num, QUAL9_VALUE_ID qual_value_id
4031       FROM JTY_WEBADI_OTH_TERR_INTF jut
4032       where jut.USER_SEQUENCE = v_user_sequence
4033   	  and jut.status is null
4034 	  and jut.terr_qual_id9 is not null
4035 	  and jut.QUAL9_VALUE_ID is not null
4039   	  and jut.header = v_header
4036 	  and jut.qual9_value1 is null
4037   	  and jut.INTERFACE_TYPE = v_intf_type
4038 	  AND jut.ACTION_FLAG = v_action_flag
4040       union all
4041       select terr_id, user_sequence,
4042 	  terr_qual_id10 terr_qual_id,
4043       10 qual_num, QUAL10_VALUE_ID qual_value_id
4044       FROM JTY_WEBADI_OTH_TERR_INTF jut
4045       where jut.USER_SEQUENCE = v_user_sequence
4046   	  and jut.status is null
4047 	  and jut.terr_qual_id10 is not null
4048 	  and jut.QUAL10_VALUE_ID is not null
4049 	  and jut.qual10_value1 is null
4050   	  and jut.INTERFACE_TYPE = v_intf_type
4051 	  AND jut.ACTION_FLAG = v_action_flag
4052   	  and jut.header = v_header
4053       union all
4054       select terr_id, user_sequence,
4055 	  terr_qual_id11 terr_qual_id,
4056       11 qual_num, QUAL11_VALUE_ID qual_value_id
4057       FROM JTY_WEBADI_OTH_TERR_INTF jut
4058       where jut.USER_SEQUENCE = v_user_sequence
4059   	  and jut.status is null
4060 	  and jut.terr_qual_id11 is not null
4061 	  and jut.QUAL11_VALUE_ID is not null
4062 	  and jut.qual11_value1 is null
4063   	  and jut.INTERFACE_TYPE = v_intf_type
4064 	  AND jut.ACTION_FLAG = v_action_flag
4065   	  and jut.header = v_header
4066       union all
4067       select terr_id, user_sequence,
4068 	  terr_qual_id12 terr_qual_id,
4069       12 qual_num, QUAL12_VALUE_ID qual_value_id
4070       FROM JTY_WEBADI_OTH_TERR_INTF jut
4071       where jut.USER_SEQUENCE = v_user_sequence
4072   	  and jut.status is null
4073 	  and jut.terr_qual_id12 is not null
4074 	  and jut.QUAL12_VALUE_ID is not null
4075 	  and jut.qual12_value1 is null
4076   	  and jut.INTERFACE_TYPE = v_intf_type
4077 	  AND jut.ACTION_FLAG = v_action_flag
4078   	  and jut.header = v_header
4079       union all
4080       select terr_id, user_sequence,
4081 	  terr_qual_id13 terr_qual_id,
4082       13 qual_num, QUAL13_VALUE_ID qual_value_id
4083       FROM JTY_WEBADI_OTH_TERR_INTF jut
4084       where jut.USER_SEQUENCE = v_user_sequence
4085   	  and jut.status is null
4086 	  and jut.terr_qual_id13 is not null
4087 	  and jut.QUAL13_VALUE_ID is not null
4088 	  and jut.qual13_value1 is null
4089   	  and jut.INTERFACE_TYPE = v_intf_type
4090 	  AND jut.ACTION_FLAG = v_action_flag
4091   	  and jut.header = v_header
4092       union all
4093       select terr_id, user_sequence,
4094 	  terr_qual_id14 terr_qual_id,
4095       14 qual_num, QUAL14_VALUE_ID qual_value_id
4096       FROM JTY_WEBADI_OTH_TERR_INTF jut
4097       where jut.USER_SEQUENCE = v_user_sequence
4098   	  and jut.status is null
4099 	  and jut.terr_qual_id14 is not null
4100 	  and jut.QUAL14_VALUE_ID is not null
4101 	  and jut.qual14_value1 is null
4102   	  and jut.INTERFACE_TYPE = v_intf_type
4103 	  AND jut.ACTION_FLAG = v_action_flag
4104   	  and jut.header = v_header
4105       union all
4106       select terr_id, user_sequence,
4107 	  terr_qual_id15 terr_qual_id,
4108       15 qual_num, QUAL15_VALUE_ID qual_value_id
4109       FROM JTY_WEBADI_OTH_TERR_INTF jut
4110       where jut.USER_SEQUENCE = v_user_sequence
4111   	  and jut.status is null
4112 	  and jut.terr_qual_id15 is not null
4113 	  and jut.QUAL15_VALUE_ID is not null
4114 	  and jut.qual15_value1 is null
4115   	  and jut.INTERFACE_TYPE = v_intf_type
4116 	  AND jut.ACTION_FLAG = v_action_flag
4117   	  and jut.header = v_header
4118       union all
4119       select terr_id, user_sequence,
4120 	  terr_qual_id16 terr_qual_id,
4121       16 qual_num, QUAL16_VALUE_ID qual_value_id
4122       FROM JTY_WEBADI_OTH_TERR_INTF jut
4123       where jut.USER_SEQUENCE = v_user_sequence
4124   	  and jut.status is null
4125 	  and jut.terr_qual_id16 is not null
4126 	  and jut.QUAL16_VALUE_ID is not null
4127 	  and jut.qual16_value1 is null
4128   	  and jut.INTERFACE_TYPE = v_intf_type
4129 	  AND jut.ACTION_FLAG = v_action_flag
4130   	  and jut.header = v_header
4131       union all
4132       select terr_id, user_sequence,
4133 	  terr_qual_id17 terr_qual_id,
4134       17 qual_num, QUAL17_VALUE_ID qual_value_id
4135       FROM JTY_WEBADI_OTH_TERR_INTF jut
4136       where jut.USER_SEQUENCE = v_user_sequence
4137   	  and jut.status is null
4138 	  and jut.terr_qual_id17 is not null
4139 	  and jut.QUAL17_VALUE_ID is not null
4140 	  and jut.qual17_value1 is null
4141   	  and jut.INTERFACE_TYPE = v_intf_type
4142 	  AND jut.ACTION_FLAG = v_action_flag
4143   	  and jut.header = v_header
4144       union all
4145       select terr_id, user_sequence,
4146 	  terr_qual_id18 terr_qual_id,
4147       18 qual_num, QUAL18_VALUE_ID qual_value_id
4148       FROM JTY_WEBADI_OTH_TERR_INTF jut
4149       where jut.USER_SEQUENCE = v_user_sequence
4150   	  and jut.status is null
4151 	  and jut.terr_qual_id18 is not null
4152 	  and jut.QUAL18_VALUE_ID is not null
4153 	  and jut.qual18_value1 is null
4154   	  and jut.INTERFACE_TYPE = v_intf_type
4155 	  AND jut.ACTION_FLAG = v_action_flag
4156   	  and jut.header = v_header
4157       union all
4158       select terr_id, user_sequence,
4159 	  terr_qual_id19 terr_qual_id,
4160       19 qual_num, QUAL19_VALUE_ID qual_value_id
4161       FROM JTY_WEBADI_OTH_TERR_INTF jut
4162       where jut.USER_SEQUENCE = v_user_sequence
4163   	  and jut.status is null
4164 	  and jut.terr_qual_id19 is not null
4165 	  and jut.QUAL19_VALUE_ID is not null
4166 	  and jut.qual19_value1 is null
4167   	  and jut.INTERFACE_TYPE = v_intf_type
4171       select terr_id, user_sequence,
4168 	  AND jut.ACTION_FLAG = v_action_flag
4169   	  and jut.header = v_header
4170       union all
4172 	  terr_qual_id20 terr_qual_id,
4173       20 qual_num, QUAL20_VALUE_ID qual_value_id
4174       FROM JTY_WEBADI_OTH_TERR_INTF jut
4175       where jut.USER_SEQUENCE = v_user_sequence
4176   	  and jut.status is null
4177 	  and jut.terr_qual_id20 is not null
4178 	  and jut.QUAL20_VALUE_ID is not null
4179 	  and jut.qual20_value1 is null
4180   	  and jut.INTERFACE_TYPE = v_intf_type
4181 	  AND jut.ACTION_FLAG = v_action_flag
4182   	  and jut.header = v_header
4183       union all
4184       select terr_id, user_sequence,
4185 	  terr_qual_id21 terr_qual_id,
4186       21 qual_num, QUAL21_VALUE_ID qual_value_id
4187       FROM JTY_WEBADI_OTH_TERR_INTF jut
4188       where jut.USER_SEQUENCE = v_user_sequence
4189   	  and jut.status is null
4190 	  and jut.terr_qual_id21 is not null
4191 	  and jut.QUAL21_VALUE_ID is not null
4192 	  and jut.qual21_value1 is null
4193   	  and jut.INTERFACE_TYPE = v_intf_type
4194 	  AND jut.ACTION_FLAG = v_action_flag
4195   	  and jut.header = v_header
4196       union all
4197       select terr_id, user_sequence,
4198 	  terr_qual_id22 terr_qual_id,
4199       22 qual_num, QUAL22_VALUE_ID qual_value_id
4200       FROM JTY_WEBADI_OTH_TERR_INTF jut
4201       where jut.USER_SEQUENCE = v_user_sequence
4202   	  and jut.status is null
4203 	  and jut.terr_qual_id22 is not null
4204 	  and jut.QUAL22_VALUE_ID is not null
4205 	  and jut.qual22_value1 is null
4206   	  and jut.INTERFACE_TYPE = v_intf_type
4207 	  AND jut.ACTION_FLAG = v_action_flag
4208   	  and jut.header = v_header
4209       union all
4210       select terr_id, user_sequence,
4211 	  terr_qual_id23 terr_qual_id,
4212       23 qual_num, QUAL23_VALUE_ID qual_value_id
4213       FROM JTY_WEBADI_OTH_TERR_INTF jut
4214       where jut.USER_SEQUENCE = v_user_sequence
4215   	  and jut.status is null
4216 	  and jut.terr_qual_id23 is not null
4217 	  and jut.QUAL23_VALUE_ID is not null
4218 	  and jut.qual23_value1 is null
4219   	  and jut.INTERFACE_TYPE = v_intf_type
4220 	  AND jut.ACTION_FLAG = v_action_flag
4221   	  and jut.header = v_header
4222       union all
4223       select terr_id, user_sequence,
4224 	  terr_qual_id24 terr_qual_id,
4225       24 qual_num, QUAL24_VALUE_ID qual_value_id
4226       FROM JTY_WEBADI_OTH_TERR_INTF jut
4227       where jut.USER_SEQUENCE = v_user_sequence
4228   	  and jut.status is null
4229 	  and jut.terr_qual_id24 is not null
4230 	  and jut.QUAL24_VALUE_ID is not null
4231 	  and jut.qual24_value1 is null
4232   	  and jut.INTERFACE_TYPE = v_intf_type
4233 	  AND jut.ACTION_FLAG = v_action_flag
4234   	  and jut.header = v_header
4235       union all
4236       select terr_id, user_sequence,
4237 	  terr_qual_id25 terr_qual_id,
4238       25 qual_num, QUAL25_VALUE_ID qual_value_id
4239       FROM JTY_WEBADI_OTH_TERR_INTF jut
4240       where jut.USER_SEQUENCE = v_user_sequence
4241   	  and jut.status is null
4242 	  and jut.terr_qual_id25 is not null
4243 	  and jut.QUAL25_VALUE_ID is not null
4244 	  and jut.qual25_value1 is null
4245   	  and jut.INTERFACE_TYPE = v_intf_type
4246 	  AND jut.ACTION_FLAG = v_action_flag
4247   	  and jut.header = v_header) sub;
4248 
4249 	l_Terr_Qual_Rec		Terr_Qual_Rec_Type;
4250 	l_terr_values_rec 	Terr_values_rec_type;
4251 	l_terr_values_out_rec Terr_values_out_rec_type;
4252 
4253     l_intf_type VARCHAR2(1) := 'U';
4254 	l_header	varchar2(15)  := 'QUAL';
4255 	l_action_flag varchar2(1);
4256 
4257 BEGIN
4258   x_return_status := FND_API.G_RET_STS_SUCCESS;
4259   x_msg_data := NULL;
4260 
4261   --l_action_flag := p_action_flag;
4262   l_action_flag := 'C';
4263   open get_qual_csr(P_USER_SEQUENCE, l_action_flag, l_intf_type, l_header);
4264   fetch get_qual_csr bulk collect into
4265 	l_Terr_Qual_Rec.TERR_QUAL_ID, l_Terr_Qual_Rec.terr_id,
4266 	l_Terr_Qual_Rec.qual_value_id, l_Terr_Qual_Rec.qual_value1,
4267 	l_Terr_Qual_Rec.qual_value2, l_Terr_Qual_Rec.qual_value3,
4268   	l_Terr_Qual_Rec.org_id, l_Terr_Qual_Rec.last_updated_by,
4269 	l_Terr_Qual_Rec.last_update_date, l_Terr_Qual_Rec.last_update_login,
4270 	l_Terr_Qual_Rec.creation_date, l_Terr_Qual_Rec.created_by,
4271   	l_Terr_Qual_Rec.qual_usg_id, l_Terr_Qual_Rec.qual_type,
4272 	l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG, l_Terr_Qual_Rec.qualifier_num,
4273 	l_Terr_Qual_Rec.html_lov_sql1, l_Terr_Qual_Rec.qual_cond;
4274   close get_qual_csr;
4275 
4276   if (l_action_flag = 'C' AND l_Terr_Qual_Rec.TERR_ID.count > 0) then
4277 
4278     INSERT_TERR_QUAL (
4279       p_Terr_Qual_Rec	=> l_Terr_Qual_Rec,
4280    	  x_return_status	=> x_return_status,
4281       x_msg_data		=> x_msg_data);
4282 
4283     if (x_return_status = FND_API.G_RET_STS_SUCCESS) then
4284       INSERT_TERR_VALUES(
4285      	p_Terr_Qual_Rec	=> l_terr_qual_rec,
4286      	p_terr_values_out_rec => l_terr_values_out_rec,
4287      	x_return_status		  => x_return_status,
4288      	x_msg_data			  => x_msg_data);
4289     end if;
4290 
4291     forall i in l_Terr_Qual_Rec.TERR_ID.first..l_Terr_Qual_Rec.TERR_ID.last
4292 	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4293 	  set
4294 	  jwot.terr_qual_id1 = decode(l_Terr_Qual_Rec.qualifier_num(i),1,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id1),
4295 	  jwot.terr_qual_id2 = decode(l_Terr_Qual_Rec.qualifier_num(i),2,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id2),
4299 	  jwot.terr_qual_id6 = decode(l_Terr_Qual_Rec.qualifier_num(i),6,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id6),
4296 	  jwot.terr_qual_id3 = decode(l_Terr_Qual_Rec.qualifier_num(i),3,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id3),
4297 	  jwot.terr_qual_id4 = decode(l_Terr_Qual_Rec.qualifier_num(i),4,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id4),
4298 	  jwot.terr_qual_id5 = decode(l_Terr_Qual_Rec.qualifier_num(i),5,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id5),
4300 	  jwot.terr_qual_id7 = decode(l_Terr_Qual_Rec.qualifier_num(i),7,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id7),
4301 	  jwot.terr_qual_id8 = decode(l_Terr_Qual_Rec.qualifier_num(i),8,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id8),
4302 	  jwot.terr_qual_id9 = decode(l_Terr_Qual_Rec.qualifier_num(i),9,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id9),
4303 	  jwot.terr_qual_id10 = decode(l_Terr_Qual_Rec.qualifier_num(i),10,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id10),
4304 	  jwot.terr_qual_id11 = decode(l_Terr_Qual_Rec.qualifier_num(i),11,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id11),
4305 	  jwot.terr_qual_id12 = decode(l_Terr_Qual_Rec.qualifier_num(i),12,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id12),
4306 	  jwot.terr_qual_id13 = decode(l_Terr_Qual_Rec.qualifier_num(i),13,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id13),
4307 	  jwot.terr_qual_id14 = decode(l_Terr_Qual_Rec.qualifier_num(i),14,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id14),
4308 	  jwot.terr_qual_id15 = decode(l_Terr_Qual_Rec.qualifier_num(i),15,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id15),
4309 	  jwot.terr_qual_id16 = decode(l_Terr_Qual_Rec.qualifier_num(i),16,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id16),
4310 	  jwot.terr_qual_id17 = decode(l_Terr_Qual_Rec.qualifier_num(i),17,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id17),
4311 	  jwot.terr_qual_id18 = decode(l_Terr_Qual_Rec.qualifier_num(i),18,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id18),
4312 	  jwot.terr_qual_id19 = decode(l_Terr_Qual_Rec.qualifier_num(i),19,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id19),
4313 	  jwot.terr_qual_id20 = decode(l_Terr_Qual_Rec.qualifier_num(i),20,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id20),
4314 	  jwot.terr_qual_id21 = decode(l_Terr_Qual_Rec.qualifier_num(i),21,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id21),
4315 	  jwot.terr_qual_id22 = decode(l_Terr_Qual_Rec.qualifier_num(i),22,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id22),
4316 	  jwot.terr_qual_id23 = decode(l_Terr_Qual_Rec.qualifier_num(i),23,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id23),
4317 	  jwot.terr_qual_id24 = decode(l_Terr_Qual_Rec.qualifier_num(i),24,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id24),
4318 	  jwot.terr_qual_id25 = decode(l_Terr_Qual_Rec.qualifier_num(i),25,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id25)
4319 	  where TERR_ID = l_Terr_Qual_Rec.TERR_ID(i)
4320 	    and user_sequence = p_user_sequence
4321 		and header = l_header
4322 		and interface_type = l_intf_type;
4323 
4324 	if x_return_status = FND_API.G_RET_STS_ERROR then
4325     forall i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last
4326 	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4327 	  SET STATUS = 	x_return_status,
4328 	  ERROR_MSG = x_msg_data
4329 	  WHERE l_Terr_Qual_Rec.TERR_QUAL_ID(i) in
4330 	  		( jwot.TERR_QUAL_ID1, jwot.TERR_QUAL_ID2, jwot.TERR_QUAL_ID3,
4331 			  jwot.TERR_QUAL_ID4, jwot.TERR_QUAL_ID5, jwot.TERR_QUAL_ID6,
4332 			  jwot.TERR_QUAL_ID7, jwot.TERR_QUAL_ID8, jwot.TERR_QUAL_ID9,
4333 			  jwot.TERR_QUAL_ID10, jwot.TERR_QUAL_ID11, jwot.TERR_QUAL_ID12,
4334 			  jwot.TERR_QUAL_ID13, jwot.TERR_QUAL_ID14, jwot.TERR_QUAL_ID15,
4335 			  jwot.TERR_QUAL_ID16, jwot.TERR_QUAL_ID17, jwot.TERR_QUAL_ID18,
4336 			  jwot.TERR_QUAL_ID19, jwot.TERR_QUAL_ID20, jwot.TERR_QUAL_ID21,
4337 			  jwot.TERR_QUAL_ID22, jwot.TERR_QUAL_ID23,
4338 			  jwot.TERR_QUAL_ID24, jwot.TERR_QUAL_ID25)
4339 		AND interface_type = l_intf_type
4340 		and header = l_header
4341 		and user_sequence = p_user_sequence;
4342 	end if;
4343 
4344   end if; -- terr_id count
4345   l_action_flag := 'U';
4346 
4347   x_return_status := FND_API.G_RET_STS_SUCCESS;
4348   open get_qual_csr(P_USER_SEQUENCE, l_action_flag, l_intf_type, l_header);
4349   fetch get_qual_csr bulk collect into
4350 	l_Terr_Qual_Rec.TERR_QUAL_ID, l_Terr_Qual_Rec.terr_id,
4351 	l_Terr_Qual_Rec.qual_value_id, l_Terr_Qual_Rec.qual_value1,
4352 	l_Terr_Qual_Rec.qual_value2, l_Terr_Qual_Rec.qual_value3,
4353   	l_Terr_Qual_Rec.org_id, l_Terr_Qual_Rec.last_updated_by,
4354 	l_Terr_Qual_Rec.last_update_date, l_Terr_Qual_Rec.last_update_login,
4355 	l_Terr_Qual_Rec.creation_date, l_Terr_Qual_Rec.created_by,
4356   	l_Terr_Qual_Rec.qual_usg_id, l_Terr_Qual_Rec.qual_type,
4357 	l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG, l_Terr_Qual_Rec.qualifier_num,
4358 	l_Terr_Qual_Rec.html_lov_sql1, l_Terr_Qual_Rec.qual_cond;
4359   close get_qual_csr;
4360 
4361   if (l_action_flag = 'U' AND l_Terr_Qual_Rec.TERR_ID.count > 0) then
4362 
4363     INSERT_TERR_QUAL (
4364       p_Terr_Qual_Rec	=> l_Terr_Qual_Rec,
4365    	  x_return_status	=> x_return_status,
4366       x_msg_data		=> x_msg_data);
4367 
4368     if (x_return_status = FND_API.G_RET_STS_SUCCESS) then
4369       INSERT_TERR_VALUES(
4370      	p_Terr_Qual_Rec	  	  => l_terr_qual_rec,
4371      	p_terr_values_out_rec => l_terr_values_out_rec,
4372      	x_return_status		  => x_return_status,
4373      	x_msg_data			  => x_msg_data);
4374     end if;
4375 
4376     forall i in l_Terr_Qual_Rec.TERR_ID.first..l_Terr_Qual_Rec.TERR_ID.last
4377 	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4378 	  set
4379 	  jwot.terr_qual_id1 = decode(l_Terr_Qual_Rec.qualifier_num(i),1,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id1),
4383 	  jwot.terr_qual_id5 = decode(l_Terr_Qual_Rec.qualifier_num(i),5,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id5),
4380 	  jwot.terr_qual_id2 = decode(l_Terr_Qual_Rec.qualifier_num(i),2,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id2),
4381 	  jwot.terr_qual_id3 = decode(l_Terr_Qual_Rec.qualifier_num(i),3,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id3),
4382 	  jwot.terr_qual_id4 = decode(l_Terr_Qual_Rec.qualifier_num(i),4,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id4),
4384 	  jwot.terr_qual_id6 = decode(l_Terr_Qual_Rec.qualifier_num(i),6,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id6),
4385 	  jwot.terr_qual_id7 = decode(l_Terr_Qual_Rec.qualifier_num(i),7,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id7),
4386 	  jwot.terr_qual_id8 = decode(l_Terr_Qual_Rec.qualifier_num(i),8,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id8),
4387 	  jwot.terr_qual_id9 = decode(l_Terr_Qual_Rec.qualifier_num(i),9,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id9),
4388 	  jwot.terr_qual_id10 = decode(l_Terr_Qual_Rec.qualifier_num(i),10,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id10),
4389 	  jwot.terr_qual_id11 = decode(l_Terr_Qual_Rec.qualifier_num(i),11,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id11),
4390 	  jwot.terr_qual_id12 = decode(l_Terr_Qual_Rec.qualifier_num(i),12,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id12),
4391 	  jwot.terr_qual_id13 = decode(l_Terr_Qual_Rec.qualifier_num(i),13,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id13),
4392 	  jwot.terr_qual_id14 = decode(l_Terr_Qual_Rec.qualifier_num(i),14,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id14),
4393 	  jwot.terr_qual_id15 = decode(l_Terr_Qual_Rec.qualifier_num(i),15,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id15),
4394 	  jwot.terr_qual_id16 = decode(l_Terr_Qual_Rec.qualifier_num(i),16,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id16),
4395 	  jwot.terr_qual_id17 = decode(l_Terr_Qual_Rec.qualifier_num(i),17,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id17),
4396 	  jwot.terr_qual_id18 = decode(l_Terr_Qual_Rec.qualifier_num(i),18,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id18),
4397 	  jwot.terr_qual_id19 = decode(l_Terr_Qual_Rec.qualifier_num(i),19,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id19),
4398 	  jwot.terr_qual_id20 = decode(l_Terr_Qual_Rec.qualifier_num(i),20,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id20),
4399 	  jwot.terr_qual_id21 = decode(l_Terr_Qual_Rec.qualifier_num(i),21,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id21),
4400 	  jwot.terr_qual_id22 = decode(l_Terr_Qual_Rec.qualifier_num(i),22,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id22),
4401 	  jwot.terr_qual_id23 = decode(l_Terr_Qual_Rec.qualifier_num(i),23,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id23),
4402 	  jwot.terr_qual_id24 = decode(l_Terr_Qual_Rec.qualifier_num(i),24,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id24),
4403 	  jwot.terr_qual_id25 = decode(l_Terr_Qual_Rec.qualifier_num(i),25,l_Terr_Qual_Rec.TERR_QUAL_ID(i),jwot.terr_qual_id25)
4404 	  where TERR_ID = l_Terr_Qual_Rec.TERR_ID(i)
4405 	    and user_sequence = p_user_sequence
4406 		and header = l_header
4407 		and interface_type = l_intf_type;
4408 
4409 	if x_return_status = FND_API.G_RET_STS_ERROR then
4410     forall i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last
4411 	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4412 	  SET STATUS = 	x_return_status,
4413 	  ERROR_MSG = x_msg_data
4414 	  WHERE l_Terr_Qual_Rec.TERR_QUAL_ID(i) in
4415 	  		( jwot.TERR_QUAL_ID1, jwot.TERR_QUAL_ID2, jwot.TERR_QUAL_ID3,
4416 			  jwot.TERR_QUAL_ID4, jwot.TERR_QUAL_ID5, jwot.TERR_QUAL_ID6,
4417 			  jwot.TERR_QUAL_ID7, jwot.TERR_QUAL_ID8, jwot.TERR_QUAL_ID9,
4418 			  jwot.TERR_QUAL_ID10, jwot.TERR_QUAL_ID11, jwot.TERR_QUAL_ID12,
4419 			  jwot.TERR_QUAL_ID13, jwot.TERR_QUAL_ID14, jwot.TERR_QUAL_ID15,
4420 			  jwot.TERR_QUAL_ID16, jwot.TERR_QUAL_ID17, jwot.TERR_QUAL_ID18,
4421 			  jwot.TERR_QUAL_ID19, jwot.TERR_QUAL_ID20, jwot.TERR_QUAL_ID21,
4422 			  jwot.TERR_QUAL_ID22, jwot.TERR_QUAL_ID23,
4423 			  jwot.TERR_QUAL_ID24, jwot.TERR_QUAL_ID25)
4424 		AND interface_type = l_intf_type
4425 		and header = l_header
4426 		and user_sequence = p_user_sequence;
4427 	end if;
4428   end if; -- terr_id count
4429 
4430   x_return_status := FND_API.G_RET_STS_SUCCESS;
4431   open get_c_terr_value_csr(P_USER_SEQUENCE, l_intf_type, l_header);
4432   fetch get_c_terr_value_csr bulk collect into
4433 	l_Terr_Qual_Rec.TERR_QUAL_ID, l_Terr_Qual_Rec.terr_id,
4434 	l_Terr_Qual_Rec.qual_value_id, l_Terr_Qual_Rec.qual_value1,
4435 	l_Terr_Qual_Rec.qual_value2, l_Terr_Qual_Rec.qual_value3,
4436   	l_Terr_Qual_Rec.org_id, l_Terr_Qual_Rec.last_updated_by,
4437 	l_Terr_Qual_Rec.last_update_date, l_Terr_Qual_Rec.last_update_login,
4438 	l_Terr_Qual_Rec.creation_date, l_Terr_Qual_Rec.created_by,
4439   	l_Terr_Qual_Rec.qual_usg_id, l_Terr_Qual_Rec.qual_type,
4440 	l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG, l_Terr_Qual_Rec.qualifier_num,
4441 	l_Terr_Qual_Rec.html_lov_sql1, l_Terr_Qual_Rec.qual_cond;
4442   close get_c_terr_value_csr;
4443 
4444   if (l_Terr_Qual_Rec.TERR_QUAL_ID.count > 0) then
4445     INSERT_TERR_VALUES(
4446 	p_Terr_Qual_Rec	=> l_terr_qual_rec,
4447 	p_terr_values_out_rec => l_terr_values_out_rec,
4448 	x_return_status		  => x_return_status,
4449 	x_msg_data			  => x_msg_data);
4450 
4451 	if x_return_status = FND_API.G_RET_STS_ERROR then
4452 	forall i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last
4453 	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4454 	  SET STATUS = 	x_return_status,
4455 	  ERROR_MSG = x_msg_data
4456 	  WHERE l_Terr_Qual_Rec.TERR_QUAL_ID(i) in
4457 	  		( jwot.TERR_QUAL_ID1, jwot.TERR_QUAL_ID2, jwot.TERR_QUAL_ID3,
4458 			  jwot.TERR_QUAL_ID4, jwot.TERR_QUAL_ID5, jwot.TERR_QUAL_ID6,
4459 			  jwot.TERR_QUAL_ID7, jwot.TERR_QUAL_ID8, jwot.TERR_QUAL_ID9,
4460 			  jwot.TERR_QUAL_ID10, jwot.TERR_QUAL_ID11, jwot.TERR_QUAL_ID12,
4461 			  jwot.TERR_QUAL_ID13, jwot.TERR_QUAL_ID14, jwot.TERR_QUAL_ID15,
4465 			  jwot.TERR_QUAL_ID24, jwot.TERR_QUAL_ID25)
4462 			  jwot.TERR_QUAL_ID16, jwot.TERR_QUAL_ID17, jwot.TERR_QUAL_ID18,
4463 			  jwot.TERR_QUAL_ID19, jwot.TERR_QUAL_ID20, jwot.TERR_QUAL_ID21,
4464 			  jwot.TERR_QUAL_ID22, jwot.TERR_QUAL_ID23,
4466 		AND interface_type = l_intf_type
4467 		and header = l_header
4468 		and user_sequence = p_user_sequence;
4469 	end if;
4470   end if; --get_c_terr_value_csr
4471 
4472   open get_u_terr_value_csr(P_USER_SEQUENCE, l_action_flag, l_intf_type, l_header);
4473   fetch get_u_terr_value_csr bulk collect into
4474 	l_Terr_Qual_Rec.TERR_QUAL_ID, l_Terr_Qual_Rec.terr_id,
4475 	l_Terr_Qual_Rec.qual_value_id, l_Terr_Qual_Rec.qual_value1,
4476 	l_Terr_Qual_Rec.qual_value2, l_Terr_Qual_Rec.qual_value3,
4477   	l_Terr_Qual_Rec.org_id, l_Terr_Qual_Rec.last_updated_by,
4478 	l_Terr_Qual_Rec.last_update_date, l_Terr_Qual_Rec.last_update_login,
4479 	l_Terr_Qual_Rec.creation_date, l_Terr_Qual_Rec.created_by,
4480   	l_Terr_Qual_Rec.qual_usg_id, l_Terr_Qual_Rec.qual_type,
4481 	l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG, l_Terr_Qual_Rec.qualifier_num,
4482 	l_Terr_Qual_Rec.html_lov_sql1, l_Terr_Qual_Rec.qual_cond;
4483   close get_u_terr_value_csr;
4484 
4485   if (l_Terr_Qual_Rec.TERR_QUAL_ID.count > 0) then
4486   BEGIN
4487     --dbms_output.put_line('U: get_u_terr_value_csr: update TV, rowcount: ' || l_Terr_Qual_Rec.TERR_QUAL_ID.count);
4488 	x_return_status := FND_API.G_RET_STS_SUCCESS;
4489     for i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last loop
4490 
4491 	   l_Terr_Values_Rec.LAST_UPDATE_DATE(i)  := l_Terr_Qual_Rec.LAST_UPDATE_DATE(i);
4492 	   l_Terr_Values_Rec.LAST_UPDATED_BY(i)   := l_Terr_Qual_Rec.LAST_UPDATED_BY(i);
4493 	   l_Terr_Values_Rec.CREATION_DATE(i) 	  := l_Terr_Qual_Rec.CREATION_DATE(i);
4494 	   l_Terr_Values_Rec.CREATED_BY(i)    	  := l_Terr_Qual_Rec.CREATED_BY(i);
4495 	   l_Terr_Values_Rec.LAST_UPDATE_LOGIN(i) := l_Terr_Qual_Rec.LAST_UPDATE_LOGIN(i);
4496 	   l_Terr_Values_Rec.TERR_QUAL_ID(i)  	  := l_Terr_Qual_Rec.TERR_QUAL_ID(i);
4497 	   l_Terr_Values_Rec.COMPARISON_OPERATOR(i) := l_Terr_Qual_Rec.qual_cond(i);
4498        l_Terr_Values_Rec.ID_USED_FLAG(i)        := l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG(i);
4499 	   l_Terr_Values_Rec.ORG_ID(i)              := l_Terr_Qual_Rec.ORG_ID(i);
4500 	   l_Terr_Values_Rec.TERR_VALUE_ID(i) := l_Terr_Qual_Rec.qual_value_id(i);
4501         l_Terr_Values_Rec.LOW_VALUE_CHAR(i):= NULL;
4502         l_Terr_Values_Rec.HIGH_VALUE_CHAR(i):= NULL;
4503         l_Terr_Values_Rec.LOW_VALUE_NUMBER(i):= NULL;
4504         l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i):= NULL;
4505         l_Terr_Values_Rec.INTEREST_TYPE_ID(i):= NULL;
4506         l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i):= NULL;
4507         l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i):= NULL;
4508         l_Terr_Values_Rec.CURRENCY_CODE(i):= NULL;
4509         l_Terr_Values_Rec.ID_USED_FLAG(i):= NULL;
4510         l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i):= NULL;
4511         --l_Terr_Values_Rec.ORG_ID(i):= NULL;
4512         l_Terr_Values_Rec.VALUE1_ID(i):= NULL;
4513         l_Terr_Values_Rec.VALUE2_ID(i):= NULL;
4514         l_Terr_Values_Rec.VALUE3_ID(i):= NULL;
4515 	    case
4516 	     when l_Terr_Qual_Rec.qual_type(i) = 'CHAR' then
4517 		   IF l_Terr_Qual_Rec.CONVERT_TO_ID_FLAG(i) = 'N' then
4518 		     l_Terr_Values_Rec.LOW_VALUE_CHAR(i) := l_Terr_Qual_Rec.qual_value1(i);
4519 			 if l_Terr_Qual_Rec.qual_value2.count > 0 then
4520 		       l_Terr_Values_Rec.HIGH_VALUE_CHAR(i) := l_Terr_Qual_Rec.qual_value2(i);
4521 			 else l_Terr_Values_Rec.HIGH_VALUE_CHAR(i) := NULL;
4522 			 end if;
4523 		   else
4524 		     l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
4525 		   end if;
4526          when l_Terr_Qual_Rec.qual_type(i) = 'DEP_2FIELDS_1CHAR_1ID' then
4527 		   l_Terr_Values_Rec.LOW_VALUE_CHAR(i) := l_Terr_Qual_Rec.qual_value1(i);
4528 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
4529 		     l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
4530 		   else l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i) := NULL;
4531 		   end if;
4532          when l_Terr_Qual_Rec.qual_type(i) in
4533            ('CHAR_2IDS', 'DEP_2FIELDS', 'DEP_2FIELDS_CHAR_2IDS') then
4534 		   l_Terr_Values_Rec.VALUE1_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
4535 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
4536 		     l_Terr_Values_Rec.VALUE2_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
4537 		   else l_Terr_Values_Rec.VALUE2_ID(i) := NULL;
4538 		   end if;
4539          when l_Terr_Qual_Rec.qual_type(i) = 'DEP_3FIELDS_CHAR_3IDS' then
4540 		   l_Terr_Values_Rec.VALUE1_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
4541 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
4542 		     l_Terr_Values_Rec.VALUE2_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
4543 		   else l_Terr_Values_Rec.VALUE2_ID(i) := NULL;
4544 		   end if;
4545 		   if l_Terr_Qual_Rec.qual_value3.count > 0 then
4546 		     l_Terr_Values_Rec.VALUE3_ID(i) := l_Terr_Qual_Rec.qual_value3(i);
4547 		   else l_Terr_Values_Rec.VALUE3_ID(i) := NULL;
4548 		   end if;
4549          when l_Terr_Qual_Rec.qual_type(i) = 'INTEREST_TYPE' then
4550 		   l_Terr_Values_Rec.INTEREST_TYPE_ID(i) := l_Terr_Qual_Rec.qual_value1(i);
4551 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
4552 		     l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i) := l_Terr_Qual_Rec.qual_value2(i);
4553 		   else l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i) := NULL;
4554 		   end if;
4555 		   if l_Terr_Qual_Rec.qual_value3.count > 0 then
4556 		     l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i) := l_Terr_Qual_Rec.qual_value3(i);
4557 		   else l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i) := NULL;
4558 		   end if;
4559          when l_Terr_Qual_Rec.qual_type(i) = 'NUMERIC' then
4563 		   else l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := NULL;
4560 		   l_Terr_Values_Rec.LOW_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value1(i);
4561 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
4562 		     l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value2(i);
4564 		   end if;
4565          when l_Terr_Qual_Rec.qual_type(i) = 'CURRENCY' then
4566 		   l_Terr_Values_Rec.LOW_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value1(i);
4567 		   if l_Terr_Qual_Rec.qual_value2.count > 0 then
4568 		     l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := l_Terr_Qual_Rec.qual_value2(i);
4569 		   else l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i) := NULL;
4570 		   end if;
4571 		   if l_Terr_Qual_Rec.qual_value3.count > 0 then
4572 		     l_Terr_Values_Rec.CURRENCY_CODE(i) := l_Terr_Qual_Rec.qual_value3(i);
4573 		   else l_Terr_Values_Rec.CURRENCY_CODE(i) := NULL;
4574 		   end if;
4575          else null;
4576 	   end case;
4577 	end loop;
4578 
4579 	forall i in l_Terr_Values_Rec.TERR_VALUE_ID.first..l_Terr_Values_Rec.TERR_VALUE_ID.last
4580       Update JTF_TERR_VALUES_ALL
4581       SET
4582         LAST_UPDATED_BY = l_Terr_Values_Rec.LAST_UPDATED_BY(i),
4583         LAST_UPDATE_DATE = l_Terr_Values_Rec.LAST_UPDATE_DATE(i),
4584         LAST_UPDATE_LOGIN = l_Terr_Values_Rec.LAST_UPDATE_LOGIN(i),
4585         TERR_QUAL_ID = l_Terr_Values_Rec.TERR_QUAL_ID(i),
4586         COMPARISON_OPERATOR = l_Terr_Values_Rec.COMPARISON_OPERATOR(i),
4587         LOW_VALUE_CHAR = l_Terr_Values_Rec.LOW_VALUE_CHAR(i),
4588         HIGH_VALUE_CHAR = l_Terr_Values_Rec.HIGH_VALUE_CHAR(i),
4589         LOW_VALUE_NUMBER = l_Terr_Values_Rec.LOW_VALUE_NUMBER(i),
4590         HIGH_VALUE_NUMBER = l_Terr_Values_Rec.HIGH_VALUE_NUMBER(i),
4591         INTEREST_TYPE_ID = l_Terr_Values_Rec.INTEREST_TYPE_ID(i),
4592         PRIMARY_INTEREST_CODE_ID = l_Terr_Values_Rec.PRIMARY_INTEREST_CODE_ID(i),
4593         SECONDARY_INTEREST_CODE_ID = l_Terr_Values_Rec.SECONDARY_INTEREST_CODE_ID(i),
4594         CURRENCY_CODE = l_Terr_Values_Rec.CURRENCY_CODE(i),
4595         ID_USED_FLAG = l_Terr_Values_Rec.ID_USED_FLAG(i),
4596         LOW_VALUE_CHAR_ID = l_Terr_Values_Rec.LOW_VALUE_CHAR_ID(i),
4597         ORG_ID = l_Terr_Values_Rec.ORG_ID(i),
4598         VALUE1_ID = l_Terr_Values_Rec.VALUE1_ID(i),
4599         VALUE2_ID = l_Terr_Values_Rec.VALUE2_ID(i),
4600         VALUE3_ID = l_Terr_Values_Rec.VALUE3_ID(i)
4601       where TERR_VALUE_ID = l_Terr_Values_Rec.TERR_VALUE_ID(i);
4602 	--dbms_output.put_line(' U: get_u_terr_value_csr:TV, actual row processed:  '||SQL%ROWCOUNT);
4603 
4604 	EXCEPTION
4605 	  WHEN OTHERS THEN
4606         x_return_status := FND_API.G_RET_STS_ERROR;
4607 		fnd_message.clear;
4608         fnd_message.set_name ('JTF', 'JTY_OTH_TERR_UPDATE_QUAL_VAL');
4609         X_Msg_Data := fnd_message.get();
4610 
4611     	forall i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last
4612     	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4613     	  SET STATUS = x_return_status,
4614 		  error_msg = 'U: ' || x_msg_data
4615     	  WHERE l_Terr_Qual_Rec.TERR_QUAL_ID(i) in
4616     	  		( jwot.TERR_QUAL_ID1, jwot.TERR_QUAL_ID2, jwot.TERR_QUAL_ID3,
4617     			  jwot.TERR_QUAL_ID4, jwot.TERR_QUAL_ID5, jwot.TERR_QUAL_ID6,
4618     			  jwot.TERR_QUAL_ID7, jwot.TERR_QUAL_ID8, jwot.TERR_QUAL_ID9,
4619     			  jwot.TERR_QUAL_ID10, jwot.TERR_QUAL_ID11, jwot.TERR_QUAL_ID12,
4620     			  jwot.TERR_QUAL_ID13, jwot.TERR_QUAL_ID14, jwot.TERR_QUAL_ID15,
4621     			  jwot.TERR_QUAL_ID16, jwot.TERR_QUAL_ID17, jwot.TERR_QUAL_ID18,
4622     			  jwot.TERR_QUAL_ID19, jwot.TERR_QUAL_ID20, jwot.TERR_QUAL_ID21,
4623     			  jwot.TERR_QUAL_ID22, jwot.TERR_QUAL_ID23,
4624     			  jwot.TERR_QUAL_ID24, jwot.TERR_QUAL_ID25)
4625     		AND interface_type = l_intf_type
4626     		and header = l_header
4627     		and action_flag = l_action_flag
4628     		and user_sequence = p_user_sequence;
4629 	END;
4630   end if; --get_u_terr_value_csr
4631 
4632   open get_d_terr_value_csr(P_USER_SEQUENCE, l_action_flag, l_intf_type, l_header);
4633   fetch get_d_terr_value_csr bulk collect into
4634 	l_Terr_Qual_Rec.TERR_QUAL_ID, l_Terr_Qual_Rec.terr_id,
4635 	l_Terr_Qual_Rec.qual_value_id, l_Terr_Qual_Rec.qualifier_num;
4636   close get_d_terr_value_csr;
4637 
4638   if (l_Terr_Qual_Rec.TERR_QUAL_ID.count > 0) then
4639   BEGIN
4640     --dbms_output.put_line('U: get_d_terr_value_csr: delete TV, rowcount: ' || l_Terr_Qual_Rec.TERR_QUAL_ID.count);
4641 	x_return_status := FND_API.G_RET_STS_SUCCESS;
4642 	forall i in l_Terr_Qual_Rec.qual_value_id.first..l_Terr_Qual_Rec.qual_value_id.last
4643       DELETE FROM JTF_TERR_VALUES_ALL
4644       where TERR_VALUE_ID = l_Terr_Qual_Rec.qual_value_id(i);
4645 	--dbms_output.put_line(' U: get_d_terr_value_csr:actual row processed:  '||SQL%ROWCOUNT);
4646 
4647 	forall i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last
4648       DELETE FROM JTF_TERR_QUAL_ALL jtq
4649 	  WHERE not exists
4650 	  ( select 1 from JTF_TERR_VALUES_ALL jtv
4651 	    where jtv.terr_qual_id = jtq.terr_qual_id)
4652 	  and jtq.terr_qual_id = l_Terr_Qual_Rec.TERR_QUAL_ID(i);
4653 	--dbms_output.put_line(' U: get_d_terr_value_csr:TV, actual row processed:  '||SQL%ROWCOUNT);
4654 
4655 	forall i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last
4656 	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4657 	  SET STATUS = 	x_return_status
4658 	  WHERE l_Terr_Qual_Rec.TERR_QUAL_ID(i) in
4659 	  		( jwot.TERR_QUAL_ID1, jwot.TERR_QUAL_ID2, jwot.TERR_QUAL_ID3,
4660 			  jwot.TERR_QUAL_ID4, jwot.TERR_QUAL_ID5, jwot.TERR_QUAL_ID6,
4661 			  jwot.TERR_QUAL_ID7, jwot.TERR_QUAL_ID8, jwot.TERR_QUAL_ID9,
4665 			  jwot.TERR_QUAL_ID19, jwot.TERR_QUAL_ID20, jwot.TERR_QUAL_ID21,
4662 			  jwot.TERR_QUAL_ID10, jwot.TERR_QUAL_ID11, jwot.TERR_QUAL_ID12,
4663 			  jwot.TERR_QUAL_ID13, jwot.TERR_QUAL_ID14, jwot.TERR_QUAL_ID15,
4664 			  jwot.TERR_QUAL_ID16, jwot.TERR_QUAL_ID17, jwot.TERR_QUAL_ID18,
4666 			  jwot.TERR_QUAL_ID22, jwot.TERR_QUAL_ID23,
4667 			  jwot.TERR_QUAL_ID24, jwot.TERR_QUAL_ID25)
4668 		AND interface_type = l_intf_type
4669 		and header = l_header
4670 		and action_flag = l_action_flag
4671 		and user_sequence = p_user_sequence;
4672 
4673 	EXCEPTION
4674 	  WHEN OTHERS THEN
4675         x_return_status := FND_API.G_RET_STS_ERROR;
4676 		fnd_message.clear;
4677         fnd_message.set_name ('JTF', 'JTY_OTH_TERR_DELETE_QUAL_VAL');
4678         X_Msg_Data := fnd_message.get();
4679 
4680     	forall i in l_Terr_Qual_Rec.TERR_QUAL_ID.first..l_Terr_Qual_Rec.TERR_QUAL_ID.last
4681     	  UPDATE JTY_WEBADI_OTH_TERR_INTF jwot
4682     	  SET STATUS = x_return_status,
4683 		  error_msg = 'U: ' || x_msg_data
4684     	  WHERE l_Terr_Qual_Rec.TERR_QUAL_ID(i) in
4685     	  		( jwot.TERR_QUAL_ID1, jwot.TERR_QUAL_ID2, jwot.TERR_QUAL_ID3,
4686     			  jwot.TERR_QUAL_ID4, jwot.TERR_QUAL_ID5, jwot.TERR_QUAL_ID6,
4687     			  jwot.TERR_QUAL_ID7, jwot.TERR_QUAL_ID8, jwot.TERR_QUAL_ID9,
4688     			  jwot.TERR_QUAL_ID10, jwot.TERR_QUAL_ID11, jwot.TERR_QUAL_ID12,
4689     			  jwot.TERR_QUAL_ID13, jwot.TERR_QUAL_ID14, jwot.TERR_QUAL_ID15,
4690     			  jwot.TERR_QUAL_ID16, jwot.TERR_QUAL_ID17, jwot.TERR_QUAL_ID18,
4691     			  jwot.TERR_QUAL_ID19, jwot.TERR_QUAL_ID20, jwot.TERR_QUAL_ID21,
4692     			  jwot.TERR_QUAL_ID22, jwot.TERR_QUAL_ID23,
4693     			  jwot.TERR_QUAL_ID24, jwot.TERR_QUAL_ID25)
4694     		AND interface_type = l_intf_type
4695     		and header = l_header
4696     		and action_flag = l_action_flag
4697     		and user_sequence = p_user_sequence;
4698 	END;
4699   end if; --get_d_terr_value_csr
4700 
4701   update JTY_WEBADI_OTH_TERR_INTF
4702   set status = x_return_status
4703   where interface_type = l_intf_type
4704     and header = l_header
4705     and status is null
4706     and user_sequence = p_user_sequence;
4707 
4708 END UPDATE_TERR_QUAL;
4709 
4710 PROCEDURE UPDATE_TERR_RSC(
4711     P_USER_SEQUENCE 	  IN  NUMBER,
4712 	--p_action_flag		  IN VARCHAR2,
4713 	x_return_status		  out nocopy varchar2,
4714 	x_msg_data			  out nocopy varchar2
4715 ) IS
4716 
4717   cursor GET_RSC_CSR(
4718   v_user_sequence	 number,
4719   v_intf_type		 varchar2,
4720   v_header			 varchar2,
4721   v_action_flag		 varchar2) IS
4722   SELECT jwot.LAY_SEQ_NUM, jwr.TERR_RSC_ID,
4723     jwot.LAST_UPDATE_DATE, jwot.LAST_UPDATED_BY, jwot.CREATION_DATE,
4724     jwot.CREATED_BY, jwot.LAST_UPDATE_LOGIN,
4725     jwot.TERR_ID, jwr.resource_id,
4726     decode(jwr.resource_type,0, 'RS_'||res.category,
4727 	  --decode(res.category, 'PARTNER', 'RS_PARTNER', 'EMPLOYEE', 'RS_EMPLOYEE',NULL)
4728 	  1,'RS_GROUP',2,'RS_TEAM',3,'RS_ROLE', null) RESOURCE_TYPE,
4729 	jwr.role_code, null PRIMARY_CONTACT_FLAG,
4730     NVL(jwr.RES_START_DATE, jwot.TERR_START_DATE) START_DATE_ACTIVE,
4731     NVL(jwr.RES_END_DATE, jwot.TERR_END_DATE) END_DATE_ACTIVE,
4732     jwot.org_id, 'N' FULL_ACCESS_FLAG,
4733     jwr.group_id, NULL SECURITY_GROUP_ID,
4734     res.source_id person_id, NULL OBJECT_VERSION_NUMBER,
4735     jwr.ATTRIBUTE_CATEGORY, jwr.ATTRIBUTE1, jwr.ATTRIBUTE2,
4736     jwr.ATTRIBUTE3, jwr.ATTRIBUTE4, jwr.ATTRIBUTE5,
4737     jwr.ATTRIBUTE6, jwr.ATTRIBUTE7, jwr.ATTRIBUTE8,
4741   from
4738     jwr.ATTRIBUTE9, jwr.ATTRIBUTE10, jwr.ATTRIBUTE11,
4739     jwr.ATTRIBUTE12, jwr.ATTRIBUTE13, jwr.ATTRIBUTE14,
4740     jwr.ATTRIBUTE15
4742     JTY_WEBADI_OTH_TERR_INTF jwot,
4743     JTY_WEBADI_RESOURCES jwr,
4744     jtf_rs_resource_extns res
4745   where jwr.RESOURCE_ID = res.resource_id(+)
4746     and jwr.user_sequence = jwot.user_sequence
4747     and jwr.interface_type = jwot.interface_type
4748     and jwot.lay_seq_num = jwr.lay_seq_num
4749     and jwot.user_sequence = v_user_sequence
4750     and jwot.interface_type = v_intf_type
4751     and jwot.header = v_header
4752     and jwot.status is null
4753     and jwr.header = jwot.header
4754     and jwot.action_flag = v_action_flag;
4755 
4756 	cursor get_c_rsc_access_csr(
4757 	   v_user_sequence  number,
4758 	   v_header			varchar2,
4759 	   v_intf_type		varchar2) is
4760 	   select sub.TERR_RSC_ACCESS_ID,
4761        jwot.LAST_UPDATE_DATE, jwot.LAST_UPDATED_BY,
4762        jwot.CREATION_DATE, jwot.CREATED_BY, jwot.LAST_UPDATE_LOGIN,
4763        sub.TERR_RSC_ID, jq.QUAL_TYPE_NAME ACCESS_TYPE,
4764        jwot.ORG_ID, sub.TRANS_ACCESS_CODE
4765       from JTY_WEBADI_QUAL_TYPE_HEADER jq,
4766 	  JTY_WEBADI_OTH_TERR_INTF jwot,
4767   	  (
4768       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4769       1 qual_type_num, TERR_RSC_ACCESS_ID1 TERR_RSC_ACCESS_ID,
4770       TRANS_ACCESS_CODE1 TRANS_ACCESS_CODE
4771       FROM JTY_WEBADI_RESOURCES jut
4772       where jut.USER_SEQUENCE = v_user_sequence
4773       and jut.TRANS_ACCESS_CODE1 is not null
4774 	  and jut.TERR_RSC_ACCESS_ID1 is null
4775 	  and jut.header = v_header
4776   	  and jut.INTERFACE_TYPE = v_intf_type
4777       union all
4778       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4779       2 qual_type_num, TERR_RSC_ACCESS_ID2 TERR_RSC_ACCESS_ID,
4780       TRANS_ACCESS_CODE2 TRANS_ACCESS_CODE
4781       FROM JTY_WEBADI_RESOURCES jut
4782       where jut.USER_SEQUENCE = v_user_sequence
4783       and jut.TRANS_ACCESS_CODE2 is not null
4784 	  and jut.TERR_RSC_ACCESS_ID2 is null
4785 	  and jut.header = v_header
4786   	  and jut.INTERFACE_TYPE = v_intf_type
4787       union all
4788       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4789       3 qual_type_num, TERR_RSC_ACCESS_ID3 TERR_RSC_ACCESS_ID,
4790       TRANS_ACCESS_CODE3 TRANS_ACCESS_CODE
4791       FROM JTY_WEBADI_RESOURCES jut
4792       where jut.USER_SEQUENCE = v_user_sequence
4793       and jut.TRANS_ACCESS_CODE3 is not null
4794 	  and jut.TERR_RSC_ACCESS_ID3 is null
4795 	  and jut.header = v_header
4796   	  and jut.INTERFACE_TYPE = v_intf_type
4797       union all
4798       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4799       4 qual_type_num, TERR_RSC_ACCESS_ID4 TERR_RSC_ACCESS_ID,
4800       TRANS_ACCESS_CODE4 TRANS_ACCESS_CODE
4801       FROM JTY_WEBADI_RESOURCES jut
4802       where jut.USER_SEQUENCE = v_user_sequence
4803       and jut.TRANS_ACCESS_CODE4 is not null
4804 	  and jut.TERR_RSC_ACCESS_ID4 is null
4805 	  and jut.header = v_header
4806   	  and jut.INTERFACE_TYPE = v_intf_type
4807       union all
4808       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4809       5 qual_type_num, TERR_RSC_ACCESS_ID5 TERR_RSC_ACCESS_ID,
4810       TRANS_ACCESS_CODE5 TRANS_ACCESS_CODE
4811       FROM JTY_WEBADI_RESOURCES jut
4812       where jut.USER_SEQUENCE = v_user_sequence
4813       and jut.TRANS_ACCESS_CODE5 is not null
4814 	  and jut.TERR_RSC_ACCESS_ID5 is null
4815 	  and jut.header = v_header
4816   	  and jut.INTERFACE_TYPE = v_intf_type
4817       union all
4818       select user_sequence, TERR_RSC_ID,lay_seq_num, header,
4819       6 qual_type_num, TERR_RSC_ACCESS_ID6 TERR_RSC_ACCESS_ID,
4820       TRANS_ACCESS_CODE6 TRANS_ACCESS_CODE
4821       FROM JTY_WEBADI_RESOURCES jut
4822       where jut.USER_SEQUENCE = v_user_sequence
4823       and jut.TRANS_ACCESS_CODE6 is not null
4824 	  and jut.TERR_RSC_ACCESS_ID6 is null
4825 	  and jut.header = v_header
4826   	  and jut.INTERFACE_TYPE = v_intf_type
4827       union all
4828       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4829       7 qual_type_num, TERR_RSC_ACCESS_ID7 TERR_RSC_ACCESS_ID,
4830       TRANS_ACCESS_CODE7 TRANS_ACCESS_CODE
4831       FROM JTY_WEBADI_RESOURCES jut
4832       where jut.USER_SEQUENCE = v_user_sequence
4833       and jut.TRANS_ACCESS_CODE7 is not null
4834 	  and jut.TERR_RSC_ACCESS_ID7 is null
4835 	  and jut.header = v_header
4836   	  and jut.INTERFACE_TYPE = v_intf_type
4837       union all
4838       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4839       8 qual_type_num, TERR_RSC_ACCESS_ID8 TERR_RSC_ACCESS_ID,
4840       TRANS_ACCESS_CODE8 TRANS_ACCESS_CODE
4844 	  and jut.TERR_RSC_ACCESS_ID8 is null
4841       FROM JTY_WEBADI_RESOURCES jut
4842       where jut.USER_SEQUENCE = v_user_sequence
4843       and jut.TRANS_ACCESS_CODE8 is not null
4845 	  and jut.header = v_header
4846   	  and jut.INTERFACE_TYPE = v_intf_type
4847       union all
4848       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4849       9 qual_type_num, TERR_RSC_ACCESS_ID9 TERR_RSC_ACCESS_ID,
4850       TRANS_ACCESS_CODE9 TRANS_ACCESS_CODE
4851       FROM JTY_WEBADI_RESOURCES jut
4852       where jut.USER_SEQUENCE = v_user_sequence
4853       and jut.TRANS_ACCESS_CODE9 is not null
4854 	  and jut.TERR_RSC_ACCESS_ID9 is null
4855 	  and jut.header = v_header
4856   	  and jut.INTERFACE_TYPE = v_intf_type
4857       union all
4858       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4859       10 qual_type_num, TERR_RSC_ACCESS_ID10 TERR_RSC_ACCESS_ID,
4860       TRANS_ACCESS_CODE10 TRANS_ACCESS_CODE
4861       FROM JTY_WEBADI_RESOURCES jut
4862       where jut.USER_SEQUENCE = v_user_sequence
4863       and jut.TRANS_ACCESS_CODE10 is not null
4864 	  and jut.TERR_RSC_ACCESS_ID10 is null
4865 	  and jut.header = v_header
4866   	  and jut.INTERFACE_TYPE = v_intf_type) sub
4867 	  where sub.user_sequence = jq.user_sequence
4868 	  and sub.qual_type_num = jq.QUAL_TYPE_NUM
4869 	  and sub.lay_seq_num = jwot.lay_seq_num
4870 	  and sub.user_sequence = jwot.user_sequence
4871 	  and sub.header = jwot.header
4872 	  and jwot.interface_type = v_intf_type
4873 	  and jwot.status is null;
4874 
4875 	cursor get_u_rsc_access_csr(
4876 	   v_user_sequence  number,
4877 	   v_header			varchar2,
4878 	   v_intf_type		varchar2) is
4879 	   select sub.TERR_RSC_ACCESS_ID,
4880        jwot.LAST_UPDATE_DATE, jwot.LAST_UPDATED_BY,
4881        jwot.CREATION_DATE, jwot.CREATED_BY, jwot.LAST_UPDATE_LOGIN,
4882        sub.TERR_RSC_ID, jq.QUAL_TYPE_NAME ACCESS_TYPE,
4883        jwot.ORG_ID, sub.TRANS_ACCESS_CODE
4884       from JTY_WEBADI_QUAL_TYPE_HEADER jq,
4885 	  JTY_WEBADI_OTH_TERR_INTF jwot,
4886   	  (
4887       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4888       1 qual_type_num, TERR_RSC_ACCESS_ID1 TERR_RSC_ACCESS_ID,
4889       TRANS_ACCESS_CODE1 TRANS_ACCESS_CODE
4890       FROM JTY_WEBADI_RESOURCES jut
4891       where jut.USER_SEQUENCE = v_user_sequence
4892       and jut.TRANS_ACCESS_CODE1 is not null
4893 	  and jut.TERR_RSC_ACCESS_ID1 is not null
4894 	  and jut.header = v_header
4895   	  and jut.INTERFACE_TYPE = v_intf_type
4896       union all
4897       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4898       2 qual_type_num, TERR_RSC_ACCESS_ID2 TERR_RSC_ACCESS_ID,
4899       TRANS_ACCESS_CODE2 TRANS_ACCESS_CODE
4900       FROM JTY_WEBADI_RESOURCES jut
4901       where jut.USER_SEQUENCE = v_user_sequence
4902       and jut.TRANS_ACCESS_CODE2 is not null
4903 	  and jut.TERR_RSC_ACCESS_ID2 is not null
4904 	  and jut.header = v_header
4905   	  and jut.INTERFACE_TYPE = v_intf_type
4906       union all
4907       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4908       3 qual_type_num, TERR_RSC_ACCESS_ID3 TERR_RSC_ACCESS_ID,
4909       TRANS_ACCESS_CODE3 TRANS_ACCESS_CODE
4910       FROM JTY_WEBADI_RESOURCES jut
4911       where jut.USER_SEQUENCE = v_user_sequence
4912       and jut.TRANS_ACCESS_CODE3 is not null
4913 	  and jut.TERR_RSC_ACCESS_ID3 is not null
4914 	  and jut.header = v_header
4915   	  and jut.INTERFACE_TYPE = v_intf_type
4916       union all
4917       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4918       4 qual_type_num, TERR_RSC_ACCESS_ID4 TERR_RSC_ACCESS_ID,
4919       TRANS_ACCESS_CODE4 TRANS_ACCESS_CODE
4920       FROM JTY_WEBADI_RESOURCES jut
4921       where jut.USER_SEQUENCE = v_user_sequence
4922       and jut.TRANS_ACCESS_CODE4 is not null
4923 	  and jut.TERR_RSC_ACCESS_ID4 is not null
4924 	  and jut.header = v_header
4925   	  and jut.INTERFACE_TYPE = v_intf_type
4926       union all
4927       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4928       5 qual_type_num, TERR_RSC_ACCESS_ID5 TERR_RSC_ACCESS_ID,
4929       TRANS_ACCESS_CODE5 TRANS_ACCESS_CODE
4930       FROM JTY_WEBADI_RESOURCES jut
4931       where jut.USER_SEQUENCE = v_user_sequence
4932       and jut.TRANS_ACCESS_CODE5 is not null
4933 	  and jut.TERR_RSC_ACCESS_ID5 is not null
4934 	  and jut.header = v_header
4935   	  and jut.INTERFACE_TYPE = v_intf_type
4936       union all
4937       select user_sequence, TERR_RSC_ID,lay_seq_num, header,
4938       6 qual_type_num, TERR_RSC_ACCESS_ID6 TERR_RSC_ACCESS_ID,
4939       TRANS_ACCESS_CODE6 TRANS_ACCESS_CODE
4940       FROM JTY_WEBADI_RESOURCES jut
4941       where jut.USER_SEQUENCE = v_user_sequence
4942       and jut.TRANS_ACCESS_CODE6 is not null
4943 	  and jut.TERR_RSC_ACCESS_ID6 is not null
4944 	  and jut.header = v_header
4945   	  and jut.INTERFACE_TYPE = v_intf_type
4946       union all
4947       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4948       7 qual_type_num, TERR_RSC_ACCESS_ID7 TERR_RSC_ACCESS_ID,
4949       TRANS_ACCESS_CODE7 TRANS_ACCESS_CODE
4950       FROM JTY_WEBADI_RESOURCES jut
4951       where jut.USER_SEQUENCE = v_user_sequence
4952       and jut.TRANS_ACCESS_CODE7 is not null
4953 	  and jut.TERR_RSC_ACCESS_ID7 is not null
4954 	  and jut.header = v_header
4955   	  and jut.INTERFACE_TYPE = v_intf_type
4956       union all
4957       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4958       8 qual_type_num, TERR_RSC_ACCESS_ID8 TERR_RSC_ACCESS_ID,
4959       TRANS_ACCESS_CODE8 TRANS_ACCESS_CODE
4963 	  and jut.TERR_RSC_ACCESS_ID8 is not null
4960       FROM JTY_WEBADI_RESOURCES jut
4961       where jut.USER_SEQUENCE = v_user_sequence
4962       and jut.TRANS_ACCESS_CODE8 is not null
4964 	  and jut.header = v_header
4965   	  and jut.INTERFACE_TYPE = v_intf_type
4966       union all
4967       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4968       9 qual_type_num, TERR_RSC_ACCESS_ID9 TERR_RSC_ACCESS_ID,
4969       TRANS_ACCESS_CODE9 TRANS_ACCESS_CODE
4970       FROM JTY_WEBADI_RESOURCES jut
4971       where jut.USER_SEQUENCE = v_user_sequence
4972       and jut.TRANS_ACCESS_CODE9 is not null
4973 	  and jut.TERR_RSC_ACCESS_ID9 is not null
4974 	  and jut.header = v_header
4975   	  and jut.INTERFACE_TYPE = v_intf_type
4976       union all
4977       select user_sequence, TERR_RSC_ID, lay_seq_num, header,
4978       10 qual_type_num, TERR_RSC_ACCESS_ID10 TERR_RSC_ACCESS_ID,
4979       TRANS_ACCESS_CODE10 TRANS_ACCESS_CODE
4980       FROM JTY_WEBADI_RESOURCES jut
4981       where jut.USER_SEQUENCE = v_user_sequence
4982       and jut.TRANS_ACCESS_CODE10 is not null
4983 	  and jut.TERR_RSC_ACCESS_ID10 is not null
4984 	  and jut.header = v_header
4985   	  and jut.INTERFACE_TYPE = v_intf_type) sub
4986 	  where sub.user_sequence = jq.user_sequence
4987 	  and sub.qual_type_num = jq.QUAL_TYPE_NUM
4988 	  and sub.lay_seq_num = jwot.lay_seq_num
4989 	  and sub.user_sequence = jwot.user_sequence
4990 	  and sub.header = jwot.header
4991 	  and jwot.interface_type = v_intf_type
4992 	  and jwot.status is null;
4993 
4994   TYPE rsc_rec_type IS RECORD
4995   ( LAY_SEQ_NUM		   		  number_tbl_type,
4996     TERR_RSC_ID		   		  number_tbl_type,
4997   	LAST_UPDATE_DATE		  date_tbl_type,
4998 	LAST_UPDATED_BY	  		  number_tbl_type,
4999     CREATION_DATE			  date_tbl_type,
5000 	CREATED_BY	   			  number_tbl_type,
5001 	LAST_UPDATE_LOGIN		  number_tbl_type,
5002     TERR_ID					  number_tbl_type,
5003 	RESOURCE_ID				  number_tbl_type,
5004 	RESOURCE_TYPE			  varchar2_tbl_type,
5005     ROLE_CODE				  varchar2_tbl_type,
5006 	PRIMARY_CONTACT_FLAG	  varchar2_tbl_type,
5007 	START_DATE_ACTIVE		  date_tbl_type,
5008     END_DATE_ACTIVE			  date_tbl_type,
5009 	ORG_ID			   	  	  number_tbl_type,
5010 	FULL_ACCESS_FLAG		  varchar2_tbl_type,
5011     GROUP_ID		  		  number_tbl_type,
5012 	SECURITY_GROUP_ID		  number_tbl_type,
5013 	PERSON_ID				  number_tbl_type,
5014     OBJECT_VERSION_NUMBER	  number_tbl_type,
5015 	ATTRIBUTE_CATEGORY		  varchar2_tbl_type,
5016 	ATTRIBUTE1				  varchar2_tbl_type,
5017     ATTRIBUTE2				  varchar2_tbl_type,
5018 	ATTRIBUTE3				  varchar2_tbl_type,
5019 	ATTRIBUTE4				  varchar2_tbl_type,
5020     ATTRIBUTE5				  varchar2_tbl_type,
5021 	ATTRIBUTE6				  varchar2_tbl_type,
5022 	ATTRIBUTE7				  varchar2_tbl_type,
5023     ATTRIBUTE8				  varchar2_tbl_type,
5024 	ATTRIBUTE9				  varchar2_tbl_type,
5025 	ATTRIBUTE10				  varchar2_tbl_type,
5026     ATTRIBUTE11				  varchar2_tbl_type,
5027 	ATTRIBUTE12				  varchar2_tbl_type,
5028 	ATTRIBUTE13				  varchar2_tbl_type,
5029     ATTRIBUTE14				  varchar2_tbl_type,
5030 	ATTRIBUTE15				  varchar2_tbl_type
5031 	);
5032 
5033 	 TYPE rsc_access_rec_type IS RECORD(
5034        TERR_RSC_ACCESS_ID	  number_tbl_type,
5035        LAST_UPDATE_DATE		  date_tbl_type,
5036        LAST_UPDATED_BY		  number_tbl_type,
5037        CREATION_DATE		  date_tbl_type,
5038        CREATED_BY	 		  number_tbl_type,
5039        LAST_UPDATE_LOGIN	  number_tbl_type,
5040        TERR_RSC_ID			  number_tbl_type,
5041        ACCESS_TYPE			  varchar2_tbl_type,
5042 	   TRANS_ACCESS_CODE	  varchar2_tbl_type,
5043        ORG_ID			 	  number_tbl_type);
5044 
5045   l_rsc_rec 		  rsc_rec_type;
5046   l_get_terr_rsc_rec  rsc_rec_type;
5047   l_rsc_access_rec	  rsc_access_rec_type;
5048 
5049   l_header varchar2(15) := 'RSC';
5050   l_intf_type varchar2(1) := 'U';
5051   l_action_flag varchar2(1);
5052   l_api_version_number      CONSTANT NUMBER   := 1.0;
5053   X_Msg_Count		 number;
5054 
5055 BEGIN
5056 
5057   --l_action_flag := p_action_flag;
5058   x_return_status := FND_API.G_RET_STS_SUCCESS;
5059   l_action_flag := 'C';
5060 
5061   -- process resource records
5062   open get_rsc_csr(p_user_sequence, l_intf_type, l_header, l_action_flag);
5063   fetch get_rsc_csr bulk collect into l_rsc_rec.LAY_SEQ_NUM,
5064   		l_rsc_rec.TERR_RSC_ID, l_rsc_rec.LAST_UPDATE_DATE, l_rsc_rec.LAST_UPDATED_BY,
5065   		l_rsc_rec.CREATION_DATE, l_rsc_rec.CREATED_BY, l_rsc_rec.LAST_UPDATE_LOGIN,
5066   		l_rsc_rec.TERR_ID, l_rsc_rec.RESOURCE_ID, l_rsc_rec.RESOURCE_TYPE,
5067   		l_rsc_rec.ROLE_CODE, l_rsc_rec.PRIMARY_CONTACT_FLAG, l_rsc_rec.START_DATE_ACTIVE,
5068   		l_rsc_rec.END_DATE_ACTIVE, l_rsc_rec.ORG_ID, l_rsc_rec.FULL_ACCESS_FLAG,
5069   		l_rsc_rec.GROUP_ID, l_rsc_rec.SECURITY_GROUP_ID, l_rsc_rec.PERSON_ID,
5070   		l_rsc_rec.OBJECT_VERSION_NUMBER, l_rsc_rec.ATTRIBUTE_CATEGORY, l_rsc_rec.ATTRIBUTE1,
5071   		l_rsc_rec.ATTRIBUTE2, l_rsc_rec.ATTRIBUTE3, l_rsc_rec.ATTRIBUTE4,
5072   		l_rsc_rec.ATTRIBUTE5, l_rsc_rec.ATTRIBUTE6, l_rsc_rec.ATTRIBUTE7,
5073   		l_rsc_rec.ATTRIBUTE8, l_rsc_rec.ATTRIBUTE9, l_rsc_rec.ATTRIBUTE10,
5074   		l_rsc_rec.ATTRIBUTE11, l_rsc_rec.ATTRIBUTE12, l_rsc_rec.ATTRIBUTE13,
5075   		l_rsc_rec.ATTRIBUTE14, l_rsc_rec.ATTRIBUTE15;
5076   close get_rsc_csr;
5077 
5078 	  -- direct insert into the table since the package JTF_TERRITORY_RESOURCE_PVT
5079 	  -- doesnot support flex field insert
5080   if (l_action_flag = 'C' AND l_rsc_rec.RESOURCE_ID.count > 0) then
5081     --dbms_output.put_line('Create territory resource information, rowcount: ' || l_rsc_rec.RESOURCE_ID.count);
5082     forall i in l_rsc_rec.RESOURCE_ID.first..l_rsc_rec.RESOURCE_ID.last
5083        INSERT INTO JTF_TERR_RSC_ALL (
5084           TERR_RSC_ID, LAST_UPDATE_DATE, LAST_UPDATED_BY,
5085           CREATION_DATE, CREATED_BY, LAST_UPDATE_LOGIN,
5086           TERR_ID, RESOURCE_ID, RESOURCE_TYPE,
5087           ROLE, PRIMARY_CONTACT_FLAG, START_DATE_ACTIVE,
5088           END_DATE_ACTIVE, ORG_ID, FULL_ACCESS_FLAG,
5089           GROUP_ID, SECURITY_GROUP_ID, PERSON_ID,
5090           OBJECT_VERSION_NUMBER, ATTRIBUTE_CATEGORY, ATTRIBUTE1,
5091           ATTRIBUTE2, ATTRIBUTE3, ATTRIBUTE4,
5092           ATTRIBUTE5, ATTRIBUTE6, ATTRIBUTE7,
5093           ATTRIBUTE8, ATTRIBUTE9, ATTRIBUTE10,
5094           ATTRIBUTE11, ATTRIBUTE12, ATTRIBUTE13,
5095           ATTRIBUTE14, ATTRIBUTE15)
5096 		VALUES ( JTF_TERR_RSC_s.nextval, l_rsc_rec.LAST_UPDATE_DATE(i),
5097 		  l_rsc_rec.LAST_UPDATED_BY(i), l_rsc_rec.CREATION_DATE(i),
5098 		  l_rsc_rec.CREATED_BY(i), l_rsc_rec.LAST_UPDATE_LOGIN(i),
5099           l_rsc_rec.TERR_ID(i), l_rsc_rec.RESOURCE_ID(i), l_rsc_rec.RESOURCE_TYPE(i),
5100           l_rsc_rec.ROLE_CODE(i), l_rsc_rec.PRIMARY_CONTACT_FLAG(i), l_rsc_rec.START_DATE_ACTIVE(i),
5101           l_rsc_rec.END_DATE_ACTIVE(i), l_rsc_rec.ORG_ID(i), l_rsc_rec.FULL_ACCESS_FLAG(i),
5102           l_rsc_rec.GROUP_ID(i), l_rsc_rec.SECURITY_GROUP_ID(i), l_rsc_rec.PERSON_ID(i),
5103           l_rsc_rec.OBJECT_VERSION_NUMBER(i), l_rsc_rec.ATTRIBUTE_CATEGORY(i), l_rsc_rec.ATTRIBUTE1(i),
5104           l_rsc_rec.ATTRIBUTE2(i), l_rsc_rec.ATTRIBUTE3(i), l_rsc_rec.ATTRIBUTE4(i),
5105           l_rsc_rec.ATTRIBUTE5(i), l_rsc_rec.ATTRIBUTE6(i), l_rsc_rec.ATTRIBUTE7(i),
5106           l_rsc_rec.ATTRIBUTE8(i), l_rsc_rec.ATTRIBUTE9(i), l_rsc_rec.ATTRIBUTE10(i),
5107           l_rsc_rec.ATTRIBUTE11(i), l_rsc_rec.ATTRIBUTE12(i), l_rsc_rec.ATTRIBUTE13(i),
5108           l_rsc_rec.ATTRIBUTE14(i), l_rsc_rec.ATTRIBUTE15(i) )
5109 		RETURNING terr_rsc_id, resource_id, terr_id
5110 		bulk collect into l_get_terr_rsc_rec.terr_rsc_id,
5111 		  l_get_terr_rsc_rec.resource_id, l_get_terr_rsc_rec.terr_id;
5112 
5113 	--dbms_output.put_line(' # records processed for create:  '||SQL%ROWCOUNT);
5114 
5115 	  if (l_get_terr_rsc_rec.terr_rsc_id.count > 0) then
5116 		forall i in l_get_terr_rsc_rec.terr_rsc_id.first..l_get_terr_rsc_rec.terr_rsc_id.last
5117 		   update JTY_WEBADI_RESOURCES
5118   		   set terr_rsc_id = l_get_terr_rsc_rec.terr_rsc_id(i)
5119 		   where resource_id = l_get_terr_rsc_rec.resource_id(i)
5120 --		     and terr_id = l_get_terr_rsc_rec.terr_id(i)
5121 			 and interface_type = l_intf_type
5122 			 and user_sequence = p_user_sequence
5123 			 and header = l_header
5124 			 and terr_rsc_id is null;
5125 	  end if;
5126 
5127   end if;
5128 
5129   l_action_flag := 'U';
5130 
5131   -- process resource records
5132   open get_rsc_csr(p_user_sequence, l_intf_type, l_header, l_action_flag);
5133   fetch get_rsc_csr bulk collect into l_rsc_rec.LAY_SEQ_NUM,
5134   		l_rsc_rec.TERR_RSC_ID, l_rsc_rec.LAST_UPDATE_DATE, l_rsc_rec.LAST_UPDATED_BY,
5135   		l_rsc_rec.CREATION_DATE, l_rsc_rec.CREATED_BY, l_rsc_rec.LAST_UPDATE_LOGIN,
5136   		l_rsc_rec.TERR_ID, l_rsc_rec.RESOURCE_ID, l_rsc_rec.RESOURCE_TYPE,
5137   		l_rsc_rec.ROLE_CODE, l_rsc_rec.PRIMARY_CONTACT_FLAG, l_rsc_rec.START_DATE_ACTIVE,
5138   		l_rsc_rec.END_DATE_ACTIVE, l_rsc_rec.ORG_ID, l_rsc_rec.FULL_ACCESS_FLAG,
5139   		l_rsc_rec.GROUP_ID, l_rsc_rec.SECURITY_GROUP_ID, l_rsc_rec.PERSON_ID,
5143   		l_rsc_rec.ATTRIBUTE8, l_rsc_rec.ATTRIBUTE9, l_rsc_rec.ATTRIBUTE10,
5140   		l_rsc_rec.OBJECT_VERSION_NUMBER, l_rsc_rec.ATTRIBUTE_CATEGORY, l_rsc_rec.ATTRIBUTE1,
5141   		l_rsc_rec.ATTRIBUTE2, l_rsc_rec.ATTRIBUTE3, l_rsc_rec.ATTRIBUTE4,
5142   		l_rsc_rec.ATTRIBUTE5, l_rsc_rec.ATTRIBUTE6, l_rsc_rec.ATTRIBUTE7,
5144   		l_rsc_rec.ATTRIBUTE11, l_rsc_rec.ATTRIBUTE12, l_rsc_rec.ATTRIBUTE13,
5145   		l_rsc_rec.ATTRIBUTE14, l_rsc_rec.ATTRIBUTE15;
5146   close get_rsc_csr;
5147 
5148 	  -- direct insert into the table since the package JTF_TERRITORY_RESOURCE_PVT
5149 	  -- doesnot support flex field insert
5150   if (l_action_flag = 'U' AND l_rsc_rec.TERR_RSC_ID.count > 0) then
5151     --dbms_output.put_line('Update territory resource information, rowcount: ' || l_rsc_rec.TERR_RSC_ID.count);
5152 
5153     forall i in l_rsc_rec.TERR_RSC_ID.first..l_rsc_rec.TERR_RSC_ID.last
5154 	  UPDATE JTF_TERR_RSC_ALL
5155 	  SET LAST_UPDATE_DATE = l_rsc_rec.LAST_UPDATE_DATE(i),
5156 	    LAST_UPDATED_BY = l_rsc_rec.LAST_UPDATED_BY(i),
5157 		LAST_UPDATE_LOGIN = l_rsc_rec.LAST_UPDATE_LOGIN(i),
5158         TERR_ID = l_rsc_rec.TERR_ID(i),
5159 		RESOURCE_ID = l_rsc_rec.RESOURCE_ID(i),
5160 		RESOURCE_TYPE = l_rsc_rec.RESOURCE_TYPE(i),
5161         ROLE = l_rsc_rec.ROLE_CODE(i),
5162 		START_DATE_ACTIVE = l_rsc_rec.START_DATE_ACTIVE(i),
5163         END_DATE_ACTIVE = l_rsc_rec.END_DATE_ACTIVE(i),
5164 		ORG_ID = l_rsc_rec.ORG_ID(i),
5165         GROUP_ID = l_rsc_rec.GROUP_ID(i),
5166 		PERSON_ID = l_rsc_rec.PERSON_ID(i),
5167 		ATTRIBUTE_CATEGORY = l_rsc_rec.ATTRIBUTE_CATEGORY(i),
5168 		ATTRIBUTE1 = l_rsc_rec.ATTRIBUTE1(i),
5169         ATTRIBUTE2 = l_rsc_rec.ATTRIBUTE2(i),
5170 		ATTRIBUTE3 = l_rsc_rec.ATTRIBUTE3(i),
5171 		ATTRIBUTE4 = l_rsc_rec.ATTRIBUTE4(i),
5172         ATTRIBUTE5 = l_rsc_rec.ATTRIBUTE5(i),
5173 		ATTRIBUTE6 = l_rsc_rec.ATTRIBUTE6(i),
5174 		ATTRIBUTE7 = l_rsc_rec.ATTRIBUTE7(i),
5175         ATTRIBUTE8 = l_rsc_rec.ATTRIBUTE8(i),
5176 		ATTRIBUTE9 = l_rsc_rec.ATTRIBUTE9(i),
5177 		ATTRIBUTE10 = l_rsc_rec.ATTRIBUTE10(i),
5178         ATTRIBUTE11 = l_rsc_rec.ATTRIBUTE11(i),
5179 		ATTRIBUTE12 = l_rsc_rec.ATTRIBUTE12(i),
5180 		ATTRIBUTE13 = l_rsc_rec.ATTRIBUTE13(i),
5181         ATTRIBUTE14 = l_rsc_rec.ATTRIBUTE14(i),
5182 		ATTRIBUTE15 = l_rsc_rec.ATTRIBUTE15(i)
5183 	  WHERE TERR_RSC_ID = l_rsc_rec.TERR_RSC_ID(i);
5184 
5185 	--dbms_output.put_line(' # records processed for update:  '||SQL%ROWCOUNT);
5186   end if;
5187 
5188   BEGIN
5189     x_return_status := FND_API.G_RET_STS_SUCCESS;
5190       open get_c_rsc_access_csr(P_USER_SEQUENCE, l_header, l_intf_type);
5191       fetch get_c_rsc_access_csr bulk collect into l_rsc_access_rec.TERR_RSC_ACCESS_ID,
5192        l_rsc_access_rec.LAST_UPDATE_DATE, l_rsc_access_rec.LAST_UPDATED_BY,
5193        l_rsc_access_rec.CREATION_DATE, l_rsc_access_rec.CREATED_BY,
5194 	   l_rsc_access_rec.LAST_UPDATE_LOGIN,
5195        l_rsc_access_rec.TERR_RSC_ID, l_rsc_access_rec.ACCESS_TYPE,
5196        l_rsc_access_rec.ORG_ID, l_rsc_access_rec.TRANS_ACCESS_CODE;
5197       close get_c_rsc_access_csr;
5198 
5199       --dbms_output.put_line('Add Access to resource_id: '||l_terr_rsc_out_tbl(i).terr_rsc_id ||',access count: '||l_tr_access_tbl.count );
5200       if l_rsc_access_rec.TERR_RSC_ID.count > 0 then
5201 		forall i in l_rsc_access_rec.TERR_RSC_ID.first..l_rsc_access_rec.TERR_RSC_ID.last
5202           INSERT INTO JTF_TERR_RSC_ACCESS_ALL(
5203             TERR_RSC_ACCESS_ID,
5204             LAST_UPDATE_DATE,
5205             LAST_UPDATED_BY,
5206             CREATION_DATE,
5207             CREATED_BY,
5208             LAST_UPDATE_LOGIN,
5209             TERR_RSC_ID,
5210             ACCESS_TYPE,
5211        	   	TRANS_ACCESS_CODE,
5212             ORG_ID)
5213 		  VALUES (
5214             JTF_TERR_RSC_ACCESS_s.nextval,
5215             l_rsc_access_rec.LAST_UPDATE_DATE(i),
5216             l_rsc_access_rec.LAST_UPDATED_BY(i),
5217             l_rsc_access_rec.CREATION_DATE(i),
5218             l_rsc_access_rec.CREATED_BY(i),
5219             l_rsc_access_rec.LAST_UPDATE_LOGIN(i),
5220             l_rsc_access_rec.TERR_RSC_ID(i),
5221             l_rsc_access_rec.ACCESS_TYPE(i),
5222             l_rsc_access_rec.TRANS_ACCESS_CODE(i),
5223             l_rsc_access_rec.ORG_ID(i)
5224            );
5225 
5226 		forall i in l_rsc_access_rec.TERR_RSC_ID.first..l_rsc_access_rec.TERR_RSC_ID.last
5227       	  update JTY_WEBADI_OTH_TERR_INTF jwot
5228     	  set status = x_return_status
5229     	  where exists
5230     	    (select 1 from JTY_WEBADI_RESOURCES jwr
5231     		 where jwr.LAY_SEQ_NUM = jwot.LAY_SEQ_NUM
5232     		   and jwr.header = jwot.header
5233     		   and jwr.user_sequence = jwot.user_sequence
5234     		   and jwr.interface_type = jwot.interface_type
5235 			   and jwr.terr_rsc_id = l_rsc_access_rec.terr_rsc_id(i))
5236     		and jwot.USER_SEQUENCE = p_user_sequence
5237     		and jwot.header = l_header
5238     		and jwot.interface_type = l_intf_type
5239     		and jwot.status is null;
5240 	  end if; -- create resource access type
5241 
5242 	  exception
5243 	  when others then
5244 	    x_return_status := FND_API.G_RET_STS_ERROR;
5245 		fnd_message.clear;
5246    	  	fnd_message.set_name ('JTF', 'JTY_OTH_TERR_CREATE_ACCESS');
5247    	  	X_Msg_Data := fnd_message.get();
5248 
5249   		forall i in l_rsc_access_rec.TERR_RSC_ID.first..l_rsc_access_rec.TERR_RSC_ID.last
5250       	  update JTY_WEBADI_OTH_TERR_INTF jwot
5251     	  set status = x_return_status,
5252 		  error_msg = x_msg_data
5253     	  where exists
5257     		   and jwr.user_sequence = jwot.user_sequence
5254     	    (select 1 from JTY_WEBADI_RESOURCES jwr
5255     		 where jwr.LAY_SEQ_NUM = jwot.LAY_SEQ_NUM
5256     		   and jwr.header = jwot.header
5258     		   and jwr.interface_type = jwot.interface_type
5259 			   and jwr.terr_rsc_id = l_rsc_access_rec.terr_rsc_id(i))
5260     		and jwot.USER_SEQUENCE = p_user_sequence
5261     		and jwot.header = l_header
5262     		and jwot.interface_type = l_intf_type
5263     		and jwot.status is null;
5264     END;
5265 
5266 	BEGIN
5267     x_return_status := FND_API.G_RET_STS_SUCCESS;
5268       open get_u_rsc_access_csr(P_USER_SEQUENCE, l_header, l_intf_type);
5269       fetch get_u_rsc_access_csr bulk collect into l_rsc_access_rec.TERR_RSC_ACCESS_ID,
5270        l_rsc_access_rec.LAST_UPDATE_DATE, l_rsc_access_rec.LAST_UPDATED_BY,
5271        l_rsc_access_rec.CREATION_DATE, l_rsc_access_rec.CREATED_BY,
5272 	   l_rsc_access_rec.LAST_UPDATE_LOGIN,
5273        l_rsc_access_rec.TERR_RSC_ID, l_rsc_access_rec.ACCESS_TYPE,
5274        l_rsc_access_rec.ORG_ID, l_rsc_access_rec.TRANS_ACCESS_CODE;
5275       close get_u_rsc_access_csr;
5276 
5277       --dbms_output.put_line('Add Access to resource_id: '||l_terr_rsc_out_tbl(i).terr_rsc_id ||',access count: '||l_tr_access_tbl.count );
5278       if l_rsc_access_rec.TERR_RSC_ID.count > 0 then
5279 		forall i in l_rsc_access_rec.TERR_RSC_ID.first..l_rsc_access_rec.TERR_RSC_ID.last
5280             Update JTF_TERR_RSC_ACCESS_ALL
5281             SET
5282               LAST_UPDATE_DATE = l_rsc_access_rec.LAST_UPDATE_DATE(i),
5283               LAST_UPDATED_BY = l_rsc_access_rec.LAST_UPDATED_BY(i),
5284               LAST_UPDATE_LOGIN = l_rsc_access_rec.LAST_UPDATE_LOGIN(i),
5285               TERR_RSC_ID = l_rsc_access_rec.TERR_RSC_ID(i),
5286               ACCESS_TYPE = l_rsc_access_rec.ACCESS_TYPE(i),
5287               TRANS_ACCESS_CODE = l_rsc_access_rec.TRANS_ACCESS_CODE(i),
5288               ORG_ID = l_rsc_access_rec.ORG_ID(i)
5289             where TERR_RSC_ACCESS_ID = l_rsc_access_rec.TERR_RSC_ACCESS_ID(i);
5290 
5291 		forall i in l_rsc_access_rec.TERR_RSC_ID.first..l_rsc_access_rec.TERR_RSC_ID.last
5292       	  update JTY_WEBADI_OTH_TERR_INTF jwot
5293     	  set status = x_return_status
5294     	  where exists
5295     	    (select 1 from JTY_WEBADI_RESOURCES jwr
5296     		 where jwr.LAY_SEQ_NUM = jwot.LAY_SEQ_NUM
5297     		   and jwr.header = jwot.header
5298     		   and jwr.user_sequence = jwot.user_sequence
5299     		   and jwr.interface_type = jwot.interface_type
5300 			   and jwr.terr_rsc_id = l_rsc_access_rec.terr_rsc_id(i))
5301     		and jwot.USER_SEQUENCE = p_user_sequence
5302     		and jwot.header = l_header
5303     		and jwot.interface_type = l_intf_type
5304     		and jwot.status is null;
5305 	  end if;
5306 
5307 	  exception
5308 	  when others then
5309 	    x_return_status := FND_API.G_RET_STS_ERROR;
5310 		fnd_message.clear;
5311    	  	fnd_message.set_name ('JTF', 'JTY_OTH_TERR_UPDATE_ACCESS');
5312    	  	X_Msg_Data := fnd_message.get();
5313 
5314   		forall i in l_rsc_access_rec.TERR_RSC_ID.first..l_rsc_access_rec.TERR_RSC_ID.last
5315       	  update JTY_WEBADI_OTH_TERR_INTF jwot
5316     	  set status = x_return_status,
5317 		  error_msg = x_msg_data
5318     	  where exists
5319     	    (select 1 from JTY_WEBADI_RESOURCES jwr
5320     		 where jwr.LAY_SEQ_NUM = jwot.LAY_SEQ_NUM
5321     		   and jwr.header = jwot.header
5322     		   and jwr.user_sequence = jwot.user_sequence
5323     		   and jwr.interface_type = jwot.interface_type
5324 			   and jwr.terr_rsc_id = l_rsc_access_rec.terr_rsc_id(i))
5325     		and jwot.USER_SEQUENCE = p_user_sequence
5326     		and jwot.header = l_header
5327     		and jwot.interface_type = l_intf_type
5328     		and jwot.status is null;
5329     END;
5330 
5331   exception
5332     when others then
5333 	  x_return_status := FND_API.G_RET_STS_ERROR;
5334 	  fnd_message.clear;
5335       fnd_message.set_name ('JTF', 'JTY_OTH_TERR_UPDATE_RSC');
5336       X_Msg_Data := fnd_message.get();
5337 
5338   	  update JTY_WEBADI_OTH_TERR_INTF jwot
5339 	  set status = x_return_status,
5340 	  error_msg = X_Msg_Data
5341 	  where exists
5342 	    (select 1 from JTY_WEBADI_RESOURCES jwr
5343 		 where jwr.LAY_SEQ_NUM = jwot.LAY_SEQ_NUM
5344 		   and jwr.header = jwot.header
5345 		   and jwr.user_sequence = jwot.user_sequence
5346 		   and jwr.interface_type = jwot.interface_type)
5347 		and jwot.USER_SEQUENCE = p_user_sequence
5348 		and jwot.header = l_header
5349 		and jwot.interface_type = l_intf_type
5350 		and jwot.status is null;
5351 
5352 END UPDATE_TERR_RSC;
5353 
5354 PROCEDURE UPDATE_TERR_DEF(
5355     x_errbuf            	  OUT NOCOPY VARCHAR2,
5356     x_retcode           	  OUT NOCOPY VARCHAR2,
5357     P_USER_SEQUENCE 			  	  IN  NUMBER,
5358 	--p_ORG_id						  IN  NUMBER,
5359 	p_usage_id						  IN  NUMBER,
5360 	p_user_id						  IN  NUMBER
5361 ) IS
5362 
5363   	--l_api_version_number      CONSTANT NUMBER   := 1.0;
5364   	x_return_status	 varchar2(255);
5365   	X_Msg_Count		 number;
5366   	X_Msg_Data		 varchar2(255);
5367 
5368     l_rowid	   varchar2(30) := null;
5369 	l_action_flag varchar2(1);
5370 	l_header	  varchar2(30);
5371 	l_intf_type	  varchar2(1) := 'U';
5372 	l_row_count	  number;
5373 	l_row_count2	  number;
5374 	l_sql			  varchar2(5000);
5375 	l_login_id 		  number := fnd_global.login_id;
5376 
5377 	l_status		  varchar2_tbl_type;
5378 	l_error_msg		  varchar2_tbl_type;
5379 	l_lay_seq_num	  number_tbl_type;
5380 
5381 BEGIN
5382 
5383   x_return_status := FND_API.G_RET_STS_SUCCESS;
5384   --l_action_flag := p_action_flag;
5385   --fnd_file.put_line( FND_FILE.OUTPUT, 'Program starting....., sequence: '||p_USER_SEQUENCE ||', user_id: '||p_user_id);
5386   --dbms_output.put_line('Program starting....., sequence: '||p_USER_SEQUENCE ||', user_id: '||p_user_id);
5387 
5388   begin
5389   -- remove old inserted values from the interface table
5390   -- added 05/26/2006, bug 5249085
5391     delete from JTY_WEBADI_OTH_TERR_INTF
5392     where interface_type = l_intf_type
5393       and status is not null;
5394     --dbms_output.put_line('removed old data, row count: '||SQL%ROWCOUNT);
5395 	commit;
5396 
5397 	exception
5398 	  when others then
5399 	    rollback;
5400   end;
5401 
5402   -- initial operating unit
5403   mo_global.init('JTF');
5404 
5405   -- get hierarchy for territory
5406   --dbms_output.put_line('get hierarchy');
5407   get_hierarchy
5408     ( p_user_sequence  => p_user_sequence,
5409   	  p_intf_type	   => l_intf_type,
5410 	  x_return_status  => x_return_status,
5411 	  x_msg_data	   => x_msg_data );
5412 
5413   --dbms_output.put_line('get hierarchy completed, status: ' ||x_return_status);
5414 
5415   if x_return_status = FND_API.G_RET_STS_SUCCESS then
5416       --dbms_output.put_line('set create record ');
5417       -- set all create records id to null
5418       SET_CREATE_RECORDS(
5419       p_user_sequence 	=> p_user_sequence,
5420       p_intf_type		=> l_intf_type);
5421 
5422       --dbms_output.put_line('update terr qual id');
5423       l_header := 'QUAL';
5424       UPDATE_TERR_QUAL_ID(
5425           P_USER_SEQUENCE	=> p_user_sequence,
5426     	  P_INTF_TYPE		=> l_intf_type,
5427     	  P_HEADER			=> l_header);
5428 
5429     -- validate all records
5430   --dbms_output.put_line('validating record');
5431     validate_territory_records
5432     ( p_user_sequence 		=> p_user_sequence,
5433       P_INTF_TYPE				=> l_intf_type,
5434    	  x_return_status			=> x_return_status,
5435    	  X_MSG_DATA				=> x_msg_data);
5436   --dbms_output.put_line('validating record completed, status: '||x_return_status);
5437 
5438     if x_return_status = FND_API.G_RET_STS_SUCCESS then
5439       -- process qualifier records
5440       --dbms_output.put_line(' Call delete records procedure...');
5441       l_action_flag := 'D';
5442 	  delete_records(
5443 	    P_USER_SEQUENCE => p_user_sequence,
5444 	    P_INTF_TYPE 	  => l_intf_type ,
5445 		p_action_flag	  => l_action_flag);
5446 
5447       -- process territory definition records
5448   	  --dbms_output.put_line(' process terr header records... ');
5449       l_action_flag := 'C';
5450       UPDATE_TERR(
5451         p_user_sequence    => p_user_sequence,
5452         p_action_flag	   => l_action_flag,
5453         x_return_status    => x_return_status,
5454         x_msg_data	  	   => x_msg_data);
5455 
5456       if x_return_status = FND_API.G_RET_STS_SUCCESS then
5457 	  -- process terr update
5458   	  --dbms_output.put_line(' process terr update... ');
5459         l_action_flag := 'U';
5460         UPDATE_TERR(
5461           p_user_sequence => p_user_sequence,
5462           p_action_flag	  => l_action_flag,
5463           x_return_status => x_return_status,
5464           x_msg_data	  => x_msg_data);
5465 
5466       if x_return_status = FND_API.G_RET_STS_SUCCESS then
5467         -- process resources
5468   	  --dbms_output.put_line(' process resource records');
5469         UPDATE_TERR_RSC(P_USER_SEQUENCE	 => p_user_sequence,
5470 		  --p_action_flag	  => l_action_flag,
5471           x_return_status => x_return_status,
5472           x_msg_data	  => x_msg_data);
5473 
5474     	if x_return_status = FND_API.G_RET_STS_SUCCESS then
5475         	-- process qualifier
5476   	  --dbms_output.put_line(' process qualifier records');
5477           UPDATE_TERR_QUAL(P_USER_SEQUENCE	 => p_user_sequence,
5478 		    --p_action_flag   => l_action_flag,
5479         	x_return_status => x_return_status,
5480         	x_msg_data	  	=> x_msg_data);
5481 		else
5482 		  select NVL(status,'S') status, NVL(error_msg,'Success') error_msg, lay_seq_num
5483 		  bulk collect into l_status, l_error_msg, l_lay_seq_num
5484 		  from JTY_WEBADI_OTH_TERR_INTF
5485 		  where interface_type = l_intf_type
5486   		    and user_sequence = p_user_sequence;
5487 
5488 		  -- rollback all updated info
5489 		  rollback;
5490 
5491 		  forall i in l_lay_seq_num.first..l_lay_seq_num.last
5492 		    update JTY_WEBADI_OTH_TERR_INTF
5493 		    set status = l_status(i),
5494 			error_msg = l_error_msg(i)
5495 			where lay_seq_num = l_lay_seq_num(i)
5496 			  and interface_type = l_intf_type
5497   		    and user_sequence = p_user_sequence;
5498     	end if;-- qualifier
5499 	  else
5500 		  select NVL(status,'S') status, NVL(error_msg,'Success') error_msg, lay_seq_num
5501 		  bulk collect into l_status, l_error_msg, l_lay_seq_num
5502 		  from JTY_WEBADI_OTH_TERR_INTF
5503 		  where interface_type = l_intf_type
5504   		    and user_sequence = p_user_sequence;
5505 
5506 		  -- rollback all updated info
5507 		  rollback;
5508 
5509 		  forall i in l_lay_seq_num.first..l_lay_seq_num.last
5510 		    update JTY_WEBADI_OTH_TERR_INTF
5511 		    set status = l_status(i),
5512 			error_msg = l_error_msg(i)
5513 			where lay_seq_num = l_lay_seq_num(i)
5514 			  and interface_type = l_intf_type
5515   		    and user_sequence = p_user_sequence;
5516 	  end if; -- resource
5517 	  else
5518 		  select NVL(status,'S') status, NVL(error_msg,'Success') error_msg, lay_seq_num
5519 		  bulk collect into l_status, l_error_msg, l_lay_seq_num
5520 		  from JTY_WEBADI_OTH_TERR_INTF
5521 		  where interface_type = l_intf_type
5522   		    and user_sequence = p_user_sequence;
5523 
5524 		  -- rollback all updated info
5525 		  rollback;
5526 
5527 		  forall i in l_lay_seq_num.first..l_lay_seq_num.last
5528 		    update JTY_WEBADI_OTH_TERR_INTF
5529 		    set status = l_status(i),
5530 			error_msg = l_error_msg(i)
5531 			where lay_seq_num = l_lay_seq_num(i)
5532 			  and interface_type = l_intf_type
5533   		    and user_sequence = p_user_sequence;
5534 	  end if; -- resource
5535 
5536 	  else
5537 	  select NVL(status,'S') status, NVL(error_msg,'Success') error_msg, lay_seq_num
5538 	  bulk collect into l_status, l_error_msg, l_lay_seq_num
5539 	  from JTY_WEBADI_OTH_TERR_INTF
5540 	  where interface_type = l_intf_type
5541  		and user_sequence = p_user_sequence;
5542 
5543 	  -- rollback all updated info
5544 	  rollback;
5545 
5546 	  forall i in l_lay_seq_num.first..l_lay_seq_num.last
5547 	    update JTY_WEBADI_OTH_TERR_INTF
5548 	    set status = l_status(i),
5549 		error_msg = l_error_msg(i)
5550 		where lay_seq_num = l_lay_seq_num(i)
5551 		  and interface_type = l_intf_type
5552  		  and user_sequence = p_user_sequence;
5553 	end if; -- territory
5554   else
5555     select NVL(status,'S') status, NVL(error_msg,'Success') error_msg, lay_seq_num
5556     bulk collect into l_status, l_error_msg, l_lay_seq_num
5557     from JTY_WEBADI_OTH_TERR_INTF
5558     where interface_type = l_intf_type
5559   	  and user_sequence = p_user_sequence;
5560 
5561     -- rollback all updated info
5562     rollback;
5563 
5564     forall i in l_lay_seq_num.first..l_lay_seq_num.last
5565       update JTY_WEBADI_OTH_TERR_INTF
5566       set status = l_status(i),
5567   	  error_msg = l_error_msg(i)
5568   	  where lay_seq_num = l_lay_seq_num(i)
5569   	    and interface_type = l_intf_type
5570   		and user_sequence = p_user_sequence;
5571   end if;  -- validation
5572 
5573   x_errbuf := FND_API.G_RET_STS_SUCCESS;
5574   x_retcode := 0;
5575   --commit;
5576 
5577     exception
5578       WHEN others THEN
5579       	  --fnd_file.put_line( FND_FILE.OUTPUT,'Exception: Others in upload');
5580       	  --fnd_file.put_line( FND_FILE.OUTPUT,' SQLERRM: ' || SQLERRM);
5581       	  --dbms_output.put_line( 'Exception: Others in upload');
5582       	  --dbms_output.put_line( ' SQLERRM: ' || SQLERRM);
5583 		x_errbuf := sqlcode||': '||SQLERRM;
5584 		x_retcode := 2;
5585 
5586 END UPDATE_TERR_DEF;
5587 
5588 END JTY_WEBADI_OTH_TERR_UPDATE_PKG;