DBA Data[Home] [Help]

PACKAGE BODY: APPS.WSH_TP_RELEASE_INT

Source


1 PACKAGE BODY WSH_TP_RELEASE_INT as
2 /* $Header: WSHTPREB.pls 120.7 2006/01/18 09:52:43 parkhj noship $ */
3 
4 G_TP_RELEASE_CODE CONSTANT VARCHAR2(30) := WSH_TP_RELEASE_GRP.G_TP_RELEASE_CODE;
5 
6 G_PKG_NAME CONSTANT VARCHAR2(50) := 'WSH_TP_RELEASE_INT';
7 
8 
9 TYPE date_tab_type IS TABLE OF DATE INDEX BY BINARY_INTEGER;
10 TYPE planned_flag_tab_type IS TABLE OF WSH_TRIPS.PLANNED_FLAG%TYPE INDEX BY BINARY_INTEGER;
11 
12 
13 -- This cursor c_map_lines is used by generate_lock_candidates
14 -- It needs to be internally global because of reference
15 -- in an internal API attributes_match.
16 -- choice 1 : SE lines in SE deliveries matching the plan's lines
17 -- and their deliveries that were firm at time of snapshot
18 -- (which may or may not be currently firmed)
19 -- choice 2 : unassigned SE lines
20 -- choice 3 : SE lines in other SE deliveries that are not firmed
21 CURSOR c_map_lines(x_plan_delivery_detail_id    IN NUMBER,
22                    x_plan_delivery_id           IN NUMBER,
23                    x_plan_source_code           IN VARCHAR2,
24                    x_plan_source_header_id      IN NUMBER,
25                    x_plan_source_line_id        IN NUMBER,
26                    x_plan_source_line_set_id    IN NUMBER,
27                    x_plan_po_shipment_line_id   IN NUMBER, -- inbound/drop
28                    x_plan_ship_from_location_id IN NUMBER,
29                    x_plan_ship_to_location_id   IN NUMBER) IS
30 SELECT
31      decode(wda.delivery_id, x_plan_delivery_id,1,null, 2, 3) choice,
32      decode(wdd.delivery_detail_id,
33                x_plan_delivery_detail_id, 1,
34                decode(wdd.source_line_id,
35                          x_plan_source_line_id, 2,
36                          3)) scope,
37      wdd.delivery_detail_id,
38      nvl(wdd.shipped_quantity, nvl(wdd.picked_quantity, wdd.requested_quantity)) quantity,
39      wdd.requested_quantity_uom quantity_uom,
40      wdd.source_code,
41      wdd.source_header_id,
42      wdd.source_line_set_id,
43      wdd.source_line_id,
44      wdd.ship_from_location_id,
45      wdd.ship_to_location_id,
46      wdd.inventory_item_id,
47      wdd.released_status,
48      wdd.move_order_line_id,
49      wdd.line_direction,
50      wdd.ship_set_id,
51      wdd.po_shipment_line_id,  -- inbound/drop
52      wdd.top_model_line_id,
53      wdd.ato_line_id,
54      wdd.ship_model_complete_flag,
55      wdd.organization_id,
56      wdd.customer_id,
57      wdd.fob_code,
58      wdd.freight_terms_code,
59      wdd.intmed_ship_to_location_id,
60      wdd.ship_method_code,
61      wdd.mode_of_transport,
62      wdd.service_level,
63      wdd.carrier_id,
64      wda.parent_delivery_detail_id parent_cont_id,
65      wda.delivery_id,
66      wdd.src_requested_quantity,
67      wdd.src_requested_quantity_uom,
68      wdd.shipping_control,
69      wdd.vendor_id,
70      wdd.party_id,
71      wdd.wv_frozen_flag -- WV changes
72 FROM   wsh_delivery_details wdd,
73        wsh_delivery_assignments_v wda
74 WHERE
75      (
76           wdd.source_code = x_plan_source_code
77       AND wdd.source_header_id = x_plan_source_header_id
78       AND (   wdd.source_line_set_id = x_plan_source_line_set_id
79            OR wdd.source_line_id     = x_plan_source_line_id)
80       AND (   x_plan_po_shipment_line_id IS NULL   -- inbound/drop
81            OR wdd.po_shipment_line_id = x_plan_po_shipment_line_id)
82      )
83      AND wdd.ship_from_location_id = x_plan_ship_from_location_id
84      AND wdd.ship_to_location_id = x_plan_ship_to_location_id
85      AND wdd.released_status IN ('N', 'R', 'B', 'S', 'Y', 'X')
86      AND nvl(wdd.ignore_for_planning,'N') = 'N'
87      AND wda.delivery_detail_id = wdd.delivery_detail_id
88      AND wdd.container_flag = 'N'
89      AND (
90           (wda.delivery_id IS NOT NULL
91            AND wda.delivery_id = x_plan_delivery_id
92            AND x_plan_delivery_id IS NOT NULL
93           )
94           OR
95           (wda.delivery_id IS NULL
96           )
97           OR
98           (exists ( select 'x'
99                 from wsh_new_deliveries       wnd
100                 where  wnd.delivery_id = wda.delivery_id
101                 AND wnd.planned_flag = 'N'
102                 AND wnd.status_code IN ('OP', 'SA')
103                 AND wnd.delivery_id <> NVL(x_plan_delivery_id, 0)
104                 )
105           )
106          )
107 ORDER BY choice ASC, scope ASC, quantity DESC;
108 
109 
110 PROCEDURE init_context(
111            x_context                  IN OUT NOCOPY context_rec_type,
112            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
113            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
114            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
115            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
116            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
117            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
118            x_plan_trip_moves          IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
119            x_plan_moves               IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
120            x_used_details             IN OUT NOCOPY used_details_tab_type,
121            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
122            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
123            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
124            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
125            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
126            x_return_status               OUT NOCOPY VARCHAR2
127           );
128 
129 PROCEDURE resync_interface_tables(
130            x_context                  IN OUT NOCOPY context_rec_type,
131            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
132            x_return_status               OUT NOCOPY VARCHAR2
133           );
134 
135 PROCEDURE generate_lock_candidates(
136            x_context                  IN OUT NOCOPY context_rec_type,
137            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
138            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
139            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
140            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
141            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
142            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
143            x_plan_trip_moves          IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
144            x_plan_moves               IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
145            x_used_details             IN OUT NOCOPY used_details_tab_type,
146            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
147            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
148            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
149            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
150            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
151            x_return_status               OUT NOCOPY VARCHAR2
152 );
153 
154 PROCEDURE validate_plan(
155            x_context                  IN OUT NOCOPY context_rec_type,
156            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
157            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
158            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
159            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
160            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
161            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
162            x_used_details             IN OUT NOCOPY used_details_tab_type,
163            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
164            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
165            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
166            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
167            x_return_status               OUT NOCOPY VARCHAR2
168           );
169 
170 PROCEDURE reconciliate_plan(
171            x_context                  IN OUT NOCOPY context_rec_type,
172            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
173            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
174            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
175            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
176            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
177            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
178            x_plan_trip_moves          IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
179            x_plan_moves               IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
180            x_used_details             IN OUT NOCOPY used_details_tab_type,
181            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
182            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
183            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
184            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
185            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
186            x_return_status               OUT NOCOPY VARCHAR2
187           );
188 
189 PROCEDURE plan_cleanup(
190            x_context                  IN OUT NOCOPY context_rec_type,
191            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
192            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
193            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
194            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
195            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
196            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
197            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
198            x_return_status               OUT NOCOPY VARCHAR2
199           );
200 
201 
202 FUNCTION attributes_match(
203            x_attributes IN plan_detail_rec_type,
204            x_values     IN c_map_lines%ROWTYPE)
205 RETURN BOOLEAN;
206 
207 
208 PROCEDURE flush_used_details(
209               x_context              IN OUT NOCOPY context_rec_type,
210               x_current_used_details IN OUT NOCOPY used_details_tab_type,
211               x_used_details         IN OUT NOCOPY used_details_tab_type,
212               x_errors_tab           IN OUT NOCOPY interface_errors_tab_type,
213               x_return_status           OUT NOCOPY VARCHAR2);
214 
215 
216 PROCEDURE map_dangling_containers(
217            x_context                  IN OUT NOCOPY context_rec_type,
218            p_delivery_index           IN            NUMBER,
219            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
220            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
221            x_return_status               OUT NOCOPY VARCHAR2
222           );
223 
224 
225 PROCEDURE match_deliveries(
226            x_context                  IN OUT NOCOPY context_rec_type,
227            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
228            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
229            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
230            x_used_details             IN OUT NOCOPY used_details_tab_type,
231            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
232            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
233            x_return_status               OUT NOCOPY VARCHAR2
234           );
235 
236 
237 PROCEDURE validate_wms(
238            x_context                  IN OUT NOCOPY context_rec_type,
239            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
240            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
241            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
242            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
243            x_return_status               OUT NOCOPY VARCHAR2
244           );
245 
246 
247 
248 PROCEDURE match_trips(
249            x_context                  IN OUT NOCOPY context_rec_type,
250            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
251            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
252            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
253            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
254            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
255            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
256            x_return_status               OUT NOCOPY VARCHAR2
257           );
258 
259 
260 PROCEDURE debug_dump_context(
261            x_context                  IN context_rec_type,
262            x_plan_details             IN plan_detail_tab_type,
263            x_track_conts              IN track_cont_tab_type,
264            x_plan_deliveries          IN plan_delivery_tab_type,
265            x_plan_legs                IN plan_leg_tab_type,
266            x_plan_stops               IN plan_stop_tab_type,
267            x_plan_trips               IN plan_trip_tab_type,
268            x_plan_trip_moves          IN WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
269            x_plan_moves               IN WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
270            x_used_details             IN used_details_tab_type,
271            x_delivery_unassigns       IN delivery_unassign_tab_type,
272            x_trip_unassigns           IN trip_unassign_tab_type,
273            x_obsoleted_stops          IN obsoleted_stop_tab_type,
274            x_obsoleted_trip_moves     IN WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type
275           );
276 
277 
278 PROCEDURE copy_delivery_record(
279            p_plan_delivery_rec   IN            plan_delivery_rec_type,
280            x_delivery_attrs_rec     OUT NOCOPY wsh_new_deliveries_pvt.delivery_rec_type,
281            x_return_status          OUT NOCOPY VARCHAR2
282           );
283 
284 PROCEDURE copy_trip_record(
285            p_plan_trip_rec   IN            plan_trip_rec_type,
286            x_trip_attrs_rec     OUT NOCOPY wsh_trips_pvt.trip_rec_type
287           );
288 
289 PROCEDURE copy_stop_record(
290            p_plan_stop_rec   IN            plan_stop_rec_type,
291            p_plan_trips      IN            plan_trip_tab_type,
292            x_stop_attrs_rec     OUT NOCOPY wsh_trip_stops_pvt.trip_stop_rec_type
293           );
294 
295 PROCEDURE create_update_plan_trips(
296            p_phase                    IN            NUMBER,
297            x_context                  IN OUT NOCOPY context_rec_type,
298            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
299            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
300            x_return_status               OUT NOCOPY VARCHAR2
301           );
302 
303 
304 PROCEDURE insert_interface_errors(
305             p_errors_tab    IN         interface_errors_tab_type,
306             x_return_status OUT NOCOPY VARCHAR2);
307 
308 FUNCTION get_plan_trip_num(
309              p_context      IN         context_rec_type)
310 RETURN VARCHAR2;
311 
312 PROCEDURE Log_WV_Exceptions(
313           p_details_loc_tab in wsh_util_core.id_tab_type,
314           p_deliveries_loc_tab in wsh_util_core.id_tab_type,
315           p_stops_loc_tab in wsh_util_core.id_tab_type,
316           x_return_status out NOCOPY varchar2);
317 
318 
319 --
320 --  Procedure:          release_plan
321 --  Parameters:
322 --               p_group_ids           list of group_ids to process their
323 --                                     WSH_TRIPS_INTERFACE records and
324 --                                     their associated tables' records.
325 --               p_commit_flag         FND_API.G_TRUE - commit changes; FND_API.G_FALSE - do not commit
326 --               x_return_status       return status
327 --                                        success means all groups have been released.
328 --                                        warning means at least one group is released and at least one
329 --                                                      group failed.
330 --                                        error   means all groups failed.
331 --                                        caller needs to look at WSH_INTERFACE_ERRORS
332 --                                        to check for groups that failed.
333 --
334 --  Description:
335 --               Reconciliate shipping data with the transportation
336 --               plan populated in the WSH and FTE interface tables.
337 --
338 --
339 PROCEDURE release_plan(
340   p_group_ids              IN            WSH_TP_RELEASE_GRP.id_tab_type,
341   p_commit_flag            IN            VARCHAR2,
342   x_return_status          OUT NOCOPY    VARCHAR2)
343 IS
344   l_index BINARY_INTEGER := NULL;
345   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'RELEASE_PLAN';
346   --
347   l_debug_on BOOLEAN;
348   --
349   l_savepoint_set BOOLEAN := FALSE;
350 
351   l_context                  context_rec_type;
352   l_plan_details             plan_detail_tab_type;
353   l_track_conts              track_cont_tab_type;
354   l_plan_deliveries          plan_delivery_tab_type;
355   l_plan_legs                plan_leg_tab_type;
356   l_plan_stops               plan_stop_tab_type;
357   l_plan_trips               plan_trip_tab_type;
358   l_plan_trip_moves          WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type;
359   l_plan_moves               WSH_FTE_TP_INTEGRATION.plan_move_tab_type;
360   l_used_details             used_details_tab_type;
361   l_delivery_unassigns       delivery_unassign_tab_type;
362   l_trip_unassigns           trip_unassign_tab_type;
363   l_obsoleted_stops          obsoleted_stop_tab_type;
364   l_obsoleted_trip_moves     WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type;
365   l_errors_tab               interface_errors_tab_type;
366   l_return_status            VARCHAR2(1);
367   l_group_error_count        NUMBER := 0;
368 
369 BEGIN
370   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
371   --
372   IF l_debug_on IS NULL THEN
373     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
374   END IF;
375 
376   IF l_debug_on THEN
377     WSH_DEBUG_SV.push(l_module_name);
378   END IF;
379 
380   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
381 
382   l_index := p_group_ids.FIRST;
383   WHILE l_index IS NOT NULL LOOP
384     SAVEPOINT before_group;
385     l_savepoint_set := TRUE;
386 
387     IF l_debug_on THEN
388         WSH_DEBUG_SV.log(l_module_name,'looping group_id', p_group_ids(l_index));
389     END IF;
390 
391     l_context.group_id     := p_group_ids(l_index);
392     l_context.wms_in_group := FALSE;
393 
394     -- reset the variables before mapping each group.
395     Init_Context(
396        x_context                  => l_context,
397        x_plan_details             => l_plan_details,
398        x_track_conts              => l_track_conts,
399        x_plan_deliveries          => l_plan_deliveries,
400        x_plan_legs                => l_plan_legs,
401        x_plan_stops               => l_plan_stops,
402        x_plan_trips               => l_plan_trips,
403        x_plan_trip_moves          => l_plan_trip_moves,
404        x_plan_moves               => l_plan_moves,
405        x_used_details             => l_used_details,
406        x_delivery_unassigns       => l_delivery_unassigns,
407        x_trip_unassigns           => l_trip_unassigns,
408        x_obsoleted_stops          => l_obsoleted_stops,
409        x_obsoleted_trip_moves     => l_obsoleted_trip_moves,
410        x_errors_tab               => l_errors_tab,
411        x_return_status            => l_return_status
412     );
413 
414     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
415       GOTO group_error;
416     END IF;
417 
418 
419     -- update wsh_del_details_interface
420     -- with current source_line_set and source_line_id of
421     -- delivery details that were snapshot or their associated details.
422     Resync_Interface_Tables(
423        x_context                  => l_context,
424        x_errors_tab               => l_errors_tab,
425        x_return_status => l_return_status
426     );
427 
428     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
429       GOTO group_error;
430     END IF;
431 
432 
433     -- map the interface records to shipping records
434     -- and lock the shipping records and their associations.
435     Generate_Lock_Candidates(
436        x_context                  => l_context,
437        x_plan_details             => l_plan_details,
438        x_track_conts              => l_track_conts,
439        x_plan_deliveries          => l_plan_deliveries,
440        x_plan_legs                => l_plan_legs,
441        x_plan_stops               => l_plan_stops,
442        x_plan_trips               => l_plan_trips,
443        x_plan_trip_moves          => l_plan_trip_moves,
444        x_plan_moves               => l_plan_moves,
445        x_used_details             => l_used_details,
446        x_delivery_unassigns       => l_delivery_unassigns,
447        x_trip_unassigns           => l_trip_unassigns,
448        x_obsoleted_stops          => l_obsoleted_stops,
449        x_obsoleted_trip_moves     => l_obsoleted_trip_moves,
450        x_errors_tab               => l_errors_tab,
451        x_return_status => l_return_status
452     );
453 
454     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
455       GOTO group_error;
456     END IF;
457 
458     -- identify shipping records that need to be
459     -- unassigned from the mapped shipping records
460     -- and perform other validations not done by above calls.
461     Validate_Plan(
462        x_context                  => l_context,
463        x_plan_details             => l_plan_details,
464        x_track_conts              => l_track_conts,
465        x_plan_deliveries          => l_plan_deliveries,
466        x_plan_legs                => l_plan_legs,
467        x_plan_stops               => l_plan_stops,
468        x_plan_trips               => l_plan_trips,
469        x_used_details             => l_used_details,
470        x_delivery_unassigns       => l_delivery_unassigns,
471        x_trip_unassigns           => l_trip_unassigns,
472        x_obsoleted_stops          => l_obsoleted_stops,
473        x_errors_tab               => l_errors_tab,
474        x_return_status => l_return_status
475     );
476 
477     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
478       GOTO group_error;
479     END IF;
480 
481     -- execute the actions to split, create, unassign, update
482     -- and assign shipping records as per the plan.
483     Reconciliate_Plan(
484        x_context                  => l_context,
485        x_plan_details             => l_plan_details,
486        x_track_conts              => l_track_conts,
487        x_plan_deliveries          => l_plan_deliveries,
488        x_plan_legs                => l_plan_legs,
489        x_plan_stops               => l_plan_stops,
490        x_plan_trips               => l_plan_trips,
491        x_plan_trip_moves          => l_plan_trip_moves,
492        x_plan_moves               => l_plan_moves,
493        x_used_details             => l_used_details,
494        x_delivery_unassigns       => l_delivery_unassigns,
495        x_trip_unassigns           => l_trip_unassigns,
496        x_obsoleted_stops          => l_obsoleted_stops,
497        x_obsoleted_trip_moves     => l_obsoleted_trip_moves,
498        x_errors_tab               => l_errors_tab,
499        x_return_status => l_return_status
500     );
501 
502     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
503       GOTO group_error;
504     END IF;
505 
506 
507 
508     -- remove deliveries and trips that have become empty
509     -- and log exceptions against plan's delivery details
510     -- whose dates do not agree with the dates of
511     -- their initial pick up and ultimate drop off stops.
512     Plan_Cleanup(
513        x_context                  => l_context,
514        x_plan_details             => l_plan_details,
515        x_plan_deliveries          => l_plan_deliveries,
516        x_plan_stops               => l_plan_stops,
517        x_plan_trips               => l_plan_trips,
518        x_trip_unassigns           => l_trip_unassigns,
519        x_obsoleted_trip_moves     => l_obsoleted_trip_moves,
520        x_errors_tab               => l_errors_tab,
521        x_return_status => l_return_status
522     );
523 
524     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
525       GOTO group_error;
526     END IF;
527 
528     IF l_debug_on THEN
529       -- dump will help troubleshoot issues with successful release.
530       debug_dump_Context(
531        x_context                  => l_context,
532        x_plan_details             => l_plan_details,
533        x_track_conts              => l_track_conts,
534        x_plan_deliveries          => l_plan_deliveries,
535        x_plan_legs                => l_plan_legs,
536        x_plan_stops               => l_plan_stops,
537        x_plan_trips               => l_plan_trips,
538        x_plan_trip_moves          => l_plan_trip_moves,
539        x_plan_moves               => l_plan_moves,
540        x_used_details             => l_used_details,
541        x_delivery_unassigns       => l_delivery_unassigns,
542        x_trip_unassigns           => l_trip_unassigns,
543        x_obsoleted_stops          => l_obsoleted_stops,
544        x_obsoleted_trip_moves     => l_obsoleted_trip_moves
545       );
546     END IF;
547 
548     GOTO next_record;
549 
550 <<group_error>>
551     IF l_debug_on THEN
552         WSH_DEBUG_SV.log(l_module_name,'return_status failing group', l_return_status);
553     END IF;
554     l_savepoint_set := FALSE;
555     ROLLBACK TO before_group;
556 
557     IF l_debug_on THEN
558       -- dump will help troubleshoot the plan failure.
559       debug_dump_Context(
560        x_context                  => l_context,
561        x_plan_details             => l_plan_details,
562        x_track_conts              => l_track_conts,
563        x_plan_deliveries          => l_plan_deliveries,
564        x_plan_legs                => l_plan_legs,
565        x_plan_stops               => l_plan_stops,
566        x_plan_trips               => l_plan_trips,
567        x_plan_trip_moves          => l_plan_trip_moves,
568        x_plan_moves               => l_plan_moves,
569        x_used_details             => l_used_details,
570        x_delivery_unassigns       => l_delivery_unassigns,
571        x_trip_unassigns           => l_trip_unassigns,
572        x_obsoleted_stops          => l_obsoleted_stops,
573        x_obsoleted_trip_moves     => l_obsoleted_trip_moves
574       );
575     END IF;
576 
577     IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
578        -- unexpected error needs to set a TP failure message,
579        -- so that we can pull the detail messages.
580        stamp_interface_error(p_group_id => l_context.group_id,
581                               p_entity_table_name => 'NONE',
582                               p_entity_interface_id => -1,
583                               p_message_name => 'WSH_TP_F_UNEXP_ERROR',  -- new message
584                               x_errors_tab => l_errors_tab,
585                               x_return_status => l_return_status);
586     END IF;
587 
588     IF l_errors_tab.count > 0 THEN
589       insert_interface_errors(
590            p_errors_tab    => l_errors_tab,
591            x_return_status => l_return_status);
592     END IF;
593     l_group_error_count := l_group_error_count + 1;
594 
595 <<next_record>>
596     IF l_debug_on THEN
597         WSH_DEBUG_SV.log(l_module_name,'finished group_id', p_group_ids(l_index));
598     END IF;
599 
600     l_savepoint_set := FALSE;
601     IF p_commit_flag = FND_API.G_TRUE THEN
602       COMMIT;
603     END IF;
604 
605     -- before we go on to the next group, clear the errors.
606     l_errors_tab.DELETE;
607     l_index := p_group_ids.NEXT(l_index);
608 
609   END LOOP;
610 
611 
612   IF l_debug_on THEN
613         WSH_DEBUG_SV.log(l_module_name,'l_group_error_count', l_group_error_count);
614   END IF;
615 
616   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
617 
618   IF      l_group_error_count > 0
619       AND l_group_error_count < p_group_ids.COUNT THEN
620     x_return_status := WSH_UTIL_CORE.G_RET_STS_WARNING;
621   ELSIF l_group_error_count = p_group_ids.COUNT THEN
622     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
623   END IF;
624 
625   IF l_debug_on THEN
626      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
627      WSH_DEBUG_SV.pop(l_module_name);
628   END IF;
629 
630   EXCEPTION
631     WHEN OTHERS THEN
632       IF l_savepoint_set THEN
633         ROLLBACK TO before_group;
634       END IF;
635       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
636       WSH_UTIL_CORE.DEFAULT_HANDLER(
637                         'WSH_TP_RELEASE.RELEASE_PLAN',
638                         l_module_name);
639 
640     IF l_debug_on THEN
641        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
642        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
643     END IF;
644 
645 END release_plan;
646 
647 
648 
649 
650 --
651 --  Procedure:          purge_interface_tables
652 --  Parameters:
653 --               p_group_ids           list of group_ids to purge their
654 --                                     WSH_TRIPS_INTERFACE records and
655 --                                     their associated tables' records.
656 --                                     WSH_INTERFACE_ERRORS will be purged.
657 --               p_commit_flag         FND_API.G_TRUE - commit changes; FND_API.G_FALSE - do not commit
658 --               x_return_status       return status
659 --
660 --  Description:
661 --               Delete the records from WSH and FTE interface tables.
662 --
663 PROCEDURE purge_interface_tables(
664   p_group_ids              IN            WSH_TP_RELEASE_GRP.id_tab_type,
665   p_commit_flag            IN            VARCHAR2,
666   x_return_status          OUT NOCOPY    VARCHAR2)
667 IS
668   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'PURGE_INTERFACE_TABLES';
669   --
670   l_debug_on BOOLEAN;
671   --
672 BEGIN
673   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
674   --
675   IF l_debug_on IS NULL THEN
676     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
677   END IF;
678 
679   IF l_debug_on THEN
680     WSH_DEBUG_SV.push(l_module_name);
681   END IF;
682   --
683   --
684 
685   SAVEPOINT before_purge;
686 
687   -- Purge wsh_del_details interface.
688   -- Remove usage of distinct in code for Bug 3821688 from each Bulk Delete
689 
690   FORALL i in p_group_ids.FIRST ..p_group_ids.LAST
691     delete from wsh_del_details_interface wddi
692     where wddi.delivery_detail_interface_id in (
693     select wdai.delivery_detail_interface_id
694     from   wsh_trips_interface wti,
695          wsh_trip_stops_interface wtsi,
696          wsh_del_legs_interface wdli,
697          wsh_del_assgn_interface wdai
698     where  wti.group_id = p_group_ids(i)
699     and    wti.trip_interface_id = wtsi.trip_interface_id
700     and    wti.interface_action_code = G_TP_RELEASE_CODE
701     and    wtsi.stop_interface_id = wdli.pick_up_stop_interface_id
702     and    wtsi.interface_action_code = G_TP_RELEASE_CODE
703     and    wdli.delivery_interface_id = wdai.delivery_interface_id
704     and    wdli.interface_action_code = G_TP_RELEASE_CODE
705     and    wdai.delivery_interface_id is not null
706     and    wdai.interface_action_code = G_TP_RELEASE_CODE)
707     and    wddi.interface_action_code = G_TP_RELEASE_CODE;
708 
709   -- Purge wsh_del_assgn_interface
710 
711   FORALL i in p_group_ids.FIRST ..p_group_ids.LAST
712     delete from wsh_del_assgn_interface wdai
713     where  wdai.delivery_interface_id in (
714     select wdli.delivery_interface_id
715     from   wsh_trips_interface wti,
716            wsh_trip_stops_interface wtsi,
717            wsh_del_legs_interface wdli
718     where  wti.group_id = p_group_ids(i)
719     and    wti.trip_interface_id = wtsi.trip_interface_id
720     and    wti.interface_action_code = G_TP_RELEASE_CODE
721     and    wtsi.stop_interface_id = wdli.pick_up_stop_interface_id
722     and    wtsi.interface_action_code = G_TP_RELEASE_CODE
723     and    wdli.interface_action_code = G_TP_RELEASE_CODE)
724     and    wdai.interface_action_code = G_TP_RELEASE_CODE;
725 
726 
727   -- Purge wsh_new_del_interface
728 
729   FORALL i in p_group_ids.FIRST ..p_group_ids.LAST
730     delete from wsh_new_del_interface wndi
731     where  wndi.delivery_interface_id in (
732     select wdli.delivery_interface_id
733     from   wsh_trips_interface wti,
734            wsh_trip_stops_interface wtsi,
735            wsh_del_legs_interface wdli
736     where  wti.group_id = p_group_ids(i)
737     and    wti.trip_interface_id = wtsi.trip_interface_id
738     and    wti.interface_action_code = G_TP_RELEASE_CODE
739     and    wtsi.stop_interface_id = wdli.pick_up_stop_interface_id
740     and    wtsi.interface_action_code = G_TP_RELEASE_CODE
741     and    wdli.interface_action_code = G_TP_RELEASE_CODE)
742     and    wndi.interface_action_code = G_TP_RELEASE_CODE;
743 
744   -- Purge wsh_del_legs_interface
745 
746   FORALL i in p_group_ids.FIRST ..p_group_ids.LAST
747     delete from wsh_del_legs_interface wdli
748     where wdli.pick_up_stop_interface_id in (
749     select wtsi.stop_interface_id
750     from   wsh_trips_interface wti,
751            wsh_trip_stops_interface wtsi
752     where  wti.group_id = p_group_ids(i)
753     and    wti.trip_interface_id = wtsi.trip_interface_id
754     and    wti.interface_action_code = G_TP_RELEASE_CODE
755     and    wtsi.interface_action_code = G_TP_RELEASE_CODE)
756     and    wdli.interface_action_code = G_TP_RELEASE_CODE;
757 
758   -- Purge wsh_trip_stops_interface
759 
760   FORALL i in p_group_ids.FIRST ..p_group_ids.LAST
761     delete from wsh_trip_stops_interface wtsi
762     where wtsi.trip_interface_id in (
763     select wtsi.stop_interface_id
764     from   wsh_trips_interface wti
765     where  wti.group_id = p_group_ids(i)
766     and    wti.interface_action_code = G_TP_RELEASE_CODE)
767     and    wtsi.interface_action_code = G_TP_RELEASE_CODE;
768 
769 
770   -- Purge continuous moves in FTE interface tables
771 
772   WSH_FTE_TP_INTEGRATION.purge_interface_tables(
773     p_group_ids     => p_group_ids,
774     x_return_status => x_return_status);
775 
776   IF x_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
777                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
778      IF l_debug_on THEN
779        WSH_DEBUG_SV.log(l_module_name, 'FTE interface tables failed: x_return_status', x_return_status);
780        WSH_DEBUG_SV.pop(l_module_name);
781      END IF;
782      RETURN;
783   END IF;
784 
785 
786   -- Purge wsh_trips_interface
787   -- (Trips have to be the last entity interface table purged
788   --  because this is the only entity interface table having GROUP_ID.)
789 
790   FORALL i in p_group_ids.FIRST ..p_group_ids.LAST
791     delete from wsh_trips_interface wti
792     where wti.group_id = p_group_ids(i)
793     and wti.INTERFACE_ACTION_CODE = G_TP_RELEASE_CODE;
794 
795 
796   -- Purge wsh_interface_errors
797 
798   FORALL i in p_group_ids.FIRST ..p_group_ids.LAST
799     delete from wsh_interface_errors wie
800     where wie.interface_error_group_id = p_group_ids(i)
801     and wie.interface_action_code = G_TP_RELEASE_CODE;
802 
803 
804   IF p_commit_flag = FND_API.G_TRUE THEN
805      commit;
806   END IF;
807 
808 
809   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
810 
811   --
812   --
813   IF l_debug_on THEN
814      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
815      WSH_DEBUG_SV.pop(l_module_name);
816   END IF;
817 
818   EXCEPTION
819     WHEN OTHERS THEN
820       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
821       WSH_UTIL_CORE.DEFAULT_HANDLER(
822                         'WSH_TP_RELEASE.PURGE_INTERFACE_TABLES',
823                         l_module_name);
824 
825     IF l_debug_on THEN
826        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
827        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
828     END IF;
829     ROLLBACK TO before_purge;
830 
831 END purge_interface_tables;
832 
833 
834 
835 
836 --
837 --  Procedure:          init_context
838 --  Parameters:
839 --               x_context             context in this session
840 --               x_plan_details        list of delivery details mapped to interface lines
841 --               x_track_conts         list of topmost containers to track
842 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
843 --               x_plan_legs           list of delivery legs mapped to interface legs
844 --               x_plan_stops          list of stops mapped to interface stops
845 --               x_plan_trips          list of trips mapped to interface trips
846 --               x_plan_trip_moves     list of trip moves mapped to interface trip moves (FTE)
847 --               x_plan_moves          list of moves mapped to interface moves (FTE)
848 --               x_used_details        list of delivery details partially used by the plan
849 --               x_delivery_unassigns  list of delivery lines to unassign from their deliveries
850 --               x_trip_unassigns      list of deliveries to unassign from their trips
851 --               x_obsoleted_stops     list of mapped trips' stops that are not mapped in the plan
852 --               x_obsoleted_trip_moves  list of mapped trips' moves that are not mapped in the plan
853 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
854 --               x_return_status       return status
855 --
856 --  Description:
857 --               Clears the message stack and context at the beginning
858 --               before processing a group's release plan.
859 --
860 PROCEDURE init_context(
861            x_context                  IN OUT NOCOPY context_rec_type,
862            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
863            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
864            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
865            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
866            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
867            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
868            x_plan_trip_moves          IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
869            x_plan_moves               IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
870            x_used_details             IN OUT NOCOPY used_details_tab_type,
871            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
872            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
873            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
874            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
875            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
876            x_return_status   OUT NOCOPY VARCHAR2
877           )
878 IS
879   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'INIT_CONTEXT';
880   --
881   l_debug_on BOOLEAN;
882 BEGIN
883   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
884   --
885   IF l_debug_on IS NULL THEN
886     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
887   END IF;
888 
889   IF l_debug_on THEN
890     WSH_DEBUG_SV.push(l_module_name);
891   END IF;
892   --
893   --
894 
895   -- Bug 3283987, initialize mesg stack for each group.
896   FND_MSG_PUB.initialize;
897 
898   x_plan_details.DELETE;
899   x_track_conts.DELETE;
900   x_plan_deliveries.DELETE;
901   x_plan_legs.DELETE;
902   x_plan_stops.DELETE;
903   x_plan_trips.DELETE;
904   x_plan_trip_moves.DELETE;
905   x_plan_moves.DELETE;
906   x_used_details.DELETE;
907   x_delivery_unassigns.DELETE;
908   x_trip_unassigns.DELETE;
909   x_obsoleted_stops.DELETE;
910   x_obsoleted_trip_moves.DELETE;
911   x_context.wv_exception_details.delete;
912   x_context.wv_exception_dels.delete;
913   x_context.wv_exception_stops.delete;
914 
915   -- J+ project, TP owns this profile
916   -- If profile is set then auto tender the trips
917   IF  fnd_profile.value('MST_AUTO_TENDER_ON') = 'Y' THEN
918     x_context.auto_tender_flag := 'Y';
919   ELSE
920     x_context.auto_tender_flag := 'N';
921   END IF;
922 
923   IF l_debug_on THEN
924     WSH_DEBUG_SV.log(l_module_name, 'Auto Tender Flag', x_context.auto_tender_flag);
925   END IF;
926 
927   x_context.linked_trip_count := 0;
928 
929   --
930   --
931   IF l_debug_on THEN
932      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
933      WSH_DEBUG_SV.pop(l_module_name);
934   END IF;
935 
936   EXCEPTION
937     WHEN OTHERS THEN
938       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
939       WSH_UTIL_CORE.DEFAULT_HANDLER(
940                         'WSH_TP_RELEASE.INIT_CONTEXT',
941                         l_module_name);
942 
943     IF l_debug_on THEN
944        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
945        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
946     END IF;
947 
948 END init_context;
949 
950 
951 
952 
953 --
954 --  Procedure:          resync_interface_tables
955 --  Parameters:
956 --               x_context             context in this session
957 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
958 --               x_return_status       return status
959 --
960 --  Description:
961 --               Resynchronizes the interface tables' records with their base table records'
962 --               values that may have been changed since the snapshot:
963 --               Outbound:
964 --               * fail plan if lines are completely canceled or shipped
965 --               * refresh the delivery detail's source_line_id, source_line_set_id
966 --               Inbound/Drop:
967 --               * fail plan if lines are completely closed (L) or shipped (C) or purged (P).
968 --               * refresh the delivery detail's po_shipment_line_id and its number
969 --                    (TP releases only po_shipment_line_number)
970 --
971 PROCEDURE resync_interface_tables(
972            x_context                  IN OUT NOCOPY context_rec_type,
973            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
974            x_return_status   OUT NOCOPY VARCHAR2
975           )
976 IS
977   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'RESYNC_INTERFACE_TABLES';
978   --
979   l_debug_on BOOLEAN;
980 
981   -- this cursor list the interface lines
982   cursor c_tp_released_lines (p_group_id in number,
983                               p_dir1     in varchar2,
984                               p_dir2     in varchar2) is
985   select distinct wddi.delivery_detail_interface_id delivery_detail_interface_id,
986          wddi.delivery_detail_id delivery_detail_id,
987          wddi.split_from_delivery_detail_id split_from_delivery_detail_id,
988          wddi.source_code,
989          wddi.source_header_id,
990          wddi.source_line_id,
991          wddi.source_line_set_id,
992          wndi.tp_delivery_number
993   from   wsh_trips_interface wti,
994          wsh_trip_stops_interface wtsi,
995          wsh_del_legs_interface wdli,
996          wsh_del_assgn_interface wdai,
997          wsh_del_details_interface wddi,
998          wsh_new_del_interface wndi
999   where  wti.group_id = p_group_id
1000   and    wti.trip_interface_id = wtsi.trip_interface_id
1001   and    wti.interface_action_code = G_TP_RELEASE_CODE
1002   and    wtsi.stop_interface_id = wdli.pick_up_stop_interface_id
1003   and    wtsi.interface_action_code = G_TP_RELEASE_CODE
1004   and    wdli.delivery_interface_id = wdai.delivery_interface_id
1005   and    wdli.interface_action_code = G_TP_RELEASE_CODE
1006   and    wdai.delivery_interface_id is not null
1007   and    wdai.interface_action_code = G_TP_RELEASE_CODE
1008   and    wddi.delivery_detail_interface_id = wdai.delivery_detail_interface_id
1009   and    wddi.interface_action_code = G_TP_RELEASE_CODE
1010   and    wddi.container_flag = 'N'
1011   and    wddi.requested_quantity <> 0 -- bug 4322654
1012   and    wndi.delivery_interface_id = wdli.delivery_interface_id
1013   and    wndi.interface_action_code = G_TP_RELEASE_CODE
1014   and    NVL(wddi.line_direction, 'O') IN (p_dir1, p_dir2);
1015 
1016   -- outbound: this cursor checks if the delivery detail or other delivery
1017   -- details in its order line are available (not canceled or shipped).
1018   cursor c_check_detail_valid (p_delivery_detail_id in number) is
1019   select a.released_status, a.delivery_detail_id
1020   from wsh_delivery_details a,
1021        wsh_delivery_details b
1022   where b.delivery_detail_id  = p_delivery_detail_id
1023   and b.released_status = 'D'
1024   and a.source_code    = b.source_code
1025   and a.source_line_id = b.source_line_id
1026   and a.delivery_detail_id <> b.delivery_detail_id
1027   and a.released_status NOT IN ('C', 'D')
1028   and rownum = 1
1029   UNION
1030   select released_status, delivery_detail_id
1031   from wsh_delivery_details
1032   where released_status NOT IN ('C', 'D')
1033   and delivery_detail_id = p_delivery_detail_id;
1034 
1035   -- outbound: this cursor checks if the line set can be found
1036   -- for the delivery detail where delivery
1037   -- details in this set are available (not canceled or shipped).
1038   cursor c_check_set_valid (p_delivery_detail_id in number) is
1039   select a.released_status, a.delivery_detail_id
1040   from wsh_delivery_details a,
1041        wsh_delivery_details b
1042   where b.delivery_detail_id  = p_delivery_detail_id
1043   and a.source_code      = b.source_code
1044   and a.source_header_id = b.source_header_id
1045   and a.source_line_set_id IN (
1046            SELECT c.source_line_set_id
1047            FROM WSH_DELIVERY_DETAILS c
1048            WHERE c.source_code = b.source_code
1049            AND   c.source_line_id = b.source_line_id
1050            AND   c.source_line_set_id IS NOT NULL)
1051   and a.delivery_detail_id <> b.delivery_detail_id
1052   and a.released_status NOT IN ('C', 'D')
1053   and rownum = 1;
1054 
1055   -- outbound: this cursor looks for valid details within the source line to resync
1056   -- if delivery detail is deleted.
1057   cursor c_find_line (p_source_code in varchar2,
1058                       p_source_header_id in number,
1059                       p_source_line_id in number) is
1060   select wdd.delivery_detail_id
1061   from wsh_delivery_details wdd
1062   where wdd.source_code = p_source_code
1063   and wdd.source_header_id = p_source_header_id
1064   and wdd.source_line_id = p_source_line_id
1065   and wdd.container_flag = 'N'
1066   and wdd.released_status NOT IN ('C', 'D')
1067   and rownum = 1;
1068 
1069   -- outbound: look for any delivery line to get the source line set
1070   -- when original delivery detail is deleted and source line is not eligible.
1071   cursor c_get_line_set (p_source_code in varchar2,
1072                          p_source_header_id in number,
1073                          p_source_line_id in number) is
1074   select wdd.source_line_set_id
1075   from wsh_delivery_details wdd
1076   where wdd.source_code = p_source_code
1077   and wdd.source_header_id = p_source_header_id
1078   and wdd.source_line_id = p_source_line_id
1079   and wdd.container_flag = 'N'
1080   and wdd.source_line_set_id IS NOT NULL
1081   and rownum = 1;
1082 
1083   -- outbound: this cursor looks for available details within source line set to resync
1084   -- if delivery detail is deleted and line is unavailable
1085   cursor c_find_set (p_source_code in varchar2,
1086                      p_source_header_id in number,
1087                      p_source_line_set_id in number) is
1088   select wdd.delivery_detail_id
1089   from wsh_delivery_details wdd
1090   where wdd.source_code = p_source_code
1091   and wdd.source_header_id = p_source_header_id
1092   and wdd.source_line_set_id = p_source_line_set_id
1093   and wdd.container_flag = 'N'
1094   and wdd.released_status NOT IN ('C', 'D')
1095   and rownum = 1;
1096 
1097 
1098   -- inbound/drop: this cursor checks if the delivery detail or other delivery
1099   -- details in its order line are available (viz., po line shipment has
1100   -- a detail with released status 'X')
1101   cursor c_check_inbound_detail_valid (p_delivery_detail_id in number) is
1102   select a.delivery_detail_id
1103   from wsh_delivery_details a,
1104        wsh_delivery_details b
1105   where b.delivery_detail_id  = p_delivery_detail_id
1106   and a.source_code    = b.source_code
1107   and a.source_line_id = b.source_line_id
1108   and a.po_shipment_line_id = b.po_shipment_line_id
1109   and a.delivery_detail_id <> b.delivery_detail_id
1110   and a.released_status = 'X'
1111   and rownum = 1
1112   UNION
1113   select delivery_detail_id
1114   from wsh_delivery_details
1115   where released_status = 'X'
1116   and delivery_detail_id = p_delivery_detail_id;
1117 
1118 
1119 
1120   -- this cursor checks for existence of interface (dangling) containers
1121   -- Bug 4274651: It also checks for existence of cancelled lines.
1122   cursor c_tp_dangling_conts (p_group_id in number) is
1123   select wddi.delivery_detail_id
1124   from   wsh_trips_interface wti,
1125          wsh_trip_stops_interface wtsi,
1126          wsh_del_legs_interface wdli,
1127          wsh_del_assgn_interface wdai,
1128          wsh_del_details_interface wddi,
1129          wsh_new_del_interface wndi
1130   where  wti.group_id = p_group_id
1131   and    wti.trip_interface_id = wtsi.trip_interface_id
1132   and    wti.interface_action_code = G_TP_RELEASE_CODE
1133   and    wtsi.stop_interface_id = wdli.pick_up_stop_interface_id
1134   and    wtsi.interface_action_code = G_TP_RELEASE_CODE
1135   and    wdli.delivery_interface_id = wdai.delivery_interface_id
1136   and    wdli.interface_action_code = G_TP_RELEASE_CODE
1137   and    wdai.delivery_interface_id is not null
1138   and    wdai.interface_action_code = G_TP_RELEASE_CODE
1139   and    wddi.delivery_detail_interface_id = wdai.delivery_detail_interface_id
1140   and    wddi.interface_action_code = G_TP_RELEASE_CODE
1141   and    ((wddi.container_flag = 'Y') OR
1142          (wddi.container_flag = 'N' and wddi.requested_quantity = 0))
1143   and    wndi.delivery_interface_id = wdli.delivery_interface_id
1144   and    wndi.interface_action_code = G_TP_RELEASE_CODE
1145   and    rownum = 1;
1146 
1147   l_released_status  VARCHAR2(1);
1148   l_dd_id            WSH_UTIL_CORE.ID_TAB_TYPE;
1149   l_split_from_dd_id WSH_UTIL_CORE.ID_TAB_TYPE;
1150   l_dd_interface_id  WSH_UTIL_CORE.ID_TAB_TYPE;
1151   l_source_codes     WSH_UTIL_CORE.COLUMN_TAB_TYPE;
1152   l_source_headers   WSH_UTIL_CORE.ID_TAB_TYPE;
1153   l_source_lines     WSH_UTIL_CORE.ID_TAB_TYPE;
1154   l_source_line_sets WSH_UTIL_CORE.ID_TAB_TYPE;
1155   l_tp_del_numbers   WSH_UTIL_CORE.COLUMN_TAB_TYPE;
1156   l_delivery_detail  NUMBER;
1157   l_count            NUMBER := 0;
1158   l_return_status    VARCHAR2(1);
1159   l_notfound         BOOLEAN;
1160   l_outbound_count   NUMBER;
1161   l_inbound_count    NUMBER;
1162 
1163   WSH_INV_REL_STATUS_ERROR EXCEPTION;
1164 
1165 BEGIN
1166   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
1167   --
1168   IF l_debug_on IS NULL THEN
1169     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
1170   END IF;
1171 
1172   IF l_debug_on THEN
1173     WSH_DEBUG_SV.push(l_module_name);
1174   END IF;
1175   --
1176   --
1177 
1178   IF l_debug_on THEN
1179     WSH_DEBUG_SV.log(l_module_name, 'Bulk collect outbound interface lines in group', x_context.group_id);
1180   END IF;
1181 
1182   OPEN c_tp_released_lines (x_context.group_id, 'O', 'IO');
1183   FETCH  c_tp_released_lines BULK COLLECT INTO
1184          l_dd_interface_id,
1185          l_dd_id,
1186          l_split_from_dd_id,
1187          l_source_codes,
1188          l_source_headers,
1189          l_source_lines,
1190          l_source_line_sets,
1191          l_tp_del_numbers;
1192   CLOSE c_tp_released_lines;
1193 
1194   IF l_debug_on THEN
1195     WSH_DEBUG_SV.log(l_module_name, 'l_dd_interface_id.COUNT (outbound)', l_dd_interface_id.COUNT);
1196   END IF;
1197 
1198   l_outbound_count := l_dd_interface_id.COUNT;
1199 
1200 
1201   IF l_outbound_count > 0 THEN
1202 
1203     -- Check for shipped or deleted lines (stamp interface error and fail if found).
1204 
1205     FOR i in l_dd_interface_id.FIRST .. l_dd_interface_id.LAST LOOP
1206        l_delivery_detail := NVL(l_dd_id(i),
1207                                 l_split_from_dd_id(i));
1208 
1209        IF l_debug_on THEN
1210          WSH_DEBUG_SV.log(l_module_name, 'resync looping: dd_id', l_delivery_detail);
1211        END IF;
1212 
1213        OPEN  c_check_detail_valid(l_delivery_detail);
1214        -- we should use valid delivery_detail_id to refresh source_line_set_id.
1215        FETCH c_check_detail_valid INTO l_released_status, l_delivery_detail;
1216        l_notfound := c_check_detail_valid%NOTFOUND;
1217        CLOSE c_check_detail_valid;
1218 
1219        IF l_notfound THEN
1220           OPEN c_check_set_valid(l_delivery_detail);
1221           FETCH c_check_set_valid INTO l_released_status, l_delivery_detail;
1222           l_notfound := c_check_set_valid%NOTFOUND;
1223           CLOSE c_check_set_valid;
1224        END IF;
1225 
1226        IF l_notfound THEN
1227           -- look for any valid detail within the source line.
1228           OPEN c_find_line(p_source_code      => l_source_codes(i),
1229                            p_source_header_id => l_source_headers(i),
1230                            p_source_line_id   => l_source_lines(i));
1231           FETCH c_find_line INTO l_delivery_detail;
1232           l_notfound := c_find_line%NOTFOUND;
1233           CLOSE c_find_line;
1234        END IF;
1235 
1236        IF l_notfound AND l_source_line_sets(i) IS NULL THEN
1237           -- check if the source line has been split since the snapshot.
1238           OPEN c_get_line_set(p_source_code      => l_source_codes(i),
1239                               p_source_header_id => l_source_headers(i),
1240                               p_source_line_id   => l_source_lines(i));
1241           FETCH c_get_line_set INTO l_source_line_sets(i);
1242           CLOSE c_get_line_set;
1243        END IF;
1244 
1245        IF l_notfound AND l_source_line_sets(i) IS NOT NULL THEN
1246           -- look for any valid detail within the source line set
1247           OPEN c_find_set(p_source_code        => l_source_codes(i),
1248                            p_source_header_id   => l_source_headers(i),
1249                            p_source_line_set_id => l_source_line_sets(i));
1250           FETCH c_find_set INTO l_delivery_detail;
1251           l_notfound := c_find_set%NOTFOUND;
1252           CLOSE c_find_set;
1253        END IF;
1254 
1255        IF l_notfound THEN
1256          -- if still not found, this line cannot be mapped or handled.
1257          stamp_interface_error(p_group_id => x_context.group_id,
1258                               p_entity_table_name => 'WSH_DEL_DETAILS_INTERFACE',
1259                               p_entity_interface_id => l_dd_interface_id(i),
1260                               p_message_name => 'WSH_TP_F_INVALID_DETAIL',
1261                               p_token_1_name => 'DETAIL_ID',
1262                               p_token_1_value => NVL(l_dd_id(i), l_split_from_dd_id(i)),
1263                               p_token_2_name  => 'PLAN_DEL_NUM',
1264                               p_token_2_value => l_tp_del_numbers(i),
1265                               x_errors_tab => x_errors_tab,
1266                               x_return_status => l_return_status);
1267           RAISE WSH_INV_REL_STATUS_ERROR;
1268        END IF;
1269 
1270        IF l_debug_on THEN
1271          WSH_DEBUG_SV.log(l_module_name, 'to be refreshed by dd_id', l_delivery_detail);
1272        END IF;
1273 
1274        l_dd_id(i) := l_delivery_detail;
1275     END LOOP;
1276 
1277     -- Resync released lines' source_line_id, source_line_set_id.
1278     IF l_debug_on THEN
1279       WSH_DEBUG_SV.logmsg(l_module_name, 'Resync lines');
1280     END IF;
1281 
1282 
1283     FORALL i in 1..l_dd_interface_id.count
1284     UPDATE wsh_del_details_interface
1285     SET (source_line_id, source_line_set_id, source_header_id) =
1286         (select wdd.source_line_id, wdd.source_line_set_id, wdd.source_header_id
1287          from wsh_delivery_details wdd
1288          where wdd.delivery_detail_id = l_dd_id(i))
1289     WHERE  delivery_detail_interface_id = l_dd_interface_id(i);
1290 
1291   END IF;
1292 
1293 
1294 
1295   -- inbound/drop: resync the interface records' po_shipment_line_id and po_shipment_line_number
1296   IF l_debug_on THEN
1297     WSH_DEBUG_SV.log(l_module_name, 'Bulk collect inbound/drop interface lines in group', x_context.group_id);
1298   END IF;
1299 
1300   OPEN c_tp_released_lines (x_context.group_id, 'I', 'D');
1301   FETCH  c_tp_released_lines BULK COLLECT INTO
1302          l_dd_interface_id,
1303          l_dd_id,
1304          l_split_from_dd_id,
1305          l_source_codes,
1306          l_source_headers,
1307          l_source_lines,
1308          l_source_line_sets,
1309          l_tp_del_numbers;
1310   CLOSE c_tp_released_lines;
1311 
1312   IF l_debug_on THEN
1313     WSH_DEBUG_SV.log(l_module_name, 'l_dd_interface_id.COUNT (inbound)', l_dd_interface_id.COUNT);
1314   END IF;
1315 
1316   l_inbound_count := l_dd_interface_id.COUNT;
1317 
1318 
1319   IF l_inbound_count > 0 THEN
1320 
1321     FOR i in l_dd_interface_id.FIRST .. l_dd_interface_id.LAST LOOP
1322        l_delivery_detail := NVL(l_dd_id(i), l_split_from_dd_id(i));
1323        OPEN  c_check_inbound_detail_valid(l_delivery_detail);
1324        FETCH c_check_inbound_detail_valid INTO l_delivery_detail;
1325        l_notfound := c_check_inbound_detail_valid%NOTFOUND;
1326        CLOSE c_check_inbound_detail_valid;
1327 
1328        IF l_notfound THEN
1329          -- if not found, this po line shipment cannot be mapped or handled.
1330          stamp_interface_error(p_group_id => x_context.group_id,
1331                               p_entity_table_name => 'WSH_DEL_DETAILS_INTERFACE',
1332                               p_entity_interface_id => l_dd_interface_id(i),
1333                               p_message_name => 'WSH_TP_F_INVALID_DETAIL_IB',  -- new message
1334                               p_token_1_name => 'DETAIL_ID',
1335                               p_token_1_value => NVL(l_dd_id(i), l_split_from_dd_id(i)),
1336                               p_token_2_name  => 'PLAN_DEL_NUM',
1337                               p_token_2_value => l_tp_del_numbers(i),
1338                               x_errors_tab => x_errors_tab,
1339                               x_return_status => l_return_status);
1340 /*
1341  !!! create new message WSH_TP_F_INVALID_DETAIL_IB for inbound: canceled, shipped, purged, or closed.
1342  "The delivery line DETAIL_ID that is required to be assigned to plan delivery PLAN_DEL_NUM has been shipped, purged, closed or canceled."
1343 */
1344           RAISE WSH_INV_REL_STATUS_ERROR;
1345        END IF;
1346     END LOOP;
1347 
1348     FORALL i in 1..l_dd_interface_id.count
1349     UPDATE wsh_del_details_interface
1350     SET (source_line_id, source_line_set_id, source_header_id, po_shipment_line_id, po_shipment_line_number) =
1351         (select wdd.source_line_id, wdd.source_line_set_id, wdd.source_header_id, po_shipment_line_id, po_shipment_line_number
1352          from wsh_delivery_details wdd
1353          where wdd.delivery_detail_id = l_dd_id(i))
1354     WHERE  delivery_detail_interface_id = l_dd_interface_id(i);
1355 
1356   END IF;
1357 
1358   IF (l_outbound_count + l_inbound_count) = 0 THEN
1359     -- at this point, there is no interface line to release.
1360     --
1361     -- we need to check whether we will release
1362     -- deliveries with only dangling containers
1363     -- before we raise this error.
1364     OPEN  c_tp_dangling_conts(x_context.group_id);
1365     FETCH c_tp_dangling_conts INTO l_delivery_detail;
1366     IF c_tp_dangling_conts%NOTFOUND THEN
1367       l_delivery_detail := NULL;
1368     END IF;
1369     CLOSE c_tp_dangling_conts;
1370 
1371     IF l_delivery_detail IS NULL THEN
1372       stamp_interface_error(p_group_id            => x_context.group_id,
1373                             p_entity_table_name   => 'NONE',
1374                             p_entity_interface_id => -1,
1375                             p_message_name        => 'WSH_TP_F_NO_INT_LINES',
1376                             x_errors_tab          => x_errors_tab,
1377                             x_return_status       => l_return_status);
1378       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
1379       IF l_debug_on THEN
1380         WSH_DEBUG_SV.pop(l_module_name);
1381       END IF;
1382       RETURN;
1383     END IF;
1384   END IF;
1385 
1386   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
1387 
1388   --
1389   --
1390   IF l_debug_on THEN
1391      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
1392      WSH_DEBUG_SV.pop(l_module_name);
1393   END IF;
1394 
1395   EXCEPTION
1396     WHEN WSH_INV_REL_STATUS_ERROR THEN
1397       IF c_check_detail_valid%ISOPEN THEN
1398          CLOSE c_check_detail_valid;
1399       END IF;
1400       IF c_check_set_valid%ISOPEN THEN
1401          CLOSE c_check_set_valid;
1402       END IF;
1403       IF c_find_line%ISOPEN THEN
1404          CLOSE c_find_line;
1405       END IF;
1406       IF c_get_line_set%ISOPEN THEN
1407          CLOSE c_get_line_set;
1408       END IF;
1409       IF c_find_set%ISOPEN THEN
1410          CLOSE c_find_set;
1411       END IF;
1412       IF c_check_inbound_detail_valid%ISOPEN THEN
1413          CLOSE c_check_inbound_detail_valid;
1414       END IF;
1415       IF c_tp_dangling_conts%ISOPEN THEN
1416          CLOSE c_tp_dangling_conts;
1417       END IF;
1418       x_return_status  := WSH_UTIL_CORE.G_RET_STS_ERROR;
1419       IF l_debug_on THEN
1420           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:WSH_INV_REL_STATUS_ERROR');
1421       END IF;
1422 
1423     WHEN OTHERS THEN
1424       IF c_check_detail_valid%ISOPEN THEN
1425          CLOSE c_check_detail_valid;
1426       END IF;
1427       IF c_check_set_valid%ISOPEN THEN
1428          CLOSE c_check_set_valid;
1429       END IF;
1430       IF c_find_line%ISOPEN THEN
1431          CLOSE c_find_line;
1432       END IF;
1433       IF c_get_line_set%ISOPEN THEN
1434          CLOSE c_get_line_set;
1435       END IF;
1436       IF c_find_set%ISOPEN THEN
1437          CLOSE c_find_set;
1438       END IF;
1439        IF c_check_inbound_detail_valid%ISOPEN THEN
1440          CLOSE c_check_inbound_detail_valid;
1441       END IF;
1442       IF c_tp_dangling_conts%ISOPEN THEN
1443          CLOSE c_tp_dangling_conts;
1444       END IF;
1445      x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
1446       WSH_UTIL_CORE.DEFAULT_HANDLER(
1447                         'WSH_TP_RELEASE.RESYNC_INTERFACE_TABLES',
1448                         l_module_name);
1449 
1450     IF l_debug_on THEN
1451        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
1452        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
1453     END IF;
1454 
1455 END resync_interface_tables;
1456 
1457 
1458 
1459 
1460 
1461 --
1462 --  Procedure:          generate_lock_candidates
1463 --  Parameters:
1464 --               x_context             context in this session
1465 --               x_plan_details        list of delivery details mapped to interface lines
1466 --               x_track_conts         list of topmost containers to track
1467 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
1468 --               x_plan_legs           list of delivery legs mapped to interface legs
1469 --               x_plan_stops          list of stops mapped to interface stops
1470 --               x_plan_trips          list of trips mapped to interface trips
1471 --               x_plan_trip_moves     list of trip moves mapped to interface trip moves (FTE)
1472 --               x_plan_moves          list of moves mapped to interface moves (FTE)
1473 --               x_used_details        list of delivery details partially used by the plan
1474 --               x_delivery_unassigns  list of delivery lines to unassign from their deliveries
1475 --               x_trip_unassigns      list of deliveries to unassign from their trips
1476 --               x_obsoleted_stops     list of mapped trips' stops that are not mapped in the plan
1477 --               x_obsoleted_trip_moves     list of mapped trips' moves that are not mapped in the plan
1478 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
1479 --               x_return_status       return status
1480 --
1481 --  Description:
1482 --               Maps the interface records to their base records and locks them:
1483 --               1. set up list of interface deliveries, so they can be referenced by lines
1484 --               2. Map and lock delivery details and their topmost LPNs,
1485 --                  using l_current_used_details to track partially and fully used details,
1486 --                        x_track_conts and l_cont_contents to track topmost LPN configurations
1487 --                     * validate grouping attributes (inbound and outbound)
1488 --                     * validate that splitting is allowed
1489 --                     * validate the order line quantity has not increased if it is still ready to release.
1490 --                     * add topmost LPNs to x_delivery_unassigns when they need to be unassigned from current delivery.
1491 --                  partially used details in l_current_used_details will be flushed into x_used_details
1492 --               3. check outcome of mapping lines and deliveries.
1493 --                     * validate that lines have been mapped
1494 --                     * validate deliveries are not empty
1495 --                     * validate LPNs are not broken
1496 --                     * validate partially split details can be unassigned from plan deliveries
1497 --               4. Map and lock trips and stops
1498 --               5. Map and lock legs; reuse existing trips and stops whenever possible
1499 --               6. Map and lock continuous moves (FTE), also matching their segments.
1500 --
1501 --               NOTE: Above tasks have been placed in a specific order in order to build up
1502 --               the mapping.
1503 --
1504 PROCEDURE generate_lock_candidates(
1505            x_context                  IN OUT NOCOPY context_rec_type,
1506            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
1507            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
1508            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
1509            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
1510            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
1511            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
1512            x_plan_trip_moves          IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
1513            x_plan_moves               IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
1514            x_used_details             IN OUT NOCOPY used_details_tab_type,
1515            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
1516            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
1517            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
1518            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
1519            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
1520            x_return_status               OUT NOCOPY VARCHAR2
1521 )
1522 IS
1523   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'GENERATE_LOCK_CANDIDATES';
1524   --
1525   l_debug_on BOOLEAN;
1526 
1527   l_current_source_code  WSH_DELIVERY_DETAILS.SOURCE_CODE%TYPE := NULL;
1528   l_current_line_set_id  NUMBER := NULL;
1529   l_current_line_id      NUMBER := NULL;
1530   l_current_po_shipment_line_id  NUMBER := NULL;
1531   l_current_used_details used_details_tab_type;
1532   l_used_index           NUMBER := 0;
1533   l_new_index            NUMBER := 1;
1534   l_match_found          BOOLEAN;
1535   l_working_quantity     NUMBER := NULL;
1536   l_current_attributes   plan_detail_rec_type;
1537   l_original_quantity    NUMBER;
1538   l_candidates_count     NUMBER;
1539   l_first_line_map       BOOLEAN;
1540   l_deliveries_populated NUMBER := 0;
1541   l_interface_lines_count NUMBER := 0;
1542   l_return_status        VARCHAR2(1);
1543   l_plan_dd_index        NUMBER := 0;
1544   l_plan_del_index       NUMBER := 0;
1545   l_mapped_quantity      NUMBER;
1546   l_map_split_flag       VARCHAR2(1);
1547   l_target_delivery_index NUMBER := 0;
1548   l_target_delivery_interface_id NUMBER := 0;
1549   l_dummy_dd_id          NUMBER;
1550 
1551 -- HW OPMCONV - Removed OPM local variables and added l_item_info
1552   l_item_info            WSH_DELIVERY_DETAILS_INV.mtl_system_items_rec;
1553   l_msg_data             VARCHAR2(4000);
1554   l_msg_count            NUMBER;
1555   l_last_interface_id    NUMBER;
1556   l_stop_index           NUMBER;
1557 
1558   -- variables to track topmost LPNs' configuration
1559   --              and assignments to deliveries.
1560   l_cont_index NUMBER    := 1;
1561   l_new_cont   BOOLEAN   := TRUE;
1562   l_cont_contents        track_cont_content_tab_type;
1563 
1564 
1565   -- variable to track grouping violation for the current interface line
1566   l_group_not_matched    BOOLEAN;
1567 
1568 
1569   -- Candidates are grouped by source line set or source line and
1570   -- ordered by quantity descending.
1571   --
1572   CURSOR c_tp_interface_lines (p_group_id IN NUMBER) IS
1573   SELECT DISTINCT
1574          wddi.delivery_detail_interface_id dd_interface_id,
1575          wddi.delivery_detail_id delivery_detail_id,
1576          wddi.split_from_delivery_detail_id split_from_delivery_detail_id,
1577          wddi.tp_delivery_detail_id,
1578          wddi.source_code,
1579          wddi.source_header_id,
1580          wddi.source_line_id,
1581          wddi.source_line_set_id,
1582          wddi.po_shipment_line_id,
1583          wddi.ship_from_location_id,
1584          wddi.ship_to_location_id,
1585          wddi.requested_quantity     quantity,
1586          wddi.requested_quantity_uom quantity_uom,
1587          wddi.src_requested_quantity,
1588          wddi.src_requested_quantity_uom,
1589          wndi.delivery_id,
1590          wndi.delivery_interface_id
1591   FROM   wsh_trips_interface          wti,
1592          wsh_trip_stops_interface     wtsi,
1593          wsh_del_legs_interface       wdli,
1594          wsh_new_del_interface        wndi,
1595          wsh_del_assgn_interface      wdai,
1596          wsh_del_details_interface    wddi
1597   WHERE  wti.group_id = p_group_id
1598   AND    wti.interface_action_code  = G_TP_RELEASE_CODE
1599   AND    wti.trip_interface_id      = wtsi.trip_interface_id
1600   AND    wtsi.stop_interface_id     = wdli.pick_up_stop_interface_id
1601   AND    wtsi.interface_action_code = G_TP_RELEASE_CODE
1602   AND    wndi.delivery_interface_id = wdli.delivery_interface_id
1603   AND    wdai.delivery_interface_id = wndi.delivery_interface_id
1604   AND    wdai.delivery_interface_id IS NOT NULL
1605   AND    wdai.interface_action_code = G_TP_RELEASE_CODE
1606   AND    wddi.delivery_detail_interface_id = wdai.delivery_detail_interface_id
1607   AND    wddi.interface_action_code = G_TP_RELEASE_CODE
1608   AND    wddi.container_flag = 'N'
1609   AND    wddi.requested_quantity <> 0 -- Bug 4322654
1610   ORDER BY
1611   -- outbound: group by line sets and order lines
1612   -- inbound/drop: group by po line shipments
1613            wddi.source_code,
1614            wddi.source_line_set_id,
1615            DECODE(wddi.source_line_set_id,
1616                   NULL, 0,
1617                   wddi.requested_quantity) DESC,
1618            wddi.source_line_id,
1619            wddi.po_shipment_line_id,
1620            wddi.requested_quantity DESC
1621   ;
1622 
1623 
1624 
1625 
1626   -- c_tp_interface_dels lists the interface deliveries
1627   -- and has outer-join to map the shipping deliveries that
1628   -- were snapshot; the code will later validate whether
1629   -- the existing shipping delivery can be mapped.
1630   --    If the plan specifies an existing TE delivery, use its grouping attribute values (via NVL)
1631   --    for outbound/inbound/drop to validate the candidate lines can be grouped/assigned:
1632   --         ship method components, fob, freight terms, intermediate ship to,
1633   --         customer, shipping control, vendor, and party
1634   --            TP must always pass organization_id, shipment_direction and other columns
1635   --            which are not NVL'd. below.  As of 2/10/2004, TP does not populate these
1636   --            grouping attributes being NVL'd.
1637   CURSOR c_tp_interface_dels (p_group_id IN NUMBER) IS
1638   SELECT DISTINCT
1639          wndi.delivery_id original_delivery_id,
1640          wndi.delivery_interface_id,
1641          wndi.tp_delivery_number,
1642          wndi.tp_plan_name,
1643          wndi.planned_flag,
1644          wnd.delivery_id,  -- if delivery has been deleted, we can create a new record.
1645          wnd.name wsh_name,
1646          wnd.status_code,
1647          nvl(wnd.ignore_for_planning, 'N') ignore_for_planning,
1648          wnd.planned_flag                 wsh_planned_flag,
1649          wnd.initial_pickup_location_id   wsh_initial_pu_loc_id,
1650          wnd.ultimate_dropoff_location_id wsh_ultimate_do_loc_id,
1651          wndi.initial_pickup_location_id,
1652          wndi.initial_pickup_date,
1653          wndi.ultimate_dropoff_location_id,
1654          wndi.ultimate_dropoff_date,
1655          NVL(wnd.ship_method_code, wndi.ship_method_code)     ship_method_code,
1656          NVL(wnd.mode_of_transport, wndi.mode_of_transport)   mode_of_transport,
1657          NVL(wnd.service_level, wndi.service_level)           service_level,
1658          NVL(wnd.freight_terms_code, wndi.freight_terms_code) freight_terms_code,
1659          wndi.name,
1660          wndi.loading_sequence,
1661          wndi.loading_order_flag,
1662          NVL(wnd.fob_code, wndi.fob_code)                     fob_code,
1663          NVL(wnd.fob_location_id, wndi.fob_location_id)       fob_location_id,
1664          wndi.waybill,
1665          wndi.currency_code,
1666          NVL(wnd.party_id, wndi.party_id)                     party_id,
1667          NVL(wnd.shipping_control, wndi.shipping_control)     shipping_control,
1668          NVL(wnd.vendor_id, wndi.vendor_id)                   vendor_id,
1669          wndi.organization_id,
1670          wndi.customer_id,
1671          NVL(wnd.intmed_ship_to_location_id, wndi.intmed_ship_to_location_id)
1672                                                               intmed_ship_to_location_id,
1673          NVL(wnd.carrier_id, wndi.carrier_id)                 carrier_id,
1674          NVL(wnd.shipment_direction, wndi.shipment_direction) shipment_direction,
1675          wndi.additional_shipment_info,
1676          wndi.gross_weight,
1677          wndi.net_weight,
1678          wndi.weight_uom_code,
1679          wndi.volume,
1680          wndi.volume_uom_code,
1681          wndi.pooled_ship_to_location_id,
1682          wndi.dock_code,
1683          wnd.wv_frozen_flag -- WV changes
1684   FROM   wsh_trips_interface          wti,
1685          wsh_trip_stops_interface     wtsi,
1686          wsh_del_legs_interface       wdli,
1687          wsh_new_del_interface        wndi,
1688          wsh_new_deliveries           wnd
1689   WHERE  wti.group_id = p_group_id
1690   AND    wti.interface_action_code  = G_TP_RELEASE_CODE
1691   AND    wti.trip_interface_id      = wtsi.trip_interface_id
1692   AND    wtsi.stop_interface_id     = wdli.pick_up_stop_interface_id
1693   AND    wtsi.interface_action_code = G_TP_RELEASE_CODE
1694   AND    wndi.delivery_interface_id = wdli.delivery_interface_id
1695   AND    wndi.interface_action_code = G_TP_RELEASE_CODE
1696   AND    wnd.delivery_id(+)         = wndi.delivery_id
1697   ORDER BY wndi.delivery_interface_id
1698   ;
1699 
1700   -- c_tp_interface_trips makes a list of interface trips,
1701   -- mapping shipping trips that were snapshot;
1702   -- code will later validate these shipping trips can be mapped.
1703   CURSOR c_tp_interface_trips (p_group_id IN NUMBER) IS
1704   SELECT
1705          wti.trip_interface_id,
1706          wti.planned_flag,
1707          wti.tp_trip_number,
1708          wti.tp_plan_name,
1709          wt.trip_id,
1710          wt.status_code                       wsh_status_code,
1711          wt.planned_flag                      wsh_planned_flag,
1712          nvl(wt.ignore_for_planning, 'N')     wsh_ignore_for_planning,
1713          wt.carrier_id                        wsh_carrier_id,
1714          wt.mode_of_transport                 wsh_mode_of_transport,
1715          wt.service_level                     wsh_service_level,
1716          wt.vehicle_organization_id           wsh_vehicle_org_id,
1717          wt.vehicle_item_id                   wsh_vehicle_item_id,
1718          wt.lane_id                           wsh_lane_id,
1719          wti.name,
1720          wti.vehicle_item_id,
1721          wti.vehicle_organization_id,
1722          wti.vehicle_num_prefix,
1723          wti.vehicle_number,
1724          wti.carrier_id,
1725          wti.ship_method_code,
1726          wti.route_id,
1727          wti.routing_instructions,
1728          wti.service_level,
1729          wti.mode_of_transport,
1730          wti.freight_terms_code,
1731          wti.schedule_id,
1732          wti.consolidation_allowed,
1733          wti.route_lane_id,
1734          wti.lane_id,
1735          wti.seal_code,
1736          wti.shipments_type_flag,
1737          wti.booking_number,
1738          wti.vessel,
1739          wti.voyage_number,
1740          wti.port_of_loading,
1741          wti.port_of_discharge,
1742          wti.carrier_contact_id,
1743          wti.shipper_wait_time,
1744          wti.wait_time_uom,
1745          wti.carrier_response,
1746          wti.operator
1747   FROM   wsh_trips_interface          wti,
1748          wsh_trips                    wt
1749   WHERE  wti.group_id = p_group_id
1750   AND    wti.interface_action_code  = G_TP_RELEASE_CODE
1751   AND    wt.trip_id(+)              = wti.trip_id
1752   ORDER BY wti.trip_interface_id
1753   ;
1754 
1755   -- c_tp_interface_stops makes a list of interface stops.
1756   -- there is no outer-join; the information will be used
1757   -- to match shipping stops selected by c_map_stop
1758   CURSOR c_tp_interface_stops (p_trip_interface_id IN NUMBER) IS
1759   SELECT
1760          wtsi.stop_interface_id,
1761          wtsi.stop_id,
1762          wtsi.tp_stop_id,
1763          wtsi.stop_location_id,
1764          wtsi.stop_sequence_number,
1765          wtsi.planned_arrival_date,
1766          wtsi.planned_departure_date,
1767          wtsi.departure_gross_weight,
1768          wtsi.departure_net_weight,
1769          wtsi.weight_uom_code,
1770          wtsi.departure_volume,
1771          wtsi.volume_uom_code,
1772          wtsi.departure_seal_code,
1773          wtsi.departure_fill_percent,
1774          wtsi.wkend_layover_stops,
1775          wtsi.wkday_layover_stops,
1776          wtsi.shipments_type_flag
1777   FROM   wsh_trip_stops_interface     wtsi
1778   WHERE  wtsi.trip_interface_id      = p_trip_interface_id
1779   AND    wtsi.interface_action_code  = G_TP_RELEASE_CODE
1780   ORDER BY wtsi.stop_sequence_number
1781   ;
1782 
1783   -- c_map_stop finds a shipping stop that matches the
1784   -- essential stop attributes of the interface stop.
1785   --    select the physical stop or the unlinked dummy stop
1786   --    at the physical stop location with same dates.
1787   CURSOR c_map_stop(p_trip_id                IN NUMBER,
1788                     p_stop_location_id       IN NUMBER,
1789                     p_planned_arrival_date   IN DATE,
1790                     p_planned_departure_date IN DATE)  IS
1791   SELECT wts.stop_id,
1792          wv_frozen_flag  -- WV changes
1793   FROM   wsh_trip_stops    wts
1794   WHERE  wts.trip_id                = p_trip_id
1795   AND    wts.stop_location_id       = p_stop_location_id
1796   AND    wts.planned_arrival_date   = p_planned_arrival_date
1797   AND    wts.planned_departure_date = p_planned_departure_date
1798   AND    wts.status_code = 'OP'
1799   UNION
1800   SELECT wts.stop_id, wv_frozen_flag
1801   FROM   wsh_trip_stops   wts
1802   WHERE  wts.trip_id                = p_trip_id
1803   AND    wts.physical_location_id   = p_stop_location_id
1804   AND    wts.physical_stop_id       IS NULL
1805   AND    wts.planned_arrival_date   = p_planned_arrival_date
1806   AND    wts.planned_departure_date = p_planned_departure_date
1807   AND    wts.status_code = 'OP'
1808   ;
1809 
1810   l_map_stop_rec  c_map_stop%ROWTYPE;
1811 
1812   -- c_snapshot_legs generates a list of delivery legs
1813   -- of the delivery being mapped.
1814   CURSOR c_snapshot_legs(p_delivery_id IN NUMBER) IS
1815   SELECT wdl.delivery_leg_id           delivery_leg_id,
1816          wt.trip_id                    trip_id,
1817          wt.planned_flag               trip_planned_flag,
1818          wt.carrier_id                 carrier_id,
1819          wt.mode_of_transport          mode_of_transport,
1820          wt.service_level              service_level,
1821          wt.vehicle_organization_id    vehicle_org_id,
1822          wt.vehicle_item_id            vehicle_item_id,
1823          wt.lane_id                    lane_id,
1824          wts_pu.stop_id                pu_stop_id,
1825          wts_pu.stop_location_id       pu_stop_location_id,
1826          wts_pu.planned_arrival_date   pu_planned_arrival_date,
1827          wts_pu.planned_departure_date pu_planned_departure_date,
1828          wts_pu.wv_frozen_flag         pu_wv_flag,
1829          NVL(wts_do.physical_stop_id, wts_do.stop_id) do_stop_id,
1830          NVL(wts_do.physical_location_id, wts_do.stop_location_id)
1831                                        do_stop_location_id,
1832          wts_do.physical_stop_id       do_physical_stop_id,
1833          wts_do.planned_arrival_date   do_planned_arrival_date,
1834          wts_do.planned_departure_date do_planned_departure_date,
1835          wts_do.wv_frozen_flag         do_wv_flag
1836   FROM   wsh_delivery_legs wdl,
1837          wsh_trip_stops wts_pu,
1838          wsh_trip_stops wts_do,
1839          wsh_trips      wt
1840   WHERE  wdl.delivery_id = p_delivery_id
1841   AND    wts_pu.stop_id  = wdl.pick_up_stop_id
1842   AND    wts_do.stop_id  = wdl.drop_off_stop_id
1843   AND    wt.trip_id      = wts_pu.trip_id
1844   ORDER BY wdl.delivery_leg_id;
1845 
1846   l_snapshot_leg_ids          wsh_util_core.id_tab_type;
1847   l_snapshot_trip_ids         wsh_util_core.id_tab_type;
1848   l_snapshot_trip_plan_flags  planned_flag_tab_type;
1849   l_snapshot_pu_stop_ids      wsh_util_core.id_tab_type;
1850   l_snapshot_pu_seq_nums      wsh_util_core.id_tab_type;
1851   l_snapshot_pu_loc_ids       wsh_util_core.id_tab_type;
1852   l_snapshot_pu_arrive_dates  date_tab_type;
1853   l_snapshot_pu_depart_dates  date_tab_type;
1854   l_snapshot_pu_wv_flag       planned_flag_tab_type;
1855   l_snapshot_do_stop_ids      wsh_util_core.id_tab_type;
1856   l_snapshot_do_phys_stop_ids wsh_util_core.id_tab_type;
1857   l_snapshot_do_seq_nums      wsh_util_core.id_tab_type;
1858   l_snapshot_do_loc_ids       wsh_util_core.id_tab_type;
1859   l_snapshot_do_arrive_dates  date_tab_type;
1860   l_snapshot_do_depart_dates  date_tab_type;
1861   l_snapshot_do_wv_flag       planned_flag_tab_type;
1862   l_snapshot_carrier_ids      wsh_util_core.id_tab_type;
1863   l_snapshot_modes            wsh_util_core.column_tab_type;
1864   l_snapshot_service_levels   wsh_util_core.column_tab_type;
1865   l_snapshot_veh_org_ids      wsh_util_core.id_tab_type;
1866   l_snapshot_veh_item_ids     wsh_util_core.id_tab_type;
1867   l_snapshot_lane_ids         wsh_util_core.id_tab_type;
1868 
1869   -- c_physical_stop looks up the physical stop's dates for matching plan
1870   CURSOR c_physical_stop(p_stop_id  IN NUMBER)  IS
1871   SELECT planned_arrival_date,
1872          planned_departure_date,
1873          wv_frozen_flag
1874   FROM   wsh_trip_stops    wts
1875   WHERE  wts.stop_id = p_stop_id;
1876 
1877 
1878   -- c_tp_interface_legs makes a list of interface legs.
1879   CURSOR c_tp_interface_legs (p_delivery_interface_id IN NUMBER) IS
1880   SELECT wdli.delivery_leg_interface_id,
1881          wdli.delivery_leg_id,
1882          wdli.pick_up_stop_interface_id,
1883          wdli.drop_off_stop_interface_id,
1884          wtsi_pu.trip_interface_id
1885   FROM   wsh_del_legs_interface       wdli,
1886          wsh_trip_stops_interface     wtsi_pu
1887   WHERE  wdli.delivery_interface_id    = p_delivery_interface_id
1888   AND    wdli.interface_action_code    = G_TP_RELEASE_CODE
1889   AND    wtsi_pu.stop_interface_id     = wdli.pick_up_stop_interface_id
1890   AND    wtsi_pu.interface_action_code = G_TP_RELEASE_CODE
1891   ORDER BY wdli.sequence_number, wdli.delivery_leg_interface_id
1892   ;
1893 
1894 
1895   -- c_being_staged is used to verify whether the order line
1896   -- is ready to release (status 'Backordered' implies the
1897   -- order line had been staged sometime).
1898   -- This is to meet the requirement of failing the plan
1899   -- if the order line's quantity has been increased and
1900   -- it is not yet staged.
1901   CURSOR c_being_staged(p_source_code IN VARCHAR2,
1902                   p_source_line_id IN VARCHAR2) IS
1903     SELECT wdd.delivery_detail_id
1904     FROM   WSH_DELIVERY_DETAILS wdd
1905     WHERE  wdd.source_code    = p_source_code
1906     AND    wdd.source_line_id = p_source_line_id
1907     AND    wdd.released_status IN ('B', 'S', 'Y', 'C')
1908     AND    rownum = 1;
1909 
1910   -- c_location_change is used to validate that
1911   -- the order line's ship-from and ship-to locations have
1912   -- not been changed in the event that no candidates have
1913   -- been found for the interface line; otherwise, the plan will fail.
1914   CURSOR c_location_change(p_source_code           IN VARCHAR2,
1915                            p_source_header_id      IN VARCHAR2,
1916                            p_source_line_id        IN NUMBER,
1917                            p_ship_from_location_id IN NUMBER,
1918                            p_ship_to_location_id   IN NUMBER) IS
1919     SELECT wdd.delivery_detail_id
1920     FROM   wsh_delivery_details wdd
1921     WHERE
1922          (
1923               wdd.source_code      = p_source_code
1924           AND wdd.source_header_id = p_source_header_id
1925           AND wdd.source_line_id   = p_source_line_id
1926          )
1927          AND (   wdd.ship_from_location_id <> p_ship_from_location_id
1928               OR wdd.ship_to_location_id <> p_ship_to_location_id)
1929          AND wdd.released_status IN ('N', 'R', 'B', 'S', 'Y', 'X')
1930          AND nvl(wdd.ignore_for_planning, 'N') = 'N'
1931          AND rownum = 1;
1932 
1933   -- c_ignored is to validate whether the order line has become
1934   -- ignored for planning, in the event that no candidates
1935   -- have been found for the interface line; if so, the plan will fail.
1936   CURSOR c_ignored(p_source_code           IN VARCHAR2,
1937                    p_source_header_id      IN VARCHAR2,
1938                    p_source_line_id        IN NUMBER) IS
1939     SELECT wdd.delivery_detail_id
1940     FROM   wsh_delivery_details wdd
1941     WHERE
1942          (
1943               wdd.source_code      = p_source_code
1944           AND wdd.source_header_id = p_source_header_id
1945           AND wdd.source_line_id   = p_source_line_id
1946          )
1947          AND wdd.released_status IN ('N', 'R', 'B', 'S', 'Y', 'X')
1948          AND wdd.ignore_for_planning = 'Y'
1949          AND NOT EXISTS (SELECT 'planning'
1950                          FROM   wsh_delivery_details wdd2
1951                          WHERE  wdd2.source_code      = p_source_code
1952                          AND    wdd2.source_header_id = p_source_header_id
1953                          AND    wdd2.source_line_id   = p_source_line_id
1954                          AND    nvl(wdd2.ignore_for_planning, 'N') = 'N')
1955          AND rownum = 1;
1956 
1957   l_phys_ult_do_mapped BOOLEAN;  -- track whether internal customer location mapping is unchanged
1958   l_last_do_stop_index NUMBER;   -- track each delivery's last drop off stop
1959 
1960 
1961 BEGIN
1962   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
1963   --
1964   IF l_debug_on IS NULL THEN
1965     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
1966   END IF;
1967 
1968   IF l_debug_on THEN
1969     WSH_DEBUG_SV.push(l_module_name);
1970   END IF;
1971   --
1972   --
1973 
1974   -- 1. set up list of interface deliveries, so they can be referenced by lines
1975 
1976   -- Bug 3555487 initialize message stack for each major action point.
1977   FND_MSG_PUB.initialize;
1978 
1979   l_new_index := 0;
1980   FOR idel IN c_tp_interface_dels(p_group_id => x_context.group_id)  LOOP
1981 
1982     IF l_debug_on THEN
1983       WSH_DEBUG_SV.log(l_module_name,
1984                        'idel looping: delivery_interface_id',
1985                        idel.delivery_interface_id);
1986     END IF;
1987 
1988     l_new_index := l_new_index + 1;
1989     x_plan_deliveries(l_new_index).del_interface_id := idel.delivery_interface_id;
1990     x_plan_deliveries(l_new_index).delivery_id      := idel.delivery_id;
1991     x_plan_deliveries(l_new_index).tp_delivery_number  := idel.tp_delivery_number;
1992     x_plan_deliveries(l_new_index).tp_plan_name     := idel.tp_plan_name;
1993     x_plan_deliveries(l_new_index).planned_flag     := idel.planned_flag;
1994     x_plan_deliveries(l_new_index).wsh_planned_flag := idel.wsh_planned_flag;
1995     x_plan_deliveries(l_new_index).initial_pickup_location_id  := idel.initial_pickup_location_id;
1996     x_plan_deliveries(l_new_index).ultimate_dropoff_location_id := idel.ultimate_dropoff_location_id;
1997     x_plan_deliveries(l_new_index).initial_pickup_date        := idel.initial_pickup_date;
1998     x_plan_deliveries(l_new_index).ultimate_dropoff_date      := idel.ultimate_dropoff_date;
1999     x_plan_deliveries(l_new_index).ship_method_code           := idel.ship_method_code;
2000     x_plan_deliveries(l_new_index).mode_of_transport          := idel.mode_of_transport;
2001     x_plan_deliveries(l_new_index).service_level              := idel.service_level;
2002     x_plan_deliveries(l_new_index).freight_terms_code         := idel.freight_terms_code;
2003     x_plan_deliveries(l_new_index).name                       := idel.name;
2004     x_plan_deliveries(l_new_index).loading_sequence           := idel.loading_sequence;
2005     x_plan_deliveries(l_new_index).loading_order_flag         := idel.loading_order_flag;
2006     x_plan_deliveries(l_new_index).fob_code                   := idel.fob_code;
2007     x_plan_deliveries(l_new_index).fob_location_id            := idel.fob_location_id;
2008     x_plan_deliveries(l_new_index).waybill                    := idel.waybill;
2009     x_plan_deliveries(l_new_index).currency_code              := idel.currency_code;
2010     x_plan_deliveries(l_new_index).party_id                   := idel.party_id;
2011     x_plan_deliveries(l_new_index).shipping_control           := idel.shipping_control;
2012     x_plan_deliveries(l_new_index).vendor_id                  := idel.vendor_id;
2013     x_plan_deliveries(l_new_index).organization_id            := idel.organization_id;
2014     x_plan_deliveries(l_new_index).customer_id                := idel.customer_id;
2015     x_plan_deliveries(l_new_index).intmed_ship_to_location_id := idel.intmed_ship_to_location_id;
2016     x_plan_deliveries(l_new_index).carrier_id                 := idel.carrier_id;
2017     x_plan_deliveries(l_new_index).shipment_direction         := idel.shipment_direction;
2018     x_plan_deliveries(l_new_index).additional_shipment_info   := idel.additional_shipment_info;
2019     x_plan_deliveries(l_new_index).gross_weight               := idel.gross_weight;
2020     x_plan_deliveries(l_new_index).net_weight                 := idel.net_weight;
2021     x_plan_deliveries(l_new_index).weight_uom_code            := idel.weight_uom_code;
2022     x_plan_deliveries(l_new_index).volume                     := idel.volume;
2023     x_plan_deliveries(l_new_index).volume_uom_code            := idel.volume_uom_code;
2024     x_plan_deliveries(l_new_index).pooled_ship_to_location_id := idel.pooled_ship_to_location_id;
2025     x_plan_deliveries(l_new_index).dock_code                  := idel.dock_code;
2026     x_plan_deliveries(l_new_index).ilines_count               := 0;
2027     x_plan_deliveries(l_new_index).lines_count                := 0;
2028     x_plan_deliveries(l_new_index).s_lines_count              := 0;
2029     x_plan_deliveries(l_new_index).dangling_conts_count       := 0;
2030     x_plan_deliveries(l_new_index).assign_details_count       := 0;
2031     x_plan_deliveries(l_new_index).wv_frozen_flag             := idel.wv_frozen_flag;   -- WV changes
2032 
2033     x_plan_deliveries(l_new_index).wms_org_flag :=
2034             wsh_util_validate.Check_Wms_Org(idel.organization_id);
2035     IF x_plan_deliveries(l_new_index).wms_org_flag = 'Y' THEN
2036       x_context.wms_in_group := TRUE;
2037     END IF;
2038     x_plan_deliveries(l_new_index).leg_base_index := NULL;
2039     IF l_debug_on THEN
2040           WSH_DEBUG_SV.log(l_module_name, 'delivery_wv_frozen_flag', idel.wv_frozen_flag);
2041           WSH_DEBUG_SV.log(l_module_name, 'delivery_id', idel.delivery_id);
2042     END IF;
2043 
2044     WSH_LOCATIONS_PKG.convert_internal_cust_location(
2045             p_internal_cust_location_id => x_plan_deliveries(l_new_index).ultimate_dropoff_location_id,
2046             x_internal_org_location_id  => x_plan_deliveries(l_new_index).physical_ultimate_do_loc_id,
2047             x_return_status             => l_return_status);
2048     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
2049         stamp_interface_error(
2050           p_group_id            => x_context.group_id,
2051           p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2052           p_entity_interface_id => idel.delivery_interface_id,
2053           p_message_name        => 'WSH_TP_F_CONVERT_LOC', --!!! new message
2054           p_token_1_name        => 'PLAN_DEL_NUM',
2055           p_token_1_value       => idel.tp_delivery_number,
2056           x_errors_tab          => x_errors_tab,
2057           x_return_status       => l_return_status);
2058        x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2059        IF l_debug_on THEN
2060          WSH_DEBUG_SV.pop(l_module_name);
2061        END IF;
2062        RETURN;
2063     END IF;
2064 
2065 
2066     -- if the existing delivery is firmed, key attributes need to match.
2067     IF      idel.wsh_planned_flag IN ('Y', 'F')
2068        AND  (   idel.wsh_initial_pu_loc_id <> idel.initial_pickup_location_id
2069              OR idel.wsh_ultimate_do_loc_id <> idel.ultimate_dropoff_location_id) THEN
2070 
2071         stamp_interface_error(
2072           p_group_id            => x_context.group_id,
2073           p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2074           p_entity_interface_id => idel.delivery_interface_id,
2075           p_message_name        => 'WSH_TP_F_DEL_DIFF_ATTR',
2076           p_token_1_name        => 'DELIVERY_NAME',
2077           p_token_1_value       => idel.wsh_name,
2078           x_errors_tab          => x_errors_tab,
2079           x_return_status       => l_return_status);
2080         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2081         IF l_debug_on THEN
2082           WSH_DEBUG_SV.log(l_module_name, 'delivery key attributes do not match', x_return_status);
2083           WSH_DEBUG_SV.log(l_module_name, 'delivery_interface_id', idel.delivery_interface_id);
2084           WSH_DEBUG_SV.log(l_module_name, 'delivery_id', idel.delivery_id);
2085           WSH_DEBUG_SV.log(l_module_name, 'wsh_planned_flag', idel.wsh_planned_flag);
2086           WSH_DEBUG_SV.log(l_module_name, 'wsh_initial_pu_loc_id', idel.wsh_initial_pu_loc_id);
2087           WSH_DEBUG_SV.log(l_module_name, 'wsh_initial_do_loc_id', idel.wsh_ultimate_do_loc_id);
2088           WSH_DEBUG_SV.log(l_module_name, 'initial_pickup_location_id', idel.initial_pickup_location_id);
2089           WSH_DEBUG_SV.log(l_module_name, 'ultimate_dropoff_location_id', idel.ultimate_dropoff_location_id);
2090           WSH_DEBUG_SV.pop(l_module_name);
2091         END IF;
2092         RETURN;
2093     END IF;
2094 
2095     IF idel.delivery_id IS NOT NULL THEN
2096       IF idel.status_code NOT IN ('OP', 'SA') THEN
2097         stamp_interface_error(
2098           p_group_id            => x_context.group_id,
2099           p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2100           p_entity_interface_id => idel.delivery_interface_id,
2101           p_message_name        => 'WSH_TP_F_DEL_NOT_OPEN',
2102           p_token_1_name        => 'DELIVERY_NAME',
2103           p_token_1_value       => idel.wsh_name,
2104           x_errors_tab          => x_errors_tab,
2105           x_return_status       => l_return_status);
2106         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2107         IF l_debug_on THEN
2108           WSH_DEBUG_SV.pop(l_module_name);
2109         END IF;
2110         RETURN;
2111       END IF;
2112 
2113       IF idel.ignore_for_planning = 'Y' THEN
2114         stamp_interface_error(
2115           p_group_id            => x_context.group_id,
2116           p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2117           p_entity_interface_id => idel.delivery_interface_id,
2118           p_message_name        => 'WSH_TP_F_DEL_IGNORED',
2119           p_token_1_name        => 'DELIVERY_NAME',
2120           p_token_1_value       => idel.wsh_name,
2121           p_token_2_name        => 'PLAN_TRIP_NUM',
2122           p_token_2_value       =>  get_plan_trip_num(x_context),
2123           p_token_3_name        => 'PLAN_DEL_NUM',
2124           p_token_3_value       => idel.tp_delivery_number,
2125           x_errors_tab          => x_errors_tab,
2126           x_return_status       => l_return_status);
2127         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2128         IF l_debug_on THEN
2129           WSH_DEBUG_SV.pop(l_module_name);
2130         END IF;
2131         RETURN;
2132       END IF;
2133 
2134       BEGIN
2135         wsh_new_deliveries_pvt.lock_dlvy_no_compare(
2136                p_delivery_id => idel.delivery_id);
2137       EXCEPTION
2138         WHEN OTHERS THEN
2139                stamp_interface_error(
2140                    p_group_id            => x_context.group_id,
2141                    p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2142                    p_entity_interface_id => idel.delivery_interface_id,
2143                    p_message_name        => 'WSH_TP_F_NO_LOCK_DEL',
2144                    p_token_1_name        => 'DELIVERY_NAME',
2145                    p_token_1_value       => idel.wsh_name,
2146                    x_errors_tab          => x_errors_tab,
2147                    x_return_status       => l_return_status);
2148                x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2149                IF l_debug_on THEN
2150                  WSH_DEBUG_SV.pop(l_module_name);
2151                END IF;
2152                RETURN;
2153       END;
2154 
2155       BEGIN
2156         wsh_delivery_details_pkg.lock_detail_no_compare(
2157                p_delivery_id => idel.delivery_id);
2158       EXCEPTION
2159         WHEN OTHERS THEN
2160                stamp_interface_error(
2161                    p_group_id            => x_context.group_id,
2162                    p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2163                    p_entity_interface_id => idel.delivery_interface_id,
2164                    p_message_name        => 'WSH_TP_F_NO_LOCK_DEL_CONTENTS',
2165                    p_token_1_name        => 'DELIVERY_NAME',
2166                    p_token_1_value       => idel.wsh_name,
2167                    x_errors_tab          => x_errors_tab,
2168                    x_return_status       => l_return_status);
2169                x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2170                IF l_debug_on THEN
2171                  WSH_DEBUG_SV.pop(l_module_name);
2172                END IF;
2173                RETURN;
2174       END;
2175     END IF;
2176 
2177   END LOOP;
2178 
2179   IF l_debug_on THEN
2180     WSH_DEBUG_SV.log(l_module_name, 'x_plan_deliveries.COUNT', x_plan_deliveries.COUNT);
2181   END IF;
2182 
2183   IF x_plan_deliveries.COUNT = 0 THEN
2184       stamp_interface_error(
2185            p_group_id            => x_context.group_id,
2186            p_entity_table_name   => 'NONE',
2187            p_entity_interface_id => -1,
2188            p_message_name        => 'WSH_TP_F_NO_DELS',
2189            p_token_1_name        => 'PLAN_TRIP_NUM',
2190            p_token_1_value       => get_plan_trip_num(x_context),
2191            x_errors_tab          => x_errors_tab,
2192            x_return_status       => l_return_status);
2193       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2194       IF l_debug_on THEN
2195         WSH_DEBUG_SV.pop(l_module_name);
2196       END IF;
2197       RETURN;
2198   END IF;
2199 
2200 
2201   -- 2. Map and lock delivery details and their topmost LPNs
2202 
2203   -- Bug 3555487 initialize message stack for each major action point.
2204   FND_MSG_PUB.initialize;
2205 
2206   FOR iline IN c_tp_interface_lines(p_group_id => x_context.group_id)  LOOP
2207 
2208     IF l_debug_on THEN
2209       WSH_DEBUG_SV.log(l_module_name,
2210                        'iline looping: dd_interface_id',
2211                        iline.dd_interface_id);
2212     END IF;
2213 
2214     l_original_quantity                      := iline.quantity;
2215     l_candidates_count                       := 0;
2216     l_current_attributes.mapped_quantity     := iline.quantity;
2217     l_current_attributes.mapped_quantity_uom := iline.quantity_uom;
2218     l_group_not_matched                      := FALSE;
2219     l_interface_lines_count                  := l_interface_lines_count + 1;
2220 
2221     -- find the mapped delivery index if it has changed.
2222     IF l_target_delivery_interface_id <> iline.delivery_interface_id THEN
2223 
2224       l_target_delivery_interface_id := iline.delivery_interface_id;
2225       l_new_index := x_plan_deliveries.FIRST;
2226 
2227       WHILE l_new_index IS NOT NULL LOOP
2228         IF x_plan_deliveries(l_new_index).del_interface_id = iline.delivery_interface_id THEN
2229           l_target_delivery_index := l_new_index;
2230           EXIT;
2231         END IF;
2232         l_new_index := x_plan_deliveries.NEXT(l_new_index);
2233       END LOOP;
2234 
2235     END IF;
2236 
2237     -- count the interface lines mapped to this delivery.
2238     x_plan_deliveries(l_target_delivery_index).ilines_count :=
2239             x_plan_deliveries(l_target_delivery_index).ilines_count  + 1;
2240 
2241     -- We need to check grouping depending on whether this is the first line
2242     -- being mapped for the interface delivery.
2243     -- Otherwise, the first interface line being successfully mapped will set the delivery's
2244     -- grouping attributes.
2245     l_first_line_map := (x_plan_deliveries(l_target_delivery_index).lines_count = 0);
2246 
2247     -- The line should be compatible with this delivery's shipping attributes when enforced;
2248     -- some of the grouping attributes are passed from the interface delivery and must be validated,
2249     -- so we always do these assignments.
2250     l_current_attributes.line_direction     := x_plan_deliveries(l_target_delivery_index).shipment_direction;
2251     l_current_attributes.ship_from_location_id := x_plan_deliveries(l_target_delivery_index).initial_pickup_location_id;
2252     l_current_attributes.ship_to_location_id := x_plan_deliveries(l_target_delivery_index).ultimate_dropoff_location_id;
2253     l_current_attributes.organization_id    := x_plan_deliveries(l_target_delivery_index).organization_id;
2254     l_current_attributes.customer_id        := x_plan_deliveries(l_target_delivery_index).customer_id;
2255     l_current_attributes.fob_code           := x_plan_deliveries(l_target_delivery_index).fob_code;
2256     l_current_attributes.freight_terms_code := x_plan_deliveries(l_target_delivery_index).freight_terms_code;
2257     l_current_attributes.intmed_ship_to_location_id := x_plan_deliveries(l_target_delivery_index).intmed_ship_to_location_id;
2258     l_current_attributes.ship_method_code   := x_plan_deliveries(l_target_delivery_index).ship_method_code;
2259     l_current_attributes.mode_of_transport  := x_plan_deliveries(l_target_delivery_index).mode_of_transport;
2260     l_current_attributes.service_level      := x_plan_deliveries(l_target_delivery_index).service_level;
2261     l_current_attributes.carrier_id         := x_plan_deliveries(l_target_delivery_index).carrier_id;
2262     l_current_attributes.shipping_control   := x_plan_deliveries(l_target_delivery_index).shipping_control;
2263     l_current_attributes.vendor_id          := x_plan_deliveries(l_target_delivery_index).vendor_id;
2264     l_current_attributes.party_id           := x_plan_deliveries(l_target_delivery_index).party_id;
2265 
2266     -- if new source line set or source line,
2267     --  flush temp list to used list (clear fully used and keep partially used)
2268     --    inbound/drop: track po_shipment_line_id change as well
2269     --      assumption: only inbound/drop will have po_shipment_line_id populated.
2270     IF       (NVL(l_current_source_code, 'NEW') <> iline.source_code)
2271           OR (l_current_line_set_id IS NULL AND l_current_line_id IS NULL)
2272           OR (iline.source_line_set_id <> l_current_line_set_id)
2273           OR (iline.source_line_set_id IS NULL     AND l_current_line_set_id IS NOT NULL)
2274           OR (iline.source_line_set_id IS NOT NULL AND l_current_line_set_id IS NULL)
2275           OR (iline.source_line_id <> l_current_line_id)
2276           OR (NVL(iline.po_shipment_line_id, -1) <> NVL(l_current_po_shipment_line_id, -1))
2277     THEN
2278 
2279        IF l_debug_on THEN
2280           WSH_DEBUG_SV.log(l_module_name, 'flushing iline.source_code', iline.source_code);
2281           WSH_DEBUG_SV.log(l_module_name, 'iline.source_line_set_id', iline.source_line_set_id);
2282           WSH_DEBUG_SV.log(l_module_name, 'iline.source_line_id', iline.source_line_id);
2283           WSH_DEBUG_SV.log(l_module_name, 'iline.po_shipment_line_id', iline.po_shipment_line_id);
2284           WSH_DEBUG_SV.log(l_module_name, 'l_current_source_code', l_current_source_code);
2285           WSH_DEBUG_SV.log(l_module_name, 'l_current_line_set_id', l_current_line_set_id);
2286           WSH_DEBUG_SV.log(l_module_name, 'l_current_line_id', l_current_line_id);
2287           WSH_DEBUG_SV.log(l_module_name, 'l_current_po_shipment_line_id', l_current_po_shipment_line_id);
2288        END IF;
2289 
2290        l_current_source_code         := iline.source_code;
2291        l_current_po_shipment_line_id := iline.po_shipment_line_id;
2292 
2293        IF iline.source_line_set_id IS NOT NULL THEN
2294          l_current_line_set_id := iline.source_line_set_id;
2295          l_current_line_id     := NULL;
2296        ELSE
2297          l_current_line_set_id := NULL;
2298          l_current_line_id     := iline.source_line_id;
2299        END IF;
2300 
2301 
2302        IF (l_current_used_details.COUNT > 0) THEN
2303          flush_used_details(
2304              x_context              => x_context,
2305              x_current_used_details => l_current_used_details,
2306              x_used_details         => x_used_details,
2307              x_errors_tab           => x_errors_tab,
2308              x_return_status        => l_return_status);
2309          IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
2310            x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2311            IF l_debug_on THEN
2312              WSH_DEBUG_SV.pop(l_module_name);
2313            END IF;
2314            RETURN;
2315          END IF;
2316        END IF;
2317 
2318     END IF;
2319 
2320     IF l_debug_on THEN
2321       WSH_DEBUG_SV.log(l_module_name, 'iline.source_code', iline.source_code);
2322       WSH_DEBUG_SV.log(l_module_name, 'iline.delivery_detail_id', iline.delivery_detail_id);
2323       WSH_DEBUG_SV.log(l_module_name, 'iline.split_from_delivery_detail_id', iline.split_from_delivery_detail_id);
2324       WSH_DEBUG_SV.log(l_module_name, 'l_target_delivery_index', l_target_delivery_index);
2325       WSH_DEBUG_SV.log(l_module_name, 'x_plan_deliveries->delivery_id', x_plan_deliveries(l_target_delivery_index).delivery_id);
2326       WSH_DEBUG_SV.log(l_module_name, 'iline.source_code', iline.source_code);
2327       WSH_DEBUG_SV.log(l_module_name, 'iline.source_header_id', iline.source_header_id);
2328       WSH_DEBUG_SV.log(l_module_name, 'iline.source_line_id', iline.source_line_id);
2329       WSH_DEBUG_SV.log(l_module_name, 'iline.source_line_set_id', iline.source_line_set_id);
2330       WSH_DEBUG_SV.log(l_module_name, 'iline.ship_from_location_id', iline.ship_from_location_id);
2331       WSH_DEBUG_SV.log(l_module_name, 'iline.ship_to_location_id', iline.ship_to_location_id);
2332       WSH_DEBUG_SV.log(l_module_name, 'iline.src_requested_quantity', iline.src_requested_quantity);
2333       WSH_DEBUG_SV.log(l_module_name, 'iline.src_requested_quantity_uom', iline.src_requested_quantity_uom);
2334       WSH_DEBUG_SV.log(l_module_name, 'iline.quantity', iline.quantity);
2335       WSH_DEBUG_SV.log(l_module_name, 'iline.quantity_uom', iline.quantity_uom);
2336     END IF;
2337 
2338 <<candidates_loop>>
2339     FOR candidate IN c_map_lines (
2340         x_plan_delivery_detail_id    => NVL(iline.delivery_detail_id,
2341                                           iline.split_from_delivery_detail_id),
2342         x_plan_delivery_id           => x_plan_deliveries(l_target_delivery_index).delivery_id,
2343         x_plan_source_code           => iline.source_code,
2344         x_plan_source_header_id      => iline.source_header_id,
2345         x_plan_source_line_id        => iline.source_line_id,
2346         x_plan_source_line_set_id    => iline.source_line_set_id,
2347         x_plan_po_shipment_line_id   => iline.po_shipment_line_id,
2348         x_plan_ship_from_location_id => iline.ship_from_location_id,
2349         x_plan_ship_to_location_id   => iline.ship_to_location_id) LOOP
2350 
2351       l_candidates_count := l_candidates_count + 1;
2352 
2353       IF l_debug_on THEN
2354         WSH_DEBUG_SV.log(l_module_name,
2355                          'candidate looping: delivery_detail_id',
2356                          candidate.delivery_detail_id);
2357 
2358         WSH_DEBUG_SV.log(l_module_name, 'candidate.choice', candidate.choice);
2359         WSH_DEBUG_SV.log(l_module_name, 'candidate.scope', candidate.scope);
2360         WSH_DEBUG_SV.log(l_module_name, 'candidate.delivery_id', candidate.delivery_id);
2361         WSH_DEBUG_SV.log(l_module_name, 'candidate.source_line_id', candidate.source_line_id);
2362         WSH_DEBUG_SV.log(l_module_name, 'candidate.released_status', candidate.released_status);
2363         WSH_DEBUG_SV.log(l_module_name, 'candidate.line_direction', candidate.line_direction);
2364         WSH_DEBUG_SV.log(l_module_name, 'candidate.src_requested_quantity', candidate.src_requested_quantity);
2365         WSH_DEBUG_SV.log(l_module_name, 'candidate.src_requested_quantity_uom', candidate.src_requested_quantity_uom);
2366         WSH_DEBUG_SV.log(l_module_name, 'candidate.quantity', candidate.quantity);
2367         WSH_DEBUG_SV.log(l_module_name, 'candidate.quantity_uom', candidate.quantity_uom);
2368       END IF;
2369 
2370 
2371       IF     (x_plan_deliveries(l_target_delivery_index).delivery_id IS NOT NULL)
2372          AND (x_plan_deliveries(l_target_delivery_index).wsh_planned_flag <> 'N') THEN
2373         IF (candidate.delivery_id IS NULL
2374             OR candidate.delivery_id <> x_plan_deliveries(l_target_delivery_index).delivery_id)  THEN
2375           -- since delivery is firmed, its shipping attributes cannot be updated (ship method should not be overwritten)
2376           -- and its contents cannot be changed.
2377           -- That means we cannot assign this candidate to the mapped delivery.
2378           stamp_interface_error(
2379                p_group_id            => x_context.group_id,
2380                p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2381                p_entity_interface_id => iline.dd_interface_id,
2382                p_message_name        => 'WSH_TP_I_LINE_NOT_ASSIGNED',
2383                p_token_1_name        => 'DETAIL_ID',
2384                p_token_1_value       => candidate.delivery_detail_id,
2385                x_errors_tab          => x_errors_tab,
2386                x_return_status       => l_return_status);
2387           GOTO next_candidate;
2388         END IF;
2389       END IF;
2390 
2391       -- assume that we need to add the new candidate to l_current_used_details
2392       -- unless we find it in l_current_used_details.
2393       l_match_found := FALSE;
2394 
2395       -- is this delivery detail fully or partialy used?
2396       IF l_current_used_details.COUNT > 0 THEN
2397         l_new_index := l_current_used_details.FIRST;
2398 
2399 <<used_loop>>
2400         WHILE l_new_index IS NOT NULL LOOP
2401           IF (candidate.delivery_detail_id = l_current_used_details(l_new_index).delivery_detail_id)  THEN
2402             l_match_found := TRUE;
2403             IF l_debug_on THEN
2404               WSH_DEBUG_SV.log(l_module_name, 'l_current_used_details found: l_new_index', l_new_index);
2405             END IF;
2406             EXIT used_loop;
2407           END IF;
2408           l_new_index := l_current_used_details.NEXT(l_new_index);
2409         END LOOP;
2410       END IF;
2411 
2412       IF     l_match_found
2413          AND l_current_used_details(l_new_index).available_quantity = 0 THEN
2414         -- this candidate has been used up.
2415         IF l_debug_on THEN
2416           WSH_DEBUG_SV.log(l_module_name, 'candidate is used up: delivery_detail_id', l_current_used_details(l_new_index).delivery_detail_id);
2417         END IF;
2418         GOTO next_candidate;
2419       END IF;
2420 
2421       -- if necessary, make sure candidate's essential grouping attributes match before we map
2422       IF l_first_line_map THEN
2423         -- we need to make sure the first line match
2424         -- the interface delivery's grouping attributes that are passed by TP (and
2425         -- cannot be overwritten by data protection if delivery already has lines assigned):
2426         --   shipment_direction, ship_from_location_id, ship_to_location_id, organization_id, customer_id
2427 
2428         -- line can update these grouping attributes;
2429         --    if we can map the line we will update the plan delivery later.
2430         l_current_attributes.fob_code              := candidate.fob_code;
2431         l_current_attributes.freight_terms_code    := candidate.freight_terms_code;
2432         l_current_attributes.intmed_ship_to_location_id := candidate.intmed_ship_to_location_id;
2433         l_current_attributes.ship_method_code      := candidate.ship_method_code;
2434         l_current_attributes.mode_of_transport     := candidate.mode_of_transport;
2435         l_current_attributes.service_level         := candidate.service_level;
2436         l_current_attributes.carrier_id            := candidate.carrier_id;
2437         l_current_attributes.vendor_id             := candidate.vendor_id;
2438         l_current_attributes.party_id              := candidate.party_id;
2439         l_current_attributes.shipping_control      := candidate.shipping_control;
2440       ELSE
2441         -- these ship method components need to stay current to support generic carrier.
2442         -- (interface line may map to delivery details within an order line set).
2443         l_current_attributes.ship_method_code   := x_plan_deliveries(l_target_delivery_index).ship_method_code;
2444         l_current_attributes.mode_of_transport  := x_plan_deliveries(l_target_delivery_index).mode_of_transport;
2445         l_current_attributes.service_level      := x_plan_deliveries(l_target_delivery_index).service_level;
2446         l_current_attributes.carrier_id         := x_plan_deliveries(l_target_delivery_index).carrier_id;
2447       END IF;
2448 
2449       IF NOT attributes_match(
2450              x_attributes => l_current_attributes,
2451              x_values     => candidate)  THEN
2452         l_group_not_matched := TRUE;
2453         IF l_debug_on THEN
2454           WSH_DEBUG_SV.logmsg(l_module_name, 'attributes_match returns FALSE');
2455         END IF;
2456         GOTO next_candidate;
2457       END IF;
2458 
2459 
2460       IF NOT l_match_found THEN  --{
2461         -- delivery detail is new. Lock it and add it to the list of used details.
2462         BEGIN
2463           wsh_delivery_details_pkg.lock_detail_no_compare(
2464                  p_delivery_detail_id => candidate.delivery_detail_id);
2465         EXCEPTION
2466           WHEN OTHERS THEN
2467                stamp_interface_error(
2468                    p_group_id            => x_context.group_id,
2469                    p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
2470                    p_entity_interface_id => iline.dd_interface_id,
2471                    p_message_name        => 'WSH_TP_I_NO_LOCK_DD',
2472                    p_token_1_name        => 'DETAIL_ID',
2473                    p_token_1_value       => candidate.delivery_detail_id,
2474                    x_errors_tab          => x_errors_tab,
2475                    x_return_status       => l_return_status);
2476                GOTO next_candidate;
2477         END;
2478 
2479         -- Lock its delivery if it will be unassigned
2480         IF     candidate.delivery_id IS NOT NULL
2481            AND candidate.delivery_id <> NVL(x_plan_deliveries(l_target_delivery_index).delivery_id,0) THEN
2482           BEGIN
2483             wsh_new_deliveries_pvt.lock_dlvy_no_compare(
2484                 p_delivery_id => candidate.delivery_id);
2485           EXCEPTION
2486             WHEN OTHERS THEN
2487               stamp_interface_error(
2488                     p_group_id            => x_context.group_id,
2489                     p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
2490                     p_entity_interface_id => iline.dd_interface_id,
2491                     p_message_name        => 'WSH_TP_I_NO_LOCK_DEL',
2492                     p_token_1_name        => 'DELIVERY_NAME',
2493                     p_token_1_value       => WSH_NEW_DELIVERIES_PVT.get_name(candidate.delivery_id),
2494                     x_errors_tab          => x_errors_tab,
2495                     x_return_status       => l_return_status);
2496               GOTO next_candidate;
2497           END;
2498         END IF;
2499 
2500         -- if this is the same order line mapped and its order line is not yet picked,
2501         -- validate that the ordered quantity has not been increased.
2502         -- inbound/drop: this validation applies only to outbound and internal outbound lines.
2503         IF     candidate.source_line_id = iline.source_line_id
2504            AND candidate.released_status IN ('N', 'R', 'X')
2505            AND NVL(candidate.line_direction, 'O') IN ('O', 'IO') THEN
2506 
2507           IF l_debug_on THEN
2508             WSH_DEBUG_SV.logmsg(l_module_name,'Validating ordered quantity increase');
2509           END IF;
2510 
2511           -- if the order line has been split, its quantity will generally be reduced.
2512           IF candidate.src_requested_quantity >
2513                   WSH_WV_UTILS.Convert_Uom(
2514                     from_uom => iline.src_requested_quantity_uom,
2515                     to_uom   => candidate.src_requested_quantity_uom,
2516                     quantity => iline.src_requested_quantity,
2517                     item_id  => candidate.inventory_item_id)  THEN
2518 
2519             IF l_debug_on THEN
2520               WSH_DEBUG_SV.log(l_module_name, 'candidate.src_requested_quantity', candidate.src_requested_quantity);
2521               WSH_DEBUG_SV.log(l_module_name, 'candidate.src_requested_quantity_uom', candidate.src_requested_quantity_uom);
2522 
2523               WSH_DEBUG_SV.log(l_module_name, 'iline.src_requested_quantity',     iline.src_requested_quantity);
2524               WSH_DEBUG_SV.log(l_module_name, 'iline.src_requested_quantity_uom', iline.src_requested_quantity_uom);
2525             END IF;
2526 
2527             -- order line quantity has been increased;
2528             -- check if order line is in process of being staged
2529             l_dummy_dd_id := NULL;
2530             OPEN c_being_staged(p_source_code => candidate.source_code,
2531                                     p_source_line_id => candidate.source_line_id);
2532             FETCH c_being_staged INTO l_dummy_dd_id;
2533             IF c_being_staged%NOTFOUND THEN
2534                l_dummy_dd_id := NULL;
2535             END IF;
2536             CLOSE c_being_staged;
2537 
2538             IF l_dummy_dd_id IS NULL THEN
2539               -- order line has not been staged; fail the plan.
2540               stamp_interface_error(
2541                  p_group_id            => x_context.group_id,
2542                  p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
2543                  p_entity_interface_id => iline.dd_interface_id,
2544                  p_message_name        => 'WSH_TP_F_QTY_INCREASE',
2545                  p_token_1_name        => 'DETAIL_ID',
2546                  p_token_1_value       => candidate.delivery_detail_id,
2547                  x_errors_tab          => x_errors_tab,
2548                  x_return_status       => l_return_status);
2549               x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2550               IF l_debug_on THEN
2551                 WSH_DEBUG_SV.pop(l_module_name);
2552               END IF;
2553               RETURN;
2554             END IF;
2555           END IF;
2556         END IF;
2557 
2558         -- add to l_current_used_details
2559         IF (l_current_used_details.COUNT = 0)  THEN
2560           l_new_index := 1;
2561         ELSE
2562           l_new_index := l_current_used_details.LAST + 1;
2563         END IF;
2564 
2565         IF l_debug_on THEN
2566           WSH_DEBUG_SV.log(l_module_name, 'adding to l_current_used_details: l_new_index', l_new_index);
2567         END IF;
2568 
2569 
2570 
2571         l_current_used_details(l_new_index).delivery_detail_id  := candidate.delivery_detail_id;
2572         l_current_used_details(l_new_index).dd_interface_id     := iline.dd_interface_id;
2573         l_current_used_details(l_new_index).available_quantity  := candidate.quantity;
2574         l_current_used_details(l_new_index).available_quantity_uom := candidate.quantity_uom;
2575         l_current_used_details(l_new_index).current_delivery_id := candidate.delivery_id;
2576         l_current_used_details(l_new_index).topmost_cont_id     := wsh_container_utilities.get_master_cont_id(candidate.delivery_detail_id);
2577         l_current_used_details(l_new_index).target_delivery_index := l_target_delivery_index;
2578         l_current_used_details(l_new_index).track_cont_content_found := FALSE;
2579         l_current_used_details(l_new_index).released_status     := candidate.released_status;
2580         l_current_used_details(l_new_index).move_order_line_id  := candidate.move_order_line_id;
2581         l_current_used_details(l_new_index).split_count         := 0;
2582         l_current_used_details(l_new_index).need_unassignment   := FALSE;
2583         l_current_used_details(l_new_index).organization_id     := candidate.organization_id;
2584         l_current_used_details(l_new_index).line_direction      := candidate.line_direction;
2585       END IF; --}
2586 
2587 
2588       -- at this point, l_new_index points to the record in l_current_used_details
2589       -- we need to finish the mapping:
2590       --    * check conditions when line is split:
2591       --         it is in WMS org and released to warehouse or it is packed,
2592       --         and it goes into different deliveries, fail because
2593       --         it will break LPN configuration.
2594       --    * add the line to x_plan_details
2595       --    * track delivery associated with delivery lines
2596       --           if delivery is new, lock delivery and add to x_plan_deliveries
2597 
2598       IF l_debug_on THEN
2599         WSH_DEBUG_SV.log(l_module_name, 'quantity left to map',  l_current_attributes.mapped_quantity);
2600         WSH_DEBUG_SV.log(l_module_name, 'current used detail available quantity',  l_current_used_details(l_new_index).available_quantity);
2601       END IF;
2602 
2603       -- make sure that if line has been split and lines packed in the same container,
2604       -- the remaining quantity will not go into a different delivery.
2605       IF     l_current_used_details(l_new_index).split_count > 0
2606          AND l_current_used_details(l_new_index).topmost_cont_id IS NOT NULL
2607          AND x_plan_deliveries(l_current_used_details(l_new_index).target_delivery_index).del_interface_id
2608              <> iline.delivery_interface_id THEN
2609         stamp_interface_error(
2610             p_group_id            => x_context.group_id,
2611             p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
2612             p_entity_interface_id => iline.dd_interface_id,
2613             p_message_name        => 'WSH_TP_I_LPN_BREAK',
2614             p_token_1_name        => 'DETAIL_ID',
2615             p_token_1_value       => candidate.delivery_detail_id,
2616             x_errors_tab          => x_errors_tab,
2617             x_return_status       => l_return_status);
2618          GOTO next_candidate;
2619       END IF;
2620 
2621 
2622       IF l_current_used_details(l_new_index).available_quantity > l_current_attributes.mapped_quantity THEN
2623         -- this line has to be split.
2624 
2625         l_mapped_quantity := l_current_attributes.mapped_quantity;
2626         l_map_split_flag  := 'Y';
2627 
2628         -- if line is released to warehouse, it cannot be split.
2629         IF candidate.released_status = 'S' THEN
2630           stamp_interface_error(
2631               p_group_id            => x_context.group_id,
2632               p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
2633               p_entity_interface_id => iline.dd_interface_id,
2634               p_message_name        => 'WSH_TP_I_SPLIT_RELEASED',
2635               p_token_1_name        => 'DETAIL_ID',
2636               p_token_1_value       => candidate.delivery_detail_id,
2637               x_errors_tab          => x_errors_tab,
2638               x_return_status       => l_return_status);
2639            GOTO next_candidate;
2640         END IF;
2641 
2642 -- HW OPMCONV - Removed branching and replaced old OPM API call
2643 -- to check for item with new WSH API
2644 
2645        WSH_DELIVERY_DETAILS_INV.Get_item_information
2646           (
2647                p_organization_id       => candidate.organization_id
2648               , p_inventory_item_id    => candidate.inventory_item_id
2649               , x_mtl_system_items_rec => l_item_info
2650               , x_return_status        => l_return_status
2651             );
2652 -- HW OPMCONV - check for lot_divisible using new variable
2653            IF (  l_item_info.lot_divisible_flag = 'N' AND
2654                  l_item_info.lot_control_code= 2)
2655              AND (l_current_used_details(l_new_index).released_status
2656                     IN ('Y', 'B'))  THEN
2657              stamp_interface_error(
2658                  p_group_id            => x_context.group_id,
2659                  p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
2660                  p_entity_interface_id => iline.dd_interface_id,
2661                  p_message_name        => 'WSH_TP_I_SPLIT_OPM_IND_LOT',
2662                  p_token_1_name        => 'DETAIL_ID',
2663                  p_token_1_value       => candidate.delivery_detail_id,
2664                  x_errors_tab          => x_errors_tab,
2665                  x_return_status       => l_return_status);
2666             GOTO next_candidate;
2667           END IF;
2668 
2669 
2670         -- line will be split.
2671         l_current_used_details(l_new_index).split_count :=
2672                l_current_used_details(l_new_index).split_count + 1;
2673 
2674       ELSE
2675         l_mapped_quantity := l_current_used_details(l_new_index).available_quantity;
2676         l_map_split_flag  := 'N';
2677       END IF;
2678 
2679       -- if line is packed in the container, make sure they go into the same target delivery.
2680       -- l_cont_index and l_new_cont will be used later to track LPN configuration
2681       IF l_current_used_details(l_new_index).topmost_cont_id IS NOT NULL THEN --{
2682         IF (x_track_conts.COUNT > 0)  THEN
2683 
2684           l_cont_index := x_track_conts.FIRST;
2685           l_new_cont   := TRUE;
2686 
2687           WHILE l_cont_index <= x_track_conts.COUNT LOOP
2688             IF (x_track_conts(l_cont_index).topmost_cont_id =
2689                     l_current_used_details(l_new_index).topmost_cont_id) THEN
2690               l_new_cont := FALSE;
2691               EXIT;
2692             END IF;
2693             l_cont_index := l_cont_index + 1;
2694           END LOOP;
2695 
2696           IF NOT l_new_cont THEN
2697             IF (x_track_conts(l_cont_index).target_delivery_index <> l_target_delivery_index) THEN
2698               -- we should not break the LPN.
2699               GOTO next_candidate;
2700             END IF;
2701           END IF;
2702         ELSE
2703           l_cont_index := 1;
2704           l_new_cont   := TRUE;
2705         END IF;
2706       END IF; --}
2707 
2708       -- check if the interface delivery needs mapping
2709       IF     x_plan_deliveries(l_target_delivery_index).delivery_id IS NULL
2710          AND candidate.delivery_id IS NOT NULL THEN
2711 
2712         -- first, verify delivery_id has not already been mapped.
2713         DECLARE
2714           i NUMBER;
2715         BEGIN
2716           i := x_plan_deliveries.FIRST;
2717 <<deliveries_loop>>
2718           WHILE i IS NOT NULL LOOP
2719             IF (x_plan_deliveries(i).delivery_id = candidate.delivery_id)  THEN
2720               EXIT deliveries_loop;
2721             END IF;
2722             i := x_plan_deliveries.NEXT(i);
2723           END LOOP;
2724 
2725           IF i IS NULL THEN
2726             -- this delivery is now mapped.
2727             x_plan_deliveries(l_target_delivery_index).delivery_id := candidate.delivery_id;
2728           END IF;
2729         END;
2730       END IF;
2731 
2732       IF l_debug_on THEN
2733         WSH_DEBUG_SV.log(l_module_name,'mapping delivery_detail_id: ', l_current_used_details(l_new_index).delivery_detail_id);
2734         WSH_DEBUG_SV.log(l_module_name,'l_mapped_quantity: ', l_mapped_quantity);
2735         WSH_DEBUG_SV.log(l_module_name,'l_map_split_flag: ', l_map_split_flag);
2736       END IF;
2737 
2738       l_plan_dd_index := l_plan_dd_index + 1;
2739 
2740       x_plan_details(l_plan_dd_index).delivery_detail_id    := l_current_used_details(l_new_index).delivery_detail_id;
2741       x_plan_details(l_plan_dd_index).dd_interface_id       := iline.dd_interface_id;
2742       x_plan_details(l_plan_dd_index).tp_delivery_detail_id := iline.tp_delivery_detail_id;
2743       x_plan_details(l_plan_dd_index).mapped_quantity       := l_mapped_quantity;
2744       x_plan_details(l_plan_dd_index).mapped_quantity_uom   := candidate.quantity_uom;
2745       x_plan_details(l_plan_dd_index).map_split_flag        := l_map_split_flag;
2746       x_plan_details(l_plan_dd_index).released_status       := candidate.released_status;
2747       x_plan_details(l_plan_dd_index).move_order_line_id    := candidate.move_order_line_id;
2748       x_plan_details(l_plan_dd_index).line_direction        := candidate.line_direction;
2749       x_plan_details(l_plan_dd_index).source_code           := candidate.source_code;
2750       x_plan_details(l_plan_dd_index).source_header_id      := candidate.source_header_id;
2751       x_plan_details(l_plan_dd_index).source_line_set_id    := candidate.source_line_set_id;
2752       x_plan_details(l_plan_dd_index).source_line_id        := candidate.source_line_id;
2753       x_plan_details(l_plan_dd_index).ship_set_id           := candidate.ship_set_id;
2754       x_plan_details(l_plan_dd_index).top_model_line_id     := candidate.top_model_line_id;
2755       x_plan_details(l_plan_dd_index).ato_line_id           := candidate.ato_line_id;
2756       x_plan_details(l_plan_dd_index).ship_model_complete_flag   := candidate.ship_model_complete_flag;
2757       x_plan_details(l_plan_dd_index).ship_from_location_id := candidate.ship_from_location_id;
2758       x_plan_details(l_plan_dd_index).ship_to_location_id   := candidate.ship_to_location_id;
2759       x_plan_details(l_plan_dd_index).organization_id       := candidate.organization_id;
2760       x_plan_details(l_plan_dd_index).customer_id           := candidate.customer_id;
2761       x_plan_details(l_plan_dd_index).fob_code              := candidate.fob_code;
2762       x_plan_details(l_plan_dd_index).freight_terms_code    := candidate.freight_terms_code;
2763       x_plan_details(l_plan_dd_index).intmed_ship_to_location_id   := candidate.intmed_ship_to_location_id;
2764       x_plan_details(l_plan_dd_index).ship_method_code      := candidate.ship_method_code;
2765       x_plan_details(l_plan_dd_index).mode_of_transport     := candidate.mode_of_transport;
2766       x_plan_details(l_plan_dd_index).service_level         := candidate.service_level;
2767       x_plan_details(l_plan_dd_index).carrier_id            := candidate.carrier_id;
2768       x_plan_details(l_plan_dd_index).topmost_cont_id       := l_current_used_details(l_new_index).topmost_cont_id;
2769       x_plan_details(l_plan_dd_index).current_delivery_id   := candidate.delivery_id;
2770       x_plan_details(l_plan_dd_index).target_delivery_index := l_target_delivery_index;
2771       x_plan_details(l_plan_dd_index).wv_frozen_flag        := candidate.wv_frozen_flag;  -- WV changes
2772 
2773 
2774       IF (l_first_line_map) THEN
2775         -- in this context, make sure the delivery's grouping attributes match
2776         -- those of the first line mapped.
2777         -- (per data protection, only currency, organization, customer, ship-from
2778         --  and ship-to cannot be updated)
2779         l_first_line_map := FALSE;
2780 
2781         x_plan_deliveries(l_target_delivery_index).fob_code := candidate.fob_code;
2782         x_plan_deliveries(l_target_delivery_index).freight_terms_code := candidate.freight_terms_code;
2783         x_plan_deliveries(l_target_delivery_index).intmed_ship_to_location_id := candidate.intmed_ship_to_location_id;
2784         x_plan_deliveries(l_target_delivery_index).ship_method_code := candidate.ship_method_code;
2785         x_plan_deliveries(l_target_delivery_index).mode_of_transport := candidate.mode_of_transport;
2786         x_plan_deliveries(l_target_delivery_index).service_level := candidate.service_level;
2787         x_plan_deliveries(l_target_delivery_index).carrier_id := candidate.carrier_id;
2788         x_plan_deliveries(l_target_delivery_index).vendor_id := candidate.vendor_id;
2789         x_plan_deliveries(l_target_delivery_index).party_id := candidate.party_id;
2790         x_plan_deliveries(l_target_delivery_index).shipping_control := candidate.shipping_control;
2791 
2792         x_plan_deliveries(l_target_delivery_index).wms_org_flag :=
2793                  wsh_util_validate.Check_Wms_Org(candidate.organization_id);
2794 
2795       END IF;
2796 
2797       -- support generic carrier by updating NULL values with non-NULL values
2798       --  we will later create/update the delivery with components;
2799       --     ship_method_code is not used.
2800       x_plan_deliveries(l_target_delivery_index).ship_method_code :=
2801                   NVL(x_plan_deliveries(l_target_delivery_index).ship_method_code, candidate.ship_method_code);
2802       x_plan_deliveries(l_target_delivery_index).mode_of_transport :=
2803                   NVL(x_plan_deliveries(l_target_delivery_index).mode_of_transport, candidate.mode_of_transport);
2804       x_plan_deliveries(l_target_delivery_index).service_level :=
2805                   NVL(x_plan_deliveries(l_target_delivery_index).service_level, candidate.service_level);
2806       x_plan_deliveries(l_target_delivery_index).carrier_id :=
2807                   NVL(x_plan_deliveries(l_target_delivery_index).carrier_id, candidate.carrier_id);
2808 
2809 
2810 
2811       IF (x_plan_deliveries(l_target_delivery_index).lines_count = 0)  THEN
2812         -- count the deliveries that have at least one line mapped.
2813         IF l_debug_on THEN
2814           WSH_DEBUG_SV.log(l_module_name, 'l_target_delivery_index for delivery is populated: ', l_target_delivery_index);
2815         END IF;
2816         l_deliveries_populated := l_deliveries_populated + 1;
2817       END IF;
2818 
2819       x_plan_deliveries(l_target_delivery_index).lines_count :=
2820                      x_plan_deliveries(l_target_delivery_index).lines_count + 1;
2821       IF l_current_used_details(l_new_index).released_status = 'S' THEN
2822         x_plan_deliveries(l_target_delivery_index).s_lines_count :=
2823                      x_plan_deliveries(l_target_delivery_index).s_lines_count + 1;
2824       END IF;
2825 
2826       l_current_used_details(l_new_index).available_quantity :=
2827                      l_current_used_details(l_new_index).available_quantity - l_mapped_quantity;
2828       IF l_debug_on THEN
2829          WSH_DEBUG_SV.log(l_module_name, 'l_current_used_details(' || l_new_index || ').available_quantity is now',
2830                   l_current_used_details(l_new_index).available_quantity);
2831       END IF;
2832 
2833       l_current_attributes.mapped_quantity := l_current_attributes.mapped_quantity - l_mapped_quantity;
2834 
2835       -- next, track LPN configuration associated with that delivery detail being mapped
2836       --     For new topmost LPNs, we build sublist of non-container contents
2837       --     As we go through the interface lines that are packed, we will remove them from these sublists.
2838       --     LPN configuration will be broken only if the sublists remain after we complete the interface lines.
2839       IF     (x_plan_details(l_plan_dd_index).topmost_cont_id IS NOT NULL)
2840          AND (NOT l_current_used_details(l_new_index).track_cont_content_found) THEN
2841         IF l_debug_on THEN
2842           WSH_DEBUG_SV.log(l_module_name,'tracking topmost_cont_id', x_plan_details(l_plan_dd_index).topmost_cont_id);
2843         END IF;
2844 
2845         DECLARE
2846           l_content_index NUMBER := NULL;
2847 
2848           -- make a flat list of contents within the topmost container.
2849           CURSOR c_contents(p_topmost_cont_id IN NUMBER) IS
2850              SELECT wda.delivery_detail_id
2851              FROM   wsh_delivery_assignments_v wda
2852              START WITH wda.delivery_detail_id = p_topmost_cont_id
2853              CONNECT BY PRIOR wda.delivery_detail_id = wda.parent_delivery_detail_id;
2854 
2855           -- another cursor to look up the attributes
2856           -- because CONNECT BY does not support joins.
2857           CURSOR c_detail(p_detail_id IN NUMBER) IS
2858              SELECT wdd.delivery_detail_id,
2859                     wdd.container_flag,
2860                     wdd.lpn_id,
2861                     wdd.released_status,
2862                     wdd.source_code
2863              FROM   wsh_delivery_details wdd
2864              WHERE  delivery_detail_id = p_detail_id;
2865 
2866         BEGIN
2867           -- l_cont_index and l_cont_found have been set up
2868           -- to ensure that the outermost LPN can be used for this candidate.
2869 
2870           IF l_new_cont THEN
2871             -- add this new topmost container to x_track_conts list.
2872             -- and build a sublist of container contents that are not yet mapped in l_cont_contents.
2873             --   do not add this mapped line to that sublist because it is accounted for.
2874             IF l_debug_on THEN
2875               WSH_DEBUG_SV.log(l_module_name,'new container: l_cont_index', l_cont_index);
2876             END IF;
2877 
2878             x_track_conts(l_cont_index).topmost_cont_id         := x_plan_details(l_plan_dd_index).topmost_cont_id;
2879             x_track_conts(l_cont_index).plan_dd_index           := l_plan_dd_index;
2880             x_track_conts(l_cont_index).current_delivery_id     := x_plan_details(l_plan_dd_index).current_delivery_id;
2881             x_track_conts(l_cont_index).target_delivery_index   := x_plan_details(l_plan_dd_index).target_delivery_index;
2882             x_track_conts(l_cont_index).organization_id         := x_plan_details(l_plan_dd_index).organization_id;
2883             x_track_conts(l_cont_index).cont_content_base_index := NULL;
2884 
2885             -- lock all details in the LPN configuration and build sublist of non-container contents
2886             FOR content IN c_contents(x_track_conts(l_cont_index).topmost_cont_id) LOOP --[
2887               FOR c IN c_detail(content.delivery_detail_id) LOOP --{
2888 
2889                 IF l_debug_on THEN
2890                   WSH_DEBUG_SV.log(l_module_name,'looping c_contents: delivery_detail_id', c.delivery_detail_id);
2891                 END IF;
2892 
2893                 IF (c.delivery_detail_id = x_track_conts(l_cont_index).topmost_cont_id)  THEN
2894                   -- cache this topmost container details for future reference
2895                   x_track_conts(l_cont_index).lpn_id          := c.lpn_id;
2896                   x_track_conts(l_cont_index).released_status := c.released_status;
2897                   x_track_conts(l_cont_index).source_code     := c.source_code;
2898                 END IF;
2899 
2900                 BEGIN
2901                   wsh_delivery_details_pkg.lock_detail_no_compare(
2902                        p_delivery_detail_id => c.delivery_detail_id);
2903                 EXCEPTION
2904                   WHEN OTHERS THEN
2905                      stamp_interface_error(
2906                        p_group_id            => x_context.group_id,
2907                        p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
2908                        p_entity_interface_id => x_plan_details(x_track_conts(l_cont_index).plan_dd_index).dd_interface_id,
2909                        p_message_name        => 'WSH_TP_F_NO_LOCK_LPN_CONTENTS',
2910                        p_token_1_name        => 'CONTAINER_NAME',
2911                        p_token_1_value       => wsh_container_utilities.get_cont_name(x_track_conts(l_cont_index).topmost_cont_id),
2912                        x_errors_tab          => x_errors_tab,
2913                        x_return_status       => l_return_status);
2914                    x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
2915                    IF l_debug_on THEN
2916                      WSH_DEBUG_SV.pop(l_module_name);
2917                    END IF;
2918                    RETURN;
2919                  END;
2920 
2921                  IF c.container_flag = 'N' THEN --{
2922                    IF l_debug_on THEN
2923                     WSH_DEBUG_SV.log(l_module_name,'topmost_cont_id: content line status', c.released_status);
2924                    END IF;
2925 
2926                    x_track_conts(l_cont_index).lines_staged :=
2927                            x_track_conts(l_cont_index).lines_staged OR (c.released_status IN ('Y', 'C'));
2928                    IF c.delivery_detail_id = x_plan_details(l_plan_dd_index).delivery_detail_id  THEN --[
2929                       l_current_used_details(l_new_index).track_cont_content_found := TRUE;
2930                    ELSE
2931                       IF l_content_index IS NULL THEN --(
2932                         IF l_cont_contents.COUNT = 0 THEN
2933                           l_content_index := 1;
2934                         ELSE
2935                           l_content_index := l_cont_contents.LAST + 1;
2936                         END IF;
2937                         x_track_conts(l_cont_index).cont_content_base_index := l_content_index;
2938                       ELSE
2939                         l_content_index := l_content_index + 1;
2940                       END IF; --)
2941 
2942                       IF l_debug_on THEN
2943                        WSH_DEBUG_SV.log(l_module_name,'topmost_cont_id: l_content_index', l_content_index);
2944                       END IF;
2945 
2946                       l_cont_contents(l_content_index).track_cont_index   := l_cont_index;
2947                       l_cont_contents(l_content_index).delivery_detail_id := c.delivery_detail_id;
2948                    END IF; --]
2949                  END IF;  --}
2950               END LOOP; --}
2951             END LOOP; --]
2952 
2953             -- check if this LPN will need unassignment from current delivery
2954             IF (x_plan_deliveries(x_track_conts(l_cont_index).target_delivery_index).delivery_id
2955                   <> x_track_conts(l_cont_index).current_delivery_id) THEN
2956               x_delivery_unassigns(x_delivery_unassigns.COUNT + 1).delivery_detail_id := x_track_conts(l_cont_index).topmost_cont_id;
2957               x_delivery_unassigns(x_delivery_unassigns.COUNT).delivery_id            := x_track_conts(l_cont_index).current_delivery_id;
2958               x_delivery_unassigns(x_delivery_unassigns.COUNT).organization_id        := x_track_conts(l_cont_index).organization_id;
2959               x_delivery_unassigns(x_delivery_unassigns.COUNT).container_flag         := 'Y';
2960               x_delivery_unassigns(x_delivery_unassigns.COUNT).lines_staged           := x_track_conts(l_cont_index).lines_staged;
2961               x_delivery_unassigns(x_delivery_unassigns.COUNT).wms_org_flag           := wsh_util_validate.Check_Wms_Org(x_track_conts(l_cont_index).organization_id);
2962               x_delivery_unassigns(x_delivery_unassigns.COUNT).source_code            := x_track_conts(l_cont_index).source_code;
2963               x_delivery_unassigns(x_delivery_unassigns.COUNT).released_status        := x_track_conts(l_cont_index).released_status;
2964               x_delivery_unassigns(x_delivery_unassigns.COUNT).lpn_id                 := x_track_conts(l_cont_index).lpn_id;
2965               x_delivery_unassigns(x_delivery_unassigns.COUNT).plan_dd_index          := l_plan_dd_index;
2966               x_delivery_unassigns(x_delivery_unassigns.COUNT).plan_del_index         := NULL;
2967             END IF;
2968 
2969           ELSE
2970             -- look for this delivery detail to remove from the sublist.
2971             l_content_index := x_track_conts(l_cont_index).cont_content_base_index;
2972 
2973             WHILE l_content_index IS NOT NULL LOOP
2974                IF (l_cont_contents(l_content_index).delivery_detail_id = x_plan_details(l_plan_dd_index).delivery_detail_id) THEN
2975 
2976                  -- now that the content is found, remove it from the topmost container's sublist.
2977                  IF l_debug_on THEN
2978                     WSH_DEBUG_SV.log(l_module_name,
2979                                      'cont content is found for dd_id: ',
2980                                      l_cont_contents(l_content_index).delivery_detail_id);
2981                  END IF;
2982                  l_current_used_details(l_new_index).track_cont_content_found := TRUE;
2983 
2984                  IF (l_content_index <> x_track_conts(l_cont_index).cont_content_base_index)  THEN
2985                     l_cont_contents.DELETE(l_content_index);
2986                  ELSE
2987                     -- base index needs to be updated.
2988                     x_track_conts(l_cont_index).cont_content_base_index := l_cont_contents.NEXT(l_content_index);
2989                     l_cont_contents.DELETE(l_content_index);
2990                     l_content_index := x_track_conts(l_cont_index).cont_content_base_index;
2991                     IF l_content_index IS NOT NULL THEN
2992                       -- make sure this new base index is for the same topmost container.
2993                       IF (l_cont_contents(l_content_index).track_cont_index <> l_cont_index)  THEN
2994                          l_content_index := NULL;
2995                          x_track_conts(l_cont_index).cont_content_base_index := NULL;
2996                          IF l_debug_on THEN
2997                            WSH_DEBUG_SV.log(l_module_name, 'sublist is empty for l_cont_index: ', l_cont_index);
2998                          END IF;
2999                       END IF;
3000                     END IF;
3001                  END IF;
3002 
3003                  EXIT;
3004 
3005                END IF;
3006                l_content_index := l_cont_contents.NEXT(l_content_index);
3007                IF l_content_index IS NOT NULL THEN
3008                  IF l_cont_contents(l_content_index).track_cont_index <> l_cont_index THEN
3009                    -- end of the container sublist has been reached.
3010                    EXIT;
3011                  END IF;
3012                END IF;
3013             END LOOP;
3014 
3015           END IF;
3016 
3017         EXCEPTION
3018           WHEN OTHERS THEN
3019             IF c_contents%ISOPEN THEN
3020               CLOSE c_contents;
3021             END IF;
3022             IF c_detail%ISOPEN THEN
3023               CLOSE c_detail;
3024             END IF;
3025             x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
3026             WSH_UTIL_CORE.DEFAULT_HANDLER(
3027                         'WSH_TP_RELEASE.GENERATE_LOCK_CANDIDATES(nested)',
3028                         l_module_name);
3029 
3030             IF l_debug_on THEN
3031               WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured in nested block. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
3032               WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
3033             END IF;
3034             RETURN;
3035         END;
3036 
3037         IF l_debug_on THEN
3038           WSH_DEBUG_SV.log(l_module_name,'finished tracking topmost_cont_id: ', x_plan_details(l_plan_dd_index).topmost_cont_id);
3039         END IF;
3040 
3041       END IF;
3042 
3043       IF l_current_attributes.mapped_quantity <= 0 THEN
3044         -- at this point, interface line is now fully mapped.
3045         EXIT candidates_loop;
3046       END IF;
3047 
3048 <<next_candidate>>
3049       NULL;
3050 
3051     END LOOP; -- candidate
3052 
3053     IF l_current_attributes.mapped_quantity > 0 THEN --{
3054       -- the interface line has not been fully mapped.
3055       -- Possible reasons:
3056       --    -  order line quantity has been split or reduced (this is OK)
3057       --    -  no candidate available
3058       --              * enforced grouping attributes not matched (should fail)
3059       --              * in firmed deliveries
3060       --              * ignored for planning
3061       --    -  ship-from and ship-to location changes (this should fail)
3062       --    -  order line has become ignored for planning (this should fail)
3063 
3064       IF l_candidates_count > 0 THEN --[
3065 
3066         stamp_interface_error(
3067             p_group_id            => x_context.group_id,
3068             p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
3069             p_entity_interface_id => iline.dd_interface_id,
3070             p_message_name        => 'WSH_TP_I_LINE_LEFTOVER',
3071             p_token_1_name        => 'QUANTITY',
3072             p_token_1_value       => l_current_attributes.mapped_quantity,
3073             p_token_2_name        => 'QTY_UOM',
3074             p_token_2_value       => l_current_attributes.mapped_quantity_uom,
3075             x_errors_tab          => x_errors_tab,
3076             x_return_status       => l_return_status);
3077 
3078       ELSE
3079         -- since no candidate was found for the line,
3080         --  check location changes and ignore_for_planning.
3081         --   Note: if the line is completely ignored or has location changes,
3082         --         we would not find any candidate to try matching the group.
3083 
3084         l_dummy_dd_id := NULL;
3085         FOR c IN c_location_change(p_source_code           => iline.source_code,
3086                                    p_source_header_id      => iline.source_header_id,
3087                                    p_source_line_id        => iline.source_line_id,
3088                                    p_ship_from_location_id => iline.ship_from_location_id,
3089                                    p_ship_to_location_id   => iline.ship_to_location_id) LOOP
3090           -- this loop runs at most once.
3091           l_dummy_dd_id := c.delivery_detail_id;
3092         END LOOP;
3093         IF l_dummy_dd_id IS NOT NULL THEN
3094           stamp_interface_error(
3095               p_group_id            => x_context.group_id,
3096               p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
3097               p_entity_interface_id => iline.dd_interface_id,
3098               p_message_name        => 'WSH_TP_F_LOC_CHANGE',
3099               x_errors_tab          => x_errors_tab,
3100               x_return_status       => l_return_status);
3101           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3102           IF l_debug_on THEN
3103             WSH_DEBUG_SV.pop(l_module_name);
3104           END IF;
3105           RETURN;
3106         END IF;
3107 
3108         l_dummy_dd_id := NULL;
3109         FOR c IN c_ignored(p_source_code           => iline.source_code,
3110                            p_source_header_id      => iline.source_header_id,
3111                            p_source_line_id        => iline.source_line_id)  LOOP
3112           -- this loop runs at most once.
3113           l_dummy_dd_id := c.delivery_detail_id;
3114         END LOOP;
3115         IF l_dummy_dd_id IS NOT NULL THEN
3116           stamp_interface_error(
3117               p_group_id            => x_context.group_id,
3118               p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
3119               p_entity_interface_id => iline.dd_interface_id,
3120               p_message_name        => 'WSH_TP_F_IGNORED',
3121               p_token_1_name        => 'DETAIL_ID',
3122               p_token_1_value       => l_dummy_dd_id,
3123               x_errors_tab          => x_errors_tab,
3124               x_return_status       => l_return_status);
3125           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3126           IF l_debug_on THEN
3127             WSH_DEBUG_SV.pop(l_module_name);
3128           END IF;
3129           RETURN;
3130         END IF;
3131 
3132       END IF; --]
3133 
3134       -- if the line is fully unmapped, check if that is due to grouping
3135       -- this condition is not as serious as the above conditions
3136       -- becuause usually you can disable grouping attributes in
3137       -- shipping parameters for outbound lines.
3138       IF     (l_current_attributes.mapped_quantity = iline.quantity)
3139          AND l_group_not_matched THEN
3140         stamp_interface_error(
3141               p_group_id            => x_context.group_id,
3142               p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
3143               p_entity_interface_id => iline.dd_interface_id,
3144               p_message_name        => 'WSH_TP_F_NOT_IN_GROUP',
3145               p_token_1_name        => 'PLAN_DEL_NUM',
3146               p_token_1_value       => x_plan_deliveries(l_target_delivery_index).tp_delivery_number,
3147               x_errors_tab          => x_errors_tab,
3148               x_return_status       => l_return_status);
3149         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3150         IF l_debug_on THEN
3151           WSH_DEBUG_SV.pop(l_module_name);
3152         END IF;
3153         RETURN;
3154       END IF;
3155 
3156     END IF; --}
3157 
3158     l_last_interface_id := iline.delivery_detail_id;
3159 
3160   END LOOP; -- iline
3161 
3162   IF l_debug_on THEN
3163     WSH_DEBUG_SV.log(l_module_name,'end of iline loop: l_current_used_details.COUNT: ', l_current_used_details.COUNT);
3164   END IF;
3165 
3166   IF (l_current_used_details.COUNT > 0)  THEN
3167     flush_used_details(
3168        x_context              => x_context,
3169        x_current_used_details => l_current_used_details,
3170        x_used_details         => x_used_details,
3171        x_errors_tab           => x_errors_tab,
3172        x_return_status        => l_return_status);
3173     IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR, WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR))  THEN
3174       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3175       IF l_debug_on THEN
3176         WSH_DEBUG_SV.pop(l_module_name);
3177       END IF;
3178       RETURN;
3179     END IF;
3180   END IF;
3181 
3182   -- 3. check outcome of mapping lines and deliveries.
3183 
3184   -- Bug 3555487 initialize message stack for each major action points.
3185   FND_MSG_PUB.initialize;
3186 
3187   IF l_debug_on THEN
3188      WSH_DEBUG_SV.log(l_module_name, 'x_plan_details.COUNT', x_plan_details.COUNT);
3189   END IF;
3190 
3191   IF     (l_interface_lines_count > 0)
3192      AND (x_plan_details.COUNT = 0)     THEN
3193     -- we fail the plan if none of the plan lines got mapped.
3194     --
3195     -- We are allowed to release a plan with only dangling containers
3196     -- (in case that TP captures the firm deliveries with only containers),
3197     -- which we will check in the next step.
3198 
3199     stamp_interface_error(
3200           p_group_id            => x_context.group_id,
3201           p_entity_table_name   => 'NONE',
3202           p_entity_interface_id => -1,
3203           p_message_name        => 'WSH_TP_F_NO_LINES',
3204           p_token_1_name        => 'PLAN_TRIP_NUM',
3205           p_token_1_value       => get_plan_trip_num(x_context),
3206           x_errors_tab          => x_errors_tab,
3207           x_return_status       => l_return_status);
3208     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3209     IF l_debug_on THEN
3210        WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
3211        WSH_DEBUG_SV.pop(l_module_name);
3212     END IF;
3213     RETURN;
3214   END IF;
3215 
3216 
3217   IF l_debug_on THEN
3218     WSH_DEBUG_SV.log(l_module_name, 'l_deliveries_populated', l_deliveries_populated);
3219     WSH_DEBUG_SV.log(l_module_name, 'x_plan_deliveries.COUNT', x_plan_deliveries.COUNT);
3220   END IF;
3221 
3222   IF l_deliveries_populated < x_plan_deliveries.COUNT THEN
3223     -- we need to check for these deliveries:
3224     --      a) delivery without lines mapped (fail the plan)
3225     --      b) delivery with dangling containers
3226     --             (fail the plan if the delivery is not found or firm
3227     --              or if the dangling containers do not match the
3228     --              plan containers).
3229 
3230     l_new_index := x_plan_deliveries.FIRST;
3231     WHILE l_new_index IS NOT NULL LOOP
3232 
3233       IF x_plan_deliveries(l_new_index).ilines_count > 0
3234          AND x_plan_deliveries(l_new_index).lines_count = 0 THEN
3235         -- fail the plan because this delivery has plan lines
3236         -- and none of them has been mapped.
3237         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3238         stamp_interface_error(
3239             p_group_id            => x_context.group_id,
3240             p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
3241             p_entity_interface_id => x_plan_deliveries(l_new_index).del_interface_id,
3242             p_message_name        => 'WSH_TP_F_EMPTY_DEL',
3243             p_token_1_name        => 'PLAN_DEL_NUM',
3244             p_token_1_value       => x_plan_deliveries(l_new_index).tp_delivery_number,
3245             p_token_2_name        => 'PLAN_TRIP_NUM',
3246             p_token_2_value       => get_plan_trip_num(x_context),
3247             x_errors_tab          => x_errors_tab,
3248             x_return_status       => l_return_status);
3249         -- ignore return status
3250       ELSIF x_plan_deliveries(l_new_index).ilines_count = 0 THEN
3251 
3252         -- TE delivery must exist for the plan delivery
3253         -- and their firm states must match.
3254         IF    (x_plan_deliveries(l_new_index).delivery_id IS NULL)
3255            OR (x_plan_deliveries(l_new_index).wsh_planned_flag <>
3256                  x_plan_deliveries(l_new_index).planned_flag)  THEN
3257           -- fail the plan because this empty delivery with dangling
3258           -- containers does not exist or does not have the original firm status.
3259           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3260           stamp_interface_error(
3261               p_group_id            => x_context.group_id,
3262               p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
3263               p_entity_interface_id => x_plan_deliveries(l_new_index).del_interface_id,
3264               p_message_name        => 'WSH_TP_F_DANGLING_DEL_DIFF',  -- new message
3265               p_token_1_name        => 'PLAN_DEL_NUM',
3266               p_token_1_value       => x_plan_deliveries(l_new_index).tp_delivery_number,
3267               p_token_2_name        => 'PLAN_TRIP_NUM',
3268               p_token_2_value       => get_plan_trip_num(x_context),
3269               x_errors_tab          => x_errors_tab,
3270               x_return_status       => l_return_status);
3271           -- ignore return status
3272         END IF;
3273 
3274         map_dangling_containers(
3275             x_context              => x_context,
3276             p_delivery_index       => l_new_index,
3277             x_plan_deliveries      => x_plan_deliveries,
3278             x_errors_tab           => x_errors_tab,
3279             x_return_status        => l_return_status);
3280 
3281         IF l_return_status = WSH_UTIL_CORE.G_RET_STS_ERROR
3282            AND x_return_status <> WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR THEN
3283           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3284           EXIT;  -- stop processing as soon as we know release will fail.
3285         ELSIF l_return_status = WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR THEN
3286           x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
3287           EXIT;  -- unexpected error should stop the loop.
3288         END IF;
3289 
3290       END IF;
3291 
3292       l_new_index := x_plan_deliveries.NEXT(l_new_index);
3293     END LOOP;
3294 
3295     IF x_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
3296                            WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
3297       IF l_debug_on THEN
3298          WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
3299          WSH_DEBUG_SV.pop(l_module_name);
3300       END IF;
3301       RETURN;
3302     END IF;
3303 
3304   END IF;
3305 
3306 
3307   IF l_debug_on THEN
3308     WSH_DEBUG_SV.log(l_module_name, 'l_cont_contents.COUNT', l_cont_contents.COUNT);
3309   END IF;
3310 
3311   IF l_cont_contents.COUNT > 0 THEN
3312     -- LPN configuration is broken if sublists have not been completely mapped/deleted.
3313 
3314     l_new_index := x_track_conts.FIRST;
3315     WHILE l_new_index IS NOT NULL LOOP
3316 
3317       IF x_track_conts(l_new_index).cont_content_base_index IS NOT NULL THEN
3318         stamp_interface_error(
3319             p_group_id            => x_context.group_id,
3320             p_entity_table_name   => 'WSH_NEW_DEL_DETAILS_INTERFACE',
3321             p_entity_interface_id => x_plan_details(x_track_conts(l_new_index).plan_dd_index).dd_interface_id,
3322             p_message_name        => 'WSH_TP_F_BROKEN_LPN',
3323             p_token_1_name        => 'CONTAINER_NAME',
3324             p_token_1_value       => wsh_container_utilities.get_cont_name(x_track_conts(l_new_index).topmost_cont_id),
3325             p_token_2_name        => 'PLAN_TRIP_NUM',
3326             p_token_2_value       => get_plan_trip_num(x_context),
3327             x_errors_tab          => x_errors_tab,
3328             x_return_status       => l_return_status);
3329         -- ignore return status
3330       END IF;
3331 
3332       l_new_index := x_track_conts.NEXT(l_new_index);
3333     END LOOP;
3334 
3335     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3336     IF l_debug_on THEN
3337        WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
3338        WSH_DEBUG_SV.pop(l_module_name);
3339     END IF;
3340     RETURN;
3341 
3342   END IF;
3343 
3344   IF l_debug_on THEN
3345     WSH_DEBUG_SV.log(l_module_name, 'x_used_details.COUNT', x_used_details.COUNT);
3346   END IF;
3347 
3348   IF x_used_details.COUNT > 0 THEN
3349     -- validate we can unassign partially unused details from their current deliveries.
3350 
3351     l_new_index := x_used_details.FIRST;
3352     WHILE l_new_index IS NOT NULL LOOP
3353       l_target_delivery_index := x_used_details(l_new_index).target_delivery_index;
3354 
3355       IF     (x_used_details(l_new_index).current_delivery_id
3356                = x_plan_deliveries(l_target_delivery_index).delivery_id)
3357           AND
3358              (x_plan_deliveries(l_target_delivery_index).wsh_planned_flag <> 'N') THEN
3359         stamp_interface_error(
3360             p_group_id            => x_context.group_id,
3361             p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
3362             p_entity_interface_id => x_used_details(l_new_index).dd_interface_id,
3363             p_message_name        => 'WSH_TP_F_FIRM_DEL_UNUSED',
3364             p_token_1_name        => 'DELIVERY_NAME',
3365             p_token_1_value       => WSH_NEW_DELIVERIES_PVT.get_name(
3366                                          x_plan_deliveries(l_target_delivery_index).delivery_id),
3367             p_token_2_name        => 'PLAN_TRIP_NUM',
3368             p_token_2_value       => get_plan_trip_num(x_context),
3369             p_token_3_name        => 'PLAN_DEL_NUM',
3370             p_token_3_value       => x_plan_deliveries(l_target_delivery_index).tp_delivery_number,
3371             x_errors_tab          => x_errors_tab,
3372             x_return_status       => l_return_status);
3373         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3374         IF l_debug_on THEN
3375           WSH_DEBUG_SV.log(l_module_name, 'unused line in firmed del: x_return_status', x_return_status);
3376           WSH_DEBUG_SV.pop(l_module_name);
3377         END IF;
3378         RETURN;
3379       END IF;
3380       l_new_index := x_used_details.NEXT(l_new_index);
3381     END LOOP;
3382 
3383   END IF;
3384 
3385 
3386 
3387   -- 4. Map and lock trips and stops
3388 
3389   -- Bug 3555487 initialize message stack for each major action point.
3390   FND_MSG_PUB.initialize;
3391 
3392   l_new_index := 0;
3393   FOR itrip IN c_tp_interface_trips(p_group_id => x_context.group_id)  LOOP  --{
3394 
3395     IF l_debug_on THEN
3396       WSH_DEBUG_SV.log(l_module_name,
3397                        'itrip looping: trip_interface_id',
3398                        itrip.trip_interface_id);
3399       WSH_DEBUG_SV.log(l_module_name, 'itrip.trip_id', itrip.trip_id);
3400     END IF;
3401 
3402     l_new_index := l_new_index + 1;
3403     x_plan_trips(l_new_index).trip_interface_id   := itrip.trip_interface_id;
3404     x_plan_trips(l_new_index).trip_id             := itrip.trip_id;
3405     x_plan_trips(l_new_index).tp_plan_name        := itrip.tp_plan_name;
3406     x_plan_trips(l_new_index).tp_trip_number      := itrip.tp_trip_number;
3407     x_plan_trips(l_new_index).planned_flag        := itrip.planned_flag;
3408     x_plan_trips(l_new_index).wsh_planned_flag    := itrip.wsh_planned_flag;
3409     x_plan_trips(l_new_index).name                := itrip.name;
3410     x_plan_trips(l_new_index).vehicle_item_id     := itrip.vehicle_item_id;
3411     x_plan_trips(l_new_index).vehicle_organization_id := itrip.vehicle_organization_id;
3412     x_plan_trips(l_new_index).vehicle_num_prefix  := itrip.vehicle_num_prefix;
3413     x_plan_trips(l_new_index).vehicle_number      := itrip.vehicle_number;
3414     x_plan_trips(l_new_index).carrier_id          := itrip.carrier_id;
3415     x_plan_trips(l_new_index).ship_method_code    := itrip.ship_method_code;
3416     x_plan_trips(l_new_index).route_id            := itrip.route_id;
3417     x_plan_trips(l_new_index).routing_instructions := itrip.routing_instructions;
3418     x_plan_trips(l_new_index).service_level       := itrip.service_level;
3419     x_plan_trips(l_new_index).mode_of_transport   := itrip.mode_of_transport;
3420     x_plan_trips(l_new_index).freight_terms_code  := itrip.freight_terms_code;
3421     x_plan_trips(l_new_index).seal_code           := itrip.seal_code;
3422     x_plan_trips(l_new_index).shipments_type_flag := itrip.shipments_type_flag;
3423     x_plan_trips(l_new_index).consolidation_allowed := itrip.consolidation_allowed;
3424     x_plan_trips(l_new_index).schedule_id         := itrip.schedule_id;
3425     x_plan_trips(l_new_index).route_lane_id       := itrip.route_lane_id;
3426     x_plan_trips(l_new_index).lane_id             := itrip.lane_id;
3427     x_plan_trips(l_new_index).booking_number      := itrip.booking_number;
3428     x_plan_trips(l_new_index).vessel              := itrip.vessel;
3429     x_plan_trips(l_new_index).voyage_number       := itrip.voyage_number;
3430     x_plan_trips(l_new_index).port_of_loading     := itrip.port_of_loading;
3431     x_plan_trips(l_new_index).port_of_discharge   := itrip.port_of_discharge;
3432     x_plan_trips(l_new_index).carrier_contact_id  := itrip.carrier_contact_id;
3433     x_plan_trips(l_new_index).shipper_wait_time   := itrip.shipper_wait_time;
3434     x_plan_trips(l_new_index).wait_time_uom       := itrip.wait_time_uom;
3435     x_plan_trips(l_new_index).carrier_response    := itrip.carrier_response;
3436     x_plan_trips(l_new_index).operator            := itrip.operator;
3437     x_plan_trips(l_new_index).stop_base_index     := x_plan_stops.COUNT + 1;
3438     x_plan_trips(l_new_index).linked_stop_count   := 0;
3439 
3440     IF l_debug_on THEN
3441         WSH_DEBUG_SV.log(l_module_name,
3442                          'itrip.lane_id: ',
3443                          itrip.lane_id);
3444         WSH_DEBUG_SV.log(l_module_name,
3445                          'x_plan_trips(l_new_index).lane_id: ',
3446                          x_plan_trips(l_new_index).lane_id);
3447     END IF;
3448 
3449     IF     itrip.wsh_ignore_for_planning = 'Y'
3450        OR  itrip.wsh_status_code IN ('IT', 'CL') THEN
3451 
3452       IF l_debug_on THEN
3453         WSH_DEBUG_SV.log(l_module_name,
3454                          'itrip.wsh_ignore_for_planning: ',
3455                          itrip.wsh_ignore_for_planning);
3456         WSH_DEBUG_SV.log(l_module_name,
3457                          'itrip.wsh_status_code: ',
3458                          itrip.wsh_status_code);
3459       END IF;
3460 
3461       -- create a new trip if it is ignored or no longer eligible.
3462       x_plan_trips(l_new_index).trip_id          := NULL;
3463       x_plan_trips(l_new_index).wsh_planned_flag := NULL;
3464     ELSIF    itrip.wsh_planned_flag = 'N'
3465          AND (itrip.wsh_lane_id IS NOT NULL)
3466          AND (
3467                   (NVL(itrip.wsh_carrier_id, FND_API.G_MISS_NUM)
3468                    <> NVL(itrip.carrier_id, FND_API.G_MISS_NUM))
3469                OR
3470                   (NVL(itrip.wsh_mode_of_transport, FND_API.G_MISS_CHAR)
3471                    <> NVL(itrip.mode_of_transport, FND_API.G_MISS_CHAR))
3472                OR
3473                   (NVL(itrip.wsh_service_level, FND_API.G_MISS_CHAR)
3474                    <> NVL(itrip.service_level, FND_API.G_MISS_CHAR))
3475                OR
3476                   (itrip.wsh_lane_id <> NVL(itrip.lane_id, FND_API.G_MISS_NUM))
3477               )  THEN
3478       -- bug 3295628: FTE does not allow existing trip's ship method components
3479       --  to be changed if the trip has a lane.
3480       --  In this case, we should create a new trip
3481       IF l_debug_on THEN
3482         WSH_DEBUG_SV.log(l_module_name,
3483                          'not mapping trip because of lane/ship method changes: ',
3484                          itrip.trip_id);
3485       END IF;
3486 
3487       x_plan_trips(l_new_index).trip_id          := NULL;
3488       x_plan_trips(l_new_index).wsh_planned_flag := NULL;
3489     ELSE
3490       -- fail plan if the firmed trip's attributes do not match the plan trip's.
3491       -- Bug 3507047: Allow the lane_id to be updated on a firmed trip
3492       -- without failing the plan.
3493       IF     itrip.wsh_planned_flag IN ('Y', 'F')
3494          AND (
3495                   (NVL(itrip.wsh_carrier_id, FND_API.G_MISS_NUM)
3496                    <> NVL(itrip.carrier_id, FND_API.G_MISS_NUM))
3497                OR
3498                   (NVL(itrip.wsh_mode_of_transport, FND_API.G_MISS_CHAR)
3499                    <> NVL(itrip.mode_of_transport, FND_API.G_MISS_CHAR))
3500                OR
3501                   (NVL(itrip.wsh_service_level, FND_API.G_MISS_CHAR)
3502                    <> NVL(itrip.service_level, FND_API.G_MISS_CHAR))
3503                OR
3504                   (NVL(itrip.wsh_vehicle_org_id, FND_API.G_MISS_NUM)
3505                    <> NVL(itrip.vehicle_organization_id, FND_API.G_MISS_NUM))
3506                OR
3507                   (NVL(itrip.wsh_vehicle_item_id, FND_API.G_MISS_NUM)
3508                    <> NVL(itrip.vehicle_item_id, FND_API.G_MISS_NUM))
3509              ) THEN
3510         stamp_interface_error(
3511                   p_group_id            => x_context.group_id,
3512                   p_entity_table_name   => 'WSH_TRIPS_INTERFACE',
3513                   p_entity_interface_id => itrip.trip_interface_id,
3514                   p_message_name        => 'WSH_TP_F_TRIP_DIFF_ATTR',
3515                   p_token_1_name        => 'TRIP_NAME',
3516                   p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(l_new_index).trip_id),
3517                   x_errors_tab          => x_errors_tab,
3518                   x_return_status       => l_return_status);
3519         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3520         IF l_debug_on THEN
3521           WSH_DEBUG_SV.log(l_module_name, 'firmed trip not matched: x_return_status', x_return_status);
3522           WSH_DEBUG_SV.log(l_module_name, 'trip_interface_id', itrip.trip_interface_id);
3523           WSH_DEBUG_SV.log(l_module_name, 'trip_id', itrip.trip_id);
3524           WSH_DEBUG_SV.log(l_module_name, 'wsh_carrier_id', itrip.wsh_carrier_id);
3525           WSH_DEBUG_SV.log(l_module_name, 'wsh_mode_of_transport', itrip.wsh_mode_of_transport);
3526           WSH_DEBUG_SV.log(l_module_name, 'wsh_service_level', itrip.wsh_service_level);
3527           WSH_DEBUG_SV.log(l_module_name, 'wsh_vehicle_org_id', itrip.wsh_vehicle_org_id);
3528           WSH_DEBUG_SV.log(l_module_name, 'wsh_vehicle_item_id', itrip.wsh_vehicle_item_id);
3529           WSH_DEBUG_SV.log(l_module_name, 'wsh_lane_id', itrip.wsh_lane_id);
3530           WSH_DEBUG_SV.log(l_module_name, 'carrier_id', itrip.carrier_id);
3531           WSH_DEBUG_SV.log(l_module_name, 'mode_of_transport', itrip.mode_of_transport);
3532           WSH_DEBUG_SV.log(l_module_name, 'service_level', itrip.service_level);
3533           WSH_DEBUG_SV.log(l_module_name, 'vehicle_organization_id', itrip.vehicle_organization_id);
3534           WSH_DEBUG_SV.log(l_module_name, 'vehicle_item_id', itrip.vehicle_item_id);
3535           WSH_DEBUG_SV.log(l_module_name, 'lane_id', itrip.lane_id);
3536           WSH_DEBUG_SV.pop(l_module_name);
3537         END IF;
3538         RETURN;
3539       END IF;
3540     END IF;
3541 
3542     IF x_plan_trips(l_new_index).trip_id IS NOT NULL THEN
3543       -- lock trip
3544       BEGIN
3545         wsh_trips_pvt.lock_trip_no_compare(
3546               p_trip_id => x_plan_trips(l_new_index).trip_id);
3547       EXCEPTION
3548           WHEN OTHERS THEN
3549             stamp_interface_error(
3550                   p_group_id            => x_context.group_id,
3551                   p_entity_table_name   => 'WSH_TRIPS_INTERFACE',
3552                   p_entity_interface_id => itrip.trip_interface_id,
3553                   p_message_name        => 'WSH_TP_F_NO_LOCK_TRIP',
3554                   p_token_1_name        => 'TRIP_NAME',
3555                   p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(l_new_index).trip_id),
3556                   x_errors_tab          => x_errors_tab,
3557                   x_return_status       => l_return_status);
3558             x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3559             IF l_debug_on THEN
3560               WSH_DEBUG_SV.log(l_module_name, 'trip not locked: x_return_status', x_return_status);
3561               WSH_DEBUG_SV.pop(l_module_name);
3562             END IF;
3563             RETURN;
3564       END;
3565 
3566     END IF;
3567 
3568     -- TP always releases ship method components
3569     -- They need to be validated and ship_method_code will be populated.
3570     DECLARE
3571       l_ship_method_name FND_LOOKUP_VALUES_VL.meaning%TYPE;
3572       l_carrier_name     PO_VENDORS.VENDOR_NAME%TYPE;
3573     BEGIN -- validate freight carrier
3574 
3575       -- TP must pass valid freight carrier
3576 
3577       WSH_UTIL_VALIDATE.Validate_Freight_carrier(
3578             p_ship_method_name  => l_ship_method_name,
3579             x_ship_method_code  => x_plan_trips(l_new_index).ship_method_code,
3580             p_carrier_name      => l_carrier_name,
3581             x_carrier_id        => x_plan_trips(l_new_index).carrier_id,
3582             x_service_level     => x_plan_trips(l_new_index).service_level,
3583             x_mode_of_transport => x_plan_trips(l_new_index).mode_of_transport,
3584             p_entity_type       => 'TRIP',
3585             p_entity_id         => x_plan_trips(l_new_index).trip_id,
3586             x_return_status     => l_return_status,
3587             p_caller            => 'WSH_TP_RELEASE');
3588 
3589 
3590       -- warning means something could not be looked up even if some values are valid.
3591       IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_WARNING,
3592                              WSH_UTIL_CORE.G_RET_STS_ERROR,
3593                              WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
3594             stamp_interface_error(
3595                   p_group_id            => x_context.group_id,
3596                   p_entity_table_name   => 'WSH_TRIPS_INTERFACE',
3597                   p_entity_interface_id => itrip.trip_interface_id,
3598                   p_message_name        => 'WSH_TP_F_INVALID_FC',
3599                   p_token_1_name        => 'PLAN_TRIP_NUM',
3600                   p_token_1_value       => itrip.tp_trip_number,
3601                   p_token_2_name        => 'CARRIER',
3602                   p_token_2_value       => itrip.carrier_id,  --!!! lookup
3603                   p_token_3_name        => 'MODE_OF_TRANSPORT',
3604                   p_token_3_value       => itrip.mode_of_transport, --!!! lookup
3605                   p_token_4_name        => 'SERVICE_LEVEL',
3606                   p_token_4_value       => itrip.service_level, --!!! lookup
3607                   x_errors_tab          => x_errors_tab,
3608                   x_return_status       => l_return_status);
3609             x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3610             IF l_debug_on THEN
3611               WSH_DEBUG_SV.log(l_module_name, 'invalid freight carrier: x_return_status', x_return_status);
3612               WSH_DEBUG_SV.pop(l_module_name);
3613             END IF;
3614             RETURN;
3615           END IF;
3616     END; -- validate freight carrier
3617 
3618     l_stop_index := x_plan_trips(l_new_index).stop_base_index;
3619 
3620     FOR istop IN c_tp_interface_stops(p_trip_interface_id => itrip.trip_interface_id)  LOOP  --[
3621 
3622       IF l_debug_on THEN
3623         WSH_DEBUG_SV.log(l_module_name,
3624                          'istop looping: stop_interface_id',
3625                          istop.stop_interface_id);
3626         WSH_DEBUG_SV.log(l_module_name, 'istop.stop_id', istop.stop_id);
3627       END IF;
3628 
3629       x_plan_stops(l_stop_index).stop_interface_id      := istop.stop_interface_id;
3630       x_plan_stops(l_stop_index).stop_id                := NULL;  -- to be updated
3631       x_plan_stops(l_stop_index).tp_stop_id             := istop.tp_stop_id;
3632       x_plan_stops(l_stop_index).trip_index             := l_new_index;
3633       x_plan_stops(l_stop_index).stop_location_id       := istop.stop_location_id;
3634       x_plan_stops(l_stop_index).stop_sequence_number   := istop.stop_sequence_number;
3635       x_plan_stops(l_stop_index).planned_arrival_date   := istop.planned_arrival_date;
3636       x_plan_stops(l_stop_index).planned_departure_date := istop.planned_departure_date;
3637       x_plan_stops(l_stop_index).departure_gross_weight := istop.departure_gross_weight;
3638       x_plan_stops(l_stop_index).departure_net_weight   := istop.departure_net_weight;
3639       x_plan_stops(l_stop_index).weight_uom_code        := istop.weight_uom_code;
3640       x_plan_stops(l_stop_index).departure_volume       := istop.departure_volume;
3641       x_plan_stops(l_stop_index).volume_uom_code        := istop.volume_uom_code;
3642       x_plan_stops(l_stop_index).departure_seal_code    := istop.departure_seal_code;
3643       x_plan_stops(l_stop_index).departure_fill_percent := istop.departure_fill_percent;
3644       x_plan_stops(l_stop_index).wkend_layover_stops    := istop.wkend_layover_stops;
3645       x_plan_stops(l_stop_index).wkday_layover_stops    := istop.wkday_layover_stops;
3646       x_plan_stops(l_stop_index).shipments_type_flag    := istop.shipments_type_flag;
3647       x_plan_stops(l_stop_index).wv_frozen_flag         := NULL;  -- to be updated, WV changes
3648       x_plan_stops(l_stop_index).internal_do_count      := 0;
3649       x_plan_stops(l_stop_index).external_pd_count      := 0;
3650 
3651       IF x_plan_trips(l_new_index).trip_id IS NOT NULL THEN --(
3652         -- since this trip is mapped, try to map the stop.
3653 
3654         OPEN c_map_stop(p_trip_id                => x_plan_trips(l_new_index).trip_id,
3655                         p_stop_location_id       => istop.stop_location_id,
3656                         p_planned_arrival_date   => istop.planned_arrival_date,
3657                         p_planned_departure_date => istop.planned_departure_date);
3658         FETCH c_map_stop INTO l_map_stop_rec;
3659         l_match_found := c_map_stop%FOUND;
3660         CLOSE c_map_stop;
3661 
3662         IF l_match_found THEN  --{
3663 
3664           IF l_debug_on THEN
3665             WSH_DEBUG_SV.log(l_module_name, 'matched stop_id', l_map_stop_rec.stop_id);
3666           END IF;
3667 
3668           -- wsh trip has a stop mapped.
3669           x_plan_stops(l_stop_index).stop_id := l_map_stop_rec.stop_id;
3670           x_plan_stops(l_stop_index).wv_frozen_flag := l_map_stop_rec.wv_frozen_flag; -- WV changes
3671 
3672           -- lock stop
3673           BEGIN
3674             wsh_trip_stops_pvt.lock_trip_stop_no_compare(
3675                p_stop_id => x_plan_stops(l_stop_index).stop_id);
3676           EXCEPTION
3677               WHEN OTHERS THEN
3678                 stamp_interface_error(
3679                       p_group_id            => x_context.group_id,
3680                       p_entity_table_name   => 'WSH_TRIP_STOPS_INTERFACE',
3681                       p_entity_interface_id => istop.stop_interface_id,
3682                       p_message_name        => 'WSH_TP_F_NO_LOCK_STOP',
3683                       p_token_1_name        => 'TRIP_NAME',
3684                       p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(l_new_index).trip_id),
3685                       x_errors_tab          => x_errors_tab,
3686                       x_return_status       => l_return_status);
3687                 x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3688                 IF l_debug_on THEN
3689                   WSH_DEBUG_SV.log(l_module_name, 'stop not locked: x_return_status', x_return_status);
3690                   WSH_DEBUG_SV.pop(l_module_name);
3691                 END IF;
3692                 RETURN;
3693           END;
3694 
3695         ELSE
3696 
3697           x_plan_stops(l_stop_index).stop_id := NULL;
3698           -- if wsh trip is firmed and plan does not find matching stop, fail plan
3699           IF itrip.wsh_planned_flag IN ('Y', 'F')  THEN  --[
3700             stamp_interface_error(
3701                  p_group_id            => x_context.group_id,
3702                  p_entity_table_name   => 'WSH_TRIPS_INTERFACE',
3703                  p_entity_interface_id => x_plan_trips(l_new_index).trip_interface_id,
3704                  p_message_name        => 'WSH_TP_F_TRIP_FIRM_NO_STOP',
3705                  p_token_1_name        => 'TRIP_NAME',
3706                  p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(l_new_index).trip_id),
3707                  p_token_2_name        => 'PLAN_TRIP_NUM',
3708                  p_token_2_value       => x_plan_trips(l_new_index).tp_trip_number,
3709                  x_errors_tab          => x_errors_tab,
3710                  x_return_status       => l_return_status);
3711             x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3712             IF l_debug_on THEN
3713               WSH_DEBUG_SV.log(l_module_name, 'stop not mapped: x_return_status', x_return_status);
3714               WSH_DEBUG_SV.pop(l_module_name);
3715             END IF;
3716             RETURN;
3717           END IF; --]
3718 
3719         END IF; --}
3720 
3721       END IF; --)
3722 
3723       l_stop_index := l_stop_index + 1;
3724 
3725     END LOOP;  --] istop
3726 
3727   END LOOP;  --} itrip
3728 
3729 
3730   -- 5. Map and lock legs; reuse existing trips and stops whenever possible
3731 
3732   -- Bug 3555487 initialize message stack for each major action point.
3733   FND_MSG_PUB.initialize;
3734 
3735   FOR l_plan_del_index IN x_plan_deliveries.FIRST .. x_plan_deliveries.LAST LOOP
3736 
3737     IF l_debug_on THEN
3738       WSH_DEBUG_SV.log(l_module_name, 'looping l_plan_del_index: ', l_plan_del_index);
3739     END IF;
3740 
3741     -- take snapshot of legs in shipping datamodel
3742     l_snapshot_leg_ids.DELETE;
3743     l_snapshot_trip_ids.DELETE;
3744     l_snapshot_trip_plan_flags.DELETE;
3745     l_snapshot_pu_stop_ids.DELETE;
3746     l_snapshot_pu_seq_nums.DELETE;
3747     l_snapshot_pu_loc_ids.DELETE;
3748     l_snapshot_pu_arrive_dates.DELETE;
3749     l_snapshot_pu_depart_dates.DELETE;
3750     l_snapshot_pu_wv_flag.DELETE;
3751     l_snapshot_do_stop_ids.DELETE;
3752     l_snapshot_do_phys_stop_ids.DELETE;
3753     l_snapshot_do_seq_nums.DELETE;
3754     l_snapshot_do_loc_ids.DELETE;
3755     l_snapshot_do_arrive_dates.DELETE;
3756     l_snapshot_do_depart_dates.DELETE;
3757     l_snapshot_do_wv_flag.DELETE;
3758 
3759     l_snapshot_carrier_ids.DELETE;
3760     l_snapshot_modes.DELETE;
3761     l_snapshot_service_levels.DELETE;
3762     l_snapshot_veh_org_ids.DELETE;
3763     l_snapshot_veh_item_ids.DELETE;
3764     l_snapshot_lane_ids.DELETE;
3765 
3766 
3767     IF x_plan_deliveries(l_plan_del_index).delivery_id IS NOT NULL THEN
3768 
3769       -- lock legs in this delivery
3770       BEGIN
3771         wsh_delivery_legs_pvt.lock_dlvy_leg_no_compare(
3772            p_delivery_id => x_plan_deliveries(l_plan_del_index).delivery_id);
3773       EXCEPTION
3774           WHEN OTHERS THEN
3775             stamp_interface_error(
3776                   p_group_id            => x_context.group_id,
3777                   p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
3778                   p_entity_interface_id => x_plan_deliveries(l_plan_del_index).del_interface_id,
3779                   p_message_name        => 'WSH_TP_F_NO_LOCK_LEGS',
3780                   p_token_1_name        => 'DELIVERY_NAME',
3781                   p_token_1_value       =>
3782                        WSH_NEW_DELIVERIES_PVT.get_name(x_plan_deliveries(l_plan_del_index).delivery_id),
3783                   x_errors_tab          => x_errors_tab,
3784                   x_return_status       => l_return_status);
3785             x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
3786             IF l_debug_on THEN
3787               WSH_DEBUG_SV.log(l_module_name, 'stop not locked: x_return_status', x_return_status);
3788               WSH_DEBUG_SV.pop(l_module_name);
3789             END IF;
3790             RETURN;
3791       END;
3792 
3793       IF l_debug_on THEN
3794         WSH_DEBUG_SV.log(l_module_name, 'snapshot legs for delivery_id: ',
3795                          x_plan_deliveries(l_plan_del_index).delivery_id);
3796       END IF;
3797 
3798       OPEN  c_snapshot_legs(p_delivery_id => x_plan_deliveries(l_plan_del_index).delivery_id);
3799       FETCH c_snapshot_legs BULK COLLECT INTO
3800                l_snapshot_leg_ids,
3801                l_snapshot_trip_ids,
3802                l_snapshot_trip_plan_flags,
3803                l_snapshot_carrier_ids,
3804                l_snapshot_modes,
3805                l_snapshot_service_levels,
3806                l_snapshot_veh_org_ids,
3807                l_snapshot_veh_item_ids,
3808                l_snapshot_lane_ids,
3809                l_snapshot_pu_stop_ids,
3810                l_snapshot_pu_loc_ids,
3811                l_snapshot_pu_arrive_dates,
3812                l_snapshot_pu_depart_dates,
3813                l_snapshot_pu_wv_flag,
3814                l_snapshot_do_stop_ids,
3815                l_snapshot_do_phys_stop_ids,
3816                l_snapshot_do_loc_ids,
3817                l_snapshot_do_arrive_dates,
3818                l_snapshot_do_depart_dates,
3819                l_snapshot_do_wv_flag;
3820       CLOSE c_snapshot_legs;
3821 
3822 
3823       -- look up physical stop dates to match the plan.
3824       DECLARE
3825         i NUMBER;
3826       BEGIN
3827         FOR i IN 1..l_snapshot_do_phys_stop_ids.COUNT LOOP
3828           IF l_snapshot_do_phys_stop_ids(i) IS NOT NULL THEN
3829              IF l_debug_on THEN
3830                WSH_DEBUG_SV.log(l_module_name, 'mapping physical stop id',
3831                                l_snapshot_do_phys_stop_ids(i));
3832              END IF;
3833 
3834              OPEN c_physical_stop(l_snapshot_do_phys_stop_ids(i));
3835              FETCH c_physical_stop INTO
3836                    l_snapshot_do_arrive_dates(i),
3837                    l_snapshot_do_arrive_dates(i),
3838                    l_snapshot_do_wv_flag(i);
3839              CLOSE c_physical_stop;
3840           END IF;
3841         END LOOP;
3842       END;
3843 
3844       IF l_debug_on THEN
3845         WSH_DEBUG_SV.log(l_module_name, 'count of existing legs: ',
3846                          l_snapshot_leg_ids.COUNT);
3847       END IF;
3848 
3849     END IF;
3850 
3851     -- build the plan list of legs
3852 
3853     l_new_index := x_plan_legs.COUNT;
3854     x_plan_deliveries(l_plan_del_index).leg_base_index := l_new_index;
3855 
3856     l_phys_ult_do_mapped := FALSE;
3857 
3858     FOR ileg IN c_tp_interface_legs(
3859                      p_delivery_interface_id => x_plan_deliveries(l_plan_del_index).del_interface_id)
3860     LOOP --[ ileg
3861 
3862       IF l_debug_on THEN
3863         WSH_DEBUG_SV.log(l_module_name, 'ileg looping: delivery_leg_interface_id',
3864                          ileg.delivery_leg_interface_id);
3865       END IF;
3866 
3867       l_new_index := l_new_index + 1;
3868       x_plan_legs(l_new_index).leg_interface_id   := ileg.delivery_leg_interface_id;
3869       x_plan_legs(l_new_index).delivery_index     := l_plan_del_index;
3870 
3871       x_plan_legs(l_new_index).delivery_leg_id    := NULL;
3872       x_plan_legs(l_new_index).trip_index         := NULL;
3873       x_plan_legs(l_new_index).pickup_stop_index  := NULL;
3874       x_plan_legs(l_new_index).dropoff_stop_index := NULL;
3875 
3876       DECLARE
3877         l_trip_index         NUMBER;
3878         l_pu_stop_index      NUMBER;
3879         l_do_stop_index      NUMBER;
3880       BEGIN
3881 
3882         l_phys_ult_do_mapped := FALSE;
3883         l_last_do_stop_index := NULL;
3884 
3885         -- find plan trip
3886         l_trip_index := x_plan_trips.FIRST;
3887         WHILE x_plan_trips(l_trip_index).trip_interface_id <> ileg.trip_interface_id LOOP
3888           l_trip_index := x_plan_trips.NEXT(l_trip_index);
3889         END LOOP;
3890 
3891         IF l_debug_on THEN
3892           WSH_DEBUG_SV.log(l_module_name, 'l_trip_index', l_trip_index);
3893         END IF;
3894 
3895         -- find plan pick up stop
3896         l_pu_stop_index := x_plan_trips(l_trip_index).stop_base_index;
3897         WHILE x_plan_stops(l_pu_stop_index).stop_interface_id <> ileg.pick_up_stop_interface_id LOOP
3898           l_pu_stop_index := x_plan_stops.NEXT(l_pu_stop_index);
3899         END LOOP;
3900 
3901         IF l_debug_on THEN
3902           WSH_DEBUG_SV.log(l_module_name, 'l_pu_stop_index: ', l_pu_stop_index);
3903         END IF;
3904 
3905         -- track external pick ups
3906         IF x_plan_stops(l_pu_stop_index).stop_location_id
3907               = x_plan_deliveries(l_plan_del_index).initial_pickup_location_id  THEN
3908           -- external pick-ups can trigger linking
3909           IF x_plan_stops(l_pu_stop_index).external_pd_count = 0
3910              AND x_plan_stops(l_pu_stop_index).internal_do_count > 0 THEN
3911              IF x_plan_trips(l_trip_index).linked_stop_count = 0 THEN
3912                x_context.linked_trip_count := x_context.linked_trip_count + 1;
3913              END IF;
3914              x_plan_trips(l_trip_index).linked_stop_count := x_plan_trips(l_trip_index).linked_stop_count + 1;
3915           END IF;
3916           x_plan_stops(l_pu_stop_index).external_pd_count :=
3917                 x_plan_stops(l_pu_stop_index).external_pd_count + 1;
3918         END IF;
3919 
3920         -- find plan drop off stop
3921         l_do_stop_index := x_plan_stops.NEXT(l_pu_stop_index);
3922         WHILE x_plan_stops(l_do_stop_index).stop_interface_id <> ileg.drop_off_stop_interface_id LOOP
3923           l_do_stop_index := x_plan_stops.NEXT(l_do_stop_index);
3924         END LOOP;
3925 
3926         -- since legs are sequenced (by TP), assume that the last leg will
3927         -- have the ultimate drop off.
3928         l_last_do_stop_index := l_do_stop_index;
3929 
3930         -- track internal drop offs
3931         IF x_plan_stops(l_do_stop_index).stop_location_id
3932               = x_plan_deliveries(l_plan_del_index).physical_ultimate_do_loc_id  THEN
3933           -- internal drop-offs can trigger linking
3934           l_phys_ult_do_mapped := TRUE;
3935           IF x_plan_stops(l_do_stop_index).internal_do_count = 0
3936              AND x_plan_stops(l_do_stop_index).external_pd_count > 0 THEN
3937              IF x_plan_trips(l_trip_index).linked_stop_count = 0 THEN
3938                x_context.linked_trip_count := x_context.linked_trip_count + 1;
3939              END IF;
3940              x_plan_trips(l_trip_index).linked_stop_count := x_plan_trips(l_trip_index).linked_stop_count + 1;
3941           END IF;
3942           x_plan_stops(l_do_stop_index).internal_do_count :=
3943                 x_plan_stops(l_do_stop_index).internal_do_count + 1;
3944         ELSE
3945           -- external drop-offs can trigger linking
3946           IF x_plan_stops(l_do_stop_index).external_pd_count = 0
3947              AND x_plan_stops(l_do_stop_index).internal_do_count > 0 THEN
3948              IF x_plan_trips(l_trip_index).linked_stop_count = 0 THEN
3949                x_context.linked_trip_count := x_context.linked_trip_count + 1;
3950              END IF;
3951              x_plan_trips(l_trip_index).linked_stop_count := x_plan_trips(l_trip_index).linked_stop_count + 1;
3952           END IF;
3953           x_plan_stops(l_do_stop_index).external_pd_count :=
3954                 x_plan_stops(l_do_stop_index).external_pd_count + 1;
3955         END IF;
3956 
3957 
3958         IF l_debug_on THEN
3959           WSH_DEBUG_SV.log(l_module_name, 'l_do_stop_index: ', l_do_stop_index);
3960         END IF;
3961 
3962         IF l_debug_on THEN
3963           WSH_DEBUG_SV.log(l_module_name, 'l_pu_stop_index', l_pu_stop_index);
3964           WSH_DEBUG_SV.log(l_module_name, 'l_do_stop_index', l_do_stop_index);
3965         END IF;
3966 
3967         x_plan_legs(l_new_index).trip_index         := l_trip_index;
3968         x_plan_legs(l_new_index).pickup_stop_index  := l_pu_stop_index;
3969         x_plan_legs(l_new_index).dropoff_stop_index := l_do_stop_index;
3970 
3971         -- try to match interface leg with snapshot leg
3972         IF l_snapshot_leg_ids.COUNT > 0 THEN
3973 
3974           l_used_index := l_snapshot_leg_ids.FIRST;
3975           WHILE l_used_index IS NOT NULL  LOOP  --{ looking for a snapshot leg to match this interface leg.
3976 
3977             IF l_debug_on THEN
3978               WSH_DEBUG_SV.log(l_module_name, 'snapshot leg: looping l_used_index', l_used_index);
3979             END IF;
3980 
3981             IF     x_plan_trips(l_trip_index).trip_id    = l_snapshot_trip_ids(l_used_index)
3982                AND x_plan_stops(l_pu_stop_index).stop_id = l_snapshot_pu_stop_ids(l_used_index)
3983                AND x_plan_stops(l_do_stop_index).stop_id = l_snapshot_do_stop_ids(l_used_index)  THEN
3984               -- when the existing IDs match, keep leg_id
3985               IF l_debug_on THEN
3986                 WSH_DEBUG_SV.log(l_module_name, 'existing leg is mapped: l_used_index: ', l_used_index);
3987               END IF;
3988               x_plan_legs(l_new_index).delivery_leg_id := l_snapshot_leg_ids(l_used_index);
3989 
3990               EXIT;
3991             ELSIF  (x_plan_trips(l_trip_index).trip_id IS NULL)
3992                   AND
3993                   (    x_plan_stops(l_pu_stop_index).stop_location_id =
3994                           l_snapshot_pu_loc_ids(l_used_index)
3995                    AND x_plan_stops(l_pu_stop_index).planned_arrival_date =
3996                           l_snapshot_pu_arrive_dates(l_used_index)
3997                    AND x_plan_stops(l_pu_stop_index).planned_departure_date =
3998                           l_snapshot_pu_depart_dates(l_used_index))
3999                   AND
4000                   (    x_plan_stops(l_do_stop_index).stop_location_id =
4001                           l_snapshot_do_loc_ids(l_used_index)
4002                    AND x_plan_stops(l_do_stop_index).planned_arrival_date =
4003                           l_snapshot_do_arrive_dates(l_used_index)
4004                    AND x_plan_stops(l_do_stop_index).planned_departure_date =
4005                           l_snapshot_do_depart_dates(l_used_index))
4006                   AND
4007                    (
4008                        (
4009                         -- if trip is not firmed, use it unless it has a lane
4010                         --    and ship method components will be changed (bug 3295628).
4011                              (l_snapshot_trip_plan_flags(l_used_index) = 'N')
4012                         AND  (   (l_snapshot_lane_ids(l_used_index) IS NULL)
4013                               OR (
4014                                   -- if trip has lane, lane and ship method must match.
4015                                       (NVL(l_snapshot_lane_ids(l_used_index), FND_API.G_MISS_NUM)
4016                                           = NVL(x_plan_trips(l_trip_index).lane_id,
4017                                                 FND_API.G_MISS_NUM))
4018                                   AND (NVL(l_snapshot_carrier_ids(l_used_index),
4019                                              FND_API.G_MISS_NUM)
4020                                         = NVL(x_plan_trips(l_trip_index).carrier_id,
4021                                                FND_API.G_MISS_NUM))
4022                                   AND (NVL(l_snapshot_modes(l_used_index),
4023                                            FND_API.G_MISS_CHAR)
4024                                         = NVL(x_plan_trips(l_trip_index).mode_of_transport,
4025                                              FND_API.G_MISS_CHAR))
4026                                   AND (NVL(l_snapshot_service_levels(l_used_index),
4027                                            FND_API.G_MISS_CHAR)
4028                                         = NVL(x_plan_trips(l_trip_index).service_level,
4029                                               FND_API.G_MISS_CHAR))
4030                                  )
4031                              )
4032                        )
4033                    OR  (
4034                         -- if trip is firmed, trip attributes must match the plan's attributes
4035                              (NVL(l_snapshot_carrier_ids(l_used_index),
4036                                   FND_API.G_MISS_NUM)
4037                               = NVL(x_plan_trips(l_trip_index).carrier_id,
4038                                     FND_API.G_MISS_NUM))
4039                          AND (NVL(l_snapshot_modes(l_used_index),
4040                                   FND_API.G_MISS_CHAR)
4041                               = NVL(x_plan_trips(l_trip_index).mode_of_transport,
4042                                     FND_API.G_MISS_CHAR))
4043                          AND (NVL(l_snapshot_service_levels(l_used_index),
4044                                   FND_API.G_MISS_CHAR)
4045                               = NVL(x_plan_trips(l_trip_index).service_level,
4046                                     FND_API.G_MISS_CHAR))
4047                          AND (NVL(l_snapshot_veh_org_ids(l_used_index),
4048                                   FND_API.G_MISS_NUM)
4049                               = NVL(x_plan_trips(l_trip_index).vehicle_organization_id,
4050                                     FND_API.G_MISS_NUM))
4051                          AND (NVL(l_snapshot_veh_item_ids(l_used_index),
4052                                   FND_API.G_MISS_NUM)
4053                               = NVL(x_plan_trips(l_trip_index).vehicle_item_id,
4054                                     FND_API.G_MISS_NUM))
4055                          AND (NVL(l_snapshot_lane_ids(l_used_index),
4056                                   FND_API.G_MISS_NUM)
4057                               = NVL(x_plan_trips(l_trip_index).lane_id,
4058                                     FND_API.G_MISS_NUM))
4059                        )
4060                    )
4061                  THEN
4062               -- stop information match and we can map these plan records to existing trip/stops
4063               IF l_debug_on THEN
4064                 WSH_DEBUG_SV.log(l_module_name, 'mapping existing leg and trip: l_used_index: ', l_used_index);
4065               END IF;
4066               x_plan_trips(l_trip_index).trip_id       := l_snapshot_trip_ids(l_used_index);
4067               x_plan_trips(l_trip_index).wsh_planned_flag := l_snapshot_trip_plan_flags(l_used_index);
4068               x_plan_stops(l_pu_stop_index).stop_id    := l_snapshot_pu_stop_ids(l_used_index);
4069               x_plan_stops(l_pu_stop_index).wv_frozen_flag := l_snapshot_pu_wv_flag(l_used_index);
4070               x_plan_stops(l_do_stop_index).stop_id    := l_snapshot_do_stop_ids(l_used_index);
4071               x_plan_stops(l_do_stop_index).wv_frozen_flag := l_snapshot_do_wv_flag(l_used_index);
4072               x_plan_legs(l_new_index).delivery_leg_id := l_snapshot_leg_ids(l_used_index);
4073 
4074               -- lock the newly mapped trip and map the plan stops to the trip's existing stops.
4075               BEGIN
4076                 wsh_trips_pvt.lock_trip_no_compare(
4077                   p_trip_id => x_plan_trips(l_trip_index).trip_id);
4078                 EXCEPTION
4079                 WHEN OTHERS THEN
4080                   stamp_interface_error(
4081                      p_group_id            => x_context.group_id,
4082                      p_entity_table_name   => 'WSH_TRIPS_INTERFACE',
4083                      p_entity_interface_id => x_plan_trips(l_trip_index).trip_interface_id,
4084                      p_message_name        => 'WSH_TP_F_NO_LOCK_TRIP',
4085                      p_token_1_name        => 'TRIP_NAME',
4086                      p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(l_trip_index).trip_id),
4087                      x_errors_tab          => x_errors_tab,
4088                      x_return_status       => l_return_status);
4089                   x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4090                   IF l_debug_on THEN
4091                     WSH_DEBUG_SV.log(l_module_name, 'leg: trip not locked: x_return_status', x_return_status);
4092                     WSH_DEBUG_SV.pop(l_module_name);
4093                   END IF;
4094                   RETURN;
4095               END;
4096 
4097               l_stop_index := x_plan_trips(l_trip_index).stop_base_index;
4098 
4099               WHILE l_stop_index IS NOT NULL LOOP --(
4100 
4101                 IF l_debug_on THEN
4102                   WSH_DEBUG_SV.log(l_module_name, 'looping to map stops', l_stop_index);
4103                 END IF;
4104 
4105                 OPEN c_map_stop(p_trip_id                => x_plan_trips(l_trip_index).trip_id,
4106                                 p_stop_location_id       => x_plan_stops(l_stop_index).stop_location_id,
4107                                 p_planned_arrival_date   => x_plan_stops(l_stop_index).planned_arrival_date,
4108                                 p_planned_departure_date => x_plan_stops(l_stop_index).planned_departure_date);
4109                 FETCH c_map_stop INTO l_map_stop_rec;
4110                 l_match_found := c_map_stop%FOUND;
4111                 CLOSE c_map_stop;
4112 
4113                 IF l_match_found THEN  --{
4114 
4115                   IF l_debug_on THEN
4116                     WSH_DEBUG_SV.log(l_module_name, 'plan stop matches shipping stop', l_map_stop_rec.stop_id);
4117                   END IF;
4118 
4119                   -- wsh trip has a stop mapped.
4120                   x_plan_stops(l_stop_index).stop_id := l_map_stop_rec.stop_id;
4121 
4122                   -- lock stop
4123                   BEGIN
4124                     wsh_trip_stops_pvt.lock_trip_stop_no_compare(
4125                          p_stop_id => x_plan_stops(l_stop_index).stop_id);
4126                   EXCEPTION
4127                     WHEN OTHERS THEN
4128                       stamp_interface_error(
4129                         p_group_id            => x_context.group_id,
4130                         p_entity_table_name   => 'WSH_TRIP_STOPS_INTERFACE',
4131                         p_entity_interface_id => x_plan_stops(l_stop_index).stop_interface_id,
4132                         p_message_name        => 'WSH_TP_F_NO_LOCK_STOP',
4133                         p_token_1_name        => 'TRIP_NAME',
4134                         p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(l_trip_index).trip_id),
4135                         p_token_2_name        => 'STOP_SEQUENCE',
4136                         p_token_2_value       => x_plan_stops(l_stop_index).stop_sequence_number,
4137                         x_errors_tab          => x_errors_tab,
4138                         x_return_status       => l_return_status);
4139                       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4140                       IF l_debug_on THEN
4141                         WSH_DEBUG_SV.log(l_module_name, 'leg: stop not locked: x_return_status', x_return_status);
4142                         WSH_DEBUG_SV.pop(l_module_name);
4143                       END IF;
4144                       RETURN;
4145                   END;
4146 
4147                 ELSE
4148 
4149                   -- plan does not find the matching stop or the trip does not match:
4150                   -- if wsh trip is routing firmed, this trip cannot be mapped.
4151                   -- if wsh trip is routing and contents firmed, fail plan
4152                   IF x_plan_trips(l_trip_index).wsh_planned_flag = 'Y'  THEN  --[
4153                      -- if flag = 'Y', clear the plan trip and stops' shipping IDs
4154                      -- clear l_used_index and exit.
4155                      IF l_debug_on THEN
4156                        WSH_DEBUG_SV.logmsg(l_module_name, 'stop not matched on routing-firmed trip; undo the mapping');
4157                      END IF;
4158                      x_plan_trips(l_trip_index).trip_id := NULL;
4159                      x_plan_trips(l_trip_index).wsh_planned_flag := NULL;
4160 
4161                      l_stop_index := x_plan_trips(l_trip_index).stop_base_index;
4162                      WHILE l_stop_index IS NOT NULL LOOP
4163                         x_plan_stops(l_stop_index).stop_id := NULL;
4164                         l_stop_index := x_plan_stops.NEXT(l_stop_index);
4165                         IF l_stop_index IS NOT NULL THEN
4166                            IF x_plan_stops(l_stop_index).trip_index <> l_trip_index THEN
4167                              l_stop_index := NULL;
4168                            END IF;
4169                         END IF;
4170                      END LOOP;
4171                      EXIT;
4172 
4173                   ELSIF (x_plan_trips(l_trip_index).wsh_planned_flag = 'F')  THEN  --[
4174                       stamp_interface_error(
4175                         p_group_id            => x_context.group_id,
4176                         p_entity_table_name   => 'WSH_TRIPS_INTERFACE',
4177                         p_entity_interface_id => x_plan_trips(l_trip_index).trip_interface_id,
4178                         p_message_name        => 'WSH_TP_F_TRIP_FIRM_NO_STOP',
4179                         p_token_1_name        => 'TRIP_NAME',
4180                         p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(l_trip_index).trip_id),
4181                         x_errors_tab          => x_errors_tab,
4182                         x_return_status       => l_return_status);
4183                         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4184                       IF l_debug_on THEN
4185                         WSH_DEBUG_SV.log(l_module_name, 'leg: stop not mapped: x_return_status', x_return_status);
4186                         WSH_DEBUG_SV.pop(l_module_name);
4187                       END IF;
4188                       RETURN;
4189                   END IF; --]
4190 
4191                 END IF; --}
4192 
4193                 l_stop_index := x_plan_stops.NEXT(l_stop_index);
4194                 IF l_stop_index IS NOT NULL THEN
4195                   -- clear index if we finished going through the sublist of stops for this trip.
4196                   IF x_plan_stops(l_stop_index).trip_index <> l_trip_index  THEN
4197                     l_stop_index := NULL;
4198                   END IF;
4199                 END IF;
4200 
4201               END LOOP; --)
4202 
4203               EXIT; -- exit when leg is mapped
4204 
4205             END IF;
4206 
4207             l_used_index := l_snapshot_leg_ids.NEXT(l_used_index);
4208           END LOOP; --}
4209 
4210           -- l_used_index being not null means we found a match.
4211           IF l_used_index IS NOT NULL THEN
4212             l_snapshot_leg_ids.DELETE(l_used_index);
4213             l_snapshot_leg_ids.DELETE(l_used_index);
4214             l_snapshot_trip_ids.DELETE(l_used_index);
4215             l_snapshot_trip_plan_flags.DELETE(l_used_index);
4216             l_snapshot_pu_stop_ids.DELETE(l_used_index);
4217             l_snapshot_pu_seq_nums.DELETE(l_used_index);
4218             l_snapshot_pu_loc_ids.DELETE(l_used_index);
4219             l_snapshot_pu_arrive_dates.DELETE(l_used_index);
4220             l_snapshot_pu_depart_dates.DELETE(l_used_index);
4221             l_snapshot_do_stop_ids.DELETE(l_used_index);
4222             l_snapshot_do_seq_nums.DELETE(l_used_index);
4223             l_snapshot_do_loc_ids.DELETE(l_used_index);
4224             l_snapshot_do_arrive_dates.DELETE(l_used_index);
4225             l_snapshot_do_depart_dates.DELETE(l_used_index);
4226           END IF;
4227 
4228         END IF;
4229 
4230       END;
4231 
4232     END LOOP; --] ileg
4233 
4234     -- verify the internal customer location mapping has not changed
4235     IF (NOT l_phys_ult_do_mapped)
4236        AND (x_plan_deliveries(l_plan_del_index).physical_ultimate_do_loc_id IS NOT NULL)  THEN
4237         stamp_interface_error(
4238             p_group_id            => x_context.group_id,
4239             p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
4240             p_entity_interface_id => x_plan_deliveries(l_plan_del_index).del_interface_id,
4241             p_message_name        => 'WSH_TP_F_INT_LOC_CHANGED',  --!!! new message
4242             p_token_1_name        => 'PLAN_DEL_NUM',
4243             p_token_1_value       => x_plan_deliveries(l_plan_del_index).tp_delivery_number,
4244             p_token_2_name        => 'PHYS_LOC_CODE',
4245             p_token_2_value       => WSH_UTIL_CORE.Get_Location_Description(
4246                                        p_location_id => x_plan_stops(l_last_do_stop_index).stop_location_id,
4247                                        p_format      => 'NEW UI CODE'),
4248             p_token_3_name        => 'INT_LOC_CODE',
4249             p_token_3_value       => WSH_UTIL_CORE.Get_Location_Description(
4250                                        p_location_id => x_plan_deliveries(l_plan_del_index).ultimate_dropoff_location_id,
4251                                        p_format      => 'NEW UI CODE'),
4252             x_errors_tab          => x_errors_tab,
4253             x_return_status       => l_return_status);
4254         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4255         IF l_debug_on THEN
4256           WSH_DEBUG_SV.log(l_module_name, 'firmed delivery with extra leg: x_return_status', x_return_status);
4257           WSH_DEBUG_SV.pop(l_module_name);
4258         END IF;
4259         RETURN;
4260     END IF;
4261 
4262     IF l_snapshot_leg_ids.COUNT > 0 THEN
4263 
4264       -- fail plan if delivery is routing/contents firmed
4265       IF x_plan_deliveries(l_plan_del_index).wsh_planned_flag = 'F'  THEN
4266         stamp_interface_error(
4267             p_group_id            => x_context.group_id,
4268             p_entity_table_name   => 'WSH_TRIPS_INTERFACE',
4269             p_entity_interface_id => x_plan_deliveries(l_plan_del_index).del_interface_id,
4270             p_message_name        => 'WSH_TP_F_DEL_FIRM_LEG_DIFF',
4271             p_token_1_name        => 'DELIVERY_NAME',
4272             p_token_1_value       => wsh_new_deliveries_pvt.get_name(x_plan_deliveries(l_plan_del_index).delivery_id),
4273             p_token_2_name        => 'PLAN_TRIP_NUM',
4274             p_token_2_value       => x_plan_trips(1).tp_trip_number,
4275             x_errors_tab          => x_errors_tab,
4276             x_return_status       => l_return_status);
4277         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4278         IF l_debug_on THEN
4279           WSH_DEBUG_SV.log(l_module_name, 'firmed delivery with extra leg: x_return_status', x_return_status);
4280           WSH_DEBUG_SV.pop(l_module_name);
4281         END IF;
4282         RETURN;
4283       END IF;
4284 
4285       -- otherwise, add these unmapped legs to x_trip_unassigns if not routing/contents firmed
4286       IF l_debug_on THEN
4287         WSH_DEBUG_SV.log(l_module_name, 'count of legs to unassign: ',
4288                          l_snapshot_leg_ids.COUNT);
4289       END IF;
4290 
4291 
4292       l_used_index := l_snapshot_trip_ids.FIRST;
4293       WHILE l_used_index IS NOT NULL LOOP
4294 
4295         IF l_snapshot_trip_plan_flags(l_used_index) = 'F' THEN
4296           -- cannot unassign delivery from this routing/contents firmed trip
4297           stamp_interface_error(
4298               p_group_id            => x_context.group_id,
4299               p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
4300               p_entity_interface_id => x_plan_deliveries(l_plan_del_index).del_interface_id,
4301               p_message_name        => 'WSH_TP_F_TRIP_FIRM_NO_UNASSIGN',
4302               p_token_1_name        => 'DELIVERY_NAME',
4303               p_token_1_value       => wsh_new_deliveries_pvt.get_name(x_plan_deliveries(l_plan_del_index).delivery_id),
4304               p_token_2_name        => 'PLAN_TRIP_NUM',
4305               p_token_2_value       => x_plan_trips(1).tp_trip_number,
4306               x_errors_tab          => x_errors_tab,
4307               x_return_status       => l_return_status);
4308           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4309           IF l_debug_on THEN
4310             WSH_DEBUG_SV.log(l_module_name, 'delivery with extra firmed leg: x_return_status', x_return_status);
4311             WSH_DEBUG_SV.pop(l_module_name);
4312           END IF;
4313           RETURN;
4314         END IF;
4315 
4316         x_trip_unassigns( x_trip_unassigns.COUNT + 1 ).delivery_id := x_plan_deliveries(l_plan_del_index).delivery_id;
4317         x_trip_unassigns( x_trip_unassigns.COUNT ).organization_id := x_plan_deliveries(l_plan_del_index).organization_id;
4318         x_trip_unassigns( x_trip_unassigns.COUNT ).trip_id         := l_snapshot_trip_ids(l_used_index);
4319         x_trip_unassigns( x_trip_unassigns.COUNT ).delivery_leg_id := l_snapshot_leg_ids(l_used_index);
4320         x_trip_unassigns( x_trip_unassigns.COUNT ).pickup_stop_id  := l_snapshot_pu_stop_ids(l_used_index);
4321         x_trip_unassigns( x_trip_unassigns.COUNT ).dropoff_stop_id := l_snapshot_do_stop_ids(l_used_index);
4322 
4323         l_used_index := l_snapshot_trip_ids.NEXT(l_used_index);
4324 
4325       END LOOP;
4326 
4327     END IF;
4328 
4329   END LOOP;
4330 
4331 
4332   -- 6. Map and lock continuous moves (FTE), also matching their segments.
4333 
4334   -- Bug 3555487 initialize message stack for each major action point.
4335   FND_MSG_PUB.initialize;
4336 
4337 
4338   WSH_FTE_TP_INTEGRATION.map_moves(
4339            x_context                 => x_context,
4340            x_plan_trips              => x_plan_trips,
4341            x_plan_trip_moves         => x_plan_trip_moves,
4342            x_plan_moves              => x_plan_moves,
4343            x_obsoleted_trip_moves    => x_obsoleted_trip_moves,
4344            x_errors_tab              => x_errors_tab,
4345            x_return_status           => l_return_status
4346           );
4347 
4348   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
4349                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
4350      x_return_status := l_return_status;
4351      IF l_debug_on THEN
4352        WSH_DEBUG_SV.log(l_module_name, 'map_moves failed: x_return_status', x_return_status);
4353        WSH_DEBUG_SV.pop(l_module_name);
4354      END IF;
4355      RETURN;
4356   END IF;
4357 
4358 
4359   --
4360   --
4361   IF l_debug_on THEN
4362      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
4363      WSH_DEBUG_SV.pop(l_module_name);
4364   END IF;
4365 
4366   EXCEPTION
4367     WHEN OTHERS THEN
4368       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
4369       WSH_UTIL_CORE.DEFAULT_HANDLER(
4370                         'WSH_TP_RELEASE.GENERATE_LOCK_CANDIDATES',
4371                         l_module_name);
4372 
4373     IF l_debug_on THEN
4374        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
4375        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
4376     END IF;
4377 
4378     IF c_being_staged%ISOPEN THEN
4379       CLOSE c_being_staged;
4380     END IF;
4381     IF c_map_stop%ISOPEN THEN
4382       CLOSE c_map_stop;
4383     END IF;
4384     IF c_snapshot_legs%ISOPEN THEN
4385       CLOSE c_snapshot_legs;
4386     END IF;
4387     IF c_physical_stop%ISOPEN THEN
4388       CLOSE c_physical_stop;
4389     END IF;
4390 
4391 END generate_lock_candidates;
4392 
4393 
4394 
4395 
4396 
4397 --
4398 --  Procedure:          validate_plan
4399 --  Parameters:
4400 --               x_context             context in this session
4401 --               x_plan_details        list of delivery details mapped to interface lines
4402 --               x_track_conts         list of topmost containers to track
4403 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
4404 --               x_plan_legs           list of delivery legs mapped to interface legs
4405 --               x_plan_stops          list of stops mapped to interface stops
4406 --               x_plan_trips          list of trips mapped to interface trips
4407 --               x_used_details        list of delivery details partially used by the plan
4408 --               x_delivery_unassigns  list of delivery lines to unassign from their deliveries
4409 --               x_trip_unassigns      list of deliveries to unassign from their trips
4410 --               x_obsoleted_stops     list of mapped trips' stops that are not mapped in the plan
4411 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
4412 --               x_return_status       return status
4413 --
4414 --  Description:
4415 --               *  Perform general validation checks on the plan not
4416 --                  covered by other APIs (resync_interface_tables,
4417 --                  generate_lock_candidates, reconciliate_plan, and
4418 --                  plan_cleanup); for performance reason, the validation checks are done in
4419 --                  these other APIs as part of the code flow..
4420 --               *  Identify shipping records that need to be unassigned or deleted
4421 --                  to implement the plan.
4422 --
4423 --               1. Deliveries:
4424 --                 *  calls match_deliveries to identify what detail needs to be unassigned
4425 --                    from delivery
4426 --                       + if delivery is firmed, fail if there is anything to unassign.
4427 --               2. Lines/LPNs:
4428 --                     call validate_wms to check WMS conditions
4429 --                     Note: Deliveries must be validated first, as match_deliveries
4430 --                           may update x_delivery_unassigns with LPNs
4431 --               3. Trips:
4432 --                 *  calls match_trips to identify deliveries to be unassigned
4433 --                    and the stops to be deleted.
4434 --                       + if trip is firmed, fail if there is anything to unassign or delete.
4435 --
4436 PROCEDURE validate_plan(
4437            x_context                  IN OUT NOCOPY context_rec_type,
4438            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
4439            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
4440            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
4441            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
4442            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
4443            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
4444            x_used_details             IN OUT NOCOPY used_details_tab_type,
4445            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
4446            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
4447            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
4448            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
4449            x_return_status               OUT NOCOPY VARCHAR2
4450           )
4451 IS
4452   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'VALIDATE_PLAN';
4453   --
4454   l_debug_on BOOLEAN;
4455 
4456   l_return_status VARCHAR2(1);
4457 
4458   l_del_index NUMBER;
4459   l_indexes   WSH_UTIL_CORE.ID_TAB_TYPE;
4460 
4461   l_discard_rs    VARCHAR2(1);
4462   l_msg_data      VARCHAR2(2000);
4463   l_msg_count     NUMBER;
4464 
4465 BEGIN
4466   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
4467   --
4468   IF l_debug_on IS NULL THEN
4469     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
4470   END IF;
4471 
4472   IF l_debug_on THEN
4473     WSH_DEBUG_SV.push(l_module_name);
4474   END IF;
4475   --
4476   --
4477 
4478   --  1. Deliveries:
4479   --    *  calls match_deliveries to identify what detail needs to be unassigned
4480   --       from delivery
4481   --          + if delivery is firmed, fail if there is anything to unassign.
4482 
4483   -- Bug 3555487 initialize message stack for each major action point.
4484   FND_MSG_PUB.initialize;
4485 
4486   match_deliveries(
4487            x_context             => x_context,
4488            x_plan_details        => x_plan_details,
4489            x_track_conts         => x_track_conts,
4490            x_plan_deliveries     => x_plan_deliveries,
4491            x_used_details        => x_used_details,
4492            x_delivery_unassigns  => x_delivery_unassigns,
4493            x_errors_tab          => x_errors_tab,
4494            x_return_status       => l_return_status
4495          );
4496 
4497   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
4498                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
4499     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4500     IF l_debug_on THEN
4501        WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
4502        WSH_DEBUG_SV.pop(l_module_name);
4503     END IF;
4504     RETURN;
4505   END IF;
4506 
4507 
4508 
4509   -- 2. Lines/LPNs:
4510   --      call validate_wms to check WMS conditions
4511   --    Note: Deliveries must be validated first, as match_deliveries
4512   --          may update x_delivery_unassigns with LPNs
4513 
4514   IF x_context.wms_in_group THEN
4515     -- Bug 3555487 initialize message stack for each major action point.
4516     FND_MSG_PUB.initialize;
4517 
4518     validate_wms(
4519         x_context                  => x_context,
4520         x_plan_details             => x_plan_details,
4521         x_plan_deliveries          => x_plan_deliveries,
4522         x_delivery_unassigns       => x_delivery_unassigns,
4523         x_errors_tab               => x_errors_tab,
4524         x_return_status            => l_return_status
4525     );
4526 
4527     IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
4528                            WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
4529       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4530       IF l_debug_on THEN
4531          WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
4532          WSH_DEBUG_SV.pop(l_module_name);
4533       END IF;
4534       RETURN;
4535     END IF;
4536   END IF;
4537 
4538 
4539 
4540   --  3. Trips:
4541   --    *  calls match_trips to identify deliveries to be unassigned
4542   --       and the stops to be deleted.
4543   --          + if trip is firmed, fail if there is anything to unassign or delete.
4544 
4545   -- Bug 3555487 initialize message stack for each major action point.
4546   FND_MSG_PUB.initialize;
4547 
4548   match_trips(
4549            x_context               => x_context,
4550            x_plan_deliveries       => x_plan_deliveries,
4551            x_plan_stops            => x_plan_stops,
4552            x_plan_trips            => x_plan_trips,
4553            x_trip_unassigns        => x_trip_unassigns,
4554            x_obsoleted_stops       => x_obsoleted_stops,
4555            x_errors_tab            => x_errors_tab,
4556            x_return_status         => l_return_status
4557          );
4558 
4559   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
4560                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
4561     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4562     IF l_debug_on THEN
4563        WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
4564        WSH_DEBUG_SV.pop(l_module_name);
4565     END IF;
4566     RETURN;
4567   END IF;
4568 
4569   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
4570 
4571   --
4572   --
4573   IF l_debug_on THEN
4574      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
4575      WSH_DEBUG_SV.pop(l_module_name);
4576   END IF;
4577 
4578   EXCEPTION
4579     WHEN OTHERS THEN
4580       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
4581       WSH_UTIL_CORE.DEFAULT_HANDLER(
4582                         'WSH_TP_RELEASE.VALIDATE_PLAN',
4583                         l_module_name);
4584 
4585     IF l_debug_on THEN
4586        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
4587        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
4588     END IF;
4589 
4590 END validate_plan;
4591 
4592 
4593 
4594 
4595 
4596 --
4597 --  Procedure:          reconciliate_plan
4598 --  Parameters:
4599 --               x_context             context in this session
4600 --               x_plan_details        list of delivery details mapped to interface lines
4601 --               x_track_conts         list of topmost containers to track
4602 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
4603 --               x_plan_legs           list of delivery legs mapped to interface legs
4604 --               x_plan_stops          list of stops mapped to interface stops
4605 --               x_plan_trips          list of trips mapped to interface trips
4606 --               x_plan_trip_moves     list of trip moves mapped to interface trip moves (FTE)
4607 --               x_plan_moves          list of moves mapped to interface moves (FTE)
4608 --               x_used_details        list of delivery details partially used by the plan
4609 --               x_delivery_unassigns  list of delivery lines to unassign from their deliveries
4610 --               x_trip_unassigns      list of deliveries to unassign from their trips
4611 --               x_obsoleted_stops     list of mapped trips' stops that are not mapped in the plan
4612 --               x_obsoleted_trip_moves     list of mapped trips' moves that are not mapped in the plan
4613 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
4614 --               x_return_status       return status
4615 --
4616 --  Description:
4617 --               Implement the plan by calling group APIs:
4618 --               These activities will happen based on the plan lists:
4619 --                1. unassign non-plan lines and all LPNs in x_delivery_unassigns
4620 --                      inbound/drop: add non-plan lines and non-plan LPNs to split-delivery list.
4621 --                2. unassign delivieres in x_trip_unassigns
4622 --                3. create new deliveries and update existing deliveries
4623 --                4.0 create new trips and update existing trips with NULL lane_id
4624 --                         per bug 3580374, stops should be manipulated
4625 --                         when their trips have NULL lane_id to skip
4626 --                         unneeded stop validation that raised errors.
4627 --                4.1 delete stops in x_obsoleted_stops
4628 --                4.2 create new stops and update existing stops
4629 --                4.3 update trips to have lane_id from the plan
4630 --                5. split lines in x_plan_details
4631 --                6. unassign lines in x_used_details and x_plan_details if need_unassignment is TRUE
4632 --                   inbound/drop: add x_used_details to the split-delivery list if needed and invoke SPLIT-DELIVERY
4633 --                                 and skip unassignment of x_plan_details
4634 --                7. assign details in x_plan_details and x_track_conts to plan deliveries (if different from
4635 --                   current deliveries).
4636 --                   plan lines are unassigned if needed (topmost containers are covered in step 1).
4637 --                     inbound/drop: plan lines and plan topmost containers in x_delivery_unassigns
4638 --                     will use SPLIT-DELIVERY instead of ASSIGN.
4639 --                   * validate that ship sets and SMC are completed in each delivery
4640 --                   (if enforced in shipping parameters)
4641 --                8. update delivery details with TP attributes
4642 --                9. assign deliveries to trips
4643 --               10. reconciliate continuous moves (FTE)
4644 --               11. upgrade deliveries' planned_flag per the plan
4645 --               12. upgrade trips' planned_flag per the plan
4646 --               13. upgrade moves' planned_flag per the plan (FTE)
4647 --               14. call the rating API
4648 --               [HIDING PROJECT] 14.5 call the rank list API
4649 --               15. call the Trip_Action API for Auto-Tender, context variable added
4650 --                   auto_tender_flag
4651 --
4652 --               Note: it is important to keep these activities in the proper order
4653 --                     because later activities will depend on outcome of earlier activities.
4654 --                     For example, splitting details too soon would require tracking all the
4655 --                     new delivery details to be unassigned/assigned, etc.
4656 --
4657 PROCEDURE reconciliate_plan(
4658            x_context                  IN OUT NOCOPY context_rec_type,
4659            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
4660            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
4661            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
4662            x_plan_legs                IN OUT NOCOPY plan_leg_tab_type,
4663            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
4664            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
4665            x_plan_trip_moves          IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
4666            x_plan_moves               IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
4667            x_used_details             IN OUT NOCOPY used_details_tab_type,
4668            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
4669            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
4670            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
4671            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
4672            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
4673            x_return_status               OUT NOCOPY VARCHAR2
4674           )
4675 IS
4676   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'RECONCILIATE_PLAN';
4677   --
4678   l_debug_on BOOLEAN;
4679 
4680   l_index      NUMBER;
4681   l_next_index NUMBER;
4682   l_work_index NUMBER;
4683 
4684   l_message_name       VARCHAR2(30);
4685   l_return_status      VARCHAR2(1);
4686   l_number_of_warnings NUMBER;
4687   l_number_of_errors   NUMBER;
4688   l_msg_data           VARCHAR2(32767);
4689   l_msg_count          NUMBER;
4690   l_interface_entity   WSH_INTERFACE_ERRORS.INTERFACE_TABLE_NAME%TYPE;
4691   l_interface_id       NUMBER;
4692 
4693   l_temp_delivery_index NUMBER;
4694 
4695   l_dd_attrs         WSH_GLBL_VAR_STRCT_GRP.delivery_details_attr_tbl_type;
4696   l_dd_action_prms   WSH_GLBL_VAR_STRCT_GRP.dd_action_parameters_rec_type;
4697   l_dd_defaults      WSH_GLBL_VAR_STRCT_GRP.dd_default_parameters_rec_type;
4698   l_dd_action_rec    WSH_GLBL_VAR_STRCT_GRP.dd_action_out_rec_type;
4699 
4700   -- inbound/drop: track unmapped delivery details for action SPLIT-DELIVERY.
4701   l_sd_dd_attrs      WSH_GLBL_VAR_STRCT_GRP.delivery_details_attr_tbl_type;
4702   l_sd_dd_count      NUMBER := 0;
4703   l_skip_step        BOOLEAN;
4704   l_is_inbound       BOOLEAN;
4705 
4706   l_del_attrs        WSH_NEW_DELIVERIES_PVT.delivery_attr_tbl_type;
4707   l_del_action_prms  WSH_DELIVERIES_GRP.action_parameters_rectype;
4708   l_del_action_rec   WSH_DELIVERIES_GRP.delivery_action_out_rec_type;
4709   l_del_defaults     WSH_DELIVERIES_GRP.default_parameters_rectype;
4710   l_del_in_rec       WSH_DELIVERIES_GRP.DEL_IN_REC_TYPE;
4711   l_del_out_tab      WSH_DELIVERIES_GRP.DEL_OUT_TBL_TYPE;
4712 
4713   l_stop_attrs       WSH_TRIP_STOPS_PVT.stop_attr_tbl_type;
4714   l_stop_action_prms WSH_TRIP_STOPS_GRP.action_parameters_rectype;
4715   l_stop_action_rec  WSH_TRIP_STOPS_GRP.StopActionOutRecType;
4716   l_stop_defaults    WSH_TRIP_STOPS_GRP.default_parameters_rectype;
4717   l_stop_in_rec      WSH_TRIP_STOPS_GRP.stopInRecType;
4718   l_stop_out_tab     WSH_TRIP_STOPS_GRP.stop_out_tab_type;
4719   l_stop_wt_vol_out_tab WSH_TRIP_STOPS_GRP.stop_wt_vol_tab_type;
4720 
4721   l_trip_attrs       WSH_TRIPS_PVT.trip_attr_tbl_type;
4722   l_trip_action_prms WSH_TRIPS_GRP.action_parameters_rectype;
4723   l_trip_in_rec      WSH_TRIPS_GRP.TripInRecType;
4724   l_trip_defaults    WSH_TRIPS_GRP.default_parameters_rectype;
4725   l_trip_out_rec     WSH_TRIPS_GRP.tripActionOutRecType;
4726   l_trip_out_tab     WSH_TRIPS_GRP.trip_out_tab_type;
4727 
4728   -- WV changes
4729   -- Checks if the delivery has wv_frozen flag set to 'Y'
4730   -- Used when unassigning from delivery not in plan.
4731   cursor c_check_del_wv_frozen(p_delivery_id in NUMBER) IS
4732   select initial_pickup_location_id
4733   from wsh_new_deliveries
4734   where wv_frozen_flag = 'Y'
4735   and delivery_id = p_delivery_id;
4736 
4737   -- Checks if the stop has wv_frozen flag set to 'Y'.
4738   -- Looks at all the stops on the delivery leg.
4739   -- Used when unassigning from a trip not in plan.
4740   cursor c_check_stop_wv_frozen(p_trip_id in NUMBER,
4741                                 p_pickup_stop_id in NUMBER,
4742                                 p_dropoff_stop_id in NUMBER) IS
4743   select s.stop_id, s.stop_location_id
4744   from wsh_trip_stops s,
4745        wsh_trip_stops spu,
4746        wsh_trip_stops sdo
4747   where s.wv_frozen_flag = 'Y'
4748   and   (s.stop_sequence_number between
4749          spu.stop_sequence_number
4750          and
4751          sdo.stop_sequence_number)
4752   and  spu.stop_id = p_pickup_stop_id
4753   and  sdo.stop_id = p_dropoff_stop_id
4754   and  s.trip_id = p_trip_id;
4755 
4756 
4757   -- Checks if the stops on the delivery
4758   -- have their wv frozen, even if the
4759   -- delivery does not have its wv frozen.
4760   -- Used when unassigning from delivery.
4761   cursor c_check_del_stop_wv_frozen(p_delivery_id in number) IS
4762   select wts.stop_id, wts.stop_location_id
4763   from wsh_delivery_legs wdl,
4764        wsh_trip_stops wts,
4765        wsh_trip_stops wtspu,
4766        wsh_trip_stops wtsdo
4767   where wdl.delivery_id = p_delivery_id
4768   and   (wts.stop_sequence_number between
4769          wtspu.stop_sequence_number
4770          and
4771          wtsdo.stop_sequence_number)
4772   and   wdl.pick_up_stop_id = wtspu.stop_id
4773   and   wdl.drop_off_stop_id = wtsdo.stop_id
4774   and   wts.trip_id  = wtspu.trip_id
4775   and  wts.wv_frozen_flag = 'Y';
4776 
4777 
4778   -- Checks if the intermediate stops to the
4779   -- given pickup and dropoff stops of the
4780   -- trip have their wv flags frozen.
4781   -- Used when unassigning from a trip in plan.
4782   cursor c_check_intmed_stop_wv_frozen
4783          (p_trip_id in number,
4784           p_pickup_seq_number in number,
4785           p_dropoff_seq_number in number) is
4786   select wts.stop_id, wts.stop_location_id
4787   from   wsh_trip_stops wts
4788   where  wts.stop_sequence_number < p_dropoff_seq_number
4789   and    wts.stop_sequence_number > p_pickup_seq_number
4790   and    wts.trip_id = p_trip_id
4791   and    wts.wv_frozen_flag = 'Y';
4792 
4793   l_wv_frozen_flag VARCHAR2(1);
4794   l_location_id NUMBER;
4795 
4796 
4797 BEGIN
4798   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
4799   --
4800   IF l_debug_on IS NULL THEN
4801     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
4802   END IF;
4803 
4804   IF l_debug_on THEN
4805     WSH_DEBUG_SV.push(l_module_name);
4806   END IF;
4807   --
4808   --
4809 
4810   --  1. unassign non-plan lines and all LPNs in x_delivery_unassigns
4811   --       inbound/drop: add non-plan lines and non-plan LPNs to split-delivery list.
4812 
4813 
4814   IF l_debug_on THEN
4815     WSH_DEBUG_SV.log(l_module_name,'1. Unassign lines/LPNs: x_delivery_unassigns.COUNT', x_delivery_unassigns.COUNT);
4816   END IF;
4817 
4818   IF (x_delivery_unassigns.COUNT > 0)  THEN
4819 
4820     l_dd_attrs.DELETE;
4821     l_index := x_delivery_unassigns.FIRST;
4822     l_work_index := 0;
4823     WHILE l_index IS NOT NULL LOOP
4824       -- x_delivery_unassigns entity is one of the below kinds:
4825       --     a) plan line to unassign from a different delivery mapped in the plan
4826       --          outcome:  Skip this entity in this step; step 8 will take care of its unassignment.
4827       --     b) topmost container associated with a plan line to unassign from a different delivery (mapped or not)
4828       --          outcome:  Unassign in this step if outbound; otherwise, step 8 will take care of it.
4829       --     c) unmapped line or topmost container to unassign from the mapped plan delivery
4830       --          outcome:  Unassign in this step if outbound; otherwise, add to l_sd_dd_attrs for step 7.
4831       IF x_delivery_unassigns(l_index).plan_dd_index IS NOT NULL THEN
4832         -- this entity is part of the plan.
4833         l_is_inbound := (x_plan_details(x_delivery_unassigns(l_index).plan_dd_index).line_direction IN ('I', 'D'));
4834         l_skip_step  :=    (x_delivery_unassigns(l_index).container_flag = 'N')
4835                         OR (l_is_inbound);
4836       ELSE
4837         -- this unmapped entity is to be unassigned from the mapped plan delivery.
4838         l_is_inbound := (x_plan_deliveries(x_delivery_unassigns(l_index).plan_del_index).shipment_direction IN ('I', 'D'));
4839         l_skip_step := FALSE;
4840       END IF;
4841 
4842       IF NOT l_skip_step THEN --{
4843         IF l_is_inbound THEN
4844           l_sd_dd_count := l_sd_dd_count + 1;
4845           l_sd_dd_attrs(l_sd_dd_count).delivery_detail_id := x_delivery_unassigns(l_index).delivery_detail_id;
4846           l_sd_dd_attrs(l_sd_dd_count).organization_id    := x_delivery_unassigns(l_index).organization_id;
4847         ELSE
4848           l_work_index := l_work_index + 1;
4849           l_dd_attrs(l_work_index).delivery_detail_id := x_delivery_unassigns(l_index).delivery_detail_id;
4850           l_dd_attrs(l_work_index).organization_id    := x_delivery_unassigns(l_index).organization_id;
4851         END IF;
4852 
4853         -- WV changes
4854         -- Keep track of deliveries that have WV frozen, but
4855         -- would have their WV changed due to unassignments.
4856         -- Need the ship from locations to log exceptions.
4857 
4858         IF x_delivery_unassigns(l_index).wv_frozen_flag = 'Y' THEN
4859            IF NOT x_context.wv_exception_dels.exists(x_delivery_unassigns(l_index).delivery_id) THEN
4860               x_context.wv_exception_dels(x_delivery_unassigns(l_index).delivery_id) := x_delivery_unassigns(l_index).initial_pickup_location_id;
4861 
4862               IF l_debug_on THEN
4863                  WSH_DEBUG_SV.log(l_module_name,'Log WV exception against del', x_delivery_unassigns(l_index).delivery_id);
4864               END IF;
4865            END IF;
4866 
4867         END IF;
4868 
4869         -- Handles the case where the delivery wv may not be frozen, but it may
4870         -- have a stop that might have its wv frozen.
4871 
4872         FOR stop in c_check_del_stop_wv_frozen(x_delivery_unassigns(l_index).delivery_id) LOOP
4873             IF NOT x_context.wv_exception_stops.exists(stop.stop_id) THEN
4874                x_context.wv_exception_stops(stop.stop_id) := stop.stop_location_id;
4875                IF l_debug_on THEN
4876                   WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', stop.stop_id);
4877                END IF;
4878             END IF;
4879         END LOOP;
4880 
4881       END IF; --} NOT l_skip_step
4882 
4883       l_index := x_delivery_unassigns.NEXT(l_index);
4884 
4885     END LOOP;
4886 
4887     IF l_work_index > 0 THEN
4888       -- invoke unassignment only if there are outbound details
4889       l_dd_action_prms.caller      := 'WSH_TP_RELEASE';
4890       l_dd_action_prms.action_code := 'UNASSIGN';
4891 
4892       WSH_DELIVERY_DETAILS_GRP.delivery_detail_action(
4893         p_api_version_number => 1.0,
4894         p_init_msg_list      => FND_API.G_TRUE,
4895         p_commit             => FND_API.G_FALSE,
4896         x_return_status      => l_return_status,
4897         x_msg_count          => l_msg_count,
4898         x_msg_data           => l_msg_data,
4899         p_rec_attr_tab       => l_dd_attrs,
4900         p_action_prms        => l_dd_action_prms,
4901         x_defaults           => l_dd_defaults,
4902         x_action_out_rec     => l_dd_action_rec);
4903 
4904       IF l_debug_on THEN
4905         WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
4906         WSH_DEBUG_SV.log(l_module_name, 'l_dd_action_rec.valid_id_tab.COUNT', l_dd_action_rec.valid_id_tab.COUNT);
4907         WSH_DEBUG_SV.log(l_module_name, 'l_dd_attrs.COUNT', l_dd_attrs.COUNT);
4908       END IF;
4909 
4910       -- convert warning to error if the count of valid ids does not match.
4911       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
4912           AND l_dd_action_rec.valid_id_tab.COUNT < l_dd_attrs.COUNT) THEN
4913         IF l_debug_on THEN
4914           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
4915         END IF;
4916         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
4917       END IF;
4918 
4919       l_interface_entity := 'NONE';
4920       l_interface_id     := -1;
4921       l_message_name     := 'WSH_TP_F_DEL_UNASSIGNS';
4922       wsh_util_core.api_post_call(
4923            p_return_status => l_return_status,
4924            x_num_warnings  => l_number_of_warnings,
4925            x_num_errors    => l_number_of_errors);
4926     END IF;
4927 
4928     IF l_debug_on THEN
4929       WSH_DEBUG_SV.log(l_module_name,'after x_delivery_unassigns, l_sd_dd_count', l_sd_dd_count);
4930     END IF;
4931 
4932   END IF;
4933 
4934 
4935 
4936   --  2. unassign delivieres in x_trip_unassigns
4937 
4938   IF l_debug_on THEN
4939     WSH_DEBUG_SV.log(l_module_name,'2. Unassign deliveries: x_trip_unassigns.COUNT: ', x_trip_unassigns.COUNT);
4940   END IF;
4941 
4942   IF (x_trip_unassigns.COUNT > 0)  THEN
4943 
4944     l_index := x_trip_unassigns.FIRST;
4945 
4946     l_del_attrs.DELETE;
4947     l_del_action_prms.caller      := 'WSH_TP_RELEASE';
4948     l_del_action_prms.action_code := 'UNASSIGN-TRIP';
4949 
4950     WHILE l_index IS NOT NULL LOOP
4951 
4952       IF x_trip_unassigns(l_index).delivery_id IS NOT NULL THEN
4953         l_del_attrs(1).delivery_id     := x_trip_unassigns(l_index).delivery_id;
4954         l_del_attrs(1).organization_id := x_trip_unassigns(l_index).organization_id;
4955         l_del_action_prms.trip_id  := x_trip_unassigns(l_index).trip_id;
4956 
4957         -- WV changes
4958         -- Keep track of stops that have WV frozen, but
4959         -- would have their WV changed due to unassignments.
4960         -- Need the stop locations to log exceptions.
4961 
4962         FOR stop in c_check_stop_wv_frozen(p_trip_id => x_trip_unassigns(l_index).trip_id,
4963                                            p_pickup_stop_id => x_trip_unassigns(l_index).pickup_stop_id,
4964                                            p_dropoff_stop_id => x_trip_unassigns(l_index).dropoff_stop_id)
4965         LOOP
4966 
4967            IF NOT x_context.wv_exception_stops.exists(stop.stop_id) THEN
4968               x_context.wv_exception_stops(stop.stop_id) :=  stop.stop_location_id;
4969 
4970               IF l_debug_on THEN
4971                  WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', stop.stop_id);
4972               END IF;
4973            END IF;
4974         END LOOP;
4975 
4976 
4977         WSH_DELIVERIES_GRP.delivery_action(
4978           p_api_version_number => 1.0,
4979           p_init_msg_list      => FND_API.G_TRUE,
4980           p_commit             => FND_API.G_FALSE,
4981           p_action_prms        => l_del_action_prms,
4982           p_rec_attr_tab       => l_del_attrs,
4983           x_delivery_out_rec   => l_del_action_rec,
4984           x_defaults_rec       => l_del_defaults,
4985           x_return_status      => l_return_status,
4986           x_msg_count          => l_msg_count,
4987           x_msg_data           => l_msg_data);
4988 
4989         l_interface_entity := 'NONE';
4990         l_interface_id     := -1;
4991         l_message_name     := 'WSH_TP_F_TRIP_UNASSIGNS';
4992         wsh_util_core.api_post_call(
4993              p_return_status => l_return_status,
4994              x_num_warnings  => l_number_of_warnings,
4995              x_num_errors    => l_number_of_errors);
4996 
4997         l_index := x_trip_unassigns.NEXT(l_index);
4998       END IF;
4999 
5000     END LOOP;
5001 
5002   END IF;
5003 
5004 
5005 
5006   --  3. create new deliveries and update existing deliveries
5007 
5008   IF l_debug_on THEN
5009     WSH_DEBUG_SV.log(l_module_name,'3. Create/update deliveries: x_plan_deliveries.COUNT: ', x_plan_deliveries.COUNT);
5010   END IF;
5011 
5012   l_del_attrs.DELETE;
5013   l_del_in_rec.caller      := 'WSH_TP_RELEASE';
5014   l_del_in_rec.action_code := 'CREATE';
5015 
5016   l_index := x_plan_deliveries.FIRST;
5017   WHILE l_index IS NOT NULL LOOP
5018 
5019     -- Bug 3555487 initialize message stack for each major action point.
5020     FND_MSG_PUB.initialize;
5021 
5022     IF l_debug_on THEN
5023       WSH_DEBUG_SV.log(l_module_name, 'create/update deliveries: l_index', l_index);
5024     END IF;
5025 
5026     IF x_plan_deliveries(l_index).delivery_id IS NULL THEN
5027       l_del_in_rec.action_code := 'CREATE';
5028       l_message_name     := 'WSH_TP_F_CREATE_DEL';
5029     ELSE
5030       l_del_in_rec.action_code := 'UPDATE';
5031       l_message_name     := 'WSH_TP_F_UPDATE_DEL';
5032     END IF;
5033 
5034     copy_delivery_record(
5035             p_plan_delivery_rec  => x_plan_deliveries(l_index),
5036             x_delivery_attrs_rec => l_del_attrs(1),
5037             x_return_status      => l_return_status
5038     );
5039 
5040     -- bug 3593690: fail release if delivery cannot be populated
5041     -- usually because shipping parameters are missing.
5042     l_interface_entity := 'WSH_NEW_DEL_INTERFACE';
5043     l_interface_id     := x_plan_deliveries(l_index).del_interface_id;
5044     wsh_util_core.api_post_call(
5045              p_return_status => l_return_status,
5046              x_num_warnings  => l_number_of_warnings,
5047              x_num_errors    => l_number_of_errors);
5048 
5049 
5050     wsh_deliveries_grp.create_update_delivery (
5051         p_api_version_number     =>    1.0,
5052         p_init_msg_list          =>    FND_API.G_FALSE, -- bug 3593690: retain messages from copy_delivery_record
5053         p_commit                 =>    FND_API.G_FALSE,
5054         p_in_rec                 =>    l_del_in_rec,
5055         p_rec_attr_tab           =>    l_del_attrs,
5056         x_del_out_rec_tab        =>    l_del_out_tab,
5057         x_return_status          =>    l_return_status,
5058         x_msg_count              =>    l_msg_count,
5059         x_msg_data               =>    l_msg_data);
5060 
5061     l_interface_entity := 'WSH_NEW_DEL_INTERFACE';
5062     l_interface_id     := x_plan_deliveries(l_index).del_interface_id;
5063     wsh_util_core.api_post_call(
5064              p_return_status => l_return_status,
5065              x_num_warnings  => l_number_of_warnings,
5066              x_num_errors    => l_number_of_errors);
5067 
5068     IF l_del_in_rec.action_code = 'CREATE' THEN
5069       x_plan_deliveries(l_index).delivery_id := l_del_out_tab(l_del_out_tab.FIRST).delivery_id;
5070     END IF;
5071 
5072     l_index := x_plan_deliveries.NEXT(l_index);
5073 
5074   END LOOP;
5075 
5076 
5077 
5078   --  4.0 create new trips and update existing trips with NULL lane_id
5079 
5080   IF l_debug_on THEN
5081     WSH_DEBUG_SV.log(l_module_name,'4.0. Create/update trips: x_plan_trips.COUNT: ', x_plan_trips.COUNT);
5082   END IF;
5083 
5084   create_update_plan_trips(
5085            p_phase        => 1,
5086            x_context      => x_context,
5087            x_plan_trips   => x_plan_trips,
5088            x_errors_tab   => x_errors_tab,
5089            x_return_status => l_return_status
5090           );
5091   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
5092                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
5093      x_return_status := l_return_status;
5094      RETURN;
5095   END IF;
5096 
5097 
5098   --  4.1. delete stops in x_obsoleted_stops
5099 
5100   IF l_debug_on THEN
5101     WSH_DEBUG_SV.log(l_module_name,'4.1. Delete stops: x_obsoleted_stops.COUNT: ', x_obsoleted_stops.COUNT);
5102   END IF;
5103 
5104   IF (x_obsoleted_stops.COUNT > 0)  THEN
5105 
5106     l_stop_attrs.DELETE;
5107     l_index := x_obsoleted_stops.FIRST;
5108     l_work_index := 0;
5109     WHILE l_index IS NOT NULL LOOP
5110       l_work_index := l_work_index + 1;
5111       l_stop_attrs(l_work_index).stop_id := x_obsoleted_stops(l_index).stop_id;
5112       l_stop_attrs(l_work_index).trip_id := x_obsoleted_stops(l_index).trip_id;
5113       l_index := x_obsoleted_stops.NEXT(l_index);
5114     END LOOP;
5115 
5116     l_stop_action_prms.caller      := 'WSH_TP_RELEASE';
5117     l_stop_action_prms.action_code := 'DELETE';
5118 
5119     WSH_TRIP_STOPS_GRP.stop_action(
5120       p_api_version_number => 1.0,
5121       p_init_msg_list      => FND_API.G_TRUE,
5122       p_commit             => FND_API.G_FALSE,
5123       p_action_prms        => l_stop_action_prms,
5124       p_rec_attr_tab       => l_stop_attrs,
5125       x_stop_out_rec       => l_stop_action_rec,
5126       x_def_rec            => l_stop_defaults,
5127       x_return_status      => l_return_status,
5128       x_msg_count          => l_msg_count,
5129       x_msg_data           => l_msg_data);
5130 
5131     l_interface_entity := 'NONE';
5132     l_interface_id     := -1;
5133     l_message_name     := 'WSH_TP_F_OBSOLETED_STOPS';
5134     wsh_util_core.api_post_call(
5135          p_return_status => l_return_status,
5136          x_num_warnings  => l_number_of_warnings,
5137          x_num_errors    => l_number_of_errors);
5138 
5139   END IF;
5140 
5141 
5142 
5143   --  4.2 create new stops and update existing stops
5144   IF l_debug_on THEN
5145     WSH_DEBUG_SV.log(l_module_name,'4.2 Create/update stops: x_plan_stops.COUNT: ', x_plan_stops.COUNT);
5146   END IF;
5147 
5148   l_stop_attrs.DELETE;
5149   l_stop_in_rec.caller      := 'WSH_TP_RELEASE';
5150 
5151   l_index := x_plan_stops.FIRST;
5152   WHILE l_index IS NOT NULL LOOP
5153 
5154     -- Bug 3555487 initialize message stack for each major action point.
5155     FND_MSG_PUB.initialize;
5156 
5157     IF l_debug_on THEN
5158       WSH_DEBUG_SV.log(l_module_name, 'create/update stops: l_index', l_index);
5159     END IF;
5160 
5161     copy_stop_record(
5162           p_plan_stop_rec  => x_plan_stops(l_index),
5163           p_plan_trips     => x_plan_trips,
5164           x_stop_attrs_rec => l_stop_attrs(1)
5165     );
5166 
5167     IF x_plan_stops(l_index).stop_id IS NULL THEN
5168       l_stop_in_rec.action_code := 'CREATE';
5169       l_message_name     := 'WSH_TP_F_CREATE_STOP';
5170     ELSE
5171       l_stop_in_rec.action_code := 'UPDATE';
5172       l_message_name     := 'WSH_TP_F_UPDATE_STOP';
5173     END IF;
5174 
5175     wsh_trip_stops_grp.Create_Update_Stop(
5176         p_api_version_number     =>    1.0,
5177         p_init_msg_list          =>    FND_API.G_FALSE,
5178         p_commit                 =>    FND_API.G_FALSE,
5179         p_in_rec                 =>    l_stop_in_rec,
5180         p_rec_attr_tab           =>    l_stop_attrs,
5181         x_stop_out_tab           =>    l_stop_out_tab,
5182         x_return_status          =>    l_return_status,
5183         x_msg_count              =>    l_msg_count,
5184         x_msg_data               =>    l_msg_data,
5185         x_stop_wt_vol_out_tab    =>    l_stop_wt_vol_out_tab
5186       );
5187 
5188     l_interface_entity := 'WSH_TRIP_STOPS_INTERFACE';
5189     l_interface_id     := x_plan_stops(l_index).stop_interface_id;
5190 
5191     wsh_util_core.api_post_call(
5192              p_return_status => l_return_status,
5193              x_num_warnings  => l_number_of_warnings,
5194              x_num_errors    => l_number_of_errors);
5195 
5196     IF l_stop_in_rec.action_code = 'CREATE' THEN
5197         x_plan_stops(l_index).stop_id := l_stop_out_tab(l_stop_out_tab.FIRST).stop_id;
5198     END IF;
5199 
5200     l_index := x_plan_stops.NEXT(l_index);
5201   END LOOP;
5202 
5203 
5204   --  4.3 update trips to have lane_id from the plan
5205 
5206   IF l_debug_on THEN
5207     WSH_DEBUG_SV.log(l_module_name,'4.3 update lane_id on trips: x_plan_trips.COUNT: ', x_plan_trips.COUNT);
5208   END IF;
5209 
5210   create_update_plan_trips(
5211            p_phase        => 2,
5212            x_context      => x_context,
5213            x_plan_trips   => x_plan_trips,
5214            x_errors_tab   => x_errors_tab,
5215            x_return_status => l_return_status
5216           );
5217   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
5218                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
5219      x_return_status := l_return_status;
5220      RETURN;
5221   END IF;
5222 
5223 
5224 
5225   --  5. split lines in x_plan_details
5226   IF l_debug_on THEN
5227     WSH_DEBUG_SV.logmsg(l_module_name,'5. Split lines in x_plan_details');
5228   END IF;
5229 
5230   l_index := x_plan_details.FIRST;
5231   WHILE l_index IS NOT NULL LOOP
5232 
5233     IF x_plan_details(l_index).map_split_flag = 'Y' THEN
5234 
5235       l_dd_attrs.DELETE;
5236       l_dd_attrs(1).delivery_detail_id := x_plan_details(l_index).delivery_detail_id;
5237       l_dd_attrs(1).organization_id := x_plan_details(l_index).organization_id;
5238 
5239       l_dd_action_prms.caller      := 'WSH_TP_RELEASE';
5240       l_dd_action_prms.action_code := 'SPLIT-LINE';
5241       l_dd_action_prms.split_quantity := x_plan_details(l_index).mapped_quantity;
5242 
5243       -- WV changes
5244       -- Keep track of details that have WV frozen, but
5245       -- may have their WV changed due to splits.
5246       -- Need the ship from locations to log exceptions.
5247 
5248       IF x_plan_details(l_index).wv_frozen_flag = 'Y' THEN
5249          IF NOT x_context.wv_exception_details.exists(x_plan_details(l_index).delivery_detail_id) THEN
5250             x_context.wv_exception_details(x_plan_details(l_index).delivery_detail_id)
5251               := x_plan_details(l_index).ship_from_location_id;
5252               IF l_debug_on THEN
5253                  WSH_DEBUG_SV.log(l_module_name,'Log WV exception against detail', x_plan_details(l_index).delivery_detail_id);
5254               END IF;
5255          END IF;
5256       END IF;
5257 
5258 
5259       WSH_DELIVERY_DETAILS_GRP.delivery_detail_action(
5260         p_api_version_number => 1.0,
5261         p_init_msg_list      => FND_API.G_TRUE,
5262         p_commit             => FND_API.G_FALSE,
5263         x_return_status      => l_return_status,
5264         x_msg_count          => l_msg_count,
5265         x_msg_data           => l_msg_data,
5266         p_rec_attr_tab       => l_dd_attrs,
5267         p_action_prms        => l_dd_action_prms,
5268         x_defaults           => l_dd_defaults,
5269         x_action_out_rec     => l_dd_action_rec);
5270 
5271       l_interface_entity := 'WSH_DEL_DETAILS_INTERFACE';
5272       l_interface_id     := x_plan_details(l_index).dd_interface_id;
5273       l_message_name     := 'WSH_TP_F_LINE_SPLIT';
5274       wsh_util_core.api_post_call(
5275            p_return_status => l_return_status,
5276            x_num_warnings  => l_number_of_warnings,
5277            x_num_errors    => l_number_of_errors);
5278 
5279       x_plan_details(l_index).delivery_detail_id := l_dd_action_rec.result_id_tab(1);
5280 
5281     END IF;
5282 
5283     l_index := x_plan_details.NEXT(l_index);
5284   END LOOP;
5285 
5286 
5287 
5288   --  6. unassign lines in x_used_details and x_plan_details if need_unassignment is TRUE
5289   --      inbound/drop: add x_used_details to the split-delivery list if needed and invoke SPLIT-DELIVERY
5290   --                    and skip unassignment of x_plan_details
5291 
5292   IF l_debug_on THEN
5293     WSH_DEBUG_SV.log(l_module_name,'6. Unassign used details: x_used_details.COUNT: ', x_used_details.COUNT);
5294   END IF;
5295 
5296   l_dd_attrs.DELETE;
5297   l_work_index := 0;
5298 
5299   IF x_used_details.COUNT > 0 THEN
5300     l_index := x_used_details.FIRST;
5301     WHILE l_index IS NOT NULL LOOP
5302       IF x_used_details(l_index).need_unassignment THEN
5303         IF x_used_details(l_index).line_direction IN ('I', 'D') THEN
5304           -- inbound/drop
5305           l_sd_dd_count := l_sd_dd_count + 1;
5306           l_sd_dd_attrs(l_sd_dd_count).delivery_detail_id := x_used_details(l_index).delivery_detail_id;
5307           l_sd_dd_attrs(l_sd_dd_count).organization_id    := x_used_details(l_index).organization_id;
5308         ELSE
5309           -- outbound
5310           l_work_index := l_work_index + 1;
5311           l_dd_attrs(l_work_index).delivery_detail_id := x_used_details(l_index).delivery_detail_id;
5312           l_dd_attrs(l_work_index).organization_id    := x_used_details(l_index).organization_id;
5313         END IF;
5314       END IF;
5315 
5316       l_index := x_used_details.NEXT(l_index);
5317     END LOOP;
5318 
5319     IF l_debug_on THEN
5320       WSH_DEBUG_SV.log(l_module_name,'after used details, l_dd_attrs.COUNT', l_dd_attrs.COUNT);
5321       WSH_DEBUG_SV.log(l_module_name,'after used details, l_sd_dd_count', l_sd_dd_count);
5322     END IF;
5323   END IF;
5324 
5325   l_index := x_plan_details.FIRST;
5326   WHILE l_index IS NOT NULL LOOP
5327     IF x_plan_details(l_index).current_delivery_id <>
5328          NVL(x_plan_deliveries(x_plan_details(l_index).target_delivery_index).delivery_id, -1) THEN
5329 
5330       -- inbound/drop lines will be taken care of in step 8 with SPLIT-DELIVERY.
5331       IF NVL(x_plan_details(l_index).line_direction, 'O') IN ('O', 'IO') THEN
5332         l_work_index := l_work_index + 1;
5333         l_dd_attrs(l_work_index).delivery_detail_id := x_plan_details(l_index).delivery_detail_id;
5334         l_dd_attrs(l_work_index).organization_id    := x_plan_details(l_index).organization_id;
5335       END IF;
5336 
5337       -- WV changes
5338       -- Keep track of deliveries that have WV frozen, but
5339       -- would have their WV changed due to unassignments.
5340       -- Need the ship from locations to log exceptions.
5341 
5342 
5343       IF NOT x_context.wv_exception_dels.exists(x_plan_details(l_index).current_delivery_id) THEN
5344 
5345          l_location_id := NULL;
5346          OPEN c_check_del_wv_frozen(x_plan_details(l_index).current_delivery_id);
5347          FETCH c_check_del_wv_frozen INTO l_location_id;
5348          CLOSE c_check_del_wv_frozen;
5349 
5350          IF l_location_id IS NOT NULL THEN
5351             x_context.wv_exception_dels(x_plan_details(l_index).current_delivery_id) :=  l_location_id;
5352             IF l_debug_on THEN
5353                WSH_DEBUG_SV.log(l_module_name,'Log WV exception against delivery', x_plan_details(l_index).current_delivery_id);
5354             END IF;
5355           END IF;
5356 
5357       END IF;
5358 
5359       -- Handles the case where the delivery wv may not be frozen, but it may
5360       -- have a stop that might have its wv frozen.
5361 
5362       FOR stop in c_check_del_stop_wv_frozen(x_plan_details(l_index).current_delivery_id) LOOP
5363           IF NOT x_context.wv_exception_stops.exists(stop.stop_id) THEN
5364              x_context.wv_exception_stops(stop.stop_id) := stop.stop_location_id;
5365              IF l_debug_on THEN
5366                 WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', stop.stop_id);
5367              END IF;
5368           END IF;
5369       END LOOP;
5370 
5371     END IF;
5372     l_index := x_plan_details.NEXT(l_index);
5373   END LOOP;
5374 
5375   IF l_debug_on THEN
5376     WSH_DEBUG_SV.log(l_module_name,'after plan details, l_dd_attrs.COUNT', l_dd_attrs.COUNT);
5377   END IF;
5378 
5379   IF l_dd_attrs.COUNT > 0 THEN
5380       l_dd_action_prms.caller      := 'WSH_TP_RELEASE';
5381       l_dd_action_prms.action_code := 'UNASSIGN';
5382       l_dd_action_prms.split_quantity := NULL;
5383 
5384       WSH_DELIVERY_DETAILS_GRP.delivery_detail_action(
5385         p_api_version_number => 1.0,
5386         p_init_msg_list      => FND_API.G_TRUE,
5387         p_commit             => FND_API.G_FALSE,
5388         x_return_status      => l_return_status,
5389         x_msg_count          => l_msg_count,
5390         x_msg_data           => l_msg_data,
5391         p_rec_attr_tab       => l_dd_attrs,
5392         p_action_prms        => l_dd_action_prms,
5393         x_defaults           => l_dd_defaults,
5394         x_action_out_rec     => l_dd_action_rec);
5395 
5396 
5397       IF l_debug_on THEN
5398         WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
5399         WSH_DEBUG_SV.log(l_module_name, 'l_dd_action_rec.valid_id_tab.COUNT', l_dd_action_rec.valid_id_tab.COUNT);
5400         WSH_DEBUG_SV.log(l_module_name, 'l_dd_attrs.COUNT', l_dd_attrs.COUNT);
5401       END IF;
5402 
5403       -- convert warning to error if the count of valid ids does not match.
5404       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
5405           AND l_dd_action_rec.valid_id_tab.COUNT < l_dd_attrs.COUNT) THEN
5406         IF l_debug_on THEN
5407           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
5408         END IF;
5409         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5410       END IF;
5411 
5412       l_interface_entity := 'NONE';
5413       l_interface_id     := -1;
5414       l_message_name     := 'WSH_TP_F_UNASSIGN_UNUSED';
5415       wsh_util_core.api_post_call(
5416            p_return_status => l_return_status,
5417            x_num_warnings  => l_number_of_warnings,
5418            x_num_errors    => l_number_of_errors);
5419   END IF;
5420 
5421   IF l_sd_dd_count > 0 THEN
5422       -- Inbound/drop: invoke SPLIT-DELIVERY on the unmapped lines/containers.
5423       l_dd_action_prms.caller         := 'WSH_TP_RELEASE';
5424       l_dd_action_prms.action_code    := 'SPLIT_DELIVERY';
5425       l_dd_action_prms.split_quantity := NULL;
5426       l_dd_action_prms.delivery_id    := NULL;
5427 
5428       WSH_DELIVERY_DETAILS_GRP.delivery_detail_action(
5429         p_api_version_number => 1.0,
5430         p_init_msg_list      => FND_API.G_TRUE,
5431         p_commit             => FND_API.G_FALSE,
5432         x_return_status      => l_return_status,
5433         x_msg_count          => l_msg_count,
5434         x_msg_data           => l_msg_data,
5435         p_rec_attr_tab       => l_sd_dd_attrs,
5436         p_action_prms        => l_dd_action_prms,
5437         x_defaults           => l_dd_defaults,
5438         x_action_out_rec     => l_dd_action_rec);
5439 
5440 
5441       IF l_debug_on THEN
5442         WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
5443         WSH_DEBUG_SV.log(l_module_name, 'l_dd_action_rec.valid_id_tab.COUNT', l_dd_action_rec.valid_id_tab.COUNT);
5444         WSH_DEBUG_SV.log(l_module_name, 'l_sd_dd_attrs.COUNT', l_sd_dd_attrs.COUNT);
5445       END IF;
5446 
5447       -- convert warning to error if the count of valid ids does not match.
5448       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
5449           AND l_dd_action_rec.valid_id_tab.COUNT < l_sd_dd_attrs.COUNT) THEN
5450         IF l_debug_on THEN
5451           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
5452         END IF;
5453         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5454       END IF;
5455 
5456       l_interface_entity := 'NONE';
5457       l_interface_id     := -1;
5458       l_message_name     := 'WSH_TP_F_SPLITDEL_UNUSED';
5459       wsh_util_core.api_post_call(
5460            p_return_status => l_return_status,
5461            x_num_warnings  => l_number_of_warnings,
5462            x_num_errors    => l_number_of_errors);
5463   END IF;
5464 
5465 
5466   --  7. assign details in x_plan_details and x_track_conts to plan deliveries (if different from
5467   --     current deliveries)
5468   --     plan lines are unassigned if needed (topmost containers are covered in step 1).
5469   --       inbound/drop: plan lines and plan topmost containers in x_delivery_unassigns
5470   --       will use SPLIT-DELIVERY instead of ASSIGN.
5471   --     * validate that ship sets and SMC are completed in each delivery
5472   --       (if enforced in shipping parameters)
5473 
5474   IF l_debug_on THEN
5475     WSH_DEBUG_SV.logmsg(l_module_name,'7. Assign details to deliveries (unassigning as needed)');
5476   END IF;
5477 
5478   DECLARE
5479     l_work_details     plan_detail_tab_type;
5480     l_next_index   NUMBER;
5481     l_count        NUMBER;
5482     l_ship_params  wsh_shipping_params_pvt.parameter_rec_typ;
5483     l_valid_flag   BOOLEAN;
5484     l_unassign_index NUMBER := 0;
5485     l_unassign_details WSH_GLBL_VAR_STRCT_GRP.delivery_details_attr_tbl_type;
5486     l_unassign_deliveries WSH_UTIL_CORE.id_tab_type;
5487 
5488   BEGIN
5489     -- make a working copy of details that need assignments
5490     IF l_debug_on THEN
5491       WSH_DEBUG_SV.logmsg(l_module_name, 'build working list of details to assign and unassign');
5492     END IF;
5493 
5494     l_index := x_plan_details.FIRST;
5495     l_work_index := 0;
5496 
5497     -- go through unpacked lines to be unassigned and assigned
5498     WHILE l_index IS NOT NULL LOOP
5499       IF (x_plan_details(l_index).topmost_cont_id IS NULL)
5500          AND (
5501                  (x_plan_details(l_index).current_delivery_id IS NULL)
5502               OR (x_plan_details(l_index).current_delivery_id
5503                   <> x_plan_deliveries(x_plan_details(l_index).target_delivery_index).delivery_id))  THEN
5504         l_work_index := l_work_index + 1;
5505         l_work_details(l_work_index) := x_plan_details(l_index);
5506         x_plan_deliveries(x_plan_details(l_index).target_delivery_index).assign_details_count :=
5507             x_plan_deliveries(x_plan_details(l_index).target_delivery_index).assign_details_count + 1;
5508 
5509         IF x_plan_details(l_index).current_delivery_id IS NOT NULL THEN
5510           -- if assigned, add to the list to be unassigned
5511           -- this will loop at most once.
5512           IF l_debug_on THEN
5513             WSH_DEBUG_SV.log(l_module_name, 'check whether to unassign delivery_detail_id', x_plan_details(l_index).delivery_detail_id);
5514           END IF;
5515 
5516           IF x_plan_details(l_index).line_direction IN ('O', 'IO') THEN
5517             -- only outbound lines need to be unassigned
5518             l_unassign_index := l_unassign_index + 1;
5519             l_unassign_details(l_unassign_index).delivery_detail_id := x_plan_details(l_index).delivery_detail_id;
5520             l_unassign_details(l_unassign_index).organization_id := x_plan_details(l_index).organization_id;
5521           END IF;
5522 
5523           -- WV changes
5524           -- Keep track of deliveries that have WV frozen, but
5525           -- would have their WV changed due to unassignments.
5526           -- Need the ship from locations to log exceptions.
5527 
5528 
5529           IF NOT x_context.wv_exception_dels.exists(x_plan_details(l_index).current_delivery_id) THEN
5530 
5531                l_location_id := NULL;
5532                OPEN c_check_del_wv_frozen(x_plan_details(l_index).current_delivery_id);
5533                FETCH c_check_del_wv_frozen INTO l_location_id;
5534                CLOSE c_check_del_wv_frozen;
5535 
5536                IF l_location_id IS NOT NULL THEN
5537                   x_context.wv_exception_dels(x_plan_details(l_index).current_delivery_id) :=  l_location_id;
5538                   IF l_debug_on THEN
5539                      WSH_DEBUG_SV.log(l_module_name,'Log WV exception against delivery', x_plan_details(l_index).current_delivery_id);
5540                   END IF;
5541 
5542                END IF;
5543             END IF;
5544 
5545             -- Handles the case where the delivery wv may not be frozen, but it may
5546             -- have a stop that might have its wv frozen.
5547 
5548             FOR stop in c_check_del_stop_wv_frozen(x_plan_details(l_index).current_delivery_id) LOOP
5549                 IF NOT x_context.wv_exception_stops.exists(stop.stop_id) THEN
5550                    x_context.wv_exception_stops(stop.stop_id) := stop.stop_location_id;
5551                    IF l_debug_on THEN
5552                       WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', stop.stop_location_id);
5553                    END IF;
5554                 END IF;
5555             END LOOP;
5556 
5557         END IF;
5558 
5559       END IF;
5560       l_index := x_plan_details.NEXT(l_index);
5561     END LOOP;
5562 
5563     IF l_debug_on THEN
5564       WSH_DEBUG_SV.log(l_module_name, 'unassign: l_unassign_details.COUNT', l_unassign_details.COUNT);
5565     END IF;
5566 
5567     -- unassign lines that need to be unassigned
5568     IF l_unassign_details.COUNT > 0 THEN
5569       -- there are details to unassign before we can assign them to the delivery mapped by plan.
5570       l_dd_action_prms.caller      := 'WSH_TP_RELEASE';
5571       l_dd_action_prms.action_code := 'UNASSIGN';
5572       l_dd_action_prms.split_quantity := NULL;
5573       l_dd_action_prms.delivery_id     := NULL;
5574 
5575       WSH_DELIVERY_DETAILS_GRP.delivery_detail_action(
5576           p_api_version_number => 1.0,
5577           p_init_msg_list      => FND_API.G_TRUE,
5578           p_commit             => FND_API.G_FALSE,
5579           x_return_status      => l_return_status,
5580           x_msg_count          => l_msg_count,
5581           x_msg_data           => l_msg_data,
5582           p_rec_attr_tab       => l_unassign_details,
5583           p_action_prms        => l_dd_action_prms,
5584           x_defaults           => l_dd_defaults,
5585           x_action_out_rec     => l_dd_action_rec);
5586 
5587       IF l_debug_on THEN
5588         WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
5589         WSH_DEBUG_SV.log(l_module_name, 'l_dd_action_rec.valid_id_tab.COUNT', l_dd_action_rec.valid_id_tab.COUNT);
5590         WSH_DEBUG_SV.log(l_module_name, 'l_unassign_details.COUNT', l_unassign_details.COUNT);
5591       END IF;
5592 
5593       -- convert warning to error if the count of valid ids does not match.
5594       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
5595           AND l_dd_action_rec.valid_id_tab.COUNT < l_unassign_details.COUNT) THEN
5596         IF l_debug_on THEN
5597           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
5598         END IF;
5599         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5600       END IF;
5601 
5602       l_interface_entity := 'NONE';
5603       l_interface_id     := -1;
5604       l_message_name     := 'WSH_TP_F_UNASSIGNMENTS';
5605       wsh_util_core.api_post_call(
5606              p_return_status => l_return_status,
5607              x_num_warnings  => l_number_of_warnings,
5608              x_num_errors    => l_number_of_errors);
5609     END IF;
5610 
5611 
5612 
5613     -- go through containers if they exist
5614     -- (containers will already have been unassigned if outbound and needed)
5615     IF x_track_conts.COUNT > 0 THEN
5616 
5617       l_index := x_track_conts.FIRST;
5618       WHILE l_index IS NOT NULL LOOP
5619         IF   (x_track_conts(l_index).current_delivery_id IS NULL)
5620           OR (x_track_conts(l_index).current_delivery_id
5621                   <> x_plan_deliveries(x_track_conts(l_index).target_delivery_index).delivery_id)  THEN
5622           l_work_index := l_work_index + 1;
5623           l_work_details(l_work_index).delivery_detail_id    := x_track_conts(l_index).topmost_cont_id;
5624           l_work_details(l_work_index).target_delivery_index := x_track_conts(l_index).target_delivery_index;
5625           l_work_details(l_work_index).organization_id       := x_track_conts(l_index).organization_id;
5626 
5627           x_plan_deliveries(x_track_conts(l_index).target_delivery_index).assign_details_count :=
5628               x_plan_deliveries(x_track_conts(l_index).target_delivery_index).assign_details_count + 1;
5629 
5630         END IF;
5631         l_index := x_track_conts.NEXT(l_index);
5632       END LOOP;
5633 
5634     END IF;
5635 
5636 
5637     -- start assigning
5638     l_index := x_plan_deliveries.FIRST;
5639     WHILE l_index IS NOT NULL LOOP
5640       IF l_debug_on THEN
5641         WSH_DEBUG_SV.log(l_module_name, 'check for assignments in delivery', x_plan_deliveries(l_index).delivery_id);
5642         WSH_DEBUG_SV.log(l_module_name, 'count of details to assign', x_plan_deliveries(l_index).assign_details_count);
5643       END IF;
5644 
5645       IF x_plan_deliveries(l_index).assign_details_count > 0 THEN
5646         -- there are details to assign to this delivery
5647         l_dd_attrs.DELETE;
5648 
5649         l_dd_action_prms.caller      := 'WSH_TP_RELEASE';
5650         IF x_plan_deliveries(l_index).shipment_direction IN ('I', 'D')  THEN
5651           -- this action will unassign inbound/drop details from old deliveries to the mapped delivery
5652           -- without resetting their ship_from_location_id, ignore_for_planning, routing_request_id, etc.
5653           l_dd_action_prms.action_code := 'SPLIT_DELIVERY';
5654         ELSE
5655           l_dd_action_prms.action_code := 'ASSIGN';
5656         END IF;
5657         l_dd_action_prms.split_quantity := NULL;
5658         l_dd_action_prms.delivery_id := x_plan_deliveries(l_index).delivery_id;
5659 
5660         -- WV changes
5661         -- Keep track of deliveries that have WV frozen, but
5662         -- would have their WV changed due to assignments.
5663         -- Need the ship from locations to log exceptions.
5664 
5665         IF x_plan_deliveries(l_index).wv_frozen_flag = 'Y' THEN
5666            IF NOT x_context.wv_exception_dels.exists(x_plan_deliveries(l_index).delivery_id) THEN
5667               x_context.wv_exception_dels(x_plan_deliveries(l_index).delivery_id) := x_plan_deliveries(l_index).initial_pickup_location_id;
5668               IF l_debug_on THEN
5669                  WSH_DEBUG_SV.log(l_module_name,'Log WV exception against delivery', x_plan_deliveries(l_index).delivery_id);
5670               END IF;
5671            END IF;
5672         END IF;
5673         -- Handles the case where the delivery wv may not be frozen, but it may
5674         -- have a stop that might have its wv frozen.
5675 
5676         FOR stop in c_check_del_stop_wv_frozen(x_plan_deliveries(l_index).delivery_id) LOOP
5677             IF NOT x_context.wv_exception_stops.exists(stop.stop_id) THEN
5678                x_context.wv_exception_stops(stop.stop_id) := stop.stop_location_id;
5679                IF l_debug_on THEN
5680                   WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', stop.stop_id);
5681                END IF;
5682             END IF;
5683         END LOOP;
5684 
5685         l_count      := 0;
5686         l_work_index := l_work_details.FIRST;
5687         WHILE     (l_work_index IS NOT NULL)
5688               AND (l_count < x_plan_deliveries(l_index).assign_details_count) LOOP
5689 
5690           l_next_index := l_work_details.NEXT(l_work_index);
5691 
5692           IF l_work_details(l_work_index).target_delivery_index = l_index THEN
5693              l_dd_attrs(l_dd_attrs.COUNT + 1).delivery_detail_id := l_work_details(l_work_index).delivery_detail_id;
5694              l_dd_attrs(l_dd_attrs.COUNT).organization_id := l_work_details(l_work_index).organization_id;
5695              l_count := l_count + 1;
5696              l_work_details.DELETE(l_work_index);
5697           END IF;
5698 
5699           l_work_index := l_next_index;
5700         END LOOP;
5701 
5702         WSH_DELIVERY_DETAILS_GRP.delivery_detail_action(
5703           p_api_version_number => 1.0,
5704           p_init_msg_list      => FND_API.G_TRUE,
5705           p_commit             => FND_API.G_FALSE,
5706           x_return_status      => l_return_status,
5707           x_msg_count          => l_msg_count,
5708           x_msg_data           => l_msg_data,
5709           p_rec_attr_tab       => l_dd_attrs,
5710           p_action_prms        => l_dd_action_prms,
5711           x_defaults           => l_dd_defaults,
5712           x_action_out_rec     => l_dd_action_rec);
5713 
5714         IF l_debug_on THEN
5715           WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
5716           WSH_DEBUG_SV.log(l_module_name, 'l_dd_action_rec.valid_id_tab.COUNT', l_dd_action_rec.valid_id_tab.COUNT);
5717           WSH_DEBUG_SV.log(l_module_name, 'l_dd_attrs.COUNT', l_dd_attrs.COUNT);
5718         END IF;
5719 
5720         -- convert warning to error if the count of valid ids does not match.
5721         IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
5722             AND l_dd_action_rec.valid_id_tab.COUNT < l_dd_attrs.COUNT) THEN
5723           IF l_debug_on THEN
5724             WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
5725           END IF;
5726           l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5727         END IF;
5728 
5729         l_interface_entity := 'WSH_NEW_DEL_INTERFACE';
5730         l_interface_id     := x_plan_deliveries(l_index).del_interface_id;
5731         l_message_name     := 'WSH_TP_F_ASSIGN_DEL';
5732         wsh_util_core.api_post_call(
5733              p_return_status => l_return_status,
5734              x_num_warnings  => l_number_of_warnings,
5735              x_num_errors    => l_number_of_errors);
5736       END IF;
5737 
5738       -- validate ship sets and smc are completed if enforced
5739       IF l_debug_on THEN
5740         WSH_DEBUG_SV.log(l_module_name, 'check if we enforce ship sets and SMC in organization', x_plan_deliveries(l_index).organization_id);
5741       END IF;
5742 
5743       wsh_shipping_params_pvt.get(
5744                    p_organization_id => x_plan_deliveries(l_index).organization_id,
5745                    x_param_info      => l_ship_params,
5746                    x_return_status   => l_return_status);
5747 
5748       l_interface_entity := 'WSH_NEW_DEL_INTERFACE';
5749       l_interface_id     := x_plan_deliveries(l_index).del_interface_id;
5750       l_message_name     := 'WSH_TP_F_SHIP_PARAMS';
5751       wsh_util_core.api_post_call(
5752            p_return_status => l_return_status,
5753            x_num_warnings  => l_number_of_warnings,
5754            x_num_errors    => l_number_of_errors);
5755 
5756 
5757       IF l_debug_on THEN
5758         WSH_DEBUG_SV.log(l_module_name, 'l_ship_params.enforce_ship_set_and_smc', l_ship_params.enforce_ship_set_and_smc);
5759       END IF;
5760 
5761       IF l_ship_params.enforce_ship_set_and_smc = 'Y' THEN
5762 
5763         wsh_tpa_delivery_pkg.check_ship_set(
5764                p_delivery_id   => x_plan_deliveries(l_index).delivery_id,
5765                x_valid_flag    => l_valid_flag,
5766                x_return_status => l_return_status);
5767 
5768         IF NOT l_valid_flag THEN
5769           l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5770           l_interface_entity := 'WSH_NEW_DEL_INTERFACE';
5771           l_interface_id     := x_plan_deliveries(l_index).del_interface_id;
5772           l_message_name     := 'WSH_TP_F_BROKEN_SHIP_SET';
5773           wsh_util_core.api_post_call(
5774              p_return_status => l_return_status,
5775              x_num_warnings  => l_number_of_warnings,
5776              x_num_errors    => l_number_of_errors);
5777         END IF;
5778 
5779         wsh_tpa_delivery_pkg.check_smc(
5780                p_delivery_id   => x_plan_deliveries(l_index).delivery_id,
5781                x_valid_flag    => l_valid_flag,
5782                x_return_status => l_return_status);
5783 
5784         IF NOT l_valid_flag THEN
5785           l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
5786           l_interface_entity := 'WSH_NEW_DEL_INTERFACE';
5787           l_interface_id     := x_plan_deliveries(l_index).del_interface_id;
5788           l_message_name     := 'WSH_TP_F_BROKEN_SMC';
5789           wsh_util_core.api_post_call(
5790              p_return_status => l_return_status,
5791              x_num_warnings  => l_number_of_warnings,
5792              x_num_errors    => l_number_of_errors);
5793         END IF;
5794 
5795       END IF;
5796 
5797       l_index := x_plan_deliveries.NEXT(l_index);
5798     END LOOP;
5799 
5800   END;
5801 
5802 
5803 
5804   --  8. update delivery details with TP attributes
5805 
5806   IF l_debug_on THEN
5807     WSH_DEBUG_SV.log(l_module_name,'8. update delivery details with TP attributes: x_plan_details.COUNT: ', x_plan_details.COUNT);
5808   END IF;
5809 
5810     -- avoid overhead of calling group API to update one column
5811     l_index := x_plan_details.FIRST;
5812     WHILE l_index IS NOT NULL LOOP
5813       UPDATE wsh_delivery_details
5814       SET tp_delivery_detail_id  = x_plan_details(l_index).tp_delivery_detail_id,
5815           last_update_date       = SYSDATE,
5816           last_updated_by        = FND_GLOBAL.USER_ID
5817       WHERE delivery_detail_id = x_plan_details(l_index).delivery_detail_id;
5818       l_index := x_plan_details.NEXT(l_index);
5819     END LOOP;
5820 
5821 
5822   -- 9. assign deliveries to trips
5823 
5824   IF l_debug_on THEN
5825     WSH_DEBUG_SV.log(l_module_name,'9. Assign deliveries to trips: x_plan_legs.COUNT: ', x_plan_legs.COUNT);
5826   END IF;
5827 
5828   l_index := x_plan_legs.FIRST;
5829   WHILE l_index IS NOT NULL LOOP
5830 
5831     IF (x_plan_legs(l_index).delivery_leg_id IS NULL)  THEN
5832 
5833       l_del_attrs.DELETE;
5834       l_del_action_prms.caller      := 'WSH_TP_RELEASE';
5835       l_del_action_prms.action_code := 'ASSIGN-TRIP';
5836 
5837       l_del_attrs(1).delivery_id        := x_plan_deliveries( x_plan_legs(l_index).delivery_index     ).delivery_id;
5838       l_del_attrs(1).organization_id    := x_plan_deliveries( x_plan_legs(l_index).delivery_index     ).organization_id;
5839 
5840       l_del_action_prms.trip_id          := x_plan_trips( x_plan_legs(l_index).trip_index         ).trip_id;
5841       l_del_action_prms.pickup_stop_id   := x_plan_stops( x_plan_legs(l_index).pickup_stop_index  ).stop_id;
5842       l_del_action_prms.pickup_loc_id    := x_plan_stops( x_plan_legs(l_index).pickup_stop_index  ).stop_location_id;
5843       l_del_action_prms.pickup_stop_seq  := x_plan_stops( x_plan_legs(l_index).pickup_stop_index  ).stop_sequence_number;
5844       l_del_action_prms.pickup_arr_date  := x_plan_stops( x_plan_legs(l_index).pickup_stop_index  ).planned_arrival_date;
5845       l_del_action_prms.pickup_dep_date  := x_plan_stops( x_plan_legs(l_index).pickup_stop_index  ).planned_departure_date;
5846       l_del_action_prms.pickup_stop_status := 'OP';
5847       l_del_action_prms.dropoff_stop_id  := x_plan_stops( x_plan_legs(l_index).dropoff_stop_index ).stop_id;
5848       l_del_action_prms.dropoff_loc_id   := x_plan_stops( x_plan_legs(l_index).dropoff_stop_index  ).stop_location_id;
5849       l_del_action_prms.dropoff_stop_seq := x_plan_stops( x_plan_legs(l_index).dropoff_stop_index  ).stop_sequence_number;
5850       l_del_action_prms.dropoff_arr_date := x_plan_stops( x_plan_legs(l_index).dropoff_stop_index  ).planned_arrival_date;
5851       l_del_action_prms.dropoff_dep_date := x_plan_stops( x_plan_legs(l_index).dropoff_stop_index  ).planned_departure_date;
5852       l_del_action_prms.dropoff_stop_status := 'OP';
5853 
5854       -- WV changes
5855       -- Keep track of stops that have WV frozen, but
5856       -- would have their WV changed due to assignments.
5857       -- Need the stop locations to log exceptions.
5858 
5859       IF x_plan_stops(x_plan_legs(l_index).pickup_stop_index).wv_frozen_flag = 'Y' THEN
5860          IF NOT x_context.wv_exception_stops.exists(x_plan_stops(x_plan_legs(l_index).pickup_stop_index).stop_id) THEN
5861             x_context.wv_exception_stops(x_plan_stops(x_plan_legs(l_index).pickup_stop_index).stop_id) :=
5862              x_plan_stops(x_plan_legs(l_index).pickup_stop_index).stop_location_id;
5863             IF l_debug_on THEN
5864                WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', x_plan_stops(x_plan_legs(l_index).pickup_stop_index).stop_id);
5865             END IF;
5866          END IF;
5867       END IF;
5868       IF x_plan_stops(x_plan_legs(l_index).dropoff_stop_index).wv_frozen_flag = 'Y' THEN
5869          IF NOT x_context.wv_exception_stops.exists(x_plan_stops(x_plan_legs(l_index).dropoff_stop_index).stop_id) THEN
5870             x_context.wv_exception_stops(x_plan_stops(x_plan_legs(l_index).dropoff_stop_index).stop_id) :=
5871              x_plan_stops(x_plan_legs(l_index).dropoff_stop_index).stop_location_id;
5872             IF l_debug_on THEN
5873                WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', x_plan_stops(x_plan_legs(l_index).dropoff_stop_index).stop_id);
5874             END IF;
5875          END IF;
5876       END IF;
5877 
5878       -- Also check the intermediate stops on the trip since they too would be affected by the assignment.
5879 
5880       FOR stop in c_check_intmed_stop_wv_frozen(p_trip_id => l_del_action_prms.trip_id,
5881                                                       p_pickup_seq_number  => l_del_action_prms.pickup_stop_seq,
5882                                                       p_dropoff_seq_number => l_del_action_prms.dropoff_stop_seq)
5883       LOOP
5884 
5885          IF NOT x_context.wv_exception_stops.exists(stop.stop_id) THEN
5886             x_context.wv_exception_stops(stop.stop_id) :=  stop.stop_location_id;
5887 
5888             IF l_debug_on THEN
5889                WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', stop.stop_id);
5890             END IF;
5891          END IF;
5892       END LOOP;
5893 
5894       WSH_DELIVERIES_GRP.delivery_action(
5895         p_api_version_number => 1.0,
5896         p_init_msg_list      => FND_API.G_TRUE,
5897         p_commit             => FND_API.G_FALSE,
5898         p_action_prms        => l_del_action_prms,
5899         p_rec_attr_tab       => l_del_attrs,
5900         x_delivery_out_rec   => l_del_action_rec,
5901         x_defaults_rec       => l_del_defaults,
5902         x_return_status      => l_return_status,
5903         x_msg_count          => l_msg_count,
5904         x_msg_data           => l_msg_data);
5905 
5906       l_interface_entity := 'WSH_DEL_LEGS_INTERFACE';
5907       l_interface_id     := x_plan_legs(l_index).leg_interface_id;
5908       l_message_name     := 'WSH_TP_F_ASSIGN_TRIP';
5909       wsh_util_core.api_post_call(
5910            p_return_status => l_return_status,
5911            x_num_warnings  => l_number_of_warnings,
5912            x_num_errors    => l_number_of_errors);
5913 
5914     END IF;
5915 
5916     l_index := x_plan_legs.NEXT(l_index);
5917   END LOOP;
5918 
5919 
5920 
5921   -- 10. reconciliate continuous moves (FTE)
5922 
5923   IF l_debug_on THEN
5924     WSH_DEBUG_SV.log(l_module_name,'10. reconciliate continuous moves: x_plan_moves.COUNT', x_plan_moves.COUNT);
5925     WSH_DEBUG_SV.log(l_module_name,'x_plan_trip_moves.COUNT', x_plan_trip_moves.COUNT);
5926     WSH_DEBUG_SV.log(l_module_name,'x_obsoleted_trip_moves.COUNT', x_obsoleted_trip_moves.COUNT);
5927   END IF;
5928 
5929   -- Bug 3555487 initialize message stack for each major action point.
5930   FND_MSG_PUB.initialize;
5931 
5932   WSH_FTE_TP_INTEGRATION.reconciliate_moves(
5933            x_context              => x_context,
5934            x_plan_trips           => x_plan_trips,
5935            x_plan_trip_moves      => x_plan_trip_moves,
5936            x_plan_moves           => x_plan_moves,
5937            x_obsoleted_trip_moves => x_obsoleted_trip_moves,
5938            x_errors_tab           => x_errors_tab,
5939            x_return_status        => l_return_status
5940           );
5941 
5942   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
5943                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
5944      x_return_status := l_return_status;
5945      IF l_debug_on THEN
5946        WSH_DEBUG_SV.log(l_module_name, 'reconcilate_moves failed: x_return_status', x_return_status);
5947        WSH_DEBUG_SV.pop(l_module_name);
5948      END IF;
5949      RETURN;
5950   END IF;
5951 
5952 
5953   -- 11. upgrade deliveries' planned_flag per the plan
5954 
5955   IF l_debug_on THEN
5956     WSH_DEBUG_SV.log(l_module_name,'11. Firm deliveries: x_plan_deliveries.COUNT', x_plan_deliveries.COUNT);
5957   END IF;
5958 
5959   DECLARE
5960      -- list of deliveries to become contents firmed.
5961      l_cf_dels   WSH_NEW_DELIVERIES_PVT.delivery_attr_tbl_type;
5962      -- list of deliveries to become routing and contents firmed.
5963      l_rcf_dels  WSH_NEW_DELIVERIES_PVT.delivery_attr_tbl_type;
5964      l_discard_return_status VARCHAR2(1);
5965   BEGIN  --{
5966 
5967     l_index := x_plan_deliveries.FIRST;
5968     WHILE l_index IS NOT NULL LOOP
5969       -- make lists of deliveries that need to be firmed without downgrading.
5970 
5971       IF     (x_plan_deliveries(l_index).planned_flag = 'Y')
5972          AND (NVL(x_plan_deliveries(l_index).wsh_planned_flag, 'N') = 'N') THEN
5973         copy_delivery_record(
5974             p_plan_delivery_rec  => x_plan_deliveries(l_index),
5975             x_delivery_attrs_rec => l_cf_dels(l_cf_dels.COUNT+1),
5976             x_return_status      => l_discard_return_status
5977         );
5978       ELSIF (x_plan_deliveries(l_index).planned_flag = 'F')
5979          AND (NVL(x_plan_deliveries(l_index).wsh_planned_flag, 'N') <> 'F') THEN
5980         copy_delivery_record(
5981             p_plan_delivery_rec  => x_plan_deliveries(l_index),
5982             x_delivery_attrs_rec => l_rcf_dels(l_rcf_dels.COUNT+1),
5983             x_return_status      => l_discard_return_status
5984         );
5985       END IF;
5986 
5987       l_index := x_plan_deliveries.NEXT(l_index);
5988     END LOOP;
5989 
5990     IF l_debug_on THEN
5991       WSH_DEBUG_SV.log(l_module_name,'Contents Firm deliveries: l_cf_dels.COUNT', l_cf_dels.COUNT);
5992     END IF;
5993 
5994     IF l_cf_dels.COUNT > 0 THEN
5995 
5996       l_del_action_prms.caller      := 'WSH_TP_RELEASE';
5997       l_del_action_prms.action_code := 'PLAN';
5998 
5999       l_del_action_prms.trip_id         := NULL;
6000       l_del_action_prms.pickup_stop_id  := NULL;
6001       l_del_action_prms.dropoff_stop_id := NULL;
6002 
6003       WSH_DELIVERIES_GRP.delivery_action(
6004         p_api_version_number => 1.0,
6005         p_init_msg_list      => FND_API.G_TRUE,
6006         p_commit             => FND_API.G_FALSE,
6007         p_action_prms        => l_del_action_prms,
6008         p_rec_attr_tab       => l_cf_dels,
6009         x_delivery_out_rec   => l_del_action_rec,
6010         x_defaults_rec       => l_del_defaults,
6011         x_return_status      => l_return_status,
6012         x_msg_count          => l_msg_count,
6013         x_msg_data           => l_msg_data);
6014 
6015       IF l_debug_on THEN
6016          WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
6017          WSH_DEBUG_SV.log(l_module_name, 'l_del_action_rec.valid_ids_tab.COUNT', l_del_action_rec.valid_ids_tab.COUNT);
6018          WSH_DEBUG_SV.log(l_module_name, 'l_cf_dels.COUNT', l_cf_dels.COUNT);
6019       END IF;
6020 
6021       -- convert warning to error if the count of valid ids does not match.
6022       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
6023           AND l_del_action_rec.valid_ids_tab.COUNT < l_cf_dels.COUNT) THEN
6024         IF l_debug_on THEN
6025           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
6026         END IF;
6027         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6028       END IF;
6029 
6030       l_interface_entity := 'NONE';
6031       l_interface_id     := -1;
6032       l_message_name     := 'WSH_TP_F_CF_DEL_FAILED';
6033       wsh_util_core.api_post_call(
6034              p_return_status => l_return_status,
6035              x_num_warnings  => l_number_of_warnings,
6036              x_num_errors    => l_number_of_errors);
6037     END IF;
6038 
6039 
6040     IF l_debug_on THEN
6041       WSH_DEBUG_SV.log(l_module_name,'Routing and Contents Firm deliveries: l_rcf_dels.COUNT', l_rcf_dels.COUNT);
6042     END IF;
6043 
6044     IF l_rcf_dels.COUNT > 0 THEN
6045 
6046       l_del_action_prms.caller      := 'WSH_TP_RELEASE';
6047       l_del_action_prms.action_code := 'FIRM';
6048 
6049       l_del_action_prms.trip_id         := NULL;
6050       l_del_action_prms.pickup_stop_id  := NULL;
6051       l_del_action_prms.dropoff_stop_id := NULL;
6052 
6053       WSH_DELIVERIES_GRP.delivery_action(
6054         p_api_version_number => 1.0,
6055         p_init_msg_list      => FND_API.G_TRUE,
6056         p_commit             => FND_API.G_FALSE,
6057         p_action_prms        => l_del_action_prms,
6058         p_rec_attr_tab       => l_rcf_dels,
6059         x_delivery_out_rec   => l_del_action_rec,
6060         x_defaults_rec       => l_del_defaults,
6061         x_return_status      => l_return_status,
6062         x_msg_count          => l_msg_count,
6063         x_msg_data           => l_msg_data);
6064 
6065       IF l_debug_on THEN
6066          WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
6067          WSH_DEBUG_SV.log(l_module_name, 'l_del_action_rec.valid_ids_tab.COUNT', l_del_action_rec.valid_ids_tab.COUNT);
6068          WSH_DEBUG_SV.log(l_module_name, 'l_rcf_dels.COUNT', l_rcf_dels.COUNT);
6069       END IF;
6070 
6071       -- convert warning to error if the count of valid ids does not match.
6072       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
6073           AND l_del_action_rec.valid_ids_tab.COUNT < l_rcf_dels.COUNT) THEN
6074         IF l_debug_on THEN
6075           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
6076         END IF;
6077         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6078       END IF;
6079 
6080       l_interface_entity := 'NONE';
6081       l_interface_id     := -1;
6082       l_message_name     := 'WSH_TP_F_RCF_DEL_FAILED';
6083       wsh_util_core.api_post_call(
6084              p_return_status => l_return_status,
6085              x_num_warnings  => l_number_of_warnings,
6086              x_num_errors    => l_number_of_errors);
6087     END IF;
6088 
6089   END;  --}
6090 
6091 
6092 
6093   -- 12. upgrade trips' planned_flag per the plan
6094 
6095   IF l_debug_on THEN
6096     WSH_DEBUG_SV.log(l_module_name,'12. Firm trips: x_plan_trips.COUNT', x_plan_trips.COUNT);
6097   END IF;
6098 
6099   DECLARE
6100      -- list of trips to become routing firmed.
6101      l_rf_trips   WSH_TRIPS_PVT.trip_attr_tbl_type;
6102      -- list of trips to become routing and contents firmed.
6103      l_rcf_trips  WSH_TRIPS_PVT.trip_attr_tbl_type;
6104   BEGIN  --{
6105 
6106     l_index := x_plan_trips.FIRST;
6107     WHILE l_index IS NOT NULL LOOP
6108       -- make lists of deliveries that need to be firmed without downgrading.
6109 
6110       IF     (x_plan_trips(l_index).planned_flag = 'Y')
6111          AND (NVL(x_plan_trips(l_index).wsh_planned_flag, 'N') = 'N') THEN
6112         copy_trip_record(
6113             p_plan_trip_rec  => x_plan_trips(l_index),
6114             x_trip_attrs_rec => l_rf_trips(l_rf_trips.COUNT+1)
6115         );
6116       ELSIF (x_plan_trips(l_index).planned_flag = 'F')
6117          AND (NVL(x_plan_trips(l_index).wsh_planned_flag, 'N') <> 'F') THEN
6118         copy_trip_record(
6119             p_plan_trip_rec  => x_plan_trips(l_index),
6120             x_trip_attrs_rec => l_rcf_trips(l_rcf_trips.COUNT+1)
6121         );
6122       END IF;
6123 
6124       l_index := x_plan_trips.NEXT(l_index);
6125     END LOOP;
6126 
6127     IF l_debug_on THEN
6128       WSH_DEBUG_SV.log(l_module_name,'Contents Firm trips: l_rf_trips.COUNT', l_rf_trips.COUNT);
6129     END IF;
6130 
6131     IF l_rf_trips.COUNT > 0 THEN
6132 
6133       l_trip_action_prms.caller      := 'WSH_TP_RELEASE';
6134       l_trip_action_prms.action_code := 'PLAN';
6135 
6136       wsh_trips_grp.trip_action(
6137          p_api_version_number =>  1.0,
6138          p_init_msg_list      =>  FND_API.G_TRUE,
6139          p_commit             =>  FND_API.G_FALSE,
6140          p_action_prms        =>  l_trip_action_prms,
6141          p_rec_attr_tab       =>  l_rf_trips,
6142          x_trip_out_rec       =>  l_trip_out_rec,
6143          x_def_rec            =>  l_trip_defaults,
6144          x_return_status      =>  l_return_status,
6145          x_msg_count          =>  l_msg_count,
6146          x_msg_data           =>  l_msg_data);
6147 
6148       IF l_debug_on THEN
6149          WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
6150          WSH_DEBUG_SV.log(l_module_name, 'l_trip_out_rec.valid_ids_tab.COUNT', l_trip_out_rec.valid_ids_tab.COUNT);
6151          WSH_DEBUG_SV.log(l_module_name, 'l_rf_trips.COUNT', l_rf_trips.COUNT);
6152       END IF;
6153 
6154       -- convert warning to error if the count of valid ids does not match.
6155       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
6156           AND l_trip_out_rec.valid_ids_tab.COUNT < l_rf_trips.COUNT) THEN
6157         IF l_debug_on THEN
6158           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
6159         END IF;
6160         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6161       END IF;
6162 
6163       l_interface_entity := 'NONE';
6164       l_interface_id     := -1;
6165       l_message_name     := 'WSH_TP_F_RF_TRIP_FAILED';
6166       wsh_util_core.api_post_call(
6167              p_return_status => l_return_status,
6168              x_num_warnings  => l_number_of_warnings,
6169              x_num_errors    => l_number_of_errors);
6170     END IF;
6171 
6172 
6173     IF l_debug_on THEN
6174       WSH_DEBUG_SV.log(l_module_name,'Routing and Contents Firm deliveries: l_rcf_trips.COUNT', l_rcf_trips.COUNT);
6175     END IF;
6176 
6177     IF l_rcf_trips.COUNT > 0 THEN
6178 
6179       l_trip_action_prms.caller      := 'WSH_TP_RELEASE';
6180       l_trip_action_prms.action_code := 'FIRM';
6181 
6182       wsh_trips_grp.trip_action(
6183          p_api_version_number =>  1.0,
6184          p_init_msg_list      =>  FND_API.G_TRUE,
6185          p_commit             =>  FND_API.G_FALSE,
6186          p_action_prms        =>  l_trip_action_prms,
6187          p_rec_attr_tab       =>  l_rcf_trips,
6188          x_trip_out_rec       =>  l_trip_out_rec,
6189          x_def_rec            =>  l_trip_defaults,
6190          x_return_status      =>  l_return_status,
6191          x_msg_count          =>  l_msg_count,
6192          x_msg_data           =>  l_msg_data);
6193 
6194       IF l_debug_on THEN
6195          WSH_DEBUG_SV.log(l_module_name, 'l_return_status', l_return_status);
6196          WSH_DEBUG_SV.log(l_module_name, 'l_trip_out_rec.valid_ids_tab.COUNT', l_trip_out_rec.valid_ids_tab.COUNT);
6197          WSH_DEBUG_SV.log(l_module_name, 'l_rcf_trips.COUNT', l_rcf_trips.COUNT);
6198       END IF;
6199 
6200       -- convert warning to error if the count of valid ids does not match.
6201       IF (l_return_status = WSH_UTIL_CORE.G_RET_STS_WARNING
6202           AND l_trip_out_rec.valid_ids_tab.COUNT < l_rcf_trips.COUNT) THEN
6203         IF l_debug_on THEN
6204           WSH_DEBUG_SV.LOG(l_module_name, 'converting return status', WSH_UTIL_CORE.G_RET_STS_ERROR);
6205         END IF;
6206         l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6207       END IF;
6208 
6209       l_interface_entity := 'NONE';
6210       l_interface_id     := -1;
6211       l_message_name     := 'WSH_TP_F_RCF_TRIP_FAILED';
6212       wsh_util_core.api_post_call(
6213              p_return_status => l_return_status,
6214              x_num_warnings  => l_number_of_warnings,
6215              x_num_errors    => l_number_of_errors);
6216     END IF;
6217 
6218   END;  --}
6219 
6220 
6221 
6222   -- 13. upgrade moves' planned_flag per the plan (FTE)
6223 
6224   IF l_debug_on THEN
6225     WSH_DEBUG_SV.log(l_module_name,'13. firm moves: x_plan_moves.COUNT', x_plan_moves.COUNT);
6226   END IF;
6227 
6228   WSH_FTE_TP_INTEGRATION.tp_firm_moves(
6229            x_context              => x_context,
6230            x_plan_moves           => x_plan_moves,
6231            x_errors_tab           => x_errors_tab,
6232            x_return_status        => l_return_status
6233           );
6234 
6235   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
6236                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
6237      x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6238      IF l_debug_on THEN
6239        WSH_DEBUG_SV.log(l_module_name, 'tp_firm_moves failed: l_return_status', l_return_status);
6240        WSH_DEBUG_SV.pop(l_module_name);
6241      END IF;
6242      RETURN;
6243   END IF;
6244 
6245 
6246   -- 14. call the rating API
6247 
6248   IF l_debug_on THEN
6249     WSH_DEBUG_SV.log(l_module_name,'14. call rating API: x_plan_trips.COUNT', x_plan_trips.COUNT);
6250   END IF;
6251 
6252   DECLARE
6253     l_rating_action_params WSH_FTE_INTEGRATION.rating_action_param_rec;
6254     -- [HIDING PROJECT] comment the table used for building the rank list.
6255     --l_trip_id_tab          WSH_UTIL_CORE.ID_TAB_TYPE;
6256     l_index                NUMBER;
6257     l_tab_index            NUMBER;
6258     l_return_status        VARCHAR2(1);
6259     l_msg_count            NUMBER;
6260     l_msg_data             VARCHAR2(32767);
6261   BEGIN
6262      l_tab_index := 0;
6263 
6264      l_rating_action_params.caller := 'WSH';
6265      l_rating_action_params.event  := 'TP-RELEASE';
6266      l_rating_action_params.action := 'RATE';
6267 
6268      l_index := x_plan_trips.FIRST;
6269      WHILE l_index IS NOT NULL LOOP
6270        l_tab_index := l_tab_index + 1;
6271        l_rating_action_params.trip_id_list(l_tab_index) := x_plan_trips(l_index).trip_id;
6272        --l_trip_id_tab(l_tab_index) := x_plan_trips(l_index).trip_id;
6273        l_index := x_plan_trips.NEXT(l_index);
6274      END LOOP;
6275 
6276      WSH_FTE_INTEGRATION.Rate_Trip (
6277              p_api_version   => 1.0,
6278              p_init_msg_list => FND_API.G_TRUE,
6279              p_action_params => l_rating_action_params,
6280              p_commit        => FND_API.G_FALSE,
6281              x_return_status => l_return_status,
6282              x_msg_count     => l_msg_count,
6283              x_msg_data      => l_msg_data);
6284 
6285 
6286     -- Do not return or fail the plan if we cannot rate.
6287     -- If something goes wrong later, We may need to know about this one.
6288     IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
6289                            WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
6290        IF l_debug_on THEN
6291          WSH_DEBUG_SV.log(l_module_name, 'rate_trip failed: l_return_status', l_return_status);
6292        END IF;
6293 
6294        stamp_interface_error(
6295               p_group_id            => x_context.group_id,
6296               p_entity_table_name   => 'NONE',
6297               p_entity_interface_id => -1,
6298               p_message_name        => 'WSH_TP_I_TRIP_RATE_FAILED',
6299               x_errors_tab          => x_errors_tab,
6300               x_return_status       => l_return_status);
6301     END IF;
6302 
6303 
6304     --[HIDING PROJECT] start
6305     -- below code is commented.
6306     -- 14.5 call the rank list API
6307     --
6308     --IF l_debug_on THEN
6309     --  WSH_DEBUG_SV.log(l_module_name,'14.5 call rank list API: l_trip_id_tab.COUNT', l_trip_id_tab.COUNT);
6310     --END IF;
6311     --
6312     --WSH_FTE_INTEGRATION.CREATE_RANK_LIST_BULK(
6313     --    p_api_version_number => 1,
6314     --    p_init_msg_list      => FND_API.G_TRUE,
6315     --    x_return_status      => l_return_status,
6316     --    x_msg_count          => l_msg_count,
6317     --    x_msg_data           => l_msg_data,
6318     --    p_source             => WSH_FTE_INTEGRATION.C_RANKLIST_SOURCE_TP,
6319     --    p_trip_id_tab        => l_trip_id_tab
6320     --);
6321     --
6322     ---- Do not return or fail the plan if we cannot rank.
6323     ---- If something goes wrong later, We may need to know about this one.
6324     --IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
6325     --                       WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
6326     --   IF l_debug_on THEN
6327     --     WSH_DEBUG_SV.log(l_module_name, 'create_rank_list_bulk failed: l_return_status', l_return_status);
6328     --   END IF;
6329     --
6330     --
6331     --   stamp_interface_error(
6332     --          p_group_id            => x_context.group_id,
6333     --          p_entity_table_name   => 'NONE',
6334     --          p_entity_interface_id => -1,
6335     --          p_message_name        => 'WSH_TP_I_TRIP_RANK_FAILED', --!!! new message
6336     --          x_errors_tab          => x_errors_tab,
6337     --          x_return_status       => l_return_status);
6338     --END IF;
6339     --[HIDING PROJECT] end
6340 
6341 
6342 
6343     l_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;  -- deliberately ignore rating outcome
6344 
6345 
6346   END;
6347 
6348 
6349 
6350   --
6351   -- J+ , call for Tendering Trips
6352   -- 15. call the Trip_Action API for tendering selected trips
6353 
6354   IF l_debug_on THEN
6355     WSH_DEBUG_SV.log(l_module_name,'15. call Auto Tender API: x_plan_trips.COUNT', x_plan_trips.COUNT);
6356     WSH_DEBUG_SV.log(l_module_name,'15. Auto Tender Flag: ', x_context.auto_tender_flag||'-'||x_return_status);
6357   END IF;
6358 
6359   -- Check if Profile Option is set
6360   -- TP owns this profile - "MST : Auto-tender on"
6361   -- Profile Value is cached in x_context.auto_tender_flag
6362   IF x_context.auto_tender_flag = 'Y' THEN
6363 
6364     DECLARE
6365       l_trip_action_params  WSH_FTE_INTEGRATION.wsh_trip_action_param_rec;
6366       l_trip_action_out_rec WSH_FTE_INTEGRATION.wsh_trip_action_out_rec;
6367       l_trip_id_tab         WSH_UTIL_CORE.id_tab_type;
6368       l_index                NUMBER;
6369       l_return_status        VARCHAR2(1);
6370       l_msg_count            NUMBER;
6371       l_msg_data             VARCHAR2(32767);
6372     BEGIN
6373        l_trip_action_params.action_code := 'TENDER';
6374 
6375        l_index := x_plan_trips.FIRST;
6376        WHILE l_index IS NOT NULL LOOP
6377          l_trip_id_tab(l_trip_id_tab.count + 1) := x_plan_trips(l_index).trip_id;
6378          IF l_debug_on THEN
6379            WSH_DEBUG_SV.log(l_module_name,'Trip id-',x_plan_trips(l_index).trip_id);
6380          END IF;
6381          l_index := x_plan_trips.NEXT(l_index);
6382        END LOOP;
6383 
6384        IF l_debug_on THEN
6385          WSH_DEBUG_SV.log(l_module_name, 'Trip Count', l_trip_id_tab.count);
6386        END IF;
6387 
6388        WSH_FTE_INTEGRATION.Trip_Action (
6389              p_api_version              => 1.0,
6390              p_init_msg_list            => FND_API.G_TRUE,  --??? or FALSE
6391              p_trip_id_tab              => l_trip_id_tab,
6392              p_action_params            => l_trip_action_params,
6393              p_commit                   => FND_API.G_FALSE,
6394              x_action_out_rec           => l_trip_action_out_rec,
6395              x_return_status            => l_return_status,
6396              x_msg_count                => l_msg_count,
6397              x_msg_data                 => l_msg_data);
6398 
6399        IF l_debug_on THEN
6400          WSH_DEBUG_SV.log(l_module_name,'After Trip Action Tender: l_return_status', l_return_status);
6401        END IF;
6402 
6403       -- If tendering process completed successfully or had a warning
6404       -- Always Log summarized information
6405       WSH_UTIL_CORE.Enable_Concurrent_Log_Print;
6406       -- New Messages to be Added as per DLD
6407       -- Number of Input = Number of Output --> Success
6408       IF l_trip_id_tab.count = l_trip_action_out_rec.valid_ids_tab.count THEN
6409         FND_MESSAGE.SET_NAME('WSH','WSH_ALL_TRIPS_TENDERED');
6410         FND_MESSAGE.SET_TOKEN('TOTAL_TRIPS',l_trip_id_tab.count);
6411         FND_MESSAGE.SET_TOKEN('SUCCESS_TRIPS',l_trip_action_out_rec.valid_ids_tab.count);
6412         WSH_UTIL_CORE.PrintMsg(fnd_message.get);
6413         -- Message type is set as error to make sure that this gets populated
6414         -- TP wants to see this log message for all return statuses
6415         wsh_util_core.add_message(WSH_UTIL_CORE.G_RET_STS_ERROR);
6416         IF l_debug_on THEN
6417           WSH_DEBUG_SV.log(l_module_name, 'Total Input Trips', l_trip_id_tab.count);
6418           WSH_DEBUG_SV.log(l_module_name, 'Successfully Tendered',l_trip_action_out_rec.valid_ids_tab.count);
6419         END IF;
6420       ELSE -- Number of Input <> Number of Output --> Error, partial success
6421         FND_MESSAGE.SET_NAME('WSH','WSH_PARTIAL_TRIPS_TENDERED');
6422         FND_MESSAGE.SET_TOKEN('TOTAL_TRIPS',l_trip_id_tab.count);
6423         FND_MESSAGE.SET_TOKEN('SUCCESS_TRIPS',l_trip_action_out_rec.valid_ids_tab.count);
6424         WSH_UTIL_CORE.PrintMsg(fnd_message.get);
6425         wsh_util_core.add_message(l_return_status);
6426 
6427         IF l_debug_on THEN
6428           WSH_DEBUG_SV.log(l_module_name, 'Total Input Trips', l_trip_id_tab.count);
6429           WSH_DEBUG_SV.log(l_module_name, 'Trips could not be Tendered');
6430         END IF;
6431       END IF;
6432 
6433       -- Do not return or fail the plan if we cannot Tender.
6434       IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
6435                            WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR) THEN
6436          -- do not set x_return_status as per l_return_status purposely
6437 
6438          stamp_interface_error(
6439               p_group_id            => x_context.group_id,
6440               p_entity_table_name   => 'NONE',
6441               p_entity_interface_id => -1,
6442               p_message_name        => 'WSH_TP_I_AUTOTENDER', --***** new message required
6443               x_errors_tab          => x_errors_tab,
6444               x_return_status       => l_return_status);
6445       END IF;
6446 
6447       l_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;  -- deliberately ignore Auto Tender outcome
6448       -- this is not set above
6449 
6450     END;
6451 
6452   END IF;  -- profile MST_AUTO_TENDER_ON is set
6453   -- End of J+ code
6454 
6455   --
6456   --
6457   IF l_debug_on THEN
6458      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
6459      WSH_DEBUG_SV.pop(l_module_name);
6460   END IF;
6461 
6462   EXCEPTION
6463     WHEN fnd_api.g_exc_error THEN
6464       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
6465       fnd_msg_pub.count_and_get(
6466           p_count   => l_msg_count,
6467           p_data    => l_msg_data,
6468           p_encoded => fnd_api.g_false);
6469       --
6470       stamp_interface_error(
6471           p_group_id            => x_context.group_id,
6472           p_entity_table_name   => l_interface_entity,
6473           p_entity_interface_id => l_interface_id,
6474           p_message_name        => l_message_name,
6475           x_errors_tab          => x_errors_tab,
6476           x_return_status       => l_return_status);
6477       --
6478       IF l_debug_on THEN
6479          wsh_debug_sv.logmsg(l_module_name, 'FND_API.G_EXC_ERROR exception has occured.', wsh_debug_sv.c_excep_level);
6480          wsh_debug_sv.pop(l_module_name, 'EXCEPTION:FND_API.G_EXC_ERROR');
6481       END IF;
6482       --
6483 
6484 
6485 
6486     WHEN fnd_api.g_exc_unexpected_error THEN
6487       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
6488       fnd_msg_pub.count_and_get(
6489          p_count    => l_msg_count,
6490          p_data     => l_msg_data,
6491          p_encoded  => fnd_api.g_false);
6492       --
6493       stamp_interface_error(
6494           p_group_id            => x_context.group_id,
6495           p_entity_table_name   => l_interface_entity,
6496           p_entity_interface_id => l_interface_id,
6497           p_message_name        => l_message_name,
6498           x_errors_tab          => x_errors_tab,
6499           x_return_status       => l_return_status);
6500       --
6501       IF l_debug_on THEN
6502          wsh_debug_sv.logmsg(l_module_name, 'FND_API.G_EXC_UNEXPECTED_ERROR exception has occured.', wsh_debug_sv.c_excep_level);
6503          wsh_debug_sv.pop(l_module_name, 'EXCEPTION:FND_API.G_EXC_UNEXPECTED_ERROR');
6504       END IF;
6505 
6506 
6507     WHEN OTHERS THEN
6508       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
6509       WSH_UTIL_CORE.DEFAULT_HANDLER(
6510                         'WSH_TP_RELEASE.RECONCILIATE_PLAN',
6511                         l_module_name);
6512 
6513       IF l_debug_on THEN
6514          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
6515          WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
6516       END IF;
6517 
6518 END reconciliate_plan;
6519 
6520 
6521 
6522 
6523 
6524 --
6525 --  Procedure:          plan_cleanup
6526 --  Parameters:
6527 --               x_context              context in this session
6528 --               x_plan_details         list of delivery details mapped to interface lines
6529 --               x_plan_deliveries      list of deliveries mapped to interface deliveries
6530 --               x_plan_stops           list of stops mapped to interface stops
6531 --               x_plan_trips           list of trips mapped to interface trips
6532 --               x_trip_unassigns       list of trip unassignments to check for empty stops/trips to delete
6533 --               x_obsoleted_trip_moves list of obsoleted moves that might have empty trips
6534 --                                      belonging to an obsoleted move.
6535 --               x_errors_tab           list of errors to insert into wsh_interface_errors at the end
6536 --               x_return_status        return status
6537 --
6538 --  Description:
6539 --               Clean up the shipping data not used by the plan and log exceptions as needed:
6540 --               1. delete empty deliveries; their trip assignments go into x_trip_unassigns.
6541 --                  We look at the deliveries from which we unassigned the plan lines.
6542 --                    Note: since we start with x_plan_details, we will not find the
6543 --                          trips that are deliberately empty per the plan (as part of
6544 --                          continuous moves) because at this time, the delivery unassignments
6545 --                          and unmatched stops' deletions will already have been performed.
6546 --               2. log exceptions against the lines where their dates do not agree with
6547 --                  their initial pick up stops or ultimate drop off stops' dates.
6548 --               3. clean up trip unassignments that result in empty stops/trips not used by plan,
6549 --                  and empty trips that may belong to obsoleted moves.
6550 --               4. refresh the plan stops' stop_id where they have linked stops, to ensure
6551 --                  that the interface stop records will point to the physical stops.
6552 --               5. update interface tables for details, deliveries, stops and trips
6553 --                  so that TP can identify the TE records mapped.
6554 --               6. Log exceptions against lines, deliveries, and stops that
6555 --                  have their weights and volumes frozen, but may have their
6556 --                  weights and volumes updated.
6557 PROCEDURE plan_cleanup(
6558            x_context                  IN OUT NOCOPY context_rec_type,
6559            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
6560            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
6561            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
6562            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
6563            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
6564            x_obsoleted_trip_moves     IN OUT NOCOPY WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type,
6565            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
6566            x_return_status               OUT NOCOPY VARCHAR2
6567           )
6568 IS
6569   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'PLAN_CLEANUP';
6570   --
6571   l_debug_on BOOLEAN;
6572 
6573   -- validate whether the delivery has a delivery detail.
6574   cursor c_empty_dels (p_del_id IN NUMBER) is
6575   select delivery_detail_id
6576   from wsh_delivery_assignments_v
6577   where delivery_id = p_del_id
6578   and delivery_id is not null
6579   and rownum = 1;
6580 
6581   -- look up delivery's legs/stops/trips
6582   cursor c_legs (p_del_id in number) is
6583   select wdl.delivery_leg_id,
6584          wdl.pick_up_stop_id,
6585          wdl.drop_off_stop_id,
6586          wts.trip_id
6587   from wsh_delivery_legs wdl,
6588        wsh_trip_stops wts
6589   where wdl.delivery_id = p_del_id
6590   and   wts.stop_id = wdl.pick_up_stop_id;
6591 
6592   l_del_checked_flag VARCHAR2(1);
6593   l_empty_dels WSH_UTIL_CORE.ID_TAB_TYPE;
6594   l_org_ids    WSH_UTIL_CORE.ID_TAB_TYPE;
6595   l_assigned_dels WSH_UTIL_CORE.ID_TAB_TYPE;
6596   l_empty_stops WSH_UTIL_CORE.ID_TAB_TYPE;
6597   l_empty_trips WSH_UTIL_CORE.ID_TAB_TYPE;
6598 
6599   -- look up initial pick up stop's departure date for the delivery.
6600   cursor c_initial_pu_date (p_delivery_id in number,
6601                             p_init_pu_loc_id in number) is
6602   select s.planned_departure_date
6603   from   wsh_trip_stops s,
6604          wsh_delivery_legs l
6605   where  s.stop_location_id = p_init_pu_loc_id
6606   and    s.stop_id = l.pick_up_stop_id
6607   and    l.delivery_id = p_delivery_id;
6608 
6609   -- look up ultimate drop off stop's arrival date for the delivery.
6610   cursor c_final_do_date(p_delivery_id in number,
6611                          p_ult_do_loc_id in number) is
6612   select s.planned_arrival_date
6613   from   wsh_trip_stops s,
6614          wsh_delivery_legs l
6615   where  s.stop_location_id = p_ult_do_loc_id
6616   and    s.stop_id = l.drop_off_stop_id
6617   and    l.delivery_id = p_delivery_id;
6618 
6619   -- look up the primary stop's arrival date for the delivery
6620   -- when its ultimate drop off location is dummy.
6621   cursor c_final_do_date_dummy(p_delivery_id in number,
6622                           p_ult_do_loc_id in number) is
6623   select s.planned_arrival_date
6624   from   wsh_trip_stops s,
6625          wsh_delivery_legs l
6626   where  s.stop_location_id = p_ult_do_loc_id
6627   and    s.stop_id = l.drop_off_stop_id
6628   and    l.delivery_id = p_delivery_id
6629   and    s.physical_stop_id IS NOT NULL
6630   UNION
6631   select phys_s.planned_arrival_date
6632   from   wsh_trip_stops s,
6633          wsh_trip_stops phys_s,
6634          wsh_delivery_legs l
6635   where  s.stop_location_id = p_ult_do_loc_id
6636   and    s.stop_id = l.drop_off_stop_id
6637   and    l.delivery_id = p_delivery_id
6638   and    phys_s.stop_id = s.physical_stop_id
6639   ;
6640 
6641   -- look for delivery lines in a delivery where
6642   -- their earliest pick up date is after the initial pick up date
6643   -- or their latest drop off date is before the ultimate drop off date.
6644   cursor c_dd_date_range (p_delivery_id in number, p_initial_pu_date in date, p_final_do_date in date) is
6645   select wdd.delivery_detail_id,
6646          wdd.ship_from_location_id,
6647          wdd.earliest_pickup_date,
6648          wdd.latest_dropoff_date
6649   from   wsh_delivery_details wdd,
6650          wsh_new_deliveries wnd,
6651          wsh_delivery_assignments_v wda
6652   where  wdd.delivery_detail_id = wda.delivery_detail_id
6653   and    wda.delivery_id = wnd.delivery_id
6654   and    wnd.delivery_id is not null
6655   and    (wdd.earliest_pickup_date > p_initial_pu_date or
6656           wdd.latest_dropoff_date  < p_final_do_date)
6657   and    wnd.delivery_id = p_delivery_id;
6658 
6659   -- check if trip has at least one remaining stop
6660   CURSOR c_exist_stop(p_trip_id NUMBER) IS
6661   SELECT wts.stop_id
6662   FROM WSH_TRIP_STOPS wts
6663   WHERE wts.trip_id = p_trip_id
6664   AND rownum = 1;
6665 
6666   -- check if trip has deliveries assigned.
6667   CURSOR c_exist_dels(p_trip_id NUMBER) IS
6668   SELECT wdl.delivery_leg_id
6669   FROM WSH_DELIVERY_LEGS wdl,
6670        WSH_TRIP_STOPS    wts
6671   WHERE wdl.pick_up_stop_id = wts.stop_id
6672   AND   wts.trip_id = p_trip_id
6673   AND rownum = 1
6674   UNION
6675   SELECT wdl.delivery_leg_id
6676   FROM WSH_DELIVERY_LEGS wdl,
6677        WSH_TRIP_STOPS    wts
6678   WHERE wdl.drop_off_stop_id = wts.stop_id
6679   AND   wts.trip_id = p_trip_id
6680   AND rownum = 1;
6681 
6682   -- WV changes
6683   -- Look for stops that have their wv frozen.
6684   CURSOR c_check_stop_wv_frozen(p_stop_id in NUMBER) IS
6685   select stop_location_id
6686   from wsh_trip_stops
6687   where wv_frozen_flag = 'Y'
6688   and stop_id = p_stop_id;
6689 
6690   -- CM Cleanup
6691   -- Look for stops on this trip
6692   CURSOR c_trip_stops(p_trip_id in NUMBER) IS
6693   SELECT stop_id
6694   FROM wsh_trip_stops
6695   WHERE trip_id = p_trip_id;
6696 
6697 
6698   -- physical stop lookup to use primary stop
6699   CURSOR c_lookup_physical_stop(p_stop_id IN NUMBER) IS
6700   SELECT NVL(wts.physical_stop_id, wts.stop_id)
6701   FROM   WSH_TRIP_STOPS wts
6702   WHERE  wts.stop_id = p_stop_id;
6703 
6704 
6705   l_location_id NUMBER;
6706 
6707   l_initial_pu_date DATE;
6708   l_final_do_date DATE;
6709   l_delivery_name VARCHAR2(30);
6710   l_msg VARCHAR2(2000);
6711   l_exception_error_message  VARCHAR2(2000) := NULL;
6712   l_exception_msg_count NUMBER;
6713   l_dummy_exception_id NUMBER;
6714   l_exception_msg_data VARCHAR2(4000) := NULL;
6715   l_return_status      VARCHAR2(1);
6716   l_temp_id            NUMBER;
6717   i                    NUMBER;
6718   j                    NUMBER;
6719   l_found              BOOLEAN;
6720 
6721 
6722   l_message_name       VARCHAR2(30);
6723   l_msg_data           VARCHAR2(32767);
6724   l_msg_count          NUMBER;
6725 
6726   l_del_attrs        WSH_NEW_DELIVERIES_PVT.delivery_attr_tbl_type;
6727   l_del_action_prms  WSH_DELIVERIES_GRP.action_parameters_rectype;
6728   l_del_action_rec   WSH_DELIVERIES_GRP.delivery_action_out_rec_type;
6729   l_del_defaults     WSH_DELIVERIES_GRP.default_parameters_rectype;
6730 
6731   l_stop_attrs       WSH_TRIP_STOPS_PVT.stop_attr_tbl_type;
6732   l_stop_action_prms WSH_TRIP_STOPS_GRP.action_parameters_rectype;
6733   l_stop_action_rec  WSH_TRIP_STOPS_GRP.StopActionOutRecType;
6734   l_stop_defaults    WSH_TRIP_STOPS_GRP.default_parameters_rectype;
6735 
6736   l_trip_attrs       WSH_TRIPS_PVT.trip_attr_tbl_type;
6737   l_trip_action_prms WSH_TRIPS_GRP.action_parameters_rectype;
6738   l_trip_in_rec      WSH_TRIPS_GRP.TripInRecType;
6739   l_trip_defaults    WSH_TRIPS_GRP.default_parameters_rectype;
6740   l_trip_out_rec     WSH_TRIPS_GRP.tripActionOutRecType;
6741   l_trip_out_tab     WSH_TRIPS_GRP.trip_out_tab_type;
6742 
6743   WSH_EMPTY_TRIP EXCEPTION;
6744 
6745 
6746   --
6747   --  Procedure:         examine_stop
6748   --  Parameters:
6749   --             p_stop_id             stop_id to examine
6750   --             p_trip_id             stop's trip_id
6751   --
6752   --  Description:
6753   --       Updates l_empty_stops and l_empty_trips:
6754   --         If this stop is empty, it is added to l_empty_stops.
6755   --         If the trip is new, add it to l_empty_trips to be examined later.
6756   PROCEDURE examine_stop(p_stop_id IN NUMBER,
6757                          p_trip_id IN NUMBER) IS
6758     -- check if stop has any delivery to pick up or drop off
6759     CURSOR c_stop_dels(p_stop_id NUMBER) IS
6760     SELECT wdl.delivery_leg_id
6761     FROM WSH_DELIVERY_LEGS wdl
6762     WHERE wdl.pick_up_stop_id = p_stop_id
6763     AND rownum = 1
6764     UNION
6765     SELECT wdl.delivery_leg_id
6766     FROM WSH_DELIVERY_LEGS wdl
6767     WHERE  wdl.drop_off_stop_id = p_stop_id
6768     AND rownum = 1;
6769 
6770     l_temp_id NUMBER;
6771     j       NUMBER;
6772     l_found BOOLEAN;
6773   BEGIN
6774     IF l_debug_on THEN
6775       WSH_DEBUG_SV.log(l_module_name, 'entering examine_stop: p_stop_id', p_stop_id);
6776       WSH_DEBUG_SV.log(l_module_name, 'p_trip_id', p_trip_id);
6777     END IF;
6778 
6779     -- check that this stop is not already in list of empty stops
6780     IF l_empty_stops.COUNT = 0 THEN
6781       j := 1;
6782       l_found := FALSE;
6783     ELSE
6784       l_found := FALSE;
6785       j := l_empty_stops.FIRST;
6786       WHILE j IS NOT NULL AND NOT l_found LOOP
6787         l_found := (l_empty_stops(j) = p_stop_id);
6788         j := l_empty_stops.NEXT(j);
6789       END LOOP;
6790     END IF;
6791 
6792     IF NOT l_found THEN
6793       -- check whether this stop is empty
6794       OPEN c_stop_dels(p_stop_id);
6795       FETCH c_stop_dels INTO l_temp_id;
6796       l_found := c_stop_dels%FOUND;
6797       CLOSE c_stop_dels;
6798 
6799       IF NOT l_found THEN
6800         -- this stop is empty.
6801         IF l_empty_stops.COUNT = 0 THEN
6802           l_empty_stops(1) := p_stop_id;
6803         ELSE
6804           l_empty_stops( l_empty_stops.LAST+1 ) := p_stop_id;
6805         END IF;
6806 
6807         -- add trip to list of possibly empty trips if not there already
6808         IF l_empty_trips.COUNT = 0 THEN
6809           l_empty_trips(1) := x_trip_unassigns(i).trip_id;
6810         ELSE
6811           j := l_empty_trips.FIRST;
6812           WHILE j IS NOT NULL AND NOT l_found LOOP
6813             l_found := (l_empty_trips(j) = p_trip_id);
6814             j := l_empty_trips.NEXT(j);
6815           END LOOP;
6816           IF NOT l_found THEN
6817             j := l_empty_trips.LAST + 1;
6818             l_empty_trips(j) := p_trip_id;
6819           END IF;
6820         END IF;
6821       END IF;
6822     END IF;
6823 
6824     IF l_debug_on THEN
6825       WSH_DEBUG_SV.logmsg(l_module_name, 'exiting examine_stop');
6826     END IF;
6827 
6828   EXCEPTION
6829     WHEN OTHERS THEN
6830       IF c_stop_dels%ISOPEN THEN
6831          CLOSE  c_stop_dels;
6832       END IF;
6833       IF l_debug_on THEN
6834         WSH_DEBUG_SV.logmsg(l_module_name, 'exiting examine_stop exception handler');
6835       END IF;
6836       RAISE;
6837   END examine_stop;
6838 
6839 
6840 BEGIN
6841   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
6842   --
6843   IF l_debug_on IS NULL THEN
6844     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
6845   END IF;
6846 
6847   IF l_debug_on THEN
6848     WSH_DEBUG_SV.push(l_module_name);
6849   END IF;
6850   --
6851   --
6852 
6853   -- Bug 3555487 initialize message stack for each major action point.
6854   FND_MSG_PUB.initialize;
6855 
6856 
6857   --
6858   -- 1. DELETE EMPTY DELIVERIES FROM WHICH PLAN LINES ARE UNASSIGNED.
6859   --    If empty deliveries are assigned to trips, add them to x_trip_unassigns.
6860   --
6861 
6862   IF l_debug_on THEN
6863     WSH_DEBUG_SV.logmsg(l_module_name, '1. purge empty deliveries');
6864   END IF;
6865 
6866   IF x_plan_details.COUNT > 0 THEN
6867     -- Find distinct empty deliveries and add them to l_empty_dels.
6868     --   We use l_assigned_dels to list the deliveries that we know have lines,
6869     --   so we do not have to keep querying them.
6870 
6871     FOR i in x_plan_details.FIRST.. x_plan_details.LAST LOOP
6872         l_del_checked_flag := 'N';
6873         IF x_plan_details(i).current_delivery_id <>
6874            x_plan_deliveries(x_plan_details(i).target_delivery_index).delivery_id
6875         THEN
6876 
6877            FOR j in 1.. l_empty_dels.count LOOP
6878              IF l_empty_dels(j) =  x_plan_details(i).current_delivery_id THEN
6879                 l_del_checked_flag := 'Y';
6880                 EXIT;
6881              END IF;
6882            END LOOP;
6883 
6884            IF l_del_checked_flag <> 'Y' THEN
6885               FOR j in 1.. l_assigned_dels.count LOOP
6886                 IF l_assigned_dels(j) =  x_plan_details(i).current_delivery_id THEN
6887                    l_del_checked_flag := 'Y';
6888                    EXIT;
6889                 END IF;
6890               END LOOP;
6891            END IF;
6892 
6893            IF l_del_checked_flag <> 'Y' THEN
6894               OPEN c_empty_dels (x_plan_details(i).current_delivery_id);
6895               FETCH c_empty_dels INTO l_temp_id;
6896               IF c_empty_dels%NOTFOUND THEN
6897                  l_empty_dels(l_empty_dels.count + 1) := x_plan_details(i).current_delivery_id;
6898                  l_org_ids(l_empty_dels.LAST)         := x_plan_details(i).organization_id;
6899               ELSE
6900                  l_assigned_dels(l_assigned_dels.count + 1) := x_plan_details(i).current_delivery_id;
6901               END IF;
6902               CLOSE c_empty_dels;
6903            END IF;
6904 
6905         END IF;
6906 
6907     END LOOP;
6908   END IF;
6909 
6910   IF l_debug_on THEN
6911     WSH_DEBUG_SV.log(l_module_name, 'Compiled lists: l_empty_dels.COUNT', l_empty_dels.COUNT);
6912     WSH_DEBUG_SV.log(l_module_name, 'l_assigned_dels.COUNT', l_assigned_dels.COUNT);
6913   END IF;
6914 
6915   --
6916   -- Go through the list of empty deliveries to unassign from trips and populate x_trip_unassigns.
6917   --
6918 
6919   l_del_action_prms.caller      := 'WSH_TP_RELEASE';
6920   l_del_action_prms.action_code := 'UNASSIGN-TRIP';
6921 
6922   FOR i in 1 .. l_empty_dels.count LOOP -- {
6923 
6924     IF l_debug_on THEN
6925       WSH_DEBUG_SV.log(l_module_name, 'looping empty delivery', l_empty_dels(i));
6926     END IF;
6927 
6928     FOR leg IN c_legs( l_empty_dels(i) ) LOOP
6929 
6930         l_del_attrs(1).delivery_id     := l_empty_dels(i);
6931         l_del_attrs(1).organization_id := l_org_ids(i);
6932         l_del_action_prms.trip_id := leg.trip_id;
6933 
6934         WSH_DELIVERIES_GRP.delivery_action(
6935           p_api_version_number => 1.0,
6936           p_init_msg_list      => FND_API.G_TRUE,
6937           p_commit             => FND_API.G_FALSE,
6938           p_action_prms        => l_del_action_prms,
6939           p_rec_attr_tab       => l_del_attrs,
6940           x_delivery_out_rec   => l_del_action_rec,
6941           x_defaults_rec       => l_del_defaults,
6942           x_return_status      => l_return_status,
6943           x_msg_count          => l_msg_count,
6944           x_msg_data           => l_msg_data);
6945         -- ignore return status
6946 
6947         IF x_trip_unassigns.COUNT = 0 THEN
6948           j := 1;
6949         ELSE
6950           j := x_trip_unassigns.LAST + 1;
6951         END IF;
6952         x_trip_unassigns(j).delivery_leg_id := leg.delivery_leg_id;
6953         x_trip_unassigns(j).trip_id         := leg.trip_id;
6954         x_trip_unassigns(j).pickup_stop_id  := leg.pick_up_stop_id;
6955         x_trip_unassigns(j).dropoff_stop_id := leg.drop_off_stop_id;
6956         x_trip_unassigns(j).delivery_id     := l_empty_dels(i);
6957         x_trip_unassigns(j).organization_id := l_org_ids(i);
6958 
6959         -- WV changes
6960         -- Add the stops with WV frozen that have deliveries unassigned from, to
6961         -- the list of stops to log wv exceptions.
6962 
6963         IF NOT x_context.wv_exception_stops.exists(x_trip_unassigns(j).pickup_stop_id) THEN
6964 
6965            l_location_id := NULL;
6966            OPEN c_check_stop_wv_frozen(x_trip_unassigns(j).pickup_stop_id);
6967            FETCH c_check_stop_wv_frozen INTO l_location_id;
6968            CLOSE c_check_stop_wv_frozen;
6969 
6970            IF l_location_id IS NOT NULL THEN
6971               x_context.wv_exception_stops(x_trip_unassigns(j).pickup_stop_id) :=  l_location_id;
6972               IF l_debug_on THEN
6973                  WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', x_trip_unassigns(j).pickup_stop_id);
6974               END IF;
6975            END IF;
6976         END IF;
6977 
6978         IF NOT x_context.wv_exception_stops.exists(x_trip_unassigns(j).dropoff_stop_id) THEN
6979 
6980            l_location_id := NULL;
6981            OPEN c_check_stop_wv_frozen(x_trip_unassigns(j).dropoff_stop_id);
6982            FETCH c_check_stop_wv_frozen INTO l_location_id;
6983            CLOSE c_check_stop_wv_frozen;
6984 
6985            IF l_location_id IS NOT NULL THEN
6986               x_context.wv_exception_stops(x_trip_unassigns(j).dropoff_stop_id) :=  l_location_id;
6987               IF l_debug_on THEN
6988                  WSH_DEBUG_SV.log(l_module_name,'Log WV exception against stop', x_trip_unassigns(j).dropoff_stop_id);
6989               END IF;
6990            END IF;
6991         END IF;
6992 
6993         -- WV changes
6994         -- Remove the empty deliveries (to be deleted) from
6995         -- from the WV exceptions list.
6996 
6997         IF x_context.wv_exception_dels.exists(l_empty_dels(i)) THEN
6998            x_context.wv_exception_dels.delete(l_empty_dels(i));
6999         END IF;
7000 
7001 
7002 
7003     END LOOP;
7004 
7005 
7006   END LOOP;
7007 
7008   -- Delete the list of empty deliveries.
7009 
7010   IF l_debug_on THEN
7011      WSH_DEBUG_SV.log(l_module_name,'before DELETE: l_empty_dels.COUNT', l_empty_dels.COUNT);
7012   END IF;
7013   WSH_UTIL_CORE.Delete(p_type => 'DLVY',
7014                        p_rows => l_empty_dels,
7015                        x_return_status => l_return_status);
7016   -- ignore return status
7017 
7018   --
7019   -- 2. LOG EXCEPTIONS IF DATES ARE OUT OF SYNC.
7020   --
7021 
7022   IF l_debug_on THEN
7023     WSH_DEBUG_SV.logmsg(l_module_name, '2. log exceptions if dates are out of sync');
7024   END IF;
7025 
7026   --
7027   -- Go through list of deliveries in plan comparing the dates and log
7028   -- exceptions in the following cases.
7029   -- dd.earliest_pickup_date > initial stop.planned_departure_date or
7030   -- dd.latest_drop_off_date < final stop.planned_arrival_date.
7031   --
7032 
7033   FOR i in 1..x_plan_deliveries.count LOOP
7034 
7035       OPEN c_initial_pu_date(x_plan_deliveries(i).delivery_id,
7036                              x_plan_deliveries(i).initial_pickup_location_id);
7037       FETCH c_initial_pu_date INTO l_initial_pu_date;
7038       CLOSE c_initial_pu_date;
7039 
7040       IF x_plan_deliveries(i).physical_ultimate_do_loc_id IS NULL THEN
7041         OPEN c_final_do_date(x_plan_deliveries(i).delivery_id,
7042                              x_plan_deliveries(i).ultimate_dropoff_location_id);
7043         FETCH c_final_do_date INTO l_final_do_date;
7044         CLOSE c_final_do_date;
7045       ELSE
7046         OPEN c_final_do_date_dummy(x_plan_deliveries(i).delivery_id,
7047                              x_plan_deliveries(i).ultimate_dropoff_location_id);
7048         FETCH c_final_do_date_dummy INTO l_final_do_date;
7049         CLOSE c_final_do_date_dummy;
7050       END IF;
7051 
7052       l_delivery_name := NULL;
7053 
7054 
7055       FOR dd IN c_dd_date_range(x_plan_deliveries(i).delivery_id, l_initial_pu_date, l_final_do_date) LOOP
7056 
7057          IF dd.earliest_pickup_date > l_initial_pu_date THEN
7058             -- Log exception.
7059             IF l_delivery_name is null THEN
7060                l_delivery_name := wsh_new_deliveries_pvt.get_name(x_plan_deliveries(i).delivery_id);
7061             END IF;
7062             FND_MESSAGE.SET_NAME('WSH', 'WSH_TP_EARLY_PICKUP');
7063             FND_MESSAGE.SET_TOKEN('SHIP_DATE',FND_DATE.DATE_TO_DISPLAYDT(l_initial_pu_date));
7064             FND_MESSAGE.SET_TOKEN('PICK_DATE',FND_DATE.DATE_TO_DISPLAYDT(dd.earliest_pickup_date));
7065             FND_MESSAGE.SET_TOKEN('DETAIL_ID',dd.delivery_detail_id);
7066             FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_delivery_name);
7067 
7068             l_msg := FND_MESSAGE.GET;
7069 
7070             l_dummy_exception_id := NULL;
7071 
7072             wsh_xc_util.log_exception(p_api_version => 1.0,
7073                x_return_status => l_return_status,
7074                x_msg_count => l_exception_msg_count,
7075                x_msg_data => l_exception_msg_data,
7076                x_exception_id => l_dummy_exception_id,
7077                p_exception_location_id => dd.ship_from_location_id,
7078                p_logged_at_location_id => dd.ship_from_location_id,
7079                p_logging_entity => 'SHIPPER',
7080                p_logging_entity_id => FND_GLOBAL.USER_ID,
7081                p_exception_name => 'WSH_TP_EARLY_PICKUP',
7082                p_message => l_msg,
7083                p_delivery_detail_id => dd.delivery_detail_id,
7084                p_error_message => l_exception_error_message);
7085          END IF;
7086 
7087          IF dd.latest_dropoff_date < l_final_do_date THEN
7088             -- Log exception.
7089             IF l_delivery_name is null THEN
7090                l_delivery_name := wsh_new_deliveries_pvt.get_name(x_plan_deliveries(i).delivery_id);
7091             END IF;
7092             FND_MESSAGE.SET_NAME('WSH', 'WSH_TP_LATE_DROPOFF');
7093             FND_MESSAGE.SET_TOKEN('DROP_DATE',FND_DATE.DATE_TO_DISPLAYDT(l_final_do_date));
7094             FND_MESSAGE.SET_TOKEN('DELIVER_DATE',FND_DATE.DATE_TO_DISPLAYDT(dd.latest_dropoff_date));
7095             FND_MESSAGE.SET_TOKEN('DETAIL_ID',dd.delivery_detail_id);
7096             FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_delivery_name);
7097 
7098             l_msg := FND_MESSAGE.GET;
7099 
7100             l_dummy_exception_id := NULL;
7101 
7102             wsh_xc_util.log_exception(p_api_version => 1.0,
7103                x_return_status => l_return_status,
7104                x_msg_count => l_exception_msg_count,
7105                x_msg_data => l_exception_msg_data,
7106                x_exception_id => l_dummy_exception_id,
7107                p_exception_location_id => dd.ship_from_location_id,
7108                p_logged_at_location_id => dd.ship_from_location_id,
7109                p_logging_entity => 'SHIPPER',
7110                p_logging_entity_id => FND_GLOBAL.USER_ID,
7111                p_exception_name => 'WSH_TP_LATE_DROPOFF',
7112                p_message => l_msg,
7113                p_delivery_detail_id => dd.delivery_detail_id,
7114                p_error_message => l_exception_error_message);
7115          END IF;
7116       END LOOP;
7117 
7118   END LOOP;
7119 
7120   --
7121   --  3. clean up trip unassignments that result in empty stops/trips not used by plan.
7122   --
7123 
7124   IF l_debug_on THEN
7125     WSH_DEBUG_SV.logmsg(l_module_name, '3. purge empty stops/trips');
7126   END IF;
7127 
7128   -- Add the obsoleted move trips to trip unassignment.
7129   -- Leave the pickup and dropoff stops null
7130   IF x_obsoleted_trip_moves.COUNT > 0 THEN
7131     FOR i in x_obsoleted_trip_moves.FIRST .. x_obsoleted_trip_moves.LAST LOOP
7132 
7133       IF   x_obsoleted_trip_moves(i).trip_id IS NOT NULL THEN
7134 
7135            x_trip_unassigns(x_trip_unassigns.count + 1).trip_id := x_obsoleted_trip_moves(i).trip_id;
7136 
7137       END IF;
7138 
7139     END LOOP;
7140   END IF;
7141 
7142   IF x_trip_unassigns.COUNT > 0 THEN
7143 
7144     IF l_debug_on THEN
7145       WSH_DEBUG_SV.log(l_module_name, 'Clean up trip unassignments: x_trip_unassigns.COUNT', x_trip_unassigns.COUNT);
7146     END IF;
7147 
7148     l_empty_stops.DELETE;
7149     l_empty_trips.DELETE;
7150 
7151     i := x_trip_unassigns.FIRST;
7152     WHILE i IS NOT NULL LOOP
7153 
7154       -- If unassignment is due to matching plan trips, the trip is part of the plan.
7155       -- Therefore, we look at only the trip unassignments
7156       -- that involve stops/trips not part of the plan.
7157       -- We want to track trip unassignments created by matching the delivery's legs to the plan.
7158 
7159       IF x_trip_unassigns(i).trip_index IS NULL THEN
7160 
7161         -- make sure trip is not mapped to plan
7162         -- (if trip is in the plan, its stops would be cleaned up as needed.)
7163         j := x_plan_trips.FIRST;
7164         l_found := (x_plan_trips(j).trip_id = x_trip_unassigns(i).trip_id);
7165         WHILE j IS NOT NULL AND NOT l_found LOOP
7166           l_found := (x_plan_trips(j).trip_id = x_trip_unassigns(i).trip_id);
7167           IF NOT l_found THEN
7168             j := x_plan_trips.NEXT(j);
7169           END IF;
7170         END LOOP;
7171 
7172 	IF NOT l_found THEN
7173            -- trip is not in plan; find out if its stops should be deleted.
7174 
7175            -- If the pickup and drop off stops are populated, then
7176            -- trips are not from obsoleted moves.
7177 
7178            IF x_trip_unassigns(i).pickup_stop_id IS NOT NULL
7179            OR x_trip_unassigns(i).dropoff_stop_id IS NOT NULL THEN
7180 
7181               examine_stop(p_stop_id => x_trip_unassigns(i).pickup_stop_id,
7182                            p_trip_id => x_trip_unassigns(i).trip_id);
7183 
7184               examine_stop(p_stop_id => x_trip_unassigns(i).dropoff_stop_id,
7185                            p_trip_id => x_trip_unassigns(i).trip_id);
7186 
7187             ELSE
7188 
7189               FOR trip in c_trip_stops(x_trip_unassigns(i).trip_id) LOOP
7190 
7191                   BEGIN
7192                     -- Lock the stop
7193                     WSH_TRIP_STOPS_PVT.lock_trip_stop_no_compare(trip.stop_id);
7194                     examine_stop(p_stop_id => trip.stop_id,
7195                                  p_trip_id => x_trip_unassigns(i).trip_id);
7196                   EXCEPTION
7197                     WHEN OTHERS THEN
7198                     -- Do nothing. If the stop has been locked by another
7199                     -- process, unlikely that it will need to be deleted.
7200 
7201                     IF l_debug_on THEN
7202                        WSH_DEBUG_SV.log(l_module_name, 'Could not lock Stop: ', trip.stop_id);
7203                     END IF;
7204                   END;
7205 
7206               END LOOP;
7207 
7208             END IF;
7209 
7210         END IF;
7211 
7212       END IF;
7213 
7214       i := x_trip_unassigns.NEXT(i);
7215     END LOOP;
7216 
7217     IF l_debug_on THEN
7218       WSH_DEBUG_SV.log(l_module_name, 'l_empty_stops.COUNT', l_empty_stops.COUNT);
7219       WSH_DEBUG_SV.log(l_module_name, 'l_empty_trips.COUNT', l_empty_trips.COUNT);
7220     END IF;
7221 
7222     IF l_empty_stops.COUNT > 0 THEN
7223 
7224       -- A. delete empty stops
7225 
7226       i := l_empty_stops.FIRST;
7227       j := 0;
7228       WHILE i IS NOT NULL LOOP
7229        j := j + 1;
7230        l_stop_attrs(j).stop_id := l_empty_stops(i);
7231 
7232         -- WV changes
7233         -- Remove the empty stops (to be deleted) from
7234         -- from the WV exceptions list.
7235 
7236         IF x_context.wv_exception_stops.exists(l_empty_stops(i)) THEN
7237            x_context.wv_exception_stops.delete(l_empty_stops(i));
7238            IF l_debug_on THEN
7239               WSH_DEBUG_SV.log(l_module_name,'Do not Log WV exception against stop', l_empty_stops(i));
7240            END IF;
7241         END IF;
7242 
7243 
7244        i:= l_empty_stops.NEXT(i);
7245       END LOOP;
7246 
7247       l_stop_action_prms.caller      := 'WSH_TP_RELEASE';
7248       l_stop_action_prms.action_code := 'DELETE';
7249 
7250       WSH_TRIP_STOPS_GRP.stop_action(
7251         p_api_version_number => 1.0,
7252         p_init_msg_list      => FND_API.G_TRUE,
7253         p_commit             => FND_API.G_FALSE,
7254         p_action_prms        => l_stop_action_prms,
7255         p_rec_attr_tab       => l_stop_attrs,
7256         x_stop_out_rec       => l_stop_action_rec,
7257         x_def_rec            => l_stop_defaults,
7258         x_return_status      => l_return_status,
7259         x_msg_count          => l_msg_count,
7260         x_msg_data           => l_msg_data);
7261 
7262       -- discard return status here; errors deleting stops will not fail the plan.
7263       -- sometimes, deleting a stop will fail but deleting its trip will succeed
7264       -- (for example, FTE validates that a trip has at least 2 stops).
7265 
7266       -- B. delete trips that have no stops or deliveries assigned.
7267 
7268       i := l_empty_trips.FIRST;
7269       j := 0;
7270       WHILE i IS NOT NULL LOOP
7271         OPEN c_exist_stop(l_empty_trips(i));
7272         FETCH c_exist_stop INTO l_temp_id;
7273         IF c_exist_stop%NOTFOUND THEN
7274           IF l_debug_on THEN
7275             WSH_DEBUG_SV.log(l_module_name, 'no stops for trip', l_empty_trips(i));
7276           END IF;
7277           j := j + 1;
7278           l_trip_attrs(j).trip_id := l_empty_trips(i);
7279         ELSE
7280           OPEN c_exist_dels(l_empty_trips(i));
7281           FETCH c_exist_dels INTO l_temp_id;
7282           IF c_exist_dels%NOTFOUND THEN
7283             IF l_debug_on THEN
7284               WSH_DEBUG_SV.log(l_module_name, 'no deliveries for trip', l_empty_trips(i));
7285             END IF;
7286             j := j + 1;
7287             l_trip_attrs(j).trip_id := l_empty_trips(i);
7288           END IF;
7289           CLOSE c_exist_dels;
7290         END IF;
7291         CLOSE c_exist_stop;
7292 
7293         i := l_empty_trips.NEXT(i);
7294       END LOOP;
7295 
7296       IF l_debug_on THEN
7297         WSH_DEBUG_SV.log(l_module_name, 'l_trip_attrs.COUNT', l_trip_attrs.COUNT);
7298       END IF;
7299 
7300       IF l_trip_attrs.COUNT > 0 THEN
7301 
7302         l_trip_action_prms.caller      := 'WSH_TP_RELEASE';
7303         l_trip_action_prms.action_code := 'DELETE';
7304 
7305         wsh_trips_grp.trip_action(
7306            p_api_version_number =>  1.0,
7307            p_init_msg_list      =>  FND_API.G_TRUE,
7308            p_commit             =>  FND_API.G_FALSE,
7309            p_action_prms        =>  l_trip_action_prms,
7310            p_rec_attr_tab       =>  l_trip_attrs,
7311            x_trip_out_rec       =>  l_trip_out_rec,
7312            x_def_rec            =>  l_trip_defaults,
7313            x_return_status      =>  l_return_status,
7314            x_msg_count          =>  l_msg_count,
7315            x_msg_data           =>  l_msg_data);
7316 
7317         -- discard return status here; errors deleting trips will not fail the plan.
7318 
7319       END IF;
7320 
7321     END IF;
7322 
7323   END IF;
7324 
7325 
7326   --
7327   --  4. refresh the plan stops' stop_id where they have linked stops, to ensure
7328   --     that the interface stop records will point to the physical stops.
7329   --
7330 
7331   IF l_debug_on THEN
7332     WSH_DEBUG_SV.logmsg(l_module_name, '4. refresh physical stop ids');
7333     WSH_DEBUG_SV.log(l_module_name, 'x_context.linked_trip_count', x_context.linked_trip_count);
7334   END IF;
7335 
7336   IF x_context.linked_trip_count > 0 THEN
7337     DECLARE
7338       l_trip_count NUMBER;
7339       l_trip_index NUMBER;
7340 
7341       l_stop_count NUMBER;
7342       l_stop_index NUMBER;
7343       l_stop_id    NUMBER;
7344     BEGIN
7345       l_trip_count := x_context.linked_trip_count;
7346       l_trip_index := x_plan_trips.FIRST;
7347 
7348       WHILE l_trip_count > 0 LOOP
7349 
7350          IF x_plan_trips(l_trip_index).linked_stop_count > 0 THEN
7351 
7352            l_stop_count := x_plan_trips(l_trip_index).linked_stop_count;
7353            l_stop_index := x_plan_trips(l_trip_index).stop_base_index;
7354 
7355            WHILE l_stop_count > 0 LOOP
7356               IF     x_plan_stops(l_stop_index).internal_do_count > 0
7357                  AND x_plan_stops(l_stop_index).external_pd_count > 0 THEN
7358 
7359                OPEN c_lookup_physical_stop(x_plan_stops(l_stop_index).stop_id);
7360                FETCH c_lookup_physical_stop INTO l_stop_id;
7361                IF c_lookup_physical_stop%FOUND
7362                   AND l_stop_id <> x_plan_stops(l_stop_index).stop_id  THEN
7363                  IF l_debug_on THEN
7364                    WSH_DEBUG_SV.log(l_module_name, 'physical stop('||l_stop_index||') looked up',
7365                                     x_plan_stops(l_stop_index).stop_id || ' -> ' || l_stop_id);
7366                  END IF;
7367                  x_plan_stops(l_stop_index).stop_id := l_stop_id;
7368                END IF;
7369                CLOSE c_lookup_physical_stop;
7370 
7371                l_stop_count := l_stop_count - 1;
7372               END IF;
7373 
7374             l_stop_index := x_plan_stops.NEXT(l_stop_index);
7375            END LOOP;
7376 
7377            l_trip_count := l_trip_count - 1;
7378          END IF;
7379 
7380          l_trip_index := x_plan_trips.NEXT(l_trip_index);
7381       END LOOP;
7382     END;
7383   END IF;
7384 
7385 
7386   --
7387   --  5. update interface tables for details, deliveries, stops and trips
7388   --     so that TP can identify the TE records mapped.
7389   --
7390 
7391   IF l_debug_on THEN
7392     WSH_DEBUG_SV.logmsg(l_module_name, '5. update interface tables');
7393   END IF;
7394 
7395 
7396   DECLARE
7397     l_interface_ids  WSH_UTIL_CORE.id_tab_type;
7398     l_ids            WSH_UTIL_CORE.id_tab_type;
7399     l_qtys           WSH_UTIL_CORE.id_tab_type;
7400     l_p_index          NUMBER;
7401     l_i_index          NUMBER;
7402   BEGIN
7403 
7404     -- update trips
7405     l_i_index := 0;
7406     l_p_index := x_plan_trips.FIRST;
7407     WHILE l_p_index IS NOT NULL LOOP
7408       l_i_index := l_i_index + 1;
7409       l_interface_ids(l_i_index) := x_plan_trips(l_p_index).trip_interface_id;
7410       l_ids(l_i_index)           := x_plan_trips(l_p_index).trip_id;
7411       l_p_index := x_plan_trips.NEXT(l_p_index);
7412     END LOOP;
7413 
7414     IF l_debug_on THEN
7415       WSH_DEBUG_SV.log(l_module_name, 'Bulk update trips: count', l_interface_ids.COUNT);
7416     END IF;
7417 
7418     FORALL i in 1..l_interface_ids.count
7419     UPDATE wsh_trips_interface
7420     SET trip_id = l_ids(i)
7421     WHERE trip_interface_id = l_interface_ids(i);
7422 
7423     -- update stops
7424     l_interface_ids.DELETE;
7425     l_ids.DELETE;
7426     l_i_index := 0;
7427     l_p_index := x_plan_stops.FIRST;
7428     WHILE l_p_index IS NOT NULL LOOP
7429       l_i_index := l_i_index + 1;
7430       l_interface_ids(l_i_index) := x_plan_stops(l_p_index).stop_interface_id;
7431       l_ids(l_i_index)           := x_plan_stops(l_p_index).stop_id;
7432       l_p_index := x_plan_stops.NEXT(l_p_index);
7433     END LOOP;
7434 
7435     IF l_debug_on THEN
7436       WSH_DEBUG_SV.log(l_module_name, 'Bulk update stops: count', l_interface_ids.COUNT);
7437     END IF;
7438 
7439     FORALL i in 1..l_interface_ids.count
7440     UPDATE wsh_trip_stops_interface
7441     SET stop_id = l_ids(i)
7442     WHERE stop_interface_id = l_interface_ids(i);
7443 
7444 
7445 
7446     -- update deliveries
7447     l_interface_ids.DELETE;
7448     l_ids.DELETE;
7449     l_i_index := 0;
7450     l_p_index := x_plan_deliveries.FIRST;
7451     WHILE l_p_index IS NOT NULL LOOP
7452       l_i_index := l_i_index + 1;
7453       l_interface_ids(l_i_index) := x_plan_deliveries(l_p_index).del_interface_id;
7454       l_ids(l_i_index)           := x_plan_deliveries(l_p_index).delivery_id;
7455       l_p_index := x_plan_deliveries.NEXT(l_p_index);
7456     END LOOP;
7457 
7458     IF l_debug_on THEN
7459       WSH_DEBUG_SV.log(l_module_name, 'Bulk update deliveries: count', l_interface_ids.COUNT);
7460     END IF;
7461 
7462     FORALL i in 1..l_interface_ids.count
7463     UPDATE wsh_new_del_interface
7464     SET delivery_id = l_ids(i)
7465     WHERE delivery_interface_id = l_interface_ids(i);
7466 
7467 
7468 
7469     -- update delivery details
7470     --  question: what happens if one interface line is mapped to multiple details?
7471     --    answer: TE, TP PM and Development had discussed this question; for now,
7472     --            the interface line will get one of these details' IDs because the
7473     --            interface delivery gets their delivery_id which will be first choice
7474     --            in the plan's next re-release.
7475     l_interface_ids.DELETE;
7476     l_ids.DELETE;
7477     l_i_index := 0;
7478     l_p_index := x_plan_details.FIRST;
7479     WHILE l_p_index IS NOT NULL LOOP
7480       l_i_index := l_i_index + 1;
7481       l_interface_ids(l_i_index) := x_plan_details(l_p_index).dd_interface_id;
7482       l_ids(l_i_index)           := x_plan_details(l_p_index).delivery_detail_id;
7483       l_qtys(l_i_index)          := x_plan_details(l_p_index).mapped_quantity;
7484       l_p_index := x_plan_details.NEXT(l_p_index);
7485     END LOOP;
7486 
7487     IF l_debug_on THEN
7488       WSH_DEBUG_SV.log(l_module_name, 'Bulk update details: count', l_interface_ids.COUNT);
7489     END IF;
7490 
7491     FORALL i in 1..l_interface_ids.count
7492     UPDATE wsh_del_details_interface
7493     SET delivery_detail_id = l_ids(i),
7494         requested_quantity = l_qtys(i)
7495     WHERE delivery_detail_interface_id = l_interface_ids(i);
7496 
7497 
7498 
7499   END;
7500 
7501   --
7502   -- 6. Log exceptions against lines, deliveries, and stops that
7503   --    have their weights and volumes frozen, but may have their
7504   --    weights and volumes updated.
7505   --    WV changes
7506   --
7507   IF l_debug_on THEN
7508     WSH_DEBUG_SV.logmsg(l_module_name, '6.  Log exceptions against lines, deliveries, and stops');
7509   END IF;
7510 
7511   Log_WV_Exceptions(
7512           p_details_loc_tab => x_context.wv_exception_details,
7513           p_deliveries_loc_tab => x_context.wv_exception_dels,
7514           p_stops_loc_tab => x_context.wv_exception_stops,
7515           x_return_status => x_return_status);
7516 
7517   x_context.wv_exception_details.delete;
7518   x_context.wv_exception_dels.delete;
7519   x_context.wv_exception_stops.delete;
7520 
7521   --
7522   --
7523   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
7524 
7525   IF l_debug_on THEN
7526      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
7527      WSH_DEBUG_SV.pop(l_module_name);
7528   END IF;
7529 
7530   EXCEPTION
7531 
7532     WHEN OTHERS THEN
7533       IF c_empty_dels%isopen THEN
7534          CLOSE  c_empty_dels;
7535       END IF;
7536       IF c_legs%isopen THEN
7537          CLOSE  c_legs;
7538       END IF;
7539       IF c_initial_pu_date%ISOPEN THEN
7540          CLOSE c_initial_pu_date;
7541       END IF;
7542       IF c_final_do_date%ISOPEN THEN
7543          CLOSE c_final_do_date;
7544       END IF;
7545       IF c_final_do_date_dummy%ISOPEN THEN
7546          CLOSE c_final_do_date_dummy;
7547       END IF;
7548       IF c_dd_date_range%ISOPEN THEN
7549          CLOSE c_dd_date_range;
7550       END IF;
7551       IF c_exist_stop%ISOPEN THEN
7552          CLOSE  c_exist_stop;
7553       END IF;
7554       IF c_exist_dels%ISOPEN THEN
7555          CLOSE  c_exist_dels;
7556       END IF;
7557       IF c_lookup_physical_stop%ISOPEN THEN
7558          CLOSE c_lookup_physical_stop;
7559       END IF;
7560 
7561       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
7562       WSH_UTIL_CORE.DEFAULT_HANDLER(
7563                         'WSH_TP_RELEASE.PLAN_CLEANUP',
7564                         l_module_name);
7565 
7566     IF l_debug_on THEN
7567        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
7568        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
7569     END IF;
7570 
7571 END plan_cleanup;
7572 
7573 
7574 
7575 
7576 --
7577 --  Procedure:          attributes_match
7578 --  Parameters:
7579 --               x_attributes          reference attribute values
7580 --               x_values              line's attribute values
7581 --
7582 --  Description:
7583 --               Returns TRUE if the grouping attributes match (for inbound
7584 --               and outbound).
7585 --               This is called by generate_lock_candidates.
7586 --
7587 FUNCTION attributes_match(
7588            x_attributes IN plan_detail_rec_type,
7589            x_values     IN c_map_lines%ROWTYPE)
7590 RETURN BOOLEAN
7591 IS
7592   l_module_name   CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'ATTRIBUTES_MATCH';
7593   l_debug_on      BOOLEAN;
7594   --
7595   l_attr_tab         WSH_DELIVERY_AUTOCREATE.grp_attr_tab_type;
7596   l_action_rec       WSH_DELIVERY_AUTOCREATE.action_rec_type;
7597   l_target_rec       WSH_DELIVERY_AUTOCREATE.grp_attr_rec_type;
7598   l_group_tab        WSH_DELIVERY_AUTOCREATE.grp_attr_tab_type;
7599   l_matched_entities WSH_UTIL_CORE.id_tab_type;
7600   l_out_rec          WSH_DELIVERY_AUTOCREATE.out_rec_type;
7601   l_return_status    VARCHAR2(1);
7602 
7603   l_match_flag       BOOLEAN := FALSE;
7604 
7605 BEGIN
7606   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
7607   --
7608   IF l_debug_on IS NULL THEN
7609     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
7610   END IF;
7611   --
7612   IF l_debug_on THEN
7613     WSH_DEBUG_SV.push(l_module_name);
7614     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.line_direction',             x_attributes.line_direction);
7615     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.ship_from_location_id',      x_attributes.ship_from_location_id);
7616     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.ship_to_location_id',        x_attributes.ship_to_location_id);
7617     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.organization_id',            x_attributes.organization_id);
7618     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.customer_id',                x_attributes.customer_id);
7619     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.freight_terms_code',         x_attributes.freight_terms_code);
7620     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.intmed_ship_to_location_id', x_attributes.intmed_ship_to_location_id);
7621     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.ship_method_code',           x_attributes.ship_method_code);
7622     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.mode_of_transport',          x_attributes.mode_of_transport);
7623     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.service_level',              x_attributes.service_level);
7624     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.carrier_id',                 x_attributes.carrier_id);
7625     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.shipping_control',           x_attributes.shipping_control);
7626     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.vendor_id',                  x_attributes.vendor_id);
7627     WSH_DEBUG_SV.log(l_module_name, 'x_attributes.party_id',                   x_attributes.party_id);
7628     WSH_DEBUG_SV.log(l_module_name, 'x_values.line_direction',             x_values.line_direction);
7629     WSH_DEBUG_SV.log(l_module_name, 'x_values.ship_from_location_id',      x_values.ship_from_location_id);
7630     WSH_DEBUG_SV.log(l_module_name, 'x_values.ship_to_location_id',        x_values.ship_to_location_id);
7631     WSH_DEBUG_SV.log(l_module_name, 'x_values.organization_id',            x_values.organization_id);
7632     WSH_DEBUG_SV.log(l_module_name, 'x_values.customer_id',                x_values.customer_id);
7633     WSH_DEBUG_SV.log(l_module_name, 'x_values.freight_terms_code',         x_values.freight_terms_code);
7634     WSH_DEBUG_SV.log(l_module_name, 'x_values.intmed_ship_to_location_id', x_values.intmed_ship_to_location_id);
7635     WSH_DEBUG_SV.log(l_module_name, 'x_values.ship_method_code',           x_values.ship_method_code);
7636     WSH_DEBUG_SV.log(l_module_name, 'x_values.mode_of_transport',          x_values.mode_of_transport);
7637     WSH_DEBUG_SV.log(l_module_name, 'x_values.service_level',              x_values.service_level);
7638     WSH_DEBUG_SV.log(l_module_name, 'x_values.carrier_id',                 x_values.carrier_id);
7639     WSH_DEBUG_SV.log(l_module_name, 'x_values.shipping_control',           x_values.shipping_control);
7640     WSH_DEBUG_SV.log(l_module_name, 'x_values.vendor_id',                  x_values.vendor_id);
7641     WSH_DEBUG_SV.log(l_module_name, 'x_values.party_id',                   x_values.party_id);
7642   END IF;
7643 
7644 
7645   l_action_rec.caller                 := 'WSH_TP_RELEASE';
7646   l_action_rec.action                 := 'MATCH_GROUPS';
7647   l_action_rec.group_by_header_flag   := 'N';
7648   l_action_rec.group_by_delivery_flag := 'N';
7649   l_action_rec.output_format_type     := NULL;
7650   l_action_rec.output_entity_type     := NULL;
7651   l_action_rec.check_single_grp       := 'Y';
7652 
7653   l_attr_tab(1).entity_id                  := NULL;
7654   l_attr_tab(1).entity_type                := 'DLVB';
7655   l_attr_tab(1).ship_to_location_id        := x_attributes.ship_to_location_id;
7656   l_attr_tab(1).ship_from_location_id      := x_attributes.ship_from_location_id;
7657   l_attr_tab(1).customer_id                := x_attributes.customer_id;
7658   l_attr_tab(1).intmed_ship_to_location_id := x_attributes.intmed_ship_to_location_id;
7659   l_attr_tab(1).fob_code                   := x_attributes.fob_code;
7660   l_attr_tab(1).freight_terms_code         := x_attributes.freight_terms_code;
7661   l_attr_tab(1).ship_method_code           := x_attributes.ship_method_code;
7662   l_attr_tab(1).carrier_id                 := x_attributes.carrier_id;
7663   l_attr_tab(1).service_level              := x_attributes.service_level;
7664   l_attr_tab(1).mode_of_transport          := x_attributes.mode_of_transport;
7665   l_attr_tab(1).source_header_id           := x_attributes.source_header_id;
7666   l_attr_tab(1).organization_id            := x_attributes.organization_id;
7667   l_attr_tab(1).ignore_for_planning        := 'N';
7668   l_attr_tab(1).line_direction             := x_attributes.line_direction;
7669   l_attr_tab(1).shipping_control           := x_attributes.shipping_control;
7670   l_attr_tab(1).vendor_id                  := x_attributes.vendor_id;
7671   l_attr_tab(1).party_id                   := x_attributes.party_id;
7672   l_attr_tab(1).container_flag             := 'N';
7673 
7674   l_attr_tab(2).entity_id                  := NULL;
7675   l_attr_tab(2).entity_type                := 'DLVB';
7676   l_attr_tab(2).ship_to_location_id        := x_values.ship_to_location_id;
7677   l_attr_tab(2).ship_from_location_id      := x_values.ship_from_location_id;
7678   l_attr_tab(2).customer_id                := x_values.customer_id;
7679   l_attr_tab(2).intmed_ship_to_location_id := x_values.intmed_ship_to_location_id;
7680   l_attr_tab(2).fob_code                   := x_values.fob_code;
7681   l_attr_tab(2).freight_terms_code         := x_values.freight_terms_code;
7682   l_attr_tab(2).ship_method_code           := x_values.ship_method_code;
7683   l_attr_tab(2).carrier_id                 := x_values.carrier_id;
7684   l_attr_tab(2).service_level              := x_values.service_level;
7685   l_attr_tab(2).mode_of_transport          := x_values.mode_of_transport;
7686   l_attr_tab(2).source_header_id           := x_values.source_header_id;
7687   l_attr_tab(2).organization_id            := x_values.organization_id;
7688   l_attr_tab(2).ignore_for_planning        := 'N';
7689   l_attr_tab(2).line_direction             := x_values.line_direction;
7690   l_attr_tab(2).shipping_control           := x_values.shipping_control;
7691   l_attr_tab(2).vendor_id                  := x_values.vendor_id;
7692   l_attr_tab(2).party_id                   := x_values.party_id;
7693   l_attr_tab(2).container_flag             := 'N';
7694 
7695 
7696   WSH_DELIVERY_AUTOCREATE.Find_Matching_Groups(
7697            p_attr_tab         => l_attr_tab,
7698            p_action_rec       => l_action_rec,
7699            p_target_rec       => l_target_rec,
7700            p_group_tab        => l_group_tab,
7701            x_matched_entities => l_matched_entities,
7702            x_out_rec          => l_out_rec,
7703            x_return_status    => l_return_status);
7704 
7705   IF l_debug_on THEN
7706     WSH_DEBUG_SV.log(l_module_name, 'find_matching_groups: l_return_status', l_return_status);
7707     WSH_DEBUG_SV.log(l_module_name, 'l_out_rec.single_group', l_out_rec.single_group);
7708   END IF;
7709 
7710   IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_SUCCESS,
7711                          WSH_UTIL_CORE.G_RET_STS_WARNING) THEN
7712     l_match_flag := (l_out_rec.single_group = 'Y');
7713   ELSE
7714     l_match_flag := FALSE;
7715   END IF;
7716 
7717   IF l_debug_on THEN
7718       WSH_DEBUG_SV.pop(l_module_name);
7719   END IF;
7720   RETURN l_match_flag;
7721 END attributes_match;
7722 
7723 
7724 --
7725 --  Procedure:          flush_used_details
7726 --  Parameters:
7727 --               x_context             context in this session
7728 --               x_current_used_details  current list of fully and partially used details
7729 --                                       which will be deleted when the procedure returns.
7730 --               x_used_details          master list of partially used details
7731 --               x_errors_tab            list of errors to insert into wsh_interface_errors at the end
7732 --               x_return_status         return status
7733 --  Description:
7734 --               Flushes the current list, adding only partially used details
7735 --               to the master list.
7736 --               * It also checks for possibility of breaking LPN configuration
7737 --                 because the partially used detail will definitely not stay in the plan's deliveries.
7738 --               The current list will be deleted.
7739 --               This is called by generate_lock_candidates.
7740 --
7741 PROCEDURE flush_used_details(
7742               x_context              IN OUT NOCOPY context_rec_type,
7743               x_current_used_details IN OUT NOCOPY used_details_tab_type,
7744               x_used_details         IN OUT NOCOPY used_details_tab_type,
7745               x_errors_tab           IN OUT NOCOPY interface_errors_tab_type,
7746               x_return_status           OUT NOCOPY VARCHAR2)
7747 IS
7748   l_module_name   CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'FLUSH_USED_DETAILS';
7749   l_debug_on      BOOLEAN;
7750   l_new_index     NUMBER;
7751   l_used_index    NUMBER;
7752   l_return_status VARCHAR2(1);
7753 BEGIN
7754   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
7755   --
7756   IF l_debug_on IS NULL THEN
7757     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
7758   END IF;
7759 
7760   IF l_debug_on THEN
7761     WSH_DEBUG_SV.push(l_module_name);
7762   END IF;
7763 
7764   IF x_current_used_details.COUNT > 0 THEN
7765     -- there are records to flush.
7766     l_used_index := x_current_used_details.FIRST;
7767     IF x_used_details.COUNT > 0 THEN
7768       l_new_index  := x_used_details.LAST + 1;
7769     ELSE
7770       l_new_index  := 1;
7771     END IF;
7772 
7773     WHILE l_used_index IS NOT NULL LOOP
7774 
7775       IF x_current_used_details(l_used_index).available_quantity > 0 THEN
7776 
7777         -- track these partially used lines;
7778         -- also check whether LPN configuration would be broken.
7779         IF x_current_used_details(l_used_index).topmost_cont_id IS NOT NULL THEN
7780           stamp_interface_error(
7781               p_group_id            => x_context.group_id,
7782               p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
7783               p_entity_interface_id => x_current_used_details(l_used_index).dd_interface_id,
7784               p_message_name        => 'WSH_TP_F_LPN_BREAK',
7785               p_token_1_name        => 'CONTAINER_NAME',
7786               p_token_1_value       => wsh_container_utilities.get_cont_name(
7787                                                x_current_used_details(l_used_index).topmost_cont_id),
7788               p_token_2_name        => 'PLAN_TRIP_NUM',
7789               p_token_2_value       => get_plan_trip_num(x_context),
7790               x_errors_tab          => x_errors_tab,
7791               x_return_status       => l_return_status);
7792           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7793           IF l_debug_on THEN
7794             WSH_DEBUG_SV.log(l_module_name, 'broken lpn: x_return_status', x_return_status);
7795             WSH_DEBUG_SV.pop(l_module_name);
7796           END IF;
7797           RETURN;
7798         END IF;
7799 
7800         x_used_details(l_new_index) := x_current_used_details(l_used_index);
7801         l_new_index := l_new_index + 1;
7802       END IF;
7803 
7804       l_used_index := x_current_used_details.NEXT(l_used_index);
7805     END LOOP;
7806 
7807     x_current_used_details.DELETE;
7808 
7809   END IF;
7810 
7811 
7812   -- Bug 3555487 initialize message stack for each major action point
7813   -- Messages should be flushed before each new set of plan lines.
7814   FND_MSG_PUB.initialize;
7815 
7816   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
7817   IF l_debug_on THEN
7818       WSH_DEBUG_SV.pop(l_module_name);
7819   END IF;
7820 
7821 END flush_used_details;
7822 
7823 
7824 
7825 
7826 --
7827 --  Procedure:          map_dangling_containers
7828 --  Parameters:
7829 --               x_context             context in this session
7830 --               p_delivery_index      index into x_plan_deliveries to map dangling containers
7831 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
7832 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
7833 --               x_return_status       return status
7834 --
7835 --  Description:
7836 --               Maps and locks all containers of a delivery that have no plan lines.
7837 --
7838 --               This is the only time the process looks at plan containers in wsh_del_details_interface.
7839 --               This is to allow TP to release what they snapshot because they may have to
7840 --               capture firm deliveries that have only dangling containers.
7841 --
7842 --               * fail the plan if the contents of TE delivery does not match the plan delivery's contents.
7843 PROCEDURE map_dangling_containers(
7844            x_context                  IN OUT NOCOPY context_rec_type,
7845            p_delivery_index           IN            NUMBER,
7846            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
7847            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
7848            x_return_status               OUT NOCOPY VARCHAR2
7849           )
7850 IS
7851   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'MAP_DANGLING_CONTAINERS';
7852   --
7853   l_debug_on BOOLEAN;
7854 
7855   -- check for any line in this delivery
7856   CURSOR c_has_line(p_delivery_id IN NUMBER) IS
7857   SELECT d.delivery_detail_id
7858   FROM   wsh_delivery_assignments_v a, wsh_delivery_details d
7859   WHERE  a.parent_delivery_detail_id is NULL
7860   AND    a.delivery_id = p_delivery_id
7861   AND    a.delivery_id IS NOT NULL
7862   AND    a.delivery_detail_id = d.delivery_detail_id
7863   AND    d.released_status <> 'D' -- 4322654
7864   AND    d.container_flag = 'N'
7865   AND    rownum = 1;
7866 
7867   -- compare all containers in delivery with plan.
7868   CURSOR c_dangling_lpns(p_delivery_id IN NUMBER) IS
7869   SELECT d.delivery_detail_id,
7870          d.container_name,
7871          a.parent_delivery_detail_id
7872   FROM   wsh_delivery_assignments_v a, wsh_delivery_details d
7873   WHERE  a.delivery_id = p_delivery_id
7874   AND    a.delivery_id IS NOT NULL
7875   AND    a.delivery_detail_id = d.delivery_detail_id
7876   AND    d.container_flag = 'Y'
7877   FOR UPDATE NOWAIT;
7878 
7879   -- count containers in plan
7880   -- currently, TP captures all LPNs within the delivery.
7881   CURSOR c_plan_lpn_count(x_delivery_interface_id IN NUMBER) IS
7882   SELECT count(wddi.delivery_detail_id)
7883   FROM   wsh_del_assgn_interface      wdai,
7884          wsh_del_details_interface    wddi
7885   WHERE  wdai.delivery_interface_id = x_delivery_interface_id
7886   AND    wdai.delivery_interface_id IS NOT NULL
7887   AND    wdai.interface_action_code = G_TP_RELEASE_CODE
7888   AND    wddi.delivery_detail_interface_id = wdai.delivery_detail_interface_id
7889   AND    wddi.interface_action_code = G_TP_RELEASE_CODE
7890   AND    wddi.container_flag = 'Y';
7891 
7892   -- verify that this TE container is mentioned in plan
7893   CURSOR c_plan_lpn(x_delivery_interface_id IN NUMBER,
7894                     x_delivery_detail_id    IN NUMBER) IS
7895   SELECT wddi.delivery_detail_interface_id
7896   FROM   wsh_del_assgn_interface      wdai,
7897          wsh_del_details_interface    wddi
7898   WHERE  wdai.delivery_interface_id = x_delivery_interface_id
7899   AND    wdai.delivery_interface_id IS NOT NULL
7900   AND    wdai.interface_action_code = G_TP_RELEASE_CODE
7901   AND    wddi.delivery_detail_interface_id = wdai.delivery_detail_interface_id
7902   AND    wddi.interface_action_code = G_TP_RELEASE_CODE
7903   AND    wddi.delivery_detail_id = x_delivery_detail_id
7904   AND    wddi.container_flag = 'Y'
7905   AND    rownum = 1;
7906 
7907 
7908   l_detail_id        NUMBER;
7909   l_found            BOOLEAN;
7910   l_scanning_lpns    BOOLEAN := FALSE;
7911   l_plan_lpn_count   NUMBER  := 0;
7912   l_te_lpn_count     NUMBER  := 0;
7913   l_return_status    VARCHAR2(1);
7914 
7915 BEGIN
7916 
7917   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
7918   --
7919   IF l_debug_on IS NULL THEN
7920     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
7921   END IF;
7922 
7923   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
7924 
7925   -- Bug 3555487 initialize message stack for each major action point.
7926   FND_MSG_PUB.initialize;
7927 
7928   IF l_debug_on THEN
7929     WSH_DEBUG_SV.push(l_module_name);
7930     WSH_DEBUG_SV.log(l_module_name, 'p_delivery_index', p_delivery_index);
7931   END IF;
7932 
7933   OPEN c_has_line(x_plan_deliveries(p_delivery_index).delivery_id);
7934   FETCH c_has_line INTO l_detail_id;
7935   l_found := c_has_line%FOUND;
7936   CLOSE c_has_line;
7937 
7938   IF l_found THEN
7939     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7940     stamp_interface_error(
7941       p_group_id            => x_context.group_id,
7942       p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
7943       p_entity_interface_id => x_plan_deliveries(p_delivery_index).del_interface_id,
7944       p_message_name        => 'WSH_TP_F_DEL_LINE',  -- new message
7945       p_token_1_name        => 'PLAN_DEL_NUM',
7946       p_token_1_value       => x_plan_deliveries(p_delivery_index).tp_delivery_number,
7947       p_token_2_name        => 'DELIVERY_NAME',
7948       p_token_2_value       => x_plan_deliveries(p_delivery_index).name,
7949       x_errors_tab          => x_errors_tab,
7950       x_return_status       => l_return_status);
7951   ELSE
7952     -- count plan LPNs
7953     OPEN c_plan_lpn_count(x_plan_deliveries(p_delivery_index).del_interface_id);
7954     FETCH c_plan_lpn_count INTO l_plan_lpn_count;
7955     IF c_plan_lpn_count%NOTFOUND THEN
7956       l_plan_lpn_count := 0;
7957     END IF;
7958     CLOSE c_plan_lpn_count;
7959 
7960     IF l_debug_on THEN
7961       WSH_DEBUG_SV.log(l_module_name, 'l_plan_lpn_count', l_plan_lpn_count);
7962     END IF;
7963 
7964     IF l_plan_lpn_count = 0 THEN
7965       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7966       stamp_interface_error(
7967         p_group_id            => x_context.group_id,
7968         p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
7969         p_entity_interface_id => x_plan_deliveries(p_delivery_index).del_interface_id,
7970         p_message_name        => 'WSH_TP_F_NO_PLAN_LPNS',  -- new message
7971         p_token_1_name        => 'PLAN_DEL_NUM',
7972         p_token_1_value       => x_plan_deliveries(p_delivery_index).tp_delivery_number,
7973         p_token_2_name        => 'DELIVERY_NAME',
7974         p_token_2_value       => x_plan_deliveries(p_delivery_index).name,
7975         x_errors_tab          => x_errors_tab,
7976         x_return_status       => l_return_status);
7977     ELSE
7978 
7979       -- check that all dangling containers in TE and plan are matched.
7980       l_scanning_lpns := TRUE;
7981       l_found := TRUE;
7982       FOR lpn_rec IN c_dangling_lpns(x_plan_deliveries(p_delivery_index).delivery_id)  LOOP
7983         l_te_lpn_count := l_te_lpn_count + 1;
7984 
7985         IF lpn_rec.parent_delivery_detail_id IS NULL THEN
7986           x_plan_deliveries(p_delivery_index).dangling_conts_count := x_plan_deliveries(p_delivery_index).dangling_conts_count + 1;
7987         END IF;
7988 
7989         OPEN c_plan_lpn(x_plan_deliveries(p_delivery_index).del_interface_id,
7990                         lpn_rec.delivery_detail_id);
7991         FETCH c_plan_lpn INTO l_detail_id;
7992         l_found := c_plan_lpn%FOUND;
7993         CLOSE c_plan_lpn;
7994 
7995         IF NOT l_found THEN
7996           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
7997           stamp_interface_error(
7998             p_group_id            => x_context.group_id,
7999             p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
8000             p_entity_interface_id => x_plan_deliveries(p_delivery_index).del_interface_id,
8001             p_message_name        => 'WSH_TP_F_NEW_LPN',  -- new message
8002             p_token_1_name        => 'PLAN_DEL_NUM',
8003             p_token_1_value       => x_plan_deliveries(p_delivery_index).tp_delivery_number,
8004             p_token_2_name        => 'DELIVERY_NAME',
8005             p_token_2_value       => x_plan_deliveries(p_delivery_index).name,
8006             p_token_3_name        => 'CONTAINER_NAME',
8007             p_token_3_value       => lpn_rec.container_name,
8008             x_errors_tab          => x_errors_tab,
8009             x_return_status       => l_return_status);
8010           -- at this point, l_found will be FALSE so we will not get the next message.
8011           EXIT;
8012         END IF;
8013 
8014       END LOOP;
8015       l_scanning_lpns := FALSE;
8016 
8017       IF l_debug_on THEN
8018         WSH_DEBUG_SV.log(l_module_name, 'l_te_lpn_count', l_te_lpn_count);
8019       END IF;
8020 
8021       IF l_found AND l_plan_lpn_count <> l_te_lpn_count THEN
8022         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
8023         stamp_interface_error(
8024           p_group_id            => x_context.group_id,
8025           p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
8026           p_entity_interface_id => x_plan_deliveries(p_delivery_index).del_interface_id,
8027           p_message_name        => 'WSH_TP_F_LPN_MISMATCH',  -- new message
8028           p_token_1_name        => 'PLAN_DEL_NUM',
8029           p_token_1_value       => x_plan_deliveries(p_delivery_index).tp_delivery_number,
8030           p_token_2_name        => 'DELIVERY_NAME',
8031           p_token_2_value       => x_plan_deliveries(p_delivery_index).name,
8032           x_errors_tab          => x_errors_tab,
8033           x_return_status       => l_return_status);
8034       END IF;
8035     END IF;
8036   END IF;
8037 
8038   --
8039   --
8040   IF l_debug_on THEN
8041      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
8042      WSH_DEBUG_SV.pop(l_module_name);
8043   END IF;
8044 
8045   EXCEPTION
8046     WHEN OTHERS THEN
8047       IF c_has_line%ISOPEN THEN
8048          CLOSE c_has_line;
8049       END IF;
8050       IF c_dangling_lpns%ISOPEN THEN
8051          CLOSE c_dangling_lpns;
8052       END IF;
8053       IF c_plan_lpn_count%ISOPEN THEN
8054          CLOSE c_plan_lpn_count;
8055       END IF;
8056       IF c_plan_lpn%ISOPEN THEN
8057          CLOSE c_plan_lpn;
8058       END IF;
8059 
8060       IF l_scanning_lpns THEN
8061         -- probably error due to lock on LPN.
8062         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
8063         stamp_interface_error(
8064           p_group_id            => x_context.group_id,
8065           p_entity_table_name   => 'WSH_NEW_DELIVERIES_INTERFACE',
8066           p_entity_interface_id => x_plan_deliveries(p_delivery_index).del_interface_id,
8067           p_message_name        => 'WSH_TP_F_DANGL_LPN_NO_LOCK',  -- new message
8068           p_token_1_name        => 'PLAN_DEL_NUM',
8069           p_token_1_value       => x_plan_deliveries(p_delivery_index).tp_delivery_number,
8070           p_token_2_name        => 'DELIVERY_NAME',
8071           p_token_2_value       => x_plan_deliveries(p_delivery_index).name,
8072           x_errors_tab          => x_errors_tab,
8073           x_return_status       => l_return_status);
8074       ELSE
8075         x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
8076         WSH_UTIL_CORE.DEFAULT_HANDLER(
8077                           'WSH_TP_RELEASE.MAP_DANGLING_CONTAINERS',
8078                           l_module_name);
8079       END IF;
8080 
8081     IF l_debug_on THEN
8082        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
8083        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
8084     END IF;
8085 
8086 END map_dangling_containers;
8087 
8088 
8089 
8090 
8091 
8092 
8093 --
8094 --  Procedure:          match_deliveries
8095 --  Parameters:
8096 --               x_context             context in this session
8097 --               x_plan_details        list of delivery details mapped to interface lines
8098 --               x_track_conts         list of topmost containers to track
8099 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
8100 --               x_used_details        list of delivery details partially used by the plan
8101 --               x_delivery_unassigns  list of delivery lines to unassign from their deliveries
8102 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
8103 --               x_return_status       return status
8104 --
8105 --  Description:
8106 --               Goes through each mapped delivery (with lines) to identify details
8107 --               that need to be unassigned so that it will match the plan's delivery.
8108 --               Note: dangling containers will be ignored and allowed to stay
8109 --                 with the delivery (TE user may be in middle of packing or TP may have
8110 --                 snapshot a firm itinerary having a delivery with dangling containers and no lines).
8111 --               Also processes x_used_details to mark partially split details that
8112 --               need to be unassigned if their current deliveries are in the plan.
8113 --                      Dangling container is a container that may have nested containers
8114 --                      but does not have any line packed within.
8115 --
8116 --               x_delivery_unassigns will have the unassignments for lines and LPNs not in plan.
8117 --               x_used_details will have need_unassignment set TRUE.
8118 --
8119 PROCEDURE match_deliveries(
8120            x_context                  IN OUT NOCOPY context_rec_type,
8121            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
8122            x_track_conts              IN OUT NOCOPY track_cont_tab_type,
8123            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
8124            x_used_details             IN OUT NOCOPY used_details_tab_type,
8125            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
8126            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
8127            x_return_status               OUT NOCOPY VARCHAR2
8128           )
8129 IS
8130   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'MATCH_DELIVERIES';
8131   --
8132   l_debug_on BOOLEAN;
8133 
8134   -- list the topmost containers and loose lines in the delivery
8135   CURSOR c_assigned_details(p_delivery_id IN NUMBER) IS
8136   SELECT d.delivery_detail_id,
8137          d.organization_id,
8138          d.container_flag,
8139          d.source_code,
8140          d.released_status,
8141          d.lpn_id
8142   FROM   wsh_delivery_assignments_v a, wsh_delivery_details d
8143   WHERE  a.parent_delivery_detail_id is NULL
8144   AND    a.delivery_id = p_delivery_id
8145   AND    a.delivery_id IS NOT NULL
8146   AND    a.delivery_detail_id = d.delivery_detail_id
8147   AND    d.released_status <> 'D'; -- Bug 4322654
8148 
8149   l_assigned_rec c_assigned_details%ROWTYPE;
8150 
8151   -- x_delivery_unassigns may have elements from mapping LPNs in generate_lock_candidates.
8152   -- we need to screen LPNs against these elements.
8153   l_last_mapped_lpn      NUMBER                := x_delivery_unassigns.COUNT;
8154   l_count                NUMBER                := x_delivery_unassigns.COUNT;
8155   l_index                NUMBER;
8156   l_detail_matches       VARCHAR2(1);
8157   l_du_index             NUMBER;
8158   l_dummy_id             NUMBER;
8159   l_flag                 VARCHAR2(1);
8160   l_working_used_details used_details_tab_type := x_used_details;
8161   l_next_index           NUMBER;
8162   l_plan_dd_index        NUMBER;
8163 
8164 
8165   -- check if topmost container has content which means it is not dangling.
8166 
8167   -- bug 4891939, sql 15039839
8168   -- removed rownum = 1
8169   -- since it causes 2 full table scan on wsh_delivery_assignments
8170 
8171   CURSOR c_has_content(p_container_id IN NUMBER) IS
8172   SELECT wdd.delivery_detail_id
8173   FROM   wsh_delivery_details wdd
8174   WHERE wdd.container_flag = 'N'
8175   AND   wdd.released_status <> 'D' -- 4322654
8176   AND   wdd.delivery_detail_id IN
8177         (SELECT  wda.delivery_detail_id
8178          FROM  wsh_delivery_assignments_v wda
8179          START WITH parent_delivery_detail_id = p_container_id
8180          CONNECT BY prior delivery_detail_id = parent_delivery_detail_id);
8181 
8182 
8183   -- WMS LPNs need to be checked for staged contents.
8184 
8185   -- bug 4891939, sql 15039851
8186   -- removed rownum = 1
8187   -- since it causes 2 full table scan on wsh_delivery_assignments
8188 
8189   CURSOR c_staged_content(p_container_id IN NUMBER) IS
8190   SELECT wdd.delivery_detail_id
8191   FROM   wsh_delivery_details wdd
8192   WHERE wdd.container_flag = 'N'
8193   AND   wdd.released_status = 'Y'
8194   AND   wdd.delivery_detail_id IN
8195         (SELECT  wda.delivery_detail_id
8196          FROM  wsh_delivery_assignments_v wda
8197          START WITH parent_delivery_detail_id = p_container_id
8198          CONNECT BY prior delivery_detail_id = parent_delivery_detail_id);
8199 
8200   WSH_PLANNED_DEL_NOT_MATCH EXCEPTION;
8201 
8202 BEGIN
8203 
8204   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
8205   --
8206   IF l_debug_on IS NULL THEN
8207     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
8208   END IF;
8209 
8210   IF l_debug_on THEN
8211     WSH_DEBUG_SV.push(l_module_name);
8212   END IF;
8213   --
8214   --
8215   -- Go through list of deliveries in plan.
8216 
8217   FOR i in 1.. x_plan_deliveries.count LOOP
8218 
8219       -- Skip if the delivery in plan is not mapped to an existing delivery
8220       -- or the delivery has only dangling containers (which has already been validated).
8221 
8222       IF    (x_plan_deliveries(i).delivery_id IS NOT NULL)
8223          OR (x_plan_deliveries(i).dangling_conts_count = 0)        THEN --{
8224 
8225         -- Go through list of loose details and containers in this delivery to check for unassignments.
8226         IF l_debug_on THEN
8227           WSH_DEBUG_SV.log(l_module_name, 'looping x_plan_deliveries(i).delivery_id', x_plan_deliveries(i).delivery_id);
8228         END IF;
8229 
8230         OPEN c_assigned_details(x_plan_deliveries(i).delivery_id);
8231         LOOP
8232            FETCH c_assigned_details INTO l_assigned_rec;
8233            EXIT WHEN c_assigned_details%NOTFOUND;
8234 
8235            IF l_debug_on THEN
8236              WSH_DEBUG_SV.log(l_module_name, 'looping assigned_details: delivery_detail_id', l_assigned_rec.delivery_detail_id);
8237            END IF;
8238 
8239            l_detail_matches := 'N';
8240            l_plan_dd_index  := NULL;
8241 
8242            IF NVL(l_assigned_rec.container_flag, 'N') = 'N' THEN
8243 
8244               FOR j in 1.. x_plan_details.count LOOP
8245 
8246                   IF (x_plan_details(j).delivery_detail_id = l_assigned_rec.delivery_detail_id)  THEN
8247                      l_plan_dd_index := j;
8248                      IF (x_plan_details(j).target_delivery_index = i) THEN
8249                        l_detail_matches := 'Y';
8250                      END IF;
8251                      -- stop scanning because the record is found in plan.
8252                      EXIT;
8253                   END IF;
8254 
8255               END LOOP;
8256 
8257            ELSE
8258 
8259               -- check if we've already added this container to x_delivery_unassigns
8260               FOR j IN 1..l_last_mapped_lpn LOOP
8261                  IF x_delivery_unassigns(j).delivery_detail_id = l_assigned_rec.delivery_detail_id THEN
8262                    l_detail_matches := 'Y';
8263                    EXIT;
8264                  END IF;
8265               END LOOP;
8266 
8267               IF l_detail_matches = 'N' THEN
8268                 -- if not yet found, then check the containers mapped in plan.
8269 
8270                 FOR j in 1.. x_track_conts.count LOOP
8271 
8272                     IF (x_track_conts(j).topmost_cont_id = l_assigned_rec.delivery_detail_id)  THEN
8273                        l_plan_dd_index := x_track_conts(j).plan_dd_index;
8274                        IF (x_track_conts(j).target_delivery_index = i) THEN
8275                          l_detail_matches := 'Y';
8276                        END IF;
8277                        -- stop scanning because the record is found in plan.
8278                        EXIT;
8279                     END IF;
8280 
8281                  END LOOP;
8282               END IF;
8283 
8284               IF l_detail_matches = 'N' THEN
8285                 -- if still not found, check that this container is dangling.
8286                 --  We consider dangling containers to match the plan because
8287                 -- the TE user may be in the middle of packing or TP may have
8288                 -- snapshot a firm delivery with dangling containers.
8289                 OPEN c_has_content(l_assigned_rec.delivery_detail_id);
8290                 FETCH c_has_content INTO l_dummy_id;
8291                 IF c_has_content%NOTFOUND THEN
8292                   -- automatically match this dangling container.
8293                   l_detail_matches := 'Y';
8294                   x_plan_deliveries(i).dangling_conts_count := x_plan_deliveries(i).dangling_conts_count + 1;
8295                 END IF;
8296                 CLOSE c_has_content;
8297               END IF;
8298 
8299            END IF;
8300 
8301 
8302            IF l_detail_matches = 'N' THEN
8303 
8304               -- Fail Plan if the delivery is planned or firmed.
8305               IF l_debug_on THEN
8306                 WSH_DEBUG_SV.logmsg(l_module_name, 'not matched in plan');
8307               END IF;
8308 
8309               IF NVL(x_plan_deliveries(i).wsh_planned_flag, 'N') <> 'N' THEN
8310                  Stamp_Interface_Error(p_group_id => x_context.group_id,
8311                                        p_entity_table_name => 'WSH_NEW_DEL_INTERFACE',
8312                                        p_entity_interface_id => x_plan_deliveries(i).del_interface_id,
8313                                        p_message_name => 'WSH_TP_F_PLAN_DEL_NOT_MATCH',
8314                                        p_token_1_name    => 'DELIVERY_NAME',
8315                                        p_token_1_value       => WSH_NEW_DELIVERIES_PVT.get_name(
8316                                                       x_plan_deliveries(i).delivery_id),
8317                                        p_token_2_name        => 'PLAN_TRIP_NUM',
8318                                        p_token_2_value       => get_plan_trip_num(x_context),
8319                                        p_token_3_name        => 'PLAN_DEL_NUM',
8320                                        p_token_3_value       => x_plan_deliveries(i).tp_delivery_number,
8321                                        x_errors_tab => x_errors_tab,
8322                                        x_return_status => x_return_status);
8323 
8324                  RAISE WSH_PLANNED_DEL_NOT_MATCH;
8325 
8326               END IF;
8327 
8328               -- Add to table of unassigns
8329 
8330               l_count := l_count + 1;
8331 
8332               x_delivery_unassigns(l_count).delivery_id        := x_plan_deliveries(i).delivery_id;
8333               x_delivery_unassigns(l_count).delivery_detail_id := l_assigned_rec.delivery_detail_id;
8334               x_delivery_unassigns(l_count).organization_id    := l_assigned_rec.organization_id;
8335               x_delivery_unassigns(l_count).container_flag     := l_assigned_rec.container_flag;
8336               x_delivery_unassigns(l_count).lines_staged       := NULL;
8337               x_delivery_unassigns(l_count).wms_org_flag       := wsh_util_validate.Check_Wms_Org(l_assigned_rec.organization_id);
8338               x_delivery_unassigns(l_count).source_code        := l_assigned_rec.source_code;
8339               x_delivery_unassigns(l_count).released_status    := l_assigned_rec.released_status;
8340               x_delivery_unassigns(l_count).lpn_id             := l_assigned_rec.lpn_id;
8341               x_delivery_unassigns(l_count).plan_dd_index      := l_plan_dd_index;
8342               x_delivery_unassigns(l_count).plan_del_index     := i;
8343               x_delivery_unassigns(l_count).wv_frozen_flag     := x_plan_deliveries(i).wv_frozen_flag;  -- WV changes
8344               x_delivery_unassigns(l_count).initial_pickup_location_id     := x_plan_deliveries(i).initial_pickup_location_id;
8345 
8346               IF     x_delivery_unassigns(l_count).wms_org_flag = 'Y'
8347                  AND x_delivery_unassigns(l_count).container_flag = 'Y' THEN
8348 
8349                  IF l_debug_on THEN
8350                    WSH_DEBUG_SV.log(l_module_name, 'WMS container to be unassigned', x_delivery_unassigns(l_count).delivery_detail_id);
8351                  END IF;
8352 
8353                  OPEN c_staged_content(x_delivery_unassigns(l_count).delivery_detail_id);
8354                  FETCH c_staged_content INTO l_dummy_id;
8355                  IF c_staged_content%FOUND THEN
8356                    l_flag := 'Y';
8357                  ELSE
8358                    l_flag := 'N';
8359                  END IF;
8360                  CLOSE c_staged_content;
8361 
8362                  IF l_debug_on THEN
8363                    WSH_DEBUG_SV.log(l_module_name, 'lines_staged', l_flag);
8364                  END IF;
8365 
8366                  x_delivery_unassigns(l_count).lines_staged := (l_flag = 'Y');
8367               END IF;
8368 
8369 
8370            END IF;
8371 
8372         END LOOP;
8373 
8374         CLOSE c_assigned_details;
8375 
8376         -- look for partially used details that need to be unassigned
8377         IF l_debug_on THEN
8378           WSH_DEBUG_SV.log(l_module_name, 'l_working_used_details.COUNT', l_working_used_details.COUNT);
8379         END IF;
8380 
8381         IF l_working_used_details.COUNT > 0 THEN --[
8382           l_index := l_working_used_details.FIRST;
8383 
8384           WHILE l_index IS NOT NULL LOOP
8385 
8386             l_next_index := l_working_used_details.NEXT(l_index);
8387 
8388             IF    l_working_used_details(l_index).current_delivery_id IS NULL
8389                OR l_working_used_details(l_index).topmost_cont_id IS NOT NULL THEN
8390               -- remove from working list to reduce the list for future iterations,
8391               -- as this line is not assigned or it is packed in a container.
8392               l_working_used_details.DELETE(l_index);
8393             ELSIF l_working_used_details(l_index).current_delivery_id =
8394                     x_plan_deliveries(i).delivery_id  THEN
8395 
8396               -- Fail Plan if the delivery is planned or firmed.
8397               -- (backup for the logic in flush_details to set WSH_TP_F_FIRM_DEL_UNUSED)
8398 
8399               IF NVL(x_plan_deliveries(i).wsh_planned_flag, 'N') <> 'N' THEN
8400                  Stamp_Interface_Error(p_group_id => x_context.group_id,
8401                                        p_entity_table_name => 'WSH_NEW_DEL_INTERFACE',
8402                                        p_entity_interface_id => x_plan_deliveries(i).del_interface_id,
8403                                        p_message_name => 'WSH_TP_F_PLAN_DEL_NOT_MATCH',
8404                                        x_errors_tab => x_errors_tab,
8405                                        x_return_status => x_return_status);
8406 
8407                  RAISE WSH_PLANNED_DEL_NOT_MATCH;
8408 
8409               END IF;
8410 
8411               x_used_details(l_index).need_unassignment := TRUE;
8412 
8413               -- remove from working list to reduce the list for future iterations.
8414               l_working_used_details.DELETE(l_index);
8415 
8416             END IF;
8417 
8418             l_index := l_next_index;
8419 
8420           END LOOP;
8421 
8422         END IF; --]
8423 
8424       END IF;  --}
8425 
8426   END LOOP;
8427 
8428 
8429   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
8430 
8431   --
8432   --
8433   IF l_debug_on THEN
8434      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
8435      WSH_DEBUG_SV.pop(l_module_name);
8436   END IF;
8437 
8438   EXCEPTION
8439     WHEN WSH_PLANNED_DEL_NOT_MATCH THEN
8440       IF c_assigned_details%isopen THEN
8441          CLOSE c_assigned_details;
8442       END IF;
8443       IF c_has_content%ISOPEN THEN
8444          CLOSE c_has_content;
8445       END IF;
8446       IF c_staged_content%ISOPEN THEN
8447          CLOSE c_staged_content;
8448       END IF;
8449       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
8450       IF l_debug_on THEN
8451           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:WSH_PLANNED_DEL_NOT_MATCH');
8452       END IF;
8453 
8454     WHEN OTHERS THEN
8455       IF c_assigned_details%isopen THEN
8456          CLOSE c_assigned_details;
8457       END IF;
8458       IF c_has_content%ISOPEN THEN
8459          CLOSE c_has_content;
8460       END IF;
8461       IF c_staged_content%ISOPEN THEN
8462          CLOSE c_staged_content;
8463       END IF;
8464       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
8465       WSH_UTIL_CORE.DEFAULT_HANDLER(
8466                         'WSH_TP_RELEASE.MATCH_DELIVERIES',
8467                         l_module_name);
8468 
8469     IF l_debug_on THEN
8470        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
8471        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
8472     END IF;
8473 
8474 END match_deliveries;
8475 
8476 
8477 
8478 --
8479 --  Procedure:          validate_wms
8480 --  Parameters:
8481 --               x_context             context in this session
8482 --               x_plan_details        list of delivery details mapped to interface lines
8483 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
8484 --               x_delivery_unassigns  list of details to unassign in order to validate LPN unassignments
8485 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
8486 --               x_return_status       return status
8487 --
8488 --  Description: Validate that the plan will not affect WMS data.
8489 --                 1.  call WMS to validate the lines released to warehouse that will go into each
8490 --                       delivery can be assigned to that delivery and will not break LPN configuration.
8491 --                 2.  call WMS to validate the staged LPNs in x_delivery_unassigns can be unassigned
8492 --
8493 PROCEDURE validate_wms(
8494            x_context                  IN OUT NOCOPY context_rec_type,
8495            x_plan_details             IN OUT NOCOPY plan_detail_tab_type,
8496            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
8497            x_delivery_unassigns       IN OUT NOCOPY delivery_unassign_tab_type,
8498            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
8499            x_return_status               OUT NOCOPY VARCHAR2
8500           )
8501 IS
8502   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'VALIDATE_WMS';
8503   --
8504   l_debug_on BOOLEAN;
8505 
8506   l_return_status VARCHAR2(1);
8507 
8508   l_wms_table WMS_SHIPPING_INTERFACE_GRP.g_delivery_detail_tbl;
8509   l_del_index NUMBER;
8510   l_dd_index  NUMBER;
8511   l_dd_count  NUMBER;
8512   l_indexes   WSH_UTIL_CORE.ID_TAB_TYPE;
8513   l_unassign_index NUMBER;
8514 
8515   l_discard_rs    VARCHAR2(1);
8516   l_msg_data      VARCHAR2(2000);
8517   l_msg_count     NUMBER;
8518 
8519 BEGIN
8520   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
8521   --
8522   IF l_debug_on IS NULL THEN
8523     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
8524   END IF;
8525 
8526   IF l_debug_on THEN
8527     WSH_DEBUG_SV.push(l_module_name);
8528   END IF;
8529   --
8530   --
8531 
8532   --  1.  call WMS to validate the lines released to warehouse that will go into each
8533   --          delivery can be assigned to that delivery and will not break LPN configuration.
8534 
8535   IF l_debug_on THEN
8536     WSH_DEBUG_SV.log(l_module_name,'1. call WMS to validate lines released to warehouse: x_plan_deliveries.COUNT: ', x_plan_deliveries.COUNT);
8537   END IF;
8538 
8539   l_del_index := x_plan_deliveries.FIRST;
8540   WHILE l_del_index IS NOT NULL LOOP
8541 
8542     IF x_plan_deliveries(l_del_index).wms_org_flag = 'Y' THEN --[
8543 
8544       IF x_plan_deliveries(l_del_index).s_lines_count > 0  THEN
8545 
8546         IF l_debug_on THEN
8547           WSH_DEBUG_SV.log(l_module_name,'Validating S lines in WMS: l_del_index: ', l_del_index);
8548         END IF;
8549 
8550         l_wms_table.DELETE;
8551         l_indexes.DELETE;
8552         l_dd_count := 0;
8553         l_dd_index := x_plan_details.FIRST;
8554 
8555         WHILE     (l_dd_index IS NOT NULL)
8556               AND (l_dd_count < x_plan_deliveries(l_del_index).s_lines_count) LOOP
8557           -- make a list of lines released to warehouse to be validated by WMS
8558 
8559           IF     (x_plan_details(l_dd_index).target_delivery_index = l_del_index)
8560              AND (x_plan_details(l_dd_index).released_status = 'S')  THEN
8561              l_dd_count := l_dd_count + 1;
8562 
8563              -- track index so we can look up interface lines in case of errors
8564              l_indexes(l_dd_count) := l_dd_index;
8565 
8566              l_wms_table(l_dd_count).delivery_detail_id := x_plan_details(l_dd_index).delivery_detail_id;
8567              l_wms_table(l_dd_count).organization_id    := x_plan_details(l_dd_index).organization_id;
8568              l_wms_table(l_dd_count).released_status    := x_plan_details(l_dd_index).released_status;
8569              l_wms_table(l_dd_count).container_flag     := 'N';
8570              l_wms_table(l_dd_count).source_code        := x_plan_details(l_dd_index).source_code;
8571              l_wms_table(l_dd_count).move_order_line_id := x_plan_details(l_dd_index).move_order_line_id;
8572              l_wms_table(l_dd_count).lpn_id             := NULL;
8573           END IF;
8574 
8575           l_dd_index := x_plan_details.NEXT(l_dd_index);
8576         END LOOP;
8577 
8578 
8579         IF l_debug_on THEN
8580           WSH_DEBUG_SV.log(l_module_name,'l_wms_table.COUNT: ', l_wms_table.COUNT);
8581         END IF;
8582 
8583 
8584         IF l_wms_table.COUNT > 0 THEN
8585 
8586           IF l_debug_on THEN
8587             WSH_DEBUG_SV.logmsg(l_module_name,'Calling WMS_SHIPPING_INTERFACE_GRP.process_delivery_details',WSH_DEBUG_SV.C_PROC_LEVEL);
8588           END IF;
8589 
8590           WMS_SHIPPING_INTERFACE_GRP.process_delivery_details (
8591                  p_api_version          => 1.0,
8592                  p_action               => WMS_SHIPPING_INTERFACE_GRP.g_action_unassign_delivery,
8593                  p_delivery_detail_tbl  => l_wms_table,
8594                  x_return_status        => l_return_status,
8595                  x_msg_count            => l_msg_count,
8596                  x_msg_data             => l_msg_data);
8597 
8598           IF l_debug_on THEN
8599             WSH_DEBUG_SV.log(l_module_name,'WMS_SHIPPING_INTERFACE_GRP.process_delivery_details return_status',
8600                                            l_return_status);
8601           END IF;
8602 
8603           IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
8604                                   WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
8605             -- at least one line is breaking LPN configurations in WMS
8606 
8607             -- bug 4552612: issue #3
8608             -- WMS API is setting the same message that is returned below,
8609             -- so it should be purged to avoid repetition.
8610             FND_MSG_PUB.initialize;
8611 
8612             -- per discussion with WMS, this action will not mark lines as
8613             -- having errors, and the message does not have a token.
8614             stamp_interface_error(
8615                       p_group_id            => x_context.group_id,
8616                       p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
8617                       p_entity_interface_id => x_plan_deliveries(l_del_index).del_interface_id,
8618                       p_message_name        => l_wms_table(1).r_message_code,
8619                       p_message_appl        => l_wms_table(1).r_message_appl,
8620                       p_message_text        => l_wms_table(1).r_message_text,
8621                       x_errors_tab          => x_errors_tab,
8622                       x_return_status       => l_discard_rs);
8623 
8624             stamp_interface_error(
8625                     p_group_id            => x_context.group_id,
8626                     p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
8627                     p_entity_interface_id => x_plan_deliveries(l_del_index).del_interface_id,
8628                     p_message_name        => 'WSH_TP_F_WMS_REL_LPN_BREAK',
8629                     p_token_1_name        => 'PLAN_TRIP_NUM',
8630                     p_token_1_value       => get_plan_trip_num(x_context),
8631                     x_errors_tab          => x_errors_tab,
8632                     x_return_status       => l_discard_rs);
8633           END IF;
8634         END IF;
8635 
8636         l_wms_table.DELETE;
8637         l_indexes.DELETE;
8638 
8639         IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
8640                                WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
8641            x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
8642            IF l_debug_on THEN
8643              WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
8644              WSH_DEBUG_SV.pop(l_module_name);
8645           END IF;
8646           RETURN;
8647         END IF;
8648 
8649       END IF;
8650 
8651     END IF; --]
8652 
8653     l_del_index := x_plan_deliveries.NEXT(l_del_index);
8654 
8655   END LOOP;
8656 
8657 
8658   --  2.  go through x_delivery_unassigns, look for staged LPNs to call WMS to validate they
8659   --      can be unassigned from deliveries.
8660 
8661   IF l_debug_on THEN
8662     WSH_DEBUG_SV.log(l_module_name,'2. go through x_delivery_unassigns: x_delivery_unassigns.COUNT', x_delivery_unassigns.COUNT);
8663   END IF;
8664 
8665   -- Bug 3555487 initialize message stack for each major action point.
8666   FND_MSG_PUB.initialize;
8667 
8668   IF x_delivery_unassigns.COUNT > 0 THEN --[
8669 
8670     l_wms_table.DELETE;
8671     l_dd_count  := 0;
8672     l_indexes.DELETE;
8673     l_unassign_index := x_delivery_unassigns.FIRST;
8674 
8675     WHILE l_unassign_index IS NOT NULL LOOP
8676        IF     x_delivery_unassigns(l_unassign_index).wms_org_flag = 'Y'
8677           AND x_delivery_unassigns(l_unassign_index).container_flag = 'Y'
8678           AND x_delivery_unassigns(l_unassign_index).lines_staged  THEN
8679 
8680          l_dd_count := l_dd_count + 1;
8681 
8682          -- track index so we can look up interface lines in case of errors
8683          l_indexes(l_dd_count) := l_unassign_index;
8684 
8685          l_wms_table(l_dd_count).delivery_detail_id := x_delivery_unassigns(l_unassign_index).delivery_detail_id;
8686          l_wms_table(l_dd_count).organization_id    := x_delivery_unassigns(l_unassign_index).organization_id;
8687          l_wms_table(l_dd_count).released_status    := x_delivery_unassigns(l_unassign_index).released_status;
8688          l_wms_table(l_dd_count).container_flag     := x_delivery_unassigns(l_unassign_index).container_flag;
8689          l_wms_table(l_dd_count).source_code        := x_delivery_unassigns(l_unassign_index).source_code;
8690          l_wms_table(l_dd_count).lpn_id             := x_delivery_unassigns(l_unassign_index).lpn_id;
8691        END IF;
8692 
8693        l_unassign_index := x_delivery_unassigns.NEXT(l_unassign_index);
8694     END LOOP;
8695 
8696     IF l_debug_on THEN
8697       WSH_DEBUG_SV.log(l_module_name,'l_wms_table.COUNT: ', l_wms_table.COUNT);
8698     END IF;
8699 
8700     IF l_wms_table.COUNT > 0 THEN  --{
8701       IF l_debug_on THEN
8702         WSH_DEBUG_SV.logmsg(l_module_name,'Calling WMS_SHIPPING_INTERFACE_GRP.process_delivery_details',WSH_DEBUG_SV.C_PROC_LEVEL);
8703       END IF;
8704 
8705       WMS_SHIPPING_INTERFACE_GRP.process_delivery_details (
8706                p_api_version          => 1.0,
8707                p_action               => WMS_SHIPPING_INTERFACE_GRP.g_action_unassign_delivery,
8708                p_delivery_detail_tbl  => l_wms_table,
8709                x_return_status        => l_return_status,
8710                x_msg_count            => l_msg_count,
8711                x_msg_data             => l_msg_data);
8712 
8713       IF l_debug_on THEN
8714         WSH_DEBUG_SV.log(l_module_name,'WMS_SHIPPING_INTERFACE_GRP.process_delivery_details return_status',
8715                                        l_return_status);
8716       END IF;
8717 
8718       -- bug 4552612: issue #4
8719       -- fixed error-handling to correctly handle the results:
8720       --   API returning success means to check the list for specific errors.
8721       IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
8722                               WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
8723         NULL;  -- here, internal issues have happened.
8724                -- We will error out below.
8725                -- We will also keep the messages on stack to be stamped.
8726       ELSE
8727         -- check the list to find out
8728         -- whether any LPN cannot be unassigned in WMS.
8729         -- If so, set l_return_status to ERROR.
8730 
8731         -- bug 4552612: issue #3
8732         -- if WMS API has set messages for failed validation,
8733         -- they need to be purged to avoid repetition
8734         -- because the same messages will be set below.
8735         FND_MSG_PUB.initialize;
8736 
8737         l_dd_index := l_wms_table.FIRST;
8738         WHILE l_dd_index IS NOT NULL LOOP
8739           IF (l_wms_table(l_dd_index).return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
8740                                                         WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
8741             l_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
8742             IF x_delivery_unassigns(l_indexes(l_dd_index)).plan_dd_index IS NOT NULL THEN
8743               stamp_interface_error(
8744                       p_group_id            => x_context.group_id,
8745                       p_entity_table_name   => 'WSH_DEL_DETAILS_INTERFACE',
8746                       p_entity_interface_id => x_plan_details(x_delivery_unassigns(l_indexes(l_dd_index)).plan_dd_index).dd_interface_id,
8747                       p_message_name        => l_wms_table(l_dd_index).r_message_code,
8748                       p_message_appl        => l_wms_table(l_dd_index).r_message_appl,
8749                       p_message_text        => l_wms_table(l_dd_index).r_message_text,
8750                       p_token_1_name        => 'LPN_NAME',
8751                       p_token_1_value       => l_wms_table(l_dd_index).r_message_token,
8752                       x_errors_tab          => x_errors_tab,
8753                       x_return_status       => l_discard_rs);
8754             ELSIF x_delivery_unassigns(l_indexes(l_dd_index)).plan_del_index IS NOT NULL THEN
8755               stamp_interface_error(
8756                       p_group_id            => x_context.group_id,
8757                       p_entity_table_name   => 'WSH_NEW_DEL_INTERFACE',
8758                       p_entity_interface_id => x_plan_deliveries(x_delivery_unassigns(l_indexes(l_dd_index)).plan_del_index).del_interface_id,
8759                       p_message_name        => l_wms_table(l_dd_index).r_message_code,
8760                       p_message_appl        => l_wms_table(l_dd_index).r_message_appl,
8761                       p_message_text        => l_wms_table(l_dd_index).r_message_text,
8762                       p_token_1_name        => 'LPN_NAME',
8763                       p_token_1_value       => l_wms_table(l_dd_index).r_message_token,
8764                       x_errors_tab          => x_errors_tab,
8765                       x_return_status       => l_discard_rs);
8766             ELSE
8767               stamp_interface_error(
8768                       p_group_id            => x_context.group_id,
8769                       p_entity_table_name   => 'NONE',
8770                       p_entity_interface_id => -1,
8771                       p_message_name        => l_wms_table(l_dd_index).r_message_code,
8772                       p_message_appl        => l_wms_table(l_dd_index).r_message_appl,
8773                       p_message_text        => l_wms_table(l_dd_index).r_message_text,
8774                       p_token_1_name        => 'LPN_NAME',
8775                       p_token_1_value       => l_wms_table(l_dd_index).r_message_token,
8776                       x_errors_tab          => x_errors_tab,
8777                       x_return_status       => l_discard_rs);
8778             END IF;
8779           END IF;
8780 
8781           l_dd_index := l_wms_table.NEXT(l_dd_index);
8782         END LOOP;
8783 
8784         IF l_debug_on THEN
8785           WSH_DEBUG_SV.log(l_module_name,'l_return status after scanning wms list',
8786                                           l_return_status);
8787         END IF;
8788 
8789       END IF;
8790 
8791       IF (l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
8792                               WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)) THEN
8793         stamp_interface_error(
8794                       p_group_id            => x_context.group_id,
8795                       p_entity_table_name   => 'NONE',
8796                       p_entity_interface_id => -1,
8797                       p_message_name        => 'WSH_TP_F_WMS_LPN_BREAK',
8798                       p_token_1_name        => 'PLAN_TRIP_NUM',
8799                       p_token_1_value       => get_plan_trip_num(x_context),
8800                       x_errors_tab          => x_errors_tab,
8801                       x_return_status       => l_discard_rs);
8802       END IF;
8803 
8804       l_wms_table.DELETE;
8805       l_indexes.DELETE;
8806 
8807       IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
8808                              WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
8809         x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
8810         IF l_debug_on THEN
8811           WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
8812           WSH_DEBUG_SV.pop(l_module_name);
8813         END IF;
8814         RETURN;
8815       END IF;
8816 
8817     END IF; --}
8818 
8819   END IF;  --]
8820 
8821   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
8822 
8823   --
8824   --
8825   IF l_debug_on THEN
8826      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
8827      WSH_DEBUG_SV.pop(l_module_name);
8828   END IF;
8829 
8830   EXCEPTION
8831     WHEN OTHERS THEN
8832       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
8833       WSH_UTIL_CORE.DEFAULT_HANDLER(
8834                         'WSH_TP_RELEASE.VALIDATE_WMS',
8835                         l_module_name);
8836 
8837     IF l_debug_on THEN
8838        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
8839        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
8840     END IF;
8841 
8842 END validate_wms;
8843 
8844 
8845 
8846 
8847 --
8848 --  Procedure:          match_trips
8849 --  Parameters:
8850 --               x_context             context in this session
8851 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
8852 --               x_plan_stops          list of stops mapped to interface stops
8853 --               x_plan_trips          list of trips mapped to interface trips
8854 --               x_trip_unassigns      list of deliveries to unassign from their trips
8855 --               x_obsoleted_stops     list of mapped trips' stops that are not mapped in the plan
8856 --               x_errors_tab          list of errors to insert into wsh_interface_errors at the end
8857 --               x_return_status       return status
8858 --
8859 --  Description:
8860 --               Goes through each mapped trip to identify deliveries
8861 --               that need to be unassigned and stops that need to be removed
8862 --               so that it will match the plan's trip.
8863 --               Linked stops count as one stop.
8864 --               x_trip_unassigns will have the unassignments.
8865 --               x_obsoleted_stops will have the unassignments.
8866 --
8867 PROCEDURE match_trips(
8868            x_context                  IN OUT NOCOPY context_rec_type,
8869            x_plan_deliveries          IN OUT NOCOPY plan_delivery_tab_type,
8870            x_plan_stops               IN OUT NOCOPY plan_stop_tab_type,
8871            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
8872            x_trip_unassigns           IN OUT NOCOPY trip_unassign_tab_type,
8873            x_obsoleted_stops          IN OUT NOCOPY obsoleted_stop_tab_type,
8874            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
8875            x_return_status               OUT NOCOPY VARCHAR2
8876           )
8877 IS
8878   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'MATCH_TRIPS';
8879   --
8880   l_debug_on BOOLEAN;
8881 
8882   -- list the open stops in a trip in sequence
8883   CURSOR c_stops_in_trip(p_trip_id IN NUMBER) IS
8884   SELECT ts.stop_id,
8885          ts.physical_location_id,
8886          ts.physical_stop_id,
8887          ts.planned_arrival_date
8888   FROM   wsh_trip_stops ts
8889   WHERE  ts.trip_id = p_trip_id
8890   AND    ts.status_code = 'OP'
8891   ORDER BY ts.stop_sequence_number;
8892 
8893   -- find deliveries associated with each stop
8894   -- as a pick up or drop off.
8895   CURSOR c_legs_in_trip(p_stop_id IN NUMBER) IS
8896   SELECT l.delivery_id,
8897          d.organization_id,
8898          l.pick_up_stop_id,
8899          l.drop_off_stop_id
8900   FROM   wsh_delivery_legs l,
8901          wsh_new_deliveries d
8902   WHERE  l.pick_up_stop_id = p_stop_id
8903   AND    d.delivery_id = l.delivery_id
8904   UNION
8905   SELECT l.delivery_id,
8906          d.organization_id,
8907          l.pick_up_stop_id,
8908          l.drop_off_stop_id
8909   FROM   wsh_delivery_legs l,
8910          wsh_new_deliveries d
8911   WHERE  l.drop_off_stop_id = p_stop_id
8912   AND    d.delivery_id = l.delivery_id;
8913 
8914   l_trip_count NUMBER;
8915   l_stop_count NUMBER;
8916   l_stop_id NUMBER;
8917   l_stop_matches VARCHAR2(1);
8918   l_leg_id NUMBER;
8919   l_delivery_matches VARCHAR2(1);
8920   l_stops_start_index NUMBER;
8921   l_stop_index NUMBER;
8922   l_temp_unassigned_dels WSH_UTIL_CORE.ID_TAB_TYPE;
8923   l_delivery_unassigned VARCHAR2(1);
8924   l_tmp_entity_name VARCHAR2(30);
8925 
8926   l_unassign_mark NUMBER;
8927   l_index         NUMBER;
8928 
8929   WSH_PLANNED_TRIP_NOT_MATCH EXCEPTION;
8930 
8931 BEGIN
8932 
8933   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
8934   --
8935   IF l_debug_on IS NULL THEN
8936     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
8937   END IF;
8938 
8939   IF l_debug_on THEN
8940     WSH_DEBUG_SV.push(l_module_name);
8941   END IF;
8942   --
8943   --
8944 
8945   l_trip_count :=  x_trip_unassigns.count;
8946   l_stop_count :=  x_obsoleted_stops.count;
8947 
8948   -- bug 3303766: mark the last x_trip_unassigns record that was created
8949   --  by the leg mapping process in API generate_lock_candidates
8950   l_unassign_mark := x_trip_unassigns.COUNT;
8951   IF l_unassign_mark > 0 THEN
8952     l_unassign_mark := x_trip_unassigns.LAST;
8953   END IF;
8954 
8955 
8956   -- Loop through the list of trips in the plan.
8957 
8958   FOR i in 1.. x_plan_trips.count LOOP
8959 
8960     -- Skip if trip is not mapped to an existing trip.
8961 
8962     IF x_plan_trips(i).trip_id IS NOT NULL THEN -- {
8963 
8964       IF l_debug_on THEN
8965         WSH_DEBUG_SV.log(l_module_name, 'looping x_plan_trips(i).trip_id', x_plan_trips(i).trip_id);
8966       END IF;
8967 
8968       l_stops_start_index := x_plan_trips(i).stop_base_index;
8969 
8970       FOR  s IN c_stops_in_trip(x_plan_trips(i).trip_id)
8971       LOOP
8972 
8973         IF l_debug_on THEN
8974           WSH_DEBUG_SV.log(l_module_name, 'looping stop_id to match plan', s.stop_id);
8975         END IF;
8976 
8977         l_stop_matches := 'N';
8978         l_stop_index := l_stops_start_index;
8979 
8980         -- l_stops_start_index becomes NULL when the last plan stop is mapped.
8981         -- The last trip being mapped might have extra stops that need to be removed.
8982         IF l_stop_index IS NOT NULL THEN
8983           LOOP
8984           EXIT WHEN (x_plan_stops(l_stop_index).trip_index <> i);
8985 
8986             IF (x_plan_stops(l_stop_index).stop_id = s.stop_id)
8987             AND (x_plan_stops(l_stop_index).planned_arrival_date = s.planned_arrival_date) THEN
8988 
8989                l_stop_matches := 'Y';
8990                l_stops_start_index := x_plan_stops.NEXT(l_stop_index);
8991                IF l_debug_on THEN
8992                  WSH_DEBUG_SV.log(l_module_name, 'stop matches index', l_stop_index);
8993                  WSH_DEBUG_SV.log(l_module_name, 'l_stops_start_index', l_stops_start_index);
8994                END IF;
8995                EXIT;
8996 
8997             ELSIF (x_plan_stops(l_stop_index).planned_arrival_date >  s.planned_arrival_date) THEN
8998                EXIT;
8999             END IF;
9000             l_stop_index := x_plan_stops.NEXT(l_stop_index);
9001 
9002             IF l_stop_index IS NULL THEN
9003                EXIT;
9004             END IF;
9005 
9006           END LOOP;
9007         END IF;
9008 
9009 
9010         IF     l_stop_matches = 'N' THEN
9011           IF l_stop_index IS NOT NULL AND NVL(s.physical_stop_id, -1) = x_plan_stops(l_stop_index).stop_id THEN
9012             -- linked stops are considered to match the physical stop
9013             l_stop_matches := 'Y';
9014 
9015             IF l_debug_on THEN
9016               WSH_DEBUG_SV.log(l_module_name,'matching linked stop', s.stop_id);
9017             END IF;
9018 
9019             BEGIN
9020               wsh_trip_stops_pvt.lock_trip_stop_no_compare(
9021                     p_stop_id => s.stop_id);
9022             EXCEPTION
9023               WHEN OTHERS THEN
9024                    stamp_interface_error(
9025                                p_group_id            => x_context.group_id,
9026                                p_entity_table_name   => 'WSH_TRIP_INTERFACE',
9027                                p_entity_interface_id => x_plan_trips(i).trip_interface_id,
9028                                p_message_name        => 'WSH_TP_F_NO_LOCK_STOP',
9029                                p_token_1_name        => 'TRIP_NAME',
9030                                p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(i).trip_id),
9031                                x_errors_tab          => x_errors_tab,
9032                                x_return_status       => x_return_status);
9033                    x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9034                    IF l_debug_on THEN
9035                      WSH_DEBUG_SV.pop(l_module_name);
9036                    END IF;
9037                    RETURN;
9038             END;
9039 
9040           ELSE
9041 
9042             -- Fail plan if a stop in a firmed or planned trip does not match
9043 
9044             IF NVL(x_plan_trips(i).wsh_planned_flag,'N') <> 'N' THEN
9045 
9046                IF l_debug_on THEN
9047                   WSH_DEBUG_SV.logmsg(l_module_name,'plan does not match firmed trip having stop_id', s.stop_id);
9048                END IF;
9049 
9050                Stamp_Interface_Error(p_group_id => x_context.group_id,
9051                                      p_entity_table_name => 'WSH_TRIPS_INTERFACE',
9052                                      p_entity_interface_id => x_plan_trips(i).trip_interface_id,
9053                                      p_message_name => 'WSH_TP_F_PLAN_TRIP_NOT_MATCH',
9054                                      p_token_1_name => 'PLAN_TRIP_NUM',
9055                                      p_token_1_value => x_plan_trips(i).tp_trip_number,
9056                                      p_token_2_name => 'TRIP_NAME',
9057                                      p_token_2_value => WSH_TRIPS_PVT.get_name(x_plan_trips(i).trip_id),
9058                                      x_errors_tab => x_errors_tab,
9059                                      x_return_status => x_return_status);
9060 
9061                RAISE WSH_PLANNED_TRIP_NOT_MATCH;
9062 
9063             END IF;
9064 
9065             -- Lock the stop.
9066             BEGIN
9067               wsh_trip_stops_pvt.lock_trip_stop_no_compare(
9068                     p_stop_id => s.stop_id);
9069               EXCEPTION
9070                 WHEN OTHERS THEN
9071                     stamp_interface_error(
9072                                 p_group_id            => x_context.group_id,
9073                                 p_entity_table_name   => 'WSH_TRIP_INTERFACE',
9074                                 p_entity_interface_id => x_plan_trips(i).trip_interface_id,
9075                                 p_message_name        => 'WSH_TP_F_NO_LOCK_STOP_UNASSIGN',
9076                                 p_token_1_name        => 'TRIP_NAME',
9077                                 p_token_1_value       => WSH_TRIPS_PVT.get_name(x_plan_trips(i).trip_id),
9078                                 x_errors_tab          => x_errors_tab,
9079                                 x_return_status       => x_return_status);
9080                     x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9081                     IF l_debug_on THEN
9082                       WSH_DEBUG_SV.pop(l_module_name);
9083                     END IF;
9084                     RETURN;
9085             END;
9086 
9087             -- Add to list of obsoleted stops.
9088 
9089             l_stop_count := l_stop_count + 1;
9090             x_obsoleted_stops(l_stop_count).trip_id := x_plan_trips(i).trip_id;
9091             x_obsoleted_stops(l_stop_count).stop_id := s.stop_id;
9092           END IF;
9093 
9094         END IF;
9095 
9096         FOR leg in c_legs_in_trip(s.stop_id) LOOP
9097 
9098           -- Check if this delivery has already been added to the unassigned list.
9099           -- This can happen as the delivery is attached to both the pickup as
9100           -- well as the dropoff stop, and we are looping through all the stops for
9101           -- that trip.
9102 
9103           IF l_debug_on THEN
9104             WSH_DEBUG_SV.log(l_module_name, 'looping leg to match trip: delivery_id', leg.delivery_id);
9105           END IF;
9106 
9107           l_delivery_unassigned := 'N';
9108 
9109           FOR k in 1.. l_temp_unassigned_dels.count LOOP
9110 
9111               IF l_temp_unassigned_dels(k) = leg.delivery_id THEN
9112 
9113                  l_delivery_unassigned := 'Y';
9114                  EXIT;
9115 
9116               END IF;
9117 
9118           END LOOP;
9119 
9120 
9121           IF l_delivery_unassigned = 'N' THEN
9122 
9123             l_delivery_matches := 'N';
9124 
9125             IF l_stop_matches = 'Y' THEN
9126 
9127                FOR j in 1..x_plan_deliveries.count LOOP
9128 
9129                    IF (x_plan_deliveries(j).delivery_id = leg.delivery_id) THEN
9130 
9131                       l_delivery_matches := 'Y';
9132 
9133                       EXIT;
9134 
9135                    END IF;
9136 
9137                END LOOP;
9138 
9139             END IF;
9140 
9141 
9142 
9143             IF l_delivery_matches = 'N' THEN
9144 
9145                 -- Fail plan if a delivery in a firmed trip does not match.
9146                 IF l_debug_on THEN
9147                  WSH_DEBUG_SV.log(l_module_name,'plan does not match trip having delivery_id', leg.delivery_id);
9148                 END IF;
9149 
9150                 IF NVL(x_plan_trips(i).wsh_planned_flag,'N') =  'F' THEN
9151                    Stamp_Interface_Error(p_group_id => x_context.group_id,
9152                                          p_entity_table_name => 'WSH_TRIPS_INTERFACE',
9153                                          p_entity_interface_id => x_plan_trips(i).trip_interface_id,
9154                                          p_message_name => 'WSH_TP_F_PLAN_TRIP_NOT_MATCH',
9155                                          x_errors_tab => x_errors_tab,
9156                                          x_return_status => x_return_status);
9157 
9158                    RAISE WSH_PLANNED_TRIP_NOT_MATCH;
9159 
9160                 END IF;
9161 
9162                 -- bug 3303766: make sure x_trip_unassigns does not already have this unassignment
9163                 --  which would be populated at time of mapping the delivery's leg.
9164                 --  Possible scenario: This delivery may need to be unassigned from the TE trip because
9165                 --    its plan stops do not match the TE stops but another delivery may reuse that
9166                 --    TE trip which is to carry both deliveries.
9167                 --
9168                 --  Scan only the x_trip_unassigns records created by the leg mapping process
9169                 --  in the API generate_lock_candidates.
9170                 IF l_unassign_mark > 0 THEN
9171                   l_index := x_trip_unassigns.FIRST;
9172                   WHILE l_index <= l_unassign_mark LOOP
9173                      IF     x_trip_unassigns(l_index).trip_id     = x_plan_trips(i).trip_id
9174                         AND x_trip_unassigns(l_index).delivery_id = leg.delivery_id THEN
9175                        l_delivery_matches := 'Y';
9176                        EXIT;
9177                      END IF;
9178                      l_index := x_trip_unassigns.NEXT(l_index);
9179                    END LOOP;
9180                 END IF;
9181 
9182                 IF l_delivery_matches = 'N' THEN
9183                   -- Lock the delivery.
9184                   BEGIN
9185                     wsh_new_deliveries_pvt.lock_dlvy_no_compare(
9186                            p_delivery_id => leg.delivery_id);
9187                     EXCEPTION
9188                       WHEN OTHERS THEN
9189                           l_tmp_entity_name :=  WSH_NEW_DELIVERIES_PVT.Get_Name(leg.delivery_id);
9190                           stamp_interface_error(
9191                                  p_group_id            => x_context.group_id,
9192                                  p_entity_table_name   => 'WSH_TRIP_INTERFACE',
9193                                  p_entity_interface_id => x_plan_trips(i).trip_interface_id,
9194                                  p_message_name        => 'WSH_TP_F_NO_LOCK_DEL_UNASSIGN',
9195                                  p_token_1_name        => 'DELIVERY_NAME',
9196                                  p_token_1_value       => l_tmp_entity_name,
9197                                  x_errors_tab          => x_errors_tab,
9198                                  x_return_status       => x_return_status);
9199                           x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9200                           IF l_debug_on THEN
9201                             WSH_DEBUG_SV.pop(l_module_name);
9202                           END IF;
9203                           RETURN;
9204                   END;
9205 
9206                   -- Add to list of deliveries to be unassigned.
9207 
9208                   l_trip_count := l_trip_count + 1;
9209                   x_trip_unassigns(l_trip_count).delivery_id     := leg.delivery_id;
9210                   x_trip_unassigns(l_trip_count).organization_id := leg.organization_id;
9211                   x_trip_unassigns(l_trip_count).trip_id         := x_plan_trips(i).trip_id;
9212                   x_trip_unassigns(l_trip_count).trip_index      := i;
9213                   x_trip_unassigns(l_trip_count).pickup_stop_id  := leg.pick_up_stop_id;
9214                   x_trip_unassigns(l_trip_count).dropoff_stop_id := leg.drop_off_stop_id;
9215 
9216                 END IF;
9217 
9218                 -- track this delivery internally so we can skip it when we find its drop off stop.
9219                 l_temp_unassigned_dels(l_temp_unassigned_dels.count + 1) := leg.delivery_id;
9220 
9221             END IF;
9222 
9223           END IF;
9224 
9225         END LOOP;
9226 
9227       END LOOP;
9228 
9229       -- Refresh this list for each trip.
9230 
9231       l_temp_unassigned_dels.delete;
9232 
9233     END IF; --}
9234 
9235   END LOOP;
9236 
9237   --
9238   --
9239   IF l_debug_on THEN
9240      WSH_DEBUG_SV.log(l_module_name, 'x_return_status', x_return_status);
9241      WSH_DEBUG_SV.pop(l_module_name);
9242   END IF;
9243 
9244   EXCEPTION
9245     WHEN WSH_PLANNED_TRIP_NOT_MATCH THEN
9246       IF c_stops_in_trip%isopen THEN
9247          CLOSE c_stops_in_trip;
9248       END IF;
9249       IF c_legs_in_trip%isopen THEN
9250          CLOSE c_legs_in_trip;
9251       END IF;
9252       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9253       IF l_debug_on THEN
9254           WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:WSH_PLANNED_DEL_NOT_MATCH');
9255       END IF;
9256     WHEN OTHERS THEN
9257       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
9258       WSH_UTIL_CORE.DEFAULT_HANDLER(
9259                         'WSH_TP_RELEASE.MATCH_TRIPS',
9260                         l_module_name);
9261 
9262     IF l_debug_on THEN
9263        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
9264        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
9265     END IF;
9266 
9267 END match_trips;
9268 
9269 
9270 --
9271 --  Procedure:          debug_dump_context
9272 --  Parameters:
9273 --               x_context             context in this session
9274 --               x_plan_details        list of delivery details mapped to interface lines
9275 --               x_track_conts         list of topmost containers to track
9276 --               x_plan_deliveries     list of deliveries mapped to interface deliveries
9277 --               x_plan_legs           list of delivery legs mapped to interface legs
9278 --               x_plan_stops          list of stops mapped to interface stops
9279 --               x_plan_trips          list of trips mapped to interface trips
9280 --               x_plan_trip_moves     list of trip moves mapped to interface trip moves (FTE)
9281 --               x_plan_moves          list of moves mapped to interface moves (FTE)
9282 --               x_used_details        list of delivery details partially used by the plan
9283 --               x_delivery_unassigns  list of delivery lines to unassign from their deliveries
9284 --               x_trip_unassigns      list of deliveries to unassign from their trips
9285 --               x_obsoleted_stops     list of mapped trips' stops that are not mapped in the plan
9286 --               x_obsoleted_trip_moves  list of mapped trips' moves that are not mapped in the plan
9287 --
9288 --  Description:
9289 --               Dump the state for debugging purposes.
9290 --
9291 
9292 PROCEDURE debug_dump_context(
9293            x_context                  IN context_rec_type,
9294            x_plan_details             IN plan_detail_tab_type,
9295            x_track_conts              IN track_cont_tab_type,
9296            x_plan_deliveries          IN plan_delivery_tab_type,
9297            x_plan_legs                IN plan_leg_tab_type,
9298            x_plan_stops               IN plan_stop_tab_type,
9299            x_plan_trips               IN plan_trip_tab_type,
9300            x_plan_trip_moves          IN WSH_FTE_TP_INTEGRATION.plan_trip_move_tab_type,
9301            x_plan_moves               IN WSH_FTE_TP_INTEGRATION.plan_move_tab_type,
9302            x_used_details             IN used_details_tab_type,
9303            x_delivery_unassigns       IN delivery_unassign_tab_type,
9304            x_trip_unassigns           IN trip_unassign_tab_type,
9305            x_obsoleted_stops          IN obsoleted_stop_tab_type,
9306            x_obsoleted_trip_moves     IN WSH_FTE_TP_INTEGRATION.obsoleted_trip_move_tab_type
9307           )
9308 IS
9309   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'DEBUG_DUMP_CONTEXT';
9310   --
9311   l_debug_on BOOLEAN;
9312   --
9313   l_index NUMBER;
9314   l_string VARCHAR2(2000);
9315 BEGIN
9316   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
9317        --
9318   IF l_debug_on IS NULL THEN
9319      l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
9320   END IF;
9321 
9322   IF l_debug_on THEN
9323     WSH_DEBUG_SV.push(l_module_name);
9324 
9325 
9326     WSH_DEBUG_SV.logmsg(l_module_name, '------- start of dump -----------');
9327 
9328     -- dump x_context
9329     WSH_DEBUG_SV.log(l_module_name, 'x_context.group_id', x_context.group_id);
9330 
9331     IF x_context.wms_in_group THEN
9332       WSH_DEBUG_SV.log(l_module_name, 'x_context.wms_in_group', 'TRUE');
9333     ELSE
9334       WSH_DEBUG_SV.log(l_module_name, 'x_context.wms_in_group', 'FALSE');
9335     END IF;
9336     WSH_DEBUG_SV.log(l_module_name, 'x_context.auto_tender_flag', x_context.auto_tender_flag);
9337     WSH_DEBUG_SV.log(l_module_name, 'x_context.linked_trip_count', x_context.linked_trip_count);
9338 
9339 
9340     -- dump x_plan_details
9341     WSH_DEBUG_SV.log(l_module_name, 'x_plan_details.COUNT', x_plan_details.COUNT);
9342     IF x_plan_details.COUNT > 0  THEN
9343       l_index := x_plan_details.FIRST;
9344       WHILE l_index IS NOT NULL LOOP
9345        --
9346        l_string :=              'dd_interface_id=' || x_plan_details(l_index).dd_interface_id;
9347        l_string := l_string || ', dd=' || x_plan_details(l_index).delivery_detail_id;
9348        l_string := l_string || ', tp_dd=' || x_plan_details(l_index).tp_delivery_detail_id;
9349        l_string := l_string || ', map_qty=' || x_plan_details(l_index).mapped_quantity || ' ' || x_plan_details(l_index).mapped_quantity_uom;
9350        l_string := l_string || ', split=' || x_plan_details(l_index).map_split_flag;
9351        l_string := l_string || ', RS=' || x_plan_details(l_index).released_status;
9352        l_string := l_string || ', LD=' || x_plan_details(l_index).line_direction;
9353        l_string := l_string || ', top_cont_id=' || x_plan_details(l_index).topmost_cont_id;
9354        l_string := l_string || ', cur_del_id=' || x_plan_details(l_index).current_delivery_id;
9355        l_string := l_string || ', target_del_index=' || x_plan_details(l_index).target_delivery_index;
9356        --
9357        WSH_DEBUG_SV.log(l_module_name, 'iline(' || l_index || ')', l_string);
9358        l_index := x_plan_details.NEXT(l_index);
9359       END LOOP;
9360     END IF;
9361 
9362     -- dump x_track_conts
9363     WSH_DEBUG_SV.log(l_module_name, 'x_track_conts.COUNT', x_track_conts.COUNT);
9364     IF x_track_conts.COUNT > 0  THEN
9365       l_index := x_track_conts.FIRST;
9366       WHILE l_index IS NOT NULL LOOP
9367        --
9368        l_string := 'topmost_cont_id=' || x_track_conts(l_index).topmost_cont_id;
9369        l_string := l_string || ', current_delivery_id=' || x_track_conts(l_index).current_delivery_id;
9370        l_string := l_string || ', target_delivery_index=' || x_track_conts(l_index).target_delivery_index;
9371        IF x_track_conts(l_index).lines_staged THEN
9372          l_string := l_string || ', lines_staged=TRUE';
9373        ELSE
9374          l_string := l_string || ', lines_staged=FALSE';
9375        END IF;
9376        l_string := l_string || ', lpn_id=' || x_track_conts(l_index).lpn_id;
9377        --
9378        WSH_DEBUG_SV.log(l_module_name, 'tcont(' || l_index || ')', l_string);
9379        l_index := x_track_conts.NEXT(l_index);
9380       END LOOP;
9381     END IF;
9382 
9383     -- dump x_plan_deliveries
9384     WSH_DEBUG_SV.log(l_module_name, 'x_plan_deliveries.COUNT', x_plan_deliveries.COUNT);
9385     IF x_plan_deliveries.COUNT > 0  THEN
9386       l_index := x_plan_deliveries.FIRST;
9387       WHILE l_index IS NOT NULL LOOP
9388        --
9389        l_string := 'del_interface_id=' || x_plan_deliveries(l_index).del_interface_id;
9390        l_string := l_string || ', del=' || x_plan_deliveries(l_index).delivery_id;
9391        l_string := l_string || ', tp_del=' || x_plan_deliveries(l_index).tp_delivery_number;
9392        l_string := l_string || ', planned=' || x_plan_deliveries(l_index).planned_flag;
9393        l_string := l_string || ', wsh_planned=' || x_plan_deliveries(l_index).wsh_planned_flag;
9394        l_string := l_string || ', SD=' || x_plan_deliveries(l_index).shipment_direction;
9395        l_string := l_string || ', IPU=' || x_plan_deliveries(l_index).initial_pickup_location_id;
9396        l_string := l_string || '-' || FND_DATE.DATE_TO_CANONICAL(x_plan_deliveries(l_index).initial_pickup_date);
9397        l_string := l_string || ', UDO=' ||  x_plan_deliveries(l_index).ultimate_dropoff_location_id;
9398        l_string := l_string || ', P_UDO=' ||  x_plan_deliveries(l_index).physical_ultimate_do_loc_id;
9399        l_string := l_string || '-' || FND_DATE.DATE_TO_CANONICAL(x_plan_deliveries(l_index).ultimate_dropoff_date);
9400        l_string := l_string || ', ilines_count=' || x_plan_deliveries(l_index).ilines_count;
9401        l_string := l_string || ', lines_count=' || x_plan_deliveries(l_index).lines_count;
9402        l_string := l_string || ', s_lines_count=' || x_plan_deliveries(l_index).s_lines_count;
9403        l_string := l_string || ', d_conts_count=' || x_plan_deliveries(l_index).dangling_conts_count;
9404        l_string := l_string || ', wms_org_flag=' || x_plan_deliveries(l_index).wms_org_flag;
9405        l_string := l_string || ', leg_base_index=' || x_plan_deliveries(l_index).leg_base_index;
9406        --
9407        WSH_DEBUG_SV.log(l_module_name, 'idel(' || l_index || ')', l_string);
9408        l_index := x_plan_deliveries.NEXT(l_index);
9409       END LOOP;
9410     END IF;
9411 
9412     -- dump x_plan_legs
9413     WSH_DEBUG_SV.log(l_module_name, 'x_plan_legs.COUNT', x_plan_legs.COUNT);
9414     IF x_plan_legs.COUNT > 0  THEN
9415       l_index := x_plan_legs.FIRST;
9416       WHILE l_index IS NOT NULL LOOP
9417        --
9418        l_string := 'leg_interface_id=' || x_plan_legs(l_index).leg_interface_id;
9419        l_string := l_string || ', leg_id=' || x_plan_legs(l_index).delivery_leg_id;
9420        l_string := l_string || ', del_interface_id=' || x_plan_legs(l_index).del_interface_id;
9421        l_string := l_string || ', delivery_index=' || x_plan_legs(l_index).delivery_index;
9422        l_string := l_string || ', pickup_stop_index=' || x_plan_legs(l_index).pickup_stop_index;
9423        l_string := l_string || ', dropoff_stop_index=' || x_plan_legs(l_index).dropoff_stop_index;
9424        l_string := l_string || ', trip_index=' || x_plan_legs(l_index).trip_index;
9425        --
9426        WSH_DEBUG_SV.log(l_module_name, 'ileg(' || l_index || ')', l_string);
9427        l_index := x_plan_legs.NEXT(l_index);
9428       END LOOP;
9429     END IF;
9430 
9431     -- dump x_plan_stops
9432     WSH_DEBUG_SV.log(l_module_name, 'x_plan_stops.COUNT', x_plan_stops.COUNT);
9433     IF x_plan_stops.COUNT > 0  THEN
9434       l_index := x_plan_stops.FIRST;
9435       WHILE l_index IS NOT NULL LOOP
9436        l_string := '';
9437        --
9438        l_string := 'stop_interface_id=' || x_plan_stops(l_index).stop_interface_id;
9439        l_string := l_string || ', stop=' || x_plan_stops(l_index).stop_id;
9440        l_string := l_string || ', tp_stop=' || x_plan_stops(l_index).tp_stop_id;
9441        l_string := l_string || ', trip_index=' || x_plan_stops(l_index).trip_index;
9442        l_string := l_string || ', stop_location_id=' || x_plan_stops(l_index).stop_location_id;
9443        l_string := l_string || ', stop_sequence=' || x_plan_stops(l_index).stop_sequence_number;
9444        l_string := l_string || ', planned_arr=' || FND_DATE.DATE_TO_CANONICAL(x_plan_stops(l_index).planned_arrival_date);
9445        l_string := l_string || ', planned_dep=' || FND_DATE.DATE_TO_CANONICAL(x_plan_stops(l_index).planned_departure_date);
9446        l_string := l_string || ', shipments_type_flag=' || x_plan_stops(l_index).shipments_type_flag;
9447        l_string := l_string || ', int_do_count=' || x_plan_stops(l_index).internal_do_count;
9448        l_string := l_string || ', ext_pd_count=' || x_plan_stops(l_index).external_pd_count;
9449        l_string := l_string || ', wsh_phys_loc=' || x_plan_stops(l_index).wsh_physical_location_id;
9450        --
9451        WSH_DEBUG_SV.log(l_module_name, 'istop(' || l_index || ')', l_string);
9452        l_index := x_plan_stops.NEXT(l_index);
9453       END LOOP;
9454     END IF;
9455 
9456     -- dump x_plan_trips
9457     WSH_DEBUG_SV.log(l_module_name, 'x_plan_trips.COUNT', x_plan_trips.COUNT);
9458     IF x_plan_trips.COUNT > 0  THEN
9459       l_index := x_plan_trips.FIRST;
9460       WHILE l_index IS NOT NULL LOOP
9461        --
9462        l_string := 'trip_interface_id=' || x_plan_trips(l_index).trip_interface_id;
9463        l_string := l_string || ', trip=' || x_plan_trips(l_index).trip_id;
9464        l_string := l_string || ', tp_trip=' || x_plan_trips(l_index).tp_trip_number;
9465        l_string := l_string || ', planned=' || x_plan_trips(l_index).planned_flag;
9466        l_string := l_string || ', wsh_planned=' || x_plan_trips(l_index).wsh_planned_flag;
9467        l_string := l_string || ', stop_base_index=' || x_plan_trips(l_index).stop_base_index;
9468        l_string := l_string || ', shipments_type_flag=' || x_plan_trips(l_index).shipments_type_flag;
9469        l_string := l_string || ', lane_id=' || x_plan_trips(l_index).lane_id;
9470        l_string := l_string || ', linked_stop_count=' || x_plan_trips(l_index).linked_stop_count;
9471        --
9472        WSH_DEBUG_SV.log(l_module_name, 'itrip(' || l_index || ')', l_string);
9473        l_index := x_plan_trips.NEXT(l_index);
9474       END LOOP;
9475     END IF;
9476 
9477     -- dump x_plan_trip_moves
9478     WSH_DEBUG_SV.log(l_module_name, 'x_plan_trip_moves.COUNT', x_plan_trip_moves.COUNT);
9479     IF x_plan_trip_moves.COUNT > 0  THEN
9480       l_index := x_plan_trip_moves.FIRST;
9481       WHILE l_index IS NOT NULL LOOP
9482        --
9483        l_string := 'trip_move_interface_id=' || x_plan_trip_moves(l_index).trip_move_interface_id;
9484        l_string := l_string || ', trip_move_id=' || x_plan_trip_moves(l_index).trip_move_id;
9485        l_string := l_string || ', move_interface=' || x_plan_trip_moves(l_index).move_interface_id;
9486        l_string := l_string || ', move_index=' || x_plan_trip_moves(l_index).move_index;
9487        l_string := l_string || ', trip_interface=' || x_plan_trip_moves(l_index).trip_interface_id;
9488        l_string := l_string || ', trip_index=' || x_plan_trip_moves(l_index).trip_index;
9489        l_string := l_string || ', sequence_number=' || x_plan_trip_moves(l_index).sequence_number;
9490        --
9491        WSH_DEBUG_SV.log(l_module_name, 'itripmove(' || l_index || ')', l_string);
9492        l_index := x_plan_trip_moves.NEXT(l_index);
9493       END LOOP;
9494     END IF;
9495 
9496     -- dump x_plan_moves
9497     WSH_DEBUG_SV.log(l_module_name, 'x_plan_moves.COUNT', x_plan_moves.COUNT);
9498     IF x_plan_moves.COUNT > 0  THEN
9499       l_index := x_plan_moves.FIRST;
9500       WHILE l_index IS NOT NULL LOOP
9501        --
9502        l_string := 'move_interface_id=' || x_plan_moves(l_index).move_interface_id;
9503        l_string := l_string || ', move_id=' || x_plan_moves(l_index).move_id;
9504        l_string := l_string || ', move_type_code=' || x_plan_moves(l_index).move_type_code;
9505        l_string := l_string || ', lane_id=' || x_plan_moves(l_index).lane_id;
9506        l_string := l_string || ', service_level=' || x_plan_moves(l_index).service_level;
9507        l_string := l_string || ', planned=' || x_plan_moves(l_index).planned_flag;
9508        l_string := l_string || ', fte_planned=' || x_plan_moves(l_index).fte_planned_flag;
9509        l_string := l_string || ', cm_trip_number=' || x_plan_moves(l_index).cm_trip_number;
9510        l_string := l_string || ', trip_move_base_index=' || x_plan_moves(l_index).trip_move_base_index;
9511        --
9512        WSH_DEBUG_SV.log(l_module_name, 'imove(' || l_index || ')', l_string);
9513        l_index := x_plan_moves.NEXT(l_index);
9514       END LOOP;
9515     END IF;
9516 
9517     -- dump x_used_details
9518     WSH_DEBUG_SV.log(l_module_name, 'x_used_details.COUNT', x_used_details.COUNT);
9519     IF x_used_details.COUNT > 0  THEN
9520       l_index := x_used_details.FIRST;
9521       WHILE l_index IS NOT NULL LOOP
9522        l_string := '';
9523        --
9524        l_string := 'delivery_detail_id=' || x_used_details(l_index).delivery_detail_id;
9525        l_string := l_string || ', dd_interface_id=' || x_used_details(l_index).dd_interface_id;
9526        l_string := l_string || ', available_qty=' || x_used_details(l_index).available_quantity || ' ' || x_used_details(l_index).available_quantity_uom;
9527        l_string := l_string || ', current_del=' || x_used_details(l_index).current_delivery_id;
9528        l_string := l_string || ', split_count=' || x_used_details(l_index).split_count;
9529        --
9530        WSH_DEBUG_SV.log(l_module_name, 'used_details(' || l_index || ')', l_string);
9531        l_index := x_used_details.NEXT(l_index);
9532       END LOOP;
9533     END IF;
9534 
9535     -- dump x_delivery_unassigns
9536     WSH_DEBUG_SV.log(l_module_name, 'x_delivery_unassigns.COUNT', x_delivery_unassigns.COUNT);
9537     IF x_delivery_unassigns.COUNT > 0  THEN
9538       l_index := x_delivery_unassigns.FIRST;
9539       WHILE l_index IS NOT NULL LOOP
9540        l_string := '';
9541        --
9542        l_string := 'delivery_detail_id=' || x_delivery_unassigns(l_index).delivery_detail_id;
9543        l_string := l_string || ', delivery_id=' || x_delivery_unassigns(l_index).delivery_id;
9544        l_string := l_string || ', wms_org_flag=' || x_delivery_unassigns(l_index).wms_org_flag;
9545        l_string := l_string || ', source=' || x_delivery_unassigns(l_index).source_code;
9546        l_string := l_string || ', RS=' || x_delivery_unassigns(l_index).released_status;
9547        l_string := l_string || ', lpn_id=' || x_delivery_unassigns(l_index).lpn_id;
9548        l_string := l_string || ', plan_dd_index=' || x_delivery_unassigns(l_index).plan_dd_index;
9549        l_string := l_string || ', plan_del_index=' || x_delivery_unassigns(l_index).plan_del_index;
9550        --
9551        WSH_DEBUG_SV.log(l_module_name, 'del_unassigns(' || l_index || ')', l_string);
9552        l_index := x_delivery_unassigns.NEXT(l_index);
9553       END LOOP;
9554     END IF;
9555 
9556     -- dump x_trip_unassigns
9557     WSH_DEBUG_SV.log(l_module_name, 'x_trip_unassigns.COUNT', x_trip_unassigns.COUNT);
9558     IF x_trip_unassigns.COUNT > 0  THEN
9559       l_index := x_trip_unassigns.FIRST;
9560       WHILE l_index IS NOT NULL LOOP
9561        --
9562        l_string := 'delivery_id=' || x_trip_unassigns(l_index).delivery_id;
9563        l_string := l_string || ', trip_id=' || x_trip_unassigns(l_index).trip_id;
9564        l_string := l_string || ', trip_index=' || x_trip_unassigns(l_index).trip_index;
9565        l_string := l_string || ', delivery_leg_id=' || x_trip_unassigns(l_index).delivery_leg_id;
9566        l_string := l_string || ', pickup_stop_id=' || x_trip_unassigns(l_index).pickup_stop_id;
9567        l_string := l_string || ', dropoff_stop_id=' || x_trip_unassigns(l_index).dropoff_stop_id;
9568        --
9569        WSH_DEBUG_SV.log(l_module_name, 'trip_unassigns(' || l_index || ')', l_string);
9570        l_index := x_trip_unassigns.NEXT(l_index);
9571       END LOOP;
9572     END IF;
9573 
9574     -- dump x_obsoleted_stops
9575     WSH_DEBUG_SV.log(l_module_name, 'x_obsoleted_stops.COUNT', x_obsoleted_stops.COUNT);
9576     IF x_obsoleted_stops.COUNT > 0  THEN
9577       l_index := x_obsoleted_stops.FIRST;
9578       WHILE l_index IS NOT NULL LOOP
9579        --
9580        l_string := 'trip_id=' || x_obsoleted_stops(l_index).trip_id;
9581        l_string := l_string || ', stop_id=' || x_obsoleted_stops(l_index).stop_id;
9582        --
9583        WSH_DEBUG_SV.log(l_module_name, 'obs_stops(' || l_index || ')', l_string);
9584        l_index := x_obsoleted_stops.NEXT(l_index);
9585       END LOOP;
9586     END IF;
9587 
9588     -- dump x_obsoleted_trip_moves
9589     WSH_DEBUG_SV.log(l_module_name, 'x_obsoleted_trip_moves.COUNT', x_obsoleted_trip_moves.COUNT);
9590     IF x_obsoleted_trip_moves.COUNT > 0  THEN
9591       l_index := x_obsoleted_trip_moves.FIRST;
9592       WHILE l_index IS NOT NULL LOOP
9593        --
9594        l_string := 'trip_move_id=' || x_obsoleted_trip_moves(l_index).trip_move_id;
9595        l_string := l_string || ', move_id=' || x_obsoleted_trip_moves(l_index).move_id;
9596        l_string := l_string || ', trip_id=' || x_obsoleted_trip_moves(l_index).trip_id;
9597        l_string := l_string || ', sequence_number=' || x_obsoleted_trip_moves(l_index).sequence_number;
9598        --
9599        WSH_DEBUG_SV.log(l_module_name, 'obs_t_moves(' || l_index || ')', l_string);
9600        l_index := x_obsoleted_trip_moves.NEXT(l_index);
9601       END LOOP;
9602     END IF;
9603 
9604     WSH_DEBUG_SV.logmsg(l_module_name, '------- end of dump -----------');
9605 
9606     WSH_DEBUG_SV.pop(l_module_name);
9607   END IF;
9608 
9609 EXCEPTION
9610   WHEN OTHERS THEN
9611     WSH_UTIL_CORE.DEFAULT_HANDLER(
9612                       'WSH_TP_RELEASE.debug_dump_context',
9613                       l_module_name);
9614 
9615     IF l_debug_on THEN
9616          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
9617        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
9618     END IF;
9619     RETURN;
9620 END debug_dump_context;
9621 
9622 
9623 
9624 --
9625 --  Procedure:          copy_delivery_record
9626 --  Parameters:
9627 --               p_plan_delivery_rec   plan delivery record
9628 --               x_delivery_attrs_rec  output delivery attributes record
9629 --               x_return_status       standard return status
9630 --  Description:
9631 --               transforms plan data structure into group api data structure for delivery.
9632 --
9633 PROCEDURE copy_delivery_record(
9634            p_plan_delivery_rec   IN            plan_delivery_rec_type,
9635            x_delivery_attrs_rec     OUT NOCOPY wsh_new_deliveries_pvt.delivery_rec_type,
9636            x_return_status          OUT NOCOPY VARCHAR2
9637           ) IS
9638   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'COPY_DELIVERY_RECORD';
9639   --
9640   l_debug_on BOOLEAN;
9641   --
9642   CURSOR c_del_info(x_delivery_id NUMBER) IS
9643      SELECT name,
9644             gross_weight,
9645             net_weight,
9646             weight_uom_code,
9647             volume,
9648             volume_uom_code,
9649             customer_id,
9650             intmed_ship_to_location_id,
9651             fob_code,
9652             freight_terms_code,
9653             ship_method_code,
9654             carrier_id,
9655             service_level,
9656             mode_of_transport,
9657             vendor_id,
9658             party_id,
9659             shipping_control,
9660             shipment_direction
9661      FROM   wsh_new_deliveries
9662      WHERE  delivery_id = x_delivery_id;
9663 
9664   l_group_flags wsh_delivery_autocreate.group_by_flags_rec_type;
9665   l_return_status VARCHAR2(1);
9666   l_del_info c_del_info%ROWTYPE;
9667 BEGIN
9668   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
9669        --
9670   IF l_debug_on IS NULL THEN
9671      l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
9672   END IF;
9673 
9674   IF l_debug_on THEN
9675      WSH_DEBUG_SV.push(l_module_name);
9676   END IF;
9677 
9678   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
9679 
9680   x_delivery_attrs_rec.DELIVERY_ID                  := p_plan_delivery_rec.delivery_id;
9681 
9682   IF     p_plan_delivery_rec.delivery_id IS NOT NULL THEN
9683      -- look up delivery info if it exists and key values are missing.
9684     OPEN   c_del_info(p_plan_delivery_rec.delivery_id);
9685     FETCH  c_del_info INTO l_del_info;
9686     IF c_del_info%NOTFOUND THEN
9687       IF l_debug_on THEN
9688         WSH_DEBUG_SV.log(l_module_name, 'delivery_id is not found', p_plan_delivery_rec.delivery_id);
9689       END IF;
9690       -- setting this value to NULL will cause an error.
9691       x_delivery_attrs_rec.DELIVERY_ID := NULL;
9692       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
9693     END IF;
9694     CLOSE  c_del_info;
9695     IF x_delivery_attrs_rec.DELIVERY_ID IS NULL THEN
9696       IF l_debug_on THEN
9697         WSH_DEBUG_SV.pop(l_module_name);
9698       END IF;
9699       RETURN;
9700     END IF;
9701   END IF;
9702 
9703 
9704   -- if delivery is outbound, determine which enforced attributes
9705   -- we need to set when creating or update the delivery.
9706   --  At this point, this should succeed because we have already matched groups.
9707   IF NVL(p_plan_delivery_rec.shipment_direction, 'O') IN ('O', 'IO') THEN
9708     WSH_DELIVERY_AUTOCREATE.get_group_by_attr (
9709                 p_organization_id => p_plan_delivery_rec.organization_id,
9710                 x_group_by_flags  => l_group_flags,
9711                 x_return_status   => l_return_status);
9712 
9713     IF l_return_status IN (WSH_UTIL_CORE.G_RET_STS_ERROR,
9714                          WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR)  THEN
9715        x_return_status := l_return_status;
9716        IF l_debug_on THEN
9717          WSH_DEBUG_SV.log(l_module_name,
9718                           'create/update del will fail because grouping attrs could not be found for org',
9719                           p_plan_delivery_rec.organization_id);
9720          WSH_DEBUG_SV.pop(l_module_name);
9721        END IF;
9722        RETURN;
9723     END IF;
9724   ELSE
9725     -- inbound or drop
9726     IF p_plan_delivery_rec.shipment_direction = 'D' THEN
9727       l_group_flags.customer := 'Y';
9728     ELSE
9729       l_group_flags.customer := 'N';
9730     END IF;
9731     l_group_flags.intmed        := 'N';
9732     l_group_flags.fob           := 'N';
9733     l_group_flags.freight_terms := 'N';
9734     l_group_flags.ship_method   := 'N';
9735     -- carrier is not used (it is part of ship method)
9736   END IF;
9737 
9738 
9739   x_delivery_attrs_rec.NAME                         := NVL(p_plan_delivery_rec.name, l_del_info.name);
9740   x_delivery_attrs_rec.LOADING_SEQUENCE             := p_plan_delivery_rec.loading_sequence;
9741   x_delivery_attrs_rec.LOADING_ORDER_FLAG           := p_plan_delivery_rec.loading_order_flag;
9742   x_delivery_attrs_rec.INITIAL_PICKUP_DATE          := p_plan_delivery_rec.initial_pickup_date;
9743   x_delivery_attrs_rec.INITIAL_PICKUP_LOCATION_ID   := p_plan_delivery_rec.initial_pickup_location_id;
9744   x_delivery_attrs_rec.ORGANIZATION_ID              := p_plan_delivery_rec.organization_id;
9745   x_delivery_attrs_rec.ULTIMATE_DROPOFF_LOCATION_ID := p_plan_delivery_rec.ultimate_dropoff_location_id;
9746   x_delivery_attrs_rec.ULTIMATE_DROPOFF_DATE        := p_plan_delivery_rec.ultimate_dropoff_date;
9747   x_delivery_attrs_rec.POOLED_SHIP_TO_LOCATION_ID   := p_plan_delivery_rec.pooled_ship_to_location_id;
9748   x_delivery_attrs_rec.FOB_LOCATION_ID              := p_plan_delivery_rec.fob_location_id;
9749   x_delivery_attrs_rec.WAYBILL                      := p_plan_delivery_rec.waybill;
9750   x_delivery_attrs_rec.dock_code                    := p_plan_delivery_rec.dock_code;
9751   x_delivery_attrs_rec.TP_DELIVERY_NUMBER           := p_plan_delivery_rec.tp_delivery_number;
9752   x_delivery_attrs_rec.TP_PLAN_NAME                 := p_plan_delivery_rec.tp_plan_name;
9753   x_delivery_attrs_rec.IGNORE_FOR_PLANNING          := 'N';
9754 
9755 
9756   x_delivery_attrs_rec.WEIGHT_UOM_CODE              := p_plan_delivery_rec.weight_uom_code;
9757   x_delivery_attrs_rec.VOLUME_UOM_CODE              := p_plan_delivery_rec.volume_uom_code;
9758 
9759   IF p_plan_delivery_rec.delivery_id IS NOT NULL THEN
9760     -- W/V: existing delivery has W/V; convert W/V to the plan UOMs
9761     x_delivery_attrs_rec.GROSS_WEIGHT := WSH_WV_UTILS.Convert_Uom(
9762                                            from_uom => l_del_info.weight_uom_code,
9763                                            to_uom   => p_plan_delivery_rec.weight_uom_code,
9764                                            quantity => l_del_info.gross_weight);
9765 
9766     x_delivery_attrs_rec.NET_WEIGHT   := WSH_WV_UTILS.Convert_Uom(
9767                                            from_uom => l_del_info.weight_uom_code,
9768                                            to_uom   => p_plan_delivery_rec.weight_uom_code,
9769                                            quantity => l_del_info.net_weight);
9770     x_delivery_attrs_rec.VOLUME       := WSH_WV_UTILS.Convert_Uom(
9771                                            from_uom => l_del_info.volume_uom_code,
9772                                            to_uom   => p_plan_delivery_rec.volume_uom_code,
9773                                            quantity => l_del_info.volume);
9774 
9775     -- See if UOM conversion is successful. If not, revert back to the old UOM.
9776     IF (NVL(x_delivery_attrs_rec.GROSS_WEIGHT, 0))  = 0 and (NVL(l_del_info.gross_weight, 0) <> 0) THEN
9777 
9778        x_delivery_attrs_rec.GROSS_WEIGHT := l_del_info.gross_weight;
9779        x_delivery_attrs_rec.NET_WEIGHT := l_del_info.net_weight;
9780        x_delivery_attrs_rec.WEIGHT_UOM_CODE := l_del_info.weight_uom_code;
9781 
9782     END IF;
9783     IF (NVL(x_delivery_attrs_rec.VOLUME, 0))  = 0 and (NVL(l_del_info.volume, 0) <> 0) THEN
9784 
9785        x_delivery_attrs_rec.VOLUME := l_del_info.volume;
9786        x_delivery_attrs_rec.VOLUME_UOM_CODE := l_del_info.volume_uom_code;
9787 
9788     END IF;
9789 
9790 
9791 
9792     IF l_debug_on THEN
9793       WSH_DEBUG_SV.log(l_module_name, 'l_del_info.GROSS_WEIGHT', l_del_info.GROSS_WEIGHT);
9794       WSH_DEBUG_SV.log(l_module_name, 'l_del_info.NET_WEIGHT', l_del_info.NET_WEIGHT);
9795       WSH_DEBUG_SV.log(l_module_name, 'l_del_info.VOLUME', l_del_info.VOLUME);
9796       WSH_DEBUG_SV.log(l_module_name, 'x_delivery_attrs_rec.GROSS_WEIGHT', x_delivery_attrs_rec.GROSS_WEIGHT);
9797       WSH_DEBUG_SV.log(l_module_name, 'x_delivery_attrs_rec.NET_WEIGHT', x_delivery_attrs_rec.NET_WEIGHT);
9798       WSH_DEBUG_SV.log(l_module_name, 'x_delivery_attrs_rec.VOLUME', x_delivery_attrs_rec.VOLUME);
9799     END IF;
9800   ELSE
9801     -- W/V: new delivery is empty; therefore W/V = 0.
9802     x_delivery_attrs_rec.GROSS_WEIGHT := 0;
9803     x_delivery_attrs_rec.NET_WEIGHT   := 0;
9804     x_delivery_attrs_rec.VOLUME       := 0;
9805   END IF;
9806   x_delivery_attrs_rec.ADDITIONAL_SHIPMENT_INFO     := p_plan_delivery_rec.additional_shipment_info;
9807   x_delivery_attrs_rec.CURRENCY_CODE                := p_plan_delivery_rec.currency_code;
9808   x_delivery_attrs_rec.NUMBER_OF_LPN	            := NULL;
9809 
9810 
9811   -- populate grouping attributes:
9812   --    Update grouping attributes from lines if enforced;
9813   --    otherwise, use values from the existing delivery or NULL.
9814 
9815   IF l_group_flags.customer = 'Y' THEN
9816     x_delivery_attrs_rec.CUSTOMER_ID                  := p_plan_delivery_rec.customer_id;
9817   ELSE
9818    -- TP gives us customer_id, so we may want to update the delivery with that value if not NULL.
9819     x_delivery_attrs_rec.CUSTOMER_ID                  := NVL(p_plan_delivery_rec.customer_id, l_del_info.customer_id);
9820   END IF;
9821 
9822   IF l_group_flags.intmed = 'Y' THEN
9823     x_delivery_attrs_rec.INTMED_SHIP_TO_LOCATION_ID   := p_plan_delivery_rec.intmed_ship_to_location_id;
9824   ELSE
9825     x_delivery_attrs_rec.INTMED_SHIP_TO_LOCATION_ID   := l_del_info.intmed_ship_to_location_id;
9826   END IF;
9827 
9828   IF l_group_flags.fob = 'Y' THEN
9829     x_delivery_attrs_rec.FOB_CODE                     := p_plan_delivery_rec.fob_code;
9830   ELSE
9831     x_delivery_attrs_rec.FOB_CODE                     := l_del_info.fob_code;
9832   END IF;
9833 
9834   IF l_group_flags.freight_terms = 'Y' THEN
9835     x_delivery_attrs_rec.FREIGHT_TERMS_CODE           := p_plan_delivery_rec.freight_terms_code;
9836   ELSE
9837     x_delivery_attrs_rec.FREIGHT_TERMS_CODE           := l_del_info.freight_terms_code;
9838   END IF;
9839 
9840   IF l_group_flags.ship_method = 'Y' THEN
9841     x_delivery_attrs_rec.SHIP_METHOD_CODE             := NULL; -- to be derived from its components
9842     x_delivery_attrs_rec.CARRIER_ID                   := p_plan_delivery_rec.carrier_id;
9843     x_delivery_attrs_rec.service_level                := p_plan_delivery_rec.service_level;
9844     x_delivery_attrs_rec.mode_of_transport            := p_plan_delivery_rec.mode_of_transport;
9845   ELSE
9846     x_delivery_attrs_rec.SHIP_METHOD_CODE             := l_del_info.ship_method_code;
9847     x_delivery_attrs_rec.CARRIER_ID                   := l_del_info.carrier_id;
9848     x_delivery_attrs_rec.service_level                := l_del_info.service_level;
9849     x_delivery_attrs_rec.mode_of_transport            := l_del_info.mode_of_transport;
9850   END IF;
9851 
9852   -- inbound grouping attributes are mandatory (customer is covered for Drop):
9853   IF p_plan_delivery_rec.shipment_direction IN ('I', 'D')  THEN
9854     x_delivery_attrs_rec.party_id                     := p_plan_delivery_rec.party_id;
9855     x_delivery_attrs_rec.shipping_control             := p_plan_delivery_rec.shipping_control;
9856     x_delivery_attrs_rec.vendor_id                    := p_plan_delivery_rec.vendor_id;
9857   ELSE
9858     x_delivery_attrs_rec.party_id                     := l_del_info.party_id;
9859     x_delivery_attrs_rec.shipping_control             := l_del_info.shipping_control;
9860     x_delivery_attrs_rec.vendor_id                    := l_del_info.vendor_id;
9861   END IF;
9862 
9863   IF l_debug_on THEN
9864      WSH_DEBUG_SV.pop(l_module_name);
9865   END IF;
9866 
9867 
9868 EXCEPTION
9869   WHEN OTHERS THEN
9870     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
9871     IF c_del_info%ISOPEN THEN
9872       CLOSE c_del_info;
9873     END IF;
9874     WSH_UTIL_CORE.DEFAULT_HANDLER(
9875                       'WSH_TP_RELEASE.copy_delivery_record',
9876                       l_module_name);
9877 
9878     IF l_debug_on THEN
9879          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
9880        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
9881     END IF;
9882 
9883 END copy_delivery_record;
9884 
9885 
9886 
9887 --
9888 --  Procedure:          copy_trip_record
9889 --  Parameters:
9890 --               p_plan_trip_rec   plan trip record
9891 --               x_trip_attrs_rec  output trip attributes record
9892 --  Description:
9893 --               transforms plan data structure into group api data structure for trip.
9894 --
9895 PROCEDURE copy_trip_record(
9896            p_plan_trip_rec   IN            plan_trip_rec_type,
9897            x_trip_attrs_rec     OUT NOCOPY wsh_trips_pvt.trip_rec_type
9898           ) IS
9899   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'COPY_TRIP_RECORD';
9900   --
9901   l_debug_on BOOLEAN;
9902   --
9903   CURSOR c_trip_info(x_trip_id NUMBER) IS
9904      SELECT name,
9905             operator,
9906             load_tender_status,
9907             load_tender_number
9908      FROM   wsh_trips
9909      WHERE  trip_id = x_trip_id;
9910 
9911   l_trip_info c_trip_info%ROWTYPE;
9912 BEGIN
9913   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
9914        --
9915   IF l_debug_on IS NULL THEN
9916      l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
9917   END IF;
9918 
9919   IF l_debug_on THEN
9920      WSH_DEBUG_SV.push(l_module_name);
9921   END IF;
9922 
9923   x_trip_attrs_rec.TRIP_ID                      := p_plan_trip_rec.trip_id;
9924 
9925   IF     p_plan_trip_rec.trip_id IS NOT NULL
9926      AND p_plan_trip_rec.name IS NULL THEN
9927      -- look up trip info if it exists and key values are missing.
9928     OPEN   c_trip_info(p_plan_trip_rec.trip_id);
9929     FETCH  c_trip_info INTO l_trip_info;
9930     IF c_trip_info%NOTFOUND THEN
9931       IF l_debug_on THEN
9932         WSH_DEBUG_SV.log(l_module_name, 'trip_id is not found', p_plan_trip_rec.trip_id);
9933       END IF;
9934       -- setting this value to NULL will cause an error.
9935       x_trip_attrs_rec.TRIP_ID := NULL;
9936     END IF;
9937     CLOSE  c_trip_info;
9938     IF x_trip_attrs_rec.TRIP_ID IS NULL THEN
9939       IF l_debug_on THEN
9940         WSH_DEBUG_SV.pop(l_module_name);
9941       END IF;
9942       RETURN;
9943     END IF;
9944   END IF;
9945 
9946   x_trip_attrs_rec.NAME                         := NVL(p_plan_trip_rec.name, l_trip_info.name);
9947   x_trip_attrs_rec.VEHICLE_ITEM_ID              := p_plan_trip_rec.vehicle_item_id;
9948   x_trip_attrs_rec.VEHICLE_ORGANIZATION_ID      := p_plan_trip_rec.vehicle_organization_id;
9949   x_trip_attrs_rec.VEHICLE_NUM_PREFIX           := p_plan_trip_rec.vehicle_num_prefix;
9950   x_trip_attrs_rec.VEHICLE_NUMBER               := p_plan_trip_rec.vehicle_number;
9951   x_trip_attrs_rec.CARRIER_ID                   := p_plan_trip_rec.carrier_id;
9952   x_trip_attrs_rec.SHIP_METHOD_CODE             := p_plan_trip_rec.ship_method_code;
9953   x_trip_attrs_rec.ROUTE_ID                     := p_plan_trip_rec.route_id;
9954   x_trip_attrs_rec.ROUTING_INSTRUCTIONS         := p_plan_trip_rec.routing_instructions;
9955   x_trip_attrs_rec.SERVICE_LEVEL                := p_plan_trip_rec.service_level;
9956   x_trip_attrs_rec.MODE_OF_TRANSPORT            := p_plan_trip_rec.mode_of_transport;
9957   x_trip_attrs_rec.FREIGHT_TERMS_CODE           := p_plan_trip_rec.freight_terms_code;
9958   x_trip_attrs_rec.SEAL_CODE                    := p_plan_trip_rec.seal_code;
9959   x_trip_attrs_rec.TP_PLAN_NAME                 := p_plan_trip_rec.tp_plan_name;
9960   x_trip_attrs_rec.TP_TRIP_NUMBER               := p_plan_trip_rec.tp_trip_number;
9961   x_trip_attrs_rec.SHIPMENTS_TYPE_FLAG          := p_plan_trip_rec.shipments_type_flag;
9962   x_trip_attrs_rec.CONSOLIDATION_ALLOWED        := p_plan_trip_rec.consolidation_allowed;
9963   x_trip_attrs_rec.SCHEDULE_ID                  := p_plan_trip_rec.schedule_id;
9964   x_trip_attrs_rec.ROUTE_LANE_ID                := p_plan_trip_rec.route_lane_id;
9965   x_trip_attrs_rec.LANE_ID                      := p_plan_trip_rec.lane_id;
9966   x_trip_attrs_rec.BOOKING_NUMBER               := p_plan_trip_rec.booking_number;
9967   x_trip_attrs_rec.VESSEL                       := p_plan_trip_rec.vessel;
9968   x_trip_attrs_rec.VOYAGE_NUMBER                := p_plan_trip_rec.voyage_number;
9969   x_trip_attrs_rec.PORT_OF_LOADING              := p_plan_trip_rec.port_of_loading;
9970   x_trip_attrs_rec.PORT_OF_DISCHARGE            := p_plan_trip_rec.port_of_discharge;
9971   x_trip_attrs_rec.CARRIER_CONTACT_ID           := p_plan_trip_rec.carrier_contact_id;
9972   x_trip_attrs_rec.SHIPPER_WAIT_TIME            := p_plan_trip_rec.shipper_wait_time;
9973   x_trip_attrs_rec.WAIT_TIME_UOM                := p_plan_trip_rec.wait_time_uom;
9974   x_trip_attrs_rec.CARRIER_RESPONSE             := p_plan_trip_rec.carrier_response;
9975   x_trip_attrs_rec.OPERATOR                     := NVL(p_plan_trip_rec.operator, l_trip_info.operator);
9976   x_trip_attrs_rec.LOAD_TENDER_STATUS           := l_trip_info.load_tender_status;
9977   x_trip_attrs_rec.LOAD_TENDER_NUMBER           := l_trip_info.load_tender_number;  -- J+ auto tender
9978   x_trip_attrs_rec.IGNORE_FOR_PLANNING          := 'N';
9979 
9980   IF l_debug_on THEN
9981      WSH_DEBUG_SV.pop(l_module_name);
9982   END IF;
9983 
9984 EXCEPTION
9985   WHEN OTHERS THEN
9986     IF c_trip_info%ISOPEN THEN
9987       CLOSE c_trip_info;
9988     END IF;
9989     WSH_UTIL_CORE.DEFAULT_HANDLER(
9990                       'WSH_TP_RELEASE.copy_trip_record',
9991                       l_module_name);
9992 
9993     IF l_debug_on THEN
9994          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
9995        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
9996     END IF;
9997     RAISE;
9998 
9999 END copy_trip_record;
10000 
10001 
10002 
10003 --
10004 --  Procedure:          copy_stop_record
10005 --  Parameters:
10006 --               p_plan_stop_rec   plan stop record
10007 --               p_plan_trips      list of plan trips to look up trip_id
10008 --               x_stop_attrs_rec  output stop attributes record
10009 --  Description:
10010 --               transforms plan data structure into group api data structure for trip stop.
10011 --
10012 
10013 PROCEDURE copy_stop_record(
10014            p_plan_stop_rec   IN            plan_stop_rec_type,
10015            p_plan_trips      IN            plan_trip_tab_type,
10016            x_stop_attrs_rec     OUT NOCOPY wsh_trip_stops_pvt.trip_stop_rec_type
10017           ) IS
10018   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'COPY_STOP_RECORD';
10019   --
10020   l_debug_on BOOLEAN;
10021   --
10022   CURSOR c_stop_info(x_stop_id NUMBER) IS
10023      SELECT departure_gross_weight,
10024             departure_net_weight,
10025             weight_uom_code,
10026             departure_volume,
10027             volume_uom_code,
10028             departure_fill_percent
10029      FROM   wsh_trip_stops
10030      WHERE  stop_id = x_stop_id;
10031 
10032   l_stop_info c_stop_info%ROWTYPE;
10033 
10034 BEGIN
10035   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
10036        --
10037   IF l_debug_on IS NULL THEN
10038      l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
10039   END IF;
10040 
10041   IF l_debug_on THEN
10042      WSH_DEBUG_SV.push(l_module_name);
10043      WSH_DEBUG_SV.log(l_module_name, 'p_plan_stop_rec.trip_index', p_plan_stop_rec.trip_index);
10044   END IF;
10045 
10046   IF p_plan_stop_rec.stop_id IS NOT NULL THEN
10047     OPEN  c_stop_info(p_plan_stop_rec.stop_id);
10048     FETCH c_stop_info INTO l_stop_info;
10049     IF c_stop_info%NOTFOUND THEN
10050       IF l_debug_on THEN
10051         WSH_DEBUG_SV.log(l_module_name, 'stop_id is not found', p_plan_stop_rec.stop_id);
10052       END IF;
10053       -- setting this value to NULL will cause an error.
10054       x_stop_attrs_rec.STOP_ID := NULL;
10055     END IF;
10056     CLOSE c_stop_info;
10057   END IF;
10058 
10059   x_stop_attrs_rec.STOP_ID                  := p_plan_stop_rec.stop_id;
10060   x_stop_attrs_rec.TP_STOP_ID               := p_plan_stop_rec.tp_stop_id;
10061   x_stop_attrs_rec.TRIP_ID                  := p_plan_trips(p_plan_stop_rec.trip_index).trip_id;
10062   x_stop_attrs_rec.STOP_LOCATION_ID         := p_plan_stop_rec.stop_location_id;
10063   x_stop_attrs_rec.STOP_SEQUENCE_NUMBER     := p_plan_stop_rec.stop_sequence_number;
10064   x_stop_attrs_rec.PLANNED_ARRIVAL_DATE     := p_plan_stop_rec.planned_arrival_date;
10065   x_stop_attrs_rec.PLANNED_DEPARTURE_DATE   := p_plan_stop_rec.planned_departure_date;
10066   x_stop_attrs_rec.WEIGHT_UOM_CODE          := p_plan_stop_rec.weight_uom_code;
10067   x_stop_attrs_rec.VOLUME_UOM_CODE          := p_plan_stop_rec.volume_uom_code;
10068   IF p_plan_stop_rec.stop_id IS NOT NULL THEN
10069     -- W/V: existing stop has W/V; convert W/V to the plan UOMs
10070     x_stop_attrs_rec.DEPARTURE_GROSS_WEIGHT := WSH_WV_UTILS.Convert_Uom(
10071                                            from_uom => l_stop_info.weight_uom_code,
10072                                            to_uom   => p_plan_stop_rec.weight_uom_code,
10073                                            quantity => l_stop_info.departure_gross_weight);
10074     x_stop_attrs_rec.DEPARTURE_NET_WEIGHT := WSH_WV_UTILS.Convert_Uom(
10075                                            from_uom => l_stop_info.weight_uom_code,
10076                                            to_uom   => p_plan_stop_rec.weight_uom_code,
10077                                            quantity => l_stop_info.departure_net_weight);
10078     x_stop_attrs_rec.DEPARTURE_VOLUME := WSH_WV_UTILS.Convert_Uom(
10079                                            from_uom => l_stop_info.volume_uom_code,
10080                                            to_uom   => p_plan_stop_rec.volume_uom_code,
10081                                            quantity => l_stop_info.departure_volume);
10082     -- See if UOM conversion is successful. If not, revert back to the old UOM.
10083     IF (NVL(x_stop_attrs_rec.DEPARTURE_GROSS_WEIGHT, 0))  = 0 and (NVL(l_stop_info.departure_gross_weight, 0) <> 0) THEN
10084 
10085        x_stop_attrs_rec.DEPARTURE_GROSS_WEIGHT := l_stop_info.departure_gross_weight;
10086        x_stop_attrs_rec.DEPARTURE_NET_WEIGHT := l_stop_info.departure_net_weight;
10087        x_stop_attrs_rec.WEIGHT_UOM_CODE := l_stop_info.weight_uom_code;
10088 
10089     END IF;
10090     IF (NVL(x_stop_attrs_rec.DEPARTURE_VOLUME, 0))  = 0 and (NVL(l_stop_info.departure_volume, 0) <> 0) THEN
10091 
10092        x_stop_attrs_rec.DEPARTURE_VOLUME := l_stop_info.departure_volume;
10093        x_stop_attrs_rec.VOLUME_UOM_CODE := l_stop_info.volume_uom_code;
10094 
10095     END IF;
10096     IF l_debug_on THEN
10097       WSH_DEBUG_SV.log(l_module_name, 'p_plan_stop_rec.weight_uom_code', p_plan_stop_rec.weight_uom_code);
10098       WSH_DEBUG_SV.log(l_module_name, 'p_plan_stop_rec.volume_uom_code', p_plan_stop_rec.volume_uom_code);
10099       WSH_DEBUG_SV.log(l_module_name, 'x_stop_attrs_rec.DEPARTURE_GROSS_WEIGHT', x_stop_attrs_rec.DEPARTURE_GROSS_WEIGHT);
10100       WSH_DEBUG_SV.log(l_module_name, 'x_stop_attrs_rec.DEPARTURE_NET_WEIGHT', x_stop_attrs_rec.DEPARTURE_NET_WEIGHT);
10101       WSH_DEBUG_SV.log(l_module_name, 'x_stop_attrs_rec.DEPARTURE_VOLUME', x_stop_attrs_rec.DEPARTURE_VOLUME);
10102     END IF;
10103 
10104   ELSE
10105     -- new stop is empty; assume W/V = 0.
10106     --!!! W/V: issue: if we create a stop that is in middle of trip, it may need calculating.
10107     x_stop_attrs_rec.DEPARTURE_GROSS_WEIGHT := 0;
10108     x_stop_attrs_rec.DEPARTURE_NET_WEIGHT   := 0;
10109     x_stop_attrs_rec.DEPARTURE_VOLUME       := 0;
10110   END IF;
10111   x_stop_attrs_rec.DEPARTURE_SEAL_CODE      := p_plan_stop_rec.departure_seal_code;
10112   x_stop_attrs_rec.DEPARTURE_FILL_PERCENT   := NVL(l_stop_info.departure_fill_percent,
10113                                                    p_plan_stop_rec.departure_fill_percent);
10114   x_stop_attrs_rec.WKEND_LAYOVER_STOPS      := p_plan_stop_rec.wkend_layover_stops;
10115   x_stop_attrs_rec.WKDAY_LAYOVER_STOPS      := p_plan_stop_rec.wkday_layover_stops;
10116   x_stop_attrs_rec.SHIPMENTS_TYPE_FLAG      := p_plan_stop_rec.shipments_type_flag;
10117 
10118   IF l_debug_on THEN
10119      WSH_DEBUG_SV.pop(l_module_name);
10120   END IF;
10121 
10122 
10123 EXCEPTION
10124   WHEN OTHERS THEN
10125     IF c_stop_info%ISOPEN THEN
10126       CLOSE c_stop_info;
10127     END IF;
10128     WSH_UTIL_CORE.DEFAULT_HANDLER(
10129                       'WSH_TP_RELEASE.copy_stop_record',
10130                       l_module_name);
10131 
10132     IF l_debug_on THEN
10133          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
10134        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
10135     END IF;
10136     RAISE;
10137 
10138 END copy_stop_record;
10139 
10140 
10141 --
10142 --  Procedure:          create_update_plan_trips
10143 --  Parameters:
10144 --               p_phase           value 1 = create or update with NULL lane_id
10145 --                                 value 2 = update with lane_id
10146 --               x_plan_trips      list of plan trips
10147 --               x_errors_tab      list of errors to insert into wsh_interface_errors at the end
10148 --               x_return_status   return status
10149 --  Description:
10150 --               in phase 1, create or update trips with NULL lane_id.
10151 --               in phase 2, update trips with lane_id.
10152 --               This API was created to resolve bug 3580374
10153 --
10154 
10155 PROCEDURE create_update_plan_trips(
10156            p_phase                    IN            NUMBER,  -- 1 to create/update trips with NULL lane_id
10157                                                              -- 2 to update trips with lane_id populated
10158            x_context                  IN OUT NOCOPY context_rec_type,
10159            x_plan_trips               IN OUT NOCOPY plan_trip_tab_type,
10160            x_errors_tab               IN OUT NOCOPY interface_errors_tab_type,
10161            x_return_status               OUT NOCOPY VARCHAR2
10162           )
10163 IS
10164   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'CREATE_UPDATE_PLAN_TRIPS';
10165   --
10166   l_debug_on BOOLEAN;
10167   --
10168   l_index            NUMBER;
10169   l_message_name       VARCHAR2(30);
10170   l_return_status      VARCHAR2(1);
10171   l_number_of_warnings NUMBER;
10172   l_number_of_errors   NUMBER;
10173   l_msg_data           VARCHAR2(32767);
10174   l_msg_count          NUMBER;
10175   l_interface_entity   WSH_INTERFACE_ERRORS.INTERFACE_TABLE_NAME%TYPE;
10176   l_interface_id       NUMBER;
10177   --
10178   l_trip_attrs       WSH_TRIPS_PVT.trip_attr_tbl_type;
10179   l_trip_action_prms WSH_TRIPS_GRP.action_parameters_rectype;
10180   l_trip_in_rec      WSH_TRIPS_GRP.TripInRecType;
10181   l_trip_defaults    WSH_TRIPS_GRP.default_parameters_rectype;
10182   l_trip_out_rec     WSH_TRIPS_GRP.tripActionOutRecType;
10183   l_trip_out_tab     WSH_TRIPS_GRP.trip_out_tab_type;
10184 
10185 BEGIN
10186   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
10187        --
10188   IF l_debug_on IS NULL THEN
10189      l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
10190   END IF;
10191 
10192   IF l_debug_on THEN
10193      WSH_DEBUG_SV.push(l_module_name);
10194      WSH_DEBUG_SV.log(l_module_name, 'p_phase', p_phase);
10195   END IF;
10196   --
10197   --
10198 
10199   l_trip_attrs.DELETE;
10200   l_trip_in_rec.caller      := 'WSH_TP_RELEASE';
10201 
10202   l_index := x_plan_trips.FIRST;
10203   WHILE l_index IS NOT NULL LOOP
10204     -- Bug 3555487 initialize message stack for each major action point.
10205     FND_MSG_PUB.initialize;
10206 
10207     IF l_debug_on THEN
10208       WSH_DEBUG_SV.log(l_module_name, 'create/update trips: l_index', l_index);
10209       IF (p_phase = 2) THEN
10210         WSH_DEBUG_SV.log(l_module_name, 'x_plan_trips(l_index).lane_id',  x_plan_trips(l_index).lane_id);
10211       END IF;
10212     END IF;
10213 
10214     copy_trip_record(
10215               p_plan_trip_rec  => x_plan_trips(l_index),
10216               x_trip_attrs_rec => l_trip_attrs(1)
10217     );
10218     IF (p_phase = 1) THEN
10219       -- bug 3580374: always create/update trip with NULL lane_id
10220       -- so that stops can be created/updated without location validation.
10221       l_trip_attrs(1).lane_id   := NULL;
10222     END IF;
10223 
10224     IF x_plan_trips(l_index).trip_id IS NULL THEN
10225       -- creation happens only in phase 1
10226       l_trip_in_rec.action_code := 'CREATE';
10227       l_message_name     := 'WSH_TP_F_CREATE_TRIP';
10228     ELSE
10229       l_trip_in_rec.action_code := 'UPDATE';
10230       IF (p_phase = 1) THEN
10231         l_message_name     := 'WSH_TP_F_UPDATE_TRIP';
10232       ELSE
10233         l_message_name     := 'WSH_TP_F_UPDATE_LANE';
10234       END IF;
10235     END IF;
10236 
10237     IF l_debug_on THEN
10238       WSH_DEBUG_SV.log(l_module_name, 'l_trip_attrs(1).lane_id', l_trip_attrs(1).lane_id );
10239     END IF;
10240 
10241     IF (p_phase = 1)
10242        OR (p_phase = 2 AND x_plan_trips(l_index).lane_id IS NOT NULL) THEN
10243       -- phase 1: always call this API
10244       -- phase 2: call this API only if lane_id needs to be populated.
10245 
10246       wsh_trips_grp.Create_Update_Trip(
10247           p_api_version_number     =>    1.0,
10248           p_init_msg_list          =>    FND_API.G_FALSE,
10249           p_commit                 =>    FND_API.G_FALSE,
10250           p_trip_info_tab          =>    l_trip_attrs,
10251           p_In_rec                 =>    l_trip_in_rec,
10252           x_Out_Tab                =>    l_trip_out_tab,
10253           x_return_status          =>    l_return_status,
10254           x_msg_count              =>    l_msg_count,
10255           x_msg_data               =>    l_msg_data);
10256 
10257       l_interface_entity := 'WSH_TRIPS_INTERFACE';
10258       l_interface_id     := x_plan_trips(l_index).trip_interface_id;
10259       wsh_util_core.api_post_call(
10260            p_return_status => l_return_status,
10261            x_num_warnings  => l_number_of_warnings,
10262            x_num_errors    => l_number_of_errors);
10263     END IF;
10264 
10265     IF l_trip_in_rec.action_code = 'CREATE' THEN
10266       x_plan_trips(l_index).trip_id := l_trip_out_tab(1).trip_id;
10267     END IF;
10268 
10269     l_index := x_plan_trips.NEXT(l_index);
10270 
10271   END LOOP;
10272 
10273   --
10274   --
10275   IF l_debug_on THEN
10276      WSH_DEBUG_SV.pop(l_module_name);
10277   END IF;
10278 
10279 
10280 EXCEPTION
10281     WHEN fnd_api.g_exc_error THEN
10282       x_return_status := WSH_UTIL_CORE.G_RET_STS_ERROR;
10283       fnd_msg_pub.count_and_get(
10284           p_count   => l_msg_count,
10285           p_data    => l_msg_data,
10286           p_encoded => fnd_api.g_false);
10287       --
10288       stamp_interface_error(
10289           p_group_id            => x_context.group_id,
10290           p_entity_table_name   => l_interface_entity,
10291           p_entity_interface_id => l_interface_id,
10292           p_message_name        => l_message_name,
10293           x_errors_tab          => x_errors_tab,
10294           x_return_status       => l_return_status);
10295       --
10296       IF l_debug_on THEN
10297          wsh_debug_sv.logmsg(l_module_name, 'FND_API.G_EXC_ERROR exception has occured.', wsh_debug_sv.c_excep_level);
10298          wsh_debug_sv.pop(l_module_name, 'EXCEPTION:FND_API.G_EXC_ERROR');
10299       END IF;
10300 
10301     WHEN fnd_api.g_exc_unexpected_error THEN
10302       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
10303       fnd_msg_pub.count_and_get(
10304          p_count    => l_msg_count,
10305          p_data     => l_msg_data,
10306          p_encoded  => fnd_api.g_false);
10307       --
10308       stamp_interface_error(
10309           p_group_id            => x_context.group_id,
10310           p_entity_table_name   => l_interface_entity,
10311           p_entity_interface_id => l_interface_id,
10312           p_message_name        => l_message_name,
10313           x_errors_tab          => x_errors_tab,
10314           x_return_status       => l_return_status);
10315       --
10316       IF l_debug_on THEN
10317          wsh_debug_sv.logmsg(l_module_name, 'FND_API.G_EXC_UNEXPECTED_ERROR exception has occured.', wsh_debug_sv.c_excep_level);
10318          wsh_debug_sv.pop(l_module_name, 'EXCEPTION:FND_API.G_EXC_UNEXPECTED_ERROR');
10319       END IF;
10320 
10321   WHEN OTHERS THEN
10322     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
10323     WSH_UTIL_CORE.DEFAULT_HANDLER(
10324                       'WSH_TP_RELEASE.create_update_plan_trips',
10325                       l_module_name);
10326 
10327     IF l_debug_on THEN
10328          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
10329        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
10330     END IF;
10331 
10332 END create_update_plan_trips;
10333 
10334 
10335 
10336 
10337 --
10338 --  Procedure:          stamp_interface_error
10339 --  Parameters:
10340 --               p_group_id            group identifier where the error is found
10341 --               p_entity_table_name   entity table where the error is found
10342 --               p_entity_interface_id record where the error is found
10343 --               p_message_name        message name identifying the error
10344 --               p_message_appl        message application name (NULL means 'WSH')
10345 --               p_message_text        optional text for output to the user-
10346 --               p_token_1_name        optional token 1 name
10347 --               p_token_1_value       optional token 1 value
10348 --               p_token_2_name        optional token 2 name
10349 --               p_token_2_value       optional token 2 value
10350 --               p_token_3_name        optional token 3 name
10351 --               p_token_3_value       optional token 3 value
10352 --               p_token_4_name        optional token 4 name
10353 --               p_token_4_value       optional token 4 value
10354 --               p_errors_tab          list of errors to insert into wsh_interface_errors at the end
10355 --               x_return_status       return status
10356 --
10357 --  Description:
10358 --               puts the error information into the list p_errors_tab
10359 --
10360 PROCEDURE stamp_interface_error(
10361             p_group_id           IN            NUMBER,
10362             p_entity_table_name  IN            VARCHAR2,
10363             p_entity_interface_id   IN         NUMBER,
10364             p_message_name       IN            VARCHAR2,
10365             p_message_appl       IN            VARCHAR2 DEFAULT NULL,
10366             p_message_text       IN            VARCHAR2 DEFAULT NULL,
10367             p_token_1_name       IN            VARCHAR2 DEFAULT NULL,
10368             p_token_1_value      IN            VARCHAR2 DEFAULT NULL,
10369             p_token_2_name       IN            VARCHAR2 DEFAULT NULL,
10370             p_token_2_value      IN            VARCHAR2 DEFAULT NULL,
10371             p_token_3_name       IN            VARCHAR2 DEFAULT NULL,
10372             p_token_3_value      IN            VARCHAR2 DEFAULT NULL,
10373             p_token_4_name       IN            VARCHAR2 DEFAULT NULL,
10374             p_token_4_value      IN            VARCHAR2 DEFAULT NULL,
10375             x_errors_tab         IN OUT NOCOPY interface_errors_tab_type,
10376             x_return_status         OUT NOCOPY VARCHAR2)
10377 IS
10378   l_index BINARY_INTEGER := NULL;
10379   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'STAMP_INTERFACE_ERROR';
10380   --
10381   l_debug_on BOOLEAN;
10382   --
10383   l_message_appl VARCHAR2(30) := p_message_appl;
10384   c NUMBER;
10385   l_buffer VARCHAR2(4000);
10386   l_index_out NUMBER;
10387 
10388 BEGIN
10389   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
10390        --
10391   IF l_debug_on IS NULL THEN
10392      l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
10393   END IF;
10394 
10395   IF l_debug_on THEN
10396      WSH_DEBUG_SV.push(l_module_name);
10397      WSH_DEBUG_SV.log(l_module_name,'p_group_id', p_group_id);
10398      WSH_DEBUG_SV.log(l_module_name,'p_entity_table_name', p_entity_table_name);
10399      WSH_DEBUG_SV.log(l_module_name,'p_entity_interface_id', p_entity_interface_id);
10400      WSH_DEBUG_SV.log(l_module_name,'p_message_appl', p_message_appl);
10401      WSH_DEBUG_SV.log(l_module_name,'p_message_name', p_message_name);
10402      WSH_DEBUG_SV.log(l_module_name,'p_message_text', p_message_text);
10403      WSH_DEBUG_SV.log(l_module_name,'p_token_1_name', p_token_1_name);
10404      WSH_DEBUG_SV.log(l_module_name,'p_token_1_value', p_token_1_value);
10405      WSH_DEBUG_SV.log(l_module_name,'p_token_2_name', p_token_2_name);
10406      WSH_DEBUG_SV.log(l_module_name,'p_token_2_value', p_token_2_value);
10407      WSH_DEBUG_SV.log(l_module_name,'p_token_3_name', p_token_3_name);
10408      WSH_DEBUG_SV.log(l_module_name,'p_token_3_value', p_token_3_value);
10409      WSH_DEBUG_SV.log(l_module_name,'p_token_4_name', p_token_4_name);
10410      WSH_DEBUG_SV.log(l_module_name,'p_token_4_value', p_token_4_value);
10411   END IF;
10412 
10413   l_index := x_errors_tab.COUNT + 1;
10414 
10415   IF p_message_text IS NULL THEN
10416 
10417      l_message_appl := NVL(l_message_appl, 'WSH');
10418 
10419      FND_MESSAGE.SET_NAME(l_message_appl, p_message_name);
10420      IF p_token_1_name IS NOT NULL THEN
10421         FND_MESSAGE.SET_TOKEN(p_token_1_name, p_token_1_value);
10422         IF p_token_2_name IS NOT NULL THEN
10423            FND_MESSAGE.SET_TOKEN(p_token_2_name, p_token_2_value);
10424            IF p_token_3_name IS NOT NULL THEN
10425              FND_MESSAGE.SET_TOKEN(p_token_3_name, p_token_3_value);
10426                IF  p_token_4_name IS NOT NULL THEN
10427                  FND_MESSAGE.SET_TOKEN(p_token_4_name, p_token_4_value);
10428                END IF;
10429            END IF;
10430         END IF;
10431      END IF;
10432 
10433      fnd_msg_pub.add;
10434 
10435   ELSE
10436 
10437      x_errors_tab(l_index).ERROR_MESSAGE            := p_message_text;
10438      x_errors_tab(l_index).INTERFACE_TABLE_NAME     := p_entity_table_name;
10439      x_errors_tab(l_index).INTERFACE_ID             := p_entity_interface_id;
10440      x_errors_tab(l_index).INTERFACE_ERROR_GROUP_ID := p_group_id;
10441      x_errors_tab(l_index).MESSAGE_NAME             := p_message_name;
10442 
10443      l_index := l_index +1;
10444 
10445   END IF;
10446 
10447 
10448   c := FND_MSG_PUB.count_msg;
10449   IF l_debug_on THEN
10450     WSH_DEBUG_SV.log(l_module_name,'COUNT--',c);
10451   END IF;
10452   FOR i in 1..c LOOP
10453       FND_MSG_PUB.get(p_encoded => FND_API.G_FALSE,
10454           p_msg_index => i,
10455           p_data => l_buffer,
10456           p_msg_index_out => l_index_out);
10457 
10458 
10459       x_errors_tab(l_index).ERROR_MESSAGE            := l_buffer;
10460       x_errors_tab(l_index).INTERFACE_TABLE_NAME     := p_entity_table_name;
10461       x_errors_tab(l_index).INTERFACE_ID             := p_entity_interface_id;
10462       x_errors_tab(l_index).INTERFACE_ERROR_GROUP_ID := p_group_id;
10463       IF (i = 1) AND (p_message_text IS NULL)  THEN
10464          x_errors_tab(l_index).MESSAGE_NAME          := p_message_name;
10465       ELSE
10466          x_errors_tab(l_index).MESSAGE_NAME          := 'WSH_TP_I_GENERIC';
10467       END IF;
10468 
10469       l_index := l_index + 1;
10470 
10471   END LOOP;
10472 
10473   -- bug 4552612: issue #3
10474   -- since all messages in stack are already pulled into x_errors_tab,
10475   -- the stack should be purged before the next call to stamp_interface_error
10476   -- to avoid repeating the same messages.
10477   FND_MSG_PUB.initialize;
10478 
10479 
10480   IF l_debug_on THEN
10481     WSH_DEBUG_SV.log(l_module_name,'l_index--',l_index);
10482   END IF;
10483 
10484   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
10485 
10486   IF l_debug_on THEN
10487      WSH_DEBUG_SV.pop(l_module_name);
10488   END IF;
10489 
10490 
10491 EXCEPTION
10492   WHEN OTHERS THEN
10493     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
10494     WSH_UTIL_CORE.DEFAULT_HANDLER(
10495                       'WSH_TP_RELEASE.stamp_interface_error',
10496                       l_module_name);
10497 
10498     IF l_debug_on THEN
10499          WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
10500        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
10501      END IF;
10502 
10503 END stamp_interface_error;
10504 
10505 
10506 
10507 
10508 
10509 --
10510 --  Procedure:          insert_interface_errors
10511 --  Parameters:
10512 --               p_errors_tab          list of errors to insert into wsh_interface_errors[
10513 --               x_return_status       return status
10514 --
10515 --  Description:
10516 --               does a bulk-insert of error records into wsh_interface_errors
10517 --
10518 PROCEDURE insert_interface_errors(
10519             p_errors_tab    IN         interface_errors_tab_type,
10520             x_return_status OUT NOCOPY VARCHAR2)
10521 IS
10522   TYPE text_tab_type IS TABLE OF WSH_INTERFACE_ERRORS.ERROR_MESSAGE%TYPE INDEX BY BINARY_INTEGER;
10523   --
10524   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'INSERT_INTERFACE_ERRORS';
10525   --
10526   l_debug_on BOOLEAN;
10527   --
10528   l_groups        WSH_UTIL_CORE.ID_TAB_TYPE;
10529   l_table_names   WSH_UTIL_CORE.COLUMN_TAB_TYPE;
10530   l_interface_ids WSH_UTIL_CORE.ID_TAB_TYPE;
10531   l_message_names WSH_UTIL_CORE.COLUMN_TAB_TYPE;
10532   l_messages      text_tab_type;
10533 
10534 BEGIN
10535 
10536 
10537   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
10538   --
10539   IF l_debug_on IS NULL THEN
10540     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
10541   END IF;
10542 
10543   IF l_debug_on THEN
10544     WSH_DEBUG_SV.push(l_module_name);
10545   END IF;
10546 
10547   SAVEPOINT before_insert;
10548 
10549   -- decompose the table of records into tables that can be used by FORALL.
10550   FOR i IN p_errors_tab.FIRST .. p_errors_tab.LAST LOOP
10551     l_groups(i)        := p_errors_tab(i).INTERFACE_ERROR_GROUP_ID;
10552     l_table_names(i)   := p_errors_tab(i).INTERFACE_TABLE_NAME;
10553     l_interface_ids(i) := p_errors_tab(i).INTERFACE_ID;
10554     l_message_names(i) := p_errors_tab(i).MESSAGE_NAME;
10555     l_messages(i)      := p_errors_tab(i).ERROR_MESSAGE;
10556   END LOOP;
10557 
10558   FORALL i IN l_groups.FIRST .. l_groups.LAST
10559          INSERT INTO WSH_INTERFACE_ERRORS (
10560           INTERFACE_ERROR_ID,
10561           INTERFACE_ERROR_GROUP_ID,
10562           INTERFACE_TABLE_NAME,
10563           INTERFACE_ID,
10564           INTERFACE_ACTION_CODE,
10565           MESSAGE_NAME,
10566           ERROR_MESSAGE,
10567           CREATION_DATE,
10568           CREATED_BY,
10569           LAST_UPDATE_DATE,
10570           LAST_UPDATED_BY)
10571           VALUES (
10572           WSH_INTERFACE_ERRORS_S.nextval,
10573           l_groups(i),
10574           l_table_names(i),
10575           l_interface_ids(i),
10576           G_TP_RELEASE_CODE,
10577           l_message_names(i),
10578           l_messages(i),
10579           SYSDATE,
10580           FND_GLOBAL.USER_ID,
10581           SYSDATE,
10582           FND_GLOBAL.USER_ID);
10583 
10584   x_return_status := WSH_UTIL_CORE.G_RET_STS_SUCCESS;
10585 
10586   IF l_debug_on THEN
10587      WSH_DEBUG_SV.pop(l_module_name);
10588   END IF;
10589 
10590 
10591 EXCEPTION
10592   WHEN OTHERS THEN
10593     x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
10594     WSH_UTIL_CORE.DEFAULT_HANDLER(
10595                       'WSH_TP_RELEASE.insert_interface_errors',
10596                       l_module_name);
10597     ROLLBACK TO before_insert;
10598 
10599     IF l_debug_on THEN
10600        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
10601        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
10602     END IF;
10603 
10604 END insert_interface_errors;
10605 
10606 
10607 --
10608 --  Function:          get_plan_trip_num
10609 --  Parameters:
10610 --               p_context             plan release context
10611 --               x_return_status       return status
10612 --
10613 --  Description:
10614 --               looks up a plan trip num for purpose
10615 --               of populating the error messages (to be used
10616 --               only if the trips have not yet been mapped).
10617 FUNCTION get_plan_trip_num(
10618              p_context      IN         context_rec_type)
10619 RETURN VARCHAR2 IS
10620   l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'GET_PLAN_TRIP_NUM';
10621   --
10622   l_debug_on BOOLEAN;
10623   --
10624   l_plan_trip_num WSH_TRIPS.TP_TRIP_NUMBER%TYPE := '';
10625 
10626   -- c_tp_interface_trips makes a list of interface trips,
10627   -- mapping shipping trips that were snapshot;
10628   -- code will later validate these shipping trips can be mapped.
10629   CURSOR c_name(p_group_id IN NUMBER) IS
10630   SELECT wti.tp_trip_number
10631   FROM   wsh_trips_interface          wti
10632   WHERE  wti.group_id = p_group_id
10633   AND    wti.interface_action_code  = G_TP_RELEASE_CODE
10634   AND    rownum = 1
10635   ;
10636 
10637 BEGIN
10638   l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
10639   --
10640   IF l_debug_on IS NULL THEN
10641     l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
10642   END IF;
10643 
10644   IF l_debug_on THEN
10645     WSH_DEBUG_SV.push(l_module_name);
10646     WSH_DEBUG_SV.log(l_module_name, 'p_context.group_id', p_context.group_id);
10647   END IF;
10648   --
10649   --
10650   OPEN c_name(p_context.group_id);
10651   FETCH c_name INTO l_plan_trip_num;
10652   IF c_name%NOTFOUND THEN
10653     l_plan_trip_num := '';
10654   END IF;
10655   CLOSE c_name;
10656 
10657   --
10658   --
10659   IF l_debug_on THEN
10660      WSH_DEBUG_SV.log(l_module_name, 'l_plan_trip_num', l_plan_trip_num);
10661      WSH_DEBUG_SV.pop(l_module_name);
10662   END IF;
10663 
10664   RETURN l_plan_trip_num;
10665 
10666 EXCEPTION
10667   WHEN OTHERS THEN
10668     WSH_UTIL_CORE.DEFAULT_HANDLER(
10669                       'WSH_TP_RELEASE.get_plan_trip_num',
10670                       l_module_name);
10671     IF l_debug_on THEN
10672        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
10673        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
10674     END IF;
10675     IF c_name%ISOPEN THEN
10676       CLOSE c_name;
10677     END IF;
10678     RETURN l_plan_trip_num;
10679 
10680 END get_plan_trip_num;
10681 
10682 --
10683 --  Procedure: Log_WV_Exceptions
10684 --  Parameters:
10685 --               p_details_loc_tab  table of locations of delivery details (indexed by delivery_detail_id).
10686 --               p_deliveries_loc_tab  table of locations of deliveries (indexed by delivery_id).
10687 --               p_stops_loc_tab  table of locations of stops (indexed by stop_id).
10688 --               x_return_status       return status
10689 --
10690 --  Description:
10691 --               Logs exceptions against lines, deliveries, and stops that
10692 --               have their Weights/Volume frozen, but may have their W/V changed.
10693 --               WV changes
10694 
10695 
10696 
10697 PROCEDURE Log_WV_Exceptions(
10698           p_details_loc_tab in wsh_util_core.id_tab_type,
10699           p_deliveries_loc_tab in wsh_util_core.id_tab_type,
10700           p_stops_loc_tab in wsh_util_core.id_tab_type,
10701           x_return_status out NOCOPY varchar2) IS
10702 
10703 i NUMBER;
10704 
10705 l_exception_msg_count NUMBER;
10706 l_exception_msg_data varchar2(2000);
10707 l_dummy_exception_id NUMBER;
10708 l_msg varchar2(2000);
10709 
10710 l_module_name CONSTANT VARCHAR2(100) := 'wsh.plsql.' || G_PKG_NAME || '.' || 'Log_WV_Exceptions';
10711 --
10712 l_debug_on BOOLEAN;
10713 
10714 
10715 BEGIN
10716 
10717 l_debug_on := WSH_DEBUG_INTERFACE.g_debug;
10718 --
10719 IF l_debug_on IS NULL THEN
10720   l_debug_on := WSH_DEBUG_SV.is_debug_enabled;
10721 END IF;
10722 
10723 IF l_debug_on THEN
10724   WSH_DEBUG_SV.push(l_module_name);
10725   WSH_DEBUG_SV.logmsg(l_module_name, 'line exceptions count: '|| p_details_loc_tab.count);
10726   WSH_DEBUG_SV.logmsg(l_module_name, 'delivery exceptions count: '|| p_deliveries_loc_tab.count);
10727   WSH_DEBUG_SV.logmsg(l_module_name, 'stop exceptions count: '|| p_stops_loc_tab.count);
10728 END IF;
10729 
10730 x_return_status :=  WSH_UTIL_CORE.G_RET_STS_SUCCESS;
10731 
10732 
10733 IF p_details_loc_tab.count > 0 THEN
10734 
10735    l_msg := FND_MESSAGE.Get_String('WSH', 'WSH_TP_DET_WV_FROZEN');
10736    i := p_details_loc_tab.FIRST;
10737 
10738    WHILE i is NOT NULL LOOP
10739 
10740      l_dummy_exception_id := NULL;
10741      wsh_xc_util.log_exception(
10742                     p_api_version             => 1.0,
10743                     x_return_status           => x_return_status,
10744                     x_msg_count               => l_exception_msg_count,
10745                     x_msg_data                => l_exception_msg_data,
10746                     x_exception_id            => l_dummy_exception_id ,
10747                     p_logged_at_location_id   => p_details_loc_tab(i),
10748                     p_exception_location_id   => p_details_loc_tab(i),
10749                     p_logging_entity          => 'SHIPPER',
10750                     p_logging_entity_id       => FND_GLOBAL.USER_ID,
10751                     p_exception_name          => 'WSH_TP_WV_FROZEN',
10752                     p_message                 => l_msg,
10753                     p_delivery_detail_id      => i
10754                      );
10755 
10756      i := p_details_loc_tab.next(i);
10757 
10758    END LOOP;
10759 
10760 END IF;
10761 
10762 IF p_deliveries_loc_tab.count > 0 THEN
10763 
10764    l_msg := FND_MESSAGE.Get_String('WSH', 'WSH_TP_DEL_WV_FROZEN');
10765    i := p_deliveries_loc_tab.FIRST;
10766 
10767    WHILE i is NOT NULL LOOP
10768 
10769      l_dummy_exception_id := NULL;
10770      wsh_xc_util.log_exception(
10771                     p_api_version             => 1.0,
10772                     x_return_status           => x_return_status,
10773                     x_msg_count               => l_exception_msg_count,
10774                     x_msg_data                => l_exception_msg_data,
10775                     x_exception_id            => l_dummy_exception_id ,
10776                     p_logged_at_location_id   => p_deliveries_loc_tab(i),
10777                     p_exception_location_id   => p_deliveries_loc_tab(i),
10778                     p_logging_entity          => 'SHIPPER',
10779                     p_logging_entity_id       => FND_GLOBAL.USER_ID,
10780                     p_exception_name          => 'WSH_TP_WV_FROZEN',
10781                     p_message                 => l_msg,
10782                     p_delivery_id             => i
10783                      );
10784 
10785      i := p_deliveries_loc_tab.next(i);
10786 
10787    END LOOP;
10788 
10789 END IF;
10790 
10791 IF p_stops_loc_tab.count > 0 THEN
10792 
10793    l_msg := FND_MESSAGE.Get_String('WSH', 'WSH_TP_STOP_WV_FROZEN');
10794    i := p_stops_loc_tab.FIRST;
10795 
10796    WHILE i is NOT NULL LOOP
10797 
10798      l_dummy_exception_id := NULL;
10799      wsh_xc_util.log_exception(
10800                     p_api_version             => 1.0,
10801                     x_return_status           => x_return_status,
10802                     x_msg_count               => l_exception_msg_count,
10803                     x_msg_data                => l_exception_msg_data,
10804                     x_exception_id            => l_dummy_exception_id ,
10805                     p_logged_at_location_id   => p_stops_loc_tab(i),
10806                     p_exception_location_id   => p_stops_loc_tab(i),
10807                     p_logging_entity          => 'SHIPPER',
10808                     p_logging_entity_id       => FND_GLOBAL.USER_ID,
10809                     p_exception_name          => 'WSH_TP_WV_FROZEN',
10810                     p_message                 => l_msg,
10811                     p_trip_stop_id            => i
10812                      );
10813 
10814      i := p_stops_loc_tab.next(i);
10815 
10816    END LOOP;
10817 
10818 END IF;
10819 
10820 IF l_debug_on THEN
10821   WSH_DEBUG_SV.pop(l_module_name);
10822 END IF;
10823 
10824 EXCEPTION
10825   WHEN OTHERS THEN
10826     x_return_status :=  WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
10827     WSH_UTIL_CORE.DEFAULT_HANDLER('WSH_TP_RELEASE.log_wv_exception', l_module_name);
10828     IF l_debug_on THEN
10829        WSH_DEBUG_SV.logmsg(l_module_name,'Unexpected error has occured. Oracle error message is '|| SQLERRM,WSH_DEBUG_SV.C_UNEXPEC_ERR_LEVEL);
10830        WSH_DEBUG_SV.pop(l_module_name,'EXCEPTION:OTHERS');
10831     END IF;
10832 
10833 END Log_WV_Exceptions;
10834 
10835 END WSH_TP_RELEASE_INT;