DBA Data[Home] [Help]

PACKAGE BODY: APPS.AHL_AVF_OPER_VSTS_PVT

Source


1 PACKAGE BODY  AHL_AVF_OPER_VSTS_PVT AS
2 /* $Header: AHLVOPVB.pls 120.0.12020000.2 2012/12/07 01:20:57 sareepar noship $ */
3 
4 G_PKG_NAME     CONSTANT VARCHAR2(30) := ' AHL_AVF_OPER_VSTS_PVT';
5 G_APP_NAME     CONSTANT VARCHAR2(3)  := 'AHL';
6 
7 ------------------------------------
8 -- Common constants and variables --
9 ------------------------------------
10 l_log_current_level     NUMBER      := fnd_log.g_current_runtime_level;
11 l_log_statement         NUMBER      := fnd_log.level_statement;
12 l_log_procedure         NUMBER      := fnd_log.level_procedure;
13 
14 ---------------------------------------------------------------------
15 --   Define Record Types for record structures needed by the APIs  --
16 ---------------------------------------------------------------------
17 --TCHIMIRA :: 14-Jun-2012 :: start
18 -- Record type for operational visit parameters
19 TYPE oper_param_rec_type IS RECORD
20    (visit_type_code         VARCHAR2(30),
21     mc_id                   NUMBER,
22     alternate_dep_id        NUMBER,
23     autovst_oper_id         NUMBER );
24 
25 -- Record type for flight schedules
26 TYPE flight_schedule_rec_type IS RECORD
27    (unit_config_header_id    NUMBER,
28     departure_org_id         NUMBER,
29     departure_dept_id        NUMBER,
30     unit_schedule_id         NUMBER,
31     est_departure_time       DATE,
32     arrival_org_id           NUMBER,
33     arrival_dept_id          NUMBER,
34     est_arrival_time         DATE);
35 --TCHIMIRA :: 14-Jun-2012 :: end
36 --------------------------------------------------------------------
37 -- Define Table Type for Records Structures                       --
38 --------------------------------------------------------------------
39 -- NO TABLE TYPES **************
40 
41 --------------------------------------------------------------------
42 -- START: Defining local functions and procedures BODY            --
43 --------------------------------------------------------------------
44 --TCHIMIRA :: 14-Jun-2012 :: start
45 --   To Create a departure visit at the departing org of the flight
46 PROCEDURE Create_Departure_visit(
47     p_oper_param_rec       IN      oper_param_rec_type,
48     p_flight_schedule_rec  IN      flight_schedule_rec_type,
49     p_present_time         IN      DATE, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
50     p_is_creation_success  OUT NOCOPY VARCHAR2, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
51     x_return_status        OUT NOCOPY     VARCHAR2,
52     x_msg_count            OUT NOCOPY NUMBER,
53     x_msg_data             OUT NOCOPY VARCHAR2
54 );
55 
56 --   To Create an arrival visit at the arrival org of the flight
57 PROCEDURE Create_Arrival_visit(
58     p_oper_param_rec       IN      oper_param_rec_type,
59     p_flight_schedule_rec  IN      flight_schedule_rec_type,
60     p_present_time         IN      DATE, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
61     p_is_creation_success  OUT NOCOPY VARCHAR2, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
62     x_return_status        OUT NOCOPY     VARCHAR2,
63     x_msg_count            OUT NOCOPY NUMBER,
64     x_msg_data             OUT NOCOPY VARCHAR2
65 );
66 
67 --   To Create a downtime visit at the arrival org of the flight
68 PROCEDURE Create_Downtime_visit(
69     p_oper_param_rec       IN      oper_param_rec_type,
70     p_flight_schedule_rec  IN      flight_schedule_rec_type,
71     p_present_time         IN      DATE, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
72     p_is_creation_success  OUT NOCOPY VARCHAR2, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
73     x_return_status        OUT NOCOPY     VARCHAR2,
74     x_msg_count            OUT NOCOPY NUMBER,
75     x_msg_data             OUT NOCOPY VARCHAR2
76 );
77 
78 --   To Delete an auto visit
79 PROCEDURE Delete_Oper_Visit(
80     p_visit_id       IN   NUMBER,
81     x_return_status  OUT NOCOPY     VARCHAR2);
82 --TCHIMIRA :: 14-Jun-2012 :: end
83 
84 -- PRAKKUM :: Bug 13844759 :: 26/07/2012
85 PROCEDURE Load_Can_Cancel_Visit (
86     p_visit_id               IN                     NUMBER,
87     p_visit_id2              IN                     NUMBER,
88     x_can_cancel_visit       OUT NOCOPY             VARCHAR2,
89     x_can_cancel_visit2      OUT NOCOPY             VARCHAR2,
90     x_return_status          OUT NOCOPY             VARCHAR2,
91     x_msg_count              OUT NOCOPY             NUMBER,
92     x_msg_data               OUT NOCOPY             VARCHAR2
93 );
94 
95 --PRAKKUM :: 01/08/2012 :: Added procedure
96 PROCEDURE Disconnect_Flight_Visit (
97     p_visit_id               IN                     NUMBER,
98     p_unit_schedule_id       IN                     NUMBER,
99     x_return_status          OUT NOCOPY             VARCHAR2,
100     x_msg_count              OUT NOCOPY             NUMBER,
101     x_msg_data               OUT NOCOPY             VARCHAR2
102 );
103 
104 ----------------------------------------------------------------------------------------------------------------
105 --  Procedure name    : Create_Oper_Visit
106 --  Type              : Private
107 --  Function          : Procedure to create visit based on operational params
108 --  Parameters  :
109 --
110 --  Standard IN  Parameters :
111 --      p_api_version      IN  NUMBER        Required
112 --      p_init_msg_list    IN  VARCHAR2      Default  FND_API.G_FALSE
113 --      p_validation_level IN  NUMBER        Default  FND_API.G_VALID_LEVEL_FULL
114 --
115 --  Standard OUT Parameters :
116 --      x_return_status    OUT VARCHAR2      Required
117 --      x_msg_count        OUT NUMBER        Required
118 --      x_msg_data         OUT VARCHAR2      Required
119 --------------------------------------------------------------------------------------------------------------------
120 PROCEDURE Create_Oper_Visit (
121     p_api_version           IN             NUMBER    := 1.0,
122     p_init_msg_list         IN             VARCHAR2  := FND_API.G_FALSE,
123     p_commit                IN             VARCHAR2  := FND_API.G_FALSE,
124     p_validation_level      IN             NUMBER    := FND_API.G_VALID_LEVEL_FULL,
125     x_return_status         OUT NOCOPY     VARCHAR2,
126     x_msg_count             OUT NOCOPY     NUMBER,
127     x_msg_data              OUT NOCOPY     VARCHAR2)IS
128 
129 -- Local Variables
130 
131 -- Standard in/out parameters
132 l_api_name                    VARCHAR2(30) := 'CREATE_OPER_VISIT';
133 l_api_version                 NUMBER       := 1.0;
134 l_debug_key          CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
135 l_msg_count                   NUMBER;
136 l_msg_data                    VARCHAR2(2000);
137 l_return_status               VARCHAR2(1);
138 l_init_msg_list               VARCHAR2(10):= p_init_msg_list;
139 l_visit_type                  VARCHAR2(40);
140 l_visit_id                    NUMBER;
141 l_visit_type_duration         NUMBER;
142 l_visit_id2                   NUMBER;
143 x_item_key                    VARCHAR2(100);
144 l_can_cancel_visit            VARCHAR2(1);
145 l_can_cancel_visit2           VARCHAR2(1);
146 l_count_prec_arr_org          NUMBER;
147 l_count_prec_arr_dep          NUMBER;
148 l_count_prec_arr_cat          NUMBER;
149 l_visit_type_code             VARCHAR2(30);
150 l_flight_schedule_rec         flight_schedule_rec_type;
151 l_oper_param_rec              oper_param_rec_type;
152 l_prec_flight_schedule_rec    flight_schedule_rec_type;
153 -- Bug 14336467 :: PRAKKUM :: 13/07/2012
154 l_present_time                DATE := SYSDATE;
155 l_is_creation_success         VARCHAR2(1) := 'Y';
156 l_is_downtime_vst_created     VARCHAR2(1) := 'Y';
157 
158 -- Cursor to get the visit Number from visit_id
159 CURSOR get_visit_number(c_visit_id IN NUMBER) IS
160 SELECT visit_number
161 FROM ahl_visits_b
162 WHERE visit_id = c_visit_id;
163 
164 -- Cursor to get all the flight schedule records whose status is 'Created'
165 Cursor get_flight_schedule_rows(c_status IN VARCHAR2)
166 IS
167 SELECT *
168 FROM AHL_UNIT_SCHEDULES
169 WHERE AUTOVISIT_PROCESS_STATUS = c_status;
170 
171 --Get the count of rows matching with the UC and org of the flight schedule and enabled_flag as Y
172 Cursor get_non_null_org_count (c_uc_id IN NUMBER, c_org_id IN NUMBER)
173 IS
174 SELECT count (*)
175 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
176 WHERE op.enabled_flag = 'Y'
177  AND uc.unit_config_header_id = c_uc_id
178  AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
179  AND op.organization_id = c_org_id;
180  l_count_org NUMBER;
181 
182 --Get the count of rows matching with the UC, org and dep of the flight schedule and enabled_flag as Y
183 Cursor get_non_null_dep_count (c_uc_id IN NUMBER, c_org_id IN NUMBER, c_dep_id IN NUMBER)
184 IS
185 SELECT count (*)
186 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
187 WHERE op.enabled_flag = 'Y'
188  AND uc.unit_config_header_id = c_uc_id
189  AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
190  AND op.organization_id = c_org_id
191  AND op.department_id = c_dep_id;
192  l_count_dep NUMBER;
193 
194 --Get the count of rows matching with the UC, org, dep and category of the flight schedule and enabled_flag as Y
195 Cursor get_non_null_cat_count (c_uc_id IN NUMBER, c_org_id IN NUMBER, c_dep_id IN NUMBER, c_flight_cat IN VARCHAR2)
196 IS
197 SELECT count (*)
198 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
199 WHERE op.enabled_flag = 'Y'
200  AND uc.unit_config_header_id = c_uc_id
201  AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
202  AND op.organization_id = c_org_id
203  AND op.department_id = c_dep_id
204  AND op.flight_category_code = c_flight_cat;
205  l_count_cat NUMBER;
206 
207 -- Get the ground time (in number of hours) between two flight schedules
208 Cursor get_ground_time (c_prev_fs_id IN NUMBER, c_succ_fs_id IN NUMBER)
209 IS
210 SELECT to_number((succ_fs.EST_DEPARTURE_TIME - prev_fs.EST_ARRIVAL_TIME)*24*60)
211 FROM AHL_UNIT_SCHEDULES prev_fs, AHL_UNIT_SCHEDULES succ_fs
212 WHERE prev_fs.UNIT_SCHEDULE_ID = c_prev_fs_id
213  AND succ_fs.UNIT_SCHEDULE_ID = c_succ_fs_id;
214  l_ground_time NUMBER;
215 
216 --Cursor to fetch operational param rows whose enabled flag is Y and matches master config
217 Cursor get_oper_param_rows1(c_uc_id IN NUMBER, c_org_id IN NUMBER, c_dep_id IN NUMBER, c_cat_code IN VARCHAR2,
218                             c_org_count IN NUMBER, c_dep_count IN NUMBER, c_cat_count IN NUMBER,
219                             c_create_for IN VARCHAR2, c_ground_time IN NUMBER)
220 IS
221 SELECT op.*
222 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
223  WHERE op.enabled_flag = 'Y'
224    AND uc.unit_config_header_id = c_uc_id
225    AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
226    AND ( (op.organization_id = c_org_id)
227         OR ( c_org_count = 0 -- there are no non-null org rows meeting mc and enabled_flag
228              AND op.organization_id IS NULL))
229    AND ( ( c_org_count = 0 )
230             OR (op.department_id = c_dep_id)
231             OR ( c_dep_count = 0
232                  AND op.department_id IS NULL))
233    AND ( (op.flight_category_code = c_cat_code)
234             OR ( ((c_cat_count = 0) OR ( c_cat_code IS NULL))
235                  AND op.flight_category_code IS NULL))
236    AND op.create_for = c_create_for
237    -- if create_for is not downtime, pass null as c_ground_time to this cursor
238    AND ( (c_ground_time IS NULL)
239          OR (c_ground_time BETWEEN op.START_TIME AND NVL(op.END_TIME,c_ground_time)));
240 get_oper_param_rec get_oper_param_rows1%ROWTYPE;
241 
242 --curosr that checks if there is any succeeding flight for this flight schedule
243 Cursor get_succeeding_us_det (c_fs_id IN NUMBER)
244  IS
245  SELECT UNIT_SCHEDULE_ID, EST_DEPARTURE_TIME FROM AHL_UNIT_SCHEDULES
246  WHERE  preceding_us_id = c_fs_id;
247 l_succeeding_us_id NUMBER;
248 l_succ_dep_time DATE;
249 
250 Cursor get_visit_type_duration(c_visit_type_code IN VARCHAR2, c_mc_id IN NUMBER)
251 IS
252 SELECT estimated_duration
253 FROM ahl_visit_types_b
254 WHERE visit_type_code = c_visit_type_code
255  AND mc_id = c_mc_id
256  AND status_code = 'COMPLETE';
257 
258 -- Cursor to get preceeding flight schedule details
259 Cursor get_pre_fs_det(c_unit_schedule_id IN NUMBER)
260 IS
261 SELECT *
262 FROM AHL_UNIT_SCHEDULES
263 WHERE UNIT_SCHEDULE_ID = c_unit_schedule_id;
264 pre_fs_det_rec get_pre_fs_det%ROWTYPE;
265 
266 --Cursor to find if the visit is in planning status and not firmed and not locked
267 Cursor can_cancel_visit(c_visit_id IN NUMBER)
268 IS
269 SELECT 'X'
270 FROM AHL_VISITS_B
271 WHERE VISIT_ID = c_visit_id
272  AND STATUS_CODE = 'PLANNING'
273  AND NVL(FIRMED_FLAG,'N') <> 'Y'
274  AND NVL(LOCKED_FLAG,'N') <> 'Y';
275 
276 -- Cursor to get visit type of a visit
277 Cursor get_visit_type_code(c_visit_id IN NUMBER)
278 IS
279 SELECT visit_type_code
280 FROM AHL_VISITS_B
281 WHERE VISIT_ID = c_visit_id;
282 
283 BEGIN
284 
285  IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)THEN
286       fnd_log.string
287       (
288         fnd_log.level_procedure,
289        'ahl.plsql.AHL_AVF_OPER_VSTS_PVT.Create_Oper_Visit.begin',
290        'At the start of PLSQL procedure'
291       );
292  END IF;
293 
294  -- Standard start of API savepoint
295  SAVEPOINT Create_Oper_Visit_pvt;
296 
297  -- Initialize message list if p_init_msg_list is set to TRUE
298 
299  IF FND_API.To_Boolean( p_init_msg_list) THEN
300    FND_MSG_PUB.Initialize;
301  END IF;
302 
303  -- Initialize API return status to success
304  x_return_status := FND_API.G_RET_STS_SUCCESS;
305 
306  -- Standard call to check for call compatibility.
307  IF NOT Fnd_Api.COMPATIBLE_API_CALL(l_api_version,
308                                     p_api_version,
309                                     l_api_name,G_PKG_NAME)
310  THEN
311    RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
312  END IF;
313 
314  -- Added for logging visit numbers :: SATRAJEN
315  fnd_file.put_line(fnd_file.log, 'List of Operational Visit Numbers created ');
316 
317  -- Loop through all the flight schedule records whose status is Created.
318  FOR flight_schedule_rec IN get_flight_schedule_rows('C') LOOP
319   -- store all the flight params in the record
320   l_flight_schedule_rec.unit_config_header_id := flight_schedule_rec.unit_config_header_id;
321   l_flight_schedule_rec.departure_org_id := flight_schedule_rec.departure_org_id;
322   l_flight_schedule_rec.departure_dept_id := flight_schedule_rec.departure_dept_id;
323   l_flight_schedule_rec.unit_schedule_id := flight_schedule_rec.unit_schedule_id;
324   l_flight_schedule_rec.est_departure_time := flight_schedule_rec.est_departure_time;
325   l_flight_schedule_rec.arrival_org_id := flight_schedule_rec.arrival_org_id;
326   l_flight_schedule_rec.arrival_dept_id := flight_schedule_rec.arrival_dept_id;
327   l_flight_schedule_rec.est_arrival_time := flight_schedule_rec.est_arrival_time;
328 
329   -- CASE I: First consider the departing org of the flight schedule record
330   IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
331     fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'CASE 1, flight_schedule_rec.unit_schedule_id= '||flight_schedule_rec.unit_schedule_id );
332   END IF;
333 
334   OPEN get_non_null_org_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id);
335   FETCH get_non_null_org_count INTO l_count_org;
336   CLOSE get_non_null_org_count;
337 
338   OPEN get_non_null_dep_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
339                               flight_schedule_rec.departure_dept_id);
340   FETCH get_non_null_dep_count INTO l_count_dep;
341   CLOSE get_non_null_dep_count;
342 
343   OPEN get_non_null_cat_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
344                               flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code);
345   FETCH get_non_null_cat_count INTO l_count_cat;
346   CLOSE get_non_null_cat_count;
347 
348   -- check if there exisits a visit and if yes, get the visit details
349   AHL_AVF_OPER_VSTS_PVT.Get_Visit_Type(flight_schedule_rec.unit_schedule_id, 'P', l_visit_type, l_visit_id,l_visit_id2);
350 
351   IF (l_log_statement >= l_log_current_level) THEN
352     fnd_log.string(l_log_statement,L_DEBUG_KEY, 'flight_schedule_rec.preceding_us_id--@>'||flight_schedule_rec.preceding_us_id);
353   END IF;
354   --If the flight schedule does not have any preceeding event then
355   IF ( flight_schedule_rec.preceding_us_id IS NULL ) THEN
356     IF substr (l_visit_type,1,1) = 'D' THEN --existing visit is a departure visit
357       IF (l_log_statement >= l_log_current_level) THEN
358         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' There is an existing departure visit at dep org, do nothing');
359       END IF;
360       NULL; --Do nothing :: Go to arrival org consideration
361     ELSIF l_visit_type = 'N' THEN -- no visit
362 
363       IF (l_log_statement >= l_log_current_level) THEN
364         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' flight_schedule_rec.unit_config_header_id--@>'||flight_schedule_rec.unit_config_header_id||
365                                                     ' ;flight_schedule_rec.departure_org_id--@>'||flight_schedule_rec.departure_org_id||
366                                                     ' ;flight_schedule_rec.departure_dept_id--@>'||flight_schedule_rec.departure_dept_id||
367                                                     ' ;flight_schedule_rec.flight_category_code--@>'||flight_schedule_rec.flight_category_code||
368                                                     ' ;l_count_org--@>'||l_count_org||
369                                                     ' ;l_count_dep--@>'||l_count_dep||
370                                                     ' ;l_count_cat--@>'||l_count_cat||
371                                                     ' ;create for--@>DEPARTURE');
372       END IF;
373       -- check for the possibility of creating a departure visit;
374       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
375                                 flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code, l_count_org,
376                                 l_count_dep, l_count_cat,'DEPARTURE', NULL);
377       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
378       IF get_oper_param_rows1%FOUND THEN
379         --If there is a possibility to create a departure visit
380         --TYPE 1: Departure Org :: no visit to cancel :: create a departure visit
381         IF (l_log_statement >= l_log_current_level) THEN
382           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 1 and before calling Create_Departure_visit');
383         END IF;
384           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
385           l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
386           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
387           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
388 
389           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
390           SAVEPOINT OPER_VISIT_CREATION;
391 
392           Create_Departure_visit(
393             p_oper_param_rec         => l_oper_param_rec,
394             p_flight_schedule_rec    => l_flight_schedule_rec,
395             p_present_time           => l_present_time,
396             p_is_creation_success    => l_is_creation_success,
397             x_return_status          => l_return_status,
398             x_msg_count              => l_msg_count,
399             x_msg_data               => l_msg_data
400           );
401 
402           IF (l_log_statement >= l_log_current_level) THEN
403             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Departure_visit and l_return_status: '||l_return_status);
404             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
405           END IF;
406 
407           -- Bug 14336467 :: PRAKKUM :: 13/07/2012
408           IF l_is_creation_success<>'Y' THEN
409             ROLLBACK TO OPER_VISIT_CREATION;
410           END IF;
411           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
412       END IF;
413       CLOSE get_oper_param_rows1;
414        -- Go to arrival org consideration
415     END IF; --ELSIF l_visit_type = 'N'
416 
417   --If the flight schedule has any preceeding event then
418   ELSE
419    -- check if any of the arrival prec flight visit or departure current flight visit does not satisfy any of
420    -- the criteria to be automatically cancelled.
421    -- PRAKKUM :: Bug 13844759 :: 26/07/2012
422    load_can_cancel_visit(
423       p_visit_id               => l_visit_id,
424       p_visit_id2              => l_visit_id2,
425       x_can_cancel_visit       => l_can_cancel_visit,
426       x_can_cancel_visit2      => l_can_cancel_visit2,
427       x_return_status          => l_return_status,
428       x_msg_count              => l_msg_count,
429       x_msg_data               => l_msg_data
430       );
431    IF (l_log_statement >= l_log_current_level) THEN
432        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
433    END IF;
434 
435    OPEN get_pre_fs_det(flight_schedule_rec.preceding_us_id);
436    FETCH get_pre_fs_det INTO pre_fs_det_rec;
437    CLOSE get_pre_fs_det;
438    -- store all the preceding flight params in the record
439    l_prec_flight_schedule_rec := null;
440    l_prec_flight_schedule_rec.unit_config_header_id := flight_schedule_rec.unit_config_header_id;
441    l_prec_flight_schedule_rec.unit_schedule_id      := pre_fs_det_rec.unit_schedule_id;
442    l_prec_flight_schedule_rec.arrival_org_id        := pre_fs_det_rec.arrival_org_id;
443    l_prec_flight_schedule_rec.arrival_dept_id       := pre_fs_det_rec.arrival_dept_id;
444    l_prec_flight_schedule_rec.est_arrival_time      := pre_fs_det_rec.est_arrival_time;
445 
446    IF (l_log_statement >= l_log_current_level) THEN
447         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' flight_schedule_rec.unit_config_header_id--@>'||flight_schedule_rec.unit_config_header_id||
448                                                     ' ;flight_schedule_rec.departure_org_id--@>'||flight_schedule_rec.departure_org_id||
449                                                     ' ;flight_schedule_rec.departure_dept_id--@>'||flight_schedule_rec.departure_dept_id||
450                                                     ' ;flight_schedule_rec.flight_category_code--@>'||flight_schedule_rec.flight_category_code||
451                                                     ' ;l_count_org--@>'||l_count_org||
452                                                     ' ;l_count_dep--@>'||l_count_dep||
453                                                     ' ;l_count_cat--@>'||l_count_cat||
454                                                     ' ;create for--@>DEPARTURE');
455    END IF;
456 
457 
458    -- if arrival prec flight visit can not be cancelled and there is no visit associated to dep org of current FS
459    -- then look for the possibility of creating a departure visit
460    IF ((l_can_cancel_visit2 IS NULL) AND (l_visit_id IS NULL) AND (l_visit_type <> 'T')) THEN
461       -- check for the possibility of creating a departure visit;
462       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
463                                 flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code, l_count_org,
464                                 l_count_dep, l_count_cat,'DEPARTURE', NULL);
465       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
466       IF get_oper_param_rows1%FOUND THEN
467         --If there is a possibility to create a departure visit
468         --TYPE 1a: Departure Org :: no visit to cancel :: create a departure visit
469         IF (l_log_statement >= l_log_current_level) THEN
470           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 1a and before calling Create_Departure_visit');
471         END IF;
472           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
473           l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
474           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
475           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
476 
477           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
478           SAVEPOINT OPER_VISIT_CREATION;
479 
480           Create_Departure_visit(
481             p_oper_param_rec         => l_oper_param_rec,
482             p_flight_schedule_rec    => l_flight_schedule_rec,
483             p_present_time           => l_present_time,
484             p_is_creation_success    => l_is_creation_success,
485             x_return_status          => l_return_status,
486             x_msg_count              => l_msg_count,
487             x_msg_data               => l_msg_data
488           );
489           IF (l_log_statement >= l_log_current_level) THEN
490             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Departure_visit and l_return_status: '||l_return_status);
491             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
492           END IF;
493 
494           IF l_is_creation_success<>'Y' THEN
495              ROLLBACK TO OPER_VISIT_CREATION;
496           END IF;
497           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
498       END IF; -- IF get_oper_param_rows1%FOUND THEN
499       CLOSE get_oper_param_rows1;
500        -- Go to arrival org consideration
501    END IF; --IF ((l_can_cancel_visit2 IS NULL) AND (l_visit_id IS NULL)) THEN
502 
503    -- if both arrival prec flight visit and dep visit can be automatically cancelled
504    IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
505     --Get ground time at the departing org of a FS
506     OPEN get_ground_time (flight_schedule_rec.preceding_us_id, flight_schedule_rec.UNIT_SCHEDULE_ID);
507     FETCH get_ground_time INTO l_ground_time;
508     CLOSE get_ground_time;
509 
510     IF (l_log_statement >= l_log_current_level) THEN
511         fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Ground time is '||l_ground_time);
512     END IF;
513 
514     OPEN get_non_null_org_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id);
515     FETCH get_non_null_org_count INTO l_count_prec_arr_org;
516     CLOSE get_non_null_org_count;
517 
518     OPEN get_non_null_dep_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
519                               pre_fs_det_rec.arrival_dept_id);
520     FETCH get_non_null_dep_count INTO l_count_prec_arr_dep;
521     CLOSE get_non_null_dep_count;
522 
523     OPEN get_non_null_cat_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
524                               pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code);
525     FETCH get_non_null_cat_count INTO l_count_prec_arr_cat;
526     CLOSE get_non_null_cat_count;
527 
528     IF substr (l_visit_type,1,1) <> 'T' THEN -- either no visit or departure visit at the departing org of current FS
529       IF (l_log_statement >= l_log_current_level) THEN
530           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'unit_config_header_id: '||pre_fs_det_rec.unit_config_header_id
531           ||', pre_fs_det_rec.arrival_org_id: '||pre_fs_det_rec.arrival_org_id||',  arrival_dept_id: , '
532           ||pre_fs_det_rec.arrival_dept_id  ||',  flight_category_code: ,'||pre_fs_det_rec.flight_category_code
533           ||',  l_count_prec_arr_org: ,'||l_count_prec_arr_org
534           ||', l_count_prec_arr_dep:   ,'||l_count_prec_arr_dep||',  l_count_prec_arr_cat: ,'||l_count_prec_arr_cat);
535       END IF;
536       -- check for the possibility to create a downtime visit
537       OPEN get_oper_param_rows1(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
538                                 pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code, l_count_prec_arr_org, l_count_prec_arr_dep, l_count_prec_arr_cat,
539                                 'DOWNTIME', l_ground_time);
540       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
541       l_is_downtime_vst_created := 'Y';
542       IF get_oper_param_rows1%FOUND THEN   -- there is possibility of creating a downtime visit
543        CLOSE get_oper_param_rows1;
544        IF l_visit_id2 is null THEN  -- if there is no visit associated to the arrival org of previous FS
545         --TYPE 2: Departure Org :: no visit for arrival org of previous FS to cancel :: create a downime visit
546 
547         SAVEPOINT OPER_VISIT_CREATION; -- Bug 14336467 :: PRAKKUM :: 13/07/2012
548 
549         --cancel the departure visit of the current flight if it is not null
550         IF(l_visit_id is not null) then
551           Delete_Oper_Visit(
552               p_visit_id       => l_visit_id,
553               x_return_status  => l_return_status);
554         END IF;--IF(l_visit_id is not null) then
555         IF (l_log_statement >= l_log_current_level) THEN
556           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 2 and before calling Create_Downtime_visit');
557         END IF;
558           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
559           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
560           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
561 
562           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
563           -- we need to create a downtime visit and associate to the arrival org of preceeding flight, so pass preceeding flight details
564           Create_Downtime_visit(
565             p_oper_param_rec         => l_oper_param_rec,
566             p_flight_schedule_rec    => l_prec_flight_schedule_rec,
567             p_present_time           => l_present_time,
568             p_is_creation_success    => l_is_creation_success,
569             x_return_status          => l_return_status,
570             x_msg_count              => l_msg_count,
571             x_msg_data               => l_msg_data
572           );
573 
574         IF (l_log_statement >= l_log_current_level) THEN
575           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
576           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
577         END IF;
578 
579         IF l_is_creation_success<>'Y' THEN
580             ROLLBACK TO OPER_VISIT_CREATION;
581             l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating departure visit
582         END IF;
583         -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
584 
585        ELSE -- l_visit_id2 is not null i.e if there is visit associated to the arrival org of previous FS
586         -- We need to match the visit type of the existing visit and operational param row visit type
587         OPEN get_visit_type_code(l_visit_id2);
588         FETCH get_visit_type_code INTO l_visit_type_code;
589         CLOSE get_visit_type_code;
590         IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
591           -- if it does not match then Cancel the visit assoc to arrival org and also if any departure visit associated
592           -- to the dep org of current FS. then create a downtime visit
593           IF (l_log_statement >= l_log_current_level) THEN
594              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 3');
595           END IF;
596 
597           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
598           SAVEPOINT OPER_VISIT_CREATION;
599 
600           --TYPE 3: Departure Org :: cancel arrival visit and dep visit(if exists) :: create a downtime visit
601           --Cancel the existing arrival visit of the prec FS since the visit types did not match
602           Delete_Oper_Visit(
603               p_visit_id       => l_visit_id2,
604               x_return_status  => l_return_status);
605 
606                 --cancel the departure visit of the current if it is not null
607           IF(l_visit_id is not null) then
608              Delete_Oper_Visit(
609                p_visit_id       => l_visit_id,
610                x_return_status  => l_return_status);
611           END IF;--IF(l_visit_id is not null) then
612           IF (l_log_statement >= l_log_current_level) THEN
613             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
614           END IF;
615           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
616           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
617           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
618 
619           -- we need to create a downtime visit and associate to the arrival org of preceeding flight, so pass preceeding flight details
620           Create_Downtime_visit(
621             p_oper_param_rec         => l_oper_param_rec,
622             p_flight_schedule_rec    => l_prec_flight_schedule_rec,
623             p_present_time           => l_present_time,
624             p_is_creation_success    => l_is_creation_success,
625             x_return_status          => l_return_status,
626             x_msg_count              => l_msg_count,
627             x_msg_data               => l_msg_data
628             );
629 
630           IF (l_log_statement >= l_log_current_level) THEN
631             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
632             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
633           END IF;
634 
635           IF l_is_creation_success<>'Y' THEN
636             ROLLBACK TO OPER_VISIT_CREATION;
637             l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating departure visit
638           END IF;
639           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
640 
641         ELSE
642           -- If the visit type matches, we will retain the arrival visit
643           -- check for the possibility of creating a departure visit if there is no dep visit at dep org
644           IF l_visit_id is null THEN
645            OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
646                                 flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code, l_count_org,
647                                 l_count_dep, l_count_cat,'DEPARTURE', NULL);
648            FETCH get_oper_param_rows1 INTO get_oper_param_rec;
649            IF get_oper_param_rows1%FOUND THEN
650             --there is a possibility to create a departure visit
651             --TYPE 4: Departure Org :: no dep visit associated to current FS dep org :: create a departure visit
652             IF (l_log_statement >= l_log_current_level) THEN
653                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 4 and before calling Create_Departure_visit');
654             END IF;
655             l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
656             l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
657             l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
658             l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
659 
660             -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
661             SAVEPOINT OPER_VISIT_CREATION;
662 
663             Create_Departure_visit(
664               p_oper_param_rec         => l_oper_param_rec,
665               p_flight_schedule_rec    => l_flight_schedule_rec,
666               p_present_time           => l_present_time,
667               p_is_creation_success    => l_is_creation_success,
668               x_return_status          => l_return_status,
669               x_msg_count              => l_msg_count,
670               x_msg_data               => l_msg_data
671             );
672             IF (l_log_statement >= l_log_current_level) THEN
673                fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Departure_visit and l_return_status: '||l_return_status);
674                fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
675             END IF;
676 
677             IF l_is_creation_success<>'Y' THEN
678                ROLLBACK TO OPER_VISIT_CREATION;
679             END IF;
680 
681             -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
682 
683            END IF; --IF get_oper_param_rows1%FOUND THEN
684            CLOSE get_oper_param_rows1;
685            -- Go to arrival org consideration
686           END IF; --IF l_visit_id is null THEN
687         END IF;--else of l_visit_type_code <> get_oper_param_rec.visit_type_code
688        END IF;-- else of l_visit_id2 is null
689       ELSE --a downtime visit cannot be created
690         CLOSE get_oper_param_rows1;
691         l_is_downtime_vst_created := 'N';
692       END IF;
693       IF l_is_downtime_vst_created = 'N' THEN -- Downtime visit is not applicable, so go for creating departure visit
694         -- if there is no existing visit, then check for creation of departure visit;
695         IF l_visit_type = 'N' THEN -- no existing visit
696           OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
697                                     flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
698                                     'DEPARTURE', NULL);
699           FETCH get_oper_param_rows1 INTO get_oper_param_rec;
700           IF get_oper_param_rows1%FOUND THEN
701             --TYPE 5: Departure Org :: no visit to cancel :: create a departure visit
702             IF (l_log_statement >= l_log_current_level) THEN
703               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 5 and before calling Create_Departure_visit');
704             END IF;
705             l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
706             l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
707             l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
708             l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
709 
710              -- Bug 14336467 :: PRAKKUM :: 13/07/2012
711              SAVEPOINT OPER_VISIT_CREATION;
712 
713             Create_Departure_visit(
714               p_oper_param_rec         => l_oper_param_rec,
715               p_flight_schedule_rec    => l_flight_schedule_rec,
716               p_present_time           => l_present_time,
717               p_is_creation_success    => l_is_creation_success,
718               x_return_status          => l_return_status,
719               x_msg_count              => l_msg_count,
720               x_msg_data               => l_msg_data
721             );
722             IF (l_log_statement >= l_log_current_level) THEN
723                fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Departure_visit and l_return_status: '||l_return_status);
724                fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
725             END IF;
726 
727             IF l_is_creation_success<>'Y' THEN
728                ROLLBACK TO OPER_VISIT_CREATION;
729             END IF;
730 
731             -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
732 
733           END IF; --IF get_oper_param_rows1%FOUND THEN
734           CLOSE get_oper_param_rows1;--added by tchimira for bug 14008978 on 27-Apr-2012
735         END IF; --IF l_visit_type = 'N' THEN
736         -- commented the below line by tchimira for bug 14008978 on 27-Apr-2012
737         --CLOSE get_oper_param_rows1;
738         -- Go to arrival org consideration
739       END IF; --IF get_oper_param_rows1%FOUND for checking the possibility of creating a downtime visit
740 
741     ELSE -- existing visit is a downtime visit associated to the arrival org of preceding FS
742       -- check for the possibility to create a downtime visit
743       OPEN get_oper_param_rows1(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
744                                 pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code, l_count_prec_arr_org, l_count_prec_arr_dep, l_count_prec_arr_cat,
745                                 'DOWNTIME', l_ground_time);
746       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
747       IF get_oper_param_rows1%FOUND THEN
748         OPEN get_visit_type_duration (get_oper_param_rec.visit_type_code,get_oper_param_rec.mc_id);
749         FETCH get_visit_type_duration INTO l_visit_type_duration;
750         CLOSE get_visit_type_duration;
751         -- get the visit type code of the visit associated to the arrival org of prec FS
752         OPEN get_visit_type_code(l_visit_id2);
753         FETCH get_visit_type_code INTO l_visit_type_code;
754         CLOSE get_visit_type_code;
755         -- If we can create a downtime visit and existing visit is also a downtime visit, the check if visit type of the current visit matches with row visit type
756         IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
757 
758           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
759           SAVEPOINT OPER_VISIT_CREATION;
760 
761           --  if it does not match then cancel the existing downtime visit with id 'l_visit_id2'
762           --  create a downtime visit
763           -- TYPE 6: Departure Org :: cancel existing downtime visit :: create a downtime visit
764           IF (l_log_statement >= l_log_current_level) THEN
765               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 6');
766           END IF;
767           Delete_Oper_Visit(
768                p_visit_id       => l_visit_id2,
769                x_return_status  => l_return_status);
770           IF (l_log_statement >= l_log_current_level) THEN
771             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
772           END IF;
773           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
774           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
775           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
776 
777           -- we need to create a downtime visit and associate to the arrival org of preceeding flight, so pass preceeding flight details
778           Create_Downtime_visit(
779             p_oper_param_rec         => l_oper_param_rec,
780             p_flight_schedule_rec    => l_prec_flight_schedule_rec,
781             p_present_time           => l_present_time,
782             p_is_creation_success    => l_is_creation_success,
783             x_return_status          => l_return_status,
784             x_msg_count              => l_msg_count,
785             x_msg_data               => l_msg_data
786             );
787 
788           IF (l_log_statement >= l_log_current_level) THEN
789             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
790             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
791           END IF;
792 
793           IF l_is_creation_success<>'Y' THEN
794              ROLLBACK TO OPER_VISIT_CREATION;
795           END IF;
796           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
797 
798         END IF; -- If condition of comparing visit types: IF substr (l_visit_type,3) <> get_oper_param_rec.visit_type_code
799         -- Go to arrival org consideration
800       END IF;   --IF get_oper_param_rows1%FOUND THEN
801       CLOSE get_oper_param_rows1;
802     END IF; --if the existing visit is a downtime visit
803    END IF; -- IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
804   END IF; -- if there is any preceeding event
805 
806   IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
807     fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'CASE II, flight_schedule_rec.unit_schedule_id= '||flight_schedule_rec.unit_schedule_id );
808   END IF;
809   -- CASE II: Now consider the arrival org of the flight schedule record
810   OPEN get_non_null_org_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id);
811   FETCH get_non_null_org_count INTO l_count_org;
812   CLOSE get_non_null_org_count;
813 
814   OPEN get_non_null_dep_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
815                               flight_schedule_rec.arrival_dept_id);
816   FETCH get_non_null_dep_count INTO l_count_dep;
817   CLOSE get_non_null_dep_count;
818 
819   OPEN get_non_null_cat_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
820                               flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code);
821   FETCH get_non_null_cat_count INTO l_count_cat;
822   CLOSE get_non_null_cat_count;
823 
824   OPEN get_succeeding_us_det (flight_schedule_rec.unit_schedule_id);
825   FETCH get_succeeding_us_det INTO l_succeeding_us_id, l_succ_dep_time;
826   CLOSE get_succeeding_us_det;
827 
828   -- check if there exisits a visit and if yes, what visit type - either arrival or downtime
829   AHL_AVF_OPER_VSTS_PVT.Get_Visit_Type(flight_schedule_rec.unit_schedule_id, 'S', l_visit_type, l_visit_id,l_visit_id2);
830   IF (l_log_statement >= l_log_current_level) THEN
831     fnd_log.string(l_log_statement,L_DEBUG_KEY, ' l_succeeding_us_id--@>'||l_succeeding_us_id);
832   END IF;
833   --If the flight schedule does not have any succeeding flight then
834   IF ( l_succeeding_us_id IS NULL ) THEN
835     IF substr (l_visit_type,1,1) = 'A' THEN -- arrival visit associated to the arrival org of current FS
836       -- Go to end
837       IF (l_log_statement >= l_log_current_level) THEN
838         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' There is an existing arrival visit at arrival org, do nothing');
839       END IF;
840       NULL; --Do nothing :: Go to end
841     ELSIF l_visit_type = 'N' THEN -- no visit associated to the arrival org of current FS
842       -- check for the possibility of creating an arrival visit;
843       IF (l_log_statement >= l_log_current_level) THEN
844         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' flight_schedule_rec.unit_config_header_id--@>'||flight_schedule_rec.unit_config_header_id||
845                                                     ' ;flight_schedule_rec.arrival_org_id--@>'||flight_schedule_rec.arrival_org_id||
846                                                     ' ;flight_schedule_rec.arrival_dept_id--@>'||flight_schedule_rec.arrival_dept_id||
847                                                     ' ;flight_schedule_rec.flight_category_code--@>'||flight_schedule_rec.flight_category_code||
848                                                     ' ;l_count_org--@>'||l_count_org||
849                                                     ' ;l_count_dep--@>'||l_count_dep||
850                                                     ' ;l_count_cat--@>'||l_count_cat||
851                                                     ' ;create for--@>ARRIVAL');
852       END IF;
853       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
854                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
855                                 'ARRIVAL', NULL);
856       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
857       IF get_oper_param_rows1%FOUND THEN
858         --If there is a possibility , create an arrival visit
859         --TYPE 7: Arrival Org :: no visit to cancel :: create an arrival visit
860         IF (l_log_statement >= l_log_current_level) THEN
861           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 7 and before calling Create_Arrival_visit');
862         END IF;
863            l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
864            l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
865            l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
866            l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
867 
868           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
869           SAVEPOINT OPER_VISIT_CREATION;
870 
871           Create_Arrival_visit(
872             p_oper_param_rec       => l_oper_param_rec,
873             p_flight_schedule_rec  => l_flight_schedule_rec,
874             p_present_time         => l_present_time,
875             p_is_creation_success  => l_is_creation_success,
876             x_return_status        => l_return_status,
877             x_msg_count         => l_msg_count,
878             x_msg_data          => l_msg_data
879             );
880 
881           IF (l_log_statement >= l_log_current_level) THEN
882             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Arrival_visit and l_return_status: '||l_return_status);
883           END IF;
884 
885           IF l_is_creation_success<>'Y' THEN
886             ROLLBACK TO OPER_VISIT_CREATION;
887           END IF;
888           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
889 
890       END IF; -- IF get_oper_param_rows1%FOUND THEN
891       CLOSE get_oper_param_rows1;
892       -- Go to end
893     END If; --ELSIF l_visit_type = 'N'
894 
895   --If the flight schedule has any succeeding event then
896   ELSE
897    -- check if any of the arrival current flight visit or departure succeeding flight visit does not satisfy any of
898    -- the criteria to be automatically cancelled.
899    -- PRAKKUM :: Bug 13844759 :: 26/07/2012
900    load_can_cancel_visit(
901       p_visit_id               => l_visit_id,
902       p_visit_id2              => l_visit_id2,
903       x_can_cancel_visit       => l_can_cancel_visit,
904       x_can_cancel_visit2      => l_can_cancel_visit2,
905       x_return_status          => l_return_status,
906       x_msg_count              => l_msg_count,
907       x_msg_data               => l_msg_data
908       );
909    IF (l_log_statement >= l_log_current_level) THEN
910        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
911    END IF;
912 
913    IF (l_log_statement >= l_log_current_level) THEN
914         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' flight_schedule_rec.unit_config_header_id--@>'||flight_schedule_rec.unit_config_header_id||
915                                                     ' ;flight_schedule_rec.arrival_org_id--@>'||flight_schedule_rec.arrival_org_id||
916                                                     ' ;flight_schedule_rec.arrival_dept_id--@>'||flight_schedule_rec.arrival_dept_id||
917                                                     ' ;flight_schedule_rec.flight_category_code--@>'||flight_schedule_rec.flight_category_code||
918                                                     ' ;l_count_org--@>'||l_count_org||
919                                                     ' ;l_count_dep--@>'||l_count_dep||
920                                                     ' ;l_count_cat--@>'||l_count_cat||
921                                                     ' ;create for--@>ARRIVAL');
922    END IF;
923 
924 
925    -- if departure visit of succ flight can not be cancelled and there is no visit associated to arrival org of current FS
926    -- then look for the possibility of creating an arrival visit at arrival org of current FS
927    IF ((l_can_cancel_visit2 IS NULL) AND (l_visit_id IS NULL)) THEN
928       -- check for the possibility of creating an arrival visit;
929       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
930                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
931                                 'ARRIVAL', NULL);
932       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
933       IF get_oper_param_rows1%FOUND THEN
934         -- if there is possibility of creating an arrival visit
935         --TYPE 8: Arrival Org :: no visit to cancel :: create an arrival visit
936         IF (l_log_statement >= l_log_current_level) THEN
937           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 8 and before calling Create_Arrival_visit');
938         END IF;
939         l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
940         l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
941         l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
942         l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
943 
944           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
945           SAVEPOINT OPER_VISIT_CREATION;
946 
947           Create_Arrival_visit(
948             p_oper_param_rec       => l_oper_param_rec,
949             p_flight_schedule_rec  => l_flight_schedule_rec,
950             p_present_time         => l_present_time,
951             p_is_creation_success  => l_is_creation_success,
952             x_return_status        => l_return_status,
953             x_msg_count         => l_msg_count,
954             x_msg_data          => l_msg_data
955             );
956           IF (l_log_statement >= l_log_current_level) THEN
957             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Arrival_visit and l_return_status: '||l_return_status);
958             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
959           END IF;
960 
961           IF l_is_creation_success<>'Y' THEN
962              ROLLBACK TO OPER_VISIT_CREATION;
963           END IF;
964           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
965       END IF; -- IF get_oper_param_rows1%FOUND THEN
966       CLOSE get_oper_param_rows1;
967       -- Go to end
968    END IF; --IF ((l_can_cancel_visit2 IS NULL) AND (l_visit_id IS NULL)) THEN
969 
970    -- if both dep succ flight visit and arrival visit can be automatically cancelled
971    IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
972     --Get ground time at the arrival org of a FS
973     OPEN get_ground_time (flight_schedule_rec.UNIT_SCHEDULE_ID, l_succeeding_us_id);
974     FETCH get_ground_time INTO l_ground_time;
975     CLOSE get_ground_time;
976 
977     IF substr (l_visit_type,1,1) <> 'T' THEN -- either no visit or arrival visit associated to the arrival org of current FS
978       -- check for the possibility to create a downtime visit
979       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
980                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
981                                 'DOWNTIME', l_ground_time);
982       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
983       l_is_downtime_vst_created := 'Y';
984       IF get_oper_param_rows1%FOUND THEN -- if there is possibiliy of creating a downtime visit
985         IF l_visit_type = 'N' THEN   -- no visit associated to the arrival org of current FS
986            IF (l_log_statement >= l_log_current_level) THEN
987               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 9');
988            END IF;
989            --TYPE 9: Arrival Org :: no visit of current FS to cancel :: create a downtime visit
990            --cancel the departure visit of the succeeding flight if it is not null
991 
992            -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
993            SAVEPOINT OPER_VISIT_CREATION;
994 
995            IF(l_visit_id2 is not null) then
996                Delete_Oper_Visit(
997                   p_visit_id       => l_visit_id2,
998                   x_return_status  => l_return_status);
999            END IF;--IF(l_visit_id2 is not null) then
1000            IF (l_log_statement >= l_log_current_level) THEN
1001             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
1002            END IF;
1003            l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
1004            l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
1005            l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
1006 
1007            -- we need to create a downtime visit and associate to the arrival org of current flight, so pass current flight details
1008            Create_Downtime_visit(
1009             p_oper_param_rec         => l_oper_param_rec,
1010             p_flight_schedule_rec    => l_flight_schedule_rec,
1011             p_present_time           => l_present_time,
1012             p_is_creation_success    => l_is_creation_success,
1013             x_return_status          => l_return_status,
1014             x_msg_count              => l_msg_count,
1015             x_msg_data               => l_msg_data
1016             );
1017 
1018            IF (l_log_statement >= l_log_current_level) THEN
1019              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
1020              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
1021            END IF;
1022 
1023            IF l_is_creation_success<>'Y' THEN
1024               ROLLBACK TO OPER_VISIT_CREATION;
1025               l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating arrival visit
1026            END IF;
1027            -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
1028 
1029         ELSIF substr (l_visit_type,1,1) = 'A' THEN -- there is an arrival visit associated to the arrival org of current FS
1030           -- If we can create a downtime visit and existing visit is an arrival visit, the check if visit type of the current visit matches with row visit type
1031           -- get the visit type code of the arrival visit
1032           OPEN get_visit_type_code(l_visit_id);
1033           FETCH get_visit_type_code INTO l_visit_type_code;
1034           CLOSE get_visit_type_code;
1035           IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
1036             --  if it does not match, then cancel the arrival visit with id 'l_visit_id'
1037             -- and create a downtime visit
1038             IF (l_log_statement >= l_log_current_level) THEN
1039               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 10');
1040             END IF;
1041             --TYPE 10: Arrival Org :: cancel existing arrival visit :: create a downtime visit
1042              Delete_Oper_Visit(
1043                p_visit_id       => l_visit_id,
1044                x_return_status  => l_return_status);
1045 
1046             --cancel the departure visit of the succeeding flight if it is not null
1047             IF(l_visit_id2 is not null) then
1048              Delete_Oper_Visit(
1049                p_visit_id       => l_visit_id2,
1050                x_return_status  => l_return_status);
1051             END IF;--IF(l_visit_id2 is not null) then
1052             IF (l_log_statement >= l_log_current_level) THEN
1053              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
1054             END IF;
1055             l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
1056             l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
1057             l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
1058 
1059             -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
1060             SAVEPOINT OPER_VISIT_CREATION;
1061 
1062             -- we need to create a downtime visit and associate to the arrival org of current flight, so pass current flight details
1063             Create_Downtime_visit(
1064              p_oper_param_rec         => l_oper_param_rec,
1065              p_flight_schedule_rec    => l_flight_schedule_rec,
1066              p_present_time           => l_present_time,
1067              p_is_creation_success    => l_is_creation_success,
1068              x_return_status          => l_return_status,
1069              x_msg_count              => l_msg_count,
1070              x_msg_data               => l_msg_data
1071              );
1072             IF (l_log_statement >= l_log_current_level) THEN
1073               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
1074               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
1075             END IF;
1076 
1077             IF l_is_creation_success<>'Y' THEN
1078               ROLLBACK TO OPER_VISIT_CREATION;
1079               l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating arrival visit
1080             END IF;
1081             -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
1082           END IF; -- IF part of comparing visit types : IF substr (l_visit_type,3) <> get_oper_param_rec.visit_type_code
1083          -- Go to end
1084         END IF;  --IF l_visit_type = 'N' THEN
1085         CLOSE get_oper_param_rows1;
1086       ELSE --a downtime visit cannot be created
1087         CLOSE get_oper_param_rows1;
1088         l_is_downtime_vst_created := 'N';
1089       END IF;
1090       IF l_is_downtime_vst_created = 'N' THEN -- Downtime visit cannot be created, so check for availability of creating an arrival visit
1091         -- if there is no visit, then check for creation of arrival visit;
1092         IF l_visit_type = 'N' THEN -- no visit associated to the arrival org of current FS
1093           OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
1094                                     flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
1095                                     'ARRIVAL', NULL);
1096           FETCH get_oper_param_rows1 INTO get_oper_param_rec;
1097           IF get_oper_param_rows1%FOUND THEN
1098             --TYPE 11: Arrival Org :: no visit to cancel :: create an arrival visit
1099             IF (l_log_statement >= l_log_current_level) THEN
1100                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 11 and before calling Create_Arrival_visit');
1101             END IF;
1102             l_oper_param_rec.visit_type_code  := get_oper_param_rec.visit_type_code;
1103             l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
1104             l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
1105             l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
1106 
1107             -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
1108             SAVEPOINT OPER_VISIT_CREATION;
1109 
1110             Create_Arrival_visit(
1111               p_oper_param_rec       => l_oper_param_rec,
1112               p_flight_schedule_rec  => l_flight_schedule_rec,
1113               p_present_time         => l_present_time,
1114               p_is_creation_success  => l_is_creation_success,
1115               x_return_status        => l_return_status,
1116               x_msg_count         => l_msg_count,
1117               x_msg_data          => l_msg_data
1118             );
1119             IF (l_log_statement >= l_log_current_level) THEN
1120               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Arrival_visit and l_return_status: '||l_return_status);
1121               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
1122             END IF;
1123 
1124             IF l_is_creation_success<>'Y' THEN
1125                ROLLBACK TO OPER_VISIT_CREATION;
1126             END IF;
1127             -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
1128 
1129           END IF; -- IF get_oper_param_rows1%FOUND THEN
1130           CLOSE get_oper_param_rows1; --added by tchimira for the bug 14008978 on 27-Apr-2012
1131         END IF; -- IF l_visit_type = 'N' THEN
1132         -- commented the below line by tchimira for bug 14008978 on 27-Apr-2012
1133         --CLOSE get_oper_param_rows1;
1134       END IF; --IF get_oper_param_rows1%FOUND for checking the possibility of creating a downtime visit
1135 
1136     ELSE -- existing visit is a downtime visit
1137       -- check for the possibility to create a downtime visit
1138       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
1139                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
1140                                 'DOWNTIME', l_ground_time);
1141       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
1142       IF get_oper_param_rows1%FOUND THEN
1143         -- If we can create a downtime visit and existing visit is also a downtime visit, then check if visit type of the current visit matches with row visit type
1144         -- get the visit type code of the existing visit
1145         OPEN get_visit_type_code(l_visit_id);
1146         FETCH get_visit_type_code INTO l_visit_type_code;
1147         CLOSE get_visit_type_code;
1148         IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
1149           -- if it does not match then cancel the existing downtime visit with id 'l_visit_id'
1150           -- and create a downtime visit
1151           IF (l_log_statement >= l_log_current_level) THEN
1152               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 12 ');
1153           END IF;
1154           --TYPE 12: Arrival Org :: cancel existing downtime visit :: create a downtime visit
1155           Delete_Oper_Visit(
1156                p_visit_id       => l_visit_id,
1157                x_return_status  => l_return_status);
1158           IF (l_log_statement >= l_log_current_level) THEN
1159             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
1160           END IF;
1161           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
1162           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
1163           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
1164 
1165           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
1166           SAVEPOINT OPER_VISIT_CREATION;
1167 
1168           -- we need to create a downtime visit and associate to the arrival org of current flight, so pass current flight details
1169           Create_Downtime_visit(
1170             p_oper_param_rec         => l_oper_param_rec,
1171             p_flight_schedule_rec    => l_flight_schedule_rec,
1172             p_present_time           => l_present_time,
1173             p_is_creation_success    => l_is_creation_success,
1174             x_return_status          => l_return_status,
1175             x_msg_count              => l_msg_count,
1176             x_msg_data               => l_msg_data
1177             );
1178 
1179            IF (l_log_statement >= l_log_current_level) THEN
1180              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
1181              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
1182            END IF;
1183 
1184            IF l_is_creation_success<>'Y' THEN
1185               ROLLBACK TO OPER_VISIT_CREATION;
1186            END IF;
1187            -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
1188 
1189         END IF; -- IF substr (l_visit_type,3) <> get_oper_param_rec.visit_type_cod
1190         -- Go to end
1191       END IF; -- IF get_oper_param_rows1%FOUND THEN
1192       CLOSE get_oper_param_rows1;
1193     END IF; --IF substr (l_visit_type,1,1) <> 'T' THEN
1194    END IF;-- IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
1195   END IF; -- if there is any succeeding event
1196 
1197   --Update the Flight Schedule auto create status to 'R'
1198   UPDATE AHL_UNIT_SCHEDULES
1199   set AUTOVISIT_PROCESS_STATUS = 'R',
1200       OBJECT_VERSION_NUMBER = object_version_number + 1,
1201       LAST_UPDATE_DATE      = SYSDATE,
1202       LAST_UPDATED_BY       = Fnd_Global.USER_ID,
1203       LAST_UPDATE_LOGIN     = Fnd_Global.LOGIN_ID
1204   WHERE UNIT_SCHEDULE_ID    = flight_schedule_rec.UNIT_SCHEDULE_ID;
1205  END LOOP;
1206 
1207     ---------------------------End of Body-------------------------------------
1208     -- END of API body.
1209     -- Standard check of p_commit.
1210 
1211      IF Fnd_Api.To_Boolean (p_commit) THEN
1212         COMMIT WORK;
1213      END IF;
1214 
1215      Fnd_Msg_Pub.count_and_get(
1216            p_encoded => Fnd_Api.g_false,
1217            p_count   => x_msg_count,
1218            p_data    => x_msg_data
1219     );
1220 
1221     IF (l_log_procedure >= l_log_current_level) THEN
1222        fnd_log.string(l_log_procedure,
1223                       L_DEBUG_KEY ||'.end',
1224                       'At the end of PL SQL procedure. Return Status = ' || x_return_status);
1225     END IF;
1226 
1227 EXCEPTION
1228 
1229  WHEN FND_API.G_EXC_ERROR THEN
1230 
1231    x_return_status := FND_API.G_RET_STS_ERROR;
1232    ROLLBACK TO Create_Oper_Visit_pvt;
1233    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1234                               p_data  => x_msg_data,
1235                               p_encoded => fnd_api.g_false);
1236 
1237  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1238    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1239    ROLLBACK TO Create_Oper_Visit_pvt;
1240    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1241                               p_data  => x_msg_data,
1242                               p_encoded => fnd_api.g_false);
1243 
1244  WHEN OTHERS THEN
1245     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1246     ROLLBACK TO Create_Oper_Visit_pvt;
1247 
1248     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1249        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
1250                                p_procedure_name => 'Create_Oper_Visit',
1251                                p_error_text     => SUBSTR(SQLERRM,1,500));
1252     END IF;
1253 
1254     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1255                                p_data  => x_msg_data,
1256                                p_encoded => fnd_api.g_false);
1257 END Create_Oper_Visit;
1258 
1259 ---------------------------------------------------------------------------------------
1260 -- PROCEDURE
1261 --    Get_Visit_Type
1262 -- Type             : Public
1263 -- PURPOSE
1264 --    To find out whether visit associated to the organization is arrival or departure or downtime
1265 ----------------------------------------------------------------------------------------
1266 
1267 PROCEDURE Get_Visit_Type(
1268     p_fs_id       IN         NUMBER,
1269     p_fs_type     IN         VARCHAR2,
1270     x_vst_typ     OUT NOCOPY VARCHAR2,
1271     x_visit_id    OUT NOCOPY NUMBER,
1272     x_visit_id2   OUT NOCOPY NUMBER
1273     )
1274 IS
1275   -- To find out if there is any visit associated to the passed flight schedule
1276   -- Cursor to find our departure visit
1277   CURSOR c_is_visit_departure (c_fs_id IN NUMBER) IS
1278     SELECT visit.visit_type_code, visit.visit_id
1279     FROM ahl_visits_b visit, ahl_unit_schedules flight
1280     WHERE visit.unit_schedule_id = flight.unit_schedule_id
1281      AND visit.organization_id = flight.departure_org_id
1282      AND flight.unit_schedule_id = p_fs_id
1283      AND visit.status_code NOT IN ('DELETED','CANCELLED')
1284      AND ((NVL(visit.auto_visit_type_flag,'X') = 'D')--Departure --TCHIMIRA :: 14-Jun-2012 :: ER 14015560 ::added the condition
1285           OR (visit.auto_visit_type_flag IS NULL )); -- TCHIMIRA:: 28-Jun-2012 :: handle upgrade cases
1286 
1287   -- To find out if there is any visit associated to the passed flight schedule
1288   -- Cursor to find downtime visit at departure org
1289   CURSOR c_is_visit_downtime (c_fs_id IN NUMBER) IS
1290     SELECT visit.visit_type_code, visit.visit_id
1291     FROM ahl_visits_b visit, ahl_unit_schedules flight
1292     WHERE visit.unit_schedule_id = flight.preceding_us_id
1293       AND flight.unit_schedule_id = c_fs_id
1294       AND visit.organization_id = flight.departure_org_id
1295       AND visit.status_code NOT IN ('DELETED','CANCELLED')
1296       AND ((NVL(visit.auto_visit_type_flag,'X') = 'T')--Downtime --TCHIMIRA :: 14-Jun-2012 :: ER 14015560 ::added the condition
1297           OR (visit.auto_visit_type_flag IS NULL -- TCHIMIRA:: 28-Jun-2012 :: handle upgrade cases
1298 	     AND TO_CHAR(visit.close_date_time,'DD-MON-YYYY HH24:MI') = TO_CHAR(flight.est_departure_time,'DD-MON-YYYY HH24:MI')));
1299 
1300   -- To find out if there is any visit associated to the passed flight schedule
1301   -- Cursor to find visit at arrival org
1302   CURSOR c_is_visit_arrival_or_dwntm (c_fs_id IN NUMBER) IS
1303     SELECT visit.visit_type_code, visit.visit_id,visit.AUTO_VISIT_TYPE_FLAG
1304     FROM ahl_visits_b visit, ahl_unit_schedules flight
1305     WHERE visit.unit_schedule_id = flight.unit_schedule_id
1306      and visit.organization_id = flight.arrival_org_id
1307      and flight.unit_schedule_id = p_fs_id
1308      AND visit.status_code NOT IN ('DELETED','CANCELLED')
1309      AND ((NVL(visit.auto_visit_type_flag,'X') IN ('A', 'T'))--TCHIMIRA :: 14-Jun-2012 :: ER 14015560 ::added the condition
1310           OR (visit.auto_visit_type_flag IS NULL )); -- TCHIMIRA:: 28-Jun-2012 :: handle upgrade cases
1311 
1312   -- Cursor to find visit at arrival org if AUTO_VISIT_TYPE_FLAG for the visit is null
1313   CURSOR c_is_visit_arr_or_dwntm_old (c_fs_id IN NUMBER) IS
1314     SELECT succ_flight.est_departure_time, visit.close_date_time, visit.visit_type_code, visit.visit_id
1315     FROM ahl_visits_b visit, ahl_unit_schedules flight, ahl_unit_schedules succ_flight
1316     WHERE visit.unit_schedule_id = flight.unit_schedule_id
1317      and visit.organization_id = flight.arrival_org_id
1318      and succ_flight.preceding_us_id(+) = flight.unit_schedule_id
1319      and flight.unit_schedule_id = p_fs_id
1320      AND visit.status_code NOT IN ('DELETED','CANCELLED')
1321      AND flight.est_arrival_time = visit.start_date_time; --JROTICH :: 11-July-2012 added this condition to rule out same org departure visits
1322 
1323   -- Cursor to find if there is any arrival visit associated to the preceeding flight
1324   Cursor c_get_arr_vst_of_prec_flt (c_fs_id IN NUMBER)
1325   IS
1326     SELECT visit.visit_id
1327     FROM ahl_visits_b visit, ahl_unit_schedules flight, ahl_unit_schedules pre_flight
1328     WHERE visit.unit_schedule_id = pre_flight.unit_schedule_id
1329       AND flight.unit_schedule_id = c_fs_id
1330       AND flight.preceding_us_id  = pre_flight.unit_schedule_id
1331       AND visit.organization_id = flight.departure_org_id
1332       AND visit.status_code NOT IN ('DELETED','CANCELLED')
1333       AND ((NVL(visit.auto_visit_type_flag,'X') = 'A')--TCHIMIRA :: 14-Jun-2012 :: ER 14015560 ::added the condition
1334            OR (visit.auto_visit_type_flag IS NULL
1335            AND flight.est_departure_time <> visit.close_date_time)); -- TCHIMIRA:: 28-Jun-2012 :: handle upgrade cases
1336 
1337   -- Cursor to find if there is any departure visit associated to the succeeding flight
1338   Cursor c_get_dep_vst_of_succ_flt (c_fs_id IN NUMBER)
1339   IS
1340     SELECT visit.visit_id
1341     FROM ahl_visits_b visit, ahl_unit_schedules flight, ahl_unit_schedules succ_flight
1342     WHERE visit.unit_schedule_id = succ_flight.unit_schedule_id
1343      and visit.organization_id = flight.arrival_org_id
1344      and succ_flight.preceding_us_id = flight.unit_schedule_id
1345      and flight.unit_schedule_id = p_fs_id
1346      AND visit.status_code NOT IN ('DELETED','CANCELLED')
1347      AND ((NVL(visit.auto_visit_type_flag,'X') = 'D')--TCHIMIRA :: 14-Jun-2012 :: ER 14015560 ::added the condition
1348             OR (visit.auto_visit_type_flag IS NULL
1349             AND TO_CHAR(succ_flight.est_departure_time,'DD-MON-YYYY HH24:MI') = TO_CHAR(visit.close_date_time,'DD-MON-YYYY HH24:MI')
1350             AND flight.est_arrival_time <> visit.start_date_time )); -- TCHIMIRA:: 28-Jun-2012 :: handle upgrade cases
1351 
1352   l_auto_visit_type_flag    VARCHAR2(1); --TCHIMIRA :: 14-Jun-2012 :: ER 14015560
1353   l_visit_type         VARCHAR2(40):= 'N'; -- No visit associated
1354   L_API_NAME           CONSTANT VARCHAR2(30) := 'Get_Visit_Type';
1355   L_DEBUG_KEY          CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
1356   l_vt_code         VARCHAR2(30);
1357   l_succ_flight_dep_date DATE;
1358   l_visit_end_date       DATE;
1359 
1360 BEGIN
1361 
1362    IF (l_log_procedure >= l_log_current_level) THEN
1363       fnd_log.string(l_log_procedure,
1364                      L_DEBUG_KEY ||'.begin',
1365                      'At the start of PL SQL function.');
1366    END IF;
1367 
1368    IF (l_log_statement >= l_log_current_level) THEN
1369       fnd_log.string(l_log_statement,L_DEBUG_KEY, ' Flight schedule id: ' || p_fs_id||' , type: '||p_fs_type);
1370    END IF;
1371 
1372    IF p_fs_type = 'P' THEN  -- we are processing visits associated to the departure org of the flight schedule
1373      OPEN c_is_visit_departure (p_fs_id);
1374      FETCH c_is_visit_departure INTO l_vt_code, x_visit_id;
1375      IF c_is_visit_departure%FOUND THEN
1376        l_visit_type := 'D' || l_vt_code; --It is Departure visit
1377      ELSE
1378        OPEN c_is_visit_downtime (p_fs_id);
1379        FETCH c_is_visit_downtime INTO l_vt_code, x_visit_id2; --downtime visit ID
1380        IF c_is_visit_downtime%FOUND THEN
1381          l_visit_type := 'T' || l_vt_code; --It is Downtime visit
1382        ELSE
1383          l_visit_type := 'N'; -- there is no visit
1384        END IF;
1385        CLOSE c_is_visit_downtime;
1386      END IF;
1387      CLOSE c_is_visit_departure;
1388      -- if there is no downtime visit associated to the arrival org of prec FS,
1389      -- then check if there is an arrival visit.
1390      IF x_visit_id2 is null then
1391       OPEN c_get_arr_vst_of_prec_flt (p_fs_id);
1392       FETCH c_get_arr_vst_of_prec_flt INTO x_visit_id2; --arrival visit ID
1393       CLOSE c_get_arr_vst_of_prec_flt;
1394      END IF;
1395 
1396    ELSIF p_fs_type = 'S' THEN  -- we are processing visits associated to the arrival org of the flight schedule
1397      OPEN c_is_visit_arrival_or_dwntm (p_fs_id);
1398      FETCH c_is_visit_arrival_or_dwntm INTO l_vt_code, x_visit_id,l_auto_visit_type_flag;
1399      IF c_is_visit_arrival_or_dwntm%FOUND THEN
1400          l_visit_type := l_auto_visit_type_flag || l_vt_code;
1401      ELSE
1402         l_visit_type := 'N'; -- there is no visit
1403      END IF;
1404      CLOSE c_is_visit_arrival_or_dwntm;
1405 
1406      -- the following code handles cases where auto visit type flag is null;
1407      -- if the flag is null, the get assoicated visits based on time and organization match
1408      IF l_auto_visit_type_flag IS NULL THEN
1409        OPEN c_is_visit_arr_or_dwntm_old (p_fs_id);
1410        FETCH c_is_visit_arr_or_dwntm_old INTO l_succ_flight_dep_date, l_visit_end_date, l_vt_code, x_visit_id;
1411        IF c_is_visit_arr_or_dwntm_old%FOUND THEN
1412          IF TO_CHAR(l_succ_flight_dep_date,'DD-MON-YYYY HH24:MI') = TO_CHAR(l_visit_end_date,'DD-MON-YYYY HH24:MI') THEN
1413            l_visit_type := 'T ' || l_vt_code;--It is Downtime visit
1414          ELSE
1415            l_visit_type := 'A ' || l_vt_code;--It is arrival visit
1416          END IF;
1417        ELSE
1418           l_visit_type := 'N'; -- there is no visit
1419        END IF;
1420        CLOSE c_is_visit_arr_or_dwntm_old;
1421      END IF;
1422 
1423      -- if there is no downtime visit associated to the arrival org of current FS,
1424      -- then check if there is an dep visit at succ FS.
1425      IF NVL(l_auto_visit_type_flag, 'X') <> 'T' THEN
1426       OPEN c_get_dep_vst_of_succ_flt (p_fs_id);
1427       FETCH c_get_dep_vst_of_succ_flt INTO x_visit_id2;
1428       CLOSE c_get_dep_vst_of_succ_flt;
1429      END IF;
1430    END IF;
1431 
1432    IF (l_log_statement >= l_log_current_level) THEN
1433       fnd_log.string(l_log_statement,L_DEBUG_KEY, ' l_visit_type = ' || l_visit_type||',x_visit_id: '||x_visit_id||', x_visit_id2: '||x_visit_id2);
1434    END IF;
1435 
1436    IF (l_log_procedure >= l_log_current_level) THEN
1437       fnd_log.string(l_log_procedure,
1438                      L_DEBUG_KEY ||'.end',
1439                      'At the end of PL SQL function.');
1440    END IF;
1441    x_vst_typ := l_visit_type;
1442 
1443 END Get_Visit_Type;
1444 
1445 ----------------------------------------------------------------------------------------------------------------
1446 --  Procedure name    : Update_Oper_Visit
1447 --  Type              : Private
1448 --  Function          : Procedure to Update visit based on operational params
1449 --  Parameters  :
1450 --
1451 --  Standard IN  Parameters :
1452 --      p_api_version      IN  NUMBER        Required
1453 --      p_init_msg_list    IN  VARCHAR2      Default  FND_API.G_FALSE
1454 --      p_validation_level IN  NUMBER        Default  FND_API.G_VALID_LEVEL_FULL
1455 --
1456 --  Standard OUT Parameters :
1457 --      x_return_status    OUT VARCHAR2      Required
1458 --      x_msg_count        OUT NUMBER        Required
1459 --      x_msg_data         OUT VARCHAR2      Required
1460 --------------------------------------------------------------------------------------------------------------------
1461 PROCEDURE Update_Oper_Visit (
1462     p_api_version           IN             NUMBER    := 1.0,
1463     p_init_msg_list         IN             VARCHAR2  := FND_API.G_FALSE,
1464     p_commit                IN             VARCHAR2  := FND_API.G_FALSE,
1465     p_validation_level      IN             NUMBER    := FND_API.G_VALID_LEVEL_FULL,
1466     x_return_status         OUT NOCOPY     VARCHAR2,
1467     x_msg_count             OUT NOCOPY     NUMBER,
1468     x_msg_data              OUT NOCOPY     VARCHAR2)IS
1469 
1470 -- Local Variables
1471 
1472 -- Standard in/out parameters
1473 l_api_name                    VARCHAR2(30) := 'Update_Oper_Visit';
1474 l_api_version                 NUMBER       := 1.0;
1475 l_debug_key          CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
1476 l_msg_count                   NUMBER;
1477 l_msg_data                    VARCHAR2(2000);
1478 l_return_status               VARCHAR2(1);
1479 l_init_msg_list               VARCHAR2(10):= p_init_msg_list;
1480 l_visit_type                  VARCHAR2(40);
1481 l_visit_id                    NUMBER;
1482 l_visit_rec                   AHL_VWP_VISITS_PVT.Visit_Rec_Type;
1483 l_start_date                  DATE;
1484 l_end_date                    DATE;
1485 l_visit_type_duration         NUMBER;
1486 l_visit_id2                   NUMBER;
1487 l_visit_end_time              DATE;
1488 l_visit_type_code             VARCHAR2(30);
1489 l_flag                        VARCHAR2(1) := 'N';
1490 l_visit_start_time            DATE;
1491 l_difference_dep_time         NUMBER;
1492 l_difference_arrival_time     NUMBER;
1493 l_profile_varchar             VARCHAR2(100);
1494 l_profile_number              NUMBER;
1495 l_profile_varchar2            VARCHAR2(100);
1496 l_profile_number2             NUMBER;
1497 l_count_prec_arr_org          NUMBER;
1498 l_count_prec_arr_dep          NUMBER;
1499 l_count_prec_arr_cat          NUMBER;
1500 x_item_key                    VARCHAR2(100);
1501 l_can_cancel_visit            VARCHAR2(1);
1502 l_can_cancel_visit2           VARCHAR2(1);
1503 l_can_update_visit            VARCHAR2(1); -- added by tchimira for bug 13844759
1504 l_flight_schedule_rec         flight_schedule_rec_type;
1505 l_oper_param_rec              oper_param_rec_type;
1506 l_prec_flight_schedule_rec    flight_schedule_rec_type;
1507 l_present_time                DATE := SYSDATE; -- Bug 14336467 :: PRAKKUM :: 13/07/2012
1508 l_is_creation_success         VARCHAR2(1) := 'Y'; -- Bug 14336467 :: PRAKKUM :: 13/07/2012
1509 l_is_downtime_vst_created     VARCHAR2(1) := 'Y'; -- Bug 14336467 :: PRAKKUM :: 13/07/2012
1510 l_can_disconnect_visit        VARCHAR2(1) := 'N'; -- Bug 14368696 :: PRAKKUM :: 01/08/2012
1511 
1512 -- Cursor to find the visit numbers from visit_ids
1513 CURSOR get_visit_number(c_visit_id IN NUMBER) IS
1514 SELECT visit_number
1515 FROM ahl_visits_b
1516 WHERE visit_id = c_visit_id;
1517 
1518 -- Cursor to get all the flight schedule records whose status is 'Updated'
1519 Cursor get_flight_schedule_rows(c_status IN VARCHAR2)
1520 IS
1521 SELECT *
1522 FROM AHL_UNIT_SCHEDULES
1523 WHERE AUTOVISIT_PROCESS_STATUS = c_status;
1524 
1525 --Get the count of rows matching with the UC and org of the flight schedule and enabled_flag as Y
1526 Cursor get_non_null_org_count (c_uc_id IN NUMBER, c_org_id IN NUMBER)
1527 IS
1528 SELECT count (*)
1529 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
1530 WHERE op.enabled_flag = 'Y'
1531  AND uc.unit_config_header_id = c_uc_id
1532  AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
1533  AND op.organization_id = c_org_id;
1534  l_count_org NUMBER;
1535 
1536 --Get the count of rows matching with the UC, org and dep of the flight schedule and enabled_flag as Y
1537 Cursor get_non_null_dep_count (c_uc_id IN NUMBER, c_org_id IN NUMBER, c_dep_id IN NUMBER)
1538 IS
1539 SELECT count (*)
1540 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
1541 WHERE op.enabled_flag = 'Y'
1542  AND uc.unit_config_header_id = c_uc_id
1543  AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
1544  AND op.organization_id = c_org_id
1545  AND op.department_id = c_dep_id;
1546  l_count_dep NUMBER;
1547 
1548 --Get the count of rows matching with the UC, org, dep and category of the flight schedule and enabled_flag as Y
1549 Cursor get_non_null_cat_count (c_uc_id IN NUMBER, c_org_id IN NUMBER, c_dep_id IN NUMBER, c_flight_cat IN VARCHAR2)
1550 IS
1551 SELECT count (*)
1552 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
1553 WHERE op.enabled_flag = 'Y'
1554  AND uc.unit_config_header_id = c_uc_id
1555  AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
1556  AND op.organization_id = c_org_id
1557  AND op.department_id = c_dep_id
1558  AND op.flight_category_code = c_flight_cat;
1559  l_count_cat NUMBER;
1560 
1561 -- Get the ground time (in number of hours) between two flight schedules
1562 Cursor get_ground_time (c_prev_fs_id IN NUMBER, c_succ_fs_id IN NUMBER)
1563 IS
1564 SELECT to_number((succ_fs.EST_DEPARTURE_TIME - prev_fs.EST_ARRIVAL_TIME)*24*60)
1565 FROM AHL_UNIT_SCHEDULES prev_fs, AHL_UNIT_SCHEDULES succ_fs
1566 WHERE prev_fs.UNIT_SCHEDULE_ID = c_prev_fs_id
1567  AND succ_fs.UNIT_SCHEDULE_ID = c_succ_fs_id;
1568  l_ground_time NUMBER;
1569 
1570 --Cursor to fetch operational param rows whose enabled flag is Y and matches master config
1571 Cursor get_oper_param_rows1(c_uc_id IN NUMBER, c_org_id IN NUMBER, c_dep_id IN NUMBER, c_cat_code IN VARCHAR2,
1572                             c_org_count IN NUMBER, c_dep_count IN NUMBER, c_cat_count IN NUMBER,
1573                             c_create_for IN VARCHAR2, c_ground_time IN NUMBER)
1574 IS
1575 SELECT op.*
1576 FROM ahl_oper_vst_autocreate op, ahl_unit_config_headers uc
1577  WHERE op.enabled_flag = 'Y'
1578    AND uc.unit_config_header_id = c_uc_id
1579    AND (select mc_id from ahl_mc_headers_b where mc_header_id = uc.master_config_id) = op.mc_id
1580    AND ( (op.organization_id = c_org_id)
1581         OR ( c_org_count = 0 -- there are no non-null org rows meeting mc and enabled_flag
1582              AND op.organization_id IS NULL))
1583    AND ( ( c_org_count = 0 )
1584             OR (op.department_id = c_dep_id)
1585             OR ( c_dep_count = 0
1586                  AND op.department_id IS NULL))
1587    AND ( (op.flight_category_code = c_cat_code)
1588             OR ( ((c_cat_count = 0) OR ( c_cat_code IS NULL))
1589                  AND op.flight_category_code IS NULL))
1590    AND op.create_for = c_create_for
1591    -- if create_for is not downtime, pass null as c_ground_time to this cursor
1592    AND ( (c_ground_time IS NULL)
1593          OR (c_ground_time BETWEEN op.START_TIME AND NVL(op.END_TIME,c_ground_time)));
1594 get_oper_param_rec get_oper_param_rows1%ROWTYPE;
1595 
1596 --curosr that checks if there is any succeeding flight for this flight schedule
1597 Cursor get_succeeding_us_det (c_fs_id IN NUMBER)
1598  IS
1599  SELECT UNIT_SCHEDULE_ID, EST_DEPARTURE_TIME FROM AHL_UNIT_SCHEDULES
1600  WHERE  preceding_us_id = c_fs_id;
1601 l_succeeding_us_id NUMBER;
1602 l_succ_dep_time DATE;
1603 
1604 Cursor get_visit_type_duration(c_visit_type_code IN VARCHAR2, c_mc_id IN NUMBER)
1605 IS
1606 SELECT estimated_duration
1607 FROM ahl_visit_types_b
1608 WHERE visit_type_code = c_visit_type_code
1609  AND mc_id = c_mc_id
1610  AND status_code = 'COMPLETE';
1611 
1612 Cursor get_fs_arr_time (c_fs_id IN NUMBER)
1613 IS
1614 SELECT EST_ARRIVAL_TIME
1615 FROM AHL_UNIT_SCHEDULES
1616 WHERE UNIT_SCHEDULE_ID = c_fs_id;
1617 
1618 -- Cursor to get all the associated visits for the passed flight schedule
1619 Cursor get_assoc_visits(c_flight_schedule_id IN NUMBER)
1620 IS
1621 SELECT visit_id, organization_id, locked_flag, firmed_flag, status_code, start_date_time, close_date_time,AUTO_VISIT_TYPE_FLAG
1622 FROM AHL_VISITS_B
1623 WHERE UNIT_SCHEDULE_ID = C_FLIGHT_SCHEDULE_ID
1624 AND status_code NOT IN ('DELETED','CANCELLED')
1625 ORDER BY visit_id asc; --added this condition by tchimira for bug 13828335
1626 
1627 -- Cursor to get visit end time
1628 Cursor get_visit_end_time(c_visit_id IN NUMBER)
1629 IS
1630 SELECT close_date_time
1631 FROM AHL_VISITS_B
1632 WHERE VISIT_ID = c_visit_id;
1633 
1634 -- Cursor to get preceeding flight schedule details
1635 Cursor get_pre_fs_det(c_unit_schedule_id IN NUMBER)
1636 IS
1637 SELECT *
1638 FROM AHL_UNIT_SCHEDULES
1639 WHERE UNIT_SCHEDULE_ID = c_unit_schedule_id;
1640 pre_fs_det_rec get_pre_fs_det%ROWTYPE;
1641 
1642 -- Cursor to get visit type of a visit
1643 Cursor get_visit_type_code(c_visit_id IN NUMBER)
1644 IS
1645 SELECT visit_type_code
1646 FROM AHL_VISITS_B
1647 WHERE VISIT_ID = c_visit_id;
1648 
1649 -- Cursor to get visit start time
1650 Cursor get_visit_start_time(c_visit_id IN NUMBER)
1651 IS
1652 SELECT start_date_time
1653 FROM AHL_VISITS_B
1654 WHERE VISIT_ID = c_visit_id;
1655 
1656 -- To find visit related information
1657 CURSOR c_Visit(c_visit_id IN NUMBER) IS
1658 SELECT * FROM   Ahl_Visits_VL
1659 WHERE  VISIT_ID = c_visit_id;
1660 
1661 c_Visit_rec    c_Visit%ROWTYPE;
1662 
1663 --To get visit type duration of a given visit
1664 --Modified the below cursor by tchimira as a fix for issue 1 in the bug 13777327 on 01-Mar-2012
1665 CURSOR c_Visit_type_dur(c_visit_id IN NUMBER) IS
1666 SELECT vtyp.estimated_duration
1667 FROM  ahl_visit_types_b vtyp, ahl_visits_b visit, ahl_unit_config_headers unit, ahl_mc_headers_b mc
1668 WHERE  visit.visit_id = c_visit_id
1669  AND vtyp.visit_type_code = visit.visit_type_code
1670  AND unit.csi_item_instance_id = visit.item_instance_id
1671  AND unit.master_config_id = mc.mc_header_id
1672  AND vtyp.mc_id = mc.mc_id
1673  AND vtyp.status_code = 'COMPLETE';
1674 
1675 --Cursor to find if the visit is in planning status and not firmed and not locked
1676 -- if all the conditions match, then the visit can be updated or cancelled
1677 Cursor can_cancel_visit(c_visit_id IN NUMBER)
1678 IS
1679 SELECT 'X'
1680 FROM AHL_VISITS_B
1681 WHERE VISIT_ID = c_visit_id
1682  AND STATUS_CODE = 'PLANNING'
1683  AND NVL(FIRMED_FLAG,'N') <> 'Y'
1684  AND NVL(LOCKED_FLAG,'N') <> 'Y';
1685 
1686 Cursor is_org_in_current_OU (c_org_id IN NUMBER)
1687 IS
1688 SELECT 'X'
1689 FROM org_organization_definitions hou
1690 WHERE hou.organization_id                                  = c_org_id
1691 AND NVL(hou.operating_unit,mo_global.get_current_org_id()) = mo_global.get_current_org_id();
1692 l_is_org_in_curr_OU VARCHAR2(1);
1693 
1694 BEGIN
1695 
1696  IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)THEN
1697       fnd_log.string
1698       (
1699         fnd_log.level_procedure,
1700        'ahl.plsql.AHL_AVF_OPER_VSTS_PVT.Update_Oper_Visit.begin',
1701        'At the start of PLSQL procedure'
1702       );
1703  END IF;
1704 
1705  -- Standard start of API savepoint
1706  SAVEPOINT Update_Oper_Visit_pvt;
1707 
1708  -- Initialize message list if p_init_msg_list is set to TRUE
1709 
1710  IF FND_API.To_Boolean( p_init_msg_list) THEN
1711    FND_MSG_PUB.Initialize;
1712  END IF;
1713 
1714  -- Initialize API return status to success
1715  x_return_status := FND_API.G_RET_STS_SUCCESS;
1716 
1717  -- Standard call to check for call compatibility.
1718  IF NOT Fnd_Api.COMPATIBLE_API_CALL(l_api_version,
1719                                     p_api_version,
1720                                     l_api_name,G_PKG_NAME)
1721  THEN
1722    RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
1723  END IF;
1724 
1725  l_profile_varchar :=  FND_PROFILE.VALUE('AHL_AVF_VST_UPD_FLT_TIME');
1726  l_profile_varchar2 :=  FND_PROFILE.VALUE('AHL_AVF_FLT_VST_UPD_WINDOW');
1727 
1728  -- Loop through all the flight schedule records whose status is Updated.
1729  FOR flight_schedule_rec IN get_flight_schedule_rows('U') LOOP
1730   -- store all the flight params in the record
1731   l_flight_schedule_rec.unit_config_header_id := flight_schedule_rec.unit_config_header_id;
1732   l_flight_schedule_rec.departure_org_id := flight_schedule_rec.departure_org_id;
1733   l_flight_schedule_rec.departure_dept_id := flight_schedule_rec.departure_dept_id;
1734   l_flight_schedule_rec.unit_schedule_id := flight_schedule_rec.unit_schedule_id;
1735   l_flight_schedule_rec.est_departure_time := flight_schedule_rec.est_departure_time;
1736   l_flight_schedule_rec.arrival_org_id := flight_schedule_rec.arrival_org_id;
1737   l_flight_schedule_rec.arrival_dept_id := flight_schedule_rec.arrival_dept_id;
1738   l_flight_schedule_rec.est_arrival_time := flight_schedule_rec.est_arrival_time;
1739 
1740   -- For each flight schedule, get all the visits IDs associated
1741   FOR assoc_visits_rec IN get_assoc_visits(flight_schedule_rec.UNIT_SCHEDULE_ID) LOOP
1742       IF (l_log_statement >= l_log_current_level) THEN
1743         fnd_log.string(l_log_statement,L_DEBUG_KEY, 'assoc_visits_rec.visit_id is: '||assoc_visits_rec.visit_id||
1744         ', assoc_visits_rec.AUTO_VISIT_TYPE_FLAG: '||assoc_visits_rec.AUTO_VISIT_TYPE_FLAG||', assoc_visits_rec.organization_id: '||assoc_visits_rec.organization_id
1745         ||', flight_schedule_rec.departure_org_id: '||flight_schedule_rec.departure_org_id||', flight_schedule_rec.arrival_org_id: '||flight_schedule_rec.arrival_org_id);
1746       END IF;
1747 
1748 
1749     -- For the visit fetched above, see if the visits organization matches with either departing or arrival org
1750     -- of the flight schedule;
1751     IF (((assoc_visits_rec.organization_id <> flight_schedule_rec.departure_org_id) AND (assoc_visits_rec.AUTO_VISIT_TYPE_FLAG = 'D'))
1752             OR ((assoc_visits_rec.organization_id <> flight_schedule_rec.arrival_org_id) AND (assoc_visits_rec.AUTO_VISIT_TYPE_FLAG IN ('A', 'T'))))
1753       THEN
1754 
1755       --If there is no match then cancel the visit;
1756       --Cancel the visit only if it is not firm or locked and is in planning status
1757       IF(assoc_visits_rec.locked_flag <> 'Y' AND assoc_visits_rec.firmed_flag <> 'Y' AND assoc_visits_rec.status_code = 'PLANNING') THEN
1758         Delete_Oper_Visit(
1759                p_visit_id       => assoc_visits_rec.visit_id,
1760                x_return_status  => l_return_status);
1761 
1762       -- If the visit is either firm/locked or not in planning status then disassociate the visit from the flight schedule
1763       ELSE
1764         --PRAKKUM :: 01/08/2012 :: Added procedure :: START
1765         IF (l_log_statement >= l_log_current_level) THEN
1766           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'inside else assoc_visits_rec.visit_id is: '||assoc_visits_rec.visit_id);
1767         END IF;
1768 
1769         Disconnect_Flight_Visit(
1770             p_visit_id               => assoc_visits_rec.visit_id,
1771             p_unit_schedule_id       => flight_schedule_rec.UNIT_SCHEDULE_ID,
1772             x_return_status          => l_return_status,
1773             x_msg_count              => l_msg_count,
1774             x_msg_data               => l_msg_data
1775           );
1776         IF (l_log_statement >= l_log_current_level) THEN
1777            fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
1778         END IF;
1779 
1780         IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
1781          x_msg_count := l_msg_count;
1782          x_return_status := l_return_status;
1783          IF l_return_status = Fnd_Api.g_ret_sts_error THEN
1784            RAISE Fnd_Api.g_exc_error;
1785          ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
1786            RAISE Fnd_Api.g_exc_unexpected_error;
1787          END IF;
1788         END IF;
1789        --PRAKKUM :: 01/08/2012 :: Added procedure :: END
1790       END IF; -- Check before cancelling the visit
1791     END IF; --IF condition to compare the organization of the visit with FS orgs
1792   END LOOP; -- Loop of all the visits associated to the FS
1793 
1794   -- Now we have handled all the scenarios where the organization of the FS is updated. Next step is to look into
1795   -- the departure and arrival timings update
1796   -- CASE I: First consider the departing org of the flight schedule record
1797   IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
1798     fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'CASE 1, flight_schedule_rec.unit_schedule_id= '||flight_schedule_rec.unit_schedule_id );
1799   END IF;
1800 
1801   OPEN get_non_null_org_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id);
1802   FETCH get_non_null_org_count INTO l_count_org;
1803   CLOSE get_non_null_org_count;
1804 
1805   OPEN get_non_null_dep_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
1806                               flight_schedule_rec.departure_dept_id);
1807   FETCH get_non_null_dep_count INTO l_count_dep;
1808   CLOSE get_non_null_dep_count;
1809 
1810   OPEN get_non_null_cat_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
1811                               flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code);
1812   FETCH get_non_null_cat_count INTO l_count_cat;
1813   CLOSE get_non_null_cat_count;
1814 
1815 
1816   -- check if there exisits a visit and if yes, get the visit details
1817   AHL_AVF_OPER_VSTS_PVT.Get_Visit_Type(flight_schedule_rec.unit_schedule_id, 'P', l_visit_type, l_visit_id,l_visit_id2);
1818 
1819   --If the flight schedule does not have any preceeding event then
1820   IF ( flight_schedule_rec.preceding_us_id IS NULL ) THEN
1821     IF substr (l_visit_type,1,1) = 'D' THEN --there is a visit at the departing org of the current FS
1822       IF (l_log_statement >= l_log_current_level) THEN
1823         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' There is an existing departure visit at dep org, do nothing');
1824       END IF;
1825       -- based on the profile value see if you can adjust the visit dates
1826       OPEN get_visit_end_time(l_visit_id);
1827       FETCH get_visit_end_time INTO l_visit_end_time;
1828       CLOSE get_visit_end_time;
1829 
1830       OPEN c_Visit_type_dur (l_visit_id);
1831       FETCH c_Visit_type_dur INTO l_visit_type_duration;
1832       CLOSE c_Visit_type_dur;
1833       -- Added the below code to see if the visit can be updated or not : tchimira: bug 13844759
1834       l_can_update_visit := null;
1835       OPEN can_cancel_visit(l_visit_id);
1836       FETCH can_cancel_visit INTO l_can_update_visit;
1837       CLOSE can_cancel_visit;
1838 
1839       IF (l_log_statement >= l_log_current_level) THEN
1840         fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_visit_end_time is: '||l_visit_end_time||' ,est_departure_time: '||flight_schedule_rec.est_departure_time||', l_can_update_visit is: '||l_can_update_visit);
1841       END IF;
1842 
1843       IF flight_schedule_rec.est_departure_time > l_visit_end_time THEN
1844         l_difference_dep_time := (flight_schedule_rec.est_departure_time - l_visit_end_time)*24*60;
1845       ELSE
1846         l_difference_dep_time := (l_visit_end_time - flight_schedule_rec.est_departure_time)*24*60;
1847       END IF;
1848 
1849       IF (l_log_statement >= l_log_current_level) THEN
1850         fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_difference_dep_time is: '||l_difference_dep_time);
1851       END IF;
1852 
1853       IF  l_profile_varchar is not null  THEN
1854       l_profile_number := to_number(l_profile_varchar) ;
1855       ELSE
1856       l_profile_number := l_difference_dep_time-1;
1857       END IF;
1858 
1859       IF  l_profile_varchar2 is not null THEN
1860       l_profile_number2 := to_number(l_profile_varchar2) ;
1861       ELSE
1862       l_profile_number2 := flight_schedule_rec.est_departure_time-sysdate+1;
1863       END IF;
1864 
1865       IF((l_difference_dep_time > l_profile_number)
1866          AND (l_difference_dep_time <> 0)
1867          AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_departure_time) ) THEN
1868        -- TCHIMIRA : Bug 13844759 : check if the visit is updateable.
1869        -- if yes, the update the visit else diconnect the visit
1870        IF l_can_update_visit IS NOT NULL THEN
1871         IF (l_log_statement >= l_log_current_level) THEN
1872            fnd_log.string(l_log_statement,L_DEBUG_KEY, ' Before type 1 ');
1873         END IF;
1874         --TYPE 1: Departure Org :: dep visit at dep org of current FS :: Update the departure visit dates
1875         l_start_date := (flight_schedule_rec.est_departure_time - l_visit_type_duration/24);
1876         l_end_date := flight_schedule_rec.est_departure_time;
1877 
1878         l_visit_rec := null;
1879         l_visit_rec.START_DATE            := l_start_date;
1880         l_visit_rec.PLAN_END_DATE         := l_end_date;
1881         l_visit_rec.visit_id              := l_visit_id;
1882 
1883         OPEN c_Visit(l_visit_id);
1884         FETCH c_Visit INTO c_Visit_rec;
1885         IF c_Visit%NOTFOUND THEN
1886           CLOSE c_Visit;
1887           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
1888              Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
1889              Fnd_Msg_Pub.ADD;
1890              IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
1891                fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
1892              END IF;
1893           END IF;
1894           RAISE Fnd_Api.g_exc_error;
1895         ELSE
1896           CLOSE c_Visit;
1897         END IF;
1898         -- Complete Visit Record
1899         l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
1900         l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
1901         l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
1902         l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
1903         l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
1904         l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
1905         l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
1906         l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
1907         l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
1908         l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
1909         l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
1910         l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
1911         l_visit_rec.AUTO_VISIT_TYPE_FLAG  := c_Visit_rec.AUTO_VISIT_TYPE_FLAG; --TCHIMIRA :: 14-Jun-2012 :: ER 14015560
1912 
1913         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
1914                    fnd_log.string
1915                     ( fnd_log.level_procedure,
1916                      'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
1917                      'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id||', l_visit_rec.START_DATE: '||l_visit_rec.START_DATE);
1918         END IF;
1919         AHL_VWP_VISITS_PVT.Update_Visit (
1920                                       p_api_version => l_api_version,
1921                                       p_init_msg_list => Fnd_Api.g_false,
1922                                       p_commit => Fnd_Api.g_false,
1923                                       p_validation_level => p_validation_level,
1924                                       p_module_type => 'API',
1925                                       p_x_visit_rec  => l_visit_rec,
1926                                       x_return_status => l_return_status,
1927                                       x_msg_count => l_msg_count,
1928                                       x_msg_data => l_msg_data
1929                                      );
1930 
1931         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
1932                    fnd_log.string
1933                     ( fnd_log.level_procedure,
1934                      'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
1935                      'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
1936         END IF;
1937         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1938           x_msg_count := FND_MSG_PUB.count_msg;
1939           IF (l_log_statement >= l_log_current_level) THEN
1940               fnd_log.string(l_log_statement,
1941                              L_DEBUG_KEY,
1942                              'Errors from Update_Visit. Message count: ' || x_msg_count);
1943           END IF;
1944           IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1945              RAISE FND_API.G_EXC_ERROR;
1946           ELSE
1947              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1948           END IF;
1949         END IF;  -- Return Status is not Success
1950        ELSE
1951         --PRAKKUM :: 01/08/2012 :: Added procedure :: START
1952         Disconnect_Flight_Visit(
1953             p_visit_id               => l_visit_id,
1954             p_unit_schedule_id       => flight_schedule_rec.UNIT_SCHEDULE_ID,
1955             x_return_status          => l_return_status,
1956             x_msg_count              => l_msg_count,
1957             x_msg_data               => l_msg_data
1958           );
1959         IF (l_log_statement >= l_log_current_level) THEN
1960            fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
1961         END IF;
1962 
1963         IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
1964          x_msg_count := l_msg_count;
1965          x_return_status := l_return_status;
1966          IF l_return_status = Fnd_Api.g_ret_sts_error THEN
1967            RAISE Fnd_Api.g_exc_error;
1968          ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
1969            RAISE Fnd_Api.g_exc_unexpected_error;
1970          END IF;
1971         END IF;
1972         --PRAKKUM :: 01/08/2012 :: Added procedure :: END
1973        END IF; --IF condition to see if l_can_update_visit IS NOT NULL
1974       END IF;  -- IF condition to see if the visit dates has to be updated or not.
1975     END IF; --IF substr (l_visit_type,1,1) = 'D'
1976 
1977     -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: START
1978     -- Before this, previous visits can be cancelled so fetch details again
1979     -- check if there exisits a visit and if yes, what visit type - either arrival or downtime
1980     AHL_AVF_OPER_VSTS_PVT.Get_Visit_Type(flight_schedule_rec.unit_schedule_id, 'P', l_visit_type, l_visit_id,l_visit_id2);
1981 
1982     l_can_update_visit := null;
1983     IF l_visit_id IS NOT NULL THEN
1984       OPEN can_cancel_visit(l_visit_id);
1985       FETCH can_cancel_visit INTO l_can_update_visit;
1986       CLOSE can_cancel_visit;
1987     END IF;
1988     -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: END
1989 
1990     -- TCHIMIRA : Bug 13844759 : 29 - Mar - 2012
1991     -- If there is no departure visit, look for the possibility of creating one
1992     IF ((substr (l_visit_type,1,1) <> 'D')OR ( l_can_update_visit IS NULL)) THEN
1993       -- check for the possibility of creating a departure visit;
1994       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
1995                                 flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code, l_count_org,
1996                                 l_count_dep, l_count_cat,'DEPARTURE', NULL);
1997       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
1998       IF get_oper_param_rows1%FOUND THEN
1999         -- there is a possibility to create a departure visit
2000         --TYPE 2: Departure Org :: no dep visit associated to current FS dep org :: create a departure visit
2001         IF (l_log_statement >= l_log_current_level) THEN
2002           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 2 and before calling Create_Departure_visit');
2003         END IF;
2004           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
2005           l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
2006           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
2007           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
2008 
2009           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
2010           SAVEPOINT OPER_VISIT_CREATION;
2011 
2012             Create_Departure_visit(
2013               p_oper_param_rec         => l_oper_param_rec,
2014               p_flight_schedule_rec    => l_flight_schedule_rec,
2015               p_present_time           => l_present_time,
2016               p_is_creation_success    => l_is_creation_success,
2017               x_return_status          => l_return_status,
2018               x_msg_count              => l_msg_count,
2019               x_msg_data               => l_msg_data
2020             );
2021           IF (l_log_statement >= l_log_current_level) THEN
2022             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Departure_visit and l_return_status: '||l_return_status);
2023             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
2024           END IF;
2025 
2026           IF l_is_creation_success<>'Y' THEN
2027              ROLLBACK TO OPER_VISIT_CREATION;
2028           END IF;
2029           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
2030 
2031       END IF; --IF get_oper_param_rows1%FOUND THEN
2032       CLOSE get_oper_param_rows1;
2033       -- Go to arrival org consideration
2034     END IF; -- if condition to check the if there is no departure visit
2035 
2036   --If the flight schedule has a preceeding event then
2037   ELSE
2038    -- PRAKKUM :: Bug 13844759 :: 26/07/2012
2039    load_can_cancel_visit(
2040       p_visit_id               => l_visit_id,
2041       p_visit_id2              => l_visit_id2,
2042       x_can_cancel_visit       => l_can_cancel_visit,
2043       x_can_cancel_visit2      => l_can_cancel_visit2,
2044       x_return_status          => l_return_status,
2045       x_msg_count              => l_msg_count,
2046       x_msg_data               => l_msg_data
2047 
2048       );
2049    IF (l_log_statement >= l_log_current_level) THEN
2050        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
2051    END IF;
2052 
2053    OPEN get_pre_fs_det(flight_schedule_rec.preceding_us_id);
2054    FETCH get_pre_fs_det INTO pre_fs_det_rec;
2055    CLOSE get_pre_fs_det;
2056    -- store all the preceding flight params in the record
2057    l_prec_flight_schedule_rec := null;
2058    l_prec_flight_schedule_rec.unit_config_header_id := flight_schedule_rec.unit_config_header_id;
2059    l_prec_flight_schedule_rec.unit_schedule_id      := pre_fs_det_rec.unit_schedule_id;
2060    l_prec_flight_schedule_rec.arrival_org_id        := pre_fs_det_rec.arrival_org_id;
2061    l_prec_flight_schedule_rec.arrival_dept_id       := pre_fs_det_rec.arrival_dept_id;
2062    l_prec_flight_schedule_rec.est_arrival_time      := pre_fs_det_rec.est_arrival_time;
2063 
2064    -- PRAKKUM :: Bug 13844759 :: 25/07/2012
2065    -- If existing visit is departure visit, then see whether it needs to be updated or disassociate
2066    IF substr (l_visit_type,1,1) = 'D' THEN -- if there is a visit
2067       IF l_visit_id IS NOT NULL THEN
2068 
2069           IF (l_log_statement >= l_log_current_level) THEN
2070             fnd_log.string(l_log_statement,L_DEBUG_KEY, ' There is an existing departure visit at dep org, do nothing');
2071           END IF;
2072           -- based on the profile value see if you can adjust the visit dates
2073           OPEN get_visit_end_time(l_visit_id);
2074           FETCH get_visit_end_time INTO l_visit_end_time;
2075           CLOSE get_visit_end_time;
2076 
2077           OPEN c_Visit_type_dur (l_visit_id);
2078           FETCH c_Visit_type_dur INTO l_visit_type_duration;
2079           CLOSE c_Visit_type_dur;
2080           -- Added the below code to see if the visit can be updated or not : tchimira: bug 13844759
2081           l_can_update_visit := null;
2082           OPEN can_cancel_visit(l_visit_id);
2083           FETCH can_cancel_visit INTO l_can_update_visit;
2084           CLOSE can_cancel_visit;
2085 
2086           IF (l_log_statement >= l_log_current_level) THEN
2087             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_visit_end_time is: '||l_visit_end_time||' ,est_departure_time: '||flight_schedule_rec.est_departure_time||', l_can_update_visit is: '||l_can_update_visit);
2088           END IF;
2089 
2090           IF flight_schedule_rec.est_departure_time > l_visit_end_time THEN
2091             l_difference_dep_time := (flight_schedule_rec.est_departure_time - l_visit_end_time)*24*60;
2092           ELSE
2093             l_difference_dep_time := (l_visit_end_time - flight_schedule_rec.est_departure_time)*24*60;
2094           END IF;
2095 
2096           IF (l_log_statement >= l_log_current_level) THEN
2097             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_difference_dep_time is: '||l_difference_dep_time);
2098           END IF;
2099 
2100           IF  l_profile_varchar is not null  THEN
2101           l_profile_number := to_number(l_profile_varchar) ;
2102           ELSE
2103           l_profile_number := l_difference_dep_time-1;
2104           END IF;
2105 
2106           IF  l_profile_varchar2 is not null THEN
2107           l_profile_number2 := to_number(l_profile_varchar2) ;
2108           ELSE
2109           l_profile_number2 := flight_schedule_rec.est_departure_time-sysdate+1;
2110           END IF;
2111 
2112           IF((l_difference_dep_time > l_profile_number)
2113              AND (l_difference_dep_time <> 0)
2114              AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_departure_time) ) THEN
2115            -- TCHIMIRA : Bug 13844759 : check if the visit is updateable.
2116            -- if yes, the update the visit else diconnect the visit
2117            IF l_can_update_visit IS NOT NULL THEN
2118             IF (l_log_statement >= l_log_current_level) THEN
2119                fnd_log.string(l_log_statement,L_DEBUG_KEY, ' Before type 1 ');
2120             END IF;
2121             --TYPE 1: Departure Org :: dep visit at dep org of current FS :: Update the departure visit dates
2122             l_start_date := (flight_schedule_rec.est_departure_time - l_visit_type_duration/24);
2123             l_end_date := flight_schedule_rec.est_departure_time;
2124 
2125             l_visit_rec := null;
2126             l_visit_rec.START_DATE            := l_start_date;
2127             l_visit_rec.PLAN_END_DATE         := l_end_date;
2128             l_visit_rec.visit_id              := l_visit_id;
2129 
2130             OPEN c_Visit(l_visit_id);
2131             FETCH c_Visit INTO c_Visit_rec;
2132             IF c_Visit%NOTFOUND THEN
2133               CLOSE c_Visit;
2134               IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
2135                  Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
2136                  Fnd_Msg_Pub.ADD;
2137                  IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
2138                    fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
2139                  END IF;
2140               END IF;
2141               RAISE Fnd_Api.g_exc_error;
2142             ELSE
2143               CLOSE c_Visit;
2144             END IF;
2145             -- Complete Visit Record
2146             l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
2147             l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
2148             l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
2149             l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
2150             l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
2151             l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
2152             l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
2153             l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
2154             l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
2155             l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
2156             l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
2157             l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
2158             l_visit_rec.AUTO_VISIT_TYPE_FLAG  := c_Visit_rec.AUTO_VISIT_TYPE_FLAG; --TCHIMIRA :: 14-Jun-2012 :: ER 14015560
2159 
2160             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
2161                        fnd_log.string
2162                         ( fnd_log.level_procedure,
2163                          'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
2164                          'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id||', l_visit_rec.START_DATE: '||l_visit_rec.START_DATE);
2165             END IF;
2166             AHL_VWP_VISITS_PVT.Update_Visit (
2167                                           p_api_version => l_api_version,
2168                                           p_init_msg_list => Fnd_Api.g_false,
2169                                           p_commit => Fnd_Api.g_false,
2170                                           p_validation_level => p_validation_level,
2171                                           p_module_type => 'API',
2172                                           p_x_visit_rec  => l_visit_rec,
2173                                           x_return_status => l_return_status,
2174                                           x_msg_count => l_msg_count,
2175                                           x_msg_data => l_msg_data
2176                                          );
2177 
2178             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
2179                        fnd_log.string
2180                         ( fnd_log.level_procedure,
2181                          'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
2182                          'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
2183             END IF;
2184             IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2185               x_msg_count := FND_MSG_PUB.count_msg;
2186               IF (l_log_statement >= l_log_current_level) THEN
2187                   fnd_log.string(l_log_statement,
2188                                  L_DEBUG_KEY,
2189                                  'Errors from Update_Visit. Message count: ' || x_msg_count);
2190               END IF;
2191               IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2192                  RAISE FND_API.G_EXC_ERROR;
2193               ELSE
2194                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2195               END IF;
2196             END IF;  -- Return Status is not Success
2197            ELSE
2198              --PRAKKUM :: 01/08/2012 :: Added procedure :: START
2199              Disconnect_Flight_Visit(
2200                 p_visit_id               => l_visit_id,
2201                 p_unit_schedule_id       => flight_schedule_rec.UNIT_SCHEDULE_ID,
2202                 x_return_status          => l_return_status,
2203                 x_msg_count              => l_msg_count,
2204                 x_msg_data               => l_msg_data
2205              );
2206              IF (l_log_statement >= l_log_current_level) THEN
2207                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
2208              END IF;
2209 
2210              IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
2211               x_msg_count := l_msg_count;
2212               x_return_status := l_return_status;
2213               IF l_return_status = Fnd_Api.g_ret_sts_error THEN
2214                 RAISE Fnd_Api.g_exc_error;
2215               ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
2216                 RAISE Fnd_Api.g_exc_unexpected_error;
2217               END IF;
2218              END IF;
2219              --PRAKKUM :: 01/08/2012 :: Added procedure :: END
2220              l_visit_id := NULL; --Since visit is cancelled
2221 
2222            END IF; --IF condition to see if l_can_update_visit IS NOT NULL
2223           END IF;  -- IF condition to see if the visit dates has to be updated or not.
2224        END IF; -- l_visit_id IS NOT NULL
2225    END IF; -- substr (l_visit_type,1,1) = 'D'
2226 
2227    -- PRAKKUM :: Bug 13844759 :: 26/07/2012
2228    -- Recheck to confirm if any visit is disaasociated
2229    load_can_cancel_visit(
2230       p_visit_id               => l_visit_id,
2231       p_visit_id2              => l_visit_id2,
2232       x_can_cancel_visit       => l_can_cancel_visit,
2233       x_can_cancel_visit2      => l_can_cancel_visit2,
2234       x_return_status          => l_return_status,
2235       x_msg_count              => l_msg_count,
2236       x_msg_data               => l_msg_data
2237       );
2238    IF (l_log_statement >= l_log_current_level) THEN
2239        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
2240    END IF;
2241 
2242    -- PRAKKUM :: Bug 14368696 :: 01/08/2012 :: START
2243    IF (l_can_cancel_visit2 IS NULL) THEN -- If downtime or arrival is not allowed to cancel
2244        IF substr (l_visit_type,1,1) = 'T' AND l_visit_id2 IS NOT NULL THEN --Downtime visit exists
2245 
2246         IF (l_log_statement >= l_log_current_level) THEN
2247            fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Handle case that downtime visit not allowed to cancel--@>');
2248         END IF;
2249 
2250         --Get ground time at the departing org of a FS
2251         OPEN get_ground_time (flight_schedule_rec.preceding_us_id, flight_schedule_rec.UNIT_SCHEDULE_ID);
2252         FETCH get_ground_time INTO l_ground_time;
2253         CLOSE get_ground_time;
2254 
2255         OPEN get_non_null_org_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id);
2256         FETCH get_non_null_org_count INTO l_count_prec_arr_org;
2257         CLOSE get_non_null_org_count;
2258 
2259         OPEN get_non_null_dep_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2260                               pre_fs_det_rec.arrival_dept_id);
2261         FETCH get_non_null_dep_count INTO l_count_prec_arr_dep;
2262         CLOSE get_non_null_dep_count;
2263 
2264         OPEN get_non_null_cat_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2265                               pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code);
2266         FETCH get_non_null_cat_count INTO l_count_prec_arr_cat;
2267         CLOSE get_non_null_cat_count;
2268 
2269         -- check for the possibility to create a downtime visit
2270         OPEN get_oper_param_rows1(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2271                                 pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code, l_count_prec_arr_org, l_count_prec_arr_dep, l_count_prec_arr_cat,
2272                                 'DOWNTIME', l_ground_time);
2273         FETCH get_oper_param_rows1 INTO get_oper_param_rec;
2274         l_can_disconnect_visit := 'N';
2275         IF get_oper_param_rows1%FOUND THEN -- there is a possibility of creating a downtime visit at arrival org of prec FS
2276           OPEN get_visit_type_code(l_visit_id2);
2277           FETCH get_visit_type_code INTO l_visit_type_code;
2278           CLOSE get_visit_type_code;
2279           IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
2280              l_can_disconnect_visit := 'Y';
2281           ELSE
2282              OPEN get_fs_arr_time(flight_schedule_rec.preceding_us_id);
2283              FETCH get_fs_arr_time INTO l_start_date;
2284              CLOSE get_fs_arr_time;
2285              -- based on the profile value see if you can adjust the visit dates
2286              OPEN get_visit_end_time(l_visit_id2);
2287              FETCH get_visit_end_time INTO l_visit_end_time;
2288              CLOSE get_visit_end_time;
2289              IF flight_schedule_rec.est_departure_time > l_visit_end_time THEN
2290                 l_difference_dep_time := (flight_schedule_rec.est_departure_time - l_visit_end_time)*24*60;
2291              ELSE
2292                 l_difference_dep_time := (l_visit_end_time - flight_schedule_rec.est_departure_time)*24*60;
2293              END IF;
2294              IF  l_profile_varchar is not null  THEN
2295                  l_profile_number := to_number(l_profile_varchar) ;
2296              ELSE
2297                  l_profile_number := l_difference_dep_time-1;
2298              END IF;
2299 
2300              IF  l_profile_varchar2 is not null THEN
2301                  l_profile_number2 := to_number(l_profile_varchar2) ;
2302              ELSE
2303                  l_profile_number2 := flight_schedule_rec.est_departure_time-sysdate+1;
2304              END IF;
2305 
2306              IF (l_log_statement >= l_log_current_level) THEN
2307                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_difference_dep_time--@>'||l_difference_dep_time);
2308                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number--@>'||l_profile_number);
2309                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number2--@>'||l_profile_number);
2310                 fnd_log.string(l_log_statement,L_DEBUG_KEY, '(sysdate + l_profile_number2)--@>'||(cast((sysdate + l_profile_number2) AS timestamp)));
2311                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'flight_schedule_rec.est_departure_time--@>'||(cast((flight_schedule_rec.est_departure_time) AS timestamp)));
2312              END IF;
2313 
2314              IF((l_difference_dep_time > l_profile_number)
2315                   AND (l_difference_dep_time <> 0)
2316                   AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_departure_time) ) THEN
2317 
2318                   l_can_disconnect_visit := 'Y';
2319 
2320              END IF;
2321           END IF;
2322         ELSE
2323            l_can_disconnect_visit := 'Y';
2324         END IF;
2325         CLOSE get_oper_param_rows1;
2326 
2327         IF l_can_disconnect_visit ='Y' THEN
2328              --PRAKKUM :: 01/08/2012 :: Added procedure :: START
2329              Disconnect_Flight_Visit(
2330                 p_visit_id               => l_visit_id2,
2331                 p_unit_schedule_id       => pre_fs_det_rec.unit_schedule_id,
2332                 x_return_status          => l_return_status,
2333                 x_msg_count              => l_msg_count,
2334                 x_msg_data               => l_msg_data
2335              );
2336              IF (l_log_statement >= l_log_current_level) THEN
2337                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
2338              END IF;
2339 
2340              IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
2341               x_msg_count := l_msg_count;
2342               x_return_status := l_return_status;
2343               IF l_return_status = Fnd_Api.g_ret_sts_error THEN
2344                 RAISE Fnd_Api.g_exc_error;
2345               ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
2346                 RAISE Fnd_Api.g_exc_unexpected_error;
2347               END IF;
2348              END IF;
2349              --PRAKKUM :: 01/08/2012 :: Added procedure :: END
2350              l_visit_id2 := NULL; --Since visit is cancelled
2351 
2352         END IF;--l_can_disconnect_visit ='Y'
2353        END IF;--Downtime visit exists
2354    END IF; --IF (l_can_cancel_visit2 IS NULL)
2355 
2356    -- Recheck to confirm if any visit is disaasociated
2357    load_can_cancel_visit(
2358       p_visit_id               => l_visit_id,
2359       p_visit_id2              => l_visit_id2,
2360       x_can_cancel_visit       => l_can_cancel_visit,
2361       x_can_cancel_visit2      => l_can_cancel_visit2,
2362       x_return_status          => l_return_status,
2363       x_msg_count              => l_msg_count,
2364       x_msg_data               => l_msg_data
2365       );
2366    IF (l_log_statement >= l_log_current_level) THEN
2367        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
2368    END IF;
2369    -- PRAKKUM :: Bug 14368696 :: 01/08/2012 :: END
2370 
2371    -- if preceding arrival flight visit can not be cancelled and there is no visit associated to departure org of current FS
2372    -- then look for the possibility of creating an departure visit at departure org of current FS
2373    IF (l_can_cancel_visit2 IS NULL) THEN -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: START
2374 
2375        IF substr (l_visit_type,1,1) = 'T' AND l_visit_id2 IS NOT NULL THEN --Downtime visit exists
2376 
2377         --Get ground time at the departing org of a FS
2378         OPEN get_ground_time (flight_schedule_rec.preceding_us_id, flight_schedule_rec.UNIT_SCHEDULE_ID);
2379         FETCH get_ground_time INTO l_ground_time;
2380         CLOSE get_ground_time;
2381 
2382         OPEN get_non_null_org_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id);
2383         FETCH get_non_null_org_count INTO l_count_prec_arr_org;
2384         CLOSE get_non_null_org_count;
2385 
2386         OPEN get_non_null_dep_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2387                               pre_fs_det_rec.arrival_dept_id);
2388         FETCH get_non_null_dep_count INTO l_count_prec_arr_dep;
2389         CLOSE get_non_null_dep_count;
2390 
2391         OPEN get_non_null_cat_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2392                               pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code);
2393         FETCH get_non_null_cat_count INTO l_count_prec_arr_cat;
2394         CLOSE get_non_null_cat_count;
2395 
2396         -- check for the possibility to create a downtime visit
2397         OPEN get_oper_param_rows1(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2398                                 pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code, l_count_prec_arr_org, l_count_prec_arr_dep, l_count_prec_arr_cat,
2399                                 'DOWNTIME', l_ground_time);
2400         FETCH get_oper_param_rows1 INTO get_oper_param_rec;
2401         l_can_disconnect_visit := 'N';
2402         IF get_oper_param_rows1%FOUND THEN -- there is a possibility of creating a downtime visit at arrival org of prec FS
2403           OPEN get_visit_type_code(l_visit_id2);
2404           FETCH get_visit_type_code INTO l_visit_type_code;
2405           CLOSE get_visit_type_code;
2406           IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
2407              l_can_disconnect_visit := 'Y';
2408           ELSE
2409              OPEN get_fs_arr_time(flight_schedule_rec.preceding_us_id);
2410              FETCH get_fs_arr_time INTO l_start_date;
2411              CLOSE get_fs_arr_time;
2412              -- based on the profile value see if you can adjust the visit dates
2413              OPEN get_visit_end_time(l_visit_id2);--PRAKKUM :: Bug 14342603 :: 18/07/2012
2414              FETCH get_visit_end_time INTO l_visit_end_time;
2415              CLOSE get_visit_end_time;
2416              IF flight_schedule_rec.est_departure_time > l_visit_end_time THEN
2417                 l_difference_dep_time := (flight_schedule_rec.est_departure_time - l_visit_end_time)*24*60;
2418              ELSE
2419                 l_difference_dep_time := (l_visit_end_time - flight_schedule_rec.est_departure_time)*24*60;
2420              END IF;
2421              IF  l_profile_varchar is not null  THEN
2422                  l_profile_number := to_number(l_profile_varchar) ;
2423              ELSE
2424                  l_profile_number := l_difference_dep_time-1;
2425              END IF;
2426 
2427              IF  l_profile_varchar2 is not null THEN
2428                  l_profile_number2 := to_number(l_profile_varchar2) ;
2429              ELSE
2430                  l_profile_number2 := flight_schedule_rec.est_departure_time-sysdate+1;
2431              END IF;
2432 
2433              IF (l_log_statement >= l_log_current_level) THEN --PRAKKUM :: Bug 14342603 :: 18/07/2012
2434                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_difference_dep_time--@>'||l_difference_dep_time);
2435                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number--@>'||l_profile_number);
2436                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number2--@>'||l_profile_number);
2437                 fnd_log.string(l_log_statement,L_DEBUG_KEY, '(sysdate + l_profile_number2)--@>'||(cast((sysdate + l_profile_number2) AS timestamp)));
2438                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'flight_schedule_rec.est_departure_time--@>'||(cast((flight_schedule_rec.est_departure_time) AS timestamp)));
2439              END IF;
2440 
2441              IF((l_difference_dep_time > l_profile_number)
2442                   AND (l_difference_dep_time <> 0)
2443                   AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_departure_time) ) THEN
2444 
2445                   l_can_update_visit := null;
2446                   OPEN can_cancel_visit(l_visit_id2);
2447                   FETCH can_cancel_visit INTO l_can_update_visit;
2448                   CLOSE can_cancel_visit;
2449 
2450                   IF l_can_update_visit IS NULL THEN
2451                      l_can_disconnect_visit := 'Y';
2452                   END IF;
2453 
2454              END IF;
2455           END IF;
2456         ELSE
2457            l_can_disconnect_visit := 'Y';
2458         END IF;
2459 
2460         IF l_can_disconnect_visit ='Y' THEN
2461              --PRAKKUM :: 01/08/2012 :: Added procedure :: START
2462              Disconnect_Flight_Visit(
2463                 p_visit_id               => l_visit_id2,
2464                 p_unit_schedule_id       => pre_fs_det_rec.unit_schedule_id,
2465                 x_return_status          => l_return_status,
2466                 x_msg_count              => l_msg_count,
2467                 x_msg_data               => l_msg_data
2468              );
2469              IF (l_log_statement >= l_log_current_level) THEN
2470                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
2471              END IF;
2472 
2473              IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
2474               x_msg_count := l_msg_count;
2475               x_return_status := l_return_status;
2476               IF l_return_status = Fnd_Api.g_ret_sts_error THEN
2477                 RAISE Fnd_Api.g_exc_error;
2478               ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
2479                 RAISE Fnd_Api.g_exc_unexpected_error;
2480               END IF;
2481              END IF;
2482              --PRAKKUM :: 01/08/2012 :: Added procedure :: END
2483              l_visit_id2 := NULL; --Since visit is cancelled
2484 
2485         END IF;--l_can_disconnect_visit ='Y'
2486        END IF;--Downtime visit exists
2487 
2488        IF l_visit_id IS NULL THEN -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: END
2489 
2490           -- check for the possibility of creating a departure visit;
2491           OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
2492                                     flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code, l_count_org,
2493                                     l_count_dep, l_count_cat,'DEPARTURE', NULL);
2494           FETCH get_oper_param_rows1 INTO get_oper_param_rec;
2495           IF get_oper_param_rows1%FOUND THEN
2496             --If there is a possibility to create a departure visit
2497             --TYPE 3: Departure Org :: no visit to cancel :: create a departure visit
2498             IF (l_log_statement >= l_log_current_level) THEN
2499               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 3 and before calling Create_Departure_visit');
2500             END IF;
2501               l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
2502               l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
2503               l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
2504               l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
2505 
2506               -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
2507               SAVEPOINT OPER_VISIT_CREATION;
2508 
2509                Create_Departure_visit(
2510                   p_oper_param_rec         => l_oper_param_rec,
2511                   p_flight_schedule_rec    => l_flight_schedule_rec,
2512                   p_present_time           => l_present_time,
2513                   p_is_creation_success    => l_is_creation_success,
2514                   x_return_status          => l_return_status,
2515                   x_msg_count              => l_msg_count,
2516                   x_msg_data               => l_msg_data
2517                    );
2518                IF (l_log_statement >= l_log_current_level) THEN
2519                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Departure_visit and l_return_status: '||l_return_status);
2520                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
2521                END IF;
2522 
2523               IF l_is_creation_success<>'Y' THEN
2524                 ROLLBACK TO OPER_VISIT_CREATION;
2525               END IF;
2526               -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
2527 
2528           END IF;
2529           CLOSE get_oper_param_rows1;
2530            -- Go to arrival org consideration
2531 
2532        END IF; -- l_visit_id IS NULL
2533    END IF; --IF ((l_can_cancel_visit2 IS NULL) AND (l_visit_id IS NULL)) THEN
2534 
2535    IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
2536     --Get ground time at the departing org of a FS
2537     OPEN get_ground_time (flight_schedule_rec.preceding_us_id, flight_schedule_rec.UNIT_SCHEDULE_ID);
2538     FETCH get_ground_time INTO l_ground_time;
2539     CLOSE get_ground_time;
2540 
2541     OPEN get_non_null_org_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id);
2542     FETCH get_non_null_org_count INTO l_count_prec_arr_org;
2543     CLOSE get_non_null_org_count;
2544 
2545     OPEN get_non_null_dep_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2546                               pre_fs_det_rec.arrival_dept_id);
2547     FETCH get_non_null_dep_count INTO l_count_prec_arr_dep;
2548     CLOSE get_non_null_dep_count;
2549 
2550     OPEN get_non_null_cat_count(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2551                               pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code);
2552     FETCH get_non_null_cat_count INTO l_count_prec_arr_cat;
2553     CLOSE get_non_null_cat_count;
2554 
2555     IF (l_log_statement >= l_log_current_level) THEN
2556        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'passing values are unit_config_header_id: '||pre_fs_det_rec.unit_config_header_id
2557        ||', pre_fs_det_rec.arrival_org_id: '||pre_fs_det_rec.arrival_org_id||',  arrival_dept_id: , '||pre_fs_det_rec.arrival_dept_id
2558        ||',  flight_category_code: ,'||pre_fs_det_rec.flight_category_code||',  l_count_prec_arr_org: ,'||l_count_prec_arr_org
2559        ||', l_count_prec_arr_dep:   ,'||l_count_prec_arr_dep||',  l_count_prec_arr_cat: ,'
2560        ||l_count_prec_arr_cat||' ground time is '||l_ground_time);
2561     END IF;
2562 
2563     -- check for the possibility to create a downtime visit
2564     OPEN get_oper_param_rows1(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2565                                 pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code, l_count_prec_arr_org, l_count_prec_arr_dep, l_count_prec_arr_cat,
2566                                 'DOWNTIME', l_ground_time);
2567     FETCH get_oper_param_rows1 INTO get_oper_param_rec;
2568     l_is_downtime_vst_created := 'Y';
2569     IF get_oper_param_rows1%FOUND THEN -- there is a possibility of creating a downtime visit at arrival org of prec FS
2570       IF l_visit_id2 is null THEN  -- if there is no visit associated to the arrival org of previous FS
2571         IF (l_log_statement >= l_log_current_level) THEN
2572           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 4');
2573         END IF;
2574 
2575         -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
2576         SAVEPOINT OPER_VISIT_CREATION;
2577 
2578         --TYPE 4: Departure Org :: no visit for arrival org of previous FS to cancel :: create a downime visit
2579         --cancel the departure visit of the current flight if it is not null
2580         IF(l_visit_id is not null) then
2581           Delete_Oper_Visit(
2582                p_visit_id       => l_visit_id,
2583                x_return_status  => l_return_status);
2584         END IF;--IF(l_visit_id is not null) then
2585         IF (l_log_statement >= l_log_current_level) THEN
2586             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
2587         END IF;
2588         l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
2589         l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
2590         l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
2591 
2592         -- we need to create a downtime visit and associate to the departure org of current flight, so pass preceeding flight details
2593         Create_Downtime_visit(
2594             p_oper_param_rec         => l_oper_param_rec,
2595             p_flight_schedule_rec    => l_prec_flight_schedule_rec,
2596             p_present_time           => l_present_time,
2597             p_is_creation_success    => l_is_creation_success,
2598             x_return_status          => l_return_status,
2599             x_msg_count              => l_msg_count,
2600             x_msg_data               => l_msg_data
2601             );
2602 
2603         IF (l_log_statement >= l_log_current_level) THEN
2604            fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
2605            fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
2606         END IF;
2607         IF l_is_creation_success<>'Y' THEN
2608            ROLLBACK TO OPER_VISIT_CREATION;
2609            l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating arrival visit
2610         END IF;
2611         -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
2612 
2613       ELSE -- l_visit_id2 is not null i.e if there is visit associated to the arrival org of previous FS
2614         -- We need to match the visit type of the existing visit and operational param row visit type
2615         OPEN get_visit_type_code(l_visit_id2);
2616         FETCH get_visit_type_code INTO l_visit_type_code;
2617         CLOSE get_visit_type_code;
2618         IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
2619           -- if it does not match then Cancel the visit assoc to arrival org and also if any departure visit associated
2620           -- to the dep org of current FS. then create a downtime visit
2621           IF (l_log_statement >= l_log_current_level) THEN
2622              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 5');
2623           END IF;
2624           --TYPE 5: Departure Org :: cancel arrival visit and dep visit(if exists) :: create a downtime visit
2625           --Cancel the existing arrival visit of the prec FS since the visit types did not match
2626            Delete_Oper_Visit(
2627                p_visit_id       => l_visit_id2,
2628                x_return_status  => l_return_status);
2629           IF(l_visit_id is not null) then
2630             Delete_Oper_Visit(
2631                p_visit_id       => l_visit_id,
2632                x_return_status  => l_return_status);
2633           END IF;--IF(l_visit_id is not null) then
2634           IF (l_log_statement >= l_log_current_level) THEN
2635             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
2636           END IF;
2637           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
2638           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
2639           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
2640 
2641           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
2642           SAVEPOINT OPER_VISIT_CREATION;
2643 
2644           -- we need to create a downtime visit and associate to the arrival org of preceeding flight, so pass preceeding flight details
2645           Create_Downtime_visit(
2646             p_oper_param_rec         => l_oper_param_rec,
2647             p_flight_schedule_rec    => l_prec_flight_schedule_rec,
2648             p_present_time           => l_present_time,
2649             p_is_creation_success    => l_is_creation_success,
2650             x_return_status          => l_return_status,
2651             x_msg_count              => l_msg_count,
2652             x_msg_data               => l_msg_data
2653             );
2654 
2655           IF (l_log_statement >= l_log_current_level) THEN
2656              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
2657              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
2658           END IF;
2659           IF l_is_creation_success<>'Y' THEN
2660              ROLLBACK TO OPER_VISIT_CREATION;
2661              l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating arrival visit
2662           END IF;
2663           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
2664         ELSE
2665           IF (l_log_statement >= l_log_current_level) THEN
2666              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 6');
2667           END IF;
2668           --TYPE 6::Departure Org::cancel dep visit(if exists):: extend the existing visit
2669           -- If the visit type matches, we need to update the visit dates
2670           --Before updating the visit first cancel the departure visit of the current if it is not null
2671           IF(l_visit_id is not null) then
2672             Delete_Oper_Visit(
2673                p_visit_id       => l_visit_id,
2674                x_return_status  => l_return_status);
2675           END IF;--IF(l_visit_id is not null) then
2676           -- Now update the visit dates to span the entire downtime
2677           OPEN get_fs_arr_time(flight_schedule_rec.preceding_us_id);
2678           FETCH get_fs_arr_time INTO l_start_date;
2679           CLOSE get_fs_arr_time;
2680           -- based on the profile value see if you can adjust the visit dates
2681           OPEN get_visit_end_time(l_visit_id2);--PRAKKUM :: Bug 14342603 :: 18/07/2012
2682           FETCH get_visit_end_time INTO l_visit_end_time;
2683           CLOSE get_visit_end_time;
2684           IF flight_schedule_rec.est_departure_time > l_visit_end_time THEN
2685             l_difference_dep_time := (flight_schedule_rec.est_departure_time - l_visit_end_time)*24*60;
2686           ELSE
2687             l_difference_dep_time := (l_visit_end_time - flight_schedule_rec.est_departure_time)*24*60;
2688           END IF;
2689           IF  l_profile_varchar is not null  THEN
2690               l_profile_number := to_number(l_profile_varchar) ;
2691           ELSE
2692               l_profile_number := l_difference_dep_time-1;
2693           END IF;
2694 
2695           IF  l_profile_varchar2 is not null THEN
2696               l_profile_number2 := to_number(l_profile_varchar2) ;
2697           ELSE
2698               l_profile_number2 := flight_schedule_rec.est_departure_time-sysdate+1;
2699           END IF;
2700 
2701           IF (l_log_statement >= l_log_current_level) THEN --PRAKKUM :: Bug 14342603 :: 18/07/2012
2702              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_difference_dep_time--@>'||l_difference_dep_time);
2703              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number--@>'||l_profile_number);
2704              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number2--@>'||l_profile_number);
2705              fnd_log.string(l_log_statement,L_DEBUG_KEY, '(sysdate + l_profile_number2)--@>'||(cast((sysdate + l_profile_number2) AS timestamp)));
2706              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'flight_schedule_rec.est_departure_time--@>'||(cast((flight_schedule_rec.est_departure_time) AS timestamp)));
2707           END IF;
2708 
2709           IF((l_difference_dep_time > l_profile_number)
2710               AND (l_difference_dep_time <> 0)
2711               AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_departure_time) ) THEN
2712 
2713             l_end_date := flight_schedule_rec.est_departure_time;
2714             l_visit_rec.START_DATE            := l_start_date;
2715             l_visit_rec.PLAN_END_DATE         := l_end_date;
2716             l_visit_rec.VISIT_ID              := l_visit_id2;--PRAKKUM :: Bug 14342603 :: 18/07/2012
2717 
2718             OPEN c_Visit(l_visit_id2);--PRAKKUM :: Bug 14342603 :: 18/07/2012
2719             FETCH c_Visit INTO c_Visit_rec;
2720             IF c_Visit%NOTFOUND THEN
2721              CLOSE c_Visit;
2722              IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
2723               Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
2724               Fnd_Msg_Pub.ADD;
2725               IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
2726                 fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
2727               END IF;
2728              END IF;
2729              RAISE Fnd_Api.g_exc_error;
2730             ELSE
2731              CLOSE c_Visit;
2732             END IF;
2733 
2734             -- Complete Visit Record
2735             l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
2736             l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
2737             l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
2738             l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
2739             l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
2740             l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
2741             l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
2742             l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
2743             l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
2744             l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
2745             l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
2746             l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
2747             l_visit_rec.AUTO_VISIT_TYPE_FLAG  := 'T'; --TCHIMIRA :: 14-Jun-2012 :: ER 14015560
2748 
2749             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
2750                 fnd_log.string
2751                  ( fnd_log.level_procedure,
2752                    'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
2753                    'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id);
2754             END IF;
2755             AHL_VWP_VISITS_PVT.Update_Visit (
2756                                       p_api_version => l_api_version,
2757                                       p_init_msg_list => Fnd_Api.g_false,
2758                                       p_commit => Fnd_Api.g_false,
2759                                       p_validation_level => p_validation_level,
2760                                       p_module_type => 'API',
2761                                       p_x_visit_rec  => l_visit_rec,
2762                                       x_return_status => l_return_status,
2763                                       x_msg_count => l_msg_count,
2764                                       x_msg_data => l_msg_data
2765                                      );
2766 
2767             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
2768                 fnd_log.string
2769                 ( fnd_log.level_procedure,
2770                     'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
2771                     'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
2772             END IF;
2773             IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2774               x_msg_count := FND_MSG_PUB.count_msg;
2775               IF (l_log_statement >= l_log_current_level) THEN
2776                  fnd_log.string(l_log_statement,
2777                                 L_DEBUG_KEY,
2778                                 'Errors from Update_Visit. Message count: ' || x_msg_count);
2779               END IF;
2780               IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2781                 RAISE FND_API.G_EXC_ERROR;
2782               ELSE
2783                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2784               END IF;
2785             END IF;  -- Return Status is not Success
2786           END IF;       --IF((l_difference_dep_time > nvl
2787         END IF;--else of l_visit_type_code <> get_oper_param_rec.visit_type_code
2788       END IF;-- else of l_visit_id2 is null
2789       CLOSE get_oper_param_rows1;
2790     ELSE --a downtime visit cannot be created
2791       CLOSE get_oper_param_rows1;
2792       l_is_downtime_vst_created := 'N';
2793     END IF;
2794     IF l_is_downtime_vst_created = 'N' THEN -- Downtime visit cannot be created
2795       -- if there is a visit associated to the arrival org of Prec FS
2796       IF l_visit_id2 is not null THEN
2797         IF (l_log_statement >= l_log_current_level) THEN
2798           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 7');
2799         END IF;
2800         -- TYPE 7::Departure Org:: there is a visit assoc to arrival org of prev FS::
2801         -- we need to check the type of the visit associated to the arrival org
2802         -- it can be either arrival or downtime;
2803         -- if the visit type of the existing visit matches with any of the visit types of operational param rows then
2804         -- it is a downtime visit else and arrival visit
2805         OPEN get_visit_type_code (l_visit_id2);
2806         FETCH get_visit_type_code INTO l_visit_type_code;
2807         CLOSE get_visit_type_code;
2808         OPEN get_oper_param_rows1(pre_fs_det_rec.unit_config_header_id,pre_fs_det_rec.arrival_org_id,
2809                                 pre_fs_det_rec.arrival_dept_id, pre_fs_det_rec.flight_category_code, l_count_prec_arr_org, l_count_prec_arr_dep, l_count_prec_arr_cat,
2810                                 'DOWNTIME', null);
2811         LOOP
2812           FETCH get_oper_param_rows1 INTO get_oper_param_rec;
2813           EXIT WHEN get_oper_param_rows1%NOTFOUND;
2814           IF get_oper_param_rec.visit_type_code = l_visit_type_code THEN
2815             l_flag := 'Y';
2816           END IF;
2817         END LOOP;
2818         CLOSE get_oper_param_rows1;
2819 
2820         -- if the visit associated to the arrival org of Prec FS is downtime, then cancel the visit
2821         IF l_flag = 'Y' THEN
2822           -- it is a downtime visit and we need to cancel that visit
2823           Delete_Oper_Visit(
2824              p_visit_id       => l_visit_id2,
2825              x_return_status  => l_return_status);
2826         END IF; --IF l_flag = 'Y'
2827       END IF;-- l_visit_id2 is not null
2828       -- PRAKKUM :: Bug 13844759 :: 31/07/2012
2829       IF substr (l_visit_type,1,1) = 'D' AND l_visit_id IS NOT NULL THEN --existing visit is a departure visit
2830         IF (l_log_statement >= l_log_current_level) THEN
2831           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 8');
2832         END IF;
2833         -- TYPE 8::Departure Org:: there is a dep visit assoc to dep org of curr FS::
2834         -- based on the profile value see if you can adjust the visit dates
2835         OPEN get_visit_end_time(l_visit_id);
2836         FETCH get_visit_end_time INTO l_visit_end_time;
2837         CLOSE get_visit_end_time;
2838 
2839         OPEN c_Visit_type_dur (l_visit_id);
2840         FETCH c_Visit_type_dur INTO l_visit_type_duration;
2841         CLOSE c_Visit_type_dur;
2842 
2843         IF flight_schedule_rec.est_departure_time > l_visit_end_time THEN
2844           l_difference_dep_time := (flight_schedule_rec.est_departure_time - l_visit_end_time)*24*60;
2845         ELSE
2846           l_difference_dep_time := (l_visit_end_time - flight_schedule_rec.est_departure_time)*24*60;
2847         END IF;
2848         IF  l_profile_varchar is not null  THEN
2849          l_profile_number := to_number(l_profile_varchar) ;
2850         ELSE
2851          l_profile_number := l_difference_dep_time-1;
2852         END IF;
2853 
2854         IF  l_profile_varchar2 is not null THEN
2855          l_profile_number2 := to_number(l_profile_varchar2) ;
2856         ELSE
2857          l_profile_number2 := flight_schedule_rec.est_departure_time-sysdate+1;
2858         END IF;
2859 
2860         IF((l_difference_dep_time > l_profile_number)
2861            AND (l_difference_dep_time <> 0)
2862            AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_departure_time) ) THEN
2863           l_start_date := flight_schedule_rec.est_departure_time - l_visit_type_duration/24;
2864           l_end_date := flight_schedule_rec.est_departure_time;
2865 
2866           l_visit_rec := null;
2867           l_visit_rec.START_DATE            := l_start_date;
2868           l_visit_rec.PLAN_END_DATE         := l_end_date;
2869           l_visit_rec.VISIT_ID              := l_visit_id;
2870 
2871           OPEN c_Visit(l_visit_id);
2872           FETCH c_Visit INTO c_Visit_rec;
2873           IF c_Visit%NOTFOUND THEN
2874             CLOSE c_Visit;
2875             IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
2876               Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
2877               Fnd_Msg_Pub.ADD;
2878               IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
2879                 fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
2880               END IF;
2881              END IF;
2882              RAISE Fnd_Api.g_exc_error;
2883           ELSE
2884              CLOSE c_Visit;
2885           END IF;
2886 
2887           -- Complete Visit Record
2888           l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
2889           l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
2890           l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
2891           l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
2892           l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
2893           l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
2894           l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
2895           l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
2896           l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
2897           l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
2898           l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
2899           l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
2900           l_visit_rec.AUTO_VISIT_TYPE_FLAG  := c_Visit_rec.AUTO_VISIT_TYPE_FLAG;--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
2901 
2902           IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
2903               fnd_log.string
2904                 ( fnd_log.level_procedure,
2905                   'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
2906                   'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id);
2907           END IF;
2908           AHL_VWP_VISITS_PVT.Update_Visit (
2909                                       p_api_version => l_api_version,
2910                                       p_init_msg_list => Fnd_Api.g_false,
2911                                       p_commit => Fnd_Api.g_false,
2912                                       p_validation_level => p_validation_level,
2913                                       p_module_type => 'API',
2914                                       p_x_visit_rec  => l_visit_rec,
2915                                       x_return_status => l_return_status,
2916                                       x_msg_count => l_msg_count,
2917                                       x_msg_data => l_msg_data
2918                                      );
2919 
2920           IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
2921               fnd_log.string
2922                 ( fnd_log.level_procedure,
2923                   'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
2924                   'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
2925           END IF;
2926           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2927             x_msg_count := FND_MSG_PUB.count_msg;
2928             IF (l_log_statement >= l_log_current_level) THEN
2929                 fnd_log.string(l_log_statement,
2930                                L_DEBUG_KEY,
2931                                'Errors from Update_Visit. Message count: ' || x_msg_count);
2932             END IF;
2933             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2934               RAISE FND_API.G_EXC_ERROR;
2935             ELSE
2936               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2937             END IF;
2938           END IF;  -- Return Status is not Success
2939         END IF;  -- IF((l_difference_dep_time > nvl(FND_PROFIL
2940       ELSE -- there is no visit at the dep org of current FS
2941         OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.departure_org_id,
2942                                     flight_schedule_rec.departure_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
2943                                     'DEPARTURE', NULL);
2944         FETCH get_oper_param_rows1 INTO get_oper_param_rec;
2945         IF get_oper_param_rows1%FOUND THEN
2946           --TYPE 9: Departure Org :: no visit at dep org of current FS :: create a departure visit
2947           IF (l_log_statement >= l_log_current_level) THEN
2948             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 9 and before calling Create_Departure_visit');
2949           END IF;
2950           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
2951           l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
2952           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
2953           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
2954 
2955           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
2956           SAVEPOINT OPER_VISIT_CREATION;
2957 
2958             Create_Departure_visit(
2959               p_oper_param_rec         => l_oper_param_rec,
2960               p_flight_schedule_rec    => l_flight_schedule_rec,
2961               p_present_time           => l_present_time,
2962               p_is_creation_success    => l_is_creation_success,
2963               x_return_status          => l_return_status,
2964               x_msg_count              => l_msg_count,
2965               x_msg_data               => l_msg_data
2966             );
2967           IF (l_log_statement >= l_log_current_level) THEN
2968             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Departure_visit and l_return_status: '||l_return_status);
2969             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
2970           END IF;
2971           IF l_is_creation_success<>'Y' THEN
2972               ROLLBACK TO OPER_VISIT_CREATION;
2973           END IF;
2974           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
2975 
2976         END IF;   --IF get_oper_param_rows1%FOUND THEN
2977         CLOSE get_oper_param_rows1;
2978       END IF; --IF substr (l_visit_type,1,1) = 'D'
2979     END IF; --IF get_oper_param_rows1%FOUND
2980    END IF; --IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
2981   END IF; --IF ( flight_schedule_rec.preceding_us_id IS NULL )
2982 
2983 
2984   IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
2985     fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'CASE II, flight_schedule_rec.unit_schedule_id= '||flight_schedule_rec.unit_schedule_id );
2986   END IF;
2987   -- CASE II: Now consider the arrival org of the flight schedule record
2988   OPEN get_non_null_org_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id);
2989   FETCH get_non_null_org_count INTO l_count_org;
2990   CLOSE get_non_null_org_count;
2991 
2992   OPEN get_non_null_dep_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
2993                               flight_schedule_rec.arrival_dept_id);
2994   FETCH get_non_null_dep_count INTO l_count_dep;
2995   CLOSE get_non_null_dep_count;
2996 
2997   OPEN get_non_null_cat_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
2998                               flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code);
2999   FETCH get_non_null_cat_count INTO l_count_cat;
3000   CLOSE get_non_null_cat_count;
3001 
3002   OPEN get_succeeding_us_det (flight_schedule_rec.unit_schedule_id);
3003   FETCH get_succeeding_us_det INTO l_succeeding_us_id, l_succ_dep_time;
3004   CLOSE get_succeeding_us_det;
3005 
3006   -- check if there exisits a visit and if yes, what visit type - either arrival or downtime
3007   AHL_AVF_OPER_VSTS_PVT.Get_Visit_Type(flight_schedule_rec.unit_schedule_id, 'S', l_visit_type, l_visit_id,l_visit_id2);
3008 
3009   --If the flight schedule does not have any succeeding flight then
3010   IF ( l_succeeding_us_id IS NULL ) THEN
3011 
3012     IF substr (l_visit_type,1,1) = 'A' THEN -- if there is a visit
3013 
3014       IF (l_log_statement >= l_log_current_level) THEN
3015         fnd_log.string(l_log_statement,L_DEBUG_KEY, ' Before Type 10.There is an existing arrival visit at arrival org');
3016       END IF;
3017       --TYPE 10: Arrival Org :: arrival visit associated to the arrival org of current FS :: update the visit
3018 
3019       OPEN get_visit_type_duration (get_oper_param_rec.visit_type_code,get_oper_param_rec.mc_id);
3020       FETCH get_visit_type_duration INTO l_visit_type_duration;
3021       CLOSE get_visit_type_duration;
3022 
3023       IF (l_log_statement >= l_log_current_level) THEN
3024               fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Visit type duration is: '||l_visit_type_duration);
3025       END IF;
3026       -- based on the profile value see if you can adjust the visit dates
3027       OPEN get_visit_start_time(l_visit_id);
3028       FETCH get_visit_start_time INTO l_visit_start_time;
3029       CLOSE get_visit_start_time;
3030 
3031       -- Added the below code to see if the visit can be updated or not : tchimira: bug 13844759
3032       l_can_update_visit := null;
3033       OPEN can_cancel_visit(l_visit_id);
3034       FETCH can_cancel_visit INTO l_can_update_visit;
3035       CLOSE can_cancel_visit;
3036 
3037       IF flight_schedule_rec.est_arrival_time > l_visit_start_time THEN
3038         l_difference_arrival_time := (flight_schedule_rec.est_arrival_time - l_visit_start_time)*24*60;
3039       ELSE
3040         l_difference_arrival_time := (l_visit_start_time - flight_schedule_rec.est_arrival_time)*24*60;
3041       END IF;
3042 
3043       IF  l_profile_varchar is not null  THEN
3044       l_profile_number := to_number(l_profile_varchar) ;
3045       ELSE
3046       l_profile_number := l_difference_arrival_time-1;
3047       END IF;
3048 
3049       IF  l_profile_varchar2 is not null THEN
3050       l_profile_number2 := to_number(l_profile_varchar2) ;
3051       ELSE
3052       l_profile_number2 := flight_schedule_rec.est_arrival_time-sysdate+1;
3053       END IF;
3054 
3055       IF((l_difference_arrival_time > l_profile_number)
3056          AND (l_difference_arrival_time <> 0)
3057          AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_arrival_time) ) THEN
3058        -- TCHIMIRA : Bug 13844759 : check if the visit is updateable.
3059        -- if yes, the update the visit else diconnect the visit and try to recreate a new departure visit
3060        IF l_can_update_visit IS NOT NULL THEN
3061 
3062         --TYPE 10: Arrival Org :: there is a visit at arrival org of current FS :: update the visit
3063         l_start_date := flight_schedule_rec.est_arrival_time;
3064         l_end_date := flight_schedule_rec.est_arrival_time + l_visit_type_duration/24;
3065 
3066         l_visit_rec := null;
3067         l_visit_rec.START_DATE            := l_start_date;
3068         l_visit_rec.PLAN_END_DATE         := l_end_date;
3069         l_visit_rec.VISIT_ID              := l_visit_id;
3070 
3071         OPEN c_Visit(l_visit_id);
3072         FETCH c_Visit INTO c_Visit_rec;
3073         IF c_Visit%NOTFOUND THEN
3074           CLOSE c_Visit;
3075           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3076             Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
3077             Fnd_Msg_Pub.ADD;
3078             IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
3079               fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
3080             END IF;
3081            END IF;
3082            RAISE Fnd_Api.g_exc_error;
3083         ELSE
3084            CLOSE c_Visit;
3085         END IF;
3086 
3087         -- Complete Visit Record
3088         l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
3089         l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
3090         l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
3091         l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
3092         l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
3093         l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
3094         l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
3095         l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
3096         l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
3097         l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
3098         l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
3099         l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
3100         l_visit_rec.AUTO_VISIT_TYPE_FLAG  := c_Visit_rec.AUTO_VISIT_TYPE_FLAG;--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
3101 
3102         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3103                    fnd_log.string
3104                     ( fnd_log.level_procedure,
3105                      'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3106                      'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id);
3107         END IF;
3108         AHL_VWP_VISITS_PVT.Update_Visit (
3109                                       p_api_version => l_api_version,
3110                                       p_init_msg_list => Fnd_Api.g_false,
3111                                       p_commit => Fnd_Api.g_false,
3112                                       p_validation_level => p_validation_level,
3113                                       p_module_type => 'API',
3114                                       p_x_visit_rec  => l_visit_rec,
3115                                       x_return_status => l_return_status,
3116                                       x_msg_count => l_msg_count,
3117                                       x_msg_data => l_msg_data
3118                                      );
3119 
3120         IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3121                    fnd_log.string
3122                     ( fnd_log.level_procedure,
3123                      'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3124                      'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
3125         END IF;
3126         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3127             x_msg_count := FND_MSG_PUB.count_msg;
3128             IF (l_log_statement >= l_log_current_level) THEN
3129                 fnd_log.string(l_log_statement,
3130                                L_DEBUG_KEY,
3131                                'Errors from Update_Visit. Message count: ' || x_msg_count);
3132             END IF;
3133             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3134                RAISE FND_API.G_EXC_ERROR;
3135             ELSE
3136                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3137             END IF;
3138         END IF;  -- Return Status is not Success
3139        ELSE
3140           --PRAKKUM :: 01/08/2012 :: Added procedure :: START
3141           Disconnect_Flight_Visit(
3142              p_visit_id               => l_visit_id,
3143              p_unit_schedule_id       => flight_schedule_rec.UNIT_SCHEDULE_ID,
3144              x_return_status          => l_return_status,
3145              x_msg_count              => l_msg_count,
3146              x_msg_data               => l_msg_data
3147           );
3148           IF (l_log_statement >= l_log_current_level) THEN
3149              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
3150           END IF;
3151 
3152           IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
3153            x_msg_count := l_msg_count;
3154            x_return_status := l_return_status;
3155            IF l_return_status = Fnd_Api.g_ret_sts_error THEN
3156              RAISE Fnd_Api.g_exc_error;
3157            ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
3158              RAISE Fnd_Api.g_exc_unexpected_error;
3159            END IF;
3160           END IF;
3161           --PRAKKUM :: 01/08/2012 :: Added procedure :: END
3162        END IF;-- check to see if the visit is updateble or not
3163       END IF; -- IF((l_difference_arrival_time > l_profile_number)
3164     END IF; --IF substr (l_visit_type,1,1) = 'A' THEN
3165 
3166     -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: START
3167     -- Before this, previous visits can be cancelled so fetch details again
3168     -- check if there exisits a visit and if yes, what visit type - either arrival or downtime
3169     AHL_AVF_OPER_VSTS_PVT.Get_Visit_Type(flight_schedule_rec.unit_schedule_id, 'S', l_visit_type, l_visit_id,l_visit_id2);
3170 
3171     l_can_update_visit := null;
3172     IF l_visit_id IS NOT NULL THEN
3173       OPEN can_cancel_visit(l_visit_id);
3174       FETCH can_cancel_visit INTO l_can_update_visit;
3175       CLOSE can_cancel_visit;
3176     END IF;
3177     -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: END
3178 
3179     -- TCHIMIRA : Bug 13844759 : 29 - Mar - 2012
3180     -- If there is no arrival visit, look for the possibility of creating one
3181     IF ((substr (l_visit_type,1,1) <> 'A') OR (l_can_update_visit IS NULL)) THEN
3182       -- check for the possibility of creating an arrival visit;
3183       IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
3184         fnd_log.string(l_log_statement,L_DEBUG_KEY, 'passing values are unit_config_header_id: '||flight_schedule_rec.unit_config_header_id
3185         ||', flight_schedule_rec.arrival_org_id: '||flight_schedule_rec.arrival_org_id||',  arrival_dept_id: , '||flight_schedule_rec.arrival_dept_id
3186         ||',  flight_category_code: ,'||flight_schedule_rec.flight_category_code||',  l_count_org: ,'||l_count_org||', l_count_dep:   ,'
3187         ||l_count_dep||',  l_count_cat: ,'||l_count_cat);
3188       END IF;
3189       OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
3190                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
3191                                 'ARRIVAL', NULL);
3192       FETCH get_oper_param_rows1 INTO get_oper_param_rec;
3193       IF get_oper_param_rows1%FOUND THEN
3194         -- if there is a possibility , create an arrival visit
3195         --TYPE 11: Arrival Org :: no visit associated to the arrival org of current FS :: create an arrival visit
3196         IF (l_log_statement >= l_log_current_level) THEN
3197           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 11 and before calling Create_Arrival_visit');
3198         END IF;
3199         l_oper_param_rec.visit_type_code  := get_oper_param_rec.visit_type_code;
3200         l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
3201         l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
3202         l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
3203 
3204           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
3205           SAVEPOINT OPER_VISIT_CREATION;
3206 
3207           Create_Arrival_visit(
3208             p_oper_param_rec       => l_oper_param_rec,
3209             p_flight_schedule_rec  => l_flight_schedule_rec,
3210             p_present_time         => l_present_time,
3211             p_is_creation_success  => l_is_creation_success,
3212             x_return_status        => l_return_status,
3213             x_msg_count         => l_msg_count,
3214             x_msg_data          => l_msg_data
3215             );
3216           IF (l_log_statement >= l_log_current_level) THEN
3217             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Arrival_visit and l_return_status: '||l_return_status);
3218             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
3219           END IF;
3220           IF l_is_creation_success<>'Y' THEN
3221             ROLLBACK TO OPER_VISIT_CREATION;
3222           END IF;
3223           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
3224 
3225       END IF; --IF get_oper_param_rows1%FOUND THEN
3226       CLOSE get_oper_param_rows1;
3227       -- Go to end
3228     END If; -- if condition to check for the possibility of creating an arrival visit
3229 
3230   --If the flight schedule has a succeeding event then
3231   ELSE
3232    -- check if any of the arrival current flight visit or departure succeeding flight visit does not satisfy any of
3233    -- the criteria to be automatically cancelled.
3234    -- PRAKKUM :: Bug 13844759 :: 26/07/2012
3235    load_can_cancel_visit(
3236       p_visit_id               => l_visit_id,
3237       p_visit_id2              => l_visit_id2,
3238       x_can_cancel_visit       => l_can_cancel_visit,
3239       x_can_cancel_visit2      => l_can_cancel_visit2,
3240       x_return_status          => l_return_status,
3241       x_msg_count              => l_msg_count,
3242       x_msg_data               => l_msg_data
3243       );
3244    IF (l_log_statement >= l_log_current_level) THEN
3245        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
3246    END IF;
3247 
3248    -- PRAKKUM :: Bug 13844759 :: 25/07/2012
3249    -- If existing visit is arrival visit, then see whether it needs to be updated or disassociate
3250    IF substr (l_visit_type,1,1) = 'A' THEN -- if there is a visit
3251        IF l_visit_id IS NOT NULL THEN
3252 
3253           IF (l_log_statement >= l_log_current_level) THEN
3254             fnd_log.string(l_log_statement,L_DEBUG_KEY, ' Before Type 10.There is an existing arrival visit at arrival org');
3255           END IF;
3256           --TYPE 10: Arrival Org :: arrival visit associated to the arrival org of current FS :: update the visit
3257 
3258           OPEN get_visit_type_duration (get_oper_param_rec.visit_type_code,get_oper_param_rec.mc_id);
3259           FETCH get_visit_type_duration INTO l_visit_type_duration;
3260           CLOSE get_visit_type_duration;
3261 
3262           IF (l_log_statement >= l_log_current_level) THEN
3263                   fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Visit type duration is: '||l_visit_type_duration);
3264           END IF;
3265           -- based on the profile value see if you can adjust the visit dates
3266           OPEN get_visit_start_time(l_visit_id);
3267           FETCH get_visit_start_time INTO l_visit_start_time;
3268           CLOSE get_visit_start_time;
3269 
3270           -- Added the below code to see if the visit can be updated or not : tchimira: bug 13844759
3271           l_can_update_visit := null;
3272           OPEN can_cancel_visit(l_visit_id);
3273           FETCH can_cancel_visit INTO l_can_update_visit;
3274           CLOSE can_cancel_visit;
3275 
3276           IF flight_schedule_rec.est_arrival_time > l_visit_start_time THEN
3277             l_difference_arrival_time := (flight_schedule_rec.est_arrival_time - l_visit_start_time)*24*60;
3278           ELSE
3279             l_difference_arrival_time := (l_visit_start_time - flight_schedule_rec.est_arrival_time)*24*60;
3280           END IF;
3281 
3282           IF  l_profile_varchar is not null  THEN
3283           l_profile_number := to_number(l_profile_varchar) ;
3284           ELSE
3285           l_profile_number := l_difference_arrival_time-1;
3286           END IF;
3287 
3288           IF  l_profile_varchar2 is not null THEN
3289           l_profile_number2 := to_number(l_profile_varchar2) ;
3290           ELSE
3291           l_profile_number2 := flight_schedule_rec.est_arrival_time-sysdate+1;
3292           END IF;
3293 
3294           IF((l_difference_arrival_time > l_profile_number)
3295              AND (l_difference_arrival_time <> 0)
3296              AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_arrival_time) ) THEN
3297            -- TCHIMIRA : Bug 13844759 : check if the visit is updateable.
3298            -- if yes, the update the visit else diconnect the visit and try to recreate a new departure visit
3299            IF l_can_update_visit IS NOT NULL THEN
3300 
3301             --TYPE 10: Arrival Org :: there is a visit at arrival org of current FS :: update the visit
3302             l_start_date := flight_schedule_rec.est_arrival_time;
3303             l_end_date := flight_schedule_rec.est_arrival_time + l_visit_type_duration/24;
3304 
3305             l_visit_rec := null;
3306             l_visit_rec.START_DATE            := l_start_date;
3307             l_visit_rec.PLAN_END_DATE         := l_end_date;
3308             l_visit_rec.VISIT_ID              := l_visit_id;
3309 
3310             OPEN c_Visit(l_visit_id);
3311             FETCH c_Visit INTO c_Visit_rec;
3312             IF c_Visit%NOTFOUND THEN
3313               CLOSE c_Visit;
3314               IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3315                 Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
3316                 Fnd_Msg_Pub.ADD;
3317                 IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
3318                   fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
3319                 END IF;
3320                END IF;
3321                RAISE Fnd_Api.g_exc_error;
3322             ELSE
3323                CLOSE c_Visit;
3324             END IF;
3325 
3326             -- Complete Visit Record
3327             l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
3328             l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
3329             l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
3330             l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
3331             l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
3332             l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
3333             l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
3334             l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
3335             l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
3336             l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
3337             l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
3338             l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
3339             l_visit_rec.AUTO_VISIT_TYPE_FLAG  := c_Visit_rec.AUTO_VISIT_TYPE_FLAG;--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
3340 
3341             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3342                        fnd_log.string
3343                         ( fnd_log.level_procedure,
3344                          'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3345                          'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id);
3346             END IF;
3347             AHL_VWP_VISITS_PVT.Update_Visit (
3348                                           p_api_version => l_api_version,
3349                                           p_init_msg_list => Fnd_Api.g_false,
3350                                           p_commit => Fnd_Api.g_false,
3351                                           p_validation_level => p_validation_level,
3352                                           p_module_type => 'API',
3353                                           p_x_visit_rec  => l_visit_rec,
3354                                           x_return_status => l_return_status,
3355                                           x_msg_count => l_msg_count,
3356                                           x_msg_data => l_msg_data
3357                                          );
3358 
3359             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3360                        fnd_log.string
3361                         ( fnd_log.level_procedure,
3362                          'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3363                          'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
3364             END IF;
3365             IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3366                 x_msg_count := FND_MSG_PUB.count_msg;
3367                 IF (l_log_statement >= l_log_current_level) THEN
3368                     fnd_log.string(l_log_statement,
3369                                    L_DEBUG_KEY,
3370                                    'Errors from Update_Visit. Message count: ' || x_msg_count);
3371                 END IF;
3372                 IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3373                    RAISE FND_API.G_EXC_ERROR;
3374                 ELSE
3375                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3376                 END IF;
3377             END IF;  -- Return Status is not Success
3378            ELSE
3379              --PRAKKUM :: 01/08/2012 :: Added procedure :: START
3380              Disconnect_Flight_Visit(
3381                 p_visit_id               => l_visit_id,
3382                 p_unit_schedule_id       => flight_schedule_rec.UNIT_SCHEDULE_ID,
3383                 x_return_status          => l_return_status,
3384                 x_msg_count              => l_msg_count,
3385                 x_msg_data               => l_msg_data
3386               );
3387              IF (l_log_statement >= l_log_current_level) THEN
3388                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
3389              END IF;
3390 
3391              IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
3392                x_msg_count := l_msg_count;
3393                x_return_status := l_return_status;
3394                IF l_return_status = Fnd_Api.g_ret_sts_error THEN
3395                  RAISE Fnd_Api.g_exc_error;
3396                ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
3397                  RAISE Fnd_Api.g_exc_unexpected_error;
3398                END IF;
3399              END IF;
3400              --PRAKKUM :: 01/08/2012 :: Added procedure :: END
3401              l_visit_id := NULL; --SInce visit is cancelled
3402 
3403            END IF;-- check to see if the visit is updateble or not
3404           END IF; -- IF((l_difference_arrival_time > l_profile_number)
3405       END IF; --l_visit_id IS NOT NULL
3406    END IF;
3407 
3408    -- PRAKKUM :: Bug 13844759 :: 26/07/2012
3409    -- check if any of the arrival current flight visit or departure succeeding flight visit does not satisfy any of
3410    -- the criteria to be automatically cancelled.
3411    load_can_cancel_visit(
3412       p_visit_id               => l_visit_id,
3413       p_visit_id2              => l_visit_id2,
3414       x_can_cancel_visit       => l_can_cancel_visit,
3415       x_can_cancel_visit2      => l_can_cancel_visit2,
3416       x_return_status          => l_return_status,
3417       x_msg_count              => l_msg_count,
3418       x_msg_data               => l_msg_data
3419       );
3420    IF (l_log_statement >= l_log_current_level) THEN
3421        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
3422    END IF;
3423 
3424    -- PRAKKUM :: Bug 14368696 :: 01/08/2012 :: START
3425    IF (l_can_cancel_visit IS NULL) THEN -- If downtime or arrival is not allowed to cancel
3426        IF substr (l_visit_type,1,1) = 'T' AND l_visit_id IS NOT NULL THEN --Downtime visit exists
3427 
3428         IF (l_log_statement >= l_log_current_level) THEN
3429            fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Handle case that downtime visit not allowed to cancel--@>');
3430         END IF;
3431 
3432         --Get ground time at the departing org of a FS
3433         OPEN get_ground_time (flight_schedule_rec.UNIT_SCHEDULE_ID, l_succeeding_us_id);
3434         FETCH get_ground_time INTO l_ground_time;
3435         CLOSE get_ground_time;
3436 
3437         -- check for the possibility to create a downtime visit
3438         OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
3439                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
3440                                 'DOWNTIME', l_ground_time);
3441         FETCH get_oper_param_rows1 INTO get_oper_param_rec;
3442         l_can_disconnect_visit := 'N';
3443         IF get_oper_param_rows1%FOUND THEN -- there is a possibility of creating a downtime visit at arrival org of prec FS
3444           OPEN get_visit_type_code(l_visit_id);
3445           FETCH get_visit_type_code INTO l_visit_type_code;
3446           CLOSE get_visit_type_code;
3447           IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
3448              l_can_disconnect_visit := 'Y';
3449           ELSE
3450              OPEN get_fs_arr_time(flight_schedule_rec.unit_schedule_id);
3451              FETCH get_fs_arr_time INTO l_start_date;
3452              CLOSE get_fs_arr_time;
3453 
3454              -- based on the profile value see if you can adjust the visit dates
3455              OPEN get_visit_start_time(l_visit_id);
3456              FETCH get_visit_start_time INTO l_visit_start_time;
3457              CLOSE get_visit_start_time;
3458 
3459              IF flight_schedule_rec.est_arrival_time > l_visit_start_time THEN
3460                 l_difference_arrival_time := (flight_schedule_rec.est_arrival_time - l_visit_start_time)*24*60;
3461              ELSE
3462                 l_difference_arrival_time := (l_visit_start_time - flight_schedule_rec.est_arrival_time)*24*60;
3463              END IF;
3464 
3465              IF  l_profile_varchar is not null  THEN
3466                 l_profile_number := to_number(l_profile_varchar) ;
3467              ELSE
3468                 l_profile_number := l_difference_arrival_time-1;
3469              END IF;
3470 
3471              IF  l_profile_varchar2 is not null THEN
3472                 l_profile_number2 := to_number(l_profile_varchar2) ;
3473              ELSE
3474                 l_profile_number2 := flight_schedule_rec.est_arrival_time-sysdate+1;
3475              END IF;
3476 
3477              IF (l_log_statement >= l_log_current_level) THEN
3478                  fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_difference_arrival_time--@>'||l_difference_arrival_time);
3479                  fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number--@>'||l_profile_number);
3480                  fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number2--@>'||l_profile_number);
3481                  fnd_log.string(l_log_statement,L_DEBUG_KEY, '(sysdate + l_profile_number2)--@>'||(cast((sysdate + l_profile_number2) AS timestamp)));
3482                  fnd_log.string(l_log_statement,L_DEBUG_KEY, 'flight_schedule_rec.est_arrival_time--@>'||(cast((flight_schedule_rec.est_arrival_time) AS timestamp)));
3483              END IF;
3484 
3485              IF((l_difference_arrival_time > l_profile_number)
3486                AND (l_difference_arrival_time <> 0)
3487                AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_arrival_time) ) THEN
3488                --TYPE 15: Arrival Org :: update visit at arrival org of current FS:: visit types do match
3489 
3490                   l_can_disconnect_visit := 'Y';
3491 
3492              END IF;
3493           END IF;
3494         ELSE
3495            l_can_disconnect_visit := 'Y';
3496         END IF;
3497         CLOSE get_oper_param_rows1;
3498 
3499         IF l_can_disconnect_visit ='Y' THEN
3500              --PRAKKUM :: 01/08/2012 :: Added procedure :: START
3501              Disconnect_Flight_Visit(
3502                 p_visit_id               => l_visit_id,
3503                 p_unit_schedule_id       => flight_schedule_rec.unit_schedule_id,
3504                 x_return_status          => l_return_status,
3505                 x_msg_count              => l_msg_count,
3506                 x_msg_data               => l_msg_data
3507               );
3508              IF (l_log_statement >= l_log_current_level) THEN
3509                 fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Disconnect_Flight_Visit and l_return_status: '||l_return_status);
3510              END IF;
3511 
3512              IF l_msg_count > 0 OR NVL(l_return_status,'x') <> FND_API.G_RET_STS_SUCCESS THEN
3513                x_msg_count := l_msg_count;
3514                x_return_status := l_return_status;
3515                IF l_return_status = Fnd_Api.g_ret_sts_error THEN
3516                  RAISE Fnd_Api.g_exc_error;
3517                ELSIF l_return_status = Fnd_Api.g_ret_sts_unexp_error THEN
3518                  RAISE Fnd_Api.g_exc_unexpected_error;
3519                END IF;
3520              END IF;
3521              --PRAKKUM :: 01/08/2012 :: Added procedure :: END
3522              l_visit_id := NULL; --Since visit is cancelled
3523         END IF;--l_can_disconnect_visit ='Y'
3524        END IF;--Downtime visit exists
3525    END IF; --IF (l_can_cancel_visit2 IS NULL)
3526 
3527    -- Recheck to confirm if any visit is disaasociated
3528    load_can_cancel_visit(
3529       p_visit_id               => l_visit_id,
3530       p_visit_id2              => l_visit_id2,
3531       x_can_cancel_visit       => l_can_cancel_visit,
3532       x_can_cancel_visit2      => l_can_cancel_visit2,
3533       x_return_status          => l_return_status,
3534       x_msg_count              => l_msg_count,
3535       x_msg_data               => l_msg_data
3536       );
3537    IF (l_log_statement >= l_log_current_level) THEN
3538        fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling load_can_cancel_visit and l_return_status: '||l_return_status);
3539    END IF;
3540 
3541    -- if departure succ flight visit can not be cancelled and there is no visit associated to arrival org of current FS
3542    -- then look for the possibility of creating an arrival visit at arrival org of current FS
3543    IF (l_can_cancel_visit2 IS NULL) THEN -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: START
3544 
3545       IF l_visit_id IS NULL THEN -- PRAKKUM :: Bug 13844759 :: 25/07/2012 :: END
3546 
3547         -- check for the possibility of creating an arrival visit;
3548         IF (l_log_statement >= l_log_current_level) THEN
3549                           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'passing values are unit_config_header_id: '
3550                           ||flight_schedule_rec.unit_config_header_id||', flight_schedule_rec.arrival_org_id: '||flight_schedule_rec.arrival_org_id||',  arrival_dept_id: , '
3551                           ||flight_schedule_rec.arrival_dept_id||',  flight_category_code: ,'||flight_schedule_rec.flight_category_code||',  l_count_org: ,'||l_count_org||', l_count_dep:   ,'
3552                           ||l_count_dep||',  l_count_cat: ,'||l_count_cat);
3553         END IF;
3554         OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
3555                                   flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
3556                                   'ARRIVAL', NULL);
3557         FETCH get_oper_param_rows1 INTO get_oper_param_rec;
3558         IF get_oper_param_rows1%FOUND THEN
3559           -- if there is possibility of creating an arrival visit
3560           --TYPE 12: Arrival Org :: no visit to cancel :: create an arrival visit
3561           IF (l_log_statement >= l_log_current_level) THEN
3562             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 12 and before calling Create_Arrival_visit');
3563           END IF;
3564           l_oper_param_rec.visit_type_code  := get_oper_param_rec.visit_type_code;
3565           l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
3566           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
3567           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
3568 
3569           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
3570           SAVEPOINT OPER_VISIT_CREATION;
3571             Create_Arrival_visit(
3572               p_oper_param_rec       => l_oper_param_rec,
3573               p_flight_schedule_rec  => l_flight_schedule_rec,
3574               p_present_time         => l_present_time,
3575               p_is_creation_success  => l_is_creation_success,
3576               x_return_status        => l_return_status,
3577               x_msg_count         => l_msg_count,
3578               x_msg_data          => l_msg_data
3579               );
3580           IF (l_log_statement >= l_log_current_level) THEN
3581             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Arrival_visit and l_return_status: '||l_return_status);
3582             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
3583           END IF;
3584           IF l_is_creation_success<>'Y' THEN
3585             ROLLBACK TO OPER_VISIT_CREATION;
3586           END IF;
3587           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
3588         END IF; -- IF get_oper_param_rows1%FOUND THEN
3589         CLOSE get_oper_param_rows1;
3590 
3591       END IF;
3592       -- Go to end
3593    END IF; --IF ((l_can_cancel_visit2 IS NULL) AND (l_visit_id IS NULL)) THEN
3594 
3595    OPEN get_non_null_org_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id);
3596    FETCH get_non_null_org_count INTO l_count_org;
3597    CLOSE get_non_null_org_count;
3598 
3599    OPEN get_non_null_dep_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
3600                               flight_schedule_rec.arrival_dept_id);
3601    FETCH get_non_null_dep_count INTO l_count_dep;
3602    CLOSE get_non_null_dep_count;
3603 
3604    OPEN get_non_null_cat_count(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
3605                               flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code);
3606    FETCH get_non_null_cat_count INTO l_count_cat;
3607    CLOSE get_non_null_cat_count;
3608 
3609    -- if both dep succ flight visit and arrival visit can be automatically cancelled
3610    IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
3611     --Get ground time at the arrival org of a FS
3612     OPEN get_ground_time (flight_schedule_rec.UNIT_SCHEDULE_ID, l_succeeding_us_id);
3613     FETCH get_ground_time INTO l_ground_time;
3614     CLOSE get_ground_time;
3615 
3616     -- check for the possibility to create a downtime visit
3617     OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
3618                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
3619                                 'DOWNTIME', l_ground_time);
3620     FETCH get_oper_param_rows1 INTO get_oper_param_rec;
3621     l_is_downtime_vst_created := 'Y';
3622     IF get_oper_param_rows1%FOUND THEN
3623       IF l_visit_id is null THEN -- if there is no visit associated to the arrival org of currrent FS
3624         IF (l_log_statement >= l_log_current_level) THEN
3625            fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 13');
3626         END IF;
3627 
3628         -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
3629         SAVEPOINT OPER_VISIT_CREATION;
3630 
3631         --TYPE 13: Arrival Org :: no arrival visit at arrival org of current FS::create a downime visit
3632         --cancel the departure visit of the succeeding flight if it is not null
3633         IF(l_visit_id2 is not null) then
3634                Delete_Oper_Visit(
3635                p_visit_id       => l_visit_id2,
3636                x_return_status  => l_return_status);
3637         END IF;--IF(l_visit_id2 is not null) then
3638         IF (l_log_statement >= l_log_current_level) THEN
3639           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
3640         END IF;
3641             l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
3642             l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
3643             l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
3644 
3645           -- we need to create a downtime visit and associate to the arrival org of current flight, so pass current flight details
3646             Create_Downtime_visit(
3647             p_oper_param_rec         => l_oper_param_rec,
3648             p_flight_schedule_rec    => l_flight_schedule_rec,
3649             p_present_time           => l_present_time,
3650             p_is_creation_success    => l_is_creation_success,
3651             x_return_status          => l_return_status,
3652             x_msg_count              => l_msg_count,
3653             x_msg_data               => l_msg_data
3654             );
3655 
3656         IF (l_log_statement >= l_log_current_level) THEN
3657           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
3658           fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
3659         END IF;
3660 
3661         IF l_is_creation_success<>'Y' THEN
3662            ROLLBACK TO OPER_VISIT_CREATION;
3663            l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating arrival visit
3664         END IF;
3665         -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
3666 
3667       ELSE -- if there is a visit associated to the arrival org of currrent FS
3668         -- We need to match the visit type of the visit and operational param row visit type
3669         OPEN get_visit_type_code(l_visit_id);
3670         FETCH get_visit_type_code INTO l_visit_type_code;
3671         CLOSE get_visit_type_code;
3672         IF l_visit_type_code <> get_oper_param_rec.visit_type_code THEN
3673           --  if it does not match then call create visit API
3674           -- Cancel the arrival visit with id 'l_visit_id' and also if any departure visit associated
3675           -- to the dep org of succ FS. then create a downtime visit
3676           IF (l_log_statement >= l_log_current_level) THEN
3677             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 14');
3678           END IF;
3679 
3680           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
3681           SAVEPOINT OPER_VISIT_CREATION;
3682 
3683           --TYPE 14: Arrival Org :: a visit at arrival org of current FS:: visit types do not match
3684           --Cancel the existing visit of the current FS
3685           Delete_Oper_Visit(
3686                p_visit_id       => l_visit_id,
3687                x_return_status  => l_return_status);
3688           --cancel the departure visit of the succ event if it is not null
3689           IF(l_visit_id2 is not null) then
3690             Delete_Oper_Visit(
3691                p_visit_id       => l_visit_id2,
3692                x_return_status  => l_return_status);
3693           END IF;--IF(l_visit_id2 is not null) then
3694           IF (l_log_statement >= l_log_current_level) THEN
3695             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before calling Create_Downtime_visit');
3696           END IF;
3697           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
3698           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
3699           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
3700 
3701           -- we need to create a downtime visit and associate to the arrival org of current flight, so pass current flight details
3702           Create_Downtime_visit(
3703             p_oper_param_rec         => l_oper_param_rec,
3704             p_flight_schedule_rec    => l_flight_schedule_rec,
3705             p_present_time           => l_present_time,
3706             p_is_creation_success    => l_is_creation_success,
3707             x_return_status          => l_return_status,
3708             x_msg_count              => l_msg_count,
3709             x_msg_data               => l_msg_data
3710           );
3711 
3712           IF (l_log_statement >= l_log_current_level) THEN
3713             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Downtime_visit and l_return_status: '||l_return_status);
3714             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
3715           END IF;
3716 
3717           IF l_is_creation_success<>'Y' THEN
3718             ROLLBACK TO OPER_VISIT_CREATION;
3719             l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating arrival visit
3720           END IF;
3721           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
3722 
3723         ELSE
3724           -- If the visit type matches, we need to update the visit dates
3725           --Before updating the visit first cancel the departure visit of the succ event if it is not null
3726           IF (l_log_statement >= l_log_current_level) THEN
3727             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 15');
3728           END IF;
3729           --TYPE 15: Arrival Org :: visit types do match ::  update the visit
3730           IF(l_visit_id2 IS NOT NULL) THEN
3731              Delete_Oper_Visit(
3732                p_visit_id       => l_visit_id2,
3733                x_return_status  => l_return_status);
3734           END IF;--IF(l_visit_id2 is not null) then
3735           -- Now update the visit dates to span the entire downtime
3736           OPEN get_fs_arr_time(flight_schedule_rec.unit_schedule_id);
3737           FETCH get_fs_arr_time INTO l_start_date;
3738           CLOSE get_fs_arr_time;
3739 
3740           -- based on the profile value see if you can adjust the visit dates
3741           OPEN get_visit_start_time(l_visit_id);
3742           FETCH get_visit_start_time INTO l_visit_start_time;
3743           CLOSE get_visit_start_time;
3744 
3745           IF flight_schedule_rec.est_arrival_time > l_visit_start_time THEN
3746              l_difference_arrival_time := (flight_schedule_rec.est_arrival_time - l_visit_start_time)*24*60;
3747           ELSE
3748              l_difference_arrival_time := (l_visit_start_time - flight_schedule_rec.est_arrival_time)*24*60;
3749           END IF;
3750 
3751           IF  l_profile_varchar is not null  THEN
3752            l_profile_number := to_number(l_profile_varchar) ;
3753           ELSE
3754            l_profile_number := l_difference_arrival_time-1;
3755           END IF;
3756 
3757           IF  l_profile_varchar2 is not null THEN
3758            l_profile_number2 := to_number(l_profile_varchar2) ;
3759           ELSE
3760            l_profile_number2 := flight_schedule_rec.est_arrival_time-sysdate+1;
3761           END IF;
3762 
3763           IF (l_log_statement >= l_log_current_level) THEN --PRAKKUM :: Bug 14342603 :: 18/07/2012
3764              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_difference_arrival_time--@>'||l_difference_arrival_time);
3765              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number--@>'||l_profile_number);
3766              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_profile_number2--@>'||l_profile_number);
3767              fnd_log.string(l_log_statement,L_DEBUG_KEY, '(sysdate + l_profile_number2)--@>'||(cast((sysdate + l_profile_number2) AS timestamp)));
3768              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'flight_schedule_rec.est_arrival_time--@>'||(cast((flight_schedule_rec.est_arrival_time) AS timestamp)));
3769           END IF;
3770 
3771           IF((l_difference_arrival_time > l_profile_number)
3772             AND (l_difference_arrival_time <> 0)
3773             AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_arrival_time) ) THEN
3774             --TYPE 15: Arrival Org :: update visit at arrival org of current FS:: visit types do match
3775 
3776             l_end_date := l_succ_dep_time;
3777             l_visit_rec.START_DATE            := l_start_date;
3778             l_visit_rec.PLAN_END_DATE         := l_end_date;
3779             l_visit_rec.VISIT_ID              := l_visit_id;
3780 
3781             OPEN c_Visit(l_visit_id);
3782             FETCH c_Visit INTO c_Visit_rec;
3783             IF c_Visit%NOTFOUND THEN
3784               CLOSE c_Visit;
3785               IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3786                 Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
3787                 Fnd_Msg_Pub.ADD;
3788                 IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
3789                   fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
3790                 END IF;
3791                END IF;
3792                RAISE Fnd_Api.g_exc_error;
3793             ELSE
3794                CLOSE c_Visit;
3795             END IF;
3796 
3797             -- Complete Visit Record
3798             l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
3799             l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
3800             l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
3801             l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
3802             l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
3803             l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
3804             l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
3805             l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
3806             l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
3807             l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
3808             l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
3809             l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
3810             l_visit_rec.AUTO_VISIT_TYPE_FLAG  := 'T';--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
3811 
3812             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3813                 fnd_log.string
3814                   ( fnd_log.level_procedure,
3815                     'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3816                     'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id);
3817             END IF;
3818             AHL_VWP_VISITS_PVT.Update_Visit (
3819                                       p_api_version => l_api_version,
3820                                       p_init_msg_list => Fnd_Api.g_false,
3821                                       p_commit => Fnd_Api.g_false,
3822                                       p_validation_level => p_validation_level,
3823                                       p_module_type => 'API',
3824                                       p_x_visit_rec  => l_visit_rec,
3825                                       x_return_status => l_return_status,
3826                                       x_msg_count => l_msg_count,
3827                                       x_msg_data => l_msg_data
3828                                      );
3829 
3830             IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3831                 fnd_log.string
3832                    ( fnd_log.level_procedure,
3833                      'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3834                      'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
3835             END IF;
3836             IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3837               x_msg_count := FND_MSG_PUB.count_msg;
3838               IF (l_log_statement >= l_log_current_level) THEN
3839                   fnd_log.string(l_log_statement,
3840                                  L_DEBUG_KEY,
3841                                  'Errors from Update_Visit. Message count: ' || x_msg_count);
3842               END IF;
3843               IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3844                  RAISE FND_API.G_EXC_ERROR;
3845               ELSE
3846                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3847               END IF;
3848             END IF;  -- Return Status is not Success
3849           END IF;--IF((l_difference_arrival_time > nv
3850         END IF;--IF of l_visit_type_code <> get_oper_param_rec.visit_type_code
3851       END IF;-- else of l_visit_id is null
3852       CLOSE get_oper_param_rows1;
3853     ELSE
3854       CLOSE get_oper_param_rows1;
3855       l_is_downtime_vst_created := 'N'; -- Downtime visit creation failed, so consider for creating arrival visit
3856     END IF;
3857     IF l_is_downtime_vst_created = 'N' THEN
3858       -- We cannot create a downtime visit at the arrival org of current FS
3859       -- if there is any visit associated to the arrival org of current FS
3860       IF l_visit_id is not null THEN
3861         -- we need to check the type of the visit associated to the arrival org
3862         -- it can be either arrival or downtime
3863         -- if the visit type of the existing visit matches with visit types of the operational param rows
3864         OPEN get_visit_type_code (l_visit_id);
3865         FETCH get_visit_type_code INTO l_visit_type_code;
3866         CLOSE get_visit_type_code;
3867         -- we need to check the type of the visit - either arrival or downtime
3868         OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
3869                                 flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
3870                                 'DOWNTIME', null);
3871         LOOP
3872          FETCH get_oper_param_rows1 INTO get_oper_param_rec;
3873          EXIT WHEN get_oper_param_rows1%NOTFOUND;
3874          IF get_oper_param_rec.visit_type_code = l_visit_type_code THEN
3875            l_flag := 'Y';
3876          END IF;
3877         END LOOP;
3878         CLOSE get_oper_param_rows1;
3879         IF l_flag = 'Y' THEN
3880           -- it is a downtime visit and we need to cancel that visit
3881           Delete_Oper_Visit(
3882                p_visit_id       => l_visit_id,
3883                x_return_status  => l_return_status);
3884         END IF; --IF l_flag = 'Y'
3885       END IF;-- l_visit_id is not null
3886       -- PRAKKUM :: Bug 13844759 :: 31/07/2012
3887       IF ((substr (l_visit_type,1,1) <> 'N') and (l_flag <> 'Y') AND l_visit_id is not null) THEN -- there is an arrival visit to the arrival org
3888         -- based on the profile value see if you can adjust the visit dates
3889         OPEN get_visit_start_time(l_visit_id);
3890         FETCH get_visit_start_time INTO l_visit_start_time;
3891         CLOSE get_visit_start_time;
3892 
3893         OPEN c_Visit_type_dur (l_visit_id);
3894         FETCH c_Visit_type_dur INTO l_visit_type_duration;
3895         CLOSE c_Visit_type_dur;
3896 
3897         IF flight_schedule_rec.est_arrival_time > l_visit_start_time THEN
3898           l_difference_arrival_time := (flight_schedule_rec.est_arrival_time - l_visit_start_time)*24*60;
3899         ELSE
3900           l_difference_arrival_time := (l_visit_start_time - flight_schedule_rec.est_arrival_time)*24*60;
3901         END IF;
3902         IF  l_profile_varchar is not null  THEN
3903           l_profile_number := to_number(l_profile_varchar) ;
3904         ELSE
3905           l_profile_number := l_difference_arrival_time-1;
3906         END IF;
3907 
3908         IF  l_profile_varchar2 is not null THEN
3909           l_profile_number2 := to_number(l_profile_varchar2) ;
3910         ELSE
3911           l_profile_number2 := flight_schedule_rec.est_arrival_time-sysdate+1;
3912         END IF;
3913 
3914         IF((l_difference_arrival_time > l_profile_number)
3915           AND (l_difference_arrival_time <> 0)
3916           AND ((sysdate + l_profile_number2)> flight_schedule_rec.est_arrival_time) ) THEN
3917           IF (l_log_statement >= l_log_current_level) THEN
3918             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 16');
3919           END IF;
3920           --TYPE 16: Arrival Org :: a visit at arrival org of current FS:: update the arrival visit
3921           l_start_date := flight_schedule_rec.est_arrival_time;
3922           l_end_date := flight_schedule_rec.est_arrival_time + l_visit_type_duration/24;
3923 
3924           l_visit_rec := null;
3925           l_visit_rec.START_DATE            := l_start_date;
3926           l_visit_rec.PLAN_END_DATE         := l_end_date;
3927           l_visit_rec.VISIT_ID              := l_visit_id;
3928 
3929           OPEN c_Visit(l_visit_id);
3930           FETCH c_Visit INTO c_Visit_rec;
3931           IF c_Visit%NOTFOUND THEN
3932             CLOSE c_Visit;
3933             IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3934               Fnd_Message.set_name('AHL', 'AHL_API_RECORD_NOT_FOUND');
3935               Fnd_Msg_Pub.ADD;
3936               IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level) THEN
3937                 fnd_log.string(fnd_log.level_statement,L_DEBUG_KEY,'Visit not found for - ' ||l_visit_rec.visit_id );
3938               END IF;
3939              END IF;
3940              RAISE Fnd_Api.g_exc_error;
3941           ELSE
3942              CLOSE c_Visit;
3943           END IF;
3944 
3945           -- Complete Visit Record
3946           l_visit_rec.VISIT_TYPE_CODE       := c_Visit_rec.VISIT_TYPE_CODE;
3947           l_visit_rec.SERVICE_REQUEST_ID    := c_Visit_rec.SERVICE_REQUEST_ID;
3948           l_visit_rec.SPACE_CATEGORY_CODE   := c_Visit_rec.SPACE_CATEGORY_CODE;
3949           l_visit_rec.OBJECT_VERSION_NUMBER := c_Visit_rec.OBJECT_VERSION_NUMBER;
3950           l_visit_rec.VISIT_NAME            := c_Visit_rec.VISIT_NAME;
3951           l_visit_rec.DESCRIPTION           := c_Visit_rec.DESCRIPTION;
3952           l_visit_rec.PRIORITY_CODE         := c_Visit_rec.PRIORITY_CODE;
3953           l_visit_rec.PROJ_TEMPLATE_ID      := c_Visit_rec.PROJECT_TEMPLATE_ID;
3954           l_visit_rec.ITEM_INSTANCE_ID      := c_Visit_rec.ITEM_INSTANCE_ID;
3955           l_visit_rec.UNIT_SCHEDULE_ID      := c_Visit_rec.UNIT_SCHEDULE_ID;
3956           l_visit_rec.ORGANIZATION_ID       := c_Visit_rec.ORGANIZATION_ID;
3957           l_visit_rec.DEPARTMENT_ID         := c_Visit_rec.DEPARTMENT_ID;
3958           l_visit_rec.AUTO_VISIT_TYPE_FLAG  := c_Visit_rec.AUTO_VISIT_TYPE_FLAG;--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
3959 
3960           IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3961               fnd_log.string
3962                 ( fnd_log.level_procedure,
3963                   'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3964                   'Before calling AHL_VWP_VISITS_PVT.Update_Visit autovst_oper_id: '||get_oper_param_rec.autovst_oper_id);
3965           END IF;
3966           AHL_VWP_VISITS_PVT.Update_Visit (
3967                                       p_api_version => l_api_version,
3968                                       p_init_msg_list => Fnd_Api.g_false,
3969                                       p_commit => Fnd_Api.g_false,
3970                                       p_validation_level => p_validation_level,
3971                                       p_module_type => 'API',
3972                                       p_x_visit_rec  => l_visit_rec,
3973                                       x_return_status => l_return_status,
3974                                       x_msg_count => l_msg_count,
3975                                       x_msg_data => l_msg_data
3976                                      );
3977 
3978           IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
3979                    fnd_log.string
3980                     ( fnd_log.level_procedure,
3981                      'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
3982                      'After calling AHL_VWP_VISITS_PVT.Update_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
3983           END IF;
3984           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3985             x_msg_count := FND_MSG_PUB.count_msg;
3986             IF (l_log_statement >= l_log_current_level) THEN
3987                 fnd_log.string(l_log_statement,
3988                                L_DEBUG_KEY,
3989                                'Errors from Update_Visit. Message count: ' || x_msg_count);
3990             END IF;
3991             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3992               RAISE FND_API.G_EXC_ERROR;
3993             ELSE
3994               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3995             END IF;
3996           END IF;  -- Return Status is not Success
3997         END If; -- IF((l_difference_arrival_time > nvl(FND_PROFILE
3998       ELSE  -- there is no visit associated to the arrival org of current FS
3999         -- Look for creating an arrival visit
4000         OPEN get_oper_param_rows1(flight_schedule_rec.unit_config_header_id,flight_schedule_rec.arrival_org_id,
4001                                     flight_schedule_rec.arrival_dept_id, flight_schedule_rec.flight_category_code, l_count_org, l_count_dep, l_count_cat,
4002                                     'ARRIVAL', NULL);
4003         FETCH get_oper_param_rows1 INTO get_oper_param_rec;
4004         IF get_oper_param_rows1%FOUND THEN
4005           --TYPE 17: Arrival Org :: no visit at arrival org of current FS:: create the arrival visit
4006           IF (l_log_statement >= l_log_current_level) THEN
4007              fnd_log.string(l_log_statement,L_DEBUG_KEY, 'Before Type 17 and before calling Create_Arrival_visit');
4008           END IF;
4009           l_oper_param_rec.visit_type_code  :=  get_oper_param_rec.visit_type_code;
4010           l_oper_param_rec.mc_id            := get_oper_param_rec.mc_id;
4011           l_oper_param_rec.alternate_dep_id := get_oper_param_rec.alternate_dep_id;
4012           l_oper_param_rec.autovst_oper_id  := get_oper_param_rec.autovst_oper_id;
4013 
4014           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: START
4015           SAVEPOINT OPER_VISIT_CREATION;
4016 
4017           Create_Arrival_visit(
4018             p_oper_param_rec       => l_oper_param_rec,
4019             p_flight_schedule_rec  => l_flight_schedule_rec,
4020             p_present_time         => l_present_time,
4021             p_is_creation_success  => l_is_creation_success,
4022             x_return_status        => l_return_status,
4023             x_msg_count         => l_msg_count,
4024             x_msg_data          => l_msg_data
4025             );
4026           IF (l_log_statement >= l_log_current_level) THEN
4027             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'After calling Create_Arrival_visit and l_return_status: '||l_return_status);
4028             fnd_log.string(l_log_statement,L_DEBUG_KEY, 'l_is_creation_success: '||l_is_creation_success);
4029           END IF;
4030           IF l_is_creation_success<>'Y' THEN
4031             ROLLBACK TO OPER_VISIT_CREATION;
4032           END IF;
4033           -- Bug 14336467 :: PRAKKUM :: 13/07/2012 :: END
4034 
4035         END IF;
4036         CLOSE get_oper_param_rows1; -- Added the line for Bug # 13706579 by TCHIMIRA on 15th Feb 2012
4037       END IF; --IF substr (l_visit_type,1,1) <> 'N'
4038     END IF; --IF get_oper_param_rows1%FOUND
4039    END IF; -- IF (l_can_cancel_visit IS NOT NULL) AND (l_can_cancel_visit2 IS NOT NULL) THEN
4040   END IF; --IF ( l_succeeding_us_id IS NULL )
4041 
4042   --Update the Flight Schedule auto create status to 'R'
4043   UPDATE AHL_UNIT_SCHEDULES
4044   set AUTOVISIT_PROCESS_STATUS = 'R',
4045       OBJECT_VERSION_NUMBER = object_version_number + 1,
4046       LAST_UPDATE_DATE      = SYSDATE,
4047       LAST_UPDATED_BY       = Fnd_Global.USER_ID,
4048       LAST_UPDATE_LOGIN     = Fnd_Global.LOGIN_ID
4049   WHERE UNIT_SCHEDULE_ID    = flight_schedule_rec.UNIT_SCHEDULE_ID;
4050  END LOOP;
4051 
4052  -- END of API body.
4053  -- Standard check of p_commit.
4054  IF Fnd_Api.To_Boolean (p_commit) THEN
4055     COMMIT WORK;
4056  END IF;
4057 
4058  Fnd_Msg_Pub.count_and_get(
4059            p_encoded => Fnd_Api.g_false,
4060            p_count   => x_msg_count,
4061            p_data    => x_msg_data
4062  );
4063 
4064  IF (l_log_procedure >= l_log_current_level) THEN
4065      fnd_log.string(l_log_procedure,
4066                     L_DEBUG_KEY ||'.end',
4067                     'At the end of PL SQL procedure. Return Status = ' || x_return_status);
4068  END IF;
4069 
4070 EXCEPTION
4071 
4072  WHEN FND_API.G_EXC_ERROR THEN
4073    x_return_status := FND_API.G_RET_STS_ERROR;
4074    ROLLBACK TO Update_Oper_Visit_pvt;
4075    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
4076                               p_data  => x_msg_data,
4077                               p_encoded => fnd_api.g_false);
4078 
4079  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4080    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4081    ROLLBACK TO Update_Oper_Visit_pvt;
4082    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
4083                               p_data  => x_msg_data,
4084                               p_encoded => fnd_api.g_false);
4085 
4086  WHEN OTHERS THEN
4087     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4088     ROLLBACK TO Update_Oper_Visit_pvt;
4089 
4090     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4091        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
4092                                p_procedure_name => 'Update_Oper_Visit',
4093                                p_error_text     => SUBSTR(SQLERRM,1,500));
4094     END IF;
4095 
4096     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
4097                                p_data  => x_msg_data,
4098                                p_encoded => fnd_api.g_false);
4099 END Update_Oper_Visit;
4100 
4101 --TCHIMIRA :: 14-Jun-2012 :: start
4102 ---------------------------------------------------------------------------------------
4103 -- PROCEDURE
4104 --    Create_Departure_visit
4105 --  Type   : Private
4106 --
4107 -- PURPOSE
4108 --    To create a departure visit at departure organization of the flight
4109 --
4110 --  Parameters :
4111 --  Create_Departure_visit Parameters
4112 --      p_oper_param_rec         IN oper_param_rec_type Required
4113 --      p_flight_schedule_rec    IN flight_schedule_rec_type Required
4114 --
4115 --  Standard OUT Parameters :
4116 --      x_return_status           OUT     VARCHAR2     Required
4117 --      x_msg_count               OUT     NUMBER       Required
4118 --      x_msg_data                OUT     VARCHAR2     Required
4119 --
4120 ----------------------------------------------------------------------------------------
4121 
4122 PROCEDURE Create_Departure_visit(
4123     p_oper_param_rec       IN      oper_param_rec_type,
4124     p_flight_schedule_rec  IN      flight_schedule_rec_type,
4125     p_present_time         IN      DATE, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4126     p_is_creation_success  OUT NOCOPY VARCHAR2, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4127     x_return_status        OUT NOCOPY     VARCHAR2,
4128     x_msg_count            OUT NOCOPY NUMBER,
4129     x_msg_data             OUT NOCOPY VARCHAR2)
4130 IS
4131 
4132 -- Cursor to find visit type duration
4133 Cursor get_visit_type_duration(c_visit_type_code IN VARCHAR2, c_mc_id IN NUMBER)
4134 IS
4135 SELECT estimated_duration
4136 FROM ahl_visit_types_b
4137 WHERE visit_type_code = c_visit_type_code
4138  AND mc_id = c_mc_id
4139  AND status_code = 'COMPLETE';
4140 
4141 Cursor is_org_in_current_OU (c_org_id IN NUMBER)
4142 IS
4143 SELECT 'X'
4144 FROM org_organization_definitions hou
4145 WHERE hou.organization_id                                  = c_org_id
4146 AND NVL(hou.operating_unit,mo_global.get_current_org_id()) = mo_global.get_current_org_id();
4147 
4148 -- Cursor to get the visit Number from visit_id
4149 CURSOR get_visit_number(c_visit_id IN NUMBER) IS
4150 SELECT visit_number
4151 FROM ahl_visits_b
4152 WHERE visit_id = c_visit_id;
4153 
4154 --Added cursor to get flight details for loggin :: PRAKKUM :: 01/08/2012
4155 Cursor get_flight_details (c_us_id IN NUMBER)
4156 IS
4157 SELECT flight_number
4158 FROM ahl_unit_schedules
4159 WHERE unit_schedule_id  = c_us_id;
4160 flt_dets_rec get_flight_details%RowType;
4161 
4162   L_API_NAME             CONSTANT VARCHAR2(30) := 'Create_Departure_visit';
4163   L_DEBUG_KEY            CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
4164   l_visit_type_duration  NUMBER;
4165   l_visit_rec            AHL_VWP_VISITS_PVT.Visit_Rec_Type;
4166   l_is_org_in_curr_OU    VARCHAR2(1);
4167   l_visit_number         NUMBER;
4168   l_start_date_ts        TIMESTAMP;
4169   l_end_date_ts          TIMESTAMP;
4170   l_start_date           DATE;
4171   l_start_hour           NUMBER;
4172   l_start_min            NUMBER;
4173   l_end_date             DATE;
4174   l_end_hour             NUMBER;
4175   l_end_min              NUMBER;
4176   l_return_status        VARCHAR2(1);
4177   -- SATRAJEN :: Bug 14336467 :: Prevent erroring out in case of Operational visit.
4178   l_pub_msg              VARCHAR2(2000);
4179 
4180 BEGIN
4181 
4182  SAVEPOINT Create_Dep_visit_pvt;
4183 
4184  IF (l_log_procedure >= l_log_current_level) THEN
4185       fnd_log.string(l_log_procedure,
4186                      L_DEBUG_KEY ||'.begin',
4187                      'At the start of PL SQL function.');
4188  END IF;
4189 
4190  -- initialize procedure return status to success
4191  x_return_status := FND_API.G_RET_STS_SUCCESS;
4192 
4193  -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4194  p_is_creation_success := 'Y'; --initialize to success
4195 
4196  OPEN get_visit_type_duration (p_oper_param_rec.visit_type_code,p_oper_param_rec.mc_id);
4197  FETCH get_visit_type_duration INTO l_visit_type_duration;
4198  CLOSE get_visit_type_duration;
4199 
4200  l_visit_rec := null;
4201 
4202  SELECT NAME INTO l_visit_rec.UNIT_NAME FROM AHL_UNIT_CONFIG_HEADERS
4203   WHERE unit_config_header_id= p_flight_schedule_rec.unit_config_header_id;
4204 
4205  -- Populate all the visit attributes
4206  l_visit_rec.VISIT_TYPE_CODE       := p_oper_param_rec.visit_type_code;
4207  l_visit_rec.ORGANIZATION_ID       := p_flight_schedule_rec.departure_org_id;
4208  l_visit_rec.VISIT_NAME            := 'Auto-Visit';
4209  l_visit_rec.DEPARTMENT_ID         := NVL(p_oper_param_rec.alternate_dep_id, p_flight_schedule_rec.departure_dept_id);
4210 
4211  l_start_date := p_flight_schedule_rec.est_departure_time - l_visit_type_duration/24;
4212  l_start_date_ts := cast(l_start_date as timestamp);
4213  l_start_hour := (extract(HOUR from l_start_date_ts));
4214  l_start_min := (extract(MINUTE from l_start_date_ts));
4215 
4216  l_end_date := p_flight_schedule_rec.est_departure_time;
4217  l_end_date_ts := cast(l_end_date as timestamp);
4218  l_end_hour := (extract(HOUR from l_end_date_ts));
4219  l_end_min := (extract(MINUTE from l_end_date_ts));
4220 
4221  l_visit_rec.START_DATE            := trunc(l_start_date);
4222  l_visit_rec.START_HOUR            := l_start_hour;
4223  l_visit_rec.START_MIN             := l_start_min;
4224  l_visit_rec.PLAN_END_DATE         := trunc(l_end_date);
4225  l_visit_rec.PLAN_END_HOUR         := l_end_hour;
4226  l_visit_rec.PLAN_END_MIN          := l_end_min;
4227  l_visit_rec.VISIT_CREATE_TYPE     := 'PLANNING';
4228  l_visit_rec.UNIT_SCHEDULE_ID      := p_flight_schedule_rec.unit_schedule_id;
4229  l_visit_rec.AUTO_VISIT_TYPE_FLAG  := 'D';--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
4230 
4231  -- Check if the visit org is in the current OU
4232  l_is_org_in_curr_OU := null;
4233  OPEN is_org_in_current_OU (l_visit_rec.ORGANIZATION_ID);
4234  FETCH is_org_in_current_OU INTO l_is_org_in_curr_OU;
4235  CLOSE is_org_in_current_OU;
4236 
4237  IF (l_log_statement >= l_log_current_level) THEN
4238       fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_start_date --@>'||cast(l_start_date as timestamp));
4239       fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_end_date --@>'||cast(l_end_date as timestamp));
4240       fnd_log.string(l_log_statement, L_DEBUG_KEY , 'p_present_time --@>'||cast(p_present_time as timestamp));
4241       fnd_log.string(l_log_statement, L_DEBUG_KEY , 'mo_global.get_current_org_id() --@>'||mo_global.get_current_org_id());
4242       fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_visit_rec.ORGANIZATION_ID --@>'||l_visit_rec.ORGANIZATION_ID);
4243       fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_is_org_in_curr_OU --@>'||l_is_org_in_curr_OU);
4244  END IF;
4245 
4246  IF l_start_date < p_present_time THEN -- Bug 14336486 :: PRAKKUM :: 12/07/2012
4247     -- Do not create a visit
4248     OPEN get_flight_details(p_flight_schedule_rec.unit_schedule_id);
4249     FETCH get_flight_details INTO flt_dets_rec;
4250     CLOSE get_flight_details;
4251 
4252     IF l_end_date < p_present_time THEN
4253         fnd_file.put_line(fnd_file.log, 'Validation Failure: No departure visit created, since flight '||flt_dets_rec.flight_number||' is already departured.');
4254     ELSE
4255         fnd_file.put_line(fnd_file.log, 'Validation Failure: No departure visit created, since visit start date for flight '||flt_dets_rec.flight_number||' is deriving to past.');
4256     END IF;
4257     p_is_creation_success := 'N'; -- Flag as creation failed due to validations
4258 
4259  ELSE
4260    -- IF the visit org is NOT in current OU, do not create the visit
4261    IF l_is_org_in_curr_OU = 'X' THEN
4262      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4263               fnd_log.string
4264              ( fnd_log.level_procedure,
4265                'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
4266                'Before calling AHL_VWP_VISITS_PVT.Create_Visit autovst_oper_id: '||p_oper_param_rec.autovst_oper_id);
4267      END IF;
4268      AHL_VWP_VISITS_PVT.Create_Visit (
4269                                          p_api_version => 1.0,
4270                                          p_module_type => 'API',
4271                                          p_x_visit_rec  => l_visit_rec,
4272                                          x_return_status => l_return_status,
4273                                          x_msg_count => x_msg_count,
4274                                          x_msg_data => x_msg_data
4275                                          );
4276 
4277      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4278                fnd_log.string
4279                ( fnd_log.level_procedure,
4280                  'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
4281                  'After calling AHL_VWP_VISITS_PVT.Create_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
4282      END IF;
4283 
4284      -- SATRAJEN :: Bug 14336467 :: Prevent Erroring out when the called from Operational Visit Procedure. :: START
4285      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4286          l_pub_msg := FND_MSG_PUB.Get_Detail (p_msg_index => FND_MSG_PUB.G_LAST);
4287          IF (l_pub_msg like '%AHL_VWP_PRD_MR_ASSOC_FAIL%') THEN
4288            IF (l_log_statement >= l_log_current_level) THEN
4289              fnd_log.string
4290              ( l_log_statement,
4291                L_DEBUG_KEY,
4292                'visit id= '||l_visit_rec.visit_id || 'Had problems with the MRs associated. So departure visit creation is not successful. ');
4293            END IF;
4294            x_return_status := FND_API.G_RET_STS_SUCCESS;
4295            p_is_creation_success := 'N'; -- Flag as creation of visit failed due to validations
4296            -- Remove message from FND
4297            FND_MSG_PUB.Delete_Msg(p_msg_index => FND_MSG_PUB.count_msg);
4298 
4299            OPEN get_flight_details(p_flight_schedule_rec.unit_schedule_id);
4300            FETCH get_flight_details INTO flt_dets_rec;
4301            CLOSE get_flight_details;
4302 
4303            fnd_file.put_line(fnd_file.log, 'Validation Failure: Had problems with the MRs associated. So departure visit creation for flight '||flt_dets_rec.flight_number||' is not successful.');
4304          ELSE
4305            x_return_status := l_return_status ;
4306            x_msg_count := FND_MSG_PUB.count_msg;
4307            IF (l_log_statement >= l_log_current_level) THEN
4308                   fnd_log.string(l_log_statement,
4309                                  L_DEBUG_KEY,
4310                                  'Errors from Create_Visit. Message count: ' || x_msg_count);
4311            END IF;
4312            IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4313              RAISE FND_API.G_EXC_ERROR;
4314            ELSE
4315              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4316            END IF;
4317          END IF;
4318       ELSE
4319            x_return_status := l_return_status ;
4320            l_visit_number := NULL;
4321            OPEN get_visit_number(l_visit_rec.visit_id);
4322            FETCH get_visit_number INTO l_visit_number;
4323            CLOSE get_visit_number;
4324            fnd_file.put_line(fnd_file.log, 'Created departure visit number -> '||l_visit_number);
4325          -- End of logging
4326      END IF;
4327      -- SATRAJEN :: Bug 14336467 :: Prevent Erroring out when the called from Operational Visit Procedure. :: END
4328    END IF; -- IF l_is_org_in_curr_OU = 'X'
4329  END IF; -- l_start_date
4330 
4331  IF (l_log_procedure >= l_log_current_level) THEN
4332       fnd_log.string(l_log_procedure,
4333                      L_DEBUG_KEY ||'.end',
4334                      'At the end of PL SQL function.');
4335  END IF;
4336 EXCEPTION
4337  WHEN FND_API.G_EXC_ERROR THEN
4338    x_return_status := FND_API.G_RET_STS_ERROR;
4339    ROLLBACK TO Create_Dep_visit_pvt;
4340    FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4341                               p_data  => x_msg_data,
4342                               p_encoded => fnd_api.g_false);
4343 
4344  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4345    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4346    ROLLBACK TO Create_Dep_visit_pvt;
4347    FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4348                               p_data  => x_msg_data,
4349                               p_encoded => fnd_api.g_false);
4350 
4351  WHEN OTHERS THEN
4352     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4353     ROLLBACK TO Create_Dep_visit_pvt;
4354     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4355        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
4356                                p_procedure_name => 'Create_Departure_visit',
4357                                p_error_text     => SUBSTR(SQLERRM,1,500));
4358     END IF;
4359     FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4360                                p_data  => x_msg_data,
4361                                p_encoded => fnd_api.g_false);
4362 END Create_Departure_visit;
4363 
4364 ---------------------------------------------------------------------------------------
4365 -- PROCEDURE
4366 --    Create_Arrival_visit
4367 -- Type             : Public
4368 -- PURPOSE
4369 --    To create an Arrival visit at arrival organization of the flight schedule
4370 --  Parameters :
4371 --  Create_Arrival_visit Parameters
4372 --      p_oper_param_rec         IN oper_param_rec_type Required
4373 --      p_flight_schedule_rec    IN flight_schedule_rec_type Required
4374 --
4375 --  Standard OUT Parameters :
4376 --      x_return_status           OUT     VARCHAR2     Required
4377 --      x_msg_count               OUT     NUMBER       Required
4378 --      x_msg_data                OUT     VARCHAR2     Required
4379 ----------------------------------------------------------------------------------------
4380 
4381 PROCEDURE Create_Arrival_visit(
4382     p_oper_param_rec       IN  oper_param_rec_type,
4383     p_flight_schedule_rec  IN  flight_schedule_rec_type,
4384     p_present_time         IN      DATE, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4385     p_is_creation_success  OUT NOCOPY VARCHAR2, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4386     x_return_status      OUT NOCOPY  VARCHAR2,
4387     x_msg_count          OUT NOCOPY NUMBER,
4388     x_msg_data           OUT NOCOPY VARCHAR2
4389 )
4390 IS
4391 
4392 -- Cursor to find visit type duration
4393 Cursor get_visit_type_duration(c_visit_type_code IN VARCHAR2, c_mc_id IN NUMBER)
4394 IS
4395 SELECT estimated_duration
4396 FROM ahl_visit_types_b
4397 WHERE visit_type_code = c_visit_type_code
4398  AND mc_id = c_mc_id
4399  AND status_code = 'COMPLETE';
4400 
4401 Cursor is_org_in_current_OU (c_org_id IN NUMBER)
4402 IS
4403 SELECT 'X'
4404 FROM org_organization_definitions hou
4405 WHERE hou.organization_id                                  = c_org_id
4406 AND NVL(hou.operating_unit,mo_global.get_current_org_id()) = mo_global.get_current_org_id();
4407 
4408 -- Cursor to get the visit Number from visit_id
4409 CURSOR get_visit_number(c_visit_id IN NUMBER) IS
4410 SELECT visit_number
4411 FROM ahl_visits_b
4412 WHERE visit_id = c_visit_id;
4413 
4414 --Added cursor to get flight details for loggin :: PRAKKUM :: 01/08/2012
4415 Cursor get_flight_details (c_us_id IN NUMBER)
4416 IS
4417 SELECT flight_number
4418 FROM ahl_unit_schedules
4419 WHERE unit_schedule_id  = c_us_id;
4420 flt_dets_rec get_flight_details%RowType;
4421 
4422   L_API_NAME             CONSTANT VARCHAR2(30) := 'Create_Arrival_visit';
4423   L_DEBUG_KEY            CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
4424   l_visit_type_duration  NUMBER;
4425   l_visit_rec            AHL_VWP_VISITS_PVT.Visit_Rec_Type;
4426   l_is_org_in_curr_OU    VARCHAR2(1);
4427   l_visit_number         NUMBER;
4428   l_start_date_ts        TIMESTAMP;
4429   l_end_date_ts          TIMESTAMP;
4430   l_start_date           DATE;
4431   l_start_hour           NUMBER;
4432   l_start_min            NUMBER;
4433   l_end_date             DATE;
4434   l_end_hour             NUMBER;
4435   l_end_min              NUMBER;
4436   l_return_status        VARCHAR2(1);
4437   -- SATRAJEN :: Bug 14336467 :: Prevent erroring out in case of Operational visit.
4438   l_pub_msg              VARCHAR2(2000);
4439 
4440 BEGIN
4441 
4442  SAVEPOINT Create_Arr_visit_pvt;
4443  IF (l_log_procedure >= l_log_current_level) THEN
4444       fnd_log.string(l_log_procedure,
4445                      L_DEBUG_KEY ||'.begin',
4446                      'At the start of PL SQL function.');
4447  END IF;
4448 
4449  -- initialize procedure return status to success
4450  x_return_status := FND_API.G_RET_STS_SUCCESS;
4451 
4452  -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4453  p_is_creation_success := 'Y'; -- Initialize to success
4454 
4455  OPEN get_visit_type_duration (p_oper_param_rec.visit_type_code,p_oper_param_rec.mc_id);
4456  FETCH get_visit_type_duration INTO l_visit_type_duration;
4457  CLOSE get_visit_type_duration;
4458 
4459  l_visit_rec := null;
4460 
4461  SELECT NAME INTO l_visit_rec.UNIT_NAME FROM AHL_UNIT_CONFIG_HEADERS
4462   WHERE unit_config_header_id= p_flight_schedule_rec.unit_config_header_id;
4463 
4464  -- Populate all the visit attributes
4465  l_visit_rec.VISIT_TYPE_CODE       := p_oper_param_rec.visit_type_code;
4466  l_visit_rec.ORGANIZATION_ID       := p_flight_schedule_rec.arrival_org_id;
4467  l_visit_rec.VISIT_NAME            := 'Auto-Visit';
4468  l_visit_rec.DEPARTMENT_ID         := NVL(p_oper_param_rec.alternate_dep_id, p_flight_schedule_rec.arrival_dept_id);
4469 
4470  l_start_date := p_flight_schedule_rec.est_arrival_time;
4471  l_start_date_ts := cast(l_start_date as timestamp);
4472  l_start_hour := (extract(HOUR from l_start_date_ts));
4473  l_start_min := (extract(MINUTE from l_start_date_ts));
4474 
4475  --l_end_date := p_flight_schedule_rec.est_arrival_time + l_visit_type_duration/24;
4476  l_end_date := l_start_date + l_visit_type_duration/24;
4477  l_end_date_ts := cast(l_end_date as timestamp);
4478  l_end_hour := (extract(HOUR from l_end_date_ts));
4479  l_end_min := (extract(MINUTE from l_end_date_ts));
4480 
4481  l_visit_rec.START_DATE            := trunc(l_start_date);
4482  l_visit_rec.START_HOUR            := l_start_hour;
4483  l_visit_rec.START_MIN             := l_start_min;
4484  l_visit_rec.PLAN_END_DATE         := trunc(l_end_date);
4485  l_visit_rec.PLAN_END_HOUR         := l_end_hour;
4486  l_visit_rec.PLAN_END_MIN          := l_end_min;
4487  l_visit_rec.VISIT_CREATE_TYPE     := 'PLANNING';
4488  l_visit_rec.UNIT_SCHEDULE_ID      := p_flight_schedule_rec.unit_schedule_id;
4489  l_visit_rec.AUTO_VISIT_TYPE_FLAG  := 'A';--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
4490 
4491  -- Check if the visit org is in the current OU
4492  l_is_org_in_curr_OU := null;
4493  OPEN is_org_in_current_OU (l_visit_rec.ORGANIZATION_ID);
4494  FETCH is_org_in_current_OU INTO l_is_org_in_curr_OU;
4495  CLOSE is_org_in_current_OU;
4496 
4497  IF (l_log_statement >= l_log_current_level) THEN
4498     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_start_date --@>'||cast(l_start_date as timestamp));
4499     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_end_date --@>'||cast(l_end_date as timestamp));
4500     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'p_present_time --@>'||cast(p_present_time as timestamp));
4501     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'mo_global.get_current_org_id() --@>'||mo_global.get_current_org_id());
4502     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_visit_rec.ORGANIZATION_ID --@>'||l_visit_rec.ORGANIZATION_ID);
4503     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_is_org_in_curr_OU --@>'||l_is_org_in_curr_OU);
4504  END IF;
4505 
4506   -- Bug 14336486 :: PRAKKUM :: 13/07/2012
4507  IF l_start_date < p_present_time THEN -- If arrival time is in past, do not create a visit
4508 
4509     OPEN get_flight_details(p_flight_schedule_rec.unit_schedule_id);
4510     FETCH get_flight_details INTO flt_dets_rec;
4511     CLOSE get_flight_details;
4512 
4513     -- Do not create a visit
4514     fnd_file.put_line(fnd_file.log, 'Validation Failure: No arrival visit created, since visit start date for flight '||flt_dets_rec.flight_number||' is deriving to past.');
4515     p_is_creation_success := 'N'; -- Flag as creation failed due to validations
4516 
4517  ELSE
4518    -- IF the visit org is NOT in current OU, do not create the visit
4519    IF l_is_org_in_curr_OU = 'X' THEN
4520      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4521               fnd_log.string
4522              ( fnd_log.level_procedure,
4523                'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
4524                'Before calling AHL_VWP_VISITS_PVT.Create_Visit autovst_oper_id: '||p_oper_param_rec.autovst_oper_id);
4525      END IF;
4526      AHL_VWP_VISITS_PVT.Create_Visit (
4527                                          p_api_version => 1.0,
4528                                          p_module_type => 'API',
4529                                          p_x_visit_rec  => l_visit_rec,
4530                                          x_return_status => l_return_status,
4531                                          x_msg_count => x_msg_count,
4532                                          x_msg_data => x_msg_data
4533                                          );
4534 
4535      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4536                fnd_log.string
4537                ( fnd_log.level_procedure,
4538                  'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
4539                  'After calling AHL_VWP_VISITS_PVT.Create_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
4540      END IF;
4541      -- SATRAJEN :: Bug 14336467 :: Prevent Erroring out when the called from Operational Visit Procedure. :: START
4542      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4543            l_pub_msg := FND_MSG_PUB.Get_Detail (p_msg_index => FND_MSG_PUB.G_LAST);
4544            IF (l_pub_msg like '%AHL_VWP_PRD_MR_ASSOC_FAIL%') THEN
4545              IF (l_log_statement >= l_log_current_level) THEN
4546                fnd_log.string
4547                ( l_log_statement,
4548                  L_DEBUG_KEY,
4549                  'visit id= '||l_visit_rec.visit_id || 'Had problems with the MRs associated. So arrival visit creation is not successful.');
4550              END IF;
4551              x_return_status := FND_API.G_RET_STS_SUCCESS;
4552              p_is_creation_success := 'N'; -- Flag as creation of visit failed due to validations
4553              -- Remove message from FND
4554              FND_MSG_PUB.Delete_Msg(p_msg_index => FND_MSG_PUB.count_msg);
4555 
4556              OPEN get_flight_details(p_flight_schedule_rec.unit_schedule_id);
4557              FETCH get_flight_details INTO flt_dets_rec;
4558              CLOSE get_flight_details;
4559 
4560              fnd_file.put_line(fnd_file.log, 'Validation Failure: Had problems with the MRs associated. So arrival visit creation for flight '||flt_dets_rec.flight_number||' is not successful.');
4561            ELSE
4562              x_return_status := l_return_status ;
4563              x_msg_count := FND_MSG_PUB.count_msg;
4564              IF (l_log_statement >= l_log_current_level) THEN
4565                   fnd_log.string(l_log_statement,
4566                                  L_DEBUG_KEY,
4567                                  'Errors from Create_Visit. Message count: ' || x_msg_count);
4568              END IF;
4569              IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4570                 RAISE FND_API.G_EXC_ERROR;
4571              ELSE
4572                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4573              END IF;
4574            END IF;
4575      ELSE
4576            x_return_status := l_return_status ;
4577            l_visit_number := NULL;
4578            OPEN get_visit_number(l_visit_rec.visit_id);
4579            FETCH get_visit_number INTO l_visit_number;
4580            CLOSE get_visit_number;
4581            fnd_file.put_line(fnd_file.log, 'Created arrival visit number -> '||l_visit_number);
4582          -- End of logging
4583      END IF;
4584      -- SATRAJEN :: Bug 14336467 :: Prevent Erroring out when the called from Operational Visit Procedure. :: END
4585    END IF; -- IF l_is_org_in_curr_OU = 'X'
4586  END IF; -- l_start_date < p_present_time
4587 
4588  IF (l_log_procedure >= l_log_current_level) THEN
4589       fnd_log.string(l_log_procedure,
4590                      L_DEBUG_KEY ||'.end',
4591                      'At the end of PL SQL function.');
4592  END IF;
4593 EXCEPTION
4594  WHEN FND_API.G_EXC_ERROR THEN
4595    x_return_status := FND_API.G_RET_STS_ERROR;
4596    ROLLBACK TO Create_Arr_visit_pvt;
4597    FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4598                               p_data  => x_msg_data,
4599                               p_encoded => fnd_api.g_false);
4600 
4601  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4602    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4603    ROLLBACK TO Create_Arr_visit_pvt;
4604    FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4605                               p_data  => x_msg_data,
4606                               p_encoded => fnd_api.g_false);
4607 
4608  WHEN OTHERS THEN
4609     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4610     ROLLBACK TO Create_Arr_visit_pvt;
4611     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4612        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
4613                                p_procedure_name => 'Create_Arrival_visit',
4614                                p_error_text     => SUBSTR(SQLERRM,1,500));
4615     END IF;
4616     FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4617                                p_data  => x_msg_data,
4618                                p_encoded => fnd_api.g_false);
4619 END Create_Arrival_visit;
4620 
4621 ---------------------------------------------------------------------------------------
4622 -- PROCEDURE
4623 --    Create_Downtime_visit
4624 --  Type   : Private
4625 --
4626 -- PURPOSE
4627 --    To create a downtime visit at arrival organization of the flight
4628 --
4629 --  Parameters :
4630 --  Create_Downtime_visit Parameters
4631 --      p_oper_param_rec         IN oper_param_rec_type Required
4632 --      p_flight_schedule_rec    IN flight_schedule_rec_type Required
4633 --
4634 --  Standard OUT Parameters :
4635 --      x_return_status           OUT     VARCHAR2     Required
4636 --      x_msg_count               OUT     NUMBER       Required
4637 --      x_msg_data                OUT     VARCHAR2     Required
4638 --
4639 ----------------------------------------------------------------------------------------
4640 
4641 PROCEDURE Create_Downtime_visit(
4642     p_oper_param_rec       IN      oper_param_rec_type,
4643     p_flight_schedule_rec  IN      flight_schedule_rec_type,
4644     p_present_time         IN      DATE, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4645     p_is_creation_success  OUT NOCOPY VARCHAR2, -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4646     x_return_status        OUT NOCOPY     VARCHAR2,
4647     x_msg_count            OUT NOCOPY NUMBER,
4648     x_msg_data             OUT NOCOPY VARCHAR2)
4649 IS
4650 
4651 --cursor that checks if there is any succeeding flight for this flight schedule
4652 Cursor get_succeeding_us_det (c_fs_id IN NUMBER)
4653  IS
4654  SELECT UNIT_SCHEDULE_ID, EST_DEPARTURE_TIME FROM AHL_UNIT_SCHEDULES
4655  WHERE  preceding_us_id = c_fs_id;
4656 
4657 Cursor is_org_in_current_OU (c_org_id IN NUMBER)
4658 IS
4659 SELECT 'X'
4660 FROM org_organization_definitions hou
4661 WHERE hou.organization_id                                  = c_org_id
4662 AND NVL(hou.operating_unit,mo_global.get_current_org_id()) = mo_global.get_current_org_id();
4663 
4664 -- Cursor to get the visit Number from visit_id
4665 CURSOR get_visit_number(c_visit_id IN NUMBER) IS
4666 SELECT visit_number
4667 FROM ahl_visits_b
4668 WHERE visit_id = c_visit_id;
4669 
4670 -- Bug 14336467 :: PRAKKUM :: 12/07/2012
4671 -- Cursor to get visit type duration
4672 CURSOR get_visit_type_dur(c_vst_type_code IN VARCHAR2, c_mc_id IN NUMBER) IS
4673 select estimated_duration from ahl_visit_types_vl
4674 where
4675 visit_type_code = c_vst_type_code
4676 and mc_id = c_mc_id
4677 and status_code = 'COMPLETE';
4678 
4679 --Added cursor to get flight details for loggin :: PRAKKUM :: 01/08/2012
4680 Cursor get_flight_details (c_us_id IN NUMBER)
4681 IS
4682 SELECT flight_number
4683 FROM ahl_unit_schedules
4684 WHERE unit_schedule_id  = c_us_id;
4685 flt_dets_rec get_flight_details%RowType;
4686 
4687   L_API_NAME             CONSTANT VARCHAR2(30) := 'Create_Downtime_visit';
4688   L_DEBUG_KEY            CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
4689   l_visit_rec            AHL_VWP_VISITS_PVT.Visit_Rec_Type;
4690   l_is_org_in_curr_OU    VARCHAR2(1);
4691   l_visit_number         NUMBER;
4692   l_start_date_ts        TIMESTAMP;
4693   l_end_date_ts          TIMESTAMP;
4694   l_start_date           DATE;
4695   l_start_hour           NUMBER;
4696   l_start_min            NUMBER;
4697   l_end_date             DATE;
4698   l_end_hour             NUMBER;
4699   l_end_min              NUMBER;
4700   l_return_status        VARCHAR2(1);
4701   l_succeeding_us_id     NUMBER;
4702   l_succ_dep_time        DATE;
4703   l_vst_type_est_dur     NUMBER;  -- Bug 14336486 :: PRAKKUM :: 12/07/2012
4704   -- SATRAJEN :: Bug 14336467 :: Prevent erroring out in case of Operational visit.
4705   l_pub_msg              VARCHAR2(2000);
4706 
4707 BEGIN
4708 
4709  SAVEPOINT Create_Downtime_visit_pvt;
4710 
4711  IF (l_log_procedure >= l_log_current_level) THEN
4712       fnd_log.string(l_log_procedure,
4713                      L_DEBUG_KEY ||'.begin',
4714                      'At the start of PL SQL function.');
4715  END IF;
4716 
4717  -- initialize procedure return status to success
4718  x_return_status := FND_API.G_RET_STS_SUCCESS;
4719 
4720  -- Bug 14336467 :: PRAKKUM :: 13/07/2012
4721  p_is_creation_success := 'Y'; -- Initialize to success
4722 
4723  l_visit_rec := null;
4724  SELECT NAME INTO l_visit_rec.UNIT_NAME FROM AHL_UNIT_CONFIG_HEADERS
4725   WHERE unit_config_header_id= p_flight_schedule_rec.unit_config_header_id;
4726 
4727   OPEN get_succeeding_us_det (p_flight_schedule_rec.unit_schedule_id);
4728   FETCH get_succeeding_us_det INTO l_succeeding_us_id, l_succ_dep_time;
4729   CLOSE get_succeeding_us_det;
4730 
4731  -- Populate all the visit attributes
4732  l_visit_rec.VISIT_TYPE_CODE       := p_oper_param_rec.visit_type_code;
4733  l_visit_rec.ORGANIZATION_ID       := p_flight_schedule_rec.arrival_org_id;
4734  l_visit_rec.VISIT_NAME            := 'Auto-Visit';
4735  l_visit_rec.DEPARTMENT_ID         := NVL(p_oper_param_rec.alternate_dep_id, p_flight_schedule_rec.arrival_dept_id);
4736 
4737  l_start_date := p_flight_schedule_rec.est_arrival_time;
4738  l_start_date_ts := cast(l_start_date as timestamp);
4739  l_start_hour := (extract(HOUR from l_start_date_ts));
4740  l_start_min := (extract(MINUTE from l_start_date_ts));
4741 
4742  l_end_date := l_succ_dep_time;
4743  l_end_date_ts := cast(l_end_date as timestamp);
4744  l_end_hour := (extract(HOUR from l_end_date_ts));
4745  l_end_min := (extract(MINUTE from l_end_date_ts));
4746 
4747  -- Bug 14336486 :: PRAKKUM :: 12/07/2012 :: START
4748 
4749  OPEN  get_visit_type_dur(p_oper_param_rec.visit_type_code, p_oper_param_rec.mc_id);
4750  FETCH get_visit_type_dur INTO l_vst_type_est_dur;
4751  CLOSE get_visit_type_dur;
4752 
4753  IF (l_log_statement >= l_log_current_level) THEN
4754     fnd_log.string(l_log_statement, L_DEBUG_KEY, 'p_oper_param_rec.visit_type_code--@>' || p_oper_param_rec.visit_type_code);
4755     fnd_log.string(l_log_statement, L_DEBUG_KEY, 'p_oper_param_rec.mc_id--@>' ||  p_oper_param_rec.mc_id);
4756     fnd_log.string(l_log_statement, L_DEBUG_KEY, 'l_vst_type_est_dur--@>' ||  l_vst_type_est_dur);
4757  END IF;
4758 
4759  OPEN get_flight_details(p_flight_schedule_rec.unit_schedule_id);
4760  FETCH get_flight_details INTO flt_dets_rec;
4761  CLOSE get_flight_details;
4762 
4763  IF (l_start_date < p_present_time ) THEN
4764    fnd_file.put_line(fnd_file.log, 'Validation Failure: No downtime visit created, since visit start date for flight '||flt_dets_rec.flight_number||' is deriving to past');
4765    p_is_creation_success := 'N'; -- Flag as creation of visit failed due to validations
4766  END IF;
4767 
4768  -- SATRAJEN :: BUG 14475778 :: Checking the condition only if p_is_creation_success := Y from previous contidition :: 13-AUG-2012
4769  -- SATRAJEN :: BUG 14481051 :: Calculation prob in l_vst_type_est_dur :: 13-AUG-2012
4770  --IF (l_end_date - l_vst_type_est_dur) < l_start_date THEN
4771  IF ((p_is_creation_success<>'N') AND ((l_end_date - (l_vst_type_est_dur/24)) < l_start_date)) THEN
4772    fnd_file.put_line(fnd_file.log, 'Validation Failure: No downtime visit created, since duration of visit for flight '||flt_dets_rec.flight_number||' is lesser than visit type duration');
4773    p_is_creation_success := 'N'; -- Flag as creation of visit failed due to validations
4774  END IF;
4775 
4776  -- Bug 14336486 :: PRAKKUM :: 12/07/2012 :: END
4777 
4778  l_visit_rec.START_DATE            := trunc(l_start_date);
4779  l_visit_rec.START_HOUR            := l_start_hour;
4780  l_visit_rec.START_MIN             := l_start_min;
4781  l_visit_rec.PLAN_END_DATE         := trunc(l_end_date);
4782  l_visit_rec.PLAN_END_HOUR         := l_end_hour;
4783  l_visit_rec.PLAN_END_MIN          := l_end_min;
4784  l_visit_rec.VISIT_CREATE_TYPE     := 'PLANNING';
4785  l_visit_rec.UNIT_SCHEDULE_ID      := p_flight_schedule_rec.unit_schedule_id;
4786  l_visit_rec.AUTO_VISIT_TYPE_FLAG  := 'T';--TCHIMIRA :: 14-Jun-2012 :: ER 14015560
4787 
4788  -- Check if the visit org is in the current OU
4789  l_is_org_in_curr_OU := null;
4790  OPEN is_org_in_current_OU (l_visit_rec.ORGANIZATION_ID);
4791  FETCH is_org_in_current_OU INTO l_is_org_in_curr_OU;
4792  CLOSE is_org_in_current_OU;
4793 
4794  IF (l_log_statement >= l_log_current_level) THEN
4795     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_start_date --@>'||cast(l_start_date as timestamp));
4796     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_end_date --@>'||cast(l_end_date as timestamp));
4797     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'p_present_time --@>'||cast(p_present_time as timestamp));
4798     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'mo_global.get_current_org_id() --@>'||mo_global.get_current_org_id());
4799     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_visit_rec.ORGANIZATION_ID --@>'||l_visit_rec.ORGANIZATION_ID);
4800     fnd_log.string(l_log_statement, L_DEBUG_KEY , 'l_is_org_in_curr_OU --@>'||l_is_org_in_curr_OU);
4801  END IF;
4802 
4803 
4804  -- Bug 14336486 :: PRAKKUM :: 12/07/2012
4805  IF p_is_creation_success<>'N' THEN --Don't create a downtime visit, if visit duration is going to be lesser than visit type duration
4806    -- IF the visit org is NOT in current OU, do not create the visit
4807    IF l_is_org_in_curr_OU = 'X' THEN
4808      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4809               fnd_log.string
4810              ( fnd_log.level_procedure,
4811                'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
4812                'Before calling AHL_VWP_VISITS_PVT.Create_Visit autovst_oper_id: '||p_oper_param_rec.autovst_oper_id);
4813      END IF;
4814      AHL_VWP_VISITS_PVT.Create_Visit (
4815                                          p_api_version => 1.0,
4816                                          p_module_type => 'API',
4817                                          p_x_visit_rec  => l_visit_rec,
4818                                          x_return_status => l_return_status,
4819                                          x_msg_count => x_msg_count,
4820                                          x_msg_data => x_msg_data
4821                                          );
4822 
4823      IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4824                fnd_log.string
4825                ( fnd_log.level_procedure,
4826                  'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
4827                  'After calling AHL_VWP_VISITS_PVT.Create_Visit, l_return_status= '||l_return_status||', visit id= '||l_visit_rec.visit_id);
4828      END IF;
4829 
4830      -- SATRAJEN :: Bug 14336467 :: Prevent Erroring out when the called from Operational Visit Procedure. :: START
4831      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4832          l_pub_msg := FND_MSG_PUB.Get_Detail (p_msg_index => FND_MSG_PUB.G_LAST);
4833          IF (l_pub_msg like '%AHL_VWP_PRD_MR_ASSOC_FAIL%') THEN
4834            IF (l_log_statement >= l_log_current_level) THEN
4835              fnd_log.string
4836              ( l_log_statement,
4837                L_DEBUG_KEY,
4838                'visit id= '||l_visit_rec.visit_id || 'Had problems with the MRs associated. So downtime visit creation is not successful. ');
4839            END IF;
4840            x_return_status := FND_API.G_RET_STS_SUCCESS;
4841            p_is_creation_success := 'N'; -- Flag as creation of visit failed due to validations
4842            -- Remove message from FND
4843            FND_MSG_PUB.Delete_Msg(p_msg_index => FND_MSG_PUB.count_msg);
4844            fnd_file.put_line(fnd_file.log, 'Validation Failure: Had problems with the MRs associated. So downtime visit creation for flight '||flt_dets_rec.flight_number||' is not successful.');
4845          ELSE
4846            x_return_status := l_return_status ;
4847            x_msg_count := FND_MSG_PUB.count_msg;
4848            IF (l_log_statement >= l_log_current_level) THEN
4849                   fnd_log.string(l_log_statement,
4850                                  L_DEBUG_KEY,
4851                                  'Errors from Create_Visit. Message count: ' || x_msg_count);
4852            END IF;
4853            IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4854              RAISE FND_API.G_EXC_ERROR;
4855            ELSE
4856              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4857            END IF;
4858          END IF;
4859       ELSE
4860            x_return_status := l_return_status ;
4861            l_visit_number := NULL;
4862            OPEN get_visit_number(l_visit_rec.visit_id);
4863            FETCH get_visit_number INTO l_visit_number;
4864            CLOSE get_visit_number;
4865            fnd_file.put_line(fnd_file.log, 'Created downtime visit number -> '||l_visit_number);
4866          -- End of logging
4867      END IF;
4868      -- SATRAJEN :: Bug 14336467 :: Prevent Erroring out when the called from Operational Visit Procedure. :: END
4869    END IF; -- IF l_is_org_in_curr_OU = 'X'
4870  END IF; -- (l_end_date - l_vst_type_est_dur) > l_start_date
4871 
4872  IF (l_log_procedure >= l_log_current_level) THEN
4873       fnd_log.string(l_log_procedure,
4874                      L_DEBUG_KEY ||'.end',
4875                      'At the end of PL SQL function.');
4876  END IF;
4877 EXCEPTION
4878  WHEN FND_API.G_EXC_ERROR THEN
4879    x_return_status := FND_API.G_RET_STS_ERROR;
4880    ROLLBACK TO Create_Downtime_visit_pvt;
4881    FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4882                               p_data  => x_msg_data,
4883                               p_encoded => fnd_api.g_false);
4884 
4885  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4886    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4887    ROLLBACK TO Create_Downtime_visit_pvt;
4888    FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4889                               p_data  => x_msg_data,
4890                               p_encoded => fnd_api.g_false);
4891 
4892  WHEN OTHERS THEN
4893     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4894     ROLLBACK TO Create_Downtime_visit_pvt;
4895     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4896        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
4897                                p_procedure_name => 'Create_Downtime_visit',
4898                                p_error_text     => SUBSTR(SQLERRM,1,500));
4899     END IF;
4900     FND_MSG_PUB.count_and_get(p_count => x_msg_count,
4901                                p_data  => x_msg_data,
4902                                p_encoded => fnd_api.g_false);
4903 END Create_Downtime_visit;
4904 
4905 ---------------------------------------------------------------------------------------
4906 -- PROCEDURE
4907 --    Delete_Oper_Visit
4908 -- Type             : Private
4909 -- PURPOSE
4910 --    To delete the passed operational visit
4911 ----------------------------------------------------------------------------------------
4912 
4913 PROCEDURE Delete_Oper_Visit(
4914     p_visit_id       IN   NUMBER,
4915     x_return_status  OUT NOCOPY  VARCHAR2)
4916 IS
4917 
4918 --Cursor to get planned UEs
4919 -- SATRAJEN :: Bug 14464977 :: Changed for retrieving the Non Routines also :: Aug 2012
4920 /*Cursor get_visit_planned_ues(c_visit_id IN NUMBER)
4921 IS
4922 SELECT distinct unit_effectivity_id
4923 FROM AHL_VISIT_TASKS_B
4924 WHERE VISIT_ID = c_visit_id
4925  AND TASK_TYPE_CODE = 'PLANNED';*/
4926 Cursor get_visit_planned_ues(c_visit_id IN NUMBER)
4927 IS
4928 SELECT unit_effectivity_id
4929 FROM AHL_VISIT_TASKS_B
4930 WHERE VISIT_ID = c_visit_id
4931 AND TASK_TYPE_CODE = 'PLANNED' AND SERVICE_REQUEST_ID IS NULL
4932 UNION
4933 SELECT unit_effectivity_id
4934 FROM AHL_VISIT_TASKS_B
4935 WHERE VISIT_ID = c_visit_id
4936 AND TASK_TYPE_CODE = 'SUMMARY' AND originating_task_id IS NULL AND SERVICE_REQUEST_ID IS NOT NULL;
4937 
4938 
4939 L_API_NAME             CONSTANT VARCHAR2(30) := 'Delete_Oper_Visit';
4940 L_DEBUG_KEY            CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
4941 l_ue_ids               VARCHAR2(4000);
4942 l_return_status        VARCHAR2(1);
4943 l_msg_count                  NUMBER;
4944 l_msg_data                   VARCHAR2(2000);
4945 x_item_key             VARCHAR2(100);
4946 
4947 BEGIN
4948 
4949  IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4950      fnd_log.string
4951      ( fnd_log.level_procedure,
4952              L_DEBUG_KEY||'.begin',
4953              'Before calling AHL_VWP_VISITS_PVT.Delete_Visit, visit id: '||p_visit_id);
4954  END IF;
4955 
4956  -- PRAKKUM :: Bug 14334961 :: 18/07/2012 :: Get planned ues before deleting the visit
4957  --Get all the planned ue ids for this visit so as to pass it to notification
4958  l_ue_ids := null;
4959  FOR l_planned_ue_id_rec IN get_visit_planned_ues(p_visit_id) LOOP
4960   IF l_ue_ids IS NULL THEN
4961      l_ue_ids := to_char(l_planned_ue_id_rec.unit_effectivity_id);
4962   ELSE
4963      l_ue_ids := l_ue_ids || ',' || to_char(l_planned_ue_id_rec.unit_effectivity_id);
4964   END IF;
4965  END LOOP;
4966 
4967  AHL_VWP_VISITS_PVT.Delete_Visit (
4968                         p_api_version => 1.0,
4969                         p_visit_id => p_visit_id,
4970                         x_return_status => l_return_status,
4971                         x_msg_count => l_msg_count,
4972                         x_msg_data => l_msg_data
4973                         );
4974 
4975  IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level) THEN
4976      fnd_log.string
4977           ( fnd_log.level_procedure,
4978            'ahl.plsql.'||g_pkg_name||'.'||l_api_name||':',
4979            'After calling AHL_VWP_VISITS_PVT.Delete_Visit, l_return_status= '||l_return_status);
4980  END IF;
4981  x_return_status := l_return_status ;
4982  IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4983     l_msg_count := FND_MSG_PUB.count_msg;
4984     IF (l_log_statement >= l_log_current_level) THEN
4985         fnd_log.string(l_log_statement,
4986                        L_DEBUG_KEY,
4987                        'Errors from Delete_Visit. Message count: ' || l_msg_count);
4988     END IF;
4989     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4990       RAISE FND_API.G_EXC_ERROR;
4991     ELSE
4992       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4993     END IF;
4994  END IF;  -- Return Status is not Success
4995 
4996  -- Also send a visit cancel notification
4997  -- if no planned UEs associated to the visit, then do not send notification
4998  IF l_ue_ids is not null then
4999     AHL_AVF_OPER_VSTS_PVT.Launch_Visit_Can_Notification (
5000                  p_visit_id        => p_visit_id,
5001                  p_ue_ids          => l_ue_ids,
5002                  p_commit          => Fnd_Api.g_false ,
5003                  x_item_key        => x_item_key,
5004                  x_return_status   => l_return_status );
5005     IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5006         l_msg_count := FND_MSG_PUB.count_msg;
5007         IF (l_log_statement >= l_log_current_level) THEN
5008            fnd_log.string(l_log_statement,
5009                           L_DEBUG_KEY,
5010                           'Errors from Launch_Visit_Can_Notification ' );
5011         END IF;
5012         IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5013            RAISE FND_API.G_EXC_ERROR;
5014         ELSE
5015            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5016         END IF;
5017     END IF;  -- Return Status is not Success
5018  END IF; --IF l_ue_ids is not null then
5019 
5020  IF (l_log_procedure >= l_log_current_level) THEN
5021       fnd_log.string(l_log_procedure,
5022                      L_DEBUG_KEY ||'.end',
5023                      'At the end of PL SQL function.');
5024  END IF;
5025 
5026 END Delete_Oper_Visit;
5027 --TCHIMIRA :: 14-Jun-2012 :: end
5028 
5029 --------------------------------------------------------------------
5030 -- PROCEDURE
5031 --    Process_Operational_visits
5032 --
5033 -- PURPOSE
5034 --    Made as an executable for the P2P CP
5035 --  Process_Operational_visits Parameters :
5036 --      errbuf              OUT   VARCHAR2   Required
5037 --         Defines in pl/sql to store procedure to get error messages into log file
5038 --      retcode             OUT   NUMBER     Required
5039 --         To get the status of the concurrent program
5040 
5041 --------------------------------------------------------------------
5042 PROCEDURE Process_Operational_visits(
5043     errbuf            OUT NOCOPY VARCHAR2,
5044     retcode           OUT NOCOPY NUMBER,
5045     p_api_version     IN  NUMBER,
5046     p_oper_flag       IN  VARCHAR2
5047 )
5048 IS
5049 
5050 
5051 -- Local variables section
5052 l_msg_count             NUMBER;
5053 l_msg_data              VARCHAR2(2000);
5054 l_return_status         VARCHAR2(1);
5055 l_api_version           NUMBER := 1.0;
5056 l_api_name              VARCHAR2(30) := 'Process_Operational_visits';
5057 l_err_msg               VARCHAR2(2000);
5058 l_msg_index_out         NUMBER;
5059 
5060 BEGIN
5061 
5062    -- Standard start of API savepoint
5063    SAVEPOINT Process_Operational_visits;
5064 
5065    -- 1. Initialize error message stack by default
5066    FND_MSG_PUB.Initialize;
5067 
5068    -- Standard call to check for call compatibility
5069    IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME) THEN
5070       retcode := 2;
5071       errbuf := FND_MSG_PUB.Get;
5072       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5073    END IF;
5074 
5075    -- 2. Dump all input parameters
5076    fnd_file.put_line(fnd_file.log, '*************API input parameters**************');
5077    fnd_file.put_line(fnd_file.log, 'p_oper_flag -> '|| p_oper_flag);
5078    fnd_file.put_line(fnd_file.log, 'fnd_global.USER_ID -> '|| fnd_global.USER_ID);
5079    fnd_file.put_line(fnd_file.log, 'fnd_global.RESP_ID -> '||fnd_global.RESP_ID);
5080    fnd_file.put_line(fnd_file.log, 'fnd_global.PROG_APPL_ID -> '|| fnd_global.PROG_APPL_ID);
5081    fnd_file.put_line(fnd_file.log, 'mo_global.get_current_org_id -> '|| mo_global.get_current_org_id());
5082 
5083 
5084    IF p_oper_flag = 'C' THEN
5085 
5086       fnd_file.put_line(fnd_file.log, 'before calling Create_Oper_Visit');
5087 
5088       Create_Oper_Visit (
5089                           p_api_version => 1.0,
5090                           p_init_msg_list => FND_API.G_FALSE,
5091                           p_commit => FND_API.G_FALSE,
5092                           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
5093                           x_return_status => l_return_status,
5094                           x_msg_count => l_msg_count,
5095                           x_msg_data => l_msg_data
5096                          );
5097 
5098        l_msg_count := FND_MSG_PUB.Count_Msg;
5099        IF (l_msg_count > 0) THEN
5100           fnd_file.put_line(fnd_file.log, 'Following error occured during the call to Create_Oper_Visit..');
5101           IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
5102               RAISE FND_API.G_EXC_ERROR;
5103           ELSE
5104               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5105           END IF;
5106        ELSE
5107           COMMIT WORK;
5108        END IF;
5109 
5110 
5111    ELSIF p_oper_flag = 'U' THEN
5112 
5113       fnd_file.put_line(fnd_file.log, 'before calling Update_Oper_Visit');
5114 
5115       Update_Oper_Visit (
5116                          p_api_version => 1.0,
5117                          p_init_msg_list => FND_API.G_FALSE,
5118                          p_commit => FND_API.G_FALSE,
5119                          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
5120                          x_return_status => l_return_status,
5121                          x_msg_count => l_msg_count,
5122                          x_msg_data => l_msg_data
5123                         );
5124 
5125        l_msg_count := FND_MSG_PUB.Count_Msg;
5126        IF (l_msg_count > 0) THEN
5127           fnd_file.put_line(fnd_file.log, 'Following error occured during the call to Update_Oper_Visit..');
5128           IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
5129               RAISE FND_API.G_EXC_ERROR;
5130           ELSE
5131               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5132           END IF;
5133        ELSE
5134           COMMIT WORK;
5135        END IF;
5136 
5137    END IF;
5138 
5139 EXCEPTION
5140  WHEN FND_API.G_EXC_ERROR THEN
5141    ROLLBACK TO Process_Operational_visits;
5142    retcode := 2;
5143    FOR i IN 1..l_msg_count
5144        LOOP
5145          fnd_msg_pub.get( p_msg_index => i,
5146                           p_encoded   => FND_API.G_FALSE,
5147                           p_data      => l_err_msg,
5148                           p_msg_index_out => l_msg_index_out);
5149 
5150          fnd_file.put_line(FND_FILE.LOG, 'Err message-'||l_msg_index_out||':' || l_err_msg);
5151        END LOOP;
5152 
5153 
5154  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5155    ROLLBACK TO Process_Operational_visits;
5156    retcode := 2;
5157    l_msg_count := Fnd_Msg_Pub.count_msg;
5158    FOR i IN 1..l_msg_count
5159        LOOP
5160          fnd_msg_pub.get( p_msg_index => i,
5161                           p_encoded   => FND_API.G_FALSE,
5162                           p_data      => l_err_msg,
5163                           p_msg_index_out => l_msg_index_out);
5164 
5165          fnd_file.put_line(FND_FILE.LOG, 'Err message-'||l_msg_index_out||':' || l_err_msg);
5166        END LOOP;
5167 
5168 
5169  WHEN OTHERS THEN
5170    ROLLBACK TO Process_Operational_visits;
5171    retcode := 2;
5172    IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5173      fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
5174                              p_procedure_name => 'Process_Operational_visits',
5175                              p_error_text     => SUBSTR(SQLERRM,1,500));
5176    END IF;
5177    l_msg_count := Fnd_Msg_Pub.count_msg;
5178    FOR i IN 1..l_msg_count
5179      LOOP
5180         fnd_msg_pub.get( p_msg_index => i,
5181                          p_encoded   => FND_API.G_FALSE,
5182                          p_data      => l_err_msg,
5183                          p_msg_index_out => l_msg_index_out);
5184 
5185         fnd_file.put_line(FND_FILE.LOG, 'Err message-'||l_msg_index_out||':' || l_err_msg);
5186      END LOOP;
5187 
5188 
5189 END Process_Operational_visits;
5190 
5191 ------------------------------------------------------------------------------------
5192 -- Start of Comments
5193 --  Procedure name    : Launch_Visit_Can_Notification
5194 --  Type              : Public
5195 --  Function          : Launches a Workflow notification when a Visit is cancelled
5196 --  Pre-reqs          :
5197 --  Parameters        :
5198 --
5199 --  Launch_Visit_Can_Notification Parameters:
5200 --       p_visit_id               IN     Visit Id                                       Required
5201 --       p_ue_ids                 IN     Unit Effectivity Ids concatenated by using ',' Required
5202 --       p_commit                 IN     Commit flag. Workflow will be launched only    Required
5203 --                                       after a commit.
5204 --       x_item_key               OUT    Item key of the launched notification          Required
5205 --       x_return_status          OUT    Return status. Item key to be used only if     Required
5206 --                                       this status is FND_API.G_RET_STS_SUCCESS.
5207 --
5208 --  End of Comments
5209 
5210 PROCEDURE Launch_Visit_Can_Notification (
5211     p_visit_id                    IN              NUMBER,
5212     p_ue_ids                      IN              VARCHAR2,
5213     p_commit                      IN              VARCHAR2  := FND_API.G_FALSE,
5214     x_item_key                    OUT     NOCOPY  VARCHAR2,
5215     x_return_status               OUT     NOCOPY  VARCHAR2
5216 ) IS
5217 
5218 --
5219 l_api_name     CONSTANT VARCHAR2(30)  := 'Launch_Visit_Can_Notification';
5220 l_full_name    CONSTANT VARCHAR2(100) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
5221 
5222 l_msg_count             NUMBER;
5223 l_msg_data              VARCHAR2(4000);
5224 l_active_flag           VARCHAR2(1);
5225 l_process_name          VARCHAR2(30);
5226 l_item_type             VARCHAR2(8);
5227 l_subject               FND_NEW_MESSAGES.message_text%TYPE;
5228 --
5229 
5230 BEGIN
5231     IF (l_log_procedure >= l_log_current_level) THEN
5232         FND_LOG.string(l_log_procedure, l_full_name || '.begin', 'At the start of the API');
5233     END IF;
5234 
5235     -- initialize procedure return status to success
5236     x_return_status := FND_API.G_RET_STS_SUCCESS;
5237 
5238     IF (l_log_statement >= l_log_current_level) THEN
5239         FND_LOG.string(l_log_statement, l_full_name, 'the arguments: '||
5240                        '  Visit ID > '||p_visit_id||
5241                        '  UE IDs > '||p_ue_ids||
5242                        ', p_commit > '||p_commit);
5243     END IF;
5244 
5245     -- check for the Visit Id
5246     IF (p_visit_id IS NULL) THEN
5247         x_return_status := FND_API.G_RET_STS_ERROR;
5248         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_VWP_INVALID_VST'); -- Visit id is invalid.
5249         FND_MESSAGE.Set_Token('VISIT_ID', p_visit_id);
5250         FND_MSG_PUB.ADD;
5251         RETURN;
5252     END IF;
5253 
5254     -- check for the Unit Effectivity ID
5255     IF (p_ue_ids IS NULL) THEN
5256         x_return_status := FND_API.G_RET_STS_ERROR;
5257         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_UMP_UE_ID_INVALID'); -- Unit Effectivity Id (UEID) is invalid
5258         FND_MESSAGE.Set_Token('UEID', p_ue_ids);
5259         FND_MSG_PUB.ADD;
5260         RETURN;
5261     END IF;
5262 
5263     -- get the details of the Workflow process mapped to the object G_WF_CANC_OBJ
5264     AHL_UTILITY_PVT.Get_WF_Process_Name(
5265         p_object        => G_WF_CANC_OBJ,
5266         x_active        => l_active_flag,
5267         x_process_name  => l_process_name ,
5268         x_item_type     => l_item_type,
5269         x_msg_count     => l_msg_count,
5270         x_msg_data      => l_msg_data,
5271         x_return_status => x_return_status);
5272 
5273     IF (l_log_statement >= l_log_current_level) THEN
5274         FND_LOG.string(l_log_statement, l_full_name, 'the returned values from AHL_UTILITY_PVT.Get_WF_Process_Name : '||
5275                        '  l_active_flag > '||l_active_flag||
5276                        ', l_process_name > '||l_process_name||
5277                        ', l_item_type > '||l_item_type||
5278                        ', x_return_status > '||x_return_status);
5279     END IF;
5280 
5281     -- if returned with error, don't proceed any further
5282     IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5283         RETURN;
5284     END IF;
5285 
5286     -- if the mapping is active, call the notification API
5287     If (l_active_flag = 'Y') THEN
5288         -- get the subject text
5289         l_subject := FND_MESSAGE.GET_STRING(G_APP_NAME, G_VISIT_CANC_SBJ);
5290 
5291         IF (l_log_statement >= l_log_current_level) THEN
5292             FND_LOG.string(l_log_statement, l_full_name,
5293                            'before calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification with arguments: '||
5294                            '  p_object > '||G_WF_CANC_OBJ||
5295                            ', p_process_name > '||l_process_name||
5296                            ', p_item_type > '||l_item_type||
5297                            ', p_subject > '||l_subject||
5298                            ', p_oa_function > '||G_VISIT_CANCEL_FN||
5299                            ', p_param1_name > '||G_VISIT_ID_FN_PARAM1||
5300                            ', p_param1_value > '||p_visit_id||
5301                            ', p_param2_name > '||G_UE_IDS_FN_PARAM2||
5302                            ', p_param2_value > '||p_ue_ids);
5303         END IF;
5304 
5305         -- call AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification
5306         AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification(
5307             p_object        => G_WF_CANC_OBJ,
5308             p_process_name  => l_process_name,
5309             p_item_type     => l_item_type,
5310             p_subject       => l_subject,
5311             p_oa_function   => G_VISIT_CANCEL_FN,
5312             p_param1_name   => G_VISIT_ID_FN_PARAM1,
5313             p_param2_name   => G_UE_IDS_FN_PARAM2,
5314             p_param1_value  => TO_CHAR(p_visit_id),
5315             p_param2_value  => p_ue_ids,
5316             p_param3_name   => G_PAGE_FUNC_PARAM3,
5317             p_param3_value  => G_VISIT_CANCEL_FN,
5318             x_item_key      => x_item_key,
5319             x_return_status => x_return_status);
5320 
5321         IF (l_log_statement >= l_log_current_level) THEN
5322             FND_LOG.string(l_log_statement, l_full_name,
5323                            'after calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification, '||
5324                            '  x_item_key > '||x_item_key||
5325                            ', x_return_status > '||x_return_status);
5326         END IF;
5327 
5328         -- if returned with error, don't proceed any further
5329         IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5330             RETURN;
5331         END IF;
5332     END IF;
5333 
5334     -- Standard check of p_commit
5335     IF FND_API.TO_BOOLEAN(p_commit) THEN
5336         COMMIT WORK;
5337     END IF;
5338 
5339     IF (l_log_procedure >= l_log_current_level) THEN
5340         FND_LOG.string(l_log_procedure, l_full_name || '.end', 'At the end of the API');
5341     END IF;
5342 
5343 END Launch_Visit_Can_Notification;
5344 ------------------------------------------------------------------------------------
5345 
5346 ------------------------------------------------------------------------------------
5347 -- Start of Comments
5348 --  Procedure name    : Launch_Visit_SCE_Notification
5349 --  Type              : Public
5350 --  Function          : Launches a Workflow notification when a Visit UE's Service Category is greater
5351 --                      than the visit's organization and department service category
5352 --  Pre-reqs          :
5353 --  Parameters        :
5354 --
5355 --  Launch_Visit_SCE_Notification Parameters:
5356 --       p_visit_id               IN     Visit Id                                       Required
5357 --       p_ue_ids                 IN     Unit Effectivity Ids concatenated by using ',' Required
5358 --       p_commit                 IN     Commit flag. Workflow will be launched only    Required
5359 --                                       after a commit.
5360 --       x_item_key               OUT    Item key of the launched notification          Required
5361 --       x_return_status          OUT    Return status. Item key to be used only if     Required
5362 --                                       this status is FND_API.G_RET_STS_SUCCESS.
5363 --
5364 --  End of Comments
5365 
5366 PROCEDURE Launch_Visit_SCE_Notification (
5367     p_visit_id                    IN              NUMBER,
5368     p_ue_ids                      IN              VARCHAR2,
5369     p_commit                      IN              VARCHAR2  := FND_API.G_FALSE,
5370     x_item_key                    OUT     NOCOPY  VARCHAR2,
5371     x_return_status               OUT     NOCOPY  VARCHAR2
5372 ) IS
5373 
5374 --
5375 l_api_name     CONSTANT VARCHAR2(30)  := 'Launch_Visit_SCE_Notification';
5376 l_full_name    CONSTANT VARCHAR2(100) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
5377 
5378 l_msg_count             NUMBER;
5379 l_msg_data              VARCHAR2(4000);
5380 l_active_flag           VARCHAR2(1);
5381 l_process_name          VARCHAR2(30);
5382 l_item_type             VARCHAR2(8);
5383 l_subject               FND_NEW_MESSAGES.message_text%TYPE;
5384 --
5385 
5386 BEGIN
5387     IF (l_log_procedure >= l_log_current_level) THEN
5388         FND_LOG.string(l_log_procedure, l_full_name || '.begin', 'At the start of the API');
5389     END IF;
5390 
5391     -- initialize procedure return status to success
5392     x_return_status := FND_API.G_RET_STS_SUCCESS;
5393 
5394     IF (l_log_statement >= l_log_current_level) THEN
5395         FND_LOG.string(l_log_statement, l_full_name, 'the arguments: '||
5396                        '  Visit ID > '||p_visit_id||
5397                        '  UE IDs > '||p_ue_ids||
5398                        ', p_commit > '||p_commit);
5399     END IF;
5400 
5401     -- check for the Visit Id
5402     IF (p_visit_id IS NULL) THEN
5403         x_return_status := FND_API.G_RET_STS_ERROR;
5404         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_VWP_INVALID_VST'); -- Visit id is invalid.
5405         FND_MESSAGE.Set_Token('VISIT_ID', p_visit_id);
5406         FND_MSG_PUB.ADD;
5407         RETURN;
5408     END IF;
5409 
5410     -- check for the Unit Effectivity ID
5411     IF (p_ue_ids IS NULL) THEN
5412         x_return_status := FND_API.G_RET_STS_ERROR;
5413         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_UMP_UE_ID_INVALID'); -- Unit Effectivity Id (UEID) is invalid
5414         FND_MESSAGE.Set_Token('UEID', p_ue_ids);
5415         FND_MSG_PUB.ADD;
5416         RETURN;
5417     END IF;
5418 
5419     -- get the details of the Workflow process mapped to the object G_WF_VSCE_OBJ
5420     AHL_UTILITY_PVT.Get_WF_Process_Name(
5421         p_object        => G_WF_VSCE_OBJ,
5422         x_active        => l_active_flag,
5423         x_process_name  => l_process_name ,
5424         x_item_type     => l_item_type,
5425         x_msg_count     => l_msg_count,
5426         x_msg_data      => l_msg_data,
5427         x_return_status => x_return_status);
5428 
5429     IF (l_log_statement >= l_log_current_level) THEN
5430         FND_LOG.string(l_log_statement, l_full_name, 'the returned values from AHL_UTILITY_PVT.Get_WF_Process_Name : '||
5431                        '  l_active_flag > '||l_active_flag||
5432                        ', l_process_name > '||l_process_name||
5433                        ', l_item_type > '||l_item_type||
5434                        ', x_return_status > '||x_return_status);
5435     END IF;
5436 
5437     -- if returned with error, don't proceed any further
5438     IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5439         RETURN;
5440     END IF;
5441 
5442     -- if the mapping is active, call the notification API
5443     If (l_active_flag = 'Y') THEN
5444         -- get the subject text
5445         l_subject := FND_MESSAGE.GET_STRING(G_APP_NAME, G_VISIT_USCE_SBJ);
5446 
5447         IF (l_log_statement >= l_log_current_level) THEN
5448             FND_LOG.string(l_log_statement, l_full_name,
5449                            'before calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification with arguments: '||
5450                            '  p_object > '||G_WF_VSCE_OBJ||
5451                            ', p_process_name > '||l_process_name||
5452                            ', p_item_type > '||l_item_type||
5453                            ', p_subject > '||l_subject||
5454                            ', p_oa_function > '||G_VISIT_USCE_FN||
5455                            ', p_param1_name > '||G_VISIT_ID_FN_PARAM1||
5456                            ', p_param1_value > '||p_visit_id||
5457                            ', p_param2_name > '||G_UE_IDS_FN_PARAM2||
5458                            ', p_param2_value > '||p_ue_ids);
5459         END IF;
5460 
5461         -- call AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification
5462         AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification(
5463             p_object        => G_WF_VSCE_OBJ,
5464             p_process_name  => l_process_name,
5465             p_item_type     => l_item_type,
5466             p_subject       => l_subject,
5467             p_oa_function   => G_VISIT_USCE_FN,
5468             p_param1_name   => G_VISIT_ID_FN_PARAM1,
5469             p_param2_name   => G_UE_IDS_FN_PARAM2,
5470             p_param1_value  => TO_CHAR(p_visit_id),
5471             p_param2_value  => p_ue_ids,
5472             p_param3_name   => G_PAGE_FUNC_PARAM3,
5473             p_param3_value  => G_VISIT_USCE_FN,
5474             x_item_key      => x_item_key,
5475             x_return_status => x_return_status);
5476 
5477         IF (l_log_statement >= l_log_current_level) THEN
5478             FND_LOG.string(l_log_statement, l_full_name,
5479                            'after calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification, '||
5480                            '  x_item_key > '||x_item_key||
5481                            ', x_return_status > '||x_return_status);
5482         END IF;
5483 
5484         -- if returned with error, don't proceed any further
5485         IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5486             RETURN;
5487         END IF;
5488     END IF;
5489 
5490     -- Standard check of p_commit
5491     IF FND_API.TO_BOOLEAN(p_commit) THEN
5492         COMMIT WORK;
5493     END IF;
5494 
5495     IF (l_log_procedure >= l_log_current_level) THEN
5496         FND_LOG.string(l_log_procedure, l_full_name || '.end', 'At the end of the API');
5497     END IF;
5498 
5499 END Launch_Visit_SCE_Notification;
5500 ------------------------------------------------------------------------------------
5501 
5502 ------------------------------------------------------------------------------------
5503 -- Start of Comments
5504 --  Procedure name    : Launch_Visit_Disc_Notification
5505 --  Type              : Public
5506 --  Function          : Launches a Workflow notification when a visit is disconnected from the flight
5507 --  Pre-reqs          :
5508 --  Parameters        :
5509 --
5510 --  Launch_Visit_Disc_Notification Parameters:
5511 --       p_unit_schedule_id       IN     Unit Schedule Id                                Required
5512 --       p_visit_ids              IN     Visit Ids affected by the flight               Required
5513 --       p_commit                 IN     Commit flag. Workflow will be launched only    Required
5514 --                                       after a commit.
5515 --       x_item_key               OUT    Item key of the launched notification          Required
5516 --       x_return_status          OUT    Return status. Item key to be used only if     Required
5517 --                                       this status is FND_API.G_RET_STS_SUCCESS.
5518 --
5519 --  End of Comments
5520 
5521 PROCEDURE Launch_Visit_Disc_Notification (
5522     p_unit_schedule_id            IN              NUMBER,
5523     p_visit_ids                   IN              VARCHAR2,
5524     p_commit                      IN              VARCHAR2  := FND_API.G_FALSE,
5525     x_item_key                    OUT     NOCOPY  VARCHAR2,
5526     x_return_status               OUT     NOCOPY  VARCHAR2
5527 ) IS
5528 
5529 --
5530 l_api_name     CONSTANT VARCHAR2(30)  := 'Launch_Visit_Disc_Notification';
5531 l_full_name    CONSTANT VARCHAR2(100) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
5532 
5533 l_msg_count             NUMBER;
5534 l_msg_data              VARCHAR2(4000);
5535 l_active_flag           VARCHAR2(1);
5536 l_process_name          VARCHAR2(30);
5537 l_item_type             VARCHAR2(8);
5538 l_subject               FND_NEW_MESSAGES.message_text%TYPE;
5539 --
5540 
5541 BEGIN
5542     IF (l_log_procedure >= l_log_current_level) THEN
5543         FND_LOG.string(l_log_procedure, l_full_name || '.begin', 'At the start of the API');
5544     END IF;
5545 
5546     -- initialize procedure return status to success
5547     x_return_status := FND_API.G_RET_STS_SUCCESS;
5548 
5549     IF (l_log_statement >= l_log_current_level) THEN
5550         FND_LOG.string(l_log_statement, l_full_name, 'the arguments: '||
5551                        'Unit Schedule Id > '||p_unit_schedule_id||
5552                        '  Visit IDs > '||p_visit_ids||
5553                        ', p_commit > '||p_commit);
5554     END IF;
5555 
5556     -- check for the Visit Id
5557     IF (p_visit_ids IS NULL) THEN
5558         x_return_status := FND_API.G_RET_STS_ERROR;
5559         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_VWP_INVALID_VST'); -- Visit id is invalid.
5560         FND_MESSAGE.Set_Token('VISIT_ID', p_visit_ids);
5561         FND_MSG_PUB.ADD;
5562         RETURN;
5563     END IF;
5564 
5565     -- check for the Flight Schedule
5566     IF (p_unit_schedule_id IS NULL) THEN
5567         x_return_status := FND_API.G_RET_STS_ERROR;
5568         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_UA_US_NOT_FOUND'); -- Flight Schedule (RECORD) does not exist.
5569         FND_MESSAGE.Set_Token('RECORD', p_unit_schedule_id);
5570         FND_MSG_PUB.ADD;
5571         RETURN;
5572     END IF;
5573 
5574     -- get the details of the Workflow process mapped to the object G_WF_DISC_OBJ
5575     AHL_UTILITY_PVT.Get_WF_Process_Name(
5576         p_object        => G_WF_DISC_OBJ,
5577         x_active        => l_active_flag,
5578         x_process_name  => l_process_name ,
5579         x_item_type     => l_item_type,
5580         x_msg_count     => l_msg_count,
5581         x_msg_data      => l_msg_data,
5582         x_return_status => x_return_status);
5583 
5584     IF (l_log_statement >= l_log_current_level) THEN
5585         FND_LOG.string(l_log_statement, l_full_name, 'the returned values from AHL_UTILITY_PVT.Get_WF_Process_Name : '||
5586                        '  l_active_flag > '||l_active_flag||
5587                        ', l_process_name > '||l_process_name||
5588                        ', l_item_type > '||l_item_type||
5589                        ', x_return_status > '||x_return_status);
5590     END IF;
5591 
5592     -- if returned with error, don't proceed any further
5593     IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5594         RETURN;
5595     END IF;
5596 
5597     -- if the mapping is active, call the notification API
5598     If (l_active_flag = 'Y') THEN
5599         -- get the subject text
5600         l_subject := FND_MESSAGE.GET_STRING(G_APP_NAME, G_VISIT_DISC_SBJ);
5601 
5602         IF (l_log_statement >= l_log_current_level) THEN
5603             FND_LOG.string(l_log_statement, l_full_name,
5604                            'before calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification with arguments: '||
5605                            '  p_object > '||G_WF_DISC_OBJ||
5606                            ', p_process_name > '||l_process_name||
5607                            ', p_item_type > '||l_item_type||
5608                            ', p_subject > '||l_subject||
5609                            ', p_oa_function > '||G_VISIT_DISC_FN||
5610                            ', p_param1_name > '||G_VISIT_ID_FN_PARAM1||
5611                            ', p_param1_value > '||p_visit_ids||
5612                            ', p_param2_name > '||G_UNIT_SCH_ID_FN_PARAM2||
5613                            ', p_param2_value > '||p_unit_schedule_id);
5614         END IF;
5615 
5616         -- call AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification
5617         AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification(
5618             p_object        => G_WF_DISC_OBJ,
5619             p_process_name  => l_process_name,
5620             p_item_type     => l_item_type,
5621             p_subject       => l_subject,
5622             p_oa_function   => G_VISIT_DISC_FN,
5623             p_param1_name   => G_VISIT_ID_FN_PARAM1,
5624             p_param1_value  => p_visit_ids,
5625             p_param2_name   => G_UNIT_SCH_ID_FN_PARAM2,
5626             p_param2_value  => TO_CHAR(p_unit_schedule_id),
5627             p_param3_name   => G_PAGE_FUNC_PARAM3,
5628             p_param3_value  => G_VISIT_DISC_FN,
5629             x_item_key      => x_item_key,
5630             x_return_status => x_return_status);
5631 
5632         IF (l_log_statement >= l_log_current_level) THEN
5633             FND_LOG.string(l_log_statement, l_full_name,
5634                            'after calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification, '||
5635                            '  x_item_key > '||x_item_key||
5636                            ', x_return_status > '||x_return_status);
5637         END IF;
5638 
5639         -- if returned with error, don't proceed any further
5640         IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5641             RETURN;
5642         END IF;
5643     END IF;
5644 
5645     -- Standard check of p_commit
5646     IF FND_API.TO_BOOLEAN(p_commit) THEN
5647         COMMIT WORK;
5648     END IF;
5649 
5650     IF (l_log_procedure >= l_log_current_level) THEN
5651         FND_LOG.string(l_log_procedure, l_full_name || '.end', 'At the end of the API');
5652     END IF;
5653 
5654 END Launch_Visit_Disc_Notification;
5655 ------------------------------------------------------------------------------------
5656 
5657 ------------------------------------------------------------------------------------
5658 -- Start of Comments
5659 --  Procedure name    : Launch_Visit_Dur_Notification
5660 --  Type              : Public
5661 --  Function          : Launches a Workflow notification when a visit is disconnected from the flight
5662 --  Pre-reqs          :
5663 --  Parameters        :
5664 --
5665 --  Launch_Visit_Dur_Notification Parameters:
5666 --       p_unit_schedule_id       IN     Unit Schedule Id                                Required
5667 --       p_visit_ids              IN     Visit Ids affected by the flight               Required
5668 --       p_commit                 IN     Commit flag. Workflow will be launched only    Required
5669 --                                       after a commit.
5670 --       x_item_key               OUT    Item key of the launched notification          Required
5671 --       x_return_status          OUT    Return status. Item key to be used only if     Required
5672 --                                       this status is FND_API.G_RET_STS_SUCCESS.
5673 --
5674 --  End of Comments
5675 
5676 PROCEDURE Launch_Visit_Dur_Notification (
5677     p_unit_schedule_id            IN              NUMBER,
5678     p_visit_ids                   IN              VARCHAR2,
5679     p_commit                      IN              VARCHAR2  := FND_API.G_FALSE,
5680     x_item_key                    OUT     NOCOPY  VARCHAR2,
5681     x_return_status               OUT     NOCOPY  VARCHAR2
5682 ) IS
5683 
5684 --
5685 l_api_name     CONSTANT VARCHAR2(30)  := 'Launch_Visit_Dur_Notification';
5686 l_full_name    CONSTANT VARCHAR2(100) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
5687 
5688 l_msg_count             NUMBER;
5689 l_msg_data              VARCHAR2(4000);
5690 l_active_flag           VARCHAR2(1);
5691 l_process_name          VARCHAR2(30);
5692 l_item_type             VARCHAR2(8);
5693 l_subject               FND_NEW_MESSAGES.message_text%TYPE;
5694 --
5695 
5696 BEGIN
5697     IF (l_log_procedure >= l_log_current_level) THEN
5698         FND_LOG.string(l_log_procedure, l_full_name || '.begin', 'At the start of the API');
5699     END IF;
5700 
5701     -- initialize procedure return status to success
5702     x_return_status := FND_API.G_RET_STS_SUCCESS;
5703 
5704     IF (l_log_statement >= l_log_current_level) THEN
5705         FND_LOG.string(l_log_statement, l_full_name, 'the arguments: '||
5706                        'Unit Schedule Id > '||p_unit_schedule_id||
5707                        '  Visit ID > '||p_visit_ids||
5708                        ', p_commit > '||p_commit);
5709     END IF;
5710 
5711     -- check for the Visit Id
5712     IF (p_visit_ids IS NULL) THEN
5713         x_return_status := FND_API.G_RET_STS_ERROR;
5714         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_VWP_INVALID_VST'); -- Visit id is invalid.
5715         FND_MESSAGE.Set_Token('VISIT_ID', p_visit_ids);
5716         FND_MSG_PUB.ADD;
5717         RETURN;
5718     END IF;
5719 
5720     -- check for the Flight Schedule
5721     IF (p_unit_schedule_id IS NULL) THEN
5722         x_return_status := FND_API.G_RET_STS_ERROR;
5723         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_UA_US_NOT_FOUND'); -- Flight Schedule (RECORD) does not exist.
5724         FND_MESSAGE.Set_Token('RECORD', p_unit_schedule_id);
5725         FND_MSG_PUB.ADD;
5726         RETURN;
5727     END IF;
5728 
5729     -- get the details of the Workflow process mapped to the object G_WF_VDUR_OBJ
5730     AHL_UTILITY_PVT.Get_WF_Process_Name(
5731         p_object        => G_WF_VDUR_OBJ,
5732         x_active        => l_active_flag,
5733         x_process_name  => l_process_name ,
5734         x_item_type     => l_item_type,
5735         x_msg_count     => l_msg_count,
5736         x_msg_data      => l_msg_data,
5737         x_return_status => x_return_status);
5738 
5739     IF (l_log_statement >= l_log_current_level) THEN
5740         FND_LOG.string(l_log_statement, l_full_name, 'the returned values from AHL_UTILITY_PVT.Get_WF_Process_Name : '||
5741                        '  l_active_flag > '||l_active_flag||
5742                        ', l_process_name > '||l_process_name||
5743                        ', l_item_type > '||l_item_type||
5744                        ', x_return_status > '||x_return_status);
5745     END IF;
5746 
5747     -- if returned with error, don't proceed any further
5748     IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5749         RETURN;
5750     END IF;
5751 
5752     -- if the mapping is active, call the notification API
5753     If (l_active_flag = 'Y') THEN
5754         -- get the subject text
5755         l_subject := FND_MESSAGE.GET_STRING(G_APP_NAME, G_VISIT_DURA_SBJ);
5756 
5757         IF (l_log_statement >= l_log_current_level) THEN
5758             FND_LOG.string(l_log_statement, l_full_name,
5759                            'before calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification with arguments: '||
5760                            '  p_object > '||G_WF_VDUR_OBJ||
5761                            ', p_process_name > '||l_process_name||
5762                            ', p_item_type > '||l_item_type||
5763                            ', p_subject > '||l_subject||
5764                            ', p_oa_function > '||G_VISIT_DURA_FN||
5765                            ', p_param1_name > '||G_VISIT_ID_FN_PARAM1||
5766                            ', p_param1_value > '||p_visit_ids||
5767                            ', p_param2_name > '||G_UNIT_SCH_ID_FN_PARAM2||
5768                            ', p_param2_value > '||p_unit_schedule_id);
5769         END IF;
5770 
5771         -- call AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification
5772         AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification(
5773             p_object        => G_WF_VDUR_OBJ,
5774             p_process_name  => l_process_name,
5775             p_item_type     => l_item_type,
5776             p_subject       => l_subject,
5777             p_oa_function   => G_VISIT_DURA_FN,
5778             p_param1_name   => G_VISIT_ID_FN_PARAM1,
5779             p_param1_value  => p_visit_ids,
5780             p_param2_name   => G_UNIT_SCH_ID_FN_PARAM2,
5781             p_param2_value  => TO_CHAR(p_unit_schedule_id),
5782             p_param3_name   => G_PAGE_FUNC_PARAM3,
5783             p_param3_value  => G_VISIT_DURA_FN,
5784             x_item_key      => x_item_key,
5785             x_return_status => x_return_status);
5786 
5787         IF (l_log_statement >= l_log_current_level) THEN
5788             FND_LOG.string(l_log_statement, l_full_name,
5789                            'after calling AHL_WF_NOTIFICATION_PVT.Launch_OA_Notification, '||
5790                            '  x_item_key > '||x_item_key||
5791                            ', x_return_status > '||x_return_status);
5792         END IF;
5793 
5794         -- if returned with error, don't proceed any further
5795         IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5796             RETURN;
5797         END IF;
5798     END IF;
5799 
5800     -- Standard check of p_commit
5801     IF FND_API.TO_BOOLEAN(p_commit) THEN
5802         COMMIT WORK;
5803     END IF;
5804 
5805     IF (l_log_procedure >= l_log_current_level) THEN
5806         FND_LOG.string(l_log_procedure, l_full_name || '.end', 'At the end of the API');
5807     END IF;
5808 
5809 END Launch_Visit_Dur_Notification;
5810 ------------------------------------------------------------------------------------
5811 
5812 -- PRAKKUM :: Bug 13844759 :: 26/07/2012 :: START
5813 ------------------------------------------------------------------------------------
5814 -- Start of Comments
5815 --  Procedure name    : Load_Can_Cancel_Visit
5816 --  Type              : Public
5817 --  Function          : Procedure to get whether visit can be cancelled or not
5818 --  Pre-reqs          :
5819 --  Parameters        :
5820 --
5821 --  Launch_Visit_Dur_Notification Parameters:
5822 --       p_visit_id               IN    Visit Id                                Optional
5823 --       p_visit_id2              IN    Visit Id                                Optional
5824 --       l_can_cancel_visit       OUT   Flag denotes whether Visit with p_visit_id can be cancelled
5825 --       l_can_cancel_visit2      OUT   Flag denotes whether Visit with p_visit_id2 can be cancelled
5826 --       x_return_status          OUT   Return status. Item key to be used only if     Required
5827 --                                      this status is FND_API.G_RET_STS_SUCCESS.
5828 --
5829 --  End of Comments
5830 
5831 PROCEDURE Load_Can_Cancel_Visit (
5832     p_visit_id               IN                     NUMBER,
5833     p_visit_id2              IN                     NUMBER,
5834     x_can_cancel_visit       OUT NOCOPY             VARCHAR2,
5835     x_can_cancel_visit2      OUT NOCOPY             VARCHAR2,
5836     x_return_status          OUT NOCOPY             VARCHAR2,
5837     x_msg_count              OUT NOCOPY             NUMBER,
5838     x_msg_data               OUT NOCOPY             VARCHAR2
5839 ) IS
5840 
5841 --
5842 l_api_name     CONSTANT VARCHAR2(30)  := 'Load_Can_Cancel_Visit';
5843 l_full_name    CONSTANT VARCHAR2(100) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
5844 
5845 --Cursor to find if the visit is in planning status and not firmed and not locked
5846 Cursor can_cancel_visit(c_visit_id IN NUMBER)
5847 IS
5848 SELECT 'X'
5849 FROM AHL_VISITS_B
5850 WHERE VISIT_ID = c_visit_id
5851  AND STATUS_CODE = 'PLANNING'
5852  AND NVL(FIRMED_FLAG,'N') <> 'Y'
5853  AND NVL(LOCKED_FLAG,'N') <> 'Y';
5854 
5855 BEGIN
5856     IF (l_log_procedure >= l_log_current_level) THEN
5857         FND_LOG.string(l_log_procedure, l_full_name || '.begin', 'At the start of the API');
5858     END IF;
5859 
5860     -- Standard start of API savepoint
5861     SAVEPOINT Load_Can_Cancel_Visit;
5862 
5863     -- initialize procedure return status to success
5864     x_return_status := FND_API.G_RET_STS_SUCCESS;
5865 
5866     IF (l_log_statement >= l_log_current_level) THEN
5867         FND_LOG.string(l_log_statement, l_full_name, 'the arguments: '||
5868                        'p_visit_id --@> '||p_visit_id||
5869                        'p_visit_id2 --@> '||p_visit_id2);
5870     END IF;
5871 
5872     x_can_cancel_visit := null;
5873     x_can_cancel_visit2 := null;
5874     IF p_visit_id is not null THEN
5875       OPEN can_cancel_visit(p_visit_id);
5876       FETCH can_cancel_visit INTO x_can_cancel_visit;
5877       CLOSE can_cancel_visit;
5878     ELSE
5879       x_can_cancel_visit := 'X';
5880     END IF;
5881     IF p_visit_id2 is not null THEN
5882       OPEN can_cancel_visit(p_visit_id2);
5883       FETCH can_cancel_visit INTO x_can_cancel_visit2;
5884       CLOSE can_cancel_visit;
5885     ELSE
5886       x_can_cancel_visit2 := 'X';
5887     END IF;
5888 
5889     IF (l_log_statement >= l_log_current_level) THEN
5890         FND_LOG.string(l_log_statement, l_full_name, 'p_visit_id ** x_can_cancel_visit: '||p_visit_id||' ** '||x_can_cancel_visit);
5891         FND_LOG.string(l_log_statement, l_full_name, 'p_visit_id2 ** x_can_cancel_visit2: '||p_visit_id2||' ** '||x_can_cancel_visit2);
5892     END IF;
5893 
5894     IF (l_log_procedure >= l_log_current_level) THEN
5895         FND_LOG.string(l_log_procedure, l_full_name || '.end', 'At the end of the API');
5896     END IF;
5897 
5898 EXCEPTION
5899 
5900  WHEN FND_API.G_EXC_ERROR THEN
5901 
5902    x_return_status := FND_API.G_RET_STS_ERROR;
5903    ROLLBACK TO Load_Can_Cancel_Visit;
5904    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
5905                               p_data  => x_msg_data,
5906                               p_encoded => fnd_api.g_false);
5907 
5908  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5909    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5910    ROLLBACK TO Load_Can_Cancel_Visit;
5911    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
5912                               p_data  => x_msg_data,
5913                               p_encoded => fnd_api.g_false);
5914 
5915  WHEN OTHERS THEN
5916     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5917     ROLLBACK TO Load_Can_Cancel_Visit;
5918 
5919     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5920        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
5921                                p_procedure_name => 'Load_Can_Cancel_Visit',
5922                                p_error_text     => SUBSTR(SQLERRM,1,500));
5923     END IF;
5924 
5925     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
5926                                p_data  => x_msg_data,
5927                                p_encoded => fnd_api.g_false);
5928 
5929 END Load_Can_Cancel_Visit;
5930 -- PRAKKUM :: Bug 13844759 :: 26/07/2012 :: END
5931 
5932 ------------------------------------------------------------------------------------
5933 -- Start of Comments
5934 --  Procedure name    : Disconnect_Flight_Visit
5935 --  Type              : Public
5936 --  Function          : Procedure to disconnect visit and to launch disconnect notification
5937 --  Pre-reqs          :
5938 --  Parameters        :
5939 --
5940 --  Launch_Visit_Dur_Notification Parameters:
5941 --       p_visit_id               IN    Visit Id                                Required
5942 --       p_unit_schedule_id       IN    Unit Schedule Id                        Required
5943 --       x_return_status          OUT   Return status. Item key to be used only if     Required
5944 --                                      this status is FND_API.G_RET_STS_SUCCESS.
5945 --
5946 --  End of Comments
5947 
5948 PROCEDURE Disconnect_Flight_Visit (
5949     p_visit_id               IN                     NUMBER,
5950     p_unit_schedule_id       IN                     NUMBER,
5951     x_return_status          OUT NOCOPY             VARCHAR2,
5952     x_msg_count              OUT NOCOPY             NUMBER,
5953     x_msg_data               OUT NOCOPY             VARCHAR2
5954 ) IS
5955 
5956 --
5957 l_api_name     CONSTANT VARCHAR2(30)  := 'Disconnect_Flight_Visit';
5958 l_full_name    CONSTANT VARCHAR2(100) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
5959 x_item_key              VARCHAR2(100);
5960 l_return_status               VARCHAR2(1);
5961 
5962 --Cursor to get visit details
5963 CURSOR get_visit_details(c_visit_id IN NUMBER) IS
5964 SELECT visit_number, auto_visit_type_flag auto_flag
5965 FROM ahl_visits_b
5966 WHERE visit_id = c_visit_id;
5967 vst_details_rec get_visit_details%ROWTYPE;
5968 
5969 
5970 BEGIN
5971 
5972     IF (l_log_procedure >= l_log_current_level) THEN
5973         FND_LOG.string(l_log_procedure, l_full_name || '.begin', 'At the start of the API');
5974     END IF;
5975 
5976     -- Standard start of API savepoint
5977     SAVEPOINT Disconnect_Flight_Visit;
5978 
5979     -- initialize procedure return status to success
5980     x_return_status := FND_API.G_RET_STS_SUCCESS;
5981 
5982     IF (l_log_statement >= l_log_current_level) THEN
5983         FND_LOG.string(l_log_statement, l_full_name, 'the arguments: '||
5984                        'p_visit_id --@> '||p_visit_id||
5985                        'p_unit_schedule_id --@> '||p_unit_schedule_id);
5986     END IF;
5987 
5988     UPDATE ahl_visits_b
5989     SET UNIT_SCHEDULE_ID = null,
5990     OBJECT_VERSION_NUMBER = object_version_number + 1,
5991     LAST_UPDATE_DATE      = SYSDATE,
5992     LAST_UPDATED_BY       = Fnd_Global.USER_ID,
5993     LAST_UPDATE_LOGIN     = Fnd_Global.LOGIN_ID
5994     WHERE visit_id = p_visit_id;
5995 
5996     -- Send a visit disconnect notification
5997     AHL_AVF_OPER_VSTS_PVT.Launch_Visit_Disc_Notification (
5998              p_unit_schedule_id => p_unit_schedule_id,
5999              p_visit_ids        => p_visit_id,
6000              p_commit           => Fnd_Api.g_false ,
6001              x_item_key         => x_item_key,
6002              x_return_status    => l_return_status );
6003     IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6004       x_msg_count := FND_MSG_PUB.count_msg;
6005       IF (l_log_statement >= l_log_current_level) THEN
6006          fnd_log.string(l_log_statement,
6007                         l_full_name,
6008                         'Errors from Launch_Visit_Disc_Notification ' );
6009       END IF;
6010       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6011          RAISE FND_API.G_EXC_ERROR;
6012       ELSE
6013          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6014       END IF;
6015     END IF;  -- Return Status is not Success
6016 
6017     -- For Logging :: START
6018     OPEN get_visit_details(p_visit_id);
6019     FETCH get_visit_details INTO vst_details_rec;
6020     CLOSE get_visit_details;
6021 
6022     IF vst_details_rec.visit_number IS NOT NULL THEN
6023       IF vst_details_rec.auto_flag IS NOT NULL THEN
6024          IF vst_details_rec.auto_flag = 'T' THEN
6025             fnd_file.put_line(fnd_file.log, 'Disconnected downtime visit number -> '||vst_details_rec.visit_number);
6026          ELSIF vst_details_rec.auto_flag = 'D' THEN
6027             fnd_file.put_line(fnd_file.log, 'Disconnected departure visit number -> '||vst_details_rec.visit_number);
6028          ELSIF vst_details_rec.auto_flag = 'A' THEN
6029             fnd_file.put_line(fnd_file.log, 'Disconnected arrival visit number -> '||vst_details_rec.visit_number);
6030          END IF;
6031       ELSE
6032          fnd_file.put_line(fnd_file.log, 'Disconnected visit number -> '||vst_details_rec.visit_number);
6033       END IF;
6034     END IF;
6035     -- For Logging :: END
6036 
6037     IF (l_log_procedure >= l_log_current_level) THEN
6038         FND_LOG.string(l_log_procedure, l_full_name || '.end', 'At the end of the API');
6039     END IF;
6040 
6041 EXCEPTION
6042 
6043  WHEN FND_API.G_EXC_ERROR THEN
6044 
6045    x_return_status := FND_API.G_RET_STS_ERROR;
6046    ROLLBACK TO Disconnect_Flight_Visit;
6047    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
6048                               p_data  => x_msg_data,
6049                               p_encoded => fnd_api.g_false);
6050 
6051  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6052    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6053    ROLLBACK TO Disconnect_Flight_Visit;
6054    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
6055                               p_data  => x_msg_data,
6056                               p_encoded => fnd_api.g_false);
6057 
6058  WHEN OTHERS THEN
6059     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6060     ROLLBACK TO Disconnect_Flight_Visit;
6061 
6062     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6063        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
6064                                p_procedure_name => 'Disconnect_Flight_Visit',
6065                                p_error_text     => SUBSTR(SQLERRM,1,500));
6066     END IF;
6067 
6068     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
6069                                p_data  => x_msg_data,
6070                                p_encoded => fnd_api.g_false);
6071 
6072 END Disconnect_Flight_Visit;
6073 
6074 END  AHL_AVF_OPER_VSTS_PVT;