DBA Data[Home] [Help]

PACKAGE BODY: APPS.AHL_PRD_WORKORDER_PVT

Source


1 PACKAGE BODY AHL_PRD_WORKORDER_PVT AS
2  /* $Header: AHLVPRJB.pls 120.32.12010000.5 2009/01/12 23:31:35 sikumar ship $ */
3 
4 G_PKG_NAME   VARCHAR2(30)  := 'AHL_PRD_WORKORDER_PVT';
5 G_DEBUG      VARCHAR2(1)   := AHL_DEBUG_PUB.is_log_enabled;
6 
7 -- Operation Statuses
8 G_OP_STATUS_UNCOMPLETE VARCHAR2(2) := '2'; --Uncomplete
9 G_OP_STATUS_COMPLETE   VARCHAR2(2) := '1'; --Complete
10 
11 -- Job Statuses
12 G_JOB_STATUS_UNRELEASED VARCHAR2(2) := '1'; --Unreleased
13 G_JOB_STATUS_RELEASED VARCHAR2(2) := '3'; --Released
14 G_JOB_STATUS_COMPLETE VARCHAR2(2) := '4'; --Complete
15 G_JOB_STATUS_COMPLETE_NC VARCHAR2(2) := '5'; --Complete No Charges
16 G_JOB_STATUS_ON_HOLD VARCHAR2(2) := '6'; --On Hold
17 G_JOB_STATUS_CANCELLED VARCHAR2(2) := '7'; --Cancelled
18 G_JOB_STATUS_CLOSED VARCHAR2(2) := '12'; --Closed
19 G_JOB_STATUS_DRAFT VARCHAR2(2) := '17'; --Draft
20 G_JOB_STATUS_PARTS_HOLD VARCHAR2(2) := '19'; --Parts Hold
21 G_JOB_STATUS_QA_PENDING VARCHAR2(2) := '20'; --Pending QA Approval
22 G_JOB_STATUS_DEFERRAL_PENDING VARCHAR2(2) := '21'; --Pending Deferr
23 G_JOB_STATUS_DELETED VARCHAR2(2) := '22'; --Deleted
24 
25 -- MR Statuses
26 G_MR_STATUS_SIGNED_OFF VARCHAR2(30) := 'ACCOMPLISHED'; --Signed Off
27 G_MR_STATUS_DEFERRED VARCHAR2(30) := 'DEFERRED'; --Deferred
28 G_MR_STATUS_DEFERRAL_PENDING VARCHAR2(30) := 'DEFERRAL_PENDING'; --Deferral Pending
29 G_MR_STATUS_TERMINATED VARCHAR2(30) := 'TERMINATED'; --Terminated
30 G_MR_STATUS_CANCELLED VARCHAR2(30) := 'CANCELLED'; --Cancelled
31 G_MR_STATUS_JOBS_CANCELLED VARCHAR2(30) := 'ALL_JOBS_CANCELLED'; --All Jobs Cancelled
32 G_CALLED_FROM VARCHAR2(30) := NULL; --When Called from VWP For Department Change
33 
34 FUNCTION get_date_and_time(p_date IN DATE,
35                            p_date_hh24 IN VARCHAR2,
36                            p_date_mi IN VARCHAR2,
37                            p_date_ss IN VARCHAR2) RETURN DATE;
38 
39 
40 PROCEDURE log_path(
41   x_output_dir      OUT NOCOPY VARCHAR2
42 
43 	  )
44 	IS
45 		        l_full_path     VARCHAR2(512);
46 			l_new_full_path         VARCHAR2(512);
47 			l_file_dir      VARCHAR2(512);
48 
49 			fileHandler     UTL_FILE.FILE_TYPE;
50 			fileName        VARCHAR2(50);
51 
52 			l_flag          NUMBER;
53 	BEGIN
54 	           fileName:='test.log';--this is only a dummy filename to check if directory is valid or not
55 
56 
57         	   /* get output directory path from database */
58 			SELECT value
59 			INTO   l_full_path
60 			FROM   v$parameter
61 			WHERE  name = 'utl_file_dir';
62 
63 			l_flag := 0;
64 			--l_full_path contains a list of comma-separated directories
65 			WHILE(TRUE)
66 			LOOP
67 					    --get the first dir in the list
68 					    SELECT trim(substr(l_full_path, 1, decode(instr(l_full_path,',')-1,
69 											  -1, length(l_full_path),
70 											  instr(l_full_path, ',')-1
71 											 )
72 								  )
73 							   )
74 					    INTO  l_file_dir
75 					    FROM  dual;
76 
77 					    -- check if the dir is valid
78 					    BEGIN
79 						    fileHandler := UTL_FILE.FOPEN(l_file_dir , filename, 'w');
80 						    l_flag := 1;
81 					    EXCEPTION
82 						    WHEN utl_file.invalid_path THEN
83 							l_flag := 0;
84 						    WHEN utl_file.invalid_operation THEN
85 							l_flag := 0;
86 					    END;
87 
88 					    IF l_flag = 1 THEN --got a valid directory
89 						utl_file.fclose(fileHandler);
90 						EXIT;
91 					    END IF;
92 
93 					    --earlier found dir was not a valid dir,
94 					    --so remove that from the list, and get the new list
95 					    l_new_full_path := trim(substr(l_full_path, instr(l_full_path, ',')+1, length(l_full_path)));
96 
97 					    --if the new list has not changed, there are no more valid dirs left
98 					    IF l_full_path = l_new_full_path THEN
99 						    l_flag:=0;
100 						    EXIT;
101 					    END IF;
102 					     l_full_path := l_new_full_path;
103 			 END LOOP;
104 
105 
106 			 IF(l_flag=1) THEN --found a valid directory
107 			     x_output_dir := l_file_dir;
108 
109 			  ELSE
110 			      x_output_dir:= null;
111 
112 			  END IF;
113          EXCEPTION
114               WHEN OTHERS THEN
115                   x_output_dir := null;
116 
117 	END log_path;
118 PROCEDURE set_eam_debug_params
119 (
120   x_debug           OUT NOCOPY VARCHAR2,
121   x_output_dir      OUT NOCOPY VARCHAR2,
122   x_debug_file_name OUT NOCOPY VARCHAR2,
123   x_debug_file_mode OUT NOCOPY VARCHAR2
124 )
125 AS
126 l_output_dir  VARCHAR2(512);
127 
128 BEGIN
129   x_debug := 'Y';
130 
131 --  SELECT trim(substr(VALUE, 1, DECODE( instr( VALUE, ','), 0, length( VALUE), instr( VALUE, ',') -1 ) ) )
132 --  INTO   x_output_dir
133 --  FROM   V$PARAMETER
134 --  WHERE  NAME = 'utl_file_dir';
135   --Call log path
136  log_path( x_output_dir => l_output_dir);
137   --
138   x_output_dir := l_output_dir;
139   x_debug_file_name := 'EAMDEBUG.log';
140   x_debug_file_mode := 'a';
141 
142   IF ( G_DEBUG = 'Y' ) THEN
143     AHL_DEBUG_PUB.debug( 'Debug Log Directory Path:'||x_output_dir );
144     AHL_DEBUG_PUB.debug( 'Debug Log File Name:'||x_debug_file_name );
145   END IF;
146 
147 END set_eam_debug_params;
148 -- End of Modification
149 PROCEDURE default_missing_attributes
150 (
151   p_x_prd_workorder_rec   IN OUT NOCOPY prd_workorder_rec
152 )
153 As
154 
155 CURSOR get_workorder_rec(c_workorder_id NUMBER)
156 is
157 SELECT *
158 FROM   AHL_ALL_WORKORDERS_V
159 WHERE  workorder_id=c_workorder_id;
160 
161 l_prd_workorder_rec   AHL_ALL_WORKORDERS_V%ROWTYPE;
162 
163 BEGIN
164 
165   p_x_prd_workorder_rec.DML_OPERATION := 'U';
166 
167   OPEN  get_workorder_rec(p_x_prd_workorder_rec.workorder_id);
168   FETCH get_workorder_rec INTO l_prd_workorder_rec;
169   IF get_workorder_rec%NOTFOUND THEN
170     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_WO_NOT_FOUND');
171     FND_MSG_PUB.ADD;
172     CLOSE get_workorder_rec;
173     RETURN;
174   END IF;
175   CLOSE get_workorder_rec;
176 
177   IF G_CALLED_FROM = 'API' THEN
178       p_x_prd_workorder_rec.WIP_ENTITY_ID:=l_prd_workorder_rec.WIP_ENTITY_ID;
179       p_x_prd_workorder_rec.JOB_NUMBER:=l_prd_workorder_rec.JOB_NUMBER;
180       p_x_prd_workorder_rec.JOB_DESCRIPTION:=l_prd_workorder_rec.JOB_DESCRIPTION;
181 
182     ELSE
183 
184     IF p_x_prd_workorder_rec.WIP_ENTITY_ID= FND_API.G_MISS_NUM THEN
185       p_x_prd_workorder_rec.WIP_ENTITY_ID:=NULL;
186     ELSIF p_x_prd_workorder_rec.WIP_ENTITY_ID IS NULL THEN
187       p_x_prd_workorder_rec.WIP_ENTITY_ID:=l_prd_workorder_rec.WIP_ENTITY_ID;
188     END IF;
189 
190     IF p_x_prd_workorder_rec.JOB_NUMBER= FND_API.G_MISS_CHAR THEN
191       p_x_prd_workorder_rec.JOB_NUMBER:=NULL;
192     ELSIF p_x_prd_workorder_rec.JOB_NUMBER IS NULL THEN
193       p_x_prd_workorder_rec.JOB_NUMBER:=l_prd_workorder_rec.JOB_NUMBER;
194     END IF;
195 
196     IF p_x_prd_workorder_rec.JOB_DESCRIPTION= FND_API.G_MISS_CHAR THEN
197       p_x_prd_workorder_rec.JOB_DESCRIPTION:=NULL;
198     ELSIF p_x_prd_workorder_rec.JOB_DESCRIPTION IS NULL THEN
199       p_x_prd_workorder_rec.JOB_DESCRIPTION:=l_prd_workorder_rec.JOB_DESCRIPTION;
200     END IF;
201     END IF; --G_Called_from
202 
203 
204   IF p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER= FND_API.G_MISS_NUM THEN
205     p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER:=NULL;
206   ELSIF p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER IS NULL THEN
207     p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER:=l_prd_workorder_rec.OBJECT_VERSION_NUMBER;
208   END IF;
209 
210 
211   IF p_x_prd_workorder_rec.ORGANIZATION_ID= FND_API.G_MISS_NUM THEN
212     p_x_prd_workorder_rec.ORGANIZATION_ID:=NULL;
213   ELSIF p_x_prd_workorder_rec.ORGANIZATION_ID IS NULL THEN
214     p_x_prd_workorder_rec.ORGANIZATION_ID:=l_prd_workorder_rec.ORGANIZATION_ID;
215   END IF;
216 
217 
218 
219   IF p_x_prd_workorder_rec.ORGANIZATION_NAME= FND_API.G_MISS_CHAR THEN
220     p_x_prd_workorder_rec.ORGANIZATION_NAME:=NULL;
221   ELSIF p_x_prd_workorder_rec.ORGANIZATION_NAME IS NULL THEN
222     p_x_prd_workorder_rec.ORGANIZATION_NAME:=l_prd_workorder_rec.ORGANIZATION_NAME;
223   END IF;
224 
225   IF p_x_prd_workorder_rec.FIRM_PLANNED_FLAG= FND_API.G_MISS_NUM THEN
226     p_x_prd_workorder_rec.FIRM_PLANNED_FLAG:=NULL;
227   ELSIF p_x_prd_workorder_rec.FIRM_PLANNED_FLAG IS NULL THEN
228     p_x_prd_workorder_rec.FIRM_PLANNED_FLAG:=l_prd_workorder_rec.FIRM_PLANNED_FLAG;
229   END IF;
230 
231   IF p_x_prd_workorder_rec.CLASS_CODE= FND_API.G_MISS_CHAR THEN
232     p_x_prd_workorder_rec.CLASS_CODE:=NULL;
233   ELSIF p_x_prd_workorder_rec.CLASS_CODE IS NULL THEN
234     p_x_prd_workorder_rec.CLASS_CODE:=l_prd_workorder_rec.CLASS_CODE;
235   END IF;
236 
237   IF G_CALLED_FROM = 'API' THEN
238 
239     IF p_x_prd_workorder_rec.DEPARTMENT_ID= FND_API.G_MISS_NUM THEN
240       p_x_prd_workorder_rec.DEPARTMENT_ID:=NULL;
241     ELSIF p_x_prd_workorder_rec.DEPARTMENT_ID IS NULL THEN
242       p_x_prd_workorder_rec.DEPARTMENT_ID:=l_prd_workorder_rec.DEPARTMENT_ID;
243     END IF;
244 
245     ELSE
246 
247     IF p_x_prd_workorder_rec.DEPARTMENT_NAME= FND_API.G_MISS_CHAR THEN
248       p_x_prd_workorder_rec.DEPARTMENT_NAME:=NULL;
249     ELSIF p_x_prd_workorder_rec.DEPARTMENT_NAME IS NULL THEN
250       p_x_prd_workorder_rec.DEPARTMENT_NAME:=l_prd_workorder_rec.DEPARTMENT_NAME;
251     END IF;
252 
253     IF p_x_prd_workorder_rec.DEPARTMENT_ID= FND_API.G_MISS_NUM THEN
254       p_x_prd_workorder_rec.DEPARTMENT_ID:=NULL;
255     ELSIF p_x_prd_workorder_rec.DEPARTMENT_ID IS NULL THEN
256       p_x_prd_workorder_rec.DEPARTMENT_ID:=l_prd_workorder_rec.DEPARTMENT_ID;
257     END IF;
258 
259     END IF;
260 
261   IF p_x_prd_workorder_rec.STATUS_CODE= FND_API.G_MISS_CHAR THEN
262     p_x_prd_workorder_rec.STATUS_CODE:=NULL;
263   ELSIF p_x_prd_workorder_rec.STATUS_CODE IS NULL THEN
264     p_x_prd_workorder_rec.STATUS_CODE:=l_prd_workorder_rec.job_STATUS_CODE;
265   END IF;
266 
267   IF p_x_prd_workorder_rec.STATUS_MEANING= FND_API.G_MISS_CHAR THEN
268     p_x_prd_workorder_rec.STATUS_MEANING:=NULL;
269   ELSIF p_x_prd_workorder_rec.STATUS_MEANING IS NULL THEN
270     p_x_prd_workorder_rec.STATUS_MEANING:=l_prd_workorder_rec.JOB_STATUS_MEANING;
271   END IF;
272 
273   IF p_x_prd_workorder_rec.SCHEDULED_START_DATE=FND_API.G_MISS_DATE THEN
274     p_x_prd_workorder_rec.SCHEDULED_START_DATE:=NULL;
275   ELSIF p_x_prd_workorder_rec.SCHEDULED_START_DATE IS NULL THEN
276     p_x_prd_workorder_rec.SCHEDULED_START_DATE:=l_prd_workorder_rec.SCHEDULED_START_DATE;
277   END IF;
278 
279   IF p_x_prd_workorder_rec.SCHEDULED_START_HR=FND_API.G_MISS_NUM THEN
280     p_x_prd_workorder_rec.SCHEDULED_START_HR:=NULL;
281   ELSIF p_x_prd_workorder_rec.SCHEDULED_START_HR IS NULL THEN
282     p_x_prd_workorder_rec.SCHEDULED_START_HR:=l_prd_workorder_rec.SCHEDULED_START_HR;
283   END IF;
284 
285   IF p_x_prd_workorder_rec.SCHEDULED_START_MI=FND_API.G_MISS_NUM THEN
286     p_x_prd_workorder_rec.SCHEDULED_START_MI:=NULL;
287   ELSIF p_x_prd_workorder_rec.SCHEDULED_START_MI IS NULL THEN
288     p_x_prd_workorder_rec.SCHEDULED_START_MI:=l_prd_workorder_rec.SCHEDULED_START_MI;
289   END IF;
290 
291   IF p_x_prd_workorder_rec.SCHEDULED_END_DATE=FND_API.G_MISS_DATE THEN
292     p_x_prd_workorder_rec.SCHEDULED_END_DATE:=NULL;
293   ELSIF p_x_prd_workorder_rec.SCHEDULED_END_DATE IS NULL THEN
294     p_x_prd_workorder_rec.SCHEDULED_END_DATE:=l_prd_workorder_rec.SCHEDULED_END_DATE;
295   END IF;
296 
297   IF p_x_prd_workorder_rec.SCHEDULED_END_HR=FND_API.G_MISS_NUM THEN
298     p_x_prd_workorder_rec.SCHEDULED_END_HR:=NULL;
299   ELSIF p_x_prd_workorder_rec.SCHEDULED_END_HR IS NULL THEN
300     p_x_prd_workorder_rec.SCHEDULED_END_HR:=l_prd_workorder_rec.SCHEDULED_END_HR;
301   END IF;
302 
303   IF p_x_prd_workorder_rec.SCHEDULED_END_MI=FND_API.G_MISS_NUM THEN
304     p_x_prd_workorder_rec.SCHEDULED_END_MI:=NULL;
305   ELSIF p_x_prd_workorder_rec.SCHEDULED_END_MI IS NULL THEN
306     p_x_prd_workorder_rec.SCHEDULED_END_MI:=l_prd_workorder_rec.SCHEDULED_END_MI;
307   END IF;
308 
309   IF p_x_prd_workorder_rec.ACTUAL_START_DATE=FND_API.G_MISS_DATE THEN
310     p_x_prd_workorder_rec.ACTUAL_START_DATE:=NULL;
311   ELSIF p_x_prd_workorder_rec.ACTUAL_START_DATE IS NULL THEN
312     p_x_prd_workorder_rec.ACTUAL_START_DATE:=l_prd_workorder_rec.ACTUAL_START_DATE;
313   END IF;
314 
315   IF p_x_prd_workorder_rec.ACTUAL_START_HR=FND_API.G_MISS_NUM THEN
316     p_x_prd_workorder_rec.ACTUAL_START_HR:=NULL;
317   ELSIF p_x_prd_workorder_rec.ACTUAL_START_HR IS NULL THEN
318     p_x_prd_workorder_rec.ACTUAL_START_HR:=l_prd_workorder_rec.ACTUAL_START_HR;
319   END IF;
320 
321   IF p_x_prd_workorder_rec.ACTUAL_START_MI=FND_API.G_MISS_NUM THEN
322     p_x_prd_workorder_rec.ACTUAL_START_MI:=NULL;
323   ELSIF p_x_prd_workorder_rec.ACTUAL_START_MI IS NULL THEN
324     p_x_prd_workorder_rec.ACTUAL_START_MI:=l_prd_workorder_rec.ACTUAL_START_MI;
325   END IF;
326 
327   IF p_x_prd_workorder_rec.ACTUAL_END_DATE=FND_API.G_MISS_DATE THEN
328     p_x_prd_workorder_rec.ACTUAL_END_DATE:=NULL;
329   ELSIF p_x_prd_workorder_rec.ACTUAL_END_DATE IS NULL THEN
330     p_x_prd_workorder_rec.ACTUAL_END_DATE:=l_prd_workorder_rec.ACTUAL_END_DATE;
331   END IF;
332 
333   IF p_x_prd_workorder_rec.ACTUAL_END_HR=FND_API.G_MISS_NUM THEN
334     p_x_prd_workorder_rec.ACTUAL_END_HR:=NULL;
335   ELSIF p_x_prd_workorder_rec.ACTUAL_END_HR IS NULL THEN
336     p_x_prd_workorder_rec.ACTUAL_END_HR:=l_prd_workorder_rec.ACTUAL_END_HR;
337   END IF;
338 
339   IF p_x_prd_workorder_rec.ACTUAL_END_MI=FND_API.G_MISS_NUM THEN
340     p_x_prd_workorder_rec.ACTUAL_END_MI:=NULL;
341   ELSIF p_x_prd_workorder_rec.ACTUAL_END_MI IS NULL THEN
342     p_x_prd_workorder_rec.ACTUAL_END_MI:=l_prd_workorder_rec.ACTUAL_END_MI;
343   END IF;
344 
345   IF p_x_prd_workorder_rec.INVENTORY_ITEM_ID= FND_API.G_MISS_NUM THEN
346     p_x_prd_workorder_rec.INVENTORY_ITEM_ID:=NULL;
347   ELSIF p_x_prd_workorder_rec.INVENTORY_ITEM_ID IS NULL THEN
348     p_x_prd_workorder_rec.INVENTORY_ITEM_ID:=l_prd_workorder_rec.INVENTORY_ITEM_ID;
349   END IF;
350 
351   IF p_x_prd_workorder_rec.ITEM_INSTANCE_ID= FND_API.G_MISS_NUM THEN
352     p_x_prd_workorder_rec.ITEM_INSTANCE_ID:=NULL;
353   ELSIF p_x_prd_workorder_rec.ITEM_INSTANCE_ID IS NULL THEN
354     p_x_prd_workorder_rec.ITEM_INSTANCE_ID:=l_prd_workorder_rec.ITEM_INSTANCE_ID;
355   END IF;
356 
357   IF p_x_prd_workorder_rec.UNIT_NAME= FND_API.G_MISS_CHAR THEN
358     p_x_prd_workorder_rec.UNIT_NAME:=NULL;
359   ELSIF p_x_prd_workorder_rec.UNIT_NAME IS NULL THEN
360     p_x_prd_workorder_rec.UNIT_NAME:=l_prd_workorder_rec.UNIT_NAME;
361   END IF;
362 
363   IF p_x_prd_workorder_rec.ITEM_INSTANCE_NUMBER= FND_API.G_MISS_CHAR THEN
364     p_x_prd_workorder_rec.ITEM_INSTANCE_NUMBER:=NULL;
365   ELSIF p_x_prd_workorder_rec.ITEM_INSTANCE_NUMBER IS NULL THEN
366     p_x_prd_workorder_rec.ITEM_INSTANCE_NUMBER:=l_prd_workorder_rec.ITEM_INSTANCE_NUMBER;
367   END IF;
368 
369   IF p_x_prd_workorder_rec.QUANTITY= FND_API.G_MISS_NUM THEN
370     p_x_prd_workorder_rec.QUANTITY:=NULL;
371   ELSIF p_x_prd_workorder_rec.QUANTITY IS NULL THEN
372     p_x_prd_workorder_rec.QUANTITY:=l_prd_workorder_rec.QUANTITY;
373   END IF;
374 
375   IF p_x_prd_workorder_rec.WO_PART_NUMBER= FND_API.G_MISS_CHAR THEN
376     p_x_prd_workorder_rec.WO_PART_NUMBER:=NULL;
377   ELSIF p_x_prd_workorder_rec.WO_PART_NUMBER IS NULL THEN
378     p_x_prd_workorder_rec.WO_PART_NUMBER:=l_prd_workorder_rec.WO_PART_NUMBER;
379   END IF;
380 
381   IF p_x_prd_workorder_rec.ITEM_DESCRIPTION= FND_API.G_MISS_CHAR THEN
382     p_x_prd_workorder_rec.ITEM_DESCRIPTION:=NULL;
383   ELSIF p_x_prd_workorder_rec.ITEM_DESCRIPTION IS NULL THEN
384     p_x_prd_workorder_rec.ITEM_DESCRIPTION:=l_prd_workorder_rec.ITEM_DESCRIPTION;
385   END IF;
386 
387   IF p_x_prd_workorder_rec.SERIAL_NUMBER= FND_API.G_MISS_CHAR THEN
388     p_x_prd_workorder_rec.SERIAL_NUMBER:=NULL;
389   ELSIF p_x_prd_workorder_rec.SERIAL_NUMBER IS NULL THEN
390     p_x_prd_workorder_rec.SERIAL_NUMBER:=l_prd_workorder_rec.SERIAL_NUMBER;
391   END IF;
392 
393   IF p_x_prd_workorder_rec.ITEM_INSTANCE_UOM= FND_API.G_MISS_CHAR THEN
394     p_x_prd_workorder_rec.ITEM_INSTANCE_UOM:=NULL;
395   ELSIF p_x_prd_workorder_rec.ITEM_INSTANCE_UOM IS NULL THEN
396     p_x_prd_workorder_rec.ITEM_INSTANCE_UOM:=l_prd_workorder_rec.ITEM_INSTANCE_UOM;
397   END IF;
398 
399   IF p_x_prd_workorder_rec.COMPLETION_SUBINVENTORY= FND_API.G_MISS_CHAR THEN
400     p_x_prd_workorder_rec.COMPLETION_SUBINVENTORY:=NULL;
401   ELSIF p_x_prd_workorder_rec.COMPLETION_SUBINVENTORY IS NULL THEN
402     p_x_prd_workorder_rec.COMPLETION_SUBINVENTORY:=l_prd_workorder_rec.COMPLETION_SUBINVENTORY;
403   END IF;
404 
405   IF p_x_prd_workorder_rec.COMPLETION_LOCATOR_ID= FND_API.G_MISS_NUM THEN
406     p_x_prd_workorder_rec.COMPLETION_LOCATOR_ID:=NULL;
407   ELSIF p_x_prd_workorder_rec.COMPLETION_LOCATOR_ID IS NULL THEN
408     p_x_prd_workorder_rec.COMPLETION_LOCATOR_ID:=l_prd_workorder_rec.COMPLETION_LOCATOR_ID;
409   END IF;
410 
411   IF p_x_prd_workorder_rec.VISIT_ID= FND_API.G_MISS_NUM THEN
412     p_x_prd_workorder_rec.VISIT_ID:=NULL;
413   ELSIF p_x_prd_workorder_rec.VISIT_ID IS NULL THEN
414     p_x_prd_workorder_rec.VISIT_ID:=l_prd_workorder_rec.VISIT_ID;
415   END IF;
416 
417   IF p_x_prd_workorder_rec.VISIT_NUMBER= FND_API.G_MISS_NUM THEN
418     p_x_prd_workorder_rec.VISIT_NUMBER:=NULL;
419   ELSIF p_x_prd_workorder_rec.VISIT_NUMBER IS NULL THEN
420     p_x_prd_workorder_rec.VISIT_NUMBER:=l_prd_workorder_rec.VISIT_NUMBER;
421   END IF;
422 
423   IF p_x_prd_workorder_rec.VISIT_NAME= FND_API.G_MISS_CHAR THEN
424     p_x_prd_workorder_rec.VISIT_NAME:=NULL;
425   ELSIF p_x_prd_workorder_rec.VISIT_NAME IS NULL THEN
426     p_x_prd_workorder_rec.VISIT_NAME:=l_prd_workorder_rec.VISIT_NAME;
427   END IF;
428 
429   IF p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG= FND_API.G_MISS_CHAR THEN
430     p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG:=NULL;
431   ELSIF p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG IS NULL THEN
432     p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG:=l_prd_workorder_rec.MASTER_WORKORDER_FLAG;
433   END IF;
434 
435   IF p_x_prd_workorder_rec.VISIT_TASK_ID= FND_API.G_MISS_NUM THEN
436     p_x_prd_workorder_rec.VISIT_TASK_ID:=NULL;
437   ELSIF p_x_prd_workorder_rec.VISIT_TASK_ID IS NULL THEN
438     p_x_prd_workorder_rec.VISIT_TASK_ID:=l_prd_workorder_rec.VISIT_TASK_ID;
439   END IF;
440 
441   IF p_x_prd_workorder_rec.MR_HEADER_ID= FND_API.G_MISS_NUM THEN
442     p_x_prd_workorder_rec.MR_HEADER_ID:=NULL;
443   ELSIF p_x_prd_workorder_rec.MR_HEADER_ID IS NULL THEN
444     p_x_prd_workorder_rec.MR_HEADER_ID:=l_prd_workorder_rec.MR_HEADER_ID;
445   END IF;
446 
447   IF p_x_prd_workorder_rec.VISIT_TASK_NUMBER= FND_API.G_MISS_NUM THEN
448     p_x_prd_workorder_rec.VISIT_TASK_NUMBER:=NULL;
449   ELSIF p_x_prd_workorder_rec.VISIT_TASK_NUMBER IS NULL THEN
450     p_x_prd_workorder_rec.VISIT_TASK_NUMBER:=l_prd_workorder_rec.VISIT_TASK_NUMBER;
451   END IF;
452 
453   IF p_x_prd_workorder_rec.MR_TITLE= FND_API.G_MISS_CHAR THEN
454     p_x_prd_workorder_rec.MR_TITLE:=NULL;
455   ELSIF p_x_prd_workorder_rec.MR_TITLE IS NULL THEN
456     p_x_prd_workorder_rec.MR_TITLE:=l_prd_workorder_rec.MR_TITLE;
457   END IF;
458 
459   IF p_x_prd_workorder_rec.SERVICE_ITEM_ID= FND_API.G_MISS_NUM THEN
460     p_x_prd_workorder_rec.SERVICE_ITEM_ID:=NULL;
461   ELSIF p_x_prd_workorder_rec.SERVICE_ITEM_ID IS NULL THEN
462     p_x_prd_workorder_rec.SERVICE_ITEM_ID:=l_prd_workorder_rec.SERVICE_ITEM_ID;
463   END IF;
464 
465   IF p_x_prd_workorder_rec.SERVICE_ITEM_ORG_ID= FND_API.G_MISS_NUM THEN
466     p_x_prd_workorder_rec.SERVICE_ITEM_ORG_ID:=NULL;
467   ELSIF p_x_prd_workorder_rec.SERVICE_ITEM_ORG_ID IS NULL THEN
468     p_x_prd_workorder_rec.SERVICE_ITEM_ORG_ID:=l_prd_workorder_rec.SERVICE_ITEM_ORG_ID;
469   END IF;
470 
471   IF p_x_prd_workorder_rec.SERVICE_ITEM_DESCRIPTION= FND_API.G_MISS_CHAR THEN
472     p_x_prd_workorder_rec.SERVICE_ITEM_DESCRIPTION:=NULL;
473   ELSIF p_x_prd_workorder_rec.SERVICE_ITEM_DESCRIPTION IS NULL THEN
474     p_x_prd_workorder_rec.SERVICE_ITEM_DESCRIPTION:=l_prd_workorder_rec.SERVICE_ITEM_DESCRIPTION;
475   END IF;
476 
477   IF p_x_prd_workorder_rec.SERVICE_ITEM_NUMBER= FND_API.G_MISS_CHAR THEN
478     p_x_prd_workorder_rec.SERVICE_ITEM_NUMBER:=NULL;
479   ELSIF p_x_prd_workorder_rec.SERVICE_ITEM_NUMBER IS NULL THEN
480     p_x_prd_workorder_rec.SERVICE_ITEM_NUMBER:=l_prd_workorder_rec.SERVICE_ITEM_NUMBER;
481   END IF;
482 
483   IF p_x_prd_workorder_rec.SERVICE_ITEM_UOM= FND_API.G_MISS_CHAR THEN
484     p_x_prd_workorder_rec.SERVICE_ITEM_UOM:=NULL;
485   ELSIF p_x_prd_workorder_rec.SERVICE_ITEM_UOM IS NULL THEN
486     p_x_prd_workorder_rec.SERVICE_ITEM_UOM:=l_prd_workorder_rec.SERVICE_ITEM_UOM;
487   END IF;
488 
489   IF p_x_prd_workorder_rec.PROJECT_ID= FND_API.G_MISS_NUM THEN
490     p_x_prd_workorder_rec.PROJECT_ID:=NULL;
491   ELSIF p_x_prd_workorder_rec.PROJECT_ID IS NULL THEN
492     p_x_prd_workorder_rec.PROJECT_ID:=l_prd_workorder_rec.PROJECT_ID;
493   END IF;
494 
495   IF p_x_prd_workorder_rec.PROJECT_TASK_ID= FND_API.G_MISS_NUM THEN
496     p_x_prd_workorder_rec.PROJECT_TASK_ID:=NULL;
497   ELSIF p_x_prd_workorder_rec.PROJECT_TASK_ID IS NULL THEN
498     p_x_prd_workorder_rec.PROJECT_TASK_ID:=l_prd_workorder_rec.PROJECT_TASK_ID;
499   END IF;
500 
501   IF p_x_prd_workorder_rec.INCIDENT_ID= FND_API.G_MISS_NUM THEN
502     p_x_prd_workorder_rec.INCIDENT_ID:=NULL;
503   ELSIF p_x_prd_workorder_rec.INCIDENT_ID IS NULL THEN
504     p_x_prd_workorder_rec.INCIDENT_ID:=l_prd_workorder_rec.INCIDENT_ID;
505   END IF;
506 
507   IF p_x_prd_workorder_rec.UNIT_EFFECTIVITY_ID= FND_API.G_MISS_NUM THEN
508     p_x_prd_workorder_rec.UNIT_EFFECTIVITY_ID:=NULL;
509   ELSIF p_x_prd_workorder_rec.UNIT_EFFECTIVITY_ID IS NULL THEN
510     p_x_prd_workorder_rec.UNIT_EFFECTIVITY_ID:=l_prd_workorder_rec.UNIT_EFFECTIVITY_ID;
511   END IF;
512 
513   IF p_x_prd_workorder_rec.PLAN_ID= FND_API.G_MISS_NUM THEN
514     p_x_prd_workorder_rec.PLAN_ID:=NULL;
515   ELSIF p_x_prd_workorder_rec.PLAN_ID IS NULL THEN
516     p_x_prd_workorder_rec.PLAN_ID:=l_prd_workorder_rec.PLAN_ID;
517   END IF;
518 
519   IF p_x_prd_workorder_rec.COLLECTION_ID= FND_API.G_MISS_NUM THEN
520     p_x_prd_workorder_rec.COLLECTION_ID:=NULL;
521   ELSIF p_x_prd_workorder_rec.COLLECTION_ID IS NULL THEN
522     p_x_prd_workorder_rec.COLLECTION_ID:=l_prd_workorder_rec.COLLECTION_ID;
523   END IF;
524 
525   IF p_x_prd_workorder_rec.JOB_PRIORITY= FND_API.G_MISS_NUM THEN
526     p_x_prd_workorder_rec.JOB_PRIORITY:=NULL;
527   ELSIF p_x_prd_workorder_rec.JOB_PRIORITY IS NULL THEN
528     p_x_prd_workorder_rec.JOB_PRIORITY:=l_prd_workorder_rec.PRIORITY;
529   END IF;
530 
531   IF p_x_prd_workorder_rec.JOB_PRIORITY_MEANING= FND_API.G_MISS_CHAR THEN
532     p_x_prd_workorder_rec.JOB_PRIORITY_MEANING:=NULL;
533   ELSIF p_x_prd_workorder_rec.JOB_PRIORITY_MEANING IS NULL THEN
534     p_x_prd_workorder_rec.JOB_PRIORITY_MEANING:=l_prd_workorder_rec.PRIORITY_MEANING;
535   END IF;
536 
537 		IF p_x_prd_workorder_rec.CONFIRM_FAILURE_FLAG = FND_API.G_MISS_CHAR THEN
538     p_x_prd_workorder_rec.CONFIRM_FAILURE_FLAG :=NULL;
539   ELSIF p_x_prd_workorder_rec.CONFIRM_FAILURE_FLAG  IS NULL THEN
540     p_x_prd_workorder_rec.CONFIRM_FAILURE_FLAG :=l_prd_workorder_rec.CONFIRM_FAILURE_FLAG;
541   END IF;
542   IF p_x_prd_workorder_rec.ATTRIBUTE_CATEGORY= FND_API.G_MISS_CHAR THEN
543     p_x_prd_workorder_rec.ATTRIBUTE_CATEGORY:=NULL;
544   ELSIF p_x_prd_workorder_rec.ATTRIBUTE_CATEGORY IS NULL THEN
545     p_x_prd_workorder_rec.ATTRIBUTE_CATEGORY:=l_prd_workorder_rec.ATTRIBUTE_CATEGORY;
546   END IF;
547 
548   IF p_x_prd_workorder_rec.ATTRIBUTE1= FND_API.G_MISS_CHAR THEN
549     p_x_prd_workorder_rec.ATTRIBUTE1:=NULL;
550   ELSIF p_x_prd_workorder_rec.ATTRIBUTE1 IS NULL THEN
551     p_x_prd_workorder_rec.ATTRIBUTE1:=l_prd_workorder_rec.ATTRIBUTE1;
552   END IF;
553 
554   IF p_x_prd_workorder_rec.ATTRIBUTE2= FND_API.G_MISS_CHAR THEN
555     p_x_prd_workorder_rec.ATTRIBUTE2:=NULL;
556   ELSIF p_x_prd_workorder_rec.ATTRIBUTE2 IS NULL THEN
557     p_x_prd_workorder_rec.ATTRIBUTE2:=l_prd_workorder_rec.ATTRIBUTE2;
558   END IF;
559 
560   IF p_x_prd_workorder_rec.ATTRIBUTE3= FND_API.G_MISS_CHAR THEN
561     p_x_prd_workorder_rec.ATTRIBUTE3:=NULL;
562   ELSIF p_x_prd_workorder_rec.ATTRIBUTE3 IS NULL THEN
563     p_x_prd_workorder_rec.ATTRIBUTE3:=l_prd_workorder_rec.ATTRIBUTE3;
564   END IF;
565 
566   IF p_x_prd_workorder_rec.ATTRIBUTE4= FND_API.G_MISS_CHAR THEN
567     p_x_prd_workorder_rec.ATTRIBUTE4:=NULL;
568   ELSIF p_x_prd_workorder_rec.ATTRIBUTE4 IS NULL THEN
569     p_x_prd_workorder_rec.ATTRIBUTE4:=l_prd_workorder_rec.ATTRIBUTE4;
570   END IF;
571 
572   IF p_x_prd_workorder_rec.ATTRIBUTE5= FND_API.G_MISS_CHAR THEN
573     p_x_prd_workorder_rec.ATTRIBUTE5:=NULL;
574   ELSIF p_x_prd_workorder_rec.ATTRIBUTE5 IS NULL THEN
575     p_x_prd_workorder_rec.ATTRIBUTE5:=l_prd_workorder_rec.ATTRIBUTE5;
576   END IF;
577 
578   IF p_x_prd_workorder_rec.ATTRIBUTE6= FND_API.G_MISS_CHAR THEN
579     p_x_prd_workorder_rec.ATTRIBUTE6:=NULL;
580   ELSIF p_x_prd_workorder_rec.ATTRIBUTE6 IS NULL THEN
581     p_x_prd_workorder_rec.ATTRIBUTE6:=l_prd_workorder_rec.ATTRIBUTE6;
582   END IF;
583 
584   IF p_x_prd_workorder_rec.ATTRIBUTE7= FND_API.G_MISS_CHAR THEN
585     p_x_prd_workorder_rec.ATTRIBUTE7:=NULL;
586   ELSIF p_x_prd_workorder_rec.ATTRIBUTE7 IS NULL THEN
587     p_x_prd_workorder_rec.ATTRIBUTE7:=l_prd_workorder_rec.ATTRIBUTE7;
588   END IF;
589 
590   IF p_x_prd_workorder_rec.ATTRIBUTE8= FND_API.G_MISS_CHAR THEN
591     p_x_prd_workorder_rec.ATTRIBUTE8:=NULL;
592   ELSIF p_x_prd_workorder_rec.ATTRIBUTE8 IS NULL THEN
593     p_x_prd_workorder_rec.ATTRIBUTE8:=l_prd_workorder_rec.ATTRIBUTE8;
594   END IF;
595 
596   IF p_x_prd_workorder_rec.ATTRIBUTE9= FND_API.G_MISS_CHAR THEN
597     p_x_prd_workorder_rec.ATTRIBUTE9:=NULL;
598   ELSIF p_x_prd_workorder_rec.ATTRIBUTE9 IS NULL THEN
599     p_x_prd_workorder_rec.ATTRIBUTE9:=l_prd_workorder_rec.ATTRIBUTE9;
600   END IF;
601 
602   IF p_x_prd_workorder_rec.ATTRIBUTE10= FND_API.G_MISS_CHAR THEN
603     p_x_prd_workorder_rec.ATTRIBUTE10:=NULL;
604   ELSIF p_x_prd_workorder_rec.ATTRIBUTE10 IS NULL THEN
605     p_x_prd_workorder_rec.ATTRIBUTE10:=l_prd_workorder_rec.ATTRIBUTE10;
606   END IF;
607 
608   IF p_x_prd_workorder_rec.ATTRIBUTE11= FND_API.G_MISS_CHAR THEN
609     p_x_prd_workorder_rec.ATTRIBUTE11:=NULL;
610   ELSIF p_x_prd_workorder_rec.ATTRIBUTE11 IS NULL THEN
611     p_x_prd_workorder_rec.ATTRIBUTE11:=l_prd_workorder_rec.ATTRIBUTE11;
612   END IF;
613 
614   IF p_x_prd_workorder_rec.ATTRIBUTE12= FND_API.G_MISS_CHAR THEN
615     p_x_prd_workorder_rec.ATTRIBUTE12:=NULL;
616   ELSIF p_x_prd_workorder_rec.ATTRIBUTE12 IS NULL THEN
617     p_x_prd_workorder_rec.ATTRIBUTE12:=l_prd_workorder_rec.ATTRIBUTE12;
618   END IF;
619 
620   IF p_x_prd_workorder_rec.ATTRIBUTE13= FND_API.G_MISS_CHAR THEN
621     p_x_prd_workorder_rec.ATTRIBUTE13:=NULL;
622   ELSIF p_x_prd_workorder_rec.ATTRIBUTE13 IS NULL THEN
623     p_x_prd_workorder_rec.ATTRIBUTE13:=l_prd_workorder_rec.ATTRIBUTE13;
624   END IF;
625 
626   IF p_x_prd_workorder_rec.ATTRIBUTE14= FND_API.G_MISS_CHAR THEN
627     p_x_prd_workorder_rec.ATTRIBUTE14:=NULL;
628   ELSIF p_x_prd_workorder_rec.ATTRIBUTE14 IS NULL THEN
629     p_x_prd_workorder_rec.ATTRIBUTE14:=l_prd_workorder_rec.ATTRIBUTE14;
630   END IF;
631 
632   IF p_x_prd_workorder_rec.ATTRIBUTE15= FND_API.G_MISS_CHAR THEN
633     p_x_prd_workorder_rec.ATTRIBUTE15:=NULL;
634   ELSIF p_x_prd_workorder_rec.ATTRIBUTE15 IS NULL THEN
635     p_x_prd_workorder_rec.ATTRIBUTE15:=l_prd_workorder_rec.ATTRIBUTE15;
636   END IF;
637 
638   IF p_x_prd_workorder_rec.HOLD_REASON_CODE = FND_API.G_MISS_CHAR THEN
639     p_x_prd_workorder_rec.HOLD_REASON_CODE:=NULL;
640   ELSIF p_x_prd_workorder_rec.HOLD_REASON_CODE IS NULL THEN
641     p_x_prd_workorder_rec.HOLD_REASON_CODE:=l_prd_workorder_rec.HOLD_REASON_CODE;
642   END IF;
643 
644 END default_missing_attributes;
645 
646 PROCEDURE convert_values_to_ids
647 (
648   p_x_prd_workorder_rec   IN OUT NOCOPY PRD_WORKORDER_REC
649 )
650 AS
651 
652 CURSOR get_quantity(c_instance_id NUMBER)
653 IS
654 SELECT quantity
655 FROM   CSI_ITEM_INSTANCES
656 WHERE  instance_id=c_instance_id
657 AND    TRUNC(SYSDATE) BETWEEN TRUNC(NVL(ACTIVE_START_DATE,SYSDATE))
658                       AND TRUNC(NVL(ACTIVE_END_DATE,SYSDATE));
659 
660 CURSOR get_department(c_dept VARCHAR2,c_org_id NUMBER)
661 IS
662 SELECT department_id
663 FROM   BOM_DEPARTMENTS
664 WHERE  UPPER(description) LIKE UPPER(c_dept)
665 AND    ORganization_id=c_org_id;
666 -- bug 4143943
667 CURSOR get_wo_status(c_workorder_id NUMBER)
668 IS
669 SELECT STATUS_CODE
670 FROM AHL_WORKORDERS
671 WHERE workorder_id = c_workorder_id;
672 
673 -- Balaji added for Release NR error
674 CURSOR get_wo_sch_sec(c_wip_entity_id IN NUMBER)
675 IS
676 SELECT
677    TO_CHAR(scheduled_start_date, 'ss') schedule_start_sec,
678    TO_CHAR(scheduled_completion_date, 'ss') schedule_end_sec
679 FROM
680    WIP_DISCRETE_JOBS
681 WHERE
682    WIP_ENTITY_ID = c_wip_entity_id;
683 
684 -- Balaji added for Release NR error
685 CURSOR get_wo_act_sec(c_workorder_id IN NUMBER)
686 IS
687 SELECT
688    TO_CHAR(actual_start_date, 'ss') actual_start_sec,
689    TO_CHAR(actual_end_date, 'ss') actual_end_sec
690 FROM
691    AHL_WORKORDERS
692 WHERE
693    WORKORDER_ID = c_workorder_id;
694 
695 -- FP bug# 7631453
696 CURSOR get_hold_reason_code_csr(c_hold_reason VARCHAR2)
697 IS
698 SELECT Lookup_code
699 FROM FND_LOOKUPS
700 WHERE lookup_type = 'AHL_PRD_WO_HOLD_REASON'
701 AND MEANING = c_hold_reason;
702 -- End FP bug# 7631453
703 
704 l_ctr                   NUMBER:=0;
705 --l_hour                  VARCHAR2(30);
706 --l_minutes               VARCHAR2(30);
707 l_sec                   VARCHAR2(30);
708 --l_date_time             VARCHAR2(30);
709 l_wo_status             VARCHAR2(30);
710 l_sch_start_sec         VARCHAR2(30);
711 l_sch_end_sec           VARCHAR2(30);
712 l_act_start_sec         VARCHAR2(30);
713 l_act_end_sec           VARCHAR2(30);
714 
715 
716 BEGIN
717   IF  p_x_prd_workorder_rec.dml_operation='C' THEN
718 
719     IF p_x_prd_workorder_rec.QUANTITY IS NULL OR
720        p_x_prd_workorder_rec.QUANTITY=FND_API.G_MISS_CHAR THEN
721       OPEN  get_quantity(p_x_prd_workorder_rec.ITEM_INSTANCE_ID);
722       FETCH get_quantity INTO p_x_prd_workorder_rec.QUANTITY;
723       CLOSE get_quantity;
724     END IF;
725 
726     IF p_x_prd_workorder_rec.STATUS_CODE IS NULL OR
727        p_x_prd_workorder_rec.STATUS_CODE=FND_API.G_MISS_CHAR THEN
728       p_x_prd_workorder_rec.STATUS_CODE:=G_JOB_STATUS_UNRELEASED;  -- Job_Status_Code '1' UnReleseed
729     END IF;
730 
731     IF p_x_prd_workorder_rec.job_priority IS NULL OR
732        p_x_prd_workorder_rec.job_priority=FND_API.G_MISS_NUM THEN
733       p_x_prd_workorder_rec.job_priority:=NULL;
734     ELSIF p_x_prd_workorder_rec.job_priority IS not NULL AND
735           p_x_prd_workorder_rec.job_priority<>FND_API.G_MISS_NUM THEN
736 
737       SELECT COUNT(*) INTO l_ctr
738       FROM   MFG_LOOKUPS
739       WHERE  lookup_type='WIP_EAM_ACTIVITY_PRIORITY'
740       AND    lookup_code=p_x_prd_workorder_rec.job_priority;
741 
742       IF l_ctr=0 THEN
743         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_PRIORITY_INVALID');
744         FND_MSG_PUB.ADD;
745       END IF;
746     END IF;
747 
748   ELSIF  p_x_prd_workorder_rec.dml_operation='U' THEN
749     IF p_x_prd_workorder_rec.job_priority IS NULL OR
750        p_x_prd_workorder_rec.job_priority=FND_API.G_MISS_NUM THEN
751       p_x_prd_workorder_rec.job_priority:=NULL;
752     END IF;
753 
754     IF p_x_prd_workorder_rec.Department_Name IS not NULL AND
755        p_x_prd_workorder_rec.Department_Name<>FND_API.G_MISS_CHAR THEN
756 
757       OPEN get_department(p_x_prd_workorder_rec.Department_Name,p_x_prd_workorder_rec.Organization_id);
758       FETCH get_department INTO p_x_prd_workorder_rec.department_id;
759 
760       IF get_department%NOTFOUND THEN
761         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_DEPT_INVALID');
762         FND_MSG_PUB.ADD;
763       END IF;
764       CLOSE get_department;
765     END IF;
766 
767   END IF;
768 -- bug 4143943
769 		-- if the existing workorder status is 17
770 		-- that is the workorder is not being updated from the prod UI
771 		-- but is coming from VWP Push to Prod,
772 		-- then the scheduled start hr and min fields are not populated
773 		-- and are already part of the start and end dates
774 		-- so these should not be converted to 00:00
775 
776 		OPEN get_wo_status(p_x_prd_workorder_rec.workorder_id);
777 		FETCH get_wo_status INTO l_wo_status;
778 		CLOSE get_wo_status;
779 
780 		IF l_wo_status <> '17' THEN
781 
782   -- portion of the code to get workorder seconds from the DB.
783   -- These values will be used in case if its not passed from the UI
784   OPEN get_wo_sch_sec(p_x_prd_workorder_rec.wip_entity_id);
785   FETCH get_wo_sch_sec INTO l_sch_start_sec, l_sch_end_sec;
786   CLOSE get_wo_sch_sec;
787 
788   IF p_x_prd_workorder_rec.SCHEDULED_START_DATE IS NOT NULL AND
789      p_x_prd_workorder_rec.SCHEDULED_START_DATE <> FND_API.G_MISS_DATE
790      AND G_CALLED_FROM <> 'OAF' THEN
791 
792      -- Fix for error while releasing unreleased workorders
793     -- the seconds value needs to be taken into account for workorders, otherwise
794     -- this might lead to a discrepancy with the operation dates
795     l_sec :=  TO_CHAR(p_x_prd_workorder_rec.SCHEDULED_START_DATE, 'ss');
796     IF(l_sec = '00') THEN
797        l_sec := l_sch_start_sec;
798     END IF;
799     p_x_prd_workorder_rec.SCHEDULED_START_DATE :=
800                   get_date_and_time
801                                   (p_date => p_x_prd_workorder_rec.SCHEDULED_START_DATE,
802                                    p_date_hh24 => p_x_prd_workorder_rec.SCHEDULED_START_HR,
803                                    p_date_mi => p_x_prd_workorder_rec.SCHEDULED_START_MI,
804                                    p_date_ss => l_sec);
805 
806   END IF;
807   IF ( G_DEBUG = 'Y' ) THEN
808     AHL_DEBUG_PUB.debug( 'p_x_prd_workorder_rec.SCHEDULED_START_DATE : ' || to_char(p_x_prd_workorder_rec.SCHEDULED_START_DATE,'DD-MON-YY hh24:mi:ss') );
809   END IF;
810 
811   IF p_x_prd_workorder_rec.SCHEDULED_END_DATE IS NOT NULL AND
812      p_x_prd_workorder_rec.SCHEDULED_END_DATE <> FND_API.G_MISS_DATE
813      AND G_CALLED_FROM <> 'OAF' THEN
814 
815     -- Fix for error while releasing unreleased workorders
816     -- the seconds value needs to be taken into account for workorders, otherwise
817     -- this might lead to a discrepancy with the operation dates
818     l_sec := TO_CHAR(p_x_prd_workorder_rec.SCHEDULED_END_DATE, 'ss');
819 
820     IF(l_sec = '00') THEN
821        l_sec := l_sch_end_sec;
822     END IF;
823 
824     p_x_prd_workorder_rec.SCHEDULED_END_DATE :=
825                   get_date_and_time
826                                   (p_date => p_x_prd_workorder_rec.SCHEDULED_END_DATE,
827                                    p_date_hh24 => p_x_prd_workorder_rec.SCHEDULED_END_HR,
828                                    p_date_mi => p_x_prd_workorder_rec.SCHEDULED_END_MI,
829                                    p_date_ss => l_sec);
830 
831 
832 
833   END IF;
834   IF ( G_DEBUG = 'Y' ) THEN
835       AHL_DEBUG_PUB.debug( 'p_x_prd_workorder_rec.SCHEDULED_END_DATE : ' || to_char(p_x_prd_workorder_rec.SCHEDULED_END_DATE,'DD-MON-YY hh24:mi:ss') );
836      END IF;
837 END IF; -- l_wo_status <> '17'
838 
839   -- portion of the code to get workorder seconds from the DB.
840   -- These values will be used in case if its not passed from the UI
841   -- Balaji added for Release NR error
842   OPEN get_wo_act_sec(p_x_prd_workorder_rec.workorder_id);
843   FETCH get_wo_act_sec INTO l_act_start_sec, l_act_end_sec;
844   CLOSE get_wo_act_sec;
845 
846   IF p_x_prd_workorder_rec.ACTUAL_START_DATE IS NOT NULL AND
847      p_x_prd_workorder_rec.ACTUAL_START_DATE <> FND_API.G_MISS_DATE THEN
848 
849     l_sec := TO_CHAR(p_x_prd_workorder_rec.ACTUAL_START_DATE, 'ss');
850 
851     -- Balaji added for Release NR error
852     IF(l_sec = '00') THEN
853        l_sec := l_act_start_sec;
854     END IF;
855      p_x_prd_workorder_rec.ACTUAL_START_DATE :=
856                   get_date_and_time
857                                   (p_date => p_x_prd_workorder_rec.ACTUAL_START_DATE,
858                                    p_date_hh24 => p_x_prd_workorder_rec.ACTUAL_START_HR,
859                                    p_date_mi => p_x_prd_workorder_rec.ACTUAL_START_MI,
860                                    p_date_ss => l_sec);
861      IF ( G_DEBUG = 'Y' ) THEN
862       AHL_DEBUG_PUB.debug( 'p_x_prd_workorder_rec.ACTUAL_START_DATE : ' || to_char(p_x_prd_workorder_rec.ACTUAL_START_DATE,'DD-MON-YY hh24:mi:ss') );
863      END IF;
864 
865   END IF;
866 
867   IF p_x_prd_workorder_rec.ACTUAL_END_DATE IS NOT NULL AND
868      p_x_prd_workorder_rec.ACTUAL_END_DATE <> FND_API.G_MISS_DATE THEN
869 
870     l_sec := TO_CHAR(p_x_prd_workorder_rec.ACTUAL_END_DATE, 'ss');
871 
872     -- Balaji added for Release NR error
873     IF(l_sec = '00') THEN
874        l_sec := l_act_end_sec;
875     END IF;
876     p_x_prd_workorder_rec.ACTUAL_END_DATE :=
877                   get_date_and_time
878                                   (p_date => p_x_prd_workorder_rec.ACTUAL_END_DATE,
879                                    p_date_hh24 => p_x_prd_workorder_rec.ACTUAL_END_HR,
880                                    p_date_mi => p_x_prd_workorder_rec.ACTUAL_END_MI,
881                                    p_date_ss => l_sec);
882      IF ( G_DEBUG = 'Y' ) THEN
883       AHL_DEBUG_PUB.debug( 'p_x_prd_workorder_rec.ACTUAL_START_DATE : ' || to_char(p_x_prd_workorder_rec.ACTUAL_END_DATE,'DD-MON-YY hh24:mi:ss') );
884      END IF;
885 
886   END IF;
887 
888   -- FP bug# 7631453
889   IF (p_x_prd_workorder_rec.HOLD_REASON_CODE IS NULL OR G_CALLED_FROM <> 'API' OR G_CALLED_FROM IS NULL) THEN
890      IF p_x_prd_workorder_rec.HOLD_REASON IS NOT NULL AND p_x_prd_workorder_rec.HOLD_REASON <> FND_API.G_MISS_CHAR THEN
891        OPEN get_hold_reason_code_csr(p_x_prd_workorder_rec.HOLD_REASON);
892        FETCH get_hold_reason_code_csr INTO p_x_prd_workorder_rec.HOLD_REASON_CODE;
893        IF get_hold_reason_code_csr%NOTFOUND THEN
894           FND_MESSAGE.SET_NAME('AHL','AHL_PP_REASON_INVALID');
895           FND_MSG_PUB.ADD;
896        END IF;
897        CLOSE get_hold_reason_code_csr;
898      END IF;
899   END IF;
900 
901 END convert_values_to_ids;
902 
903 FUNCTION is_wo_updated(p_prd_workorder_rec IN PRD_WORKORDER_REC)
904 RETURN BOOLEAN
905 AS
906 
907 CURSOR get_old_wo_values(c_workorder_id NUMBER)
908 IS
909 SELECT AWOS.CONFIRM_FAILURE_FLAG,
910        AWOS.ACTUAL_START_DATE,
911        AWOS.ACTUAL_END_DATE,
912        WIP.SCHEDULED_START_DATE,
913        WIP.SCHEDULED_COMPLETION_DATE,
914        WIP.OWNING_DEPARTMENT
915 FROM AHL_WORKORDERS AWOS,
916      WIP_DISCRETE_JOBS WIP
917 WHERE AWOS.WIP_ENTITY_ID = WIP.WIP_ENTITY_ID
918 AND AWOS.workorder_id = c_workorder_id;
919 
920 l_old_workorder_rec get_old_wo_values%ROWTYPE;
921 
922 BEGIN
923 
924 		OPEN get_old_wo_values(p_prd_workorder_rec.workorder_id);
925 		FETCH get_old_wo_values INTO l_old_workorder_rec;
926 		CLOSE get_old_wo_values;
927 
928 		IF p_prd_workorder_rec.confirm_failure_flag <> l_old_workorder_rec.confirm_failure_flag THEN
929 				RETURN TRUE;
930 		ELSIF p_prd_workorder_rec.actual_start_date <> l_old_workorder_rec.actual_start_date THEN
931 				RETURN TRUE;
932 		ELSIF p_prd_workorder_rec.actual_end_date <> l_old_workorder_rec.actual_end_date THEN
933 				RETURN TRUE;
934 		ELSIF p_prd_workorder_rec.scheduled_start_date <> l_old_workorder_rec.scheduled_start_date THEN
935 				RETURN TRUE;
936 		ELSIF p_prd_workorder_rec.scheduled_end_date <> l_old_workorder_rec.scheduled_completion_date THEN
937 				RETURN TRUE;
938 		ELSIF p_prd_workorder_rec.department_id <> l_old_workorder_rec.owning_department THEN
939 				RETURN TRUE;
940 		END IF;
941 
942 		RETURN FALSE;
943 
944 
945 END is_wo_updated;
946 
947 PROCEDURE validate_workorder
948 (
949   p_prd_workorder_rec            IN      PRD_WORKORDER_REC,
950   p_wip_load_flag                IN      VARCHAR2
951 )
952 as
953 
954 CURSOR get_lookup_type_code(c_lookup_code VARCHAR2,c_lookup_type VARCHAR2)
955 IS
956 SELECT lookup_code
957 FROM   FND_LOOKUP_VALUES_VL
958 WHERE  lookup_code = c_lookup_code
959 AND    lookup_type = c_lookup_type
960 AND    SYSDATE BETWEEN NVL(start_date_active,SYSDATE)
961                AND NVL(end_date_active,SYSDATE);
962 
963 CURSOR validate_org(c_org_id NUMBER)
964 IS
965 
966 /* Perf. fix for bug# 4949394. Replace ORG_ORGANIZATION_DEFINITIONS
967    with INV_ORGANIZATION_NAME_V.
968 SELECT a.organization_id,
969        b.eam_enabled_flag
970 FROM   ORG_ORGANIZATION_DEFINITIONS a,MTL_PARAMETERS b
971 WHERE  a.organization_id=b.organization_id
972 AND    a.organization_id=c_org_id;
973 */
974 
975 SELECT a.organization_id,
976        b.eam_enabled_flag
977 FROM   INV_ORGANIZATION_NAME_V a,MTL_PARAMETERS b
978 WHERE  a.organization_id=b.organization_id
979 AND    a.organization_id=c_org_id;
980 
981 l_org_rec                       validate_org%ROWTYPE;
982 
983 CURSOR validate_dept(c_dept_id NUMBER,c_org_id NUMBER)
984 IS
985 SELECT a.department_id
986 FROM   BOM_DEPARTMENTS a
987 WHERE  a.department_id=c_dept_id
988 AND    a.organization_id=c_org_id;
989 
990 CURSOR validate_item_instance(c_inv_item_id NUMBER,c_inst_id NUMBER)
991 IS
992 SELECT a.instance_id
993 FROM   CSI_ITEM_INSTANCES a
994 WHERE  a.inventory_item_id=c_inv_item_id
995 AND    a.instance_id=c_inst_id;
996 
997 l_instance_rec           validate_item_instance%ROWTYPE;
998 
999 CURSOR validate_subinventory(c_org_id NUMBER,c_sub_inv VARCHAR2)
1000 IS
1001 SELECT a.organization_id,
1002        a.secondary_inventory,
1003        b.eam_enabled_flag
1004 FROM   MTL_ITEM_SUB_INVENTORIES a,
1005        MTL_PARAMETERS b
1006 WHERE  a.organization_id=b.organization_id
1007 AND    a.organization_id=c_org_id
1008 AND    a.secondary_inventory=c_sub_inv;
1009 
1010 l_subinv_rec    validate_subinventory%ROWTYPE;
1011 
1012 CURSOR get_visit_task_name(c_visit_task_id NUMBER)
1013 IS
1014 SELECT visit_task_name
1015 FROM   AHL_VISIT_TASKS_VL
1016 WHERE  visit_task_id=c_visit_task_id;
1017 
1018 CURSOR validate_project(C_org_id NUMBER)
1019 IS
1020 SELECT 1
1021 FROM   MTL_PARAMETERS mpr
1022 WHERE  mpr.organization_id=c_org_id
1023 AND    NVL(mpr.project_reference_enabled,2)=1;
1024 
1025 CURSOR  get_visit_wo_dates(c_visit_id NUMBER)
1026 IS
1027 SELECT  WDJ.scheduled_start_date,
1028         WDJ.scheduled_completion_date
1029 FROM    WIP_DISCRETE_JOBS WDJ,
1030         AHL_WORKORDERS WO
1031 WHERE   WDJ.wip_entity_id = WO.wip_entity_id
1032 AND     WO.visit_task_id IS NULL
1033 AND     WO.master_workorder_flag = 'Y'
1034 AND     WO.visit_id = c_visit_id;
1035 
1036 -- bug4393092
1037 CURSOR get_wo_status(c_workorder_id VARCHAR2)
1038 IS
1039 SELECT AWOS.status_code,
1040        FNDL.meaning
1041 FROM AHL_WORKORDERS AWOS,
1042      FND_LOOKUP_VALUES_VL FNDL
1043 WHERE AWOS.WORKORDER_ID = c_workorder_id
1044 AND FNDL.lookup_type = 'AHL_JOB_STATUS'
1045 AND FNDL.lookup_code(+) = AWOS.status_code;
1046 -- cursor to retrieve minimum actual start date and maximum actual
1047 -- end date from all the operations within this workorder
1048 -- Balaji added for the bug where actual operation dates fall outside
1049 -- workorder dates
1050 CURSOR c_get_op_actual_dates(c_workorder_id NUMBER)
1051 IS
1052 SELECT MIN(ACTUAL_START_DATE),
1053 MAX(ACTUAL_END_DATE)
1054 FROM AHL_WORKORDER_OPERATIONS
1055 WHERE WORKORDER_ID = c_workorder_id;
1056 
1057 -- FP bug# 7631453
1058 Cursor validate_hold_reason_code_csr(c_hold_reason VARCHAR2)
1059 IS
1060 Select 1
1061 FROM FND_LOOKUPS
1062 WHERE lookup_type = 'AHL_PRD_WO_HOLD_REASON'
1063 AND lookup_code = c_hold_reason;
1064 
1065 l_dummy_ctr                     NUMBER:=0;
1066 l_lookup_code                   VARCHAR2(30);
1067 l_record_str                    VARCHAR2(80);
1068 l_visit_start_date              DATE;
1069 l_visit_end_date                DATE;
1070 
1071 l_return_status                 VARCHAR2(1);
1072 l_wo_status                     VARCHAR2(80);
1073 l_wo_status_code                VARCHAR2(30);
1074 l_op_min_act_start_date         DATE;
1075 l_op_max_act_end_date           DATE;
1076 BEGIN
1077 
1078   IF p_prd_workorder_rec.DML_OPERATION='C' THEN
1079     IF p_prd_workorder_rec.VISIT_TASK_ID IS NOT NULL THEN
1080       OPEN  get_visit_task_name(p_prd_workorder_rec.VISIT_TASK_ID);
1081       FETCH get_visit_task_name INTO l_record_str;
1082       CLOSE get_visit_task_name;
1083     ELSE
1084       l_record_str := p_prd_workorder_rec.VISIT_NAME;
1085     END IF;
1086   ELSE
1087     l_record_str:=p_prd_workorder_rec.JOB_NUMBER;
1088   END IF;
1089 
1090 		IF p_prd_workorder_rec.DML_OPERATION = 'U' THEN
1091 		-- bug 4393092
1092 		IF is_wo_updated(p_prd_workorder_rec) = TRUE THEN
1093 		OPEN get_wo_status(p_prd_workorder_rec.workorder_id);
1094 		FETCH get_wo_status INTO l_wo_status_code, l_wo_status;
1095 		CLOSE get_wo_status;
1096 		IF l_wo_status_code IN ('22','7','12','4','5') THEN
1097     FND_MESSAGE.set_name('AHL','AHL_PRD_UPD_WO_STS');
1098 				FND_MESSAGE.set_token('WO_STATUS', l_wo_status);
1099 				FND_MSG_PUB.add;
1100 				RAISE FND_API.G_EXC_ERROR;
1101 		END IF;	-- IF l_wo_status_code IN ('22','7','12','4','5') THEN
1102 		END IF; -- IF is_wo_updated = TRUE THEN
1103 		END IF; -- IF p_prd_workorder_rec.DML_OPERATION = 'U' THEN
1104 
1105 
1106 		 -- rroy
1107 			-- ACL Changes
1108 			l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => NULL,
1109 																																																						p_ue_id => NULL,
1110 																																																						p_visit_id => NULL,
1111 																																																						p_item_instance_id => p_prd_workorder_rec.item_instance_id);
1112 			IF l_return_status = FND_API.G_TRUE THEN
1113 					IF p_prd_workorder_rec.DML_OPERATION='C' THEN
1114 	  				FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_CRT_WO_UNTLCKD');
1115 					ELSE
1116 	  				FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_UPD_WO_UNTLCKD');
1117 					END IF;
1118 					FND_MSG_PUB.ADD;
1119 					RAISE FND_API.G_EXC_ERROR;
1120 			END IF;
1121 			-- rroy
1122 			-- ACL Changes
1123 
1124 
1125   SELECT COUNT(*)
1126   INTO   l_dummy_ctr
1127   FROM   WIP_EAM_PARAMETERS
1128   WHERE  organization_id=p_prd_workorder_rec.ORGANIZATION_ID;
1129 
1130   IF l_dummy_ctr=0 THEN
1131     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_EAM_PREFIX_NOTSETUP');
1132     FND_MESSAGE.SET_TOKEN('ORG',p_prd_workorder_rec.ORGANIZATION_ID,false);
1133     FND_MSG_PUB.ADD;
1134     RETURN;
1135   END IF;
1136   IF p_prd_workorder_rec.SCHEDULED_START_DATE  IS NULL OR
1137      p_prd_workorder_rec.SCHEDULED_START_DATE=FND_API.G_MISS_DATE THEN
1138     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_SCHED_ST_DT_NULL');
1139     FND_MSG_PUB.ADD;
1140   END IF;
1141 
1142   IF p_prd_workorder_rec.SCHEDULED_END_DATE  IS NULL OR
1143      p_prd_workorder_rec.SCHEDULED_END_DATE=FND_API.G_MISS_DATE THEN
1144     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_SCHED_END_DT_NULL');
1145     FND_MSG_PUB.ADD;
1146   END IF;
1147 
1148   IF p_prd_workorder_rec.SCHEDULED_START_DATE IS NOT NULL AND
1149      p_prd_workorder_rec.SCHEDULED_START_DATE<>FND_API.G_MISS_DATE AND
1150      p_prd_workorder_rec.SCHEDULED_END_DATE IS NOT NULL AND
1151      p_prd_workorder_rec.SCHEDULED_END_DATE<>FND_API.G_MISS_DATE THEN
1152 
1153     IF p_prd_workorder_rec.SCHEDULED_START_DATE > p_prd_workorder_rec.SCHEDULED_END_DATE THEN
1154       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_SCHD_STDT_GT_SCHD_DATE');
1155       FND_MSG_PUB.ADD;
1156     END IF;
1157 
1158     IF p_prd_workorder_rec.VISIT_TASK_ID IS NOT NULL AND
1159        p_wip_load_flag = 'Y' THEN
1160       OPEN  get_visit_wo_dates( p_prd_workorder_rec.visit_id );
1161       FETCH get_visit_wo_dates
1162       INTO  l_visit_start_date,
1163             l_visit_end_date;
1164       CLOSE get_visit_wo_dates;
1165 
1166 -- as per mail from shailaja dtd tue,21 sep 2004,
1167 -- Re: [Fwd: Re: Reschedule Visit Jobs after Visit Planned Start/End time change]
1168 -- the workorder dates on the production ui's should be compared only with the visit master
1169 -- workorder dates
1170       IF ( p_prd_workorder_rec.SCHEDULED_START_DATE < l_visit_start_date OR
1171            p_prd_workorder_rec.SCHEDULED_END_DATE > l_visit_end_date ) THEN
1172         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_SCHD_DT_EXCEEDS_VISIT');
1173         FND_MESSAGE.SET_TOKEN('START_DT', TO_CHAR( l_visit_start_date, 'DD-MON-YYYY HH24:MI' ),false);
1174         FND_MESSAGE.SET_TOKEN('END_DT', TO_CHAR( l_visit_end_date, 'DD-MON-YYYY HH24:MI' ),false);
1175         FND_MSG_PUB.ADD;
1176       END IF;
1177     END IF;
1178 
1179   END IF;
1180 
1181   IF p_prd_workorder_rec.ORGANIZATION_ID IS NULL OR
1182      p_prd_workorder_rec.ORGANIZATION_ID=FND_API.G_MISS_NUM THEN
1183     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ORGID_NULL');
1184     FND_MSG_PUB.ADD;
1185   ELSE
1186     OPEN validate_org(p_prd_workorder_rec.ORGANIZATION_ID);
1187     FETCH validate_org INTO l_org_rec;
1188     IF validate_org%NOTFOUND THEN
1189       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ORG_ID_INVALID');
1190       FND_MSG_PUB.ADD;
1191     ELSIF validate_org%FOUND AND l_org_rec.eam_enabled_flag='N' THEN
1192       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ORG_ID_not_eam_enabled');
1193       FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1194       FND_MSG_PUB.ADD;
1195     END IF;
1196     CLOSE validate_org;
1197   END IF;
1198 
1199   IF p_prd_workorder_rec.DEPARTMENT_ID IS NULL OR
1200      p_prd_workorder_rec.DEPARTMENT_ID=FND_API.G_MISS_NUM THEN
1201     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_DEPT_ID_NULL');
1202     FND_MSG_PUB.ADD;
1203   ELSE
1204     OPEN  validate_dept(p_prd_workorder_rec.Department_id, p_prd_workorder_rec.Organization_id);
1205     FETCH validate_dept INTO l_dummy_ctr;
1206     IF validate_dept%NOTFOUND THEN
1207       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_DEPTID_INVALID');
1208       FND_MESSAGE.SET_TOKEN('TASK_JOB',l_record_str,false);
1209       FND_MESSAGE.SET_TOKEN('ORG',p_prd_workorder_rec.Organization_id,false);
1210       FND_MSG_PUB.ADD;
1211     END IF;
1212     CLOSE validate_dept;
1213   END IF;
1214 
1215   IF p_prd_workorder_rec.ITEM_INSTANCE_ID IS NULL OR
1216      p_prd_workorder_rec.ITEM_INSTANCE_ID=FND_API.G_MISS_NUM THEN
1217     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ITMINSTID_NULL');
1218     FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1219     FND_MSG_PUB.ADD;
1220   ELSE
1221     OPEN  validate_item_instance(p_prd_workorder_rec.inventory_item_id, p_prd_workorder_rec.item_instance_id);
1222     FETCH validate_item_instance INTO l_instance_rec;
1223     IF    validate_item_instance%NOTFOUND THEN
1224       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ITMINSTID_INVALID');
1225       FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1226       FND_MSG_PUB.ADD;
1227     END IF;
1228     CLOSE validate_item_instance;
1229   END IF;
1230 
1231   IF p_prd_workorder_rec.STATUS_CODE IS NULL OR
1232      p_prd_workorder_rec.STATUS_CODE=FND_API.G_MISS_CHAR THEN
1233     FND_MESSAGE.SET_NAME('AHL','AHL_PRD_STATUS_CODE_INVALID');
1234     FND_MSG_PUB.ADD;
1235   ELSE
1236     OPEN get_lookup_type_code(p_prd_workorder_rec.STATUS_CODE,'AHL_JOB_STATUS');
1237     FETCH get_lookup_type_code INTO L_LOOKUP_CODE;
1238     IF get_lookup_type_code%NOTFOUND THEN
1239       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_STATUS_CODE_INVALID');
1240       FND_MSG_PUB.ADD;
1241     END IF;
1242     CLOSE get_lookup_type_code;
1243   END IF;
1244 
1245   IF p_prd_workorder_rec.DML_OPERATION='U' THEN
1246 
1247     IF (p_prd_workorder_rec.actual_start_date  IS NULL OR
1248         p_prd_workorder_rec.actual_start_date=FND_API.G_MISS_DATE) AND
1249        (p_prd_workorder_rec.actual_end_date<>FND_API.G_MISS_DATE AND
1250         p_prd_workorder_rec.actual_end_date IS NOT NULL) THEN
1251       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ACTUAL_START_DT_NULL');
1252       FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1253       FND_MSG_PUB.ADD;
1254     END IF;
1255 
1256     IF  p_prd_workorder_rec.actual_start_date IS NOT NULL AND
1257         p_prd_workorder_rec.actual_start_date<>FND_API.G_MISS_DATE AND
1258         p_prd_workorder_rec.actual_end_date<>FND_API.G_MISS_DATE AND
1259         p_prd_workorder_rec.actual_end_date IS NOT NULL AND
1260         p_prd_workorder_rec.actual_start_date > p_prd_workorder_rec.actual_end_date THEN
1261       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ACTUAL_ST_GT_SCHD_DATE');
1262       FND_MSG_PUB.ADD;
1263     END IF;
1264 
1265     IF  p_prd_workorder_rec.actual_end_date<>FND_API.G_MISS_DATE AND
1266         p_prd_workorder_rec.actual_end_date IS NOT NULL AND
1267         TRUNC(p_prd_workorder_rec.actual_end_date) > TRUNC( SYSDATE )  THEN
1268       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_ACTUAL_END_GT_SYSDATE');
1269       FND_MSG_PUB.ADD;
1270     END IF;
1271 
1272     OPEN c_get_op_actual_dates(p_prd_workorder_rec.workorder_id);
1273     FETCH c_get_op_actual_dates INTO l_op_min_act_start_date, l_op_max_act_end_date;
1274     CLOSE c_get_op_actual_dates;
1275 
1276     IF ( p_prd_workorder_rec.actual_start_date IS NOT NULL AND
1277          p_prd_workorder_rec.actual_start_date <> FND_API.G_MISS_DATE AND
1278          l_op_min_act_start_date IS NOT NULL AND
1279          p_prd_workorder_rec.actual_start_date > l_op_min_act_start_date ) THEN
1280       FND_MESSAGE.set_name( 'AHL', 'AHL_PRD_WO_OP_ST_DT' );
1281       FND_MSG_PUB.add;
1282     END IF;
1283 
1284     IF ( p_prd_workorder_rec.actual_end_date IS NOT NULL AND
1285          p_prd_workorder_rec.actual_end_date <> FND_API.G_MISS_DATE AND
1286          l_op_max_act_end_date IS NOT NULL AND
1287          p_prd_workorder_rec.actual_end_date < l_op_max_act_end_date ) THEN
1288        FND_MESSAGE.set_name( 'AHL', 'AHL_PRD_WO_OP_END_DT' );
1289        FND_MSG_PUB.add;
1290     END IF;
1291     IF p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_RELEASED AND
1292        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_UNRELEASED AND
1293        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_COMPLETE AND
1294        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_ON_HOLD AND
1295        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_COMPLETE_NC AND
1296        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_CLOSED AND
1297        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_DRAFT AND
1298        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_PARTS_HOLD AND
1299        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_QA_PENDING AND
1300        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_DEFERRAL_PENDING AND
1301        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_DELETED AND
1302        p_prd_workorder_rec.STATUS_CODE<> G_JOB_STATUS_CANCELLED THEN
1303       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_STATUS_NOT_VALIDINMOD');
1304       FND_MSG_PUB.ADD;
1305     END IF;
1306 
1307     IF p_prd_workorder_rec.WORKORDER_ID IS NULL OR
1308        p_prd_workorder_rec.WORKORDER_ID=FND_API.G_MISS_NUM THEN
1309       FND_MESSAGE.SET_NAME('AHL','AHL_WORKORDER_ID_NULL');
1310       FND_MSG_PUB.ADD;
1311     END IF;
1312 
1313     IF p_prd_workorder_rec.OBJECT_VERSION_NUMBER IS NULL OR
1314        p_prd_workorder_rec.OBJECT_VERSION_NUMBER=FND_API.G_MISS_NUM THEN
1315       FND_MESSAGE.SET_NAME('AHL','OBJ_CANNOT_B_NULL');
1316       FND_MSG_PUB.ADD;
1317     END IF;
1318 
1319     SELECT COUNT(*)
1320     INTO   l_dummy_ctr
1321     FROM   ORG_ACCT_PERIODS
1322     WHERE  organization_id=p_prd_workorder_rec.organization_id
1323     AND    TRUNC(p_prd_workorder_rec.scheduled_start_date) BETWEEN
1324            TRUNC(period_start_date) AND TRUNC(NVL(schedule_CLOSE_date,SYSDATE+1));
1325 
1326     IF l_dummy_ctr=0 THEN
1327       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_SCHEDSDTACCTPERIODINV');
1328       FND_MSG_PUB.ADD;
1329     END IF;
1330 
1331   ELSIF p_prd_workorder_rec.DML_OPERATION='C' THEN
1332 
1333     IF p_prd_workorder_rec.organization_id IS not NULL AND
1334        p_prd_workorder_rec.organization_id<>FND_API.G_MISS_NUM THEN
1335       OPEN  validate_project(p_prd_workorder_rec.organization_id);
1336       FETCH validate_project INTO l_dummy_ctr;
1337       IF validate_project%NOTFOUND THEN
1338         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_PROJECT_INVALID');
1339         FND_MESSAGE.SET_TOKEN('ORGID',l_record_str,false);
1340         FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1341         FND_MSG_PUB.ADD;
1342       ELSIF p_prd_workorder_rec.Project_id  IS not NULL AND
1343             p_prd_workorder_rec.Project_id<>FND_API.G_MISS_NUM THEN
1344 
1345         -- replace MTL_PROJECT_V with PJM_PROJECTS_ORG_OU_SECURE_V for R12
1346         -- required for PJM MOAC changes.
1347         SELECT COUNT(*) INTO l_dummy_ctr
1348         --SELECT 1 INTO l_dummy_ctr
1349         -- FROM   MTL_PROJECT_V
1350         FROM PJM_PROJECTS_ORG_OU_SECURE_V
1351         WHERE  project_id=p_prd_workorder_rec.PROJECT_ID
1352           AND  org_id = mo_global.get_current_org_id();
1353 
1354         IF l_dummy_ctr=0 THEN
1355           FND_MESSAGE.SET_NAME('AHL','AHL_PRD_project_id_invalid');
1356           FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1357           FND_MSG_PUB.ADD;
1358         ELSIF l_dummy_ctr>0 THEN
1359           IF p_prd_workorder_rec.Project_task_id  IS not NULL AND
1360              p_prd_workorder_rec.Project_task_id<>FND_API.G_MISS_NUM THEN
1361             SELECT COUNT(*) INTO l_dummy_ctr
1362             --SELECT 1 INTO l_dummy_ctr
1363             FROM   pa_tasks
1364             WHERE  project_id=p_prd_workorder_rec.project_id
1365             AND    task_id=p_prd_workorder_rec.project_task_id;
1366 
1367             IF l_dummy_ctr=0 THEN
1368               FND_MESSAGE.SET_NAME('AHL','AHL_PRD_PROJECT_TASKID_INVALID');
1369               FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1370               FND_MSG_PUB.ADD;
1371             END IF;
1372           END IF;
1373         END IF;
1374       END IF;
1375       CLOSE validate_project;
1376     END IF;
1377 
1378     IF ( p_prd_workorder_rec.Visit_id IS NULL OR
1379          p_prd_workorder_rec.Visit_id=FND_API.G_MISS_NUM ) THEN
1380       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_VISITID_NULL');
1381       FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1382     END IF;
1383 
1384     IF ( p_prd_workorder_rec.Master_workorder_flag IS NULL OR
1385          p_prd_workorder_rec.Master_workorder_flag=FND_API.G_MISS_CHAR OR
1386          p_prd_workorder_rec.Master_workorder_flag NOT IN ( 'Y' , 'N' ) ) THEN
1387       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_MWOFLAG_INVALID');
1388       FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1389     ELSIF ( ( p_prd_workorder_rec.Visit_task_id IS NULL OR
1390               p_prd_workorder_rec.Visit_task_id=FND_API.G_MISS_NUM ) AND
1391             p_prd_workorder_rec.Master_workorder_flag = 'N' ) THEN
1392       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_VISITASKID_NULL');
1393       FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1394       FND_MSG_PUB.ADD;
1395     END IF;
1396 
1397     IF ( p_prd_workorder_rec.Visit_task_id IS NOT NULL AND
1398          p_prd_workorder_rec.Visit_task_id<>FND_API.G_MISS_NUM ) THEN
1399 
1400       SELECT COUNT(*)
1401       INTO   l_dummy_ctr
1402       FROM   AHL_WORKORDERS
1403       WHERE  visit_task_id=NVL(p_prd_workorder_rec.visit_task_id,0)
1404       AND    LTRIM(RTRIM(status_code)) NOT IN ( G_JOB_STATUS_CANCELLED, G_JOB_STATUS_DELETED );
1405 
1406       IF l_dummy_ctr >0 THEN
1407         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_INVAL_CREATE_WORKORDER');
1408         FND_MESSAGE.SET_TOKEN('RECORD',l_record_str,false);
1409         FND_MSG_PUB.ADD;
1410         RETURN;
1411       END IF;
1412 
1413       SELECT COUNT(*)
1414       INTO   l_dummy_ctr
1415       FROM   AHL_VISIT_TASKS_B
1416       WHERE  visit_task_id=p_prd_workorder_rec.visit_task_id;
1417 
1418       IF l_dummy_ctr=0 THEN
1419         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_visit_task_invalid');
1420         FND_MSG_PUB.ADD;
1421       END IF;
1422 
1423     END IF;
1424 
1425     IF p_prd_workorder_rec.completion_subinventory IS NOT NULL AND
1426        p_prd_workorder_rec.COMPLETION_SUBINVENTORY<>FND_API.G_MISS_CHAR THEN
1427       OPEN  validate_subinventory(p_prd_workorder_rec.organization_id, p_prd_workorder_rec.Completion_subinventory);
1428       FETCH validate_subinventory INTO l_subinv_rec;
1429       IF validate_subinventory%FOUND AND
1430          NVL(l_subinv_rec.eam_enabled_flag,'x')='N' THEN
1431         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_COMPLSUBINV_INVALID');
1432         FND_MSG_PUB.ADD;
1433       ELSIF validate_subinventory%NOTFOUND THEN
1434         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_COMPLSUBINV_INVALID');
1435         FND_MSG_PUB.ADD;
1436       END IF;
1437       CLOSE validate_subinventory;
1438     END IF;
1439 
1440     IF p_prd_workorder_rec.Completion_locator_id IS not NULL AND
1441        p_prd_workorder_rec.Completion_locator_id<>FND_API.G_MISS_CHAR THEN
1442       SELECT COUNT(*)
1443       INTO   l_dummy_ctr
1444       FROM   MTL_ITEM_LOCATIONS
1445       WHERE inventory_location_id=p_prd_workorder_rec.completion_locator_id;
1446       IF l_dummy_ctr =0 THEN
1447         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_COMPLSUBINV_INVALID');
1448         FND_MSG_PUB.ADD;
1449       END IF;
1450     END IF;
1451 
1452     IF p_prd_workorder_rec.Firm_planned_flag  IS NULL OR
1453        p_prd_workorder_rec.Completion_locator_id=FND_API.G_MISS_NUM THEN
1454       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_FIRM_PLANNED_FLAG_NULL');
1455       FND_MSG_PUB.ADD;
1456     ELSIF p_prd_workorder_rec.Firm_planned_flag <1 AND
1457           p_prd_workorder_rec.Firm_planned_flag >2 THEN
1458       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_FIRM_PLANNED_FLAG_INV');
1459       FND_MSG_PUB.ADD;
1460     END IF;
1461 
1462     IF p_prd_workorder_rec.mr_route_id IS not NULL OR
1463        p_prd_workorder_rec.mr_route_id<>FND_API.G_MISS_NUM THEN
1464       SELECT COUNT(*)
1465       INTO   l_dummy_ctr
1466       FROM   AHL_MR_ROUTES_V   -- Chnaged from AHL_MR_ROUTES to be Application Usage complaint.
1467       WHERE  mr_route_id=p_prd_workorder_rec.mr_route_id;
1468 
1469       IF NVL(l_dummy_ctr,0)>1 OR NVL(l_dummy_ctr,0)=0 THEN
1470         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_MR_ROUTE_ID_INVLD');
1471         FND_MSG_PUB.ADD;
1472       END IF;
1473     END IF;
1474 
1475     IF p_prd_workorder_rec.ITEM_INSTANCE_ID IS NULL OR
1476        p_prd_workorder_rec.ITEM_INSTANCE_ID=FND_API.G_MISS_NUM THEN
1477       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_INSTANCE_ID_NULL');
1478       FND_MSG_PUB.ADD;
1479     ELSE
1480       SELECT COUNT(a.instance_id)
1481       INTO   l_dummy_ctr
1482       FROM   CSI_ITEM_INSTANCES  a,
1483              MTL_SYSTEM_ITEMS b
1484       WHERE  a.instance_id=p_prd_workorder_rec.item_instance_id
1485       AND    TRUNC(SYSDATE) BETWEEN TRUNC(NVL(a.active_start_date,SYSDATE))
1486                             AND TRUNC(NVL(a.active_end_date,SYSDATE+1))
1487       AND    a.inventory_item_id=b.inventory_item_id;
1488 
1489       IF NVL(l_dummy_ctr,0)=0 THEN
1490         FND_MESSAGE.SET_NAME('AHL','AHL_PRD_INVITEM_NOTACTIVE');
1491         FND_MSG_PUB.ADD;
1492       END IF;
1493     END IF;
1494 
1495     IF p_prd_workorder_rec.INVENTORY_ITEM_ID IS NULL OR
1496        p_prd_workorder_rec.INVENTORY_ITEM_ID=FND_API.G_MISS_NUM THEN
1497       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_INV_ITEM_ID_NULL');
1498       FND_MSG_PUB.ADD;
1499     END IF;
1500 
1501   END IF;
1502 
1503   -- nsikka: ER 5846702
1504   IF p_prd_workorder_rec.hold_reason_code IS NOT NULL AND
1505      p_prd_workorder_rec.hold_reason_code<>FND_API.G_MISS_CHAR THEN
1506        OPEN validate_hold_reason_code_csr(p_prd_workorder_rec.hold_reason_code);
1507         FETCH validate_hold_reason_code_csr INTO l_dummy_ctr;
1508         IF validate_hold_reason_code_csr%NOTFOUND THEN
1509           FND_MESSAGE.SET_NAME('AHL','AHL_PP_REASON_INVALID');
1510           FND_MSG_PUB.ADD;
1511         END IF;
1512         CLOSE validate_hold_reason_code_csr;
1513   END IF;
1514 
1515   IF (p_prd_workorder_rec.status_code = '6' OR p_prd_workorder_rec.status_code = '19' )
1516       AND p_prd_workorder_rec.hold_reason_code IS NULL THEN
1517         FND_MESSAGE.SET_NAME('AHL','AHL_PP_REASON_NULL');
1518         FND_MSG_PUB.ADD;
1519   END IF;
1520 
1521 END validate_workorder;
1522 
1523 -- Added for FP bug# 7238868.
1524 PROCEDURE get_Resource_Dept(p_aso_resource_id   IN NUMBER,
1525                             p_organization_id   IN NUMBER,
1526                             p_aso_resc_name     IN VARCHAR2,
1527                             x_department_id     OUT NOCOPY NUMBER,
1528                             x_department_name   OUT NOCOPY VARCHAR2,
1529                             x_return_status     OUT NOCOPY VARCHAR2)
1530 IS
1531 
1532 -- find out dept. level resource if exists.
1533 CURSOR get_res_dept_csr (p_aso_resource_id   IN NUMBER,
1534                          p_org_id            IN NUMBER)
1535 IS
1536   SELECT DISTINCT MAP.department_id, DEPT.description
1537   FROM   BOM_DEPARTMENT_RESOURCES BD, AHL_RESOURCE_MAPPINGS MAP,
1538          BOM_RESOURCES BR, BOM_DEPARTMENTS DEPT
1539   WHERE  BD.resource_id = MAP.bom_resource_id
1540   AND    BR.resource_id = BD.resource_id
1541   AND    BR.organization_id = p_org_id
1542   AND    MAP.aso_resource_id = p_aso_resource_id
1543   AND    MAP.BOM_org_id = p_org_id
1544   AND    MAP.department_id = BD.department_id
1545   AND    BD.department_id = dept.department_id;
1546 
1547 l_department_id  NUMBER;
1548 l_department_name bom_departments.description%TYPE;
1549 
1550 
1551 BEGIN
1552 
1553   x_return_status := FND_API.G_RET_STS_SUCCESS;
1554   x_department_id := NULL;
1555 
1556   OPEN get_res_dept_csr (p_aso_resource_id, p_organization_id);
1557   FETCH get_res_dept_csr  INTO l_department_id, l_department_name;
1558   IF (get_res_dept_csr%FOUND) THEN
1559       x_department_id := l_department_id;
1560       --x_department_name:= l_department_name;
1561   END IF;
1562 
1563   -- check if another record exists.
1564   FETCH get_res_dept_csr  INTO l_department_id, l_department_name;
1565   IF (get_res_dept_csr%FOUND) THEN
1566      -- raise error
1567      FND_MESSAGE.set_name('AHL','AHL_DUPLICATE_DEPT_FOUND');
1568      FND_MESSAGE.set_token('DESC',p_aso_resc_name);
1569      FND_MSG_PUB.add;
1570      x_return_status := FND_API.G_RET_STS_ERROR;
1571   END IF;
1572 
1573   CLOSE get_res_dept_csr;
1574 
1575 END get_Resource_Dept;
1576 
1577 
1578 -- Added for FP bug# 7238868.
1579 PROCEDURE Get_Default_Rt_Op_dept(p_object_id in number,
1580                                  p_association_type in varchar2,
1581                                  p_organization_id  in number,
1582                                  x_return_status    out nocopy varchar2,
1583                                  x_department_id    out nocopy number,
1584                                  x_department_name  out nocopy varchar2,
1585                                  x_object_resource_found out nocopy varchar2)
1586 IS
1587 
1588   -- For Getting Resource Requirements defined for Route or Operation
1589   CURSOR get_rt_oper_resources(c_object_id NUMBER,
1590                                c_association_type VARCHAR2)
1591   IS
1592   SELECT AR.rt_oper_resource_id,
1593          AR.aso_resource_id,
1594          ART.name
1595   FROM   AHL_RT_OPER_RESOURCES AR, AHL_RESOURCES ART
1596   WHERE  AR.aso_resource_id = ART.resource_id
1597   AND    AR.association_type_code=p_association_type
1598   AND    AR.object_id=p_object_id;
1599 
1600 
1601 l_rt_oper_resource_rec  get_rt_oper_resources%ROWTYPE;
1602 l_return_status         varchar2(1);
1603 l_final_department_id   NUMBER;
1604 l_final_department_name bom_departments.description%TYPE;
1605 l_department_id         NUMBER;
1606 l_department_name       bom_departments.description%TYPE;
1607 
1608 
1609 BEGIN
1610 
1611      IF ( G_DEBUG = 'Y' ) THEN
1612         AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'p_object_id:' || p_object_id );
1613         AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'p_association_type:' || p_association_type);
1614         AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'p_organization_id:' || p_organization_id);
1615      END IF;
1616 
1617      l_final_department_id := NULL;
1618      x_object_resource_found := 'N';
1619 
1620      OPEN get_rt_oper_resources( p_object_id, p_association_type);
1621      LOOP
1622        FETCH get_rt_oper_resources INTO l_rt_oper_resource_rec;
1623        EXIT WHEN get_rt_oper_resources%NOTFOUND;
1624 
1625        IF ( G_DEBUG = 'Y' ) THEN
1626           AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'Found Route/Oper Res:' || l_rt_oper_resource_rec.aso_resource_id );
1627           AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'Resource Name:' || l_rt_oper_resource_rec.name);
1628        END IF;
1629 
1630        -- Atleast one Resource Requirement Found for the Route
1631        x_object_resource_found := 'Y';
1632 
1633        -- Derive Dept from ASO Resource setup.
1634        get_Resource_Dept(l_rt_oper_resource_rec.aso_resource_id,
1635                          p_organization_id,
1636                          l_rt_oper_resource_rec.name,
1637                          l_department_id,
1638                          l_department_name,
1639                          l_return_status);
1640        IF ( G_DEBUG = 'Y' ) THEN
1641           AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'l_return_status:' || l_return_status );
1642           AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'l_department_id:' || l_department_id);
1643           AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'l_department_name:' || l_department_name);
1644           AHL_DEBUG_PUB.debug( 'Get_Default_Rt_Op_dept-'|| 'l_final_department_id:' || l_final_department_id);
1645        END IF;
1646 
1647        IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1648           RAISE FND_API.G_EXC_ERROR;
1649        END IF;
1650 
1651        IF (l_department_id IS NOT NULL) THEN
1652           IF (l_final_department_id IS NULL) THEN
1653              l_final_department_id := l_department_id;
1654              l_final_department_name := l_department_name;
1655           ELSIF (l_department_id <> l_final_department_id) THEN
1656              -- raise error.
1657              x_return_status := FND_API.G_RET_STS_ERROR;
1658              EXIT;
1659           END IF;
1660           l_department_id := NULL;
1661           l_department_name := NULL;
1662        END IF;
1663 
1664      END LOOP;
1665 
1666      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1667         RAISE FND_API.G_EXC_ERROR;
1668      END IF;
1669 
1670      -- only one unique dept. may be found.
1671      -- or no dept level resource found.
1672 
1673      IF (l_final_department_id IS NOT NULL) THEN
1674         x_department_id := l_final_department_id;
1675         x_department_name := l_final_department_name;
1676      ELSE
1677         x_department_id := NULL;
1678         x_department_name := NULL;
1679      END IF;
1680 
1681 END Get_Default_Rt_Op_dept;
1682 
1683 
1684 PROCEDURE get_op_resource_req
1685 (
1686   p_workorder_rec  IN            prd_workorder_rec,
1687   p_operation_tbl  IN            AHL_PRD_OPERATIONS_PVT.prd_operation_tbl,
1688   p_x_resource_tbl IN OUT NOCOPY AHL_PP_RESRC_REQUIRE_PVT.resrc_require_tbl_type
1689 )
1690 AS
1691 
1692   -- For Getting Resource Requirements defined for Operation
1693   -- For Getting Resource Requirements defined for Route or Operation
1694   -- JKJAIN US space FP 7644260 for ER # 6998882 -- start
1695   -- added schedule_seq_num to the query.
1696   CURSOR get_oper_resources(c_operation_id NUMBER)
1697   IS
1698   SELECT AR.rt_oper_resource_id,
1699          AR.aso_resource_id,
1700         (AR.duration * AR.quantity ) duration, --Modified by Srini for Costing ER
1701          AR.quantity,
1702          AR.activity_id,
1703          AR.cost_basis_id,
1704          AR.scheduled_type_id,
1705          AR.autocharge_type_id,
1706          AR.standard_rate_flag,
1707 		 AR.schedule_seq
1708   FROM   AHL_RT_OPER_RESOURCES AR
1709   WHERE  AR.association_type_code='OPERATION'
1710   AND    AR.object_id=c_operation_id;
1711 -- JKJAIN US space FP 7644260 for ER # 6998882 -- end
1712   l_oper_resource_rec  get_oper_resources%ROWTYPE;
1713 
1714   -- For Getting Alternate Resources for a Resource Requirement
1715   CURSOR   get_alternate_aso_resources(c_rt_oper_resource_id NUMBER)
1716   IS
1717   SELECT   aso_resource_id
1718   FROM     AHL_ALTERNATE_RESOURCES
1719   WHERE    rt_oper_resource_id=c_rt_oper_resource_id
1720   ORDER BY priority;
1721 
1722   -- For Getting the BOM Resource for the ASO Resource and the Visit's
1723   -- Organization plus Visit Task's Department
1724   CURSOR get_bom_resource(c_aso_resource_id NUMBER,
1725                           c_org_id NUMBER,
1726                           c_dept_id NUMBER)
1727   IS
1728   SELECT BR.resource_id,
1729          BR.resource_code,
1730          BR.resource_type,
1731          BR.description,
1732          BR.unit_of_measure
1733   FROM   BOM_DEPARTMENT_RESOURCES BDR,
1734          BOM_RESOURCES BR,
1735          AHL_RESOURCE_MAPPINGS MAP
1736   WHERE  BDR.department_id=c_dept_id
1737   AND    BDR.resource_id=BR.resource_id
1738   AND    BR.organization_id=c_org_id
1739   AND    BR.resource_id=MAP.bom_resource_id
1740   AND    MAP.aso_resource_id=c_aso_resource_id;
1741 
1742   l_bom_resource_rec     get_bom_resource%ROWTYPE;
1743 
1744   l_res_ctr              NUMBER := 0;
1745   l_res_seq_num          NUMBER := 0;
1746   l_bom_resource_found   BOOLEAN := FALSE;
1747 
1748  -- cursor for getting task quantity for a given workorder
1749  -- Begin OGMA Issue # 105 - Balaji
1750  CURSOR c_get_task_quantity(p_workorder_id NUMBER)
1751  IS
1752  SELECT
1753    nvl(vtsk.quantity, 1)
1754  FROM
1755    ahl_visit_tasks_b vtsk,
1756    ahl_workorders awo
1757  WHERE
1758    vtsk.visit_task_id = awo.visit_task_id AND
1759    awo.workorder_id = p_workorder_id;
1760 
1761  l_task_quantity NUMBER;
1762 -- End OGMA Issue # 105 - Balaji
1763 
1764 BEGIN
1765 
1766   -- Process the the Resource Requirements defined for the Operations
1767   FOR i in p_operation_tbl.FIRST..p_operation_tbl.LAST LOOP
1768 
1769     IF ( p_operation_tbl(i).dml_operation = 'C' AND
1770          p_operation_tbl(i).operation_id IS NOT NULL AND
1771          p_operation_tbl(i).operation_id <> FND_API.G_MISS_NUM ) THEN
1772 
1773       -- Get the Resource Requirements defined for the Operation
1774       OPEN get_oper_resources( p_operation_tbl(i).operation_id );
1775       LOOP
1776         FETCH get_oper_resources INTO l_oper_resource_rec;
1777         EXIT WHEN get_oper_resources%NOTFOUND;
1778 
1779         -- Get the BOM Resource for the ASO Resource and the Visit's
1780         -- Organization plus Visit Task's Department
1781         OPEN get_bom_resource( l_oper_resource_rec.aso_resource_id, p_operation_tbl(i).organization_id, p_operation_tbl(i).department_id );
1782         FETCH get_bom_resource INTO l_bom_resource_rec;
1783         IF ( get_bom_resource%FOUND ) THEN
1784           CLOSE get_bom_resource;
1785 
1786           -- BOM Resource Found
1787           l_bom_resource_found := TRUE;
1788         ELSE
1789           CLOSE get_bom_resource;
1790 
1791           -- Since Primary Resource is not Found, Get the Alternate Resources
1792           -- defined for the Resource Requirement
1793           FOR alt_res_cursor IN get_alternate_aso_resources( l_oper_resource_rec.rt_oper_resource_id ) LOOP
1794 
1795             -- Get the BOM Resource for the Alternate ASO Resource and the
1796             -- Visit's Organization plus Visit Task's Department
1797             OPEN get_bom_resource( alt_res_cursor.aso_resource_id, p_operation_tbl(i).organization_id, p_operation_tbl(i).department_id );
1798             FETCH get_bom_resource INTO l_bom_resource_rec;
1799             IF ( get_bom_resource%FOUND ) THEN
1800               CLOSE get_bom_resource;
1801 
1802               -- BOM Resource Found
1803               l_bom_resource_found := TRUE;
1804               EXIT;
1805             END IF;
1806             CLOSE get_bom_resource;
1807           END LOOP;
1808         END IF;
1809 
1810         -- If a BOM Resource is Found for the Resource Requirement, then,
1811         -- Add the Resource Requirement to the Corresponding Operation
1812         IF ( l_bom_resource_found = TRUE ) THEN
1813           l_res_seq_num := l_res_seq_num + 10;
1814           l_res_ctr := l_res_ctr + 1;
1815           l_bom_resource_found := FALSE;
1816 
1817           p_x_resource_tbl(l_res_ctr).operation_resource_id    :=NULL;
1818           p_x_resource_tbl(l_res_ctr).resource_seq_number      :=l_res_seq_num;
1819           p_x_resource_tbl(l_res_ctr).operation_seq_number     :=p_operation_tbl(i).operation_sequence_num;
1820           p_x_resource_tbl(l_res_ctr).workorder_operation_id   :=p_operation_tbl(i).workorder_operation_id;
1821           p_x_resource_tbl(l_res_ctr).workorder_id             :=p_workorder_rec.workorder_id;
1822           p_x_resource_tbl(l_res_ctr).wip_entity_id            :=p_workorder_rec.wip_entity_id;
1823           p_x_resource_tbl(l_res_ctr).organization_id          :=p_workorder_rec.organization_id;
1824           p_x_resource_tbl(l_res_ctr).department_id            :=p_operation_tbl(i).department_id;
1825           p_x_resource_tbl(l_res_ctr).department_name          :=p_operation_tbl(i).department_name;
1826           p_x_resource_tbl(l_res_ctr).oper_start_date          :=p_operation_tbl(i).scheduled_start_date;
1827           p_x_resource_tbl(l_res_ctr).oper_end_date            :=p_operation_tbl(i).scheduled_end_date;
1828           p_x_resource_tbl(l_res_ctr).req_start_date           :=p_operation_tbl(i).scheduled_start_date;
1829           p_x_resource_tbl(l_res_ctr).req_end_date             :=p_operation_tbl(i).scheduled_end_date;
1830           p_x_resource_tbl(l_res_ctr).resource_type_code       :=l_bom_resource_rec.resource_type;
1831           p_x_resource_tbl(l_res_ctr).resource_id              :=l_bom_resource_rec.resource_id;
1832           p_x_resource_tbl(l_res_ctr).uom_code                 :=l_bom_resource_rec.unit_of_measure;
1833           -- Begin OGMA Issue # 105 - Balaji
1834           p_x_resource_tbl(l_res_ctr).duration                 := l_oper_resource_rec.duration;
1835           IF l_oper_resource_rec.cost_basis_id IS NOT NULL AND
1836              l_oper_resource_rec.cost_basis_id = 1
1837           THEN
1838 
1839                 OPEN c_get_task_quantity(p_workorder_rec.workorder_id);
1840                 FETCH c_get_task_quantity INTO l_task_quantity;
1841                 CLOSE c_get_task_quantity;
1842 
1843                 p_x_resource_tbl(l_res_ctr).duration                 := p_x_resource_tbl(l_res_ctr).duration * l_task_quantity;
1844 
1845           END IF;
1846           -- End OGMA Issue # 105 - Balaji
1847           --p_x_resource_tbl(l_res_ctr).duration                 :=l_oper_resource_rec.duration;
1848           p_x_resource_tbl(l_res_ctr).quantity                 :=l_oper_resource_rec.quantity;
1849           p_x_resource_tbl(l_res_ctr).cost_basis_code          :=l_oper_resource_rec.cost_basis_id;
1850           p_x_resource_tbl(l_res_ctr).charge_type_code         :=l_oper_resource_rec.autocharge_type_id;
1851           p_x_resource_tbl(l_res_ctr).std_rate_flag_code       :=l_oper_resource_rec.standard_rate_flag;
1852           p_x_resource_tbl(l_res_ctr).scheduled_type_code      :=l_oper_resource_rec.scheduled_type_id;
1853 --JKJAIN US space FP for ER # 6998882 -- start
1854  	           p_x_resource_tbl(l_res_ctr).schedule_seq_num         :=l_oper_resource_rec.schedule_seq;
1855 --JKJAIN US space FP for ER # 6998882 -- end
1856           --p_x_resource_tbl(l_res_ctr).activity_code          :=l_oper_resource_rec.activity_id;
1857           p_x_resource_tbl(l_res_ctr).operation_flag         :='C';
1858         END IF;
1859       END LOOP;
1860       CLOSE get_oper_resources;
1861     END IF;
1862   END LOOP;
1863 
1864 END get_op_resource_req;
1865 
1866 PROCEDURE get_rt_resource_req
1867 (
1868   p_workorder_rec  IN            prd_workorder_rec,
1869   p_operation_tbl  IN            AHL_PRD_OPERATIONS_PVT.prd_operation_tbl,
1870   p_x_resource_tbl IN OUT NOCOPY AHL_PP_RESRC_REQUIRE_PVT.resrc_require_tbl_type
1871 )
1872 AS
1873 
1874   -- For Getting Resource Requirements defined for Route or Operation
1875   -- JKJAIN US space FP 7644260 for ER # 6998882-- start
1876   -- added schedule_seq_num to the query.
1877   CURSOR get_rt_oper_resources(c_object_id NUMBER,
1878                            c_association_type VARCHAR2)
1879   IS
1880   SELECT AR.rt_oper_resource_id,
1881          AR.aso_resource_id,
1882         (AR.duration * AR.quantity ) duration,
1883          AR.quantity,
1884          AR.activity_id,
1885          AR.cost_basis_id,
1886          AR.scheduled_type_id,
1887          AR.autocharge_type_id,
1888          AR.standard_rate_flag,
1889 		  AR.schedule_seq
1890   FROM   AHL_RT_OPER_RESOURCES AR
1891   WHERE  AR.association_type_code=c_association_type
1892   AND    AR.object_id=c_object_id;
1893  -- JKJAIN US space FP 7644260 for ER # 6998882 -- end
1894   l_rt_oper_resource_rec  get_rt_oper_resources%ROWTYPE;
1895 
1896   -- For Getting Alternate Resources for a Resource Requirement
1897   CURSOR   get_alternate_aso_resources(c_rt_oper_resource_id NUMBER)
1898   IS
1899   SELECT   aso_resource_id
1900   FROM     AHL_ALTERNATE_RESOURCES
1901   WHERE    rt_oper_resource_id=c_rt_oper_resource_id
1902   ORDER BY priority;
1903 
1904   -- For Getting the BOM Resource for the ASO Resource and the Visit's
1905   -- Organization plus Visit Task's Department
1906   CURSOR get_bom_resource(c_aso_resource_id NUMBER,
1907                           c_org_id NUMBER,
1908                           c_dept_id NUMBER)
1909   IS
1910   SELECT BR.resource_id,
1911          BR.resource_code,
1912          BR.resource_type,
1913          BR.description,
1914          BR.unit_of_measure
1915   FROM   BOM_DEPARTMENT_RESOURCES BDR,
1916          BOM_RESOURCES BR,
1917          AHL_RESOURCE_MAPPINGS MAP
1918   WHERE  BDR.department_id=c_dept_id
1919   AND    BDR.resource_id=BR.resource_id
1920   AND    BR.organization_id=c_org_id
1921   AND    BR.resource_id=MAP.bom_resource_id
1922   AND    MAP.aso_resource_id=c_aso_resource_id;
1923 
1924   l_bom_resource_rec     get_bom_resource%ROWTYPE;
1925 
1926   l_res_ctr              NUMBER := 0;
1927   l_res_seq_num          NUMBER := 0;
1928   l_bom_resource_found   BOOLEAN := FALSE;
1929   l_route_resource_found BOOLEAN := FALSE;
1930 
1931  -- cursor for getting task quantity for a given workorder
1932  -- Begin OGMA Issue # 105 - Balaji
1933  CURSOR c_get_task_quantity(p_workorder_id NUMBER)
1934  IS
1935  SELECT
1936    nvl(vtsk.quantity, 1)
1937  FROM
1938    ahl_visit_tasks_b vtsk,
1939    ahl_workorders awo
1940  WHERE
1941    vtsk.visit_task_id = awo.visit_task_id AND
1942    awo.workorder_id = p_workorder_id;
1943 
1944  l_task_quantity NUMBER;
1945 -- End OGMA Issue # 105 - Balaji
1946 
1947 BEGIN
1948 
1949   -- Get the Resource Requirements defined for the Route
1950   OPEN get_rt_oper_resources( p_workorder_rec.route_id, 'ROUTE' );
1951   LOOP
1952     FETCH get_rt_oper_resources INTO l_rt_oper_resource_rec;
1953     EXIT WHEN get_rt_oper_resources%NOTFOUND;
1954 
1955     -- Atleast one Resource Requirement Found for the Route
1956     l_route_resource_found := TRUE;
1957 
1958     -- Get the BOM Resource for the ASO Resource and the Visit's
1959     -- Organization plus Visit Task's Department
1960     --OPEN get_bom_resource( l_rt_oper_resource_rec.aso_resource_id, p_workorder_rec.organization_id, p_workorder_rec.department_id );
1961     OPEN get_bom_resource( l_rt_oper_resource_rec.aso_resource_id, p_workorder_rec.organization_id, p_operation_tbl(p_operation_tbl.FIRST).department_id );
1962     FETCH get_bom_resource INTO l_bom_resource_rec;
1963     IF ( get_bom_resource%FOUND ) THEN
1964       CLOSE get_bom_resource;
1965 
1966       -- BOM Resource Found
1967       l_bom_resource_found := TRUE;
1968     ELSE
1969       CLOSE get_bom_resource;
1970 
1971       -- Since Primary Resource is not Found, Get the Alternate Resources
1972       -- defined for the Resource Requirement
1973       FOR alt_res_cursor IN get_alternate_aso_resources( l_rt_oper_resource_rec.rt_oper_resource_id ) LOOP
1974 
1975         -- Get the BOM Resource for the Alternate ASO Resource and the Visit's
1976         -- Organization plus Visit Task's Department
1977         --OPEN get_bom_resource( alt_res_cursor.aso_resource_id, p_workorder_rec.organization_id, p_workorder_rec.department_id );
1978         OPEN get_bom_resource( alt_res_cursor.aso_resource_id, p_workorder_rec.organization_id, p_operation_tbl(p_operation_tbl.FIRST).department_id);
1979         FETCH get_bom_resource INTO l_bom_resource_rec;
1980         IF ( get_bom_resource%FOUND ) THEN
1981           CLOSE get_bom_resource;
1982 
1983           -- BOM Resource Found
1984           l_bom_resource_found := TRUE;
1985           EXIT;
1986         END IF;
1987         CLOSE get_bom_resource;
1988       END LOOP;
1989     END IF;
1990 
1991     -- If a BOM Resource is Found for the Resource Requirement, then,
1992     -- Add the Job Resource Requirement to the First Operation
1993     IF ( l_bom_resource_found = TRUE ) THEN
1994       l_res_seq_num := l_res_seq_num + 10;
1995       l_res_ctr := l_res_ctr + 1;
1996       l_bom_resource_found := FALSE;
1997 
1998       p_x_resource_tbl(l_res_ctr).operation_resource_id    :=NULL;
1999       p_x_resource_tbl(l_res_ctr).resource_seq_number      :=l_res_seq_num;
2000       p_x_resource_tbl(l_res_ctr).operation_seq_number     :=p_operation_tbl(p_operation_tbl.FIRST).operation_sequence_num;
2001       p_x_resource_tbl(l_res_ctr).workorder_operation_id   :=p_operation_tbl(p_operation_tbl.FIRST).workorder_operation_id;
2002       p_x_resource_tbl(l_res_ctr).workorder_id             :=p_workorder_rec.workorder_id;
2003       p_x_resource_tbl(l_res_ctr).organization_id          :=p_workorder_rec.organization_id;
2004       -- Default from operation -- FP bug# 7238868
2005       --p_x_resource_tbl(l_res_ctr).department_id            :=p_workorder_rec.department_id;
2006       --p_x_resource_tbl(l_res_ctr).department_name          :=p_workorder_rec.department_name;
2007       p_x_resource_tbl(l_res_ctr).department_id            :=p_operation_tbl(p_operation_tbl.FIRST).department_id;
2008       p_x_resource_tbl(l_res_ctr).department_name          :=p_operation_tbl(p_operation_tbl.FIRST).department_name;
2009 
2010       p_x_resource_tbl(l_res_ctr).oper_start_date          :=p_workorder_rec.scheduled_start_date;
2011       p_x_resource_tbl(l_res_ctr).oper_end_date            :=p_workorder_rec.scheduled_end_date;
2012       p_x_resource_tbl(l_res_ctr).req_start_date           :=p_workorder_rec.scheduled_start_date;
2013       p_x_resource_tbl(l_res_ctr).req_end_date             :=p_workorder_rec.scheduled_end_date;
2014       p_x_resource_tbl(l_res_ctr).resource_type_code       :=l_bom_resource_rec.resource_type;
2015       p_x_resource_tbl(l_res_ctr).resource_id              :=l_bom_resource_rec.resource_id;
2016       p_x_resource_tbl(l_res_ctr).uom_code                 :=l_bom_resource_rec.unit_of_measure;
2017       p_x_resource_tbl(l_res_ctr).duration                 :=l_rt_oper_resource_rec.duration;
2018       -- Begin OGMA Issue # 105 - Balaji
2019       IF l_rt_oper_resource_rec.cost_basis_id IS NOT NULL AND
2020          l_rt_oper_resource_rec.cost_basis_id = 1
2021       THEN
2022 
2023         OPEN c_get_task_quantity(p_workorder_rec.workorder_id);
2024         FETCH c_get_task_quantity INTO l_task_quantity;
2025         CLOSE c_get_task_quantity;
2026 
2027         p_x_resource_tbl(l_res_ctr).duration                 :=p_x_resource_tbl(l_res_ctr).duration * l_task_quantity;
2028 
2029       END IF;
2030       -- End OGMA Issue # 105 - Balaji
2031       p_x_resource_tbl(l_res_ctr).quantity                 :=l_rt_oper_resource_rec.quantity;
2032       p_x_resource_tbl(l_res_ctr).cost_basis_code          :=l_rt_oper_resource_rec.cost_basis_id;
2033       p_x_resource_tbl(l_res_ctr).charge_type_code         :=l_rt_oper_resource_rec.autocharge_type_id;
2034       p_x_resource_tbl(l_res_ctr).std_rate_flag_code       :=l_rt_oper_resource_rec.standard_rate_flag;
2035       p_x_resource_tbl(l_res_ctr).scheduled_type_code      :=l_rt_oper_resource_rec.scheduled_type_id;
2036  -- JKJAIN US space FP for ER # 6998882 -- start
2037  	  p_x_resource_tbl(l_res_ctr).schedule_seq_num         :=l_rt_oper_resource_rec.schedule_seq;
2038  -- JKJAIN US space FP for ER # 6998882 -- end
2039      --p_x_resource_tbl(l_res_ctr).activity_code          :=l_rt_oper_resource_rec.activity_id;
2040       p_x_resource_tbl(l_res_ctr).operation_flag           :='C';
2041     END IF;
2042   END LOOP;
2043   CLOSE get_rt_oper_resources;
2044 
2045   -- If the Route has no Resource Requirement defined, then, process the
2046   -- the Resource Requirements defined for the Associated Operations
2047   IF ( l_route_resource_found = FALSE ) THEN
2048 
2049     FOR i in p_operation_tbl.FIRST..p_operation_tbl.LAST LOOP
2050       l_res_seq_num := 0;
2051 
2052       -- Get the Resource Requirements defined for the Operation
2053       OPEN get_rt_oper_resources( p_operation_tbl(i).operation_id, 'OPERATION' );
2054       LOOP
2055         FETCH get_rt_oper_resources INTO l_rt_oper_resource_rec;
2056         EXIT WHEN get_rt_oper_resources%NOTFOUND;
2057 
2058         -- Get the BOM Resource for the ASO Resource and the Visit's
2059         -- Organization plus Visit Task's Department
2060         --OPEN get_bom_resource( l_rt_oper_resource_rec.aso_resource_id, p_workorder_rec.organization_id, p_workorder_rec.department_id );
2061         OPEN get_bom_resource( l_rt_oper_resource_rec.aso_resource_id, p_workorder_rec.organization_id, p_operation_tbl(i).department_id );
2062         FETCH get_bom_resource INTO l_bom_resource_rec;
2063         IF ( get_bom_resource%FOUND ) THEN
2064           CLOSE get_bom_resource;
2065 
2066           -- BOM Resource Found
2067           l_bom_resource_found := TRUE;
2068         ELSE
2069           CLOSE get_bom_resource;
2070 
2071           -- Since Primary Resource is not Found, Get the Alternate Resources
2072           -- defined for the Resource Requirement
2073           FOR alt_res_cursor IN get_alternate_aso_resources( l_rt_oper_resource_rec.rt_oper_resource_id ) LOOP
2074 
2075             -- Get the BOM Resource for the Alternate ASO Resource and the
2076             -- Visit's Organization plus Visit Task's Department
2077             --OPEN get_bom_resource( alt_res_cursor.aso_resource_id, p_workorder_rec.organization_id, p_workorder_rec.department_id );
2078             OPEN get_bom_resource( alt_res_cursor.aso_resource_id, p_workorder_rec.organization_id, p_operation_tbl(i).department_id );
2079             FETCH get_bom_resource INTO l_bom_resource_rec;
2080             IF ( get_bom_resource%FOUND ) THEN
2081               CLOSE get_bom_resource;
2082 
2083               -- BOM Resource Found
2084               l_bom_resource_found := TRUE;
2085               EXIT;
2086             END IF;
2087             CLOSE get_bom_resource;
2088           END LOOP;
2089         END IF;
2090 
2091         -- If a BOM Resource is Found for the Resource Requirement, then,
2092         -- Add the Resource Requirement to the Corresponding Operation
2093         IF ( l_bom_resource_found = TRUE ) THEN
2094           l_res_seq_num := l_res_seq_num + 10;
2095           l_res_ctr := l_res_ctr + 1;
2096           l_bom_resource_found := FALSE;
2097 
2098           p_x_resource_tbl(l_res_ctr).operation_resource_id    :=NULL;
2099           p_x_resource_tbl(l_res_ctr).resource_seq_number      :=l_res_seq_num;
2100           p_x_resource_tbl(l_res_ctr).operation_seq_number     :=p_operation_tbl(i).operation_sequence_num;
2101           p_x_resource_tbl(l_res_ctr).workorder_operation_id   :=p_operation_tbl(i).workorder_operation_id;
2102           p_x_resource_tbl(l_res_ctr).workorder_id             :=p_workorder_rec.workorder_id;
2103           p_x_resource_tbl(l_res_ctr).organization_id          :=p_workorder_rec.organization_id;
2104           -- Default from Operation - FP bug# 7238868
2105           --p_x_resource_tbl(l_res_ctr).department_id            :=p_workorder_rec.department_id;
2106           --p_x_resource_tbl(l_res_ctr).department_name          :=p_workorder_rec.department_name;
2107           p_x_resource_tbl(l_res_ctr).department_id            :=p_operation_tbl(i).department_id;
2108           p_x_resource_tbl(l_res_ctr).department_name          :=p_operation_tbl(i).department_name;
2109 
2110           p_x_resource_tbl(l_res_ctr).oper_start_date          :=p_operation_tbl(i).scheduled_start_date;
2111           p_x_resource_tbl(l_res_ctr).oper_end_date            :=p_operation_tbl(i).scheduled_end_date;
2112           p_x_resource_tbl(l_res_ctr).req_start_date           :=p_operation_tbl(i).scheduled_start_date;
2113           p_x_resource_tbl(l_res_ctr).req_end_date             :=p_operation_tbl(i).scheduled_end_date;
2114           p_x_resource_tbl(l_res_ctr).resource_type_code       :=l_bom_resource_rec.resource_type;
2115           p_x_resource_tbl(l_res_ctr).resource_id              :=l_bom_resource_rec.resource_id;
2116           p_x_resource_tbl(l_res_ctr).uom_code                 :=l_bom_resource_rec.unit_of_measure;
2117           p_x_resource_tbl(l_res_ctr).duration                 :=l_rt_oper_resource_rec.duration;
2118           -- Begin OGMA Issue # 105 - Balaji
2119           IF l_rt_oper_resource_rec.cost_basis_id IS NOT NULL AND
2120              l_rt_oper_resource_rec.cost_basis_id = 1
2121           THEN
2122 
2123              OPEN c_get_task_quantity(p_workorder_rec.workorder_id);
2124              FETCH c_get_task_quantity INTO l_task_quantity;
2125              CLOSE c_get_task_quantity;
2126 
2127              p_x_resource_tbl(l_res_ctr).duration              :=p_x_resource_tbl(l_res_ctr).duration * l_task_quantity;
2128 
2129           END IF;
2130           -- End OGMA Issue # 105 - Balaji
2131           p_x_resource_tbl(l_res_ctr).quantity                 :=l_rt_oper_resource_rec.quantity;
2132           p_x_resource_tbl(l_res_ctr).cost_basis_code          :=l_rt_oper_resource_rec.cost_basis_id;
2133           p_x_resource_tbl(l_res_ctr).charge_type_code         :=l_rt_oper_resource_rec.autocharge_type_id;
2134           p_x_resource_tbl(l_res_ctr).std_rate_flag_code       :=l_rt_oper_resource_rec.standard_rate_flag;
2135           p_x_resource_tbl(l_res_ctr).scheduled_type_code      :=l_rt_oper_resource_rec.scheduled_type_id;
2136  -- JKJAIN US space FP for ER # 6998882 -- start
2137  	      p_x_resource_tbl(l_res_ctr).schedule_seq_num      :=l_rt_oper_resource_rec.schedule_seq;
2138  -- JKJAIN US space FP for ER # 6998882 -- end
2139           --p_x_resource_tbl(l_res_ctr).activity_code          :=l_rt_oper_resource_rec.activity_id;
2140           p_x_resource_tbl(l_res_ctr).operation_flag         :='C';
2141         END IF;
2142       END LOOP;
2143       CLOSE get_rt_oper_resources;
2144     END LOOP;
2145   END IF;
2146 
2147 END get_rt_resource_req;
2148 
2149 PROCEDURE get_op_material_req
2150 (
2151   p_workorder_rec  IN            prd_workorder_rec,
2152   p_operation_tbl  IN            AHL_PRD_OPERATIONS_PVT.prd_operation_tbl,
2153   p_x_material_tbl IN OUT NOCOPY AHL_PP_MATERIALS_PVT.req_material_tbl_type
2154 )
2155 AS
2156 
2157   -- For Getting the Material Requirements for an Operation
2158   CURSOR get_oper_materials (c_operation_id NUMBER)
2159   IS
2160   SELECT MAT.rt_oper_material_id,
2161          MAT.inventory_item_id,
2162          MAT.item_group_id,
2163          MAT.quantity,
2164          MAT.uom_code,
2165          -- Bug # 6377990 - start
2166          MAT.in_service
2167          -- Bug # 6377990 - end
2168   FROM   AHL_RT_OPER_MATERIALS MAT
2169   WHERE  MAT.association_type_code='OPERATION'
2170   AND    MAT.object_id=c_operation_id;
2171 
2172   l_material_req_rec   get_oper_materials%ROWTYPE;
2173 
2174   -- For getting the Alternate Items for an Item Group
2175   CURSOR   get_alternate_items (c_item_group_id NUMBER)
2176   IS
2177   SELECT   inventory_item_id
2178   FROM     AHL_ITEM_ASSOCIATIONS_B
2179   WHERE    item_group_id=c_item_group_id
2180   ORDER BY priority;
2181 
2182   -- For Checking whether an Item exisits in an Organization.
2183   -- ***Incorporate Master Org check***
2184   CURSOR check_org_item (c_inventory_item_id NUMBER,
2185                          c_org_id NUMBER)
2186   IS
2187   SELECT 'X'
2188   FROM   MTL_SYSTEM_ITEMS
2189   WHERE  inventory_item_id=c_inventory_item_id
2190   AND    organization_id=c_org_id;
2191 
2192   l_mat_ctr            NUMBER := 0;
2193   l_org_item_found     BOOLEAN := FALSE;
2194   l_dummy              VARCHAR2(1);
2195   l_alternate_item_id  NUMBER;
2196 
2197 BEGIN
2198 
2199   FOR i in p_operation_tbl.FIRST..p_operation_tbl.LAST LOOP
2200     IF ( p_operation_tbl(i).dml_operation = 'C' AND
2201          p_operation_tbl(i).operation_id IS NOT NULL AND
2202          p_operation_tbl(i).operation_id <> FND_API.G_MISS_NUM ) THEN
2203 
2204       -- Get the Material Requirements defined for the Operation
2205       OPEN get_oper_materials( p_operation_tbl(i).operation_id );
2206       LOOP
2207         FETCH get_oper_materials INTO l_material_req_rec;
2208         EXIT WHEN get_oper_materials%NOTFOUND;
2209 
2210         -- The Material Requirement is based on an Item
2211         IF ( l_material_req_rec.inventory_item_id IS NOT NULL ) THEN
2212 
2213           -- Check if the Item is available in the Visit's Organization
2214           OPEN check_org_item( l_material_req_rec.inventory_item_id, p_operation_tbl(i).organization_id );
2215           FETCH check_org_item INTO l_dummy;
2216           IF ( check_org_item%FOUND ) THEN
2217 
2218             -- Organization Item Found
2219             l_org_item_found := TRUE;
2220           END IF;
2221           CLOSE check_org_item;
2222         ELSE
2223 
2224           -- Since the Material Requirement is Based on a Item Group,
2225           -- Process all the Alternate Items for the Item Group
2226           -- based on Priority
2227           FOR alt_item_cursor IN get_alternate_items( l_material_req_rec.item_group_id ) LOOP
2228 
2229             -- Check if the Alternate Item is available in the
2230             -- Visit's Organization
2231             OPEN check_org_item( alt_item_cursor.inventory_item_id, p_operation_tbl(i).organization_id );
2232             FETCH check_org_item INTO l_dummy;
2233             IF ( check_org_item%FOUND ) THEN
2234 
2235               -- Organization Item Found
2236               l_org_item_found := TRUE;
2237               l_alternate_item_id := alt_item_cursor.inventory_item_id;
2238               CLOSE check_org_item;
2239               EXIT;
2240             END IF;
2241             CLOSE check_org_item;
2242 
2243           END LOOP;
2244         END IF;
2245 
2246         -- If an Organization Item is Found for the Material Requirement, then,
2247         -- Add the Material Requirement to the Corresponding Operation
2248         IF ( l_org_item_found = TRUE ) THEN
2249           l_org_item_found := FALSE;
2250           l_mat_ctr := l_mat_ctr + 1;
2251           p_x_material_tbl(l_mat_ctr).rt_oper_material_id       :=l_material_req_rec.rt_oper_material_id;
2252           p_x_material_tbl(l_mat_ctr).inventory_item_id         :=NVL(l_material_req_rec.inventory_item_id, l_alternate_item_id);
2253           p_x_material_tbl(l_mat_ctr).requested_quantity        :=l_material_req_rec.quantity;
2254           -- Bug # 6377990 - start
2255           p_x_material_tbl(l_mat_ctr).repair_item               :=l_material_req_rec.in_service;
2256           -- Bug # 6377990 - end
2257           p_x_material_tbl(l_mat_ctr).visit_id                  :=p_workorder_rec.visit_id;
2258           p_x_material_tbl(l_mat_ctr).visit_task_id             :=p_workorder_rec.visit_task_id;
2259           p_x_material_tbl(l_mat_ctr).organization_id           :=p_workorder_rec.organization_id;
2260           p_x_material_tbl(l_mat_ctr).requested_date            :=p_operation_tbl(i).scheduled_start_date;
2261           p_x_material_tbl(l_mat_ctr).job_number                :=p_workorder_rec.job_number;
2262           p_x_material_tbl(l_mat_ctr).workorder_id              :=p_workorder_rec.workorder_id;
2263           p_x_material_tbl(l_mat_ctr).wip_entity_id             :=p_workorder_rec.wip_entity_id;
2264           p_x_material_tbl(l_mat_ctr).workorder_operation_id    :=p_operation_tbl(i).workorder_operation_id;
2265           p_x_material_tbl(l_mat_ctr).operation_sequence        :=p_operation_tbl(i).operation_sequence_num;
2266           p_x_material_tbl(l_mat_ctr).operation_code            :=p_operation_tbl(i).operation_code;
2267           p_x_material_tbl(l_mat_ctr).operation_flag            :='C';
2268         END IF;
2269 
2270       END LOOP;
2271       CLOSE get_oper_materials;
2272     END IF;
2273   END LOOP;
2274 
2275 END get_op_material_req;
2276 
2277 PROCEDURE get_rt_material_req
2278 (
2279   p_workorder_rec  IN            prd_workorder_rec,
2280   p_operation_tbl  IN            AHL_PRD_OPERATIONS_PVT.prd_operation_tbl,
2281   p_x_material_tbl IN OUT NOCOPY AHL_PP_MATERIALS_PVT.req_material_tbl_type
2282 )
2283 AS
2284 
2285   -- For Getting the Material Requirements for a Route or Operation
2286   CURSOR get_rt_oper_materials (c_object_id NUMBER,
2287                                 c_association_type VARCHAR2)
2288   IS
2289   SELECT MAT.rt_oper_material_id,
2290          MAT.inventory_item_id,
2291          MAT.item_group_id,
2292          MAT.quantity,
2293          MAT.uom_code
2294   FROM   AHL_RT_OPER_MATERIALS MAT
2295   WHERE  MAT.association_type_code=c_association_type
2296   AND    MAT.object_id=c_object_id;
2297 
2298   l_material_req_rec   get_rt_oper_materials%ROWTYPE;
2299 
2300   -- For getting the Alternate Items for an Item Group
2301   CURSOR   get_alternate_items (c_item_group_id NUMBER)
2302   IS
2303   SELECT   inventory_item_id
2304   FROM     AHL_ITEM_ASSOCIATIONS_B
2305   WHERE    item_group_id=c_item_group_id
2306   ORDER BY priority;
2307 
2308   -- For Checking whether an Item exisits in an Organization.
2309   -- ***Incorporate Master Org check***
2310   CURSOR check_org_item (c_inventory_item_id NUMBER,
2311                          c_org_id NUMBER)
2312   IS
2313   SELECT 'X'
2314   FROM   MTL_SYSTEM_ITEMS
2315   WHERE  inventory_item_id=c_inventory_item_id
2316   AND    organization_id=c_org_id;
2317 
2318   l_mat_ctr            NUMBER := 0;
2319   l_route_mat_found    BOOLEAN := FALSE;
2320   l_org_item_found     BOOLEAN := FALSE;
2321   l_dummy              VARCHAR2(1);
2322   l_alternate_item_id  NUMBER;
2323 
2324 BEGIN
2325 
2326   -- Get the Material Requirements defined for the Route
2327   OPEN get_rt_oper_materials( p_workorder_rec.route_id, 'ROUTE' );
2328   LOOP
2329     FETCH get_rt_oper_materials INTO l_material_req_rec;
2330     EXIT WHEN get_rt_oper_materials%NOTFOUND;
2331 
2332     -- Atleast One Material Requirement defined for the Route
2333     l_route_mat_found := TRUE;
2334 
2335     -- The Material Requirement is based on an Item
2336     IF ( l_material_req_rec.inventory_item_id IS NOT NULL ) THEN
2337 
2338       -- Check if the Item is available in the Visit's Organization
2339       OPEN check_org_item( l_material_req_rec.inventory_item_id, p_workorder_rec.organization_id );
2340       FETCH check_org_item INTO l_dummy;
2341       IF ( check_org_item%FOUND ) THEN
2342 
2343         -- Organization Item Found
2344         l_org_item_found := TRUE;
2345       END IF;
2346       CLOSE check_org_item;
2347     ELSE
2348 
2349       -- Since the Material Requirement is Based on a Item Group,
2350       -- Process all the Alternate Items for the Item Group based on Priority
2351       FOR alt_item_cursor IN get_alternate_items( l_material_req_rec.item_group_id ) LOOP
2352 
2353         -- Check if the Alternate Item is available in the Visit's Organization
2354         OPEN check_org_item( alt_item_cursor.inventory_item_id, p_workorder_rec.organization_id );
2355         FETCH check_org_item INTO l_dummy;
2356         IF ( check_org_item%FOUND ) THEN
2357 
2358           -- Organization Item Found
2359           l_org_item_found := TRUE;
2360           l_alternate_item_id := alt_item_cursor.inventory_item_id;
2361           CLOSE check_org_item;
2362           EXIT;
2363         END IF;
2364         CLOSE check_org_item;
2365 
2366       END LOOP;
2367     END IF;
2368 
2369     -- If an Organization Item is Found for the Material Requirement, then,
2370     -- Add the Job Material Requirement to the First Operation
2371     IF ( l_org_item_found = TRUE ) THEN
2372       l_org_item_found := FALSE;
2373       l_mat_ctr := l_mat_ctr + 1;
2374       p_x_material_tbl(l_mat_ctr).rt_oper_material_id       :=l_material_req_rec.rt_oper_material_id;
2375       p_x_material_tbl(l_mat_ctr).inventory_item_id         :=NVL(l_material_req_rec.inventory_item_id, l_alternate_item_id);
2376       p_x_material_tbl(l_mat_ctr).requested_quantity        :=l_material_req_rec.quantity;
2377       p_x_material_tbl(l_mat_ctr).visit_id                  :=p_workorder_rec.visit_id;
2378       p_x_material_tbl(l_mat_ctr).visit_task_id             :=p_workorder_rec.visit_task_id;
2379       p_x_material_tbl(l_mat_ctr).organization_id           :=p_workorder_rec.organization_id;
2380       p_x_material_tbl(l_mat_ctr).requested_date            :=p_workorder_rec.scheduled_start_date;
2381       p_x_material_tbl(l_mat_ctr).job_number                :=p_workorder_rec.job_number;
2382       p_x_material_tbl(l_mat_ctr).workorder_id              :=p_workorder_rec.workorder_id;
2383       p_x_material_tbl(l_mat_ctr).workorder_operation_id    :=p_operation_tbl(p_operation_tbl.FIRST).workorder_operation_id;
2384       p_x_material_tbl(l_mat_ctr).operation_sequence        :=p_operation_tbl(p_operation_tbl.FIRST).operation_sequence_num;
2385       p_x_material_tbl(l_mat_ctr).operation_code            :=p_operation_tbl(p_operation_tbl.FIRST).operation_code;
2386       p_x_material_tbl(l_mat_ctr).operation_flag            :='C';
2387     END IF;
2388 
2389   END LOOP;
2390   CLOSE get_rt_oper_materials;
2391 
2392   IF ( l_route_mat_found = FALSE ) THEN
2393     FOR i in p_operation_tbl.FIRST..p_operation_tbl.LAST LOOP
2394 
2395       -- Get the Material Requirements defined for the Operation
2396       OPEN get_rt_oper_materials( p_operation_tbl(i).operation_id, 'OPERATION' );
2397       LOOP
2398         FETCH get_rt_oper_materials INTO l_material_req_rec;
2399         EXIT WHEN get_rt_oper_materials%NOTFOUND;
2400 
2401         -- The Material Requirement is based on an Item
2402         IF ( l_material_req_rec.inventory_item_id IS NOT NULL ) THEN
2403 
2404           -- Check if the Item is available in the Visit's Organization
2405           OPEN check_org_item( l_material_req_rec.inventory_item_id, p_workorder_rec.organization_id );
2406           FETCH check_org_item INTO l_dummy;
2407           IF ( check_org_item%FOUND ) THEN
2408 
2409             -- Organization Item Found
2410             l_org_item_found := TRUE;
2411           END IF;
2412           CLOSE check_org_item;
2413         ELSE
2414 
2415           -- Since the Material Requirement is Based on a Item Group,
2416           -- Process all the Alternate Items for the Item Group
2417           -- based on Priority
2418           FOR alt_item_cursor IN get_alternate_items( l_material_req_rec.item_group_id ) LOOP
2419 
2420             -- Check if the Alternate Item is available in the
2421             -- Visit's Organization
2422             OPEN check_org_item( alt_item_cursor.inventory_item_id, p_workorder_rec.organization_id );
2423             FETCH check_org_item INTO l_dummy;
2424             IF ( check_org_item%FOUND ) THEN
2425 
2426               -- Organization Item Found
2427               l_org_item_found := TRUE;
2428               l_alternate_item_id := alt_item_cursor.inventory_item_id;
2429               CLOSE check_org_item;
2430               EXIT;
2431             END IF;
2432             CLOSE check_org_item;
2433 
2434           END LOOP;
2435         END IF;
2436 
2437         -- If an Organization Item is Found for the Material Requirement, then,
2438         -- Add the Material Requirement to the Corresponding Operation
2439         IF ( l_org_item_found = TRUE ) THEN
2440           l_org_item_found := FALSE;
2441           l_mat_ctr := l_mat_ctr + 1;
2442           p_x_material_tbl(l_mat_ctr).rt_oper_material_id       :=l_material_req_rec.rt_oper_material_id;
2443           p_x_material_tbl(l_mat_ctr).inventory_item_id         :=NVL(l_material_req_rec.inventory_item_id, l_alternate_item_id);
2444           p_x_material_tbl(l_mat_ctr).requested_quantity        :=l_material_req_rec.quantity;
2445           p_x_material_tbl(l_mat_ctr).visit_id                  :=p_workorder_rec.visit_id;
2446           p_x_material_tbl(l_mat_ctr).visit_task_id             :=p_workorder_rec.visit_task_id;
2447           p_x_material_tbl(l_mat_ctr).organization_id           :=p_workorder_rec.organization_id;
2448           p_x_material_tbl(l_mat_ctr).requested_date            :=p_workorder_rec.scheduled_start_date;
2449           p_x_material_tbl(l_mat_ctr).job_number                :=p_workorder_rec.job_number;
2450           p_x_material_tbl(l_mat_ctr).workorder_id              :=p_workorder_rec.workorder_id;
2451           p_x_material_tbl(l_mat_ctr).workorder_operation_id    :=p_operation_tbl(i).workorder_operation_id;
2452           p_x_material_tbl(l_mat_ctr).operation_sequence        :=p_operation_tbl(i).operation_sequence_num;
2453           p_x_material_tbl(l_mat_ctr).operation_code            :=p_operation_tbl(i).operation_code;
2454           p_x_material_tbl(l_mat_ctr).operation_flag            :='C';
2455         END IF;
2456 
2457       END LOOP;
2458       CLOSE get_rt_oper_materials;
2459     END LOOP;
2460   END IF;
2461 
2462 END get_rt_material_req;
2463 
2464 PROCEDURE default_attributes
2465 (
2466   p_x_prd_workorder_rec   IN OUT NOCOPY prd_workorder_rec
2467 )
2468 AS
2469   CURSOR get_route_inspection_type(c_route_id NUMBER)
2470   IS
2471   SELECT qa_inspection_type
2472   --FROM   AHL_ROUTES_V --Changed from AHL_ROUTES_B for Application Usage Complaince.
2473   FROM   AHL_ROUTES_APP_V --Changed from AHL_ROUTES_V for perf bug# 4949394.
2474   WHERE  route_id=c_route_id;
2475 
2476     --Adithya added for bug# 6830028
2477      CURSOR get_route_acc_class_code(c_route_id NUMBER)
2478      IS
2479      SELECT ACCOUNTING_CLASS_CODE
2480      FROM   AHL_ROUTES_APP_V
2481      WHERE  route_id=c_route_id;
2482 
2483      CURSOR validate_acc_class_code( c_wip_acc_class_code VARCHAR2, c_organization_id NUMBER)
2484      IS
2485      select 'x'
2486      from WIP_ACCOUNTING_CLASSES
2487      where class_code = c_wip_acc_class_code
2488      and organization_id = c_organization_id
2489      and class_type = 6;
2490 
2491   l_acc_class_code        VARCHAR2(10);
2492   l_qa_inspection_type    VARCHAR2(150);
2493   l_msg_count             NUMBER;
2494   l_msg_data              VARCHAR2(2000);
2495   l_return_status         VARCHAR2(1);
2496   -- ER 4460913
2497   l_project_num           VARCHAR2(25);
2498   l_visit_name            VARCHAR2(80);
2499   l_dummy                 VARCHAR2(1);
2500 
2501   -- rroy
2502   -- replace MTL_PROJECT_V with PJM_PROJECTS_ORG_OU_SECURE_V for R12
2503   -- required for PJM MOAC changes.
2504   CURSOR get_project_num(c_project_id NUMBER)
2505   IS
2506     SELECT project_number
2507     --FROM   MTL_PROJECT_V
2508     FROM PJM_PROJECTS_ORG_OU_SECURE_V
2509     WHERE  project_id = c_project_id;
2510 
2511   CURSOR get_visit_name(c_visit_id NUMBER)
2512   IS
2513     SELECT VISIT_NAME
2514     FROM AHL_VISITS_TL
2515     WHERE VISIT_ID = c_visit_id;
2516 
2517 --Balaji added for BAE ER # 4462462.
2518 CURSOR c_get_SR_details(p_visit_task_id NUMBER)
2519 IS
2520 SELECT
2521   CSIA.summary, -- sr summary
2522   CSIT.name -- sr type attribute
2523 FROM
2524   AHL_VISIT_TASKS_B VTSK,
2525   AHL_UNIT_EFFECTIVITIES_B UE,
2526   CS_INCIDENTS_ALL CSIA,
2527   CS_INCIDENT_TYPES_VL CSIT
2528 WHERE
2529   VTSK.visit_task_id = p_visit_task_id AND
2530   UE.Unit_effectivity_id = VTSK.unit_effectivity_id AND
2531   UE.manually_planned_flag = 'Y' AND
2532   UE.cs_incident_id IS NOT NULL AND
2533   NOT EXISTS (SELECT
2534                  'X'
2535               FROM
2536                   AHL_UE_RELATIONSHIPS UER
2537               WHERE
2538                   UER.related_ue_id = UE.Unit_effectivity_id OR
2539                   UER.ue_id = UE.Unit_effectivity_id) AND
2540   CSIA.incident_id = UE.cs_incident_id AND
2541   CSIT.incident_type_id = CSIA.incident_type_id;
2542 
2543   l_sr_summary            VARCHAR2(240);
2544   l_sr_type               VARCHAR2(30);
2545 BEGIN
2546 
2547   SELECT AHL_WORKORDERS_S.NEXTVAL
2548   INTO   p_x_prd_workorder_rec.WORKORDER_ID
2549   FROM   DUAL;
2550 
2551   IF p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG = 'Y' AND p_x_prd_workorder_rec.VISIT_TASK_ID IS NULL THEN
2552 		  -- As per ER 4460913
2553 				-- Visit master workorder name will be <project number> - <visit name>
2554 				OPEN get_project_num(p_x_prd_workorder_rec.PROJECT_ID);
2555 				FETCH get_project_num INTO l_project_num;
2556 				CLOSE get_project_num;
2557 
2558 				OPEN get_visit_name(p_x_prd_workorder_rec.VISIT_ID);
2559 				FETCH get_visit_name INTO l_visit_name;
2560 				CLOSE get_visit_name;
2561 
2562     p_x_prd_workorder_rec.JOB_NUMBER := l_project_num || ' - ' || substrb(l_visit_name, 1, 77 - (length(l_project_num)));
2563 		ELSE
2564   SELECT work_order_prefix,
2565          default_eam_class
2566   INTO   p_x_prd_workorder_rec.JOB_NUMBER,
2567           l_acc_class_code
2568   FROM   WIP_EAM_PARAMETERS
2569   WHERE  ORGANIZATION_ID=p_x_prd_workorder_rec.ORGANIZATION_ID;
2570 
2571   SELECT p_x_prd_workorder_rec.JOB_NUMBER||TO_CHAR(AHL_WORKORDER_JOB_S.NEXTVAL)
2572   INTO   p_x_prd_workorder_rec.JOB_NUMBER
2573   FROM   DUAL;
2574 
2575         --Adithya added for Accounting class bug#
2576         IF p_x_prd_workorder_rec.CLASS_CODE is NULL and p_x_prd_workorder_rec.ROUTE_ID IS NOT NULL THEN
2577            OPEN  get_route_acc_class_code(p_x_prd_workorder_rec.ROUTE_ID);
2578            FETCH get_route_acc_class_code INTO p_x_prd_workorder_rec.CLASS_CODE;
2579            CLOSE get_route_acc_class_code;
2580          IF p_x_prd_workorder_rec.CLASS_CODE IS NOT NULL THEN
2581            OPEN  validate_acc_class_code(p_x_prd_workorder_rec.CLASS_CODE, p_x_prd_workorder_rec.ORGANIZATION_ID);
2582            FETCH validate_acc_class_code INTO l_dummy;
2583            CLOSE validate_acc_class_code;
2584            IF l_dummy IS NULL THEN
2585               p_x_prd_workorder_rec.CLASS_CODE := l_acc_class_code;
2586            END IF;
2587          END IF;
2588         END IF;
2589      END IF;
2590 
2591      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2592          fnd_log.string(FND_LOG.LEVEL_STATEMENT,'ahl.plsql.AHL_PRD_WORKORDER_PVT.default_attributes',
2593                   'Acc Class code: ' || p_x_prd_workorder_rec.CLASS_CODE);
2594 
2595   END IF;
2596 
2597   --Balaji added for BAE ER # 4462462 - Start.
2598   -- Default workorder description for NR Workorder in following cases
2599   -- 1. Non-Routines(NR) created on the shop floor.
2600   -- 2. Non-Routines with no MRs associated and planned from UMP into a Visit.
2601   OPEN c_get_SR_details(p_x_prd_workorder_rec.visit_task_id);
2602   FETCH c_get_SR_details INTO l_sr_summary, l_sr_type;
2603   CLOSE c_get_SR_details;
2604 
2605   IF l_sr_summary IS NOT NULL AND l_sr_type IS NOT NULL
2606   THEN
2607      p_x_prd_workorder_rec.job_description := SUBSTR(l_sr_type || ' - ' ||l_sr_summary, 1, 240);
2608   END IF;
2609   --Balaji added for BAE ER # 4462462 - End.
2610   p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER   :=1;
2611   p_x_prd_workorder_rec.LAST_UPDATE_DATE        :=SYSDATE;
2612   p_x_prd_workorder_rec.LAST_UPDATED_BY         :=FND_GLOBAL.user_id;
2613   p_x_prd_workorder_rec.CREATION_DATE           :=SYSDATE;
2614   p_x_prd_workorder_rec.CREATED_BY              :=FND_GLOBAL.user_id;
2615   p_x_prd_workorder_rec.LAST_UPDATE_LOGIN       :=FND_GLOBAL.user_id;
2616   p_x_prd_workorder_rec.WIP_ENTITY_ID           :=NULL;
2617   --p_x_prd_workorder_rec.STATUS_CODE             :=G_JOB_STATUS_UNRELEASED; -- Unreleased
2618 
2619   IF p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG IS NULL OR
2620      p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG = FND_API.G_MISS_CHAR THEN
2621     p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG := 'N';
2622   END IF;
2623 
2624   IF p_x_prd_workorder_rec.VISIT_TASK_ID = FND_API.G_MISS_NUM THEN
2625     p_x_prd_workorder_rec.VISIT_TASK_ID := NULL;
2626   END IF;
2627 
2628   IF p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG = 'Y' THEN
2629     l_qa_inspection_type:= NULL;
2630   ELSIF p_x_prd_workorder_rec.ROUTE_ID IS NULL THEN
2631     l_qa_inspection_type:= FND_PROFILE.value('AHL_NR_WO_PLAN_TYPE');
2632   ELSIF p_x_prd_workorder_rec.ROUTE_ID IS NOT NULL THEN
2633     OPEN  get_route_inspection_type(p_x_prd_workorder_rec.ROUTE_ID);
2634     FETCH get_route_inspection_type INTO l_qa_inspection_type;
2635     CLOSE get_route_inspection_type;
2636   END IF;
2637 
2638   IF l_qa_inspection_type is NOT NULL THEN
2639     AHL_QA_RESULTS_PVT.get_qa_plan
2640     (
2641       p_api_version           => 1.0,
2642       p_init_msg_list         => FND_API.G_FALSE,
2643       p_commit                => FND_API.G_FALSE,
2644       p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
2645       p_default               => FND_API.G_FALSE,
2646       p_module_type           => NULL,
2647       x_return_status         => l_return_status,
2648       x_msg_count             => l_msg_count,
2649       x_msg_data              => l_msg_data,
2650       p_organization_id       => p_x_prd_workorder_rec.organization_id,
2651       p_transaction_number    => 2001,
2652       p_col_trigger_value     => l_qa_inspection_type,
2653       x_plan_id               => p_x_prd_workorder_rec.plan_id
2654     );
2655   END IF;
2656 
2657   -- Default Job Description from Task Name ( if not passed )
2658 
2659 END default_attributes;
2660 
2661 PROCEDURE get_operations
2662 (
2663   p_workorder_rec     IN             prd_workorder_rec,
2664   p_x_operations_tbl  IN OUT NOCOPY  AHL_PRD_OPERATIONS_PVT.prd_operation_tbl
2665 )
2666 AS
2667   CURSOR get_route_operations(c_route_id NUMBER)
2668   IS
2669   SELECT   RO.operation_id,
2670            RO.step,
2671            OP.concatenated_segments,
2672            OP.operation_type_code,
2673            OP.description
2674   FROM     AHL_OPERATIONS_VL OP,
2675            AHL_ROUTE_OPERATIONS RO
2676   WHERE    OP.operation_id=RO.operation_id
2677 --  AND      OP.revision_status_code='COMPLETE'
2678   AND      RO.route_id=c_route_id
2679   AND      OP.revision_number IN
2680            ( SELECT MAX(OP1.revision_number)
2681              FROM   AHL_OPERATIONS_B_KFV OP1
2682              WHERE  OP1.concatenated_segments=OP.concatenated_segments
2683              AND    OP1.revision_status_code='COMPLETE'
2684              AND    TRUNC(SYSDATE) BETWEEN TRUNC(OP1.start_date_active) AND
2685                                            TRUNC(NVL(OP1.end_date_active,SYSDATE+1))
2686            )
2687   ORDER BY RO.step;
2688 
2689   l_count                NUMBER := 1;
2690   l_blank_operation_txt  FND_NEW_MESSAGES.message_text%TYPE;
2691 
2692   -- Added for FP bug# 7238868
2693   l_department_id        NUMBER;
2694   l_department_name       bom_departments.description%TYPE;
2695   l_route_found          VARCHAR2(1);
2696   l_oper_found           VARCHAR2(1);
2697   l_return_status        VARCHAR2(1);
2698 
2699 BEGIN
2700   IF ( p_workorder_rec.ROUTE_ID IS NOT NULL ) THEN
2701     FOR op_cursor IN get_route_operations( p_workorder_rec.ROUTE_ID ) LOOP
2702       p_x_operations_tbl( l_count ).operation_sequence_num := op_cursor.step;
2703       p_x_operations_tbl( l_count ).operation_id := op_cursor.operation_id;
2704       p_x_operations_tbl( l_count ).operation_code := op_cursor.concatenated_segments;
2705       p_x_operations_tbl( l_count ).operation_type_code := op_cursor.operation_type_code;
2706       p_x_operations_tbl( l_count ).operation_description := op_cursor.description;
2707       p_x_operations_tbl( l_count ).organization_id := p_workorder_rec.organization_id;
2708       p_x_operations_tbl( l_count ).workorder_id := p_workorder_rec.workorder_id;
2709       p_x_operations_tbl( l_count ).route_id := p_workorder_rec.route_id;
2710       p_x_operations_tbl( l_count ).department_id := p_workorder_rec.department_id;
2711       p_x_operations_tbl( l_count ).scheduled_start_date := p_workorder_rec.scheduled_start_date;
2712       p_x_operations_tbl( l_count ).scheduled_end_date := p_workorder_rec.scheduled_end_date;
2713       p_x_operations_tbl( l_count ).status_code := G_OP_STATUS_UNCOMPLETE;
2714       p_x_operations_tbl( l_count ).propagate_flag := 'N';
2715       p_x_operations_tbl( l_count ).dml_operation := 'C';
2716 
2717       l_count := l_count + 1;
2718     END LOOP;
2719   END IF;
2720 
2721   IF ( p_x_operations_tbl.COUNT = 0 ) THEN
2722     -- Blank Operation
2723     p_x_operations_tbl( l_count ).operation_sequence_num := 10;
2724 
2725     p_x_operations_tbl( l_count ).organization_id := p_workorder_rec.organization_id;
2726     p_x_operations_tbl( l_count ).workorder_id := p_workorder_rec.workorder_id;
2727     p_x_operations_tbl( l_count ).route_id := p_workorder_rec.route_id;
2728     p_x_operations_tbl( l_count ).department_id := p_workorder_rec.department_id;
2729     p_x_operations_tbl( l_count ).scheduled_start_date := p_workorder_rec.scheduled_start_date;
2730     p_x_operations_tbl( l_count ).scheduled_end_date := p_workorder_rec.scheduled_end_date;
2731     p_x_operations_tbl( l_count ).status_code := G_OP_STATUS_UNCOMPLETE;
2732     p_x_operations_tbl( l_count ).propagate_flag := 'N';
2733 
2734     FND_MESSAGE.set_name('AHL','AHL_PRD_BLANK_OPERATION');
2735     l_blank_operation_txt := FND_MESSAGE.get;
2736     p_x_operations_tbl( l_count ).operation_description := NVL(SUBSTR(RTRIM(l_blank_operation_txt),1,80),'Blank Operation');
2737 
2738     p_x_operations_tbl( l_count ).dml_operation := 'C';
2739 
2740   END IF;
2741 
2742   -- Added for FP bug# 7238868
2743   IF (p_workorder_rec.ROUTE_ID IS NOT NULL ) THEN
2744 
2745         IF ( G_DEBUG = 'Y' ) THEN
2746           AHL_DEBUG_PUB.debug( 'Get_Operations'|| ' - Before Get_Default_Rt_Op_dept' );
2747         END IF;
2748 
2749         -- Get default department from route/oper resources.
2750         Get_Default_Rt_Op_dept(p_object_id => p_workorder_rec.ROUTE_ID,
2751                                p_association_type => 'ROUTE',
2752                                p_organization_id => p_workorder_rec.organization_id,
2753                                x_return_status => l_return_status,
2754                                x_department_id => l_department_id,
2755                                x_department_name => l_department_name,
2756                                x_object_resource_found => l_route_found);
2757 
2758         IF ( G_DEBUG = 'Y' ) THEN
2759            AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_return_status:' || l_return_status );
2760            AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_department_id:' || l_department_id);
2761            AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_department_name:' || l_department_name);
2762            AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_route_found:' || l_route_found);
2763         END IF;
2764 
2765         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2766            RAISE FND_API.G_EXC_ERROR;
2767         END IF;
2768 
2769         IF (l_route_found = 'Y') THEN
2770            IF (l_department_id IS NOT NULL) THEN
2771              -- replace only first operation dept with default dept.
2772              p_x_operations_tbl(p_x_operations_tbl.FIRST).department_id := l_department_id;
2773              p_x_operations_tbl(p_x_operations_tbl.FIRST).department_name := l_department_name;
2774            END IF;
2775 
2776         ELSE
2777            -- loop each operation and default dept.
2778            FOR i IN p_x_operations_tbl.FIRST..p_x_operations_tbl.LAST
2779            LOOP
2780               -- Get default department from route/oper resources.
2781               Get_Default_Rt_Op_dept(p_object_id => p_x_operations_tbl(i).operation_id,
2782                                      p_association_type => 'OPERATION',
2783                                      p_organization_id => p_workorder_rec.organization_id,
2784                                      x_return_status => l_return_status,
2785                                      x_department_id => l_department_id,
2786                                      x_department_name => l_department_name,
2787                                      x_object_resource_found => l_oper_found);
2788 
2789               IF ( G_DEBUG = 'Y' ) THEN
2790                 AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_return_status:' || l_return_status );
2791                 AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_department_id:' || l_department_id);
2792                 AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_department_name:' || l_department_name);
2793                 AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_oper_found:' || l_oper_found);
2794               END IF;
2795 
2796               IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2797                  RAISE FND_API.G_EXC_ERROR;
2798               END IF;
2799 
2800               IF (l_department_id IS NOT NULL) THEN
2801                  p_x_operations_tbl(i).department_id := l_department_id;
2802                  p_x_operations_tbl(i).department_name := l_department_name;
2803               END IF;
2804 
2805            END LOOP;
2806         END IF; -- l_route_found = 'Y'
2807   END IF; -- p_workorder_rec.ROUTE_ID
2808 
2809 END get_operations;
2810 
2811 PROCEDURE create_job
2812 (
2813   p_api_version          IN            NUMBER     := 1.0,
2814   p_init_msg_list        IN            VARCHAR2   := FND_API.G_TRUE,
2815   p_commit               IN            VARCHAR2   := FND_API.G_FALSE,
2816   p_validation_level     IN            NUMBER     := FND_API.G_VALID_LEVEL_FULL,
2817   p_default              IN            VARCHAR2   := FND_API.G_FALSE,
2818   p_module_type          IN            VARCHAR2,
2819   x_return_status        OUT NOCOPY    VARCHAR2,
2820   x_msg_count            OUT NOCOPY    NUMBER,
2821   x_msg_data             OUT NOCOPY    VARCHAR2,
2822   p_wip_load_flag        IN            VARCHAR2   := 'Y', -- Change to N
2823   p_x_prd_workorder_rec  IN OUT NOCOPY prd_workorder_rec,
2824   x_operation_tbl        OUT NOCOPY    AHL_PRD_OPERATIONS_PVT.prd_operation_tbl,
2825   x_resource_tbl         OUT NOCOPY    AHL_PP_RESRC_REQUIRE_PVT.resrc_require_tbl_type,
2826   x_material_tbl         OUT NOCOPY    AHL_PP_MATERIALS_PVT.req_material_tbl_type
2827 )
2828 AS
2829   l_api_name     CONSTANT VARCHAR2(30) := 'CREATE_JOB';
2830   l_api_version  CONSTANT NUMBER       := 1.0;
2831   l_msg_count             NUMBER;
2832   l_msg_data              VARCHAR2(2000);
2833   l_return_status         VARCHAR2(1);
2834   l_job_return_status     VARCHAR2(1);
2835 
2836 -- Begin OGMA Issue # 105 - Balaji
2837 -- cursor for checking if given task is planned task created form forecasted UE.
2838 CURSOR c_can_update_quantity(p_task_id NUMBER)
2839 IS
2840 SELECT
2841   'X'
2842 FROM
2843   ahl_visit_tasks_b vtsk
2844 WHERE
2845   vtsk.quantity IS NULL AND
2846   vtsk.status_code <> 'DELETED' AND
2847   vtsk.visit_task_id = p_task_id
2848 
2849 UNION
2850 
2851 SELECT
2852   'X'
2853 FROM
2854   ahl_visit_tasks_b vtsk,
2855   ahl_unit_effectivities_b aue
2856 WHERE
2857   nvl(aue.manually_planned_flag, 'N') = 'N' AND
2858   vtsk.unit_effectivity_id = aue.unit_effectivity_id AND
2859   vtsk.status_code <> 'DELETED' AND
2860   vtsk.visit_task_id = p_task_id;
2861 
2862 -- cursor for getting instance quantity for planned tasks.
2863 CURSOR c_get_instance_quantity(p_task_id NUMBER)
2864 IS
2865 SELECT
2866   csi.quantity
2867 FROM
2868   csi_item_instances csi,
2869   ahl_visit_tasks_b vtsk
2870 WHERE
2871   vtsk.instance_id = csi.instance_id AND
2872   vtsk.status_code <> 'DELETED' AND
2873   vtsk.visit_task_id = p_task_id;
2874 
2875 l_instance_quantity NUMBER;
2876 l_can_update_quantity VARCHAR2(1);
2877 -- End OGMA Issue # 105 - Balaji
2878 
2879 BEGIN
2880   SAVEPOINT create_job_PVT;
2881 
2882   IF NOT FND_API.compatible_api_call(l_api_version,
2883                                      p_api_version,
2884                                      l_api_name,G_PKG_NAME) THEN
2885     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2886   END IF;
2887 
2888   IF FND_API.to_boolean(p_init_msg_list) THEN
2889     FND_MSG_PUB.initialize;
2890   END IF;
2891 
2892   x_return_status:=FND_API.G_RET_STS_SUCCESS;
2893 
2894   IF G_DEBUG='Y' THEN
2895       AHL_DEBUG_PUB.enable_debug;
2896   END IF;
2897 
2898   -- User Hooks
2899   IF (JTF_USR_HKS.Ok_to_execute('AHL_PRD_WORKORDER_PVT', 'CREATE_JOB', 'B', 'C' )) then
2900       ahl_prd_workorder_CUHK.create_job_pre(
2901         p_prd_workorder_rec => p_x_prd_workorder_rec,
2902         x_msg_count => l_msg_count,
2903         x_msg_data => l_msg_data,
2904         x_return_status => l_return_status);
2905       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
2906         RAISE FND_API.G_EXC_ERROR;
2907       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2908         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2909       END IF;
2910   END IF;
2911 
2912   IF ( G_DEBUG = 'Y' ) THEN
2913     AHL_DEBUG_PUB.debug( l_api_name || ' - Before validate_workorder' );
2914   END IF;
2915 
2916   IF (p_validation_level = FND_API.G_VALID_LEVEL_FULL ) THEN
2917     validate_workorder
2918     (
2919       p_prd_workorder_rec            =>p_x_prd_workorder_rec,
2920       p_wip_load_flag                =>p_wip_load_flag
2921     );
2922 
2923     l_msg_count := FND_MSG_PUB.count_msg;
2924     IF l_msg_count > 0 THEN
2925       x_msg_count := l_msg_count;
2926       RAISE FND_API.G_EXC_ERROR;
2927     END IF;
2928   END IF;
2929 
2930   IF ( G_DEBUG = 'Y' ) THEN
2931     AHL_DEBUG_PUB.debug( l_api_name || ' - Before default_attributes' );
2932   END IF;
2933 
2934   default_attributes
2935   (
2936     p_x_prd_workorder_rec            =>p_x_prd_workorder_rec
2937   );
2938 
2939   IF ( G_DEBUG = 'Y' ) THEN
2940     AHL_DEBUG_PUB.debug( l_api_name || ' - Before Insert into AHL_WORKORDERS' );
2941   END IF;
2942 
2943   -- Begin OGMA Issue # 105 - Balaji
2944   -- update VWP planned task quantity current instance quantity
2945   -- this logic need to be moved to VWP later as per discussion with Shailaja and Jay.
2946   IF p_x_prd_workorder_rec.visit_task_id IS NOT NULL AND p_x_prd_workorder_rec.STATUS_CODE <> '17'
2947   THEN
2948 
2949           OPEN c_can_update_quantity(p_x_prd_workorder_rec.visit_task_id);
2950           FETCH c_can_update_quantity INTO l_can_update_quantity;
2951           CLOSE c_can_update_quantity;
2952 
2953           IF l_can_update_quantity IS NOT NULL
2954           THEN
2955 
2956 		  OPEN c_get_instance_quantity(p_x_prd_workorder_rec.visit_task_id);
2957 		  FETCH c_get_instance_quantity INTO l_instance_quantity;
2958 		  CLOSE c_get_instance_quantity;
2959 
2960 		  UPDATE
2961 		   ahl_visit_tasks_b
2962 		  SET
2963 		   quantity = l_instance_quantity
2964 		  WHERE
2965 		   visit_task_id = p_x_prd_workorder_rec.visit_task_id;
2966 	  END IF;
2967   END IF;
2968   -- End OGMA Issue # 105 - Balaji
2969 
2970   INSERT INTO AHL_WORKORDERS
2971   (
2972     WORKORDER_ID,
2973     OBJECT_VERSION_NUMBER,
2974     LAST_UPDATE_DATE,
2975     LAST_UPDATED_BY,
2976     CREATION_DATE,
2977     CREATED_BY,
2978     LAST_UPDATE_LOGIN,
2979     WORKORDER_NAME,
2980     WIP_ENTITY_ID,
2981     VISIT_ID,
2982     VISIT_TASK_ID,
2983     STATUS_CODE,
2984     PLAN_ID,
2985     COLLECTION_ID,
2986     ROUTE_ID,
2987     ACTUAL_START_DATE,
2988     ACTUAL_END_DATE,
2989     CONFIRM_FAILURE_FLAG,
2990     MASTER_WORKORDER_FLAG,
2991     ATTRIBUTE_CATEGORY,
2992     ATTRIBUTE1,
2993     ATTRIBUTE2,
2994     ATTRIBUTE3,
2995     ATTRIBUTE4,
2996     ATTRIBUTE5,
2997     ATTRIBUTE6,
2998     ATTRIBUTE7,
2999     ATTRIBUTE8,
3000     ATTRIBUTE9,
3001     ATTRIBUTE10,
3002     ATTRIBUTE11,
3003     ATTRIBUTE12,
3004     ATTRIBUTE13,
3005     ATTRIBUTE14,
3006     ATTRIBUTE15
3007   ) VALUES
3008   (
3009     p_x_prd_workorder_rec.WORKORDER_ID,
3010     p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER,
3011     p_x_prd_workorder_rec.LAST_UPDATE_DATE,
3012     p_x_prd_workorder_rec.LAST_UPDATED_BY,
3013     p_x_prd_workorder_rec.CREATION_DATE,
3014     p_x_prd_workorder_rec.CREATED_BY,
3015     p_x_prd_workorder_rec.LAST_UPDATE_LOGIN,
3016     p_x_prd_workorder_rec.JOB_NUMBER,
3017     p_x_prd_workorder_rec.WIP_ENTITY_ID,
3018     p_x_prd_workorder_rec.VISIT_ID,
3019     p_x_prd_workorder_rec.VISIT_TASK_ID,
3020     p_x_prd_workorder_rec.STATUS_CODE,
3021     p_x_prd_workorder_rec.PLAN_ID,
3022     p_x_prd_workorder_rec.COLLECTION_ID,
3023     p_x_prd_workorder_rec.ROUTE_ID,
3024     p_x_prd_workorder_rec.ACTUAL_START_DATE,
3025     p_x_prd_workorder_rec.ACTUAL_END_DATE,
3026     p_x_prd_workorder_rec.CONFIRM_FAILURE_FLAG,
3027     p_x_prd_workorder_rec.MASTER_WORKORDER_FLAG,
3028     p_x_prd_workorder_rec.ATTRIBUTE_CATEGORY,
3029     p_x_prd_workorder_rec.ATTRIBUTE1,
3030     p_x_prd_workorder_rec.ATTRIBUTE2,
3031     p_x_prd_workorder_rec.ATTRIBUTE3,
3032     p_x_prd_workorder_rec.ATTRIBUTE4,
3033     p_x_prd_workorder_rec.ATTRIBUTE5,
3034     p_x_prd_workorder_rec.ATTRIBUTE6,
3035     p_x_prd_workorder_rec.ATTRIBUTE7,
3036     p_x_prd_workorder_rec.ATTRIBUTE8,
3037     p_x_prd_workorder_rec.ATTRIBUTE9,
3038     p_x_prd_workorder_rec.ATTRIBUTE10,
3039     p_x_prd_workorder_rec.ATTRIBUTE11,
3040     p_x_prd_workorder_rec.ATTRIBUTE12,
3041     p_x_prd_workorder_rec.ATTRIBUTE13,
3042     p_x_prd_workorder_rec.ATTRIBUTE14,
3043     p_x_prd_workorder_rec.ATTRIBUTE15
3044   );
3045 
3046   IF ( G_DEBUG = 'Y' ) THEN
3047     AHL_DEBUG_PUB.debug( l_api_name || ' - Before Insert into AHL_WORKORDER_TXNS' );
3048   END IF;
3049 
3050   INSERT INTO AHL_WORKORDER_TXNS
3051   (
3052     WORKORDER_TXN_ID,
3053     OBJECT_VERSION_NUMBER,
3054     LAST_UPDATE_DATE,
3055     LAST_UPDATED_BY,
3056     CREATION_DATE,
3057     CREATED_BY,
3058     LAST_UPDATE_LOGIN,
3059     WORKORDER_ID,
3060     TRANSACTION_TYPE_CODE,
3061     STATUS_CODE,
3062     SCHEDULED_START_DATE,
3063     SCHEDULED_END_DATE,
3064     ACTUAL_START_DATE,
3065     ACTUAL_END_DATE,
3066     LOT_NUMBER,
3067     COMPLETION_SUBINVENTORY,
3068     COMPLETION_LOCATOR_ID,
3069     SECURITY_GROUP_ID,
3070     ATTRIBUTE_CATEGORY,
3071     ATTRIBUTE1,
3072     ATTRIBUTE2,
3073     ATTRIBUTE3,
3074     ATTRIBUTE4,
3075     ATTRIBUTE5,
3076     ATTRIBUTE6,
3077     ATTRIBUTE7,
3078     ATTRIBUTE8,
3079     ATTRIBUTE9,
3080     ATTRIBUTE10,
3081     ATTRIBUTE11,
3082     ATTRIBUTE12,
3083     ATTRIBUTE13,
3084     ATTRIBUTE14,
3085     ATTRIBUTE15
3086   ) VALUES
3087   (
3088     AHL_WORKORDER_TXNS_S.NEXTVAL,
3089     p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER,
3090     p_x_prd_workorder_rec.LAST_UPDATE_DATE,
3091     p_x_prd_workorder_rec.LAST_UPDATED_BY,
3092     p_x_prd_workorder_rec.CREATION_DATE,
3093     p_x_prd_workorder_rec.CREATED_BY,
3094     p_x_prd_workorder_rec.LAST_UPDATE_LOGIN,
3095     p_x_prd_workorder_rec.WORKORDER_ID,
3096     0,  -- check this transaction type code
3097     p_x_prd_workorder_rec.STATUS_CODE,
3098     p_x_prd_workorder_rec.SCHEDULED_START_DATE,
3099     p_x_prd_workorder_rec.SCHEDULED_END_DATE,
3100     p_x_prd_workorder_rec.ACTUAL_START_DATE,
3101     p_x_prd_workorder_rec.ACTUAL_END_DATE,
3102     NULL,
3103     p_x_prd_workorder_rec.COMPLETION_SUBINVENTORY,
3104     p_x_prd_workorder_rec.COMPLETION_LOCATOR_ID,
3105     p_x_prd_workorder_rec.SECURITY_GROUP_ID,
3106     p_x_prd_workorder_rec.ATTRIBUTE_CATEGORY,
3107     p_x_prd_workorder_rec.ATTRIBUTE1,
3108     p_x_prd_workorder_rec.ATTRIBUTE2,
3109     p_x_prd_workorder_rec.ATTRIBUTE3,
3110     p_x_prd_workorder_rec.ATTRIBUTE4,
3111     p_x_prd_workorder_rec.ATTRIBUTE5,
3112     p_x_prd_workorder_rec.ATTRIBUTE6,
3113     p_x_prd_workorder_rec.ATTRIBUTE7,
3114     p_x_prd_workorder_rec.ATTRIBUTE8,
3115     p_x_prd_workorder_rec.ATTRIBUTE9,
3116     p_x_prd_workorder_rec.ATTRIBUTE10,
3117     p_x_prd_workorder_rec.ATTRIBUTE11,
3118     p_x_prd_workorder_rec.ATTRIBUTE12,
3119     p_x_prd_workorder_rec.ATTRIBUTE13,
3120     p_x_prd_workorder_rec.ATTRIBUTE14,
3121     p_x_prd_workorder_rec.ATTRIBUTE15
3122   );
3123 
3124   IF ( p_x_prd_workorder_rec.master_workorder_flag = 'N' ) THEN
3125 
3126     IF ( G_DEBUG = 'Y' ) THEN
3127       AHL_DEBUG_PUB.debug( l_api_name || ' - Before get_operations. Route ID: '|| p_x_prd_workorder_rec.route_id );
3128     END IF;
3129 
3130     get_operations
3131     (
3132       p_workorder_rec      => p_x_prd_workorder_rec,
3133       p_x_operations_tbl   => x_operation_tbl
3134     );
3135 
3136     IF ( G_DEBUG = 'Y' ) THEN
3137       AHL_DEBUG_PUB.debug( l_api_name || ' - Before AHL_PRD_OPERATIONS_PVT.process_operations . Total Operations : '|| x_operation_tbl.COUNT );
3138     END IF;
3139 
3140     AHL_PRD_OPERATIONS_PVT.process_operations
3141     (
3142       p_api_version                  => 1.0,
3143       p_init_msg_list                => FND_API.G_TRUE,
3144       p_commit                       => FND_API.G_FALSE,
3145       p_validation_level             => FND_API.G_VALID_LEVEL_FULL,
3146       p_default                      => FND_API.G_TRUE,
3147       p_module_type                  => NULL,
3148       p_wip_mass_load_flag           => 'N',
3149       x_return_status                => l_return_status,
3150       x_msg_count                    => l_msg_count,
3151       x_msg_data                     => l_msg_data,
3152       p_x_prd_operation_tbl          => x_operation_tbl
3153     );
3154 
3155     IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
3156       RAISE FND_API.G_EXC_ERROR;
3157     END IF;
3158 
3159     IF ( p_x_prd_workorder_rec.route_id IS NOT NULL ) THEN
3160 
3161       IF ( G_DEBUG = 'Y' ) THEN
3162         AHL_DEBUG_PUB.debug( l_api_name || ' - Before get_rt_resource_req' );
3163       END IF;
3164 
3165       get_rt_resource_req
3166       (
3167         p_workorder_rec  => p_x_prd_workorder_rec,
3168         p_operation_tbl  => x_operation_tbl,
3169         p_x_resource_tbl => x_resource_tbl
3170       );
3171 
3172       IF ( x_resource_tbl.COUNT > 0 ) THEN
3173 
3174         IF ( G_DEBUG = 'Y' ) THEN
3175           AHL_DEBUG_PUB.debug( l_api_name || ' - Before AHL_PP_RESRC_REQUIRE_PVT.process_resrc_require' );
3176         END IF;
3177 
3178         AHL_PP_RESRC_REQUIRE_PVT.process_resrc_require
3179         (
3180           p_api_version                  => 1.0,
3181           p_init_msg_list                => FND_API.G_TRUE,
3182           p_commit                       => FND_API.G_FALSE,
3183           p_validation_level             => FND_API.G_VALID_LEVEL_FULL,
3184           p_module_type                  => NULL,
3185           p_interface_flag               => 'N',
3186           p_operation_flag               => 'C',
3187           p_x_resrc_require_tbl          => x_resource_tbl,
3188           x_return_status                => l_return_status,
3189           x_msg_count                    => l_msg_count,
3190           x_msg_data                     => l_msg_data
3191         );
3192 
3193         IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
3194           RAISE FND_API.G_EXC_ERROR;
3195         END IF;
3196       END IF;
3197 
3198       IF ( G_DEBUG = 'Y' ) THEN
3199         AHL_DEBUG_PUB.debug( l_api_name || ' - Before get_rt_material_req' );
3200       END IF;
3201    --Code changes made by Srini
3202 
3203   AHL_PP_MATERIALS_PVT.Process_Wo_Op_Materials
3204       (
3205         p_api_version          => 1.0,
3206         p_init_msg_list        => Fnd_Api.G_TRUE,
3207         p_commit               => Fnd_Api.G_FALSE,
3208         p_validation_level     => Fnd_Api.G_VALID_LEVEL_FULL,
3209         p_operation_flag       => 'C',
3210 	    p_prd_wooperation_tbl  => x_operation_tbl,
3211         x_req_material_tbl     => x_material_tbl,
3212         x_return_status        => l_return_status,
3213         x_msg_count            => l_msg_count,
3214         x_msg_data             => l_msg_data
3215        );
3216 
3217         IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
3218           RAISE FND_API.G_EXC_ERROR;
3219         END IF;
3220 
3221         IF ( G_DEBUG = 'Y' ) THEN
3222           AHL_DEBUG_PUB.debug( l_api_name || ' - After AHL_PP_MATERIALS_PVT.Process_Wo_Op_Materials' );
3223         END IF;
3224 
3225     END IF;
3226 
3227   END IF;
3228 
3229   IF ( p_wip_load_flag = 'Y' ) THEN
3230 
3231     IF ( G_DEBUG = 'Y' ) THEN
3232       AHL_DEBUG_PUB.debug( l_api_name || ' - Before AHL_EAM_JOB_PVT.create_eam_workorder' );
3233     END IF;
3234 
3235     AHL_EAM_JOB_PVT.create_eam_workorder
3236     (
3237       p_api_version            => 1.0,
3238       p_init_msg_list          => FND_API.G_TRUE,
3239       p_commit                 => FND_API.G_FALSE,
3240       p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
3241       p_default                => FND_API.G_FALSE,
3242       p_module_type            => NULL,
3243       x_return_status          => l_return_status,
3244       x_msg_count              => l_msg_count,
3245       x_msg_data               => l_msg_data,
3246       p_x_workorder_rec        => p_x_prd_workorder_rec,
3247       p_operation_tbl          => x_operation_tbl,
3248       p_material_req_tbl       => x_material_tbl,
3249       p_resource_req_tbl       => x_resource_tbl
3250     );
3251 
3252     IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
3253       RAISE FND_API.G_EXC_ERROR;
3254     ELSE
3255 
3256       IF ( G_DEBUG = 'Y' ) THEN
3257         AHL_DEBUG_PUB.debug( l_api_name || ' - Before Update AHL_WORKORDERS with wip_entity_id' );
3258       END IF;
3259 
3260       UPDATE AHL_WORKORDERS
3261       SET    wip_entity_id = p_x_prd_workorder_rec.wip_entity_id
3262       WHERE  workorder_id = p_x_prd_workorder_rec.workorder_id;
3263     END IF;
3264 
3265   END IF;
3266 
3267   IF FND_API.to_boolean(p_commit) THEN
3268     COMMIT;
3269   END IF;
3270 
3271   -- User Hooks
3272   IF (JTF_USR_HKS.Ok_to_execute('AHL_PRD_WORKORDER_PVT', 'CREATE_JOB', 'A', 'C' )) then
3273       ahl_prd_workorder_CUHK.create_job_post(
3274         p_prd_workorder_rec => p_x_prd_workorder_rec,
3275         p_operation_tbl =>  x_operation_tbl ,
3276         p_resource_tbl  =>  x_resource_tbl,
3277         p_material_tbl  =>  x_material_tbl,
3278         x_msg_count => l_msg_count,
3279         x_msg_data => l_msg_data,
3280         x_return_status => l_return_status);
3281       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3282         RAISE FND_API.G_EXC_ERROR;
3283       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3284         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3285       END IF;
3286   END IF;
3287 
3288   IF ( G_DEBUG = 'Y' ) THEN
3289     AHL_DEBUG_PUB.debug( l_api_name || ' - Success' );
3290   END IF;
3291 
3292   IF G_DEBUG='Y' THEN
3293     AHL_DEBUG_PUB.disable_debug;
3294   END IF;
3295 
3296 EXCEPTION
3297  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3298     ROLLBACK TO create_job_PVT;
3299     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3300     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
3301                                p_count => x_msg_count,
3302                                p_data  => x_msg_data);
3303  WHEN FND_API.G_EXC_ERROR THEN
3304     ROLLBACK TO create_job_PVT;
3305     x_return_status := FND_API.G_RET_STS_ERROR;
3306     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
3307                                p_count => x_msg_count,
3308                                p_data  => x_msg_data);
3309 
3310  WHEN OTHERS THEN
3311     ROLLBACK TO create_job_PVT;
3312     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3313     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3314       FND_MSG_PUB.add_exc_msg(p_pkg_name        =>g_pkg_name,
3315                               p_procedure_name  =>l_api_name,
3316                               p_error_text      => SUBSTR(SQLERRM,1,240));
3317 
3318     END IF;
3319     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
3320                                p_count => x_msg_count,
3321                                p_data  => x_msg_data);
3322 
3323 END create_job;
3324 
3325 PROCEDURE update_job
3326 (
3327  p_api_version           IN            NUMBER    := 1.0,
3328  p_init_msg_list         IN            VARCHAR2  := FND_API.G_TRUE,
3329  p_commit                IN            VARCHAR2  := FND_API.G_FALSE,
3330  p_validation_level      IN            NUMBER    := FND_API.G_VALID_LEVEL_FULL,
3331  p_default               IN            VARCHAR2  := FND_API.G_FALSE,
3332  p_module_type           IN            VARCHAR2,
3333  x_return_status         OUT NOCOPY    VARCHAR2,
3334  x_msg_count             OUT NOCOPY    NUMBER,
3335  x_msg_data              OUT NOCOPY    VARCHAR2,
3336  p_wip_load_flag         IN            VARCHAR2   := 'Y',
3337  p_x_prd_workorder_rec   IN OUT NOCOPY prd_workorder_rec,
3338  p_x_prd_workoper_tbl    IN OUT NOCOPY prd_workoper_tbl
3339 )
3340 AS
3341   l_api_name     CONSTANT VARCHAR2(30) := 'UPDATE_JOB'; -- adithya::Corrected the variable precision
3342   l_api_version  CONSTANT NUMBER       := 1.0;
3343   l_msg_count             NUMBER;
3344   l_msg_data              VARCHAR2(2000);
3345   l_return_status         VARCHAR2(1);
3346   l_job_return_status     VARCHAR2(1);
3347   l_prd_workoper_tbl      AHL_PRD_OPERATIONS_PVT.prd_operation_tbl;
3348   l_resource_tbl          AHL_PP_RESRC_REQUIRE_PVT.resrc_require_tbl_type;
3349   l_material_tbl          AHL_PP_MATERIALS_PVT.req_material_tbl_type;
3350   l_parent_workorder_rec  prd_workorder_rec;
3351   l_parent_workoper_tbl   prd_workoper_tbl;
3352   l_wo_status             VARCHAR2(80);
3353   l_wo_status_code        VARCHAR2(30);
3354 
3355   l_debug_module CONSTANT VARCHAR2(100) := 'ahl.plsql.AHL_PRD_WORKORDER_PVT.UPDATE_JOB';
3356 
3357   -- Bug # 7643668 (FP for Bug # 6493302) -- start
3358   CURSOR  get_parent_workorders( c_child_wip_entity_id NUMBER )
3359   IS
3360   SELECT  WO.workorder_id,
3361           WO.object_version_number,
3362           WO.wip_entity_id,
3363           WO.visit_task_id,
3364           WO.status_code,
3365           WIPJ.scheduled_start_date,
3366           WIPJ.scheduled_completion_date scheduled_end_date,
3367           WO.actual_start_date,
3368    	  WO.actual_end_date
3369   FROM    AHL_WORKORDERS WO,
3370           WIP_SCHED_RELATIONSHIPS WOR,
3371           WIP_DISCRETE_JOBS wipj
3372   WHERE
3373           WIPJ.wip_entity_id = WO.wip_entity_id
3374   AND     WO.wip_entity_id = WOR.parent_object_id
3375   AND     WO.master_workorder_flag = 'Y'
3376   AND     WO.status_code <> G_JOB_STATUS_DELETED
3377   AND     WOR.parent_object_type_id = 1
3378   AND     WOR.relationship_type = 1
3379   AND     WOR.child_object_type_id = 1
3380   AND     WOR.child_object_id = c_child_wip_entity_id;
3381   -- Bug # 7643668 (FP for Bug # 6493302) -- end
3382 
3383   CURSOR  get_child_workorders( c_wip_entity_id NUMBER )
3384   IS
3385   SELECT  WDJ.scheduled_start_date scheduled_start_date,
3386           WDJ.scheduled_completion_date scheduled_end_date,
3387           WO.actual_start_date actual_start_date,
3388           WO.actual_end_date actual_end_date,
3389           WO.status_code status_code
3390   FROM    WIP_DISCRETE_JOBS WDJ,
3391           AHL_WORKORDERS WO
3392   WHERE   WDJ.wip_entity_id = WO.wip_entity_id
3393   AND     WO.status_code <> G_JOB_STATUS_DELETED
3394   AND     WO.wip_entity_id in
3395           (
3396             SELECT     child_object_id
3397             FROM       WIP_SCHED_RELATIONSHIPS
3398             WHERE      parent_object_type_id = 1
3399             AND        child_object_type_id = 1
3400             START WITH parent_object_id = c_wip_entity_id
3401                   AND  relationship_type = 1
3402             CONNECT BY parent_object_id = PRIOR child_object_id
3403                   AND  relationship_type = 1
3404           );
3405 		-- bug4393092
3406 		-- Bug # 6680137 -- begin
3407 		CURSOR get_wo_status(c_workorder_id VARCHAR2)
3408 		IS
3409 		SELECT AWOS.status_code,
3410 		AWOS.workorder_name,
3411 		FNDL.meaning
3412 		FROM AHL_WORKORDERS AWOS,
3413 					FND_LOOKUP_VALUES_VL FNDL
3414 		WHERE AWOS.WORKORDER_ID = c_workorder_id
3415 		AND FNDL.lookup_type = 'AHL_JOB_STATUS'
3416 		AND FNDL.lookup_code(+) = AWOS.status_code;
3417 		-- Bug # 6680137 -- end
3418 
3419   -- Added for R12: Serial Reservation.
3420   CURSOR get_scheduled_mater_csr (p_workorder_id IN NUMBER) IS
3421     SELECT scheduled_material_id
3422     FROM  ahl_job_oper_materials_v
3423     WHERE workorder_id = p_workorder_id
3424       AND reserved_quantity > 0;
3425 
3426   -- Added for R12: Tech UI Login/Logout feature.
3427   -- Logout all employees logged in at all levels when cancelling a workorder.
3428   CURSOR c_get_login_recs(p_workorder_id NUMBER)
3429   IS
3430      SELECT employee_id, operation_seq_num, resource_seq_num
3431      FROM   ahl_work_login_times
3432      WHERE  workorder_id = p_workorder_id
3433      AND LOGOUT_DATE IS NULL;
3434 
3435   -- Fix for bug# 5347560.
3436   CURSOR chk_inst_in_job (p_workorder_id IN NUMBER) IS
3437        SELECT 'x'
3438        FROM  CSI_ITEM_INSTANCES CII, AHL_WORKORDERS AWO
3439        WHERE CII.WIP_JOB_ID = AWO.WIP_ENTITY_ID
3440          AND AWO.workorder_id = p_workorder_id
3441          AND ACTIVE_START_DATE <= SYSDATE
3442          AND ((ACTIVE_END_DATE IS NULL) OR (ACTIVE_END_DATE >= SYSDATE))
3443          AND LOCATION_TYPE_CODE = 'WIP'
3444          AND NOT EXISTS (SELECT 'X' FROM CSI_II_RELATIONSHIPS CIR
3445                          WHERE CIR.SUBJECT_ID = CII.INSTANCE_ID
3446                            AND CIR.RELATIONSHIP_TYPE_CODE = 'COMPONENT-OF'
3447                            AND SYSDATE BETWEEN NVL(ACTIVE_START_DATE,SYSDATE) AND NVL(ACTIVE_END_DATE,SYSDATE));
3448 
3449   -- Cursor added for bug # 6680137
3450   CURSOR c_wo_vtsk_id(p_wo_id NUMBER)
3451   IS
3452   SELECT
3453     visit_task_id
3454   FROM
3455     AHL_WORKORDERS
3456   WHERE
3457     workorder_id = p_wo_id;
3458 
3459   l_scheduled_start_date DATE;
3460   l_scheduled_end_date   DATE;
3461   l_actual_start_date    DATE;
3462   l_actual_end_date      DATE;
3463   l_status_code          VARCHAR2(30);
3464   l_status_code_multi    VARCHAR2(30) := 'MULTIPLE_STATUSES';
3465 
3466   l_employee_id          NUMBER;
3467   l_operation_seq_num    NUMBER;
3468   l_resource_seq_num     NUMBER;
3469   l_workorder_name       ahl_workorders.workorder_name%TYPE;
3470 
3471 -- Begin OGMA Issue # 105 - Balaji
3472 -- cursor for checking if given task is planned task created form forecasted UE.
3473 CURSOR c_can_update_quantity(p_task_id NUMBER)
3474 IS
3475 SELECT
3476   'X'
3477 FROM
3478   ahl_visit_tasks_b vtsk
3479 WHERE
3480   vtsk.quantity IS NULL AND
3481   vtsk.status_code <> 'DELETED' AND
3482   vtsk.visit_task_id = p_task_id
3483 
3484 UNION
3485 
3486 SELECT
3487   'X'
3488 FROM
3489   ahl_visit_tasks_b vtsk,
3490   ahl_unit_effectivities_b aue
3491 WHERE
3492   nvl(aue.manually_planned_flag, 'N') = 'N' AND
3493   vtsk.unit_effectivity_id = aue.unit_effectivity_id AND
3494   vtsk.status_code <> 'DELETED' AND
3495   vtsk.visit_task_id = p_task_id;
3496 
3497 -- cursor for getting instance quantity for planned tasks.
3498 CURSOR c_get_instance_quantity(p_task_id NUMBER)
3499 IS
3500 SELECT
3501   csi.quantity
3502 FROM
3503   csi_item_instances csi,
3504   ahl_visit_tasks_b vtsk
3505 WHERE
3506   vtsk.instance_id = csi.instance_id AND
3507   vtsk.status_code <> 'DELETED' AND
3508   vtsk.visit_task_id = p_task_id;
3509 
3510 l_instance_quantity NUMBER;
3511 l_can_update_quantity VARCHAR2(1);
3512 
3513 CURSOR c_get_current_WO_status(p_workorder_id NUMBER)
3514 IS
3515 SELECT
3516   status_code
3517 FROM
3518   ahl_workorders
3519 WHERE
3520   workorder_id = p_workorder_id;
3521 
3522 l_curr_wo_status VARCHAR2(30);
3523 -- End OGMA Issue # 105 - Balaji
3524 
3525   -- Bug # 6680137 -- start
3526   l_status_meaning        VARCHAR2(80);
3527   -- Bug # 6680137 -- end
3528 
3529   -- Added for FP bug# 7238868
3530   l_department_id        NUMBER;
3531   l_department_name      bom_departments.description%TYPE;
3532   l_oper_found           VARCHAR2(1);
3533 
3534   CURSOR get_operation_details(p_operation_code VARCHAR2)
3535     IS
3536     SELECT OP.operation_id
3537     FROM   AHL_OPERATIONS_VL OP
3538     WHERE  OP.concatenated_segments=p_operation_code
3539     AND    OP.revision_number IN
3540            ( SELECT MAX(OP1.revision_number)
3541              FROM   AHL_OPERATIONS_B_KFV OP1
3542              WHERE  OP1.concatenated_segments=OP.concatenated_segments
3543              AND    TRUNC(SYSDATE) BETWEEN TRUNC(OP1.start_date_active) AND
3544                                            TRUNC(NVL(OP1.end_date_active,SYSDATE+1))
3545              AND    OP1.revision_status_code='COMPLETE'
3546            );
3547   -- End FP bug# 7238868
3548 
3549 BEGIN
3550   SAVEPOINT update_job_PVT;
3551 
3552   IF NOT FND_API.COMPATIBLE_API_CALL(l_api_version,
3553                                      p_api_version,
3554                                      l_api_name,G_PKG_NAME) THEN
3555     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3556   END IF;
3557 
3558   IF FND_API.to_boolean(p_init_msg_list) THEN
3559     FND_MSG_PUB.initialize;
3560   END IF;
3561 
3562   x_return_status:=FND_API.G_RET_STS_SUCCESS;
3563 
3564   IF G_DEBUG='Y' THEN
3565     AHL_DEBUG_PUB.enable_debug;
3566   END IF;
3567 
3568   IF ( G_DEBUG = 'Y' ) THEN
3569     AHL_DEBUG_PUB.debug( l_api_name || ' - Before default_missing_attributes' );
3570   END IF;
3571 --Modified by Srini
3572  /*IF p_module_type = 'API'
3573  THEN
3574    G_CALLED_FROM := 'API';
3575  END IF;*/
3576  G_CALLED_FROM := p_module_type;
3577 
3578  -- User Hooks
3579   IF (JTF_USR_HKS.Ok_to_execute('AHL_PRD_WORKORDER_PVT', 'UPDATE_JOB', 'B', 'C' )) THEN
3580      ahl_prd_workorder_CUHK.update_job_pre(
3581        p_prd_workorder_rec => p_x_prd_workorder_rec,
3582        p_prd_workoper_tbl  =>  p_x_prd_workoper_tbl,
3583        x_msg_count => l_msg_count,
3584        x_msg_data => l_msg_data,
3585        x_return_status => l_return_status);
3586      IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3587        RAISE FND_API.G_EXC_ERROR;
3588      ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3589        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3590      END IF;
3591   END IF;
3592 
3593   --IF FND_API.to_boolean(p_default) THEN
3594     default_missing_attributes
3595     (
3596       p_x_prd_workorder_rec
3597     );
3598   --END IF;
3599 
3600   l_msg_count := FND_MSG_PUB.count_msg;
3601   IF l_msg_count > 0 THEN
3602     x_msg_count := l_msg_count;
3603     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3604     RAISE FND_API.G_EXC_ERROR;
3605   END IF;
3606 
3607   IF (p_validation_level = FND_API.G_VALID_LEVEL_FULL ) THEN
3608 
3609     IF ( G_DEBUG = 'Y' ) THEN
3610       AHL_DEBUG_PUB.debug( l_api_name || ' - Before convert_values_to_ids' );
3611     END IF;
3612 
3613     convert_values_to_ids
3614     (
3615       p_x_prd_workorder_rec   =>p_x_prd_workorder_rec
3616     );
3617   END IF;
3618 
3619   l_msg_count := FND_MSG_PUB.count_msg;
3620   IF l_msg_count > 0 THEN
3621     x_msg_count := l_msg_count;
3622     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3623     RAISE FND_API.G_EXC_ERROR;
3624   END IF;
3625 
3626   IF (p_validation_level = FND_API.G_VALID_LEVEL_FULL ) THEN
3627 
3628     IF ( G_DEBUG = 'Y' ) THEN
3629       AHL_DEBUG_PUB.debug( l_api_name || ' - Before validate_workorder' );
3630     END IF;
3631 
3632     validate_workorder
3633     (
3634       p_prd_workorder_rec            =>p_x_prd_workorder_rec,
3635       p_wip_load_flag                =>p_wip_load_flag
3636     );
3637 
3638     IF  p_x_prd_workoper_tbl.COUNT > 0 THEN
3639         -- Bug # 6680137 -- begin
3640 	OPEN get_wo_status(p_x_prd_workorder_rec.workorder_id);
3641 	FETCH get_wo_status INTO l_wo_status_code, l_workorder_name, l_wo_status;
3642 	CLOSE get_wo_status;
3643 	-- Bug # 6680137 -- end
3644 	IF l_wo_status_code IN ('22','7','12','4','5') THEN
3645     		FND_MESSAGE.set_name('AHL','AHL_PRD_UPD_WO_STS');
3646 		FND_MESSAGE.set_token('WO_STATUS', l_wo_status);
3647 		FND_MSG_PUB.add;
3648 		RAISE FND_API.G_EXC_ERROR;
3649 	END IF;
3650     END IF;
3651 
3652 
3653   END IF;
3654 
3655   l_msg_count := FND_MSG_PUB.count_msg;
3656   IF l_msg_count > 0 THEN
3657     x_msg_count := l_msg_count;
3658     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3659     RAISE FND_API.G_EXC_ERROR;
3660   END IF;
3661 
3662   -- fix for bug# 5347560. In case of cancelling or closing a wo check if
3663   -- there are any trackable materials in the job.
3664   IF (p_x_prd_workorder_rec.status_code IN (G_JOB_STATUS_CANCELLED, G_JOB_STATUS_COMPLETE_NC,
3665                                             G_JOB_STATUS_CLOSED)) THEN
3666     OPEN chk_inst_in_job(p_x_prd_workorder_rec.workorder_id);
3667     FETCH chk_inst_in_job INTO l_workorder_name;
3668     IF (chk_inst_in_job%FOUND) THEN
3669         -- Bug # 6680137 -- begin
3670         -- show the right workorder name and status
3671         OPEN get_wo_status(p_x_prd_workorder_rec.workorder_id);
3672         FETCH get_wo_status INTO l_wo_status_code, l_workorder_name, l_wo_status;
3673         CLOSE get_wo_status;
3674 
3675         --Get status meaning
3676         SELECT meaning INTO l_status_meaning
3677 	   FROM fnd_lookup_values_vl
3678         WHERE lookup_type = 'AHL_JOB_STATUS'
3679           AND LOOKUP_CODE = p_x_prd_workorder_rec.status_code;
3680         -- Bug # 6680137 -- end
3681         FND_MESSAGE.set_name('AHL','AHL_PRD_MAT_NOT_RETURN');
3682         FND_MESSAGE.set_token('WO_STATUS', l_status_meaning);
3683         FND_MESSAGE.set_token('WO_NAME', l_workorder_name);
3684         FND_MSG_PUB.add;
3685         RAISE FND_API.G_EXC_ERROR;
3686     END IF;
3687     CLOSE chk_inst_in_job;
3688   END IF;
3689 
3690   -- R12 Tech UI enhancement project.
3691   -- Log all technicians out of the workorder being cancelled.
3692   IF (p_x_prd_workorder_rec.status_code IN (G_JOB_STATUS_COMPLETE_NC, G_JOB_STATUS_COMPLETE,
3693                                             G_JOB_STATUS_CANCELLED)) THEN
3694       IF ( G_DEBUG = 'Y' ) THEN
3695         AHL_DEBUG_PUB.debug( l_api_name || ' Before - AHL_PRD_WO_LOGIN_PVT.Workorder_Logout');
3696         AHL_DEBUG_PUB.debug( 'Workorder Status:' || p_x_prd_workorder_rec.status_code);
3697       END IF;
3698 
3699       OPEN c_get_login_recs(p_x_prd_workorder_rec.workorder_id);
3700       LOOP
3701       FETCH c_get_login_recs INTO l_employee_id, l_operation_seq_num, l_resource_seq_num;
3702       EXIT WHEN c_get_login_recs%NOTFOUND;
3703           AHL_PRD_WO_LOGIN_PVT.Workorder_Logout(p_api_version       => 1.0,
3704                                           p_init_msg_list     => p_init_msg_list,
3705                                           p_commit            => FND_API.G_FALSE,
3706                                           p_validation_level  => p_validation_level,
3707                                           x_return_status     => l_return_status,
3708                                           x_msg_count         => l_msg_count,
3709                                           x_msg_data          => l_msg_data,
3710                                           p_employee_id       => l_employee_id,
3711                                           p_workorder_id      => p_x_prd_workorder_rec.workorder_id,
3712                                           p_operation_seq_num => l_operation_seq_num,
3713                                           p_resource_seq_num  => l_resource_seq_num
3714                                          );
3715           IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3716             EXIT;
3717           END IF;
3718       END LOOP;
3719       CLOSE c_get_login_recs;
3720 
3721       IF ( G_DEBUG = 'Y' ) THEN
3722         AHL_DEBUG_PUB.debug( l_api_name || ' Before - AHL_PRD_WO_LOGIN_PVT.Workorder_Logout');
3723         AHL_DEBUG_PUB.debug( 'Return Status:' || l_return_status);
3724       END IF;
3725 
3726   END IF; -- p_x_prd_workorder_rec.STATUS_CODE
3727 
3728   IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3729         RAISE FND_API.G_EXC_ERROR;
3730   END IF;
3731 
3732   -- hold reason is only for on-hold and parts-hold.
3733   -- FP Bug# 7631453
3734   IF p_x_prd_workorder_rec.STATUS_CODE NOT IN ('6', '19') THEN
3735       p_x_prd_workorder_rec.HOLD_REASON_CODE := NULL;
3736   END IF;
3737 
3738   IF ( G_DEBUG = 'Y' ) THEN
3739     AHL_DEBUG_PUB.debug( l_api_name || ' - Before Updating AHL_WORKORDERS' );
3740   END IF;
3741 
3742   -- Begin OGMA Issue # 105 - Balaji
3743   -- update VWP planned task quantity current instance quantity
3744   -- this logic need to be moved to VWP later as per discussion with Shailaja and Jay.
3745 
3746   OPEN c_get_current_WO_status(p_x_prd_workorder_rec.workorder_id);
3747   FETCH c_get_current_WO_status INTO l_curr_wo_status;
3748   CLOSE c_get_current_WO_status;
3749 
3750   IF p_x_prd_workorder_rec.visit_task_id IS NOT NULL AND l_curr_wo_status = '17'
3751   THEN
3752 
3753           OPEN c_can_update_quantity(p_x_prd_workorder_rec.visit_task_id);
3754           FETCH c_can_update_quantity INTO l_can_update_quantity;
3755           CLOSE c_can_update_quantity;
3756 
3757           IF l_can_update_quantity IS NOT NULL
3758           THEN
3759 
3760 		  OPEN c_get_instance_quantity(p_x_prd_workorder_rec.visit_task_id);
3761 		  FETCH c_get_instance_quantity INTO l_instance_quantity;
3762 		  CLOSE c_get_instance_quantity;
3763 
3764 		  UPDATE
3765 		   ahl_visit_tasks_b
3766 		  SET
3767 		   quantity = l_instance_quantity
3768 		  WHERE
3769 		   visit_task_id = p_x_prd_workorder_rec.visit_task_id;
3770 	  END IF;
3771   END IF;
3772   -- End OGMA Issue # 105 - Balaji
3773 
3774   UPDATE AHL_WORKORDERS SET
3775     OBJECT_VERSION_NUMBER   =p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER + 1,
3776     LAST_UPDATE_DATE        =NVL(p_x_prd_workorder_rec.LAST_UPDATE_DATE,SYSDATE),
3777     LAST_UPDATED_BY         =NVL(p_x_prd_workorder_rec.LAST_UPDATED_BY,FND_GLOBAL.user_id),
3778     LAST_UPDATE_LOGIN       =NVL(p_x_prd_workorder_rec.LAST_UPDATE_LOGIN,FND_GLOBAL.user_id),
3779     STATUS_CODE             =p_x_prd_workorder_rec.STATUS_CODE,
3780     ACTUAL_START_DATE       =p_x_prd_workorder_rec.ACTUAL_START_DATE,
3781     ACTUAL_END_DATE         =p_x_prd_workorder_rec.ACTUAL_END_DATE,
3782     CONFIRM_FAILURE_FLAG    =p_x_prd_workorder_rec.CONFIRM_FAILURE_FLAG,
3783     SECURITY_GROUP_ID       =p_x_prd_workorder_rec.SECURITY_GROUP_ID,
3784     ATTRIBUTE_CATEGORY      =p_x_prd_workorder_rec.ATTRIBUTE_CATEGORY,
3785     ATTRIBUTE1              =p_x_prd_workorder_rec.ATTRIBUTE1,
3786     ATTRIBUTE2              =p_x_prd_workorder_rec.ATTRIBUTE2,
3787     ATTRIBUTE3              =p_x_prd_workorder_rec.ATTRIBUTE3,
3788     ATTRIBUTE4              =p_x_prd_workorder_rec.ATTRIBUTE4,
3789     ATTRIBUTE5              =p_x_prd_workorder_rec.ATTRIBUTE5,
3790     ATTRIBUTE6              =p_x_prd_workorder_rec.ATTRIBUTE6,
3791     ATTRIBUTE7              =p_x_prd_workorder_rec.ATTRIBUTE7,
3792     ATTRIBUTE8              =p_x_prd_workorder_rec.ATTRIBUTE8,
3793     ATTRIBUTE9              =p_x_prd_workorder_rec.ATTRIBUTE9,
3794     ATTRIBUTE10             =p_x_prd_workorder_rec.ATTRIBUTE10,
3795     ATTRIBUTE11             =p_x_prd_workorder_rec.ATTRIBUTE11,
3796     ATTRIBUTE12             =p_x_prd_workorder_rec.ATTRIBUTE12,
3797     ATTRIBUTE13             =p_x_prd_workorder_rec.ATTRIBUTE13,
3798     ATTRIBUTE14             =p_x_prd_workorder_rec.ATTRIBUTE14,
3799     ATTRIBUTE15             =p_x_prd_workorder_rec.ATTRIBUTE15,
3800     HOLD_REASON_CODE        =p_x_prd_workorder_rec.HOLD_REASON_CODE
3801   WHERE WORKORDER_ID=p_x_prd_workorder_rec.WORKORDER_ID
3802   AND   OBJECT_VERSION_NUMBER=p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER;
3803 
3804   p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER := p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER + 1;
3805 
3806   IF SQL%NOTFOUND THEN
3807     FND_MESSAGE.SET_NAME('AHL','AHL_COM_RECORD_CHANGED');
3808     FND_MSG_PUB.ADD;
3809     RAISE FND_API.G_EXC_ERROR;
3810   END IF;
3811 
3812   IF ( G_DEBUG = 'Y' ) THEN
3813     AHL_DEBUG_PUB.debug( l_api_name || ' - Before Inserting into AHL_WORKORDER_TXNS' );
3814   END IF;
3815 
3816   INSERT INTO AHL_WORKORDER_TXNS
3817   (
3818     WORKORDER_TXN_ID,
3819     OBJECT_VERSION_NUMBER,
3820     LAST_UPDATE_DATE,
3821     LAST_UPDATED_BY,
3822     CREATION_DATE,
3823     CREATED_BY,
3824     LAST_UPDATE_LOGIN,
3825     WORKORDER_ID,
3826     TRANSACTION_TYPE_CODE,
3827     STATUS_CODE,
3828     SCHEDULED_START_DATE,
3829     SCHEDULED_END_DATE,
3830     ACTUAL_START_DATE,
3831     ACTUAL_END_DATE,
3832     LOT_NUMBER,
3833     COMPLETION_SUBINVENTORY,
3834     COMPLETION_LOCATOR_ID,
3835     HOLD_REASON_CODE
3836   ) VALUES
3837   (
3838     AHL_WORKORDER_TXNS_S.NEXTVAL,
3839     NVL(p_x_prd_workorder_rec.OBJECT_VERSION_NUMBER,1),
3840     NVL(p_x_prd_workorder_rec.LAST_UPDATE_DATE,SYSDATE),
3841     NVL(p_x_prd_workorder_rec.LAST_UPDATED_BY,FND_GLOBAL.USER_ID),
3842     NVL(p_x_prd_workorder_rec.CREATION_DATE,SYSDATE),
3843     NVL(p_x_prd_workorder_rec.CREATED_BY,FND_GLOBAL.USER_ID),
3844     NVL(p_x_prd_workorder_rec.LAST_UPDATE_LOGIN,FND_GLOBAL.USER_ID),
3845     p_x_prd_workorder_rec.WORKORDER_ID,
3846     0,
3847     p_x_prd_workorder_rec.STATUS_CODE,
3848     p_x_prd_workorder_rec.SCHEDULED_START_DATE,
3849     p_x_prd_workorder_rec.SCHEDULED_END_DATE,
3850     p_x_prd_workorder_rec.ACTUAL_START_DATE,
3851     p_x_prd_workorder_rec.ACTUAL_END_DATE,
3852     0,
3853     p_x_prd_workorder_rec.COMPLETION_SUBINVENTORY,
3854     p_x_prd_workorder_rec.COMPLETION_LOCATOR_ID,
3855     p_x_prd_workorder_rec.HOLD_REASON_CODE
3856   );
3857 
3858   IF p_x_prd_workoper_tbl.COUNT >0 THEN
3859     FOR i in p_x_prd_workoper_tbl.FIRST..p_x_prd_workoper_tbl.LAST
3860     LOOP
3861       l_prd_workoper_tbl(i).WORKORDER_OPERATION_ID :=p_x_prd_workoper_tbl(i).WORKORDER_OPERATION_ID;
3862       l_prd_workoper_tbl(i).ORGANIZATION_ID        :=p_x_prd_workoper_tbl(i).ORGANIZATION_ID;
3863       l_prd_workoper_tbl(i).OPERATION_SEQUENCE_NUM :=p_x_prd_workoper_tbl(i).OPERATION_SEQUENCE_NUM;
3864       l_prd_workoper_tbl(i).OPERATION_DESCRIPTION  :=p_x_prd_workoper_tbl(i).OPERATION_DESCRIPTION;
3865       l_prd_workoper_tbl(i).WORKORDER_ID           :=p_x_prd_workoper_tbl(i).WORKORDER_ID;
3866       l_prd_workoper_tbl(i).WIP_ENTITY_ID          :=p_x_prd_workoper_tbl(i).WIP_ENTITY_ID;
3867       l_prd_workoper_tbl(i).ROUTE_ID               :=p_x_prd_workoper_tbl(i).ROUTE_ID;
3868       l_prd_workoper_tbl(i).OBJECT_VERSION_NUMBER  :=p_x_prd_workoper_tbl(i).OBJECT_VERSION_NUMBER;
3869       l_prd_workoper_tbl(i).LAST_UPDATE_DATE       :=p_x_prd_workoper_tbl(i).LAST_UPDATE_DATE;
3870       l_prd_workoper_tbl(i).LAST_UPDATED_BY        :=p_x_prd_workoper_tbl(i).LAST_UPDATED_BY;
3871       l_prd_workoper_tbl(i).CREATION_DATE          :=p_x_prd_workoper_tbl(i).CREATION_DATE;
3872       l_prd_workoper_tbl(i).CREATED_BY             :=p_x_prd_workoper_tbl(i).CREATED_BY;
3873       l_prd_workoper_tbl(i).LAST_UPDATE_LOGIN      :=p_x_prd_workoper_tbl(i).LAST_UPDATE_LOGIN;
3874       l_prd_workoper_tbl(i).DEPARTMENT_ID          :=p_x_prd_workoper_tbl(i).DEPARTMENT_ID;
3875       l_prd_workoper_tbl(i).DEPARTMENT_NAME        :=p_x_prd_workoper_tbl(i).DEPARTMENT_NAME;
3876 
3877       -- For new operations, default operation department based on resources - FP Bug# 7238868
3878       IF (p_x_prd_workoper_tbl(i).OPERATION_CODE IS NOT NULL
3879           AND p_x_prd_workoper_tbl(i).DML_OPERATION = 'C' ) THEN  -- route operation.
3880 
3881 
3882          -- first find the operation id.
3883          OPEN get_operation_details(p_x_prd_workoper_tbl(i).OPERATION_CODE);
3884          FETCH get_operation_details INTO p_x_prd_workoper_tbl(i).operation_id;
3885          CLOSE get_operation_details;
3886 
3887          IF ( G_DEBUG = 'Y' ) THEN
3888             AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'dept id:' || p_x_prd_workorder_rec.department_id);
3889             AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'dept ID(Oper):' || p_x_prd_workoper_tbl(i).DEPARTMENT_ID);
3890             AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'deptnameD(Oper):' || p_x_prd_workoper_tbl(i).DEPARTMENT_NAME);
3891 
3892          END IF;
3893 
3894          --IF (p_x_prd_workoper_tbl(i).DEPARTMENT_ID = p_x_prd_workorder_rec.department_id) THEN
3895          IF (p_x_prd_workoper_tbl(i).operation_id IS NOT NULL) THEN
3896              Get_Default_Rt_Op_dept(p_object_id => p_x_prd_workoper_tbl(i).operation_id,
3897                                     p_association_type => 'OPERATION',
3898                                     p_organization_id => p_x_prd_workorder_rec.organization_id,
3899                                     x_return_status => l_return_status,
3900                                     x_department_id => l_department_id,
3901                                     x_department_name => l_department_name,
3902                                     x_object_resource_found => l_oper_found);
3903 
3904              IF ( G_DEBUG = 'Y' ) THEN
3905                  AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_return_status:' || l_return_status );
3906                  AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_department_id:' || l_department_id);
3907                  AHL_DEBUG_PUB.debug( 'Get_Operations'|| 'l_oper_found:' || l_oper_found);
3908              END IF;
3909 
3910              IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3911                 RAISE FND_API.G_EXC_ERROR;
3912              END IF;
3913 
3914              IF (l_department_id IS NOT NULL) THEN
3915                  l_prd_workoper_tbl(i).department_id := l_department_id;
3916                  l_prd_workoper_tbl(i).DEPARTMENT_NAME := l_department_name;
3917              END IF;
3918          END IF; -- operation id.
3919 
3920       END IF;
3921 
3922       l_prd_workoper_tbl(i).OPERATION_ID           :=p_x_prd_workoper_tbl(i).OPERATION_ID;
3923       l_prd_workoper_tbl(i).OPERATION_CODE         :=p_x_prd_workoper_tbl(i).OPERATION_CODE;
3924       l_prd_workoper_tbl(i).OPERATION_TYPE_CODE    :=p_x_prd_workoper_tbl(i).OPERATION_TYPE_CODE;
3925       l_prd_workoper_tbl(i).OPERATION_TYPE         :=p_x_prd_workoper_tbl(i).OPERATION_TYPE;
3926       l_prd_workoper_tbl(i).STATUS_CODE            :=p_x_prd_workoper_tbl(i).STATUS_CODE;
3927       l_prd_workoper_tbl(i).STATUS_MEANING         :=p_x_prd_workoper_tbl(i).STATUS_MEANING;
3928       l_prd_workoper_tbl(i).PROPAGATE_FLAG         :=p_x_prd_workoper_tbl(i).PROPAGATE_FLAG;
3929       l_prd_workoper_tbl(i).SCHEDULED_START_DATE   :=p_x_prd_workoper_tbl(i).SCHEDULED_START_DATE;
3930       l_prd_workoper_tbl(i).SCHEDULED_START_HR     :=p_x_prd_workoper_tbl(i).SCHEDULED_START_HR;
3931       l_prd_workoper_tbl(i).SCHEDULED_START_MI     :=p_x_prd_workoper_tbl(i).SCHEDULED_START_MI;
3932       l_prd_workoper_tbl(i).SCHEDULED_END_DATE     :=p_x_prd_workoper_tbl(i).SCHEDULED_END_DATE;
3933       l_prd_workoper_tbl(i).SCHEDULED_END_HR       :=p_x_prd_workoper_tbl(i).SCHEDULED_END_HR;
3934       l_prd_workoper_tbl(i).SCHEDULED_END_MI       :=p_x_prd_workoper_tbl(i).SCHEDULED_END_MI;
3935       l_prd_workoper_tbl(i).ACTUAL_START_DATE      :=p_x_prd_workoper_tbl(i).ACTUAL_START_DATE;
3936       l_prd_workoper_tbl(i).ACTUAL_START_HR        :=p_x_prd_workoper_tbl(i).ACTUAL_START_HR;
3937       l_prd_workoper_tbl(i).ACTUAL_START_MI        :=p_x_prd_workoper_tbl(i).ACTUAL_START_MI;
3938       l_prd_workoper_tbl(i).ACTUAL_END_DATE        :=p_x_prd_workoper_tbl(i).ACTUAL_END_DATE;
3939       l_prd_workoper_tbl(i).ACTUAL_END_HR          :=p_x_prd_workoper_tbl(i).ACTUAL_END_HR;
3940       l_prd_workoper_tbl(i).ACTUAL_END_MI          :=p_x_prd_workoper_tbl(i).ACTUAL_END_MI;
3941       l_prd_workoper_tbl(i).PLAN_ID                :=p_x_prd_workoper_tbl(i).PLAN_ID;
3942       l_prd_workoper_tbl(i).COLLECTION_ID          :=p_x_prd_workoper_tbl(i).COLLECTION_ID;
3943       l_prd_workoper_tbl(i).SECURITY_GROUP_ID      :=p_x_prd_workoper_tbl(i).SECURITY_GROUP_ID;
3944       l_prd_workoper_tbl(i).ATTRIBUTE_CATEGORY     :=p_x_prd_workoper_tbl(i).ATTRIBUTE_CATEGORY;
3945       l_prd_workoper_tbl(i).ATTRIBUTE1             :=p_x_prd_workoper_tbl(i).ATTRIBUTE1;
3946       l_prd_workoper_tbl(i).ATTRIBUTE2             :=p_x_prd_workoper_tbl(i).ATTRIBUTE2;
3947       l_prd_workoper_tbl(i).ATTRIBUTE3             :=p_x_prd_workoper_tbl(i).ATTRIBUTE3;
3948       l_prd_workoper_tbl(i).ATTRIBUTE4             :=p_x_prd_workoper_tbl(i).ATTRIBUTE4;
3949       l_prd_workoper_tbl(i).ATTRIBUTE5             :=p_x_prd_workoper_tbl(i).ATTRIBUTE5;
3950       l_prd_workoper_tbl(i).ATTRIBUTE6             :=p_x_prd_workoper_tbl(i).ATTRIBUTE6;
3951       l_prd_workoper_tbl(i).ATTRIBUTE7             :=p_x_prd_workoper_tbl(i).ATTRIBUTE7;
3952       l_prd_workoper_tbl(i).ATTRIBUTE8             :=p_x_prd_workoper_tbl(i).ATTRIBUTE8;
3953       l_prd_workoper_tbl(i).ATTRIBUTE9             :=p_x_prd_workoper_tbl(i).ATTRIBUTE9;
3954       l_prd_workoper_tbl(i).ATTRIBUTE10            :=p_x_prd_workoper_tbl(i).ATTRIBUTE10;
3955       l_prd_workoper_tbl(i).ATTRIBUTE11            :=p_x_prd_workoper_tbl(i).ATTRIBUTE11;
3956       l_prd_workoper_tbl(i).ATTRIBUTE12            :=p_x_prd_workoper_tbl(i).ATTRIBUTE12;
3957       l_prd_workoper_tbl(i).ATTRIBUTE13            :=p_x_prd_workoper_tbl(i).ATTRIBUTE13;
3958       l_prd_workoper_tbl(i).ATTRIBUTE14            :=p_x_prd_workoper_tbl(i).ATTRIBUTE14;
3959       l_prd_workoper_tbl(i).ATTRIBUTE15            :=p_x_prd_workoper_tbl(i).ATTRIBUTE15;
3960       l_prd_workoper_tbl(i).DML_OPERATION          :=p_x_prd_workoper_tbl(i).DML_OPERATION;
3961 
3962     END LOOP;
3963 
3964     IF ( G_DEBUG = 'Y' ) THEN
3965       AHL_DEBUG_PUB.debug( l_api_name || ' - Before AHL_PRD_OPERATIONS_PVT.process_operations' );
3966     END IF;
3967 
3968     AHL_PRD_OPERATIONS_PVT.process_operations
3969     (
3970       p_api_version                  =>1.0,
3971       p_init_msg_list                =>FND_API.G_TRUE,
3972       p_commit                       =>FND_API.G_FALSE,
3973       p_validation_level             =>p_validation_level,
3974       p_default                      =>p_default,
3975       p_module_type                  =>p_module_type,
3976       p_wip_mass_load_flag           =>'N',
3977       x_return_status                =>l_return_status,
3978       x_msg_count                    =>l_msg_count  ,
3979       x_msg_data                     =>l_msg_data,
3980       p_x_prd_operation_tbl          =>l_prd_workoper_tbl
3981     );
3982 
3983     IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
3984       RAISE FND_API.G_EXC_ERROR;
3985     END IF;
3986 
3987     IF ( p_wip_load_flag = 'Y' ) THEN
3988 
3989       IF ( G_DEBUG = 'Y' ) THEN
3990         AHL_DEBUG_PUB.debug( l_api_name || ' - Before get_op_resource_req' );
3991       END IF;
3992 
3993       -- Get the Resource Requirements for New Operations
3994       get_op_resource_req
3995       (
3996         p_workorder_rec  => p_x_prd_workorder_rec,
3997         p_operation_tbl  => l_prd_workoper_tbl,
3998         p_x_resource_tbl => l_resource_tbl
3999       );
4000 
4001       IF ( G_DEBUG = 'Y' ) THEN
4002         AHL_DEBUG_PUB.debug( l_api_name || ' - Before AHL_PP_RESRC_REQUIRE_PVT.process_resrc_require' );
4003       END IF;
4004 
4005       IF ( l_resource_tbl.COUNT > 0 ) THEN
4006         AHL_PP_RESRC_REQUIRE_PVT.process_resrc_require
4007         (
4008           p_api_version                  => 1.0,
4009           p_init_msg_list                => FND_API.G_TRUE,
4010           p_commit                       => FND_API.G_FALSE,
4011           p_validation_level             => FND_API.G_VALID_LEVEL_FULL,
4012           p_module_type                  => NULL,
4013           p_interface_flag               => 'N',
4014           p_operation_flag               => 'C',
4015           p_x_resrc_require_tbl          => l_resource_tbl,
4016           x_return_status                => l_return_status,
4017           x_msg_count                    => l_msg_count,
4018           x_msg_data                     => l_msg_data
4019         );
4020 
4021         IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4022           RAISE FND_API.G_EXC_ERROR;
4023         END IF;
4024       END IF;
4025 
4026       IF ( G_DEBUG = 'Y' ) THEN
4027         AHL_DEBUG_PUB.debug( l_api_name || ' - Before get_op_material_req' );
4028       END IF;
4029 
4030       -- Get the Material Requirements for New Operations
4031       get_op_material_req
4032       (
4033         p_workorder_rec  => p_x_prd_workorder_rec,
4034         p_operation_tbl  => l_prd_workoper_tbl,
4035         p_x_material_tbl => l_material_tbl
4036       );
4037 
4038       IF ( G_DEBUG = 'Y' ) THEN
4039         AHL_DEBUG_PUB.debug( l_api_name || ' - Before AHL_PP_MATERIALS_PVT.create_material_reqst' );
4040       END IF;
4041 
4042       IF ( l_material_tbl.COUNT > 0 ) THEN
4043         AHL_PP_MATERIALS_PVT.create_material_reqst
4044         (
4045           p_api_version                  => 1.0,
4046           p_init_msg_list                => FND_API.G_TRUE,
4047           p_commit                       => FND_API.G_FALSE,
4048           p_validation_level             => FND_API.G_VALID_LEVEL_FULL,
4049           p_interface_flag               => 'N',
4050           p_x_req_material_tbl           => l_material_tbl,
4051           x_job_return_status            => l_job_return_status,
4052           x_return_status                => l_return_status,
4053           x_msg_count                    => l_msg_count,
4054           x_msg_data                     => l_msg_data
4055         );
4056 
4057         IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4058           RAISE FND_API.G_EXC_ERROR;
4059         END IF;
4060       END IF;
4061 
4062     END IF; -- For WIP Load Flag
4063 
4064   END IF; -- For Operations
4065 
4066   IF ( p_wip_load_flag = 'Y' ) THEN
4067 
4068     IF ( G_DEBUG = 'Y' ) THEN
4069       AHL_DEBUG_PUB.debug( l_api_name || ' - Getting parent_workorders' );
4070     END IF;
4071 
4072     FOR parent_csr IN get_parent_workorders( p_x_prd_workorder_rec.wip_entity_id ) LOOP
4073       l_parent_workorder_rec.workorder_id := parent_csr.workorder_id;
4074       l_parent_workorder_rec.object_version_number := parent_csr.object_version_number;
4075 
4076       IF ( G_DEBUG = 'Y' ) THEN
4077         AHL_DEBUG_PUB.debug( l_api_name || ' - parent workorder_id :' || l_parent_workorder_rec.workorder_id );
4078       END IF;
4079 
4080       FOR child_csr IN get_child_workorders( parent_csr.wip_entity_id ) LOOP
4081 
4082         IF ( l_status_code IS NULL ) THEN
4083           l_status_code := child_csr.status_code;
4084         ELSIF ( l_status_code <> child_csr.status_code ) THEN
4085           l_status_code := l_status_code_multi;
4086         END IF;
4087 
4088         IF ( l_scheduled_start_date IS NULL ) THEN
4089           l_scheduled_start_date := child_csr.scheduled_start_date;
4090         ELSIF ( NVL(child_csr.scheduled_start_date, l_scheduled_start_date) < l_scheduled_start_date ) THEN
4091           l_scheduled_start_date := child_csr.scheduled_start_date;
4092         END IF;
4093 
4094         IF ( l_scheduled_end_date IS NULL ) THEN
4095           l_scheduled_end_date := child_csr.scheduled_end_date;
4096         ELSIF ( NVL(child_csr.scheduled_end_date, l_scheduled_end_date) > l_scheduled_end_date ) THEN
4097           l_scheduled_end_date := child_csr.scheduled_end_date;
4098         END IF;
4099 
4100         IF ( l_actual_start_date IS NULL ) THEN
4101           l_actual_start_date := child_csr.actual_start_date;
4102         ELSIF ( NVL(child_csr.actual_start_date, l_actual_start_date) < l_actual_start_date ) THEN
4103           l_actual_start_date := child_csr.actual_start_date;
4104         END IF;
4105 
4106         IF ( l_actual_end_date IS NULL ) THEN
4107           l_actual_end_date := child_csr.actual_end_date;
4108         ELSIF ( NVL(child_csr.actual_end_date, l_actual_end_date) > l_actual_end_date ) THEN
4109           l_actual_end_date := child_csr.actual_end_date;
4110         END IF;
4111 
4112       END LOOP;
4113 
4114       IF ( parent_csr.visit_task_id IS NOT NULL ) THEN
4115         IF ( l_scheduled_start_date IS NULL OR
4116              p_x_prd_workorder_rec.scheduled_start_date < l_scheduled_start_date ) THEN
4117           l_parent_workorder_rec.scheduled_start_date := p_x_prd_workorder_rec.scheduled_start_date;
4118           l_parent_workorder_rec.scheduled_start_hr := p_x_prd_workorder_rec.scheduled_start_hr;
4119           l_parent_workorder_rec.scheduled_start_mi := p_x_prd_workorder_rec.scheduled_start_mi;
4120         ELSE
4121           l_parent_workorder_rec.scheduled_start_date := l_scheduled_start_date;
4122           l_parent_workorder_rec.scheduled_start_hr := TO_NUMBER( TO_CHAR( l_scheduled_start_date, 'HH24' ) );
4123           l_parent_workorder_rec.scheduled_start_mi := TO_NUMBER( TO_CHAR( l_scheduled_start_date, 'MI' ) );
4124         END IF;
4125       END IF;
4126 
4127       IF ( parent_csr.visit_task_id IS NOT NULL ) THEN
4128         IF ( l_scheduled_end_date IS NULL OR
4129              p_x_prd_workorder_rec.scheduled_end_date > l_scheduled_end_date ) THEN
4130           l_parent_workorder_rec.scheduled_end_date := p_x_prd_workorder_rec.scheduled_end_date;
4131           l_parent_workorder_rec.scheduled_end_hr := p_x_prd_workorder_rec.scheduled_end_hr;
4132           l_parent_workorder_rec.scheduled_end_mi := p_x_prd_workorder_rec.scheduled_end_mi;
4133         ELSE
4134           l_parent_workorder_rec.scheduled_end_date := l_scheduled_end_date;
4135           l_parent_workorder_rec.scheduled_end_hr := TO_NUMBER( TO_CHAR( l_scheduled_end_date, 'HH24' ) );
4136           l_parent_workorder_rec.scheduled_end_mi := TO_NUMBER( TO_CHAR( l_scheduled_end_date, 'MI' ) );
4137         END IF;
4138       END IF;
4139 
4140       IF ( l_actual_start_date IS NOT NULL ) THEN
4141         l_parent_workorder_rec.actual_start_date := l_actual_start_date;
4142         l_parent_workorder_rec.actual_start_hr := TO_NUMBER( TO_CHAR( l_actual_start_date, 'HH24' ) );
4143         l_parent_workorder_rec.actual_start_mi := TO_NUMBER( TO_CHAR( l_actual_start_date, 'MI' ) );
4144       END IF;
4145 
4146       IF ( l_actual_end_date IS NOT NULL ) THEN
4147         l_parent_workorder_rec.actual_end_date := l_actual_end_date;
4148         l_parent_workorder_rec.actual_end_hr := TO_NUMBER( TO_CHAR( l_actual_end_date, 'HH24' ) );
4149         l_parent_workorder_rec.actual_end_mi := TO_NUMBER( TO_CHAR( l_actual_end_date, 'MI' ) );
4150       END IF;
4151 
4152       IF ( l_status_code = l_status_code_multi ) THEN
4153         IF ( ( p_x_prd_workorder_rec.status_code = G_JOB_STATUS_RELEASED OR
4154                p_x_prd_workorder_rec.status_code = G_JOB_STATUS_ON_HOLD OR
4155                p_x_prd_workorder_rec.status_code = G_JOB_STATUS_CANCELLED OR
4156                p_x_prd_workorder_rec.status_code = G_JOB_STATUS_PARTS_HOLD OR
4157                p_x_prd_workorder_rec.status_code = G_JOB_STATUS_DEFERRAL_PENDING OR
4158                p_x_prd_workorder_rec.status_code = G_JOB_STATUS_DELETED ) AND
4159              ( parent_csr.status_code = G_JOB_STATUS_UNRELEASED OR
4160                parent_csr.status_code = G_JOB_STATUS_DRAFT ) ) THEN
4161           l_parent_workorder_rec.status_code := G_JOB_STATUS_RELEASED;
4162         END IF;
4163       ELSE
4164         IF ( parent_csr.visit_task_id IS NULL AND
4165              parent_csr.status_code = G_JOB_STATUS_DRAFT AND
4166              p_x_prd_workorder_rec.status_code = G_JOB_STATUS_DELETED ) THEN
4167               l_parent_workorder_rec.status_code := G_JOB_STATUS_RELEASED;
4168 	      -- auto close should not be allowed
4169 	      -- rroy
4170 	      -- bug 4626717 and Shailaja's Mail dated Mon, 26 Sep 2005
4171 	      -- Subj: [Fwd: proposed change to visit closure process]
4172 		/*
4173 		 * Balaji commented out following portion of code for bug # 5138200
4174 		 * Since master workorder cannot be updated to cancelled status
4175 		 * when child workorder are not already cancelled. The recursive logic
4176 		 * in this API updates parent workorders first and then child workorders.
4177 		 * Cancelling parent workorders will be taken care by Cancel_Visit_Jobs API.
4178 		 *
4179 	ELSIF l_status_code <> G_JOB_STATUS_CLOSED THEN
4180           l_parent_workorder_rec.status_code := l_status_code;
4181         */
4182         END IF;
4183       END IF;
4184        -- rroy
4185        -- validate the status change so that
4186        -- the sequence of status changes is valid
4187        -- and eam does not throw errors
4188        -- the validations are similar to those in
4189        -- eam_wo_validate_pvt
4190        IF(l_parent_workorder_Rec.status_code = G_JOB_STATUS_DRAFT AND parent_csr.status_code <> G_JOB_STATUS_DRAFT) THEN
4191 		l_parent_workorder_rec.status_code := parent_csr.status_code;
4192        ELSIF(parent_csr.status_code = G_JOB_STATUS_COMPLETE_NC AND
4193             (l_parent_workorder_rec.status_code	 NOT IN (G_JOB_STATUS_COMPLETE_NC, G_JOB_STATUS_CLOSED,
4194                                                          G_JOB_STATUS_COMPLETE))) THEN
4195    		l_parent_workorder_rec.status_code := parent_csr.status_code;
4196       ELSIF (l_parent_workorder_rec.status_code = G_JOB_STATUS_COMPLETE_NC AND
4197             (parent_csr.status_code NOT IN (G_JOB_STATUS_COMPLETE_NC, G_JOB_STATUS_CLOSED, G_JOB_STATUS_COMPLETE))) THEN
4198 		l_parent_workorder_rec.status_code := parent_csr.status_code;
4199       END IF;
4200 
4201       IF ( G_DEBUG = 'Y' ) THEN
4202         AHL_DEBUG_PUB.debug( l_api_name || ' - Before update_job for parent workorder' );
4203         AHL_DEBUG_PUB.debug( l_api_name || ' - parent workorder-status_code: '|| l_parent_workorder_rec.status_code );
4204         AHL_DEBUG_PUB.debug( l_api_name || ' - parent workorder-scheduled_start_date: '|| TO_CHAR(l_parent_workorder_rec.scheduled_start_date , 'DD-MON-YYYY HH24:MI' ) );
4205         AHL_DEBUG_PUB.debug( l_api_name || ' - parent workorder-scheduled_end_date: '|| TO_CHAR(l_parent_workorder_rec.scheduled_end_date , 'DD-MON-YYYY HH24:MI' ) );
4206         AHL_DEBUG_PUB.debug( l_api_name || ' - parent workorder-actual_start_date: '|| TO_CHAR(l_parent_workorder_rec.actual_start_date , 'DD-MON-YYYY HH24:MI' ) );
4207         AHL_DEBUG_PUB.debug( l_api_name || ' - parent workorder-actual_end_date: '|| TO_CHAR(l_parent_workorder_rec.actual_end_date , 'DD-MON-YYYY HH24:MI' ) );
4208       END IF;
4209 
4210        -- Bug # 7643668 (FP for Bug # 6493302) -- start
4211        -- Unless any of scheduled date, actual date or status changes for a
4212        -- parent work order dont call update_job for Master workorder.
4213        IF
4214 	(
4215 	 parent_csr.status_code <> l_parent_workorder_rec.status_code OR
4216 	 parent_csr.scheduled_start_date <> l_parent_workorder_rec.scheduled_start_date OR
4217 	 parent_csr.scheduled_end_date <> l_parent_workorder_rec.scheduled_end_date OR
4218 	 TO_NUMBER( TO_CHAR( parent_csr.scheduled_start_date, 'HH24' ) ) <> l_parent_workorder_rec.scheduled_start_hr OR
4219 	 TO_NUMBER( TO_CHAR( parent_csr.scheduled_start_date, 'MI' ) ) <> l_parent_workorder_rec.scheduled_start_mi OR
4220 	 TO_NUMBER( TO_CHAR( parent_csr.scheduled_end_date, 'HH24' ) ) <> l_parent_workorder_rec.scheduled_end_hr OR
4221 	 TO_NUMBER( TO_CHAR( parent_csr.scheduled_end_date, 'MI' ) ) <> l_parent_workorder_rec.scheduled_end_mi OR
4222 	 (
4223 	  (
4224 	   parent_csr.actual_start_date IS NULL AND
4225 	   l_parent_workorder_rec.actual_start_date IS NOT NULL
4226 	  ) OR
4227 	  parent_csr.actual_start_date <> l_parent_workorder_rec.actual_start_date
4228 	 ) OR
4229 	 (
4230 	  (
4231 	   parent_csr.actual_end_date IS NULL AND
4232 	   l_parent_workorder_rec.actual_end_date IS NOT NULL
4233 	  ) OR
4234 	  parent_csr.actual_end_date <> l_parent_workorder_rec.actual_end_date
4235 	 ) OR
4236 	 (
4237 	  parent_csr.actual_start_date IS NOT NULL AND
4238 	  TO_NUMBER( TO_CHAR( parent_csr.actual_start_date, 'HH24' ) ) <> l_parent_workorder_rec.actual_start_hr
4239 	 ) OR
4240 	 (
4241 	  parent_csr.actual_start_date IS NOT NULL AND
4242 	  TO_NUMBER( TO_CHAR( parent_csr.actual_start_date, 'MI' ) ) <> l_parent_workorder_rec.actual_start_mi
4243 	 ) OR
4244 	 (
4245 	  parent_csr.actual_end_date IS NOT NULL AND
4246 	  TO_NUMBER( TO_CHAR( parent_csr.actual_end_date, 'HH24' ) ) <> l_parent_workorder_rec.actual_end_hr
4247 	 ) OR
4248 	 (
4249 	  parent_csr.actual_end_date IS NOT NULL AND
4250 	  TO_NUMBER( TO_CHAR( parent_csr.actual_end_date, 'MI' ) ) <> l_parent_workorder_rec.actual_end_mi
4251 	 )
4252 
4253        )
4254        THEN
4255 	      update_job
4256 	      (
4257 		p_api_version            => 1.0                        ,
4258 		p_init_msg_list          => FND_API.G_TRUE             ,
4259 		p_commit                 => FND_API.G_FALSE            ,
4260 		p_validation_level       => FND_API.G_VALID_LEVEL_FULL ,
4261 		p_default                => FND_API.G_TRUE             ,
4262 		p_module_type            => NULL                       ,
4263 		x_return_status          => l_return_status            ,
4264 		x_msg_count              => l_msg_count                ,
4265 		x_msg_data               => l_msg_data                 ,
4266 		p_wip_load_flag          => p_wip_load_flag            ,
4267 		p_x_prd_workorder_rec    => l_parent_workorder_rec     ,
4268 		p_x_prd_workoper_tbl     => l_parent_workoper_tbl
4269 	      );
4270 
4271 	      IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4272 		RAISE FND_API.G_EXC_ERROR;
4273 	      END IF;
4274 
4275 	      IF ( G_DEBUG = 'Y' ) THEN
4276 		AHL_DEBUG_PUB.debug( l_api_name || ' - update_job for parent_workorder successful' );
4277 	      END IF;
4278 
4279         END IF;
4280         -- Bug # 7643668 (FP for Bug # 6493302) -- end
4281     END LOOP;
4282 
4283     IF ( G_DEBUG = 'Y' ) THEN
4284       AHL_DEBUG_PUB.debug( l_api_name || ' - Before AHL_EAM_JOB_PVT.update_job_operations' );
4285     END IF;
4286 
4287     AHL_EAM_JOB_PVT.update_job_operations
4288     (
4289       p_api_version            => 1.0                        ,
4290       p_init_msg_list          => FND_API.G_TRUE             ,
4291       p_commit                 => FND_API.G_FALSE            ,
4292       p_validation_level       => FND_API.G_VALID_LEVEL_FULL ,
4293       p_default                => FND_API.G_TRUE             ,
4294       p_module_type            => NULL                       ,
4295       x_return_status          => l_return_status            ,
4296       x_msg_count              => l_msg_count                ,
4297       x_msg_data               => l_msg_data                 ,
4298       p_workorder_rec          => p_x_prd_workorder_rec      ,
4299       p_operation_tbl          => l_prd_workoper_tbl         ,
4300       p_material_req_tbl       => l_material_tbl             ,
4301       p_resource_req_tbl       => l_resource_tbl
4302     );
4303 
4304     IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4305       RAISE FND_API.G_EXC_ERROR;
4306     END IF;
4307 
4308     IF ( G_DEBUG = 'Y' ) THEN
4309       AHL_DEBUG_PUB.debug( l_api_name || ' - AHL_EAM_JOB_PVT.update_job_operations succesful' );
4310     END IF;
4311 
4312   END IF; -- For WIP Load Flag
4313 
4314   -- R12: Serial Reservation enhancements.
4315   -- Delete existing reservations if cancelling or completing a workorder.
4316   IF (p_x_prd_workorder_rec.status_code IN (G_JOB_STATUS_COMPLETE_NC, G_JOB_STATUS_COMPLETE,
4317                                             G_JOB_STATUS_CANCELLED, G_JOB_STATUS_DELETED)) THEN
4318       IF ( G_DEBUG = 'Y' ) THEN
4319         AHL_DEBUG_PUB.debug( l_api_name || ' Before - AHL_RSV_RESERVATIONS_PVT.Delete_Reservation');
4320         AHL_DEBUG_PUB.debug( 'Workorder Status:' || p_x_prd_workorder_rec.status_code);
4321       END IF;
4322 
4323       FOR get_scheduled_mater_rec IN get_scheduled_mater_csr(p_x_prd_workorder_rec.workorder_id)
4324       LOOP
4325          -- Call delete reservation API.
4326          AHL_RSV_RESERVATIONS_PVT.Delete_Reservation (
4327                                    p_api_version => 1.0,
4328                                    p_init_msg_list          => FND_API.G_TRUE             ,
4329                                    p_commit                 => FND_API.G_FALSE            ,
4330                                    p_validation_level       => FND_API.G_VALID_LEVEL_FULL ,
4331                                    p_module_type            => NULL,
4332                                    x_return_status          => l_return_status            ,
4333                                    x_msg_count              => l_msg_count                ,
4334                                    x_msg_data               => l_msg_data                 ,
4335                                    p_scheduled_material_id  => get_scheduled_mater_rec.scheduled_material_id);
4336 
4337          -- Check return status.
4338          IF (l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4339             IF ( G_DEBUG = 'Y' ) THEN
4340                  AHL_DEBUG_PUB.debug('Delete_Reservation failed for schedule material ID: '
4341                          || get_scheduled_mater_rec.scheduled_material_id);
4342             END IF; -- G_DEBUG.
4343 
4344             EXIT;
4345          END IF; -- l_return_status
4346 
4347       END LOOP;
4348 
4349       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4350           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4351       ELSIF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4352           RAISE FND_API.G_EXC_ERROR;
4353       END IF;
4354 
4355   END IF; -- p_x_prd_workorder_rec.status_code
4356 
4357   -- Balaji added following code for bug # 6680137
4358   -- Material requirements will be updated to status History for work orders
4359   -- in status cancelled or closed. These requirements will not be moved to
4360   -- DP history and will not be available in active demand.
4361   IF  (
4362         p_x_prd_workorder_rec.status_code IN (
4363                                               G_JOB_STATUS_CANCELLED,
4364                                               G_JOB_STATUS_CLOSED,
4365                                               G_JOB_STATUS_COMPLETE_NC
4366                                              )
4367         AND p_x_prd_workorder_rec.master_workorder_flag = 'N'
4368       )
4369   THEN
4370 
4371 	IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
4372 		fnd_log.string
4373 		(
4374 			fnd_log.level_statement,
4375 			l_debug_module,
4376 			'Cancelling Material Requirement for WO # ->'||p_x_prd_workorder_rec.JOB_NUMBER
4377 		);
4378 		fnd_log.string
4379 		(
4380 			fnd_log.level_statement,
4381 			l_debug_module,
4382 			'Work Order status ->'||p_x_prd_workorder_rec.status_code
4383 		);
4384 	END IF;
4385 
4386 	-- Retrieve visit task id if its not already present.
4387 	IF (
4388 	   p_x_prd_workorder_rec.VISIT_TASK_ID IS NULL
4389 	   )
4390 	THEN
4391 	        OPEN c_wo_vtsk_id(p_x_prd_workorder_rec.WORKORDER_ID);
4392 	        FETCH c_wo_vtsk_id INTO p_x_prd_workorder_rec.VISIT_TASK_ID;
4393 	        CLOSE c_wo_vtsk_id;
4394 	END IF;
4395 
4396 	IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
4397 		fnd_log.string
4398 		(
4399 			fnd_log.level_statement,
4400 			l_debug_module,
4401 			'Visit Task Id # ->'||p_x_prd_workorder_rec.VISIT_TASK_ID
4402 		);
4403 		fnd_log.string
4404 		(
4405 			fnd_log.level_statement,
4406 			l_debug_module,
4407 			'Before calling AHL_LTP_REQST_MATRL_PVT.Update_Material_Reqrs_status'
4408 		);
4409 	END IF;
4410 
4411 	-- Call LTP API to update material requirement status to History.
4412 	AHL_LTP_REQST_MATRL_PVT.Update_Material_Reqrs_status(
4413 	           p_api_version      => 1.0,
4414 	           p_init_msg_list    => FND_API.G_TRUE,
4415 	           p_commit           => FND_API.G_FALSE,
4416 	           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
4417 	           p_module_type      => NULL,
4418 	           p_visit_task_id    => p_x_prd_workorder_rec.VISIT_TASK_ID,
4419 	           x_return_status    => x_return_status,
4420 	           x_msg_count        => x_msg_count,
4421 	           x_msg_data         => x_msg_data
4422 	 );
4423 
4424 	IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
4425 		fnd_log.string
4426 		(
4427 			fnd_log.level_statement,
4428 			l_debug_module,
4429 			'return status after call to Update_Material_Reqrs_status -> '|| x_return_status
4430 		);
4431 	END IF;
4432 
4433 	IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4434 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4435 	ELSIF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4436 	  RAISE FND_API.G_EXC_ERROR;
4437 	END IF;
4438 
4439   END IF;
4440 
4441   IF FND_API.to_boolean(p_commit) THEN
4442     COMMIT;
4443   END IF;
4444 
4445   -- User Hooks
4446   IF (JTF_USR_HKS.Ok_to_execute('AHL_PRD_WORKORDER_PVT', 'UPDATE_JOB', 'A', 'C' )) THEN
4447 
4448       ahl_prd_workorder_CUHK.update_job_post(
4449         p_prd_workorder_rec => p_x_prd_workorder_rec,
4450         p_prd_workoper_tbl  =>  p_x_prd_workoper_tbl,
4451         x_msg_count => l_msg_count,
4452         x_msg_data => l_msg_data,
4453         x_return_status => l_return_status);
4454 
4455       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4456         RAISE FND_API.G_EXC_ERROR;
4457       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4458         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4459       END IF;
4460   END IF;
4461 
4462   IF ( G_DEBUG = 'Y' ) THEN
4463     AHL_DEBUG_PUB.debug( l_api_name || ' - Success' );
4464   END IF;
4465 
4466   IF G_DEBUG='Y' THEN
4467     AHL_DEBUG_PUB.disable_debug;
4468   END IF;
4469 
4470 EXCEPTION
4471  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4472     ROLLBACK TO update_job_PVT;
4473     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4474     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
4475                                p_count => x_msg_count,
4476                                p_data  => x_msg_data);
4477  WHEN FND_API.G_EXC_ERROR THEN
4478     ROLLBACK TO update_job_PVT;
4479     x_return_status := FND_API.G_RET_STS_ERROR;
4480     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
4481                                p_count => x_msg_count,
4482                                p_data  => x_msg_data);
4483 
4484  WHEN OTHERS THEN
4485     ROLLBACK TO update_job_PVT;
4486     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4487     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4488       FND_MSG_PUB.add_exc_msg(p_pkg_name        =>g_pkg_name,
4489                               p_procedure_name  =>l_api_name,
4490                               p_error_text      =>SUBSTR(SQLERRM,1,240));
4491     END IF;
4492 
4493     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
4494                                p_count => x_msg_count,
4495                                p_data  => x_msg_data);
4496 END update_job;
4497 
4498 PROCEDURE process_jobs
4499 (
4500  p_api_version           IN            NUMBER     := 1.0,
4501  p_init_msg_list         IN            VARCHAR2   := FND_API.G_TRUE,
4502  p_commit                IN            VARCHAR2   := FND_API.G_FALSE,
4503  p_validation_level      IN            NUMBER     := FND_API.G_VALID_LEVEL_FULL,
4504  p_default               IN            VARCHAR2   := FND_API.G_FALSE,
4505  p_module_type           IN            VARCHAR2,
4506  x_return_status         OUT NOCOPY    VARCHAR2,
4507  x_msg_count             OUT NOCOPY    NUMBER,
4508  x_msg_data              OUT NOCOPY    VARCHAR2,
4509  p_x_prd_workorder_tbl   IN OUT NOCOPY PRD_WORKORDER_TBL,
4510  p_prd_workorder_rel_tbl IN            PRD_WORKORDER_REL_TBL
4511 )
4512 
4513 AS
4514   l_api_name     CONSTANT VARCHAR2(30) := 'PROCESS_JOBS';
4515   l_api_version  CONSTANT NUMBER       := 1.0;
4516   l_msg_count             NUMBER;
4517   l_msg_data              VARCHAR2(2000);
4518   l_return_status         VARCHAR2(1);
4519   l_dummy_op_tbl          prd_workoper_tbl;
4520   l_operation_tbl         AHL_PRD_OPERATIONS_PVT.prd_operation_tbl;
4521   l_resource_tbl          AHL_PP_RESRC_REQUIRE_PVT.resrc_require_tbl_type;
4522   l_material_tbl          AHL_PP_MATERIALS_PVT.req_material_tbl_type;
4523   l_eam_wo_tbl            EAM_PROCESS_WO_PUB.eam_wo_tbl_type;
4524   l_eam_wo_relations_tbl  EAM_PROCESS_WO_PUB.eam_wo_relations_tbl_type;
4525   l_eam_op_tbl            EAM_PROCESS_WO_PUB.eam_op_tbl_type;
4526   l_eam_res_req_tbl       EAM_PROCESS_WO_PUB.eam_res_tbl_type;
4527   l_eam_mat_req_tbl       EAM_PROCESS_WO_PUB.eam_mat_req_tbl_type;
4528 
4529   l_wo_rel_rec_found      BOOLEAN      := FALSE;
4530   total_operations        NUMBER       := 0;
4531   total_resources         NUMBER       := 0;
4532   total_materials         NUMBER       := 0;
4533 
4534 BEGIN
4535   SAVEPOINT process_jobs_PVT;
4536 
4537   IF NOT FND_API.compatible_api_call(l_api_version,
4538                                      p_api_version,
4539                                      l_api_name,G_PKG_NAME) THEN
4540     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4541   END IF;
4542 
4543   IF FND_API.to_boolean(p_init_msg_list) THEN
4544     FND_MSG_PUB.initialize;
4545   END IF;
4546 
4547   x_return_status:=FND_API.G_RET_STS_SUCCESS;
4548 
4549   IF G_DEBUG='Y' THEN
4550       AHL_DEBUG_PUB.enable_debug;
4551   END IF;
4552 
4553   FOR i IN p_x_prd_workorder_tbl.FIRST..p_x_prd_workorder_tbl.LAST LOOP
4554 
4555     IF ( p_x_prd_workorder_tbl(i).batch_id IS NULL OR
4556          p_x_prd_workorder_tbl(i).batch_id = FND_API.G_MISS_NUM ) THEN
4557       FND_MESSAGE.set_name('AHL','AHL_PRD_BATCH_ID_NULL');
4558       FND_MSG_PUB.add;
4559       RAISE FND_API.G_EXC_ERROR;
4560     END IF;
4561 
4562     IF ( p_x_prd_workorder_tbl(i).header_id IS NULL OR
4563          p_x_prd_workorder_tbl(i).header_id = FND_API.G_MISS_NUM ) THEN
4564       FND_MESSAGE.set_name('AHL','AHL_PRD_HEADER_ID_NULL');
4565       FND_MSG_PUB.add;
4566       RAISE FND_API.G_EXC_ERROR;
4567     END IF;
4568 
4569     IF ( G_DEBUG = 'Y' ) THEN
4570       AHL_DEBUG_PUB.debug( 'Processing Batch : ' || p_x_prd_workorder_tbl(i).batch_id  || ' Header : ' || p_x_prd_workorder_tbl(i).header_id );
4571     END IF;
4572     IF ( p_x_prd_workorder_tbl(i).dml_operation = 'C' ) THEN
4573 
4574       -- Check if atleast one relationship record exists
4575 						IF p_prd_workorder_rel_tbl.COUNT > 0 THEN
4576       FOR j IN p_prd_workorder_rel_tbl.FIRST..p_prd_workorder_rel_tbl.LAST LOOP
4577         IF ( p_x_prd_workorder_tbl(i).header_id = p_prd_workorder_rel_tbl(j).parent_header_id OR
4578              p_x_prd_workorder_tbl(i).header_id = p_prd_workorder_rel_tbl(j).child_header_id ) THEN
4579           l_wo_rel_rec_found := TRUE;
4580           EXIT;
4581         END IF;
4582       END LOOP;
4583 						END IF;
4584 
4585 -- rroy - post 11.5.10
4586 -- visits without any tasks and therefore without any relations
4587 -- should be pushed to prod without errors
4588       /*IF ( l_wo_rel_rec_found = FALSE ) THEN
4589         FND_MESSAGE.set_name('AHL','AHL_PRD_WO_REL_NOT_FOUND');
4590         FND_MESSAGE.set_token('RECORD', p_x_prd_workorder_tbl(i).header_id);
4591         FND_MSG_PUB.add;
4592         RAISE FND_API.G_EXC_ERROR;
4593       ELSE
4594         l_wo_rel_rec_found := FALSE;
4595       END IF;
4596 */
4597       IF ( G_DEBUG = 'Y' ) THEN
4598         AHL_DEBUG_PUB.debug( 'Invoking create_job API for Workorder ' || i );
4599       END IF;
4600 
4601       create_job
4602       (
4603         p_api_version          => 1.0,
4604         p_init_msg_list        => FND_API.G_TRUE,
4605         p_commit               => FND_API.G_FALSE,
4606         p_validation_level     => FND_API.G_VALID_LEVEL_FULL,
4607         p_default              => FND_API.G_FALSE,
4608         p_module_type          => NULL,
4609         x_return_status        => l_return_status,
4610         x_msg_count            => l_msg_count,
4611         x_msg_data             => l_msg_data,
4612         p_wip_load_flag        => 'N',
4613         p_x_prd_workorder_rec  => p_x_prd_workorder_tbl(i),
4614         x_operation_tbl        => l_operation_tbl,
4615         x_resource_tbl         => l_resource_tbl,
4616         x_material_tbl         => l_material_tbl
4617       );
4618 
4619       IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4620         RAISE FND_API.G_EXC_ERROR;
4621       END IF;
4622 
4623       IF ( G_DEBUG = 'Y' ) THEN
4624         AHL_DEBUG_PUB.debug( 'create_job API Success' );
4625       END IF;
4626 
4627       IF ( G_DEBUG = 'Y' ) THEN
4628         AHL_DEBUG_PUB.debug( 'Mapping Job Header Record Number : ' || i );
4629       END IF;
4630 
4631       -- Map all input AHL Job Header record attributes to the
4632       -- corresponding EAM Job Header record attributes.
4633       AHL_EAM_JOB_PVT.map_ahl_eam_wo_rec
4634       (
4635         p_workorder_rec    => p_x_prd_workorder_tbl(i),
4636         x_eam_wo_rec       => l_eam_wo_tbl(i)
4637       );
4638 
4639       IF ( G_DEBUG = 'Y' ) THEN
4640         AHL_DEBUG_PUB.debug( 'Job Header Record Mapping Complete' );
4641       END IF;
4642 
4643       -- Map all input AHL Operation record attributes to the
4644       -- corresponding EAM Operation record attributes.
4645       IF ( l_operation_tbl.COUNT > 0 ) THEN
4646         FOR j IN l_operation_tbl.FIRST..l_operation_tbl.LAST LOOP
4647 
4648           IF ( G_DEBUG = 'Y' ) THEN
4649             AHL_DEBUG_PUB.debug( 'Mapping Operation Record Number : ' || j );
4650           END IF;
4651 
4652           total_operations := total_operations + 1;
4653 
4654           AHL_EAM_JOB_PVT.map_ahl_eam_op_rec
4655           (
4656             p_operation_rec    => l_operation_tbl(j),
4657             x_eam_op_rec       => l_eam_op_tbl(total_operations)
4658           );
4659 
4660           l_eam_op_tbl(total_operations).batch_id := p_x_prd_workorder_tbl(i).batch_id;
4661           l_eam_op_tbl(total_operations).header_id := p_x_prd_workorder_tbl(i).header_id;
4662 
4663         END LOOP;
4664 
4665         IF ( G_DEBUG = 'Y' ) THEN
4666           AHL_DEBUG_PUB.debug( 'Operations Record Mapping Complete' );
4667         END IF;
4668       END IF;
4669 
4670       -- Map all input AHL Material Requirement record attributes to the
4671       -- corresponding EAM Material Requirement record attributes.
4672       IF ( l_material_tbl.COUNT > 0 ) THEN
4673         FOR j IN l_material_tbl.FIRST..l_material_tbl.LAST LOOP
4674 
4675           IF ( G_DEBUG = 'Y' ) THEN
4676             AHL_DEBUG_PUB.debug( 'Mapping Material Requirement Record Number : ' || j );
4677           END IF;
4678 
4679           total_materials := total_materials + 1;
4680 
4681           AHL_EAM_JOB_PVT.map_ahl_eam_mat_rec
4682           (
4683             p_material_req_rec    => l_material_tbl(j),
4684             x_eam_mat_req_rec     => l_eam_mat_req_tbl(total_materials)
4685           );
4686 
4687           l_eam_mat_req_tbl(total_materials).batch_id := p_x_prd_workorder_tbl(i).batch_id;
4688           l_eam_mat_req_tbl(total_materials).header_id := p_x_prd_workorder_tbl(i).header_id;
4689 
4690         END LOOP;
4691 
4692         IF ( G_DEBUG = 'Y' ) THEN
4693           AHL_DEBUG_PUB.debug( 'Material Requirements Record Mapping Complete' );
4694         END IF;
4695 
4696       END IF;
4697 
4698       -- Map all input AHL Resource Requirement record attributes to the
4699       -- corresponding EAM Resource Requirement record attributes.
4700       IF ( l_resource_tbl.COUNT > 0 ) THEN
4701         FOR j IN l_resource_tbl.FIRST..l_resource_tbl.LAST LOOP
4702 
4703           IF ( G_DEBUG = 'Y' ) THEN
4704             AHL_DEBUG_PUB.debug( 'Mapping Resource Requirement Record Number : ' || j );
4705           END IF;
4706 
4707           total_resources := total_resources + 1;
4708 
4709           AHL_EAM_JOB_PVT.map_ahl_eam_res_rec
4710           (
4711             p_resource_req_rec    => l_resource_tbl(j),
4712             x_eam_res_rec         => l_eam_res_req_tbl(total_resources)
4713           );
4714 
4715           l_eam_res_req_tbl(total_resources).batch_id := p_x_prd_workorder_tbl(i).batch_id;
4716           l_eam_res_req_tbl(total_resources).header_id := p_x_prd_workorder_tbl(i).header_id;
4717 
4718         END LOOP;
4719 
4720         IF ( G_DEBUG = 'Y' ) THEN
4721           AHL_DEBUG_PUB.debug( 'Resource Requirement Record Mapping Complete' );
4722         END IF;
4723 
4724       END IF;
4725 
4726       IF ( l_operation_tbl.COUNT > 0 ) THEN
4727         l_operation_tbl.DELETE;
4728       END IF;
4729 
4730       IF ( l_resource_tbl.COUNT > 0 ) THEN
4731         l_resource_tbl.DELETE;
4732       END IF;
4733 
4734       IF ( l_material_tbl.COUNT > 0 ) THEN
4735         l_material_tbl.DELETE;
4736       END IF;
4737 
4738     ELSIF ( p_x_prd_workorder_tbl(i).dml_operation = 'U' ) THEN
4739 
4740       IF ( G_DEBUG = 'Y' ) THEN
4741         AHL_DEBUG_PUB.debug( 'Invoking update_job API for Workorder ' || i );
4742       END IF;
4743 
4744       update_job
4745       (
4746         p_api_version          => 1.0,
4747         p_init_msg_list        => FND_API.G_TRUE,
4748         p_commit               => FND_API.G_FALSE,
4749         p_validation_level     => FND_API.G_VALID_LEVEL_FULL,
4750         p_default              => FND_API.G_FALSE,
4751         p_module_type          => NULL,
4752         x_return_status        => l_return_status,
4753         x_msg_count            => l_msg_count,
4754         x_msg_data             => l_msg_data,
4755         p_wip_load_flag        => 'N',
4756         p_x_prd_workorder_rec  => p_x_prd_workorder_tbl(i),
4757         p_x_prd_workoper_tbl   => l_dummy_op_tbl
4758       );
4759 
4760       IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4761         RAISE FND_API.G_EXC_ERROR;
4762       END IF;
4763 
4764       IF ( G_DEBUG = 'Y' ) THEN
4765         AHL_DEBUG_PUB.debug( 'update_job API Success' );
4766       END IF;
4767 
4768       IF ( G_DEBUG = 'Y' ) THEN
4769         AHL_DEBUG_PUB.debug( 'Mapping Job Header Record Number : ' || i );
4770       END IF;
4771 
4772       -- Map all input AHL Job Header record attributes to the
4773       -- corresponding EAM Job Header record attributes.
4774       AHL_EAM_JOB_PVT.map_ahl_eam_wo_rec
4775       (
4776         p_workorder_rec    => p_x_prd_workorder_tbl(i),
4777         x_eam_wo_rec       => l_eam_wo_tbl(i)
4778       );
4779 
4780       IF ( G_DEBUG = 'Y' ) THEN
4781         AHL_DEBUG_PUB.debug( 'Job Header Record Mapping Complete' );
4782       END IF;
4783 
4784     END IF;
4785 
4786   END LOOP;
4787 
4788   IF ( p_prd_workorder_rel_tbl.COUNT > 0 ) THEN
4789     FOR i IN p_prd_workorder_rel_tbl.FIRST..p_prd_workorder_rel_tbl.LAST LOOP
4790 
4791       IF ( G_DEBUG = 'Y' ) THEN
4792         AHL_DEBUG_PUB.debug( 'Mapping WO Relationship Record Number : ' || i );
4793       END IF;
4794 
4795       -- Map all input AHL Workorder Relationship attributes to the
4796       -- corresponding EAM Workorder Relationship attributes.
4797       AHL_EAM_JOB_PVT.map_ahl_eam_wo_rel_rec
4798       (
4799        p_workorder_rel_rec    => p_prd_workorder_rel_tbl(i),
4800        x_eam_wo_relations_rec => l_eam_wo_relations_tbl(i)
4801       );
4802 
4803       IF ( G_DEBUG = 'Y' ) THEN
4804         AHL_DEBUG_PUB.debug( 'WO Relationship Record Mapping Complete' );
4805       END IF;
4806     END LOOP;
4807   END IF;
4808 
4809   IF ( G_DEBUG = 'Y' ) THEN
4810     AHL_DEBUG_PUB.debug( 'Invoking AHL_EAM_JOB_PVT.process_eam_workorders API');
4811     AHL_DEBUG_PUB.debug( 'Total Workorders : ' || l_eam_wo_tbl.COUNT);
4812     AHL_DEBUG_PUB.debug( 'Total Operations : ' || l_eam_op_tbl.COUNT);
4813     AHL_DEBUG_PUB.debug( 'Total Resources : ' || l_eam_res_req_tbl.COUNT);
4814     AHL_DEBUG_PUB.debug( 'Total Materials : ' || l_eam_mat_req_tbl.COUNT);
4815     AHL_DEBUG_PUB.debug( 'Total Relationships : ' || l_eam_wo_relations_tbl.COUNT);
4816   END IF;
4817 
4818 
4819   AHL_EAM_JOB_PVT.process_eam_workorders
4820   (
4821      p_api_version            => 1.0,
4822      p_init_msg_list          => FND_API.G_TRUE,
4823      p_commit                 => FND_API.G_FALSE,
4824      p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
4825      p_default                => FND_API.G_FALSE,
4826      p_module_type            => NULL,
4827      x_return_status          => l_return_status,
4828      x_msg_count              => l_msg_count,
4829      x_msg_data               => l_msg_data,
4830      p_x_eam_wo_tbl           => l_eam_wo_tbl,
4831      p_eam_wo_relations_tbl   => l_eam_wo_relations_tbl,
4832      p_eam_op_tbl             => l_eam_op_tbl,
4833      p_eam_res_req_tbl        => l_eam_res_req_tbl,
4834      p_eam_mat_req_tbl        => l_eam_mat_req_tbl
4835   );
4836  IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4837     RAISE FND_API.G_EXC_ERROR;
4838   ELSE
4839     IF ( G_DEBUG = 'Y' ) THEN
4840       AHL_DEBUG_PUB.debug( 'AHL_EAM_JOB_PVT.process_eam_workorders API Success' );
4841     END IF;
4842 
4843     FOR i IN p_x_prd_workorder_tbl.FIRST..p_x_prd_workorder_tbl.LAST LOOP
4844 
4845       IF ( p_x_prd_workorder_tbl(i).dml_operation = 'C' ) THEN
4846         IF ( G_DEBUG = 'Y' ) THEN
4847           AHL_DEBUG_PUB.debug( l_api_name || 'Updating AHL_WORKORDERS with wip_entity_id for Workorder ' || i );
4848         END IF;
4849 
4850         UPDATE AHL_WORKORDERS
4851         SET    wip_entity_id = l_eam_wo_tbl(i).wip_entity_id,
4852 	       object_version_number = p_x_prd_workorder_tbl(i).object_version_number + 1,
4853                last_update_date      = SYSDATE,
4854                last_updated_by       = Fnd_Global.USER_ID,
4855                last_update_login     = Fnd_Global.LOGIN_ID
4856 
4857         WHERE  workorder_id = p_x_prd_workorder_tbl(i).workorder_id;
4858 
4859       IF ( G_DEBUG = 'Y' ) THEN
4860           AHL_DEBUG_PUB.debug( l_api_name || 'Before calling Create Job Dispostions ' || i );
4861           AHL_DEBUG_PUB.debug( 'Workorder Id: ' || p_x_prd_workorder_tbl(i).workorder_id );
4862 
4863         END IF;
4864 
4865   --Call disposition API Post 11.5.10 Changes
4866   AHL_PRD_DISPOSITION_PVT.create_job_dispositions(
4867             p_api_version          => 1.0,
4868             p_init_msg_list        => FND_API.G_TRUE,
4869             p_commit               => FND_API.G_FALSE,
4870             p_validation_level     => FND_API.G_VALID_LEVEL_FULL,
4871             x_return_status        => l_return_status,
4872             x_msg_count            => l_msg_count,
4873             x_msg_data             => l_msg_data,
4874             p_workorder_id         => p_x_prd_workorder_tbl(i).workorder_id);
4875 
4876       IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
4877         RAISE FND_API.G_EXC_ERROR;
4878      END IF;
4879 
4880 	 ELSIF ( p_x_prd_workorder_tbl(i).dml_operation = 'U' ) THEN
4881         IF ( G_DEBUG = 'Y' ) THEN
4882           AHL_DEBUG_PUB.debug( l_api_name || 'Updating AHL_WORKORDERS with status code for Workorder ' || i );
4883         END IF;
4884         --
4885         UPDATE AHL_WORKORDERS
4886         SET    status_code = l_eam_wo_tbl(i).status_type,
4887 	       object_version_number = p_x_prd_workorder_tbl(i).object_version_number + 1,
4888                last_update_date      = SYSDATE,
4889                last_updated_by       = Fnd_Global.USER_ID,
4890                last_update_login     = Fnd_Global.LOGIN_ID
4891 
4892         WHERE  workorder_id = p_x_prd_workorder_tbl(i).workorder_id;
4893 
4894    END IF;
4895 
4896     END LOOP;
4897 
4898     IF FND_API.to_boolean(p_commit) THEN
4899       COMMIT;
4900     END IF;
4901 
4902   END IF;
4903 
4904   IF G_DEBUG='Y' THEN
4905     AHL_DEBUG_PUB.disable_debug;
4906   END IF;
4907 
4908 EXCEPTION
4909  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4910     ROLLBACK TO process_jobs_PVT;
4911     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4912     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
4913                                p_count => x_msg_count,
4914                                p_data  => x_msg_data);
4915  WHEN FND_API.G_EXC_ERROR THEN
4916     ROLLBACK TO process_jobs_PVT;
4917     x_return_status := FND_API.G_RET_STS_ERROR;
4918     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
4919                                p_count => x_msg_count,
4920                                p_data  => x_msg_data);
4921 
4922  WHEN OTHERS THEN
4923     ROLLBACK TO process_jobs_PVT;
4924     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4925     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4926       FND_MSG_PUB.add_exc_msg(p_pkg_name        =>g_pkg_name,
4927                               p_procedure_name  =>l_api_name,
4928                               p_error_text      => SUBSTR(SQLERRM,1,240));
4929 
4930     END IF;
4931     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
4932                                p_count => x_msg_count,
4933                                p_data  => x_msg_data);
4934 
4935 END process_jobs;
4936 
4937 PROCEDURE release_visit_jobs
4938 (
4939   p_api_version         IN   NUMBER    := 1.0,
4940   p_init_msg_list       IN   VARCHAR2  := FND_API.G_TRUE,
4941   p_commit              IN   VARCHAR2  := FND_API.G_FALSE,
4942   p_validation_level    IN   NUMBER    := FND_API.G_VALID_LEVEL_FULL,
4943   p_default             IN   VARCHAR2  := FND_API.G_FALSE,
4944   p_module_type         IN   VARCHAR2  := NULL,
4945   x_return_status       OUT NOCOPY VARCHAR2,
4946   x_msg_count           OUT NOCOPY NUMBER,
4947   x_msg_data            OUT NOCOPY VARCHAR2,
4948   p_visit_id            IN   NUMBER,
4949   p_unit_effectivity_id IN   NUMBER,
4950   p_workorder_id        IN   NUMBER
4951 )
4952 AS
4953   l_api_name     CONSTANT VARCHAR2(30) := 'release_visit_jobs';
4954   l_api_version  CONSTANT NUMBER       := 1.0;
4955 
4956   l_msg_count             NUMBER;
4957   l_msg_data              VARCHAR2(2000);
4958   l_return_status         VARCHAR2(1);
4959 
4960   l_input_type            VARCHAR2(3);
4961 
4962   l_wo_count              NUMBER := 0;
4963   l_workorder_tbl         PRD_WORKORDER_TBL;
4964   l_workorder_rel_tbl     PRD_WORKORDER_REL_TBL;
4965 
4966   l_workorder_id          NUMBER;
4967   l_object_version_number NUMBER;
4968   l_wip_entity_id         NUMBER;
4969   l_status_code           VARCHAR2(30);
4970 
4971   l_wo_id                 NUMBER;
4972   l_ovn                   NUMBER;
4973   l_sts_code              VARCHAR2(30);
4974   l_wip_id                NUMBER;
4975   l_mwo_flag              VARCHAR2(1);
4976   l_child_wo_name               VARCHAR2(80);
4977   l_parent_wo_name               VARCHAR2(80);
4978 
4979 		-- rroy
4980 		-- ACL Changes
4981 		l_wo_name															VARCHAR2(80);
4982 		l_master_wo_flag								VARCHAR2(1);
4983 		-- rroy
4984 		-- ACL Changes
4985 
4986   -- To get the Visit Master Workorder
4987   CURSOR       get_visit_mwo( c_visit_id NUMBER ) IS
4988     SELECT     workorder_id,
4989                object_version_number,
4990                status_code,
4991                wip_entity_id,
4992 															workorder_name
4993     FROM       AHL_WORKORDERS
4994     WHERE      visit_id = c_visit_id
4995     AND        status_code <> G_JOB_STATUS_DELETED
4996     AND        visit_task_id IS NULL;
4997   -- To get a workorder from the wp_entity_id
4998   -- Fix for connect by issue
4999   CURSOR       get_wip_wo( c_wip_entity_id NUMBER ) IS
5000     SELECT     WO.workorder_id workorder_id,
5001                WO.object_version_number object_version_number,
5002                WO.wip_entity_id wip_entity_id,
5003                WO.status_code status_code,
5004 															WO.master_workorder_flag,
5005 															WO.workorder_name
5006     FROM       AHL_WORKORDERS WO
5007     WHERE wip_entity_id = c_wip_entity_id
5008     AND STATUS_CODE <> G_JOB_STATUS_DELETED;
5009 
5010   -- To get the Child Workorders
5011   -- Top Down for Release workorders
5012   CURSOR       get_child_wos( c_wip_entity_id NUMBER ) IS
5013     SELECT     REL.child_object_id
5014     FROM       WIP_SCHED_RELATIONSHIPS REL
5015     WHERE      REL.parent_object_type_id = 1
5016     AND        REL.child_object_type_id = 1
5017     START WITH REL.parent_object_id = c_wip_entity_id
5018         AND    REL.relationship_type = 1
5019     CONNECT BY REL.parent_object_id = PRIOR REL.child_object_id
5020         AND    REL.relationship_type = 1
5021     ORDER BY   level;
5022 -- Fix for connect by issue
5023 
5024   -- To get the Parent Workorders
5025   -- Top Down for Release workorders
5026   CURSOR       get_parent_wos( c_wip_entity_id NUMBER ) IS
5027     SELECT     REL.parent_object_id
5028     FROM       WIP_SCHED_RELATIONSHIPS REL
5029     WHERE      REL.parent_object_type_id = 1
5030     AND        REL.child_object_type_id = 1
5031     START WITH REL.child_object_id = c_wip_entity_id
5032         AND    REL.relationship_type = 1
5033     CONNECT BY REL.child_object_id = PRIOR REL.parent_object_id
5034         AND    REL.relationship_type = 1
5035     ORDER BY   level DESC;
5036 
5037   -- To get the UE Master Workorder
5038   CURSOR       get_ue_mwo( c_unit_effectivity_id NUMBER ) IS
5039     SELECT     WO.workorder_id workorder_id,
5040                WO.object_version_number object_version_number,
5041                WO.status_code status_code,
5042                WO.wip_entity_id wip_entity_id,
5043 															WO.workorder_name workorder_name,
5044 															WO.master_workorder_flag master_workorder_flag
5045     FROM       AHL_WORKORDERS WO,
5046                AHL_VISIT_TASKS_B VT
5047     WHERE      WO.status_code <> G_JOB_STATUS_DELETED
5048     AND        WO.visit_task_id = VT.visit_task_id
5049     AND        VT.task_type_code IN ( 'SUMMARY', 'UNASSOCIATED' )
5050     AND        VT.unit_effectivity_id = c_unit_effectivity_id;
5051 
5052   -- To get the Workorder
5053   CURSOR       get_wo( c_workorder_id NUMBER ) IS
5054     SELECT     workorder_id,
5055                object_version_number,
5056                status_code,
5057                wip_entity_id,
5058 															workorder_name
5059     FROM       AHL_WORKORDERS
5060     WHERE      workorder_id =p_workorder_id;
5061 
5062 BEGIN
5063   SAVEPOINT release_visit_jobs_PVT;
5064 
5065   IF NOT FND_API.compatible_api_call(l_api_version,
5066                                      p_api_version,
5067                                      l_api_name,G_PKG_NAME) THEN
5068     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5069   END IF;
5070 
5071   IF FND_API.to_boolean(p_init_msg_list) THEN
5072     FND_MSG_PUB.initialize;
5073   END IF;
5074 
5075   x_return_status:=FND_API.G_RET_STS_SUCCESS;
5076 
5077   IF G_DEBUG='Y' THEN
5078       AHL_DEBUG_PUB.enable_debug;
5079   END IF;
5080   -- Validate Inputs
5081   IF ( ( p_workorder_id IS NULL OR
5082          p_workorder_id = FND_API.G_MISS_NUM ) AND
5083        ( p_unit_effectivity_id IS NULL OR
5084          p_unit_effectivity_id = FND_API.G_MISS_NUM ) AND
5085        ( p_visit_id IS NULL OR
5086          p_visit_id =  FND_API.G_MISS_NUM ) ) THEN
5087     FND_MESSAGE.set_name('AHL','AHL_PRD_WRONG_ARGUMENTS');
5088     FND_MESSAGE.set_token('PROC_NAME', l_api_name);
5089     FND_MSG_PUB.add;
5090     RAISE FND_API.G_EXC_ERROR;
5091   END IF;
5092 
5093   -- Determine the type of API call
5094   IF ( p_workorder_id IS NOT NULL AND
5095        p_workorder_id <> FND_API.G_MISS_NUM ) THEN
5096     l_input_type := 'WO';
5097   ELSIF ( p_unit_effectivity_id IS NOT NULL AND
5098           p_unit_effectivity_id <> FND_API.G_MISS_NUM ) THEN
5099     l_input_type := 'UE';
5100   ELSIF ( p_visit_id IS NOT NULL AND
5101           p_visit_id <>  FND_API.G_MISS_NUM ) THEN
5102     l_input_type := 'VST';
5103   END IF;
5104 
5105   -- Process Visit
5106   IF ( l_input_type = 'VST' ) THEN
5107 
5108     -- Get the Visit Master Workorder
5109     OPEN  get_visit_mwo( p_visit_id );
5110     FETCH get_visit_mwo
5111     INTO  l_workorder_id,
5112           l_object_version_number,
5113           l_status_code,
5114           l_wip_entity_id,
5115 										l_wo_name;
5116 
5117     IF ( get_visit_mwo%NOTFOUND ) THEN
5118       FND_MESSAGE.set_name('AHL','AHL_PRD_VISIT_MWO_NOT_FOUND');
5119       FND_MSG_PUB.add;
5120       CLOSE get_visit_mwo;
5121       RAISE FND_API.G_EXC_ERROR;
5122     END IF;
5123 
5124     CLOSE get_visit_mwo;
5125 
5126     -- If the visit needs to be Released, Add the Visit WO in the WO Table
5127     IF ( l_status_code = G_JOB_STATUS_UNRELEASED OR
5128          l_status_code = G_JOB_STATUS_DRAFT ) THEN
5129 						-- rroy
5130 						-- ACL Changes
5131 						l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_workorder_id,
5132 																																																									p_ue_id => NULL,
5133 																																																									p_visit_id => NULL,
5134 																																																									p_item_instance_id => NULL);
5135 
5136 
5137 						IF l_return_status = FND_API.G_TRUE THEN
5138 								FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_VST_MWO_RLS_UNTLCKD');
5139 								FND_MESSAGE.Set_Token('WO_NAME', l_wo_name);
5140 								FND_MSG_PUB.ADD;
5141 								RAISE FND_API.G_EXC_ERROR;
5142 						END IF;
5143 
5144 						-- rroy
5145 						-- ACL Changes
5146 
5147       l_wo_count := l_wo_count + 1;
5148       l_workorder_tbl(l_wo_count).dml_operation := 'U';
5149       l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
5150       l_workorder_tbl(l_wo_count).header_id := l_wip_entity_id;
5151       l_workorder_tbl(l_wo_count).workorder_id := l_workorder_id;
5152       l_workorder_tbl(l_wo_count).object_version_number := l_object_version_number;
5153       l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_RELEASED;
5154     END IF;
5155 
5156     -- Process all the Child Workorders of the Visit
5157     FOR child_csr IN get_child_wos( l_wip_entity_id ) LOOP
5158       OPEN get_wip_wo(child_csr.child_object_id);
5159       FETCH get_wip_wo INTO l_wo_id, l_ovn, l_wip_id,l_sts_code, l_mwo_flag, l_child_wo_name;
5160       CLOSE get_wip_wo;
5161 
5162       -- If a Child WO needs to be Released add it in the WO Table
5163       IF ( l_sts_code = G_JOB_STATUS_UNRELEASED OR
5164            l_sts_code = G_JOB_STATUS_DRAFT ) THEN
5165 								-- rroy
5166 								-- ACL Changes
5167 								l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
5168 																																																									p_ue_id => NULL,
5169 																																																									p_visit_id => NULL,
5170 																																																									p_item_instance_id => NULL);
5171 
5172 								IF l_return_status = FND_API.G_TRUE THEN
5173 								IF l_mwo_flag <> 'Y' THEN
5174 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_VST_WO_RLS_UNTLCKD');
5175 								ELSE
5176 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_VST_MWO_RLS_UNTLCKD');
5177 								END IF;
5178 										FND_MESSAGE.Set_Token('WO_NAME', l_child_wo_name);
5179 										FND_MSG_PUB.ADD;
5180 										RAISE FND_API.G_EXC_ERROR;
5181 								END IF;
5182 								-- rroy
5183 								-- ACL Changes
5184 
5185         l_wo_count := l_wo_count + 1;
5186         l_workorder_tbl(l_wo_count).dml_operation := 'U';
5187         l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
5188         l_workorder_tbl(l_wo_count).header_id := child_csr.child_object_id;
5189         l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
5190         l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
5191         l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_RELEASED;
5192 
5193       END IF;
5194 
5195     END LOOP;
5196 
5197   -- Process UE
5198   ELSIF ( l_input_type = 'UE' ) THEN
5199 
5200     -- Get the UE Master Workorder
5201     OPEN  get_ue_mwo( p_unit_effectivity_id );
5202     FETCH get_ue_mwo
5203     INTO  l_workorder_id,
5204           l_object_version_number,
5205           l_status_code,
5206           l_wip_entity_id,
5207 										l_wo_name,
5208 										l_master_wo_flag;
5209 
5210     IF ( get_ue_mwo%NOTFOUND ) THEN
5211       FND_MESSAGE.set_name('AHL','AHL_PRD_MR_MWO_NOT_FOUND');
5212       FND_MSG_PUB.add;
5213       CLOSE get_ue_mwo;
5214       RAISE FND_API.G_EXC_ERROR;
5215     END IF;
5216 
5217     CLOSE get_ue_mwo;
5218 
5219     -- Process all the Parent Workorders of the UE
5220     FOR parent_csr IN get_parent_wos( l_wip_entity_id ) LOOP
5221       OPEN get_wip_wo(parent_csr.parent_object_id);
5222       FETCH get_wip_wo INTO  l_wo_id, l_ovn, l_wip_id,l_sts_code, l_mwo_flag, l_parent_wo_name;
5223       CLOSE get_wip_wo;
5224 
5225       -- If a Parent WO needs to be Released add it in the WO Table
5226       IF ( l_sts_code = G_JOB_STATUS_UNRELEASED OR
5227            l_sts_code = G_JOB_STATUS_DRAFT ) THEN
5228 								-- rroy
5229 								-- ACL Changes
5230 								-- skip the check for master workorders
5231 								l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
5232 																																																									p_ue_id => NULL,
5233 																																																									p_visit_id => NULL,
5234 																																																									p_item_instance_id => NULL);
5235 
5236 								IF l_return_status = FND_API.G_TRUE THEN
5237 								IF l_mwo_flag <> 'Y' THEN
5238 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_WO_RLS_UNTLCKD');
5239 								ELSE
5240 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_MWO_RLS_UNTLCKD');
5241 								END IF;
5242 										FND_MESSAGE.Set_Token('WO_NAME', l_parent_wo_name);
5243 										FND_MSG_PUB.ADD;
5244 										RAISE FND_API.G_EXC_ERROR;
5245 								END IF;
5246 								-- rroy
5247 								-- ACL Changes
5248 
5249 
5250         l_wo_count := l_wo_count + 1;
5251         l_workorder_tbl(l_wo_count).dml_operation := 'U';
5252         l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
5253         l_workorder_tbl(l_wo_count).header_id := parent_csr.parent_object_id;
5254         l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
5255         l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
5256         l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_RELEASED;
5257 
5258       END IF;
5259 
5260     END LOOP;
5261 
5262     -- If the UE needs to be Released, Add the WO in the WO Table
5263     IF ( l_status_code = G_JOB_STATUS_UNRELEASED OR
5264          l_status_code = G_JOB_STATUS_DRAFT ) THEN
5265 						-- rroy
5266 						-- ACL Changes
5267 						l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_workorder_id,
5268 																																																									p_ue_id => NULL,
5269 																																																									p_visit_id => NULL,
5270 																																																									p_item_instance_id => NULL);
5271 
5272 						IF l_return_status = FND_API.G_TRUE THEN
5273 						IF l_master_wo_flag <> 'Y' THEN
5274 								FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_WO_RLS_UNTLCKD');
5275 						ELSE
5276 								FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_MWO_RLS_UNTLCKD');
5277 						END IF;
5278 								FND_MESSAGE.Set_Token('WO_NAME', l_wo_name);
5279 								FND_MSG_PUB.ADD;
5280 								RAISE FND_API.G_EXC_ERROR;
5281 						END IF;
5282 						-- rroy
5283 						-- ACL Changes
5284 
5285       l_wo_count := l_wo_count + 1;
5286       l_workorder_tbl(l_wo_count).dml_operation := 'U';
5287       l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
5288       l_workorder_tbl(l_wo_count).header_id := l_wip_entity_id;
5289       l_workorder_tbl(l_wo_count).workorder_id := l_workorder_id;
5290       l_workorder_tbl(l_wo_count).object_version_number := l_object_version_number;
5291       l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_RELEASED;
5292     END IF;
5293 
5294     -- Process all the Child Workorders of the UE
5295     FOR child_csr IN get_child_wos( l_wip_entity_id ) LOOP
5296       OPEN get_wip_wo(child_csr.child_object_id);
5297       FETCH get_wip_wo INTO  l_wo_id, l_ovn, l_wip_id,l_sts_code, l_mwo_flag, l_child_wo_name;
5298       CLOSE get_wip_wo;
5299 
5300       -- If a Child WO needs to be Released add it in the WO Table
5301       IF ( l_sts_code = G_JOB_STATUS_UNRELEASED OR
5302            l_sts_code = G_JOB_STATUS_DRAFT ) THEN
5303 						-- rroy
5304 						-- ACL Changes
5305 						l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
5306 																																																									p_ue_id => NULL,
5307 																																																									p_visit_id => NULL,
5308 																																																									p_item_instance_id => NULL);
5309 						IF l_return_status = FND_API.G_TRUE THEN
5310 						IF l_mwo_flag <> 'Y' THEN
5311 								FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_WO_RLS_UNTLCKD');
5312 						ELSE
5313 								FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_MWO_RLS_UNTLCKD');
5314 						END IF;
5315 								FND_MESSAGE.Set_Token('WO_NAME', l_child_wo_name);
5316 								FND_MSG_PUB.ADD;
5317 								RAISE FND_API.G_EXC_ERROR;
5318 						END IF;
5319 						-- rroy
5320 						-- ACL Changes
5321 
5322         l_wo_count := l_wo_count + 1;
5323         l_workorder_tbl(l_wo_count).dml_operation := 'U';
5324         l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
5325         l_workorder_tbl(l_wo_count).header_id := child_csr.child_object_id;
5326         l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
5327         l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
5328         l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_RELEASED;
5329 
5330       END IF;
5331 
5332     END LOOP;
5333 
5334   -- Process WO
5335   ELSIF ( l_input_type = 'WO' ) THEN
5336 
5337     -- Get the Workorder
5338     OPEN  get_wo( p_workorder_id );
5339     FETCH get_wo
5340     INTO  l_workorder_id,
5341           l_object_version_number,
5342           l_status_code,
5343           l_wip_entity_id,
5344 										l_wo_name;
5345 
5346     IF ( get_wo%NOTFOUND ) THEN
5347       FND_MESSAGE.set_name('AHL','AHL_PRD_WO_NOT_FOUND');
5348       FND_MSG_PUB.add;
5349       CLOSE get_wo;
5350       RAISE FND_API.G_EXC_ERROR;
5351     END IF;
5352 
5353     CLOSE get_wo;
5354 
5355     -- Process all the Parent Workorders of the WO
5356     FOR parent_csr IN get_parent_wos( l_wip_entity_id ) LOOP
5357       OPEN get_wip_wo(parent_csr.parent_object_id);
5358       FETCH get_wip_wo INTO  l_wo_id, l_ovn, l_wip_id,l_sts_code, l_mwo_flag, l_parent_wo_name;
5359       CLOSE get_wip_wo;
5360 
5361       -- If a Parent WO needs to be Released add it in the WO Table
5362       IF ( l_sts_code = G_JOB_STATUS_UNRELEASED OR
5363            l_sts_code = G_JOB_STATUS_DRAFT ) THEN
5364 								-- rroy
5365 								-- ACL Changes
5366 								-- skip the check for master workorders
5367 								l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
5368 																																																									p_ue_id => NULL,
5369 																																																									p_visit_id => NULL,
5370 																																																									p_item_instance_id => NULL);
5371 								IF l_return_status = FND_API.G_TRUE THEN
5372 								IF l_mwo_flag <> 'Y' THEN
5373 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_WO_RLS_UNTLCKD');
5374 								ELSE
5375 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MWO_RLS_UNTLCKD');
5376 								END IF;
5377 										FND_MESSAGE.Set_Token('WO_NAME', l_parent_wo_name);
5378 										FND_MSG_PUB.ADD;
5379 										RAISE FND_API.G_EXC_ERROR;
5380 								END IF;
5381 								-- rroy
5382 								-- ACL Changes
5383 
5384         l_wo_count := l_wo_count + 1;
5385         l_workorder_tbl(l_wo_count).dml_operation := 'U';
5386         l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
5387         l_workorder_tbl(l_wo_count).header_id := parent_csr.parent_object_id;
5388         l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
5389         l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
5390         l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_RELEASED;
5391 
5392       END IF;
5393 
5394     END LOOP;
5395 
5396     -- If the WO needs to be Released, Add the WO in the WO Table
5397     IF ( l_status_code = G_JOB_STATUS_UNRELEASED OR
5398          l_status_code = G_JOB_STATUS_DRAFT ) THEN
5399 						-- rroy
5400 						-- ACL Changes
5401 						l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_workorder_id,
5402 																																																									p_ue_id => NULL,
5403 																																																									p_visit_id => NULL,
5404 																																																									p_item_instance_id => NULL);
5405 						IF l_return_status = FND_API.G_TRUE THEN
5406 								FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_WO_RLS_UNTLCKD');
5407 								FND_MESSAGE.Set_Token('WO_NAME', l_wo_name);
5408 								FND_MSG_PUB.ADD;
5409 								RAISE FND_API.G_EXC_ERROR;
5410 						END IF;
5411 						-- rroy
5412 						-- ACL Changes
5413 
5414       l_wo_count := l_wo_count + 1;
5415       l_workorder_tbl(l_wo_count).dml_operation := 'U';
5416       l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
5417       l_workorder_tbl(l_wo_count).header_id := l_wip_entity_id;
5418       l_workorder_tbl(l_wo_count).workorder_id := l_workorder_id;
5419       l_workorder_tbl(l_wo_count).object_version_number := l_object_version_number;
5420       l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_RELEASED;
5421     END IF;
5422 
5423   END IF;
5424 
5425   -- Invoke Process Jobs API to perform the Release
5426   IF ( l_wo_count > 0 ) THEN
5427     process_jobs
5428     (
5429       p_api_version            => 1.0,
5430       p_init_msg_list          => FND_API.G_TRUE,
5431       p_commit                 => FND_API.G_FALSE,
5432       p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
5433       p_default                => FND_API.G_FALSE,
5434       p_module_type            => NULL,
5435       x_return_status          => l_return_status,
5436       x_msg_count              => l_msg_count,
5437       x_msg_data               => l_msg_data,
5438       p_x_prd_workorder_tbl    => l_workorder_tbl,
5439       p_prd_workorder_rel_tbl  => l_workorder_rel_tbl
5440     );
5441 
5442     IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
5443       RAISE FND_API.G_EXC_ERROR;
5444     END IF;
5445   ELSE
5446       FND_MESSAGE.SET_NAME('AHL','AHL_PRD_VISIT_RELEASED');
5447       FND_MSG_PUB.ADD;
5448       RAISE FND_API.G_EXC_ERROR;
5449   END IF;
5450   IF FND_API.to_boolean(p_commit) THEN
5451     COMMIT;
5452   END IF;
5453 
5454   IF G_DEBUG='Y' THEN
5455     AHL_DEBUG_PUB.disable_debug;
5456   END IF;
5457 
5458 EXCEPTION
5459  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5460     ROLLBACK TO release_visit_jobs_PVT;
5461     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5462     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
5463                                p_count => x_msg_count,
5464                                p_data  => x_msg_data);
5465  WHEN FND_API.G_EXC_ERROR THEN
5466     ROLLBACK TO release_visit_jobs_PVT;
5467     x_return_status := FND_API.G_RET_STS_ERROR;
5468     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
5469                                p_count => x_msg_count,
5470                                p_data  => x_msg_data);
5471 
5472  WHEN OTHERS THEN
5473     ROLLBACK TO release_visit_jobs_PVT;
5474     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5475     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5476       FND_MSG_PUB.add_exc_msg(p_pkg_name        =>g_pkg_name,
5477                               p_procedure_name  =>l_api_name,
5478                               p_error_text      => SUBSTR(SQLERRM,1,240));
5479 
5480     END IF;
5481     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
5482                                p_count => x_msg_count,
5483                                p_data  => x_msg_data);
5484 END release_visit_jobs;
5485 
5486 PROCEDURE validate_dependencies
5487 (
5488   p_api_version         IN   NUMBER    := 1.0,
5489   p_init_msg_list       IN   VARCHAR2  := FND_API.G_TRUE,
5490   p_commit              IN   VARCHAR2  := FND_API.G_FALSE,
5491   p_validation_level    IN   NUMBER    := FND_API.G_VALID_LEVEL_FULL,
5492   p_default             IN   VARCHAR2  := FND_API.G_FALSE,
5493   p_module_type         IN   VARCHAR2  := NULL,
5494   x_return_status       OUT NOCOPY VARCHAR2,
5495   x_msg_count           OUT NOCOPY NUMBER,
5496   x_msg_data            OUT NOCOPY VARCHAR2,
5497   p_visit_id            IN   NUMBER,
5498   p_unit_effectivity_id IN   NUMBER,
5499   p_workorder_id        IN   NUMBER
5500 )
5501 AS
5502   l_api_name     CONSTANT VARCHAR2(30) := 'validate_dependencies';
5503   l_api_version  CONSTANT NUMBER       := 1.0;
5504 
5505   l_input_type            VARCHAR2(3);
5506   l_workorder_name        VARCHAR2(80);
5507   l_wip_entity_id         NUMBER;
5508   l_wo_count              NUMBER := 0;
5509   l_match_found           BOOLEAN := FALSE;
5510 
5511   CURSOR       get_visit_child_wos( c_visit_id NUMBER ) IS
5512     SELECT     workorder_name,
5513                wip_entity_id
5514     FROM       AHL_WORKORDERS
5515     WHERE      visit_id = c_visit_id
5516     AND        status_code NOT IN ( G_JOB_STATUS_COMPLETE,
5517                                     G_JOB_STATUS_COMPLETE_NC,
5518                                     G_JOB_STATUS_CANCELLED,
5519                                     G_JOB_STATUS_CLOSED,
5520                                     G_JOB_STATUS_DELETED );
5521 
5522   CURSOR       get_visit_dependencies( c_visit_id NUMBER, c_wip_entity_id NUMBER ) IS
5523     SELECT     WO.workorder_name workorder_name
5524     FROM       AHL_WORKORDERS WO,
5525                WIP_SCHED_RELATIONSHIPS REL
5526     WHERE      WO.wip_entity_id = REL.parent_object_id
5527     AND        WO.visit_id <> c_visit_id
5528     AND        WO.status_code NOT IN ( G_JOB_STATUS_COMPLETE,
5529                                        G_JOB_STATUS_COMPLETE_NC,
5530                                        G_JOB_STATUS_CANCELLED,
5531                                        G_JOB_STATUS_CLOSED,
5532                                        G_JOB_STATUS_DELETED )
5533     AND        REL.parent_object_type_id = 1
5534     AND        REL.child_object_type_id = 1
5535     AND        REL.child_object_id = c_wip_entity_id
5536     AND        REL.relationship_type = 2;
5537 
5538   CURSOR       get_ue_mwo( c_unit_effectivity_id NUMBER ) IS
5539     SELECT     WO.workorder_name workorder_name,
5540                WO.wip_entity_id wip_entity_id
5541     FROM       AHL_WORKORDERS WO,
5542                AHL_VISIT_TASKS_B VT
5543     WHERE      WO.visit_task_id = VT.visit_task_id
5544     AND        WO.status_code NOT IN ( G_JOB_STATUS_COMPLETE,
5545                                        G_JOB_STATUS_COMPLETE_NC,
5546                                        G_JOB_STATUS_CANCELLED,
5547                                        G_JOB_STATUS_CLOSED,
5548                                        G_JOB_STATUS_DELETED )
5549     AND        VT.task_type_code IN ( 'SUMMARY', 'UNASSOCIATED' )
5550     AND        VT.unit_effectivity_id = c_unit_effectivity_id;
5551 
5552 -- Fix for connect by issue
5553   CURSOR       get_child_wos( c_wip_entity_id NUMBER ) IS
5554     SELECT     WO.wip_entity_id wip_entity_id,
5555                WO.workorder_name workorder_name
5556     FROM       AHL_WORKORDERS WO
5557     WHERE      WO.status_code NOT IN ( G_JOB_STATUS_COMPLETE,
5558                                        G_JOB_STATUS_COMPLETE_NC,
5559                                        G_JOB_STATUS_CANCELLED,
5560                                        G_JOB_STATUS_CLOSED,
5561                                        G_JOB_STATUS_DELETED )
5562     AND        WO.wip_entity_id IN (SELECT REL.child_object_id
5563                FROM WIP_SCHED_RELATIONSHIPS REL
5564                WHERE REL.parent_object_type_id = 1
5565                AND        REL.child_object_type_id = 1
5566                START WITH REL.parent_object_id = c_wip_entity_id
5567                AND    REL.relationship_type = 1
5568                CONNECT BY REL.parent_object_id = PRIOR REL.child_object_id
5569                AND    REL.relationship_type = 1);
5570 
5571   TYPE child_wo_rec IS RECORD
5572   (
5573     wip_entity_id  NUMBER,
5574     workorder_name VARCHAR2(80)
5575   );
5576 
5577   TYPE child_wo_tbl IS TABLE OF child_wo_rec INDEX BY BINARY_INTEGER;
5578 
5579   l_child_wo_tbl child_wo_tbl;
5580 
5581   CURSOR       get_wo_dependencies( c_wip_entity_id NUMBER ) IS
5582     SELECT     WO.workorder_name workorder_name,
5583                WO.wip_entity_id wip_entity_id
5584     FROM       AHL_WORKORDERS WO,
5585                WIP_SCHED_RELATIONSHIPS REL
5586     WHERE      WO.wip_entity_id = REL.parent_object_id
5587     AND        WO.status_code NOT IN ( G_JOB_STATUS_COMPLETE,
5588                                        G_JOB_STATUS_COMPLETE_NC,
5589                                        G_JOB_STATUS_CANCELLED,
5590                                        G_JOB_STATUS_CLOSED,
5591                                        G_JOB_STATUS_DELETED )
5592     AND        REL.parent_object_type_id = 1
5593     AND        REL.child_object_type_id = 1
5594     AND        REL.child_object_id = c_wip_entity_id
5595     AND        REL.relationship_type = 2;
5596 
5597   CURSOR       get_wo( c_workorder_id NUMBER ) IS
5598     SELECT     workorder_name,
5599                wip_entity_id
5600     FROM       AHL_WORKORDERS
5601     WHERE      workorder_id =p_workorder_id
5602     AND        status_code NOT IN ( G_JOB_STATUS_COMPLETE,
5603                                     G_JOB_STATUS_COMPLETE_NC,
5604                                     G_JOB_STATUS_CANCELLED,
5605                                     G_JOB_STATUS_CLOSED,
5606                                     G_JOB_STATUS_DELETED );
5607 
5608 BEGIN
5609   SAVEPOINT validate_dependencies_PVT;
5610 
5611   IF NOT FND_API.compatible_api_call(l_api_version,
5612                                      p_api_version,
5613                                      l_api_name,G_PKG_NAME) THEN
5614     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5615   END IF;
5616 
5617   IF FND_API.to_boolean(p_init_msg_list) THEN
5618     FND_MSG_PUB.initialize;
5619   END IF;
5620 
5621   x_return_status:=FND_API.G_RET_STS_SUCCESS;
5622 
5623   IF G_DEBUG='Y' THEN
5624       AHL_DEBUG_PUB.enable_debug;
5625   END IF;
5626 
5627   -- Validate Inputs
5628   IF ( ( p_workorder_id IS NULL OR
5629          p_workorder_id = FND_API.G_MISS_NUM ) AND
5630        ( p_unit_effectivity_id IS NULL OR
5631          p_unit_effectivity_id = FND_API.G_MISS_NUM ) AND
5632        ( p_visit_id IS NULL OR
5633          p_visit_id =  FND_API.G_MISS_NUM ) ) THEN
5634     FND_MESSAGE.set_name('AHL','AHL_PRD_WRONG_ARGUMENTS');
5635     FND_MESSAGE.set_token('PROC_NAME', l_api_name);
5636     FND_MSG_PUB.add;
5637     RAISE FND_API.G_EXC_ERROR;
5638   END IF;
5639 
5640   -- Determine the type of API call
5641   IF ( p_workorder_id IS NOT NULL AND
5642        p_workorder_id <> FND_API.G_MISS_NUM ) THEN
5643     l_input_type := 'WO';
5644   ELSIF ( p_unit_effectivity_id IS NOT NULL AND
5645           p_unit_effectivity_id <> FND_API.G_MISS_NUM ) THEN
5646     l_input_type := 'UE';
5647   ELSIF ( p_visit_id IS NOT NULL AND
5648           p_visit_id <>  FND_API.G_MISS_NUM ) THEN
5649     l_input_type := 'VST';
5650   END IF;
5651 
5652   -- Validate Visit Dependencies
5653   IF ( l_input_type = 'VST' ) THEN
5654 
5655     FOR visit_csr IN get_visit_child_wos( p_visit_id ) LOOP
5656 
5657       FOR dep_csr IN get_visit_dependencies( p_visit_id, visit_csr.wip_entity_id ) LOOP
5658         FND_MESSAGE.set_name('AHL','AHL_PRD_DEP_WO_NOT_CMPL');
5659         FND_MESSAGE.set_token('WO', visit_csr.workorder_name);
5660         FND_MESSAGE.set_token('DEP_WO', dep_csr.workorder_name);
5661         FND_MSG_PUB.add;
5662       END LOOP;
5663 
5664     END LOOP;
5665 
5666   -- Validate UE Dependencies
5667   ELSIF ( l_input_type = 'UE' ) THEN
5668 
5669     -- Get the UE Master Workorder
5670     OPEN  get_ue_mwo( p_unit_effectivity_id );
5671     FETCH get_ue_mwo
5672     INTO  l_workorder_name,
5673           l_wip_entity_id;
5674 
5675     IF ( get_ue_mwo%NOTFOUND ) THEN
5676       CLOSE get_ue_mwo;
5677       RETURN;
5678     END IF;
5679     CLOSE get_ue_mwo;
5680 
5681     OPEN get_child_wos( l_wip_entity_id );
5682     LOOP
5683       EXIT WHEN get_child_wos%NOTFOUND;
5684 
5685       l_wo_count := l_wo_count + 1;
5686       FETCH get_child_wos
5687       INTO  l_child_wo_tbl( l_wo_count ).wip_entity_id,
5688             l_child_wo_tbl( l_wo_count ).workorder_name;
5689     END LOOP;
5690     CLOSE get_child_wos;
5691     -- Removing the below check
5692 				-- as part of fix for bug 4094884
5693 				-- The below check is to see if there are any child workorders
5694 				-- which are not complete
5695 				-- but now all child dependencies are being auto-deleted
5696 				-- Note: At present this API is being used by
5697 				-- cancel_visit_jobs API alone
5698 				-- If this API is called from any other API, then the below validations
5699 				-- may need to be added accordingly
5700     /*
5701     FOR dep_csr IN get_wo_dependencies( l_wip_entity_id ) LOOP
5702       FOR j IN l_child_wo_tbl.FIRST..l_child_wo_tbl.LAST LOOP
5703         IF ( dep_csr.wip_entity_id = l_child_wo_tbl(j).wip_entity_id ) THEN
5704           l_match_found := TRUE;
5705           EXIT;
5706         END IF;
5707       END LOOP;
5708 
5709       IF ( l_match_found = TRUE ) THEN
5710         l_match_found := FALSE;
5711       ELSE
5712         FND_MESSAGE.set_name('AHL','AHL_PRD_DEP_WO_NOT_CMPL');
5713         FND_MESSAGE.set_token('WO', l_workorder_name);
5714         FND_MESSAGE.set_token('DEP_WO', dep_csr.workorder_name);
5715         FND_MSG_PUB.add;
5716       END IF;
5717     END LOOP;
5718 				*/
5719 				-- Removing the below check
5720 				-- as part of fix for bug 4094884
5721 				-- The below check is to see if there are any child workorders
5722 				-- which are not complete
5723 				-- but now all child dependencies are being auto-deleted
5724 				-- Note: At present this API is being used by
5725 				-- cancel_visit_jobs API alone
5726 				-- If this API is called from any other API, then the below validations
5727 				-- may need to be added accordingly
5728 
5729     /*
5730     IF l_child_wo_tbl.COUNT > 0 THEN
5731     --
5732     FOR i IN l_child_wo_tbl.FIRST..l_child_wo_tbl.LAST LOOP
5733       FOR dep_csr IN get_wo_dependencies( l_child_wo_tbl(i).wip_entity_id ) LOOP
5734 
5735         FOR j IN l_child_wo_tbl.FIRST..l_child_wo_tbl.LAST LOOP
5736           IF ( dep_csr.wip_entity_id = l_child_wo_tbl(j).wip_entity_id ) THEN
5737             l_match_found := TRUE;
5738             EXIT;
5739           END IF;
5740         END LOOP;
5741 
5742         IF ( l_match_found = TRUE ) THEN
5743           l_match_found := FALSE;
5744         ELSE
5745           FND_MESSAGE.set_name('AHL','AHL_PRD_DEP_WO_NOT_CMPL');
5746           FND_MESSAGE.set_token('WO', l_child_wo_tbl(i).workorder_name);
5747           FND_MESSAGE.set_token('DEP_WO', dep_csr.workorder_name);
5748           FND_MSG_PUB.add;
5749         END IF;
5750       END LOOP;
5751     END LOOP;
5752 
5753    END IF;
5754 			*/
5755   -- Validate WO Dependencies
5756   ELSIF ( l_input_type = 'WO' ) THEN
5757 
5758     -- Get the Workorder
5759     OPEN  get_wo( p_workorder_id );
5760     FETCH get_wo
5761     INTO  l_workorder_name,
5762           l_wip_entity_id;
5763 
5764     IF ( get_wo%NOTFOUND ) THEN
5765       CLOSE get_wo;
5766       RETURN;
5767     END IF;
5768     CLOSE get_wo;
5769     -- removing the below check
5770 				-- since as part of bug fix for bug #4094884
5771 				-- completion dependencies are deleted automatically.
5772 				-- Note: At present this API is being used by
5773 				-- cancel_visit_jobs API alone
5774 				-- If this API is called from any other API, then the below validations
5775 				-- may need to be added accordingly
5776 
5777     /*
5778     FOR dep_csr IN get_wo_dependencies( l_wip_entity_id ) LOOP
5779       FND_MESSAGE.set_name('AHL','AHL_PRD_DEP_WO_NOT_CMPL');
5780       FND_MESSAGE.set_token('WO', l_workorder_name);
5781       FND_MESSAGE.set_token('DEP_WO', dep_csr.workorder_name);
5782       FND_MSG_PUB.add;
5783     END LOOP;
5784 				*/
5785   END IF;
5786 
5787   x_msg_count := FND_MSG_PUB.count_msg;
5788   IF ( x_msg_count > 0 ) THEN
5789     RAISE FND_API.G_EXC_ERROR;
5790   END IF;
5791 
5792 EXCEPTION
5793  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5794     ROLLBACK TO validate_dependencies_PVT;
5795     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5796     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
5797                                p_count => x_msg_count,
5798                                p_data  => x_msg_data);
5799  WHEN FND_API.G_EXC_ERROR THEN
5800     ROLLBACK TO validate_dependencies_PVT;
5801     x_return_status := FND_API.G_RET_STS_ERROR;
5802     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
5803                                p_count => x_msg_count,
5804                                p_data  => x_msg_data);
5805 
5806  WHEN OTHERS THEN
5807     ROLLBACK TO validate_dependencies_PVT;
5808     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5809     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5810       FND_MSG_PUB.add_exc_msg(p_pkg_name        =>g_pkg_name,
5811                               p_procedure_name  =>l_api_name,
5812                               p_error_text      => SUBSTR(SQLERRM,1,240));
5813 
5814     END IF;
5815     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
5816                                p_count => x_msg_count,
5817                                p_data  => x_msg_data);
5818 END validate_dependencies;
5819 
5820 FUNCTION are_child_wos_cancelled
5821 (
5822   p_wip_entity_id  IN NUMBER,
5823   p_workorder_tbl  IN PRD_WORKORDER_TBL
5824 ) RETURN BOOLEAN
5825 IS
5826 
5827   l_match_found BOOLEAN := FALSE;
5828 
5829   -- Get Child WOs which are not cancelled
5830   CURSOR       get_child_wos( c_wip_entity_id NUMBER ) IS
5831     SELECT     WO.workorder_id workorder_id
5832     FROM       AHL_WORKORDERS WO,
5833                WIP_SCHED_RELATIONSHIPS REL
5834     WHERE      WO.status_code NOT IN ( G_JOB_STATUS_CANCELLED,
5835                                        G_JOB_STATUS_DELETED )
5836     AND        WO.wip_entity_id = REL.child_object_id
5837     AND        REL.parent_object_id = c_wip_entity_id
5838     AND        REL.parent_object_type_id = 1
5839     AND        REL.child_object_type_id = 1
5840     AND        REL.relationship_type = 1;
5841 
5842 BEGIN
5843   FOR child_csr IN get_child_wos( p_wip_entity_id ) LOOP
5844     FOR i IN p_workorder_tbl.FIRST..p_workorder_tbl.LAST LOOP
5845       IF ( p_workorder_tbl(i).workorder_id = child_csr.workorder_id ) THEN
5846         l_match_found := TRUE;
5847         EXIT;
5848       END IF;
5849     END LOOP;
5850 
5851     IF ( l_match_found = FALSE ) THEN
5852       RETURN FALSE;
5853     ELSE
5854       l_match_found := FALSE;
5855     END IF;
5856   END LOOP;
5857   --Modified by srini not to cancel the master workorders, if all the child work orders were cancelled
5858   RETURN FALSE;
5859 
5860 END are_child_wos_cancelled;
5861 
5862 PROCEDURE cancel_visit_jobs
5863 (
5864   p_api_version         IN   NUMBER    := 1.0,
5865   p_init_msg_list       IN   VARCHAR2  := FND_API.G_TRUE,
5866   p_commit              IN   VARCHAR2  := FND_API.G_FALSE,
5867   p_validation_level    IN   NUMBER    := FND_API.G_VALID_LEVEL_FULL,
5868   p_default             IN   VARCHAR2  := FND_API.G_FALSE,
5869   p_module_type         IN   VARCHAR2  := NULL,
5870   x_return_status       OUT NOCOPY  VARCHAR2,
5871   x_msg_count           OUT NOCOPY NUMBER,
5872   x_msg_data            OUT NOCOPY VARCHAR2,
5873   p_visit_id            IN   NUMBER,
5874   p_unit_effectivity_id IN   NUMBER,
5875   p_workorder_id        IN   NUMBER
5876 )
5877 AS
5878   l_api_name     CONSTANT VARCHAR2(30) := 'cancel_visit_jobs';
5879   l_api_version  CONSTANT NUMBER       := 1.0;
5880   l_msg_count             NUMBER;
5881   l_msg_data              VARCHAR2(2000);
5882   l_return_status         VARCHAR2(1);
5883 
5884   l_input_type            VARCHAR2(3);
5885 
5886   l_wo_count              NUMBER := 0;
5887   l_mwo_count              NUMBER := 0;
5888 		idx                     NUMBER := 0;
5889 		l_count                 NUMBER := 0;
5890 
5891   /* bug 5104519 - start */
5892   l_mwo_return_status      VARCHAR2(1);
5893   --l_mwo_flag 		  VARCHAR2(1);
5894   l_cannot_cancel_child	  NUMBER;
5895 
5896   l_master_workorder_tbl     PRD_WORKORDER_TBL;
5897   l_copy_mwo_tbl          PRD_WORKORDER_TBL;
5898   /* bug 5104519 - end */
5899 
5900   l_workorder_tbl         PRD_WORKORDER_TBL;
5901   l_workorder_rel_tbl     PRD_WORKORDER_REL_TBL;
5902 		l_workorder_rel_cancel_tbl PRD_WORKORDER_REL_TBL;
5903 
5904   l_ue_count              NUMBER := 0;
5905   l_unit_effectivity_tbl  AHL_UMP_UNITMAINT_PVT.unit_effectivity_tbl_type;
5906   l_unit_accomplish_tbl   AHL_UMP_UNITMAINT_PVT.unit_accomplish_tbl_type;
5907   l_unit_threshold_tbl    AHL_UMP_UNITMAINT_PVT.unit_threshold_tbl_type;
5908 
5909   l_workorder_id          NUMBER;
5910   l_unit_effectivity_id   NUMBER;
5911   l_object_version_number NUMBER;
5912   l_wip_entity_id         NUMBER;
5913   l_ue_wip_entity_id         NUMBER;
5914   l_status_code           VARCHAR2(30);
5915   l_status_meaning        VARCHAR2(80);
5916 		l_eam_wo_tbl            EAM_PROCESS_WO_PUB.eam_wo_tbl_type;
5917   l_eam_wo_relations_tbl  EAM_PROCESS_WO_PUB.eam_wo_relations_tbl_type;
5918   l_eam_op_tbl            EAM_PROCESS_WO_PUB.eam_op_tbl_type;
5919   l_eam_res_req_tbl       EAM_PROCESS_WO_PUB.eam_res_tbl_type;
5920   l_eam_mat_req_tbl       EAM_PROCESS_WO_PUB.eam_mat_req_tbl_type;
5921 		l_rel_found             BOOLEAN := FALSE;
5922 
5923   l_wo_id                 NUMBER;
5924   l_ovn                   NUMBER;
5925   l_sts_code              VARCHAR2(30);
5926   l_wip_id                NUMBER;
5927 		l_mwo_flag 							      VARCHAR2(1);
5928 		l_child_wo_name         VARCHAR2(80);
5929 		l_parent_wo_name        VARCHAR2(80);
5930 
5931 
5932 		-- rroy
5933 		-- ACL Changes
5934 		l_wo_name 														VARCHAR2(80);
5935 		l_master_wo_flag 							VARCHAR2(1);
5936 		-- rroy
5937 		-- ACL Changes
5938 
5939   -- To get the Visit Master Workorder
5940   CURSOR       get_visit_mwo( c_visit_id NUMBER ) IS
5941     SELECT     workorder_id,
5942                object_version_number,
5943                status_code,
5944                wip_entity_id,
5945 															workorder_name
5946     FROM       AHL_WORKORDERS
5947     WHERE      visit_id = c_visit_id
5948     AND        status_code NOT IN (G_JOB_STATUS_DELETED,G_JOB_STATUS_CANCELLED)
5949     AND        visit_task_id IS NULL;
5950 
5951   -- To get the UE Master Workorder
5952   CURSOR       get_ue_mwo( c_unit_effectivity_id NUMBER ) IS
5953     SELECT     WO.workorder_id workorder_id,
5954                WO.object_version_number wo_object_version_number,
5955                WO.status_code status_code,
5956                WO.wip_entity_id wip_entity_id,
5957 															WO.workorder_name workorder_name,
5958 															WO.master_workorder_flag master_workorder_flag
5959     FROM       AHL_WORKORDERS WO,
5960                AHL_VISIT_TASKS_B VT
5961     WHERE      WO.status_code <> G_JOB_STATUS_DELETED
5962     AND        WO.visit_task_id = VT.visit_task_id
5963     AND        VT.task_type_code IN ( 'SUMMARY', 'UNASSOCIATED' )
5964     AND        VT.unit_effectivity_id = c_unit_effectivity_id;
5965 
5966 		-- bug 4094884
5967 		-- To get the UE Workorder
5968   CURSOR       get_ue_wo( c_unit_effectivity_id NUMBER ) IS
5969     SELECT     WO.wip_entity_id wip_entity_id
5970     FROM       AHL_WORKORDERS WO,
5971                AHL_VISIT_TASKS_B VT
5972     WHERE      WO.status_code <> G_JOB_STATUS_DELETED
5973     AND        WO.visit_task_id = VT.visit_task_id
5974     AND        VT.task_type_code NOT IN ('SUMMARY')
5975     AND        VT.unit_effectivity_id = c_unit_effectivity_id;
5976 
5977 
5978   -- To get the Workorder
5979   CURSOR       get_wo( c_workorder_id NUMBER ) IS
5980     SELECT     workorder_id,
5981                object_version_number,
5982                status_code,
5983                wip_entity_id,
5984 															workorder_name
5985     FROM       AHL_WORKORDERS
5986     WHERE      workorder_id =p_workorder_id;
5987 
5988   -- To get the Child Workorders of a Master Workorder
5989   -- Fix for connect by issue
5990   -- Balaji added master workorder flag for bug # 5104519
5991   /* bug 5104519 - start */
5992   CURSOR       get_wip_wo(c_wip_entity_id NUMBER ) IS
5993     SELECT     WO.workorder_id workorder_id,
5994                WO.object_version_number object_version_number,
5995                WO.status_code status_code,
5996                WO.wip_entity_id wip_entity_id,
5997 	       WO.master_workorder_flag,
5998 	       WO.workorder_name
5999     FROM       AHL_WORKORDERS WO
6000     WHERE      WO.wip_entity_id = c_wip_entity_id;
6001   /* bug 5104519 - end */
6002   -- To get the Child Workorders of a Master Workorder
6003   CURSOR       get_child_wos( c_wip_entity_id NUMBER ) IS
6004     SELECT     REL.child_object_id
6005     FROM       WIP_SCHED_RELATIONSHIPS REL
6006     WHERE      REL.parent_object_type_id = 1
6007     AND        REL.child_object_type_id = 1
6008     START WITH REL.parent_object_id = c_wip_entity_id
6009         AND    REL.relationship_type = 1
6010     CONNECT BY REL.parent_object_id = PRIOR REL.child_object_id
6011         AND    REL.relationship_type = 1
6012     ORDER BY   level DESC;
6013 
6014   -- To get the Parent Workorders of a Workorder
6015   CURSOR       get_parent_wos( c_wip_entity_id NUMBER ) IS
6016     SELECT     REL.parent_object_id
6017     FROM       WIP_SCHED_RELATIONSHIPS REL
6018     WHERE      REL.parent_object_type_id = 1
6019     AND        REL.child_object_type_id = 1
6020     START WITH REL.child_object_id = c_wip_entity_id
6021         AND    REL.relationship_type = 1
6022     CONNECT BY REL.child_object_id = PRIOR REL.parent_object_id
6023         AND    REL.relationship_type = 1
6024     ORDER BY   level;
6025 
6026   -- To Get the Top Unplanned UEs for a Visit
6027   /*
6028    * As per the mail from Shailaja dated 20-Apr-2005, Cancel Visit Jobs and Cancel MR Jobs will
6029    * not update the UE Status to 'CANCELLED'. Hence Balaji commented out these 4 cursors.
6030    * Reference bug #s 4095002 and 4094884.
6031    */
6032   /*
6033   CURSOR       get_visit_mrs( c_visit_id NUMBER ) IS
6034     SELECT     UE.unit_effectivity_id unit_effectivity_id,
6035                UE.object_version_number object_version_number
6036     FROM       AHL_UNIT_EFFECTIVITIES_B UE,
6037                AHL_VISIT_TASKS_B VT
6038     WHERE      UE.unit_effectivity_id = VT.unit_effectivity_id
6039     AND        UE.manually_planned_flag = 'Y'
6040     -- Check added by balaji by balaji for bug # 4095002
6041     -- As per the update in the bug, for Manually planned UEs of type SR
6042     -- Status should not be updated to CANCELLED on workorder or MR or Visit
6043     -- Cancellation. Hence adding the check to filter out UEs based on SRs.
6044     AND        UE.object_type <> 'SR'
6045     AND        VT.task_type_code = 'SUMMARY'
6046     AND        VT.originating_task_id IS NULL
6047     AND        VT.unit_effectivity_id IS NOT NULL
6048     AND        VT.visit_id = c_visit_id;
6049 
6050   -- To Get the Unplanned UE Details
6051   CURSOR       get_ue_details( c_unit_effectivity_id NUMBER ) IS
6052     SELECT     UE.object_version_number object_version_number
6053     FROM       AHL_UNIT_EFFECTIVITIES_B UE
6054     WHERE      UE.unit_effectivity_id = c_unit_effectivity_id
6055     AND        UE.manually_planned_flag = 'Y'
6056     -- Check added by balaji by balaji for bug # 4095002
6057     -- As per the update in the bug, for Manually planned UEs of type SR
6058     -- Status should not be updated to CANCELLED on workorder or MR or Visit
6059     -- Cancellation. Hence adding the check to filter out UEs based on SRs.
6060     AND        UE.object_type <> 'SR';
6061 
6062   -- To get the UnPlanned UE for a given WO
6063   CURSOR       get_ue_details_for_wo( c_workorder_id NUMBER ) IS
6064     SELECT     UE.unit_effectivity_id unit_effectivity_id,
6065                UE.object_version_number object_version_number
6066     FROM       AHL_UNIT_EFFECTIVITIES_B UE,
6067                AHL_VISIT_TASKS_B VT,
6068                AHL_WORKORDERS WO
6069     WHERE      UE.unit_effectivity_id = VT.unit_effectivity_id
6070     AND        UE.manually_planned_flag = 'Y'
6071     -- Check added by balaji by balaji for bug # 4095002
6072     -- As per the update in the bug, for Manually planned UEs of type SR
6073     -- Status should not be updated to CANCELLED on workorder or MR or Visit
6074     -- Cancellation. Hence adding the check to filter out UEs based on SRs.
6075     AND        UE.object_type <> 'SR'
6076     AND        VT.visit_task_id = WO.visit_task_id
6077     AND        WO.workorder_id = c_workorder_id;
6078 
6079   -- To get all the Parent UnPlanned UEs for a given UE
6080   CURSOR       get_parent_ues( c_unit_effectivity_id NUMBER ) IS
6081     SELECT     UE.unit_effectivity_id unit_effectivity_id,
6082                UE.object_version_number object_version_number
6083     FROM       AHL_UNIT_EFFECTIVITIES_B UE,
6084                AHL_UE_RELATIONSHIPS REL
6085     WHERE      UE.unit_effectivity_id = REL.ue_id
6086     AND        UE.manually_planned_flag = 'Y'
6087     -- Check added by balaji by balaji for bug # 4095002
6088     -- As per the update in the bug, for Manually planned UEs of type SR
6089     -- Status should not be updated to CANCELLED on workorder or MR or Visit
6090     -- Cancellation. Hence adding the check to filter out UEs based on SRs.
6091     AND        UE.object_type <> 'SR'
6092     START WITH REL.related_ue_id = c_unit_effectivity_id
6093            AND REL.relationship_code = 'PARENT'
6094     CONNECT BY REL.related_ue_id = PRIOR REL.ue_id
6095            AND REL.relationship_code = 'PARENT'
6096     ORDER BY   level;
6097     */
6098 				-- To get all the workorders for a visit
6099 				-- that are not master workorders
6100 				CURSOR get_visit_wos(c_visit_id NUMBER)
6101 				IS
6102 				SELECT wip_entity_id
6103 				FROM AHL_WORKORDERS
6104 				WHERE visit_id = c_visit_id
6105 				AND master_workorder_flag <> 'Y';
6106 
6107 				-- to see if a workorder is a top level workorder
6108 				-- in the completion dependency hierarchy
6109 				CURSOR get_parent_wos_count(c_wip_entity_id NUMBER)
6110 				IS
6111 				SELECT count(*)
6112 				FROM WIP_SCHED_RELATIONSHIPS
6113 				WHERE child_object_id = c_wip_entity_id
6114 				AND   child_object_type_id = 1
6115 				AND   relationship_type = 2;
6116 
6117 				-- to get all the child workorders
6118 				-- for a given top level workorder
6119 				-- completion dependencies
6120 
6121 				CURSOR     get_completion_dep_wo_all(c_wip_entity_id NUMBER)
6122 				IS
6123     SELECT     REL.sched_relationship_id,
6124 															REL.parent_object_id,
6125 															REL.child_object_id
6126 				FROM       --AHL_WORKORDERS WO,
6127                WIP_SCHED_RELATIONSHIPS REL
6128     WHERE      --WO.wip_entity_id = REL.child_object_id
6129                REL.parent_object_type_id = 1
6130     AND        REL.child_object_type_id = 1
6131 				START WITH REL.parent_object_id = c_wip_entity_id
6132 				AND 							REL.relationship_type = 2
6133 				CONNECT BY REL.parent_object_id = PRIOR REL.child_object_id
6134 				AND 							REL.relationship_type = 2
6135 				ORDER BY level DESC;
6136 
6137     -- to get all the parent wos of a ue mwo
6138 				CURSOR     get_ue_completion_parents(c_wip_entity_id NUMBER)
6139 				IS
6140     SELECT     REL.sched_relationship_id,
6141 															REL.parent_object_id,
6142 															REL.child_object_id
6143 				FROM       WIP_SCHED_RELATIONSHIPS REL
6144     WHERE      REL.parent_object_type_id = 1
6145     AND        REL.child_object_type_id = 1
6146 				START WITH REL.child_object_id = c_wip_entity_id
6147 				AND 							REL.relationship_type = 2
6148 				CONNECT BY REL.child_object_id = PRIOR REL.parent_object_id
6149 				AND 							REL.relationship_type = 2
6150 				ORDER BY level;
6151 
6152 				-- To get the immediate parent of a ue wo
6153 				CURSOR     get_immediate_ue_parent(c_wip_entity_id NUMBER)
6154 				IS
6155     SELECT     REL.sched_relationship_id,
6156 															REL.parent_object_id
6157 				FROM       WIP_SCHED_RELATIONSHIPS REL
6158     WHERE      REL.child_object_id = c_wip_entity_id
6159     AND        REL.parent_object_type_id = 1
6160     AND        REL.child_object_type_id = 1
6161 				AND 							REL.relationship_type = 2;
6162 
6163 				get_immediate_ue_parent_rec get_immediate_ue_parent%ROWTYPE;
6164 
6165 				-- To get all the immediate child workorders of a
6166 				-- particular workorder
6167 				CURSOR     get_completion_dep_wo(c_wip_entity_id NUMBER)
6168 				IS
6169     SELECT     REL.sched_relationship_id,
6170 															REL.child_object_id
6171 				FROM       WIP_SCHED_RELATIONSHIPS REL
6172     WHERE      REL.parent_object_id = c_wip_entity_id
6173     AND        REL.parent_object_type_id = 1
6174     AND        REL.child_object_type_id = 1
6175 				AND 							REL.relationship_type = 2;
6176 
6177 				-- To get all the immediate parent workorders of a
6178 				-- particular workorder
6179 				CURSOR     get_completion_dep_wo_child(c_wip_entity_id NUMBER)
6180 				IS
6181     SELECT     REL.sched_relationship_id,
6182 															REL.parent_object_id
6183 				FROM       WIP_SCHED_RELATIONSHIPS REL
6184     WHERE      REL.child_object_id = c_wip_entity_id
6185     AND        REL.parent_object_type_id = 1
6186     AND        REL.child_object_type_id = 1
6187 				AND 							REL.relationship_type = 2;
6188 
6189   /* bug 5104519 - start */
6190   -- cursor to check if all workorders in a visit are cancelled.
6191   CURSOR  chk_cmplt_wo_exists(c_wip_entity_id NUMBER )
6192   IS
6193     SELECT 'x'
6194     FROM AHL_WORKORDERS AWO, WIP_DISCRETE_JOBS WDJ
6195     WHERE awo.wip_entity_id = wdj.wip_entity_id
6196        AND wdj.date_completed IS NOT NULL
6197        --AND master_workorder_flag = 'N'
6198        --AND status_code NOT IN ('7', '22', '12')
6199        AND VISIT_TASK_ID IS NOT NULL
6200        AND awo.wip_entity_id IN (SELECT rel.child_object_id
6201                                 FROM wip_sched_relationships rel
6202                                 START WITH REL.parent_object_id = c_wip_entity_id
6203                                 CONNECT BY REL.parent_object_id = PRIOR REL.child_object_id
6204                                 AND REL.parent_object_type_id = PRIOR REL.child_object_type_id
6205                                 AND REL.relationship_type = 1);
6206   l_exists   VARCHAR2(1);
6207   /* bug 5104519 - end */
6208 
6209 BEGIN
6210   SAVEPOINT cancel_visit_jobs_PVT;
6211 
6212   IF NOT FND_API.compatible_api_call(l_api_version,
6213                                      p_api_version,
6214                                      l_api_name,G_PKG_NAME) THEN
6215     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6216   END IF;
6217 
6218   IF FND_API.to_boolean(p_init_msg_list) THEN
6219     FND_MSG_PUB.initialize;
6220   END IF;
6221 
6222   x_return_status:=FND_API.G_RET_STS_SUCCESS;
6223 
6224   IF G_DEBUG='Y' THEN
6225     AHL_DEBUG_PUB.enable_debug;
6226   END IF;
6227 
6228   --sikumar: added for FP for ER 5571241 -- Check if user has permission to cancel jobs.
6229   IF AHL_PRD_UTIL_PKG.Is_Wo_Cancel_Allowed = FND_API.G_FALSE THEN
6230     FND_MESSAGE.set_name('AHL','AHL_PRD_CANCEL_NOT_ALLOWED');
6231     FND_MSG_PUB.add;
6232     RAISE FND_API.G_EXC_ERROR;
6233   END IF;
6234   -- Validate Inputs
6235   IF ( ( p_workorder_id IS NULL OR
6236          p_workorder_id = FND_API.G_MISS_NUM ) AND
6237        ( p_unit_effectivity_id IS NULL OR
6238          p_unit_effectivity_id = FND_API.G_MISS_NUM ) AND
6239        ( p_visit_id IS NULL OR
6240          p_visit_id =  FND_API.G_MISS_NUM ) ) THEN
6241     FND_MESSAGE.set_name('AHL','AHL_PRD_WRONG_ARGUMENTS');
6242     FND_MESSAGE.set_token('PROC_NAME', l_api_name);
6243     FND_MSG_PUB.add;
6244     RAISE FND_API.G_EXC_ERROR;
6245   END IF;
6246 
6247 
6248 
6249   -- Validate if Completion Dependencies exist
6250   validate_dependencies
6251   (
6252     p_api_version            => 1.0,
6253     p_init_msg_list          => FND_API.G_TRUE,
6254     p_commit                 => FND_API.G_FALSE,
6255     p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
6256     p_default                => FND_API.G_FALSE,
6257     p_module_type            => NULL,
6258     x_return_status          => l_return_status,
6259     x_msg_count              => l_msg_count,
6260     x_msg_data               => l_msg_data,
6261     p_visit_id               => p_visit_id,
6262     p_unit_effectivity_id    => p_unit_effectivity_id,
6263     p_workorder_id           => p_workorder_id
6264   );
6265 
6266   IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
6267     RAISE FND_API.G_EXC_ERROR;
6268   END IF;
6269 
6270   -- Determine the type of API call
6271   IF ( p_workorder_id IS NOT NULL AND
6272        p_workorder_id <> FND_API.G_MISS_NUM ) THEN
6273     l_input_type := 'WO';
6274   ELSIF ( p_unit_effectivity_id IS NOT NULL AND
6275           p_unit_effectivity_id <> FND_API.G_MISS_NUM ) THEN
6276     l_input_type := 'UE';
6277   ELSIF ( p_visit_id IS NOT NULL AND
6278           p_visit_id <>  FND_API.G_MISS_NUM ) THEN
6279     l_input_type := 'VST';
6280   END IF;
6281 
6282   -- Process Inputs for Cancelling Workorders
6283   -- Process Visit
6284   IF ( l_input_type = 'VST' ) THEN
6285  			-- bug 4094884
6286 				-- need to delete all completion dependencies
6287 				-- if the l_input_type = 'VST' (whole visit is cancelled)
6288 				-- so all the completion dependencies need to be deleted
6289     -- will use process_eam_workorders
6290 
6291 				-- 1. Find all the workorders in the visit that are not master wos
6292 				-- 2. Loop through all the workorders and  find the top level wos
6293 				-- 3. get the entire hierarchy of workorders for all top level workorders
6294 				idx := 1;
6295 				FOR visit_wos_rec IN get_visit_wos(p_visit_id) LOOP
6296 				  OPEN get_parent_wos_count(visit_wos_rec.wip_entity_id);
6297 						FETCH get_parent_wos_count INTO l_count;
6298 						CLOSE get_parent_wos_count;
6299 						IF l_count = 0 THEN
6300 						-- this is a top level workorder in the completion dependency hierarchy
6301 
6302 				  FOR com_dep_rec IN get_completion_dep_wo_all(visit_wos_rec.wip_entity_id) LOOP
6303 								l_rel_found := FALSE;
6304 								IF l_workorder_rel_tbl.COUNT > 0 THEN
6305 								FOR i IN l_workorder_rel_tbl.FIRST..l_workorder_rel_tbl.LAST LOOP
6306 								  IF l_workorder_rel_tbl(i).wo_relationship_id = com_dep_rec.sched_relationship_id THEN
6307 												l_rel_found := TRUE;
6308 												EXIT;
6309 										END IF;
6310 								END LOOP;
6311 								END IF;
6312 
6313 								IF l_rel_found = FALSE THEN
6314 								l_workorder_rel_tbl(idx).wo_relationship_id := com_dep_rec.sched_relationship_id;
6315 								l_workorder_rel_tbl(idx).batch_id := p_visit_id;
6316 								l_workorder_rel_tbl(idx).parent_header_id := idx;
6317 								l_workorder_rel_tbl(idx).child_header_id := idx;
6318 								l_workorder_rel_tbl(idx).parent_wip_entity_id := com_dep_rec.parent_object_id;
6319 								l_workorder_rel_tbl(idx).child_wip_entity_id := com_dep_rec.child_object_id;
6320 								l_workorder_rel_tbl(idx).relationship_type := 2;
6321 								l_workorder_rel_tbl(idx).dml_operation := 'D';
6322 								idx := idx + 1;
6323 								END IF;
6324 						END LOOP;
6325 						END IF;
6326 				END LOOP;
6327 
6328   				IF ( l_workorder_rel_tbl.COUNT > 0 ) THEN
6329     		FOR i IN l_workorder_rel_tbl.FIRST..l_workorder_rel_tbl.LAST LOOP
6330 
6331       -- Map all input AHL Workorder Relationship attributes to the
6332       -- corresponding EAM Workorder Relationship attributes.
6333       AHL_EAM_JOB_PVT.map_ahl_eam_wo_rel_rec
6334       (
6335        p_workorder_rel_rec    => l_workorder_rel_tbl(i),
6336        x_eam_wo_relations_rec => l_eam_wo_relations_tbl(i)
6337       );
6338 
6339       END LOOP;
6340  		 END IF;
6341 
6342     -- Get the Visit Master Workorder
6343     OPEN  get_visit_mwo( p_visit_id );
6344     FETCH get_visit_mwo
6345     INTO  l_workorder_id,
6346           l_object_version_number,
6347           l_status_code,
6348           l_wip_entity_id,
6349 										l_wo_name;
6350 
6351     IF ( get_visit_mwo%NOTFOUND ) THEN
6352       FND_MESSAGE.set_name('AHL','AHL_PRD_VISIT_MWO_NOT_FOUND');
6353       FND_MSG_PUB.add;
6354       CLOSE get_visit_mwo;
6355       RAISE FND_API.G_EXC_ERROR;
6356     END IF;
6357 
6358     CLOSE get_visit_mwo;
6359 
6360 				-- rroy
6361  			-- ACL Changes
6362 				l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_workorder_id,
6363 																																																									p_ue_id => NULL,
6364 																																																									p_visit_id => NULL,
6365 																																																									p_item_instance_id => NULL);
6366 				IF l_return_status = FND_API.G_TRUE THEN
6367 	  			FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_VST_MWO_CNCL_UNTLCKD');
6368 			 		FND_MESSAGE.Set_Token('WO_NAME', l_wo_name);
6369 					 FND_MSG_PUB.ADD;
6370 				 	RAISE FND_API.G_EXC_ERROR;
6371 				END IF;
6372 
6373 				-- rroy
6374 				-- ACL Changes
6375 
6376 
6377     -- Check if the Visit WO is in a status which can be cancelled
6378     IF ( l_status_code = G_JOB_STATUS_COMPLETE OR
6379          l_status_code = G_JOB_STATUS_COMPLETE_NC OR
6380          l_status_code = G_JOB_STATUS_CANCELLED OR
6381          l_status_code = G_JOB_STATUS_CLOSED OR
6382          l_status_code = G_JOB_STATUS_DELETED ) THEN
6383 
6384        --Get status meaning
6385        SELECT meaning INTO l_status_meaning
6386 	   FROM fnd_lookup_values_vl
6387         WHERE lookup_type = 'AHL_JOB_STATUS'
6388           AND LOOKUP_CODE = l_status_code;
6389 		--
6390       FND_MESSAGE.set_name('AHL','AHL_PRD_CANCEL_VISIT_STATUS');
6391       FND_MESSAGE.set_token('STATUS', l_status_meaning );
6392       FND_MSG_PUB.add;
6393       RAISE FND_API.G_EXC_ERROR;
6394     END IF;
6395 
6396     -- Process all the Child Workorders of the Visit
6397     FOR child_csr IN get_child_wos( l_wip_entity_id ) LOOP
6398       /* bug 5104519 - start */
6399       OPEN get_wip_wo(child_csr.child_object_id);
6400       FETCH get_wip_wo INTO l_wo_id, l_ovn, l_sts_code,l_wip_id, l_mwo_flag, l_child_wo_name;
6401       CLOSE get_wip_wo;
6402 
6403       -- If a Child WO needs to be Cancelled add it in the WO Table
6404       IF ( l_sts_code <> G_JOB_STATUS_COMPLETE AND
6405            l_sts_code <> G_JOB_STATUS_COMPLETE_NC AND
6406            l_sts_code <> G_JOB_STATUS_CANCELLED AND
6407            l_sts_code <> G_JOB_STATUS_CLOSED AND
6408            l_sts_code <> G_JOB_STATUS_DELETED ) THEN
6409 								-- rroy
6410 								-- ACL Changes
6411 								l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
6412 																																																									p_ue_id => NULL,
6413 																																																									p_visit_id => NULL,
6414 																																																									p_item_instance_id => NULL);
6415 								IF l_return_status = FND_API.G_TRUE THEN
6416 								IF l_mwo_flag <> 'Y' THEN
6417 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_VST_WO_CNCL_UNTLCKD');
6418 								ELSE
6419 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_VST_MWO_CNCL_UNTLCKD');
6420 								END IF;
6421 										FND_MESSAGE.Set_Token('WO_NAME', l_child_wo_name);
6422 										FND_MSG_PUB.ADD;
6423 										RAISE FND_API.G_EXC_ERROR;
6424 								END IF;
6425 								-- rroy
6426 								-- ACL Changes
6427 
6428  	/*
6429  	 * Balaji added following logic for bug # 5104519(Reopened issue reported by BANARAYA)
6430  	 * EAM has a validation which doesnt allow master workorder to be cancelled when all child
6431  	 * workorders are not in status 7(cancelled),12(closed),14(Pending Close),15(Failed Close).
6432          * Hence master workorders will be post processed after child workorder processing and will
6433          * be cancelled or closed accordingly.
6434  	 */
6435  	/* bug 5104519 - start */
6436         IF l_mwo_flag = 'N' THEN
6437 
6438          --sikumar: added for FP for ER 5571241 -- Check if user has permission to cancel jobs.
6439                 IF AHL_PRD_UTIL_PKG.Is_Wo_Cancel_Allowed(l_wo_id) = FND_API.G_FALSE THEN
6440                   FND_MESSAGE.set_name('AHL','AHL_PRD_CANCEL_NOT_ALLOWED');
6441                   FND_MSG_PUB.add;
6442                   RAISE FND_API.G_EXC_ERROR;
6443                 END IF;
6444 
6445 		l_wo_count := l_wo_count + 1;
6446 		l_workorder_tbl(l_wo_count).dml_operation := 'U';
6447 		l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
6448 		l_workorder_tbl(l_wo_count).header_id := l_wip_id;
6449 		l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
6450 		l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
6451                 l_workorder_tbl(l_wo_count).hold_reason_code := FND_API.G_MISS_CHAR;
6452 
6453 		-- If the Status is Draft, then, Delete else, Cancel
6454 		IF ( l_sts_code = G_JOB_STATUS_DRAFT ) THEN
6455 		  l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_DELETED;
6456 		ELSE
6457 		  l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_CANCELLED;
6458 		END IF;
6459 	ELSE
6460 		l_mwo_count := l_mwo_count + 1;
6461 		l_master_workorder_tbl(l_mwo_count).dml_operation := 'U';
6462 		l_master_workorder_tbl(l_mwo_count).batch_id := l_wip_entity_id;
6463 		l_master_workorder_tbl(l_mwo_count).header_id := l_wip_id;
6464 		l_master_workorder_tbl(l_mwo_count).workorder_id := l_wo_id;
6465 		l_master_workorder_tbl(l_mwo_count).object_version_number := l_ovn;
6466 
6467 		-- If the Status is Draft, then, Delete else, Cancel
6468 		IF ( l_sts_code = G_JOB_STATUS_DRAFT ) THEN
6469 		  l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_DELETED;
6470 		ELSE
6471 		  l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_CANCELLED;
6472 		END IF;
6473 	END IF;
6474 	/* bug 5104519 - end */
6475       END IF;
6476 
6477     END LOOP;
6478     /* bug 5104519 - Start */
6479     -- Add the Visit WO in the WO Table
6480     l_mwo_count := l_mwo_count + 1;
6481     l_master_workorder_tbl(l_mwo_count).dml_operation := 'U';
6482     l_master_workorder_tbl(l_mwo_count).batch_id := l_wip_entity_id;
6483     l_master_workorder_tbl(l_mwo_count).header_id := l_wip_entity_id;
6484     l_master_workorder_tbl(l_mwo_count).workorder_id := l_workorder_id;
6485     l_master_workorder_tbl(l_mwo_count).object_version_number := l_object_version_number;
6486 
6487     -- If the Status is Draft, then, Delete else, Cancel
6488     IF ( l_status_code = G_JOB_STATUS_DRAFT ) THEN
6489       l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_DELETED;
6490     ELSE
6491       l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_CANCELLED;
6492     END IF;
6493 
6494   -- Process UE
6495   ELSIF ( l_input_type = 'UE' ) THEN
6496 
6497     -- Get the UE Master Workorder
6498     OPEN  get_ue_mwo( p_unit_effectivity_id );
6499     FETCH get_ue_mwo
6500     INTO  l_workorder_id,
6501           l_object_version_number,
6502           l_status_code,
6503           l_wip_entity_id,
6504           l_wo_name,
6505           l_master_wo_flag;
6506 
6507     IF ( get_ue_mwo%NOTFOUND ) THEN
6508       FND_MESSAGE.set_name('AHL','AHL_PRD_MR_MWO_NOT_FOUND');
6509       FND_MSG_PUB.add;
6510       CLOSE get_ue_mwo;
6511       RAISE FND_API.G_EXC_ERROR;
6512     END IF;
6513 
6514     CLOSE get_ue_mwo;
6515     -- rroy
6516     -- ACL Changes
6517     l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_workorder_id,
6518                                                        p_ue_id => NULL,
6519                                                        p_visit_id => NULL,
6520                                                        p_item_instance_id => NULL);
6521     IF l_return_status = FND_API.G_TRUE THEN
6522       IF l_master_wo_flag <> 'Y' THEN
6523         FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_WO_CNCL_UNTLCKD');
6524       ELSE
6525         FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_MWO_CNCL_UNTLCKD');
6526       END IF;
6527       FND_MESSAGE.Set_Token('WO_NAME', l_wo_name);
6528       FND_MSG_PUB.ADD;
6529       RAISE FND_API.G_EXC_ERROR;
6530    END IF;
6531    -- rroy
6532    -- ACL Changes
6533 
6534     -- Check if the UE WO is in a status which can be cancelled
6535     IF ( l_status_code = G_JOB_STATUS_COMPLETE OR
6536          l_status_code = G_JOB_STATUS_COMPLETE_NC OR
6537          l_status_code = G_JOB_STATUS_CANCELLED OR
6538          l_status_code = G_JOB_STATUS_CLOSED OR
6539          l_status_code = G_JOB_STATUS_DELETED ) THEN
6540        --Get status meaning
6541        SELECT meaning INTO l_status_meaning
6542 	   FROM fnd_lookup_values_vl
6543         WHERE lookup_type = 'AHL_JOB_STATUS'
6544           AND LOOKUP_CODE = l_status_code;
6545 
6546       FND_MESSAGE.set_name('AHL','AHL_PRD_CANCEL_MR_STATUS');
6547       FND_MESSAGE.set_token('STATUS', l_status_meaning );
6548       FND_MSG_PUB.add;
6549       RAISE FND_API.G_EXC_ERROR;
6550     END IF;
6551 
6552     -- Process all the Child Workorders of the UE
6553     FOR child_csr IN get_child_wos( l_wip_entity_id ) LOOP
6554       /* bug 5104519 - start */
6555       OPEN get_wip_wo(child_csr.child_object_id);
6556       FETCH get_wip_wo INTO l_wo_id, l_ovn, l_sts_code,l_wip_id, l_mwo_flag, l_child_wo_name;
6557       CLOSE get_wip_wo;
6558 
6559       -- If a Child WO needs to be Cancelled add it in the WO Table
6560       IF ( l_sts_code <> G_JOB_STATUS_COMPLETE AND
6561            l_sts_code <> G_JOB_STATUS_COMPLETE_NC AND
6562            l_sts_code <> G_JOB_STATUS_CANCELLED AND
6563            l_sts_code <> G_JOB_STATUS_CLOSED AND
6564            l_sts_code <> G_JOB_STATUS_DELETED ) THEN
6565             -- rroy
6566             -- ACL Changes
6567             l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
6568                                                                p_ue_id => NULL,
6569                                                                p_visit_id => NULL,
6570                                                                p_item_instance_id => NULL);
6571             IF l_return_status = FND_API.G_TRUE THEN
6572               IF l_mwo_flag <> 'Y' THEN
6573                 FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_WO_CNCL_UNTLCKD');
6574               ELSE
6575                 FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_MWO_CNCL_UNTLCKD');
6576               END IF;
6577               FND_MESSAGE.Set_Token('WO_NAME', l_child_wo_name);
6578               FND_MSG_PUB.ADD;
6579               RAISE FND_API.G_EXC_ERROR;
6580             END IF;
6581             -- rroy
6582             -- ACL Changes
6583 
6584 	/*
6585 	 * Balaji added following logic for bug # 5104519(Reopened issue reported by BANARAYA)
6586 	 * EAM has a validation which doesnt allow master workorder to be cancelled when all child
6587 	 * workorders are not in status 7(cancelled),12(closed),14(Pending Close),15(Failed Close).
6588          * Hence master workorders will be post processed after child workorder processing and will
6589          * be cancelled or closed accordingly.
6590 	 */
6591 	/* bug 5104519 - start */
6592         IF l_mwo_flag = 'N' THEN
6593 
6594          --sikumar: added for FP for ER 5571241 -- Check if user has permission to cancel jobs.
6595          IF AHL_PRD_UTIL_PKG.Is_Wo_Cancel_Allowed(l_wo_id) = FND_API.G_FALSE THEN
6596            FND_MESSAGE.set_name('AHL','AHL_PRD_CANCEL_NOT_ALLOWED');
6597            FND_MSG_PUB.add;
6598            RAISE FND_API.G_EXC_ERROR;
6599          END IF;
6600 	   l_wo_count := l_wo_count + 1;
6601 	   l_workorder_tbl(l_wo_count).dml_operation := 'U';
6602 	   l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
6603 	   l_workorder_tbl(l_wo_count).header_id := child_csr.child_object_id;
6604 	   l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
6605 	   l_workorder_tbl(l_wo_count).wip_entity_id := l_wip_id;
6606 	   l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
6607            l_workorder_tbl(l_wo_count).hold_reason_code := FND_API.G_MISS_CHAR;
6608 
6609 	   -- If the Status is Draft, then, Delete else, Cancel
6610 	   IF ( l_sts_code = G_JOB_STATUS_DRAFT ) THEN
6611 	      l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_DELETED;
6612 	   ELSE
6613 	      l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_CANCELLED;
6614 	   END IF;
6615 	ELSE
6616             l_mwo_count := l_mwo_count + 1;
6617             l_master_workorder_tbl(l_mwo_count).dml_operation := 'U';
6618             l_master_workorder_tbl(l_mwo_count).batch_id := l_wip_entity_id;
6619             l_master_workorder_tbl(l_mwo_count).header_id := child_csr.child_object_id;
6620             l_master_workorder_tbl(l_mwo_count).workorder_id := l_wo_id;
6621             l_master_workorder_tbl(l_mwo_count).wip_entity_id := l_wip_id;
6622             l_master_workorder_tbl(l_mwo_count).object_version_number := l_ovn;
6623 
6624             -- If the Status is Draft, then, Delete else, Cancel
6625             IF ( l_sts_code = G_JOB_STATUS_DRAFT ) THEN
6626                l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_DELETED;
6627             ELSE
6628                l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_CANCELLED;
6629             END IF;
6630 	END IF;
6631 	/* bug 5104519 - start */
6632       END IF;
6633 
6634     END LOOP;
6635 
6636     /* bug 5104519 - start */
6637     -- Add the UE WO in the WO Table
6638     l_mwo_count := l_mwo_count + 1;
6639     l_master_workorder_tbl(l_mwo_count).dml_operation := 'U';
6640     l_master_workorder_tbl(l_mwo_count).batch_id := l_wip_entity_id;
6641     l_master_workorder_tbl(l_mwo_count).header_id := l_wip_entity_id;
6642     l_master_workorder_tbl(l_mwo_count).workorder_id := l_workorder_id;
6643     l_master_workorder_tbl(l_mwo_count).wip_entity_id := l_wip_entity_id;
6644     l_master_workorder_tbl(l_mwo_count).object_version_number := l_object_version_number;
6645 
6646     IF ( l_status_code = G_JOB_STATUS_DRAFT OR
6647          l_status_code = G_JOB_STATUS_UNRELEASED ) THEN
6648 
6649       -- Need to Release Parents if they are DRAFT or UNRELEASED so,
6650       -- Release the UE WO which will in-turn release the parent WOs
6651       release_visit_jobs
6652       (
6653         p_api_version            => 1.0,
6654         p_init_msg_list          => FND_API.G_TRUE,
6655         p_commit                 => FND_API.G_FALSE,
6656         p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
6657         p_default                => FND_API.G_FALSE,
6658         p_module_type            => NULL,
6659         x_return_status          => l_return_status,
6660         x_msg_count              => l_msg_count,
6661         x_msg_data               => l_msg_data,
6662         p_visit_id               => NULL,
6663         p_unit_effectivity_id    => NULL,
6664         p_workorder_id           => l_workorder_id
6665       );
6666 
6667       IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
6668         RAISE FND_API.G_EXC_ERROR;
6669       END IF;
6670       -- Release job updates the OVN. Hence requery the record to get the new OVN.
6671       -- Balaji added the fix as a part of BAE OVN Fix.
6672       OPEN  get_ue_mwo( p_unit_effectivity_id );
6673       FETCH get_ue_mwo
6674       INTO  l_workorder_id,
6675             l_object_version_number,
6676             l_status_code,
6677             l_wip_entity_id,
6678             l_wo_name,
6679 	    l_master_wo_flag;
6680       CLOSE get_ue_mwo;
6681 
6682       l_master_workorder_tbl(l_mwo_count).object_version_number := l_object_version_number;
6683 
6684     END IF;
6685 
6686     -- If the Status is Draft, then, Delete else, Cancel
6687     IF ( l_status_code = G_JOB_STATUS_DRAFT ) THEN
6688       l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_DELETED;
6689     ELSE
6690       l_master_workorder_tbl(l_mwo_count).status_code := G_JOB_STATUS_CANCELLED;
6691     END IF;
6692     /* bug 5104519 - end */
6693 
6694     -- Process all the Parent Workorders of the UE
6695     -- Commented following code for not processing the parents any more
6696     /*Start of commented code*/
6697     /*FOR parent_csr IN get_parent_wos( l_wip_entity_id ) LOOP
6698       OPEN get_wip_wo(parent_csr.parent_object_id);
6699       FETCH get_wip_wo INTO l_wo_id, l_ovn, l_sts_code,l_wip_id, l_mwo_flag, l_parent_wo_name;
6700       CLOSE get_wip_wo;
6701 
6702       -- Check if the parent WO needs to be Cancelled
6703       IF ( l_sts_code <> G_JOB_STATUS_COMPLETE AND
6704            l_sts_code <> G_JOB_STATUS_COMPLETE_NC AND
6705            l_sts_code <> G_JOB_STATUS_CANCELLED AND
6706            l_sts_code <> G_JOB_STATUS_CLOSED AND
6707            l_sts_code <> G_JOB_STATUS_DELETED ) THEN
6708 
6709         -- Parent WO can be cancelled only if all the children are cancelled
6710         IF ( are_child_wos_cancelled( parent_csr.parent_object_id, l_workorder_tbl ) ) THEN
6711 								-- rroy
6712 								-- ACL Changes
6713 								l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
6714 																																																									p_ue_id => NULL,
6715 																																																									p_visit_id => NULL,
6716 																																																									p_item_instance_id => NULL);
6717 								IF l_return_status = FND_API.G_TRUE THEN
6718 								IF l_mwo_flag <> 'Y' THEN
6719 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_WO_CNCL_UNTLCKD');
6720 								ELSE
6721 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MR_MWO_CNCL_UNTLCKD');
6722 								END IF;
6723 										FND_MESSAGE.Set_Token('WO_NAME', l_parent_wo_name);
6724 										FND_MSG_PUB.ADD;
6725 										RAISE FND_API.G_EXC_ERROR;
6726 								END IF;
6727 								-- rroy
6728 								-- ACL Changes
6729 
6730           l_wo_count := l_wo_count + 1;
6731           l_workorder_tbl(l_wo_count).dml_operation := 'U';
6732           l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
6733           l_workorder_tbl(l_wo_count).header_id := parent_csr.parent_object_id;
6734           l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
6735           l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
6736           l_workorder_tbl(l_wo_count).wip_entity_id := l_wip_id;
6737 
6738           -- If the Status is Draft, then, Delete else, Cancel
6739           IF ( l_sts_code = G_JOB_STATUS_DRAFT ) THEN
6740             l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_DELETED;
6741           ELSE
6742             l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_CANCELLED;
6743           END IF;
6744         ELSE
6745           -- No more parents can be cancelled
6746           EXIT;
6747         END IF;
6748 
6749       END IF;
6750 
6751     END LOOP;*/
6752     /*End of commented code*/
6753 				-- adding the relationships to be deleted
6754 				-- delete only those relationships which contain
6755 				-- the ue
6756 				-- 1. first, all the child dependencies of the ue mwo need to be deleted
6757 				idx := 1;
6758 				OPEN get_ue_wo(p_unit_effectivity_id);
6759 				FETCH get_ue_wo INTO l_ue_wip_entity_id;
6760 				CLOSE get_ue_wo;
6761 
6762 				FOR com_dep_rec IN get_completion_dep_wo_all(l_ue_wip_entity_id) LOOP
6763 								l_workorder_rel_tbl(idx).wo_relationship_id := com_dep_rec.sched_relationship_id;
6764 								l_workorder_rel_tbl(idx).batch_id := l_ue_wip_entity_id;
6765 								l_workorder_rel_tbl(idx).parent_header_id := idx;
6766 								l_workorder_rel_tbl(idx).child_header_id := idx;
6767 								l_workorder_rel_tbl(idx).parent_wip_entity_id := com_dep_rec.parent_object_id;
6768 								l_workorder_rel_tbl(idx).child_wip_entity_id := com_dep_rec.child_object_id;
6769 								l_workorder_rel_tbl(idx).relationship_type := 2;
6770 								l_workorder_rel_tbl(idx).dml_operation := 'D';
6771 								idx := idx + 1;
6772 				END LOOP;
6773 
6774 				FOR com_dep_rec IN get_immediate_ue_parent(l_ue_wip_entity_id) LOOP
6775 								l_workorder_rel_tbl(idx).wo_relationship_id := com_dep_rec.sched_relationship_id;
6776 								l_workorder_rel_tbl(idx).batch_id := l_ue_wip_entity_id;
6777 								l_workorder_rel_tbl(idx).parent_header_id := idx;
6778 								l_workorder_rel_tbl(idx).child_header_id := idx;
6779 								l_workorder_rel_tbl(idx).parent_wip_entity_id := com_dep_rec.parent_object_id;
6780 								l_workorder_rel_tbl(idx).child_wip_entity_id := l_ue_wip_entity_id;
6781 								l_workorder_rel_tbl(idx).relationship_type := 2;
6782 								l_workorder_rel_tbl(idx).dml_operation := 'D';
6783 
6784 								idx := idx + 1;
6785 				END LOOP;
6786 				-- 2. second, get all the parents of the ue
6787 				FOR ue_parent_rec IN get_ue_completion_parents(l_ue_wip_entity_id) LOOP
6788 				-- 3. for each parent, check if it is in the list of ue wos to be cancelled
6789 				   FOR i in l_workorder_tbl.FIRST..l_workorder_tbl.LAST LOOP
6790 							  IF l_workorder_tbl(i).wip_entity_id = ue_parent_rec.parent_object_id THEN
6791 									-- This parent is in the list of wos to be cancelled
6792 									-- therefore we need to delete all its child dependencies
6793 											FOR com_dep_rec IN get_completion_dep_wo_all(ue_parent_rec.parent_object_id) LOOP
6794 													-- if the relationship id does not exist already, then
6795 													-- add it to the relationships table
6796 													l_rel_found := FALSE;
6797 													IF l_workorder_rel_tbl.COUNT > 0 THEN
6798 													FOR j in l_workorder_rel_tbl.FIRST..l_workorder_rel_tbl.LAST LOOP
6799 													  IF l_workorder_rel_tbl(j).wo_relationship_id = com_dep_rec.sched_relationship_id THEN
6800 															  l_rel_found := TRUE;
6801 																	EXIT;
6802 															END IF;
6803 													END LOOP;
6804 													END IF;
6805 													IF l_rel_found = FALSE THEN
6806                -- if relationship does not exist
6807 															-- then add it to the relationships table
6808 															l_workorder_rel_tbl(idx).wo_relationship_id := com_dep_rec.sched_relationship_id;
6809 															l_workorder_rel_tbl(idx).batch_id := l_ue_wip_entity_id;
6810 															l_workorder_rel_tbl(idx).parent_header_id := idx;
6811 															l_workorder_rel_tbl(idx).child_header_id := idx;
6812 															l_workorder_rel_tbl(idx).parent_wip_entity_id := com_dep_rec.parent_object_id;
6813 															l_workorder_rel_tbl(idx).child_wip_entity_id := com_dep_rec.child_object_id;
6814 															l_workorder_rel_tbl(idx).relationship_type := 2;
6815 															l_workorder_rel_tbl(idx).dml_operation := 'D';
6816 															idx := idx + 1;
6817 													END IF;
6818 											END LOOP;
6819 											-- after deleting all the child dependencies
6820 											-- delete its immediate parent dependency
6821 											FOR imm_parents_rec IN get_immediate_ue_parent(ue_parent_rec.parent_object_id) LOOP
6822 															l_workorder_rel_tbl(idx).wo_relationship_id := imm_parents_rec.sched_relationship_id;
6823 															l_workorder_rel_tbl(idx).batch_id := l_ue_wip_entity_id;
6824 															l_workorder_rel_tbl(idx).parent_header_id := idx;
6825 															l_workorder_rel_tbl(idx).child_header_id := idx;
6826 															l_workorder_rel_tbl(idx).parent_wip_entity_id := imm_parents_rec.parent_object_id;
6827 															l_workorder_rel_tbl(idx).child_wip_entity_id := ue_parent_rec.parent_object_id;
6828 															l_workorder_rel_tbl(idx).relationship_type := 2;
6829 															l_workorder_rel_tbl(idx).dml_operation := 'D';
6830 															idx := idx + 1;
6831 											END LOOP;
6832 
6833 									END IF;
6834 							END LOOP;
6835 				END LOOP;
6836 
6837 
6838   				IF ( l_workorder_rel_tbl.COUNT > 0 ) THEN
6839     		FOR i IN l_workorder_rel_tbl.FIRST..l_workorder_rel_tbl.LAST LOOP
6840 
6841       -- Map all input AHL Workorder Relationship attributes to the
6842       -- corresponding EAM Workorder Relationship attributes.
6843       AHL_EAM_JOB_PVT.map_ahl_eam_wo_rel_rec
6844       (
6845        p_workorder_rel_rec    => l_workorder_rel_tbl(i),
6846        x_eam_wo_relations_rec => l_eam_wo_relations_tbl(i)
6847       );
6848 
6849 
6850 
6851       END LOOP;
6852  		 END IF;
6853 
6854   -- Process Workorder
6855   ELSIF ( l_input_type = 'WO' ) THEN
6856 
6857     -- Get the Workorder
6858     OPEN  get_wo( p_workorder_id );
6859     FETCH get_wo
6860     INTO  l_workorder_id,
6861           l_object_version_number,
6862           l_status_code,
6863           l_wip_entity_id,
6864 										l_wo_name;
6865 
6866     IF ( get_wo%NOTFOUND ) THEN
6867       FND_MESSAGE.set_name('AHL','AHL_PRD_WO_NOT_FOUND');
6868       FND_MSG_PUB.add;
6869       CLOSE get_wo;
6870       RAISE FND_API.G_EXC_ERROR;
6871     END IF;
6872 				-- rroy
6873 				-- ACL Changes
6874 				l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_workorder_id,
6875 																																																									p_ue_id => NULL,
6876 																																																									p_visit_id => NULL,
6877 																																																									p_item_instance_id => NULL);
6878 				IF l_return_status = FND_API.G_TRUE THEN
6879 						FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_WO_CNCL_UNTLCKD');
6880 						FND_MESSAGE.Set_Token('WO_NAME', l_wo_name);
6881 						FND_MSG_PUB.ADD;
6882 						RAISE FND_API.G_EXC_ERROR;
6883 				END IF;
6884 				-- rroy
6885 				-- ACL Changes
6886 
6887     CLOSE get_wo;
6888 
6889     --sikumar: added for FP for ER 5571241 -- Check if user has permission to cancel jobs.
6890     IF AHL_PRD_UTIL_PKG.Is_Wo_Cancel_Allowed(p_workorder_id) = FND_API.G_FALSE THEN
6891        FND_MESSAGE.set_name('AHL','AHL_PRD_CANCEL_NOT_ALLOWED');
6892        FND_MSG_PUB.add;
6893        RAISE FND_API.G_EXC_ERROR;
6894     END IF;
6895 
6896     -- Check if the WO is in a status which can be cancelled
6897     IF ( l_status_code = G_JOB_STATUS_COMPLETE OR
6898          l_status_code = G_JOB_STATUS_COMPLETE_NC OR
6899          l_status_code = G_JOB_STATUS_CANCELLED OR
6900          l_status_code = G_JOB_STATUS_CLOSED OR
6901          l_status_code = G_JOB_STATUS_DELETED ) THEN
6902        --Get status meaning
6903        SELECT meaning INTO l_status_meaning
6904 	   FROM fnd_lookup_values_vl
6905         WHERE lookup_type = 'AHL_JOB_STATUS'
6906           AND LOOKUP_CODE = l_status_code;
6907 
6908       FND_MESSAGE.set_name('AHL','AHL_PRD_CANCEL_WO_STATUS');
6909       FND_MESSAGE.set_token('STATUS', l_status_meaning );
6910       FND_MSG_PUB.add;
6911       RAISE FND_API.G_EXC_ERROR;
6912     END IF;
6913 
6914     -- Add the WO in the WO Table
6915     l_wo_count := l_wo_count + 1;
6916     l_workorder_tbl(l_wo_count).dml_operation := 'U';
6917     l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
6918     l_workorder_tbl(l_wo_count).header_id := l_wip_entity_id;
6919     l_workorder_tbl(l_wo_count).workorder_id := l_workorder_id;
6920     l_workorder_tbl(l_wo_count).object_version_number := l_object_version_number;
6921 
6922     IF ( l_status_code = G_JOB_STATUS_DRAFT OR
6923          l_status_code = G_JOB_STATUS_UNRELEASED ) THEN
6924 
6925       -- Need to Release Parents if they are DRAFT or UNRELEASED so,
6926       -- Release the WO which will in-turn release the parent WOs
6927       release_visit_jobs
6928       (
6929         p_api_version            => 1.0,
6930         p_init_msg_list          => FND_API.G_TRUE,
6931         p_commit                 => FND_API.G_FALSE,
6932         p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
6933         p_default                => FND_API.G_FALSE,
6934         p_module_type            => NULL,
6935         x_return_status          => l_return_status,
6936         x_msg_count              => l_msg_count,
6937         x_msg_data               => l_msg_data,
6938         p_visit_id               => NULL,
6939         p_unit_effectivity_id    => NULL,
6940         p_workorder_id           => l_workorder_id
6941       );
6942 
6943       IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
6944         RAISE FND_API.G_EXC_ERROR;
6945       END IF;
6946       -- Release job updates the OVN. Hence requery the record to get the new OVN.
6947       -- Balaji added the fix as a part of BAE OVN Fix.
6948       OPEN  get_wo( p_workorder_id );
6949       FETCH get_wo
6950       INTO  l_workorder_id,
6951             l_object_version_number,
6952             l_status_code,
6953             l_wip_entity_id,
6954             l_wo_name;
6955       CLOSE get_wo;
6956 
6957       l_workorder_tbl(l_wo_count).object_version_number := l_object_version_number;
6958 
6959     END IF;
6960 
6961     -- If the Status is Draft, then, Delete else, Cancel
6962     IF ( l_status_code = G_JOB_STATUS_DRAFT ) THEN
6963       l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_DELETED;
6964     ELSE
6965       l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_CANCELLED;
6966     END IF;
6967 
6968     -- Process all the Parent Workorders of the WO
6969     -- Commented following code for not processing the parents any more
6970     /*Start of commented code*/
6971     /*
6972     FOR parent_csr IN get_parent_wos( l_wip_entity_id ) LOOP
6973       OPEN get_wip_wo(parent_csr.parent_object_id);
6974       FETCH get_wip_wo INTO l_wo_id, l_ovn, l_sts_code,l_wip_id, l_mwo_flag, l_parent_wo_name;
6975       CLOSE get_wip_wo;
6976 
6977       -- Check if the parent WO needs to be Cancelled
6978       IF ( l_sts_code <> G_JOB_STATUS_COMPLETE AND
6979            l_sts_code <> G_JOB_STATUS_COMPLETE_NC AND
6980            l_sts_code <> G_JOB_STATUS_CANCELLED AND
6981            l_sts_code <> G_JOB_STATUS_CLOSED AND
6982            l_sts_code <> G_JOB_STATUS_DELETED ) THEN
6983 
6984         -- Parent WO can be cancelled only if all the children are cancelled
6985         IF ( are_child_wos_cancelled( parent_csr.parent_object_id, l_workorder_tbl ) ) THEN
6986 								-- rroy
6987 								-- ACL Changes
6988 								l_return_status := AHL_PRD_UTIL_PKG.Is_Unit_Locked(p_workorder_id => l_wo_id,
6989 																																																									p_ue_id => NULL,
6990 																																																									p_visit_id => NULL,
6991 																																																									p_item_instance_id => NULL);
6992 								IF l_return_status = FND_API.G_TRUE THEN
6993 								IF l_mwo_flag <> 'Y' THEN
6994 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_WO_CNCL_UNTLCKD');
6995 								ELSE
6996 										FND_MESSAGE.Set_Name('AHL', 'AHL_PRD_MWO_CNCL_UNTLCKD');
6997 								END IF;
6998 										FND_MESSAGE.Set_Token('WO_NAME', l_parent_wo_name);
6999 										FND_MSG_PUB.ADD;
7000 										RAISE FND_API.G_EXC_ERROR;
7001 								END IF;
7002 								-- rroy
7003 								-- ACL Changes
7004 
7005           l_wo_count := l_wo_count + 1;
7006           l_workorder_tbl(l_wo_count).dml_operation := 'U';
7007           l_workorder_tbl(l_wo_count).batch_id := l_wip_entity_id;
7008           l_workorder_tbl(l_wo_count).header_id := parent_csr.parent_object_id;
7009           l_workorder_tbl(l_wo_count).workorder_id := l_wo_id;
7010           l_workorder_tbl(l_wo_count).object_version_number := l_ovn;
7011 
7012           -- If the Status is Draft, then, Delete else, Cancel
7013           IF ( l_sts_code = G_JOB_STATUS_DRAFT ) THEN
7014             l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_DELETED;
7015           ELSE
7016             l_workorder_tbl(l_wo_count).status_code := G_JOB_STATUS_CANCELLED;
7017           END IF;
7018         ELSE
7019           -- No more parents can be cancelled
7020           EXIT;
7021         END IF;
7022 
7023       END IF;
7024 
7025     END LOOP;*/
7026     /*End of commented code*/				-- bug 4094884
7027 				-- need to delete all completion dependencies
7028 				-- if the l_input_type = 'WO'
7029 				-- so all the completion dependencies for the
7030 				-- particular workorder need to be cancelled
7031 				  idx := 1;
7032 						-- first get all the relationships where this workorder is the parent
7033 				  FOR com_dep_rec IN get_completion_dep_wo(l_wip_entity_id) LOOP
7034 								l_workorder_rel_tbl(idx).wo_relationship_id := com_dep_rec.sched_relationship_id;
7035 								l_workorder_rel_tbl(idx).batch_id := l_wip_entity_id;
7036 								l_workorder_rel_tbl(idx).parent_header_id := idx;
7037 								l_workorder_rel_tbl(idx).child_header_id := idx;
7038 								l_workorder_rel_tbl(idx).parent_wip_entity_id := l_wip_entity_id;
7039 								l_workorder_rel_tbl(idx).child_wip_entity_id := com_dep_rec.child_object_id;
7040 								l_workorder_rel_tbl(idx).relationship_type := 2;
7041 								l_workorder_rel_tbl(idx).dml_operation := 'D';
7042 								idx := idx + 1;
7043 
7044 						END LOOP;
7045 						-- second, get all the relationships where this workorder is the child
7046 						FOR com_dep_rec IN get_completion_dep_wo_child(l_wip_entity_id) LOOP
7047 								l_workorder_rel_tbl(idx).wo_relationship_id := com_dep_rec.sched_relationship_id;
7048 								l_workorder_rel_tbl(idx).batch_id := l_wip_entity_id;
7049 								l_workorder_rel_tbl(idx).parent_header_id := idx;
7050 								l_workorder_rel_tbl(idx).child_header_id := idx;
7051 								l_workorder_rel_tbl(idx).parent_wip_entity_id := com_dep_rec.parent_object_id;
7052 								l_workorder_rel_tbl(idx).child_wip_entity_id := l_wip_entity_id;
7053 								l_workorder_rel_tbl(idx).relationship_type := 2;
7054 								l_workorder_rel_tbl(idx).dml_operation := 'D';
7055 								idx := idx + 1;
7056 
7057 						END LOOP;
7058 
7059   				IF ( l_workorder_rel_tbl.COUNT > 0 ) THEN
7060     		FOR i IN l_workorder_rel_tbl.FIRST..l_workorder_rel_tbl.LAST LOOP
7061 
7062       -- Map all input AHL Workorder Relationship attributes to the
7063       -- corresponding EAM Workorder Relationship attributes.
7064       AHL_EAM_JOB_PVT.map_ahl_eam_wo_rel_rec
7065       (
7066        p_workorder_rel_rec    => l_workorder_rel_tbl(i),
7067        x_eam_wo_relations_rec => l_eam_wo_relations_tbl(i)
7068       );
7069 
7070       END LOOP;
7071  		 END IF;
7072 
7073   END IF;
7074 		  -- Bug 4094884
7075 				-- Before cancelling the workorders
7076 				-- delete the workorder completion dependencies
7077 				IF l_eam_wo_relations_tbl.COUNT > 0 THEN
7078 				AHL_EAM_JOB_PVT.process_eam_workorders
7079   		(
7080      p_api_version            => 1.0,
7081      p_init_msg_list          => FND_API.G_TRUE,
7082      p_commit                 => FND_API.G_FALSE,
7083      p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
7084      p_default                => FND_API.G_FALSE,
7085      p_module_type            => NULL,
7086      x_return_status          => l_return_status,
7087      x_msg_count              => l_msg_count,
7088      x_msg_data               => l_msg_data,
7089      p_x_eam_wo_tbl           => l_eam_wo_tbl,
7090      p_eam_wo_relations_tbl   => l_eam_wo_relations_tbl,
7091      p_eam_op_tbl             => l_eam_op_tbl,
7092      p_eam_res_req_tbl        => l_eam_res_req_tbl,
7093      p_eam_mat_req_tbl        => l_eam_mat_req_tbl
7094   		);
7095 
7096  			IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
7097     		RAISE FND_API.G_EXC_ERROR;
7098 				END IF;
7099 				END IF;
7100 
7101 
7102   -- Invoke Process Jobs API to perform the WO Cancellation
7103   IF ( l_wo_count > 0 ) THEN
7104     process_jobs
7105     (
7106       p_api_version            => 1.0,
7107       p_init_msg_list          => FND_API.G_TRUE,
7108       p_commit                 => FND_API.G_FALSE,
7109       p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
7110       p_default                => FND_API.G_FALSE,
7111       p_module_type            => NULL,
7112       x_return_status          => l_return_status,
7113       x_msg_count              => l_msg_count,
7114       x_msg_data               => l_msg_data,
7115       p_x_prd_workorder_tbl    => l_workorder_tbl,
7116       p_prd_workorder_rel_tbl  => l_workorder_rel_cancel_tbl
7117 					);
7118 
7119     IF ( l_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
7120       RAISE FND_API.G_EXC_ERROR;
7121     END IF;
7122   END IF;
7123 
7124   /* bug 5104519 - Start */
7125  /*
7126   * Balaji added following logic for bug # 5104519(Reopened issue reported by BANARAYA)
7127   * EAM has a validation which doesnt allow master workorder to be cancelled when all child
7128   * workorders are not in status 7(cancelled),12(closed),14(Pending Close),15(Failed Close).
7129   * Hence master workorders will be post processed after child workorder processing and will
7130   * be cancelled or closed accordingly as below.
7131   */
7132   -- All child workorders are processed.Now process parent workorders in order.
7133   IF l_mwo_count > 0
7134   THEN
7135      FOR l_count IN l_master_workorder_tbl.FIRST .. l_master_workorder_tbl.LAST
7136      LOOP
7137                 OPEN chk_cmplt_wo_exists(l_master_workorder_tbl(l_count).wip_entity_id);
7138                 FETCH chk_cmplt_wo_exists INTO l_exists;
7139 		CLOSE chk_cmplt_wo_exists;
7140                 /*
7141                  * If All jobs under a MWO are cancelled(or valid statuses after that)
7142                  * then cancel MWO else complete Master WO.
7143                  */
7144                 IF l_exists IS NULL THEN
7145 		        IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7146 				fnd_log.string
7147 				(
7148 					fnd_log.level_statement,
7149 					'ahl.AHL_PRD_WORKORDER_PVT.Cancel_Visit_Jobs',
7150 					'cancelling mwo->'||l_master_workorder_tbl(l_count).workorder_id
7151 				);
7152 		        END IF;
7153 		        -- Bug # 6815689 (FP for Bug # 68156890) -- start
7154 		        l_copy_mwo_tbl(1) := null;
7155 		        -- Bug # 6815689 (FP for Bug # 68156890) -- end
7156 
7157 		        l_copy_mwo_tbl(1).dml_operation := l_master_workorder_tbl(l_count).dml_operation;
7158 		        l_copy_mwo_tbl(1).batch_id := l_master_workorder_tbl(l_count).batch_id;
7159 		        l_copy_mwo_tbl(1).header_id := l_master_workorder_tbl(l_count).header_id;
7160 		        l_copy_mwo_tbl(1).workorder_id := l_master_workorder_tbl(l_count).workorder_id;
7161 		        l_copy_mwo_tbl(1).wip_entity_id := l_master_workorder_tbl(l_count).wip_entity_id;
7162 		        l_copy_mwo_tbl(1).object_version_number := l_master_workorder_tbl(l_count).object_version_number;
7163 		        l_copy_mwo_tbl(1).status_code := l_master_workorder_tbl(l_count).status_code;
7164 
7165 			process_jobs
7166 			(
7167 			   p_api_version            => 1.0,
7168 			   p_init_msg_list          => FND_API.G_TRUE,
7169 			   p_commit                 => FND_API.G_FALSE,
7170 			   p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
7171 			   p_default                => FND_API.G_FALSE,
7172 			   p_module_type            => NULL,
7173 			   x_return_status          => l_return_status,
7174 			   x_msg_count              => l_msg_count,
7175 			   x_msg_data               => l_msg_data,
7176 			   p_x_prd_workorder_tbl    => l_copy_mwo_tbl,
7177 			   p_prd_workorder_rel_tbl  => l_workorder_rel_cancel_tbl
7178 			);
7179 
7180 			IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7181 				fnd_log.string
7182 				(
7183 					fnd_log.level_statement,
7184 					'ahl.AHL_PRD_WORKORDER_PVT.Cancel_Visit_Jobs',
7185 					'return status after calling complete_master_wo: ' || l_mwo_return_status
7186 				);
7187 			END IF;
7188 
7189 			IF l_return_status = FND_API.G_RET_STS_ERROR THEN
7190 				RAISE FND_API.G_EXC_ERROR;
7191 			ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7192 				RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7193 			END IF;
7194 
7195 		ELSE
7196 		        IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7197 				fnd_log.string
7198 				(
7199 					fnd_log.level_statement,
7200 					'ahl.AHL_PRD_WORKORDER_PVT.Cancel_Visit_Jobs',
7201 					'completing mwo->'||l_master_workorder_tbl(l_count).workorder_id
7202 				);
7203 		         END IF;
7204 
7205 			 l_mwo_return_status := AHL_COMPLETIONS_PVT.complete_master_wo(
7206 				 p_visit_id	=>	null,
7207 				 p_workorder_id	=>	l_master_workorder_tbl(l_count).workorder_id,
7208 				 p_ue_id	=>	null
7209 			 );
7210 
7211 			 IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7212 				fnd_log.string
7213 				(
7214 					fnd_log.level_statement,
7215 					'ahl.AHL_PRD_WORKORDER_PVT.Cancel_Visit_Jobs',
7216 					'return status after calling complete_master_wo: ' || l_mwo_return_status
7217 				);
7218 			 END IF;
7219 
7220 			 IF l_mwo_return_status = FND_API.G_RET_STS_ERROR THEN
7221 				RAISE FND_API.G_EXC_ERROR;
7222 			 ELSIF l_mwo_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7223 				RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7224 			 END IF;
7225 
7226 		END IF;
7227      END LOOP;
7228   END IF;
7229   /* bug 5104519 - End */
7230   -- Post-Process Inputs for Cancelling Un-Planned MRs in UMP
7231   -- This step is not required while cancelling WOs during Defferal
7232   /*
7233    * As per the mail from Shailaja dated 20-Apr-2005, Cancel Visit Jobs and Cancel MR Jobs will
7234    * not update the UE Status to 'CANCELLED'. Hence Balaji commented following portion of code
7235    * which updates the UE status. Reference bug #s 4095002 and 4094884.
7236    */
7237   /*
7238   IF ( NVL( p_module_type, 'X' ) <> 'DF' ) THEN
7239 
7240     -- Process Visit
7241     IF ( l_input_type = 'VST' ) THEN
7242 
7243       -- Get all the Unplanned Top UEs in the Visit
7244       FOR mr_csr IN get_visit_mrs( p_visit_id ) LOOP
7245 
7246         l_status_code := AHL_COMPLETIONS_PVT.get_mr_status( mr_csr.unit_effectivity_id );
7247         -- UEs can be cancelled only if all it's WOs are cancelled
7248         IF ( l_status_code = G_MR_STATUS_JOBS_CANCELLED ) THEN
7249           l_ue_count := l_ue_count + 1;
7250           l_unit_effectivity_tbl(l_ue_count).unit_effectivity_id := mr_csr.unit_effectivity_id;
7251           l_unit_effectivity_tbl(l_ue_count).object_version_number := mr_csr.object_version_number;
7252           l_unit_effectivity_tbl(l_ue_count).status_code := G_MR_STATUS_CANCELLED;
7253         END IF;
7254 
7255       END LOOP;
7256 
7257     -- Process UE
7258     ELSIF ( l_input_type = 'UE' ) THEN
7259 
7260       -- Get the Unplanned UE Details
7261       OPEN  get_ue_details( p_unit_effectivity_id );
7262       FETCH get_ue_details
7263       INTO  l_object_version_number;
7264 
7265       IF ( get_ue_details%FOUND ) THEN
7266         CLOSE get_ue_details;
7267 
7268         l_status_code := AHL_COMPLETIONS_PVT.get_mr_status( p_unit_effectivity_id );
7269         -- UEs can be cancelled only if all it's WOs are cancelled
7270         IF ( l_status_code = G_MR_STATUS_JOBS_CANCELLED ) THEN
7271           l_ue_count := l_ue_count + 1;
7272           l_unit_effectivity_tbl(l_ue_count).unit_effectivity_id := p_unit_effectivity_id;
7273           l_unit_effectivity_tbl(l_ue_count).object_version_number := l_object_version_number;
7274           l_unit_effectivity_tbl(l_ue_count).status_code := G_MR_STATUS_CANCELLED;
7275         END IF;
7276 
7277         -- Get the Parent Unplanned UEs for the given UE
7278         FOR parent_csr IN get_parent_ues( p_unit_effectivity_id ) LOOP
7279           OPEN get_ue_details(parent_csr.ue_id);
7280 	         FETCH get_ue_details INTO l_ovn;
7281        	  CLOSE get_ue_details;
7282 
7283           l_status_code := AHL_COMPLETIONS_PVT.get_mr_status( parent_csr.ue_id );
7284 
7285           -- UEs can be cancelled only if all it's WOs are cancelled
7286           IF ( l_status_code = G_MR_STATUS_JOBS_CANCELLED ) THEN
7287             l_ue_count := l_ue_count + 1;
7288             l_unit_effectivity_tbl(l_ue_count).unit_effectivity_id := parent_csr.ue_id;
7289             l_unit_effectivity_tbl(l_ue_count).object_version_number := l_ovn;
7290             l_unit_effectivity_tbl(l_ue_count).status_code := G_MR_STATUS_CANCELLED;
7291           END IF;
7292 
7293         END LOOP;
7294       ELSE
7295 						  CLOSE get_ue_details;
7296       END IF;
7297 
7298 
7299     -- Process WO
7300     ELSIF ( l_input_type = 'WO' ) THEN
7301 
7302       -- Get the UE Details for WO
7303       OPEN  get_ue_details_for_wo( p_workorder_id );
7304       FETCH get_ue_details_for_wo
7305       INTO  l_unit_effectivity_id,
7306             l_object_version_number;
7307 
7308       IF ( get_ue_details_for_wo%FOUND ) THEN
7309         l_status_code := AHL_COMPLETIONS_PVT.get_mr_status( l_unit_effectivity_id );
7310         -- UEs can be cancelled only if all it's WOs are cancelled
7311         IF ( l_status_code = G_MR_STATUS_JOBS_CANCELLED ) THEN
7312           l_ue_count := l_ue_count + 1;
7313           l_unit_effectivity_tbl(l_ue_count).unit_effectivity_id := l_unit_effectivity_id;
7314           l_unit_effectivity_tbl(l_ue_count).object_version_number := l_object_version_number;
7315           l_unit_effectivity_tbl(l_ue_count).status_code := G_MR_STATUS_CANCELLED;
7316         END IF;
7317 
7318         -- Get the Parent Unplanned UEs for the UE associated to WO
7319         FOR parent_csr IN get_parent_ues( l_unit_effectivity_id ) LOOP
7320           OPEN get_ue_details(parent_csr.ue_id);
7321 	  FETCH get_ue_details INTO l_ovn;
7322 	  CLOSE get_ue_details;
7323 
7324           l_status_code := AHL_COMPLETIONS_PVT.get_mr_status( parent_csr.ue_id );
7325 
7326           -- UEs can be cancelled only if all it's WOs are cancelled
7327           IF ( l_status_code = G_MR_STATUS_JOBS_CANCELLED ) THEN
7328             l_ue_count := l_ue_count + 1;
7329             l_unit_effectivity_tbl(l_ue_count).unit_effectivity_id := parent_csr.ue_id;
7330             l_unit_effectivity_tbl(l_ue_count).object_version_number := l_ovn;
7331             l_unit_effectivity_tbl(l_ue_count).status_code := G_MR_STATUS_CANCELLED;
7332           END IF;
7333 
7334         END LOOP;
7335 
7336       END IF;
7337 
7338       CLOSE get_ue_details_for_wo;
7339 
7340     END IF;
7341 
7342   END IF;
7343 
7344   -- Cancel Unplanned MRs in UMP
7345   IF ( l_unit_effectivity_tbl.COUNT > 0 ) THEN
7346 
7347     AHL_UMP_UNITMAINT_PVT.capture_mr_updates
7348     (
7349       p_api_version           => 1.0,
7350       p_init_msg_list         => FND_API.G_TRUE,
7351       p_commit                => FND_API.G_FALSE,
7352       p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
7353       p_default               => FND_API.G_TRUE,
7354       p_module_type           => NULL,
7355       p_unit_effectivity_tbl  => l_unit_effectivity_tbl,
7356       p_x_unit_threshold_tbl  => l_unit_threshold_tbl,
7357       p_x_unit_accomplish_tbl => l_unit_accomplish_tbl,
7358       x_return_status         => l_return_status,
7359       x_msg_count             => l_msg_count,
7360       x_msg_data              => l_msg_data
7361     );
7362 
7363     IF ( l_return_status = FND_API.G_RET_STS_ERROR ) THEN
7364       -- Add Debug
7365       IF FND_API.to_boolean(p_init_msg_list) THEN
7366         FND_MSG_PUB.initialize;
7367       END IF;
7368     END IF;
7369   END IF;
7370   */
7371   IF FND_API.to_boolean(p_commit) THEN
7372     COMMIT;
7373   END IF;
7374 
7375   IF G_DEBUG='Y' THEN
7376     AHL_DEBUG_PUB.disable_debug;
7377   END IF;
7378 
7379 EXCEPTION
7380  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7381     ROLLBACK TO cancel_visit_jobs_PVT;
7382     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7383     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
7384                                p_count => x_msg_count,
7385                                p_data  => x_msg_data);
7386  WHEN FND_API.G_EXC_ERROR THEN
7387     ROLLBACK TO cancel_visit_jobs_PVT;
7388     x_return_status := FND_API.G_RET_STS_ERROR;
7389     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
7390                                p_count => x_msg_count,
7391                                p_data  => x_msg_data);
7392 
7393  WHEN OTHERS THEN
7394     ROLLBACK TO cancel_visit_jobs_PVT;
7395     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7396     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
7397       FND_MSG_PUB.add_exc_msg(p_pkg_name        =>g_pkg_name,
7398                               p_procedure_name  =>l_api_name,
7399                               p_error_text      => SUBSTR(SQLERRM,1,240));
7400 
7401     END IF;
7402     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
7403                                p_count => x_msg_count,
7404                                p_data  => x_msg_data);
7405 END cancel_visit_jobs;
7406 --
7407 PROCEDURE Reschedule_Visit_Jobs
7408 (
7409   p_api_version          IN  NUMBER    := 1.0 ,
7410   p_init_msg_list        IN  VARCHAR2  :=  FND_API.G_TRUE,
7411   p_commit               IN  VARCHAR2  :=  FND_API.G_FALSE,
7412   p_validation_level     IN  NUMBER    :=  FND_API.G_VALID_LEVEL_FULL,
7413   p_default              IN  VARCHAR2   := FND_API.G_FALSE,
7414   p_module_type          IN  VARCHAR2  := Null,
7415   x_return_status        OUT NOCOPY VARCHAR2,
7416   x_msg_count            OUT NOCOPY NUMBER,
7417   x_msg_data             OUT NOCOPY VARCHAR2,
7418   p_visit_id             IN  NUMBER,
7419   p_x_scheduled_start_date  IN OUT NOCOPY DATE,
7420   p_x_scheduled_end_date   IN OUT NOCOPY DATE
7421 )
7422 
7423 AS
7424 
7425   l_api_name     CONSTANT VARCHAR2(30) := 'Reschedule_Visit_Jobs';
7426   l_api_version  CONSTANT NUMBER       := 1.0;
7427   l_msg_count             NUMBER;
7428   l_msg_data              VARCHAR2(2000);
7429   l_return_status         VARCHAR2(1);
7430   l_dummy                 NUMBER;
7431   l_work_order_id         NUMBER;
7432   l_status_code           NUMBER;
7433   l_scheduled_start_date  DATE ;
7434   l_scheduled_end_date    DATE ;
7435 
7436   l_offset                NUMBER ;
7437   l_offset_direction      NUMBER := 1;
7438   l_schedule_method       NUMBER := 1;
7439   l_ignore_firm_flag      VARCHAR2(1) := 'Y';
7440 
7441   l_object_type_id        NUMBER ;
7442 
7443   l_debug                    VARCHAR2(1)  := 'N';
7444   l_output_dir               VARCHAR2(80);
7445   l_debug_filename           VARCHAR2(80);
7446   l_debug_file_mode          VARCHAR2(1);
7447 
7448   l_prd_workorder_tbl         PRD_WORKORDER_TBL;
7449   l_prd_workorder_rel_tbl     PRD_WORKORDER_REL_TBL;
7450 
7451 
7452 CURSOR validate_visit(c_visit_id NUMBER)
7453     IS
7454  SELECT 1
7455    FROM   AHL_VISITS_B
7456   WHERE  VISIT_ID=c_visit_id;
7457 
7458 CURSOR check_workorder_exists(c_visit_id NUMBER)
7459     IS
7460  SELECT AWO.WORKORDER_ID,
7461         AWO.OBJECT_VERSION_NUMBER,
7462         WIP.WIP_ENTITY_ID,
7463         AWO.STATUS_CODE,
7464         WIP.SCHEDULED_START_DATE,
7465         WIP.SCHEDULED_COMPLETION_DATE
7466   FROM  AHL_WORKORDERS AWO,
7467         WIP_DISCRETE_JOBS WIP
7468  WHERE AWO.VISIT_ID=c_visit_id
7469    AND AWO.VISIT_TASK_ID IS NULL
7470    AND AWO.MASTER_WORKORDER_FLAG = 'Y'
7471    AND AWO.WIP_ENTITY_ID = WIP.WIP_ENTITY_ID
7472    AND AWO.STATUS_CODE NOT IN ('22', '7');
7473 
7474 check_workorder_exists_rec check_workorder_exists%rowtype;
7475 
7476 CURSOR get_latest_schedule_dates(c_wip_entity_id IN NUMBER)
7477  IS
7478 SELECT scheduled_start_date,
7479        scheduled_completion_date
7480  FROM wip_discrete_jobs
7481  WHERE wip_entity_id = c_wip_entity_id;
7482 
7483 latest_schedule_dates_rec get_latest_schedule_dates%ROWTYPE;
7484 
7485 BEGIN
7486 
7487      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)THEN
7488 		fnd_log.string
7489 		(
7490 			fnd_log.level_procedure,
7491 			'ahl.plsql.AHL_PRD_WORKORDER_PVT.Reschedule_Visit_Jobs.begin',
7492 			'At the start of PLSQL procedure'
7493 		);
7494      END IF;
7495      -- Standard start of API savepoint
7496      SAVEPOINT Reschedule_Visit_Jobs;
7497      --
7498      IF NOT FND_API.compatible_api_call(l_api_version,
7499                                         p_api_version,
7500                                         l_api_name,G_PKG_NAME) THEN
7501       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7502       END IF;
7503      -- Initialize message list if p_init_msg_list is set to TRUE
7504       IF FND_API.to_boolean(p_init_msg_list) THEN
7505          FND_MSG_PUB.initialize;
7506       END IF;
7507      -- Initialize API return status to success
7508       x_return_status:=FND_API.G_RET_STS_SUCCESS;
7509 
7510      IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7511 		fnd_log.string
7512 		(
7513 			fnd_log.level_statement,
7514             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7515 			'Request for Rescheduling  Workorders for Visit ID : ' || p_visit_id
7516 		);
7517 
7518 		fnd_log.string
7519 		(
7520 			fnd_log.level_statement,
7521             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7522 			'Request for Rescheduling  Workorder Schedule Start Date : ' || p_x_scheduled_start_date
7523 		);
7524 		fnd_log.string
7525 		(
7526 			fnd_log.level_statement,
7527             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7528 			'Request for Rescheduling  Workorder Schedule End Date : ' || p_x_scheduled_end_date
7529 		);
7530 
7531      END IF;
7532 
7533   ---Begin of the Validation
7534    OPEN validate_visit(p_visit_id)  ;
7535    FETCH validate_visit into l_dummy ;
7536    IF (validate_visit%NOTFOUND)
7537 		THEN
7538 		  	FND_MESSAGE.Set_Name('AHL','AHL_PRD_VISIT_NOT_FOUND');
7539 		  	FND_MSG_PUB.ADD;
7540 	close validate_visit;
7541   	RAISE FND_API.G_EXC_ERROR;
7542 	END IF;
7543 	close validate_visit;
7544 
7545 
7546   IF ( G_DEBUG = 'Y' ) THEN
7547     set_eam_debug_params
7548     (
7549       x_debug           => l_debug,
7550       x_output_dir      => l_output_dir,
7551       x_debug_file_name => l_debug_filename,
7552       x_debug_file_mode => l_debug_file_mode
7553     );
7554   END IF;
7555 
7556 
7557    OPEN check_workorder_exists(p_visit_id);
7558    FETCH check_workorder_exists into check_workorder_exists_rec;
7559    IF (check_workorder_exists%NOTFOUND)
7560    THEN
7561 		  	FND_MESSAGE.Set_Name('AHL','AHL_PRD_VISIT_MWO_NOT_FOUND');
7562 		  	FND_MSG_PUB.ADD;
7563 					close check_workorder_exists;
7564 		  	RAISE FND_API.G_EXC_ERROR;
7565    END IF;
7566 
7567      IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7568 		fnd_log.string
7569 		(
7570 			fnd_log.level_statement,
7571             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7572 			'Workorder Status Code : ' || check_workorder_exists_rec.status_code
7573 		);
7574 
7575      END IF;
7576    -- Allow reschedule for Unreleased, Released and Draft Workorders
7577    /*Balaji commented out the code for the BAE bug # 4615383 which prevents
7578    the user from re-scheduling the visit when the visit workorder is in
7579    status other than 1,3 and 17.However this needs to be modified to the end status
7580    Complete, Cancelled, Closed and Complete No Charge as per the discussion with
7581    Shailaja. If the master workorder falls in any of these end statuses then
7582    rescheduling should be prevented.
7583    Hence removing this validation instead adding following validation.
7584    IF (check_workorder_exists_rec.status_code NOT IN ( 1,3,17) )
7585    THEN
7586  		  FND_MESSAGE.Set_Name('AHL','AHL_PRD_JOB_STATUS_INVALID');
7587 		  	FND_MSG_PUB.ADD;
7588 					close check_workorder_exists;
7589 		  	RAISE FND_API.G_EXC_ERROR;
7590    END IF;
7591    */
7592    -- 4 -> Complete
7593    -- 5 -> Complete No Charge
7594    -- 7 -> Cancelled
7595    -- 12 -> Closed
7596    IF (check_workorder_exists_rec.status_code IN (4, 5, 7, 12) )
7597    THEN
7598  	FND_MESSAGE.Set_Name('AHL','AHL_PRD_JOB_STATUS_INVALID');
7599 	FND_MSG_PUB.ADD;
7600 	close check_workorder_exists;
7601 	RAISE FND_API.G_EXC_ERROR;
7602    END IF;
7603 
7604    close check_workorder_exists;
7605 
7606 
7607       IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7608          fnd_log.string
7609 	    (
7610 		    fnd_log.level_statement,
7611 		    'ahl.plsql.AHL_PRD_WORKORDER_PVT.Reschedule_Visit_Jobs',
7612 		    'After Validate workorder details '
7613 	    );
7614 
7615      END IF;
7616    -- rroy
7617    -- reschedule logic:
7618    -- 1. Reschedule will be done only if
7619    -- a) the visit start date and planned end date change
7620    -- b) only the visit start date changes
7621    -- 2. If only the visit planned end date changes then
7622    -- the visit master workorder end date will be updated
7623    -- 3. for all reschedules, the visit master workorder end date will,
7624    -- after rescheduling, be updated to the visit planned end date
7625    -- if while carrying out update, the resulting visit master workorder
7626    -- end date lies before any of the child workorder end dates
7627    -- then the EAM APIs will throw an error, which will
7628    -- probably be the scheduling hierarchy error.
7629    -- 4. All scheduling will always be forward scheduling
7630 
7631    l_offset := p_x_scheduled_start_date - check_workorder_exists_rec.scheduled_start_date;
7632    l_schedule_method  := 1; -- forward schedule
7633    IF (l_offset = 0) THEN
7634    	-- only the planned end date has changed
7635    	-- so we need to update the master workorder end date
7636    	-- with a call to update jobs
7637    	l_prd_workorder_tbl(1).DML_OPERATION := 'U';
7638    	l_prd_workorder_tbl(1).WORKORDER_ID := check_workorder_exists_rec.workorder_id;
7639    	l_prd_workorder_tbl(1).OBJECT_VERSION_NUMBER := check_workorder_exists_rec.object_version_number;
7640    	l_prd_workorder_tbl(1).STATUS_CODE  := check_workorder_exists_rec.status_code;
7641    	l_prd_workorder_tbl(1).SCHEDULED_START_DATE  := check_workorder_exists_rec.scheduled_start_date;
7642    	l_prd_workorder_tbl(1).SCHEDULED_END_DATE    := p_x_scheduled_end_date;
7643    	l_prd_workorder_tbl(1).BATCH_ID := 1;
7644    	l_prd_workorder_tbl(1).HEADER_ID := 0;
7645 
7646 
7647      IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7648          fnd_log.string
7649 	    (
7650 		    fnd_log.level_statement,
7651 		    'ahl.plsql.AHL_PRD_WORKORDER_PVT.Reschedule_Visit_Jobs',
7652 		    'Before Calling Process Jobs '
7653 	    );
7654 
7655      END IF;
7656 
7657    	process_jobs
7658    	(
7659       	p_api_version            => 1.0,
7660       	p_init_msg_list          => FND_API.G_TRUE,
7661       	p_commit                 => FND_API.G_FALSE,
7662       	p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
7663       	p_default                => FND_API.G_TRUE,
7664       	p_module_type            => NULL,
7665       	x_return_status          => l_return_status,
7666      	x_msg_count              => l_msg_count,
7667       	x_msg_data               => l_msg_data,
7668       	p_x_prd_workorder_tbl    => l_prd_workorder_tbl,
7669       	p_prd_workorder_rel_tbl  => l_prd_workorder_rel_tbl
7670     	);
7671 
7672     	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7673        		RAISE Fnd_Api.g_exc_error;
7674     	END IF;
7675 
7676         IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7677             fnd_log.string
7678        		(
7679 	            fnd_log.level_statement,
7680        			'ahl.plsql.AHL_PRD_WORKORDER_PVT.Reschedule_Visit_Jobs',
7681        			'After calling Process Jobs API to reschedule master workorder'
7682        		);
7683 
7684          END IF;
7685 
7686    	RETURN;
7687    END IF ; -- IF (l_offset = 0) THEN
7688     /*
7689       It is just an assumption. As all the CMRO Workorders Type are 1
7690     */
7691 
7692     l_object_type_id := 1;
7693 
7694 	IF  l_offset > 0
7695     THEN
7696       l_offset_direction := 1;
7697     ELSE
7698        l_offset_direction := -1;
7699        l_offset       := l_offset * (-1);
7700     END IF;
7701 
7702     IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7703 		fnd_log.string
7704 		(
7705 			fnd_log.level_statement,
7706             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7707 			'Rescheduled Wip Entity ID : ' || check_workorder_exists_rec.wip_entity_id
7708 		);
7709 
7710 		fnd_log.string
7711 		(
7712 			fnd_log.level_statement,
7713             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7714 			'Object Type Id : ' || l_object_type_id
7715 		);
7716 		fnd_log.string
7717 		(
7718 			fnd_log.level_statement,
7719             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7720 			'Offset : ' || l_offset
7721 		);
7722 		fnd_log.string
7723 		(
7724 			fnd_log.level_statement,
7725             'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
7726 			'Offset Direction : ' || l_offset_direction
7727 		);
7728 
7729      END IF;
7730 
7731     --Call Eam APi TO Move all the workorders
7732 
7733      IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7734          fnd_log.string
7735 	    (
7736 		    fnd_log.level_statement,
7737 		    'ahl.plsql.AHL_PRD_WORKORDER_PVT.Reschedule_Visit_Jobs',
7738 		    'Before Calling EAM_WO_NETWORK_UTIL_PVT.Move_WO '
7739 	    );
7740 
7741      END IF;
7742 
7743     EAM_WO_NETWORK_UTIL_PVT.Move_WO
7744         (
7745         p_api_version         => p_api_version,
7746         p_init_msg_list       => p_init_msg_list,
7747         p_commit              => p_commit,
7748         p_validation_level     => p_validation_level,
7749 
7750         p_work_object_id       =>  check_workorder_exists_rec.wip_entity_id,
7751         p_work_object_type_id      => l_object_type_id,
7752         p_offset_days              => l_offset,  -- 1 Day Default
7753         p_offset_direction         => l_offset_direction, -- Forward
7754         p_start_date               => p_x_scheduled_start_date,
7755         p_completion_date          => l_scheduled_end_date,
7756         p_schedule_method          => l_schedule_method,
7757 	    p_ignore_firm_flag		   => l_ignore_firm_flag,
7758 
7759         x_return_status            => l_return_status,
7760         x_msg_count                => l_msg_count,
7761         x_msg_data                 => l_msg_data
7762 
7763 		);
7764 
7765 
7766     	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7767        		RAISE Fnd_Api.g_exc_error;
7768     	END IF;
7769 
7770      IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7771          fnd_log.string
7772 	    (
7773 		    fnd_log.level_statement,
7774 		    'ahl.plsql.AHL_PRD_WORKORDER_PVT.Reschedule_Visit_Jobs',
7775 		    'After Calling EAM_WO_NETWORK_UTIL_PVT.Move_WO ' ||l_return_status
7776 	    );
7777 
7778      END IF;
7779 
7780   -- after rescheduling
7781   -- update the visit master workorder end date with the
7782   -- visit planned end date
7783   -- if there are any discrepancies here
7784   -- let EAM raise the error
7785    OPEN get_latest_schedule_dates(check_workorder_exists_rec.wip_entity_id);
7786    FETCH get_latest_schedule_dates INTO latest_schedule_dates_rec;
7787    IF get_latest_schedule_dates%FOUND THEN
7788          p_x_scheduled_start_date := latest_schedule_dates_rec.scheduled_start_date;
7789 	   END IF;
7790      CLOSE get_latest_schedule_dates;
7791   --
7792   IF p_x_scheduled_end_date <> latest_schedule_dates_rec.scheduled_completion_date THEN
7793   --
7794  IF ( G_DEBUG = 'Y' ) THEN
7795           AHL_DEBUG_PUB.debug( 'after dates descripency'||p_x_scheduled_end_date );
7796           AHL_DEBUG_PUB.debug( 'after dates descripency'||latest_schedule_dates_rec.scheduled_completion_date );
7797 
7798   END IF;
7799 
7800   l_prd_workorder_tbl(1).DML_OPERATION         := 'U';
7801   l_prd_workorder_tbl(1).WORKORDER_ID          := check_workorder_exists_rec.workorder_id;
7802   l_prd_workorder_tbl(1).OBJECT_VERSION_NUMBER := check_workorder_exists_rec.object_version_number;
7803   l_prd_workorder_tbl(1).STATUS_CODE           := check_workorder_exists_rec.status_code;
7804   l_prd_workorder_tbl(1).SCHEDULED_END_DATE    := p_x_scheduled_end_date;
7805   l_prd_workorder_tbl(1).BATCH_ID              := 1;
7806   l_prd_workorder_tbl(1).HEADER_ID             := 0;
7807 
7808  IF ( G_DEBUG = 'Y' ) THEN
7809           AHL_DEBUG_PUB.debug( 'Calling process_jobs API' );
7810   END IF;
7811 
7812   process_jobs
7813   (
7814       	p_api_version            => 1.0,
7815       	p_init_msg_list          => FND_API.G_TRUE,
7816       	p_commit                 => FND_API.G_FALSE,
7817       	p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
7818       	p_default                => FND_API.G_TRUE,
7819       	p_module_type            => NULL,
7820       	x_return_status          => l_return_status,
7821      	x_msg_count              => l_msg_count,
7822       	x_msg_data               => l_msg_data,
7823       	p_x_prd_workorder_tbl    => l_prd_workorder_tbl,
7824       	p_prd_workorder_rel_tbl  => l_prd_workorder_rel_tbl
7825   );
7826 
7827   END IF;
7828 
7829   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7830    IF p_x_scheduled_end_date <> latest_schedule_dates_rec.scheduled_completion_date
7831    THEN
7832       -- Initialize the message list not to show the EAM Error message
7833       IF FND_API.to_boolean(p_init_msg_list) THEN
7834          FND_MSG_PUB.initialize;
7835       END IF;
7836 	  FND_MESSAGE.Set_Name('AHL','AHL_PRD_PLANNED_DATE_INVALID');
7837       FND_MESSAGE.SET_TOKEN('DATE',latest_schedule_dates_rec.scheduled_completion_date);
7838   	  FND_MSG_PUB.ADD;
7839     END IF;
7840   	RAISE Fnd_Api.g_exc_error;
7841 
7842   END IF;
7843 
7844     --
7845      IF FND_API.to_boolean(p_commit) THEN
7846          COMMIT;
7847      END IF;
7848 
7849      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)THEN
7850 		fnd_log.string
7851 		(
7852 			fnd_log.level_procedure,
7853 			'ahl.plsql.AHL_PRD_WORKORDER_PVT.Reschedule_Visit_Jobs.end',
7854 			'At the end of PLSQL procedure'
7855 		);
7856      END IF;
7857 
7858 EXCEPTION
7859  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7860     ROLLBACK TO Reschedule_Visit_Jobs;
7861     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7862     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
7863                                p_count => x_msg_count,
7864                                p_data  => x_msg_data);
7865  WHEN FND_API.G_EXC_ERROR THEN
7866     ROLLBACK TO Reschedule_Visit_Jobs;
7867     x_return_status := FND_API.G_RET_STS_ERROR;
7868     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
7869                                p_count => x_msg_count,
7870                                p_data  => x_msg_data);
7871 
7872  WHEN OTHERS THEN
7873     ROLLBACK TO Reschedule_Visit_Jobs;
7874     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7875     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
7876       FND_MSG_PUB.add_exc_msg(p_pkg_name        =>g_pkg_name,
7877                               p_procedure_name  =>l_api_name,
7878                               p_error_text      => SUBSTR(SQLERRM,1,240));
7879 
7880     END IF;
7881     FND_MSG_PUB.count_and_get( p_encoded => FND_API.G_FALSE,
7882                                p_count => x_msg_count,
7883                                p_data  => x_msg_data);
7884 
7885 END Reschedule_Visit_Jobs;
7886 
7887 
7888 PROCEDURE INSERT_TURNOVER_NOTES
7889 (
7890   p_api_version          IN  NUMBER    := 1.0 ,
7891   p_init_msg_list        IN  VARCHAR2  :=  FND_API.G_TRUE,
7892   p_commit               IN  VARCHAR2  :=  FND_API.G_FALSE,
7893   p_validation_level     IN  NUMBER    :=  FND_API.G_VALID_LEVEL_FULL,
7894   p_default              IN  VARCHAR2   := FND_API.G_FALSE,
7895   p_module_type          IN  VARCHAR2  := Null,
7896   x_return_status        OUT NOCOPY VARCHAR2,
7897   x_msg_count            OUT NOCOPY NUMBER,
7898   x_msg_data             OUT NOCOPY VARCHAR2,
7899   p_trunover_notes_tbl	 IN OUT NOCOPY	AHL_PRD_WORKORDER_PVT.turnover_notes_tbl_type
7900 ) IS
7901 
7902    l_api_version      CONSTANT NUMBER := 1.0;
7903    l_api_name         CONSTANT VARCHAR2(30) := 'INSERT_TURNOVER_NOTES';
7904 
7905    CURSOR get_user_id_csr(p_emp_name VARCHAR2, p_org_id NUMBER) IS
7906 
7907    /* fix for perf. bug# 4919298.
7908    SELECT FU.user_id, PF.person_id FROM fnd_user FU,PER_PEOPLE_F PF, HR_ORGANIZATION_UNITS HOU, PER_PERSON_TYPES PEPT, BOM_RESOURCE_EMPLOYEES BRE
7909    WHERE NVL(PF.CURRENT_EMPLOYEE_FLAG, 'X') = 'Y'
7910    AND PEPT.PERSON_TYPE_ID = PF.PERSON_TYPE_ID
7911    AND PEPT.SYSTEM_PERSON_TYPE = 'EMP'
7912    AND PF.PERSON_ID = BRE.PERSON_ID
7913    AND (TRUNC(SYSDATE) BETWEEN PF.EFFECTIVE_START_DATE AND PF.EFFECTIVE_END_DATE)
7914    AND HOU.BUSINESS_GROUP_ID = PF.BUSINESS_GROUP_ID
7915    AND HOU.ORGANIZATION_ID = NVL(p_org_id,HOU.ORGANIZATION_ID)
7916    --AND NVL(FU.employee_id,-1) = PF.person_id
7917    AND FU.employee_id = PF.person_id  -- removed NVL to avoid FTS on fnd_user.
7918    AND UPPER(PF.FULL_NAME) like UPPER(p_emp_name);
7919    */
7920 
7921     SELECT DISTINCT bre.person_id, fu.user_id
7922     FROM  mtl_employees_current_view pf, bom_resource_employees bre, fnd_user fu
7923     WHERE pf.employee_id=bre.person_id
7924       and pf.organization_id = bre.organization_id
7925       and sysdate between BRE.EFFECTIVE_START_DATE and BRE.EFFECTIVE_END_DATE
7926       and FU.employee_id = pf.employee_id
7927       and pf.organization_id= p_org_id
7928       and UPPER(pf.full_name) like UPPER(p_emp_name);
7929 
7930 
7931    l_user_id NUMBER;
7932    l_count NUMBER;
7933    matchFound BOOLEAN;
7934 
7935 BEGIN
7936    IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)THEN
7937 		fnd_log.string
7938 		(
7939 			fnd_log.level_procedure,
7940 			'ahl.plsql.AHL_PRD_WORKORDER_PVT.INSERT_TURNOVER_NOTES.begin',
7941 			'At the start of PLSQL procedure'
7942 		);
7943   END IF;
7944   -- Standard start of API savepoint
7945   SAVEPOINT INSERT_TURNOVER_NOTES;
7946 
7947   -- Standard call to check for call compatibility
7948   IF NOT FND_API.Compatible_API_Call( l_api_version, p_api_version,l_api_name, G_PKG_NAME ) THEN
7949     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7950   END IF;
7951     -- Initialize message list if p_init_msg_list is set to TRUE
7952   IF FND_API.To_Boolean( p_init_msg_list) THEN
7953     FND_MSG_PUB.Initialize;
7954   END IF;
7955 
7956   IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
7957 		fnd_log.string
7958 		(
7959 			fnd_log.level_statement,
7960 			'ahl.plsql.AHL_PRD_WORKORDER_PVT.INSERT_TURNOVER_NOTES',
7961 			'p_init_message_list : ' || p_init_msg_list
7962 		);
7963         fnd_log.string
7964 		(
7965 			fnd_log.level_statement,
7966 			'ahl.plsql.AHL_PRD_WORKORDER_PVT.INSERT_TURNOVER_NOTES',
7967 			'p_commit : ' || p_commit
7968 		);
7969   END IF;
7970   -- Initialize API return status to success
7971   x_return_status := FND_API.G_RET_STS_SUCCESS;
7972   -- Insert notes
7973   FOR i IN p_trunover_notes_tbl.FIRST..p_trunover_notes_tbl.LAST  LOOP
7974     -- validate source object code
7975     IF(p_trunover_notes_tbl(i).source_object_code <> 'AHL_WO_TURNOVER_NOTES')THEN
7976        FND_MESSAGE.Set_Name('AHL','AHL_PRD_WO_NOTES_INV_SOURCE');
7977        FND_MSG_PUB.ADD;
7978        IF (fnd_log.level_error >= fnd_log.g_current_runtime_level)THEN
7979             fnd_log.string
7980             (
7981               fnd_log.level_error,
7982               'ahl.plsql.AHL_PRD_WORKORDER_PVT.INSERT_TURNOVER_NOTES',
7983               'Invalid source object code for JTF notes' || p_trunover_notes_tbl(i).source_object_code
7984             );
7985        END IF;
7986     END IF;
7987     -- validate entered data
7988     IF(p_trunover_notes_tbl(i).entered_date > SYSDATE)THEN
7989        FND_MESSAGE.Set_Name('AHL','AHL_PRD_WO_NOTES_INV_ENT_DT');
7990        FND_MESSAGE.Set_Token('ENTERED_DATE',p_trunover_notes_tbl(i).entered_date);
7991        FND_MSG_PUB.ADD;
7992        IF (fnd_log.level_error >= fnd_log.g_current_runtime_level)THEN
7993             fnd_log.string
7994             (
7995               fnd_log.level_error,
7996               'ahl.plsql.AHL_PRD_WORKORDER_PVT.INSERT_TURNOVER_NOTES',
7997               'Invalid enterded date' || to_char(p_trunover_notes_tbl(i).entered_date)
7998             );
7999        END IF;
8000     END IF;
8001     -- validate that notes cant not be null
8002     IF(p_trunover_notes_tbl(i).notes IS NULL)THEN
8003       FND_MESSAGE.Set_Name('AHL','AHL_PRD_WO_NOTES_INV_NOTES_NLL');
8004       FND_MSG_PUB.ADD;
8005       IF (fnd_log.level_error >= fnd_log.g_current_runtime_level)THEN
8006             fnd_log.string
8007             (
8008               fnd_log.level_error,
8009               'ahl.plsql.AHL_PRD_WORKORDER_PVT.INSERT_TURNOVER_NOTES',
8010               'Invalid enterded date' || to_char(p_trunover_notes_tbl(i).entered_date)
8011             );
8012       END IF;
8013     END IF;
8014 
8015     IF p_trunover_notes_tbl(i).employee_name IS NULL THEN
8016           FND_MESSAGE.set_name('AHL','AHL_PRD_EMP_NULL_TRNTS');
8017           FND_MSG_PUB.ADD;
8018           --l_user_id := FND_GLOBAL.user_id;
8019     ELSE
8020        matchFound := FALSE;
8021        FOR emp_rec IN get_user_id_csr(p_trunover_notes_tbl(i).employee_name,
8022                            p_trunover_notes_tbl(i).org_id) LOOP
8023            l_count := l_count + 1;
8024            l_user_id := emp_rec.user_id;
8025            IF(emp_rec.person_id = p_trunover_notes_tbl(i).employee_id)THEN
8026              matchFound := TRUE;
8027              EXIT;
8028            END IF;
8029        END LOOP;
8030        IF NOT(l_count = 1 OR  matchFound)THEN
8031          -- Invalid or non unique employee
8032          FND_MESSAGE.set_name('AHL','AHL_PRD_INV_EMP_TRNTS');
8033          FND_MESSAGE.SET_TOKEN('EMP_NAME',p_trunover_notes_tbl(i).employee_name);
8034          FND_MSG_PUB.ADD;
8035        END IF;
8036     END IF;
8037 
8038     IF(l_user_id IS NULL)THEN
8039       FND_MESSAGE.set_name('AHL','AHL_PRD_INV_EMP_NUQ_TRNTS');
8040       FND_MSG_PUB.ADD;
8041     END IF;
8042 
8043 
8044     -- add notes if no messages
8045     IF(FND_MSG_PUB.count_msg = 0)THEN
8046        JTF_NOTES_PUB.Create_note
8047        (
8048           p_api_version           => 1.0,
8049           p_init_msg_list         => FND_API.G_FALSE,
8050           p_commit                => FND_API.G_FALSE,
8051           p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
8052           x_return_status         => x_return_status,
8053           x_msg_count             => x_msg_count,
8054           x_msg_data              => x_msg_data,
8055           p_source_object_id      => p_trunover_notes_tbl(i).source_object_id,
8056           p_source_object_code    => p_trunover_notes_tbl(i).source_object_code,
8057           p_notes                 => p_trunover_notes_tbl(i).notes,
8058           p_entered_by            => l_user_id,
8059           p_entered_date          => p_trunover_notes_tbl(i).entered_date,
8060           x_jtf_note_id           => p_trunover_notes_tbl(i).jtf_note_id
8061        );
8062     END IF;
8063   END LOOP;
8064   -- Check Error Message stack.
8065   x_msg_count := FND_MSG_PUB.count_msg;
8066   IF x_msg_count > 0 THEN
8067      RAISE  FND_API.G_EXC_ERROR;
8068   END IF;
8069 
8070   -- Standard check of p_commit
8071   IF FND_API.TO_BOOLEAN(p_commit) THEN
8072       COMMIT WORK;
8073   END IF;
8074   -- Standard call to get message count and if count is 1, get message info
8075   FND_MSG_PUB.Count_And_Get
8076     ( p_count => x_msg_count,
8077       p_data  => x_msg_data,
8078       p_encoded => fnd_api.g_false
8079     );
8080   IF(x_msg_count > 0 )THEN
8081     RAISE  FND_API.G_EXC_ERROR;
8082   END IF;
8083 
8084   IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)THEN
8085 		fnd_log.string
8086 		(
8087 			fnd_log.level_procedure,
8088 			'ahl.plsql.AHL_PRD_WORKORDER_PVT.INSERT_TURNOVER_NOTES.end',
8089 			'At the end of PLSQL procedure'
8090 		);
8091   END IF;
8092 
8093  EXCEPTION
8094   WHEN FND_API.G_EXC_ERROR THEN
8095    Rollback to INSERT_TURNOVER_NOTES;
8096    x_return_status := FND_API.G_RET_STS_ERROR;
8097    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
8098                               p_data  => x_msg_data,
8099                               p_encoded => fnd_api.g_false);
8100 
8101 
8102  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8103    Rollback to INSERT_TURNOVER_NOTES;
8104    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8105    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
8106                               p_data  => x_msg_data,
8107                               p_encoded => fnd_api.g_false);
8108 
8109 
8110  WHEN OTHERS THEN
8111     Rollback to INSERT_TURNOVER_NOTES;
8112     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8113     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
8114        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
8115                                p_procedure_name => l_api_name,
8116                                p_error_text     => SUBSTR(SQLERRM,1,500));
8117     END IF;
8118     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
8119                                p_data  => x_msg_data,
8120                                p_encoded => fnd_api.g_false);
8121 END INSERT_TURNOVER_NOTES;
8122 
8123 FUNCTION get_date_and_time(p_date IN DATE,
8124                            p_date_hh24 IN VARCHAR2,
8125                            p_date_mi IN VARCHAR2,
8126                            p_date_ss IN VARCHAR2) RETURN DATE IS
8127 
8128 l_hour                  VARCHAR2(30);
8129 l_sec                   VARCHAR2(30);
8130 l_minutes               VARCHAR2(30);
8131 l_date_time             VARCHAR2(30);
8132 l_date                  DATE;
8133 
8134 BEGIN
8135 
8136     l_sec := TO_CHAR(p_date, 'ss');
8137     l_hour := TO_CHAR(p_date, 'hh24');
8138     l_minutes := TO_CHAR(p_date, 'mi');
8139     l_date := p_date;
8140 
8141     IF ( p_date_hh24 IS NOT NULL AND
8142          p_date_hh24 <> FND_API.G_MISS_NUM ) THEN
8143       l_hour := p_date_hh24;
8144     END IF;
8145 
8146     IF ( p_date_mi IS NOT NULL AND
8147          p_date_mi <> FND_API.G_MISS_NUM ) THEN
8148       l_minutes := p_date_mi;
8149     END IF;
8150 
8151     IF(p_date_ss IS NOT NULL AND
8152        p_date_ss <> FND_API.G_MISS_NUM) THEN
8153        l_sec := p_date_ss;
8154     END IF;
8155 
8156     IF ( l_hour <> '00' OR l_minutes <> '00' OR l_sec <> '00') THEN
8157       l_date_time := TO_CHAR(p_date, 'DD-MM-YYYY')||' :'|| l_hour ||':'|| l_minutes || ':'|| l_sec;
8158       l_date := TO_DATE(l_date_time , 'DD-MM-YYYY :HH24:MI:ss');
8159     END IF;
8160     RETURN l_date;
8161 END get_date_and_time;
8162 
8163 END AHL_PRD_WORKORDER_PVT;