DBA Data[Home] [Help]

PACKAGE BODY: APPS.WSH_TRIPS_ACTIONS

Source


1 PACKAGE BODY WSH_TRIPS_ACTIONS as
2 /* $Header: WSHTRACB.pls 120.21.12000000.7 2007/03/30 09:34:27 nchellam ship $ */
3 
4 --
5 G_PKG_NAME CONSTANT VARCHAR2(50) := 'WSH_TRIPS_ACTIONS';
6 --
7 
8 g_int_mask              VARCHAR2(12) := 'S00000000000';
9 type numtabvc2 is table of number index by varchar2(2000);
10 TYPE g_num_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
11 TYPE g_v30_tbl_type IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;
12 
13 TYPE Del_Rec_Type IS RECORD (
14      delivery_id  g_num_tbl_type,
15      INITIAL_PICKUP_LOCATION_ID  g_v30_tbl_type,
16      ULTIMATE_DROPOFF_LOCATION_ID  g_v30_tbl_type,
17      MODE_OF_TRANSPORT  g_v30_tbl_type,
18      service_level g_v30_tbl_type,
19      carrier_id g_v30_tbl_type
20 );
21 
22 -- SSN change
23 -- Global Variable to cache the sequencing mode
24   G_STOP_SEQ_MODE NUMBER;
25 
26 -- SSN change
27 -- New API to cache Stop sequence mode base on profile value
28 FUNCTION Get_Stop_Seq_Mode return Number is
29 
30   l_debug_on BOOLEAN;
31   l_module_name CONSTANT VARCHAR2(100):= 'wsh.plsql.' || G_PKG_NAME || '.' || 'GET_STOP_SEQ_MODE';
32 
33   -- OTM R12, glog proj
34   l_gc3_is_installed VARCHAR2(1);
35 
36 BEGIN
37   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
38   --
39   IF l_debug_on IS NULL
40   THEN
41     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
42   END IF;
43   IF l_debug_on THEN
44     WSH_DEBUG_SV.push(l_module_name);
45   END IF;
46 
47   --OTM R12, glog proj, use Global Variable
48   l_gc3_is_installed := WSH_UTIL_CORE.G_GC3_IS_INSTALLED;
49 
50   -- If null, call the function
51   IF l_gc3_is_installed IS NULL THEN
52     l_gc3_is_installed := WSH_UTIL_CORE.GC3_IS_INSTALLED;
53   END IF;
54   -- end of OTM R12, glog proj
55 
56   -- OTM R12, glog project
57   -- IF GC3 is INSTALLED, Mode should be SSN
58   IF G_STOP_SEQ_MODE IS NULL THEN--{
59     IF l_gc3_is_installed = 'Y' THEN
60       G_STOP_SEQ_MODE := WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN;
61     ELSE
62       IF WSH_UTIL_CORE.FTE_IS_INSTALLED = 'Y' THEN
63         G_STOP_SEQ_MODE := WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD;
64       ELSIF fnd_profile.value('WSH_STOP_SEQ_MODE') = ('PAD') THEN
65         G_STOP_SEQ_MODE := WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD;
66       ELSE -- non-existent profile or any other value of fnd_profile.value(' WSH_STOP_SEQ_MODE') implies SSN
67         G_STOP_SEQ_MODE := WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN;
68       END IF;
69     END IF;
70   END IF;--}
71 
72   IF l_debug_on THEN
73     WSH_DEBUG_SV.log(l_module_name,'Internal Value of Sequence Mode',g_stop_seq_mode);
74     WSH_DEBUG_SV.pop(l_module_name);
75   END IF;
76   Return G_STOP_SEQ_MODE;
77 
78 EXCEPTION
79   WHEN OTHERS THEN
80     IF l_debug_on THEN
81       WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
82       WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
83     END IF;
84     -- Bug 4253334, default value is SSN mode
85     RETURN nvl(G_STOP_SEQ_MODE, WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN);
86 END get_stop_seq_mode;
87 
88 PROCEDURE Get_Trip_Defaults(p_trip_id in NUMBER,
89                             p_trip_name in VARCHAR2 DEFAULT NULL,
90                             x_def_rec IN OUT  NOCOPY WSH_TRIPS_GRP.default_parameters_rectype,
91                             x_return_Status OUT NOCOPY varchar2 ) IS
92 
93   cursor get_pickup_stop (c_trip_id number) is
94        select distinct st.stop_id, st.stop_location_id
95        from   wsh_trip_stops st,
96               wsh_delivery_legs dg
97        where  st.trip_id = c_trip_id
98        and    dg.pick_up_stop_id = st.stop_id;
99 
100    cursor get_pickup_delivery (c_stop_id number, c_stop_location_id number) is
101        select '1'
102        from dual
103        where exists (select dl.delivery_id
104                      from   wsh_new_deliveries dl,
105                             wsh_delivery_legs dg
106                      where  dg.pick_up_stop_id = c_stop_id
107                      and    dl.initial_pickup_location_id = c_stop_location_id
108                      AND    nvl(dl.shipment_direction,'O') IN ('O','IO')   -- J-IB-NPARIKH
109                      and    dl.delivery_id = dg.delivery_id );
110 
111   cursor get_org_id (c_trip_id number) is
112        select distinct dl.organization_id
113        from   wsh_trip_stops st, wsh_delivery_legs dg, wsh_new_deliveries dl
114        where  st.trip_id = c_trip_id
115        and    dg.delivery_id = dl.delivery_id
116        and    st.stop_location_id = dl.initial_pickup_location_id
117        and    st.stop_id = dg.pick_up_stop_id;
118 
119   cursor  get_doc_set (c_report_set_id NUMBER) is
120        select name
121        from   wsh_report_sets
122        where  report_set_id = c_report_set_id;
123 
124   l_org_id                      number;
125   l_temp_org_id                 number;
126   l_param_info                  WSH_SHIPPING_PARAMS_PVT.Parameter_Rec_Typ ;
127 
128   -- Bug 3346237:Value for parameter Defer_interface to be taken from Global Parameters table.
129   l_global_info                 WSH_SHIPPING_PARAMS_PVT.Global_Parameters_Rec_Typ;
130   l_stop_id                     number;
131   l_stop_location_id            number;
132   l_exists                      number;
133   l_stop_location_code          varchar2(100);
134   l_return_status               varchar2(500);
135   l_num_errors                  number;
136   l_num_warning                 number;
137 
138 
139 l_debug_on BOOLEAN;
140   --
141   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'Get_Trip_Defaults';
142   e_trip_confirm_exception   EXCEPTION;
143 
144 
145    BEGIN
146 
147    --
148    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
149    --
150    IF l_debug_on IS NULL
151    THEN
152        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
153    END IF;
154    --
155    IF l_debug_on THEN
156        wsh_debug_sv.push (l_module_name);
157        WSH_DEBUG_SV.log(l_module_name,'p_trip_id', p_trip_id);
158        WSH_DEBUG_SV.log(l_module_name,'p_trip_name', p_trip_name);
159    END IF;
160 
161          x_return_status := wsh_util_core.g_ret_sts_success;
162 
163          open get_pickup_stop (p_trip_id);
164          fetch get_pickup_stop into l_stop_id, l_stop_location_id;
165          if get_pickup_stop%notfound then
166             close get_pickup_stop;
167             fnd_message.set_name('WSH','WSH_TRIP_NO_STOPS');
168             fnd_message.set_token('TRIP_NAME',p_trip_name);
169             x_return_status := wsh_util_core.g_ret_sts_error;
170             raise e_trip_confirm_exception;
171          else
172             fetch get_pickup_stop into l_stop_id, l_stop_location_id;
173             if get_pickup_stop%found then
174                close get_pickup_stop;
175                fnd_message.set_name('WSH','WSH_TRIP_MULTIPLE_PICKUPS');
176                l_stop_location_code := fnd_message.get;
177                x_def_rec.trip_multiple_pickup := 'Y';
178             else
179                close get_pickup_stop;
180                l_stop_location_code := wsh_util_core.get_location_description(l_stop_location_id, 'NEW UI CODE');
181                l_stop_location_code := substrb(l_stop_location_code, 1, 60);
182                open get_pickup_delivery(l_stop_id, l_stop_location_id);
183                fetch get_pickup_delivery into l_exists;
184                if get_pickup_delivery%notfound then
185                   close get_pickup_delivery;
186                   fnd_message.set_name('WSH','WSH_TRIP_CONFIRM_MISSING_DEL');
187                   fnd_message.set_token('TRIP',p_trip_name);
188                   fnd_message.set_token('STOP_NAME',l_stop_location_code);
189                   x_return_status := wsh_util_core.g_ret_sts_error;
190                   raise e_trip_confirm_exception;
191                else
192                   close get_pickup_delivery;
193                   x_def_rec.trip_multiple_pickup := 'N';
194                end if;
195             end if;
196          end if;
197 
198              -- set properties and default values on block
199 
200          x_def_rec.stop_location_code := l_stop_location_code;
201 
202          open get_org_id (p_trip_id);
203          fetch get_org_id into l_org_id ;
204          if get_org_id%notfound then
205             l_org_id := NULL;
206          else
207             fetch get_org_id into l_temp_org_id;
208             if get_org_id%notfound then
209                l_temp_org_id := NULL;
210             end if;
211          end if;
212          close get_org_id;
213 
214          if l_org_id is not null then
215             wsh_shipping_params_pvt.get(l_org_id, l_param_info, l_return_status);
216             wsh_util_core.api_post_call(p_return_status  =>l_return_status,
217                                         x_num_warnings     =>l_num_warning,
218                                         x_num_errors       =>l_num_errors);
219 
220         -- Bug 3346237:Value for parameter Defer_interface to be taken from Global Parameters table.
221         wsh_shipping_params_pvt.Get_Global_Parameters(
222                     x_param_info => l_global_info,
223                     x_return_status => l_return_status);
224 
225         wsh_util_core.api_post_call(p_return_status  =>l_return_status,
226                                         x_num_warnings     =>l_num_warning,
227                                         x_num_errors       =>l_num_errors);
228 
229             x_def_rec.defer_interface_flag := l_global_info.defer_interface ;
230 
231         if l_temp_org_id is null then
232                x_def_rec.report_set_id := l_param_info.delivery_report_set_id;
233                open get_doc_set (l_param_info.delivery_report_set_id);
234                fetch get_doc_set into x_def_rec.report_set_name;
235                if get_doc_set%notfound then
236                   x_def_rec.report_set_id := NULL;
237                   x_def_rec.report_set_name := NULL;
238                end if;
239                close get_doc_set;
240             end if;
241          end if;
242 
243     IF l_debug_on THEN
244          WSH_DEBUG_SV.pop(l_module_name);
245     END IF;
246 
247    EXCEPTION
248 
249    WHEN e_trip_confirm_exception THEN
250         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
251       wsh_util_core.add_message(x_return_status,l_module_name);
252       IF l_debug_on THEN
253            wsh_debug_sv.log (l_module_name,'G_EXC_ERROR');
254            WSH_DEBUG_SV.pop(l_module_name);
255       END IF;
256 
257    WHEN OTHERS THEN
258 
259      wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.Get_Trip_Defaults');
260 
261       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
262       IF l_debug_on THEN
263            wsh_debug_sv.log (l_module_name,'Others',substr(sqlerrm,1,200));
264            WSH_DEBUG_SV.pop(l_module_name);
265       END IF;
266 
267 
268 END Get_Trip_Defaults;
269 
270 
271 PROCEDURE Confirm_Trip (
272                          p_trip_id        IN   NUMBER,
273                          p_action_flag    IN   VARCHAR2,
274                          p_intransit_flag IN   VARCHAR2,
275                          p_close_flag     IN   VARCHAR2,
276                          p_stage_del_flag IN   VARCHAR2,
277                          p_report_set_id  IN   NUMBER,
278                          p_ship_method    IN   VARCHAR2,
279                          p_actual_dep_date    IN    DATE,
280                          p_bol_flag       IN   VARCHAR2,
281                          p_defer_interface_flag  IN VARCHAR2,
282              p_mbol_flag  IN VARCHAR2, -- Added MBOL flag
283                          x_return_status  OUT  NOCOPY VARCHAR2) IS
284 
285 cursor get_stops (c_stop_id NUMBER) is
286   select stop_id
287   from   wsh_trip_stops
288   where  trip_id = p_trip_id
289   and    stop_id = NVL(c_stop_id, stop_id)
290   and    status_code IN ('OP','AR')
291   and    nvl(SHIPMENTS_TYPE_FLAG, 'O') IN  ('O', 'M')  -- J Inbound Logistics jckwok
292   order by stop_sequence_number asc ;
293 
294 cursor get_pickup_stop is
295   select t.stop_id
296   from   wsh_trip_stops t,
297          wsh_delivery_legs dg
298   where  t.trip_id = p_trip_id
299   and    dg.pick_up_stop_id = t.stop_id
300   and    t.status_code IN ('OP','AR')
301   and    nvl(SHIPMENTS_TYPE_FLAG, 'O') IN  ('O', 'M');  -- J Inbound Logistics jckwok
302 
303 l_return_status VARCHAR2(1) := NULL;
304 first_stop      VARCHAR2(1) := NULL;
305 l_stop_id       NUMBER      := NULL;
306 invalid_stop    EXCEPTION;
307 trip_confirm_error EXCEPTION;
308 others          EXCEPTION;
309 
310 --Bug#: 2867209 - Start
311 CURSOR c_stop_del_status(p_stop_id NUMBER) IS
312 SELECT 1
313 FROM   wsh_new_deliveries dl,
314        wsh_delivery_legs dg,
315        wsh_trip_stops st,
316        wsh_trips t
317 WHERE  dl.delivery_id = dg.delivery_id AND
318        (dg.pick_up_stop_id = st.stop_id OR dg.drop_off_stop_id = st.stop_id) AND
319        st.trip_id = t.trip_id AND
320        dl.STATUS_CODE <> 'CO' and
321        st.stop_id = p_stop_id;
322 
323 l_stop_del_close NUMBER;
324 l_is_action_not_performed BOOLEAN := TRUE;
325 --Bug#: 2867209 - End
326 
327 -- Exceptions Project
328 l_exceptions_tab  wsh_xc_util.XC_TAB_TYPE;
329 l_exp_logged      BOOLEAN := FALSE;
330 check_exceptions  EXCEPTION;
331 l_msg_count       NUMBER;
332 l_msg_summary     VARCHAR2(4000);
333 l_msg_details     VARCHAR2(4000);
334 l_msg_data        VARCHAR2(4000);
335 x_msg_data        VARCHAR2(4000);
336 x_msg_count       NUMBER;
337 
338 CURSOR c_trip_status IS
339   SELECT status_code
340   FROM   wsh_trips
341   WHERE  trip_id = p_trip_id ;
342 
343 l_trip_status VARCHAR2(2);
344 
345 -- J MBOL
346 l_document_number VARCHAR2(50);
347 --
348 l_wf_rs VARCHAR2(1); --Workflow API return status
349 --
350 l_debug_on BOOLEAN;
351 --
352 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'CONFIRM_TRIP';
353 --
354 
355 BEGIN
356 
357    --
358    -- Debug Statements
359    --
360    --
361    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
362    --
363    IF l_debug_on IS NULL
364    THEN
365        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
366    END IF;
367    --
368    --
369    IF l_debug_on THEN
370        WSH_DEBUG_SV.push(l_module_name);
371        --
372        WSH_DEBUG_SV.log(l_module_name,'P_TRIP_ID',P_TRIP_ID);
373        WSH_DEBUG_SV.log(l_module_name,'P_ACTION_FLAG',P_ACTION_FLAG);
374        WSH_DEBUG_SV.log(l_module_name,'P_INTRANSIT_FLAG',P_INTRANSIT_FLAG);
375        WSH_DEBUG_SV.log(l_module_name,'P_CLOSE_FLAG',P_CLOSE_FLAG);
376        WSH_DEBUG_SV.log(l_module_name,'P_STAGE_DEL_FLAG',P_STAGE_DEL_FLAG);
377        WSH_DEBUG_SV.log(l_module_name,'P_REPORT_SET_ID',P_REPORT_SET_ID);
378        WSH_DEBUG_SV.log(l_module_name,'P_SHIP_METHOD',P_SHIP_METHOD);
379        WSH_DEBUG_SV.log(l_module_name,'P_ACTUAL_DEP_DATE',P_ACTUAL_DEP_DATE);
380        WSH_DEBUG_SV.log(l_module_name,'P_BOL_FLAG',P_BOL_FLAG);
381        WSH_DEBUG_SV.log(l_module_name,'P_DEFER_INTERFACE_FLAG',P_DEFER_INTERFACE_FLAG);
382        WSH_DEBUG_SV.log(l_module_name,'P_MBOL_FLAG',P_MBOL_FLAG);
383 
384    END IF;
385    --
386 
387    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
388 
389    IF p_trip_id IS NULL THEN
390       RAISE others ;
391    END IF;
392 
393    IF p_mbol_flag = 'Y' THEN
394       WSH_MBOLS_PVT.Generate_MBOL(
395                    p_trip_id          => p_trip_id,
396                x_sequence_number  => l_document_number,
397            x_return_status    => l_return_status );
398       IF ( l_return_status <>  WSH_UTIL_CORE.G_RET_STS_SUCCESS )  THEN
399           x_return_status := l_return_status;
400           wsh_util_core.add_message(x_return_status);
401           IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
402             raise TRIP_CONFIRM_ERROR;
403           END IF;
404       END IF;
405    END IF;
406 
407    -- Check for Exceptions against Trip and its contents, only if trip is being set to In-Transit or Closed
408    IF p_intransit_flag = 'Y' OR p_close_flag = 'Y' THEN
409       IF l_debug_on THEN
410          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_XC_UTIL.Check_Exceptions',WSH_DEBUG_SV.C_PROC_LEVEL);
411       END IF;
412       l_exceptions_tab.delete;
413       l_exp_logged      := FALSE;
414       WSH_XC_UTIL.Check_Exceptions (
415                                         p_api_version           => 1.0,
416                                         x_return_status         => l_return_status,
417                                         x_msg_count             =>  l_msg_count,
418                                         x_msg_data              => l_msg_data,
419                                         p_logging_entity_id     => p_trip_id ,
420                                         p_logging_entity_name   => 'TRIP',
421                                         p_consider_content      => 'Y',
422                                         x_exceptions_tab        => l_exceptions_tab
423                                       );
424       IF ( l_return_status <>  WSH_UTIL_CORE.G_RET_STS_SUCCESS )  THEN
425             x_return_status := l_return_status;
426             wsh_util_core.add_message(x_return_status);
427             IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
428                raise others;
429             END IF;
430       END IF;
431       FOR exp_cnt in 1..l_exceptions_tab.COUNT LOOP
432             IF l_exceptions_tab(exp_cnt).exception_behavior = 'ERROR' THEN
433                IF l_exceptions_tab(exp_cnt).entity_name = 'TRIP' THEN
434                   FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_ENTITY');
435                ELSE
436                   FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_CONTENTS');
437                END IF;
438                FND_MESSAGE.SET_TOKEN('ENTITY_NAME','Trip');
439                FND_MESSAGE.SET_TOKEN('ENTITY_ID',l_exceptions_tab(exp_cnt).entity_id);
440                FND_MESSAGE.SET_TOKEN('EXCEPTION_BEHAVIOR','Error');
441                x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
442                wsh_util_core.add_message(x_return_status);
443                raise check_exceptions;
444             ELSIF l_exceptions_tab(exp_cnt).exception_behavior = 'WARNING' THEN
445                IF l_exceptions_tab(exp_cnt).entity_name = 'TRIP' THEN
446                   FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_ENTITY');
447                   FND_MESSAGE.SET_TOKEN('ENTITY_NAME','Trip');
448                   FND_MESSAGE.SET_TOKEN('ENTITY_ID',l_exceptions_tab(exp_cnt).entity_id);
449                   FND_MESSAGE.SET_TOKEN('EXCEPTION_BEHAVIOR','Warning');
450                   x_return_status :=  WSH_UTIL_CORE.G_RET_STS_WARNING;
451                   wsh_util_core.add_message(x_return_status);
452                ELSIF NOT (l_exp_logged) THEN
453                   FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_CONTENTS');
454                   FND_MESSAGE.SET_TOKEN('ENTITY_NAME','Trip');
455                   FND_MESSAGE.SET_TOKEN('ENTITY_ID',l_exceptions_tab(exp_cnt).entity_id);
456                   FND_MESSAGE.SET_TOKEN('EXCEPTION_BEHAVIOR','Warning');
457                   x_return_status :=  WSH_UTIL_CORE.G_RET_STS_WARNING;
458                   l_exp_logged := TRUE;
459                   wsh_util_core.add_message(x_return_status);
460                END IF;
461             END IF;
462       END LOOP;
463    END IF; -- end of check if trip is being set to In-Transit or Closed
464 
465    -- Check if Trip should be set In-Transit, then get the Pickup Stop
466    --  ( If no Open Pickup Stop exists, then return to caller with success )
467    -- Otherwise select all Stops
468    IF p_intransit_flag = 'Y' AND p_close_flag = 'N' THEN
469       OPEN get_pickup_stop;
470       FETCH get_pickup_stop INTO l_stop_id;
471       IF get_pickup_stop%NOTFOUND THEN
472          CLOSE get_pickup_stop;
473          GOTO loop_end ;
474       END IF;
475       CLOSE get_pickup_stop;
476    ELSE
477       l_stop_id := NULL;
478    END IF;
479 
480    --
481    IF l_debug_on THEN
482        WSH_DEBUG_SV.log(l_module_name,'L_STOP_ID',L_STOP_ID);
483    END IF;
484    --
485 
486    g_rate_trip_id := null;
487 
488    FOR stop_rec IN get_stops(l_stop_id) LOOP
489 
490      savepoint sp_confirm_trip;
491 
492      IF l_stop_id IS NOT NULL AND stop_rec.stop_id <> l_stop_id THEN
493         RAISE invalid_stop;
494      END IF;
495 
496     --Bug# 2867209 - Start
497       IF P_CLOSE_FLAG = 'N' THEN
498         open c_stop_del_status(stop_rec.stop_id);
499         FETCH c_stop_del_status INTO l_stop_del_close;
500         IF c_stop_del_status%FOUND THEN
501            l_is_action_not_performed := FALSE;
502         END IF;
503         close c_stop_del_status;
504       END IF;
505     --Bug# 2867209 - End
506 
507      IF first_stop IS NULL THEN
508         first_stop := 'Y';
509      ELSE
510         first_stop := 'N';
511      END IF;
512 
513      --
514      IF l_debug_on THEN
515         WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_STOPS_ACTIONS.Confirm_Stop',WSH_DEBUG_SV.C_PROC_LEVEL);
516      END IF;
517      --
518      WSH_TRIP_STOPS_ACTIONS.Confirm_Stop  (
519                                             p_stop_id               => stop_rec.stop_id,
520                                             p_action_flag           => p_action_flag,
521                                             p_intransit_flag        => p_intransit_flag,
522                                             p_close_flag            => p_close_flag,
523                                             p_stage_del_flag        => p_stage_del_flag,
524                                             p_report_set_id         => p_report_set_id,
525                                             p_ship_method           => p_ship_method,
526                                             p_actual_dep_date       => p_actual_dep_date,
527                                             p_bol_flag              => p_bol_flag,
528                                             p_defer_interface_flag  => p_defer_interface_flag,
529                                             x_return_status         => l_return_status
530                                           );
531 
532      IF l_return_status =  WSH_UTIL_CORE.G_RET_STS_ERROR  THEN
533         -- For first stop, always error out ; for others, treat as warning
534         IF first_stop = 'Y' THEN
535            x_return_status := l_return_status ;
536            rollback to sp_confirm_trip;
537            EXIT ;
538         ELSE
539            -- since it's not the first stop, it can be treated as warning
540            x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING ;
541         END IF;
542 
543      ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR THEN
544         -- For Unexpected errors, always error out
545         x_return_status := l_return_status ;
546         rollback to sp_confirm_trip;
547         EXIT ;
548 
549      ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
550         x_return_status := l_return_status ;
551 
552      END IF;
553 
554    END LOOP ;
555 
556    -- Close Exceptions for the Trip and its contents, only if the Trip is Closed
557    IF p_close_flag = 'Y' THEN
558       -- Only in the case of ITM , Trip remains Open but Confirm_Stop returns success
559       -- Hence before closing exceptions, check to see if Trip status has been changed
560       -- If trip status is still Open, do not close the exceptions
561       OPEN c_trip_status;
562       FETCH c_trip_status INTO l_trip_status;
563       IF c_trip_status%NOTFOUND THEN
564          CLOSE c_trip_status;
565          IF l_debug_on THEN
566             WSH_DEBUG_SV.logmsg(l_module_name,'Trip not found :'||p_trip_id,WSH_DEBUG_SV.C_PROC_LEVEL);
567          END IF;
568          raise no_data_found;
569       END IF;
570       CLOSE c_trip_status;
571 
572       IF l_trip_status = 'CL' THEN
573          IF l_debug_on THEN
574             WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_XC_UTIL.Close_Exceptions',WSH_DEBUG_SV.C_PROC_LEVEL);
575          END IF;
576          WSH_XC_UTIL.Close_Exceptions (
577                                            p_api_version           => 1.0,
578                                            x_return_status         => l_return_status,
579                                            x_msg_count             => l_msg_count,
580                                            x_msg_data              => l_msg_data,
581                                            p_logging_entity_id     => p_trip_id,
582                                            p_logging_entity_name   => 'TRIP',
583                                            p_consider_content      => 'Y'
584                                         ) ;
585 
586          IF ( l_return_status <>  WSH_UTIL_CORE.G_RET_STS_SUCCESS )  THEN
587                x_return_status := l_return_status;
588                wsh_util_core.add_message(x_return_status);
589                IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
590                   raise TRIP_CONFIRM_ERROR;
591                END IF;
592          END IF;
593       END IF;
594    END IF;
595 
596 <<loop_end>>
597   NULL;
598 
599 
600    -- Bug 3374306
601    IF x_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
602       FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_CONFIRM_WARNING');
603       wsh_util_core.add_message(x_return_status);
604    END IF;
605 
606 --Bug# 2867209 - Start
607    IF l_debug_on THEN
608        WSH_DEBUG_SV.log(l_module_name,'l_is_action_not_performed',l_is_action_not_performed);
609    END IF;
610 
611   IF x_return_status = WSH_UTIL_CORE.G_RET_STS_SUCCESS AND P_CLOSE_FLAG = 'N' AND l_is_action_not_performed THEN
612            FND_MESSAGE.SET_NAME('WSH','WSH_NO_CHANGE_ACTION');
613        wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_WARNING);
614   END IF;
615 --Bug# 2867209 - End
616 --
617 
618 --Raise Event: Pick To Pod Workflow
619   IF l_debug_on THEN
620    WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WF_STD.RAISE_EVENT',WSH_DEBUG_SV.C_PROC_LEVEL);
621   END IF;
622 
623  WSH_WF_STD.Raise_Event(
624                          p_entity_type => 'TRIP',
625                          p_entity_id =>  p_trip_id,
626                          p_event => 'oracle.apps.wsh.trip.gen.shipconfirmed' ,
627                          x_return_status => l_wf_rs ) ;
628 
629   IF l_debug_on THEN
630     WSH_DEBUG_SV.log(l_module_name,'Trip ID is  ',  p_trip_id );
631     WSH_DEBUG_SV.log(l_module_name,'Return Status After Calling WSH_WF_STD.Raise_Event',l_wf_rs);
632   END IF;
633 --Done Raise Event: Pick To Pod Workflow
634 
635 -- Debug Statements
636 --
637 IF l_debug_on THEN
638     WSH_DEBUG_SV.pop(l_module_name);
639 END IF;
640 --
641 
642 
643 EXCEPTION
644    WHEN trip_confirm_error THEN
645         --
646         IF l_debug_on THEN
647           WSH_DEBUG_SV.logmsg(l_module_name,'trip_confirm_error exception has occured ',WSH_DEBUG_SV.C_EXCEP_LEVEL);
648           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:trip_confirm_error');
649         END IF;
650         FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_CONFIRM_ERROR');
651         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
652         wsh_util_core.add_message(x_return_status);
653         --
654    WHEN check_exceptions THEN
655         --
656         IF l_debug_on THEN
657           WSH_DEBUG_SV.logmsg(l_module_name,'Check_Exceptions exception has occured ',WSH_DEBUG_SV.C_EXCEP_LEVEL);
658           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:Check_Exceptions');
659         END IF;
660         --
661         WSH_UTIL_CORE.get_messages('N', l_msg_summary, l_msg_details, x_msg_count);
662         --
663         IF l_debug_on THEN
664            WSH_DEBUG_SV.logmsg(l_module_name,  L_MSG_SUMMARY  );
665         END IF;
666         --
667         if x_msg_count > 1 then
668            x_msg_data := l_msg_summary || l_msg_details;
669         else
670            x_msg_data := l_msg_summary;
671         end if;
672 
673 
674    WHEN others THEN
675         rollback to sp_confirm_trip;
676         x_return_status :=  WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR ;
677         wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.CONFIRM_TRIP');
678         --
679         IF l_debug_on THEN
680            WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
681            WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
682         END IF;
683         --
684         WSH_UTIL_CORE.get_messages('N', l_msg_summary, l_msg_details, x_msg_count);
685         --
686         IF l_debug_on THEN
687            WSH_DEBUG_SV.logmsg(l_module_name,  L_MSG_SUMMARY  );
688         END IF;
689         --
690         if x_msg_count > 1 then
691            x_msg_data := l_msg_summary || l_msg_details;
692         else
693            x_msg_data := l_msg_summary;
694         end if;
695 
696 END;
697 
698 
699 
700 PROCEDURE Plan(
701       p_trip_rows    IN wsh_util_core.id_tab_type,
702       p_action IN   VARCHAR2,
703       x_return_status   OUT NOCOPY    VARCHAR2) IS
704 
705 l_num_error  NUMBER := 0;
706 l_num_warn   NUMBER := 0;
707 l_trip_rows  wsh_util_core.id_tab_type;
708 l_trip_status VARCHAR2(2);
709 l_stop_status VARCHAR2(2);
710 
711 cursor get_status(c_trip_id IN NUMBER) is
712 select status_code,
713        NVL(shipments_type_flag,'O')   -- J-IB-NPARIKH
714 from wsh_trips
715 where trip_id = c_trip_id;
716 
717 cursor get_stops(c_trip_id IN NUMBER) is
718 select 'Y'
719 from wsh_trip_stops
720 where trip_id = c_trip_id
721 and status_code <> 'OP'
722 and rownum = 1;
723 
724 CURSOR c_istripfirm(p_tripid IN NUMBER) IS
725 select 'Y'
726 from wsh_trips
727 where trip_id=p_tripid AND
728 planned_flag='F';
729 
730 l_tripfirm VARCHAR2(1);
731 
732 others EXCEPTION;
733 
734 --
735 l_debug_on BOOLEAN;
736 --
737 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'PLAN';
738 --
739 l_shipments_type_flag     VARCHAR2(30);
740 BEGIN
741 
742    --
743    -- Debug Statements
744    --
745    --
746    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
747    --
748    IF l_debug_on IS NULL
749    THEN
750        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
751    END IF;
752    --
753    IF l_debug_on THEN
754        WSH_DEBUG_SV.push(l_module_name);
755        --
756        WSH_DEBUG_SV.log(l_module_name,'P_ACTION',P_ACTION);
757    END IF;
758    --
759    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
760 
761 
762    IF (p_trip_rows.count = 0) THEN
763     raise others;
764    END IF;
765 
766    FOR i IN 1..p_trip_rows.count LOOP
767 
768      --tkt removed code for validation of trip status for planning/firming
769 
770      IF (p_action IN ('PLAN', 'FIRM')) THEN
771        --
772        -- Debug Statements
773        --
774        IF l_debug_on THEN
775            WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_VALIDATIONS.CHECK_PLAN',WSH_DEBUG_SV.C_PROC_LEVEL);
776        END IF;
777        --
778        wsh_trip_validations.check_plan(p_trip_rows(i), x_return_status);
779      ELSIF (p_action='UNPLAN') THEN
780        --
781        -- Debug Statements
782        --
783        IF l_debug_on THEN
784            WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_VALIDATIONS.CHECK_UNPLAN',WSH_DEBUG_SV.C_PROC_LEVEL);
785        END IF;
786        --
787        wsh_trip_validations.check_unplan(p_trip_rows(i), x_return_status);
788      END IF;
789 
790      IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR) OR (x_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
791       goto plan_error;
792     ELSIF (x_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING) THEN
793       l_num_warn := l_num_warn + 1;
794      END IF;
795 
796 /* H integration ,this call is made from Group and Public API
797 TO DO
798 verify for the FORM */
799 /* End of H integration */
800      /* J TP Release */
801 
802      IF p_action='FIRM' THEN
803          wsh_tp_release.firm_entity( p_entity        => 'TRIP',
804                                      p_entity_id     =>p_trip_rows(i),
805                                      x_return_status =>x_return_status);
806          IF (x_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN
807            IF x_return_status=WSH_UTIL_CORE.G_RET_STS_WARNING THEN
808               l_num_warn := l_num_warn + 1;
809            ELSE
810               goto plan_error;
811            END IF;
812          END IF;
813 
814      ELSIF p_action IN ('PLAN','UNPLAN') THEN
815          wsh_tp_release.unfirm_entity( p_entity      => 'TRIP',
816                                      p_entity_id     =>p_trip_rows(i),
817                                      p_action        =>p_action,
818                                      x_return_status =>x_return_status);
819          IF (x_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN
820            IF x_return_status=WSH_UTIL_CORE.G_RET_STS_WARNING THEN
821               l_num_warn := l_num_warn + 1;
822            ELSE
823               goto plan_error;
824            END IF;
825          END IF;
826      END IF;
827 
828     goto loop_end;
829 
830     <<plan_error>>
831 
832        IF (p_action = 'PLAN') THEN
833          FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_PLAN_ERROR');
834        ELSIF (p_action='FIRM') THEN
835          FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_FIRM_ERROR');
836        ELSE
837          FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_INVALID_STATUS');
838        END IF;
839 
840       --
841       -- Debug Statements
842       --
843       IF l_debug_on THEN
844           WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
845       END IF;
846       --
847       FND_MESSAGE.SET_TOKEN('TRIP_NAME',wsh_trips_pvt.get_name(p_trip_rows(i)));
848       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
849       wsh_util_core.add_message(x_return_status);
850       l_num_error := l_num_error + 1;
851 
852      <<loop_end>>
853       null;
854 
855    END LOOP;
856 
857    IF (p_trip_rows.count > 1) THEN
858 
859      IF (l_num_error > 0) OR (l_num_warn > 0) THEN
860 
861        IF (p_action = 'PLAN') THEN
862          FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_PLAN_SUMMARY');
863          FND_MESSAGE.SET_TOKEN('NUM_WARN',l_num_warn);
864        ELSIF (p_action = 'FIRM') THEN
865          FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_FIRM_SUMMARY');
866          FND_MESSAGE.SET_TOKEN('NUM_WARN',l_num_warn);
867        ELSE
868          FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_UNPLAN_SUMMARY');
869        END IF;
870 
871       FND_MESSAGE.SET_TOKEN('NUM_ERROR',l_num_error);
872       FND_MESSAGE.SET_TOKEN('NUM_SUCCESS',p_trip_rows.count - l_num_error - l_num_warn);
873 
874       IF (p_trip_rows.count = l_num_error) THEN
875          x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
876        ELSE
877          x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
878        END IF;
879 
880       wsh_util_core.add_message(x_return_status);
881 
882      ELSE
883       x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
884      END IF;
885 
886    END IF;
887 
888 --
889 -- Debug Statements
890 --
891 IF l_debug_on THEN
892     WSH_DEBUG_SV.pop(l_module_name);
893 END IF;
894 --
895    EXCEPTION
896      WHEN others THEN
897       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.PLAN');
898       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
899 
900 --
901 -- Debug Statements
902 --
903 IF l_debug_on THEN
904     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
905     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
906 END IF;
907 --
908 END Plan;
909 
910 PROCEDURE Change_Status (
911       p_trip_id    IN    NUMBER,
912       p_status_code  IN VARCHAR2,
913       x_return_status   OUT NOCOPY    VARCHAR2) IS
914 
915 i   BINARY_INTEGER;
916 /* H integration  for Multi Leg */
917   l_stop_rec   WSH_TRIP_STOPS_PVT.TRIP_STOP_REC_TYPE;
918   l_pub_stop_rec  WSH_TRIP_STOPS_PUB.TRIP_STOP_PUB_REC_TYPE;
919   l_trip_rec   WSH_TRIPS_PVT.TRIP_REC_TYPE;
920   l_pub_trip_rec  WSH_TRIPS_PUB.TRIP_PUB_REC_TYPE;
921   l_return_status VARCHAR2(30);
922   l_num_warn NUMBER := 0;
923 
924 -- Exceptions Project
925 l_exceptions_tab  wsh_xc_util.XC_TAB_TYPE;
926 l_exp_logged      BOOLEAN := FALSE;
927 check_exceptions  EXCEPTION;
928 l_msg_count       NUMBER;
929 l_msg_data        VARCHAR2(4000);
930 
931 --
932 l_debug_on BOOLEAN;
933 --
934 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'CHANGE_STATUS';
935 --
936 BEGIN
937 /* H integration  */
938 /* Could not find a place where this is being called*/
939 --
940 -- Debug Statements
941 --
942 --
943 l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
944 --
945 IF l_debug_on IS NULL
946 THEN
947     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
948 END IF;
949 --
950 IF l_debug_on THEN
951     WSH_DEBUG_SV.push(l_module_name);
952     --
953     WSH_DEBUG_SV.log(l_module_name,'P_TRIP_ID',P_TRIP_ID);
954     WSH_DEBUG_SV.log(l_module_name,'P_STATUS_CODE',P_STATUS_CODE);
955 END IF;
956 
957 /**
958 -- J-IB-NPARIKH-{
959 --
960 -- stubbed out as no longer being called.
961 --
962      x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
963      RETURN;
964 -- J-IB-NPARIKH-}
965 **/
966 
967 -- Check for Exceptions if p_status_code = 'CL' or 'IT'
968 IF p_status_code IN ('IT','CL') THEN
969    IF l_debug_on THEN
970       WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_XC_UTIL.Check_Exceptions',WSH_DEBUG_SV.C_PROC_LEVEL);
971    END IF;
972    l_exceptions_tab.delete;
973    l_exp_logged      := FALSE;
974    WSH_XC_UTIL.Check_Exceptions (
975                                      p_api_version           => 1.0,
976                                      x_return_status         => l_return_status,
977                                      x_msg_count             =>  l_msg_count,
978                                      x_msg_data              => l_msg_data,
979                                      p_logging_entity_id     => p_trip_id ,
980                                      p_logging_entity_name   => 'TRIP',
981                                      p_consider_content      => 'Y',
982                                      x_exceptions_tab        => l_exceptions_tab
983                                    );
984    IF ( l_return_status <>  WSH_UTIL_CORE.G_RET_STS_SUCCESS )  THEN
985          x_return_status := l_return_status;
986          wsh_util_core.add_message(x_return_status);
987          IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
988             raise FND_API.G_EXC_ERROR;
989          END IF;
990    END IF;
991    FOR exp_cnt in 1..l_exceptions_tab.COUNT LOOP
992          IF l_exceptions_tab(exp_cnt).exception_behavior = 'ERROR' THEN
993             IF l_exceptions_tab(exp_cnt).entity_name = 'TRIP' THEN
994                FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_ENTITY');
995             ELSE
996                FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_CONTENTS');
997             END IF;
998             FND_MESSAGE.SET_TOKEN('ENTITY_NAME','Trip');
999             FND_MESSAGE.SET_TOKEN('ENTITY_ID',l_exceptions_tab(exp_cnt).entity_id);
1000             FND_MESSAGE.SET_TOKEN('EXCEPTION_BEHAVIOR','Error');
1001             x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
1002             wsh_util_core.add_message(x_return_status);
1003             raise check_exceptions;
1004          ELSIF l_exceptions_tab(exp_cnt).exception_behavior = 'WARNING' THEN
1005             IF l_exceptions_tab(exp_cnt).entity_name = 'TRIP' THEN
1006                FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_ENTITY');
1007                FND_MESSAGE.SET_TOKEN('ENTITY_NAME','Trip');
1008                FND_MESSAGE.SET_TOKEN('ENTITY_ID',l_exceptions_tab(exp_cnt).entity_id);
1009                FND_MESSAGE.SET_TOKEN('EXCEPTION_BEHAVIOR','Warning');
1010                x_return_status :=  WSH_UTIL_CORE.G_RET_STS_WARNING;
1011                wsh_util_core.add_message(x_return_status);
1012             ELSIF NOT (l_exp_logged) THEN
1013                FND_MESSAGE.SET_NAME('WSH','WSH_XC_EXIST_CONTENTS');
1014                FND_MESSAGE.SET_TOKEN('ENTITY_NAME','Trip');
1015                FND_MESSAGE.SET_TOKEN('ENTITY_ID',l_exceptions_tab(exp_cnt).entity_id);
1016                FND_MESSAGE.SET_TOKEN('EXCEPTION_BEHAVIOR','Warning');
1017                x_return_status :=  WSH_UTIL_CORE.G_RET_STS_WARNING;
1018                l_exp_logged := TRUE;
1019                wsh_util_core.add_message(x_return_status);
1020             END IF;
1021          END IF;
1022    END LOOP;
1023 END IF;
1024 
1025 --
1026 IF (WSH_UTIL_CORE.FTE_IS_INSTALLED = 'Y') THEN
1027  -- Get pvt type record structure for trip
1028      --
1029      -- Debug Statements
1030      --
1031      IF l_debug_on THEN
1032          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_GRP.GET_TRIP_DETAILS_PVT',WSH_DEBUG_SV.C_PROC_LEVEL);
1033      END IF;
1034      --
1035      wsh_trips_grp.get_trip_details_pvt
1036        (p_trip_id => p_trip_id,
1037         x_trip_rec => l_trip_rec,
1038         x_return_status => l_return_status);
1039 
1040      IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
1041       x_return_status := l_return_status;
1042       --
1043       -- Debug Statements
1044       --
1045       IF l_debug_on THEN
1046           WSH_DEBUG_SV.pop(l_module_name);
1047       END IF;
1048       --
1049       RETURN;
1050      END IF;
1051      l_trip_rec.status_code := p_status_code;
1052 
1053      --
1054      -- Debug Statements
1055      --
1056      IF l_debug_on THEN
1057          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_FTE_INTEGRATION.TRIP_STOP_VALIDATIONS',WSH_DEBUG_SV.C_PROC_LEVEL);
1058      END IF;
1059      --
1060      wsh_fte_integration.trip_stop_validations
1061       (p_stop_rec => l_stop_rec,
1062        p_trip_rec => l_trip_rec,
1063        p_action => 'UPDATE',
1064        x_return_status => l_return_status);
1065 
1066      IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
1067             IF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
1068               l_num_warn := l_num_warn + 1;
1069             ELSE
1070       x_return_status := l_return_status;
1071       --
1072       -- Debug Statements
1073       --
1074       IF l_debug_on THEN
1075           WSH_DEBUG_SV.pop(l_module_name);
1076       END IF;
1077       --
1078       RETURN;
1079             END IF;
1080      END IF;
1081 
1082 END IF;
1083 /* End of H integration  */
1084 
1085  --Bug Fix 2993711 added last_update_date,last_updated_by,last_update_login --
1086 
1087   UPDATE wsh_trips
1088    SET status_code = p_status_code,
1089        last_update_date  = SYSDATE,
1090        last_updated_by   = fnd_global.user_id,
1091        last_update_login = fnd_global.login_id
1092    WHERE trip_id = p_trip_id;
1093 
1094 IF (SQL%NOTFOUND) THEN
1095      x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
1096     --
1097     -- Debug Statements
1098     --
1099     IF l_debug_on THEN
1100         WSH_DEBUG_SV.pop(l_module_name);
1101     END IF;
1102     --
1103     RETURN;
1104    END IF;
1105 
1106 -- Close Exceptions for the Trip and its contents
1107 IF p_status_code IN ('IT','CL') THEN
1108    IF l_debug_on THEN
1109       WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_XC_UTIL.Close_Exceptions',WSH_DEBUG_SV.C_PROC_LEVEL);
1110    END IF;
1111    WSH_XC_UTIL.Close_Exceptions (
1112                                      p_api_version           => 1.0,
1113                                      x_return_status         => l_return_status,
1114                                      x_msg_count             => l_msg_count,
1115                                      x_msg_data              => l_msg_data,
1116                                      p_logging_entity_id     => p_trip_id,
1117                                      p_logging_entity_name   => 'TRIP',
1118                                      p_consider_content      => 'Y'
1119                                   ) ;
1120 
1121    IF ( l_return_status <>  WSH_UTIL_CORE.G_RET_STS_SUCCESS )  THEN
1122          x_return_status := l_return_status;
1123          wsh_util_core.add_message(x_return_status);
1124          IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
1125             raise FND_API.G_EXC_ERROR;
1126          END IF;
1127    END IF;
1128 
1129    IF l_num_warn >0 THEN
1130      x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
1131    ELSE
1132      x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
1133    END IF;
1134 END IF;
1135 
1136 -- Debug Statements
1137 --
1138 IF l_debug_on THEN
1139     WSH_DEBUG_SV.pop(l_module_name);
1140 END IF;
1141 --
1142 
1143 EXCEPTION
1144      WHEN check_exceptions THEN
1145         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
1146         --
1147         IF l_debug_on THEN
1148           WSH_DEBUG_SV.logmsg(l_module_name,'Check_Exceptions exception has occured ',WSH_DEBUG_SV.C_EXCEP_LEVEL);
1149           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:Check_Exceptions');
1150         END IF;
1151         --
1152 
1153      WHEN others THEN
1154         wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.CHANGE_STATUS');
1155         x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
1156         --
1157         IF l_debug_on THEN
1158            WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
1159            WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
1160         END IF;
1161         --
1162 
1163 END Change_Status;
1164 
1165 
1166 -- J-IB-NPARIKH-{
1167 --
1168 --========================================================================
1169 -- PROCEDURE : changeStatus
1170 --
1171 -- PARAMETERS: p_in_rec          Trip details record.
1172 --             x_return_status   Return status of the API
1173 --
1174 --
1175 -- COMMENT   : This procedure is called only from
1176 --               - stop close API (to set trip to in-transit/closed)
1177 --               - stop open API (to set trip to in-transit/open, for inbound only).
1178 --             It performs the following steps:
1179 --             01. Check that trip's new status is OP/IT/CL.
1180 --             02. Check for exceptions against trip(part of J exceptions project) -- moved to stop validations api
1181 --             03. If FTE is installed, callout to FTE for validations
1182 --             04. Update trip with new status
1183 --             05. TP Release actions - Change firm and ignore for plan status of trip
1184 --             06. Close exceptions against trip, if set to in-transit/closed (part of J exceptions project)
1185 --
1186 --========================================================================
1187 --
1188 PROCEDURE changeStatus
1189             (
1190               p_in_rec             IN          WSH_TRIP_VALIDATIONS.ChgStatus_in_rec_type,
1191               x_return_status      OUT NOCOPY  VARCHAR2
1192             )
1193 IS
1194 --{
1195     --- TP release
1196     CURSOR c_getdels IS
1197     SELECT delivery_id
1198     FROM wsh_delivery_legs wdl, wsh_trip_stops wts
1199     WHERE wdl.pick_up_stop_id=wts.stop_id AND
1200         wts.trip_id=p_in_rec.trip_id;
1201 
1202 
1203     /* H integration  for Multi Leg */
1204     l_stop_rec   WSH_TRIP_STOPS_PVT.TRIP_STOP_REC_TYPE;
1205     l_trip_rec   WSH_TRIPS_PVT.TRIP_REC_TYPE;
1206     --
1207     --
1208     l_num_warnings          NUMBER;
1209     l_num_errors            NUMBER;
1210     l_return_status         VARCHAR2(30);
1211     --
1212 
1213     -- Exceptions Project
1214     l_msg_count       NUMBER;
1215     l_msg_data        VARCHAR2(4000);
1216 
1217     l_trip_id_tab     wsh_util_core.id_tab_type;
1218     l_del_tmp_rows      wsh_util_core.id_tab_type;
1219 
1220     l_wf_rs 	VARCHAR2(1); 	-- Workflow Project
1221     l_debug_on    BOOLEAN;
1222     --
1223     l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'changeStatus';
1224 --}
1225 BEGIN
1226 --{
1227     --SAVEPOINT trip_chgStatus_begin_sp;
1228     --
1229     -- Debug Statements
1230     --
1231     --
1232     l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
1233     --
1234     IF l_debug_on IS NULL
1235     THEN
1236       l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
1237     END IF;
1238     --
1239     IF l_debug_on THEN
1240       WSH_DEBUG_SV.push(l_module_name);
1241       --
1242       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.trip_id         ', p_in_rec.trip_id         );
1243       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.name            ', p_in_rec.name            );
1244       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.new_Status_code ', p_in_rec.new_Status_code );
1245       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.put_messages    ', p_in_rec.put_messages    );
1246       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.manual_flag     ', p_in_rec.manual_flag     );
1247       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.caller          ', p_in_rec.caller          );
1248       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.actual_date     ', p_in_rec.actual_date     );
1249       wsh_debug_sv.LOG(l_module_name, 'p_in_rec.stop_id         ', p_in_rec.stop_id         );
1250     END IF;
1251     --
1252     x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
1253     l_num_warnings  := 0;
1254     l_num_errors    := 0;
1255     --
1256     IF p_in_rec.new_status_code NOT IN ('OP','IT','CL')
1257     THEN
1258     --{
1259           --
1260           -- Invalid status for trip, raise error.
1261           --
1262           FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_INVALID_STATUS');
1263           FND_MESSAGE.SET_TOKEN('TRIP_NAME','p_in_rec.name');
1264           wsh_util_core.add_message(wsh_util_core.g_ret_sts_error,l_module_name);
1265           RAISE FND_API.G_EXC_ERROR;
1266     --}
1267     END IF;
1268     --
1269 
1270     IF (WSH_UTIL_CORE.FTE_IS_INSTALLED = 'Y')
1271     THEN
1272     --{
1273          -- Get pvt type record structure for trip
1274          --
1275          -- Debug Statements
1276          --
1277          IF l_debug_on THEN
1278              WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_GRP.GET_TRIP_DETAILS_PVT',WSH_DEBUG_SV.C_PROC_LEVEL);
1279          END IF;
1280          --
1281          wsh_trips_grp.get_trip_details_pvt
1282            (p_trip_id => p_in_rec.trip_id,
1283             x_trip_rec => l_trip_rec,
1284             x_return_status => l_return_status);
1285         --
1286         WSH_UTIL_CORE.api_post_call
1287             (
1288                 p_return_status => l_return_status,
1289                 x_num_warnings  => l_num_warnings,
1290                 x_num_errors    => l_num_errors
1291             );
1292         --
1293          l_trip_rec.status_code := p_in_rec.new_status_code;
1294 
1295          --
1296          -- Debug Statements
1297          --
1298          IF l_debug_on THEN
1299              WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_FTE_INTEGRATION.TRIP_STOP_VALIDATIONS',WSH_DEBUG_SV.C_PROC_LEVEL);
1300          END IF;
1301          --
1302          wsh_fte_integration.trip_stop_validations
1303           (p_stop_rec => l_stop_rec,
1304            p_trip_rec => l_trip_rec,
1305            p_action => 'UPDATE',
1306            x_return_status => l_return_status);
1307         --
1308         WSH_UTIL_CORE.api_post_call
1309             (
1310                 p_return_status => l_return_status,
1311                 x_num_warnings  => l_num_warnings,
1312                 x_num_errors    => l_num_errors
1313             );
1314         --
1315     --}
1316     END IF;
1317     /* End of H integration  */
1318 
1319      --Bug Fix 2993711 added last_update_date,last_updated_by,last_update_login --
1320 
1321       UPDATE wsh_trips
1322        SET status_code       = p_in_rec.new_status_code,
1323            last_update_date  = SYSDATE,
1324            last_updated_by   = fnd_global.user_id,
1325            last_update_login = fnd_global.login_id
1326        WHERE trip_id         = p_in_rec.trip_id;
1327 
1328     IF (SQL%NOTFOUND) THEN
1329         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1330     END IF;
1331    --
1332    --
1333    -- Workflow Project
1334    IF p_in_rec.new_status_code = 'IT' THEN
1335         -- Raise Initial Pickup Stop Closed Event
1336 	IF l_debug_on THEN
1337 	  WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WF_STD.RAISE_EVENT',WSH_DEBUG_SV.C_PROC_LEVEL);
1338 	END IF;
1339 
1340 	WSH_WF_STD.RAISE_EVENT( p_entity_type   =>      'TRIP',
1341 				p_entity_id     =>      p_in_rec.trip_id,
1342 				p_event         =>      'oracle.apps.wsh.trip.gen.initialpickupstopclosed',
1343 				x_return_status =>      l_wf_rs
1344 			      );
1345 	IF l_debug_on THEN
1346 	  WSH_DEBUG_SV.log(l_module_name,'Return status after calling WSH_WF_STD.RAISE_EVENT => ',l_wf_rs);
1347 	END IF;
1348    ELSIF p_in_rec.new_status_code = 'CL' THEN
1349         -- Raise Ultimate Dropoff Stop Closed Event
1350 	IF l_debug_on THEN
1351 	  WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WF_STD.RAISE_EVENT',WSH_DEBUG_SV.C_PROC_LEVEL);
1352 	END IF;
1353 
1354 	WSH_WF_STD.RAISE_EVENT( p_entity_type   =>      'TRIP',
1355 				p_entity_id     =>      p_in_rec.trip_id,
1356 				p_event         =>      'oracle.apps.wsh.trip.gen.ultimatedropoffstopclosed',
1357 				x_return_status =>      l_wf_rs
1358 			      );
1359 	IF l_debug_on THEN
1360 	  WSH_DEBUG_SV.log(l_module_name,'Return status after calling WSH_WF_STD.RAISE_EVENT => ',l_wf_rs);
1361 	END IF;
1362    END IF;
1363    -- End of code for Workflow Project
1364 
1365     --setting trip to in-transit, need to set delivery to be firmed
1366     --which will make trip to be atleast planned
1367     IF WSH_UTIL_CORE.TP_IS_INSTALLED='Y' AND l_trip_rec.ship_method_code is not null THEN
1368       IF l_trip_rec.mode_of_transport ='TRUCK' and l_trip_rec.vehicle_item_id is null THEN
1369          --if mode is truck, we can't plan/firm trip.
1370          IF l_debug_on THEN
1371              WSH_DEBUG_SV.logmsg(l_module_name,'Not trying to firm trip/delivery because mode is truck');
1372          END IF;
1373       ELSE -- mode and vehicle
1374           IF p_in_rec.new_status_code = 'IT' THEN
1375 
1376              l_del_tmp_rows.delete;
1377              FOR cur IN c_getdels LOOP
1378                l_del_tmp_rows(l_del_tmp_rows.COUNT+1):=cur.delivery_id;
1379              END LOOP;
1380 
1381              IF l_del_tmp_rows.COUNT>0 THEN
1382                wsh_new_delivery_actions.firm (p_del_rows      => l_del_tmp_rows,
1383                                            x_return_status => l_return_status);
1384                WSH_UTIL_CORE.api_post_call
1385                 (
1386                     p_return_status => l_return_status,
1387                     x_num_warnings  => l_num_warnings,
1388                     x_num_errors    => l_num_errors
1389                 );
1390              END IF;
1391           --closing trip => trip has to be firmed.all deliveries will be firmed as well
1392           ELSIF p_in_rec.new_status_code = 'CL' THEN
1393             l_trip_id_tab.delete;
1394             l_trip_id_tab(1):=p_in_rec.trip_id;
1395             Plan(
1396                  p_trip_rows       => l_trip_id_tab,
1397                  p_action          => 'FIRM',
1398                  x_return_status   => l_return_status);
1399 
1400             WSH_UTIL_CORE.api_post_call
1401                 (
1402                     p_return_status => l_return_status,
1403                     x_num_warnings  => l_num_warnings,
1404                     x_num_errors    => l_num_errors
1405                 );
1406           END IF;--p_status_code
1407       END IF;
1408     ELSIF WSH_UTIL_CORE.TP_IS_INSTALLED='Y' AND l_trip_rec.ship_method_code is null THEN
1409          --TP is installed and ship method is null, cannot make the trip as firmed
1410          IF l_debug_on THEN
1411              WSH_DEBUG_SV.logmsg(l_module_name,'Not trying to firm trip/delivery because ship method is null');
1412          END IF;
1413     END IF;
1414    --
1415    -- Close Exceptions for the Trip and its contents, if new status = 'CL' or 'IT'
1416    IF p_in_rec.new_status_code IN ('IT','CL') THEN
1417       IF l_debug_on THEN
1418          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_XC_UTIL.Close_Exceptions',WSH_DEBUG_SV.C_PROC_LEVEL);
1419       END IF;
1420       WSH_XC_UTIL.Close_Exceptions (
1421                                         p_api_version           => 1.0,
1422                                         x_return_status         => l_return_status,
1423                                         x_msg_count             => l_msg_count,
1424                                         x_msg_data              => l_msg_data,
1425                                         p_logging_entity_id     => p_in_rec.trip_id,
1426                                         p_logging_entity_name   => 'TRIP',
1427                                         p_consider_content      => 'Y'
1428                                     ) ;
1429 
1430       IF ( l_return_status <>  WSH_UTIL_CORE.G_RET_STS_SUCCESS )  THEN
1431            x_return_status := l_return_status;
1432            wsh_util_core.add_message(x_return_status);
1433            IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
1434               raise FND_API.G_EXC_ERROR;
1435            END IF;
1436       END IF;
1437    END IF;
1438    --
1439    --
1440    IF l_num_errors > 0
1441    THEN
1442         x_return_status         := WSH_UTIL_CORE.G_RET_STS_ERROR;
1443    ELSIF l_num_warnings > 0
1444    THEN
1445         x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
1446    ELSE
1447         x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
1448    END IF;
1449    --
1450    --
1451     --
1452     -- Debug Statements
1453     --
1454     IF l_debug_on THEN
1455         WSH_DEBUG_SV.pop(l_module_name);
1456     END IF;
1457     --
1458 --}
1459 EXCEPTION
1460 --{
1461     WHEN FND_API.G_EXC_ERROR THEN
1462 
1463       --ROLLBACK TO trip_chgStatus_begin_sp;
1464       --
1465       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
1466       --
1467       IF l_debug_on THEN
1468         WSH_DEBUG_SV.logmsg(l_module_name,'FND_API.G_EXC_ERROR exception has occured ',WSH_DEBUG_SV.C_EXCEP_LEVEL);
1469         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:FND_API.G_EXC_ERROR');
1470       END IF;
1471       --
1472     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1473 
1474       --ROLLBACK TO trip_chgStatus_begin_sp;
1475       --
1476       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR ;
1477       --
1478       IF l_debug_on THEN
1479         WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
1480         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:FND_API.G_EXC_UNEXPECTED_ERROR');
1481       END IF;
1482       --
1483     WHEN WSH_UTIL_CORE.G_EXC_WARNING THEN
1484       x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
1485       --
1486       IF l_debug_on THEN
1487         WSH_DEBUG_SV.logmsg(l_module_name,'WSH_UTIL_CORE.G_EXC_WARNING exception has occured ',WSH_DEBUG_SV.C_EXCEP_LEVEL);
1488         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:WSH_UTIL_CORE.G_EXC_WARNING');
1489       END IF;
1490      WHEN others THEN
1491         wsh_util_core.default_handler('WSH_NEW_DELIVERY_ACTIONS.changeStatus',l_module_name);
1492         --
1493         --ROLLBACK TO trip_chgStatus_begin_sp;
1494         --
1495         x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
1496         --
1497         -- Debug Statements
1498         --
1499         IF l_debug_on THEN
1500             WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
1501         END IF;
1502         --
1503 --}
1504 END changeStatus;
1505 
1506 --
1507 --========================================================================
1508 -- PROCEDURE : generateRoutingResponse
1509 --
1510 -- PARAMETERS: p_action_prms     Standard action parameters record
1511 --             p_rec_attr_tab    Table of trip records
1512 --             x_return_status   Return status of the API
1513 --
1514 --
1515 -- COMMENT   : Trigger routing response for deliveries within trip.
1516 --             This procedure finds all deliveries with initial pickup location on the trip.
1517 --             For all such deliveries, it calls delivery-level group API to generate routing response
1518 --             If there are no such deliveries among all input trips, it raises an error.
1519 --
1520 --========================================================================
1521 PROCEDURE generateRoutingResponse
1522             (
1523               p_action_prms            IN   WSH_TRIPS_GRP.action_parameters_rectype,
1524               p_rec_attr_tab           IN   WSH_TRIPS_PVT.Trip_Attr_Tbl_Type,
1525               x_return_status          OUT     NOCOPY  VARCHAR2
1526             )
1527 IS
1528 --{
1529     l_num_warnings              NUMBER  := 0;
1530     l_num_errors                NUMBER  := 0;
1531     l_return_status             VARCHAR2(30);
1532     l_msg_count                 NUMBER;
1533     l_msg_data                  VARCHAR2(4000);
1534     --
1535     l_index                     NUMBER;
1536     --
1537     -- Get deliveries which have initial pickup location on the input trip
1538     --
1539     CURSOR dlvy_csr(p_trip_id NUMBER)
1540     IS
1541       SELECT  wdl.delivery_id, wt.name
1542       FROM    wsh_trip_stops wts,
1543               wsh_Delivery_legs wdl,
1544               wsh_new_deliveries wnd,
1545               wsh_trips wt
1546       WHERE   wt.trip_id                      = p_trip_id
1547       AND     wts.trip_id                     = p_trip_id
1548       AND     wdl.pick_up_stop_id             = wts.stop_id
1549       AND     wnd.delivery_id                 = wdl.delivery_id
1550       AND     nvl(wnd.shipment_direction,'O') NOT IN ('O','IO')   -- J-IB-NPARIKH
1551       AND     wnd.initial_pickup_location_id  = wts.stop_location_id;
1552     --
1553     --
1554     l_deliveryIdTbl       WSH_UTIL_CORE.key_value_tab_type;
1555     l_deliveryIdExtTbl    WSH_UTIL_CORE.key_value_tab_type;
1556     --
1557     l_action_prms wsh_deliveries_grp.action_parameters_rectype;
1558     l_del_action_out_rec wsh_deliveries_grp.Delivery_Action_Out_Rec_Type;
1559     l_delivery_id_tab             wsh_util_core.id_tab_type;
1560     l_trip_name                   VARCHAR2(30);
1561     l_cnt                         NUMBER;
1562     l_totalCnt                    NUMBER;
1563 --
1564 l_debug_on BOOLEAN;
1565 --
1566 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'generateRoutingResponse';
1567 --
1568 --}
1569 BEGIN
1570 --{
1571     --
1572     l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
1573     --
1574     IF l_debug_on IS NULL
1575     THEN
1576         l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
1577     END IF;
1578     --
1579     --
1580     -- Debug Statements
1581     --
1582     IF l_debug_on THEN
1583         WSH_DEBUG_SV.push(l_module_name);
1584        wsh_debug_sv.log (l_module_name,'action_code',p_action_prms.action_code);
1585        wsh_debug_sv.log (l_module_name,'caller',p_action_prms.caller);
1586        wsh_debug_sv.log (l_module_name,'COUNT',p_rec_attr_tab.COUNT);
1587     END IF;
1588     --
1589     --
1590     l_cnt := 0;
1591     l_totalCnt := 0;
1592     --
1593     l_index := p_rec_attr_tab.FIRST;
1594     --
1595     --
1596     WHILE l_index IS NOT NULL
1597     LOOP
1598     --{
1599         l_cnt := 0;
1600         l_trip_name := NULL;
1601         --
1602         IF l_debug_on THEN
1603             wsh_debug_sv.log (l_module_name,'l_index',l_index);
1604             wsh_debug_sv.log (l_module_name,'trip_id',p_rec_attr_tab(l_index).trip_id);
1605         END IF;
1606         --
1607         -- Get deliveries which have initial pickup location on the input trip
1608         --
1609         FOR dlvy_rec IN dlvy_csr(p_rec_attr_tab(l_index).trip_id)
1610         LOOP
1611         --{
1612             l_trip_name := dlvy_rec.name;
1613             l_cnt       := l_cnt + 1;
1614             --
1615             --
1616             IF l_debug_on THEN
1617                wsh_debug_sv.log (l_module_name,'dlvy_rec.delivery_id',dlvy_rec.delivery_id);
1618                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_UTIL_CORE.GET_CACHED_VALUE-l_deliveryIdTbl',WSH_DEBUG_SV.C_PROC_LEVEL);
1619             END IF;
1620             --
1621             -- Build a cache of unique delivery IDs
1622             --
1623             wsh_util_core.get_cached_value
1624               (
1625                 p_cache_tbl         => l_deliveryIdTbl,
1626                 p_cache_ext_tbl     => l_deliveryIdExtTbl,
1627                 p_key               => dlvy_rec.delivery_id,
1628                 p_value             => dlvy_rec.delivery_id,
1629                 p_action            => 'PUT',
1630                 x_return_status     => l_return_status
1631               );
1632             --
1633             --
1634             IF l_debug_on THEN
1635                WSH_DEBUG_SV.log(l_module_name,'l_return_status',l_return_status);
1636                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_UTIL_CORE.API_POST_CALL',WSH_DEBUG_SV.C_PROC_LEVEL);
1637             END IF;
1638             --
1639             wsh_util_core.api_post_call
1640              (
1641                p_return_status => l_return_status,
1642                x_num_warnings  => l_num_warnings,
1643                x_num_errors    => l_num_errors
1644              );
1645         --}
1646         END LOOP;
1647         --
1648         l_totalCnt := l_totalCnt + l_cnt;
1649         --
1650         IF l_cnt = 0
1651         --AND l_trip_name IS NOT NULL
1652         THEN
1653         --{
1654             --
1655             -- Trip does not have any delivery with initial pickup location
1656             -- Put a warning message for the trip
1657             --
1658             --
1659             IF p_rec_attr_tab(l_index).trip_id IS NULL
1660             THEN
1661                 FND_MESSAGE.SET_NAME('WSH','WSH_OI_INVALID_TRIP_NAME');
1662             ELSE
1663                 FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_NO_PICKUP_ERROR');
1664                 --FND_MESSAGE.SET_TOKEN('TRIP_NAME',NVL(l_trip_name,p_rec_attr_tab(l_index).trip_id));
1665                 FND_MESSAGE.SET_TOKEN('TRIP_NAME',NVL(l_trip_name, wsh_trips_pvt.get_name( p_rec_attr_tab(l_index).trip_id)));
1666             END IF;
1667             --
1668             wsh_util_core.add_message(wsh_util_core.g_ret_sts_warning,l_module_name);
1669                l_num_warnings := NVL(l_num_warnings,0) + 1;
1670         --}
1671         END IF;
1672         --
1673         l_index := p_rec_attr_tab.NEXT(l_index);
1674     --}
1675     END LOOP;
1676     --
1677     --
1678     IF l_debug_on THEN
1679         wsh_debug_sv.log (l_module_name,'l_totalCnt',l_totalCnt);
1680     END IF;
1681     --
1682     --
1683     -- None of the trips have any delivery with initial pickup location
1684     -- Return with error.
1685     --
1686     IF l_totalCnt = 0
1687     THEN
1688     --{
1689         RAISE FND_API.G_EXC_ERROR;
1690     --}
1691     ELSE
1692     --{
1693         l_cnt := 0;
1694         --
1695         -- Convert Delivery ID cache into a contiguous table.
1696         --
1697         l_index := l_deliveryIdTbl.FIRST;
1698         --
1699         WHILE l_index IS NOT NULL
1700         LOOP
1701         --{
1702             l_cnt := l_cnt + 1;
1703             l_delivery_id_tab(l_cnt) := l_deliveryIdTbl(l_index).value;
1704             --
1705             l_index := l_deliveryIdTbl.NEXT(l_index);
1706         --}
1707         END LOOP;
1708         --
1709         --
1710         l_index := l_deliveryIdExtTbl.FIRST;
1711         --
1712         WHILE l_index IS NOT NULL
1713         LOOP
1714         --{
1715             l_cnt := l_cnt + 1;
1716             l_delivery_id_tab(l_cnt) := l_deliveryIdExtTbl(l_index).value;
1717             --
1718             l_index := l_deliveryIdExtTbl.NEXT(l_index);
1719         --}
1720         END LOOP;
1721         --
1722         --
1723         IF l_debug_on THEN
1724             WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit wsh_interface_grp.Delivery_Action',WSH_DEBUG_SV.C_PROC_LEVEL);
1725         END IF;
1726         --
1727         l_action_prms.caller        := p_action_prms.caller;
1728         l_action_prms.phase         := p_action_prms.phase;
1729         l_action_prms.action_code   := p_action_prms.action_code;
1730         --
1731         -- Call Delivery group API to generate routing response
1732         --
1733         wsh_interface_grp.Delivery_Action(
1734               p_api_version_number     =>  1.0,
1735               p_init_msg_list          =>  FND_API.G_FALSE,
1736               p_commit                 =>  FND_API.G_FALSE,
1737               p_action_prms            =>  l_action_prms,
1738               p_delivery_id_tab        =>  l_delivery_id_tab,
1739               x_delivery_out_rec       =>  l_del_action_out_rec,
1740               x_return_status          =>  l_return_status,
1741               x_msg_count              =>  l_msg_count,
1742               x_msg_data               =>  l_msg_data);
1743         --
1744         --
1745         IF l_debug_on THEN
1746                 wsh_debug_sv.log(l_module_name,'Return Status After Calling generate_routing_response',l_return_status);
1747         END IF;
1748         --
1749         wsh_util_core.api_post_call(
1750             p_return_status    => l_return_status,
1751             x_num_warnings     => l_num_warnings,
1752             x_num_errors       => l_num_errors,
1753             p_msg_Data         => l_msg_data);
1754     --}
1755     END IF;
1756     --
1757     --
1758     IF l_debug_on THEN
1759         WSH_DEBUG_SV.logmsg(l_module_name,
1760                             'Number of Errors='||l_num_errors||',Number of Warnings='||l_num_warnings);
1761     END IF;
1762     --
1763     IF l_num_errors > 0
1764     THEN
1765         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
1766     ELSIF l_num_warnings > 0
1767     THEN
1768         x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
1769     ELSE
1770         x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
1771     END IF;
1772 --
1773 -- Debug Statements
1774 --
1775 IF l_debug_on THEN
1776     WSH_DEBUG_SV.pop(l_module_name);
1777 END IF;
1778 --
1779 --}
1780 EXCEPTION
1781 --{
1782     WHEN FND_API.G_EXC_ERROR THEN
1783       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
1784       --
1785       -- Debug Statements
1786       --
1787       IF l_debug_on THEN
1788           WSH_DEBUG_SV.logmsg(l_module_name,'FND_API.G_EXC_ERROR exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
1789           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:FND_API.G_EXC_ERROR');
1790       END IF;
1791       --
1792     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1793       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR ;
1794       --
1795       -- Debug Statements
1796       --
1797       IF l_debug_on THEN
1798           WSH_DEBUG_SV.logmsg(l_module_name,'FND_API.G_EXC_UNEXPECTED_ERROR exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
1799           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:FND_API.G_EXC_UNEXPECTED_ERROR');
1800       END IF;
1801       --
1802     WHEN WSH_UTIL_CORE.G_EXC_WARNING THEN
1803       x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
1804       --
1805       -- Debug Statements
1806       --
1807       IF l_debug_on THEN
1808           WSH_DEBUG_SV.logmsg(l_module_name,'WSH_UTIL_CORE.G_EXC_WARNING exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
1809           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:WSH_UTIL_CORE.G_EXC_WARNING');
1810       END IF;
1811       --
1812     WHEN OTHERS THEN
1813       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR ;
1814       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.generateRoutingResponse');
1815 --
1816 -- Debug Statements
1817 --
1818 IF l_debug_on THEN
1819     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
1820     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
1821 END IF;
1822 --
1823 --}
1824 END generateRoutingResponse;
1825 --
1826 --
1827 -- J-IB-NPARIKH-}
1828 
1829 
1830 /* Bug 4037457 */
1831 --========================================================================
1832 -- PROCEDURE : get_next_del_rows
1833 --
1834 -- PARAMETERS: p_del_tbl   Is a table of record for delivery lines
1835 --             x_idx       This parameter indicates the index to
1836 --                         table p_del_tbl, as a starting index.  It is
1837 --                         an in/out parameter so it will be remembered
1838 --                         by the calling API
1839 --             x_del_rows  is a table of deliver_ids that can be grouped
1840 --                         together.
1841 --
1842 --
1843 -- COMMENT   : This procedure is called from autocreate_trip_wrp.  It returns
1844 --             a table of deliveries that can be put together in a trip.  If
1845 --             IF FTE is not installed then deliveries that contain same
1846 --             mode of transport, service level and carrier are put
1847 --             together.
1848 --             IF FTE is installed in addition to the grouping above
1849 --             if deliveries with mode_of_transport other than truck
1850 --             have a different pick up or drop off then they will put
1851 --             in a different trip
1852 --             Once there are no deliveries to process parameter
1853 --             x_idx is set to NULL.
1854 --
1855 --========================================================================
1856 PROCEDURE get_next_del_rows( p_del_tbl  IN Del_Rec_Type,
1857                              x_idx      IN OUT NOCOPY NUMBER,
1858                              x_del_rows OUT NOCOPY wsh_util_core.id_tab_type,
1859                              x_return_status OUT NOCOPY VARCHAR) IS
1860 
1861 --
1862    j NUMBER;
1863    l_fte_flag VARCHAR2(2) := 'N';
1864 
1865    -- the following 5 variables are defined as varchar2(30) since they
1866    -- will get their value from wsh_tmp table which has varchar2 columns
1867 
1868    l_pickup_location_id VARCHAR2(30);
1869    l_dropoff_location_id VARCHAR2(30);
1870    l_MODE_OF_TRANSPORT     VARCHAR2(30);
1871    l_service_level     VARCHAR2(30);
1872    l_carrier_id     VARCHAR2(30);
1873    --
1874    --
1875    l_delivery_id    NUMBER;
1876    l_counter NUMBER;
1877    l_debug_on BOOLEAN;
1878    l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'GET_NEXT_DEL_ROWS';
1879 --
1880 BEGIN
1881    --
1882    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
1883    --
1884    IF l_debug_on IS NULL
1885    THEN
1886        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
1887    END IF;
1888    --
1889    IF l_debug_on THEN
1890      wsh_debug_sv.push(l_module_name);
1891      wsh_debug_sv.log(l_module_name, 'p_del_tbl.COUNT', p_del_tbl.delivery_id.COUNT);
1892    END IF;
1893    --
1894 
1895    IF (WSH_UTIL_CORE.FTE_IS_INSTALLED = 'Y') THEN
1896      l_fte_flag := 'Y';
1897    END IF;
1898 
1899    --If the x_idx is greater than the delivery_id.COUNT then
1900    -- return NULL to calling API to show that there is no more
1901    -- rows to process.
1902 
1903    IF x_idx > p_del_tbl.delivery_id.COUNT THEN
1904       x_idx := NULL;
1905    ELSE --{
1906       j := x_idx;
1907       l_delivery_id := p_del_tbl.delivery_id(j);
1908       l_pickup_location_id := p_del_tbl.INITIAL_PICKUP_LOCATION_ID(j);
1909       l_dropoff_location_id := p_del_tbl.ULTIMATE_DROPOFF_LOCATION_ID(j);
1910       l_MODE_OF_TRANSPORT := p_del_tbl.MODE_OF_TRANSPORT(j);
1911       l_service_level := p_del_tbl.service_level(j);
1912       l_carrier_id := p_del_tbl.carrier_id(j);
1913 
1914       l_counter := 1;
1915       FOR i IN j..p_del_tbl.delivery_id.COUNT LOOP --{
1916          --
1917          -- IF any of 3 components of Shipmethod changes, create a
1918          -- separate trip.
1919          --
1920          IF (NVL(p_del_tbl.MODE_OF_TRANSPORT(i),'~') <>
1921                   NVL(l_MODE_OF_TRANSPORT,'~'))
1922            OR
1923             (NVL(p_del_tbl.service_level(i),'~') <>
1924                   NVL(l_service_level,'~'))
1925            OR
1926             (NVL(p_del_tbl.carrier_id(i),'~') <>
1927                   NVL(l_carrier_id,'~'))
1928          THEN --{
1929             IF l_debug_on THEN
1930               wsh_debug_sv.log(l_module_name, 'SM l_delivery_id', l_delivery_id);
1931               wsh_debug_sv.log(l_module_name, 'p_del_tbl.delivery_id', p_del_tbl.delivery_id(i));
1932             END IF;
1933             EXIT;
1934          END IF; --}
1935 
1936          -- IF FTE is installed and the mode of transport is not
1937          -- Truck, then only deliveries with the same pickup, drop off
1938          -- should be grouped in one trip
1939          --
1940          IF  ( l_fte_flag = 'Y'
1941            AND NVL(p_del_tbl.MODE_OF_TRANSPORT(i),'~')  <> 'TRUCK'
1942            AND (l_dropoff_location_id <>
1943                    p_del_tbl.ULTIMATE_DROPOFF_LOCATION_ID(i)
1944                 OR
1945                 l_pickup_location_id <>
1946                 p_del_tbl.INITIAL_PICKUP_LOCATION_ID(i)
1947                )
1948              )
1949          THEN
1950             IF l_debug_on THEN
1951               wsh_debug_sv.log(l_module_name, 'SF/ST l_delivery_id', l_delivery_id);
1952               wsh_debug_sv.log(l_module_name, 'p_del_tbl.delivery_id', p_del_tbl.delivery_id(i));
1953             END IF;
1954             EXIT;
1955          END IF;
1956 
1957          x_idx := x_idx + 1;
1958          x_del_rows(l_counter) := p_del_tbl.delivery_id(i);
1959          IF l_debug_on THEN
1960            wsh_debug_sv.log(l_module_name, 'p_del_tbl.delivery_id(i)',
1961                                                p_del_tbl.delivery_id(i));
1962          END IF;
1963          l_counter := l_counter + 1;
1964 
1965       END LOOP; --}
1966    END IF; --}
1967    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
1968 
1969    IF l_debug_on THEN
1970       wsh_debug_sv.log(l_module_name, 'x_idx', x_idx);
1971       WSH_DEBUG_SV.pop(l_module_name);
1972    END IF;
1973    --
1974    EXCEPTION
1975      --
1976      WHEN others THEN
1977       --
1978       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.GET_NEXT_DEL_ROWS');
1979       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
1980       --
1981       IF l_debug_on THEN
1982         WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
1983         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
1984       END IF;
1985       --
1986 END get_next_del_rows;
1987 
1988 
1989 /* Bug 4037457 */
1990 --========================================================================
1991 -- PROCEDURE : sort_del
1992 --
1993 -- PARAMETERS: p_del_tbl record of table of deliveries that need to be
1994 --                       sorted.
1995 --             x_del_tbl This is the p_del_tbl sorted by the criteria
1996 --                        mentioned in the comments.
1997 --
1998 --
1999 -- COMMENT   : This procedure is called from autocreate_trip_wrp.  It sorts
2000 --             the deliveries based on the p_del_tbl.MODE_OF_TRANSPORT,
2001 --             service_level, carrier_id, pickup and then drop-off
2002 --             locatio_id
2003 --
2004 --========================================================================
2005 PROCEDURE sort_del( p_del_tbl IN Del_Rec_Type,
2006                     x_del_tbl OUT NOCOPY Del_Rec_Type,
2007                     x_return_status OUT NOCOPY VARCHAR) IS
2008 
2009 --
2010    CURSOR c_sort_del IS
2011    SELECT id,
2012           column1,
2013           column2,
2014           column3,
2015           column4,
2016           column5
2017    FROM WSH_TMP
2018    WHERE flag = '~'
2019    order by column1, column2, column3, column4, column5;
2020 
2021    e_invalid_count  EXCEPTION;
2022 
2023    l_debug_on BOOLEAN;
2024    l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'SORT_DEL';
2025 --
2026 BEGIN
2027    --
2028    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
2029    --
2030    IF l_debug_on IS NULL
2031    THEN
2032        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
2033    END IF;
2034    --
2035    IF l_debug_on THEN
2036      wsh_debug_sv.push(l_module_name);
2037      wsh_debug_sv.log(l_module_name, 'p_del_tbl.delivery_id.COUNT', p_del_tbl.delivery_id.COUNT);
2038    END IF;
2039    --
2040    IF p_del_tbl.delivery_id.COUNT < 1 THEN
2041       RAISE e_invalid_count;
2042    END IF;
2043 
2044    DELETE FROM wsh_tmp where flag = '~';
2045 
2046 
2047    FORALL i IN 1..p_del_tbl.delivery_id.count
2048     INSERT INTO wsh_tmp (id,
2049                          flag,
2050                          column1,
2051                          column2,
2052                          column3,
2053                          column4,
2054                          column5 )
2055                    VALUES(p_del_tbl.delivery_id(i),
2056                          '~',
2057                          p_del_tbl.MODE_OF_TRANSPORT(i),
2058                          p_del_tbl.service_level(i),
2059                          p_del_tbl.carrier_id(i),
2060                          p_del_tbl.INITIAL_PICKUP_LOCATION_ID(i),
2061                          p_del_tbl.ULTIMATE_DROPOFF_LOCATION_ID(i)
2062                    );
2063 
2064 
2065    OPEN c_sort_del;
2066    FETCH c_sort_del BULK COLLECT INTO x_del_tbl.delivery_id,
2067                          x_del_tbl.MODE_OF_TRANSPORT,
2068                          x_del_tbl.service_level,
2069                          x_del_tbl.carrier_id,
2070                          x_del_tbl.INITIAL_PICKUP_LOCATION_ID,
2071                          x_del_tbl.ULTIMATE_DROPOFF_LOCATION_ID;
2072 
2073    -- the column sequence determines the sorting
2074 
2075    CLOSE c_sort_del;
2076 
2077    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
2078 
2079    IF l_debug_on THEN
2080       wsh_debug_sv.log(l_module_name, 'x_del_tbl.COUNT', x_del_tbl.delivery_id.COUNT);
2081       WSH_DEBUG_SV.pop(l_module_name);
2082    END IF;
2083    --
2084    EXCEPTION
2085      --
2086      WHEN e_invalid_count THEN
2087       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
2088       --
2089       IF l_debug_on THEN
2090         WSH_DEBUG_SV.logmsg(l_module_name,'The input table is empty.' ,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
2091         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:e_invalid_count');
2092       END IF;
2093       --
2094      WHEN others THEN
2095       --
2096       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.SORT_DEL');
2097       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
2098       --
2099       IF l_debug_on THEN
2100         WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
2101         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
2102       END IF;
2103       --
2104 END sort_del;
2105 
2106 /* Bug 4037457 */
2107 --========================================================================
2108 -- PROCEDURE : autocreate_trip_wrp
2109 --
2110 -- PARAMETERS:
2111 --
2112 --
2113 -- COMMENT   : This procedure is called by autocreate_trip_multi.  It is a
2114 --             wrapper around procedure autocreate_trip.  It calls
2115 --             sort_del to sort the deliveries then it calls
2116 --             get_next_del_rows in a loop to get the group of
2117 --             deliveries which can be sent in one trip.  It exists the
2118 --             loop, once there are no more deliveries to process.
2119 --
2120 --========================================================================
2121 
2122 PROCEDURE autocreate_trip_wrp(
2123          p_del_rows   IN    wsh_util_core.id_tab_type,
2124          p_entity     IN    VARCHAR2,
2125          x_trip_ids    OUT NOCOPY    wsh_util_core.id_tab_type,
2126          x_trip_names  OUT NOCOPY    wsh_util_core.Column_Tab_Type,
2127          x_return_status OUT NOCOPY  VARCHAR2,
2128          p_sc_pickup_date         IN      DATE   DEFAULT NULL,
2129          p_sc_dropoff_date        IN      DATE   DEFAULT NULL) IS
2130 
2131 --
2132    l_idx number;
2133 
2134    CURSOR c_get_del_detail (v_delivery_id number) IS
2135       SELECT delivery_id,
2136              INITIAL_PICKUP_LOCATION_ID,
2137              ULTIMATE_DROPOFF_LOCATION_ID,
2138              MODE_OF_TRANSPORT,
2139              service_level,
2140              carrier_id
2141       FROM wsh_new_deliveries
2142       WHERE delivery_id = v_delivery_id;
2143 
2144    l_del_tbl Del_Rec_Type;
2145    l_del_tbl_not_sorted Del_Rec_Type;
2146    l_return_status varchar2(1);
2147    l_del_rows  wsh_util_core.id_tab_type;
2148    l_trip_id   NUMBER;
2149    l_trip_name  wsh_trips.name%TYPE;
2150    l_initial_pickup_location_id  NUMBER;
2151    l_ultimate_dropoff_location_id  NUMBER;
2152    l_num_warnings        NUMBER := 0;
2153    l_num_errors          NUMBER := 0;
2154    l_api_calls           NUMBER := 0;
2155    i                     NUMBER;
2156 
2157    l_debug_on BOOLEAN;
2158    l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'AUTOCREATE_TRIP_WRP';
2159 --
2160 BEGIN
2161    --
2162    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
2163    --
2164    IF l_debug_on IS NULL
2165    THEN
2166        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
2167    END IF;
2168    --
2169    IF l_debug_on THEN
2170      wsh_debug_sv.push(l_module_name);
2171      wsh_debug_sv.log(l_module_name, 'p_entity', p_entity);
2172      wsh_debug_sv.log(l_module_name, 'p_del_rows.COUNT', p_del_rows.COUNT);
2173      wsh_debug_sv.log(l_module_name, 'p_sc_pickup_date', p_sc_pickup_date);
2174      wsh_debug_sv.log(l_module_name, 'p_sc_dropoff_date', p_sc_dropoff_date);
2175      wsh_debug_sv.log(l_module_name, 'p_del_rows.count', p_del_rows.count);
2176    END IF;
2177    --
2178    l_idx := p_del_rows.FIRST;
2179 
2180    WHILE l_idx IS NOT NULL LOOP --{
2181 
2182       OPEN c_get_del_detail(p_del_rows(l_idx));
2183       FETCH c_get_del_detail INTO
2184       l_del_tbl_not_sorted.delivery_id(l_idx),
2185       l_del_tbl_not_sorted.INITIAL_PICKUP_LOCATION_ID(l_idx),
2186       l_del_tbl_not_sorted.ULTIMATE_DROPOFF_LOCATION_ID(l_idx),
2187       l_del_tbl_not_sorted.MODE_OF_TRANSPORT(l_idx),
2188       l_del_tbl_not_sorted.service_level(l_idx),
2189       l_del_tbl_not_sorted.carrier_id(l_idx) ;
2190       CLOSE c_get_del_detail;
2191 
2192       l_idx := p_del_rows.NEXT(l_idx);
2193 
2194    END LOOP; --}
2195    --
2196    -- If some delivereis contain mode of transport ('LTL' or "PARCEL')
2197    -- and have different pickup and drop-off locations then group these
2198    -- deliveries and create several trips for them.
2199 
2200    sort_del(l_del_tbl_not_sorted,l_del_tbl, l_return_status );
2201    wsh_util_core.api_post_call(
2202            p_return_status => l_return_status,
2203            x_num_warnings  => l_num_warnings,
2204            x_num_errors    => l_num_errors
2205    );
2206    l_idx := l_del_tbl.delivery_id.FIRST;
2207 
2208    WHILE l_idx IS NOT NULL LOOP --{
2209         --
2210         -- get the deliveries that can be grouped in one trip
2211         --
2212         get_next_del_rows( p_del_tbl       => l_del_tbl,
2213                            x_idx           => l_idx,
2214                            x_del_rows      => l_del_rows,
2215                            x_return_status => l_return_status);
2216          wsh_util_core.api_post_call(
2217              p_return_status => l_return_status,
2218              x_num_warnings  => l_num_warnings,
2219              x_num_errors    => l_num_errors
2220          );
2221 
2222         autocreate_trip(
2223            p_del_rows   => l_del_rows,
2224            p_entity     => p_entity,
2225            x_trip_id    => l_trip_id,
2226            x_trip_name  => l_trip_name,
2227            x_return_status => l_return_status,
2228            p_sc_pickup_date => p_sc_pickup_date,
2229            p_sc_dropoff_date => p_sc_dropoff_date);
2230 
2231         IF l_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR THEN
2232             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2233         END IF;
2234         wsh_util_core.api_post_call(
2235              p_return_status => l_return_status,
2236              x_num_warnings  => l_num_warnings,
2237              x_num_errors    => l_num_errors,
2238              p_raise_error_flag => FALSE
2239         );
2240         l_api_calls := l_api_calls + 1;
2241         l_del_rows.DELETE;
2242         x_trip_names(x_trip_names.count + 1) := l_trip_name;
2243         x_trip_ids(x_trip_ids.count + 1) := l_trip_id;
2244 
2245   END LOOP; --}
2246 
2247 
2248    IF l_num_errors = 0 AND l_num_warnings = 0 THEN
2249       x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
2250    ELSIF l_num_errors = l_api_calls THEN
2251       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2252    ELSE
2253       x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING ;
2254    END IF;
2255 
2256    IF l_debug_on THEN
2257       WSH_DEBUG_SV.pop(l_module_name);
2258    END IF;
2259    --
2260    EXCEPTION
2261      --
2262      WHEN others THEN
2263       --
2264       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP_WRP');
2265       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
2266       --
2267       IF l_debug_on THEN
2268         WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
2269         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
2270       END IF;
2271       --
2272 END autocreate_trip_wrp;
2273 
2274 /* J TP Release */
2275 --
2276 -- Procedure:   Autocreate_Trip_multi
2277 --              New procedure which gets the ignore_for_planning flag
2278 --              and groups them on the basis of that.
2279 PROCEDURE autocreate_trip_multi(
2280          p_del_rows   IN    wsh_util_core.id_tab_type,
2281          p_entity     IN    VARCHAR2,
2282          x_trip_ids    OUT NOCOPY    wsh_util_core.id_tab_type,
2283          x_trip_names  OUT NOCOPY    wsh_util_core.Column_Tab_Type,
2284          x_return_status OUT NOCOPY  VARCHAR2) IS
2285 
2286 --OTM R12, glog proj
2287 -- When OTM is installed and user performs autocreate Trip action
2288 -- the trip would be created as Ignore for Planning(based on the delivery)
2289 -- Autocreate trip action is not allowed on an Include for Planning Delivery
2290 --
2291 -- When OTM is not installed, the behavior is that Trips are
2292 -- created as Include/Ignore for Planning based on the delivery
2293 -- and Include for Planning if null value at delivery
2294 CURSOR c_get_del_ignoreplan (p_delid IN NUMBER,p_ignore_flag IN VARCHAR2) IS
2295 SELECT NVL(ignore_for_planning,p_ignore_flag) ignore_for_planning
2296 FROM wsh_new_deliveries
2297 WHERE delivery_id=p_delid;
2298 
2299 l_ignore_ids        wsh_util_core.id_tab_type;
2300 l_include_ids       wsh_util_core.id_tab_type;
2301 l_return_status     VARCHAR2(1);
2302 l_trip_id           NUMBER;
2303 l_trip_name         wsh_trips.name%TYPE;
2304 l_index             NUMBER;
2305 l_trip_ids          wsh_util_core.id_tab_type;
2306 l_trip_names        wsh_util_core.Column_Tab_Type;
2307 l_gc3_is_installed  VARCHAR2(1); -- OTM R12, glog proj
2308 l_ignore_flag       WSH_TRIPS.IGNORE_FOR_PLANNING%TYPE;  -- OTM R12, glog proj
2309 l_count_error       NUMBER;
2310 
2311 --
2312 l_debug_on BOOLEAN;
2313 --
2314 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'AUTOCREATE_TRIP_multi';
2315 --
2316 BEGIN
2317    --
2318    -- Debug Statements
2319    --
2320    --
2321    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
2322    --
2323    IF l_debug_on IS NULL
2324    THEN
2325        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
2326    END IF;
2327    --
2328    IF l_debug_on THEN
2329      WSH_DEBUG_SV.push(l_module_name);
2330      wsh_debug_sv.log(l_module_name, 'p_entity', p_entity);
2331      wsh_debug_sv.log(l_module_name, 'p_del_rows.COUNT', p_del_rows.COUNT);
2332    END IF;
2333    --
2334    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
2335 
2336    --OTM R12, glog proj, use Global Variable
2337    l_gc3_is_installed := WSH_UTIL_CORE.G_GC3_IS_INSTALLED;
2338 
2339    -- If null, call the function
2340    IF l_gc3_is_installed IS NULL THEN
2341      l_gc3_is_installed := WSH_UTIL_CORE.GC3_IS_INSTALLED;
2342    END IF;
2343 
2344    IF l_gc3_is_installed = 'Y' THEN
2345      l_ignore_flag := 'Y';
2346    ELSE
2347      l_ignore_flag := 'N';
2348    END IF;
2349    -- end of OTM R12, glog proj
2350 
2351 
2352    -- OTM R12, glog project
2353    -- If gc3 is installed, sort Include and Ignore lines/deliveries
2354    IF (WSH_UTIL_CORE.TP_IS_INSTALLED = 'N') AND (l_gc3_is_installed = 'N') THEN
2355       --just call the core api
2356       autocreate_trip_wrp(
2357          p_del_rows      => p_del_rows,
2358          p_entity        => p_entity,
2359          x_trip_ids      => x_trip_ids,
2360          x_trip_names    => x_trip_names,
2361          x_return_status => x_return_status);
2362    ELSE
2363       l_index:=p_del_rows.FIRST;
2364       l_count_error := 0;
2365       WHILE l_index IS NOT NULL LOOP
2366         FOR cur in c_get_del_ignoreplan(p_del_rows(l_index),l_ignore_flag) LOOP
2367            IF cur.ignore_for_planning='Y' THEN
2368               l_ignore_ids(l_ignore_ids.COUNT+1):=p_del_rows(l_index);
2369            ELSE
2370              --OTM R12, glog proj
2371              -- only select the ignore_for_planning delivery for autocreate trip
2372              -- for include for planning delivery, give a error message and skip record
2373              -- The Code flow should not come to this branch, as TP installed would not
2374              -- be Yes, when GC3 is installed.
2375              IF l_gc3_is_installed = 'Y' THEN
2376                -- Raise a new generic error message here, just trip creation will not happen.
2377                ----- skip this delivery and not add to the list
2378                l_count_error := l_count_error + 1;
2379              ELSE
2380                l_include_ids(l_include_ids.COUNT+1):=p_del_rows(l_index);
2381              END IF;
2382              --
2383            END IF;
2384         END LOOP;
2385         l_index:=p_del_rows.NEXT(l_index);
2386       END LOOP;
2387 
2388       -- OTM R12, glog proj, count the number above and display a single error message
2389       -- as oppose to showing multiple error messages
2390       -- The process will continue, as the message will come up as a Information Note
2391       -- Only filtered deliveries will be considered for further processing
2392       IF l_count_error > 0 THEN
2393         FND_MESSAGE.SET_NAME('WSH','WSH_OTM_CR_TRIP_SUMMARY');
2394         IF l_count_error = p_del_rows.count THEN
2395           WSH_UTIL_CORE.ADD_MESSAGE(WSH_UTIL_CORE.G_RET_STS_ERROR);
2396           l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2397           x_return_status := l_return_status;
2398           IF l_debug_on THEN
2399             WSH_DEBUG_SV.log(l_module_name, 'Count of Errors', l_count_error);
2400             WSH_DEBUG_SV.pop(l_module_name);
2401           END IF;
2402           RETURN;
2403         ELSE
2404           WSH_UTIL_CORE.ADD_MESSAGE(WSH_UTIL_CORE.G_RET_STS_WARNING);
2405           l_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
2406           x_return_status := l_return_status;
2407         END IF;
2408       END IF;
2409 
2410       --call autocreate_trip_core separately for these 2 ids
2411       IF l_ignore_ids is not null and l_ignore_ids.COUNT>0 THEN
2412          autocreate_trip_wrp(
2413            p_del_rows      => l_ignore_ids,
2414            p_entity        => p_entity,
2415            x_trip_ids      => l_trip_ids,
2416            x_trip_names    => l_trip_names,
2417            x_return_status => l_return_status);
2418 
2419          IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
2420            x_return_status:=l_return_status;
2421            IF l_debug_on THEN
2422               WSH_DEBUG_SV.pop(l_module_name);
2423            END IF;
2424            RETURN;
2425          END IF;
2426          IF l_trip_ids.COUNT > 0 THEN
2427            FOR i in l_trip_ids.FIRST..l_trip_ids.LAST LOOP
2428               x_trip_ids(x_trip_ids.COUNT+1):=l_trip_ids(i);
2429               x_trip_names(x_trip_names.COUNT+1):=l_trip_names(i);
2430            END LOOP;
2431          END IF;
2432       END IF;
2433 
2434       IF l_include_ids is not null and l_include_ids.COUNT>0 THEN
2435          l_trip_ids.DELETE;
2436          l_trip_names.DELETE;
2437          autocreate_trip_wrp(
2438            p_del_rows      => l_include_ids,
2439            p_entity        => p_entity,
2440            x_trip_ids      => l_trip_ids,
2441            x_trip_names    => l_trip_names,
2442            x_return_status => l_return_status);
2443 
2444 
2445          IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
2446            x_return_status:=l_return_status;  -- Bug#3884302
2447            IF l_debug_on THEN
2448               WSH_DEBUG_SV.pop(l_module_name);
2449            END IF;
2450            RETURN;
2451          END IF;
2452          IF l_trip_ids.COUNT > 0 THEN
2453            FOR i in l_trip_ids.FIRST..l_trip_ids.LAST LOOP
2454               x_trip_ids(x_trip_ids.COUNT+1):=l_trip_ids(i);
2455               x_trip_names(x_trip_names.COUNT+1):=l_trip_names(i);
2456            END LOOP;
2457          END IF;
2458       END IF;
2459 
2460       IF l_return_status=WSH_UTIL_CORE.G_RET_STS_WARNING THEN
2461          x_return_status:=WSH_UTIL_CORE.G_RET_STS_WARNING;
2462       END IF;
2463 
2464    END IF; --tp_is_installed
2465 
2466    --
2467    -- Debug Statements
2468    --
2469    IF l_debug_on THEN
2470      WSH_DEBUG_SV.pop(l_module_name);
2471    END IF;
2472    --
2473 
2474    EXCEPTION
2475      WHEN others THEN
2476       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP_MULTI');
2477       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
2478 
2479       --
2480       -- Debug Statements
2481       --
2482       IF l_debug_on THEN
2483         WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
2484         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
2485       END IF;
2486       --
2487 END autocreate_trip_multi;
2488 
2489 
2490 PROCEDURE autocreate_trip(
2491          p_del_rows   IN    wsh_util_core.id_tab_type,
2492          p_entity     IN    VARCHAR2,
2493          x_trip_id    OUT NOCOPY    NUMBER,
2494          x_trip_name  OUT NOCOPY    VARCHAR2,
2495          x_return_status OUT NOCOPY  VARCHAR2,
2496          p_sc_pickup_date         IN      DATE   DEFAULT NULL,
2497          p_sc_dropoff_date        IN      DATE   DEFAULT NULL) IS
2498 
2499 CURSOR check_assigned(p_del_id IN NUMBER) IS
2500 SELECT delivery_id
2501 FROM   wsh_delivery_legs
2502 WHERE  delivery_id = p_del_id;
2503 
2504 -- When OTM is installed and user performs autocreate Trip action
2505 -- the trip would be created as Ignore for Planning(based on the delivery)
2506 -- Autocreate trip action is not allowed on an Include for Planning Delivery
2507 --
2508 -- When OTM is not installed, the behavior is that Trips are
2509 -- created as Include/Ignore for Planning based on the delivery
2510 -- and Include for Planning if null value at delivery
2511 CURSOR check_ship_method(p_del_id IN NUMBER,p_ignore_flag IN VARCHAR2) IS
2512 SELECT ship_method_code, carrier_id, mode_of_transport, service_level,
2513        status_code,
2514        --OTM R12, glog proj
2515        NVL(ignore_for_planning,p_ignore_flag),
2516        initial_pickup_date, ultimate_dropoff_date , nvl(shipment_direction,'O')
2517 FROM   wsh_new_deliveries
2518 WHERE  delivery_id = p_del_id;
2519 
2520 l_shipment_direction VARCHAR2(100);
2521 
2522 CURSOR trip_stops(p_trip_id IN NUMBER) IS
2523 SELECT stop_id,
2524      planned_departure_date,
2525      planned_arrival_date,
2526      NVL(shipments_type_flag,'O') shipments_type_flag  --J-IB-NPARIKH
2527 FROM   wsh_trip_stops
2528 WHERE  trip_id = p_trip_id
2529 ORDER BY stop_sequence_number;   -- J-IB-NPARIKH
2530 
2531 l_delivery_id         NUMBER;
2532 l_trip_id             NUMBER;
2533 
2534 l_ship_method         wsh_trips.ship_method_code%TYPE;
2535 l_ship_method_old     wsh_trips.ship_method_code%TYPE;
2536 l_carrier_id          wsh_trips.carrier_id%TYPE;
2537 l_mode                wsh_trips.mode_of_transport%TYPE;
2538 l_service_level       wsh_trips.service_level%TYPE;
2539 l_carrier_id_old      wsh_trips.carrier_id%TYPE;
2540 l_mode_old            wsh_trips.mode_of_transport%TYPE;
2541 l_service_level_old   wsh_trips.service_level%TYPE;
2542 l_ignore_for_planning wsh_trips.ignore_for_planning%TYPE;
2543 
2544 l_status_code         VARCHAR2(30);
2545 
2546 l_ship_method_flag    BOOLEAN := TRUE;
2547 
2548 l_rowid               VARCHAR2(30);
2549 l_trip_info           WSH_TRIPS_PVT.TRIP_REC_TYPE;
2550 assigned_del_error    EXCEPTION;
2551 l_return_status       VARCHAR2(1);
2552 l_del_legs            WSH_UTIL_CORE.ID_TAB_TYPE;
2553 l_good_dels           WSH_UTIL_CORE.ID_TAB_TYPE;
2554 
2555 /* H integration  for Multi Leg */
2556 l_num_warn            NUMBER := 0;
2557 l_stop_rec            WSH_TRIP_STOPS_PVT.TRIP_STOP_REC_TYPE;
2558 l_pub_stop_rec        WSH_TRIP_STOPS_PUB.TRIP_STOP_PUB_REC_TYPE;
2559 l_trip_rec            WSH_TRIPS_PVT.TRIP_REC_TYPE;
2560 l_pub_trip_rec        WSH_TRIPS_PUB.TRIP_PUB_REC_TYPE;
2561 l_grp_trip_rec        WSH_TRIPS_GRP.TRIP_PUB_REC_TYPE;
2562 --l_return_status     VARCHAR2(30);
2563 l_fte_flag            VARCHAR2(1):= 'N';
2564 --
2565 l_debug_on            BOOLEAN;
2566 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'AUTOCREATE_TRIP';
2567 --
2568 l_stop_in_rec       WSH_TRIP_STOPS_VALIDATIONS.chkClose_in_rec_type;
2569 l_stop_processed    VARCHAR2(10);
2570 -- Bug 3413364, skip the delivery if it does not have both initial pickup date and ultimate dropoff date
2571 l_initial_pickup_date    DATE;
2572 l_ultimate_dropoff_date  DATE;
2573 l_num_skipped            NUMBER := 0;
2574 l_stop_tab               WSH_UTIL_CORE.id_tab_type;  -- DBI Project
2575 l_dbi_rs                 VARCHAR2(1);         -- DBI Project
2576 
2577 l_trip_ids           WSH_UTIL_CORE.ID_TAB_TYPE;
2578 l_success_trip_ids   WSH_UTIL_CORE.ID_TAB_TYPE;
2579 
2580 l_gc3_is_installed   VARCHAR2(1); -- OTM R12, glog proj
2581 l_ignore_flag        WSH_TRIPS.IGNORE_FOR_PLANNING%TYPE;  -- OTM R12, glog proj
2582 l_count_error        NUMBER;
2583 
2584 --
2585 BEGIN
2586    -- Bug 3413364, skip the delivery if it does not have both initial pickup date and ultimate dropoff date
2587    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
2588    -- Create a list of deliveries that are unassigned. All other deliveries have
2589    -- error messages set.
2590    --
2591    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
2592    --
2593    IF l_debug_on IS NULL
2594    THEN
2595        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
2596    END IF;
2597    --
2598    IF l_debug_on THEN
2599      wsh_debug_sv.push(l_module_name);
2600      wsh_debug_sv.log(l_module_name, 'p_entity', p_entity);
2601      wsh_debug_sv.log(l_module_name, 'p_del_rows.COUNT', p_del_rows.COUNT);
2602      wsh_debug_sv.log(l_module_name, 'p_sc_pickup_date', p_sc_pickup_date);
2603      wsh_debug_sv.log(l_module_name, 'p_sc_dropoff_date', p_sc_dropoff_date);
2604    END IF;
2605    --
2606 
2607    --OTM R12, glog proj, use Global Variable
2608    l_gc3_is_installed := WSH_UTIL_CORE.G_GC3_IS_INSTALLED;
2609    l_count_error := 0;
2610 
2611    -- If null, call the function
2612    IF l_gc3_is_installed IS NULL THEN
2613      l_gc3_is_installed := WSH_UTIL_CORE.GC3_IS_INSTALLED;
2614    END IF;
2615    IF l_gc3_is_installed = 'Y' THEN
2616      l_ignore_flag := 'Y';
2617    ELSE
2618      l_ignore_flag := 'N';
2619    END IF;
2620    -- end of OTM R12, glog proj
2621 
2622    FOR i IN 1..p_del_rows.count LOOP --{
2623       l_delivery_id := NULL;
2624       --
2625       OPEN check_assigned(p_del_rows(i)) ;
2626       FETCH check_assigned INTO l_delivery_id;
2627       CLOSE check_assigned;
2628       --
2629       IF (l_delivery_id IS NOT NULL) THEN --{
2630         -- Bug 3535050 : Omit the error message, if autocreate_trip
2631         -- is called from delivery detail and if the trip has already
2632         -- been created as part of the rating module.
2633         --
2634         IF (p_entity = 'D') THEN
2635          --{
2636          FND_MESSAGE.SET_NAME('WSH','WSH_DEL_AU_TRIP_ASSIGN_DEL');
2637          FND_MESSAGE.SET_TOKEN('DEL_NAME',
2638                                wsh_new_deliveries_pvt.get_name(p_del_rows(i)));
2639          wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_ERROR);
2640          l_num_skipped := l_num_skipped + 1;
2641          --}
2642         END IF;
2643       ELSE
2644          --} {
2645          -- Bug 3413364, skip the delivery if it does not have both initial pickup date and ultimate dropoff date
2646          l_ship_method := NULL;
2647          --
2648          OPEN  check_ship_method(p_del_rows(i),l_ignore_flag);
2649          FETCH check_ship_method INTO
2650                l_ship_method, l_carrier_id, l_mode,
2651                l_service_level, l_status_code, l_ignore_for_planning,
2652                l_initial_pickup_date, l_ultimate_dropoff_date, l_shipment_direction;
2653          CLOSE check_ship_method;
2654 
2655          --OTM R12, glog proj
2656          IF l_debug_on THEN
2657            WSH_DEBUG_SV.log(l_module_name,'l_gc3_is_installed',l_gc3_is_installed);
2658            WSH_DEBUG_SV.log(l_module_name,'l_ignore_for_planning',l_ignore_for_planning);
2659          END IF;
2660 
2661          IF l_gc3_is_installed = 'Y' AND l_ignore_for_planning = 'N' THEN--{
2662            -- Raise a new generic error message here, just trip creation will not happen.
2663            FND_MESSAGE.SET_NAME('WSH','WSH_OTM_CR_TRIP');
2664            WSH_UTIL_CORE.ADD_MESSAGE(WSH_UTIL_CORE.G_RET_STS_ERROR);
2665            ----- skip this delivery, handled towards the end
2666            l_num_skipped := l_num_skipped + 1;
2667            l_count_error := l_count_error + 1;
2668          ELSE--} {
2669 
2670            -- If the pick up date and drop off date are defaulted from the
2671            -- ship confirm API then use these dates. Bug 3913206
2672            --
2673            IF (p_sc_pickup_date IS NOT NULL) OR (p_sc_dropoff_date IS NOT NULL)
2674            THEN --{
2675              l_initial_pickup_date := p_sc_pickup_date;
2676              l_ultimate_dropoff_date := p_sc_dropoff_date;
2677            END IF; --}
2678            --
2679            -- Bug 3413364, skip the delivery if it does not have both initial pickup date and ultimate dropoff date
2680            IF l_debug_on THEN
2681              WSH_DEBUG_SV.log(l_module_name,'l_shipment_direction',l_shipment_direction);
2682            END IF;
2683 
2684            IF (l_initial_pickup_date is NULL or l_ultimate_dropoff_date is NULL)
2685              AND nvl(l_shipment_direction,'O') IN ('O', 'IO') THEN--{
2686              --
2687              FND_MESSAGE.SET_NAME('WSH','WSH_DEL_DATES_MISSING');
2688              FND_MESSAGE.SET_TOKEN('DEL_NAME',wsh_new_deliveries_pvt.get_name(p_del_rows(i)));
2689              wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_ERROR);
2690              l_num_skipped := l_num_skipped + 1;
2691              --
2692            ELSE
2693              --} {
2694              l_good_dels(l_good_dels.count+1) := p_del_rows(i);
2695              -- Bug 3334363, compare the ship method components
2696              -- to check if the ship method/components of the delivery
2697              -- should be defaulted to the trip.
2698              IF (l_carrier_id IS NOT NULL) THEN
2699                IF (nvl(l_carrier_id_old, l_carrier_id) = l_carrier_id) THEN
2700                  l_carrier_id_old := l_carrier_id;
2701                ELSE
2702                  l_ship_method_flag := FALSE;
2703                  EXIT;
2704                END IF;
2705              END IF;
2706              --
2707              IF (l_mode IS NOT NULL) THEN
2708                IF (nvl(l_mode_old, l_mode) = l_mode) THEN
2709                  l_mode_old := l_mode;
2710                ELSE
2711                  l_ship_method_flag := FALSE;
2712                  EXIT;
2713                END IF;
2714              END IF;
2715              --
2716              IF (l_service_level IS NOT NULL) THEN
2717                IF (nvl(l_service_level_old, l_service_level) = l_service_level) THEN
2718                  l_service_level_old := l_service_level;
2719                ELSE
2720                  l_ship_method_flag := FALSE;
2721                  EXIT;
2722                END IF;
2723              END IF;
2724              --
2725 
2726              IF l_ship_method_old IS NULL THEN
2727                l_ship_method_old :=  l_ship_method;
2728              END IF;
2729            END IF;   --}   -- for null dates
2730          END IF;--} for OTM 12, glog proj,
2731      END IF;--} -- delivery_id is null
2732    END LOOP;--}
2733 
2734    -- OTM R12, glog proj, count the number above and display a single error message
2735    -- as oppose to showing multiple error messages
2736    -- The process will continue, as the message will come up as a Information Note
2737    -- Only filtered deliveries will be considered for further processing
2738    -- l_num_skipped is used towards the end to determine the return status out of this
2739    -- API - Error or Warning
2740    IF l_count_error > 0 THEN
2741      FND_MESSAGE.SET_NAME('WSH','WSH_OTM_CR_TRIP_SUMMARY');
2742      IF l_count_error = p_del_rows.count THEN
2743        x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2744        WSH_UTIL_CORE.ADD_MESSAGE(WSH_UTIL_CORE.G_RET_STS_ERROR);
2745        IF l_debug_on THEN
2746          WSH_DEBUG_SV.log(l_module_name, 'Count of Errors', l_count_error);
2747          WSH_DEBUG_SV.pop(l_module_name);
2748        END IF;
2749        RETURN;
2750      ELSE -- x_return_status is populated at the end
2751        WSH_UTIL_CORE.ADD_MESSAGE(WSH_UTIL_CORE.G_RET_STS_WARNING);
2752      END IF;
2753    END IF;
2754 
2755    --
2756    IF (l_good_dels.count > 0) THEN
2757     --{
2758     IF (l_ship_method_flag) THEN
2759         l_trip_info.ship_method_code := l_ship_method_old;
2760         l_trip_info.carrier_id       := l_carrier_id_old;
2761         l_trip_info.mode_of_transport:= l_mode_old;
2762         l_trip_info.service_level    := l_service_level_old;
2763     END IF;
2764     --
2765     l_trip_info.ignore_for_planning := l_ignore_for_planning;
2766     l_trip_info.planned_flag    := 'N';
2767     l_trip_info.status_code   := 'OP';
2768     l_trip_info.creation_date  := SYSDATE;
2769     l_trip_info.created_by     := fnd_global.user_id;
2770     l_trip_info.last_update_date  := SYSDATE;
2771     l_trip_info.last_updated_by   := fnd_global.user_id;
2772     l_trip_info.last_update_login := fnd_global.login_id;
2773     --
2774     /* H integration */
2775     /* The trip level call should have been made at parent level */
2776     --
2777     IF l_debug_on THEN
2778        WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_PVT.CREATE_TRIP',WSH_DEBUG_SV.C_PROC_LEVEL);
2779     END IF;
2780     --
2781     wsh_trips_pvt.create_trip(l_trip_info, l_rowid, x_trip_id, x_trip_name,x_return_status);
2782     --
2783     IF (x_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN
2784      --
2785      IF l_debug_on THEN
2786        wsh_debug_sv.log(l_module_name, 'Return Status after calling create_trip', x_return_Status);
2787        WSH_DEBUG_SV.pop(l_module_name);
2788      END IF;
2789      --
2790      RETURN;
2791      --
2792     END IF;
2793     --
2794     IF l_debug_on THEN
2795       WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_ACTIONS.ASSIGN_DELIVERIES',WSH_DEBUG_SV.C_PROC_LEVEL);
2796     END IF;
2797     --
2798     wsh_delivery_legs_actions.assign_deliveries(
2799                p_del_rows => l_good_dels,
2800                p_trip_id => x_trip_id,
2801                p_create_flag => 'Y',
2802                x_leg_rows => l_del_legs,
2803                x_return_status => x_return_status,
2804                p_caller => 'AUTOCREATE_TRIP',
2805                -- Bug 3913206
2806                p_sc_pickup_date => p_sc_pickup_date,
2807                p_sc_dropoff_date => p_sc_dropoff_date
2808                );
2809     --
2810 
2811     IF x_return_status in ( WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
2812      --
2813      IF l_debug_on THEN
2814        wsh_debug_sv.log(l_module_name, 'Return Status after calling wsh_delivery_legs_actions.assign_deliveries', x_return_Status);
2815        WSH_DEBUG_SV.pop(l_module_name);
2816      END IF;
2817      --
2818      RETURN;
2819      --
2820     ELSE
2821      --{
2822      -- H integration
2823      --
2824      IF x_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
2825       l_num_warn := l_num_warn + 1;
2826      END IF;
2827 
2828      IF (WSH_UTIL_CORE.FTE_IS_INSTALLED = 'Y') THEN
2829        l_fte_flag := 'Y';
2830      END IF;
2831      --
2832      FOR st IN trip_stops(x_trip_id) LOOP
2833        --{
2834        IF (st.planned_departure_date IS NULL) THEN
2835          --{
2836          IF l_fte_flag = 'Y' THEN
2837           --{
2838           -- H integration
2839           -- Get pvt type record structure for stop
2840           --
2841           IF l_debug_on THEN
2842            WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_STOPS_GRP.GET_STOP_DETAILS_PVT',WSH_DEBUG_SV.C_PROC_LEVEL);
2843           END IF;
2844           --
2845           wsh_trip_stops_grp.get_stop_details_pvt
2846              (p_stop_id => st.stop_id,
2847               x_stop_rec => l_stop_rec,
2848               x_return_status => l_return_status);
2849           --
2850           IF x_return_status in ( WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
2851             --
2852             x_return_status := l_return_status;
2853             --
2854             IF l_debug_on THEN
2855               WSH_DEBUG_SV.pop(l_module_name);
2856             END IF;
2857             --
2858             RETURN;
2859             --
2860           ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
2861              l_num_warn := l_num_warn + 1;
2862           END IF;
2863           --
2864           l_stop_rec.planned_departure_date := nvl(l_stop_rec.planned_arrival_date,SYSDATE);
2865           --
2866           IF l_debug_on THEN
2867            WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_FTE_INTEGRATION.TRIP_STOP_VALIDATIONS',WSH_DEBUG_SV.C_PROC_LEVEL);
2868           END IF;
2869           --
2870           wsh_fte_integration.trip_stop_validations
2871             (p_stop_rec => l_stop_rec,
2872              p_trip_rec => l_trip_rec,
2873              p_action => 'UPDATE',
2874              x_return_status => l_return_status);
2875           --
2876           IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
2877            --
2878            IF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
2879               l_num_warn := l_num_warn + 1;
2880            ELSE
2881               x_return_status := l_return_status;
2882               --
2883               IF l_debug_on THEN
2884                WSH_DEBUG_SV.pop(l_module_name);
2885               END IF;
2886               --
2887               RETURN;
2888            END IF;
2889            --
2890           END IF;
2891           --}
2892          END IF;
2893          --
2894          -- End of H integration
2895          --
2896          UPDATE wsh_trip_stops
2897          SET   planned_departure_date = nvl(planned_arrival_date, SYSDATE)
2898          WHERE  stop_id = st.stop_id;
2899          --}
2900        END IF;
2901        --
2902        IF (st.planned_arrival_date IS NULL) THEN
2903         --{
2904         IF l_fte_flag = 'Y' THEN
2905          --{
2906          -- H integration
2907          -- Get pvt type record structure for stop
2908          --
2909          IF l_debug_on THEN
2910            WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_STOPS_GRP.GET_STOP_DETAILS_PVT',WSH_DEBUG_SV.C_PROC_LEVEL);
2911          END IF;
2912          --
2913          wsh_trip_stops_grp.get_stop_details_pvt
2914            (p_stop_id => st.stop_id,
2915             x_stop_rec => l_stop_rec,
2916             x_return_status => l_return_status);
2917          --
2918          IF x_return_status in ( WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
2919            x_return_status := l_return_status;
2920            --
2921            IF l_debug_on THEN
2922              WSH_DEBUG_SV.pop(l_module_name);
2923            END IF;
2924            --
2925            RETURN;
2926          ELSIF  l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
2927            l_num_warn := l_num_warn + 1;
2928          END IF;
2929          --
2930          l_stop_rec.planned_arrival_date := nvl(l_stop_rec.planned_departure_date,SYSDATE);
2931          --
2932          IF l_debug_on THEN
2933            WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_FTE_INTEGRATION.TRIP_STOP_VALIDATIONS',WSH_DEBUG_SV.C_PROC_LEVEL);
2934          END IF;
2935          --
2936          wsh_fte_integration.trip_stop_validations
2937            (p_stop_rec => l_stop_rec,
2938             p_trip_rec => l_trip_rec,
2939             p_action => 'UPDATE',
2940             x_return_status => l_return_status);
2941          --
2942          IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
2943           --
2944           IF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
2945             l_num_warn := l_num_warn + 1;
2946           ELSE
2947             x_return_status := l_return_status;
2948             --
2949             IF l_debug_on THEN
2950               WSH_DEBUG_SV.pop(l_module_name);
2951             END IF;
2952             --
2953             RETURN;
2954           END IF;
2955           --
2956          END IF;
2957          --}
2958         END IF;
2959         --
2960         -- End of H integration
2961         UPDATE wsh_trip_stops
2962         SET   planned_arrival_date = nvl(planned_departure_date, SYSDATE)
2963         WHERE  stop_id = st.stop_id;
2964         --
2965         -- DBI Project
2966         -- Updating  WSH_TRIP_STOPS.
2967         -- Call DBI API after the Update.
2968         -- This API will also check for DBI Installed or not
2969         IF l_debug_on THEN
2970           WSH_DEBUG_SV.log(l_module_name,'Calling DBI API.Stop id -',st.stop_id);
2971         END IF;
2972         l_stop_tab(1) := st.stop_id;
2973         WSH_INTEGRATION.DBI_Update_Trip_Stop_Log
2974           (p_stop_id_tab    => l_stop_tab,
2975            p_dml_type       => 'UPDATE',
2976            x_return_status      => l_dbi_rs);
2977 
2978         IF l_debug_on THEN
2979           WSH_DEBUG_SV.log(l_module_name,'Return Status after DBI Call-',l_dbi_rs);
2980         END IF;
2981         IF l_dbi_rs = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR THEN
2982               x_return_status := l_dbi_rs;
2983               -- just pass this return status to caller API
2984              IF l_debug_on THEN
2985                 WSH_DEBUG_SV.log(l_module_name,'DBI API Returned Unexpected error '||x_return_status);
2986                 WSH_DEBUG_SV.pop(l_module_name);
2987         END IF;
2988         return;
2989         END IF;
2990         -- End of Code for DBI Project
2991 
2992         --}
2993        END IF;
2994        --}
2995      END LOOP;
2996 
2997      -- SSN change
2998      -- Call to reset_stop_planned_dates API should be made
2999      -- only for profile = PAD
3000      IF WSH_TRIPS_ACTIONS.GET_STOP_SEQ_MODE  = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD THEN
3001        -- re-set the dates according to sequence number
3002        -- bug 3516052
3003        l_trip_id := x_trip_id;
3004 
3005        WSH_TRIPS_ACTIONS.reset_stop_planned_dates( p_trip_id => l_trip_id,
3006                                 p_caller        => 'WSH_AUTOCREATE_TRIP',
3007                                 x_return_status => l_return_status);
3008        IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
3009          IF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
3010            l_num_warn := l_num_warn + 1;
3011          ELSE
3012            x_return_status := l_return_status;
3013            --
3014            IF l_debug_on THEN
3015              WSH_DEBUG_SV.pop(l_module_name);
3016            END IF;
3017            --
3018            RETURN;
3019          END IF;
3020        END IF;
3021      ELSE
3022        -- call handle_internal_stop here for mode = SSN
3023        l_trip_ids(1) := x_trip_id;
3024        Handle_Internal_Stops
3025           (  p_trip_ids          =>  l_trip_ids,
3026              p_caller            => 'WSH_AUTOCREATE_TRIP',
3027              x_success_trip_ids  => l_success_trip_ids,
3028              x_return_status     => l_return_status);
3029        IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR, WSH_UTIL_CORE.G_RET_STS_ERROR) THEN
3030          x_return_status := l_return_status;
3031          --
3032          IF l_debug_on THEN
3033            WSH_DEBUG_SV.pop(l_module_name);
3034          END IF;
3035          --
3036          RETURN;
3037        ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
3038          l_num_warn := l_num_warn + 1;
3039        END IF;
3040      END IF;   --if get_stop_seq_mode = PAD
3041      --}
3042     END IF;
3043     --}
3044    END IF; /* if good_dels.COUNT */
3045     IF l_debug_on THEN
3046          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_FLEXFIELD_UTILS.WRITE_DFF_ATTRIBUTES',WSH_DEBUG_SV.C_PROC_LEVEL);
3047     END IF;
3048            -- for bug 5948562, to populate values in additional trip information flexfield, when auto creating a trip
3049            wsh_flexfield_utils.WRITE_DFF_ATTRIBUTES
3050                                                (p_table_name => 'WSH_TRIPS',
3051                                                 p_primary_id => x_trip_id,
3052                                                 x_return_status => x_return_status);
3053 
3054   IF (x_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN
3055                   IF l_debug_on THEN
3056                      WSH_DEBUG_SV.logmsg(l_module_name,  'PROC WSH_FLEXFIELD_UTILS.WRITE_DFF_ATTRIBUTES RETURNED ERROR'  );
3057                   END IF;
3058                   IF l_debug_on THEN
3059                      WSH_DEBUG_SV.pop(l_module_name);
3060                   END IF;
3061                   RETURN;
3062                END IF;
3063    --
3064    -- Bug 3413364, skip the delivery if it does not have both
3065    -- initial pickup date and ultimate dropoff date
3066    --
3067    IF l_num_skipped > 0 AND l_num_skipped = p_del_rows.count THEN
3068        x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3069    ELSIF l_num_skipped > 0 or l_num_warn> 0  THEN
3070        x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
3071    END IF;
3072    --
3073    -- TO DO: Add message for successful completion of action
3074    IF x_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
3075       FND_MESSAGE.SET_NAME('WSH', 'WSH_AUTOCREATE_TRIP_WARN');
3076       wsh_util_core.add_message(x_return_status);
3077    END IF;
3078    --
3079    IF l_debug_on THEN
3080       WSH_DEBUG_SV.pop(l_module_name);
3081    END IF;
3082    --
3083    EXCEPTION
3084      --
3085      WHEN others THEN
3086       --
3087       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP');
3088       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
3089       --
3090       IF l_debug_on THEN
3091         WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
3092         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
3093       END IF;
3094       --
3095 END autocreate_trip;
3096 
3097 
3098 --Compatibility Changes - removed trip_id, trip_name and added x_trip_rows
3099 
3100 PROCEDURE autocreate_del_trip(
3101          p_line_rows     IN    wsh_util_core.id_tab_type,
3102          p_org_rows      IN    wsh_util_core.id_tab_type,
3103          p_max_detail_commit  IN NUMBER := 1000,
3104          x_del_rows      OUT   NOCOPY wsh_util_core.id_tab_type,
3105          x_trip_rows      OUT  NOCOPY wsh_util_core.id_tab_type,
3106          x_return_status    OUT NOCOPY VARCHAR2) IS
3107 
3108 l_grouping_rows wsh_util_core.id_tab_type;
3109 
3110 --
3111 l_debug_on BOOLEAN;
3112 --
3113 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'AUTOCREATE_DEL_TRIP';
3114 
3115 --Compatibility Changes
3116     l_cc_validate_result        VARCHAR2(1);
3117     l_cc_failed_records         WSH_FTE_COMP_CONSTRAINT_PKG.failed_line_tab_type;
3118     l_cc_line_groups            WSH_FTE_COMP_CONSTRAINT_PKG.line_group_tab_type;
3119     l_cc_group_info         WSH_FTE_COMP_CONSTRAINT_PKG.cc_group_tab_type;
3120 
3121     b_cc_linefailed         boolean;
3122     b_cc_groupidexists          boolean;
3123     l_id_tab_temp           wsh_util_core.id_tab_type;
3124     l_line_rows_temp            wsh_util_core.id_tab_type;
3125     l_cc_count_success          NUMBER;
3126     l_cc_count_group_ids        NUMBER;
3127     l_cc_count_rec          NUMBER;
3128     l_cc_group_ids          wsh_util_core.id_tab_type;
3129     l_cc_count_trip_rows        NUMBER;
3130     l_cc_count_del_rows         NUMBER;
3131     l_cc_count_grouping_rows        NUMBER;
3132     l_del_rows_temp         wsh_util_core.id_tab_type;
3133     --l_trip_name               VARCHAR2(30);
3134     l_cc_trip_id            wsh_util_core.id_tab_type;
3135     l_trip_id_tab           wsh_util_core.id_tab_type;
3136     l_cc_del_rows           wsh_util_core.id_tab_type;
3137     l_cc_grouping_rows          wsh_util_core.id_tab_type;
3138     l_cc_return_status          VARCHAR2(1);
3139 --    l_trip_id             NUMBER;
3140 
3141     l_cc_upd_dlvy_intmed_ship_to    VARCHAR2(1);
3142     l_cc_upd_dlvy_ship_method       VARCHAR2(1);
3143     l_cc_dlvy_intmed_ship_to        NUMBER;
3144     l_cc_dlvy_ship_method       VARCHAR2(30);
3145 
3146     l_num_errors            NUMBER;
3147     l_num_warnings          NUMBER;
3148     l_msg_count             NUMBER;
3149     l_msg_data              VARCHAR2(2000);
3150     --dummy tables for calling validate_constraint_mainper
3151     l_cc_del_attr_tab           WSH_NEW_DELIVERIES_PVT.Delivery_Attr_Tbl_Type;
3152     l_cc_det_attr_tab           WSH_GLBL_VAR_STRCT_GRP.Delivery_Details_Attr_Tbl_Type;
3153     l_cc_trip_attr_tab          WSH_TRIPS_PVT.Trip_Attr_Tbl_Type;
3154     l_cc_stop_attr_tab          WSH_TRIP_STOPS_PVT.Stop_Attr_Tbl_Type;
3155     l_cc_in_ids             wsh_util_core.id_tab_type;
3156     l_cc_fail_ids       wsh_util_core.id_tab_type;
3157 
3158     CURSOR del_cur(p_dlvy_id NUMBER) IS
3159     SELECT SHIP_METHOD_CODE, INTMED_SHIP_TO_LOCATION_ID
3160     FROM wsh_new_deliveries
3161     WHERE delivery_id = p_dlvy_id;
3162     --and (SHIP_METHOD_CODE is not null OR INTMED_SHIP_TO_LOCATION_ID is not null);
3163 
3164     CURSOR trip_cur(p_trip_id NUMBER) IS
3165     SELECT SHIP_METHOD_CODE
3166     FROM wsh_trips
3167     WHERE trip_id = p_trip_id;
3168     --and SHIP_METHOD_CODE is not null;
3169 
3170     l_line_rows             wsh_util_core.id_tab_type:=p_line_rows;
3171 --Compatibility Changes
3172 
3173 /* J TP Release */
3174 l_trip_ids wsh_util_core.id_tab_type;
3175 l_trip_names wsh_util_core.column_tab_type;
3176 l_tripindex NUMBER;
3177 
3178 -- deliveryMerge
3179 l_return_status         VARCHAR2(1);
3180 Adjust_Planned_Flag_Err EXCEPTION;
3181 --
3182 BEGIN
3183 
3184    --
3185    -- Debug Statements
3186    --
3187    --
3188    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
3189    --
3190    IF l_debug_on IS NULL--{
3191    THEN
3192        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
3193    END IF;--}
3194    --
3195    IF l_debug_on THEN--{
3196        WSH_DEBUG_SV.push(l_module_name);
3197        --
3198        WSH_DEBUG_SV.log(l_module_name,'P_MAX_DETAIL_COMMIT',P_MAX_DETAIL_COMMIT);
3199    END IF;--}
3200    --
3201    --
3202    -- Debug Statements
3203    --
3204    IF l_debug_on THEN--{
3205        WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_AUTOCREATE.AUTOCREATE_DEL_ACROSS_ORGS',WSH_DEBUG_SV.C_PROC_LEVEL);
3206    END IF;--}
3207    --
3208     --Compatibility Changes
3209     IF wsh_util_core.fte_is_installed = 'Y' THEN--{
3210 
3211       WSH_FTE_COMP_CONSTRAINT_PKG.validate_constraint_main(
3212          p_api_version_number   =>  1.0,
3213          p_init_msg_list        =>  FND_API.G_FALSE,
3214          p_entity_type          =>  'L',
3215          p_target_id            =>  null,
3216          p_action_code          =>  'AUTOCREATE-DEL',
3217          p_del_attr_tab         =>  l_cc_del_attr_tab,
3218          p_det_attr_tab         =>  l_cc_det_attr_tab,
3219          p_trip_attr_tab        =>  l_cc_trip_attr_tab,
3220          p_stop_attr_tab        =>  l_cc_stop_attr_tab,
3221          p_in_ids               =>  l_line_rows,
3222          x_fail_ids             =>  l_cc_fail_ids,
3223          x_validate_result          =>  l_cc_validate_result,
3224          x_failed_lines             =>  l_cc_failed_records,
3225          x_line_groups              =>  l_cc_line_groups,
3226          x_group_info               =>  l_cc_group_info,
3227          x_msg_count                =>  l_msg_count,
3228          x_msg_data                 =>  l_msg_data,
3229          x_return_status            =>  x_return_status);
3230 
3231       IF l_debug_on THEN--{
3232         wsh_debug_sv.log(l_module_name,'Return Status After Calling validate_constraint_main',x_return_status);
3233         wsh_debug_sv.log(l_module_name,'validate_result After Calling validate_constraint_main',l_cc_validate_result);
3234         wsh_debug_sv.log(l_module_name,'msg_count After Calling validate_constraint_main',l_msg_count);
3235         wsh_debug_sv.log(l_module_name,'msg_data After Calling validate_constraint_main',l_msg_data);
3236         wsh_debug_sv.log(l_module_name,'fail_ids count After Calling validate_constraint_main',l_cc_failed_records.COUNT);
3237         wsh_debug_sv.log(l_module_name,'l_cc_line_groups.count count After Calling validate_constraint_main',l_cc_line_groups.COUNT);
3238         wsh_debug_sv.log(l_module_name,'group_info count After Calling validate_constraint_main',l_cc_group_info.COUNT);
3239       END IF;--}
3240       --
3241 
3242 
3243     IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)
3244        OR (x_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR AND l_cc_failed_records.COUNT=l_line_rows.COUNT)
3245     THEN--{
3246         --
3247         -- Debug Statements
3248         --
3249         IF l_debug_on THEN--{
3250           WSH_DEBUG_SV.pop(l_module_name);
3251         END IF;--}
3252         --
3253         RETURN;
3254     END IF;--}
3255 
3256     IF l_cc_failed_records.COUNT>0 AND x_return_status=wsh_util_core.g_ret_sts_error THEN--{
3257          IF l_debug_on THEN--{
3258            WSH_DEBUG_SV.logmsg(l_module_name,'All dels have failed compatibility -> delivery and Trip not created');
3259          END IF;--}
3260          -- if one one delivery fails for auto create trip, all the lines should
3261          -- should be unassigned from the deliveries and the action should
3262          -- fail for all the lines. (pack J 2862777)
3263          x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3264          fnd_message.set_name('WSH', 'WSH_COMP_ACT_FAIL');
3265          wsh_util_core.add_message(x_return_status);
3266          IF l_debug_on THEN--{
3267              WSH_DEBUG_SV.pop(l_module_name);
3268          END IF;--}
3269          --
3270          RETURN;
3271     ELSIF l_cc_line_groups.COUNT>0 AND x_return_status=wsh_util_core.g_ret_sts_error THEN--}{
3272 
3273         --1. get the group ids by which the constraints API has grouped the lines
3274         l_cc_count_group_ids:=1;
3275         FOR i in l_cc_line_groups.FIRST..l_cc_line_groups.LAST LOOP --{
3276             b_cc_groupidexists:=FALSE;
3277             IF l_cc_group_ids.COUNT>0 THEN--{
3278              FOR j in l_cc_group_ids.FIRST..l_cc_group_ids.LAST LOOP--{
3279                 IF (l_cc_line_groups(i).line_group_id=l_cc_group_ids(j)) THEN--{
3280                     b_cc_groupidexists:=TRUE;
3281                 END IF;--}
3282              END LOOP;--}
3283             END IF;--}
3284             IF (NOT(b_cc_groupidexists)) THEN--{
3285                 l_cc_group_ids(l_cc_count_group_ids):=l_cc_line_groups(i).line_group_id;
3286                 l_cc_count_group_ids:=l_cc_count_group_ids+1;
3287             END IF;--}
3288         END LOOP;--}
3289 
3290         --2. from the group id table above, loop thru lines table to get the lines which belong
3291         --to each group and call autocreate_trip for each group
3292 
3293         FOR i in l_cc_group_ids.FIRST..l_cc_group_ids.LAST LOOP--{
3294             l_cc_count_rec:=1;
3295             FOR j in l_cc_line_groups.FIRST..l_cc_line_groups.LAST LOOP     --{
3296                 IF l_cc_line_groups(j).line_group_id=l_cc_group_ids(i) THEN--{
3297                   l_id_tab_temp(l_cc_count_rec):=l_cc_line_groups(j).entity_line_id;
3298                   l_cc_count_rec:=l_cc_count_rec+1;
3299                 END IF;--}
3300             END LOOP;--}
3301 
3302                 --
3303             IF l_debug_on THEN--{
3304                     wsh_debug_sv.log(l_module_name,'id_tab_temp count ',l_id_tab_temp.COUNT);
3305             END IF;--}
3306 
3307 
3308            wsh_delivery_autocreate.autocreate_del_across_orgs(
3309                       p_line_rows => l_id_tab_temp,
3310                       p_org_rows => p_org_rows,
3311                       p_container_flag => 'N',
3312                       p_check_flag => 'N',
3313                       p_caller     => 'WSH_AUTO_CREATE_DEL_TRIP',
3314                       p_max_detail_commit => p_max_detail_commit,
3315                       x_del_rows => x_del_rows,
3316                       x_grouping_rows => l_grouping_rows,
3317                       x_return_status => x_return_status);
3318 
3319           --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3320           l_cc_upd_dlvy_intmed_ship_to:='Y';
3321                   l_cc_upd_dlvy_ship_method:='Y';
3322           IF l_cc_group_info.COUNT>0 THEN--{
3323            FOR j in l_cc_group_info.FIRST..l_cc_group_info.LAST LOOP--{
3324               IF l_cc_group_info(j).line_group_id=l_cc_group_ids(i) THEN--{
3325                 l_cc_upd_dlvy_intmed_ship_to:=l_cc_group_info(j).upd_dlvy_intmed_ship_to;
3326                 l_cc_upd_dlvy_ship_method:=l_cc_group_info(j).upd_dlvy_ship_method;
3327               END IF;--}
3328            END LOOP;--}
3329           END IF;--}
3330 
3331         IF l_debug_on THEN--{
3332                 wsh_debug_sv.log(l_module_name,'l_cc_upd_dlvy_intmed_ship_to ',l_cc_upd_dlvy_intmed_ship_to);
3333                 wsh_debug_sv.log(l_module_name,'l_cc_upd_dlvy_ship_method ',l_cc_upd_dlvy_ship_method);
3334                 wsh_debug_sv.log(l_module_name,'l_delivery_ids_tbl.COUNT ',x_del_rows.COUNT);
3335                 wsh_debug_sv.log(l_module_name,'l_grouping_tbl.COUNT ',l_grouping_rows.COUNT);
3336             wsh_debug_sv.log(l_module_name,'l_return_status after calling autocreate_del in comp ',x_return_status);
3337         END IF;--}
3338 
3339           IF l_cc_upd_dlvy_intmed_ship_to='N' OR l_cc_upd_dlvy_ship_method='N' THEN--{
3340              IF l_id_tab_temp.COUNT>0 THEN--{
3341                  FOR i in l_id_tab_temp.FIRST..l_id_tab_temp.LAST LOOP--{
3342                    FOR delcurtemp in del_cur(l_id_tab_temp(i)) LOOP--{
3343                         l_cc_dlvy_intmed_ship_to:=delcurtemp.INTMED_SHIP_TO_LOCATION_ID;
3344                         l_cc_dlvy_ship_method:=delcurtemp.SHIP_METHOD_CODE;
3345                         IF l_cc_upd_dlvy_intmed_ship_to='N' and l_cc_dlvy_intmed_ship_to IS NOT NULL THEN--{
3346                             update wsh_new_deliveries set INTMED_SHIP_TO_LOCATION_ID=null
3347                             where delivery_id=l_id_tab_temp(i);
3348                         END IF;--}
3349                         --IF l_cc_upd_dlvy_ship_method='N' and l_cc_dlvy_ship_method IS NOT NULL THEN
3350                         IF l_cc_upd_dlvy_ship_method='N' THEN--{
3351                             update wsh_new_deliveries
3352                             set SHIP_METHOD_CODE=null,
3353                                 CARRIER_ID = null,
3354                                 MODE_OF_TRANSPORT = null,
3355                                 SERVICE_LEVEL = null
3356                             where delivery_id=l_id_tab_temp(i);
3357                         END IF;             --}
3358                   END LOOP;--}
3359                 END LOOP;--}
3360              END IF;--}
3361           END IF;--}
3362           --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3363 
3364           IF l_cc_del_rows.COUNT=0 THEN--{
3365             l_cc_del_rows:=x_del_rows;
3366           ELSE--}{
3367             l_cc_count_del_rows:=l_cc_del_rows.COUNT;
3368             IF x_del_rows.COUNT>0 THEN--{
3369                FOR i in x_del_rows.FIRST..x_del_rows.LAST LOOP--{
3370                 l_cc_del_rows(l_cc_count_del_rows+i):=x_del_rows(i);
3371                END LOOP;--}
3372             END IF;--}
3373           END IF;         --}
3374 
3375           IF l_cc_grouping_rows.COUNT=0 THEN--{
3376             l_cc_grouping_rows:=l_grouping_rows;
3377           ELSE--}{
3378             l_cc_count_grouping_rows:=l_cc_grouping_rows.COUNT;
3379             IF l_grouping_rows.COUNT>0 THEN--{
3380                FOR i in l_grouping_rows.FIRST..l_grouping_rows.LAST LOOP--{
3381                 l_cc_grouping_rows(l_cc_count_grouping_rows+i):=l_grouping_rows(i);
3382                END LOOP;--}
3383             END IF;--}
3384           END IF;         --}
3385 
3386           IF (l_cc_return_status is not null AND l_cc_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN--{
3387               x_return_status:=l_cc_return_status;
3388           ELSIF (l_cc_return_status is not null AND l_cc_return_status=WSH_UTIL_CORE.G_RET_STS_WARNING AND x_return_status=WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN--}{
3389               x_return_status:=l_cc_return_status;
3390           ELSE--}{
3391               l_cc_return_status:=x_return_status;
3392           END IF;--}
3393               --
3394               --
3395         END LOOP;--}
3396         l_grouping_rows:=l_cc_grouping_rows;
3397         x_del_rows:=l_cc_del_rows;
3398         x_return_status:=l_cc_return_status;
3399 
3400         IF l_debug_on THEN--{
3401                 wsh_debug_sv.log(l_module_name,'l_delivery_ids_tbl.COUNT after loop ',x_del_rows.COUNT);
3402                 wsh_debug_sv.log(l_module_name,'l_grouping_tbl.COUNT after loop',l_grouping_rows.COUNT);
3403         END IF;--}
3404 
3405         ELSE -- line_group>0 }{
3406 
3407            wsh_delivery_autocreate.autocreate_del_across_orgs(
3408                   p_line_rows => p_line_rows,
3409                   p_org_rows => p_org_rows,
3410                   p_container_flag => 'N',
3411                   p_check_flag => 'N',
3412                   p_caller     => 'WSH_AUTO_CREATE_DEL_TRIP',
3413                   p_max_detail_commit => p_max_detail_commit,
3414                   x_del_rows => x_del_rows,
3415                   x_grouping_rows => l_grouping_rows,
3416                   x_return_status => x_return_status);
3417 
3418            --bug 2729742
3419              IF l_debug_on THEN--{
3420                     wsh_debug_sv.log(l_module_name,'l_cc_group_info.COUNT ',l_cc_group_info.COUNT);
3421              END IF;--}
3422 
3423              IF l_cc_group_info.COUNT>0 THEN--{
3424                   --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3425                   l_cc_upd_dlvy_intmed_ship_to:='Y';
3426                   l_cc_upd_dlvy_ship_method:='Y';
3427                   l_cc_upd_dlvy_intmed_ship_to:=l_cc_group_info(1).upd_dlvy_intmed_ship_to;
3428                   l_cc_upd_dlvy_ship_method:=l_cc_group_info(1).upd_dlvy_ship_method;
3429 
3430                   IF l_debug_on THEN--{
3431 
3432                     wsh_debug_sv.log(l_module_name,'l_cc_group_info.COUNT ',l_cc_group_info.COUNT);
3433                     wsh_debug_sv.log(l_module_name,'l_cc_upd_dlvy_intmed_ship_to ',l_cc_upd_dlvy_intmed_ship_to);
3434                     wsh_debug_sv.log(l_module_name,'l_cc_upd_dlvy_ship_method ',l_cc_upd_dlvy_ship_method);
3435                   END IF;--}
3436 
3437                   IF l_cc_upd_dlvy_intmed_ship_to='N' OR l_cc_upd_dlvy_ship_method='N' THEN--{
3438                      IF x_del_rows.COUNT>0 THEN--{
3439                        FOR i in x_del_rows.FIRST..x_del_rows.LAST LOOP--{
3440                           FOR delcurtemp in del_cur(x_del_rows(i)) LOOP--{
3441                              l_cc_dlvy_intmed_ship_to:=delcurtemp.INTMED_SHIP_TO_LOCATION_ID;
3442                              l_cc_dlvy_ship_method:=delcurtemp.SHIP_METHOD_CODE;
3443                              IF l_cc_upd_dlvy_intmed_ship_to='N' and l_cc_dlvy_intmed_ship_to IS NOT NULL THEN--{
3444                                update wsh_new_deliveries set INTMED_SHIP_TO_LOCATION_ID=null
3445                                where delivery_id=x_del_rows(i);
3446                              END IF;--}
3447                              --IF l_cc_upd_dlvy_ship_method='N' and l_cc_dlvy_ship_method IS NOT NULL THEN
3448                              IF l_cc_upd_dlvy_ship_method='N' THEN--{
3449                                update wsh_new_deliveries
3450                                set SHIP_METHOD_CODE=null,
3451                                 CARRIER_ID = null,
3452                                 MODE_OF_TRANSPORT = null,
3453                                 SERVICE_LEVEL = null
3454                                where delivery_id=x_del_rows(i);
3455                              END IF;             --}
3456                           END LOOP;--}
3457                        END LOOP;--}
3458                      END IF;--}
3459                   END IF;--}
3460                   --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3461              END IF;--group_info.count>0--}
3462            --bug 2729742
3463 
3464         END IF;--line_groups is not null--}
3465     ELSE --}{
3466             --
3467        wsh_delivery_autocreate.autocreate_del_across_orgs(
3468                   p_line_rows => p_line_rows,
3469                   p_org_rows => p_org_rows,
3470                   p_container_flag => 'N',
3471                   p_check_flag => 'N',
3472                   p_caller     => 'WSH_AUTO_CREATE_DEL_TRIP',
3473                   p_max_detail_commit => p_max_detail_commit,
3474                   x_del_rows => x_del_rows,
3475                   x_grouping_rows => l_grouping_rows,
3476                   x_return_status => x_return_status);
3477 
3478     END IF;--}
3479     --Compatibility Changes
3480 
3481       --
3482       IF l_debug_on THEN--{
3483         wsh_debug_sv.log(l_module_name,'Return Status After Calling Autocreate_Del_across_orgs',x_return_status);
3484       END IF;--}
3485 
3486 
3487     IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR) OR
3488         (x_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN--{
3489         --
3490         -- Debug Statements
3491         --
3492         IF l_debug_on THEN--{
3493           WSH_DEBUG_SV.pop(l_module_name);
3494         END IF;--}
3495         --
3496         RETURN;
3497     END IF;--}
3498 
3499 --autocreate trip with all delivery lines
3500     --Compatibility Changes
3501     --initialize tables
3502     l_cc_failed_records.DELETE;
3503     l_cc_line_groups.DELETE;
3504     l_cc_group_ids.DELETE;
3505     l_id_tab_temp.DELETE;
3506     IF wsh_util_core.fte_is_installed = 'Y' THEN--{
3507 
3508        WSH_FTE_COMP_CONSTRAINT_PKG.validate_constraint_main(
3509          p_api_version_number   =>  1.0,
3510          p_init_msg_list        =>  FND_API.G_FALSE,
3511          p_entity_type          =>  'D',
3512          p_target_id            =>  null,
3513          p_action_code          =>  'AUTOCREATE-TRIP',
3514          p_del_attr_tab         =>  l_cc_del_attr_tab,
3515          p_det_attr_tab         =>  l_cc_det_attr_tab,
3516          p_trip_attr_tab        =>  l_cc_trip_attr_tab,
3517          p_stop_attr_tab        =>  l_cc_stop_attr_tab,
3518          p_in_ids               =>  x_del_rows,
3519          x_fail_ids             =>  l_cc_fail_ids,
3520          x_validate_result          =>  l_cc_validate_result,
3521          x_failed_lines             =>  l_cc_failed_records,
3522          x_line_groups              =>  l_cc_line_groups,
3523          x_group_info               =>  l_cc_group_info,
3524          x_msg_count                =>  l_msg_count,
3525          x_msg_data                 =>  l_msg_data,
3526          x_return_status            =>  x_return_status);
3527 
3528        IF l_debug_on THEN--{
3529          wsh_debug_sv.logmsg(l_module_name,'For autocreate_trip');
3530          wsh_debug_sv.log(l_module_name,'Return Status After Calling validate_constraint_main',x_return_status);
3531          wsh_debug_sv.log(l_module_name,'validate_result After Calling validate_constraint_main',l_cc_validate_result);
3532          wsh_debug_sv.log(l_module_name,'msg_count After Calling validate_constraint_main',l_msg_count);
3533          wsh_debug_sv.log(l_module_name,'msg_data After Calling validate_constraint_main',l_msg_data);
3534          wsh_debug_sv.log(l_module_name,'fail_ids count After Calling validate_constraint_main',l_cc_failed_records.COUNT);
3535          wsh_debug_sv.log(l_module_name,'l_cc_line_groups.count count After Calling validate_constraint_main',l_cc_line_groups.COUNT);
3536          wsh_debug_sv.log(l_module_name,'group_info count After Calling validate_constraint_main',l_cc_group_info.COUNT);
3537        END IF;--}
3538        --
3539 
3540        IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN--{
3541         --
3542         -- Debug Statements
3543         --
3544             IF l_debug_on THEN--{
3545                 WSH_DEBUG_SV.pop(l_module_name);
3546             END IF;--}
3547             --
3548             RETURN;
3549        END IF;--}
3550 
3551        --do nothing if all dels have errored out (deliveries shud be created and trip shud not be
3552        IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR AND l_cc_failed_records.COUNT > 0) THEN--{
3553          IF l_debug_on THEN--{
3554            WSH_DEBUG_SV.logmsg(l_module_name,'All dels have failed compatibility -> Trip not created');
3555          END IF;--}
3556          -- if one one delivery fails for auto create trip, all the lines should
3557          -- should be unassigned from the deliveries and the action should
3558          -- fail for all the lines. (pack J 2862777)
3559          x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3560          fnd_message.set_name('WSH', 'WSH_COMP_ACT_FAIL');
3561          wsh_util_core.add_message(x_return_status);
3562          IF l_debug_on THEN--{
3563              WSH_DEBUG_SV.pop(l_module_name);
3564          END IF;--}
3565          --
3566          RETURN;
3567        ELSIF l_cc_line_groups.COUNT>0 AND x_return_status=wsh_util_core.g_ret_sts_error THEN--}{
3568 
3569          --1. get the group ids by which the constraints API has grouped the lines
3570          l_cc_count_group_ids:=1;
3571          FOR i in l_cc_line_groups.FIRST..l_cc_line_groups.LAST LOOP --{
3572             b_cc_groupidexists:=FALSE;
3573             IF l_cc_group_ids.COUNT>0 THEN--{
3574                FOR j in l_cc_group_ids.FIRST..l_cc_group_ids.LAST LOOP--{
3575                 IF (l_cc_line_groups(i).line_group_id=l_cc_group_ids(j)) THEN--{
3576                     b_cc_groupidexists:=TRUE;
3577                 END IF;--}
3578                END LOOP;--}
3579             END IF;--}
3580             IF (NOT(b_cc_groupidexists)) THEN--{
3581                 l_cc_group_ids(l_cc_count_group_ids):=l_cc_line_groups(i).line_group_id;
3582                 l_cc_count_group_ids:=l_cc_count_group_ids+1;
3583             END IF;--}
3584          END LOOP;--}
3585 
3586          --2. from the group id table above, loop thru lines table to get the lines which belong
3587          --to each group and call autocreate_trip for each group
3588 
3589          FOR i in l_cc_group_ids.FIRST..l_cc_group_ids.LAST LOOP--{
3590             l_cc_count_rec:=1;
3591             FOR j in l_cc_line_groups.FIRST..l_cc_line_groups.LAST LOOP     --{
3592                 IF l_cc_line_groups(j).line_group_id=l_cc_group_ids(i) THEN--{
3593                   l_id_tab_temp(l_cc_count_rec):=l_cc_line_groups(j).entity_line_id;
3594                   l_cc_count_rec:=l_cc_count_rec+1;
3595                 END IF;--}
3596             END LOOP;--}
3597 
3598             --
3599             IF l_debug_on THEN--{
3600                 wsh_debug_sv.log(l_module_name,'id_tab_temp count ',l_id_tab_temp.COUNT);
3601                 wsh_debug_sv.logmsg(l_module_name,'Calling program unit WSH_NEW_DELIVERY_ACTIONS.Adjust_Planned_Flag ');
3602             END IF;--}
3603 
3604             -- deliveryMerge
3605             WSH_NEW_DELIVERY_ACTIONS.Adjust_Planned_Flag(
3606            p_delivery_ids           => l_id_tab_temp,
3607            p_caller                 => 'WSH_DLMG',
3608            p_force_appending_limit  => 'N',
3609            p_call_lcss              => 'Y',
3610            p_event                  => NULL,
3611            x_return_status          => l_return_status);
3612 
3613             IF l_debug_on THEN                --{
3614                 wsh_debug_sv.log(l_module_name,'Return status from WSH_NEW_DELIVERY_ACTIONS.Adjust_Planned_Flag ', l_return_status);
3615             END IF;--}
3616 
3617             IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR,WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN--{
3618                raise Adjust_Planned_Flag_Err;
3619             END IF;            --}
3620 
3621             /* J TP Release */
3622             autocreate_trip_multi(
3623                              p_del_rows      => l_id_tab_temp,
3624                              p_entity        => 'L',
3625                              x_trip_ids      => l_trip_ids,
3626                              x_trip_names    => l_trip_names,
3627                              x_return_status => x_return_status);
3628 
3629             --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3630             l_cc_upd_dlvy_ship_method:='Y';
3631             IF l_cc_group_info.COUNT>0 THEN--{
3632              FOR j in l_cc_group_info.FIRST..l_cc_group_info.LAST LOOP--{
3633               IF l_cc_group_info(j).line_group_id=l_cc_group_ids(i) THEN--{
3634                 l_cc_upd_dlvy_ship_method:=l_cc_group_info(j).upd_dlvy_ship_method;
3635               END IF;--}
3636              END LOOP;--}
3637             END IF;--}
3638 
3639             IF l_debug_on THEN--{
3640                 wsh_debug_sv.log(l_module_name,'l_cc_upd_dlvy_ship_method ',l_cc_upd_dlvy_ship_method);
3641                 wsh_debug_sv.log(l_module_name,'l_trip_id ',l_trip_ids(1));
3642                 wsh_debug_sv.log(l_module_name,'l_trip_name ',l_trip_names(1));
3643                 wsh_debug_sv.log(l_module_name,'l_return_status after calling autocreate_trip in comp ',x_return_status);
3644             END IF;--}
3645 
3646             /* J TP Release */
3647             IF l_trip_ids is not null AND l_trip_ids.COUNT>0 THEN--{
3648              FOR l_tripindex IN l_trip_ids.FIRST..l_trip_ids.LAST LOOP--{
3649                IF l_cc_upd_dlvy_ship_method='N' THEN--{
3650                 FOR tripcurtemp in trip_cur(l_trip_ids(l_tripindex)) LOOP--{
3651                             update wsh_trips
3652                             set SHIP_METHOD_CODE=null,
3653                                 CARRIER_ID = null,
3654                                 MODE_OF_TRANSPORT = null,
3655                                 SERVICE_LEVEL = null
3656                             where trip_id=l_trip_ids(l_tripindex);
3657                 END LOOP;--}
3658                END IF;--}
3659                --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3660                  l_cc_trip_id(l_cc_trip_id.COUNT+1):=l_trip_ids(l_tripindex);
3661              END LOOP;--}
3662             END IF;--}
3663 
3664             IF (l_cc_return_status is not null AND l_cc_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN--{
3665               x_return_status:=l_cc_return_status;
3666             ELSIF (l_cc_return_status is not null AND l_cc_return_status=WSH_UTIL_CORE.G_RET_STS_WARNING AND x_return_status=WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN--}{
3667               x_return_status:=l_cc_return_status;
3668             ELSE --}{
3669               l_cc_return_status:=x_return_status;
3670             END IF;--}
3671               --
3672          END LOOP;--}
3673 
3674          l_trip_id_tab:=l_cc_trip_id;
3675          x_return_status:=l_cc_return_status;
3676 
3677          IF l_debug_on THEN--{
3678                 wsh_debug_sv.log(l_module_name,'l_trip_id_tab.COUNT after loop ',l_trip_id_tab.COUNT);
3679          END IF;--}
3680 
3681        ELSE --line_groups --}{
3682          --
3683             -- deliveryMerge
3684             WSH_NEW_DELIVERY_ACTIONS.Adjust_Planned_Flag(
3685            p_delivery_ids           => x_del_rows,
3686            p_caller                 => 'WSH_DLMG',
3687            p_force_appending_limit  => 'N',
3688            p_call_lcss              => 'Y',
3689            p_event                  => NULL,
3690            x_return_status          => l_return_status);
3691 
3692             IF l_debug_on THEN                --{
3693                 wsh_debug_sv.log(l_module_name,'Return status from WSH_NEW_DELIVERY_ACTIONS.Adjust_Planned_Flag ', l_return_status);
3694             END IF;--}
3695 
3696             IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR,WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN--{
3697                raise Adjust_Planned_Flag_Err;
3698             END IF;         --}
3699 
3700             /* J TP Release */
3701             autocreate_trip_multi(
3702                              p_del_rows      => x_del_rows,
3703                              p_entity        => 'L',
3704                              x_trip_ids      => l_trip_id_tab,
3705                              x_trip_names    => l_trip_names,
3706                              x_return_status => x_return_status);
3707 
3708            --bug 2729742
3709            --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3710              IF l_debug_on THEN--{
3711                     wsh_debug_sv.log(l_module_name,'l_cc_group_info.COUNT ',l_cc_group_info.COUNT);
3712              END IF;--}
3713 
3714              IF l_cc_group_info.COUNT>0 THEN--{
3715 
3716                   l_cc_upd_dlvy_ship_method:='Y';
3717                   l_cc_upd_dlvy_ship_method:=l_cc_group_info(1).upd_dlvy_ship_method;
3718 
3719                   IF l_debug_on THEN--{
3720 
3721                     wsh_debug_sv.log(l_module_name,'l_cc_group_info.COUNT ',l_cc_group_info.COUNT);
3722                     wsh_debug_sv.log(l_module_name,'l_cc_upd_dlvy_ship_method ',l_cc_upd_dlvy_ship_method);
3723                   END IF;--}
3724 
3725                   IF l_cc_upd_dlvy_ship_method='N' and l_trip_id_tab is not null and l_trip_id_tab.COUNT>0 THEN--{
3726                    FOR l_tripindex IN l_trip_id_tab.FIRST..l_trip_id_tab.LAST LOOP--{
3727                     FOR tripcurtemp in trip_cur(l_trip_id_tab(l_tripindex)) LOOP--{
3728                             update wsh_trips
3729                             set SHIP_METHOD_CODE=null,
3730                                 CARRIER_ID = null,
3731                                 MODE_OF_TRANSPORT = null,
3732                                 SERVICE_LEVEL = null
3733                             where trip_id=l_trip_id_tab(l_tripindex);
3734                     END LOOP;--}
3735                    END LOOP;--}
3736                   END IF;--}
3737 
3738                   --set the intermediate ship to, ship method to null if group rec from constraint validation has these as 'N'
3739              END IF;--group_info.count>0--}
3740            --bug 2729742
3741 
3742        END IF;--line_groups is not null--}
3743 
3744     ELSE --}{
3745             --
3746             -- deliveryMerge
3747        WSH_NEW_DELIVERY_ACTIONS.Adjust_Planned_Flag(
3748           p_delivery_ids           => x_del_rows,
3749           p_caller                 => 'WSH_DLMG',
3750           p_force_appending_limit  => 'N',
3751           p_call_lcss              => 'Y',
3752           p_event                  => NULL,
3753           x_return_status          => l_return_status);
3754 
3755        IF l_debug_on THEN                --{
3756            wsh_debug_sv.log(l_module_name,'Return status from WSH_NEW_DELIVERY_ACTIONS.Adjust_Planned_Flag ', l_return_status);
3757        END IF;--}
3758 
3759        IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR,WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN--{
3760           raise Adjust_Planned_Flag_Err;
3761        END IF; --}
3762 
3763        /* J TP Release */
3764        autocreate_trip_multi(
3765                              p_del_rows      => x_del_rows,
3766                              p_entity        => 'L',
3767                              x_trip_ids      => l_trip_id_tab,
3768                              x_trip_names    => l_trip_names,
3769                              x_return_status => x_return_status);
3770 
3771     END IF;--}
3772     --Compatibility Changes
3773     --autocreate trip
3774     x_trip_rows:=l_trip_id_tab;
3775 
3776 
3777 
3778    IF (x_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN--{
3779       --
3780       -- Debug Statements
3781       --
3782       IF l_debug_on THEN--{
3783           WSH_DEBUG_SV.pop(l_module_name);
3784       END IF;--}
3785       --
3786       RETURN;
3787    END IF;--}
3788 
3789    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
3790 
3791    --
3792    -- Debug Statements
3793    --
3794    IF l_debug_on THEN--{
3795       WSH_DEBUG_SV.pop(l_module_name);
3796    END IF;--}
3797    --
3798 
3799    EXCEPTION
3800 
3801      -- deliveryMerge
3802      WHEN Adjust_Planned_Flag_Err THEN
3803            FND_MESSAGE.SET_NAME('WSH', 'WSH_ADJUST_PLANNED_FLAG_ERR');
3804            WSH_UTIL_CORE.add_message(l_return_status,l_module_name);
3805            x_return_status := l_return_status;
3806 
3807            IF l_debug_on THEN--{
3808               WSH_DEBUG_SV.logmsg(l_module_name,'Adjust_Planned_Flag_Err exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
3809               WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:Adjust_Planned_Flag_Err');
3810            END IF;--}
3811 
3812      WHEN others THEN
3813       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.AUTOCREATE_DEL_TRIP');
3814       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
3815 
3816 
3817 --
3818 -- Debug Statements
3819 --
3820 IF l_debug_on THEN--{
3821     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
3822     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
3823 END IF;--}
3824 --
3825 END autocreate_del_trip;
3826 
3827 PROCEDURE assign_trip( p_line_rows IN wsh_util_core.id_tab_type,
3828                p_trip_id   IN NUMBER,
3829                x_del_rows  OUT NOCOPY  wsh_util_core.id_tab_type,
3830                x_return_status OUT NOCOPY  VARCHAR2) IS
3831 
3832 l_grouping_rows wsh_util_core.id_tab_type;
3833 l_del_legs wsh_util_core.id_tab_type;
3834 l_org_rows wsh_util_core.id_tab_type;
3835 
3836 l_return_status       VARCHAR2(1);
3837 reprice_required_err EXCEPTION;
3838 
3839 --
3840 l_debug_on BOOLEAN;
3841 --
3842 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'ASSIGN_TRIP';
3843 --
3844 BEGIN
3845    --
3846    -- Debug Statements
3847    --
3848    --
3849    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
3850    --
3851    IF l_debug_on IS NULL
3852    THEN
3853        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
3854    END IF;
3855    --
3856    IF l_debug_on THEN
3857        WSH_DEBUG_SV.push(l_module_name);
3858        --
3859        WSH_DEBUG_SV.log(l_module_name,'P_TRIP_ID',P_TRIP_ID);
3860    END IF;
3861    --
3862    --
3863    -- Debug Statements
3864    --
3865    IF l_debug_on THEN
3866        WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_AUTOCREATE.AUTOCREATE_DEL_ACROSS_ORGS',WSH_DEBUG_SV.C_PROC_LEVEL);
3867    END IF;
3868    --
3869    wsh_delivery_autocreate.autocreate_del_across_orgs(
3870                   p_line_rows => p_line_rows,
3871                   p_org_rows => l_org_rows,
3872                   p_container_flag => 'N',
3873                   p_check_flag => 'N',
3874                   p_max_detail_commit => 1000,
3875                   x_del_rows => x_del_rows,
3876                   x_grouping_rows => l_grouping_rows,
3877                   x_return_status => x_return_status);
3878 
3879    IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR) OR
3880     (x_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
3881       --
3882       -- Debug Statements
3883       --
3884       IF l_debug_on THEN
3885           WSH_DEBUG_SV.pop(l_module_name);
3886       END IF;
3887       --
3888       RETURN;
3889    END IF;
3890 
3891    --
3892    -- Debug Statements
3893    --
3894    IF l_debug_on THEN
3895        WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_ACTIONS.ASSIGN_DELIVERIES',WSH_DEBUG_SV.C_PROC_LEVEL);
3896    END IF;
3897    --
3898    wsh_delivery_legs_actions.assign_deliveries(
3899                p_del_rows => x_del_rows,
3900                p_trip_id => p_trip_id,
3901                p_create_flag => 'Y',
3902                x_leg_rows => l_del_legs,
3903                x_return_status => x_return_status
3904                );
3905 
3906    IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR) OR
3907       (x_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
3908         --
3909         -- Debug Statements
3910         --
3911         IF l_debug_on THEN
3912             WSH_DEBUG_SV.pop(l_module_name);
3913         END IF;
3914         --
3915         RETURN;
3916    END IF;
3917 
3918 
3919 
3920 
3921                --
3922                -- Debug Statements
3923                --
3924                IF l_debug_on THEN
3925                    WSH_DEBUG_SV.pop(l_module_name);
3926                END IF;
3927                --
3928    EXCEPTION
3929      WHEN reprice_required_err THEN
3930          x_return_status := l_return_status;
3931          fnd_message.set_name('WSH', 'WSH_REPRICE_REQUIRED_ERR');
3932       wsh_util_core.add_message(x_return_status);
3933       --
3934       -- Debug Statements
3935       --
3936       IF l_debug_on THEN
3937           WSH_DEBUG_SV.logmsg(l_module_name,'REPRICE_REQUIRED_ERR exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
3938           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:REPRICE_REQUIRED_ERR');
3939       END IF;
3940       --
3941      WHEN others THEN
3942       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.ASSIGN_TRIP');
3943       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
3944 
3945 --
3946 -- Debug Statements
3947 --
3948 IF l_debug_on THEN
3949     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
3950     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
3951 END IF;
3952 --
3953 END assign_trip;
3954 
3955 
3956 PROCEDURE check_assign_trip (
3957       p_del_rows      IN    wsh_util_core.id_tab_type,
3958       p_trip_id       IN    NUMBER,
3959       p_pickup_stop_id  IN NUMBER := NULL,
3960       p_dropoff_stop_id  IN      NUMBER := NULL,
3961       p_pickup_location_id  IN     NUMBER := NULL,
3962       p_dropoff_location_id    IN    NUMBER := NULL,
3963       p_pickup_arr_date    IN    DATE := to_date(NULL),
3964       p_pickup_dep_date    IN    DATE := to_date(NULL),
3965       p_dropoff_arr_date     IN     DATE := to_date(NULL),
3966       p_dropoff_dep_date     IN     DATE := to_date(NULL),
3967       x_return_status    OUT   VARCHAR2) IS
3968 
3969 CURSOR pickup_leg_check (l_delivery_id IN NUMBER) IS
3970 SELECT dg.delivery_leg_id,
3971      st.stop_id
3972 FROM   wsh_trip_stops st,
3973       wsh_delivery_legs dg
3974 WHERE  dg.delivery_id = l_delivery_id AND
3975       dg.pick_up_stop_id = st.stop_id AND
3976       st.stop_location_id = p_pickup_location_id;
3977 
3978 CURSOR dropoff_leg_check (l_delivery_id IN NUMBER) IS
3979 SELECT dg.delivery_leg_id,
3980      st.stop_id
3981 FROM   wsh_trip_stops st,
3982       wsh_delivery_legs dg
3983 WHERE  dg.delivery_id = l_delivery_id AND
3984       dg.drop_off_stop_id = st.stop_id AND
3985       st.stop_location_id = p_dropoff_location_id;
3986 
3987 l_delivery_leg_id NUMBER;
3988 l_stop_id       NUMBER;
3989 
3990 num_warn      NUMBER := 0;
3991 num_error       NUMBER := 0;
3992 warn_flag       BOOLEAN;
3993 error_flag     BOOLEAN;
3994 
3995 --
3996 l_debug_on BOOLEAN;
3997 --
3998 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'CHECK_ASSIGN_TRIP';
3999 --
4000 -- patchset J csun Stop Sequence Change
4001 l_stop_details_rec WSH_TRIP_STOPS_VALIDATIONS.stop_details;
4002 l_return_status    VARCHAR2(1);
4003 
4004 BEGIN
4005 
4006   --
4007   -- Debug Statements
4008   --
4009   --
4010   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
4011   --
4012   IF l_debug_on IS NULL
4013   THEN
4014       l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
4015   END IF;
4016   --
4017   IF l_debug_on THEN
4018       WSH_DEBUG_SV.push(l_module_name);
4019       --
4020       WSH_DEBUG_SV.log(l_module_name,'P_TRIP_ID',P_TRIP_ID);
4021       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_STOP_ID',P_PICKUP_STOP_ID);
4022       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_STOP_ID',P_DROPOFF_STOP_ID);
4023       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_LOCATION_ID',P_PICKUP_LOCATION_ID);
4024       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_LOCATION_ID',P_DROPOFF_LOCATION_ID);
4025       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_ARR_DATE',P_PICKUP_ARR_DATE);
4026       -- Pack J csun Stop Sequence Change
4027       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_DEP_DATE',P_PICKUP_DEP_DATE);
4028       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_ARR_DATE',P_DROPOFF_ARR_DATE);
4029       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_DEP_DATE',P_DROPOFF_DEP_DATE);
4030   END IF;
4031   --
4032   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
4033 
4034   IF (p_pickup_location_id = p_dropoff_location_id) THEN
4035     FND_MESSAGE.SET_NAME('WSH','WSH_STOP_LOCATIONS_SAME');
4036     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4037    wsh_util_core.add_message(x_return_status);
4038     --
4039     -- Debug Statements
4040     --
4041     IF l_debug_on THEN
4042         WSH_DEBUG_SV.pop(l_module_name);
4043     END IF;
4044     --
4045     RETURN;
4046   END IF;
4047 
4048 -- Loop through all deliveries
4049 
4050   FOR i IN 1..p_del_rows.count LOOP
4051 
4052    warn_flag := FALSE;
4053    error_flag := FALSE;
4054 
4055    l_delivery_leg_id := NULL;
4056    l_stop_id := NULL;
4057 
4058    OPEN pickup_leg_check (p_del_rows(i));
4059    FETCH pickup_leg_check INTO l_delivery_leg_id, l_stop_id;
4060    CLOSE pickup_leg_check;
4061 
4062    IF (l_delivery_leg_id IS NOT NULL) THEN
4063 
4064       FND_MESSAGE.SET_NAME('WSH','WSH_DEL_MULTIPLE_LEGS');
4065      --
4066      -- Debug Statements
4067      --
4068      IF l_debug_on THEN
4069          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_NEW_DELIVERIES_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
4070      END IF;
4071      --
4072      FND_MESSAGE.SET_TOKEN('DEL_NAME',wsh_new_deliveries_pvt.get_name(p_del_rows(i)));
4073       x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
4074      wsh_util_core.add_message(x_return_status);
4075      warn_flag := TRUE;
4076 
4077       --
4078       -- Debug Statements
4079       --
4080       IF l_debug_on THEN
4081           WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_PVT.DELETE_DELIVERY_LEG',WSH_DEBUG_SV.C_PROC_LEVEL);
4082       END IF;
4083       --
4084       wsh_delivery_legs_pvt.delete_delivery_leg(NULL, l_delivery_leg_id, x_return_status);
4085 
4086    END IF;
4087 
4088    l_delivery_leg_id := NULL;
4089    l_stop_id := NULL;
4090 
4091    OPEN dropoff_leg_check (p_del_rows(i));
4092    FETCH dropoff_leg_check INTO l_delivery_leg_id, l_stop_id;
4093    CLOSE dropoff_leg_check;
4094 
4095    IF (l_delivery_leg_id IS NOT NULL) THEN
4096 
4097       FND_MESSAGE.SET_NAME('WSH','WSH_DEL_MULTIPLE_LEGS');
4098      --
4099      -- Debug Statements
4100      --
4101      IF l_debug_on THEN
4102          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_NEW_DELIVERIES_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
4103      END IF;
4104      --
4105      FND_MESSAGE.SET_TOKEN('DEL_NAME',wsh_new_deliveries_pvt.get_name(p_del_rows(i)));
4106       x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
4107      wsh_util_core.add_message(x_return_status);
4108      warn_flag := TRUE;
4109 
4110       --
4111       -- Debug Statements
4112       --
4113       IF l_debug_on THEN
4114           WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_PVT.DELETE_DELIVERY_LEG',WSH_DEBUG_SV.C_PROC_LEVEL);
4115       END IF;
4116       --
4117       wsh_delivery_legs_pvt.delete_delivery_leg(NULL, l_delivery_leg_id, x_return_status);
4118 
4119    END IF;
4120 
4121    -- Security rule check for assigning
4122 
4123    --
4124    -- Debug Statements
4125    --
4126    IF l_debug_on THEN
4127        WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_VALIDATIONS.CHECK_ASSIGN_TRIP',WSH_DEBUG_SV.C_PROC_LEVEL);
4128    END IF;
4129    --
4130    wsh_delivery_validations.check_assign_trip(
4131       p_delivery_id => p_del_rows(i),
4132       p_trip_id => p_trip_id,
4133       p_pickup_stop_id => p_pickup_stop_id,
4134       p_dropoff_stop_id => p_dropoff_stop_id,
4135       x_return_status => x_return_status);
4136 
4137    IF (x_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN
4138      IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING) THEN
4139       num_warn := num_warn + 1;
4140       ELSE
4141        --
4142        -- Debug Statements
4143        --
4144        IF l_debug_on THEN
4145            WSH_DEBUG_SV.pop(l_module_name);
4146        END IF;
4147        --
4148        RETURN;
4149       END IF;
4150    END IF;
4151 
4152    IF (error_flag) THEN
4153       num_error := num_error + 1;
4154    ELSIF (warn_flag) THEN
4155       num_warn := num_warn + 1;
4156    END IF;
4157 
4158   END LOOP;
4159 
4160   IF (num_warn > 0) OR (num_error > 0) THEN
4161    x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
4162   END IF;
4163 
4164 --
4165 -- Debug Statements
4166 --
4167 IF l_debug_on THEN
4168     WSH_DEBUG_SV.pop(l_module_name);
4169 END IF;
4170 --
4171   EXCEPTION
4172      WHEN others THEN
4173       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.CHECK_ASSIGN_TRIP');
4174       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
4175 
4176 --
4177 -- Debug Statements
4178 --
4179 IF l_debug_on THEN
4180     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
4181     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
4182 END IF;
4183 --
4184 END check_assign_trip;
4185 
4186 --
4187 --  Procedure:   process_dels_for_internal_locs
4188 --  Parameters:
4189 --               p_del_rows                                   list of deliveries to be assigned
4190 --               p_trip_id                                    trip to be assigned to
4191 --               p_pickup_stop_id, p_dropoff_stop_id          Parameters for assign trip
4192 --               p_pickup_location_id, p_dropoff_location_id  Parameters for assign trip
4193 --               x_pickup_stop_id                             Modified (if any) pickup_stop_id
4194 --               x_dropoff_stop_id                            Modified (if any) dropoff_stop_id
4195 --               x_pickup_location_id                         Modified (if any) pickup_location_id
4196 --               x_dropoff_location_id                        Modified (if any) dropoff_location_id
4197 --               x_dropoff_seq_num                            Populated if needed for SSN mode
4198 --               x_return_status                              return status
4199 --
4200 --  Description:
4201 --               Should be called only when user tries to assign delivery to trip
4202 --               Autocreate trip should not call this
4203 --               Process deliveries and trip to be assigned for internal
4204 --               locations changes and if need be convert the pickup
4205 --               stop/location and the dropoff stop/location
4206 --               PICKUP
4207 --               If pickup stop is chosen from assign-trip window, check
4208 --               whether it is a internal location and caller is TP release
4209 --               or FTE.If so, and if there is no other dropoff or pickup
4210 --               at this location, convert the location for this stop to
4211 --               be the physical location . WSH caller should not be able to
4212 --               use a dummy location for pickup. If there is a dropoff at
4213 --               this location, create a new stop with the physical location
4214 --               and use this for pickup
4215 --               DROPOFF
4216 --               If dropoff stop is chosen from assign-trip window, check whether delivery's
4217 --               dropoff is a internal location and
4218 --               if the physical location corresponding to the internal  loc (del's dropoff)
4219 --               is same as the dropoff chosen from assign-trip window, and if there is no
4220 --               other delivery getting dropoff or pickedup at the stop's location, convert the location for
4221 --               this stop to be the dummy location . Else create new dropoff stop with dummy loc
4222 
4223 PROCEDURE process_dels_for_internal_locs(
4224       p_del_rows             IN    wsh_util_core.id_tab_type,
4225       p_trip_id              IN    NUMBER,
4226       p_pickup_stop_id       IN    NUMBER := NULL,
4227       p_dropoff_stop_id      IN    NUMBER := NULL,
4228       p_pickup_location_id   IN    NUMBER := NULL,
4229       p_dropoff_location_id  IN    NUMBER := NULL,
4230       p_caller               IN    VARCHAR2,
4231       x_return_status        OUT NOCOPY VARCHAR2,
4232       x_pickup_stop_id       OUT NOCOPY NUMBER,
4233       x_dropoff_stop_id      OUT NOCOPY NUMBER,
4234       x_pickup_location_id   OUT NOCOPY NUMBER,
4235       x_dropoff_location_id  OUT NOCOPY NUMBER,
4236       x_dropoff_seq_num      IN OUT NOCOPY NUMBER,
4237       x_internal_del_ids     OUT NOCOPY wsh_util_core.id_tab_type,
4238       x_del_ids              OUT NOCOPY wsh_util_core.id_tab_type
4239 )
4240 IS
4241   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'process_dels_for_internal_locs';
4242   --
4243   l_debug_on BOOLEAN;
4244 
4245 CURSOR get_stop_location (l_trip_stop_id IN NUMBER) IS
4246 SELECT  stop_location_id
4247 FROM   wsh_trip_stops
4248 WHERE stop_id = l_trip_stop_id;
4249 
4250 CURSOR c_any_activity_exists(p_stop_id IN NUMBER) IS
4251 SELECT '1'
4252 FROM wsh_delivery_legs wdl
4253 WHERE (wdl.pick_up_stop_id=p_stop_id OR wdl.drop_off_stop_id=p_stop_id)
4254 AND rownum=1;
4255 
4256 CURSOR c_get_stops IS
4257 SELECT stop_id, stop_location_id, status_code
4258 FROM wsh_trip_stops
4259 WHERE trip_id=p_trip_id
4260 ORDER BY stop_sequence_number;
4261 
4262 CURSOR delivery_info (del_id IN NUMBER) IS
4263 SELECT  initial_pickup_location_id,
4264 ultimate_dropoff_location_id
4265 FROM    wsh_new_deliveries
4266 WHERE delivery_id = del_id;
4267 
4268 CURSOR C_DEL_PHYS_DROPOFF (del_id IN NUMBER, loc_id IN NUMBER) IS
4269 SELECT  'Y'
4270 FROM    wsh_new_deliveries
4271 WHERE delivery_id = del_id
4272 AND ultimate_dropoff_location_id=loc_id;
4273 
4274 l_phys_trip_pickup_loc_id   NUMBER;
4275 l_trip_pickup_loc_id        NUMBER;
4276 l_trip_dropoff_loc_id       NUMBER;
4277 l_physical_stop_id          NUMBER;
4278 l_find_stop                 NUMBER;
4279 l_trip_ids                  wsh_util_core.id_tab_type;
4280 l_dummy_trip_ids            wsh_util_core.id_tab_type;
4281 l_getstops_stop_id          wsh_util_core.id_tab_type;
4282 l_getstops_stop_loc_id      wsh_util_core.id_tab_type;
4283 l_getstops_status_code      WSH_UTIL_CORE.Column_Tab_Type;
4284 l_is_stop_pickup            VARCHAR2(1):='N';
4285 l_is_stop_dropoff           VARCHAR2(1):='N';
4286 l_find_leg                  VARCHAR2(1);
4287 b_physical_loc_updated      BOOLEAN:=FALSE;
4288 l_phys_del_pickup_loc_id    NUMBER;
4289 l_phys_del_dropoff_loc_id   NUMBER;
4290 l_del_rows_count            NUMBER:=0;
4291 l_phys_trip_dropoff_loc_id  NUMBER;
4292 l_stop_rec_physical_loc_id  NUMBER;
4293 l_return_status             VARCHAR2(30);
4294 l_pickup_location_id        NUMBER;
4295 l_dropoff_location_id       NUMBER;
4296 l_pickup_stop_id            NUMBER;
4297 l_dropoff_stop_id           NUMBER;
4298 l_num_error                 NUMBER := 0;
4299 l_num_warn                  NUMBER := 0;
4300 bad_trip_stop               EXCEPTION;
4301 l_internal_del_id_count     NUMBER := 0;
4302 l_del_id_count              NUMBER := 0;
4303 l_internal_del_ids          wsh_util_core.id_tab_type;
4304 l_del_ids                   wsh_util_core.id_tab_type;
4305 b_checkstopupdate           BOOLEAN:=TRUE;
4306 b_checkdels                 BOOLEAN:=FALSE;
4307 b_gotstops                  BOOLEAN:=FALSE;
4308 l_del_phys_dropoff          VARCHAR2(1);
4309 
4310    FUNCTION derive_next_ssn(p_trip_id IN NUMBER,
4311                             p_stop_id IN NUMBER) RETURN NUMBER IS
4312      CURSOR c_stop_seqs IS
4313          -- get the SSNs of this stop and the next stop.
4314          SELECT ts2.stop_sequence_number
4315          FROM   wsh_trip_stops ts1, -- get this stop's SSN
4316                 wsh_trip_stops ts2  -- then look for SSN and next SSN
4317          WHERE  ts1.stop_id = p_stop_id
4318          AND    ts2.trip_id = p_trip_id
4319          AND    ts2.stop_sequence_number >= ts1.stop_sequence_number
4320          AND    rownum <= 2
4321          ORDER BY ts2.stop_sequence_number;
4322      l_ssn NUMBER;
4323    BEGIN
4324      IF l_debug_on THEN
4325        WSH_DEBUG_SV.log(l_module_name, 'entering internal function derive_next_ssn, p_stop_id ', p_stop_id);
4326      END IF;
4327 
4328      l_ssn := NULL;
4329 
4330      -- first item is the linking stop
4331      -- second item is the next row if it exists.
4332      FOR s IN c_stop_seqs LOOP
4333         IF l_ssn IS NULL THEN
4334             l_ssn := s.stop_sequence_number + 1;
4335         ELSE
4336             IF l_ssn > s.stop_sequence_number THEN
4337               -- to ensure uniqueness and correct sequence,
4338               -- let us take the average which may have decimal places.
4339               l_ssn := ( (l_ssn-1) + s.stop_sequence_number) / 2;
4340             END IF;
4341         END IF;
4342      END LOOP;
4343 
4344      IF l_debug_on THEN
4345        WSH_DEBUG_SV.log(l_module_name, 'exiting internal function derive_next_ssn, returning l_ssn', l_ssn);
4346      END IF;
4347      RETURN l_ssn;
4348 
4349    EXCEPTION
4350       WHEN OTHERS THEN
4351           IF l_debug_on THEN
4352             WSH_DEBUG_SV.log(l_module_name, 'exiting internal function derive_next_ssn, unhandled exception');
4353           END IF;
4354           RETURN 0;
4355    END;
4356 
4357 BEGIN
4358   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
4359   --
4360   IF l_debug_on IS NULL THEN
4361     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
4362   END IF;
4363 
4364   IF l_debug_on THEN
4365      WSH_DEBUG_SV.push(l_module_name);
4366      WSH_DEBUG_SV.log(l_module_name, 'p_caller', p_caller);
4367      WSH_DEBUG_SV.log(l_module_name, 'p_pickup_stop_id', p_pickup_stop_id);
4368      WSH_DEBUG_SV.log(l_module_name, 'p_pickup_location_id', p_pickup_location_id);
4369      WSH_DEBUG_SV.log(l_module_name, 'p_dropoff_stop_id', p_dropoff_stop_id);
4370      WSH_DEBUG_SV.log(l_module_name, 'p_dropoff_location_id', p_dropoff_location_id);
4371   END IF;
4372   --
4373   --
4374   x_return_status        :=WSH_UTIL_CORE.G_RET_STS_SUCCESS;
4375   x_pickup_stop_id       :=p_pickup_stop_id;
4376   x_dropoff_stop_id      :=p_dropoff_stop_id;
4377   x_pickup_location_id   :=p_pickup_location_id;
4378   x_dropoff_location_id  :=p_dropoff_location_id;
4379 
4380    --1. Process the trip data
4381    IF (p_pickup_stop_id IS NOT NULL) THEN
4382       OPEN get_stop_location (p_pickup_stop_id);
4383       FETCH get_stop_location INTO l_trip_pickup_loc_id;
4384       IF (get_stop_location%NOTFOUND) THEN
4385          RAISE bad_trip_stop;
4386       END IF;
4387       CLOSE get_stop_location;
4388    END IF;
4389 
4390    IF (p_dropoff_stop_id IS NOT NULL) THEN
4391       OPEN get_stop_location (p_dropoff_stop_id);
4392       FETCH get_stop_location INTO l_trip_dropoff_loc_id;
4393       IF (get_stop_location%NOTFOUND) THEN
4394          RAISE bad_trip_stop;
4395       END IF;
4396       CLOSE get_stop_location;
4397    END IF;
4398 
4399    IF p_pickup_location_id IS NOT NULL THEN
4400       l_trip_pickup_loc_id := p_pickup_location_id;
4401    END IF;
4402    IF p_dropoff_location_id IS NOT NULL THEN
4403       l_trip_dropoff_loc_id := p_dropoff_location_id;
4404    END IF;
4405 
4406    --get the physical locations
4407    IF (p_pickup_stop_id IS NOT NULL OR p_pickup_location_id IS NOT NULL) THEN
4408              WSH_LOCATIONS_PKG.Convert_internal_cust_location(
4409                p_internal_cust_location_id   => l_trip_pickup_loc_id,
4410                x_internal_org_location_id    => l_phys_trip_pickup_loc_id,
4411                x_return_status               => l_return_status);
4412          IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
4413             x_return_status:=l_return_status;
4414             RETURN;
4415          END IF;
4416          IF l_debug_on THEN
4417                WSH_DEBUG_SV.log(l_module_name,'l_phys_trip_pickup_loc_id',l_phys_trip_pickup_loc_id);
4418          END IF;
4419    END IF;
4420 
4421    --b) only in ASSIGN-TRIP cases we need to check for internal locs
4422    --If pickup stop is chosen from assign-trip window, check whether it is a
4423    --internal location and caller is TP release or FTE.If so, and if there
4424    --is no other dropoff or pickup at this location, convert the location for
4425    --this stop to be the physical location . WSH caller should not be able to
4426    --use a dummy location for pickup
4427    IF (p_pickup_stop_id IS NOT NULL OR p_pickup_location_id IS NOT NULL)
4428       AND l_phys_trip_pickup_loc_id IS NOT NULL THEN --which means internal loc
4429 
4430             IF (nvl(p_caller,'@@@') like 'FTE%'
4431                 OR nvl(p_caller,'@@@') like 'WSH_TP_RELEASE%'
4432                ) THEN
4433 
4434                IF p_pickup_stop_id IS NOT NULL THEN
4435                   l_physical_stop_id:=null;
4436 
4437                   --find if prev. or next stop matches physical loc.
4438                   OPEN c_get_stops;
4439                   FETCH c_get_stops BULK COLLECT INTO l_getstops_stop_id,
4440                                         l_getstops_stop_loc_id,
4441                                         l_getstops_status_code;
4442                   CLOSE c_get_stops;
4443                   b_gotstops:=TRUE;
4444 
4445                   IF l_getstops_stop_id IS NOT NULL AND l_getstops_stop_id.count > 0 THEN
4446                      FOR j in l_getstops_stop_id.first .. l_getstops_stop_id.last LOOP
4447                         IF p_pickup_stop_id=l_getstops_stop_id(j) THEN
4448                            IF j>l_getstops_stop_id.first AND l_phys_trip_pickup_loc_id=l_getstops_stop_loc_id(j-1)
4449                               AND l_getstops_status_code(j-1) IN ('OP','AR') THEN
4450                                l_physical_stop_id:=l_getstops_stop_id(j-1);
4451                                EXIT;
4452                            ELSIF j<l_getstops_stop_id.last AND l_phys_trip_pickup_loc_id=l_getstops_stop_loc_id(j+1)
4453                                  AND l_getstops_status_code(j+1) IN ('OP','AR') THEN
4454                                l_physical_stop_id:=l_getstops_stop_id(j+1);
4455                                EXIT;
4456                            END IF;
4457                         END IF;
4458                      END LOOP;
4459                   END IF;
4460 
4461                   IF l_physical_stop_id IS NOT NULL THEN
4462                      --if we find a stop with same physical location in the
4463                      --trip, we use that for pickup
4464                      IF l_debug_on THEN
4465                        WSH_DEBUG_SV.log(l_module_name,'Caller is FTE/TP using pickup stop l_physical_stop_id',l_physical_stop_id);
4466                      END IF;
4467                      x_pickup_stop_id:=l_physical_stop_id;
4468                   ELSE  --l_physical_stop_id IS NULL
4469                      --make pickup loc = l_phys_trip_pickup_loc_id if there
4470                      --is no dropoff or pickup at this dummy stop
4471                      OPEN c_any_activity_exists (p_pickup_stop_id);
4472                      FETCH c_any_activity_exists INTO l_find_leg;
4473 
4474                      IF c_any_activity_exists%NOTFOUND THEN
4475                         IF l_debug_on THEN
4476                            WSH_DEBUG_SV.logmsg(l_module_name,'Caller is FTE/TP changing pickup loc to be l_phys_trip_pickup_loc_id');
4477                         END IF;
4478 
4479                         UPDATE wsh_trip_stops
4480                         SET stop_location_id= l_phys_trip_pickup_loc_id,
4481                             physical_location_id=null,
4482                             last_update_date      = sysdate,
4483                             last_updated_by       = FND_GLOBAL.USER_ID
4484                         WHERE stop_id=p_pickup_stop_id;
4485 
4486                         --set the l_trip_pickup_loc_id to point to the new value
4487                         x_pickup_location_id:=l_phys_trip_pickup_loc_id;
4488                      ELSE
4489                         --there is a leg associated with internal stop and user is trying to use this for pickup as well.
4490                         --in this case we create new stop with l_phys_trip_pickup_loc_id
4491                         IF l_debug_on THEN
4492                            WSH_DEBUG_SV.logmsg(l_module_name,'Caller is FTE/TP changing pickup stop to create new pickup stop with l_phys_trip_pickup_loc_id');
4493                         END IF;
4494                         x_pickup_stop_id:=null;
4495                         x_pickup_location_id:=l_phys_trip_pickup_loc_id;
4496                      END IF;
4497 
4498                      CLOSE c_any_activity_exists;
4499 
4500                   END IF;--l_physical_stop_id IS NOT NULL
4501 
4502                ELSE --p_pickup_stop_id is null
4503                   --set l_pickup_location_id as the physical location so that this loc will be used to create stop
4504                   IF l_debug_on THEN
4505                       WSH_DEBUG_SV.logmsg(l_module_name,'Caller is FTE/TP create new pickup stop with l_phys_trip_pickup_loc_id');
4506                   END IF;
4507                   x_pickup_location_id:=l_phys_trip_pickup_loc_id;
4508                END IF;
4509 
4510             ELSE--caller is WSH
4511                x_return_status:=WSH_UTIL_CORE.G_RET_STS_ERROR;
4512                FND_MESSAGE.SET_NAME('WSH','WSH_CANNOT_USE_DUMMY_PICKUP');
4513                IF l_debug_on THEN
4514               WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_UTIL_CORE.get_location_description',WSH_DEBUG_SV.C_PROC_LEVEL);
4515                END IF;
4516                FND_MESSAGE.SET_TOKEN('STOP_NAME',SUBSTR(WSH_UTIL_CORE.get_location_description(l_phys_trip_pickup_loc_id, 'NEW UI CODE'),1,60));
4517                wsh_util_core.add_message(x_return_status,l_module_name);
4518                IF l_debug_on THEN
4519               WSH_DEBUG_SV.pop(l_module_name);
4520                END IF;
4521                RETURN;
4522             END IF;
4523    END IF; --p_pickpu_stop_id is not null
4524 
4525 
4526   --2. Process the deliveries
4527    FOR i IN 1..p_del_rows.count LOOP
4528 
4529      IF l_debug_on THEN
4530         wsh_debug_sv.log(l_module_name, 'p_del_rows(i)', p_del_rows(i));
4531      END IF;
4532 
4533       l_pickup_location_id := NULL;
4534       l_dropoff_location_id := NULL;
4535 
4536       -- Fetch delivery information including initial_pickup_location,
4537       -- ultimate_dropoff_location
4538       OPEN delivery_info (p_del_rows(i));
4539       FETCH delivery_info INTO l_pickup_location_id, l_dropoff_location_id;
4540       CLOSE delivery_info;
4541 
4542      --
4543      IF l_debug_on THEN
4544         wsh_debug_sv.log(l_module_name, 'l_pickup_location_id', l_pickup_location_id);
4545         wsh_debug_sv.log(l_module_name, 'l_dropoff_location_id', l_dropoff_location_id);
4546      END IF;
4547 
4548 
4549     --Check if del's pickup and dropoff are same location
4550 
4551     l_phys_del_dropoff_loc_id:=null;
4552 
4553     WSH_LOCATIONS_PKG.Convert_internal_cust_location(
4554                p_internal_cust_location_id   => l_dropoff_location_id,
4555                x_internal_org_location_id    => l_phys_del_dropoff_loc_id,
4556                x_return_status               => l_return_status);
4557 
4558     IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
4559             x_return_status:=l_return_status;
4560             RETURN;
4561     END IF;
4562 
4563 
4564 -- J Locations - only in ASSIGN-TRIP cases we need to check for internal locs
4565 -- If dropoff stop is chosen from assign-trip window, check whether delivery's
4566 -- dropoff is a internal location and
4567 -- if the physical location corresponding to the internal  loc (del's dropoff)
4568 -- is same as the dropoff chosen from assign-trip window, and if there is no
4569 -- other dropoff or pickup at the stop's location, convert the location for
4570 -- this stop to be the dummy location .
4571    IF (p_dropoff_stop_id IS NOT NULL OR p_dropoff_location_id IS NOT NULL) THEN
4572 
4573          l_find_leg:=null;
4574 
4575          --physical loc of del corresponds to dropoff stop for trip chosen,
4576          --convert stop with l_trip_dropoff_loc_id as stop loc to be stop with l_dropoff_loc_id as
4577          --stop_loc if no other activity is taking place in stop
4578          --if for a set of deliveries, internal stop is identified and changed per logic below,
4579          --we do not need to repeat the steps again for later deliveries
4580 
4581          IF l_phys_del_dropoff_loc_id IS NOT NULL AND l_phys_del_dropoff_loc_id=l_trip_dropoff_loc_id THEN
4582                    IF l_debug_on THEN
4583                       WSH_DEBUG_SV.log(l_module_name,'l_phys_del_dropoff_loc_id',l_phys_del_dropoff_loc_id);
4584                    END IF;
4585                    l_find_stop:=null;
4586                    l_internal_del_id_count:=l_internal_del_id_count+1;
4587                    l_internal_del_ids(l_internal_del_id_count):=p_del_rows(i);
4588                    --find if prev. or next stop matches del's dropoff loc.
4589                    IF NOT(b_gotstops) THEN
4590                       OPEN c_get_stops;
4591                       FETCH c_get_stops BULK COLLECT INTO l_getstops_stop_id,
4592                                         l_getstops_stop_loc_id,
4593                                         l_getstops_status_code;
4594                       CLOSE c_get_stops;
4595                       b_gotstops:=TRUE;
4596                    END IF;
4597 
4598                    IF l_getstops_stop_id IS NOT NULL AND l_getstops_stop_id.count > 0 THEN
4599                      FOR j in l_getstops_stop_id.first .. l_getstops_stop_id.last LOOP
4600                         IF p_dropoff_stop_id=l_getstops_stop_id(j) OR p_dropoff_location_id=l_getstops_stop_loc_id(j) THEN
4601                            IF j>l_getstops_stop_id.first AND l_dropoff_location_id=l_getstops_stop_loc_id(j-1)
4602                               AND l_getstops_status_code(j-1) IN ('OP') THEN
4603                                l_find_stop:=l_getstops_stop_id(j-1);
4604                                EXIT;
4605                            ELSIF j<l_getstops_stop_id.last AND l_dropoff_location_id=l_getstops_stop_loc_id(j+1)
4606                                  AND l_getstops_status_code(j+1) IN ('OP') THEN
4607                                l_find_stop:=l_getstops_stop_id(j+1);
4608                                EXIT;
4609                            END IF;
4610                         END IF;
4611                      END LOOP;
4612                    END IF;
4613 
4614 
4615                    --see if you can find stop with del's dropoff loc
4616                    IF l_find_stop IS NOT NULL THEN
4617                       IF l_debug_on THEN
4618                          WSH_DEBUG_SV.log(l_module_name,'existing stop used as dropoff',l_find_stop);
4619                       END IF;
4620                       x_dropoff_stop_id:=l_find_stop;
4621                       --set the l_trip_dropoff_loc_id to point to the new value
4622                       x_dropoff_location_id:=l_dropoff_location_id;
4623                    ELSE
4624                       IF p_dropoff_stop_id IS NOT NULL THEN
4625                          OPEN c_any_activity_exists (p_dropoff_stop_id);
4626                          FETCH c_any_activity_exists INTO l_find_leg;
4627                          IF c_any_activity_exists%NOTFOUND THEN
4628                             --change location only if other dels do not have same physical dropoff
4629                             IF NOT(b_checkdels) THEN
4630                                 FOR k IN 1..p_del_rows.count LOOP
4631                                    OPEN c_del_phys_dropoff(p_del_rows(k), l_trip_dropoff_loc_id);
4632                                    FETCH c_del_phys_dropoff INTO l_del_phys_dropoff;
4633                                    IF c_del_phys_dropoff%FOUND THEN
4634                                       b_checkstopupdate:=FALSE;
4635                                       CLOSE c_del_phys_dropoff;
4636                                       EXIT;
4637                                    END IF;
4638                                    CLOSE c_del_phys_dropoff;
4639                                 END LOOP;
4640                                 b_checkdels:=TRUE;
4641                             END IF;
4642 
4643                             IF NOT(b_checkstopupdate) THEN
4644                                IF l_debug_on THEN
4645                                   WSH_DEBUG_SV.log(l_module_name,'Have to create new internal stop as del with physical dropoff has been passed',l_dropoff_location_id);
4646                                END IF;
4647 
4648                                IF  WSH_TRIPS_ACTIONS.GET_STOP_SEQ_MODE = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN THEN
4649                                   x_dropoff_seq_num := derive_next_ssn(p_trip_id, x_dropoff_stop_id);
4650                                   IF l_debug_on THEN
4651                                   WSH_DEBUG_SV.log(l_module_name,'x_dropoff_seq_num adjusted', x_dropoff_seq_num);
4652                                  END IF;
4653                                END IF; -- SSN mode
4654 
4655                                x_dropoff_stop_id:=null;
4656                                x_dropoff_location_id:=l_dropoff_location_id;
4657                             ELSE
4658                                IF l_debug_on THEN
4659                                   WSH_DEBUG_SV.log(l_module_name,'changing stop location to internal location l_dropoff_location_id',l_dropoff_location_id);
4660                                END IF;
4661 
4662                                UPDATE wsh_trip_stops
4663                                SET stop_location_id= l_dropoff_location_id,
4664                                    physical_location_id=l_trip_dropoff_loc_id,
4665                                    last_update_date      = sysdate,
4666                                    last_updated_by       = FND_GLOBAL.USER_ID
4667                                WHERE stop_id=p_dropoff_stop_id;
4668 
4669                                --set the l_trip_dropoff_loc_id to point to the new value
4670                                x_dropoff_location_id:=l_dropoff_location_id;
4671                             END IF;
4672                          ELSE
4673                             IF l_debug_on THEN
4674                                WSH_DEBUG_SV.log(l_module_name,'Have to create new internal stop as existing physical stop has activities',l_dropoff_location_id);
4675                             END IF;
4676 
4677                             IF  WSH_TRIPS_ACTIONS.GET_STOP_SEQ_MODE = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN THEN
4678                               x_dropoff_seq_num := derive_next_ssn(p_trip_id, x_dropoff_stop_id);
4679                               IF l_debug_on THEN
4680                                WSH_DEBUG_SV.log(l_module_name,'x_dropoff_seq_num adjusted', x_dropoff_seq_num);
4681                               END IF;
4682                             END IF;
4683 
4684                             x_dropoff_stop_id:=null;
4685                             x_dropoff_location_id:=l_dropoff_location_id;
4686                          END IF;
4687                          CLOSE c_any_activity_exists;
4688                       ELSE--p_dropoff_location_id is not null
4689                             x_dropoff_stop_id:=null;
4690                             x_dropoff_location_id:=l_dropoff_location_id;
4691                       END IF;
4692                    END IF;--l_find_stop
4693 
4694          ELSE  --regular deliveries
4695                    l_del_id_count:=l_del_id_count+1;
4696                    l_del_ids(l_del_id_count):=p_del_rows(i);
4697          END IF;--l_phys_del_dropoff_loc_id is not null
4698     ELSE  --regular deliveries
4699         l_del_id_count:=l_del_id_count+1;
4700         l_del_ids(l_del_id_count):=p_del_rows(i);
4701     END IF;--p_dropoff_stop_id is not null
4702    END LOOP;
4703 
4704   IF l_debug_on THEN
4705      WSH_DEBUG_SV.log(l_module_name, 'x_del_ids count', l_del_id_count);
4706      WSH_DEBUG_SV.log(l_module_name, 'x_internal_del_ids count', l_internal_del_id_count);
4707   END IF;
4708 
4709  x_internal_del_ids:=l_internal_del_ids;
4710  x_del_ids:=l_del_ids;
4711 
4712   --
4713   --
4714   IF l_debug_on THEN
4715      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
4716      WSH_DEBUG_SV.log(l_module_name, 'x_pickup_stop_id', x_pickup_stop_id);
4717      WSH_DEBUG_SV.log(l_module_name, 'x_pickup_location_id', x_pickup_location_id);
4718      WSH_DEBUG_SV.log(l_module_name, 'x_dropoff_stop_id', x_dropoff_stop_id);
4719      WSH_DEBUG_SV.log(l_module_name, 'x_dropoff_location_id', x_dropoff_location_id);
4720      WSH_DEBUG_SV.pop(l_module_name);
4721   END IF;
4722 
4723   EXCEPTION
4724     WHEN bad_trip_stop THEN
4725       IF get_stop_location%isopen THEN
4726         close get_stop_location;
4727       END IF;
4728       FND_MESSAGE.SET_NAME('WSH','WSH_STOP_NOT_FOUND');
4729       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4730       wsh_util_core.add_message(x_return_status);
4731       --
4732       -- Debug Statements
4733       --
4734       IF l_debug_on THEN
4735          WSH_DEBUG_SV.logmsg(l_module_name,'BAD_TRIP_STOP exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
4736          WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:BAD_TRIP_STOP');
4737       END IF;
4738       --
4739     WHEN OTHERS THEN
4740       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
4741       WSH_UTIL_CORE.DEFAULT_HANDLER(
4742                         'WSH_TRIPS_ACTIONS.process_dels_for_internal_locs',
4743                         l_module_name);
4744 
4745     IF l_debug_on THEN
4746        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
4747        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
4748     END IF;
4749 
4750 END process_dels_for_internal_locs;
4751 
4752 PROCEDURE assign_trip(
4753       p_del_rows      IN    wsh_util_core.id_tab_type,
4754       p_trip_id       IN    NUMBER,
4755       p_pickup_stop_id  IN NUMBER := NULL,
4756       p_pickup_stop_seq IN NUMBER := NULL,
4757       p_dropoff_stop_id  IN      NUMBER := NULL,
4758       p_dropoff_stop_seq    IN      NUMBER := NULL,
4759       p_pickup_location_id  IN     NUMBER := NULL,
4760       p_dropoff_location_id    IN    NUMBER := NULL,
4761       p_pickup_arr_date    IN    DATE := to_date(NULL),
4762       p_pickup_dep_date    IN    DATE := to_date(NULL),
4763       p_dropoff_arr_date     IN     DATE := to_date(NULL),
4764       p_dropoff_dep_date     IN     DATE := to_date(NULL),
4765       x_return_status    OUT   VARCHAR2,
4766       p_caller        IN      VARCHAR2
4767       ) IS
4768 
4769 CURSOR get_pickup_stop (l_loc_id IN NUMBER, l_del_id IN NUMBER) IS
4770 SELECT st.stop_id
4771 FROM   wsh_trip_stops st,
4772      wsh_delivery_legs dg
4773 WHERE  st.stop_location_id = l_loc_id AND
4774      dg.pick_up_stop_id = st.stop_id AND
4775      dg.delivery_id = l_del_id;
4776 
4777 CURSOR get_dropoff_stop (l_loc_id IN NUMBER, l_del_id IN NUMBER) IS
4778 SELECT st.stop_id
4779 FROM   wsh_trip_stops st,
4780      wsh_delivery_legs dg
4781 WHERE  st.stop_location_id = l_loc_id AND
4782      dg.drop_off_stop_id = st.stop_id AND
4783      dg.delivery_id = l_del_id;
4784 
4785 l_del_legs wsh_util_core.id_tab_type;
4786 l_stop_id       NUMBER;
4787 l_return_status   VARCHAR2(1);
4788 
4789 stop_not_found EXCEPTION;
4790 others EXCEPTION;
4791 /* H integration  for Multi Leg */
4792   l_stop_rec   WSH_TRIP_STOPS_PVT.TRIP_STOP_REC_TYPE;
4793   l_pub_stop_rec  WSH_TRIP_STOPS_PUB.TRIP_STOP_PUB_REC_TYPE;
4794   l_trip_rec   WSH_TRIPS_PVT.TRIP_REC_TYPE;
4795   l_pub_trip_rec  WSH_TRIPS_PUB.TRIP_PUB_REC_TYPE;
4796   l_num_warn  NUMBER := 0;
4797   l_num_err   NUMBER := 0;
4798   --l_return_status VARCHAR2(30);
4799 
4800 --
4801 l_debug_on BOOLEAN;
4802 --
4803 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'ASSIGN_TRIP';
4804 --
4805 -- patchset J csun Stop Sequence Change
4806 l_stop_details_rec WSH_TRIP_STOPS_VALIDATIONS.stop_details;
4807 
4808   l_del_rows            wsh_util_core.id_tab_type;
4809   l_pickup_stop_id      NUMBER;
4810   l_dropoff_stop_id     NUMBER;
4811   l_pickup_location_id  NUMBER;
4812   l_dropoff_location_id NUMBER;
4813   l_pickup_stop_seq     NUMBER;
4814   l_dropoff_stop_seq    NUMBER;
4815   l_trip_ids           wsh_util_core.id_tab_type;
4816   l_dummy_trip_ids     wsh_util_core.id_tab_type;
4817   l_del_ids            wsh_util_core.id_tab_type;
4818   l_internal_del_ids   wsh_util_core.id_tab_type;
4819 
4820   l_stop_seq_mode         NUMBER; --SSN
4821 
4822   --WF: CMR
4823   l_del_old_carrier_ids WSH_UTIL_CORE.ID_TAB_TYPE;
4824   l_wf_rs VARCHAR2(1);
4825 
4826 BEGIN
4827 /* The  validations for stop sequence number are already done before calling this */
4828   --
4829   -- Debug Statements
4830   --
4831   --
4832   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
4833   --
4834   IF l_debug_on IS NULL
4835   THEN
4836       l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
4837   END IF;
4838   --
4839   l_stop_seq_mode := WSH_TRIPS_ACTIONS.GET_STOP_SEQ_MODE;
4840 
4841   IF l_debug_on THEN
4842       WSH_DEBUG_SV.push(l_module_name);
4843       --
4844       WSH_DEBUG_SV.log(l_module_name,'P_TRIP_ID',P_TRIP_ID);
4845       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_STOP_ID',P_PICKUP_STOP_ID);
4846       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_STOP_SEQ',P_PICKUP_STOP_SEQ);
4847       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_STOP_ID',P_DROPOFF_STOP_ID);
4848       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_STOP_SEQ',P_DROPOFF_STOP_SEQ);
4849       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_LOCATION_ID',P_PICKUP_LOCATION_ID);
4850       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_LOCATION_ID',P_DROPOFF_LOCATION_ID);
4851       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_ARR_DATE',P_PICKUP_ARR_DATE);
4852       -- Pack J csun Stop Sequence Change
4853       WSH_DEBUG_SV.log(l_module_name,'P_PICKUP_DEP_DATE',P_PICKUP_DEP_DATE);
4854       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_ARR_DATE',P_DROPOFF_ARR_DATE);
4855       WSH_DEBUG_SV.log(l_module_name,'P_DROPOFF_DEP_DATE',P_DROPOFF_DEP_DATE);
4856       WSH_DEBUG_SV.log(l_module_name,'STOP SEQUENCE MODE',l_stop_seq_mode);
4857 
4858   END IF;
4859   --
4860   IF (p_del_rows.count = 0) THEN
4861    raise others;
4862   END IF;
4863 
4864   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
4865 
4866   -- Pack J csun Stop Sequence Change
4867   -- This is the change to sequence trip stops by
4868   -- planned arrival date rather than stop sequence number.
4869   -- the checking above was commented out since it is not applicable any more.
4870   IF ((p_pickup_location_id IS NOT NULL
4871      AND (p_pickup_arr_date IS NOT NULL OR p_pickup_dep_date IS NOT NULL))
4872      AND (p_dropoff_location_id IS NOT NULL
4873      AND (p_dropoff_arr_date IS NOT NULL OR p_dropoff_dep_date IS NOT NULL))
4874      ) THEN
4875 
4876     -- bug 3516052
4877     -- bug 4036204: We relax the restriction so that p_pickup_dep_date = p_dropoff_arr_date
4878     -- as long as p_pickup_arr_date >= p_dropoff_arr_date
4879     -- SSN Change, add conditional check
4880     IF (l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD) AND
4881        ((p_pickup_dep_date > p_dropoff_arr_date) OR
4882         ((p_pickup_dep_date = p_dropoff_arr_date) AND
4883          (p_pickup_arr_date >= p_dropoff_arr_date)
4884         )
4885        )THEN
4886       FND_MESSAGE.SET_NAME('WSH', 'WSH_INVALID_PLANNED_DATE');
4887       FND_MESSAGE.SET_TOKEN('PICKUP_DATE',  fnd_date.date_to_displaydt(p_pickup_dep_date));
4888       FND_MESSAGE.SET_TOKEN('DROPOFF_DATE', fnd_date.date_to_displaydt(p_dropoff_arr_date));
4889       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4890       wsh_util_core.add_message(x_return_status);
4891       --
4892       -- Debug Statements
4893       --
4894       IF l_debug_on THEN
4895           WSH_DEBUG_SV.pop(l_module_name);
4896       END IF;
4897       --
4898       RETURN;
4899     END IF;
4900   END IF;
4901 
4902 /* End of H integration  04/01/2002 */
4903 
4904   -- Action check for assigning
4905 
4906   check_assign_trip(
4907       p_del_rows      =>   p_del_rows,
4908       p_trip_id       =>   p_trip_id,
4909       p_pickup_stop_id   =>   p_pickup_stop_id,
4910       p_dropoff_stop_id  =>   p_dropoff_stop_id,
4911       p_pickup_location_id  =>   p_pickup_location_id,
4912       p_dropoff_location_id    =>   p_dropoff_location_id,
4913       p_pickup_arr_date    => p_pickup_arr_date,
4914       p_pickup_dep_date    => p_pickup_dep_date,
4915       p_dropoff_arr_date     =>  p_dropoff_arr_date,
4916       p_dropoff_dep_date     =>  p_dropoff_dep_date,
4917       x_return_status    =>   x_return_status);
4918 
4919   IF (x_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
4920    --
4921    -- Debug Statements
4922    --
4923    IF l_debug_on THEN
4924        WSH_DEBUG_SV.pop(l_module_name);
4925    END IF;
4926    --
4927    RETURN;
4928   END IF;
4929 
4930   l_pickup_stop_id      :=p_pickup_stop_id;
4931   l_dropoff_stop_id     :=p_dropoff_stop_id;
4932   l_pickup_location_id  :=p_pickup_location_id;
4933   l_dropoff_location_id :=p_dropoff_location_id;
4934   l_pickup_stop_seq     :=p_pickup_stop_seq;
4935   l_dropoff_stop_seq    :=p_dropoff_stop_seq;
4936 
4937 /*CURRENTLY NOT IN USE
4938   --WF: CMR
4939   WSH_WF_STD.Get_Carrier(p_del_ids => p_del_rows,
4940                          x_del_old_carrier_ids => l_del_old_carrier_ids,
4941                          x_return_status => l_wf_rs);
4942 */
4943     IF l_debug_on THEN
4944         WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit process_dels_for_internal_locs',WSH_DEBUG_SV.C_PROC_LEVEL);
4945     END IF;
4946     process_dels_for_internal_locs(
4947       p_del_rows             => p_del_rows,
4948       p_trip_id              => p_trip_id,
4949       p_pickup_stop_id       => p_pickup_stop_id,
4950       p_dropoff_stop_id      => p_dropoff_stop_id,
4951       p_pickup_location_id   => p_pickup_location_id,
4952       p_dropoff_location_id  => p_dropoff_location_id,
4953       p_caller               => p_caller,
4954       x_return_status        => x_return_status,
4955       x_pickup_stop_id       => l_pickup_stop_id,
4956       x_dropoff_stop_id      => l_dropoff_stop_id,
4957       x_pickup_location_id   => l_pickup_location_id,
4958       x_dropoff_location_id  => l_dropoff_location_id,
4959       x_dropoff_seq_num      => l_dropoff_stop_seq,
4960       x_del_ids              => l_del_ids,
4961       x_internal_del_ids     => l_internal_del_ids);
4962 
4963     IF (x_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
4964       IF l_debug_on THEN
4965          WSH_DEBUG_SV.log(l_module_name,'process_dels_for_internal_locs return_status',x_return_status);
4966          WSH_DEBUG_SV.pop(l_module_name);
4967       END IF;
4968       RETURN;
4969     END IF;
4970 
4971     -- SSN Change, add conditional check
4972     IF (l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD) AND
4973       -- Bug 4017507: If we are creating a new stop, we need to resequence.
4974       (l_pickup_stop_id IS NULL OR l_dropoff_stop_id IS NULL)
4975     THEN
4976       l_pickup_stop_seq := NULL;
4977       l_dropoff_stop_seq := NULL;
4978     END IF;
4979     -- end of SSN Change, add conditional check
4980 
4981     IF l_internal_del_ids IS NOT NULL and l_internal_del_ids.count>0 THEN
4982        IF l_debug_on THEN
4983            WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_ACTIONS.ASSIGN_DELIVERIES for internal deliveries',WSH_DEBUG_SV.C_PROC_LEVEL);
4984        END IF;
4985        wsh_delivery_legs_actions.assign_deliveries(
4986           p_del_rows => l_internal_del_ids,
4987           p_trip_id => p_trip_id,
4988           p_pickup_stop_id => l_pickup_stop_id,
4989           p_pickup_stop_seq => l_pickup_stop_seq,
4990           p_dropoff_stop_id => l_dropoff_stop_id,
4991           p_dropoff_stop_seq => l_dropoff_stop_seq,
4992           p_pickup_location_id => l_pickup_location_id,
4993           p_dropoff_location_id => l_dropoff_location_id,
4994           p_create_flag => 'Y',
4995           x_leg_rows => l_del_legs,
4996           x_return_status => l_return_status,
4997           p_caller        => p_caller,
4998           p_pickup_arr_date   => p_pickup_arr_date,
4999           p_pickup_dep_date   => p_pickup_dep_date,
5000           p_dropoff_arr_date  => p_dropoff_arr_date,
5001           p_dropoff_dep_date  => p_dropoff_dep_date);
5002 
5003         IF l_debug_on THEN
5004            WSH_DEBUG_SV.log(l_module_name,'wsh_delivery_legs_actions.assign_deliveries return_status',l_return_status);
5005         END IF;
5006 
5007        IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
5008           x_return_status := l_return_status;
5009           IF l_debug_on THEN
5010              WSH_DEBUG_SV.pop(l_module_name);
5011           END IF;
5012           RETURN;
5013        ELSIF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING) THEN
5014           x_return_status := l_return_status;
5015        END IF;
5016     END IF;
5017 
5018 
5019     IF l_del_ids IS NOT NULL and l_del_ids.count>0 THEN
5020       IF l_debug_on THEN
5021         WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_ACTIONS.ASSIGN_DELIVERIES for regular deliveries',WSH_DEBUG_SV.C_PROC_LEVEL);
5022       END IF;
5023       wsh_delivery_legs_actions.assign_deliveries(
5024         p_del_rows => l_del_ids,
5025         p_trip_id => p_trip_id,
5026         p_pickup_stop_id => l_pickup_stop_id,
5027         p_pickup_stop_seq => l_pickup_stop_seq,
5028         p_dropoff_stop_id => p_dropoff_stop_id,
5029         p_dropoff_stop_seq => l_dropoff_stop_seq,
5030         p_pickup_location_id => l_pickup_location_id,
5031         p_dropoff_location_id => p_dropoff_location_id,
5032         p_create_flag => 'Y',
5033         x_leg_rows => l_del_legs,
5034         x_return_status => l_return_status,
5035         p_caller        => p_caller,
5036         p_pickup_arr_date   => p_pickup_arr_date,
5037         p_pickup_dep_date   => p_pickup_dep_date,
5038         p_dropoff_arr_date  => p_dropoff_arr_date,
5039         p_dropoff_dep_date  => p_dropoff_dep_date);
5040 
5041         IF l_debug_on THEN
5042            WSH_DEBUG_SV.log(l_module_name,'wsh_delivery_legs_actions.assign_deliveries return_status',l_return_status);
5043         END IF;
5044 
5045         IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
5046           x_return_status := l_return_status;
5047           IF l_debug_on THEN
5048             WSH_DEBUG_SV.pop(l_module_name);
5049           END IF;
5050           RETURN;
5051         ELSIF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING) THEN
5052           x_return_status := l_return_status;
5053         END IF;
5054     END IF;
5055 
5056   -- Pack J csun Stop Sequence Change
5057   -- resequence trip stops in the trip
5058   -- trip stops are re-sequenced based on planned arrival date
5059   l_stop_details_rec.stop_id := NULL;
5060   l_stop_details_rec.trip_id := p_trip_id;
5061 
5062   -- SSN change
5063   -- Call Reset_Stop_Seq_Numbers only if mode = PAD
5064   IF l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD THEN
5065     -- need to resequence the stops' SSNs and validate their dates.
5066     WSH_TRIP_STOPS_ACTIONS.RESET_STOP_SEQ_NUMBERS(
5067       p_stop_details_rec => l_stop_details_rec,
5068       x_return_status => l_return_status );
5069 
5070     IF l_debug_on THEN
5071       WSH_DEBUG_SV.log(l_module_name,'return status from WSH_TRIP_STOPS_ACTIONS.RESET_STOP_SEQ_NUMBERS',l_return_status);
5072     END IF;
5073 
5074     IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
5075       IF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
5076         l_num_warn := l_num_warn + 1;
5077       ELSE
5078         x_return_status := l_return_status;
5079         --
5080         -- Debug Statements
5081         --
5082         IF l_debug_on THEN
5083            WSH_DEBUG_SV.pop(l_module_name);
5084         END IF;
5085         --
5086         RETURN;
5087       END IF;
5088     END IF;
5089 
5090     -- SSN change, call to Validate_stop_dates is also made only when mode = PAD
5091     -- 3516052
5092     -- call validate_stop_dates after all the changes had been applied to
5093     -- database
5094     WSH_TRIP_VALIDATIONS.Validate_Stop_Dates (
5095       p_trip_id                => p_trip_id,
5096       x_return_status          => l_return_status,
5097       p_caller                 => p_caller
5098       );
5099 
5100     IF l_debug_on THEN
5101       wsh_debug_sv.log(l_module_name,'Return Status From WSH_TRIP_STOPS_VALIDATIONS.Validate_Stop_Dates for trip '||p_trip_id ,l_return_status);
5102     END IF;
5103 
5104     IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
5105       IF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
5106         l_num_warn := l_num_warn + 1;
5107       ELSE
5108         x_return_status := l_return_status;
5109         --
5110         -- Debug Statements
5111         --
5112         IF l_debug_on THEN
5113            WSH_DEBUG_SV.pop(l_module_name);
5114         END IF;
5115         --
5116         RETURN;
5117       END IF;
5118     END IF;
5119   END IF; -- if mode = PAD
5120 
5121   -- end of Stop Sequence Change
5122 
5123   /*CURRENTLY NOT IN USE
5124   --WF: CMR
5125   WSH_WF_STD.Handle_Trip_Carriers(p_trip_id => p_trip_id,
5126 			          p_del_ids => p_del_rows,
5127 			          p_del_old_carrier_ids => l_del_old_carrier_ids,
5128 			          x_return_status => l_wf_rs);
5129 */
5130   IF (l_num_warn > 0 AND x_return_status = WSH_UTIL_CORE.G_RET_STS_SUCCESS) THEN
5131     x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
5132   END IF;
5133 
5134 --
5135 -- Debug Statements
5136 --
5137 IF l_debug_on THEN
5138     WSH_DEBUG_SV.pop(l_module_name);
5139 END IF;
5140 --
5141   EXCEPTION
5142     WHEN stop_not_found THEN
5143       FND_MESSAGE.SET_NAME('WSH','WSH_STOP_NOT_FOUND');
5144       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5145       wsh_util_core.add_message(x_return_status);
5146       --
5147       -- Debug Statements
5148       --
5149       IF l_debug_on THEN
5150           WSH_DEBUG_SV.logmsg(l_module_name,'STOP_NOT_FOUND exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
5151           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:STOP_NOT_FOUND');
5152       END IF;
5153       --
5154      WHEN others THEN
5155       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.ASSIGN_TRIP');
5156       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
5157 
5158 --
5159 -- Debug Statements
5160 --
5161 IF l_debug_on THEN
5162     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
5163     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
5164 END IF;
5165 --
5166 END assign_trip;
5167 
5168 
5169 -- J: W/V Changes
5170 
5171 -- Start of comments
5172 -- API name : calc_stop_fill_percent
5173 -- Type     : Public
5174 -- Pre-reqs : None.
5175 -- Function : Calculates the fill% of stop with specified W/V info
5176 -- Parameters :
5177 -- IN:
5178 --    p_stop_id      IN NUMBER Required
5179 --    p_gross_weight IN  NUMBER
5180 --      Gross Wt. of the stop
5181 --    p_volume       IN  NUMBER
5182 --      Volume of the stop
5183 -- OUT:
5184 --    x_stop_fill_percent OUT NUMBER
5185 --       gives the calculated fill%
5186 --    x_return_status OUT VARCHAR2 Required
5187 --       give the return status of API
5188 -- Version : 1.0
5189 -- End of comments
5190 
5191 PROCEDURE calc_stop_fill_percent(
5192             p_stop_id           IN  NUMBER,
5193             p_gross_weight      IN  NUMBER,
5194             p_volume            IN  NUMBER,
5195             x_stop_fill_percent OUT NOCOPY NUMBER,
5196             x_return_status     OUT NOCOPY  VARCHAR2) IS
5197 
5198 CURSOR c_get_stop_trip(c_stop_id IN NUMBER) IS
5199 SELECT trip_id,
5200        weight_uom_code,
5201        volume_uom_code
5202 FROM   wsh_trip_stops
5203 WHERE  stop_id = c_stop_id;
5204 
5205 CURSOR trip_vehicle_info (c_trip_id NUMBER) IS
5206 SELECT vehicle_item_id,
5207        vehicle_organization_id
5208 FROM   wsh_trips
5209 WHERE  trip_id = c_trip_id;
5210 
5211 CURSOR trip_info (c_trip_id NUMBER) IS
5212 SELECT msi.maximum_load_weight,
5213        msi.internal_volume,
5214        msi.minimum_fill_percent,
5215        msi.weight_uom_code,
5216        msi.volume_uom_code,
5217        msi.organization_id
5218 FROM   mtl_system_items msi,
5219        wsh_trips t
5220 WHERE  msi.organization_id = t.vehicle_organization_id AND
5221        t.vehicle_item_id = msi.inventory_item_id AND
5222        t.trip_id = c_trip_id;
5223 
5224 CURSOR org_info (c_trip_id NUMBER) IS
5225 SELECT wsp.percent_fill_basis_flag
5226 FROM   wsh_shipping_parameters wsp,
5227        wsh_trips t
5228 WHERE  wsp.organization_id  = t.vehicle_organization_id AND
5229        t.trip_id = c_trip_id;
5230 
5231 l_trip_id         NUMBER;
5232 l_stop_wt_uom     VARCHAR2(3);
5233 l_stop_vol_uom    VARCHAR2(3);
5234 l_vehicle_item_id NUMBER;
5235 l_vehicle_org_id  NUMBER;
5236 l_trip_max_weight NUMBER;
5237 l_trip_max_volume NUMBER;
5238 l_trip_min_fill   NUMBER;
5239 l_trip_weight_uom VARCHAR2(3);
5240 l_trip_volume_uom VARCHAR2(3);
5241 l_trip_org_id     NUMBER;
5242 l_fill_basis      VARCHAR2(1);
5243 l_wt_vol_tmp      NUMBER;
5244 
5245 l_debug_on BOOLEAN;
5246 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'CALC_STOP_FILL_PERCENT';
5247 
5248 BEGIN
5249 
5250    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
5251    --
5252    IF l_debug_on IS NULL
5253    THEN
5254        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
5255    END IF;
5256    --
5257    IF l_debug_on THEN
5258        WSH_DEBUG_SV.push(l_module_name);
5259        --
5260        WSH_DEBUG_SV.log(l_module_name,'P_STOP_ID',P_STOP_ID);
5261        WSH_DEBUG_SV.log(l_module_name,'P_GROSS_WEIGHT',P_GROSS_WEIGHT);
5262        WSH_DEBUG_SV.log(l_module_name,'P_VOLUME',P_VOLUME);
5263    END IF;
5264    --
5265    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
5266 
5267    -- Return if stop is null or if W/V is null
5268    IF p_stop_id is NULL OR (p_gross_weight is NULL AND p_volume is NULL) THEN
5269      IF l_debug_on THEN
5270        WSH_DEBUG_SV.pop(l_module_name);
5271      END IF;
5272      return;
5273    END IF;
5274 
5275    -- Check if stop exists. Find the associated trip
5276    OPEN  c_get_stop_trip(p_stop_id);
5277    FETCH c_get_stop_trip INTO l_trip_id, l_stop_wt_uom, l_stop_vol_uom;
5278    IF c_get_stop_trip%NOTFOUND THEN
5279      IF l_debug_on THEN
5280        WSH_DEBUG_SV.logmsg(l_module_name,'Stop '||p_stop_id||' not found');
5281        WSH_DEBUG_SV.pop(l_module_name);
5282      END IF;
5283      CLOSE c_get_stop_trip;
5284      return;
5285    END IF;
5286    CLOSE c_get_stop_trip;
5287 
5288    -- Get the Vehicle Info for the trip
5289    OPEN  trip_vehicle_info (l_trip_id);
5290    FETCH trip_vehicle_info INTO l_vehicle_item_id, l_vehicle_org_id;
5291    CLOSE trip_vehicle_info;
5292 
5293    -- Get the W/V attributes of the vehicle item
5294    IF (l_vehicle_item_id IS NOT NULL) AND (l_vehicle_org_id IS NOT NULL) THEN
5295      OPEN  trip_info (l_trip_id);
5296      FETCH trip_info
5297      INTO  l_trip_max_weight,
5298            l_trip_max_volume,
5299            l_trip_min_fill,
5300            l_trip_weight_uom,
5301            l_trip_volume_uom,
5302            l_trip_org_id;
5303      IF (trip_info%NOTFOUND) THEN
5304        CLOSE trip_info;
5305        FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_NOT_FOUND');
5306        x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5307        wsh_util_core.add_message(x_return_status);
5308      END IF;
5309      CLOSE trip_info;
5310    END IF;
5311 
5312    -- Get the fill basis of vehicle org
5313    IF (l_vehicle_org_id IS NOT NULL) THEN
5314 
5315      OPEN org_info (l_trip_id);
5316      FETCH org_info INTO l_fill_basis;
5317 
5318      IF (org_info%NOTFOUND) THEN
5319        l_fill_basis := 'W';
5320      END IF;
5321 
5322      CLOSE org_info;
5323    END IF;
5324 
5325    IF l_debug_on THEN
5326      WSH_DEBUG_SV.logmsg(l_module_name,'l_vehicle_item_id '||l_vehicle_item_id||' l_vehicle_org_id '||l_vehicle_org_id||' l_fill_basis '||l_fill_basis);
5327      WSH_DEBUG_SV.logmsg(l_module_name,'l_trip_max_weight '||l_trip_max_weight||' l_trip_weight_uom '||l_trip_weight_uom||' l_trip_max_volume '||l_trip_max_volume||' l_trip_volume_uom '||l_trip_volume_uom);
5328    END IF;
5329 
5330 
5331    -- Calculate the fill%
5332    IF (l_vehicle_item_id IS NOT NULL) AND (l_vehicle_org_id IS NOT NULL) THEN
5333 
5334      IF (l_fill_basis = 'W') AND (l_trip_max_weight IS NOT NULL) AND (l_trip_weight_uom IS NOT NULL) THEN
5335        IF l_debug_on THEN
5336          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5337        END IF;
5338        --
5339        l_wt_vol_tmp := wsh_wv_utils.convert_uom(l_trip_weight_uom, l_stop_wt_uom, l_trip_max_weight);
5340        IF (l_wt_vol_tmp > 0) THEN
5341           x_stop_fill_percent := round( 100 * p_gross_weight / l_wt_vol_tmp);
5342        END IF;
5343      ELSIF (l_fill_basis = 'V') AND (l_trip_max_volume IS NOT NULL) AND (l_trip_volume_uom IS NOT NULL) THEN
5344 
5345        IF l_debug_on THEN
5346          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5347        END IF;
5348        --
5349        l_wt_vol_tmp := wsh_wv_utils.convert_uom(l_trip_volume_uom, l_stop_vol_uom, l_trip_max_volume);
5350        IF (l_wt_vol_tmp > 0) THEN
5351           x_stop_fill_percent := round( 100 * p_volume / l_wt_vol_tmp );
5352        END IF;
5353      ELSE
5354        x_stop_fill_percent := NULL;
5355      END IF;
5356 
5357    END IF;
5358 
5359    IF l_debug_on THEN
5360      WSH_DEBUG_SV.logmsg(l_module_name,'x_stop_fill_percent '||x_stop_fill_percent||' x_return_status '||x_return_status);
5361      WSH_DEBUG_SV.pop(l_module_name);
5362    END IF;
5363 
5364 EXCEPTION
5365   WHEN others THEN
5366     IF c_get_stop_trip%ISOPEN THEN
5367       CLOSE c_get_stop_trip;
5368     END IF;
5369     IF trip_vehicle_info%ISOPEN THEN
5370       CLOSE trip_vehicle_info;
5371     END IF;
5372     IF trip_info%ISOPEN THEN
5373       CLOSE trip_info;
5374     END IF;
5375     IF org_info%ISOPEN THEN
5376       CLOSE org_info;
5377     END IF;
5378     wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.CALC_STOP_FILL_PERCENT');
5379     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
5380 
5381     IF l_debug_on THEN
5382       WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
5383       WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
5384     END IF;
5385 
5386 END calc_stop_fill_percent;
5387 
5388 PROCEDURE trip_weight_volume( p_trip_rows IN wsh_util_core.id_tab_type,
5389                p_override_flag IN VARCHAR2,
5390                p_calc_wv_if_frozen IN VARCHAR2,
5391                p_start_departure_date IN DATE,
5392                p_calc_del_wv IN      VARCHAR2,
5393                x_return_status OUT NOCOPY  VARCHAR2,
5394                p_suppress_errors IN VARCHAR2 DEFAULT NULL,
5395 --tkt
5396                p_caller                IN      VARCHAR2) IS
5397 CURSOR trip_stop_info (l_trip_id NUMBER) IS
5398 SELECT stop_id,
5399      weight_uom_code,
5400      volume_uom_code,
5401      status_code,
5402      departure_gross_weight,
5403      departure_net_weight,
5404      departure_volume,
5405      nvl(shipments_type_flag,'O'),
5406      nvl(wv_frozen_flag,'Y')
5407 FROM   wsh_trip_stops
5408 WHERE  trip_id = l_trip_id AND
5409       nvl(planned_departure_date, nvl(p_start_departure_date, FND_API.G_MISS_DATE))  >= nvl(p_start_departure_date, FND_API.G_MISS_DATE)
5410 ORDER BY stop_sequence_number;
5411 
5412 
5413 CURSOR lock_stop(c_stop_id NUMBER) IS
5414 SELECT stop_id
5415 FROM   wsh_trip_stops
5416 WHERE  stop_id = c_stop_id
5417 FOR UPDATE NOWAIT;
5418 
5419 
5420 CURSOR start_stop_info (l_trip_id NUMBER) IS
5421 SELECT stop_id,
5422      departure_gross_weight,
5423      departure_net_weight,
5424      departure_volume,
5425      weight_uom_code,
5426      volume_uom_code
5427 FROM   wsh_trip_stops
5428 WHERE  trip_id = l_trip_id AND
5429       nvl(planned_departure_date, nvl(p_start_departure_date, FND_API.G_MISS_DATE))  <= nvl(p_start_departure_date, FND_API.G_MISS_DATE) AND
5430      rownum = 1
5431 ORDER BY stop_sequence_number DESC;
5432 
5433 CURSOR last_stop (l_trip_id NUMBER) IS
5434 SELECT stop_id
5435 FROM   wsh_trip_stops
5436 WHERE  trip_id = l_trip_id
5437 AND    stop_sequence_number = ( SELECT MAX(wts.stop_sequence_number)
5438                                 FROM   wsh_trip_stops wts
5439                                 WHERE  wts.trip_id = l_trip_id );
5440 
5441 CURSOR pickup_deliveries (l_stop_id IN NUMBER) IS
5442 SELECT dl.delivery_id d_id,
5443      dl.weight_uom_code wt_uom,
5444      dl.volume_uom_code vol_uom,
5445      dl.gross_weight,
5446      dl.net_weight,
5447      dl.volume,
5448         dl.organization_id
5449 FROM   wsh_trip_stops t,
5450      wsh_delivery_legs dg,
5451      wsh_new_deliveries dl
5452 WHERE  t.stop_id = l_stop_id AND
5453      dg.pick_up_stop_id = t.stop_id AND
5454      dl.delivery_id = dg.delivery_id AND
5455      dg.parent_delivery_leg_id is NULL;
5456 
5457 CURSOR dropoff_deliveries (l_stop_id NUMBER) IS
5458 SELECT dl.delivery_id d_id,
5459        dl.organization_id
5460 FROM   wsh_trip_stops t,
5461      wsh_delivery_legs dg,
5462      wsh_new_deliveries dl
5463 WHERE  t.stop_id = l_stop_id AND
5464      dg.drop_off_stop_id = t.stop_id AND
5465      dl.delivery_id = dg.delivery_id AND
5466      dg.parent_delivery_leg_id is NULL;
5467 
5468 CURSOR trip_info (l_trip_id NUMBER) IS
5469 SELECT msi.maximum_load_weight,
5470      msi.internal_volume,
5471      msi.minimum_fill_percent,
5472      msi.weight_uom_code,
5473      msi.volume_uom_code,
5474      msi.organization_id
5475 FROM   mtl_system_items msi,
5476      wsh_trips t
5477 WHERE  msi.organization_id = t.vehicle_organization_id AND
5478      t.vehicle_item_id = msi.inventory_item_id AND
5479      t.trip_id = l_trip_id;
5480 
5481 CURSOR org_info (l_trip_id NUMBER) IS
5482 SELECT wsp.percent_fill_basis_flag
5483 FROM   wsh_shipping_parameters wsp,
5484      wsh_trips t
5485 WHERE  wsp.organization_id  = t.vehicle_organization_id AND
5486      t.trip_id = l_trip_id;
5487 
5488 CURSOR trip_vehicle_info (l_trip_id NUMBER) IS
5489 SELECT vehicle_item_id,
5490      vehicle_organization_id
5491 FROM   wsh_trips
5492 WHERE  trip_id = l_trip_id;
5493 
5494 
5495 l_net_weight   NUMBER;
5496 l_gross_weight NUMBER;
5497 l_volume    NUMBER;
5498 l_wt_vol_flag  VARCHAR2(1);
5499 l_organization NUMBER;
5500 
5501 l_total_net_weight NUMBER := 0;
5502 l_total_gross_weight NUMBER := 0;
5503 l_total_volume NUMBER := 0;
5504 
5505 l_stop_id  NUMBER;
5506 l_stop_wt_uom VARCHAR2(3);
5507 l_stop_vol_uom VARCHAR2(3);
5508 l_stop_fill_percent NUMBER;
5509 
5510 l_last_stop_id  NUMBER;
5511 
5512 l_start_stop_id NUMBER;
5513 l_prev_wt_uom VARCHAR2(3);
5514 l_prev_vol_uom VARCHAR2(3);
5515 
5516 l_stop_status VARCHAR2(3);
5517 l_tmp_dep_gross_wt NUMBER;
5518 l_tmp_dep_net_wt NUMBER;
5519 l_tmp_dep_vol NUMBER;
5520 l_shipment_type_flag VARCHAR2(1);
5521 l_wv_frozen_flag VARCHAR2(1);
5522 
5523 l_trip_max_weight   NUMBER;
5524 l_trip_max_volume   NUMBER;
5525 l_trip_min_fill    NUMBER;
5526 l_trip_weight_uom   VARCHAR2(3);
5527 l_trip_volume_uom   VARCHAR2(3);
5528 l_trip_org_id     NUMBER;
5529 
5530 l_fill_basis      VARCHAR2(1);
5531 l_org_name     VARCHAR2(60);
5532 l_wt_vol_tmp      NUMBER;
5533 
5534 l_vehicle_item_id   NUMBER;
5535 l_vehicle_org_id  NUMBER;
5536 l_num_error     NUMBER := 0;
5537 l_num_warn       NUMBER := 0;
5538 l_trip_num_warn       NUMBER := 0;
5539 l_stop_num_warn       NUMBER := 0;
5540 
5541 l_return_status    VARCHAR2(1);
5542 g_return_status    VARCHAR2(1);
5543 l_locked_stop_id   NUMBER := 0;
5544 others EXCEPTION;
5545 /* H integration  for Multi Leg */
5546   l_pub_stop_rec  WSH_TRIP_STOPS_PUB.TRIP_STOP_PUB_REC_TYPE;
5547   l_pub_trip_rec  WSH_TRIPS_PUB.TRIP_PUB_REC_TYPE;
5548 
5549 --R12 MDC
5550 l_total_pick_up_weight NUMBER := 0;
5551 l_total_drop_off_weight NUMBER := 0;
5552 l_total_pick_up_volume NUMBER := 0;
5553 l_total_drop_off_volume NUMBER := 0;
5554 
5555 TYPE Del_WV_Tab_Type IS TABLE OF pickup_deliveries%ROWTYPE INDEX BY BINARY_INTEGER;
5556 pickup_del_tab     Del_WV_Tab_Type;
5557 
5558                                                   --
5559 l_debug_on BOOLEAN;
5560                                                   --
5561                                                   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'TRIP_WEIGHT_VOLUME';
5562                                                   --
5563 BEGIN
5564    --
5565    -- Debug Statements
5566    --
5567    --
5568    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
5569    --
5570    IF l_debug_on IS NULL
5571    THEN
5572        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
5573    END IF;
5574    --
5575    IF l_debug_on THEN
5576        WSH_DEBUG_SV.push(l_module_name);
5577        --
5578        WSH_DEBUG_SV.log(l_module_name,'P_OVERRIDE_FLAG',P_OVERRIDE_FLAG);
5579        WSH_DEBUG_SV.log(l_module_name,'P_CALC_WV_IF_FROZEN',P_CALC_WV_IF_FROZEN);
5580        WSH_DEBUG_SV.log(l_module_name,'P_START_DEPARTURE_DATE',P_START_DEPARTURE_DATE);
5581        WSH_DEBUG_SV.log(l_module_name,'P_CALC_DEL_WV',P_CALC_DEL_WV);
5582        WSH_DEBUG_SV.log(l_module_name,'P_SUPPRESS_ERRORS',P_SUPPRESS_ERRORS);
5583    END IF;
5584    --
5585    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
5586    IF (p_trip_rows.count = 0) THEN
5587       raise others;
5588    END IF;
5589    FOR i IN 1..p_trip_rows.count LOOP
5590      /*
5591      l_num_warn is used to calculate the no. of warnings in all trips, successful trips etc.
5592 so use l_trip_num_warn to update for warnings and then if l_trip_num_warn is >1 at loop end,
5593 increase l_num_warn by 1. In some cases, l_num_warn is directly updated and since it is directly
5594 redirected to end of loop, those are okay.
5595      */
5596      l_trip_num_warn:=0;
5597      OPEN trip_vehicle_info (p_trip_rows(i));
5598      FETCH trip_vehicle_info INTO l_vehicle_item_id, l_vehicle_org_id;
5599      CLOSE trip_vehicle_info;
5600      pickup_del_tab.delete;
5601 
5602      --only open trip info if vehicle info found
5603      IF (l_vehicle_item_id IS NOT NULL) AND (l_vehicle_org_id IS NOT NULL) THEN
5604         OPEN trip_info (p_trip_rows(i));
5605         FETCH trip_info INTO l_trip_max_weight, l_trip_max_volume, l_trip_min_fill, l_trip_weight_uom, l_trip_volume_uom, l_trip_org_id;
5606         IF (trip_info%NOTFOUND) THEN
5607            CLOSE trip_info;
5608            FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_NOT_FOUND');
5609            x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5610            wsh_util_core.add_message(x_return_status);
5611            goto wt_vol_error;
5612         END IF;
5613         CLOSE trip_info;
5614      END IF;
5615 
5616 
5617      --only open trip info if vehicle info found
5618      IF (l_vehicle_org_id IS NOT NULL) THEN
5619 
5620         OPEN org_info (p_trip_rows(i));
5621         FETCH org_info INTO l_fill_basis;
5622 
5623         IF (org_info%NOTFOUND) THEN
5624            --Start of Bug 2415809
5625            FND_MESSAGE.SET_NAME('WSH','WSH_VEHICLE_ORG_FILLBASIS_NULL');
5626            --
5627            -- Debug Statements
5628            --
5629            IF l_debug_on THEN
5630                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_UTIL_CORE.GET_ORG_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
5631            END IF;
5632            --
5633            FND_MESSAGE.SET_TOKEN('l_vehicle_org', WSH_UTIL_CORE.Get_Org_Name(l_vehicle_org_id));
5634            --End of Bug 2415809
5635            x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
5636            wsh_util_core.add_message(x_return_status);
5637            l_trip_num_warn:=l_trip_num_warn+1;
5638            l_fill_basis := 'W';
5639         END IF;
5640 
5641         CLOSE org_info;
5642      END IF;
5643 
5644      OPEN trip_stop_info (p_trip_rows(i));
5645      FETCH trip_stop_info INTO l_stop_id, l_stop_wt_uom, l_stop_vol_uom, l_stop_status, l_tmp_dep_gross_wt, l_tmp_dep_net_wt, l_tmp_dep_vol, l_shipment_type_flag, l_wv_frozen_flag;
5646 
5647      IF (trip_stop_info%NOTFOUND) THEN
5648         CLOSE trip_stop_info;
5649         -- start bug 2366163: go to next trip without error if caller does not need w/v.
5650         IF p_suppress_errors = 'Y' THEN
5651            goto loop_end;
5652         END IF;
5653         -- end bug 2366163
5654         FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_NO_STOPS');
5655         --
5656         -- Debug Statements
5657         --
5658         IF l_debug_on THEN
5659             WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
5660         END IF;
5661         --
5662         FND_MESSAGE.SET_TOKEN('TRIP_NAME',wsh_trips_pvt.get_name(p_trip_rows(i)));
5663         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5664         wsh_util_core.add_message(x_return_status);
5665         goto wt_vol_error;
5666      END IF;
5667 
5668      OPEN last_stop(p_trip_rows(i));
5669      FETCH last_stop INTO l_last_stop_id;
5670      IF last_stop%NOTFOUND THEN
5671         CLOSE last_stop;
5672         CLOSE trip_stop_info;
5673         -- start bug 2366163: go to next trip without error if caller does not need w/v.
5674         IF p_suppress_errors = 'Y' THEN
5675            goto loop_end;
5676         END IF;
5677         -- end bug 2366163
5678         FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_NO_STOPS');
5679         --
5680         -- Debug Statements
5681         --
5682         IF l_debug_on THEN
5683             WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
5684         END IF;
5685         --
5686         FND_MESSAGE.SET_TOKEN('TRIP_NAME',wsh_trips_pvt.get_name(p_trip_rows(i)));
5687         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5688         wsh_util_core.add_message(x_return_status);
5689         goto wt_vol_error;
5690      END IF;
5691      CLOSE last_stop;
5692 
5693      OPEN start_stop_info (p_trip_rows(i));
5694      FETCH start_stop_info INTO l_start_stop_id,
5695                      l_gross_weight,
5696                      l_net_weight,
5697                      l_volume,
5698                      l_prev_wt_uom,
5699                      l_prev_vol_uom;
5700 
5701      IF (start_stop_info%FOUND) THEN
5702         IF (l_prev_wt_uom IS NULL) OR (l_prev_vol_uom IS NULL) THEN
5703            CLOSE start_stop_info;
5704            -- start bug 2366163: go to next trip without error if caller does not need w/v.
5705            IF (p_suppress_errors = 'Y')THEN
5706              goto wt_vol_error;
5707            END IF;
5708            -- end bug 2366163
5709 -- bug 2732503 , need a message only when FTE is installed
5710 -- else continue
5711            IF (wsh_util_core.fte_is_installed <> 'Y') THEN
5712              goto loop_end;
5713            END IF;
5714 
5715            FND_MESSAGE.SET_NAME('WSH','WSH_STOP_UOM_NULL');
5716            --
5717            -- Debug Statements
5718            --
5719            IF l_debug_on THEN
5720                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_STOPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
5721            END IF;
5722            --
5723            FND_MESSAGE.SET_TOKEN('stop_name',wsh_trip_stops_pvt.get_name(l_start_stop_id,p_caller));
5724 -- bug 2732503 , need a warning message only when FTE is installed
5725 -- still will go to wt_vol_error
5726            x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
5727            wsh_util_core.add_message(x_return_status);
5728            l_num_warn := l_num_warn + 1;
5729            goto loop_end;
5730         END IF;
5731      END IF;
5732 
5733      CLOSE start_stop_info;
5734       /***********LOOP for stop start****************/
5735      l_stop_num_warn:=0;
5736      --increment l_stop_num_warn for this stop loop so that for each trip, l_num_warn will get updated max by 1
5737      LOOP
5738 
5739        IF l_debug_on THEN
5740          WSH_DEBUG_SV.logmsg(l_module_name,'*** Processing Stop '||l_stop_id||' ***');
5741        END IF;
5742 
5743        l_total_pick_up_weight := 0;
5744        l_total_drop_off_weight := 0;
5745        l_total_pick_up_volume  := 0;
5746        l_total_drop_off_volume := 0;
5747 
5748 
5749 
5750        IF (p_calc_wv_if_frozen = 'N' and l_wv_frozen_flag = 'Y') THEN
5751 
5752          l_total_gross_weight := l_total_gross_weight + l_tmp_dep_gross_wt;
5753          l_total_net_weight := l_total_net_weight + l_tmp_dep_net_wt;
5754          l_total_volume := l_total_volume + l_tmp_dep_vol;
5755          l_prev_wt_uom := l_stop_wt_uom;
5756          l_prev_vol_uom := l_stop_vol_uom;
5757 
5758          IF l_debug_on THEN
5759            WSH_DEBUG_SV.logmsg(l_module_name,'Accumulated W/V after conversions are Gross '||l_total_gross_weight||' Net '||l_total_net_weight||' Vol '||l_total_volume);
5760            WSH_DEBUG_SV.logmsg(l_module_name,'WV Frozen '||l_wv_frozen_flag||'. Skipping the Stop W/V calculation.');
5761          END IF;
5762          --R12 MDC
5763          --Removing the goto statement because code needs to update the new pick_up/drop_off weight/volume columns even if frozen flag is Y
5764          -- Code has been added before the update statement to check the frozen flag
5765          -- Update existing wt/vol columns only if frozen_flag = N
5766          -- Update new wt/vol columns irrespective of frozen flag being Y or N.
5767          -- goto continue_next;
5768 
5769        END IF;
5770 
5771         IF (l_stop_wt_uom IS NULL) OR (l_stop_vol_uom IS NULL) THEN
5772            -- start bug 2366163: go to next trip without error if caller does not need w/v.
5773            IF (p_suppress_errors = 'Y')THEN
5774               goto loop_end;
5775            END IF;
5776            -- end bug 2366163
5777 -- bug 2732503 , need a message only when FTE is installed
5778 -- this will skip calculating wt/vol for all the stops
5779 -- else continue
5780            IF (wsh_util_core.fte_is_installed <> 'Y') THEN
5781              goto loop_end;
5782            END IF;
5783 
5784            FND_MESSAGE.SET_NAME('WSH','WSH_STOP_UOM_NULL');
5785            --
5786            -- Debug Statements
5787            --
5788            IF l_debug_on THEN
5789                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_STOPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
5790            END IF;
5791            --
5792            FND_MESSAGE.SET_TOKEN('stop_name',wsh_trip_stops_pvt.get_name(l_stop_id,p_caller));
5793 -- bug 2732503 , need a warning message only when FTE is installed
5794 -- still will go to wt_vol_error
5795            x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
5796            wsh_util_core.add_message(x_return_status);
5797            l_stop_num_warn := l_stop_num_warn + 1;
5798            goto continue_next;
5799         END IF;
5800 
5801          --check if  stop status is closed, and weight, volume are not null
5802          --if NOT so, proceed
5803          -- ELSE proceeed to end of loop,  go to the next stop.
5804         IF NOT(l_stop_status='CL' AND l_shipment_type_flag = 'O') THEN
5805 
5806            --
5807            -- Debug Statements
5808            --
5809            IF l_debug_on THEN
5810                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5811            END IF;
5812            --
5813            l_total_net_weight := wsh_wv_utils.convert_uom(l_prev_wt_uom, l_stop_wt_uom, l_total_net_weight);
5814            --
5815            -- Debug Statements
5816            --
5817            IF l_debug_on THEN
5818                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5819            END IF;
5820            --
5821            l_total_gross_weight := wsh_wv_utils.convert_uom(l_prev_wt_uom, l_stop_wt_uom, l_total_gross_weight);
5822            --
5823            -- Debug Statements
5824            --
5825            IF l_debug_on THEN
5826                WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5827            END IF;
5828            --
5829            l_volume := wsh_wv_utils.convert_uom(l_prev_vol_uom, l_stop_vol_uom, l_total_volume);
5830 
5831            g_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
5832 
5833             IF l_debug_on THEN
5834               WSH_DEBUG_SV.logmsg(l_module_name,'Processing Pickup Deliveries ...');
5835             END IF;
5836 
5837             FOR pkup_dl IN pickup_deliveries (l_stop_id) LOOP
5838 
5839                   IF p_calc_del_wv = 'Y' THEN --{
5840                   --
5841                   -- Debug Statements
5842                   --
5843                   IF l_debug_on THEN
5844                       WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.DELIVERY_WEIGHT_VOLUME',WSH_DEBUG_SV.C_PROC_LEVEL);
5845                   END IF;
5846                   --
5847                   wsh_wv_utils.delivery_weight_volume(
5848                     p_delivery_id       => pkup_dl.d_id,
5849                     p_update_flag       => 'Y',
5850                     p_calc_wv_if_frozen => p_calc_wv_if_frozen,
5851                     x_gross_weight      => l_gross_weight,
5852                     x_net_weight        => l_net_weight,
5853                     x_volume            => l_volume,
5854                     x_return_status     => l_return_status);
5855 
5856                   IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR)
5857                   OR (l_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
5858                      FND_MESSAGE.SET_NAME('WSH','WSH_DEL_WT_VOL_FAILED');
5859                      --
5860                      -- Debug Statements
5861                      --
5862                      IF l_debug_on THEN
5863                          WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_NEW_DELIVERIES_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
5864                      END IF;
5865                      --
5866                      FND_MESSAGE.SET_TOKEN('del_name',wsh_new_deliveries_pvt.get_name(pkup_dl.d_id));
5867                      g_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
5868                      wsh_util_core.add_message(g_return_status);
5869                   END IF;
5870 
5871 
5872                   ELSE
5873 
5874                   l_gross_weight    := pkup_dl.gross_weight;
5875                   l_net_weight      := pkup_dl.net_weight;
5876                   l_volume          := pkup_dl.volume;
5877 
5878                   END IF; --}
5879 
5880                pickup_del_tab(pkup_dl.d_id).d_id            := pkup_dl.d_id;
5881                pickup_del_tab(pkup_dl.d_id).wt_uom          := pkup_dl.wt_uom;
5882                pickup_del_tab(pkup_dl.d_id).vol_uom         := pkup_dl.vol_uom;
5883                pickup_del_tab(pkup_dl.d_id).gross_weight    := l_gross_weight;
5884                pickup_del_tab(pkup_dl.d_id).net_weight      := l_net_weight;
5885                pickup_del_tab(pkup_dl.d_id).volume          := l_volume;
5886                pickup_del_tab(pkup_dl.d_id).organization_id := pkup_dl.organization_id;
5887 
5888 -- J: W/V Changes
5889                IF l_net_weight >= 0 THEN
5890                  IF pkup_dl.wt_uom <> l_stop_wt_uom THEN
5891                    IF l_debug_on THEN
5892                      WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5893                    END IF;
5894                    --
5895                    IF l_net_weight > 0
5896                    THEN
5897                     l_total_net_weight := l_total_net_weight + wsh_wv_utils.convert_uom(pkup_dl.wt_uom, l_stop_wt_uom, l_net_weight);
5898                    ELSE
5899                     l_total_net_weight := l_total_net_weight + l_net_weight;
5900                    END IF;
5901                  ELSE
5902                    l_total_net_weight := l_total_net_weight + l_net_weight;
5903                  END IF;
5904                END IF;
5905 
5906                IF l_gross_weight >= 0 THEN
5907                  IF pkup_dl.wt_uom <> l_stop_wt_uom THEN
5908                    IF l_debug_on THEN
5909                      WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5910                    END IF;
5911                    --
5912                    IF l_gross_weight > 0
5913                    THEN
5914                     l_total_gross_weight := l_total_gross_weight + wsh_wv_utils.convert_uom(pkup_dl.wt_uom, l_stop_wt_uom, l_gross_weight);
5915                     --R12 MDC
5916                     l_total_pick_up_weight := l_total_pick_up_weight + wsh_wv_utils.convert_uom(pkup_dl.wt_uom, l_stop_wt_uom, l_gross_weight);
5917                    ELSE
5918                     l_total_gross_weight := l_total_gross_weight + l_gross_weight;
5919                     --R12 MDC
5920                     l_total_pick_up_weight := l_total_pick_up_weight + l_gross_weight;
5921                    END IF;
5922                  ELSE
5923                    l_total_gross_weight := l_total_gross_weight + l_gross_weight;
5924                    --R12 MDC
5925                    l_total_pick_up_weight := l_total_pick_up_weight + l_gross_weight;
5926                  END IF;
5927                END IF;
5928 
5929                IF l_volume >= 0 THEN
5930                  IF pkup_dl.vol_uom <> l_stop_vol_uom THEN
5931                    IF l_debug_on THEN
5932                      WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5933                    END IF;
5934                    --
5935                    IF l_volume > 0
5936                    THEN
5937                     l_total_volume := l_total_volume + wsh_wv_utils.convert_uom(pkup_dl.vol_uom, l_stop_vol_uom, l_volume);
5938                     --R12 MDC
5939                     l_total_pick_up_volume := l_total_pick_up_volume + wsh_wv_utils.convert_uom(pkup_dl.vol_uom, l_stop_vol_uom, l_volume);
5940                    ELSE
5941                     l_total_volume := l_total_volume + l_volume;
5942                     --R12 MDC
5943                     l_total_pick_up_volume := l_total_pick_up_volume + l_volume;
5944                    END IF;
5945                  ELSE
5946                    l_total_volume := l_total_volume + l_volume;
5947                    --R12 MDC
5948                    l_total_pick_up_volume := l_total_pick_up_volume + l_volume;
5949                  END IF;
5950                END IF;
5951 
5952                IF l_debug_on THEN
5953                  WSH_DEBUG_SV.logmsg(l_module_name,'Accumulated W/V after conversions are Gross '||l_total_gross_weight||' Net '||l_total_net_weight||' Vol '||l_total_volume);
5954                END IF;
5955 
5956 
5957             END LOOP;
5958 
5959             IF (g_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING) THEN
5960               x_return_status := g_return_status;
5961               l_stop_num_warn := l_stop_num_warn + 1;
5962             END IF;
5963 
5964             IF l_debug_on THEN
5965               WSH_DEBUG_SV.logmsg(l_module_name,'After Pickup LOOP, Gross '||l_total_gross_weight||' Net '||l_total_net_weight||' Vol '||l_total_volume);
5966               WSH_DEBUG_SV.logmsg(l_module_name,'Processing Dropoff Deliveries ...');
5967             END IF;
5968 
5969 
5970             FOR dpoff_dl IN dropoff_deliveries (l_stop_id) LOOP
5971                IF pickup_del_tab.EXISTS(dpoff_dl.d_id) THEN
5972                   IF pickup_del_tab(dpoff_dl.d_id).d_id = dpoff_dl.d_id THEN
5973                     IF l_stop_id <> l_last_stop_id THEN
5974 -- J: W/V Changes
5975                        IF pickup_del_tab(dpoff_dl.d_id).net_weight > 0 THEN
5976                          IF pickup_del_tab(dpoff_dl.d_id).wt_uom <> l_stop_wt_uom THEN
5977                            IF l_debug_on THEN
5978                              WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5979                            END IF;
5980                            --
5981                            l_total_net_weight := l_total_net_weight - wsh_wv_utils.convert_uom(pickup_del_tab(dpoff_dl.d_id).wt_uom, l_stop_wt_uom , pickup_del_tab(dpoff_dl.d_id).net_weight);
5982                          ELSE
5983                            l_total_net_weight := l_total_net_weight - pickup_del_tab(dpoff_dl.d_id).net_weight;
5984                          END IF;
5985                        END IF;
5986                     END IF;
5987 
5988                     IF pickup_del_tab(dpoff_dl.d_id).gross_weight > 0 THEN
5989                        IF pickup_del_tab(dpoff_dl.d_id).wt_uom <> l_stop_wt_uom THEN
5990                           IF l_debug_on THEN
5991                              WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
5992                           END IF;
5993                           --
5994                           IF l_stop_id <> l_last_stop_id THEN
5995                            l_total_gross_weight := l_total_gross_weight - wsh_wv_utils.convert_uom(pickup_del_tab(dpoff_dl.d_id).wt_uom, l_stop_wt_uom, pickup_del_tab(dpoff_dl.d_id).gross_weight);
5996                           END IF;
5997                           --R12 MDC
5998                           l_total_drop_off_weight := l_total_drop_off_weight + wsh_wv_utils.convert_uom(pickup_del_tab(dpoff_dl.d_id).wt_uom, l_stop_wt_uom, pickup_del_tab(dpoff_dl.d_id).gross_weight);
5999                        ELSE
6000                           IF l_stop_id <> l_last_stop_id THEN
6001                             l_total_gross_weight := l_total_gross_weight - pickup_del_tab(dpoff_dl.d_id).gross_weight;
6002                           END IF;
6003                             --R12 MDC
6004                             l_total_drop_off_weight := l_total_drop_off_weight + pickup_del_tab(dpoff_dl.d_id).gross_weight;
6005                          END IF;
6006                        END IF;
6007 
6008                     IF pickup_del_tab(dpoff_dl.d_id).volume > 0 THEN
6009                        IF pickup_del_tab(dpoff_dl.d_id).vol_uom <> l_stop_vol_uom THEN
6010                           IF l_debug_on THEN
6011                              WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
6012                           END IF;
6013                            --
6014                           IF l_stop_id <> l_last_stop_id THEN
6015                             l_total_volume := l_total_volume - wsh_wv_utils.convert_uom(pickup_del_tab(dpoff_dl.d_id).vol_uom, l_stop_vol_uom, pickup_del_tab(dpoff_dl.d_id).volume);
6016                           END IF;
6017                           --R12 MDC
6018                           l_total_drop_off_volume := l_total_drop_off_volume + wsh_wv_utils.convert_uom(pickup_del_tab(dpoff_dl.d_id).vol_uom, l_stop_vol_uom, pickup_del_tab(dpoff_dl.d_id).volume);
6019                        ELSE
6020                           IF l_stop_id <> l_last_stop_id THEN
6021                              l_total_volume := l_total_volume - pickup_del_tab(dpoff_dl.d_id).volume;
6022                           END IF;
6023                           --R12 MDC
6024                           l_total_drop_off_volume := l_total_drop_off_volume + pickup_del_tab(dpoff_dl.d_id).volume;
6025                        END IF;
6026                      END IF;
6027 
6028                      IF l_debug_on THEN
6029                         WSH_DEBUG_SV.logmsg(l_module_name,'Accumulated W/V after conversions are Gross '||l_total_gross_weight||' Net '||l_total_net_weight||' Vol '||l_total_volume);
6030                      END IF;
6031 
6032                   END IF;
6033                END IF;
6034             END LOOP;
6035 
6036             IF l_debug_on THEN
6037                WSH_DEBUG_SV.logmsg(l_module_name,'After Dropoff, LOOP, Gross '||l_total_gross_weight||' Net '||l_total_net_weight||' Vol '||l_total_volume);
6038             END IF;
6039 
6040             IF l_stop_id <> l_last_stop_id THEN
6041               -- Calculate fill_percent of stop
6042               --only if vehicle info found, calc. fill percent
6043               IF (l_vehicle_item_id IS NOT NULL) AND (l_vehicle_org_id IS NOT NULL) THEN
6044                  IF (l_fill_basis = 'W') AND (l_trip_max_weight IS NOT NULL) AND (l_trip_weight_uom IS NOT NULL) THEN
6045                     --
6046                     -- Debug Statements
6047                     --
6048                     IF l_debug_on THEN
6049                         WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
6050                     END IF;
6051                     --
6052                     l_wt_vol_tmp := wsh_wv_utils.convert_uom(l_trip_weight_uom, l_stop_wt_uom, l_trip_max_weight);
6053                     IF (l_wt_vol_tmp > 0) THEN
6054                        l_stop_fill_percent := round( 100 * l_total_gross_weight / l_wt_vol_tmp);
6055                     END IF;
6056                  ELSIF (l_fill_basis = 'V') AND (l_trip_max_volume IS NOT NULL) AND (l_trip_volume_uom IS NOT NULL) THEN
6057 
6058                     --
6059                     -- Debug Statements
6060                     --
6061                     IF l_debug_on THEN
6062                         WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_WV_UTILS.CONVERT_UOM',WSH_DEBUG_SV.C_PROC_LEVEL);
6063                     END IF;
6064                     --
6065                     l_wt_vol_tmp := wsh_wv_utils.convert_uom(l_trip_volume_uom, l_stop_vol_uom, l_trip_max_volume);
6066                     IF (l_wt_vol_tmp > 0) THEN
6067                        l_stop_fill_percent := round( 100 * l_total_volume / l_wt_vol_tmp );
6068                     END IF;
6069                  ELSE
6070                     l_stop_fill_percent := NULL;
6071                  END IF;
6072 
6073                  IF (l_stop_fill_percent < l_trip_min_fill) OR (l_stop_fill_percent > 100) THEN
6074                     FND_MESSAGE.SET_NAME('WSH','WSH_STOP_FILL_PC_EXCEEDED');
6075                     --
6076                     -- Debug Statements
6077                     --
6078                     IF l_debug_on THEN
6079                         WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_STOPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
6080                     END IF;
6081                     --
6082                     FND_MESSAGE.SET_TOKEN('STOP_NAME',wsh_trip_stops_pvt.get_name(l_stop_id,p_caller));
6083                     x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
6084                     wsh_util_core.add_message(x_return_status);
6085                     l_stop_num_warn := l_stop_num_warn + 1;
6086                  END IF;
6087               END IF;
6088 
6089             ELSE
6090               -- as this is the last stop for the trip, weight/volume/fill percent are 0
6091               -- pickup w/v also 0.
6092               IF l_debug_on THEN
6093                  WSH_DEBUG_SV.logmsg(l_module_name,'For last stop, assigning weight/volume/fill percent as zero ',WSH_DEBUG_SV.C_PROC_LEVEL);              END IF;
6094               --
6095               l_total_net_weight     := 0;
6096               l_total_gross_weight   := 0;
6097               l_total_volume         := 0;
6098               l_total_pick_up_weight := 0;
6099               l_total_pick_up_volume := 0;
6100               IF (l_vehicle_item_id IS NOT NULL) AND (l_vehicle_org_id IS NOT NULL) THEN
6101                  IF ((l_fill_basis = 'W') AND (l_trip_max_weight IS NOT NULL) AND (l_trip_weight_uom IS NOT NULL)) OR
6102                     ((l_fill_basis = 'V') AND (l_trip_max_volume IS NOT NULL) AND (l_trip_volume_uom IS NOT NULL)) THEN
6103                      l_stop_fill_percent  := 0;
6104                  ELSE
6105                      l_stop_fill_percent  := NULL;
6106                  END IF;
6107               END IF;
6108             END IF;
6109 
6110             /* H integration - J: W/V Changes */
6111             IF WSH_UTIL_CORE.FTE_IS_INSTALLED = 'Y' AND p_override_flag = 'Y' THEN
6112               IF l_debug_on THEN
6113                 WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit Fte_Load_Tender',WSH_DEBUG_SV.C_PROC_LEVEL);
6114               END IF;
6115               Fte_Load_Tender(
6116                 p_stop_id       => l_stop_id,
6117                 p_gross_weight  => l_total_gross_weight,
6118                 p_net_weight    => l_total_net_weight,
6119                 p_volume        => l_total_volume,
6120                 p_fill_percent  => l_stop_fill_percent,
6121                 x_return_status => l_return_status);
6122 
6123               IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
6124                 IF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
6125                   l_stop_num_warn := l_stop_num_warn + 1;
6126                 ELSE
6127                   x_return_status := l_return_status;
6128                   IF l_debug_on THEN
6129                     WSH_DEBUG_SV.pop(l_module_name);
6130                   END IF;
6131                   --
6132                   RETURN;
6133                 END IF;
6134               END IF;
6135             END IF;
6136             /* End of H integration - */
6137 
6138 -- J: W/V Changes
6139             IF p_override_flag = 'Y' THEN
6140               OPEN lock_stop(l_stop_id);
6141               FETCH lock_stop INTO l_locked_stop_id;
6142               IF lock_stop%FOUND THEN
6143 
6144                     --R12 MDC
6145                     --UPDATE new columns pickup/dropoff weight/volume first
6146 
6147                     IF l_debug_on THEN
6148                        WSH_DEBUG_SV.log(l_module_name,'l_total_pick_up_weight', l_total_pick_up_weight);
6149                        WSH_DEBUG_SV.log(l_module_name,'l_total_drop_off_weight', l_total_drop_off_weight);
6150                        WSH_DEBUG_SV.log(l_module_name,'l_total_pick_up_volume', l_total_pick_up_volume);
6151                        WSH_DEBUG_SV.log(l_module_name,'l_total_drop_off_volume', l_total_drop_off_volume);
6152                     END IF;
6153 
6154 
6155                     UPDATE wsh_trip_stops
6156                     SET   pick_up_weight = l_total_pick_up_weight,
6157                           drop_off_weight = l_total_drop_off_weight,
6158                           pick_up_volume = l_total_pick_up_volume,
6159                           drop_off_volume = l_total_drop_off_volume
6160                     WHERE  stop_id = l_stop_id;
6161 
6162                     IF (SQL%NOTFOUND) THEN
6163                        FND_MESSAGE.SET_NAME('WSH','WSH_STOP_NOT_FOUND');
6164                        x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6165                        wsh_util_core.add_message(x_return_status);
6166                        goto wt_vol_error;
6167                     END IF;
6168 
6169                --R12 MDC
6170                --UPDATE existing weight/volume columns only if frozen flag is Not Y
6171                IF NOT (p_calc_wv_if_frozen = 'N' and l_wv_frozen_flag = 'Y') THEN
6172                  UPDATE wsh_trip_stops
6173                     SET   departure_gross_weight = l_total_gross_weight,
6174                           departure_net_weight =  l_total_net_weight,
6175                           departure_volume = l_total_volume,
6176                           departure_fill_percent = l_stop_fill_percent,
6177                           wv_frozen_flag = 'N'
6178                     WHERE  stop_id = l_stop_id;
6179                  IF (SQL%NOTFOUND) THEN
6180                     FND_MESSAGE.SET_NAME('WSH','WSH_STOP_NOT_FOUND');
6181                     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6182                     wsh_util_core.add_message(x_return_status);
6183                     goto wt_vol_error;
6184                  END IF;
6185                END IF;
6186 
6187               END IF;
6188               CLOSE lock_stop;
6189 
6190             end if;
6191             --added ttrichy
6192 
6193             --below else for the condition when stop is closed and all the values
6194             --already exist=>no calculation needed
6195         ELSE
6196            l_total_net_weight := l_tmp_dep_net_wt;
6197            l_total_gross_weight :=l_tmp_dep_gross_wt;
6198            l_total_volume :=l_tmp_dep_vol;
6199         END IF;
6200 
6201        l_prev_wt_uom := l_stop_wt_uom;
6202        l_prev_vol_uom := l_stop_vol_uom;
6203        l_stop_fill_percent := NULL;
6204 
6205 -- Bug 2732503,need to continue with no message
6206 -- warning message is being set above
6207        <<continue_next>>
6208        null;
6209 
6210        FETCH trip_stop_info INTO l_stop_id, l_stop_wt_uom, l_stop_vol_uom, l_stop_status, l_tmp_dep_gross_wt, l_tmp_dep_net_wt, l_tmp_dep_vol, l_shipment_type_flag, l_wv_frozen_flag;
6211 
6212        EXIT WHEN (trip_stop_info%NOTFOUND);
6213      END LOOP;
6214 
6215      --need to have only 1 warning for a trip
6216      IF l_stop_num_warn>0 OR l_trip_num_warn>0  THEN
6217           l_num_warn := l_num_warn + 1;
6218      END IF;
6219 
6220      /*********LOOP for stop end***********/
6221 
6222      CLOSE trip_stop_info;
6223 
6224      goto loop_end;
6225 
6226      <<wt_vol_error>>
6227 
6228       FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_WT_VOL_ERROR');
6229       --
6230       -- Debug Statements
6231       --
6232       IF l_debug_on THEN
6233           WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
6234       END IF;
6235       --
6236       FND_MESSAGE.SET_TOKEN('TRIP_NAME',wsh_trips_pvt.get_name(p_trip_rows(i)));
6237       wsh_util_core.add_message(x_return_status);
6238       IF (x_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR) THEN
6239         l_num_error := l_num_error + 1;
6240       ELSIF (x_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING) THEN
6241         l_num_warn := l_num_warn + 1;
6242       END IF;
6243 
6244      <<loop_end>>
6245      -- bug 2366163: make sure this cursor gets closed.
6246      IF trip_stop_info%ISOPEN THEN
6247         CLOSE trip_stop_info;
6248      END IF;
6249 
6250      IF lock_stop%ISOPEN THEN
6251         CLOSE lock_stop;
6252      END IF;
6253 
6254    END LOOP;
6255    /*********TRIP LOOP END*********/
6256 
6257    IF l_debug_on THEN
6258      WSH_DEBUG_SV.log(l_module_name,'RET STATUS-'||x_return_status);
6259      WSH_DEBUG_SV.log(l_module_name,'NUM WARN-'||l_num_warn);
6260      WSH_DEBUG_SV.log(l_module_name,'NUM ERR-'||l_num_error);
6261      WSH_DEBUG_SV.log(l_module_name,'TRIP ROW COUNT-'||p_trip_rows.count);
6262    END IF;
6263    IF (l_num_error > 0) OR (l_num_warn > 0) THEN
6264      -- Bug 3688384 Display separate message for one trip .
6265      IF (p_trip_rows.count = 1) and (l_num_error = 0) and (l_num_warn > 0) THEN
6266     FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_WT_VOL_WARN');
6267     FND_MESSAGE.SET_TOKEN('TRIP_NAME',wsh_trips_pvt.get_name(p_trip_rows(1)));
6268      ELSIF (p_trip_rows.count = 1) and (l_num_error > 0) THEN
6269     FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_WT_VOL_ERR');
6270     FND_MESSAGE.SET_TOKEN('TRIP_NAME',wsh_trips_pvt.get_name(p_trip_rows(1)));
6271      ELSIF (p_trip_rows.count > 1) then
6272      -- Bug 3688384 .
6273     FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_WT_VOL_SUMMARY');
6274     FND_MESSAGE.SET_TOKEN('NUM_WARN',l_num_warn);
6275     FND_MESSAGE.SET_TOKEN('NUM_ERROR',l_num_error);
6276     FND_MESSAGE.SET_TOKEN('NUM_SUCCESS',p_trip_rows.count - l_num_error - l_num_warn);
6277      END IF;
6278      IF (p_trip_rows.count = l_num_error) THEN
6279        x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6280      ELSE
6281        x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
6282      END IF;
6283 
6284      wsh_util_core.add_message(x_return_status);
6285 
6286    ELSE
6287      x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
6288    END IF;
6289 
6290    IF l_debug_on THEN
6291      WSH_DEBUG_SV.log(l_module_name,'RET STATUS-'||x_return_status);
6292      WSH_DEBUG_SV.log(l_module_name,'NUM WARN-'||l_num_warn);
6293      WSH_DEBUG_SV.log(l_module_name,'NUM ERR-'||l_num_error);
6294    END IF;
6295    --
6296    -- Debug Statements
6297    --
6298    IF l_debug_on THEN
6299        WSH_DEBUG_SV.pop(l_module_name);
6300    END IF;
6301    --
6302    EXCEPTION
6303      WHEN others THEN
6304       IF lock_stop%ISOPEN THEN
6305          CLOSE lock_stop;
6306       END IF;
6307       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.TRIP_WEIGHT_VOLUME');
6308       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
6309 
6310 --
6311 -- Debug Statements
6312 --
6313 IF l_debug_on THEN
6314     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
6315     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
6316 END IF;
6317 --
6318 END trip_weight_volume;
6319 
6320 
6321 PROCEDURE validate_stop_sequence ( p_trip_id IN NUMBER,
6322                      x_return_status OUT NOCOPY  VARCHAR2) IS
6323 
6324 CURSOR stops IS
6325 SELECT stop_id,
6326      stop_sequence_number,
6327      planned_arrival_date,
6328      planned_departure_date
6329 FROM   wsh_trip_stops
6330 WHERE  trip_id = p_trip_id
6331 ORDER BY stop_sequence_number;
6332 
6333 l_old_arrival_date   DATE;
6334 l_old_departure_date DATE;
6335 l_start_flag       BOOLEAN := TRUE;
6336 l_stop_seq_num    NUMBER;
6337 
6338 invalid_sequence   EXCEPTION;
6339 
6340 --
6341 l_debug_on BOOLEAN;
6342 --
6343 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'VALIDATE_STOP_SEQUENCE';
6344 --
6345 BEGIN
6346 
6347    --
6348    -- Debug Statements
6349    --
6350    --
6351    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
6352    --
6353    IF l_debug_on IS NULL
6354    THEN
6355        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
6356    END IF;
6357    --
6358    IF l_debug_on THEN
6359        WSH_DEBUG_SV.push(l_module_name);
6360        --
6361        WSH_DEBUG_SV.log(l_module_name,'P_TRIP_ID',P_TRIP_ID);
6362    END IF;
6363    --
6364    FOR s IN stops LOOP
6365 
6366     l_stop_seq_num := s.stop_sequence_number;
6367 
6368     IF (l_start_flag) THEN
6369       l_old_arrival_date := s.planned_arrival_date;
6370       l_old_departure_date := s.planned_departure_date;
6371      ELSE
6372 
6373       IF (s.planned_arrival_date IS NOT NULL) THEN
6374         IF (s.planned_arrival_date < nvl(l_old_arrival_date, s.planned_arrival_date)) THEN
6375          raise invalid_sequence;
6376          END IF;
6377         l_old_arrival_date := s.planned_arrival_date;
6378        END IF;
6379 
6380       IF (s.planned_departure_date IS NOT NULL) THEN
6381         IF (s.planned_departure_date < nvl(l_old_departure_date, s.planned_departure_date)) THEN
6382          raise invalid_sequence;
6383          END IF;
6384         l_old_departure_date := s.planned_departure_date;
6385        END IF;
6386 
6387      END IF;
6388 
6389    END LOOP;
6390 
6391    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
6392 
6393 --
6394 -- Debug Statements
6395 --
6396 IF l_debug_on THEN
6397     WSH_DEBUG_SV.pop(l_module_name);
6398 END IF;
6399 --
6400    EXCEPTION
6401     WHEN invalid_sequence THEN
6402        FND_MESSAGE.SET_NAME('WSH','WSH_TRIP_SEQUENCE_INVALID');
6403       --
6404       -- Debug Statements
6405       --
6406       IF l_debug_on THEN
6407           WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIPS_PVT.GET_NAME',WSH_DEBUG_SV.C_PROC_LEVEL);
6408       END IF;
6409       --
6410       FND_MESSAGE.SET_TOKEN('TRIP_NAME',wsh_trips_pvt.get_name(p_trip_id));
6411       wsh_util_core.add_message(x_return_status);
6412       --
6413       -- Debug Statements
6414       --
6415       IF l_debug_on THEN
6416           WSH_DEBUG_SV.logmsg(l_module_name,'INVALID_SEQUENCE exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
6417           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:INVALID_SEQUENCE');
6418       END IF;
6419       --
6420      WHEN others THEN
6421       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.VALIDATE_STOP_SEQUENCE');
6422       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
6423 
6424 --
6425 -- Debug Statements
6426 --
6427 IF l_debug_on THEN
6428     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
6429     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
6430 END IF;
6431 --
6432 END validate_stop_sequence;
6433 
6434 PROCEDURE Check_Unassign_Trip(
6435       p_del_rows      IN    wsh_util_core.id_tab_type,
6436       x_trip_rows    OUT NOCOPY   wsh_util_core.id_tab_type,
6437       x_return_status    OUT NOCOPY    VARCHAR2) IS
6438 
6439 cnt NUMBER := 0;
6440 l_count NUMBER;
6441 stmt_str VARCHAR2(2000) := NULL;
6442 del_str  VARCHAR2(2000) := NULL;
6443 
6444 TYPE deltripcurtype IS REF CURSOR;
6445 
6446 deltrip_cv  deltripcurtype;
6447 
6448 others EXCEPTION;
6449 
6450 
6451 --
6452 l_debug_on BOOLEAN;
6453 --
6454 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'CHECK_UNASSIGN_TRIP';
6455 --
6456 BEGIN
6457 
6458    --
6459    -- Debug Statements
6460    --
6461    --
6462    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
6463    --
6464    IF l_debug_on IS NULL
6465    THEN
6466        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
6467    END IF;
6468    --
6469    IF l_debug_on THEN
6470        WSH_DEBUG_SV.push(l_module_name);
6471    END IF;
6472    --
6473    x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
6474 
6475    IF (p_del_rows.count = 0) THEN
6476      raise others;
6477    END IF;
6478 
6479    FOR i IN 1..p_del_rows.count - 1 LOOP
6480     del_str := del_str || to_char(p_del_rows(i)) || ',';
6481    END LOOP;
6482 
6483    del_str := del_str || to_char(p_del_rows(p_del_rows.count));
6484 
6485    -- bug 2374603: we cannot unassign from PLANNED trips
6486    --J can assign from planned trip
6487    stmt_str := 'select st.trip_id, count(*) '||
6488             'from   wsh_trip_stops st, wsh_delivery_legs dg, wsh_trips tr '||
6489             'where  dg.pick_up_stop_id = st.stop_id AND '||
6490                            'tr.trip_id = st.trip_id AND tr.planned_flag IN (''N'',''Y'') AND ' ||
6491          'dg.delivery_id IN (' || del_str || ') ' ||
6492             'group by st.trip_id '||
6493             'having count(*) = '||to_char(p_del_rows.count);
6494 
6495    OPEN deltrip_cv FOR stmt_str;
6496 
6497    LOOP
6498     cnt := cnt + 1;
6499     FETCH deltrip_cv INTO x_trip_rows(cnt), l_count;
6500     EXIT WHEN deltrip_cv%NOTFOUND;
6501    END LOOP;
6502 
6503    CLOSE deltrip_cv;
6504 
6505    IF (cnt = 1) THEN -- cnt := cnt + 1 before we exit loop above, so cnt is always > 0, bug 2434673.
6506      FND_MESSAGE.SET_NAME('WSH','WSH_DEL_MULTI_UNASSIGN_ERROR');
6507     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6508     wsh_util_core.add_message(x_return_status);
6509    END IF;
6510 
6511 --
6512 -- Debug Statements
6513 --
6514 IF l_debug_on THEN
6515     WSH_DEBUG_SV.pop(l_module_name);
6516 END IF;
6517 --
6518    EXCEPTION
6519      WHEN others THEN
6520       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.CHECK_UNASSIGN_TRIP');
6521       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
6522 
6523 --
6524 -- Debug Statements
6525 --
6526 IF l_debug_on THEN
6527     WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
6528     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
6529 END IF;
6530 --
6531 END Check_Unassign_Trip;
6532 
6533 
6534 PROCEDURE Unassign_Trip(
6535       p_del_rows      IN    wsh_util_core.id_tab_type,
6536       p_trip_id         IN   NUMBER,
6537       x_return_status    OUT NOCOPY    VARCHAR2) IS
6538 
6539 cursor get_trip_info(p_trip_id in number) is
6540 SELECT name, planned_flag
6541 FROM   wsh_trips
6542 WHERE  trip_id = p_trip_id;
6543 
6544 
6545 cursor get_consol_child_deliveries(p_trip_id in number, p_delivery_id in number) is
6546 select l.delivery_id from wsh_delivery_legs l, wsh_trip_stops s
6547 where l.delivery_id = p_delivery_id
6548 and l.parent_delivery_leg_id is not null
6549 and l.pick_up_stop_id = s.stop_id
6550 and s.trip_id = p_trip_id;
6551 
6552 l_trip_tab        WSH_UTIL_CORE.id_tab_type;
6553 l_mdc_del_tab     WSH_UTIL_CORE.id_tab_type;
6554 l_return_status   VARCHAR2(10);
6555 l_trip_name       VARCHAR2(30);
6556 l_planned_flag    wsh_trips.planned_flag%TYPE;
6557 j                 NUMBER := 0;
6558 unassign_deliveries_err   EXCEPTION;
6559 reprice_required_err      EXCEPTION;
6560 trip_not_found            EXCEPTION;
6561 trip_planned              EXCEPTION;
6562 
6563 --WF: CMR
6564 l_del_old_carrier_ids WSH_UTIL_CORE.ID_TAB_TYPE;
6565 l_wf_rs VARCHAR2(1);
6566 
6567 
6568 --
6569 l_debug_on BOOLEAN;
6570 --
6571 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'UNASSIGN_TRIP';
6572 --
6573 BEGIN
6574 
6575    -- bug 2374603: check that trip is not planned
6576    --
6577    -- Debug Statements
6578    --
6579    --
6580    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
6581    --
6582    IF l_debug_on IS NULL
6583    THEN
6584        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
6585    END IF;
6586    --
6587    IF l_debug_on THEN
6588        WSH_DEBUG_SV.push(l_module_name);
6589        --
6590        WSH_DEBUG_SV.log(l_module_name,'P_TRIP_ID',P_TRIP_ID);
6591    END IF;
6592    --
6593    OPEN   get_trip_info(p_trip_id);
6594    FETCH  get_trip_info INTO l_trip_name, l_planned_flag;
6595    IF get_trip_info%NOTFOUND THEN
6596      l_trip_name    := NULL;
6597      l_planned_flag := NULL;
6598    END IF;
6599    CLOSE  get_trip_info;
6600 
6601    IF l_trip_name IS NULL THEN
6602      l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6603      raise trip_not_found;
6604    END IF;
6605 
6606    IF l_planned_flag = 'F' THEN
6607      l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6608      raise trip_planned;
6609    END IF;
6610 
6611    --- MDC: Check if the deliveries are assigned to consol deliveries at this trip, and unassgin them from the consol delas well.
6612    FOR i in 1..p_del_rows.count LOOP
6613        j := j + 1;
6614        OPEN get_consol_child_deliveries(p_trip_id, p_del_rows(i));
6615        FETCH get_consol_child_deliveries
6616        INTO l_mdc_del_tab(j);
6617        IF get_consol_child_deliveries%NOTFOUND THEN
6618           l_mdc_del_tab.delete(j);
6619           j := j - 1;
6620        END IF;
6621        CLOSE get_consol_child_deliveries;
6622    END LOOP;
6623 
6624    IF l_mdc_del_tab.count > 0 THEN
6625 
6626       WSH_NEW_DELIVERY_ACTIONS.Unassign_Dels_from_Consol_Del(
6627           p_parent_del     => NULL,
6628           p_caller         => 'WSH_UNASSIGN_TRIP',
6629           p_del_tab        => l_mdc_del_tab,
6630           x_return_status  => l_return_status);
6631       IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
6632          raise unassign_deliveries_err;
6633       ELSE
6634          x_return_status := l_return_status;
6635       END IF;
6636 
6637    END IF;
6638 
6639    /*CURRENTLY NOT IN USE
6640    --WF: CMR
6641    WSH_WF_STD.Get_Carrier(p_del_ids => p_del_rows,
6642                           x_del_old_carrier_ids => l_del_old_carrier_ids,
6643                           x_return_status => l_wf_rs);
6644    */
6645    --
6646    -- Debug Statements
6647    --
6648    IF l_debug_on THEN
6649        WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_ACTIONS.UNASSIGN_DELIVERIES',WSH_DEBUG_SV.C_PROC_LEVEL);
6650    END IF;
6651    --
6652    wsh_delivery_legs_actions.unassign_deliveries( p_del_rows, p_trip_id, NULL, NULL, l_return_status);
6653    IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
6654       raise unassign_deliveries_err;
6655    ELSE
6656        x_return_status := l_return_status;
6657    END IF;
6658 
6659   /*CURRENTLY NOT IN USE
6660    --WF: CMR
6661    WSH_WF_STD.Handle_Trip_Carriers(p_trip_id => p_trip_id,
6662 			           p_del_ids => p_del_rows,
6663 			           p_del_old_carrier_ids => l_del_old_carrier_ids,
6664 			           x_return_status => l_wf_rs);
6665   */
6666 
6667    l_trip_tab.delete;
6668    l_trip_tab(1) := p_trip_id;
6669    --
6670    -- Debug Statements
6671    --
6672    IF l_debug_on THEN
6673        WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_DELIVERY_LEGS_ACTIONS.MARK_REPRICE_REQUIRED',WSH_DEBUG_SV.C_PROC_LEVEL);
6674    END IF;
6675    --
6676    WSH_DELIVERY_LEGS_ACTIONS.Mark_Reprice_Required(
6677                 p_entity_type    => 'TRIP',
6678                 p_entity_ids     => l_trip_tab,
6679                 x_return_status  => l_return_status);
6680    IF l_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR THEN
6681       raise reprice_required_err;
6682    ELSE
6683       IF x_return_status = WSH_UTIL_CORE.G_RET_STS_SUCCESS  AND
6684          l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
6685          x_return_status := l_return_status;
6686       END IF;
6687    END IF;
6688 
6689  --
6690  -- Debug Statements
6691  --
6692  IF l_debug_on THEN
6693      WSH_DEBUG_SV.pop(l_module_name);
6694  END IF;
6695  --
6696  EXCEPTION
6697 
6698      WHEN trip_not_found THEN
6699          x_return_status := l_return_status;
6700          fnd_message.set_name('WSH', 'WSH_TRIP_NOT_FOUND');
6701          wsh_util_core.add_message(x_return_status);
6702 
6703                  --
6704                  -- Debug Statements
6705                  --
6706                  IF l_debug_on THEN
6707                      WSH_DEBUG_SV.logmsg(l_module_name,'TRIP_NOT_FOUND exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
6708                      WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:TRIP_NOT_FOUND');
6709                  END IF;
6710                  --
6711      WHEN unassign_deliveries_err THEN
6712          x_return_status := l_return_status;
6713       fnd_message.set_name('WSH', 'WSH_DEL_UNASSIGN_ERROR');
6714       fnd_message.set_token('TRIP_NAME', l_trip_name);
6715       wsh_util_core.add_message(x_return_status);
6716 
6717                 --
6718                 -- Debug Statements
6719                 --
6720                 IF l_debug_on THEN
6721                     WSH_DEBUG_SV.logmsg(l_module_name,'UNASSIGN_DELIVERIES_ERR exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
6722                     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:UNASSIGN_DELIVERIES_ERR');
6723                 END IF;
6724                 --
6725      WHEN reprice_required_err THEN
6726       x_return_status := l_return_status;
6727       fnd_message.set_name('WSH', 'WSH_REPRICE_REQUIRED_ERR');
6728       wsh_util_core.add_message(x_return_status);
6729 
6730 --
6731 -- Debug Statements
6732 --
6733 IF l_debug_on THEN
6734     WSH_DEBUG_SV.logmsg(l_module_name,'REPRICE_REQUIRED_ERR exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
6735     WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:REPRICE_REQUIRED_ERR');
6736 END IF;
6737 --
6738      WHEN trip_planned THEN
6739         x_return_status := l_return_status;
6740         fnd_message.set_name('WSH', 'WSH_PLANNED_TRIP_NO_ACTION');
6741         fnd_message.set_token('TRIP_NAME', l_trip_name);
6742         wsh_util_core.add_message(x_return_status);
6743 
6744           --
6745           -- Debug Statements
6746           --
6747           IF l_debug_on THEN
6748               WSH_DEBUG_SV.logmsg(l_module_name,'TRIP_PLANNED exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
6749               WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:TRIP_PLANNED');
6750           END IF;
6751           --
6752      WHEN others THEN
6753         IF get_trip_info%ISOPEN THEN
6754           CLOSE get_trip_info;
6755         END IF;
6756       wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.UNASSIGN_TRIP');
6757    x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
6758 
6759      --
6760      -- Debug Statements
6761      --
6762      IF l_debug_on THEN
6763          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
6764          WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
6765      END IF;
6766      --
6767 END Unassign_Trip;
6768 
6769 -- Start of comments
6770 -- API name : Fte_Load_Tender
6771 -- Type     : Public
6772 -- Pre-reqs : None.
6773 -- Function : Calls the FTE API to check for any change in Stop Info
6774 -- Parameters :
6775 -- IN:
6776 --    p_stop_id      IN NUMBER Required
6777 --    p_gross_weight IN  NUMBER
6778 --      Gross Wt. of the stop
6779 --    p_net_weight IN  NUMBER
6780 --      Net Wt. of the stop
6781 --    p_volume       IN  NUMBER
6782 --      Volume of the stop
6783 --    p_fill_percent IN  NUMBER
6784 --      Fill Percent of the stop
6785 -- OUT:
6786 --    x_return_status OUT VARCHAR2 Required
6787 --       give the return status of API
6788 -- Version : 1.0
6789 -- End of comments
6790 
6791 PROCEDURE Fte_Load_Tender(
6792             p_stop_id       IN NUMBER,
6793             p_gross_weight  IN NUMBER,
6794             p_net_weight    IN NUMBER,
6795             p_volume        IN NUMBER,
6796             p_fill_percent  IN NUMBER,
6797             x_return_status OUT NOCOPY  VARCHAR2) IS
6798 
6799 l_stop_rec   WSH_TRIP_STOPS_PVT.TRIP_STOP_REC_TYPE;
6800 l_trip_rec   WSH_TRIPS_PVT.TRIP_REC_TYPE;
6801 l_return_status VARCHAR2(1);
6802 
6803 l_debug_on BOOLEAN;
6804 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'FTE_LOAD_TENDER';
6805 
6806 BEGIN
6807 
6808   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
6809   IF l_debug_on IS NULL THEN
6810     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
6811   END IF;
6812   --
6813   IF l_debug_on THEN
6814     WSH_DEBUG_SV.push(l_module_name);
6815     WSH_DEBUG_SV.log(l_module_name,'P_STOP_ID',P_STOP_ID);
6816     WSH_DEBUG_SV.log(l_module_name,'P_GROSS_WEIGHT',P_GROSS_WEIGHT);
6817     WSH_DEBUG_SV.log(l_module_name,'P_NET_WEIGHT',P_NET_WEIGHT);
6818     WSH_DEBUG_SV.log(l_module_name,'P_VOLUME',P_VOLUME);
6819     WSH_DEBUG_SV.log(l_module_name,'P_FILL_PERCENT',P_FILL_PERCENT);
6820   END IF;
6821   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
6822 
6823   -- Get pvt type record structure for stop
6824   IF l_debug_on THEN
6825     WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_TRIP_STOPS_GRP.GET_STOP_DETAILS_PVT',WSH_DEBUG_SV.C_PROC_LEVEL);
6826   END IF;
6827   --
6828   wsh_trip_stops_grp.get_stop_details_pvt(
6829     p_stop_id => p_stop_id,
6830     x_stop_rec => l_stop_rec,
6831     x_return_status => l_return_status);
6832 
6833   IF l_return_status <> WSH_UTIL_CORE.G_RET_STS_SUCCESS THEN
6834     x_return_status := l_return_status;
6835 
6836     IF l_debug_on THEN
6837       WSH_DEBUG_SV.pop(l_module_name);
6838     END IF;
6839     --
6840     RETURN;
6841   END IF;
6842 
6843   l_stop_rec.departure_gross_weight := p_gross_weight;
6844   l_stop_rec.departure_net_weight   := p_net_weight;
6845   l_stop_rec.departure_volume       := p_volume;
6846   l_stop_rec.departure_fill_percent := p_fill_percent;
6847 
6848   IF l_debug_on THEN
6849     WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_FTE_INTEGRATION.TRIP_STOP_VALIDATIONS',WSH_DEBUG_SV.C_PROC_LEVEL);
6850   END IF;
6851   --
6852   WSH_FTE_INTEGRATION.trip_stop_validations(
6853     p_stop_rec => l_stop_rec,
6854     p_trip_rec => l_trip_rec,
6855     p_action   => 'UPDATE',
6856     x_return_status => l_return_status);
6857 
6858   x_return_status := l_return_status;
6859 
6860   IF l_debug_on THEN
6861     WSH_DEBUG_SV.logmsg(l_module_name,'x_return_status '||x_return_status);
6862     WSH_DEBUG_SV.pop(l_module_name);
6863   END IF;
6864 
6865 EXCEPTION
6866   WHEN others THEN
6867     wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.Fte_Load_Tender');
6868     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
6869 
6870     IF l_debug_on THEN
6871       WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
6872       WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
6873     END IF;
6874 END Fte_Load_Tender;
6875 
6876 
6877 -- bug 3516052
6878 -- -----------------------------------------------------------------------
6879 -- Name
6880 --   PROCEDURE Reset_Stop_Planned_Dates
6881 -- Purpose
6882 --   This procedure reset the stop planned dates to be 10 minutes apart
6883 --   within a trip based on stop_sequence_number.
6884 --   It is used by the upgrade script to upgrade 11.5.9 and 11.5.8 data.
6885 --   It assumes that stop sequence numbers had been set correctly.
6886 --   For 11.5.10 or later, make sure this is called after
6887 --   reset_stop_seq_number
6888 -- Input Parameters:
6889 --   p_trip_ids : list of trip ids to be updated
6890 --
6891 -- Output Parameters:
6892 --   x_success_trip_ids list of trip ids validated or udpated
6893 --   x_return_status  - Success: if all of the trips are validated or updated
6894 --                      Warning: if some of the trips are validated or updated
6895 --                      Error  : if none of the trips are validated or updated
6896 --                      Unexpected Error
6897 -- ----------------------------------------------------------------------
6898 PROCEDURE Reset_Stop_Planned_Dates
6899     (   p_trip_ids          IN         wsh_util_core.id_tab_type,
6900         p_caller            IN  VARCHAR2,
6901         x_success_trip_ids  OUT NOCOPY wsh_util_core.id_tab_type,
6902         x_return_status     OUT NOCOPY VARCHAR2)
6903 IS
6904 
6905   CURSOR lock_stop(c_stop_id NUMBER) IS
6906   SELECT stop_id
6907   FROM   wsh_trip_stops
6908   WHERE  stop_id = c_stop_id
6909   FOR UPDATE NOWAIT;
6910 
6911   CURSOR c_trip_stops (c_trip_id NUMBER )IS
6912   SELECT wst.stop_sequence_number,
6913          wst.stop_id,
6914          wst.status_code,
6915          wst.planned_arrival_date,
6916          wst.planned_departure_date,
6917          wst.actual_arrival_date,
6918          wst.actual_departure_date
6919   FROM wsh_trip_stops wst,
6920        wsh_trips wtp
6921   WHERE wtp.status_code in ('OP', 'IT')
6922   AND   wtp.trip_id = c_trip_id
6923   AND   wtp.trip_id = wst.trip_id
6924   ORDER  BY wst.stop_sequence_number,wst.stop_id;
6925 
6926 l_stop_rec        c_trip_stops%ROWTYPE;
6927 l_last_stop_rec   c_trip_stops%ROWTYPE;
6928 
6929 l_trip_ids           wsh_util_core.id_tab_type;
6930 l_success_trip_ids   wsh_util_core.id_tab_type;
6931 l_return_status      VARCHAR2(1);
6932 
6933 l_update_flag     BOOLEAN;
6934 l_trip_error      BOOLEAN;
6935 l_first_stop      BOOLEAN;
6936 l_locked_stop_id  NUMBER;
6937 i                 NUMBER;
6938 l_warn_num        NUMBER;
6939 l_base_date       DATE;
6940 l_debug_on        BOOLEAN;
6941 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'RESET_STOP_PLANNED_DATES';
6942 
6943 stop_locked EXCEPTION;
6944 
6945 l_stop_tab WSH_UTIL_CORE.id_tab_type;  -- DBI Project
6946 l_dbi_rs    VARCHAR2(1);               -- DBI Project
6947 
6948 PRAGMA EXCEPTION_INIT(stop_locked, -00054);
6949 
6950 BEGIN
6951   l_warn_num := 0;
6952   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
6953   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
6954   IF l_debug_on IS NULL THEN
6955     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
6956   END IF;
6957   --
6958   IF l_debug_on THEN
6959     WSH_DEBUG_SV.push(l_module_name);
6960     WSH_DEBUG_SV.log(l_module_name,'p_trip_ids.count ', p_trip_ids.count);
6961   END IF;
6962 
6963   IF p_trip_ids.count = 0 THEN
6964     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6965     IF l_debug_on THEN
6966         WSH_DEBUG_SV.logmsg(l_module_name,' p_trip_ids.count is zero');
6967         WSH_DEBUG_SV.pop(l_module_name);
6968     END IF;
6969     return;
6970   END IF;
6971 
6972   i:= p_trip_ids.first;
6973   WHILE i is not NULL LOOP
6974      savepoint  start_of_the_trip;
6975      l_trip_error := FALSE;
6976      l_first_stop := TRUE;
6977      IF l_debug_on THEN
6978          WSH_DEBUG_SV.logmsg(l_module_name,'p_trip_ids('||i||') ==> '||p_trip_ids(i));
6979      END IF;
6980      OPEN c_trip_stops(p_trip_ids(i));
6981      LOOP
6982         FETCH c_trip_stops INTO  l_stop_rec;
6983         EXIT WHEN c_trip_stops%NOTFOUND;
6984         l_update_flag := FALSE;
6985 
6986          IF l_debug_on THEN
6987             WSH_DEBUG_SV.log(l_module_name,'l_stop_rec.stop_sequence_number', l_stop_rec.stop_sequence_number);
6988             WSH_DEBUG_SV.log(l_module_name,'l_stop_rec.stop_id', l_stop_rec.stop_id);
6989             WSH_DEBUG_SV.log(l_module_name,'l_stop_rec.status_code', l_stop_rec.status_code);
6990             WSH_DEBUG_SV.log(l_module_name,'l_stop_rec.planned_arrival_date', to_char(l_stop_rec.planned_arrival_date, 'DD-MON-YYYY HH24:MI:SS'));
6991             WSH_DEBUG_SV.log(l_module_name,'l_stop_rec.planned_departure_date', to_char(l_stop_rec.planned_departure_date,'DD-MON-YYYY HH24:MI:SS'));
6992             WSH_DEBUG_SV.log(l_module_name,'l_stop_rec.actual_arrival_date',  to_char(l_stop_rec.actual_arrival_date,'DD-MON-YYYY HH24:MI:SS'));
6993             WSH_DEBUG_SV.log(l_module_name,'l_stop_rec.actual_departure_date', to_char(l_stop_rec.actual_departure_date,'DD-MON-YYYY HH24:MI:SS'));
6994          END IF;
6995 
6996 
6997 
6998         IF not l_first_stop AND l_stop_rec.status_code = 'OP' THEN
6999            -- update open stops only
7000            IF l_stop_rec.planned_arrival_date is NULL THEN
7001               l_update_flag := TRUE;
7002               l_stop_rec.planned_arrival_date := NVL(l_stop_rec.planned_departure_date, sysdate);
7003            END IF;
7004 
7005 
7006            IF l_last_stop_rec.status_code = 'OP' THEN
7007               -- the reviouse stop is Open
7008               l_base_date := greatest(
7009                           NVL(l_last_stop_rec.planned_arrival_date,NVL(l_last_stop_rec.planned_departure_date, sysdate)) ,
7010                           NVL(l_last_stop_rec.planned_departure_date, sysdate));
7011            ELSE
7012               -- consider actual arrival or departure date if the revious stop is Arrived or Closed
7013               l_base_date := greatest(
7014                           NVL(l_last_stop_rec.planned_arrival_date,NVL(l_last_stop_rec.planned_departure_date, sysdate)) ,
7015                           NVL(l_last_stop_rec.planned_departure_date, sysdate),
7016                           NVL(l_last_stop_rec.actual_departure_date, l_last_stop_rec.actual_arrival_date));
7017            END IF;
7018 
7019            IF l_stop_rec.planned_arrival_date <= l_base_date THEN
7020               l_stop_rec.planned_arrival_date := l_base_date  + WSH_TRIPS_ACTIONS.C_TEN_MINUTES;
7021               l_update_flag := TRUE;
7022            END IF;
7023 
7024            IF l_update_flag THEN
7025               IF l_stop_rec.planned_departure_date < l_stop_rec.planned_arrival_date THEN
7026                  l_stop_rec.planned_departure_date := l_stop_rec.planned_arrival_date;
7027               END IF;
7028 
7029               BEGIN
7030                  -- lock the trip stop
7031                  OPEN lock_stop(l_stop_rec.stop_id);
7032                  FETCH lock_stop INTO l_locked_stop_id;
7033                  UPDATE wsh_trip_stops
7034                  SET planned_arrival_date = l_stop_rec.planned_arrival_date,
7035                      planned_departure_date = l_stop_rec.planned_departure_date,
7036                      last_update_date  = SYSDATE,
7037                      last_updated_by   = FND_GLOBAL.user_id,
7038                      last_update_login = FND_GLOBAL.login_id
7039                  WHERE  stop_id = l_stop_rec.stop_id;
7040 
7041                  -- DBI Project
7042                  -- Updating  WSH_TRIP_STOPS.
7043                  -- Call DBI API after the Update.
7044                  -- This API will also check for DBI Installed or not
7045                  IF l_debug_on THEN
7046                     WSH_DEBUG_SV.log(l_module_name,'Calling DBI API.Stop id -',l_stop_rec.stop_id);
7047                  END IF;
7048                  l_stop_tab(1) := l_stop_rec.stop_id;
7049                  WSH_INTEGRATION.DBI_Update_Trip_Stop_Log
7050                     (p_stop_id_tab    => l_stop_tab,
7051                      p_dml_type       => 'UPDATE',
7052                      x_return_status  => l_dbi_rs);
7053 
7054                 IF l_debug_on THEN
7055                   WSH_DEBUG_SV.log(l_module_name,'Return Status after DBI Call-',l_dbi_rs);
7056                 END IF;
7057                 IF l_dbi_rs = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR THEN
7058                     x_return_status := l_dbi_rs;
7059                     -- just pass this return status to caller API
7060                     IF l_debug_on THEN
7061                         WSH_DEBUG_SV.log(l_module_name,'DBI API Returned Unexpected error '||x_return_status);
7062                         WSH_DEBUG_SV.pop(l_module_name);
7063                     END IF;
7064                     return;
7065                 END IF;
7066         -- End of Code for DBI Project
7067 
7068                  CLOSE lock_stop;
7069 
7070               EXCEPTION
7071                  WHEN stop_locked THEN
7072                     IF lock_stop%ISOPEN THEN
7073               CLOSE lock_stop;
7074                     END IF;
7075                     --
7076                     IF l_debug_on THEN
7077                       WSH_DEBUG_SV.logmsg(l_module_name,'ERROR: stop is locked, cannot update it');
7078                     END IF;
7079                     --
7080                     l_trip_error := TRUE;
7081                     exit;
7082                  WHEN OTHERS THEN
7083                     IF lock_stop%ISOPEN THEN
7084                       CLOSE lock_stop;
7085                     END IF;
7086                     l_trip_error := TRUE;
7087                     exit;
7088               END;
7089            END IF;
7090         END IF;
7091         -- stop the information about last queried stop
7092         l_last_stop_rec               := l_stop_rec;
7093 
7094     IF l_first_stop  THEN
7095        l_first_stop := FALSE;
7096     END IF;
7097 
7098      END LOOP;
7099      CLOSE c_trip_stops;
7100 
7101      -- call handle_internal_stop here
7102      l_trip_ids(1) := p_trip_ids(i);
7103      Handle_Internal_Stops
7104           (  p_trip_ids          =>  l_trip_ids,
7105              p_caller            => p_caller,
7106              x_success_trip_ids  => l_success_trip_ids,
7107              x_return_status     => l_return_status);
7108      IF l_return_status in (WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR, WSH_UTIL_CORE.G_RET_STS_ERROR) THEN
7109         l_trip_error := TRUE;
7110         exit;
7111      ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
7112         l_warn_num  := l_warn_num + 1;
7113      END IF;
7114 
7115 
7116 
7117      IF l_trip_error THEN
7118        rollback to  start_of_the_trip;
7119      ELSE
7120        x_success_trip_ids(x_success_trip_ids.count+1) := p_trip_ids(i);
7121      END IF;
7122   i := p_trip_ids.next(i);
7123   END LOOP;
7124 
7125   IF x_success_trip_ids.count = 0 THEN
7126      x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7127   ELSIF x_success_trip_ids.count <  p_trip_ids.count
7128         OR  l_warn_num > 0 THEN
7129      x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
7130 
7131   END IF;
7132 
7133   IF l_debug_on THEN
7134     WSH_DEBUG_SV.logmsg(l_module_name,'x_return_status '||x_return_status);
7135     WSH_DEBUG_SV.log(l_module_name,'l_warn_num ', l_warn_num);
7136     WSH_DEBUG_SV.pop(l_module_name);
7137   END IF;
7138 
7139 EXCEPTION
7140 
7141 
7142   WHEN others THEN
7143     IF lock_stop%ISOPEN THEN
7144        close lock_stop;
7145     END IF;
7146     IF c_trip_stops%ISOPEN THEN
7147        close c_trip_stops;
7148     END IF;
7149     wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.reset_stop_planned_dates');
7150     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
7151 
7152     IF l_debug_on THEN
7153       WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
7154       WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
7155     END IF;
7156 
7157 END reset_stop_planned_dates;
7158 
7159 
7160 -- -----------------------------------------------------------------------
7161 -- Name
7162 --   PROCEDURE Reset_Stop_Planned_Dates
7163 -- Purpose
7164 --   This procedure reset the stop planned dates to be 10 minutes apart
7165 --   within a trip based on stop_sequence_number.
7166 --   It is used by the upgrade script to upgrade 11.5.9 and 11.5.8 data.
7167 --   It assumes that stop sequence numbers had been set correctly.
7168 --   For 11.5.10 or later, make sure this is called after
7169 --   reset_stop_seq_number
7170 -- Input Parameters:
7171 --   p_trip_id : trip id to be udpated
7172 --
7173 -- Output Parameters:
7174 --   x_success_trip_ids list of trip ids validated or udpated
7175 --   x_return_status  - Success: if the trip is validated or updated
7176 --                      Error  : if the trip could not be updated, usually due to locking issue
7177 --                      Unexpected Error
7178 -- ----------------------------------------------------------------------
7179 PROCEDURE reset_stop_planned_dates
7180     (   p_trip_id           IN  NUMBER,
7181         p_caller            IN  VARCHAR2,
7182         x_return_status     OUT NOCOPY  VARCHAR2)
7183 IS
7184 
7185 l_trip_ids          wsh_util_core.id_tab_type;
7186 l_success_trip_ids  wsh_util_core.id_tab_type;
7187 l_return_status     VARCHAR2(1);
7188 l_debug_on          BOOLEAN;
7189 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'RESET_STOP_PLANNED_DATES';
7190 
7191 BEGIN
7192 
7193   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
7194   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
7195   IF l_debug_on IS NULL THEN
7196     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
7197   END IF;
7198   --
7199   IF l_debug_on THEN
7200     WSH_DEBUG_SV.push(l_module_name);
7201     WSH_DEBUG_SV.log(l_module_name,'p_trip_id ', p_trip_id);
7202   END IF;
7203 
7204   IF p_trip_id is NULL THEN
7205     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7206     IF l_debug_on THEN
7207       WSH_DEBUG_SV.logmsg(l_module_name,'ERROR: Trip ID is NULL');
7208       WSH_DEBUG_SV.pop(l_module_name);
7209     END IF;
7210     return;
7211   END IF;
7212   l_trip_ids(1) := p_trip_id;
7213   WSH_TRIPS_ACTIONS.reset_stop_planned_dates
7214      ( p_trip_ids         => l_trip_ids,
7215        p_caller           => p_caller,
7216        x_success_trip_ids => l_success_trip_ids,
7217        x_return_status    => l_return_status);
7218   IF l_return_status  in (WSH_UTIL_CORE.G_RET_STS_ERROR , WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
7219     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7220   ELSE
7221     x_return_status := l_return_status;
7222   END IF;
7223 
7224   IF l_debug_on THEN
7225     WSH_DEBUG_SV.logmsg(l_module_name,'x_return_status '||x_return_status);
7226     WSH_DEBUG_SV.pop(l_module_name);
7227   END IF;
7228 
7229 EXCEPTION
7230 
7231   WHEN others THEN
7232     wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.reset_stop_planned_dates');
7233     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
7234 
7235     IF l_debug_on THEN
7236       WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
7237       WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
7238     END IF;
7239 
7240 END reset_stop_planned_dates;
7241 
7242 --
7243 -- -----------------------------------------------------------------------
7244 -- Name
7245 --   PROCEDURE Handle_Internal_Stops
7246 --
7247 -- Purpose
7248 --   This procedure links/unlinks the internal stops with the physical
7249 --   stops in the trip
7250 --   This is called after trip stops are created, updated, deleted, or
7251 --   upgraded. The main cursor respects the existing linking of the
7252 --   stops and feches rows in the order that the linked dummy stop is
7253 --   always immediately before the linked physical stop.
7254 --
7255 --   This procedure automatically resolves the date conflict caused by
7256 --   date change of a linked physical stop or forming of a new link.
7257 --   The caller of this procedure should always evaluate this particular logic
7258 --   to see if it meets the new requirements.
7259 --
7260 --   Note: the API has forked logic to handle either sequencing mode
7261 --         set by the profile WSH_STOP_SEQ_MODE.
7262 --
7263 --   Linking Rules:
7264 --      1. One dummy stop is linked exactly to one physical stop
7265 --      2. Linking happens when both dummy and physical are of OPEN status
7266 --      3. if the stop is at dummy location, and the next stop is at the
7267 --         mapping physical location, link the stop with next stop by
7268 --         pupulating physical_stop_id
7269 --      4. if the stop is at dummy location, and the previous stop is not
7270 --         already linked and at the mapping physical location and there
7271 --         does not exist a delivery picking up at the physical stop
7272 --         and droping off at the dummy stop,
7273 --         the dummy and physical stop should be flipped
7274 --      5. when linking is in place, the planned dates of the dummy
7275 --         location are always 10 second before the planned arrival
7276 --         date of the linked physical stop. Any other stop which
7277 --         has conflicted dates will be pushed to 10 second before the
7278 --         next stop. One planned date change of a linked physical stop
7279 --         could result in date changes of multiple stops in the trip.
7280 --
7281 --         If stop sequence mode is SSN, SSN of dummy will be offset
7282 --         from the physical stop's SSN by -1. One SSN change could
7283 --         result in SSN changes of multiple stops in the trip.
7284 --         If SSNs become 0 or negative because of adjustments,
7285 --         API will error out.
7286 --
7287 --      6. Date change on a linked physical location or forming a new link
7288 --         could cause date conflict with existing stops.
7289 --         When it happens, the dates of existing stops are automatically
7290 --         updated to avoid the date conflicts only if sequencing mode is PAD.
7291 --
7292 --   Unlinking Rules:
7293 --      1. Physical_stop_id of of a dummy stop is set to NULL to unlink
7294 --         it from the physical stop.
7295 --      2. Unlinking could happen to stops of OPEN or CLOSED status
7296 --      3. Unlink the dummy stop if the physical_location_id <> location_id
7297 --         of the physical stop
7298 --
7299 --      Note that in WSHSTGPB.pls, create_update_stop has logic to unlink
7300 --      the dummy stop if its stop location is modified so that its physical
7301 --      location does not match the physical stop's location and that
7302 --      in this same file WSHSTGPB.pls (add_to_delete_list), deleting
7303 --      a physical stop will also delete its linked dummy stop.
7304 --
7305 -- Therefore, it is sufficient for this API to only compare the physical
7306 -- stop's location to decide whether to unlink.
7307 --
7308 -- Input Parameters:
7309 --   p_trip_ids : list of trip ids to be processed
7310 --   p_caller:    if the caller is 'WSH_TRIP_UPGRADE', it will check the
7311 --                mapping of internal location. Otherwise, it just assume
7312 --                tha mapping has been checked previously.
7313 --
7314 -- Output Parameters:
7315 --   x_success_trip_ids list of trip ids processed
7316 --   x_return_status  - Success: if all of the trips are validated or updated
7317 --                      Warning: if some of the trips are validated or updated
7318 --                      Error  : if none of the trips are validated or updated
7319 --
7320 -- ----------------------------------------------------------------------
7321 PROCEDURE Handle_Internal_Stops
7322     (   p_trip_ids          IN  wsh_util_core.id_tab_type,
7323         p_caller            IN  VARCHAR2,
7324         x_success_trip_ids  OUT NOCOPY wsh_util_core.id_tab_type,
7325         x_return_status     OUT NOCOPY VARCHAR2)
7326 IS
7327     --
7328     -- make a list of stops sequenced by PAD,
7329     -- accounting for unlinking events (i.e., physical locations
7330     -- of currently linked stops no longer match the stop locations of the
7331     -- physical stops)
7332     --    stops that remain linked dummy stops will be automatically synchronized
7333     --    with the physical stops (PAD/PDD by 10 seconds)
7334     -- SSN is not synchronized in this cursor because RESET_STOP_SEQ_NUMBERS
7335     -- will update the SSNs.
7336     --
7337 CURSOR c_get_stops_PAD(c_trip_id NUMBER) IS
7338 SELECT wts.stop_id,
7339        wts.stop_location_id,
7340        wts.physical_location_id,
7341        wts.physical_stop_id,
7342        wts.stop_sequence_number,
7343 
7344        -- conditions to keep the link to synchronize the columns:
7345        --    a. physical stop exists
7346        --    b. (physical location matches or dummy stop's status is Arrived)
7347        --
7348        -- The same DECODE structure must be maintained for these columns
7349        -- in this cursor:
7350        --             PLANNED_ARRIVAL_DATE,
7351        --             PLANNED_DEPARTURE_DATE,
7352        --             BREAK_LINK_FLAG,
7353        --         and ORDER BY first clause
7354        DECODE(
7355               DECODE(wts.physical_stop_id,
7356                      NULL, 0,
7357                      NVL(pts.stop_id,-1),
7358                            DECODE(wts.physical_location_id,
7359                                   pts.stop_location_id, 1,
7360                                   DECODE(wts.status_code,'AR',1, 0)
7361                                  ),
7362                      0 -- linked to a non-existent physical stop
7363                     ),
7364               1, (pts.planned_arrival_date - C_TEN_SECONDS),
7365               wts.planned_arrival_date)    PLANNED_ARRIVAL_DATE,
7366 
7367        DECODE(
7368               DECODE(wts.physical_stop_id,
7369                      NULL, 0,
7370                      NVL(pts.stop_id,-1),
7371                            DECODE(wts.physical_location_id,
7372                                   pts.stop_location_id, 1,
7373                                   DECODE(wts.status_code,'AR',1, 0)
7374                                 ),
7375                      0 -- linked to a non-existent physical stop
7376                     ),
7377                1, (pts.planned_arrival_date - C_TEN_SECONDS),
7378               wts.planned_departure_date)    PLANNED_DEPARTURE_DATE,
7379 
7380        wts.stop_sequence_number  org_stop_seq_num,
7381        wts.planned_arrival_date org_pl_arr_date,
7382        wts.planned_departure_date org_pl_dep_date,
7383        wts.status_code,
7384 
7385        -- break_link_flag: 'Y' or 'N'
7386        -- The DECODE structure is slightly modified
7387        -- to decide whether link needs breaking.
7388        -- If stop is not linked (NULL), there is no link to break.
7389        -- If physical stop_id matches, check for need to break link.
7390        DECODE(
7391               DECODE(wts.physical_stop_id,
7392                      NULL, 1,  -- modified value because there is no link to break
7393                      NVL(pts.stop_id,-1),
7394                            DECODE(wts.physical_location_id,
7395                                   pts.stop_location_id, 1,
7396                                   DECODE(wts.status_code,'AR',1, 0)
7397                                  ),
7398                      0 -- linked to a non-existent physical stop
7399                     ),
7400               1, 'N',
7401               'Y')   BREAK_LINK_FLAG
7402 FROM wsh_trip_stops wts,
7403      wsh_trip_stops pts
7404 WHERE wts.trip_id = c_trip_id
7405       and wts.physical_stop_id = pts.stop_id(+)
7406 ORDER BY
7407       -- we need to order by PAD;
7408       -- if stop stays linked, use its physical stop's PAD.
7409       -- second column is to ensure the dummy stop will precede
7410       -- physical stop (as non-NULL always precede NULL).
7411       DECODE(
7412               DECODE(wts.physical_stop_id,
7413                      NULL, 0,
7414                      NVL(pts.stop_id,-1),
7415                            DECODE(wts.physical_location_id,
7416                                   pts.stop_location_id, 1,
7417                                   DECODE(wts.status_code,'AR',1, 0)
7418                                  ),
7419                      0 -- linked to a non-existent physical stop
7420                     ),
7421               1, pts.planned_arrival_date,
7422               wts.planned_arrival_date
7423             ),
7424       pts.stop_id;
7425 
7426 --
7427 -- make a list of stops sequenced by SSN,
7428 -- accounting for unlinking events (i.e,. physical locations
7429 -- of currently linked stops no longer match the stop locations of the
7430 -- physical stops)
7431 --    stops that remain linked dummy stops will be automatically
7432 --    synchronized with the physical stops (SSN will be offset by -1;
7433 --    PAD/PDD by 10 seconds).
7434 --
7435 --
7436 -- This cursor is derived from c_get_stops_PAD so that the ORDER BY clause
7437 -- can be modified to sort the stops by SSN, instead of PAD.
7438 --
7439 --
7440 CURSOR c_get_stops_SSN(c_trip_id NUMBER) IS
7441 SELECT wts.stop_id,
7442        wts.stop_location_id,
7443        wts.physical_location_id,
7444        wts.physical_stop_id,
7445 
7446        -- conditions to keep the link to synchronize the columns:
7447        --    a. physical stop exists/matches
7448        --    b. (physical location matches or dummy stop's status is Arrived)
7449        --
7450        -- The same DECODE structure must be maintained for these columns
7451        -- in this cursor:
7452        --             STOP_SEQUENCE_NUMBER
7453        --             PLANNED_ARRIVAL_DATE,
7454        --             PLANNED_DEPARTURE_DATE,
7455        --             BREAK_LINK_FLAG,
7456        --         and ORDER BY first clause
7457        DECODE(
7458               DECODE(wts.physical_stop_id,
7459                      NULL, 0,
7460                      NVL(pts.stop_id,-1),
7461                            DECODE(wts.physical_location_id,
7462                                   pts.stop_location_id, 1,
7463                                   DECODE(wts.status_code,'AR',1, 0)
7464                                  ),
7465                      0 -- linked to a non-existent physical stop
7466                     ),
7467               1, (pts.stop_sequence_number - 1),
7468               wts.stop_sequence_number)    STOP_SEQUENCE_NUMBER,
7469 
7470        DECODE(
7471               DECODE(wts.physical_stop_id,
7472                      NULL, 0,
7473                      NVL(pts.stop_id,-1),
7474                            DECODE(wts.physical_location_id,
7475                                   pts.stop_location_id, 1,
7476                                   DECODE(wts.status_code,'AR',1, 0)
7477                                  ),
7478                      0 -- linked to a non-existent physical stop
7479                     ),
7480               1, (pts.planned_arrival_date - C_TEN_SECONDS),
7481               wts.planned_arrival_date)    PLANNED_ARRIVAL_DATE,
7482 
7483        DECODE(
7484               DECODE(wts.physical_stop_id,
7485                      NULL, 0,
7486                      NVL(pts.stop_id,-1),
7487                            DECODE(wts.physical_location_id,
7488                                   pts.stop_location_id, 1,
7489                                   DECODE(wts.status_code,'AR',1, 0)
7490                                  ),
7491                      0 -- linked to a non-existent physical stop
7492                     ),
7493                1, (pts.planned_arrival_date - C_TEN_SECONDS),
7494               wts.planned_departure_date)    PLANNED_DEPARTURE_DATE,
7495 
7496        wts.stop_sequence_number  org_stop_seq_num,
7497        wts.planned_arrival_date org_pl_arr_date,
7498        wts.planned_departure_date org_pl_dep_date,
7499        wts.status_code,
7500 
7501        -- break_link_flag: 'Y' or 'N'
7502        -- The DECODE structure is modified
7503        -- to decide whether link needs breaking.
7504        -- If stop is not linked (NULL), there is no link to break.
7505        -- If physical stop_id matches, check for delinking.
7506        -- Otherwise, the physical stop must have been deleted.
7507        DECODE(
7508               DECODE(wts.physical_stop_id,
7509                      NULL, 1,  -- no link to break
7510                      NVL(pts.stop_id,-1),
7511                            DECODE(wts.physical_location_id,
7512                                   pts.stop_location_id, 1,
7513                                   DECODE(wts.status_code,'AR',1, 0)
7514                                  ),
7515                      0 -- linked to a non-existent physical stop
7516                     ),
7517               1, 'N',
7518               'Y')   BREAK_LINK_FLAG
7519 FROM wsh_trip_stops wts,
7520      wsh_trip_stops pts
7521 WHERE wts.trip_id = c_trip_id
7522       and wts.physical_stop_id = pts.stop_id(+)
7523 ORDER BY
7524       -- we need to order by SSN;
7525       -- if stop stays linked, use its physical stop's SSN.
7526       -- second column is to ensure the dummy stop will precede
7527       -- physical stop (as non-NULL always precede NULL).
7528       DECODE(
7529               DECODE(wts.physical_stop_id,
7530                      NULL, 0,
7531                      NVL(pts.stop_id,-1),
7532                            DECODE(wts.physical_location_id,
7533                                   pts.stop_location_id, 1,
7534                                   DECODE(wts.status_code,'AR',1, 0)
7535                                  ),
7536                      0 -- linked to a non-existent physical stop
7537                        -- (should never happen)
7538                     ),
7539               1, pts.stop_sequence_number,
7540               wts.stop_sequence_number
7541             ),
7542       pts.stop_id;
7543 
7544 CURSOR c_flip_disallowed(c_dummy_stop_id NUMBER , c_physical_stop_id NUMBER ) IS
7545  select delivery_leg_id
7546  from
7547     wsh_delivery_legs wlg
7548  where
7549     pick_up_stop_id = c_physical_stop_id
7550     AND drop_off_stop_id = c_dummy_stop_id
7551     AND rownum = 1;
7552 
7553 --
7554 l_getstops_stop_id             wsh_util_core.id_tab_type;
7555 l_getstops_stop_loc_id         wsh_util_core.id_tab_type;
7556 l_getstops_phys_loc_id         wsh_util_core.id_tab_type;
7557 l_getstops_phys_stop_id        wsh_util_core.id_tab_type;
7558 l_getstops_stop_seq_num        wsh_util_core.id_tab_type;
7559 l_getstops_pl_arr_date         wsh_util_core.date_tab_type;
7560 l_getstops_pl_dep_date         wsh_util_core.date_tab_type;
7561 l_getstops_org_stop_seq_num    wsh_util_core.id_tab_type;
7562 l_getstops_org_pl_arr_date     wsh_util_core.date_tab_type;
7563 l_getstops_org_pl_dep_date     wsh_util_core.date_tab_type;
7564 l_getstops_status_code         WSH_UTIL_CORE.Column_Tab_Type;
7565 l_getstops_break_link_flags    WSH_UTIL_CORE.Column_Tab_Type;
7566 
7567 -- both the cursors c_get_stops_PAD and c_get_stops_SSN select identical fields
7568 l_getstops_tmp                 c_get_stops_PAD%ROWTYPE;
7569 
7570 i                        NUMBER;
7571 j                        NUMBER;
7572 l_delivery_leg_id        NUMBER;
7573 l_warn_num               NUMBER;
7574 l_debug_on               BOOLEAN;
7575 l_update_flag            BOOLEAN;
7576 l_push_date_allowed      BOOLEAN;
7577 l_push_ssn_allowed       BOOLEAN;
7578 l_return_status          VARCHAR2(1);
7579 l_stop_details_rec       WSH_TRIP_STOPS_VALIDATIONS.stop_details;
7580 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'Handle_Internal_Stops';
7581 stop_locked              EXCEPTION;
7582 get_physical_loc_err     EXCEPTION;
7583 reset_stop_seq_number_error  EXCEPTION;
7584 invalid_stop_date EXCEPTION;
7585 invalid_stop_seq_num     EXCEPTION;
7586 invalid_ssn_adjustment   EXCEPTION;
7587 duplicate_stop_seq      EXCEPTION;
7588 
7589 validate_stop_date_error EXCEPTION;
7590 PRAGMA EXCEPTION_INIT(stop_locked, -00054);
7591 l_dbi_rs                VARCHAR2(1); -- DBI Project
7592 
7593 l_stop_seq_mode         NUMBER;
7594 
7595 BEGIN
7596   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
7597   l_warn_num := 0;
7598   x_success_trip_ids.delete;
7599 
7600   l_stop_seq_mode := WSH_TRIPS_ACTIONS.GET_STOP_SEQ_MODE;
7601 
7602   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
7603   IF l_debug_on IS NULL THEN
7604     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
7605   END IF;
7606   --
7607   IF l_debug_on THEN
7608     WSH_DEBUG_SV.push(l_module_name);
7609     WSH_DEBUG_SV.log(l_module_name,'p_trip_ids.count ', p_trip_ids.count);
7610     WSH_DEBUG_SV.log(l_module_name,'p_caller ', p_caller);
7611   END IF;
7612 
7613   IF p_trip_ids.count = 0 THEN
7614     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7615     IF l_debug_on THEN
7616         WSH_DEBUG_SV.logmsg(l_module_name,' p_trip_ids.count is zero');
7617         WSH_DEBUG_SV.pop(l_module_name);
7618     END IF;
7619     return;
7620   END IF;
7621 
7622   i:= p_trip_ids.first;
7623   WHILE i is not NULL LOOP
7624      SAVEPOINT start_of_the_trip;
7625      BEGIN
7626         l_update_flag := FALSE;
7627         IF l_debug_on THEN
7628             WSH_DEBUG_SV.logmsg(l_module_name,'p_trip_ids('||i||') ==> '||p_trip_ids(i));
7629         END IF;
7630 
7631         IF l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD THEN
7632 
7633           OPEN c_get_stops_PAD(p_trip_ids(i));
7634           FETCH c_get_stops_PAD BULK COLLECT INTO l_getstops_stop_id,
7635                                           l_getstops_stop_loc_id,
7636                                           l_getstops_phys_loc_id,
7637                                           l_getstops_phys_stop_id,
7638                                           l_getstops_stop_seq_num,
7639                                           l_getstops_pl_arr_date,
7640                                           l_getstops_pl_dep_date,
7641                                           l_getstops_org_stop_seq_num,
7642                                           l_getstops_org_pl_arr_date,
7643                                           l_getstops_org_pl_dep_date,
7644                                           l_getstops_status_code,
7645                                           l_getstops_break_link_flags;
7646           CLOSE c_get_stops_PAD;
7647         ELSE
7648           OPEN c_get_stops_SSN(p_trip_ids(i));
7649           FETCH c_get_stops_SSN BULK COLLECT INTO l_getstops_stop_id,
7650                                           l_getstops_stop_loc_id,
7651                                           l_getstops_phys_loc_id,
7652                                           l_getstops_phys_stop_id,
7653                                           l_getstops_stop_seq_num,
7654                                           l_getstops_pl_arr_date,
7655                                           l_getstops_pl_dep_date,
7656                                           l_getstops_org_stop_seq_num,
7657                                           l_getstops_org_pl_arr_date,
7658                                           l_getstops_org_pl_dep_date,
7659                                           l_getstops_status_code,
7660                                           l_getstops_break_link_flags;
7661           CLOSE c_get_stops_SSN;
7662         END IF;
7663 
7664         IF l_debug_on THEN
7665         WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_id.count', l_getstops_stop_id.count);
7666         END IF;
7667         IF l_getstops_stop_id.count > 0 THEN
7668            FOR j in l_getstops_stop_id.first .. l_getstops_stop_id.last LOOP
7669                IF l_debug_on THEN
7670                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_id('||j||')', l_getstops_stop_id(j));
7671                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_loc_id('||j||')', l_getstops_stop_loc_id(j));
7672                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_phys_loc_id('||j||')', l_getstops_phys_loc_id(j));
7673                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_phys_stop_id('||j||')', l_getstops_phys_stop_id(j));
7674                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_seq_num('||j||')', l_getstops_stop_seq_num(j));
7675                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_org_stop_seq_num('||j||')', l_getstops_org_stop_seq_num(j));
7676                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_pl_arr_date('||j||')', to_char(l_getstops_pl_arr_date(j),'DD-MON-YYYY HH24:MI:SS'));
7677                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_pl_dep_date('||j||')', to_char(l_getstops_pl_dep_date(j),'DD-MON-YYYY HH24:MI:SS'));
7678                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_org_pl_arr_date('||j||')', to_char(l_getstops_org_pl_arr_date(j),'DD-MON-YYYY HH24:MI:SS'));
7679                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_org_pl_dep_date('||j||')', to_char(l_getstops_org_pl_dep_date(j),'DD-MON-YYYY HH24:MI:SS'));
7680                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_status_code('||j||')', l_getstops_status_code(j));
7681                  WSH_DEBUG_SV.log(l_module_name,'l_getstops_break_link_flags('||j||')', l_getstops_break_link_flags(j));
7682                END IF;
7683               IF p_caller = 'WSH_TRIP_UPGRADE' THEN
7684                  WSH_LOCATIONS_PKG.Convert_internal_cust_location(
7685                     p_internal_cust_location_id => l_getstops_stop_loc_id(j),
7686                     x_internal_org_location_id  => l_getstops_phys_loc_id(j),
7687                     x_return_status             => l_return_status);
7688 
7689                  IF l_return_status in (FND_API.G_RET_STS_UNEXP_ERROR, FND_API.G_RET_STS_ERROR) THEN
7690                     RAISE get_physical_loc_err;
7691                  END IF;
7692               END IF;
7693 
7694               IF l_getstops_phys_loc_id(j) is not NULL THEN
7695                  -- current stop is a dummy stop
7696 
7697                  IF l_getstops_phys_stop_id(j) is not NULL THEN
7698                     -- check if unlinking is necessary
7699 
7700                     IF l_getstops_break_link_flags(j) = 'Y' THEN
7701                       IF l_debug_on THEN
7702                         WSH_DEBUG_SV.log(l_module_name,'unlink stop', l_getstops_stop_id(j));
7703                       END IF;
7704                       l_getstops_phys_stop_id(j) := NULL;
7705                       l_update_flag := TRUE;
7706                     END IF;
7707                  END IF;
7708 
7709                  -- check for linking to next stop or previous stop
7710                  IF l_getstops_stop_id.exists(j+1)
7711                     AND l_getstops_status_code(j) = 'OP'
7712                     AND l_getstops_status_code(j+1) = 'OP'
7713                     AND l_getstops_phys_loc_id(j) = l_getstops_stop_loc_id(j+1) THEN
7714 
7715                     -- link to next stop
7716                     IF l_debug_on THEN
7717                       WSH_DEBUG_SV.log(l_module_name,'link to next stop at stop', l_getstops_stop_id(j));
7718                     END IF;
7719                      l_getstops_phys_stop_id(j) := l_getstops_stop_id(j+1);
7720                      l_getstops_pl_arr_date(j) := l_getstops_pl_arr_date(j+1) - WSH_TRIPS_ACTIONS.C_TEN_SECONDS;
7721                      l_getstops_pl_dep_date(j) := l_getstops_pl_arr_date(j);
7722                      IF l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN THEN
7723                        l_getstops_stop_seq_num(j) := l_getstops_stop_seq_num(j+1) - 1;
7724                      END IF;
7725                      l_update_flag := TRUE;
7726                  ELSIF l_getstops_stop_id.exists(j-1)
7727                     AND l_getstops_status_code(j) = 'OP'
7728                     AND l_getstops_status_code(j-1) = 'OP'
7729                     AND l_getstops_phys_loc_id(j) = l_getstops_stop_loc_id(j-1)
7730                     AND (NOT l_getstops_stop_id.exists(j-2) OR
7731                          NVL(l_getstops_phys_stop_id(j-2), -99) <> l_getstops_stop_id(j-1))
7732                     THEN
7733                       -- flip and link
7734 
7735                       OPEN c_flip_disallowed(l_getstops_stop_id(j), l_getstops_stop_id(j-1));
7736                       FETCH c_flip_disallowed INTO l_delivery_leg_id;
7737                       IF c_flip_disallowed%NOTFOUND THEN
7738                          IF l_debug_on THEN
7739                             WSH_DEBUG_SV.log(l_module_name,'flip and link at stop', l_getstops_stop_id(j));
7740                          END IF;
7741                          -- flip these two stops
7742 
7743                          l_getstops_tmp.stop_id                 := l_getstops_stop_id(j);
7744                          l_getstops_tmp.stop_location_id        := l_getstops_stop_loc_id(j);
7745                          l_getstops_tmp.physical_location_id    := l_getstops_phys_loc_id(j);
7746                          l_getstops_tmp.status_code             := l_getstops_status_code(j);
7747 
7748                          l_getstops_stop_id(j)                  := l_getstops_stop_id(j-1);
7749                          l_getstops_stop_loc_id(j)      := l_getstops_stop_loc_id(j-1);
7750                          l_getstops_phys_loc_id(j)      := l_getstops_phys_loc_id(j-1);
7751                          l_getstops_phys_stop_id(j)         := l_getstops_phys_stop_id(j-1);
7752                          l_getstops_pl_arr_date(j)      := l_getstops_pl_arr_date(j-1);
7753                          l_getstops_pl_dep_date(j)      := l_getstops_pl_dep_date(j-1);
7754                          l_getstops_status_code(j)      := l_getstops_status_code(j-1);
7755 
7756                          l_getstops_stop_id(j-1)                := l_getstops_tmp.stop_id;
7757                          l_getstops_stop_loc_id(j-1)            := l_getstops_tmp.stop_location_id;
7758                          l_getstops_phys_loc_id(j-1)            := l_getstops_tmp.physical_location_id;
7759                          l_getstops_status_code(j-1)            := l_getstops_tmp.status_code;
7760                          l_getstops_phys_stop_id(j-1)           := l_getstops_stop_id(j);  -- link
7761                          l_getstops_pl_arr_date(j-1)            := l_getstops_pl_arr_date(j) - WSH_TRIPS_ACTIONS.C_TEN_SECONDS;
7762                          l_getstops_pl_dep_date(j-1)            := l_getstops_pl_arr_date(j-1);
7763 
7764                          IF l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN THEN
7765                            -- we need to adjust the sequence numbers of these stops flipped:
7766                            --   physical stop should retain the SSN.
7767                            --   dummy stop should have SSN subtracted by 1.
7768                            l_getstops_stop_seq_num(j) := l_getstops_stop_seq_num(j-1);
7769                            l_getstops_stop_seq_num(j-1) := l_getstops_stop_seq_num(j) - 1;
7770                          END IF;
7771 
7772                          l_update_flag := TRUE;
7773                       END IF;
7774                       CLOSE c_flip_disallowed;
7775                  END IF;
7776               END IF;
7777            END LOOP;
7778 
7779            IF l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN THEN
7780              -- in SSN mode, resequencing a physical stop to be first
7781              -- could cause its linked dummy stop sequence to become 0
7782              -- or negative.
7783              -- Not an issue in PAD because SSNs will be renumbered.
7784              IF l_getstops_stop_seq_num(1) <= 0 THEN
7785                  RAISE invalid_ssn_adjustment;
7786              END IF;
7787            END IF;
7788 
7789            FOR j IN l_getstops_stop_id.first .. l_getstops_stop_id.last LOOP
7790               IF l_debug_on THEN
7791                 WSH_DEBUG_SV.log(l_module_name,'j ', j );
7792                 WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_id(j)' , l_getstops_stop_id(j));
7793                 WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_seq_num(j)' , l_getstops_stop_seq_num(j));
7794                 WSH_DEBUG_SV.log(l_module_name,'l_getstops_pl_arr_date(j)' , to_char(l_getstops_pl_arr_date(j), 'DD-MON-YYYY HH24:MI:SS'));
7795                 WSH_DEBUG_SV.log(l_module_name,'l_getstops_phys_stop_id(j) ' , l_getstops_phys_stop_id(j) );
7796               END IF;
7797            END LOOP;
7798 
7799            IF l_debug_on THEN
7800              WSH_DEBUG_SV.logmsg(l_module_name,'loop backward checking for date or SSN conflict');
7801              WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_id.last', l_getstops_stop_id.last);
7802              WSH_DEBUG_SV.log(l_module_name,'l_getstops_stop_id.first' , l_getstops_stop_id.first);
7803            END IF;
7804 
7805            l_push_date_allowed := FALSE;
7806            l_push_ssn_allowed  := FALSE;
7807            j := l_getstops_stop_id.last;
7808            WHILE j is not null LOOP
7809 
7810              -- check backward for planned arrival date or SSN conflict
7811              -- bug 4248428: fixed issue with wrong error message by
7812              -- revising the conditions to check for or resolve conflict
7813              -- only when the current stop is dummy or dummy stop has pushed
7814              -- the preceding stop back.
7815              IF j > 1 and
7816                 (l_getstops_phys_stop_id(j) is not NULL
7817                  OR l_push_date_allowed OR l_push_ssn_allowed) THEN
7818 
7819                 IF l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD THEN
7820 
7821                   IF l_getstops_pl_arr_date(j-1) >= l_getstops_pl_arr_date(j) THEN
7822                      IF l_debug_on THEN
7823                        WSH_DEBUG_SV.log(l_module_name,'arr date of prev stop conflict at stop' , l_getstops_stop_id(j));
7824                      END IF;
7825                      IF l_push_date_allowed or
7826                         (l_getstops_pl_arr_date(j) <> l_getstops_org_pl_arr_date(j) and
7827                         l_getstops_phys_stop_id(j) is not NULL ) THEN
7828                         -- the linked physical stop has been modified, it is ok to push
7829                         -- other stops to avoid the time conflict
7830                         l_getstops_pl_arr_date(j-1) := l_getstops_pl_arr_date(j) - WSH_TRIPS_ACTIONS.C_TEN_SECONDS;
7831                         l_getstops_pl_dep_date(j-1) := l_getstops_pl_arr_date(j-1);
7832                         l_update_flag := TRUE;
7833                         l_push_date_allowed := TRUE;
7834                      ELSE
7835                        raise invalid_stop_date;
7836                      END IF;
7837                   ELSIF l_getstops_pl_dep_date(j-1) >= l_getstops_pl_arr_date(j) THEN
7838                      IF l_debug_on THEN
7839                        WSH_DEBUG_SV.log(l_module_name,'dep date of prev stop conflict at stop' , l_getstops_stop_id(j-1));
7840                      END IF;
7841                      l_getstops_pl_dep_date(j-1) := l_getstops_pl_arr_date(j-1);
7842                      l_update_flag := TRUE;
7843                   END IF;
7844 
7845                 ELSE -- mode is SSN
7846 
7847                   IF l_getstops_stop_seq_num(j-1) >= l_getstops_stop_seq_num(j) THEN
7848                      IF l_debug_on THEN
7849                        WSH_DEBUG_SV.log(l_module_name,'SSN of prev stop conflict at stop' , l_getstops_stop_id(j));
7850                      END IF;
7851                      IF l_push_ssn_allowed or
7852                         (l_getstops_stop_seq_num(j) <> l_getstops_org_stop_seq_num(j) and
7853                         l_getstops_phys_stop_id(j) is not NULL ) THEN
7854                         -- the linked physical stop has been modified, it is ok to push
7855                         -- other stops to avoid the SSN conflict.
7856                         l_getstops_stop_seq_num(j-1) := l_getstops_stop_seq_num(j) - 1;
7857                         IF l_getstops_stop_seq_num(j-1) <= 0 THEN
7858                           -- adjusting SSNs such that they become 0 or negative
7859                           -- is a corner-case that will return error.
7860                           RAISE invalid_ssn_adjustment;
7861                         END IF;
7862                         l_update_flag := TRUE;
7863                         l_push_ssn_allowed := TRUE;
7864                      ELSE
7865                          -- here the user has attempted to sandwich a stop
7866                          -- between the linked dummy stop and its
7867                          -- physical stop.
7868                          raise invalid_stop_seq_num;
7869                      END IF;
7870                   END IF;
7871 
7872                 END IF;
7873              END IF;
7874 
7875              IF j > 1
7876                 AND  l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_SSN THEN
7877                -- bug 4245339: check for non-unique SSN
7878                IF l_getstops_stop_seq_num(j-1)
7879                    = l_getstops_stop_seq_num(j) THEN
7880                  raise duplicate_stop_seq;
7881                END IF;
7882              END IF;
7883 
7884              j := l_getstops_stop_id.prior(j);
7885 
7886            END LOOP;
7887         END IF;
7888 
7889         IF l_update_flag or p_caller = 'WSH_CREATE_TRIP_STOP' THEN
7890            IF l_debug_on THEN
7891           WSH_DEBUG_SV.log(l_module_name,'updating stops ' , l_getstops_stop_id.count);
7892            END IF;
7893            FORALL j IN l_getstops_stop_id.first..l_getstops_stop_id.last
7894               UPDATE WSH_TRIP_STOPS
7895               SET
7896                 PHYSICAL_LOCATION_ID    = l_getstops_phys_loc_id(j),
7897                 PHYSICAL_STOP_ID        = l_getstops_phys_stop_id(j),
7898                 PLANNED_ARRIVAL_DATE    = l_getstops_pl_arr_date(j),
7899                 PLANNED_DEPARTURE_DATE  = l_getstops_pl_dep_date(j),
7900                 STOP_SEQUENCE_NUMBER    = l_getstops_stop_seq_num(j),
7901                 last_update_date        = SYSDATE,
7902                 last_updated_by         = FND_GLOBAL.USER_ID,
7903                 last_update_login       = FND_GLOBAL.LOGIN_ID
7904               WHERE  STOP_ID = l_getstops_stop_id(j);
7905            IF l_debug_on THEN
7906           WSH_DEBUG_SV.log(l_module_name,'done update' , l_getstops_stop_id.count);
7907            END IF;
7908             --
7909         -- DBI Project
7910         -- Updating  WSH_TRIP_STOPS.
7911         -- Call DBI API after the Update.
7912         -- This API will also check for DBI Installed or not
7913         IF l_debug_on THEN
7914           WSH_DEBUG_SV.log(l_module_name,'Calling DBI API.Stop count -',l_getstops_stop_id.count);
7915         END IF;
7916 	    WSH_INTEGRATION.DBI_Update_Trip_Stop_Log
7917           (p_stop_id_tab	=> l_getstops_stop_id,
7918            p_dml_type		=> 'UPDATE',
7919            x_return_status      => l_dbi_rs);
7920 
7921         IF l_debug_on THEN
7922           WSH_DEBUG_SV.log(l_module_name,'Return Status after DBI Call-',l_dbi_rs);
7923         END IF;
7924         IF l_dbi_rs = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR THEN
7925 	       x_return_status := l_dbi_rs;
7926 	       rollback to start_of_the_trip;
7927            -- just pass this return status to caller API
7928            IF l_debug_on THEN
7929             WSH_DEBUG_SV.log(l_module_name,'DBI API Returned Unexpected error '||x_return_status);
7930             WSH_DEBUG_SV.pop(l_module_name);
7931            END IF;
7932            return;
7933         END IF;
7934 
7935         -- End of Code for DBI Project
7936 
7937         END IF;
7938         IF l_stop_seq_mode = WSH_INTERFACE_GRP.G_STOP_SEQ_MODE_PAD THEN
7939           -- need to resequence the stops' SSNs and validate their dates.
7940 
7941           l_stop_details_rec.trip_id :=  p_trip_ids(i);
7942           --l_stop_details_rec.stop_id :=  NULL;
7943           --l_stop_details_rec.stop_sequence_number := NULL;
7944 
7945           WSH_TRIP_STOPS_ACTIONS.RESET_STOP_SEQ_NUMBERS(
7946                 p_stop_details_rec => l_stop_details_rec,
7947                 x_return_status => l_return_status);
7948 
7949           IF l_debug_on THEN
7950              wsh_debug_sv.log(l_module_name,'Return Status From WWSH_TRIP_STOPS_ACTIONS.RESET_STOP_SEQ_NUMBERS for trip '||p_trip_ids(i) ,l_return_status);
7951           END IF;
7952 
7953           IF l_return_status in ( WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
7954              raise reset_stop_seq_number_error;
7955           ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
7956              l_warn_num := l_warn_num + 1;
7957           END IF;
7958 
7959           WSH_TRIP_VALIDATIONS.Validate_Stop_Dates (
7960              p_trip_id               => p_trip_ids(i),
7961              x_return_status         => l_return_status);
7962 
7963           IF l_debug_on THEN
7964             wsh_debug_sv.log(l_module_name,'Return Status From WSH_TRIP_STOPS_VALIDATIONS.Validate_Stop_Dates for trip '||p_trip_ids(i) ,l_return_status);
7965           END IF;
7966 
7967           IF l_return_status in ( WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
7968              raise validate_stop_date_error;
7969           ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING THEN
7970              l_warn_num := l_warn_num + 1;
7971           END IF;
7972 
7973         END IF;  -- PAD resequencing
7974 
7975         x_success_trip_ids(x_success_trip_ids.count+1) := p_trip_ids(i);
7976 
7977      EXCEPTION
7978        WHEN get_physical_loc_err THEN
7979           IF c_get_stops_PAD%ISOPEN THEN
7980              CLOSE c_get_stops_PAD;
7981           END IF;
7982           IF c_get_stops_SSN%ISOPEN THEN
7983              CLOSE c_get_stops_SSN;
7984           END IF;
7985 
7986           rollback to start_of_the_trip;
7987 
7988 -- Handle Date Exception
7989        WHEN invalid_stop_date THEN
7990           rollback to start_of_the_trip;
7991          FND_MESSAGE.SET_NAME('WSH', 'WSH_BETWEEN_LINKED_STOPS');
7992          FND_MESSAGE.SET_TOKEN('DUMMY_STOP_DATE', fnd_date.date_to_displaydt(l_getstops_pl_arr_date(j)));
7993          FND_MESSAGE.SET_TOKEN('DUMMY_LOCATION_DESP',
7994             WSH_UTIL_CORE.Get_Location_Description (l_getstops_stop_loc_id(j),'NEW UI CODE INFO'));
7995          FND_MESSAGE.SET_TOKEN('PHYSICAL_STOP_DATE', fnd_date.date_to_displaydt(l_getstops_pl_arr_date(j+1)));
7996          FND_MESSAGE.SET_TOKEN('PHYSICAL_LOCATION_DESP',
7997          WSH_UTIL_CORE.Get_Location_Description (l_getstops_stop_loc_id(j+1),'NEW UI CODE INFO'));
7998          wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_ERROR);
7999 
8000 -- Handle SSN Exception
8001        WHEN invalid_stop_seq_num THEN
8002           rollback to start_of_the_trip;
8003          FND_MESSAGE.SET_NAME('WSH', 'WSH_BETWEEN_LINKED_STOPS_SSN');
8004          FND_MESSAGE.SET_TOKEN('DUMMY_STOP_SEQ_NUM', l_getstops_stop_seq_num(j));
8005          FND_MESSAGE.SET_TOKEN('DUMMY_LOCATION_DESP',
8006             WSH_UTIL_CORE.Get_Location_Description (l_getstops_stop_loc_id(j),'NEW UI CODE INFO'));
8007          FND_MESSAGE.SET_TOKEN('PHYSICAL_STOP_SEQ_NUM', l_getstops_stop_seq_num(j+1));
8008          FND_MESSAGE.SET_TOKEN('PHYSICAL_LOCATION_DESP',
8009          WSH_UTIL_CORE.Get_Location_Description (l_getstops_stop_loc_id(j+1),'NEW UI CODE INFO'));
8010          wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_ERROR);
8011 
8012        WHEN invalid_ssn_adjustment THEN
8013           rollback to start_of_the_trip;
8014          FND_MESSAGE.SET_NAME('WSH', 'WSH_INVALID_SSN_ADJUSTMENT');
8015          wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_ERROR);
8016 
8017 -- Handle non unique SSN exception (bug 4245339)
8018        WHEN duplicate_stop_seq THEN
8019           rollback to start_of_the_trip;
8020           FND_MESSAGE.SET_NAME('WSH','WSH_STOP_DUP_SEQUENCE');
8021           wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_ERROR);
8022 
8023        WHEN reset_stop_seq_number_error THEN
8024           rollback to start_of_the_trip;
8025 
8026        WHEN validate_stop_date_error THEN
8027 
8028           rollback to start_of_the_trip;
8029 
8030        WHEN Others THEN
8031           IF c_get_stops_PAD%ISOPEN THEN
8032              CLOSE c_get_stops_PAD;
8033           END IF;
8034           IF c_get_stops_SSN%ISOPEN THEN
8035              CLOSE c_get_stops_SSN;
8036           END IF;
8037 
8038           IF c_flip_disallowed%ISOPEN THEN
8039              CLOSE c_flip_disallowed;
8040           END IF;
8041 
8042           rollback to start_of_the_trip;
8043      END;
8044 
8045   i := p_trip_ids.next(i);
8046   END LOOP;
8047 
8048   IF x_success_trip_ids.count = 0 THEN
8049      x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
8050   ELSIF x_success_trip_ids.count <  p_trip_ids.count
8051         OR  l_warn_num > 0 THEN
8052      x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
8053   END IF;
8054 
8055   IF l_debug_on THEN
8056     WSH_DEBUG_SV.log(l_module_name,'x_success_trip_ids.count', x_success_trip_ids.count);
8057     WSH_DEBUG_SV.logmsg(l_module_name,'x_return_status '||x_return_status);
8058     WSH_DEBUG_SV.pop(l_module_name);
8059   END IF;
8060 
8061 EXCEPTION
8062 
8063   WHEN others THEN
8064     IF c_get_stops_PAD%ISOPEN THEN
8065        close c_get_stops_PAD;
8066     END IF;
8067     IF c_get_stops_SSN%ISOPEN THEN
8068        close c_get_stops_SSN;
8069     END IF;
8070 
8071     wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.LINK_TO_REGULAR_STOPS');
8072     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
8073 
8074     IF l_debug_on THEN
8075       WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
8076       WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
8077     END IF;
8078 
8079 END Handle_Internal_Stops;
8080 
8081 
8082 
8083 PROCEDURE PROCESS_CARRIER_SELECTION (
8084         p_init_msg_list            IN            VARCHAR2 DEFAULT fnd_api.g_false,
8085         p_trip_id_tab              IN            wsh_util_core.id_tab_type,
8086         p_caller                   IN  VARCHAR2 DEFAULT NULL, -- WSH_FSTRX / WSH_PUB /  WSH_GROUP/ FTE
8087         x_msg_count                OUT NOCOPY    NUMBER,
8088         x_msg_data                 OUT NOCOPY    VARCHAR2,
8089         x_return_status            OUT NOCOPY  VARCHAR2)
8090 
8091 IS
8092 
8093 -- Cursor to get trip level information
8094    CURSOR c_get_trip_details(c_tripid IN NUMBER) is
8095    SELECT trip_id
8096       , name
8097       , planned_flag
8098       , status_code
8099       , carrier_id
8100       , mode_of_transport
8101       , service_level
8102       , ship_method_code
8103 --      , track_only_flag,
8104       , consignee_carrier_ac_no
8105       , freight_terms_code
8106       , Load_tender_status
8107       , lane_id
8108       , rank_id
8109     FROM WSH_TRIPS
8110     WHERE trip_id = c_tripid;
8111 
8112 -- Cursor to get stop level information
8113     CURSOR c_get_trip_stops(c_tripid IN NUMBER) is
8114     SELECT wts.STOP_ID
8115       , wts.TRIP_ID
8116       --To handle dummy locations #DUM_LOC(S)
8117       , wts.STOP_LOCATION_ID
8118       , wts.STATUS_CODE
8119       , wts.STOP_SEQUENCE_NUMBER
8120       , wts.PLANNED_ARRIVAL_DATE
8121       , wts.PLANNED_DEPARTURE_DATE
8122       , wts.ACTUAL_ARRIVAL_DATE
8123       , wts.ACTUAL_DEPARTURE_DATE
8124       --#DUM_LOC(S)
8125       , wts.PHYSICAL_LOCATION_ID
8126       --#DUM_LOC(E)
8127       , wts.PHYSICAL_STOP_ID
8128       , wts.pick_up_weight
8129       , wts.weight_uom_code
8130       , wts.pick_up_volume
8131       , wts.volume_uom_code
8132     FROM wsh_trip_stops wts
8133     WHERE wts.trip_id = c_tripid
8134     order by wts.STOP_SEQUENCE_NUMBER;
8135 
8136     CURSOR c_get_firststop_dlvy(c_stop_id IN NUMBER) IS
8137     SELECT wnd.DELIVERY_ID
8138       , wnd.CUSTOMER_ID
8139       , wnd.ORGANIZATION_ID
8140     FROM wsh_new_deliveries wnd,
8141          wsh_delivery_legs wdl,
8142          wsh_trip_stops wts1
8143     WHERE wnd.delivery_id = wdl.delivery_id
8144     AND   wdl.pick_up_stop_id = wts1.stop_id
8145     AND   wnd.initial_pickup_location_id = wts1.stop_location_id
8146     AND   nvl(wnd.shipping_control,'BUYER') <> 'SUPPLIER'
8147     AND   wts1.stop_id = c_stop_id;
8148 
8149     CURSOR c_get_laststop_dlvy(c_stop_id IN NUMBER) IS
8150     SELECT wnd.DELIVERY_ID
8151       , wnd.CUSTOMER_ID
8152       , wnd.ORGANIZATION_ID
8153     FROM wsh_new_deliveries wnd,
8154          wsh_delivery_legs wdl,
8155          wsh_trip_stops wts1
8156     WHERE wnd.delivery_id = wdl.delivery_id
8157     AND   wdl.drop_off_stop_id = wts1.stop_id
8158     AND   wnd.ultimate_dropoff_location_id = wts1.stop_location_id
8159     AND   nvl(wnd.shipping_control,'BUYER') <> 'SUPPLIER'
8160     AND   wts1.stop_id = c_stop_id;
8161 
8162 /*
8163     CURSOR c_get_trip_cmove(c_trip_id IN NUMBER) IS
8164     SELECT MOVE_ID
8165     FROM   FTE_TRIP_MOVES
8166     WHERE  TRIP_ID = c_trip_id;
8167 */
8168 
8169 CURSOR c_ship_to_site_use(c_location_id IN NUMBER) IS
8170 SELECT SITE.SITE_USE_ID
8171 FROM HZ_CUST_ACCT_SITES_ALL     ACCT_SITE,
8172  HZ_PARTY_SITES             PARTY_SITE,
8173  HZ_LOCATIONS               LOC,
8174  HZ_CUST_SITE_USES_ALL      SITE
8175 WHERE
8176  SITE.SITE_USE_CODE = 'SHIP_TO'
8177  AND SITE.CUST_ACCT_SITE_ID = ACCT_SITE.CUST_ACCT_SITE_ID
8178  AND ACCT_SITE.PARTY_SITE_ID    = PARTY_SITE.PARTY_SITE_ID
8179  AND PARTY_SITE.LOCATION_ID     = LOC.LOCATION_ID
8180  AND LOC.LOCATION_ID = c_location_id;
8181 
8182 --l_global_parameters       WSH_SHIPPING_PARAMS_PVT.Global_Parameters_Rec_Typ;
8183 
8184 l_cs_trip_rec            cs_trip_rec_type;
8185 l_cs_tripstops_tab       cs_stop_tab_type;
8186 --l_stop_dlvy_tab          stop_delivery_tab_type;
8187 
8188 i                NUMBER := 0;
8189 j                NUMBER := 0;
8190 rec_cnt                NUMBER := 0;
8191 inp_itr                NUMBER := 0;
8192 list_cnt               NUMBER := 0;
8193 l_trip_id              NUMBER := 0;
8194 l_rank_id              NUMBER := 0;
8195 l_move_id              NUMBER := 0;
8196 l_prev_trip_id         NUMBER := 0;
8197 l_trip_result_type     VARCHAR2(30);
8198 l_rank_result_cnt      NUMBER := 0;
8199 
8200 l_organization_tab      WSH_UTIL_CORE.id_tab_type;
8201 l_customer_tab          WSH_UTIL_CORE.id_tab_type;
8202 l_organization_id       NUMBER := 0;
8203 l_customer_id           NUMBER := 0;
8204 l_delivery_id           NUMBER := 0;
8205 l_customer_site_id      NUMBER := NULL;
8206 l_rg_trip               BOOLEAN := TRUE;
8207 --l_rg_trip_cnt           NUMBER := 0;
8208 l_stop_organization_id_tab       WSH_UTIL_CORE.id_tab_type;
8209 l_stop_customer_id_tab           WSH_UTIL_CORE.id_tab_type;
8210 l_stop_delivery_id_tab           WSH_UTIL_CORE.id_tab_type;
8211 l_base_weight_uom       VARCHAR2(3);
8212 l_base_volume_uom       VARCHAR2(3);
8213 l_pickup_weight_convert NUMBER := 0;
8214 l_pickup_volume_convert NUMBER := 0;
8215 l_total_pickup_weight   NUMBER := 0;
8216 l_total_pickup_volume   NUMBER := 0;
8217 
8218 l_commit                VARCHAR2(100) := FND_API.G_FALSE;
8219 l_init_msg_list         VARCHAR2(100) := FND_API.G_FALSE;
8220 
8221 l_trip_in_rec           WSH_TRIPS_GRP.tripInRecType;
8222 l_trip_info_tab         WSH_TRIPS_PVT.Trip_Attr_Tbl_Type;
8223 l_trip_out_rec_tab      WSH_TRIPS_GRP.Trip_Out_Tab_Type;
8224 
8225 l_msg_count                   NUMBER;
8226 l_msg_data                    VARCHAR2(2000);
8227 l_return_message              VARCHAR2(2000);
8228 l_return_status               VARCHAR2(1);
8229 
8230 l_triporigin_intorg_id        NUMBER := 0;
8231 l_initial_pickup_loc_id       NUMBER := 0;
8232 l_initial_pickup_date         DATE;
8233 l_tripdest_org_id             NUMBER := 0;
8234 l_ultimate_dropoff_loc_id     NUMBER := 0;
8235 l_ultimate_dropoff_date       DATE;
8236 
8237 l_trip_rank_array             numtabvc2;
8238 
8239 
8240 l_ranked_list          WSH_FTE_INTEGRATION.CARRIER_RANK_LIST_TBL_TYPE;
8241 l_cs_input_tab         WSH_FTE_INTEGRATION.wsh_cs_entity_tab_type;
8242 l_cs_result_tab        WSH_FTE_INTEGRATION.wsh_cs_result_tab_type;
8243 l_cs_output_message_tab  WSH_FTE_INTEGRATION.wsh_cs_output_message_tab;
8244 
8245 -- Debug Variables
8246 --
8247 l_debug_on BOOLEAN;
8248 --
8249 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'PROCESS_CARRIER_SELECTION';
8250 BEGIN
8251 
8252       IF FND_API.to_Boolean( p_init_msg_list ) THEN
8253          FND_MSG_PUB.initialize;
8254       END IF;
8255 
8256       l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
8257        --
8258       IF l_debug_on IS NULL
8259       THEN
8260        l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
8261       END IF;
8262       --
8263 
8264       IF l_debug_on THEN
8265        wsh_debug_sv.push(l_module_name);
8266        WSH_DEBUG_SV.logmsg(l_module_name,'p_init_msg_list : '||p_init_msg_list);
8267        WSH_DEBUG_SV.logmsg(l_module_name,'P_CALLER : '||P_CALLER);
8268       END IF;
8269       --
8270       x_return_status  := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
8271 
8272       SAVEPOINT before_trip_update;
8273 
8274       i := p_trip_id_tab.FIRST;
8275       IF i IS NOT NULL THEN
8276       LOOP
8277 
8278          l_rg_trip := TRUE;
8279          l_cs_tripstops_tab.DELETE;
8280          l_customer_id := NULL;
8281          l_triporigin_intorg_id := NULL;
8282          l_initial_pickup_loc_id := NULL;
8283          l_initial_pickup_date := NULL;
8284          l_tripdest_org_id := NULL;
8285          l_customer_site_id := NULL;
8286          l_ultimate_dropoff_loc_id := NULL;
8287          l_ultimate_dropoff_date := NULL;
8288          l_total_pickup_weight := 0;
8289          l_base_weight_uom := NULL;
8290          l_total_pickup_volume := 0;
8291          l_base_volume_uom := NULL;
8292 
8293          IF l_debug_on THEN
8294                   WSH_DEBUG_SV.logmsg(l_module_name,'trip_id '|| p_trip_id_tab(i));
8295          END IF;
8296 
8297          OPEN c_get_trip_details(p_trip_id_tab(i));
8298          FETCH c_get_trip_details INTO l_cs_trip_rec;
8299          CLOSE c_get_trip_details;
8300 
8301          -- Perform security checks from the TDD
8302 
8303 /*
8304         Not allowed when
8305 
8306         o	Trip status  IT, CL
8307         o	Lane assigned
8308         o	Tender status  Tendered /Accepted
8309         o	Planning status  Planned, Firmed
8310         o	Continous Move  Part of
8311 
8312         -- Above handled in is_action_enabled
8313         -- This API should always be called through group API s
8314 
8315 
8316         -- Warning when trip already has an attached ranked list
8317 */
8318 
8319          OPEN c_get_trip_stops(p_trip_id_tab(i));
8320          FETCH c_get_trip_stops BULK COLLECT INTO l_cs_tripstops_tab;
8321          CLOSE c_get_trip_stops;
8322 
8323          IF l_cs_tripstops_tab.COUNT = 0 THEN
8324             FND_MESSAGE.SET_NAME('WSH','WSH_FTE_SEL_NO_STOPS');
8325             --FND_MESSAGE.SET_TOKEN('TRIPID',p_trip_id_tab(i));
8326             --FND_MSG_PUB.ADD;
8327             x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
8328             l_rg_trip := FALSE;
8329             WSH_UTIL_CORE.add_message(x_return_status);
8330          END IF;
8331 
8332          j:=l_cs_tripstops_tab.FIRST;
8333          IF j  IS NOT NULL THEN
8334          LOOP
8335 
8336             l_organization_id := NULL;
8337             IF l_base_weight_uom IS NULL THEN
8338                l_base_weight_uom := l_cs_tripstops_tab(j).weight_uom_code;
8339             END IF;
8340 
8341             IF l_base_volume_uom IS NULL THEN
8342                l_base_volume_uom := l_cs_tripstops_tab(j).volume_uom_code;
8343             END IF;
8344          -- Sum up pickup weight / volume of all stops / deliveries in this trip
8345 
8346             IF nvl(l_cs_tripstops_tab(j).PICK_UP_WEIGHT,0) <> 0 THEN
8347 
8348              IF l_cs_tripstops_tab(j).weight_uom_code <> l_base_weight_uom THEN
8349                l_pickup_weight_convert := WSH_WV_UTILS.Convert_Uom(
8350                                            from_uom => l_cs_tripstops_tab(j).weight_uom_code,
8351                                            to_uom   => l_base_weight_uom,
8352                                            quantity => l_cs_tripstops_tab(j).PICK_UP_WEIGHT);
8353              ELSE
8354                l_pickup_weight_convert := l_cs_tripstops_tab(j).PICK_UP_WEIGHT;
8355              END IF;
8356              l_total_pickup_weight := l_total_pickup_weight + l_pickup_weight_convert;
8357             END IF;
8358 
8359             IF nvl(l_cs_tripstops_tab(j).PICK_UP_volume,0) <> 0 THEN
8360 
8361              IF l_cs_tripstops_tab(j).volume_uom_code <> l_base_volume_uom THEN
8362                l_pickup_volume_convert := WSH_WV_UTILS.Convert_Uom(
8363                                            from_uom => l_cs_tripstops_tab(j).volume_uom_code,
8364                                            to_uom   => l_base_volume_uom,
8365                                            quantity => l_cs_tripstops_tab(j).PICK_UP_volume);
8366              ELSE
8367                l_pickup_volume_convert := l_cs_tripstops_tab(j).PICK_UP_volume;
8368              END IF;
8369 
8370                l_total_pickup_volume := l_total_pickup_volume + l_pickup_volume_convert;
8371             END IF;
8372 
8373             IF l_debug_on THEN
8374                   WSH_DEBUG_SV.logmsg(l_module_name,'stop_id '|| l_cs_tripstops_tab(j).stop_id);
8375                   WSH_DEBUG_SV.logmsg(l_module_name,'l_pickup_weight_convert '|| l_pickup_weight_convert);
8376                   WSH_DEBUG_SV.logmsg(l_module_name,'l_pickup_volume_convert '|| l_pickup_volume_convert);
8377                   WSH_DEBUG_SV.logmsg(l_module_name,'volume_uom_code '|| l_cs_tripstops_tab(j).volume_uom_code);
8378                   WSH_DEBUG_SV.logmsg(l_module_name,'l_base_volume_uom '|| l_base_volume_uom);
8379                   WSH_DEBUG_SV.logmsg(l_module_name,'weight_uom_code '|| l_cs_tripstops_tab(j).weight_uom_code);
8380                   WSH_DEBUG_SV.logmsg(l_module_name,'l_base_weight_uom '|| l_base_weight_uom);
8381              END IF;
8382          -- triporigin_internalorg_id is that of first stop / any delivery being picked up at the first stop of the trip
8383          -- organization_id is that of last stop / any delivery being dropped off at the last stop of the trip
8384 
8385             IF j IN (l_cs_tripstops_tab.FIRST,l_cs_tripstops_tab.LAST) THEN
8386 
8387                WSH_UTIL_CORE.get_org_from_location(
8388                    p_location_id         => nvl(l_cs_tripstops_tab(j).physical_location_id,l_cs_tripstops_tab(j).stop_location_id),
8389                    x_organization_tab    => l_organization_tab,
8390                    x_return_status       => l_return_status);
8391 
8392                /* If l_organization_tab.COUNT = 0 no organization_id is passed*/
8393                IF l_organization_tab.COUNT > 1 THEN
8394                   -- Get the organization_id of any delivery having initial_pick_up_loc_id / ultimate_dropoff_loc_id
8395                   -- at this location
8396                   -- If there are multiple organizations, use the first one obtained
8397                   IF j = l_cs_tripstops_tab.FIRST THEN
8398                      OPEN c_get_firststop_dlvy(l_cs_tripstops_tab(j).stop_id);
8399                      --FETCH c_get_firststop_dlvy BULK COLLECT INTO l_stop_dlvy_tab;
8400                      FETCH c_get_firststop_dlvy BULK COLLECT INTO l_stop_delivery_id_tab,l_stop_customer_id_tab,l_stop_organization_id_tab;
8401                      CLOSE c_get_firststop_dlvy;
8402 
8403                      --IF l_stop_dlvy_tab.COUNT > 0 THEN
8404                      IF l_stop_delivery_id_tab.COUNT > 0 THEN
8405                         --l_organization_id := l_stop_dlvy_tab(1).organization_id;
8406                         l_organization_id := l_stop_organization_id_tab(1);
8407                      ELSE
8408                         l_organization_id := l_organization_tab(1);
8409                      END IF;
8410 
8411                   ELSIF j = l_cs_tripstops_tab.LAST THEN
8412                      OPEN c_get_laststop_dlvy(l_cs_tripstops_tab(j).stop_id);
8413                      --FETCH c_get_laststop_dlvy BULK COLLECT INTO l_stop_dlvy_tab;
8414                      FETCH c_get_laststop_dlvy BULK COLLECT INTO l_stop_delivery_id_tab,l_stop_customer_id_tab,l_stop_organization_id_tab;
8415                      CLOSE c_get_laststop_dlvy;
8416 
8417                      --IF l_stop_dlvy_tab.COUNT > 0 THEN
8418                      IF l_stop_delivery_id_tab.COUNT > 0 THEN
8419                         --l_organization_id := l_stop_dlvy_tab(1).organization_id;
8420                         l_organization_id := l_stop_organization_id_tab(1);
8421                      ELSE
8422                         l_organization_id := l_organization_tab(1);
8423                      END IF;
8424 
8425                   END IF;
8426 
8427                ELSIF l_organization_tab.COUNT = 1 THEN
8428                   l_organization_id := l_organization_tab(1);
8429                END IF;
8430 
8431          -- customer_id is ship to customer_id (owner) of the last stop  (Only if it is a customer location)
8432          -- pickup_date and dropoff_date are the planned_departure and planned_arrival_dates of first and last stop
8433 
8434                --l_stop_dlvy_tab.DELETE;
8435                l_stop_delivery_id_tab.DELETE;
8436                l_stop_customer_id_tab.DELETE;
8437                l_stop_organization_id_tab.DELETE;
8438 
8439                -- Populate triporigin_internalorg_id OR organization_id
8440                IF j = l_cs_tripstops_tab.FIRST THEN
8441                   l_triporigin_intorg_id := l_organization_id;
8442                   l_initial_pickup_loc_id := nvl(l_cs_tripstops_tab(j).physical_location_id,l_cs_tripstops_tab(j).stop_location_id);
8443                   l_initial_pickup_date := l_cs_tripstops_tab(j).planned_departure_date;
8444                ELSIF j = l_cs_tripstops_tab.LAST THEN
8445                   l_tripdest_org_id := l_organization_id;
8446                   l_ultimate_dropoff_loc_id := nvl(l_cs_tripstops_tab(j).physical_location_id,l_cs_tripstops_tab(j).stop_location_id);
8447                   l_ultimate_dropoff_date := l_cs_tripstops_tab(j).planned_arrival_date;
8448 
8449                   WSH_UTIL_CORE.get_customer_from_loc(
8450                       p_location_id    => nvl(l_cs_tripstops_tab(j).physical_location_id,l_cs_tripstops_tab(j).stop_location_id),
8451                       x_customer_id_tab   => l_customer_tab,
8452                       x_return_status  => l_return_status);
8453 
8454                   /* If l_customer_tab.COUNT = 0 no customer_id is passed*/
8455                   IF l_customer_tab.COUNT > 1 THEN
8456                     -- Get the customer_id of any delivery having ultimate_dropoff_loc_id
8457                     -- at this location
8458                     -- If there are multiple customers, use the first one obtained
8459 
8460                      OPEN c_get_laststop_dlvy(l_cs_tripstops_tab(j).stop_id);
8461                      --FETCH c_get_laststop_dlvy BULK COLLECT INTO l_stop_dlvy_tab;
8462                      FETCH c_get_laststop_dlvy BULK COLLECT INTO l_stop_delivery_id_tab,l_stop_customer_id_tab,l_stop_organization_id_tab;
8463                      CLOSE c_get_laststop_dlvy;
8464 
8465                      --IF l_stop_dlvy_tab.COUNT > 0 THEN
8466                      IF l_stop_delivery_id_tab.COUNT > 0 THEN
8467                         --l_customer_id := l_stop_dlvy_tab(1).customer_id;
8468                         l_customer_id := l_stop_customer_id_tab(1);
8469                      ELSE
8470                         l_customer_id := l_customer_tab(1);
8471                      END IF;
8472 
8473                   ELSIF l_customer_tab.COUNT = 1 THEN
8474                      l_customer_id := l_customer_tab(1);
8475                   END IF;
8476 
8477                   --l_cs_input_tab(i).customer_id := l_customer_id;
8478 
8479                END IF;
8480 
8481             END IF;
8482 
8483             EXIT WHEN j = l_cs_tripstops_tab.LAST;
8484             j := l_cs_tripstops_tab.NEXT(j);
8485 
8486          END LOOP;
8487          --END IF;
8488 
8489          --
8490          --Determine the ship to site id.
8491          --
8492 
8493          OPEN c_ship_to_site_use(l_ultimate_dropoff_loc_id);
8494          FETCH c_ship_to_site_use INTO l_customer_site_id;
8495          IF c_ship_to_site_use%NOTFOUND THEN
8496           IF l_customer_id IS NOT NULL THEN
8497             FND_MESSAGE.SET_NAME('WSH','WSH_LOCATION_NO_SITE');
8498             --FND_MESSAGE.Set_Token('LOCATION',l_ultimate_dropoff_loc_id);
8499             FND_MESSAGE.Set_Token('TRIPNAME',l_cs_trip_rec.trip_name);
8500             x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
8501             l_rg_trip := FALSE;
8502           ELSE
8503             l_customer_site_id := NULL;
8504           END IF;
8505          END IF;
8506          CLOSE c_ship_to_site_use;
8507 
8508          IF l_rg_trip THEN
8509 
8510          --l_rg_trip_cnt := l_rg_trip_cnt + 1;
8511          IF l_cs_trip_rec.rank_id IS NOT NULL THEN
8512             l_trip_rank_array(TO_CHAR(l_cs_trip_rec.trip_id, g_int_mask)) := l_cs_trip_rec.rank_id;
8513          ELSE
8514             l_trip_rank_array(TO_CHAR(l_cs_trip_rec.trip_id, g_int_mask)) := NULL;
8515             --l_trip_rank_array(l_cs_trip_rec.trip_id) := NULL;
8516          END IF;
8517 
8518          -- freight_terms will not be used as an input for trip level call
8519 
8520          -- Populate into l_cs_input_tab
8521          -- only those trips which have non zero stops
8522 
8523          inp_itr := l_cs_input_tab.COUNT + 1;
8524 
8525          l_cs_input_tab(inp_itr).trip_id      := l_cs_trip_rec.trip_id; -- If input is trip
8526          l_cs_input_tab(inp_itr).trip_name    := l_cs_trip_rec.trip_name;
8527          --l_cs_input_tab(inp_itr).freight_terms_code   := l_cs_trip_rec.freight_terms_code;
8528          -- Stop level Info
8529          l_cs_input_tab(inp_itr).customer_id := l_customer_id;
8530          l_cs_input_tab(inp_itr).triporigin_internalorg_id := l_triporigin_intorg_id;
8531          l_cs_input_tab(inp_itr).initial_pickup_loc_id := l_initial_pickup_loc_id;
8532          l_cs_input_tab(inp_itr).initial_pickup_date := l_initial_pickup_date;
8533          l_cs_input_tab(inp_itr).organization_id := l_tripdest_org_id;
8534          l_cs_input_tab(inp_itr).customer_site_id := l_customer_site_id;
8535          l_cs_input_tab(inp_itr).ultimate_dropoff_loc_id := l_ultimate_dropoff_loc_id;
8536          l_cs_input_tab(inp_itr).ultimate_dropoff_date := l_ultimate_dropoff_date;
8537          l_cs_input_tab(inp_itr).gross_weight := l_total_pickup_weight;
8538          l_cs_input_tab(inp_itr).weight_uom_code  := l_base_weight_uom;
8539          l_cs_input_tab(inp_itr).volume       := l_total_pickup_volume;
8540          l_cs_input_tab(inp_itr).volume_uom_code  := l_base_volume_uom;
8541 
8542          IF l_debug_on THEN
8543             WSH_DEBUG_SV.logmsg(l_module_name,'trip_id '|| l_cs_input_tab(inp_itr).trip_id);
8544             WSH_DEBUG_SV.logmsg(l_module_name,'name '|| l_cs_input_tab(inp_itr).trip_name);
8545             --WSH_DEBUG_SV.logmsg(l_module_name,'freight_terms_code '|| l_cs_input_tab(inp_itr).freight_terms_code);
8546             WSH_DEBUG_SV.logmsg(l_module_name,'triporigin_internalorg_id '|| l_cs_input_tab(inp_itr).triporigin_internalorg_id);
8547             WSH_DEBUG_SV.logmsg(l_module_name,'initial_pickup_loc_id '|| l_cs_input_tab(inp_itr).initial_pickup_loc_id);
8548             WSH_DEBUG_SV.logmsg(l_module_name,'initial_pickup_date '|| l_cs_input_tab(inp_itr).initial_pickup_date);
8549             WSH_DEBUG_SV.logmsg(l_module_name,'customer_site_id '|| l_cs_input_tab(inp_itr).customer_site_id);
8550             WSH_DEBUG_SV.logmsg(l_module_name,'ultimate_dropoff_loc_id '|| l_cs_input_tab(inp_itr).ultimate_dropoff_loc_id);
8551             WSH_DEBUG_SV.logmsg(l_module_name,'customer_id '|| l_cs_input_tab(inp_itr).customer_id);
8552             WSH_DEBUG_SV.logmsg(l_module_name,'organization_id '|| l_cs_input_tab(inp_itr).organization_id);
8553             WSH_DEBUG_SV.logmsg(l_module_name,'ultimate_dropoff_date '|| l_cs_input_tab(inp_itr).ultimate_dropoff_date);
8554             WSH_DEBUG_SV.logmsg(l_module_name,'gross_weight '|| l_cs_input_tab(inp_itr).gross_weight);
8555             WSH_DEBUG_SV.logmsg(l_module_name,'weight_uom_code '|| l_cs_input_tab(inp_itr).weight_uom_code);
8556             WSH_DEBUG_SV.logmsg(l_module_name,'volume '|| l_cs_input_tab(inp_itr).volume);
8557             WSH_DEBUG_SV.logmsg(l_module_name,'volume_uom_code '|| l_cs_input_tab(inp_itr).volume_uom_code);
8558          END IF;
8559 
8560          END IF; -- l_rg_trip
8561          END IF;
8562 
8563 
8564          EXIT WHEN i = p_trip_id_tab.LAST;
8565          i := p_trip_id_tab.NEXT(i);
8566 
8567       END LOOP;
8568       END IF;
8569 
8570       -- Call Carrier Selection Engine
8571       -- TODO only if there are atleast one eligible trips
8572 
8573       IF l_cs_input_tab.COUNT > 0 THEN
8574 
8575       WSH_FTE_INTEGRATION.CARRIER_SELECTION(
8576                                           p_format_cs_tab           => l_cs_input_tab,
8577                                           p_messaging_yn            => 'Y',
8578                                           p_caller                  => p_caller,
8579                                           p_entity                  => 'TRIP',
8580                                           x_cs_output_tab           => l_cs_result_tab,
8581                                           x_cs_output_message_tab   => l_cs_output_message_tab,
8582                                           x_return_message          => l_return_message,
8583                                           x_return_status           => l_return_status);
8584 
8585       -- Handle results (Error out if multileg)
8586 
8587       IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_SUCCESS,WSH_UTIL_CORE.G_RET_STS_WARNING)) THEN
8588 
8589        IF l_debug_on THEN
8590           WSH_DEBUG_SV.logmsg(l_module_name,'l_cs_result_tab.COUNT is '|| l_cs_result_tab.COUNT);
8591        END IF;
8592        IF (l_cs_result_tab.COUNT > 0)  THEN
8593 
8594          rec_cnt := l_cs_result_tab.FIRST;
8595 
8596          -- Loop through result tab in order to update trip for each trip
8597 
8598          -- l_cs_result_tab is ordered by trip_id, rank/leg_sequence
8599          -- There is one record in l_cs_result_tab per rank / leg output
8600 
8601          l_prev_trip_id := -1;
8602          list_cnt := 0;
8603          l_rank_result_cnt := 0;
8604          IF rec_cnt IS NOT NULL THEN
8605             LOOP
8606             --{
8607                l_trip_id := l_cs_result_tab(rec_cnt).trip_id;
8608                l_trip_result_type := l_cs_result_tab(rec_cnt).result_type;
8609                IF l_debug_on THEN
8610                   WSH_DEBUG_SV.logmsg(l_module_name,'l_trip_id is '|| l_trip_id);
8611                   WSH_DEBUG_SV.logmsg(l_module_name,'l_trip_result_type is '|| l_trip_result_type);
8612                   WSH_DEBUG_SV.logmsg(l_module_name,'rule_id is '|| l_cs_result_tab(rec_cnt).rule_id);
8613                END IF;
8614 
8615                IF l_trip_result_type = 'MULTILEG' THEN
8616                   IF l_trip_id <> l_prev_trip_id THEN
8617                      FND_MESSAGE.SET_NAME('WSH','WSH_FTE_CS_MULTILEG_TRIP');
8618                      FND_MESSAGE.SET_TOKEN('TRIPID',l_trip_id);
8619                      --FND_MSG_PUB.ADD;
8620                      x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
8621                      WSH_UTIL_CORE.add_message(x_return_status);
8622                   END IF;
8623                ELSIF l_trip_result_type = 'RANK' THEN
8624                   l_rank_result_cnt := l_rank_result_cnt + 1;
8625                   IF l_trip_id <> l_prev_trip_id THEN
8626 
8627                         -- Warning when trip already has an attached ranked list
8628 
8629                         --IF l_trip_rank_array(l_trip_id) IS NOT NULL THEN
8630                         IF l_trip_rank_array(TO_CHAR(l_trip_id, g_int_mask)) IS NOT NULL THEN
8631                            FND_MESSAGE.SET_NAME('WSH','WSH_FTE_CS_UPD_TRIP_RANK_LIST');
8632                            FND_MESSAGE.SET_TOKEN('TRIPID',l_trip_id);
8633                            --FND_MSG_PUB.ADD;
8634                            IF l_debug_on THEN
8635                               WSH_DEBUG_SV.logmsg(l_module_name,'Trip already has a ranked list ');
8636                            END IF;
8637                            x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
8638                            WSH_UTIL_CORE.add_message(x_return_status);
8639                         END IF;
8640 
8641                       --IF (l_cs_result_tab(rec_cnt).rank = 1) THEN
8642                         -- Has to update trip
8643                         -- Call WSH_TRIPS_GRP.create_update_trip to update trip if required
8644 
8645                         -- AG
8646                         l_trip_info_tab.DELETE;
8647                         --l_trip_in_rec.caller      := 'FTE_CARRIER_SELECTION_FORM';
8648                         -- AG bug found in local UT
8649                         l_trip_in_rec.caller      := 'FTE_ROUTING_GUIDE';
8650                         l_trip_in_rec.phase       := null;
8651                         l_trip_in_rec.action_code := 'UPDATE';
8652 
8653                         WSH_TRIPS_PVT.populate_record(
8654                            p_trip_id       => l_trip_id,
8655                            x_trip_info     => l_trip_info_tab(1),
8656                            x_return_status => l_return_status);
8657 
8658                         IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
8659                            WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
8660                            raise FND_API.G_EXC_UNEXPECTED_ERROR;
8661                         END IF;
8662 
8663                         --l_trip_info_tab(1).trip_id := l_trip_id;
8664                         -- rank_list_action takes care of updating rank_id for the trip
8665                         --l_trip_info_tab(1).rank_id := l_ranked_list(1).rank_id;
8666 
8667                         l_trip_info_tab(1).carrier_id := l_cs_result_tab(rec_cnt).carrier_id;
8668                         l_trip_info_tab(1).mode_of_transport := l_cs_result_tab(rec_cnt).mode_of_transport;
8669                         l_trip_info_tab(1).service_level := l_cs_result_tab(rec_cnt).service_level;
8670                         l_trip_info_tab(1).ship_method_code := l_cs_result_tab(rec_cnt).ship_method_code;
8671                         l_trip_info_tab(1).consignee_carrier_ac_no := l_cs_result_tab(rec_cnt).consignee_carrier_ac_no;
8672                         l_trip_info_tab(1).freight_terms_code := l_cs_result_tab(rec_cnt).freight_terms_code;
8673                         -- AG
8674                         l_trip_info_tab(1).routing_rule_id := l_cs_result_tab(rec_cnt).rule_id;
8675                         l_trip_info_tab(1).append_flag := 'Y';
8676 
8677                         WSH_TRIPS_GRP.Create_Update_Trip(
8678                           p_api_version_number => 1.0,
8679                           p_init_msg_list      => l_init_msg_list,
8680                           p_commit             => l_commit,
8681                           x_return_status      => l_return_status,
8682                           x_msg_count          => l_msg_count,
8683                           x_msg_data           => l_msg_data,
8684                           p_trip_info_tab      => l_trip_info_tab,
8685                           p_in_rec             => l_trip_in_rec,
8686                           x_out_tab            => l_trip_out_rec_tab);
8687 
8688                         --IF l_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR THEN
8689                         IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
8690                            raise FND_API.G_EXC_UNEXPECTED_ERROR;
8691                         END IF;
8692 
8693                       --END IF;
8694 
8695                   END IF;
8696 
8697                   list_cnt := list_cnt + 1;
8698                   -- Build ranked list structure
8699                   l_ranked_list(list_cnt).TRIP_ID := l_trip_id;
8700                   l_ranked_list(list_cnt).SOURCE := 'RG';
8701                   l_ranked_list(list_cnt).RANK_SEQUENCE := l_cs_result_tab(rec_cnt).rank;
8702                   l_ranked_list(list_cnt).CARRIER_ID := l_cs_result_tab(rec_cnt).carrier_id;
8703                   l_ranked_list(list_cnt).SERVICE_LEVEL := l_cs_result_tab(rec_cnt).service_level;
8704                   l_ranked_list(list_cnt).MODE_OF_TRANSPORT := l_cs_result_tab(rec_cnt).mode_of_transport;
8705                   l_ranked_list(list_cnt).consignee_carrier_ac_no := l_cs_result_tab(rec_cnt).consignee_carrier_ac_no;
8706                   l_ranked_list(list_cnt).freight_terms_code := l_cs_result_tab(rec_cnt).freight_terms_code;
8707                   -- AG
8708                   l_ranked_list(list_cnt).call_rg_flag := 'N';
8709 
8710                   IF l_trip_id <> l_prev_trip_id THEN
8711                   --IF (l_cs_result_tab(rec_cnt).rank = 1) THEN
8712                      l_ranked_list(list_cnt).IS_CURRENT := 'Y';
8713                   ELSE
8714                      l_ranked_list(list_cnt).IS_CURRENT := 'N';
8715                   END IF;
8716 
8717                   IF l_debug_on THEN
8718                     WSH_DEBUG_SV.logmsg(l_module_name,'trip_id '|| l_trip_id);
8719                     WSH_DEBUG_SV.logmsg(l_module_name,'rank sequence '|| l_cs_result_tab(rec_cnt).rank);
8720                     WSH_DEBUG_SV.logmsg(l_module_name,'carrier_id '|| l_cs_result_tab(rec_cnt).carrier_id);
8721                     WSH_DEBUG_SV.logmsg(l_module_name,'service_level '|| l_cs_result_tab(rec_cnt).service_level);
8722                     WSH_DEBUG_SV.logmsg(l_module_name,'mode_of_transport '|| l_cs_result_tab(rec_cnt).mode_of_transport);
8723                     WSH_DEBUG_SV.logmsg(l_module_name,'consignee_carrier_ac_no '|| l_cs_result_tab(rec_cnt).consignee_carrier_ac_no);
8724                     WSH_DEBUG_SV.logmsg(l_module_name,'freight_terms_code '|| l_cs_result_tab(rec_cnt).freight_terms_code);
8725                   END IF;
8726                   -- For the last record in l_cs_result_tab attach ranked list here
8727 
8728                   -- Replace ranked list if the trip already had a ranked list attached
8729 
8730                   IF (rec_cnt = l_cs_result_tab.LAST) OR
8731                      (rec_cnt <> l_cs_result_tab.LAST AND l_cs_result_tab(l_cs_result_tab.NEXT(rec_cnt)).trip_id <> l_trip_id) THEN
8732                   --IF rec_cnt = l_cs_result_tab.LAST THEN
8733 
8734                      -- Call FTE_CARRIER_RANK_LIST_PVT.RANK_LIST_ACTION(
8735 
8736                         WSH_FTE_INTEGRATION.RANK_LIST_ACTION(
8737                         p_api_version        =>  1.0,
8738                         p_init_msg_list      =>  FND_API.G_FALSE,
8739                         x_return_status      =>  l_return_status,
8740                         x_msg_count          =>  l_msg_count,
8741                         x_msg_data           =>  l_msg_data,
8742                         p_action_code        =>  'REPLACE',
8743                         p_ranklist           =>  l_ranked_list,
8744                         p_trip_id             =>  l_trip_id,
8745                         p_rank_id            =>  l_rank_id
8746                         --x_ranklist           =>  l_ranked_list
8747                         );
8748                         IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
8749                            raise FND_API.G_EXC_UNEXPECTED_ERROR;
8750                         END IF;
8751                         l_ranked_list.DELETE;
8752                         list_cnt := 0;
8753                   END IF;
8754 
8755                END IF;
8756 
8757                l_prev_trip_id := l_trip_id;
8758            EXIT WHEN rec_cnt = l_cs_result_tab.LAST;
8759            rec_cnt := l_cs_result_tab.NEXT(rec_cnt);
8760             --}
8761           END LOOP;
8762         END IF;
8763         IF l_rank_result_cnt = 0 THEN
8764                   --
8765                   -- All results found are multileg, return an error
8766                   --
8767                   FND_MESSAGE.SET_NAME('WSH','WSH_FTE_CS_NO_RANKED_RESULT');
8768                   x_return_status  := WSH_UTIL_CORE.G_RET_STS_ERROR;
8769                   WSH_UTIL_CORE.add_message(x_return_status);
8770                   --
8771                   -- Debug Statements
8772                   --
8773                   IF l_debug_on THEN
8774                      WSH_DEBUG_SV.logmsg(l_module_name,  'NO RANKED RESULT FOUND FROM PROCESS CARRIER SELECTION '  );
8775                   END IF;
8776                   --
8777 
8778         END IF;
8779        ELSE -- l_cs_result_tab.COUNT = 0
8780                   --
8781                   -- No results at all where found, return a warning
8782                   --
8783                   FND_MESSAGE.SET_NAME('WSH','WSH_FTE_CS_NO_CARRIER_SELECTED');
8784                   x_return_status  := WSH_UTIL_CORE.G_RET_STS_WARNING;
8785                   WSH_UTIL_CORE.add_message(x_return_status);
8786                   --
8787                   -- Debug Statements
8788                   --
8789                   IF l_debug_on THEN
8790                      WSH_DEBUG_SV.logmsg(l_module_name,  'NO CARRIER FOUND FROM PROCESS CARRIER SELECTION '  );
8791                   END IF;
8792                   --
8793        END IF;
8794       ELSE
8795         raise FND_API.G_EXC_UNEXPECTED_ERROR;
8796       END IF;
8797       -- TODO
8798       ELSE -- No eligible trips were found
8799                   FND_MESSAGE.SET_NAME('WSH','WSH_FTE_CS_NO_VALID_TRIPS');
8800                   x_return_status  := WSH_UTIL_CORE.G_RET_STS_ERROR;
8801                   WSH_UTIL_CORE.add_message(x_return_status);
8802                   --
8803                   -- Debug Statements
8804                   --
8805                   IF l_debug_on THEN
8806                      WSH_DEBUG_SV.logmsg(l_module_name,  'NO VALID TRIP FOUND FOR PROCESS CARRIER SELECTION '  );
8807                   END IF;
8808                   --
8809       END IF;
8810 
8811       -- Standard call to get message count and if count is 1,
8812       -- get message info.
8813 
8814       FND_MSG_PUB.Count_And_Get (
8815        p_count         =>      x_msg_count,
8816        p_data          =>      x_msg_data ,
8817        p_encoded       =>      FND_API.G_FALSE );
8818 
8819       IF l_debug_on THEN
8820        wsh_debug_sv.log (l_module_name,'No. of messages stacked : ',to_char(x_msg_count));
8821        wsh_debug_sv.pop(l_module_name);
8822       END IF;
8823 
8824       --
8825 
8826 EXCEPTION
8827 WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8828 
8829       ROLLBACK TO before_trip_update;
8830       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR ;
8831       --
8832       IF l_debug_on THEN
8833         WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
8834         WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:FND_API.G_EXC_UNEXPECTED_ERROR');
8835       END IF;
8836       --
8837 
8838 WHEN others THEN
8839 
8840     ROLLBACK TO before_trip_update;
8841     IF c_get_trip_details%ISOPEN THEN
8842        CLOSE c_get_trip_details;
8843     END IF;
8844     IF c_get_trip_stops%ISOPEN THEN
8845        CLOSE c_get_trip_stops;
8846     END IF;
8847     IF c_get_firststop_dlvy%ISOPEN THEN
8848        CLOSE c_get_firststop_dlvy;
8849     END IF;
8850     IF c_get_laststop_dlvy%ISOPEN THEN
8851        CLOSE c_get_laststop_dlvy;
8852     END IF;
8853     IF c_ship_to_site_use%ISOPEN THEN
8854        CLOSE c_ship_to_site_use;
8855     END IF;
8856 /*
8857     IF c_get_trip_cmove%ISOPEN THEN
8858        CLOSE c_get_trip_cmove;
8859     END IF;
8860 */
8861 
8862     wsh_util_core.default_handler('WSH_TRIPS_ACTIONS.PROCESS_CARRIER_SELECTION');
8863     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
8864 
8865     IF l_debug_on THEN
8866       WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
8867       WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
8868     END IF;
8869 
8870 END PROCESS_CARRIER_SELECTION;
8871 
8872 --------------------------------------------------------------------------------------------
8873 -- Name:       Remove_Consolidation
8874 -- Purpose:    Removes the consolidation delivery from the trip.
8875 --             If p_unassign_all is 'Y', it will also unassign all deliveries
8876 --             from the trip.
8877 --             If p_unassign_all is 'N', it will retain all the content
8878 --             deliveries, directly or indirectly (through consol delivery)
8879 --             assign to the trip, getting rid of the consol deliveries.
8880 -- Parameters:
8881 --             p_trip_id:       Trip that needs its children unassigned from.
8882 --             p_unassign_all: 'Y'/'N'. If 'Y' will unassign all deliveries from
8883 --                             the trip resulting in an empty trip.
8884 --             x_return_status: return status.
8885 
8886 
8887 
8888 PROCEDURE Remove_Consolidation(
8889                 p_trip_id_tab   IN wsh_util_core.id_tab_type,
8890                 p_unassign_all  IN VARCHAR2,
8891                 p_caller        IN VARCHAR2,
8892                 x_return_status OUT NOCOPY VARCHAR2) IS
8893 
8894 cursor c_get_trip_deliveries(p_trip_id in number) is
8895 select d.delivery_id, d.delivery_type
8896 from wsh_new_deliveries d, wsh_trip_stops s, wsh_delivery_legs l
8897 where d.delivery_id = l.delivery_id
8898 and   l.pick_up_stop_id = s.stop_id
8899 and   s.trip_id = p_trip_id
8900 order by d.delivery_type;
8901 
8902 l_delivery_tab wsh_util_core.id_tab_type;
8903 l_delivery_type_tab wsh_util_core.column_tab_type;
8904 l_consol_delivery_tab wsh_util_core.id_tab_type;
8905 l_content_delivery_tab wsh_util_core.id_tab_type;
8906 l_dummy_tab wsh_util_core.id_tab_type;
8907 j NUMBER := 0;
8908 k NUMBER := 0;
8909 
8910 l_num_warnings              NUMBER  := 0;
8911 l_num_errors                NUMBER  := 0;
8912 l_return_status             VARCHAR2(30);
8913 
8914 
8915 WSH_INVALID_ACTION          EXCEPTION;
8916 
8917 l_debug_on BOOLEAN;
8918 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' ||
8919 'Remove_Consolidation';
8920 
8921 BEGIN
8922 
8923    l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
8924    --
8925    IF l_debug_on IS NULL
8926    THEN
8927      l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
8928    END IF;
8929    --
8930    IF l_debug_on THEN
8931      wsh_debug_sv.push (l_module_name);
8932      WSH_DEBUG_SV.log(l_module_name,'p_unassign_all', p_unassign_all);
8933      WSH_DEBUG_SV.log(l_module_name,'p_caller', p_caller);
8934    END IF;
8935 
8936    FOR i in 1..p_trip_id_tab.count LOOP
8937 
8938      IF l_debug_on THEN
8939        WSH_DEBUG_SV.log(l_module_name,'p_trip_id_tab', p_trip_id_tab(i));
8940      END IF;
8941      -- separate the consol deliveries from the content deliveries
8942      -- that are directly assigned to the trip
8943      l_delivery_tab.delete;
8944      l_consol_delivery_tab.delete;
8945      j := 0;
8946      k := 0;
8947      FOR del in c_get_trip_deliveries(p_trip_id_tab(i)) LOOP
8948 
8949          IF del.delivery_type = 'CONSOLIDATION' THEN
8950             j := j +1;
8951             l_consol_delivery_tab(j) := del.delivery_id;
8952          ELSE
8953             k := k+1;
8954             l_delivery_tab(k) := del.delivery_id;
8955          END IF;
8956 
8957      END LOOP;
8958 
8959      -- Unassign the consol deliveries from the trip
8960      IF (NVL(p_unassign_all, 'N') = 'N') AND l_consol_delivery_tab.count = 0 THEN
8961         RAISE WSH_INVALID_ACTION;
8962      END IF;
8963 
8964      FOR i in 1..l_consol_delivery_tab.count LOOP
8965 
8966         IF l_debug_on THEN
8967             WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_NEW_DELIVERY_ACTIONS.Unassign_Dels_from_Consol_Del',WSH_DEBUG_SV.C_PROC_LEVEL);
8968         END IF;
8969         WSH_NEW_DELIVERY_ACTIONS.Unassign_Dels_from_Consol_Del(
8970           p_parent_del     => l_consol_delivery_tab(i),
8971           p_caller         => p_caller,
8972           p_del_tab        => l_dummy_tab,
8973           x_return_status  => l_return_status);
8974 
8975         wsh_util_core.api_post_call
8976                       (
8977                         p_return_status => l_return_status,
8978                         x_num_warnings  => l_num_warnings,
8979                         x_num_errors    => l_num_errors
8980                       );
8981      END LOOP;
8982 
8983      -- Unassign the content deliveries that are directly assigned to the trip.
8984 
8985      IF p_unassign_all = 'Y' THEN
8986 
8987         IF l_delivery_tab.count = 0 THEN
8988            RAISE WSH_INVALID_ACTION;
8989         END IF;
8990         IF l_debug_on THEN
8991             WSH_DEBUG_SV.logmsg(l_module_name,'Calling program unit WSH_Trips_Actions.Unassign_Trip',WSH_DEBUG_SV.C_PROC_LEVEL);
8992         END IF;
8993 
8994         WSH_Trips_Actions.Unassign_Trip(
8995                    p_del_rows         => l_delivery_tab,
8996                    p_trip_id          => p_trip_id_tab(i),
8997                    x_return_status    => l_return_status);
8998 
8999         wsh_util_core.api_post_call
9000                       (
9001                         p_return_status => l_return_status,
9002                         x_num_warnings  => l_num_warnings,
9003                         x_num_errors    => l_num_errors
9004                       );
9005      END IF;
9006 
9007    END LOOP;
9008 
9009    IF l_num_errors > 0
9010    THEN
9011       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9012    ELSIF l_num_warnings > 0
9013    THEN
9014       x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
9015    ELSE
9016       x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
9017    END IF;
9018 --
9019 -- Debug Statements
9020 --
9021 IF l_debug_on THEN
9022     WSH_DEBUG_SV.pop(l_module_name);
9023 END IF;
9024 EXCEPTION
9025   WHEN WSH_INVALID_ACTION THEN
9026     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9027     --
9028     -- Debug Statements
9029     FND_MESSAGE.SET_NAME('WSH','WSH_REMOVE_CONSOL_ERR');
9030     wsh_util_core.add_message(x_return_status);
9031     IF l_debug_on THEN
9032        WSH_DEBUG_SV.logmsg(l_module_name,'WSH_INVALID_ACTION exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
9033        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:WSH_INVALID_ACTION');
9034     END IF;
9035 
9036   WHEN FND_API.G_EXC_ERROR THEN
9037     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9038     --
9039     -- Debug Statements
9040     --
9041     IF l_debug_on THEN
9042        WSH_DEBUG_SV.logmsg(l_module_name,'FND_API.G_EXC_ERROR exception has occured.',WSH_DEBUG_SV.C_EXCEP_LEVEL);
9043        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:FND_API.G_EXC_ERROR');
9044     END IF;
9045   WHEN OTHERS THEN
9046     wsh_util_core.default_handler('wsh_trips_actions',l_module_name);
9047       --
9048     IF l_debug_on THEN
9049       wsh_debug_sv.pop(l_module_name, 'EXCEPTION:OTHERS');
9050     END IF;
9051 
9052 END Remove_Consolidation;
9053 --
9054 
9055 END WSH_TRIPS_ACTIONS;