DBA Data[Home] [Help]

PACKAGE BODY: APPS.WSH_TRIPS_ACTIONS

Source


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