DBA Data[Home] [Help]

PACKAGE BODY: APPS.WMS_TASK_DISPATCH_PUT_AWAY

Source


1 PACKAGE BODY wms_task_dispatch_put_away AS
2   /* $Header: WMSTKPTB.pls 120.26.12010000.3 2008/09/22 06:30:44 pbonthu ship $ */
3 
4 
5   -- Global constant holding the package name
6   g_pkg_name CONSTANT VARCHAR2(30) := 'WMS_Task_Dispatch_put_away';
7 
8   /* FP-J Lot/Serial Support Enhancement
9    * Record type to store information for one serial number. This is used for
10    * inserting MTL_SERIAL_NUMBERS for the serial numbers in the LPN
11    */
12   TYPE msn_attribute_rec_tp IS RECORD(
13       serial_number             mtl_serial_numbers.serial_number%TYPE
14     , to_serial_number          mtl_serial_numbers.serial_number%TYPE
15     , vendor_serial_number      mtl_serial_numbers.vendor_serial_number%TYPE
16     , vendor_lot_number         mtl_serial_numbers.vendor_lot_number%TYPE
17     , parent_serial_number      mtl_serial_numbers.parent_serial_number%TYPE
18     , origination_date          mtl_serial_numbers.origination_date%TYPE
19     , end_item_unit_number      mtl_serial_numbers.end_item_unit_number%TYPE
20     , territory_code            mtl_serial_numbers.territory_code%TYPE
21     , time_since_new            mtl_serial_numbers.time_since_new%TYPE
22     , cycles_since_new          mtl_serial_numbers.cycles_since_new%TYPE
23     , time_since_overhaul       mtl_serial_numbers.time_since_overhaul%TYPE
24     , cycles_since_overhaul     mtl_serial_numbers.cycles_since_overhaul%TYPE
25     , time_since_repair         mtl_serial_numbers.time_since_repair%TYPE
26     , cycles_since_repair       mtl_serial_numbers.cycles_since_repair%TYPE
27     , time_since_visit          mtl_serial_numbers.time_since_visit%TYPE
28     , cycles_since_visit        mtl_serial_numbers.cycles_since_visit%TYPE
29     , time_since_mark           mtl_serial_numbers.time_since_mark%TYPE
30     , cycles_since_mark         mtl_serial_numbers.cycles_since_mark%TYPE
31     , number_of_repairs         mtl_serial_numbers.number_of_repairs%TYPE
32     , serial_attribute_category mtl_serial_numbers.serial_attribute_category%TYPE
33     , c_attribute1              mtl_serial_numbers.c_attribute1%TYPE
34     , c_attribute2              mtl_serial_numbers.c_attribute2%TYPE
35     , c_attribute3              mtl_serial_numbers.c_attribute3%TYPE
36     , c_attribute4              mtl_serial_numbers.c_attribute4%TYPE
37     , c_attribute5              mtl_serial_numbers.c_attribute5%TYPE
38     , c_attribute6              mtl_serial_numbers.c_attribute6%TYPE
39     , c_attribute7              mtl_serial_numbers.c_attribute7%TYPE
40     , c_attribute8              mtl_serial_numbers.c_attribute8%TYPE
41     , c_attribute9              mtl_serial_numbers.c_attribute9%TYPE
42     , c_attribute10             mtl_serial_numbers.c_attribute10%TYPE
43     , c_attribute11             mtl_serial_numbers.c_attribute11%TYPE
44     , c_attribute12             mtl_serial_numbers.c_attribute12%TYPE
45     , c_attribute13             mtl_serial_numbers.c_attribute13%TYPE
46     , c_attribute14             mtl_serial_numbers.c_attribute14%TYPE
47     , c_attribute15             mtl_serial_numbers.c_attribute15%TYPE
48     , c_attribute16             mtl_serial_numbers.c_attribute16%TYPE
49     , c_attribute17             mtl_serial_numbers.c_attribute17%TYPE
50     , c_attribute18             mtl_serial_numbers.c_attribute18%TYPE
51     , c_attribute19             mtl_serial_numbers.c_attribute19%TYPE
52     , c_attribute20             mtl_serial_numbers.c_attribute20%TYPE
53     , d_attribute1              mtl_serial_numbers.d_attribute1%TYPE
54     , d_attribute2              mtl_serial_numbers.d_attribute2%TYPE
55     , d_attribute3              mtl_serial_numbers.d_attribute3%TYPE
56     , d_attribute4              mtl_serial_numbers.d_attribute4%TYPE
57     , d_attribute5              mtl_serial_numbers.d_attribute5%TYPE
58     , d_attribute6              mtl_serial_numbers.d_attribute6%TYPE
59     , d_attribute7              mtl_serial_numbers.d_attribute7%TYPE
60     , d_attribute8              mtl_serial_numbers.d_attribute8%TYPE
61     , d_attribute9              mtl_serial_numbers.d_attribute9%TYPE
62     , d_attribute10             mtl_serial_numbers.d_attribute10%TYPE
63     , n_attribute1              mtl_serial_numbers.n_attribute1%TYPE
64     , n_attribute2              mtl_serial_numbers.n_attribute2%TYPE
65     , n_attribute3              mtl_serial_numbers.n_attribute3%TYPE
66     , n_attribute4              mtl_serial_numbers.n_attribute4%TYPE
67     , n_attribute5              mtl_serial_numbers.n_attribute5%TYPE
68     , n_attribute6              mtl_serial_numbers.n_attribute6%TYPE
69     , n_attribute7              mtl_serial_numbers.n_attribute7%TYPE
70     , n_attribute8              mtl_serial_numbers.n_attribute8%TYPE
71     , n_attribute9              mtl_serial_numbers.n_attribute9%TYPE
72     , n_attribute10             mtl_serial_numbers.n_attribute10%TYPE
73     );
74 
75 
76     -- Constants and types declaration
77 
78     TYPE num_tab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
79 
80     -- Task Record
81     SUBTYPE task_rec IS wms_dispatched_tasks%ROWTYPE;
82 
83     -- ATF Related constants
84     G_ATF_ACTIVATE_PLAN    CONSTANT NUMBER := 1;
85     G_ATF_ABORT_PLAN       CONSTANT NUMBER := 2;
86     G_ATF_CANCEL_PLAN      CONSTANT NUMBER := 3;
87 
88     G_OP_TYPE_DROP         CONSTANT NUMBER := wms_globals.G_OP_TYPE_DROP;
89     G_OP_ACTIVITY_INBOUND  CONSTANT NUMBER := wms_globals.G_OP_ACTIVITY_INBOUND;
90 
91     /**Constants defined for Inspection Flag in validate_operation
92     */
93     G_NO_INSPECTION      CONSTANT NUMBER:=1;
94     G_PARTIAL_INSPECTION CONSTANT NUMBER:=2;
95     G_FULL_INSPECTION    CONSTANT NUMBER:=3;
96 
97     /**Constants defined for Load Flag in validate_operation
98     */
99     G_NO_LOAD      CONSTANT NUMBER:=1;
100     G_PARTIAL_LOAD CONSTANT NUMBER:=2;
101     G_FULL_LOAD    CONSTANT NUMBER:=3;
102 
103 
104     /**Constants defined for Drop Flag in validate_operation
105     */
106     G_NO_DROP      CONSTANT NUMBER:=1;
107     G_PARTIAL_DROP CONSTANT NUMBER:=2;
108     G_FULL_DROP    CONSTANT NUMBER:=3;
109 
110 
111   PROCEDURE mydebug(msg IN VARCHAR2) IS
112     l_msg   VARCHAR2(5100);
113     l_ts    VARCHAR2(30);
114   BEGIN
115     --   select to_char(sysdate,'MM/DD/YYYY HH:MM:SS') INTO l_ts from dual;
116     --   l_msg:=l_ts||'  '||msg;
117 
118     l_msg := msg;
119     inv_mobile_helper_functions.tracelog(p_err_msg => l_msg
120         , p_module => 'WMS_Task_Dispatch_put_away'
121         , p_level => 4);
122     --dbms_output.put_line(l_msg);
123     NULL;
124   END;
125 
126   PROCEDURE create_mo_line(
127     p_org_id                      IN             NUMBER
128   , p_inventory_item_id           IN             NUMBER
129   , p_qty                         IN             NUMBER
130   , p_uom                         IN             VARCHAR2
131   , p_lpn                         IN             NUMBER
132   , p_project_id                  IN             NUMBER
133   , p_task_id                     IN             NUMBER
134   , p_reference                   IN             VARCHAR2
135   , p_reference_type_code         IN             NUMBER
136   , p_reference_id                IN             NUMBER
137   , p_header_id                   IN             NUMBER
138   , p_lot_number                  IN             VARCHAR2
139   , p_revision                    IN             VARCHAR2
140   , p_inspection_status           IN             NUMBER := NULL
141   , p_txn_source_id               IN             NUMBER := fnd_api.g_miss_num
142   , p_transaction_type_id         IN             NUMBER := fnd_api.g_miss_num
143   , p_transaction_source_type_id  IN             NUMBER := fnd_api.g_miss_num
144   , p_wms_process_flag            IN             NUMBER := NULL
145   , x_return_status               OUT NOCOPY     VARCHAR2
146   , x_msg_count                   OUT NOCOPY     NUMBER
147   , x_msg_data                    OUT NOCOPY     VARCHAR2
148   , p_from_cost_group_id          IN             NUMBER := NULL
149   , p_sec_qty                     IN             NUMBER := NULL   -- Added for OPM convergance
150   , p_sec_uom                     IN             VARCHAR2 := NULL -- Added for OPM convergance
151   , x_line_id                    OUT nocopy NUMBER    -- Added for R12 MOL Consolidation
152   ) IS
153     l_trolin_tbl         inv_move_order_pub.trolin_tbl_type;
154     l_return_status      VARCHAR2(1) := fnd_api.g_ret_sts_success;
155     l_msg_count          NUMBER;
156     l_msg_data           VARCHAR2(2400);
157     l_trohdr_val_rec     inv_move_order_pub.trolin_val_tbl_type;
158     l_commit             VARCHAR2(1)                            := fnd_api.g_false;
159     l_trolin_val_tbl     inv_move_order_pub.trolin_val_tbl_type;
160     l_line_num           NUMBER      := 0;
161     l_header_id          NUMBER      := fnd_api.g_miss_num;
162     l_order_count        NUMBER      := 1;
163     l_trohdr_val_rec     inv_move_order_pub.trohdr_val_rec_type;
164     l_commit             VARCHAR2(1) := fnd_api.g_false;
165     p_need_by_date       DATE        := SYSDATE;
166     p_src_subinv         VARCHAR2(30);
167     l_org_id             NUMBER;
168     l_inventory_item_id  NUMBER;
169     l_lpn                NUMBER;
170     l_qty                NUMBER;
171     l_uom                VARCHAR2(60);
172     l_project_id         NUMBER;
173     l_task_id            NUMBER;
174     l_ref                VARCHAR2(240);
175     l_ref_type           NUMBER;
176     l_ref_id             NUMBER;
177 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
178     l_lot_number         VARCHAR2(80);
179     l_revision           VARCHAR2(3);
180     l_txn_type_id        NUMBER      := fnd_api.g_miss_num;
181     l_txn_source_id      NUMBER      := fnd_api.g_miss_num;
182     l_txn_source_type_id NUMBER      := fnd_api.g_miss_num;
183     l_insp_status        NUMBER;
184     l_wms_process_flag   NUMBER;
185     l_cg_id              NUMBER;
186     l_tcg_id             NUMBER;
187     l_cg_line            NUMBER;
188     l_from_cg_id         NUMBER;
189     l1                   VARCHAR2(30);
190     l2                   NUMBER;
191     l3                   NUMBER;
192     l_debug              NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
193     l_sec_qty            NUMBER; -- Added for OPM convergance
194     l_sec_uom            VARCHAR2(3); -- Added for OPM convergance
195   BEGIN
196     --Get Max line number
197     SELECT MAX(line_number)
198     INTO   l_line_num
199     FROM   mtl_txn_request_lines
200     WHERE  header_id = p_header_id;
201 
202     l_header_id := p_header_id;
203     l_org_id := p_org_id;
204     l_inventory_item_id := p_inventory_item_id;
205     l_lpn := p_lpn;
206     l_qty := p_qty;
207     l_uom := p_uom;
208     l_project_id := p_project_id;
209     /*
210     ** Bug 2677818
211     ** l_task_id:=l_task_id;
212     ** Correcting this as below
213     */
214     l_task_id := p_task_id;
215     l_ref := p_reference;
216     l_ref_type := p_reference_type_code;
217     l_ref_id := p_reference_id;
218     l_lot_number := p_lot_number;
219     l_revision := p_revision;
220     l_insp_status := p_inspection_status;
221     l_wms_process_flag := p_wms_process_flag;
222     l_from_cg_id := p_from_cost_group_id;
223 
224     l_sec_qty := p_sec_qty; -- Added for OPM convergance
225     l_sec_uom := p_sec_uom; -- Added for OPM convergance
226 
227     -- Derive txn_source and type info
228 
229     /*Bug 5662957:set the txn_type_id = 12 and txn_source_type_id =13 if reference_type_code = 6.
230     The same was already done in create_mo() API as a part of bug fix 4996680.*/
231 
232     IF (l_ref_type = 4) THEN
233       l_txn_type_id := 18;
234       l_txn_source_type_id := 1;
235     ELSIF(l_ref_type = 8) THEN
236       l_txn_type_id := 61;
237       l_txn_source_type_id := 7;
238     ELSIF(l_ref_type = 7) THEN
239       l_txn_type_id := 15;
240       l_txn_source_type_id := 12;
241     ELSIF(l_ref_type = 6) THEN
242       l_txn_type_id         := 12;
243       l_txn_source_type_id  := 13;
244     ELSE
245       l_txn_type_id := 18;
246       l_txn_source_type_id := 1;
247     END IF;
248 
249     -- Note might need to add additional stuff here
250     -- Added for WIP
251     IF (p_txn_source_id <> fnd_api.g_miss_num) THEN
252       l_txn_source_id := p_txn_source_id;
253     ELSE
254       l_txn_source_id := NULL;
255     END IF;
256 
257     IF (p_transaction_source_type_id <> fnd_api.g_miss_num) THEN
258       l_txn_source_type_id := p_transaction_source_type_id;
259     END IF;
260 
261     IF (p_transaction_type_id <> fnd_api.g_miss_num) THEN
262       l_txn_type_id := p_transaction_type_id;
263     END IF;
264 
265     --Increment by 1
266     l_line_num := l_line_num + 1;
267     l_trolin_tbl(l_order_count).header_id := p_header_id;
268     l_trolin_tbl(l_order_count).created_by := fnd_global.user_id;
269     l_trolin_tbl(l_order_count).creation_date := SYSDATE;
270     l_trolin_tbl(l_order_count).date_required           := SYSDATE;
271     l_trolin_tbl(l_order_count).from_subinventory_code  := NULL;
272     l_trolin_tbl(l_order_count).inventory_item_id       := p_inventory_item_id;
273     l_trolin_tbl(l_order_count).last_updated_by         := fnd_global.user_id;
274     l_trolin_tbl(l_order_count).last_update_date        := SYSDATE;
275     l_trolin_tbl(l_order_count).last_update_login       := fnd_global.login_id;
276     l_trolin_tbl(l_order_count).line_id                 := fnd_api.g_miss_num;
277     l_trolin_tbl(l_order_count).line_number             := l_line_num;
278     l_trolin_tbl(l_order_count).line_status             := inv_globals.g_to_status_preapproved;
279     l_trolin_tbl(l_order_count).organization_id         := p_org_id;
280     l_trolin_tbl(l_order_count).quantity                := p_qty;
281     l_trolin_tbl(l_order_count).status_date             := SYSDATE;
282     l_trolin_tbl(l_order_count).to_subinventory_code    := NULL;
283     l_trolin_tbl(l_order_count).uom_code                := p_uom;
284     l_trolin_tbl(l_order_count).db_flag                 := fnd_api.g_true;
285     l_trolin_tbl(l_order_count).operation               := inv_globals.g_opr_create;
286     l_trolin_tbl(l_order_count).lpn_id                  := p_lpn;
287     l_trolin_tbl(l_order_count).REFERENCE               := l_ref;
288     l_trolin_tbl(l_order_count).reference_type_code     := l_ref_type;
289     l_trolin_tbl(l_order_count).reference_id            := l_ref_id;
290     l_trolin_tbl(l_order_count).project_id              := l_project_id;
291     l_trolin_tbl(l_order_count).task_id                 := l_task_id;
292     l_trolin_tbl(l_order_count).lot_number              := l_lot_number;
293     l_trolin_tbl(l_order_count).revision                := l_revision;
294     l_trolin_tbl(l_order_count).transaction_type_id     := l_txn_type_id;
295     l_trolin_tbl(l_order_count).transaction_source_type_id := l_txn_source_type_id;
296     l_trolin_tbl(l_order_count).inspection_status       := l_insp_status;
297     l_trolin_tbl(l_order_count).wms_process_flag        := l_wms_process_flag;
298     l_trolin_tbl(l_order_count).secondary_quantity      := l_sec_qty; -- Added for OPM convergance
299     l_trolin_tbl(l_order_count).secondary_uom           := l_sec_uom; -- Added for OPM convergance
300 
301     IF (p_txn_source_id <> fnd_api.g_miss_num) THEN
302       l_trolin_tbl(l_order_count).txn_source_id := l_txn_source_id;
303     END IF;
304 
305     l_trolin_tbl(l_order_count).from_cost_group_id  := l_from_cg_id;
306     l_trolin_tbl(l_order_count).to_cost_group_id    := l_from_cg_id;
307 
308     inv_move_order_pub.create_move_order_lines(
309       p_api_version_number     => 1.0
310     , p_init_msg_list          => 'F'
311     , p_commit                 => fnd_api.g_false
312     , x_return_status          => l_return_status
313     , x_msg_count              => l_msg_count
314     , x_msg_data               => l_msg_data
315     , p_trolin_tbl             => l_trolin_tbl
316     , p_trolin_val_tbl         => l_trolin_val_tbl
317     , x_trolin_tbl             => l_trolin_tbl
318     , x_trolin_val_tbl         => l_trolin_val_tbl
319     );
320 
321     IF (l_debug = 1) THEN
322       mydebug('create_mo_line: Org');
323       mydebug('create_mo_line: ' || l_trolin_tbl(1).organization_id);
324       mydebug('create_mo_line: Line');
325       mydebug('create_mo_line: ' || l_trolin_tbl(1).line_id);
326       mydebug('create_mo_line: Status ');
327       mydebug('create_mo_line: ' || l_return_status);
328     END IF;
329 
330     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
331       fnd_message.set_name('WMS', 'WMS_TD_MOL_ERROR');
332       fnd_msg_pub.ADD;
333       RAISE fnd_api.g_exc_unexpected_error;
334     ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
335       fnd_message.set_name('WMS', 'WMS_TD_MOL_ERROR');
336       fnd_msg_pub.ADD;
337       RAISE fnd_api.g_exc_error;
338     END IF;
339 
340     l_cg_line := l_trolin_tbl(1).line_id;
341     x_line_id := l_trolin_tbl(1).line_id;
342 
343     IF (l_debug = 1) THEN
344       mydebug('create_mo_line: Line: ' || l_cg_line);
345     END IF;
346 
347     SELECT from_subinventory_code
348          , from_cost_group_id
349          , to_cost_group_id
350     INTO   l1
351          , l2
352          , l3
353     FROM   mtl_txn_request_lines
354     WHERE  line_id = l_cg_line;
355 
356     IF (l_debug = 1) THEN
357       mydebug('create_mo_line: Act Sub: ' || l1);
358       mydebug('create_mo_line: Act FCG: ' || l2);
359       mydebug('create_mo_line: Act TCG: ' || l3);
360     END IF;
361 
362     IF l_from_cg_id IS NULL THEN
363       IF (l_debug = 1) THEN
364         mydebug('create_mo_line: Calling CG Engine');
365       END IF;
366 
367       -- Call Cost Group Engine
368       inv_cost_group_pub.assign_cost_group(
369         p_api_version_number         => 1.0
370       , p_init_msg_list              => fnd_api.g_false
371       , p_commit                     => fnd_api.g_false
372       , x_return_status              => l_return_status
373       , x_msg_count                  => l_msg_count
374       , x_msg_data                   => l_msg_data
375       , p_line_id                    => l_cg_line
376       , p_organization_id            => l_org_id
377       , p_input_type                 => inv_cost_group_pub.g_input_moline
378       , x_cost_group_id              => l_cg_id
379       , x_transfer_cost_group_id     => l_tcg_id
380       );
381 
382       IF (l_debug = 1) THEN
383         mydebug('create_mo_line: After Calling CG Engine');
384         mydebug('create_mo_line: CG: ' || l_cg_id);
385         mydebug('create_mo_line: TCG: ' || l_tcg_id);
386       END IF;
387 
388       fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
389 
390       IF (l_msg_count = 0) THEN
391         IF (l_debug = 1) THEN
392           mydebug('create_mo_line: Successful');
393         END IF;
394       ELSIF(l_msg_count = 1) THEN
395         IF (l_debug = 1) THEN
396           mydebug('create_mo_line: Not Successful');
397           mydebug(REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
398         END IF;
399       ELSE
400         IF (l_debug = 1) THEN
401           mydebug('create_mo_line: Not Successful2');
402         END IF;
403 
404         FOR i IN 1 .. l_msg_count LOOP
405           l_msg_data := fnd_msg_pub.get(i, 'F');
406 
407           IF (l_debug = 1) THEN
408             mydebug('create_mo_line: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
409           END IF;
410         END LOOP;
411       END IF;
412 
413       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
414         fnd_message.set_name('WMS', 'WMS_TD_CG_ERROR');
415         fnd_msg_pub.ADD;
416         RAISE fnd_api.g_exc_unexpected_error;
417       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
418         fnd_message.set_name('WMS', 'WMS_TD_CG_ERROR');
419         fnd_msg_pub.ADD;
420         RAISE fnd_api.g_exc_error;
421       END IF;
422     END IF;
423 
424     x_return_status := fnd_api.g_ret_sts_success;
425   EXCEPTION
426     WHEN fnd_api.g_exc_error THEN
427       x_return_status := fnd_api.g_ret_sts_error;
428       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
429     WHEN OTHERS THEN
430       x_return_status := fnd_api.g_ret_sts_unexp_error;
431       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
432   END create_mo_line;
433 
434   PROCEDURE create_mo(
435     p_org_id                      IN             NUMBER
436   , p_inventory_item_id           IN             NUMBER
437   , p_qty                         IN             NUMBER
438   , p_uom                         IN             VARCHAR2
439   , p_lpn                         IN             NUMBER
440   , p_project_id                  IN             NUMBER := NULL
441   , p_task_id                     IN             NUMBER := NULL
442   , p_reference                   IN             VARCHAR2 := NULL
443   , p_reference_type_code         IN             NUMBER := NULL
444   , p_reference_id                IN             NUMBER := NULL
445   , p_lot_number                  IN             VARCHAR2
446   , p_revision                    IN             VARCHAR2
447   , p_header_id                   IN OUT NOCOPY  NUMBER
448   , p_sub                         IN             VARCHAR := NULL
449   , p_loc                         IN             NUMBER  := NULL
450   , x_line_id                     OUT NOCOPY     NUMBER
451   , p_inspection_status           IN             NUMBER  := NULL
452   , p_txn_source_id               IN             NUMBER  := fnd_api.g_miss_num
453   , p_transaction_type_id         IN             NUMBER  := fnd_api.g_miss_num
454   , p_transaction_source_type_id  IN             NUMBER  := fnd_api.g_miss_num
455   , p_wms_process_flag            IN             NUMBER  := NULL
456   , x_return_status               OUT NOCOPY     VARCHAR2
457   , x_msg_count                   OUT NOCOPY     NUMBER
458   , x_msg_data                    OUT NOCOPY     VARCHAR2
459   , p_from_cost_group_id          IN             NUMBER  := NULL
460   , p_transfer_org_id             IN             NUMBER  DEFAULT NULL
461   , p_sec_qty                     IN             NUMBER := NULL -- Added for OPM Convergance
462   , p_sec_uom                     IN             VARCHAR2 := NULL -- Added for OPM Convergance
463   ) IS
464     l_trohdr_rec         inv_move_order_pub.trohdr_rec_type;
465     l_trolin_tbl         inv_move_order_pub.trolin_tbl_type;
466     l_trolin_val_tbl     inv_move_order_pub.trolin_val_tbl_type;
467     l_return_status      VARCHAR2(1)   := fnd_api.g_ret_sts_success;
468     l_msg_count          NUMBER;
469     l_msg_data           VARCHAR2(2000);
470     msg                  VARCHAR2(2000);
471     l_header_id          NUMBER        := fnd_api.g_miss_num;
472     l_line_num           NUMBER        := 0;
473     l_order_count        NUMBER        := 1;
474     l_trohdr_val_rec     inv_move_order_pub.trohdr_val_rec_type;
475     l_commit             VARCHAR2(1)   := fnd_api.g_false;
476     p_need_by_date       DATE          := SYSDATE;
477     p_src_subinv         VARCHAR2(30);
478     l_org_id             NUMBER;
479     l_inventory_item_id  NUMBER;
480     l_lpn                NUMBER;
481     l_qty                NUMBER;
482     l_uom                VARCHAR2(60);
483     l_project_id         NUMBER;
484     l_task_id            NUMBER;
485     l_ref                VARCHAR2(240);
486     l_ref_type           NUMBER;
487     l_ref_id             NUMBER;
488 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
489     l_lot_number         VARCHAR2(80);
490     l_revision           VARCHAR2(3);
491     l_txn_type_id        NUMBER        := fnd_api.g_miss_num;
492     l_txn_source_id      NUMBER        := fnd_api.g_miss_num;
493     l_txn_source_type_id NUMBER        := fnd_api.g_miss_num;
494     l_insp_status        NUMBER;
495     l_wms_process_flag   NUMBER;
496     l_cg_id              NUMBER;
497     l_tcg_id             NUMBER;
498     l_cg_line            NUMBER;
499     l_from_cg_id         NUMBER;
500     l1                   VARCHAR2(30);
501     l2                   NUMBER;
502     l3                   NUMBER;
503     l_project_comingle   NUMBER;
504     l_debug              NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
505     l_sec_qty            NUMBER;
506     l_sec_uom            VARCHAR2(3);
507   BEGIN
508     l_header_id := p_header_id;
509 
510     IF p_header_id = 0 THEN
511       l_header_id := fnd_api.g_miss_num;
512     END IF;
513 
514     l_org_id            := p_org_id;
515     l_inventory_item_id := p_inventory_item_id;
516     l_lpn               := p_lpn;
517     l_qty               := p_qty;
518     l_uom               := p_uom;
519     l_project_id        := p_project_id;
520     /*
521     ** l_task_id:=l_task_id;
522     ** Correcting this as below
523     */
524     l_task_id           := p_task_id;
525     l_ref               := p_reference;
526     l_ref_type          := p_reference_type_code;
527     l_ref_id            := p_reference_id;
528     l_lot_number        := p_lot_number;
529     l_revision          := p_revision;
530     l_insp_status       := p_inspection_status;
531     l_wms_process_flag  := p_wms_process_flag;
532     l_from_cg_id        := p_from_cost_group_id;
533     l_return_status     := fnd_api.g_ret_sts_success;
534 
535     l_sec_qty           := p_sec_qty; -- Added for OPM convergance
536     l_sec_uom           := p_sec_uom; -- Added for OPM convergance
537 
538     /*
539     ** Sense project/task comingling in LPN
540     */
541     IF ((p_lpn > 0) AND(p_org_id > 0)) THEN
542       BEGIN
543         SELECT 1
544         INTO   l_project_comingle
545         FROM   DUAL
546         WHERE  EXISTS(
547                  SELECT 1
548                  FROM   mtl_txn_request_lines
549                  WHERE  lpn_id = p_lpn
550                  AND    organization_id = p_org_id
551                  AND    line_status <> inv_globals.g_to_status_closed
552                  AND    NVL(project_id, -1) <> NVL(p_project_id, -1)
553                  AND    NVL(task_id, -1) <> NVL(p_task_id, -1));
554 
555         fnd_message.set_name('WMS', 'WMS_PROJ_LPN_COMINGLE');
556         fnd_msg_pub.ADD;
557         RAISE fnd_api.g_exc_error;
558       EXCEPTION
559         WHEN NO_DATA_FOUND THEN
560           NULL;
561       END;
562     END IF;
563 
564     -- Derive txn_source and type info
565     IF (l_ref_type = 4)   THEN
566       l_txn_type_id         := 18;
567       l_txn_source_type_id  := 1;
568     ELSIF(l_ref_type = 8) THEN
569       l_txn_type_id         := 61;
570       l_txn_source_type_id  := 7;
571     ELSIF(l_ref_type = 7) THEN
572       l_txn_type_id         := 15;
573       l_txn_source_type_id  := 12;
574     ELSIF(l_ref_type = 6) THEN
575          l_txn_type_id         := 12;
576          l_txn_source_type_id  := 13;
577     ELSE
578       l_txn_type_id         := 18;
579       l_txn_source_type_id  := 1;
580     END IF;
581 
582     -- Note might need to add additional stuff here
583     -- Added for WIP
584     IF (p_txn_source_id <> fnd_api.g_miss_num) THEN
585       l_txn_source_id := p_txn_source_id;
586     ELSE
587       l_txn_source_id := NULL;
588     END IF;
589 
590     IF (p_transaction_source_type_id <> fnd_api.g_miss_num) THEN
591       l_txn_source_type_id := p_transaction_source_type_id;
592     END IF;
593 
594     IF (p_transaction_type_id <> fnd_api.g_miss_num) THEN
595       l_txn_type_id := p_transaction_type_id;
596     END IF;
597 
598     l_trohdr_rec.created_by                                 := fnd_global.user_id;
599     l_trohdr_rec.creation_date                              := SYSDATE;
600     l_trohdr_rec.date_required                              := SYSDATE;
601     l_trohdr_rec.from_subinventory_code                     := p_sub;
602     l_trohdr_rec.header_status                              := inv_globals.g_to_status_preapproved;
603     l_trohdr_rec.last_updated_by                            := fnd_global.user_id;
604     l_trohdr_rec.last_update_date                           := SYSDATE;
605     l_trohdr_rec.last_update_login                          := fnd_global.user_id;
606     l_trohdr_rec.organization_id                            := l_org_id;
607     l_trohdr_rec.status_date                                := SYSDATE;
608     l_trohdr_rec.to_subinventory_code                       := NULL;
609     l_trohdr_rec.move_order_type                            := inv_globals.g_move_order_put_away;
610     l_trohdr_rec.db_flag                                    := fnd_api.g_true;
611     l_trohdr_rec.operation                                  := inv_globals.g_opr_create;
612     l_line_num                                              := l_line_num + 1;
613     l_trolin_tbl(l_order_count).header_id                   := l_trohdr_rec.header_id;
614     l_trolin_tbl(l_order_count).created_by                  := fnd_global.user_id;
615     l_trolin_tbl(l_order_count).creation_date               := SYSDATE;
616     l_trolin_tbl(l_order_count).date_required               := SYSDATE;
617     l_trolin_tbl(l_order_count).from_subinventory_code      := p_sub;
618     l_trolin_tbl(l_order_count).from_locator_id             := p_loc;
619     l_trolin_tbl(l_order_count).inventory_item_id           := l_inventory_item_id;
620     l_trolin_tbl(l_order_count).last_updated_by             := fnd_global.user_id;
621     l_trolin_tbl(l_order_count).last_update_date            := SYSDATE;
622     l_trolin_tbl(l_order_count).last_updated_by             := fnd_global.user_id;
623     l_trolin_tbl(l_order_count).last_update_date            := SYSDATE;
624     l_trolin_tbl(l_order_count).last_update_login           := fnd_global.login_id;
625     l_trolin_tbl(l_order_count).line_id                     := fnd_api.g_miss_num;
626     l_trolin_tbl(l_order_count).line_number                 := l_line_num;
627     l_trolin_tbl(l_order_count).line_status                 := inv_globals.g_to_status_preapproved;
628     l_trolin_tbl(l_order_count).organization_id             := l_org_id;
629     l_trolin_tbl(l_order_count).quantity                    := l_qty;
630     l_trolin_tbl(l_order_count).status_date                 := SYSDATE;
631     l_trolin_tbl(l_order_count).to_subinventory_code        := NULL;
632     l_trolin_tbl(l_order_count).uom_code                    := l_uom;
633     l_trolin_tbl(l_order_count).db_flag                     := fnd_api.g_true;
634     l_trolin_tbl(l_order_count).operation                   := inv_globals.g_opr_create;
635     l_trolin_tbl(l_order_count).lpn_id                      := p_lpn;
636     l_trolin_tbl(l_order_count).REFERENCE                   := l_ref;
637     l_trolin_tbl(l_order_count).reference_type_code         := l_ref_type;
638     l_trolin_tbl(l_order_count).reference_id                := l_ref_id;
639     l_trolin_tbl(l_order_count).project_id                  := l_project_id;
640     l_trolin_tbl(l_order_count).task_id                     := l_task_id;
641     l_trolin_tbl(l_order_count).lot_number                  := l_lot_number;
642     l_trolin_tbl(l_order_count).revision                    := l_revision;
643     l_trolin_tbl(l_order_count).transaction_type_id         := l_txn_type_id;
644     l_trolin_tbl(l_order_count).transaction_source_type_id  := l_txn_source_type_id;
645     l_trolin_tbl(l_order_count).inspection_status           := l_insp_status;
646     l_trolin_tbl(l_order_count).wms_process_flag            := l_wms_process_flag;
647     -- Added this to be populated for intransit shipments.
648     l_trolin_tbl(l_order_count).to_organization_id          := p_transfer_org_id;
649 
650     l_trolin_tbl(l_order_count).secondary_quantity          := l_sec_qty; -- Added for OPM convergance
651     l_trolin_tbl(l_order_count).secondary_uom               := l_sec_uom; -- Added for OPM convergance
652 
653     --  l_trolin_tbl(l_order_count).move_order_type:=inv_globals.g_move_order_put_away;
654     IF (p_txn_source_id <> fnd_api.g_miss_num) THEN
655       l_trolin_tbl(l_order_count).txn_source_id := l_txn_source_id;
656     END IF;
657 
658     l_trolin_tbl(l_order_count).from_cost_group_id := l_from_cg_id;
659     l_trolin_tbl(l_order_count).to_cost_group_id := l_from_cg_id;
660 
661     IF (l_debug = 1) THEN
662       mydebug('create_mo: Before Checking header');
663     END IF;
664 
665     IF (l_header_id IS NULL
666         OR l_header_id = fnd_api.g_miss_num) THEN
667       IF (l_debug = 1) THEN
668         mydebug('create_mo: Header Not passed. Calling Process_Move_Order');
669       END IF;
670 
671       inv_move_order_pub.process_move_order(
672         p_api_version_number     => 1.0
673       , p_init_msg_list          => 'F'
674       , p_commit                 => fnd_api.g_false
675       , x_return_status          => l_return_status
676       , x_msg_count              => l_msg_count
677       , x_msg_data               => l_msg_data
678       , p_trohdr_rec             => l_trohdr_rec
679       , p_trohdr_val_rec         => l_trohdr_val_rec
680       , p_trolin_tbl             => l_trolin_tbl
681       , p_trolin_val_tbl         => l_trolin_val_tbl
682       , x_trohdr_rec             => l_trohdr_rec
683       , x_trohdr_val_rec         => l_trohdr_val_rec
684       , x_trolin_tbl             => l_trolin_tbl
685       , x_trolin_val_tbl         => l_trolin_val_tbl
686       );
687       fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
688 
689       IF (l_msg_count = 0) THEN
690         IF (l_debug = 1) THEN
691           mydebug('create_mo: Successful');
692         END IF;
693       ELSIF(l_msg_count = 1) THEN
694         IF (l_debug = 1) THEN
695           mydebug('create_mo: Not Successful');
696           mydebug('create_mo: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
697         END IF;
698       ELSE
699         IF (l_debug = 1) THEN
700           mydebug('create_mo: Not Successful2');
701         END IF;
702 
703         FOR i IN 1 .. l_msg_count LOOP
704           l_msg_data := fnd_msg_pub.get(i, 'F');
705 
706           IF (l_debug = 1) THEN
707             mydebug('create_mo: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
708           END IF;
709         END LOOP;
710       END IF;
711 
712       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
713         fnd_message.set_name('WMS', 'WMS_TD_MO_ERROR');
714         fnd_msg_pub.ADD;
715         RAISE fnd_api.g_exc_unexpected_error;
716       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
717         fnd_message.set_name('WMS', 'WMS_TD_MO_ERROR');
718         fnd_msg_pub.ADD;
719         RAISE fnd_api.g_exc_error;
720       END IF;
721 
722       /* Get header and line ids */
723       p_header_id := l_trohdr_rec.header_id;
724       x_line_id := l_trolin_tbl(l_order_count).line_id;
725 
726       IF (l_debug = 1) THEN
727         mydebug('create_mo: Header' || p_header_id);
728         mydebug('create_mo: Org');
729         mydebug('create_mo: ' || l_trolin_tbl(1).organization_id);
730       END IF;
731 
732       l_cg_line := l_trolin_tbl(1).line_id;
733 
734       IF (l_debug = 1) THEN
735         mydebug('create_mo: Line: ' || l_cg_line);
736         mydebug('create_mo: From CG ' || l_trolin_tbl(1).from_cost_group_id);
737         mydebug('create_mo: To CG ' || l_trolin_tbl(1).to_cost_group_id);
738         mydebug('create_mo: From Sub ' || l_trolin_tbl(1).from_subinventory_code);
739       END IF;
740 
741       SELECT from_subinventory_code
742            , from_cost_group_id
743            , to_cost_group_id
744       INTO   l1
745            , l2
746            , l3
747       FROM   mtl_txn_request_lines
748       WHERE  line_id = l_cg_line;
749 
750       IF (l_debug = 1) THEN
751         mydebug('create_mo: Act Sub: ' || l1);
752         mydebug('create_mo: Act FCG: ' || l2);
753         mydebug('create_mo: Act TCG: ' || l3);
754       END IF;
755 
756       IF l_from_cg_id IS NULL THEN
757         IF (l_debug = 1) THEN
758           mydebug('create_mo: Calling CG Engine');
759         END IF;
760 
761         inv_cost_group_pub.assign_cost_group(
762           p_api_version_number         => 1.0
763         , p_init_msg_list              => fnd_api.g_false
764         , p_commit                     => fnd_api.g_false
765         , x_return_status              => l_return_status
766         , x_msg_count                  => l_msg_count
767         , x_msg_data                   => l_msg_data
768         , p_line_id                    => l_cg_line
769         , p_organization_id            => l_org_id
770         , p_input_type                 => inv_cost_group_pub.g_input_moline
771         , x_cost_group_id              => l_cg_id
772         , x_transfer_cost_group_id     => l_tcg_id
773         );
774 
775         IF (l_debug = 1) THEN
776           mydebug('create_mo: After Calling CG Engine');
777           mydebug('create_mo: CG: ' || l_cg_id);
778           mydebug('create_mo: TCG: ' || l_tcg_id);
779         END IF;
780 
781         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
782           IF (l_debug = 1) THEN
783             mydebug('create_mo: UnexpError In CG');
784           END IF;
785 
786           fnd_message.set_name('WMS', 'WMS_TD_CG_ERROR');
787           fnd_msg_pub.ADD;
788           RAISE fnd_api.g_exc_unexpected_error;
789         ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
790           IF (l_debug = 1) THEN
791             mydebug('create_mo: Error In CG');
792           END IF;
793 
794           fnd_message.set_name('WMS', 'WMS_TD_CG_ERROR');
795           fnd_msg_pub.ADD;
796           RAISE fnd_api.g_exc_error;
797         END IF;
798       END IF;
799     ELSE
800       IF (l_debug = 1) THEN
801         mydebug('create_mo: Header was passed. Calling create_mo_line');
802       END IF;
803 
804       wms_task_dispatch_put_away.create_mo_line(
805         p_org_id                         => l_org_id
806       , p_inventory_item_id              => l_inventory_item_id
807       , p_qty                            => l_qty
808       , p_uom                            => l_uom
809       , p_lpn                            => l_lpn
810       , p_project_id                     => l_project_id
811       , p_task_id                        => l_task_id
812       , p_reference                      => l_ref
813       , p_reference_type_code            => l_ref_type
814       , p_reference_id                   => l_ref_id
815       , p_header_id                      => l_header_id
816       , p_lot_number                     => l_lot_number
817       , p_revision                       => l_revision
818       , p_inspection_status              => l_insp_status
819       , p_txn_source_id                  => l_txn_source_id
820       , p_transaction_type_id            => l_txn_type_id
821       , p_transaction_source_type_id     => l_txn_source_type_id
822       , p_wms_process_flag               => l_wms_process_flag
823       , x_return_status                  => l_return_status
824       , x_msg_count                      => l_msg_count
825       , x_msg_data                       => l_msg_data
826       , p_from_cost_group_id             => l_from_cg_id
827       , p_sec_qty                        => l_sec_qty
828       , p_sec_uom                        => l_sec_uom
829       , x_line_id                        => x_line_id
830 	);
831 
832       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
833         fnd_message.set_name('WMS', 'WMS_TD_MO_ERROR');
834         fnd_msg_pub.ADD;
835         RAISE fnd_api.g_exc_unexpected_error;
836       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
837         fnd_message.set_name('WMS', 'WMS_TD_MO_ERROR');
838         fnd_msg_pub.ADD;
839         RAISE fnd_api.g_exc_error;
840       END IF;
841     END IF;
842 
843     x_return_status := fnd_api.g_ret_sts_success;
844   EXCEPTION
845     WHEN fnd_api.g_exc_error THEN
846       x_return_status := fnd_api.g_ret_sts_error;
847       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
848     WHEN OTHERS THEN
849       x_return_status := fnd_api.g_ret_sts_unexp_error;
850 
851       IF SQLCODE IS NOT NULL THEN
852         inv_mobile_helper_functions.sql_error('WMS_Task_Dispatch_put_away.create_mo', '10', SQLCODE);
853       END IF;
854 
855       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
856   END create_mo;
857 
858   -- added for bug 3401817
859   -- An autonomous_transaction wrapper arount wms_putaway_suggestions.cleanup_suggestions
860   -- This is because after cleanup_suggestions, we call create_suggestions,
861   -- which may very well suggest the same locator. In cleanup_suggestions, we call abort_operation_instance, which
862   -- in turn calls revert_loc_sugg_capacity_nauto. However, in create_suggestions, we call update_loc_suggested_capacity,
863   -- with autonomous_transaction, which will cause resource busy.
864 
865   /*
866 PROCEDURE cleanup_suggestions
867 ( p_org_id              IN          NUMBER
868 , p_lpn_id              IN          NUMBER
869 , x_return_status       OUT NOCOPY  VARCHAR2
870 , x_msg_count           OUT NOCOPY  NUMBER
871 , x_msg_data            OUT NOCOPY  VARCHAR2
872 , p_move_order_line_id  IN          NUMBER   DEFAULT NULL  -- added for ATF_J2
873 ) IS
874   PRAGMA autonomous_transaction;
875 BEGIN
876   wms_putaway_suggestions.cleanup_suggestions
877   ( p_lpn_id              => p_lpn_id
878   , p_org_id              => p_org_id
879   , x_return_status       => x_return_status
880   , x_msg_count           => x_msg_count
881   , x_msg_data            => x_msg_data
882   , p_move_order_line_id  => p_move_order_line_id
883   );
884   COMMIT;
885 END;
886 
887   */
888 
889   -- Create Putaway Suggestions
890   -- Bug# 2752119
891   -- Added an extra input parameter called p_check_for_crossdock
892   -- which will default to 'Y' = Yes.
893   -- This is needed when we are performing an Express Drop and need
894   -- to validate against the rules.  In that case, it is possible that
895   -- a crossdocking opportunity exists but the user chose to ignore it
896   -- and proceed with the express drop.  We should not call the
897   -- crossdocking API's at all in that case since it might split the
898   -- move order lines.
899   PROCEDURE suggestions_pub(
900     p_lpn_id               IN             NUMBER
901   , p_org_id               IN             NUMBER
902   , p_user_id              IN             NUMBER
903   , p_eqp_ins              IN             VARCHAR2
904   , x_number_of_rows       OUT NOCOPY     NUMBER
905   , x_return_status        OUT NOCOPY     VARCHAR2
906   , x_msg_count            OUT NOCOPY     NUMBER
907   , x_msg_data             OUT NOCOPY     VARCHAR2
908   , x_crossdock            OUT NOCOPY     VARCHAR2
909   , p_status               IN             NUMBER := 3
910   , p_check_for_crossdock  IN             VARCHAR2 := 'Y'
911   , p_move_order_line_id   IN             NUMBER DEFAULT NULL -- added for ATF_J
912   , p_commit               IN             VARCHAR2
913   , p_drop_type            IN             VARCHAR2
914   , p_subinventory         IN             VARCHAR2
915   , p_locator_id           IN             NUMBER
916   ) IS
917     l_api_version_number  CONSTANT NUMBER        := 1.0;
918     l_init_msg_list                VARCHAR2(255) := fnd_api.g_false;
919     l_api_name            CONSTANT VARCHAR2(30)  := 'Suggestions_PUB';
920     l_num_of_rows                  NUMBER        := 0;
921     l_detailed_qty                 NUMBER        := 0;
922     l_ser_index                    NUMBER;
923     x_success                      NUMBER;
924     l_revision                     VARCHAR2(3);
925     l_transfer_to_location         NUMBER;
926     l_locator_id                   NUMBER;
927     l_transaction_temp_id          NUMBER;
928     l_transaction_header_id        NUMBER;
929     l_subinventory_code            VARCHAR2(30);
930     l_transaction_quantity         NUMBER;
931     l_inventory_item_id            NUMBER;
932     l_temp_id                      NUMBER;
933     l_mtl_reservation              inv_reservation_global.mtl_reservation_tbl_type;
934     l_return_status                VARCHAR2(1);
935     l_grouping_rule_id             NUMBER;
936     l_message                      VARCHAR2(2000);
937     l_msg_data                     VARCHAR2(2000);
938     l_count                        NUMBER;
939     l_msg_count                    NUMBER;
940     l_from_serial_number           VARCHAR2(30);
941     l_to_serial_number             VARCHAR2(30);
942     l_priority                     NUMBER;
943     l_std_op_id                    NUMBER;
944     l_next_task_id                 NUMBER;
945     l_wms_task_type                NUMBER;
946     l_operation_plan_id            NUMBER;
947     l_move_order_line_id           NUMBER;
948     l_line_id                      NUMBER;
949     l_serial_flag                  VARCHAR2(30)  := 'N';
950     l_rcount                       NUMBER;
951     l_lpn_id                       NUMBER;
952     --     l_lpn_cont                      NUMBER;
953     l_lpn_cg_id                    NUMBER;
954     l_completion_txn_id            NUMBER;
955     l_ref_id                       NUMBER;
956     l_txn_source_type_id           NUMBER;
957     l_flow_schedule                VARCHAR2(1);
958     l_transaction_source_id        NUMBER;
959     l_rows_detailed                NUMBER;
960     l_mtl_status                   NUMBER;
961     l_quantity_detailed            NUMBER;
962     l_quantity                     NUMBER;
963     l_backorder_delivery_detail_id NUMBER;
964     l_crossdock_type               NUMBER;
965     wdt_exist                      VARCHAR2(1)   := NULL;
966     l_tt_id                        NUMBER        := NULL;
967 
968     CURSOR molines_csr IS
969        SELECT mtrl.line_id
970             , mtrl.reference_id
971             , mtrl.transaction_source_type_id
972             , mtrl.quantity_detailed
973             , mtrl.quantity
974             , mtrl.backorder_delivery_detail_id
975 	    , NVL(crossdock_type, 1)
976 	    , mtrl.to_subinventory_code
977 	    , mtrl.to_locator_id
978             , mtrl.reference_detail_id
979 	 FROM   mtl_txn_request_lines mtrl, mtl_txn_request_headers mtrh
980 	 WHERE  mtrl.lpn_id = p_lpn_id
981 	 AND    mtrl.organization_id = p_org_id
982 	 AND    mtrl.header_id = mtrh.header_id
983 	 AND    mtrl.line_status <> inv_globals.g_to_status_closed
984 	 AND    mtrh.move_order_type = inv_globals.g_move_order_put_away
985 	 AND    mtrl.line_id = NVL(p_move_order_line_id, mtrl.line_id)    -- added for ATF_J
986 
987      --BUG 5194761
988      UNION
989        SELECT mtrl.line_id
990             , mtrl.reference_id
991             , mtrl.transaction_source_type_id
992             , mtrl.quantity_detailed
993             , mtrl.quantity
994             , mtrl.backorder_delivery_detail_id
995 	    , NVL(crossdock_type, 1)
996 	    , mtrl.to_subinventory_code
997 	    , mtrl.to_locator_id
998             , mtrl.reference_detail_id
999 	 FROM   mtl_txn_request_lines mtrl, mtl_txn_request_headers mtrh
1000 	 WHERE  mtrl.lpn_id = p_lpn_id
1001 	 AND    mtrl.organization_id = p_org_id
1002 	 AND    mtrl.header_id = mtrh.header_id
1003 	 AND    mtrl.line_status <> inv_globals.g_to_status_closed
1004 	 AND    mtrh.move_order_type = inv_globals.g_move_order_put_away
1005 	 AND    p_move_order_line_id IS NOT NULL
1006          AND    mtrl.reference_detail_id = p_move_order_line_id;
1007     --END BUG 5194761
1008 
1009 
1010     -- ATF_J2
1011     -- Modified pregen_suggestions_csr cursor to get data from MOL also.
1012     CURSOR pregen_suggestions_csr IS
1013        SELECT mmtt.last_update_date
1014             , mmtt.transaction_temp_id
1015             , mmtt.locator_id mmtt_loc_id
1016             , mol.to_locator_id mol_loc_id
1017             , mol.backorder_delivery_detail_id
1018 	    , mmtt.operation_plan_id
1019 	    , mol.inspection_status
1020 	 FROM   mtl_material_transactions_temp mmtt, mtl_txn_request_lines mol
1021 	 WHERE  mmtt.lpn_id = l_lpn_id
1022 	 -- Added following line for ATF_J
1023 	 AND    mmtt.move_order_line_id = NVL(p_move_order_line_id, mmtt.move_order_line_id)
1024 	 AND    mmtt.organization_id = p_org_id
1025 	 -- Added for ATF_J to make sure dummy packing MMTT lines are not selected
1026 	 AND    mmtt.transaction_action_id NOT IN (50, 51, 52)
1027 	 AND    mmtt.move_order_line_id = mol.line_id
1028 	 -- Bug# 3434940 - Performance Fixes
1029 	 -- Also join against org and LPN for MOL to speed up the parsing
1030 	 -- time for the query
1031 	 AND    mol.organization_id = p_org_id
1032 	 AND    mol.lpn_id = l_lpn_id;
1033 
1034     l_pregen_suggestion            pregen_suggestions_csr%ROWTYPE;
1035 
1036     -- End ATF_J2
1037 
1038 
1039     -- ATF_J
1040     -- It is not necessary to add P_move_order_line_ID to cursor suggestion_csr
1041     -- because lpn_csr is called within molines_csr and l_line_id is already restricted.
1042 
1043 
1044     CURSOR suggestions_csr IS
1045       SELECT transaction_header_id
1046            , transaction_temp_id
1047            , inventory_item_id
1048            , revision
1049            , subinventory_code
1050            , locator_id
1051            , transaction_quantity
1052            , transfer_to_location
1053            , NVL(standard_operation_id, 2)
1054            , task_priority
1055            , NVL(wms_task_type, 2)
1056            , operation_plan_id
1057            , move_order_line_id
1058   FROM   mtl_material_transactions_temp
1059   WHERE  move_order_line_id = l_line_id
1060   AND    transaction_action_id NOT IN (50, 51, 52)   --ATF_J3: to make sure dummy packing MMTT lines not selected
1061 
1062   ;
1063 
1064     -- ATF_J
1065     -- It is not necessary to add P_move_order_line_ID to cursor lpn_csr
1066     -- because lpn_csr is for creating move order lines when they do not exist,
1067     -- in which case P_move_order_line_ID will not be passed.
1068 
1069     CURSOR lpn_csr IS
1070       SELECT inventory_item_id
1071            , quantity
1072            , uom_code
1073            , lot_number
1074            , revision
1075            , cost_group_id
1076            , secondary_quantity -- Added for OPM convergance
1077            , secondary_uom_code -- Added for OPM convergance
1078       FROM   wms_lpn_contents
1079       WHERE  parent_lpn_id = p_lpn_id;
1080 
1081     l_m_item                       NUMBER;
1082     l_m_qty                        NUMBER;
1083     l_m_uom                        VARCHAR2(3);
1084 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
1085     l_m_lot                        VARCHAR2(80);
1086     l_m_rev                        VARCHAR2(3);
1087     l_m_hdr                        NUMBER;
1088     l_m_stat                       VARCHAR2(30);
1089     l_m_line                       NUMBER;
1090     l_m_sub                        VARCHAR2(10);
1091     l_m_loc                        NUMBER;
1092     l_cdock_flag                   NUMBER;
1093     l_ret_crossdock                NUMBER;
1094     l_td_crossdock                 VARCHAR2(3);
1095     -- ATFJ_2
1096     -- removed l_make_suggestions check.
1097     -- Because it is sufficient to determine if call rules engine or not
1098     -- based on mol.quantity_detailed and mol.quantity
1099 
1100     --     l_make_suggestions           VARCHAR2(2);
1101     l_regeneration_interval        NUMBER;
1102     l_last_update_date             DATE;
1103     l_pregen_putaway_tasks_flag    NUMBER;
1104     l_temp                         NUMBER;
1105     l_temp_update_date             DATE;
1106     l_wip_supply_type              NUMBER;
1107     l_lpn_context                  NUMBER;
1108     l_project_id                   NUMBER;
1109     l_task_id                      NUMBER;
1110     l_emp_id                       NUMBER;
1111     -- Following variables added in ATF_J
1112 
1113     l_atf_error_code               NUMBER;
1114     l_crossdock_missmatch_flag     VARCHAR2(1) := 'N';
1115     l_task_dispatched_flag         VARCHAR2(1) := 'N';
1116     l_op_plan_started_flag         VARCHAR2(1) := 'N';
1117     l_mmtt_staled_flag             VARCHAR2(1) := 'N';
1118     l_need_to_cleanup_pregen       VARCHAR2(1) := 'N';
1119     l_op_plan_instance_status      NUMBER;
1120     l_wlc_mol_missmatch_flag       VARCHAR2(1) := 'N';
1121 
1122       -- End variables added in ATF_J
1123     l_to_sub_code VARCHAR2(30);
1124     l_to_loc_id NUMBER;
1125     l_ref_detail_id NUMBER;--BUG 5194761
1126 
1127     -- Following variables added per GRAO's request
1128     -- used for inventory move by rules's engine
1129     l_quick_pick_flag              VARCHAR2(1) := 'N';
1130     -- End GRAO
1131 
1132     l_debug                        NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1133 
1134     -- Added for OPM convergance
1135     l_sec_qty                      NUMBER;
1136     l_sec_uom                      VARCHAR2(3);
1137     l_process_flag                 VARCHAR2(5); -- nsinghi. Added for GME-WMS Integration
1138   BEGIN
1139     IF (l_debug = 1) THEN
1140       mydebug('suggestions_pub: In suggestions_pub');
1141     END IF;
1142 
1143     -- Bug# 2744186
1144     -- Set the savepoint
1145     SAVEPOINT suggestions_pub_sp;
1146     l_lpn_id := p_lpn_id;
1147     l_return_status := fnd_api.g_ret_sts_success;
1148 
1149     IF (l_lpn_id = 0) THEN
1150       l_lpn_id := NULL;
1151     END IF;
1152 
1153     l_rcount := 0;
1154     l_td_crossdock := 'N';
1155 
1156     -- Bug# 2750060
1157     -- Get the employee ID so we can populate
1158     -- the person_id column in WDT properly.
1159     BEGIN
1160       SELECT employee_id
1161       INTO   l_emp_id
1162       FROM   fnd_user
1163       WHERE  user_id = p_user_id;
1164     EXCEPTION
1165       WHEN NO_DATA_FOUND THEN
1166         IF (l_debug = 1) THEN
1167           mydebug('suggestions_pub: There is no employee tied to the user');
1168         END IF;
1169 
1170         l_emp_id := NULL;
1171     END;
1172 
1173     SELECT regeneration_interval
1174          , NVL(crossdock_flag, 2) cdock
1175          , pregen_putaway_tasks_flag
1176     INTO   l_regeneration_interval
1177          , l_cdock_flag
1178          , l_pregen_putaway_tasks_flag
1179     FROM   mtl_parameters
1180     WHERE  organization_id = p_org_id;
1181 
1182     -- Bug# 2752119
1183     -- Only check for crossdocking if the input parameter is set to 'Y'
1184     -- in case we are calling the Suggestions_PUB while performing an
1185     -- Express Drop with rules validation.  We don't want to check for
1186     -- crossdocking in that scenario
1187 
1188 
1189     --Bug# 2990197
1190     -- The second part of bug 2265157 was never propogated to the main
1191     -- branch. In suggestions_Pub (file WMSTKPTB.pls), before we call
1192     -- crossdock, we should check to make sure that the LPN context is
1193     -- only receiving or WIP.
1194 
1195     SELECT lpn_context
1196     INTO   l_lpn_context
1197     FROM   wms_license_plate_numbers
1198     WHERE  lpn_id = l_lpn_id
1199     AND    organization_id = p_org_id;
1200 
1201     -- ATF_J2 moved crossdock call to after cleanup_suggestions
1202 
1203 
1204     -- Check to see if mo lines exist already
1205     -- If not, we have to create it
1206     BEGIN
1207       SELECT 1
1208       INTO   l_rcount
1209       FROM   DUAL
1210       WHERE  EXISTS(
1211                SELECT 1
1212                FROM   mtl_txn_request_lines l, mtl_txn_request_headers h
1213                WHERE  l.lpn_id = l_lpn_id
1214                AND    l.line_id = NVL(p_move_order_line_id, l.line_id) -- added for ATF_J
1215                AND    NVL(l.quantity_delivered, 0) < l.quantity -- added for ATF_J
1216                AND    l.organization_id = p_org_id
1217                AND    l.header_id = h.header_id
1218                AND    l.line_status <> inv_globals.g_to_status_closed
1219                AND    h.move_order_type = inv_globals.g_move_order_put_away);
1220     --       AND Nvl(l.quantity_detailed,0) <l.quantity);
1221 
1222     EXCEPTION
1223       WHEN NO_DATA_FOUND THEN
1224         l_rcount := 0;
1225     END;
1226 
1227     IF (l_rcount = 0) THEN
1228       IF (l_debug = 1) THEN
1229         mydebug('suggestions_pub: Move order does not exist. Creating new MO..');
1230       END IF;
1231 
1232       IF l_lpn_context <> 1 THEN
1233         IF (l_debug = 1) THEN
1234           mydebug('suggestions_pub: Not an INV LPN');
1235         END IF;
1236 
1237         fnd_message.set_name('WMS', 'WMS_MO_NOT_FOUND');
1238         fnd_msg_pub.ADD;
1239         RAISE fnd_api.g_exc_error;
1240       END IF;
1241     END IF; -- (l_rcount=0)  -- fixed in ATF
1242 
1243     -- ATF_J5
1244     -- Added p_move_order_line_id check below.
1245     -- When item load an inventory LPN, putaway UI will create
1246     -- a move order line and pass that to suggestions_pub.
1247     -- In this case we shouldn't close that move order line and create line.
1248     IF l_lpn_context = 1 AND p_move_order_line_id IS NULL THEN
1249       -- ATF_J2
1250       -- Need to chek whether operation plan instance commenced or not
1251       -- If any MMTT lines has operation plan instance commenced, should not
1252       -- delete MMTT and close move order line.
1253       -- only applicable to J or above
1254 
1255 
1256        mydebug('suggestions_pub: Current release is above J.');
1257        OPEN pregen_suggestions_csr;
1258 
1259        LOOP
1260           FETCH pregen_suggestions_csr INTO l_pregen_suggestion;
1261           EXIT WHEN pregen_suggestions_csr%NOTFOUND;
1262 
1263           IF l_pregen_suggestion.operation_plan_id IS NOT NULL THEN
1264             IF (l_debug = 1) THEN
1265               mydebug('suggestions_pub:  Calling WMS_ATF_RUNTIME_PUB_APIS.Check_Plan_Status with:');
1266               mydebug('p_source_task_id => ' || l_pregen_suggestion.transaction_temp_id);
1267               mydebug('p_activity_type_id => ' || '1');
1268             END IF;
1269 
1270             wms_atf_runtime_pub_apis.check_plan_status(
1271               x_return_status        => l_return_status
1272             , x_msg_data             => l_msg_data
1273             , x_msg_count            => l_msg_count
1274             , x_error_code           => l_atf_error_code
1275             , x_plan_status          => l_op_plan_instance_status
1276             , p_source_task_id       => l_pregen_suggestion.transaction_temp_id
1277             , p_activity_type_id     => '1' -- inbound
1278             );
1279 
1280             IF (l_debug = 1) THEN
1281               mydebug('suggestions_pub:  After calling WMS_ATF_RUNTIME_PUB_APIS.Check_Plan_Status.');
1282               mydebug('x_return_status => ' || l_return_status);
1283               mydebug('x_msg_data => ' || l_msg_data);
1284               mydebug('x_msg_count => ' || l_msg_count);
1285               mydebug('x_error_code => ' || l_atf_error_code);
1286               mydebug('x_plan_status => ' || l_op_plan_instance_status);
1287             END IF;
1288 
1289             IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
1290               IF (l_debug = 1) THEN
1291                 mydebug('suggestions_pub: WMS_ATF_RUNTIME_PUB_APIS.Check_Plan_Status failed.');
1292               END IF;
1293 
1294               RAISE fnd_api.g_exc_error;
1295             END IF; -- (l_return_status <> fnd_api.g_ret_sts_success)
1296 
1297             IF l_op_plan_instance_status IS NOT NULL
1298                AND l_op_plan_instance_status <> 1 -- not pending status
1299                                                   THEN
1300               l_op_plan_started_flag := 'Y';
1301               EXIT;
1302             END IF; -- (l_op_plan_instance_status IS NOT NULL)
1303           END IF; -- IF l_pregen_suggestion.operation_plan_id IS NOT NULL
1304        END LOOP;
1305 
1306        CLOSE pregen_suggestions_csr;
1307 
1308       -- End ATF_J2
1309 
1310       -- ATF_J2
1311       -- only delete MMTT/WDT, close MOL, when there is no operation plan instance commenced
1312 
1313       IF l_op_plan_started_flag = 'Y' THEN
1314         NULL;
1315       --      l_make_suggestions := 'N';  --??
1316 
1317       ELSE
1318         --      l_make_suggestions := 'Y';
1319 
1320         SELECT subinventory_code
1321              , locator_id
1322         INTO   l_m_sub
1323              , l_m_loc
1324         FROM   wms_license_plate_numbers
1325         WHERE  lpn_id = l_lpn_id;
1326 
1327         -- Need to close those old MOL lines
1328         -- for inventory LPN
1329         -- Bug 2271470
1330 
1331         --Also delete corresponding WDT and MMTTs. bug # 2503594
1332         DELETE FROM wms_dispatched_tasks
1333               WHERE transaction_temp_id IN(
1334                       SELECT transaction_temp_id
1335                       FROM   mtl_material_transactions_temp
1336                       WHERE  move_order_line_id IN(
1337                                SELECT mol.line_id
1338                                FROM   mtl_txn_request_lines mol
1339                                WHERE  mol.lpn_id = l_lpn_id
1340                                AND    mol.line_id = NVL(p_move_order_line_id, mol.line_id) -- added for ATF_J
1341                                AND    mol.organization_id = p_org_id
1342                                AND    mol.quantity_detailed > 0
1343                                AND    EXISTS(
1344                                         SELECT 1
1345                                         FROM   mtl_txn_request_headers moh
1346                                         WHERE  mol.header_id = moh.header_id
1347                                         AND    moh.move_order_type = inv_globals.g_move_order_put_away)));
1348 
1349         -- ATF_J2
1350         -- Need to delete MTLT before deleting MMTT
1351         -- Need tuning ???
1352 
1353 
1354         DELETE FROM mtl_transaction_lots_temp mtlt
1355               WHERE EXISTS(
1356                       SELECT 1
1357                       FROM   mtl_material_transactions_temp mmtt
1358                       WHERE  mtlt.transaction_temp_id = mmtt.transaction_temp_id
1359                       AND    mmtt.move_order_line_id IN(
1360                                SELECT mol.line_id
1361                                FROM   mtl_txn_request_lines mol
1362                                WHERE  mol.lpn_id = l_lpn_id
1363                                AND    mol.line_id = NVL(p_move_order_line_id, mol.line_id) -- added for ATF_J
1364                                AND    mol.organization_id = p_org_id
1365                                AND    mol.quantity_detailed > 0
1366                                AND    EXISTS(
1367                                         SELECT 1
1368                                         FROM   mtl_txn_request_headers moh
1369                                         WHERE  mol.header_id = moh.header_id
1370                                         AND    moh.move_order_type = inv_globals.g_move_order_put_away)));
1371 
1372         DELETE FROM mtl_material_transactions_temp
1373               WHERE move_order_line_id IN(
1374                       SELECT mol.line_id
1375                       FROM   mtl_txn_request_lines mol
1376                       WHERE  mol.lpn_id = l_lpn_id
1377                       AND    mol.line_id = NVL(p_move_order_line_id, mol.line_id) -- added for ATF_J
1378                       AND    mol.organization_id = p_org_id
1379                       AND    mol.quantity_detailed > 0
1380                       AND    EXISTS(SELECT 1
1381                                     FROM   mtl_txn_request_headers moh
1382                                     WHERE  mol.header_id = moh.header_id
1383                                     AND    moh.move_order_type = inv_globals.g_move_order_put_away));
1384 
1385         UPDATE mtl_txn_request_lines mol
1386         SET mol.line_status = inv_globals.g_to_status_closed
1387         WHERE  mol.lpn_id = l_lpn_id
1388         AND    mol.line_id = NVL(p_move_order_line_id, mol.line_id) -- added for ATF_J
1389         AND    mol.organization_id = p_org_id
1390 --        AND    mol.quantity_detailed > 0    -- removed in ATF_J3
1391         AND    EXISTS(SELECT 1
1392                       FROM   mtl_txn_request_headers moh
1393                       WHERE  mol.header_id = moh.header_id
1394                       AND    moh.move_order_type = inv_globals.g_move_order_put_away);
1395 
1396         IF (l_debug = 1) THEN
1397           mydebug('suggestions_pub: after getting sub and loc');
1398           mydebug('suggestions_pub: Loc' || l_m_loc);
1399           mydebug('suggestions_pub: Sub' || l_m_sub);
1400         END IF;
1401 
1402         OPEN lpn_csr;
1403 
1404         IF (l_debug = 1) THEN
1405           mydebug('suggestions_pub: Opened lpncsr');
1406         END IF;
1407 
1408         LOOP
1409           FETCH lpn_csr INTO
1410             l_m_item,
1411             l_m_qty,
1412             l_m_uom,
1413             l_m_lot,
1414             l_m_rev,
1415             l_lpn_cg_id,
1416             l_sec_qty,
1417             l_sec_uom;
1418           EXIT WHEN lpn_csr%NOTFOUND;
1419 
1420           IF (l_debug = 1) THEN
1421             mydebug('suggestions_pub: lpn loop');
1422             mydebug('suggestions_pub: lot' || l_m_lot);
1423           END IF;
1424 
1425           IF l_m_lot = '-999' THEN
1426             l_m_lot := NULL;
1427           END IF;
1428 
1429           IF l_m_rev = '-999' THEN
1430             l_m_rev := NULL;
1431           END IF;
1432 
1433           SELECT mil.project_id
1434                , mil.task_id
1435           INTO   l_project_id
1436                , l_task_id
1437           FROM   mtl_item_locations mil
1438           WHERE  mil.inventory_location_id = l_m_loc
1439           AND    mil.organization_id = p_org_id
1440           AND    mil.subinventory_code = l_m_sub;
1441 
1442           -- Call create_mo
1443           IF (l_debug = 1) THEN
1444             mydebug('suggestions_pub: Calling create_mo');
1445           END IF;
1446 
1447           wms_task_dispatch_put_away.create_mo(
1448             p_org_id                         => p_org_id
1449           , p_inventory_item_id              => l_m_item
1450           , p_qty                            => l_m_qty
1451           , p_uom                            => l_m_uom
1452           , p_lpn                            => l_lpn_id
1453           , p_project_id                     => l_project_id
1454           , p_task_id                        => l_task_id
1455           , p_reference                      => NULL
1456           , p_reference_type_code            => NULL
1457           , p_reference_id                   => NULL
1458           , p_lot_number                     => l_m_lot
1459           , p_revision                       => l_m_rev
1460           , p_header_id                      => l_m_hdr
1461           , p_sub                            => l_m_sub
1462           , p_loc                            => l_m_loc
1463           , x_line_id                        => l_m_line
1464           , p_inspection_status              => NULL
1465           , p_transaction_type_id            => 64
1466           , p_transaction_source_type_id     => 4
1467           , p_wms_process_flag               => NULL
1468           , x_return_status                  => l_return_status
1469           , x_msg_count                      => l_msg_count
1470           , x_msg_data                       => l_msg_data
1471           , p_from_cost_group_id             => l_lpn_cg_id
1472           , p_sec_qty                        => l_sec_qty  -- Added for OPM Convergance
1473           , p_sec_uom                        => l_sec_uom  -- Added for OPM Convergance
1474           );
1475           -- bug fix 2271470
1476           l_m_hdr := NULL;
1477           fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
1478 
1479           IF (l_msg_count = 0) THEN
1480             IF (l_debug = 1) THEN
1481               mydebug('suggestions_pub: Successful');
1482             END IF;
1483           ELSIF(l_msg_count = 1) THEN
1484             IF (l_debug = 1) THEN
1485               mydebug('suggestions_pub: Not Successful');
1486               mydebug('suggestions_pub: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
1487             END IF;
1488           ELSE
1489             IF (l_debug = 1) THEN
1490               mydebug('suggestions_pub: Not Successful2');
1491             END IF;
1492 
1493             FOR i IN 1 .. l_msg_count LOOP
1494               l_msg_data := fnd_msg_pub.get(i, 'F');
1495 
1496               IF (l_debug = 1) THEN
1497                 mydebug('suggestions_pub: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
1498               END IF;
1499             END LOOP;
1500           END IF;
1501 
1502           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1503             fnd_message.set_name('WMS', 'WMS_TD_CMO_ERROR');
1504             fnd_msg_pub.ADD;
1505             RAISE fnd_api.g_exc_error;
1506           ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1507             fnd_message.set_name('WMS', 'WMS_TD_CMO_ERROR');
1508             fnd_msg_pub.ADD;
1509             RAISE fnd_api.g_exc_error;
1510           END IF;
1511 
1512           IF (l_debug = 1) THEN
1513             mydebug('suggestions_pub: Line ID created');
1514             mydebug('suggestions_pub: ' || l_m_line);
1515           END IF;
1516         END LOOP;
1517 
1518         CLOSE lpn_csr;
1519       END IF; -- (l_op_plan_started_flag = 'Y') added for ATF_J2
1520 
1521       IF p_drop_type = 'MD' THEN
1522 
1523         -- Update the move order lines with the user inputted sub/loc.
1524         -- Also null out the quantity detailed
1525 	 IF (l_debug = 1) THEN
1526 	    mydebug('Manual Drop: Updating Move orders to : Sub: ' || p_subinventory || ' loc : '||p_locator_id );
1527 	 END IF;
1528 
1529 	 UPDATE mtl_txn_request_lines mol
1530 	   SET    to_subinventory_code = p_subinventory
1531 	   , to_locator_id = p_locator_id
1532 	   , quantity_detailed = NULL
1533 	   WHERE  organization_id = p_org_id
1534 	   AND    lpn_id = l_lpn_id
1535 	   AND    mol.line_status <> inv_globals.g_to_status_closed;
1536       END IF;
1537 
1538     END IF; -- (l_lpn_context = 1)
1539 
1540     IF (l_debug = 1) THEN
1541       mydebug('suggestions_pub: after lpn crsr loop');
1542     END IF;
1543 
1544     -- Code to check for pregenerated putaway allocations
1545     -- The corresponding pseudo code for that is as follows:
1546     --
1547     -- For a given Equipment, LPN, user, org
1548     -- If there are putaway suggestions already created for this LPN
1549     --  if (current time - update date >   regeneration interval)
1550     --      or (l_pregen_putaway_tasks_flag = 0)
1551     --         Delete suggestions and call Putaway Rules Engine again.
1552     --         Delete all MMTT lines for that LPN and set detailed quantity
1553     --                           = 0 in MO line
1554     --         l_make_suggestions := Y;
1555     --
1556     --  Else
1557     --     Dont call suggestions API
1558     --     l_make_suggestions := N;
1559     --Else
1560     --  l_make_suggestions := Y
1561     --
1562     --If l_make_suggestions = Y
1563     --  Proceed as before in calling suggestions API
1564     --Else
1565     --  Do not create suggestions
1566     --
1567 
1568     IF (l_debug = 1) THEN
1569       mydebug('suggestions_pub: Code to check for pregenerated putaway suggestions');
1570     END IF;
1571 
1572     l_rcount := 0;
1573     l_temp_update_date := SYSDATE;
1574 
1575     -- ATF_J2
1576     -- Rewrite the logic for deleting pre-generated MMTT
1577     --
1578     -- 1. If LPN context is INV, do not need to delete pregenerate, because MMTT deletion has been
1579     --    taken care of previously for inventory LPNs.
1580     --
1581     -- 2. Open and LOOP through pregen_suggestions_csr, which will decide for this
1582     --    LPN/MOL combination do we need to cleanup suggestions or not,
1583     --    by setting l_Need_to_cleanup_pregen flag.
1584     --
1585     --
1586     --
1587     --    2.1 If mol.backorder_delivery_detail_id IS NOT NULL (crossdocked)
1588     --           AND MOL.to_locator_id <> MMTT.locator_id (and crossdock doesnot match with MMTT)
1589     --          THEN
1590     --              set l_crossdock_missmatch_flag Yes
1591     --
1592     --         END IF;
1593     --       If MOL crossdock locator doesnot match MMTT's destination locator,
1594     --       for sure there would not be task or operation plan commence, therefore
1595     --       does not need to check those.
1596     --
1597     --    2.2 If Task has commenced, set l_task_dispatched_flag
1598     --
1599     --    2.3 If operation plan has commenced, set l_op_plan_started_flag
1600     --
1601     --    2.4 If task staled and not crossdock, set l_mmtt_staled_flag
1602     --
1603     -- 3. After the loop,
1604     --      set l_Need_to_cleanup_prege based on the flags set.
1605     --
1606     --
1607     -- 4. If l_Need_to_cleanup_prege is YES, call wms_putaway_suggestions.cleanup_suggestions, passing lpn_id and move_order_line_id
1608 
1609     IF l_lpn_context <> 1 THEN
1610       IF pregen_suggestions_csr%ISOPEN THEN
1611         CLOSE pregen_suggestions_csr;
1612       END IF;
1613 
1614       OPEN pregen_suggestions_csr;
1615 
1616       LOOP
1617         FETCH pregen_suggestions_csr INTO l_pregen_suggestion;
1618         EXIT WHEN pregen_suggestions_csr%NOTFOUND;
1619 
1620   l_rcount := l_rcount + 1;
1621 
1622         /*
1623                -- 2.1 Check if MOL crossdock locator doesnot match MMTT's destination locator.
1624 
1625                IF l_pregen_suggestion.backorder_delivery_detail_id IS NOT NULL -- This MOL has been back ordered
1626                  AND l_pregen_suggestion.mol_loc_id <> l_pregen_suggestion.mmtt_loc_id -- crossdock locator does not match pregenerated MMTT.locator
1627                  AND l_crossdock_missmatch_flag = 'N'
1628                  THEN
1629             l_crossdock_missmatch_flag := 'Y';
1630             EXIT;
1631             -- we can only exit in this check, because crossdock mismatch has the highest
1632             -- precedence.
1633 
1634                END IF;   --(l_pregen_suggestion.backorder_delivery_detail_id IS NOT NULL)
1635 
1636                  */
1637 
1638         -- 2.2 Check if task has been dispatched.
1639 
1640         IF l_task_dispatched_flag = 'N' THEN
1641           BEGIN
1642             SELECT '1'
1643             INTO   wdt_exist
1644             FROM   DUAL
1645             WHERE  EXISTS(SELECT transaction_temp_id
1646                           FROM   wms_dispatched_tasks
1647                           WHERE  transaction_temp_id = l_pregen_suggestion.transaction_temp_id);
1648           EXCEPTION
1649             WHEN NO_DATA_FOUND THEN
1650               wdt_exist := '2';
1651           END;
1652 
1653           IF (wdt_exist = '1') THEN
1654             -- some tasks are already dispatched
1655             -- so nothing needs to be done with pregenerated suggestions
1656             l_task_dispatched_flag := 'Y';
1657             EXIT;
1658           END IF; -- (wdt_exist = '1')
1659         END IF; -- l_task_dispatched_flag = 'N'
1660 
1661   -- 2.3 check if operation plan has commenced
1662 
1663         IF l_pregen_suggestion.operation_plan_id IS NOT NULL THEN
1664 	   mydebug('suggestions_pub: Current release is above J.');
1665 
1666 	   IF l_op_plan_started_flag = 'N' THEN
1667 	      IF (l_debug = 1) THEN
1668 		 mydebug('suggestions_pub:  Calling WMS_ATF_RUNTIME_PUB_APIS.Check_Plan_Status with:');
1669 		 mydebug('p_source_task_id => ' || l_pregen_suggestion.transaction_temp_id);
1670 	      END IF;
1671 
1672 	      wms_atf_runtime_pub_apis.check_plan_status
1673 		(
1674 		 x_return_status        => l_return_status
1675 		 , x_msg_data             => l_msg_data
1676 		 , x_msg_count            => l_msg_count
1677 		 , x_error_code           => l_atf_error_code
1678 		 , x_plan_status          => l_op_plan_instance_status
1679 		 , p_source_task_id       => l_pregen_suggestion.transaction_temp_id
1680 		 , p_activity_type_id     => '1' -- inbound
1681 		 );
1682 
1683 	      IF (l_debug = 1) THEN
1684 		 mydebug('suggestions_pub:  After calling WMS_ATF_RUNTIME_PUB_APIS.Check_Plan_Status.');
1685 		 mydebug('x_return_status => ' || l_return_status);
1686 		 mydebug('x_msg_data => ' || l_msg_data);
1687 		 mydebug('x_msg_count => ' || l_msg_count);
1688 		 mydebug('x_error_code => ' || l_atf_error_code);
1689 		 mydebug('x_plan_status => ' || l_op_plan_instance_status);
1690 	      END IF;
1691 
1692 	      IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
1693 		 IF (l_debug = 1) THEN
1694 		    mydebug('suggestions_pub: WMS_ATF_RUNTIME_PUB_APIS.Check_Plan_Status failed.');
1695 		 END IF;
1696 
1697 		 RAISE fnd_api.g_exc_error;
1698 	      END IF; -- (l_return_status <> fnd_api.g_ret_sts_success)
1699 
1700 	      IF l_op_plan_instance_status IS NOT NULL
1701 		AND l_op_plan_instance_status <> 1 -- not pending status
1702 		THEN
1703 		 l_op_plan_started_flag := 'Y';
1704 		 EXIT;
1705 	      END IF; -- (l_op_plan_instance_status IS NOT NULL)
1706 	   END IF; -- l_op_plan_started_flag = 'N'
1707         END IF; -- (l_pregen_suggestion.operation_plan_id
1708 
1709         IF (
1710             ((SYSDATE - l_pregen_suggestion.last_update_date) * 24 * 60 > l_regeneration_interval)
1711             OR -- MMTT line staled
1712                l_pregen_putaway_tasks_flag <> 1  -- ATF_J5: also treat MMTT lines other than pre-generated as stale. This is to cover what cleanup_partial_putaway used to do
1713            ) -- MMTT was not generated by pregenerate (???)
1714            AND l_mmtt_staled_flag = 'N' THEN
1715 	   IF (l_pregen_suggestion.backorder_delivery_detail_id IS NULL -- not a backordered line
1716 	       AND Nvl(l_pregen_suggestion.inspection_status, 2) <> 1) THEN  -- not a line that requires inspection i.e. manually pre-gen
1717             l_mmtt_staled_flag := 'Y';
1718           END IF; -- (l_pregen_suggestion.backorder_delivery_detail_id IS NULL)
1719         END IF; -- (Sysdate - l_pregen_suggestion.last_update_date)*24*60
1720       END LOOP;
1721 
1722       CLOSE pregen_suggestions_csr;
1723 
1724       IF (l_debug = 1) THEN
1725         mydebug(' suggestions_pub: l_crossdock_missmatch_flag = ' || l_crossdock_missmatch_flag);
1726         mydebug(' suggestions_pub: l_task_dispatched_flag = ' || l_task_dispatched_flag);
1727         mydebug(' suggestions_pub: l_op_plan_started_flag = ' || l_op_plan_started_flag);
1728         mydebug(' suggestions_pub: l_mmtt_staled_flag = ' || l_mmtt_staled_flag);
1729       END IF;
1730 
1731       IF l_task_dispatched_flag = 'Y' THEN
1732         l_need_to_cleanup_pregen := 'N';
1733       ELSIF l_op_plan_started_flag = 'Y' THEN
1734         l_need_to_cleanup_pregen := 'N';
1735       ELSIF l_mmtt_staled_flag = 'Y' THEN
1736         l_need_to_cleanup_pregen := 'Y';
1737       END IF;
1738 
1739       IF (l_debug = 1) THEN
1740         mydebug('suggestions_pub: l_need_to_cleanup_pregen = ' || l_need_to_cleanup_pregen);
1741       END IF;
1742 
1743       IF l_need_to_cleanup_pregen = 'Y' OR  -- pregenerated MMTT needs cleanup
1744 	l_rcount = 0  -- there is no pregenerated MMTT
1745 	THEN
1746 
1747 	 IF l_rcount <> 0 THEN -- there are pregenerated MMTT lines, therefore do need to cleanup
1748 	    IF (l_debug = 1) THEN
1749 	       mydebug('suggestions_pub: calling cleanup_suggestions (autonomous_transaction) with: ');
1750 	       mydebug('p_lpn_id = ' || l_lpn_id);
1751 	       mydebug('p_org_id = ' || p_org_id);
1752 	       mydebug('p_move_order_line_id = '||p_move_order_line_id);
1753 	    END IF;
1754 
1755 
1756 	    -- Modified the following for bug fix 3866880
1757 	    -- Decommission the cleanup_suggestions
1758 	    -- api with autonomous commit, which is conflicting with
1759 	    -- MMTT and MOL update from inbound UI for item load.
1760 	    -- The original need for autonomous cleanup_suggestions
1761 	    -- is satisfied by passing p_for_manual_drop => true
1762 	    -- into abort_operation_instance .
1763 
1764 	    wms_putaway_suggestions.cleanup_suggestions
1765 	      (
1766 	       p_lpn_id                 => l_lpn_id
1767 	       , p_org_id                 => p_org_id
1768 	       , x_return_status          => l_return_status
1769 	       , x_msg_count              => x_msg_count
1770 	       , x_msg_data               => x_msg_data
1771 	       , p_move_order_line_id     => p_move_order_line_id
1772 	       ); --added for ATF_J2
1773 
1774 	    IF (l_debug = 1) THEN
1775 	       mydebug(' suggestions_pub: After calling wms_putaway_suggestions.cleanup_suggestions ');
1776 	       mydebug('x_return_status = ' || l_return_status);
1777 	       mydebug('x_msg_count = ' || x_msg_count);
1778 	       mydebug('x_msg_data = ' || x_msg_data);
1779 	    END IF;
1780 
1781 	    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1782 	       IF (l_debug = 1) THEN
1783 		  mydebug(' suggestions_pub: wms_putaway_suggestions.cleanup_suggestions failed');
1784 	       END IF;
1785 
1786 	       RAISE fnd_api.g_exc_error;
1787 	    END IF;
1788 
1789 	 END IF; --  IF l_rcount <> 0 THEN
1790 
1791        ELSE     -- (l_need_to_cleanup_pregen = 'Y')
1792 	 --      l_make_suggestions := 'N';
1793 	 NULL;
1794       END IF; -- (l_need_to_cleanup_pregen = 'Y')
1795     END IF; -- (l_lpn_context <> 1)
1796 
1797 
1798     -- Before creating suggestions
1799     -- verify if MOL and LPN contents match or not
1800     -- Only compare if MOL and LPN content record has the same UOM code, which should always be true
1801 
1802 
1803     IF l_lpn_context <> 1 OR p_drop_type IS NULL
1804       OR p_drop_type <> 'IIL' THEN
1805        -- Do not check for mismatch for inventory item load
1806        -- because for inventory LPN item load will create move order line
1807        -- on the fly for the loaded quantity, which will not match the quantity in LPN contents.
1808       BEGIN
1809 
1810        SELECT 'Y'
1811 	 INTO l_wlc_mol_missmatch_flag
1812 	 FROM dual
1813 	 WHERE exists
1814 	 (SELECT wlc.inventory_item_id
1815 	  FROM
1816 	  (SELECT parent_lpn_id,
1817 	   SUM(quantity) quantity,
1818 	   uom_code,
1819 	   inventory_item_id,
1820 	   revision,
1821 	   lot_number,
1822 	   organization_id--BUG 4607833
1823 	   FROM
1824 	   wms_lpn_contents
1825 	   GROUP BY parent_lpn_id, inventory_item_id, revision, lot_number,uom_code,organization_id--BUG 4607833
1826 	   ) wlc,   -- sub-query is necessary because there could be more than one wlc record for the same inventory_item_id, revision, lot_number
1827 	  mtl_txn_request_lines mol
1828 	  WHERE wlc.parent_lpn_id = mol.lpn_id
1829 	  AND wlc.inventory_item_id = mol.inventory_item_id
1830 --	  AND wlc.uom_code = mol.uom_code  -- Bug fix 3200526
1831 	  AND wlc.organization_id = mol.organization_id --Bug 4607833
1832 	  AND (wlc.revision = mol.revision
1833 	       OR(wlc.revision IS NULL AND mol.revision IS NULL)
1834 	       )
1835 	  AND (wlc.lot_number = mol.lot_number
1836 	       OR(wlc.lot_number IS NULL AND mol.lot_number IS NULL)
1837 	       )
1838 	  AND mol.line_status <> 5   -- not closed
1839 	  AND mol.lpn_id = l_lpn_id
1840 	  --    AND mol.line_id = Nvl(p_move_order_line_id, mol.line_id)  -- comment out in ATF_J3, we should only check mismatch based on LPN, because in item load pack/unpack happens after receiving TM call
1841           -- Bug fix 3200526 If the MOLs for this LPN have different UOMs,
1842           -- do not consider it mismatch, because we really don't want to call
1843           -- UOM converstion for this pre-cautionary check.
1844 	GROUP BY wlc.inventory_item_id, wlc.lot_number, wlc.revision, wlc.uom_code
1845 	HAVING MIN(wlc.quantity) <> SUM(mol.quantity-Nvl(mol.quantity_delivered, 0))
1846          AND MIN(mol.UOM_CODE) = MAX(mol.UOM_CODE)
1847          AND wlc.UOM_CODE = MIN(mol.UOM_CODE)
1848        );
1849 
1850       EXCEPTION
1851 	 WHEN OTHERS THEN
1852 	    l_wlc_mol_missmatch_flag := 'N';
1853       END;
1854 
1855       IF l_wlc_mol_missmatch_flag = 'Y' THEN
1856 
1857 	 IF (l_debug = 1) THEN
1858 	    mydebug('suggestions_pub: There is a mismatch between LPN content and MOL quantity.');
1859 	 END IF;
1860 
1861 	 RAISE fnd_api.g_exc_error;
1862       END IF;   -- IF l_wlc_mol_missmatch_flag = 'Y'
1863 
1864     END IF; -- IF l_lpn_context <> 1 THEN
1865 
1866 
1867     -- End ATFJ_2
1868 
1869 
1870     OPEN molines_csr;
1871 
1872     LOOP
1873       FETCH molines_csr INTO l_line_id, l_ref_id, l_txn_source_type_id, l_quantity_detailed, l_quantity
1874      , l_backorder_delivery_detail_id, l_crossdock_type, l_to_sub_code, l_to_loc_id, l_ref_detail_id;
1875       EXIT WHEN molines_csr%NOTFOUND;
1876 
1877         -- ATF_J2
1878         -- Only need to call crossdock if we  cleanup suggestions and
1879         -- create new suggestions.
1880 
1881       --{{
1882       --  When move order line is crossdocked but destination sub/loc not stamped
1883       --  need to do check crossdock again.
1884       --  Need to test the case multiple MOL in the same LPN since it is called by MOL here
1885       --}}
1886 
1887       IF (l_debug = 1) THEN
1888 	mydebug('suggestions_pub: p_move_order_line_id:'||p_move_order_line_id);
1889       END IF;
1890 
1891       --BUG 5194761: Do not call crossdock API if this is called from Item Load
1892       --(p_move_order_line_id is NULL), because it would be called from
1893       -- WMSTKILB pre_process_load
1894       IF (p_move_order_line_id IS NULL
1895           AND ((l_cdock_flag = 1  -- WIP, op-xdock enabled, and x-dock not happened
1896 	        AND p_check_for_crossdock = 'Y'
1897 	        AND l_lpn_context = 2
1898 	        AND l_backorder_delivery_detail_id IS NULL)
1899 	       OR
1900                (l_lpn_context = 3  -- RCV, xdock happened, but staging lane suggestion not successful
1901 	        AND l_backorder_delivery_detail_id IS NOT NULL
1902 		AND (l_to_sub_code IS NULL OR l_to_loc_id IS NULL))
1903 	    ))
1904 	       THEN
1905 	 IF (l_debug = 1) THEN
1906 	    mydebug('suggestions_pub: Crossdock enabled and check is yes');
1907 	 END IF;
1908 
1909 	 -- Call the cross dock API
1910 	 wms_cross_dock_pvt.crossdock(
1911 				      p_org_id                 => p_org_id
1912 				      , p_lpn                    => l_lpn_id
1913 				      , x_ret                    => l_ret_crossdock
1914 				      , x_return_status          => l_return_status
1915 				      , x_msg_count              => l_msg_count
1916 				      , x_msg_data               => l_msg_data
1917 				      , p_move_order_line_id     => l_line_id
1918 				      ); -- added for ATF_J
1919 
1920 	 IF (l_debug = 1) THEN
1921             mydebug('suggestions_pub: Finished calling crossdock API');
1922 	 END IF;
1923 
1924 	 -- See if there are any error messages returned from the API
1925 	 fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
1926 
1927 	 IF (l_msg_count = 0) THEN
1928             IF (l_debug = 1) THEN
1929 	       mydebug('suggestions_pub: Successful');
1930             END IF;
1931           ELSIF(l_msg_count = 1) THEN
1932             IF (l_debug = 1) THEN
1933 	       mydebug('suggestions_pub: Not Successful');
1934 	       mydebug('suggestions_pub: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
1935             END IF;
1936           ELSE
1937             IF (l_debug = 1) THEN
1938 	       mydebug('suggestions_pub: Not Successful2');
1939             END IF;
1940 
1941             FOR i IN 1 .. l_msg_count LOOP
1942 	       l_msg_data := fnd_msg_pub.get(i, 'F');
1943 
1944 	       IF (l_debug = 1) THEN
1945 		  mydebug('suggestions_pub: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
1946 	       END IF;
1947             END LOOP;
1948 	 END IF;
1949 
1950 	 -- Bug# 2744186
1951 	 -- Check the return status from the API call
1952 	 -- Throw an exception if the call was not completed successfully
1953 	 IF (l_return_status = fnd_api.g_ret_sts_success) THEN
1954             IF (l_debug = 1) THEN
1955 	       mydebug('suggestions_pub: Success returned from WMS_Cross_Dock_Pvt.crossdock API');
1956             END IF;
1957           ELSE
1958             IF (l_debug = 1) THEN
1959 	       mydebug('suggestions_pub: Failure returned from WMS_Cross_Dock_Pvt.crossdock API');
1960             END IF;
1961 
1962             RAISE fnd_api.g_exc_error;
1963 	 END IF;
1964 
1965 	 -- Check the cross dock return value
1966 	 IF l_ret_crossdock = 1 THEN
1967             IF (l_debug = 1) THEN
1968 	       mydebug('suggestions_pub: Nothing to Crossdock');
1969             END IF;
1970           ELSIF l_ret_crossdock = 0 THEN
1971             IF (l_debug = 1) THEN
1972 	       mydebug('suggestions_pub: CrossDock Succeeded');
1973             END IF;
1974 
1975             l_td_crossdock := 'Y';
1976           ELSE
1977             -- DO SOMETHING
1978             l_td_crossdock := 'N';
1979 
1980             IF (l_debug = 1) THEN
1981 	       mydebug('suggestions_pub: CrossDock Error');
1982             END IF;
1983 	 END IF;
1984 
1985 	 x_crossdock := l_td_crossdock;
1986        ELSE
1987 	 IF (l_debug = 1) THEN
1988             mydebug('suggestions_pub: Crossdock Not enabled or no check');
1989 	 END IF;
1990       END IF; -- (l_cdock_flag = 1 AND p_check_for_crossdock = 'Y')
1991     END LOOP;
1992 
1993     CLOSE molines_csr;
1994 
1995     OPEN molines_csr;
1996     LOOP
1997       FETCH molines_csr INTO l_line_id, l_ref_id, l_txn_source_type_id, l_quantity_detailed, l_quantity
1998      , l_backorder_delivery_detail_id, l_crossdock_type, l_to_sub_code, l_to_loc_id, l_ref_detail_id;
1999       EXIT WHEN molines_csr%NOTFOUND;
2000 
2001       IF l_quantity_detailed IS NULL THEN
2002         l_quantity_detailed := 0;
2003       END IF;
2004 
2005       IF (l_debug = 1) THEN
2006         mydebug('suggestions_pub: Line ID' || l_line_id);
2007         mydebug('suggestions_pub: l_quantity_detailed' || l_quantity_detailed);
2008         mydebug('suggestions_pub: l_quantity' || l_quantity);
2009         mydebug('suggestions_pub: l_ref_detail_id' || l_ref_detail_id);
2010       END IF;
2011 
2012 
2013       IF (l_ref_detail_id IS NOT NULL) THEN
2014          BEGIN
2015            UPDATE mtl_txn_request_lines
2016             SET  reference_detail_id = NULL
2017            WHERE line_id = l_line_id;
2018          EXCEPTION
2019            WHEN OTHERS THEN
2020             IF (l_debug = 1) THEN
2021               mydebug('suggestions_pub: Error nulling out mtrl.ref_detail_id. SQLERRM:'||SQLERRM);
2022             END IF;
2023             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2024          END;
2025 
2026          IF (l_debug = 1) THEN
2027            mydebug('suggestions_pub: Successfully Null out mtrl.ref_detail_id');
2028          END IF;
2029       END IF;
2030 
2031       -- ATFJ_2
2032       -- removed l_make_suggestions check.
2033       -- Because it is sufficient to determine if call rules engine or not
2034       -- based on mol.quantity_detailed and mol.quantity
2035 
2036       IF NVL(l_quantity_detailed, 0) < NVL(l_quantity, 0) THEN
2037         IF (l_debug = 1) THEN
2038           mydebug('suggestions_pub: Before PPE');
2039           mydebug('suggestions_pub: MOve order line ID ' || l_line_id);
2040         END IF;
2041 
2042         IF l_lpn_context = 1 THEN
2043 	   IF (l_debug = 1) THEN
2044 	      mydebug('suggestions_pub: Clearing qty. tree Cache');
2045 	      --call clear qty tree cache for inventory lpns.
2046 	   END IF;
2047 	   inv_quantity_tree_pvt.clear_quantity_cache;
2048 
2049 	   -- added per GRAO's request
2050 	   -- inventory move for rules' engine
2051 
2052 	   IF (l_debug = 1) THEN
2053 	      mydebug('suggestions_pub: Current release is above J. set l_quick_pick_flag to Y');
2054 	      l_quick_pick_flag := 'Y';
2055 	   END IF;
2056 
2057         END IF;
2058 
2059         inv_ppengine_pvt.create_suggestions(
2060           p_api_version             => l_api_version_number
2061         , p_init_msg_list           => l_init_msg_list
2062         , p_commit                  => fnd_api.g_false
2063         , p_validation_level        => fnd_api.g_valid_level_full
2064         , x_return_status           => l_return_status
2065         , x_msg_count               => x_msg_count
2066         , x_msg_data                => x_msg_data
2067         , p_transaction_temp_id     => l_line_id
2068         , p_reservations            => l_mtl_reservation
2069         , p_suggest_serial          => l_serial_flag
2070         , p_quick_pick_flag         => l_quick_pick_flag
2071         );
2072 --        fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
2073 
2074         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2075           IF (l_debug = 1) THEN
2076             mydebug('suggestions_pub: unexpected error in inv_ppengine_pvt.create_suggestions');
2077           END IF;
2078 
2079           fnd_message.set_name('WMS', 'WMS_ALLOCATE_FAIL');
2080           fnd_msg_pub.ADD;
2081           RAISE fnd_api.g_exc_error;
2082         ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
2083           IF (l_debug = 1) THEN
2084             mydebug('suggestions_pub: expected error in inv_ppengine_pvt.create_suggestions');
2085           END IF;
2086 
2087           fnd_message.set_name('WMS', 'WMS_ALLOCATE_FAIL');
2088           fnd_msg_pub.ADD;
2089           RAISE fnd_api.g_exc_error;
2090         END IF;
2091 
2092         IF (l_debug = 1) THEN
2093           mydebug('suggestions_pub: After calling inv_ppengine_pvt.create_suggestions');
2094           mydebug('suggestions_pub: l_return_status = ' || l_return_status);
2095         END IF;
2096 
2097         -- Update Qty detailed in Mtl_txn_request_lines
2098         -- Bug fix 2271470
2099         -- Need to consider quantity_delivered
2100 
2101         UPDATE mtl_txn_request_lines mol
2102         SET mol.quantity_detailed = (SELECT NVL(mol.quantity_delivered, 0) + NVL(SUM(mmtt.transaction_quantity), 0)
2103                                      FROM   mtl_material_transactions_temp mmtt
2104                                      WHERE  mmtt.move_order_line_id = l_line_id
2105 				     AND transaction_action_id NOT IN (50, 51, 52)
2106 				     AND NOT (transaction_action_id = 2
2107 					      AND transaction_source_type_id = 13) -- this is to make sure the dummy MMTT erwin created does not contribute this calculation
2108 				     )
2109         WHERE  mol.line_id = l_line_id;
2110 
2111         IF (l_debug = 1) THEN
2112           mydebug('suggestions_pub: After UPdate');
2113         END IF;
2114       END IF; -- Nvl(l_quantity_detailed,0) < Nvl(l_quantity,0)
2115 
2116       l_num_of_rows := 0;
2117       OPEN suggestions_csr;
2118 
2119       LOOP
2120         FETCH suggestions_csr INTO l_transaction_header_id
2121        , l_transaction_temp_id
2122        , l_inventory_item_id
2123        , l_revision
2124        , l_subinventory_code
2125        , l_locator_id
2126        , l_transaction_quantity
2127        , l_transfer_to_location
2128        , l_std_op_id
2129        , l_priority
2130        , l_wms_task_type
2131        , l_operation_plan_id
2132        , l_move_order_line_id;
2133 
2134         IF (l_debug = 1) THEN
2135           mydebug('suggestions_pub: Before crs Exit');
2136         END IF;
2137 
2138         EXIT WHEN suggestions_csr%NOTFOUND;
2139 
2140         IF (l_debug = 1) THEN
2141           mydebug('suggestions_pub: After crs Exit ');
2142           mydebug('suggestions_pub: l_crossdock_type ' || l_crossdock_type);
2143           mydebug('suggestions_pub:l_backorder_delivery_detail_id  ' || l_backorder_delivery_detail_id);
2144         END IF;
2145 
2146 	-- {{
2147 	--  removed update WIP related info into MMTT, this should have been take care of by rules engine
2148 	-- }}
2149 
2150         -- Check mtl statuses
2151         IF (l_debug = 1) THEN
2152           mydebug('suggestions_pub: Checking mtl_status');
2153         END IF;
2154 
2155         wms_task_dispatch_put_away.check_mmtt_mtl_status(
2156           p_temp_id           => l_transaction_temp_id
2157         , p_org_id            => p_org_id
2158         , x_mtl_status        => l_mtl_status
2159         , x_return_status     => l_return_status
2160         , x_msg_count         => l_msg_count
2161         , x_msg_data          => l_msg_data
2162         );
2163 
2164         IF (l_debug = 1) THEN
2165           mydebug('suggestions_pub: mtl_status = ' || l_mtl_status);
2166         END IF;
2167 
2168         IF l_mtl_status = 1 THEN
2169           IF (l_debug = 1) THEN
2170             mydebug('suggestions_pub: Invalid Status');
2171           END IF;
2172 
2173           -- Bug# 2743821
2174           -- Commenting out the following rollback statement
2175           -- since the savepoint is never set.  Also setting a
2176           -- different error message since the one used initially
2177           -- doesn't seem to be a valid seeded message.
2178           --ROLLBACK TO mtl_stat_chk;
2179           --FND_MESSAGE.SET_NAME('INV', 'INV_INVALID_STATUS');
2180           fnd_message.set_name('WMS', 'WMS_INVALID_LPN_ITEM_STATUS');
2181           fnd_msg_pub.ADD;
2182           RAISE fnd_api.g_exc_error;
2183         END IF;
2184 
2185    /* nsinghi - GME-WMS Integration. Added the following select stmt to determine if Process Org.
2186    Also added check in If statement to not refer to Wip_Lpn_Completions table for discrete Orgs.
2187    Also added the If statement to update transaction_source_id in MMTT for Process Orgs. */
2188 
2189         SELECT NVL(process_enabled_flag, 'N') INTO l_process_flag
2190         FROM mtl_parameters WHERE organization_id = p_org_id;
2191 
2192         -- Update MMTT for WIP
2193         IF (l_txn_source_type_id = 5 AND l_process_flag = 'N') THEN
2194           SELECT completion_transaction_id
2195                , DECODE(wip_entity_type, 4, 'Y', 'N')
2196                , wip_entity_id
2197           INTO   l_completion_txn_id
2198                , l_flow_schedule
2199                , l_transaction_source_id
2200           FROM   wip_lpn_completions
2201           WHERE  header_id = l_ref_id;
2202 
2203           UPDATE mtl_material_transactions_temp
2204           SET completion_transaction_id = l_completion_txn_id
2205             , flow_schedule = l_flow_schedule
2206             , transaction_source_id = l_transaction_source_id
2207           WHERE  transaction_temp_id = l_transaction_temp_id;
2208         END IF;
2209 
2210         /* nsinghi - added the If statement to update transaction_source_id in MMTT for Process Orgs. */
2211         IF (l_txn_source_type_id = 5 AND l_process_flag = 'Y') THEN
2212            SELECT txn_source_id INTO l_transaction_source_id
2213            FROM mtl_txn_request_lines
2214            WHERE line_id = l_line_id;
2215 
2216            UPDATE mtl_material_transactions_temp
2217            SET flow_schedule = 'N'
2218              , transaction_source_id = l_transaction_source_id
2219            WHERE  transaction_temp_id = l_transaction_temp_id;
2220         END IF;
2221 
2222         -- ATF_J:
2223         -- Following two apis,
2224         -- operation_plan_assignment and init_op_plan_instance,
2225         -- are only called if customer is at patchset J or above.
2226         -- Also, only need to call these APIs if current MMTT does not yet have
2227         -- operation_plan_id stamped.
2228 
2229         --We should not call the ATF APIs in case of Manual Drop
2230         --Hence adding a check for drop type
2231 
2232         IF ( p_drop_type IS NULL OR p_drop_type <> 'MD') THEN
2233 	   mydebug('suggestions_pub: Current release is above J.');
2234 
2235           IF l_operation_plan_id IS NULL
2236              AND l_lpn_context = 3 THEN -- LPN context resides in receiving
2237             mydebug(
2238               ' operation_plan_id is null on MMTT and this is a receiving LPN, assign operation plan and initialize operation plan instance.'
2239             );
2240             --Following API assigns operation plan to an MMTT
2241             mydebug('suggestions_pub: Before calling wms_rule_pvt.assign_operation_plan with following parameters: ');
2242             mydebug('p_task_id = ' || l_transaction_temp_id);
2243             mydebug('p_activity_type_id = ' || 1);
2244             mydebug('p_organization_id = ' || p_org_id);
2245             wms_atf_util_apis.assign_operation_plan(
2246               p_api_version          => 1.0
2247             , x_return_status        => l_return_status
2248             , x_msg_count            => l_msg_count
2249             , x_msg_data             => l_msg_data
2250             , p_task_id              => l_transaction_temp_id
2251             , p_activity_type_id     => 1
2252             , -- Inbound
2253               p_organization_id      => p_org_id
2254             );
2255             mydebug('suggestions_pub: After calling wms_rule_pvt.assign_operation_plan');
2256             mydebug('l_return_status = ' || l_return_status);
2257 
2258             IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
2259               IF (l_debug = 1) THEN
2260                 mydebug('suggestions_pub: wms_rule_pvt.assign_operation_plan failed.');
2261               END IF;
2262 
2263               RAISE fnd_api.g_exc_error;
2264             END IF; -- (l_return_status <> fnd_api.g_ret_sts_success)
2265 
2266                     --Following API initializes the operation plan instance
2267 
2268             mydebug('suggestions_pub: Before calling wms_op_runtime_pub_apis.init_op_plan_instance with following parameters: ');
2269             mydebug('p_source_task_id = ' || l_transaction_temp_id);
2270             mydebug('p_activity_id = ' || 1);
2271             wms_atf_runtime_pub_apis.init_op_plan_instance(
2272               x_return_status      => l_return_status
2273             , x_msg_data           => l_msg_data
2274             , x_msg_count          => l_msg_count
2275             , x_error_code         => l_atf_error_code
2276             , p_source_task_id     => l_transaction_temp_id
2277             , p_activity_id        => 1 -- Inbound
2278             );
2279             mydebug('suggestions_pub: After calling wms_op_runtime_pub_apis.init_plan_instance');
2280             mydebug('l_return_status = ' || l_return_status);
2281 
2282             IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
2283               IF (l_debug = 1) THEN
2284                 mydebug('suggestions_pub: wms_op_runtime_pub_apis.init_plan_instance failed.');
2285               END IF;
2286 
2287               RAISE fnd_api.g_exc_error;
2288             END IF; -- (l_return_status <> fnd_api.g_ret_sts_success)
2289           END IF; -- (l_operation_plan_id IS NULL)
2290         END IF; -- ( p_drop_type IS NULL OR p_drop_type <> 'MD')
2291 
2292                 -- End ATF_J calling operation_plan_assignment and init_op_plan_instance
2293 
2294 
2295                 -- ATF_J
2296                 -- By pass inserting into WMS_DISPATCHED_TASKS
2297                 -- if customer is at patchset J or above,
2298                 -- because this will be handled by activate_operation_instance.
2299                 -- In other words, only insert WMS_DISPATCHED_TASKS if
2300                 -- customer release is below patchset J
2301 
2302                 -- We should not insert record directly into WDT if the drop type is Manual Drop.
2303                 -- Since ATF API activate op instance will be called from complete_putaway_wrapper in this case.
2304 
2305 
2306         l_num_of_rows := l_num_of_rows + 1;
2307 
2308         --inv_debug.message('number of rows = ' || l_num_of_rows);
2309 
2310         IF l_transaction_quantity < 0 THEN
2311           UPDATE mtl_material_transactions_temp
2312           SET transaction_quantity = ABS(transaction_quantity)
2313             , primary_quantity = ABS(primary_quantity)
2314           WHERE  transaction_temp_id = l_transaction_temp_id;
2315         END IF;
2316       END LOOP; -- Detail loop
2317 
2318       CLOSE suggestions_csr;
2319 
2320     END LOOP; -- MO Lines loop
2321 
2322     CLOSE molines_csr;
2323 
2324     -- set output variables
2325     -- Get total number of rows detailed
2326     SELECT COUNT(t.transaction_temp_id)
2327     INTO   l_rows_detailed
2328     FROM   mtl_material_transactions_temp t, mtl_txn_request_lines l
2329     WHERE  l.lpn_id = l_lpn_id
2330     AND    l.organization_id = p_org_id
2331     AND    l.line_id = NVL(p_move_order_line_id, l.line_id) -- Added for ATF_J
2332     AND    l.line_id = t.move_order_line_id;
2333 
2334     x_number_of_rows := l_rows_detailed;
2335     x_crossdock := l_td_crossdock;
2336     x_return_status := fnd_api.g_ret_sts_success;
2337 
2338     -- Fix for Bug 2374961
2339     -- For an Inventory LPN, the rules engine creates a lock
2340     -- at the item org level as part of the detailing
2341     -- Since no commit was being done till the user actually confirmed
2342     -- the drop, the lock was being maintained for an inordinate
2343     -- amount of time, preventing other users from putting away
2344     -- the same item even if it is on a different LPn because of the
2345     -- fact that the rules engine cannot obtain a new lock unless
2346     -- the old one has been released. Hence, for an inventory LPN
2347     -- , have added a commit statement. If the user then changes his
2348     -- mind and presses F2 from the Putaway page, the
2349     --  cleanup_partial_putaway_LPN API is called which deletes
2350     -- the MMTT and MOL lines manually
2351 
2352     --Nested LPN Support - Do the commit only if the flag is set.
2353     IF (l_lpn_context = 1 AND l_rows_detailed > 0) THEN
2354       IF (NVL(p_commit, 'Y') = 'Y') THEN
2355         COMMIT;
2356       END IF;
2357     END IF;
2358   EXCEPTION
2359     WHEN fnd_api.g_exc_error THEN
2360       -- Bug# 2744186
2361       -- Perform a rollback in the exception blocks
2362       ROLLBACK TO suggestions_pub_sp;
2363       x_return_status := fnd_api.g_ret_sts_error;
2364       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2365 
2366       IF (l_debug = 1) THEN
2367         mydebug('suggestions_pub: Excecution error - ' || SQLERRM);
2368       END IF;
2369 
2370       IF pregen_suggestions_csr%ISOPEN THEN
2371         CLOSE pregen_suggestions_csr;
2372       END IF;
2373 
2374       IF molines_csr%ISOPEN THEN
2375         CLOSE molines_csr;
2376       END IF;
2377 
2378       IF lpn_csr%ISOPEN THEN
2379         CLOSE lpn_csr;
2380       END IF;
2381 
2382       IF suggestions_csr%ISOPEN THEN
2383         CLOSE suggestions_csr;
2384       END IF;
2385     WHEN fnd_api.g_exc_unexpected_error THEN
2386       ROLLBACK TO suggestions_pub_sp;
2387       x_return_status := fnd_api.g_ret_sts_unexp_error;
2388       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2389 
2390       IF (l_debug = 1) THEN
2391         mydebug('suggestions_pub: Unexpected error - ' || SQLERRM);
2392       END IF;
2393 
2394       IF pregen_suggestions_csr%ISOPEN THEN
2395         CLOSE pregen_suggestions_csr;
2396       END IF;
2397 
2398       IF molines_csr%ISOPEN THEN
2399         CLOSE molines_csr;
2400       END IF;
2401 
2402       IF lpn_csr%ISOPEN THEN
2403         CLOSE lpn_csr;
2404       END IF;
2405 
2406       IF suggestions_csr%ISOPEN THEN
2407         CLOSE suggestions_csr;
2408       END IF;
2409     WHEN OTHERS THEN
2410       ROLLBACK TO suggestions_pub_sp;
2411       x_return_status := fnd_api.g_ret_sts_unexp_error;
2412       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2413 
2414       IF (l_debug = 1) THEN
2415         mydebug('suggestions_pub: Others exception - ' || SQLERRM);
2416       END IF;
2417 
2418       IF pregen_suggestions_csr%ISOPEN THEN
2419         CLOSE pregen_suggestions_csr;
2420       END IF;
2421 
2422       IF molines_csr%ISOPEN THEN
2423         CLOSE molines_csr;
2424       END IF;
2425 
2426       IF lpn_csr%ISOPEN THEN
2427         CLOSE lpn_csr;
2428       END IF;
2429 
2430       IF suggestions_csr%ISOPEN THEN
2431         CLOSE suggestions_csr;
2432       END IF;
2433   END suggestions_pub;
2434 
2435   /* Local function to insert record in MTL_SERIAL_NUMBERS_TEMP given
2436    * a record type containing the information of one serial number (including
2437    * attributes). This procedure is called from complete_putaway to create
2438    * as many MSNT records for the serial numbers within the LPN for the quantity
2439    * confirmed
2440    */
2441   FUNCTION insert_msnt_rec(
2442     p_transaction_temp_id  IN  NUMBER
2443   , p_serial_number        IN  VARCHAR2
2444   , p_serial_atts          IN  msn_attribute_rec_tp
2445   , p_user_id              IN  NUMBER
2446   , p_to_serial_number     IN  VARCHAR2 DEFAULT NULL) RETURN BOOLEAN IS
2447   BEGIN
2448     INSERT INTO mtl_serial_numbers_temp
2449                 (
2450                  transaction_temp_id
2451                , last_update_date
2452                , last_updated_by
2453                , creation_date
2454                , created_by
2455                , fm_serial_number
2456                , to_serial_number
2457                , vendor_serial_number
2458                , vendor_lot_number
2459                , parent_serial_number
2460                , origination_date
2461                , end_item_unit_number
2462                , territory_code
2463                , time_since_new
2464                , cycles_since_new
2465                , time_since_overhaul
2466                , cycles_since_overhaul
2467                , time_since_repair
2468                , cycles_since_repair
2469                , time_since_visit
2470                , cycles_since_visit
2471                , time_since_mark
2472                , cycles_since_mark
2473                , number_of_repairs
2474                , serial_attribute_category
2475                , c_attribute1
2476                , c_attribute2
2477                , c_attribute3
2478                , c_attribute4
2479                , c_attribute5
2480                , c_attribute6
2481                , c_attribute7
2482                , c_attribute8
2483                , c_attribute9
2484                , c_attribute10
2485                , c_attribute11
2486                , c_attribute12
2487                , c_attribute13
2488                , c_attribute14
2489                , c_attribute15
2490                , c_attribute16
2491                , c_attribute17
2492                , c_attribute18
2493                , c_attribute19
2494                , c_attribute20
2495                , d_attribute1
2496                , d_attribute2
2497                , d_attribute3
2498                , d_attribute4
2499                , d_attribute5
2500                , d_attribute6
2501                , d_attribute7
2502                , d_attribute8
2503                , d_attribute9
2504                , d_attribute10
2505                , n_attribute1
2506                , n_attribute2
2507                , n_attribute3
2508                , n_attribute4
2509                , n_attribute5
2510                , n_attribute6
2511                , n_attribute7
2512                , n_attribute8
2513                , n_attribute9
2514                , n_attribute10
2515                 )
2516     VALUES      (
2517                  p_transaction_temp_id
2518                , SYSDATE
2519                , p_user_id
2520                , SYSDATE
2521                , p_user_id
2522                , p_serial_number
2523                , Nvl(p_to_serial_number,p_serial_number)
2524                , p_serial_atts.vendor_serial_number
2525                , p_serial_atts.vendor_lot_number
2526                , p_serial_atts.parent_serial_number
2527                , p_serial_atts.origination_date
2528                , p_serial_atts.end_item_unit_number
2529                , p_serial_atts.territory_code
2530                , p_serial_atts.time_since_new
2531                , p_serial_atts.cycles_since_new
2532                , p_serial_atts.time_since_overhaul
2533                , p_serial_atts.cycles_since_overhaul
2534                , p_serial_atts.time_since_repair
2535                , p_serial_atts.cycles_since_repair
2536                , p_serial_atts.time_since_visit
2537                , p_serial_atts.cycles_since_visit
2538                , p_serial_atts.time_since_mark
2539                , p_serial_atts.cycles_since_mark
2540                , p_serial_atts.number_of_repairs
2541                , p_serial_atts.serial_attribute_category
2542                , p_serial_atts.c_attribute1
2543                , p_serial_atts.c_attribute2
2544                , p_serial_atts.c_attribute3
2545                , p_serial_atts.c_attribute4
2546                , p_serial_atts.c_attribute5
2547                , p_serial_atts.c_attribute6
2548                , p_serial_atts.c_attribute7
2549                , p_serial_atts.c_attribute8
2550                , p_serial_atts.c_attribute9
2551                , p_serial_atts.c_attribute10
2552                , p_serial_atts.c_attribute11
2553                , p_serial_atts.c_attribute12
2554                , p_serial_atts.c_attribute13
2555                , p_serial_atts.c_attribute14
2556                , p_serial_atts.c_attribute15
2557                , p_serial_atts.c_attribute16
2558                , p_serial_atts.c_attribute17
2559                , p_serial_atts.c_attribute18
2560                , p_serial_atts.c_attribute19
2561                , p_serial_atts.c_attribute20
2562                , p_serial_atts.d_attribute1
2563                , p_serial_atts.d_attribute2
2564                , p_serial_atts.d_attribute3
2565                , p_serial_atts.d_attribute4
2566                , p_serial_atts.d_attribute5
2567                , p_serial_atts.d_attribute6
2568                , p_serial_atts.d_attribute7
2569                , p_serial_atts.d_attribute8
2570                , p_serial_atts.d_attribute9
2571                , p_serial_atts.d_attribute10
2572                , p_serial_atts.n_attribute1
2573                , p_serial_atts.n_attribute2
2574                , p_serial_atts.n_attribute3
2575                , p_serial_atts.n_attribute4
2576                , p_serial_atts.n_attribute5
2577                , p_serial_atts.n_attribute6
2578                , p_serial_atts.n_attribute7
2579                , p_serial_atts.n_attribute8
2580                , p_serial_atts.n_attribute9
2581                , p_serial_atts.n_attribute10
2582                 );
2583     RETURN TRUE;
2584   EXCEPTION
2585     WHEN OTHERS THEN
2586       RETURN FALSE;
2587   END insert_msnt_rec;
2588 
2589 
2590   /* Local function to insert a record in MTL_TRANSACTION_LOTS_TEMP
2591    * from the original MTLT record. These MTLT records are needed by
2592    * the receiving transaction manager for the deliver transaction
2593    */
2594   FUNCTION insert_dup_mtlt (
2595         p_orig_temp_id    IN  NUMBER
2596       , p_new_temp_id     IN  NUMBER
2597       , p_serial_temp_id  IN  NUMBER
2598       , p_item_id         IN  NUMBER
2599       , p_organization_id IN  NUMBER) RETURN BOOLEAN IS
2600   BEGIN
2601     INSERT INTO mtl_transaction_lots_temp
2602                 (
2603                  transaction_temp_id
2604                , last_update_date
2605                , last_updated_by
2606                , creation_date
2607                , created_by
2608                , transaction_quantity
2609                , primary_quantity
2610                , lot_number
2611                , lot_expiration_date
2612                , serial_transaction_temp_id
2613                , description
2614                , vendor_name
2615                , supplier_lot_number
2616                , origination_date
2617                , date_code
2618                , grade_code
2619                , change_date
2620                , maturity_date
2621                , retest_date
2622                , age
2623                , item_size
2624                , color
2625                , volume
2626                , volume_uom
2627                , place_of_origin
2628                , best_by_date
2629                , LENGTH
2630                , length_uom
2631                , recycled_content
2632                , thickness
2633                , thickness_uom
2634                , width
2635                , width_uom
2636                , curl_wrinkle_fold
2637                , lot_attribute_category
2638                , c_attribute1
2639                , c_attribute2
2640                , c_attribute3
2641                , c_attribute4
2642                , c_attribute5
2643                , c_attribute6
2644                , c_attribute7
2645                , c_attribute8
2646                , c_attribute9
2647                , c_attribute10
2648                , c_attribute11
2649                , c_attribute12
2650                , c_attribute13
2651                , c_attribute14
2652                , c_attribute15
2653                , c_attribute16
2654                , c_attribute17
2655                , c_attribute18
2656                , c_attribute19
2657                , c_attribute20
2658                , d_attribute1
2659                , d_attribute2
2660                , d_attribute3
2661                , d_attribute4
2662                , d_attribute5
2663                , d_attribute6
2664                , d_attribute7
2665                , d_attribute8
2666                , d_attribute9
2667                , d_attribute10
2668                , n_attribute1
2669                , n_attribute2
2670                , n_attribute3
2671                , n_attribute4
2672                , n_attribute5
2673                , n_attribute6
2674                , n_attribute7
2675                , n_attribute8
2676                , n_attribute9
2677                , n_attribute10
2678                , vendor_id
2679                , territory_code
2680                 )
2681       (SELECT p_new_temp_id
2682             , mtlt.last_update_date
2683             , mtlt.last_updated_by
2684             , mtlt.creation_date
2685             , mtlt.created_by
2686             , mtlt.transaction_quantity
2687             , mtlt.primary_quantity
2688             , mtlt.lot_number
2689             , mtlt.lot_expiration_date
2690             , p_serial_temp_id
2691             , mln.description
2692             , mln.vendor_name
2693             , mln.supplier_lot_number
2694             , mln.origination_date
2695             , mln.date_code
2696             , mln.grade_code
2697             , mln.change_date
2698             , mln.maturity_date
2699             , mln.retest_date
2700             , mln.age
2701             , mln.item_size
2702             , mln.color
2703             , mln.volume
2704             , mln.volume_uom
2705             , mln.place_of_origin
2706             , mln.best_by_date
2707             , mln.LENGTH
2708             , mln.length_uom
2709             , mln.recycled_content
2710             , mln.thickness
2711             , mln.thickness_uom
2712             , mln.width
2713             , mln.width_uom
2714             , mln.curl_wrinkle_fold
2715             , mln.lot_attribute_category
2716             , mln.c_attribute1
2717             , mln.c_attribute2
2718             , mln.c_attribute3
2719             , mln.c_attribute4
2720             , mln.c_attribute5
2721             , mln.c_attribute6
2722             , mln.c_attribute7
2723             , mln.c_attribute8
2724             , mln.c_attribute9
2725             , mln.c_attribute10
2726             , mln.c_attribute11
2727             , mln.c_attribute12
2728             , mln.c_attribute13
2729             , mln.c_attribute14
2730             , mln.c_attribute15
2731             , mln.c_attribute16
2732             , mln.c_attribute17
2733             , mln.c_attribute18
2734             , mln.c_attribute19
2735             , mln.c_attribute20
2736             , mln.d_attribute1
2737             , mln.d_attribute2
2738             , mln.d_attribute3
2739             , mln.d_attribute4
2740             , mln.d_attribute5
2741             , mln.d_attribute6
2742             , mln.d_attribute7
2743             , mln.d_attribute8
2744             , mln.d_attribute9
2745             , mln.d_attribute10
2746             , mln.n_attribute1
2747             , mln.n_attribute2
2748             , mln.n_attribute3
2749             , mln.n_attribute4
2750             , mln.n_attribute5
2751             , mln.n_attribute6
2752             , mln.n_attribute7
2753             , mln.n_attribute8
2754             , mln.n_attribute9
2755             , mln.n_attribute10
2756             , mln.vendor_id
2757             , mln.territory_code
2758        FROM   mtl_transaction_lots_temp mtlt
2759             , mtl_lot_numbers mln
2760        WHERE  mtlt.transaction_temp_id = p_orig_temp_id
2761        AND    mln.lot_number = mtlt.lot_number
2762        AND    mln.inventory_item_id = p_item_id
2763        AND    mln.organization_id = p_organization_id);
2764     RETURN TRUE;
2765   EXCEPTION
2766     WHEN OTHERS THEN
2767       RETURN FALSE;
2768   END insert_dup_mtlt;
2769 
2770   /* Local function to insert records in MTL_SERIAL_NUMBERS_TEMP
2771    * from the original MSNT record(s). These records are needed by
2772    * the receiving transaction manager for the deliver transaction
2773    */
2774   FUNCTION insert_dup_msnt (
2775         p_orig_temp_id    IN  NUMBER
2776       , p_new_temp_id     IN  NUMBER) RETURN BOOLEAN IS
2777   BEGIN
2778     INSERT INTO mtl_serial_numbers_temp
2779                 (
2780                  transaction_temp_id
2781                , last_update_date
2782                , last_updated_by
2783                , creation_date
2784                , created_by
2785                , fm_serial_number
2786                , to_serial_number
2787                , vendor_serial_number
2788                , vendor_lot_number
2789                , parent_serial_number
2790                , origination_date
2791                , end_item_unit_number
2792                , territory_code
2793                , time_since_new
2794                , cycles_since_new
2795                , time_since_overhaul
2796                , cycles_since_overhaul
2797                , time_since_repair
2798                , cycles_since_repair
2799                , time_since_visit
2800                , cycles_since_visit
2801                , time_since_mark
2802                , cycles_since_mark
2803                , number_of_repairs
2804                , serial_attribute_category
2805                , c_attribute1
2806                , c_attribute2
2807                , c_attribute3
2808                , c_attribute4
2809                , c_attribute5
2810                , c_attribute6
2811                , c_attribute7
2812                , c_attribute8
2813                , c_attribute9
2814                , c_attribute10
2815                , c_attribute11
2816                , c_attribute12
2817                , c_attribute13
2818                , c_attribute14
2819                , c_attribute15
2820                , c_attribute16
2821                , c_attribute17
2822                , c_attribute18
2823                , c_attribute19
2824                , c_attribute20
2825                , d_attribute1
2826                , d_attribute2
2827                , d_attribute3
2828                , d_attribute4
2829                , d_attribute5
2830                , d_attribute6
2831                , d_attribute7
2832                , d_attribute8
2833                , d_attribute9
2834                , d_attribute10
2835                , n_attribute1
2836                , n_attribute2
2837                , n_attribute3
2838                , n_attribute4
2839                , n_attribute5
2840                , n_attribute6
2841                , n_attribute7
2842                , n_attribute8
2843                , n_attribute9
2844                , n_attribute10
2845                 )
2846       (SELECT p_new_temp_id
2847             , last_update_date
2848             , last_updated_by
2849             , creation_date
2850             , created_by
2851             , fm_serial_number
2852             , to_serial_number
2853             , vendor_serial_number
2854             , vendor_lot_number
2855             , parent_serial_number
2856             , origination_date
2857             , end_item_unit_number
2858             , territory_code
2859             , time_since_new
2860             , cycles_since_new
2861             , time_since_overhaul
2862             , cycles_since_overhaul
2863             , time_since_repair
2864             , cycles_since_repair
2865             , time_since_visit
2866             , cycles_since_visit
2867             , time_since_mark
2868             , cycles_since_mark
2869             , number_of_repairs
2870             , serial_attribute_category
2871             , c_attribute1
2872             , c_attribute2
2873             , c_attribute3
2874             , c_attribute4
2875             , c_attribute5
2876             , c_attribute6
2877             , c_attribute7
2878             , c_attribute8
2879             , c_attribute9
2880             , c_attribute10
2881             , c_attribute11
2882             , c_attribute12
2883             , c_attribute13
2884             , c_attribute14
2885             , c_attribute15
2886             , c_attribute16
2887             , c_attribute17
2888             , c_attribute18
2889             , c_attribute19
2890             , c_attribute20
2891             , d_attribute1
2892             , d_attribute2
2893             , d_attribute3
2894             , d_attribute4
2895             , d_attribute5
2896             , d_attribute6
2897             , d_attribute7
2898             , d_attribute8
2899             , d_attribute9
2900             , d_attribute10
2901             , n_attribute1
2902             , n_attribute2
2903             , n_attribute3
2904             , n_attribute4
2905             , n_attribute5
2906             , n_attribute6
2907             , n_attribute7
2908             , n_attribute8
2909             , n_attribute9
2910             , n_attribute10
2911        FROM   mtl_serial_numbers_temp
2912        WHERE  transaction_temp_id = p_orig_temp_id);
2913     RETURN TRUE;
2914   EXCEPTION
2915     WHEN OTHERS THEN
2916       RETURN FALSE;
2917   END insert_dup_msnt;
2918 
2919   /* FP-J Lot/Serial Support Enhancement
2920    * Helper routine to create MTL_TRANSACTION_LOTS_INTERFACE records
2921    * for the lot and quantity corresponding to the drop quantity
2922    * Called for a receiving LPN when INV and PO patch levels are J or higher
2923    */
2924   FUNCTION insert_mtli_helper(
2925           p_txn_if_id       IN OUT NOCOPY NUMBER
2926         , p_lot_number      IN            VARCHAR2
2927         , p_txn_qty         IN            NUMBER
2928         , p_prm_qty         IN            NUMBER
2929         , p_item_id         IN            NUMBER
2930         , p_org_id          IN            NUMBER
2931         , x_serial_temp_id  OUT NOCOPY    NUMBER
2932         , p_product_txn_id  IN OUT NOCOPY NUMBER
2933         , p_temp_id         IN            NUMBER
2934         , p_secondary_quantity IN NUMBER --OPM Convergence
2935         , p_secondary_uom   IN NUMBER --OPM Convergence
2936         ) RETURN BOOLEAN IS
2937     --Local variables
2938     l_lot_status_id         NUMBER;
2939     l_txn_if_id             NUMBER      :=  p_txn_if_id;
2940     l_product_txn_id        NUMBER      :=  p_product_txn_id;
2941     l_expiration_date       DATE;
2942     l_prod_code             VARCHAR2(5) := inv_rcv_integration_apis.G_PROD_CODE;
2943     l_yes                   VARCHAR2(1) := inv_rcv_integration_apis.G_YES;
2944     l_no                    VARCHAR2(1) := inv_rcv_integration_apis.G_NO;
2945     l_false                 VARCHAR2(1) := inv_rcv_integration_apis.G_FALSE;
2946     l_return_status         VARCHAR2(1);
2947     l_msg_count             NUMBER;
2948     l_msg_data              VARCHAR2(10000);
2949     l_debug                 NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2950   BEGIN
2951     --Get the required columns from MLN first
2952     SELECT  expiration_date
2953           , status_id
2954     INTO    l_expiration_date
2955           , l_lot_status_id
2956     FROM    mtl_lot_numbers
2957     WHERE   lot_number = p_lot_number
2958     AND     inventory_item_id = p_item_id
2959     AND     organization_id = p_org_id;
2960 
2961 /*
2962     , p_secondary_quantity         IN             NUMBER  DEFAULT NULL--OPM Convergence
2963     , p_origination_type           IN             NUMBER DEFAULT NULL--OPM Convergence
2964     , p_expiration_action_code     IN             VARCHAR2 DEFAULT NULL--OPM Convergence
2965     , p_expiration_action_date     IN             DATE DEFAULT NULL-- OPM Convergence
2966     , p_hold_date                  IN             DATE DEFAULT NULL--OPM Convergence
2967     , p_parent_lot_number          IN             VARCHAR2 DEFAULT NULL--OPM Convergence
2968     , p_reasond_id                 IN             NUMBER DEFAULT NULL--OPM convergence
2969     ); */
2970     --Call the insert_mtli API
2971     inv_rcv_integration_apis.insert_mtli(
2972           p_api_version                 =>  1.0
2973         , p_init_msg_lst                =>  l_false
2974         , x_return_status               =>  l_return_status
2975         , x_msg_count                   =>  l_msg_count
2976         , x_msg_data                    =>  l_msg_data
2977         , p_transaction_interface_id    =>  l_txn_if_id
2978         , p_lot_number                  =>  p_lot_number
2979         , p_transaction_quantity        =>  p_txn_qty
2980         , p_primary_quantity            =>  p_prm_qty
2981         , p_organization_id             =>  p_org_id
2982         , p_inventory_item_id           =>  p_item_id
2983         , p_expiration_date             =>  l_expiration_date
2984         , p_status_id                   =>  l_lot_status_id
2985         , x_serial_transaction_temp_id  =>  x_serial_temp_id
2986         , p_product_transaction_id      =>  l_product_txn_id
2987         , p_product_code                =>  l_prod_code
2988         , p_att_exist                   =>  l_yes
2989         , p_update_mln                  =>  l_no
2990         , p_secondary_quantity          =>  p_secondary_quantity --OPM Convergence
2991         );
2992 
2993     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2994       fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
2995       IF (l_debug = 1) THEN
2996         mydebug('insert_mtli_helper: Error occurred while creating interface lots: ' || l_msg_data);
2997       END IF;
2998       RETURN FALSE;
2999     END IF;
3000 
3001     RETURN TRUE;
3002 
3003   EXCEPTION
3004     WHEN OTHERS THEN
3005       IF (l_debug = 1) THEN
3006         mydebug('Exception occurred in insert_mtli_helper: ');
3007       END IF;
3008       RETURN FALSE;
3009   END insert_mtli_helper;
3010 
3011   /* FP-J Lot/Serial Support Enhancement
3012    * Helper routine to create MTL_SERIAL_NUMBERS_INTERFACE records
3013    * for the serials corresponding to the dropped quantity
3014    * Called for a receiving LPN when INV and PO patch levels are J or higher
3015    */
3016   FUNCTION insert_msni_helper(
3017           p_txn_if_id       IN OUT NOCOPY NUMBER
3018         , p_serial_number   IN            VARCHAR2
3019         , p_item_id         IN            NUMBER
3020         , p_org_id          IN            NUMBER
3021         , p_product_txn_id  IN OUT NOCOPY NUMBER
3022        ) RETURN BOOLEAN IS
3023     --Local variables
3024     l_serial_status_id      NUMBER;
3025     l_txn_if_id             NUMBER      :=  p_txn_if_id;
3026     l_product_txn_id        NUMBER      :=  p_product_txn_id;
3027     l_prod_code             VARCHAR2(5) := inv_rcv_integration_apis.G_PROD_CODE;
3028     l_yes                   VARCHAR2(1) := inv_rcv_integration_apis.G_YES;
3029     l_no                    VARCHAR2(1) := inv_rcv_integration_apis.G_NO;
3030     l_false                 VARCHAR2(1) := inv_rcv_integration_apis.G_FALSE;
3031     l_return_status         VARCHAR2(1);
3032     l_msg_count             NUMBER;
3033     l_msg_data              VARCHAR2(10000);
3034     l_debug                 NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3035   BEGIN
3036 
3037     --Get the serial status
3038     SELECT  status_id
3039     INTO    l_serial_status_id
3040     FROM    mtl_serial_numbers
3041     WHERE   serial_number = p_serial_number
3042     AND     inventory_item_id = p_item_id;
3043 
3044     --Call the insert_msni API
3045     inv_rcv_integration_apis.insert_msni(
3046           p_api_version                 =>  1.0
3047         , p_init_msg_lst                =>  l_false
3048         , x_return_status               =>  l_return_status
3049         , x_msg_count                   =>  l_msg_count
3050         , x_msg_data                    =>  l_msg_data
3051         , p_transaction_interface_id    =>  l_txn_if_id
3052         , p_fm_serial_number            =>  p_serial_number
3053         , p_to_serial_number            =>  p_serial_number
3054         , p_organization_id             =>  p_org_id
3055         , p_inventory_item_id           =>  p_item_id
3056         , p_status_id                   =>  l_serial_status_id
3057         , p_product_transaction_id      =>  l_product_txn_id
3058         , p_product_code                =>  l_prod_code
3059         , p_att_exist                   =>  l_yes
3060         , p_update_msn                  =>  l_no);
3061 
3062     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3063       fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
3064       IF (l_debug = 1) THEN
3065         mydebug('insert_msni_helper: Error occurred while creating interface serials: ' || l_msg_data);
3066       END IF;
3067       RETURN FALSE;
3068     END IF;
3069 
3070     RETURN TRUE;
3071 
3072   EXCEPTION
3073     WHEN OTHERS THEN
3074       IF (l_debug = 1) THEN
3075         mydebug('Exception occurred in insert_msni_helper: ');
3076       END IF;
3077       RETURN FALSE;
3078   END insert_msni_helper;
3079 
3080   --3978111 CHANGES START
3081 procedure create_snapshot(p_temp_id NUMBER, p_org_id NUMBER)
3082 IS
3083     l_errNum                       NUMBER;
3084     l_errCode                      VARCHAR2(1);
3085     l_errMsg                       VARCHAR2(241);
3086     l_cst_ret                      NUMBER(1):=0;
3087     l_primary_cost_method          NUMBER;
3088     l_debug			   NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3089 BEGIN
3090 	select primary_cost_method
3091 	into   l_primary_cost_method
3092 	from   mtl_parameters
3093 	where  organization_id = p_org_id;
3094 
3095 	IF (l_debug = 1) THEN
3096 		mydebug('complete_putaway: cost method of org'||p_org_id||' is '||l_primary_cost_method);
3097 	END IF;
3098 
3099 	IF l_primary_cost_method in (2,5,6) THEN
3100 		IF (l_debug = 1) THEN
3101 			mydebug('complete_putaway: PRIMARY COST METHOD IS AVG OR FIFO OR LIFO CALLING CSTACOSN.op_snapshot'||p_temp_id);
3102 		END IF;
3103 
3104 		l_cst_ret := CSTACOSN.op_snapshot(i_txn_temp_id => p_temp_id,
3105 						err_num => l_errNum,
3106 						err_code => l_errCode,
3107 						err_msg => l_errMsg);
3108 		IF(l_cst_ret <> 1) THEN
3109 			fnd_message.set_name('BOM', 'CST_SNAPSHOT_FAILED');
3110 			fnd_msg_pub.ADD;
3111 			IF (l_debug = 1) THEN
3112 				mydebug('complete_putaway: Error from CSTACOSN.op_snapshot ');
3113 			END IF;
3114 			raise fnd_api.g_exc_unexpected_error;
3115 		ELSE
3116 			mydebug('complete_putaway: CALL TO CSTACOSN.op_snapshot SUCCESSFULL');
3117 		END IF;
3118 
3119 	END IF;
3120 END create_snapshot;
3121 --3978111 CHANGES END
3122 
3123   -- Bug# 2795096
3124   -- Added an extra input parameter called p_commit
3125   -- which will default to 'Y' = Yes.
3126   -- This is needed when we are performing a consolidated drop
3127   -- where complete_putaway is called for each and every MMTT line
3128   -- within the same commit cycle.  Previously it would perform a
3129   -- commit at the end of the call to complete_putaway.  This doesn't
3130   -- work for consolidated drops since if one of the MMTT lines fails
3131   -- in the call to complete_putaway, we'd like to rollback all of the
3132   -- changes done.  Thus we should not call a commit until complete_putaway
3133   -- has been successfully called for every MMTT line.
3134 
3135   -- FP-J Lot/Serial Support Enhancement
3136   -- Added a new parameter p_product_transaction_id which stores
3137   -- the product_transaction_id column value in MTLI/MSNI for lots and serials
3138   -- that were created from the putaway drop UI. This value would be populated
3139   -- only if there were a quantity discrepancy in the UI
3140 
3141   -- Nested LPN support
3142   -- Added new parameter p_lpn_mode,
3143   -- This will have the following 3 values
3144   --    1 - Tranfer all contents, including child LPNs
3145   --    2 - Drop into To another LPN
3146   --    3 - Item based drop
3147 
3148   PROCEDURE complete_putaway(
3149     p_lpn_id                  IN             NUMBER
3150   , p_org_id                  IN             NUMBER
3151   , p_temp_id                 IN             NUMBER
3152   , p_item_id                 IN             NUMBER
3153   , p_rev                     IN             VARCHAR2
3154   , p_lot                     IN             VARCHAR2
3155   , p_loc                     IN             NUMBER
3156   , p_sub                     IN             VARCHAR2
3157   , p_qty                     IN             NUMBER
3158   , p_uom                     IN             VARCHAR2
3159   , p_user_id                 IN             NUMBER
3160   , p_disc                    IN             VARCHAR2
3161   , x_return_status           OUT NOCOPY     VARCHAR2
3162   , x_msg_count               OUT NOCOPY     NUMBER
3163   , x_msg_data                OUT NOCOPY     VARCHAR2
3164   , p_entire_lpn              IN             VARCHAR2 := 'N'
3165   , p_to_lpn                  IN             VARCHAR2 := fnd_api.g_miss_char
3166   , p_qty_reason_id           IN             NUMBER
3167   , p_loc_reason_id           IN             NUMBER
3168   , p_process_serial_flag     IN             VARCHAR2
3169   , p_commit                  IN             VARCHAR2 := 'Y'
3170   , p_product_transaction_id  IN             NUMBER
3171   , p_lpn_mode                IN             NUMBER
3172   , p_new_txn_header_id       IN             NUMBER
3173   , p_secondary_quantity      IN             NUMBER --OPM Convergence
3174   , p_secondary_uom           IN             VARCHAR2 --OPM Convergence
3175   , p_primary_uom             IN             VARCHAR2
3176     ) IS
3177     l_exist_lpn                  NUMBER;
3178     l_ref                        VARCHAR2(240);
3179     l_ref_id                     NUMBER;
3180     l_ref_type                   NUMBER;
3181     l_qty                        NUMBER;
3182     l_lot_code                   NUMBER;
3183     l_serial_code                NUMBER;
3184     l_is_msni_req                NUMBER        :=0;
3185     l_pr_qty                     NUMBER;
3186     l_serial                     VARCHAR2(30);
3187     l_ser_seq                    NUMBER;
3188     l_org_sub                    VARCHAR2(30);
3189     l_org_loc                    NUMBER;
3190     l_lpn_context                NUMBER;
3191     l_txn_ret                    NUMBER        := 0;
3192     l_orig_qty                   NUMBER;
3193     l_txn_header_id              NUMBER;
3194     l_orig_txn_header_id         NUMBER;
3195     l_to_lpn_id                  NUMBER;
3196     l_txn_mode                   NUMBER        := 2;
3197     l_del_detail_id              NUMBER;
3198     l_orig_txn_uom               VARCHAR2(3);
3199     l_dest_loc_id                NUMBER;
3200     l_dest_sub                   VARCHAR2(10);
3201     l_txn_action_id              NUMBER;
3202     l_dup_temp_id                NUMBER;
3203     l_dup_ser_temp_id            NUMBER;
3204     l_dup_ser_temp_id_exist      NUMBER;
3205     l_lpn_sub                    VARCHAR2(30);
3206     l_lpn_loc                    NUMBER;
3207     l_flow_code                  NUMBER;
3208     l_transaction_source_type_id NUMBER        := 0;
3209     l_inspection_status          NUMBER        := 1;
3210     l_mo_lpn_id                  NUMBER        := 0;
3211     l_wf                         NUMBER;
3212     l_wf_process                 VARCHAR2(80);
3213     l_task_id                    NUMBER;
3214     l_wip_supply_type            NUMBER        := NULL;
3215     l_crossdock_type             NUMBER        := NULL;
3216     cnt                          NUMBER        := NULL;
3217     l_business_flow_code         NUMBER        := 30; -- default to Inventory Putaway
3218     l_wf_temp_sub                VARCHAR2(255);
3219     l_wf_temp_loc                NUMBER;
3220     -- following variables added for ATF_J3
3221 
3222     l_atf_error_code               NUMBER;
3223     l_op_plan_instance_status      NUMBER;
3224     l_return_status                VARCHAR2(1);
3225     l_msg_data                     VARCHAR2(2000);
3226     l_msg_count                    NUMBER;
3227     l_move_order_line_id           NUMBER;
3228     l_mmtt_transaction_qty         NUMBER;
3229     l_serial_transaction_temp_id   NUMBER;
3230     l_txn_type_id		   NUMBER;--Added bug 3978111
3231     -- End variables added for ATF_J3
3232 
3233     l_secondary_uom_code  mtl_material_transactions_temp.secondary_uom_code%TYPE;
3234     l_secondary_qty NUMBER;
3235 
3236     l_exist_msnt                   NUMBER:=0 ;--Added for bug 5213359
3237 
3238     l_acct_period_id               NUMBER:=0; --Added for bug 5403420
3239     l_open_past_period		   BOOLEAN; --Added for bug 5403420
3240 
3241     -- Added for Bug 6356147
3242     l_flow_schedule                VARCHAR2(1);
3243 
3244     --Bug #2966531 - Fetch serial attributes in addition to serial number
3245     --from MTL_SERIAL_NUMBERS for the LPN item combination
3246     CURSOR ser_csr IS
3247       SELECT serial_number
3248 	   , serial_number to_serial_number
3249            , vendor_serial_number
3250            , vendor_lot_number
3251            , parent_serial_number
3252            , origination_date
3253            , end_item_unit_number
3254            , territory_code
3255            , time_since_new
3256            , cycles_since_new
3257            , time_since_overhaul
3258            , cycles_since_overhaul
3259            , time_since_repair
3260            , cycles_since_repair
3261            , time_since_visit
3262            , cycles_since_visit
3263            , time_since_mark
3264            , cycles_since_mark
3265            , number_of_repairs
3266            , serial_attribute_category
3267            , c_attribute1
3268            , c_attribute2
3269            , c_attribute3
3270            , c_attribute4
3271            , c_attribute5
3272            , c_attribute6
3273            , c_attribute7
3274            , c_attribute8
3275            , c_attribute9
3276            , c_attribute10
3277            , c_attribute11
3278            , c_attribute12
3279            , c_attribute13
3280            , c_attribute14
3281            , c_attribute15
3282            , c_attribute16
3283            , c_attribute17
3284            , c_attribute18
3285            , c_attribute19
3286           , c_attribute20
3287            , d_attribute1
3288            , d_attribute2
3289            , d_attribute3
3290            , d_attribute4
3291            , d_attribute5
3292            , d_attribute6
3293            , d_attribute7
3294            , d_attribute8
3295            , d_attribute9
3296            , d_attribute10
3297            , n_attribute1
3298            , n_attribute2
3299            , n_attribute3
3300            , n_attribute4
3301            , n_attribute5
3302            , n_attribute6
3303            , n_attribute7
3304            , n_attribute8
3305            , n_attribute9
3306            , n_attribute10
3307       FROM   mtl_serial_numbers
3308       WHERE  lpn_id = p_lpn_id
3309       AND    inventory_item_id = p_item_id
3310       AND    NVL(revision, '#%^') = NVL(p_rev, '#%^')
3311       AND    NVL(lot_number, -999) = NVL(p_lot, -999)
3312       AND    ROWNUM <= l_pr_qty
3313       AND    (
3314               (p_entire_lpn = 'Y'
3315                AND(group_mark_id IS NULL
3316                    OR group_mark_id = -1))
3317               OR(p_entire_lpn = 'N'
3318                  AND group_mark_id = 1)
3319               OR(p_process_serial_flag = 'N'
3320                  AND p_entire_lpn = 'N'
3321                  AND group_mark_id IS NULL)
3322              )
3323       -- Bug# 2772676
3324       -- For WIP completions, there is a specific serial
3325       -- tied to the MOL/MMTT line which we have to use
3326       AND    (
3327               (l_transaction_source_type_id = 5
3328                AND serial_number IN(SELECT fm_serial_number
3329                                     FROM   wip_lpn_completions_serials
3330                                     WHERE  header_id = l_ref_id))
3331               OR l_transaction_source_type_id <> 5
3332              );
3333 
3334     transfer_lpn_id       NUMBER;
3335     content_lpn_id        NUMBER;
3336     lpn_id                NUMBER;
3337     temp_count            NUMBER;
3338     l_emp_id              NUMBER;
3339     l_debug               NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3340 
3341     -- ER 7307189 changes start
3342 
3343     res_count       NUMBER :=0;
3344 
3345 
3346      CURSOR ser_csr_reserved_lpn
3347                 IS
3348                         SELECT  serial_number                  ,
3349                                 serial_number to_serial_number ,
3350                                 vendor_serial_number           ,
3351                                 vendor_lot_number              ,
3352                                 parent_serial_number           ,
3353                                 origination_date               ,
3354                                 end_item_unit_number           ,
3355                                 territory_code                 ,
3356                                 time_since_new                 ,
3357                                 cycles_since_new               ,
3358                                 time_since_overhaul            ,
3359                                 cycles_since_overhaul          ,
3360                                 time_since_repair              ,
3361                                 cycles_since_repair            ,
3362                                 time_since_visit               ,
3363                                 cycles_since_visit             ,
3364                                 time_since_mark                ,
3365                                 cycles_since_mark              ,
3366                                 number_of_repairs              ,
3367                                 serial_attribute_category      ,
3368                                 c_attribute1                   ,
3369                                 c_attribute2                   ,
3370                                 c_attribute3                   ,
3371                                 c_attribute4                   ,
3372                                 c_attribute5                   ,
3373                                 c_attribute6                   ,
3374                                 c_attribute7                   ,
3375                                 c_attribute8                   ,
3376                                 c_attribute9                   ,
3377                                 c_attribute10                  ,
3378                                 c_attribute11                  ,
3379                                 c_attribute12                  ,
3380                                 c_attribute13                  ,
3381                                 c_attribute14                  ,
3382                                 c_attribute15                  ,
3383                                 c_attribute16                  ,
3384                                 c_attribute17                  ,
3385                                 c_attribute18                  ,
3386                                 c_attribute19                  ,
3387                                 c_attribute20                  ,
3388                                 d_attribute1                   ,
3389                                 d_attribute2                   ,
3390                                 d_attribute3                   ,
3391                                 d_attribute4                   ,
3392                                 d_attribute5                   ,
3393                                 d_attribute6                   ,
3394                                 d_attribute7                   ,
3395                                 d_attribute8                   ,
3396                                 d_attribute9                   ,
3397                                 d_attribute10                  ,
3398                                 n_attribute1                   ,
3399                                 n_attribute2                   ,
3400                                 n_attribute3                   ,
3401                                 n_attribute4                   ,
3402                                 n_attribute5                   ,
3403                                 n_attribute6                   ,
3404                                 n_attribute7                   ,
3405                                 n_attribute8                   ,
3406                                 n_attribute9                   ,
3407                                 n_attribute10
3408                         FROM    mtl_serial_numbers
3409                         WHERE   lpn_id                = p_lpn_id
3410                             AND inventory_item_id     = p_item_id
3411                             AND NVL(revision, '#%^')  = NVL(p_rev, '#%^')
3412                             AND NVL(lot_number, -999) = NVL(p_lot, -999)
3413                             AND ROWNUM               <= l_pr_qty
3414                             AND
3415                                 (
3416                                         (
3417                                             p_entire_lpn = 'Y'
3418                                             AND
3419                                                 (
3420                                                         group_mark_id IS NULL
3421                                                      OR group_mark_id >= -1
3422                                                 )
3423                                         )
3424                                      OR
3425                                         (
3426                                             p_entire_lpn  = 'N'
3427                                             AND group_mark_id = 1
3428                                         )
3429                                      OR
3430                                         (
3431                                             p_process_serial_flag = 'N'
3432                                             AND p_entire_lpn          = 'N'
3433                                             AND group_mark_id IS NULL
3434                                         )
3435                                 )
3436                                 -- Bug# 2772676
3437                                 -- For WIP completions, there is a specific serial
3438                                 -- tied to the MOL/MMTT line which we have to use
3439                             AND
3440                                 (
3441                                         (
3442                                             l_transaction_source_type_id = 5
3443                                             AND serial_number               IN
3444                                                 (
3445                                                         SELECT  fm_serial_number
3446                                                         FROM    wip_lpn_completions_serials
3447                                                         WHERE   header_id = l_ref_id
3448                                                 )
3449                                         )
3450                                      OR l_transaction_source_type_id <> 5
3451                                 );
3452     -- ER 7307189 changes end
3453 
3454 
3455     --Cursors and Variables for FP-J Lot/Serial Support enhancement
3456     CURSOR c_rcv_ser_csr IS
3457       SELECT serial_number
3458 	   , serial_number to_serial_number
3459            , vendor_serial_number
3460            , vendor_lot_number
3461            , parent_serial_number
3462            , origination_date
3463            , end_item_unit_number
3464            , territory_code
3465            , time_since_new
3466            , cycles_since_new
3467            , time_since_overhaul
3468            , cycles_since_overhaul
3469            , time_since_repair
3470            , cycles_since_repair
3471            , time_since_visit
3472            , cycles_since_visit
3473            , time_since_mark
3474            , cycles_since_mark
3475            , number_of_repairs
3476            , serial_attribute_category
3477            , c_attribute1
3478            , c_attribute2
3479            , c_attribute3
3480            , c_attribute4
3481            , c_attribute5
3482            , c_attribute6
3483            , c_attribute7
3484            , c_attribute8
3485            , c_attribute9
3486            , c_attribute10
3487            , c_attribute11
3488            , c_attribute12
3489            , c_attribute13
3490            , c_attribute14
3491            , c_attribute15
3492            , c_attribute16
3493            , c_attribute17
3494            , c_attribute18
3495            , c_attribute19
3496            , c_attribute20
3497            , d_attribute1
3498            , d_attribute2
3499            , d_attribute3
3500            , d_attribute4
3501            , d_attribute5
3502            , d_attribute6
3503            , d_attribute7
3504            , d_attribute8
3505            , d_attribute9
3506            , d_attribute10
3507            , n_attribute1
3508            , n_attribute2
3509            , n_attribute3
3510            , n_attribute4
3511            , n_attribute5
3512            , n_attribute6
3513            , n_attribute7
3514            , n_attribute8
3515            , n_attribute9
3516            , n_attribute10
3517 	FROM   mtl_serial_numbers msn
3518       WHERE  msn.lpn_id = p_lpn_id
3519       AND    msn.inventory_item_id = p_item_id
3520       AND    ((p_rev IS NOT NULL AND msn.revision = p_rev)
3521               OR (p_rev IS NULL AND msn.revision IS NULL))
3522       AND    ((p_lot IS NOT NULL AND msn.lot_number = p_lot)
3523               OR (p_lot IS NULL AND msn.lot_number IS NULL))
3524       AND    ROWNUM <= l_pr_qty
3525       AND    (
3526               (p_entire_lpn = 'Y'
3527                AND(msn.group_mark_id IS NULL OR msn.group_mark_id = -1)
3528               )
3529               OR(p_entire_lpn = 'N' AND group_mark_id = 1)
3530               OR(p_process_serial_flag = 'N'
3531                  AND p_entire_lpn = 'N'
3532                  AND group_mark_id IS NULL)
3533 	      )
3534      ORDER BY msn.lot_number, msn.serial_number;
3535 
3536      --Cursors and Variables for FP-J Lot/Serial Support enhancement
3537      CURSOR c_msni_ser_csr(v_prod_txn_id NUMBER) IS
3538       SELECT fm_serial_number serial_number
3539 	   , to_serial_number to_serial_number
3540            , vendor_serial_number
3541            , vendor_lot_number
3542            , parent_serial_number
3543            , origination_date
3544            , NULL end_item_unit_number
3545            , territory_code
3546            , time_since_new
3547            , cycles_since_new
3548            , time_since_overhaul
3549            , cycles_since_overhaul
3550            , time_since_repair
3551            , cycles_since_repair
3552            , time_since_visit
3553            , cycles_since_visit
3554            , time_since_mark
3555            , cycles_since_mark
3556            , number_of_repairs
3557            , serial_attribute_category
3558            , c_attribute1
3559            , c_attribute2
3560            , c_attribute3
3561            , c_attribute4
3562            , c_attribute5
3563            , c_attribute6
3564            , c_attribute7
3565            , c_attribute8
3566            , c_attribute9
3567            , c_attribute10
3568            , c_attribute11
3569            , c_attribute12
3570            , c_attribute13
3571            , c_attribute14
3572            , c_attribute15
3573            , c_attribute16
3574            , c_attribute17
3575            , c_attribute18
3576            , c_attribute19
3577            , c_attribute20
3578            , d_attribute1
3579            , d_attribute2
3580            , d_attribute3
3581            , d_attribute4
3582            , d_attribute5
3583            , d_attribute6
3584            , d_attribute7
3585            , d_attribute8
3586            , d_attribute9
3587            , d_attribute10
3588            , n_attribute1
3589            , n_attribute2
3590            , n_attribute3
3591            , n_attribute4
3592            , n_attribute5
3593            , n_attribute6
3594            , n_attribute7
3595            , n_attribute8
3596            , n_attribute9
3597            , n_attribute10
3598 	FROM mtl_serial_numbers_interface msni
3599 	-- Bug# 3281512 - Performance Fixes
3600 	-- Since this cursor is only used for receiving, the product code
3601 	-- will always be 'RCV'.  This is needed in order to use the index.
3602 	WHERE msni.product_code = 'RCV'
3603 	AND msni.product_transaction_id = v_prod_txn_id;
3604 
3605     l_parent_txn_id           NUMBER;
3606     l_mol_lot_number          mtl_lot_numbers.lot_number%TYPE;
3607     l_result                  BOOLEAN := TRUE;
3608     l_serial_rec              msn_attribute_rec_tp;
3609     l_operation_plan_id       NUMBER;
3610     l_ser_mismatch_count      NUMBER;
3611     l_msnt_temp_id            NUMBER;
3612     l_product_transaction_id  NUMBER;
3613     l_drop_sub_type           NUMBER;
3614     l_mo_line_id              NUMBER;
3615     l_is_crossdocked          BOOLEAN := FALSE;
3616     l_fm_serial               VARCHAR2(30);
3617     l_to_serial               VARCHAR2(30);
3618 
3619     l_cost_group_id           NUMBER; -- BUG 4134432
3620     l_mmtt_id                 NUMBER; -- for DBI wms_exception and wdth link fix in J
3621     l_process_flag            VARCHAR2(5); -- nsinghi. Added for GME-WMS Integration
3622   BEGIN
3623     IF (l_debug = 1) THEN
3624       mydebug('complete_putaway: in complete putaway');
3625     END IF;
3626 
3627     /*nsinghi GME-WMS Integration Start. Determine if Process Org*/
3628 
3629     SELECT NVL(process_enabled_flag, 'N') INTO l_process_flag
3630     FROM mtl_parameters WHERE organization_id = p_org_id;
3631 
3632     /*nsinghi GME-WMS Integration End. Determine if Process Org*/
3633 
3634     -- Bug# 2744170
3635     -- Set the savepoint
3636     SAVEPOINT complete_putaway_sp;
3637     l_qty := p_qty;
3638     x_return_status := fnd_api.g_ret_sts_success;
3639 
3640     IF l_to_lpn_id = 0 THEN
3641       l_to_lpn_id := NULL;
3642     END IF;
3643 
3644 
3645     IF (l_debug = 1) THEN
3646       mydebug('complete_putaway: complete_putaway API 30');
3647     END IF;
3648 
3649     SELECT task_id
3650     INTO   l_task_id
3651     FROM   wms_dispatched_tasks
3652     WHERE  transaction_temp_id = p_temp_id
3653     AND    ROWNUM < 2;             -- prevent exception
3654                        --Bug 2127361 fix
3655 
3656     IF (l_debug = 1) THEN
3657       mydebug('complete_putaway: complete_putaway API');
3658     END IF;
3659 
3660     SELECT mol.REFERENCE
3661          , mol.reference_type_code
3662          , mol.reference_id
3663          , mol.backorder_delivery_detail_id
3664          , mmtt.subinventory_code
3665          , mmtt.locator_id
3666          , mmtt.transaction_action_id
3667          , mmtt.transfer_subinventory
3668          , mmtt.transfer_to_location
3669          , mmtt.transaction_header_id
3670          , mol.transaction_source_type_id
3671          , mol.inspection_status
3672          , mol.lpn_id
3673          , mmtt.transaction_uom
3674          , mmtt.transaction_quantity
3675          , mmtt.wip_supply_type
3676          , mol.crossdock_type
3677          , mol.txn_source_id
3678          , mol.lot_number
3679          , mol.line_id
3680          , mmtt.operation_plan_id
3681          , mmtt.transaction_type_id
3682          , mmtt.secondary_transaction_quantity --OPM Convergence
3683          , mol.to_cost_group_id --BUG 4134432
3684          , mmtt.secondary_uom_code  --OPM Convergence
3685 
3686     INTO   l_ref
3687          , l_ref_type
3688          , l_ref_id
3689          , l_del_detail_id
3690          , l_org_sub
3691          , l_org_loc
3692          , l_txn_action_id
3693          , l_dest_sub
3694          , l_dest_loc_id
3695          , l_orig_txn_header_id
3696          , l_transaction_source_type_id
3697          , l_inspection_status
3698          , l_mo_lpn_id
3699          , l_orig_txn_uom
3700          , l_orig_qty
3701          , l_wip_supply_type
3702          , l_crossdock_type
3703          , l_parent_txn_id
3704          , l_mol_lot_number
3705          , l_mo_line_id
3706          , l_operation_plan_id
3707          , l_txn_type_id
3708          , l_secondary_qty --OPM Convergence
3709 
3710          , l_cost_group_id -- BUG 4134432
3711          , l_secondary_uom_code --OPM Convergence
3712 
3713     FROM   mtl_txn_request_lines mol
3714          , mtl_material_transactions_temp mmtt
3715     WHERE  mmtt.transaction_temp_id = p_temp_id
3716     AND    mmtt.move_order_line_id = mol.line_id;
3717 
3718     IF (l_debug = 1) THEN
3719       mydebug('complete_putaway: complete_putaway API 40');
3720       mydebug('l_org_sub ' || l_org_sub || ' l_org_loc ' || l_org_loc);
3721       mydebug('p_sub ' || p_sub || ' p_loc ' || p_loc);
3722       mydebug('l_DEST_sub ' || l_dest_sub || ' l_dest_loc ' || l_dest_loc_id);
3723       mydebug('l_crossdock_type : ' || l_crossdock_type);
3724       mydebug('l_transaction_source_type_id: ' || l_transaction_source_type_id);
3725       mydebug('l_del_detail_id: ' || l_del_detail_id);
3726       mydebug('l_wip_supply_type: ' || l_wip_supply_type);
3727       mydebug('l_parent_txn_id: ' || l_parent_txn_id);
3728       mydebug('l_mol_lot_number: ' || l_mol_lot_number);
3729       mydebug('l_operation_plan_id: ' || l_operation_plan_id);
3730       mydebug('l_secondary_qty ' || l_secondary_qty);
3731       mydebug('l_secondary_uom_code ' || l_secondary_uom_code);
3732     END IF;
3733 
3734     -- Bug# 2672785: Populate the emp id instead of user id while logging an exception
3735     -- Bug# 2750060: Catch for no data found exception
3736     BEGIN
3737       SELECT employee_id
3738       INTO   l_emp_id
3739       FROM   fnd_user
3740       WHERE  user_id = p_user_id;
3741     EXCEPTION
3742       WHEN NO_DATA_FOUND THEN
3743         IF (l_debug = 1) THEN
3744           mydebug('complete_putaway: There is no employee tied to the user');
3745         END IF;
3746         l_emp_id := NULL;
3747     END;
3748 
3749        /* FP-J Lot/Serial Support Enhancement
3750      * Moved this SQL upward because based on the LPN context, we need
3751      * to open a new cursor for a receiving LPN if INV and PO patch levels
3752      * are J or higher
3753      */
3754     -- Check to see if its a receipt LPN
3755     SELECT lpn_context
3756     INTO   l_lpn_context
3757     FROM   wms_license_plate_numbers
3758     WHERE  lpn_id = p_lpn_id;
3759 
3760     IF l_lpn_context = 3 THEN
3761        IF (l_debug =1) THEN
3762 	  mydebug('RCV LPN: Always generate new txn header id');
3763        END IF;
3764 
3765        SELECT mtl_material_transactions_s.NEXTVAL
3766 	 INTO   l_txn_header_id
3767 	 FROM   DUAL;
3768      ELSE
3769        IF (l_debug =1) THEN
3770 	  mydebug('NON RCV LPN. p_new_txn_header_id: '||p_new_txn_header_id);
3771        END IF;
3772 
3773        -- IN case of Resides in Inventroy LPNs use the txn header id generated in wrapper
3774        l_txn_header_id := p_new_txn_header_id;
3775     END IF;
3776 
3777     -- ATF_J3
3778     -- Before J, Customer used entering 0 quantity as a workaround to skip a task
3779     -- because they could not specify which item to putaway prior to
3780     -- patchset J.
3781     -- In patchset J, with item based putaway, user should not need to
3782     -- enter 0 quantity to skip a task.
3783 
3784     IF l_qty <= 0 THEN
3785       IF (l_debug = 1) THEN
3786         mydebug('complete_putaway: user confirmed 0 qty.');
3787       END IF;
3788 
3789       IF (l_debug = 1) THEN
3790 	 mydebug('complete_putaway: Current release is above J. Disallow 0 quantity.');
3791       END IF;
3792       fnd_message.set_name('INV', 'INV_QTY_MUST_EXCEED_ZERO');
3793       fnd_msg_pub.ADD;
3794 
3795       RAISE fnd_api.g_exc_error;
3796     END IF; --IF l_qty <= 0 THEN
3797 
3798     IF (l_debug = 1) THEN
3799       mydebug('complete_putaway: complete_putaway API 60');
3800     END IF;
3801 
3802     -- Check to see if UOM has been changed. If yes, update MMTT
3803     IF p_uom <> l_orig_txn_uom
3804        AND l_orig_qty = l_qty THEN
3805       IF (l_debug = 1) THEN
3806         mydebug('complete_putaway: Uom has changed - need to update MMTT');
3807         mydebug('complete_putaway: complete_putaway API 70');
3808       END IF;
3809 
3810       l_qty := inv_convert.inv_um_convert(
3811           item_id           => p_item_id
3812         , PRECISION         => NULL
3813         , from_quantity     => l_qty
3814         , from_unit         => p_uom
3815         , to_unit           => l_orig_txn_uom
3816         , from_name         => NULL
3817         , to_name           => NULL
3818         );
3819 
3820       UPDATE mtl_material_transactions_temp
3821       SET transaction_uom = p_uom
3822         , transaction_quantity = l_qty
3823       WHERE  transaction_temp_id = p_temp_id;
3824     END IF;
3825 
3826     -- Update loc and zone info, if different
3827     IF l_txn_action_id = 2 THEN
3828       -- Subtransfer , putaway of lpn already in inv
3829       l_lpn_sub := l_dest_sub;
3830       l_lpn_loc := l_dest_loc_id;
3831 
3832       IF (p_loc <> l_dest_loc_id)
3833          OR(p_sub <> l_dest_sub) THEN
3834         IF (l_debug = 1) THEN
3835           mydebug('complete_putaway: Updating transfer sub  and loc');
3836         END IF;
3837 
3838         UPDATE  mtl_material_transactions_temp
3839         SET     transfer_subinventory = p_sub
3840               , transfer_to_location = p_loc
3841         WHERE  transaction_temp_id = p_temp_id;
3842       END IF;
3843     ELSE
3844       -- Normal Putaway
3845       l_lpn_sub := l_org_sub;
3846       l_lpn_loc := l_org_loc;
3847 
3848       IF (p_loc <> l_org_loc)
3849          OR(p_sub <> l_org_sub) THEN
3850         IF (l_debug = 1) THEN
3851           mydebug('complete_putaway: Updating sub and loc');
3852         END IF;
3853 
3854         IF l_lpn_context = 3 THEN
3855 
3856           IF (l_debug = 1) THEN
3857             mydebug('complete_putaway: RCV LPN, No updates of sub and loc for receving lpn');
3858           END IF;
3859         ELSE
3860 
3861           UPDATE mtl_material_transactions_temp
3862           SET subinventory_code = p_sub
3863             , locator_id = p_loc
3864           WHERE  transaction_temp_id = p_temp_id;
3865         END IF; -- END IF l_lpn_context = 3 THEN
3866       END IF;   --END IF check txn action
3867     END IF;   --END IF check sub/loc discrepancy
3868 
3869     -- Check to see if to lpn exists. if not, create it
3870     IF p_to_lpn = fnd_api.g_miss_char
3871        OR p_to_lpn IS NULL
3872        OR p_to_lpn = '' THEN
3873       l_to_lpn_id := NULL;
3874     ELSE
3875       IF (l_debug = 1) THEN
3876         mydebug('complete_putaway: To LPN was passed');
3877       END IF;
3878 
3879       l_exist_lpn := 0;
3880 
3881       BEGIN
3882         SELECT 1
3883         INTO   l_exist_lpn
3884         FROM   DUAL
3885         WHERE  EXISTS(SELECT 1
3886                       FROM   wms_license_plate_numbers
3887                       WHERE  license_plate_number = p_to_lpn
3888                       AND    organization_id = p_org_id);
3889       EXCEPTION
3890         WHEN NO_DATA_FOUND THEN
3891           l_exist_lpn := 0;
3892       END;
3893 
3894       IF (l_exist_lpn = 0) THEN
3895         -- LPN does not exist, create it
3896         -- Call Suresh's Create LPN API
3897         wms_container_pub.create_lpn(
3898           p_api_version           => 1.0
3899         , p_init_msg_list         => fnd_api.g_false
3900         , p_commit                => fnd_api.g_false
3901         , x_return_status         => x_return_status
3902         , x_msg_count             => x_msg_count
3903         , x_msg_data              => x_msg_data
3904         , p_lpn                   => p_to_lpn
3905         , p_organization_id       => p_org_id
3906         , p_container_item_id     => NULL
3907         , p_lot_number            => NULL
3908         , p_revision              => NULL
3909         , p_serial_number         => NULL
3910         , p_subinventory          => l_lpn_sub
3911         , p_locator_id            => l_lpn_loc
3912         , p_source                => 1
3913         , p_cost_group_id         => NULL
3914         , x_lpn_id                => l_to_lpn_id
3915         );
3916         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3917 
3918         IF (x_msg_count = 0) THEN
3919           IF (l_debug = 1) THEN
3920             mydebug('complete_putaway: Successful');
3921           END IF;
3922         ELSIF(x_msg_count = 1) THEN
3923           IF (l_debug = 1) THEN
3924             mydebug('complete_putaway: Not Successful');
3925             mydebug('complete_putaway: ' || REPLACE(x_msg_data, fnd_global.local_chr(0), ' '));
3926           END IF;
3927         ELSE
3928           IF (l_debug = 1) THEN
3929             mydebug('complete_putaway: Not Successful2');
3930           END IF;
3931 
3932           FOR i IN 1 .. x_msg_count LOOP
3933             x_msg_data := fnd_msg_pub.get(i, 'F');
3934 
3935             IF (l_debug = 1) THEN
3936               mydebug('complete_putaway: ' || REPLACE(x_msg_data, fnd_global.local_chr(0), ' '));
3937             END IF;
3938           END LOOP;
3939         END IF;
3940 
3941         IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
3942           fnd_message.set_name('WMS', 'WMS_TD_CREATE_LPN_ERROR');
3943           fnd_msg_pub.ADD;
3944           RAISE fnd_api.g_exc_unexpected_error;
3945         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
3946           fnd_message.set_name('WMS', 'WMS_TD_CREATE_LPN_ERROR');
3947           fnd_msg_pub.ADD;
3948           RAISE fnd_api.g_exc_error;
3949         END IF;
3950       ELSE
3951         -- LPN exists. Get LPN ID
3952         SELECT lpn_id
3953         INTO   l_to_lpn_id
3954         FROM   wms_license_plate_numbers
3955         WHERE  license_plate_number = p_to_lpn
3956         AND    organization_id = p_org_id;
3957       END IF;
3958     END IF;
3959 
3960     IF (l_debug = 1) THEN
3961       mydebug('complete_putaway: After LPN creation');
3962     END IF;
3963 
3964     SELECT lot_control_code
3965          , serial_number_control_code
3966     INTO   l_lot_code
3967          , l_serial_code
3968     FROM   mtl_system_items
3969     WHERE  organization_id = p_org_id
3970     AND    inventory_item_id = p_item_id;
3971 
3972 
3973     -- Bug 3405320
3974     -- If serial_code  is 6 then check if serials exists for that item
3975     -- inside the LPN
3976     --4049874
3977     IF l_serial_code = 6 THEN
3978        SELECT count(1)
3979 	 INTO   l_is_msni_req
3980 	 FROM   mtl_serial_numbers
3981 	 WHERE  inventory_item_id = p_item_id
3982 	 AND    lpn_id = p_lpn_id
3983 	 AND    current_status = 7
3984 	 AND    current_organization_id = p_org_id;
3985     END IF;
3986 
3987     --If the line is crossdocked to a sales order or WIP job, set the flag
3988     IF (l_del_detail_id IS NOT NULL) THEN
3989       l_is_crossdocked := TRUE;
3990     ELSE
3991       l_is_crossdocked := FALSE;
3992     END IF;
3993 
3994     -- Do Lot and serial stuff only if there was no discrepancy
3995     IF (p_disc = 'N') THEN
3996       -- Calculate Primary Quantity
3997       l_pr_qty := wms_task_dispatch_gen.get_primary_quantity(
3998               p_item_id         => p_item_id
3999             , p_organization_id => p_org_id
4000             , p_from_quantity   => l_orig_qty
4001             , p_from_unit       => l_orig_txn_uom); --Bug 5225012. Passing correct UOM
4002                                                     --Earlier p_uom was being passed.
4003 
4004       IF (l_debug = 1) THEN
4005         mydebug('complete_putaway: primary qty: ' || l_pr_qty);
4006       END IF;
4007 
4008       IF l_lot_code > 1 THEN
4009         IF (l_debug = 1) THEN
4010           mydebug('complete_putaway: Inserting Lots');
4011         END IF;
4012 
4013         l_ser_seq := NULL;
4014 
4015         -- Bug 2458540
4016         -- IF l_serial_code>1 AND l_serial_code<>6  THEN
4017         --4049874
4018         IF ((l_serial_code > 1 AND l_serial_code <> 6)
4019             OR(l_serial_code = 6 AND
4020                                     (l_ref = 'ORDER_LINE_ID'
4021                                      OR l_ref = 'SHIPMENT_LINE_ID'
4022                                     )
4023               )
4024             ) THEN
4025           -- Need to get the next value for serial temp id
4026           SELECT mtl_material_transactions_s.NEXTVAL
4027           INTO   l_ser_seq
4028           FROM   DUAL;
4029 
4030           IF (l_debug = 1) THEN
4031             mydebug('complete_putaway: Lot and serial controlled item: ' || l_ser_seq);
4032           END IF;
4033         END IF;
4034 
4035         /* FP-J Lot/Serial Support Enhancement
4036          * Do not update MTL_TRANSACTION_LOTS_TEMP  from here if the LPN Resides in
4037          * Receiving and WMS and PO patch levels are J or higher
4038          * For other LPN contexts and if patch levels are lower than J,
4039          * continue with the updates
4040          */
4041         IF (l_lpn_context = 3) THEN
4042           IF (l_is_crossdocked = FALSE) THEN
4043             IF l_debug = 1 THEN
4044               mydebug('complete_putaway: LPN Resides in Receiving. No updates to MTLT from here.');
4045             END IF;
4046           ELSE
4047             UPDATE mtl_transaction_lots_temp
4048             SET    serial_transaction_temp_id = l_ser_seq
4049                  , last_update_date = SYSDATE
4050                  , last_updated_by = p_user_id
4051             WHERE  transaction_temp_id = p_temp_id
4052             AND    lot_number = p_lot;
4053           END IF;
4054 
4055 	--INV/WIP LPN
4056         ELSE
4057           IF l_debug = 1 THEN
4058             mydebug('complete_putaway: INV/WIP LPN. Update MTLT.');
4059           END IF;
4060 
4061           UPDATE mtl_transaction_lots_temp
4062           SET transaction_quantity = l_orig_qty
4063             , primary_quantity = l_pr_qty
4064             , serial_transaction_temp_id = l_ser_seq
4065             , last_update_date = SYSDATE
4066             , last_updated_by = p_user_id
4067           WHERE  transaction_temp_id = p_temp_id
4068           AND    lot_number = p_lot;
4069         END IF;
4070 
4071         -- Capture lot info for WIP transactions
4072 
4073         /* nsinghi - GME-WMS Integration. Added additional check for l_process_flag.
4074         Update Lot attrs from wip_lpn_completions_lots table only for discrete orgs. */
4075 
4076         IF (l_transaction_source_type_id = 5 AND l_process_flag = 'N') THEN
4077           IF (l_debug = 1) THEN
4078             mydebug('complete_putaway: Capture lot atts from wip tables');
4079             mydebug('p_ref_id: ==> ' || l_ref_id);
4080             mydebug('p_temp_id: => ' || p_temp_id);
4081             mydebug('p_lot: =====> ' || p_lot);
4082           END IF;
4083 
4084           wms_wip_integration.capture_lot_atts(
4085               p_ref_id  =>  l_ref_id,
4086               p_temp_id =>  p_temp_id,
4087               p_lot     =>  p_lot);
4088 
4089 	 --Start Bug 6356147
4090 	 -- Update MMTT for WIP flow completions
4091           SELECT DECODE(wip_entity_type, 4, 'Y', 'N')
4092           INTO l_flow_schedule
4093           FROM   wip_lpn_completions
4094           WHERE  header_id = l_ref_id;
4095 
4096 	 IF (l_debug = 1) THEN
4097             mydebug('l_flow_schedule  => ' || l_flow_schedule);
4098           END IF;
4099 
4100 	  UPDATE mtl_material_transactions_temp
4101           SET flow_schedule = l_flow_schedule
4102           WHERE  transaction_temp_id = p_temp_id;
4103 	  --End Bug 6356147
4104         ELSE
4105 	   /*
4106            * If the item is lot controlled, we do not need temp records for
4107            * processing but the interface records MTLI. So we can create the
4108            * MTLI record here for this quantity
4109            */
4110           IF (l_lpn_context = 3) THEN
4111 
4112             IF l_product_transaction_id IS NULL THEN
4113               SELECT  rcv_transactions_interface_s.NEXTVAL
4114               INTO    l_product_transaction_id
4115               FROM    sys.dual;
4116             END IF;
4117 
4118             l_result := TRUE;
4119 
4120             --Call the helper routine to create the MTLI record
4121             l_result := insert_mtli_helper(
4122                   p_txn_if_id       =>  l_dup_temp_id
4123                 , p_lot_number      =>  p_lot
4124                 , p_txn_qty         =>  p_qty --Bug 5225012. Earlier l_orig_qty was being passed
4125                 , p_prm_qty         =>  l_pr_qty
4126                 , p_item_id         =>  p_item_id
4127                 , p_org_id          =>  p_org_id
4128                 , x_serial_temp_id  =>  l_dup_ser_temp_id
4129                 , p_product_txn_id  =>  l_product_transaction_id
4130                 , p_temp_id         =>  p_temp_id
4131                 , p_secondary_quantity => p_secondary_quantity --OPM Convergence
4132                 , p_secondary_uom   =>  p_secondary_uom);   --OPM Convergence
4133             IF NOT l_result THEN
4134               IF (l_debug = 1) THEN
4135               mydebug('complete_putaway: Failure while Inserting MSNI records - lot and serial controlled item');
4136               END IF;
4137               RAISE fnd_api.g_exc_unexpected_error;
4138             END IF;   --END IF check l_result
4139 
4140             IF (l_debug = 1) THEN
4141               mydebug('complete_putaway: Inserted MTLI for lot and serial item. intf_txn_id: ' || l_dup_temp_id ||
4142                 ', ser_temp_id : ' || l_dup_ser_temp_id || ' , prod_txn_id: ' || l_product_transaction_id);
4143             END IF;
4144           END IF;   --END IF (l_lpn_context = 3) THEN
4145         END IF;   --END IF l_transaction_source_type_id = 5
4146 
4147         IF (l_debug = 1) THEN
4148           mydebug('complete_putaway: SNCODE:' || l_serial_code);
4149         END IF;
4150 
4151         -- Bug 2458540
4152         -- IF l_serial_code>1 AND l_serial_code<>6  THEN
4153         --4049874 : removed the l_wms_po_j_or_higher check
4154         IF ((l_serial_code > 1  AND l_serial_code <> 6)
4155             OR (l_serial_code = 6 AND l_ref = 'ORDER_LINE_ID')
4156             OR (l_serial_code = 6 AND  l_ref = 'SHIPMENT_LINE_ID'
4157                 AND l_is_msni_req > 0
4158               )
4159            ) THEN
4160           IF (l_debug = 1) THEN
4161             mydebug('complete_putaway: Inserting Serials');
4162           END IF;
4163 
4164 	  -- ER 7307189 changes start
4165 
4166 		SELECT  COUNT(msn.group_mark_id)
4167 		INTO    res_count
4168 		FROM    mtl_serial_numbers msn ,
4169 			mtl_reservations mr
4170 		WHERE   mr.lpn_id        =msn.lpn_id
4171 		    AND msn.group_mark_id=mr.reservation_id
4172 		    AND msn.lpn_id       = p_lpn_id;
4173 
4174 		mydebug(' complete_putaway : res_count value ' || res_count);
4175 
4176 	   -- ER 7307189 changes end
4177           /*
4178            * If the MOL LPN context is "RESIDES IN RECEIVING" and INV and PO Patch
4179            * levels are J or higher, then open the cursor c_rcv_ser_csr that
4180            * fetches the serial by matches the serials in rcv_serials_supply for
4181            * the parent transaction.
4182            * If either of these is not true, then open the original cursor
4183            */
4184           IF (l_lpn_context = 3) THEN
4185             OPEN c_rcv_ser_csr;
4186           ELSE
4187 	        -- ER 7307189 changes start
4188 		IF ( res_count > 0 ) THEN
4189 			OPEN ser_csr_reserved_lpn;
4190 		ELSE
4191 			OPEN ser_csr;
4192 		END IF;
4193 		-- ER 7307189 changes end
4194           END IF;
4195 
4196           LOOP
4197 	    IF (l_lpn_context = 3) THEN
4198               FETCH c_rcv_ser_csr INTO l_serial_rec;
4199               EXIT WHEN c_rcv_ser_csr%NOTFOUND;
4200             ELSE
4201 	        -- ER 7307189 changes start
4202 		IF ( res_count > 0 ) THEN
4203 			FETCH   ser_csr_reserved_lpn INTO    l_serial_rec;
4204 			EXIT WHEN ser_csr_reserved_lpn%NOTFOUND;
4205 		ELSE
4206 			FETCH   ser_csr	INTO    l_serial_rec;
4207 			EXIT WHEN ser_csr%NOTFOUND;
4208 		END IF;
4209 		-- ER 7307189 changes end
4210 
4211             END IF;
4212 
4213             l_serial := l_serial_rec.serial_number;
4214 
4215             IF (l_debug = 1) THEN
4216               mydebug('complete_putaway: SN:' || l_serial);
4217             END IF;
4218 
4219             --Since we would not be exploding the content LPN in the inventory TM
4220             --we have to create the MSNT records from here for the TM to process
4221             --This is applicable for putaway of an entire INV/WIP LPN when
4222             --WMS and PO J are installed
4223             IF ( (p_entire_lpn = 'N') OR
4224                  (l_txn_action_id IN(27, 12)) OR
4225                  (p_entire_lpn = 'Y' AND l_lpn_context IN (1,2))
4226               ) THEN
4227               -- Capture serial info for WIP transactions
4228         /* nsinghi - GME-WMS Integration. Added additional check for l_process_flag.
4229         Update Serial attrs from wip_lpn_completions_serials table only for discrete orgs. */
4230 
4231               IF (l_transaction_source_type_id = 5 AND l_process_flag = 'N') THEN
4232                 IF (l_debug = 1) THEN
4233                   mydebug('complete_putaway: Capture serial atts from wip tables');
4234                   mydebug('p_ref_id: ===========> ' || l_ref_id);
4235                   mydebug('p_temp_id: ==========> ' || p_temp_id);
4236                   mydebug('p_fm_serial_number: => ' || l_serial);
4237                   mydebug('p_to_serial_number: => ' || l_serial);
4238                   mydebug('p_serial_temp_id: ===> ' || l_ser_seq);
4239                   mydebug('p_serial_flag: ======> ' || 2);
4240                 END IF;
4241 
4242                 wms_wip_integration.capture_serial_atts(
4243                   p_ref_id               => l_ref_id
4244                 , p_temp_id              => p_temp_id
4245                 , p_last_update_date     => SYSDATE
4246                 , p_last_updated_by      => p_user_id
4247                 , p_creation_date        => SYSDATE
4248                 , p_created_by           => p_user_id
4249                 , p_fm_serial_number     => l_serial
4250                 , p_to_serial_number     => l_serial
4251                 , p_serial_temp_id       => l_ser_seq
4252                 , p_serial_flag          => 2
4253                 );
4254               ELSE
4255                 l_result := TRUE;
4256                 /*
4257                  * If INV and PO patch levels are J or higher, we will create
4258                  * create one MSNI record for each serial fetched above and
4259                  * link it to the MTLI record created above. No Temp records
4260                  */
4261                 IF (l_lpn_context = 3) THEN
4262                   IF (l_debug = 1) THEN
4263                     mydebug('complete_putaway: Inserting MSNI - lot and serial item');
4264                   END IF;
4265                   --Call the helper routine to create the MSNI record
4266                   l_result := insert_msni_helper(
4267                         p_txn_if_id       =>  l_dup_ser_temp_id
4268                       , p_serial_number   =>  l_serial
4269                       , p_org_id          =>  p_org_id
4270                       , p_item_id         =>  p_item_id
4271                       , p_product_txn_id  =>  l_product_transaction_id
4272                       );
4273 
4274                   IF NOT l_result THEN
4275                     IF (l_debug = 1) THEN
4276                     mydebug('complete_putaway: Failure while Inserting MSNI records - lot and serial controlled item');
4277                     END IF;
4278                     RAISE fnd_api.g_exc_unexpected_error;
4279                   END IF;   --END IF check l_result
4280 
4281                   IF (l_debug = 1) THEN
4282                     mydebug('complete_putaway: Inserted MSNI for lot and serial item. intf_txn_id: '
4283                       || l_dup_ser_temp_id || ' , prod_txn_id: ' || l_product_transaction_id);
4284                   END IF;
4285 
4286                   --If the line is crossdocked, create MSNT also
4287                   IF (l_is_crossdocked) THEN
4288                     IF (l_debug = 1) THEN
4289                       mydebug('complete_putaway: Inserting MSNT for xdock - lot and serial item' || l_ser_seq);
4290                     END IF;
4291                     --Create one MSNT record for each serial number returned by
4292                     --the cursor
4293                     l_result := insert_msnt_rec(
4294                           p_transaction_temp_id =>  l_ser_seq
4295                         , p_serial_number       =>  l_serial
4296                         , p_serial_atts         =>  l_serial_rec
4297                         , p_user_id             =>  p_user_id);
4298 
4299                     IF NOT l_result THEN
4300                       IF (l_debug = 1) THEN
4301                         mydebug('complete_putaway: Failure while creating xdock MSNT - lot and serial');
4302                       END IF;
4303                       RAISE fnd_api.g_exc_unexpected_error;
4304                     END IF;   --END IF check l_result
4305                   END IF;  --END IF insert MSNT for xdock
4306 
4307 		--LPN is not receiving LPN
4308                 ELSE
4309                   IF (l_debug = 1) THEN
4310                     mydebug('complete_putaway: Inserting MSNT - lot and serial item' || l_ser_seq);
4311                   END IF;
4312                   --Create one MSNT record for each serial number returned by
4313                   --the cursor
4314                   l_result := insert_msnt_rec(
4315                         p_transaction_temp_id =>  l_ser_seq
4316                       , p_serial_number       =>  l_serial
4317                       , p_serial_atts         =>  l_serial_rec
4318                       , p_user_id             =>  p_user_id);
4319 
4320                   IF NOT l_result THEN
4321                     IF (l_debug = 1) THEN
4322                       mydebug('complete_putaway: Failure while MSNT');
4323                     END IF;
4324                     RAISE fnd_api.g_exc_unexpected_error;
4325                   END IF;   --END IF check l_result
4326                 END IF;   --END IF (l_lpn_context = 3) THEN
4327               END IF;   --END IF check transaction_source_type = 5clear scr
4328             END IF;   --END IF p_entire_lpn = 'N'
4329 
4330             /*
4331              * Do not update MTL_SERIAL_NUMBERS from here if the LPN Resides in
4332              * Receiving and WMS and PO patch levels are J or higher
4333              * For other LPN contexts and if patch levels are lower than J,
4334              * continue with the updates
4335              */
4336             IF (l_lpn_context = 3) THEN
4337               IF l_debug = 1 THEN
4338                 mydebug('complete_putaway: The LPN resides in Receiving. No updates to MSN from here.');
4339               END IF;
4340             --INV/WIP LPN
4341             ELSE
4342               IF l_debug = 1 THEN
4343                 mydebug('complete_putaway: INV/WIP LPN. Update MSN.');
4344               END IF;
4345 
4346               -- Update MSN's group_mark_id
4347               UPDATE mtl_serial_numbers
4348               SET group_mark_id = p_temp_id
4349               WHERE  serial_number = l_serial
4350               AND    inventory_item_id = p_item_id
4351               AND    lot_number = p_lot;
4352 
4353               -- Bug 2458540
4354               -- Update the Serial Status to 4 before calling Receiving Transaction
4355               -- Processor for RMA serial At SALES ORDER ISSUE STANDARD RECEIPT CASE
4356               IF (l_serial_code = 6 AND l_ref = 'ORDER_LINE_ID') THEN
4357                 UPDATE mtl_serial_numbers
4358                 SET current_status = 4
4359                   , previous_status = current_status
4360                 WHERE  serial_number = l_serial
4361                 AND    inventory_item_id = p_item_id
4362                 AND    lot_number = p_lot;
4363               END IF;
4364             END IF;   --END IF (l_lpn_context = 3) T
4365           END LOOP;   --END IF loop through serials in lot
4366 
4367           IF ser_csr%ISOPEN THEN
4368             CLOSE ser_csr;
4369           END IF;
4370 
4371 	 -- ER 7307189 changes start
4372 	 IF ser_csr_reserved_lpn%ISOPEN THEN
4373 		CLOSE ser_csr_reserved_lpn;
4374 	 END IF;
4375 	 -- ER 7307189 changes end
4376 
4377           IF c_rcv_ser_csr%ISOPEN THEN
4378             CLOSE c_rcv_ser_csr;
4379           END IF;
4380         END IF;   -- End Serial Loop
4381       -- Bug 2458540
4382       -- ELSIF l_serial_code>1 AND l_serial_code<>6 THEN
4383       --not lot controlled
4384       --4049874 : removed the l_wms_po_j_or_higher check
4385       ELSIF((l_serial_code > 1 AND l_serial_code <> 6)
4386             OR ( l_serial_code = 6 AND l_ref = 'ORDER_LINE_ID')
4387             OR ( l_serial_code = 6 AND  l_ref = 'SHIPMENT_LINE_ID'
4388                  AND l_is_msni_req > 0
4389               )
4390            ) THEN
4391         IF (l_debug = 1) THEN
4392           mydebug('complete_putaway: serial item only');
4393         END IF;
4394 
4395 	-- ER 7307189 changes start
4396 
4397 	SELECT  COUNT(msn.group_mark_id)
4398         INTO    res_count
4399         FROM    mtl_serial_numbers msn ,
4400                 mtl_reservations mr
4401         WHERE   mr.lpn_id        =msn.lpn_id
4402               AND msn.group_mark_id=mr.reservation_id
4403               AND msn.lpn_id       = p_lpn_id;
4404 
4405         mydebug(' complete_putaway : res_count value ' || res_count);
4406 
4407        -- ER 7307189 changes end
4408 
4409 
4410         /* FP-J Lot/Serial Support Enhancement
4411          * If the MOL LPN context is "RESIDES IN RECEIVING" and INV and PO Patch
4412          * levels are J or higher, then open the cursor c_rcv_ser_csr that
4413          * fetches the serial by matches the serials in rcv_serials_supply for
4414          * the parent transaction
4415          * If either of these is not true, then open the original cursor
4416          */
4417         IF (l_lpn_context = 3) THEN
4418           OPEN c_rcv_ser_csr;
4419         ELSE
4420 
4421 	  -- ER 7307189 changes start
4422           IF ( res_count > 0 ) THEN
4423                   OPEN ser_csr_reserved_lpn;
4424           ELSE
4425                   OPEN ser_csr;
4426           END IF;
4427 	  -- ER 7307189 changes end
4428 
4429         END IF;
4430 
4431         LOOP
4432           IF (l_lpn_context = 3) THEN
4433             FETCH c_rcv_ser_csr INTO l_serial_rec;
4434             EXIT WHEN c_rcv_ser_csr%NOTFOUND;
4435           ELSE
4436 	    -- ER 7307189 changes start
4437             IF ( res_count > 0 ) THEN
4438                     FETCH   ser_csr_reserved_lpn  INTO    l_serial_rec;
4439                     EXIT WHEN ser_csr_reserved_lpn%NOTFOUND;
4440             ELSE
4441                     FETCH   ser_csr INTO    l_serial_rec;
4442                     EXIT WHEN ser_csr%NOTFOUND;
4443             END IF;
4444 	    -- ER 7307189 changes end
4445           END IF;
4446 
4447           l_serial := l_serial_rec.serial_number;
4448 
4449           /*IF p_entire_lpn = 'N'
4450              OR l_txn_action_id IN(27, 12) THEN*/
4451 
4452           --Since we would not be exploding the content LPN in the inventory TM
4453           --we have to create the MSNT records from here for the TM to process
4454           --This is applicable for putaway of an entire INV/WIP LPN when
4455           --WMS and PO J are installed
4456           IF ( (p_entire_lpn = 'N') OR
4457                  (l_txn_action_id IN(27, 12)) OR
4458                  (p_entire_lpn = 'Y' AND l_lpn_context IN (1,2))
4459               ) THEN
4460             -- Capture serial info for WIP transactions
4461             IF l_transaction_source_type_id = 5 THEN
4462               IF (l_debug = 1) THEN
4463                 mydebug('complete_putaway: Capture serial atts from wip tables');
4464                 mydebug('p_ref_id: ===========> ' || l_ref_id);
4465                 mydebug('p_temp_id: ==========> ' || p_temp_id);
4466                 mydebug('p_fm_serial_number: => ' || l_serial);
4467                 mydebug('p_to_serial_number: => ' || l_serial);
4468                 mydebug('p_serial_temp_id: ===> ' || p_temp_id);
4469                 mydebug('p_serial_flag: ======> ' || 3);
4470               END IF;
4471 
4472               wms_wip_integration.capture_serial_atts(
4473                 p_ref_id               => l_ref_id
4474               , p_temp_id              => p_temp_id
4475               , p_last_update_date     => SYSDATE
4476               , p_last_updated_by      => p_user_id
4477               , p_creation_date        => SYSDATE
4478               , p_created_by           => p_user_id
4479               , p_fm_serial_number     => l_serial
4480               , p_to_serial_number     => l_serial
4481               , p_serial_temp_id       => p_temp_id
4482               , p_serial_flag          => 3
4483               );
4484             ELSE
4485               l_result := TRUE;
4486 
4487               /* FP-J Lot/Serial Support Enhancement
4488                * If the LPN Resides in Receiving and WMS and PO patch levels
4489                * are J or higher, we can create one MSNI record for each serial
4490                * fetched from the cursor. No MSNT records needed here
4491                */
4492               IF (l_lpn_context = 3) THEN
4493                 IF (l_debug = 1) THEN
4494                   mydebug('complete_putaway: Inserting MSNI - serial controlled only' || p_temp_id);
4495                 END IF;
4496 
4497                 IF l_product_transaction_id IS NULL THEN
4498                   SELECT  rcv_transactions_interface_s.NEXTVAL
4499                   INTO    l_product_transaction_id
4500                   FROM    sys.dual;
4501                 END IF;
4502 
4503                 --Call the helper routine to create the MSNI record
4504                 l_result := insert_msni_helper(
4505                       p_txn_if_id       =>  l_dup_temp_id
4506                     , p_serial_number   =>  l_serial
4507                     , p_org_id          =>  p_org_id
4508                     , p_item_id         =>  p_item_id
4509                     , p_product_txn_id  =>  l_product_transaction_id);
4510 
4511                 IF NOT l_result THEN
4512                   IF (l_debug = 1) THEN
4513                   mydebug('complete_putaway: Failure while Inserting MSNI records - serial controlled item');
4514                   END IF;
4515                   RAISE fnd_api.g_exc_unexpected_error;
4516                 END IF;   --END IF check l_result
4517 		IF (l_debug = 1) THEN
4518 		   mydebug('complete_putaway: Inserting MSNT for xdock - serial item' || l_ser_seq);
4519 		END IF;
4520 
4521 		--If the line croosdocked, create one MSNT record for each serial number returned by the cursor
4522                 IF (l_is_crossdocked) THEN
4523 		  l_result := insert_msnt_rec(
4524   		      p_transaction_temp_id =>  p_temp_id
4525 	            , p_serial_number       =>  l_serial
4526 	            , p_serial_atts         =>  l_serial_rec
4527 	           , p_user_id              =>  p_user_id);
4528 
4529 	  	  IF NOT l_result THEN
4530 		    IF (l_debug = 1) THEN
4531 		       mydebug('complete_putaway: Failure while MSNT');
4532 		    END IF;
4533 		    RAISE fnd_api.g_exc_unexpected_error;
4534 		  END IF;   --END IF check l_result
4535                 END IF;  --END IF create MSNT for xdock
4536 
4537 	       --INV/WIP LPN
4538 	       ELSE
4539                 IF (l_debug = 1) THEN
4540                    mydebug('complete_putaway: Inserting MSNT - serial controlled only' || p_temp_id);
4541                    mydebug('complete_putaway p_temp_id:'|| p_temp_id);
4542                    mydebug('complete_putaway l_serial:'|| l_serial);
4543                 END IF;
4544 
4545                  /* Bug 5213359-Added the following check */
4546 
4547                 SELECT count(transaction_temp_id)
4548                 INTO l_exist_msnt
4549                 FROM mtl_serial_numbers_temp
4550                 WHERE transaction_temp_id = p_temp_id
4551                 AND fm_serial_number = l_serial ;
4552 
4553                 mydebug('complete_putaway In patchset J, checking for msnt l_exist_msnt:'|| l_exist_msnt);
4554 
4555                 IF l_exist_msnt = 0 THEN
4556                  --Create one MSNT record for each serial number returned by the cursor
4557                    l_result := insert_msnt_rec(
4558                          p_transaction_temp_id =>  p_temp_id
4559                        , p_serial_number       =>  l_serial
4560                        , p_serial_atts         =>  l_serial_rec
4561                        , p_user_id             =>  p_user_id);
4562                 END IF;
4563 
4564                 /* End of fix for Bug 5213359*/
4565 
4566                 IF NOT l_result THEN
4567                   IF (l_debug = 1) THEN
4568                     mydebug('complete_putaway: Failure while Inserting MSNT - serial controlled only');
4569                   END IF;
4570                   RAISE fnd_api.g_exc_unexpected_error;
4571                 END IF;   --END IF check l_result
4572               END IF;   --END IF (l_lpn_context = 3) THEN
4573             END IF;   --END IF l_transaction_source_type_id = 5
4574           END IF;   --END IF p_entire_lpn = 'N'
4575 
4576           /*
4577            * Do not update MTL_SERIAL_NUMBERS from here if the LPN Resides in
4578            * Receiving and WMS and PO patch levels are J or higher
4579            * For other LPN contexts and if patch levels are lower than J,
4580            * continue with the updates
4581            */
4582           IF (l_lpn_context = 3) THEN
4583             IF l_debug =1 THEN
4584               mydebug('complete_putaway: LPN Resides in Receiving. No updates to MSN from here.');
4585             END IF;
4586           --WMS or PO are lower than J - have to update MSN
4587          ELSE
4588            IF l_debug =1 THEN
4589              mydebug('complete_putaway: INV/WIP LPN. ');
4590            END IF;
4591 
4592            -- Update MSN's group_mark_id
4593             UPDATE mtl_serial_numbers
4594             SET group_mark_id = p_temp_id
4595             WHERE  serial_number = l_serial
4596             AND    inventory_item_id = p_item_id;
4597 
4598             -- Bug 2458540
4599             -- Update the Serial Status to 4 before calling Receiving Transaction
4600             -- Processor for RMA serial At SALES ORDER ISSUE STANDARD RECEIPT CASE
4601             IF (l_serial_code = 6 AND l_ref = 'ORDER_LINE_ID') THEN
4602               UPDATE mtl_serial_numbers
4603               SET current_status = 4
4604                 , previous_status = current_status
4605               WHERE  serial_number = l_serial
4606               AND    inventory_item_id = p_item_id;
4607             END IF;
4608           END IF;   --END IF (l_lpn_context = 3)
4609         END LOOP;
4610 
4611         IF ser_csr%ISOPEN THEN
4612           CLOSE ser_csr;
4613         END IF;
4614          -- ER 7307189 changes start
4615 	 IF ser_csr_reserved_lpn%ISOPEN THEN
4616 		CLOSE ser_csr_reserved_lpn;
4617 	 END IF;
4618 	 -- ER 7307189 changes end
4619         IF c_rcv_ser_csr%ISOPEN THEN
4620           CLOSE c_rcv_ser_csr;
4621         END IF;
4622       END IF; -- End Lot Loop
4623     --If there is a quantity discrepancy
4624     ELSIF (p_disc = 'Y') THEN
4625       /*
4626        * If the MOL LPN context is "RESIDES IN RECEIVING" and INV and PO Patch
4627        * levels are J or higher then MSNI records would have been created before
4628        * calling complete_putaway, one per serial number.
4629        * Over here we need to match the serials confirmed in the UI against the
4630        * serials in RCV_SERIALS_SUPPLY for the parent transaction
4631        * Get the parent transaction from the current MOL and check for serials
4632        * that exist in MSNI but not in RCV_SERIALS_SUPPLY. If at least one such
4633        * serial is found, then error out the transaction since match failed
4634        */
4635       IF (l_lpn_context = 3) THEN
4636 
4637         --In case of quantity discrepancy, the MTLI/MSNI record would be created
4638         --in the UI itself and we get the product_transaction_id from there
4639         --So, we just need to assign this value to the local variable and subsequently
4640         --pass it the transfer/deliver API for lot/serial splits
4641         l_product_transaction_id := p_product_transaction_id;
4642         --4049874
4643         IF ((l_serial_code > 1  AND l_serial_code <> 6) OR
4644             (l_serial_code = 6  AND (l_ref = 'ORDER_LINE_ID' OR l_ref = 'SHIPMENT_LINE_ID'))) THEN
4645 
4646           --If the line is crossdocked then insert the MSNT records for the serials
4647           --that were confirmed in the UI (using product_transaction_id)
4648           IF (l_is_crossdocked) THEN
4649 	    IF (l_lot_code > 1) THEN
4650 	       SELECT serial_transaction_temp_id
4651 		 INTO   l_msnt_temp_id
4652 		 FROM   mtl_transaction_lots_interface
4653 		 WHERE  lot_number = p_lot
4654 		 AND    product_transaction_id = l_product_transaction_id;
4655 	     ELSE
4656 	       l_msnt_temp_id := p_temp_id;
4657             END IF;
4658 
4659             IF (l_debug = 1) THEN
4660               mydebug('complete_putaway: Case when p_disc = Y. l_msnt_temp_id:'||l_msnt_temp_id);
4661             END IF;
4662 
4663 	    OPEN c_msni_ser_csr(l_product_transaction_id);
4664   	    LOOP
4665 	     FETCH c_msni_ser_csr INTO l_serial_rec;
4666 	     EXIT WHEN c_msni_ser_csr%NOTFOUND;
4667 	     l_fm_serial := l_serial_rec.serial_number;
4668 	     l_to_serial := l_serial_rec.to_serial_number;
4669 	     l_result := insert_msnt_rec(
4670 		 p_transaction_temp_id =>  l_msnt_temp_id
4671                , p_serial_number       =>  l_fm_serial
4672                , p_serial_atts         =>  l_serial_rec
4673                , p_user_id             =>  p_user_id
4674                , p_to_serial_number    =>  l_to_serial);
4675 	     IF NOT l_result THEN
4676 	       IF (l_debug = 1) THEN
4677 		   mydebug('complete_putaway: Failure while Inserting MSNT - qty disc');
4678 		END IF;
4679 		IF c_msni_ser_csr%ISOPEN THEN
4680 		  CLOSE c_msni_ser_csr;
4681 		END IF;
4682 		RAISE fnd_api.g_exc_unexpected_error;
4683 	     END IF;   --END IF check l_result
4684 	  END LOOP;
4685 	  IF c_msni_ser_csr%ISOPEN THEN
4686 	     CLOSE c_msni_ser_csr;
4687 	  END IF;
4688          END IF;   --END IF line is crossdocked
4689         END IF;   --Item is serial controlled
4690       ELSE
4691         --If the LPN is an INV/ WIP LPN and there is a quantity discrepancy,
4692         --for an item that is lot controlled and not serial controlled, then
4693         --update the MTLT record with the user confirmed quantity
4694         --This update statement should be executed if
4695         --  a) WMS and PO Patchset levels are less than J (for LPN contexts)
4696         --  b) If the LPN is an INV/WIP LPN (if patchset levels are > j)
4697         IF ((l_lot_code > 1) AND
4698             (l_serial_code = 1 OR (l_serial_code = 6 AND l_ref <> 'ORDER_LINE_ID'))) THEN
4699           -- Calculate Primary Quantity
4700           l_pr_qty := wms_task_dispatch_gen.get_primary_quantity(
4701                   p_item_id         => p_item_id
4702                 , p_organization_id => p_org_id
4703                 , p_from_quantity   => l_qty
4704                 , p_from_unit       => p_uom);
4705 
4706           IF (l_debug = 1) THEN
4707             mydebug('complete_putaway: primary qty: ' || l_pr_qty);
4708           END IF;
4709           UPDATE mtl_transaction_lots_temp
4710           SET transaction_quantity = l_qty
4711             , primary_quantity = l_pr_qty
4712             , last_update_date = SYSDATE
4713             , last_updated_by = p_user_id
4714           WHERE  transaction_temp_id = p_temp_id
4715           AND    lot_number = p_lot;
4716         END IF;
4717       END IF;   --END IF check patch levels and LPN context
4718     END IF; -- End disc if
4719 
4720     IF (l_debug = 1) THEN
4721       mydebug(
4722            'complete_putaway: :'
4723         || p_org_id
4724         || ':'
4725         || l_ref_id
4726         || ':'
4727         || l_ref
4728         || ':'
4729         || l_ref_type
4730         || ':'
4731         || p_item_id
4732         || ':'
4733         || p_rev
4734         || ':'
4735         || p_sub
4736         || ':'
4737         || p_loc
4738         || ':'
4739         || l_qty
4740         || ':'
4741         || p_uom
4742         || ':'
4743         || p_temp_id
4744         || ':'
4745         || l_lot_code
4746         || ':'
4747         || l_serial_code
4748       );
4749     END IF;
4750 
4751      --Bug 3989684 Start
4752            UPDATE mtl_material_transactions_temp
4753            SET  transaction_header_id = l_txn_header_id
4754            WHERE  transaction_temp_id = p_temp_id;
4755        --Bug 3989684 End
4756 
4757     -- Call Amins Log exceptions
4758     IF (p_qty_reason_id > 0) THEN
4759       --Log exception
4760       IF (l_debug = 1) THEN
4761         mydebug('Logging exception for qty discrepancy');
4762         mydebug('txn_header_id: ' || l_txn_header_id);
4763       END IF;
4764 
4765 
4766       l_mmtt_id := p_temp_id;
4767 
4768       wms_txnrsn_actions_pub.log_exception(
4769         p_api_version_number     => 1.0
4770       , p_init_msg_lst           => fnd_api.g_false
4771       , p_commit                 => fnd_api.g_false
4772       , x_return_status          => x_return_status
4773       , x_msg_count              => x_msg_count
4774       , x_msg_data               => x_msg_data
4775       , p_organization_id        => p_org_id
4776       , p_mmtt_id                => l_mmtt_id
4777       , p_task_id                => l_txn_header_id
4778       , p_reason_id              => p_qty_reason_id
4779       , p_subinventory_code      => p_sub
4780       , p_locator_id             => p_loc
4781       , p_discrepancy_type       => 2
4782       , p_user_id                => l_emp_id --p_user_id Bug 2672785
4783       , p_item_id                => p_item_id
4784       , p_revision               => p_rev
4785       , p_lot_number             => p_lot
4786       );
4787 
4788       IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
4789         fnd_message.set_name('WMS', 'WMS_LOG_EXCEPTION_FAIL');
4790         fnd_msg_pub.ADD;
4791         RAISE fnd_api.g_exc_unexpected_error;
4792       ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
4793         fnd_message.set_name('WMS', 'WMS_LOG_EXCEPTION_FAIL');
4794         fnd_msg_pub.ADD;
4795         RAISE fnd_api.g_exc_error;
4796       END IF;
4797     END IF;
4798 
4799     IF (p_loc_reason_id > 0) THEN
4800       --Log exception
4801       IF (l_debug = 1) THEN
4802         mydebug('Logging exception for loc discrepancy');
4803         mydebug('txn_header_id: ' || l_txn_header_id);
4804       END IF;
4805 
4806       l_mmtt_id := p_temp_id;
4807 
4808       wms_txnrsn_actions_pub.log_exception(
4809         p_api_version_number     => 1.0
4810       , p_init_msg_lst           => fnd_api.g_false
4811       , p_commit                 => fnd_api.g_false
4812       , x_return_status          => x_return_status
4813       , x_msg_count              => x_msg_count
4814       , x_msg_data               => x_msg_data
4815       , p_organization_id        => p_org_id
4816       , p_mmtt_id                => l_mmtt_id
4817       , p_task_id                => l_txn_header_id
4818       , p_reason_id              => p_loc_reason_id
4819       , p_subinventory_code      => p_sub
4820       , p_locator_id             => p_loc
4821       , p_discrepancy_type       => 2
4822       , p_user_id                => l_emp_id --p_user_id Bug 2672785
4823       , p_item_id                => p_item_id
4824       , p_revision               => p_rev
4825       , p_lot_number             => p_lot
4826       , p_is_loc_desc            => TRUE  --Changes for Bug 3989684
4827       );
4828 
4829       IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
4830         fnd_message.set_name('WMS', 'WMS_LOG_EXCEPTION_FAIL');
4831         fnd_msg_pub.ADD;
4832         RAISE fnd_api.g_exc_unexpected_error;
4833       ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
4834         fnd_message.set_name('WMS', 'WMS_LOG_EXCEPTION_FAIL');
4835         fnd_msg_pub.ADD;
4836         RAISE fnd_api.g_exc_error;
4837       END IF;
4838     END IF;
4839 
4840     --If the LPN context is Resides in Receiving
4841     IF (l_lpn_context = 3) THEN
4842       IF (l_debug = 1) THEN
4843         mydebug('complete_putaway: Rcving LPN..');
4844         mydebug('complete_putaway: Updating mmtt info...');
4845         mydebug('complete_putaway: To LPN' || l_to_lpn_id);
4846         mydebug('complete_putaway: Entire LPN' || p_entire_lpn);
4847       END IF;
4848 
4849       IF (p_entire_lpn = 'N') THEN
4850         --Bug 2074100 Fix
4851         IF (l_debug = 1) THEN
4852           mydebug('complete_putaway: not ent lpn');
4853         END IF;
4854 
4855         IF ((l_del_detail_id IS NOT NULL)
4856             AND(l_crossdock_type = 2)
4857             AND(l_wip_supply_type = 1)) THEN
4858           IF (l_debug = 1) THEN
4859             mydebug(' This is a crossdocked line for wip issue ');
4860             mydebug(' setting transfer_lpn to null ');
4861 	  END IF;
4862 	  --This is Receiving LPN, we have to update header_id as we need
4863 	  -- one for each txn
4864 	  --We always will generate new txn header id for a receiving lpn due to the
4865 	  --problem in join between WDTH and MMT for the deliver transaction
4866 	  UPDATE mtl_material_transactions_temp
4867             SET  transaction_header_id = l_txn_header_id
4868             WHERE  transaction_temp_id = p_temp_id;
4869         ELSE
4870           IF (l_debug = 1) THEN
4871             mydebug(' In else for checking wip issue ');
4872           END IF;
4873 
4874 	  IF (l_debug =1 ) THEN
4875 	     mydebug('Update only transfer LPN ID to NULL');
4876 	  END IF;
4877 
4878 	  UPDATE mtl_material_transactions_temp
4879             SET  transaction_header_id = l_txn_header_id
4880             WHERE  transaction_temp_id = p_temp_id;
4881         END IF; --IF ((l_del_detail_id IS NOT NULL) AND(l_crossdock_type = 2) AND(l_wip_supply_type = 1))
4882       ELSE
4883         IF (l_debug = 1) THEN
4884           mydebug('complete_putaway: updating mmtt with content lpn');
4885         END IF;
4886 
4887         -- COMMIT;
4888 	IF l_debug = 1 THEN
4889 	   mydebug('Updating content LPN ID for inventory/WIP LPNs ');
4890 	END IF;
4891 
4892 	UPDATE mtl_material_transactions_temp
4893           SET  transaction_header_id = l_txn_header_id
4894           WHERE  transaction_temp_id = p_temp_id;
4895 
4896 	IF (l_debug = 1) THEN
4897 	   mydebug('complete_putaway: after updating mmtt with content lpn');
4898         END IF;
4899       END IF;--END IF (p_entire_lpn = 'N') THEN
4900 
4901       -- Have to update WMS_Exceptions so that any exceptions already
4902       -- recorded for this MMTT line will now be updated with the new txn
4903       -- header_id
4904       -- Bug# 3434940 - Performance Fixes
4905       -- Go against the org and item which are input params for
4906       -- complete_putaway since this will make use of the index in
4907       -- WMS_EXCEPTIONS
4908       UPDATE wms_exceptions
4909 	SET transaction_header_id = l_txn_header_id
4910 	WHERE  transaction_header_id = l_orig_txn_header_id
4911 	AND organization_id = p_org_id
4912 	AND inventory_item_id = p_item_id;
4913 
4914       IF (l_debug = 1) THEN
4915         mydebug('complete_putaway : Calling Workflow from complete_putaway');
4916       END IF;
4917 
4918       -- Call the workflows
4919 
4920       l_wf := 0;
4921 
4922       IF (p_qty_reason_id > 0) THEN
4923         BEGIN
4924           SELECT 1
4925           INTO   l_wf
4926           FROM   mtl_transaction_reasons
4927           WHERE  reason_id = p_qty_reason_id
4928           AND    workflow_name IS NOT NULL
4929           AND    workflow_name <> ' '
4930           AND    workflow_process IS NOT NULL
4931           AND    workflow_process <> ' ';
4932         EXCEPTION
4933           WHEN NO_DATA_FOUND THEN
4934             l_wf := 0;
4935         END;
4936 
4937         IF l_wf > 0 THEN
4938           IF (l_debug = 1) THEN
4939             mydebug(' complete_putaway : WF exists for this reason code: ' || p_qty_reason_id);
4940             mydebug('complete_putaway : Calling workflow wrapper FOR qty');
4941           END IF;
4942 
4943           -- Calling Workflow
4944           wms_workflow_wrappers.wf_wrapper(
4945             p_api_version         => 1.0
4946           , p_init_msg_list       => fnd_api.g_false
4947           , p_commit              => fnd_api.g_false
4948           , x_return_status       => x_return_status
4949           , x_msg_count           => x_msg_count
4950           , x_msg_data            => x_msg_data
4951           , p_org_id              => p_org_id
4952           , p_rsn_id              => p_qty_reason_id
4953           , p_calling_program     => 'complete_putaway - for qty discrepancy - loose'
4954           , p_tmp_id              => p_temp_id
4955           , p_quantity_picked     => l_qty
4956           , p_dest_sub            => l_org_sub
4957           , p_dest_loc            => l_org_loc
4958           );
4959 
4960           IF (l_debug = 1) THEN
4961             mydebug('complete_putaway : After Calling WF Wrapper');
4962           END IF;
4963 
4964           IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
4965             IF (l_debug = 1) THEN
4966               mydebug('complete_putaway : Error callinf WF wrapper');
4967             END IF;
4968 
4969             fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
4970             fnd_msg_pub.ADD;
4971             RAISE fnd_api.g_exc_unexpected_error;
4972           ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
4973             IF (l_debug = 1) THEN
4974               mydebug('complete_putaway : Error calling WF wrapper');
4975             END IF;
4976 
4977             fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
4978             fnd_msg_pub.ADD;
4979             RAISE fnd_api.g_exc_error;
4980           END IF;
4981         END IF;
4982       END IF;
4983 
4984       -- call workflow for location discrepancy
4985       l_wf := 0;
4986 
4987       IF (p_loc_reason_id > 0) THEN
4988         -- update MMTT with reason id = p_loc_reason_id. (bugfix 4294713)
4989 	update mtl_material_transactions_temp
4990 	set reason_id = p_loc_reason_id
4991 	where transaction_temp_id = p_temp_id;
4992 
4993         BEGIN
4994 	   SELECT 1,
4995 	     workflow_process
4996 	     INTO   l_wf,
4997 	     l_wf_process
4998 	     FROM   mtl_transaction_reasons
4999 	     WHERE  reason_id = p_loc_reason_id
5000 	     AND    workflow_name IS NOT NULL
5001 	     AND    workflow_name <> ' '
5002 	     AND    workflow_process IS NOT NULL
5003 	     AND    workflow_process <> ' ';
5004         EXCEPTION
5005 	   WHEN NO_DATA_FOUND THEN
5006             l_wf := 0;
5007             l_wf_process := null; --Bugfix 4294713
5008         END;
5009 
5010 	IF (  ( l_wf_process IS NOT NULL               --Bugfix 4294713
5011                 AND
5012                 l_wf_process <> 'WMS_N_STEP_PUTAWAY'
5013                )
5014                OR
5015 	       l_operation_plan_id IS NULL
5016            ) THEN
5017 	   IF l_wf > 0 THEN
5018 	      IF (l_debug = 1) THEN
5019 		 mydebug(' complete_putaway : WF exists for this reason code: ' || p_loc_reason_id);
5020 		 mydebug('complete_putaway : Calling workflow wrapper FOR location');
5021 		 mydebug('dest sub: ' || p_sub);
5022 		 mydebug('dest loc: ' || p_loc);
5023 	      END IF;
5024 
5025 	      -- Calling Workflow
5026 	      wms_workflow_wrappers.wf_wrapper(
5027 					       p_api_version         => 1.0
5028 					       , p_init_msg_list       => fnd_api.g_false
5029 					       , p_commit              => fnd_api.g_false
5030 					       , x_return_status       => x_return_status
5031 					       , x_msg_count           => x_msg_count
5032 					       , x_msg_data            => x_msg_data
5033 					       , p_org_id              => p_org_id
5034 					       , p_rsn_id              => p_loc_reason_id
5035 					       , p_calling_program     => 'complete_putaway - for loc discrepancy - loose'
5036 					       , p_tmp_id              => p_temp_id
5037 					       , p_quantity_picked     => NULL
5038 					       , p_dest_sub            => l_org_sub
5039 					       , p_dest_loc            => l_org_loc
5040 					       );
5041 
5042 	      IF (l_debug = 1) THEN
5043 		 mydebug('complete_putaway : After Calling WF Wrapper');
5044 	      END IF;
5045 
5046 	      IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
5047 		 IF (l_debug = 1) THEN
5048 		    mydebug('complete_putaway : Error callinf WF wrapper');
5049 		 END IF;
5050 
5051 		 fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
5052 		 fnd_msg_pub.ADD;
5053 		 RAISE fnd_api.g_exc_unexpected_error;
5054 	       ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
5055 		 IF (l_debug = 1) THEN
5056 		    mydebug('complete_putaway : Error calling WF wrapper');
5057 		 END IF;
5058 
5059 		 fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
5060 		 fnd_msg_pub.ADD;
5061 		 RAISE fnd_api.g_exc_error;
5062 	      END IF;
5063 	   END IF;--END IF l_wf > 0 THEN
5064 	END IF; -- END IF IF (( l_wf_process IS NOT NULL AND l_wf_process <> 'WMS_N_STEP_PUTAWAY') OR l_operation_plan_id IS NULL)
5065       END IF;--IF (p_loc_reason_id > 0) THEN
5066 
5067       -- bug 2271470
5068       -- This MMTT record has been putaway by the user,
5069       -- therefore we don't want this record being seen
5070       -- again if user scans the same LPN.
5071       -- Therefore we update wms_task_type column to -1 as a flag
5072       -- signifying this MMTT record should not be
5073       -- shown in the putaway drop page again.
5074       -- In putaway drop page, we make sure those MMTT
5075       -- with wms_task_type of -1 not selected.
5076 
5077       UPDATE mtl_material_transactions_temp
5078       SET wms_task_type = -1
5079       WHERE  transaction_temp_id = p_temp_id;
5080 
5081       IF (l_debug = 1) THEN
5082         mydebug('complete_putaway: Calling Karuns API');
5083       END IF;
5084 
5085       /* FP-J Lot/Serial Support Enhancement
5086        * If the LPN Context is Resides in Receiving, then do the following:
5087        *  IF INV J and PO J are installed Then
5088        *    Check subinventory type of the drop subinventory
5089        *    If destination sub is receiving subinventory Then
5090        *      Call the Transfer API
5091        *    Else
5092        *      Set the variable p_transaction_temp_id to product_transaction_id
5093        *      Call the Deliver API
5094        *    End If
5095        *  Else
5096        *    Set the value of p_transaction_temp_id to l_dup_temp_id
5097        *    Retain the old code to call deliver API by passing NULL values for
5098        *    the new parameters
5099        *  End If;
5100        */
5101 
5102       l_drop_sub_type := wms_putaway_utils.get_subinventory_type(p_organization_id   =>  p_org_id
5103 								 , p_subinventory_code =>  p_sub);
5104 
5105       IF (l_drop_sub_type < 0) THEN
5106 	 fnd_message.set_name('WMS', 'WMS_TASK_ERROR');
5107 	 fnd_msg_pub.add;
5108 	 IF (l_debug = 1) THEN
5109             mydebug('complete_putaway: Error fetching drop subinventory type');
5110 	 END IF;
5111 	 RAISE fnd_api.g_exc_error;
5112       END IF;
5113 
5114       --Update the wms_process_flag to 2 so that one can work with
5115       --with this move order line from elsewhere
5116       UPDATE mtl_txn_request_lines
5117 	SET wms_process_flag = 2
5118 	WHERE line_id = l_mo_line_id;
5119 
5120       --Storage subinventory - Call Deliver API
5121       IF l_drop_sub_type = 1 THEN
5122 	 IF (l_debug = 1) THEN
5123             mydebug('complete_putaway: Calling the deliver API with product_transaction_id: ' || l_product_transaction_id ||
5124 		    ', from lpn, ' || l_mo_lpn_id || ', xfer_lpn_id: ' ||  l_to_lpn_id || ' lot_num: ' || l_mol_lot_number);
5125 	 END IF;
5126 
5127 	 --R12
5128 	 inv_rcv_std_deliver_apis.Match_putaway_rcvtxn_intf_rec
5129 	   (p_organization_id            => p_org_id
5130 	    , p_reference_id               => l_ref_id
5131 	    , p_reference                  => l_ref
5132 	    , p_reference_type_code        => l_ref_type
5133 	    , p_item_id                    => p_item_id
5134 	    , p_revision                   => p_rev
5135 	    , p_subinventory_code          => p_sub
5136 	    , p_locator_id                 => p_loc
5137 	    , p_rcvtxn_qty                 => l_qty
5138 	    , p_rcvtxn_uom_code            => p_uom
5139 	    , p_transaction_temp_id        => l_product_transaction_id
5140 	    , p_lot_control_code           => l_lot_code
5141 	    , p_serial_control_code        => l_serial_code
5142 	    , p_original_txn_temp_id       => p_temp_id
5143 	    , x_return_status              => x_return_status
5144 	    , x_msg_count                  => x_msg_count
5145 	    , x_msg_data                   => x_msg_data
5146 	    , p_inspection_status_code     => l_inspection_status
5147 	    , p_lpn_id                     => l_mo_lpn_id
5148 	    , p_transfer_lpn_id            => l_to_lpn_id
5149 	    , p_lot_number                 => l_mol_lot_number
5150 	   , p_parent_txn_id              => l_parent_txn_id
5151 	   , p_secondary_quantity         => p_secondary_quantity --OPM Integration
5152 	   , p_secondary_uom              => p_secondary_uom --OPM Integration
5153 	   , p_inspection_status     => l_inspection_status
5154 	   , p_primary_uom_code      => p_primary_uom
5155 	   );
5156 	 IF (l_debug = 1) THEN
5157             mydebug('complete_putaway: After  Karuns Delivery API');
5158 	 END IF;
5159 
5160 	 IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
5161             fnd_message.set_name('WMS', 'WMS_TD_DEL_ERROR');
5162             fnd_msg_pub.ADD;
5163             RAISE fnd_api.g_exc_unexpected_error;
5164           ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
5165             fnd_message.set_name('WMS', 'WMS_TD_DEL_ERROR');
5166             fnd_msg_pub.ADD;
5167             RAISE fnd_api.g_exc_error;
5168 	 END IF;
5169 	 --Receiving subinventory - Call Transfer API
5170        ELSE
5171 	 IF (l_debug = 1) THEN
5172             mydebug('complete_putaway: Calling the Transfer API');
5173 	 END IF;
5174 
5175 	 --R12
5176 	 inv_rcv_std_transfer_apis.Match_transfer_rcvtxn_rec
5177 	   (x_return_status       =>  x_return_status
5178             , x_msg_count           =>  x_msg_count
5179             , x_msg_data            =>  x_msg_data
5180             , p_organization_id     =>  p_org_id
5181             , p_parent_txn_id       =>  l_parent_txn_id
5182             , p_reference_id        =>  l_ref_id
5183             , p_reference           =>  l_ref
5184             , p_reference_type_code =>  l_ref_type
5185             , p_item_id             =>  p_item_id
5186             , p_revision            =>  p_rev
5187             , p_subinventory_code   =>  p_sub
5188             , p_locator_id          =>  p_loc
5189             , p_transfer_quantity   =>  l_qty
5190             , p_transfer_uom_code   =>  p_uom
5191             , p_lot_control_code    =>  l_lot_code
5192             , p_serial_control_code =>  l_serial_code
5193             , p_original_rti_id     =>  l_product_transaction_id
5194             , p_original_temp_id    =>  p_temp_id
5195             , p_lot_number          =>  l_mol_lot_number
5196             , p_lpn_id              =>  l_mo_lpn_id
5197 	   , p_transfer_lpn_id     =>  l_to_lpn_id
5198 	   , p_sec_transfer_quantity   =>  p_secondary_quantity--OPM Integration
5199 	   , p_sec_transfer_uom_code   =>  p_secondary_uom --OPM Integration
5200 	   , p_inspection_status     => l_inspection_status
5201 	   , p_primary_uom_code      => p_primary_uom
5202 	   );
5203 	 IF (l_debug = 1) THEN
5204             mydebug('complete_putaway: After Transfer API');
5205 	 END IF;
5206 
5207 	 IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
5208             fnd_message.set_name('WMS', 'WMS_TASK_ERROR');
5209             fnd_msg_pub.ADD;
5210             RAISE fnd_api.g_exc_unexpected_error;
5211           ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
5212             fnd_message.set_name('WMS', 'WMS_TASK_ERROR');
5213             fnd_msg_pub.ADD;
5214             RAISE fnd_api.g_exc_error;
5215 	 END IF;
5216       END IF;   --END IF check sub type to call the corresponding API
5217 
5218       IF (l_debug = 1) THEN
5219         mydebug(' updating WDT with the group id');
5220       END IF;
5221 
5222       UPDATE wms_dispatched_tasks
5223       SET task_group_id = inv_rcv_common_apis.g_rcv_global_var.interface_group_id
5224       WHERE  transaction_temp_id = p_temp_id;
5225 
5226       l_txn_mode := 2;
5227 
5228       -- Bug 2074100 Fix
5229 
5230       IF ((l_del_detail_id IS NOT NULL)
5231           AND(l_crossdock_type = 2)
5232           AND(l_wip_supply_type = 1)) THEN
5233         IF (l_debug = 1) THEN
5234           mydebug(' This is a crossdocked line for wip issue ');
5235           mydebug(' need to update the context of the from lpn   ');
5236         END IF;
5237 
5238 	-- Bug# 3281512 - Performance Fixes
5239 	-- Match against the org too otherwise we will do a full table
5240 	-- scan on MMTT which is really bad.  Org is an indexed column.
5241 	-- Bug# 3434940 - Performance fixes
5242 	-- Use an EXISTS instead since we are just checking for existence
5243 	-- of any MMTT records for the given LPN.  Also use the item
5244 	-- passed as an input parameter into complete_putaway too so we
5245 	-- can make use of the item/org index or the index on lpn_id.
5246 	-- Note that content_lpn_id currently is not an indexed column.
5247         BEGIN
5248 	   SELECT   1
5249 	     INTO   cnt
5250 	     FROM   DUAL
5251 	     WHERE  EXISTS (SELECT 1
5252 			    FROM   mtl_material_transactions_temp
5253 			    WHERE  lpn_id = p_lpn_id
5254 			    AND    organization_id = p_org_id
5255 			    AND    inventory_item_id = p_item_id)
5256 	     OR     EXISTS (SELECT 1
5257 			    FROM   mtl_material_transactions_temp
5258 			    WHERE  content_lpn_id = p_lpn_id
5259 			    AND    organization_id = p_org_id
5260 			    AND    inventory_item_id = p_item_id);
5261         EXCEPTION
5262           WHEN NO_DATA_FOUND THEN
5263             cnt := 0;
5264         END;
5265 
5266         IF (l_debug = 1) THEN
5267           mydebug('cnt is ' || cnt);
5268         END IF;
5269 
5270        ELSE
5271         IF (l_debug = 1) THEN
5272           mydebug(' In else for checking for wip issue after tm');
5273           mydebug(' to upd the lpn context ');
5274         END IF;
5275       END IF;
5276     --For other LPN contexts (Resides in Inventory, Resides in WIP)
5277     ELSE
5278       -- Set mode to normal
5279       l_txn_mode := 1;
5280 
5281       IF (l_debug = 1) THEN
5282         mydebug('complete_putaway: INV or WIP LPN');
5283         mydebug('complete_putaway: Updating mmtt info...');
5284         mydebug('complete_putaway: To LPN :' || l_to_lpn_id);
5285         mydebug('complete_putaway: Entire LPN' || p_entire_lpn);
5286         mydebug('complete_putaway: lpn drop type ' || p_lpn_mode);
5287         mydebug('complete_putaway: from lpn ' || p_lpn_id );
5288       END IF;
5289 
5290 	--Changes for bug 5403420
5291 	 invttmtx.tdatechk(org_id           => p_org_id,
5292 			   transaction_date => SYSDATE,
5293 			   period_id        => l_acct_period_id,
5294 			   open_past_period => l_open_past_period);
5295 
5296 	 IF l_acct_period_id <= 0 THEN
5297 	   fnd_message.set_name('INV', 'INV_NO_OPEN_PERIOD');
5298 	   fnd_msg_pub.ADD;
5299 	   RAISE fnd_api.g_exc_unexpected_error;
5300 	 END IF;
5301 	--End of changes for bug 5403420
5302 
5303       -- Nested LPN changes
5304       -- Update MMTT for drop entire LPN
5305       IF (l_debug = 1) THEN
5306 	 mydebug('complete_putaway: Patchset J or above , Update MMTT');
5307 	 -- ER 7307189 changes start
5308 	 mydebug(' Update MMTT l_dest_loc_id with ' || l_dest_loc_id);
5309 	 mydebug(' Update MMTT l_dest_sub with ' || l_dest_sub);
5310 	 -- ER 7307189 changes end
5311       END IF;
5312 
5313       IF (p_lpn_id = l_to_lpn_id) THEN
5314 
5315 	 IF (l_debug = 1) THEN
5316 	    mydebug(' Update MMTT contet LPN_ID WITH ' || p_lpn_id);
5317 	    mydebug(' Update MMTT cost_group_id with ' || l_cost_group_id);
5318 	 END IF;
5319 
5320 	 UPDATE mtl_material_transactions_temp
5321 	   SET transaction_header_id = l_txn_header_id
5322 	   , content_lpn_id = p_lpn_id
5323 	   , lpn_id = NULL
5324 	   , transaction_status = 3
5325 	   , posting_flag = 'Y'
5326 	   , cost_group_id = Decode(l_lpn_context,1,l_cost_group_id,cost_group_id)  --BUG 4134432,4475607
5327 	   , transfer_cost_group_id = Decode(l_lpn_context,1,l_cost_group_id,transfer_cost_group_id)  --BUG 4134432, 4475607
5328            , transaction_date = sysdate --added per Karun and Saju's request 04/2006
5329            , acct_period_id = l_acct_period_id --Added for bug 5403420
5330 	   WHERE  transaction_temp_id = p_temp_id;
5331        ELSE
5332 
5333 	 IF (l_debug = 1) THEN
5334 	    mydebug(' Update MMTT from lpn_id with ' || p_lpn_id);
5335 	    mydebug(' Update MMTT to lpn_id with ' || l_to_lpn_id);
5336 	    mydebug(' Update MMTT cost_group_id with ' || l_cost_group_id);
5337 	 END IF;
5338 
5339 	 UPDATE mtl_material_transactions_temp
5340 	   SET transaction_header_id = l_txn_header_id
5341 	   , lpn_id  = p_lpn_id
5342 	   , content_lpn_id = NULL
5343 	   , transfer_lpn_id = l_to_lpn_id
5344 	   , transaction_status = 3
5345 	   , posting_flag = 'Y'
5346 	   , cost_group_id = Decode(l_lpn_context,1,l_cost_group_id,cost_group_id)  --BUG 4134432, 4475607
5347 	   , transfer_cost_group_id = Decode(l_lpn_context,1,l_cost_group_id,transfer_cost_group_id)  --BUG 4134432,4475607
5348            , transaction_date = sysdate --added per Karun and Saju's request 04/2006
5349            , acct_period_id = l_acct_period_id --Added for bug 5403420
5350 	   WHERE  transaction_temp_id = p_temp_id;
5351 
5352       END IF; --END IF (p_lpn_id = l_to_lpn_id) THEN
5353 
5354 
5355       -- Have to update WMS_Exceptions so that any excpetions already
5356       -- recorded for this MMTT line will now be updated with the new txn
5357       -- header_id
5358       -- Bug# 3434940 - Performance Fixes
5359       -- Go against the org and item which are input params for
5360       -- complete_putaway since this will make use of the index in
5361       -- WMS_EXCEPTIONS
5362       UPDATE wms_exceptions
5363 	SET transaction_header_id = l_txn_header_id
5364 	WHERE  transaction_header_id = l_orig_txn_header_id
5365 	AND organization_id = p_org_id
5366 	AND inventory_item_id = p_item_id;
5367 
5368       IF (l_debug = 1) THEN
5369         mydebug('Calling Workflow from complete_putaway');
5370       END IF;
5371 
5372       -- Call the workflows
5373       l_wf := 0;
5374 
5375       IF (p_qty_reason_id > 0) THEN
5376         BEGIN
5377           SELECT 1
5378           INTO   l_wf
5379           FROM   mtl_transaction_reasons
5380           WHERE  reason_id = p_qty_reason_id
5381           AND    workflow_name IS NOT NULL
5382           AND    workflow_name <> ' '
5383           AND    workflow_process IS NOT NULL
5384           AND    workflow_process <> ' ';
5385         EXCEPTION
5386           WHEN NO_DATA_FOUND THEN
5387             l_wf := 0;
5388         END;
5389 
5390         IF l_wf > 0 THEN
5391           IF (l_debug = 1) THEN
5392             mydebug('complete_putaway : WF exists for this reason code: ' || p_qty_reason_id);
5393             mydebug('complete_putaway : Calling workflow wrapper FOR qty');
5394           END IF;
5395 
5396           -- Calling Workflow
5397           wms_workflow_wrappers.wf_wrapper(
5398             p_api_version         => 1.0
5399           , p_init_msg_list       => fnd_api.g_false
5400           , p_commit              => fnd_api.g_false
5401           , x_return_status       => x_return_status
5402           , x_msg_count           => x_msg_count
5403           , x_msg_data            => x_msg_data
5404           , p_org_id              => p_org_id
5405           , p_rsn_id              => p_qty_reason_id
5406           , p_calling_program     => 'complete_putaway - for qty discrepancy - loose'
5407           , p_tmp_id              => p_temp_id
5408           , p_quantity_picked     => l_qty
5409           , p_dest_sub            => l_org_sub
5410           , p_dest_loc            => l_org_loc
5411           );
5412 
5413           IF (l_debug = 1) THEN
5414             mydebug('complete_putaway : After Calling WF Wrapper');
5415           END IF;
5416 
5417           IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
5418             IF (l_debug = 1) THEN
5419               mydebug('complete_putaway : Error callinf WF wrapper');
5420             END IF;
5421 
5422             fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
5423             fnd_msg_pub.ADD;
5424             RAISE fnd_api.g_exc_unexpected_error;
5425           ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
5426             IF (l_debug = 1) THEN
5427               mydebug('complete_putaway : Error calling WF wrapper');
5428             END IF;
5429 
5430             fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
5431             fnd_msg_pub.ADD;
5432             RAISE fnd_api.g_exc_error;
5433           END IF;
5434         END IF; --IF l_wf > 0 THEN
5435       END IF;--IF (p_qty_reason_id > 0) THEN
5436 
5437       -- call workflow for location discrepancy
5438       l_wf := 0;
5439 
5440 
5441       IF (p_loc_reason_id > 0) THEN
5442         -- update MMTT with reason id = p_loc_reason_id.(bugfix 4294713)
5443         update mtl_material_transactions_temp
5444         set reason_id = p_loc_reason_id
5445         where transaction_temp_id = p_temp_id;
5446 
5447         BEGIN
5448 	   SELECT 1,
5449 	     workflow_process
5450 	     INTO   l_wf,
5451 	     l_wf_process
5452 	     FROM   mtl_transaction_reasons
5453 	     WHERE  reason_id = p_loc_reason_id
5454 	     AND    workflow_name IS NOT NULL
5455 	     AND    workflow_name <> ' '
5456 	     AND    workflow_process IS NOT NULL
5457 	     AND    workflow_process <> ' ';
5458         EXCEPTION
5459 	   WHEN NO_DATA_FOUND THEN
5460             l_wf := 0;
5461             l_wf_process := null; --Bugfix 4294713
5462         END;
5463 
5464 	IF (  ( l_wf_process IS NOT NULL               --Bugfix 4294713
5465                 AND
5466                 l_wf_process <> 'WMS_N_STEP_PUTAWAY'
5467                )
5468                OR
5469 	       l_operation_plan_id IS NULL
5470            ) THEN
5471 	   -- Bug 3346762
5472 	   IF (l_lpn_context=2) AND l_dest_sub IS NULL THEN
5473 	      l_wf_temp_sub := l_org_sub;
5474 	      l_wf_temp_loc := l_org_loc;
5475 	    ELSE
5476 	      l_wf_temp_sub := l_dest_sub;
5477 	      l_wf_temp_loc := l_dest_loc_id;
5478 	   END IF;
5479 
5480 	   IF l_wf > 0 THEN
5481 	      IF (l_debug = 1) THEN
5482 		 mydebug(' complete_putaway : WF exists for this reason code: ' || p_loc_reason_id);
5483 		 mydebug('complete_putaway : Calling workflow wrapper FOR location');
5484 		 mydebug('dest sub: ' || p_sub);
5485 		 mydebug('dest loc: ' || p_loc);
5486 	      END IF;
5487 
5488 
5489 
5490 	      -- Calling Workflow
5491 	      wms_workflow_wrappers.wf_wrapper(
5492 					       p_api_version         => 1.0
5493 					       , p_init_msg_list       => fnd_api.g_false
5494 					       , p_commit              => fnd_api.g_false
5495 					       , x_return_status       => x_return_status
5496 					       , x_msg_count           => x_msg_count
5497 					       , x_msg_data            => x_msg_data
5498 					       , p_org_id              => p_org_id
5499 					       , p_rsn_id              => p_loc_reason_id
5500 					       , p_calling_program     => 'complete_putaway - for loc discrepancy - loose'
5501 					       , p_tmp_id              => p_temp_id
5502 					       , p_quantity_picked     => NULL
5503 					       , p_dest_sub            => l_wf_temp_sub
5504 					       , p_dest_loc            => l_wf_temp_loc
5505 					       );
5506 
5507 	      IF (l_debug = 1) THEN
5508 		 mydebug('complete_putaway : After Calling WF Wrapper');
5509 	      END IF;
5510 
5511 	      IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
5512 		 IF (l_debug = 1) THEN
5513 		    mydebug('complete_putaway : Error callinf WF wrapper');
5514 		 END IF;
5515 
5516 		 fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
5517 		 fnd_msg_pub.ADD;
5518 		 RAISE fnd_api.g_exc_unexpected_error;
5519 	       ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
5520 		 IF (l_debug = 1) THEN
5521 		    mydebug('complete_putaway : Error calling WF wrapper');
5522 		 END IF;
5523 
5524 		 fnd_message.set_name('WMS', 'WMS_WORK_FLOW_FAIL');
5525 		 fnd_msg_pub.ADD;
5526 		 RAISE fnd_api.g_exc_error;
5527 	      END IF;
5528 	   END IF;--END IF l_wf > 0 THEN
5529 	END IF; --IF (( l_wf_process IS NOT NULL AND l_wf_process <> 'WMS_N_STEP_PUTAWAY') OR l_operation_plan_id IS NULL) THEN
5530       END IF;--END IF (p_loc_reason_id > 0) THEN
5531 
5532       IF l_transaction_source_type_id = 5 THEN
5533         IF (l_debug = 1) THEN
5534           mydebug('complete_putaway : calling wma_inv_wrappers.transferReservation');
5535           mydebug('l_ref_id: => ' || l_ref_id);
5536           mydebug('p_sub: ====> ' || p_sub);
5537           mydebug('p_loc: ====> ' || p_loc);
5538           mydebug('l_del_detail_id ====> ' || l_del_detail_id);
5539         END IF;
5540 
5541         --bug 2310251 for WIP Putaway, use the new business flow code
5542         l_business_flow_code := 35;
5543 
5544         IF (l_del_detail_id IS NULL) THEN
5545           wms_wip_integration.transfer_reservation(
5546             p_header_id             => l_ref_id
5547           , p_subinventory_code     => p_sub
5548           , p_locator_id            => p_loc
5549           , x_return_status         => x_return_status
5550           , x_msg_count             => x_msg_count
5551           , x_err_msg               => x_msg_data
5552           , p_temp_id               => p_temp_id
5553           );
5554         ELSE
5555           IF (l_debug = 1) THEN
5556             mydebug('complete_putaway : call transfer_reservation from complete_crossdock api');
5557           END IF;
5558         END IF;
5559 
5560         IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
5561           IF (l_debug = 1) THEN
5562             mydebug('complete_putaway : wma_inv_wrappers.transferReservation - Unexpect error');
5563           END IF;
5564 
5565           RAISE fnd_api.g_exc_unexpected_error;
5566         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
5567           IF (l_debug = 1) THEN
5568             mydebug('complete_putaway : wma_inv_wrappers.transferReservation - Expect error');
5569           END IF;
5570 
5571           RAISE fnd_api.g_exc_error;
5572         END IF;
5573       END IF;
5574 
5575       -- Added release level check for ATF_J3
5576 
5577       IF (l_debug = 1) THEN
5578 	 mydebug('complete_putaway: Patchset J or higher. Call to Inventroy TM will be done in complete_putaway_wrapper ');
5579       END IF;
5580 
5581       -- 4515887 CHANGES START
5582       IF l_txn_action_id =31 AND l_txn_type_id= 44 THEN
5583 	 IF (l_debug = 1) THEN
5584 	    mydebug('complete_putaway: Calling create_snapshot');
5585 	 END IF;
5586 	 create_snapshot(p_temp_id,p_org_id);
5587       END IF;
5588       -- 4515887 CHANGES END
5589 
5590     END IF;
5591 
5592     -- Bug# 2795096
5593     -- Do a commit only if the input parameter is set to 'Y' = YES
5594     IF (p_commit = 'Y') THEN
5595       COMMIT;
5596     END IF;
5597 
5598     x_return_status := fnd_api.g_ret_sts_success;
5599 
5600     IF (l_debug = 1) THEN
5601       mydebug('complete_putaway: done WITH API');
5602     END IF;
5603   EXCEPTION
5604     WHEN fnd_api.g_exc_error THEN
5605       -- Bug# 2744170
5606       -- Perform a rollback in the exception blocks
5607       ROLLBACK TO complete_putaway_sp;
5608       x_return_status := fnd_api.g_ret_sts_error;
5609       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5610       IF ser_csr%ISOPEN THEN
5611         CLOSE ser_csr;
5612       END IF;
5613       -- ER 7307189 changes start
5614 	 IF ser_csr_reserved_lpn%ISOPEN THEN
5615 		CLOSE ser_csr_reserved_lpn;
5616 	 END IF;
5617       -- ER 7307189 changes end
5618       IF c_rcv_ser_csr%ISOPEN THEN
5619         CLOSE c_rcv_ser_csr;
5620       END IF;
5621       IF (l_debug = 1) THEN
5622         mydebug('complete_putaway: Excection error - ' || SQLERRM);
5623       END IF;
5624     WHEN OTHERS THEN
5625 
5626       ROLLBACK TO complete_putaway_sp;
5627       x_return_status := fnd_api.g_ret_sts_unexp_error;
5628       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5629       IF ser_csr%ISOPEN THEN
5630         CLOSE ser_csr;
5631       END IF;
5632       -- ER 7307189 changes start
5633 	 IF ser_csr_reserved_lpn%ISOPEN THEN
5634 		CLOSE ser_csr_reserved_lpn;
5635 	 END IF;
5636       -- ER 7307189 changes end
5637       IF c_rcv_ser_csr%ISOPEN THEN
5638         CLOSE c_rcv_ser_csr;
5639       END IF;
5640       IF (l_debug = 1) THEN
5641         mydebug('complete_putaway: Others exception - ' || SQLERRM);
5642       END IF;
5643   END complete_putaway;
5644 
5645   /*changes for OPM are not needed in this procedure since this is not called post-patchset J */
5646   PROCEDURE discrepancy(
5647     p_lpn_id         IN             NUMBER
5648   , p_org_id         IN             NUMBER
5649   , p_temp_id        IN             NUMBER
5650   , p_qty            IN             NUMBER
5651   , p_uom            IN             VARCHAR2
5652   , p_user_id        IN             NUMBER
5653   , x_return_status  OUT NOCOPY     VARCHAR2
5654   ) IS
5655     l_orig_qty      NUMBER;
5656     l_orig_uom      VARCHAR2(3);
5657     l_mol_uom       VARCHAR2(3);
5658     l_line_id       NUMBER;
5659     l_item_id       NUMBER;
5660     l_temp_id       NUMBER;
5661     l_org_id        NUMBER;
5662     l_uom           VARCHAR2(3);
5663     l_qty           NUMBER;
5664     l_qty_diff      NUMBER;
5665     l_qty_diff_prim NUMBER;
5666     l_cnt1          NUMBER;
5667     l_debug         NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5668   BEGIN
5669     l_temp_id := p_temp_id;
5670     l_org_id := p_org_id;
5671     l_uom := p_uom;
5672     l_qty := p_qty;
5673 
5674     SELECT transaction_quantity
5675          , transaction_uom
5676          , inventory_item_id
5677          , move_order_line_id
5678     INTO   l_orig_qty
5679          , l_orig_uom
5680          , l_item_id
5681          , l_line_id
5682     FROM   mtl_material_transactions_temp
5683     WHERE  transaction_temp_id = l_temp_id
5684     AND    organization_id = l_org_id;
5685 
5686     IF (l_uom <> l_orig_uom) THEN
5687       --mydebug('Converting uom');
5688       l_qty :=
5689         inv_convert.inv_um_convert(
5690           item_id           => l_item_id
5691         , PRECISION         => NULL
5692         , from_quantity     => l_qty
5693         , from_unit         => l_uom
5694         , to_unit           => l_orig_uom
5695         , from_name         => NULL
5696         , to_name           => NULL
5697         );
5698     END IF;
5699 
5700     -- Calculate the difference
5701 
5702     l_qty_diff := l_orig_qty - l_qty;
5703     -- Calculate the difference in terms of the primary uom
5704 
5705     l_qty_diff_prim :=
5706       wms_task_dispatch_gen.get_primary_quantity(
5707           p_item_id         => l_item_id
5708         , p_organization_id => l_org_id
5709         , p_from_quantity   => l_qty_diff
5710         , p_from_unit       => l_orig_uom);
5711 
5712     -- Update MMTT with new values for txn and primary qty
5713 
5714     UPDATE mtl_material_transactions_temp
5715     SET transaction_quantity = transaction_quantity - l_qty_diff
5716       , primary_quantity = primary_quantity - l_qty_diff_prim
5717     WHERE  transaction_temp_id = l_temp_id
5718     AND    organization_id = l_org_id;
5719 
5720     -- Update mtl_transaction_lots_temp, if necessary
5721     --calling this procedure
5722     /* Bug #2966531
5723      * Do not update MTLT again since they would already have been done before
5724      * coming here */
5725     /*
5726     BEGIN
5727      UPDATE mtl_transaction_lots_temp
5728      SET transaction_quantity = transaction_quantity-l_qty_diff,
5729          primary_quantity = primary_quantity-l_qty_diff_prim
5730      WHERE transaction_temp_id = l_temp_id;
5731     EXCEPTION
5732      WHEN OTHERS THEN
5733      NULL;
5734     END;*/
5735 
5736     --mydebug('After updating mmtt');
5737     -- Update MTL_TXN_REQUESTS_TABLE
5738     -- Since the uom code in mtl_txn_request_lines will always
5739     -- be the same as the transaction_uom in mmtt, no need to do the
5740     -- conversion again
5741     UPDATE mtl_txn_request_lines
5742     SET quantity_detailed = quantity_detailed - l_qty_diff
5743     WHERE  line_id = l_line_id
5744     AND    organization_id = l_org_id;
5745     --mydebug('After updating mol');
5746 
5747     x_return_status := fnd_api.g_ret_sts_success;
5748   EXCEPTION
5749     WHEN fnd_api.g_exc_error THEN
5750       x_return_status := fnd_api.g_ret_sts_error;
5751     WHEN OTHERS THEN
5752       x_return_status := fnd_api.g_ret_sts_unexp_error;
5753   END discrepancy;
5754 
5755   -- Important, please note that the input parameter, p_user_id
5756   -- does NOT refer to the fnd user id.  Instead it refers to the employee id.
5757   -- It is used against the wms_dispatched_tasks.person_id column which
5758   -- is populated with the employee id.
5759   PROCEDURE check_lpn_validity(
5760     p_org_id         IN             NUMBER
5761   , p_lpn_id         IN             NUMBER
5762   , x_ret            OUT NOCOPY     NUMBER
5763   , x_return_status  OUT NOCOPY     VARCHAR2
5764   , x_msg_count      OUT NOCOPY     NUMBER
5765   , x_msg_data       OUT NOCOPY     VARCHAR2
5766   , x_context        OUT NOCOPY     NUMBER
5767   , p_user_id        IN             NUMBER
5768   ) IS
5769     l_ret              NUMBER;
5770     l_msg_data         VARCHAR2(2000);
5771     l_lpn_id           NUMBER;
5772     l_dummy            NUMBER;
5773     l_org_id           NUMBER;
5774     l_lpn_context      NUMBER;
5775     l_count            NUMBER;
5776     l_insp_status      NUMBER;
5777     l_process_flag_cnt NUMBER;
5778     l_return_status    VARCHAR2(1)    := fnd_api.g_ret_sts_success;
5779     l_mo_cnt           NUMBER;
5780     l_mo_cnt2          NUMBER;
5781     l_so_cnt           NUMBER;
5782     l_nested_lpn_cnt   NUMBER;
5783     l_sub              VARCHAR2(10);
5784     l_locator_id       NUMBER;
5785     l_emp_id           NUMBER;--Bug# 3116925
5786     -- Need to trap the error returned when
5787     -- unable to lock the lpn record.
5788     record_locked      EXCEPTION;
5789     PRAGMA EXCEPTION_INIT(record_locked, -54);
5790     l_debug            NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5791 
5792     -- Following variables added in ATF_J
5793 
5794     -- end variables added in ATF_J
5795 
5796     l_tempid_tab         num_tab;
5797     l_error_code         NUMBER;
5798     l_inspection_flag    NUMBER;
5799     l_load_flag          NUMBER;
5800     l_drop_flag          NUMBER;
5801     l_load_prim_quantity NUMBER;
5802     l_insp_prim_quantity NUMBER;
5803     l_drop_prim_quantity NUMBER;
5804 
5805 
5806     -- This cursor will get the MMTTs assoicated with the LPN passed
5807     CURSOR c_lpn_mmtt_cursor IS
5808       SELECT mmtt.transaction_temp_id
5809       FROM   mtl_material_transactions_temp mmtt
5810             ,mtl_txn_request_lines mtrl
5811       WHERE mtrl.line_id = mmtt.move_order_line_id
5812             AND mtrl.line_status = 7
5813             AND mtrl.lpn_id = p_lpn_id
5814             AND mtrl.organization_id = p_org_id;
5815 
5816   BEGIN
5817     l_lpn_id := p_lpn_id;
5818     l_org_id := p_org_id;
5819     x_ret := 0;
5820     l_count := 0;
5821     l_process_flag_cnt := 0;
5822 
5823     IF (l_debug = 1) THEN
5824       mydebug('check_lpn_validity: In LPN Valid APi');
5825       mydebug('Org' || l_org_id);
5826       mydebug('check_lpn_validity: LPN' || l_lpn_id);
5827     END IF;
5828 
5829     --get the context
5830     BEGIN
5831        SELECT lpn_context
5832 	 INTO   l_lpn_context
5833 	 FROM   wms_license_plate_numbers
5834 	 WHERE  lpn_id = l_lpn_id
5835 	 AND    organization_id = l_org_id;
5836     EXCEPTION
5837       WHEN NO_DATA_FOUND THEN
5838         IF (l_debug = 1) THEN
5839           mydebug('check_lpn_validity: LPN does not belong to ' || l_org_id || '  organization');
5840         END IF;
5841         fnd_message.set_name('INV', 'INV_NO_RESULT_FOUND');
5842         fnd_msg_pub.ADD;
5843         RAISE fnd_api.g_exc_unexpected_error;
5844     END;
5845 
5846     mydebug('check_lpn_validity: LPN Context' || l_lpn_context);
5847     l_mo_cnt := 0;
5848     l_mo_cnt2 := 0;
5849     l_so_cnt := 0;
5850     l_nested_lpn_cnt := 0;
5851 
5852     --BUG 3625990: Don't lock if the LPN is in Receiving
5853     IF (l_lpn_context <> 3) THEN
5854        -- Lock the LPN or return an error if another user
5855        -- already has a lock.  This is to prevent the
5856        -- current session from hanging (bug 1724818).
5857       BEGIN
5858 	 SELECT     lpn_id
5859 	   INTO       l_dummy
5860 	   FROM       wms_license_plate_numbers
5861 	   WHERE      lpn_id = l_lpn_id
5862 	   AND        organization_id = l_org_id
5863 	   FOR UPDATE NOWAIT;
5864       EXCEPTION
5865 	 WHEN NO_DATA_FOUND THEN
5866 	    IF (l_debug = 1) THEN
5867 	       mydebug('check_lpn_validity: LPN does not belong to ' || l_org_id || '  organization');
5868 	    END IF;
5869 
5870 	    fnd_message.set_name('INV', 'INV_NO_RESULT_FOUND');
5871 	    fnd_msg_pub.ADD;
5872 	    RAISE fnd_api.g_exc_unexpected_error;
5873 	 WHEN record_locked THEN
5874 	    IF (l_debug = 1) THEN
5875 	       mydebug('check_lpn_validity: LPN not available. locked by someone else');
5876 	    END IF;
5877 
5878 	    fnd_message.set_name('WMS', 'WMS_LPN_UNAVAIL');
5879 	    fnd_msg_pub.ADD;
5880 	    RAISE fnd_api.g_exc_unexpected_error;
5881       END;
5882     END IF;
5883 
5884     IF (l_debug = 1) THEN
5885       mydebug('check_lpn_validity:  nested LPN Check');
5886     END IF;
5887 
5888     -- Check to see if this LPN has already been loaded by another user
5889     -- Check to see if this is a nested LPN
5890     -- Dont do this check if WMS and PO patch levels are J or higher
5891 
5892     IF l_nested_lpn_cnt > 0 THEN
5893       IF (l_debug = 1) THEN
5894         mydebug('check_lpn_validity: This is a nested LPN');
5895       END IF;
5896 
5897       x_ret := 3;
5898     ELSE
5899       IF (l_debug = 1) THEN
5900         mydebug('check_lpn_validity: This is NOT a nested LPN');
5901       END IF;
5902 
5903       x_context := l_lpn_context;
5904 
5905       IF (l_lpn_context <> 1) THEN
5906         IF (l_debug = 1) THEN
5907           mydebug('check_lpn_validity: Not an inv lpn, so MO has to exist');
5908         END IF;
5909 
5910         BEGIN
5911           SELECT 1
5912           INTO   l_mo_cnt
5913           FROM   DUAL
5914           WHERE  EXISTS(SELECT 1
5915                         FROM   mtl_txn_request_lines
5916                         WHERE  lpn_id = l_lpn_id
5917                         AND    organization_id = l_org_id
5918 			);
5919         EXCEPTION
5920           WHEN NO_DATA_FOUND THEN
5921             l_mo_cnt := 0;
5922 
5923             IF (l_debug = 1) THEN
5924               mydebug('check_lpn_validity: MO not Found');
5925               mydebug('check_lpn_validity: Not an inv lpn and MO does not exist, hence it is invalid');
5926             END IF;
5927 
5928             x_ret := 3;
5929         END;
5930 
5931         IF l_mo_cnt > 0 THEN
5932           /*Check process flag - Flag to indicate processing status for putaways.
5933           1 means Ok to process,
5934           2 means Do not Allocate,
5935           3 means Allocate but do not process.
5936           To be used by Receiving and WIP*/
5937           BEGIN
5938             SELECT 1
5939             INTO   l_process_flag_cnt
5940             FROM   DUAL
5941             WHERE  EXISTS(SELECT 1
5942                           FROM   mtl_txn_request_lines
5943                           WHERE  lpn_id = l_lpn_id
5944                           AND    organization_id = l_org_id
5945                           AND    NVL(wms_process_flag, 1) = 2
5946 			  AND    line_status <> 5); -- 3773255
5947           EXCEPTION
5948             WHEN NO_DATA_FOUND THEN
5949               l_process_flag_cnt := 0;
5950           END;
5951 
5952           IF l_process_flag_cnt > 0 THEN
5953             x_ret := 2;
5954 
5955             IF (l_debug = 1) THEN
5956               mydebug('check_lpn_validity: wms_process_flag does not allow allocations/putaway');
5957             END IF;
5958           ELSE
5959             l_count := 0;
5960 
5961             IF (l_debug = 1) THEN
5962               mydebug('check_lpn_validity: wms_process_flag allows allocations/putaway');
5963             END IF;
5964 
5965 
5966       -- Calling the ATF API Validate to check whether inspection is required or not.
5967       -- If MMTT is available for the LPN (pre-generate or in middle of operation cases)
5968       -- call validate_operation by passing the MMTT ID
5969       -- Else call validate_operation by passing the LPN ID
5970 
5971        OPEN c_lpn_mmtt_cursor;
5972 
5973        FETCH c_lpn_mmtt_cursor
5974          BULK COLLECT
5975           INTO l_tempid_tab;
5976 
5977        -- MMTT records exist check
5978        IF l_tempid_tab.COUNT > 0 THEN
5979 	  -- The LPN passed has MMTT records so call validate_operation
5980 
5981 	  -- Validate each MMTT record
5982 	  FOR i IN 1 .. l_tempid_tab.COUNT LOOP
5983 
5984 	     wms_atf_runtime_pub_apis.validate_operation (
5985                 x_return_status    =>   x_return_status
5986                ,x_msg_data         =>   x_msg_data
5987                ,x_msg_count        =>   x_msg_count
5988                ,x_error_code       =>   l_error_code
5989                ,x_inspection_flag  =>   l_inspection_flag
5990                ,x_load_flag        =>   l_load_flag
5991                ,x_drop_flag        =>   l_drop_flag
5992                ,x_load_prim_quantity    => l_load_prim_quantity
5993                ,x_drop_prim_quantity    => l_drop_prim_quantity
5994                ,x_inspect_prim_quantity => l_insp_prim_quantity
5995                ,p_source_task_id        => l_tempid_tab(i)
5996                ,p_move_order_line_id    => NULL
5997                ,p_inventory_item_id     => NULL
5998                ,p_lpn_id                => NULL
5999                ,p_activity_type_id      => G_OP_ACTIVITY_INBOUND
6000                ,p_organization_id       => p_org_id
6001               );
6002 
6003             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
6004                IF (l_debug = 1) THEN
6005                   mydebug('Check LPN Validity: validate_operation failed ' || x_msg_data );
6006                END IF;
6007                RAISE fnd_api.g_exc_error;
6008             END IF;
6009 
6010 	    -- Bug# 3349802
6011 	    -- The variable l_count is initialized to 0 previously.  For
6012 	    -- each MMTT record validated, if it requires inspection, then
6013 	    -- add 1 to l_count.  Once we are done looping through all MMTT
6014 	    -- records, if l_count = 0 then that means all MMTT records do
6015 	    -- not require inspection.  If l_count = l_tempid_tab.COUNT, then
6016 	    -- all MMTT records require inspection.  Otherwise some of the
6017 	    -- MMTT records require inspection.  We do not want to exit the
6018 	    -- validation loop if one of the MMTT records require
6019 	    -- inspection.  Otherwise there would be no way to determine if
6020 	    -- partial inspection or full inspection is required for the LPN.
6021             IF (l_inspection_flag <> G_NO_INSPECTION) THEN
6022                -- This MMTT requires inspection
6023                mydebug('MMTT:'||l_tempid_tab(i)||' Requires inspection');
6024 
6025                -- Increment l_count by 1
6026                l_count := l_count + 1;
6027             END IF;
6028 
6029          END LOOP; -- Finished validating each MMTT record for the given LPN
6030 
6031 	 -- Check the value for l_count to determine what type of
6032 	 -- inspection is required for the LPN.  Also set the value
6033 	 -- of l_count to the appropriate value as before.
6034 	 IF (l_count = l_tempid_tab.COUNT) THEN
6035 	    l_count := 1;
6036 	    mydebug('All of the MMTT records require inspection');
6037 	  ELSIF (l_count = 0) THEN
6038 	    l_count := 0;
6039 	    mydebug('None of the MMTT records require inspection');
6040 	  ELSE
6041 	    l_count := 0;
6042 	    x_ret := 4;
6043 	    mydebug('Some of the MMTT records require inspection');
6044 	 END IF;
6045 
6046 	ELSE
6047          -- The LPN passed does not have MMTTs so call validate_operation by passing LPN ID
6048 
6049          wms_atf_runtime_pub_apis.validate_operation (
6050              x_return_status    =>   x_return_status
6051             ,x_msg_data         =>   x_msg_data
6052             ,x_msg_count        =>   x_msg_count
6053             ,x_error_code       =>   l_error_code
6054             ,x_inspection_flag  =>   l_inspection_flag
6055             ,x_load_flag        =>   l_load_flag
6056             ,x_drop_flag        =>   l_drop_flag
6057             ,x_load_prim_quantity    => l_load_prim_quantity
6058             ,x_drop_prim_quantity    => l_drop_prim_quantity
6059             ,x_inspect_prim_quantity => l_insp_prim_quantity
6060             ,p_source_task_id        => NULL
6061             ,p_move_order_line_id    => NULL
6062             ,p_inventory_item_id     => NULL
6063             ,p_lpn_id                => p_lpn_id
6064             ,p_activity_type_id      => G_OP_ACTIVITY_INBOUND
6065             ,p_organization_id       => p_org_id
6066            );
6067 
6068          IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
6069             IF (l_debug = 1) THEN
6070                mydebug('Check LPN Validty:validate_operation failed ' || x_msg_data );
6071             END IF;
6072             RAISE fnd_api.g_exc_error;
6073          END IF;
6074 
6075 	 IF (l_inspection_flag = g_partial_inspection) THEN
6076 	    mydebug('LPN: '|| p_lpn_id||' is partially inspected');
6077 	    l_count := 0;
6078 	    x_ret := 4;
6079 	  ELSIF (l_inspection_flag <> G_NO_INSPECTION) THEN
6080             -- This MMTT requires inspection, hence set the inspect req flag and exit out of the loop
6081             mydebug('LPN: '|| p_lpn_id||' Requires inspection. hence set the ret status to inspect required');
6082             -- Setting the flag which will indicate inspect is req
6083             l_count := 1;
6084          END IF;
6085 
6086        END IF; -- MMTTS exists check
6087 
6088 
6089 
6090             IF l_count > 0 THEN
6091               IF (l_debug = 1) THEN
6092                 mydebug('check_lpn_validity: LPN Needs inspection');
6093               END IF;
6094 
6095               x_ret := 1;
6096             END IF;
6097           END IF;
6098         END IF;
6099       ELSE
6100         IF (l_debug = 1) THEN
6101           mydebug('check_lpn_validity: It is an INV LPN');
6102           mydebug('check_lpn_validity: Verifying that this LPN has not been picked for any sales order');
6103         END IF;
6104 
6105         BEGIN
6106           SELECT 1
6107           INTO   l_so_cnt
6108           FROM   DUAL
6109           WHERE  EXISTS(
6110                    SELECT 1
6111                    FROM   wsh_delivery_details_ob_grp_v wdd, wsh_delivery_assignments_v wda
6112                    WHERE  wdd.lpn_id = l_lpn_id
6113 		   AND    wdd.released_status = 'X'  -- For LPN reuse ER : 6845650
6114                    AND    wdd.organization_id = l_org_id
6115                    AND    wdd.delivery_detail_id = wda.parent_delivery_id);
6116         EXCEPTION
6117           WHEN NO_DATA_FOUND THEN
6118             l_so_cnt := 0;
6119         END;
6120 
6121         IF l_so_cnt > 0 THEN
6122           IF (l_debug = 1) THEN
6123             mydebug('check_lpn_validity: LPN has been picked for a sales order and hence cannot be putaway');
6124           END IF;
6125 
6126           x_ret := 3;
6127         END IF;
6128 
6129         -- Fix for Bug No. : 2374961
6130         -- Since we are now commiting after detailing for an
6131         -- INV LPN (in suggestions_pub), we have to check to see if
6132         -- move orders exist
6133         -- if they do, it implies that somebody else is attempting to
6134         -- putaway the LPN
6135 
6136         IF (l_debug = 1) THEN
6137           mydebug('check_lpn_validity: Verifying that this LPN is not in use');
6138         END IF;
6139 
6140         --Checking whether this LPN is being processed by somebody else.
6141 
6142         --Bug# 3116925
6143         --Get the employee ID so we can use it
6144         --for checking against the person_id column in WDT
6145         --because while creating the task, employee_id is stored.
6146         BEGIN
6147           SELECT employee_id
6148           INTO l_emp_id
6149           FROM fnd_user
6150           WHERE user_id = p_user_id;
6151         EXCEPTION
6152           WHEN NO_DATA_FOUND THEN
6153       IF (l_debug = 1) THEN
6154         mydebug('Check_LPN_Validity: There is no employee tied to the user');
6155       END IF;
6156     l_emp_id := NULL;
6157         END;
6158 
6159         IF (l_debug = 1) THEN
6160      mydebug('Check_LPN_Validity:  Emp Id ' || l_emp_id );
6161         END IF;
6162 
6163         BEGIN
6164           SELECT 1
6165           INTO   l_mo_cnt2
6166           FROM   DUAL
6167           WHERE  EXISTS(
6168                    SELECT 1
6169                    FROM   wms_dispatched_tasks wdt
6170                         , mtl_material_transactions_temp mmtt
6171                         , mtl_txn_request_lines mtrl
6172                    WHERE  mtrl.lpn_id = l_lpn_id
6173                    AND    mtrl.organization_id = l_org_id
6174                    AND    mtrl.line_id = mmtt.move_order_line_id
6175                    AND    wdt.transaction_temp_id = mmtt.transaction_temp_id
6176                    AND    wdt.status <> 4
6177                    AND NOT (wdt.status = 3 AND wdt.person_id = l_emp_id ));--Bug# 3116925
6178                   -- AND    NOT(wdt.status = 3
6179                   --            AND wdt.person_id = p_user_id));
6180         /*
6181         SELECT 1 INTO l_mo_cnt2 FROM DUAL WHERE  exists
6182           (SELECT 1
6183            FROM mtl_txn_request_lines mol,
6184            mtl_material_transactions_temp mmtt
6185            , wms_dispatched_tasks wdt
6186            WHERE mol.lpn_id=l_lpn_id
6187            AND mol.organization_id=l_org_id
6188            AND mol.line_id=mmtt.move_order_line_id
6189            AND wdt.transaction_temp_id=mmtt.transaction_temp_id
6190           );
6191           */
6192         EXCEPTION
6193           WHEN NO_DATA_FOUND THEN
6194             l_mo_cnt2 := 0;
6195         END;
6196 
6197         IF l_mo_cnt2 > 0 THEN
6198           IF (l_debug = 1) THEN
6199             mydebug('check_lpn_validity: LPN is being processed by somebody else and hence cannot be putaway');
6200           END IF;
6201 
6202           x_ret := 3;
6203           fnd_message.set_name('WMS', 'WMS_LPN_UNAVAIL');
6204           fnd_msg_pub.ADD;
6205           RAISE fnd_api.g_exc_unexpected_error;
6206         END IF;
6207       END IF;
6208     END IF;
6209 
6210     IF (l_debug = 1) THEN
6211       mydebug('check_lpn_validity: Ret Status' || l_return_status);
6212     END IF;
6213 
6214     x_return_status := fnd_api.g_ret_sts_success;
6215   EXCEPTION
6216     WHEN OTHERS THEN
6217       x_return_status := fnd_api.g_ret_sts_unexp_error;
6218       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6219   END check_lpn_validity;
6220 
6221   PROCEDURE archive_task(
6222     p_temp_id        IN             NUMBER
6223   , p_org_id         IN             NUMBER
6224   , x_return_status  OUT NOCOPY     VARCHAR2
6225   , x_msg_count      OUT NOCOPY     NUMBER
6226   , x_msg_data       OUT NOCOPY     VARCHAR2
6227   ) IS
6228     l_temp_id               NUMBER;
6229     l_txn_header_id         NUMBER;
6230     l_org_id                NUMBER;
6231     l_return_status         VARCHAR2(1);
6232     l_msg_count             NUMBER;
6233     l_msg_data              VARCHAR2(2000);
6234     l_mmtt_line_id          NUMBER;
6235     l_mmtt_qty              NUMBER;
6236     l_transaction_batch_id  NUMBER;
6237     l_transaction_batch_seq NUMBER;
6238     l_debug                 NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6239   BEGIN
6240     IF (l_debug = 1) THEN
6241       mydebug('In archive task..' || p_temp_id || ':' || p_org_id);
6242     END IF;
6243 
6244     l_temp_id := p_temp_id;
6245     l_org_id := p_org_id;
6246 
6247     IF (l_debug = 1) THEN
6248       mydebug('Get relevant info..');
6249     END IF;
6250 
6251     SELECT move_order_line_id
6252          , transaction_quantity
6253          , transaction_header_id
6254          , transaction_batch_id
6255          , transaction_batch_seq
6256     INTO   l_mmtt_line_id
6257          , l_mmtt_qty
6258          , l_txn_header_id
6259          , l_transaction_batch_id
6260          , l_transaction_batch_seq
6261     FROM   mtl_material_transactions_temp
6262     WHERE  transaction_temp_id = l_temp_id
6263     AND    organization_id = l_org_id;
6264 
6265     IF (l_debug = 1) THEN
6266       mydebug('Line id' || l_mmtt_line_id);
6267       mydebug('temp id' || l_temp_id);
6268     END IF;
6269 
6270     -- Insert into WMS_DISPATCHED_TASKS_HISTORY with a status of 6, 'complete'
6271 
6272     l_return_status := fnd_api.g_ret_sts_success;
6273     wms_insert_wdth_pvt.insert_into_wdth
6274       ( x_return_status         => l_return_status
6275 	, p_txn_header_id         => l_txn_header_id
6276 	, p_transaction_temp_id   => l_temp_id
6277 	, p_transaction_batch_id  => l_transaction_batch_id
6278 	, p_transaction_batch_seq => l_transaction_batch_seq
6279 	, p_transfer_lpn_id       => NULL
6280 	);
6281 
6282     IF l_return_status <> fnd_api.g_ret_sts_success
6283       THEN
6284        IF l_debug = 1 THEN
6285 	  mydebug ('Error from wms_insert_wdth_pvt.insert_into_wdth');
6286        END IF;
6287        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6288     END IF;
6289 
6290     IF (l_debug = 1) THEN
6291       mydebug('After wmsdt update');
6292     END IF;
6293 
6294     -- Delete this row from wms_dispatched_tasks
6295     DELETE      wms_dispatched_tasks
6296           WHERE transaction_temp_id = l_temp_id;
6297 
6298     -- Delete lot and serial records
6299     BEGIN
6300       DELETE mtl_serial_numbers_temp
6301       WHERE  transaction_temp_id = (SELECT serial_transaction_temp_id
6302                                     FROM   mtl_transaction_lots_temp
6303                                     WHERE  transaction_temp_id = l_temp_id);
6304     EXCEPTION
6305       WHEN NO_DATA_FOUND THEN
6306         NULL;
6307     END;
6308 
6309     BEGIN
6310       DELETE mtl_transaction_lots_temp
6311       WHERE  transaction_temp_id = l_temp_id;
6312     EXCEPTION
6313       WHEN NO_DATA_FOUND THEN
6314         NULL;
6315     END;
6316 
6317     BEGIN
6318       DELETE mtl_serial_numbers_temp
6319       WHERE  transaction_temp_id = l_temp_id;
6320     EXCEPTION
6321       WHEN NO_DATA_FOUND THEN
6322         NULL;
6323     END;
6324 
6325     -- Delete this row from mmtt
6326     DELETE mtl_material_transactions_temp
6327     WHERE  transaction_temp_id = l_temp_id;
6328 
6329     x_return_status := fnd_api.g_ret_sts_success;
6330 
6331     IF (l_debug = 1) THEN
6332       mydebug('done WITH API');
6333     END IF;
6334   EXCEPTION
6335     WHEN OTHERS THEN
6336       x_return_status := fnd_api.g_ret_sts_error;
6337       fnd_message.set_name('WMS', 'WMS_TD_AT_FAIL');
6338       fnd_msg_pub.ADD;
6339       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6340   END archive_task;
6341 
6342   PROCEDURE archive_task(
6343     p_temp_id           IN             NUMBER
6344   , p_org_id            IN             NUMBER
6345   , x_return_status     OUT NOCOPY     VARCHAR2
6346   , x_msg_count         OUT NOCOPY     NUMBER
6347   , x_msg_data          OUT NOCOPY     VARCHAR2
6348   , p_delete_mmtt_flag  IN             VARCHAR2
6349   , p_txn_header_id     IN             NUMBER
6350   , p_transfer_lpn_id   IN             NUMBER DEFAULT NULL
6351   ) IS
6352     l_temp_id               NUMBER;
6353     l_org_id                NUMBER;
6354     l_return_status         VARCHAR2(1);
6355     l_msg_count             NUMBER;
6356     l_msg_data              VARCHAR2(2000);
6357     l_transaction_batch_id  NUMBER;
6358     l_transaction_batch_seq NUMBER;
6359     l_debug                 NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6360   BEGIN
6361     IF (l_debug = 1) THEN
6362       mydebug('archive_task: In archive task..' || p_temp_id || ':' || p_org_id);
6363     END IF;
6364 
6365     l_temp_id := p_temp_id;
6366     l_org_id := p_org_id;
6367     l_return_status := fnd_api.g_ret_sts_success;
6368 
6369 
6370     /* Bug 3961107-Modified the query to select transaction_batch_id and transaction_batch_seq
6371        as null if they have null values in the table.
6372     SELECT NVL(transaction_batch_id, -999)
6373          , NVL(transaction_batch_seq, -999)*/
6374     SELECT transaction_batch_id,
6375            transaction_batch_seq
6376     --End of fix for Bug 3961107
6377     INTO   l_transaction_batch_id
6378          , l_transaction_batch_seq
6379     FROM   mtl_material_transactions_temp
6380     WHERE  transaction_temp_id = l_temp_id
6381     AND    organization_id = l_org_id;
6382 
6383     IF (l_debug = 1) THEN
6384       mydebug('Temp ID: ' || l_temp_id);
6385       mydebug('Batch ID: ' || l_transaction_batch_id);
6386       mydebug('Batch Seq: ' || l_transaction_batch_seq);
6387     END IF;
6388 
6389     -- Insert into WMS_DISPATCHED_TASKS_HISTORY with a status of 6, 'complete'
6390 
6391     l_return_status := fnd_api.g_ret_sts_success;
6392     wms_insert_wdth_pvt.insert_into_wdth
6393       ( x_return_status         => l_return_status
6394 	, p_txn_header_id         => p_txn_header_id
6395 	, p_transaction_temp_id   => l_temp_id
6396 	, p_transaction_batch_id  => l_transaction_batch_id
6397 	, p_transaction_batch_seq => l_transaction_batch_seq
6398 	, p_transfer_lpn_id       => p_transfer_lpn_id
6399 	);
6400 
6401     IF l_return_status <> fnd_api.g_ret_sts_success
6402       THEN
6403        IF l_debug = 1 THEN
6404 	  mydebug ('Error from wms_insert_wdth_pvt.insert_into_wdth');
6405        END IF;
6406        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6407     END IF;
6408 
6409     -- Delete this row from wms_dispatched_tasks
6410     DELETE FROM wms_dispatched_tasks
6411           WHERE transaction_temp_id = l_temp_id;
6412 
6413     IF (l_debug = 1) THEN
6414       mydebug('archive_task: After wmsdt update');
6415     END IF;
6416 
6417     IF p_delete_mmtt_flag = 'Y' THEN
6418       -- Delete lot and serial records
6419       BEGIN
6420         DELETE mtl_serial_numbers_temp
6421         WHERE  transaction_temp_id IN
6422           (SELECT serial_transaction_temp_id
6423            FROM   mtl_transaction_lots_temp
6424            WHERE  transaction_temp_id = l_temp_id);
6425       EXCEPTION
6426         WHEN OTHERS THEN
6427           mydebug('archive_task: Error deleting MSNT for MTLT: ' || sqlerrm);
6428           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6429       END;
6430 
6431       BEGIN
6432         DELETE  mtl_transaction_lots_temp
6433         WHERE   transaction_temp_id = l_temp_id;
6434       EXCEPTION
6435         WHEN OTHERS THEN
6436           mydebug('archive_task: Error deleting MTLT: ' || sqlerrm);
6437           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6438       END;
6439 
6440       BEGIN
6441         DELETE  mtl_serial_numbers_temp
6442         WHERE   transaction_temp_id = l_temp_id;
6443       EXCEPTION
6444         WHEN OTHERS THEN
6445           mydebug('archive_task: Error deleting MSNT: ' || sqlerrm);
6446           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6447       END;
6448 
6449       BEGIN
6450         DELETE  mtl_material_transactions_temp
6451         WHERE   transaction_temp_id = l_temp_id;
6452       EXCEPTION
6453         WHEN OTHERS THEN
6454           mydebug('archive_task: Error deleting MMTT: ' || sqlerrm);
6455           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6456       END;
6457     END IF;
6458 
6459     x_return_status := fnd_api.g_ret_sts_success;
6460 
6461     IF (l_debug = 1) THEN
6462       mydebug('archive_task: done WITH API');
6463     END IF;
6464   EXCEPTION
6465     WHEN OTHERS THEN
6466       IF (l_debug = 1) THEN
6467          mydebug('Other exception occurred: ' || sqlerrm);
6468       END IF;
6469 
6470       x_return_status := fnd_api.g_ret_sts_error;
6471       fnd_message.set_name('WMS', 'WMS_TD_AT_FAIL');
6472       fnd_msg_pub.ADD;
6473       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6474   END archive_task;
6475 
6476   PROCEDURE putaway_cleanup(
6477     p_temp_id        IN             NUMBER
6478   , p_org_id         IN             NUMBER
6479   , x_return_status  OUT NOCOPY     VARCHAR2
6480   , x_msg_count      OUT NOCOPY     NUMBER
6481   , x_msg_data       OUT NOCOPY     VARCHAR2
6482   ) IS
6483     l_temp_id                 NUMBER;
6484     l_txn_header_id           NUMBER;
6485     l_org_id                  NUMBER;
6486     l_return_status           VARCHAR2(1);
6487     l_msg_count               NUMBER;
6488     l_msg_data                VARCHAR2(2000);
6489     l_mmtt_line_id            NUMBER;
6490     l_mmtt_qty                NUMBER;
6491     l_item_id                 NUMBER;
6492     l_person_id               NUMBER;
6493     l_loc_id                  NUMBER;
6494     l_sub                     VARCHAR2(10);
6495 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
6496     l_lot                     VARCHAR2(80);
6497     l_rev                     VARCHAR2(3);
6498     l_last_updated_by         NUMBER;
6499     l_sequence                NUMBER;
6500     cnt                       NUMBER         := 0;
6501     l_back_id                 NUMBER         := 0;
6502     l_crdk_type               NUMBER         := 0;
6503     l_demand_source_header_id NUMBER         := -1;
6504     l_repetitive_line_id      NUMBER         := -1;
6505     l_operation_seq_num       NUMBER         := -1;
6506     l_primary_qty             NUMBER         := -1;
6507     l_debug                   NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6508     -- Following variables added in ATF_J4
6509 
6510 
6511     l_secondary_quantity  NUMBER; --OPM Convergence
6512     -- End variables added in ATF_J4
6513 
6514     -- Bug# 3281512 - Performance Fixes
6515     -- Add cursor to get serial_transaction_temp_id values for performance.
6516     -- SQL compiler complains of a Hash Join otherwise.
6517     CURSOR c_serial_transaction_temp_id IS
6518        SELECT serial_transaction_temp_id
6519 	 FROM mtl_transaction_lots_temp
6520 	 WHERE transaction_temp_id = l_temp_id;
6521     l_serial_transaction_temp_id   NUMBER;
6522     -- Bug# 3434940 - Performance Fixes
6523     -- Add cursor to get the serial number(s) for a given MSNT record.
6524     -- SQL compiler complains of a Hash Join otherwise.
6525     CURSOR c_serial_number(v_transaction_temp_id NUMBER) IS
6526        SELECT fm_serial_number
6527 	 FROM   mtl_serial_numbers_temp
6528 	 WHERE  transaction_temp_id = v_transaction_temp_id;
6529     l_fm_serial_number NUMBER;
6530 
6531   BEGIN
6532     IF (l_debug = 1) THEN
6533       mydebug('In putaway cleanup..');
6534     END IF;
6535 
6536     l_temp_id := p_temp_id;
6537     l_org_id := p_org_id;
6538     l_return_status := fnd_api.g_ret_sts_success;
6539 
6540     IF (l_debug = 1) THEN
6541       mydebug('Get relevant info..');
6542     END IF;
6543 
6544     SELECT t.move_order_line_id
6545          , t.transaction_quantity
6546          , t.inventory_item_id
6547          , t.primary_quantity
6548          , w.person_id
6549          , t.locator_id
6550          , t.subinventory_code
6551          , t.lot_number
6552          , t.revision
6553          , t.last_updated_by
6554          , t.demand_source_header_id
6555          , t.repetitive_line_id
6556          , t.operation_seq_num
6557          , t.secondary_transaction_quantity  --OPM Convergence
6558     INTO   l_mmtt_line_id
6559          , l_mmtt_qty
6560          , l_item_id
6561          , l_primary_qty
6562          , l_person_id
6563          , l_loc_id
6564          , l_sub
6565          , l_lot
6566          , l_rev
6567          , l_last_updated_by
6568          , l_demand_source_header_id
6569          , l_repetitive_line_id
6570          , l_operation_seq_num
6571          , l_secondary_quantity --OPM Convergence
6572     FROM   mtl_material_transactions_temp t, wms_dispatched_tasks w
6573     WHERE  t.transaction_temp_id = l_temp_id
6574     AND    t.organization_id = l_org_id
6575     AND    t.transaction_temp_id = w.transaction_temp_id;
6576 
6577     IF (l_debug = 1) THEN
6578       mydebug('Line id' || l_mmtt_line_id);
6579     END IF;
6580 
6581     -- Log exception
6582 
6583     --Calculate Sequence Number
6584     SELECT wms_exceptions_s.NEXTVAL
6585     INTO   l_sequence
6586     FROM   DUAL;
6587 
6588     IF (l_debug = 1) THEN
6589       mydebug('Inserting into exceptions');
6590       mydebug(l_sequence);
6591     END IF;
6592 
6593     INSERT INTO wms_exceptions
6594                 (
6595                  task_id
6596                , sequence_number
6597                , organization_id
6598                , inventory_item_id
6599                , person_id
6600                , effective_start_date
6601                , effective_end_date
6602                , inventory_location_id
6603                , reason_id
6604                , discrepancy_type
6605                , subinventory_code
6606                , lot_number
6607                , revision
6608                , last_update_date
6609                , last_updated_by
6610                , creation_date
6611                , created_by
6612                 )
6613     VALUES      (
6614                  l_temp_id
6615                , l_sequence
6616                , l_org_id
6617                , l_item_id
6618                , l_person_id
6619                , SYSDATE
6620                , SYSDATE
6621                , l_loc_id
6622                , -999
6623                , 2
6624                , l_sub
6625                , l_lot
6626                , l_rev
6627                , SYSDATE
6628                , l_last_updated_by
6629                , SYSDATE
6630                , l_last_updated_by
6631                 );
6632 
6633     -- ATF_J4
6634     -- If current release is J or above, do not
6635     -- update move order line, delete MMTT and revert crossdocking.
6636     -- Because cleanup can happen at an operation in the middle of
6637     -- an operation plan.
6638     -- If MMTT is not tied to an active operation plan,
6639     -- delete MMTT and update move order line will happen in suggestions_PUB;
6640     -- Not reverting crossdocking is probably not optimal, but safe.
6641 
6642 
6643   -- Cleanup the serials before Deleting MMTT
6644     IF (l_debug = 1) THEN
6645       mydebug('Updating Group_mark_id for Serials ');
6646     END IF;
6647 
6648     -- Update for Lot/Serial controlled item
6649     -- Bug# 3281512 - Performance Fixes
6650     -- Use the c_serial_transaction_temp_id cursor to avoid the hash join.
6651     -- Also match against MSN using the item and the org to use the indexes more efficiently.
6652     -- Bug# 3434940 - Performance Fixes
6653     -- Also create and use another cursor, c_serial_number to avoid the
6654     -- hash join.  This cursor will loop through the serials in MSNT for
6655     -- the given transaction temp id.
6656     OPEN c_serial_transaction_temp_id;
6657     LOOP
6658        FETCH c_serial_transaction_temp_id INTO l_serial_transaction_temp_id;
6659        EXIT WHEN c_serial_transaction_temp_id%NOTFOUND;
6660 
6661        OPEN c_serial_number(l_serial_transaction_temp_id);
6662        LOOP
6663 	  FETCH c_serial_number INTO l_fm_serial_number;
6664 	  EXIT WHEN c_serial_number%NOTFOUND;
6665 
6666 	  UPDATE mtl_serial_numbers
6667 	    SET    group_mark_id = NULL
6668 	    WHERE  serial_number = l_fm_serial_number
6669 	    AND    inventory_item_id = l_item_id
6670 	    AND    current_organization_id = l_org_id;
6671 
6672        END LOOP;
6673        CLOSE c_serial_number;
6674 
6675     END LOOP;
6676     CLOSE c_serial_transaction_temp_id;
6677 
6678     -- Update for Serial controlled item
6679     -- Bug# 3281512 - Performance Fixes
6680     -- Match against MSN using the item and org to use the indexes more efficiently.
6681     -- Bug# 3434940 - Performance Fixes
6682     -- Also create and use another cursor, c_serial_number to avoid the
6683     -- hash join.  This cursor will loop through the serials in MSNT for
6684     -- the given transaction temp id.
6685     OPEN c_serial_number(l_temp_id);
6686     LOOP
6687        FETCH c_serial_number INTO l_fm_serial_number;
6688        EXIT WHEN c_serial_number%NOTFOUND;
6689 
6690        UPDATE mtl_serial_numbers
6691 	 SET    group_mark_id = NULL
6692 	 WHERE  serial_number = l_fm_serial_number
6693 	 AND    inventory_item_id = l_item_id
6694 	 AND    current_organization_id = l_org_id;
6695 
6696     END LOOP;
6697     CLOSE c_serial_number;
6698 
6699     -- Bug 2458540
6700     -- Reset the Status Back if putaway fails
6701     -- For cases with RMA and serial at sales order Issue
6702 
6703     -- Update for Lot/Serial controlled item
6704     -- Bug# 3281512 - Performance Fixes
6705     -- Use the cursor to avoid the hash join.  Also match against MSN using
6706     -- the org to use the indexes more efficiently.
6707     -- Bug# 3434940 - Performance Fixes
6708     -- Also create and use another cursor, c_serial_number to avoid the
6709     -- hash join.  This cursor will loop through the serials in MSNT for
6710     -- the given transaction temp id.
6711     OPEN c_serial_transaction_temp_id;
6712     LOOP
6713        FETCH c_serial_transaction_temp_id INTO l_serial_transaction_temp_id;
6714        EXIT WHEN c_serial_transaction_temp_id%NOTFOUND;
6715 
6716        OPEN c_serial_number(l_serial_transaction_temp_id);
6717        LOOP
6718 	  FETCH c_serial_number INTO l_fm_serial_number;
6719 	  EXIT WHEN c_serial_number%NOTFOUND;
6720 
6721 	  UPDATE mtl_serial_numbers
6722 	    SET    current_status = 5,
6723   	           previous_status = NULL
6724 	    WHERE  serial_number = l_fm_serial_number
6725 	    AND    inventory_item_id = l_item_id
6726 	    AND    lot_number = l_lot
6727 	    AND    current_status = 4
6728 	    AND    current_organization_id = l_org_id
6729 	    AND    EXISTS (SELECT 1
6730 			   FROM   mtl_txn_request_lines mol
6731 			   WHERE  mol.line_id = l_mmtt_line_id
6732 			   AND    mol.REFERENCE = 'ORDER_LINE_ID');
6733        END LOOP;
6734        CLOSE c_serial_number;
6735 
6736     END LOOP;
6737     CLOSE c_serial_transaction_temp_id;
6738 
6739     -- Update for Serial controlled item
6740     -- Bug# 3281512 - Performance Fixes
6741     -- Match against MSN using the item and org to use the indexes more efficiently.
6742     -- Bug# 3434940 - Performance Fixes
6743     -- Also create and use another cursor, c_serial_number to avoid the
6744     -- hash join.  This cursor will loop through the serials in MSNT for
6745     -- the given transaction temp id.
6746     OPEN c_serial_number(l_temp_id);
6747     LOOP
6748        FETCH c_serial_number INTO l_fm_serial_number;
6749        EXIT WHEN c_serial_number%NOTFOUND;
6750 
6751        UPDATE mtl_serial_numbers
6752 	 SET current_status = 5,
6753 	     previous_status = NULL
6754 	 WHERE  serial_number = l_fm_serial_number
6755 	 AND    inventory_item_id = l_item_id
6756 	 AND    current_status = 4
6757 	 AND    current_organization_id = l_org_id
6758 	 AND    EXISTS (SELECT 1
6759 			FROM   mtl_txn_request_lines mol
6760 			WHERE  mol.line_id = l_mmtt_line_id
6761 			AND    mol.REFERENCE = 'ORDER_LINE_ID');
6762     END LOOP;
6763     CLOSE c_serial_number;
6764 
6765 
6766     x_return_status := l_return_status;
6767 
6768     --x_return_status:=FND_API.G_RET_STS_SUCCESS;
6769     IF (l_debug = 1) THEN
6770       mydebug('done WITH API');
6771     END IF;
6772   EXCEPTION
6773     WHEN OTHERS THEN
6774       x_return_status := fnd_api.g_ret_sts_error;
6775       fnd_message.set_name('WMS', 'WMS_TD_PUT_CLEAN_FAIL');
6776       fnd_msg_pub.ADD;
6777       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6778   END putaway_cleanup;
6779 
6780   PROCEDURE validate_putaway_to_lpn(
6781     p_org_id         IN             NUMBER
6782   , p_to_lpn         IN             VARCHAR2
6783   , p_from_lpn       IN             VARCHAR2
6784   , p_sub            IN             VARCHAR2
6785   , p_loc_id         IN             NUMBER
6786   , x_return_status  OUT NOCOPY     VARCHAR2
6787   , x_msg_count      OUT NOCOPY     NUMBER
6788   , x_msg_data       OUT NOCOPY     VARCHAR2
6789   , x_return         OUT NOCOPY     NUMBER
6790   , p_crossdock      IN             VARCHAR2 DEFAULT NULL
6791   ) IS
6792     l_count       NUMBER;
6793     l_to_lpn_id   NUMBER;
6794     l_lpn_context NUMBER;
6795     l_quantity    NUMBER;
6796     l_debug       NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6797 
6798     -- Bug 4411792
6799     l_lpn_update  WMS_CONTAINER_PUB.LPN;
6800     l_return_status           VARCHAR2(1);
6801     l_msg_count               NUMBER;
6802     l_msg_data                VARCHAR2(2000);
6803 
6804 
6805   BEGIN
6806     IF (l_debug = 1) THEN
6807       mydebug('validate_putaway_to_lpn : validate_putaway_to_lpn Entered');
6808     END IF;
6809 
6810     x_return := 0;
6811     l_count := 0;
6812     x_return_status := fnd_api.g_ret_sts_success;
6813 
6814     BEGIN
6815       SELECT 1
6816            , lpn_id
6817       INTO   l_count
6818            , l_to_lpn_id
6819       FROM   wms_license_plate_numbers wlpn
6820       WHERE  wlpn.license_plate_number = p_to_lpn
6821       AND    wlpn.organization_id = p_org_id;
6822     EXCEPTION
6823       WHEN NO_DATA_FOUND THEN
6824         l_count := 0;
6825     END;
6826 
6827     IF l_count = 0 THEN
6828       -- New LPN
6829       IF (l_debug = 1) THEN
6830         mydebug('validate_putaway_to_lpn : LPN does not exist. Create new LPN');
6831       END IF;
6832 
6833       wms_task_dispatch_gen.create_lpn(
6834         p_organization_id     => p_org_id
6835       , p_lpn                 => p_to_lpn
6836       , p_lpn_id              => l_to_lpn_id
6837       , x_return_status       => x_return_status
6838       , x_msg_count           => x_msg_count
6839       , x_msg_data            => x_msg_data
6840       );
6841 
6842       IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6843         fnd_message.set_name('WMS', 'WMS_TD_CREATE_LPN_ERROR');
6844         fnd_msg_pub.ADD;
6845         RAISE fnd_api.g_exc_unexpected_error;
6846       ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
6847         fnd_message.set_name('WMS', 'WMS_TD_CREATE_LPN_ERROR');
6848         fnd_msg_pub.ADD;
6849         RAISE fnd_api.g_exc_error;
6850       END IF;
6851 
6852       x_return := 1;
6853     ELSE
6854       IF (l_debug = 1) THEN
6855         mydebug('validate_putaway_to_lpn : LPN already exists. Check to see if the LPN resides in the sub/loc');
6856         mydebug('From LPN ID: ' || p_from_lpn || ' To LPN ID: ' || p_to_lpn);
6857       END IF;
6858 
6859       -- Check from LPN and to LPN match
6860       -- bug fix 2271470
6861 
6862       IF p_from_lpn <> p_to_lpn THEN
6863         -- Bug Fix 2505636
6864         IF p_crossdock = 'Y' THEN
6865           IF (l_debug = 1) THEN
6866             mydebug('LPN crossdocked and from and to lpn different');
6867           END IF;
6868 
6869           BEGIN
6870             -- Check the LPN Content it should be null
6871             SELECT quantity
6872             INTO   l_quantity
6873             FROM   wms_lpn_contents
6874             WHERE  parent_lpn_id = l_to_lpn_id
6875             AND    ROWNUM < 2;
6876 
6877             --
6878             IF l_quantity >= 0 THEN
6879               IF (l_debug = 1) THEN
6880                 mydebug('LPN has contents so can not putaway');
6881               END IF;
6882 
6883               x_return := 0;
6884               RETURN;
6885             END IF;
6886           EXCEPTION
6887             WHEN NO_DATA_FOUND THEN
6888               IF (l_debug = 1) THEN
6889                 mydebug('LPN has no contents so should be OK to putaway');
6890               END IF;
6891           END;
6892         END IF;
6893 
6894         l_count := 0;
6895 
6896         BEGIN
6897           SELECT 1
6898           INTO   l_count
6899           FROM   DUAL
6900           WHERE  EXISTS(
6901                    SELECT 1
6902                    FROM   mtl_material_transactions_temp mmtt
6903                    WHERE  (
6904                            NVL(mmtt.subinventory_code, '@') <> NVL(p_sub, NVL(mmtt.subinventory_code, '@'))
6905                            OR NVL(mmtt.locator_id, '0') <> NVL(p_loc_id, NVL(mmtt.locator_id, '0'))
6906                           )
6907                    AND    mmtt.organization_id = p_org_id
6908                    AND    mmtt.transaction_status = 3
6909                    AND    mmtt.transfer_lpn_id = l_to_lpn_id);
6910         EXCEPTION
6911           WHEN NO_DATA_FOUND THEN
6912             l_count := 0;
6913         END;
6914 
6915         IF l_count > 0 THEN
6916           x_return := 0;
6917 
6918           IF (l_debug = 1) THEN
6919             mydebug('validate_putaway_to_lpn: LPN has a pending transaction going to different location');
6920           END IF;
6921 
6922           RETURN;
6923         END IF;
6924 
6925         IF (l_debug = 1) THEN
6926           mydebug('p_org_id : ' || p_org_id);
6927           mydebug('p_to_lpn : ' || p_to_lpn);
6928           mydebug('p_sub : ' || p_sub);
6929           mydebug('p_loc_id : ' || p_loc_id);
6930           mydebug('p_from_lpn : ' || p_from_lpn);
6931         END IF;
6932 
6933         BEGIN
6934           SELECT 1
6935           INTO   l_count
6936           FROM   DUAL
6937           WHERE  EXISTS(
6938                    SELECT 1
6939                    FROM   wms_license_plate_numbers wlpn
6940                    WHERE  wlpn.organization_id = p_org_id
6941                    AND    license_plate_number = p_to_lpn
6942                    AND    (
6943                            wlpn.lpn_context = 5
6944                            OR(wlpn.lpn_context = 1
6945                               AND wlpn.subinventory_code IS NULL
6946                               AND wlpn.locator_id IS NULL)
6947                            OR(
6948                               wlpn.lpn_context = 1
6949                               AND NVL(wlpn.subinventory_code, '@') = NVL(p_sub, NVL(wlpn.subinventory_code, '@'))
6950                               AND NVL(wlpn.locator_id, '0') = NVL(p_loc_id, NVL(wlpn.locator_id, '0'))
6951                               AND NOT wlpn.license_plate_number = NVL(p_from_lpn, -999)
6952                               AND inv_material_status_grp.is_status_applicable(
6953                                    'TRUE'
6954                                  , NULL
6955                                  , inv_globals.g_type_container_pack
6956                                  , NULL
6957                                  , NULL
6958                                  , p_org_id
6959                                  , NULL
6960                                  , wlpn.subinventory_code
6961                                  , wlpn.locator_id
6962                                  , NULL
6963                                  , NULL
6964                                  , 'Z'
6965                                  ) = 'Y'
6966                               AND inv_material_status_grp.is_status_applicable(
6967                                    'TRUE'
6968                                  , NULL
6969                                  , inv_globals.g_type_container_pack
6970                                  , NULL
6971                                  , NULL
6972                                  , p_org_id
6973                                  , NULL
6974                                  , wlpn.subinventory_code
6975                                  , wlpn.locator_id
6976                                  , NULL
6977                                  , NULL
6978                                  , 'L'
6979                                  ) = 'Y'
6980                              )
6981                           ));
6982         EXCEPTION
6983           WHEN NO_DATA_FOUND THEN
6984             l_count := 0;
6985 
6986             IF (l_debug = 1) THEN
6987               mydebug('validate_putaway_to_lpn: No data found');
6988             END IF;
6989         END;
6990 
6991         IF l_count > 0 THEN
6992           x_return := 1;
6993 
6994           IF (l_debug = 1) THEN
6995             mydebug('validate_putaway_to_lpn: LPN exists in the sub/loc or is defined but not used');
6996           END IF;
6997         ELSE
6998           x_return := 0;
6999 
7000           IF (l_debug = 1) THEN
7001             mydebug('validate_putaway_to_lpn: LPN does not exist in the sub/loc');
7002           END IF;
7003         END IF;
7004       -- bug fix 2271470
7005       ELSE -- p_from_lpn = p_to_lpn
7006         BEGIN
7007           SELECT COUNT(1)
7008           INTO   l_count
7009           FROM   mtl_txn_request_lines mol
7010           WHERE  mol.lpn_id = l_to_lpn_id
7011           AND    mol.line_status <> inv_globals.g_to_status_closed
7012           AND    (
7013                   (mol.quantity - NVL(mol.quantity_delivered, 0)) > (SELECT SUM(mmtt.transaction_quantity)
7014                                                                      FROM   mtl_material_transactions_temp mmtt
7015                                                                      WHERE  mmtt.move_order_line_id = mol.line_id)
7016                   OR(mol.quantity - NVL(mol.quantity_delivered, 0) > 0
7017                      AND NOT EXISTS(SELECT 1
7018                                     FROM   mtl_material_transactions_temp mmtt
7019                                     WHERE  mmtt.move_order_line_id = mol.line_id))
7020                  );
7021 
7022           IF (l_debug = 1) THEN
7023             mydebug('l_count = ' || l_count);
7024           END IF;
7025         EXCEPTION
7026           WHEN NO_DATA_FOUND THEN
7027             l_count := 0;
7028         END;
7029 
7030         IF l_count > 0 THEN
7031           x_return := 0;
7032 
7033           IF (l_debug = 1) THEN
7034             mydebug(
7035               'Validate_putaway_to_lpn: LPN has a pending transaction with quantity less then the difference between total quantity on MOL and quantity delivered. Cannot putaway this LPN.'
7036             );
7037           END IF;
7038 
7039           RETURN;
7040         ELSE
7041           x_return := 1;
7042           RETURN;
7043         END IF;
7044       END IF;
7045     END IF;
7046 
7047     SELECT wlc.lpn_context
7048     INTO   l_lpn_context
7049     FROM   wms_license_plate_numbers wlc
7050     WHERE  wlc.license_plate_number = p_to_lpn
7051     AND    wlc.organization_id = p_org_id;
7052 
7053     IF (l_debug = 1) THEN
7054       mydebug('lpn_context = ' || l_lpn_context);
7055     END IF;
7056 
7057     IF l_lpn_context = wms_container_pub.lpn_context_pregenerated THEN
7058       IF (l_debug = 1) THEN
7059         mydebug('update LPN context to 1');
7060       END IF;
7061 
7062       -- Bug 4411792
7063       l_lpn_update.license_plate_number      :=  p_to_lpn;
7064       l_lpn_update.organization_id           :=  p_org_id;
7065       l_lpn_update.lpn_context               := wms_container_pub.lpn_context_inv;
7066 
7067       wms_container_pvt.Modify_LPN
7068              (
7069                p_api_version             => 1.0
7070                , p_validation_level      => fnd_api.g_valid_level_none
7071                , x_return_status         => l_return_status
7072                , x_msg_count             => l_msg_count
7073                , x_msg_data              => l_msg_data
7074                , p_lpn                   => l_lpn_update
7075       ) ;
7076 
7077       l_lpn_update := NULL;
7078 
7079       -- 4411792 The below is replaced by the above API call
7080       --UPDATE wms_license_plate_numbers
7081       --SET lpn_context = wms_container_pub.lpn_context_inv
7082       --WHERE  license_plate_number = p_to_lpn
7083       --AND    organization_id = p_org_id;
7084 
7085     END IF;
7086 
7087     x_return_status := fnd_api.g_ret_sts_success;
7088   EXCEPTION
7089     WHEN OTHERS THEN
7090       IF (l_debug = 1) THEN
7091         mydebug('validate_putaway_to_lpn : Exception : ' || SQLERRM);
7092       END IF;
7093 
7094       x_return_status := fnd_api.g_ret_sts_error;
7095       fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN');
7096       fnd_msg_pub.ADD;
7097       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7098   END validate_putaway_to_lpn;
7099 
7100   /* This API will check the status of the mmtt lines
7101    This will be called from the suggestions_api, as part of the putaway
7102      process We need to do this at the mmtt line level rather than just
7103      checking the lpn contents because the transaction type id might differ
7104      in each MOL. Returns x_mtl_stat 0 if everything is fine, 1 otherwise*/
7105   PROCEDURE check_mmtt_mtl_status(
7106     p_temp_id        IN             VARCHAR2
7107   , p_org_id         IN             NUMBER
7108   , x_mtl_status     OUT NOCOPY     NUMBER
7109   , x_return_status  OUT NOCOPY     VARCHAR2
7110   , x_msg_count      OUT NOCOPY     NUMBER
7111   , x_msg_data       OUT NOCOPY     VARCHAR2
7112   ) IS
7113     l_temp_id          NUMBER;
7114     l_org_id           NUMBER;
7115     l_serial_temp_id   NUMBER;
7116     l_txn_type_id      NUMBER;
7117     l_ser_control_code NUMBER;
7118     l_lot_control_code NUMBER;
7119     l_item_id          NUMBER;
7120     l_serial_number    VARCHAR2(30);
7121 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
7122     l_lot_number       VARCHAR2(80);
7123     l_sub              VARCHAR2(30);
7124     l_loc              NUMBER;
7125     l_lpn_id           NUMBER;
7126 
7127     CURSOR lot_csr IS
7128       SELECT lot_number
7129            , serial_transaction_temp_id
7130       FROM   mtl_transaction_lots_temp
7131       WHERE  transaction_temp_id = l_temp_id;
7132 
7133     CURSOR ser_csr IS
7134       SELECT serial_number
7135       FROM   mtl_serial_numbers
7136       WHERE  lpn_id = l_lpn_id
7137       AND    inventory_item_id = l_item_id
7138       AND    current_organization_id = l_org_id
7139       AND    NVL(lot_number, -999) = NVL(l_lot_number, -999);
7140 
7141     l_debug            NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7142   BEGIN
7143     IF (l_debug = 1) THEN
7144       mydebug('in check_mmtt_mtl_status api');
7145       mydebug('p_temp_id => ' || p_temp_id);
7146       mydebug('p_org_id => ' || p_org_id);
7147     END IF;
7148 
7149     x_return_status := fnd_api.g_ret_sts_success;
7150     x_mtl_status := 0;
7151     l_temp_id := p_temp_id;
7152     l_org_id := p_org_id;
7153     l_lot_number := NULL;
7154 
7155     SELECT msi.lot_control_code
7156          , msi.serial_number_control_code
7157          , mmtt.transaction_type_id
7158          , mmtt.inventory_item_id
7159          , DECODE(mmtt.transaction_action_id, 2, mmtt.transfer_subinventory, subinventory_code) sub
7160          , DECODE(mmtt.transaction_action_id, 2, mmtt.transfer_to_location, locator_id)
7161          , lpn_id
7162     INTO   l_lot_control_code
7163          , l_ser_control_code
7164          , l_txn_type_id
7165          , l_item_id
7166          , l_sub
7167          , l_loc
7168          , l_lpn_id
7169     FROM   mtl_system_items msi, mtl_material_transactions_temp mmtt
7170     WHERE  mmtt.organization_id = l_org_id
7171     AND    mmtt.transaction_temp_id = l_temp_id
7172     AND    msi.organization_id = mmtt.organization_id
7173     AND    msi.inventory_item_id = mmtt.inventory_item_id;
7174 
7175     IF (l_debug = 1) THEN
7176       mydebug('check_mmtt_mtl_status: After getting relevant info..');
7177       mydebug('check_mmtt_mtl_status: Lot Code: ' || l_lot_control_code);
7178       mydebug('check_mmtt_mtl_status: Ser Code: ' || l_ser_control_code);
7179       mydebug('check_mmtt_mtl_status: Tran Type ID: ' || l_txn_type_id);
7180       mydebug('check_mmtt_mtl_status: Item: ' || l_item_id);
7181     END IF;
7182 
7183     IF l_lot_control_code > 1 THEN
7184       IF (l_debug = 1) THEN
7185         mydebug('check_mmtt_mtl_status: Lot controlled');
7186       END IF;
7187 
7188       -- lot controlled - CHECK lot status
7189       OPEN lot_csr;
7190 
7191       LOOP
7192         FETCH lot_csr INTO l_lot_number, l_serial_temp_id;
7193         EXIT WHEN lot_csr%NOTFOUND;
7194 
7195         IF l_ser_control_code > 1
7196            AND l_ser_control_code <> 6 THEN
7197           IF (l_debug = 1) THEN
7198             mydebug('check_mmtt_mtl_status: Lot and serial controlled');
7199           END IF;
7200 
7201           OPEN ser_csr;
7202 
7203           LOOP
7204             FETCH ser_csr INTO l_serial_number;
7205             EXIT WHEN ser_csr%NOTFOUND;
7206 
7207             IF inv_material_status_grp.is_status_applicable(
7208                  p_wms_installed             => 'TRUE'
7209                , p_trx_status_enabled        => NULL
7210                , p_trx_type_id               => l_txn_type_id
7211                , p_lot_status_enabled        => NULL
7212                , p_serial_status_enabled     => NULL
7213                , p_organization_id           => l_org_id
7214                , p_inventory_item_id         => l_item_id
7215                , p_sub_code                  => NULL
7216                , p_locator_id                => NULL
7217                , p_lot_number                => l_lot_number
7218                , p_serial_number             => l_serial_number
7219                , p_object_type               => 'A'
7220                ) = 'N' THEN
7221               IF (l_debug = 1) THEN
7222                 mydebug('check_mmtt_mtl_status: After 0');
7223               END IF;
7224 
7225               x_mtl_status := 1;
7226               RETURN;
7227             END IF;
7228           END LOOP;
7229 
7230           CLOSE ser_csr;
7231         ELSE
7232           IF inv_material_status_grp.is_status_applicable(
7233                p_wms_installed             => 'TRUE'
7234              , p_trx_status_enabled        => NULL
7235              , p_trx_type_id               => l_txn_type_id
7236              , p_lot_status_enabled        => NULL
7237              , p_serial_status_enabled     => NULL
7238              , p_organization_id           => l_org_id
7239              , p_inventory_item_id         => l_item_id
7240              , p_sub_code                  => NULL
7241              , p_locator_id                => NULL
7242              , p_lot_number                => l_lot_number
7243              , p_serial_number             => NULL
7244              , p_object_type               => 'O'
7245              ) = 'N' THEN
7246             IF (l_debug = 1) THEN
7247               mydebug('check_mmtt_mtl_status: After 1');
7248             END IF;
7249 
7250             x_mtl_status := 1;
7251             RETURN;
7252           END IF;
7253         END IF;
7254       END LOOP;
7255 
7256       CLOSE lot_csr;
7257     ELSIF l_ser_control_code > 1
7258           AND l_ser_control_code <> 6 THEN
7259       IF (l_debug = 1) THEN
7260         mydebug('check_mmtt_mtl_status: Serial controlled only');
7261       END IF;
7262 
7263       l_serial_temp_id := l_temp_id;
7264       -- serial controlled - CHECK serial status
7265       OPEN ser_csr;
7266 
7267       LOOP
7268         FETCH ser_csr INTO l_serial_number;
7269         EXIT WHEN ser_csr%NOTFOUND;
7270 
7271         IF inv_material_status_grp.is_status_applicable(
7272              p_wms_installed             => 'TRUE'
7273            , p_trx_status_enabled        => NULL
7274            , p_trx_type_id               => l_txn_type_id
7275            , p_lot_status_enabled        => NULL
7276            , p_serial_status_enabled     => NULL
7277            , p_organization_id           => l_org_id
7278            , p_inventory_item_id         => l_item_id
7279            , p_sub_code                  => NULL
7280            , p_locator_id                => NULL
7281            , p_lot_number                => NULL
7282            , p_serial_number             => l_serial_number
7283            , p_object_type               => 'S'
7284            ) = 'N' THEN
7285           IF (l_debug = 1) THEN
7286             mydebug('check_mmtt_mtl_status: After 6');
7287           END IF;
7288 
7289           x_mtl_status := 1;
7290           RETURN;
7291         END IF;
7292       END LOOP;
7293 
7294       CLOSE ser_csr;
7295     ELSE
7296       IF (l_debug = 1) THEN
7297         mydebug('check_mmtt_mtl_status: No controls');
7298       END IF;
7299 
7300       IF inv_material_status_grp.is_status_applicable(
7301            p_wms_installed             => 'TRUE'
7302          , p_trx_status_enabled        => NULL
7303          , p_trx_type_id               => l_txn_type_id
7304          , p_lot_status_enabled        => NULL
7305          , p_serial_status_enabled     => NULL
7306          , p_organization_id           => l_org_id
7307          , p_inventory_item_id         => l_item_id
7308          , p_sub_code                  => l_sub
7309          , p_locator_id                => l_loc
7310          , p_lot_number                => l_lot_number
7311          , p_serial_number             => NULL
7312          , p_object_type               => 'A'
7313          ) = 'N' THEN
7314         IF (l_debug = 1) THEN
7315           mydebug('check_mmtt_mtl_status: After Check');
7316         END IF;
7317 
7318         x_mtl_status := 1;
7319         RETURN;
7320       END IF;
7321 
7322       IF (l_debug = 1) THEN
7323         mydebug('check_mmtt_mtl_status: After Check');
7324       END IF;
7325 
7326       x_mtl_status := 0;
7327       RETURN;
7328     END IF;
7329 
7330     x_mtl_status := 0;
7331   END check_mmtt_mtl_status;
7332 
7333   -- added for bug 2271470
7334 
7335   PROCEDURE cleanup_partial_putaway_lpn(
7336     x_return_status  OUT NOCOPY     VARCHAR2
7337   , x_msg_count      OUT NOCOPY     NUMBER
7338   , x_msg_data       OUT NOCOPY     VARCHAR2
7339   , p_lpn_id         IN             NUMBER
7340   ) IS
7341     l_lpn_context NUMBER;
7342     PRAGMA AUTONOMOUS_TRANSACTION;
7343     l_debug       NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7344 
7345     -- Following variables added in ATF_J3
7346 
7347     -- End variables added in ATF_J3
7348 
7349     -- Bug# 3281512 - Performance Fixes
7350     -- Cursor added to retrieve valid MO header IDs so we can avoid
7351     -- the hash join problem.
7352     CURSOR c_header_id IS
7353        SELECT   moh.header_id
7354 	 FROM   mtl_txn_request_headers moh, mtl_txn_request_lines mol
7355 	 WHERE  moh.move_order_type = inv_globals.g_move_order_put_away
7356 	 AND    moh.header_id = mol.header_id
7357 	 AND    mol.lpn_id = p_lpn_id;
7358     l_header_id  NUMBER;
7359 
7360   BEGIN
7361      x_return_status := fnd_api.g_ret_sts_success;
7362 
7363      -- ATF_J3
7364      -- Skip cleanup_partial_putaway_lpn logic
7365      -- if current release is J or above.
7366      -- We take care of cleaning up MMTT in suggestions_PUB
7367 
7368      IF (l_debug = 1) THEN
7369 	mydebug('cleanup_partial_putaway_LPN: Current release is above J, return without doing anything.  p_lpn_id : ' || p_lpn_id);
7370      END IF;
7371 
7372      RETURN;
7373 
7374   EXCEPTION
7375     WHEN OTHERS THEN
7376       x_return_status := fnd_api.g_ret_sts_unexp_error;
7377 
7378       IF (l_debug = 1) THEN
7379         mydebug('cleanup_partial_putaway_LPN  upexpected error: ' || SQLCODE);
7380       END IF;
7381   END cleanup_partial_putaway_lpn;
7382 
7383   /**
7384     * Inbound  -  Nested LPN changes
7385     * Creates Move order line by exploding the given lpn
7386     */
7387 
7388 
7389   PROCEDURE create_mo_lpn(
7390     p_lpn_id         IN NUMBER
7391   , p_org_id         IN NUMBER
7392   , x_return_status  OUT NOCOPY  VARCHAR2
7393   , x_msg_count      OUT NOCOPY  NUMBER
7394   , x_msg_data       OUT NOCOPY  VARCHAR2
7395   ) IS
7396     l_lpn_id                       NUMBER;
7397     l_m_item                       NUMBER;
7398     l_m_qty                        NUMBER;
7399     l_m_uom                        VARCHAR2(3);
7400 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
7401     l_m_lot                        VARCHAR2(80);
7402     l_m_rev                        VARCHAR2(3);
7403     l_m_hdr                        NUMBER;
7404     l_m_line                       NUMBER;
7405     l_m_sub                        VARCHAR2(10);
7406     l_m_loc                        NUMBER;
7407     l_return_status                VARCHAR2(1);
7408     l_msg_count                    NUMBER;
7409     l_msg_data                     VARCHAR2(2000);
7410     l_lpn_cg_id                    NUMBER;
7411     l_debug                        NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7412     l_project_id                   NUMBER;
7413     l_task_id                      NUMBER;
7414 
7415     -- Bug# 3434940 - Performance Fixes
7416     -- Create a cursor to loop through all the nested LPNs within
7417     -- the given outer LPN, p_lpn_id.
7418     CURSOR lpn_csr IS
7419        SELECT lpn_id
7420 	 FROM wms_license_plate_numbers
7421 	 START WITH lpn_id = p_lpn_id
7422 	 CONNECT BY PRIOR lpn_id = parent_lpn_id;
7423     l_current_lpn_id     NUMBER;
7424 
7425     -- Bug# 3434940 - Performance Fixes
7426     -- Rename this cursor to lpn_contents_csr.  Do not loop through the
7427     -- nested LPNs here.  We will open a seperate outer cursor called
7428     -- lpn_csr to do that.
7429     CURSOR lpn_contents_csr IS
7430        SELECT inventory_item_id
7431             , quantity
7432             , uom_code
7433             , lot_number
7434             , revision
7435             , cost_group_id
7436             , parent_lpn_id
7437 	 FROM   wms_lpn_contents
7438 	 WHERE  parent_lpn_id = l_current_lpn_id;
7439   BEGIN
7440     -- Intialize out variables
7441     x_return_status := fnd_api.g_ret_sts_success;
7442 
7443     OPEN lpn_csr;
7444     IF (l_debug = 1) THEN
7445        mydebug('create_mo_lpn: Opened lpn_csr');
7446     END IF;
7447     LOOP
7448        FETCH lpn_csr INTO l_current_lpn_id;
7449        EXIT WHEN lpn_csr%NOTFOUND;
7450 
7451        IF (l_debug = 1) THEN
7452 	  mydebug('create_mo_lpn: Current LPN ID is - ' || l_current_lpn_id);
7453        END IF;
7454        OPEN lpn_contents_csr;
7455        IF (l_debug = 1) THEN
7456 	  mydebug('create_mo_lpn: Opened lpn_contents_csr');
7457        END IF;
7458 
7459        LOOP
7460 	  FETCH lpn_contents_csr INTO
7461 	    l_m_item,
7462 	    l_m_qty,
7463 	    l_m_uom,
7464 	    l_m_lot,
7465 	    l_m_rev,
7466 	    l_lpn_cg_id,
7467 	    l_lpn_id;
7468 
7469 	  EXIT WHEN lpn_contents_csr%NOTFOUND;
7470 
7471 	  -- Close existing move orders detailed.
7472 	  UPDATE mtl_txn_request_lines mol
7473 	    SET mol.line_status = inv_globals.g_to_status_closed
7474 	    WHERE mol.lpn_id = l_lpn_id
7475 	    AND mol.organization_id   = p_org_id
7476 	    AND mol.quantity_detailed > 0
7477 	    AND EXISTS
7478             (SELECT 1
7479              FROM
7480              mtl_txn_request_headers moh,
7481              wms_license_plate_numbers wlc
7482              WHERE mol.header_id     = moh.header_id
7483              AND moh.move_order_type = inv_globals.g_move_order_put_away
7484              AND wlc.lpn_id          = mol.lpn_id
7485              AND wlc.lpn_context     = 1);
7486 
7487 	  IF (l_debug = 1) THEN
7488 	     mydebug('create_mo_lpn: lpn loop');
7489 	     mydebug('create_mo_lpn: lot' || l_m_lot);
7490 	     mydebug('item              ' || l_m_item);
7491 	     mydebug('uom               ' || l_m_uom);
7492 	     mydebug('rev               ' || l_m_rev);
7493 	     mydebug('cost group id     ' || l_lpn_cg_id);
7494 	     mydebug('lpn_id            ' || l_lpn_id);
7495 	  END IF;
7496 
7497 	  SELECT subinventory_code
7498                , locator_id
7499 	    INTO    l_m_sub
7500                   , l_m_loc
7501 	    FROM   wms_license_plate_numbers
7502 	    WHERE  lpn_id = l_lpn_id;
7503 
7504 	  SELECT mil.project_id
7505 	       , mil.task_id
7506 	    INTO   l_project_id
7507 	         , l_task_id
7508 	    FROM   mtl_item_locations mil
7509 	    WHERE  mil.inventory_location_id = l_m_loc
7510 	    AND    mil.organization_id = p_org_id
7511 	    AND    mil.subinventory_code = l_m_sub;
7512 
7513 	  -- Call create_mo
7514 	  IF (l_debug = 1) THEN
7515 	     mydebug('create_mo_lpn: Calling create_mo');
7516 	  END IF;
7517 
7518 	  wms_task_dispatch_put_away.create_mo
7519 	    (p_org_id                         => p_org_id,
7520 	     p_inventory_item_id              => l_m_item,
7521 	     p_qty                            => l_m_qty,
7522 	     p_uom                            => l_m_uom,
7523 	     p_lpn                            => l_lpn_id,
7524 	     p_project_id                     => l_project_id,
7525 	     p_task_id                        => l_task_id,
7526 	     p_lot_number                     => l_m_lot,
7527 	     p_revision                       => l_m_rev,
7528 	     p_header_id                      => l_m_hdr,
7529 	     p_sub                            => l_m_sub,
7530 	     p_loc                            => l_m_loc,
7531 	     x_line_id                        => l_m_line,
7532 	     p_inspection_status              => NULL,
7533 	     p_transaction_type_id            => 64,
7534 	     p_transaction_source_type_id     => 4,
7535 	     p_wms_process_flag               => NULL,
7536 	     x_return_status                  => l_return_status,
7537 	     x_msg_count                      => l_msg_count,
7538 	     x_msg_data                       => l_msg_data,
7539 	     p_from_cost_group_id             => l_lpn_cg_id
7540 	    );
7541 
7542 	  l_m_hdr := NULL;
7543 	  fnd_msg_pub.count_and_get(p_count => l_msg_count, p_data => l_msg_data);
7544 
7545 	  IF (l_msg_count = 0) THEN
7546 	     IF (l_debug = 1) THEN
7547 		mydebug('create_mo_lpn: Successful');
7548 	     END IF;
7549 	   ELSIF(l_msg_count = 1) THEN
7550 	     IF (l_debug = 1) THEN
7551 		mydebug('create_mo_lpn: Not Successful');
7552 		mydebug('create_mo_lpn: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
7553 	     END IF;
7554 	   ELSE
7555 	     IF (l_debug = 1) THEN
7556 		mydebug('create_mo_lpn: Not Successful2');
7557 	     END IF;
7558 
7559 	     FOR i IN 1 .. l_msg_count LOOP
7560 		l_msg_data := fnd_msg_pub.get(i, 'F');
7561 
7562 		IF (l_debug = 1) THEN
7563 		   mydebug('create_mo_lpn: ' || REPLACE(l_msg_data, fnd_global.local_chr(0), ' '));
7564 		END IF;
7565 	     END LOOP;
7566 	  END IF;
7567 
7568 	  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7569 	     fnd_message.set_name('WMS', 'WMS_TD_CMO_ERROR');
7570 	     fnd_msg_pub.ADD;
7571 	     RAISE fnd_api.g_exc_error;
7572 	   ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
7573 	     fnd_message.set_name('WMS', 'WMS_TD_CMO_ERROR');
7574 	     fnd_msg_pub.ADD;
7575 	     RAISE fnd_api.g_exc_error;
7576 	  END IF;
7577 
7578 	  IF (l_debug = 1) THEN
7579 	     mydebug('create_mo: Line ID created');
7580 	     mydebug('create_mo: ' || l_m_line);
7581 	  END IF;
7582 
7583        END LOOP;
7584        CLOSE lpn_contents_csr;
7585 
7586     END LOOP;
7587     CLOSE lpn_csr;
7588 
7589   EXCEPTION
7590      WHEN fnd_api.g_exc_error THEN
7591 	x_return_status := fnd_api.g_ret_sts_error;
7592 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7593      WHEN OTHERS THEN
7594 	x_return_status := fnd_api.g_ret_sts_unexp_error;
7595 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7596   END create_mo_lpn;
7597 
7598 
7599   PROCEDURE validate_against_rules(
7600     p_organization_id    IN             NUMBER
7601   , p_lpn_id             IN             NUMBER
7602   , p_subinventory       IN             VARCHAR2
7603   , p_locator_id         IN             NUMBER
7604   , p_user_id            IN             NUMBER
7605   , p_eqp_ins            IN             VARCHAR2
7606   , p_project_id         IN             NUMBER
7607   , p_task_id            IN             NUMBER
7608   , x_return_status      OUT NOCOPY     VARCHAR2
7609   , x_msg_count          OUT NOCOPY     NUMBER
7610   , x_msg_data           OUT NOCOPY     VARCHAR2
7611   , x_validation_passed  OUT NOCOPY     VARCHAR2
7612   ) IS
7613     l_api_name CONSTANT VARCHAR2(30) := 'validate_against_rules';
7614     l_progress          VARCHAR2(10);
7615     l_mo_lines_count    NUMBER;
7616     l_mmtt_lines_count  NUMBER;
7617     l_number_of_rows    NUMBER;
7618     l_return_status     VARCHAR2(1);
7619     l_crossdock         VARCHAR2(3);
7620     l_fully_allocated   BOOLEAN      := TRUE;
7621     l_debug             NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7622     l_is_content_lpn    VARCHAR2(1)  := 'N';
7623     l_is_mo_there       VARCHAR2(1)  := 'N';
7624     l_lpn_context       NUMBER;
7625     l_lpn_id            NUMBER;
7626     l_is_mo_created     VARCHAR2(1)  := 'N';
7627     l_emp_id            NUMBER;
7628 
7629     CURSOR qty_check_cursor IS
7630       SELECT mtrl.primary_quantity
7631            , mmtt.primary_quantity
7632       FROM   mtl_txn_request_lines mtrl, mtl_material_transactions_temp mmtt
7633       WHERE  mtrl.organization_id = p_organization_id
7634       AND    mtrl.lpn_id = l_lpn_id
7635       AND    mmtt.move_order_line_id = mtrl.line_id;
7636 
7637     l_mo_primary_qty    NUMBER;
7638     l_mmtt_primary_qty  NUMBER;
7639 
7640     CURSOR lpn_cur IS
7641       SELECT lpn_id
7642 	FROM wms_license_plate_numbers
7643 	START WITH lpn_id = p_lpn_id
7644 	CONNECT BY parent_lpn_id = PRIOR lpn_id;
7645 
7646   BEGIN
7647     IF (l_debug = 1) THEN
7648       mydebug('***Calling validate_against_rules***');
7649     END IF;
7650 
7651     -- Set the savepoint
7652     SAVEPOINT validate_rules_sp;
7653     x_return_status := fnd_api.g_ret_sts_success;
7654     l_progress := '10';
7655 
7656     -- Call the ATF API to abort the plan before deleting the MMTTs
7657 
7658     -- Patchset level check
7659     mydebug('validate_against_rules: Current release is above J.');
7660 
7661     BEGIN -- Abort op plan call
7662 
7663        mydebug('validate_against_rules: Calling ATF_For_Manual_Drop to abort the plan, if any');
7664 
7665        l_progress := '12';
7666        WMS_PUTAWAY_UTILS.ATF_For_Manual_Drop(
7667 					     x_return_status => x_return_status
7668 					     ,x_msg_count     => x_msg_count
7669 					     ,x_msg_data      => x_msg_data
7670 					     ,p_call_type     => G_ATF_ABORT_PLAN
7671 					     ,p_org_id        => p_organization_id
7672 					     ,p_lpn_id        => p_lpn_id
7673 					     ,p_emp_id        => NULL
7674 					     );
7675        l_progress := '13';
7676 
7677        IF x_return_status = fnd_api.g_ret_sts_success THEN
7678           -- Abort op plan completed successfully.
7679           NULL;
7680 
7681         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
7682           RAISE fnd_api.g_exc_error;
7683 
7684         ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
7685           RAISE fnd_api.g_exc_unexpected_error;
7686 
7687        END IF;
7688        l_progress := '18';
7689 
7690     EXCEPTION
7691        WHEN OTHERS THEN
7692           RAISE fnd_api.g_exc_unexpected_error;
7693     END; -- Abort op plan call
7694 
7695 
7696     OPEN lpn_cur;
7697     LOOP
7698       FETCH lpn_cur INTO l_lpn_id;
7699 
7700       l_is_content_lpn := 'N';
7701       IF lpn_cur%notfound THEN
7702 	 EXIT;
7703       END IF;
7704 
7705       BEGIN
7706         SELECT 'Y' INTO l_is_content_lpn
7707         FROM wms_lpn_contents
7708         WHERE parent_lpn_id = l_lpn_id;
7709       EXCEPTION
7710         WHEN no_data_found THEN
7711           l_is_content_lpn := 'N';
7712         WHEN too_many_rows THEN
7713           l_is_content_lpn := 'Y';
7714       END;
7715 
7716       IF (l_is_content_lpn = 'Y') THEN
7717 
7718 	 --BUG 3369873:  We no longer need to call revert_loc_suggested_capacity
7719 	 --because it is done inside abort_operation_instance_already.
7720 
7721           -- Clean up any old existing MMTT, MTLT, and WDT records
7722           -- which might exist if on the user directed putaway mobile page,
7723           -- you are performing a user drop and you navigate back to the loc
7724           -- to enter a different one than before.
7725           IF (l_debug = 1) THEN
7726             mydebug('Clean up old WDT, MTLT, and MMTT records first');
7727           END IF;
7728 
7729           -- Before clean up call abort operations plan
7730 
7731           -- After Abort Operation plan do clean up.
7732 
7733           DELETE FROM wms_dispatched_tasks
7734           WHERE task_type = 2
7735           AND   transaction_temp_id IN
7736                 (SELECT transaction_temp_id
7737                  FROM   mtl_material_transactions_temp
7738                  WHERE  move_order_line_id IN
7739                     (SELECT line_id
7740                      FROM   mtl_txn_request_lines
7741                      WHERE  organization_id = p_organization_id
7742                      AND    lpn_id = l_lpn_id)
7743                 );
7744 
7745           DELETE FROM mtl_transaction_lots_temp
7746           WHERE transaction_temp_id IN
7747               (SELECT transaction_temp_id
7748                FROM   mtl_material_transactions_temp
7749                WHERE  move_order_line_id IN
7750                   (SELECT line_id
7751                    FROM   mtl_txn_request_lines
7752                    WHERE  organization_id = p_organization_id
7753                    AND    lpn_id = l_lpn_id));
7754 
7755           DELETE FROM mtl_material_transactions_temp
7756           WHERE move_order_line_id IN
7757               (SELECT line_id
7758                FROM   mtl_txn_request_lines
7759                WHERE  organization_id = p_organization_id
7760                AND    lpn_id = l_lpn_id);
7761 
7762           IF (l_debug = 1) THEN
7763             mydebug('Finished cleaning up old WDT, MTLT, and MMTT records');
7764           END IF;
7765 
7766           l_progress := '40';
7767 
7768           -- Update the move order lines with the user inputted sub/loc.
7769           -- Also null out the quantity detailed in case the user enters
7770           -- a loc, validates it against rules, but then goes back and changes
7771           -- the sub/loc before processing it.
7772           UPDATE mtl_txn_request_lines
7773 	    SET to_subinventory_code = p_subinventory
7774             , to_locator_id = p_locator_id
7775             , quantity_detailed = NULL
7776 	    WHERE  organization_id = p_organization_id
7777 	    AND    lpn_id = l_lpn_id
7778 	    AND    line_status = 7;
7779 
7780           l_progress := '50';
7781 
7782           -- Call the rules engine and see if MMTT suggestions are returned
7783           -- This allows for rules engine validation of the user inputted sub/loc
7784           -- Bug# 2752119
7785           -- Added an extra input parameter in API call so we will not check for
7786           -- crossdocking opportunities for rules validated express drops
7787           wms_task_dispatch_put_away.suggestions_pub(
7788             p_lpn_id                  => l_lpn_id
7789           , p_org_id                  => p_organization_id
7790           , p_user_id                 => p_user_id
7791           , p_eqp_ins                 => p_eqp_ins
7792           , x_number_of_rows          => l_number_of_rows
7793           , x_return_status           => l_return_status
7794           , x_msg_count               => x_msg_count
7795           , x_msg_data                => x_msg_data
7796           , x_crossdock               => l_crossdock
7797           , p_status                  => 4
7798           , p_check_for_crossdock     => 'N'
7799           , p_drop_type               => 'MD' -- Manual drop
7800           , p_subinventory            =>  p_subinventory
7801           , p_locator_id              =>  p_locator_id );
7802 
7803           IF (l_debug = 1) THEN
7804             mydebug('Finished calling the suggestions_pub API');
7805           END IF;
7806 
7807           l_progress := '60';
7808 
7809           -- Check to see if the suggestions_pub returned successfully
7810           IF (l_return_status = fnd_api.g_ret_sts_success) THEN
7811             IF (l_debug = 1) THEN
7812               mydebug('Success returned from suggestions_pub API');
7813             END IF;
7814           ELSE
7815             IF (l_debug = 1) THEN
7816               mydebug('Failure returned from suggestions_pub API');
7817             END IF;
7818 
7819             RAISE fnd_api.g_exc_error;
7820           END IF;
7821 
7822 	  -- Bug# 3352224
7823 	  -- For Patchset J and above, we allow manual drops within
7824 	  -- Inventory.	 Therefore the call to suggestions_pub would
7825 	  -- create move order lines if none existed for inventory LPNs.
7826 	  -- The query to see how many move order lines there are for the
7827 	  -- LPN should be done after the call to suggestions_pub and not before.
7828 
7829           -- Get the number of move order lines for this given LPN
7830           SELECT COUNT(1)
7831 	    INTO   l_mo_lines_count
7832 	    FROM   mtl_txn_request_lines
7833 	    WHERE  organization_id = p_organization_id
7834 	    AND    lpn_id = l_lpn_id
7835 	    AND    line_status = 7;
7836 
7837           IF (l_debug = 1) THEN
7838             mydebug('The number of move order lines is: ' || l_mo_lines_count);
7839           END IF;
7840 
7841           l_progress := '70';
7842 
7843           -- Check to see if MMTT allocations were created
7844           SELECT COUNT(mmtt.transaction_temp_id)
7845           INTO   l_mmtt_lines_count
7846           FROM   mtl_material_transactions_temp mmtt, mtl_txn_request_lines mtrl
7847           WHERE  mtrl.lpn_id = l_lpn_id
7848           AND    mtrl.organization_id = p_organization_id
7849           AND    NVL(mmtt.wms_task_type, 0) <> -1
7850           AND    mtrl.line_id = mmtt.move_order_line_id
7851           AND    NVL(mtrl.project_id, -1) = DECODE(mtrl.project_id, NULL, -1, NVL(p_project_id, NVL(mtrl.project_id, -1)))
7852           AND    NVL(mtrl.task_id, -1) = DECODE(mtrl.task_id, NULL, -1, NVL(p_task_id, NVL(mtrl.task_id, -1)));
7853 
7854           IF (l_debug = 1) THEN
7855             mydebug('Checked to see how many MMTT lines were created: ' || l_mmtt_lines_count);
7856           END IF;
7857 
7858           l_progress := '80';
7859 
7860           -- Check that for every move order line, exactly one MMTT suggestion is created
7861           IF (l_mmtt_lines_count = 0
7862               OR l_mmtt_lines_count <> l_mo_lines_count) THEN
7863             IF (l_debug = 1) THEN
7864               mydebug('Rules engine validation failed!');
7865             END IF;
7866 
7867             x_validation_passed := 'N';
7868 
7869             -- Bug #2745971
7870             -- We need to explicitly revert the locator capacities since this is
7871             -- done autonomously by the rules engine.  Performing a rollback
7872             -- will not revert the locator capacities.
7873             IF (l_debug = 1) THEN
7874               mydebug('Call revert_loc_suggested_capacity to clean up capacities');
7875             END IF;
7876 
7877             wms_task_dispatch_put_away.revert_loc_suggested_capacity(
7878               x_return_status       => l_return_status
7879             , x_msg_count           => x_msg_count
7880             , x_msg_data            => x_msg_data
7881             , p_organization_id     => p_organization_id
7882             , p_lpn_id              => p_lpn_id
7883             );
7884             l_progress := '90';
7885 
7886             -- Check to see if the call to revert_loc_suggested_capacity returned successfully
7887             IF (l_return_status = fnd_api.g_ret_sts_success) THEN
7888               IF (l_debug = 1) THEN
7889                 mydebug('Success returned from revert_loc_suggested_capacity API');
7890               END IF;
7891             ELSE
7892               IF (l_debug = 1) THEN
7893                 mydebug('Failure returned from revert_loc_suggested_capacity API');
7894               END IF;
7895               -- Bug 5393727: do not raise an exception if revert API returns an error
7896               -- RAISE fnd_api.g_exc_error;
7897             END IF;
7898 
7899             -- Perform a rollback to clean up the rest of the data
7900             BEGIN
7901               ROLLBACK TO validate_rules_sp;
7902             EXCEPTION WHEN OTHERS THEN
7903               NULL;
7904             END;
7905             EXIT;
7906           ELSE
7907             -- Bug #2745971
7908             -- Check to see if the MMTT allocations are all full allocations,
7909             -- i.e. not partially allocated suggestions
7910             IF (l_debug = 1) THEN
7911               mydebug('Check if suggestions are fully allocated');
7912             END IF;
7913 
7914             OPEN qty_check_cursor;
7915 
7916             LOOP
7917               FETCH qty_check_cursor INTO l_mo_primary_qty, l_mmtt_primary_qty;
7918               EXIT WHEN qty_check_cursor%NOTFOUND;
7919 
7920               IF (l_mo_primary_qty <> l_mmtt_primary_qty) THEN
7921                 -- If any of the MOL and corresponding MMTT lines have
7922                 -- different primary quantities, the line was only partially allocated
7923                 l_fully_allocated := FALSE;
7924                 EXIT;
7925               END IF;
7926             END LOOP;
7927 
7928             CLOSE qty_check_cursor;
7929 
7930             IF (l_debug = 1) THEN
7931               mydebug('Finished checking for full allocations');
7932             END IF;
7933 
7934             l_progress := '100';
7935 
7936             -- Rules engine validation should fail if the lines are not fully allocated
7937             IF (l_fully_allocated) THEN
7938               IF (l_debug = 1) THEN
7939                 mydebug('Rules engine validation passed!');
7940               END IF;
7941 
7942               x_validation_passed := 'Y';
7943             ELSE
7944               IF (l_debug = 1) THEN
7945                 mydebug('Rules engine validation failed!');
7946               END IF;
7947 
7948               x_validation_passed := 'N';
7949 
7950               -- Bug #2745971
7951               -- We need to explicitly revert the locator capacities since this is
7952               -- done autonomously by the rules engine.  Performing a rollback
7953               -- will not revert the locator capacities.
7954               IF (l_debug = 1) THEN
7955                 mydebug('Call revert_loc_suggested_capacity to clean up capacities');
7956               END IF;
7957 
7958               wms_task_dispatch_put_away.revert_loc_suggested_capacity(
7959                 x_return_status       => l_return_status
7960               , x_msg_count           => x_msg_count
7961               , x_msg_data            => x_msg_data
7962               , p_organization_id     => p_organization_id
7963               , p_lpn_id              => p_lpn_id
7964               );
7965               l_progress := '110';
7966 
7967               -- Check to see if the call to revert_loc_suggested_capacity returned successfully
7968               IF (l_return_status = fnd_api.g_ret_sts_success) THEN
7969                 IF (l_debug = 1) THEN
7970                   mydebug('Success returned from revert_loc_suggested_capacity API');
7971                 END IF;
7972               ELSE
7973                 IF (l_debug = 1) THEN
7974                   mydebug('Failure returned from revert_loc_suggested_capacity API');
7975                 END IF;
7976                 -- Bug 5393727: do not raise an exception if revert API returns an error
7977                 -- RAISE fnd_api.g_exc_error;
7978               END IF;
7979 
7980               -- Perform a rollback to clean up the rest of the data
7981               BEGIN
7982                 ROLLBACK TO validate_rules_sp;
7983               EXCEPTION WHEN OTHERS THEN
7984                  NULL;
7985               END;
7986               EXIT;
7987             END IF;
7988           END IF;
7989 
7990           l_progress := '120';
7991           x_return_status := fnd_api.g_ret_sts_success;
7992 
7993       END IF;
7994   END LOOP;
7995 
7996   -- Call the ATF API to insert WDTs.
7997   -- Moved the ATF API calls to the complete_putaway_wrapper API instead.  -etam
7998 
7999   IF (l_debug = 1) THEN
8000      mydebug('***End of validate_against_rules***');
8001   END IF;
8002 
8003   EXCEPTION
8004     WHEN fnd_api.g_exc_error THEN
8005       ROLLBACK TO validate_rules_sp;
8006       x_validation_passed := 'N';
8007       x_return_status := fnd_api.g_ret_sts_error;
8008       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8009 
8010       IF (l_debug = 1) THEN
8011         mydebug('Exiting validate_against_rules - Execution error: ' || l_progress || ' '
8012           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8013       END IF;
8014     WHEN fnd_api.g_exc_unexpected_error THEN
8015       ROLLBACK TO validate_rules_sp;
8016       x_validation_passed := 'N';
8017       x_return_status := fnd_api.g_ret_sts_unexp_error;
8018       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8019 
8020       IF (l_debug = 1) THEN
8021         mydebug('Exiting validate_against_rules - Unexpected error: ' || l_progress || ' '
8022           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8023       END IF;
8024     WHEN OTHERS THEN
8025       ROLLBACK TO validate_rules_sp;
8026       x_validation_passed := 'N';
8027       x_return_status := fnd_api.g_ret_sts_unexp_error;
8028 
8029       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
8030         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
8031       END IF;
8032 
8033       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8034 
8035       IF (l_debug = 1) THEN
8036         mydebug('Exiting validate_against_rules - Others exception: ' || l_progress || ' '
8037           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8038       END IF;
8039   END validate_against_rules;
8040 
8041 
8042   PROCEDURE create_user_suggestions(
8043     p_organization_id  IN             NUMBER
8044   , p_lpn_id           IN             NUMBER
8045   , p_subinventory     IN             VARCHAR2
8046   , p_locator_id       IN             NUMBER
8047   , p_user_id          IN             NUMBER
8048   , p_eqp_ins          IN             VARCHAR2
8049   , x_return_status    OUT NOCOPY     VARCHAR2
8050   , x_msg_count        OUT NOCOPY     NUMBER
8051   , x_msg_data         OUT NOCOPY     VARCHAR2
8052   , x_number_of_rows   OUT NOCOPY     NUMBER
8053   ) IS
8054     l_api_name CONSTANT VARCHAR2(30) := 'create_user_suggestions';
8055     l_progress          VARCHAR2(10);
8056     l_txn_header_id     NUMBER;
8057     l_txn_temp_id       NUMBER;
8058     l_ser_trx_id        NUMBER;
8059     l_trx_action_id     NUMBER;
8060     l_completion_txn_id NUMBER;
8061     l_primary_uom_code  VARCHAR2(3);
8062     l_task_id           NUMBER;
8063     l_return            NUMBER;
8064     l_number_of_rows    NUMBER       := 0;
8065     l_mo_exists         VARCHAR2(1)  := 'N';
8066     l_lpn_context       NUMBER;
8067     l_is_mo_created     VARCHAR2(1)  := 'N';
8068     l_subinventory      mtl_secondary_inventories.secondary_inventory_name%TYPE;
8069     l_locator_id        NUMBER;
8070     l_operation_seq_num  NUMBER := NULL;
8071     l_repetitive_line_id NUMBER := NULL;
8072 
8073     TYPE num_tab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
8074     TYPE var3_tab IS TABLE OF VARCHAR(3) INDEX BY BINARY_INTEGER;
8075     l_mmtt_id_tab num_tab;
8076     l_mmtt_qty_tab num_tab;
8077     l_mmtt_mol_id_tab num_tab;
8078     l_mmtt_uom_tab var3_tab;
8079     l_mmtt_sec_qty_tab num_tab; --OPM Convergence
8080     l_mmtt_sec_uom_tab var3_tab; --OPM Convergence
8081 
8082     -- Bug# 3281512 - Performance Fixes
8083     -- Cursor to retrieve the nested LPNs within a given outer LPN
8084     CURSOR nested_lpn_cursor IS
8085        SELECT lpn_id
8086 	 FROM wms_license_plate_numbers
8087 	 START WITH lpn_id = p_lpn_id
8088 	 CONNECT BY PRIOR lpn_id = parent_lpn_id;
8089     l_current_lpn_id    NUMBER;
8090 
8091     -- Nested LPN changes.
8092     -- Changed MO Lines cursor to not consider closed move orders
8093     -- Bug# 3281512 - Performance Fixes
8094     --
8095     CURSOR mo_lines_cursor IS
8096        SELECT mol.lpn_id
8097             , mol.line_id
8098             , mol.inventory_item_id
8099             , mol.revision
8100             , mol.lot_number
8101             , mol.uom_code
8102             , mol.quantity
8103             , mol.primary_quantity
8104             , mol.reference_id
8105             , mol.project_id
8106             , mol.task_id
8107             , mol.txn_source_id
8108             , mol.transaction_type_id
8109             , mol.transaction_source_type_id
8110             , mol.to_cost_group_id
8111 	 FROM   mtl_txn_request_lines mol
8112 	 WHERE  mol.organization_id   = p_organization_id
8113 	 AND    mol.header_id IN (SELECT moh.header_id
8114 				  FROM mtl_txn_request_headers moh
8115 				  WHERE moh.move_order_type = inv_globals.g_move_order_put_away
8116 				  )
8117 	 AND    mol.line_status = 7
8118 	 AND    mol.lpn_id = l_current_lpn_id;
8119 
8120     l_debug             NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8121     l_emp_id            NUMBER;
8122 
8123 
8124 
8125   BEGIN
8126     IF (l_debug = 1) THEN
8127       mydebug('***Calling create_user_suggestions***');
8128     END IF;
8129 
8130     -- Set the savepoint
8131     SAVEPOINT create_suggestions_sp;
8132     l_progress := '10';
8133 
8134     -- Bug# 2750060
8135     -- Get the employee ID so we can populate
8136     -- the person_id column in WDT properly.
8137     IF (l_debug = 1) THEN
8138       mydebug('Get the employee ID tied to the user');
8139     END IF;
8140 
8141     BEGIN
8142       SELECT employee_id
8143       INTO   l_emp_id
8144       FROM   fnd_user
8145       WHERE  user_id = p_user_id;
8146     EXCEPTION
8147       WHEN NO_DATA_FOUND THEN
8148         IF (l_debug = 1) THEN
8149           mydebug('There is no employee tied to the user');
8150         END IF;
8151 
8152         l_emp_id := NULL;
8153     END;
8154 
8155     IF (l_debug = 1) THEN
8156       mydebug('Employee ID: ' || l_emp_id);
8157     END IF;
8158 
8159     l_progress := '20';
8160 
8161     -- Call the ATF API to abort the plan before deleting the MMTTs
8162 
8163     -- Patchset level check
8164     mydebug('create_user_suggestions: Current release is above J.');
8165 
8166     BEGIN -- Abort op plan call
8167 
8168        mydebug('create_user_suggestions: Calling WMS_PUTAWAY_UTILS.ATF_For_Manual_Drop to abort the plan, if any');
8169 
8170        l_progress := '22';
8171        WMS_PUTAWAY_UTILS.ATF_For_Manual_Drop(
8172 					     x_return_status => x_return_status
8173 					     ,x_msg_count     => x_msg_count
8174 					     ,x_msg_data      => x_msg_data
8175 					     ,p_call_type     => G_ATF_ABORT_PLAN
8176 					     ,p_org_id        => p_organization_id
8177 					     ,p_lpn_id        => p_lpn_id
8178 					     ,p_emp_id        => l_emp_id
8179 					     );
8180        l_progress := '23';
8181 
8182        IF x_return_status = fnd_api.g_ret_sts_success THEN
8183           -- Abort op plan completed successfully.
8184           NULL;
8185 
8186         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
8187           RAISE fnd_api.g_exc_error;
8188 
8189         ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
8190           RAISE fnd_api.g_exc_unexpected_error;
8191 
8192        END IF;
8193        l_progress := '28';
8194 
8195     EXCEPTION
8196        WHEN OTHERS THEN
8197           RAISE fnd_api.g_exc_unexpected_error;
8198     END; -- Abort op plan call
8199 
8200 
8201     -- Clean up any old existing MMTT, MTLT, and WDT records
8202     -- which might exist if on the user directed putaway mobile page,
8203     -- you are performing a user drop and you navigate back to the loc
8204     -- to enter a different one than before.
8205     IF (l_debug = 1) THEN
8206       mydebug('Clean up old WDT, MTLT, and MMTT records');
8207     END IF;
8208 
8209 
8210     -- Nested LPN changes
8211     -- Select the old MMTTs.  Delete them at the end.
8212     -- Bug# 3434940 - Performance Fixes
8213     -- For now we will leave the hash join problem.  We're not able
8214     -- to use the outer nested_lpn_cursor because of the bulk collect.
8215     SELECT mmtt.transaction_temp_id,
8216       mmtt.transaction_quantity,
8217       mmtt.transaction_uom,
8218       mmtt.move_order_line_id,
8219       mmtt.secondary_transaction_quantity, --OPM Convergence
8220       mmtt.secondary_uom_code --OPM Convergence
8221       bulk collect
8222       INTO l_mmtt_id_tab,
8223       l_mmtt_qty_tab,
8224       l_mmtt_uom_tab,
8225       l_mmtt_mol_id_tab,
8226       l_mmtt_sec_qty_tab, --OPM Convergence
8227       l_mmtt_sec_uom_tab --OPM Convergence
8228       FROM mtl_material_transactions_temp mmtt,
8229       mtl_txn_request_lines mtrl
8230       WHERE mmtt.organization_id = p_organization_id
8231       AND mmtt.move_order_line_id = mtrl.line_id
8232       AND mtrl.lpn_id IN (SELECT lpn_id
8233 			  FROM wms_license_plate_numbers
8234 			  START WITH lpn_id = p_lpn_id
8235 			  CONNECT BY parent_lpn_id = PRIOR lpn_id);
8236 
8237 
8238 
8239     SELECT lpn_context,subinventory_code,locator_id
8240       INTO l_lpn_context,l_subinventory,l_locator_id
8241       FROM wms_license_plate_numbers
8242       WHERE lpn_id = p_lpn_id;
8243 
8244     IF l_lpn_context = 1 THEN
8245        -- Inventory LPN so need to create MOLs first
8246        create_mo_lpn( p_lpn_id        => p_lpn_id
8247 		      , p_org_id        => p_organization_id
8248 		      , x_return_status => x_return_status
8249 		      , x_msg_count     => x_msg_count
8250 		      , x_msg_data      => x_msg_data );
8251 
8252        IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
8253           RAISE fnd_api.g_exc_unexpected_error;
8254         ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
8255           RAISE fnd_api.g_exc_error;
8256        END IF;
8257     END IF; -- End for is Inventory lpn
8258 
8259     l_progress := '30';
8260 
8261     -- Loop through the move order lines cursor
8262     -- Bug# 3281512 - Performance Fixes
8263     -- Open up the nested LPN cursor first and for each LPN,
8264     -- loop through the move order lines cursor.
8265     OPEN nested_lpn_cursor;
8266     LOOP
8267        FETCH nested_lpn_cursor INTO l_current_lpn_id;
8268        EXIT WHEN nested_lpn_cursor%NOTFOUND;
8269        IF (l_debug = 1) THEN
8270 	  mydebug('Current LPN ID: ' || l_current_lpn_id);
8271        END IF;
8272 
8273        FOR v_mo_line IN mo_lines_cursor LOOP
8274 	  l_number_of_rows := l_number_of_rows + 1;
8275 
8276 	  IF (l_debug = 1) THEN
8277 	     mydebug('Current move order line values:');
8278 	     mydebug('LPN ID:==============> ' || v_mo_line.lpn_id);
8279 	     mydebug('Line ID: ============> ' || v_mo_line.line_id);
8280 	     mydebug('Inventory Item ID: ==> ' || v_mo_line.inventory_item_id);
8281 	     mydebug('Revision: ===========> ' || v_mo_line.revision);
8282 	     mydebug('Lot Number: =========> ' || v_mo_line.lot_number);
8283 	     mydebug('UOM Code: ===========> ' || v_mo_line.uom_code);
8284 	     mydebug('Qty: ================> ' || v_mo_line.quantity);
8285 	     mydebug('Primary Qty: ========> ' || v_mo_line.primary_quantity);
8286 	     mydebug('Project ID: =========> ' || v_mo_line.project_id);
8287 	     mydebug('Task ID: ============> ' || v_mo_line.task_id);
8288 	     mydebug('Txn Source ID: ======> ' || v_mo_line.txn_source_id);
8289 	     mydebug('Txn Type ID: ========> ' || v_mo_line.transaction_type_id);
8290 	     mydebug('Txn Source Type ID: => ' || v_mo_line.transaction_source_type_id);
8291 	     mydebug('Cost Group ID: ======> ' || v_mo_line.to_cost_group_id);
8292 	  END IF;
8293 
8294 	  -- Get a new transaction header id from the sequence
8295 	  SELECT mtl_material_transactions_s.NEXTVAL
8296 	    INTO   l_txn_header_id
8297 	    FROM   DUAL;
8298 
8299 	  IF (l_debug = 1) THEN
8300 	     mydebug('Transaction header ID: ' || l_txn_header_id);
8301 	  END IF;
8302 
8303 	  l_progress := '40';
8304 
8305 	  -- Get the value for the transaction action ID
8306 	  IF (v_mo_line.transaction_source_type_id = 5
8307 	      AND v_mo_line.transaction_type_id = 44) THEN
8308 	     -- WIP assembly completion
8309 	     l_trx_action_id := 31;
8310 	   ELSIF(v_mo_line.transaction_source_type_id = 1
8311 		 AND v_mo_line.transaction_type_id = 18) THEN
8312 	     -- Purchase order receipt
8313 	     l_trx_action_id := 27;
8314 	   ELSIF(v_mo_line.transaction_source_type_id = 7
8315 		 AND v_mo_line.transaction_type_id = 61) THEN
8316 	     --Internal Req/Intransit Shipment Receipt
8317 	     l_trx_action_id := 12;
8318 	   ELSIF(v_mo_line.transaction_source_type_id = 8
8319 		 AND v_mo_line.transaction_type_id = 62) THEN
8320 	     -- Internal order intransit shipment
8321 	     l_trx_action_id := 21;
8322 	   ELSIF(v_mo_line.transaction_source_type_id = 12
8323 		 AND v_mo_line.transaction_type_id = 15) THEN
8324 	     -- RMA receipt of customer return
8325 	     l_trx_action_id := 27;
8326 	   ELSIF(v_mo_line.transaction_source_type_id = 4) THEN
8327 	     -- Inventory lpn
8328 	     l_trx_action_id := 2;
8329 	   ELSE
8330 	     -- This case should never come about
8331 	     l_trx_action_id := -999;
8332 	  END IF;
8333 
8334 	  IF (l_debug = 1) THEN
8335 	     mydebug('Transaction action ID is: ' || l_trx_action_id);
8336 	  END IF;
8337 
8338 	  l_progress := '50';
8339 
8340 	  IF (l_debug = 1) THEN
8341 	     mydebug('Getting operation_seq_num and repetive_line_id for mol: ' || v_mo_line.line_id);
8342 	  END IF;
8343 	  l_progress := '52';
8344 	  BEGIN
8345 	     SELECT DISTINCT operation_seq_num, repetitive_line_id
8346 	       INTO l_operation_seq_num,l_repetitive_line_id
8347 	       FROM mtl_material_transactions_temp
8348 	       WHERE move_order_line_id = v_mo_line.line_id;
8349 	     IF (l_debug = 1) THEN
8350 		mydebug(' operation_seq_num ===> '|| l_operation_seq_num);
8351 		mydebug(' repetive_line_id ====> '|| l_repetitive_line_id);
8352 	     END IF;
8353 	     l_progress := '53';
8354 	  EXCEPTION
8355 	     WHEN no_data_found THEN
8356 		l_operation_seq_num := NULL;
8357 		l_repetitive_line_id := NULL;
8358 	     WHEN OTHERS THEN
8359 		IF (l_debug = 1) THEN
8360 		      mydebug('These values are not distinct!!');
8361 		END IF;
8362 		RAISE fnd_api.g_exc_error;
8363 	  END;
8364 
8365 	  -- Insert a record into MMTT
8366 	  -- Nested LPN changes,
8367 	  -- For Resides in Receiving LPNs insert destination sub destint loc as sub and loc
8368 	  -- For Resides in Inventory LPNs insert destination sub and loc as to_sub and to_loc.
8369 
8370 
8371           mydebug('lpn context =====>' || l_lpn_context );
8372           mydebug('l_subinventory  =====>' || l_subinventory );
8373           mydebug('p_subinventory  =====>' || p_subinventory );
8374           mydebug('l_locator_id  =====>' || l_locator_id );
8375           mydebug('p_locator_id  =====>' || p_locator_id );
8376 
8377 	  IF l_lpn_context = 3  OR l_lpn_context = 2 THEN --added context =2 bug 4189437
8378 
8379 	     l_return :=
8380 	       inv_trx_util_pub.insert_line_trx
8381 	       (p_trx_hdr_id          => l_txn_header_id,
8382 		p_item_id             => v_mo_line.inventory_item_id,
8383 		p_revision            => v_mo_line.revision,
8384 		p_org_id              => p_organization_id,
8385 		p_trx_action_id       => l_trx_action_id,
8386 		p_subinv_code         => p_subinventory,
8387 		p_locator_id          => p_locator_id,
8388 		p_trx_type_id         => v_mo_line.transaction_type_id,
8389 		p_trx_src_type_id     => v_mo_line.transaction_source_type_id,
8390 		p_trx_qty             => v_mo_line.quantity,
8391 		p_pri_qty             => v_mo_line.primary_quantity,
8392 		p_uom                 => v_mo_line.uom_code,
8393 		p_user_id             => p_user_id,
8394 		p_cost_group          => v_mo_line.to_cost_group_id,
8395 		p_from_lpn_id         => v_mo_line.lpn_id,
8396 		p_trx_src_id          => v_mo_line.txn_source_id,
8397 		x_trx_tmp_id          => l_txn_temp_id,
8398 		x_proc_msg            => x_msg_data,
8399 		p_project_id          => v_mo_line.project_id,
8400 		p_task_id             => v_mo_line.task_id,
8401 		p_transaction_status  => 2);
8402 	     --BUG 3356366: Insert MMTT with txn_status 2 so that it won't
8403 	     --invoke the DB trigger that calls update_loc_suggested_capacity
8404 	   ELSE
8405 	     l_return :=
8406 	       inv_trx_util_pub.insert_line_trx
8407 	       (p_trx_hdr_id          => l_txn_header_id,
8408 		p_item_id             => v_mo_line.inventory_item_id,
8409 		p_revision            => v_mo_line.revision,
8410 		p_org_id              => p_organization_id,
8411 		p_trx_action_id       => l_trx_action_id,
8412 		p_subinv_code         => nvl(l_subinventory,p_subinventory), -- 4156992
8413 		p_locator_id          => nvl(l_locator_id,p_locator_id) , -- 4156992
8414 		p_trx_type_id         => v_mo_line.transaction_type_id,
8415 		p_trx_src_type_id     => v_mo_line.transaction_source_type_id,
8416 		p_trx_qty             => v_mo_line.quantity,
8417 		p_pri_qty             => v_mo_line.primary_quantity,
8418 		p_uom                 => v_mo_line.uom_code,
8419 		p_user_id             => p_user_id,
8420 		p_cost_group          => v_mo_line.to_cost_group_id,
8421 		p_from_lpn_id         => v_mo_line.lpn_id,
8422 		p_trx_src_id          => v_mo_line.txn_source_id,
8423 		x_trx_tmp_id          => l_txn_temp_id,
8424 		x_proc_msg            => x_msg_data,
8425 		p_project_id          => v_mo_line.project_id,
8426 		p_task_id             => v_mo_line.task_id,
8427 		p_tosubinv_code       => p_subinventory,
8428 	        p_tolocator_id        => p_locator_id,
8429 	        p_transaction_status => 2);
8430 	     --BUG 3356366: Insert MMTT with txn_status 2 so that it won't
8431 	     --invoke the DB trigger that calls update_loc_suggested_capacity
8432 	  END IF;
8433 
8434 	  IF (l_debug = 1) THEN
8435 	     mydebug('Successfully inserted MMTT record: ' || l_txn_temp_id);
8436 	  END IF;
8437 
8438 	  l_progress := '60';
8439 
8440 	  IF (l_return <> 0) THEN
8441 	     ROLLBACK TO create_suggestions_sp;
8442 
8443 	     IF (l_debug = 1) THEN
8444 		mydebug('Error occurred while calling inv_trx_util_pub.insert_line_trx');
8445 	     END IF;
8446 
8447 	     RAISE fnd_api.g_exc_unexpected_error;
8448 	  END IF;
8449 
8450 	  -- Get the value for the completion_transaction_id
8451 	  -- in the case of WIP completions
8452 	  IF (v_mo_line.transaction_source_type_id = 5 AND
8453 	      v_mo_line.transaction_type_id = 44) THEN
8454 	     SELECT NVL(completion_transaction_id, -999)
8455 	       INTO   l_completion_txn_id
8456 	       FROM   wip_lpn_completions
8457 	       WHERE  header_id = v_mo_line.reference_id
8458 	       AND    lpn_id = p_lpn_id;
8459 	   ELSE
8460 	     l_completion_txn_id := NULL;
8461 	  END IF;
8462 
8463 	  IF (l_debug = 1) THEN
8464 	     mydebug('Completion transaction ID: ' || l_completion_txn_id);
8465 	  END IF;
8466 
8467 	  l_progress := '70';
8468 
8469 	  -- Get the item's primary uom code
8470 	  SELECT primary_uom_code
8471 	    INTO   l_primary_uom_code
8472 	    FROM   mtl_system_items
8473 	    WHERE  inventory_item_id = v_mo_line.inventory_item_id
8474 	    AND    organization_id = p_organization_id;
8475 
8476 	  IF (l_debug = 1) THEN
8477 	     mydebug('Item primary UOM code: ' || l_primary_uom_code);
8478 	  END IF;
8479 
8480 	  l_progress := '80';
8481 
8482 	  --Need to update MOL.quantity_detailed
8483 	  --Because in WMSOPIBB.COMPLETE, it will deduct
8484 	  --MMTT.TRANSACTION_QUANTITY from MOL.QUANITY_DETIALED
8485 	  UPDATE mtl_txn_request_lines
8486 	    SET  quantity_detailed = Nvl(quantity_detailed,0)+quantity
8487 	    WHERE line_id = v_mo_line.line_id;
8488 
8489 	  -- Update the MMTT record with the move order line,
8490 	  -- completion transaction ID in the case of WIP completion,
8491 	  -- item primary UOM code, transaction status (2 for suggestions),
8492 	  -- and wms task type (2 for putaway).
8493 	  UPDATE mtl_material_transactions_temp
8494 	    SET move_order_line_id = v_mo_line.line_id
8495 	    , completion_transaction_id = l_completion_txn_id
8496 	    , item_primary_uom_code = l_primary_uom_code
8497 	    , transaction_status = 2
8498 	    , wms_task_type = 2
8499 	    , operation_seq_num = l_operation_seq_num   --need these 2 columns when
8500 	    , repetitive_line_id = l_repetitive_line_id --reverting crossdock
8501 	    WHERE  transaction_temp_id = l_txn_temp_id;
8502 
8503 	  IF (l_debug = 1) THEN
8504 	     mydebug('Updated the MMTT record with additional info');
8505 	  END IF;
8506 
8507 	  l_progress := '90';
8508 
8509 	  IF (v_mo_line.lot_number IS NOT NULL) THEN
8510 	     -- Insert a record into MTLT
8511 	     IF (l_debug = 1) THEN
8512 		mydebug('Insert a record into MTLT for lot: ' || v_mo_line.lot_number);
8513 	     END IF;
8514 
8515 	     l_return :=
8516 	       inv_trx_util_pub.insert_lot_trx
8517 	       (p_trx_tmp_id     => l_txn_temp_id,
8518 		p_user_id        => p_user_id,
8519 		p_lot_number     => v_mo_line.lot_number,
8520 		p_trx_qty        => v_mo_line.quantity,
8521 		p_pri_qty        => v_mo_line.primary_quantity,
8522 		x_ser_trx_id     => l_ser_trx_id,
8523 		x_proc_msg       => x_msg_data);
8524 
8525 	     IF (l_debug = 1) THEN
8526 		mydebug('Successfully inserted MTLT record');
8527 	     END IF;
8528 
8529 	     l_progress := '100';
8530 
8531 	     IF (l_return <> 0) THEN
8532 		IF (l_debug = 1) THEN
8533 		   mydebug('Error occurred while calling inv_trx_util_pub.insert_lot_trx');
8534 		END IF;
8535 
8536 		RAISE fnd_api.g_exc_unexpected_error;
8537 	     END IF;
8538 
8539 	     -- Update the MTLT record to clear out the serial_transaction_temp_id column
8540 	     -- since insert_lot_trx by default will insert a value for it.
8541 	     UPDATE mtl_transaction_lots_temp
8542 	       SET serial_transaction_temp_id = NULL
8543 	       WHERE  transaction_temp_id = l_txn_temp_id;
8544 
8545 	     IF (l_debug = 1) THEN
8546 		mydebug('Cleared out the serial txn temp ID column in MTLT record');
8547 	     END IF;
8548 
8549 	     l_progress := '110';
8550 	  END IF;
8551 
8552 
8553 	  l_progress := '130';
8554        END LOOP; -- End mo_lines_cursor LOOP
8555     END LOOP; -- End nested_lpn_cursor LOOP
8556     CLOSE nested_lpn_cursor;
8557 
8558 
8559     -- Delete WDT
8560     FORALL i in 1..l_mmtt_id_tab.COUNT
8561       DELETE FROM wms_dispatched_tasks
8562       WHERE  transaction_temp_id = l_mmtt_id_tab(i)
8563       AND  task_type = 2;
8564 
8565 
8566     -- Delete MTLT
8567     FORALL i in 1..l_mmtt_id_tab.COUNT
8568       DELETE FROM mtl_transaction_lots_temp
8569       WHERE  transaction_temp_id = l_mmtt_id_tab(i);
8570 
8571     FOR i IN 1..l_mmtt_id_tab.COUNT LOOP
8572        UPDATE mtl_txn_request_lines
8573 	 SET  quantity_detailed = Nvl(quantity_detailed,0)
8574 	 -Decode(uom_code
8575 		 ,l_mmtt_uom_tab(i)
8576 		 ,l_mmtt_qty_tab(i)
8577 		 ,inv_convert.inv_um_convert
8578 		 (inventory_item_id
8579 		  ,NULL
8580 		  ,l_mmtt_qty_tab(i)
8581 		  ,l_mmtt_uom_tab(i)
8582 		  ,uom_code
8583 		  ,NULL
8584 		  ,NULL)
8585 		 )
8586 	 WHERE line_id = l_mmtt_mol_id_tab(i);
8587     END LOOP;
8588 
8589     -- Delete MMTT.
8590     FORALL i in 1..l_mmtt_id_tab.COUNT
8591       DELETE FROM mtl_material_transactions_temp
8592       WHERE transaction_temp_id = l_mmtt_id_tab(i);
8593 
8594     IF (l_debug = 1) THEN
8595        mydebug('Finished cleaning up old WDT, MTLT, and MMTT records');
8596     END IF;
8597 
8598     l_progress := '135';
8599 
8600     x_return_status := fnd_api.g_ret_sts_success;
8601     x_number_of_rows := l_number_of_rows;
8602 
8603     IF (l_debug = 1) THEN
8604       mydebug('Finished inserting suggestion records: ' || l_number_of_rows);
8605     END IF;
8606 
8607     l_progress := '140';
8608 
8609 
8610     -- Call the ATF API to insert WDTs
8611     -- Moved the ATF API calls to the complete_putaway_wrapper API instead.  -etam
8612 
8613     IF (l_debug = 1) THEN
8614       mydebug('***End of create_user_suggestions***');
8615     END IF;
8616 
8617   EXCEPTION
8618     WHEN fnd_api.g_exc_error THEN
8619       ROLLBACK TO create_suggestions_sp;
8620       x_return_status := fnd_api.g_ret_sts_error;
8621       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8622 
8623       IF (l_debug = 1) THEN
8624         mydebug('Exiting create_user_suggestions - Execution error: ' || l_progress || ' '
8625           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8626       END IF;
8627     WHEN fnd_api.g_exc_unexpected_error THEN
8628       ROLLBACK TO create_suggestions_sp;
8629       x_return_status := fnd_api.g_ret_sts_unexp_error;
8630       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8631 
8632       IF (l_debug = 1) THEN
8633         mydebug('Exiting create_user_suggestions - Unexpected error: ' || l_progress || ' '
8634           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8635       END IF;
8636     WHEN OTHERS THEN
8637       ROLLBACK TO create_suggestions_sp;
8638       x_return_status := fnd_api.g_ret_sts_unexp_error;
8639 
8640       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
8641         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
8642       END IF;
8643 
8644       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8645 
8646       IF (l_debug = 1) THEN
8647         mydebug('Exiting create_user_suggestions - Others exception: ' || l_progress || ' '
8648           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8649       END IF;
8650   END create_user_suggestions;
8651 
8652 
8653   PROCEDURE validate_lot_serial_status(
8654     p_organization_id    IN             NUMBER
8655   , p_lpn_id             IN             NUMBER
8656   , x_return_status      OUT NOCOPY     VARCHAR2
8657   , x_msg_count          OUT NOCOPY     NUMBER
8658   , x_msg_data           OUT NOCOPY     VARCHAR2
8659   , x_validation_passed  OUT NOCOPY     VARCHAR2
8660   , x_invalid_value      OUT NOCOPY     VARCHAR2
8661   ) IS
8662     l_api_name   CONSTANT VARCHAR2(30) := 'validate_lot_serial_status';
8663     l_progress            VARCHAR2(10);
8664     l_item_id             NUMBER;
8665 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
8666     l_lot_number          VARCHAR2(80);
8667     l_serial_number       VARCHAR2(30);
8668     l_transaction_type    NUMBER;
8669     l_lot_control_code    NUMBER;
8670     l_serial_control_code NUMBER;
8671     l_return              VARCHAR2(1);
8672 
8673     CURSOR l_item_txn_cursor IS
8674       SELECT inventory_item_id
8675            , lot_number
8676            , transaction_type_id
8677       FROM   mtl_txn_request_lines
8678       WHERE  organization_id = p_organization_id
8679       AND    lpn_id = p_lpn_id;
8680 
8681     CURSOR l_serial_cursor IS
8682       SELECT serial_number
8683       FROM   mtl_serial_numbers
8684       WHERE  inventory_item_id = l_item_id
8685       AND    current_organization_id = p_organization_id
8686       AND    lpn_id = p_lpn_id;
8687 
8688     l_debug               NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8689   BEGIN
8690     IF (l_debug = 1) THEN
8691       mydebug('***Calling validate_lot_serial_status***');
8692       mydebug('Org ID: => ' || p_organization_id);
8693       mydebug('LPN ID: => ' || p_lpn_id);
8694     END IF;
8695 
8696     -- Set the savepoint
8697     SAVEPOINT validate_status_sp;
8698     l_progress := '10';
8699     -- Initialize the return variable
8700     l_return := 'Y';
8701     -- Loop through each move order line for the LPN
8702     OPEN l_item_txn_cursor;
8703 
8704     LOOP
8705       FETCH l_item_txn_cursor INTO l_item_id, l_lot_number, l_transaction_type;
8706       EXIT WHEN l_item_txn_cursor%NOTFOUND;
8707 
8708       IF (l_debug = 1) THEN
8709         mydebug('Current move order line values:');
8710         mydebug('Inventory Item ID: ==> ' || l_item_id);
8711         mydebug('Lot Number: =========> ' || l_lot_number);
8712         mydebug('Transaction type: ===> ' || l_transaction_type);
8713       END IF;
8714 
8715       -- Get the item's lot and serial control code
8716       SELECT lot_control_code
8717            , serial_number_control_code
8718       INTO   l_lot_control_code
8719            , l_serial_control_code
8720       FROM   mtl_system_items
8721       WHERE  inventory_item_id = l_item_id
8722       AND    organization_id = p_organization_id;
8723 
8724       IF (l_debug = 1) THEN
8725         mydebug('Lot and serial control code: ' || l_lot_control_code || ', ' || l_serial_control_code);
8726       END IF;
8727 
8728       l_progress := '20';
8729 
8730       -- Check lot status if lot controlled
8731       IF (l_lot_control_code = 2) THEN
8732         -- Make sure that a lot number was stamped onto the move order line
8733         IF (l_lot_number IS NULL) THEN
8734           IF (l_debug = 1) THEN
8735             mydebug('No lot number on move order line!');
8736           END IF;
8737 
8738           RAISE fnd_api.g_exc_unexpected_error;
8739         END IF;
8740 
8741         l_progress := '30';
8742         -- Check if the lot material status is valid
8743         -- for the current move order line's transaction type
8744         l_return :=
8745           inv_material_status_grp.is_status_applicable(
8746             p_wms_installed             => 'TRUE'
8747           , p_trx_status_enabled        => NULL
8748           , p_trx_type_id               => l_transaction_type
8749           , p_lot_status_enabled        => NULL
8750           , p_serial_status_enabled     => NULL
8751           , p_organization_id           => p_organization_id
8752           , p_inventory_item_id         => l_item_id
8753           , p_sub_code                  => NULL
8754           , p_locator_id                => NULL
8755           , p_lot_number                => l_lot_number
8756           , p_serial_number             => NULL
8757           , p_object_type               => 'O'
8758           );
8759 
8760         IF (l_debug = 1) THEN
8761           mydebug('Lot material status valid: ' || l_return);
8762         END IF;
8763 
8764         l_progress := '40';
8765 
8766         -- The function returned 'N' so the lot is not valid
8767         -- for the transaction type in the current move order line
8768         IF (l_return = 'N') THEN
8769           x_invalid_value := l_lot_number;
8770           EXIT;
8771         END IF;
8772       END IF;
8773 
8774       -- Check serial status if serial controlled
8775       IF (l_serial_control_code <> 1) THEN
8776         -- Loop through each serial for the item in the LPN
8777         OPEN l_serial_cursor;
8778 
8779         LOOP
8780           FETCH l_serial_cursor INTO l_serial_number;
8781           EXIT WHEN l_serial_cursor%NOTFOUND;
8782 
8783           IF (l_debug = 1) THEN
8784             mydebug('Current serial: ' || l_serial_number);
8785           END IF;
8786 
8787           l_progress := '50';
8788           -- Check if the serial material status is valid
8789           -- for the current move order line's transaction type
8790           l_return :=
8791             inv_material_status_grp.is_status_applicable(
8792               p_wms_installed             => 'TRUE'
8793             , p_trx_status_enabled        => NULL
8794             , p_trx_type_id               => l_transaction_type
8795             , p_lot_status_enabled        => NULL
8796             , p_serial_status_enabled     => NULL
8797             , p_organization_id           => p_organization_id
8798             , p_inventory_item_id         => l_item_id
8799             , p_sub_code                  => NULL
8800             , p_locator_id                => NULL
8801             , p_lot_number                => NULL
8802             , p_serial_number             => l_serial_number
8803             , p_object_type               => 'S'
8804             );
8805 
8806           IF (l_debug = 1) THEN
8807             mydebug('Serial material status valid: ' || l_return);
8808           END IF;
8809 
8810           l_progress := '60';
8811 
8812           -- The function returned 'N' so the serial is not valid
8813           -- for the transaction type in the current move order line
8814           IF (l_return = 'N') THEN
8815             x_invalid_value := l_serial_number;
8816             EXIT;
8817           END IF;
8818         END LOOP;
8819 
8820         CLOSE l_serial_cursor;
8821 
8822         -- If any one of the serials did not pass validation,
8823         -- exit the l_item_txn_cursor loop.  No further validation
8824         -- is required since validation has already failed.
8825         IF (l_return = 'N') THEN
8826           EXIT;
8827         END IF;
8828       END IF;
8829     END LOOP;
8830 
8831     CLOSE l_item_txn_cursor;
8832 
8833     -- Finished validating lot and serial statuses
8834     IF (l_debug = 1) THEN
8835       mydebug('Validation passed: ' || l_return);
8836       mydebug('Invalid value: ' || x_invalid_value);
8837     END IF;
8838 
8839     l_progress := '70';
8840     -- Set the output variable
8841     x_validation_passed := l_return;
8842     x_return_status := fnd_api.g_ret_sts_success;
8843 
8844     IF (l_debug = 1) THEN
8845       mydebug('***End of validate_lot_serial_status***');
8846     END IF;
8847   EXCEPTION
8848     WHEN fnd_api.g_exc_error THEN
8849       ROLLBACK TO validate_status_sp;
8850       x_validation_passed := 'N';
8851       x_return_status := fnd_api.g_ret_sts_error;
8852       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8853 
8854       IF (l_debug = 1) THEN
8855         mydebug('Exiting validate_lot_serial_status - Execution error: ' || l_progress || ' '
8856           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8857       END IF;
8858     WHEN fnd_api.g_exc_unexpected_error THEN
8859       ROLLBACK TO validate_status_sp;
8860       x_validation_passed := 'N';
8861       x_return_status := fnd_api.g_ret_sts_unexp_error;
8862       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8863 
8864       IF (l_debug = 1) THEN
8865         mydebug('Exiting validate_lot_serial_status - Unexpected error: ' || l_progress || ' '
8866           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8867       END IF;
8868     WHEN OTHERS THEN
8869       ROLLBACK TO validate_status_sp;
8870       x_validation_passed := 'N';
8871       x_return_status := fnd_api.g_ret_sts_unexp_error;
8872 
8873       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
8874         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
8875       END IF;
8876 
8877       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8878 
8879       IF (l_debug = 1) THEN
8880         mydebug('Exiting validate_lot_serial_status - Others exception: ' || l_progress || ' '
8881           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
8882       END IF;
8883   END validate_lot_serial_status;
8884 
8885   PROCEDURE revert_loc_suggested_capacity(
8886     x_return_status    OUT NOCOPY     VARCHAR2
8887   , x_msg_count        OUT NOCOPY     NUMBER
8888   , x_msg_data         OUT NOCOPY     VARCHAR2
8889   , p_organization_id  IN             NUMBER
8890   , p_lpn_id           IN             NUMBER
8891   ) IS
8892     l_api_name CONSTANT VARCHAR2(30) := 'revert_loc_suggested_capacity';
8893     l_progress          VARCHAR2(10);
8894     l_item_id           NUMBER;
8895     l_locator_id        NUMBER;
8896     l_quantity          NUMBER;
8897     l_uom_code          VARCHAR2(3);
8898     l_debug             NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8899     l_parent_line_id    NUMBER; --6962664
8900     l_txn_header_id     NUMBER; --6962664
8901 
8902     -- Bug# 3434940 - Performance Fixes
8903     -- Cursor to retrieve the nested LPNs within a given outer LPN
8904     CURSOR nested_lpn_cursor IS
8905        SELECT lpn_id
8906 	 FROM wms_license_plate_numbers
8907 	 START WITH lpn_id = p_lpn_id
8908 	 CONNECT BY PRIOR lpn_id = parent_lpn_id;
8909     l_current_lpn_id    NUMBER;
8910 
8911     -- Bug# 3434940 - Performance Fixes
8912     -- Take the CONNECT by clause out and use an outer cursor loop
8913     -- to get all of the nested LPNs
8914     CURSOR l_suggestions_cursor IS
8915      SELECT mmtt.inventory_item_id
8916        --BUG 3541045: For Inventory Move, suggested sub/loc will be stamped
8917        --on transfer_subinventory/transfer_to_location.  So, look at that
8918        --first.  Only if it is null should we use the locator_id
8919            , Nvl(mmtt.transfer_to_location,mmtt.locator_id) locator_id
8920            , mmtt.transaction_quantity
8921            , mmtt.transaction_uom
8922 	   , mmtt.parent_line_id         --6962664
8923            , mmtt.transaction_header_id  --6962664
8924       FROM   mtl_material_transactions_temp mmtt,
8925              mtl_txn_request_lines mtrl
8926       WHERE  mmtt.move_order_line_id = mtrl.line_id
8927         AND  mmtt.organization_id = p_organization_id
8928         AND  mmtt.lpn_id = l_current_lpn_id
8929         AND  NVL(mmtt.wms_task_type, 0) <> -1;
8930 
8931   BEGIN
8932     IF (l_debug = 1) THEN
8933       mydebug('***Calling revert_loc_suggested_capacity***');
8934       mydebug('Org ID: => ' || p_organization_id);
8935       mydebug('LPN ID: => ' || p_lpn_id);
8936     END IF;
8937 
8938     -- Set the savepoint
8939     SAVEPOINT revert_capacity_sp;
8940     l_progress := '10';
8941 
8942     -- Bug# 3434940 - Performance Fixes
8943     -- Loop through the nested LPNs first
8944     OPEN nested_lpn_cursor;
8945     LOOP
8946        FETCH nested_lpn_cursor INTO l_current_lpn_id;
8947        EXIT WHEN nested_lpn_cursor%NOTFOUND;
8948        IF (l_debug = 1) THEN
8949 	  mydebug('Current LPN ID: ' || l_current_lpn_id);
8950        END IF;
8951 
8952        -- Loop through each suggested MMTT line for the current LPN
8953        OPEN l_suggestions_cursor;
8954        LOOP
8955 	  FETCH l_suggestions_cursor INTO l_item_id, l_locator_id, l_quantity, l_uom_code,l_parent_line_id,l_txn_header_id; --6962664 added last two variables
8956 	  EXIT WHEN l_suggestions_cursor%NOTFOUND;
8957     --6962664 START
8958           mydebug('Locator id value is '|| l_locator_id);
8959           IF l_locator_id IS NULL THEN
8960           BEGIN
8961 	  mydebug('Getting locator value using parent mmtt as locator value is null');
8962           SELECT locator_id INTO l_locator_id
8963           FROM mtl_material_transactions_temp
8964           WHERE transaction_temp_id = l_parent_line_id
8965           AND transaction_header_id = l_txn_header_id;
8966 
8967           EXCEPTION
8968           WHEN No_Data_Found THEN
8969           mydebug('The locator id could not be fetched  ');
8970           WHEN OTHERS THEN
8971           mydebug('Unknown Exception');
8972           END;
8973           END IF;
8974     --6962664  END
8975 
8976 	  IF (l_debug = 1) THEN
8977 	     mydebug('Current MMTT suggestion values:');
8978 	     mydebug('Inventory Item ID: => ' || l_item_id);
8979 	     mydebug('Locator ID: ========> ' || l_locator_id);
8980 	     mydebug('Transaction qty: ===> ' || l_quantity);
8981 	     mydebug('Transaction UOM: ===> ' || l_uom_code);
8982 	  END IF;
8983 
8984 	  l_progress := '20';
8985 
8986 	  IF (l_debug = 1) THEN
8987 	     mydebug('Call INV_LOC_WMS_UTILS.revert_loc_suggested_capacity API');
8988 	  END IF;
8989 
8990 	  inv_loc_wms_utils.revert_loc_suggested_capacity
8991 	    (x_return_status             => x_return_status
8992 	     , x_msg_count                 => x_msg_count
8993 	     , x_msg_data                  => x_msg_data
8994 	     , p_organization_id           => p_organization_id
8995 	     , p_inventory_location_id     => l_locator_id
8996 	     , p_inventory_item_id         => l_item_id
8997 	     , p_primary_uom_flag          => 'N'
8998 	     , p_transaction_uom_code      => l_uom_code
8999 	     , p_quantity                  => l_quantity
9000 	     );
9001 	  l_progress := '30';
9002 
9003 	  IF (x_return_status = fnd_api.g_ret_sts_success) THEN
9004 	     IF (l_debug = 1) THEN
9005 		mydebug('Success returned from revert_loc_suggested_capacity API');
9006 	     END IF;
9007 	   ELSE
9008 	     IF (l_debug = 1) THEN
9009 		mydebug('Failure returned from revert_loc_suggested_capacity API');
9010 	     END IF;
9011 	     -- Bug 5393727: do not raise an exception if revert API returns an error
9012 	     -- RAISE fnd_api.g_exc_error;
9013 	  END IF;
9014 
9015 	  l_progress := '40';
9016        END LOOP;
9017        CLOSE l_suggestions_cursor;
9018 
9019     END LOOP;
9020     CLOSE nested_lpn_cursor;
9021 
9022     -- Set the output variable
9023     x_return_status := fnd_api.g_ret_sts_success;
9024     l_progress := '50';
9025 
9026     IF (l_debug = 1) THEN
9027       mydebug('***End of revert_loc_suggested_capacity***');
9028     END IF;
9029   EXCEPTION
9030     WHEN fnd_api.g_exc_error THEN
9031       ROLLBACK TO revert_capacity_sp;
9032       x_return_status := fnd_api.g_ret_sts_error;
9033       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9034 
9035       IF (l_debug = 1) THEN
9036         mydebug(
9037           'Exiting revert_loc_suggested_capacity - Execution error: ' || l_progress || ' '
9038           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
9039         );
9040       END IF;
9041     WHEN fnd_api.g_exc_unexpected_error THEN
9042       ROLLBACK TO revert_capacity_sp;
9043       x_return_status := fnd_api.g_ret_sts_unexp_error;
9044       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9045 
9046       IF (l_debug = 1) THEN
9047         mydebug(
9048           'Exiting revert_loc_suggested_capacity - Unexpected error: ' || l_progress || ' '
9049           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
9050         );
9051       END IF;
9052     WHEN OTHERS THEN
9053       ROLLBACK TO revert_capacity_sp;
9054       x_return_status := fnd_api.g_ret_sts_unexp_error;
9055 
9056       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
9057         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
9058       END IF;
9059 
9060       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9061 
9062       IF (l_debug = 1) THEN
9063         mydebug(
9064           'Exiting revert_loc_suggested_capacity - Others exception: ' || l_progress || ' '
9065           || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS')
9066         );
9067       END IF;
9068   END revert_loc_suggested_capacity;
9069 
9070   --{{
9071   -- Test the flow where check_for_crossdock is called
9072   --}}
9073   PROCEDURE check_for_crossdock(
9074     p_organization_id  IN             NUMBER
9075   , p_lpn_id           IN             NUMBER
9076   , x_return_status    OUT NOCOPY     VARCHAR2
9077   , x_msg_count        OUT NOCOPY     NUMBER
9078   , x_msg_data         OUT NOCOPY     VARCHAR2
9079   , x_crossdock        OUT NOCOPY     VARCHAR2
9080   ) IS
9081     l_api_name CONSTANT VARCHAR2(30) := 'check_for_crossdock';
9082     l_progress          VARCHAR2(10);
9083     l_cross_dock_flag   NUMBER;
9084     l_ret_crossdock     NUMBER;
9085     l_debug             NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
9086   BEGIN
9087     IF (l_debug = 1) THEN
9088       mydebug('***Calling check_for_crossdock***');
9089       mydebug('Org ID: => ' || p_organization_id);
9090       mydebug('LPN ID: => ' || p_lpn_id);
9091     END IF;
9092 
9093     -- Set the savepoint
9094     SAVEPOINT check_crossdock_sp;
9095     l_progress := '10';
9096 
9097     x_crossdock := 'N';
9098 
9099     BEGIN
9100        SELECT 'Y'
9101 	 INTO x_crossdock
9102 	 FROM dual
9103 	 WHERE exists
9104 	 (SELECT 1
9105 	  FROM mtl_txn_request_lines
9106 	  WHERE lpn_id = p_lpn_id
9107 	  AND organization_id = p_organization_id
9108 	  AND backorder_delivery_detail_id IS NOT NULL);
9109     EXCEPTION
9110        WHEN OTHERS THEN
9111 	  x_crossdock := 'N';
9112     END ;
9113 
9114 /*
9115     -- Check to see if cross dock is enabled for the org
9116     SELECT NVL(crossdock_flag, 2)
9117     INTO   l_cross_dock_flag
9118     FROM   mtl_parameters
9119     WHERE  organization_id = p_organization_id;
9120 
9121     IF (l_debug = 1) THEN
9122       mydebug('Cross Dock Flag: ' || l_cross_dock_flag);
9123     END IF;
9124 
9125     l_progress := '20';
9126 
9127     IF (l_cross_dock_flag = 1) THEN
9128       IF (l_debug = 1) THEN
9129         mydebug('Crossdock is enabled so check for it');
9130       END IF;
9131 
9132       -- Call the cross dock API
9133       wms_cross_dock_pvt.crossdock(
9134         p_org_id            => p_organization_id
9135       , p_lpn               => p_lpn_id
9136       , x_ret               => l_ret_crossdock
9137       , x_return_status     => x_return_status
9138       , x_msg_count         => x_msg_count
9139       , x_msg_data          => x_msg_data
9140       );
9141 
9142       IF (l_debug = 1) THEN
9143         mydebug('Finished calling WMS_Cross_Dock_Pvt.crossdock API');
9144       END IF;
9145 
9146       l_progress := '30';
9147       -- See if there are any error messages returned from the API
9148       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9149 
9150       IF (x_msg_count = 0) THEN
9151         IF (l_debug = 1) THEN
9152           mydebug('Successful');
9153         END IF;
9154       ELSIF(x_msg_count = 1) THEN
9155         IF (l_debug = 1) THEN
9156           mydebug('Not Successful');
9157           mydebug('Error message: ' || REPLACE(x_msg_data, fnd_global.local_chr(0), ' '));
9158         END IF;
9159       ELSE
9160         IF (l_debug = 1) THEN
9161           mydebug('Not Successful2');
9162         END IF;
9163 
9164         FOR i IN 1 .. x_msg_count LOOP
9165           x_msg_data := fnd_msg_pub.get(i, 'F');
9166 
9167           IF (l_debug = 1) THEN
9168             mydebug('Error messages: ' || REPLACE(x_msg_data, fnd_global.local_chr(0), ' '));
9169           END IF;
9170         END LOOP;
9171       END IF;
9172 
9173       l_progress := '40';
9174 
9175       -- Check the return status from the API call
9176       IF (x_return_status = fnd_api.g_ret_sts_success) THEN
9177         IF (l_debug = 1) THEN
9178           mydebug('Success returned from WMS_Cross_Dock_Pvt.crossdock API');
9179         END IF;
9180       ELSE
9181         IF (l_debug = 1) THEN
9182           mydebug('Failure returned from WMS_Cross_Dock_Pvt.crossdock API');
9183         END IF;
9184 
9185         RAISE fnd_api.g_exc_error;
9186       END IF;
9187 
9188       l_progress := '50';
9189 
9190       -- Check the cross dock return value
9191       IF (l_ret_crossdock = 1) THEN
9192         IF (l_debug = 1) THEN
9193           mydebug('Nothing to Cross Dock');
9194         END IF;
9195 
9196         x_crossdock := 'N';
9197       ELSIF(l_ret_crossdock = 0) THEN
9198         IF (l_debug = 1) THEN
9199           mydebug('Cross Dock Succeeded');
9200         END IF;
9201 
9202         x_crossdock := 'Y';
9203       ELSE
9204         IF (l_debug = 1) THEN
9205           mydebug('Cross Dock Errored');
9206         END IF;
9207 
9208         x_crossdock := 'N';
9209       END IF;
9210 
9211       l_progress := '60';
9212     ELSE
9213       IF (l_debug = 1) THEN
9214         mydebug('Cross Dock is not enabled');
9215       END IF;
9216 
9217       x_crossdock := 'N';
9218     END IF;
9219 */
9220     -- Set the output variable
9221     x_return_status := fnd_api.g_ret_sts_success;
9222     l_progress := '70';
9223 
9224     IF (l_debug = 1) THEN
9225       mydebug('***End of check_for_crossdock***');
9226     END IF;
9227   EXCEPTION
9228     WHEN fnd_api.g_exc_error THEN
9229       ROLLBACK TO check_crossdock_sp;
9230       x_crossdock := 'N';
9231       x_return_status := fnd_api.g_ret_sts_error;
9232       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9233 
9234       IF (l_debug = 1) THEN
9235 	 mydebug('Exiting check_for_crossdock - Execution error: ' ||
9236 		 l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
9237       END IF;
9238     WHEN fnd_api.g_exc_unexpected_error THEN
9239       ROLLBACK TO check_crossdock_sp;
9240       x_crossdock := 'N';
9241       x_return_status := fnd_api.g_ret_sts_unexp_error;
9242       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9243 
9244       IF (l_debug = 1) THEN
9245 	 mydebug('Exiting check_for_crossdock - Unexpected error: ' ||
9246 		 l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
9247       END IF;
9248     WHEN OTHERS THEN
9249       ROLLBACK TO check_crossdock_sp;
9250       x_crossdock := 'N';
9251       x_return_status := fnd_api.g_ret_sts_unexp_error;
9252 
9253       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
9254         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
9255       END IF;
9256 
9257       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9258 
9259       IF (l_debug = 1) THEN
9260 	 mydebug('Exiting check_for_crossdock - Others exception: ' ||
9261 		 l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'));
9262       END IF;
9263   END check_for_crossdock;
9264 
9265 
9266 END wms_task_dispatch_put_away;