DBA Data[Home] [Help]

PACKAGE BODY: APPS.WMS_CARTNZN_PUB

Source


1 PACKAGE BODY WMS_CARTNZN_PUB AS
2 /* $Header: WMSCRTNB.pls 120.26.12020000.7 2013/02/22 03:16:21 sahmahes ship $*/
3 --
4 -- File        : WMSCRTNB.pls
5 -- Content     : WMS_CARTNZN_PUB package specification
6 -- Description : WMS cartonization API
7 -- Notes       :
8 -- Modified    : 09/12/2000 cjandhya created
9 
10 
11 -- API name    : cartonize
12 -- Type        : group
13 -- Function    : populates the cartonization_id, container_item_id columns,
14 --               of rows belonging to a particular move order header id in
15 --               mtl_material_transactions_temp.
16 
17 -- Pre-reqs    :  Those columns wouldn't be populated if the cartonization_id
18 --                for that row is already populated,
19 --                or if values for organization_id, inventory_item_id ,
20 --                primary qunatity, transaction_quantity,  transaction_uom,
21 --                trans action_temp_id are not all filled or if there is no
22 --                conversion defined between primary and transaction uoms of
23 --                the item of interest. each item has to be assigned to a
24 --                category of contained_item category set and that category
25 --                should have some container items.
26 --                The lines that can be packed together are identified by the
27 --                carton_grouping_id(MTL_TXN_REQEST_LINES) for the
28 --                move_order_line_id of that line.
29 
30 
31 -- Parameters  :
32 --   p_api_version          Standard Input Parameter
33 --   p_init_msg_list        Standard Input Parameter
34 --   p_commit               Standard Input Parameter
35 --   p_validation_level     Standard Input Parameter
36 --   p_out_bound            specifies if the call is for outbound process
37 --   p_org_id                 organization_id
38 --   p_move_order_header_id header_id for the lines to be cartonized
39 
40 
41 -- Output Parameters
42 --   x_return_status        Standard Output Parameter
43 --   x_msg_count            Standard Output Parameter
44 --   x_msg_data             Standard Output Parameter
45 
46 -- Version
47 --   Currently version is 1.0
48 
49    PROCEDURE rulebased_cartonization(
50                       x_return_status         OUT   NOCOPY VARCHAR2,
51                       x_msg_count             OUT   NOCOPY NUMBER,
52                       x_msg_data              OUT   NOCOPY VARCHAR2,
53                       p_out_bound             IN    VARCHAR2,
54                       p_org_id                IN    NUMBER,
55                       p_move_order_header_id  IN    NUMBER,
56                       p_input_for_bulk        IN    WMS_BULK_PICK.bulk_input_rec  DEFAULT null
57                       );
58 
59 
60  G_PKG_NAME   CONSTANT VARCHAR2(30) := 'WMS_CARTNZN_PUB';
61  g_current_release_level CONSTANT NUMBER  :=  WMS_CONTROL.G_CURRENT_RELEASE_LEVEL;
62  g_j_release_level  CONSTANT NUMBER := INV_RELEASE.G_J_RELEASE_LEVEL;
63  g_k_release_level  CONSTANT NUMBER := INV_RELEASE.G_K_RELEASE_LEVEL;  -- ER : 6682436
64  g_move_order_pick_wave CONSTANT NUMBER       := inv_globals.g_move_order_pick_wave;
65 
66 
67  CURR_HEADER_ID_FOR_MIXED  NUMBER;
68  PREV_HEADER_ID_FOR_MIXED  NUMBER;
69 
70  -- variable used to store the error code, pushed into stack on error
71  ERROR_CODE            VARCHAR2(30)  := NULL;
72  ERROR_MSG             VARCHAR2(255) := NULL;
73 
74 
75  --  Record type used to store the original and the transaction_temp_id
76  --  split from it
77 
78  TYPE t_temp_id_rel_rec IS RECORD
79    (
80      orig_temp_id mtl_material_transactions_temp.transaction_temp_id%TYPE ,
81      splt_temp_id mtl_material_transactions_temp.transaction_temp_id%TYPE,
82      primary_quantity NUMBER,
83      secondary_quantity NUMBER,
84      processed VARCHAR2(1) := 'N');
85 
86 
87  TYPE t_temp_id_rel_tb IS TABLE OF t_temp_id_rel_rec INDEX BY  BINARY_INTEGER;
88 
89  --  TABLE used to store the original and the transaction_temp_id
90  --  split from it
91 
92  TEMP_ID_TABLE       t_temp_id_rel_tb;
93 
94  TEMP_ID_TABLE_INDEX NUMBER := 0;
95 
96  --Variable storing if log is enabled
97  g_trace_on           NUMBER := NULL;
98 
99   --for device integration
100  SUBTYPE WDR_ROW IS WMS_DEVICE_REQUESTS%ROWTYPE;
101 
102  SUBTYPE MMTT_ROW_TYPE IS MTL_MATERIAL_TRANSACTIONS_TEMP%ROWTYPE;
103 
104  -- Table that needs to be passed inv_rcv_common_apis for splitting
105  -- mtl_transaction_lots_temp and mtl_serial_numbers_temp
106 
107  api_table inv_rcv_common_apis.trans_rec_tb_tp;
108 
109  -- This value is incremented and inserted into sequence_id of the table
110  -- wms_packaging_hist on every insert
111  --sequence_id      NUMBER := 1;
112 
113 
114  dummy_header_id  NUMBER := -1;
115  dummy_temp_id    NUMBER := -1;
116 
117  -- Mode in which cartonization is called
118  packaging_mode      NUMBER := 1;
119 
120  -- caching items associated with LPNs and packages
121  cache_package_id     NUMBER := -1;
122  cache_pkg_item_id    NUMBER := NULL;
123 
124  cache_lpn_id         NUMBER := -1;
125  cache_lpn_item_id    NUMBER := NULL;
126 
127  --Bug 2520774 fix
128  l_auto_pick_confirm_flag   VARCHAR2(1)   := 'N';
129 
130 
131  FUNCTION to_mmtt(wct_rec wct_row_type) RETURN mmtt_row_type
132    IS
133       mmt_rec mmtt_row_type;
134  BEGIN
135 
136     mmt_rec.transaction_header_id:=wct_rec.transaction_header_id;
137     mmt_rec.transaction_temp_id:=wct_rec.transaction_temp_id;
138     mmt_rec.source_code:=wct_rec.source_code;
139     mmt_rec.source_line_id:=wct_rec.source_line_id;
140     mmt_rec.transaction_mode:=wct_rec.transaction_mode;
141     mmt_rec.lock_flag:=wct_rec.lock_flag;
142     mmt_rec.last_update_date:=wct_rec.last_update_date;
143     mmt_rec.last_updated_by:=wct_rec.last_updated_by;
144     mmt_rec.creation_date:=wct_rec.creation_date;
145     mmt_rec.created_by:=wct_rec.created_by;
146     mmt_rec.last_update_login:=wct_rec.last_update_login;
147     mmt_rec.request_id:=wct_rec.request_id;
148     mmt_rec.program_application_id:=wct_rec.program_application_id;
149     mmt_rec.program_id:=wct_rec.program_id;
150     mmt_rec.program_update_date:=wct_rec.program_update_date;
151     mmt_rec.inventory_item_id:=wct_rec.inventory_item_id;
152     mmt_rec.revision:=wct_rec.revision;
153     mmt_rec.organization_id:=wct_rec.organization_id;
154     mmt_rec.subinventory_code:=wct_rec.subinventory_code;
155     mmt_rec.locator_id:=wct_rec.locator_id;
156     mmt_rec.transaction_quantity:=wct_rec.transaction_quantity;
157     mmt_rec.primary_quantity:=wct_rec.primary_quantity;
158     mmt_rec.transaction_uom:=wct_rec.transaction_uom;
159     mmt_rec.transaction_cost:=wct_rec.transaction_cost;
160     mmt_rec.transaction_type_id:=wct_rec.transaction_type_id;
161     mmt_rec.transaction_action_id:=wct_rec.transaction_action_id;
162     mmt_rec.transaction_source_type_id:=wct_rec.transaction_source_type_id;
163     mmt_rec.transaction_source_id:=wct_rec.transaction_source_id;
164     mmt_rec.transaction_source_name:=wct_rec.transaction_source_name;
165     mmt_rec.transaction_date:=wct_rec.transaction_date;
166     mmt_rec.acct_period_id:=wct_rec.acct_period_id;
167     mmt_rec.distribution_account_id:=wct_rec.distribution_account_id;
168     mmt_rec.transaction_reference:=wct_rec.transaction_reference;
169     mmt_rec.requisition_line_id:=wct_rec.requisition_line_id;
170     mmt_rec.requisition_distribution_id:=wct_rec.requisition_distribution_id;
171     mmt_rec.reason_id:=wct_rec.reason_id;
172     mmt_rec.lot_number:=wct_rec.lot_number;
173     mmt_rec.lot_expiration_date:=wct_rec.lot_expiration_date;
174     mmt_rec.serial_number:=wct_rec.serial_number;
175     mmt_rec.receiving_document:=wct_rec.receiving_document;
176     mmt_rec.demand_id:=wct_rec.demand_id;
177     mmt_rec.rcv_transaction_id:=wct_rec.rcv_transaction_id;
178     mmt_rec.move_transaction_id:=wct_rec.move_transaction_id;
179     mmt_rec.completion_transaction_id:=wct_rec.completion_transaction_id;
180     mmt_rec.wip_entity_type:=wct_rec.wip_entity_type;
181     mmt_rec.schedule_id:=wct_rec.schedule_id;
182     mmt_rec.repetitive_line_id:=wct_rec.repetitive_line_id;
183     mmt_rec.employee_code:=wct_rec.employee_code;
184     mmt_rec.primary_switch:=wct_rec.primary_switch;
185     mmt_rec.schedule_update_code:=wct_rec.schedule_update_code;
186     mmt_rec.setup_teardown_code:=wct_rec.setup_teardown_code;
187     mmt_rec.item_ordering:=wct_rec.item_ordering;
188     mmt_rec.negative_req_flag:=wct_rec.negative_req_flag;
189     mmt_rec.operation_seq_num:=wct_rec.operation_seq_num;
190     mmt_rec.picking_line_id:=wct_rec.picking_line_id;
191     mmt_rec.trx_source_line_id:=wct_rec.trx_source_line_id;
192     mmt_rec.trx_source_delivery_id:=wct_rec.trx_source_delivery_id;
193     mmt_rec.physical_adjustment_id:=wct_rec.physical_adjustment_id;
194     mmt_rec.cycle_count_id:=wct_rec.cycle_count_id;
195     mmt_rec.rma_line_id:=wct_rec.rma_line_id;
196     mmt_rec.customer_ship_id:=wct_rec.customer_ship_id;
197     mmt_rec.currency_code:=wct_rec.currency_code;
198     mmt_rec.currency_conversion_rate:=wct_rec.currency_conversion_rate;
199     mmt_rec.currency_conversion_type:=wct_rec.currency_conversion_type;
200     mmt_rec.currency_conversion_date:=wct_rec.currency_conversion_date;
201     mmt_rec.ussgl_transaction_code:=wct_rec.ussgl_transaction_code;
202     mmt_rec.vendor_lot_number:=wct_rec.vendor_lot_number;
203     mmt_rec.encumbrance_account:=wct_rec.encumbrance_account;
204     mmt_rec.encumbrance_amount:=wct_rec.encumbrance_amount;
205     mmt_rec.ship_to_location:=wct_rec.ship_to_location;
206     mmt_rec.shipment_number:=wct_rec.shipment_number;
207     mmt_rec.transfer_cost:=wct_rec.transfer_cost;
208     mmt_rec.transportation_cost:=wct_rec.transportation_cost;
209     mmt_rec.transportation_account:=wct_rec.transportation_account;
210     mmt_rec.freight_code:=wct_rec.freight_code;
211     mmt_rec.containers:=wct_rec.containers;
212     mmt_rec.waybill_airbill:=wct_rec.waybill_airbill;
213     mmt_rec.expected_arrival_date:=wct_rec.expected_arrival_date;
214     mmt_rec.transfer_subinventory:=wct_rec.transfer_subinventory;
215     mmt_rec.transfer_organization:=wct_rec.transfer_organization;
216     mmt_rec.transfer_to_location:=wct_rec.transfer_to_location;
217     mmt_rec.new_average_cost:=wct_rec.new_average_cost;
218     mmt_rec.value_change:=wct_rec.value_change;
219     mmt_rec.percentage_change:=wct_rec.percentage_change;
220     mmt_rec.material_allocation_temp_id:=wct_rec.material_allocation_temp_id;
221     mmt_rec.demand_source_header_id:=wct_rec.demand_source_header_id;
222     mmt_rec.demand_source_line:=wct_rec.demand_source_line;
223     mmt_rec.demand_source_delivery:=wct_rec.demand_source_delivery;
224     mmt_rec.item_segments:=wct_rec.item_segments;
225     mmt_rec.item_description:=wct_rec.item_description;
226     mmt_rec.item_trx_enabled_flag:=wct_rec.item_trx_enabled_flag;
227     mmt_rec.item_location_control_code:=wct_rec.item_location_control_code;
228     mmt_rec.item_restrict_subinv_code:=wct_rec.item_restrict_subinv_code;
229     mmt_rec.item_restrict_locators_code:=wct_rec.item_restrict_locators_code;
230     mmt_rec.item_revision_qty_control_code:=wct_rec.item_revision_qty_control_code;
231     mmt_rec.item_primary_uom_code:=wct_rec.item_primary_uom_code;
232     mmt_rec.item_uom_class:=wct_rec.item_uom_class;
233     mmt_rec.item_shelf_life_code:=wct_rec.item_shelf_life_code;
234     mmt_rec.item_shelf_life_days:=wct_rec.item_shelf_life_days;
235     mmt_rec.item_lot_control_code:=wct_rec.item_lot_control_code;
236     mmt_rec.item_serial_control_code:=wct_rec.item_serial_control_code;
237     mmt_rec.item_inventory_asset_flag:=wct_rec.item_inventory_asset_flag;
238     mmt_rec.allowed_units_lookup_code:=wct_rec.allowed_units_lookup_code;
239     mmt_rec.department_id:=wct_rec.department_id;
240     mmt_rec.department_code:=wct_rec.department_code;
241     mmt_rec.wip_supply_type:=wct_rec.wip_supply_type;
242     mmt_rec.supply_subinventory:=wct_rec.supply_subinventory;
243     mmt_rec.supply_locator_id:=wct_rec.supply_locator_id;
244     mmt_rec.valid_subinventory_flag:=wct_rec.valid_subinventory_flag;
245     mmt_rec.valid_locator_flag:=wct_rec.valid_locator_flag;
246     mmt_rec.locator_segments:=wct_rec.locator_segments;
247     mmt_rec.current_locator_control_code:=wct_rec.current_locator_control_code;
248     mmt_rec.number_of_lots_entered:=wct_rec.number_of_lots_entered;
249     mmt_rec.wip_commit_flag:=wct_rec.wip_commit_flag;
250     mmt_rec.next_lot_number:=wct_rec.next_lot_number;
251     mmt_rec.lot_alpha_prefix:=wct_rec.lot_alpha_prefix;
252     mmt_rec.next_serial_number:=wct_rec.next_serial_number;
253     mmt_rec.serial_alpha_prefix:=wct_rec.serial_alpha_prefix;
254     mmt_rec.shippable_flag:=wct_rec.shippable_flag;
255     mmt_rec.posting_flag:=wct_rec.posting_flag;
256     mmt_rec.required_flag:=wct_rec.required_flag;
257     mmt_rec.process_flag:=wct_rec.process_flag;
258     mmt_rec.error_code:=wct_rec.error_code;
259     mmt_rec.error_explanation:=wct_rec.error_explanation;
260     mmt_rec.attribute_category:=wct_rec.attribute_category;
261     mmt_rec.attribute1:=wct_rec.attribute1;
262     mmt_rec.attribute2:=wct_rec.attribute2;
263     mmt_rec.attribute3:=wct_rec.attribute3;
264     mmt_rec.attribute4:=wct_rec.attribute4;
265     mmt_rec.attribute5:=wct_rec.attribute5;
266     mmt_rec.attribute6:=wct_rec.attribute6;
267     mmt_rec.attribute7:=wct_rec.attribute7;
268     mmt_rec.attribute8:=wct_rec.attribute8;
269     mmt_rec.attribute9:=wct_rec.attribute9;
270     mmt_rec.attribute10:=wct_rec.attribute10;
271     mmt_rec.attribute11:=wct_rec.attribute11;
272     mmt_rec.attribute12:=wct_rec.attribute12;
273     mmt_rec.attribute13:=wct_rec.attribute13;
274     mmt_rec.attribute14:=wct_rec.attribute14;
275     mmt_rec.attribute15:=wct_rec.attribute15;
276     mmt_rec.movement_id:=wct_rec.movement_id;
277     mmt_rec.reservation_quantity:=wct_rec.reservation_quantity;
278     mmt_rec.shipped_quantity:=wct_rec.shipped_quantity;
279     mmt_rec.transaction_line_number:=wct_rec.transaction_line_number;
280     mmt_rec.task_id:=wct_rec.task_id;
281     mmt_rec.to_task_id:=wct_rec.to_task_id;
282     mmt_rec.source_task_id:=wct_rec.source_task_id;
283     mmt_rec.project_id:=wct_rec.project_id;
284     mmt_rec.source_project_id:=wct_rec.source_project_id;
285     mmt_rec.pa_expenditure_org_id:=wct_rec.pa_expenditure_org_id;
286     mmt_rec.to_project_id:=wct_rec.to_project_id;
287     mmt_rec.expenditure_type:=wct_rec.expenditure_type;
288     mmt_rec.final_completion_flag:=wct_rec.final_completion_flag;
289     mmt_rec.transfer_percentage:=wct_rec.transfer_percentage;
290     mmt_rec.transaction_sequence_id:=wct_rec.transaction_sequence_id;
291     mmt_rec.material_account:=wct_rec.material_account;
292     mmt_rec.material_overhead_account:=wct_rec.material_overhead_account;
293     mmt_rec.resource_account:=wct_rec.resource_account;
294     mmt_rec.outside_processing_account:=wct_rec.outside_processing_account;
295     mmt_rec.overhead_account:=wct_rec.overhead_account;
296     mmt_rec.flow_schedule:=wct_rec.flow_schedule;
297     mmt_rec.cost_group_id:=wct_rec.cost_group_id;
298     mmt_rec.demand_class:=wct_rec.demand_class;
299     mmt_rec.qa_collection_id:=wct_rec.qa_collection_id;
300     mmt_rec.kanban_card_id:=wct_rec.kanban_card_id;
301     mmt_rec.overcompletion_transaction_qty:=wct_rec.overcompletion_txn_qty;
302     mmt_rec.overcompletion_primary_qty:=wct_rec.overcompletion_primary_qty;
303     mmt_rec.overcompletion_transaction_id:=wct_rec.overcompletion_transaction_id;
304     mmt_rec.end_item_unit_number:=wct_rec.end_item_unit_number;
305     mmt_rec.scheduled_payback_date:=wct_rec.scheduled_payback_date;
306     mmt_rec.line_type_code:=wct_rec.line_type_code;
307     mmt_rec.parent_transaction_temp_id:=wct_rec.parent_transaction_temp_id;
308     mmt_rec.put_away_strategy_id:=wct_rec.put_away_strategy_id;
309     mmt_rec.put_away_rule_id:=wct_rec.put_away_rule_id;
310     mmt_rec.pick_strategy_id:=wct_rec.pick_strategy_id;
311     mmt_rec.pick_rule_id:=wct_rec.pick_rule_id;
312     mmt_rec.move_order_line_id:=wct_rec.move_order_line_id;
313     mmt_rec.task_group_id:=wct_rec.task_group_id;
314     mmt_rec.pick_slip_number:=wct_rec.pick_slip_number;
315     mmt_rec.reservation_id:=wct_rec.reservation_id;
316     mmt_rec.common_bom_seq_id:=wct_rec.common_bom_seq_id;
317     mmt_rec.common_routing_seq_id:=wct_rec.common_routing_seq_id;
318     mmt_rec.org_cost_group_id:=wct_rec.org_cost_group_id;
319     mmt_rec.cost_type_id:=wct_rec.cost_type_id;
320     mmt_rec.transaction_status:=wct_rec.transaction_status;
321     mmt_rec.standard_operation_id:=wct_rec.standard_operation_id;
322     mmt_rec.task_priority:=wct_rec.task_priority;
323     mmt_rec.wms_task_type:=wct_rec.wms_task_type;
324     mmt_rec.parent_line_id:=wct_rec.parent_line_id;
325     mmt_rec.transfer_cost_group_id:=wct_rec.transfer_cost_group_id;
326     mmt_rec.lpn_id:=wct_rec.lpn_id;
327     mmt_rec.transfer_lpn_id:=wct_rec.transfer_lpn_id;
328     mmt_rec.wms_task_status:=wct_rec.wms_task_status;
329     mmt_rec.content_lpn_id:=wct_rec.content_lpn_id;
330     mmt_rec.container_item_id:=wct_rec.container_item_id;
331     mmt_rec.cartonization_id:=wct_rec.cartonization_id;
332     mmt_rec.pick_slip_date:=wct_rec.pick_slip_date;
333     mmt_rec.rebuild_item_id:=wct_rec.rebuild_item_id;
334     mmt_rec.rebuild_serial_number:=wct_rec.rebuild_serial_number;
335     mmt_rec.rebuild_activity_id:=wct_rec.rebuild_activity_id;
336     mmt_rec.rebuild_job_name:=wct_rec.rebuild_job_name;
337     mmt_rec.organization_type:=wct_rec.organization_type;
338     mmt_rec.transfer_organization_type:=wct_rec.transfer_organization_type;
339     mmt_rec.owning_organization_id:=wct_rec.owning_organization_id;
340     mmt_rec.owning_tp_type:=wct_rec.owning_tp_type;
341     mmt_rec.xfr_owning_organization_id:=wct_rec.xfr_owning_organization_id;
342     mmt_rec.transfer_owning_tp_type:=wct_rec.transfer_owning_tp_type;
343     mmt_rec.planning_organization_id:=wct_rec.planning_organization_id;
344     mmt_rec.planning_tp_type:=wct_rec.planning_tp_type;
345     mmt_rec.xfr_planning_organization_id:=wct_rec.xfr_planning_organization_id;
346     mmt_rec.transfer_planning_tp_type:=wct_rec.transfer_planning_tp_type;
347     mmt_rec.secondary_uom_code:=wct_rec.secondary_uom_code;
348     mmt_rec.secondary_transaction_quantity:=wct_rec.secondary_transaction_quantity;
349     mmt_rec.transaction_batch_id:=wct_rec.transaction_batch_id;
350     mmt_rec.transaction_batch_seq:=wct_rec.transaction_batch_seq;
351     mmt_rec.allocated_lpn_id:=wct_rec.allocated_lpn_id;
352     mmt_rec.schedule_number:=wct_rec.schedule_number;
353     mmt_rec.scheduled_flag:=wct_rec.scheduled_flag;
354     mmt_rec.class_code:=wct_rec.class_code;
355     mmt_rec.schedule_group:=wct_rec.schedule_group;
356     mmt_rec.build_sequence:=wct_rec.build_sequence;
357     mmt_rec.bom_revision:=wct_rec.bom_revision;
358     mmt_rec.routing_revision:=wct_rec.routing_revision;
359     mmt_rec.bom_revision_date:=wct_rec.bom_revision_date;
360     mmt_rec.routing_revision_date:=wct_rec.routing_revision_date;
361     mmt_rec.alternate_bom_designator:=wct_rec.alternate_bom_designator;
362     mmt_rec.alternate_routing_designator:=wct_rec.alternate_routing_designator;
363     mmt_rec.operation_plan_id:=wct_rec.operation_plan_id;
364     mmt_rec.move_order_header_id:=wct_rec.move_order_header_id;
365     mmt_rec.serial_allocated_flag:=wct_rec.serial_allocated_flag;
366     mmt_rec.fulfillment_base:=wct_rec.fulfillment_base;
367 
368     RETURN mmt_rec;
369 
370  END to_mmtt;
371 
372 
373  PROCEDURE create_wct(p_move_order_header_id IN NUMBER,
374                       p_transaction_header_id IN NUMBER,
375                       p_input_for_bulk IN WMS_BULK_PICK.BULK_INPUT_REC := null)
376    IS
377 
378       mmtt_rec MMTT_ROW_TYPE;
379 
380       CURSOR c1(mo_hdr_id NUMBER) IS
381        /* added the index hint with the suggestion of apps performance team */
382         SELECT /*+index (mmtt,mtl_material_trans_temp_n14)*/  mmtt.* FROM
383            mtl_material_transactions_temp mmtt, mtl_txn_request_lines
384            mtrl WHERE mtrl.header_id =  mo_hdr_id AND
385            mmtt.move_order_line_id = mtrl.line_id AND
386            --2513907 fix
387            mmtt.container_item_id IS null
388 		   and mmtt.cartonization_id is null ; ---(Condition "mmtt.cartonization_id is null" is added for the bug14546443)
389 
390       CURSOR c2(txn_hdr_id NUMBER) IS
391          SELECT mmtt.* FROM
392            mtl_material_transactions_temp mmtt
393            WHERE
394            mmtt.transaction_header_id = txn_hdr_id;
395 
396            -- following cursors added for patchset J bulk picking   ---------------
397 
398      CURSOR c11_bulk_all_fast(p_organization_id NUMBER) IS
399          SELECT mmtt.* FROM
400            mtl_material_transactions_temp mmtt,mtl_allocations_gtmp mag
401           WHERE    mmtt.wms_task_status = 8   -- unreleased
402 	   And     nvl(mmtt.lock_flag,'$') <> 'Y'  --Bug#8546026
403            And     mmtt.cartonization_id IS null   -- not cartonized
404            And     mmtt.organization_id = p_organization_id
405            AND     mmtt.allocated_lpn_id IS NULL -- if lpn allocated, no need to do consolidation
406            AND     mmtt.move_order_header_id = mag.move_order_header_id
407            AND    (mmtt.serial_allocated_flag <> 'Y' OR
408                    mmtt.serial_allocated_flag IS NULL ) --Should not bulk
409                       --Bug3628747
410            AND  mmtt.subinventory_code = nvl(p_input_for_bulk.subinventory_code, mmtt.subinventory_code)
411            AND mmtt.inventory_item_id = nvl(p_input_for_bulk.item_id,mmtt.inventory_item_id)
412            AND Exists ( select 1
413                         From wsh_delivery_details wdd,wsh_delivery_assignments_v wda
414                         Where wdd.move_order_line_id = mmtt.move_order_line_id
415                           AND wdd.delivery_detail_id = wda.delivery_Detail_id
416                           AND nvl(wda.delivery_id,-1) = decode(p_input_for_bulk.delivery_id,null,nvl(wda.delivery_id,-1),p_input_for_bulk.delivery_id)
417                       )
418            for update of mmtt.transaction_temp_id; -- to lock the records
419 
420       CURSOR c11_bulk_only_sub_item_fast(p_organization_id NUMBER) IS
421           SELECT mmtt.* FROM
422            mtl_material_transactions_temp mmtt,mtl_allocations_gtmp mag
423           WHERE    mmtt.wms_task_status = 8   -- unreleased
424 	   And     nvl(mmtt.lock_flag,'$') <> 'Y'  --Bug#8546026
425            And     mmtt.cartonization_id IS null   -- not cartonized
426            And     mmtt.organization_id = p_organization_id
427            AND mmtt.allocated_lpn_id IS NULL -- if lpn allocated, no need to do consolidation
428           AND (mmtt.serial_allocated_flag <> 'Y' OR
429                 mmtt.serial_allocated_flag IS NULL ) -- should not bulk those childs
430                --Bug3628747
431            AND mmtt.move_order_header_id = mag.move_order_header_id
432            AND  mmtt.subinventory_code = nvl(p_input_for_bulk.subinventory_code, mmtt.subinventory_code)
433            AND mmtt.inventory_item_id = nvl(p_input_for_bulk.item_id,mmtt.inventory_item_id)
434            AND Exists ( select 1
435                         From wsh_delivery_details wdd,wsh_delivery_assignments_v wda
436                         Where wdd.move_order_line_id = mmtt.move_order_line_id
437                           AND wdd.delivery_detail_id = wda.delivery_Detail_id
438                           AND nvl(wda.delivery_id,-1) = decode(p_input_for_bulk.delivery_id,null,nvl(wda.delivery_id,-1),p_input_for_bulk.delivery_id)
439                       )
440            AND  ( EXISTS (SELECT 1   -- sub is bulk picking enabled
441                          FROM mtl_secondary_inventories msi
442                         WHERE msi.secondary_inventory_name = mmtt.subinventory_code
443                           AND msi.organization_id = mmtt.organization_id
444                           AND msi.enable_bulk_pick = 'Y')
445                OR EXISTS (SELECT 1   -- item is bulk picking enabled
446                             FROM mtl_system_items msi
447                            WHERE msi.inventory_item_id = mmtt.inventory_item_id
448                              AND msi.bulk_picked_flag = 'Y')
449                  )
450            for update of mmtt.transaction_temp_id; -- to lock the records
451 
452 
453      CURSOR c11_bulk_all(p_organization_id NUMBER) IS
454          SELECT mmtt.* FROM
455            mtl_material_transactions_temp mmtt,mtl_txn_request_headers moh,
456            wsh_pick_grouping_rules spg
457           WHERE    mmtt.wms_task_status = 8   -- unreleased
458 	   And     nvl(mmtt.lock_flag,'$') <> 'Y'  --Bug#8546026
459            And     mmtt.cartonization_id IS null   -- not cartonized
460            And     mmtt.organization_id = p_organization_id
461            AND mmtt.allocated_lpn_id IS NULL -- if lpn allocated, no need to do consolidation
462            AND mmtt.move_order_header_id = moh.header_id
463            AND spg.pick_grouping_rule_id = moh.grouping_rule_id
464            AND spg.pick_method <>WMS_GLOBALS.PICK_METHOD_ORDER  -- not order pciking
465            AND moh.move_order_type = INV_GLOBALS.g_move_order_pick_wave   -- pick wave move order only
466                 -- following is the logic for the input parameters
467            AND (mmtt.serial_allocated_flag <> 'Y' OR
468                  mmtt.serial_allocated_flag IS NULL ) -- should not bulk those childs Bug3628747
469            AND  mmtt.subinventory_code = nvl(p_input_for_bulk.subinventory_code, mmtt.subinventory_code)
470            AND mmtt.inventory_item_id = nvl(p_input_for_bulk.item_id,mmtt.inventory_item_id)
471            AND moh.request_number between nvl(p_input_for_bulk.start_mo_request_number,moh.request_number) and
472                            nvl(p_input_for_bulk.end_mo_request_number,moh.request_number)
473            AND moh.creation_date between nvl(p_input_for_bulk.start_release_date,moh.creation_date) and
474                             nvl(p_input_for_bulk.end_release_date,moh.creation_date)
475            AND Exists ( select 1
476                         From wsh_delivery_details wdd,wsh_delivery_assignments_v wda
477                         Where wdd.move_order_line_id = mmtt.move_order_line_id
478                           AND wdd.delivery_detail_id = wda.delivery_Detail_id
479                           AND nvl(wda.delivery_id,-1) = decode(p_input_for_bulk.delivery_id,null,nvl(wda.delivery_id,-1),p_input_for_bulk.delivery_id)
480                       )
481            for update of mmtt.transaction_temp_id; -- to lock the records
482 
483       CURSOR c11_bulk_only_sub_item(p_organization_id NUMBER) IS
484           SELECT mmtt.* FROM
485            mtl_material_transactions_temp mmtt,mtl_txn_request_headers moh,
486            wsh_pick_grouping_rules spg
487           WHERE    mmtt.wms_task_status = 8   -- unreleased
488 	   And     nvl(mmtt.lock_flag,'$') <> 'Y'  --Bug#8546026
489            And     mmtt.cartonization_id IS null   -- not cartonized
490            And     mmtt.organization_id = p_organization_id
491            AND mmtt.allocated_lpn_id IS NULL -- if lpn allocated, no need to do consolidation
492            AND mmtt.move_order_header_id = moh.header_id
493            AND spg.pick_grouping_rule_id = moh.grouping_rule_id
494            AND spg.pick_method <>WMS_GLOBALS.PICK_METHOD_ORDER  -- not order pciking
495             AND (mmtt.serial_allocated_flag <> 'Y' OR
496                  mmtt.serial_allocated_flag IS NULL ) -- should not bulk those lines
497                 -- Bug3628747
498            AND moh.move_order_type = INV_GLOBALS.g_move_order_pick_wave   -- pick wave move order only
499                 -- following is the logic for the input parameters
500            AND  mmtt.subinventory_code = nvl(p_input_for_bulk.subinventory_code, mmtt.subinventory_code)
501            AND mmtt.inventory_item_id = nvl(p_input_for_bulk.item_id,mmtt.inventory_item_id)
502            AND moh.request_number between nvl(p_input_for_bulk.start_mo_request_number,moh.request_number) and
503                            nvl(p_input_for_bulk.end_mo_request_number,moh.request_number)
504            AND moh.creation_date between nvl(p_input_for_bulk.start_release_date,moh.creation_date) and
505                             nvl(p_input_for_bulk.end_release_date,moh.creation_date)
506            AND Exists ( select 1
507                         From wsh_delivery_details wdd,wsh_delivery_assignments_v wda
508                         Where wdd.move_order_line_id = mmtt.move_order_line_id
509                           AND wdd.delivery_detail_id = wda.delivery_Detail_id
510                           AND nvl(wda.delivery_id,-1) = decode(p_input_for_bulk.delivery_id,null,nvl(wda.delivery_id,-1),p_input_for_bulk.delivery_id)
511                       )
512            AND  ( EXISTS (SELECT 1   -- sub is bulk picking enabled
513                          FROM mtl_secondary_inventories msi
514                         WHERE msi.secondary_inventory_name = mmtt.subinventory_code
515                           AND msi.organization_id = mmtt.organization_id
516                           AND msi.enable_bulk_pick = 'Y')
517                OR EXISTS (SELECT 1   -- item is bulk picking enabled
518                             FROM mtl_system_items msi
519                            WHERE msi.inventory_item_id = mmtt.inventory_item_id
520                              AND msi.bulk_picked_flag = 'Y')
521                  )
522            for update of mmtt.transaction_temp_id; -- to lock the records
523 
524           CURSOR c11_bulk_trip(p_organization_id NUMBER) IS
525           select mmtt.*
526           from   wsh_delivery_legs wdl,wsh_delivery_details wdd, wsh_delivery_assignments_v wda,
527                  wsh_trip_stops wts, mtl_material_transactions_temp mmtt,mtl_txn_request_headers moh,
528                  wsh_pick_grouping_rules spg
529           where wts.trip_id = p_input_for_bulk.trip_id
530            and  wdl.pick_up_stop_id = wts.stop_id
531            and wdl.delivery_id = wda.delivery_id
532            and wda.delivery_detail_id = wdd.delivery_detail_id
533            and mmtt.move_order_line_id = wdd.move_order_line_id
534            and mmtt.wms_task_status = 8   -- unreleased
535            And nvl(mmtt.lock_flag,'$') <> 'Y'  --Bug#8546026
536            And mmtt.cartonization_id IS null   -- not cartonized
537            And mmtt.organization_id = p_organization_id
538            AND mmtt.allocated_lpn_id IS NULL -- if lpn allocated, no need to do consolidation
539            AND mmtt.move_order_header_id = moh.header_id
540            AND spg.pick_grouping_rule_id = moh.grouping_rule_id
541            AND spg.pick_method <>WMS_GLOBALS.PICK_METHOD_ORDER  -- not order pciking
542            AND (mmtt.serial_allocated_flag <> 'Y' OR
543                  mmtt.serial_allocated_flag IS NULL ) -- should not bulk those lines
544              --Bug3628747
545            AND moh.move_order_type = INV_GLOBALS.g_move_order_pick_wave   -- pick wave move order only
546                 -- following is the logic for the input parameters
547            AND  mmtt.subinventory_code = nvl(p_input_for_bulk.subinventory_code, mmtt.subinventory_code)
548            AND mmtt.inventory_item_id = nvl(p_input_for_bulk.item_id,mmtt.inventory_item_id)
549            AND moh.request_number between nvl(p_input_for_bulk.start_mo_request_number,moh.request_number) and
550                            nvl(p_input_for_bulk.end_mo_request_number,moh.request_number)
551            AND moh.creation_date between nvl(p_input_for_bulk.start_release_date,moh.creation_date) and
552                             nvl(p_input_for_bulk.end_release_date,moh.creation_date)
553            AND Exists ( select 1
554                         From wsh_delivery_details wdd,wsh_delivery_assignments_v wda
555                         Where wdd.move_order_line_id = mmtt.move_order_line_id
556                           AND wdd.delivery_detail_id = wda.delivery_Detail_id
557                           AND nvl(wda.delivery_id,-1) = decode(p_input_for_bulk.delivery_id,null,nvl(wda.delivery_id,-1),p_input_for_bulk.delivery_id)
558                       )
559            for update of mmtt.transaction_temp_id; -- to lock the records
560 
561            CURSOR c11_bulk_trip_sub_item(p_organization_id NUMBER) IS
562           select mmtt.*
563           from   wsh_delivery_legs wdl,wsh_delivery_details wdd, wsh_delivery_assignments_v wda,
564                  wsh_trip_stops wts, mtl_material_transactions_temp mmtt,mtl_txn_request_headers moh,
565                  wsh_pick_grouping_rules spg
566           where wts.trip_id = p_input_for_bulk.trip_id
567            and  wdl.pick_up_stop_id = wts.stop_id
568            and wdl.delivery_id = wda.delivery_id
569            and wda.delivery_detail_id = wdd.delivery_detail_id
570            and mmtt.move_order_line_id = wdd.move_order_line_id
571            and mmtt.wms_task_status = 8   -- unreleased
572 	   And nvl(mmtt.lock_flag,'$') <> 'Y'  --Bug#8546026
573            And mmtt.cartonization_id IS null   -- not cartonized
574            And mmtt.organization_id = p_organization_id
575            AND mmtt.allocated_lpn_id IS NULL -- if lpn allocated, no need to do consolidation
576            AND mmtt.move_order_header_id = moh.header_id
577            AND spg.pick_grouping_rule_id = moh.grouping_rule_id
578            AND spg.pick_method <>WMS_GLOBALS.PICK_METHOD_ORDER  -- not order pciking
579            AND (mmtt.serial_allocated_flag <> 'Y' OR
580                  mmtt.serial_allocated_flag IS NULL ) -- should not bulk those lines
581                --Bug3628747
582            AND moh.move_order_type = INV_GLOBALS.g_move_order_pick_wave   -- pick wave move order only
583                 -- following is the logic for the input parameters
584            AND  mmtt.subinventory_code = nvl(p_input_for_bulk.subinventory_code, mmtt.subinventory_code)
585            AND mmtt.inventory_item_id = nvl(p_input_for_bulk.item_id,mmtt.inventory_item_id)
586            AND moh.request_number between nvl(p_input_for_bulk.start_mo_request_number,moh.request_number) and
587                            nvl(p_input_for_bulk.end_mo_request_number,moh.request_number)
588            AND moh.creation_date between nvl(p_input_for_bulk.start_release_date,moh.creation_date) and
589                             nvl(p_input_for_bulk.end_release_date,moh.creation_date)
590            AND Exists ( select 1
591                         From wsh_delivery_details wdd,wsh_delivery_assignments_v wda
592                         Where wdd.move_order_line_id = mmtt.move_order_line_id
593                           AND wdd.delivery_detail_id = wda.delivery_Detail_id
594                           AND nvl(wda.delivery_id,-1) = decode(p_input_for_bulk.delivery_id,null,nvl(wda.delivery_id,-1),p_input_for_bulk.delivery_id)
595                       )
596            AND  ( EXISTS (SELECT 1   -- sub is bulk picking enabled
597                                          FROM mtl_secondary_inventories msi
598                                         WHERE msi.secondary_inventory_name = mmtt.subinventory_code
599                                           AND msi.organization_id = mmtt.organization_id
600                                           AND msi.enable_bulk_pick = 'Y')
601                   OR EXISTS (SELECT 1   -- item is bulk picking enabled
602                                             FROM mtl_system_items msi
603                                            WHERE msi.inventory_item_id = mmtt.inventory_item_id
604                                              AND msi.bulk_picked_flag = 'Y')
605                  )
606            for update of mmtt.transaction_temp_id; -- to lock the records
607            l_organization_id NUMBER;
608            l_return_status   VARCHAR2(1);
609            l_lot_control_code NUMBER;
610            l_serial_control_code NUMBER;
611            l_fast_possible boolean;
612 
613  BEGIN
614 
615     if (g_trace_on = 1) then log_event('Enter create_wct'); END IF;
616 
617     error_code := 'GET_CLPN_ATTR';
618 
619     IF( p_move_order_header_id IS NULL AND
620         p_transaction_header_id IS NULL ) THEN
621 
622        if (g_trace_on = 1) then log_event('Error- mo hdr id and txn hdr id are null'); END IF;
623        RAISE fnd_api.g_exc_unexpected_error;
624 
625     END IF;
626 
627     IF(p_move_order_header_id IS NOT NULL) THEN
628 
629        if (g_trace_on = 1) then log_event(' mo hdr id '||p_move_order_header_id);  END IF;
630 
631        IF (p_move_order_header_id <> -1) THEN     --- patchset J bulk picking    ------------
632            if (g_trace_on = 1) then log_event('calling from pick release'); end if;
633            OPEN c1(p_move_order_header_id);
634        ELSE
635            if (g_trace_on = 1) then --log_event('PATCHSET J-- BULK PICKING -- START');
636                                     log_event('calling from concurrent program'); end if;
637            l_organization_id := p_input_for_bulk.organization_id;
638            if (g_trace_on = 1) then log_event(' organization_id '||l_organization_id); end if;
639 
640            -- make sure the temp table is empty, the following is to improve performance
641            -- when move order number or creation dates are entered
642            delete mtl_allocations_gtmp;
643            l_fast_possible := false;
644            if p_input_for_bulk.start_mo_request_number is not null
645               or p_input_for_bulk.start_release_date is not null then -- select the move_order_headers
646                l_fast_possible := true;
647                insert into mtl_allocations_gtmp
648                (move_order_header_id)
649                   select moh.header_id
650                   from mtl_txn_request_headers moh,
651                        wsh_pick_grouping_rules spg
652                   where spg.pick_grouping_rule_id = moh.grouping_rule_id
653                     AND spg.pick_method <>WMS_GLOBALS.PICK_METHOD_ORDER  -- not order pciking
654                     AND moh.move_order_type = INV_GLOBALS.g_move_order_pick_wave   -- pick wave move order only
655                     AND moh.request_number between nvl(p_input_for_bulk.start_mo_request_number,moh.request_number) and
656                            nvl(p_input_for_bulk.end_mo_request_number,moh.request_number)
657                     AND moh.creation_date between nvl(p_input_for_bulk.start_release_date,moh.creation_date) and
658                             nvl(p_input_for_bulk.end_release_date,moh.creation_date);
659            end if;
660 
661            if (p_input_for_bulk.trip_id is null) then
662               if (p_input_for_bulk.only_sub_item = 1) then
663                   if (l_fast_possible) then
664                       if (g_trace_on = 1) then log_event('OPEN c11_bulk_only_sub_item_fast'); end if;
665                       OPEN c11_bulk_only_sub_item_fast(l_organization_id);
666                   else
667                       if (g_trace_on = 1) then log_event('OPEN c11_bulk_only_sub_item'); end if;
668 
669                       OPEN c11_bulk_only_sub_item(l_organization_id);
670                   end if;
671               else
672                   if (l_fast_possible) then
673                       if (g_trace_on = 1) then log_event('OPEN c11_bulk_all_fast'); end if;
674                       OPEN c11_bulk_all_fast(l_organization_id);
675                   else
676                       if (g_trace_on = 1) then log_event('OPEN c11_bulk_all'); end if;
677                       OPEN c11_bulk_all(l_organization_id);
678                   end if;
679               end if;
680            else
681                if (p_input_for_bulk.only_sub_item = 1) then
682                     if (g_trace_on = 1) then log_event('OPEN c11_bulk_trip_sub_item'); end if;
683                     OPEN c11_bulk_trip_sub_item(l_organization_id);
684                else
685                     if (g_trace_on = 1) then log_event('OPEN c11_bulk_trip'); end if;
686                    OPEN c11_bulk_trip(l_organization_id);
687               end if;
688            end if;
689         END IF;
690        -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING -- END'); end if;
691      ELSIF(p_transaction_header_id IS NOT NULL) THEN
692 
693        if (g_trace_on = 1) then log_event(' txn hdr id '||p_transaction_header_id); END IF;
694 
695        OPEN c2(p_transaction_header_id);
696 
697     END IF;
698 
699     LOOP
700        mmtt_rec := NULL;
701 
702        IF(p_move_order_header_id IS NOT NULL) THEN
703           IF (p_move_order_header_id <> -1) THEN    -- patchset J bulk picking  -----
704              IF c1%isopen THEN
705                  FETCH c1 INTO mmtt_rec;
706                  EXIT WHEN c1%notfound;
707               END IF;
708           ELSE
709             -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING -- START'); end if;
710              IF c11_bulk_all%isopen THEN
711                  FETCH c11_bulk_all INTO mmtt_rec;
712                  EXIT WHEN c11_bulk_all%notfound;
713              ELSIF c11_bulk_only_sub_item%isopen THEN
714                  FETCH c11_bulk_only_sub_item INTO mmtt_rec;
715                  EXIT WHEN c11_bulk_only_sub_item%notfound;
716              ELSIF c11_bulk_trip%isopen THEN
717                  FETCH c11_bulk_trip INTO mmtt_rec;
718                  EXIT WHEN c11_bulk_trip%notfound;
719              ELSIF c11_bulk_trip_sub_item%isopen THEN
720                  FETCH c11_bulk_trip_sub_item INTO mmtt_rec;
721                  EXIT WHEN c11_bulk_trip_sub_item%notfound;
722              ELSIF c11_bulk_all_fast%isopen THEN
723                  FETCH c11_bulk_all_fast INTO mmtt_rec;
724                  EXIT WHEN c11_bulk_all_fast%notfound;
725              ELSIF c11_bulk_only_sub_item_fast%isopen THEN
726                  FETCH c11_bulk_only_sub_item_fast INTO mmtt_rec;
727                  EXIT WHEN c11_bulk_only_sub_item_fast%notfound;
728              END IF;
729             -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING -- END'); end if;
730           END IF;
731 
732         ELSIF(p_transaction_header_id IS NOT NULL) THEN
733 
734              IF c2%isopen THEN
735                 FETCH c2 INTO mmtt_rec;
736                 EXIT WHEN c2%notfound;
737              END IF;
738        END IF;
739 
740        ---   patchset J bulk picking   -----
741        if mmtt_rec.parent_line_id is not null then  -- child line
742            if (g_trace_on = 1) then -- log_event('PATCHSET J-- BULK PICKING -- START');
743                                     log_event('checking the lot and serial control code'); end if;
744            select lot_control_code,serial_number_control_code
745            into l_lot_control_code,l_serial_control_code
746            from mtl_system_items_b msi, mtl_material_transactions_temp mmtt
747            where mmtt.transaction_temp_id = mmtt_rec.transaction_Temp_id
748              and mmtt.organization_id = msi.organization_id
749              and msi.inventory_item_id = mmtt.inventory_item_id;
750            if (g_trace_on = 1) then log_event('transaction_temp_id:'|| mmtt_rec.transaction_Temp_id ||
751                                               ' lot control code :'||l_lot_control_code ||
752                                               ' serial control code :'||l_serial_control_code);
753            end if;
754            if (g_trace_on = 1) then log_event('nullify the parent_line_id for the child task '); end if;
755 
756            update mtl_material_transactions_temp
757            set parent_line_id = null
758            where transaction_temp_id = mmtt_rec.transaction_Temp_id;
759 
760            -- call update_parent_mmtt to change the qty or delete the parent line --------
761            if (g_trace_on = 1) then log_event('calling update_parent_mmtt ....'); end if;
762            inv_trx_util_pub.update_parent_mmtt(x_return_status => l_return_status
763                                                , p_parent_line_id =>mmtt_rec.parent_line_id
764                                                , p_child_line_id => mmtt_rec.transaction_Temp_id
765                                                , p_lot_control_code => l_lot_control_code
766                                                , p_serial_control_code => l_serial_control_code);
767            if (g_trace_on = 1) then log_event('returning from update_parent_mmtt:'||l_return_status); end if;
768            if (l_return_status <> fnd_api.g_ret_sts_success) then
769                RAISE fnd_api.g_exc_unexpected_error;
770            end if;
771           -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING --END'); end if;
772        end if;
773 
774        ---- end of patchset J bulk picking  -------------------
775        if (g_trace_on = 1) then log_event('Insert transaction temp id:'||mmtt_rec.transaction_temp_id); end if;
776        if (g_trace_on = 1) then log_event('transaction quantity:'||mmtt_rec.transaction_quantity); end if;
777        if (g_trace_on = 1) then log_event('secondary transaction quantity:'||mmtt_rec.secondary_transaction_quantity); end if; --invconv kkillams
778        INSERT INTO wms_cartonization_temp
779          (transaction_header_id,
780           transaction_temp_id,
781           source_code,
782           source_line_id,
783           transaction_mode,
784           lock_flag,
785           last_update_date,
786           last_updated_by,
787           creation_date,
788           created_by,
789           last_update_login,
790           request_id,
791           program_application_id,
792           program_id,
793           program_update_date,
794           inventory_item_id,
795           revision,
796           organization_id,
797           subinventory_code,
798           locator_id,
799           transaction_quantity,
800           primary_quantity,
801           transaction_uom,
802           transaction_cost,
803           transaction_type_id,
804           transaction_action_id,
805           transaction_source_type_id,
806           transaction_source_id,
807           transaction_source_name,
808           transaction_date,
809           acct_period_id,
810           distribution_account_id,
811           transaction_reference,
812           requisition_line_id,
813           requisition_distribution_id,
814           reason_id,
815           lot_number,
816           lot_expiration_date,
817           serial_number,
818           receiving_document,
819           demand_id,
820           rcv_transaction_id,
821           move_transaction_id,
822           completion_transaction_id,
823           wip_entity_type,
824           schedule_id,
825           repetitive_line_id,
826          employee_code,
827          primary_switch,
828          schedule_update_code,
829          setup_teardown_code,
830          item_ordering,
831          negative_req_flag,
832          operation_seq_num,
833          picking_line_id,
834          trx_source_line_id,
835          trx_source_delivery_id,
836          physical_adjustment_id,
837          cycle_count_id,
838          rma_line_id,
839          customer_ship_id,
840          currency_code,
841          currency_conversion_rate,
842          currency_conversion_type,
843          currency_conversion_date,
844          ussgl_transaction_code,
845          vendor_lot_number,
846          encumbrance_account,
847          encumbrance_amount,
848          ship_to_location,
849          shipment_number,
850          transfer_cost,
851          transportation_cost,
852          transportation_account,
853          freight_code,
854          containers,
855          waybill_airbill,
856          expected_arrival_date,
857          transfer_subinventory,
858          transfer_organization,
859          transfer_to_location,
860          new_average_cost,
861          value_change,
862          percentage_change,
863          material_allocation_temp_id,
864          demand_source_header_id,
865          demand_source_line,
866          demand_source_delivery,
867          item_segments,
868          item_description,
869          item_trx_enabled_flag,
870          item_location_control_code,
871          item_restrict_subinv_code,
872          item_restrict_locators_code,
873          item_revision_qty_control_code,
874          item_primary_uom_code,
875          item_uom_class,
876          item_shelf_life_code,
877          item_shelf_life_days,
878          item_lot_control_code,
879          item_serial_control_code,
880          item_inventory_asset_flag,
881          allowed_units_lookup_code,
882          department_id,
883          department_code,
884          wip_supply_type,
885          supply_subinventory,
886          supply_locator_id,
887          valid_subinventory_flag,
888          valid_locator_flag,
889          locator_segments,
890          current_locator_control_code,
891          number_of_lots_entered,
892          wip_commit_flag,
893          next_lot_number,
894          lot_alpha_prefix,
895          next_serial_number,
896          serial_alpha_prefix,
897          shippable_flag,
898          posting_flag,
899          required_flag,
900          process_flag,
901          error_code,
902          error_explanation,
903          attribute_category,
904          attribute1,
905          attribute2,
906          attribute3,
907          attribute4,
908          attribute5,
909          attribute6,
910          attribute7,
911          attribute8,
912          attribute9,
913          attribute10,
914          attribute11,
915          attribute12,
916          attribute13,
917          attribute14,
918          attribute15,
919          movement_id,
920          reservation_quantity,
921          shipped_quantity,
922          transaction_line_number,
923          task_id,
924          to_task_id,
925          source_task_id,
926          project_id,
927          source_project_id,
928          pa_expenditure_org_id,
929          to_project_id,
930          expenditure_type,
931          final_completion_flag,
932          transfer_percentage,
933          transaction_sequence_id,
934          material_account,
935          material_overhead_account,
936          resource_account,
937          outside_processing_account,
938          overhead_account,
939          flow_schedule,
940          cost_group_id,
941          demand_class,
942          qa_collection_id,
943          kanban_card_id,
944          overcompletion_txn_qty,
945          overcompletion_primary_qty,
946          overcompletion_transaction_id,
947          end_item_unit_number,
948          scheduled_payback_date,
949          line_type_code,
950          parent_transaction_temp_id,
951          put_away_strategy_id,
952          put_away_rule_id,
953          pick_strategy_id,
954          pick_rule_id,
955          move_order_line_id,
956          task_group_id,
957          pick_slip_number,
958          reservation_id,
959          common_bom_seq_id,
960          common_routing_seq_id,
961          org_cost_group_id,
962          cost_type_id,
963          transaction_status,
964          standard_operation_id,
965          task_priority,
966          wms_task_type,
967          parent_line_id,
968          --source_lot_number,
969          transfer_cost_group_id,
970          lpn_id,
971          transfer_lpn_id,
972          wms_task_status,
973          content_lpn_id,
974          container_item_id,
975          cartonization_id,
976          pick_slip_date,
977          rebuild_item_id,
978          rebuild_serial_number,
979          rebuild_activity_id,
980          rebuild_job_name,
981          organization_type,
982          transfer_organization_type,
983          owning_organization_id,
984          owning_tp_type,
985          xfr_owning_organization_id,
986          transfer_owning_tp_type,
987          planning_organization_id,
988          planning_tp_type,
989          xfr_planning_organization_id,
990          transfer_planning_tp_type,
991          secondary_uom_code,
992          secondary_transaction_quantity,
993          transaction_batch_id,
994          transaction_batch_seq,
995          allocated_lpn_id,
996          schedule_number,
997          scheduled_flag,
998          class_code,
999          schedule_group,
1000          build_sequence,
1001          bom_revision,
1002          routing_revision,
1003          bom_revision_date,
1004          routing_revision_date,
1005          alternate_bom_designator,
1006          alternate_routing_designator,
1007          operation_plan_id,
1008          move_order_header_id,
1009          serial_allocated_flag,
1010          fulfillment_base)
1011          values
1012          (mmtt_rec.transaction_header_id,
1013           mmtt_rec.transaction_temp_id,
1014           mmtt_rec.source_code,
1015           mmtt_rec.source_line_id,
1016           mmtt_rec.transaction_mode,
1017           mmtt_rec.lock_flag,
1018           mmtt_rec.last_update_date,
1019           mmtt_rec.last_updated_by,
1020           mmtt_rec.creation_date,
1021           mmtt_rec.created_by,
1022           mmtt_rec.last_update_login,
1023           mmtt_rec.request_id,
1024           mmtt_rec.program_application_id,
1025           mmtt_rec.program_id,
1026           mmtt_rec.program_update_date,
1027           mmtt_rec.inventory_item_id,
1028           mmtt_rec.revision,
1029           mmtt_rec.organization_id,
1030           mmtt_rec.subinventory_code,
1031           mmtt_rec.locator_id,
1032           mmtt_rec.transaction_quantity,
1033           mmtt_rec.primary_quantity,
1034           mmtt_rec.transaction_uom,
1035           mmtt_rec.transaction_cost,
1036           mmtt_rec.transaction_type_id,
1037           mmtt_rec.transaction_action_id,
1038           mmtt_rec.transaction_source_type_id,
1039           mmtt_rec.transaction_source_id,
1040           mmtt_rec.transaction_source_name,
1041           mmtt_rec.transaction_date,
1042           mmtt_rec.acct_period_id,
1043           mmtt_rec.distribution_account_id,
1044           mmtt_rec.transaction_reference,
1045          mmtt_rec.requisition_line_id,
1046          mmtt_rec.requisition_distribution_id,
1047          mmtt_rec.reason_id,
1048          mmtt_rec.lot_number,
1049          mmtt_rec.lot_expiration_date,
1050          mmtt_rec.serial_number,
1051          mmtt_rec.receiving_document,
1052          mmtt_rec.demand_id,
1053          mmtt_rec.rcv_transaction_id,
1054          mmtt_rec.move_transaction_id,
1055          mmtt_rec.completion_transaction_id,
1056          mmtt_rec.wip_entity_type,
1057          mmtt_rec.schedule_id,
1058          mmtt_rec.repetitive_line_id,
1059          mmtt_rec.employee_code,
1060          mmtt_rec.primary_switch,
1061          mmtt_rec.schedule_update_code,
1062          mmtt_rec.setup_teardown_code,
1063          mmtt_rec.item_ordering,
1064          mmtt_rec.negative_req_flag,
1065          mmtt_rec.operation_seq_num,
1066          mmtt_rec.picking_line_id,
1067          mmtt_rec.trx_source_line_id,
1068          mmtt_rec.trx_source_delivery_id,
1069          mmtt_rec.physical_adjustment_id,
1070          mmtt_rec.cycle_count_id,
1071          mmtt_rec.rma_line_id,
1072          mmtt_rec.customer_ship_id,
1073          mmtt_rec.currency_code,
1074          mmtt_rec.currency_conversion_rate,
1075          mmtt_rec.currency_conversion_type,
1076          mmtt_rec.currency_conversion_date,
1077          mmtt_rec.ussgl_transaction_code,
1078          mmtt_rec.vendor_lot_number,
1079          mmtt_rec.encumbrance_account,
1080          mmtt_rec.encumbrance_amount,
1081          mmtt_rec.ship_to_location,
1082          mmtt_rec.shipment_number,
1083          mmtt_rec.transfer_cost,
1084          mmtt_rec.transportation_cost,
1085          mmtt_rec.transportation_account,
1086          mmtt_rec.freight_code,
1087          mmtt_rec.containers,
1088          mmtt_rec.waybill_airbill,
1089          mmtt_rec.expected_arrival_date,
1090          mmtt_rec.transfer_subinventory,
1091          mmtt_rec.transfer_organization,
1092          mmtt_rec.transfer_to_location,
1093          mmtt_rec.new_average_cost,
1094          mmtt_rec.value_change,
1095          mmtt_rec.percentage_change,
1096          mmtt_rec.material_allocation_temp_id,
1097          mmtt_rec.demand_source_header_id,
1098          mmtt_rec.demand_source_line,
1099          mmtt_rec.demand_source_delivery,
1100          mmtt_rec.item_segments,
1101          mmtt_rec.item_description,
1102          mmtt_rec.item_trx_enabled_flag,
1103          mmtt_rec.item_location_control_code,
1104          mmtt_rec.item_restrict_subinv_code,
1105          mmtt_rec.item_restrict_locators_code,
1106          mmtt_rec.item_revision_qty_control_code,
1107          mmtt_rec.item_primary_uom_code,
1108          mmtt_rec.item_uom_class,
1109          mmtt_rec.item_shelf_life_code,
1110          mmtt_rec.item_shelf_life_days,
1111          mmtt_rec.item_lot_control_code,
1112          mmtt_rec.item_serial_control_code,
1113          mmtt_rec.item_inventory_asset_flag,
1114          mmtt_rec.allowed_units_lookup_code,
1115          mmtt_rec.department_id,
1116          mmtt_rec.department_code,
1117          mmtt_rec.wip_supply_type,
1118          mmtt_rec.supply_subinventory,
1119          mmtt_rec.supply_locator_id,
1120          mmtt_rec.valid_subinventory_flag,
1121          mmtt_rec.valid_locator_flag,
1122          mmtt_rec.locator_segments,
1123          mmtt_rec.current_locator_control_code,
1124          mmtt_rec.number_of_lots_entered,
1125          mmtt_rec.wip_commit_flag,
1126          mmtt_rec.next_lot_number,
1127          mmtt_rec.lot_alpha_prefix,
1128          mmtt_rec.next_serial_number,
1129          mmtt_rec.serial_alpha_prefix,
1130          mmtt_rec.shippable_flag,
1131          mmtt_rec.posting_flag,
1132          mmtt_rec.required_flag,
1133          mmtt_rec.process_flag,
1134          mmtt_rec.error_code,
1135          mmtt_rec.error_explanation,
1136          mmtt_rec.attribute_category,
1137          mmtt_rec.attribute1,
1138          mmtt_rec.attribute2,
1139          mmtt_rec.attribute3,
1140          mmtt_rec.attribute4,
1141          mmtt_rec.attribute5,
1142          mmtt_rec.attribute6,
1143          mmtt_rec.attribute7,
1144          mmtt_rec.attribute8,
1145          mmtt_rec.attribute9,
1146          mmtt_rec.attribute10,
1147          mmtt_rec.attribute11,
1148          mmtt_rec.attribute12,
1149          mmtt_rec.attribute13,
1150          mmtt_rec.attribute14,
1151          mmtt_rec.attribute15,
1152          mmtt_rec.movement_id,
1153          mmtt_rec.reservation_quantity,
1154          mmtt_rec.shipped_quantity,
1155          mmtt_rec.transaction_line_number,
1156          mmtt_rec.task_id,
1157          mmtt_rec.to_task_id,
1158          mmtt_rec.source_task_id,
1159          mmtt_rec.project_id,
1160          mmtt_rec.source_project_id,
1161          mmtt_rec.pa_expenditure_org_id,
1162          mmtt_rec.to_project_id,
1163          mmtt_rec.expenditure_type,
1164          mmtt_rec.final_completion_flag,
1165          mmtt_rec.transfer_percentage,
1166          mmtt_rec.transaction_sequence_id,
1167          mmtt_rec.material_account,
1168          mmtt_rec.material_overhead_account,
1169          mmtt_rec.resource_account,
1170          mmtt_rec.outside_processing_account,
1171          mmtt_rec.overhead_account,
1172          mmtt_rec.flow_schedule,
1173          mmtt_rec.cost_group_id,
1174          mmtt_rec.demand_class,
1175          mmtt_rec.qa_collection_id,
1176          mmtt_rec.kanban_card_id,
1177          mmtt_rec.overcompletion_transaction_qty,
1178          mmtt_rec.overcompletion_primary_qty,
1179          mmtt_rec.overcompletion_transaction_id,
1180          mmtt_rec.end_item_unit_number,
1181          mmtt_rec.scheduled_payback_date,
1182          mmtt_rec.line_type_code,
1183          mmtt_rec.parent_transaction_temp_id,
1184          mmtt_rec.put_away_strategy_id,
1185          mmtt_rec.put_away_rule_id,
1186          mmtt_rec.pick_strategy_id,
1187          mmtt_rec.pick_rule_id,
1188          mmtt_rec.move_order_line_id,
1189          mmtt_rec.task_group_id,
1190          mmtt_rec.pick_slip_number,
1191          mmtt_rec.reservation_id,
1192          mmtt_rec.common_bom_seq_id,
1193          mmtt_rec.common_routing_seq_id,
1194          mmtt_rec.org_cost_group_id,
1195          mmtt_rec.cost_type_id,
1196          mmtt_rec.transaction_status,
1197          mmtt_rec.standard_operation_id,
1198          mmtt_rec.task_priority,
1199          mmtt_rec.wms_task_type,
1200          mmtt_rec.parent_line_id,
1201          --mmtt_rec.source_lot_number,
1202          mmtt_rec.transfer_cost_group_id,
1203          mmtt_rec.lpn_id,
1204          mmtt_rec.transfer_lpn_id,
1205          mmtt_rec.wms_task_status,
1206          mmtt_rec.content_lpn_id,
1207          mmtt_rec.container_item_id,
1208          mmtt_rec.cartonization_id,
1209          mmtt_rec.pick_slip_date,
1210          mmtt_rec.rebuild_item_id,
1211          mmtt_rec.rebuild_serial_number,
1212          mmtt_rec.rebuild_activity_id,
1213          mmtt_rec.rebuild_job_name,
1214          mmtt_rec.organization_type,
1215          mmtt_rec.transfer_organization_type,
1216          mmtt_rec.owning_organization_id,
1217          mmtt_rec.owning_tp_type,
1218          mmtt_rec.xfr_owning_organization_id,
1219          mmtt_rec.transfer_owning_tp_type,
1220          mmtt_rec.planning_organization_id,
1221          mmtt_rec.planning_tp_type,
1222          mmtt_rec.xfr_planning_organization_id,
1223          mmtt_rec.transfer_planning_tp_type,
1224          mmtt_rec.secondary_uom_code,
1225          mmtt_rec.secondary_transaction_quantity,
1226          mmtt_rec.transaction_batch_id,
1227          mmtt_rec.transaction_batch_seq,
1228          mmtt_rec.allocated_lpn_id,
1229          mmtt_rec.schedule_number,
1230          mmtt_rec.scheduled_flag,
1231          mmtt_rec.class_code,
1232          mmtt_rec.schedule_group,
1233          mmtt_rec.build_sequence,
1234          mmtt_rec.bom_revision,
1235          mmtt_rec.routing_revision,
1236          mmtt_rec.bom_revision_date,
1237          mmtt_rec.routing_revision_date,
1238          mmtt_rec.alternate_bom_designator,
1239          mmtt_rec.alternate_routing_designator,
1240          mmtt_rec.operation_plan_id,
1241          mmtt_rec.move_order_header_id,
1242          mmtt_rec.serial_allocated_flag,
1243          mmtt_rec.fulfillment_base);
1244 
1245     END LOOP;
1246 
1247     IF c1%isopen THEN
1248        CLOSE c1;
1249      ELSIF c2%isopen THEN
1250        CLOSE c2;
1251     END IF;
1252 
1253     IF c11_bulk_all%isopen THEN
1254         CLOSE c11_bulk_all;
1255     ELSIF c11_bulk_only_sub_item%isopen THEN
1256         CLOSE c11_bulk_only_sub_item;
1257     ELSIF c11_bulk_trip%isopen THEN
1258         CLOSE c11_bulk_trip;
1259     ELSIF c11_bulk_trip_sub_item%isopen THEN
1260         CLOSE c11_bulk_trip_sub_item;
1261     ELSIF c11_bulk_all_fast%isopen THEN
1262         CLOSE c11_bulk_all_fast;
1263     ELSIF c11_bulk_only_sub_item_fast%isopen THEN
1264         CLOSE c11_bulk_only_sub_item_fast;
1265     END IF;
1266  EXCEPTION
1267     WHEN OTHERS THEN
1268        if (g_trace_on = 1) then log_event(' Error occurred in create_wct proc'||Sqlerrm); END IF;
1269        RAISE fnd_api.g_exc_unexpected_error;
1270  END create_wct;
1271 
1272 
1273 
1274 
1275 
1276 
1277 
1278  PROCEDURE get_cached_lpn_attributes(p_lpn_id                  IN NUMBER,
1279                                      x_inventory_item_id       OUT NOCOPY NUMBER,
1280                                      x_gross_weight            OUT NOCOPY NUMBER,
1281                                      x_content_volume          OUT NOCOPY NUMBER,
1282                                      x_gross_weight_uom_code   OUT NOCOPY VARCHAR2,
1283                                      x_content_volume_uom_code OUT NOCOPY VARCHAR2,
1284                                      x_tare_weight             OUT NOCOPY NUMBER,
1285                                      x_tare_weight_uom_code    OUT NOCOPY VARCHAR2,
1286                                      x_found_flag              OUT NOCOPY VARCHAR2)
1287    IS
1288  BEGIN
1289 
1290     error_code := 'GET_CLPN_ATTR';
1291 
1292 
1293     x_inventory_item_id       := lpn_attr_table(p_lpn_id).inventory_item_id;
1294     x_gross_weight            := lpn_attr_table(p_lpn_id).gross_weight;
1295     x_gross_weight_uom_code   := lpn_attr_table(p_lpn_id).gross_weight_uom_code;
1296     x_content_volume          := lpn_attr_table(p_lpn_id).content_volume;
1297     x_content_volume_uom_code := lpn_attr_table(p_lpn_id).content_volume_uom_code;
1298     x_tare_weight             := lpn_attr_table(p_lpn_id).tare_weight;
1299     x_tare_weight_uom_code    := lpn_attr_table(p_lpn_id).tare_weight_uom_code;
1300     x_found_flag              := 'Y';
1301  EXCEPTION
1302     WHEN OTHERS THEN
1303        x_inventory_item_id       := NULL;
1304        x_gross_weight            := NULL;
1305        x_gross_weight_uom_code   := NULL;
1306        x_content_volume          := NULL;
1307        x_content_volume_uom_code := NULL;
1308        x_tare_weight             := NULL;
1309        x_tare_weight_uom_code    := NULL;
1310        x_found_flag              := 'N';
1311  END;
1312 
1313  PROCEDURE get_cached_package_attributes(p_org_id                  IN NUMBER,
1314                                          p_package_id              IN NUMBER,
1315                                          x_inventory_item_id       OUT NOCOPY NUMBER,
1316                                          x_gross_weight            OUT NOCOPY NUMBER,
1317                                          x_content_volume          OUT NOCOPY NUMBER,
1318                                          x_gross_weight_uom_code   OUT NOCOPY VARCHAR2,
1319                                          x_content_volume_uom_code OUT NOCOPY VARCHAR2,
1320                                          x_tare_weight             OUT NOCOPY NUMBER,
1321                                          x_tare_weight_uom_code    OUT NOCOPY VARCHAR2,
1322                                          x_found_flag              OUT NOCOPY VARCHAR2)
1323 
1324    IS
1325  BEGIN
1326     error_code := 'GET_CPKG_ATTR';
1327     x_inventory_item_id       := pkg_attr_table(p_package_id).inventory_item_id;
1328     x_gross_weight            := pkg_attr_table(p_package_id).gross_weight;
1329     x_gross_weight_uom_code   := pkg_attr_table(p_package_id).gross_weight_uom_code;
1330     x_content_volume          := pkg_attr_table(p_package_id).content_volume;
1331     x_content_volume_uom_code := pkg_attr_table(p_package_id).content_volume_uom_code;
1332     x_tare_weight             := pkg_attr_table(p_package_id).tare_weight;
1333     x_tare_weight_uom_code    := pkg_attr_table(p_package_id).tare_weight_uom_code;
1334     x_found_flag              := 'Y';
1335  EXCEPTION
1336     WHEN OTHERS THEN
1337        x_inventory_item_id       := NULL;
1338        x_gross_weight            := NULL;
1339        x_gross_weight_uom_code   := NULL;
1340        x_content_volume          := NULL;
1341        x_content_volume_uom_code := NULL;
1342        x_tare_weight             := NULL;
1343        x_tare_weight_uom_code    := NULL;
1344        x_found_flag              := 'N';
1345  END;
1346 
1347 
1348 
1349  -- Given a transaction_temp_id, this returns whether the allocated lpn is
1350  -- is fully  allocated,  partially allocated or no LPN allocated.
1351 
1352   FUNCTION get_lpn_alloc_flag(p_temp_id IN NUMBER)
1353     return  VARCHAR2   IS
1354     l_lpn_alloc_flag VARCHAR2(1):= null;
1355   BEGIN
1356      error_code := 'GET_LPN_ALLOC';
1357 
1358        l_lpn_alloc_flag := t_lpn_alloc_flag_table(p_temp_id).lpn_alloc_flag;
1359        return l_lpn_alloc_flag;
1360   EXCEPTION
1361       WHEN  OTHERS  THEN
1362             RETURN 'N';
1363   END  get_lpn_alloc_flag;
1364 
1365 
1366   -- This procedures returns different attributes for an LPN
1367 
1368   PROCEDURE get_lpn_attributes(p_lpn_id                  IN NUMBER,
1369                                x_inventory_item_id       OUT NOCOPY NUMBER,
1370                                x_gross_weight            OUT NOCOPY NUMBER,
1371                                x_content_volume          OUT NOCOPY NUMBER,
1372                                x_gross_weight_uom_code   OUT NOCOPY VARCHAR2,
1373                                x_content_volume_uom_code OUT NOCOPY VARCHAR2,
1374                                x_tare_weight             OUT NOCOPY NUMBER,
1375                                x_tare_weight_uom_code    OUT NOCOPY VARCHAR2)
1376     IS
1377        l_inventory_item_id       NUMBER:= NULL;
1378        l_gross_weight            NUMBER:= NULL;
1379        l_content_volume          NUMBER:= NULL;
1380        l_gross_weight_uom_code   VARCHAR2(3):= NULL;
1381        l_content_volume_uom_code VARCHAR2(3):= NULL;
1382        l_tare_weight             NUMBER      := NULL;
1383        l_tare_weight_uom_code    VARCHAR2(3)    := NULL;
1384        l_found_flag VARCHAR2(1)              := NULL;
1385 
1386   BEGIN
1387      error_code := 'GET_LPN_ATTR';
1388 
1389      get_cached_lpn_attributes (p_lpn_id                 => p_lpn_id,
1390                                 x_inventory_item_id       => l_inventory_item_id,
1391                                 x_gross_weight            => l_gross_weight,
1392                                 x_content_volume          => l_content_volume,
1393                                 x_gross_weight_uom_code   => l_gross_weight_uom_code,
1394                                 x_content_volume_uom_code => l_content_volume_uom_code,
1395                                 x_tare_weight             => l_tare_weight,
1396                                 x_tare_weight_uom_code    => l_tare_weight_uom_code,
1397                                 x_found_flag              => l_found_flag );
1398 
1399        IF( l_found_flag = 'Y') THEN
1400           if (g_trace_on = 1) then log_event(' using cached values '); END IF;
1401           x_inventory_item_id  := l_inventory_item_id;
1402           x_gross_weight       := l_gross_weight;
1403           x_content_volume     := l_content_volume;
1404           x_gross_weight_uom_code   := l_gross_weight_uom_code;
1405           x_content_volume_uom_code := l_content_volume_uom_code;
1406           x_tare_weight             :=  l_tare_weight;
1407           x_tare_weight_uom_code    := l_tare_weight_uom_code;
1408         ELSE
1409 
1410           SELECT
1411             inventory_item_id,
1412             Decode(gross_weight_uom_code,NULL,0,nvl(gross_weight,0)),
1413             gross_weight_uom_code,
1414             Decode(content_volume_uom_code,NULL,0,nvl(content_volume,0)),
1415             content_volume_uom_code,
1416             Decode(tare_weight_uom_code,NULL,0,nvl(tare_weight,0)),
1417             tare_weight_uom_code
1418             INTO
1419             x_inventory_item_id,
1420             x_gross_weight,
1421             x_gross_weight_uom_code,
1422             x_content_volume,
1423             x_content_volume_uom_code,
1424             x_tare_weight,
1425             x_tare_weight_uom_code
1426             FROM
1427             wms_license_plate_numbers
1428             WHERE
1429             lpn_id = p_lpn_id;
1430 
1431           lpn_attr_table(p_lpn_id).inventory_item_id  := x_inventory_item_id;
1432           lpn_attr_table(p_lpn_id).gross_weight       := x_gross_weight;
1433           lpn_attr_table(p_lpn_id).content_volume     := x_content_volume;
1434           lpn_attr_table(p_lpn_id).gross_weight_uom_code   := x_gross_weight_uom_code;
1435           lpn_attr_table(p_lpn_id).content_volume_uom_code := x_content_volume_uom_code;
1436           lpn_attr_table(p_lpn_id).tare_weight             := x_tare_weight;
1437           lpn_attr_table(p_lpn_id).tare_weight_uom_code    := x_tare_weight_uom_code;
1438 
1439        END IF;
1440 
1441 
1442        if (g_trace_on = 1) then
1443           log_event(' inventory_item_id:'||x_inventory_item_id);
1444           log_event(' gross_weight:'||x_gross_weight);
1445           log_event(' gross_weight_uom_code:'||x_gross_weight_uom_code);
1446           log_event(' content_volume:'||x_content_volume);
1447           log_event(' content_volume_uom_code:'||x_content_volume_uom_code);
1448           log_event(' tare:'||x_tare_weight);
1449           log_event(' tare_uom_code:'||x_tare_weight_uom_code);
1450 
1451         END IF;
1452 
1453   EXCEPTION
1454      WHEN OTHERS THEN
1455         if (g_trace_on = 1) then log_event(' LPN attributes not found'); END IF;
1456         x_inventory_item_id := NULL;
1457         x_gross_weight := NULL;
1458         x_gross_weight_uom_code := NULL;
1459         x_content_volume := NULL;
1460         x_content_volume_uom_code := NULL;
1461         x_tare_weight := NULL;
1462         x_tare_weight_uom_code := NULL;
1463 
1464 
1465   END get_lpn_attributes;
1466 
1467   -- This procedures returns different attributes for a package
1468   PROCEDURE get_package_attributes(p_org_id                  IN NUMBER,
1469                                    p_package_id              IN NUMBER,
1470                                    x_inventory_item_id       OUT NOCOPY NUMBER,
1471                                    x_gross_weight            OUT NOCOPY NUMBER,
1472                                    x_content_volume          OUT NOCOPY NUMBER,
1473                                    x_gross_weight_uom_code   OUT NOCOPY VARCHAR2,
1474                                    x_content_volume_uom_code OUT NOCOPY VARCHAR2,
1475                                    x_tare_weight             OUT NOCOPY NUMBER,
1476                                    x_tare_weight_uom_code    OUT NOCOPY VARCHAR2)
1477     IS
1478 
1479        l_c_inventory_item_id       NUMBER:= NULL;
1480        l_c_gross_weight            NUMBER:= NULL;
1481        l_c_content_volume          NUMBER:= NULL;
1482        l_c_gross_weight_uom_code   VARCHAR2(3):= NULL;
1483        l_c_content_volume_uom_code VARCHAR2(3):= NULL;
1484        l_c_tare_weight             NUMBER:= NULL;
1485        l_c_tare_weight_uom_code   VARCHAR2(3):= NULL;
1486 
1487        l_found_flag                VARCHAR2(1):= NULL;
1488 
1489        l_gross_weight            NUMBER;
1490        l_content_volume          NUMBER;
1491        l_final_wt                NUMBER := 0;
1492        l_final_twt               NUMBER := 0;
1493        l_final_vol               NUMBER := 0;
1494        l_gross_weight_uom_code   VARCHAR2(3);
1495        l_content_volume_uom_code VARCHAR2(3);
1496        l_tare_weight             NUMBER:= NULL;
1497        l_tare_weight_uom_code    VARCHAR2(3):= NULL;
1498 
1499        l_std_wt_uom              VARCHAR2(3);
1500        l_std_vol_uom             VARCHAR2(3);
1501        l_ut_wt                   NUMBER := NULL;
1502        l_ut_vol                  NUMBER := NULL;
1503        l_item_id                 NUMBER := NULL;
1504        l_wt                      NUMBER := NULL;
1505        l_vol                     NUMBER := NULL;
1506        l_twt                     NUMBER := NULL;
1507        l_msg                     VARCHAR2(255);
1508 
1509        CURSOR wts_n_vols IS
1510           SELECT
1511             parent_item_id,
1512             Decode(gross_weight_uom_code,NULL,0,nvl(gross_weight,0)),
1513             gross_weight_uom_code,
1514             Decode(content_volume_uom_code,NULL,0,nvl(content_volume,0)),
1515             content_volume_uom_code,
1516             Decode(tare_weight_uom_code,NULL,0,nvl(tare_weight,0)),
1517             tare_weight_uom_code
1518             FROM
1519             WMS_PACKAGING_HIST
1520             WHERE
1521             PARENT_PACKAGE_id = p_package_id;
1522   BEGIN
1523      if (g_trace_on = 1) then log_event(' Entered get label attributes for package:'||p_package_id); END IF;
1524 
1525      error_code := 'GET_PKG_ATTR';
1526 
1527      get_cached_package_attributes (p_org_id              => p_org_id,
1528                                     p_package_id              => p_package_id,
1529                                     x_inventory_item_id       => l_c_inventory_item_id,
1530                                     x_gross_weight            => l_c_gross_weight,
1531                                     x_content_volume          => l_c_content_volume,
1532                                     x_gross_weight_uom_code   => l_c_gross_weight_uom_code,
1533                                     x_content_volume_uom_code => l_c_content_volume_uom_code,
1534                                     x_tare_weight             => l_c_tare_weight,
1535                                     x_tare_weight_uom_code    => l_c_tare_weight_uom_code,
1536                                     x_found_flag              => l_found_flag );
1537 
1538        IF( l_found_flag = 'Y') THEN
1539 
1540           if (g_trace_on = 1) then log_event(' using cached values '); END IF;
1541           x_inventory_item_id       := l_c_inventory_item_id;
1542           x_gross_weight            := l_c_gross_weight;
1543           x_content_volume          := l_c_content_volume;
1544           x_gross_weight_uom_code   := l_c_gross_weight_uom_code;
1545           x_content_volume_uom_code := l_c_content_volume_uom_code;
1546           x_tare_weight             := l_c_tare_weight;
1547           x_tare_weight_uom_code    := l_c_tare_weight_uom_code;
1548 
1549         ELSE
1550           x_inventory_item_id := NULL;
1551           x_gross_weight := NULL;
1552           x_gross_weight_uom_code := NULL;
1553           x_content_volume := NULL;
1554           x_content_volume_uom_code := NULL;
1555           x_tare_weight             := NULL;
1556           x_tare_weight_uom_code    := NULL;
1557 
1558 
1559           SELECT
1560             parent_item_id
1561             INTO
1562             x_inventory_item_id
1563             FROM
1564             WMS_PACKAGING_HIST
1565             WHERE
1566             PARENT_PACKAGE_id = p_package_id
1567             AND rownum < 2;
1568 
1569           if (g_trace_on = 1) then log_event(' container item_id:'||x_inventory_item_id); END IF;
1570 
1571           SELECT
1572             Decode(weight_uom_code,NULL,0,nvl(unit_weight,0)),
1573             Decode(volume_uom_code,NULL,0,Nvl(unit_volume,0)),
1574             weight_uom_code,
1575             volume_uom_code
1576             INTO
1577             l_ut_wt,
1578             l_ut_vol,
1579             l_std_wt_uom,
1580             l_std_vol_uom
1581             FROM
1582             mtl_system_items
1583             WHERE
1584             organization_id = p_org_id AND
1585             inventory_item_id = x_inventory_item_id;
1586 
1587           if (g_trace_on = 1) then
1588              log_event(' Container Item attributes are');
1589              log_event(' unit_wt'||l_ut_wt);
1590              log_event(' Unit_vol'||l_ut_vol);
1591              log_event(' std wt uom'||l_std_wt_uom);
1592              log_event(' std vol uom '||l_std_vol_uom);
1593           END IF;
1594           OPEN wts_n_vols;
1595 
1596           LOOP
1597 
1598 
1599 
1600              if (g_trace_on = 1) then log_event(' Fetching from wts_n_vols'); END IF;
1601              FETCH wts_n_vols INTO
1602                l_item_id,
1603                l_gross_weight,
1604                l_gross_weight_uom_code,
1605                l_content_volume,
1606                l_content_volume_uom_code,
1607                l_tare_weight,
1608                l_tare_weight_uom_code;
1609 
1610              EXIT WHEN wts_n_vols%notfound;
1611 
1612              if (g_trace_on = 1) then
1613                 log_event(' container it id'||l_item_id);
1614                 log_event(' gross_weight '||l_gross_weight);
1615                 log_event(' content_volume '||l_content_volume);
1616                 log_event(' gross_weight_uom_code '||l_gross_weight_uom_code);
1617                 log_event(' content_volume_uom_code '||l_content_volume_uom_code);
1618                 log_event(' tare_weight '||l_tare_weight);
1619                 log_event(' tare_weight_uom_code '||l_tare_weight_uom_code);
1620              END IF;
1621                 -- Logic
1622              -- We try to convert everything to the container items wt/vol uom first
1623              -- If that is null we have to pick up first non null wt/vol uom as standard
1624              -- for each record logic should be
1625              -- if the weight uom is not null on the record
1626              --     if the std wt uom is not set
1627              --         set this uom as the standard uom
1628              --     else
1629              --        if std wt uom is different from the current uom
1630              --           convert wt to the std uom
1631              --           if conversion returns null, -1 set the wt to zero
1632              --        else
1633              --           no conversion needed
1634              --        end if
1635              --      end if
1636              --  else
1637              --     there should be no contribution of weight from this record.
1638              --     set l_gross_weight to zero
1639              --  end if;
1640              --  Same logic applies for volume
1641 
1642              IF (l_gross_weight_uom_code IS NOT NULL) THEN
1643 
1644                 IF l_std_wt_uom IS NULL THEN
1645 
1646                    l_std_wt_uom := l_gross_weight_uom_code;
1647 
1648                  ELSIF (l_gross_weight_uom_code <> l_std_wt_uom ) THEN
1649 
1650                    l_wt := inv_convert.inv_um_convert
1651                      ( l_item_id,
1652                        5,
1653                        l_gross_weight,
1654                        l_gross_weight_uom_code,
1655                        l_std_wt_uom,
1656                        NULL,
1657                        null
1658                        );
1659 
1660                    IF( l_wt < 0 ) THEN
1661                       if (g_trace_on = 1) then log_event('Error converting UOM from '||l_gross_weight_uom_code||' to '||l_std_wt_uom||' qty '||l_gross_weight); END IF;
1662                       l_gross_weight := 0;
1663                     ELSE
1664                       l_gross_weight := l_wt;
1665                    END IF;
1666 
1667 
1668                    if (g_trace_on = 1) then log_event(' gross_weight - after conv '||l_gross_weight); END IF;
1669 
1670                  ELSE
1671 
1672                    NULL;
1673 
1674                 END IF;
1675 
1676               ELSE
1677 
1678                 l_gross_weight := 0;
1679 
1680              END IF;
1681 
1682              IF (l_tare_weight_uom_code IS NOT NULL) THEN
1683 
1684                 IF l_std_wt_uom IS NULL THEN
1685 
1686                    l_std_wt_uom := l_tare_weight_uom_code;
1687 
1688                  ELSIF (l_tare_weight_uom_code <> l_std_wt_uom ) THEN
1689 
1690                    l_twt := inv_convert.inv_um_convert
1691                      ( l_item_id,
1692                        5,
1693                        l_tare_weight,
1694                        l_tare_weight_uom_code,
1695                        l_std_wt_uom,
1696                        NULL,
1697                        null
1698                        );
1699 
1700                    IF( l_twt < 0 ) THEN
1701                       if (g_trace_on = 1) then log_event('Error converting UOM from '||l_tare_weight_uom_code||' to '||l_std_wt_uom||' qty '||l_tare_weight); END IF;
1702                       l_tare_weight := 0;
1703                     ELSE
1704                       l_tare_weight := l_twt;
1705                       l_tare_weight_uom_code  := l_std_wt_uom;
1706                    END IF;
1707 
1708                    if (g_trace_on = 1) then log_event(' tare_weight - after conv '||l_tare_weight); END IF;
1709 
1710                  ELSE
1711 
1712                    NULL;
1713 
1714                 END IF;
1715 
1716               ELSE
1717 
1718                 l_tare_weight := 0;
1719 
1720              END IF;
1721 
1722 
1723              l_final_wt := l_final_wt + l_gross_weight;
1724              l_final_twt := l_final_twt + l_tare_weight;
1725 
1726              if (g_trace_on = 1) then log_event('new l_final_wt '||l_final_wt); END IF;
1727 
1728 
1729              IF (l_content_volume_uom_code IS NOT NULL) THEN
1730 
1731                 IF l_std_vol_uom IS NULL THEN
1732 
1733                    l_std_vol_uom := l_content_volume_uom_code;
1734 
1735                  ELSIF (l_content_volume_uom_code <> l_std_vol_uom ) THEN
1736 
1737                    l_vol := inv_convert.inv_um_convert
1738                      ( l_item_id,
1739                        5,
1740                        l_content_volume,
1741                        l_content_volume_uom_code,
1742                        l_std_vol_uom,
1743                        NULL,
1744                        null
1745                        );
1746 
1747                    IF( l_vol < 0 ) THEN
1748                       if (g_trace_on = 1) then log_event('Error converting UOM from '||l_content_volume_uom_code||' to '||l_std_vol_uom||' qty '||l_content_volume); END IF;
1749                       l_content_volume := 0;
1750 
1751                     ELSE
1752 
1753                       l_content_volume := l_vol;
1754 
1755                    END IF;
1756 
1757                    if (g_trace_on = 1) then log_event(' content_volume - after conv '||l_content_volume); END IF;
1758                  ELSE
1759 
1760                    NULL;
1761 
1762                 END IF;
1763 
1764               ELSE
1765 
1766                 l_content_volume := 0;
1767 
1768              END IF;
1769 
1770              l_final_vol := l_final_vol + l_content_volume;
1771              if (g_trace_on = 1) then log_event(' new l_final_vol '||l_final_vol); END IF;
1772 
1773           END LOOP;
1774 
1775           IF wts_n_vols%isopen then
1776              CLOSE wts_n_vols;
1777           END IF;
1778 
1779 
1780           l_final_wt := l_final_wt + l_ut_wt;
1781           l_final_twt := l_final_twt + l_ut_wt;
1782 
1783           l_final_vol := greatest(l_final_vol, l_ut_vol);
1784 
1785           if (g_trace_on = 1) then
1786              log_event(' l_final_wt - at end '||l_final_wt);
1787              log_event(' l_final_vol - at end '||l_final_vol);
1788           END IF;
1789 
1790           x_gross_weight := l_final_wt;
1791           x_content_volume := l_final_vol;
1792           x_gross_weight_uom_code := l_std_wt_uom;
1793           x_content_volume_uom_code := l_std_vol_uom;
1794           x_tare_weight    := l_final_twt;
1795           x_tare_weight_uom_code := l_std_wt_uom;
1796 
1797           pkg_attr_table(p_package_id).inventory_item_id       := x_inventory_item_id;
1798           pkg_attr_table(p_package_id).gross_weight            := x_gross_weight;
1799           pkg_attr_table(p_package_id).content_volume          := x_content_volume;
1800           pkg_attr_table(p_package_id).gross_weight_uom_code   := x_gross_weight_uom_code;
1801           pkg_attr_table(p_package_id).content_volume_uom_code := x_content_volume_uom_code;
1802           pkg_attr_table(p_package_id).tare_weight             := x_tare_weight;
1803           pkg_attr_table(p_package_id).tare_weight_uom_code    := x_tare_weight_uom_code;
1804 
1805        END IF;
1806 
1807   EXCEPTION
1808      WHEN OTHERS THEN
1809         if (g_trace_on = 1) then log_event('Error Occurred in get Attributes'); END IF;
1810         l_msg := Sqlerrm;
1811         if (g_trace_on = 1) then log_event(l_msg); END IF;
1812 
1813   END get_package_attributes;
1814 
1815 
1816   -- Returns the conatiner item associated with an LPN
1817 
1818   FUNCTION get_lpn_Itemid(P_lpn_id IN NUMBER)
1819     return NUMBER
1820     IS
1821        l_ret NUMBER := NULL;
1822   BEGIN
1823      error_code := 'GET_LPN_ITM';
1824       IF( cache_lpn_id = p_lpn_id) THEN
1825 
1826         RETURN cache_lpn_item_id;
1827        ELSE
1828 
1829          SELECT inventory_item_id
1830            INTO l_ret
1831            FROM wms_license_plate_numbers
1832            WHERE
1833            lpn_id = p_lpn_id;
1834 
1835 
1836          cache_lpn_id := p_lpn_id;
1837 
1838          cache_lpn_item_id := l_ret;
1839       END IF;
1840 
1841      RETURN l_ret;
1842 
1843   EXCEPTION
1844      WHEN OTHERS THEN
1845         RETURN NULL;
1846   END get_lpn_Itemid;
1847 
1848 
1849   -- Returns the conatiner item associated with a package
1850 
1851   FUNCTION get_PACKAGE_Itemid(P_PACKAGE_id IN NUMBER)
1852     return NUMBER
1853     IS
1854        l_ret NUMBER := NULL;
1855   BEGIN
1856      error_code := 'GET_PKG_ATTR';
1857      IF( cache_package_id = p_package_id) THEN
1858 
1859         RETURN cache_pkg_item_id;
1860       ELSE
1861         SELECT PARENT_ITEM_ID
1862           INTO l_ret
1863           FROM WMS_PACKAGING_HIST
1864           WHERE
1865           PARENT_PACKAGE_id = p_PACKAGE_id
1866           AND rownum < 2;
1867 
1868 
1869         cache_package_id := p_package_id;
1870 
1871         cache_pkg_item_id := l_ret;
1872      END IF;
1873 
1874 
1875      RETURN l_ret;
1876 
1877   EXCEPTION
1878      WHEN OTHERS THEN
1879         RETURN NULL;
1880   END get_package_Itemid;
1881 
1882 
1883  -- This procedure inserts rows into packaging history table
1884 
1885  PROCEDURE insert_ph
1886    (p_orig_header_id            IN NUMBER,
1887     p_transaction_temp_id       IN NUMBER)
1888    IS
1889       l_header_id   NUMBER;
1890       l_item_id     NUMBER;
1891       l_org_id      NUMBER;
1892       l_prim_qty    NUMBER;
1893       l_rev         VARCHAR2(3);
1894       l_clpn_id     VARCHAR2(30);
1895       l_cartonization_id VARCHAR2(30);
1896       l_citem_id    NUMBER;
1897 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
1898       l_lot_number  VARCHAR2(80);
1899       l_serial_number VARCHAR2(30);
1900       counter       NUMBER := 0;
1901       v_lot_control_code NUMBER := NULL;
1902       v_serial_control_code NUMBER := NULL;
1903       l_stemp_id NUMBER := NULL;
1904       l_qty NUMBER := NULL;
1905       l_weight NUMBER := NULL;
1906       l_volume NUMBER := NULL;
1907       l_gross_weight NUMBER := NULL;
1908       l_gross_weight_uom_code VARCHAR2(3) := NULL;
1909       l_content_volume NUMBER := NULL;
1910       l_content_volume_uom_code VARCHAR2(3) := NULL;
1911       l_cont_item_id NUMBER := NULL;
1912       l_tare_weight  NUMBER := NULL;
1913       l_tare_weight_uom_code VARCHAR2(3) := NULL;
1914 
1915       l_tot_lot_prim_qty    NUMBER :=0;    -- Added for 14699845 (Flexible lot allocation)
1916 
1917 
1918       CURSOR lots is
1919          SELECT lot_number, serial_transaction_temp_id, primary_quantity
1920            FROM mtl_transaction_lots_temp
1921            WHERE
1922            transaction_temp_id = p_transaction_temp_id;
1923 
1924      CURSOR serials(p_temp_id NUMBER, p_org_id NUMBER, p_item_id NUMBER) is
1925         SELECT msn.serial_number
1926           FROM
1927           mtl_serial_numbers_temp msnt,
1928           mtl_serial_numbers msn
1929           WHERE
1930           msnt.transaction_temp_id = p_temp_id AND
1931           msn.current_organization_id = p_org_id AND
1932           msn.inventory_item_id = p_item_id AND
1933           msn.serial_number >= msnt.fm_serial_number AND
1934           msn.serial_number <= msnt.to_serial_number;
1935 
1936 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
1937      TYPE lot_tab_type IS TABLE OF VARCHAR2(80)
1938        INDEX BY binary_integer;
1939      TYPE serial_tab_type IS TABLE OF VARCHAR2(30)
1940        INDEX BY binary_integer;
1941      TYPE qty_tab_type IS TABLE OF NUMBER
1942        INDEX BY binary_integer;
1943 
1944      lot_table    lot_tab_type;
1945      serial_table serial_tab_type;
1946      qty_table    qty_tab_type;
1947      l_temp_id    NUMBER;
1948      l_msg        VARCHAR2(100);
1949  BEGIN
1950 
1951     error_code := 'INSERT_PH';
1952 
1953     if (g_trace_on = 1) then
1954 	  log_event('entered insert_ph');
1955 	  log_event(' p_orig_header_id := '||p_orig_header_id);
1956 	  log_event(' p_transaction_temp_id := '||p_transaction_temp_id);
1957 	END IF;
1958 
1959     SELECT
1960       transaction_header_id,
1961       organization_id,
1962       inventory_item_id,
1963       revision,
1964       primary_quantity,
1965       content_lpn_id,
1966       cartonization_id,
1967       container_item_id
1968       INTO
1969       l_header_id,
1970       l_org_id,
1971       l_item_id,
1972       l_rev,
1973       l_prim_qty,
1974       l_clpn_id,
1975       l_cartonization_id,
1976       l_citem_id
1977       FROM
1978       wms_cartonization_temp
1979       WHERE
1980       transaction_temp_id =  p_transaction_temp_id;
1981 
1982 	  if (g_trace_on = 1) then
1983 	    log_event(' l_header_id := '||l_header_id);
1984 	    log_event(' l_org_id := '||l_org_id);
1985 	    log_event(' l_item_id := '||l_item_id);
1986 	    log_event(' l_rev := '||l_rev);
1987 	    log_event(' l_prim_qty := '||l_prim_qty);
1988 	    log_event(' l_clpn_id := '||l_clpn_id);
1989 	    log_event(' l_cartonization_id := '||l_cartonization_id);
1990 	    log_event(' l_citem_id := '||l_citem_id);
1991 	  END IF;
1992 
1993    IF( p_transaction_temp_id < 0 ) THEN
1994       l_temp_id := NULL;
1995     ELSE
1996       l_temp_id :=  p_transaction_temp_id;
1997    END IF;
1998 
1999    l_tot_lot_prim_qty := 0;
2000 
2001 
2002 
2003    IF l_header_id > 0 THEN
2004 
2005       if (g_trace_on = 1) then log_event('header_id > 0'); END IF;
2006 
2007       IF l_clpn_id IS NULL THEN
2008 
2009          if (g_trace_on = 1) then log_event('content_lpn_id is null'); END IF;
2010 
2011          select
2012            lot_control_code,
2013            serial_number_control_code,
2014            Decode(weight_uom_code,NULL,0,nvl(unit_weight,0)),
2015            Decode(volume_uom_code,NULL,0,Nvl(unit_volume,0)),
2016            weight_uom_code,
2017            volume_uom_code
2018            INTO
2019            v_lot_control_code,
2020            v_serial_control_code,
2021            l_weight,
2022            l_volume,
2023            l_gross_weight_uom_code,
2024            l_content_volume_uom_code
2025            from mtl_system_items msi
2026            where
2027            inventory_item_id = l_item_id AND
2028            organization_id = l_org_id;
2029 
2030          if (g_trace_on = 1) then
2031             log_event('lot_control_code '||v_lot_control_code );
2032             log_event('serial_control_code '||v_serial_control_code);
2033          END IF;
2034 
2035          --Bug 3528061 fix
2036          IF( (outbound = 'Y') AND (g_allocate_serial_flag = 'N')) THEN
2037             if (g_trace_on = 1) then
2038                log_event('outbound and allocate_serial_flag is off');
2039            END IF;
2040            v_serial_control_code := 6;
2041          END IF;
2042          --Bug 3528061 fix
2043 
2044          IF (v_lot_control_code = 2 AND v_serial_control_code IN (1,6))  THEN
2045 
2046             if (g_trace_on = 1) then log_event(' Lot controlled only '); END IF;
2047             OPEN lots;
2048             LOOP
2049                FETCH lots INTO l_lot_number, l_stemp_id, l_qty;
2050 
2051                EXIT WHEN lots%notfound;
2052 
2053 			   if (g_trace_on = 1) then
2054 	             log_event(' l_lot_number := '||l_lot_number);
2055 	             log_event(' l_stemp_id := '||l_stemp_id);
2056 	             log_event(' l_qty := '||l_qty);
2057 	             log_event(' l_tot_lot_prim_qty := '||l_tot_lot_prim_qty);
2058 	           END IF;
2059 
2060                counter := counter + 1;
2061                lot_table(counter) := l_lot_number;
2062                serial_table(counter) := NULL;
2063                qty_table(counter) := l_qty;
2064                l_tot_lot_prim_qty := l_tot_lot_prim_qty + l_qty;  -- Added for 14699845 (Flexible lot allocation)
2065 
2066 
2067             END LOOP;
2068             IF( lots%isopen) then
2069                CLOSE lots;
2070             END IF;
2071 
2072 			if (g_trace_on = 1) then
2073 	          log_event(' l_prim_qty := '||l_prim_qty);
2074 	          log_event(' l_tot_lot_prim_qty := '||l_tot_lot_prim_qty);
2075 	          log_event(' p_transaction_temp_id := '||p_transaction_temp_id);
2076 	        END IF;
2077 
2078             -- Added for 14699845 (Flexible lot allocation)
2079             --IF (counter = 0 AND g_allocate_lot_flag = 'N') THEN
2080             IF (l_prim_qty > l_tot_lot_prim_qty AND p_transaction_temp_id > 0) THEN
2081                counter := counter + 1;
2082                lot_table(counter) := NULL;
2083                serial_table(counter) := NULL;
2084                qty_table(counter) := (l_prim_qty - l_tot_lot_prim_qty);
2085             END IF;
2086 
2087 
2088           ELSIF (v_lot_control_code = 1 AND v_serial_control_code NOT IN (1,6)) THEN
2089 
2090                   if (g_trace_on = 1) then log_event(' Serial controlled only '); END IF;
2091 
2092                   OPEN serials(p_transaction_temp_id,l_org_id, l_item_id);
2093                   LOOP
2094                      FETCH serials INTO l_serial_number;
2095 
2096 					 if (g_trace_on = 1) then log_event(' l_serial_number := '||l_serial_number); END IF;
2097 
2098                      EXIT WHEN serials%notfound;
2099                      counter := counter + 1;
2100                      lot_table(counter) := NULL;
2101                      serial_table(counter) := l_serial_number;
2102                      qty_table(counter) := 1;
2103 
2104 
2105                   END LOOP;
2106                   IF(serials%isopen) then
2107                      CLOSE serials;
2108                   END IF;
2109 
2110 
2111 
2112           ELSIF (v_lot_control_code = 2 AND v_serial_control_code NOT IN (1,6))  THEN
2113 
2114                         if (g_trace_on = 1) then log_event(' Both lot and Serial controlled  '); END IF;
2115 
2116                         OPEN lots;
2117                         LOOP
2118                            --FETCH lots.serial_transaction_temp_id, lots.lot_number INTO l_stemp_id,l_lot_number;
2119 
2120                            FETCH lots INTO l_lot_number, l_stemp_id, l_qty;
2121 
2122                            EXIT WHEN lots%notfound;
2123 
2124 			               if (g_trace_on = 1) then
2125 			                 log_event(' l_lot_number := '||l_lot_number);
2126 			                 log_event(' l_stemp_id := '||l_stemp_id);
2127 			                 log_event(' l_qty := '||l_qty);
2128 			                 log_event(' l_tot_lot_prim_qty := '||l_tot_lot_prim_qty);
2129 			               END IF;
2130 
2131                            OPEN serials(l_stemp_id,l_org_id, l_item_id);
2132                            LOOP
2133                               FETCH serials INTO l_serial_number;
2134 
2135                               EXIT WHEN serials%notfound;
2136 
2137 							  if (g_trace_on = 1) then log_event(' l_serial_number := '||l_serial_number); END IF;
2138 
2139                               counter := counter + 1;
2140                               lot_table(counter) := l_lot_number;
2141                               serial_table(counter) := l_serial_number;
2142                               qty_table(counter) := 1;
2143                               l_tot_lot_prim_qty := l_tot_lot_prim_qty + 1;  -- Added for 14699845 (Flexible lot allocation)
2144 
2145 
2146                            END LOOP;
2147                            IF(serials%isopen) then
2148                               CLOSE serials;
2149                             END IF;
2150                         END LOOP;
2151 
2152                         IF lots%isopen then
2153                            CLOSE lots;
2154                         END IF;
2155 
2156 			            if (g_trace_on = 1) then
2157 			              log_event(' l_prim_qty := '||l_prim_qty);
2158 			              log_event(' l_tot_lot_prim_qty := '||l_tot_lot_prim_qty);
2159 			              log_event(' p_transaction_temp_id := '||p_transaction_temp_id);
2160 			            END IF;
2161 
2162                         -- Added for 14699845 (Flexible lot allocation)
2163                         --IF (counter = 0 AND g_allocate_lot_flag = 'N') THEN
2164                         IF (l_prim_qty > l_tot_lot_prim_qty AND p_transaction_temp_id > 0) THEN
2165                           counter := counter + 1;
2166                           lot_table(counter) := NULL;
2167                           serial_table(counter) := NULL;
2168                           qty_table(counter) := l_tot_lot_prim_qty - l_prim_qty;
2169                         END IF;
2170 
2171           ELSE
2172 
2173                                  if (g_trace_on = 1) then log_event(' No Control Item'); END IF;
2174                                  counter := counter + 1;
2175 
2176                                  lot_table(counter) := NULL;
2177                                  serial_table(counter) := NULL;
2178                                  qty_table(counter) := l_prim_qty;
2179 
2180 
2181 
2182          END IF;
2183 
2184 
2185          LOOP
2186             EXIT WHEN counter <= 0;
2187 
2188             l_gross_weight := l_weight * qty_table(counter);
2189             l_content_volume := l_volume * qty_table(counter);
2190 
2191             if (g_trace_on = 1) then
2192                log_event('gross weight '||l_gross_weight);
2193                log_event('content_volume '||l_content_volume);
2194             END IF;
2195 
2196             INSERT INTO WMS_PACKAGING_HIST
2197               (
2198                organization_id,
2199                header_id,
2200                sequence_id,
2201                pack_level,
2202                inventory_item_id,
2203                creation_date,
2204                created_by,
2205                last_update_date,
2206                last_updated_by,
2207                last_update_login,
2208                revision,
2209                lot_number,
2210                fm_serial_number,
2211                to_serial_number,
2212                primary_quantity,
2213                PACKAGE_id,
2214                parent_package_id,
2215                lpn_id,
2216                parent_lpn_id,
2217                parent_item_id,
2218                gross_weight,
2219                content_volume,
2220                gross_weight_uom_code,
2221                content_volume_uom_code,
2222                packaging_mode,
2223                reference_id
2224                )
2225               VALUES
2226               (l_org_id,
2227                p_orig_header_id,
2228                wms_cartnzn_pub.g_wms_pack_hist_seq,
2229                pack_level,
2230                l_item_id,
2231                Sysdate,
2232                fnd_global.user_id,
2233                Sysdate,
2234                fnd_global.user_id,
2235                fnd_global.login_id,
2236                l_rev,
2237                lot_table(counter),
2238                serial_table(counter),
2239                serial_table(counter),
2240                qty_table(counter),
2241                null,
2242                l_cartonization_id,
2243                null,
2244                null,
2245                l_citem_id,
2246                l_gross_weight,
2247                l_content_volume,
2248                l_gross_weight_uom_code,
2249                l_content_volume_uom_code,
2250                packaging_mode,
2251                l_temp_id
2252                );
2253 
2254             wms_cartnzn_pub.g_wms_pack_hist_seq := wms_cartnzn_pub.g_wms_pack_hist_seq + 1;
2255 
2256             counter := counter -1;
2257          END LOOP;
2258 
2259 
2260        ELSE
2261                --L_CLPN_ID IS NOT NULL
2262 
2263                get_lpn_attributes(p_lpn_id => l_clpn_id,
2264                                   x_inventory_item_id => l_cont_item_id,
2265                                   x_gross_weight      => l_gross_weight,
2266                                   x_gross_weight_uom_code => l_gross_weight_uom_code,
2267                                   x_content_volume    => l_content_volume,
2268                                   x_content_volume_uom_code => l_content_volume_uom_code,
2269                                   x_tare_weight            => l_tare_weight,
2270                                   x_tare_weight_uom_code   => l_tare_weight_uom_code
2271                                   );
2272 
2273 
2274                INSERT INTO WMS_PACKAGING_HIST
2275                  (organization_id,
2276                   header_id,
2277                   sequence_id,
2278                   pack_level,
2279                   inventory_item_id,
2280                   creation_date,
2281                   created_by,
2282                   last_update_date,
2283                   last_updated_by,
2284                   last_update_login,
2285                   revision,
2286                   lot_number,
2287                   fm_serial_number,
2288                   to_serial_number,
2289                   primary_quantity,
2290                   PACKAGE_id,
2291                   parent_package_id,
2292                   lpn_id,
2293                   parent_lpn_id,
2294                   parent_item_id,
2295                   gross_weight,
2296                   content_volume,
2297                   gross_weight_uom_code,
2298                   content_volume_uom_code,
2299                   packaging_mode,
2300                   reference_id
2301                   )
2302                  VALUES
2303                  (l_org_id,
2304                   p_orig_header_id,
2305                   wms_cartnzn_pub.g_wms_pack_hist_seq,
2306                   pack_level,
2307                   NULL,
2308                   Sysdate,
2309                   fnd_global.user_id,
2310                   Sysdate,
2311                   fnd_global.user_id,
2312                   fnd_global.login_id,
2313                   null,
2314                   null,
2315                   null,
2316                   NULL,
2317                   1,
2318                   null,
2319                   l_cartonization_id,
2320                   L_CLPN_ID,
2321                   null,
2322                   l_citem_id,
2323                   l_gross_weight,
2324                   l_content_volume,
2325                   l_gross_weight_uom_code,
2326                   l_content_volume_uom_code,
2327                   packaging_mode,
2328                   l_temp_id
2329                   );
2330                wms_cartnzn_pub.g_wms_pack_hist_seq :=
2331                  wms_cartnzn_pub.g_wms_pack_hist_seq + 1;
2332       END IF;
2333 
2334     ELSE
2335                               --HEADER ID < 0
2336                               get_package_attributes(p_org_id   => l_org_id,
2337                                                      p_package_id => l_clpn_id,
2338                                                      x_inventory_item_id => l_cont_item_id,
2339                                                      x_gross_weight      => l_gross_weight,
2340                                                      x_gross_weight_uom_code => l_gross_weight_uom_code,
2341                                                      x_content_volume    => l_content_volume,
2342                                                      x_content_volume_uom_code => l_content_volume_uom_code,
2343                                                      x_tare_weight            => l_tare_weight,
2344                                                      x_tare_weight_uom_code   => l_tare_weight_uom_code
2345                                                      );
2346 
2347 
2348 
2349                               INSERT INTO WMS_PACKAGING_HIST
2350                                 (organization_id,
2351                                  header_id,
2352                                  sequence_id,
2353                                  pack_level,
2354                                  inventory_item_id,
2355                                  creation_date,
2356                                  created_by,
2357                                  last_update_date,
2358                                  last_updated_by,
2359                                  last_update_login,
2360                                  revision,
2361                                  lot_number,
2362                                  fm_serial_number,
2363                                  to_serial_number,
2364                                  primary_quantity,
2365                                  PACKAGE_id,
2366                                  parent_package_id,
2367                                  lpn_id,
2368                                  parent_lpn_id,
2369                                  parent_item_id,
2370                                  gross_weight,
2371                                  content_volume,
2372                                  gross_weight_uom_code,
2373                                  content_volume_uom_code,
2374                                  packaging_mode,
2375                                  reference_id,
2376                                  tare_weight,
2377                                  tare_weight_uom_code
2378                                  )
2379                                 VALUES
2380                                 (
2381                                  l_org_id,
2382                                  p_orig_header_id,
2383                                  wms_cartnzn_pub.g_wms_pack_hist_seq,
2384                                  pack_level,
2385                                  l_cont_item_id,
2386                                  Sysdate,
2387                                  fnd_global.user_id,
2388                                  Sysdate,
2389                                  fnd_global.user_id,
2390                                  fnd_global.login_id,
2391                                  null,
2392                                  null,
2393                                  null,
2394                                  NULL,
2395                                  1,
2396                                  l_clpn_id,
2397                                  l_cartonization_id,
2398                                  null,
2399                                  null,
2400                                  l_citem_id,
2401                                  l_gross_weight,
2402                                  l_content_volume,
2403                                  l_gross_weight_uom_code,
2404                                  l_content_volume_uom_code,
2405                                  packaging_mode,
2406                                  l_temp_id,
2407                                  l_tare_weight,
2408                                  l_tare_weight_uom_code
2409                                  );
2410 
2411                               wms_cartnzn_pub.g_wms_pack_hist_seq := wms_cartnzn_pub.g_wms_pack_hist_seq + 1;
2412    END IF;
2413 
2414  EXCEPTION
2415     WHEN OTHERS THEN
2416        l_msg := Sqlerrm;
2417        if (g_trace_on = 1) then log_event('Exception occurred in insert_ph '||l_msg); END IF;
2418  END insert_ph;
2419 
2420 
2421  -- This returns the new transaction temp id to be used to insert
2422  -- a row in WCT for packing next level
2423 
2424  FUNCTION get_next_temp_id    RETURN NUMBER
2425    IS
2426  BEGIN
2427     dummy_temp_id := dummy_temp_id -1;
2428 
2429     RETURN dummy_temp_id;
2430 
2431  END get_next_temp_id;
2432 
2433  -- This returns the new transaction header id to be used to insert
2434  -- a row in WCT for packing next level
2435 
2436  FUNCTION get_next_header_id
2437    RETURN NUMBER
2438    IS
2439  BEGIN
2440     dummy_header_id := dummy_header_id -1;
2441 
2442     RETURN dummy_header_id;
2443 
2444  END get_next_header_id;
2445 
2446 
2447  -- This returns the next package to be used
2448 
2449  FUNCTION get_next_package_id
2450    RETURN NUMBER
2451    IS
2452       l_package_sequence NUMBER;
2453  BEGIN
2454 
2455    if (g_trace_on = 1) then log_event(' In get next package id'); END IF;
2456    SELECT wms_packaging_hist_s.nextVal into
2457      l_package_sequence FROM dual;
2458 
2459    RETURN l_package_sequence;
2460 
2461  END get_next_package_id;
2462 
2463 
2464 
2465  PROCEDURE update_gross_weight(p_header_id      IN NUMBER,
2466                                p_organization_id IN NUMBER)
2467    IS
2468       CURSOR plpns IS
2469          SELECT
2470            wph.parent_lpn_id,
2471            Decode(wph.tare_weight_uom_code,NULL,0,Nvl(wph.tare_weight,0)) tare_weight,
2472            wph.tare_weight_uom_code
2473            FROM
2474            wms_packaging_hist wph
2475            WHERE
2476            header_id = p_header_id AND
2477            organization_id = p_organization_id AND
2478            parent_lpn_id IS NOT NULL
2479            ORDER BY parent_lpn_id;
2480 
2481 
2482 
2483            prev_lpn             NUMBER := -1;
2484            fin_wt               NUMBER := 0;
2485            tr_wt                NUMBER := 0;
2486            l_lpn_cur_gr_wt      NUMBER := NULL;
2487            l_lpn_wt_from_db     NUMBER := 0;
2488            gr_wt_uom_code       VARCHAR2(3) := NULL;
2489            l_wt_uom_from_db     VARCHAR2(3) := NULL;
2490 
2491            l_api_return_status  VARCHAR2(1);
2492            l_msg_count          NUMBER;
2493            l_msg_data           VARCHAR2(2000);
2494 
2495            l_lpn_rec            WMS_CONTAINER_PUB.lpn;
2496 
2497  BEGIN
2498 
2499     error_code := 'UPD_GR_WT';
2500 
2501 
2502 
2503     FOR lpn_cur IN plpns LOOP
2504 
2505        if (g_trace_on = 1) then log_event('calculating gr wt for lpn '||lpn_cur.parent_lpn_id); END IF;
2506 
2507        tr_wt := 0;
2508 
2509        IF( lpn_cur.parent_lpn_id <> prev_lpn ) THEN
2510 
2511           IF( prev_lpn <> -1) THEN
2512 
2513              if (g_trace_on = 1) then
2514                 log_event('adding to gross wt '||fin_wt);
2515                 log_event(' for the lpn '|| prev_lpn);
2516              END IF;
2517 
2518              SELECT gross_weight
2519                   , gross_weight_uom_code
2520                INTO l_lpn_wt_from_db
2521                   , l_wt_uom_from_db
2522                FROM wms_license_plate_numbers
2523               WHERE lpn_id = prev_lpn;
2524 
2525              l_lpn_rec.lpn_id := prev_lpn;
2526              l_lpn_rec.gross_weight := NVL(l_lpn_wt_from_db,0) + fin_wt;
2527              l_lpn_rec.gross_weight_uom_code := NVL(l_wt_uom_from_db,gr_wt_uom_code);
2528 
2529              l_api_return_status := fnd_api.g_ret_sts_success;
2530              wms_container_pvt.modify_lpn
2531              ( p_api_version      => 1.0
2532              , p_init_msg_list    => fnd_api.g_false
2533              , p_commit           => fnd_api.g_false
2534              , p_validation_level => fnd_api.g_valid_level_full
2535              , x_return_status    => l_api_return_status
2536              , x_msg_count        => l_msg_count
2537              , x_msg_data         => l_msg_data
2538              , p_lpn              => l_lpn_rec
2539              , p_caller           => 'WMS_LPNMGMT'
2540              );
2541 
2542              IF l_api_return_status <> fnd_api.g_ret_sts_success
2543              THEN
2544                 IF g_trace_on = 1 THEN
2545                    log_event('Error from wms_container_pvt.modify_lpn: ' || l_msg_data);
2546                 END IF;
2547                 RAISE FND_API.G_EXC_ERROR;
2548              ELSE
2549                 IF g_trace_on = 1 THEN
2550                    log_event('LPN gross weight updated');
2551                 END IF;
2552              END IF;
2553           END IF;
2554 
2555 
2556           SELECT gross_weight_uom_code, gross_weight INTO
2557             gr_wt_uom_code, l_lpn_cur_gr_wt
2558             FROM wms_license_plate_numbers
2559             WHERE lpn_id = lpn_cur.parent_lpn_id;
2560 
2561           prev_lpn := lpn_cur.parent_lpn_id ;
2562 
2563           fin_wt := 0;
2564        END IF;
2565 
2566 
2567        IF ( lpn_cur.tare_weight <> 0 ) THEN
2568 
2569           IF (lpn_cur.tare_weight_uom_code IS NOT NULL) THEN
2570 
2571              IF gr_wt_uom_code IS NULL THEN
2572 
2573                 if (g_trace_on = 1) then log_event('lpn has null uom'); END IF;
2574 
2575                 gr_wt_uom_code := lpn_cur.tare_weight_uom_code;
2576 
2577                 IF l_lpn_cur_gr_wt IS NOT NULL OR l_lpn_cur_gr_wt > 0 THEN
2578                    if (g_trace_on = 1) then log_event('INCONSISTENT LPN DATA - WEIGHT without UOM'); END IF;
2579                 END IF;
2580 
2581                 tr_wt := lpn_cur.tare_weight;
2582 
2583               ELSIF (lpn_cur.tare_weight_uom_code <> gr_wt_uom_code) THEN
2584 
2585                 tr_wt := inv_convert.inv_um_convert
2586                   ( null,
2587                     5,
2588                     lpn_cur.tare_weight,
2589                     lpn_cur.tare_weight_uom_code,
2590                     gr_wt_uom_code,
2591                     NULL,
2592                     null
2593                     );
2594 
2595                 IF( tr_wt < 0 ) THEN
2596                    if (g_trace_on = 1) then log_event('Error converting UOM from '||lpn_cur.tare_weight_uom_code||' to '||gr_wt_uom_code||' qty '||lpn_cur.tare_weight); END IF;
2597                    tr_wt := 0;
2598                 END IF;
2599 
2600               ELSE
2601                 tr_wt := lpn_cur.tare_weight;
2602              END IF;
2603 
2604              if (g_trace_on = 1) then log_event(' tare_weight - after conv '||tr_wt); END IF;
2605 
2606              IF( tr_wt > 0) THEN
2607                 fin_wt := fin_wt + tr_wt;
2608              END IF;
2609 
2610           END IF; -- lpn_cur.tare weight uom code is not null
2611 
2612        END IF; --lpn_cur.tare_weight <> 0
2613 
2614     END LOOP;
2615 
2616     IF( (fin_wt) > 0 AND (prev_lpn IS NOT NULL) AND (prev_lpn > 0) ) THEN
2617 
2618        if (g_trace_on = 1) then
2619           log_event('adding to gross wt '||fin_wt);
2620           log_event(' for the lpn '|| prev_lpn);
2621        END IF;
2622 
2623        SELECT gross_weight
2624             , gross_weight_uom_code
2625          INTO l_lpn_wt_from_db
2626             , l_wt_uom_from_db
2627          FROM wms_license_plate_numbers
2628         WHERE lpn_id = prev_lpn;
2629 
2630        l_lpn_rec.lpn_id := prev_lpn;
2631        l_lpn_rec.gross_weight := NVL(l_lpn_wt_from_db,0) + fin_wt;
2632        l_lpn_rec.gross_weight_uom_code := NVL(l_wt_uom_from_db,gr_wt_uom_code);
2633 
2634        l_api_return_status := fnd_api.g_ret_sts_success;
2635        wms_container_pvt.modify_lpn
2636        ( p_api_version      => 1.0
2637        , p_init_msg_list    => fnd_api.g_false
2638        , p_commit           => fnd_api.g_false
2639        , p_validation_level => fnd_api.g_valid_level_full
2640        , x_return_status    => l_api_return_status
2641        , x_msg_count        => l_msg_count
2642        , x_msg_data         => l_msg_data
2643        , p_lpn              => l_lpn_rec
2644        , p_caller           => 'WMS_LPNMGMT'
2645        );
2646 
2647        IF l_api_return_status <> fnd_api.g_ret_sts_success
2648        THEN
2649           IF g_trace_on = 1 THEN
2650              log_event('Error from wms_container_pvt.modify_lpn: ' || l_msg_data);
2651           END IF;
2652           RAISE FND_API.G_EXC_ERROR;
2653        ELSE
2654           IF g_trace_on = 1 THEN
2655              log_event('LPN gross weight updated');
2656           END IF;
2657        END IF;
2658     END IF;
2659 
2660  EXCEPTION
2661     WHEN OTHERS THEN
2662        if (g_trace_on = 1) then log_event('error occurred in update gross weight'); END IF;
2663  END update_gross_weight;
2664 
2665 
2666  -- This function can be obsoeleted if u use a connect by at the caller
2667 
2668  PROCEDURE get_parent(p_child IN NUMBER,
2669                       x_par_cart_id OUT NOCOPY NUMBER,
2670                       x_par_cont_id OUT NOCOPY NUMBER) IS
2671  BEGIN
2672     error_code := 'GET_PAR';
2673     if (g_trace_on = 1) then log_event(' Enter get_parent for child '||p_child); END IF;
2674 
2675 	--Bug9830363(Base Bug : 9749394) added hint
2676     SELECT /*+ index(WMS_CARTONIZATION_TEMP WMS_CARTONIZATION_TEMP_N1) */
2677 	  cartonization_id, container_item_id
2678       INTO
2679       x_par_cart_id, x_par_cont_id
2680       FROM wms_cartonization_temp
2681       WHERE
2682       content_lpn_id = p_child AND
2683       transaction_header_id < 0;
2684 
2685     if (g_trace_on = 1) then
2686        log_event(' cartonization_id '||x_par_cart_id);
2687        log_event(' container_item_id'||x_par_cont_id);
2688     END IF;
2689 
2690  EXCEPTION
2691     WHEN OTHERS THEN
2692        x_par_cart_id := NULL;
2693        x_par_cont_id := NULL;
2694        if (g_trace_on = 1) then
2695           log_event(' cartonization_id '||x_par_cart_id);
2696           log_event(' container_item_id'||x_par_cont_id);
2697        END IF;
2698  END get_parent;
2699 
2700 
2701  -- This procedure is called to generate lpns for the outermost
2702  -- packages used to pack a mmtt line
2703 
2704  PROCEDURE generate_lpns( P_header_id IN NUMBER,
2705                           p_organization_id IN NUMBER)
2706    IS
2707       orig_temp_id NUMBER := NULL;
2708       cart_id NUMBER := NULL;
2709       cont_id NUMBER := NULL;
2710       par_cart_id NUMBER := NULL;
2711       par_cont_id NUMBER := NULL;
2712       l_return_status VARCHAR2(1);
2713       l_msg_data      varchar2(255);
2714       l_msg_count     NUMBER;
2715       v_lpn_id_out    NUMBER;
2716       v_process_id    NUMBER;
2717       v_lpn_out       VARCHAR2(30);
2718       par_lpn_id      NUMBER := NULL;
2719       l_inventory_item_id NUMBER := NULL;   -- Added for LSP, bug 9087971
2720 
2721 
2722       CURSOR bpack_rows IS
2723          SELECT
2724            transaction_temp_id,
2725            cartonization_id,
2726            CONTAINER_ITEM_ID
2727            FROM
2728            wms_cartonization_temp
2729            WHERE
2730            transaction_header_id = p_header_id
2731            --Bug 2864774 fix
2732            ORDER BY cartonization_id;
2733       CURSOR obound_rows IS
2734          SELECT
2735            wct.transaction_temp_id,
2736            wct.cartonization_id,
2737            wct.CONTAINER_ITEM_ID,
2738            wct.inventory_item_id      -- Added for LSP, bug 9087971
2739            FROM
2740            wms_cartonization_temp wct,
2741            mtl_txn_request_lines mtrl
2742            WHERE
2743            wct.move_order_line_id = mtrl.line_id AND
2744            mtrl.header_id = p_header_id AND
2745            wct.transaction_header_id >= 0 -- AND
2746 --           wct.container_item_id IS NOT NULL   --ER : 6682436
2747            -- Bug 3876178
2748            ORDER BY cartonization_id;
2749 
2750 
2751 
2752  BEGIN
2753 
2754     error_code := 'GEN_LPNS';
2755 
2756     if (g_trace_on = 1) then log_event('In generated_LPNs '); END IF;
2757     if (g_trace_on = 1) then log_event('OUTBOUND : '||outbound); END IF;
2758 
2759     IF outbound = 'N' THEN
2760        OPEN bpack_rows;
2761      ELSE
2762        OPEN obound_rows;
2763        if (g_trace_on = 1) then log_event('opening outbound rows.'); END IF;
2764     END IF;
2765 
2766 
2767     LOOP
2768 
2769 
2770        IF outbound = 'N' THEN
2771           FETCH bpack_rows INTO orig_temp_id, cart_id, cont_id;
2772           EXIT WHEN bpack_rows%notfound;
2773         ELSE
2774              FETCH obound_rows INTO orig_temp_id, cart_id, cont_id, l_inventory_item_id;         -- For LSP, bug 9087971
2775              if (g_trace_on = 1) then log_event('Fetch suceeded'); END IF;
2776              EXIT WHEN obound_rows%notfound;
2777        END IF;
2778 
2779        IF cart_id IS NOT NULL THEN
2780 
2781          -- can potentially use a connect by here
2782           LOOP
2783 
2784             get_parent(p_child => cart_id,
2785                        x_par_cart_id => par_cart_id,
2786                        x_par_cont_id => par_cont_id);
2787 
2788             EXIT WHEN par_cart_id IS NULL;
2789 
2790             cart_id := par_cart_id;
2791             cont_id := par_cont_id;
2792 
2793          END LOOP;
2794 
2795 
2796          par_lpn_id := NULL;
2797 
2798 
2799          BEGIN
2800             SELECT parent_lpn_id INTO par_lpn_id
2801               FROM wms_packaging_hist
2802               WHERE
2803               parent_package_id  = cart_id AND
2804               header_id = p_header_id AND
2805               ROWNUM < 2;
2806          EXCEPTION
2807             WHEN others THEN
2808                par_lpn_id := NULL;
2809          END;
2810 
2811          l_return_status := fnd_api.g_ret_sts_success;
2812 
2813          IF par_lpn_id IS NULL THEN
2814 
2815 
2816             wms_container_pub.generate_lpn(  p_api_version => 1.0,
2817                                              p_validation_level => fnd_api.g_valid_level_none,
2818                                              x_return_status => l_return_status,
2819                                              x_msg_count =>  l_msg_count,
2820                                              x_msg_data =>  l_msg_data,
2821                                              p_organization_id =>  p_organization_id,
2822                                              p_container_item_id => cont_id,
2823                                              p_lpn_id_out => v_lpn_id_out,
2824                                              p_lpn_out => v_lpn_out,
2825                                              p_process_id => v_process_id,
2826                                              p_client_code => wms_deploy.get_client_code(l_inventory_item_id)         -- Added for LSP, bug 9087971
2827                                              );
2828 
2829             if (g_trace_on = 1) then log_event('generated LPN '||v_lpn_out); END IF;
2830 
2831           ELSE
2832 
2833             if (g_trace_on = 1) then log_event('lpn already generated '||par_lpn_id); END IF;
2834             v_lpn_id_out := par_lpn_id;
2835 
2836          END IF;
2837 
2838 
2839 
2840          IF( l_return_status <> fnd_api.g_ret_sts_success ) THEN
2841 
2842             if (g_trace_on = 1) then log_event('ERROR generating the lpns '||l_return_status); END IF;
2843             RAISE fnd_api.g_exc_unexpected_error;
2844 
2845           ELSE
2846             UPDATE wms_cartonization_temp
2847               SET
2848               cartonization_id = v_lpn_id_out,
2849               container_item_id = cont_id
2850               WHERE
2851               transaction_temp_id = orig_temp_id;
2852 
2853             if (g_trace_on = 1) then log_event('updated wms_cartonization_temp for temp id '||orig_temp_id); END IF;
2854 
2855 
2856             UPDATE WMS_PACKAGING_HIST
2857               SET parent_lpn_id = v_lpn_id_out
2858               --parent_package_id = NULL
2859               WHERE
2860               parent_package_id = cart_id;
2861 
2862             if (g_trace_on = 1) then log_event('updated WMS_PACKAGING_HIST for package_id '||cart_id); END IF;
2863 
2864 
2865 
2866          END IF; -- l_return_status <> fnd_api.g_ret_sts_success
2867 
2868        END IF;--cart_id IS NOT NULL
2869 
2870     END LOOP;
2871 
2872 
2873     IF outbound = 'N' THEN
2874        IF( bpack_rows%isopen) then
2875           CLOSE bpack_rows;
2876        END IF;
2877 
2878      ELSE
2879 
2880        IF obound_rows%isopen then
2881           CLOSE obound_rows;
2882        END IF;
2883 
2884    END IF;
2885 
2886    if (g_trace_on = 1) then log_event('updating packaging history'); END IF;
2887 
2888    UPDATE wms_packaging_hist
2889      SET parent_package_id = NULL
2890      WHERE
2891      parent_lpn_id IS NOT NULL AND
2892        parent_package_id IS NOT NULL AND
2893          header_id = p_header_id;
2894 
2895        IF packaging_mode <> wms_cartnzn_pub.PR_pKG_mode  THEN
2896           --need not be called in pick release mode
2897           update_gross_weight(p_header_id,p_organization_id);
2898        END IF;
2899 
2900  END generate_lpns;
2901 
2902 
2903 
2904 
2905 
2906 
2907 
2908  PROCEDURE split_lot_serials ( p_organization_id IN NUMBER)
2909    IS
2910       l_counter NUMBER := NULL;
2911       v_allocate_serial_flag VARCHAR2(1) := NULL;
2912       v_lot_control_code NUMBER := NULL;
2913       v_serial_control_code NUMBER := NULL;
2914       curr_temp_id NUMBER := NULL;
2915       api_table_index NUMBER := 0;
2916       l_temporary_temp_id NUMBER := NULL;
2917  BEGIN
2918 
2919     error_code := 'SPLT_LOT_SER';
2920 
2921 
2922     if (g_trace_on = 1) then log_event('Entered split_lot_serial '); END IF;
2923     l_counter := 1;
2924 
2925     --Bug 3528061
2926     IF outbound = 'N' THEN
2927        v_allocate_serial_flag := 'Y';
2928      ELSE
2929        v_allocate_serial_flag := g_allocate_serial_flag;
2930     END IF;
2931 
2932     if (g_trace_on = 1) then log_event('size of the temp table is '||temp_id_table_index); END IF;
2933 
2934     LOOP
2935        EXIT WHEN l_counter > temp_id_table_index;
2936 
2937        if (g_trace_on = 1) then log_event('counter '||l_counter); END IF;
2938 
2939        IF temp_id_table(l_counter).processed <> 'Y' THEN
2940 
2941           --log_event(' processing '||l_counter);
2942 
2943           curr_temp_id := temp_id_table(l_counter).orig_temp_id;
2944 
2945           if (g_trace_on = 1) then log_event('processsing temp_id '||curr_temp_id); END IF;
2946 
2947           select msi.lot_control_code, msi.serial_number_control_code
2948             INTO v_lot_control_code, v_serial_control_code
2949             from mtl_system_items msi where
2950             (msi.inventory_item_id, msi.organization_id) =
2951             (
2952                select mmtt.inventory_item_id, mmtt.organization_id from
2953                wms_cartonization_temp mmtt WHERE mmtt.transaction_temp_id = curr_temp_id
2954                );
2955 
2956           IF( (v_allocate_serial_flag IS NULL) OR
2957               (v_allocate_serial_flag = 'N') ) THEN
2958              v_serial_control_code := 6;
2959           END IF;
2960 
2961           api_table.delete;
2962 
2963           api_table_index := 0;
2964 
2965           /* Hack for sony */
2966           api_table_index := api_table_index + 1;
2967 
2968           SELECT mtl_material_transactions_s.NEXTVAL INTO
2969             api_table(api_table_index).transaction_id
2970             FROM dual;
2971 
2972           SELECT primary_quantity ,secondary_transaction_quantity INTO
2973             api_table(api_table_index).primary_quantity,
2974             api_table(api_table_index).secondary_quantity
2975             FROM
2976             wms_cartonization_temp
2977             WHERE
2978             transaction_temp_id = curr_temp_id;
2979 
2980           l_temporary_temp_id := api_table(api_table_index).transaction_id;
2981 
2982           /* Hack for sony */
2983 
2984 
2985           if (g_trace_on = 1) then log_event(api_table_index||'*'||api_table(api_table_index).transaction_id||'*'||api_table(api_table_index).primary_quantity); END IF;
2986 
2987           FOR l_counter2 IN 1..temp_id_table.COUNT LOOP
2988 
2989              IF temp_id_table(l_counter2).orig_temp_id = curr_temp_id  THEN
2990 
2991                 api_table_index := api_table_index + 1;
2992 
2993                 api_table(api_table_index).transaction_id :=  temp_id_table(l_counter2).splt_temp_id;
2994                 api_table(api_table_index).primary_quantity :=  temp_id_table(l_counter2).primary_quantity;
2995                 api_table(api_table_index).secondary_quantity  :=  temp_id_table(l_counter2).secondary_quantity;
2996                 temp_id_table(l_counter2).processed := 'Y';
2997 
2998                 if (g_trace_on = 1) then log_event(api_table_index||'*'||api_table(api_table_index).transaction_id||'*'||api_table(api_table_index).primary_quantity);  END IF;
2999              END IF;
3000 
3001           END LOOP;
3002 
3003           IF api_table_index  > 0 THEN
3004              --Setting  the variable to  order lots  by creation date
3005              inv_rcv_common_apis.g_order_lots_by := inv_rcv_common_apis.g_order_lots_by_creation_date;
3006 
3007              inv_rcv_common_apis.break
3008                (p_original_tid        => curr_temp_id,
3009                 p_new_transactions_tb => api_table,
3010                 p_lot_control_code => v_lot_control_code,
3011                 p_serial_control_code => v_serial_control_code);
3012 
3013              --Setting  it back to  default
3014              inv_rcv_common_apis.g_order_lots_by := inv_rcv_common_apis.g_order_lots_by_exp_date;
3015           END IF;
3016 
3017           /* Hack for sony */
3018           UPDATE mtl_transaction_lots_temp
3019             SET
3020             transaction_temp_id = curr_temp_id
3021             WHERE
3022             transaction_temp_id = l_temporary_temp_id;
3023           /* Hack for sony */
3024 
3025        END IF; --end for processed <> 'Y'
3026 
3027 
3028        --log_event( l_counter||'-'||temp_id_table(l_counter).orig_temp_id
3029        --                ||'-'||temp_id_table(l_counter).splt_temp_id
3030        --                );
3031        l_counter := l_counter + 1;
3032     END LOOP;
3033 
3034     api_table.delete;
3035 
3036    api_table_index := 0;
3037 
3038    temp_id_table.DELETE;
3039 
3040    temp_id_table_index := 0;
3041 
3042  END split_lot_serials;
3043 
3044 
3045  --Bug 2478970 fix
3046 
3047  FUNCTION validate_cont_item(p_item_id IN NUMBER,
3048                              p_org_id  IN NUMBER)
3049    RETURN VARCHAR2
3050    IS
3051       l_ret VARCHAR2(1) := 'Y';
3052  BEGIN
3053 
3054     IF p_item_id IS NULL OR
3055       p_org_id IS NULL THEN
3056        RETURN 'N';
3057     END IF;
3058 
3059     SELECT  'Y' INTO l_ret
3060       FROM dual WHERE exists
3061       (SELECT inventory_item_id
3062        FROM  MTL_SYSTEM_ITEMS
3063        WHERE ORGANIZATION_ID = p_org_id
3064        AND INVENTORY_ITEM_ID = p_item_id
3065        AND mtl_transactions_enabled_flag = 'Y'
3066        AND enabled_flag = 'Y');
3067 
3068    RETURN L_RET;
3069  EXCEPTION
3070     WHEN no_data_found THEN
3071        RETURN 'N';
3072     WHEN OTHERS THEN
3073        RETURN 'N';
3074  END validate_cont_item;
3075 
3076  --**************************************
3077  -- patchset J bulk picking
3078    --
3079    -- API name    : AssignTT
3080    -- Type        : Private
3081    -- Function    : Assign task type to records in MMTT
3082    -- Input Parameters  :
3083    --
3084    -- Output Parameters:
3085    -- Version     :
3086    --   Current version 1.0
3087    --
3088    -- Notes       : calls AssignTT(p_task_id NUMBER)
3089    --
3090    -- This procedure loops through mtl_material_transactions_temp table, assign
3091    -- user defined task type to tasks that have not been assigned a task type
3092    -- for the given Move Order Header.
3093    -- This API is created when doing patchset J bulk picking
3094    -- ****************************************
3095 
3096 
3097    PROCEDURE assigntts(
3098      p_api_version          IN            NUMBER
3099    , p_init_msg_list        IN            VARCHAR2 :=fnd_api.g_false
3100    , p_commit               IN            VARCHAR2 :=fnd_api.g_false
3101    , p_validation_level     IN            NUMBER   := fnd_api.g_valid_level_full
3102    , x_return_status        OUT NOCOPY    VARCHAR2
3103    , x_msg_count            OUT NOCOPY    NUMBER
3104    , x_msg_data             OUT NOCOPY    VARCHAR2
3105    , p_move_order_header_id IN            NUMBER
3106    ) IS
3107 
3108 
3109    CURSOR c_tasks IS
3110       SELECT transaction_temp_id
3111         FROM wms_cartonization_temp wct
3112        WHERE
3113           parent_line_id is null or             -- non bulked records
3114           parent_line_id = transaction_temp_id;  -- parents only
3115 
3116    Cursor c_tasks_con is
3117    Select transaction_temp_id
3118    From wms_cartonization_temp
3119    Where parent_line_id = transaction_temp_id;
3120 
3121 
3122      l_task_id       NUMBER;
3123      l_return_status VARCHAR2(1);
3124      l_msg_count     NUMBER;
3125      l_msg_data      VARCHAR2(2000);
3126      l_api_name      VARCHAR2(30)   := 'assignTTs';
3127  BEGIN
3128     -- Initialize API return status to success
3129     x_return_status  := fnd_api.g_ret_sts_success;
3130     IF p_move_order_header_id <> -1 THEN
3131         OPEN c_tasks;
3132     ELSE OPEN c_tasks_con;
3133     END IF;
3134 
3135     LOOP
3136       IF p_move_order_header_id <> -1 THEN
3137           FETCH c_tasks INTO l_task_id;
3138           EXIT WHEN c_tasks%NOTFOUND;
3139       ELSE
3140           FETCH c_tasks_con INTO l_task_id;
3141           EXIT WHEN c_tasks_con%NOTFOUND;
3142       END IF;
3143       wms_rule_pvt.assigntt(
3144         p_api_version                => 1.0
3145       , p_task_id                    => l_task_id
3146       , x_return_status              => l_return_status
3147       , x_msg_count                  => l_msg_count
3148       , x_msg_data                   => l_msg_data
3149       );
3150 
3151       IF l_return_status <> fnd_api.g_ret_sts_success THEN
3152         x_return_status  := fnd_api.g_ret_sts_error;
3153       END IF;
3154     END LOOP;
3155     IF p_move_order_header_id <> -1 THEN
3156         CLOSE c_tasks;
3157     ELSE
3158         CLOSE c_tasks_con;
3159     END IF;
3160   EXCEPTION
3161     WHEN OTHERS THEN
3162       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3163   END assigntts;
3164 
3165 
3166  --   Stop level
3167  --    if specified as zero - cartonizes till the levels setup
3168 
3169  PROCEDURE cartonize(
3170                       p_api_version           IN    NUMBER,
3171                       p_init_msg_list         IN    VARCHAR2 :=fnd_api.g_false,
3172                       p_commit                IN    VARCHAR2 :=fnd_api.g_false,
3173                       p_validation_level      IN    NUMBER   :=fnd_api.g_valid_level_full,
3174                       x_return_status         OUT   NOCOPY VARCHAR2,
3175                       x_msg_count             OUT   NOCOPY NUMBER,
3176                       x_msg_data              OUT   NOCOPY VARCHAR2,
3177                       p_out_bound             IN    VARCHAR2,
3178                       p_org_id                IN    NUMBER,
3179                       p_move_order_header_id  IN    NUMBER,
3180                       p_disable_cartonization IN    VARCHAR2,
3181                       p_transaction_header_id IN    NUMBER,
3182                       p_stop_level            IN    NUMBER,
3183                       p_packaging_mode        IN    NUMBER,
3184                       p_input_for_bulk        IN    WMS_BULK_PICK.bulk_input_rec  DEFAULT null
3185                       ) IS
3186 
3187                          l_api_name     CONSTANT VARCHAR2(30) := 'cartonize';
3188                          l_api_version  CONSTANT NUMBER       := 1.0;
3189 
3190 
3191                          v1 WCT_ROW_TYPE;
3192 
3193                          cartonization_profile  VARCHAR2(1)   := 'Y';
3194                          v_cart_value NUMBER;
3195                          v_container_item_id NUMBER:= NULL;
3196                          v_qty NUMBER:= -1;
3197                          v_qty_per_cont NUMBER := -1;
3198                          v_tr_qty_per_cont NUMBER:= -1;
3199                          v_sec_tr_qty_per_cont NUMBER:= -1; --invconv kkillams
3200                          v_lpn_out VARCHAR2(30) := ' ';
3201                          v_primary_uom_code VARCHAR2(3);
3202                          v_loop NUMBER := 0;
3203                          v_process_id NUMBER := 0;
3204                          v_ttemp_id NUMBER := 0;
3205                          v_lpn_id_out NUMBER := 0;
3206                          v_lpn_id NUMBER := 0;
3207                          ret_value   NUMBER := 29;
3208                          v_return_status VARCHAR2(1);
3209                          v_left_prim_quant NUMBER;
3210                          v_left_tr_quant NUMBER;
3211                          v_sec_left_tr_quant NUMBER; --invconv kkillams
3212                          v_sublvlctrl VARCHAR2(1) := '2';
3213                          --Bug 2720653 fix
3214                          --l_lpn_id_tbl inv_label.transaction_id_rec_type;
3215                          l_return_status VARCHAR2(1) := fnd_api.g_ret_sts_success;
3216                          l_msg_count NUMBER;
3217                          l_msg_data VARCHAR2(400);
3218                          l_progress VARCHAR2(10);
3219                          l_label_status VARCHAR2(500);
3220                          l_counter NUMBER := 0;
3221                          v_prev_move_order_line_id NUMBER := 0;
3222                          space_avail_for NUMBER := 0;
3223                          tr_space_avail_for NUMBER := 0;
3224                          sec_tr_space_avail_for NUMBER := 0; --invconv kkillams
3225                          curr_temp_id mtl_material_transactions_temp.transaction_temp_id%TYPE ;
3226                          v_lot_control_code NUMBER := NULL;
3227                          v_serial_control_code NUMBER := NULL;
3228                          api_table_index NUMBER := 0;
3229                          l_current_header_id  NUMBER := NULL;
3230                          l_stop_level    NUMBER := -1;
3231                          v_prev_item_id  NUMBER := NULL;
3232                          l_prev_package_id NUMBER := NULL;
3233                          l_temp_id       NUMBER := NULL;
3234                          l_item_id       NUMBER := NULL;
3235                          l_qty           NUMBER := NULL;
3236                          l_tr_qty        NUMBER := NULL;
3237                          l_sec_tr_qty    NUMBER := NULL;
3238                          l_clpn_id       NUMBER := NULL;
3239                          l_citem_id      NUMBER := NULL;
3240                          l_package_id      NUMBER := NULL;
3241                          l_upd_qty_flag VARCHAR2(1) := NULL;
3242                          l_prev_header_id NUMBER := NULL;
3243                          l_header_id     NUMBER := NULL;
3244                          L_no_pkgs_gen   VARCHAR2(1);
3245                          l_prev_condition  VARCHAR2(1);
3246                          l_revision_code   VARCHAR2(1);
3247                          l_lot_code        VARCHAR2(1);
3248                          l_serial_code     VARCHAR2(1);
3249                          l_is_revision_control  BOOLEAN;
3250                          l_is_lot_control       BOOLEAN;
3251                          l_is_serial_control    BOOLEAN;
3252                          l_rqoh NUMBER;
3253                          l_qr   NUMBER;
3254                          l_qs   NUMBER;
3255                          l_atr  NUMBER;
3256                          l_att  NUMBER;
3257                          l_qoh  NUMBER;
3258                          l_lpn_fully_allocated  VARCHAR2(1) :='N';
3259                          l_autocreate_delivery_flag VARCHAR2(1) := 'N';
3260                          percent_fill_basis         VARCHAR2(1) :='W';
3261                          l_valid_container VARCHAR2(1) := 'Y';
3262 
3263                          l_cartonize_sales_orders VARCHAR2(1) :=NULL;
3264                          l_cartonize_manufacturing VARCHAR2(1) :=NULL;
3265                          l_move_order_type   NUMBER;
3266 
3267                          l_rulebased_setup_exists    NUMBER    := 0;
3268                          l_api_return_status    VARCHAR2(1);
3269                          l_count1  NUMBER;
3270 
3271                          CURSOR wct_rows IS
3272                             SELECT wct.* FROM wms_cartonization_temp wct,
3273                               mtl_txn_request_lines mtrl,
3274                               mtl_secondary_inventories sub,
3275                               mtl_parameters mtlp
3276                               WHERE
3277                               wct.move_order_line_id =mtrl.line_id
3278                               AND mtrl.header_id = p_move_order_header_id
3279                               AND wct.cartonization_id IS null
3280                               AND mtlp.organization_id = wct.organization_id
3281                               AND sub.organization_id = wct.organization_id
3282 --                              AND wct.cartonization_id IS NULL
3283                               AND wct.transfer_lpn_id IS NULL
3284                               AND sub.secondary_inventory_name = wct.subinventory_code
3285                               AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
3286 				   OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
3287 				   OR (NVL(mtlp.cartonization_flag,-1) = 4)
3288 				   OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
3289 				  )
3290                                 ORDER BY wct.move_order_line_id,
3291                                 wct.inventory_item_id, Abs(wct.transaction_temp_id);
3292 
3293 
3294 
3295                               CURSOR OB_LBPRT IS
3296                                  SELECT DISTINCT mmtt.cartonization_id FROM
3297                                    --2513907 fix
3298                                  wms_cartonization_temp mmtt,
3299                                --mtl_material_transactions_temp mmtt,
3300                                mtl_txn_request_lines mtrl WHERE
3301                                mmtt.move_order_line_id = mtrl.line_id
3302                                AND mtrl.header_id      = p_move_order_header_id
3303                                AND mmtt.cartonization_id IS NOT NULL
3304                                  ORDER BY mmtt.cartonization_id;
3305 
3306                           CURSOR IB_LBPRT IS
3307                              SELECT DISTINCT mmtt.cartonization_id FROM
3308                                --2513907 fix
3309                                wms_cartonization_temp mmtt
3310                               --mtl_material_transactions_temp mmtt
3311                               WHERE
3312                               mmtt.transaction_header_id = p_transaction_header_id
3313                               AND mmtt.cartonization_id IS NOT NULL
3314                                 ORDER BY mmtt.cartonization_id;
3315 
3316                            /*
3317                            CURSOR bpack_rows(p_hdr_id NUMBER) IS
3318                               SELECT * FROM
3319                                 wms_cartonization_temp
3320                                 WHERE
3321                                 transaction_header_id = p_hdr_id
3322                                 AND cartonization_id IS NULL
3323                                 AND transfer_lpn_id IS NULL
3324                                 order by  move_order_line_id,
3325                                 decode(content_lpn_id,null,inventory_item_id,
3326                                                     decode(sign(p_hdr_id),
3327                                                            -1,
3328                                                            wms_cartnzn_pub.Get_package_ItemId(content_lpn_id),
3329                                                            wms_cartnzn_pub.Get_LPN_ItemId(content_lpn_id)
3330                              ) ); */
3331 
3332 
3333                            CURSOR bpack_rows(p_hdr_id NUMBER) IS
3334                               SELECT * FROM
3335                                 wms_cartonization_temp
3336                                 WHERE
3337                                 transaction_header_id = p_hdr_id
3338                                 AND cartonization_id IS NULL
3339                                 AND transfer_lpn_id IS NULL
3340                                 order by  move_order_line_id,
3341                                 decode(content_lpn_id,null,inventory_item_id,
3342                                                     decode(sign(p_hdr_id),
3343                                                            -1,
3344                                                            inventory_item_id,
3345                                                            wms_cartnzn_pub.Get_LPN_ItemId(content_lpn_id)
3346                              ) ),Abs(transaction_temp_id);
3347 
3348 
3349                            CURSOR packages(p_hdr_id NUMBER) IS
3350                               SELECT
3351                                transaction_temp_id,
3352                                inventory_item_id,
3353                                primary_quantity,
3354                                transaction_quantity,
3355                                secondary_transaction_quantity, --invconv kkillams
3356                                content_lpn_id,
3357                                container_item_id,
3358                                cartonization_id
3359                                FROM
3360                                wms_cartonization_temp
3361                                WHERE
3362                                transaction_header_id = p_hdr_id
3363                                order by cartonization_id;
3364 
3365                           CURSOR opackages(p_hdr_id NUMBER) IS
3366                              SELECT
3367                                wct.transaction_temp_id,
3368                                wct.inventory_item_id,
3369                                wct.primary_quantity,
3370                                wct.transaction_quantity,
3371                                wct.secondary_transaction_quantity, --invconv kkillams
3372                                wct.content_lpn_id,
3373                                wct.container_item_id,
3374                                wct.cartonization_id
3375                                FROM
3376                                wms_cartonization_temp wct,
3377                                mtl_txn_request_lines mtrl
3378                                WHERE
3379                                wct.move_order_line_id = mtrl.line_id AND
3380                                mtrl.header_id = p_hdr_id
3381                                order by wct.cartonization_id;
3382 
3383  BEGIN
3384 
3385     error_code := 'CARTONIZE 10';
3386 
3387     SAVEPOINT   cartonize_pub;
3388     IF (NOT fnd_api.compatible_api_call ( l_api_version,
3389                                          p_api_version,
3390                                          l_api_name,
3391                                          g_pkg_name )) THEN
3392        RAISE fnd_api.g_exc_error;
3393     END IF;
3394 
3395     IF (fnd_api.to_boolean ( p_init_msg_list )) THEN
3396        fnd_msg_pub.initialize;
3397     END IF;
3398 
3399     x_return_status := fnd_api.g_ret_sts_success;
3400 
3401     --setting the global variable for trace
3402     g_trace_on := fnd_profile.value('INV_DEBUG_TRACE');
3403 
3404     log_event('G_CURRENT_RELEASE_LEVEL : '||G_CURRENT_RELEASE_LEVEL);
3405 
3406     if (g_trace_on = 1) then
3407        log_event('Table_name is '||table_name);
3408 
3409        log_event('Deleting rows in wms_cartonization_temp');
3410     END IF;
3411 
3412     --making sure that we are starting with an empty table
3413     DELETE wms_cartonization_temp;
3414 
3415     pkg_attr_table.DELETE;
3416     lpn_attr_table.DELETE;
3417     lpns_generated_tb.DELETE;
3418 
3419     -- Setting the global variable for packaging
3420     packaging_mode := p_packaging_mode;
3421 
3422     -- Setting the global variable for sequence
3423     --sequence_id := 1;
3424 
3425     IF( packaging_mode = wms_cartnzn_pub.PR_pKG_mode OR
3426         packaging_mode = wms_cartnzn_pub.mfg_PR_pKG_mode) THEN
3427        outbound := 'Y';
3428      ELSIF(packaging_mode IN (wms_cartnzn_pub.int_bP_pkg_mode,
3429                                 wms_cartnzn_pub.mob_bP_pkg_mode ,
3430                                 wms_cartnzn_pub.prepack_pkg_mode) ) THEN
3431        outbound := 'N';
3432      ELSE
3433        outbound := p_out_bound;
3434     END IF;
3435 
3436     IF( p_disable_cartonization = 'Y') THEN
3437        -- This is the case when the cartonization is only called
3438        -- for task type assignment, task splitting and task consolidation
3439        cartonization_profile := 'N';
3440 
3441      ELSIF (outbound = 'N') THEN
3442         --  cartonization profile is always Y for non outbound
3443        cartonization_profile := 'Y';
3444 
3445      ELSE
3446        BEGIN
3447 
3448           SELECT Nvl(cartonization_flag,-1),
3449             Nvl(cartonize_sales_orders,'Y'),
3450             Nvl(cartonize_manufacturing,'N'),
3451             --Bug 3528061 fix
3452             Nvl(allocate_serial_flag,'N'),
3453             NVL(default_pick_op_plan_id,-1)
3454             INTO
3455             v_cart_value,
3456             l_cartonize_sales_orders,
3457             l_cartonize_manufacturing,
3458             --Bug 3528061 fix
3459             g_allocate_serial_flag,
3460             wms_cartnzn_pub.g_default_pick_op_plan_id
3461             FROM mtl_parameters
3462             WHERE organization_id = p_org_id;
3463 
3464             wms_cartnzn_pub.g_org_cartonization_value  :=  v_cart_value;
3465             wms_cartnzn_pub.g_org_cartonize_so_flag    :=  l_cartonize_sales_orders;
3466             wms_cartnzn_pub.g_org_cartonize_mfg_flag   :=  l_cartonize_manufacturing;
3467             wms_cartnzn_pub.g_org_allocate_serial_flag :=  g_allocate_serial_flag;
3468 
3469        EXCEPTION
3470           WHEN OTHERS THEN
3471              v_cart_value := NULL;
3472        END;
3473 
3474        IF( v_cart_value  = 1) THEN
3475 
3476           IF ((packaging_mode = wms_cartnzn_pub.pr_pkg_mode)
3477               AND (l_cartonize_sales_orders = 'Y'))
3478             OR
3479             ((packaging_mode = wms_cartnzn_pub.mfg_pr_pkg_mode)
3480              AND (l_cartonize_manufacturing = 'Y'))
3481             THEN
3482              -- Cartonization is enabled for the whole organization
3483              v_sublvlctrl := '1';
3484              cartonization_profile := 'Y';
3485            ELSE
3486              cartonization_profile := 'N';
3487           END IF;
3488 
3489         ELSIF( v_cart_value  = 3) THEN
3490 
3491           IF ((packaging_mode = wms_cartnzn_pub.pr_pkg_mode)
3492               AND (l_cartonize_sales_orders = 'Y'))
3493             OR
3494             ((packaging_mode = wms_cartnzn_pub.mfg_pr_pkg_mode)
3495              AND (l_cartonize_manufacturing = 'Y'))
3496             THEN
3497              --cartonization is controlled at the subinventory level
3498              v_sublvlctrl := '3';
3499              cartonization_profile := 'Y';
3500            ELSE
3501              cartonization_profile := 'N';
3502           END IF;
3503         ELSE
3504           cartonization_profile := 'N';
3505        END IF;
3506 
3507     END IF;
3508 
3509     g_sublvlctrl := v_sublvlctrl ; --Bug#7168367.Store it in global variable
3510 
3511     BEGIN
3512        SELECT percent_fill_basis_flag
3513          INTO percent_fill_basis
3514          FROM wsh_shipping_parameters
3515          WHERE organization_id = p_org_id AND
3516          ROWNUM = 1;
3517          wms_cartnzn_pub.g_percent_fill_basis := percent_fill_basis;
3518     EXCEPTION
3519        WHEN OTHERS THEN
3520           percent_fill_basis := 'W';
3521           wms_cartnzn_pub.g_percent_fill_basis := 'W';
3522     END;
3523 
3524     IF(g_trace_on = 1) THEN
3525        log_event(' cartonization profile is '||cartonization_profile||' outbound is'||outbound || ' controlled at sub level is '||v_sublvlctrl);
3526        log_event('Percent fill basis '||percent_fill_basis);
3527     END IF;
3528 
3529     IF (p_move_order_header_id <> -1) THEN
3530        BEGIN
3531           SELECT Nvl(auto_pick_confirm_flag, 'N')
3532                , Nvl(autocreate_delivery_flag, 'N')
3533           INTO l_auto_pick_confirm_flag
3534              , l_autocreate_delivery_flag
3535           FROM wsh_picking_batches
3536           WHERE batch_id =
3537           (SELECT request_number
3538           FROM mtl_txn_request_headers
3539           WHERE header_id = p_move_order_header_id
3540           AND move_order_type = inv_globals.G_MOVE_ORDER_PICK_WAVE);
3541 
3542           IF l_auto_pick_confirm_flag = 'Y' THEN
3543              IF (g_trace_on = 1) THEN log_event('Auto pick confirm is ON for this batch'); END IF;
3544           END IF;
3545 
3546           WMS_CARTNZN_PUB.g_autocreate_delivery_flag := l_autocreate_delivery_flag;
3547           WMS_CARTNZN_PUB.g_auto_pick_confirm_flag := l_auto_pick_confirm_flag;
3548 
3549        EXCEPTION
3550        WHEN OTHERS THEN
3551           l_auto_pick_confirm_flag := 'N';
3552           WMS_CARTNZN_PUB.g_autocreate_delivery_flag := 'N';
3553           WMS_CARTNZN_PUB.g_auto_pick_confirm_flag := 'N';
3554        END;
3555     END IF;
3556 
3557 ------------------------ ER : 6682436 START ---------------------------------
3558 -- Rule based cartonization will only be called for Pick Release Mode -------
3559 
3560    IF(g_trace_on = 1) THEN
3561       log_event('Packagin mode : '|| packaging_mode);
3562       log_event('wms_cartnzn_pub.PR_pKG_mode : '|| wms_cartnzn_pub.PR_pKG_mode);
3563    END IF;
3564 
3565    IF WMS_CONTROL.G_CURRENT_RELEASE_LEVEL >= 120001
3566    AND packaging_mode = wms_cartnzn_pub.PR_pKG_mode THEN
3567 
3568       IF(g_trace_on = 1) THEN
3569          log_event('Checking the rules setup');
3570       END IF;
3571 
3572       SELECT count(1)
3573        INTO l_rulebased_setup_exists
3574        FROM wms_selection_criteria_txn_v
3575       WHERE rule_type_code = 12
3576         AND enabled_flag = 1
3577         AND from_organization_id = p_org_id
3578         AND ROWNUM = 1;
3579 
3580       IF l_rulebased_setup_exists > 0 THEN
3581          IF(g_trace_on = 1) THEN
3582             log_event('Rules setup exists');
3583          END IF;
3584 
3585          table_name := 'wms_cartonization_temp';
3586 
3587          IF wms_cartnzn_pub.g_org_cartonization_value IN (1,3) THEN
3588             log_event('Calling rulebased cartonization ');
3589             WMS_CARTNZN_PUB.rulebased_cartonization
3590             (
3591             x_return_status         => l_api_return_status,
3592             x_msg_count             => x_msg_count,
3593             x_msg_data              => x_msg_data,
3594             p_out_bound             => 'Y',
3595             p_org_id                => p_org_id,
3596             p_move_order_header_id  => p_move_order_header_id,
3597             p_input_for_bulk        => null
3598             );
3599 
3600             log_event('RETURNING FROM CARTONIZATION');
3601             RETURN;   -- Bug : 6962305
3602          END IF;
3603       ELSE
3604          log_event('Rulebased setup does not exist, going to default cartonization logic');
3605       END IF;  -- Rulebased cartonization
3606    END IF;  -- Release level check
3607 ------------------------ ER : 6682436 END ---------------------------------
3608 
3609     -- This setting ensures that all operations we do are on
3610     -- wms_cartonization_temp until its changed
3611     table_name := 'wms_cartonization_temp';
3612 
3613     IF (g_trace_on = 1) THEN log_event(' Inserting mmtt rows to wct'); END IF;
3614 
3615     IF  ( outbound = 'Y') THEN
3616 
3617        IF ( p_move_order_header_id = 0) THEN
3618           error_code := 'CARTONIZE 20';
3619           if (g_trace_on = 1) then log_event(' error move order header id '||p_move_order_header_id);  END IF;
3620           RAISE fnd_api.g_exc_error;
3621        END IF;
3622 
3623        -- changed for patchset J bulk picking -----------
3624        -- change for concurrent bulk picking program ------
3625 
3626        IF (g_trace_on = 1) THEN log_event(' move order header id '||p_move_order_header_id);  END IF;
3627        IF (p_move_order_header_id = -1 ) THEN  -- this is called from concurrent program
3628         --   if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING '); end if;
3629            l_move_order_type := INV_GLOBALS.G_MOVE_ORDER_PICK_WAVE;
3630            if (g_trace_on = 1) then log_event('This is called from concurrent program'); end if;
3631        ELSE
3632         -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING '); end if;
3633          SELECT move_order_type
3634          INTO l_move_order_type
3635          FROM mtl_txn_request_headers
3636          WHERE header_id = p_move_order_header_id;
3637 
3638          if (g_trace_on = 1) then log_event('Move order type:'||l_move_order_type); end if;
3639       END IF;
3640 
3641       l_auto_pick_confirm_flag := 'N';
3642 
3643        IF l_auto_pick_confirm_flag = 'Y' THEN
3644           IF (g_trace_on = 1) THEN log_event('Auto pick confirm is ON - Not calling task type assignment'); END IF;
3645 
3646        ELSE
3647           error_code := 'CARTONIZE 30';
3648 
3649           --- patchset J, bulk picking    ---------------------------
3650           IF G_CURRENT_RELEASE_LEVEL < G_J_RELEASE_LEVEL OR
3651              l_move_order_type <> G_MOVE_ORDER_PICK_WAVE THEN
3652               IF (g_trace_on = 1) THEN log_event(' calling wms_rule_pvt.assigntts for task type assignment' );  END IF;
3653               wms_rule_pvt.assigntts(
3654                                   p_api_version => 1.0,
3655                                   p_move_order_header_id =>  p_move_order_header_id,
3656                                   x_return_status    => l_return_status,
3657                                   x_msg_count        => l_msg_count,
3658                                   x_msg_data         => l_msg_data);
3659 
3660           IF l_return_status <> fnd_api.g_ret_sts_success THEN
3661              error_code := 'CARTONIZE 40';
3662              -- insert the message into the stack
3663              if (g_trace_on = 1) then log_event(' Task type assignment failed'); END IF;
3664           END IF;
3665 
3666           l_return_status := fnd_api.g_ret_sts_success;
3667           END IF; -- end the in line branching
3668 
3669           IF (p_move_order_header_id <> -1 ) THEN
3670               if (g_trace_on = 1) then log_event(' calling wms_rule_pvt.assign_operation_plan for  OP assignment' );  END IF;
3671               wms_rule_pvt.assign_operation_plans(
3672                                                p_api_version => 1.0,
3673                                                p_move_order_header_id => p_move_order_header_id,
3674                                                x_return_status    => l_return_status,
3675                                                x_msg_count        => l_msg_count,
3676                                                x_msg_data         => l_msg_data);
3677 
3678               IF l_return_status <> fnd_api.g_ret_sts_success THEN
3679                   error_code := 'CARTONIZE 45';
3680                   -- insert the message into the stack
3681               if (g_trace_on = 1) then log_event(' Operation plan assignment failed'); END IF;
3682               END IF;
3683           END IF;
3684 
3685        END IF;
3686 
3687 
3688        IF (g_trace_on = 1) THEN log_event(' inserting mmtt rows of this header into wms_cartonization_temp '|| p_move_order_header_id );  END IF;
3689 
3690        error_code := 'CARTONIZE 50';
3691 
3692        -- Bug 2457428 fix
3693 
3694        create_wct(p_move_order_header_id => p_move_order_header_id,
3695                   p_transaction_header_id => NULL,
3696                   p_input_for_bulk =>p_input_for_bulk);
3697 
3698        --INSERT INTO wms_cartonization_temp SELECT mmtt.* FROM
3699        --mtl_material_transactions_temp mmtt, mtl_txn_request_lines
3700        --mtrl WHERE mtrl.header_id = p_move_order_header_id AND
3701        --mmtt.move_order_line_id = mtrl.line_id;
3702 
3703      ELSE --outbound = 'N'
3704 
3705        error_code := 'CARTONIZE 60';
3706        if (g_trace_on = 1) then log_event(' inserting mmtt rows of this header into wms_cartonization_temp '|| p_transaction_header_id ); END IF;
3707 
3708        -- Bug 2457428 fix
3709 
3710        create_wct(p_move_order_header_id => NULL,
3711                   p_transaction_header_id => p_transaction_header_id
3712                   );
3713 
3714        --INSERT INTO wms_cartonization_temp
3715        --SELECT mmtt.* FROM
3716        --mtl_material_transactions_temp mmtt
3717        --WHERE
3718        --mmtt.transaction_header_id = p_transaction_header_id;
3719 
3720     END IF;
3721 
3722     -- Setting the stop level
3723     IF p_stop_level IS NULL THEN
3724        l_stop_level := -1;
3725      ELSE
3726        l_stop_level := p_stop_level;
3727     END IF;
3728 
3729     -- Setting the current level we are packing
3730     pack_level := 0;
3731 
3732 
3733     IF(outbound = 'Y') THEN
3734        l_current_header_id := p_move_order_header_id;
3735      ELSE
3736        l_current_header_id := p_transaction_header_id;
3737     END IF;
3738 
3739 
3740     if (g_trace_on = 1) then
3741        log_event(' l_stop_level '||l_stop_level);
3742        log_event(' l_current_level '||pack_level);
3743        log_event(' l_current_header_id '||l_current_header_id);
3744     END IF;
3745 
3746     IF( cartonization_profile = 'Y') THEN
3747 
3748        if (g_trace_on = 1) then log_event(' cartonization_profile '||cartonization_profile); END IF;
3749 
3750 
3751        --- THis is the Multi level Cartonization loop
3752 
3753        LOOP
3754 
3755           if (g_trace_on = 1) then log_event(' cur lev '||pack_level ||' stop lev'||l_stop_level); END IF;
3756 
3757           EXIT WHEN ( (pack_level >= l_stop_level) AND (l_stop_level <> -1));
3758 
3759           v_prev_item_id := -1;
3760           v_prev_move_order_line_id  := -1;
3761 
3762 
3763           --bpack_rows should select only rows with transaction_header_id = l_current_header_id
3764           if (g_trace_on = 1) then log_event(' opening  cusror  hdr id'||l_current_header_id); END IF;
3765 
3766 
3767           IF( (outbound = 'Y') AND (pack_level = 0)) THEN
3768 
3769              error_code := 'CARTONIZE 70';
3770              if (g_trace_on = 1) then log_event('opening wct_rows'); END IF;
3771              OPEN wct_rows;
3772              if (g_trace_on = 1) then log_event('opened bpack rows'); END IF;
3773            ELSE
3774              error_code := 'CARTONIZE 90';
3775              if (g_trace_on = 1) then log_event('opening bpack_rows'); END IF;
3776              OPEN bpack_rows(l_current_header_id);
3777              if (g_trace_on = 1) then log_event(' Opened bpack rows'); END IF;
3778           END IF;
3779 
3780           -- THis is the loop for cartonization using the container - item
3781           -- relation ship. The cursor already orders by move_order_line_id
3782           -- inventory_item_id. Here is the pseudo code logic
3783           -- 1)Fetch a line
3784           -- 2)if line has the same move_order_line_id  as the previously fetched row.
3785           --     a) call the shipping api to see if any container is
3786           --          setup
3787           --     b) get the quantity per container
3788           --   ELSE
3789           --     if space is available in previous container
3790           --         pack as much of this item into that container
3791           --         and reduce the primary_quantity, space available for
3792           --         correspondingly
3793           --   end if;
3794           -- 3)if the line has primary_quantity to pack
3795           --     calculate the number of containers needed to pack from
3796           --     quantity per container, pack the line
3797           --   else
3798           --   go to the top of the loop to fetch new row
3799 
3800           error_code := 'CARTONIZE 100';
3801 
3802           LOOP
3803 
3804              if (g_trace_on = 1) then log_event(' Fetching rows'); END IF;
3805 
3806              IF( (outbound = 'Y') AND (pack_level = 0)) THEN
3807 
3808                 error_code := 'CARTONIZE 110';
3809                 FETCH wct_rows INTO v1;
3810                 EXIT WHEN wct_rows%notfound;
3811 
3812                 SELECT
3813                   revision_qty_control_code,
3814                   lot_control_code,
3815                   serial_number_control_code
3816                   INTO
3817                   l_revision_code,
3818                   l_lot_code,
3819                   l_serial_code
3820                   FROM    mtl_system_items
3821                   WHERE   organization_id = v1.organization_id
3822                   AND     inventory_item_id = v1.inventory_item_id;
3823 
3824                 if l_revision_code> 1 then
3825                    l_is_revision_control := TRUE;
3826                  else
3827                    l_is_revision_control := FALSE;
3828                 end if;
3829 
3830                 IF l_lot_code > 1 THEN
3831                    l_is_lot_control := TRUE;
3832                  ELSE
3833                    l_is_lot_control := FALSE;
3834                 END IF;
3835 
3836                 IF (l_serial_code>1 AND l_serial_code<>6) then
3837                    l_is_serial_control := TRUE;
3838                  else
3839                    l_is_serial_control := FALSE;
3840                 end if;
3841 
3842                 IF (v1.allocated_lpn_id IS NOT NULL ) THEN
3843 
3844                    error_code := 'CARTONIZE 120';
3845 
3846                       -- Bug 3740610 No need to call qty tree,getting on-hand qty from table
3847 
3848                         SELECT NVL(SUM(primary_transaction_quantity),0)
3849                          into l_qoh FROM mtl_onhand_quantities_detail
3850                          WHERE organization_id = v1.organization_id
3851                           AND subinventory_code = v1.subinventory_code
3852                           AND locator_id = v1.locator_id
3853                          AND lpn_id = v1.allocated_lpn_id;
3854                           if (g_trace_on = 1) then log_event('lpn_id' || v1.allocated_lpn_id);
3855                                         log_event('l_qoh' || l_qoh);
3856                                         log_event('PrimaryQty'|| v1.primary_quantity);
3857                            end if;
3858 
3859                     --Bug 3740610 Comparing on-hand qty with primary_qty of mmtt
3860 
3861                      IF (  l_qoh = v1.primary_quantity ) THEN
3862                          l_lpn_fully_allocated := 'Y';
3863                          select v1.transaction_temp_id, 'Y'
3864                            into t_lpn_alloc_flag_table(v1.transaction_temp_id)
3865                            from dual;
3866 
3867                        ELSE
3868                          l_lpn_fully_allocated := 'N';
3869                          select v1.transaction_temp_id, 'N'
3870                            into t_lpn_alloc_flag_table(v1.transaction_temp_id)
3871                            from dual;
3872                       END IF;
3873                     ELSE
3874                       l_lpn_fully_allocated := 'Y';
3875                       select v1.transaction_temp_id, 'Y'
3876                         into t_lpn_alloc_flag_table(v1.transaction_temp_id)
3877                         from dual;
3878                       if (g_trace_on = 1) then log_event(' calling qty tree returns unsuccessfully' ); END IF;
3879                    END IF ;
3880 
3881               ELSE
3882                    error_code := 'CARTONIZE 130';
3883                    -- outbound = 'N'
3884                    FETCH bpack_rows INTO v1;
3885 
3886                    EXIT WHEN bpack_rows%notfound;
3887 
3888                    -- THis setting always ensures that in non outbound
3889                    -- modes, cartonization is not retricted by lpn
3890                    -- allocation values
3891 
3892                    l_lpn_fully_allocated := 'N';
3893              END IF;
3894 
3895              if (g_trace_on = 1) then log_event(' Fetch succeeded');
3896                                       log_event('lpn_fully_allocated:'||l_lpn_fully_allocated);
3897                                        log_event ('lpn_id after if:'||v1.allocated_lpn_id);
3898               end if;
3899 
3900 
3901              IF ( v1.allocated_lpn_id IS NOT NULL ) AND
3902                ( l_lpn_fully_allocated = 'Y' ) THEN
3903                 null;
3904               ELSE
3905 
3906                 --populate lpn_alloc_flag with null for loose item
3907                 select v1.transaction_temp_id, null
3908                   into  t_lpn_alloc_flag_table(v1.transaction_temp_id)
3909                   from dual;
3910 
3911 
3912                 -- If the content_lpn_id is populated on the mmtt record
3913                 -- could be two cases. Either we are trying to pack an LPN
3914                 -- or a package. We will have packages poulated in this
3915                 -- column only by multi level cartonization and when it
3916                 -- does that, the row is inserted with negative header id
3917                 -- Basing on this we either get the item associated with
3918                 -- the lpn, or item associated with the package
3919 
3920                 error_code := 'CARTONIZE 140';
3921 
3922                 if ( v1.content_lpn_id is not null ) then
3923 
3924                    if (g_trace_on = 1) then log_event(' content_lpn_id IS NOT NULL'); END IF;
3925 
3926                    IF v1.transaction_header_id  < 0 THEN
3927                       error_code := 'CARTONIZE 150';
3928                       --THe content_lpn_id has a package in it ..
3929                       --v1.inventory_item_id := Get_package_ItemId(v1.content_lpn_id);
3930 
3931                     ELSE
3932                       error_code := 'CARTONIZE 160';
3933                       --THe content_lpn_id has a LPN in it ..
3934                       v1.inventory_item_id := Get_LPN_ItemId(v1.content_lpn_id);
3935                    END IF;
3936 
3937                    -- When we are packaing an lpn or a package the qty is
3938                    -- always 1
3939                    v1.primary_quantity := 1;
3940                    v1.transaction_quantity := 1;
3941 
3942                 end if;
3943 
3944                 error_code := 'CARTONIZE 170';
3945                 SELECT primary_uom_code INTO v_primary_uom_code FROM mtl_system_items
3946                   WHERE inventory_item_id = v1.inventory_item_id AND
3947                   organization_id = v1.ORGANIZATION_ID;
3948 
3949                 if ( v1.content_lpn_id is not null ) THEN
3950                    -- We want to set the transaction uom same as primary uom
3951                    v1.transaction_uom := v_primary_uom_code;
3952                 END IF;
3953 
3954 
3955 
3956                 if (g_trace_on = 1) then
3957                    log_event(' inventory_item_id:'||v1.inventory_item_id);
3958                    log_event(' primary_quantity:'||v1.primary_quantity);
3959                    log_event(' primary_uom_code:'||v_primary_uom_code);
3960                    log_event(' transaction_quantity:'||v1.transaction_quantity);
3961                    log_event(' transaction_uom:'||v1.transaction_uom);
3962                    log_event(' secondary_transaction_quantity:'||v1.secondary_transaction_quantity); --invconv kkillams
3963                    log_event(' secondary_uom_code:'||v1.secondary_uom_code); --invconv kkillams
3964                 END IF;
3965 
3966 
3967 
3968                 IF (outbound = 'Y') AND (pack_level = 0) AND
3969                   (v_prev_move_order_line_id  <> v1.move_order_line_id) THEN
3970                    l_prev_condition := 'Y';
3971 
3972                  ELSIF (outbound = 'Y') AND (pack_level <> 0) AND
3973                    ((v_prev_move_order_line_id  <> v1.move_order_line_id) OR
3974                    ( v1.inventory_item_id <> v_prev_item_id)) THEN
3975 
3976                    l_prev_condition := 'Y';
3977                  ELSIF (outbound = 'N') AND ( v1.inventory_item_id <>
3978                                               v_prev_item_id) THEN
3979                    l_prev_condition := 'Y';
3980                  ELSE
3981                    l_prev_condition := 'N';
3982                 END IF;
3983 
3984                 -- The below condition is used when to make a
3985                 -- call to the an api that returns conatiner item relation
3986                 -- ship if present
3987                 -- In outbound mode
3988                 --  you have to call this when previous move order line id
3989                 --  is different from the current one
3990                 -- In the inbound mode
3991                 --  We need to call if the previous item is different form
3992                 -- the current item
3993 
3994                 IF l_prev_condition = 'Y' THEN
3995 
3996 
3997                    if (g_trace_on = 1) then log_event(' call wms_container_pub.container_required_qty api for item id '||v1.inventory_item_id);  END IF;
3998 
3999 
4000                    v_prev_item_id := v1.inventory_item_id;
4001                    v_prev_move_order_line_id := v1.move_order_line_id;
4002                    v_container_item_id := NULL;
4003                    v_qty_per_cont := -1;
4004                    v_qty := -1;
4005                    space_avail_for := 0;
4006                    --Bug 2478970 fix
4007                    l_valid_container := 'Y';
4008 
4009                    IF (outbound = 'Y') AND (pack_level = 0)
4010                        AND (packaging_mode = wms_cartnzn_pub.pr_pkg_mode) THEN
4011                       error_code := 'CARTONIZE 180';
4012                       wsh_interface.Get_Max_Load_Qty(p_move_order_line_id => v1.move_order_line_id,
4013                                                      x_max_load_quantity  => v_qty_per_cont,
4014                                                      x_container_item_id  => v_container_item_id,
4015                                                      x_return_status     => v_return_status);
4016 
4017                       l_valid_container := 'Y'; --Undoing 2478970 fix
4018                       --Bug 2478970 fix
4019                       --l_valid_container := validate_cont_item(p_item_id =>v_container_item_id,
4020                       --                                        p_org_id  =>v1.organization_id);
4021                       --log_event('validate_cont ret '|| l_valid_container);
4022                       --Bug 2478970 fix
4023 
4024                       IF(  (v_return_status = fnd_api.g_ret_sts_success) AND
4025                            (v_qty_per_cont > 0) AND
4026                            (v_container_item_id IS NOT NULL) AND
4027                            (v_container_item_id > 0) ) THEN
4028 
4029                          v_qty := ceil(v1.primary_quantity/v_qty_per_cont);
4030                          -- This quantity needs to be recalculated. This is
4031                          -- poulated to pass the check marked by '#chk1'
4032 
4033                       END IF;
4034 
4035 
4036                       IF (g_trace_on = 1) THEN
4037                          log_event('wsh_interface.Get_Max_Load_Qty return status'||v_return_status);
4038                          log_event('container '||v_container_item_id);
4039                          log_event('Number of dum containers '||v_qty);
4040                       END IF;
4041 
4042                       v_prev_move_order_line_id := v1.move_order_line_id;
4043 
4044 
4045 
4046                     ELSE
4047                       error_code := 'CARTONIZE 190';
4048                       wms_container_pub.Container_Required_Qty
4049                         (  p_api_version       => 1.0,
4050                            x_return_status     => v_return_status,
4051                            x_msg_count         => l_msg_count,
4052                            x_msg_data          => l_msg_data,
4053                            p_source_item_id    => v1.inventory_item_id,
4054                            p_source_qty        => v1.primary_quantity,
4055                            p_source_qty_uom    => v_primary_uom_code,
4056                            p_organization_id   => v1.organization_id,
4057                            p_dest_cont_item_id => v_container_item_id,
4058                            p_qty_required       => v_qty
4059                            );
4060 
4061                       if (g_trace_on = 1) then
4062                          log_event('container_required_quantity return status'||v_return_status);
4063                          log_event('container '||v_container_item_id);
4064                          log_event('Number of conatiners '||v_qty);
4065                       END IF;
4066 
4067 
4068                      v_prev_item_id := v1.inventory_item_id;
4069 
4070                      IF( (v_return_status = fnd_api.g_ret_sts_success )   AND
4071                          (v_qty IS NOT NULL) AND
4072                          (v_qty > 0) AND
4073                          (v_container_item_id IS NOT NULL) AND
4074                          (v_container_item_id > 0) ) THEN
4075 
4076                         error_code := 'CARTONIZE 200';
4077 
4078 
4079                            SELECT max_load_quantity INTO v_qty_per_cont FROM wsh_container_items
4080                              WHERE load_item_id = v1.inventory_item_id AND
4081                              master_organization_id = v1.organization_id AND
4082                              container_item_id = v_container_item_id;
4083 
4084                      END IF;
4085 
4086                    END IF;
4087 
4088 
4089 
4090                    if (g_trace_on = 1) then log_event('qty per container is '||v_qty_per_cont);  END IF;
4091 
4092                    --#chk1
4093 
4094                    IF( (v_return_status <> fnd_api.g_ret_sts_success ) OR
4095                        (v_qty_per_cont IS NULL) OR
4096                        (v_qty IS NULL) OR
4097                        (v_container_item_id IS NULL) OR
4098                        (v_qty <= 0) OR
4099                        (v_container_item_id <= 0) OR
4100                        (v_qty_per_cont <= 0) OR
4101                        --Bug 2478970 fix
4102                        l_valid_container = 'N'
4103                        ) THEN
4104 
4105                       if (g_trace_on = 1) then log_event('improper values returned by container_required_qty ');  END IF;
4106 
4107 
4108                     ELSE
4109 
4110                       select msi.serial_number_control_code
4111                         INTO  v_serial_control_code
4112                         from mtl_system_items msi
4113                         where
4114                         msi.inventory_item_id = v1.inventory_item_id AND
4115                         msi.organization_id  = v1.organization_id;
4116 
4117 
4118                       IF( (v_serial_control_code NOT IN (1,6) ) AND
4119                           (Ceil(v_qty_per_cont) > v_qty_per_cont )
4120                           ) THEN
4121 
4122                          if (g_trace_on = 1) then
4123                             log_event('cannot split serial controlled items to  fractions');
4124                             log_event('Please check the container item relationships');
4125                          END IF;
4126 
4127                          v_qty_per_cont := 0;
4128                          v_serial_control_code := NULL;
4129                       END IF;
4130 
4131                       v_serial_control_code := NULL;
4132 
4133                       v_tr_qty_per_cont := inv_convert.inv_um_convert
4134                         ( v1.inventory_item_id,
4135                           5,
4136                           v_qty_per_cont,
4137                           v_primary_uom_code,
4138                           v1.transaction_uom,
4139                           NULL,
4140                          null
4141                           );
4142                      --invconv kkillams
4143                      IF v1.secondary_uom_code IS NOT NULL THEN
4144                   /*   v_sec_tr_qty_per_cont := ROUND((v_qty_per_cont * inv_convert.inv_um_convert(
4145                                                                      p_item_id       => v1.inventory_item_id,
4146                                                                      p_from_uom_code => v_primary_uom_code,
4147                                                                      p_to_uom_code   => v1.secondary_uom_code
4148                                                                      )),
4149                                                                      5);
4150                   */
4151 				--The secondary transaction qty is now calculated as a ratio coz in deviation cases the above logic does not work. See bug 10246979 and bug 10246444.
4152 				--The logic used below to calculate the sec qty is - [(Task total sec qty)/(Task total Primary qty)]*(Primary that can fix in 1 Carton)
4153 		        --Note that even with current logic there is a chance of creating task with qty more than requested but cannot workaround that as its a mathematical issue due to rounding diff b/w OM, INV and WMS
4154 				--The following fix would also not work for OPM customers if they start using this along with changes in allocation from bug 12978826.
4155 				--i.e Cartonization on top of allocation fix would start splitting in decimal which is problematic but at moment we do not have logic to take care of those cases..
4156 
4157 
4158 				  v_sec_tr_qty_per_cont := ROUND((v1.secondary_transaction_quantity / v1.primary_quantity) * v_tr_qty_per_cont,5);
4159 
4160 					ELSE
4161 					v_sec_tr_qty_per_cont := NULL;
4162 					END IF;
4163                       if (g_trace_on = 1) then log_event(' Cartonize Transaction qty per conatiner is '||v_tr_qty_per_cont); END IF;
4164                       if (g_trace_on = 1) then log_event(' Cartonize Secondary Transaction qty per conatiner is '||v_sec_tr_qty_per_cont); END IF;
4165 					  if (g_trace_on = 1) then log_event('  Tasks total secondary qty :'||v1.secondary_transaction_quantity); END IF;
4166 				      if (g_trace_on = 1) then log_event('  Tasks total primary qty   :'||v1.primary_quantity); END IF;
4167                    END IF;
4168 
4169                  ELSE
4170 
4171                    IF (space_avail_for > 0) THEN
4172 
4173                       if (g_trace_on = 1) then log_event(' Space available for '||space_avail_for); END IF;
4174 
4175                       IF (v1.primary_quantity <= space_avail_for) THEN
4176 
4177                          if (g_trace_on = 1) then log_event(' Prim qty '||v1.primary_quantity|| ' <= '||space_avail_for); END IF;
4178 
4179                          space_avail_for := space_avail_for -  v1.primary_quantity;
4180 
4181                          IF( v1.content_lpn_id IS NULL) THEN
4182                             l_upd_qty_flag := 'Y';
4183 
4184                           ELSE
4185                             l_upd_qty_flag := 'N';
4186                          END IF;
4187 
4188 
4189                          update_mmtt
4190                            (        p_transaction_temp_id => v1.transaction_temp_id,
4191                                     p_primary_quantity   => v1.primary_quantity,
4192                                     p_transaction_quantity  => v1.transaction_quantity,
4193                                     p_secondary_quantity    => v1.secondary_transaction_quantity, --invconv kkillams
4194                                     --p_LPN_string            => v_lpn_out,
4195                                     p_lpn_id                => v_lpn_id,
4196                                     p_container_item_id     => v_container_item_id,
4197                                     p_parent_line_id        => NULL,
4198                                     p_upd_qty_flag          => l_upd_qty_flag,
4199                                     x_return_status             => l_return_status,
4200                                     x_msg_count             => x_msg_count,
4201                                     x_msg_data              => x_msg_data );
4202 
4203                          v1.primary_quantity := 0;
4204 
4205                        ELSE
4206 
4207                          if (g_trace_on = 1) then log_event(' Prim qty '||v1.primary_quantity|| ' > '||space_avail_for); END IF;
4208 
4209                          tr_space_avail_for := inv_convert.inv_um_convert
4210                            ( v1.inventory_item_id,
4211                              5,
4212                              space_avail_for,
4213                              v_primary_uom_code,
4214                              v1.transaction_uom,
4215                              NULL,
4216                              null
4217                              );
4218                           --invconv kkillams
4219                          sec_tr_space_avail_for := NULL;
4220                          IF v1.secondary_uom_code IS NOT NULL THEN
4221                                  sec_tr_space_avail_for := inv_convert.inv_um_convert
4222                                    ( v1.inventory_item_id,
4223                                      5,
4224                                      space_avail_for,
4225                                      v_primary_uom_code,
4226                                      v1.secondary_uom_code,
4227                                      NULL,
4228                                      null
4229                                      );
4230                          END IF;
4231                          if (g_trace_on = 1) then log_event(' Tr space avail for '||tr_space_avail_for); END IF;
4232 
4233                          insert_mmtt
4234                            (        p_transaction_temp_id   => v1.transaction_temp_id,
4235                                     p_primary_quantity      => space_avail_for,
4236                                     p_transaction_quantity  => tr_space_avail_for,
4237                                     p_secondary_quantity    => sec_tr_space_avail_for, --invconv kkillams
4238                                     --p_LPN_string            => v_lpn_out,
4239                                     p_lpn_id                => v_lpn_id,
4240                                     p_container_item_id     => v_container_item_id,
4241                                     x_return_status      =>     l_return_status,
4242                                     x_msg_count             => x_msg_count,
4243                                     x_msg_data              => x_msg_data );
4244 
4245 
4246                          v1.primary_quantity := v1.primary_quantity -  space_avail_for;
4247 
4248                          v1.transaction_quantity := inv_convert.inv_um_convert
4249                            ( v1.inventory_item_id,
4250                              5,
4251                              v1.primary_quantity,
4252                              v_primary_uom_code,
4253                              v1.transaction_uom,
4254                              NULL,
4255                              null
4256                              );
4257                          --invconv kkillams
4258                          IF v1.secondary_uom_code IS NOT NULL THEN
4259                             v1.secondary_transaction_quantity
4260                             := inv_convert.inv_um_convert
4261                                    ( v1.inventory_item_id,
4262                                      5,
4263                                      v1.primary_quantity,
4264                                      v_primary_uom_code,
4265                                      v1.secondary_uom_code,
4266                                      NULL,
4267                                      null
4268                                      );
4269                          END IF;
4270 
4271                          space_avail_for := 0;
4272 
4273                          if (g_trace_on = 1) then
4274                             log_event('Prim qty '||v1.primary_quantity);
4275                             log_event(' Tr qty   '||v1.transaction_quantity);
4276                             log_event('Sec Tr qty   '||v1.secondary_transaction_quantity); --invconv kkillams
4277                             log_event(' Space Avail for '||space_avail_for);
4278                          END IF;
4279 
4280                       END IF;
4281 
4282 
4283                    END IF;
4284 
4285                 END IF;
4286 
4287 
4288                 /* Condition #3 */
4289                 IF ( v_return_status <> FND_API.g_ret_sts_success OR
4290                      v_qty_per_cont IS NULL                       OR
4291                      v_qty_per_cont <= 0                          OR
4292                      v_container_item_id IS NULL                  OR
4293                      v_tr_qty_per_cont  IS NULL                   OR
4294                      v_tr_qty_per_cont <= 0                       OR
4295                      v1.primary_quantity <= 0 ) THEN
4296 
4297                    if (g_trace_on = 1) then log_event(' Container_Required_Qty - inc values');  END IF;
4298                    /* Condition #3a */
4299                    NULL;
4300                  ELSE
4301                    /* Condition #3b */
4302 
4303                    IF( v1.content_lpn_id IS NULL) THEN
4304                       l_upd_qty_flag := 'Y';
4305                     ELSE
4306                       l_upd_qty_flag := 'N';
4307                    END IF;
4308 
4309                    v_qty := ceil( v1.primary_quantity/v_qty_per_cont);
4310 
4311                    IF( MOD(v1.primary_quantity,v_qty_per_cont) = 0 ) THEN
4312                       space_avail_for := 0;
4313                     else
4314                       space_avail_for :=  v_qty_per_cont -  MOD(v1.primary_quantity,v_qty_per_cont);
4315                    END IF;
4316 
4317                    if (g_trace_on = 1) then log_event('space avail for '||space_avail_for); END IF;
4318 
4319 
4320                    /* Condition #4 */
4321                    IF(  (v1.primary_quantity <= v_qty_per_cont) OR ( v_qty = 1) ) THEN
4322 
4323                       if (g_trace_on = 1) then log_event(' primary_quantity <= qty per conatiner or'||' NUMBER OF cont = 1'); END IF;
4324 
4325                       v_lpn_id := get_next_package_id;
4326                       if (g_trace_on = 1) then
4327                          log_event(' Generated label Id '||v_lpn_id);
4328                       END IF;
4329 
4330                          update_mmtt
4331                         (        p_transaction_temp_id => v1.transaction_temp_id,
4332                                  p_primary_quantity   => v1.primary_quantity,
4333                                  p_transaction_quantity  => v1.transaction_quantity,
4334                                  p_secondary_quantity    => v1.secondary_transaction_quantity, --invconv kkillams
4335                                  --p_LPN_string            => v_lpn_out,
4336                                  p_lpn_id                => v_lpn_id,
4337                                  p_container_item_id     => v_container_item_id,
4338                                  p_parent_line_id       => NULL,
4339                                  p_upd_qty_flag         => l_upd_qty_flag ,
4340                                  x_return_status              => l_return_status,
4341                                  x_msg_count             => x_msg_count,
4342                                  x_msg_data              => x_msg_data );
4343                     ELSE
4344                       /* Condition #4b */
4345 
4346                       v_loop := v_qty;
4347 
4348 
4349                      if (g_trace_on = 1) then log_event(' NUMBER OF cont:'||v_qty); END IF;
4350                      --Bug2422193 fix moved this update to above as package
4351                      --ids need TO be generated IN the ORDER IN which rows
4352                      --are considered for cartonization
4353                      v_lpn_id := get_next_package_id;
4354                      if (g_trace_on = 1) then
4355                         log_event(' Generated label Id '||v_lpn_id);
4356                         log_event('calling update_mmtt');
4357                      END IF;
4358 
4359                      update_mmtt
4360                        (        p_transaction_temp_id => v1.transaction_temp_id,
4361                                 p_primary_quantity   => v_qty_per_cont,
4362                                 p_transaction_quantity  => v_tr_qty_per_cont,
4363                                 p_secondary_quantity    => v_sec_tr_qty_per_cont, --invconv kkillams
4364                                 --p_LPN_string            => v_lpn_out,
4365                                 p_lpn_id                => v_lpn_id,
4366                                 p_container_item_id     => v_container_item_id,
4367                                 p_parent_line_id       => NULL,
4368                                 p_upd_qty_flag         => l_upd_qty_flag,
4369                                 x_return_status       => l_return_status,
4370                                 x_msg_count             => x_msg_count,
4371                                 x_msg_data              => x_msg_data );
4372 
4373 
4374 
4375                      v_loop := v_loop - 1;
4376 
4377                      --Bug2422193 fix
4378 
4379                      LOOP
4380 
4381                         EXIT WHEN v_loop < 2;
4382 
4383                         v_lpn_id := get_next_package_id;
4384                         if (g_trace_on = 1) then
4385                            log_event(' Generated label Id '||v_lpn_id);
4386                            log_event(' calling insert mmtt');
4387                          END IF;
4388                         insert_mmtt
4389                           (        p_transaction_temp_id => v1.transaction_temp_id,
4390                                    p_primary_quantity   => v_qty_per_cont,
4391                                    p_transaction_quantity  => v_tr_qty_per_cont,
4392                                    p_secondary_quantity    => v_sec_tr_qty_per_cont, --invconv kkillams
4393                                    --p_LPN_string            => v_lpn_out,
4394                                    p_lpn_id                => v_lpn_id,
4395                                    p_container_item_id     => v_container_item_id,
4396                                    x_return_status            => l_return_status,
4397                                    x_msg_count             => x_msg_count,
4398                                    x_msg_data              => x_msg_data );
4399 
4400                         if (g_trace_on = 1) then log_event(' called insert mmtt'); END IF;
4401                         v_loop := v_loop - 1;
4402 
4403 
4404                      END LOOP;
4405                      --Bug2422193 fix
4406                      --v_lpn_id := get_next_package_id;
4407                      --log_event(' Generated label Id '||v_lpn_id);
4408 
4409                      --log_event('calling update_mmtt');
4410 
4411                      --update_mmtt
4412                        --(        p_transaction_temp_id => v1.transaction_temp_id,
4413                                 --p_primary_quantity   => v_qty_per_cont,
4414                                 --p_transaction_quantity  => v_tr_qty_per_cont,
4415                                 --p_LPN_string            => v_lpn_out,
4416                                 --p_lpn_id                => v_lpn_id,
4417                                 --p_container_item_id     => v_container_item_id,
4418                                 --p_parent_line_id       => NULL,
4419                                 --p_upd_qty_flag         => l_upd_qty_flag,
4420                                 --x_return_status             => l_return_status,
4421                                 --x_msg_count             => x_msg_count,
4422                                 --x_msg_data              => x_msg_data );
4423 
4424 
4425                      --Bug2422193 fix
4426 
4427                      v_lpn_id := get_next_package_id;
4428                      if (g_trace_on = 1) then log_event(' Generated label Id '||v_lpn_id); END IF;
4429 
4430                      v_left_prim_quant :=  MOD(v1.primary_quantity,v_qty_per_cont);
4431                      v_left_tr_quant :=  MOD(v1.transaction_quantity,v_tr_qty_per_cont);
4432 
4433                      IF v1.secondary_uom_code IS NOT NULL THEN --invconv kkillams
4434                         v_sec_left_tr_quant :=  MOD(v1.secondary_transaction_quantity,v_sec_tr_qty_per_cont);
4435                      END IF;
4436 
4437                      IF(  v_left_prim_quant = 0 OR  v_left_tr_quant =0) THEN
4438                         v_left_prim_quant := v_qty_per_cont;
4439                         v_left_tr_quant   := v_tr_qty_per_cont;
4440                         IF v1.secondary_uom_code IS NOT NULL THEN --invconv kkillams
4441                            v_sec_left_tr_quant :=  v_sec_tr_qty_per_cont;
4442                         END IF;
4443                      END IF;
4444 
4445                      if (g_trace_on = 1) then log_event('calling insert mmtt'); END IF;
4446                      insert_mmtt
4447                        (        p_transaction_temp_id  => v1.transaction_temp_id,
4448                                 p_primary_quantity     => v_left_prim_quant,
4449                                 p_transaction_quantity => v_left_tr_quant,
4450                                 p_secondary_quantity    => v_sec_left_tr_quant, --invconv kkillams
4451                                 --p_LPN_string           => v_lpn_out,
4452                                 p_lpn_id                => v_lpn_id,
4453                                 p_container_item_id    => v_container_item_id,
4454                                 x_return_status       => l_return_status,
4455                                 x_msg_count            => x_msg_count,
4456                                 x_msg_data             => x_msg_data );
4457 
4458 
4459                      NULL;
4460                      -- Shipping API
4461 
4462                   END IF;
4463                   /* Close Condition #4 */
4464                 END IF;
4465                /* Close Condition #3 */
4466              END IF;
4467             /** for v1.allocated_lpn_id is not null*/
4468           END LOOP;
4469 
4470           if (g_trace_on = 1) then log_event(' end working with wms_container_pub.container_required_qty  api ' );  END IF;
4471 
4472           error_code := 'CARTONIZE 220';
4473 
4474           if (g_trace_on = 1) then log_event(' Calling item-category cartonization'); END IF;
4475 
4476           IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
4477 
4478 
4479              ret_value:= do_cartonization(p_move_order_header_id,0,outbound,
4480                                           v_sublvlctrl,percent_fill_basis);
4481 
4482 
4483            ELSE
4484              if (g_trace_on = 1) then
4485                 log_event('in else for cartonization');
4486                 log_event('passing header id '||l_current_header_id);
4487                 log_event('passing outbound '||outbound);
4488                 log_event('passing Sub level Control'||v_sublvlctrl);
4489              END IF;
4490                 ret_value:= do_cartonization(0,l_current_header_id,outbound,
4491                                              v_sublvlctrl,percent_fill_basis);
4492 
4493           END IF;
4494 
4495           if (g_trace_on = 1) then
4496              log_event(' cartonization returned'|| ret_value);
4497              log_event(' calling split_lot_serials ');
4498           END IF;
4499 
4500           split_lot_serials(p_org_id);
4501 
4502           if (g_trace_on = 1) then log_event(' Populating Packaging History Table'); END IF;
4503 
4504           l_prev_package_id := -1;
4505 
4506 
4507           l_prev_header_id := l_current_header_id;
4508 
4509           if (g_trace_on = 1) then log_event(' prev header id '||l_prev_header_id ); END IF;
4510 
4511           l_current_header_id := get_next_header_id;
4512 
4513           if (g_trace_on = 1) then log_event(' current_header_id '||l_current_header_id); END IF;
4514 
4515           error_code := 'CARTONIZE 225';
4516           t_lpn_alloc_flag_table.delete;
4517           error_code := 'CARTONIZE 226';
4518           IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
4519              error_code := 'CARTONIZE 227';
4520              OPEN opackages(l_prev_header_id);
4521            ELSE
4522              error_code := 'CARTONIZE 228';
4523              OPEN packages(l_prev_header_id);
4524           END IF;
4525 
4526 
4527           l_no_pkgs_gen := 'Y';
4528 
4529           error_code := 'CARTONIZE 230';
4530 
4531           LOOP
4532 
4533              if (g_trace_on = 1) then log_event('Fetching Packages cursor '); END IF;
4534 
4535              error_code := 'CARTONIZE 240';
4536 
4537              IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
4538                 FETCH opackages INTO l_temp_id,l_item_id, l_qty, l_tr_qty,l_sec_tr_qty, l_clpn_id, l_citem_id, l_package_id;
4539                 EXIT WHEN opackages%notfound;
4540               ELSE
4541                    FETCH packages INTO l_temp_id,l_item_id, l_qty, l_tr_qty, l_sec_tr_qty, l_clpn_id, l_citem_id, l_package_id;
4542                    EXIT WHEN packages%notfound;
4543              END IF;
4544 
4545 
4546 
4547              if (g_trace_on = 1) then
4548                 log_event('temp_id '||l_temp_id );
4549                 log_event('item_id  '||l_item_id );
4550                 log_event('qty  '||l_qty );
4551                 log_event('tr_qty '||l_tr_qty);
4552                 log_event('sec_tr_qty '||l_sec_tr_qty);
4553                 log_event('clpn_id '||l_clpn_id);
4554                 log_event('citem_id '||l_citem_id);
4555                 log_event('package_id '||l_package_id);
4556              END IF;
4557              if( l_package_id is not null ) THEN
4558 
4559                 l_no_pkgs_gen := 'N';
4560                 if( l_package_id <> l_prev_package_id ) then
4561 
4562                    l_prev_package_id := l_package_id;
4563 
4564                    if (g_trace_on = 1) then log_event(' Inserting a new row for package '||l_package_id); END IF;
4565 
4566 
4567                    insert_mmtt
4568                      (    p_transaction_temp_id  => l_temp_id,
4569                           p_primary_quantity     => l_qty,
4570                           p_transaction_quantity => l_tr_qty,
4571                           p_secondary_quantity   => l_sec_tr_qty, --invconv kkillams
4572                           p_new_txn_hdr_id    => l_current_header_id,
4573                           p_new_txn_tmp_id      => get_next_temp_id,
4574                           p_clpn_id              => l_package_id,
4575                           p_item_id             => l_citem_id,
4576                           x_return_status          => l_return_status,
4577                           x_msg_count            => x_msg_count,
4578                           x_msg_data             => x_msg_data );
4579 
4580 
4581 
4582                 end if;
4583 
4584                 if (g_trace_on = 1) then log_event(' Calling InsertPH for temp_id'||l_temp_id); END IF;
4585                 IF( outbound = 'Y' ) then
4586                    Insert_PH(p_move_order_header_id, l_temp_id);
4587                  ELSE
4588                    Insert_PH(p_transaction_header_id, l_temp_id);
4589                 END IF;
4590              END IF;
4591 
4592 
4593           END LOOP;
4594 
4595           IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
4596              IF opackages%isopen THEN
4597                 CLOSE opackages;
4598              END IF;
4599            ELSE
4600              IF packages%isopen THEN
4601                 CLOSE packages;
4602              END IF;
4603           END IF;
4604 
4605 
4606           IF( l_no_pkgs_gen = 'Y' ) THEN
4607              if (g_trace_on = 1) then log_event('no labels generated in the previous level-EXITING'); END IF;
4608 
4609              IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
4610 
4611                 IF( wct_rows%isopen) then
4612                    CLOSE wct_rows;
4613                 END IF;
4614 
4615 
4616               ELSE
4617 
4618                 IF(bpack_rows%isopen) then
4619                    CLOSE bpack_rows;
4620                 END IF;
4621 
4622 
4623              END IF;
4624 
4625              EXIT;
4626           END IF;
4627 
4628 
4629 
4630 
4631           IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
4632              IF wct_rows%isopen THEN
4633                 CLOSE wct_rows;
4634              END IF;
4635 
4636            ELSE
4637              IF(bpack_rows%isopen) then
4638                 CLOSE bpack_rows;
4639              END IF;
4640 
4641           END IF;
4642 
4643 
4644           pack_level := pack_level + 1;
4645           if (g_trace_on = 1) then
4646              log_event(' Incremented the current level');
4647              log_event(' going back to the multi-cart loop');
4648           END IF;
4649 
4650        END LOOP; -- Ends the loop for multi level cartonization
4651 
4652 
4653        IF ( outbound = 'Y') THEN
4654           l_header_id := p_move_order_header_id;
4655         ELSE
4656           l_header_id := p_transaction_header_id;
4657        END IF;
4658 
4659        -- We have to update the end labels to LPNS and update the
4660        -- packaging history and mmtt correspondingly
4661 
4662        if (g_trace_on = 1) then log_event(' calling Generate_LPNs for header id '||l_header_id); END IF;
4663 
4664        BEGIN
4665           error_code := 'CARTONIZE 250';
4666 
4667           generate_lpns(p_header_id => l_header_id,
4668                         p_organization_id => p_org_id);
4669        EXCEPTION
4670           WHEN OTHERS THEN
4671              IF( packaging_mode IN (wms_cartnzn_pub.int_bp_pkg_mode,
4672                                       wms_cartnzn_pub.mob_bP_pkg_mode ,
4673                                       wms_cartnzn_pub.prepack_pkg_mode) ) THEN
4674                 if (g_trace_on = 1) then log_event('erroring out since the mode is bulk pack ');  END IF;
4675                 RAISE fnd_api.g_exc_unexpected_error;
4676               ELSE
4677                 if (g_trace_on = 1) then log_event('not erroring out since the mode is Pick release ');  END IF;
4678                 RAISE fnd_api.g_exc_error;
4679              END IF;
4680        END ;
4681 
4682 
4683        DELETE wms_cartonization_temp
4684          WHERE
4685          transaction_header_id < 0;
4686 
4687      ELSE
4688                    -- Cartonization profile = 'N'
4689                    null;
4690     END IF;
4691 
4692     IF( outbound = 'Y') THEN
4693 
4694        IF l_auto_pick_confirm_flag = 'Y' THEN
4695           if (g_trace_on = 1) then log_event('Auto pick confirm is ON - Not calling task consolidation - splitting'); END IF;
4696 
4697         ELSE
4698 
4699           if (g_trace_on = 1) then log_event(' calling consolidation' );  END IF;
4700 
4701           -- patchset J bulk picking   -----------------------------------
4702           if G_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL
4703              AND l_move_order_type = g_move_order_pick_wave THEN
4704                   if (g_trace_on = 1)  then -- log_event('PATCHSET J-- BULK PICKING --START');
4705                                            log_event('Calling consolidate_bulk_tasks_for_so....');
4706                                            log_event('move order header id '||p_move_order_header_id); end if;
4707                   wms_task_dispatch_engine.consolidate_bulk_tasks_for_so
4708                                           (p_api_version => 1.0,
4709                                            x_return_status => l_return_status,
4710                                            x_msg_count => x_msg_count,
4711                                            x_msg_data => x_msg_data,
4712                                            p_move_order_header_id => p_move_order_header_id);
4713                    IF(l_return_status <> fnd_api.g_ret_sts_success ) THEN
4714                         -- we don't want to exit if there is any error here, for example data error or something
4715                         if (g_trace_on = 1) then log_event(' consolidate_bulk_tasks_for_so returns '||l_return_status); END IF;
4716                    END IF;
4717                    if (g_trace_on = 1) then log_event(' calling ins_wct_rows_into_mmtt after consolidation'); END IF;
4718 
4719                    ins_wct_rows_into_mmtt( p_m_o_h_id           => p_move_order_header_id,
4720                                            p_outbound           => 'Y',
4721                                            x_return_status      => l_return_status,
4722                                            x_msg_count          => x_msg_count,
4723                                            x_msg_data           => x_msg_data);
4724                    -- since the return value is not set up inside above procedure, so no need to check it
4725                    -- but above procedure will throw unexpected error in case of errors, so this API will capture the
4726                    -- error.
4727 
4728                    WMS_CARTNZN_PUB.assigntts(
4729                                                   p_api_version => 1.0,
4730                                                   p_move_order_header_id =>  p_move_order_header_id,
4731                                                   x_return_status    => l_return_status,
4732                                                   x_msg_count        => l_msg_count,
4733                                                   x_msg_data         => l_msg_data);
4734 
4735                   IF l_return_status <> fnd_api.g_ret_sts_success THEN
4736                        error_code := 'CARTONIZE 40.1';
4737                        if (g_trace_on = 1) then log_event(' Task type assignment failed'); END IF;
4738                   END IF;
4739                  -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING --END'); end if;
4740          ELSE
4741              if (g_trace_on = 1) then log_event('Calling consolidate_bulk_tasks...');  end if;
4742              wms_task_dispatch_engine.consolidate_bulk_tasks
4743                                           (p_api_version => 1.0,
4744                                            x_return_status => l_return_status,
4745                                            x_msg_count => x_msg_count,
4746                                            x_msg_data => x_msg_data,
4747                                            p_move_order_header_id => p_move_order_header_id);
4748          END IF;
4749          ----- end of changed for patchset J bulk picking -----------
4750 
4751 
4752          IF(l_return_status <> fnd_api.g_ret_sts_success ) THEN
4753                    --Push the message into the stack
4754               if (g_trace_on = 1) then log_event(' consolidate_bulk_tasks returns '||l_return_status); END IF;
4755          END IF;
4756 
4757           if (g_trace_on = 1) then log_event(' calling task splitting' );  END IF;
4758           wms_task_dispatch_engine.split_tasks
4759             (p_api_version => 1.0,
4760              x_return_status => l_return_status,
4761              x_msg_count => x_msg_count,
4762              x_msg_data => x_msg_data,
4763              p_move_order_header_id => p_move_order_header_id);
4764 
4765           IF(l_return_status <> fnd_api.g_ret_sts_success ) THEN
4766              --Push the message into the stack
4767              if (g_trace_on = 1) then log_event(' split_tasks returns '||l_return_status); END IF;
4768           END IF;
4769 
4770           -- patchset J bulk picking -------------------
4771           IF (G_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL
4772            AND l_move_order_type =g_move_order_pick_wave AND
4773            p_move_order_header_id = -1 ) THEN    -- calling from concurrent program
4774                if (g_trace_on = 1) then  -- log_event('PATCHSET J-- BULK PICKING --START');
4775                                         log_event('calling assign pick slip number...'); end if;
4776                INV_Pick_Release_PUB.assign_pick_slip_number(
4777                                     x_return_status         => l_return_status,
4778                                     x_msg_count             => x_msg_count,
4779                                     x_msg_data              => x_msg_data,
4780                                     p_move_order_header_id  => p_move_order_header_id,
4781                                     p_ps_mode            => null,
4782                                     p_grouping_rule_id  => null,
4783                                     p_allow_partial_pick => null);
4784                   IF(l_return_status <> fnd_api.g_ret_sts_success ) THEN
4785                      --Push the message into the stack
4786                      if (g_trace_on = 1) then log_event(' assign_pick_slip_number returns '||l_return_status); END IF;
4787                   END IF;
4788            END IF;
4789 
4790        END IF;
4791 
4792     END IF;
4793 
4794 
4795 
4796     if (g_trace_on = 1) then log_event(' calling ins_wct_rows_into_mmtt'); END IF;
4797 
4798 
4799     -- patchset J bulk picking   -------------------
4800     IF( outbound= 'Y' ) then
4801        if (G_CURRENT_RELEASE_LEVEL < G_J_RELEASE_LEVEL) or
4802           (G_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL
4803            AND l_move_order_type <>g_move_order_pick_wave ) THEN
4804           ins_wct_rows_into_mmtt( p_m_o_h_id           => p_move_order_header_id,
4805                                p_outbound           => 'Y',
4806                                x_return_status      => l_return_status,
4807                                x_msg_count              => x_msg_count,
4808                                x_msg_data               => x_msg_data);
4809         end if;
4810 
4811      ELSE
4812        ins_wct_rows_into_mmtt( p_m_o_h_id           => p_transaction_header_id,
4813                                p_outbound           => 'N',
4814                                x_return_status      => l_return_status,
4815                                x_msg_count              => x_msg_count,
4816                                x_msg_data               => x_msg_data);
4817     END IF;
4818 
4819 
4820     error_code := 'CARTONIZE 260';
4821     if (g_trace_on = 1) then log_event(' calling print label' ); END IF;
4822     l_counter := 1;
4823 
4824 
4825 
4826     IF( outbound = 'Y') THEN
4827 
4828        OPEN OB_LBPRT;
4829        loop
4830           FETCH OB_LBPRT INTO lpns_generated_tb(l_counter);
4831           EXIT WHEN OB_LBPRT%notfound;
4832 
4833           if (g_trace_on = 1) then log_event(' print label for lpn '|| lpns_generated_tb(l_counter)); END IF;
4834           l_counter := l_counter + 1;
4835        END LOOP;
4836        IF OB_LBPRT%ISOPEN THEN
4837           CLOSE OB_LBPRT;
4838        END IF;
4839 
4840      ELSE
4841              OPEN IB_LBPRT;
4842              LOOP
4843                 FETCH IB_LBPRT INTO lpns_generated_tb(l_counter);
4844                 EXIT WHEN IB_LBPRT%notfound;
4845 
4846                 if (g_trace_on = 1) then log_event(' print label for lpn '|| lpns_generated_tb(l_counter)); END IF;
4847                 l_counter := l_counter + 1;
4848              END LOOP;
4849              IF IB_LBPRT%ISOPEN THEN
4850                 CLOSE IB_LBPRT;
4851              END IF;
4852     END IF;
4853 
4854 
4855     IF --(l_lpn_id_tbl IS NOT NULL) AND bug 2720653 fix
4856       (lpns_generated_tb.count > 0) THEN
4857 
4858        IF( packaging_mode = wms_cartnzn_pub.PR_pKG_mode OR
4859         packaging_mode = wms_cartnzn_pub.mfg_PR_pKG_mode) THEN
4860           if (g_trace_on = 1) then log_event('wms_cartnzn_pub before  inv_label.print_label '); END IF;
4861 
4862           l_return_status := fnd_api.g_ret_sts_success;
4863 
4864           inv_label.print_label
4865             (x_return_status => l_return_status
4866              , x_msg_count => l_msg_count
4867              , x_msg_data  => l_msg_data
4868              , x_label_status  => l_label_status
4869              , p_api_version   => 1.0
4870              , p_print_mode => 1
4871              , p_business_flow_code => 22
4872              , p_transaction_id => lpns_generated_tb
4873              );
4874           if (g_trace_on = 1) then log_event('wms_cartnzn_pub after inv_label.print_label ');  END IF;
4875 
4876           IF l_return_status <> fnd_api.g_ret_sts_success THEN
4877              FND_MESSAGE.SET_NAME('INV', 'INV_RCV_CRT_PRINT_LAB_FAIL');  -- MSGTBD
4878              FND_MSG_PUB.ADD;
4879 
4880              --IF( packaging_mode IN (2,3,4) ) THEN
4881              --x_return_status := l_return_status;
4882              --x_msg_data := l_msg_data;
4883              --x_msg_count := l_msg_count;
4884              --END IF;
4885 
4886              if (g_trace_on = 1) then log_event('wms_cartnzn_pub inv_label.print_label FAILED;'|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS')); END IF;
4887           END IF;
4888        END IF; -- end packaging_mode in 1
4889 
4890      ELSE
4891        if (g_trace_on = 1) then log_event('could not cartonize any of the lines'); END IF;
4892 
4893        IF( packaging_mode IN (wms_cartnzn_pub.int_bp_pkg_mode,
4894                               wms_cartnzn_pub.mob_bP_pkg_mode ,
4895                               wms_cartnzn_pub.prepack_pkg_mode)) THEN
4896          if (g_trace_on = 1) then log_event('erroring out since the mode is bulk pack '); END IF;
4897           x_return_status := fnd_api.g_ret_sts_error;
4898        END IF;
4899 
4900     END IF;
4901 
4902     if (g_trace_on = 1) then log_event('return status is '||x_return_status);  END IF;
4903 
4904     IF wct_rows%isopen THEN
4905        CLOSE wct_rows;
4906     END IF;
4907 
4908 
4909  EXCEPTION
4910 
4911     WHEN fnd_api.g_exc_error THEN
4912        if (g_trace_on = 1) then log_event('Rolling back to savepoint cartonize_pub'); END IF;
4913        ROLLBACK TO  cartonize_pub;
4914        x_return_status := fnd_api.g_ret_sts_success;
4915 
4916        FND_MESSAGE.SET_NAME('WMS', 'WMS_CARTONIZATION_ERROR');
4917        FND_MESSAGE.SET_TOKEN('ERROR_CODE', ERROR_CODE);
4918        FND_MSG_PUB.ADD;
4919 
4920        if (g_trace_on = 1) then log_event('EXCEPTION occurred from ERROR_CODE:'||error_code); END IF;
4921 
4922        fnd_msg_pub.count_and_get
4923          (
4924            p_count  => x_msg_count,
4925            p_data   => x_msg_data
4926            );
4927 
4928        --      IF (x_msg_count = 0) THEN
4929        --        dbms_output.put_line('Successful');
4930        --       ELSIF (x_msg_count = 1) THEN
4931        --        dbms_output.put_line ('Not Successful');
4932        --        dbms_output.put_line (replace(x_msg_data,chr(0),' '));
4933        --       ELSE
4934        --        dbms_output.put_line ('Not Successful2');
4935        --        For I in 1..x_msg_count LOOP
4936        --           x_msg_data := fnd_msg_pub.get(I,'F');
4937        --           dbms_output.put_line(replace(x_msg_data,chr(0),' '));
4938        --        END LOOP;
4939        --      END IF;
4940 
4941        IF wct_rows%isopen THEN
4942           CLOSE wct_rows;
4943        END IF;
4944 
4945     WHEN fnd_api.g_exc_unexpected_error THEN
4946        if (g_trace_on = 1) then log_event('Rolling back to savepoint cartonize_pub'); END IF;
4947        ROLLBACK TO  cartonize_pub;
4948        x_return_status := fnd_api.g_ret_sts_unexp_error;
4949 
4950        FND_MESSAGE.SET_NAME('WMS', 'WMS_CARTONIZATION_ERROR');
4951        FND_MESSAGE.SET_TOKEN('ERROR_CODE', ERROR_CODE);
4952        FND_MSG_PUB.ADD;
4953        if (g_trace_on = 1) then log_event('Exception occurred from ERROR_CODE:'||error_code); END IF;
4954        fnd_msg_pub.count_and_get
4955          (
4956            p_count  => x_msg_count,
4957            p_data   => x_msg_data
4958            );
4959 
4960        IF wct_rows%isopen THEN
4961           CLOSE wct_rows;
4962        END IF;
4963 
4964     WHEN OTHERS  THEN
4965        ROLLBACK TO  cartonize_pub;
4966        if (g_trace_on = 1) then log_event('Rolling back to savepoint cartonize_pub'); END IF;
4967        x_return_status := fnd_api.g_ret_sts_unexp_error;
4968 
4969        ERROR_MSG := Sqlerrm;
4970 
4971        FND_MESSAGE.SET_NAME('WMS', 'WMS_CARTONIZATION_ERROR');
4972        FND_MESSAGE.SET_TOKEN('ERROR_CODE', ERROR_CODE);
4973        FND_MSG_PUB.ADD;
4974 
4975        if (g_trace_on = 1) then log_event('Exception occurred from ERROR_CODE:'||error_code); END IF;
4976 
4977        IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error ) THEN
4978 
4979           fnd_msg_pub.add_exc_msg
4980             ( g_pkg_name,
4981               l_api_name,
4982               ERROR_MSG
4983               );
4984        END IF;
4985 
4986       fnd_msg_pub.count_and_get
4987         (
4988           p_count  => x_msg_count,
4989           p_data   => x_msg_data
4990           );
4991 
4992       IF wct_rows%isopen THEN
4993          CLOSE wct_rows;
4994       END IF;
4995 
4996  END cartonize;
4997 
4998 
4999  PROCEDURE UPDATE_MMTT(
5000                         p_transaction_temp_id   IN        NUMBER,
5001                         p_primary_quantity        IN      NUMBER,
5002                         p_transaction_quantity    IN      NUMBER,
5003                         p_secondary_quantity      IN      NUMBER, --invconv kkillams
5004                         p_LPN_string              IN      VARCHAR2,
5005                         p_lpn_id                  IN      NUMBER,
5006                         p_container_item_id       IN      NUMBER,
5007                         p_parent_line_id          IN      NUMBER,
5008                         p_upd_qty_flag            IN      VARCHAR2,
5009                         x_return_status         OUT       NOCOPY VARCHAR2,
5010                         x_msg_count             OUT       NOCOPY NUMBER,
5011                         x_msg_data              OUT       NOCOPY VARCHAR2)
5012    IS
5013 
5014       l_lpn NUMBER := 0;
5015       par_line_id NUMBER := NULL;
5016 
5017  BEGIN
5018 
5019     error_code := 'UPD_MMTT';
5020 
5021     if (g_trace_on = 1) then log_event(' In update mmtt '||p_upd_qty_flag ); END IF;
5022 
5023     IF p_lpn_id IS NOT NULL THEN
5024 
5025        l_lpn := p_lpn_id;
5026 
5027      elsIF p_lpn_string IS NOT NULL then
5028 
5029        SELECT lpn_id INTO l_lpn FROM wms_license_plate_numbers WHERE
5030          license_plate_number = p_lpn_string;
5031      ELSE
5032 
5033        l_lpn := NULL;
5034     END IF;
5035 
5036 
5037     if (g_trace_on = 1) then log_event('l_lpn '||l_lpn); END IF;
5038 
5039 
5040 
5041     IF p_primary_quantity IS NULL THEN
5042        NULL;
5043        --GET PRIMARY QUANTITY
5044     END IF;
5045 
5046 
5047     IF table_name = 'mtl_material_transactions_temp' THEN
5048 
5049        if (g_trace_on = 1) then log_event(' table_name = mtl_material_transactions_temp'); END IF;
5050 
5051 
5052        IF p_parent_line_id = -99999 THEN
5053           SELECT parent_line_id INTO par_line_id FROM
5054             mtl_material_transactions_temp WHERE transaction_temp_id =
5055             p_transaction_temp_id;
5056        ELSE
5057           par_line_id := p_parent_line_id;
5058        END IF;
5059 
5060 
5061        if (g_trace_on = 1) then log_event(' upd qty flag is '||p_upd_qty_flag ); END IF;
5062 
5063       IF( p_upd_qty_flag = 'Y') THEN
5064 
5065 
5066          UPDATE mtl_material_transactions_temp SET
5067            primary_quantity = p_primary_quantity,
5068            transaction_quantity = p_transaction_quantity,
5069            secondary_transaction_quantity = p_secondary_quantity, --invconv kkillams
5070            cartonization_id = l_LPN,
5071            container_item_id = p_container_item_id,
5072            parent_line_id = par_line_id,
5073            last_update_date = Sysdate,
5074            last_updated_by = fnd_global.user_id
5075            WHERE
5076            transaction_temp_id = p_transaction_temp_id;
5077        ELSE
5078          UPDATE mtl_material_transactions_temp SET
5079            cartonization_id = l_LPN,
5080            container_item_id = p_container_item_id,
5081            parent_line_id = par_line_id,
5082            last_update_date = Sysdate,
5083            last_updated_by = fnd_global.user_id
5084            WHERE
5085            transaction_temp_id = p_transaction_temp_id;
5086       END IF;
5087 
5088 
5089      ELSE
5090 
5091       if (g_trace_on = 1) then log_event(' table_name = wms_cartonization_temp');  END IF;
5092 
5093       IF p_parent_line_id = -99999 THEN
5094 
5095          SELECT parent_line_id INTO par_line_id FROM
5096            wms_cartonization_temp WHERE transaction_temp_id =
5097            p_transaction_temp_id;
5098        ELSE
5099          par_line_id := p_parent_line_id;
5100       END IF;
5101 
5102       if (g_trace_on = 1) then log_event(' upd qty flag is '||p_upd_qty_flag ); END IF;
5103 
5104       IF( p_upd_qty_flag = 'Y') THEN
5105 
5106          UPDATE wms_cartonization_temp SET
5107            primary_quantity = p_primary_quantity,
5108            transaction_quantity = p_transaction_quantity,
5109            secondary_transaction_quantity = p_secondary_quantity, --invconv kkillams
5110            cartonization_id = l_LPN,
5111            parent_line_id = par_line_id,
5112            container_item_id = p_container_item_id,
5113            last_update_date = Sysdate,
5114            last_updated_by = fnd_global.user_id
5115            WHERE
5116            transaction_temp_id = p_transaction_temp_id;
5117        ELSE
5118          UPDATE wms_cartonization_temp SET
5119            cartonization_id = l_LPN,
5120            parent_line_id = par_line_id,
5121            container_item_id = p_container_item_id,
5122            last_update_date = Sysdate,
5123            last_updated_by = fnd_global.user_id
5124            WHERE
5125            transaction_temp_id = p_transaction_temp_id;
5126       END IF;
5127 
5128 
5129 
5130     END IF;
5131 
5132 
5133 
5134  END update_mmtt;
5135 
5136 -- Added for bug 8574452 FPing 8431970
5137 PROCEDURE update_mmtt_qty(
5138 			p_transaction_temp_id  	IN  	  NUMBER,
5139 			p_primary_quantity        IN      NUMBER,
5140 			p_transaction_quantity    IN      NUMBER,
5141 			x_return_status	        OUT 	  NOCOPY VARCHAR2,
5142 			x_msg_count       	OUT 	  NOCOPY NUMBER,
5143 			x_msg_data        	OUT 	  NOCOPY VARCHAR2)
5144    IS
5145 
5146 
5147  BEGIN
5148 
5149     error_code := 'UPD_MMTT_QTY';
5150 
5151    IF table_name = 'wms_cartonization_temp' THEN
5152 
5153       if (g_trace_on = 1) then log_event(' table_name = wms_cartonization_temp');  END IF;
5154 
5155       	 UPDATE wms_cartonization_temp SET
5156 	   primary_quantity = primary_quantity - NVL(p_primary_quantity,0),
5157 	   transaction_quantity = transaction_quantity - NVL(p_transaction_quantity,0),
5158 	   last_update_date = Sysdate,
5159 	   last_updated_by = fnd_global.user_id
5160 	   WHERE
5161 	   transaction_temp_id = p_transaction_temp_id;
5162 
5163 
5164     END IF;
5165 
5166 
5167 
5168  END update_mmtt_qty;
5169 
5170 
5171  PROCEDURE INSERT_MMTT(
5172                         p_transaction_temp_id   IN      NUMBER,
5173                         p_primary_quantity      IN      NUMBER,
5174                         p_transaction_quantity  IN      NUMBER,
5175                         p_secondary_quantity    IN      NUMBER , --invconv kkillams
5176                         p_LPN_string            IN      VARCHAR2,
5177                         p_lpn_id                IN      NUMBER,
5178                         p_container_item_id     IN      NUMBER,
5179                         p_new_txn_hdr_id        IN      NUMBER,
5180                         p_new_txn_tmp_id        IN      NUMBER,
5181                         p_clpn_id               IN      NUMBER,
5182                         p_item_id               IN      NUMBER,
5183                         x_return_status         OUT     NOCOPY VARCHAR2,
5184                         x_msg_count             OUT     NOCOPY NUMBER,
5185                         x_msg_data              OUT     NOCOPY VARCHAR2 )
5186    IS
5187 
5188       v1 WCT_ROW_TYPE;
5189       cnt NUMBER;
5190 
5191       l_lpn NUMBER;
5192  BEGIN
5193     error_code := 'INS_MMTT';
5194     if (g_trace_on = 1) then log_event(' In insert mmtt with temp_id '||p_transaction_temp_id ); END IF;
5195 
5196     IF p_lpn_id IS NOT NULL THEN
5197        l_lpn := p_lpn_id;
5198 
5199      elsIF p_lpn_string IS NOT NULL THEN
5200 
5201        SELECT lpn_id INTO L_lpn FROM wms_license_plate_numbers WHERE
5202          license_plate_number = p_lpn_string;
5203 
5204      ELSE
5205 
5206        l_lpn := NULL;
5207 
5208     END IF;
5209 
5210     if (g_trace_on = 1) then
5211        log_event(' lpn id IS '||l_lpn);
5212        log_event(' table name is '||table_name);
5213     END IF;
5214 
5215     IF( table_name = 'wms_cartonization_temp' ) THEN
5216 
5217 
5218        SELECT *  INTO  v1 FROM wms_cartonization_temp WHERE
5219          transaction_temp_id = p_transaction_temp_id AND ROWNUM < 2;
5220 
5221      ELSE
5222        --Bug 3296177 code should never come here
5223        if (g_trace_on = 1) THEN  log_event('ERROR:INS_MMTT with mtl_material_transactions_temp'); end if;
5224        RAISE fnd_api.g_exc_error;
5225        --SELECT *  INTO  v1 FROM mtl_material_transactions_temp WHERE
5226        --transaction_temp_id = p_transaction_temp_id;
5227 
5228     END IF;
5229 
5230     if (g_trace_on = 1) then log_event(' v1.inventory_item_id is '||v1.inventory_item_id); END IF;
5231 
5232     v1.primary_quantity := p_primary_quantity;
5233     v1.transaction_quantity := p_transaction_quantity;
5234     v1.secondary_transaction_quantity :=  p_secondary_quantity; --invconv kkillams
5235     v1.cartonization_id := l_LPN;
5236     v1.container_item_id := p_container_item_id;
5237     v1.last_update_date := Sysdate;
5238     v1.last_updated_by := fnd_global.user_id;
5239     v1.creation_date := Sysdate;
5240     v1.created_by := fnd_global.user_id;
5241 
5242 
5243     IF p_new_txn_hdr_id IS NOT NULL THEN
5244       v1.transaction_header_id := p_new_txn_hdr_id;
5245     END IF;
5246 
5247     IF ( p_new_txn_tmp_id IS NULL AND p_transaction_temp_id > 0 ) THEN
5248 
5249        SELECT mtl_material_transactions_s.NEXTVAL
5250          INTO v1.transaction_temp_id
5251          FROM dual;
5252 
5253      ELSE
5254 
5255        v1.transaction_temp_id := p_new_txn_tmp_id;
5256 
5257     END IF;
5258 
5259    IF( p_clpn_id IS NOT NULL) THEN
5260       v1.content_lpn_id := p_clpn_id;
5261    END IF;
5262 
5263 
5264    IF ( p_transaction_temp_id > 0 AND v1.transaction_temp_id > 0 ) then
5265       temp_id_table_index := temp_id_table_index + 1;
5266       temp_id_table(temp_id_table_index).orig_temp_id := p_transaction_temp_id;
5267       temp_id_table(temp_id_table_index).splt_temp_id :=  v1.transaction_temp_id;
5268       temp_id_table(temp_id_table_index).primary_quantity :=  v1.primary_quantity;
5269       temp_id_table(temp_id_table_index).secondary_quantity :=  v1.secondary_transaction_quantity;
5270    END IF;
5271 
5272 
5273     IF p_item_id IS NOT NULL THEN
5274       v1.inventory_item_id := p_item_id;
5275     END IF;
5276 
5277 
5278    IF (table_name = 'mtl_material_transactions_temp') THEN
5279 
5280       RAISE fnd_api.g_exc_error;
5281       --wms_task_dispatch_engine.insert_mmtt
5282         --(l_mmtt_rec => v1);
5283 
5284     ELSE
5285 
5286       wms_task_dispatch_engine.insert_wct
5287         (l_wct_rec => v1);
5288 
5289    END IF;
5290 
5291 
5292 
5293 
5294 
5295  END insert_mmtt;
5296 
5297 
5298 
5299 
5300 
5301 
5302 
5303  PROCEDURE log_event(
5304                       p_message VARCHAR2)
5305   IS
5306 
5307      l_module VARCHAR2(255);
5308      l_mesg VARCHAR2(255);
5309     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
5310  BEGIN
5311 
5312    l_module := 'wms.plsql.' || 'wms_cartnzn_pub' || '.' || 'cartonization';
5313 
5314    --inv_pick_wave_pick_confirm_pub.TraceLog(err_msg => p_message,
5315    --                                      module => 'WMS_CARTNZN_PUB'
5316    --                                      );
5317 
5318 
5319    l_mesg := to_char(sysdate, 'YYYY-MM-DD HH:DD:SS') || p_message;
5320 
5321    IF (l_debug = 1) THEN
5322       -- dbms_output.put_line(l_mesg);
5323       inv_trx_util_pub.trace(l_mesg, 'WMS_CARTNZN_PUB');
5324    END IF;
5325 
5326 
5327  END log_event;
5328 
5329 
5330 
5331 
5332 
5333  PROCEDURE test IS
5334 
5335     x_return_status VARCHAR2(100);
5336     x_msg_count NUMBER;
5337     x_msg_data VARCHAR2(300);
5338     mlid       NUMBER;
5339     v_qty_per_cont  NUMBER;
5340     v_container_item_id  NUMBER;
5341    v_return_status  VARCHAR2(300);
5342     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
5343  BEGIN
5344     /*mlid := 1;
5345     table_name := 'WMS_CARTONIZATION_TEMP';
5346       mlid := do_cartonization(mohdrid =>5388, trxhdrid =>3243813, outbound => 'N', sublvlctrl =>'1');
5347 
5348      dbms_output.put_line(mlid);*/
5349 
5350 
5351       cartonize( p_api_version => 1.0,
5352                  p_commit =>fnd_api.g_false,
5353                  p_out_bound => 'N',
5354                  p_org_id => 1884,
5355                  x_return_status => x_return_status,
5356                  x_msg_count     => x_msg_count,
5357                  x_msg_data      => x_msg_data,
5358                  --p_move_order_header_id => 5388,
5359                  p_transaction_header_id => 3243813,
5360                  --p_stop_level           => 2,
5361               p_packaging_mode       => 3
5362                  );
5363 
5364     /*
5365     ins_wct_rows_into_mmtt(
5366       p_m_o_h_id    => 40262,
5367       x_return_status=> x_return_status,
5368       x_msg_count => x_msg_count,
5369       x_msg_data  => x_msg_data     );
5370 
5371       SELECT line_id INTO mlid FROM mtl_txn_request_lines WHERE header_id = 40262
5372       AND roWnum < 2;
5373 
5374       wsh_interface.Get_Max_Load_Qty(p_move_order_line_id => mlid,
5375       x_max_load_quantity  => v_qty_per_cont,
5376       x_container_item_id  => v_container_item_id,
5377       x_return_status      => v_return_status);
5378 
5379       --dbms_output.put_line(v_qty_per_cont);
5380       --dbms_output.put_line(v_container_item_id);
5381       --  dbms_output.put_line( v_return_status);
5382       */
5383 
5384  END test;
5385 
5386 
5387 
5388 
5389 
5390  FUNCTION do_cartonization( mohdrid NUMBER, trxhdrid NUMBER, outbound VARCHAR2, sublvlctrl VARCHAR2, per_fill VARCHAR2) RETURN NUMBER IS
5391     language java name
5392       'oracle.apps.wms.cartonization.server.Cartonization.start(java.lang.Long, java.lang.Long, java.lang.String, java.lang.String, java.lang.String) return java.lang.Long';
5393 
5394 
5395    --This procedure inserts records into wms_device_requests table for further
5396     -- processing by device integration code.This procedure is called from
5397     -- WMSCRTNB.pls and WMSTSKUB.pls
5398 
5399   PROCEDURE insert_device_request_rec(p_mmtt_row IN mmtt_row)
5400     IS
5401 
5402        CURSOR lot_ser_cursor(p_transaction_temp_id NUMBER) IS
5403           SELECT
5404             mtlt.lot_number lot_num,
5405            mtlt.transaction_quantity lot_qty,
5406             msnt.fm_serial_number ser_num
5407             FROM mtl_material_transactions_temp mmtt,
5408             mtl_transaction_lots_temp mtlt,
5409             mtl_serial_numbers_temp msnt
5410             WHERE
5411             mmtt.transaction_temp_id = p_transaction_temp_id
5412             AND mmtt.transaction_temp_id = mtlt.transaction_temp_id(+)
5413             AND mmtt.transaction_temp_id = msnt.transaction_temp_id(+)
5414             AND ((mmtt.transaction_temp_id=msnt.transaction_temp_id
5415                   AND mtlt.serial_transaction_temp_id=msnt.transaction_temp_id)
5416                  OR  1=1);
5417 
5418        l_serial_code NUMBER;
5419        l_lot_code NUMBER;
5420        wdrData wdr_row;
5421        l_device_id NUMBER;
5422        l_qty NUMBER;
5423        l_count NUMBER;
5424        l_bus_event_id NUMBER;
5425 
5426  BEGIN
5427 
5428 
5429     IF wms_device_integration_pvt.wms_call_device_request = 1 THEN
5430        --set in INVPPICB.pls through call to is_device_set_up()
5431 
5432        l_bus_event_id :=
5433          WMS_Device_Integration_PVT.wms_be_pick_release;
5434 
5435      ELSIF wms_device_integration_pvt.wms_call_device_request = 2 THEN
5436        --set in INVUTILB.pls/WMSTSKUB.pls through call to is_device_set_up()
5437 
5438        l_bus_event_id :=
5439          WMS_Device_Integration_PVT.wms_be_mo_task_alloc;
5440 
5441 	--START Added below elseif for 13702075
5442      ELSIF wms_device_integration_pvt.wms_call_device_request = 3 THEN
5443        --set in INVUTILB.pls/WMSTSKUB.pls through call to is_device_set_up()
5444 
5445        l_bus_event_id :=
5446 	                      WMS_Device_Integration_PVT.WMS_BE_WIP_PICK_RELEASE;
5447 	--set in INVVWPKB.pls inv_wip_picking_pvt through  call to is_device_set_up()
5448 
5449    -- END Added elsif for bug 13702075
5450     END IF;
5451 
5452 
5453     IF wms_device_integration_pvt.wms_call_device_request IS NOT NULL THEN
5454        --insert line for load operation
5455        INSERT INTO wms_device_requests (request_id,
5456                                         task_id,
5457                                         task_summary,
5458                                         task_type_id,
5459                                         business_event_id,
5460                                         organization_id,
5461                                         subinventory_code,
5462                                         locator_id,
5463                                         transfer_org_id,
5464                                         transfer_sub_code,
5465                                         transfer_loc_id,
5466                                         inventory_item_id,
5467                                         revision,
5468                                         uom,
5469 					lpn_id,
5470                                         xfer_lpn_id,--4472870
5471                                         transaction_quantity,
5472                                         last_update_date,
5473                                         last_updated_by) VALUES
5474 
5475          ( WMS_Device_Integration_PVT.wms_pkRel_dev_req_id, --global var so that for all lines in a pick release, it remains same
5476            p_mmtt_row.transaction_temp_id,
5477            'Y',
5478            1, --"LOAD"
5479            l_bus_event_id,
5480            p_mmtt_row.organization_id ,  --use you local variables for these values
5481            p_mmtt_row.subinventory_code,
5482            p_mmtt_row.locator_id ,
5483            p_mmtt_row.transfer_organization,
5484            NULL,
5485            NULL,
5486            p_mmtt_row.inventory_item_id,
5487            p_mmtt_row.revision,
5488            p_mmtt_row.transaction_uom,
5489 	   p_mmtt_row.allocated_lpn_id,
5490            p_mmtt_row.cartonization_id,
5491            p_mmtt_row.transaction_quantity,
5492            Sysdate,
5493            FND_GLOBAL.USER_ID);
5494 
5495 
5496        --insert another line for drop
5497        INSERT INTO wms_device_requests (request_id,
5498                                         task_id,
5499                                         task_summary,
5500                                         task_type_id,
5501                                         business_event_id,
5502                                         organization_id,
5503                                         subinventory_code,
5504                                         locator_id,
5505                                         transfer_org_id,
5506                                         transfer_sub_code,
5507                                         transfer_loc_id,
5508                                         inventory_item_id,
5509                                         revision,
5510                                         uom,
5511 					lpn_id,
5512                                         xfer_lpn_id, --4472870
5513                                         transaction_quantity,
5514                                         last_update_date,
5515                                         last_updated_by) VALUES
5516 
5517          ( WMS_Device_Integration_PVT.wms_pkRel_dev_req_id, --global var so that for all lines in a pick release, it remains same
5518            p_mmtt_row.transaction_temp_id,
5519            'Y',
5520            2, --"DROP"
5521            l_bus_event_id,
5522            p_mmtt_row.organization_id ,
5523            NULL,
5524            NULL,
5525            p_mmtt_row.transfer_organization,
5526            p_mmtt_row.transfer_subinventory ,
5527            p_mmtt_row.transfer_to_location ,
5528            p_mmtt_row.inventory_item_id,
5529            p_mmtt_row.revision,
5530            p_mmtt_row.transaction_uom,
5531 	   p_mmtt_row.allocated_lpn_id,
5532            p_mmtt_row.cartonization_id,
5533            p_mmtt_row.transaction_quantity,
5534            Sysdate,
5535            FND_GLOBAL.USER_ID);
5536 
5537        wdrdata.request_id := WMS_Device_Integration_PVT.wms_pkrel_dev_req_id;
5538        wdrData.organization_id := p_mmtt_row.organization_id;
5539        wdrData.subinventory_code := p_mmtt_row.subinventory_code ;
5540        wdrData.business_event_id := l_bus_event_id;
5541        wdrData.locator_id := p_mmtt_row.locator_id;
5542        wdrData.task_id := p_mmtt_row.transaction_temp_id ;
5543        wdrData.task_summary := 'Y';
5544        wdrData.transfer_org_id   := p_mmtt_row.transfer_organization;
5545        wdrData.transfer_sub_code := p_mmtt_row.transfer_subinventory;
5546        wdrData.transfer_loc_id   := p_mmtt_row.transfer_to_location;
5547 
5548 
5549        --Call an api to get the device, this API also updates above
5550        --inserted records WITH found matching device id
5551        l_device_id := WMS_Device_Integration_PVT.SELECT_DEVICE(wdrData,'Y',NULL);
5552 
5553 
5554 
5555        IF  l_device_id <> 0 THEN --process further only if device IS found
5556 
5557           --CHECK whether it is a lot/Serial controlled item
5558           SELECT
5559             msi.serial_number_control_code,
5560             msi.lot_control_code
5561             INTO
5562             l_serial_code,
5563             l_lot_code
5564             FROM mtl_system_items msi
5565             WHERE msi.inventory_item_id = p_mmtt_row.inventory_item_id
5566             AND   msi.organization_id   = p_mmtt_row.organization_id;
5567 
5568 
5569           IF (l_lot_code >1 OR l_serial_code >1) THEN --LOT OR/AND SERIAL ITEMS
5570 
5571              --check to see whether need to insert lot/ser record
5572              IF wms_device_integration_pvt.wms_insert_lotSer_rec_WDR = 1 THEN
5573 
5574                 FOR l_rec IN lot_ser_cursor(p_mmtt_row.transaction_temp_id) LOOP
5575                    IF (l_rec.lot_num IS NOT NULL OR l_rec.ser_num IS NOT NULL) THEN
5576                       IF (l_rec.ser_num IS NOT NULL) THEN
5577                          l_qty := 1;
5578                        ELSE
5579                          l_qty := l_rec.lot_qty;
5580                       END IF;
5581                       l_count := l_count + 1;
5582                       INSERT INTO wms_device_requests
5583 			(request_id,
5584 			 task_id,
5585 			 relation_id,
5586 			 sequence_id,
5587 			 task_summary,
5588 			 task_type_id,
5589 			 business_event_id,
5590 			 organization_id,
5591 			 subinventory_code,
5592 			 locator_id,
5593 			 transfer_org_id,
5594 			 transfer_sub_code,
5595 			 transfer_loc_id,
5596 			 inventory_item_id,
5597 			 revision,
5598 			 uom,
5599 			 lot_number,
5600 			 lot_qty,
5601 			 serial_number,
5602 			 lpn_id,
5603 			 xfer_lpn_id,
5604 			 transaction_quantity,
5605 			 device_id,
5606 			 status_code,
5607 			 last_update_date,
5608 			 last_updated_by,
5609 			 last_update_login)
5610 			VALUES
5611                         (WMS_Device_Integration_PVT.wms_pkRel_dev_req_id,
5612                          p_mmtt_row.transaction_temp_id,
5613                          NULL,
5614                          NULL,
5615                          'N',
5616                          NULL,
5617                          l_bus_event_id,
5618                          p_mmtt_row.organization_id,
5619                          p_mmtt_row.subinventory_code,
5620                          p_mmtt_row.locator_id,
5621                          p_mmtt_row.transfer_organization,
5622                          p_mmtt_row.transfer_subinventory,
5623                          p_mmtt_row.transfer_to_location,
5624                          p_mmtt_row.inventory_item_id,
5625                          p_mmtt_row.revision,
5626                          p_mmtt_row.TRANSACTION_uom,
5627                          l_rec.lot_num,
5628                          l_rec.lot_qty,
5629                          l_rec.ser_num,
5630 			 p_mmtt_row.allocated_lpn_id,
5631 			 p_mmtt_row.cartonization_id,
5632 			 l_qty,
5633                          l_device_id,
5634                          'S',
5635                          p_mmtt_row.last_update_date,
5636                          p_mmtt_row.last_updated_by,
5637                          p_mmtt_row.last_update_login);
5638                    END IF;
5639                 END LOOP;
5640 
5641                 IF(l_count = 0) THEN
5642                    if (g_trace_on = 1) then
5643                       log_event('Error in inserting lot serial details in WDR,no data');
5644                    END IF;
5645 
5646                 END IF;
5647 
5648              END IF;--Lot/serial enabled check
5649 
5650           END IF;--lot/serial item check
5651 
5652        END IF; -- device IS found
5653 
5654     END IF;--p_mmtt_row.transaction_temp_id > 0
5655 
5656 
5657   END insert_device_request_rec;
5658 
5659 
5660  PROCEDURE ins_wct_rows_into_mmtt(
5661                                    p_m_o_h_id         IN       NUMBER,
5662                                    p_outbound         IN      VARCHAR2,
5663                                    x_return_status      OUT     NOCOPY VARCHAR2,
5664                                    x_msg_count          OUT     NOCOPY NUMBER,
5665                                    x_msg_data           OUT     NOCOPY VARCHAR2)
5666    is
5667       CURSOR wct_rows IS
5668          SELECT wct.* FROM wms_cartonization_temp wct,
5669            mtl_txn_request_lines mtrl WHERE wct.move_order_line_id =
5670            mtrl.line_id AND mtrl.header_id = p_m_o_h_id;
5671 
5672       -- patchset J bulk picking  ---
5673       -- following cursor will be used for calling from concurrent program   --
5674       -- also after patchset J, this cursor will replace wct_row since no need to
5675       -- query by move order header  and also to query parent lines ---------------------
5676       CURSOR wct_rows_bulk IS
5677          SELECT wct.* FROM wms_cartonization_temp wct;
5678 
5679       -- end of patchset J bulk picking --------------
5680 
5681       CURSOR bpack_rows IS
5682            SELECT * FROM wms_cartonization_temp
5683              WHERE
5684              transaction_header_id = p_m_o_h_id;
5685 
5686       CURSOR lot_ser_cursor(p_transaction_temp_id NUMBER) IS
5687          SELECT
5688            mtlt.lot_number lot_num,
5689            mtlt.transaction_quantity lot_qty,
5690            msnt.fm_serial_number ser_num
5691            FROM mtl_material_transactions_temp mmtt,
5692            mtl_transaction_lots_temp mtlt,
5693            mtl_serial_numbers_temp msnt
5694            WHERE
5695            mmtt.transaction_temp_id = p_transaction_temp_id
5696            AND mmtt.transaction_temp_id = mtlt.transaction_temp_id(+)
5697            AND mmtt.transaction_temp_id = msnt.transaction_temp_id(+)
5698            AND ((mmtt.transaction_temp_id=msnt.transaction_temp_id
5699                  AND mtlt.serial_transaction_temp_id=msnt.transaction_temp_id)
5700                 OR  1=1);
5701 
5702       v2 WCT_ROW_TYPE;
5703       v1 MMTT_ROW_TYPE;
5704       v_lpn VARCHAR2(255);
5705       v_exist VARCHAR2(1);
5706       l_msg VARCHAR2(255);
5707 
5708         l_serial_code NUMBER;
5709         l_lot_code NUMBER;
5710         wdrData wdr_row;
5711         l_device_id NUMBER;
5712         l_qty NUMBER;
5713         l_count NUMBER;
5714     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
5715  BEGIN
5716     error_code := 'INS_WCT_ROWS_TO_MMTT';
5717     if (g_trace_on = 1) then log_event(' Entered insert wct rows to mmtt'||p_m_o_h_id); END IF;
5718     -- Initialize API return status to success
5719     x_return_status  := fnd_api.g_ret_sts_success;
5720 
5721     IF( p_outbound IS NOT NULL AND p_outbound = 'N') THEN
5722        if (g_trace_on = 1) then log_event(' open bpack_rows'); END IF;
5723        OPEN bpack_rows;
5724      ELSE
5725        IF G_CURRENT_RELEASE_LEVEL < G_J_RELEASE_LEVEL THEN -- bulk picking patchset J ---
5726           if (g_trace_on = 1) then log_event(' open wct_rows'); END IF;
5727           OPEN wct_rows;
5728         ELSE
5729           -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING --START'); end if;
5730                 OPEN wct_rows_bulk;
5731            --  if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING --END'); end if;
5732          END IF;
5733       END IF;
5734 
5735      LOOP
5736 
5737          IF( p_outbound IS NOT NULL AND p_outbound = 'N') THEN
5738             if (g_trace_on = 1) then log_event(' fetching  a bpack row '); END IF;
5739             FETCH bpack_rows INTO v2;
5740             EXIT WHEN bpack_rows%notfound;
5741             if (g_trace_on = 1) then log_event(' fetched  a row '); END IF;
5742           ELSE
5743             IF  G_CURRENT_RELEASE_LEVEL < G_J_RELEASE_LEVEL  THEN -- bulk picking patchset J ---
5744                if (g_trace_on = 1) then log_event(' fetching  a wct row '); END IF;
5745                FETCH wct_rows INTO v2;
5746                EXIT WHEN wct_rows%notfound;
5747                if (g_trace_on = 1) then log_event(' fetched  a row ');  END IF;
5748             ELSE
5749                if (g_trace_on = 1) then -- log_event('PATCHSET J-- BULK PICKING --START');
5750                                         log_event(' fetching  a wct row '); END IF;
5751                        FETCH wct_rows_bulk INTO v2;
5752                        EXIT WHEN wct_rows_bulk%notfound;
5753                if (g_trace_on = 1) then log_event(' fetched  a row ');  END IF;
5754               -- if (g_trace_on = 1) then log_event('PATCHSET J-- BULK PICKING --END'); end if;
5755             END IF;
5756 
5757         END IF;
5758 
5759         v1 := to_mmtt(v2);
5760 
5761          IF packaging_mode IN (wms_cartnzn_pub.int_bp_pkg_mode,
5762                                wms_cartnzn_pub.mob_bp_pkg_mode ) THEN
5763 
5764             v1.transfer_lpn_id := v1.cartonization_id;
5765 
5766          END IF;
5767 
5768 
5769 
5770       IF( v1.transaction_temp_id > 0 ) then
5771 
5772           IF (v1.transaction_quantity > 0 ) THEN  --Bug#5868236.Added IF block. -- 14028129
5773 	 --INSERT RECORDS INTO WMS_DEVICE_REQUESTS TABLE
5774 	 log_event('Calling insert_device_request_rec to insert records in wms_device_requests table'); --Added for bug 12988615
5775 	  insert_device_request_rec(v1);
5776           --/////////////////
5777 		   END IF; -- 14028129
5778 
5779             if (g_trace_on = 1) then log_event(' temp_id > 0'); END IF;
5780 
5781             v_exist := 'N';
5782 
5783             BEGIN
5784                SELECT 'Y' INTO v_exist FROM dual
5785                  WHERE
5786                  exists ( SELECT transaction_temp_id FROM mtl_material_transactions_temp
5787                        WHERE transaction_temp_id = v1.transaction_temp_id);
5788             EXCEPTION
5789                WHEN no_data_found THEN
5790                   v_exist := 'N';
5791             END;
5792 
5793 
5794             if (g_trace_on = 1) then log_event(' row_exist '||v_exist); END IF;
5795 
5796             IF v1.cartonization_id IS NOT NULL THEN
5797                IF (g_trace_on = 1) then log_event(' Cartonization id  '||v1.cartonization_id); END IF;
5798                   SELECT license_plate_number INTO v_lpn FROM wms_license_plate_numbers WHERE
5799                   lpn_id = v1.cartonization_id;
5800                ELSE
5801                   v_lpn := NULL;
5802                   --2513907 fix
5803                IF v1.container_item_id IS NULL THEN
5804                   v1.container_item_id := -1;
5805                END IF;
5806               --2513907 fix
5807             END IF;
5808 
5809             if (g_trace_on = 1) then log_event(' After the cartonization id condition '); END IF;
5810 
5811             table_name := 'mtl_material_transactions_temp';
5812 
5813 
5814             IF v_exist = 'Y' THEN
5815 
5816                if (g_trace_on = 1) then
5817                   log_event('calling update mmtt for  tempid:'||v1.transaction_temp_id);
5818                   log_event(' primary quantity:'||v1.primary_quantity);
5819                   log_event(' transaction_quantity:'|| v1.transaction_quantity);
5820                   log_event(' secondary transaction_quantity:'|| v1.secondary_transaction_quantity); --invconv kkillams
5821                END IF;
5822 
5823                UPDATE_MMTT(
5824                             p_transaction_temp_id =>  v1.transaction_temp_id,
5825                             p_primary_quantity    =>  v1.primary_quantity,
5826                             p_transaction_quantity => v1.transaction_quantity,
5827                             p_secondary_quantity   => v1.secondary_transaction_quantity, --invconv kkillams
5828                             --p_LPN_string           => v_lpn,
5829                             p_lpn_id               => v1.cartonization_id,
5830                             p_container_item_id    => v1.container_item_id,
5831                             p_parent_line_id       => v1.parent_line_id,
5832                             x_return_status        => x_return_status,
5833                             x_msg_count            => x_msg_count,
5834                             x_msg_data             => x_msg_data );
5835 
5836              ELSIF v_EXIST = 'N' THEN
5837 
5838                if (g_trace_on = 1) then log_event(' inserting row into mmtt for temp id :'||v1.transaction_temp_id); END IF;
5839                wms_task_dispatch_engine.insert_mmtt(l_mmtt_rec => v1);
5840              ELSE
5841                RAISE fnd_api.g_exc_unexpected_error;
5842 
5843             END IF;
5844          END IF;
5845 
5846       END LOOP;
5847 
5848       IF( p_outbound IS NOT NULL AND p_outbound = 'N') THEN
5849          IF( bpack_rows%isopen) then
5850             close bpack_rows;
5851          END IF;
5852 
5853        ELSE
5854 
5855          IF( wct_rows%isopen ) then
5856             close wct_rows;
5857          END IF;
5858 
5859       END IF;
5860 
5861 
5862 
5863    EXCEPTION
5864     WHEN OTHERS THEN
5865        IF( wct_rows%isopen ) then
5866           CLOSE wct_rows;
5867        END IF;
5868        --l_msg := Sqlerrm;
5869        if (g_trace_on = 1) then log_event('SQL Error Message ' || SQLERRM); END IF;
5870        if (g_trace_on = 1) then log_event('SQL Error Code ' || SQLCODE); END IF;
5871        if (g_trace_on = 1) then log_event('error OCCURRED IN INSERTING WCT ROWS BACK TO MMTT'); END IF;
5872        RAISE fnd_api.g_exc_unexpected_error;
5873 
5874 
5875  END ins_wct_rows_into_mmtt;
5876 
5877  FUNCTION get_log_flag RETURN VARCHAR2 IS
5878 
5879     log_flag VARCHAR2(1) := 'Y';
5880     --Bug 3319754 changes. Also made change so that get_log_flag
5881     --returns 'Y' irrespective of level
5882     g_trace_on number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
5883     --l_dbg_lvl  NUMBER := 0;
5884 
5885     --l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
5886  BEGIN
5887     error_code := 'GET_LOG_FLAG';
5888 
5889 --    select fnd_profile.value('INV_DEBUG_TRACE')
5890 --      into g_trace_on
5891 --      from dual;
5892 
5893 --    if( g_trace_on = 1 ) then
5894 --       select fnd_profile.value('INV_DEBUG_LEVEL')
5895 --       into l_dbg_lvl
5896 --       from dual;
5897 --    END IF;
5898 
5899     if(g_trace_on = 1) THEN --AND l_dbg_lvl > 5 then
5900        log_flag := 'Y';
5901      ELSE
5902        log_flag := 'N';
5903     END IF;
5904 
5905     RETURN log_flag;
5906 
5907 
5908  EXCEPTION
5909    when others then
5910       null;
5911  END get_log_flag;
5912 
5913    PROCEDURE cartonize_single_item (
5914                         x_return_status         OUT   NOCOPY VARCHAR2,
5915                         x_msg_count             OUT   NOCOPY NUMBER,
5916                         x_msg_data              OUT   NOCOPY VARCHAR2,
5917                         p_out_bound             IN    VARCHAR2,
5918                         p_org_id                IN    NUMBER,
5919                         p_move_order_header_id  IN    NUMBER,
5920                         p_subinventory_name     IN    VARCHAR2 DEFAULT NULL
5921    )
5922    IS
5923       l_api_name     CONSTANT VARCHAR2(30) := 'cartonize_single_item';
5924       l_api_version  CONSTANT NUMBER       := 1.0;
5925       v1 WCT_ROW_TYPE;
5926       cartonization_profile  VARCHAR2(1)   := 'Y';
5927       v_cart_value NUMBER;
5928       v_container_item_id NUMBER:= NULL;
5929       v_qty NUMBER:= -1;
5930       v_qty_per_cont NUMBER := -1;
5931       v_tr_qty_per_cont NUMBER:= -1;
5932       v_sec_tr_qty_per_cont NUMBER:= -1;
5933       v_lpn_out VARCHAR2(30) := ' ';
5934       v_primary_uom_code VARCHAR2(3);
5935       v_loop NUMBER := 0;
5936       v_process_id NUMBER := 0;
5937       v_ttemp_id NUMBER := 0;
5938       v_lpn_id_out NUMBER := 0;
5939       v_lpn_id NUMBER := 0;
5940       ret_value   NUMBER := 29;
5941       v_return_status VARCHAR2(1);
5942       v_left_prim_quant NUMBER;
5943       v_left_tr_quant NUMBER;
5944       v_sec_left_tr_quant NUMBER;
5945       v_sublvlctrl VARCHAR2(1) := '2';
5946       --Bug 2720653 fix
5947       --l_lpn_id_tbl inv_label.transaction_id_rec_type;
5948       l_return_status VARCHAR2(1) := fnd_api.g_ret_sts_success;
5949       l_msg_count NUMBER;
5950       l_msg_data VARCHAR2(400);
5951       l_progress VARCHAR2(10);
5952       l_label_status VARCHAR2(500);
5953       l_counter NUMBER := 0;
5954       v_prev_move_order_line_id NUMBER := -999;
5955       space_avail_for NUMBER := 0;
5956       tr_space_avail_for NUMBER := 0;
5957       sec_tr_space_avail_for NUMBER := 0; --invconv kkillams
5958       curr_temp_id mtl_material_transactions_temp.transaction_temp_id%TYPE ;
5959       v_lot_control_code NUMBER := NULL;
5960       v_serial_control_code NUMBER := NULL;
5961       api_table_index NUMBER := 0;
5962       l_current_header_id  NUMBER := NULL;
5963       l_stop_level    NUMBER := -1;
5964       v_prev_item_id  NUMBER := -999;
5965       l_prev_package_id NUMBER := NULL;
5966       l_temp_id       NUMBER := NULL;
5967       l_item_id       NUMBER := NULL;
5968       l_qty           NUMBER := NULL;
5969       l_tr_qty        NUMBER := NULL;
5970       l_sec_tr_qty    NUMBER := NULL;
5971       l_clpn_id       NUMBER := NULL;
5972       l_citem_id      NUMBER := NULL;
5973       l_package_id      NUMBER := NULL;
5974       l_upd_qty_flag VARCHAR2(1) := NULL;
5975       l_prev_header_id NUMBER := NULL;
5976       l_header_id     NUMBER := NULL;
5977       L_no_pkgs_gen   VARCHAR2(1);
5978       l_prev_condition  VARCHAR2(1);
5979       l_revision_code   VARCHAR2(1);
5980       l_lot_code        VARCHAR2(1);
5981       l_serial_code     VARCHAR2(1);
5982       l_is_revision_control  BOOLEAN;
5983       l_is_lot_control       BOOLEAN;
5984       l_is_serial_control    BOOLEAN;
5985       l_rqoh NUMBER;
5986       l_qr   NUMBER;
5987       l_qs   NUMBER;
5988       l_atr  NUMBER;
5989       l_att  NUMBER;
5990       l_qoh  NUMBER;
5991       l_lpn_fully_allocated  VARCHAR2(1) :='N';
5992       percent_fill_basis         VARCHAR2(1) :='W';
5993       l_valid_container VARCHAR2(1) := 'Y';
5994 
5995       l_cartonize_sales_orders VARCHAR2(1) :=NULL;
5996       l_cartonize_manufacturing VARCHAR2(1) :=NULL;
5997       l_move_order_type   NUMBER;
5998       l_pack_level  number := 0;
5999 
6000       l_uom_rate number;
6001 
6002       CURSOR wct_rows IS
6003       SELECT wct.* FROM wms_cartonization_temp wct,
6004       mtl_txn_request_lines mtrl,
6005       mtl_secondary_inventories sub,
6006       mtl_parameters mtlp
6007       WHERE
6008       wct.move_order_line_id =mtrl.line_id
6009       AND mtrl.header_id = p_move_order_header_id
6010       AND wct.cartonization_id IS null
6011       AND mtlp.organization_id = wct.organization_id
6012       AND sub.organization_id = wct.organization_id
6013       AND wct.transfer_lpn_id IS NULL
6014       AND sub.secondary_inventory_name = wct.subinventory_code
6015       AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
6016 	  OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
6017 	  OR (NVL(mtlp.cartonization_flag,-1) = 4)
6018 	  OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
6019 	  )
6020       ORDER BY wct.inventory_item_id,
6021       wct.move_order_line_id,
6022           Abs(wct.transaction_temp_id);
6023 
6024       CURSOR wct_sub_rows IS
6025       SELECT wct.* FROM wms_cartonization_temp wct,
6026       mtl_txn_request_lines mtrl,
6027       mtl_secondary_inventories sub,
6028       mtl_parameters mtlp
6029       WHERE
6030       wct.move_order_line_id =mtrl.line_id
6031       AND mtrl.header_id = p_move_order_header_id
6032       AND wct.cartonization_id IS null
6033       AND mtlp.organization_id = wct.organization_id
6034       AND sub.organization_id = wct.organization_id
6035       AND wct.transfer_lpn_id IS NULL
6036       AND sub.secondary_inventory_name = wct.subinventory_code
6037       AND wct.subinventory_code = p_subinventory_name
6038       AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
6039 	  OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
6040 	  OR (NVL(mtlp.cartonization_flag,-1) = 4)
6041 	  OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
6042 	  )
6043       ORDER BY wct.inventory_item_id,
6044       wct.move_order_line_id,
6045           Abs(wct.transaction_temp_id);
6046 
6047       CURSOR OB_LBPRT IS
6048       SELECT DISTINCT mmtt.cartonization_id FROM
6049       --2513907 fix
6050       wms_cartonization_temp mmtt,
6051       --mtl_material_transactions_temp mmtt,
6052       mtl_txn_request_lines mtrl WHERE
6053       mmtt.move_order_line_id = mtrl.line_id
6054       AND mtrl.header_id      = p_move_order_header_id
6055       AND mmtt.cartonization_id IS NOT NULL
6056       ORDER BY mmtt.cartonization_id;
6057 
6058       CURSOR bpack_rows(p_hdr_id NUMBER) IS
6059       SELECT * FROM
6060       wms_cartonization_temp
6061       WHERE
6062       transaction_header_id = p_hdr_id
6063       AND cartonization_id IS NULL
6064       AND transfer_lpn_id IS NULL
6065       order by  move_order_line_id,
6066       decode(content_lpn_id,null,inventory_item_id,
6067       decode(sign(p_hdr_id),
6068       -1,
6069       inventory_item_id,
6070       wms_cartnzn_pub.Get_LPN_ItemId(content_lpn_id)
6071       ) ),Abs(transaction_temp_id);
6072 
6073       CURSOR packages(p_hdr_id NUMBER) IS
6074       SELECT
6075       transaction_temp_id,
6076       inventory_item_id,
6077       primary_quantity,
6078       transaction_quantity,
6079       secondary_transaction_quantity, --invconv kkillams
6080       content_lpn_id,
6081       container_item_id,
6082       cartonization_id
6083       FROM
6084       wms_cartonization_temp
6085       WHERE
6086       transaction_header_id = p_hdr_id
6087       order by cartonization_id;
6088 
6089       CURSOR opackages(p_hdr_id NUMBER) IS
6090       SELECT
6091       wct.transaction_temp_id,
6092       wct.inventory_item_id,
6093       wct.primary_quantity,
6094       wct.transaction_quantity,
6095       wct.secondary_transaction_quantity, --invconv kkillams
6096       wct.content_lpn_id,
6097       wct.container_item_id,
6098       wct.cartonization_id
6099       FROM
6100       wms_cartonization_temp wct,
6101       mtl_txn_request_lines mtrl
6102       WHERE
6103       wct.move_order_line_id = mtrl.line_id AND
6104       mtrl.header_id = p_hdr_id
6105       order by wct.cartonization_id;
6106 
6107 
6108 
6109    BEGIN
6110 
6111       IF (g_trace_on = 1) THEN
6112          log_event(' Inside CARTONIZE_SINGLE_ITEM()');
6113       END IF;
6114 
6115       outbound := 'Y';
6116       error_code := 'CARTONIZE 100';
6117 
6118       IF (g_trace_on = 1) THEN
6119          log_event(' outbound = '|| outbound || ' pack level ' || pack_level);
6120          log_event(' restrcited subinventory = '|| p_subinventory_name);
6121       END IF;
6122 
6123       IF p_subinventory_name IS NOT NULL THEN
6124          IF (g_trace_on = 1) THEN
6125             log_event(' Opening cursor wct_sub_row ');
6126          END IF;
6127          OPEN wct_sub_rows;
6128       ELSE
6129          IF (g_trace_on = 1) THEN
6130             log_event(' Opening cursor wct_row ');
6131          END IF;
6132          OPEN wct_rows;
6133       END IF;
6134 
6135 --      OPEN wct_rows;
6136 
6137       LOOP
6138          IF( (outbound = 'Y') AND (pack_level = 0)) THEN
6139 
6140             IF p_subinventory_name IS NOT NULL THEN
6141                FETCH wct_sub_rows INTO v1;
6142                IF (g_trace_on = 1) THEN
6143                   log_event('wct_sub_rows Fetch success');
6144                END IF;
6145                EXIT WHEN wct_sub_rows%NOTFOUND;
6146             ELSE
6147                FETCH wct_rows INTO v1;
6148                IF (g_trace_on = 1) THEN
6149                   log_event('wct_rows Fetch success');
6150                END IF;
6151                EXIT WHEN wct_rows%NOTFOUND;
6152             END IF;
6153 
6154             IF NOT INV_CACHE.set_item_rec(v1.organization_id, v1.inventory_item_id) THEN
6155                IF (g_trace_on = 1) THEN
6156                   log_event('Inventory Cache Set Item Rec Failed');
6157                END IF;
6158                RAISE fnd_api.g_exc_unexpected_error;
6159             END IF;
6160 
6161             l_revision_code := inv_cache.item_rec.revision_qty_control_code;
6162             l_lot_code := inv_cache.item_rec.lot_control_code;
6163             l_serial_code := inv_cache.item_rec.serial_number_control_code;
6164 
6165             IF (g_trace_on = 1) THEN
6166                log_event('revision code : '|| l_revision_code);
6167                log_event('lot code : '|| l_lot_code);
6168                log_event('serial code : '|| l_serial_code);
6169             END IF;
6170 
6171             IF l_revision_code> 1 THEN
6172                l_is_revision_control := TRUE;
6173             ELSE
6174                l_is_revision_control := FALSE;
6175             END IF;
6176 
6177             IF l_lot_code > 1 THEN
6178                l_is_lot_control := TRUE;
6179             ELSE
6180                l_is_lot_control := FALSE;
6181             END IF;
6182 
6183             IF (l_serial_code>1 AND l_serial_code<>6) THEN
6184                l_is_serial_control := TRUE;
6185             ELSE
6186                l_is_serial_control := FALSE;
6187             END IF;
6188 
6189             IF (v1.allocated_lpn_id IS NOT NULL ) THEN
6190                -- Bug 3740610 No need to call qty tree,getting on-hand qty from table
6191                SELECT NVL(SUM(primary_transaction_quantity),0)
6192                into l_qoh FROM mtl_onhand_quantities_detail
6193                WHERE organization_id = v1.organization_id
6194                AND subinventory_code = v1.subinventory_code
6195                AND locator_id = v1.locator_id
6196                AND lpn_id = v1.allocated_lpn_id;
6197 
6198                IF (g_trace_on = 1) THEN log_event('lpn_id' || v1.allocated_lpn_id);
6199                   log_event('l_qoh' || l_qoh);
6200                   log_event('PrimaryQty'|| v1.primary_quantity);
6201                END IF;
6202 
6203                --Bug 3740610 Comparing on-hand qty with primary_qty of mmtt
6204 
6205                IF (l_qoh = v1.primary_quantity) THEN
6206                   l_lpn_fully_allocated := 'Y';
6207                   t_lpn_alloc_flag_table(v1.transaction_temp_id).transaction_temp_id := v1.transaction_temp_id;
6208                   t_lpn_alloc_flag_table(v1.transaction_temp_id).lpn_alloc_flag := 'Y';
6209 
6210                ELSE
6211                   l_lpn_fully_allocated := 'N';
6212                   t_lpn_alloc_flag_table(v1.transaction_temp_id).transaction_temp_id := v1.transaction_temp_id;
6213                   t_lpn_alloc_flag_table(v1.transaction_temp_id).lpn_alloc_flag := 'N';
6214 
6215                END IF;
6216             ELSE
6217                l_lpn_fully_allocated := 'Y';
6218                t_lpn_alloc_flag_table(v1.transaction_temp_id).transaction_temp_id := v1.transaction_temp_id;
6219                t_lpn_alloc_flag_table(v1.transaction_temp_id).lpn_alloc_flag := 'Y';
6220 
6221             END IF ;
6222 
6223          END IF;
6224 
6225          IF (g_trace_on = 1) THEN
6226             log_event(' Fetch succeeded');
6227             log_event('lpn_fully_allocated:'||l_lpn_fully_allocated);
6228             log_event('lpn_id after if:'||v1.allocated_lpn_id);
6229          END IF;
6230 
6231          IF (v1.allocated_lpn_id IS NOT NULL)
6232          AND (l_lpn_fully_allocated = 'Y') THEN
6233             null;
6234          ELSE
6235             IF (g_trace_on = 1) THEN
6236                log_event('v1.transaction_temp_id : ' || v1.transaction_temp_id);
6237             END IF;
6238 
6239             --populate lpn_alloc_flag with null for loose item
6240             t_lpn_alloc_flag_table(v1.transaction_temp_id).transaction_temp_id := v1.transaction_temp_id;
6241             t_lpn_alloc_flag_table(v1.transaction_temp_id).lpn_alloc_flag := NULL;
6242 
6243 --            SELECT v1.transaction_temp_id, null
6244 --            INTO  t_lpn_alloc_flag_table(v1.transaction_temp_id)
6245 --            FROM dual;
6246 
6247             -- If the content_lpn_id is populated on the mmtt record
6248             -- could be two cases. Either we are trying to pack an LPN
6249             -- or a package. We will have packages poulated in this
6250             -- column only by multi level cartonization and when it
6251             -- does that, the row is inserted with negative header id
6252             -- Basing on this we either get the item associated with
6253             -- the lpn, or item associated with the package
6254 
6255             IF ( v1.content_lpn_id IS NOT NULL ) THEN
6256                IF (g_trace_on = 1) THEN log_event(' content_lpn_id IS NOT NULL'); END IF;
6257                   IF v1.transaction_header_id  < 0 THEN
6258                      error_code := 'CARTONIZE 150';
6259                      --THe content_lpn_id has a package in it ..
6260                      --v1.inventory_item_id := Get_package_ItemId(v1.content_lpn_id);
6261                   ELSE
6262                      error_code := 'CARTONIZE 160';
6263                      --THe content_lpn_id has a LPN in it ..
6264                      v1.inventory_item_id := Get_LPN_ItemId(v1.content_lpn_id);
6265                   END IF;
6266                   -- When we are packaing an lpn or a package the qty is
6267                   -- always 1
6268                   v1.primary_quantity := 1;
6269                   v1.transaction_quantity := 1;
6270                 END IF;
6271 
6272                error_code := 'CARTONIZE 170';
6273 
6274                v_primary_uom_code := inv_cache.item_rec.primary_uom_code;
6275 
6276                IF ( v1.content_lpn_id IS NOT NULL ) THEN
6277                   -- We want to set the transaction uom same as primary uom
6278                   v1.transaction_uom := v_primary_uom_code;
6279                END IF;
6280 
6281                IF (g_trace_on = 1) THEN
6282                   log_event(' inventory_item_id:'||v1.inventory_item_id);
6283                   log_event(' primary_quantity:'||v1.primary_quantity);
6284                   log_event(' primary_uom_code:'||v_primary_uom_code);
6285                   log_event(' transaction_quantity:'||v1.transaction_quantity);
6286                   log_event(' transaction_uom:'||v1.transaction_uom);
6287                   log_event(' secondary_transaction_quantity:'||v1.secondary_transaction_quantity); --invconv kkillams
6288                   log_event(' secondary_uom_code:'||v1.secondary_uom_code); --invconv kkillams
6289                END IF;
6290 
6291                IF (outbound = 'Y')
6292                AND (pack_level = 0)
6293                AND (v1.inventory_item_id <> v_prev_item_id) THEN
6294                   l_prev_condition := 'Y';
6295                ELSIF (outbound = 'Y')
6296                AND (pack_level <> 0)
6297                AND ((v_prev_move_order_line_id  <> v1.move_order_line_id)
6298                OR (v1.inventory_item_id <> v_prev_item_id) ) THEN
6299                   l_prev_condition := 'Y';
6300                ELSIF (outbound = 'N') AND ( v1.inventory_item_id <> v_prev_item_id) THEN
6301                   l_prev_condition := 'Y';
6302                ELSE
6303                   l_prev_condition := 'N';
6304                END IF;
6305 
6306                IF (g_trace_on = 1) then log_event(' l_prev_condition '||l_prev_condition);  END IF;
6307                -- The below condition is used when to make a
6308                -- call to the an api that returns conatiner item relation
6309                -- ship if present
6310                -- In outbound mode
6311                --  you have to call this when previous move order line id
6312                --  is different from the current one
6313                -- In the inbound mode
6314                --  We need to call if the previous item is different form
6315                -- the current item
6316 
6317                IF l_prev_condition = 'Y' THEN
6318                   IF (g_trace_on = 1) then log_event(' call wms_container_pub.container_required_qty api for item id '||v1.inventory_item_id);  END IF;
6319 
6320                   v_prev_item_id := v1.inventory_item_id;
6321                   v_prev_move_order_line_id := v1.move_order_line_id;
6322                   v_container_item_id := NULL;
6323                   v_qty_per_cont := -1;
6324                   v_qty := -1;
6325                   space_avail_for := 0;
6326                   --Bug 2478970 fix
6327                   l_valid_container := 'Y';
6328 
6329                   IF (outbound = 'Y') AND (pack_level = 0)
6330                   AND (packaging_mode = wms_cartnzn_pub.pr_pkg_mode) THEN
6331                      error_code := 'CARTONIZE 180';
6332                      wsh_interface.Get_Max_Load_Qty(p_move_order_line_id => v1.move_order_line_id,
6333                                                   x_max_load_quantity  => v_qty_per_cont,
6334                                                   x_container_item_id  => v_container_item_id,
6335                                                   x_return_status     => v_return_status);
6336 
6337                    l_valid_container := 'Y'; --Undoing 2478970 fix
6338 
6339                    IF(  (v_return_status = fnd_api.g_ret_sts_success) AND
6340                         (v_qty_per_cont > 0) AND
6341                         (v_container_item_id IS NOT NULL) AND
6342                         (v_container_item_id > 0) ) THEN
6343 
6344                       v_qty := ceil(v1.primary_quantity/v_qty_per_cont);
6345                       -- This quantity needs to be recalculated. This is
6346                       -- poulated to pass the check marked by '#chk1'
6347                    END IF;
6348 
6349                    IF (g_trace_on = 1) THEN
6350                       log_event('wsh_interface.Get_Max_Load_Qty return status'||v_return_status);
6351                       log_event('container '||v_container_item_id);
6352                       log_event('Number of dum containers '||v_qty);
6353                    END IF;
6354 
6355                    v_prev_move_order_line_id := v1.move_order_line_id;
6356 
6357                ELSE
6358                   error_code := 'CARTONIZE 190';
6359                   wms_container_pub.Container_Required_Qty
6360                   (  p_api_version       => 1.0,
6361                      x_return_status     => v_return_status,
6362                      x_msg_count         => l_msg_count,
6363                      x_msg_data          => l_msg_data,
6364                      p_source_item_id    => v1.inventory_item_id,
6365                      p_source_qty        => v1.primary_quantity,
6366                      p_source_qty_uom    => v_primary_uom_code,
6367                      p_organization_id   => v1.organization_id,
6368                      p_dest_cont_item_id => v_container_item_id,
6369                      p_qty_required       => v_qty
6370                      );
6371 
6372                   IF (g_trace_on = 1) THEN
6373                      log_event('container_required_quantity return status'||v_return_status);
6374                      log_event('container '||v_container_item_id);
6375                      log_event('Number of conatiners '||v_qty);
6376                   END IF;
6377 
6378                   v_prev_item_id := v1.inventory_item_id;
6379 
6380                   IF( (v_return_status = fnd_api.g_ret_sts_success )   AND
6381                       (v_qty IS NOT NULL) AND
6382                       (v_qty > 0) AND
6383                       (v_container_item_id IS NOT NULL) AND
6384                       (v_container_item_id > 0) ) THEN
6385 
6386                      error_code := 'CARTONIZE 200';
6387 
6388                      SELECT max_load_quantity
6389                      INTO v_qty_per_cont
6390                      FROM wsh_container_items
6391                      WHERE load_item_id = v1.inventory_item_id
6392                      AND master_organization_id = v1.organization_id
6393                      AND container_item_id = v_container_item_id;
6394 
6395                   END IF;
6396 
6397                END IF;
6398 
6399                IF (g_trace_on = 1) THEN
6400                   log_event('qty per container is '||v_qty_per_cont);
6401                END IF;
6402                 --#chk1
6403 
6404                IF( (v_return_status <> fnd_api.g_ret_sts_success ) OR
6405                    (v_qty_per_cont IS NULL) OR
6406                    (v_qty IS NULL) OR
6407                    (v_container_item_id IS NULL) OR
6408                    (v_qty <= 0) OR
6409                    (v_container_item_id <= 0) OR
6410                    (v_qty_per_cont <= 0) OR
6411                    --Bug 2478970 fix
6412                    l_valid_container = 'N'
6413                   ) THEN
6414 
6415                   IF (g_trace_on = 1) THEN
6416                      log_event('improper values returned by container_required_qty ');
6417                   END IF;
6418                ELSE
6419                   v_serial_control_code := inv_cache.item_rec.serial_number_control_code;
6420 
6421                   IF( (v_serial_control_code NOT IN (1,6) )
6422                   AND (Ceil(v_qty_per_cont) > v_qty_per_cont )) THEN
6423                      IF (g_trace_on = 1) THEN
6424                         log_event('cannot split serial controlled items to  fractions');
6425                         log_event('Please check the container item relationships');
6426                      END IF;
6427 
6428                      v_qty_per_cont := 0;
6429                      v_serial_control_code := NULL;
6430                   END IF;
6431 
6432                   v_serial_control_code := NULL;
6433 
6434                   v_tr_qty_per_cont := ROUND((v_qty_per_cont * inv_convert.inv_um_convert(
6435                                                                   p_item_id       => v1.inventory_item_id,
6436                                                                   p_from_uom_code => v_primary_uom_code,
6437                                                                   p_to_uom_code   => v1.transaction_uom
6438                                                                   )),
6439                                                                   5);
6440 
6441                      --invconv kkillams
6442                   IF v1.secondary_uom_code IS NOT NULL THEN
6443 
6444                   /*   v_sec_tr_qty_per_cont := ROUND((v_qty_per_cont * inv_convert.inv_um_convert(
6445                                                                      p_item_id       => v1.inventory_item_id,
6446                                                                      p_from_uom_code => v_primary_uom_code,
6447                                                                      p_to_uom_code   => v1.secondary_uom_code
6448                                                                      )),
6449                                                                      5);
6450                   */
6451 				--The secondary transaction qty is now calculated as a ratio coz in deviation cases the above logic does not work. See bug 10246979 and bug 10246444.
6452 				--The logic used below to calculate the sec qty is - [(Task total sec qty)/(Task total Primary qty)]*(Primary that can fix in 1 Carton)
6453 		        --Note that even with current logic there is a chance of creating task with qty more than requested but cannot workaround that as its a mathematical issue due to rounding diff b/w OM, INV and WMS
6454 				--The following fix would also not work for OPM customers if they start using this along with changes in allocation from bug 12978826.
6455 				--i.e Cartonization on top of allocation fix would start splitting in decimal which is problematic but at moment we do not have logic to take care of those cases..
6456 
6457 
6458 				  v_sec_tr_qty_per_cont := ROUND((v1.secondary_transaction_quantity / v1.primary_quantity) * v_tr_qty_per_cont,5);
6459 
6460 				  ELSE
6461 				  v_sec_tr_qty_per_cont := NULL;
6462 
6463                   END IF;
6464 				  if (g_trace_on = 1) then log_event('  Tasks total secondary qty :'||v1.secondary_transaction_quantity); END IF;
6465 				  if (g_trace_on = 1) then log_event('  Tasks total primary qty   :'||v1.primary_quantity); END IF;
6466 
6467                   if (g_trace_on = 1) then log_event(' Transaction qty per conatiner is '||v_tr_qty_per_cont); END IF;
6468                   if (g_trace_on = 1) then log_event(' Secondary Transaction qty per conatiner is '||v_sec_tr_qty_per_cont); END IF;
6469                END IF;
6470             ELSE
6471                IF (space_avail_for > 0) THEN
6472                   if (g_trace_on = 1) then log_event(' Space available for '||space_avail_for); END IF;
6473                   IF (v1.primary_quantity <= space_avail_for) THEN
6474                      if (g_trace_on = 1) then log_event(' Prim qty '||v1.primary_quantity|| ' <= '||space_avail_for); END IF;
6475                      space_avail_for := space_avail_for -  v1.primary_quantity;
6476 
6477                      IF( v1.content_lpn_id IS NULL) THEN
6478                         l_upd_qty_flag := 'Y';
6479                      ELSE
6480                         l_upd_qty_flag := 'N';
6481                      END IF;
6482                      update_mmtt
6483                      (p_transaction_temp_id => v1.transaction_temp_id,
6484                      p_primary_quantity   => v1.primary_quantity,
6485                      p_transaction_quantity  => v1.transaction_quantity,
6486                      p_secondary_quantity    => v1.secondary_transaction_quantity, --invconv kkillams
6487                      --p_LPN_string            => v_lpn_out,
6488                      p_lpn_id                => v_lpn_id,
6489                      p_container_item_id     => v_container_item_id,
6490                      p_parent_line_id        => NULL,
6491                      p_upd_qty_flag          => l_upd_qty_flag,
6492                      x_return_status             => l_return_status,
6493                      x_msg_count             => x_msg_count,
6494                      x_msg_data              => x_msg_data
6495                      );
6496 
6497                      v1.primary_quantity := 0;
6498                   ELSE
6499                      if (g_trace_on = 1) then log_event(' Prim qty '||v1.primary_quantity|| ' > '||space_avail_for); END IF;
6500 
6501                      tr_space_avail_for := ROUND((space_avail_for * inv_convert.inv_um_convert(
6502                                                                   p_item_id       => v1.inventory_item_id,
6503                                                                   p_from_uom_code => v_primary_uom_code,
6504                                                                   p_to_uom_code   => v1.transaction_uom
6505                                                                   )),
6506                                                                   5);
6507 
6508                      sec_tr_space_avail_for := NULL;
6509 
6510                      IF v1.secondary_uom_code IS NOT NULL THEN
6511 
6512                         sec_tr_space_avail_for := ROUND((space_avail_for * inv_convert.inv_um_convert(
6513                                                                      p_item_id       => v1.inventory_item_id,
6514                                                                      p_from_uom_code => v_primary_uom_code,
6515                                                                      p_to_uom_code   => v1.secondary_uom_code
6516                                                                      )),
6517                                                                      5);
6518 
6519                      END IF;
6520                      if (g_trace_on = 1) then log_event(' Tr space avail for '||tr_space_avail_for); END IF;
6521 
6522                      insert_mmtt
6523                      (p_transaction_temp_id   => v1.transaction_temp_id,
6524                      p_primary_quantity      => space_avail_for,
6525                      p_transaction_quantity  => tr_space_avail_for,
6526                      p_secondary_quantity    => sec_tr_space_avail_for, --invconv kkillams
6527                      --p_LPN_string            => v_lpn_out,
6528                      p_lpn_id                => v_lpn_id,
6529                      p_container_item_id     => v_container_item_id,
6530                      x_return_status      =>     l_return_status,
6531                      x_msg_count             => x_msg_count,
6532                      x_msg_data              => x_msg_data
6533                      );
6534 
6535                      v1.primary_quantity := v1.primary_quantity -  space_avail_for;
6536 
6537                      v1.transaction_quantity := ROUND((v1.primary_quantity * inv_convert.inv_um_convert(
6538                                                                   p_item_id       => v1.inventory_item_id,
6539                                                                   p_from_uom_code => v_primary_uom_code,
6540                                                                   p_to_uom_code   => v1.transaction_uom
6541                                                                   )),
6542                                                                   5);
6543 
6544                      IF v1.secondary_uom_code IS NOT NULL THEN
6545 
6546                         v1.secondary_transaction_quantity := ROUND((v1.primary_quantity * inv_convert.inv_um_convert(
6547                                                                      p_item_id       => v1.inventory_item_id,
6548                                                                      p_from_uom_code => v_primary_uom_code,
6549                                                                      p_to_uom_code   => v1.secondary_uom_code
6550                                                                      )),
6551                                                                      5);
6552 
6553                      END IF;
6554 
6555                      space_avail_for := 0;
6556 
6557                      IF (g_trace_on = 1) THEN
6558                         log_event('Prim qty '||v1.primary_quantity);
6559                         log_event('Tr qty   '||v1.transaction_quantity);
6560                         log_event('Sec Tr qty   '||v1.secondary_transaction_quantity); --invconv kkillams
6561                         log_event('Space Avail for '||space_avail_for);
6562                      END IF;
6563                   END IF;
6564                END IF;
6565             END IF;
6566 
6567             -- Condition #
6568             IF (v_return_status <> FND_API.g_ret_sts_success OR
6569                v_qty_per_cont IS NULL                       OR
6570                v_qty_per_cont <= 0                          OR
6571                v_container_item_id IS NULL                  OR
6572                v_tr_qty_per_cont  IS NULL                   OR
6573                v_tr_qty_per_cont <= 0                       OR
6574                v1.primary_quantity <= 0 ) THEN
6575 
6576                if (g_trace_on = 1) then log_event(' Container_Required_Qty - inc values');  END IF;
6577                    -- Condition #3a
6578                NULL;
6579             ELSE
6580                -- Condition #3b
6581                IF( v1.content_lpn_id IS NULL) THEN
6582                   l_upd_qty_flag := 'Y';
6583                ELSE
6584                   l_upd_qty_flag := 'N';
6585                END IF;
6586 
6587                v_qty := ceil( v1.primary_quantity/v_qty_per_cont);
6588 
6589                IF( MOD(v1.primary_quantity,v_qty_per_cont) = 0 ) THEN
6590                   space_avail_for := 0;
6591                ELSE
6592                   space_avail_for :=  v_qty_per_cont -  MOD(v1.primary_quantity,v_qty_per_cont);
6593                END IF;
6594 
6595                IF (g_trace_on = 1) THEN log_event('space avail for '||space_avail_for); END IF;
6596                /* Condition #4 */
6597                   IF(  (v1.primary_quantity <= v_qty_per_cont) OR ( v_qty = 1) ) THEN
6598                      IF (g_trace_on = 1) THEN log_event(' primary_quantity <= qty per conatiner or'||' NUMBER OF cont = 1'); END IF;
6599                      v_lpn_id := get_next_package_id;
6600                      IF (g_trace_on = 1) THEN
6601                         log_event(' Generated label Id '||v_lpn_id);
6602                      END IF;
6603 
6604                      update_mmtt
6605                      (        p_transaction_temp_id => v1.transaction_temp_id,
6606                               p_primary_quantity   => v1.primary_quantity,
6607                               p_transaction_quantity  => v1.transaction_quantity,
6608                               p_secondary_quantity    => v1.secondary_transaction_quantity, --invconv kkillams
6609                               --p_LPN_string            => v_lpn_out,
6610                               p_lpn_id                => v_lpn_id,
6611                               p_container_item_id     => v_container_item_id,
6612                               p_parent_line_id       => NULL,
6613                               p_upd_qty_flag         => l_upd_qty_flag ,
6614                               x_return_status              => l_return_status,
6615                               x_msg_count             => x_msg_count,
6616                               x_msg_data              => x_msg_data );
6617                   ELSE
6618                   /* Condition #4b */
6619 
6620                   v_loop := v_qty;
6621                   IF (g_trace_on = 1) THEN log_event(' NUMBER OF cont:'||v_qty); END IF;
6622                   --Bug2422193 fix moved this update to above as package
6623                   --ids need TO be generated IN the ORDER IN which rows
6624                   --are considered for cartonization
6625                   v_lpn_id := get_next_package_id;
6626                   IF (g_trace_on = 1) THEN
6627                      log_event('Generated label Id '||v_lpn_id);
6628                      log_event('calling update_mmtt');
6629                   END IF;
6630 
6631                   update_mmtt
6632                     (        p_transaction_temp_id => v1.transaction_temp_id,
6633                              p_primary_quantity   => v_qty_per_cont,
6634                              p_transaction_quantity  => v_tr_qty_per_cont,
6635                              p_secondary_quantity    => v_sec_tr_qty_per_cont, --invconv kkillams
6636                              --p_LPN_string            => v_lpn_out,
6637                              p_lpn_id                => v_lpn_id,
6638                              p_container_item_id     => v_container_item_id,
6639                              p_parent_line_id       => NULL,
6640                              p_upd_qty_flag         => l_upd_qty_flag,
6641                              x_return_status       => l_return_status,
6642                              x_msg_count             => x_msg_count,
6643                              x_msg_data              => x_msg_data );
6644 
6645                   v_loop := v_loop - 1;
6646 
6647                   -- Bug 2422193 fix
6648 
6649                   LOOP
6650                   EXIT WHEN v_loop < 2;
6651                      v_lpn_id := get_next_package_id;
6652                      IF (g_trace_on = 1) THEN
6653                         log_event(' Generated label Id '||v_lpn_id);
6654                         log_event(' calling insert mmtt');
6655                      END IF;
6656                      insert_mmtt
6657                        (        p_transaction_temp_id => v1.transaction_temp_id,
6658                                 p_primary_quantity   => v_qty_per_cont,
6659                                 p_transaction_quantity  => v_tr_qty_per_cont,
6660                                 p_secondary_quantity    => v_sec_tr_qty_per_cont, --invconv kkillams
6661                                 --p_LPN_string            => v_lpn_out,
6662                                 p_lpn_id                => v_lpn_id,
6663                                 p_container_item_id     => v_container_item_id,
6664                                 x_return_status            => l_return_status,
6665                                 x_msg_count             => x_msg_count,
6666                                 x_msg_data              => x_msg_data );
6667 
6668                      IF (g_trace_on = 1) THEN
6669                         log_event(' called insert mmtt');
6670                      END IF;
6671                      v_loop := v_loop - 1;
6672                   END LOOP;
6673 
6674                   v_lpn_id := get_next_package_id;
6675                   IF (g_trace_on = 1) THEN
6676                      log_event(' Generated label Id '||v_lpn_id);
6677                   END IF;
6678 
6679                   v_left_prim_quant :=  MOD(v1.primary_quantity,v_qty_per_cont);
6680                   v_left_tr_quant :=  MOD(v1.transaction_quantity,v_tr_qty_per_cont);
6681 
6682                   IF v1.secondary_uom_code IS NOT NULL THEN --invconv kkillams
6683                      v_sec_left_tr_quant :=  MOD(v1.secondary_transaction_quantity,v_sec_tr_qty_per_cont);
6684                   END IF;
6685 
6686                   IF(  v_left_prim_quant = 0 OR  v_left_tr_quant =0) THEN
6687                      v_left_prim_quant := v_qty_per_cont;
6688                      v_left_tr_quant   := v_tr_qty_per_cont;
6689                      IF v1.secondary_uom_code IS NOT NULL THEN --invconv kkillams
6690                         v_sec_left_tr_quant :=  v_sec_tr_qty_per_cont;
6691                      END IF;
6692                   END IF;
6693 
6694                   if (g_trace_on = 1) then log_event('calling insert mmtt'); END IF;
6695                   insert_mmtt
6696                     (        p_transaction_temp_id  => v1.transaction_temp_id,
6697                              p_primary_quantity     => v_left_prim_quant,
6698                              p_transaction_quantity => v_left_tr_quant,
6699                              p_secondary_quantity    => v_sec_left_tr_quant, --invconv kkillams
6700                              --p_LPN_string           => v_lpn_out,
6701                              p_lpn_id                => v_lpn_id,
6702                              p_container_item_id    => v_container_item_id,
6703                              x_return_status       => l_return_status,
6704                              x_msg_count            => x_msg_count,
6705                              x_msg_data             => x_msg_data
6706                              );
6707 
6708                   NULL;
6709                      -- Shipping API
6710                END IF;
6711                   -- Close Condition #4
6712             END IF;
6713                -- Close Condition #3
6714          END IF;
6715             -- for v1.allocated_lpn_id is not null
6716       END LOOP;
6717 
6718       IF wct_rows%isopen THEN
6719          CLOSE wct_rows;
6720       END IF;
6721 
6722       IF wct_sub_rows%isopen THEN
6723          CLOSE wct_sub_rows;
6724       END IF;
6725 
6726       IF (g_trace_on = 1) THEN
6727          log_event('out of the loops :)');
6728       END IF;
6729 
6730    x_return_status :=  fnd_api.g_ret_sts_success;
6731    EXCEPTION
6732    WHEN OTHERS THEN
6733       log_event('SQLERRM : '|| SQLERRM);
6734       log_event('SQLCODE : '|| SQLCODE);
6735    END cartonize_single_item;
6736 
6737    PROCEDURE rulebased_cartonization
6738    (
6739       x_return_status         OUT   NOCOPY VARCHAR2,
6740       x_msg_count             OUT   NOCOPY NUMBER,
6741       x_msg_data              OUT   NOCOPY VARCHAR2,
6742       p_out_bound             IN    VARCHAR2,
6743       p_org_id                IN    NUMBER,
6744       p_move_order_header_id  IN    NUMBER,
6745       p_input_for_bulk        IN    WMS_BULK_PICK.bulk_input_rec  DEFAULT NULL
6746    )
6747    IS
6748       v_cartonization_value     NUMBER;
6749       l_cartonize_sales_orders  VARCHAR2(1) :=NULL;
6750       l_cartonize_manufacturing VARCHAR2(1) :=NULL;
6751       l_auto_pick_confirm_flag  VARCHAR2(1) := 'N';
6752       l_return_status           VARCHAR2(1) := 'E';
6753       l_msg_count               NUMBER;
6754       l_move_order_type         NUMBER;
6755       l_msg_data                VARCHAR2(2000);
6756       wct_row                   WCT_ROW_TYPE;
6757       l_api_return_status       VARCHAR2(1);
6758       l_count1                  NUMBER;
6759       l_label_status            VARCHAR2(500);
6760       l_header_id               NUMBER;
6761       L_PREV_PACKAGE_ID  number;
6762       L_PREV_HEADER_ID  number;
6763       L_CURRENT_HEADER_ID number   :=  p_move_order_header_id;
6764        l_qty           NUMBER := NULL;
6765       l_tr_qty        NUMBER := NULL;
6766       l_sec_tr_qty    NUMBER := NULL;
6767       l_clpn_id       NUMBER := NULL;
6768       l_citem_id      NUMBER := NULL;
6769       l_package_id      NUMBER := NULL;
6770       l_upd_qty_flag VARCHAR2(1) := NULL;
6771       L_no_pkgs_gen   VARCHAR2(1);
6772       l_temp_id number;
6773       l_item_id number;
6774       l_packaging_return_status VARCHAR2(1);
6775 
6776       TYPE rules_table_type IS TABLE OF wms_selection_criteria_txn%rowtype;
6777       rules_table rules_table_type;
6778 
6779       CURSOR wct_rows IS
6780       SELECT wct.* FROM wms_cartonization_temp wct,
6781       mtl_txn_request_lines mtrl,
6782       mtl_secondary_inventories sub,
6783       mtl_parameters mtlp
6784       WHERE
6785       wct.move_order_line_id =mtrl.line_id
6786       AND mtrl.header_id = p_move_order_header_id
6787       AND wct.cartonization_id IS null
6788       AND mtlp.organization_id = wct.organization_id
6789       AND sub.organization_id = wct.organization_id
6790       AND wct.cartonization_id IS NULL
6791       AND wct.transfer_lpn_id IS NULL
6792       AND sub.secondary_inventory_name = wct.subinventory_code
6793       AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
6794 	  OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
6795 	  OR (NVL(mtlp.cartonization_flag,-1) = 4)
6796 	  OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
6797 	  )
6798       ORDER BY wct.move_order_line_id,
6799       wct.subinventory_code,
6800       wct.inventory_item_id, Abs(wct.transaction_temp_id);
6801 
6802       CURSOR OB_LBPRT IS
6803       SELECT DISTINCT mmtt.cartonization_id FROM
6804       --2513907 fix
6805       wms_cartonization_temp mmtt,
6806       --mtl_material_transactions_temp mmtt,
6807       mtl_txn_request_lines mtrl WHERE
6808       mmtt.move_order_line_id = mtrl.line_id
6809       AND mtrl.header_id      = p_move_order_header_id
6810       AND mmtt.cartonization_id IS NOT NULL
6811       ORDER BY mmtt.cartonization_id;
6812 
6813 
6814       CURSOR packages(p_hdr_id NUMBER) IS
6815       SELECT
6816       transaction_temp_id,
6817       inventory_item_id,
6818       primary_quantity,
6819       transaction_quantity,
6820       secondary_transaction_quantity, --invconv kkillams
6821       content_lpn_id,
6822       container_item_id,
6823       cartonization_id
6824       FROM
6825       wms_cartonization_temp
6826       WHERE
6827       transaction_header_id = p_hdr_id
6828       order by cartonization_id;
6829 
6830       CURSOR opackages(p_hdr_id NUMBER) IS
6831       SELECT
6832       wct.transaction_temp_id,
6833       wct.inventory_item_id,
6834       wct.primary_quantity,
6835       wct.transaction_quantity,
6836       wct.secondary_transaction_quantity, --invconv kkillams
6837       wct.content_lpn_id,
6838       wct.container_item_id,
6839       wct.cartonization_id
6840       FROM
6841       wms_cartonization_temp wct,
6842       mtl_txn_request_lines mtrl
6843       WHERE
6844       wct.move_order_line_id = mtrl.line_id AND
6845       mtrl.header_id = p_hdr_id
6846       order by wct.cartonization_id;
6847 
6848       l_count number;
6849       l_counter number := 1;
6850       l_return_type_id number;
6851 
6852    BEGIN
6853 
6854       IF (g_trace_on = 1) THEN
6855          log_event('In RULEBASED_CARTONIZATION()');
6856          log_event('Populating wms_cartonization_temp from MMTT rows');
6857       END IF;
6858       g_cartonize_pick_slip := 'N';
6859 
6860       IF (g_trace_on = 1) THEN
6861          log_event('Auto Pick Confirm Flag is : '|| WMS_CARTNZN_PUB.g_auto_pick_confirm_flag);
6862       END IF;
6863 
6864       IF WMS_CARTNZN_PUB.g_auto_pick_confirm_flag = 'N' THEN
6865 
6866          IF (g_trace_on = 1) THEN
6867             log_event(' move order header id '||p_move_order_header_id);
6868          END IF;
6869 
6870 /* -- bug 10202064
6871          IF (g_trace_on = 1) THEN
6872             log_event('Calling wms_rule_pvt.assigntts for task type assignment');
6873          END IF;
6874 
6875          wms_rule_pvt.assigntts(
6876                           p_api_version          => 1.0
6877                         , p_move_order_header_id => p_move_order_header_id
6878                         , x_return_status        => l_return_status
6879                         , x_msg_count            => l_msg_count
6880                         , x_msg_data             => l_msg_data
6881                         );
6882 
6883          IF (g_trace_on = 1) THEN
6884             log_event(' Task type assignment returned with status :' ||l_return_status);
6885          END IF;
6886 
6887          IF l_return_status <> fnd_api.g_ret_sts_success THEN
6888             log_event(' Task type assignment failed');
6889          END IF;
6890 */ -- bug 10202064
6891 
6892          l_return_status := fnd_api.g_ret_sts_success;
6893 
6894          IF (p_move_order_header_id <> -1 ) THEN
6895 
6896             log_event('Calling wms_rule_pvt.assign_operation_plan for Operation Plan assignment' );
6897 
6898             wms_rule_pvt.assign_operation_plans(
6899                                              p_api_version => 1.0
6900                                            , p_move_order_header_id => p_move_order_header_id
6901                                            , x_return_status        => l_return_status
6902                                            , x_msg_count            => l_msg_count
6903                                            , x_msg_data             => l_msg_data);
6904 
6905             IF l_return_status <> fnd_api.g_ret_sts_success THEN
6906                error_code := 'CARTONIZE 45';
6907                -- insert the message into the stack
6908                log_event(' Operation plan assignment failed');
6909             ELSE
6910                log_event(' Operation plan assignment returned success');
6911             END IF;
6912          END IF;
6913 
6914          IF (g_trace_on = 1) THEN
6915             log_event('Calling INV_PICK_RELEASE_PUB.assign_pick_slip_number ');
6916          END IF;
6917 
6918          INV_PICK_RELEASE_PUB.assign_pick_slip_number(
6919                                                   x_return_status         => l_return_status
6920                                                 , x_msg_count             => x_msg_count
6921                                                 , x_msg_data              => x_msg_data
6922                                                 , p_move_order_header_id  => p_move_order_header_id
6923                                                 , p_ps_mode               => null
6924                                                 , p_grouping_rule_id      => null
6925                                                 , p_allow_partial_pick    => null
6926                                                 );
6927          IF (g_trace_on = 1) THEN
6928             log_event('assign_pick_slip_number returns'||l_return_status);
6929          END IF;
6930 
6931       END IF; -- l_auto_pick_confirm_flag = 'N'
6932 
6933       IF (g_trace_on = 1) THEN
6934          log_event('Populating wms_cartonization_temp from MMTT rows');
6935       END IF;
6936 
6937       create_wct(
6938            p_move_order_header_id => p_move_order_header_id
6939          , p_transaction_header_id => NULL
6940          , p_input_for_bulk =>p_input_for_bulk
6941          );
6942 
6943       IF (g_trace_on = 1) THEN
6944          SELECT count(*)
6945          INTO l_count
6946          FROM wms_cartonization_temp;
6947          log_event('NUMBER OF ROWS INSERTED IN WCT : '|| l_count);
6948       END IF;
6949 
6950       IF (g_trace_on = 1) THEN
6951          log_event('Goint to cartonize');
6952       END IF;
6953 
6954       SELECT * BULK COLLECT
6955         INTO rules_table
6956         FROM wms_selection_criteria_txn
6957        WHERE rule_type_code = 12
6958          AND from_organization_id = p_org_id   -- Bug : 6962305
6959          AND enabled_flag = 1
6960        ORDER BY sequence_number;
6961 
6962       FOR i in rules_table.FIRST .. rules_table.LAST LOOP
6963 
6964          IF rules_table(i).return_type_id = 1 THEN
6965 
6966             cartonize_single_item
6967             (
6968                x_return_status         => l_api_return_status,
6969                x_msg_count             => x_msg_count,
6970                x_msg_data              => x_msg_data,
6971                p_out_bound             => 'Y',
6972                p_org_id                => rules_table(i).from_organization_id,
6973                p_move_order_header_id  => p_move_order_header_id,
6974                p_subinventory_name     => rules_table(i).from_subinventory_name
6975             );
6976 
6977          ELSIF rules_table(i).return_type_id = 2 THEN
6978 
6979             cartonize_mixed_item
6980             (
6981                x_return_status         => l_api_return_status,
6982                x_msg_count             => x_msg_count,
6983                x_msg_data              => x_msg_data,
6984                p_out_bound             => 'Y',
6985                p_org_id                => rules_table(i).from_organization_id,
6986                p_move_order_header_id  => p_move_order_header_id,
6987                p_transaction_header_id => NULL,
6988                p_subinventory_name     => rules_table(i).from_subinventory_name,
6989                p_pack_level            => 0,
6990                p_stop_level            => 0
6991             );
6992 
6993             pack_level := 0;
6994 
6995          ELSIF rules_table(i).return_type_id = 3 THEN
6996 
6997             g_cartonize_pick_slip := 'Y';
6998             cartonize_pick_slip
6999             (
7000                 p_org_id                => rules_table(i).from_organization_id,
7001                 p_move_order_header_id  => p_move_order_header_id,
7002                 p_subinventory_name     => rules_table(i).from_subinventory_name,
7003                 x_return_status         => l_return_status
7004              );
7005 
7006          ELSIF rules_table(i).return_type_id = 4 THEN
7007 
7008             cartonize_customer_logic
7009             (
7010                 p_org_id                => rules_table(i).from_organization_id,
7011                 p_move_order_header_id  => p_move_order_header_id,
7012                 p_subinventory_name     => rules_table(i).from_subinventory_name,
7013                 x_return_status         => l_return_status
7014              );
7015 
7016          END IF;
7017 
7018          -- Do not consider WCT rows used for multi-level packaging history
7019          SELECT count(1)
7020          INTO l_count
7021          FROM wms_cartonization_temp
7022          WHERE cartonization_id IS NULL
7023          AND transaction_header_id >= 0;
7024 
7025          IF l_count = 0 THEN
7026             EXIT;
7027          END IF;
7028 
7029       END LOOP;
7030 
7031       -- If there are any more rows left for cartonization for first level, cartonize through default logic
7032       IF l_count > 0 THEN
7033 
7034          cartonize_default_logic
7035          (
7036             p_org_id                => p_org_id,
7037             p_move_order_header_id  => p_move_order_header_id,
7038             p_out_bound             => 'Y',
7039             x_return_status         => x_return_status,
7040             x_msg_count             => x_msg_count,
7041             x_msg_data              => x_msg_data
7042          );
7043 
7044       END IF;
7045 
7046       IF (g_trace_on = 1) THEN
7047          log_event('Done with FIRST LEVEL OF CARTONIZATION, inserting packaging history');
7048       END IF;
7049 
7050       pack_level := 0;
7051 
7052       insert_ph
7053       (
7054          p_move_order_header_id  => p_move_order_header_id,
7055          p_current_header_id     => p_move_order_header_id,
7056          x_return_status         => l_packaging_return_status
7057       );
7058 
7059       IF (g_trace_on = 1) THEN
7060          log_event('After Insert_Ph');
7061       END IF;
7062 
7063       IF (g_trace_on = 1) THEN
7064          log_event('Done with FIRST LEVEL OF CARTONIZATION');
7065          log_event('Calling CARTONIZE_MIXED_ITEM for doing MULTI-LEVEL CARTONIZATION');
7066       END IF;
7067 
7068       cartonize_mixed_item
7069       (
7070          x_return_status         => l_api_return_status,
7071          x_msg_count             => x_msg_count,
7072          x_msg_data              => x_msg_data,
7073          p_out_bound             => 'Y',
7074          p_org_id                => p_org_id,
7075          p_move_order_header_id  => p_move_order_header_id,
7076          p_transaction_header_id => NULL,
7077          p_pack_level            => 1
7078       );
7079 
7080       l_header_id := p_move_order_header_id;
7081 
7082       IF (g_trace_on = 1) THEN
7083          log_event('Calling Generate_LPNs for header id ** '||l_header_id);
7084       END IF;
7085 
7086       BEGIN
7087           generate_lpns(p_header_id => l_header_id,
7088                         p_organization_id => p_org_id);
7089       EXCEPTION
7090       WHEN OTHERS THEN
7091          IF (g_trace_on = 1) THEN
7092             log_event('not erroring out since the mode is Pick release ');
7093          END IF;
7094          RAISE fnd_api.g_exc_error;
7095       END;
7096 
7097       DELETE wms_cartonization_temp
7098       WHERE transaction_header_id < 0;
7099 
7100       log_event('Out of cartonization logic going to print labels');
7101 
7102       IF( outbound = 'Y') THEN
7103          OPEN OB_LBPRT;
7104          LOOP
7105             FETCH OB_LBPRT INTO lpns_generated_tb(l_counter);
7106             EXIT WHEN OB_LBPRT%notfound;
7107 
7108             IF (g_trace_on = 1) THEN
7109                log_event(' print label for lpn '|| lpns_generated_tb(l_counter));
7110             END IF;
7111             l_counter := l_counter + 1;
7112          END LOOP;
7113          IF OB_LBPRT%ISOPEN THEN
7114             CLOSE OB_LBPRT;
7115          END IF;
7116       END IF;
7117 
7118       IF (g_trace_on = 1) THEN
7119          log_event('lpns_generated_tb.count : '|| lpns_generated_tb.count);
7120       END IF;
7121 
7122       IF (g_trace_on = 1) THEN log_event('Auto pick confirm flag : '|| l_auto_pick_confirm_flag); END IF;
7123 
7124       IF l_auto_pick_confirm_flag = 'Y' THEN
7125          if (g_trace_on = 1) then log_event('Auto pick confirm is ON - Not calling task consolidation - splitting'); END IF;
7126       ELSE
7127 
7128          IF (g_trace_on = 1)  then -- log_event('PATCHSET J-- BULK PICKING --START');
7129             log_event('Calling consolidate_bulk_tasks_for_so....');
7130             log_event('move order header id '||p_move_order_header_id);
7131          END IF;
7132 
7133          wms_task_dispatch_engine.consolidate_bulk_tasks_for_so
7134             (p_api_version => 1.0,
7135             x_return_status => l_return_status,
7136             x_msg_count => x_msg_count,
7137             x_msg_data => x_msg_data,
7138             p_move_order_header_id => p_move_order_header_id);
7139 
7140          IF(l_return_status <> fnd_api.g_ret_sts_success ) THEN
7141          -- we don't want to exit if there is any error here, for example data error or something
7142          IF (g_trace_on = 1) THEN log_event(' consolidate_bulk_tasks_for_so returns '||l_return_status); END IF;
7143          END IF;
7144          IF (g_trace_on = 1) THEN log_event(' calling ins_wct_rows_into_mmtt after consolidation'); END IF;
7145 
7146          ins_wct_rows_into_mmtt( p_m_o_h_id           => p_move_order_header_id,
7147          p_outbound           => 'Y',
7148          x_return_status      => l_return_status,
7149          x_msg_count          => x_msg_count,
7150          x_msg_data           => x_msg_data);
7151 
7152          -- since the return value is not set up inside above procedure, so no need to check it
7153          -- but above procedure will throw unexpected error in case of errors, so this API will capture the
7154          -- error.
7155 
7156          IF(l_return_status <> fnd_api.g_ret_sts_success ) THEN
7157             --Push the message into the stack
7158             IF (g_trace_on = 1) THEN log_event(' consolidate_bulk_tasks returns '||l_return_status); END IF;
7159          END IF;
7160 
7161 
7162          --start bug 10202064: added assigntts call after bulking, so that user task type appears for parent tasks.
7163          -- wms_cartnzn_pub.assigntts only assigns task type to the parent tasks, not the child tasks.
7164        --  if (g_trace_on = 1) then log_event(' calling assigntts for bug 10202064'); END IF;
7165                   WMS_CARTNZN_PUB.assigntts(
7166                                                   p_api_version => 1.0,
7167                                                   p_move_order_header_id =>  p_move_order_header_id,
7168                                                   x_return_status    => l_return_status,
7169                                                   x_msg_count        => l_msg_count,
7170                                                   x_msg_data         => l_msg_data);
7171 
7172                   IF l_return_status <> fnd_api.g_ret_sts_success THEN
7173                        error_code := 'CARTONIZE 40.1';
7174                        if (g_trace_on = 1) then log_event(' Task type assignment failed'); END IF;
7175                   END IF;
7176          -- end bug 10202064
7177 
7178          IF (g_trace_on = 1) THEN log_event(' calling task splitting' );  END IF;
7179 
7180          wms_task_dispatch_engine.split_tasks
7181          (
7182             p_api_version => 1.0,
7183             x_return_status => l_return_status,
7184             x_msg_count => x_msg_count,
7185             x_msg_data => x_msg_data,
7186             p_move_order_header_id => p_move_order_header_id
7187          );
7188 
7189          IF(l_return_status <> fnd_api.g_ret_sts_success ) THEN
7190          --Push the message into the stack
7191             IF (g_trace_on = 1) THEN log_event(' split_tasks returns '||l_return_status); END IF;
7192          END IF;
7193       END IF;
7194 
7195       --Bug 6873966
7196       --moved the label printing call after stamping the cartonization_id on the mmtt
7197       IF (lpns_generated_tb.COUNT > 0) THEN
7198 
7199          IF packaging_mode = wms_cartnzn_pub.PR_pKG_mode THEN
7200             IF (g_trace_on = 1) THEN
7201                log_event('wms_cartnzn_pub before  inv_label.print_label ');
7202             END IF;
7203             l_return_status := fnd_api.g_ret_sts_success;
7204 
7205             inv_label.print_label
7206                            (
7207                              x_return_status => l_return_status
7208                            , x_msg_count => l_msg_count
7209                            , x_msg_data  => l_msg_data
7210                            , x_label_status  => l_label_status
7211                            , p_api_version   => 1.0
7212                            , p_print_mode => 1
7213                            , p_business_flow_code => 22
7214                            , p_transaction_id => lpns_generated_tb
7215                            );
7216 
7217             IF (g_trace_on = 1) THEN
7218                log_event('wms_cartnzn_pub after inv_label.print_label ');
7219             END IF;
7220 
7221             IF l_return_status <> fnd_api.g_ret_sts_success THEN
7222                FND_MESSAGE.SET_NAME('INV', 'INV_RCV_CRT_PRINT_LAB_FAIL');  -- MSGTBD
7223                FND_MSG_PUB.ADD;
7224                IF (g_trace_on = 1) THEN
7225                   log_event('wms_cartnzn_pub inv_label.print_label FAILED;'|| to_char(sysdate, 'YYYY-MM-DD HH:DD:SS'));
7226                END IF;
7227             END IF;
7228          END IF; -- end packaging_mode in 1
7229       ELSE
7230          IF (g_trace_on = 1) THEN
7231             log_event('COULD NOT CARTONIZE ANY OF THE LINES');
7232          END IF;
7233       END IF;
7234 
7235    EXCEPTION
7236    WHEN OTHERS THEN
7237       log_event('Exception in rulebased cartonization');
7238       log_event('SQL Error Message :' || SQLERRM);
7239       log_event('SQL Error Code :' || SQLCODE);
7240 --         v_cart_value := NULL;
7241    END rulebased_cartonization;
7242 
7243    PROCEDURE cartonize_mixed_item (
7244                       x_return_status         OUT   NOCOPY VARCHAR2,
7245                       x_msg_count             OUT   NOCOPY NUMBER,
7246                       x_msg_data              OUT   NOCOPY VARCHAR2,
7247                       p_out_bound             IN    VARCHAR2,
7248                       p_org_id                IN    NUMBER,
7249                       p_move_order_header_id  IN    NUMBER,
7250                       p_transaction_header_id IN    NUMBER,
7251                       p_disable_cartonization IN    VARCHAR2 DEFAULT 'N',
7252                       p_subinventory_name     IN    VARCHAR2 DEFAULT NULL,
7253                       p_stop_level            IN    NUMBER DEFAULT NULL,
7254                       p_pack_level            IN    NUMBER
7255                       )
7256    IS
7257       l_api_name     CONSTANT VARCHAR2(30) := 'cartonize_mixed_item';
7258       l_api_version  CONSTANT NUMBER       := 1.0;
7259       v1 WCT_ROW_TYPE;
7260       cartonization_profile  VARCHAR2(1)   := 'Y';
7261       v_cart_value NUMBER;
7262       v_container_item_id NUMBER:= NULL;
7263       v_qty NUMBER:= -1;
7264       v_qty_per_cont NUMBER := -1;
7265       v_tr_qty_per_cont NUMBER:= -1;
7266       v_sec_tr_qty_per_cont NUMBER:= -1;
7267       v_lpn_out VARCHAR2(30) := ' ';
7268       v_primary_uom_code VARCHAR2(3);
7269       v_loop NUMBER := 0;
7270       v_process_id NUMBER := 0;
7271       v_ttemp_id NUMBER := 0;
7272       v_lpn_id_out NUMBER := 0;
7273       v_lpn_id NUMBER := 0;
7274       ret_value   NUMBER := 29;
7275       v_return_status VARCHAR2(1);
7276       v_left_prim_quant NUMBER;
7277       v_left_tr_quant NUMBER;
7278       v_sec_left_tr_quant NUMBER;
7279       v_sublvlctrl VARCHAR2(1) := g_sublvlctrl; --Bug#7168367.Get form global variable.
7280       l_header_id     NUMBER := NULL;
7281       --Bug 2720653 fix
7282       --l_lpn_id_tbl inv_label.transaction_id_rec_type;
7283       l_return_status VARCHAR2(1) := fnd_api.g_ret_sts_success;
7284       l_msg_count NUMBER;
7285       l_msg_data VARCHAR2(400);
7286       l_progress VARCHAR2(10);
7287       l_label_status VARCHAR2(500);
7288       l_counter NUMBER := 0;
7289       v_prev_move_order_line_id NUMBER := 0;
7290       space_avail_for NUMBER := 0;
7291       tr_space_avail_for NUMBER := 0;
7292       sec_tr_space_avail_for NUMBER := 0; --invconv kkillams
7293       curr_temp_id mtl_material_transactions_temp.transaction_temp_id%TYPE ;
7294       v_lot_control_code NUMBER := NULL;
7295       v_serial_control_code NUMBER := NULL;
7296       api_table_index NUMBER := 0;
7297       l_current_header_id  NUMBER := NULL;
7298       l_stop_level    NUMBER := -1;
7299       v_prev_item_id  NUMBER := NULL;
7300       l_prev_package_id NUMBER := NULL;
7301       l_temp_id       NUMBER := NULL;
7302       l_item_id       NUMBER := NULL;
7303 
7304       l_qty           NUMBER := NULL;
7305       l_tr_qty        NUMBER := NULL;
7306       l_sec_tr_qty    NUMBER := NULL;
7307       l_clpn_id       NUMBER := NULL;
7308       l_citem_id      NUMBER := NULL;
7309       l_package_id      NUMBER := NULL;
7310       l_upd_qty_flag VARCHAR2(1) := NULL;
7311       l_prev_header_id NUMBER := NULL;
7312       L_no_pkgs_gen   VARCHAR2(1);
7313       l_prev_condition  VARCHAR2(1);
7314       l_revision_code   VARCHAR2(1);
7315       l_lot_code        VARCHAR2(1);
7316       l_serial_code     VARCHAR2(1);
7317       l_is_revision_control  BOOLEAN;
7318       l_is_lot_control       BOOLEAN;
7319       l_is_serial_control    BOOLEAN;
7320       l_rqoh NUMBER;
7321       l_qr   NUMBER;
7322       l_qs   NUMBER;
7323       l_atr  NUMBER;
7324       l_att  NUMBER;
7325       l_qoh  NUMBER;
7326       l_lpn_fully_allocated  VARCHAR2(1) :='N';
7327       percent_fill_basis         VARCHAR2(1) :='W';
7328       l_valid_container VARCHAR2(1) := 'Y';
7329 
7330       l_cartonize_sales_orders VARCHAR2(1)  :=NULL;
7331       l_cartonize_manufacturing VARCHAR2(1) :=NULL;
7332       l_move_order_type   NUMBER;
7333       l_pack_level        NUMBER := 0;
7334 
7335        CURSOR wct_rows IS
7336           SELECT wct.* FROM wms_cartonization_temp wct,
7337             mtl_txn_request_lines mtrl,
7338             mtl_secondary_inventories sub,
7339             mtl_parameters mtlp
7340             WHERE
7341             wct.move_order_line_id =mtrl.line_id
7342             AND mtrl.header_id = p_move_order_header_id
7343             AND wct.cartonization_id IS null
7344             AND mtlp.organization_id = wct.organization_id
7345             AND sub.organization_id = wct.organization_id
7346             AND wct.cartonization_id IS NULL
7347             AND wct.transfer_lpn_id IS NULL
7348             AND sub.secondary_inventory_name = wct.subinventory_code
7349             AND wct.subinventory_code = NVL(p_subinventory_name,wct.subinventory_code)
7350             AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
7351 		OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
7352 		OR (NVL(mtlp.cartonization_flag,-1) = 4)
7353 		OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
7354 		)
7355             ORDER BY wct.move_order_line_id,
7356                      wct.inventory_item_id,
7357                      Abs(wct.transaction_temp_id);
7358 
7359             CURSOR OB_LBPRT IS
7360                SELECT DISTINCT mmtt.cartonization_id FROM
7361                  --2513907 fix
7362                wms_cartonization_temp mmtt,
7363              --mtl_material_transactions_temp mmtt,
7364              mtl_txn_request_lines mtrl WHERE
7365              mmtt.move_order_line_id = mtrl.line_id
7366              AND mtrl.header_id      = p_move_order_header_id
7367              AND mmtt.cartonization_id IS NOT NULL
7368                ORDER BY mmtt.cartonization_id;
7369 
7370          CURSOR bpack_rows(p_hdr_id NUMBER) IS
7371             SELECT * FROM
7372               wms_cartonization_temp
7373               WHERE
7374               transaction_header_id = p_hdr_id
7375               AND cartonization_id IS NULL
7376               AND transfer_lpn_id IS NULL
7377               order by  move_order_line_id,
7378               decode(content_lpn_id,null,inventory_item_id,
7379                                   decode(sign(p_hdr_id),
7380                                          -1,
7381                                          inventory_item_id,
7382                                          wms_cartnzn_pub.Get_LPN_ItemId(content_lpn_id)
7383            ) ),Abs(transaction_temp_id);
7384 
7385 
7386          CURSOR packages(p_hdr_id NUMBER) IS
7387             SELECT
7388              transaction_temp_id,
7389              inventory_item_id,
7390              primary_quantity,
7391              transaction_quantity,
7392              secondary_transaction_quantity, --invconv kkillams
7393              content_lpn_id,
7394              container_item_id,
7395              cartonization_id
7396              FROM
7397              wms_cartonization_temp
7398              WHERE
7399              transaction_header_id = p_hdr_id
7400              order by cartonization_id;
7401 
7402         CURSOR opackages(p_hdr_id NUMBER) IS
7403            SELECT
7404              wct.transaction_temp_id,
7405              wct.inventory_item_id,
7406              wct.primary_quantity,
7407              wct.transaction_quantity,
7408              wct.secondary_transaction_quantity, --invconv kkillams
7409              wct.content_lpn_id,
7410              wct.container_item_id,
7411              wct.cartonization_id
7412              FROM
7413              wms_cartonization_temp wct,
7414              mtl_txn_request_lines mtrl
7415              WHERE
7416              wct.move_order_line_id = mtrl.line_id AND
7417              mtrl.header_id = p_hdr_id
7418              order by wct.cartonization_id;
7419 
7420              l_cartonization_profile  VARCHAR2(1)   := 'Y';
7421    BEGIN
7422 
7423       --setting the global variable for trace
7424       g_trace_on := fnd_profile.value('INV_DEBUG_TRACE');
7425       -- Setting the global variable for packaging
7426       --      packaging_mode := p_packaging_mode;
7427       -- Setting the global variable for sequence
7428       --sequence_id := 1;
7429 
7430       --      l_current_header_id := p_move_order_header_id;
7431 
7432       -- For subinventory restriction in Rules Workbench
7433 
7434       IF (g_trace_on = 1) THEN log_event(' In CARTONIZE_MIXED_ITEM()'); END IF;
7435 
7436       IF p_subinventory_name IS NOT NULL THEN
7437 
7438          IF (g_trace_on = 1) THEN log_event(' Updating wct with dummy cartonization id for sub other than : ' || p_subinventory_name); END IF;
7439 
7440          UPDATE wms_cartonization_temp
7441             SET cartonization_id = -99999
7442               , container_item_id = -99999
7443           WHERE subinventory_code <> p_subinventory_name
7444             AND organization_id = p_org_id
7445             AND cartonization_id IS NULL;
7446 
7447       END IF;
7448 
7449       IF p_pack_level = 0 THEN
7450          curr_header_id_for_mixed := p_move_order_header_id;
7451       END IF;
7452 
7453       outbound := 'Y';
7454       cartonization_profile := 'Y';
7455 
7456       IF( cartonization_profile = 'Y') THEN
7457 
7458          pack_level := p_pack_level;
7459 
7460          LOOP
7461 
7462             IF (g_trace_on = 1) THEN log_event(' pack_level '||pack_level ||' l_stop_level'||l_stop_level); END IF;
7463 
7464             EXIT WHEN ( (pack_level >= l_stop_level) AND (l_stop_level <> -1));
7465 
7466             IF p_stop_level IS NOT NULL THEN
7467                l_stop_level := p_stop_level;
7468                IF (g_trace_on = 1) THEN
7469                   log_event('This procedure called for FIRST LEVEL cartonization only');
7470                END IF;
7471             ELSE
7472                IF (g_trace_on = 1) THEN
7473                   log_event('This procedure called for MULTI LEVEL cartonization');
7474                END IF;
7475             END IF;
7476 
7477             v_prev_item_id := -1;
7478             v_prev_move_order_line_id  := -1;
7479 
7480              --bpack_rows should select only rows with transaction_header_id = l_current_header_id
7481             if (g_trace_on = 1) then log_event(' opening  cusror  hdr id'||l_current_header_id); END IF;
7482 
7483 
7484             IF( (outbound = 'Y') AND (pack_level = 0)) THEN
7485                error_code := 'CARTONIZE 70';
7486                if (g_trace_on = 1) then log_event('opening wct_rows'); END IF;
7487                OPEN wct_rows;
7488                if (g_trace_on = 1) then log_event('opened bpack rows'); END IF;
7489             ELSE
7490                error_code := 'CARTONIZE 90';
7491                if (g_trace_on = 1) then log_event('opening bpack_rows'); END IF;
7492                OPEN bpack_rows(l_current_header_id);
7493                if (g_trace_on = 1) then log_event(' Opened bpack rows'); END IF;
7494             END IF;
7495 
7496             IF (g_trace_on = 1) THEN
7497                log_event(' l_stop_level '||l_stop_level);
7498                log_event(' l_current_level '||pack_level);
7499                log_event(' l_current_header_id '||l_current_header_id);
7500             END IF;
7501 
7502             IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
7503                ret_value:= do_cartonization(p_move_order_header_id,0,outbound,
7504                                           v_sublvlctrl,WMS_CARTNZN_PUB.g_percent_fill_basis);
7505 
7506             ELSE
7507                IF (g_trace_on = 1) THEN
7508                   log_event('in else for cartonization');
7509                   log_event('passing header id '||l_current_header_id);
7510                   log_event('passing outbound '||outbound);
7511                   log_event('passing Sub level Control'||v_sublvlctrl);
7512                END IF;
7513                ret_value:= do_cartonization(0,curr_header_id_for_mixed,outbound,
7514                                           v_sublvlctrl,WMS_CARTNZN_PUB.g_percent_fill_basis);
7515 
7516             END IF;
7517 
7518             IF (g_trace_on = 1) THEN
7519                log_event(' cartonization returned'|| ret_value);
7520                log_event(' calling split_lot_serials ');
7521             END IF;
7522 
7523             split_lot_serials(p_org_id);
7524 
7525             IF (g_trace_on = 1) then log_event(' pack level'); END IF;
7526 
7527             IF pack_level <> 0 THEN
7528 
7529                IF (g_trace_on = 1) then log_event(' Populating Packaging History Table'); END IF;
7530 
7531                l_prev_package_id := -1;
7532 
7533              --  l_prev_header_id := l_current_header_id;
7534 
7535                prev_header_id_for_mixed := curr_header_id_for_mixed;
7536 
7537                l_prev_header_id := prev_header_id_for_mixed;
7538 
7539                IF (g_trace_on = 1) then log_event(' prev header id '||l_prev_header_id ); END IF;
7540       --         l_current_header_id := get_next_header_id;
7541                curr_header_id_for_mixed := get_next_header_id;
7542                l_current_header_id := curr_header_id_for_mixed;
7543                IF (g_trace_on = 1) then log_event(' current_header_id '||curr_header_id_for_mixed); END IF;
7544 
7545                error_code := 'CARTONIZE 225';
7546                t_lpn_alloc_flag_table.delete;
7547                error_code := 'CARTONIZE 226';
7548 
7549                IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
7550                   error_code := 'CARTONIZE 227';
7551                    if (g_trace_on = 1) then log_event('Opening OPACKAGES cursor for l_prev_header_id'||l_prev_header_id); END IF;
7552                   OPEN opackages(l_prev_header_id);
7553                ELSE
7554                    if (g_trace_on = 1) then log_event('Opening PACKAGES cursor for l_prev_header_id'||l_prev_header_id); END IF;
7555                   error_code := 'CARTONIZE 228';
7556                   OPEN packages(l_prev_header_id);
7557                END IF;
7558 
7559                l_no_pkgs_gen := 'Y';
7560                error_code := 'CARTONIZE 230';
7561 
7562                LOOP
7563 
7564                   if (g_trace_on = 1) then log_event('Fetching Packages cursor '); END IF;
7565                   error_code := 'CARTONIZE 240';
7566 
7567                   IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
7568                       if (g_trace_on = 1) then log_event('OPACKAGES'); END IF;
7569                      FETCH opackages INTO l_temp_id,l_item_id, l_qty, l_tr_qty,l_sec_tr_qty, l_clpn_id, l_citem_id, l_package_id;
7570                      EXIT WHEN opackages%notfound;
7571                   ELSE
7572                                   if (g_trace_on = 1) then log_event('PACKAGES'); END IF;
7573                      FETCH packages INTO l_temp_id,l_item_id, l_qty, l_tr_qty, l_sec_tr_qty, l_clpn_id, l_citem_id, l_package_id;
7574                      EXIT WHEN packages%notfound;
7575                   END IF;
7576 
7577                   IF (g_trace_on = 1) THEN
7578                      log_event('temp_id '||l_temp_id );
7579                      log_event('item_id  '||l_item_id );
7580                      log_event('qty  '||l_qty );
7581                      log_event('tr_qty '||l_tr_qty);
7582                      log_event('sec_tr_qty '||l_sec_tr_qty);
7583                      log_event('clpn_id '||l_clpn_id);
7584                      log_event('citem_id '||l_citem_id);
7585                      log_event('package_id '||l_package_id);
7586                   END IF;
7587 
7588                   IF( l_package_id IS NOT NULL ) THEN
7589 
7590                      l_no_pkgs_gen := 'N';
7591                      IF( l_package_id <> l_prev_package_id ) THEN
7592                         l_prev_package_id := l_package_id;
7593                         IF (g_trace_on = 1) then log_event(' Inserting a new row for package '||l_package_id); END IF;
7594                            insert_mmtt
7595                            (p_transaction_temp_id  => l_temp_id,
7596                            p_primary_quantity     => l_qty,
7597                            p_transaction_quantity => l_tr_qty,
7598                            p_secondary_quantity   => l_sec_tr_qty, --invconv kkillams
7599                            p_new_txn_hdr_id    => l_current_header_id,
7600                            p_new_txn_tmp_id      => get_next_temp_id,
7601                            p_clpn_id              => l_package_id,
7602                            p_item_id             => l_citem_id,
7603                            x_return_status          => l_return_status,
7604                            x_msg_count            => x_msg_count,
7605                            x_msg_data             => x_msg_data );
7606                         END IF;
7607                      IF (g_trace_on = 1) THEN log_event(' Calling InsertPH for temp_id'||l_temp_id); END IF;
7608                      IF( outbound = 'Y' ) THEN
7609                         Insert_PH(p_move_order_header_id, l_temp_id);
7610                      ELSE
7611                         Insert_PH(p_transaction_header_id, l_temp_id);
7612                      END IF;
7613                   END IF;
7614                END LOOP;
7615 
7616                IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
7617                   IF opackages%isopen THEN
7618                      CLOSE opackages;
7619                   END IF;
7620                ELSE
7621                   IF packages%isopen THEN
7622                      CLOSE packages;
7623                   END IF;
7624                END IF;
7625             END IF;  -- pack level <> 0
7626 
7627             IF( l_no_pkgs_gen = 'Y' ) THEN
7628                if (g_trace_on = 1) then log_event('no labels generated in the previous level-EXITING'); END IF;
7629                IF ( (outbound = 'Y') AND (pack_level = 0) ) THEN
7630                   IF( wct_rows%isopen) then
7631                      CLOSE wct_rows;
7632                   END IF;
7633                ELSE
7634                   IF(bpack_rows%isopen) then
7635                      CLOSE bpack_rows;
7636                   END IF;
7637                END IF;
7638                EXIT;
7639             END IF;
7640 
7641             IF ((outbound = 'Y') AND (pack_level = 0)) THEN
7642                IF wct_rows%isopen THEN
7643                   CLOSE wct_rows;
7644                END IF;
7645             ELSE
7646                IF(bpack_rows%isopen) then
7647                   CLOSE bpack_rows;
7648                END IF;
7649             END IF;
7650 
7651             pack_level := pack_level + 1;
7652 
7653             IF (g_trace_on = 1) THEN
7654                log_event(' Incremented the current level');
7655                log_event(' going back to the multi-cart loop');
7656             END IF;
7657          END LOOP; -- Ends the loop for multi level cartonization
7658 
7659          l_header_id := p_move_order_header_id;
7660 
7661       ELSE
7662                    -- Cartonization profile = 'N'
7663          NULL;
7664       END IF;
7665 
7666       IF (g_trace_on = 1) THEN log_event('return status is '||x_return_status);  END IF;
7667 
7668       IF wct_rows%isopen THEN
7669          CLOSE wct_rows;
7670       END IF;
7671 
7672       IF p_subinventory_name IS NOT NULL THEN
7673          IF (g_trace_on = 1) THEN log_event(' Updating wct with back to null where the dummy cartonization id was populated for sub restrictions : '); END IF;
7674 
7675         UPDATE wms_cartonization_temp
7676            SET cartonization_id = NULL
7677              , container_item_id = NULL
7678          WHERE subinventory_code <> p_subinventory_name
7679            AND organization_id = p_org_id
7680            AND cartonization_id = -99999
7681            AND container_item_id = -99999;
7682       END IF;
7683 
7684    EXCEPTION
7685    WHEN fnd_api.g_exc_error THEN
7686       if (g_trace_on = 1) then log_event('Rolling back to savepoint cartonize_pub'); END IF;
7687       ROLLBACK TO  cartonize_pub;
7688       x_return_status := fnd_api.g_ret_sts_success;
7689       FND_MESSAGE.SET_NAME('WMS', 'WMS_CARTONIZATION_ERROR');
7690       FND_MESSAGE.SET_TOKEN('ERROR_CODE', ERROR_CODE);
7691       FND_MSG_PUB.ADD;
7692       IF (g_trace_on = 1) THEN log_event('EXCEPTION occurred from ERROR_CODE:'||error_code); END IF;
7693       fnd_msg_pub.count_and_get
7694       (
7695          p_count  => x_msg_count,
7696          p_data   => x_msg_data
7697       );
7698 
7699       IF wct_rows%isopen THEN
7700          CLOSE wct_rows;
7701       END IF;
7702 
7703    WHEN fnd_api.g_exc_unexpected_error THEN
7704       if (g_trace_on = 1) then log_event('Exception occurred from SQLERRM:'||SQLERRM); END IF;
7705       if (g_trace_on = 1) then log_event('Exception occurred from SQLCODE:'||SQLCODE); END IF;
7706       if (g_trace_on = 1) then log_event('Rolling back to savepoint cartonize_pub'); END IF;
7707       ROLLBACK TO  cartonize_pub;
7708       x_return_status := fnd_api.g_ret_sts_unexp_error;
7709 
7710       FND_MESSAGE.SET_NAME('WMS', 'WMS_CARTONIZATION_ERROR');
7711       FND_MESSAGE.SET_TOKEN('ERROR_CODE', ERROR_CODE);
7712       FND_MSG_PUB.ADD;
7713       if (g_trace_on = 1) then log_event('Exception occurred from ERROR_CODE:'||error_code); END IF;
7714       fnd_msg_pub.count_and_get
7715       (
7716          p_count  => x_msg_count,
7717          p_data   => x_msg_data
7718       );
7719 
7720       IF wct_rows%isopen THEN
7721          CLOSE wct_rows;
7722       END IF;
7723    WHEN OTHERS  THEN
7724       ROLLBACK TO  cartonize_pub;
7725       if (g_trace_on = 1) then log_event('Rolling back to savepoint cartonize_pub'); END IF;
7726       if (g_trace_on = 1) then log_event('Exception occurred from SQLERRM:'||SQLERRM); END IF;
7727       if (g_trace_on = 1) then log_event('Exception occurred from SQLCODE:'||SQLCODE); END IF;
7728       x_return_status := fnd_api.g_ret_sts_unexp_error;
7729       ERROR_MSG := Sqlerrm;
7730       FND_MESSAGE.SET_NAME('WMS', 'WMS_CARTONIZATION_ERROR');
7731       FND_MESSAGE.SET_TOKEN('ERROR_CODE', ERROR_CODE);
7732       FND_MSG_PUB.ADD;
7733       IF (g_trace_on = 1) THEN log_event('Exception occurred from ERROR_CODE:'||error_code); END IF;
7734 
7735       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error ) THEN
7736          fnd_msg_pub.add_exc_msg
7737          ( g_pkg_name,
7738            l_api_name,
7739            ERROR_MSG
7740          );
7741       END IF;
7742 
7743       fnd_msg_pub.count_and_get
7744         (
7745           p_count  => x_msg_count,
7746           p_data   => x_msg_data
7747           );
7748 
7749       IF wct_rows%isopen THEN
7750          CLOSE wct_rows;
7751       END IF;
7752 
7753    END cartonize_mixed_item;
7754 
7755 PROCEDURE cartonize_pick_slip
7756         (
7757                 p_org_id               IN  NUMBER,
7758                 p_move_order_header_id IN  NUMBER,
7759                 p_subinventory_name    IN  VARCHAR2,
7760                 x_return_status OUT NOCOPY VARCHAR2 )
7761 IS
7762         CURSOR Pick_slip_cur
7763         IS
7764                 SELECT DISTINCT wct.pick_slip_number
7765                 FROM    wms_cartonization_temp wct,
7766                         mtl_txn_request_lines mtrl,
7767                         mtl_parameters mtlp,
7768                         mtl_secondary_inventories sub
7769                 WHERE   mtrl.header_id               = p_move_order_header_id
7770                     AND mtrl.organization_id         = p_org_id
7771                     AND wct.move_order_line_id       = mtrl.line_id
7772                     AND wct.organization_id          = mtrl.organization_id
7773                     AND mtlp.organization_id         = wct.organization_id
7774                     AND sub.organization_id          = wct.organization_id
7775                     AND sub.secondary_inventory_name = wct.subinventory_code
7776                     AND wct.cartonization_id  IS NULL
7777                     AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
7778 			OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
7779 			OR (NVL(mtlp.cartonization_flag,-1) = 4)
7780 			OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
7781 			)
7782                 ORDER BY 1 ;
7783 
7784         CURSOR Pick_slip_sub_cur
7785         IS
7786                 SELECT DISTINCT wct.pick_slip_number
7787                 FROM    wms_cartonization_temp wct,
7788                         mtl_txn_request_lines mtrl,
7789                         mtl_parameters mtlp,
7790                         mtl_secondary_inventories sub
7791                 WHERE   mtrl.header_id               = p_move_order_header_id
7792                     AND mtrl.organization_id         = p_org_id
7793                     AND wct.move_order_line_id       = mtrl.line_id
7794                     AND wct.organization_id          = mtrl.organization_id
7795                     AND wct.subinventory_code        = p_subinventory_name
7796                     AND mtlp.organization_id         = wct.organization_id
7797                     AND sub.organization_id          = wct.organization_id
7798                     AND sub.secondary_inventory_name = wct.subinventory_code
7799                     AND wct.cartonization_id  IS NULL
7800                     AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
7801 			OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
7802 			OR (NVL(mtlp.cartonization_flag,-1) = 4)
7803 			OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
7804 			)
7805                 ORDER BY 1 ;
7806 
7807         CURSOR Delivery_cur (p_pick_slip_number NUMBER)
7808         IS
7809                 SELECT DISTINCT wda.delivery_id
7810                 FROM    mtl_txn_request_lines mtrl,
7811                         wms_cartonization_temp wct,
7812                         wsh_delivery_details wdd  ,
7813                         wsh_delivery_assignments wda
7814                 WHERE   mtrl.organization_id   = p_org_id
7815                     AND mtrl.header_id         = p_move_order_header_id
7816                     AND wct.move_order_line_id = mtrl.line_id
7817                     AND wct.organization_id    = mtrl.organization_id
7818                     AND wct.pick_slip_number   = p_pick_slip_number
7819                     AND wct.cartonization_id  IS NULL
7820                     AND wct.demand_source_line = wdd.source_line_id
7821                     AND wdd.move_order_line_id = mtrl.line_id
7822                     AND wda.delivery_detail_id = wdd.delivery_detail_id
7823                     AND wda.delivery_id       IS NOT NULL
7824                 ORDER BY 1 ;
7825 
7826         CURSOR Delivery_sub_cur (p_pick_slip_number NUMBER)
7827         IS
7828                 SELECT DISTINCT wda.delivery_id
7829                 FROM    mtl_txn_request_lines mtrl,
7830                         wms_cartonization_temp wct,
7831                         wsh_delivery_details wdd  ,
7832                         wsh_delivery_assignments wda
7833                 WHERE   mtrl.organization_id   = p_org_id
7834                     AND mtrl.header_id         = p_move_order_header_id
7835                     AND wct.move_order_line_id = mtrl.line_id
7836                     AND wct.organization_id    = mtrl.organization_id
7837                     AND wct.subinventory_code  = p_subinventory_name
7838                     AND wct.pick_slip_number   = p_pick_slip_number
7839                     AND wct.cartonization_id   IS NULL
7840                     AND wct.demand_source_line = wdd.source_line_id
7841                     AND wdd.move_order_line_id = mtrl.line_id
7842                     AND wda.delivery_detail_id = wdd.delivery_detail_id
7843                     AND wda.delivery_id        IS NOT NULL
7844                 ORDER BY 1 ;
7845 
7846         l_pick_slip      NUMBER;
7847         l_carton_lpn_id  NUMBER ;
7848         l_curr_deliv     NUMBER;
7849         l_del_det_id_tab WSH_UTIL_CORE.id_tab_type;
7850         l_grouping_rows  WSH_UTIL_CORE.id_tab_type;
7851         l_return_status  VARCHAR2(100);
7852         l_msg_count      NUMBER;
7853         l_msg_data       VARCHAR2(10000);
7854         l_index          NUMBER;
7855 
7856 TYPE l_grp_rec
7857 IS
7858         RECORD
7859         (
7860                 grouping_id   NUMBER ,
7861                 carton_lpn_id NUMBER
7862         );
7863 
7864 TYPE l_grp_tb
7865 IS
7866         TABLE OF l_grp_rec INDEX BY BINARY_INTEGER;
7867 
7868         l_grp_table l_grp_tb;
7869 BEGIN
7870         IF (g_trace_on = 1) THEN
7871                 log_event('In CARTONIZE_PICK_SLIP()  with following parameters ');
7872                 log_event('p_org_id =>'|| p_org_id );
7873                 log_event('p_move_order_header_id =>'|| p_move_order_header_id );
7874                 log_event('p_subinventory_name =>'|| p_subinventory_name );
7875         END IF;
7876 
7877         IF p_subinventory_name IS NULL THEN
7878            OPEN Pick_slip_cur ;
7879         ELSE
7880            OPEN Pick_slip_sub_cur ;
7881         END IF;
7882 
7883         LOOP
7884                 IF p_subinventory_name IS NULL THEN
7885                    FETCH   Pick_slip_cur
7886                    INTO    l_pick_slip ;
7887                    EXIT  WHEN Pick_slip_cur%NOTFOUND;
7888                 ELSE
7889                    FETCH   Pick_slip_sub_cur
7890                    INTO    l_pick_slip ;
7891                    EXIT  WHEN Pick_slip_sub_cur%NOTFOUND;
7892                 END IF;
7893 
7894                 IF (g_trace_on = 1) THEN
7895                         log_event('current Pick slip number : '|| l_pick_slip );
7896                 END IF;
7897 
7898                 IF p_subinventory_name IS NULL THEN
7899                    OPEN Delivery_cur (l_pick_slip);
7900                 ELSE
7901                    OPEN Delivery_sub_cur (l_pick_slip);
7902                 END IF;
7903 
7904                 LOOP
7905                         IF p_subinventory_name IS NULL THEN
7906                             FETCH   Delivery_cur
7907                             INTO    l_curr_deliv ;
7908                             EXIT  WHEN Delivery_cur%NOTFOUND;
7909                         ELSE
7910                             FETCH   Delivery_sub_cur
7911                             INTO    l_curr_deliv ;
7912                             EXIT  WHEN Delivery_sub_cur%NOTFOUND;
7913                         END IF;
7914 
7915                         IF (g_trace_on = 1) THEN
7916                                 log_event('Generating lpn for delivery id : '|| l_curr_deliv );
7917                         END IF;
7918 
7919                         l_carton_lpn_id := get_next_package_id;
7920 
7921                         UPDATE wms_cartonization_temp
7922                         SET     cartonization_id     = l_carton_lpn_id
7923                         WHERE   transaction_temp_id IN
7924                                 (SELECT wct.transaction_temp_id
7925                                 FROM    wsh_delivery_details wdd    ,
7926                                         wms_cartonization_temp wct  ,
7927                                         wsh_delivery_assignments wda,
7928                                         mtl_txn_request_lines mtrl
7929                                 WHERE   wda.delivery_detail_id = wdd.delivery_detail_id
7930                                     AND wdd.source_line_id     = wct.demand_source_line
7931                                     AND wct.move_order_line_id = mtrl.line_id
7932                                     AND wct.cartonization_id  IS NULL
7933                                     AND wda.delivery_id        = l_curr_deliv
7934                                     AND wct.pick_slip_number   = l_pick_slip
7935                                     AND wdd.move_order_line_id = mtrl.line_id
7936                                     AND mtrl.header_id         = p_move_order_header_id
7937                                     AND mtrl.organization_id   = p_org_id
7938                                 ) ;
7939                         IF (g_trace_on = 1) THEN
7940                                 log_event('Updated carton LPN :'|| l_carton_lpn_id ||' for delivery:'||l_curr_deliv );
7941                         END IF;
7942                 END LOOP ; --Delivery cursor.
7943 
7944                 IF Delivery_cur%ISOPEN THEN
7945                    CLOSE Delivery_cur ;
7946                 ELSIF Delivery_sub_cur%ISOPEN THEN
7947                    CLOSE Delivery_sub_cur ;
7948                 END IF;
7949 
7950                 IF (g_trace_on = 1) THEN
7951                         log_event('Done with all deliveries for this pick slip number, now checking non-delivery WDDs ' );
7952                 END IF;
7953                 BEGIN --- WDDs which does not have delivery created yet.
7954                      l_index := 1 ;
7955 
7956                      IF p_subinventory_name IS NULL THEN
7957                         SELECT  wdd.delivery_detail_id BULK COLLECT
7958                         INTO    l_del_det_id_tab
7959                         FROM    mtl_txn_request_lines mtrl,
7960                                 wms_cartonization_temp wct,
7961                                 wsh_delivery_details wdd  ,
7962                                 wsh_delivery_assignments wda
7963                         WHERE   mtrl.organization_id   = p_org_id
7964                             AND mtrl.header_id         = p_move_order_header_id
7965                             AND wct.move_order_line_id = mtrl.line_id
7966                             AND wct.organization_id    = mtrl.organization_id
7967                             AND wct.pick_slip_number   = l_pick_slip
7968                             AND wct.cartonization_id  IS NULL
7969                             AND wct.demand_source_line = wdd.source_line_id
7970                             AND wdd.move_order_line_id = mtrl.line_id
7971                             AND wda.delivery_detail_id = wdd.delivery_detail_id
7972                             AND wda.delivery_id IS NULL;
7973                      ELSE
7974                         SELECT  wdd.delivery_detail_id BULK COLLECT
7975                         INTO    l_del_det_id_tab
7976                         FROM    mtl_txn_request_lines mtrl,
7977                                 wms_cartonization_temp wct,
7978                                 wsh_delivery_details wdd  ,
7979                                 wsh_delivery_assignments wda
7980                         WHERE   mtrl.organization_id   = p_org_id
7981                             AND mtrl.header_id         = p_move_order_header_id
7982                             AND wct.move_order_line_id = mtrl.line_id
7983                             AND wct.organization_id    = mtrl.organization_id
7984                             AND wct.subinventory_code  = p_subinventory_name
7985                             AND wct.pick_slip_number   = l_pick_slip
7986                             AND wct.cartonization_id  IS NULL
7987                             AND wct.demand_source_line = wdd.source_line_id
7988                             AND wdd.move_order_line_id = mtrl.line_id
7989                             AND wda.delivery_detail_id = wdd.delivery_detail_id
7990                             AND wda.delivery_id IS NULL;
7991                      END IF;
7992 
7993                      IF (g_trace_on = 1) THEN
7994                              log_event('There are '||l_del_det_id_tab.COUNT ||' non-delivery WDDs ' );
7995                      END IF;
7996 
7997                      IF (l_del_det_id_tab.COUNT > 0 ) THEN
7998                            IF (g_trace_on = 1) THEN
7999                            log_event('calling WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping' );
8000                            END IF;
8001                           WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping( p_line_rows => l_del_det_id_tab,
8002                                           x_grouping_rows => l_grouping_rows,
8003                                           x_return_status => l_return_status);
8004                                     IF l_return_status  = 'E' THEN
8005                         IF (g_trace_on = 1) THEN
8006                                log_event('API WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping returned Error' );
8007                         END IF;
8008                               x_return_status := fnd_api.g_ret_sts_unexp_error ;
8009                          RETURN;
8010                      END IF;
8011 
8012                      FOR i IN l_del_det_id_tab.FIRST .. l_del_det_id_tab.LAST
8013                      LOOP
8014                         l_carton_lpn_id := NULL ;
8015                         FOR j IN 1..l_grp_table.COUNT
8016                         LOOP
8017                            IF ( l_grouping_rows(i) = l_grp_table(j).grouping_id ) THEN
8018                               l_carton_lpn_id := l_grp_table(j).carton_lpn_id ;
8019                            END IF;
8020                         END LOOP;
8021 
8022                         IF ( l_carton_lpn_id   IS NULL ) THEN
8023                            l_carton_lpn_id                     := get_next_package_id;
8024                            l_grp_table(l_index ).grouping_id   := l_grouping_rows(i);
8025                            l_grp_table(l_index ).carton_lpn_id := l_carton_lpn_id;
8026                            l_index                             := l_index + 1 ;
8027                         END IF;
8028 
8029                         IF (g_trace_on = 1) THEN
8030                            log_event('Got carton_lpn_id :'||l_carton_lpn_id );
8031                         END IF;
8032 
8033                         UPDATE wms_cartonization_temp
8034                         SET     cartonization_id     = l_carton_lpn_id
8035                         WHERE   transaction_temp_id IN
8036                         (SELECT wct.transaction_temp_id
8037                         FROM    wsh_delivery_details wdd  ,
8038                                 wms_cartonization_temp wct,
8039                                 mtl_txn_request_lines mtrl
8040                         WHERE   wdd.delivery_detail_id = l_del_det_id_tab (i)
8041                         AND wct.pick_slip_number   = l_pick_slip
8042                         AND wdd.source_line_id     = wct.demand_source_line
8043                         AND wdd.move_order_line_id = mtrl.line_id
8044                         AND wct.move_order_line_id = mtrl.line_id
8045                         AND wct.cartonization_id  IS NULL
8046                         AND mtrl.header_id         = p_move_order_header_id
8047                         AND mtrl.organization_id   = p_org_id
8048                         ) ;
8049 
8050                         IF (g_trace_on = 1) THEN
8051                            log_event('Updated WCT for wdd :'||l_del_det_id_tab (i) );
8052                         END IF;
8053                  END LOOP ; --FOR l_del_det_id_tab records
8054 
8055 			        IF (g_trace_on = 1) THEN
8056                      log_event('Done with all non-delivery WDDs for this pick slip number ' );
8057 	              END IF;
8058 			        l_grp_table.DELETE ;
8059        			  l_del_det_id_tab.DELETE ;
8060 			END IF; --WDD table count.
8061       EXCEPTION
8062       WHEN NO_DATA_FOUND THEN
8063       IF (g_trace_on = 1) THEN
8064          log_event('All WDDs have delivery associated .' );
8065       END IF;
8066       WHEN OTHERS THEN
8067          IF (g_trace_on = 1) THEN
8068                  log_event('OTHERS Exception !!! .' );
8069          END IF;
8070          x_return_status := fnd_api.g_ret_sts_unexp_error ;
8071       END ;
8072    END LOOP ; -- Pick slip cursor.
8073    IF Pick_slip_cur%ISOPEN THEN
8074       CLOSE Pick_slip_cur;
8075    ELSIF Pick_slip_sub_cur%ISOPEN THEN
8076       CLOSE Pick_slip_sub_cur;
8077    END IF;
8078    IF (g_trace_on = 1) THEN
8079           log_event('Done with all pick slip numbers ' );
8080    END IF;
8081    x_return_status := fnd_api.g_ret_sts_success;
8082    IF (g_trace_on   = 1) THEN
8083       log_event('Cartonize_pick_slip finished normally.');
8084    END IF;
8085 EXCEPTION
8086 WHEN OTHERS THEN
8087         IF (g_trace_on = 1) THEN
8088                 log_event('Error in cartonize_pick_clip () :'||SQLERRM);
8089         END IF;
8090         x_return_status := fnd_api.g_ret_sts_unexp_error ;
8091 END cartonize_pick_slip;
8092 
8093 
8094    PROCEDURE cartonize_customer_logic
8095                   (
8096                     p_org_id                 IN          NUMBER
8097                   , p_move_order_header_id   IN          NUMBER
8098                   , p_subinventory_name      IN          VARCHAR2
8099                   , x_return_status          OUT NOCOPY  VARCHAR2
8100                   )
8101    IS
8102 
8103       TYPE l_cart_tab_type IS TABLE OF mtl_material_transactions_temp.cartonization_id%TYPE INDEX BY BINARY_INTEGER;
8104       TYPE l_trans_temp_tab_type IS TABLE OF mtl_material_transactions_temp.transaction_temp_id%TYPE INDEX BY BINARY_INTEGER;
8105 
8106       l_mmtt_table          WMS_CARTONIZATION_USER_PUB.mmtt_type;
8107       l_out_mmtt_table      WMS_CARTONIZATION_USER_PUB.mmtt_type;
8108 
8109       CURSOR Delivery_cur (p_pick_slip_number NUMBER)
8110       IS
8111          SELECT DISTINCT wda.delivery_id
8112            FROM mtl_txn_request_lines mtrl,
8113                 wms_cartonization_temp wct,
8114                 wsh_delivery_details wdd  ,
8115                 wsh_delivery_assignments wda
8116           WHERE mtrl.organization_id   = p_org_id
8117             AND mtrl.header_id         = p_move_order_header_id
8118             AND wct.move_order_line_id = mtrl.line_id
8119             AND wct.organization_id    = mtrl.organization_id
8120             AND wct.pick_slip_number   = p_pick_slip_number
8121             AND wct.cartonization_id  IS NULL
8122             AND wct.demand_source_line = wdd.source_line_id
8123             AND wdd.move_order_line_id = mtrl.line_id
8124             AND wda.delivery_detail_id = wdd.delivery_detail_id
8125             AND wda.delivery_id       IS NOT NULL
8126           ORDER BY 1 ;
8127 
8128       CURSOR Delivery_sub_cur (p_pick_slip_number NUMBER)
8129       IS
8130          SELECT DISTINCT wda.delivery_id
8131            FROM mtl_txn_request_lines mtrl,
8132                 wms_cartonization_temp wct,
8133                 wsh_delivery_details wdd  ,
8134                 wsh_delivery_assignments wda
8135           WHERE mtrl.organization_id   = p_org_id
8136             AND mtrl.header_id         = p_move_order_header_id
8137             AND wct.move_order_line_id = mtrl.line_id
8138             AND wct.organization_id    = mtrl.organization_id
8139             AND wct.subinventory_code  = p_subinventory_name
8140             AND wct.pick_slip_number   = p_pick_slip_number
8141             AND wct.cartonization_id  IS NULL
8142             AND wct.demand_source_line = wdd.source_line_id
8143             AND wdd.move_order_line_id = mtrl.line_id
8144             AND wda.delivery_detail_id = wdd.delivery_detail_id
8145             AND wda.delivery_id       IS NOT NULL
8146           ORDER BY 1 ;
8147 
8148       l_pick_slip      NUMBER;
8149       l_carton_lpn_id  NUMBER ;
8150       l_curr_deliv     NUMBER;
8151       l_del_det_id_tab WSH_UTIL_CORE.id_tab_type;
8152       l_grouping_rows  WSH_UTIL_CORE.id_tab_type;
8153       l_return_status  VARCHAR2(100);
8154       l_msg_count      NUMBER;
8155       l_msg_data       VARCHAR2(10000);
8156       l_index          NUMBER;
8157 
8158       TYPE l_grp_rec
8159       IS RECORD
8160                (
8161                       grouping_id   NUMBER ,
8162                       carton_lpn_id NUMBER
8163                );
8164 
8165       TYPE l_grp_tb
8166       IS
8167       TABLE OF l_grp_rec INDEX BY BINARY_INTEGER;
8168 
8169       l_grp_table              l_grp_tb;
8170       l_cart_tab               l_cart_tab_type;
8171       l_trans_temp_tab         l_trans_temp_tab_type;
8172       l_invalid_cartonization  EXCEPTION;
8173       l_grouping_rows_temp     NUMBER;
8174       l_carton_id_exists       VARCHAR2(1);
8175       l_cart_return_status     VARCHAR2(1);
8176       l_delivery_id_temp       NUMBER;
8177       l_carton_delivery_count  NUMBER;
8178       l_cartons_not_stamped    VARCHAR2(1) := 'Y';
8179 
8180    BEGIN
8181 
8182       IF (g_trace_on = 1) THEN
8183          log_event('In CARTONIZE_CUSTOMER_LOGIC()  with following parameters ');
8184          log_event('p_org_id =>'|| p_org_id );
8185          log_event('p_move_order_header_id =>'|| p_move_order_header_id );
8186          log_event('p_subinventory_name =>'|| p_subinventory_name );
8187       END IF;
8188 
8189       IF p_subinventory_name IS NOT NULL THEN
8190 
8191          SELECT wct.* BULK COLLECT INTO l_mmtt_table
8192           FROM wms_cartonization_temp wct,
8193                mtl_txn_request_lines mtrl,
8194                mtl_secondary_inventories sub,
8195                mtl_parameters mtlp
8196          WHERE wct.move_order_line_id =mtrl.line_id
8197            AND mtrl.header_id = p_move_order_header_id
8198            AND mtlp.organization_id = wct.organization_id
8199            AND sub.organization_id = wct.organization_id
8200            AND sub.secondary_inventory_name = wct.subinventory_code
8201            AND wct.subinventory_code = p_subinventory_name
8202            AND wct.organization_id = p_org_id
8203            AND wct.cartonization_id IS NULL
8204            AND wct.transfer_lpn_id IS NULL
8205            AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
8206 		OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
8207 		OR (NVL(mtlp.cartonization_flag,-1) = 4)
8208 		OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
8209 		)
8210            ORDER BY wct.move_order_line_id,
8211                      wct.inventory_item_id,
8212                      Abs(wct.transaction_temp_id);
8213       ELSE
8214 
8215          SELECT wct.* BULK COLLECT INTO l_mmtt_table
8216           FROM wms_cartonization_temp wct,
8217                mtl_txn_request_lines mtrl,
8218                mtl_secondary_inventories sub,
8219                mtl_parameters mtlp
8220          WHERE wct.move_order_line_id =mtrl.line_id
8221            AND mtrl.header_id = p_move_order_header_id
8222            AND mtlp.organization_id = wct.organization_id
8223            AND sub.secondary_inventory_name = wct.subinventory_code
8224            AND sub.organization_id = wct.organization_id
8225            AND wct.organization_id = p_org_id
8226            AND wct.cartonization_id IS NULL
8227            AND wct.transfer_lpn_id IS NULL
8228            AND ((Nvl(mtlp.cartonization_flag,-1) = 1)
8229 		OR (Nvl(mtlp.cartonization_flag,-1) = 3 AND sub.cartonization_flag = 1)
8230 		OR (NVL(mtlp.cartonization_flag,-1) = 4)
8231 		OR (NVL(mtlp.cartonization_flag,-1) = 5 AND sub.cartonization_flag = 1)
8232 		)
8233            ORDER BY wct.move_order_line_id,
8234                      wct.inventory_item_id,
8235                      Abs(wct.transaction_temp_id);
8236 
8237       END IF;
8238 
8239       IF (g_trace_on = 1) THEN
8240          log_event('l_mmtt_table.COUNT  : ' || l_mmtt_table.COUNT);
8241       END IF;
8242 
8243       WMS_CARTONIZATION_USER_PUB.CARTONIZE (
8244                                      x_return_status    => l_cart_return_status
8245                                    , x_msg_count        => l_msg_count
8246                                    , x_msg_data         => l_msg_data
8247                                    , x_task_table       => l_out_mmtt_table
8248                                    , p_organization_id  => p_org_id
8249                                    , p_task_table       => l_mmtt_table
8250                                     );
8251 
8252       IF (g_trace_on = 1) THEN
8253          log_event('l_out_mmtt_table.COUNT  : ' || l_out_mmtt_table.COUNT);
8254       END IF;
8255 
8256       IF NVL(l_cart_return_status,'E') = 'E'
8257       OR l_mmtt_table.COUNT <> l_out_mmtt_table.COUNT THEN
8258          IF (g_trace_on = 1) THEN
8259             log_event('WMS_CARTONIZATION_USER_PUB.cartonize() returned failure, exiting cartonize_customer_logic() ');
8260          END IF;
8261          x_return_status := 'E';
8262          RETURN;
8263       END IF;
8264 
8265       -- loop to get the distinct cartonization ids
8266 --      l_cart_tab(1) := l_out_mmtt_table(1).cartonization_id;
8267 
8268       l_cart_tab.DELETE;
8269 
8270       IF (g_trace_on = 1) THEN
8271          log_event('Reached SAVEPOINT CUSTOMER_LOGIC_SP');
8272       END IF;
8273 
8274       SAVEPOINT CUSTOMER_LOGIC_SP;
8275 
8276       FOR i IN l_out_mmtt_table.FIRST .. l_out_mmtt_table.LAST LOOP
8277 
8278          IF l_out_mmtt_table(i).cartonization_id IS NOT NULL THEN
8279 
8280             IF l_cart_tab.COUNT = 0 THEN
8281                l_cart_tab(1) := l_out_mmtt_table(i).cartonization_id;
8282             END IF;
8283 
8284             l_cartons_not_stamped := 'N';
8285 
8286             UPDATE wms_cartonization_temp
8287                SET cartonization_id    = l_out_mmtt_table(i).cartonization_id
8288                  , container_item_id   = l_out_mmtt_table(i).container_item_id
8289              WHERE transaction_temp_id = l_out_mmtt_table(i).transaction_temp_id;
8290 
8291             l_carton_id_exists := 'N';
8292 
8293             FOR j IN l_cart_tab.FIRST .. l_cart_tab.LAST LOOP
8294                IF l_out_mmtt_table(i).cartonization_id = l_cart_tab(j) THEN
8295                   l_carton_id_exists := 'Y';
8296                   EXIT;
8297                END IF;
8298             END LOOP;
8299             IF l_carton_id_exists = 'N' THEN
8300                l_cart_tab(l_cart_tab.COUNT + 1) := l_out_mmtt_table(i).cartonization_id;
8301             END IF;
8302          END IF;
8303 
8304       END LOOP;
8305 
8306       IF (g_trace_on = 1) THEN
8307          log_event('Updated WCT rows with customer logic cartonization ids');
8308       END IF;
8309 
8310       IF l_cartons_not_stamped = 'Y' THEN
8311          IF (g_trace_on = 1) THEN
8312             log_event('The customer did not stamp cartonization_id on any task, returning');
8313          END IF;
8314          RAISE L_INVALID_CARTONIZATION;
8315       END IF;
8316 
8317       FOR i IN l_cart_tab.FIRST .. l_cart_tab.LAST LOOP
8318 
8319          l_del_det_id_tab.DELETE;
8320 
8321          IF (g_trace_on = 1) THEN
8322             log_event('Getting WDD rows for cartonization_id : '|| l_cart_tab(i));
8323          END IF;
8324 
8325          SELECT DISTINCT wdd.delivery_detail_id BULK COLLECT
8326            INTO l_del_det_id_tab
8327            FROM wsh_delivery_details wdd,
8328                 wms_cartonization_temp wct,
8329                 mtl_txn_request_lines mtrl
8330           WHERE wct.move_order_line_id = mtrl.line_id
8331             AND mtrl.header_id = p_move_order_header_id
8332             AND wct.organization_id = mtrl.organization_id
8333             AND wct.demand_source_line = wdd.source_line_id
8334             AND wct.organization_id = p_org_id
8335             AND wdd.move_order_line_id = mtrl.line_id
8336             AND wct.cartonization_id = l_cart_tab(i);
8337 
8338          IF (g_trace_on = 1) THEN
8339             FOR j IN l_del_det_id_tab.FIRST .. l_del_det_id_tab.LAST LOOP
8340                log_event('l_del_det_id_tab ('|| i ||') : '|| l_del_det_id_tab(j));
8341             END LOOP;
8342          END IF;
8343 
8344          IF (l_del_det_id_tab.COUNT > 0 ) THEN
8345 
8346             IF (g_trace_on = 1) THEN
8347                log_event('calling WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping' );
8348             END IF;
8349 
8350             WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping
8351                                               (
8352                                               p_line_rows     => l_del_det_id_tab,
8353                                               x_grouping_rows => l_grouping_rows,
8354                                               x_return_status => l_return_status
8355                                               );
8356 
8357             IF NVL(l_return_status,'E')  = 'E' THEN
8358                IF (g_trace_on = 1) THEN
8359                       log_event('API WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping returned Error' );
8360                END IF;
8361                RAISE L_INVALID_CARTONIZATION;
8362             END IF;
8363 
8364             IF (g_trace_on = 1) THEN
8365                log_event('Grouping rows returned for cartonization_id : '|| l_cart_tab(i));
8366                FOR j IN l_grouping_rows.FIRST .. l_grouping_rows.LAST LOOP
8367                   log_event('l_grouping_rows ( '|| i || ' ) : ' || l_grouping_rows(j));
8368                END LOOP;
8369             END IF;
8370 
8371             l_grouping_rows_temp := l_grouping_rows(1);
8372             FOR j IN l_grouping_rows.FIRST .. l_grouping_rows.LAST LOOP
8373                IF l_grouping_rows_temp <> l_grouping_rows(j) THEN
8374                   log_event('Tasks associated with cartonization_id '|| l_cart_tab(i)|| ' cannot be shipped together');
8375                   RAISE L_INVALID_CARTONIZATION;
8376                END IF;
8377             END LOOP;
8378          END IF;
8379       END LOOP;
8380 
8381       IF (g_trace_on = 1) THEN
8382          log_event ('Suggest containers by customer algorithm are verified successfully.');
8383          log_event ('Exiting normally from cartonize_customer_logic()');
8384       END IF;
8385 
8386    EXCEPTION
8387    WHEN L_INVALID_CARTONIZATION THEN
8388       x_return_status := fnd_api.g_ret_sts_unexp_error ;
8389       IF (g_trace_on = 1) THEN
8390          log_event('Unable to cartonize, ROLLING BACK to CUSTOMER_LOGIC_SP and exiting from cartonize_customer_logic');
8391       END IF;
8392       ROLLBACK TO CUSTOMER_LOGIC_SP;
8393    WHEN OTHERS THEN
8394 
8395       x_return_status := fnd_api.g_ret_sts_unexp_error ;
8396       IF (g_trace_on = 1) THEN
8397          log_event('Unexpected error in cartonize_customer_logic(), rolling back to CUSTOMER_LOGIC_SP');
8398          log_event('SQLCODE : ' || SQLCODE);
8399          log_event('SQLERRM : ' || SQLERRM);
8400       END IF;
8401       ROLLBACK TO CUSTOMER_LOGIC_SP;
8402 
8403    END cartonize_customer_logic;
8404 
8405 
8406    PROCEDURE cartonize_default_logic (
8407                       p_org_id                IN    NUMBER,
8408                       p_move_order_header_id  IN    NUMBER,
8409                       p_out_bound             IN    VARCHAR2,
8410                       x_return_status         OUT   NOCOPY VARCHAR2,
8411                       x_msg_count             OUT   NOCOPY NUMBER,
8412                       x_msg_data              OUT   NOCOPY VARCHAR2
8413    )
8414    IS
8415       l_api_return_status VARCHAR2(1);
8416 
8417    BEGIN
8418 
8419       IF (g_trace_on = 1) THEN
8420          log_event ('IN CARTONIZE_DEFAULT_LOGIC()');
8421          log_event ('calling procedure CARTONIZE_SINGLE_ITEM()');
8422       END IF;
8423 
8424       cartonize_single_item
8425       (
8426          x_return_status         => l_api_return_status,
8427          x_msg_count             => x_msg_count,
8428          x_msg_data              => x_msg_data,
8429          p_out_bound             => 'Y',
8430          p_org_id                => p_org_id,
8431          p_move_order_header_id  => p_move_order_header_id
8432       );
8433 
8434       IF (g_trace_on = 1) THEN
8435          log_event ('calling procedure CARTONIZE_MIXED_ITEM()');
8436       END IF;
8437 
8438       cartonize_mixed_item
8439       (
8440          x_return_status         => l_api_return_status,
8441          x_msg_count             => x_msg_count,
8442          x_msg_data              => x_msg_data,
8443          p_out_bound             => 'Y',
8444          p_org_id                => p_org_id,
8445          p_move_order_header_id  => p_move_order_header_id,
8446          p_transaction_header_id => NULL,
8447          p_stop_level            => 0, -- For first level only
8448          p_pack_level            => 0
8449       );
8450 
8451       IF (g_trace_on = 1) THEN log_event ('Returing normally from CARTONIZE_DEFAULT_LOGIC()'); END IF;
8452       x_return_status := 'S';
8453    EXCEPTION
8454    WHEN OTHERS THEN
8455       IF (g_trace_on = 1) THEN log_event ('SQL Error Message : '|| SQLERRM); END IF;
8456       IF (g_trace_on = 1) THEN log_event ('SQL Error Code : '|| SQLCODE); END IF;
8457       x_return_status := 'E';
8458    END;
8459 
8460 PROCEDURE insert_ph
8461          (
8462              p_move_order_header_id IN NUMBER,
8463              p_current_header_id    IN NUMBER,
8464              x_return_status        OUT NOCOPY NUMBER
8465           )
8466 IS
8467 
8468    L_PREV_PACKAGE_ID       NUMBER;
8469    L_PREV_HEADER_ID        NUMBER;
8470    L_CURRENT_HEADER_ID     NUMBER;
8471    l_qty                   NUMBER := NULL;
8472    l_tr_qty                NUMBER := NULL;
8473    l_sec_tr_qty            NUMBER := NULL;
8474    l_clpn_id               NUMBER := NULL;
8475    l_citem_id              NUMBER := NULL;
8476    l_package_id            NUMBER := NULL;
8477    l_upd_qty_flag          VARCHAR2(1) := NULL;
8478    L_no_pkgs_gen           VARCHAR2(1);
8479    l_temp_id               NUMBER;
8480    l_item_id               NUMBER;
8481    l_return_status         VARCHAR2(1);
8482    l_msg_count             NUMBER;
8483    l_msg_data              VARCHAR2(2000);
8484 
8485 
8486    CURSOR opackages(p_hdr_id NUMBER) IS
8487    SELECT
8488    wct.transaction_temp_id,
8489    wct.inventory_item_id,
8490    wct.primary_quantity,
8491    wct.transaction_quantity,
8492    wct.secondary_transaction_quantity, --invconv kkillams
8493    wct.content_lpn_id,
8494    wct.container_item_id,
8495    wct.cartonization_id
8496    FROM
8497    wms_cartonization_temp wct,
8498    mtl_txn_request_lines mtrl
8499    WHERE
8500    wct.move_order_line_id = mtrl.line_id AND
8501    mtrl.header_id = p_move_order_header_id
8502    ORDER BY wct.cartonization_id;
8503 
8504 BEGIN
8505 
8506    IF (g_trace_on = 1) THEN log_event('Coming in my insert_ph'); END IF;
8507    IF (g_trace_on = 1) THEN log_event('Pack_level' || pack_level); END IF;
8508    l_prev_package_id := -1;
8509    l_prev_header_id := p_current_header_id;
8510    l_current_header_id := get_next_header_id;
8511    curr_header_id_for_mixed := l_current_header_id;
8512 
8513    IF (g_trace_on = 1) THEN log_event('Setting curr_header_id_for_mixed : ' || curr_header_id_for_mixed); END IF;
8514 
8515 
8516    t_lpn_alloc_flag_table.DELETE;
8517 
8518    OPEN opackages(l_prev_header_id);
8519    l_no_pkgs_gen := 'Y';
8520 
8521    LOOP
8522 
8523       IF (g_trace_on = 1) THEN
8524          log_event('Fetching Packages cursor ');
8525       END IF;
8526 
8527       error_code := 'CARTONIZE 240';
8528 
8529       FETCH opackages INTO l_temp_id,l_item_id, l_qty, l_tr_qty,l_sec_tr_qty, l_clpn_id, l_citem_id, l_package_id;
8530       EXIT WHEN opackages%notfound;
8531 
8532       IF (g_trace_on = 1) THEN
8533          log_event('temp_id '||l_temp_id );
8534          log_event('item_id  '||l_item_id );
8535          log_event('qty  '||l_qty );
8536          log_event('tr_qty '||l_tr_qty);
8537          log_event('sec_tr_qty '||l_sec_tr_qty);
8538          log_event('clpn_id '||l_clpn_id);
8539          log_event('citem_id '||l_citem_id);
8540          log_event('package_id '||l_package_id);
8541       END IF;
8542 
8543       IF( l_package_id IS NOT NULL ) THEN
8544 
8545          l_no_pkgs_gen := 'N';
8546          IF( l_package_id <> l_prev_package_id ) THEN
8547             l_prev_package_id := l_package_id;
8548 
8549             IF (g_trace_on = 1) THEN
8550                log_event(' Inserting a new row for package '||l_package_id);
8551             END IF;
8552 
8553             insert_mmtt
8554             (
8555             p_transaction_temp_id  => l_temp_id,
8556             p_primary_quantity     => l_qty,
8557             p_transaction_quantity => l_tr_qty,
8558             p_secondary_quantity   => l_sec_tr_qty, --invconv kkillams
8559             p_new_txn_hdr_id    => l_current_header_id,
8560             p_new_txn_tmp_id      => get_next_temp_id,
8561             p_clpn_id              => l_package_id,
8562             p_item_id             => l_citem_id,
8563             x_return_status          => l_return_status,
8564             x_msg_count            => l_msg_count,
8565             x_msg_data             => l_msg_data
8566             );
8567          END IF;
8568 
8569          IF (g_trace_on = 1) THEN
8570             log_event(' Calling InsertPH for temp_id'||l_temp_id);
8571          END IF;
8572 
8573          insert_ph(p_move_order_header_id, l_temp_id);
8574 
8575       END IF;
8576    END LOOP;
8577 
8578    IF opackages%isopen THEN
8579       CLOSE opackages;
8580    END IF;
8581 
8582 END insert_ph;
8583 
8584 
8585 
8586 END WMS_CARTNZN_PUB;